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