xref: /freebsd/usr.bin/rpcgen/rpc_main.c (revision 15df5e2d4379bc97dd1fcac1ce18e53d71fdd016)
14e115012SGarrett Wollman /*
24e115012SGarrett Wollman  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
34e115012SGarrett Wollman  * unrestricted use provided that this legend is included on all tape
44e115012SGarrett Wollman  * media and as a part of the software program in whole or part.  Users
54e115012SGarrett Wollman  * may copy or modify Sun RPC without charge, but are not authorized
64e115012SGarrett Wollman  * to license or distribute it to anyone else except as part of a product or
74e115012SGarrett Wollman  * program developed by the user.
84e115012SGarrett Wollman  *
94e115012SGarrett Wollman  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
104e115012SGarrett Wollman  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
114e115012SGarrett Wollman  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
124e115012SGarrett Wollman  *
134e115012SGarrett Wollman  * Sun RPC is provided with no support and without any obligation on the
144e115012SGarrett Wollman  * part of Sun Microsystems, Inc. to assist in its use, correction,
154e115012SGarrett Wollman  * modification or enhancement.
164e115012SGarrett Wollman  *
174e115012SGarrett Wollman  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
184e115012SGarrett Wollman  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
194e115012SGarrett Wollman  * OR ANY PART THEREOF.
204e115012SGarrett Wollman  *
214e115012SGarrett Wollman  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
224e115012SGarrett Wollman  * or profits or other special, indirect and consequential damages, even if
234e115012SGarrett Wollman  * Sun has been advised of the possibility of such damages.
244e115012SGarrett Wollman  *
254e115012SGarrett Wollman  * Sun Microsystems, Inc.
264e115012SGarrett Wollman  * 2550 Garcia Avenue
274e115012SGarrett Wollman  * Mountain View, California  94043
284e115012SGarrett Wollman  */
29ff49530fSBill Paul 
30ff49530fSBill Paul 
310e76f40dSPhilippe Charnier #if 0
3275863a6dSPhilippe Charnier #ifndef lint
3363f17371SStefan Farfeleder #ident	"@(#)rpc_main.c	1.21	94/04/25 SMI"
34ff49530fSBill Paul static char sccsid[] = "@(#)rpc_main.c 1.30 89/03/30 (C) 1987 SMI";
354e115012SGarrett Wollman #endif
360e76f40dSPhilippe Charnier #endif
374e115012SGarrett Wollman 
3875863a6dSPhilippe Charnier #include <sys/cdefs.h>
3975863a6dSPhilippe Charnier __FBSDID("$FreeBSD$");
4075863a6dSPhilippe Charnier 
414e115012SGarrett Wollman /*
424e115012SGarrett Wollman  * rpc_main.c, Top level of the RPC protocol compiler.
434e115012SGarrett Wollman  * Copyright (C) 1987, Sun Microsystems, Inc.
444e115012SGarrett Wollman  */
454e115012SGarrett Wollman 
460e76f40dSPhilippe Charnier #include <err.h>
470e76f40dSPhilippe Charnier #include <ctype.h>
484e115012SGarrett Wollman #include <stdio.h>
49ff49530fSBill Paul #include <string.h>
50ff49530fSBill Paul #include <unistd.h>
51ff49530fSBill Paul #include <sys/types.h>
52ff49530fSBill Paul #include <sys/param.h>
534e115012SGarrett Wollman #include <sys/file.h>
54ff49530fSBill Paul #include <sys/stat.h>
554e115012SGarrett Wollman #include "rpc_parse.h"
564e115012SGarrett Wollman #include "rpc_scan.h"
57d0cc804bSStefan Farfeleder #include "rpc_util.h"
584e115012SGarrett Wollman 
59d3cb5dedSWarner Losh extern void write_sample_svc( definition * );
60d3cb5dedSWarner Losh extern int write_sample_clnt( definition * );
61d3cb5dedSWarner Losh extern void write_sample_clnt_main( void );
62d3cb5dedSWarner Losh extern void add_sample_msg( void );
63d3cb5dedSWarner Losh static void c_output( char *, char *, int, char * );
64ec06b5e8SStefan Farfeleder static void h_output( char *, char *, int, char *, int );
65d3cb5dedSWarner Losh static void l_output( char *, char *, int, char * );
66d3cb5dedSWarner Losh static void t_output( char *, char *, int, char * );
67d3cb5dedSWarner Losh static void clnt_output( char *, char *, int, char * );
68d3cb5dedSWarner Losh void c_initialize( void );
694e115012SGarrett Wollman 
70d7c089b7SJohn Birrell #if !defined(__FreeBSD__) && !defined(__NetBSD__)
71ff49530fSBill Paul char * rindex();
72ff49530fSBill Paul #endif
73ff49530fSBill Paul 
74d3cb5dedSWarner Losh static void usage(void);
75d3cb5dedSWarner Losh static void options_usage(void);
76d3cb5dedSWarner Losh static int do_registers(int, char **);
77d3cb5dedSWarner Losh static int parseargs(int, char **, struct commandline *);
78d3cb5dedSWarner Losh static void svc_output(char *, char *, int, char *);
79d3cb5dedSWarner Losh static void mkfile_output(struct commandline *);
80d3cb5dedSWarner Losh static void s_output(int, char **, char *, char *, int, char *, int, int);
81ff49530fSBill Paul 
82ff49530fSBill Paul #define	EXTEND	1		/* alias for TRUE */
83ff49530fSBill Paul #define	DONT_EXTEND	0		/* alias for FALSE */
84ff49530fSBill Paul 
85ff49530fSBill Paul #define	SVR4_CPP "/usr/ccs/lib/cpp"
86d7c089b7SJohn Birrell #define SUNOS_CPP "/usr/bin/cpp"
87ff49530fSBill Paul 
88ff49530fSBill Paul static int cppDefined = 0;	/* explicit path for C preprocessor */
89ff49530fSBill Paul 
90ff49530fSBill Paul static char *svcclosetime = "120";
91ff49530fSBill Paul static char *CPP = SVR4_CPP;
924e115012SGarrett Wollman static char CPPFLAGS[] = "-C";
93ff49530fSBill Paul static char pathbuf[MAXPATHLEN + 1];
944e115012SGarrett Wollman static char *allv[] = {
954e115012SGarrett Wollman 	"rpcgen", "-s", "udp", "-s", "tcp",
964e115012SGarrett Wollman };
974e115012SGarrett Wollman static int allc = sizeof (allv)/sizeof (allv[0]);
98ff49530fSBill Paul static char *allnv[] = {
99ff49530fSBill Paul 	"rpcgen", "-s", "netpath",
100ff49530fSBill Paul };
101ff49530fSBill Paul static int allnc = sizeof (allnv)/sizeof (allnv[0]);
102ff49530fSBill Paul 
103ff49530fSBill Paul /*
104ff49530fSBill Paul  * machinations for handling expanding argument list
105ff49530fSBill Paul  */
106ff49530fSBill Paul static void addarg();		/* add another argument to the list */
107ff49530fSBill Paul static void putarg();		/* put argument at specified location  */
108ff49530fSBill Paul static void clear_args();	/* clear argument list */
109ff49530fSBill Paul static void checkfiles();	/* check if out file already exists */
1104e115012SGarrett Wollman 
1114e115012SGarrett Wollman 
112ff49530fSBill Paul 
113ff49530fSBill Paul #define	ARGLISTLEN	20
114ff49530fSBill Paul #define	FIXEDARGS	2
115ff49530fSBill Paul 
116ff49530fSBill Paul static char *arglist[ARGLISTLEN];
117ff49530fSBill Paul static int argcount = FIXEDARGS;
118ff49530fSBill Paul 
119ff49530fSBill Paul 
120ff49530fSBill Paul int nonfatalerrors;	/* errors */
12140ad8885SAlfred Perlstein int inetdflag = 0;	/* Support for inetd is disabled by default, use -I */
12240ad8885SAlfred Perlstein int pmflag = 0;		/* Support for port monitors is disabled by default */
12340ad8885SAlfred Perlstein int tirpc_socket = 1;	/* TI-RPC on socket, no TLI library */
124ff49530fSBill Paul int logflag;		/* Use syslog instead of fprintf for errors */
125ff49530fSBill Paul int tblflag;		/* Support for dispatch table file */
126ff49530fSBill Paul int mtflag = 0;		/* Support for MT */
12740ad8885SAlfred Perlstein 
128ff49530fSBill Paul #define INLINE 0
129ff49530fSBill Paul /* length at which to start doing an inline */
130ff49530fSBill Paul 
131122562cdSStefan Farfeleder int inline_size = INLINE;
132ff49530fSBill Paul /*
133ff49530fSBill Paul  * Length at which to start doing an inline. INLINE = default
134ff49530fSBill Paul  * if 0, no xdr_inline code
135ff49530fSBill Paul  */
136ff49530fSBill Paul 
137ff49530fSBill Paul int indefinitewait;	/* If started by port monitors, hang till it wants */
138ff49530fSBill Paul int exitnow;		/* If started by port monitors, exit after the call */
139ff49530fSBill Paul int timerflag;		/* TRUE if !indefinite && !exitnow */
140ff49530fSBill Paul int newstyle;		/* newstyle of passing arguments (by value) */
141ff49530fSBill Paul int CCflag = 0;		/* C++ files */
142ff49530fSBill Paul static int allfiles;   /* generate all files */
143ff49530fSBill Paul int tirpcflag = 1;    /* generating code for tirpc, by default */
144ff49530fSBill Paul xdrfunc *xdrfunc_head = NULL; /* xdr function list */
145ff49530fSBill Paul xdrfunc *xdrfunc_tail = NULL; /* xdr function list */
146ff49530fSBill Paul pid_t childpid;
147ff49530fSBill Paul 
1484e115012SGarrett Wollman 
149526195adSJordan K. Hubbard int
1504e115012SGarrett Wollman main(argc, argv)
1514e115012SGarrett Wollman 	int argc;
1524e115012SGarrett Wollman 	char *argv[];
1534e115012SGarrett Wollman {
1544e115012SGarrett Wollman 	struct commandline cmd;
1554e115012SGarrett Wollman 
156ff49530fSBill Paul 	(void) memset((char *)&cmd, 0, sizeof (struct commandline));
157ff49530fSBill Paul 	clear_args();
158ff49530fSBill Paul 	if (!parseargs(argc, argv, &cmd))
159ff49530fSBill Paul 		usage();
160ff49530fSBill Paul 	/*
161ff49530fSBill Paul 	 * Only the client and server side stubs are likely to be customized,
162ff49530fSBill Paul 	 *  so in that case only, check if the outfile exists, and if so,
163ff49530fSBill Paul 	 *  print an error message and exit.
164ff49530fSBill Paul 	 */
165ff49530fSBill Paul 	if (cmd.Ssflag || cmd.Scflag || cmd.makefileflag) {
166ff49530fSBill Paul 		checkfiles(cmd.infile, cmd.outfile);
1674e115012SGarrett Wollman 	}
168ff49530fSBill Paul 	else
169ff49530fSBill Paul 		checkfiles(cmd.infile, NULL);
170ff49530fSBill Paul 
1714e115012SGarrett Wollman 	if (cmd.cflag) {
172ff49530fSBill Paul 		c_output(cmd.infile, "-DRPC_XDR", DONT_EXTEND, cmd.outfile);
1734e115012SGarrett Wollman 	} else if (cmd.hflag) {
174ec06b5e8SStefan Farfeleder 		h_output(cmd.infile, "-DRPC_HDR", DONT_EXTEND, cmd.outfile,
175ec06b5e8SStefan Farfeleder 		    cmd.hflag);
1764e115012SGarrett Wollman 	} else if (cmd.lflag) {
177ff49530fSBill Paul 		l_output(cmd.infile, "-DRPC_CLNT", DONT_EXTEND, cmd.outfile);
178ff49530fSBill Paul 	} else if (cmd.sflag || cmd.mflag || (cmd.nflag)) {
179ff49530fSBill Paul 		s_output(argc, argv, cmd.infile, "-DRPC_SVC", DONT_EXTEND,
180ff49530fSBill Paul 			cmd.outfile, cmd.mflag, cmd.nflag);
181ff49530fSBill Paul 	} else if (cmd.tflag) {
182ff49530fSBill Paul 		t_output(cmd.infile, "-DRPC_TBL", DONT_EXTEND, cmd.outfile);
183ff49530fSBill Paul 	} else if  (cmd.Ssflag) {
184ff49530fSBill Paul 		svc_output(cmd.infile, "-DRPC_SERVER", DONT_EXTEND,
185ff49530fSBill Paul 			cmd.outfile);
186ff49530fSBill Paul 	} else if (cmd.Scflag) {
187ff49530fSBill Paul 		clnt_output(cmd.infile, "-DRPC_CLIENT", DONT_EXTEND,
188ff49530fSBill Paul 			    cmd.outfile);
189ff49530fSBill Paul 	} else if (cmd.makefileflag) {
190ff49530fSBill Paul 		mkfile_output(&cmd);
1914e115012SGarrett Wollman 	} else {
192ff49530fSBill Paul 		/* the rescans are required, since cpp may effect input */
1934e115012SGarrett Wollman 		c_output(cmd.infile, "-DRPC_XDR", EXTEND, "_xdr.c");
1944e115012SGarrett Wollman 		reinitialize();
195ec06b5e8SStefan Farfeleder 		h_output(cmd.infile, "-DRPC_HDR", EXTEND, ".h", cmd.hflag);
1964e115012SGarrett Wollman 		reinitialize();
1974e115012SGarrett Wollman 		l_output(cmd.infile, "-DRPC_CLNT", EXTEND, "_clnt.c");
1984e115012SGarrett Wollman 		reinitialize();
199ff49530fSBill Paul 		if (inetdflag || !tirpcflag)
2004e115012SGarrett Wollman 			s_output(allc, allv, cmd.infile, "-DRPC_SVC", EXTEND,
201ff49530fSBill Paul 			"_svc.c", cmd.mflag, cmd.nflag);
202ff49530fSBill Paul 		else
203ff49530fSBill Paul 			s_output(allnc, allnv, cmd.infile, "-DRPC_SVC",
204ff49530fSBill Paul 				EXTEND, "_svc.c", cmd.mflag, cmd.nflag);
205ff49530fSBill Paul 		if (tblflag) {
206ff49530fSBill Paul 			reinitialize();
207ff49530fSBill Paul 			t_output(cmd.infile, "-DRPC_TBL", EXTEND, "_tbl.i");
2084e115012SGarrett Wollman 		}
2094e115012SGarrett Wollman 
210ff49530fSBill Paul 		if (allfiles) {
211ff49530fSBill Paul 			reinitialize();
212ff49530fSBill Paul 			svc_output(cmd.infile, "-DRPC_SERVER", EXTEND,
213ff49530fSBill Paul 				"_server.c");
214ff49530fSBill Paul 			reinitialize();
215ff49530fSBill Paul 			clnt_output(cmd.infile, "-DRPC_CLIENT", EXTEND,
216ff49530fSBill Paul 				"_client.c");
217ff49530fSBill Paul 
218ff49530fSBill Paul 		}
219ff49530fSBill Paul 		if (allfiles || (cmd.makefileflag == 1)){
220ff49530fSBill Paul 			reinitialize();
221ff49530fSBill Paul 			mkfile_output(&cmd);
222ff49530fSBill Paul 		}
223ff49530fSBill Paul 
224ff49530fSBill Paul 	}
225ff49530fSBill Paul 	exit(nonfatalerrors);
226ff49530fSBill Paul 	/* NOTREACHED */
227ff49530fSBill Paul }
228ff49530fSBill Paul 
229ff49530fSBill Paul 
2304e115012SGarrett Wollman /*
231ff49530fSBill Paul  * add extension to filename
2324e115012SGarrett Wollman  */
2334e115012SGarrett Wollman static char *
2345ec07232SNate Williams extendfile(path, ext)
2355ec07232SNate Williams 	char *path;
2364e115012SGarrett Wollman 	char *ext;
2374e115012SGarrett Wollman {
2384e115012SGarrett Wollman 	char *res;
2394e115012SGarrett Wollman 	char *p;
240ff49530fSBill Paul 	char *file;
2414e115012SGarrett Wollman 
2425ec07232SNate Williams 	if ((file = rindex(path, '/')) == NULL)
2435ec07232SNate Williams 		file = path;
2445ec07232SNate Williams 	else
2455ec07232SNate Williams 		file++;
24675863a6dSPhilippe Charnier 	res = xmalloc(strlen(file) + strlen(ext) + 1);
247ff49530fSBill Paul 	p = strrchr(file, '.');
2484e115012SGarrett Wollman 	if (p == NULL) {
2494e115012SGarrett Wollman 		p = file + strlen(file);
2504e115012SGarrett Wollman 	}
2514e115012SGarrett Wollman 	(void) strcpy(res, file);
2524e115012SGarrett Wollman 	(void) strcpy(res + (p - file), ext);
2534e115012SGarrett Wollman 	return (res);
2544e115012SGarrett Wollman }
2554e115012SGarrett Wollman 
2564e115012SGarrett Wollman /*
2574e115012SGarrett Wollman  * Open output file with given extension
2584e115012SGarrett Wollman  */
259526195adSJordan K. Hubbard static void
2604e115012SGarrett Wollman open_output(infile, outfile)
2614e115012SGarrett Wollman 	char *infile;
2624e115012SGarrett Wollman 	char *outfile;
2634e115012SGarrett Wollman {
264ff49530fSBill Paul 
2654e115012SGarrett Wollman 	if (outfile == NULL) {
2664e115012SGarrett Wollman 		fout = stdout;
2674e115012SGarrett Wollman 		return;
2684e115012SGarrett Wollman 	}
269ff49530fSBill Paul 
2704e115012SGarrett Wollman 	if (infile != NULL && streq(outfile, infile)) {
2710e76f40dSPhilippe Charnier 		warnx("%s already exists. No output generated", infile);
2724e115012SGarrett Wollman 		crash();
2734e115012SGarrett Wollman 	}
2744e115012SGarrett Wollman 	fout = fopen(outfile, "w");
2754e115012SGarrett Wollman 	if (fout == NULL) {
2760e76f40dSPhilippe Charnier 		warn("unable to open %s", outfile);
2774e115012SGarrett Wollman 		crash();
2784e115012SGarrett Wollman 	}
2794e115012SGarrett Wollman 	record_open(outfile);
280ff49530fSBill Paul 
281526195adSJordan K. Hubbard 	return;
282ff49530fSBill Paul }
283ff49530fSBill Paul 
284526195adSJordan K. Hubbard static void
285ff49530fSBill Paul add_warning()
286ff49530fSBill Paul {
287ff49530fSBill Paul 	f_print(fout, "/*\n");
288ff49530fSBill Paul 	f_print(fout, " * Please do not edit this file.\n");
289ff49530fSBill Paul 	f_print(fout, " * It was generated using rpcgen.\n");
290ff49530fSBill Paul 	f_print(fout, " */\n\n");
291ff49530fSBill Paul }
292ff49530fSBill Paul 
293ff49530fSBill Paul /* clear list of arguments */
294ff49530fSBill Paul static void clear_args()
295ff49530fSBill Paul {
296ff49530fSBill Paul 	int i;
297ff49530fSBill Paul 	for (i = FIXEDARGS; i < ARGLISTLEN; i++)
298ff49530fSBill Paul 		arglist[i] = NULL;
299ff49530fSBill Paul 	argcount = FIXEDARGS;
300ff49530fSBill Paul }
301ff49530fSBill Paul 
302ff49530fSBill Paul /* make sure that a CPP exists */
303ff49530fSBill Paul static void find_cpp()
304ff49530fSBill Paul {
305ff49530fSBill Paul 	struct stat buf;
306ff49530fSBill Paul 
307ff49530fSBill Paul 	if (stat(CPP, &buf) < 0)  { /* SVR4 or explicit cpp does not exist */
308ff49530fSBill Paul 		if (cppDefined) {
3090e76f40dSPhilippe Charnier 			warnx("cannot find C preprocessor: %s", CPP);
310ff49530fSBill Paul 			crash();
311ff49530fSBill Paul 		} else {	/* try the other one */
312ff49530fSBill Paul 			CPP = SUNOS_CPP;
313ff49530fSBill Paul 			if (stat(CPP, &buf) < 0) { /* can't find any cpp */
314f2912673SDavid E. O'Brien 				warnx("cannot find C preprocessor: %s", CPP);
315ff49530fSBill Paul 				crash();
316ff49530fSBill Paul 			}
317ff49530fSBill Paul 		}
318ff49530fSBill Paul 	}
3194e115012SGarrett Wollman }
3204e115012SGarrett Wollman 
3214e115012SGarrett Wollman /*
3224e115012SGarrett Wollman  * Open input file with given define for C-preprocessor
3234e115012SGarrett Wollman  */
324526195adSJordan K. Hubbard static void
3254e115012SGarrett Wollman open_input(infile, define)
3264e115012SGarrett Wollman 	char *infile;
3274e115012SGarrett Wollman 	char *define;
3284e115012SGarrett Wollman {
3294e115012SGarrett Wollman 	int pd[2];
3304e115012SGarrett Wollman 
3314e115012SGarrett Wollman 	infilename = (infile == NULL) ? "<stdin>" : infile;
3324e115012SGarrett Wollman 	(void) pipe(pd);
333ff49530fSBill Paul 	switch (childpid = fork()) {
3344e115012SGarrett Wollman 	case 0:
335ff49530fSBill Paul 		find_cpp();
336ff49530fSBill Paul 		putarg(0, CPP);
337ff49530fSBill Paul 		putarg(1, CPPFLAGS);
338ff49530fSBill Paul 		addarg(define);
339ff49530fSBill Paul 		if (infile)
340ff49530fSBill Paul 			addarg(infile);
341ff49530fSBill Paul 		addarg((char *)NULL);
3424e115012SGarrett Wollman 		(void) close(1);
3434e115012SGarrett Wollman 		(void) dup2(pd[1], 1);
3444e115012SGarrett Wollman 		(void) close(pd[0]);
345ff49530fSBill Paul 		execv(arglist[0], arglist);
34675863a6dSPhilippe Charnier 		err(1, "execv");
3474e115012SGarrett Wollman 	case -1:
34875863a6dSPhilippe Charnier 		err(1, "fork");
3494e115012SGarrett Wollman 	}
3504e115012SGarrett Wollman 	(void) close(pd[1]);
3514e115012SGarrett Wollman 	fin = fdopen(pd[0], "r");
3524e115012SGarrett Wollman 	if (fin == NULL) {
3530e76f40dSPhilippe Charnier 		warn("%s", infilename);
3544e115012SGarrett Wollman 		crash();
3554e115012SGarrett Wollman 	}
3564e115012SGarrett Wollman }
3574e115012SGarrett Wollman 
358ff49530fSBill Paul /* valid tirpc nettypes */
359ff49530fSBill Paul static char* valid_ti_nettypes[] =
360ff49530fSBill Paul {
361ff49530fSBill Paul 	"netpath",
362ff49530fSBill Paul 	"visible",
363ff49530fSBill Paul 	"circuit_v",
364ff49530fSBill Paul 	"datagram_v",
365ff49530fSBill Paul 	"circuit_n",
366ff49530fSBill Paul 	"datagram_n",
367ff49530fSBill Paul 	"udp",
368ff49530fSBill Paul 	"tcp",
369ff49530fSBill Paul 	"raw",
370ff49530fSBill Paul 	NULL
371ff49530fSBill Paul 	};
372ff49530fSBill Paul 
373ff49530fSBill Paul /* valid inetd nettypes */
374ff49530fSBill Paul static char* valid_i_nettypes[] =
375ff49530fSBill Paul {
376ff49530fSBill Paul 	"udp",
377ff49530fSBill Paul 	"tcp",
378ff49530fSBill Paul 	NULL
379ff49530fSBill Paul 	};
380ff49530fSBill Paul 
381ff49530fSBill Paul static int check_nettype(name, list_to_check)
382ff49530fSBill Paul char* name;
383ff49530fSBill Paul char* list_to_check[];
384ff49530fSBill Paul {
385ff49530fSBill Paul 	int i;
386ff49530fSBill Paul 	for (i = 0; list_to_check[i] != NULL; i++) {
387ff49530fSBill Paul 		if (strcmp(name, list_to_check[i]) == 0) {
388ff49530fSBill Paul 			return (1);
389ff49530fSBill Paul 		}
390ff49530fSBill Paul 	}
3910e76f40dSPhilippe Charnier 	warnx("illegal nettype :\'%s\'", name);
392ff49530fSBill Paul 	return (0);
393ff49530fSBill Paul }
394ff49530fSBill Paul 
395ff49530fSBill Paul static char *
396ff49530fSBill Paul file_name(file, ext)
397ff49530fSBill Paul char *file;
398ff49530fSBill Paul char *ext;
399ff49530fSBill Paul {
400ff49530fSBill Paul 	char *temp;
401ff49530fSBill Paul 	temp = extendfile(file, ext);
402ff49530fSBill Paul 
403ff49530fSBill Paul 	if (access(temp, F_OK) != -1)
404ff49530fSBill Paul 		return (temp);
405ff49530fSBill Paul 	else
406ff49530fSBill Paul 		return ((char *)" ");
407ff49530fSBill Paul 
408ff49530fSBill Paul }
409ff49530fSBill Paul 
410ff49530fSBill Paul 
411526195adSJordan K. Hubbard static void
4124e115012SGarrett Wollman c_output(infile, define, extend, outfile)
4134e115012SGarrett Wollman 	char *infile;
4144e115012SGarrett Wollman 	char *define;
4154e115012SGarrett Wollman 	int extend;
4164e115012SGarrett Wollman 	char *outfile;
4174e115012SGarrett Wollman {
4184e115012SGarrett Wollman 	definition *def;
4194e115012SGarrett Wollman 	char *include;
4204e115012SGarrett Wollman 	char *outfilename;
4214e115012SGarrett Wollman 	long tell;
4224e115012SGarrett Wollman 
423ff49530fSBill Paul 	c_initialize();
4244e115012SGarrett Wollman 	open_input(infile, define);
4254e115012SGarrett Wollman 	outfilename = extend ? extendfile(infile, outfile) : outfile;
4264e115012SGarrett Wollman 	open_output(infile, outfilename);
427ff49530fSBill Paul 	add_warning();
4284e115012SGarrett Wollman 	if (infile && (include = extendfile(infile, ".h"))) {
4294e115012SGarrett Wollman 		f_print(fout, "#include \"%s\"\n", include);
4304e115012SGarrett Wollman 		free(include);
431ff49530fSBill Paul 		/* .h file already contains rpc/rpc.h */
432ff49530fSBill Paul 	} else
433ff49530fSBill Paul 		f_print(fout, "#include <rpc/rpc.h>\n");
4344e115012SGarrett Wollman 	tell = ftell(fout);
435526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
4364e115012SGarrett Wollman 		emit(def);
4374e115012SGarrett Wollman 	}
4384e115012SGarrett Wollman 	if (extend && tell == ftell(fout)) {
4394e115012SGarrett Wollman 		(void) unlink(outfilename);
4404e115012SGarrett Wollman 	}
4414e115012SGarrett Wollman }
4424e115012SGarrett Wollman 
443ff49530fSBill Paul 
444526195adSJordan K. Hubbard void
445ff49530fSBill Paul c_initialize()
446ff49530fSBill Paul {
447ff49530fSBill Paul 
448ff49530fSBill Paul 	/* add all the starting basic types */
449ff49530fSBill Paul 	add_type(1, "int");
450ff49530fSBill Paul 	add_type(1, "long");
451ff49530fSBill Paul 	add_type(1, "short");
452ff49530fSBill Paul 	add_type(1, "bool");
453ff49530fSBill Paul 	add_type(1, "u_int");
454ff49530fSBill Paul 	add_type(1, "u_long");
455ff49530fSBill Paul 	add_type(1, "u_short");
456ff49530fSBill Paul 
457ff49530fSBill Paul }
458ff49530fSBill Paul 
459ff49530fSBill Paul char rpcgen_table_dcl[] = "struct rpcgen_table {\n\
460ff49530fSBill Paul 	char	*(*proc)(); \n\
461ff49530fSBill Paul 	xdrproc_t	xdr_arg; \n\
462ff49530fSBill Paul 	unsigned	len_arg; \n\
463ff49530fSBill Paul 	xdrproc_t	xdr_res; \n\
464ff49530fSBill Paul 	unsigned	len_res; \n\
465ff49530fSBill Paul }; \n";
466ff49530fSBill Paul 
467ff49530fSBill Paul 
468ff49530fSBill Paul char *generate_guard(pathname)
469ff49530fSBill Paul 	char* pathname;
470ff49530fSBill Paul {
471080f4020SSean Kelly 	char* filename, *guard, *tmp, *stopat;
472ff49530fSBill Paul 
473ff49530fSBill Paul 	filename = strrchr(pathname, '/');  /* find last component */
474ff49530fSBill Paul 	filename = ((filename == 0) ? pathname : filename+1);
47575863a6dSPhilippe Charnier 	guard = xstrdup(filename);
476080f4020SSean Kelly 	stopat = strrchr(guard, '.');
477080f4020SSean Kelly 
478080f4020SSean Kelly 	/*
479080f4020SSean Kelly 	 * Convert to a valid C macro name and make it upper case.
480080f4020SSean Kelly 	 * Map macro unfriendly characterss to '_'.
48110c546c4SSean Kelly 	 */
482080f4020SSean Kelly 	for (tmp = guard; *tmp != '\000'; ++tmp) {
483ff49530fSBill Paul 		if (islower(*tmp))
484ff49530fSBill Paul 			*tmp = toupper(*tmp);
485080f4020SSean Kelly 		else if (isupper(*tmp) || *tmp == '_')
48610c546c4SSean Kelly 			/* OK for C */;
48710c546c4SSean Kelly 		else if (tmp == guard)
48810c546c4SSean Kelly 			*tmp = '_';
48910c546c4SSean Kelly 		else if (isdigit(*tmp))
49010c546c4SSean Kelly 			/* OK for all but first character */;
491080f4020SSean Kelly 		else if (tmp == stopat) {
492080f4020SSean Kelly 			*tmp = '\0';
49310c546c4SSean Kelly 			break;
49410c546c4SSean Kelly 		} else
49510c546c4SSean Kelly 			*tmp = '_';
49610c546c4SSean Kelly 	}
497080f4020SSean Kelly 	/*
498080f4020SSean Kelly 	 * Can't have a '_' in front, because it'll end up being "__".
499080f4020SSean Kelly 	 * "__" macros shoudln't be used. So, remove all of the
500080f4020SSean Kelly 	 * '_' characters from the front.
50110c546c4SSean Kelly 	 */
502080f4020SSean Kelly 	if (*guard == '_') {
503080f4020SSean Kelly 		for (tmp = guard; *tmp == '_'; ++tmp)
504080f4020SSean Kelly 			;
505080f4020SSean Kelly 		strcpy(guard, tmp);
506ff49530fSBill Paul 	}
507ff49530fSBill Paul 	guard = extendfile(guard, "_H_RPCGEN");
508ff49530fSBill Paul 	return (guard);
509ff49530fSBill Paul }
510ff49530fSBill Paul 
5114e115012SGarrett Wollman /*
5124e115012SGarrett Wollman  * Compile into an XDR header file
5134e115012SGarrett Wollman  */
514ff49530fSBill Paul 
515ff49530fSBill Paul 
516526195adSJordan K. Hubbard static void
517ec06b5e8SStefan Farfeleder h_output(char *infile, char *define, int extend, char *outfile, int headeronly)
5184e115012SGarrett Wollman {
5194e115012SGarrett Wollman 	definition *def;
5204e115012SGarrett Wollman 	char *outfilename;
5214e115012SGarrett Wollman 	long tell;
522ff49530fSBill Paul 	char *guard;
523ff49530fSBill Paul 	list *l;
524ff49530fSBill Paul 	xdrfunc *xdrfuncp;
5254e115012SGarrett Wollman 
5264e115012SGarrett Wollman 	open_input(infile, define);
5274e115012SGarrett Wollman 	outfilename =  extend ? extendfile(infile, outfile) : outfile;
5284e115012SGarrett Wollman 	open_output(infile, outfilename);
529ff49530fSBill Paul 	add_warning();
530ff49530fSBill Paul 	if (outfilename || infile){
531ff49530fSBill Paul 		guard = generate_guard(outfilename ? outfilename: infile);
532ff49530fSBill Paul 	} else
533ff49530fSBill Paul 		guard = "STDIN_";
534ff49530fSBill Paul 
535ff49530fSBill Paul 	f_print(fout, "#ifndef _%s\n#define	_%s\n\n", guard,
536ff49530fSBill Paul 		guard);
537ff49530fSBill Paul 
538ff49530fSBill Paul 	f_print(fout, "#include <rpc/rpc.h>\n");
539ff49530fSBill Paul 
54040ad8885SAlfred Perlstein 	if (mtflag)
54135ab9586SDavid E. O'Brien 		f_print(fout, "#include <pthread.h>\n");
542ff49530fSBill Paul 
543ff49530fSBill Paul 	/* put the C++ support */
54415df5e2dSStefan Farfeleder 	if (!CCflag) {
545ff49530fSBill Paul 		f_print(fout, "\n#ifdef __cplusplus\n");
546ff49530fSBill Paul 		f_print(fout, "extern \"C\" {\n");
547ff49530fSBill Paul 		f_print(fout, "#endif\n\n");
548ff49530fSBill Paul 	}
549ff49530fSBill Paul 
550ff49530fSBill Paul 	/* put in a typedef for quadprecision. Only with Cflag */
551ff49530fSBill Paul 
5524e115012SGarrett Wollman 	tell = ftell(fout);
553ff49530fSBill Paul 
554ff49530fSBill Paul 	/* print data definitions */
555526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
556ec06b5e8SStefan Farfeleder 		print_datadef(def, headeronly);
5574e115012SGarrett Wollman 	}
558ff49530fSBill Paul 
559ff49530fSBill Paul 	/*
560ff49530fSBill Paul 	 * print function declarations.
561ff49530fSBill Paul 	 *  Do this after data definitions because they might be used as
562ff49530fSBill Paul 	 *  arguments for functions
563ff49530fSBill Paul 	 */
564ff49530fSBill Paul 	for (l = defined; l != NULL; l = l->next) {
565ec06b5e8SStefan Farfeleder 		print_funcdef(l->val, headeronly);
566ff49530fSBill Paul 	}
567ff49530fSBill Paul 	/* Now  print all xdr func declarations */
568ff49530fSBill Paul 	if (xdrfunc_head != NULL){
569ff49530fSBill Paul 
570ff49530fSBill Paul 		f_print(fout,
571ff49530fSBill Paul 			"\n/* the xdr functions */\n");
572ff49530fSBill Paul 
573ff49530fSBill Paul 		if (CCflag){
574ff49530fSBill Paul 			f_print(fout, "\n#ifdef __cplusplus\n");
575ff49530fSBill Paul 			f_print(fout, "extern \"C\" {\n");
576ff49530fSBill Paul 			f_print(fout, "#endif\n");
577ff49530fSBill Paul 		}
578ff49530fSBill Paul 
579ff49530fSBill Paul 		xdrfuncp = xdrfunc_head;
580ff49530fSBill Paul 		while (xdrfuncp != NULL){
58115df5e2dSStefan Farfeleder 			print_xdr_func_def(xdrfuncp->name, xdrfuncp->pointerp);
582ff49530fSBill Paul 			xdrfuncp = xdrfuncp->next;
583ff49530fSBill Paul 		}
584ff49530fSBill Paul 	}
585ff49530fSBill Paul 
5864e115012SGarrett Wollman 	if (extend && tell == ftell(fout)) {
5874e115012SGarrett Wollman 		(void) unlink(outfilename);
588ff49530fSBill Paul 	} else if (tblflag) {
589ff49530fSBill Paul 		f_print(fout, rpcgen_table_dcl);
5904e115012SGarrett Wollman 	}
591ff49530fSBill Paul 
592ff49530fSBill Paul 	f_print(fout, "\n#ifdef __cplusplus\n");
593ff49530fSBill Paul 	f_print(fout, "}\n");
594ff49530fSBill Paul 	f_print(fout, "#endif\n");
595ff49530fSBill Paul 
596ff49530fSBill Paul 	f_print(fout, "\n#endif /* !_%s */\n", guard);
5974e115012SGarrett Wollman }
5984e115012SGarrett Wollman 
5994e115012SGarrett Wollman /*
6004e115012SGarrett Wollman  * Compile into an RPC service
6014e115012SGarrett Wollman  */
602526195adSJordan K. Hubbard static void
603ff49530fSBill Paul s_output(argc, argv, infile, define, extend, outfile, nomain, netflag)
6044e115012SGarrett Wollman 	int argc;
6054e115012SGarrett Wollman 	char *argv[];
6064e115012SGarrett Wollman 	char *infile;
6074e115012SGarrett Wollman 	char *define;
6084e115012SGarrett Wollman 	int extend;
6094e115012SGarrett Wollman 	char *outfile;
6104e115012SGarrett Wollman 	int nomain;
611ff49530fSBill Paul 	int netflag;
6124e115012SGarrett Wollman {
6134e115012SGarrett Wollman 	char *include;
6144e115012SGarrett Wollman 	definition *def;
615ff49530fSBill Paul 	int foundprogram = 0;
6164e115012SGarrett Wollman 	char *outfilename;
6174e115012SGarrett Wollman 
6184e115012SGarrett Wollman 	open_input(infile, define);
6194e115012SGarrett Wollman 	outfilename = extend ? extendfile(infile, outfile) : outfile;
6204e115012SGarrett Wollman 	open_output(infile, outfilename);
621ff49530fSBill Paul 	add_warning();
6224e115012SGarrett Wollman 	if (infile && (include = extendfile(infile, ".h"))) {
6234e115012SGarrett Wollman 		f_print(fout, "#include \"%s\"\n", include);
6244e115012SGarrett Wollman 		free(include);
625ff49530fSBill Paul 	} else
626ff49530fSBill Paul 		f_print(fout, "#include <rpc/rpc.h>\n");
627ff49530fSBill Paul 
628ff49530fSBill Paul 	f_print(fout, "#include <stdio.h>\n");
629ff49530fSBill Paul 	f_print(fout, "#include <stdlib.h> /* getenv, exit */\n");
63015df5e2dSStefan Farfeleder 	f_print (fout, "#include <rpc/pmap_clnt.h> /* for pmap_unset */\n");
631ff49530fSBill Paul 	f_print (fout, "#include <string.h> /* strcmp */\n");
632bcb53b16SMartin Blapp 	if (tirpcflag)
633bcb53b16SMartin Blapp 		f_print(fout, "#include <rpc/rpc_com.h>\n");
634ff49530fSBill Paul 	if (strcmp(svcclosetime, "-1") == 0)
635ff49530fSBill Paul 		indefinitewait = 1;
636ff49530fSBill Paul 	else if (strcmp(svcclosetime, "0") == 0)
637ff49530fSBill Paul 		exitnow = 1;
638ff49530fSBill Paul 	else if (inetdflag || pmflag) {
639ff49530fSBill Paul 		f_print(fout, "#include <signal.h>\n");
640ff49530fSBill Paul 		timerflag = 1;
641ff49530fSBill Paul 	}
642ff49530fSBill Paul 
643ff49530fSBill Paul 	if (!tirpcflag && inetdflag)
644ff49530fSBill Paul 		f_print(fout, "#include <sys/ttycom.h> /* TIOCNOTTY */\n");
64515df5e2dSStefan Farfeleder 	if (inetdflag || pmflag) {
646ff49530fSBill Paul 		f_print(fout, "#ifdef __cplusplus\n");
647ff49530fSBill Paul 		f_print(fout,
648ff49530fSBill Paul 			"#include <sysent.h> /* getdtablesize, open */\n");
649ff49530fSBill Paul 		f_print(fout, "#endif /* __cplusplus */\n");
650ff49530fSBill Paul 	}
65140ad8885SAlfred Perlstein 	if (tirpcflag) {
65240ad8885SAlfred Perlstein 		f_print(fout, "#include <fcntl.h> /* open */\n");
65340ad8885SAlfred Perlstein 		f_print(fout, "#include <unistd.h> /* fork / setsid */\n");
654ff49530fSBill Paul 		f_print(fout, "#include <sys/types.h>\n");
65540ad8885SAlfred Perlstein 	}
656ff49530fSBill Paul 
6574f83fd19SStefan Farfeleder 	f_print(fout, "#include <string.h>\n");
658ff49530fSBill Paul 	if (inetdflag || !tirpcflag) {
659ff49530fSBill Paul 		f_print(fout, "#include <sys/socket.h>\n");
660ff49530fSBill Paul 		f_print(fout, "#include <netinet/in.h>\n");
661ff49530fSBill Paul 	}
662ff49530fSBill Paul 
663ff49530fSBill Paul 	if ((netflag || pmflag) && tirpcflag && !nomain) {
664ff49530fSBill Paul 		f_print(fout, "#include <netconfig.h>\n");
665ff49530fSBill Paul 	}
666ff49530fSBill Paul 	if (tirpcflag)
667ff49530fSBill Paul 		f_print(fout, "#include <sys/resource.h> /* rlimit */\n");
66840ad8885SAlfred Perlstein 	if (logflag || inetdflag || pmflag || tirpcflag)
669ff49530fSBill Paul 		f_print(fout, "#include <syslog.h>\n");
670ff49530fSBill Paul 
671ff49530fSBill Paul 	f_print(fout, "\n#ifdef DEBUG\n#define	RPC_SVC_FG\n#endif\n");
672ff49530fSBill Paul 	if (timerflag)
673ff49530fSBill Paul 		f_print(fout, "\n#define	_RPCSVC_CLOSEDOWN %s\n",
674ff49530fSBill Paul 			svcclosetime);
675526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
6764e115012SGarrett Wollman 		foundprogram |= (def->def_kind == DEF_PROGRAM);
6774e115012SGarrett Wollman 	}
6784e115012SGarrett Wollman 	if (extend && !foundprogram) {
6794e115012SGarrett Wollman 		(void) unlink(outfilename);
6804e115012SGarrett Wollman 		return;
6814e115012SGarrett Wollman 	}
682ff49530fSBill Paul 	write_most(infile, netflag, nomain);
683ff49530fSBill Paul 	if (!nomain) {
684ff49530fSBill Paul 		if (!do_registers(argc, argv)) {
685ff49530fSBill Paul 			if (outfilename)
686ff49530fSBill Paul 				(void) unlink(outfilename);
687ff49530fSBill Paul 			usage();
688ff49530fSBill Paul 		}
6894e115012SGarrett Wollman 		write_rest();
6904e115012SGarrett Wollman 	}
6914e115012SGarrett Wollman }
6924e115012SGarrett Wollman 
693ff49530fSBill Paul /*
694ff49530fSBill Paul  * generate client side stubs
695ff49530fSBill Paul  */
696526195adSJordan K. Hubbard static void
6974e115012SGarrett Wollman l_output(infile, define, extend, outfile)
6984e115012SGarrett Wollman 	char *infile;
6994e115012SGarrett Wollman 	char *define;
7004e115012SGarrett Wollman 	int extend;
7014e115012SGarrett Wollman 	char *outfile;
7024e115012SGarrett Wollman {
7034e115012SGarrett Wollman 	char *include;
7044e115012SGarrett Wollman 	definition *def;
705ff49530fSBill Paul 	int foundprogram = 0;
7064e115012SGarrett Wollman 	char *outfilename;
7074e115012SGarrett Wollman 
7084e115012SGarrett Wollman 	open_input(infile, define);
7094e115012SGarrett Wollman 	outfilename = extend ? extendfile(infile, outfile) : outfile;
7104e115012SGarrett Wollman 	open_output(infile, outfilename);
711ff49530fSBill Paul 	add_warning();
7124f83fd19SStefan Farfeleder 	f_print (fout, "#include <string.h> /* for memset */\n");
7134e115012SGarrett Wollman 	if (infile && (include = extendfile(infile, ".h"))) {
7144e115012SGarrett Wollman 		f_print(fout, "#include \"%s\"\n", include);
7154e115012SGarrett Wollman 		free(include);
716ff49530fSBill Paul 	} else
717ff49530fSBill Paul 		f_print(fout, "#include <rpc/rpc.h>\n");
718526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
7194e115012SGarrett Wollman 		foundprogram |= (def->def_kind == DEF_PROGRAM);
7204e115012SGarrett Wollman 	}
7214e115012SGarrett Wollman 	if (extend && !foundprogram) {
7224e115012SGarrett Wollman 		(void) unlink(outfilename);
7234e115012SGarrett Wollman 		return;
7244e115012SGarrett Wollman 	}
7254e115012SGarrett Wollman 	write_stubs();
7264e115012SGarrett Wollman }
7274e115012SGarrett Wollman 
7284e115012SGarrett Wollman /*
729ff49530fSBill Paul  * generate the dispatch table
7304e115012SGarrett Wollman  */
731526195adSJordan K. Hubbard static void
732ff49530fSBill Paul t_output(infile, define, extend, outfile)
733ff49530fSBill Paul 	char *infile;
734ff49530fSBill Paul 	char *define;
735ff49530fSBill Paul 	int extend;
736ff49530fSBill Paul 	char *outfile;
737ff49530fSBill Paul {
738ff49530fSBill Paul 	definition *def;
739ff49530fSBill Paul 	int foundprogram = 0;
740ff49530fSBill Paul 	char *outfilename;
741ff49530fSBill Paul 
742ff49530fSBill Paul 	open_input(infile, define);
743ff49530fSBill Paul 	outfilename = extend ? extendfile(infile, outfile) : outfile;
744ff49530fSBill Paul 	open_output(infile, outfilename);
745ff49530fSBill Paul 	add_warning();
746526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
747ff49530fSBill Paul 		foundprogram |= (def->def_kind == DEF_PROGRAM);
748ff49530fSBill Paul 	}
749ff49530fSBill Paul 	if (extend && !foundprogram) {
750ff49530fSBill Paul 		(void) unlink(outfilename);
751ff49530fSBill Paul 		return;
752ff49530fSBill Paul 	}
753ff49530fSBill Paul 	write_tables();
754ff49530fSBill Paul }
755ff49530fSBill Paul 
756ff49530fSBill Paul /* sample routine for the server template */
757526195adSJordan K. Hubbard static void
758ff49530fSBill Paul svc_output(infile, define, extend, outfile)
759ff49530fSBill Paul 	char *infile;
760ff49530fSBill Paul 	char *define;
761ff49530fSBill Paul 	int extend;
762ff49530fSBill Paul 	char *outfile;
763ff49530fSBill Paul {
764ff49530fSBill Paul 	definition *def;
765ff49530fSBill Paul 	char *include;
766ff49530fSBill Paul 	char *outfilename;
767ff49530fSBill Paul 	long tell;
768ff49530fSBill Paul 	open_input(infile, define);
769ff49530fSBill Paul 	outfilename = extend ? extendfile(infile, outfile) : outfile;
770ff49530fSBill Paul 	checkfiles(infile, outfilename);
771ff49530fSBill Paul 	/*
772ff49530fSBill Paul 	 * Check if outfile already exists.
773ff49530fSBill Paul 	 * if so, print an error message and exit
774ff49530fSBill Paul 	 */
775ff49530fSBill Paul 	open_output(infile, outfilename);
776ff49530fSBill Paul 	add_sample_msg();
777ff49530fSBill Paul 
778ff49530fSBill Paul 	if (infile && (include = extendfile(infile, ".h"))) {
779ff49530fSBill Paul 		f_print(fout, "#include \"%s\"\n", include);
780ff49530fSBill Paul 		free(include);
781ff49530fSBill Paul 	} else
782ff49530fSBill Paul 		f_print(fout, "#include <rpc/rpc.h>\n");
783ff49530fSBill Paul 
784ff49530fSBill Paul 	tell = ftell(fout);
785526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
786ff49530fSBill Paul 		write_sample_svc(def);
787ff49530fSBill Paul 	}
788ff49530fSBill Paul 	if (extend && tell == ftell(fout)) {
789ff49530fSBill Paul 		(void) unlink(outfilename);
790ff49530fSBill Paul 	}
791ff49530fSBill Paul }
792ff49530fSBill Paul 
793ff49530fSBill Paul /* sample main routine for client */
794526195adSJordan K. Hubbard static void
795ff49530fSBill Paul clnt_output(infile, define, extend, outfile)
796ff49530fSBill Paul 	char *infile;
797ff49530fSBill Paul 	char *define;
798ff49530fSBill Paul 	int extend;
799ff49530fSBill Paul 	char *outfile;
800ff49530fSBill Paul {
801ff49530fSBill Paul 	definition *def;
802ff49530fSBill Paul 	char *include;
803ff49530fSBill Paul 	char *outfilename;
804ff49530fSBill Paul 	long tell;
805ff49530fSBill Paul 	int has_program = 0;
806ff49530fSBill Paul 
807ff49530fSBill Paul 	open_input(infile, define);
808ff49530fSBill Paul 	outfilename = extend ? extendfile(infile, outfile) : outfile;
809ff49530fSBill Paul 	checkfiles(infile, outfilename);
810ff49530fSBill Paul 	/*
811ff49530fSBill Paul 	 * Check if outfile already exists.
812ff49530fSBill Paul 	 * if so, print an error message and exit
813ff49530fSBill Paul 	 */
814ff49530fSBill Paul 
815ff49530fSBill Paul 	open_output(infile, outfilename);
816ff49530fSBill Paul 	add_sample_msg();
817ff49530fSBill Paul 	if (infile && (include = extendfile(infile, ".h"))) {
818ff49530fSBill Paul 		f_print(fout, "#include \"%s\"\n", include);
819ff49530fSBill Paul 		free(include);
820ff49530fSBill Paul 	} else
821ff49530fSBill Paul 		f_print(fout, "#include <rpc/rpc.h>\n");
822ff49530fSBill Paul 	tell = ftell(fout);
823526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
824ff49530fSBill Paul 		has_program += write_sample_clnt(def);
825ff49530fSBill Paul 	}
826ff49530fSBill Paul 
827ff49530fSBill Paul 	if (has_program)
828ff49530fSBill Paul 		write_sample_clnt_main();
829ff49530fSBill Paul 
830ff49530fSBill Paul 	if (extend && tell == ftell(fout)) {
831ff49530fSBill Paul 		(void) unlink(outfilename);
832ff49530fSBill Paul 	}
833ff49530fSBill Paul }
834ff49530fSBill Paul 
835ff49530fSBill Paul 
836ff49530fSBill Paul static void mkfile_output(cmd)
837ff49530fSBill Paul struct commandline *cmd;
838ff49530fSBill Paul {
839ff49530fSBill Paul 	char *mkfilename, *clientname, *clntname, *xdrname, *hdrname;
840ff49530fSBill Paul 	char *servername, *svcname, *servprogname, *clntprogname;
841ff49530fSBill Paul 	char *temp;
842ff49530fSBill Paul 
843ff49530fSBill Paul 	svcname = file_name(cmd->infile, "_svc.c");
844ff49530fSBill Paul 	clntname = file_name(cmd->infile, "_clnt.c");
845ff49530fSBill Paul 	xdrname = file_name(cmd->infile, "_xdr.c");
846ff49530fSBill Paul 	hdrname = file_name(cmd->infile, ".h");
847ff49530fSBill Paul 
848ff49530fSBill Paul 
849ff49530fSBill Paul 	if (allfiles){
850ff49530fSBill Paul 		servername = extendfile(cmd->infile, "_server.c");
851ff49530fSBill Paul 		clientname = extendfile(cmd->infile, "_client.c");
852ff49530fSBill Paul 	}else{
853ff49530fSBill Paul 		servername = " ";
854ff49530fSBill Paul 		clientname = " ";
855ff49530fSBill Paul 	}
856ff49530fSBill Paul 	servprogname = extendfile(cmd->infile, "_server");
857ff49530fSBill Paul 	clntprogname = extendfile(cmd->infile, "_client");
858ff49530fSBill Paul 
859ff49530fSBill Paul 	if (allfiles){
86075863a6dSPhilippe Charnier 		mkfilename = xmalloc(strlen("makefile.") +
861ff49530fSBill Paul 		                     strlen(cmd->infile) + 1);
862ff49530fSBill Paul 		temp = (char *)rindex(cmd->infile, '.');
863c5455e6dSMartin Blapp 		strcpy(mkfilename, "makefile.");
864ff49530fSBill Paul 		(void) strncat(mkfilename, cmd->infile,
865ff49530fSBill Paul 			(temp - cmd->infile));
866ff49530fSBill Paul 	} else
867ff49530fSBill Paul 		mkfilename = cmd->outfile;
868ff49530fSBill Paul 
869ff49530fSBill Paul 
870ff49530fSBill Paul 	checkfiles(NULL, mkfilename);
871ff49530fSBill Paul 	open_output(NULL, mkfilename);
872ff49530fSBill Paul 
873ff49530fSBill Paul 	f_print(fout, "\n# This is a template makefile generated\
874ff49530fSBill Paul 		by rpcgen \n");
875ff49530fSBill Paul 
876ff49530fSBill Paul 	f_print(fout, "\n# Parameters \n\n");
877ff49530fSBill Paul 
878ff49530fSBill Paul 	f_print(fout, "CLIENT = %s\nSERVER = %s\n\n",
879ff49530fSBill Paul 		clntprogname, servprogname);
880ff49530fSBill Paul 	f_print(fout, "SOURCES_CLNT.c = \nSOURCES_CLNT.h = \n");
881ff49530fSBill Paul 	f_print(fout, "SOURCES_SVC.c = \nSOURCES_SVC.h = \n");
882ff49530fSBill Paul 	f_print(fout, "SOURCES.x = %s\n\n", cmd->infile);
883ff49530fSBill Paul 	f_print(fout, "TARGETS_SVC.c = %s %s %s \n",
884ff49530fSBill Paul 		svcname, servername, xdrname);
885ff49530fSBill Paul 	f_print(fout, "TARGETS_CLNT.c = %s %s %s \n",
886ff49530fSBill Paul 		clntname, clientname, xdrname);
887ff49530fSBill Paul 	f_print(fout, "TARGETS = %s %s %s %s %s %s\n\n",
888ff49530fSBill Paul 		hdrname, xdrname, clntname,
889ff49530fSBill Paul 		svcname, clientname, servername);
890ff49530fSBill Paul 
891ff49530fSBill Paul 	f_print(fout, "OBJECTS_CLNT = $(SOURCES_CLNT.c:%%.c=%%.o) \
892ff49530fSBill Paul $(TARGETS_CLNT.c:%%.c=%%.o) ");
893ff49530fSBill Paul 
894ff49530fSBill Paul 	f_print(fout, "\nOBJECTS_SVC = $(SOURCES_SVC.c:%%.c=%%.o) \
895ff49530fSBill Paul $(TARGETS_SVC.c:%%.c=%%.o) ");
896ff49530fSBill Paul 
897ff49530fSBill Paul 
898ff49530fSBill Paul 	f_print(fout, "\n# Compiler flags \n");
899ff49530fSBill Paul 	if (mtflag)
90040ad8885SAlfred Perlstein 		f_print(fout, "\nCFLAGS += -D_REENTRANT -D_THEAD_SAFE \nLDLIBS += -pthread\n");
90140ad8885SAlfred Perlstein 
902ff49530fSBill Paul 	f_print(fout, "RPCGENFLAGS = \n");
903ff49530fSBill Paul 
904ff49530fSBill Paul 	f_print(fout, "\n# Targets \n\n");
905ff49530fSBill Paul 
906ff49530fSBill Paul 	f_print(fout, "all : $(CLIENT) $(SERVER)\n\n");
907ff49530fSBill Paul 	f_print(fout, "$(TARGETS) : $(SOURCES.x) \n");
908ff49530fSBill Paul 	f_print(fout, "\trpcgen $(RPCGENFLAGS) $(SOURCES.x)\n\n");
909ff49530fSBill Paul 	f_print(fout, "$(OBJECTS_CLNT) : $(SOURCES_CLNT.c) $(SOURCES_CLNT.h) \
910ff49530fSBill Paul $(TARGETS_CLNT.c) \n\n");
911ff49530fSBill Paul 
912ff49530fSBill Paul 	f_print(fout, "$(OBJECTS_SVC) : $(SOURCES_SVC.c) $(SOURCES_SVC.h) \
913ff49530fSBill Paul $(TARGETS_SVC.c) \n\n");
914ff49530fSBill Paul 	f_print(fout, "$(CLIENT) : $(OBJECTS_CLNT) \n");
915ff49530fSBill Paul 	f_print(fout, "\t$(CC) -o $(CLIENT) $(OBJECTS_CLNT) \
916ff49530fSBill Paul $(LDLIBS) \n\n");
917ff49530fSBill Paul 	f_print(fout, "$(SERVER) : $(OBJECTS_SVC) \n");
918ff49530fSBill Paul 	f_print(fout, "\t$(CC) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS)\n\n ");
919ff49530fSBill Paul 	f_print(fout, "clean:\n\t $(RM) -f core $(TARGETS) $(OBJECTS_CLNT) \
920ff49530fSBill Paul $(OBJECTS_SVC) $(CLIENT) $(SERVER)\n\n");
921ff49530fSBill Paul }
922ff49530fSBill Paul 
923ff49530fSBill Paul 
924ff49530fSBill Paul 
925ff49530fSBill Paul /*
926ff49530fSBill Paul  * Perform registrations for service output
927ff49530fSBill Paul  * Return 0 if failed; 1 otherwise.
928ff49530fSBill Paul  */
929526195adSJordan K. Hubbard static int
930526195adSJordan K. Hubbard do_registers(argc, argv)
9314e115012SGarrett Wollman 	int argc;
9324e115012SGarrett Wollman 	char *argv[];
9334e115012SGarrett Wollman {
9344e115012SGarrett Wollman 	int i;
9354e115012SGarrett Wollman 
936ff49530fSBill Paul 	if (inetdflag || !tirpcflag) {
9374e115012SGarrett Wollman 		for (i = 1; i < argc; i++) {
9384e115012SGarrett Wollman 			if (streq(argv[i], "-s")) {
939ff49530fSBill Paul 				if (!check_nettype(argv[i + 1],
940ff49530fSBill Paul 						    valid_i_nettypes))
941ff49530fSBill Paul 					return (0);
942ff49530fSBill Paul 				write_inetd_register(argv[i + 1]);
9434e115012SGarrett Wollman 				i++;
9444e115012SGarrett Wollman 			}
9454e115012SGarrett Wollman 		}
946ff49530fSBill Paul 	} else {
947ff49530fSBill Paul 		for (i = 1; i < argc; i++)
948ff49530fSBill Paul 			if (streq(argv[i], "-s")) {
949ff49530fSBill Paul 				if (!check_nettype(argv[i + 1],
950ff49530fSBill Paul 						    valid_ti_nettypes))
951ff49530fSBill Paul 					return (0);
952ff49530fSBill Paul 				write_nettype_register(argv[i + 1]);
953ff49530fSBill Paul 				i++;
954ff49530fSBill Paul 			} else if (streq(argv[i], "-n")) {
955ff49530fSBill Paul 				write_netid_register(argv[i + 1]);
956ff49530fSBill Paul 				i++;
957ff49530fSBill Paul 			}
958ff49530fSBill Paul 	}
959ff49530fSBill Paul 	return (1);
960ff49530fSBill Paul }
961ff49530fSBill Paul 
962ff49530fSBill Paul /*
963ff49530fSBill Paul  * Add another argument to the arg list
964ff49530fSBill Paul  */
965ff49530fSBill Paul static void
966ff49530fSBill Paul addarg(cp)
967ff49530fSBill Paul 	char *cp;
968ff49530fSBill Paul {
969ff49530fSBill Paul 	if (argcount >= ARGLISTLEN) {
9700e76f40dSPhilippe Charnier 		warnx("too many defines");
971ff49530fSBill Paul 		crash();
972ff49530fSBill Paul 		/*NOTREACHED*/
973ff49530fSBill Paul 	}
974ff49530fSBill Paul 	arglist[argcount++] = cp;
975ff49530fSBill Paul 
976ff49530fSBill Paul }
977ff49530fSBill Paul 
978ff49530fSBill Paul static void
979ff49530fSBill Paul putarg(where, cp)
980ff49530fSBill Paul 	char *cp;
981ff49530fSBill Paul 	int where;
982ff49530fSBill Paul {
983ff49530fSBill Paul 	if (where >= ARGLISTLEN) {
9840e76f40dSPhilippe Charnier 		warnx("arglist coding error");
985ff49530fSBill Paul 		crash();
986ff49530fSBill Paul 		/*NOTREACHED*/
987ff49530fSBill Paul 	}
988ff49530fSBill Paul 	arglist[where] = cp;
989ff49530fSBill Paul }
990ff49530fSBill Paul 
991ff49530fSBill Paul /*
992ff49530fSBill Paul  * if input file is stdin and an output file is specified then complain
993ff49530fSBill Paul  * if the file already exists. Otherwise the file may get overwritten
994ff49530fSBill Paul  * If input file does not exist, exit with an error
995ff49530fSBill Paul  */
996ff49530fSBill Paul 
997ff49530fSBill Paul static void
998ff49530fSBill Paul checkfiles(infile, outfile)
999ff49530fSBill Paul char *infile;
1000ff49530fSBill Paul char *outfile;
1001ff49530fSBill Paul {
1002ff49530fSBill Paul 
1003ff49530fSBill Paul 	struct stat buf;
1004ff49530fSBill Paul 
1005ff49530fSBill Paul 	if (infile)		/* infile ! = NULL */
1006ff49530fSBill Paul 		if (stat(infile, &buf) < 0)
1007ff49530fSBill Paul 		{
10080e76f40dSPhilippe Charnier 			warn("%s", infile);
1009ff49530fSBill Paul 			crash();
1010ff49530fSBill Paul 		};
1011ff49530fSBill Paul 	if (outfile) {
1012ff49530fSBill Paul 		if (stat(outfile, &buf) < 0)
1013ff49530fSBill Paul 			return;	/* file does not exist */
1014ff49530fSBill Paul 		else {
10150e76f40dSPhilippe Charnier 			warnx("file '%s' already exists and may be overwritten", outfile);
1016ff49530fSBill Paul 			crash();
1017ff49530fSBill Paul 		}
1018ff49530fSBill Paul 	}
10194e115012SGarrett Wollman }
10204e115012SGarrett Wollman 
10214e115012SGarrett Wollman /*
10224e115012SGarrett Wollman  * Parse command line arguments
10234e115012SGarrett Wollman  */
1024526195adSJordan K. Hubbard static int
10254e115012SGarrett Wollman parseargs(argc, argv, cmd)
10264e115012SGarrett Wollman 	int argc;
10274e115012SGarrett Wollman 	char *argv[];
10284e115012SGarrett Wollman 	struct commandline *cmd;
10294e115012SGarrett Wollman {
10304e115012SGarrett Wollman 	int i;
10314e115012SGarrett Wollman 	int j;
1032ff49530fSBill Paul 	char c, ch;
10334e115012SGarrett Wollman 	char flag[(1 << 8 * sizeof (char))];
10344e115012SGarrett Wollman 	int nflags;
10354e115012SGarrett Wollman 
10364e115012SGarrett Wollman 	cmd->infile = cmd->outfile = NULL;
10374e115012SGarrett Wollman 	if (argc < 2) {
10384e115012SGarrett Wollman 		return (0);
10394e115012SGarrett Wollman 	}
1040ff49530fSBill Paul 	allfiles = 0;
10414e115012SGarrett Wollman 	flag['c'] = 0;
10424e115012SGarrett Wollman 	flag['h'] = 0;
10434e115012SGarrett Wollman 	flag['l'] = 0;
10444e115012SGarrett Wollman 	flag['m'] = 0;
1045ff49530fSBill Paul 	flag['o'] = 0;
1046ff49530fSBill Paul 	flag['s'] = 0;
1047ff49530fSBill Paul 	flag['n'] = 0;
1048ff49530fSBill Paul 	flag['t'] = 0;
1049ff49530fSBill Paul 	flag['S'] = 0;
1050ff49530fSBill Paul 	flag['C'] = 0;
1051ff49530fSBill Paul 	flag['M'] = 0;
1052ff49530fSBill Paul 
10534e115012SGarrett Wollman 	for (i = 1; i < argc; i++) {
10544e115012SGarrett Wollman 		if (argv[i][0] != '-') {
10554e115012SGarrett Wollman 			if (cmd->infile) {
10560e76f40dSPhilippe Charnier 				warnx("cannot specify more than one input file");
10574e115012SGarrett Wollman 				return (0);
10584e115012SGarrett Wollman 			}
10594e115012SGarrett Wollman 			cmd->infile = argv[i];
10604e115012SGarrett Wollman 		} else {
10614e115012SGarrett Wollman 			for (j = 1; argv[i][j] != 0; j++) {
10624e115012SGarrett Wollman 				c = argv[i][j];
10634e115012SGarrett Wollman 				switch (c) {
1064ff49530fSBill Paul 				case 'a':
1065ff49530fSBill Paul 					allfiles = 1;
1066ff49530fSBill Paul 					break;
10674e115012SGarrett Wollman 				case 'c':
10684e115012SGarrett Wollman 				case 'h':
10694e115012SGarrett Wollman 				case 'l':
10704e115012SGarrett Wollman 				case 'm':
1071ff49530fSBill Paul 				case 't':
1072526195adSJordan K. Hubbard 					if (flag[(int)c]) {
10734e115012SGarrett Wollman 						return (0);
10744e115012SGarrett Wollman 					}
1075526195adSJordan K. Hubbard 					flag[(int)c] = 1;
10764e115012SGarrett Wollman 					break;
1077ff49530fSBill Paul 				case 'S':
1078ff49530fSBill Paul 					/*
1079ff49530fSBill Paul 					 * sample flag: Ss or Sc.
1080ff49530fSBill Paul 					 *  Ss means set flag['S'];
1081ff49530fSBill Paul 					 *  Sc means set flag['C'];
1082ff49530fSBill Paul 					 *  Sm means set flag['M'];
1083ff49530fSBill Paul 					 */
1084ff49530fSBill Paul 					ch = argv[i][++j]; /* get next char */
1085ff49530fSBill Paul 					if (ch == 's')
1086ff49530fSBill Paul 						ch = 'S';
1087ff49530fSBill Paul 					else if (ch == 'c')
1088ff49530fSBill Paul 						ch = 'C';
1089ff49530fSBill Paul 					else if (ch == 'm')
1090ff49530fSBill Paul 						ch = 'M';
1091ff49530fSBill Paul 					else
1092ff49530fSBill Paul 						return (0);
1093ff49530fSBill Paul 
1094526195adSJordan K. Hubbard 					if (flag[(int)ch]) {
1095ff49530fSBill Paul 						return (0);
1096ff49530fSBill Paul 					}
1097526195adSJordan K. Hubbard 					flag[(int)ch] = 1;
1098ff49530fSBill Paul 					break;
1099ff49530fSBill Paul 				case 'C': /* ANSI C syntax */
1100ff49530fSBill Paul 					ch = argv[i][j+1]; /* get next char */
1101ff49530fSBill Paul 
1102ff49530fSBill Paul 					if (ch != 'C')
1103ff49530fSBill Paul 						break;
1104ff49530fSBill Paul 					CCflag = 1;
1105ff49530fSBill Paul 					break;
1106ff49530fSBill Paul 				case 'b':
1107ff49530fSBill Paul 					/*
1108ff49530fSBill Paul 					 *  Turn TIRPC flag off for
1109ff49530fSBill Paul 					 *  generating backward compatible
1110ff49530fSBill Paul 					 *  code
1111ff49530fSBill Paul 					 */
1112ff49530fSBill Paul 					tirpcflag = 0;
1113ff49530fSBill Paul 					break;
1114ff49530fSBill Paul 
1115ff49530fSBill Paul 				case 'I':
1116ff49530fSBill Paul 					inetdflag = 1;
1117ff49530fSBill Paul 					break;
1118ff49530fSBill Paul 				case 'N':
1119ff49530fSBill Paul 					newstyle = 1;
1120ff49530fSBill Paul 					break;
1121ff49530fSBill Paul 				case 'L':
1122ff49530fSBill Paul 					logflag = 1;
1123ff49530fSBill Paul 					break;
112440ad8885SAlfred Perlstein 				case 'P':
112540ad8885SAlfred Perlstein 					pmflag = 1;
112640ad8885SAlfred Perlstein 					break;
1127ff49530fSBill Paul 				case 'K':
1128ff49530fSBill Paul 					if (++i == argc) {
1129ff49530fSBill Paul 						return (0);
1130ff49530fSBill Paul 					}
1131ff49530fSBill Paul 					svcclosetime = argv[i];
1132ff49530fSBill Paul 					goto nextarg;
1133ff49530fSBill Paul 				case 'T':
1134ff49530fSBill Paul 					tblflag = 1;
1135ff49530fSBill Paul 					break;
1136ff49530fSBill Paul 				case 'M':
1137ff49530fSBill Paul 					mtflag = 1;
1138ff49530fSBill Paul 					break;
1139ff49530fSBill Paul 				case 'i' :
1140ff49530fSBill Paul 					if (++i == argc) {
1141ff49530fSBill Paul 						return (0);
1142ff49530fSBill Paul 					}
1143122562cdSStefan Farfeleder 					inline_size = atoi(argv[i]);
1144ff49530fSBill Paul 					goto nextarg;
1145ff49530fSBill Paul 				case 'n':
11464e115012SGarrett Wollman 				case 'o':
11474e115012SGarrett Wollman 				case 's':
11484e115012SGarrett Wollman 					if (argv[i][j - 1] != '-' ||
11494e115012SGarrett Wollman 					    argv[i][j + 1] != 0) {
11504e115012SGarrett Wollman 						return (0);
11514e115012SGarrett Wollman 					}
1152526195adSJordan K. Hubbard 					flag[(int)c] = 1;
11534e115012SGarrett Wollman 					if (++i == argc) {
11544e115012SGarrett Wollman 						return (0);
11554e115012SGarrett Wollman 					}
1156ff49530fSBill Paul 					if (c == 'o') {
11574e115012SGarrett Wollman 						if (cmd->outfile) {
11584e115012SGarrett Wollman 							return (0);
11594e115012SGarrett Wollman 						}
11604e115012SGarrett Wollman 						cmd->outfile = argv[i];
11614e115012SGarrett Wollman 					}
11624e115012SGarrett Wollman 					goto nextarg;
1163ff49530fSBill Paul 				case 'D':
1164ff49530fSBill Paul 					if (argv[i][j - 1] != '-') {
1165ff49530fSBill Paul 						return (0);
1166ff49530fSBill Paul 					}
1167ff49530fSBill Paul 					(void) addarg(argv[i]);
1168ff49530fSBill Paul 					goto nextarg;
1169ff49530fSBill Paul 				case 'Y':
1170ff49530fSBill Paul 					if (++i == argc) {
1171ff49530fSBill Paul 						return (0);
1172ff49530fSBill Paul 					}
1173a0b13740SKris Kennaway 					(void) strlcpy(pathbuf, argv[i], sizeof(pathbuf));
1174a0b13740SKris Kennaway 					if (strlcat(pathbuf, "/cpp", sizeof(pathbuf))
1175a0b13740SKris Kennaway 					    >= sizeof(pathbuf)) {
1176a0b13740SKris Kennaway 						warnx("argument too long");
1177a0b13740SKris Kennaway 						return (0);
1178a0b13740SKris Kennaway 					}
1179ff49530fSBill Paul 					CPP = pathbuf;
1180ff49530fSBill Paul 					cppDefined = 1;
1181ff49530fSBill Paul 					goto nextarg;
1182ff49530fSBill Paul 
1183ff49530fSBill Paul 
11844e115012SGarrett Wollman 
11854e115012SGarrett Wollman 				default:
11864e115012SGarrett Wollman 					return (0);
11874e115012SGarrett Wollman 				}
11884e115012SGarrett Wollman 			}
11894e115012SGarrett Wollman 		nextarg:
11904e115012SGarrett Wollman 			;
11914e115012SGarrett Wollman 		}
11924e115012SGarrett Wollman 	}
1193ff49530fSBill Paul 
11944e115012SGarrett Wollman 	cmd->cflag = flag['c'];
11954e115012SGarrett Wollman 	cmd->hflag = flag['h'];
11964e115012SGarrett Wollman 	cmd->lflag = flag['l'];
11974e115012SGarrett Wollman 	cmd->mflag = flag['m'];
1198ff49530fSBill Paul 	cmd->nflag = flag['n'];
1199ff49530fSBill Paul 	cmd->sflag = flag['s'];
1200ff49530fSBill Paul 	cmd->tflag = flag['t'];
1201ff49530fSBill Paul 	cmd->Ssflag = flag['S'];
1202ff49530fSBill Paul 	cmd->Scflag = flag['C'];
1203ff49530fSBill Paul 	cmd->makefileflag = flag['M'];
1204ff49530fSBill Paul 
1205ff49530fSBill Paul 	if (tirpcflag) {
120640ad8885SAlfred Perlstein 		if (inetdflag)
120740ad8885SAlfred Perlstein 			pmflag = 0;
1208ff49530fSBill Paul 		if ((inetdflag && cmd->nflag)) {
1209ff49530fSBill Paul 			/* netid not allowed with inetdflag */
12100e76f40dSPhilippe Charnier 			warnx("cannot use netid flag with inetd flag");
1211ff49530fSBill Paul 			return (0);
1212ff49530fSBill Paul 		}
1213ff49530fSBill Paul 	} else {		/* 4.1 mode */
1214ff49530fSBill Paul 		pmflag = 0;	/* set pmflag only in tirpcmode */
1215ff49530fSBill Paul 		if (cmd->nflag) { /* netid needs TIRPC */
12160e76f40dSPhilippe Charnier 			warnx("cannot use netid flag without TIRPC");
1217ff49530fSBill Paul 			return (0);
1218ff49530fSBill Paul 		}
1219ff49530fSBill Paul 	}
1220ff49530fSBill Paul 
1221ff49530fSBill Paul 	if (newstyle && (tblflag || cmd->tflag)) {
12220e76f40dSPhilippe Charnier 		warnx("cannot use table flags with newstyle");
1223ff49530fSBill Paul 		return (0);
1224ff49530fSBill Paul 	}
1225ff49530fSBill Paul 
1226ff49530fSBill Paul 	/* check no conflicts with file generation flags */
1227ff49530fSBill Paul 	nflags = cmd->cflag + cmd->hflag + cmd->lflag + cmd->mflag +
1228ff49530fSBill Paul 		cmd->sflag + cmd->nflag + cmd->tflag + cmd->Ssflag +
1229ff49530fSBill Paul 			cmd->Scflag + cmd->makefileflag;
1230ff49530fSBill Paul 
12314e115012SGarrett Wollman 	if (nflags == 0) {
12324e115012SGarrett Wollman 		if (cmd->outfile != NULL || cmd->infile == NULL) {
12334e115012SGarrett Wollman 			return (0);
12344e115012SGarrett Wollman 		}
1235ff49530fSBill Paul 	} else if (cmd->infile == NULL &&
1236ff49530fSBill Paul 	    (cmd->Ssflag || cmd->Scflag || cmd->makefileflag)) {
12370e76f40dSPhilippe Charnier 		warnx("\"infile\" is required for template generation flags");
1238ff49530fSBill Paul 		return (0);
1239ff49530fSBill Paul 	} if (nflags > 1) {
12400e76f40dSPhilippe Charnier 		warnx("cannot have more than one file generation flag");
12414e115012SGarrett Wollman 		return (0);
12424e115012SGarrett Wollman 	}
12434e115012SGarrett Wollman 	return (1);
12444e115012SGarrett Wollman }
1245ff49530fSBill Paul 
1246526195adSJordan K. Hubbard static void
1247ff49530fSBill Paul usage()
1248ff49530fSBill Paul {
12490e76f40dSPhilippe Charnier 	f_print(stderr, "%s\n%s\n%s\n%s\n%s\n",
12500e76f40dSPhilippe Charnier 		"usage: rpcgen infile",
12510e76f40dSPhilippe Charnier 		"       rpcgen [-abCLNTM] [-Dname[=value]] [-i size]\
125240ad8885SAlfred Perlstein [-I -P [-K seconds]] [-Y path] infile",
12530e76f40dSPhilippe Charnier 		"       rpcgen [-c | -h | -l | -m | -t | -Sc | -Ss | -Sm]\
12540e76f40dSPhilippe Charnier [-o outfile] [infile]",
12550e76f40dSPhilippe Charnier 		"       rpcgen [-s nettype]* [-o outfile] [infile]",
12560e76f40dSPhilippe Charnier 		"       rpcgen [-n netid]* [-o outfile] [infile]");
1257ff49530fSBill Paul 	options_usage();
1258ff49530fSBill Paul 	exit(1);
1259ff49530fSBill Paul }
1260ff49530fSBill Paul 
1261526195adSJordan K. Hubbard static void
1262ff49530fSBill Paul options_usage()
1263ff49530fSBill Paul {
1264ff49530fSBill Paul 	f_print(stderr, "options:\n");
1265ff49530fSBill Paul 	f_print(stderr, "-a\t\tgenerate all files, including samples\n");
1266ff49530fSBill Paul 	f_print(stderr, "-b\t\tbackward compatibility mode (generates code \
126740ad8885SAlfred Perlstein for FreeBSD 4.X)\n");
1268ff49530fSBill Paul 	f_print(stderr, "-c\t\tgenerate XDR routines\n");
1269ff49530fSBill Paul 	f_print(stderr, "-C\t\tANSI C mode\n");
1270ff49530fSBill Paul 	f_print(stderr, "-Dname[=value]\tdefine a symbol (same as #define)\n");
1271ff49530fSBill Paul 	f_print(stderr, "-h\t\tgenerate header file\n");
1272ff49530fSBill Paul 	f_print(stderr, "-i size\t\tsize at which to start generating\
1273ff49530fSBill Paul inline code\n");
127440ad8885SAlfred Perlstein 	f_print(stderr, "-I\t\tgenerate code for inetd support in server\n");
1275ff49530fSBill Paul 	f_print(stderr, "-K seconds\tserver exits after K seconds of\
1276ff49530fSBill Paul inactivity\n");
1277ff49530fSBill Paul 	f_print(stderr, "-l\t\tgenerate client side stubs\n");
1278ff49530fSBill Paul 	f_print(stderr, "-L\t\tserver errors will be printed to syslog\n");
1279ff49530fSBill Paul 	f_print(stderr, "-m\t\tgenerate server side stubs\n");
1280ff49530fSBill Paul 	f_print(stderr, "-M\t\tgenerate MT-safe code\n");
1281ff49530fSBill Paul 	f_print(stderr, "-n netid\tgenerate server code that supports\
1282ff49530fSBill Paul named netid\n");
1283ff49530fSBill Paul 	f_print(stderr, "-N\t\tsupports multiple arguments and\
1284ff49530fSBill Paul call-by-value\n");
1285ff49530fSBill Paul 	f_print(stderr, "-o outfile\tname of the output file\n");
128640ad8885SAlfred Perlstein 	f_print(stderr, "-P\t\tgenerate code for port monitoring support in server\n");
1287ff49530fSBill Paul 	f_print(stderr, "-s nettype\tgenerate server code that supports named\
1288ff49530fSBill Paul nettype\n");
1289ff49530fSBill Paul 	f_print(stderr, "-Sc\t\tgenerate sample client code that uses remote\
1290ff49530fSBill Paul procedures\n");
1291ff49530fSBill Paul 	f_print(stderr, "-Ss\t\tgenerate sample server code that defines\
1292ff49530fSBill Paul remote procedures\n");
1293ff49530fSBill Paul 	f_print(stderr, "-Sm \t\tgenerate makefile template \n");
1294ff49530fSBill Paul 
1295ff49530fSBill Paul 	f_print(stderr, "-t\t\tgenerate RPC dispatch table\n");
1296ff49530fSBill Paul 	f_print(stderr, "-T\t\tgenerate code to support RPC dispatch tables\n");
1297ff49530fSBill Paul 	f_print(stderr, "-Y path\t\tpath where cpp is found\n");
1298ff49530fSBill Paul 	exit(1);
1299ff49530fSBill Paul }
1300