xref: /freebsd/usr.bin/rpcgen/rpc_main.c (revision 587458b7d67a697c53fc2e31fb3407d1084fe773)
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>
48*587458b7SJessica Clarke #include <stdbool.h>
494e115012SGarrett Wollman #include <stdio.h>
50ff49530fSBill Paul #include <string.h>
51ff49530fSBill Paul #include <unistd.h>
52ff49530fSBill Paul #include <sys/types.h>
53ff49530fSBill Paul #include <sys/param.h>
544e115012SGarrett Wollman #include <sys/file.h>
55ff49530fSBill Paul #include <sys/stat.h>
564e115012SGarrett Wollman #include "rpc_parse.h"
574e115012SGarrett Wollman #include "rpc_scan.h"
58d0cc804bSStefan Farfeleder #include "rpc_util.h"
594e115012SGarrett Wollman 
60e390e3afSDavid Malone static void c_output(const char *, const char *, int, const char *);
61e390e3afSDavid Malone static void h_output(const char *, const char *, int, const char *, int);
62e390e3afSDavid Malone static void l_output(const char *, const char *, int, const char *);
63e390e3afSDavid Malone static void t_output(const char *, const char *, int, const char *);
64e390e3afSDavid Malone static void clnt_output(const char *, const char *, int, const char * );
65e390e3afSDavid Malone static char *generate_guard(const char *);
66e390e3afSDavid Malone static void c_initialize(void);
674e115012SGarrett Wollman 
681a7ac2bdSAlfonso Gregory static void usage(void) __dead2;
69d3cb5dedSWarner Losh static void options_usage(void);
70e390e3afSDavid Malone static int do_registers(int, const char **);
71e390e3afSDavid Malone static int parseargs(int, const char **, struct commandline *);
72e390e3afSDavid Malone static void svc_output(const char *, const char *, int, const char *);
73d3cb5dedSWarner Losh static void mkfile_output(struct commandline *);
74e390e3afSDavid Malone static void s_output(int, const char **, const char *, const char *, int, const char *, int, int);
75ff49530fSBill Paul 
76ff49530fSBill Paul #define	EXTEND	1		/* alias for TRUE */
77ff49530fSBill Paul #define	DONT_EXTEND	0		/* alias for FALSE */
78ff49530fSBill Paul 
79e390e3afSDavid Malone static const char *svcclosetime = "120";
800dac0ed8SDimitry Andric static const char *CPP = NULL;
81e390e3afSDavid Malone static const char CPPFLAGS[] = "-C";
82ff49530fSBill Paul static char pathbuf[MAXPATHLEN + 1];
83e390e3afSDavid Malone static const char *allv[] = {
844e115012SGarrett Wollman 	"rpcgen", "-s", "udp", "-s", "tcp",
854e115012SGarrett Wollman };
864b61b26bSMarcelo Araujo static int allc = nitems(allv);
87e390e3afSDavid Malone static const char *allnv[] = {
88ff49530fSBill Paul 	"rpcgen", "-s", "netpath",
89ff49530fSBill Paul };
904b61b26bSMarcelo Araujo static int allnc = nitems(allnv);
91ff49530fSBill Paul 
92ff49530fSBill Paul /*
93ff49530fSBill Paul  * machinations for handling expanding argument list
94ff49530fSBill Paul  */
95e390e3afSDavid Malone static void addarg(const char *);	/* add another argument to the list */
960dac0ed8SDimitry Andric static void insarg(int, const char *);	/* insert arg at specified location */
97e390e3afSDavid Malone static void checkfiles(const char *, const char *);
98e390e3afSDavid Malone 					/* check if out file already exists */
994e115012SGarrett Wollman 
1007c8b268aSBrooks Davis static char **arglist;
1017c8b268aSBrooks Davis static int argcount = 0;
1027c8b268aSBrooks Davis static int argmax = 0;
103ff49530fSBill Paul 
104ff49530fSBill Paul int nonfatalerrors;	/* errors */
10540ad8885SAlfred Perlstein int inetdflag = 0;	/* Support for inetd is disabled by default, use -I */
10640ad8885SAlfred Perlstein int pmflag = 0;		/* Support for port monitors is disabled by default */
10740ad8885SAlfred Perlstein int tirpc_socket = 1;	/* TI-RPC on socket, no TLI library */
108ff49530fSBill Paul int logflag;		/* Use syslog instead of fprintf for errors */
109ff49530fSBill Paul int tblflag;		/* Support for dispatch table file */
110ff49530fSBill Paul int mtflag = 0;		/* Support for MT */
11140ad8885SAlfred Perlstein 
112ff49530fSBill Paul #define INLINE 0
113ff49530fSBill Paul /* length at which to start doing an inline */
114ff49530fSBill Paul 
115122562cdSStefan Farfeleder int inline_size = INLINE;
116ff49530fSBill Paul /*
117ff49530fSBill Paul  * Length at which to start doing an inline. INLINE = default
118ff49530fSBill Paul  * if 0, no xdr_inline code
119ff49530fSBill Paul  */
120ff49530fSBill Paul 
121ff49530fSBill Paul int indefinitewait;	/* If started by port monitors, hang till it wants */
122ff49530fSBill Paul int exitnow;		/* If started by port monitors, exit after the call */
123ff49530fSBill Paul int timerflag;		/* TRUE if !indefinite && !exitnow */
124ff49530fSBill Paul int newstyle;		/* newstyle of passing arguments (by value) */
125ff49530fSBill Paul int CCflag = 0;		/* C++ files */
126ff49530fSBill Paul static int allfiles;   /* generate all files */
127ff49530fSBill Paul int tirpcflag = 1;    /* generating code for tirpc, by default */
128ff49530fSBill Paul xdrfunc *xdrfunc_head = NULL; /* xdr function list */
129ff49530fSBill Paul xdrfunc *xdrfunc_tail = NULL; /* xdr function list */
130ff49530fSBill Paul pid_t childpid;
131ff49530fSBill Paul 
1324e115012SGarrett Wollman 
133526195adSJordan K. Hubbard int
134e390e3afSDavid Malone main(int argc, const char *argv[])
1354e115012SGarrett Wollman {
1364e115012SGarrett Wollman 	struct commandline cmd;
1374e115012SGarrett Wollman 
138ff49530fSBill Paul 	(void) memset((char *)&cmd, 0, sizeof (struct commandline));
139ff49530fSBill Paul 	if (!parseargs(argc, argv, &cmd))
140ff49530fSBill Paul 		usage();
141ff49530fSBill Paul 	/*
142ff49530fSBill Paul 	 * Only the client and server side stubs are likely to be customized,
143ff49530fSBill Paul 	 *  so in that case only, check if the outfile exists, and if so,
144ff49530fSBill Paul 	 *  print an error message and exit.
145ff49530fSBill Paul 	 */
146ff49530fSBill Paul 	if (cmd.Ssflag || cmd.Scflag || cmd.makefileflag) {
147ff49530fSBill Paul 		checkfiles(cmd.infile, cmd.outfile);
1484e115012SGarrett Wollman 	}
149ff49530fSBill Paul 	else
150ff49530fSBill Paul 		checkfiles(cmd.infile, NULL);
151ff49530fSBill Paul 
1524e115012SGarrett Wollman 	if (cmd.cflag) {
153ff49530fSBill Paul 		c_output(cmd.infile, "-DRPC_XDR", DONT_EXTEND, cmd.outfile);
1544e115012SGarrett Wollman 	} else if (cmd.hflag) {
155ec06b5e8SStefan Farfeleder 		h_output(cmd.infile, "-DRPC_HDR", DONT_EXTEND, cmd.outfile,
156ec06b5e8SStefan Farfeleder 		    cmd.hflag);
1574e115012SGarrett Wollman 	} else if (cmd.lflag) {
158ff49530fSBill Paul 		l_output(cmd.infile, "-DRPC_CLNT", DONT_EXTEND, cmd.outfile);
159ff49530fSBill Paul 	} else if (cmd.sflag || cmd.mflag || (cmd.nflag)) {
160ff49530fSBill Paul 		s_output(argc, argv, cmd.infile, "-DRPC_SVC", DONT_EXTEND,
161ff49530fSBill Paul 			cmd.outfile, cmd.mflag, cmd.nflag);
162ff49530fSBill Paul 	} else if (cmd.tflag) {
163ff49530fSBill Paul 		t_output(cmd.infile, "-DRPC_TBL", DONT_EXTEND, cmd.outfile);
164ff49530fSBill Paul 	} else if  (cmd.Ssflag) {
165ff49530fSBill Paul 		svc_output(cmd.infile, "-DRPC_SERVER", DONT_EXTEND,
166ff49530fSBill Paul 			cmd.outfile);
167ff49530fSBill Paul 	} else if (cmd.Scflag) {
168ff49530fSBill Paul 		clnt_output(cmd.infile, "-DRPC_CLIENT", DONT_EXTEND,
169ff49530fSBill Paul 			    cmd.outfile);
170ff49530fSBill Paul 	} else if (cmd.makefileflag) {
171ff49530fSBill Paul 		mkfile_output(&cmd);
1724e115012SGarrett Wollman 	} else {
173ff49530fSBill Paul 		/* the rescans are required, since cpp may effect input */
1744e115012SGarrett Wollman 		c_output(cmd.infile, "-DRPC_XDR", EXTEND, "_xdr.c");
1754e115012SGarrett Wollman 		reinitialize();
176ec06b5e8SStefan Farfeleder 		h_output(cmd.infile, "-DRPC_HDR", EXTEND, ".h", cmd.hflag);
1774e115012SGarrett Wollman 		reinitialize();
1784e115012SGarrett Wollman 		l_output(cmd.infile, "-DRPC_CLNT", EXTEND, "_clnt.c");
1794e115012SGarrett Wollman 		reinitialize();
180ff49530fSBill Paul 		if (inetdflag || !tirpcflag)
1814e115012SGarrett Wollman 			s_output(allc, allv, cmd.infile, "-DRPC_SVC", EXTEND,
182ff49530fSBill Paul 			"_svc.c", cmd.mflag, cmd.nflag);
183ff49530fSBill Paul 		else
184ff49530fSBill Paul 			s_output(allnc, allnv, cmd.infile, "-DRPC_SVC",
185ff49530fSBill Paul 				EXTEND, "_svc.c", cmd.mflag, cmd.nflag);
186ff49530fSBill Paul 		if (tblflag) {
187ff49530fSBill Paul 			reinitialize();
188ff49530fSBill Paul 			t_output(cmd.infile, "-DRPC_TBL", EXTEND, "_tbl.i");
1894e115012SGarrett Wollman 		}
1904e115012SGarrett Wollman 
191ff49530fSBill Paul 		if (allfiles) {
192ff49530fSBill Paul 			reinitialize();
193ff49530fSBill Paul 			svc_output(cmd.infile, "-DRPC_SERVER", EXTEND,
194ff49530fSBill Paul 				"_server.c");
195ff49530fSBill Paul 			reinitialize();
196ff49530fSBill Paul 			clnt_output(cmd.infile, "-DRPC_CLIENT", EXTEND,
197ff49530fSBill Paul 				"_client.c");
198ff49530fSBill Paul 
199ff49530fSBill Paul 		}
200ff49530fSBill Paul 		if (allfiles || (cmd.makefileflag == 1)){
201ff49530fSBill Paul 			reinitialize();
202ff49530fSBill Paul 			mkfile_output(&cmd);
203ff49530fSBill Paul 		}
204ff49530fSBill Paul 
205ff49530fSBill Paul 	}
206ff49530fSBill Paul 	exit(nonfatalerrors);
207ff49530fSBill Paul 	/* NOTREACHED */
208ff49530fSBill Paul }
209ff49530fSBill Paul 
210ff49530fSBill Paul 
2114e115012SGarrett Wollman /*
212ff49530fSBill Paul  * add extension to filename
2134e115012SGarrett Wollman  */
2144e115012SGarrett Wollman static char *
215e390e3afSDavid Malone extendfile(const char *path, const char *ext)
2164e115012SGarrett Wollman {
2174e115012SGarrett Wollman 	char *res;
218e390e3afSDavid Malone 	const char *p;
219e390e3afSDavid Malone 	const char *file;
2204e115012SGarrett Wollman 
221b3608ae1SEd Schouten 	if ((file = strrchr(path, '/')) == NULL)
2225ec07232SNate Williams 		file = path;
2235ec07232SNate Williams 	else
2245ec07232SNate Williams 		file++;
22575863a6dSPhilippe Charnier 	res = xmalloc(strlen(file) + strlen(ext) + 1);
226ff49530fSBill Paul 	p = strrchr(file, '.');
2274e115012SGarrett Wollman 	if (p == NULL) {
2284e115012SGarrett Wollman 		p = file + strlen(file);
2294e115012SGarrett Wollman 	}
2304e115012SGarrett Wollman 	(void) strcpy(res, file);
2314e115012SGarrett Wollman 	(void) strcpy(res + (p - file), ext);
2324e115012SGarrett Wollman 	return (res);
2334e115012SGarrett Wollman }
2344e115012SGarrett Wollman 
2354e115012SGarrett Wollman /*
2364e115012SGarrett Wollman  * Open output file with given extension
2374e115012SGarrett Wollman  */
238526195adSJordan K. Hubbard static void
239e390e3afSDavid Malone open_output(const char *infile, const char *outfile)
2404e115012SGarrett Wollman {
241ff49530fSBill Paul 
2424e115012SGarrett Wollman 	if (outfile == NULL) {
2434e115012SGarrett Wollman 		fout = stdout;
2444e115012SGarrett Wollman 		return;
2454e115012SGarrett Wollman 	}
246ff49530fSBill Paul 
2474e115012SGarrett Wollman 	if (infile != NULL && streq(outfile, infile)) {
2480e76f40dSPhilippe Charnier 		warnx("%s already exists. No output generated", infile);
2494e115012SGarrett Wollman 		crash();
2504e115012SGarrett Wollman 	}
2514e115012SGarrett Wollman 	fout = fopen(outfile, "w");
2524e115012SGarrett Wollman 	if (fout == NULL) {
2530e76f40dSPhilippe Charnier 		warn("unable to open %s", outfile);
2544e115012SGarrett Wollman 		crash();
2554e115012SGarrett Wollman 	}
2564e115012SGarrett Wollman 	record_open(outfile);
257ff49530fSBill Paul 
258526195adSJordan K. Hubbard 	return;
259ff49530fSBill Paul }
260ff49530fSBill Paul 
261526195adSJordan K. Hubbard static void
262e390e3afSDavid Malone add_warning(void)
263ff49530fSBill Paul {
264ff49530fSBill Paul 	f_print(fout, "/*\n");
265ff49530fSBill Paul 	f_print(fout, " * Please do not edit this file.\n");
266ff49530fSBill Paul 	f_print(fout, " * It was generated using rpcgen.\n");
267ff49530fSBill Paul 	f_print(fout, " */\n\n");
268ff49530fSBill Paul }
269ff49530fSBill Paul 
2700dac0ed8SDimitry Andric /* prepend C-preprocessor and flags before arguments */
271e390e3afSDavid Malone static void
2720dac0ed8SDimitry Andric prepend_cpp(void)
273ff49530fSBill Paul {
274*587458b7SJessica Clarke 	int idx = 0, quoted;
275*587458b7SJessica Clarke 	const char *var, *s;
276*587458b7SJessica Clarke 	char *dupvar, *t, *word;
277ff49530fSBill Paul 
2780dac0ed8SDimitry Andric 	if (CPP != NULL)
2799d843ba3SJessica Clarke 		insarg(idx++, CPP);
2800dac0ed8SDimitry Andric 	else if ((var = getenv("RPCGEN_CPP")) == NULL)
2819d843ba3SJessica Clarke 		insarg(idx++, "/usr/bin/cpp");
2820dac0ed8SDimitry Andric 	else {
283*587458b7SJessica Clarke 		/*
284*587458b7SJessica Clarke 		 * Parse command line like a shell (but only handle whitespace,
285*587458b7SJessica Clarke 		 * quotes and backslash).
286*587458b7SJessica Clarke 		 */
287*587458b7SJessica Clarke 		dupvar = malloc(strlen(var) + 1);
288*587458b7SJessica Clarke 		quoted = 0;
289*587458b7SJessica Clarke 		word = NULL;
290*587458b7SJessica Clarke 		for (s = var, t = dupvar; *s; ++s) {
291*587458b7SJessica Clarke 			switch (quoted) {
292*587458b7SJessica Clarke 			/* Unquoted */
293*587458b7SJessica Clarke 			case 0:
294*587458b7SJessica Clarke 				switch (*s) {
295*587458b7SJessica Clarke 				case ' ':
296*587458b7SJessica Clarke 				case '\t':
297*587458b7SJessica Clarke 				case '\n':
298*587458b7SJessica Clarke 					if (word != NULL) {
299*587458b7SJessica Clarke 						*t++ = '\0';
300*587458b7SJessica Clarke 						insarg(idx++, word);
301*587458b7SJessica Clarke 						word = NULL;
302*587458b7SJessica Clarke 					}
303*587458b7SJessica Clarke 					break;
304*587458b7SJessica Clarke 				case '\'':
305*587458b7SJessica Clarke 					if (word == NULL)
306*587458b7SJessica Clarke 						word = t;
307*587458b7SJessica Clarke 					quoted = 1;
308*587458b7SJessica Clarke 					break;
309*587458b7SJessica Clarke 				case '"':
310*587458b7SJessica Clarke 					if (word == NULL)
311*587458b7SJessica Clarke 						word = t;
312*587458b7SJessica Clarke 					quoted = 2;
313*587458b7SJessica Clarke 					break;
314*587458b7SJessica Clarke 				case '\\':
315*587458b7SJessica Clarke 					switch (*(s + 1)) {
316*587458b7SJessica Clarke 					case '\0':
317*587458b7SJessica Clarke 						break;
318*587458b7SJessica Clarke 					case '\n':
319*587458b7SJessica Clarke 						++s;
320*587458b7SJessica Clarke 						continue;
321*587458b7SJessica Clarke 					default:
322*587458b7SJessica Clarke 						++s;
323*587458b7SJessica Clarke 						break;
324*587458b7SJessica Clarke 					}
325*587458b7SJessica Clarke 					/* FALLTHROUGH */
326*587458b7SJessica Clarke 				default:
327*587458b7SJessica Clarke 					if (word == NULL)
328*587458b7SJessica Clarke 						word = t;
329*587458b7SJessica Clarke 					*t++ = *s;
330*587458b7SJessica Clarke 					break;
331*587458b7SJessica Clarke 				}
332*587458b7SJessica Clarke 				break;
333*587458b7SJessica Clarke 
334*587458b7SJessica Clarke 			/* Single-quoted */
335*587458b7SJessica Clarke 			case 1:
336*587458b7SJessica Clarke 				switch (*s) {
337*587458b7SJessica Clarke 				case '\'':
338*587458b7SJessica Clarke 					quoted = 0;
339*587458b7SJessica Clarke 					break;
340*587458b7SJessica Clarke 				default:
341*587458b7SJessica Clarke 					*t++ = *s;
342*587458b7SJessica Clarke 					break;
343*587458b7SJessica Clarke 				}
344*587458b7SJessica Clarke 				break;
345*587458b7SJessica Clarke 
346*587458b7SJessica Clarke 			/* Double-quoted */
347*587458b7SJessica Clarke 			case 2:
348*587458b7SJessica Clarke 				switch (*s) {
349*587458b7SJessica Clarke 				case '"':
350*587458b7SJessica Clarke 					quoted = 0;
351*587458b7SJessica Clarke 					break;
352*587458b7SJessica Clarke 				case '\\':
353*587458b7SJessica Clarke 					switch (*(s + 1)) {
354*587458b7SJessica Clarke 					case '\0':
355*587458b7SJessica Clarke 						break;
356*587458b7SJessica Clarke 					case '$':
357*587458b7SJessica Clarke 					case '`':
358*587458b7SJessica Clarke 					case '"':
359*587458b7SJessica Clarke 					case '\\':
360*587458b7SJessica Clarke 						++s;
361*587458b7SJessica Clarke 						break;
362*587458b7SJessica Clarke 					case '\n':
363*587458b7SJessica Clarke 						++s;
364*587458b7SJessica Clarke 						continue;
365*587458b7SJessica Clarke 					default:
366*587458b7SJessica Clarke 						break;
367*587458b7SJessica Clarke 					}
368*587458b7SJessica Clarke 					/* FALLTHROUGH */
369*587458b7SJessica Clarke 				default:
370*587458b7SJessica Clarke 					*t++ = *s;
371*587458b7SJessica Clarke 					break;
372*587458b7SJessica Clarke 				}
373*587458b7SJessica Clarke 				break;
374*587458b7SJessica Clarke 			}
375*587458b7SJessica Clarke 		}
376*587458b7SJessica Clarke 		if (quoted)
377*587458b7SJessica Clarke 			errx(1, "RPCGEN_CPP: unterminated %c",
378*587458b7SJessica Clarke 			    quoted == 1 ? '\'' : '"');
379*587458b7SJessica Clarke 		if (word != NULL) {
380*587458b7SJessica Clarke 			*t++ = '\0';
381*587458b7SJessica Clarke 			insarg(idx++, word);
3820dac0ed8SDimitry Andric 		}
3830dac0ed8SDimitry Andric 		free(dupvar);
3840dac0ed8SDimitry Andric 	}
3850dac0ed8SDimitry Andric 
3860dac0ed8SDimitry Andric 	insarg(idx, CPPFLAGS);
3874e115012SGarrett Wollman }
3884e115012SGarrett Wollman 
3894e115012SGarrett Wollman /*
3904e115012SGarrett Wollman  * Open input file with given define for C-preprocessor
3914e115012SGarrett Wollman  */
392526195adSJordan K. Hubbard static void
393e390e3afSDavid Malone open_input(const char *infile, const char *define)
3944e115012SGarrett Wollman {
3954e115012SGarrett Wollman 	int pd[2];
3964e115012SGarrett Wollman 
3974e115012SGarrett Wollman 	infilename = (infile == NULL) ? "<stdin>" : infile;
3984e115012SGarrett Wollman 	(void) pipe(pd);
399ff49530fSBill Paul 	switch (childpid = fork()) {
4004e115012SGarrett Wollman 	case 0:
4010dac0ed8SDimitry Andric 		prepend_cpp();
402ff49530fSBill Paul 		addarg(define);
403ff49530fSBill Paul 		if (infile)
404ff49530fSBill Paul 			addarg(infile);
405ff49530fSBill Paul 		addarg((char *)NULL);
4064e115012SGarrett Wollman 		(void) close(1);
4074e115012SGarrett Wollman 		(void) dup2(pd[1], 1);
4084e115012SGarrett Wollman 		(void) close(pd[0]);
409005576f6SDimitry Andric 		execvp(arglist[0], arglist);
410005576f6SDimitry Andric 		err(1, "execvp %s", arglist[0]);
4114e115012SGarrett Wollman 	case -1:
41275863a6dSPhilippe Charnier 		err(1, "fork");
4134e115012SGarrett Wollman 	}
4144e115012SGarrett Wollman 	(void) close(pd[1]);
4154e115012SGarrett Wollman 	fin = fdopen(pd[0], "r");
4164e115012SGarrett Wollman 	if (fin == NULL) {
4170e76f40dSPhilippe Charnier 		warn("%s", infilename);
4184e115012SGarrett Wollman 		crash();
4194e115012SGarrett Wollman 	}
4204e115012SGarrett Wollman }
4214e115012SGarrett Wollman 
422ff49530fSBill Paul /* valid tirpc nettypes */
423e390e3afSDavid Malone static const char *valid_ti_nettypes[] =
424ff49530fSBill Paul {
425ff49530fSBill Paul 	"netpath",
426ff49530fSBill Paul 	"visible",
427ff49530fSBill Paul 	"circuit_v",
428ff49530fSBill Paul 	"datagram_v",
429ff49530fSBill Paul 	"circuit_n",
430ff49530fSBill Paul 	"datagram_n",
431ff49530fSBill Paul 	"udp",
432ff49530fSBill Paul 	"tcp",
433ff49530fSBill Paul 	"raw",
434ff49530fSBill Paul 	NULL
435ff49530fSBill Paul 	};
436ff49530fSBill Paul 
437ff49530fSBill Paul /* valid inetd nettypes */
438e390e3afSDavid Malone static const char *valid_i_nettypes[] =
439ff49530fSBill Paul {
440ff49530fSBill Paul 	"udp",
441ff49530fSBill Paul 	"tcp",
442ff49530fSBill Paul 	NULL
443ff49530fSBill Paul 	};
444ff49530fSBill Paul 
445e390e3afSDavid Malone static int
446e390e3afSDavid Malone check_nettype(const char *name, const char *list_to_check[])
447ff49530fSBill Paul {
448ff49530fSBill Paul 	int i;
449ff49530fSBill Paul 	for (i = 0; list_to_check[i] != NULL; i++) {
450ff49530fSBill Paul 		if (strcmp(name, list_to_check[i]) == 0) {
451ff49530fSBill Paul 			return (1);
452ff49530fSBill Paul 		}
453ff49530fSBill Paul 	}
4540e76f40dSPhilippe Charnier 	warnx("illegal nettype :\'%s\'", name);
455ff49530fSBill Paul 	return (0);
456ff49530fSBill Paul }
457ff49530fSBill Paul 
458e390e3afSDavid Malone static const char *
459e390e3afSDavid Malone file_name(const char *file, const char *ext)
460ff49530fSBill Paul {
461ff49530fSBill Paul 	char *temp;
462ff49530fSBill Paul 	temp = extendfile(file, ext);
463ff49530fSBill Paul 
464ff49530fSBill Paul 	if (access(temp, F_OK) != -1)
465ff49530fSBill Paul 		return (temp);
466ff49530fSBill Paul 	else
467e390e3afSDavid Malone 		return (" ");
468ff49530fSBill Paul 
469ff49530fSBill Paul }
470ff49530fSBill Paul 
471ff49530fSBill Paul 
472526195adSJordan K. Hubbard static void
473e390e3afSDavid Malone c_output(const char *infile, const char *define, int extend, const char *outfile)
4744e115012SGarrett Wollman {
4754e115012SGarrett Wollman 	definition *def;
4764e115012SGarrett Wollman 	char *include;
477e390e3afSDavid Malone 	const char *outfilename;
4784e115012SGarrett Wollman 	long tell;
4794e115012SGarrett Wollman 
480ff49530fSBill Paul 	c_initialize();
4814e115012SGarrett Wollman 	open_input(infile, define);
4824e115012SGarrett Wollman 	outfilename = extend ? extendfile(infile, outfile) : outfile;
4834e115012SGarrett Wollman 	open_output(infile, outfilename);
484ff49530fSBill Paul 	add_warning();
4854e115012SGarrett Wollman 	if (infile && (include = extendfile(infile, ".h"))) {
4864e115012SGarrett Wollman 		f_print(fout, "#include \"%s\"\n", include);
4874e115012SGarrett Wollman 		free(include);
488ff49530fSBill Paul 		/* .h file already contains rpc/rpc.h */
489ff49530fSBill Paul 	} else
490ff49530fSBill Paul 		f_print(fout, "#include <rpc/rpc.h>\n");
4914e115012SGarrett Wollman 	tell = ftell(fout);
492526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
4934e115012SGarrett Wollman 		emit(def);
4944e115012SGarrett Wollman 	}
4954e115012SGarrett Wollman 	if (extend && tell == ftell(fout)) {
4964e115012SGarrett Wollman 		(void) unlink(outfilename);
4974e115012SGarrett Wollman 	}
4984e115012SGarrett Wollman }
4994e115012SGarrett Wollman 
500ff49530fSBill Paul 
501526195adSJordan K. Hubbard void
502e390e3afSDavid Malone c_initialize(void)
503ff49530fSBill Paul {
504ff49530fSBill Paul 
505ff49530fSBill Paul 	/* add all the starting basic types */
506ff49530fSBill Paul 	add_type(1, "int");
507ff49530fSBill Paul 	add_type(1, "long");
508ff49530fSBill Paul 	add_type(1, "short");
509ff49530fSBill Paul 	add_type(1, "bool");
510ff49530fSBill Paul 	add_type(1, "u_int");
511ff49530fSBill Paul 	add_type(1, "u_long");
512ff49530fSBill Paul 	add_type(1, "u_short");
513ff49530fSBill Paul 
514ff49530fSBill Paul }
515ff49530fSBill Paul 
516bf70beceSEd Schouten static const char rpcgen_table_dcl[] = "struct rpcgen_table {\n\
517ff49530fSBill Paul 	char	*(*proc)(); \n\
518ff49530fSBill Paul 	xdrproc_t	xdr_arg; \n\
519ff49530fSBill Paul 	unsigned	len_arg; \n\
520ff49530fSBill Paul 	xdrproc_t	xdr_res; \n\
521ff49530fSBill Paul 	unsigned	len_res; \n\
522ff49530fSBill Paul }; \n";
523ff49530fSBill Paul 
524ff49530fSBill Paul 
525e390e3afSDavid Malone char *
526e390e3afSDavid Malone generate_guard(const char *pathname)
527ff49530fSBill Paul {
528e390e3afSDavid Malone 	const char *filename;
529e390e3afSDavid Malone 	char *guard, *tmp, *stopat;
530ff49530fSBill Paul 
531ff49530fSBill Paul 	filename = strrchr(pathname, '/');  /* find last component */
5327dbab955SMarcelo Araujo 	filename = ((filename == NULL) ? pathname : filename+1);
53375863a6dSPhilippe Charnier 	guard = xstrdup(filename);
534080f4020SSean Kelly 	stopat = strrchr(guard, '.');
535080f4020SSean Kelly 
536080f4020SSean Kelly 	/*
537080f4020SSean Kelly 	 * Convert to a valid C macro name and make it upper case.
538080f4020SSean Kelly 	 * Map macro unfriendly characterss to '_'.
53910c546c4SSean Kelly 	 */
540080f4020SSean Kelly 	for (tmp = guard; *tmp != '\000'; ++tmp) {
541ff49530fSBill Paul 		if (islower(*tmp))
542ff49530fSBill Paul 			*tmp = toupper(*tmp);
543080f4020SSean Kelly 		else if (isupper(*tmp) || *tmp == '_')
54410c546c4SSean Kelly 			/* OK for C */;
54510c546c4SSean Kelly 		else if (tmp == guard)
54610c546c4SSean Kelly 			*tmp = '_';
54710c546c4SSean Kelly 		else if (isdigit(*tmp))
54810c546c4SSean Kelly 			/* OK for all but first character */;
549080f4020SSean Kelly 		else if (tmp == stopat) {
550080f4020SSean Kelly 			*tmp = '\0';
55110c546c4SSean Kelly 			break;
55210c546c4SSean Kelly 		} else
55310c546c4SSean Kelly 			*tmp = '_';
55410c546c4SSean Kelly 	}
555080f4020SSean Kelly 	/*
556080f4020SSean Kelly 	 * Can't have a '_' in front, because it'll end up being "__".
557080f4020SSean Kelly 	 * "__" macros shoudln't be used. So, remove all of the
558080f4020SSean Kelly 	 * '_' characters from the front.
55910c546c4SSean Kelly 	 */
560080f4020SSean Kelly 	if (*guard == '_') {
561080f4020SSean Kelly 		for (tmp = guard; *tmp == '_'; ++tmp)
562080f4020SSean Kelly 			;
563080f4020SSean Kelly 		strcpy(guard, tmp);
564ff49530fSBill Paul 	}
565074170fbSWarner Losh 	tmp = guard;
566ff49530fSBill Paul 	guard = extendfile(guard, "_H_RPCGEN");
567074170fbSWarner Losh 	free(tmp);
568ff49530fSBill Paul 	return (guard);
569ff49530fSBill Paul }
570ff49530fSBill Paul 
5714e115012SGarrett Wollman /*
5724e115012SGarrett Wollman  * Compile into an XDR header file
5734e115012SGarrett Wollman  */
574ff49530fSBill Paul 
575ff49530fSBill Paul 
576526195adSJordan K. Hubbard static void
577e390e3afSDavid Malone h_output(const char *infile, const char *define, int extend, const char *outfile, int headeronly)
5784e115012SGarrett Wollman {
5794e115012SGarrett Wollman 	definition *def;
580e390e3afSDavid Malone 	const char *outfilename;
5814e115012SGarrett Wollman 	long tell;
582e390e3afSDavid Malone 	const char *guard;
583ff49530fSBill Paul 	list *l;
584ff49530fSBill Paul 	xdrfunc *xdrfuncp;
585074170fbSWarner Losh 	void *tmp = NULL;
5864e115012SGarrett Wollman 
5874e115012SGarrett Wollman 	open_input(infile, define);
5884e115012SGarrett Wollman 	outfilename =  extend ? extendfile(infile, outfile) : outfile;
5894e115012SGarrett Wollman 	open_output(infile, outfilename);
590ff49530fSBill Paul 	add_warning();
591ff49530fSBill Paul 	if (outfilename || infile){
592074170fbSWarner Losh 		guard = tmp = generate_guard(outfilename ? outfilename: infile);
593ff49530fSBill Paul 	} else
594ff49530fSBill Paul 		guard = "STDIN_";
595ff49530fSBill Paul 
596ff49530fSBill Paul 	f_print(fout, "#ifndef _%s\n#define	_%s\n\n", guard,
597ff49530fSBill Paul 		guard);
598ff49530fSBill Paul 
599ff49530fSBill Paul 	f_print(fout, "#include <rpc/rpc.h>\n");
600ff49530fSBill Paul 
60140ad8885SAlfred Perlstein 	if (mtflag)
60235ab9586SDavid E. O'Brien 		f_print(fout, "#include <pthread.h>\n");
603ff49530fSBill Paul 
604ff49530fSBill Paul 	/* put the C++ support */
60515df5e2dSStefan Farfeleder 	if (!CCflag) {
606ff49530fSBill Paul 		f_print(fout, "\n#ifdef __cplusplus\n");
607ff49530fSBill Paul 		f_print(fout, "extern \"C\" {\n");
608ff49530fSBill Paul 		f_print(fout, "#endif\n\n");
609ff49530fSBill Paul 	}
610ff49530fSBill Paul 
611ff49530fSBill Paul 	/* put in a typedef for quadprecision. Only with Cflag */
612ff49530fSBill Paul 
6134e115012SGarrett Wollman 	tell = ftell(fout);
614ff49530fSBill Paul 
615ff49530fSBill Paul 	/* print data definitions */
616526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
617ec06b5e8SStefan Farfeleder 		print_datadef(def, headeronly);
6184e115012SGarrett Wollman 	}
619ff49530fSBill Paul 
620ff49530fSBill Paul 	/*
621ff49530fSBill Paul 	 * print function declarations.
622ff49530fSBill Paul 	 *  Do this after data definitions because they might be used as
623ff49530fSBill Paul 	 *  arguments for functions
624ff49530fSBill Paul 	 */
625ff49530fSBill Paul 	for (l = defined; l != NULL; l = l->next) {
626ec06b5e8SStefan Farfeleder 		print_funcdef(l->val, headeronly);
627ff49530fSBill Paul 	}
628ff49530fSBill Paul 	/* Now  print all xdr func declarations */
629ff49530fSBill Paul 	if (xdrfunc_head != NULL){
630ff49530fSBill Paul 
631ff49530fSBill Paul 		f_print(fout,
632ff49530fSBill Paul 			"\n/* the xdr functions */\n");
633ff49530fSBill Paul 
634ff49530fSBill Paul 		if (CCflag){
635ff49530fSBill Paul 			f_print(fout, "\n#ifdef __cplusplus\n");
636ff49530fSBill Paul 			f_print(fout, "extern \"C\" {\n");
637ff49530fSBill Paul 			f_print(fout, "#endif\n");
638ff49530fSBill Paul 		}
639ff49530fSBill Paul 
640ff49530fSBill Paul 		xdrfuncp = xdrfunc_head;
641ff49530fSBill Paul 		while (xdrfuncp != NULL){
64215df5e2dSStefan Farfeleder 			print_xdr_func_def(xdrfuncp->name, xdrfuncp->pointerp);
643ff49530fSBill Paul 			xdrfuncp = xdrfuncp->next;
644ff49530fSBill Paul 		}
645ff49530fSBill Paul 	}
646ff49530fSBill Paul 
6474e115012SGarrett Wollman 	if (extend && tell == ftell(fout)) {
6484e115012SGarrett Wollman 		(void) unlink(outfilename);
649ff49530fSBill Paul 	} else if (tblflag) {
650ff49530fSBill Paul 		f_print(fout, rpcgen_table_dcl);
6514e115012SGarrett Wollman 	}
652ff49530fSBill Paul 
653ff49530fSBill Paul 	f_print(fout, "\n#ifdef __cplusplus\n");
654ff49530fSBill Paul 	f_print(fout, "}\n");
655ff49530fSBill Paul 	f_print(fout, "#endif\n");
656ff49530fSBill Paul 
657ff49530fSBill Paul 	f_print(fout, "\n#endif /* !_%s */\n", guard);
658074170fbSWarner Losh 	free(tmp);
6594e115012SGarrett Wollman }
6604e115012SGarrett Wollman 
6614e115012SGarrett Wollman /*
6624e115012SGarrett Wollman  * Compile into an RPC service
6634e115012SGarrett Wollman  */
664526195adSJordan K. Hubbard static void
665e390e3afSDavid Malone s_output(int argc, const char *argv[], const char *infile, const char *define,
666e390e3afSDavid Malone     int extend, const char *outfile, int nomain, int netflag)
6674e115012SGarrett Wollman {
6684e115012SGarrett Wollman 	char *include;
6694e115012SGarrett Wollman 	definition *def;
670ff49530fSBill Paul 	int foundprogram = 0;
671e390e3afSDavid Malone 	const char *outfilename;
6724e115012SGarrett Wollman 
6734e115012SGarrett Wollman 	open_input(infile, define);
6744e115012SGarrett Wollman 	outfilename = extend ? extendfile(infile, outfile) : outfile;
6754e115012SGarrett Wollman 	open_output(infile, outfilename);
676ff49530fSBill Paul 	add_warning();
6774e115012SGarrett Wollman 	if (infile && (include = extendfile(infile, ".h"))) {
6784e115012SGarrett Wollman 		f_print(fout, "#include \"%s\"\n", include);
6794e115012SGarrett Wollman 		free(include);
680ff49530fSBill Paul 	} else
681ff49530fSBill Paul 		f_print(fout, "#include <rpc/rpc.h>\n");
682ff49530fSBill Paul 
683ff49530fSBill Paul 	f_print(fout, "#include <stdio.h>\n");
684ff49530fSBill Paul 	f_print(fout, "#include <stdlib.h> /* getenv, exit */\n");
68515df5e2dSStefan Farfeleder 	f_print (fout, "#include <rpc/pmap_clnt.h> /* for pmap_unset */\n");
686ff49530fSBill Paul 	f_print (fout, "#include <string.h> /* strcmp */\n");
687bcb53b16SMartin Blapp 	if (tirpcflag)
688bcb53b16SMartin Blapp 		f_print(fout, "#include <rpc/rpc_com.h>\n");
689ff49530fSBill Paul 	if (strcmp(svcclosetime, "-1") == 0)
690ff49530fSBill Paul 		indefinitewait = 1;
691ff49530fSBill Paul 	else if (strcmp(svcclosetime, "0") == 0)
692ff49530fSBill Paul 		exitnow = 1;
693ff49530fSBill Paul 	else if (inetdflag || pmflag) {
694ff49530fSBill Paul 		f_print(fout, "#include <signal.h>\n");
695ff49530fSBill Paul 		timerflag = 1;
696ff49530fSBill Paul 	}
697ff49530fSBill Paul 
698ff49530fSBill Paul 	if (!tirpcflag && inetdflag)
699ff49530fSBill Paul 		f_print(fout, "#include <sys/ttycom.h> /* TIOCNOTTY */\n");
70015df5e2dSStefan Farfeleder 	if (inetdflag || pmflag) {
701ff49530fSBill Paul 		f_print(fout, "#ifdef __cplusplus\n");
702ff49530fSBill Paul 		f_print(fout,
703f87730d4SXin LI 			"#include <sys/sysent.h> /* getdtablesize, open */\n");
704ff49530fSBill Paul 		f_print(fout, "#endif /* __cplusplus */\n");
705ff49530fSBill Paul 	}
70640ad8885SAlfred Perlstein 	if (tirpcflag) {
70740ad8885SAlfred Perlstein 		f_print(fout, "#include <fcntl.h> /* open */\n");
70840ad8885SAlfred Perlstein 		f_print(fout, "#include <unistd.h> /* fork / setsid */\n");
709ff49530fSBill Paul 		f_print(fout, "#include <sys/types.h>\n");
71040ad8885SAlfred Perlstein 	}
711ff49530fSBill Paul 
7124f83fd19SStefan Farfeleder 	f_print(fout, "#include <string.h>\n");
713ff49530fSBill Paul 	if (inetdflag || !tirpcflag) {
714ff49530fSBill Paul 		f_print(fout, "#include <sys/socket.h>\n");
715ff49530fSBill Paul 		f_print(fout, "#include <netinet/in.h>\n");
716ff49530fSBill Paul 	}
717ff49530fSBill Paul 
718ff49530fSBill Paul 	if ((netflag || pmflag) && tirpcflag && !nomain) {
719ff49530fSBill Paul 		f_print(fout, "#include <netconfig.h>\n");
720ff49530fSBill Paul 	}
721ff49530fSBill Paul 	if (tirpcflag)
722ff49530fSBill Paul 		f_print(fout, "#include <sys/resource.h> /* rlimit */\n");
72340ad8885SAlfred Perlstein 	if (logflag || inetdflag || pmflag || tirpcflag)
724ff49530fSBill Paul 		f_print(fout, "#include <syslog.h>\n");
725ff49530fSBill Paul 
726ff49530fSBill Paul 	f_print(fout, "\n#ifdef DEBUG\n#define	RPC_SVC_FG\n#endif\n");
727ff49530fSBill Paul 	if (timerflag)
728ff49530fSBill Paul 		f_print(fout, "\n#define	_RPCSVC_CLOSEDOWN %s\n",
729ff49530fSBill Paul 			svcclosetime);
730526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
7314e115012SGarrett Wollman 		foundprogram |= (def->def_kind == DEF_PROGRAM);
7324e115012SGarrett Wollman 	}
7334e115012SGarrett Wollman 	if (extend && !foundprogram) {
7344e115012SGarrett Wollman 		(void) unlink(outfilename);
7354e115012SGarrett Wollman 		return;
7364e115012SGarrett Wollman 	}
737ff49530fSBill Paul 	write_most(infile, netflag, nomain);
738ff49530fSBill Paul 	if (!nomain) {
739ff49530fSBill Paul 		if (!do_registers(argc, argv)) {
740ff49530fSBill Paul 			if (outfilename)
741ff49530fSBill Paul 				(void) unlink(outfilename);
742ff49530fSBill Paul 			usage();
743ff49530fSBill Paul 		}
7444e115012SGarrett Wollman 		write_rest();
7454e115012SGarrett Wollman 	}
7464e115012SGarrett Wollman }
7474e115012SGarrett Wollman 
748ff49530fSBill Paul /*
749ff49530fSBill Paul  * generate client side stubs
750ff49530fSBill Paul  */
751526195adSJordan K. Hubbard static void
752e390e3afSDavid Malone l_output(const char *infile, const char *define, int extend, const char *outfile)
7534e115012SGarrett Wollman {
7544e115012SGarrett Wollman 	char *include;
7554e115012SGarrett Wollman 	definition *def;
756ff49530fSBill Paul 	int foundprogram = 0;
757e390e3afSDavid Malone 	const char *outfilename;
7584e115012SGarrett Wollman 
7594e115012SGarrett Wollman 	open_input(infile, define);
7604e115012SGarrett Wollman 	outfilename = extend ? extendfile(infile, outfile) : outfile;
7614e115012SGarrett Wollman 	open_output(infile, outfilename);
762ff49530fSBill Paul 	add_warning();
7634f83fd19SStefan Farfeleder 	f_print (fout, "#include <string.h> /* for memset */\n");
7644e115012SGarrett Wollman 	if (infile && (include = extendfile(infile, ".h"))) {
7654e115012SGarrett Wollman 		f_print(fout, "#include \"%s\"\n", include);
7664e115012SGarrett Wollman 		free(include);
767ff49530fSBill Paul 	} else
768ff49530fSBill Paul 		f_print(fout, "#include <rpc/rpc.h>\n");
769526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
7704e115012SGarrett Wollman 		foundprogram |= (def->def_kind == DEF_PROGRAM);
7714e115012SGarrett Wollman 	}
7724e115012SGarrett Wollman 	if (extend && !foundprogram) {
7734e115012SGarrett Wollman 		(void) unlink(outfilename);
7744e115012SGarrett Wollman 		return;
7754e115012SGarrett Wollman 	}
7764e115012SGarrett Wollman 	write_stubs();
7774e115012SGarrett Wollman }
7784e115012SGarrett Wollman 
7794e115012SGarrett Wollman /*
780ff49530fSBill Paul  * generate the dispatch table
7814e115012SGarrett Wollman  */
782526195adSJordan K. Hubbard static void
783e390e3afSDavid Malone t_output(const char *infile, const char *define, int extend, const char *outfile)
784ff49530fSBill Paul {
785ff49530fSBill Paul 	definition *def;
786ff49530fSBill Paul 	int foundprogram = 0;
787e390e3afSDavid Malone 	const char *outfilename;
788ff49530fSBill Paul 
789ff49530fSBill Paul 	open_input(infile, define);
790ff49530fSBill Paul 	outfilename = extend ? extendfile(infile, outfile) : outfile;
791ff49530fSBill Paul 	open_output(infile, outfilename);
792ff49530fSBill Paul 	add_warning();
793526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
794ff49530fSBill Paul 		foundprogram |= (def->def_kind == DEF_PROGRAM);
795ff49530fSBill Paul 	}
796ff49530fSBill Paul 	if (extend && !foundprogram) {
797ff49530fSBill Paul 		(void) unlink(outfilename);
798ff49530fSBill Paul 		return;
799ff49530fSBill Paul 	}
800ff49530fSBill Paul 	write_tables();
801ff49530fSBill Paul }
802ff49530fSBill Paul 
803ff49530fSBill Paul /* sample routine for the server template */
804526195adSJordan K. Hubbard static void
805e390e3afSDavid Malone svc_output(const char *infile, const char *define, int extend, const char *outfile)
806ff49530fSBill Paul {
807ff49530fSBill Paul 	definition *def;
808ff49530fSBill Paul 	char *include;
809e390e3afSDavid Malone 	const char *outfilename;
810ff49530fSBill Paul 	long tell;
811ff49530fSBill Paul 	open_input(infile, define);
812ff49530fSBill Paul 	outfilename = extend ? extendfile(infile, outfile) : outfile;
813ff49530fSBill Paul 	checkfiles(infile, outfilename);
814ff49530fSBill Paul 	/*
815ff49530fSBill Paul 	 * Check if outfile already exists.
816ff49530fSBill Paul 	 * if so, print an error message and exit
817ff49530fSBill Paul 	 */
818ff49530fSBill Paul 	open_output(infile, outfilename);
819ff49530fSBill Paul 	add_sample_msg();
820ff49530fSBill Paul 
821ff49530fSBill Paul 	if (infile && (include = extendfile(infile, ".h"))) {
822ff49530fSBill Paul 		f_print(fout, "#include \"%s\"\n", include);
823ff49530fSBill Paul 		free(include);
824ff49530fSBill Paul 	} else
825ff49530fSBill Paul 		f_print(fout, "#include <rpc/rpc.h>\n");
826ff49530fSBill Paul 
827ff49530fSBill Paul 	tell = ftell(fout);
828526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
829ff49530fSBill Paul 		write_sample_svc(def);
830ff49530fSBill Paul 	}
831ff49530fSBill Paul 	if (extend && tell == ftell(fout)) {
832ff49530fSBill Paul 		(void) unlink(outfilename);
833ff49530fSBill Paul 	}
834ff49530fSBill Paul }
835ff49530fSBill Paul 
836ff49530fSBill Paul /* sample main routine for client */
837526195adSJordan K. Hubbard static void
838e390e3afSDavid Malone clnt_output(const char *infile, const char *define, int extend, const char *outfile)
839ff49530fSBill Paul {
840ff49530fSBill Paul 	definition *def;
841ff49530fSBill Paul 	char *include;
842e390e3afSDavid Malone 	const char *outfilename;
843ff49530fSBill Paul 	long tell;
844ff49530fSBill Paul 	int has_program = 0;
845ff49530fSBill Paul 
846ff49530fSBill Paul 	open_input(infile, define);
847ff49530fSBill Paul 	outfilename = extend ? extendfile(infile, outfile) : outfile;
848ff49530fSBill Paul 	checkfiles(infile, outfilename);
849ff49530fSBill Paul 	/*
850ff49530fSBill Paul 	 * Check if outfile already exists.
851ff49530fSBill Paul 	 * if so, print an error message and exit
852ff49530fSBill Paul 	 */
853ff49530fSBill Paul 
854ff49530fSBill Paul 	open_output(infile, outfilename);
855ff49530fSBill Paul 	add_sample_msg();
856ff49530fSBill Paul 	if (infile && (include = extendfile(infile, ".h"))) {
857ff49530fSBill Paul 		f_print(fout, "#include \"%s\"\n", include);
858ff49530fSBill Paul 		free(include);
859ff49530fSBill Paul 	} else
860ff49530fSBill Paul 		f_print(fout, "#include <rpc/rpc.h>\n");
8617c31e86bSDoug Rabson 	f_print(fout, "#include <stdio.h>\n");
8627c31e86bSDoug Rabson 	f_print(fout, "#include <stdlib.h>\n");
863ff49530fSBill Paul 	tell = ftell(fout);
864526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
865ff49530fSBill Paul 		has_program += write_sample_clnt(def);
866ff49530fSBill Paul 	}
867ff49530fSBill Paul 
868ff49530fSBill Paul 	if (has_program)
869ff49530fSBill Paul 		write_sample_clnt_main();
870ff49530fSBill Paul 
871ff49530fSBill Paul 	if (extend && tell == ftell(fout)) {
872ff49530fSBill Paul 		(void) unlink(outfilename);
873ff49530fSBill Paul 	}
874ff49530fSBill Paul }
875ff49530fSBill Paul 
876ff49530fSBill Paul 
877e390e3afSDavid Malone static void mkfile_output(struct commandline *cmd)
878ff49530fSBill Paul {
879e390e3afSDavid Malone 	const char *mkfilename, *clientname, *clntname, *xdrname, *hdrname;
880e390e3afSDavid Malone 	const char *servername, *svcname, *servprogname, *clntprogname;
881e390e3afSDavid Malone 	char *temp, *mkftemp;
882ff49530fSBill Paul 
883ff49530fSBill Paul 	svcname = file_name(cmd->infile, "_svc.c");
884ff49530fSBill Paul 	clntname = file_name(cmd->infile, "_clnt.c");
885ff49530fSBill Paul 	xdrname = file_name(cmd->infile, "_xdr.c");
886ff49530fSBill Paul 	hdrname = file_name(cmd->infile, ".h");
887ff49530fSBill Paul 
888ff49530fSBill Paul 
889ff49530fSBill Paul 	if (allfiles){
890ff49530fSBill Paul 		servername = extendfile(cmd->infile, "_server.c");
891ff49530fSBill Paul 		clientname = extendfile(cmd->infile, "_client.c");
892ff49530fSBill Paul 	}else{
893ff49530fSBill Paul 		servername = " ";
894ff49530fSBill Paul 		clientname = " ";
895ff49530fSBill Paul 	}
896ff49530fSBill Paul 	servprogname = extendfile(cmd->infile, "_server");
897ff49530fSBill Paul 	clntprogname = extendfile(cmd->infile, "_client");
898ff49530fSBill Paul 
899ff49530fSBill Paul 	if (allfiles){
900e390e3afSDavid Malone 		mkftemp = xmalloc(strlen("makefile.") +
901ff49530fSBill Paul 		                     strlen(cmd->infile) + 1);
902b3608ae1SEd Schouten 		temp = strrchr(cmd->infile, '.');
903e390e3afSDavid Malone 		strcpy(mkftemp, "makefile.");
904e390e3afSDavid Malone 		(void) strncat(mkftemp, cmd->infile,
905ff49530fSBill Paul 			(temp - cmd->infile));
906e390e3afSDavid Malone 		mkfilename = mkftemp;
907ff49530fSBill Paul 	} else
908ff49530fSBill Paul 		mkfilename = cmd->outfile;
909ff49530fSBill Paul 
910ff49530fSBill Paul 
911ff49530fSBill Paul 	checkfiles(NULL, mkfilename);
912ff49530fSBill Paul 	open_output(NULL, mkfilename);
913ff49530fSBill Paul 
914ff49530fSBill Paul 	f_print(fout, "\n# This is a template makefile generated\
915ff49530fSBill Paul 		by rpcgen \n");
916ff49530fSBill Paul 
917ff49530fSBill Paul 	f_print(fout, "\n# Parameters \n\n");
918ff49530fSBill Paul 
919ff49530fSBill Paul 	f_print(fout, "CLIENT = %s\nSERVER = %s\n\n",
920ff49530fSBill Paul 		clntprogname, servprogname);
921ff49530fSBill Paul 	f_print(fout, "SOURCES_CLNT.c = \nSOURCES_CLNT.h = \n");
922ff49530fSBill Paul 	f_print(fout, "SOURCES_SVC.c = \nSOURCES_SVC.h = \n");
923ff49530fSBill Paul 	f_print(fout, "SOURCES.x = %s\n\n", cmd->infile);
924ff49530fSBill Paul 	f_print(fout, "TARGETS_SVC.c = %s %s %s \n",
925ff49530fSBill Paul 		svcname, servername, xdrname);
926ff49530fSBill Paul 	f_print(fout, "TARGETS_CLNT.c = %s %s %s \n",
927ff49530fSBill Paul 		clntname, clientname, xdrname);
928ff49530fSBill Paul 	f_print(fout, "TARGETS = %s %s %s %s %s %s\n\n",
929ff49530fSBill Paul 		hdrname, xdrname, clntname,
930ff49530fSBill Paul 		svcname, clientname, servername);
931ff49530fSBill Paul 
932ff49530fSBill Paul 	f_print(fout, "OBJECTS_CLNT = $(SOURCES_CLNT.c:%%.c=%%.o) \
933ff49530fSBill Paul $(TARGETS_CLNT.c:%%.c=%%.o) ");
934ff49530fSBill Paul 
935ff49530fSBill Paul 	f_print(fout, "\nOBJECTS_SVC = $(SOURCES_SVC.c:%%.c=%%.o) \
936ff49530fSBill Paul $(TARGETS_SVC.c:%%.c=%%.o) ");
937ff49530fSBill Paul 
938ff49530fSBill Paul 
939ff49530fSBill Paul 	f_print(fout, "\n# Compiler flags \n");
940ff49530fSBill Paul 	if (mtflag)
94140ad8885SAlfred Perlstein 		f_print(fout, "\nCFLAGS += -D_REENTRANT -D_THEAD_SAFE \nLDLIBS += -pthread\n");
94240ad8885SAlfred Perlstein 
943ff49530fSBill Paul 	f_print(fout, "RPCGENFLAGS = \n");
944ff49530fSBill Paul 
945ff49530fSBill Paul 	f_print(fout, "\n# Targets \n\n");
946ff49530fSBill Paul 
947ff49530fSBill Paul 	f_print(fout, "all : $(CLIENT) $(SERVER)\n\n");
948ff49530fSBill Paul 	f_print(fout, "$(TARGETS) : $(SOURCES.x) \n");
949ff49530fSBill Paul 	f_print(fout, "\trpcgen $(RPCGENFLAGS) $(SOURCES.x)\n\n");
9507c31e86bSDoug Rabson 	if (allfiles) {
9517c31e86bSDoug Rabson 		f_print(fout, "\trpcgen -Sc $(RPCGENFLAGS) $(SOURCES.x) -o %s\n\n", clientname);
9527c31e86bSDoug Rabson 		f_print(fout, "\trpcgen -Ss $(RPCGENFLAGS) $(SOURCES.x) -o %s\n\n", servername);
9537c31e86bSDoug Rabson 	}
954ff49530fSBill Paul 	f_print(fout, "$(OBJECTS_CLNT) : $(SOURCES_CLNT.c) $(SOURCES_CLNT.h) \
955ff49530fSBill Paul $(TARGETS_CLNT.c) \n\n");
956ff49530fSBill Paul 
957ff49530fSBill Paul 	f_print(fout, "$(OBJECTS_SVC) : $(SOURCES_SVC.c) $(SOURCES_SVC.h) \
958ff49530fSBill Paul $(TARGETS_SVC.c) \n\n");
959ff49530fSBill Paul 	f_print(fout, "$(CLIENT) : $(OBJECTS_CLNT) \n");
960ff49530fSBill Paul 	f_print(fout, "\t$(CC) -o $(CLIENT) $(OBJECTS_CLNT) \
961ff49530fSBill Paul $(LDLIBS) \n\n");
962ff49530fSBill Paul 	f_print(fout, "$(SERVER) : $(OBJECTS_SVC) \n");
963ff49530fSBill Paul 	f_print(fout, "\t$(CC) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS)\n\n");
96436752498SBryan Drewery 	f_print(fout, "clean:\n\t rm -f core $(TARGETS) $(OBJECTS_CLNT) \
965ff49530fSBill Paul $(OBJECTS_SVC) $(CLIENT) $(SERVER)\n\n");
966ff49530fSBill Paul }
967ff49530fSBill Paul 
968ff49530fSBill Paul 
969ff49530fSBill Paul 
970ff49530fSBill Paul /*
971ff49530fSBill Paul  * Perform registrations for service output
972ff49530fSBill Paul  * Return 0 if failed; 1 otherwise.
973ff49530fSBill Paul  */
974526195adSJordan K. Hubbard static int
975e390e3afSDavid Malone do_registers(int argc, const char *argv[])
9764e115012SGarrett Wollman {
9774e115012SGarrett Wollman 	int i;
9784e115012SGarrett Wollman 
979ff49530fSBill Paul 	if (inetdflag || !tirpcflag) {
9804e115012SGarrett Wollman 		for (i = 1; i < argc; i++) {
9814e115012SGarrett Wollman 			if (streq(argv[i], "-s")) {
982ff49530fSBill Paul 				if (!check_nettype(argv[i + 1],
983ff49530fSBill Paul 						    valid_i_nettypes))
984ff49530fSBill Paul 					return (0);
985ff49530fSBill Paul 				write_inetd_register(argv[i + 1]);
9864e115012SGarrett Wollman 				i++;
9874e115012SGarrett Wollman 			}
9884e115012SGarrett Wollman 		}
989ff49530fSBill Paul 	} else {
990ff49530fSBill Paul 		for (i = 1; i < argc; i++)
991ff49530fSBill Paul 			if (streq(argv[i], "-s")) {
992ff49530fSBill Paul 				if (!check_nettype(argv[i + 1],
993ff49530fSBill Paul 						    valid_ti_nettypes))
994ff49530fSBill Paul 					return (0);
995ff49530fSBill Paul 				write_nettype_register(argv[i + 1]);
996ff49530fSBill Paul 				i++;
997ff49530fSBill Paul 			} else if (streq(argv[i], "-n")) {
998ff49530fSBill Paul 				write_netid_register(argv[i + 1]);
999ff49530fSBill Paul 				i++;
1000ff49530fSBill Paul 			}
1001ff49530fSBill Paul 	}
1002ff49530fSBill Paul 	return (1);
1003ff49530fSBill Paul }
1004ff49530fSBill Paul 
1005ff49530fSBill Paul /*
10067c8b268aSBrooks Davis  * Extend the argument list
10077c8b268aSBrooks Davis  */
10087c8b268aSBrooks Davis static void
10097c8b268aSBrooks Davis moreargs(void)
10107c8b268aSBrooks Davis {
10117c8b268aSBrooks Davis 	char **newarglist;
10127c8b268aSBrooks Davis 
10137c8b268aSBrooks Davis 	argmax = argmax == 0 ? 32 : argmax << 1;
10147c8b268aSBrooks Davis 	if (argmax > INT_MAX / 4) {
10157c8b268aSBrooks Davis 		warnx("refusing to allocate too many arguments");
10167c8b268aSBrooks Davis 		crash();
10177c8b268aSBrooks Davis 	}
10187c8b268aSBrooks Davis 	newarglist = realloc(arglist, argmax * sizeof(*arglist));
10197c8b268aSBrooks Davis 	if (newarglist == NULL) {
10207c8b268aSBrooks Davis 		warnx("unable to allocate arglist");
10217c8b268aSBrooks Davis 		crash();
10227c8b268aSBrooks Davis 	}
10237c8b268aSBrooks Davis 	arglist = newarglist;
10247c8b268aSBrooks Davis }
10257c8b268aSBrooks Davis 
10267c8b268aSBrooks Davis /*
1027ff49530fSBill Paul  * Add another argument to the arg list
1028ff49530fSBill Paul  */
1029ff49530fSBill Paul static void
1030e390e3afSDavid Malone addarg(const char *cp)
1031ff49530fSBill Paul {
10327c8b268aSBrooks Davis 	if (argcount >= argmax)
10337c8b268aSBrooks Davis 		moreargs();
10347c8b268aSBrooks Davis 
10358cb6926eSCraig Rodrigues 	if (cp != NULL)
10368cb6926eSCraig Rodrigues 		arglist[argcount++] = xstrdup(cp);
10378cb6926eSCraig Rodrigues 	else
10388cb6926eSCraig Rodrigues 		arglist[argcount++] = NULL;
1039ff49530fSBill Paul }
1040ff49530fSBill Paul 
10410dac0ed8SDimitry Andric /*
10420dac0ed8SDimitry Andric  * Insert an argument at the specified location
10430dac0ed8SDimitry Andric  */
1044ff49530fSBill Paul static void
10450dac0ed8SDimitry Andric insarg(int place, const char *cp)
1046ff49530fSBill Paul {
10470dac0ed8SDimitry Andric 	int i;
10480dac0ed8SDimitry Andric 
10497c8b268aSBrooks Davis 	if (argcount >= argmax)
10507c8b268aSBrooks Davis 		moreargs();
10510dac0ed8SDimitry Andric 
10520dac0ed8SDimitry Andric 	/* Move up existing arguments */
1053ade215fdSDimitry Andric 	for (i = argcount - 1; i >= place; i--)
10540dac0ed8SDimitry Andric 		arglist[i + 1] = arglist[i];
10550dac0ed8SDimitry Andric 
10568cb6926eSCraig Rodrigues 	arglist[place] = xstrdup(cp);
10570dac0ed8SDimitry Andric 	argcount++;
1058ff49530fSBill Paul }
1059ff49530fSBill Paul 
1060ff49530fSBill Paul /*
1061ff49530fSBill Paul  * if input file is stdin and an output file is specified then complain
1062ff49530fSBill Paul  * if the file already exists. Otherwise the file may get overwritten
1063ff49530fSBill Paul  * If input file does not exist, exit with an error
1064ff49530fSBill Paul  */
1065ff49530fSBill Paul 
1066ff49530fSBill Paul static void
1067e390e3afSDavid Malone checkfiles(const char *infile, const char *outfile)
1068ff49530fSBill Paul {
1069ff49530fSBill Paul 
1070ff49530fSBill Paul 	struct stat buf;
1071ff49530fSBill Paul 
1072ff49530fSBill Paul 	if (infile)		/* infile ! = NULL */
1073ff49530fSBill Paul 		if (stat(infile, &buf) < 0)
1074ff49530fSBill Paul 		{
10750e76f40dSPhilippe Charnier 			warn("%s", infile);
1076ff49530fSBill Paul 			crash();
107780c7cc1cSPedro F. Giffuni 		}
1078ff49530fSBill Paul 	if (outfile) {
1079ff49530fSBill Paul 		if (stat(outfile, &buf) < 0)
1080ff49530fSBill Paul 			return;	/* file does not exist */
1081ff49530fSBill Paul 		else {
10820e76f40dSPhilippe Charnier 			warnx("file '%s' already exists and may be overwritten", outfile);
1083ff49530fSBill Paul 			crash();
1084ff49530fSBill Paul 		}
1085ff49530fSBill Paul 	}
10864e115012SGarrett Wollman }
10874e115012SGarrett Wollman 
10884e115012SGarrett Wollman /*
10894e115012SGarrett Wollman  * Parse command line arguments
10904e115012SGarrett Wollman  */
1091526195adSJordan K. Hubbard static int
1092e390e3afSDavid Malone parseargs(int argc, const char *argv[], struct commandline *cmd)
10934e115012SGarrett Wollman {
10944e115012SGarrett Wollman 	int i;
10954e115012SGarrett Wollman 	int j;
1096ff49530fSBill Paul 	char c, ch;
10974e115012SGarrett Wollman 	char flag[(1 << 8 * sizeof (char))];
10984e115012SGarrett Wollman 	int nflags;
10994e115012SGarrett Wollman 
11004e115012SGarrett Wollman 	cmd->infile = cmd->outfile = NULL;
11014e115012SGarrett Wollman 	if (argc < 2) {
11024e115012SGarrett Wollman 		return (0);
11034e115012SGarrett Wollman 	}
1104ff49530fSBill Paul 	allfiles = 0;
11054e115012SGarrett Wollman 	flag['c'] = 0;
11064e115012SGarrett Wollman 	flag['h'] = 0;
11074e115012SGarrett Wollman 	flag['l'] = 0;
11084e115012SGarrett Wollman 	flag['m'] = 0;
1109ff49530fSBill Paul 	flag['o'] = 0;
1110ff49530fSBill Paul 	flag['s'] = 0;
1111ff49530fSBill Paul 	flag['n'] = 0;
1112ff49530fSBill Paul 	flag['t'] = 0;
1113ff49530fSBill Paul 	flag['S'] = 0;
1114ff49530fSBill Paul 	flag['C'] = 0;
1115ff49530fSBill Paul 	flag['M'] = 0;
1116ff49530fSBill Paul 
11174e115012SGarrett Wollman 	for (i = 1; i < argc; i++) {
11184e115012SGarrett Wollman 		if (argv[i][0] != '-') {
11194e115012SGarrett Wollman 			if (cmd->infile) {
11200e76f40dSPhilippe Charnier 				warnx("cannot specify more than one input file");
11214e115012SGarrett Wollman 				return (0);
11224e115012SGarrett Wollman 			}
11234e115012SGarrett Wollman 			cmd->infile = argv[i];
11244e115012SGarrett Wollman 		} else {
11254e115012SGarrett Wollman 			for (j = 1; argv[i][j] != 0; j++) {
11264e115012SGarrett Wollman 				c = argv[i][j];
11274e115012SGarrett Wollman 				switch (c) {
1128ff49530fSBill Paul 				case 'a':
1129ff49530fSBill Paul 					allfiles = 1;
1130ff49530fSBill Paul 					break;
11314e115012SGarrett Wollman 				case 'c':
11324e115012SGarrett Wollman 				case 'h':
11334e115012SGarrett Wollman 				case 'l':
11344e115012SGarrett Wollman 				case 'm':
1135ff49530fSBill Paul 				case 't':
1136526195adSJordan K. Hubbard 					if (flag[(int)c]) {
11374e115012SGarrett Wollman 						return (0);
11384e115012SGarrett Wollman 					}
1139526195adSJordan K. Hubbard 					flag[(int)c] = 1;
11404e115012SGarrett Wollman 					break;
1141ff49530fSBill Paul 				case 'S':
1142ff49530fSBill Paul 					/*
1143ff49530fSBill Paul 					 * sample flag: Ss or Sc.
1144ff49530fSBill Paul 					 *  Ss means set flag['S'];
1145ff49530fSBill Paul 					 *  Sc means set flag['C'];
1146ff49530fSBill Paul 					 *  Sm means set flag['M'];
1147ff49530fSBill Paul 					 */
1148ff49530fSBill Paul 					ch = argv[i][++j]; /* get next char */
1149ff49530fSBill Paul 					if (ch == 's')
1150ff49530fSBill Paul 						ch = 'S';
1151ff49530fSBill Paul 					else if (ch == 'c')
1152ff49530fSBill Paul 						ch = 'C';
1153ff49530fSBill Paul 					else if (ch == 'm')
1154ff49530fSBill Paul 						ch = 'M';
1155ff49530fSBill Paul 					else
1156ff49530fSBill Paul 						return (0);
1157ff49530fSBill Paul 
1158526195adSJordan K. Hubbard 					if (flag[(int)ch]) {
1159ff49530fSBill Paul 						return (0);
1160ff49530fSBill Paul 					}
1161526195adSJordan K. Hubbard 					flag[(int)ch] = 1;
1162ff49530fSBill Paul 					break;
1163ff49530fSBill Paul 				case 'C': /* ANSI C syntax */
1164ff49530fSBill Paul 					ch = argv[i][j+1]; /* get next char */
1165ff49530fSBill Paul 
1166ff49530fSBill Paul 					if (ch != 'C')
1167ff49530fSBill Paul 						break;
1168ff49530fSBill Paul 					CCflag = 1;
1169ff49530fSBill Paul 					break;
1170ff49530fSBill Paul 				case 'b':
1171ff49530fSBill Paul 					/*
1172ff49530fSBill Paul 					 *  Turn TIRPC flag off for
1173ff49530fSBill Paul 					 *  generating backward compatible
1174ff49530fSBill Paul 					 *  code
1175ff49530fSBill Paul 					 */
1176ff49530fSBill Paul 					tirpcflag = 0;
1177ff49530fSBill Paul 					break;
1178ff49530fSBill Paul 
1179ff49530fSBill Paul 				case 'I':
1180ff49530fSBill Paul 					inetdflag = 1;
1181ff49530fSBill Paul 					break;
1182ff49530fSBill Paul 				case 'N':
1183ff49530fSBill Paul 					newstyle = 1;
1184ff49530fSBill Paul 					break;
1185ff49530fSBill Paul 				case 'L':
1186ff49530fSBill Paul 					logflag = 1;
1187ff49530fSBill Paul 					break;
118840ad8885SAlfred Perlstein 				case 'P':
118940ad8885SAlfred Perlstein 					pmflag = 1;
119040ad8885SAlfred Perlstein 					break;
1191ff49530fSBill Paul 				case 'K':
1192ff49530fSBill Paul 					if (++i == argc) {
1193ff49530fSBill Paul 						return (0);
1194ff49530fSBill Paul 					}
1195ff49530fSBill Paul 					svcclosetime = argv[i];
1196ff49530fSBill Paul 					goto nextarg;
1197ff49530fSBill Paul 				case 'T':
1198ff49530fSBill Paul 					tblflag = 1;
1199ff49530fSBill Paul 					break;
1200ff49530fSBill Paul 				case 'M':
1201ff49530fSBill Paul 					mtflag = 1;
1202ff49530fSBill Paul 					break;
1203ff49530fSBill Paul 				case 'i' :
1204ff49530fSBill Paul 					if (++i == argc) {
1205ff49530fSBill Paul 						return (0);
1206ff49530fSBill Paul 					}
1207122562cdSStefan Farfeleder 					inline_size = atoi(argv[i]);
1208ff49530fSBill Paul 					goto nextarg;
1209ff49530fSBill Paul 				case 'n':
12104e115012SGarrett Wollman 				case 'o':
12114e115012SGarrett Wollman 				case 's':
12124e115012SGarrett Wollman 					if (argv[i][j - 1] != '-' ||
12134e115012SGarrett Wollman 					    argv[i][j + 1] != 0) {
12144e115012SGarrett Wollman 						return (0);
12154e115012SGarrett Wollman 					}
1216526195adSJordan K. Hubbard 					flag[(int)c] = 1;
12174e115012SGarrett Wollman 					if (++i == argc) {
12184e115012SGarrett Wollman 						return (0);
12194e115012SGarrett Wollman 					}
1220ff49530fSBill Paul 					if (c == 'o') {
12214e115012SGarrett Wollman 						if (cmd->outfile) {
12224e115012SGarrett Wollman 							return (0);
12234e115012SGarrett Wollman 						}
12244e115012SGarrett Wollman 						cmd->outfile = argv[i];
12254e115012SGarrett Wollman 					}
12264e115012SGarrett Wollman 					goto nextarg;
1227ff49530fSBill Paul 				case 'D':
1228ff49530fSBill Paul 					if (argv[i][j - 1] != '-') {
1229ff49530fSBill Paul 						return (0);
1230ff49530fSBill Paul 					}
1231ff49530fSBill Paul 					(void) addarg(argv[i]);
1232ff49530fSBill Paul 					goto nextarg;
1233ff49530fSBill Paul 				case 'Y':
1234ff49530fSBill Paul 					if (++i == argc) {
1235ff49530fSBill Paul 						return (0);
1236ff49530fSBill Paul 					}
1237faabfb8aSDimitry Andric 					if (strlcpy(pathbuf, argv[i],
1238faabfb8aSDimitry Andric 					    sizeof(pathbuf)) >= sizeof(pathbuf)
1239faabfb8aSDimitry Andric 					    || strlcat(pathbuf, "/cpp",
1240faabfb8aSDimitry Andric 					    sizeof(pathbuf)) >=
1241faabfb8aSDimitry Andric 					    sizeof(pathbuf)) {
1242a0b13740SKris Kennaway 						warnx("argument too long");
1243a0b13740SKris Kennaway 						return (0);
1244a0b13740SKris Kennaway 					}
1245ff49530fSBill Paul 					CPP = pathbuf;
1246ff49530fSBill Paul 					goto nextarg;
1247ff49530fSBill Paul 
1248ff49530fSBill Paul 
12494e115012SGarrett Wollman 
12504e115012SGarrett Wollman 				default:
12514e115012SGarrett Wollman 					return (0);
12524e115012SGarrett Wollman 				}
12534e115012SGarrett Wollman 			}
12544e115012SGarrett Wollman 		nextarg:
12554e115012SGarrett Wollman 			;
12564e115012SGarrett Wollman 		}
12574e115012SGarrett Wollman 	}
1258ff49530fSBill Paul 
12594e115012SGarrett Wollman 	cmd->cflag = flag['c'];
12604e115012SGarrett Wollman 	cmd->hflag = flag['h'];
12614e115012SGarrett Wollman 	cmd->lflag = flag['l'];
12624e115012SGarrett Wollman 	cmd->mflag = flag['m'];
1263ff49530fSBill Paul 	cmd->nflag = flag['n'];
1264ff49530fSBill Paul 	cmd->sflag = flag['s'];
1265ff49530fSBill Paul 	cmd->tflag = flag['t'];
1266ff49530fSBill Paul 	cmd->Ssflag = flag['S'];
1267ff49530fSBill Paul 	cmd->Scflag = flag['C'];
1268ff49530fSBill Paul 	cmd->makefileflag = flag['M'];
1269ff49530fSBill Paul 
1270ff49530fSBill Paul 	if (tirpcflag) {
127140ad8885SAlfred Perlstein 		if (inetdflag)
127240ad8885SAlfred Perlstein 			pmflag = 0;
1273ff49530fSBill Paul 		if ((inetdflag && cmd->nflag)) {
1274ff49530fSBill Paul 			/* netid not allowed with inetdflag */
12750e76f40dSPhilippe Charnier 			warnx("cannot use netid flag with inetd flag");
1276ff49530fSBill Paul 			return (0);
1277ff49530fSBill Paul 		}
1278ff49530fSBill Paul 	} else {		/* 4.1 mode */
1279ff49530fSBill Paul 		pmflag = 0;	/* set pmflag only in tirpcmode */
1280ff49530fSBill Paul 		if (cmd->nflag) { /* netid needs TIRPC */
12810e76f40dSPhilippe Charnier 			warnx("cannot use netid flag without TIRPC");
1282ff49530fSBill Paul 			return (0);
1283ff49530fSBill Paul 		}
1284ff49530fSBill Paul 	}
1285ff49530fSBill Paul 
1286ff49530fSBill Paul 	if (newstyle && (tblflag || cmd->tflag)) {
12870e76f40dSPhilippe Charnier 		warnx("cannot use table flags with newstyle");
1288ff49530fSBill Paul 		return (0);
1289ff49530fSBill Paul 	}
1290ff49530fSBill Paul 
1291ff49530fSBill Paul 	/* check no conflicts with file generation flags */
1292ff49530fSBill Paul 	nflags = cmd->cflag + cmd->hflag + cmd->lflag + cmd->mflag +
1293ff49530fSBill Paul 		cmd->sflag + cmd->nflag + cmd->tflag + cmd->Ssflag +
1294ff49530fSBill Paul 			cmd->Scflag + cmd->makefileflag;
1295ff49530fSBill Paul 
12964e115012SGarrett Wollman 	if (nflags == 0) {
12974e115012SGarrett Wollman 		if (cmd->outfile != NULL || cmd->infile == NULL) {
12984e115012SGarrett Wollman 			return (0);
12994e115012SGarrett Wollman 		}
1300ff49530fSBill Paul 	} else if (cmd->infile == NULL &&
1301ff49530fSBill Paul 	    (cmd->Ssflag || cmd->Scflag || cmd->makefileflag)) {
13020e76f40dSPhilippe Charnier 		warnx("\"infile\" is required for template generation flags");
1303ff49530fSBill Paul 		return (0);
1304ff49530fSBill Paul 	} if (nflags > 1) {
13050e76f40dSPhilippe Charnier 		warnx("cannot have more than one file generation flag");
13064e115012SGarrett Wollman 		return (0);
13074e115012SGarrett Wollman 	}
13084e115012SGarrett Wollman 	return (1);
13094e115012SGarrett Wollman }
1310ff49530fSBill Paul 
1311526195adSJordan K. Hubbard static void
1312ef636796SEd Schouten usage(void)
1313ff49530fSBill Paul {
13140e76f40dSPhilippe Charnier 	f_print(stderr, "%s\n%s\n%s\n%s\n%s\n",
13150e76f40dSPhilippe Charnier 		"usage: rpcgen infile",
13160e76f40dSPhilippe Charnier 		"       rpcgen [-abCLNTM] [-Dname[=value]] [-i size]\
131740ad8885SAlfred Perlstein [-I -P [-K seconds]] [-Y path] infile",
13180e76f40dSPhilippe Charnier 		"       rpcgen [-c | -h | -l | -m | -t | -Sc | -Ss | -Sm]\
13190e76f40dSPhilippe Charnier [-o outfile] [infile]",
13200e76f40dSPhilippe Charnier 		"       rpcgen [-s nettype]* [-o outfile] [infile]",
13210e76f40dSPhilippe Charnier 		"       rpcgen [-n netid]* [-o outfile] [infile]");
1322ff49530fSBill Paul 	options_usage();
1323ff49530fSBill Paul 	exit(1);
1324ff49530fSBill Paul }
1325ff49530fSBill Paul 
1326526195adSJordan K. Hubbard static void
1327ef636796SEd Schouten options_usage(void)
1328ff49530fSBill Paul {
1329ff49530fSBill Paul 	f_print(stderr, "options:\n");
1330ff49530fSBill Paul 	f_print(stderr, "-a\t\tgenerate all files, including samples\n");
1331ff49530fSBill Paul 	f_print(stderr, "-b\t\tbackward compatibility mode (generates code \
133240ad8885SAlfred Perlstein for FreeBSD 4.X)\n");
1333ff49530fSBill Paul 	f_print(stderr, "-c\t\tgenerate XDR routines\n");
1334ff49530fSBill Paul 	f_print(stderr, "-C\t\tANSI C mode\n");
1335ff49530fSBill Paul 	f_print(stderr, "-Dname[=value]\tdefine a symbol (same as #define)\n");
1336ff49530fSBill Paul 	f_print(stderr, "-h\t\tgenerate header file\n");
1337ff49530fSBill Paul 	f_print(stderr, "-i size\t\tsize at which to start generating\
1338ff49530fSBill Paul inline code\n");
133940ad8885SAlfred Perlstein 	f_print(stderr, "-I\t\tgenerate code for inetd support in server\n");
1340ff49530fSBill Paul 	f_print(stderr, "-K seconds\tserver exits after K seconds of\
1341ff49530fSBill Paul inactivity\n");
1342ff49530fSBill Paul 	f_print(stderr, "-l\t\tgenerate client side stubs\n");
1343ff49530fSBill Paul 	f_print(stderr, "-L\t\tserver errors will be printed to syslog\n");
1344ff49530fSBill Paul 	f_print(stderr, "-m\t\tgenerate server side stubs\n");
1345ff49530fSBill Paul 	f_print(stderr, "-M\t\tgenerate MT-safe code\n");
1346ff49530fSBill Paul 	f_print(stderr, "-n netid\tgenerate server code that supports\
1347ff49530fSBill Paul named netid\n");
1348ff49530fSBill Paul 	f_print(stderr, "-N\t\tsupports multiple arguments and\
1349ff49530fSBill Paul call-by-value\n");
1350ff49530fSBill Paul 	f_print(stderr, "-o outfile\tname of the output file\n");
135140ad8885SAlfred Perlstein 	f_print(stderr, "-P\t\tgenerate code for port monitoring support in server\n");
1352ff49530fSBill Paul 	f_print(stderr, "-s nettype\tgenerate server code that supports named\
1353ff49530fSBill Paul nettype\n");
1354ff49530fSBill Paul 	f_print(stderr, "-Sc\t\tgenerate sample client code that uses remote\
1355ff49530fSBill Paul procedures\n");
1356ff49530fSBill Paul 	f_print(stderr, "-Ss\t\tgenerate sample server code that defines\
1357ff49530fSBill Paul remote procedures\n");
1358ff49530fSBill Paul 	f_print(stderr, "-Sm \t\tgenerate makefile template \n");
1359ff49530fSBill Paul 
1360ff49530fSBill Paul 	f_print(stderr, "-t\t\tgenerate RPC dispatch table\n");
1361ff49530fSBill Paul 	f_print(stderr, "-T\t\tgenerate code to support RPC dispatch tables\n");
1362ff49530fSBill Paul 	f_print(stderr, "-Y path\t\tpath where cpp is found\n");
1363ff49530fSBill Paul 	exit(1);
1364ff49530fSBill Paul }
1365