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