1 /* $OpenBSD: rpc_main.c,v 1.36 2024/07/22 17:55:18 dv Exp $ */ 2 /* $NetBSD: rpc_main.c,v 1.9 1996/02/19 11:12:43 pk Exp $ */ 3 4 /* 5 * Copyright (c) 2010, Oracle America, Inc. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are 9 * met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above 14 * copyright notice, this list of conditions and the following 15 * disclaimer in the documentation and/or other materials 16 * provided with the distribution. 17 * * Neither the name of the "Oracle America, Inc." nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 26 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 28 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 /* 36 * rpc_main.c, Top level of the RPC protocol compiler. 37 */ 38 39 #define RPCGEN_VERSION "199506"/* This program's version (year & month) */ 40 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <unistd.h> 45 #include <limits.h> 46 #include <ctype.h> 47 #include <sys/stat.h> 48 #include "rpc_parse.h" 49 #include "rpc_util.h" 50 #include "rpc_scan.h" 51 52 #define EXTEND 1 /* alias for TRUE */ 53 #define DONT_EXTEND 0 /* alias for FALSE */ 54 55 #define SVR4_CPP "/usr/ccs/lib/cpp" 56 #define SUNOS_CPP "/lib/cpp" 57 static int cppDefined = 0; /* explicit path for C preprocessor */ 58 59 struct commandline { 60 int cflag; /* xdr C routines */ 61 int hflag; /* header file */ 62 int lflag; /* client side stubs */ 63 int mflag; /* server side stubs */ 64 int nflag; /* netid flag */ 65 int sflag; /* server stubs for the given transport */ 66 int tflag; /* dispatch Table file */ 67 int Ssflag; /* produce server sample code */ 68 int Scflag; /* produce client sample code */ 69 char *infile; /* input module name */ 70 char *outfile;/* output module name */ 71 }; 72 73 static char *cmdname; 74 75 static char *svcclosetime = "120"; 76 static char *CPP = "/usr/bin/cpp"; 77 static char CPPFLAGS[] = "-C"; 78 static char pathbuf[PATH_MAX]; 79 static char *allv[] = { 80 "rpcgen", "-s", "udp", "-s", "tcp", 81 }; 82 static int allc = sizeof(allv) / sizeof(allv[0]); 83 static char *allnv[] = { 84 "rpcgen", "-s", "netpath", 85 }; 86 static int allnc = sizeof(allnv) / sizeof(allnv[0]); 87 88 #define ARGLISTLEN 20 89 #define FIXEDARGS 2 90 91 static char *arglist[ARGLISTLEN]; 92 static int argcount = FIXEDARGS; 93 94 95 int nonfatalerrors; /* errors */ 96 int inetdflag /* = 1 */ ; /* Support for inetd *//* is now the 97 * default */ 98 int pmflag; /* Support for port monitors */ 99 int logflag; /* Use syslog instead of fprintf for errors */ 100 int tblflag; /* Support for dispatch table file */ 101 int callerflag; /* Generate svc_caller() function */ 102 103 #define INLINE 3 104 /* length at which to start doing an inline */ 105 106 int doinline = INLINE; /* length at which to start doing an 107 * inline. 3 = default if 0, no 108 * xdr_inline code */ 109 110 int indefinitewait; /* If started by port monitors, hang till it 111 * wants */ 112 int exitnow; /* If started by port monitors, exit after 113 * the call */ 114 int timerflag; /* TRUE if !indefinite && !exitnow */ 115 int newstyle; /* newstyle of passing arguments (by value) */ 116 int Cflag = 0; /* ANSI C syntax */ 117 static int allfiles; /* generate all files */ 118 int tirpcflag = 0; /* generating code for tirpc, by default */ 119 120 static void c_output(char *, char *, int, char *); 121 static void h_output(char *, char *, int, char *); 122 static void s_output(int, char **, char *, char *, int, char *, int, int); 123 static void l_output(char *, char *, int, char *); 124 static void t_output(char *, char *, int, char *); 125 static void svc_output(char *, char *, int, char *); 126 static void clnt_output(char *, char *, int, char *); 127 static int do_registers(int, char **); 128 static void addarg(char *); 129 static void putarg(int, char *); 130 static void clear_args(void); 131 static void checkfiles(char *, char *); 132 static int parseargs(int, char **, struct commandline *); 133 static void usage(void); 134 void c_initialize(void); 135 136 int 137 main(int argc, char *argv[]) 138 { 139 struct commandline cmd; 140 141 if (pledge("stdio rpath wpath cpath proc exec", NULL) == -1) { 142 perror("pledge"); 143 exit(1); 144 } 145 146 (void) memset((char *) &cmd, 0, sizeof(struct commandline)); 147 clear_args(); 148 if (!parseargs(argc, argv, &cmd)) 149 usage(); 150 151 if (cmd.cflag || cmd.hflag || cmd.lflag || cmd.tflag || cmd.sflag || 152 cmd.mflag || cmd.nflag || cmd.Ssflag || cmd.Scflag) { 153 checkfiles(cmd.infile, cmd.outfile); 154 } else 155 checkfiles(cmd.infile, NULL); 156 157 if (cmd.cflag) { 158 c_output(cmd.infile, "-DRPC_XDR", DONT_EXTEND, cmd.outfile); 159 } else if (cmd.hflag) { 160 h_output(cmd.infile, "-DRPC_HDR", DONT_EXTEND, cmd.outfile); 161 } else if (cmd.lflag) { 162 l_output(cmd.infile, "-DRPC_CLNT", DONT_EXTEND, cmd.outfile); 163 } else if (cmd.sflag || cmd.mflag || (cmd.nflag)) { 164 s_output(argc, argv, cmd.infile, "-DRPC_SVC", DONT_EXTEND, 165 cmd.outfile, cmd.mflag, cmd.nflag); 166 } else if (cmd.tflag) { 167 t_output(cmd.infile, "-DRPC_TBL", DONT_EXTEND, cmd.outfile); 168 } else if (cmd.Ssflag) { 169 svc_output(cmd.infile, "-DRPC_SERVER", DONT_EXTEND, cmd.outfile); 170 } else if (cmd.Scflag) { 171 clnt_output(cmd.infile, "-DRPC_CLIENT", DONT_EXTEND, cmd.outfile); 172 } else { 173 /* the rescans are required, since cpp may effect input */ 174 c_output(cmd.infile, "-DRPC_XDR", EXTEND, "_xdr.c"); 175 reinitialize(); 176 h_output(cmd.infile, "-DRPC_HDR", EXTEND, ".h"); 177 reinitialize(); 178 l_output(cmd.infile, "-DRPC_CLNT", EXTEND, "_clnt.c"); 179 reinitialize(); 180 if (inetdflag || !tirpcflag) 181 s_output(allc, allv, cmd.infile, "-DRPC_SVC", EXTEND, 182 "_svc.c", cmd.mflag, cmd.nflag); 183 else 184 s_output(allnc, allnv, cmd.infile, "-DRPC_SVC", 185 EXTEND, "_svc.c", cmd.mflag, cmd.nflag); 186 if (tblflag) { 187 reinitialize(); 188 t_output(cmd.infile, "-DRPC_TBL", EXTEND, "_tbl.i"); 189 } 190 if (allfiles) { 191 reinitialize(); 192 svc_output(cmd.infile, "-DRPC_SERVER", EXTEND, "_server.c"); 193 } 194 if (allfiles) { 195 reinitialize(); 196 clnt_output(cmd.infile, "-DRPC_CLIENT", EXTEND, "_client.c"); 197 } 198 } 199 exit(nonfatalerrors); 200 /* NOTREACHED */ 201 } 202 203 /* 204 * add extension to filename 205 */ 206 static char * 207 extendfile(char *path, char *ext) 208 { 209 char *file; 210 char *res; 211 char *p; 212 size_t len; 213 214 if ((file = strrchr(path, '/')) == NULL) 215 file = path; 216 else 217 file++; 218 219 len = strlen(file) + strlen(ext) + 1; 220 res = malloc(len); 221 if (res == NULL) { 222 fprintf(stderr, "could not allocate memory\n"); 223 exit(1); 224 } 225 p = strrchr(file, '.'); 226 if (p == NULL) 227 p = file + strlen(file); 228 (void) strlcpy(res, file, len); 229 (void) strlcpy(res + (p - file), ext, len - (p - file)); 230 return (res); 231 } 232 233 /* 234 * Open output file with given extension 235 */ 236 static void 237 open_output(char *infile, char *outfile) 238 { 239 240 if (outfile == NULL) { 241 fout = stdout; 242 return; 243 } 244 if (infile != NULL && streq(outfile, infile)) { 245 fprintf(stderr, "%s: output would overwrite %s\n", cmdname, 246 infile); 247 crash(); 248 } 249 fout = fopen(outfile, "w"); 250 if (fout == NULL) { 251 fprintf(stderr, "%s: unable to open ", cmdname); 252 perror(outfile); 253 crash(); 254 } 255 record_open(outfile); 256 257 } 258 259 static void 260 add_warning(void) 261 { 262 fprintf(fout, "/*\n"); 263 fprintf(fout, " * Please do not edit this file.\n"); 264 fprintf(fout, " * It was generated using rpcgen.\n"); 265 fprintf(fout, " */\n\n"); 266 } 267 268 /* clear list of arguments */ 269 static void 270 clear_args(void) 271 { 272 int i; 273 for (i = FIXEDARGS; i < ARGLISTLEN; i++) 274 arglist[i] = NULL; 275 argcount = FIXEDARGS; 276 } 277 278 /* make sure that a CPP exists */ 279 static void 280 find_cpp(void) 281 { 282 struct stat buf; 283 284 /* SVR4 or explicit cpp does not exist */ 285 if (stat(CPP, &buf) == -1) { 286 if (cppDefined) { 287 fprintf(stderr, "cannot find C preprocessor: %s \n", CPP); 288 crash(); 289 } else { 290 /* try the other one */ 291 CPP = SUNOS_CPP; 292 if (stat(CPP, &buf) == -1) { /* can't find any cpp */ 293 fprintf(stderr, 294 "cannot find any C preprocessor: %s\n", CPP); 295 crash(); 296 } 297 } 298 } 299 } 300 301 /* 302 * Open input file with given define for C-preprocessor 303 */ 304 static void 305 open_input(char *infile, char *define) 306 { 307 int pd[2]; 308 309 infilename = (infile == NULL) ? "<stdin>" : infile; 310 (void) pipe(pd); 311 switch (fork()) { 312 case 0: 313 find_cpp(); 314 putarg(0, CPP); 315 putarg(1, CPPFLAGS); 316 addarg(define); 317 addarg(infile); 318 addarg((char *) NULL); 319 (void) close(1); 320 (void) dup2(pd[1], 1); 321 (void) close(pd[0]); 322 execv(arglist[0], arglist); 323 perror("execv"); 324 exit(1); 325 case -1: 326 perror("fork"); 327 exit(1); 328 } 329 (void) close(pd[1]); 330 fin = fdopen(pd[0], "r"); 331 if (fin == NULL) { 332 fprintf(stderr, "%s: ", cmdname); 333 perror(infilename); 334 crash(); 335 } 336 } 337 338 /* valid tirpc nettypes */ 339 static char *valid_ti_nettypes[] = { 340 "netpath", 341 "visible", 342 "circuit_v", 343 "datagram_v", 344 "circuit_n", 345 "datagram_n", 346 "udp", 347 "tcp", 348 "raw", 349 NULL 350 }; 351 352 /* valid inetd nettypes */ 353 static char *valid_i_nettypes[] = { 354 "udp", 355 "tcp", 356 NULL 357 }; 358 359 static int 360 check_nettype(char *name, char *list_to_check[]) 361 { 362 int i; 363 for (i = 0; list_to_check[i] != NULL; i++) { 364 if (strcmp(name, list_to_check[i]) == 0) 365 return 1; 366 } 367 fprintf(stderr, "illegal nettype :\'%s\'\n", name); 368 return 0; 369 } 370 371 /* 372 * Compile into an XDR routine output file 373 */ 374 375 static void 376 c_output(infile, define, extend, outfile) 377 char *infile; 378 char *define; 379 int extend; 380 char *outfile; 381 { 382 definition *def; 383 char *include; 384 char *outfilename; 385 long tell; 386 387 c_initialize(); 388 open_input(infile, define); 389 outfilename = extend ? extendfile(infile, outfile) : outfile; 390 open_output(infile, outfilename); 391 add_warning(); 392 if (infile && (include = extendfile(infile, ".h"))) { 393 fprintf(fout, "#include \"%s\"\n", include); 394 free(include); 395 /* .h file already contains rpc/rpc.h */ 396 } else 397 fprintf(fout, "#include <rpc/rpc.h>\n"); 398 tell = ftell(fout); 399 while ((def = get_definition())) { 400 emit(def); 401 } 402 if (extend && tell == ftell(fout)) { 403 (void) unlink(outfilename); 404 } 405 } 406 407 408 void 409 c_initialize(void) 410 { 411 412 /* add all the starting basic types */ 413 414 add_type(1, "int"); 415 add_type(1, "long"); 416 add_type(1, "short"); 417 add_type(1, "bool"); 418 419 add_type(1, "u_int"); 420 add_type(1, "u_long"); 421 add_type(1, "u_short"); 422 423 } 424 425 static const char rpcgen_table_dcl[] = "struct rpcgen_table {\n\ 426 char *(*proc)();\n\ 427 xdrproc_t xdr_arg;\n\ 428 unsigned int len_arg;\n\ 429 xdrproc_t xdr_res;\n\ 430 unsigned int len_res;\n\ 431 };\n"; 432 433 434 static char * 435 generate_guard(char *pathname) 436 { 437 char *filename, *guard, *tmp, *tmp2; 438 439 filename = strrchr(pathname, '/'); /* find last component */ 440 filename = ((filename == 0) ? pathname : filename + 1); 441 guard = strdup(filename); 442 if (guard == NULL) { 443 fprintf(stderr, "out of memory while processing %s\n", filename); 444 crash(); 445 } 446 447 /* convert to upper case */ 448 tmp = guard; 449 while (*tmp) { 450 if (islower((unsigned char)*tmp)) 451 *tmp = toupper((unsigned char)*tmp); 452 tmp++; 453 } 454 455 tmp2 = extendfile(guard, "_H_RPCGEN"); 456 free(guard); 457 guard = tmp2; 458 459 return (guard); 460 } 461 462 /* 463 * Compile into an XDR header file 464 */ 465 466 static void 467 h_output(infile, define, extend, outfile) 468 char *infile; 469 char *define; 470 int extend; 471 char *outfile; 472 { 473 definition *def; 474 char *outfilename; 475 long tell; 476 char *guard; 477 list *l; 478 479 open_input(infile, define); 480 outfilename = extend ? extendfile(infile, outfile) : outfile; 481 open_output(infile, outfilename); 482 add_warning(); 483 if (outfilename || infile) 484 guard = generate_guard(outfilename ? outfilename : infile); 485 else 486 guard = generate_guard("STDIN"); 487 488 fprintf(fout, "#ifndef _%s\n#define _%s\n\n", guard, 489 guard); 490 491 fprintf(fout, "#define RPCGEN_VERSION\t%s\n\n", RPCGEN_VERSION); 492 fprintf(fout, "#include <rpc/rpc.h>\n\n"); 493 494 tell = ftell(fout); 495 /* print data definitions */ 496 while ((def = get_definition())) { 497 print_datadef(def); 498 } 499 500 /* 501 * print function declarations. Do this after data definitions 502 * because they might be used as arguments for functions 503 */ 504 for (l = defined; l != NULL; l = l->next) { 505 print_funcdef(l->val); 506 } 507 if (extend && tell == ftell(fout)) { 508 (void) unlink(outfilename); 509 } else if (tblflag) { 510 fprintf(fout, rpcgen_table_dcl); 511 } 512 fprintf(fout, "\n#endif /* !_%s */\n", guard); 513 514 free(guard); 515 } 516 517 /* 518 * Compile into an RPC service 519 */ 520 static void 521 s_output(argc, argv, infile, define, extend, outfile, nomain, netflag) 522 int argc; 523 char *argv[]; 524 char *infile; 525 char *define; 526 int extend; 527 char *outfile; 528 int nomain; 529 int netflag; 530 { 531 char *include; 532 definition *def; 533 int foundprogram = 0; 534 char *outfilename; 535 536 open_input(infile, define); 537 outfilename = extend ? extendfile(infile, outfile) : outfile; 538 open_output(infile, outfilename); 539 add_warning(); 540 if (infile && (include = extendfile(infile, ".h"))) { 541 fprintf(fout, "#include \"%s\"\n", include); 542 free(include); 543 } else 544 fprintf(fout, "#include <rpc/rpc.h>\n"); 545 546 fprintf(fout, "#include <unistd.h>\n"); 547 fprintf(fout, "#include <stdio.h>\n"); 548 fprintf(fout, "#include <stdlib.h>/* getenv, exit */\n"); 549 if (Cflag) { 550 fprintf(fout, 551 "#include <rpc/pmap_clnt.h> /* for pmap_unset */\n"); 552 fprintf(fout, "#include <string.h> /* strcmp */ \n"); 553 } 554 fprintf(fout, "#include <netdb.h>\n"); /* evas */ 555 if (strcmp(svcclosetime, "-1") == 0) 556 indefinitewait = 1; 557 else if (strcmp(svcclosetime, "0") == 0) 558 exitnow = 1; 559 else if (inetdflag || pmflag) { 560 fprintf(fout, "#include <signal.h>\n"); 561 timerflag = 1; 562 } 563 if (!tirpcflag && inetdflag) 564 fprintf(fout, "#include <sys/ttycom.h>/* TIOCNOTTY */\n"); 565 if (Cflag && (inetdflag || pmflag)) { 566 fprintf(fout, "#ifdef __cplusplus\n"); 567 fprintf(fout, "#include <sysent.h> /* getdtablesize, open */\n"); 568 fprintf(fout, "#endif /* __cplusplus */\n"); 569 570 if (tirpcflag) 571 fprintf(fout, "#include <unistd.h> /* setsid */\n"); 572 } 573 if (tirpcflag) 574 fprintf(fout, "#include <sys/types.h>\n"); 575 576 fprintf(fout, "#include <memory.h>\n"); 577 if (tirpcflag) 578 fprintf(fout, "#include <stropts.h>\n"); 579 580 if (inetdflag || !tirpcflag) { 581 fprintf(fout, "#include <sys/socket.h>\n"); 582 fprintf(fout, "#include <netinet/in.h>\n"); 583 } 584 if ((netflag || pmflag) && tirpcflag) { 585 fprintf(fout, "#include <netconfig.h>\n"); 586 } 587 if (/* timerflag && */ tirpcflag) 588 fprintf(fout, "#include <sys/resource.h> /* rlimit */\n"); 589 if (logflag || inetdflag || pmflag) { 590 fprintf(fout, "#include <syslog.h>\n"); 591 fprintf(fout, "#include <errno.h>\n"); 592 } 593 /* for ANSI-C */ 594 fprintf(fout, "\n#ifdef __STDC__\n#define SIG_PF void(*)(int)\n#endif\n"); 595 596 fprintf(fout, "\n#ifdef DEBUG\n#define RPC_SVC_FG\n#endif\n"); 597 if (timerflag) 598 fprintf(fout, "\n#define _RPCSVC_CLOSEDOWN %s\n", svcclosetime); 599 while ((def = get_definition())) { 600 foundprogram |= (def->def_kind == DEF_PROGRAM); 601 } 602 if (extend && !foundprogram) { 603 (void) unlink(outfilename); 604 return; 605 } 606 if (callerflag) /* EVAS */ 607 fprintf(fout, "\nstatic SVCXPRT *caller;\n"); /* EVAS */ 608 write_most(infile, netflag, nomain); 609 if (!nomain) { 610 if (!do_registers(argc, argv)) { 611 if (outfilename) 612 (void) unlink(outfilename); 613 usage(); 614 } 615 write_rest(); 616 } 617 } 618 619 /* 620 * generate client side stubs 621 */ 622 static void 623 l_output(infile, define, extend, outfile) 624 char *infile; 625 char *define; 626 int extend; 627 char *outfile; 628 { 629 char *include; 630 definition *def; 631 int foundprogram = 0; 632 char *outfilename; 633 634 open_input(infile, define); 635 outfilename = extend ? extendfile(infile, outfile) : outfile; 636 open_output(infile, outfilename); 637 add_warning(); 638 if (Cflag) 639 fprintf(fout, "#include <memory.h> /* for memset */\n"); 640 if (infile && (include = extendfile(infile, ".h"))) { 641 fprintf(fout, "#include \"%s\"\n", include); 642 free(include); 643 } else 644 fprintf(fout, "#include <rpc/rpc.h>\n"); 645 while ((def = get_definition())) 646 foundprogram |= (def->def_kind == DEF_PROGRAM); 647 648 if (extend && !foundprogram) { 649 (void) unlink(outfilename); 650 return; 651 } 652 write_stubs(); 653 } 654 655 /* 656 * generate the dispatch table 657 */ 658 static void 659 t_output(infile, define, extend, outfile) 660 char *infile; 661 char *define; 662 int extend; 663 char *outfile; 664 { 665 definition *def; 666 int foundprogram = 0; 667 char *outfilename; 668 669 open_input(infile, define); 670 outfilename = extend ? extendfile(infile, outfile) : outfile; 671 open_output(infile, outfilename); 672 add_warning(); 673 while ((def = get_definition())) 674 foundprogram |= (def->def_kind == DEF_PROGRAM); 675 676 if (extend && !foundprogram) { 677 (void) unlink(outfilename); 678 return; 679 } 680 write_tables(); 681 } 682 683 /* sample routine for the server template */ 684 static void 685 svc_output(infile, define, extend, outfile) 686 char *infile; 687 char *define; 688 int extend; 689 char *outfile; 690 { 691 definition *def; 692 char *include; 693 char *outfilename; 694 long tell; 695 696 open_input(infile, define); 697 outfilename = extend ? extendfile(infile, outfile) : outfile; 698 checkfiles(infile, outfilename); /* check if outfile already 699 * exists. if so, print an 700 * error message and exit */ 701 open_output(infile, outfilename); 702 add_sample_msg(); 703 704 if (infile && (include = extendfile(infile, ".h"))) { 705 fprintf(fout, "#include \"%s\"\n", include); 706 free(include); 707 } else 708 fprintf(fout, "#include <rpc/rpc.h>\n"); 709 710 tell = ftell(fout); 711 while ((def = get_definition())) 712 write_sample_svc(def); 713 714 if (extend && tell == ftell(fout)) 715 (void) unlink(outfilename); 716 } 717 718 719 /* sample main routine for client */ 720 static void 721 clnt_output(infile, define, extend, outfile) 722 char *infile; 723 char *define; 724 int extend; 725 char *outfile; 726 { 727 definition *def; 728 char *include, *outfilename; 729 long tell; 730 int has_program = 0; 731 732 open_input(infile, define); 733 outfilename = extend ? extendfile(infile, outfile) : outfile; 734 735 /* 736 * check if outfile already exists. if so, 737 * print an error message and exit 738 */ 739 checkfiles(infile, outfilename); 740 741 open_output(infile, outfilename); 742 add_sample_msg(); 743 if (infile && (include = extendfile(infile, ".h"))) { 744 fprintf(fout, "#include \"%s\"\n", include); 745 free(include); 746 } else 747 fprintf(fout, "#include <rpc/rpc.h>\n"); 748 tell = ftell(fout); 749 while ((def = get_definition())) 750 has_program += write_sample_clnt(def); 751 752 if (has_program) 753 write_sample_clnt_main(); 754 755 if (extend && tell == ftell(fout)) 756 (void) unlink(outfilename); 757 } 758 759 /* 760 * Perform registrations for service output 761 * Return 0 if failed; 1 otherwise. 762 */ 763 static int 764 do_registers(argc, argv) 765 int argc; 766 char *argv[]; 767 { 768 int i; 769 770 if (inetdflag || !tirpcflag) { 771 for (i = 1; i < argc; i++) { 772 if (streq(argv[i], "-s")) { 773 if (!check_nettype(argv[i + 1], valid_i_nettypes)) 774 return 0; 775 write_inetd_register(argv[i + 1]); 776 i++; 777 } 778 } 779 } else { 780 for (i = 1; i < argc; i++) 781 if (streq(argv[i], "-s")) { 782 if (!check_nettype(argv[i + 1], valid_ti_nettypes)) 783 return 0; 784 write_nettype_register(argv[i + 1]); 785 i++; 786 } else if (streq(argv[i], "-n")) { 787 write_netid_register(argv[i + 1]); 788 i++; 789 } 790 } 791 return 1; 792 } 793 794 /* 795 * Add another argument to the arg list 796 */ 797 static void 798 addarg(cp) 799 char *cp; 800 { 801 if (argcount >= ARGLISTLEN) { 802 fprintf(stderr, "rpcgen: too many defines\n"); 803 crash(); 804 /* NOTREACHED */ 805 } 806 arglist[argcount++] = cp; 807 808 } 809 810 static void 811 putarg(where, cp) 812 char *cp; 813 int where; 814 { 815 if (where >= ARGLISTLEN) { 816 fprintf(stderr, "rpcgen: arglist coding error\n"); 817 crash(); 818 /* NOTREACHED */ 819 } 820 arglist[where] = cp; 821 } 822 823 /* 824 * if input file is stdin and an output file is specified then complain 825 * if the file already exists. Otherwise the file may get overwritten 826 * If input file does not exist, exit with an error 827 */ 828 static void 829 checkfiles(infile, outfile) 830 char *infile; 831 char *outfile; 832 { 833 struct stat buf; 834 835 if (infile) /* infile ! = NULL */ 836 if (stat(infile, &buf) == -1) { 837 perror(infile); 838 crash(); 839 } 840 #if 0 841 if (outfile) { 842 if (stat(outfile, &buf) == -1) 843 return; /* file does not exist */ 844 else { 845 fprintf(stderr, 846 "file '%s' already exists and may be overwritten\n", 847 outfile); 848 crash(); 849 } 850 } 851 #endif 852 } 853 854 /* 855 * Parse command line arguments 856 */ 857 static int 858 parseargs(argc, argv, cmd) 859 int argc; 860 char *argv[]; 861 struct commandline *cmd; 862 { 863 int i, j, nflags; 864 char c, flag[(1 << 8 * sizeof(char))]; 865 866 cmdname = argv[0]; 867 cmd->infile = cmd->outfile = NULL; 868 if (argc < 2) 869 return (0); 870 871 allfiles = 0; 872 flag['c'] = 0; 873 flag['h'] = 0; 874 flag['l'] = 0; 875 flag['m'] = 0; 876 flag['o'] = 0; 877 flag['s'] = 0; 878 flag['n'] = 0; 879 flag['t'] = 0; 880 flag['S'] = 0; 881 flag['C'] = 0; 882 for (i = 1; i < argc; i++) { 883 if (argv[i][0] != '-') { 884 if (cmd->infile) { 885 fprintf(stderr, 886 "Cannot specify more than one input file!\n"); 887 return (0); 888 } 889 cmd->infile = argv[i]; 890 } else { 891 for (j = 1; argv[i][j] != 0; j++) { 892 c = argv[i][j]; 893 switch (c) { 894 case 'A': 895 callerflag = 1; 896 break; 897 case 'a': 898 allfiles = 1; 899 break; 900 case 'c': 901 case 'h': 902 case 'l': 903 case 'm': 904 case 't': 905 if (flag[(unsigned char)c]) 906 return (0); 907 flag[(unsigned char)c] = 1; 908 break; 909 case 'S': 910 /* 911 * sample flag: Ss or Sc. Ss means 912 * set flag['S']; Sc means set 913 * flag['C']; 914 */ 915 c = argv[i][++j]; /* get next char */ 916 if (c == 's') 917 c = 'S'; 918 else if (c == 'c') 919 c = 'C'; 920 else 921 return (0); 922 923 if (flag[(unsigned char)c]) 924 return (0); 925 flag[(unsigned char)c] = 1; 926 break; 927 case 'C': /* ANSI C syntax */ 928 Cflag = 1; 929 break; 930 931 case 'b': 932 /* 933 * turn TIRPC flag off for 934 * generating backward compatible 935 */ 936 tirpcflag = 0; 937 break; 938 939 case 'I': 940 inetdflag = 1; 941 break; 942 case 'N': 943 newstyle = 1; 944 break; 945 case 'L': 946 logflag = 1; 947 break; 948 case 'K': 949 if (++i == argc) 950 return (0); 951 svcclosetime = argv[i]; 952 goto nextarg; 953 case 'T': 954 tblflag = 1; 955 break; 956 case 'i': 957 if (++i == argc) 958 return (0); 959 doinline = atoi(argv[i]); 960 goto nextarg; 961 case 'n': 962 case 'o': 963 case 's': 964 if (argv[i][j - 1] != '-' || 965 argv[i][j + 1] != 0) 966 return (0); 967 flag[(unsigned char)c] = 1; 968 if (++i == argc) 969 return (0); 970 if (c == 's') { 971 if (!streq(argv[i], "udp") && 972 !streq(argv[i], "tcp")) 973 return (0); 974 } else if (c == 'o') { 975 if (cmd->outfile) 976 return (0); 977 cmd->outfile = argv[i]; 978 } 979 goto nextarg; 980 case 'D': 981 if (argv[i][j - 1] != '-') 982 return (0); 983 (void) addarg(argv[i]); 984 goto nextarg; 985 case 'Y': 986 if (++i == argc) 987 return (0); 988 if (snprintf(pathbuf, sizeof pathbuf, 989 "%s/cpp", argv[i]) >= sizeof pathbuf) 990 usage(); 991 CPP = pathbuf; 992 cppDefined = 1; 993 goto nextarg; 994 default: 995 return (0); 996 } 997 } 998 nextarg: 999 ; 1000 } 1001 } 1002 1003 cmd->cflag = flag['c']; 1004 cmd->hflag = flag['h']; 1005 cmd->lflag = flag['l']; 1006 cmd->mflag = flag['m']; 1007 cmd->nflag = flag['n']; 1008 cmd->sflag = flag['s']; 1009 cmd->tflag = flag['t']; 1010 cmd->Ssflag = flag['S']; 1011 cmd->Scflag = flag['C']; 1012 1013 if (tirpcflag) { 1014 pmflag = inetdflag ? 0 : 1; /* pmflag or inetdflag is 1015 * always TRUE */ 1016 if (inetdflag && cmd->nflag) { 1017 /* netid not allowed with inetdflag */ 1018 fprintf(stderr, "Cannot use netid flag with inetd flag!\n"); 1019 return (0); 1020 } 1021 } else { 1022 /* 4.1 mode */ 1023 pmflag = 0; /* set pmflag only in tirpcmode */ 1024 inetdflag = 1; /* inetdflag is TRUE by default */ 1025 if (cmd->nflag) { 1026 /* netid needs TIRPC */ 1027 fprintf(stderr, "Cannot use netid flag without TIRPC!\n"); 1028 return (0); 1029 } 1030 } 1031 1032 if (newstyle && (tblflag || cmd->tflag)) { 1033 fprintf(stderr, "Cannot use table flags with newstyle!\n"); 1034 return (0); 1035 } 1036 /* check no conflicts with file generation flags */ 1037 nflags = cmd->cflag + cmd->hflag + cmd->lflag + cmd->mflag + 1038 cmd->sflag + cmd->nflag + cmd->tflag + cmd->Ssflag + cmd->Scflag; 1039 1040 if (nflags == 0) { 1041 if (cmd->outfile != NULL || cmd->infile == NULL) 1042 return (0); 1043 } else if (nflags > 1) { 1044 fprintf(stderr, "Cannot have more than one file generation flag!\n"); 1045 return (0); 1046 } 1047 return (1); 1048 } 1049 1050 static void 1051 usage(void) 1052 { 1053 fprintf(stderr, "usage: %s [-abACILNT] [-Dname[=value]] [-i lines] " 1054 "[-K seconds] infile\n", cmdname); 1055 fprintf(stderr, " %s [-c | -h | -l | -m | -t | -Sc | -Ss] " 1056 "[-o outfile] [infile]\n", cmdname); 1057 fprintf(stderr, " %s [-s nettype]* [-o outfile] [infile]\n", cmdname); 1058 exit(1); 1059 } 1060