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