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