xref: /titanic_53/usr/src/cmd/rpcgen/rpc_main.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */
28*7c478bd9Sstevel@tonic-gate /*
29*7c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
30*7c478bd9Sstevel@tonic-gate  * The Regents of the University of California
31*7c478bd9Sstevel@tonic-gate  * All Rights Reserved
32*7c478bd9Sstevel@tonic-gate  *
33*7c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
34*7c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
35*7c478bd9Sstevel@tonic-gate  * contributors.
36*7c478bd9Sstevel@tonic-gate  */
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate /*
41*7c478bd9Sstevel@tonic-gate  * rpc_main.c, Top level of the RPC protocol compiler.
42*7c478bd9Sstevel@tonic-gate  */
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate #include <stdio.h>
45*7c478bd9Sstevel@tonic-gate #include <string.h>
46*7c478bd9Sstevel@tonic-gate #include <unistd.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/file.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
51*7c478bd9Sstevel@tonic-gate #include "rpc_parse.h"
52*7c478bd9Sstevel@tonic-gate #include "rpc_util.h"
53*7c478bd9Sstevel@tonic-gate #include "rpc_scan.h"
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate extern void  write_sample_svc();
57*7c478bd9Sstevel@tonic-gate int write_sample_clnt();
58*7c478bd9Sstevel@tonic-gate void write_sample_clnt_main();
59*7c478bd9Sstevel@tonic-gate char *rindex();
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate static int svc_output();
62*7c478bd9Sstevel@tonic-gate static void mkfile_output();
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate #define	EXTEND	1		/* alias for TRUE */
65*7c478bd9Sstevel@tonic-gate #define	DONT_EXTEND	0		/* alias for FALSE */
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate #define	SUNOS_CPP "/usr/lib/cpp"
68*7c478bd9Sstevel@tonic-gate static int cppDefined = 0;	/* explicit path for C preprocessor */
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate static char *cmdname;
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate static char *svcclosetime = "120";
74*7c478bd9Sstevel@tonic-gate static char *CPP = SUNOS_CPP;
75*7c478bd9Sstevel@tonic-gate static char CPPFLAGS[] = "-C";
76*7c478bd9Sstevel@tonic-gate static char pathbuf[MAXPATHLEN + 1];
77*7c478bd9Sstevel@tonic-gate static char *allv[] = {
78*7c478bd9Sstevel@tonic-gate 	"rpcgen", "-s", "udp", "-s", "tcp",
79*7c478bd9Sstevel@tonic-gate };
80*7c478bd9Sstevel@tonic-gate static int allc = sizeof (allv)/sizeof (allv[0]);
81*7c478bd9Sstevel@tonic-gate static char *allnv[] = {
82*7c478bd9Sstevel@tonic-gate 	"rpcgen", "-s", "netpath",
83*7c478bd9Sstevel@tonic-gate };
84*7c478bd9Sstevel@tonic-gate static int allnc = sizeof (allnv)/sizeof (allnv[0]);
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate /*
87*7c478bd9Sstevel@tonic-gate  * machinations for handling expanding argument list
88*7c478bd9Sstevel@tonic-gate  */
89*7c478bd9Sstevel@tonic-gate static void addarg();		/* add another argument to the list */
90*7c478bd9Sstevel@tonic-gate static void putarg();		/* put argument at specified location  */
91*7c478bd9Sstevel@tonic-gate static void clear_args();	/* clear argument list */
92*7c478bd9Sstevel@tonic-gate static void checkfiles();	/* check if out file already exists */
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate #define	ARGLISTLEN	20
97*7c478bd9Sstevel@tonic-gate #define	FIXEDARGS	2
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate static char *arglist[ARGLISTLEN];
100*7c478bd9Sstevel@tonic-gate static int argcount = FIXEDARGS;
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate int nonfatalerrors;	/* errors */
104*7c478bd9Sstevel@tonic-gate int inetdflag;	/* Support for inetd  is now the default */
105*7c478bd9Sstevel@tonic-gate int pmflag;		/* Support for port monitors */
106*7c478bd9Sstevel@tonic-gate int logflag;		/* Use syslog instead of fprintf for errors */
107*7c478bd9Sstevel@tonic-gate int tblflag;		/* Support for dispatch table file */
108*7c478bd9Sstevel@tonic-gate int mtflag = 0;		/* Support for MT */
109*7c478bd9Sstevel@tonic-gate int mtauto = 0;		/* Enable automatic mode */
110*7c478bd9Sstevel@tonic-gate int rflag = 1;		/* Eliminate tail recursion from structures */
111*7c478bd9Sstevel@tonic-gate #define	INLINE 5
112*7c478bd9Sstevel@tonic-gate /* length at which to start doing an inline */
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate int inlinelen = INLINE;
115*7c478bd9Sstevel@tonic-gate /*
116*7c478bd9Sstevel@tonic-gate  * Length at which to start doing an inline. INLINE = default
117*7c478bd9Sstevel@tonic-gate  * if 0, no xdr_inline code
118*7c478bd9Sstevel@tonic-gate  */
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate int indefinitewait;	/* If started by port monitors, hang till it wants */
121*7c478bd9Sstevel@tonic-gate int exitnow;		/* If started by port monitors, exit after the call */
122*7c478bd9Sstevel@tonic-gate int timerflag;		/* TRUE if !indefinite && !exitnow */
123*7c478bd9Sstevel@tonic-gate int newstyle;		/* newstyle of passing arguments (by value) */
124*7c478bd9Sstevel@tonic-gate int Cflag = 0;		/* ANSI C syntax */
125*7c478bd9Sstevel@tonic-gate int CCflag = 0;		/* C++ files */
126*7c478bd9Sstevel@tonic-gate static int allfiles;   /* generate all files */
127*7c478bd9Sstevel@tonic-gate int tirpcflag = 1;    /* generating code for tirpc, by default */
128*7c478bd9Sstevel@tonic-gate xdrfunc *xdrfunc_head = NULL; /* xdr function list */
129*7c478bd9Sstevel@tonic-gate xdrfunc *xdrfunc_tail = NULL; /* xdr function list */
130*7c478bd9Sstevel@tonic-gate pid_t childpid;
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate main(argc, argv)
134*7c478bd9Sstevel@tonic-gate 	int argc;
135*7c478bd9Sstevel@tonic-gate 	char *argv[];
136*7c478bd9Sstevel@tonic-gate {
137*7c478bd9Sstevel@tonic-gate 	struct commandline cmd;
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	(void) memset((char *)&cmd, 0, sizeof (struct commandline));
140*7c478bd9Sstevel@tonic-gate 	clear_args();
141*7c478bd9Sstevel@tonic-gate 	if (!parseargs(argc, argv, &cmd))
142*7c478bd9Sstevel@tonic-gate 		usage();
143*7c478bd9Sstevel@tonic-gate 	/*
144*7c478bd9Sstevel@tonic-gate 	 * Only the client and server side stubs are likely to be customized,
145*7c478bd9Sstevel@tonic-gate 	 *  so in that case only, check if the outfile exists, and if so,
146*7c478bd9Sstevel@tonic-gate 	 *  print an error message and exit.
147*7c478bd9Sstevel@tonic-gate 	 */
148*7c478bd9Sstevel@tonic-gate 	if (cmd.Ssflag || cmd.Scflag || cmd.makefileflag) {
149*7c478bd9Sstevel@tonic-gate 		checkfiles(cmd.infile, cmd.outfile);
150*7c478bd9Sstevel@tonic-gate 	}
151*7c478bd9Sstevel@tonic-gate 	else
152*7c478bd9Sstevel@tonic-gate 		checkfiles(cmd.infile, NULL);
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 	if (cmd.cflag) {
155*7c478bd9Sstevel@tonic-gate 		c_output(cmd.infile, "-DRPC_XDR", DONT_EXTEND, cmd.outfile);
156*7c478bd9Sstevel@tonic-gate 	} else if (cmd.hflag) {
157*7c478bd9Sstevel@tonic-gate 		h_output(cmd.infile, "-DRPC_HDR", DONT_EXTEND, cmd.outfile);
158*7c478bd9Sstevel@tonic-gate 	} else if (cmd.lflag) {
159*7c478bd9Sstevel@tonic-gate 		l_output(cmd.infile, "-DRPC_CLNT", DONT_EXTEND, cmd.outfile);
160*7c478bd9Sstevel@tonic-gate 	} else if (cmd.sflag || cmd.mflag || (cmd.nflag)) {
161*7c478bd9Sstevel@tonic-gate 		s_output(argc, argv, cmd.infile, "-DRPC_SVC", DONT_EXTEND,
162*7c478bd9Sstevel@tonic-gate 			cmd.outfile, cmd.mflag, cmd.nflag);
163*7c478bd9Sstevel@tonic-gate 	} else if (cmd.tflag) {
164*7c478bd9Sstevel@tonic-gate 		t_output(cmd.infile, "-DRPC_TBL", DONT_EXTEND, cmd.outfile);
165*7c478bd9Sstevel@tonic-gate 	} else if (cmd.Ssflag) {
166*7c478bd9Sstevel@tonic-gate 		svc_output(cmd.infile, "-DRPC_SERVER", DONT_EXTEND,
167*7c478bd9Sstevel@tonic-gate 			cmd.outfile);
168*7c478bd9Sstevel@tonic-gate 	} else if (cmd.Scflag) {
169*7c478bd9Sstevel@tonic-gate 		clnt_output(cmd.infile, "-DRPC_CLIENT", DONT_EXTEND,
170*7c478bd9Sstevel@tonic-gate 			    cmd.outfile);
171*7c478bd9Sstevel@tonic-gate 	} else if (cmd.makefileflag) {
172*7c478bd9Sstevel@tonic-gate 		mkfile_output(&cmd);
173*7c478bd9Sstevel@tonic-gate 	} else {
174*7c478bd9Sstevel@tonic-gate 		/* the rescans are required, since cpp may effect input */
175*7c478bd9Sstevel@tonic-gate 		c_output(cmd.infile, "-DRPC_XDR", EXTEND, "_xdr.c");
176*7c478bd9Sstevel@tonic-gate 		reinitialize();
177*7c478bd9Sstevel@tonic-gate 		h_output(cmd.infile, "-DRPC_HDR", EXTEND, ".h");
178*7c478bd9Sstevel@tonic-gate 		reinitialize();
179*7c478bd9Sstevel@tonic-gate 		l_output(cmd.infile, "-DRPC_CLNT", EXTEND, "_clnt.c");
180*7c478bd9Sstevel@tonic-gate 		reinitialize();
181*7c478bd9Sstevel@tonic-gate 		if (inetdflag || !tirpcflag)
182*7c478bd9Sstevel@tonic-gate 			s_output(allc, allv, cmd.infile, "-DRPC_SVC", EXTEND,
183*7c478bd9Sstevel@tonic-gate 			"_svc.c", cmd.mflag, cmd.nflag);
184*7c478bd9Sstevel@tonic-gate 		else
185*7c478bd9Sstevel@tonic-gate 			s_output(allnc, allnv, cmd.infile, "-DRPC_SVC",
186*7c478bd9Sstevel@tonic-gate 				EXTEND, "_svc.c", cmd.mflag, cmd.nflag);
187*7c478bd9Sstevel@tonic-gate 		if (tblflag) {
188*7c478bd9Sstevel@tonic-gate 			reinitialize();
189*7c478bd9Sstevel@tonic-gate 		t_output(cmd.infile, "-DRPC_TBL", EXTEND, "_tbl.i");
190*7c478bd9Sstevel@tonic-gate 		}
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 		if (allfiles) {
193*7c478bd9Sstevel@tonic-gate 			reinitialize();
194*7c478bd9Sstevel@tonic-gate 			svc_output(cmd.infile, "-DRPC_SERVER", EXTEND,
195*7c478bd9Sstevel@tonic-gate 				"_server.c");
196*7c478bd9Sstevel@tonic-gate 			reinitialize();
197*7c478bd9Sstevel@tonic-gate 			clnt_output(cmd.infile, "-DRPC_CLIENT", EXTEND,
198*7c478bd9Sstevel@tonic-gate 				"_client.c");
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate 		}
201*7c478bd9Sstevel@tonic-gate 		if (allfiles || (cmd.makefileflag == 1)) {
202*7c478bd9Sstevel@tonic-gate 			reinitialize();
203*7c478bd9Sstevel@tonic-gate 			mkfile_output(&cmd);
204*7c478bd9Sstevel@tonic-gate 		}
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	}
207*7c478bd9Sstevel@tonic-gate 	exit(nonfatalerrors);
208*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
209*7c478bd9Sstevel@tonic-gate }
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate /*
213*7c478bd9Sstevel@tonic-gate  * add extension to filename
214*7c478bd9Sstevel@tonic-gate  */
215*7c478bd9Sstevel@tonic-gate static char *
216*7c478bd9Sstevel@tonic-gate extendfile(file, ext)
217*7c478bd9Sstevel@tonic-gate 	char *file;
218*7c478bd9Sstevel@tonic-gate 	char *ext;
219*7c478bd9Sstevel@tonic-gate {
220*7c478bd9Sstevel@tonic-gate 	char *res;
221*7c478bd9Sstevel@tonic-gate 	char *p;
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate 	res = alloc(strlen(file) + strlen(ext) + 1);
224*7c478bd9Sstevel@tonic-gate 	if (res == NULL) {
225*7c478bd9Sstevel@tonic-gate 		abort();
226*7c478bd9Sstevel@tonic-gate 	}
227*7c478bd9Sstevel@tonic-gate 	p = strrchr(file, '.');
228*7c478bd9Sstevel@tonic-gate 	if (p == NULL) {
229*7c478bd9Sstevel@tonic-gate 		p = file + strlen(file);
230*7c478bd9Sstevel@tonic-gate 	}
231*7c478bd9Sstevel@tonic-gate 	(void) strcpy(res, file);
232*7c478bd9Sstevel@tonic-gate 	(void) strcpy(res + (p - file), ext);
233*7c478bd9Sstevel@tonic-gate 	return (res);
234*7c478bd9Sstevel@tonic-gate }
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate /*
237*7c478bd9Sstevel@tonic-gate  * Open output file with given extension
238*7c478bd9Sstevel@tonic-gate  */
239*7c478bd9Sstevel@tonic-gate static
240*7c478bd9Sstevel@tonic-gate open_output(infile, outfile)
241*7c478bd9Sstevel@tonic-gate 	char *infile;
242*7c478bd9Sstevel@tonic-gate 	char *outfile;
243*7c478bd9Sstevel@tonic-gate {
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate 	if (outfile == NULL) {
246*7c478bd9Sstevel@tonic-gate 		fout = stdout;
247*7c478bd9Sstevel@tonic-gate 		return;
248*7c478bd9Sstevel@tonic-gate 	}
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate 	if (infile != NULL && streq(outfile, infile)) {
251*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "%s: %s already exists.  No output generated.\n",
252*7c478bd9Sstevel@tonic-gate 		cmdname, infile);
253*7c478bd9Sstevel@tonic-gate 		crash();
254*7c478bd9Sstevel@tonic-gate 	}
255*7c478bd9Sstevel@tonic-gate 	fout = fopen(outfile, "w");
256*7c478bd9Sstevel@tonic-gate 	if (fout == NULL) {
257*7c478bd9Sstevel@tonic-gate 		f_print(stderr, "%s: unable to open ", cmdname);
258*7c478bd9Sstevel@tonic-gate 		perror(outfile);
259*7c478bd9Sstevel@tonic-gate 		crash();
260*7c478bd9Sstevel@tonic-gate 	}
261*7c478bd9Sstevel@tonic-gate 	record_open(outfile);
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate }
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate static
266*7c478bd9Sstevel@tonic-gate add_warning()
267*7c478bd9Sstevel@tonic-gate {
268*7c478bd9Sstevel@tonic-gate 	f_print(fout, "/*\n");
269*7c478bd9Sstevel@tonic-gate 	f_print(fout, " * Please do not edit this file.\n");
270*7c478bd9Sstevel@tonic-gate 	f_print(fout, " * It was generated using rpcgen.\n");
271*7c478bd9Sstevel@tonic-gate 	f_print(fout, " */\n\n");
272*7c478bd9Sstevel@tonic-gate }
273*7c478bd9Sstevel@tonic-gate 
274*7c478bd9Sstevel@tonic-gate /* clear list of arguments */
275*7c478bd9Sstevel@tonic-gate static void clear_args()
276*7c478bd9Sstevel@tonic-gate {
277*7c478bd9Sstevel@tonic-gate 	int i;
278*7c478bd9Sstevel@tonic-gate 	for (i = FIXEDARGS; i < ARGLISTLEN; i++)
279*7c478bd9Sstevel@tonic-gate 		arglist[i] = NULL;
280*7c478bd9Sstevel@tonic-gate 	argcount = FIXEDARGS;
281*7c478bd9Sstevel@tonic-gate }
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate /* make sure that a CPP exists */
284*7c478bd9Sstevel@tonic-gate static void find_cpp()
285*7c478bd9Sstevel@tonic-gate {
286*7c478bd9Sstevel@tonic-gate 	struct stat buf;
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate 	if (stat(CPP, &buf) < 0)  { /* SVR4 or explicit cpp does not exist */
289*7c478bd9Sstevel@tonic-gate 		if (cppDefined) {
290*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,
291*7c478bd9Sstevel@tonic-gate 				"cannot find C preprocessor: %s \n", CPP);
292*7c478bd9Sstevel@tonic-gate 			crash();
293*7c478bd9Sstevel@tonic-gate 		} else {	/* try the other one */
294*7c478bd9Sstevel@tonic-gate 			CPP = SUNOS_CPP;
295*7c478bd9Sstevel@tonic-gate 			if (stat(CPP, &buf) < 0) { /* can't find any cpp */
296*7c478bd9Sstevel@tonic-gate 				fprintf(stderr,
297*7c478bd9Sstevel@tonic-gate 		"cannot find any C preprocessor (cpp)\n");
298*7c478bd9Sstevel@tonic-gate 				crash();
299*7c478bd9Sstevel@tonic-gate 			}
300*7c478bd9Sstevel@tonic-gate 		}
301*7c478bd9Sstevel@tonic-gate 	}
302*7c478bd9Sstevel@tonic-gate }
303*7c478bd9Sstevel@tonic-gate 
304*7c478bd9Sstevel@tonic-gate /*
305*7c478bd9Sstevel@tonic-gate  * Open input file with given define for C-preprocessor
306*7c478bd9Sstevel@tonic-gate  */
307*7c478bd9Sstevel@tonic-gate static
308*7c478bd9Sstevel@tonic-gate open_input(infile, define)
309*7c478bd9Sstevel@tonic-gate 	char *infile;
310*7c478bd9Sstevel@tonic-gate 	char *define;
311*7c478bd9Sstevel@tonic-gate {
312*7c478bd9Sstevel@tonic-gate 	int pd[2];
313*7c478bd9Sstevel@tonic-gate 
314*7c478bd9Sstevel@tonic-gate 	infilename = (infile == NULL) ? "<stdin>" : infile;
315*7c478bd9Sstevel@tonic-gate 	(void) pipe(pd);
316*7c478bd9Sstevel@tonic-gate 	switch (childpid = fork()) {
317*7c478bd9Sstevel@tonic-gate 	case 0:
318*7c478bd9Sstevel@tonic-gate 		find_cpp();
319*7c478bd9Sstevel@tonic-gate 		putarg(0, CPP);
320*7c478bd9Sstevel@tonic-gate 		putarg(1, CPPFLAGS);
321*7c478bd9Sstevel@tonic-gate 		addarg(define);
322*7c478bd9Sstevel@tonic-gate 		if (infile)
323*7c478bd9Sstevel@tonic-gate 			addarg(infile);
324*7c478bd9Sstevel@tonic-gate 		addarg((char *)NULL);
325*7c478bd9Sstevel@tonic-gate 		(void) close(1);
326*7c478bd9Sstevel@tonic-gate 		(void) dup2(pd[1], 1);
327*7c478bd9Sstevel@tonic-gate 		(void) close(pd[0]);
328*7c478bd9Sstevel@tonic-gate 		execv(arglist[0], arglist);
329*7c478bd9Sstevel@tonic-gate 		perror("execv");
330*7c478bd9Sstevel@tonic-gate 		exit(1);
331*7c478bd9Sstevel@tonic-gate 	case -1:
332*7c478bd9Sstevel@tonic-gate 		perror("fork");
333*7c478bd9Sstevel@tonic-gate 		exit(1);
334*7c478bd9Sstevel@tonic-gate 	}
335*7c478bd9Sstevel@tonic-gate 	(void) close(pd[1]);
336*7c478bd9Sstevel@tonic-gate 	fin = fdopen(pd[0], "r");
337*7c478bd9Sstevel@tonic-gate 	if (fin == NULL) {
338*7c478bd9Sstevel@tonic-gate 		f_print(stderr, "%s: ", cmdname);
339*7c478bd9Sstevel@tonic-gate 		perror(infilename);
340*7c478bd9Sstevel@tonic-gate 		crash();
341*7c478bd9Sstevel@tonic-gate 	}
342*7c478bd9Sstevel@tonic-gate }
343*7c478bd9Sstevel@tonic-gate 
344*7c478bd9Sstevel@tonic-gate /* valid tirpc nettypes */
345*7c478bd9Sstevel@tonic-gate static char *valid_ti_nettypes[] =
346*7c478bd9Sstevel@tonic-gate {
347*7c478bd9Sstevel@tonic-gate 	"netpath",
348*7c478bd9Sstevel@tonic-gate 	"visible",
349*7c478bd9Sstevel@tonic-gate 	"circuit_v",
350*7c478bd9Sstevel@tonic-gate 	"datagram_v",
351*7c478bd9Sstevel@tonic-gate 	"circuit_n",
352*7c478bd9Sstevel@tonic-gate 	"datagram_n",
353*7c478bd9Sstevel@tonic-gate 	"udp",
354*7c478bd9Sstevel@tonic-gate 	"tcp",
355*7c478bd9Sstevel@tonic-gate 	"raw",
356*7c478bd9Sstevel@tonic-gate 	NULL
357*7c478bd9Sstevel@tonic-gate 	};
358*7c478bd9Sstevel@tonic-gate 
359*7c478bd9Sstevel@tonic-gate /* valid inetd nettypes */
360*7c478bd9Sstevel@tonic-gate static char *valid_i_nettypes[] =
361*7c478bd9Sstevel@tonic-gate {
362*7c478bd9Sstevel@tonic-gate 	"udp",
363*7c478bd9Sstevel@tonic-gate 	"tcp",
364*7c478bd9Sstevel@tonic-gate 	NULL
365*7c478bd9Sstevel@tonic-gate 	};
366*7c478bd9Sstevel@tonic-gate 
367*7c478bd9Sstevel@tonic-gate static int check_nettype(name, list_to_check)
368*7c478bd9Sstevel@tonic-gate char *name;
369*7c478bd9Sstevel@tonic-gate char *list_to_check[];
370*7c478bd9Sstevel@tonic-gate {
371*7c478bd9Sstevel@tonic-gate 	int i;
372*7c478bd9Sstevel@tonic-gate 	for (i = 0; list_to_check[i] != NULL; i++) {
373*7c478bd9Sstevel@tonic-gate 		if (strcmp(name, list_to_check[i]) == 0) {
374*7c478bd9Sstevel@tonic-gate 			return (1);
375*7c478bd9Sstevel@tonic-gate 		}
376*7c478bd9Sstevel@tonic-gate 	}
377*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "illegal nettype :\'%s\'\n", name);
378*7c478bd9Sstevel@tonic-gate 	return (0);
379*7c478bd9Sstevel@tonic-gate }
380*7c478bd9Sstevel@tonic-gate 
381*7c478bd9Sstevel@tonic-gate static char *
382*7c478bd9Sstevel@tonic-gate file_name(file, ext)
383*7c478bd9Sstevel@tonic-gate char *file;
384*7c478bd9Sstevel@tonic-gate char *ext;
385*7c478bd9Sstevel@tonic-gate {
386*7c478bd9Sstevel@tonic-gate 	char *temp;
387*7c478bd9Sstevel@tonic-gate 	temp = extendfile(file, ext);
388*7c478bd9Sstevel@tonic-gate 
389*7c478bd9Sstevel@tonic-gate 	if (access(temp, F_OK) != -1)
390*7c478bd9Sstevel@tonic-gate 		return (temp);
391*7c478bd9Sstevel@tonic-gate 	else
392*7c478bd9Sstevel@tonic-gate 		return ((char *)" ");
393*7c478bd9Sstevel@tonic-gate 
394*7c478bd9Sstevel@tonic-gate }
395*7c478bd9Sstevel@tonic-gate 
396*7c478bd9Sstevel@tonic-gate 
397*7c478bd9Sstevel@tonic-gate static
398*7c478bd9Sstevel@tonic-gate c_output(infile, define, extend, outfile)
399*7c478bd9Sstevel@tonic-gate 	char *infile;
400*7c478bd9Sstevel@tonic-gate 	char *define;
401*7c478bd9Sstevel@tonic-gate 	int extend;
402*7c478bd9Sstevel@tonic-gate 	char *outfile;
403*7c478bd9Sstevel@tonic-gate {
404*7c478bd9Sstevel@tonic-gate 	definition *def;
405*7c478bd9Sstevel@tonic-gate 	char *include;
406*7c478bd9Sstevel@tonic-gate 	char *outfilename;
407*7c478bd9Sstevel@tonic-gate 	long tell;
408*7c478bd9Sstevel@tonic-gate 
409*7c478bd9Sstevel@tonic-gate 	c_initialize();
410*7c478bd9Sstevel@tonic-gate 	open_input(infile, define);
411*7c478bd9Sstevel@tonic-gate 	outfilename = extend ? extendfile(infile, outfile) : outfile;
412*7c478bd9Sstevel@tonic-gate 	open_output(infile, outfilename);
413*7c478bd9Sstevel@tonic-gate 	add_warning();
414*7c478bd9Sstevel@tonic-gate 	if (infile && (include = extendfile(infile, ".h"))) {
415*7c478bd9Sstevel@tonic-gate 		f_print(fout, "#include \"%s\"\n", include);
416*7c478bd9Sstevel@tonic-gate 		free(include);
417*7c478bd9Sstevel@tonic-gate 		/* .h file already contains rpc/rpc.h */
418*7c478bd9Sstevel@tonic-gate 	} else
419*7c478bd9Sstevel@tonic-gate 		f_print(fout, "#include <rpc/rpc.h>\n");
420*7c478bd9Sstevel@tonic-gate 	/*
421*7c478bd9Sstevel@tonic-gate 	 * Include malloc.h to support mem_alloc calls.
422*7c478bd9Sstevel@tonic-gate 	 */
423*7c478bd9Sstevel@tonic-gate 	f_print(fout, "\n#ifndef _KERNEL\n");
424*7c478bd9Sstevel@tonic-gate 	f_print(fout, "#include <malloc.h>\n");
425*7c478bd9Sstevel@tonic-gate 	f_print(fout, "#endif /* !_KERNEL */\n\n");
426*7c478bd9Sstevel@tonic-gate 	tell = ftell(fout);
427*7c478bd9Sstevel@tonic-gate 	while (def = get_definition()) {
428*7c478bd9Sstevel@tonic-gate 		emit(def);
429*7c478bd9Sstevel@tonic-gate 	}
430*7c478bd9Sstevel@tonic-gate 	if (extend && tell == ftell(fout)) {
431*7c478bd9Sstevel@tonic-gate 		(void) unlink(outfilename);
432*7c478bd9Sstevel@tonic-gate 	}
433*7c478bd9Sstevel@tonic-gate }
434*7c478bd9Sstevel@tonic-gate 
435*7c478bd9Sstevel@tonic-gate 
436*7c478bd9Sstevel@tonic-gate c_initialize()
437*7c478bd9Sstevel@tonic-gate {
438*7c478bd9Sstevel@tonic-gate 
439*7c478bd9Sstevel@tonic-gate 	/*
440*7c478bd9Sstevel@tonic-gate 	 * add all the starting basic types.
441*7c478bd9Sstevel@tonic-gate 	 * We may need to add some derived types
442*7c478bd9Sstevel@tonic-gate 	 * if we need to generate INLINE macros.
443*7c478bd9Sstevel@tonic-gate 	 * These types are defined in rpc/types.h
444*7c478bd9Sstevel@tonic-gate 	 */
445*7c478bd9Sstevel@tonic-gate 	add_type(1, "int");
446*7c478bd9Sstevel@tonic-gate 	add_type(1, "long");
447*7c478bd9Sstevel@tonic-gate 	add_type(1, "short");
448*7c478bd9Sstevel@tonic-gate 	add_type(1, "bool");
449*7c478bd9Sstevel@tonic-gate 	add_type(1, "u_int");
450*7c478bd9Sstevel@tonic-gate 	add_type(1, "u_long");
451*7c478bd9Sstevel@tonic-gate 	add_type(1, "u_short");
452*7c478bd9Sstevel@tonic-gate 	add_type(1, "rpcprog_t");
453*7c478bd9Sstevel@tonic-gate 	add_type(1, "rpcvers_t");
454*7c478bd9Sstevel@tonic-gate 	add_type(1, "rpcproc_t");
455*7c478bd9Sstevel@tonic-gate 	add_type(1, "rpcprot_t");
456*7c478bd9Sstevel@tonic-gate 	add_type(1, "rpcport_t");
457*7c478bd9Sstevel@tonic-gate 
458*7c478bd9Sstevel@tonic-gate }
459*7c478bd9Sstevel@tonic-gate 
460*7c478bd9Sstevel@tonic-gate char rpcgen_table_dcl1[] = "struct rpcgen_table {\n";
461*7c478bd9Sstevel@tonic-gate 
462*7c478bd9Sstevel@tonic-gate char rpcgen_table_dcl2[] = "\txdrproc_t\txdr_arg;\n"
463*7c478bd9Sstevel@tonic-gate 				"\tunsigned\tlen_arg;\n"
464*7c478bd9Sstevel@tonic-gate 				"\txdrproc_t\txdr_res;\n"
465*7c478bd9Sstevel@tonic-gate 				"\tunsigned\tlen_res;\n"
466*7c478bd9Sstevel@tonic-gate 				"};\n";
467*7c478bd9Sstevel@tonic-gate 
468*7c478bd9Sstevel@tonic-gate char rpcgen_table_proc[] = "\tvoid\t*(*proc)();\n";
469*7c478bd9Sstevel@tonic-gate 
470*7c478bd9Sstevel@tonic-gate char rpcgen_table_proc_b[] = "\tchar\t*(*proc)();\n";
471*7c478bd9Sstevel@tonic-gate 
472*7c478bd9Sstevel@tonic-gate 
473*7c478bd9Sstevel@tonic-gate char *
474*7c478bd9Sstevel@tonic-gate generate_guard(pathname)
475*7c478bd9Sstevel@tonic-gate 	char *pathname;
476*7c478bd9Sstevel@tonic-gate {
477*7c478bd9Sstevel@tonic-gate 	char *filename, *guard, *tmp;
478*7c478bd9Sstevel@tonic-gate 
479*7c478bd9Sstevel@tonic-gate 	filename = strrchr(pathname, '/');  /* find last component */
480*7c478bd9Sstevel@tonic-gate 	filename = ((filename == 0) ? pathname : filename+1);
481*7c478bd9Sstevel@tonic-gate 	guard = extendfile(filename, "_H_RPCGEN");
482*7c478bd9Sstevel@tonic-gate 
483*7c478bd9Sstevel@tonic-gate 	/*
484*7c478bd9Sstevel@tonic-gate 	 * Guard must be an ANSI C identifier composed of
485*7c478bd9Sstevel@tonic-gate 	 * upper case letters, digits, or '_'.
486*7c478bd9Sstevel@tonic-gate 	 * Convert invalid characters to '_'.
487*7c478bd9Sstevel@tonic-gate 	 */
488*7c478bd9Sstevel@tonic-gate 	for (tmp = guard; *tmp; tmp++) {
489*7c478bd9Sstevel@tonic-gate 		if (!isalpha(*tmp) && !isdigit(*tmp)) {
490*7c478bd9Sstevel@tonic-gate 			*tmp = '_';
491*7c478bd9Sstevel@tonic-gate 			continue;
492*7c478bd9Sstevel@tonic-gate 		}
493*7c478bd9Sstevel@tonic-gate 		if (islower(*tmp))
494*7c478bd9Sstevel@tonic-gate 			*tmp = toupper(*tmp);
495*7c478bd9Sstevel@tonic-gate 	}
496*7c478bd9Sstevel@tonic-gate 
497*7c478bd9Sstevel@tonic-gate 	/*
498*7c478bd9Sstevel@tonic-gate 	 * The first character must be a letter; the underscore '_'
499*7c478bd9Sstevel@tonic-gate 	 * counts as a letter.
500*7c478bd9Sstevel@tonic-gate 	 */
501*7c478bd9Sstevel@tonic-gate 	if (!isalpha(guard[0]))
502*7c478bd9Sstevel@tonic-gate 		guard[0] = '_';
503*7c478bd9Sstevel@tonic-gate 
504*7c478bd9Sstevel@tonic-gate 	return (guard);
505*7c478bd9Sstevel@tonic-gate }
506*7c478bd9Sstevel@tonic-gate 
507*7c478bd9Sstevel@tonic-gate /*
508*7c478bd9Sstevel@tonic-gate  * Compile into an XDR header file
509*7c478bd9Sstevel@tonic-gate  */
510*7c478bd9Sstevel@tonic-gate 
511*7c478bd9Sstevel@tonic-gate 
512*7c478bd9Sstevel@tonic-gate static
513*7c478bd9Sstevel@tonic-gate h_output(infile, define, extend, outfile)
514*7c478bd9Sstevel@tonic-gate 	char *infile;
515*7c478bd9Sstevel@tonic-gate 	char *define;
516*7c478bd9Sstevel@tonic-gate 	int extend;
517*7c478bd9Sstevel@tonic-gate 	char *outfile;
518*7c478bd9Sstevel@tonic-gate {
519*7c478bd9Sstevel@tonic-gate 	definition *def;
520*7c478bd9Sstevel@tonic-gate 	char *outfilename;
521*7c478bd9Sstevel@tonic-gate 	long tell;
522*7c478bd9Sstevel@tonic-gate 	char *guard;
523*7c478bd9Sstevel@tonic-gate 	list *l;
524*7c478bd9Sstevel@tonic-gate 	xdrfunc *xdrfuncp;
525*7c478bd9Sstevel@tonic-gate 	int i;
526*7c478bd9Sstevel@tonic-gate 
527*7c478bd9Sstevel@tonic-gate 	open_input(infile, define);
528*7c478bd9Sstevel@tonic-gate 	outfilename =  extend ? extendfile(infile, outfile) : outfile;
529*7c478bd9Sstevel@tonic-gate 	open_output(infile, outfilename);
530*7c478bd9Sstevel@tonic-gate 	add_warning();
531*7c478bd9Sstevel@tonic-gate 	if (outfilename || infile) {
532*7c478bd9Sstevel@tonic-gate 		guard = generate_guard(outfilename ? outfilename: infile);
533*7c478bd9Sstevel@tonic-gate 	} else
534*7c478bd9Sstevel@tonic-gate 		guard = "STDIN_";
535*7c478bd9Sstevel@tonic-gate 
536*7c478bd9Sstevel@tonic-gate 	f_print(fout, "#ifndef _%s\n#define	_%s\n\n", guard,
537*7c478bd9Sstevel@tonic-gate 		guard);
538*7c478bd9Sstevel@tonic-gate 
539*7c478bd9Sstevel@tonic-gate 	f_print(fout, "#include <rpc/rpc.h>\n");
540*7c478bd9Sstevel@tonic-gate 
541*7c478bd9Sstevel@tonic-gate 	if (mtflag) {
542*7c478bd9Sstevel@tonic-gate 		f_print(fout, "#ifndef _KERNEL\n");
543*7c478bd9Sstevel@tonic-gate 		f_print(fout, "#include <synch.h>\n");
544*7c478bd9Sstevel@tonic-gate 		f_print(fout, "#include <thread.h>\n");
545*7c478bd9Sstevel@tonic-gate 		f_print(fout, "#endif /* !_KERNEL */\n");
546*7c478bd9Sstevel@tonic-gate 	};
547*7c478bd9Sstevel@tonic-gate 
548*7c478bd9Sstevel@tonic-gate 	/* put the C++ support */
549*7c478bd9Sstevel@tonic-gate 	if (Cflag && !CCflag) {
550*7c478bd9Sstevel@tonic-gate 		f_print(fout, "\n#ifdef __cplusplus\n");
551*7c478bd9Sstevel@tonic-gate 		f_print(fout, "extern \"C\" {\n");
552*7c478bd9Sstevel@tonic-gate 		f_print(fout, "#endif\n\n");
553*7c478bd9Sstevel@tonic-gate 	}
554*7c478bd9Sstevel@tonic-gate 
555*7c478bd9Sstevel@tonic-gate 	/* put in a typedef for quadprecision. Only with Cflag */
556*7c478bd9Sstevel@tonic-gate 
557*7c478bd9Sstevel@tonic-gate 	tell = ftell(fout);
558*7c478bd9Sstevel@tonic-gate 
559*7c478bd9Sstevel@tonic-gate 	/* print data definitions */
560*7c478bd9Sstevel@tonic-gate 	while (def = get_definition()) {
561*7c478bd9Sstevel@tonic-gate 		print_datadef(def);
562*7c478bd9Sstevel@tonic-gate 	}
563*7c478bd9Sstevel@tonic-gate 
564*7c478bd9Sstevel@tonic-gate 	/*
565*7c478bd9Sstevel@tonic-gate 	 * print function declarations.
566*7c478bd9Sstevel@tonic-gate 	 *  Do this after data definitions because they might be used as
567*7c478bd9Sstevel@tonic-gate 	 *  arguments for functions
568*7c478bd9Sstevel@tonic-gate 	 */
569*7c478bd9Sstevel@tonic-gate 	for (l = defined; l != NULL; l = l->next) {
570*7c478bd9Sstevel@tonic-gate 		print_funcdef(l->val);
571*7c478bd9Sstevel@tonic-gate 	}
572*7c478bd9Sstevel@tonic-gate 	/* Now  print all xdr func declarations */
573*7c478bd9Sstevel@tonic-gate 	if (xdrfunc_head != NULL) {
574*7c478bd9Sstevel@tonic-gate 
575*7c478bd9Sstevel@tonic-gate 		f_print(fout,
576*7c478bd9Sstevel@tonic-gate 			"\n/* the xdr functions */\n");
577*7c478bd9Sstevel@tonic-gate 
578*7c478bd9Sstevel@tonic-gate 		if (CCflag) {
579*7c478bd9Sstevel@tonic-gate 		f_print(fout, "\n#ifdef __cplusplus\n");
580*7c478bd9Sstevel@tonic-gate 		f_print(fout, "extern \"C\" {\n");
581*7c478bd9Sstevel@tonic-gate 		f_print(fout, "#endif\n");
582*7c478bd9Sstevel@tonic-gate 	}
583*7c478bd9Sstevel@tonic-gate 
584*7c478bd9Sstevel@tonic-gate 		if (!Cflag) {
585*7c478bd9Sstevel@tonic-gate 			xdrfuncp = xdrfunc_head;
586*7c478bd9Sstevel@tonic-gate 			while (xdrfuncp != NULL) {
587*7c478bd9Sstevel@tonic-gate 				print_xdr_func_def(xdrfuncp->name,
588*7c478bd9Sstevel@tonic-gate 				xdrfuncp->pointerp, 2);
589*7c478bd9Sstevel@tonic-gate 				xdrfuncp = xdrfuncp->next;
590*7c478bd9Sstevel@tonic-gate 			}
591*7c478bd9Sstevel@tonic-gate 		} else {
592*7c478bd9Sstevel@tonic-gate 
593*7c478bd9Sstevel@tonic-gate 			for (i = 1; i < 3; i++) {
594*7c478bd9Sstevel@tonic-gate 				if (i == 1)
595*7c478bd9Sstevel@tonic-gate 	f_print(fout, "\n#if defined(__STDC__) || defined(__cplusplus)\n");
596*7c478bd9Sstevel@tonic-gate 
597*7c478bd9Sstevel@tonic-gate 				else
598*7c478bd9Sstevel@tonic-gate 					f_print(fout, "\n#else /* K&R C */\n");
599*7c478bd9Sstevel@tonic-gate 
600*7c478bd9Sstevel@tonic-gate 				xdrfuncp = xdrfunc_head;
601*7c478bd9Sstevel@tonic-gate 				while (xdrfuncp != NULL) {
602*7c478bd9Sstevel@tonic-gate 					print_xdr_func_def(xdrfuncp->name,
603*7c478bd9Sstevel@tonic-gate 	xdrfuncp->pointerp, i);
604*7c478bd9Sstevel@tonic-gate 					xdrfuncp = xdrfuncp->next;
605*7c478bd9Sstevel@tonic-gate 				}
606*7c478bd9Sstevel@tonic-gate 			}
607*7c478bd9Sstevel@tonic-gate 		f_print(fout, "\n#endif /* K&R C */\n");
608*7c478bd9Sstevel@tonic-gate 		}
609*7c478bd9Sstevel@tonic-gate 	}
610*7c478bd9Sstevel@tonic-gate 
611*7c478bd9Sstevel@tonic-gate 	if (extend && tell == ftell(fout)) {
612*7c478bd9Sstevel@tonic-gate 		(void) unlink(outfilename);
613*7c478bd9Sstevel@tonic-gate 	} else if (tblflag) {
614*7c478bd9Sstevel@tonic-gate 		f_print(fout, rpcgen_table_dcl1);
615*7c478bd9Sstevel@tonic-gate 		if (tirpcflag)
616*7c478bd9Sstevel@tonic-gate 			f_print(fout, rpcgen_table_proc);
617*7c478bd9Sstevel@tonic-gate 		else
618*7c478bd9Sstevel@tonic-gate 			f_print(fout, rpcgen_table_proc_b);
619*7c478bd9Sstevel@tonic-gate 		f_print(fout, rpcgen_table_dcl2);
620*7c478bd9Sstevel@tonic-gate 	}
621*7c478bd9Sstevel@tonic-gate 
622*7c478bd9Sstevel@tonic-gate 	if (Cflag) {
623*7c478bd9Sstevel@tonic-gate 		f_print(fout, "\n#ifdef __cplusplus\n");
624*7c478bd9Sstevel@tonic-gate 		f_print(fout, "}\n");
625*7c478bd9Sstevel@tonic-gate 		f_print(fout, "#endif\n");
626*7c478bd9Sstevel@tonic-gate 	}
627*7c478bd9Sstevel@tonic-gate 
628*7c478bd9Sstevel@tonic-gate 	f_print(fout, "\n#endif /* !_%s */\n", guard);
629*7c478bd9Sstevel@tonic-gate }
630*7c478bd9Sstevel@tonic-gate 
631*7c478bd9Sstevel@tonic-gate /*
632*7c478bd9Sstevel@tonic-gate  * Compile into an RPC service
633*7c478bd9Sstevel@tonic-gate  */
634*7c478bd9Sstevel@tonic-gate static
635*7c478bd9Sstevel@tonic-gate s_output(argc, argv, infile, define, extend, outfile, nomain, netflag)
636*7c478bd9Sstevel@tonic-gate 	int argc;
637*7c478bd9Sstevel@tonic-gate 	char *argv[];
638*7c478bd9Sstevel@tonic-gate 	char *infile;
639*7c478bd9Sstevel@tonic-gate 	char *define;
640*7c478bd9Sstevel@tonic-gate 	int extend;
641*7c478bd9Sstevel@tonic-gate 	char *outfile;
642*7c478bd9Sstevel@tonic-gate 	int nomain;
643*7c478bd9Sstevel@tonic-gate 	int netflag;
644*7c478bd9Sstevel@tonic-gate {
645*7c478bd9Sstevel@tonic-gate 	char *include;
646*7c478bd9Sstevel@tonic-gate 	definition *def;
647*7c478bd9Sstevel@tonic-gate 	int foundprogram = 0;
648*7c478bd9Sstevel@tonic-gate 	char *outfilename;
649*7c478bd9Sstevel@tonic-gate 
650*7c478bd9Sstevel@tonic-gate 	open_input(infile, define);
651*7c478bd9Sstevel@tonic-gate 	outfilename = extend ? extendfile(infile, outfile) : outfile;
652*7c478bd9Sstevel@tonic-gate 	open_output(infile, outfilename);
653*7c478bd9Sstevel@tonic-gate 	add_warning();
654*7c478bd9Sstevel@tonic-gate 	if (infile && (include = extendfile(infile, ".h"))) {
655*7c478bd9Sstevel@tonic-gate 		f_print(fout, "#include \"%s\"\n", include);
656*7c478bd9Sstevel@tonic-gate 		free(include);
657*7c478bd9Sstevel@tonic-gate 	} else
658*7c478bd9Sstevel@tonic-gate 		f_print(fout, "#include <rpc/rpc.h>\n");
659*7c478bd9Sstevel@tonic-gate 
660*7c478bd9Sstevel@tonic-gate 	f_print(fout, "#include <stdio.h>\n");
661*7c478bd9Sstevel@tonic-gate 	f_print(fout, "#include <stdlib.h> /* getenv, exit */\n");
662*7c478bd9Sstevel@tonic-gate 	f_print(fout, "#include <signal.h>\n");
663*7c478bd9Sstevel@tonic-gate 
664*7c478bd9Sstevel@tonic-gate 	if (Cflag) {
665*7c478bd9Sstevel@tonic-gate 		f_print(fout,
666*7c478bd9Sstevel@tonic-gate 		"#include <rpc/pmap_clnt.h> /* for pmap_unset */\n");
667*7c478bd9Sstevel@tonic-gate 		f_print(fout, "#include <string.h> /* strcmp */\n");
668*7c478bd9Sstevel@tonic-gate 	}
669*7c478bd9Sstevel@tonic-gate 	if (strcmp(svcclosetime, "-1") == 0)
670*7c478bd9Sstevel@tonic-gate 		indefinitewait = 1;
671*7c478bd9Sstevel@tonic-gate 	else if (strcmp(svcclosetime, "0") == 0)
672*7c478bd9Sstevel@tonic-gate 		exitnow = 1;
673*7c478bd9Sstevel@tonic-gate 	else if (inetdflag || pmflag) {
674*7c478bd9Sstevel@tonic-gate 		timerflag = 1;
675*7c478bd9Sstevel@tonic-gate 	}
676*7c478bd9Sstevel@tonic-gate 
677*7c478bd9Sstevel@tonic-gate 	if (!tirpcflag && inetdflag)
678*7c478bd9Sstevel@tonic-gate 		f_print(fout, "#include <sys/termios.h> /* TIOCNOTTY */\n");
679*7c478bd9Sstevel@tonic-gate 	if (Cflag && (inetdflag || pmflag)) {
680*7c478bd9Sstevel@tonic-gate 		if (tirpcflag)
681*7c478bd9Sstevel@tonic-gate 			f_print(fout, "#include <unistd.h> /* setsid */\n");
682*7c478bd9Sstevel@tonic-gate 	}
683*7c478bd9Sstevel@tonic-gate 	if (tirpcflag)
684*7c478bd9Sstevel@tonic-gate 		f_print(fout, "#include <sys/types.h>\n");
685*7c478bd9Sstevel@tonic-gate 
686*7c478bd9Sstevel@tonic-gate 	f_print(fout, "#include <memory.h>\n");
687*7c478bd9Sstevel@tonic-gate 	f_print(fout, "#include <stropts.h>\n");
688*7c478bd9Sstevel@tonic-gate 	if (inetdflag || !tirpcflag) {
689*7c478bd9Sstevel@tonic-gate 		f_print(fout, "#include <sys/socket.h>\n");
690*7c478bd9Sstevel@tonic-gate 		f_print(fout, "#include <netinet/in.h>\n");
691*7c478bd9Sstevel@tonic-gate 		f_print(fout, "#include <rpc/svc_soc.h>\n");
692*7c478bd9Sstevel@tonic-gate 	}
693*7c478bd9Sstevel@tonic-gate 
694*7c478bd9Sstevel@tonic-gate 	if ((netflag || pmflag) && tirpcflag && !nomain) {
695*7c478bd9Sstevel@tonic-gate 		f_print(fout, "#include <netconfig.h>\n");
696*7c478bd9Sstevel@tonic-gate 	}
697*7c478bd9Sstevel@tonic-gate 	if (tirpcflag)
698*7c478bd9Sstevel@tonic-gate 		f_print(fout, "#include <sys/resource.h> /* rlimit */\n");
699*7c478bd9Sstevel@tonic-gate 	if (logflag || inetdflag || pmflag)
700*7c478bd9Sstevel@tonic-gate 		f_print(fout, "#include <syslog.h>\n");
701*7c478bd9Sstevel@tonic-gate 
702*7c478bd9Sstevel@tonic-gate 	/* for ANSI-C */
703*7c478bd9Sstevel@tonic-gate 	if (Cflag)
704*7c478bd9Sstevel@tonic-gate 		f_print(fout,
705*7c478bd9Sstevel@tonic-gate 			"\n#ifndef SIG_PF\n#define	SIG_PF void(*)\
706*7c478bd9Sstevel@tonic-gate (int)\n#endif\n");
707*7c478bd9Sstevel@tonic-gate 
708*7c478bd9Sstevel@tonic-gate 	f_print(fout, "\n#ifdef DEBUG\n#define	RPC_SVC_FG\n#endif\n");
709*7c478bd9Sstevel@tonic-gate 	if (timerflag)
710*7c478bd9Sstevel@tonic-gate 		f_print(fout, "\n#define	_RPCSVC_CLOSEDOWN %s\n",
711*7c478bd9Sstevel@tonic-gate 			svcclosetime);
712*7c478bd9Sstevel@tonic-gate 	while (def = get_definition()) {
713*7c478bd9Sstevel@tonic-gate 		foundprogram |= (def->def_kind == DEF_PROGRAM);
714*7c478bd9Sstevel@tonic-gate 	}
715*7c478bd9Sstevel@tonic-gate 	if (extend && !foundprogram) {
716*7c478bd9Sstevel@tonic-gate 		(void) unlink(outfilename);
717*7c478bd9Sstevel@tonic-gate 		return;
718*7c478bd9Sstevel@tonic-gate 	}
719*7c478bd9Sstevel@tonic-gate 	write_most(infile, netflag, nomain);
720*7c478bd9Sstevel@tonic-gate 	if (!nomain) {
721*7c478bd9Sstevel@tonic-gate 		if (!do_registers(argc, argv)) {
722*7c478bd9Sstevel@tonic-gate 			if (outfilename)
723*7c478bd9Sstevel@tonic-gate 				(void) unlink(outfilename);
724*7c478bd9Sstevel@tonic-gate 			usage();
725*7c478bd9Sstevel@tonic-gate 		}
726*7c478bd9Sstevel@tonic-gate 		write_rest();
727*7c478bd9Sstevel@tonic-gate 	}
728*7c478bd9Sstevel@tonic-gate }
729*7c478bd9Sstevel@tonic-gate 
730*7c478bd9Sstevel@tonic-gate /*
731*7c478bd9Sstevel@tonic-gate  * generate client side stubs
732*7c478bd9Sstevel@tonic-gate  */
733*7c478bd9Sstevel@tonic-gate static
734*7c478bd9Sstevel@tonic-gate l_output(infile, define, extend, outfile)
735*7c478bd9Sstevel@tonic-gate 	char *infile;
736*7c478bd9Sstevel@tonic-gate 	char *define;
737*7c478bd9Sstevel@tonic-gate 	int extend;
738*7c478bd9Sstevel@tonic-gate 	char *outfile;
739*7c478bd9Sstevel@tonic-gate {
740*7c478bd9Sstevel@tonic-gate 	char *include;
741*7c478bd9Sstevel@tonic-gate 	definition *def;
742*7c478bd9Sstevel@tonic-gate 	int foundprogram = 0;
743*7c478bd9Sstevel@tonic-gate 	char *outfilename;
744*7c478bd9Sstevel@tonic-gate 
745*7c478bd9Sstevel@tonic-gate 	open_input(infile, define);
746*7c478bd9Sstevel@tonic-gate 	outfilename = extend ? extendfile(infile, outfile) : outfile;
747*7c478bd9Sstevel@tonic-gate 	open_output(infile, outfilename);
748*7c478bd9Sstevel@tonic-gate 	add_warning();
749*7c478bd9Sstevel@tonic-gate 	if (Cflag)
750*7c478bd9Sstevel@tonic-gate 		f_print(fout, "#include <memory.h> /* for memset */\n");
751*7c478bd9Sstevel@tonic-gate 	if (infile && (include = extendfile(infile, ".h"))) {
752*7c478bd9Sstevel@tonic-gate 		f_print(fout, "#include \"%s\"\n", include);
753*7c478bd9Sstevel@tonic-gate 		free(include);
754*7c478bd9Sstevel@tonic-gate 	} else
755*7c478bd9Sstevel@tonic-gate 		f_print(fout, "#include <rpc/rpc.h>\n");
756*7c478bd9Sstevel@tonic-gate 
757*7c478bd9Sstevel@tonic-gate 	f_print(fout, "#ifndef _KERNEL\n");
758*7c478bd9Sstevel@tonic-gate 	f_print(fout, "#include <stdio.h>\n");
759*7c478bd9Sstevel@tonic-gate 	f_print(fout, "#include <stdlib.h> /* getenv, exit */\n");
760*7c478bd9Sstevel@tonic-gate 	f_print(fout, "#endif /* !_KERNEL */\n");
761*7c478bd9Sstevel@tonic-gate 
762*7c478bd9Sstevel@tonic-gate 	while (def = get_definition()) {
763*7c478bd9Sstevel@tonic-gate 		foundprogram |= (def->def_kind == DEF_PROGRAM);
764*7c478bd9Sstevel@tonic-gate 	}
765*7c478bd9Sstevel@tonic-gate 	if (extend && !foundprogram) {
766*7c478bd9Sstevel@tonic-gate 		(void) unlink(outfilename);
767*7c478bd9Sstevel@tonic-gate 		return;
768*7c478bd9Sstevel@tonic-gate 	}
769*7c478bd9Sstevel@tonic-gate 	write_stubs();
770*7c478bd9Sstevel@tonic-gate }
771*7c478bd9Sstevel@tonic-gate 
772*7c478bd9Sstevel@tonic-gate /*
773*7c478bd9Sstevel@tonic-gate  * generate the dispatch table
774*7c478bd9Sstevel@tonic-gate  */
775*7c478bd9Sstevel@tonic-gate static
776*7c478bd9Sstevel@tonic-gate t_output(infile, define, extend, outfile)
777*7c478bd9Sstevel@tonic-gate 	char *infile;
778*7c478bd9Sstevel@tonic-gate 	char *define;
779*7c478bd9Sstevel@tonic-gate 	int extend;
780*7c478bd9Sstevel@tonic-gate 	char *outfile;
781*7c478bd9Sstevel@tonic-gate {
782*7c478bd9Sstevel@tonic-gate 	definition *def;
783*7c478bd9Sstevel@tonic-gate 	int foundprogram = 0;
784*7c478bd9Sstevel@tonic-gate 	char *outfilename;
785*7c478bd9Sstevel@tonic-gate 
786*7c478bd9Sstevel@tonic-gate 	open_input(infile, define);
787*7c478bd9Sstevel@tonic-gate 	outfilename = extend ? extendfile(infile, outfile) : outfile;
788*7c478bd9Sstevel@tonic-gate 	open_output(infile, outfilename);
789*7c478bd9Sstevel@tonic-gate 	add_warning();
790*7c478bd9Sstevel@tonic-gate 	while (def = get_definition()) {
791*7c478bd9Sstevel@tonic-gate 		foundprogram |= (def->def_kind == DEF_PROGRAM);
792*7c478bd9Sstevel@tonic-gate 	}
793*7c478bd9Sstevel@tonic-gate 	if (extend && !foundprogram) {
794*7c478bd9Sstevel@tonic-gate 		(void) unlink(outfilename);
795*7c478bd9Sstevel@tonic-gate 		return;
796*7c478bd9Sstevel@tonic-gate 	}
797*7c478bd9Sstevel@tonic-gate 	write_tables();
798*7c478bd9Sstevel@tonic-gate }
799*7c478bd9Sstevel@tonic-gate 
800*7c478bd9Sstevel@tonic-gate /* sample routine for the server template */
801*7c478bd9Sstevel@tonic-gate static
802*7c478bd9Sstevel@tonic-gate svc_output(infile, define, extend, outfile)
803*7c478bd9Sstevel@tonic-gate 	char *infile;
804*7c478bd9Sstevel@tonic-gate 	char *define;
805*7c478bd9Sstevel@tonic-gate 	int extend;
806*7c478bd9Sstevel@tonic-gate 	char *outfile;
807*7c478bd9Sstevel@tonic-gate {
808*7c478bd9Sstevel@tonic-gate 	definition *def;
809*7c478bd9Sstevel@tonic-gate 	char *include;
810*7c478bd9Sstevel@tonic-gate 	char *outfilename;
811*7c478bd9Sstevel@tonic-gate 	long tell;
812*7c478bd9Sstevel@tonic-gate 	open_input(infile, define);
813*7c478bd9Sstevel@tonic-gate 	outfilename = extend ? extendfile(infile, outfile) : outfile;
814*7c478bd9Sstevel@tonic-gate 	checkfiles(infile, outfilename);
815*7c478bd9Sstevel@tonic-gate 	/*
816*7c478bd9Sstevel@tonic-gate 	 * Check if outfile already exists.
817*7c478bd9Sstevel@tonic-gate 	 * if so, print an error message and exit
818*7c478bd9Sstevel@tonic-gate 	 */
819*7c478bd9Sstevel@tonic-gate 	open_output(infile, outfilename);
820*7c478bd9Sstevel@tonic-gate 	add_sample_msg();
821*7c478bd9Sstevel@tonic-gate 
822*7c478bd9Sstevel@tonic-gate 	if (infile && (include = extendfile(infile, ".h"))) {
823*7c478bd9Sstevel@tonic-gate 		f_print(fout, "#include \"%s\"\n", include);
824*7c478bd9Sstevel@tonic-gate 		free(include);
825*7c478bd9Sstevel@tonic-gate 	} else {
826*7c478bd9Sstevel@tonic-gate 		f_print(fout, "#include <rpc/rpc.h>\n");
827*7c478bd9Sstevel@tonic-gate 	}
828*7c478bd9Sstevel@tonic-gate 
829*7c478bd9Sstevel@tonic-gate 	f_print(fout, "#include <stdio.h>\n");
830*7c478bd9Sstevel@tonic-gate 	f_print(fout, "#include <stdlib.h> /* getenv, exit */\n");
831*7c478bd9Sstevel@tonic-gate 	f_print(fout, "#include <signal.h>\n");
832*7c478bd9Sstevel@tonic-gate 
833*7c478bd9Sstevel@tonic-gate 	tell = ftell(fout);
834*7c478bd9Sstevel@tonic-gate 	while (def = get_definition()) {
835*7c478bd9Sstevel@tonic-gate 		write_sample_svc(def);
836*7c478bd9Sstevel@tonic-gate 	}
837*7c478bd9Sstevel@tonic-gate 	if (extend && tell == ftell(fout)) {
838*7c478bd9Sstevel@tonic-gate 		(void) unlink(outfilename);
839*7c478bd9Sstevel@tonic-gate 	}
840*7c478bd9Sstevel@tonic-gate }
841*7c478bd9Sstevel@tonic-gate 
842*7c478bd9Sstevel@tonic-gate /* sample main routine for client */
843*7c478bd9Sstevel@tonic-gate static
844*7c478bd9Sstevel@tonic-gate clnt_output(infile, define, extend, outfile)
845*7c478bd9Sstevel@tonic-gate 	char *infile;
846*7c478bd9Sstevel@tonic-gate 	char *define;
847*7c478bd9Sstevel@tonic-gate 	int extend;
848*7c478bd9Sstevel@tonic-gate 	char *outfile;
849*7c478bd9Sstevel@tonic-gate {
850*7c478bd9Sstevel@tonic-gate 	definition *def;
851*7c478bd9Sstevel@tonic-gate 	char *include;
852*7c478bd9Sstevel@tonic-gate 	char *outfilename;
853*7c478bd9Sstevel@tonic-gate 	long tell;
854*7c478bd9Sstevel@tonic-gate 	int has_program = 0;
855*7c478bd9Sstevel@tonic-gate 
856*7c478bd9Sstevel@tonic-gate 	open_input(infile, define);
857*7c478bd9Sstevel@tonic-gate 	outfilename = extend ? extendfile(infile, outfile) : outfile;
858*7c478bd9Sstevel@tonic-gate 	checkfiles(infile, outfilename);
859*7c478bd9Sstevel@tonic-gate 	/*
860*7c478bd9Sstevel@tonic-gate 	 * Check if outfile already exists.
861*7c478bd9Sstevel@tonic-gate 	 * if so, print an error message and exit
862*7c478bd9Sstevel@tonic-gate 	 */
863*7c478bd9Sstevel@tonic-gate 
864*7c478bd9Sstevel@tonic-gate 	open_output(infile, outfilename);
865*7c478bd9Sstevel@tonic-gate 	add_sample_msg();
866*7c478bd9Sstevel@tonic-gate 	if (infile && (include = extendfile(infile, ".h"))) {
867*7c478bd9Sstevel@tonic-gate 		f_print(fout, "#include \"%s\"\n", include);
868*7c478bd9Sstevel@tonic-gate 		free(include);
869*7c478bd9Sstevel@tonic-gate 	} else
870*7c478bd9Sstevel@tonic-gate 		f_print(fout, "#include <rpc/rpc.h>\n");
871*7c478bd9Sstevel@tonic-gate 
872*7c478bd9Sstevel@tonic-gate 	f_print(fout, "#include <stdio.h>\n");
873*7c478bd9Sstevel@tonic-gate 	f_print(fout, "#include <stdlib.h> /* getenv, exit */\n");
874*7c478bd9Sstevel@tonic-gate 
875*7c478bd9Sstevel@tonic-gate 	tell = ftell(fout);
876*7c478bd9Sstevel@tonic-gate 	while (def = get_definition()) {
877*7c478bd9Sstevel@tonic-gate 		has_program += write_sample_clnt(def);
878*7c478bd9Sstevel@tonic-gate 	}
879*7c478bd9Sstevel@tonic-gate 
880*7c478bd9Sstevel@tonic-gate 	if (has_program)
881*7c478bd9Sstevel@tonic-gate 		write_sample_clnt_main();
882*7c478bd9Sstevel@tonic-gate 
883*7c478bd9Sstevel@tonic-gate 	if (extend && tell == ftell(fout)) {
884*7c478bd9Sstevel@tonic-gate 		(void) unlink(outfilename);
885*7c478bd9Sstevel@tonic-gate 	}
886*7c478bd9Sstevel@tonic-gate }
887*7c478bd9Sstevel@tonic-gate 
888*7c478bd9Sstevel@tonic-gate 
889*7c478bd9Sstevel@tonic-gate static void mkfile_output(cmd)
890*7c478bd9Sstevel@tonic-gate struct commandline *cmd;
891*7c478bd9Sstevel@tonic-gate {
892*7c478bd9Sstevel@tonic-gate 	char *mkfilename, *clientname, *clntname, *xdrname, *hdrname;
893*7c478bd9Sstevel@tonic-gate 	char *servername, *svcname, *servprogname, *clntprogname;
894*7c478bd9Sstevel@tonic-gate 	char *temp;
895*7c478bd9Sstevel@tonic-gate 
896*7c478bd9Sstevel@tonic-gate 	svcname = file_name(cmd->infile, "_svc.c");
897*7c478bd9Sstevel@tonic-gate 	clntname = file_name(cmd->infile, "_clnt.c");
898*7c478bd9Sstevel@tonic-gate 	xdrname = file_name(cmd->infile, "_xdr.c");
899*7c478bd9Sstevel@tonic-gate 	hdrname = file_name(cmd->infile, ".h");
900*7c478bd9Sstevel@tonic-gate 
901*7c478bd9Sstevel@tonic-gate 
902*7c478bd9Sstevel@tonic-gate 	if (allfiles) {
903*7c478bd9Sstevel@tonic-gate 		servername = extendfile(cmd->infile, "_server.c");
904*7c478bd9Sstevel@tonic-gate 		clientname = extendfile(cmd->infile, "_client.c");
905*7c478bd9Sstevel@tonic-gate 	} else {
906*7c478bd9Sstevel@tonic-gate 		servername = " ";
907*7c478bd9Sstevel@tonic-gate 		clientname = " ";
908*7c478bd9Sstevel@tonic-gate 	}
909*7c478bd9Sstevel@tonic-gate 	servprogname = extendfile(cmd->infile, "_server");
910*7c478bd9Sstevel@tonic-gate 	clntprogname = extendfile(cmd->infile, "_client");
911*7c478bd9Sstevel@tonic-gate 
912*7c478bd9Sstevel@tonic-gate 	if (allfiles) {
913*7c478bd9Sstevel@tonic-gate 		mkfilename = alloc(strlen("makefile.") +
914*7c478bd9Sstevel@tonic-gate 			strlen(cmd->infile) + 1);
915*7c478bd9Sstevel@tonic-gate 		if (mkfilename == NULL) {
916*7c478bd9Sstevel@tonic-gate 			f_print(stderr, "Out of memory!\n");
917*7c478bd9Sstevel@tonic-gate 			return;
918*7c478bd9Sstevel@tonic-gate 		}
919*7c478bd9Sstevel@tonic-gate 		temp = (char *)rindex(cmd->infile, '.');
920*7c478bd9Sstevel@tonic-gate 		strcpy(mkfilename, "makefile.");
921*7c478bd9Sstevel@tonic-gate 		(void) strncat(mkfilename, cmd->infile,
922*7c478bd9Sstevel@tonic-gate 			(temp - cmd->infile));
923*7c478bd9Sstevel@tonic-gate 	} else
924*7c478bd9Sstevel@tonic-gate 		mkfilename = cmd->outfile;
925*7c478bd9Sstevel@tonic-gate 
926*7c478bd9Sstevel@tonic-gate 
927*7c478bd9Sstevel@tonic-gate 	checkfiles(NULL, mkfilename);
928*7c478bd9Sstevel@tonic-gate 	open_output(NULL, mkfilename);
929*7c478bd9Sstevel@tonic-gate 
930*7c478bd9Sstevel@tonic-gate 	f_print(fout, "\n# This is a template makefile generated\
931*7c478bd9Sstevel@tonic-gate 		by rpcgen \n");
932*7c478bd9Sstevel@tonic-gate 
933*7c478bd9Sstevel@tonic-gate 	f_print(fout, "\n# Parameters \n\n");
934*7c478bd9Sstevel@tonic-gate 
935*7c478bd9Sstevel@tonic-gate 	f_print(fout, "CLIENT = %s\nSERVER = %s\n\n",
936*7c478bd9Sstevel@tonic-gate 		clntprogname, servprogname);
937*7c478bd9Sstevel@tonic-gate 	f_print(fout, "SOURCES_CLNT.c = \nSOURCES_CLNT.h = \n");
938*7c478bd9Sstevel@tonic-gate 	f_print(fout, "SOURCES_SVC.c = \nSOURCES_SVC.h = \n");
939*7c478bd9Sstevel@tonic-gate 	f_print(fout, "SOURCES.x = %s\n\n", cmd->infile);
940*7c478bd9Sstevel@tonic-gate 	f_print(fout, "TARGETS_SVC.c = %s %s %s \n",
941*7c478bd9Sstevel@tonic-gate 		svcname, servername, xdrname);
942*7c478bd9Sstevel@tonic-gate 	f_print(fout, "TARGETS_CLNT.c = %s %s %s \n",
943*7c478bd9Sstevel@tonic-gate 		clntname, clientname, xdrname);
944*7c478bd9Sstevel@tonic-gate 	f_print(fout, "TARGETS = %s %s %s %s %s %s\n\n",
945*7c478bd9Sstevel@tonic-gate 		hdrname, xdrname, clntname,
946*7c478bd9Sstevel@tonic-gate 		svcname, clientname, servername);
947*7c478bd9Sstevel@tonic-gate 
948*7c478bd9Sstevel@tonic-gate 	f_print(fout, "OBJECTS_CLNT = $(SOURCES_CLNT.c:%%.c=%%.o) "
949*7c478bd9Sstevel@tonic-gate 			"$(TARGETS_CLNT.c:%%.c=%%.o) ");
950*7c478bd9Sstevel@tonic-gate 
951*7c478bd9Sstevel@tonic-gate 	f_print(fout, "\nOBJECTS_SVC = $(SOURCES_SVC.c:%%.c=%%.o) "
952*7c478bd9Sstevel@tonic-gate 			"$(TARGETS_SVC.c:%%.c=%%.o) ");
953*7c478bd9Sstevel@tonic-gate 
954*7c478bd9Sstevel@tonic-gate 
955*7c478bd9Sstevel@tonic-gate 	f_print(fout, "\n# Compiler flags \n");
956*7c478bd9Sstevel@tonic-gate 	if (mtflag)
957*7c478bd9Sstevel@tonic-gate 		f_print(fout, "\nCPPFLAGS += -D_REENTRANT\n"
958*7c478bd9Sstevel@tonic-gate 			"CFLAGS += -g\nLDLIBS += -lnsl\n");
959*7c478bd9Sstevel@tonic-gate 	else
960*7c478bd9Sstevel@tonic-gate 		f_print(fout, "\nCFLAGS += -g \nLDLIBS += -lnsl\n");
961*7c478bd9Sstevel@tonic-gate 	f_print(fout, "RPCGENFLAGS = \n");
962*7c478bd9Sstevel@tonic-gate 
963*7c478bd9Sstevel@tonic-gate 	f_print(fout, "\n# Targets \n\n");
964*7c478bd9Sstevel@tonic-gate 
965*7c478bd9Sstevel@tonic-gate 	f_print(fout, "all : $(CLIENT) $(SERVER)\n\n");
966*7c478bd9Sstevel@tonic-gate 	f_print(fout, "$(TARGETS) : $(SOURCES.x) \n");
967*7c478bd9Sstevel@tonic-gate 	f_print(fout, "\trpcgen $(RPCGENFLAGS) $(SOURCES.x)\n\n");
968*7c478bd9Sstevel@tonic-gate 	f_print(fout, "$(OBJECTS_CLNT) : $(SOURCES_CLNT.c) $(SOURCES_CLNT.h) \
969*7c478bd9Sstevel@tonic-gate $(TARGETS_CLNT.c) \n\n");
970*7c478bd9Sstevel@tonic-gate 
971*7c478bd9Sstevel@tonic-gate 	f_print(fout, "$(OBJECTS_SVC) : $(SOURCES_SVC.c) $(SOURCES_SVC.h) \
972*7c478bd9Sstevel@tonic-gate $(TARGETS_SVC.c) \n\n");
973*7c478bd9Sstevel@tonic-gate 	f_print(fout, "$(CLIENT) : $(OBJECTS_CLNT) \n");
974*7c478bd9Sstevel@tonic-gate 	f_print(fout, "\t$(LINK.c) -o $(CLIENT) $(OBJECTS_CLNT) \
975*7c478bd9Sstevel@tonic-gate $(LDLIBS) \n\n");
976*7c478bd9Sstevel@tonic-gate 	f_print(fout, "$(SERVER) : $(OBJECTS_SVC) \n");
977*7c478bd9Sstevel@tonic-gate 	f_print(fout, "\t$(LINK.c) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS)\n\n ");
978*7c478bd9Sstevel@tonic-gate 	f_print(fout, "clean:\n\t $(RM) core $(TARGETS) $(OBJECTS_CLNT) \
979*7c478bd9Sstevel@tonic-gate $(OBJECTS_SVC) $(CLIENT) $(SERVER)\n\n");
980*7c478bd9Sstevel@tonic-gate }
981*7c478bd9Sstevel@tonic-gate 
982*7c478bd9Sstevel@tonic-gate 
983*7c478bd9Sstevel@tonic-gate 
984*7c478bd9Sstevel@tonic-gate /*
985*7c478bd9Sstevel@tonic-gate  * Perform registrations for service output
986*7c478bd9Sstevel@tonic-gate  * Return 0 if failed; 1 otherwise.
987*7c478bd9Sstevel@tonic-gate  */
988*7c478bd9Sstevel@tonic-gate static int
989*7c478bd9Sstevel@tonic-gate do_registers(argc, argv)
990*7c478bd9Sstevel@tonic-gate 	int argc;
991*7c478bd9Sstevel@tonic-gate 	char *argv[];
992*7c478bd9Sstevel@tonic-gate {
993*7c478bd9Sstevel@tonic-gate 	int i;
994*7c478bd9Sstevel@tonic-gate 
995*7c478bd9Sstevel@tonic-gate 	if (inetdflag || !tirpcflag) {
996*7c478bd9Sstevel@tonic-gate 		for (i = 1; i < argc; i++) {
997*7c478bd9Sstevel@tonic-gate 			if (streq(argv[i], "-s")) {
998*7c478bd9Sstevel@tonic-gate 				if (!check_nettype(argv[i + 1],
999*7c478bd9Sstevel@tonic-gate 						    valid_i_nettypes))
1000*7c478bd9Sstevel@tonic-gate 					return (0);
1001*7c478bd9Sstevel@tonic-gate 				write_inetd_register(argv[i + 1]);
1002*7c478bd9Sstevel@tonic-gate 				i++;
1003*7c478bd9Sstevel@tonic-gate 			}
1004*7c478bd9Sstevel@tonic-gate 		}
1005*7c478bd9Sstevel@tonic-gate 	} else {
1006*7c478bd9Sstevel@tonic-gate 		for (i = 1; i < argc; i++)
1007*7c478bd9Sstevel@tonic-gate 			if (streq(argv[i], "-s")) {
1008*7c478bd9Sstevel@tonic-gate 				if (!check_nettype(argv[i + 1],
1009*7c478bd9Sstevel@tonic-gate 						    valid_ti_nettypes))
1010*7c478bd9Sstevel@tonic-gate 					return (0);
1011*7c478bd9Sstevel@tonic-gate 				write_nettype_register(argv[i + 1]);
1012*7c478bd9Sstevel@tonic-gate 				i++;
1013*7c478bd9Sstevel@tonic-gate 			} else if (streq(argv[i], "-n")) {
1014*7c478bd9Sstevel@tonic-gate 				write_netid_register(argv[i + 1]);
1015*7c478bd9Sstevel@tonic-gate 				i++;
1016*7c478bd9Sstevel@tonic-gate 			}
1017*7c478bd9Sstevel@tonic-gate 	}
1018*7c478bd9Sstevel@tonic-gate 	return (1);
1019*7c478bd9Sstevel@tonic-gate }
1020*7c478bd9Sstevel@tonic-gate 
1021*7c478bd9Sstevel@tonic-gate /*
1022*7c478bd9Sstevel@tonic-gate  * Add another argument to the arg list
1023*7c478bd9Sstevel@tonic-gate  */
1024*7c478bd9Sstevel@tonic-gate static void
1025*7c478bd9Sstevel@tonic-gate addarg(cp)
1026*7c478bd9Sstevel@tonic-gate 	char *cp;
1027*7c478bd9Sstevel@tonic-gate {
1028*7c478bd9Sstevel@tonic-gate 	if (argcount >= ARGLISTLEN) {
1029*7c478bd9Sstevel@tonic-gate 		f_print(stderr, "rpcgen: too many defines\n");
1030*7c478bd9Sstevel@tonic-gate 		crash();
1031*7c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
1032*7c478bd9Sstevel@tonic-gate 	}
1033*7c478bd9Sstevel@tonic-gate 	arglist[argcount++] = cp;
1034*7c478bd9Sstevel@tonic-gate 
1035*7c478bd9Sstevel@tonic-gate }
1036*7c478bd9Sstevel@tonic-gate 
1037*7c478bd9Sstevel@tonic-gate static void
1038*7c478bd9Sstevel@tonic-gate putarg(where, cp)
1039*7c478bd9Sstevel@tonic-gate 	char *cp;
1040*7c478bd9Sstevel@tonic-gate 	int where;
1041*7c478bd9Sstevel@tonic-gate {
1042*7c478bd9Sstevel@tonic-gate 	if (where >= ARGLISTLEN) {
1043*7c478bd9Sstevel@tonic-gate 		f_print(stderr, "rpcgen: arglist coding error\n");
1044*7c478bd9Sstevel@tonic-gate 		crash();
1045*7c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
1046*7c478bd9Sstevel@tonic-gate 	}
1047*7c478bd9Sstevel@tonic-gate 	arglist[where] = cp;
1048*7c478bd9Sstevel@tonic-gate }
1049*7c478bd9Sstevel@tonic-gate 
1050*7c478bd9Sstevel@tonic-gate /*
1051*7c478bd9Sstevel@tonic-gate  * if input file is stdin and an output file is specified then complain
1052*7c478bd9Sstevel@tonic-gate  * if the file already exists. Otherwise the file may get overwritten
1053*7c478bd9Sstevel@tonic-gate  * If input file does not exist, exit with an error
1054*7c478bd9Sstevel@tonic-gate  */
1055*7c478bd9Sstevel@tonic-gate 
1056*7c478bd9Sstevel@tonic-gate static void
1057*7c478bd9Sstevel@tonic-gate checkfiles(infile, outfile)
1058*7c478bd9Sstevel@tonic-gate char *infile;
1059*7c478bd9Sstevel@tonic-gate char *outfile;
1060*7c478bd9Sstevel@tonic-gate {
1061*7c478bd9Sstevel@tonic-gate 
1062*7c478bd9Sstevel@tonic-gate 	struct stat buf;
1063*7c478bd9Sstevel@tonic-gate 
1064*7c478bd9Sstevel@tonic-gate 	if (infile)		/* infile ! = NULL */
1065*7c478bd9Sstevel@tonic-gate 		if (stat(infile, &buf) < 0)
1066*7c478bd9Sstevel@tonic-gate 		{
1067*7c478bd9Sstevel@tonic-gate 			perror(infile);
1068*7c478bd9Sstevel@tonic-gate 			crash();
1069*7c478bd9Sstevel@tonic-gate 		};
1070*7c478bd9Sstevel@tonic-gate 	if (outfile) {
1071*7c478bd9Sstevel@tonic-gate 		if (stat(outfile, &buf) < 0)
1072*7c478bd9Sstevel@tonic-gate 			return;	/* file does not exist */
1073*7c478bd9Sstevel@tonic-gate 		else {
1074*7c478bd9Sstevel@tonic-gate 			f_print(stderr,
1075*7c478bd9Sstevel@tonic-gate 	"file '%s' already exists and may be overwritten\n",
1076*7c478bd9Sstevel@tonic-gate 				outfile);
1077*7c478bd9Sstevel@tonic-gate 			crash();
1078*7c478bd9Sstevel@tonic-gate 		}
1079*7c478bd9Sstevel@tonic-gate 	}
1080*7c478bd9Sstevel@tonic-gate }
1081*7c478bd9Sstevel@tonic-gate 
1082*7c478bd9Sstevel@tonic-gate /*
1083*7c478bd9Sstevel@tonic-gate  * Parse command line arguments
1084*7c478bd9Sstevel@tonic-gate  */
1085*7c478bd9Sstevel@tonic-gate static
1086*7c478bd9Sstevel@tonic-gate parseargs(argc, argv, cmd)
1087*7c478bd9Sstevel@tonic-gate 	int argc;
1088*7c478bd9Sstevel@tonic-gate 	char *argv[];
1089*7c478bd9Sstevel@tonic-gate 	struct commandline *cmd;
1090*7c478bd9Sstevel@tonic-gate {
1091*7c478bd9Sstevel@tonic-gate 	int i;
1092*7c478bd9Sstevel@tonic-gate 	int j;
1093*7c478bd9Sstevel@tonic-gate 	char c, ch;
1094*7c478bd9Sstevel@tonic-gate 	char flag[(1 << 8 * sizeof (char))];
1095*7c478bd9Sstevel@tonic-gate 	int nflags;
1096*7c478bd9Sstevel@tonic-gate 
1097*7c478bd9Sstevel@tonic-gate 	cmdname = argv[0];
1098*7c478bd9Sstevel@tonic-gate 	cmd->infile = cmd->outfile = NULL;
1099*7c478bd9Sstevel@tonic-gate 	if (argc < 2) {
1100*7c478bd9Sstevel@tonic-gate 		return (0);
1101*7c478bd9Sstevel@tonic-gate 	}
1102*7c478bd9Sstevel@tonic-gate 	allfiles = 0;
1103*7c478bd9Sstevel@tonic-gate 	flag['c'] = 0;
1104*7c478bd9Sstevel@tonic-gate 	flag['h'] = 0;
1105*7c478bd9Sstevel@tonic-gate 	flag['l'] = 0;
1106*7c478bd9Sstevel@tonic-gate 	flag['m'] = 0;
1107*7c478bd9Sstevel@tonic-gate 	flag['o'] = 0;
1108*7c478bd9Sstevel@tonic-gate 	flag['s'] = 0;
1109*7c478bd9Sstevel@tonic-gate 	flag['n'] = 0;
1110*7c478bd9Sstevel@tonic-gate 	flag['t'] = 0;
1111*7c478bd9Sstevel@tonic-gate 	flag['S'] = 0;
1112*7c478bd9Sstevel@tonic-gate 	flag['C'] = 0;
1113*7c478bd9Sstevel@tonic-gate 	flag['M'] = 0;
1114*7c478bd9Sstevel@tonic-gate 
1115*7c478bd9Sstevel@tonic-gate 	for (i = 1; i < argc; i++) {
1116*7c478bd9Sstevel@tonic-gate 		if (argv[i][0] != '-') {
1117*7c478bd9Sstevel@tonic-gate 			if (cmd->infile) {
1118*7c478bd9Sstevel@tonic-gate 				f_print(stderr,
1119*7c478bd9Sstevel@tonic-gate 	"Cannot specify more than one input file.\n");
1120*7c478bd9Sstevel@tonic-gate 
1121*7c478bd9Sstevel@tonic-gate 				return (0);
1122*7c478bd9Sstevel@tonic-gate 			}
1123*7c478bd9Sstevel@tonic-gate 			cmd->infile = argv[i];
1124*7c478bd9Sstevel@tonic-gate 		} else {
1125*7c478bd9Sstevel@tonic-gate 			for (j = 1; argv[i][j] != 0; j++) {
1126*7c478bd9Sstevel@tonic-gate 				c = argv[i][j];
1127*7c478bd9Sstevel@tonic-gate 				switch (c) {
1128*7c478bd9Sstevel@tonic-gate 				case 'a':
1129*7c478bd9Sstevel@tonic-gate 					allfiles = 1;
1130*7c478bd9Sstevel@tonic-gate 					break;
1131*7c478bd9Sstevel@tonic-gate 				case 'c':
1132*7c478bd9Sstevel@tonic-gate 				case 'h':
1133*7c478bd9Sstevel@tonic-gate 				case 'l':
1134*7c478bd9Sstevel@tonic-gate 				case 'm':
1135*7c478bd9Sstevel@tonic-gate 				case 't':
1136*7c478bd9Sstevel@tonic-gate 					if (flag[c]) {
1137*7c478bd9Sstevel@tonic-gate 						return (0);
1138*7c478bd9Sstevel@tonic-gate 					}
1139*7c478bd9Sstevel@tonic-gate 					flag[c] = 1;
1140*7c478bd9Sstevel@tonic-gate 					break;
1141*7c478bd9Sstevel@tonic-gate 				case 'S':
1142*7c478bd9Sstevel@tonic-gate 					/*
1143*7c478bd9Sstevel@tonic-gate 					 * sample flag: Ss or Sc.
1144*7c478bd9Sstevel@tonic-gate 					 *  Ss means set flag['S'];
1145*7c478bd9Sstevel@tonic-gate 					 *  Sc means set flag['C'];
1146*7c478bd9Sstevel@tonic-gate 					 *  Sm means set flag['M'];
1147*7c478bd9Sstevel@tonic-gate 					 */
1148*7c478bd9Sstevel@tonic-gate 					ch = argv[i][++j]; /* get next char */
1149*7c478bd9Sstevel@tonic-gate 					if (ch == 's')
1150*7c478bd9Sstevel@tonic-gate 						ch = 'S';
1151*7c478bd9Sstevel@tonic-gate 					else if (ch == 'c')
1152*7c478bd9Sstevel@tonic-gate 						ch = 'C';
1153*7c478bd9Sstevel@tonic-gate 					else if (ch == 'm')
1154*7c478bd9Sstevel@tonic-gate 						ch = 'M';
1155*7c478bd9Sstevel@tonic-gate 					else
1156*7c478bd9Sstevel@tonic-gate 						return (0);
1157*7c478bd9Sstevel@tonic-gate 
1158*7c478bd9Sstevel@tonic-gate 					if (flag[ch]) {
1159*7c478bd9Sstevel@tonic-gate 						return (0);
1160*7c478bd9Sstevel@tonic-gate 					}
1161*7c478bd9Sstevel@tonic-gate 					flag[ch] = 1;
1162*7c478bd9Sstevel@tonic-gate 					break;
1163*7c478bd9Sstevel@tonic-gate 				case 'C': /* ANSI C syntax */
1164*7c478bd9Sstevel@tonic-gate 					Cflag = 1;
1165*7c478bd9Sstevel@tonic-gate 					ch = argv[i][j+1]; /* get next char */
1166*7c478bd9Sstevel@tonic-gate 
1167*7c478bd9Sstevel@tonic-gate 					if (ch != 'C')
1168*7c478bd9Sstevel@tonic-gate 						break;
1169*7c478bd9Sstevel@tonic-gate 					CCflag = 1;
1170*7c478bd9Sstevel@tonic-gate 					break;
1171*7c478bd9Sstevel@tonic-gate 				case 'b':
1172*7c478bd9Sstevel@tonic-gate 					/*
1173*7c478bd9Sstevel@tonic-gate 					 *  Turn TIRPC flag off for
1174*7c478bd9Sstevel@tonic-gate 					 *  generating backward compatible
1175*7c478bd9Sstevel@tonic-gate 					 *  code
1176*7c478bd9Sstevel@tonic-gate 					 */
1177*7c478bd9Sstevel@tonic-gate 					tirpcflag = 0;
1178*7c478bd9Sstevel@tonic-gate 					break;
1179*7c478bd9Sstevel@tonic-gate 
1180*7c478bd9Sstevel@tonic-gate 				case 'I':
1181*7c478bd9Sstevel@tonic-gate 					inetdflag = 1;
1182*7c478bd9Sstevel@tonic-gate 					break;
1183*7c478bd9Sstevel@tonic-gate 				case 'N':
1184*7c478bd9Sstevel@tonic-gate 					newstyle = 1;
1185*7c478bd9Sstevel@tonic-gate 					break;
1186*7c478bd9Sstevel@tonic-gate 				case 'L':
1187*7c478bd9Sstevel@tonic-gate 					logflag = 1;
1188*7c478bd9Sstevel@tonic-gate 					break;
1189*7c478bd9Sstevel@tonic-gate 				case 'K':
1190*7c478bd9Sstevel@tonic-gate 					if (++i == argc) {
1191*7c478bd9Sstevel@tonic-gate 						return (0);
1192*7c478bd9Sstevel@tonic-gate 					}
1193*7c478bd9Sstevel@tonic-gate 					svcclosetime = argv[i];
1194*7c478bd9Sstevel@tonic-gate 					goto nextarg;
1195*7c478bd9Sstevel@tonic-gate 				case 'T':
1196*7c478bd9Sstevel@tonic-gate 					tblflag = 1;
1197*7c478bd9Sstevel@tonic-gate 					break;
1198*7c478bd9Sstevel@tonic-gate 				case 'A':
1199*7c478bd9Sstevel@tonic-gate 					mtauto = 1;
1200*7c478bd9Sstevel@tonic-gate 					/* fall through */
1201*7c478bd9Sstevel@tonic-gate 				case 'M':
1202*7c478bd9Sstevel@tonic-gate 					mtflag = 1;
1203*7c478bd9Sstevel@tonic-gate 					break;
1204*7c478bd9Sstevel@tonic-gate 				case 'i' :
1205*7c478bd9Sstevel@tonic-gate 					if (++i == argc) {
1206*7c478bd9Sstevel@tonic-gate 						return (0);
1207*7c478bd9Sstevel@tonic-gate 					}
1208*7c478bd9Sstevel@tonic-gate 					inlinelen = atoi(argv[i]);
1209*7c478bd9Sstevel@tonic-gate 					goto nextarg;
1210*7c478bd9Sstevel@tonic-gate 				case 'n':
1211*7c478bd9Sstevel@tonic-gate 				case 'o':
1212*7c478bd9Sstevel@tonic-gate 				case 's':
1213*7c478bd9Sstevel@tonic-gate 					if (argv[i][j - 1] != '-' ||
1214*7c478bd9Sstevel@tonic-gate 					    argv[i][j + 1] != 0) {
1215*7c478bd9Sstevel@tonic-gate 						return (0);
1216*7c478bd9Sstevel@tonic-gate 					}
1217*7c478bd9Sstevel@tonic-gate 					flag[c] = 1;
1218*7c478bd9Sstevel@tonic-gate 					if (++i == argc) {
1219*7c478bd9Sstevel@tonic-gate 						return (0);
1220*7c478bd9Sstevel@tonic-gate 					}
1221*7c478bd9Sstevel@tonic-gate 					if (c == 'o') {
1222*7c478bd9Sstevel@tonic-gate 						if (cmd->outfile) {
1223*7c478bd9Sstevel@tonic-gate 							return (0);
1224*7c478bd9Sstevel@tonic-gate 						}
1225*7c478bd9Sstevel@tonic-gate 						cmd->outfile = argv[i];
1226*7c478bd9Sstevel@tonic-gate 					}
1227*7c478bd9Sstevel@tonic-gate 					goto nextarg;
1228*7c478bd9Sstevel@tonic-gate 				case 'D':
1229*7c478bd9Sstevel@tonic-gate 					if (argv[i][j - 1] != '-') {
1230*7c478bd9Sstevel@tonic-gate 						return (0);
1231*7c478bd9Sstevel@tonic-gate 					}
1232*7c478bd9Sstevel@tonic-gate 					(void) addarg(argv[i]);
1233*7c478bd9Sstevel@tonic-gate 					goto nextarg;
1234*7c478bd9Sstevel@tonic-gate 				case 'v':
1235*7c478bd9Sstevel@tonic-gate 					version_info();
1236*7c478bd9Sstevel@tonic-gate 					return (0);
1237*7c478bd9Sstevel@tonic-gate 				case 'Y':
1238*7c478bd9Sstevel@tonic-gate 					if (++i == argc) {
1239*7c478bd9Sstevel@tonic-gate 						return (0);
1240*7c478bd9Sstevel@tonic-gate 					}
1241*7c478bd9Sstevel@tonic-gate 					(void) strcpy(pathbuf, argv[i]);
1242*7c478bd9Sstevel@tonic-gate 					(void) strcat(pathbuf, "/cpp");
1243*7c478bd9Sstevel@tonic-gate 					CPP = pathbuf;
1244*7c478bd9Sstevel@tonic-gate 					cppDefined = 1;
1245*7c478bd9Sstevel@tonic-gate 					goto nextarg;
1246*7c478bd9Sstevel@tonic-gate 				case 'r':
1247*7c478bd9Sstevel@tonic-gate 					rflag = !rflag;
1248*7c478bd9Sstevel@tonic-gate 					break;
1249*7c478bd9Sstevel@tonic-gate 				default:
1250*7c478bd9Sstevel@tonic-gate 					return (0);
1251*7c478bd9Sstevel@tonic-gate 				}
1252*7c478bd9Sstevel@tonic-gate 			}
1253*7c478bd9Sstevel@tonic-gate 		nextarg:
1254*7c478bd9Sstevel@tonic-gate 			;
1255*7c478bd9Sstevel@tonic-gate 		}
1256*7c478bd9Sstevel@tonic-gate 	}
1257*7c478bd9Sstevel@tonic-gate 
1258*7c478bd9Sstevel@tonic-gate 	cmd->cflag = flag['c'];
1259*7c478bd9Sstevel@tonic-gate 	cmd->hflag = flag['h'];
1260*7c478bd9Sstevel@tonic-gate 	cmd->lflag = flag['l'];
1261*7c478bd9Sstevel@tonic-gate 	cmd->mflag = flag['m'];
1262*7c478bd9Sstevel@tonic-gate 	cmd->nflag = flag['n'];
1263*7c478bd9Sstevel@tonic-gate 	cmd->sflag = flag['s'];
1264*7c478bd9Sstevel@tonic-gate 	cmd->tflag = flag['t'];
1265*7c478bd9Sstevel@tonic-gate 	cmd->Ssflag = flag['S'];
1266*7c478bd9Sstevel@tonic-gate 	cmd->Scflag = flag['C'];
1267*7c478bd9Sstevel@tonic-gate 	cmd->makefileflag = flag['M'];
1268*7c478bd9Sstevel@tonic-gate 
1269*7c478bd9Sstevel@tonic-gate 	if (tirpcflag) {
1270*7c478bd9Sstevel@tonic-gate 		if (inetdflag) {
1271*7c478bd9Sstevel@tonic-gate 			f_print(stderr,
1272*7c478bd9Sstevel@tonic-gate 				"Cannot use -I flag without -b flag.\n");
1273*7c478bd9Sstevel@tonic-gate 			return (0);
1274*7c478bd9Sstevel@tonic-gate 		}
1275*7c478bd9Sstevel@tonic-gate 		pmflag = 1;
1276*7c478bd9Sstevel@tonic-gate 	} else {		/* 4.1 mode */
1277*7c478bd9Sstevel@tonic-gate 		pmflag = 0;	/* set pmflag only in tirpcmode */
1278*7c478bd9Sstevel@tonic-gate 		inetdflag = 1;	/* inetdflag is TRUE by default */
1279*7c478bd9Sstevel@tonic-gate 		if (cmd->nflag) { /* netid needs TIRPC */
1280*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "Cannot use netid flag without TIRPC.\n");
1281*7c478bd9Sstevel@tonic-gate 			return (0);
1282*7c478bd9Sstevel@tonic-gate 		}
1283*7c478bd9Sstevel@tonic-gate 	}
1284*7c478bd9Sstevel@tonic-gate 
1285*7c478bd9Sstevel@tonic-gate 	if (newstyle && (tblflag || cmd->tflag)) {
1286*7c478bd9Sstevel@tonic-gate 		f_print(stderr, "Cannot use table flags with newstyle.\n");
1287*7c478bd9Sstevel@tonic-gate 		return (0);
1288*7c478bd9Sstevel@tonic-gate 	}
1289*7c478bd9Sstevel@tonic-gate 
1290*7c478bd9Sstevel@tonic-gate 	/* check no conflicts with file generation flags */
1291*7c478bd9Sstevel@tonic-gate 	nflags = cmd->cflag + cmd->hflag + cmd->lflag + cmd->mflag +
1292*7c478bd9Sstevel@tonic-gate 		cmd->sflag + cmd->nflag + cmd->tflag + cmd->Ssflag +
1293*7c478bd9Sstevel@tonic-gate 			cmd->Scflag + cmd->makefileflag;
1294*7c478bd9Sstevel@tonic-gate 
1295*7c478bd9Sstevel@tonic-gate 	if (nflags == 0) {
1296*7c478bd9Sstevel@tonic-gate 		if (cmd->outfile != NULL || cmd->infile == NULL) {
1297*7c478bd9Sstevel@tonic-gate 			return (0);
1298*7c478bd9Sstevel@tonic-gate 		}
1299*7c478bd9Sstevel@tonic-gate 	} else if (cmd->infile == NULL &&
1300*7c478bd9Sstevel@tonic-gate 	    (cmd->Ssflag || cmd->Scflag || cmd->makefileflag)) {
1301*7c478bd9Sstevel@tonic-gate 		f_print(stderr, "\"infile\" is required for template"
1302*7c478bd9Sstevel@tonic-gate 			" generation flags.\n");
1303*7c478bd9Sstevel@tonic-gate 		return (0);
1304*7c478bd9Sstevel@tonic-gate 	} if (nflags > 1) {
1305*7c478bd9Sstevel@tonic-gate 		f_print(stderr,
1306*7c478bd9Sstevel@tonic-gate 			"Cannot have more than one file generation flag.\n");
1307*7c478bd9Sstevel@tonic-gate 		return (0);
1308*7c478bd9Sstevel@tonic-gate 	}
1309*7c478bd9Sstevel@tonic-gate 	return (1);
1310*7c478bd9Sstevel@tonic-gate }
1311*7c478bd9Sstevel@tonic-gate 
1312*7c478bd9Sstevel@tonic-gate static
1313*7c478bd9Sstevel@tonic-gate usage()
1314*7c478bd9Sstevel@tonic-gate {
1315*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "%s  (%d.%d)\n", cmdname, RPCGEN_MAJOR, RPCGEN_MINOR);
1316*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "usage:  %s infile\n", cmdname);
1317*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "\t%s [-abCLNTMA] [-Dname[=value]] [-i size]"
1318*7c478bd9Sstevel@tonic-gate 		" [-I [-K seconds]] [-Y path] infile\n", cmdname);
1319*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "\t%s [-c | -h | -l | -m | -t | -Sc | -Ss | -Sm]"
1320*7c478bd9Sstevel@tonic-gate 		" [-o outfile] [infile]\n", cmdname);
1321*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "\t%s [-s nettype]* [-o outfile] [infile]\n", cmdname);
1322*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "\t%s [-n netid]* [-o outfile] [infile]\n", cmdname);
1323*7c478bd9Sstevel@tonic-gate 	options_usage();
1324*7c478bd9Sstevel@tonic-gate 	exit(1);
1325*7c478bd9Sstevel@tonic-gate }
1326*7c478bd9Sstevel@tonic-gate 
1327*7c478bd9Sstevel@tonic-gate static
1328*7c478bd9Sstevel@tonic-gate version_info()
1329*7c478bd9Sstevel@tonic-gate {
1330*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "%s %d.%d\n", cmdname, RPCGEN_MAJOR, RPCGEN_MINOR);
1331*7c478bd9Sstevel@tonic-gate 	exit(1);
1332*7c478bd9Sstevel@tonic-gate }
1333*7c478bd9Sstevel@tonic-gate 
1334*7c478bd9Sstevel@tonic-gate static
1335*7c478bd9Sstevel@tonic-gate options_usage()
1336*7c478bd9Sstevel@tonic-gate {
1337*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "options:\n");
1338*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "-a\t\tgenerate all files, including samples\n");
1339*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "-A\t\tgenerate code to enable automatic MT mode\n");
1340*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "-b\t\tbackward compatibility mode (generates code"
1341*7c478bd9Sstevel@tonic-gate 			" for SunOS 4.X)\n");
1342*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "-c\t\tgenerate XDR routines\n");
1343*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "-C\t\tANSI C mode\n");
1344*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "-Dname[=value]\tdefine a symbol (same as #define)\n");
1345*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "-h\t\tgenerate header file\n");
1346*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "-i size\t\tsize at which to start generating"
1347*7c478bd9Sstevel@tonic-gate 			" inline code\n");
1348*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "-I\t\tgenerate code for inetd support in server"
1349*7c478bd9Sstevel@tonic-gate 			" (for SunOS 4.X)\n");
1350*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "-K seconds\tserver exits after K seconds of"
1351*7c478bd9Sstevel@tonic-gate 			" inactivity\n");
1352*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "-l\t\tgenerate client side stubs\n");
1353*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "-L\t\tserver errors will be printed to syslog\n");
1354*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "-m\t\tgenerate server side stubs\n");
1355*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "-M\t\tgenerate MT-safe code\n");
1356*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "-n netid\tgenerate server code that supports"
1357*7c478bd9Sstevel@tonic-gate 			" named netid\n");
1358*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "-N\t\tsupports multiple arguments and"
1359*7c478bd9Sstevel@tonic-gate 			" call-by-value\n");
1360*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "-o outfile\tname of the output file\n");
1361*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "-s nettype\tgenerate server code that supports named"
1362*7c478bd9Sstevel@tonic-gate 			" nettype\n");
1363*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "-Sc\t\tgenerate sample client code that uses remote"
1364*7c478bd9Sstevel@tonic-gate 			" procedures\n");
1365*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "-Ss\t\tgenerate sample server code that defines"
1366*7c478bd9Sstevel@tonic-gate 			" remote procedures\n");
1367*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "-Sm \t\tgenerate makefile template \n");
1368*7c478bd9Sstevel@tonic-gate 
1369*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "-t\t\tgenerate RPC dispatch table\n");
1370*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "-T\t\tgenerate code to support RPC dispatch tables\n");
1371*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "-v\t\tprint version information and exit\n");
1372*7c478bd9Sstevel@tonic-gate 	f_print(stderr, "-Y path\t\tpath where cpp is found\n");
1373*7c478bd9Sstevel@tonic-gate 	exit(1);
1374*7c478bd9Sstevel@tonic-gate }
1375*7c478bd9Sstevel@tonic-gate 
1376*7c478bd9Sstevel@tonic-gate 
1377*7c478bd9Sstevel@tonic-gate char *
1378*7c478bd9Sstevel@tonic-gate rindex(sp, c)
1379*7c478bd9Sstevel@tonic-gate 	register char *sp, c;
1380*7c478bd9Sstevel@tonic-gate {
1381*7c478bd9Sstevel@tonic-gate 	register char *r;
1382*7c478bd9Sstevel@tonic-gate 
1383*7c478bd9Sstevel@tonic-gate 	r = NULL;
1384*7c478bd9Sstevel@tonic-gate 	do {
1385*7c478bd9Sstevel@tonic-gate 		if (*sp == c)
1386*7c478bd9Sstevel@tonic-gate 			r = sp;
1387*7c478bd9Sstevel@tonic-gate 	} while (*sp++);
1388*7c478bd9Sstevel@tonic-gate 	return (r);
1389*7c478bd9Sstevel@tonic-gate }
1390