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