xref: /freebsd/usr.bin/rpcgen/rpc_main.c (revision 5b31cc94b10d4bb7109c6b27940a0fc76a44a331)
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 
314e115012SGarrett Wollman /*
324e115012SGarrett Wollman  * rpc_main.c, Top level of the RPC protocol compiler.
334e115012SGarrett Wollman  * Copyright (C) 1987, Sun Microsystems, Inc.
344e115012SGarrett Wollman  */
354e115012SGarrett Wollman 
360e76f40dSPhilippe Charnier #include <err.h>
370e76f40dSPhilippe Charnier #include <ctype.h>
38*587458b7SJessica Clarke #include <stdbool.h>
394e115012SGarrett Wollman #include <stdio.h>
40ff49530fSBill Paul #include <string.h>
41ff49530fSBill Paul #include <unistd.h>
42ff49530fSBill Paul #include <sys/types.h>
43ff49530fSBill Paul #include <sys/param.h>
444e115012SGarrett Wollman #include <sys/file.h>
45ff49530fSBill Paul #include <sys/stat.h>
464e115012SGarrett Wollman #include "rpc_parse.h"
474e115012SGarrett Wollman #include "rpc_scan.h"
48d0cc804bSStefan Farfeleder #include "rpc_util.h"
494e115012SGarrett Wollman 
50e390e3afSDavid Malone static void c_output(const char *, const char *, int, const char *);
51e390e3afSDavid Malone static void h_output(const char *, const char *, int, const char *, int);
52e390e3afSDavid Malone static void l_output(const char *, const char *, int, const char *);
53e390e3afSDavid Malone static void t_output(const char *, const char *, int, const char *);
54e390e3afSDavid Malone static void clnt_output(const char *, const char *, int, const char * );
55e390e3afSDavid Malone static char *generate_guard(const char *);
56e390e3afSDavid Malone static void c_initialize(void);
574e115012SGarrett Wollman 
581a7ac2bdSAlfonso Gregory static void usage(void) __dead2;
59d3cb5dedSWarner Losh static void options_usage(void);
60e390e3afSDavid Malone static int do_registers(int, const char **);
61e390e3afSDavid Malone static int parseargs(int, const char **, struct commandline *);
62e390e3afSDavid Malone static void svc_output(const char *, const char *, int, const char *);
63d3cb5dedSWarner Losh static void mkfile_output(struct commandline *);
64e390e3afSDavid Malone static void s_output(int, const char **, const char *, const char *, int, const char *, int, int);
65ff49530fSBill Paul 
66ff49530fSBill Paul #define	EXTEND	1		/* alias for TRUE */
67ff49530fSBill Paul #define	DONT_EXTEND	0		/* alias for FALSE */
68ff49530fSBill Paul 
69e390e3afSDavid Malone static const char *svcclosetime = "120";
700dac0ed8SDimitry Andric static const char *CPP = NULL;
71e390e3afSDavid Malone static const char CPPFLAGS[] = "-C";
72ff49530fSBill Paul static char pathbuf[MAXPATHLEN + 1];
73e390e3afSDavid Malone static const char *allv[] = {
744e115012SGarrett Wollman 	"rpcgen", "-s", "udp", "-s", "tcp",
754e115012SGarrett Wollman };
764b61b26bSMarcelo Araujo static int allc = nitems(allv);
77e390e3afSDavid Malone static const char *allnv[] = {
78ff49530fSBill Paul 	"rpcgen", "-s", "netpath",
79ff49530fSBill Paul };
804b61b26bSMarcelo Araujo static int allnc = nitems(allnv);
81ff49530fSBill Paul 
82ff49530fSBill Paul /*
83ff49530fSBill Paul  * machinations for handling expanding argument list
84ff49530fSBill Paul  */
85e390e3afSDavid Malone static void addarg(const char *);	/* add another argument to the list */
860dac0ed8SDimitry Andric static void insarg(int, const char *);	/* insert arg at specified location */
87e390e3afSDavid Malone static void checkfiles(const char *, const char *);
88e390e3afSDavid Malone 					/* check if out file already exists */
894e115012SGarrett Wollman 
907c8b268aSBrooks Davis static char **arglist;
917c8b268aSBrooks Davis static int argcount = 0;
927c8b268aSBrooks Davis static int argmax = 0;
93ff49530fSBill Paul 
94ff49530fSBill Paul int nonfatalerrors;	/* errors */
9540ad8885SAlfred Perlstein int inetdflag = 0;	/* Support for inetd is disabled by default, use -I */
9640ad8885SAlfred Perlstein int pmflag = 0;		/* Support for port monitors is disabled by default */
9740ad8885SAlfred Perlstein int tirpc_socket = 1;	/* TI-RPC on socket, no TLI library */
98ff49530fSBill Paul int logflag;		/* Use syslog instead of fprintf for errors */
99ff49530fSBill Paul int tblflag;		/* Support for dispatch table file */
100ff49530fSBill Paul int mtflag = 0;		/* Support for MT */
10140ad8885SAlfred Perlstein 
102ff49530fSBill Paul #define INLINE 0
103ff49530fSBill Paul /* length at which to start doing an inline */
104ff49530fSBill Paul 
105122562cdSStefan Farfeleder int inline_size = INLINE;
106ff49530fSBill Paul /*
107ff49530fSBill Paul  * Length at which to start doing an inline. INLINE = default
108ff49530fSBill Paul  * if 0, no xdr_inline code
109ff49530fSBill Paul  */
110ff49530fSBill Paul 
111ff49530fSBill Paul int indefinitewait;	/* If started by port monitors, hang till it wants */
112ff49530fSBill Paul int exitnow;		/* If started by port monitors, exit after the call */
113ff49530fSBill Paul int timerflag;		/* TRUE if !indefinite && !exitnow */
114ff49530fSBill Paul int newstyle;		/* newstyle of passing arguments (by value) */
115ff49530fSBill Paul int CCflag = 0;		/* C++ files */
116ff49530fSBill Paul static int allfiles;   /* generate all files */
117ff49530fSBill Paul int tirpcflag = 1;    /* generating code for tirpc, by default */
118ff49530fSBill Paul xdrfunc *xdrfunc_head = NULL; /* xdr function list */
119ff49530fSBill Paul xdrfunc *xdrfunc_tail = NULL; /* xdr function list */
120ff49530fSBill Paul pid_t childpid;
121ff49530fSBill Paul 
1224e115012SGarrett Wollman 
123526195adSJordan K. Hubbard int
main(int argc,const char * argv[])124e390e3afSDavid Malone main(int argc, const char *argv[])
1254e115012SGarrett Wollman {
1264e115012SGarrett Wollman 	struct commandline cmd;
1274e115012SGarrett Wollman 
128ff49530fSBill Paul 	(void) memset((char *)&cmd, 0, sizeof (struct commandline));
129ff49530fSBill Paul 	if (!parseargs(argc, argv, &cmd))
130ff49530fSBill Paul 		usage();
131ff49530fSBill Paul 	/*
132ff49530fSBill Paul 	 * Only the client and server side stubs are likely to be customized,
133ff49530fSBill Paul 	 *  so in that case only, check if the outfile exists, and if so,
134ff49530fSBill Paul 	 *  print an error message and exit.
135ff49530fSBill Paul 	 */
136ff49530fSBill Paul 	if (cmd.Ssflag || cmd.Scflag || cmd.makefileflag) {
137ff49530fSBill Paul 		checkfiles(cmd.infile, cmd.outfile);
1384e115012SGarrett Wollman 	}
139ff49530fSBill Paul 	else
140ff49530fSBill Paul 		checkfiles(cmd.infile, NULL);
141ff49530fSBill Paul 
1424e115012SGarrett Wollman 	if (cmd.cflag) {
143ff49530fSBill Paul 		c_output(cmd.infile, "-DRPC_XDR", DONT_EXTEND, cmd.outfile);
1444e115012SGarrett Wollman 	} else if (cmd.hflag) {
145ec06b5e8SStefan Farfeleder 		h_output(cmd.infile, "-DRPC_HDR", DONT_EXTEND, cmd.outfile,
146ec06b5e8SStefan Farfeleder 		    cmd.hflag);
1474e115012SGarrett Wollman 	} else if (cmd.lflag) {
148ff49530fSBill Paul 		l_output(cmd.infile, "-DRPC_CLNT", DONT_EXTEND, cmd.outfile);
149ff49530fSBill Paul 	} else if (cmd.sflag || cmd.mflag || (cmd.nflag)) {
150ff49530fSBill Paul 		s_output(argc, argv, cmd.infile, "-DRPC_SVC", DONT_EXTEND,
151ff49530fSBill Paul 			cmd.outfile, cmd.mflag, cmd.nflag);
152ff49530fSBill Paul 	} else if (cmd.tflag) {
153ff49530fSBill Paul 		t_output(cmd.infile, "-DRPC_TBL", DONT_EXTEND, cmd.outfile);
154ff49530fSBill Paul 	} else if  (cmd.Ssflag) {
155ff49530fSBill Paul 		svc_output(cmd.infile, "-DRPC_SERVER", DONT_EXTEND,
156ff49530fSBill Paul 			cmd.outfile);
157ff49530fSBill Paul 	} else if (cmd.Scflag) {
158ff49530fSBill Paul 		clnt_output(cmd.infile, "-DRPC_CLIENT", DONT_EXTEND,
159ff49530fSBill Paul 			    cmd.outfile);
160ff49530fSBill Paul 	} else if (cmd.makefileflag) {
161ff49530fSBill Paul 		mkfile_output(&cmd);
1624e115012SGarrett Wollman 	} else {
163ff49530fSBill Paul 		/* the rescans are required, since cpp may effect input */
1644e115012SGarrett Wollman 		c_output(cmd.infile, "-DRPC_XDR", EXTEND, "_xdr.c");
1654e115012SGarrett Wollman 		reinitialize();
166ec06b5e8SStefan Farfeleder 		h_output(cmd.infile, "-DRPC_HDR", EXTEND, ".h", cmd.hflag);
1674e115012SGarrett Wollman 		reinitialize();
1684e115012SGarrett Wollman 		l_output(cmd.infile, "-DRPC_CLNT", EXTEND, "_clnt.c");
1694e115012SGarrett Wollman 		reinitialize();
170ff49530fSBill Paul 		if (inetdflag || !tirpcflag)
1714e115012SGarrett Wollman 			s_output(allc, allv, cmd.infile, "-DRPC_SVC", EXTEND,
172ff49530fSBill Paul 			"_svc.c", cmd.mflag, cmd.nflag);
173ff49530fSBill Paul 		else
174ff49530fSBill Paul 			s_output(allnc, allnv, cmd.infile, "-DRPC_SVC",
175ff49530fSBill Paul 				EXTEND, "_svc.c", cmd.mflag, cmd.nflag);
176ff49530fSBill Paul 		if (tblflag) {
177ff49530fSBill Paul 			reinitialize();
178ff49530fSBill Paul 			t_output(cmd.infile, "-DRPC_TBL", EXTEND, "_tbl.i");
1794e115012SGarrett Wollman 		}
1804e115012SGarrett Wollman 
181ff49530fSBill Paul 		if (allfiles) {
182ff49530fSBill Paul 			reinitialize();
183ff49530fSBill Paul 			svc_output(cmd.infile, "-DRPC_SERVER", EXTEND,
184ff49530fSBill Paul 				"_server.c");
185ff49530fSBill Paul 			reinitialize();
186ff49530fSBill Paul 			clnt_output(cmd.infile, "-DRPC_CLIENT", EXTEND,
187ff49530fSBill Paul 				"_client.c");
188ff49530fSBill Paul 
189ff49530fSBill Paul 		}
190ff49530fSBill Paul 		if (allfiles || (cmd.makefileflag == 1)){
191ff49530fSBill Paul 			reinitialize();
192ff49530fSBill Paul 			mkfile_output(&cmd);
193ff49530fSBill Paul 		}
194ff49530fSBill Paul 
195ff49530fSBill Paul 	}
196ff49530fSBill Paul 	exit(nonfatalerrors);
197ff49530fSBill Paul 	/* NOTREACHED */
198ff49530fSBill Paul }
199ff49530fSBill Paul 
200ff49530fSBill Paul 
2014e115012SGarrett Wollman /*
202ff49530fSBill Paul  * add extension to filename
2034e115012SGarrett Wollman  */
2044e115012SGarrett Wollman static char *
extendfile(const char * path,const char * ext)205e390e3afSDavid Malone extendfile(const char *path, const char *ext)
2064e115012SGarrett Wollman {
2074e115012SGarrett Wollman 	char *res;
208e390e3afSDavid Malone 	const char *p;
209e390e3afSDavid Malone 	const char *file;
2104e115012SGarrett Wollman 
211b3608ae1SEd Schouten 	if ((file = strrchr(path, '/')) == NULL)
2125ec07232SNate Williams 		file = path;
2135ec07232SNate Williams 	else
2145ec07232SNate Williams 		file++;
21575863a6dSPhilippe Charnier 	res = xmalloc(strlen(file) + strlen(ext) + 1);
216ff49530fSBill Paul 	p = strrchr(file, '.');
2174e115012SGarrett Wollman 	if (p == NULL) {
2184e115012SGarrett Wollman 		p = file + strlen(file);
2194e115012SGarrett Wollman 	}
2204e115012SGarrett Wollman 	(void) strcpy(res, file);
2214e115012SGarrett Wollman 	(void) strcpy(res + (p - file), ext);
2224e115012SGarrett Wollman 	return (res);
2234e115012SGarrett Wollman }
2244e115012SGarrett Wollman 
2254e115012SGarrett Wollman /*
2264e115012SGarrett Wollman  * Open output file with given extension
2274e115012SGarrett Wollman  */
228526195adSJordan K. Hubbard static void
open_output(const char * infile,const char * outfile)229e390e3afSDavid Malone open_output(const char *infile, const char *outfile)
2304e115012SGarrett Wollman {
231ff49530fSBill Paul 
2324e115012SGarrett Wollman 	if (outfile == NULL) {
2334e115012SGarrett Wollman 		fout = stdout;
2344e115012SGarrett Wollman 		return;
2354e115012SGarrett Wollman 	}
236ff49530fSBill Paul 
2374e115012SGarrett Wollman 	if (infile != NULL && streq(outfile, infile)) {
2380e76f40dSPhilippe Charnier 		warnx("%s already exists. No output generated", infile);
2394e115012SGarrett Wollman 		crash();
2404e115012SGarrett Wollman 	}
2414e115012SGarrett Wollman 	fout = fopen(outfile, "w");
2424e115012SGarrett Wollman 	if (fout == NULL) {
2430e76f40dSPhilippe Charnier 		warn("unable to open %s", outfile);
2444e115012SGarrett Wollman 		crash();
2454e115012SGarrett Wollman 	}
2464e115012SGarrett Wollman 	record_open(outfile);
247ff49530fSBill Paul 
248526195adSJordan K. Hubbard 	return;
249ff49530fSBill Paul }
250ff49530fSBill Paul 
251526195adSJordan K. Hubbard static void
add_warning(void)252e390e3afSDavid Malone add_warning(void)
253ff49530fSBill Paul {
254ff49530fSBill Paul 	f_print(fout, "/*\n");
255ff49530fSBill Paul 	f_print(fout, " * Please do not edit this file.\n");
256ff49530fSBill Paul 	f_print(fout, " * It was generated using rpcgen.\n");
257ff49530fSBill Paul 	f_print(fout, " */\n\n");
258ff49530fSBill Paul }
259ff49530fSBill Paul 
2600dac0ed8SDimitry Andric /* prepend C-preprocessor and flags before arguments */
261e390e3afSDavid Malone static void
prepend_cpp(void)2620dac0ed8SDimitry Andric prepend_cpp(void)
263ff49530fSBill Paul {
264*587458b7SJessica Clarke 	int idx = 0, quoted;
265*587458b7SJessica Clarke 	const char *var, *s;
266*587458b7SJessica Clarke 	char *dupvar, *t, *word;
267ff49530fSBill Paul 
2680dac0ed8SDimitry Andric 	if (CPP != NULL)
2699d843ba3SJessica Clarke 		insarg(idx++, CPP);
2700dac0ed8SDimitry Andric 	else if ((var = getenv("RPCGEN_CPP")) == NULL)
2719d843ba3SJessica Clarke 		insarg(idx++, "/usr/bin/cpp");
2720dac0ed8SDimitry Andric 	else {
273*587458b7SJessica Clarke 		/*
274*587458b7SJessica Clarke 		 * Parse command line like a shell (but only handle whitespace,
275*587458b7SJessica Clarke 		 * quotes and backslash).
276*587458b7SJessica Clarke 		 */
277*587458b7SJessica Clarke 		dupvar = malloc(strlen(var) + 1);
278*587458b7SJessica Clarke 		quoted = 0;
279*587458b7SJessica Clarke 		word = NULL;
280*587458b7SJessica Clarke 		for (s = var, t = dupvar; *s; ++s) {
281*587458b7SJessica Clarke 			switch (quoted) {
282*587458b7SJessica Clarke 			/* Unquoted */
283*587458b7SJessica Clarke 			case 0:
284*587458b7SJessica Clarke 				switch (*s) {
285*587458b7SJessica Clarke 				case ' ':
286*587458b7SJessica Clarke 				case '\t':
287*587458b7SJessica Clarke 				case '\n':
288*587458b7SJessica Clarke 					if (word != NULL) {
289*587458b7SJessica Clarke 						*t++ = '\0';
290*587458b7SJessica Clarke 						insarg(idx++, word);
291*587458b7SJessica Clarke 						word = NULL;
292*587458b7SJessica Clarke 					}
293*587458b7SJessica Clarke 					break;
294*587458b7SJessica Clarke 				case '\'':
295*587458b7SJessica Clarke 					if (word == NULL)
296*587458b7SJessica Clarke 						word = t;
297*587458b7SJessica Clarke 					quoted = 1;
298*587458b7SJessica Clarke 					break;
299*587458b7SJessica Clarke 				case '"':
300*587458b7SJessica Clarke 					if (word == NULL)
301*587458b7SJessica Clarke 						word = t;
302*587458b7SJessica Clarke 					quoted = 2;
303*587458b7SJessica Clarke 					break;
304*587458b7SJessica Clarke 				case '\\':
305*587458b7SJessica Clarke 					switch (*(s + 1)) {
306*587458b7SJessica Clarke 					case '\0':
307*587458b7SJessica Clarke 						break;
308*587458b7SJessica Clarke 					case '\n':
309*587458b7SJessica Clarke 						++s;
310*587458b7SJessica Clarke 						continue;
311*587458b7SJessica Clarke 					default:
312*587458b7SJessica Clarke 						++s;
313*587458b7SJessica Clarke 						break;
314*587458b7SJessica Clarke 					}
315*587458b7SJessica Clarke 					/* FALLTHROUGH */
316*587458b7SJessica Clarke 				default:
317*587458b7SJessica Clarke 					if (word == NULL)
318*587458b7SJessica Clarke 						word = t;
319*587458b7SJessica Clarke 					*t++ = *s;
320*587458b7SJessica Clarke 					break;
321*587458b7SJessica Clarke 				}
322*587458b7SJessica Clarke 				break;
323*587458b7SJessica Clarke 
324*587458b7SJessica Clarke 			/* Single-quoted */
325*587458b7SJessica Clarke 			case 1:
326*587458b7SJessica Clarke 				switch (*s) {
327*587458b7SJessica Clarke 				case '\'':
328*587458b7SJessica Clarke 					quoted = 0;
329*587458b7SJessica Clarke 					break;
330*587458b7SJessica Clarke 				default:
331*587458b7SJessica Clarke 					*t++ = *s;
332*587458b7SJessica Clarke 					break;
333*587458b7SJessica Clarke 				}
334*587458b7SJessica Clarke 				break;
335*587458b7SJessica Clarke 
336*587458b7SJessica Clarke 			/* Double-quoted */
337*587458b7SJessica Clarke 			case 2:
338*587458b7SJessica Clarke 				switch (*s) {
339*587458b7SJessica Clarke 				case '"':
340*587458b7SJessica Clarke 					quoted = 0;
341*587458b7SJessica Clarke 					break;
342*587458b7SJessica Clarke 				case '\\':
343*587458b7SJessica Clarke 					switch (*(s + 1)) {
344*587458b7SJessica Clarke 					case '\0':
345*587458b7SJessica Clarke 						break;
346*587458b7SJessica Clarke 					case '$':
347*587458b7SJessica Clarke 					case '`':
348*587458b7SJessica Clarke 					case '"':
349*587458b7SJessica Clarke 					case '\\':
350*587458b7SJessica Clarke 						++s;
351*587458b7SJessica Clarke 						break;
352*587458b7SJessica Clarke 					case '\n':
353*587458b7SJessica Clarke 						++s;
354*587458b7SJessica Clarke 						continue;
355*587458b7SJessica Clarke 					default:
356*587458b7SJessica Clarke 						break;
357*587458b7SJessica Clarke 					}
358*587458b7SJessica Clarke 					/* FALLTHROUGH */
359*587458b7SJessica Clarke 				default:
360*587458b7SJessica Clarke 					*t++ = *s;
361*587458b7SJessica Clarke 					break;
362*587458b7SJessica Clarke 				}
363*587458b7SJessica Clarke 				break;
364*587458b7SJessica Clarke 			}
365*587458b7SJessica Clarke 		}
366*587458b7SJessica Clarke 		if (quoted)
367*587458b7SJessica Clarke 			errx(1, "RPCGEN_CPP: unterminated %c",
368*587458b7SJessica Clarke 			    quoted == 1 ? '\'' : '"');
369*587458b7SJessica Clarke 		if (word != NULL) {
370*587458b7SJessica Clarke 			*t++ = '\0';
371*587458b7SJessica Clarke 			insarg(idx++, word);
3720dac0ed8SDimitry Andric 		}
3730dac0ed8SDimitry Andric 		free(dupvar);
3740dac0ed8SDimitry Andric 	}
3750dac0ed8SDimitry Andric 
3760dac0ed8SDimitry Andric 	insarg(idx, CPPFLAGS);
3774e115012SGarrett Wollman }
3784e115012SGarrett Wollman 
3794e115012SGarrett Wollman /*
3804e115012SGarrett Wollman  * Open input file with given define for C-preprocessor
3814e115012SGarrett Wollman  */
382526195adSJordan K. Hubbard static void
open_input(const char * infile,const char * define)383e390e3afSDavid Malone open_input(const char *infile, const char *define)
3844e115012SGarrett Wollman {
3854e115012SGarrett Wollman 	int pd[2];
3864e115012SGarrett Wollman 
3874e115012SGarrett Wollman 	infilename = (infile == NULL) ? "<stdin>" : infile;
3884e115012SGarrett Wollman 	(void) pipe(pd);
389ff49530fSBill Paul 	switch (childpid = fork()) {
3904e115012SGarrett Wollman 	case 0:
3910dac0ed8SDimitry Andric 		prepend_cpp();
392ff49530fSBill Paul 		addarg(define);
393ff49530fSBill Paul 		if (infile)
394ff49530fSBill Paul 			addarg(infile);
395ff49530fSBill Paul 		addarg((char *)NULL);
3964e115012SGarrett Wollman 		(void) close(1);
3974e115012SGarrett Wollman 		(void) dup2(pd[1], 1);
3984e115012SGarrett Wollman 		(void) close(pd[0]);
399005576f6SDimitry Andric 		execvp(arglist[0], arglist);
400005576f6SDimitry Andric 		err(1, "execvp %s", arglist[0]);
4014e115012SGarrett Wollman 	case -1:
40275863a6dSPhilippe Charnier 		err(1, "fork");
4034e115012SGarrett Wollman 	}
4044e115012SGarrett Wollman 	(void) close(pd[1]);
4054e115012SGarrett Wollman 	fin = fdopen(pd[0], "r");
4064e115012SGarrett Wollman 	if (fin == NULL) {
4070e76f40dSPhilippe Charnier 		warn("%s", infilename);
4084e115012SGarrett Wollman 		crash();
4094e115012SGarrett Wollman 	}
4104e115012SGarrett Wollman }
4114e115012SGarrett Wollman 
412ff49530fSBill Paul /* valid tirpc nettypes */
413e390e3afSDavid Malone static const char *valid_ti_nettypes[] =
414ff49530fSBill Paul {
415ff49530fSBill Paul 	"netpath",
416ff49530fSBill Paul 	"visible",
417ff49530fSBill Paul 	"circuit_v",
418ff49530fSBill Paul 	"datagram_v",
419ff49530fSBill Paul 	"circuit_n",
420ff49530fSBill Paul 	"datagram_n",
421ff49530fSBill Paul 	"udp",
422ff49530fSBill Paul 	"tcp",
423ff49530fSBill Paul 	"raw",
424ff49530fSBill Paul 	NULL
425ff49530fSBill Paul 	};
426ff49530fSBill Paul 
427ff49530fSBill Paul /* valid inetd nettypes */
428e390e3afSDavid Malone static const char *valid_i_nettypes[] =
429ff49530fSBill Paul {
430ff49530fSBill Paul 	"udp",
431ff49530fSBill Paul 	"tcp",
432ff49530fSBill Paul 	NULL
433ff49530fSBill Paul 	};
434ff49530fSBill Paul 
435e390e3afSDavid Malone static int
check_nettype(const char * name,const char * list_to_check[])436e390e3afSDavid Malone check_nettype(const char *name, const char *list_to_check[])
437ff49530fSBill Paul {
438ff49530fSBill Paul 	int i;
439ff49530fSBill Paul 	for (i = 0; list_to_check[i] != NULL; i++) {
440ff49530fSBill Paul 		if (strcmp(name, list_to_check[i]) == 0) {
441ff49530fSBill Paul 			return (1);
442ff49530fSBill Paul 		}
443ff49530fSBill Paul 	}
4440e76f40dSPhilippe Charnier 	warnx("illegal nettype :\'%s\'", name);
445ff49530fSBill Paul 	return (0);
446ff49530fSBill Paul }
447ff49530fSBill Paul 
448e390e3afSDavid Malone static const char *
file_name(const char * file,const char * ext)449e390e3afSDavid Malone file_name(const char *file, const char *ext)
450ff49530fSBill Paul {
451ff49530fSBill Paul 	char *temp;
452ff49530fSBill Paul 	temp = extendfile(file, ext);
453ff49530fSBill Paul 
454ff49530fSBill Paul 	if (access(temp, F_OK) != -1)
455ff49530fSBill Paul 		return (temp);
456ff49530fSBill Paul 	else
457e390e3afSDavid Malone 		return (" ");
458ff49530fSBill Paul 
459ff49530fSBill Paul }
460ff49530fSBill Paul 
461ff49530fSBill Paul 
462526195adSJordan K. Hubbard static void
c_output(const char * infile,const char * define,int extend,const char * outfile)463e390e3afSDavid Malone c_output(const char *infile, const char *define, int extend, const char *outfile)
4644e115012SGarrett Wollman {
4654e115012SGarrett Wollman 	definition *def;
4664e115012SGarrett Wollman 	char *include;
467e390e3afSDavid Malone 	const char *outfilename;
4684e115012SGarrett Wollman 	long tell;
4694e115012SGarrett Wollman 
470ff49530fSBill Paul 	c_initialize();
4714e115012SGarrett Wollman 	open_input(infile, define);
4724e115012SGarrett Wollman 	outfilename = extend ? extendfile(infile, outfile) : outfile;
4734e115012SGarrett Wollman 	open_output(infile, outfilename);
474ff49530fSBill Paul 	add_warning();
4754e115012SGarrett Wollman 	if (infile && (include = extendfile(infile, ".h"))) {
4764e115012SGarrett Wollman 		f_print(fout, "#include \"%s\"\n", include);
4774e115012SGarrett Wollman 		free(include);
478ff49530fSBill Paul 		/* .h file already contains rpc/rpc.h */
479ff49530fSBill Paul 	} else
480ff49530fSBill Paul 		f_print(fout, "#include <rpc/rpc.h>\n");
4814e115012SGarrett Wollman 	tell = ftell(fout);
482526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
4834e115012SGarrett Wollman 		emit(def);
4844e115012SGarrett Wollman 	}
4854e115012SGarrett Wollman 	if (extend && tell == ftell(fout)) {
4864e115012SGarrett Wollman 		(void) unlink(outfilename);
4874e115012SGarrett Wollman 	}
4884e115012SGarrett Wollman }
4894e115012SGarrett Wollman 
490ff49530fSBill Paul 
491526195adSJordan K. Hubbard void
c_initialize(void)492e390e3afSDavid Malone c_initialize(void)
493ff49530fSBill Paul {
494ff49530fSBill Paul 
495ff49530fSBill Paul 	/* add all the starting basic types */
496ff49530fSBill Paul 	add_type(1, "int");
497ff49530fSBill Paul 	add_type(1, "long");
498ff49530fSBill Paul 	add_type(1, "short");
499ff49530fSBill Paul 	add_type(1, "bool");
500ff49530fSBill Paul 	add_type(1, "u_int");
501ff49530fSBill Paul 	add_type(1, "u_long");
502ff49530fSBill Paul 	add_type(1, "u_short");
503ff49530fSBill Paul 
504ff49530fSBill Paul }
505ff49530fSBill Paul 
506bf70beceSEd Schouten static const char rpcgen_table_dcl[] = "struct rpcgen_table {\n\
507ff49530fSBill Paul 	char	*(*proc)(); \n\
508ff49530fSBill Paul 	xdrproc_t	xdr_arg; \n\
509ff49530fSBill Paul 	unsigned	len_arg; \n\
510ff49530fSBill Paul 	xdrproc_t	xdr_res; \n\
511ff49530fSBill Paul 	unsigned	len_res; \n\
512ff49530fSBill Paul }; \n";
513ff49530fSBill Paul 
514ff49530fSBill Paul 
515e390e3afSDavid Malone char *
generate_guard(const char * pathname)516e390e3afSDavid Malone generate_guard(const char *pathname)
517ff49530fSBill Paul {
518e390e3afSDavid Malone 	const char *filename;
519e390e3afSDavid Malone 	char *guard, *tmp, *stopat;
520ff49530fSBill Paul 
521ff49530fSBill Paul 	filename = strrchr(pathname, '/');  /* find last component */
5227dbab955SMarcelo Araujo 	filename = ((filename == NULL) ? pathname : filename+1);
52375863a6dSPhilippe Charnier 	guard = xstrdup(filename);
524080f4020SSean Kelly 	stopat = strrchr(guard, '.');
525080f4020SSean Kelly 
526080f4020SSean Kelly 	/*
527080f4020SSean Kelly 	 * Convert to a valid C macro name and make it upper case.
528080f4020SSean Kelly 	 * Map macro unfriendly characterss to '_'.
52910c546c4SSean Kelly 	 */
530080f4020SSean Kelly 	for (tmp = guard; *tmp != '\000'; ++tmp) {
531ff49530fSBill Paul 		if (islower(*tmp))
532ff49530fSBill Paul 			*tmp = toupper(*tmp);
533080f4020SSean Kelly 		else if (isupper(*tmp) || *tmp == '_')
53410c546c4SSean Kelly 			/* OK for C */;
53510c546c4SSean Kelly 		else if (tmp == guard)
53610c546c4SSean Kelly 			*tmp = '_';
53710c546c4SSean Kelly 		else if (isdigit(*tmp))
53810c546c4SSean Kelly 			/* OK for all but first character */;
539080f4020SSean Kelly 		else if (tmp == stopat) {
540080f4020SSean Kelly 			*tmp = '\0';
54110c546c4SSean Kelly 			break;
54210c546c4SSean Kelly 		} else
54310c546c4SSean Kelly 			*tmp = '_';
54410c546c4SSean Kelly 	}
545080f4020SSean Kelly 	/*
546080f4020SSean Kelly 	 * Can't have a '_' in front, because it'll end up being "__".
547080f4020SSean Kelly 	 * "__" macros shoudln't be used. So, remove all of the
548080f4020SSean Kelly 	 * '_' characters from the front.
54910c546c4SSean Kelly 	 */
550080f4020SSean Kelly 	if (*guard == '_') {
551080f4020SSean Kelly 		for (tmp = guard; *tmp == '_'; ++tmp)
552080f4020SSean Kelly 			;
553080f4020SSean Kelly 		strcpy(guard, tmp);
554ff49530fSBill Paul 	}
555074170fbSWarner Losh 	tmp = guard;
556ff49530fSBill Paul 	guard = extendfile(guard, "_H_RPCGEN");
557074170fbSWarner Losh 	free(tmp);
558ff49530fSBill Paul 	return (guard);
559ff49530fSBill Paul }
560ff49530fSBill Paul 
5614e115012SGarrett Wollman /*
5624e115012SGarrett Wollman  * Compile into an XDR header file
5634e115012SGarrett Wollman  */
564ff49530fSBill Paul 
565ff49530fSBill Paul 
566526195adSJordan K. Hubbard static void
h_output(const char * infile,const char * define,int extend,const char * outfile,int headeronly)567e390e3afSDavid Malone h_output(const char *infile, const char *define, int extend, const char *outfile, int headeronly)
5684e115012SGarrett Wollman {
5694e115012SGarrett Wollman 	definition *def;
570e390e3afSDavid Malone 	const char *outfilename;
5714e115012SGarrett Wollman 	long tell;
572e390e3afSDavid Malone 	const char *guard;
573ff49530fSBill Paul 	list *l;
574ff49530fSBill Paul 	xdrfunc *xdrfuncp;
575074170fbSWarner Losh 	void *tmp = NULL;
5764e115012SGarrett Wollman 
5774e115012SGarrett Wollman 	open_input(infile, define);
5784e115012SGarrett Wollman 	outfilename =  extend ? extendfile(infile, outfile) : outfile;
5794e115012SGarrett Wollman 	open_output(infile, outfilename);
580ff49530fSBill Paul 	add_warning();
581ff49530fSBill Paul 	if (outfilename || infile){
582074170fbSWarner Losh 		guard = tmp = generate_guard(outfilename ? outfilename: infile);
583ff49530fSBill Paul 	} else
584ff49530fSBill Paul 		guard = "STDIN_";
585ff49530fSBill Paul 
586ff49530fSBill Paul 	f_print(fout, "#ifndef _%s\n#define	_%s\n\n", guard,
587ff49530fSBill Paul 		guard);
588ff49530fSBill Paul 
589ff49530fSBill Paul 	f_print(fout, "#include <rpc/rpc.h>\n");
590ff49530fSBill Paul 
59140ad8885SAlfred Perlstein 	if (mtflag)
59235ab9586SDavid E. O'Brien 		f_print(fout, "#include <pthread.h>\n");
593ff49530fSBill Paul 
594ff49530fSBill Paul 	/* put the C++ support */
59515df5e2dSStefan Farfeleder 	if (!CCflag) {
596ff49530fSBill Paul 		f_print(fout, "\n#ifdef __cplusplus\n");
597ff49530fSBill Paul 		f_print(fout, "extern \"C\" {\n");
598ff49530fSBill Paul 		f_print(fout, "#endif\n\n");
599ff49530fSBill Paul 	}
600ff49530fSBill Paul 
601ff49530fSBill Paul 	/* put in a typedef for quadprecision. Only with Cflag */
602ff49530fSBill Paul 
6034e115012SGarrett Wollman 	tell = ftell(fout);
604ff49530fSBill Paul 
605ff49530fSBill Paul 	/* print data definitions */
606526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
607ec06b5e8SStefan Farfeleder 		print_datadef(def, headeronly);
6084e115012SGarrett Wollman 	}
609ff49530fSBill Paul 
610ff49530fSBill Paul 	/*
611ff49530fSBill Paul 	 * print function declarations.
612ff49530fSBill Paul 	 *  Do this after data definitions because they might be used as
613ff49530fSBill Paul 	 *  arguments for functions
614ff49530fSBill Paul 	 */
615ff49530fSBill Paul 	for (l = defined; l != NULL; l = l->next) {
616ec06b5e8SStefan Farfeleder 		print_funcdef(l->val, headeronly);
617ff49530fSBill Paul 	}
618ff49530fSBill Paul 	/* Now  print all xdr func declarations */
619ff49530fSBill Paul 	if (xdrfunc_head != NULL){
620ff49530fSBill Paul 
621ff49530fSBill Paul 		f_print(fout,
622ff49530fSBill Paul 			"\n/* the xdr functions */\n");
623ff49530fSBill Paul 
624ff49530fSBill Paul 		if (CCflag){
625ff49530fSBill Paul 			f_print(fout, "\n#ifdef __cplusplus\n");
626ff49530fSBill Paul 			f_print(fout, "extern \"C\" {\n");
627ff49530fSBill Paul 			f_print(fout, "#endif\n");
628ff49530fSBill Paul 		}
629ff49530fSBill Paul 
630ff49530fSBill Paul 		xdrfuncp = xdrfunc_head;
631ff49530fSBill Paul 		while (xdrfuncp != NULL){
63215df5e2dSStefan Farfeleder 			print_xdr_func_def(xdrfuncp->name, xdrfuncp->pointerp);
633ff49530fSBill Paul 			xdrfuncp = xdrfuncp->next;
634ff49530fSBill Paul 		}
635ff49530fSBill Paul 	}
636ff49530fSBill Paul 
6374e115012SGarrett Wollman 	if (extend && tell == ftell(fout)) {
6384e115012SGarrett Wollman 		(void) unlink(outfilename);
639ff49530fSBill Paul 	} else if (tblflag) {
640ff49530fSBill Paul 		f_print(fout, rpcgen_table_dcl);
6414e115012SGarrett Wollman 	}
642ff49530fSBill Paul 
643ff49530fSBill Paul 	f_print(fout, "\n#ifdef __cplusplus\n");
644ff49530fSBill Paul 	f_print(fout, "}\n");
645ff49530fSBill Paul 	f_print(fout, "#endif\n");
646ff49530fSBill Paul 
647ff49530fSBill Paul 	f_print(fout, "\n#endif /* !_%s */\n", guard);
648074170fbSWarner Losh 	free(tmp);
6494e115012SGarrett Wollman }
6504e115012SGarrett Wollman 
6514e115012SGarrett Wollman /*
6524e115012SGarrett Wollman  * Compile into an RPC service
6534e115012SGarrett Wollman  */
654526195adSJordan K. Hubbard static void
s_output(int argc,const char * argv[],const char * infile,const char * define,int extend,const char * outfile,int nomain,int netflag)655e390e3afSDavid Malone s_output(int argc, const char *argv[], const char *infile, const char *define,
656e390e3afSDavid Malone     int extend, const char *outfile, int nomain, int netflag)
6574e115012SGarrett Wollman {
6584e115012SGarrett Wollman 	char *include;
6594e115012SGarrett Wollman 	definition *def;
660ff49530fSBill Paul 	int foundprogram = 0;
661e390e3afSDavid Malone 	const char *outfilename;
6624e115012SGarrett Wollman 
6634e115012SGarrett Wollman 	open_input(infile, define);
6644e115012SGarrett Wollman 	outfilename = extend ? extendfile(infile, outfile) : outfile;
6654e115012SGarrett Wollman 	open_output(infile, outfilename);
666ff49530fSBill Paul 	add_warning();
6674e115012SGarrett Wollman 	if (infile && (include = extendfile(infile, ".h"))) {
6684e115012SGarrett Wollman 		f_print(fout, "#include \"%s\"\n", include);
6694e115012SGarrett Wollman 		free(include);
670ff49530fSBill Paul 	} else
671ff49530fSBill Paul 		f_print(fout, "#include <rpc/rpc.h>\n");
672ff49530fSBill Paul 
673ff49530fSBill Paul 	f_print(fout, "#include <stdio.h>\n");
674ff49530fSBill Paul 	f_print(fout, "#include <stdlib.h> /* getenv, exit */\n");
67515df5e2dSStefan Farfeleder 	f_print (fout, "#include <rpc/pmap_clnt.h> /* for pmap_unset */\n");
676ff49530fSBill Paul 	f_print (fout, "#include <string.h> /* strcmp */\n");
677bcb53b16SMartin Blapp 	if (tirpcflag)
678bcb53b16SMartin Blapp 		f_print(fout, "#include <rpc/rpc_com.h>\n");
679ff49530fSBill Paul 	if (strcmp(svcclosetime, "-1") == 0)
680ff49530fSBill Paul 		indefinitewait = 1;
681ff49530fSBill Paul 	else if (strcmp(svcclosetime, "0") == 0)
682ff49530fSBill Paul 		exitnow = 1;
683ff49530fSBill Paul 	else if (inetdflag || pmflag) {
684ff49530fSBill Paul 		f_print(fout, "#include <signal.h>\n");
685ff49530fSBill Paul 		timerflag = 1;
686ff49530fSBill Paul 	}
687ff49530fSBill Paul 
688ff49530fSBill Paul 	if (!tirpcflag && inetdflag)
689ff49530fSBill Paul 		f_print(fout, "#include <sys/ttycom.h> /* TIOCNOTTY */\n");
69015df5e2dSStefan Farfeleder 	if (inetdflag || pmflag) {
691ff49530fSBill Paul 		f_print(fout, "#ifdef __cplusplus\n");
692ff49530fSBill Paul 		f_print(fout,
693f87730d4SXin LI 			"#include <sys/sysent.h> /* getdtablesize, open */\n");
694ff49530fSBill Paul 		f_print(fout, "#endif /* __cplusplus */\n");
695ff49530fSBill Paul 	}
69640ad8885SAlfred Perlstein 	if (tirpcflag) {
69740ad8885SAlfred Perlstein 		f_print(fout, "#include <fcntl.h> /* open */\n");
69840ad8885SAlfred Perlstein 		f_print(fout, "#include <unistd.h> /* fork / setsid */\n");
699ff49530fSBill Paul 		f_print(fout, "#include <sys/types.h>\n");
70040ad8885SAlfred Perlstein 	}
701ff49530fSBill Paul 
7024f83fd19SStefan Farfeleder 	f_print(fout, "#include <string.h>\n");
703ff49530fSBill Paul 	if (inetdflag || !tirpcflag) {
704ff49530fSBill Paul 		f_print(fout, "#include <sys/socket.h>\n");
705ff49530fSBill Paul 		f_print(fout, "#include <netinet/in.h>\n");
706ff49530fSBill Paul 	}
707ff49530fSBill Paul 
708ff49530fSBill Paul 	if ((netflag || pmflag) && tirpcflag && !nomain) {
709ff49530fSBill Paul 		f_print(fout, "#include <netconfig.h>\n");
710ff49530fSBill Paul 	}
711ff49530fSBill Paul 	if (tirpcflag)
712ff49530fSBill Paul 		f_print(fout, "#include <sys/resource.h> /* rlimit */\n");
71340ad8885SAlfred Perlstein 	if (logflag || inetdflag || pmflag || tirpcflag)
714ff49530fSBill Paul 		f_print(fout, "#include <syslog.h>\n");
715ff49530fSBill Paul 
716ff49530fSBill Paul 	f_print(fout, "\n#ifdef DEBUG\n#define	RPC_SVC_FG\n#endif\n");
717ff49530fSBill Paul 	if (timerflag)
718ff49530fSBill Paul 		f_print(fout, "\n#define	_RPCSVC_CLOSEDOWN %s\n",
719ff49530fSBill Paul 			svcclosetime);
720526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
7214e115012SGarrett Wollman 		foundprogram |= (def->def_kind == DEF_PROGRAM);
7224e115012SGarrett Wollman 	}
7234e115012SGarrett Wollman 	if (extend && !foundprogram) {
7244e115012SGarrett Wollman 		(void) unlink(outfilename);
7254e115012SGarrett Wollman 		return;
7264e115012SGarrett Wollman 	}
727ff49530fSBill Paul 	write_most(infile, netflag, nomain);
728ff49530fSBill Paul 	if (!nomain) {
729ff49530fSBill Paul 		if (!do_registers(argc, argv)) {
730ff49530fSBill Paul 			if (outfilename)
731ff49530fSBill Paul 				(void) unlink(outfilename);
732ff49530fSBill Paul 			usage();
733ff49530fSBill Paul 		}
7344e115012SGarrett Wollman 		write_rest();
7354e115012SGarrett Wollman 	}
7364e115012SGarrett Wollman }
7374e115012SGarrett Wollman 
738ff49530fSBill Paul /*
739ff49530fSBill Paul  * generate client side stubs
740ff49530fSBill Paul  */
741526195adSJordan K. Hubbard static void
l_output(const char * infile,const char * define,int extend,const char * outfile)742e390e3afSDavid Malone l_output(const char *infile, const char *define, int extend, const char *outfile)
7434e115012SGarrett Wollman {
7444e115012SGarrett Wollman 	char *include;
7454e115012SGarrett Wollman 	definition *def;
746ff49530fSBill Paul 	int foundprogram = 0;
747e390e3afSDavid Malone 	const char *outfilename;
7484e115012SGarrett Wollman 
7494e115012SGarrett Wollman 	open_input(infile, define);
7504e115012SGarrett Wollman 	outfilename = extend ? extendfile(infile, outfile) : outfile;
7514e115012SGarrett Wollman 	open_output(infile, outfilename);
752ff49530fSBill Paul 	add_warning();
7534f83fd19SStefan Farfeleder 	f_print (fout, "#include <string.h> /* for memset */\n");
7544e115012SGarrett Wollman 	if (infile && (include = extendfile(infile, ".h"))) {
7554e115012SGarrett Wollman 		f_print(fout, "#include \"%s\"\n", include);
7564e115012SGarrett Wollman 		free(include);
757ff49530fSBill Paul 	} else
758ff49530fSBill Paul 		f_print(fout, "#include <rpc/rpc.h>\n");
759526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
7604e115012SGarrett Wollman 		foundprogram |= (def->def_kind == DEF_PROGRAM);
7614e115012SGarrett Wollman 	}
7624e115012SGarrett Wollman 	if (extend && !foundprogram) {
7634e115012SGarrett Wollman 		(void) unlink(outfilename);
7644e115012SGarrett Wollman 		return;
7654e115012SGarrett Wollman 	}
7664e115012SGarrett Wollman 	write_stubs();
7674e115012SGarrett Wollman }
7684e115012SGarrett Wollman 
7694e115012SGarrett Wollman /*
770ff49530fSBill Paul  * generate the dispatch table
7714e115012SGarrett Wollman  */
772526195adSJordan K. Hubbard static void
t_output(const char * infile,const char * define,int extend,const char * outfile)773e390e3afSDavid Malone t_output(const char *infile, const char *define, int extend, const char *outfile)
774ff49530fSBill Paul {
775ff49530fSBill Paul 	definition *def;
776ff49530fSBill Paul 	int foundprogram = 0;
777e390e3afSDavid Malone 	const char *outfilename;
778ff49530fSBill Paul 
779ff49530fSBill Paul 	open_input(infile, define);
780ff49530fSBill Paul 	outfilename = extend ? extendfile(infile, outfile) : outfile;
781ff49530fSBill Paul 	open_output(infile, outfilename);
782ff49530fSBill Paul 	add_warning();
783526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
784ff49530fSBill Paul 		foundprogram |= (def->def_kind == DEF_PROGRAM);
785ff49530fSBill Paul 	}
786ff49530fSBill Paul 	if (extend && !foundprogram) {
787ff49530fSBill Paul 		(void) unlink(outfilename);
788ff49530fSBill Paul 		return;
789ff49530fSBill Paul 	}
790ff49530fSBill Paul 	write_tables();
791ff49530fSBill Paul }
792ff49530fSBill Paul 
793ff49530fSBill Paul /* sample routine for the server template */
794526195adSJordan K. Hubbard static void
svc_output(const char * infile,const char * define,int extend,const char * outfile)795e390e3afSDavid Malone svc_output(const char *infile, const char *define, int extend, const char *outfile)
796ff49530fSBill Paul {
797ff49530fSBill Paul 	definition *def;
798ff49530fSBill Paul 	char *include;
799e390e3afSDavid Malone 	const char *outfilename;
800ff49530fSBill Paul 	long tell;
801ff49530fSBill Paul 	open_input(infile, define);
802ff49530fSBill Paul 	outfilename = extend ? extendfile(infile, outfile) : outfile;
803ff49530fSBill Paul 	checkfiles(infile, outfilename);
804ff49530fSBill Paul 	/*
805ff49530fSBill Paul 	 * Check if outfile already exists.
806ff49530fSBill Paul 	 * if so, print an error message and exit
807ff49530fSBill Paul 	 */
808ff49530fSBill Paul 	open_output(infile, outfilename);
809ff49530fSBill Paul 	add_sample_msg();
810ff49530fSBill Paul 
811ff49530fSBill Paul 	if (infile && (include = extendfile(infile, ".h"))) {
812ff49530fSBill Paul 		f_print(fout, "#include \"%s\"\n", include);
813ff49530fSBill Paul 		free(include);
814ff49530fSBill Paul 	} else
815ff49530fSBill Paul 		f_print(fout, "#include <rpc/rpc.h>\n");
816ff49530fSBill Paul 
817ff49530fSBill Paul 	tell = ftell(fout);
818526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
819ff49530fSBill Paul 		write_sample_svc(def);
820ff49530fSBill Paul 	}
821ff49530fSBill Paul 	if (extend && tell == ftell(fout)) {
822ff49530fSBill Paul 		(void) unlink(outfilename);
823ff49530fSBill Paul 	}
824ff49530fSBill Paul }
825ff49530fSBill Paul 
826ff49530fSBill Paul /* sample main routine for client */
827526195adSJordan K. Hubbard static void
clnt_output(const char * infile,const char * define,int extend,const char * outfile)828e390e3afSDavid Malone clnt_output(const char *infile, const char *define, int extend, const char *outfile)
829ff49530fSBill Paul {
830ff49530fSBill Paul 	definition *def;
831ff49530fSBill Paul 	char *include;
832e390e3afSDavid Malone 	const char *outfilename;
833ff49530fSBill Paul 	long tell;
834ff49530fSBill Paul 	int has_program = 0;
835ff49530fSBill Paul 
836ff49530fSBill Paul 	open_input(infile, define);
837ff49530fSBill Paul 	outfilename = extend ? extendfile(infile, outfile) : outfile;
838ff49530fSBill Paul 	checkfiles(infile, outfilename);
839ff49530fSBill Paul 	/*
840ff49530fSBill Paul 	 * Check if outfile already exists.
841ff49530fSBill Paul 	 * if so, print an error message and exit
842ff49530fSBill Paul 	 */
843ff49530fSBill Paul 
844ff49530fSBill Paul 	open_output(infile, outfilename);
845ff49530fSBill Paul 	add_sample_msg();
846ff49530fSBill Paul 	if (infile && (include = extendfile(infile, ".h"))) {
847ff49530fSBill Paul 		f_print(fout, "#include \"%s\"\n", include);
848ff49530fSBill Paul 		free(include);
849ff49530fSBill Paul 	} else
850ff49530fSBill Paul 		f_print(fout, "#include <rpc/rpc.h>\n");
8517c31e86bSDoug Rabson 	f_print(fout, "#include <stdio.h>\n");
8527c31e86bSDoug Rabson 	f_print(fout, "#include <stdlib.h>\n");
853ff49530fSBill Paul 	tell = ftell(fout);
854526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
855ff49530fSBill Paul 		has_program += write_sample_clnt(def);
856ff49530fSBill Paul 	}
857ff49530fSBill Paul 
858ff49530fSBill Paul 	if (has_program)
859ff49530fSBill Paul 		write_sample_clnt_main();
860ff49530fSBill Paul 
861ff49530fSBill Paul 	if (extend && tell == ftell(fout)) {
862ff49530fSBill Paul 		(void) unlink(outfilename);
863ff49530fSBill Paul 	}
864ff49530fSBill Paul }
865ff49530fSBill Paul 
866ff49530fSBill Paul 
mkfile_output(struct commandline * cmd)867e390e3afSDavid Malone static void mkfile_output(struct commandline *cmd)
868ff49530fSBill Paul {
869e390e3afSDavid Malone 	const char *mkfilename, *clientname, *clntname, *xdrname, *hdrname;
870e390e3afSDavid Malone 	const char *servername, *svcname, *servprogname, *clntprogname;
871e390e3afSDavid Malone 	char *temp, *mkftemp;
872ff49530fSBill Paul 
873ff49530fSBill Paul 	svcname = file_name(cmd->infile, "_svc.c");
874ff49530fSBill Paul 	clntname = file_name(cmd->infile, "_clnt.c");
875ff49530fSBill Paul 	xdrname = file_name(cmd->infile, "_xdr.c");
876ff49530fSBill Paul 	hdrname = file_name(cmd->infile, ".h");
877ff49530fSBill Paul 
878ff49530fSBill Paul 
879ff49530fSBill Paul 	if (allfiles){
880ff49530fSBill Paul 		servername = extendfile(cmd->infile, "_server.c");
881ff49530fSBill Paul 		clientname = extendfile(cmd->infile, "_client.c");
882ff49530fSBill Paul 	}else{
883ff49530fSBill Paul 		servername = " ";
884ff49530fSBill Paul 		clientname = " ";
885ff49530fSBill Paul 	}
886ff49530fSBill Paul 	servprogname = extendfile(cmd->infile, "_server");
887ff49530fSBill Paul 	clntprogname = extendfile(cmd->infile, "_client");
888ff49530fSBill Paul 
889ff49530fSBill Paul 	if (allfiles){
890e390e3afSDavid Malone 		mkftemp = xmalloc(strlen("makefile.") +
891ff49530fSBill Paul 		                     strlen(cmd->infile) + 1);
892b3608ae1SEd Schouten 		temp = strrchr(cmd->infile, '.');
893e390e3afSDavid Malone 		strcpy(mkftemp, "makefile.");
894e390e3afSDavid Malone 		(void) strncat(mkftemp, cmd->infile,
895ff49530fSBill Paul 			(temp - cmd->infile));
896e390e3afSDavid Malone 		mkfilename = mkftemp;
897ff49530fSBill Paul 	} else
898ff49530fSBill Paul 		mkfilename = cmd->outfile;
899ff49530fSBill Paul 
900ff49530fSBill Paul 
901ff49530fSBill Paul 	checkfiles(NULL, mkfilename);
902ff49530fSBill Paul 	open_output(NULL, mkfilename);
903ff49530fSBill Paul 
904ff49530fSBill Paul 	f_print(fout, "\n# This is a template makefile generated\
905ff49530fSBill Paul 		by rpcgen \n");
906ff49530fSBill Paul 
907ff49530fSBill Paul 	f_print(fout, "\n# Parameters \n\n");
908ff49530fSBill Paul 
909ff49530fSBill Paul 	f_print(fout, "CLIENT = %s\nSERVER = %s\n\n",
910ff49530fSBill Paul 		clntprogname, servprogname);
911ff49530fSBill Paul 	f_print(fout, "SOURCES_CLNT.c = \nSOURCES_CLNT.h = \n");
912ff49530fSBill Paul 	f_print(fout, "SOURCES_SVC.c = \nSOURCES_SVC.h = \n");
913ff49530fSBill Paul 	f_print(fout, "SOURCES.x = %s\n\n", cmd->infile);
914ff49530fSBill Paul 	f_print(fout, "TARGETS_SVC.c = %s %s %s \n",
915ff49530fSBill Paul 		svcname, servername, xdrname);
916ff49530fSBill Paul 	f_print(fout, "TARGETS_CLNT.c = %s %s %s \n",
917ff49530fSBill Paul 		clntname, clientname, xdrname);
918ff49530fSBill Paul 	f_print(fout, "TARGETS = %s %s %s %s %s %s\n\n",
919ff49530fSBill Paul 		hdrname, xdrname, clntname,
920ff49530fSBill Paul 		svcname, clientname, servername);
921ff49530fSBill Paul 
922ff49530fSBill Paul 	f_print(fout, "OBJECTS_CLNT = $(SOURCES_CLNT.c:%%.c=%%.o) \
923ff49530fSBill Paul $(TARGETS_CLNT.c:%%.c=%%.o) ");
924ff49530fSBill Paul 
925ff49530fSBill Paul 	f_print(fout, "\nOBJECTS_SVC = $(SOURCES_SVC.c:%%.c=%%.o) \
926ff49530fSBill Paul $(TARGETS_SVC.c:%%.c=%%.o) ");
927ff49530fSBill Paul 
928ff49530fSBill Paul 
929ff49530fSBill Paul 	f_print(fout, "\n# Compiler flags \n");
930ff49530fSBill Paul 	if (mtflag)
93140ad8885SAlfred Perlstein 		f_print(fout, "\nCFLAGS += -D_REENTRANT -D_THEAD_SAFE \nLDLIBS += -pthread\n");
93240ad8885SAlfred Perlstein 
933ff49530fSBill Paul 	f_print(fout, "RPCGENFLAGS = \n");
934ff49530fSBill Paul 
935ff49530fSBill Paul 	f_print(fout, "\n# Targets \n\n");
936ff49530fSBill Paul 
937ff49530fSBill Paul 	f_print(fout, "all : $(CLIENT) $(SERVER)\n\n");
938ff49530fSBill Paul 	f_print(fout, "$(TARGETS) : $(SOURCES.x) \n");
939ff49530fSBill Paul 	f_print(fout, "\trpcgen $(RPCGENFLAGS) $(SOURCES.x)\n\n");
9407c31e86bSDoug Rabson 	if (allfiles) {
9417c31e86bSDoug Rabson 		f_print(fout, "\trpcgen -Sc $(RPCGENFLAGS) $(SOURCES.x) -o %s\n\n", clientname);
9427c31e86bSDoug Rabson 		f_print(fout, "\trpcgen -Ss $(RPCGENFLAGS) $(SOURCES.x) -o %s\n\n", servername);
9437c31e86bSDoug Rabson 	}
944ff49530fSBill Paul 	f_print(fout, "$(OBJECTS_CLNT) : $(SOURCES_CLNT.c) $(SOURCES_CLNT.h) \
945ff49530fSBill Paul $(TARGETS_CLNT.c) \n\n");
946ff49530fSBill Paul 
947ff49530fSBill Paul 	f_print(fout, "$(OBJECTS_SVC) : $(SOURCES_SVC.c) $(SOURCES_SVC.h) \
948ff49530fSBill Paul $(TARGETS_SVC.c) \n\n");
949ff49530fSBill Paul 	f_print(fout, "$(CLIENT) : $(OBJECTS_CLNT) \n");
950ff49530fSBill Paul 	f_print(fout, "\t$(CC) -o $(CLIENT) $(OBJECTS_CLNT) \
951ff49530fSBill Paul $(LDLIBS) \n\n");
952ff49530fSBill Paul 	f_print(fout, "$(SERVER) : $(OBJECTS_SVC) \n");
953ff49530fSBill Paul 	f_print(fout, "\t$(CC) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS)\n\n");
95436752498SBryan Drewery 	f_print(fout, "clean:\n\t rm -f core $(TARGETS) $(OBJECTS_CLNT) \
955ff49530fSBill Paul $(OBJECTS_SVC) $(CLIENT) $(SERVER)\n\n");
956ff49530fSBill Paul }
957ff49530fSBill Paul 
958ff49530fSBill Paul 
959ff49530fSBill Paul 
960ff49530fSBill Paul /*
961ff49530fSBill Paul  * Perform registrations for service output
962ff49530fSBill Paul  * Return 0 if failed; 1 otherwise.
963ff49530fSBill Paul  */
964526195adSJordan K. Hubbard static int
do_registers(int argc,const char * argv[])965e390e3afSDavid Malone do_registers(int argc, const char *argv[])
9664e115012SGarrett Wollman {
9674e115012SGarrett Wollman 	int i;
9684e115012SGarrett Wollman 
969ff49530fSBill Paul 	if (inetdflag || !tirpcflag) {
9704e115012SGarrett Wollman 		for (i = 1; i < argc; i++) {
9714e115012SGarrett Wollman 			if (streq(argv[i], "-s")) {
972ff49530fSBill Paul 				if (!check_nettype(argv[i + 1],
973ff49530fSBill Paul 						    valid_i_nettypes))
974ff49530fSBill Paul 					return (0);
975ff49530fSBill Paul 				write_inetd_register(argv[i + 1]);
9764e115012SGarrett Wollman 				i++;
9774e115012SGarrett Wollman 			}
9784e115012SGarrett Wollman 		}
979ff49530fSBill Paul 	} else {
980ff49530fSBill Paul 		for (i = 1; i < argc; i++)
981ff49530fSBill Paul 			if (streq(argv[i], "-s")) {
982ff49530fSBill Paul 				if (!check_nettype(argv[i + 1],
983ff49530fSBill Paul 						    valid_ti_nettypes))
984ff49530fSBill Paul 					return (0);
985ff49530fSBill Paul 				write_nettype_register(argv[i + 1]);
986ff49530fSBill Paul 				i++;
987ff49530fSBill Paul 			} else if (streq(argv[i], "-n")) {
988ff49530fSBill Paul 				write_netid_register(argv[i + 1]);
989ff49530fSBill Paul 				i++;
990ff49530fSBill Paul 			}
991ff49530fSBill Paul 	}
992ff49530fSBill Paul 	return (1);
993ff49530fSBill Paul }
994ff49530fSBill Paul 
995ff49530fSBill Paul /*
9967c8b268aSBrooks Davis  * Extend the argument list
9977c8b268aSBrooks Davis  */
9987c8b268aSBrooks Davis static void
moreargs(void)9997c8b268aSBrooks Davis moreargs(void)
10007c8b268aSBrooks Davis {
10017c8b268aSBrooks Davis 	char **newarglist;
10027c8b268aSBrooks Davis 
10037c8b268aSBrooks Davis 	argmax = argmax == 0 ? 32 : argmax << 1;
10047c8b268aSBrooks Davis 	if (argmax > INT_MAX / 4) {
10057c8b268aSBrooks Davis 		warnx("refusing to allocate too many arguments");
10067c8b268aSBrooks Davis 		crash();
10077c8b268aSBrooks Davis 	}
10087c8b268aSBrooks Davis 	newarglist = realloc(arglist, argmax * sizeof(*arglist));
10097c8b268aSBrooks Davis 	if (newarglist == NULL) {
10107c8b268aSBrooks Davis 		warnx("unable to allocate arglist");
10117c8b268aSBrooks Davis 		crash();
10127c8b268aSBrooks Davis 	}
10137c8b268aSBrooks Davis 	arglist = newarglist;
10147c8b268aSBrooks Davis }
10157c8b268aSBrooks Davis 
10167c8b268aSBrooks Davis /*
1017ff49530fSBill Paul  * Add another argument to the arg list
1018ff49530fSBill Paul  */
1019ff49530fSBill Paul static void
addarg(const char * cp)1020e390e3afSDavid Malone addarg(const char *cp)
1021ff49530fSBill Paul {
10227c8b268aSBrooks Davis 	if (argcount >= argmax)
10237c8b268aSBrooks Davis 		moreargs();
10247c8b268aSBrooks Davis 
10258cb6926eSCraig Rodrigues 	if (cp != NULL)
10268cb6926eSCraig Rodrigues 		arglist[argcount++] = xstrdup(cp);
10278cb6926eSCraig Rodrigues 	else
10288cb6926eSCraig Rodrigues 		arglist[argcount++] = NULL;
1029ff49530fSBill Paul }
1030ff49530fSBill Paul 
10310dac0ed8SDimitry Andric /*
10320dac0ed8SDimitry Andric  * Insert an argument at the specified location
10330dac0ed8SDimitry Andric  */
1034ff49530fSBill Paul static void
insarg(int place,const char * cp)10350dac0ed8SDimitry Andric insarg(int place, const char *cp)
1036ff49530fSBill Paul {
10370dac0ed8SDimitry Andric 	int i;
10380dac0ed8SDimitry Andric 
10397c8b268aSBrooks Davis 	if (argcount >= argmax)
10407c8b268aSBrooks Davis 		moreargs();
10410dac0ed8SDimitry Andric 
10420dac0ed8SDimitry Andric 	/* Move up existing arguments */
1043ade215fdSDimitry Andric 	for (i = argcount - 1; i >= place; i--)
10440dac0ed8SDimitry Andric 		arglist[i + 1] = arglist[i];
10450dac0ed8SDimitry Andric 
10468cb6926eSCraig Rodrigues 	arglist[place] = xstrdup(cp);
10470dac0ed8SDimitry Andric 	argcount++;
1048ff49530fSBill Paul }
1049ff49530fSBill Paul 
1050ff49530fSBill Paul /*
1051ff49530fSBill Paul  * if input file is stdin and an output file is specified then complain
1052ff49530fSBill Paul  * if the file already exists. Otherwise the file may get overwritten
1053ff49530fSBill Paul  * If input file does not exist, exit with an error
1054ff49530fSBill Paul  */
1055ff49530fSBill Paul 
1056ff49530fSBill Paul static void
checkfiles(const char * infile,const char * outfile)1057e390e3afSDavid Malone checkfiles(const char *infile, const char *outfile)
1058ff49530fSBill Paul {
1059ff49530fSBill Paul 
1060ff49530fSBill Paul 	struct stat buf;
1061ff49530fSBill Paul 
1062ff49530fSBill Paul 	if (infile)		/* infile ! = NULL */
1063ff49530fSBill Paul 		if (stat(infile, &buf) < 0)
1064ff49530fSBill Paul 		{
10650e76f40dSPhilippe Charnier 			warn("%s", infile);
1066ff49530fSBill Paul 			crash();
106780c7cc1cSPedro F. Giffuni 		}
1068ff49530fSBill Paul 	if (outfile) {
1069ff49530fSBill Paul 		if (stat(outfile, &buf) < 0)
1070ff49530fSBill Paul 			return;	/* file does not exist */
1071ff49530fSBill Paul 		else {
10720e76f40dSPhilippe Charnier 			warnx("file '%s' already exists and may be overwritten", outfile);
1073ff49530fSBill Paul 			crash();
1074ff49530fSBill Paul 		}
1075ff49530fSBill Paul 	}
10764e115012SGarrett Wollman }
10774e115012SGarrett Wollman 
10784e115012SGarrett Wollman /*
10794e115012SGarrett Wollman  * Parse command line arguments
10804e115012SGarrett Wollman  */
1081526195adSJordan K. Hubbard static int
parseargs(int argc,const char * argv[],struct commandline * cmd)1082e390e3afSDavid Malone parseargs(int argc, const char *argv[], struct commandline *cmd)
10834e115012SGarrett Wollman {
10844e115012SGarrett Wollman 	int i;
10854e115012SGarrett Wollman 	int j;
1086ff49530fSBill Paul 	char c, ch;
10874e115012SGarrett Wollman 	char flag[(1 << 8 * sizeof (char))];
10884e115012SGarrett Wollman 	int nflags;
10894e115012SGarrett Wollman 
10904e115012SGarrett Wollman 	cmd->infile = cmd->outfile = NULL;
10914e115012SGarrett Wollman 	if (argc < 2) {
10924e115012SGarrett Wollman 		return (0);
10934e115012SGarrett Wollman 	}
1094ff49530fSBill Paul 	allfiles = 0;
10954e115012SGarrett Wollman 	flag['c'] = 0;
10964e115012SGarrett Wollman 	flag['h'] = 0;
10974e115012SGarrett Wollman 	flag['l'] = 0;
10984e115012SGarrett Wollman 	flag['m'] = 0;
1099ff49530fSBill Paul 	flag['o'] = 0;
1100ff49530fSBill Paul 	flag['s'] = 0;
1101ff49530fSBill Paul 	flag['n'] = 0;
1102ff49530fSBill Paul 	flag['t'] = 0;
1103ff49530fSBill Paul 	flag['S'] = 0;
1104ff49530fSBill Paul 	flag['C'] = 0;
1105ff49530fSBill Paul 	flag['M'] = 0;
1106ff49530fSBill Paul 
11074e115012SGarrett Wollman 	for (i = 1; i < argc; i++) {
11084e115012SGarrett Wollman 		if (argv[i][0] != '-') {
11094e115012SGarrett Wollman 			if (cmd->infile) {
11100e76f40dSPhilippe Charnier 				warnx("cannot specify more than one input file");
11114e115012SGarrett Wollman 				return (0);
11124e115012SGarrett Wollman 			}
11134e115012SGarrett Wollman 			cmd->infile = argv[i];
11144e115012SGarrett Wollman 		} else {
11154e115012SGarrett Wollman 			for (j = 1; argv[i][j] != 0; j++) {
11164e115012SGarrett Wollman 				c = argv[i][j];
11174e115012SGarrett Wollman 				switch (c) {
1118ff49530fSBill Paul 				case 'a':
1119ff49530fSBill Paul 					allfiles = 1;
1120ff49530fSBill Paul 					break;
11214e115012SGarrett Wollman 				case 'c':
11224e115012SGarrett Wollman 				case 'h':
11234e115012SGarrett Wollman 				case 'l':
11244e115012SGarrett Wollman 				case 'm':
1125ff49530fSBill Paul 				case 't':
1126526195adSJordan K. Hubbard 					if (flag[(int)c]) {
11274e115012SGarrett Wollman 						return (0);
11284e115012SGarrett Wollman 					}
1129526195adSJordan K. Hubbard 					flag[(int)c] = 1;
11304e115012SGarrett Wollman 					break;
1131ff49530fSBill Paul 				case 'S':
1132ff49530fSBill Paul 					/*
1133ff49530fSBill Paul 					 * sample flag: Ss or Sc.
1134ff49530fSBill Paul 					 *  Ss means set flag['S'];
1135ff49530fSBill Paul 					 *  Sc means set flag['C'];
1136ff49530fSBill Paul 					 *  Sm means set flag['M'];
1137ff49530fSBill Paul 					 */
1138ff49530fSBill Paul 					ch = argv[i][++j]; /* get next char */
1139ff49530fSBill Paul 					if (ch == 's')
1140ff49530fSBill Paul 						ch = 'S';
1141ff49530fSBill Paul 					else if (ch == 'c')
1142ff49530fSBill Paul 						ch = 'C';
1143ff49530fSBill Paul 					else if (ch == 'm')
1144ff49530fSBill Paul 						ch = 'M';
1145ff49530fSBill Paul 					else
1146ff49530fSBill Paul 						return (0);
1147ff49530fSBill Paul 
1148526195adSJordan K. Hubbard 					if (flag[(int)ch]) {
1149ff49530fSBill Paul 						return (0);
1150ff49530fSBill Paul 					}
1151526195adSJordan K. Hubbard 					flag[(int)ch] = 1;
1152ff49530fSBill Paul 					break;
1153ff49530fSBill Paul 				case 'C': /* ANSI C syntax */
1154ff49530fSBill Paul 					ch = argv[i][j+1]; /* get next char */
1155ff49530fSBill Paul 
1156ff49530fSBill Paul 					if (ch != 'C')
1157ff49530fSBill Paul 						break;
1158ff49530fSBill Paul 					CCflag = 1;
1159ff49530fSBill Paul 					break;
1160ff49530fSBill Paul 				case 'b':
1161ff49530fSBill Paul 					/*
1162ff49530fSBill Paul 					 *  Turn TIRPC flag off for
1163ff49530fSBill Paul 					 *  generating backward compatible
1164ff49530fSBill Paul 					 *  code
1165ff49530fSBill Paul 					 */
1166ff49530fSBill Paul 					tirpcflag = 0;
1167ff49530fSBill Paul 					break;
1168ff49530fSBill Paul 
1169ff49530fSBill Paul 				case 'I':
1170ff49530fSBill Paul 					inetdflag = 1;
1171ff49530fSBill Paul 					break;
1172ff49530fSBill Paul 				case 'N':
1173ff49530fSBill Paul 					newstyle = 1;
1174ff49530fSBill Paul 					break;
1175ff49530fSBill Paul 				case 'L':
1176ff49530fSBill Paul 					logflag = 1;
1177ff49530fSBill Paul 					break;
117840ad8885SAlfred Perlstein 				case 'P':
117940ad8885SAlfred Perlstein 					pmflag = 1;
118040ad8885SAlfred Perlstein 					break;
1181ff49530fSBill Paul 				case 'K':
1182ff49530fSBill Paul 					if (++i == argc) {
1183ff49530fSBill Paul 						return (0);
1184ff49530fSBill Paul 					}
1185ff49530fSBill Paul 					svcclosetime = argv[i];
1186ff49530fSBill Paul 					goto nextarg;
1187ff49530fSBill Paul 				case 'T':
1188ff49530fSBill Paul 					tblflag = 1;
1189ff49530fSBill Paul 					break;
1190ff49530fSBill Paul 				case 'M':
1191ff49530fSBill Paul 					mtflag = 1;
1192ff49530fSBill Paul 					break;
1193ff49530fSBill Paul 				case 'i' :
1194ff49530fSBill Paul 					if (++i == argc) {
1195ff49530fSBill Paul 						return (0);
1196ff49530fSBill Paul 					}
1197122562cdSStefan Farfeleder 					inline_size = atoi(argv[i]);
1198ff49530fSBill Paul 					goto nextarg;
1199ff49530fSBill Paul 				case 'n':
12004e115012SGarrett Wollman 				case 'o':
12014e115012SGarrett Wollman 				case 's':
12024e115012SGarrett Wollman 					if (argv[i][j - 1] != '-' ||
12034e115012SGarrett Wollman 					    argv[i][j + 1] != 0) {
12044e115012SGarrett Wollman 						return (0);
12054e115012SGarrett Wollman 					}
1206526195adSJordan K. Hubbard 					flag[(int)c] = 1;
12074e115012SGarrett Wollman 					if (++i == argc) {
12084e115012SGarrett Wollman 						return (0);
12094e115012SGarrett Wollman 					}
1210ff49530fSBill Paul 					if (c == 'o') {
12114e115012SGarrett Wollman 						if (cmd->outfile) {
12124e115012SGarrett Wollman 							return (0);
12134e115012SGarrett Wollman 						}
12144e115012SGarrett Wollman 						cmd->outfile = argv[i];
12154e115012SGarrett Wollman 					}
12164e115012SGarrett Wollman 					goto nextarg;
1217ff49530fSBill Paul 				case 'D':
1218ff49530fSBill Paul 					if (argv[i][j - 1] != '-') {
1219ff49530fSBill Paul 						return (0);
1220ff49530fSBill Paul 					}
1221ff49530fSBill Paul 					(void) addarg(argv[i]);
1222ff49530fSBill Paul 					goto nextarg;
1223ff49530fSBill Paul 				case 'Y':
1224ff49530fSBill Paul 					if (++i == argc) {
1225ff49530fSBill Paul 						return (0);
1226ff49530fSBill Paul 					}
1227faabfb8aSDimitry Andric 					if (strlcpy(pathbuf, argv[i],
1228faabfb8aSDimitry Andric 					    sizeof(pathbuf)) >= sizeof(pathbuf)
1229faabfb8aSDimitry Andric 					    || strlcat(pathbuf, "/cpp",
1230faabfb8aSDimitry Andric 					    sizeof(pathbuf)) >=
1231faabfb8aSDimitry Andric 					    sizeof(pathbuf)) {
1232a0b13740SKris Kennaway 						warnx("argument too long");
1233a0b13740SKris Kennaway 						return (0);
1234a0b13740SKris Kennaway 					}
1235ff49530fSBill Paul 					CPP = pathbuf;
1236ff49530fSBill Paul 					goto nextarg;
1237ff49530fSBill Paul 
1238ff49530fSBill Paul 
12394e115012SGarrett Wollman 
12404e115012SGarrett Wollman 				default:
12414e115012SGarrett Wollman 					return (0);
12424e115012SGarrett Wollman 				}
12434e115012SGarrett Wollman 			}
12444e115012SGarrett Wollman 		nextarg:
12454e115012SGarrett Wollman 			;
12464e115012SGarrett Wollman 		}
12474e115012SGarrett Wollman 	}
1248ff49530fSBill Paul 
12494e115012SGarrett Wollman 	cmd->cflag = flag['c'];
12504e115012SGarrett Wollman 	cmd->hflag = flag['h'];
12514e115012SGarrett Wollman 	cmd->lflag = flag['l'];
12524e115012SGarrett Wollman 	cmd->mflag = flag['m'];
1253ff49530fSBill Paul 	cmd->nflag = flag['n'];
1254ff49530fSBill Paul 	cmd->sflag = flag['s'];
1255ff49530fSBill Paul 	cmd->tflag = flag['t'];
1256ff49530fSBill Paul 	cmd->Ssflag = flag['S'];
1257ff49530fSBill Paul 	cmd->Scflag = flag['C'];
1258ff49530fSBill Paul 	cmd->makefileflag = flag['M'];
1259ff49530fSBill Paul 
1260ff49530fSBill Paul 	if (tirpcflag) {
126140ad8885SAlfred Perlstein 		if (inetdflag)
126240ad8885SAlfred Perlstein 			pmflag = 0;
1263ff49530fSBill Paul 		if ((inetdflag && cmd->nflag)) {
1264ff49530fSBill Paul 			/* netid not allowed with inetdflag */
12650e76f40dSPhilippe Charnier 			warnx("cannot use netid flag with inetd flag");
1266ff49530fSBill Paul 			return (0);
1267ff49530fSBill Paul 		}
1268ff49530fSBill Paul 	} else {		/* 4.1 mode */
1269ff49530fSBill Paul 		pmflag = 0;	/* set pmflag only in tirpcmode */
1270ff49530fSBill Paul 		if (cmd->nflag) { /* netid needs TIRPC */
12710e76f40dSPhilippe Charnier 			warnx("cannot use netid flag without TIRPC");
1272ff49530fSBill Paul 			return (0);
1273ff49530fSBill Paul 		}
1274ff49530fSBill Paul 	}
1275ff49530fSBill Paul 
1276ff49530fSBill Paul 	if (newstyle && (tblflag || cmd->tflag)) {
12770e76f40dSPhilippe Charnier 		warnx("cannot use table flags with newstyle");
1278ff49530fSBill Paul 		return (0);
1279ff49530fSBill Paul 	}
1280ff49530fSBill Paul 
1281ff49530fSBill Paul 	/* check no conflicts with file generation flags */
1282ff49530fSBill Paul 	nflags = cmd->cflag + cmd->hflag + cmd->lflag + cmd->mflag +
1283ff49530fSBill Paul 		cmd->sflag + cmd->nflag + cmd->tflag + cmd->Ssflag +
1284ff49530fSBill Paul 			cmd->Scflag + cmd->makefileflag;
1285ff49530fSBill Paul 
12864e115012SGarrett Wollman 	if (nflags == 0) {
12874e115012SGarrett Wollman 		if (cmd->outfile != NULL || cmd->infile == NULL) {
12884e115012SGarrett Wollman 			return (0);
12894e115012SGarrett Wollman 		}
1290ff49530fSBill Paul 	} else if (cmd->infile == NULL &&
1291ff49530fSBill Paul 	    (cmd->Ssflag || cmd->Scflag || cmd->makefileflag)) {
12920e76f40dSPhilippe Charnier 		warnx("\"infile\" is required for template generation flags");
1293ff49530fSBill Paul 		return (0);
1294ff49530fSBill Paul 	} if (nflags > 1) {
12950e76f40dSPhilippe Charnier 		warnx("cannot have more than one file generation flag");
12964e115012SGarrett Wollman 		return (0);
12974e115012SGarrett Wollman 	}
12984e115012SGarrett Wollman 	return (1);
12994e115012SGarrett Wollman }
1300ff49530fSBill Paul 
1301526195adSJordan K. Hubbard static void
usage(void)1302ef636796SEd Schouten usage(void)
1303ff49530fSBill Paul {
13040e76f40dSPhilippe Charnier 	f_print(stderr, "%s\n%s\n%s\n%s\n%s\n",
13050e76f40dSPhilippe Charnier 		"usage: rpcgen infile",
13060e76f40dSPhilippe Charnier 		"       rpcgen [-abCLNTM] [-Dname[=value]] [-i size]\
130740ad8885SAlfred Perlstein [-I -P [-K seconds]] [-Y path] infile",
13080e76f40dSPhilippe Charnier 		"       rpcgen [-c | -h | -l | -m | -t | -Sc | -Ss | -Sm]\
13090e76f40dSPhilippe Charnier [-o outfile] [infile]",
13100e76f40dSPhilippe Charnier 		"       rpcgen [-s nettype]* [-o outfile] [infile]",
13110e76f40dSPhilippe Charnier 		"       rpcgen [-n netid]* [-o outfile] [infile]");
1312ff49530fSBill Paul 	options_usage();
1313ff49530fSBill Paul 	exit(1);
1314ff49530fSBill Paul }
1315ff49530fSBill Paul 
1316526195adSJordan K. Hubbard static void
options_usage(void)1317ef636796SEd Schouten options_usage(void)
1318ff49530fSBill Paul {
1319ff49530fSBill Paul 	f_print(stderr, "options:\n");
1320ff49530fSBill Paul 	f_print(stderr, "-a\t\tgenerate all files, including samples\n");
1321ff49530fSBill Paul 	f_print(stderr, "-b\t\tbackward compatibility mode (generates code \
132240ad8885SAlfred Perlstein for FreeBSD 4.X)\n");
1323ff49530fSBill Paul 	f_print(stderr, "-c\t\tgenerate XDR routines\n");
1324ff49530fSBill Paul 	f_print(stderr, "-C\t\tANSI C mode\n");
1325ff49530fSBill Paul 	f_print(stderr, "-Dname[=value]\tdefine a symbol (same as #define)\n");
1326ff49530fSBill Paul 	f_print(stderr, "-h\t\tgenerate header file\n");
1327ff49530fSBill Paul 	f_print(stderr, "-i size\t\tsize at which to start generating\
1328ff49530fSBill Paul inline code\n");
132940ad8885SAlfred Perlstein 	f_print(stderr, "-I\t\tgenerate code for inetd support in server\n");
1330ff49530fSBill Paul 	f_print(stderr, "-K seconds\tserver exits after K seconds of\
1331ff49530fSBill Paul inactivity\n");
1332ff49530fSBill Paul 	f_print(stderr, "-l\t\tgenerate client side stubs\n");
1333ff49530fSBill Paul 	f_print(stderr, "-L\t\tserver errors will be printed to syslog\n");
1334ff49530fSBill Paul 	f_print(stderr, "-m\t\tgenerate server side stubs\n");
1335ff49530fSBill Paul 	f_print(stderr, "-M\t\tgenerate MT-safe code\n");
1336ff49530fSBill Paul 	f_print(stderr, "-n netid\tgenerate server code that supports\
1337ff49530fSBill Paul named netid\n");
1338ff49530fSBill Paul 	f_print(stderr, "-N\t\tsupports multiple arguments and\
1339ff49530fSBill Paul call-by-value\n");
1340ff49530fSBill Paul 	f_print(stderr, "-o outfile\tname of the output file\n");
134140ad8885SAlfred Perlstein 	f_print(stderr, "-P\t\tgenerate code for port monitoring support in server\n");
1342ff49530fSBill Paul 	f_print(stderr, "-s nettype\tgenerate server code that supports named\
1343ff49530fSBill Paul nettype\n");
1344ff49530fSBill Paul 	f_print(stderr, "-Sc\t\tgenerate sample client code that uses remote\
1345ff49530fSBill Paul procedures\n");
1346ff49530fSBill Paul 	f_print(stderr, "-Ss\t\tgenerate sample server code that defines\
1347ff49530fSBill Paul remote procedures\n");
1348ff49530fSBill Paul 	f_print(stderr, "-Sm \t\tgenerate makefile template \n");
1349ff49530fSBill Paul 
1350ff49530fSBill Paul 	f_print(stderr, "-t\t\tgenerate RPC dispatch table\n");
1351ff49530fSBill Paul 	f_print(stderr, "-T\t\tgenerate code to support RPC dispatch tables\n");
1352ff49530fSBill Paul 	f_print(stderr, "-Y path\t\tpath where cpp is found\n");
1353ff49530fSBill Paul 	exit(1);
1354ff49530fSBill Paul }
1355