xref: /illumos-gate/usr/src/cmd/rpcgen/rpc_main.c (revision d67944fb)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5a2f144d1SJordan Brown  * Common Development and Distribution License (the "License").
6a2f144d1SJordan Brown  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2161961e0fSrobinson 
227c478bd9Sstevel@tonic-gate /*
23a2f144d1SJordan Brown  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
277c478bd9Sstevel@tonic-gate /* All Rights Reserved */
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
307c478bd9Sstevel@tonic-gate  * The Regents of the University of California
317c478bd9Sstevel@tonic-gate  * All Rights Reserved
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
347c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
357c478bd9Sstevel@tonic-gate  * contributors.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate  * rpc_main.c, Top level of the RPC protocol compiler.
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <stdio.h>
4361961e0fSrobinson #include <stdlib.h>
447c478bd9Sstevel@tonic-gate #include <string.h>
4561961e0fSrobinson #include <strings.h>
467c478bd9Sstevel@tonic-gate #include <unistd.h>
4761961e0fSrobinson #include <ctype.h>
487c478bd9Sstevel@tonic-gate #include <sys/types.h>
497c478bd9Sstevel@tonic-gate #include <sys/param.h>
507c478bd9Sstevel@tonic-gate #include <sys/file.h>
517c478bd9Sstevel@tonic-gate #include <sys/stat.h>
527c478bd9Sstevel@tonic-gate #include "rpc_parse.h"
537c478bd9Sstevel@tonic-gate #include "rpc_util.h"
547c478bd9Sstevel@tonic-gate #include "rpc_scan.h"
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 
5761961e0fSrobinson extern void write_sample_svc(definition *);
5861961e0fSrobinson extern int write_sample_clnt(definition *);
5961961e0fSrobinson extern void write_sample_clnt_main(void);
6061961e0fSrobinson extern void reinitialize(void);
6161961e0fSrobinson extern void crash(void);
6261961e0fSrobinson extern void add_type(int, char *);
6361961e0fSrobinson extern void add_sample_msg(void);
6461961e0fSrobinson 
6561961e0fSrobinson static void svc_output(char *, char *, int, char *);
6661961e0fSrobinson static void clnt_output(char *, char *, int, char *);
6761961e0fSrobinson static void c_output(char *, char *, int, char *);
6861961e0fSrobinson static void mkfile_output(struct commandline *);
6961961e0fSrobinson static void c_initialize(void);
7061961e0fSrobinson static void h_output(char *, char *, int, char *);
7161961e0fSrobinson static void s_output(int, char *[], char *, char *, int, char *, int, int);
7261961e0fSrobinson static void l_output(char *, char *, int, char *);
7361961e0fSrobinson static void t_output(char *, char *, int, char *);
7461961e0fSrobinson static int do_registers(int, char *[]);
7561961e0fSrobinson static uint_t parseargs(int, char *[], struct commandline *);
7661961e0fSrobinson static void usage(void);
7761961e0fSrobinson static void version_info(void);
7861961e0fSrobinson static void options_usage(void);
7961961e0fSrobinson 
8061961e0fSrobinson #define	EXTEND		1		/* alias for TRUE */
817c478bd9Sstevel@tonic-gate #define	DONT_EXTEND	0		/* alias for FALSE */
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #define	SUNOS_CPP "/usr/lib/cpp"
847c478bd9Sstevel@tonic-gate static int cppDefined = 0;	/* explicit path for C preprocessor */
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate static char *cmdname;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate static char *svcclosetime = "120";
907c478bd9Sstevel@tonic-gate static char *CPP = SUNOS_CPP;
917c478bd9Sstevel@tonic-gate static char CPPFLAGS[] = "-C";
927c478bd9Sstevel@tonic-gate static char pathbuf[MAXPATHLEN + 1];
937c478bd9Sstevel@tonic-gate static char *allv[] = {
947c478bd9Sstevel@tonic-gate 	"rpcgen", "-s", "udp", "-s", "tcp",
957c478bd9Sstevel@tonic-gate };
967c478bd9Sstevel@tonic-gate static int allc = sizeof (allv)/sizeof (allv[0]);
977c478bd9Sstevel@tonic-gate static char *allnv[] = {
987c478bd9Sstevel@tonic-gate 	"rpcgen", "-s", "netpath",
997c478bd9Sstevel@tonic-gate };
1007c478bd9Sstevel@tonic-gate static int allnc = sizeof (allnv)/sizeof (allnv[0]);
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate /*
1037c478bd9Sstevel@tonic-gate  * machinations for handling expanding argument list
1047c478bd9Sstevel@tonic-gate  */
10561961e0fSrobinson static void addarg(char *);	/* add another argument to the list */
10661961e0fSrobinson static void putarg(int, char *); /* put argument at specified location  */
10761961e0fSrobinson static void clear_args(void);	/* clear argument list */
10861961e0fSrobinson static void checkfiles(char *, char *);	/* check if out file already exists */
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate #define	ARGLISTLEN	20
1127c478bd9Sstevel@tonic-gate #define	FIXEDARGS	2
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate static char *arglist[ARGLISTLEN];
1157c478bd9Sstevel@tonic-gate static int argcount = FIXEDARGS;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate int nonfatalerrors;	/* errors */
1197c478bd9Sstevel@tonic-gate int inetdflag;	/* Support for inetd  is now the default */
1207c478bd9Sstevel@tonic-gate int pmflag;		/* Support for port monitors */
1217c478bd9Sstevel@tonic-gate int logflag;		/* Use syslog instead of fprintf for errors */
1227c478bd9Sstevel@tonic-gate int tblflag;		/* Support for dispatch table file */
1237c478bd9Sstevel@tonic-gate int mtflag = 0;		/* Support for MT */
1247c478bd9Sstevel@tonic-gate int mtauto = 0;		/* Enable automatic mode */
1257c478bd9Sstevel@tonic-gate int rflag = 1;		/* Eliminate tail recursion from structures */
1267c478bd9Sstevel@tonic-gate #define	INLINE 5
1277c478bd9Sstevel@tonic-gate /* length at which to start doing an inline */
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate int inlinelen = INLINE;
1307c478bd9Sstevel@tonic-gate /*
1317c478bd9Sstevel@tonic-gate  * Length at which to start doing an inline. INLINE = default
1327c478bd9Sstevel@tonic-gate  * if 0, no xdr_inline code
1337c478bd9Sstevel@tonic-gate  */
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate int indefinitewait;	/* If started by port monitors, hang till it wants */
1367c478bd9Sstevel@tonic-gate int exitnow;		/* If started by port monitors, exit after the call */
1377c478bd9Sstevel@tonic-gate int timerflag;		/* TRUE if !indefinite && !exitnow */
1387c478bd9Sstevel@tonic-gate int newstyle;		/* newstyle of passing arguments (by value) */
1397c478bd9Sstevel@tonic-gate int Cflag = 0;		/* ANSI C syntax */
1407c478bd9Sstevel@tonic-gate int CCflag = 0;		/* C++ files */
1417c478bd9Sstevel@tonic-gate static int allfiles;   /* generate all files */
1427c478bd9Sstevel@tonic-gate int tirpcflag = 1;    /* generating code for tirpc, by default */
1437c478bd9Sstevel@tonic-gate xdrfunc *xdrfunc_head = NULL; /* xdr function list */
1447c478bd9Sstevel@tonic-gate xdrfunc *xdrfunc_tail = NULL; /* xdr function list */
1457c478bd9Sstevel@tonic-gate pid_t childpid;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 
14861961e0fSrobinson int
main(int argc,char * argv[])14961961e0fSrobinson main(int argc, char *argv[])
1507c478bd9Sstevel@tonic-gate {
1517c478bd9Sstevel@tonic-gate 	struct commandline cmd;
1527c478bd9Sstevel@tonic-gate 
15361961e0fSrobinson 	(void) memset(&cmd, 0, sizeof (struct commandline));
1547c478bd9Sstevel@tonic-gate 	clear_args();
1557c478bd9Sstevel@tonic-gate 	if (!parseargs(argc, argv, &cmd))
1567c478bd9Sstevel@tonic-gate 		usage();
1577c478bd9Sstevel@tonic-gate 	/*
1587c478bd9Sstevel@tonic-gate 	 * Only the client and server side stubs are likely to be customized,
1597c478bd9Sstevel@tonic-gate 	 *  so in that case only, check if the outfile exists, and if so,
1607c478bd9Sstevel@tonic-gate 	 *  print an error message and exit.
1617c478bd9Sstevel@tonic-gate 	 */
16261961e0fSrobinson 	if (cmd.Ssflag || cmd.Scflag || cmd.makefileflag)
1637c478bd9Sstevel@tonic-gate 		checkfiles(cmd.infile, cmd.outfile);
1647c478bd9Sstevel@tonic-gate 	else
1657c478bd9Sstevel@tonic-gate 		checkfiles(cmd.infile, NULL);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	if (cmd.cflag) {
1687c478bd9Sstevel@tonic-gate 		c_output(cmd.infile, "-DRPC_XDR", DONT_EXTEND, cmd.outfile);
1697c478bd9Sstevel@tonic-gate 	} else if (cmd.hflag) {
1707c478bd9Sstevel@tonic-gate 		h_output(cmd.infile, "-DRPC_HDR", DONT_EXTEND, cmd.outfile);
1717c478bd9Sstevel@tonic-gate 	} else if (cmd.lflag) {
1727c478bd9Sstevel@tonic-gate 		l_output(cmd.infile, "-DRPC_CLNT", DONT_EXTEND, cmd.outfile);
1737c478bd9Sstevel@tonic-gate 	} else if (cmd.sflag || cmd.mflag || (cmd.nflag)) {
1747c478bd9Sstevel@tonic-gate 		s_output(argc, argv, cmd.infile, "-DRPC_SVC", DONT_EXTEND,
175a2f144d1SJordan Brown 		    cmd.outfile, cmd.mflag, cmd.nflag);
1767c478bd9Sstevel@tonic-gate 	} else if (cmd.tflag) {
1777c478bd9Sstevel@tonic-gate 		t_output(cmd.infile, "-DRPC_TBL", DONT_EXTEND, cmd.outfile);
1787c478bd9Sstevel@tonic-gate 	} else if (cmd.Ssflag) {
1797c478bd9Sstevel@tonic-gate 		svc_output(cmd.infile, "-DRPC_SERVER", DONT_EXTEND,
180a2f144d1SJordan Brown 		    cmd.outfile);
1817c478bd9Sstevel@tonic-gate 	} else if (cmd.Scflag) {
1827c478bd9Sstevel@tonic-gate 		clnt_output(cmd.infile, "-DRPC_CLIENT", DONT_EXTEND,
183a2f144d1SJordan Brown 		    cmd.outfile);
1847c478bd9Sstevel@tonic-gate 	} else if (cmd.makefileflag) {
1857c478bd9Sstevel@tonic-gate 		mkfile_output(&cmd);
1867c478bd9Sstevel@tonic-gate 	} else {
1877c478bd9Sstevel@tonic-gate 		/* the rescans are required, since cpp may effect input */
1887c478bd9Sstevel@tonic-gate 		c_output(cmd.infile, "-DRPC_XDR", EXTEND, "_xdr.c");
1897c478bd9Sstevel@tonic-gate 		reinitialize();
1907c478bd9Sstevel@tonic-gate 		h_output(cmd.infile, "-DRPC_HDR", EXTEND, ".h");
1917c478bd9Sstevel@tonic-gate 		reinitialize();
1927c478bd9Sstevel@tonic-gate 		l_output(cmd.infile, "-DRPC_CLNT", EXTEND, "_clnt.c");
1937c478bd9Sstevel@tonic-gate 		reinitialize();
1947c478bd9Sstevel@tonic-gate 		if (inetdflag || !tirpcflag)
1957c478bd9Sstevel@tonic-gate 			s_output(allc, allv, cmd.infile, "-DRPC_SVC", EXTEND,
196a2f144d1SJordan Brown 			    "_svc.c", cmd.mflag, cmd.nflag);
1977c478bd9Sstevel@tonic-gate 		else
1987c478bd9Sstevel@tonic-gate 			s_output(allnc, allnv, cmd.infile, "-DRPC_SVC",
199a2f144d1SJordan Brown 			    EXTEND, "_svc.c", cmd.mflag, cmd.nflag);
2007c478bd9Sstevel@tonic-gate 		if (tblflag) {
2017c478bd9Sstevel@tonic-gate 			reinitialize();
20261961e0fSrobinson 			t_output(cmd.infile, "-DRPC_TBL", EXTEND, "_tbl.i");
2037c478bd9Sstevel@tonic-gate 		}
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 		if (allfiles) {
2067c478bd9Sstevel@tonic-gate 			reinitialize();
2077c478bd9Sstevel@tonic-gate 			svc_output(cmd.infile, "-DRPC_SERVER", EXTEND,
208a2f144d1SJordan Brown 			    "_server.c");
2097c478bd9Sstevel@tonic-gate 			reinitialize();
2107c478bd9Sstevel@tonic-gate 			clnt_output(cmd.infile, "-DRPC_CLIENT", EXTEND,
211a2f144d1SJordan Brown 			    "_client.c");
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 		}
2147c478bd9Sstevel@tonic-gate 		if (allfiles || (cmd.makefileflag == 1)) {
2157c478bd9Sstevel@tonic-gate 			reinitialize();
2167c478bd9Sstevel@tonic-gate 			mkfile_output(&cmd);
2177c478bd9Sstevel@tonic-gate 		}
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	}
22061961e0fSrobinson 	return (nonfatalerrors);
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate /*
2257c478bd9Sstevel@tonic-gate  * add extension to filename
2267c478bd9Sstevel@tonic-gate  */
2277c478bd9Sstevel@tonic-gate static char *
extendfile(char * file,char * ext)22861961e0fSrobinson extendfile(char *file, char *ext)
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate 	char *res;
2317c478bd9Sstevel@tonic-gate 	char *p;
2327c478bd9Sstevel@tonic-gate 
23361961e0fSrobinson 	res = malloc(strlen(file) + strlen(ext) + 1);
23461961e0fSrobinson 	if (res == NULL)
2357c478bd9Sstevel@tonic-gate 		abort();
2367c478bd9Sstevel@tonic-gate 	p = strrchr(file, '.');
23761961e0fSrobinson 	if (p == NULL)
2387c478bd9Sstevel@tonic-gate 		p = file + strlen(file);
2397c478bd9Sstevel@tonic-gate 	(void) strcpy(res, file);
2407c478bd9Sstevel@tonic-gate 	(void) strcpy(res + (p - file), ext);
2417c478bd9Sstevel@tonic-gate 	return (res);
2427c478bd9Sstevel@tonic-gate }
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate /*
2457c478bd9Sstevel@tonic-gate  * Open output file with given extension
2467c478bd9Sstevel@tonic-gate  */
24761961e0fSrobinson static void
open_output(char * infile,char * outfile)24861961e0fSrobinson open_output(char *infile, char *outfile)
2497c478bd9Sstevel@tonic-gate {
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	if (outfile == NULL) {
2527c478bd9Sstevel@tonic-gate 		fout = stdout;
2537c478bd9Sstevel@tonic-gate 		return;
2547c478bd9Sstevel@tonic-gate 	}
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	if (infile != NULL && streq(outfile, infile)) {
257a2f144d1SJordan Brown 		f_print(stderr,
258a2f144d1SJordan Brown 		    "%s: %s already exists.  No output generated.\n",
259a2f144d1SJordan Brown 		    cmdname, infile);
2607c478bd9Sstevel@tonic-gate 		crash();
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 	fout = fopen(outfile, "w");
2637c478bd9Sstevel@tonic-gate 	if (fout == NULL) {
2647c478bd9Sstevel@tonic-gate 		f_print(stderr, "%s: unable to open ", cmdname);
2657c478bd9Sstevel@tonic-gate 		perror(outfile);
2667c478bd9Sstevel@tonic-gate 		crash();
2677c478bd9Sstevel@tonic-gate 	}
2687c478bd9Sstevel@tonic-gate 	record_open(outfile);
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate 
27261961e0fSrobinson static void
add_warning(void)27361961e0fSrobinson add_warning(void)
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate 	f_print(fout, "/*\n");
2767c478bd9Sstevel@tonic-gate 	f_print(fout, " * Please do not edit this file.\n");
2777c478bd9Sstevel@tonic-gate 	f_print(fout, " * It was generated using rpcgen.\n");
2787c478bd9Sstevel@tonic-gate 	f_print(fout, " */\n\n");
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate /* clear list of arguments */
28261961e0fSrobinson static void
clear_args(void)28361961e0fSrobinson clear_args(void)
2847c478bd9Sstevel@tonic-gate {
2857c478bd9Sstevel@tonic-gate 	int i;
28661961e0fSrobinson 
2877c478bd9Sstevel@tonic-gate 	for (i = FIXEDARGS; i < ARGLISTLEN; i++)
2887c478bd9Sstevel@tonic-gate 		arglist[i] = NULL;
2897c478bd9Sstevel@tonic-gate 	argcount = FIXEDARGS;
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate /* make sure that a CPP exists */
29361961e0fSrobinson static void
find_cpp(void)29461961e0fSrobinson find_cpp(void)
2957c478bd9Sstevel@tonic-gate {
2967c478bd9Sstevel@tonic-gate 	struct stat buf;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	if (stat(CPP, &buf) < 0)  { /* SVR4 or explicit cpp does not exist */
2997c478bd9Sstevel@tonic-gate 		if (cppDefined) {
30061961e0fSrobinson 			(void) fprintf(stderr,
301a2f144d1SJordan Brown 			    "cannot find C preprocessor: %s \n", CPP);
3027c478bd9Sstevel@tonic-gate 			crash();
3037c478bd9Sstevel@tonic-gate 		} else {	/* try the other one */
3047c478bd9Sstevel@tonic-gate 			CPP = SUNOS_CPP;
3057c478bd9Sstevel@tonic-gate 			if (stat(CPP, &buf) < 0) { /* can't find any cpp */
30661961e0fSrobinson 				(void) fprintf(stderr,
3077c478bd9Sstevel@tonic-gate 		"cannot find any C preprocessor (cpp)\n");
3087c478bd9Sstevel@tonic-gate 				crash();
3097c478bd9Sstevel@tonic-gate 			}
3107c478bd9Sstevel@tonic-gate 		}
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate }
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate /*
3157c478bd9Sstevel@tonic-gate  * Open input file with given define for C-preprocessor
3167c478bd9Sstevel@tonic-gate  */
31761961e0fSrobinson static void
open_input(char * infile,char * define)31861961e0fSrobinson open_input(char *infile, char *define)
3197c478bd9Sstevel@tonic-gate {
3207c478bd9Sstevel@tonic-gate 	int pd[2];
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	infilename = (infile == NULL) ? "<stdin>" : infile;
3237c478bd9Sstevel@tonic-gate 	(void) pipe(pd);
3247c478bd9Sstevel@tonic-gate 	switch (childpid = fork()) {
3257c478bd9Sstevel@tonic-gate 	case 0:
3267c478bd9Sstevel@tonic-gate 		find_cpp();
3277c478bd9Sstevel@tonic-gate 		putarg(0, CPP);
3287c478bd9Sstevel@tonic-gate 		putarg(1, CPPFLAGS);
3297c478bd9Sstevel@tonic-gate 		addarg(define);
3307c478bd9Sstevel@tonic-gate 		if (infile)
3317c478bd9Sstevel@tonic-gate 			addarg(infile);
3327c478bd9Sstevel@tonic-gate 		addarg((char *)NULL);
3337c478bd9Sstevel@tonic-gate 		(void) close(1);
3347c478bd9Sstevel@tonic-gate 		(void) dup2(pd[1], 1);
3357c478bd9Sstevel@tonic-gate 		(void) close(pd[0]);
33661961e0fSrobinson 		(void) execv(arglist[0], arglist);
3377c478bd9Sstevel@tonic-gate 		perror("execv");
3387c478bd9Sstevel@tonic-gate 		exit(1);
33961961e0fSrobinson 		/* NOTREACHED */
3407c478bd9Sstevel@tonic-gate 	case -1:
3417c478bd9Sstevel@tonic-gate 		perror("fork");
3427c478bd9Sstevel@tonic-gate 		exit(1);
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate 	(void) close(pd[1]);
3457c478bd9Sstevel@tonic-gate 	fin = fdopen(pd[0], "r");
3467c478bd9Sstevel@tonic-gate 	if (fin == NULL) {
3477c478bd9Sstevel@tonic-gate 		f_print(stderr, "%s: ", cmdname);
3487c478bd9Sstevel@tonic-gate 		perror(infilename);
3497c478bd9Sstevel@tonic-gate 		crash();
3507c478bd9Sstevel@tonic-gate 	}
3517c478bd9Sstevel@tonic-gate }
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate /* valid tirpc nettypes */
35461961e0fSrobinson static char *valid_ti_nettypes[] = {
3557c478bd9Sstevel@tonic-gate 	"netpath",
3567c478bd9Sstevel@tonic-gate 	"visible",
3577c478bd9Sstevel@tonic-gate 	"circuit_v",
3587c478bd9Sstevel@tonic-gate 	"datagram_v",
3597c478bd9Sstevel@tonic-gate 	"circuit_n",
3607c478bd9Sstevel@tonic-gate 	"datagram_n",
3617c478bd9Sstevel@tonic-gate 	"udp",
3627c478bd9Sstevel@tonic-gate 	"tcp",
3637c478bd9Sstevel@tonic-gate 	"raw",
3647c478bd9Sstevel@tonic-gate 	NULL
36561961e0fSrobinson };
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate /* valid inetd nettypes */
36861961e0fSrobinson static char *valid_i_nettypes[] = {
3697c478bd9Sstevel@tonic-gate 	"udp",
3707c478bd9Sstevel@tonic-gate 	"tcp",
3717c478bd9Sstevel@tonic-gate 	NULL
37261961e0fSrobinson };
3737c478bd9Sstevel@tonic-gate 
37461961e0fSrobinson static int
check_nettype(char * name,char * list_to_check[])37561961e0fSrobinson check_nettype(char *name, char *list_to_check[])
3767c478bd9Sstevel@tonic-gate {
3777c478bd9Sstevel@tonic-gate 	int i;
3787c478bd9Sstevel@tonic-gate 	for (i = 0; list_to_check[i] != NULL; i++) {
3797c478bd9Sstevel@tonic-gate 		if (strcmp(name, list_to_check[i]) == 0) {
3807c478bd9Sstevel@tonic-gate 			return (1);
3817c478bd9Sstevel@tonic-gate 		}
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate 	f_print(stderr, "illegal nettype :\'%s\'\n", name);
3847c478bd9Sstevel@tonic-gate 	return (0);
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate static char *
file_name(char * file,char * ext)38861961e0fSrobinson file_name(char *file, char *ext)
3897c478bd9Sstevel@tonic-gate {
3907c478bd9Sstevel@tonic-gate 	char *temp;
3917c478bd9Sstevel@tonic-gate 	temp = extendfile(file, ext);
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	if (access(temp, F_OK) != -1)
3947c478bd9Sstevel@tonic-gate 		return (temp);
3957c478bd9Sstevel@tonic-gate 	else
3967c478bd9Sstevel@tonic-gate 		return ((char *)" ");
3977c478bd9Sstevel@tonic-gate }
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 
40061961e0fSrobinson static void
c_output(char * infile,char * define,int extend,char * outfile)40161961e0fSrobinson c_output(char *infile, char *define, int extend, char *outfile)
4027c478bd9Sstevel@tonic-gate {
4037c478bd9Sstevel@tonic-gate 	definition *def;
4047c478bd9Sstevel@tonic-gate 	char *include;
4057c478bd9Sstevel@tonic-gate 	char *outfilename;
4067c478bd9Sstevel@tonic-gate 	long tell;
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	c_initialize();
4097c478bd9Sstevel@tonic-gate 	open_input(infile, define);
4107c478bd9Sstevel@tonic-gate 	outfilename = extend ? extendfile(infile, outfile) : outfile;
4117c478bd9Sstevel@tonic-gate 	open_output(infile, outfilename);
4127c478bd9Sstevel@tonic-gate 	add_warning();
4137c478bd9Sstevel@tonic-gate 	if (infile && (include = extendfile(infile, ".h"))) {
4147c478bd9Sstevel@tonic-gate 		f_print(fout, "#include \"%s\"\n", include);
4157c478bd9Sstevel@tonic-gate 		free(include);
4167c478bd9Sstevel@tonic-gate 		/* .h file already contains rpc/rpc.h */
4177c478bd9Sstevel@tonic-gate 	} else
4187c478bd9Sstevel@tonic-gate 		f_print(fout, "#include <rpc/rpc.h>\n");
4197c478bd9Sstevel@tonic-gate 	/*
42061961e0fSrobinson 	 * Include stdlib.h to support mem_alloc calls.
4217c478bd9Sstevel@tonic-gate 	 */
4227c478bd9Sstevel@tonic-gate 	f_print(fout, "\n#ifndef _KERNEL\n");
42361961e0fSrobinson 	f_print(fout, "#include <stdlib.h>\n");
4247c478bd9Sstevel@tonic-gate 	f_print(fout, "#endif /* !_KERNEL */\n\n");
4257c478bd9Sstevel@tonic-gate 	tell = ftell(fout);
4267c478bd9Sstevel@tonic-gate 	while (def = get_definition()) {
4277c478bd9Sstevel@tonic-gate 		emit(def);
4287c478bd9Sstevel@tonic-gate 	}
4297c478bd9Sstevel@tonic-gate 	if (extend && tell == ftell(fout)) {
4307c478bd9Sstevel@tonic-gate 		(void) unlink(outfilename);
4317c478bd9Sstevel@tonic-gate 	}
4327c478bd9Sstevel@tonic-gate }
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 
43561961e0fSrobinson static void
c_initialize(void)43661961e0fSrobinson c_initialize(void)
4377c478bd9Sstevel@tonic-gate {
4387c478bd9Sstevel@tonic-gate 	/*
4397c478bd9Sstevel@tonic-gate 	 * add all the starting basic types.
4407c478bd9Sstevel@tonic-gate 	 * We may need to add some derived types
4417c478bd9Sstevel@tonic-gate 	 * if we need to generate INLINE macros.
4427c478bd9Sstevel@tonic-gate 	 * These types are defined in rpc/types.h
4437c478bd9Sstevel@tonic-gate 	 */
4447c478bd9Sstevel@tonic-gate 	add_type(1, "int");
4457c478bd9Sstevel@tonic-gate 	add_type(1, "long");
4467c478bd9Sstevel@tonic-gate 	add_type(1, "short");
4477c478bd9Sstevel@tonic-gate 	add_type(1, "bool");
4487c478bd9Sstevel@tonic-gate 	add_type(1, "u_int");
4497c478bd9Sstevel@tonic-gate 	add_type(1, "u_long");
4507c478bd9Sstevel@tonic-gate 	add_type(1, "u_short");
4517c478bd9Sstevel@tonic-gate 	add_type(1, "rpcprog_t");
4527c478bd9Sstevel@tonic-gate 	add_type(1, "rpcvers_t");
4537c478bd9Sstevel@tonic-gate 	add_type(1, "rpcproc_t");
4547c478bd9Sstevel@tonic-gate 	add_type(1, "rpcprot_t");
4557c478bd9Sstevel@tonic-gate 	add_type(1, "rpcport_t");
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate char rpcgen_table_dcl1[] = "struct rpcgen_table {\n";
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate char rpcgen_table_dcl2[] = "\txdrproc_t\txdr_arg;\n"
4617c478bd9Sstevel@tonic-gate 				"\tunsigned\tlen_arg;\n"
4627c478bd9Sstevel@tonic-gate 				"\txdrproc_t\txdr_res;\n"
4637c478bd9Sstevel@tonic-gate 				"\tunsigned\tlen_res;\n"
4647c478bd9Sstevel@tonic-gate 				"};\n";
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate char rpcgen_table_proc[] = "\tvoid\t*(*proc)();\n";
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate char rpcgen_table_proc_b[] = "\tchar\t*(*proc)();\n";
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate char *
generate_guard(char * pathname)47261961e0fSrobinson generate_guard(char *pathname)
4737c478bd9Sstevel@tonic-gate {
4747c478bd9Sstevel@tonic-gate 	char *filename, *guard, *tmp;
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	filename = strrchr(pathname, '/');  /* find last component */
4777c478bd9Sstevel@tonic-gate 	filename = ((filename == 0) ? pathname : filename+1);
4787c478bd9Sstevel@tonic-gate 	guard = extendfile(filename, "_H_RPCGEN");
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	/*
4817c478bd9Sstevel@tonic-gate 	 * Guard must be an ANSI C identifier composed of
4827c478bd9Sstevel@tonic-gate 	 * upper case letters, digits, or '_'.
4837c478bd9Sstevel@tonic-gate 	 * Convert invalid characters to '_'.
4847c478bd9Sstevel@tonic-gate 	 */
4857c478bd9Sstevel@tonic-gate 	for (tmp = guard; *tmp; tmp++) {
4867c478bd9Sstevel@tonic-gate 		if (!isalpha(*tmp) && !isdigit(*tmp)) {
4877c478bd9Sstevel@tonic-gate 			*tmp = '_';
4887c478bd9Sstevel@tonic-gate 			continue;
4897c478bd9Sstevel@tonic-gate 		}
4907c478bd9Sstevel@tonic-gate 		if (islower(*tmp))
4917c478bd9Sstevel@tonic-gate 			*tmp = toupper(*tmp);
4927c478bd9Sstevel@tonic-gate 	}
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	/*
4957c478bd9Sstevel@tonic-gate 	 * The first character must be a letter; the underscore '_'
4967c478bd9Sstevel@tonic-gate 	 * counts as a letter.
4977c478bd9Sstevel@tonic-gate 	 */
4987c478bd9Sstevel@tonic-gate 	if (!isalpha(guard[0]))
4997c478bd9Sstevel@tonic-gate 		guard[0] = '_';
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	return (guard);
5027c478bd9Sstevel@tonic-gate }
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate /*
5057c478bd9Sstevel@tonic-gate  * Compile into an XDR header file
5067c478bd9Sstevel@tonic-gate  */
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 
50961961e0fSrobinson static void
h_output(char * infile,char * define,int extend,char * outfile)51061961e0fSrobinson h_output(char *infile, char *define, int extend, char *outfile)
5117c478bd9Sstevel@tonic-gate {
5127c478bd9Sstevel@tonic-gate 	definition *def;
5137c478bd9Sstevel@tonic-gate 	char *outfilename;
5147c478bd9Sstevel@tonic-gate 	long tell;
5157c478bd9Sstevel@tonic-gate 	char *guard;
5167c478bd9Sstevel@tonic-gate 	list *l;
5177c478bd9Sstevel@tonic-gate 	xdrfunc *xdrfuncp;
5187c478bd9Sstevel@tonic-gate 	int i;
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	open_input(infile, define);
5217c478bd9Sstevel@tonic-gate 	outfilename =  extend ? extendfile(infile, outfile) : outfile;
5227c478bd9Sstevel@tonic-gate 	open_output(infile, outfilename);
5237c478bd9Sstevel@tonic-gate 	add_warning();
52461961e0fSrobinson 	if (outfilename || infile)
5257c478bd9Sstevel@tonic-gate 		guard = generate_guard(outfilename ? outfilename: infile);
52661961e0fSrobinson 	else
5277c478bd9Sstevel@tonic-gate 		guard = "STDIN_";
5287c478bd9Sstevel@tonic-gate 
52961961e0fSrobinson 	f_print(fout, "#ifndef _%s\n#define	_%s\n\n", guard, guard);
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	f_print(fout, "#include <rpc/rpc.h>\n");
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	if (mtflag) {
5347c478bd9Sstevel@tonic-gate 		f_print(fout, "#ifndef _KERNEL\n");
5357c478bd9Sstevel@tonic-gate 		f_print(fout, "#include <synch.h>\n");
5367c478bd9Sstevel@tonic-gate 		f_print(fout, "#include <thread.h>\n");
5377c478bd9Sstevel@tonic-gate 		f_print(fout, "#endif /* !_KERNEL */\n");
5387c478bd9Sstevel@tonic-gate 	};
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	/* put the C++ support */
5417c478bd9Sstevel@tonic-gate 	if (Cflag && !CCflag) {
5427c478bd9Sstevel@tonic-gate 		f_print(fout, "\n#ifdef __cplusplus\n");
5437c478bd9Sstevel@tonic-gate 		f_print(fout, "extern \"C\" {\n");
5447c478bd9Sstevel@tonic-gate 		f_print(fout, "#endif\n\n");
5457c478bd9Sstevel@tonic-gate 	}
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	/* put in a typedef for quadprecision. Only with Cflag */
5487c478bd9Sstevel@tonic-gate 
549*d67944fbSScott Rotondo 	/*
550*d67944fbSScott Rotondo 	 * declaration of struct rpcgen_table must go before
551*d67944fbSScott Rotondo 	 *  the definition of arrays like *_1_table[]
552*d67944fbSScott Rotondo 	 */
553*d67944fbSScott Rotondo 	if (tblflag) {
554*d67944fbSScott Rotondo 		f_print(fout, rpcgen_table_dcl1);
555*d67944fbSScott Rotondo 		if (tirpcflag)
556*d67944fbSScott Rotondo 			f_print(fout, rpcgen_table_proc);
557*d67944fbSScott Rotondo 		else
558*d67944fbSScott Rotondo 			f_print(fout, rpcgen_table_proc_b);
559*d67944fbSScott Rotondo 		f_print(fout, rpcgen_table_dcl2);
560*d67944fbSScott Rotondo 	}
561*d67944fbSScott Rotondo 
5627c478bd9Sstevel@tonic-gate 	tell = ftell(fout);
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	/* print data definitions */
56561961e0fSrobinson 	while (def = get_definition())
5667c478bd9Sstevel@tonic-gate 		print_datadef(def);
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	/*
5697c478bd9Sstevel@tonic-gate 	 * print function declarations.
5707c478bd9Sstevel@tonic-gate 	 *  Do this after data definitions because they might be used as
5717c478bd9Sstevel@tonic-gate 	 *  arguments for functions
5727c478bd9Sstevel@tonic-gate 	 */
57361961e0fSrobinson 	for (l = defined; l != NULL; l = l->next)
5747c478bd9Sstevel@tonic-gate 		print_funcdef(l->val);
5757c478bd9Sstevel@tonic-gate 	/* Now  print all xdr func declarations */
5767c478bd9Sstevel@tonic-gate 	if (xdrfunc_head != NULL) {
57761961e0fSrobinson 		f_print(fout, "\n/* the xdr functions */\n");
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 		if (CCflag) {
58061961e0fSrobinson 			f_print(fout, "\n#ifdef __cplusplus\n");
58161961e0fSrobinson 			f_print(fout, "extern \"C\" {\n");
58261961e0fSrobinson 			f_print(fout, "#endif\n");
58361961e0fSrobinson 		}
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 		if (!Cflag) {
5867c478bd9Sstevel@tonic-gate 			xdrfuncp = xdrfunc_head;
5877c478bd9Sstevel@tonic-gate 			while (xdrfuncp != NULL) {
5887c478bd9Sstevel@tonic-gate 				print_xdr_func_def(xdrfuncp->name,
589a2f144d1SJordan Brown 				    xdrfuncp->pointerp, 2);
5907c478bd9Sstevel@tonic-gate 				xdrfuncp = xdrfuncp->next;
5917c478bd9Sstevel@tonic-gate 			}
5927c478bd9Sstevel@tonic-gate 		} else {
5937c478bd9Sstevel@tonic-gate 			for (i = 1; i < 3; i++) {
5947c478bd9Sstevel@tonic-gate 				if (i == 1)
59561961e0fSrobinson 					f_print(fout,
59661961e0fSrobinson "\n#if defined(__STDC__) || defined(__cplusplus)\n");
5977c478bd9Sstevel@tonic-gate 				else
5987c478bd9Sstevel@tonic-gate 					f_print(fout, "\n#else /* K&R C */\n");
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 				xdrfuncp = xdrfunc_head;
6017c478bd9Sstevel@tonic-gate 				while (xdrfuncp != NULL) {
6027c478bd9Sstevel@tonic-gate 					print_xdr_func_def(xdrfuncp->name,
603a2f144d1SJordan Brown 					    xdrfuncp->pointerp, i);
6047c478bd9Sstevel@tonic-gate 					xdrfuncp = xdrfuncp->next;
6057c478bd9Sstevel@tonic-gate 				}
6067c478bd9Sstevel@tonic-gate 			}
60761961e0fSrobinson 			f_print(fout, "\n#endif /* K&R C */\n");
6087c478bd9Sstevel@tonic-gate 		}
6097c478bd9Sstevel@tonic-gate 	}
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	if (extend && tell == ftell(fout)) {
6127c478bd9Sstevel@tonic-gate 		(void) unlink(outfilename);
6137c478bd9Sstevel@tonic-gate 	}
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	if (Cflag) {
6167c478bd9Sstevel@tonic-gate 		f_print(fout, "\n#ifdef __cplusplus\n");
6177c478bd9Sstevel@tonic-gate 		f_print(fout, "}\n");
6187c478bd9Sstevel@tonic-gate 		f_print(fout, "#endif\n");
6197c478bd9Sstevel@tonic-gate 	}
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	f_print(fout, "\n#endif /* !_%s */\n", guard);
6227c478bd9Sstevel@tonic-gate }
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate /*
6257c478bd9Sstevel@tonic-gate  * Compile into an RPC service
6267c478bd9Sstevel@tonic-gate  */
62761961e0fSrobinson static void
s_output(int argc,char * argv[],char * infile,char * define,int extend,char * outfile,int nomain,int netflag)62861961e0fSrobinson s_output(int argc, char *argv[], char *infile, char *define, int extend,
62961961e0fSrobinson 					char *outfile, int nomain, int netflag)
6307c478bd9Sstevel@tonic-gate {
6317c478bd9Sstevel@tonic-gate 	char *include;
6327c478bd9Sstevel@tonic-gate 	definition *def;
6337c478bd9Sstevel@tonic-gate 	int foundprogram = 0;
6347c478bd9Sstevel@tonic-gate 	char *outfilename;
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	open_input(infile, define);
6377c478bd9Sstevel@tonic-gate 	outfilename = extend ? extendfile(infile, outfile) : outfile;
6387c478bd9Sstevel@tonic-gate 	open_output(infile, outfilename);
6397c478bd9Sstevel@tonic-gate 	add_warning();
6407c478bd9Sstevel@tonic-gate 	if (infile && (include = extendfile(infile, ".h"))) {
6417c478bd9Sstevel@tonic-gate 		f_print(fout, "#include \"%s\"\n", include);
6427c478bd9Sstevel@tonic-gate 		free(include);
6437c478bd9Sstevel@tonic-gate 	} else
6447c478bd9Sstevel@tonic-gate 		f_print(fout, "#include <rpc/rpc.h>\n");
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	f_print(fout, "#include <stdio.h>\n");
6477c478bd9Sstevel@tonic-gate 	f_print(fout, "#include <stdlib.h> /* getenv, exit */\n");
6487c478bd9Sstevel@tonic-gate 	f_print(fout, "#include <signal.h>\n");
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	if (Cflag) {
6517c478bd9Sstevel@tonic-gate 		f_print(fout,
6527c478bd9Sstevel@tonic-gate 		"#include <rpc/pmap_clnt.h> /* for pmap_unset */\n");
6537c478bd9Sstevel@tonic-gate 		f_print(fout, "#include <string.h> /* strcmp */\n");
6547c478bd9Sstevel@tonic-gate 	}
6557c478bd9Sstevel@tonic-gate 	if (strcmp(svcclosetime, "-1") == 0)
6567c478bd9Sstevel@tonic-gate 		indefinitewait = 1;
6577c478bd9Sstevel@tonic-gate 	else if (strcmp(svcclosetime, "0") == 0)
6587c478bd9Sstevel@tonic-gate 		exitnow = 1;
65961961e0fSrobinson 	else if (inetdflag || pmflag)
6607c478bd9Sstevel@tonic-gate 		timerflag = 1;
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	if (!tirpcflag && inetdflag)
6637c478bd9Sstevel@tonic-gate 		f_print(fout, "#include <sys/termios.h> /* TIOCNOTTY */\n");
66461961e0fSrobinson 	if (Cflag && (inetdflag || pmflag))
6657c478bd9Sstevel@tonic-gate 		if (tirpcflag)
6667c478bd9Sstevel@tonic-gate 			f_print(fout, "#include <unistd.h> /* setsid */\n");
6677c478bd9Sstevel@tonic-gate 	if (tirpcflag)
6687c478bd9Sstevel@tonic-gate 		f_print(fout, "#include <sys/types.h>\n");
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	f_print(fout, "#include <memory.h>\n");
6717c478bd9Sstevel@tonic-gate 	f_print(fout, "#include <stropts.h>\n");
6727c478bd9Sstevel@tonic-gate 	if (inetdflag || !tirpcflag) {
6737c478bd9Sstevel@tonic-gate 		f_print(fout, "#include <sys/socket.h>\n");
6747c478bd9Sstevel@tonic-gate 		f_print(fout, "#include <netinet/in.h>\n");
6757c478bd9Sstevel@tonic-gate 		f_print(fout, "#include <rpc/svc_soc.h>\n");
6767c478bd9Sstevel@tonic-gate 	}
6777c478bd9Sstevel@tonic-gate 
67861961e0fSrobinson 	if ((netflag || pmflag) && tirpcflag && !nomain)
6797c478bd9Sstevel@tonic-gate 		f_print(fout, "#include <netconfig.h>\n");
6807c478bd9Sstevel@tonic-gate 	if (tirpcflag)
6817c478bd9Sstevel@tonic-gate 		f_print(fout, "#include <sys/resource.h> /* rlimit */\n");
6827c478bd9Sstevel@tonic-gate 	if (logflag || inetdflag || pmflag)
6837c478bd9Sstevel@tonic-gate 		f_print(fout, "#include <syslog.h>\n");
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	/* for ANSI-C */
6867c478bd9Sstevel@tonic-gate 	if (Cflag)
6877c478bd9Sstevel@tonic-gate 		f_print(fout,
688a2f144d1SJordan Brown 		    "\n#ifndef SIG_PF\n#define	SIG_PF void(*)\
6897c478bd9Sstevel@tonic-gate (int)\n#endif\n");
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	f_print(fout, "\n#ifdef DEBUG\n#define	RPC_SVC_FG\n#endif\n");
6927c478bd9Sstevel@tonic-gate 	if (timerflag)
6937c478bd9Sstevel@tonic-gate 		f_print(fout, "\n#define	_RPCSVC_CLOSEDOWN %s\n",
694a2f144d1SJordan Brown 		    svcclosetime);
69561961e0fSrobinson 	while (def = get_definition())
6967c478bd9Sstevel@tonic-gate 		foundprogram |= (def->def_kind == DEF_PROGRAM);
6977c478bd9Sstevel@tonic-gate 	if (extend && !foundprogram) {
6987c478bd9Sstevel@tonic-gate 		(void) unlink(outfilename);
6997c478bd9Sstevel@tonic-gate 		return;
7007c478bd9Sstevel@tonic-gate 	}
7017c478bd9Sstevel@tonic-gate 	write_most(infile, netflag, nomain);
7027c478bd9Sstevel@tonic-gate 	if (!nomain) {
7037c478bd9Sstevel@tonic-gate 		if (!do_registers(argc, argv)) {
7047c478bd9Sstevel@tonic-gate 			if (outfilename)
7057c478bd9Sstevel@tonic-gate 				(void) unlink(outfilename);
7067c478bd9Sstevel@tonic-gate 			usage();
7077c478bd9Sstevel@tonic-gate 		}
7087c478bd9Sstevel@tonic-gate 		write_rest();
7097c478bd9Sstevel@tonic-gate 	}
7107c478bd9Sstevel@tonic-gate }
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate /*
7137c478bd9Sstevel@tonic-gate  * generate client side stubs
7147c478bd9Sstevel@tonic-gate  */
71561961e0fSrobinson static void
l_output(char * infile,char * define,int extend,char * outfile)71661961e0fSrobinson l_output(char *infile, char *define, int extend, char *outfile)
7177c478bd9Sstevel@tonic-gate {
7187c478bd9Sstevel@tonic-gate 	char *include;
7197c478bd9Sstevel@tonic-gate 	definition *def;
7207c478bd9Sstevel@tonic-gate 	int foundprogram = 0;
7217c478bd9Sstevel@tonic-gate 	char *outfilename;
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	open_input(infile, define);
7247c478bd9Sstevel@tonic-gate 	outfilename = extend ? extendfile(infile, outfile) : outfile;
7257c478bd9Sstevel@tonic-gate 	open_output(infile, outfilename);
7267c478bd9Sstevel@tonic-gate 	add_warning();
7277c478bd9Sstevel@tonic-gate 	if (Cflag)
7287c478bd9Sstevel@tonic-gate 		f_print(fout, "#include <memory.h> /* for memset */\n");
7297c478bd9Sstevel@tonic-gate 	if (infile && (include = extendfile(infile, ".h"))) {
7307c478bd9Sstevel@tonic-gate 		f_print(fout, "#include \"%s\"\n", include);
7317c478bd9Sstevel@tonic-gate 		free(include);
7327c478bd9Sstevel@tonic-gate 	} else
7337c478bd9Sstevel@tonic-gate 		f_print(fout, "#include <rpc/rpc.h>\n");
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 	f_print(fout, "#ifndef _KERNEL\n");
7367c478bd9Sstevel@tonic-gate 	f_print(fout, "#include <stdio.h>\n");
7377c478bd9Sstevel@tonic-gate 	f_print(fout, "#include <stdlib.h> /* getenv, exit */\n");
7387c478bd9Sstevel@tonic-gate 	f_print(fout, "#endif /* !_KERNEL */\n");
7397c478bd9Sstevel@tonic-gate 
74061961e0fSrobinson 	while (def = get_definition())
7417c478bd9Sstevel@tonic-gate 		foundprogram |= (def->def_kind == DEF_PROGRAM);
7427c478bd9Sstevel@tonic-gate 	if (extend && !foundprogram) {
7437c478bd9Sstevel@tonic-gate 		(void) unlink(outfilename);
7447c478bd9Sstevel@tonic-gate 		return;
7457c478bd9Sstevel@tonic-gate 	}
7467c478bd9Sstevel@tonic-gate 	write_stubs();
7477c478bd9Sstevel@tonic-gate }
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate /*
7507c478bd9Sstevel@tonic-gate  * generate the dispatch table
7517c478bd9Sstevel@tonic-gate  */
75261961e0fSrobinson static void
t_output(char * infile,char * define,int extend,char * outfile)75361961e0fSrobinson t_output(char *infile, char *define, int extend, char *outfile)
7547c478bd9Sstevel@tonic-gate {
7557c478bd9Sstevel@tonic-gate 	definition *def;
7567c478bd9Sstevel@tonic-gate 	int foundprogram = 0;
7577c478bd9Sstevel@tonic-gate 	char *outfilename;
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	open_input(infile, define);
7607c478bd9Sstevel@tonic-gate 	outfilename = extend ? extendfile(infile, outfile) : outfile;
7617c478bd9Sstevel@tonic-gate 	open_output(infile, outfilename);
7627c478bd9Sstevel@tonic-gate 	add_warning();
7637c478bd9Sstevel@tonic-gate 	while (def = get_definition()) {
7647c478bd9Sstevel@tonic-gate 		foundprogram |= (def->def_kind == DEF_PROGRAM);
7657c478bd9Sstevel@tonic-gate 	}
7667c478bd9Sstevel@tonic-gate 	if (extend && !foundprogram) {
7677c478bd9Sstevel@tonic-gate 		(void) unlink(outfilename);
7687c478bd9Sstevel@tonic-gate 		return;
7697c478bd9Sstevel@tonic-gate 	}
7707c478bd9Sstevel@tonic-gate 	write_tables();
7717c478bd9Sstevel@tonic-gate }
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate /* sample routine for the server template */
77461961e0fSrobinson static void
svc_output(char * infile,char * define,int extend,char * outfile)77561961e0fSrobinson svc_output(char *infile, char *define, int extend, char *outfile)
7767c478bd9Sstevel@tonic-gate {
7777c478bd9Sstevel@tonic-gate 	definition *def;
7787c478bd9Sstevel@tonic-gate 	char *include;
7797c478bd9Sstevel@tonic-gate 	char *outfilename;
7807c478bd9Sstevel@tonic-gate 	long tell;
7817c478bd9Sstevel@tonic-gate 	open_input(infile, define);
7827c478bd9Sstevel@tonic-gate 	outfilename = extend ? extendfile(infile, outfile) : outfile;
7837c478bd9Sstevel@tonic-gate 	checkfiles(infile, outfilename);
7847c478bd9Sstevel@tonic-gate 	/*
7857c478bd9Sstevel@tonic-gate 	 * Check if outfile already exists.
7867c478bd9Sstevel@tonic-gate 	 * if so, print an error message and exit
7877c478bd9Sstevel@tonic-gate 	 */
7887c478bd9Sstevel@tonic-gate 	open_output(infile, outfilename);
7897c478bd9Sstevel@tonic-gate 	add_sample_msg();
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	if (infile && (include = extendfile(infile, ".h"))) {
7927c478bd9Sstevel@tonic-gate 		f_print(fout, "#include \"%s\"\n", include);
7937c478bd9Sstevel@tonic-gate 		free(include);
7947c478bd9Sstevel@tonic-gate 	} else {
7957c478bd9Sstevel@tonic-gate 		f_print(fout, "#include <rpc/rpc.h>\n");
7967c478bd9Sstevel@tonic-gate 	}
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	f_print(fout, "#include <stdio.h>\n");
7997c478bd9Sstevel@tonic-gate 	f_print(fout, "#include <stdlib.h> /* getenv, exit */\n");
8007c478bd9Sstevel@tonic-gate 	f_print(fout, "#include <signal.h>\n");
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	tell = ftell(fout);
80361961e0fSrobinson 	while (def = get_definition())
8047c478bd9Sstevel@tonic-gate 		write_sample_svc(def);
80561961e0fSrobinson 	if (extend && tell == ftell(fout))
8067c478bd9Sstevel@tonic-gate 		(void) unlink(outfilename);
8077c478bd9Sstevel@tonic-gate }
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate /* sample main routine for client */
81061961e0fSrobinson static void
clnt_output(char * infile,char * define,int extend,char * outfile)81161961e0fSrobinson clnt_output(char *infile, char *define, int extend, char *outfile)
8127c478bd9Sstevel@tonic-gate {
8137c478bd9Sstevel@tonic-gate 	definition *def;
8147c478bd9Sstevel@tonic-gate 	char *include;
8157c478bd9Sstevel@tonic-gate 	char *outfilename;
8167c478bd9Sstevel@tonic-gate 	long tell;
8177c478bd9Sstevel@tonic-gate 	int has_program = 0;
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	open_input(infile, define);
8207c478bd9Sstevel@tonic-gate 	outfilename = extend ? extendfile(infile, outfile) : outfile;
8217c478bd9Sstevel@tonic-gate 	checkfiles(infile, outfilename);
8227c478bd9Sstevel@tonic-gate 	/*
8237c478bd9Sstevel@tonic-gate 	 * Check if outfile already exists.
8247c478bd9Sstevel@tonic-gate 	 * if so, print an error message and exit
8257c478bd9Sstevel@tonic-gate 	 */
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	open_output(infile, outfilename);
8287c478bd9Sstevel@tonic-gate 	add_sample_msg();
8297c478bd9Sstevel@tonic-gate 	if (infile && (include = extendfile(infile, ".h"))) {
8307c478bd9Sstevel@tonic-gate 		f_print(fout, "#include \"%s\"\n", include);
8317c478bd9Sstevel@tonic-gate 		free(include);
8327c478bd9Sstevel@tonic-gate 	} else
8337c478bd9Sstevel@tonic-gate 		f_print(fout, "#include <rpc/rpc.h>\n");
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	f_print(fout, "#include <stdio.h>\n");
8367c478bd9Sstevel@tonic-gate 	f_print(fout, "#include <stdlib.h> /* getenv, exit */\n");
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 	tell = ftell(fout);
83961961e0fSrobinson 	while (def = get_definition())
8407c478bd9Sstevel@tonic-gate 		has_program += write_sample_clnt(def);
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 	if (has_program)
8437c478bd9Sstevel@tonic-gate 		write_sample_clnt_main();
8447c478bd9Sstevel@tonic-gate 
84561961e0fSrobinson 	if (extend && tell == ftell(fout))
8467c478bd9Sstevel@tonic-gate 		(void) unlink(outfilename);
8477c478bd9Sstevel@tonic-gate }
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 
85061961e0fSrobinson static void
mkfile_output(struct commandline * cmd)85161961e0fSrobinson mkfile_output(struct commandline *cmd)
8527c478bd9Sstevel@tonic-gate {
8537c478bd9Sstevel@tonic-gate 	char *mkfilename, *clientname, *clntname, *xdrname, *hdrname;
8547c478bd9Sstevel@tonic-gate 	char *servername, *svcname, *servprogname, *clntprogname;
8557c478bd9Sstevel@tonic-gate 	char *temp;
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 	svcname = file_name(cmd->infile, "_svc.c");
8587c478bd9Sstevel@tonic-gate 	clntname = file_name(cmd->infile, "_clnt.c");
8597c478bd9Sstevel@tonic-gate 	xdrname = file_name(cmd->infile, "_xdr.c");
8607c478bd9Sstevel@tonic-gate 	hdrname = file_name(cmd->infile, ".h");
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 	if (allfiles) {
8647c478bd9Sstevel@tonic-gate 		servername = extendfile(cmd->infile, "_server.c");
8657c478bd9Sstevel@tonic-gate 		clientname = extendfile(cmd->infile, "_client.c");
8667c478bd9Sstevel@tonic-gate 	} else {
8677c478bd9Sstevel@tonic-gate 		servername = " ";
8687c478bd9Sstevel@tonic-gate 		clientname = " ";
8697c478bd9Sstevel@tonic-gate 	}
8707c478bd9Sstevel@tonic-gate 	servprogname = extendfile(cmd->infile, "_server");
8717c478bd9Sstevel@tonic-gate 	clntprogname = extendfile(cmd->infile, "_client");
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 	if (allfiles) {
87461961e0fSrobinson 		mkfilename = malloc(strlen("makefile.") +
875a2f144d1SJordan Brown 		    strlen(cmd->infile) + 1);
8767c478bd9Sstevel@tonic-gate 		if (mkfilename == NULL) {
8777c478bd9Sstevel@tonic-gate 			f_print(stderr, "Out of memory!\n");
8787c478bd9Sstevel@tonic-gate 			return;
8797c478bd9Sstevel@tonic-gate 		}
8807c478bd9Sstevel@tonic-gate 		temp = (char *)rindex(cmd->infile, '.');
88161961e0fSrobinson 		(void) strcpy(mkfilename, "makefile.");
8827c478bd9Sstevel@tonic-gate 		(void) strncat(mkfilename, cmd->infile,
883a2f144d1SJordan Brown 		    (temp - cmd->infile));
8847c478bd9Sstevel@tonic-gate 	} else
8857c478bd9Sstevel@tonic-gate 		mkfilename = cmd->outfile;
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	checkfiles(NULL, mkfilename);
8897c478bd9Sstevel@tonic-gate 	open_output(NULL, mkfilename);
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 	f_print(fout, "\n# This is a template makefile generated\
8927c478bd9Sstevel@tonic-gate 		by rpcgen \n");
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 	f_print(fout, "\n# Parameters \n\n");
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	f_print(fout, "CLIENT = %s\nSERVER = %s\n\n",
897a2f144d1SJordan Brown 	    clntprogname, servprogname);
8987c478bd9Sstevel@tonic-gate 	f_print(fout, "SOURCES_CLNT.c = \nSOURCES_CLNT.h = \n");
8997c478bd9Sstevel@tonic-gate 	f_print(fout, "SOURCES_SVC.c = \nSOURCES_SVC.h = \n");
9007c478bd9Sstevel@tonic-gate 	f_print(fout, "SOURCES.x = %s\n\n", cmd->infile);
9017c478bd9Sstevel@tonic-gate 	f_print(fout, "TARGETS_SVC.c = %s %s %s \n",
902a2f144d1SJordan Brown 	    svcname, servername, xdrname);
9037c478bd9Sstevel@tonic-gate 	f_print(fout, "TARGETS_CLNT.c = %s %s %s \n",
904a2f144d1SJordan Brown 	    clntname, clientname, xdrname);
9057c478bd9Sstevel@tonic-gate 	f_print(fout, "TARGETS = %s %s %s %s %s %s\n\n",
906a2f144d1SJordan Brown 	    hdrname, xdrname, clntname,
907a2f144d1SJordan Brown 	    svcname, clientname, servername);
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	f_print(fout, "OBJECTS_CLNT = $(SOURCES_CLNT.c:%%.c=%%.o) "
910a2f144d1SJordan Brown 	    "$(TARGETS_CLNT.c:%%.c=%%.o) ");
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	f_print(fout, "\nOBJECTS_SVC = $(SOURCES_SVC.c:%%.c=%%.o) "
913a2f144d1SJordan Brown 	    "$(TARGETS_SVC.c:%%.c=%%.o) ");
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 	f_print(fout, "\n# Compiler flags \n");
9177c478bd9Sstevel@tonic-gate 	if (mtflag)
9187c478bd9Sstevel@tonic-gate 		f_print(fout, "\nCPPFLAGS += -D_REENTRANT\n"
919a2f144d1SJordan Brown 		    "CFLAGS += -g\nLDLIBS += -lnsl\n");
9207c478bd9Sstevel@tonic-gate 	else
9217c478bd9Sstevel@tonic-gate 		f_print(fout, "\nCFLAGS += -g \nLDLIBS += -lnsl\n");
9227c478bd9Sstevel@tonic-gate 	f_print(fout, "RPCGENFLAGS = \n");
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	f_print(fout, "\n# Targets \n\n");
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	f_print(fout, "all : $(CLIENT) $(SERVER)\n\n");
9277c478bd9Sstevel@tonic-gate 	f_print(fout, "$(TARGETS) : $(SOURCES.x) \n");
9287c478bd9Sstevel@tonic-gate 	f_print(fout, "\trpcgen $(RPCGENFLAGS) $(SOURCES.x)\n\n");
9297c478bd9Sstevel@tonic-gate 	f_print(fout, "$(OBJECTS_CLNT) : $(SOURCES_CLNT.c) $(SOURCES_CLNT.h) \
9307c478bd9Sstevel@tonic-gate $(TARGETS_CLNT.c) \n\n");
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 	f_print(fout, "$(OBJECTS_SVC) : $(SOURCES_SVC.c) $(SOURCES_SVC.h) \
9337c478bd9Sstevel@tonic-gate $(TARGETS_SVC.c) \n\n");
9347c478bd9Sstevel@tonic-gate 	f_print(fout, "$(CLIENT) : $(OBJECTS_CLNT) \n");
9357c478bd9Sstevel@tonic-gate 	f_print(fout, "\t$(LINK.c) -o $(CLIENT) $(OBJECTS_CLNT) \
9367c478bd9Sstevel@tonic-gate $(LDLIBS) \n\n");
9377c478bd9Sstevel@tonic-gate 	f_print(fout, "$(SERVER) : $(OBJECTS_SVC) \n");
9387c478bd9Sstevel@tonic-gate 	f_print(fout, "\t$(LINK.c) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS)\n\n ");
9397c478bd9Sstevel@tonic-gate 	f_print(fout, "clean:\n\t $(RM) core $(TARGETS) $(OBJECTS_CLNT) \
9407c478bd9Sstevel@tonic-gate $(OBJECTS_SVC) $(CLIENT) $(SERVER)\n\n");
9417c478bd9Sstevel@tonic-gate }
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate /*
9457c478bd9Sstevel@tonic-gate  * Perform registrations for service output
9467c478bd9Sstevel@tonic-gate  * Return 0 if failed; 1 otherwise.