xref: /freebsd/usr.bin/rpcgen/rpc_main.c (revision 4f83fd19620008cfa1bcec24b41665a08c014fe7)
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 
31ff49530fSBill Paul #ident	"@(#)rpc_main.c	1.21	94/04/25 SMI"
32ff49530fSBill Paul 
330e76f40dSPhilippe Charnier #if 0
3475863a6dSPhilippe Charnier #ifndef lint
35ff49530fSBill Paul static char sccsid[] = "@(#)rpc_main.c 1.30 89/03/30 (C) 1987 SMI";
364e115012SGarrett Wollman #endif
370e76f40dSPhilippe Charnier #endif
384e115012SGarrett Wollman 
3975863a6dSPhilippe Charnier #include <sys/cdefs.h>
4075863a6dSPhilippe Charnier __FBSDID("$FreeBSD$");
4175863a6dSPhilippe Charnier 
424e115012SGarrett Wollman /*
434e115012SGarrett Wollman  * rpc_main.c, Top level of the RPC protocol compiler.
444e115012SGarrett Wollman  * Copyright (C) 1987, Sun Microsystems, Inc.
454e115012SGarrett Wollman  */
464e115012SGarrett Wollman 
470e76f40dSPhilippe Charnier #include <err.h>
480e76f40dSPhilippe Charnier #include <ctype.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"
57ff49530fSBill Paul #include "rpc_util.h"
584e115012SGarrett Wollman #include "rpc_scan.h"
594e115012SGarrett Wollman 
60d3cb5dedSWarner Losh extern void write_sample_svc( definition * );
61d3cb5dedSWarner Losh extern int write_sample_clnt( definition * );
62d3cb5dedSWarner Losh extern void write_sample_clnt_main( void );
63d3cb5dedSWarner Losh extern void add_sample_msg( void );
64d3cb5dedSWarner Losh static void c_output( char *, char *, int, char * );
65d3cb5dedSWarner Losh static void h_output( char *, char *, int, char * );
66d3cb5dedSWarner Losh static void l_output( char *, char *, int, char * );
67d3cb5dedSWarner Losh static void t_output( char *, char *, int, char * );
68d3cb5dedSWarner Losh static void clnt_output( char *, char *, int, char * );
69d3cb5dedSWarner Losh void c_initialize( void );
704e115012SGarrett Wollman 
71d7c089b7SJohn Birrell #if !defined(__FreeBSD__) && !defined(__NetBSD__)
72ff49530fSBill Paul char * rindex();
73ff49530fSBill Paul #endif
74ff49530fSBill Paul 
75d3cb5dedSWarner Losh static void usage(void);
76d3cb5dedSWarner Losh static void options_usage(void);
77d3cb5dedSWarner Losh static int do_registers(int, char **);
78d3cb5dedSWarner Losh static int parseargs(int, char **, struct commandline *);
79d3cb5dedSWarner Losh static void svc_output(char *, char *, int, char *);
80d3cb5dedSWarner Losh static void mkfile_output(struct commandline *);
81d3cb5dedSWarner Losh static void s_output(int, char **, char *, char *, int, char *, int, int);
82ff49530fSBill Paul 
83ff49530fSBill Paul #define	EXTEND	1		/* alias for TRUE */
84ff49530fSBill Paul #define	DONT_EXTEND	0		/* alias for FALSE */
85ff49530fSBill Paul 
86ff49530fSBill Paul #define	SVR4_CPP "/usr/ccs/lib/cpp"
87d7c089b7SJohn Birrell #define SUNOS_CPP "/usr/bin/cpp"
88ff49530fSBill Paul 
89ff49530fSBill Paul static int cppDefined = 0;	/* explicit path for C preprocessor */
90ff49530fSBill Paul 
91ff49530fSBill Paul static char *svcclosetime = "120";
92ff49530fSBill Paul static char *CPP = SVR4_CPP;
934e115012SGarrett Wollman static char CPPFLAGS[] = "-C";
94ff49530fSBill Paul static char pathbuf[MAXPATHLEN + 1];
954e115012SGarrett Wollman static char *allv[] = {
964e115012SGarrett Wollman 	"rpcgen", "-s", "udp", "-s", "tcp",
974e115012SGarrett Wollman };
984e115012SGarrett Wollman static int allc = sizeof (allv)/sizeof (allv[0]);
99ff49530fSBill Paul static char *allnv[] = {
100ff49530fSBill Paul 	"rpcgen", "-s", "netpath",
101ff49530fSBill Paul };
102ff49530fSBill Paul static int allnc = sizeof (allnv)/sizeof (allnv[0]);
103ff49530fSBill Paul 
104ff49530fSBill Paul /*
105ff49530fSBill Paul  * machinations for handling expanding argument list
106ff49530fSBill Paul  */
107ff49530fSBill Paul static void addarg();		/* add another argument to the list */
108ff49530fSBill Paul static void putarg();		/* put argument at specified location  */
109ff49530fSBill Paul static void clear_args();	/* clear argument list */
110ff49530fSBill Paul static void checkfiles();	/* check if out file already exists */
1114e115012SGarrett Wollman 
1124e115012SGarrett Wollman 
113ff49530fSBill Paul 
114ff49530fSBill Paul #define	ARGLISTLEN	20
115ff49530fSBill Paul #define	FIXEDARGS	2
116ff49530fSBill Paul 
117ff49530fSBill Paul static char *arglist[ARGLISTLEN];
118ff49530fSBill Paul static int argcount = FIXEDARGS;
119ff49530fSBill Paul 
120ff49530fSBill Paul 
121ff49530fSBill Paul int nonfatalerrors;	/* errors */
12240ad8885SAlfred Perlstein int inetdflag = 0;	/* Support for inetd is disabled by default, use -I */
12340ad8885SAlfred Perlstein int pmflag = 0;		/* Support for port monitors is disabled by default */
12440ad8885SAlfred Perlstein int tirpc_socket = 1;	/* TI-RPC on socket, no TLI library */
125ff49530fSBill Paul int logflag;		/* Use syslog instead of fprintf for errors */
126ff49530fSBill Paul int tblflag;		/* Support for dispatch table file */
127ff49530fSBill Paul int mtflag = 0;		/* Support for MT */
12840ad8885SAlfred Perlstein 
129ff49530fSBill Paul #define INLINE 0
130ff49530fSBill Paul /* length at which to start doing an inline */
131ff49530fSBill Paul 
132ff49530fSBill Paul int inline = INLINE;
133ff49530fSBill Paul /*
134ff49530fSBill Paul  * Length at which to start doing an inline. INLINE = default
135ff49530fSBill Paul  * if 0, no xdr_inline code
136ff49530fSBill Paul  */
137ff49530fSBill Paul 
138ff49530fSBill Paul int indefinitewait;	/* If started by port monitors, hang till it wants */
139ff49530fSBill Paul int exitnow;		/* If started by port monitors, exit after the call */
140ff49530fSBill Paul int timerflag;		/* TRUE if !indefinite && !exitnow */
141ff49530fSBill Paul int newstyle;		/* newstyle of passing arguments (by value) */
142ff49530fSBill Paul int Cflag = 0;		/* ANSI C syntax */
143ff49530fSBill Paul int CCflag = 0;		/* C++ files */
144ff49530fSBill Paul static int allfiles;   /* generate all files */
145ff49530fSBill Paul int tirpcflag = 1;    /* generating code for tirpc, by default */
146ff49530fSBill Paul xdrfunc *xdrfunc_head = NULL; /* xdr function list */
147ff49530fSBill Paul xdrfunc *xdrfunc_tail = NULL; /* xdr function list */
148ff49530fSBill Paul pid_t childpid;
149ff49530fSBill Paul 
1504e115012SGarrett Wollman 
151526195adSJordan K. Hubbard int
1524e115012SGarrett Wollman main(argc, argv)
1534e115012SGarrett Wollman 	int argc;
1544e115012SGarrett Wollman 	char *argv[];
1554e115012SGarrett Wollman {
1564e115012SGarrett Wollman 	struct commandline cmd;
1574e115012SGarrett Wollman 
158ff49530fSBill Paul 	(void) memset((char *)&cmd, 0, sizeof (struct commandline));
159ff49530fSBill Paul 	clear_args();
160ff49530fSBill Paul 	if (!parseargs(argc, argv, &cmd))
161ff49530fSBill Paul 		usage();
162ff49530fSBill Paul 	/*
163ff49530fSBill Paul 	 * Only the client and server side stubs are likely to be customized,
164ff49530fSBill Paul 	 *  so in that case only, check if the outfile exists, and if so,
165ff49530fSBill Paul 	 *  print an error message and exit.
166ff49530fSBill Paul 	 */
167ff49530fSBill Paul 	if (cmd.Ssflag || cmd.Scflag || cmd.makefileflag) {
168ff49530fSBill Paul 		checkfiles(cmd.infile, cmd.outfile);
1694e115012SGarrett Wollman 	}
170ff49530fSBill Paul 	else
171ff49530fSBill Paul 		checkfiles(cmd.infile, NULL);
172ff49530fSBill Paul 
1734e115012SGarrett Wollman 	if (cmd.cflag) {
174ff49530fSBill Paul 		c_output(cmd.infile, "-DRPC_XDR", DONT_EXTEND, cmd.outfile);
1754e115012SGarrett Wollman 	} else if (cmd.hflag) {
176ff49530fSBill Paul 		h_output(cmd.infile, "-DRPC_HDR", DONT_EXTEND, cmd.outfile);
1774e115012SGarrett Wollman 	} else if (cmd.lflag) {
178ff49530fSBill Paul 		l_output(cmd.infile, "-DRPC_CLNT", DONT_EXTEND, cmd.outfile);
179ff49530fSBill Paul 	} else if (cmd.sflag || cmd.mflag || (cmd.nflag)) {
180ff49530fSBill Paul 		s_output(argc, argv, cmd.infile, "-DRPC_SVC", DONT_EXTEND,
181ff49530fSBill Paul 			cmd.outfile, cmd.mflag, cmd.nflag);
182ff49530fSBill Paul 	} else if (cmd.tflag) {
183ff49530fSBill Paul 		t_output(cmd.infile, "-DRPC_TBL", DONT_EXTEND, cmd.outfile);
184ff49530fSBill Paul 	} else if  (cmd.Ssflag) {
185ff49530fSBill Paul 		svc_output(cmd.infile, "-DRPC_SERVER", DONT_EXTEND,
186ff49530fSBill Paul 			cmd.outfile);
187ff49530fSBill Paul 	} else if (cmd.Scflag) {
188ff49530fSBill Paul 		clnt_output(cmd.infile, "-DRPC_CLIENT", DONT_EXTEND,
189ff49530fSBill Paul 			    cmd.outfile);
190ff49530fSBill Paul 	} else if (cmd.makefileflag) {
191ff49530fSBill Paul 		mkfile_output(&cmd);
1924e115012SGarrett Wollman 	} else {
193ff49530fSBill Paul 		/* the rescans are required, since cpp may effect input */
1944e115012SGarrett Wollman 		c_output(cmd.infile, "-DRPC_XDR", EXTEND, "_xdr.c");
1954e115012SGarrett Wollman 		reinitialize();
1964e115012SGarrett Wollman 		h_output(cmd.infile, "-DRPC_HDR", EXTEND, ".h");
1974e115012SGarrett Wollman 		reinitialize();
1984e115012SGarrett Wollman 		l_output(cmd.infile, "-DRPC_CLNT", EXTEND, "_clnt.c");
1994e115012SGarrett Wollman 		reinitialize();
200ff49530fSBill Paul 		if (inetdflag || !tirpcflag)
2014e115012SGarrett Wollman 			s_output(allc, allv, cmd.infile, "-DRPC_SVC", EXTEND,
202ff49530fSBill Paul 			"_svc.c", cmd.mflag, cmd.nflag);
203ff49530fSBill Paul 		else
204ff49530fSBill Paul 			s_output(allnc, allnv, cmd.infile, "-DRPC_SVC",
205ff49530fSBill Paul 				EXTEND, "_svc.c", cmd.mflag, cmd.nflag);
206ff49530fSBill Paul 		if (tblflag) {
207ff49530fSBill Paul 			reinitialize();
208ff49530fSBill Paul 			t_output(cmd.infile, "-DRPC_TBL", EXTEND, "_tbl.i");
2094e115012SGarrett Wollman 		}
2104e115012SGarrett Wollman 
211ff49530fSBill Paul 		if (allfiles) {
212ff49530fSBill Paul 			reinitialize();
213ff49530fSBill Paul 			svc_output(cmd.infile, "-DRPC_SERVER", EXTEND,
214ff49530fSBill Paul 				"_server.c");
215ff49530fSBill Paul 			reinitialize();
216ff49530fSBill Paul 			clnt_output(cmd.infile, "-DRPC_CLIENT", EXTEND,
217ff49530fSBill Paul 				"_client.c");
218ff49530fSBill Paul 
219ff49530fSBill Paul 		}
220ff49530fSBill Paul 		if (allfiles || (cmd.makefileflag == 1)){
221ff49530fSBill Paul 			reinitialize();
222ff49530fSBill Paul 			mkfile_output(&cmd);
223ff49530fSBill Paul 		}
224ff49530fSBill Paul 
225ff49530fSBill Paul 	}
226ff49530fSBill Paul 	exit(nonfatalerrors);
227ff49530fSBill Paul 	/* NOTREACHED */
228ff49530fSBill Paul }
229ff49530fSBill Paul 
230ff49530fSBill Paul 
2314e115012SGarrett Wollman /*
232ff49530fSBill Paul  * add extension to filename
2334e115012SGarrett Wollman  */
2344e115012SGarrett Wollman static char *
2355ec07232SNate Williams extendfile(path, ext)
2365ec07232SNate Williams 	char *path;
2374e115012SGarrett Wollman 	char *ext;
2384e115012SGarrett Wollman {
2394e115012SGarrett Wollman 	char *res;
2404e115012SGarrett Wollman 	char *p;
241ff49530fSBill Paul 	char *file;
2424e115012SGarrett Wollman 
2435ec07232SNate Williams 	if ((file = rindex(path, '/')) == NULL)
2445ec07232SNate Williams 		file = path;
2455ec07232SNate Williams 	else
2465ec07232SNate Williams 		file++;
24775863a6dSPhilippe Charnier 	res = xmalloc(strlen(file) + strlen(ext) + 1);
248ff49530fSBill Paul 	p = strrchr(file, '.');
2494e115012SGarrett Wollman 	if (p == NULL) {
2504e115012SGarrett Wollman 		p = file + strlen(file);
2514e115012SGarrett Wollman 	}
2524e115012SGarrett Wollman 	(void) strcpy(res, file);
2534e115012SGarrett Wollman 	(void) strcpy(res + (p - file), ext);
2544e115012SGarrett Wollman 	return (res);
2554e115012SGarrett Wollman }
2564e115012SGarrett Wollman 
2574e115012SGarrett Wollman /*
2584e115012SGarrett Wollman  * Open output file with given extension
2594e115012SGarrett Wollman  */
260526195adSJordan K. Hubbard static void
2614e115012SGarrett Wollman open_output(infile, outfile)
2624e115012SGarrett Wollman 	char *infile;
2634e115012SGarrett Wollman 	char *outfile;
2644e115012SGarrett Wollman {
265ff49530fSBill Paul 
2664e115012SGarrett Wollman 	if (outfile == NULL) {
2674e115012SGarrett Wollman 		fout = stdout;
2684e115012SGarrett Wollman 		return;
2694e115012SGarrett Wollman 	}
270ff49530fSBill Paul 
2714e115012SGarrett Wollman 	if (infile != NULL && streq(outfile, infile)) {
2720e76f40dSPhilippe Charnier 		warnx("%s already exists. No output generated", infile);
2734e115012SGarrett Wollman 		crash();
2744e115012SGarrett Wollman 	}
2754e115012SGarrett Wollman 	fout = fopen(outfile, "w");
2764e115012SGarrett Wollman 	if (fout == NULL) {
2770e76f40dSPhilippe Charnier 		warn("unable to open %s", outfile);
2784e115012SGarrett Wollman 		crash();
2794e115012SGarrett Wollman 	}
2804e115012SGarrett Wollman 	record_open(outfile);
281ff49530fSBill Paul 
282526195adSJordan K. Hubbard 	return;
283ff49530fSBill Paul }
284ff49530fSBill Paul 
285526195adSJordan K. Hubbard static void
286ff49530fSBill Paul add_warning()
287ff49530fSBill Paul {
288ff49530fSBill Paul 	f_print(fout, "/*\n");
289ff49530fSBill Paul 	f_print(fout, " * Please do not edit this file.\n");
290ff49530fSBill Paul 	f_print(fout, " * It was generated using rpcgen.\n");
291ff49530fSBill Paul 	f_print(fout, " */\n\n");
292ff49530fSBill Paul }
293ff49530fSBill Paul 
294ff49530fSBill Paul /* clear list of arguments */
295ff49530fSBill Paul static void clear_args()
296ff49530fSBill Paul {
297ff49530fSBill Paul 	int i;
298ff49530fSBill Paul 	for (i = FIXEDARGS; i < ARGLISTLEN; i++)
299ff49530fSBill Paul 		arglist[i] = NULL;
300ff49530fSBill Paul 	argcount = FIXEDARGS;
301ff49530fSBill Paul }
302ff49530fSBill Paul 
303ff49530fSBill Paul /* make sure that a CPP exists */
304ff49530fSBill Paul static void find_cpp()
305ff49530fSBill Paul {
306ff49530fSBill Paul 	struct stat buf;
307ff49530fSBill Paul 
308ff49530fSBill Paul 	if (stat(CPP, &buf) < 0)  { /* SVR4 or explicit cpp does not exist */
309ff49530fSBill Paul 		if (cppDefined) {
3100e76f40dSPhilippe Charnier 			warnx("cannot find C preprocessor: %s", CPP);
311ff49530fSBill Paul 			crash();
312ff49530fSBill Paul 		} else {	/* try the other one */
313ff49530fSBill Paul 			CPP = SUNOS_CPP;
314ff49530fSBill Paul 			if (stat(CPP, &buf) < 0) { /* can't find any cpp */
315f2912673SDavid E. O'Brien 				warnx("cannot find C preprocessor: %s", CPP);
316ff49530fSBill Paul 				crash();
317ff49530fSBill Paul 			}
318ff49530fSBill Paul 		}
319ff49530fSBill Paul 	}
3204e115012SGarrett Wollman }
3214e115012SGarrett Wollman 
3224e115012SGarrett Wollman /*
3234e115012SGarrett Wollman  * Open input file with given define for C-preprocessor
3244e115012SGarrett Wollman  */
325526195adSJordan K. Hubbard static void
3264e115012SGarrett Wollman open_input(infile, define)
3274e115012SGarrett Wollman 	char *infile;
3284e115012SGarrett Wollman 	char *define;
3294e115012SGarrett Wollman {
3304e115012SGarrett Wollman 	int pd[2];
3314e115012SGarrett Wollman 
3324e115012SGarrett Wollman 	infilename = (infile == NULL) ? "<stdin>" : infile;
3334e115012SGarrett Wollman 	(void) pipe(pd);
334ff49530fSBill Paul 	switch (childpid = fork()) {
3354e115012SGarrett Wollman 	case 0:
336ff49530fSBill Paul 		find_cpp();
337ff49530fSBill Paul 		putarg(0, CPP);
338ff49530fSBill Paul 		putarg(1, CPPFLAGS);
339ff49530fSBill Paul 		addarg(define);
340ff49530fSBill Paul 		if (infile)
341ff49530fSBill Paul 			addarg(infile);
342ff49530fSBill Paul 		addarg((char *)NULL);
3434e115012SGarrett Wollman 		(void) close(1);
3444e115012SGarrett Wollman 		(void) dup2(pd[1], 1);
3454e115012SGarrett Wollman 		(void) close(pd[0]);
346ff49530fSBill Paul 		execv(arglist[0], arglist);
34775863a6dSPhilippe Charnier 		err(1, "execv");
3484e115012SGarrett Wollman 	case -1:
34975863a6dSPhilippe Charnier 		err(1, "fork");
3504e115012SGarrett Wollman 	}
3514e115012SGarrett Wollman 	(void) close(pd[1]);
3524e115012SGarrett Wollman 	fin = fdopen(pd[0], "r");
3534e115012SGarrett Wollman 	if (fin == NULL) {
3540e76f40dSPhilippe Charnier 		warn("%s", infilename);
3554e115012SGarrett Wollman 		crash();
3564e115012SGarrett Wollman 	}
3574e115012SGarrett Wollman }
3584e115012SGarrett Wollman 
359ff49530fSBill Paul /* valid tirpc nettypes */
360ff49530fSBill Paul static char* valid_ti_nettypes[] =
361ff49530fSBill Paul {
362ff49530fSBill Paul 	"netpath",
363ff49530fSBill Paul 	"visible",
364ff49530fSBill Paul 	"circuit_v",
365ff49530fSBill Paul 	"datagram_v",
366ff49530fSBill Paul 	"circuit_n",
367ff49530fSBill Paul 	"datagram_n",
368ff49530fSBill Paul 	"udp",
369ff49530fSBill Paul 	"tcp",
370ff49530fSBill Paul 	"raw",
371ff49530fSBill Paul 	NULL
372ff49530fSBill Paul 	};
373ff49530fSBill Paul 
374ff49530fSBill Paul /* valid inetd nettypes */
375ff49530fSBill Paul static char* valid_i_nettypes[] =
376ff49530fSBill Paul {
377ff49530fSBill Paul 	"udp",
378ff49530fSBill Paul 	"tcp",
379ff49530fSBill Paul 	NULL
380ff49530fSBill Paul 	};
381ff49530fSBill Paul 
382ff49530fSBill Paul static int check_nettype(name, list_to_check)
383ff49530fSBill Paul char* name;
384ff49530fSBill Paul char* list_to_check[];
385ff49530fSBill Paul {
386ff49530fSBill Paul 	int i;
387ff49530fSBill Paul 	for (i = 0; list_to_check[i] != NULL; i++) {
388ff49530fSBill Paul 		if (strcmp(name, list_to_check[i]) == 0) {
389ff49530fSBill Paul 			return (1);
390ff49530fSBill Paul 		}
391ff49530fSBill Paul 	}
3920e76f40dSPhilippe Charnier 	warnx("illegal nettype :\'%s\'", name);
393ff49530fSBill Paul 	return (0);
394ff49530fSBill Paul }
395ff49530fSBill Paul 
396ff49530fSBill Paul static char *
397ff49530fSBill Paul file_name(file, ext)
398ff49530fSBill Paul char *file;
399ff49530fSBill Paul char *ext;
400ff49530fSBill Paul {
401ff49530fSBill Paul 	char *temp;
402ff49530fSBill Paul 	temp = extendfile(file, ext);
403ff49530fSBill Paul 
404ff49530fSBill Paul 	if (access(temp, F_OK) != -1)
405ff49530fSBill Paul 		return (temp);
406ff49530fSBill Paul 	else
407ff49530fSBill Paul 		return ((char *)" ");
408ff49530fSBill Paul 
409ff49530fSBill Paul }
410ff49530fSBill Paul 
411ff49530fSBill Paul 
412526195adSJordan K. Hubbard static void
4134e115012SGarrett Wollman c_output(infile, define, extend, outfile)
4144e115012SGarrett Wollman 	char *infile;
4154e115012SGarrett Wollman 	char *define;
4164e115012SGarrett Wollman 	int extend;
4174e115012SGarrett Wollman 	char *outfile;
4184e115012SGarrett Wollman {
4194e115012SGarrett Wollman 	definition *def;
4204e115012SGarrett Wollman 	char *include;
4214e115012SGarrett Wollman 	char *outfilename;
4224e115012SGarrett Wollman 	long tell;
4234e115012SGarrett Wollman 
424ff49530fSBill Paul 	c_initialize();
4254e115012SGarrett Wollman 	open_input(infile, define);
4264e115012SGarrett Wollman 	outfilename = extend ? extendfile(infile, outfile) : outfile;
4274e115012SGarrett Wollman 	open_output(infile, outfilename);
428ff49530fSBill Paul 	add_warning();
4294e115012SGarrett Wollman 	if (infile && (include = extendfile(infile, ".h"))) {
4304e115012SGarrett Wollman 		f_print(fout, "#include \"%s\"\n", include);
4314e115012SGarrett Wollman 		free(include);
432ff49530fSBill Paul 		/* .h file already contains rpc/rpc.h */
433ff49530fSBill Paul 	} else
434ff49530fSBill Paul 		f_print(fout, "#include <rpc/rpc.h>\n");
4354e115012SGarrett Wollman 	tell = ftell(fout);
436526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
4374e115012SGarrett Wollman 		emit(def);
4384e115012SGarrett Wollman 	}
4394e115012SGarrett Wollman 	if (extend && tell == ftell(fout)) {
4404e115012SGarrett Wollman 		(void) unlink(outfilename);
4414e115012SGarrett Wollman 	}
4424e115012SGarrett Wollman }
4434e115012SGarrett Wollman 
444ff49530fSBill Paul 
445526195adSJordan K. Hubbard void
446ff49530fSBill Paul c_initialize()
447ff49530fSBill Paul {
448ff49530fSBill Paul 
449ff49530fSBill Paul 	/* add all the starting basic types */
450ff49530fSBill Paul 	add_type(1, "int");
451ff49530fSBill Paul 	add_type(1, "long");
452ff49530fSBill Paul 	add_type(1, "short");
453ff49530fSBill Paul 	add_type(1, "bool");
454ff49530fSBill Paul 	add_type(1, "u_int");
455ff49530fSBill Paul 	add_type(1, "u_long");
456ff49530fSBill Paul 	add_type(1, "u_short");
457ff49530fSBill Paul 
458ff49530fSBill Paul }
459ff49530fSBill Paul 
460ff49530fSBill Paul char rpcgen_table_dcl[] = "struct rpcgen_table {\n\
461ff49530fSBill Paul 	char	*(*proc)(); \n\
462ff49530fSBill Paul 	xdrproc_t	xdr_arg; \n\
463ff49530fSBill Paul 	unsigned	len_arg; \n\
464ff49530fSBill Paul 	xdrproc_t	xdr_res; \n\
465ff49530fSBill Paul 	unsigned	len_res; \n\
466ff49530fSBill Paul }; \n";
467ff49530fSBill Paul 
468ff49530fSBill Paul 
469ff49530fSBill Paul char *generate_guard(pathname)
470ff49530fSBill Paul 	char* pathname;
471ff49530fSBill Paul {
472080f4020SSean Kelly 	char* filename, *guard, *tmp, *stopat;
473ff49530fSBill Paul 
474ff49530fSBill Paul 	filename = strrchr(pathname, '/');  /* find last component */
475ff49530fSBill Paul 	filename = ((filename == 0) ? pathname : filename+1);
47675863a6dSPhilippe Charnier 	guard = xstrdup(filename);
477080f4020SSean Kelly 	stopat = strrchr(guard, '.');
478080f4020SSean Kelly 
479080f4020SSean Kelly 	/*
480080f4020SSean Kelly 	 * Convert to a valid C macro name and make it upper case.
481080f4020SSean Kelly 	 * Map macro unfriendly characterss to '_'.
48210c546c4SSean Kelly 	 */
483080f4020SSean Kelly 	for (tmp = guard; *tmp != '\000'; ++tmp) {
484ff49530fSBill Paul 		if (islower(*tmp))
485ff49530fSBill Paul 			*tmp = toupper(*tmp);
486080f4020SSean Kelly 		else if (isupper(*tmp) || *tmp == '_')
48710c546c4SSean Kelly 			/* OK for C */;
48810c546c4SSean Kelly 		else if (tmp == guard)
48910c546c4SSean Kelly 			*tmp = '_';
49010c546c4SSean Kelly 		else if (isdigit(*tmp))
49110c546c4SSean Kelly 			/* OK for all but first character */;
492080f4020SSean Kelly 		else if (tmp == stopat) {
493080f4020SSean Kelly 			*tmp = '\0';
49410c546c4SSean Kelly 			break;
49510c546c4SSean Kelly 		} else
49610c546c4SSean Kelly 			*tmp = '_';
49710c546c4SSean Kelly 	}
498080f4020SSean Kelly 	/*
499080f4020SSean Kelly 	 * Can't have a '_' in front, because it'll end up being "__".
500080f4020SSean Kelly 	 * "__" macros shoudln't be used. So, remove all of the
501080f4020SSean Kelly 	 * '_' characters from the front.
50210c546c4SSean Kelly 	 */
503080f4020SSean Kelly 	if (*guard == '_') {
504080f4020SSean Kelly 		for (tmp = guard; *tmp == '_'; ++tmp)
505080f4020SSean Kelly 			;
506080f4020SSean Kelly 		strcpy(guard, tmp);
507ff49530fSBill Paul 	}
508ff49530fSBill Paul 	guard = extendfile(guard, "_H_RPCGEN");
509ff49530fSBill Paul 	return (guard);
510ff49530fSBill Paul }
511ff49530fSBill Paul 
5124e115012SGarrett Wollman /*
5134e115012SGarrett Wollman  * Compile into an XDR header file
5144e115012SGarrett Wollman  */
515ff49530fSBill Paul 
516ff49530fSBill Paul 
517526195adSJordan K. Hubbard static void
5184e115012SGarrett Wollman h_output(infile, define, extend, outfile)
5194e115012SGarrett Wollman 	char *infile;
5204e115012SGarrett Wollman 	char *define;
5214e115012SGarrett Wollman 	int extend;
5224e115012SGarrett Wollman 	char *outfile;
5234e115012SGarrett Wollman {
5244e115012SGarrett Wollman 	definition *def;
5254e115012SGarrett Wollman 	char *outfilename;
5264e115012SGarrett Wollman 	long tell;
527ff49530fSBill Paul 	char *guard;
528ff49530fSBill Paul 	list *l;
529ff49530fSBill Paul 	xdrfunc *xdrfuncp;
530ff49530fSBill Paul 	int i;
5314e115012SGarrett Wollman 
5324e115012SGarrett Wollman 	open_input(infile, define);
5334e115012SGarrett Wollman 	outfilename =  extend ? extendfile(infile, outfile) : outfile;
5344e115012SGarrett Wollman 	open_output(infile, outfilename);
535ff49530fSBill Paul 	add_warning();
536ff49530fSBill Paul 	if (outfilename || infile){
537ff49530fSBill Paul 		guard = generate_guard(outfilename ? outfilename: infile);
538ff49530fSBill Paul 	} else
539ff49530fSBill Paul 		guard = "STDIN_";
540ff49530fSBill Paul 
541ff49530fSBill Paul 	f_print(fout, "#ifndef _%s\n#define	_%s\n\n", guard,
542ff49530fSBill Paul 		guard);
543ff49530fSBill Paul 
544ff49530fSBill Paul 	f_print(fout, "#include <rpc/rpc.h>\n");
545ff49530fSBill Paul 
54640ad8885SAlfred Perlstein 	if (mtflag)
54735ab9586SDavid E. O'Brien 		f_print(fout, "#include <pthread.h>\n");
548ff49530fSBill Paul 
549ff49530fSBill Paul 	/* put the C++ support */
550ff49530fSBill Paul 	if (Cflag && !CCflag){
551ff49530fSBill Paul 		f_print(fout, "\n#ifdef __cplusplus\n");
552ff49530fSBill Paul 		f_print(fout, "extern \"C\" {\n");
553ff49530fSBill Paul 		f_print(fout, "#endif\n\n");
554ff49530fSBill Paul 	}
555ff49530fSBill Paul 
556ff49530fSBill Paul 	/* put in a typedef for quadprecision. Only with Cflag */
557ff49530fSBill Paul 
5584e115012SGarrett Wollman 	tell = ftell(fout);
559ff49530fSBill Paul 
560ff49530fSBill Paul 	/* print data definitions */
561526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
5624e115012SGarrett Wollman 		print_datadef(def);
5634e115012SGarrett Wollman 	}
564ff49530fSBill Paul 
565ff49530fSBill Paul 	/*
566ff49530fSBill Paul 	 * print function declarations.
567ff49530fSBill Paul 	 *  Do this after data definitions because they might be used as
568ff49530fSBill Paul 	 *  arguments for functions
569ff49530fSBill Paul 	 */
570ff49530fSBill Paul 	for (l = defined; l != NULL; l = l->next) {
571ff49530fSBill Paul 		print_funcdef(l->val);
572ff49530fSBill Paul 	}
573ff49530fSBill Paul 	/* Now  print all xdr func declarations */
574ff49530fSBill Paul 	if (xdrfunc_head != NULL){
575ff49530fSBill Paul 
576ff49530fSBill Paul 		f_print(fout,
577ff49530fSBill Paul 			"\n/* the xdr functions */\n");
578ff49530fSBill Paul 
579ff49530fSBill Paul 		if (CCflag){
580ff49530fSBill Paul 		f_print(fout, "\n#ifdef __cplusplus\n");
581ff49530fSBill Paul 		f_print(fout, "extern \"C\" {\n");
582ff49530fSBill Paul 		f_print(fout, "#endif\n");
583ff49530fSBill Paul 	}
584ff49530fSBill Paul 
585ff49530fSBill Paul 		if (!Cflag){
586ff49530fSBill Paul 			xdrfuncp = xdrfunc_head;
587ff49530fSBill Paul 			while (xdrfuncp != NULL){
588ff49530fSBill Paul 				print_xdr_func_def(xdrfuncp->name,
589ff49530fSBill Paul 				xdrfuncp->pointerp, 2);
590ff49530fSBill Paul 				xdrfuncp = xdrfuncp->next;
591ff49530fSBill Paul 			}
592ff49530fSBill Paul 		} else {
593ff49530fSBill Paul 
594ff49530fSBill Paul 			for (i = 1; i < 3; i++){
595ff49530fSBill Paul 				if (i == 1)
596ff49530fSBill Paul 	f_print(fout, "\n#if defined(__STDC__) || defined(__cplusplus)\n");
597ff49530fSBill Paul 
598ff49530fSBill Paul 				else
599ff49530fSBill Paul 					f_print(fout, "\n#else /* K&R C */\n");
600ff49530fSBill Paul 
601ff49530fSBill Paul 				xdrfuncp = xdrfunc_head;
602ff49530fSBill Paul 				while (xdrfuncp != NULL){
603ff49530fSBill Paul 					print_xdr_func_def(xdrfuncp->name,
604ff49530fSBill Paul 	xdrfuncp->pointerp, i);
605ff49530fSBill Paul 					xdrfuncp = xdrfuncp->next;
606ff49530fSBill Paul 				}
607ff49530fSBill Paul 			}
608ff49530fSBill Paul 		f_print(fout, "\n#endif /* K&R C */\n");
609ff49530fSBill Paul 		}
610ff49530fSBill Paul 	}
611ff49530fSBill Paul 
6124e115012SGarrett Wollman 	if (extend && tell == ftell(fout)) {
6134e115012SGarrett Wollman 		(void) unlink(outfilename);
614ff49530fSBill Paul 	} else if (tblflag) {
615ff49530fSBill Paul 		f_print(fout, rpcgen_table_dcl);
6164e115012SGarrett Wollman 	}
617ff49530fSBill Paul 
618ff49530fSBill Paul 	if (Cflag){
619ff49530fSBill Paul 		f_print(fout, "\n#ifdef __cplusplus\n");
620ff49530fSBill Paul 		f_print(fout, "}\n");
621ff49530fSBill Paul 		f_print(fout, "#endif\n");
622ff49530fSBill Paul 	}
623ff49530fSBill Paul 
624ff49530fSBill Paul 	f_print(fout, "\n#endif /* !_%s */\n", guard);
6254e115012SGarrett Wollman }
6264e115012SGarrett Wollman 
6274e115012SGarrett Wollman /*
6284e115012SGarrett Wollman  * Compile into an RPC service
6294e115012SGarrett Wollman  */
630526195adSJordan K. Hubbard static void
631ff49530fSBill Paul s_output(argc, argv, infile, define, extend, outfile, nomain, netflag)
6324e115012SGarrett Wollman 	int argc;
6334e115012SGarrett Wollman 	char *argv[];
6344e115012SGarrett Wollman 	char *infile;
6354e115012SGarrett Wollman 	char *define;
6364e115012SGarrett Wollman 	int extend;
6374e115012SGarrett Wollman 	char *outfile;
6384e115012SGarrett Wollman 	int nomain;
639ff49530fSBill Paul 	int netflag;
6404e115012SGarrett Wollman {
6414e115012SGarrett Wollman 	char *include;
6424e115012SGarrett Wollman 	definition *def;
643ff49530fSBill Paul 	int foundprogram = 0;
6444e115012SGarrett Wollman 	char *outfilename;
6454e115012SGarrett Wollman 
6464e115012SGarrett Wollman 	open_input(infile, define);
6474e115012SGarrett Wollman 	outfilename = extend ? extendfile(infile, outfile) : outfile;
6484e115012SGarrett Wollman 	open_output(infile, outfilename);
649ff49530fSBill Paul 	add_warning();
6504e115012SGarrett Wollman 	if (infile && (include = extendfile(infile, ".h"))) {
6514e115012SGarrett Wollman 		f_print(fout, "#include \"%s\"\n", include);
6524e115012SGarrett Wollman 		free(include);
653ff49530fSBill Paul 	} else
654ff49530fSBill Paul 		f_print(fout, "#include <rpc/rpc.h>\n");
655ff49530fSBill Paul 
656ff49530fSBill Paul 	f_print(fout, "#include <stdio.h>\n");
657ff49530fSBill Paul 	f_print(fout, "#include <stdlib.h> /* getenv, exit */\n");
658ff49530fSBill Paul 	if (Cflag) {
659ff49530fSBill Paul 		f_print (fout,
660ff49530fSBill Paul 		"#include <rpc/pmap_clnt.h> /* for pmap_unset */\n");
661ff49530fSBill Paul 		f_print (fout, "#include <string.h> /* strcmp */\n");
6624e115012SGarrett Wollman 	}
663bcb53b16SMartin Blapp 	if (tirpcflag)
664bcb53b16SMartin Blapp 		f_print(fout, "#include <rpc/rpc_com.h>\n");
665ff49530fSBill Paul 	if (strcmp(svcclosetime, "-1") == 0)
666ff49530fSBill Paul 		indefinitewait = 1;
667ff49530fSBill Paul 	else if (strcmp(svcclosetime, "0") == 0)
668ff49530fSBill Paul 		exitnow = 1;
669ff49530fSBill Paul 	else if (inetdflag || pmflag) {
670ff49530fSBill Paul 		f_print(fout, "#include <signal.h>\n");
671ff49530fSBill Paul 		timerflag = 1;
672ff49530fSBill Paul 	}
673ff49530fSBill Paul 
674ff49530fSBill Paul 	if (!tirpcflag && inetdflag)
675ff49530fSBill Paul 		f_print(fout, "#include <sys/ttycom.h> /* TIOCNOTTY */\n");
676ff49530fSBill Paul 	if (Cflag && (inetdflag || pmflag)) {
677ff49530fSBill Paul 		f_print(fout, "#ifdef __cplusplus\n");
678ff49530fSBill Paul 		f_print(fout,
679ff49530fSBill Paul 			"#include <sysent.h> /* getdtablesize, open */\n");
680ff49530fSBill Paul 		f_print(fout, "#endif /* __cplusplus */\n");
681ff49530fSBill Paul 	}
68240ad8885SAlfred Perlstein 	if (tirpcflag) {
68340ad8885SAlfred Perlstein 		f_print(fout, "#include <fcntl.h> /* open */\n");
68440ad8885SAlfred Perlstein 		f_print(fout, "#include <unistd.h> /* fork / setsid */\n");
685ff49530fSBill Paul 		f_print(fout, "#include <sys/types.h>\n");
68640ad8885SAlfred Perlstein 	}
687ff49530fSBill Paul 
6884f83fd19SStefan Farfeleder 	f_print(fout, "#include <string.h>\n");
689ff49530fSBill Paul 	if (inetdflag || !tirpcflag) {
690ff49530fSBill Paul 		f_print(fout, "#include <sys/socket.h>\n");
691ff49530fSBill Paul 		f_print(fout, "#include <netinet/in.h>\n");
692ff49530fSBill Paul 	}
693ff49530fSBill Paul 
694ff49530fSBill Paul 	if ((netflag || pmflag) && tirpcflag && !nomain) {
695ff49530fSBill Paul 		f_print(fout, "#include <netconfig.h>\n");
696ff49530fSBill Paul 	}
697ff49530fSBill Paul 	if (tirpcflag)
698ff49530fSBill Paul 		f_print(fout, "#include <sys/resource.h> /* rlimit */\n");
69940ad8885SAlfred Perlstein 	if (logflag || inetdflag || pmflag || tirpcflag)
700ff49530fSBill Paul 		f_print(fout, "#include <syslog.h>\n");
701ff49530fSBill Paul 
702ff49530fSBill Paul 	/* for ANSI-C */
703ff49530fSBill Paul 	if (Cflag)
704ff49530fSBill Paul 		f_print(fout,
705ff49530fSBill Paul 			"\n#ifndef SIG_PF\n#define	SIG_PF void(*)\
706ff49530fSBill Paul (int)\n#endif\n");
707ff49530fSBill Paul 
708ff49530fSBill Paul 	f_print(fout, "\n#ifdef DEBUG\n#define	RPC_SVC_FG\n#endif\n");
709ff49530fSBill Paul 	if (timerflag)
710ff49530fSBill Paul 		f_print(fout, "\n#define	_RPCSVC_CLOSEDOWN %s\n",
711ff49530fSBill Paul 			svcclosetime);
712526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
7134e115012SGarrett Wollman 		foundprogram |= (def->def_kind == DEF_PROGRAM);
7144e115012SGarrett Wollman 	}
7154e115012SGarrett Wollman 	if (extend && !foundprogram) {
7164e115012SGarrett Wollman 		(void) unlink(outfilename);
7174e115012SGarrett Wollman 		return;
7184e115012SGarrett Wollman 	}
719ff49530fSBill Paul 	write_most(infile, netflag, nomain);
720ff49530fSBill Paul 	if (!nomain) {
721ff49530fSBill Paul 		if (!do_registers(argc, argv)) {
722ff49530fSBill Paul 			if (outfilename)
723ff49530fSBill Paul 				(void) unlink(outfilename);
724ff49530fSBill Paul 			usage();
725ff49530fSBill Paul 		}
7264e115012SGarrett Wollman 		write_rest();
7274e115012SGarrett Wollman 	}
7284e115012SGarrett Wollman }
7294e115012SGarrett Wollman 
730ff49530fSBill Paul /*
731ff49530fSBill Paul  * generate client side stubs
732ff49530fSBill Paul  */
733526195adSJordan K. Hubbard static void
7344e115012SGarrett Wollman l_output(infile, define, extend, outfile)
7354e115012SGarrett Wollman 	char *infile;
7364e115012SGarrett Wollman 	char *define;
7374e115012SGarrett Wollman 	int extend;
7384e115012SGarrett Wollman 	char *outfile;
7394e115012SGarrett Wollman {
7404e115012SGarrett Wollman 	char *include;
7414e115012SGarrett Wollman 	definition *def;
742ff49530fSBill Paul 	int foundprogram = 0;
7434e115012SGarrett Wollman 	char *outfilename;
7444e115012SGarrett Wollman 
7454e115012SGarrett Wollman 	open_input(infile, define);
7464e115012SGarrett Wollman 	outfilename = extend ? extendfile(infile, outfile) : outfile;
7474e115012SGarrett Wollman 	open_output(infile, outfilename);
748ff49530fSBill Paul 	add_warning();
749ff49530fSBill Paul 	if (Cflag)
7504f83fd19SStefan Farfeleder 		f_print (fout, "#include <string.h> /* for memset */\n");
7514e115012SGarrett Wollman 	if (infile && (include = extendfile(infile, ".h"))) {
7524e115012SGarrett Wollman 		f_print(fout, "#include \"%s\"\n", include);
7534e115012SGarrett Wollman 		free(include);
754ff49530fSBill Paul 	} else
755ff49530fSBill Paul 		f_print(fout, "#include <rpc/rpc.h>\n");
756526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
7574e115012SGarrett Wollman 		foundprogram |= (def->def_kind == DEF_PROGRAM);
7584e115012SGarrett Wollman 	}
7594e115012SGarrett Wollman 	if (extend && !foundprogram) {
7604e115012SGarrett Wollman 		(void) unlink(outfilename);
7614e115012SGarrett Wollman 		return;
7624e115012SGarrett Wollman 	}
7634e115012SGarrett Wollman 	write_stubs();
7644e115012SGarrett Wollman }
7654e115012SGarrett Wollman 
7664e115012SGarrett Wollman /*
767ff49530fSBill Paul  * generate the dispatch table
7684e115012SGarrett Wollman  */
769526195adSJordan K. Hubbard static void
770ff49530fSBill Paul t_output(infile, define, extend, outfile)
771ff49530fSBill Paul 	char *infile;
772ff49530fSBill Paul 	char *define;
773ff49530fSBill Paul 	int extend;
774ff49530fSBill Paul 	char *outfile;
775ff49530fSBill Paul {
776ff49530fSBill Paul 	definition *def;
777ff49530fSBill Paul 	int foundprogram = 0;
778ff49530fSBill Paul 	char *outfilename;
779ff49530fSBill Paul 
780ff49530fSBill Paul 	open_input(infile, define);
781ff49530fSBill Paul 	outfilename = extend ? extendfile(infile, outfile) : outfile;
782ff49530fSBill Paul 	open_output(infile, outfilename);
783ff49530fSBill Paul 	add_warning();
784526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
785ff49530fSBill Paul 		foundprogram |= (def->def_kind == DEF_PROGRAM);
786ff49530fSBill Paul 	}
787ff49530fSBill Paul 	if (extend && !foundprogram) {
788ff49530fSBill Paul 		(void) unlink(outfilename);
789ff49530fSBill Paul 		return;
790ff49530fSBill Paul 	}
791ff49530fSBill Paul 	write_tables();
792ff49530fSBill Paul }
793ff49530fSBill Paul 
794ff49530fSBill Paul /* sample routine for the server template */
795526195adSJordan K. Hubbard static void
796ff49530fSBill Paul svc_output(infile, define, extend, outfile)
797ff49530fSBill Paul 	char *infile;
798ff49530fSBill Paul 	char *define;
799ff49530fSBill Paul 	int extend;
800ff49530fSBill Paul 	char *outfile;
801ff49530fSBill Paul {
802ff49530fSBill Paul 	definition *def;
803ff49530fSBill Paul 	char *include;
804ff49530fSBill Paul 	char *outfilename;
805ff49530fSBill Paul 	long tell;
806ff49530fSBill Paul 	open_input(infile, define);
807ff49530fSBill Paul 	outfilename = extend ? extendfile(infile, outfile) : outfile;
808ff49530fSBill Paul 	checkfiles(infile, outfilename);
809ff49530fSBill Paul 	/*
810ff49530fSBill Paul 	 * Check if outfile already exists.
811ff49530fSBill Paul 	 * if so, print an error message and exit
812ff49530fSBill Paul 	 */
813ff49530fSBill Paul 	open_output(infile, outfilename);
814ff49530fSBill Paul 	add_sample_msg();
815ff49530fSBill Paul 
816ff49530fSBill Paul 	if (infile && (include = extendfile(infile, ".h"))) {
817ff49530fSBill Paul 		f_print(fout, "#include \"%s\"\n", include);
818ff49530fSBill Paul 		free(include);
819ff49530fSBill Paul 	} else
820ff49530fSBill Paul 		f_print(fout, "#include <rpc/rpc.h>\n");
821ff49530fSBill Paul 
822ff49530fSBill Paul 	tell = ftell(fout);
823526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
824ff49530fSBill Paul 		write_sample_svc(def);
825ff49530fSBill Paul 	}
826ff49530fSBill Paul 	if (extend && tell == ftell(fout)) {
827ff49530fSBill Paul 		(void) unlink(outfilename);
828ff49530fSBill Paul 	}
829ff49530fSBill Paul }
830ff49530fSBill Paul 
831ff49530fSBill Paul /* sample main routine for client */
832526195adSJordan K. Hubbard static void
833ff49530fSBill Paul clnt_output(infile, define, extend, outfile)
834ff49530fSBill Paul 	char *infile;
835ff49530fSBill Paul 	char *define;
836ff49530fSBill Paul 	int extend;
837ff49530fSBill Paul 	char *outfile;
838ff49530fSBill Paul {
839ff49530fSBill Paul 	definition *def;
840ff49530fSBill Paul 	char *include;
841ff49530fSBill Paul 	char *outfilename;
842ff49530fSBill Paul 	long tell;
843ff49530fSBill Paul 	int has_program = 0;
844ff49530fSBill Paul 
845ff49530fSBill Paul 	open_input(infile, define);
846ff49530fSBill Paul 	outfilename = extend ? extendfile(infile, outfile) : outfile;
847ff49530fSBill Paul 	checkfiles(infile, outfilename);
848ff49530fSBill Paul 	/*
849ff49530fSBill Paul 	 * Check if outfile already exists.
850ff49530fSBill Paul 	 * if so, print an error message and exit
851ff49530fSBill Paul 	 */
852ff49530fSBill Paul 
853ff49530fSBill Paul 	open_output(infile, outfilename);
854ff49530fSBill Paul 	add_sample_msg();
855ff49530fSBill Paul 	if (infile && (include = extendfile(infile, ".h"))) {
856ff49530fSBill Paul 		f_print(fout, "#include \"%s\"\n", include);
857ff49530fSBill Paul 		free(include);
858ff49530fSBill Paul 	} else
859ff49530fSBill Paul 		f_print(fout, "#include <rpc/rpc.h>\n");
860ff49530fSBill Paul 	tell = ftell(fout);
861526195adSJordan K. Hubbard 	while ( (def = get_definition()) ) {
862ff49530fSBill Paul 		has_program += write_sample_clnt(def);
863ff49530fSBill Paul 	}
864ff49530fSBill Paul 
865ff49530fSBill Paul 	if (has_program)
866ff49530fSBill Paul 		write_sample_clnt_main();
867ff49530fSBill Paul 
868ff49530fSBill Paul 	if (extend && tell == ftell(fout)) {
869ff49530fSBill Paul 		(void) unlink(outfilename);
870ff49530fSBill Paul 	}
871ff49530fSBill Paul }
872ff49530fSBill Paul 
873ff49530fSBill Paul 
874ff49530fSBill Paul static void mkfile_output(cmd)
875ff49530fSBill Paul struct commandline *cmd;
876ff49530fSBill Paul {
877ff49530fSBill Paul 	char *mkfilename, *clientname, *clntname, *xdrname, *hdrname;
878ff49530fSBill Paul 	char *servername, *svcname, *servprogname, *clntprogname;
879ff49530fSBill Paul 	char *temp;
880ff49530fSBill Paul 
881ff49530fSBill Paul 	svcname = file_name(cmd->infile, "_svc.c");
882ff49530fSBill Paul 	clntname = file_name(cmd->infile, "_clnt.c");
883ff49530fSBill Paul 	xdrname = file_name(cmd->infile, "_xdr.c");
884ff49530fSBill Paul 	hdrname = file_name(cmd->infile, ".h");
885ff49530fSBill Paul 
886ff49530fSBill Paul 
887ff49530fSBill Paul 	if (allfiles){
888ff49530fSBill Paul 		servername = extendfile(cmd->infile, "_server.c");
889ff49530fSBill Paul 		clientname = extendfile(cmd->infile, "_client.c");
890ff49530fSBill Paul 	}else{
891ff49530fSBill Paul 		servername = " ";
892ff49530fSBill Paul 		clientname = " ";
893ff49530fSBill Paul 	}
894ff49530fSBill Paul 	servprogname = extendfile(cmd->infile, "_server");
895ff49530fSBill Paul 	clntprogname = extendfile(cmd->infile, "_client");
896ff49530fSBill Paul 
897ff49530fSBill Paul 	if (allfiles){
89875863a6dSPhilippe Charnier 		mkfilename = xmalloc(strlen("makefile.") +
899ff49530fSBill Paul 		                     strlen(cmd->infile) + 1);
900ff49530fSBill Paul 		temp = (char *)rindex(cmd->infile, '.');
901c5455e6dSMartin Blapp 		strcpy(mkfilename, "makefile.");
902ff49530fSBill Paul 		(void) strncat(mkfilename, cmd->infile,
903ff49530fSBill Paul 			(temp - cmd->infile));
904ff49530fSBill Paul 	} else
905ff49530fSBill Paul 		mkfilename = cmd->outfile;
906ff49530fSBill Paul 
907ff49530fSBill Paul 
908ff49530fSBill Paul 	checkfiles(NULL, mkfilename);
909ff49530fSBill Paul 	open_output(NULL, mkfilename);
910ff49530fSBill Paul 
911ff49530fSBill Paul 	f_print(fout, "\n# This is a template makefile generated\
912ff49530fSBill Paul 		by rpcgen \n");
913ff49530fSBill Paul 
914ff49530fSBill Paul 	f_print(fout, "\n# Parameters \n\n");
915ff49530fSBill Paul 
916ff49530fSBill Paul 	f_print(fout, "CLIENT = %s\nSERVER = %s\n\n",
917ff49530fSBill Paul 		clntprogname, servprogname);
918ff49530fSBill Paul 	f_print(fout, "SOURCES_CLNT.c = \nSOURCES_CLNT.h = \n");
919ff49530fSBill Paul 	f_print(fout, "SOURCES_SVC.c = \nSOURCES_SVC.h = \n");
920ff49530fSBill Paul 	f_print(fout, "SOURCES.x = %s\n\n", cmd->infile);
921ff49530fSBill Paul 	f_print(fout, "TARGETS_SVC.c = %s %s %s \n",
922ff49530fSBill Paul 		svcname, servername, xdrname);
923ff49530fSBill Paul 	f_print(fout, "TARGETS_CLNT.c = %s %s %s \n",
924ff49530fSBill Paul 		clntname, clientname, xdrname);
925ff49530fSBill Paul 	f_print(fout, "TARGETS = %s %s %s %s %s %s\n\n",
926ff49530fSBill Paul 		hdrname, xdrname, clntname,
927ff49530fSBill Paul 		svcname, clientname, servername);
928ff49530fSBill Paul 
929ff49530fSBill Paul 	f_print(fout, "OBJECTS_CLNT = $(SOURCES_CLNT.c:%%.c=%%.o) \
930ff49530fSBill Paul $(TARGETS_CLNT.c:%%.c=%%.o) ");
931ff49530fSBill Paul 
932ff49530fSBill Paul 	f_print(fout, "\nOBJECTS_SVC = $(SOURCES_SVC.c:%%.c=%%.o) \
933ff49530fSBill Paul $(TARGETS_SVC.c:%%.c=%%.o) ");
934ff49530fSBill Paul 
935ff49530fSBill Paul 
936ff49530fSBill Paul 	f_print(fout, "\n# Compiler flags \n");
937ff49530fSBill Paul 	if (mtflag)
93840ad8885SAlfred Perlstein 		f_print(fout, "\nCFLAGS += -D_REENTRANT -D_THEAD_SAFE \nLDLIBS += -pthread\n");
93940ad8885SAlfred Perlstein 
940ff49530fSBill Paul 	f_print(fout, "RPCGENFLAGS = \n");
941ff49530fSBill Paul 
942ff49530fSBill Paul 	f_print(fout, "\n# Targets \n\n");
943ff49530fSBill Paul 
944ff49530fSBill Paul 	f_print(fout, "all : $(CLIENT) $(SERVER)\n\n");
945ff49530fSBill Paul 	f_print(fout, "$(TARGETS) : $(SOURCES.x) \n");
946ff49530fSBill Paul 	f_print(fout, "\trpcgen $(RPCGENFLAGS) $(SOURCES.x)\n\n");
947ff49530fSBill Paul 	f_print(fout, "$(OBJECTS_CLNT) : $(SOURCES_CLNT.c) $(SOURCES_CLNT.h) \
948ff49530fSBill Paul $(TARGETS_CLNT.c) \n\n");
949ff49530fSBill Paul 
950ff49530fSBill Paul 	f_print(fout, "$(OBJECTS_SVC) : $(SOURCES_SVC.c) $(SOURCES_SVC.h) \
951ff49530fSBill Paul $(TARGETS_SVC.c) \n\n");
952ff49530fSBill Paul 	f_print(fout, "$(CLIENT) : $(OBJECTS_CLNT) \n");
953ff49530fSBill Paul 	f_print(fout, "\t$(CC) -o $(CLIENT) $(OBJECTS_CLNT) \
954ff49530fSBill Paul $(LDLIBS) \n\n");
955ff49530fSBill Paul 	f_print(fout, "$(SERVER) : $(OBJECTS_SVC) \n");
956ff49530fSBill Paul 	f_print(fout, "\t$(CC) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS)\n\n ");
957ff49530fSBill Paul 	f_print(fout, "clean:\n\t $(RM) -f core $(TARGETS) $(OBJECTS_CLNT) \
958ff49530fSBill Paul $(OBJECTS_SVC) $(CLIENT) $(SERVER)\n\n");
959ff49530fSBill Paul }
960ff49530fSBill Paul 
961ff49530fSBill Paul 
962ff49530fSBill Paul 
963ff49530fSBill Paul /*
964ff49530fSBill Paul  * Perform registrations for service output
965ff49530fSBill Paul  * Return 0 if failed; 1 otherwise.
966ff49530fSBill Paul  */
967526195adSJordan K. Hubbard static int
968526195adSJordan K. Hubbard do_registers(argc, argv)
9694e115012SGarrett Wollman 	int argc;
9704e115012SGarrett Wollman 	char *argv[];
9714e115012SGarrett Wollman {
9724e115012SGarrett Wollman 	int i;
9734e115012SGarrett Wollman 
974ff49530fSBill Paul 	if (inetdflag || !tirpcflag) {
9754e115012SGarrett Wollman 		for (i = 1; i < argc; i++) {
9764e115012SGarrett Wollman 			if (streq(argv[i], "-s")) {
977ff49530fSBill Paul 				if (!check_nettype(argv[i + 1],
978ff49530fSBill Paul 						    valid_i_nettypes))
979ff49530fSBill Paul 					return (0);
980ff49530fSBill Paul 				write_inetd_register(argv[i + 1]);
9814e115012SGarrett Wollman 				i++;
9824e115012SGarrett Wollman 			}
9834e115012SGarrett Wollman 		}
984ff49530fSBill Paul 	} else {
985ff49530fSBill Paul 		for (i = 1; i < argc; i++)
986ff49530fSBill Paul 			if (streq(argv[i], "-s")) {
987ff49530fSBill Paul 				if (!check_nettype(argv[i + 1],
988ff49530fSBill Paul 						    valid_ti_nettypes))
989ff49530fSBill Paul 					return (0);
990ff49530fSBill Paul 				write_nettype_register(argv[i + 1]);
991ff49530fSBill Paul 				i++;
992ff49530fSBill Paul 			} else if (streq(argv[i], "-n")) {
993ff49530fSBill Paul 				write_netid_register(argv[i + 1]);
994ff49530fSBill Paul 				i++;
995ff49530fSBill Paul 			}
996ff49530fSBill Paul 	}
997ff49530fSBill Paul 	return (1);
998ff49530fSBill Paul }
999ff49530fSBill Paul 
1000ff49530fSBill Paul /*
1001ff49530fSBill Paul  * Add another argument to the arg list
1002ff49530fSBill Paul  */
1003ff49530fSBill Paul static void
1004ff49530fSBill Paul addarg(cp)
1005ff49530fSBill Paul 	char *cp;
1006ff49530fSBill Paul {
1007ff49530fSBill Paul 	if (argcount >= ARGLISTLEN) {
10080e76f40dSPhilippe Charnier 		warnx("too many defines");
1009ff49530fSBill Paul 		crash();
1010ff49530fSBill Paul 		/*NOTREACHED*/
1011ff49530fSBill Paul 	}
1012ff49530fSBill Paul 	arglist[argcount++] = cp;
1013ff49530fSBill Paul 
1014ff49530fSBill Paul }
1015ff49530fSBill Paul 
1016ff49530fSBill Paul static void
1017ff49530fSBill Paul putarg(where, cp)
1018ff49530fSBill Paul 	char *cp;
1019ff49530fSBill Paul 	int where;
1020ff49530fSBill Paul {
1021ff49530fSBill Paul 	if (where >= ARGLISTLEN) {
10220e76f40dSPhilippe Charnier 		warnx("arglist coding error");
1023ff49530fSBill Paul 		crash();
1024ff49530fSBill Paul 		/*NOTREACHED*/
1025ff49530fSBill Paul 	}
1026ff49530fSBill Paul 	arglist[where] = cp;
1027ff49530fSBill Paul }
1028ff49530fSBill Paul 
1029ff49530fSBill Paul /*
1030ff49530fSBill Paul  * if input file is stdin and an output file is specified then complain
1031ff49530fSBill Paul  * if the file already exists. Otherwise the file may get overwritten
1032ff49530fSBill Paul  * If input file does not exist, exit with an error
1033ff49530fSBill Paul  */
1034ff49530fSBill Paul 
1035ff49530fSBill Paul static void
1036ff49530fSBill Paul checkfiles(infile, outfile)
1037ff49530fSBill Paul char *infile;
1038ff49530fSBill Paul char *outfile;
1039ff49530fSBill Paul {
1040ff49530fSBill Paul 
1041ff49530fSBill Paul 	struct stat buf;
1042ff49530fSBill Paul 
1043ff49530fSBill Paul 	if (infile)		/* infile ! = NULL */
1044ff49530fSBill Paul 		if (stat(infile, &buf) < 0)
1045ff49530fSBill Paul 		{
10460e76f40dSPhilippe Charnier 			warn("%s", infile);
1047ff49530fSBill Paul 			crash();
1048ff49530fSBill Paul 		};
1049ff49530fSBill Paul 	if (outfile) {
1050ff49530fSBill Paul 		if (stat(outfile, &buf) < 0)
1051ff49530fSBill Paul 			return;	/* file does not exist */
1052ff49530fSBill Paul 		else {
10530e76f40dSPhilippe Charnier 			warnx("file '%s' already exists and may be overwritten", outfile);
1054ff49530fSBill Paul 			crash();
1055ff49530fSBill Paul 		}
1056ff49530fSBill Paul 	}
10574e115012SGarrett Wollman }
10584e115012SGarrett Wollman 
10594e115012SGarrett Wollman /*
10604e115012SGarrett Wollman  * Parse command line arguments
10614e115012SGarrett Wollman  */
1062526195adSJordan K. Hubbard static int
10634e115012SGarrett Wollman parseargs(argc, argv, cmd)
10644e115012SGarrett Wollman 	int argc;
10654e115012SGarrett Wollman 	char *argv[];
10664e115012SGarrett Wollman 	struct commandline *cmd;
10674e115012SGarrett Wollman {
10684e115012SGarrett Wollman 	int i;
10694e115012SGarrett Wollman 	int j;
1070ff49530fSBill Paul 	char c, ch;
10714e115012SGarrett Wollman 	char flag[(1 << 8 * sizeof (char))];
10724e115012SGarrett Wollman 	int nflags;
10734e115012SGarrett Wollman 
10744e115012SGarrett Wollman 	cmd->infile = cmd->outfile = NULL;
10754e115012SGarrett Wollman 	if (argc < 2) {
10764e115012SGarrett Wollman 		return (0);
10774e115012SGarrett Wollman 	}
1078ff49530fSBill Paul 	allfiles = 0;
10794e115012SGarrett Wollman 	flag['c'] = 0;
10804e115012SGarrett Wollman 	flag['h'] = 0;
10814e115012SGarrett Wollman 	flag['l'] = 0;
10824e115012SGarrett Wollman 	flag['m'] = 0;
1083ff49530fSBill Paul 	flag['o'] = 0;
1084ff49530fSBill Paul 	flag['s'] = 0;
1085ff49530fSBill Paul 	flag['n'] = 0;
1086ff49530fSBill Paul 	flag['t'] = 0;
1087ff49530fSBill Paul 	flag['S'] = 0;
1088ff49530fSBill Paul 	flag['C'] = 0;
1089ff49530fSBill Paul 	flag['M'] = 0;
1090ff49530fSBill Paul 
10914e115012SGarrett Wollman 	for (i = 1; i < argc; i++) {
10924e115012SGarrett Wollman 		if (argv[i][0] != '-') {
10934e115012SGarrett Wollman 			if (cmd->infile) {
10940e76f40dSPhilippe Charnier 				warnx("cannot specify more than one input file");
10954e115012SGarrett Wollman 				return (0);
10964e115012SGarrett Wollman 			}
10974e115012SGarrett Wollman 			cmd->infile = argv[i];
10984e115012SGarrett Wollman 		} else {
10994e115012SGarrett Wollman 			for (j = 1; argv[i][j] != 0; j++) {
11004e115012SGarrett Wollman 				c = argv[i][j];
11014e115012SGarrett Wollman 				switch (c) {
1102ff49530fSBill Paul 				case 'a':
1103ff49530fSBill Paul 					allfiles = 1;
1104ff49530fSBill Paul 					break;
11054e115012SGarrett Wollman 				case 'c':
11064e115012SGarrett Wollman 				case 'h':
11074e115012SGarrett Wollman 				case 'l':
11084e115012SGarrett Wollman 				case 'm':
1109ff49530fSBill Paul 				case 't':
1110526195adSJordan K. Hubbard 					if (flag[(int)c]) {
11114e115012SGarrett Wollman 						return (0);
11124e115012SGarrett Wollman 					}
1113526195adSJordan K. Hubbard 					flag[(int)c] = 1;
11144e115012SGarrett Wollman 					break;
1115ff49530fSBill Paul 				case 'S':
1116ff49530fSBill Paul 					/*
1117ff49530fSBill Paul 					 * sample flag: Ss or Sc.
1118ff49530fSBill Paul 					 *  Ss means set flag['S'];
1119ff49530fSBill Paul 					 *  Sc means set flag['C'];
1120ff49530fSBill Paul 					 *  Sm means set flag['M'];
1121ff49530fSBill Paul 					 */
1122ff49530fSBill Paul 					ch = argv[i][++j]; /* get next char */
1123ff49530fSBill Paul 					if (ch == 's')
1124ff49530fSBill Paul 						ch = 'S';
1125ff49530fSBill Paul 					else if (ch == 'c')
1126ff49530fSBill Paul 						ch = 'C';
1127ff49530fSBill Paul 					else if (ch == 'm')
1128ff49530fSBill Paul 						ch = 'M';
1129ff49530fSBill Paul 					else
1130ff49530fSBill Paul 						return (0);
1131ff49530fSBill Paul 
1132526195adSJordan K. Hubbard 					if (flag[(int)ch]) {
1133ff49530fSBill Paul 						return (0);
1134ff49530fSBill Paul 					}
1135526195adSJordan K. Hubbard 					flag[(int)ch] = 1;
1136ff49530fSBill Paul 					break;
1137ff49530fSBill Paul 				case 'C': /* ANSI C syntax */
1138ff49530fSBill Paul 					Cflag = 1;
1139ff49530fSBill Paul 					ch = argv[i][j+1]; /* get next char */
1140ff49530fSBill Paul 
1141ff49530fSBill Paul 					if (ch != 'C')
1142ff49530fSBill Paul 						break;
1143ff49530fSBill Paul 					CCflag = 1;
1144ff49530fSBill Paul 					break;
1145ff49530fSBill Paul 				case 'b':
1146ff49530fSBill Paul 					/*
1147ff49530fSBill Paul 					 *  Turn TIRPC flag off for
1148ff49530fSBill Paul 					 *  generating backward compatible
1149ff49530fSBill Paul 					 *  code
1150ff49530fSBill Paul 					 */
1151ff49530fSBill Paul 					tirpcflag = 0;
1152ff49530fSBill Paul 					break;
1153ff49530fSBill Paul 
1154ff49530fSBill Paul 				case 'I':
1155ff49530fSBill Paul 					inetdflag = 1;
1156ff49530fSBill Paul 					break;
1157ff49530fSBill Paul 				case 'N':
1158ff49530fSBill Paul 					newstyle = 1;
1159ff49530fSBill Paul 					break;
1160ff49530fSBill Paul 				case 'L':
1161ff49530fSBill Paul 					logflag = 1;
1162ff49530fSBill Paul 					break;
116340ad8885SAlfred Perlstein 				case 'P':
116440ad8885SAlfred Perlstein 					pmflag = 1;
116540ad8885SAlfred Perlstein 					break;
1166ff49530fSBill Paul 				case 'K':
1167ff49530fSBill Paul 					if (++i == argc) {
1168ff49530fSBill Paul 						return (0);
1169ff49530fSBill Paul 					}
1170ff49530fSBill Paul 					svcclosetime = argv[i];
1171ff49530fSBill Paul 					goto nextarg;
1172ff49530fSBill Paul 				case 'T':
1173ff49530fSBill Paul 					tblflag = 1;
1174ff49530fSBill Paul 					break;
1175ff49530fSBill Paul 				case 'M':
1176ff49530fSBill Paul 					mtflag = 1;
1177ff49530fSBill Paul 					break;
1178ff49530fSBill Paul 				case 'i' :
1179ff49530fSBill Paul 					if (++i == argc) {
1180ff49530fSBill Paul 						return (0);
1181ff49530fSBill Paul 					}
1182ff49530fSBill Paul 					inline = atoi(argv[i]);
1183ff49530fSBill Paul 					goto nextarg;
1184ff49530fSBill Paul 				case 'n':
11854e115012SGarrett Wollman 				case 'o':
11864e115012SGarrett Wollman 				case 's':
11874e115012SGarrett Wollman 					if (argv[i][j - 1] != '-' ||
11884e115012SGarrett Wollman 					    argv[i][j + 1] != 0) {
11894e115012SGarrett Wollman 						return (0);
11904e115012SGarrett Wollman 					}
1191526195adSJordan K. Hubbard 					flag[(int)c] = 1;
11924e115012SGarrett Wollman 					if (++i == argc) {
11934e115012SGarrett Wollman 						return (0);
11944e115012SGarrett Wollman 					}
1195ff49530fSBill Paul 					if (c == 'o') {
11964e115012SGarrett Wollman 						if (cmd->outfile) {
11974e115012SGarrett Wollman 							return (0);
11984e115012SGarrett Wollman 						}
11994e115012SGarrett Wollman 						cmd->outfile = argv[i];
12004e115012SGarrett Wollman 					}
12014e115012SGarrett Wollman 					goto nextarg;
1202ff49530fSBill Paul 				case 'D':
1203ff49530fSBill Paul 					if (argv[i][j - 1] != '-') {
1204ff49530fSBill Paul 						return (0);
1205ff49530fSBill Paul 					}
1206ff49530fSBill Paul 					(void) addarg(argv[i]);
1207ff49530fSBill Paul 					goto nextarg;
1208ff49530fSBill Paul 				case 'Y':
1209ff49530fSBill Paul 					if (++i == argc) {
1210ff49530fSBill Paul 						return (0);
1211ff49530fSBill Paul 					}
1212a0b13740SKris Kennaway 					(void) strlcpy(pathbuf, argv[i], sizeof(pathbuf));
1213a0b13740SKris Kennaway 					if (strlcat(pathbuf, "/cpp", sizeof(pathbuf))
1214a0b13740SKris Kennaway 					    >= sizeof(pathbuf)) {
1215a0b13740SKris Kennaway 						warnx("argument too long");
1216a0b13740SKris Kennaway 						return (0);
1217a0b13740SKris Kennaway 					}
1218ff49530fSBill Paul 					CPP = pathbuf;
1219ff49530fSBill Paul 					cppDefined = 1;
1220ff49530fSBill Paul 					goto nextarg;
1221ff49530fSBill Paul 
1222ff49530fSBill Paul 
12234e115012SGarrett Wollman 
12244e115012SGarrett Wollman 				default:
12254e115012SGarrett Wollman 					return (0);
12264e115012SGarrett Wollman 				}
12274e115012SGarrett Wollman 			}
12284e115012SGarrett Wollman 		nextarg:
12294e115012SGarrett Wollman 			;
12304e115012SGarrett Wollman 		}
12314e115012SGarrett Wollman 	}
1232ff49530fSBill Paul 
12334e115012SGarrett Wollman 	cmd->cflag = flag['c'];
12344e115012SGarrett Wollman 	cmd->hflag = flag['h'];
12354e115012SGarrett Wollman 	cmd->lflag = flag['l'];
12364e115012SGarrett Wollman 	cmd->mflag = flag['m'];
1237ff49530fSBill Paul 	cmd->nflag = flag['n'];
1238ff49530fSBill Paul 	cmd->sflag = flag['s'];
1239ff49530fSBill Paul 	cmd->tflag = flag['t'];
1240ff49530fSBill Paul 	cmd->Ssflag = flag['S'];
1241ff49530fSBill Paul 	cmd->Scflag = flag['C'];
1242ff49530fSBill Paul 	cmd->makefileflag = flag['M'];
1243ff49530fSBill Paul 
1244ff49530fSBill Paul 	if (tirpcflag) {
124540ad8885SAlfred Perlstein 		if (inetdflag)
124640ad8885SAlfred Perlstein 			pmflag = 0;
1247ff49530fSBill Paul 		if ((inetdflag && cmd->nflag)) {
1248ff49530fSBill Paul 			/* netid not allowed with inetdflag */
12490e76f40dSPhilippe Charnier 			warnx("cannot use netid flag with inetd flag");
1250ff49530fSBill Paul 			return (0);
1251ff49530fSBill Paul 		}
1252ff49530fSBill Paul 	} else {		/* 4.1 mode */
1253ff49530fSBill Paul 		pmflag = 0;	/* set pmflag only in tirpcmode */
1254ff49530fSBill Paul 		if (cmd->nflag) { /* netid needs TIRPC */
12550e76f40dSPhilippe Charnier 			warnx("cannot use netid flag without TIRPC");
1256ff49530fSBill Paul 			return (0);
1257ff49530fSBill Paul 		}
1258ff49530fSBill Paul 	}
1259ff49530fSBill Paul 
1260ff49530fSBill Paul 	if (newstyle && (tblflag || cmd->tflag)) {
12610e76f40dSPhilippe Charnier 		warnx("cannot use table flags with newstyle");
1262ff49530fSBill Paul 		return (0);
1263ff49530fSBill Paul 	}
1264ff49530fSBill Paul 
1265ff49530fSBill Paul 	/* check no conflicts with file generation flags */
1266ff49530fSBill Paul 	nflags = cmd->cflag + cmd->hflag + cmd->lflag + cmd->mflag +
1267ff49530fSBill Paul 		cmd->sflag + cmd->nflag + cmd->tflag + cmd->Ssflag +
1268ff49530fSBill Paul 			cmd->Scflag + cmd->makefileflag;
1269ff49530fSBill Paul 
12704e115012SGarrett Wollman 	if (nflags == 0) {
12714e115012SGarrett Wollman 		if (cmd->outfile != NULL || cmd->infile == NULL) {
12724e115012SGarrett Wollman 			return (0);
12734e115012SGarrett Wollman 		}
1274ff49530fSBill Paul 	} else if (cmd->infile == NULL &&
1275ff49530fSBill Paul 	    (cmd->Ssflag || cmd->Scflag || cmd->makefileflag)) {
12760e76f40dSPhilippe Charnier 		warnx("\"infile\" is required for template generation flags");
1277ff49530fSBill Paul 		return (0);
1278ff49530fSBill Paul 	} if (nflags > 1) {
12790e76f40dSPhilippe Charnier 		warnx("cannot have more than one file generation flag");
12804e115012SGarrett Wollman 		return (0);
12814e115012SGarrett Wollman 	}
12824e115012SGarrett Wollman 	return (1);
12834e115012SGarrett Wollman }
1284ff49530fSBill Paul 
1285526195adSJordan K. Hubbard static void
1286ff49530fSBill Paul usage()
1287ff49530fSBill Paul {
12880e76f40dSPhilippe Charnier 	f_print(stderr, "%s\n%s\n%s\n%s\n%s\n",
12890e76f40dSPhilippe Charnier 		"usage: rpcgen infile",
12900e76f40dSPhilippe Charnier 		"       rpcgen [-abCLNTM] [-Dname[=value]] [-i size]\
129140ad8885SAlfred Perlstein [-I -P [-K seconds]] [-Y path] infile",
12920e76f40dSPhilippe Charnier 		"       rpcgen [-c | -h | -l | -m | -t | -Sc | -Ss | -Sm]\
12930e76f40dSPhilippe Charnier [-o outfile] [infile]",
12940e76f40dSPhilippe Charnier 		"       rpcgen [-s nettype]* [-o outfile] [infile]",
12950e76f40dSPhilippe Charnier 		"       rpcgen [-n netid]* [-o outfile] [infile]");
1296ff49530fSBill Paul 	options_usage();
1297ff49530fSBill Paul 	exit(1);
1298ff49530fSBill Paul }
1299ff49530fSBill Paul 
1300526195adSJordan K. Hubbard static void
1301ff49530fSBill Paul options_usage()
1302ff49530fSBill Paul {
1303ff49530fSBill Paul 	f_print(stderr, "options:\n");
1304ff49530fSBill Paul 	f_print(stderr, "-a\t\tgenerate all files, including samples\n");
1305ff49530fSBill Paul 	f_print(stderr, "-b\t\tbackward compatibility mode (generates code \
130640ad8885SAlfred Perlstein for FreeBSD 4.X)\n");
1307ff49530fSBill Paul 	f_print(stderr, "-c\t\tgenerate XDR routines\n");
1308ff49530fSBill Paul 	f_print(stderr, "-C\t\tANSI C mode\n");
1309ff49530fSBill Paul 	f_print(stderr, "-Dname[=value]\tdefine a symbol (same as #define)\n");
1310ff49530fSBill Paul 	f_print(stderr, "-h\t\tgenerate header file\n");
1311ff49530fSBill Paul 	f_print(stderr, "-i size\t\tsize at which to start generating\
1312ff49530fSBill Paul inline code\n");
131340ad8885SAlfred Perlstein 	f_print(stderr, "-I\t\tgenerate code for inetd support in server\n");
1314ff49530fSBill Paul 	f_print(stderr, "-K seconds\tserver exits after K seconds of\
1315ff49530fSBill Paul inactivity\n");
1316ff49530fSBill Paul 	f_print(stderr, "-l\t\tgenerate client side stubs\n");
1317ff49530fSBill Paul 	f_print(stderr, "-L\t\tserver errors will be printed to syslog\n");
1318ff49530fSBill Paul 	f_print(stderr, "-m\t\tgenerate server side stubs\n");
1319ff49530fSBill Paul 	f_print(stderr, "-M\t\tgenerate MT-safe code\n");
1320ff49530fSBill Paul 	f_print(stderr, "-n netid\tgenerate server code that supports\
1321ff49530fSBill Paul named netid\n");
1322ff49530fSBill Paul 	f_print(stderr, "-N\t\tsupports multiple arguments and\
1323ff49530fSBill Paul call-by-value\n");
1324ff49530fSBill Paul 	f_print(stderr, "-o outfile\tname of the output file\n");
132540ad8885SAlfred Perlstein 	f_print(stderr, "-P\t\tgenerate code for port monitoring support in server\n");
1326ff49530fSBill Paul 	f_print(stderr, "-s nettype\tgenerate server code that supports named\
1327ff49530fSBill Paul nettype\n");
1328ff49530fSBill Paul 	f_print(stderr, "-Sc\t\tgenerate sample client code that uses remote\
1329ff49530fSBill Paul procedures\n");
1330ff49530fSBill Paul 	f_print(stderr, "-Ss\t\tgenerate sample server code that defines\
1331ff49530fSBill Paul remote procedures\n");
1332ff49530fSBill Paul 	f_print(stderr, "-Sm \t\tgenerate makefile template \n");
1333ff49530fSBill Paul 
1334ff49530fSBill Paul 	f_print(stderr, "-t\t\tgenerate RPC dispatch table\n");
1335ff49530fSBill Paul 	f_print(stderr, "-T\t\tgenerate code to support RPC dispatch tables\n");
1336ff49530fSBill Paul 	f_print(stderr, "-Y path\t\tpath where cpp is found\n");
1337ff49530fSBill Paul 	exit(1);
1338ff49530fSBill Paul }
1339