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