Home | History | Annotate | Line # | Download | only in rpcgen
rpc_main.c revision 1.29
      1 /*	$NetBSD: rpc_main.c,v 1.29 2008/01/12 05:49:46 dholland 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.29 2008/01/12 05:49:46 dholland 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, *tmp2;
    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 		*tmp = toupper((unsigned char)*tmp);
    507 		tmp++;
    508 	}
    509 
    510 	tmp2 = extendfile(guard, "_H_RPCGEN");
    511 	free(guard);
    512 	guard = tmp2;
    513 	return (guard);
    514 }
    515 /*
    516  * Compile into an XDR header file
    517  */
    518 
    519 static void
    520 h_output(infile, define, extend, outfile)
    521 	char   *infile;
    522 	char   *define;
    523 	int     extend;
    524 	char   *outfile;
    525 {
    526 	definition *def;
    527 	char   *outfilename;
    528 	long    tell;
    529 	char   *guard;
    530 	list   *l;
    531 
    532 	open_input(infile, define);
    533 	outfilename = extend ? extendfile(infile, outfile) : outfile;
    534 	open_output(infile, outfilename);
    535 	add_warning();
    536 	guard = generate_guard(outfilename ? outfilename : infile);
    537 
    538 	f_print(fout, "#ifndef _%s\n#define _%s\n\n", guard,
    539 	    guard);
    540 
    541 	f_print(fout, "#define RPCGEN_VERSION\t%s\n\n", RPCGEN_VERSION);
    542 	f_print(fout, "#include <rpc/rpc.h>\n\n");
    543 
    544 	tell = ftell(fout);
    545 	/* print data definitions */
    546 	while ((def = get_definition()) != NULL) {
    547 		print_datadef(def);
    548 	}
    549 
    550 	/* print function declarations.  Do this after data definitions
    551 	 * because they might be used as arguments for functions */
    552 	for (l = defined; l != NULL; l = l->next) {
    553 		print_funcdef(l->val);
    554 	}
    555 	if (extend && tell == ftell(fout)) {
    556 		(void) unlink(outfilename);
    557 	} else
    558 		if (tblflag) {
    559 			f_print(fout, rpcgen_table_dcl);
    560 		}
    561 	f_print(fout, "\n#endif /* !_%s */\n", guard);
    562 
    563 	free(guard);
    564 }
    565 /*
    566  * Compile into an RPC service
    567  */
    568 static void
    569 s_output(argc, argv, infile, define, extend, outfile, nomain, netflag)
    570 	int     argc;
    571 	char   *argv[];
    572 	char   *infile;
    573 	char   *define;
    574 	int     extend;
    575 	char   *outfile;
    576 	int     nomain;
    577 	int     netflag;
    578 {
    579 	char   *include;
    580 	definition *def;
    581 	int     foundprogram = 0;
    582 	char   *outfilename;
    583 
    584 	open_input(infile, define);
    585 	outfilename = extend ? extendfile(infile, outfile) : outfile;
    586 	open_output(infile, outfilename);
    587 	add_warning();
    588 	if (infile && (include = extendfile(infile, ".h"))) {
    589 		f_print(fout, "#include \"%s\"\n", include);
    590 		free(include);
    591 	} else
    592 		f_print(fout, "#include <rpc/rpc.h>\n");
    593 
    594 	f_print(fout, "#include <sys/ioctl.h>\n");
    595 	f_print(fout, "#include <fcntl.h>\n");
    596 	f_print(fout, "#include <stdio.h>\n");
    597 	f_print(fout, "#include <stdlib.h>\n");
    598 	if (Cflag) {
    599 		f_print(fout, "#include <unistd.h>\n");
    600 		f_print(fout,
    601 		    "#include <rpc/pmap_clnt.h>\n");
    602 		f_print(fout, "#include <string.h>\n");
    603 	}
    604 	f_print(fout, "#include <netdb.h>\n");
    605 	if (strcmp(svcclosetime, "-1") == 0)
    606 		indefinitewait = 1;
    607 	else
    608 		if (strcmp(svcclosetime, "0") == 0)
    609 			exitnow = 1;
    610 		else
    611 			if (inetdflag || pmflag) {
    612 				f_print(fout, "#include <signal.h>\n");
    613 				timerflag = 1;
    614 			}
    615 	if (!tirpcflag && inetdflag)
    616 		f_print(fout, "#include <sys/ttycom.h>\n");
    617 	if (Cflag && (inetdflag || pmflag)) {
    618 		f_print(fout, "#ifdef __cplusplus\n");
    619 		f_print(fout, "#include <sysent.h>\n");
    620 		f_print(fout, "#endif /* __cplusplus */\n");
    621 	}
    622 	if (tirpcflag)
    623 		f_print(fout, "#include <sys/types.h>\n");
    624 
    625 	f_print(fout, "#include <memory.h>\n");
    626 
    627 	if (inetdflag || !tirpcflag) {
    628 		f_print(fout, "#include <sys/socket.h>\n");
    629 		f_print(fout, "#include <netinet/in.h>\n");
    630 	}
    631 	if ((netflag || pmflag) && tirpcflag) {
    632 		f_print(fout, "#include <netconfig.h>\n");
    633 	}
    634 	if ( /* timerflag && */ tirpcflag)
    635 		f_print(fout, "#include <sys/resource.h>\n");
    636 	if (logflag || inetdflag || pmflag)
    637 		f_print(fout, "#include <syslog.h>\n");
    638 
    639 	/* for ANSI-C */
    640 	f_print(fout, "\n#ifdef __STDC__\n#define SIG_PF void(*)(int)\n#endif\n");
    641 
    642 	f_print(fout, "\n#ifdef DEBUG\n#define RPC_SVC_FG\n#endif\n");
    643 	if (timerflag)
    644 		f_print(fout, "\n#define _RPCSVC_CLOSEDOWN %s\n", svcclosetime);
    645 	while ((def = get_definition()) != NULL) {
    646 		foundprogram |= (def->def_kind == DEF_PROGRAM);
    647 	}
    648 	if (extend && !foundprogram) {
    649 		(void) unlink(outfilename);
    650 		return;
    651 	}
    652 	if (callerflag)		/* EVAS */
    653 		f_print(fout, "\nstatic SVCXPRT *caller;\n");	/* EVAS */
    654 	write_most(infile, netflag, nomain);
    655 	if (!nomain) {
    656 		if (!do_registers(argc, argv)) {
    657 			if (outfilename)
    658 				(void) unlink(outfilename);
    659 			usage();
    660 		}
    661 		write_rest();
    662 	}
    663 }
    664 /*
    665  * generate client side stubs
    666  */
    667 static void
    668 l_output(infile, define, extend, outfile)
    669 	char   *infile;
    670 	char   *define;
    671 	int     extend;
    672 	char   *outfile;
    673 {
    674 	char   *include;
    675 	definition *def;
    676 	int     foundprogram = 0;
    677 	char   *outfilename;
    678 
    679 	open_input(infile, define);
    680 	outfilename = extend ? extendfile(infile, outfile) : outfile;
    681 	open_output(infile, outfilename);
    682 	add_warning();
    683 	if (Cflag)
    684 		f_print(fout, "#include <memory.h>\n");
    685 	if (infile && (include = extendfile(infile, ".h"))) {
    686 		f_print(fout, "#include \"%s\"\n", include);
    687 		free(include);
    688 	} else
    689 		f_print(fout, "#include <rpc/rpc.h>\n");
    690 	while ((def = get_definition()) != NULL) {
    691 		foundprogram |= (def->def_kind == DEF_PROGRAM);
    692 	}
    693 	if (extend && !foundprogram) {
    694 		(void) unlink(outfilename);
    695 		return;
    696 	}
    697 	write_stubs();
    698 }
    699 /*
    700  * generate the dispatch table
    701  */
    702 static void
    703 t_output(infile, define, extend, outfile)
    704 	char   *infile;
    705 	char   *define;
    706 	int     extend;
    707 	char   *outfile;
    708 {
    709 	definition *def;
    710 	int     foundprogram = 0;
    711 	char   *outfilename;
    712 
    713 	open_input(infile, define);
    714 	outfilename = extend ? extendfile(infile, outfile) : outfile;
    715 	open_output(infile, outfilename);
    716 	add_warning();
    717 	while ((def = get_definition()) != NULL) {
    718 		foundprogram |= (def->def_kind == DEF_PROGRAM);
    719 	}
    720 	if (extend && !foundprogram) {
    721 		(void) unlink(outfilename);
    722 		return;
    723 	}
    724 	write_tables();
    725 }
    726 /* sample routine for the server template */
    727 static void
    728 svc_output(infile, define, extend, outfile)
    729 	char   *infile;
    730 	char   *define;
    731 	int     extend;
    732 	char   *outfile;
    733 {
    734 	definition *def;
    735 	char   *include;
    736 	char   *outfilename;
    737 	long    tell;
    738 
    739 	open_input(infile, define);
    740 	outfilename = extend ? extendfile(infile, outfile) : outfile;
    741 	checkfiles(infile, outfilename);	/* check if outfile already
    742 						 * exists. if so, print an
    743 						 * error message and exit */
    744 	open_output(infile, outfilename);
    745 	add_sample_msg();
    746 
    747 	if (infile && (include = extendfile(infile, ".h"))) {
    748 		f_print(fout, "#include \"%s\"\n", include);
    749 		free(include);
    750 	} else
    751 		f_print(fout, "#include <rpc/rpc.h>\n");
    752 
    753 	tell = ftell(fout);
    754 	while ((def = get_definition()) != NULL) {
    755 		write_sample_svc(def);
    756 	}
    757 	if (extend && tell == ftell(fout)) {
    758 		(void) unlink(outfilename);
    759 	}
    760 }
    761 
    762 
    763 /* sample main routine for client */
    764 static void
    765 clnt_output(infile, define, extend, outfile)
    766 	char   *infile;
    767 	char   *define;
    768 	int     extend;
    769 	char   *outfile;
    770 {
    771 	definition *def;
    772 	char   *include;
    773 	char   *outfilename;
    774 	long    tell;
    775 	int     has_program = 0;
    776 
    777 	open_input(infile, define);
    778 	outfilename = extend ? extendfile(infile, outfile) : outfile;
    779 	checkfiles(infile, outfilename);	/* check if outfile already
    780 						 * exists. if so, print an
    781 						 * error message and exit */
    782 
    783 	open_output(infile, outfilename);
    784 	add_sample_msg();
    785 	if (Cflag)
    786 		f_print(fout, "#include <stdio.h>\n");
    787 	if (infile && (include = extendfile(infile, ".h"))) {
    788 		f_print(fout, "#include \"%s\"\n", include);
    789 		free(include);
    790 	} else
    791 		f_print(fout, "#include <rpc/rpc.h>\n");
    792 	tell = ftell(fout);
    793 	while ((def = get_definition()) != NULL) {
    794 		has_program += write_sample_clnt(def);
    795 	}
    796 
    797 	if (has_program)
    798 		write_sample_clnt_main();
    799 
    800 	if (extend && tell == ftell(fout)) {
    801 		(void) unlink(outfilename);
    802 	}
    803 }
    804 /*
    805  * Perform registrations for service output
    806  * Return 0 if failed; 1 otherwise.
    807  */
    808 static int
    809 do_registers(argc, argv)
    810 	int     argc;
    811 	char   *argv[];
    812 {
    813 	int     i;
    814 
    815 	if (inetdflag || !tirpcflag) {
    816 		for (i = 1; i < argc; i++) {
    817 			if (streq(argv[i], "-s")) {
    818 				if (!check_nettype(argv[i + 1], valid_i_nettypes))
    819 					return 0;
    820 				write_inetd_register(argv[i + 1]);
    821 				i++;
    822 			}
    823 		}
    824 	} else {
    825 		for (i = 1; i < argc; i++)
    826 			if (streq(argv[i], "-s")) {
    827 				if (!check_nettype(argv[i + 1], valid_ti_nettypes))
    828 					return 0;
    829 				write_nettype_register(argv[i + 1]);
    830 				i++;
    831 			} else
    832 				if (streq(argv[i], "-n")) {
    833 					write_netid_register(argv[i + 1]);
    834 					i++;
    835 				}
    836 	}
    837 	return 1;
    838 }
    839 /*
    840  * Add another argument to the arg list
    841  */
    842 static void
    843 addarg(cp)
    844 	char   *cp;
    845 {
    846 	if (argcount >= ARGLISTLEN) {
    847 		f_print(stderr, "rpcgen: too many defines\n");
    848 		crash();
    849 		/* NOTREACHED */
    850 	}
    851 	arglist[argcount++] = cp;
    852 
    853 }
    854 
    855 static void
    856 putarg(where, cp)
    857 	char   *cp;
    858 	int     where;
    859 {
    860 	if (where >= ARGLISTLEN) {
    861 		f_print(stderr, "rpcgen: arglist coding error\n");
    862 		crash();
    863 		/* NOTREACHED */
    864 	}
    865 	arglist[where] = cp;
    866 
    867 }
    868 /*
    869  * if input file is stdin and an output file is specified then complain
    870  * if the file already exists. Otherwise the file may get overwritten
    871  * If input file does not exist, exit with an error
    872  */
    873 
    874 static void
    875 checkfiles(infile, outfile)
    876 	char   *infile;
    877 	char   *outfile;
    878 {
    879 
    880 	struct stat buf;
    881 
    882 	if (infile)		/* infile ! = NULL */
    883 		if (stat(infile, &buf) < 0) {
    884 			perror(infile);
    885 			crash();
    886 		};
    887 #if 0
    888 	if (outfile) {
    889 		if (stat(outfile, &buf) < 0)
    890 			return;	/* file does not exist */
    891 		else {
    892 			f_print(stderr,
    893 			    "file '%s' already exists and may be overwritten\n", outfile);
    894 			crash();
    895 		}
    896 	}
    897 #endif
    898 }
    899 /*
    900  * Parse command line arguments
    901  */
    902 static int
    903 parseargs(argc, argv, cmd)
    904 	int     argc;
    905 	char   *argv[];
    906 	struct commandline *cmd;
    907 {
    908 	int     i;
    909 	int     j;
    910 	int     c;
    911 	char    flag[1 << CHAR_BIT];
    912 	int     nflags;
    913 
    914 	cmdname = argv[0];
    915 	cmd->infile = cmd->outfile = NULL;
    916 	if (argc < 2) {
    917 		return (0);
    918 	}
    919 	allfiles = 0;
    920 	flag['c'] = 0;
    921 	flag['h'] = 0;
    922 	flag['l'] = 0;
    923 	flag['m'] = 0;
    924 	flag['o'] = 0;
    925 	flag['s'] = 0;
    926 	flag['n'] = 0;
    927 	flag['t'] = 0;
    928 	flag['S'] = 0;
    929 	flag['C'] = 0;
    930 	for (i = 1; i < argc; i++) {
    931 		if (argv[i][0] != '-') {
    932 			if (cmd->infile) {
    933 				f_print(stderr, "Cannot specify more than one input file!\n");
    934 
    935 				return (0);
    936 			}
    937 			cmd->infile = argv[i];
    938 		} else {
    939 			for (j = 1; argv[i][j] != 0; j++) {
    940 				c = argv[i][j];
    941 				switch (c) {
    942 				case 'A':
    943 					callerflag = 1;
    944 					break;
    945 				case 'a':
    946 					allfiles = 1;
    947 					break;
    948 				case 'c':
    949 				case 'h':
    950 				case 'l':
    951 				case 'm':
    952 				case 't':
    953 					if (flag[c]) {
    954 						return (0);
    955 					}
    956 					flag[c] = 1;
    957 					break;
    958 				case 'S':
    959 					/* sample flag: Ss or Sc. Ss means set
    960 					 * flag['S']; Sc means set flag['C']; */
    961 					c = argv[i][++j];	/* get next char */
    962 					if (c == 's')
    963 						c = 'S';
    964 					else
    965 						if (c == 'c')
    966 							c = 'C';
    967 						else
    968 							return (0);
    969 
    970 					if (flag[c]) {
    971 						return (0);
    972 					}
    973 					flag[c] = 1;
    974 					break;
    975 				case 'C':	/* ANSI C syntax */
    976 					Cflag = 1;
    977 					break;
    978 
    979 				case 'b':	/* turn TIRPC flag off for
    980 						 * generating backward
    981 						 * compatible */
    982 					tirpcflag = 0;
    983 					break;
    984 
    985 				case 'I':
    986 					inetdflag = 1;
    987 					break;
    988 				case 'M':
    989 					Mflag = 1;
    990 					break;
    991 				case 'N':
    992 					newstyle = 1;
    993 					break;
    994 				case 'L':
    995 					logflag = 1;
    996 					break;
    997 				case 'K':
    998 					if (++i == argc) {
    999 						return (0);
   1000 					}
   1001 					svcclosetime = argv[i];
   1002 					goto nextarg;
   1003 				case 'T':
   1004 					tblflag = 1;
   1005 					break;
   1006 				case 'i':
   1007 					if (++i == argc) {
   1008 						return (0);
   1009 					}
   1010 					doinline = atoi(argv[i]);
   1011 					goto nextarg;
   1012 				case 'n':
   1013 				case 'o':
   1014 				case 's':
   1015 					if (argv[i][j - 1] != '-' ||
   1016 					    argv[i][j + 1] != 0) {
   1017 						return (0);
   1018 					}
   1019 					flag[c] = 1;
   1020 					if (++i == argc) {
   1021 						return (0);
   1022 					}
   1023 					if (c == 's') {
   1024 						if (!streq(argv[i], "udp") &&
   1025 						    !streq(argv[i], "tcp")) {
   1026 							return (0);
   1027 						}
   1028 					} else
   1029 						if (c == 'o') {
   1030 							if (cmd->outfile) {
   1031 								return (0);
   1032 							}
   1033 							cmd->outfile = argv[i];
   1034 						}
   1035 					goto nextarg;
   1036 				case 'D':
   1037 					if (argv[i][j - 1] != '-') {
   1038 						return (0);
   1039 					}
   1040 					(void) addarg(argv[i]);
   1041 					goto nextarg;
   1042 				case 'Y':
   1043 					if (++i == argc) {
   1044 						return (0);
   1045 					}
   1046 					(void) strlcpy(pathbuf, argv[i],
   1047 					    sizeof(pathbuf));
   1048 					(void) strlcat(pathbuf, "/cpp",
   1049 					    sizeof(pathbuf));
   1050 					CPP = pathbuf;
   1051 					goto nextarg;
   1052 
   1053 				case 'v':
   1054 					printf("version 1.0\n");
   1055 					exit(0);
   1056 
   1057 				default:
   1058 					return (0);
   1059 				}
   1060 			}
   1061 	nextarg:
   1062 			;
   1063 		}
   1064 	}
   1065 
   1066 	cmd->cflag = flag['c'];
   1067 	cmd->hflag = flag['h'];
   1068 	cmd->lflag = flag['l'];
   1069 	cmd->mflag = flag['m'];
   1070 	cmd->nflag = flag['n'];
   1071 	cmd->sflag = flag['s'];
   1072 	cmd->tflag = flag['t'];
   1073 	cmd->Ssflag = flag['S'];
   1074 	cmd->Scflag = flag['C'];
   1075 
   1076 	if (tirpcflag) {
   1077 		pmflag = inetdflag ? 0 : 1;	/* pmflag or inetdflag is
   1078 						 * always TRUE */
   1079 		if ((inetdflag && cmd->nflag)) {	/* netid not allowed
   1080 							 * with inetdflag */
   1081 			f_print(stderr, "Cannot use netid flag with inetd flag!\n");
   1082 			return (0);
   1083 		}
   1084 	} else {		/* 4.1 mode */
   1085 		pmflag = 0;	/* set pmflag only in tirpcmode */
   1086 		inetdflag = 1;	/* inetdflag is TRUE by default */
   1087 		if (cmd->nflag) {	/* netid needs TIRPC */
   1088 			f_print(stderr, "Cannot use netid flag without TIRPC!\n");
   1089 			return (0);
   1090 		}
   1091 	}
   1092 
   1093 	if (newstyle && (tblflag || cmd->tflag)) {
   1094 		f_print(stderr, "Cannot use table flags with newstyle!\n");
   1095 		return (0);
   1096 	}
   1097 	/* check no conflicts with file generation flags */
   1098 	nflags = cmd->cflag + cmd->hflag + cmd->lflag + cmd->mflag +
   1099 	    cmd->sflag + cmd->nflag + cmd->tflag + cmd->Ssflag + cmd->Scflag;
   1100 
   1101 	if (nflags == 0) {
   1102 		if (cmd->outfile != NULL || cmd->infile == NULL) {
   1103 			return (0);
   1104 		}
   1105 	} else
   1106 		if (nflags > 1) {
   1107 			f_print(stderr, "Cannot have more than one file generation flag!\n");
   1108 			return (0);
   1109 		}
   1110 	return (1);
   1111 }
   1112 
   1113 static void
   1114 usage()
   1115 {
   1116 	f_print(stderr, "usage:  %s infile\n", cmdname);
   1117 	f_print(stderr, "\t%s [-a][-b][-C][-Dname[=value]] -i size [-I [-K seconds]] [-A] [-M] [-N] [-T] infile\n",
   1118 	    cmdname);
   1119 	f_print(stderr, "\t%s [-L] [-M] [-c | -h | -l | -m | -t | -Sc | -Ss] [-o outfile] [infile]\n",
   1120 	    cmdname);
   1121 	f_print(stderr, "\t%s [-s nettype]* [-o outfile] [infile]\n", cmdname);
   1122 	f_print(stderr, "\t%s [-n netid]* [-o outfile] [infile]\n", cmdname);
   1123 	options_usage();
   1124 	exit(1);
   1125 }
   1126 
   1127 static void
   1128 options_usage()
   1129 {
   1130 	f_print(stderr, "options:\n");
   1131 	f_print(stderr, "-A\t\tgenerate svc_caller() function\n");
   1132 	f_print(stderr, "-a\t\tgenerate all files, including samples\n");
   1133 	f_print(stderr, "-b\t\tbackward compatibility mode (generates code for SunOS 4.1)\n");
   1134 	f_print(stderr, "-c\t\tgenerate XDR routines\n");
   1135 	f_print(stderr, "-C\t\tANSI C mode\n");
   1136 	f_print(stderr, "-Dname[=value]\tdefine a symbol (same as #define)\n");
   1137 	f_print(stderr, "-h\t\tgenerate header file\n");
   1138 	f_print(stderr, "-i size\t\tsize at which to start generating inline code\n");
   1139 	f_print(stderr, "-I\t\tgenerate code for inetd support in server (for SunOS 4.1)\n");
   1140 	f_print(stderr, "-K seconds\tserver exits after K seconds of inactivity\n");
   1141 	f_print(stderr, "-l\t\tgenerate client side stubs\n");
   1142 	f_print(stderr, "-L\t\tserver errors will be printed to syslog\n");
   1143 	f_print(stderr, "-m\t\tgenerate server side stubs\n");
   1144 	f_print(stderr, "-M\t\tgenerate thread-safe stubs\n");
   1145 	f_print(stderr, "-n netid\tgenerate server code that supports named netid\n");
   1146 	f_print(stderr, "-N\t\tsupports multiple arguments and call-by-value\n");
   1147 	f_print(stderr, "-o outfile\tname of the output file\n");
   1148 	f_print(stderr, "-s nettype\tgenerate server code that supports named nettype\n");
   1149 	f_print(stderr, "-Sc\t\tgenerate sample client code that uses remote procedures\n");
   1150 	f_print(stderr, "-Ss\t\tgenerate sample server code that defines remote procedures\n");
   1151 	f_print(stderr, "-t\t\tgenerate RPC dispatch table\n");
   1152 	f_print(stderr, "-T\t\tgenerate code to support RPC dispatch tables\n");
   1153 	f_print(stderr, "-Y path\t\tdirectory name to find C preprocessor (cpp)\n");
   1154 
   1155 	exit(1);
   1156 }
   1157