xref: /freebsd/usr.bin/rpcgen/rpc_main.c (revision 1a7ac2bd24c1763b0d32e23d9c70308fbc24b07a)
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 
59e390e3afSDavid Malone static void c_output(const char *, const char *, int, const char *);
60e390e3afSDavid Malone static void h_output(const char *, const char *, int, const char *, int);
61e390e3afSDavid Malone static void l_output(const char *, const char *, int, const char *);
62e390e3afSDavid Malone static void t_output(const char *, const char *, int, const char *);
63e390e3afSDavid Malone static void clnt_output(const char *, const char *, int, const char * );
64e390e3afSDavid Malone static char *generate_guard(const char *);
65e390e3afSDavid Malone static void c_initialize(void);
664e115012SGarrett Wollman 
67*1a7ac2bdSAlfonso Gregory static void usage(void) __dead2;
68d3cb5dedSWarner Losh static void options_usage(void);
69e390e3afSDavid Malone static int do_registers(int, const char **);
70e390e3afSDavid Malone static int parseargs(int, const char **, struct commandline *);
71e390e3afSDavid Malone static void svc_output(const char *, const char *, int, const char *);
72d3cb5dedSWarner Losh static void mkfile_output(struct commandline *);
73e390e3afSDavid Malone static void s_output(int, const char **, const char *, const char *, int, const char *, int, int);
74ff49530fSBill Paul 
75ff49530fSBill Paul #define	EXTEND	1		/* alias for TRUE */
76ff49530fSBill Paul #define	DONT_EXTEND	0		/* alias for FALSE */
77ff49530fSBill Paul 
78e390e3afSDavid Malone static const char *svcclosetime = "120";
790dac0ed8SDimitry Andric static const char *CPP = NULL;
80e390e3afSDavid Malone static const char CPPFLAGS[] = "-C";
81ff49530fSBill Paul static char pathbuf[MAXPATHLEN + 1];
82e390e3afSDavid Malone static const char *allv[] = {
834e115012SGarrett Wollman 	"rpcgen", "-s", "udp", "-s", "tcp",
844e115012SGarrett Wollman };
854b61b26bSMarcelo Araujo static int allc = nitems(allv);
86e390e3afSDavid Malone static const char *allnv[] = {
87ff49530fSBill Paul 	"rpcgen", "-s", "netpath",
88ff49530fSBill Paul };
894b61b26bSMarcelo Araujo static int allnc = nitems(allnv);
90ff49530fSBill Paul 
91ff49530fSBill Paul /*
92ff49530fSBill Paul  * machinations for handling expanding argument list
93ff49530fSBill Paul  */
94e390e3afSDavid Malone static void addarg(const char *);	/* add another argument to the list */
950dac0ed8SDimitry Andric static void insarg(int, const char *);	/* insert arg at specified location */
96e390e3afSDavid Malone static void checkfiles(const char *, const char *);
97e390e3afSDavid Malone 					/* check if out file already exists */
984e115012SGarrett Wollman 
997c8b268aSBrooks Davis static char **arglist;
1007c8b268aSBrooks Davis static int argcount = 0;
1017c8b268aSBrooks Davis static int argmax = 0;
102ff49530fSBill Paul 
103ff49530fSBill Paul int nonfatalerrors;	/* errors */
10440ad8885SAlfred Perlstein int inetdflag = 0;	/* Support for inetd is disabled by default, use -I */
10540ad8885SAlfred Perlstein int pmflag = 0;		/* Support for port monitors is disabled by default */
10640ad8885SAlfred Perlstein int tirpc_socket = 1;	/* TI-RPC on socket, no TLI library */
107ff49530fSBill Paul int logflag;		/* Use syslog instead of fprintf for errors */
108ff49530fSBill Paul int tblflag;		/* Support for dispatch table file */
109ff49530fSBill Paul int mtflag = 0;		/* Support for MT */
11040ad8885SAlfred Perlstein 
111ff49530fSBill Paul #define INLINE 0
112ff49530fSBill Paul /* length at which to start doing an inline */
113ff49530fSBill Paul 
114122562cdSStefan Farfeleder int inline_size = INLINE;
115ff49530fSBill Paul /*
116ff49530fSBill Paul  * Length at which to start doing an inline. INLINE = default
117ff49530fSBill Paul  * if 0, no xdr_inline code
118ff49530fSBill Paul  */
119ff49530fSBill Paul 
120ff49530fSBill Paul int indefinitewait;	/* If started by port monitors, hang till it wants */
121ff49530fSBill Paul int exitnow;		/* If started by port monitors, exit after the call */
122ff49530fSBill Paul int timerflag;		/* TRUE if !indefinite && !exitnow */
123ff49530fSBill Paul int newstyle;		/* newstyle of passing arguments (by value) */
124ff49530fSBill Paul int CCflag = 0;		/* C++ files */
125ff49530fSBill Paul static int allfiles;   /* generate all files */
126ff49530fSBill Paul int tirpcflag = 1;    /* generating code for tirpc, by default */
127ff49530fSBill Paul xdrfunc *xdrfunc_head = NULL; /* xdr function list */
128ff49530fSBill Paul xdrfunc *xdrfunc_tail = NULL; /* xdr function list */
129ff49530fSBill Paul pid_t childpid;
130ff49530fSBill Paul 
1314e115012SGarrett Wollman 
132526195adSJordan K. Hubbard int
133e390e3afSDavid Malone main(int argc, const char *argv[])
1344e115012SGarrett Wollman {
1354e115012SGarrett Wollman 	struct commandline cmd;
1364e115012SGarrett Wollman 
137ff49530fSBill Paul 	(void) memset((char *)&cmd, 0, sizeof (struct commandline));
138ff49530fSBill Paul 	if (!parseargs(argc, argv, &cmd))
139ff49530fSBill Paul 		usage();
140ff49530fSBill Paul 	/*
141ff49530fSBill Paul 	 * Only the client and server side stubs are likely to be customized,
142ff49530fSBill Paul 	 *  so in that case only, check if the outfile exists, and if so,
143ff49530fSBill Paul 	 *  print an error message and exit.
144ff49530fSBill Paul 	 */
145ff49530fSBill Paul 	if (cmd.Ssflag || cmd.Scflag || cmd.makefileflag) {
146ff49530fSBill Paul 		checkfiles(cmd.infile, cmd.outfile);
1474e115012SGarrett Wollman 	}
148ff49530fSBill Paul 	else
149ff49530fSBill Paul 		checkfiles(cmd.infile, NULL);
150ff49530fSBill Paul 
1514e115012SGarrett Wollman 	if (cmd.cflag) {
152ff49530fSBill Paul 		c_output(cmd.infile, "-DRPC_XDR", DONT_EXTEND, cmd.outfile);
1534e115012SGarrett Wollman 	} else if (cmd.hflag) {
154ec06b5e8SStefan Farfeleder 		h_output(cmd.infile, "-DRPC_HDR", DONT_EXTEND, cmd.outfile,
155ec06b5e8SStefan Farfeleder 		    cmd.hflag);
1564e115012SGarrett Wollman 	} else if (cmd.lflag) {
157ff49530fSBill Paul 		l_output(cmd.infile, "-DRPC_CLNT", DONT_EXTEND, cmd.outfile);
158ff49530fSBill Paul 	} else if (cmd.sflag || cmd.mflag || (cmd.nflag)) {
159ff49530fSBill Paul 		s_output(argc, argv, cmd.infile, "-DRPC_SVC", DONT_EXTEND,
160ff49530fSBill Paul 			cmd.outfile, cmd.mflag, cmd.nflag);
161ff49530fSBill Paul 	} else if (cmd.tflag) {
162ff49530fSBill Paul 		t_output(cmd.infile, "-DRPC_TBL", DONT_EXTEND, cmd.outfile);
163ff49530fSBill Paul 	} else if  (cmd.Ssflag) {
164ff49530fSBill Paul 		svc_output(cmd.infile, "-DRPC_SERVER", DONT_EXTEND,
165ff49530fSBill Paul 			cmd.outfile);
166ff49530fSBill Paul 	} else if (cmd.Scflag) {
167ff49530fSBill Paul 		clnt_output(cmd.infile, "-DRPC_CLIENT", DONT_EXTEND,
168ff49530fSBill Paul 			    cmd.outfile);
169ff49530fSBill Paul 	} else if (cmd.makefileflag) {
170ff49530fSBill Paul 		mkfile_output(&cmd);
1714e115012SGarrett Wollman 	} else {
172ff49530fSBill Paul 		/* the rescans are required, since cpp may effect input */
1734e115012SGarrett Wollman 		c_output(cmd.infile, "-DRPC_XDR", EXTEND, "_xdr.c");
1744e115012SGarrett Wollman 		reinitialize();
175ec06b5e8SStefan Farfeleder 		h_output(cmd.infile, "-DRPC_HDR", EXTEND, ".h", cmd.hflag);
1764e115012SGarrett Wollman 		reinitialize();
1774e115012SGarrett Wollman 		l_output(cmd.infile, "-DRPC_CLNT", EXTEND, "_clnt.c");
1784e115012SGarrett Wollman 		reinitialize();
179ff49530fSBill Paul 		if (inetdflag || !tirpcflag)
1804e115012SGarrett Wollman 			s_output(allc, allv, cmd.infile, "-DRPC_SVC", EXTEND,
181ff49530fSBill Paul 			"_svc.c", cmd.mflag, cmd.nflag);
182ff49530fSBill Paul 		else
183ff49530fSBill Paul 			s_output(allnc, allnv, cmd.infile, "-DRPC_SVC",
184ff49530fSBill Paul 				EXTEND, "_svc.c", cmd.mflag, cmd.nflag);
185ff49530fSBill Paul 		if (tblflag) {
186ff49530fSBill Paul 			reinitialize();
187ff49530fSBill Paul 			t_output(cmd.infile, "-DRPC_TBL", EXTEND, "_tbl.i");
1884e115012SGarrett Wollman 		}
1894e115012SGarrett Wollman 
190ff49530fSBill Paul 		if (allfiles) {
191ff49530fSBill Paul 			reinitialize();
192ff49530fSBill Paul 			svc_output(cmd.infile, "-DRPC_SERVER", EXTEND,
193ff49530fSBill Paul 				"_server.c");
194ff49530fSBill Paul 			reinitialize();
195ff49530fSBill Paul 			clnt_output(cmd.infile, "-DRPC_CLIENT", EXTEND,
196ff49530fSBill Paul 				"_client.c");
197ff49530fSBill Paul 
198ff49530fSBill Paul 		}
199ff49530fSBill Paul 		if (allfiles || (cmd.makefileflag == 1)){
200ff49530fSBill Paul 			reinitialize();
201ff49530fSBill Paul 			mkfile_output(&cmd);
202ff49530fSBill Paul 		}
203ff49530fSBill Paul 
204ff49530fSBill Paul 	}
205ff49530fSBill Paul 	exit(nonfatalerrors);
206ff49530fSBill Paul 	/* NOTREACHED */
207ff49530fSBill Paul }
208ff49530fSBill Paul 
209ff49530fSBill Paul 
2104e115012SGarrett Wollman /*
211ff49530fSBill Paul  * add extension to filename
2124e115012SGarrett Wollman  */
2134e115012SGarrett Wollman static char *
214e390e3afSDavid Malone extendfile(const char *path, const char *ext)
2154e115012SGarrett Wollman {
2164e115012SGarrett Wollman 	char *res;
217e390e3afSDavid Malone 	const char *p;
218e390e3afSDavid Malone 	const char *file;
2194e115012SGarrett Wollman 
220b3608ae1SEd Schouten 	if ((file = strrchr(path, '/')) == NULL)
2215ec07232SNate Williams 		file = path;
2225ec07232SNate Williams 	else
2235ec07232SNate Williams 		file++;
22475863a6dSPhilippe Charnier 	res = xmalloc(strlen(file) + strlen(ext) + 1);
225ff49530fSBill Paul 	p = strrchr(file, '.');
2264e115012SGarrett Wollman 	if (p == NULL) {
2274e115012SGarrett Wollman 		p = file + strlen(file);
2284e115012SGarrett Wollman 	}
2294e115012SGarrett Wollman 	(void) strcpy(res, file);
2304e115012SGarrett Wollman 	(void) strcpy(res + (p - file), ext);
2314e115012SGarrett Wollman 	return (res);
2324e115012SGarrett Wollman }
2334e115012SGarrett Wollman 
2344e115012SGarrett Wollman /*
2354e115012SGarrett Wollman  * Open output file with given extension
2364e115012SGarrett Wollman  */
237526195adSJordan K. Hubbard static void
238e390e3afSDavid Malone open_output(const char *infile, const char *outfile)
2394e115012SGarrett Wollman {
240ff49530fSBill Paul 
2414e115012SGarrett Wollman 	if (outfile == NULL) {
2424e115012SGarrett Wollman 		fout = stdout;
2434e115012SGarrett Wollman 		return;
2444e115012SGarrett Wollman 	}
245ff49530fSBill Paul 
2464e115012SGarrett Wollman 	if (infile != NULL && streq(outfile, infile)) {
2470e76f40dSPhilippe Charnier 		warnx("%s already exists. No output generated", infile);
2484e115012SGarrett Wollman 		crash();
2494e115012SGarrett Wollman 	}
2504e115012SGarrett Wollman 	fout = fopen(outfile, "w");
2514e115012SGarrett Wollman 	if (fout == NULL) {
2520e76f40dSPhilippe Charnier 		warn("unable to open %s", outfile);
2534e115012SGarrett Wollman 		crash();
2544e115012SGarrett Wollman 	}
2554e115012SGarrett Wollman 	record_open(outfile);
256ff49530fSBill Paul 
257526195adSJordan K. Hubbard 	return;
258ff49530fSBill Paul }
259ff49530fSBill Paul 
260526195adSJordan K. Hubbard static void
261e390e3afSDavid Malone add_warning(void)
262ff49530fSBill Paul {
263ff49530fSBill Paul 	f_print(fout, "/*\n");
264ff49530fSBill Paul 	f_print(fout, " * Please do not edit this file.\n");
265ff49530fSBill Paul 	f_print(fout, " * It was generated using rpcgen.\n");
266ff49530fSBill Paul 	f_print(fout, " */\n\n");
267ff49530fSBill Paul }
268ff49530fSBill Paul 
2690dac0ed8SDimitry Andric /* prepend C-preprocessor and flags before arguments */
270e390e3afSDavid Malone static void
2710dac0ed8SDimitry Andric prepend_cpp(void)
272ff49530fSBill Paul {
2730dac0ed8SDimitry Andric 	int idx = 1;
2740dac0ed8SDimitry Andric 	const char *var;
2750dac0ed8SDimitry Andric 	char *dupvar, *s, *t;
276ff49530fSBill Paul 
2770dac0ed8SDimitry Andric 	if (CPP != NULL)
2780dac0ed8SDimitry Andric 		insarg(0, CPP);
2790dac0ed8SDimitry Andric 	else if ((var = getenv("RPCGEN_CPP")) == NULL)
2800dac0ed8SDimitry Andric 		insarg(0, "/usr/bin/cpp");
2810dac0ed8SDimitry Andric 	else {
2820dac0ed8SDimitry Andric 		/* Parse command line in a rudimentary way */
2830dac0ed8SDimitry Andric 		dupvar = xstrdup(var);
2840dac0ed8SDimitry Andric 		for (s = dupvar, idx = 0; (t = strsep(&s, " \t")) != NULL; ) {
2850dac0ed8SDimitry Andric 			if (t[0])
2860dac0ed8SDimitry Andric 				insarg(idx++, t);
2870dac0ed8SDimitry Andric 		}
2880dac0ed8SDimitry Andric 		free(dupvar);
2890dac0ed8SDimitry Andric 	}
2900dac0ed8SDimitry Andric 
2910dac0ed8SDimitry Andric 	insarg(idx, CPPFLAGS);
2924e115012SGarrett Wollman }
2934e115012SGarrett Wollman 
2944e115012SGarrett Wollman /*
2954e115012SGarrett Wollman  * Open input file with given define for C-preprocessor
2964e115012SGarrett Wollman  */
297526195adSJordan K. Hubbard static void
298e390e3afSDavid Malone open_input(const char *infile, const char *define)
2994e115012SGarrett Wollman {
3004e115012SGarrett Wollman 	int pd[2];
3014e115012SGarrett Wollman 
3024e115012SGarrett Wollman 	infilename = (infile == NULL) ? "<stdin>" : infile;
3034e115012SGarrett Wollman 	(void) pipe(pd);
304ff49530fSBill Paul 	switch (childpid = fork()) {
3054e115012SGarrett Wollman 	case 0:
3060dac0ed8SDimitry Andric 		prepend_cpp();
307ff49530fSBill Paul 		addarg(define);
308ff49530fSBill Paul 		if (infile)
309ff49530fSBill Paul 			addarg(infile);
310ff49530fSBill Paul 		addarg((char *)NULL);
3114e115012SGarrett Wollman 		(void) close(1);
3124e115012SGarrett Wollman 		(void) dup2(pd[1], 1);
3134e115012SGarrett Wollman 		(void) close(pd[0]);
314005576f6SDimitry Andric 		execvp(arglist[0], arglist);
315005576f6SDimitry Andric 		err(1, "execvp %s", arglist[0]);
3164e115012SGarrett Wollman 	case -1:
31775863a6dSPhilippe Charnier 		err(1, "fork");
3184e115012SGarrett Wollman 	}
3194e115012SGarrett Wollman 	(void) close(pd[1]);
3204e115012SGarrett Wollman 	fin = fdopen(pd[0], "r");
3214e115012SGarrett Wollman 	if (fin == NULL) {
3220e76f40dSPhilippe Charnier 		warn("%s", infilename);
3234e115012SGarrett Wollman 		crash();
3244e115012SGarrett Wollman 	}
3254e115012SGarrett Wollman }
3264e115012SGarrett Wollman 
327ff49530fSBill Paul /* valid tirpc nettypes */
328e390e3afSDavid Malone static const char *valid_ti_nettypes[] =
329ff49530fSBill Paul {
330ff49530fSBill Paul 	"netpath",
331ff49530fSBill Paul 	"visible",
332ff49530fSBill Paul 	"circuit_v",
333ff49530fSBill Paul 	"datagram_v",
334ff49530fSBill Paul 	"circuit_n",
335ff49530fSBill Paul 	"datagram_n",
336ff49530fSBill Paul 	"udp",
337ff49530fSBill Paul 	"tcp",
338ff49530fSBill Paul 	"raw",
339ff49530fSBill Paul 	NULL
340ff49530fSBill Paul 	};
341ff49530fSBill Paul 
342ff49530fSBill Paul /* valid inetd nettypes */
343e390e3afSDavid Malone static const char *valid_i_nettypes[] =
344ff49530fSBill Paul {
345ff49530fSBill Paul 	"udp",
346ff49530fSBill Paul 	"tcp",
347ff49530fSBill Paul 	NULL
348ff49530fSBill Paul 	};
349ff49530fSBill Paul 
350e390e3afSDavid Malone static int
351e390e3afSDavid Malone check_nettype(const char *name, const char *list_to_check[])
352ff49530fSBill Paul {
353ff49530fSBill Paul 	int i;
354ff49530fSBill Paul 	for (i = 0; list_to_check[i] != NULL; i++) {
355ff49530fSBill Paul 		if (strcmp(name, list_to_check[i]) == 0) {
356ff49530fSBill Paul 			return (1);
357ff49530fSBill Paul 		}
358ff49530fSBill Paul 	}
3590e76f40dSPhilippe Charnier 	warnx("illegal nettype :\'%s\'", name);
360ff49530fSBill Paul 	return (0);
361ff49530fSBill Paul }
362ff49530fSBill Paul 
363e390e3afSDavid Malone static const char *
364e390e3afSDavid Malone file_name(const char *file, const char *ext)
365ff49530fSBill Paul {
366ff49530fSBill Paul 	char *temp;
367ff49530fSBill Paul 	temp = extendfile(file, ext);
368ff49530fSBill Paul 
369ff49530fSBill Paul 	if (access(temp, F_OK) != -1)
370ff49530fSBill Paul 		return (temp);
371ff49530fSBill Paul 	else
372e390e3afSDavid Malone 		return (" ");
373ff49530fSBill Paul 
374ff49530fSBill Paul }
375ff49530fSBill Paul 
376ff49530fSBill Paul 
377526195adSJordan K. Hubbard static void
378e390e3afSDavid Malone c_output(const char *infile, const char *define, int extend, const char *outfile)
3794e115012SGarrett Wollman {
3804e115012SGarrett Wollman 	definition *def;
3814e115012SGarrett Wollman 	char *include;
382e390e3afSDavid Malone 	const char *outfilename;
3834e115012SGarrett Wollman 	long tell;
3844e115012SGarrett Wollman 
385ff49530fSBill Paul 	c_initialize();
3864e115012SGarrett Wollman 	open_input(infile, define);
3874e115012SGarrett Wollman 	outfilename = extend ? extendfile(infile, outfile) : outfile;
3884e115012SGarrett Wollman 	open_output(infile, outfilename);
389ff49530fSBill Paul 	add_warning();
3904e115012SGarrett Wollman 	if (infile && (include = extendfile(infile, ".h"))) {
3914e115012SGarrett Wollman 		f_print(fout, "#include \"%s\"\n", include);
3924e115012SGarrett Wollman 		free(include);
393ff49530fSBill Paul 		/* .h file already contains rpc/rpc.h */
394ff49530fSBill Paul 	} else
395ff49530fSBill Paul 		f_print(fout, "#include <rpc/rpc.h>\n");
3964e115012SGarrett Wollman 	tell = ftell(fout);
397526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
3984e115012SGarrett Wollman 		emit(def);
3994e115012SGarrett Wollman 	}
4004e115012SGarrett Wollman 	if (extend && tell == ftell(fout)) {
4014e115012SGarrett Wollman 		(void) unlink(outfilename);
4024e115012SGarrett Wollman 	}
4034e115012SGarrett Wollman }
4044e115012SGarrett Wollman 
405ff49530fSBill Paul 
406526195adSJordan K. Hubbard void
407e390e3afSDavid Malone c_initialize(void)
408ff49530fSBill Paul {
409ff49530fSBill Paul 
410ff49530fSBill Paul 	/* add all the starting basic types */
411ff49530fSBill Paul 	add_type(1, "int");
412ff49530fSBill Paul 	add_type(1, "long");
413ff49530fSBill Paul 	add_type(1, "short");
414ff49530fSBill Paul 	add_type(1, "bool");
415ff49530fSBill Paul 	add_type(1, "u_int");
416ff49530fSBill Paul 	add_type(1, "u_long");
417ff49530fSBill Paul 	add_type(1, "u_short");
418ff49530fSBill Paul 
419ff49530fSBill Paul }
420ff49530fSBill Paul 
421bf70beceSEd Schouten static const char rpcgen_table_dcl[] = "struct rpcgen_table {\n\
422ff49530fSBill Paul 	char	*(*proc)(); \n\
423ff49530fSBill Paul 	xdrproc_t	xdr_arg; \n\
424ff49530fSBill Paul 	unsigned	len_arg; \n\
425ff49530fSBill Paul 	xdrproc_t	xdr_res; \n\
426ff49530fSBill Paul 	unsigned	len_res; \n\
427ff49530fSBill Paul }; \n";
428ff49530fSBill Paul 
429ff49530fSBill Paul 
430e390e3afSDavid Malone char *
431e390e3afSDavid Malone generate_guard(const char *pathname)
432ff49530fSBill Paul {
433e390e3afSDavid Malone 	const char *filename;
434e390e3afSDavid Malone 	char *guard, *tmp, *stopat;
435ff49530fSBill Paul 
436ff49530fSBill Paul 	filename = strrchr(pathname, '/');  /* find last component */
4377dbab955SMarcelo Araujo 	filename = ((filename == NULL) ? pathname : filename+1);
43875863a6dSPhilippe Charnier 	guard = xstrdup(filename);
439080f4020SSean Kelly 	stopat = strrchr(guard, '.');
440080f4020SSean Kelly 
441080f4020SSean Kelly 	/*
442080f4020SSean Kelly 	 * Convert to a valid C macro name and make it upper case.
443080f4020SSean Kelly 	 * Map macro unfriendly characterss to '_'.
44410c546c4SSean Kelly 	 */
445080f4020SSean Kelly 	for (tmp = guard; *tmp != '\000'; ++tmp) {
446ff49530fSBill Paul 		if (islower(*tmp))
447ff49530fSBill Paul 			*tmp = toupper(*tmp);
448080f4020SSean Kelly 		else if (isupper(*tmp) || *tmp == '_')
44910c546c4SSean Kelly 			/* OK for C */;
45010c546c4SSean Kelly 		else if (tmp == guard)
45110c546c4SSean Kelly 			*tmp = '_';
45210c546c4SSean Kelly 		else if (isdigit(*tmp))
45310c546c4SSean Kelly 			/* OK for all but first character */;
454080f4020SSean Kelly 		else if (tmp == stopat) {
455080f4020SSean Kelly 			*tmp = '\0';
45610c546c4SSean Kelly 			break;
45710c546c4SSean Kelly 		} else
45810c546c4SSean Kelly 			*tmp = '_';
45910c546c4SSean Kelly 	}
460080f4020SSean Kelly 	/*
461080f4020SSean Kelly 	 * Can't have a '_' in front, because it'll end up being "__".
462080f4020SSean Kelly 	 * "__" macros shoudln't be used. So, remove all of the
463080f4020SSean Kelly 	 * '_' characters from the front.
46410c546c4SSean Kelly 	 */
465080f4020SSean Kelly 	if (*guard == '_') {
466080f4020SSean Kelly 		for (tmp = guard; *tmp == '_'; ++tmp)
467080f4020SSean Kelly 			;
468080f4020SSean Kelly 		strcpy(guard, tmp);
469ff49530fSBill Paul 	}
470074170fbSWarner Losh 	tmp = guard;
471ff49530fSBill Paul 	guard = extendfile(guard, "_H_RPCGEN");
472074170fbSWarner Losh 	free(tmp);
473ff49530fSBill Paul 	return (guard);
474ff49530fSBill Paul }
475ff49530fSBill Paul 
4764e115012SGarrett Wollman /*
4774e115012SGarrett Wollman  * Compile into an XDR header file
4784e115012SGarrett Wollman  */
479ff49530fSBill Paul 
480ff49530fSBill Paul 
481526195adSJordan K. Hubbard static void
482e390e3afSDavid Malone h_output(const char *infile, const char *define, int extend, const char *outfile, int headeronly)
4834e115012SGarrett Wollman {
4844e115012SGarrett Wollman 	definition *def;
485e390e3afSDavid Malone 	const char *outfilename;
4864e115012SGarrett Wollman 	long tell;
487e390e3afSDavid Malone 	const char *guard;
488ff49530fSBill Paul 	list *l;
489ff49530fSBill Paul 	xdrfunc *xdrfuncp;
490074170fbSWarner Losh 	void *tmp = NULL;
4914e115012SGarrett Wollman 
4924e115012SGarrett Wollman 	open_input(infile, define);
4934e115012SGarrett Wollman 	outfilename =  extend ? extendfile(infile, outfile) : outfile;
4944e115012SGarrett Wollman 	open_output(infile, outfilename);
495ff49530fSBill Paul 	add_warning();
496ff49530fSBill Paul 	if (outfilename || infile){
497074170fbSWarner Losh 		guard = tmp = generate_guard(outfilename ? outfilename: infile);
498ff49530fSBill Paul 	} else
499ff49530fSBill Paul 		guard = "STDIN_";
500ff49530fSBill Paul 
501ff49530fSBill Paul 	f_print(fout, "#ifndef _%s\n#define	_%s\n\n", guard,
502ff49530fSBill Paul 		guard);
503ff49530fSBill Paul 
504ff49530fSBill Paul 	f_print(fout, "#include <rpc/rpc.h>\n");
505ff49530fSBill Paul 
50640ad8885SAlfred Perlstein 	if (mtflag)
50735ab9586SDavid E. O'Brien 		f_print(fout, "#include <pthread.h>\n");
508ff49530fSBill Paul 
509ff49530fSBill Paul 	/* put the C++ support */
51015df5e2dSStefan Farfeleder 	if (!CCflag) {
511ff49530fSBill Paul 		f_print(fout, "\n#ifdef __cplusplus\n");
512ff49530fSBill Paul 		f_print(fout, "extern \"C\" {\n");
513ff49530fSBill Paul 		f_print(fout, "#endif\n\n");
514ff49530fSBill Paul 	}
515ff49530fSBill Paul 
516ff49530fSBill Paul 	/* put in a typedef for quadprecision. Only with Cflag */
517ff49530fSBill Paul 
5184e115012SGarrett Wollman 	tell = ftell(fout);
519ff49530fSBill Paul 
520ff49530fSBill Paul 	/* print data definitions */
521526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
522ec06b5e8SStefan Farfeleder 		print_datadef(def, headeronly);
5234e115012SGarrett Wollman 	}
524ff49530fSBill Paul 
525ff49530fSBill Paul 	/*
526ff49530fSBill Paul 	 * print function declarations.
527ff49530fSBill Paul 	 *  Do this after data definitions because they might be used as
528ff49530fSBill Paul 	 *  arguments for functions
529ff49530fSBill Paul 	 */
530ff49530fSBill Paul 	for (l = defined; l != NULL; l = l->next) {
531ec06b5e8SStefan Farfeleder 		print_funcdef(l->val, headeronly);
532ff49530fSBill Paul 	}
533ff49530fSBill Paul 	/* Now  print all xdr func declarations */
534ff49530fSBill Paul 	if (xdrfunc_head != NULL){
535ff49530fSBill Paul 
536ff49530fSBill Paul 		f_print(fout,
537ff49530fSBill Paul 			"\n/* the xdr functions */\n");
538ff49530fSBill Paul 
539ff49530fSBill Paul 		if (CCflag){
540ff49530fSBill Paul 			f_print(fout, "\n#ifdef __cplusplus\n");
541ff49530fSBill Paul 			f_print(fout, "extern \"C\" {\n");
542ff49530fSBill Paul 			f_print(fout, "#endif\n");
543ff49530fSBill Paul 		}
544ff49530fSBill Paul 
545ff49530fSBill Paul 		xdrfuncp = xdrfunc_head;
546ff49530fSBill Paul 		while (xdrfuncp != NULL){
54715df5e2dSStefan Farfeleder 			print_xdr_func_def(xdrfuncp->name, xdrfuncp->pointerp);
548ff49530fSBill Paul 			xdrfuncp = xdrfuncp->next;
549ff49530fSBill Paul 		}
550ff49530fSBill Paul 	}
551ff49530fSBill Paul 
5524e115012SGarrett Wollman 	if (extend && tell == ftell(fout)) {
5534e115012SGarrett Wollman 		(void) unlink(outfilename);
554ff49530fSBill Paul 	} else if (tblflag) {
555ff49530fSBill Paul 		f_print(fout, rpcgen_table_dcl);
5564e115012SGarrett Wollman 	}
557ff49530fSBill Paul 
558ff49530fSBill Paul 	f_print(fout, "\n#ifdef __cplusplus\n");
559ff49530fSBill Paul 	f_print(fout, "}\n");
560ff49530fSBill Paul 	f_print(fout, "#endif\n");
561ff49530fSBill Paul 
562ff49530fSBill Paul 	f_print(fout, "\n#endif /* !_%s */\n", guard);
563074170fbSWarner Losh 	free(tmp);
5644e115012SGarrett Wollman }
5654e115012SGarrett Wollman 
5664e115012SGarrett Wollman /*
5674e115012SGarrett Wollman  * Compile into an RPC service
5684e115012SGarrett Wollman  */
569526195adSJordan K. Hubbard static void
570e390e3afSDavid Malone s_output(int argc, const char *argv[], const char *infile, const char *define,
571e390e3afSDavid Malone     int extend, const char *outfile, int nomain, int netflag)
5724e115012SGarrett Wollman {
5734e115012SGarrett Wollman 	char *include;
5744e115012SGarrett Wollman 	definition *def;
575ff49530fSBill Paul 	int foundprogram = 0;
576e390e3afSDavid Malone 	const char *outfilename;
5774e115012SGarrett Wollman 
5784e115012SGarrett Wollman 	open_input(infile, define);
5794e115012SGarrett Wollman 	outfilename = extend ? extendfile(infile, outfile) : outfile;
5804e115012SGarrett Wollman 	open_output(infile, outfilename);
581ff49530fSBill Paul 	add_warning();
5824e115012SGarrett Wollman 	if (infile && (include = extendfile(infile, ".h"))) {
5834e115012SGarrett Wollman 		f_print(fout, "#include \"%s\"\n", include);
5844e115012SGarrett Wollman 		free(include);
585ff49530fSBill Paul 	} else
586ff49530fSBill Paul 		f_print(fout, "#include <rpc/rpc.h>\n");
587ff49530fSBill Paul 
588ff49530fSBill Paul 	f_print(fout, "#include <stdio.h>\n");
589ff49530fSBill Paul 	f_print(fout, "#include <stdlib.h> /* getenv, exit */\n");
59015df5e2dSStefan Farfeleder 	f_print (fout, "#include <rpc/pmap_clnt.h> /* for pmap_unset */\n");
591ff49530fSBill Paul 	f_print (fout, "#include <string.h> /* strcmp */\n");
592bcb53b16SMartin Blapp 	if (tirpcflag)
593bcb53b16SMartin Blapp 		f_print(fout, "#include <rpc/rpc_com.h>\n");
594ff49530fSBill Paul 	if (strcmp(svcclosetime, "-1") == 0)
595ff49530fSBill Paul 		indefinitewait = 1;
596ff49530fSBill Paul 	else if (strcmp(svcclosetime, "0") == 0)
597ff49530fSBill Paul 		exitnow = 1;
598ff49530fSBill Paul 	else if (inetdflag || pmflag) {
599ff49530fSBill Paul 		f_print(fout, "#include <signal.h>\n");
600ff49530fSBill Paul 		timerflag = 1;
601ff49530fSBill Paul 	}
602ff49530fSBill Paul 
603ff49530fSBill Paul 	if (!tirpcflag && inetdflag)
604ff49530fSBill Paul 		f_print(fout, "#include <sys/ttycom.h> /* TIOCNOTTY */\n");
60515df5e2dSStefan Farfeleder 	if (inetdflag || pmflag) {
606ff49530fSBill Paul 		f_print(fout, "#ifdef __cplusplus\n");
607ff49530fSBill Paul 		f_print(fout,
608f87730d4SXin LI 			"#include <sys/sysent.h> /* getdtablesize, open */\n");
609ff49530fSBill Paul 		f_print(fout, "#endif /* __cplusplus */\n");
610ff49530fSBill Paul 	}
61140ad8885SAlfred Perlstein 	if (tirpcflag) {
61240ad8885SAlfred Perlstein 		f_print(fout, "#include <fcntl.h> /* open */\n");
61340ad8885SAlfred Perlstein 		f_print(fout, "#include <unistd.h> /* fork / setsid */\n");
614ff49530fSBill Paul 		f_print(fout, "#include <sys/types.h>\n");
61540ad8885SAlfred Perlstein 	}
616ff49530fSBill Paul 
6174f83fd19SStefan Farfeleder 	f_print(fout, "#include <string.h>\n");
618ff49530fSBill Paul 	if (inetdflag || !tirpcflag) {
619ff49530fSBill Paul 		f_print(fout, "#include <sys/socket.h>\n");
620ff49530fSBill Paul 		f_print(fout, "#include <netinet/in.h>\n");
621ff49530fSBill Paul 	}
622ff49530fSBill Paul 
623ff49530fSBill Paul 	if ((netflag || pmflag) && tirpcflag && !nomain) {
624ff49530fSBill Paul 		f_print(fout, "#include <netconfig.h>\n");
625ff49530fSBill Paul 	}
626ff49530fSBill Paul 	if (tirpcflag)
627ff49530fSBill Paul 		f_print(fout, "#include <sys/resource.h> /* rlimit */\n");
62840ad8885SAlfred Perlstein 	if (logflag || inetdflag || pmflag || tirpcflag)
629ff49530fSBill Paul 		f_print(fout, "#include <syslog.h>\n");
630ff49530fSBill Paul 
631ff49530fSBill Paul 	f_print(fout, "\n#ifdef DEBUG\n#define	RPC_SVC_FG\n#endif\n");
632ff49530fSBill Paul 	if (timerflag)
633ff49530fSBill Paul 		f_print(fout, "\n#define	_RPCSVC_CLOSEDOWN %s\n",
634ff49530fSBill Paul 			svcclosetime);
635526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
6364e115012SGarrett Wollman 		foundprogram |= (def->def_kind == DEF_PROGRAM);
6374e115012SGarrett Wollman 	}
6384e115012SGarrett Wollman 	if (extend && !foundprogram) {
6394e115012SGarrett Wollman 		(void) unlink(outfilename);
6404e115012SGarrett Wollman 		return;
6414e115012SGarrett Wollman 	}
642ff49530fSBill Paul 	write_most(infile, netflag, nomain);
643ff49530fSBill Paul 	if (!nomain) {
644ff49530fSBill Paul 		if (!do_registers(argc, argv)) {
645ff49530fSBill Paul 			if (outfilename)
646ff49530fSBill Paul 				(void) unlink(outfilename);
647ff49530fSBill Paul 			usage();
648ff49530fSBill Paul 		}
6494e115012SGarrett Wollman 		write_rest();
6504e115012SGarrett Wollman 	}
6514e115012SGarrett Wollman }
6524e115012SGarrett Wollman 
653ff49530fSBill Paul /*
654ff49530fSBill Paul  * generate client side stubs
655ff49530fSBill Paul  */
656526195adSJordan K. Hubbard static void
657e390e3afSDavid Malone l_output(const char *infile, const char *define, int extend, const char *outfile)
6584e115012SGarrett Wollman {
6594e115012SGarrett Wollman 	char *include;
6604e115012SGarrett Wollman 	definition *def;
661ff49530fSBill Paul 	int foundprogram = 0;
662e390e3afSDavid Malone 	const char *outfilename;
6634e115012SGarrett Wollman 
6644e115012SGarrett Wollman 	open_input(infile, define);
6654e115012SGarrett Wollman 	outfilename = extend ? extendfile(infile, outfile) : outfile;
6664e115012SGarrett Wollman 	open_output(infile, outfilename);
667ff49530fSBill Paul 	add_warning();
6684f83fd19SStefan Farfeleder 	f_print (fout, "#include <string.h> /* for memset */\n");
6694e115012SGarrett Wollman 	if (infile && (include = extendfile(infile, ".h"))) {
6704e115012SGarrett Wollman 		f_print(fout, "#include \"%s\"\n", include);
6714e115012SGarrett Wollman 		free(include);
672ff49530fSBill Paul 	} else
673ff49530fSBill Paul 		f_print(fout, "#include <rpc/rpc.h>\n");
674526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
6754e115012SGarrett Wollman 		foundprogram |= (def->def_kind == DEF_PROGRAM);
6764e115012SGarrett Wollman 	}
6774e115012SGarrett Wollman 	if (extend && !foundprogram) {
6784e115012SGarrett Wollman 		(void) unlink(outfilename);
6794e115012SGarrett Wollman 		return;
6804e115012SGarrett Wollman 	}
6814e115012SGarrett Wollman 	write_stubs();
6824e115012SGarrett Wollman }
6834e115012SGarrett Wollman 
6844e115012SGarrett Wollman /*
685ff49530fSBill Paul  * generate the dispatch table
6864e115012SGarrett Wollman  */
687526195adSJordan K. Hubbard static void
688e390e3afSDavid Malone t_output(const char *infile, const char *define, int extend, const char *outfile)
689ff49530fSBill Paul {
690ff49530fSBill Paul 	definition *def;
691ff49530fSBill Paul 	int foundprogram = 0;
692e390e3afSDavid Malone 	const char *outfilename;
693ff49530fSBill Paul 
694ff49530fSBill Paul 	open_input(infile, define);
695ff49530fSBill Paul 	outfilename = extend ? extendfile(infile, outfile) : outfile;
696ff49530fSBill Paul 	open_output(infile, outfilename);
697ff49530fSBill Paul 	add_warning();
698526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
699ff49530fSBill Paul 		foundprogram |= (def->def_kind == DEF_PROGRAM);
700ff49530fSBill Paul 	}
701ff49530fSBill Paul 	if (extend && !foundprogram) {
702ff49530fSBill Paul 		(void) unlink(outfilename);
703ff49530fSBill Paul 		return;
704ff49530fSBill Paul 	}
705ff49530fSBill Paul 	write_tables();
706ff49530fSBill Paul }
707ff49530fSBill Paul 
708ff49530fSBill Paul /* sample routine for the server template */
709526195adSJordan K. Hubbard static void
710e390e3afSDavid Malone svc_output(const char *infile, const char *define, int extend, const char *outfile)
711ff49530fSBill Paul {
712ff49530fSBill Paul 	definition *def;
713ff49530fSBill Paul 	char *include;
714e390e3afSDavid Malone 	const char *outfilename;
715ff49530fSBill Paul 	long tell;
716ff49530fSBill Paul 	open_input(infile, define);
717ff49530fSBill Paul 	outfilename = extend ? extendfile(infile, outfile) : outfile;
718ff49530fSBill Paul 	checkfiles(infile, outfilename);
719ff49530fSBill Paul 	/*
720ff49530fSBill Paul 	 * Check if outfile already exists.
721ff49530fSBill Paul 	 * if so, print an error message and exit
722ff49530fSBill Paul 	 */
723ff49530fSBill Paul 	open_output(infile, outfilename);
724ff49530fSBill Paul 	add_sample_msg();
725ff49530fSBill Paul 
726ff49530fSBill Paul 	if (infile && (include = extendfile(infile, ".h"))) {
727ff49530fSBill Paul 		f_print(fout, "#include \"%s\"\n", include);
728ff49530fSBill Paul 		free(include);
729ff49530fSBill Paul 	} else
730ff49530fSBill Paul 		f_print(fout, "#include <rpc/rpc.h>\n");
731ff49530fSBill Paul 
732ff49530fSBill Paul 	tell = ftell(fout);
733526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
734ff49530fSBill Paul 		write_sample_svc(def);
735ff49530fSBill Paul 	}
736ff49530fSBill Paul 	if (extend && tell == ftell(fout)) {
737ff49530fSBill Paul 		(void) unlink(outfilename);
738ff49530fSBill Paul 	}
739ff49530fSBill Paul }
740ff49530fSBill Paul 
741ff49530fSBill Paul /* sample main routine for client */
742526195adSJordan K. Hubbard static void
743e390e3afSDavid Malone clnt_output(const char *infile, const char *define, int extend, const char *outfile)
744ff49530fSBill Paul {
745ff49530fSBill Paul 	definition *def;
746ff49530fSBill Paul 	char *include;
747e390e3afSDavid Malone 	const char *outfilename;
748ff49530fSBill Paul 	long tell;
749ff49530fSBill Paul 	int has_program = 0;
750ff49530fSBill Paul 
751ff49530fSBill Paul 	open_input(infile, define);
752ff49530fSBill Paul 	outfilename = extend ? extendfile(infile, outfile) : outfile;
753ff49530fSBill Paul 	checkfiles(infile, outfilename);
754ff49530fSBill Paul 	/*
755ff49530fSBill Paul 	 * Check if outfile already exists.
756ff49530fSBill Paul 	 * if so, print an error message and exit
757ff49530fSBill Paul 	 */
758ff49530fSBill Paul 
759ff49530fSBill Paul 	open_output(infile, outfilename);
760ff49530fSBill Paul 	add_sample_msg();
761ff49530fSBill Paul 	if (infile && (include = extendfile(infile, ".h"))) {
762ff49530fSBill Paul 		f_print(fout, "#include \"%s\"\n", include);
763ff49530fSBill Paul 		free(include);
764ff49530fSBill Paul 	} else
765ff49530fSBill Paul 		f_print(fout, "#include <rpc/rpc.h>\n");
7667c31e86bSDoug Rabson 	f_print(fout, "#include <stdio.h>\n");
7677c31e86bSDoug Rabson 	f_print(fout, "#include <stdlib.h>\n");
768ff49530fSBill Paul 	tell = ftell(fout);
769526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
770ff49530fSBill Paul 		has_program += write_sample_clnt(def);
771ff49530fSBill Paul 	}
772ff49530fSBill Paul 
773ff49530fSBill Paul 	if (has_program)
774ff49530fSBill Paul 		write_sample_clnt_main();
775ff49530fSBill Paul 
776ff49530fSBill Paul 	if (extend && tell == ftell(fout)) {
777ff49530fSBill Paul 		(void) unlink(outfilename);
778ff49530fSBill Paul 	}
779ff49530fSBill Paul }
780ff49530fSBill Paul 
781ff49530fSBill Paul 
782e390e3afSDavid Malone static void mkfile_output(struct commandline *cmd)
783ff49530fSBill Paul {
784e390e3afSDavid Malone 	const char *mkfilename, *clientname, *clntname, *xdrname, *hdrname;
785e390e3afSDavid Malone 	const char *servername, *svcname, *servprogname, *clntprogname;
786e390e3afSDavid Malone 	char *temp, *mkftemp;
787ff49530fSBill Paul 
788ff49530fSBill Paul 	svcname = file_name(cmd->infile, "_svc.c");
789ff49530fSBill Paul 	clntname = file_name(cmd->infile, "_clnt.c");
790ff49530fSBill Paul 	xdrname = file_name(cmd->infile, "_xdr.c");
791ff49530fSBill Paul 	hdrname = file_name(cmd->infile, ".h");
792ff49530fSBill Paul 
793ff49530fSBill Paul 
794ff49530fSBill Paul 	if (allfiles){
795ff49530fSBill Paul 		servername = extendfile(cmd->infile, "_server.c");
796ff49530fSBill Paul 		clientname = extendfile(cmd->infile, "_client.c");
797ff49530fSBill Paul 	}else{
798ff49530fSBill Paul 		servername = " ";
799ff49530fSBill Paul 		clientname = " ";
800ff49530fSBill Paul 	}
801ff49530fSBill Paul 	servprogname = extendfile(cmd->infile, "_server");
802ff49530fSBill Paul 	clntprogname = extendfile(cmd->infile, "_client");
803ff49530fSBill Paul 
804ff49530fSBill Paul 	if (allfiles){
805e390e3afSDavid Malone 		mkftemp = xmalloc(strlen("makefile.") +
806ff49530fSBill Paul 		                     strlen(cmd->infile) + 1);
807b3608ae1SEd Schouten 		temp = strrchr(cmd->infile, '.');
808e390e3afSDavid Malone 		strcpy(mkftemp, "makefile.");
809e390e3afSDavid Malone 		(void) strncat(mkftemp, cmd->infile,
810ff49530fSBill Paul 			(temp - cmd->infile));
811e390e3afSDavid Malone 		mkfilename = mkftemp;
812ff49530fSBill Paul 	} else
813ff49530fSBill Paul 		mkfilename = cmd->outfile;
814ff49530fSBill Paul 
815ff49530fSBill Paul 
816ff49530fSBill Paul 	checkfiles(NULL, mkfilename);
817ff49530fSBill Paul 	open_output(NULL, mkfilename);
818ff49530fSBill Paul 
819ff49530fSBill Paul 	f_print(fout, "\n# This is a template makefile generated\
820ff49530fSBill Paul 		by rpcgen \n");
821ff49530fSBill Paul 
822ff49530fSBill Paul 	f_print(fout, "\n# Parameters \n\n");
823ff49530fSBill Paul 
824ff49530fSBill Paul 	f_print(fout, "CLIENT = %s\nSERVER = %s\n\n",
825ff49530fSBill Paul 		clntprogname, servprogname);
826ff49530fSBill Paul 	f_print(fout, "SOURCES_CLNT.c = \nSOURCES_CLNT.h = \n");
827ff49530fSBill Paul 	f_print(fout, "SOURCES_SVC.c = \nSOURCES_SVC.h = \n");
828ff49530fSBill Paul 	f_print(fout, "SOURCES.x = %s\n\n", cmd->infile);
829ff49530fSBill Paul 	f_print(fout, "TARGETS_SVC.c = %s %s %s \n",
830ff49530fSBill Paul 		svcname, servername, xdrname);
831ff49530fSBill Paul 	f_print(fout, "TARGETS_CLNT.c = %s %s %s \n",
832ff49530fSBill Paul 		clntname, clientname, xdrname);
833ff49530fSBill Paul 	f_print(fout, "TARGETS = %s %s %s %s %s %s\n\n",
834ff49530fSBill Paul 		hdrname, xdrname, clntname,
835ff49530fSBill Paul 		svcname, clientname, servername);
836ff49530fSBill Paul 
837ff49530fSBill Paul 	f_print(fout, "OBJECTS_CLNT = $(SOURCES_CLNT.c:%%.c=%%.o) \
838ff49530fSBill Paul $(TARGETS_CLNT.c:%%.c=%%.o) ");
839ff49530fSBill Paul 
840ff49530fSBill Paul 	f_print(fout, "\nOBJECTS_SVC = $(SOURCES_SVC.c:%%.c=%%.o) \
841ff49530fSBill Paul $(TARGETS_SVC.c:%%.c=%%.o) ");
842ff49530fSBill Paul 
843ff49530fSBill Paul 
844ff49530fSBill Paul 	f_print(fout, "\n# Compiler flags \n");
845ff49530fSBill Paul 	if (mtflag)
84640ad8885SAlfred Perlstein 		f_print(fout, "\nCFLAGS += -D_REENTRANT -D_THEAD_SAFE \nLDLIBS += -pthread\n");
84740ad8885SAlfred Perlstein 
848ff49530fSBill Paul 	f_print(fout, "RPCGENFLAGS = \n");
849ff49530fSBill Paul 
850ff49530fSBill Paul 	f_print(fout, "\n# Targets \n\n");
851ff49530fSBill Paul 
852ff49530fSBill Paul 	f_print(fout, "all : $(CLIENT) $(SERVER)\n\n");
853ff49530fSBill Paul 	f_print(fout, "$(TARGETS) : $(SOURCES.x) \n");
854ff49530fSBill Paul 	f_print(fout, "\trpcgen $(RPCGENFLAGS) $(SOURCES.x)\n\n");
8557c31e86bSDoug Rabson 	if (allfiles) {
8567c31e86bSDoug Rabson 		f_print(fout, "\trpcgen -Sc $(RPCGENFLAGS) $(SOURCES.x) -o %s\n\n", clientname);
8577c31e86bSDoug Rabson 		f_print(fout, "\trpcgen -Ss $(RPCGENFLAGS) $(SOURCES.x) -o %s\n\n", servername);
8587c31e86bSDoug Rabson 	}
859ff49530fSBill Paul 	f_print(fout, "$(OBJECTS_CLNT) : $(SOURCES_CLNT.c) $(SOURCES_CLNT.h) \
860ff49530fSBill Paul $(TARGETS_CLNT.c) \n\n");
861ff49530fSBill Paul 
862ff49530fSBill Paul 	f_print(fout, "$(OBJECTS_SVC) : $(SOURCES_SVC.c) $(SOURCES_SVC.h) \
863ff49530fSBill Paul $(TARGETS_SVC.c) \n\n");
864ff49530fSBill Paul 	f_print(fout, "$(CLIENT) : $(OBJECTS_CLNT) \n");
865ff49530fSBill Paul 	f_print(fout, "\t$(CC) -o $(CLIENT) $(OBJECTS_CLNT) \
866ff49530fSBill Paul $(LDLIBS) \n\n");
867ff49530fSBill Paul 	f_print(fout, "$(SERVER) : $(OBJECTS_SVC) \n");
868ff49530fSBill Paul 	f_print(fout, "\t$(CC) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS)\n\n");
86936752498SBryan Drewery 	f_print(fout, "clean:\n\t rm -f core $(TARGETS) $(OBJECTS_CLNT) \
870ff49530fSBill Paul $(OBJECTS_SVC) $(CLIENT) $(SERVER)\n\n");
871ff49530fSBill Paul }
872ff49530fSBill Paul 
873ff49530fSBill Paul 
874ff49530fSBill Paul 
875ff49530fSBill Paul /*
876ff49530fSBill Paul  * Perform registrations for service output
877ff49530fSBill Paul  * Return 0 if failed; 1 otherwise.
878ff49530fSBill Paul  */
879526195adSJordan K. Hubbard static int
880e390e3afSDavid Malone do_registers(int argc, const char *argv[])
8814e115012SGarrett Wollman {
8824e115012SGarrett Wollman 	int i;
8834e115012SGarrett Wollman 
884ff49530fSBill Paul 	if (inetdflag || !tirpcflag) {
8854e115012SGarrett Wollman 		for (i = 1; i < argc; i++) {
8864e115012SGarrett Wollman 			if (streq(argv[i], "-s")) {
887ff49530fSBill Paul 				if (!check_nettype(argv[i + 1],
888ff49530fSBill Paul 						    valid_i_nettypes))
889ff49530fSBill Paul 					return (0);
890ff49530fSBill Paul 				write_inetd_register(argv[i + 1]);
8914e115012SGarrett Wollman 				i++;
8924e115012SGarrett Wollman 			}
8934e115012SGarrett Wollman 		}
894ff49530fSBill Paul 	} else {
895ff49530fSBill Paul 		for (i = 1; i < argc; i++)
896ff49530fSBill Paul 			if (streq(argv[i], "-s")) {
897ff49530fSBill Paul 				if (!check_nettype(argv[i + 1],
898ff49530fSBill Paul 						    valid_ti_nettypes))
899ff49530fSBill Paul 					return (0);
900ff49530fSBill Paul 				write_nettype_register(argv[i + 1]);
901ff49530fSBill Paul 				i++;
902ff49530fSBill Paul 			} else if (streq(argv[i], "-n")) {
903ff49530fSBill Paul 				write_netid_register(argv[i + 1]);
904ff49530fSBill Paul 				i++;
905ff49530fSBill Paul 			}
906ff49530fSBill Paul 	}
907ff49530fSBill Paul 	return (1);
908ff49530fSBill Paul }
909ff49530fSBill Paul 
910ff49530fSBill Paul /*
9117c8b268aSBrooks Davis  * Extend the argument list
9127c8b268aSBrooks Davis  */
9137c8b268aSBrooks Davis static void
9147c8b268aSBrooks Davis moreargs(void)
9157c8b268aSBrooks Davis {
9167c8b268aSBrooks Davis 	char **newarglist;
9177c8b268aSBrooks Davis 
9187c8b268aSBrooks Davis 	argmax = argmax == 0 ? 32 : argmax << 1;
9197c8b268aSBrooks Davis 	if (argmax > INT_MAX / 4) {
9207c8b268aSBrooks Davis 		warnx("refusing to allocate too many arguments");
9217c8b268aSBrooks Davis 		crash();
9227c8b268aSBrooks Davis 	}
9237c8b268aSBrooks Davis 	newarglist = realloc(arglist, argmax * sizeof(*arglist));
9247c8b268aSBrooks Davis 	if (newarglist == NULL) {
9257c8b268aSBrooks Davis 		warnx("unable to allocate arglist");
9267c8b268aSBrooks Davis 		crash();
9277c8b268aSBrooks Davis 	}
9287c8b268aSBrooks Davis 	arglist = newarglist;
9297c8b268aSBrooks Davis }
9307c8b268aSBrooks Davis 
9317c8b268aSBrooks Davis /*
932ff49530fSBill Paul  * Add another argument to the arg list
933ff49530fSBill Paul  */
934ff49530fSBill Paul static void
935e390e3afSDavid Malone addarg(const char *cp)
936ff49530fSBill Paul {
9377c8b268aSBrooks Davis 	if (argcount >= argmax)
9387c8b268aSBrooks Davis 		moreargs();
9397c8b268aSBrooks Davis 
9408cb6926eSCraig Rodrigues 	if (cp != NULL)
9418cb6926eSCraig Rodrigues 		arglist[argcount++] = xstrdup(cp);
9428cb6926eSCraig Rodrigues 	else
9438cb6926eSCraig Rodrigues 		arglist[argcount++] = NULL;
944ff49530fSBill Paul }
945ff49530fSBill Paul 
9460dac0ed8SDimitry Andric /*
9470dac0ed8SDimitry Andric  * Insert an argument at the specified location
9480dac0ed8SDimitry Andric  */
949ff49530fSBill Paul static void
9500dac0ed8SDimitry Andric insarg(int place, const char *cp)
951ff49530fSBill Paul {
9520dac0ed8SDimitry Andric 	int i;
9530dac0ed8SDimitry Andric 
9547c8b268aSBrooks Davis 	if (argcount >= argmax)
9557c8b268aSBrooks Davis 		moreargs();
9560dac0ed8SDimitry Andric 
9570dac0ed8SDimitry Andric 	/* Move up existing arguments */
958ade215fdSDimitry Andric 	for (i = argcount - 1; i >= place; i--)
9590dac0ed8SDimitry Andric 		arglist[i + 1] = arglist[i];
9600dac0ed8SDimitry Andric 
9618cb6926eSCraig Rodrigues 	arglist[place] = xstrdup(cp);
9620dac0ed8SDimitry Andric 	argcount++;
963ff49530fSBill Paul }
964ff49530fSBill Paul 
965ff49530fSBill Paul /*
966ff49530fSBill Paul  * if input file is stdin and an output file is specified then complain
967ff49530fSBill Paul  * if the file already exists. Otherwise the file may get overwritten
968ff49530fSBill Paul  * If input file does not exist, exit with an error
969ff49530fSBill Paul  */
970ff49530fSBill Paul 
971ff49530fSBill Paul static void
972e390e3afSDavid Malone checkfiles(const char *infile, const char *outfile)
973ff49530fSBill Paul {
974ff49530fSBill Paul 
975ff49530fSBill Paul 	struct stat buf;
976ff49530fSBill Paul 
977ff49530fSBill Paul 	if (infile)		/* infile ! = NULL */
978ff49530fSBill Paul 		if (stat(infile, &buf) < 0)
979ff49530fSBill Paul 		{
9800e76f40dSPhilippe Charnier 			warn("%s", infile);
981ff49530fSBill Paul 			crash();
98280c7cc1cSPedro F. Giffuni 		}
983ff49530fSBill Paul 	if (outfile) {
984ff49530fSBill Paul 		if (stat(outfile, &buf) < 0)
985ff49530fSBill Paul 			return;	/* file does not exist */
986ff49530fSBill Paul 		else {
9870e76f40dSPhilippe Charnier 			warnx("file '%s' already exists and may be overwritten", outfile);
988ff49530fSBill Paul 			crash();
989ff49530fSBill Paul 		}
990ff49530fSBill Paul 	}
9914e115012SGarrett Wollman }
9924e115012SGarrett Wollman 
9934e115012SGarrett Wollman /*
9944e115012SGarrett Wollman  * Parse command line arguments
9954e115012SGarrett Wollman  */
996526195adSJordan K. Hubbard static int
997e390e3afSDavid Malone parseargs(int argc, const char *argv[], struct commandline *cmd)
9984e115012SGarrett Wollman {
9994e115012SGarrett Wollman 	int i;
10004e115012SGarrett Wollman 	int j;
1001ff49530fSBill Paul 	char c, ch;
10024e115012SGarrett Wollman 	char flag[(1 << 8 * sizeof (char))];
10034e115012SGarrett Wollman 	int nflags;
10044e115012SGarrett Wollman 
10054e115012SGarrett Wollman 	cmd->infile = cmd->outfile = NULL;
10064e115012SGarrett Wollman 	if (argc < 2) {
10074e115012SGarrett Wollman 		return (0);
10084e115012SGarrett Wollman 	}
1009ff49530fSBill Paul 	allfiles = 0;
10104e115012SGarrett Wollman 	flag['c'] = 0;
10114e115012SGarrett Wollman 	flag['h'] = 0;
10124e115012SGarrett Wollman 	flag['l'] = 0;
10134e115012SGarrett Wollman 	flag['m'] = 0;
1014ff49530fSBill Paul 	flag['o'] = 0;
1015ff49530fSBill Paul 	flag['s'] = 0;
1016ff49530fSBill Paul 	flag['n'] = 0;
1017ff49530fSBill Paul 	flag['t'] = 0;
1018ff49530fSBill Paul 	flag['S'] = 0;
1019ff49530fSBill Paul 	flag['C'] = 0;
1020ff49530fSBill Paul 	flag['M'] = 0;
1021ff49530fSBill Paul 
10224e115012SGarrett Wollman 	for (i = 1; i < argc; i++) {
10234e115012SGarrett Wollman 		if (argv[i][0] != '-') {
10244e115012SGarrett Wollman 			if (cmd->infile) {
10250e76f40dSPhilippe Charnier 				warnx("cannot specify more than one input file");
10264e115012SGarrett Wollman 				return (0);
10274e115012SGarrett Wollman 			}
10284e115012SGarrett Wollman 			cmd->infile = argv[i];
10294e115012SGarrett Wollman 		} else {
10304e115012SGarrett Wollman 			for (j = 1; argv[i][j] != 0; j++) {
10314e115012SGarrett Wollman 				c = argv[i][j];
10324e115012SGarrett Wollman 				switch (c) {
1033ff49530fSBill Paul 				case 'a':
1034ff49530fSBill Paul 					allfiles = 1;
1035ff49530fSBill Paul 					break;
10364e115012SGarrett Wollman 				case 'c':
10374e115012SGarrett Wollman 				case 'h':
10384e115012SGarrett Wollman 				case 'l':
10394e115012SGarrett Wollman 				case 'm':
1040ff49530fSBill Paul 				case 't':
1041526195adSJordan K. Hubbard 					if (flag[(int)c]) {
10424e115012SGarrett Wollman 						return (0);
10434e115012SGarrett Wollman 					}
1044526195adSJordan K. Hubbard 					flag[(int)c] = 1;
10454e115012SGarrett Wollman 					break;
1046ff49530fSBill Paul 				case 'S':
1047ff49530fSBill Paul 					/*
1048ff49530fSBill Paul 					 * sample flag: Ss or Sc.
1049ff49530fSBill Paul 					 *  Ss means set flag['S'];
1050ff49530fSBill Paul 					 *  Sc means set flag['C'];
1051ff49530fSBill Paul 					 *  Sm means set flag['M'];
1052ff49530fSBill Paul 					 */
1053ff49530fSBill Paul 					ch = argv[i][++j]; /* get next char */
1054ff49530fSBill Paul 					if (ch == 's')
1055ff49530fSBill Paul 						ch = 'S';
1056ff49530fSBill Paul 					else if (ch == 'c')
1057ff49530fSBill Paul 						ch = 'C';
1058ff49530fSBill Paul 					else if (ch == 'm')
1059ff49530fSBill Paul 						ch = 'M';
1060ff49530fSBill Paul 					else
1061ff49530fSBill Paul 						return (0);
1062ff49530fSBill Paul 
1063526195adSJordan K. Hubbard 					if (flag[(int)ch]) {
1064ff49530fSBill Paul 						return (0);
1065ff49530fSBill Paul 					}
1066526195adSJordan K. Hubbard 					flag[(int)ch] = 1;
1067ff49530fSBill Paul 					break;
1068ff49530fSBill Paul 				case 'C': /* ANSI C syntax */
1069ff49530fSBill Paul 					ch = argv[i][j+1]; /* get next char */
1070ff49530fSBill Paul 
1071ff49530fSBill Paul 					if (ch != 'C')
1072ff49530fSBill Paul 						break;
1073ff49530fSBill Paul 					CCflag = 1;
1074ff49530fSBill Paul 					break;
1075ff49530fSBill Paul 				case 'b':
1076ff49530fSBill Paul 					/*
1077ff49530fSBill Paul 					 *  Turn TIRPC flag off for
1078ff49530fSBill Paul 					 *  generating backward compatible
1079ff49530fSBill Paul 					 *  code
1080ff49530fSBill Paul 					 */
1081ff49530fSBill Paul 					tirpcflag = 0;
1082ff49530fSBill Paul 					break;
1083ff49530fSBill Paul 
1084ff49530fSBill Paul 				case 'I':
1085ff49530fSBill Paul 					inetdflag = 1;
1086ff49530fSBill Paul 					break;
1087ff49530fSBill Paul 				case 'N':
1088ff49530fSBill Paul 					newstyle = 1;
1089ff49530fSBill Paul 					break;
1090ff49530fSBill Paul 				case 'L':
1091ff49530fSBill Paul 					logflag = 1;
1092ff49530fSBill Paul 					break;
109340ad8885SAlfred Perlstein 				case 'P':
109440ad8885SAlfred Perlstein 					pmflag = 1;
109540ad8885SAlfred Perlstein 					break;
1096ff49530fSBill Paul 				case 'K':
1097ff49530fSBill Paul 					if (++i == argc) {
1098ff49530fSBill Paul 						return (0);
1099ff49530fSBill Paul 					}
1100ff49530fSBill Paul 					svcclosetime = argv[i];
1101ff49530fSBill Paul 					goto nextarg;
1102ff49530fSBill Paul 				case 'T':
1103ff49530fSBill Paul 					tblflag = 1;
1104ff49530fSBill Paul 					break;
1105ff49530fSBill Paul 				case 'M':
1106ff49530fSBill Paul 					mtflag = 1;
1107ff49530fSBill Paul 					break;
1108ff49530fSBill Paul 				case 'i' :
1109ff49530fSBill Paul 					if (++i == argc) {
1110ff49530fSBill Paul 						return (0);
1111ff49530fSBill Paul 					}
1112122562cdSStefan Farfeleder 					inline_size = atoi(argv[i]);
1113ff49530fSBill Paul 					goto nextarg;
1114ff49530fSBill Paul 				case 'n':
11154e115012SGarrett Wollman 				case 'o':
11164e115012SGarrett Wollman 				case 's':
11174e115012SGarrett Wollman 					if (argv[i][j - 1] != '-' ||
11184e115012SGarrett Wollman 					    argv[i][j + 1] != 0) {
11194e115012SGarrett Wollman 						return (0);
11204e115012SGarrett Wollman 					}
1121526195adSJordan K. Hubbard 					flag[(int)c] = 1;
11224e115012SGarrett Wollman 					if (++i == argc) {
11234e115012SGarrett Wollman 						return (0);
11244e115012SGarrett Wollman 					}
1125ff49530fSBill Paul 					if (c == 'o') {
11264e115012SGarrett Wollman 						if (cmd->outfile) {
11274e115012SGarrett Wollman 							return (0);
11284e115012SGarrett Wollman 						}
11294e115012SGarrett Wollman 						cmd->outfile = argv[i];
11304e115012SGarrett Wollman 					}
11314e115012SGarrett Wollman 					goto nextarg;
1132ff49530fSBill Paul 				case 'D':
1133ff49530fSBill Paul 					if (argv[i][j - 1] != '-') {
1134ff49530fSBill Paul 						return (0);
1135ff49530fSBill Paul 					}
1136ff49530fSBill Paul 					(void) addarg(argv[i]);
1137ff49530fSBill Paul 					goto nextarg;
1138ff49530fSBill Paul 				case 'Y':
1139ff49530fSBill Paul 					if (++i == argc) {
1140ff49530fSBill Paul 						return (0);
1141ff49530fSBill Paul 					}
1142faabfb8aSDimitry Andric 					if (strlcpy(pathbuf, argv[i],
1143faabfb8aSDimitry Andric 					    sizeof(pathbuf)) >= sizeof(pathbuf)
1144faabfb8aSDimitry Andric 					    || strlcat(pathbuf, "/cpp",
1145faabfb8aSDimitry Andric 					    sizeof(pathbuf)) >=
1146faabfb8aSDimitry Andric 					    sizeof(pathbuf)) {
1147a0b13740SKris Kennaway 						warnx("argument too long");
1148a0b13740SKris Kennaway 						return (0);
1149a0b13740SKris Kennaway 					}
1150ff49530fSBill Paul 					CPP = pathbuf;
1151ff49530fSBill Paul 					goto nextarg;
1152ff49530fSBill Paul 
1153ff49530fSBill Paul 
11544e115012SGarrett Wollman 
11554e115012SGarrett Wollman 				default:
11564e115012SGarrett Wollman 					return (0);
11574e115012SGarrett Wollman 				}
11584e115012SGarrett Wollman 			}
11594e115012SGarrett Wollman 		nextarg:
11604e115012SGarrett Wollman 			;
11614e115012SGarrett Wollman 		}
11624e115012SGarrett Wollman 	}
1163ff49530fSBill Paul 
11644e115012SGarrett Wollman 	cmd->cflag = flag['c'];
11654e115012SGarrett Wollman 	cmd->hflag = flag['h'];
11664e115012SGarrett Wollman 	cmd->lflag = flag['l'];
11674e115012SGarrett Wollman 	cmd->mflag = flag['m'];
1168ff49530fSBill Paul 	cmd->nflag = flag['n'];
1169ff49530fSBill Paul 	cmd->sflag = flag['s'];
1170ff49530fSBill Paul 	cmd->tflag = flag['t'];
1171ff49530fSBill Paul 	cmd->Ssflag = flag['S'];
1172ff49530fSBill Paul 	cmd->Scflag = flag['C'];
1173ff49530fSBill Paul 	cmd->makefileflag = flag['M'];
1174ff49530fSBill Paul 
1175ff49530fSBill Paul 	if (tirpcflag) {
117640ad8885SAlfred Perlstein 		if (inetdflag)
117740ad8885SAlfred Perlstein 			pmflag = 0;
1178ff49530fSBill Paul 		if ((inetdflag && cmd->nflag)) {
1179ff49530fSBill Paul 			/* netid not allowed with inetdflag */
11800e76f40dSPhilippe Charnier 			warnx("cannot use netid flag with inetd flag");
1181ff49530fSBill Paul 			return (0);
1182ff49530fSBill Paul 		}
1183ff49530fSBill Paul 	} else {		/* 4.1 mode */
1184ff49530fSBill Paul 		pmflag = 0;	/* set pmflag only in tirpcmode */
1185ff49530fSBill Paul 		if (cmd->nflag) { /* netid needs TIRPC */
11860e76f40dSPhilippe Charnier 			warnx("cannot use netid flag without TIRPC");
1187ff49530fSBill Paul 			return (0);
1188ff49530fSBill Paul 		}
1189ff49530fSBill Paul 	}
1190ff49530fSBill Paul 
1191ff49530fSBill Paul 	if (newstyle && (tblflag || cmd->tflag)) {
11920e76f40dSPhilippe Charnier 		warnx("cannot use table flags with newstyle");
1193ff49530fSBill Paul 		return (0);
1194ff49530fSBill Paul 	}
1195ff49530fSBill Paul 
1196ff49530fSBill Paul 	/* check no conflicts with file generation flags */
1197ff49530fSBill Paul 	nflags = cmd->cflag + cmd->hflag + cmd->lflag + cmd->mflag +
1198ff49530fSBill Paul 		cmd->sflag + cmd->nflag + cmd->tflag + cmd->Ssflag +
1199ff49530fSBill Paul 			cmd->Scflag + cmd->makefileflag;
1200ff49530fSBill Paul 
12014e115012SGarrett Wollman 	if (nflags == 0) {
12024e115012SGarrett Wollman 		if (cmd->outfile != NULL || cmd->infile == NULL) {
12034e115012SGarrett Wollman 			return (0);
12044e115012SGarrett Wollman 		}
1205ff49530fSBill Paul 	} else if (cmd->infile == NULL &&
1206ff49530fSBill Paul 	    (cmd->Ssflag || cmd->Scflag || cmd->makefileflag)) {
12070e76f40dSPhilippe Charnier 		warnx("\"infile\" is required for template generation flags");
1208ff49530fSBill Paul 		return (0);
1209ff49530fSBill Paul 	} if (nflags > 1) {
12100e76f40dSPhilippe Charnier 		warnx("cannot have more than one file generation flag");
12114e115012SGarrett Wollman 		return (0);
12124e115012SGarrett Wollman 	}
12134e115012SGarrett Wollman 	return (1);
12144e115012SGarrett Wollman }
1215ff49530fSBill Paul 
1216526195adSJordan K. Hubbard static void
1217ef636796SEd Schouten usage(void)
1218ff49530fSBill Paul {
12190e76f40dSPhilippe Charnier 	f_print(stderr, "%s\n%s\n%s\n%s\n%s\n",
12200e76f40dSPhilippe Charnier 		"usage: rpcgen infile",
12210e76f40dSPhilippe Charnier 		"       rpcgen [-abCLNTM] [-Dname[=value]] [-i size]\
122240ad8885SAlfred Perlstein [-I -P [-K seconds]] [-Y path] infile",
12230e76f40dSPhilippe Charnier 		"       rpcgen [-c | -h | -l | -m | -t | -Sc | -Ss | -Sm]\
12240e76f40dSPhilippe Charnier [-o outfile] [infile]",
12250e76f40dSPhilippe Charnier 		"       rpcgen [-s nettype]* [-o outfile] [infile]",
12260e76f40dSPhilippe Charnier 		"       rpcgen [-n netid]* [-o outfile] [infile]");
1227ff49530fSBill Paul 	options_usage();
1228ff49530fSBill Paul 	exit(1);
1229ff49530fSBill Paul }
1230ff49530fSBill Paul 
1231526195adSJordan K. Hubbard static void
1232ef636796SEd Schouten options_usage(void)
1233ff49530fSBill Paul {
1234ff49530fSBill Paul 	f_print(stderr, "options:\n");
1235ff49530fSBill Paul 	f_print(stderr, "-a\t\tgenerate all files, including samples\n");
1236ff49530fSBill Paul 	f_print(stderr, "-b\t\tbackward compatibility mode (generates code \
123740ad8885SAlfred Perlstein for FreeBSD 4.X)\n");
1238ff49530fSBill Paul 	f_print(stderr, "-c\t\tgenerate XDR routines\n");
1239ff49530fSBill Paul 	f_print(stderr, "-C\t\tANSI C mode\n");
1240ff49530fSBill Paul 	f_print(stderr, "-Dname[=value]\tdefine a symbol (same as #define)\n");
1241ff49530fSBill Paul 	f_print(stderr, "-h\t\tgenerate header file\n");
1242ff49530fSBill Paul 	f_print(stderr, "-i size\t\tsize at which to start generating\
1243ff49530fSBill Paul inline code\n");
124440ad8885SAlfred Perlstein 	f_print(stderr, "-I\t\tgenerate code for inetd support in server\n");
1245ff49530fSBill Paul 	f_print(stderr, "-K seconds\tserver exits after K seconds of\
1246ff49530fSBill Paul inactivity\n");
1247ff49530fSBill Paul 	f_print(stderr, "-l\t\tgenerate client side stubs\n");
1248ff49530fSBill Paul 	f_print(stderr, "-L\t\tserver errors will be printed to syslog\n");
1249ff49530fSBill Paul 	f_print(stderr, "-m\t\tgenerate server side stubs\n");
1250ff49530fSBill Paul 	f_print(stderr, "-M\t\tgenerate MT-safe code\n");
1251ff49530fSBill Paul 	f_print(stderr, "-n netid\tgenerate server code that supports\
1252ff49530fSBill Paul named netid\n");
1253ff49530fSBill Paul 	f_print(stderr, "-N\t\tsupports multiple arguments and\
1254ff49530fSBill Paul call-by-value\n");
1255ff49530fSBill Paul 	f_print(stderr, "-o outfile\tname of the output file\n");
125640ad8885SAlfred Perlstein 	f_print(stderr, "-P\t\tgenerate code for port monitoring support in server\n");
1257ff49530fSBill Paul 	f_print(stderr, "-s nettype\tgenerate server code that supports named\
1258ff49530fSBill Paul nettype\n");
1259ff49530fSBill Paul 	f_print(stderr, "-Sc\t\tgenerate sample client code that uses remote\
1260ff49530fSBill Paul procedures\n");
1261ff49530fSBill Paul 	f_print(stderr, "-Ss\t\tgenerate sample server code that defines\
1262ff49530fSBill Paul remote procedures\n");
1263ff49530fSBill Paul 	f_print(stderr, "-Sm \t\tgenerate makefile template \n");
1264ff49530fSBill Paul 
1265ff49530fSBill Paul 	f_print(stderr, "-t\t\tgenerate RPC dispatch table\n");
1266ff49530fSBill Paul 	f_print(stderr, "-T\t\tgenerate code to support RPC dispatch tables\n");
1267ff49530fSBill Paul 	f_print(stderr, "-Y path\t\tpath where cpp is found\n");
1268ff49530fSBill Paul 	exit(1);
1269ff49530fSBill Paul }
1270