1 /* $NetBSD: rpc_main.c,v 1.42 2015/05/09 23:12:57 dholland 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.42 2015/05/09 23:12:57 dholland 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 errx(1, "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; 499 500 filename = strrchr(pathname, '/'); /* find last component */ 501 filename = ((filename == 0) ? pathname : filename + 1); 502 guard = strdup(filename); 503 /* convert to upper case */ 504 tmp = guard; 505 while (*tmp) { 506 *tmp = toupper((unsigned char)*tmp); 507 tmp++; 508 } 509 510 tmp2 = extendfile(guard, "_H_RPCGEN"); 511 free(guard); 512 guard = tmp2; 513 return (guard); 514 } 515 516 /* 517 * Compile into an XDR header file 518 */ 519 static void 520 h_output(const char *infile, const char *define, int extend, 521 const char *outfile) 522 { 523 definition *def; 524 const char *outfilename; 525 long tell; 526 char *guard; 527 list *l; 528 int did; 529 530 open_input(infile, define); 531 outfilename = extend ? extendfile(infile, outfile) : outfile; 532 open_output(infile, outfilename); 533 add_warning(); 534 if (outfilename || infile) 535 guard = generate_guard(outfilename ? outfilename : infile); 536 else { 537 guard = strdup("STDIN_"); 538 if (guard == NULL) { 539 err(EXIT_FAILURE, "strdup"); 540 } 541 } 542 543 f_print(fout, "#ifndef _%s\n#define _%s\n\n", guard, 544 guard); 545 546 f_print(fout, "#define RPCGEN_VERSION\t%s\n\n", RPCGEN_VERSION); 547 f_print(fout, "#include <rpc/rpc.h>\n\n"); 548 549 tell = ftell(fout); 550 /* print data definitions */ 551 while ((def = get_definition()) != NULL) { 552 print_datadef(def); 553 } 554 555 /* print function declarations. Do this after data definitions 556 * because they might be used as arguments for functions */ 557 did = 0; 558 for (l = defined; l != NULL; l = l->next) { 559 print_funcdef(l->val, &did); 560 } 561 print_funcend(did); 562 563 for (l = defined; l != NULL; l = l->next) { 564 print_progdef(l->val); 565 } 566 567 if (extend && tell == ftell(fout)) { 568 (void) unlink(outfilename); 569 } else 570 if (tblflag) { 571 f_print(fout, rpcgen_table_dcl); 572 } 573 f_print(fout, "\n#endif /* !_%s */\n", guard); 574 575 free(guard); 576 } 577 578 /* 579 * Compile into an RPC service 580 */ 581 static void 582 s_output(int argc, char *argv[], char *infile, 583 const char *define, int extend, const char *outfile, int nomain, 584 int netflag) 585 { 586 char *include; 587 definition *def; 588 int foundprogram = 0; 589 const char *outfilename; 590 591 open_input(infile, define); 592 outfilename = extend ? extendfile(infile, outfile) : outfile; 593 open_output(infile, outfilename); 594 add_warning(); 595 if (infile && (include = extendfile(infile, ".h"))) { 596 f_print(fout, "#include \"%s\"\n", include); 597 free(include); 598 } else 599 f_print(fout, "#include <rpc/rpc.h>\n"); 600 601 f_print(fout, "#include <sys/ioctl.h>\n"); 602 f_print(fout, "#include <fcntl.h>\n"); 603 f_print(fout, "#include <stdio.h>\n"); 604 f_print(fout, "#include <err.h>\n"); 605 f_print(fout, "#include <stdlib.h>\n"); 606 f_print(fout, "#include <unistd.h>\n"); 607 f_print(fout, "#include <rpc/pmap_clnt.h>\n"); 608 f_print(fout, "#include <string.h>\n"); 609 f_print(fout, "#include <netdb.h>\n"); 610 if (strcmp(svcclosetime, "-1") == 0) 611 indefinitewait = 1; 612 else 613 if (strcmp(svcclosetime, "0") == 0) 614 exitnow = 1; 615 else 616 if (inetdflag || pmflag) { 617 f_print(fout, "#include <signal.h>\n"); 618 timerflag = 1; 619 } 620 if (!tirpcflag && inetdflag) 621 f_print(fout, "#include <sys/ttycom.h>\n"); 622 if (inetdflag || pmflag) { 623 f_print(fout, "#ifdef __cplusplus\n"); 624 f_print(fout, "#include <sysent.h>\n"); 625 f_print(fout, "#endif /* __cplusplus */\n"); 626 } 627 if (tirpcflag) 628 f_print(fout, "#include <sys/types.h>\n"); 629 630 f_print(fout, "#include <memory.h>\n"); 631 632 if (inetdflag || !tirpcflag) { 633 f_print(fout, "#include <sys/socket.h>\n"); 634 f_print(fout, "#include <netinet/in.h>\n"); 635 } 636 if ((netflag || pmflag) && tirpcflag) { 637 f_print(fout, "#include <netconfig.h>\n"); 638 } 639 if ( /* timerflag && */ tirpcflag) 640 f_print(fout, "#include <sys/resource.h>\n"); 641 if (logflag || inetdflag || pmflag) 642 f_print(fout, "#include <syslog.h>\n"); 643 644 f_print(fout, "\n#define SIG_PF void(*)(int)\n"); 645 646 f_print(fout, "\n#ifdef DEBUG\n#define RPC_SVC_FG\n#endif\n"); 647 if (timerflag) 648 f_print(fout, "\n#define _RPCSVC_CLOSEDOWN %s\n", svcclosetime); 649 while ((def = get_definition()) != NULL) { 650 foundprogram |= (def->def_kind == DEF_PROGRAM); 651 } 652 if (extend && !foundprogram) { 653 (void) unlink(outfilename); 654 return; 655 } 656 if (callerflag) /* EVAS */ 657 f_print(fout, "\nstatic SVCXPRT *caller;\n"); /* EVAS */ 658 write_most(infile, netflag, nomain); 659 if (!nomain) { 660 if (!do_registers(argc, argv)) { 661 if (outfilename) 662 (void) unlink(outfilename); 663 usage(); 664 } 665 write_rest(); 666 } 667 } 668 /* 669 * generate client side stubs 670 */ 671 static void 672 l_output(const char *infile, const char *define, int extend, 673 const char *outfile) 674 { 675 char *include; 676 definition *def; 677 int foundprogram = 0; 678 const char *outfilename; 679 680 open_input(infile, define); 681 outfilename = extend ? extendfile(infile, outfile) : outfile; 682 open_output(infile, outfilename); 683 add_warning(); 684 f_print(fout, "#include <memory.h>\n"); 685 if (infile && (include = extendfile(infile, ".h"))) { 686 f_print(fout, "#include \"%s\"\n", include); 687 free(include); 688 } else 689 f_print(fout, "#include <rpc/rpc.h>\n"); 690 while ((def = get_definition()) != NULL) { 691 foundprogram |= (def->def_kind == DEF_PROGRAM); 692 } 693 if (extend && !foundprogram) { 694 (void) unlink(outfilename); 695 return; 696 } 697 write_stubs(); 698 } 699 /* 700 * generate the dispatch table 701 */ 702 static void 703 t_output(const char *infile, const char *define, int extend, 704 const char *outfile) 705 { 706 definition *def; 707 int foundprogram = 0; 708 const char *outfilename; 709 710 open_input(infile, define); 711 outfilename = extend ? extendfile(infile, outfile) : outfile; 712 open_output(infile, outfilename); 713 add_warning(); 714 while ((def = get_definition()) != NULL) { 715 foundprogram |= (def->def_kind == DEF_PROGRAM); 716 } 717 if (extend && !foundprogram) { 718 (void) unlink(outfilename); 719 return; 720 } 721 write_tables(); 722 } 723 /* sample routine for the server template */ 724 static void 725 svc_output(const char *infile, const char *define, int extend, 726 const char *outfile) 727 { 728 definition *def; 729 char *include; 730 const char *outfilename; 731 long tell; 732 733 open_input(infile, define); 734 outfilename = extend ? extendfile(infile, outfile) : outfile; 735 checkfiles(infile, outfilename); /* check if outfile already 736 * exists. if so, print an 737 * error message and exit */ 738 open_output(infile, outfilename); 739 add_sample_msg(); 740 741 if (infile && (include = extendfile(infile, ".h"))) { 742 f_print(fout, "#include \"%s\"\n", include); 743 free(include); 744 } else 745 f_print(fout, "#include <rpc/rpc.h>\n"); 746 747 tell = ftell(fout); 748 while ((def = get_definition()) != NULL) { 749 write_sample_svc(def); 750 } 751 if (extend && tell == ftell(fout)) { 752 (void) unlink(outfilename); 753 } 754 } 755 756 757 /* sample main routine for client */ 758 static void 759 clnt_output(const char *infile, const char *define, int extend, 760 const char *outfile) 761 { 762 definition *def; 763 char *include; 764 const char *outfilename; 765 long tell; 766 int has_program = 0; 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 774 open_output(infile, outfilename); 775 add_sample_msg(); 776 f_print(fout, "#include <stdio.h>\n"); 777 f_print(fout, "#include <err.h>\n"); 778 if (infile && (include = extendfile(infile, ".h"))) { 779 f_print(fout, "#include \"%s\"\n", include); 780 free(include); 781 } else 782 f_print(fout, "#include <rpc/rpc.h>\n"); 783 tell = ftell(fout); 784 while ((def = get_definition()) != NULL) { 785 has_program += write_sample_clnt(def); 786 } 787 788 if (has_program) 789 write_sample_clnt_main(); 790 791 if (extend && tell == ftell(fout)) { 792 (void) unlink(outfilename); 793 } 794 } 795 /* 796 * Perform registrations for service output 797 * Return 0 if failed; 1 otherwise. 798 */ 799 static int 800 do_registers(int argc, char *argv[]) 801 { 802 int i; 803 804 if (inetdflag || !tirpcflag) { 805 for (i = 1; i < argc; i++) { 806 if (streq(argv[i], "-s")) { 807 if (!check_nettype(argv[i + 1], valid_i_nettypes)) 808 return 0; 809 write_inetd_register(argv[i + 1]); 810 i++; 811 } 812 } 813 } else { 814 for (i = 1; i < argc; i++) 815 if (streq(argv[i], "-s")) { 816 if (!check_nettype(argv[i + 1], valid_ti_nettypes)) 817 return 0; 818 write_nettype_register(argv[i + 1]); 819 i++; 820 } else 821 if (streq(argv[i], "-n")) { 822 write_netid_register(argv[i + 1]); 823 i++; 824 } 825 } 826 return 1; 827 } 828 /* 829 * Add another argument to the arg list 830 */ 831 static void 832 addarg(const char *cp) 833 { 834 if (argcount >= ARGLISTLEN) { 835 errx(EXIT_FAILURE, "Internal error: too many defines"); 836 /* NOTREACHED */ 837 } 838 arglist[argcount++] = cp; 839 840 } 841 842 static void 843 putarg(int pwhere, const char *cp) 844 { 845 if (pwhere >= ARGLISTLEN) { 846 errx(EXIT_FAILURE, "Internal error: arglist coding error"); 847 /* NOTREACHED */ 848 } 849 arglist[pwhere] = cp; 850 851 } 852 /* 853 * if input file is stdin and an output file is specified then complain 854 * if the file already exists. Otherwise the file may get overwritten 855 * If input file does not exist, exit with an error 856 */ 857 858 static void 859 checkfiles(const char *infile, const char *outfile) 860 { 861 862 struct stat buf; 863 864 if (infile) /* infile ! = NULL */ 865 if (stat(infile, &buf) < 0) { 866 err(EXIT_FAILURE, "Can't stat `%s'", infile); 867 }; 868 #if 0 869 if (outfile) { 870 if (stat(outfile, &buf) < 0) 871 return; /* file does not exist */ 872 else { 873 errx(EXIT_FAILURE, 874 "`%s' already exists and would be overwritten", 875 outfile); 876 } 877 } 878 #endif 879 } 880 /* 881 * Parse command line arguments 882 */ 883 static int 884 parseargs(int argc, char *argv[], struct commandline *cmd) 885 { 886 int i; 887 int j; 888 int c; 889 char flag[1 << CHAR_BIT]; 890 int nflags; 891 892 cmdname = argv[0]; 893 cmd->infile = cmd->outfile = NULL; 894 if (argc < 2) { 895 return (0); 896 } 897 allfiles = 0; 898 flag['c'] = 0; 899 flag['h'] = 0; 900 flag['l'] = 0; 901 flag['m'] = 0; 902 flag['o'] = 0; 903 flag['s'] = 0; 904 flag['n'] = 0; 905 flag['t'] = 0; 906 flag['S'] = 0; 907 flag['C'] = 0; 908 for (i = 1; i < argc; i++) { 909 if (argv[i][0] != '-') { 910 if (cmd->infile) { 911 f_print(stderr, "Cannot specify more than one input file!\n"); 912 913 return (0); 914 } 915 cmd->infile = argv[i]; 916 } else { 917 for (j = 1; argv[i][j] != 0; j++) { 918 c = argv[i][j]; 919 switch (c) { 920 case 'A': 921 callerflag = 1; 922 break; 923 case 'a': 924 allfiles = 1; 925 break; 926 case 'B': 927 BSDflag = 1; 928 break; 929 case 'c': 930 case 'h': 931 case 'l': 932 case 'm': 933 case 't': 934 if (flag[c]) { 935 return (0); 936 } 937 flag[c] = 1; 938 break; 939 case 'S': 940 /* sample flag: Ss or Sc. Ss means set 941 * flag['S']; Sc means set flag['C']; */ 942 c = argv[i][++j]; /* get next char */ 943 if (c == 's') 944 c = 'S'; 945 else 946 if (c == 'c') 947 c = 'C'; 948 else 949 return (0); 950 951 if (flag[c]) { 952 return (0); 953 } 954 flag[c] = 1; 955 break; 956 case 'C': /* deprecated ANSI C syntax */ 957 break; 958 959 case 'b': /* turn TIRPC flag off for 960 * generating backward 961 * compatible */ 962 tirpcflag = 0; 963 break; 964 965 case 'I': 966 inetdflag = 1; 967 break; 968 case 'M': 969 Mflag = 1; 970 break; 971 case 'N': 972 newstyle = 1; 973 break; 974 case 'L': 975 logflag = 1; 976 break; 977 case 'K': 978 if (++i == argc) { 979 return (0); 980 } 981 svcclosetime = argv[i]; 982 goto nextarg; 983 case 'T': 984 tblflag = 1; 985 break; 986 case 'i': 987 if (++i == argc) { 988 return (0); 989 } 990 doinline = atoi(argv[i]); 991 goto nextarg; 992 case 'n': 993 case 'o': 994 case 's': 995 if (argv[i][j - 1] != '-' || 996 argv[i][j + 1] != 0) { 997 return (0); 998 } 999 flag[c] = 1; 1000 if (++i == argc) { 1001 return (0); 1002 } 1003 if (c == 's') { 1004 if (!streq(argv[i], "udp") && 1005 !streq(argv[i], "tcp")) { 1006 return (0); 1007 } 1008 } else 1009 if (c == 'o') { 1010 if (cmd->outfile) { 1011 return (0); 1012 } 1013 cmd->outfile = argv[i]; 1014 } 1015 goto nextarg; 1016 case 'D': 1017 if (argv[i][j - 1] != '-') { 1018 return (0); 1019 } 1020 (void) addarg(argv[i]); 1021 goto nextarg; 1022 case 'Y': 1023 if (++i == argc) { 1024 return (0); 1025 } 1026 (void) strlcpy(pathbuf, argv[i], 1027 sizeof(pathbuf)); 1028 (void) strlcat(pathbuf, "/cpp", 1029 sizeof(pathbuf)); 1030 CPP = pathbuf; 1031 goto nextarg; 1032 1033 case 'v': 1034 printf("version 1.0\n"); 1035 exit(0); 1036 1037 default: 1038 return (0); 1039 } 1040 } 1041 nextarg: 1042 ; 1043 } 1044 } 1045 1046 cmd->cflag = flag['c']; 1047 cmd->hflag = flag['h']; 1048 cmd->lflag = flag['l']; 1049 cmd->mflag = flag['m']; 1050 cmd->nflag = flag['n']; 1051 cmd->sflag = flag['s']; 1052 cmd->tflag = flag['t']; 1053 cmd->Ssflag = flag['S']; 1054 cmd->Scflag = flag['C']; 1055 1056 if (tirpcflag) { 1057 pmflag = inetdflag ? 0 : 1; /* pmflag or inetdflag is 1058 * always TRUE */ 1059 if ((inetdflag && cmd->nflag)) { /* netid not allowed 1060 * with inetdflag */ 1061 f_print(stderr, "Cannot use netid flag with inetd flag!\n"); 1062 return (0); 1063 } 1064 } else { /* 4.1 mode */ 1065 pmflag = 0; /* set pmflag only in tirpcmode */ 1066 inetdflag = 1; /* inetdflag is TRUE by default */ 1067 if (cmd->nflag) { /* netid needs TIRPC */ 1068 f_print(stderr, "Cannot use netid flag without TIRPC!\n"); 1069 return (0); 1070 } 1071 } 1072 1073 if (newstyle && (tblflag || cmd->tflag)) { 1074 f_print(stderr, "Cannot use table flags with newstyle!\n"); 1075 return (0); 1076 } 1077 /* check no conflicts with file generation flags */ 1078 nflags = cmd->cflag + cmd->hflag + cmd->lflag + cmd->mflag + 1079 cmd->sflag + cmd->nflag + cmd->tflag + cmd->Ssflag + cmd->Scflag; 1080 1081 if (nflags == 0) { 1082 if (cmd->outfile != NULL || cmd->infile == NULL) { 1083 return (0); 1084 } 1085 } else 1086 if (nflags > 1) { 1087 f_print(stderr, "Cannot have more than one file generation flag!\n"); 1088 return (0); 1089 } 1090 return (1); 1091 } 1092 1093 static void 1094 usage(void) 1095 { 1096 f_print(stderr, "usage: %s infile\n", cmdname); 1097 f_print(stderr, "\t%s [-AaBbILMNTv] [-Dname[=value]] [-i size] [-K seconds] [-Y pathname] infile\n", 1098 cmdname); 1099 f_print(stderr, "\t%s [-c | -h | -l | -m | -t | -Sc | -Ss] [-o outfile] [infile]\n", 1100 cmdname); 1101 f_print(stderr, "\t%s [-s nettype] [-o outfile] [infile]\n", cmdname); 1102 f_print(stderr, "\t%s [-n netid] [-o outfile] [infile]\n", cmdname); 1103 options_usage(); 1104 exit(1); 1105 } 1106 1107 static void 1108 options_usage(void) 1109 { 1110 f_print(stderr, "options:\n"); 1111 f_print(stderr, "-A\t\tgenerate svc_caller() function\n"); 1112 f_print(stderr, "-a\t\tgenerate all files, including samples\n"); 1113 f_print(stderr, "-B\t\tgenerate BSD c++ macros\n"); 1114 f_print(stderr, "-b\t\tbackward compatibility mode (generates code for SunOS 4.1)\n"); 1115 f_print(stderr, "-c\t\tgenerate XDR routines\n"); 1116 f_print(stderr, "-Dname[=value]\tdefine a symbol (same as #define)\n"); 1117 f_print(stderr, "-h\t\tgenerate header file\n"); 1118 f_print(stderr, "-I\t\tgenerate code for inetd support in server (for SunOS 4.1)\n"); 1119 f_print(stderr, "-i size\t\tsize at which to start generating inline code\n"); 1120 f_print(stderr, "-K seconds\tserver exits after K seconds of inactivity\n"); 1121 f_print(stderr, "-L\t\tserver errors will be printed to syslog\n"); 1122 f_print(stderr, "-l\t\tgenerate client side stubs\n"); 1123 f_print(stderr, "-M\t\tgenerate thread-safe stubs\n"); 1124 f_print(stderr, "-m\t\tgenerate server side stubs\n"); 1125 f_print(stderr, "-N\t\tsupports multiple arguments and call-by-value\n"); 1126 f_print(stderr, "-n netid\tgenerate server code that supports named netid\n"); 1127 f_print(stderr, "-o outfile\tname of the output file\n"); 1128 f_print(stderr, "-s nettype\tgenerate server code that supports named nettype\n"); 1129 f_print(stderr, "-Sc\t\tgenerate sample client code that uses remote procedures\n"); 1130 f_print(stderr, "-Ss\t\tgenerate sample server code that defines remote procedures\n"); 1131 f_print(stderr, "-T\t\tgenerate code to support RPC dispatch tables\n"); 1132 f_print(stderr, "-t\t\tgenerate RPC dispatch table\n"); 1133 f_print(stderr, "-v\t\tdisplay version number\n"); 1134 f_print(stderr, "-Y path\t\tdirectory name to find C preprocessor (cpp)\n"); 1135 1136 exit(1); 1137 } 1138