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