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" 34static 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: stable/11/usr.bin/rpcgen/rpc_main.c 353939 2019-10-23 17:28:35Z brooks $"); 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 59static void c_output(const char *, const char *, int, const char *); 60static void h_output(const char *, const char *, int, const char *, int); 61static void l_output(const char *, const char *, int, const char *); 62static void t_output(const char *, const char *, int, const char *); 63static void clnt_output(const char *, const char *, int, const char * ); 64static char *generate_guard(const char *); 65static void c_initialize(void); 66 67static void usage(void); 68static void options_usage(void); 69static int do_registers(int, const char **); 70static int parseargs(int, const char **, struct commandline *); 71static void svc_output(const char *, const char *, int, const char *); 72static void mkfile_output(struct commandline *); 73static 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 78static const char *svcclosetime = "120"; 79static const char *CPP = NULL; 80static const char CPPFLAGS[] = "-C"; 81static char pathbuf[MAXPATHLEN + 1]; 82static const char *allv[] = { 83 "rpcgen", "-s", "udp", "-s", "tcp", 84}; 85static int allc = nitems(allv); 86static const char *allnv[] = { 87 "rpcgen", "-s", "netpath", 88}; 89static int allnc = nitems(allnv); 90 91/* 92 * machinations for handling expanding argument list 93 */ 94static void addarg(const char *); /* add another argument to the list */ 95static void insarg(int, const char *); /* insert arg at specified location */ 96static void checkfiles(const char *, const char *); 97 /* check if out file already exists */ 98 99static char **arglist; 100static int argcount = 0; 101static int argmax = 0; 102 103int nonfatalerrors; /* errors */ 104int inetdflag = 0; /* Support for inetd is disabled by default, use -I */ 105int pmflag = 0; /* Support for port monitors is disabled by default */ 106int tirpc_socket = 1; /* TI-RPC on socket, no TLI library */ 107int logflag; /* Use syslog instead of fprintf for errors */ 108int tblflag; /* Support for dispatch table file */ 109int mtflag = 0; /* Support for MT */ 110 111#define INLINE 0 112/* length at which to start doing an inline */ 113 114int inline_size = INLINE; 115/* 116 * Length at which to start doing an inline. INLINE = default 117 * if 0, no xdr_inline code 118 */ 119 120int indefinitewait; /* If started by port monitors, hang till it wants */ 121int exitnow; /* If started by port monitors, exit after the call */ 122int timerflag; /* TRUE if !indefinite && !exitnow */ 123int newstyle; /* newstyle of passing arguments (by value) */ 124int CCflag = 0; /* C++ files */ 125static int allfiles; /* generate all files */ 126int tirpcflag = 1; /* generating code for tirpc, by default */ 127xdrfunc *xdrfunc_head = NULL; /* xdr function list */ 128xdrfunc *xdrfunc_tail = NULL; /* xdr function list */ 129pid_t childpid; 130 131 132int 133main(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 */ 213static char * 214extendfile(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 */ 237static void 238open_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 260static void 261add_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 */ 270static void 271prepend_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 */ 297static void 298open_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 */ 328static 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 */ 343static const char *valid_i_nettypes[] = 344{ 345 "udp", 346 "tcp", 347 NULL 348 }; 349 350static int 351check_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 363static const char * 364file_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 377static void 378c_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 406void 407c_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 421static 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 430char * 431generate_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 guard = extendfile(guard, "_H_RPCGEN"); 471 return (guard); 472} 473 474/* 475 * Compile into an XDR header file 476 */ 477 478 479static void 480h_output(const char *infile, const char *define, int extend, const char *outfile, int headeronly) 481{ 482 definition *def; 483 const char *outfilename; 484 long tell; 485 const char *guard; 486 list *l; 487 xdrfunc *xdrfuncp; 488 489 open_input(infile, define); 490 outfilename = extend ? extendfile(infile, outfile) : outfile; 491 open_output(infile, outfilename); 492 add_warning(); 493 if (outfilename || infile){ 494 guard = generate_guard(outfilename ? outfilename: infile); 495 } else 496 guard = "STDIN_"; 497 498 f_print(fout, "#ifndef _%s\n#define _%s\n\n", guard, 499 guard); 500 501 f_print(fout, "#include <rpc/rpc.h>\n"); 502 503 if (mtflag) 504 f_print(fout, "#include <pthread.h>\n"); 505 506 /* put the C++ support */ 507 if (!CCflag) { 508 f_print(fout, "\n#ifdef __cplusplus\n"); 509 f_print(fout, "extern \"C\" {\n"); 510 f_print(fout, "#endif\n\n"); 511 } 512 513 /* put in a typedef for quadprecision. Only with Cflag */ 514 515 tell = ftell(fout); 516 517 /* print data definitions */ 518 while ( (def = get_definition()) ) { 519 print_datadef(def, headeronly); 520 } 521 522 /* 523 * print function declarations. 524 * Do this after data definitions because they might be used as 525 * arguments for functions 526 */ 527 for (l = defined; l != NULL; l = l->next) { 528 print_funcdef(l->val, headeronly); 529 } 530 /* Now print all xdr func declarations */ 531 if (xdrfunc_head != NULL){ 532 533 f_print(fout, 534 "\n/* the xdr functions */\n"); 535 536 if (CCflag){ 537 f_print(fout, "\n#ifdef __cplusplus\n"); 538 f_print(fout, "extern \"C\" {\n"); 539 f_print(fout, "#endif\n"); 540 } 541 542 xdrfuncp = xdrfunc_head; 543 while (xdrfuncp != NULL){ 544 print_xdr_func_def(xdrfuncp->name, xdrfuncp->pointerp); 545 xdrfuncp = xdrfuncp->next; 546 } 547 } 548 549 if (extend && tell == ftell(fout)) { 550 (void) unlink(outfilename); 551 } else if (tblflag) { 552 f_print(fout, rpcgen_table_dcl); 553 } 554 555 f_print(fout, "\n#ifdef __cplusplus\n"); 556 f_print(fout, "}\n"); 557 f_print(fout, "#endif\n"); 558 559 f_print(fout, "\n#endif /* !_%s */\n", guard); 560} 561 562/* 563 * Compile into an RPC service 564 */ 565static void 566s_output(int argc, const char *argv[], const char *infile, const char *define, 567 int extend, const char *outfile, int nomain, int netflag) 568{ 569 char *include; 570 definition *def; 571 int foundprogram = 0; 572 const char *outfilename; 573 574 open_input(infile, define); 575 outfilename = extend ? extendfile(infile, outfile) : outfile; 576 open_output(infile, outfilename); 577 add_warning(); 578 if (infile && (include = extendfile(infile, ".h"))) { 579 f_print(fout, "#include \"%s\"\n", include); 580 free(include); 581 } else 582 f_print(fout, "#include <rpc/rpc.h>\n"); 583 584 f_print(fout, "#include <stdio.h>\n"); 585 f_print(fout, "#include <stdlib.h> /* getenv, exit */\n"); 586 f_print (fout, "#include <rpc/pmap_clnt.h> /* for pmap_unset */\n"); 587 f_print (fout, "#include <string.h> /* strcmp */\n"); 588 if (tirpcflag) 589 f_print(fout, "#include <rpc/rpc_com.h>\n"); 590 if (strcmp(svcclosetime, "-1") == 0) 591 indefinitewait = 1; 592 else if (strcmp(svcclosetime, "0") == 0) 593 exitnow = 1; 594 else if (inetdflag || pmflag) { 595 f_print(fout, "#include <signal.h>\n"); 596 timerflag = 1; 597 } 598 599 if (!tirpcflag && inetdflag) 600 f_print(fout, "#include <sys/ttycom.h> /* TIOCNOTTY */\n"); 601 if (inetdflag || pmflag) { 602 f_print(fout, "#ifdef __cplusplus\n"); 603 f_print(fout, 604 "#include <sys/sysent.h> /* getdtablesize, open */\n"); 605 f_print(fout, "#endif /* __cplusplus */\n"); 606 } 607 if (tirpcflag) { 608 f_print(fout, "#include <fcntl.h> /* open */\n"); 609 f_print(fout, "#include <unistd.h> /* fork / setsid */\n"); 610 f_print(fout, "#include <sys/types.h>\n"); 611 } 612 613 f_print(fout, "#include <string.h>\n"); 614 if (inetdflag || !tirpcflag) { 615 f_print(fout, "#include <sys/socket.h>\n"); 616 f_print(fout, "#include <netinet/in.h>\n"); 617 } 618 619 if ((netflag || pmflag) && tirpcflag && !nomain) { 620 f_print(fout, "#include <netconfig.h>\n"); 621 } 622 if (tirpcflag) 623 f_print(fout, "#include <sys/resource.h> /* rlimit */\n"); 624 if (logflag || inetdflag || pmflag || tirpcflag) 625 f_print(fout, "#include <syslog.h>\n"); 626 627 f_print(fout, "\n#ifdef DEBUG\n#define RPC_SVC_FG\n#endif\n"); 628 if (timerflag) 629 f_print(fout, "\n#define _RPCSVC_CLOSEDOWN %s\n", 630 svcclosetime); 631 while ( (def = get_definition()) ) { 632 foundprogram |= (def->def_kind == DEF_PROGRAM); 633 } 634 if (extend && !foundprogram) { 635 (void) unlink(outfilename); 636 return; 637 } 638 write_most(infile, netflag, nomain); 639 if (!nomain) { 640 if (!do_registers(argc, argv)) { 641 if (outfilename) 642 (void) unlink(outfilename); 643 usage(); 644 } 645 write_rest(); 646 } 647} 648 649/* 650 * generate client side stubs 651 */ 652static void 653l_output(const char *infile, const char *define, int extend, const char *outfile) 654{ 655 char *include; 656 definition *def; 657 int foundprogram = 0; 658 const char *outfilename; 659 660 open_input(infile, define); 661 outfilename = extend ? extendfile(infile, outfile) : outfile; 662 open_output(infile, outfilename); 663 add_warning(); 664 f_print (fout, "#include <string.h> /* for memset */\n"); 665 if (infile && (include = extendfile(infile, ".h"))) { 666 f_print(fout, "#include \"%s\"\n", include); 667 free(include); 668 } else 669 f_print(fout, "#include <rpc/rpc.h>\n"); 670 while ( (def = get_definition()) ) { 671 foundprogram |= (def->def_kind == DEF_PROGRAM); 672 } 673 if (extend && !foundprogram) { 674 (void) unlink(outfilename); 675 return; 676 } 677 write_stubs(); 678} 679 680/* 681 * generate the dispatch table 682 */ 683static void 684t_output(const char *infile, const char *define, int extend, const char *outfile) 685{ 686 definition *def; 687 int foundprogram = 0; 688 const char *outfilename; 689 690 open_input(infile, define); 691 outfilename = extend ? extendfile(infile, outfile) : outfile; 692 open_output(infile, outfilename); 693 add_warning(); 694 while ( (def = get_definition()) ) { 695 foundprogram |= (def->def_kind == DEF_PROGRAM); 696 } 697 if (extend && !foundprogram) { 698 (void) unlink(outfilename); 699 return; 700 } 701 write_tables(); 702} 703 704/* sample routine for the server template */ 705static void 706svc_output(const char *infile, const char *define, int extend, const char *outfile) 707{ 708 definition *def; 709 char *include; 710 const char *outfilename; 711 long tell; 712 open_input(infile, define); 713 outfilename = extend ? extendfile(infile, outfile) : outfile; 714 checkfiles(infile, outfilename); 715 /* 716 * Check if outfile already exists. 717 * if so, print an error message and exit 718 */ 719 open_output(infile, outfilename); 720 add_sample_msg(); 721 722 if (infile && (include = extendfile(infile, ".h"))) { 723 f_print(fout, "#include \"%s\"\n", include); 724 free(include); 725 } else 726 f_print(fout, "#include <rpc/rpc.h>\n"); 727 728 tell = ftell(fout); 729 while ( (def = get_definition()) ) { 730 write_sample_svc(def); 731 } 732 if (extend && tell == ftell(fout)) { 733 (void) unlink(outfilename); 734 } 735} 736 737/* sample main routine for client */ 738static void 739clnt_output(const char *infile, const char *define, int extend, const char *outfile) 740{ 741 definition *def; 742 char *include; 743 const char *outfilename; 744 long tell; 745 int has_program = 0; 746 747 open_input(infile, define); 748 outfilename = extend ? extendfile(infile, outfile) : outfile; 749 checkfiles(infile, outfilename); 750 /* 751 * Check if outfile already exists. 752 * if so, print an error message and exit 753 */ 754 755 open_output(infile, outfilename); 756 add_sample_msg(); 757 if (infile && (include = extendfile(infile, ".h"))) { 758 f_print(fout, "#include \"%s\"\n", include); 759 free(include); 760 } else 761 f_print(fout, "#include <rpc/rpc.h>\n"); 762 f_print(fout, "#include <stdio.h>\n"); 763 f_print(fout, "#include <stdlib.h>\n"); 764 tell = ftell(fout); 765 while ( (def = get_definition()) ) { 766 has_program += write_sample_clnt(def); 767 } 768 769 if (has_program) 770 write_sample_clnt_main(); 771 772 if (extend && tell == ftell(fout)) { 773 (void) unlink(outfilename); 774 } 775} 776 777 778static void mkfile_output(struct commandline *cmd) 779{ 780 const char *mkfilename, *clientname, *clntname, *xdrname, *hdrname; 781 const char *servername, *svcname, *servprogname, *clntprogname; 782 char *temp, *mkftemp; 783 784 svcname = file_name(cmd->infile, "_svc.c"); 785 clntname = file_name(cmd->infile, "_clnt.c"); 786 xdrname = file_name(cmd->infile, "_xdr.c"); 787 hdrname = file_name(cmd->infile, ".h"); 788 789 790 if (allfiles){ 791 servername = extendfile(cmd->infile, "_server.c"); 792 clientname = extendfile(cmd->infile, "_client.c"); 793 }else{ 794 servername = " "; 795 clientname = " "; 796 } 797 servprogname = extendfile(cmd->infile, "_server"); 798 clntprogname = extendfile(cmd->infile, "_client"); 799 800 if (allfiles){ 801 mkftemp = xmalloc(strlen("makefile.") + 802 strlen(cmd->infile) + 1); 803 temp = strrchr(cmd->infile, '.'); 804 strcpy(mkftemp, "makefile."); 805 (void) strncat(mkftemp, cmd->infile, 806 (temp - cmd->infile)); 807 mkfilename = mkftemp; 808 } else 809 mkfilename = cmd->outfile; 810 811 812 checkfiles(NULL, mkfilename); 813 open_output(NULL, mkfilename); 814 815 f_print(fout, "\n# This is a template makefile generated\ 816 by rpcgen \n"); 817 818 f_print(fout, "\n# Parameters \n\n"); 819 820 f_print(fout, "CLIENT = %s\nSERVER = %s\n\n", 821 clntprogname, servprogname); 822 f_print(fout, "SOURCES_CLNT.c = \nSOURCES_CLNT.h = \n"); 823 f_print(fout, "SOURCES_SVC.c = \nSOURCES_SVC.h = \n"); 824 f_print(fout, "SOURCES.x = %s\n\n", cmd->infile); 825 f_print(fout, "TARGETS_SVC.c = %s %s %s \n", 826 svcname, servername, xdrname); 827 f_print(fout, "TARGETS_CLNT.c = %s %s %s \n", 828 clntname, clientname, xdrname); 829 f_print(fout, "TARGETS = %s %s %s %s %s %s\n\n", 830 hdrname, xdrname, clntname, 831 svcname, clientname, servername); 832 833 f_print(fout, "OBJECTS_CLNT = $(SOURCES_CLNT.c:%%.c=%%.o) \ 834$(TARGETS_CLNT.c:%%.c=%%.o) "); 835 836 f_print(fout, "\nOBJECTS_SVC = $(SOURCES_SVC.c:%%.c=%%.o) \ 837$(TARGETS_SVC.c:%%.c=%%.o) "); 838 839 840 f_print(fout, "\n# Compiler flags \n"); 841 if (mtflag) 842 f_print(fout, "\nCFLAGS += -D_REENTRANT -D_THEAD_SAFE \nLDLIBS += -pthread\n"); 843 844 f_print(fout, "RPCGENFLAGS = \n"); 845 846 f_print(fout, "\n# Targets \n\n"); 847 848 f_print(fout, "all : $(CLIENT) $(SERVER)\n\n"); 849 f_print(fout, "$(TARGETS) : $(SOURCES.x) \n"); 850 f_print(fout, "\trpcgen $(RPCGENFLAGS) $(SOURCES.x)\n\n"); 851 if (allfiles) { 852 f_print(fout, "\trpcgen -Sc $(RPCGENFLAGS) $(SOURCES.x) -o %s\n\n", clientname); 853 f_print(fout, "\trpcgen -Ss $(RPCGENFLAGS) $(SOURCES.x) -o %s\n\n", servername); 854 } 855 f_print(fout, "$(OBJECTS_CLNT) : $(SOURCES_CLNT.c) $(SOURCES_CLNT.h) \ 856$(TARGETS_CLNT.c) \n\n"); 857 858 f_print(fout, "$(OBJECTS_SVC) : $(SOURCES_SVC.c) $(SOURCES_SVC.h) \ 859$(TARGETS_SVC.c) \n\n"); 860 f_print(fout, "$(CLIENT) : $(OBJECTS_CLNT) \n"); 861 f_print(fout, "\t$(CC) -o $(CLIENT) $(OBJECTS_CLNT) \ 862$(LDLIBS) \n\n"); 863 f_print(fout, "$(SERVER) : $(OBJECTS_SVC) \n"); 864 f_print(fout, "\t$(CC) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS)\n\n"); 865 f_print(fout, "clean:\n\t rm -f core $(TARGETS) $(OBJECTS_CLNT) \ 866$(OBJECTS_SVC) $(CLIENT) $(SERVER)\n\n"); 867} 868 869 870 871/* 872 * Perform registrations for service output 873 * Return 0 if failed; 1 otherwise. 874 */ 875static int 876do_registers(int argc, const char *argv[]) 877{ 878 int i; 879 880 if (inetdflag || !tirpcflag) { 881 for (i = 1; i < argc; i++) { 882 if (streq(argv[i], "-s")) { 883 if (!check_nettype(argv[i + 1], 884 valid_i_nettypes)) 885 return (0); 886 write_inetd_register(argv[i + 1]); 887 i++; 888 } 889 } 890 } else { 891 for (i = 1; i < argc; i++) 892 if (streq(argv[i], "-s")) { 893 if (!check_nettype(argv[i + 1], 894 valid_ti_nettypes)) 895 return (0); 896 write_nettype_register(argv[i + 1]); 897 i++; 898 } else if (streq(argv[i], "-n")) { 899 write_netid_register(argv[i + 1]); 900 i++; 901 } 902 } 903 return (1); 904} 905 906/* 907 * Extend the argument list 908 */ 909static void 910moreargs(void) 911{ 912 char **newarglist; 913 914 argmax = argmax == 0 ? 32 : argmax << 1; 915 if (argmax > INT_MAX / 4) { 916 warnx("refusing to allocate too many arguments"); 917 crash(); 918 } 919 newarglist = realloc(arglist, argmax * sizeof(*arglist)); 920 if (newarglist == NULL) { 921 warnx("unable to allocate arglist"); 922 crash(); 923 } 924 free(arglist); 925 arglist = newarglist; 926} 927 928/* 929 * Add another argument to the arg list 930 */ 931static void 932addarg(const char *cp) 933{ 934 if (argcount >= argmax) 935 moreargs(); 936 937 if (cp != NULL) 938 arglist[argcount++] = xstrdup(cp); 939 else 940 arglist[argcount++] = NULL; 941} 942 943/* 944 * Insert an argument at the specified location 945 */ 946static void 947insarg(int place, const char *cp) 948{ 949 int i; 950 951 if (argcount >= argmax) 952 moreargs(); 953 954 /* Move up existing arguments */ 955 for (i = argcount - 1; i >= place; i--) 956 arglist[i + 1] = arglist[i]; 957 958 arglist[place] = xstrdup(cp); 959 argcount++; 960} 961 962/* 963 * if input file is stdin and an output file is specified then complain 964 * if the file already exists. Otherwise the file may get overwritten 965 * If input file does not exist, exit with an error 966 */ 967 968static void 969checkfiles(const char *infile, const char *outfile) 970{ 971 972 struct stat buf; 973 974 if (infile) /* infile ! = NULL */ 975 if (stat(infile, &buf) < 0) 976 { 977 warn("%s", infile); 978 crash(); 979 } 980 if (outfile) { 981 if (stat(outfile, &buf) < 0) 982 return; /* file does not exist */ 983 else { 984 warnx("file '%s' already exists and may be overwritten", outfile); 985 crash(); 986 } 987 } 988} 989 990/* 991 * Parse command line arguments 992 */ 993static int 994parseargs(int argc, const char *argv[], struct commandline *cmd) 995{ 996 int i; 997 int j; 998 char c, ch; 999 char flag[(1 << 8 * sizeof (char))]; 1000 int nflags; 1001 1002 cmd->infile = cmd->outfile = NULL; 1003 if (argc < 2) { 1004 return (0); 1005 } 1006 allfiles = 0; 1007 flag['c'] = 0; 1008 flag['h'] = 0; 1009 flag['l'] = 0; 1010 flag['m'] = 0; 1011 flag['o'] = 0; 1012 flag['s'] = 0; 1013 flag['n'] = 0; 1014 flag['t'] = 0; 1015 flag['S'] = 0; 1016 flag['C'] = 0; 1017 flag['M'] = 0; 1018 1019 for (i = 1; i < argc; i++) { 1020 if (argv[i][0] != '-') { 1021 if (cmd->infile) { 1022 warnx("cannot specify more than one input file"); 1023 return (0); 1024 } 1025 cmd->infile = argv[i]; 1026 } else { 1027 for (j = 1; argv[i][j] != 0; j++) { 1028 c = argv[i][j]; 1029 switch (c) { 1030 case 'a': 1031 allfiles = 1; 1032 break; 1033 case 'c': 1034 case 'h': 1035 case 'l': 1036 case 'm': 1037 case 't': 1038 if (flag[(int)c]) { 1039 return (0); 1040 } 1041 flag[(int)c] = 1; 1042 break; 1043 case 'S': 1044 /* 1045 * sample flag: Ss or Sc. 1046 * Ss means set flag['S']; 1047 * Sc means set flag['C']; 1048 * Sm means set flag['M']; 1049 */ 1050 ch = argv[i][++j]; /* get next char */ 1051 if (ch == 's') 1052 ch = 'S'; 1053 else if (ch == 'c') 1054 ch = 'C'; 1055 else if (ch == 'm') 1056 ch = 'M'; 1057 else 1058 return (0); 1059 1060 if (flag[(int)ch]) { 1061 return (0); 1062 } 1063 flag[(int)ch] = 1; 1064 break; 1065 case 'C': /* ANSI C syntax */ 1066 ch = argv[i][j+1]; /* get next char */ 1067 1068 if (ch != 'C') 1069 break; 1070 CCflag = 1; 1071 break; 1072 case 'b': 1073 /* 1074 * Turn TIRPC flag off for 1075 * generating backward compatible 1076 * code 1077 */ 1078 tirpcflag = 0; 1079 break; 1080 1081 case 'I': 1082 inetdflag = 1; 1083 break; 1084 case 'N': 1085 newstyle = 1; 1086 break; 1087 case 'L': 1088 logflag = 1; 1089 break; 1090 case 'P': 1091 pmflag = 1; 1092 break; 1093 case 'K': 1094 if (++i == argc) { 1095 return (0); 1096 } 1097 svcclosetime = argv[i]; 1098 goto nextarg; 1099 case 'T': 1100 tblflag = 1; 1101 break; 1102 case 'M': 1103 mtflag = 1; 1104 break; 1105 case 'i' : 1106 if (++i == argc) { 1107 return (0); 1108 } 1109 inline_size = atoi(argv[i]); 1110 goto nextarg; 1111 case 'n': 1112 case 'o': 1113 case 's': 1114 if (argv[i][j - 1] != '-' || 1115 argv[i][j + 1] != 0) { 1116 return (0); 1117 } 1118 flag[(int)c] = 1; 1119 if (++i == argc) { 1120 return (0); 1121 } 1122 if (c == 'o') { 1123 if (cmd->outfile) { 1124 return (0); 1125 } 1126 cmd->outfile = argv[i]; 1127 } 1128 goto nextarg; 1129 case 'D': 1130 if (argv[i][j - 1] != '-') { 1131 return (0); 1132 } 1133 (void) addarg(argv[i]); 1134 goto nextarg; 1135 case 'Y': 1136 if (++i == argc) { 1137 return (0); 1138 } 1139 if (strlcpy(pathbuf, argv[i], 1140 sizeof(pathbuf)) >= sizeof(pathbuf) 1141 || strlcat(pathbuf, "/cpp", 1142 sizeof(pathbuf)) >= 1143 sizeof(pathbuf)) { 1144 warnx("argument too long"); 1145 return (0); 1146 } 1147 CPP = pathbuf; 1148 goto nextarg; 1149 1150 1151 1152 default: 1153 return (0); 1154 } 1155 } 1156 nextarg: 1157 ; 1158 } 1159 } 1160 1161 cmd->cflag = flag['c']; 1162 cmd->hflag = flag['h']; 1163 cmd->lflag = flag['l']; 1164 cmd->mflag = flag['m']; 1165 cmd->nflag = flag['n']; 1166 cmd->sflag = flag['s']; 1167 cmd->tflag = flag['t']; 1168 cmd->Ssflag = flag['S']; 1169 cmd->Scflag = flag['C']; 1170 cmd->makefileflag = flag['M']; 1171 1172 if (tirpcflag) { 1173 if (inetdflag) 1174 pmflag = 0; 1175 if ((inetdflag && cmd->nflag)) { 1176 /* netid not allowed with inetdflag */ 1177 warnx("cannot use netid flag with inetd flag"); 1178 return (0); 1179 } 1180 } else { /* 4.1 mode */ 1181 pmflag = 0; /* set pmflag only in tirpcmode */ 1182 if (cmd->nflag) { /* netid needs TIRPC */ 1183 warnx("cannot use netid flag without TIRPC"); 1184 return (0); 1185 } 1186 } 1187 1188 if (newstyle && (tblflag || cmd->tflag)) { 1189 warnx("cannot use table flags with newstyle"); 1190 return (0); 1191 } 1192 1193 /* check no conflicts with file generation flags */ 1194 nflags = cmd->cflag + cmd->hflag + cmd->lflag + cmd->mflag + 1195 cmd->sflag + cmd->nflag + cmd->tflag + cmd->Ssflag + 1196 cmd->Scflag + cmd->makefileflag; 1197 1198 if (nflags == 0) { 1199 if (cmd->outfile != NULL || cmd->infile == NULL) { 1200 return (0); 1201 } 1202 } else if (cmd->infile == NULL && 1203 (cmd->Ssflag || cmd->Scflag || cmd->makefileflag)) { 1204 warnx("\"infile\" is required for template generation flags"); 1205 return (0); 1206 } if (nflags > 1) { 1207 warnx("cannot have more than one file generation flag"); 1208 return (0); 1209 } 1210 return (1); 1211} 1212 1213static void 1214usage(void) 1215{ 1216 f_print(stderr, "%s\n%s\n%s\n%s\n%s\n", 1217 "usage: rpcgen infile", 1218 " rpcgen [-abCLNTM] [-Dname[=value]] [-i size]\ 1219[-I -P [-K seconds]] [-Y path] infile", 1220 " rpcgen [-c | -h | -l | -m | -t | -Sc | -Ss | -Sm]\ 1221[-o outfile] [infile]", 1222 " rpcgen [-s nettype]* [-o outfile] [infile]", 1223 " rpcgen [-n netid]* [-o outfile] [infile]"); 1224 options_usage(); 1225 exit(1); 1226} 1227 1228static void 1229options_usage(void) 1230{ 1231 f_print(stderr, "options:\n"); 1232 f_print(stderr, "-a\t\tgenerate all files, including samples\n"); 1233 f_print(stderr, "-b\t\tbackward compatibility mode (generates code \ 1234for FreeBSD 4.X)\n"); 1235 f_print(stderr, "-c\t\tgenerate XDR routines\n"); 1236 f_print(stderr, "-C\t\tANSI C mode\n"); 1237 f_print(stderr, "-Dname[=value]\tdefine a symbol (same as #define)\n"); 1238 f_print(stderr, "-h\t\tgenerate header file\n"); 1239 f_print(stderr, "-i size\t\tsize at which to start generating\ 1240inline code\n"); 1241 f_print(stderr, "-I\t\tgenerate code for inetd support in server\n"); 1242 f_print(stderr, "-K seconds\tserver exits after K seconds of\ 1243inactivity\n"); 1244 f_print(stderr, "-l\t\tgenerate client side stubs\n"); 1245 f_print(stderr, "-L\t\tserver errors will be printed to syslog\n"); 1246 f_print(stderr, "-m\t\tgenerate server side stubs\n"); 1247 f_print(stderr, "-M\t\tgenerate MT-safe code\n"); 1248 f_print(stderr, "-n netid\tgenerate server code that supports\ 1249named netid\n"); 1250 f_print(stderr, "-N\t\tsupports multiple arguments and\ 1251call-by-value\n"); 1252 f_print(stderr, "-o outfile\tname of the output file\n"); 1253 f_print(stderr, "-P\t\tgenerate code for port monitoring support in server\n"); 1254 f_print(stderr, "-s nettype\tgenerate server code that supports named\ 1255nettype\n"); 1256 f_print(stderr, "-Sc\t\tgenerate sample client code that uses remote\ 1257procedures\n"); 1258 f_print(stderr, "-Ss\t\tgenerate sample server code that defines\ 1259remote procedures\n"); 1260 f_print(stderr, "-Sm \t\tgenerate makefile template \n"); 1261 1262 f_print(stderr, "-t\t\tgenerate RPC dispatch table\n"); 1263 f_print(stderr, "-T\t\tgenerate code to support RPC dispatch tables\n"); 1264 f_print(stderr, "-Y path\t\tpath where cpp is found\n"); 1265 exit(1); 1266} 1267