1 /* $NetBSD: rpc_main.c,v 1.44 2015/09/20 16:57:13 kamil Exp $ */ 2 3 /* 4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 5 * unrestricted use provided that this legend is included on all tape 6 * media and as a part of the software program in whole or part. Users 7 * may copy or modify Sun RPC without charge, but are not authorized 8 * to license or distribute it to anyone else except as part of a product or 9 * program developed by the user or with the express written consent of 10 * Sun Microsystems, Inc. 11 * 12 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 13 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 14 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 15 * 16 * Sun RPC is provided with no support and without any obligation on the 17 * part of Sun Microsystems, Inc. to assist in its use, correction, 18 * modification or enhancement. 19 * 20 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 21 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 22 * OR ANY PART THEREOF. 23 * 24 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 25 * or profits or other special, indirect and consequential damages, even if 26 * Sun has been advised of the possibility of such damages. 27 * 28 * Sun Microsystems, Inc. 29 * 2550 Garcia Avenue 30 * Mountain View, California 94043 31 */ 32 33 #if HAVE_NBTOOL_CONFIG_H 34 #include "nbtool_config.h" 35 #endif 36 37 #include <sys/cdefs.h> 38 #if defined(__RCSID) && !defined(lint) 39 #if 0 40 static char sccsid[] = "@(#)rpc_main.c 1.30 89/03/30 (C) 1987 SMI"; 41 #else 42 __RCSID("$NetBSD: rpc_main.c,v 1.44 2015/09/20 16:57:13 kamil Exp $"); 43 #endif 44 #endif 45 46 /* 47 * rpc_main.c, Top level of the RPC protocol compiler. 48 */ 49 50 #define RPCGEN_VERSION "199506"/* This program's version (year & month) */ 51 52 #include <sys/types.h> 53 #include <sys/param.h> 54 #include <sys/file.h> 55 #include <sys/stat.h> 56 #include <ctype.h> 57 #include <err.h> 58 #include <stdio.h> 59 #include <stdlib.h> 60 #include <string.h> 61 #include <unistd.h> 62 #include "rpc_scan.h" 63 #include "rpc_parse.h" 64 #include "rpc_util.h" 65 66 #define EXTEND 1 /* alias for TRUE */ 67 #define DONT_EXTEND 0 /* alias for FALSE */ 68 69 struct commandline { 70 int cflag; /* xdr C routines */ 71 int hflag; /* header file */ 72 int lflag; /* client side stubs */ 73 int mflag; /* server side stubs */ 74 int nflag; /* netid flag */ 75 int sflag; /* server stubs for the given transport */ 76 int tflag; /* dispatch Table file */ 77 int Ssflag; /* produce server sample code */ 78 int Scflag; /* produce client sample code */ 79 char *infile; /* input module name */ 80 char *outfile; /* output module name */ 81 }; 82 83 84 static char *cmdname; 85 86 static const char *svcclosetime = "120"; 87 static const char *CPP; 88 static char CPPFLAGS[] = "-C"; 89 static char pathbuf[MAXPATHLEN + 1]; 90 91 /* these cannot be const */ 92 static char allv0[] = "rpcgen"; 93 static char allv1[] = "-s"; 94 static char allv2[] = "udp"; 95 static char allv3[] = "-s"; 96 static char allv4[] = "tcp"; 97 static char *allv[] = { 98 allv0, 99 allv1, 100 allv2, 101 allv3, 102 allv4, 103 }; 104 static int allc = sizeof(allv) / sizeof(allv[0]); 105 106 /* these cannot be const */ 107 static char allnv0[] = "rpcgen"; 108 static char allnv1[] = "-s"; 109 static char allnv2[] = "netpath"; 110 static char *allnv[] = { 111 allnv0, 112 allnv1, 113 allnv2, 114 }; 115 static int allnc = sizeof(allnv) / sizeof(allnv[0]); 116 117 #define ARGLISTLEN 20 118 #define FIXEDARGS 2 119 120 static const char *arglist[ARGLISTLEN]; 121 static int argcount = FIXEDARGS; 122 123 124 int nonfatalerrors; /* errors */ 125 int inetdflag /* = 1 */ ; /* Support for inetd *//* is now the default */ 126 int pmflag; /* Support for port monitors */ 127 int logflag; /* Use syslog instead of fprintf for errors */ 128 int tblflag; /* Support for dispatch table file */ 129 int BSDflag; /* use BSD cplusplus macros */ 130 int callerflag; /* Generate svc_caller() function */ 131 int docleanup = 1; /* cause atexit to remove files */ 132 133 #define INLINE 3 134 /*length at which to start doing an inline */ 135 136 int doinline = INLINE; /* length at which to start doing an inline. 3 137 * = default if 0, no xdr_inline code */ 138 139 int indefinitewait; /* If started by port monitors, hang till it 140 * wants */ 141 int exitnow; /* If started by port monitors, exit after the 142 * call */ 143 int timerflag; /* TRUE if !indefinite && !exitnow */ 144 int newstyle; /* newstyle of passing arguments (by value) */ 145 int Mflag = 0; /* multithread safe */ 146 static int allfiles; /* generate all files */ 147 int tirpcflag = 1; /* generating code for tirpc, by default */ 148 149 #ifdef __MSDOS__ 150 static char *dos_cppfile = NULL; 151 #endif 152 153 static char *extendfile(const char *, const char *); 154 static void open_output(const char *, const char *); 155 static void add_warning(void); 156 static void clear_args(void); 157 static void open_input(const char *, const char *); 158 static int check_nettype(const char *, const char *[]); 159 static void c_output(const char *, const char *, int, const char *); 160 static void c_initialize(void); 161 static char *generate_guard(const char *); 162 static void h_output(const char *, const char *, int, const char *); 163 static void s_output(int, char *[], char *, const char *, int, const char *, 164 int, int); 165 static void l_output(const char *, const char *, int, const char *); 166 static void t_output(const char *, const char *, int, const char *); 167 static void svc_output(const char *, const char *, int, const char *); 168 static void clnt_output(const char *, const char *, int, const char *); 169 static int do_registers(int, char *[]); 170 static void addarg(const char *); 171 static void putarg(int, const char *); 172 static void checkfiles(const char *, const char *); 173 static int parseargs(int, char *[], struct commandline *); 174 static void usage(void) __dead; 175 static void options_usage(void) __dead; 176 177 int 178 main(int argc, char *argv[]) 179 { 180 struct commandline cmd; 181 182 setprogname(argv[0]); 183 if ((CPP = getenv("RPCGEN_CPP")) == NULL) { 184 CPP = "/usr/bin/cpp"; 185 if (access(CPP, X_OK)) 186 CPP = "/usr/bin/clang-cpp"; 187 } 188 189 (void) memset((char *) &cmd, 0, sizeof(struct commandline)); 190 clear_args(); 191 atexit(crash); 192 if (!parseargs(argc, argv, &cmd)) 193 usage(); 194 195 if (cmd.cflag || cmd.hflag || cmd.lflag || cmd.tflag || cmd.sflag || 196 cmd.mflag || cmd.nflag || cmd.Ssflag || cmd.Scflag) { 197 checkfiles(cmd.infile, cmd.outfile); 198 } else 199 checkfiles(cmd.infile, NULL); 200 201 if (cmd.cflag) { 202 c_output(cmd.infile, "-DRPC_XDR", DONT_EXTEND, cmd.outfile); 203 } else 204 if (cmd.hflag) { 205 h_output(cmd.infile, "-DRPC_HDR", DONT_EXTEND, cmd.outfile); 206 } else 207 if (cmd.lflag) { 208 l_output(cmd.infile, "-DRPC_CLNT", DONT_EXTEND, cmd.outfile); 209 } else 210 if (cmd.sflag || cmd.mflag || (cmd.nflag)) { 211 s_output(argc, argv, cmd.infile, "-DRPC_SVC", DONT_EXTEND, 212 cmd.outfile, cmd.mflag, cmd.nflag); 213 } else 214 if (cmd.tflag) { 215 t_output(cmd.infile, "-DRPC_TBL", DONT_EXTEND, cmd.outfile); 216 } else 217 if (cmd.Ssflag) { 218 svc_output(cmd.infile, "-DRPC_SERVER", DONT_EXTEND, cmd.outfile); 219 } else 220 if (cmd.Scflag) { 221 clnt_output(cmd.infile, "-DRPC_CLIENT", DONT_EXTEND, cmd.outfile); 222 } else { 223 /* the rescans 224 * are 225 * required, 226 * since cpp 227 * may effect 228 * input */ 229 c_output(cmd.infile, "-DRPC_XDR", EXTEND, "_xdr.c"); 230 reinitialize(); 231 h_output(cmd.infile, "-DRPC_HDR", EXTEND, ".h"); 232 reinitialize(); 233 l_output(cmd.infile, "-DRPC_CLNT", EXTEND, "_clnt.c"); 234 reinitialize(); 235 if (inetdflag || !tirpcflag) 236 s_output(allc, allv, cmd.infile, "-DRPC_SVC", EXTEND, 237 "_svc.c", cmd.mflag, cmd.nflag); 238 else 239 s_output(allnc, allnv, cmd.infile, "-DRPC_SVC", 240 EXTEND, "_svc.c", cmd.mflag, cmd.nflag); 241 if (tblflag) { 242 reinitialize(); 243 t_output(cmd.infile, "-DRPC_TBL", EXTEND, "_tbl.i"); 244 } 245 if (allfiles) { 246 reinitialize(); 247 svc_output(cmd.infile, "-DRPC_SERVER", EXTEND, "_server.c"); 248 } 249 if (allfiles) { 250 reinitialize(); 251 clnt_output(cmd.infile, "-DRPC_CLIENT", EXTEND, "_client.c"); 252 } 253 } 254 #ifdef __MSDOS__ 255 if (dos_cppfile != NULL) { 256 (void) fclose(fin); 257 (void) unlink(dos_cppfile); 258 } 259 #endif 260 docleanup = 0; 261 exit(nonfatalerrors); 262 /* NOTREACHED */ 263 } 264 /* 265 * add extension to filename 266 */ 267 static char * 268 extendfile(const char *path, const char *ext) 269 { 270 const char *file; 271 char *res; 272 const char *p; 273 274 if ((file = strrchr(path, '/')) == NULL) 275 file = path; 276 else 277 file++; 278 279 res = alloc(strlen(file) + strlen(ext) + 1); 280 if (res == NULL) { 281 err(EXIT_FAILURE, "Out of memory"); 282 } 283 p = strrchr(file, '.'); 284 if (p == NULL) { 285 p = file + strlen(file); 286 } 287 (void) strcpy(res, file); 288 (void) strcpy(res + (p - file), ext); 289 return (res); 290 } 291 /* 292 * Open output file with given extension 293 */ 294 static void 295 open_output(const char *infile, const char *outfile) 296 { 297 298 if (outfile == NULL) { 299 fout = stdout; 300 return; 301 } 302 if (infile != NULL && streq(outfile, infile)) { 303 errx(EXIT_FAILURE, "Output would overwrite `%s'", infile); 304 } 305 fout = fopen(outfile, "w"); 306 if (fout == NULL) { 307 err(EXIT_FAILURE, "Can't open `%s'", outfile); 308 } 309 record_open(outfile); 310 311 } 312 313 static void 314 add_warning(void) 315 { 316 f_print(fout, "/*\n"); 317 f_print(fout, " * Please do not edit this file.\n"); 318 f_print(fout, " * It was generated using rpcgen.\n"); 319 f_print(fout, " */\n\n"); 320 } 321 322 /* clear list of arguments */ 323 static void 324 clear_args(void) 325 { 326 int i; 327 for (i = FIXEDARGS; i < ARGLISTLEN; i++) 328 arglist[i] = NULL; 329 argcount = FIXEDARGS; 330 } 331 332 /* 333 * Open input file with given define for C-preprocessor 334 */ 335 static void 336 open_input(const char *infile, const char *define) 337 { 338 int pd[2]; 339 340 infilename = (infile == NULL) ? "<stdin>" : infile; 341 #ifdef __MSDOS__ 342 #define DOSCPP "\\prog\\bc31\\bin\\cpp.exe" 343 { 344 int retval; 345 char drive[MAXDRIVE], dir[MAXDIR], name[MAXFILE], ext[MAXEXT]; 346 char cppfile[MAXPATH]; 347 char *cpp; 348 349 if ((cpp = getenv("RPCGEN_CPP")) == NULL && 350 (cpp = searchpath("cpp.exe")) == NULL) 351 cpp = DOSCPP; 352 353 putarg(0, cpp); 354 putarg(1, "-P-"); 355 putarg(2, CPPFLAGS); 356 addarg(define); 357 addarg(infile); 358 addarg(NULL); 359 360 retval = spawnvp(P_WAIT, arglist[0], arglist); 361 if (retval != 0) { 362 err(EXIT_FAILURE, "C preprocessor failed"); 363 } 364 fnsplit(infile, drive, dir, name, ext); 365 fnmerge(cppfile, drive, dir, name, ".i"); 366 367 fin = fopen(cppfile, "r"); 368 if (fin == NULL) { 369 err(EXIT_FAILURE, "Can't open `%s'", cppfile); 370 } 371 dos_cppfile = strdup(cppfile); 372 if (dos_cppfile == NULL) { 373 err(EXIT_FAILURE, "Can't copy `%s'", cppfile); 374 } 375 } 376 #else 377 (void) pipe(pd); 378 switch (fork()) { 379 case 0: 380 putarg(0, CPP); 381 putarg(1, CPPFLAGS); 382 addarg(define); 383 addarg(infile); 384 addarg(NULL); 385 (void) close(1); 386 (void) dup2(pd[1], 1); 387 (void) close(pd[0]); 388 execvp(arglist[0], __UNCONST(arglist)); 389 err(EXIT_FAILURE, "$RPCGEN_CPP: %s", CPP); 390 case -1: 391 err(EXIT_FAILURE, "fork"); 392 } 393 (void) close(pd[1]); 394 fin = fdopen(pd[0], "r"); 395 #endif 396 if (fin == NULL) { 397 err(EXIT_FAILURE, "Can't open `%s'", infilename); 398 } 399 } 400 /* valid tirpc nettypes */ 401 static const char *valid_ti_nettypes[] = 402 { 403 "netpath", 404 "visible", 405 "circuit_v", 406 "datagram_v", 407 "circuit_n", 408 "datagram_n", 409 "udp", 410 "tcp", 411 "raw", 412 NULL 413 }; 414 /* valid inetd nettypes */ 415 static const char *valid_i_nettypes[] = 416 { 417 "udp", 418 "tcp", 419 NULL 420 }; 421 422 static int 423 check_nettype(const char *name, const char *list_to_check[]) 424 { 425 int i; 426 for (i = 0; list_to_check[i] != NULL; i++) { 427 if (strcmp(name, list_to_check[i]) == 0) { 428 return 1; 429 } 430 } 431 f_print(stderr, "illegal nettype :\'%s\'\n", name); 432 return 0; 433 } 434 /* 435 * Compile into an XDR routine output file 436 */ 437 438 static void 439 c_output(const char *infile, const char *define, int extend, 440 const char *outfile) 441 { 442 definition *def; 443 char *include; 444 const char *outfilename; 445 long tell; 446 447 c_initialize(); 448 open_input(infile, define); 449 outfilename = extend ? extendfile(infile, outfile) : outfile; 450 open_output(infile, outfilename); 451 add_warning(); 452 if (infile && (include = extendfile(infile, ".h"))) { 453 f_print(fout, "#include \"%s\"\n", include); 454 free(include); 455 /* .h file already contains rpc/rpc.h */ 456 } else 457 f_print(fout, "#include <rpc/rpc.h>\n"); 458 tell = ftell(fout); 459 while ((def = get_definition()) != NULL) { 460 emit(def); 461 } 462 if (extend && tell == ftell(fout)) { 463 (void) unlink(outfilename); 464 } 465 } 466 467 468 static void 469 c_initialize(void) 470 { 471 472 /* add all the starting basic types */ 473 474 add_type(1, "int"); 475 add_type(1, "long"); 476 add_type(1, "short"); 477 add_type(1, "bool"); 478 479 add_type(1, "u_int"); 480 add_type(1, "u_long"); 481 add_type(1, "u_short"); 482 483 } 484 485 const char rpcgen_table_dcl[] = "struct rpcgen_table {\n\ 486 char *(*proc)();\n\ 487 xdrproc_t xdr_arg;\n\ 488 unsigned len_arg;\n\ 489 xdrproc_t xdr_res;\n\ 490 unsigned len_res;\n\ 491 };\n"; 492 493 494 static char * 495 generate_guard(const char *pathname) 496 { 497 const char *filename; 498 char *guard, *tmp, *tmp2, *extdot; 499 500 filename = strrchr(pathname, '/'); /* find last component */ 501 filename = ((filename == 0) ? pathname : filename + 1); 502 guard = strdup(filename); 503 if (guard == NULL) { 504 err(EXIT_FAILURE, "strdup"); 505 } 506 extdot = strrchr(guard, '.'); 507 508 /* 509 * Convert to valid C symbol name and make it upper case. 510 * Map non alphanumerical characters to '_'. 511 * 512 * Leave extension as it is. It will be handled in extendfile(). 513 */ 514 for (tmp = guard; *tmp; tmp++) { 515 if (islower((unsigned char)*tmp)) 516 *tmp = toupper((unsigned char)*tmp); 517 else if (isupper((unsigned char)*tmp)) 518 continue; 519 else if (isdigit((unsigned char)*tmp)) 520 continue; 521 else if (*tmp == '_') 522 continue; 523 else if (tmp == extdot) 524 break; 525 else 526 *tmp = '_'; 527 } 528 529 /* 530 * Can't have a '_' or '.' at the front of a symbol name, beacuse it 531 * will end up as "__". 532 * 533 * Prefix it with "RPCGEN_". 534 */ 535 if (guard[0] == '_' || guard[0] == '.') { 536 if (asprintf(&tmp2, "RPCGEN_%s", guard) == -1) { 537 err(EXIT_FAILURE, "asprintf"); 538 } 539 free(guard); 540 guard = tmp2; 541 } 542 543 /* Replace the file extension */ 544 tmp2 = extendfile(guard, "_H_RPCGEN"); 545 free(guard); 546 guard = tmp2; 547 548 return (guard); 549 } 550 551 /* 552 * Compile into an XDR header file 553 */ 554 static void 555 h_output(const char *infile, const char *define, int extend, 556 const char *outfile) 557 { 558 definition *def; 559 const char *outfilename; 560 long tell; 561 char *guard; 562 list *l; 563 int did; 564 565 open_input(infile, define); 566 outfilename = extend ? extendfile(infile, outfile) : outfile; 567 open_output(infile, outfilename); 568 add_warning(); 569 if (outfilename || infile) 570 guard = generate_guard(outfilename ? outfilename : infile); 571 else { 572 guard = strdup("STDIN_"); 573 if (guard == NULL) { 574 err(EXIT_FAILURE, "strdup"); 575 } 576 } 577 578 f_print(fout, "#ifndef _%s\n#define _%s\n\n", guard, 579 guard); 580 581 f_print(fout, "#define RPCGEN_VERSION\t%s\n\n", RPCGEN_VERSION); 582 f_print(fout, "#include <rpc/rpc.h>\n\n"); 583 584 tell = ftell(fout); 585 /* print data definitions */ 586 while ((def = get_definition()) != NULL) { 587 print_datadef(def); 588 } 589 590 /* print function declarations. Do this after data definitions 591 * because they might be used as arguments for functions */ 592 did = 0; 593 for (l = defined; l != NULL; l = l->next) { 594 print_funcdef(l->val, &did); 595 } 596 print_funcend(did); 597 598 for (l = defined; l != NULL; l = l->next) { 599 print_progdef(l->val); 600 } 601 602 if (extend && tell == ftell(fout)) { 603 (void) unlink(outfilename); 604 } else 605 if (tblflag) { 606 f_print(fout, rpcgen_table_dcl); 607 } 608 f_print(fout, "\n#endif /* !_%s */\n", guard); 609 610 free(guard); 611 } 612 613 /* 614 * Compile into an RPC service 615 */ 616 static void 617 s_output(int argc, char *argv[], char *infile, 618 const char *define, int extend, const char *outfile, int nomain, 619 int netflag) 620 { 621 char *include; 622 definition *def; 623 int foundprogram = 0; 624 const char *outfilename; 625 626 open_input(infile, define); 627 outfilename = extend ? extendfile(infile, outfile) : outfile; 628 open_output(infile, outfilename); 629 add_warning(); 630 if (infile && (include = extendfile(infile, ".h"))) { 631 f_print(fout, "#include \"%s\"\n", include); 632 free(include); 633 } else 634 f_print(fout, "#include <rpc/rpc.h>\n"); 635 636 f_print(fout, "#include <sys/ioctl.h>\n"); 637 f_print(fout, "#include <fcntl.h>\n"); 638 f_print(fout, "#include <stdio.h>\n"); 639 f_print(fout, "#include <err.h>\n"); 640 f_print(fout, "#include <stdlib.h>\n"); 641 f_print(fout, "#include <unistd.h>\n"); 642 f_print(fout, "#include <rpc/pmap_clnt.h>\n"); 643 f_print(fout, "#include <string.h>\n"); 644 f_print(fout, "#include <netdb.h>\n"); 645 if (strcmp(svcclosetime, "-1") == 0) 646 indefinitewait = 1; 647 else 648 if (strcmp(svcclosetime, "0") == 0) 649 exitnow = 1; 650 else 651 if (inetdflag || pmflag) { 652 f_print(fout, "#include <signal.h>\n"); 653 timerflag = 1; 654 } 655 if (!tirpcflag && inetdflag) 656 f_print(fout, "#include <sys/ttycom.h>\n"); 657 if (inetdflag || pmflag) { 658 f_print(fout, "#ifdef __cplusplus\n"); 659 f_print(fout, "#include <sysent.h>\n"); 660 f_print(fout, "#endif /* __cplusplus */\n"); 661 } 662 if (tirpcflag) 663 f_print(fout, "#include <sys/types.h>\n"); 664 665 f_print(fout, "#include <memory.h>\n"); 666 667 if (inetdflag || !tirpcflag) { 668 f_print(fout, "#include <sys/socket.h>\n"); 669 f_print(fout, "#include <netinet/in.h>\n"); 670 } 671 if ((netflag || pmflag) && tirpcflag) { 672 f_print(fout, "#include <netconfig.h>\n"); 673 } 674 if ( /* timerflag && */ tirpcflag) 675 f_print(fout, "#include <sys/resource.h>\n"); 676 if (logflag || inetdflag || pmflag) 677 f_print(fout, "#include <syslog.h>\n"); 678 679 f_print(fout, "\n#define SIG_PF void(*)(int)\n"); 680 681 f_print(fout, "\n#ifdef DEBUG\n#define RPC_SVC_FG\n#endif\n"); 682 if (timerflag) 683 f_print(fout, "\n#define _RPCSVC_CLOSEDOWN %s\n", svcclosetime); 684 while ((def = get_definition()) != NULL) { 685 foundprogram |= (def->def_kind == DEF_PROGRAM); 686 } 687 if (extend && !foundprogram) { 688 (void) unlink(outfilename); 689 return; 690 } 691 if (callerflag) /* EVAS */ 692 f_print(fout, "\nstatic SVCXPRT *caller;\n"); /* EVAS */ 693 write_most(infile, netflag, nomain); 694 if (!nomain) { 695 if (!do_registers(argc, argv)) { 696 if (outfilename) 697 (void) unlink(outfilename); 698 usage(); 699 } 700 write_rest(); 701 } 702 } 703 /* 704 * generate client side stubs 705 */ 706 static void 707 l_output(const char *infile, const char *define, int extend, 708 const char *outfile) 709 { 710 char *include; 711 definition *def; 712 int foundprogram = 0; 713 const char *outfilename; 714 715 open_input(infile, define); 716 outfilename = extend ? extendfile(infile, outfile) : outfile; 717 open_output(infile, outfilename); 718 add_warning(); 719 f_print(fout, "#include <memory.h>\n"); 720 if (infile && (include = extendfile(infile, ".h"))) { 721 f_print(fout, "#include \"%s\"\n", include); 722 free(include); 723 } else 724 f_print(fout, "#include <rpc/rpc.h>\n"); 725 while ((def = get_definition()) != NULL) { 726 foundprogram |= (def->def_kind == DEF_PROGRAM); 727 } 728 if (extend && !foundprogram) { 729 (void) unlink(outfilename); 730 return; 731 } 732 write_stubs(); 733 } 734 /* 735 * generate the dispatch table 736 */ 737 static void 738 t_output(const char *infile, const char *define, int extend, 739 const char *outfile) 740 { 741 definition *def; 742 int foundprogram = 0; 743 const char *outfilename; 744 745 open_input(infile, define); 746 outfilename = extend ? extendfile(infile, outfile) : outfile; 747 open_output(infile, outfilename); 748 add_warning(); 749 while ((def = get_definition()) != NULL) { 750 foundprogram |= (def->def_kind == DEF_PROGRAM); 751 } 752 if (extend && !foundprogram) { 753 (void) unlink(outfilename); 754 return; 755 } 756 write_tables(); 757 } 758 /* sample routine for the server template */ 759 static void 760 svc_output(const char *infile, const char *define, int extend, 761 const char *outfile) 762 { 763 definition *def; 764 char *include; 765 const char *outfilename; 766 long tell; 767 768 open_input(infile, define); 769 outfilename = extend ? extendfile(infile, outfile) : outfile; 770 checkfiles(infile, outfilename); /* check if outfile already 771 * exists. if so, print an 772 * error message and exit */ 773 open_output(infile, outfilename); 774 add_sample_msg(); 775 776 if (infile && (include = extendfile(infile, ".h"))) { 777 f_print(fout, "#include \"%s\"\n", include); 778 free(include); 779 } else 780 f_print(fout, "#include <rpc/rpc.h>\n"); 781 782 tell = ftell(fout); 783 while ((def = get_definition()) != NULL) { 784 write_sample_svc(def); 785 } 786 if (extend && tell == ftell(fout)) { 787 (void) unlink(outfilename); 788 } 789 } 790 791 792 /* sample main routine for client */ 793 static void 794 clnt_output(const char *infile, const char *define, int extend, 795 const char *outfile) 796 { 797 definition *def; 798 char *include; 799 const char *outfilename; 800 long tell; 801 int has_program = 0; 802 803 open_input(infile, define); 804 outfilename = extend ? extendfile(infile, outfile) : outfile; 805 checkfiles(infile, outfilename); /* check if outfile already 806 * exists. if so, print an 807 * error message and exit */ 808 809 open_output(infile, outfilename); 810 add_sample_msg(); 811 f_print(fout, "#include <stdio.h>\n"); 812 f_print(fout, "#include <err.h>\n"); 813 if (infile && (include = extendfile(infile, ".h"))) { 814 f_print(fout, "#include \"%s\"\n", include); 815 free(include); 816 } else 817 f_print(fout, "#include <rpc/rpc.h>\n"); 818 tell = ftell(fout); 819 while ((def = get_definition()) != NULL) { 820 has_program += write_sample_clnt(def); 821 } 822 823 if (has_program) 824 write_sample_clnt_main(); 825 826 if (extend && tell == ftell(fout)) { 827 (void) unlink(outfilename); 828 } 829 } 830 /* 831 * Perform registrations for service output 832 * Return 0 if failed; 1 otherwise. 833 */ 834 static int 835 do_registers(int argc, char *argv[]) 836 { 837 int i; 838 839 if (inetdflag || !tirpcflag) { 840 for (i = 1; i < argc; i++) { 841 if (streq(argv[i], "-s")) { 842 if (!check_nettype(argv[i + 1], valid_i_nettypes)) 843 return 0; 844 write_inetd_register(argv[i + 1]); 845 i++; 846 } 847 } 848 } else { 849 for (i = 1; i < argc; i++) 850 if (streq(argv[i], "-s")) { 851 if (!check_nettype(argv[i + 1], valid_ti_nettypes)) 852 return 0; 853 write_nettype_register(argv[i + 1]); 854 i++; 855 } else 856 if (streq(argv[i], "-n")) { 857 write_netid_register(argv[i + 1]); 858 i++; 859 } 860 } 861 return 1; 862 } 863 /* 864 * Add another argument to the arg list 865 */ 866 static void 867 addarg(const char *cp) 868 { 869 if (argcount >= ARGLISTLEN) { 870 errx(EXIT_FAILURE, "Internal error: too many defines"); 871 /* NOTREACHED */ 872 } 873 arglist[argcount++] = cp; 874 875 } 876 877 static void 878 putarg(int pwhere, const char *cp) 879 { 880 if (pwhere >= ARGLISTLEN) { 881 errx(EXIT_FAILURE, "Internal error: arglist coding error"); 882 /* NOTREACHED */ 883 } 884 arglist[pwhere] = cp; 885 886 } 887 /* 888 * if input file is stdin and an output file is specified then complain 889 * if the file already exists. Otherwise the file may get overwritten 890 * If input file does not exist, exit with an error 891 */ 892 893 static void 894 checkfiles(const char *infile, const char *outfile) 895 { 896 897 struct stat buf; 898 899 if (infile) /* infile ! = NULL */ 900 if (stat(infile, &buf) < 0) { 901 err(EXIT_FAILURE, "Can't stat `%s'", infile); 902 }; 903 #if 0 904 if (outfile) { 905 if (stat(outfile, &buf) < 0) 906 return; /* file does not exist */ 907 else { 908 errx(EXIT_FAILURE, 909 "`%s' already exists and would be overwritten", 910 outfile); 911 } 912 } 913 #endif 914 } 915 /* 916 * Parse command line arguments 917 */ 918 static int 919 parseargs(int argc, char *argv[], struct commandline *cmd) 920 { 921 int i; 922 int j; 923 int c; 924 char flag[1 << CHAR_BIT]; 925 int nflags; 926 927 cmdname = argv[0]; 928 cmd->infile = cmd->outfile = NULL; 929 if (argc < 2) { 930 return (0); 931 } 932 allfiles = 0; 933 flag['c'] = 0; 934 flag['h'] = 0; 935 flag['l'] = 0; 936 flag['m'] = 0; 937 flag['o'] = 0; 938 flag['s'] = 0; 939 flag['n'] = 0; 940 flag['t'] = 0; 941 flag['S'] = 0; 942 flag['C'] = 0; 943 for (i = 1; i < argc; i++) { 944 if (argv[i][0] != '-') { 945 if (cmd->infile) { 946 f_print(stderr, "Cannot specify more than one input file!\n"); 947 948 return (0); 949 } 950 cmd->infile = argv[i]; 951 } else { 952 for (j = 1; argv[i][j] != 0; j++) { 953 c = argv[i][j]; 954 switch (c) { 955 case 'A': 956 callerflag = 1; 957 break; 958 case 'a': 959 allfiles = 1; 960 break; 961 case 'B': 962 BSDflag = 1; 963 break; 964 case 'c': 965 case 'h': 966 case 'l': 967 case 'm': 968 case 't': 969 if (flag[c]) { 970 return (0); 971 } 972 flag[c] = 1; 973 break; 974 case 'S': 975 /* sample flag: Ss or Sc. Ss means set 976 * flag['S']; Sc means set flag['C']; */ 977 c = argv[i][++j]; /* get next char */ 978 if (c == 's') 979 c = 'S'; 980 else 981 if (c == 'c') 982 c = 'C'; 983 else 984 return (0); 985 986 if (flag[c]) { 987 return (0); 988 } 989 flag[c] = 1; 990 break; 991 case 'C': /* deprecated ANSI C syntax */ 992 break; 993 994 case 'b': /* turn TIRPC flag off for 995 * generating backward 996 * compatible */ 997 tirpcflag = 0; 998 break; 999 1000 case 'I': 1001 inetdflag = 1; 1002 break; 1003 case 'M': 1004 Mflag = 1; 1005 break; 1006 case 'N': 1007 newstyle = 1; 1008 break; 1009 case 'L': 1010 logflag = 1; 1011 break; 1012 case 'K': 1013 if (++i == argc) { 1014 return (0); 1015 } 1016 svcclosetime = argv[i]; 1017 goto nextarg; 1018 case 'T': 1019 tblflag = 1; 1020 break; 1021 case 'i': 1022 if (++i == argc) { 1023 return (0); 1024 } 1025 doinline = atoi(argv[i]); 1026 goto nextarg; 1027 case 'n': 1028 case 'o': 1029 case 's': 1030 if (argv[i][j - 1] != '-' || 1031 argv[i][j + 1] != 0) { 1032 return (0); 1033 } 1034 flag[c] = 1; 1035 if (++i == argc) { 1036 return (0); 1037 } 1038 if (c == 's') { 1039 if (!streq(argv[i], "udp") && 1040 !streq(argv[i], "tcp")) { 1041 return (0); 1042 } 1043 } else 1044 if (c == 'o') { 1045 if (cmd->outfile) { 1046 return (0); 1047 } 1048 cmd->outfile = argv[i]; 1049 } 1050 goto nextarg; 1051 case 'D': 1052 if (argv[i][j - 1] != '-') { 1053 return (0); 1054 } 1055 (void) addarg(argv[i]); 1056 goto nextarg; 1057 case 'Y': 1058 if (++i == argc) { 1059 return (0); 1060 } 1061 (void) strlcpy(pathbuf, argv[i], 1062 sizeof(pathbuf)); 1063 (void) strlcat(pathbuf, "/cpp", 1064 sizeof(pathbuf)); 1065 CPP = pathbuf; 1066 goto nextarg; 1067 1068 case 'v': 1069 printf("version 1.0\n"); 1070 exit(0); 1071 1072 default: 1073 return (0); 1074 } 1075 } 1076 nextarg: 1077 ; 1078 } 1079 } 1080 1081 cmd->cflag = flag['c']; 1082 cmd->hflag = flag['h']; 1083 cmd->lflag = flag['l']; 1084 cmd->mflag = flag['m']; 1085 cmd->nflag = flag['n']; 1086 cmd->sflag = flag['s']; 1087 cmd->tflag = flag['t']; 1088 cmd->Ssflag = flag['S']; 1089 cmd->Scflag = flag['C']; 1090 1091 if (tirpcflag) { 1092 pmflag = inetdflag ? 0 : 1; /* pmflag or inetdflag is 1093 * always TRUE */ 1094 if ((inetdflag && cmd->nflag)) { /* netid not allowed 1095 * with inetdflag */ 1096 f_print(stderr, "Cannot use netid flag with inetd flag!\n"); 1097 return (0); 1098 } 1099 } else { /* 4.1 mode */ 1100 pmflag = 0; /* set pmflag only in tirpcmode */ 1101 inetdflag = 1; /* inetdflag is TRUE by default */ 1102 if (cmd->nflag) { /* netid needs TIRPC */ 1103 f_print(stderr, "Cannot use netid flag without TIRPC!\n"); 1104 return (0); 1105 } 1106 } 1107 1108 if (newstyle && (tblflag || cmd->tflag)) { 1109 f_print(stderr, "Cannot use table flags with newstyle!\n"); 1110 return (0); 1111 } 1112 /* check no conflicts with file generation flags */ 1113 nflags = cmd->cflag + cmd->hflag + cmd->lflag + cmd->mflag + 1114 cmd->sflag + cmd->nflag + cmd->tflag + cmd->Ssflag + cmd->Scflag; 1115 1116 if (nflags == 0) { 1117 if (cmd->outfile != NULL || cmd->infile == NULL) { 1118 return (0); 1119 } 1120 } else 1121 if (nflags > 1) { 1122 f_print(stderr, "Cannot have more than one file generation flag!\n"); 1123 return (0); 1124 } 1125 return (1); 1126 } 1127 1128 static void 1129 usage(void) 1130 { 1131 f_print(stderr, "usage: %s infile\n", cmdname); 1132 f_print(stderr, "\t%s [-AaBbILMNTv] [-Dname[=value]] [-i size] [-K seconds] [-Y pathname] infile\n", 1133 cmdname); 1134 f_print(stderr, "\t%s [-c | -h | -l | -m | -t | -Sc | -Ss] [-o outfile] [infile]\n", 1135 cmdname); 1136 f_print(stderr, "\t%s [-s nettype] [-o outfile] [infile]\n", cmdname); 1137 f_print(stderr, "\t%s [-n netid] [-o outfile] [infile]\n", cmdname); 1138 options_usage(); 1139 exit(1); 1140 } 1141 1142 static void 1143 options_usage(void) 1144 { 1145 f_print(stderr, "options:\n"); 1146 f_print(stderr, "-A\t\tgenerate svc_caller() function\n"); 1147 f_print(stderr, "-a\t\tgenerate all files, including samples\n"); 1148 f_print(stderr, "-B\t\tgenerate BSD c++ macros\n"); 1149 f_print(stderr, "-b\t\tbackward compatibility mode (generates code for SunOS 4.1)\n"); 1150 f_print(stderr, "-c\t\tgenerate XDR routines\n"); 1151 f_print(stderr, "-Dname[=value]\tdefine a symbol (same as #define)\n"); 1152 f_print(stderr, "-h\t\tgenerate header file\n"); 1153 f_print(stderr, "-I\t\tgenerate code for inetd support in server (for SunOS 4.1)\n"); 1154 f_print(stderr, "-i size\t\tsize at which to start generating inline code\n"); 1155 f_print(stderr, "-K seconds\tserver exits after K seconds of inactivity\n"); 1156 f_print(stderr, "-L\t\tserver errors will be printed to syslog\n"); 1157 f_print(stderr, "-l\t\tgenerate client side stubs\n"); 1158 f_print(stderr, "-M\t\tgenerate thread-safe stubs\n"); 1159 f_print(stderr, "-m\t\tgenerate server side stubs\n"); 1160 f_print(stderr, "-N\t\tsupports multiple arguments and call-by-value\n"); 1161 f_print(stderr, "-n netid\tgenerate server code that supports named netid\n"); 1162 f_print(stderr, "-o outfile\tname of the output file\n"); 1163 f_print(stderr, "-s nettype\tgenerate server code that supports named nettype\n"); 1164 f_print(stderr, "-Sc\t\tgenerate sample client code that uses remote procedures\n"); 1165 f_print(stderr, "-Ss\t\tgenerate sample server code that defines remote procedures\n"); 1166 f_print(stderr, "-T\t\tgenerate code to support RPC dispatch tables\n"); 1167 f_print(stderr, "-t\t\tgenerate RPC dispatch table\n"); 1168 f_print(stderr, "-v\t\tdisplay version number\n"); 1169 f_print(stderr, "-Y path\t\tdirectory name to find C preprocessor (cpp)\n"); 1170 1171 exit(1); 1172 } 1173