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