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