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