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