1/*	$NetBSD: rpc_main.c,v 1.33 2009/04/13 06:42:25 lukem Exp $	*/
2
3/*
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part.  Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user or with the express written consent of
10 * Sun Microsystems, Inc.
11 *
12 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
13 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
14 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15 *
16 * Sun RPC is provided with no support and without any obligation on the
17 * part of Sun Microsystems, Inc. to assist in its use, correction,
18 * modification or enhancement.
19 *
20 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
21 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
22 * OR ANY PART THEREOF.
23 *
24 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
25 * or profits or other special, indirect and consequential damages, even if
26 * Sun has been advised of the possibility of such damages.
27 *
28 * Sun Microsystems, Inc.
29 * 2550 Garcia Avenue
30 * Mountain View, California  94043
31 */
32
33#if HAVE_NBTOOL_CONFIG_H
34#include "nbtool_config.h"
35#endif
36
37#include <sys/cdefs.h>
38#if defined(__RCSID) && !defined(lint)
39#if 0
40static char sccsid[] = "@(#)rpc_main.c 1.30 89/03/30 (C) 1987 SMI";
41#else
42__RCSID("$NetBSD: rpc_main.c,v 1.33 2009/04/13 06:42:25 lukem Exp $");
43#endif
44#endif
45
46/*
47 * rpc_main.c, Top level of the RPC protocol compiler.
48 */
49
50#define RPCGEN_VERSION	"199506"/* This program's version (year & month) */
51
52#include <sys/types.h>
53#include <sys/param.h>
54#include <sys/file.h>
55#include <sys/stat.h>
56#include <ctype.h>
57#include <err.h>
58#include <stdio.h>
59#include <stdlib.h>
60#include <string.h>
61#include <unistd.h>
62#include "rpc_scan.h"
63#include "rpc_parse.h"
64#include "rpc_util.h"
65
66#define EXTEND	1		/* alias for TRUE */
67#define DONT_EXTEND	0	/* alias for FALSE */
68
69struct commandline {
70	int     cflag;		/* xdr C routines */
71	int     hflag;		/* header file */
72	int     lflag;		/* client side stubs */
73	int     mflag;		/* server side stubs */
74	int     nflag;		/* netid flag */
75	int     sflag;		/* server stubs for the given transport */
76	int     tflag;		/* dispatch Table file */
77	int     Ssflag;		/* produce server sample code */
78	int     Scflag;		/* produce client sample code */
79	char   *infile;		/* input module name */
80	char   *outfile;	/* output module name */
81};
82
83
84static char *cmdname;
85
86static char *svcclosetime = "120";
87static char *CPP;
88static char CPPFLAGS[] = "-C";
89static char pathbuf[MAXPATHLEN + 1];
90static char *allv[] = {
91	"rpcgen", "-s", "udp", "-s", "tcp",
92};
93static int allc = sizeof(allv) / sizeof(allv[0]);
94static char *allnv[] = {
95	"rpcgen", "-s", "netpath",
96};
97static int allnc = sizeof(allnv) / sizeof(allnv[0]);
98
99#define ARGLISTLEN	20
100#define FIXEDARGS         2
101
102static char *arglist[ARGLISTLEN];
103static int argcount = FIXEDARGS;
104
105
106int     nonfatalerrors;		/* errors */
107int     inetdflag /* = 1 */ ;	/* Support for inetd *//* is now the default */
108int     pmflag;			/* Support for port monitors */
109int     logflag;		/* Use syslog instead of fprintf for errors */
110int     tblflag;		/* Support for dispatch table file */
111int     callerflag;		/* Generate svc_caller() function */
112
113#define INLINE 3
114/*length at which to start doing an inline */
115
116int     doinline = INLINE;	/* length at which to start doing an inline. 3
117				 * = default if 0, no xdr_inline code */
118
119int     indefinitewait;		/* If started by port monitors, hang till it
120				 * wants */
121int     exitnow;		/* If started by port monitors, exit after the
122				 * call */
123int     timerflag;		/* TRUE if !indefinite && !exitnow */
124int     newstyle;		/* newstyle of passing arguments (by value) */
125int     Cflag = 0;		/* ANSI C syntax */
126int	Mflag = 0;		/* multithread safe */
127static int allfiles;		/* generate all files */
128int     tirpcflag = 1;		/* generating code for tirpc, by default */
129
130#ifdef __MSDOS__
131static char *dos_cppfile = NULL;
132#endif
133
134int main __P((int, char *[]));
135
136static char *extendfile __P((char *, char *));
137static void open_output __P((char *, char *));
138static void add_warning __P((void));
139static void clear_args __P((void));
140static void open_input __P((char *, char *));
141static int check_nettype __P((char *, char *[]));
142static void c_output __P((char *, char *, int, char *));
143static void c_initialize __P((void));
144static char *generate_guard __P((char *));
145static void h_output __P((char *, char *, int, char *));
146static void s_output __P((int, char *[], char *, char *, int, char *, int, int));
147static void l_output __P((char *, char *, int, char *));
148static void t_output __P((char *, char *, int, char *));
149static void svc_output __P((char *, char *, int, char *));
150static void clnt_output __P((char *, char *, int, char *));
151static int do_registers __P((int, char *[]));
152static void addarg __P((char *));
153static void putarg __P((int, char *));
154static void checkfiles __P((char *, char *));
155static int parseargs __P((int, char *[], struct commandline *));
156static void usage __P((void));
157static void options_usage __P((void));
158
159int
160main(argc, argv)
161	int     argc;
162	char   *argv[];
163{
164	struct commandline cmd;
165
166	setprogname(argv[0]);
167	if (!(CPP = getenv("RPCGEN_CPP")))
168		CPP = "/usr/bin/cpp";
169
170	(void) memset((char *) &cmd, 0, sizeof(struct commandline));
171	clear_args();
172	if (!parseargs(argc, argv, &cmd))
173		usage();
174
175	if (cmd.cflag || cmd.hflag || cmd.lflag || cmd.tflag || cmd.sflag ||
176	    cmd.mflag || cmd.nflag || cmd.Ssflag || cmd.Scflag) {
177		checkfiles(cmd.infile, cmd.outfile);
178	} else
179		checkfiles(cmd.infile, NULL);
180
181	if (cmd.cflag) {
182		c_output(cmd.infile, "-DRPC_XDR", DONT_EXTEND, cmd.outfile);
183	} else
184		if (cmd.hflag) {
185			h_output(cmd.infile, "-DRPC_HDR", DONT_EXTEND, cmd.outfile);
186		} else
187			if (cmd.lflag) {
188				l_output(cmd.infile, "-DRPC_CLNT", DONT_EXTEND, cmd.outfile);
189			} else
190				if (cmd.sflag || cmd.mflag || (cmd.nflag)) {
191					s_output(argc, argv, cmd.infile, "-DRPC_SVC", DONT_EXTEND,
192					    cmd.outfile, cmd.mflag, cmd.nflag);
193				} else
194					if (cmd.tflag) {
195						t_output(cmd.infile, "-DRPC_TBL", DONT_EXTEND, cmd.outfile);
196					} else
197						if (cmd.Ssflag) {
198							svc_output(cmd.infile, "-DRPC_SERVER", DONT_EXTEND, cmd.outfile);
199						} else
200							if (cmd.Scflag) {
201								clnt_output(cmd.infile, "-DRPC_CLIENT", DONT_EXTEND, cmd.outfile);
202							} else {
203								/* the rescans
204								 * are
205								 * required,
206								 * since cpp
207								 * may effect
208								 * input */
209								c_output(cmd.infile, "-DRPC_XDR", EXTEND, "_xdr.c");
210								reinitialize();
211								h_output(cmd.infile, "-DRPC_HDR", EXTEND, ".h");
212								reinitialize();
213								l_output(cmd.infile, "-DRPC_CLNT", EXTEND, "_clnt.c");
214								reinitialize();
215								if (inetdflag || !tirpcflag)
216									s_output(allc, allv, cmd.infile, "-DRPC_SVC", EXTEND,
217									    "_svc.c", cmd.mflag, cmd.nflag);
218								else
219									s_output(allnc, allnv, cmd.infile, "-DRPC_SVC",
220									    EXTEND, "_svc.c", cmd.mflag, cmd.nflag);
221								if (tblflag) {
222									reinitialize();
223									t_output(cmd.infile, "-DRPC_TBL", EXTEND, "_tbl.i");
224								}
225								if (allfiles) {
226									reinitialize();
227									svc_output(cmd.infile, "-DRPC_SERVER", EXTEND, "_server.c");
228								}
229								if (allfiles) {
230									reinitialize();
231									clnt_output(cmd.infile, "-DRPC_CLIENT", EXTEND, "_client.c");
232								}
233							}
234#ifdef __MSDOS__
235	if (dos_cppfile != NULL) {
236		(void) fclose(fin);
237		(void) unlink(dos_cppfile);
238	}
239#endif
240	exit(nonfatalerrors);
241	/* NOTREACHED */
242}
243/*
244 * add extension to filename
245 */
246static char *
247extendfile(path, ext)
248	char   *path;
249	char   *ext;
250{
251	char   *file;
252	char   *res;
253	char   *p;
254
255	if ((file = strrchr(path, '/')) == NULL)
256		file = path;
257	else
258		file++;
259
260	res = alloc(strlen(file) + strlen(ext) + 1);
261	if (res == NULL) {
262		errx(1, "Out of memory");
263	}
264	p = strrchr(file, '.');
265	if (p == NULL) {
266		p = file + strlen(file);
267	}
268	(void) strcpy(res, file);
269	(void) strcpy(res + (p - file), ext);
270	return (res);
271}
272/*
273 * Open output file with given extension
274 */
275static void
276open_output(infile, outfile)
277	char   *infile;
278	char   *outfile;
279{
280
281	if (outfile == NULL) {
282		fout = stdout;
283		return;
284	}
285	if (infile != NULL && streq(outfile, infile)) {
286		f_print(stderr, "%s: output would overwrite %s\n", cmdname,
287		    infile);
288		crash();
289	}
290	fout = fopen(outfile, "w");
291	if (fout == NULL) {
292		f_print(stderr, "%s: unable to open ", cmdname);
293		perror(outfile);
294		crash();
295	}
296	record_open(outfile);
297
298}
299
300static void
301add_warning()
302{
303	f_print(fout, "/*\n");
304	f_print(fout, " * Please do not edit this file.\n");
305	f_print(fout, " * It was generated using rpcgen.\n");
306	f_print(fout, " */\n\n");
307}
308/* clear list of arguments */
309static void
310clear_args()
311{
312	int     i;
313	for (i = FIXEDARGS; i < ARGLISTLEN; i++)
314		arglist[i] = NULL;
315	argcount = FIXEDARGS;
316}
317
318/*
319 * Open input file with given define for C-preprocessor
320 */
321static void
322open_input(infile, define)
323	char   *infile;
324	char   *define;
325{
326	int     pd[2];
327
328	infilename = (infile == NULL) ? "<stdin>" : infile;
329#ifdef __MSDOS__
330#define	DOSCPP	"\\prog\\bc31\\bin\\cpp.exe"
331	{
332		int     retval;
333		char    drive[MAXDRIVE], dir[MAXDIR], name[MAXFILE], ext[MAXEXT];
334		char    cppfile[MAXPATH];
335		char   *cpp;
336
337		if ((cpp = getenv("RPCGEN_CPP")) == NULL &&
338		    (cpp = searchpath("cpp.exe")) == NULL)
339			cpp = DOSCPP;
340
341		putarg(0, cpp);
342		putarg(1, "-P-");
343		putarg(2, CPPFLAGS);
344		addarg(define);
345		addarg(infile);
346		addarg(NULL);
347
348		retval = spawnvp(P_WAIT, arglist[0], arglist);
349		if (retval != 0) {
350			fprintf(stderr, "%s: C PreProcessor failed\n", cmdname);
351			crash();
352		}
353		fnsplit(infile, drive, dir, name, ext);
354		fnmerge(cppfile, drive, dir, name, ".i");
355
356		fin = fopen(cppfile, "r");
357		if (fin == NULL) {
358			f_print(stderr, "%s: ", cmdname);
359			perror(cppfile);
360			crash();
361		}
362		dos_cppfile = strdup(cppfile);
363		if (dos_cppfile == NULL) {
364			fprintf(stderr, "%s: out of memory\n", cmdname);
365			crash();
366		}
367	}
368#else
369	(void) pipe(pd);
370	switch (fork()) {
371	case 0:
372		putarg(0, CPP);
373		putarg(1, CPPFLAGS);
374		addarg(define);
375		addarg(infile);
376		addarg(NULL);
377		(void) close(1);
378		(void) dup2(pd[1], 1);
379		(void) close(pd[0]);
380		execvp(arglist[0], arglist);
381		err(1, "$RPCGEN_CPP: %s", CPP);
382	case -1:
383		err(1, "fork");
384	}
385	(void) close(pd[1]);
386	fin = fdopen(pd[0], "r");
387#endif
388	if (fin == NULL) {
389		f_print(stderr, "%s: ", cmdname);
390		perror(infilename);
391		crash();
392	}
393}
394/* valid tirpc nettypes */
395static char *valid_ti_nettypes[] =
396{
397	"netpath",
398	"visible",
399	"circuit_v",
400	"datagram_v",
401	"circuit_n",
402	"datagram_n",
403	"udp",
404	"tcp",
405	"raw",
406	NULL
407};
408/* valid inetd nettypes */
409static char *valid_i_nettypes[] =
410{
411	"udp",
412	"tcp",
413	NULL
414};
415
416static int
417check_nettype(name, list_to_check)
418	char   *name;
419	char   *list_to_check[];
420{
421	int     i;
422	for (i = 0; list_to_check[i] != NULL; i++) {
423		if (strcmp(name, list_to_check[i]) == 0) {
424			return 1;
425		}
426	}
427	f_print(stderr, "illegal nettype :\'%s\'\n", name);
428	return 0;
429}
430/*
431 * Compile into an XDR routine output file
432 */
433
434static void
435c_output(infile, define, extend, outfile)
436	char   *infile;
437	char   *define;
438	int     extend;
439	char   *outfile;
440{
441	definition *def;
442	char   *include;
443	char   *outfilename;
444	long    tell;
445
446	c_initialize();
447	open_input(infile, define);
448	outfilename = extend ? extendfile(infile, outfile) : outfile;
449	open_output(infile, outfilename);
450	add_warning();
451	if (infile && (include = extendfile(infile, ".h"))) {
452		f_print(fout, "#include \"%s\"\n", include);
453		free(include);
454		/* .h file already contains rpc/rpc.h */
455	} else
456		f_print(fout, "#include <rpc/rpc.h>\n");
457	tell = ftell(fout);
458	while ((def = get_definition()) != NULL) {
459		emit(def);
460	}
461	if (extend && tell == ftell(fout)) {
462		(void) unlink(outfilename);
463	}
464}
465
466
467static void
468c_initialize()
469{
470
471	/* add all the starting basic types */
472
473	add_type(1, "int");
474	add_type(1, "long");
475	add_type(1, "short");
476	add_type(1, "bool");
477
478	add_type(1, "u_int");
479	add_type(1, "u_long");
480	add_type(1, "u_short");
481
482}
483
484const char    rpcgen_table_dcl[] = "struct rpcgen_table {\n\
485	char	*(*proc)();\n\
486	xdrproc_t	xdr_arg;\n\
487	unsigned	len_arg;\n\
488	xdrproc_t	xdr_res;\n\
489	unsigned	len_res;\n\
490};\n";
491
492
493static char *
494generate_guard(pathname)
495	char   *pathname;
496{
497	char   *filename, *guard, *tmp, *tmp2;
498
499	filename = strrchr(pathname, '/');	/* find last component */
500	filename = ((filename == 0) ? pathname : filename + 1);
501	guard = strdup(filename);
502	/* convert to upper case */
503	tmp = guard;
504	while (*tmp) {
505		*tmp = toupper((unsigned char)*tmp);
506		tmp++;
507	}
508
509	tmp2 = extendfile(guard, "_H_RPCGEN");
510	free(guard);
511	guard = tmp2;
512	return (guard);
513}
514/*
515 * Compile into an XDR header file
516 */
517
518static void
519h_output(infile, define, extend, outfile)
520	char   *infile;
521	char   *define;
522	int     extend;
523	char   *outfile;
524{
525	definition *def;
526	char   *outfilename;
527	long    tell;
528	char   *guard;
529	list   *l;
530
531	open_input(infile, define);
532	outfilename = extend ? extendfile(infile, outfile) : outfile;
533	open_output(infile, outfilename);
534	add_warning();
535	if (outfilename || infile)
536		guard = generate_guard(outfilename ? outfilename : infile);
537	else
538		guard = "STDIN_";
539
540	f_print(fout, "#ifndef _%s\n#define _%s\n\n", guard,
541	    guard);
542
543	f_print(fout, "#define RPCGEN_VERSION\t%s\n\n", RPCGEN_VERSION);
544	f_print(fout, "#include <rpc/rpc.h>\n\n");
545
546	tell = ftell(fout);
547	/* print data definitions */
548	while ((def = get_definition()) != NULL) {
549		print_datadef(def);
550	}
551
552	/* print function declarations.  Do this after data definitions
553	 * because they might be used as arguments for functions */
554	for (l = defined; l != NULL; l = l->next) {
555		print_funcdef(l->val);
556	}
557	if (extend && tell == ftell(fout)) {
558		(void) unlink(outfilename);
559	} else
560		if (tblflag) {
561			f_print(fout, rpcgen_table_dcl);
562		}
563	f_print(fout, "\n#endif /* !_%s */\n", guard);
564
565	free(guard);
566}
567/*
568 * Compile into an RPC service
569 */
570static void
571s_output(argc, argv, infile, define, extend, outfile, nomain, netflag)
572	int     argc;
573	char   *argv[];
574	char   *infile;
575	char   *define;
576	int     extend;
577	char   *outfile;
578	int     nomain;
579	int     netflag;
580{
581	char   *include;
582	definition *def;
583	int     foundprogram = 0;
584	char   *outfilename;
585
586	open_input(infile, define);
587	outfilename = extend ? extendfile(infile, outfile) : outfile;
588	open_output(infile, outfilename);
589	add_warning();
590	if (infile && (include = extendfile(infile, ".h"))) {
591		f_print(fout, "#include \"%s\"\n", include);
592		free(include);
593	} else
594		f_print(fout, "#include <rpc/rpc.h>\n");
595
596	f_print(fout, "#include <sys/ioctl.h>\n");
597	f_print(fout, "#include <fcntl.h>\n");
598	f_print(fout, "#include <stdio.h>\n");
599	f_print(fout, "#include <stdlib.h>\n");
600	if (Cflag) {
601		f_print(fout, "#include <unistd.h>\n");
602		f_print(fout,
603		    "#include <rpc/pmap_clnt.h>\n");
604		f_print(fout, "#include <string.h>\n");
605	}
606	f_print(fout, "#include <netdb.h>\n");
607	if (strcmp(svcclosetime, "-1") == 0)
608		indefinitewait = 1;
609	else
610		if (strcmp(svcclosetime, "0") == 0)
611			exitnow = 1;
612		else
613			if (inetdflag || pmflag) {
614				f_print(fout, "#include <signal.h>\n");
615				timerflag = 1;
616			}
617	if (!tirpcflag && inetdflag)
618		f_print(fout, "#include <sys/ttycom.h>\n");
619	if (Cflag && (inetdflag || pmflag)) {
620		f_print(fout, "#ifdef __cplusplus\n");
621		f_print(fout, "#include <sysent.h>\n");
622		f_print(fout, "#endif /* __cplusplus */\n");
623	}
624	if (tirpcflag)
625		f_print(fout, "#include <sys/types.h>\n");
626
627	f_print(fout, "#include <memory.h>\n");
628
629	if (inetdflag || !tirpcflag) {
630		f_print(fout, "#include <sys/socket.h>\n");
631		f_print(fout, "#include <netinet/in.h>\n");
632	}
633	if ((netflag || pmflag) && tirpcflag) {
634		f_print(fout, "#include <netconfig.h>\n");
635	}
636	if ( /* timerflag && */ tirpcflag)
637		f_print(fout, "#include <sys/resource.h>\n");
638	if (logflag || inetdflag || pmflag)
639		f_print(fout, "#include <syslog.h>\n");
640
641	/* for ANSI-C */
642	f_print(fout, "\n#ifdef __STDC__\n#define SIG_PF void(*)(int)\n#endif\n");
643
644	f_print(fout, "\n#ifdef DEBUG\n#define RPC_SVC_FG\n#endif\n");
645	if (timerflag)
646		f_print(fout, "\n#define _RPCSVC_CLOSEDOWN %s\n", svcclosetime);
647	while ((def = get_definition()) != NULL) {
648		foundprogram |= (def->def_kind == DEF_PROGRAM);
649	}
650	if (extend && !foundprogram) {
651		(void) unlink(outfilename);
652		return;
653	}
654	if (callerflag)		/* EVAS */
655		f_print(fout, "\nstatic SVCXPRT *caller;\n");	/* EVAS */
656	write_most(infile, netflag, nomain);
657	if (!nomain) {
658		if (!do_registers(argc, argv)) {
659			if (outfilename)
660				(void) unlink(outfilename);
661			usage();
662		}
663		write_rest();
664	}
665}
666/*
667 * generate client side stubs
668 */
669static void
670l_output(infile, define, extend, outfile)
671	char   *infile;
672	char   *define;
673	int     extend;
674	char   *outfile;
675{
676	char   *include;
677	definition *def;
678	int     foundprogram = 0;
679	char   *outfilename;
680
681	open_input(infile, define);
682	outfilename = extend ? extendfile(infile, outfile) : outfile;
683	open_output(infile, outfilename);
684	add_warning();
685	if (Cflag)
686		f_print(fout, "#include <memory.h>\n");
687	if (infile && (include = extendfile(infile, ".h"))) {
688		f_print(fout, "#include \"%s\"\n", include);
689		free(include);
690	} else
691		f_print(fout, "#include <rpc/rpc.h>\n");
692	while ((def = get_definition()) != NULL) {
693		foundprogram |= (def->def_kind == DEF_PROGRAM);
694	}
695	if (extend && !foundprogram) {
696		(void) unlink(outfilename);
697		return;
698	}
699	write_stubs();
700}
701/*
702 * generate the dispatch table
703 */
704static void
705t_output(infile, define, extend, outfile)
706	char   *infile;
707	char   *define;
708	int     extend;
709	char   *outfile;
710{
711	definition *def;
712	int     foundprogram = 0;
713	char   *outfilename;
714
715	open_input(infile, define);
716	outfilename = extend ? extendfile(infile, outfile) : outfile;
717	open_output(infile, outfilename);
718	add_warning();
719	while ((def = get_definition()) != NULL) {
720		foundprogram |= (def->def_kind == DEF_PROGRAM);
721	}
722	if (extend && !foundprogram) {
723		(void) unlink(outfilename);
724		return;
725	}
726	write_tables();
727}
728/* sample routine for the server template */
729static void
730svc_output(infile, define, extend, outfile)
731	char   *infile;
732	char   *define;
733	int     extend;
734	char   *outfile;
735{
736	definition *def;
737	char   *include;
738	char   *outfilename;
739	long    tell;
740
741	open_input(infile, define);
742	outfilename = extend ? extendfile(infile, outfile) : outfile;
743	checkfiles(infile, outfilename);	/* check if outfile already
744						 * exists. if so, print an
745						 * error message and exit */
746	open_output(infile, outfilename);
747	add_sample_msg();
748
749	if (infile && (include = extendfile(infile, ".h"))) {
750		f_print(fout, "#include \"%s\"\n", include);
751		free(include);
752	} else
753		f_print(fout, "#include <rpc/rpc.h>\n");
754
755	tell = ftell(fout);
756	while ((def = get_definition()) != NULL) {
757		write_sample_svc(def);
758	}
759	if (extend && tell == ftell(fout)) {
760		(void) unlink(outfilename);
761	}
762}
763
764
765/* sample main routine for client */
766static void
767clnt_output(infile, define, extend, outfile)
768	char   *infile;
769	char   *define;
770	int     extend;
771	char   *outfile;
772{
773	definition *def;
774	char   *include;
775	char   *outfilename;
776	long    tell;
777	int     has_program = 0;
778
779	open_input(infile, define);
780	outfilename = extend ? extendfile(infile, outfile) : outfile;
781	checkfiles(infile, outfilename);	/* check if outfile already
782						 * exists. if so, print an
783						 * error message and exit */
784
785	open_output(infile, outfilename);
786	add_sample_msg();
787	if (Cflag)
788		f_print(fout, "#include <stdio.h>\n");
789	if (infile && (include = extendfile(infile, ".h"))) {
790		f_print(fout, "#include \"%s\"\n", include);
791		free(include);
792	} else
793		f_print(fout, "#include <rpc/rpc.h>\n");
794	tell = ftell(fout);
795	while ((def = get_definition()) != NULL) {
796		has_program += write_sample_clnt(def);
797	}
798
799	if (has_program)
800		write_sample_clnt_main();
801
802	if (extend && tell == ftell(fout)) {
803		(void) unlink(outfilename);
804	}
805}
806/*
807 * Perform registrations for service output
808 * Return 0 if failed; 1 otherwise.
809 */
810static int
811do_registers(argc, argv)
812	int     argc;
813	char   *argv[];
814{
815	int     i;
816
817	if (inetdflag || !tirpcflag) {
818		for (i = 1; i < argc; i++) {
819			if (streq(argv[i], "-s")) {
820				if (!check_nettype(argv[i + 1], valid_i_nettypes))
821					return 0;
822				write_inetd_register(argv[i + 1]);
823				i++;
824			}
825		}
826	} else {
827		for (i = 1; i < argc; i++)
828			if (streq(argv[i], "-s")) {
829				if (!check_nettype(argv[i + 1], valid_ti_nettypes))
830					return 0;
831				write_nettype_register(argv[i + 1]);
832				i++;
833			} else
834				if (streq(argv[i], "-n")) {
835					write_netid_register(argv[i + 1]);
836					i++;
837				}
838	}
839	return 1;
840}
841/*
842 * Add another argument to the arg list
843 */
844static void
845addarg(cp)
846	char   *cp;
847{
848	if (argcount >= ARGLISTLEN) {
849		f_print(stderr, "rpcgen: too many defines\n");
850		crash();
851		/* NOTREACHED */
852	}
853	arglist[argcount++] = cp;
854
855}
856
857static void
858putarg(pwhere, cp)
859	char   *cp;
860	int     pwhere;
861{
862	if (pwhere >= ARGLISTLEN) {
863		f_print(stderr, "rpcgen: arglist coding error\n");
864		crash();
865		/* NOTREACHED */
866	}
867	arglist[pwhere] = cp;
868
869}
870/*
871 * if input file is stdin and an output file is specified then complain
872 * if the file already exists. Otherwise the file may get overwritten
873 * If input file does not exist, exit with an error
874 */
875
876static void
877checkfiles(infile, outfile)
878	char   *infile;
879	char   *outfile;
880{
881
882	struct stat buf;
883
884	if (infile)		/* infile ! = NULL */
885		if (stat(infile, &buf) < 0) {
886			perror(infile);
887			crash();
888		};
889#if 0
890	if (outfile) {
891		if (stat(outfile, &buf) < 0)
892			return;	/* file does not exist */
893		else {
894			f_print(stderr,
895			    "file '%s' already exists and may be overwritten\n", outfile);
896			crash();
897		}
898	}
899#endif
900}
901/*
902 * Parse command line arguments
903 */
904static int
905parseargs(argc, argv, cmd)
906	int     argc;
907	char   *argv[];
908	struct commandline *cmd;
909{
910	int     i;
911	int     j;
912	int     c;
913	char    flag[1 << CHAR_BIT];
914	int     nflags;
915
916	cmdname = argv[0];
917	cmd->infile = cmd->outfile = NULL;
918	if (argc < 2) {
919		return (0);
920	}
921	allfiles = 0;
922	flag['c'] = 0;
923	flag['h'] = 0;
924	flag['l'] = 0;
925	flag['m'] = 0;
926	flag['o'] = 0;
927	flag['s'] = 0;
928	flag['n'] = 0;
929	flag['t'] = 0;
930	flag['S'] = 0;
931	flag['C'] = 0;
932	for (i = 1; i < argc; i++) {
933		if (argv[i][0] != '-') {
934			if (cmd->infile) {
935				f_print(stderr, "Cannot specify more than one input file!\n");
936
937				return (0);
938			}
939			cmd->infile = argv[i];
940		} else {
941			for (j = 1; argv[i][j] != 0; j++) {
942				c = argv[i][j];
943				switch (c) {
944				case 'A':
945					callerflag = 1;
946					break;
947				case 'a':
948					allfiles = 1;
949					break;
950				case 'c':
951				case 'h':
952				case 'l':
953				case 'm':
954				case 't':
955					if (flag[c]) {
956						return (0);
957					}
958					flag[c] = 1;
959					break;
960				case 'S':
961					/* sample flag: Ss or Sc. Ss means set
962					 * flag['S']; Sc means set flag['C']; */
963					c = argv[i][++j];	/* get next char */
964					if (c == 's')
965						c = 'S';
966					else
967						if (c == 'c')
968							c = 'C';
969						else
970							return (0);
971
972					if (flag[c]) {
973						return (0);
974					}
975					flag[c] = 1;
976					break;
977				case 'C':	/* ANSI C syntax */
978					Cflag = 1;
979					break;
980
981				case 'b':	/* turn TIRPC flag off for
982						 * generating backward
983						 * compatible */
984					tirpcflag = 0;
985					break;
986
987				case 'I':
988					inetdflag = 1;
989					break;
990				case 'M':
991					Mflag = 1;
992					break;
993				case 'N':
994					newstyle = 1;
995					break;
996				case 'L':
997					logflag = 1;
998					break;
999				case 'K':
1000					if (++i == argc) {
1001						return (0);
1002					}
1003					svcclosetime = argv[i];
1004					goto nextarg;
1005				case 'T':
1006					tblflag = 1;
1007					break;
1008				case 'i':
1009					if (++i == argc) {
1010						return (0);
1011					}
1012					doinline = atoi(argv[i]);
1013					goto nextarg;
1014				case 'n':
1015				case 'o':
1016				case 's':
1017					if (argv[i][j - 1] != '-' ||
1018					    argv[i][j + 1] != 0) {
1019						return (0);
1020					}
1021					flag[c] = 1;
1022					if (++i == argc) {
1023						return (0);
1024					}
1025					if (c == 's') {
1026						if (!streq(argv[i], "udp") &&
1027						    !streq(argv[i], "tcp")) {
1028							return (0);
1029						}
1030					} else
1031						if (c == 'o') {
1032							if (cmd->outfile) {
1033								return (0);
1034							}
1035							cmd->outfile = argv[i];
1036						}
1037					goto nextarg;
1038				case 'D':
1039					if (argv[i][j - 1] != '-') {
1040						return (0);
1041					}
1042					(void) addarg(argv[i]);
1043					goto nextarg;
1044				case 'Y':
1045					if (++i == argc) {
1046						return (0);
1047					}
1048					(void) strlcpy(pathbuf, argv[i],
1049					    sizeof(pathbuf));
1050					(void) strlcat(pathbuf, "/cpp",
1051					    sizeof(pathbuf));
1052					CPP = pathbuf;
1053					goto nextarg;
1054
1055				case 'v':
1056					printf("version 1.0\n");
1057					exit(0);
1058
1059				default:
1060					return (0);
1061				}
1062			}
1063	nextarg:
1064			;
1065		}
1066	}
1067
1068	cmd->cflag = flag['c'];
1069	cmd->hflag = flag['h'];
1070	cmd->lflag = flag['l'];
1071	cmd->mflag = flag['m'];
1072	cmd->nflag = flag['n'];
1073	cmd->sflag = flag['s'];
1074	cmd->tflag = flag['t'];
1075	cmd->Ssflag = flag['S'];
1076	cmd->Scflag = flag['C'];
1077
1078	if (tirpcflag) {
1079		pmflag = inetdflag ? 0 : 1;	/* pmflag or inetdflag is
1080						 * always TRUE */
1081		if ((inetdflag && cmd->nflag)) {	/* netid not allowed
1082							 * with inetdflag */
1083			f_print(stderr, "Cannot use netid flag with inetd flag!\n");
1084			return (0);
1085		}
1086	} else {		/* 4.1 mode */
1087		pmflag = 0;	/* set pmflag only in tirpcmode */
1088		inetdflag = 1;	/* inetdflag is TRUE by default */
1089		if (cmd->nflag) {	/* netid needs TIRPC */
1090			f_print(stderr, "Cannot use netid flag without TIRPC!\n");
1091			return (0);
1092		}
1093	}
1094
1095	if (newstyle && (tblflag || cmd->tflag)) {
1096		f_print(stderr, "Cannot use table flags with newstyle!\n");
1097		return (0);
1098	}
1099	/* check no conflicts with file generation flags */
1100	nflags = cmd->cflag + cmd->hflag + cmd->lflag + cmd->mflag +
1101	    cmd->sflag + cmd->nflag + cmd->tflag + cmd->Ssflag + cmd->Scflag;
1102
1103	if (nflags == 0) {
1104		if (cmd->outfile != NULL || cmd->infile == NULL) {
1105			return (0);
1106		}
1107	} else
1108		if (nflags > 1) {
1109			f_print(stderr, "Cannot have more than one file generation flag!\n");
1110			return (0);
1111		}
1112	return (1);
1113}
1114
1115static void
1116usage()
1117{
1118	f_print(stderr, "usage:  %s infile\n", cmdname);
1119	f_print(stderr, "\t%s [-a][-b][-C][-Dname[=value]] -i size [-I [-K seconds]] [-A] [-M] [-N] [-T] infile\n",
1120	    cmdname);
1121	f_print(stderr, "\t%s [-L] [-M] [-c | -h | -l | -m | -t | -Sc | -Ss] [-o outfile] [infile]\n",
1122	    cmdname);
1123	f_print(stderr, "\t%s [-s nettype]* [-o outfile] [infile]\n", cmdname);
1124	f_print(stderr, "\t%s [-n netid]* [-o outfile] [infile]\n", cmdname);
1125	options_usage();
1126	exit(1);
1127}
1128
1129static void
1130options_usage()
1131{
1132	f_print(stderr, "options:\n");
1133	f_print(stderr, "-A\t\tgenerate svc_caller() function\n");
1134	f_print(stderr, "-a\t\tgenerate all files, including samples\n");
1135	f_print(stderr, "-b\t\tbackward compatibility mode (generates code for SunOS 4.1)\n");
1136	f_print(stderr, "-c\t\tgenerate XDR routines\n");
1137	f_print(stderr, "-C\t\tANSI C mode\n");
1138	f_print(stderr, "-Dname[=value]\tdefine a symbol (same as #define)\n");
1139	f_print(stderr, "-h\t\tgenerate header file\n");
1140	f_print(stderr, "-i size\t\tsize at which to start generating inline code\n");
1141	f_print(stderr, "-I\t\tgenerate code for inetd support in server (for SunOS 4.1)\n");
1142	f_print(stderr, "-K seconds\tserver exits after K seconds of inactivity\n");
1143	f_print(stderr, "-l\t\tgenerate client side stubs\n");
1144	f_print(stderr, "-L\t\tserver errors will be printed to syslog\n");
1145	f_print(stderr, "-m\t\tgenerate server side stubs\n");
1146	f_print(stderr, "-M\t\tgenerate thread-safe stubs\n");
1147	f_print(stderr, "-n netid\tgenerate server code that supports named netid\n");
1148	f_print(stderr, "-N\t\tsupports multiple arguments and call-by-value\n");
1149	f_print(stderr, "-o outfile\tname of the output file\n");
1150	f_print(stderr, "-s nettype\tgenerate server code that supports named nettype\n");
1151	f_print(stderr, "-Sc\t\tgenerate sample client code that uses remote procedures\n");
1152	f_print(stderr, "-Ss\t\tgenerate sample server code that defines remote procedures\n");
1153	f_print(stderr, "-t\t\tgenerate RPC dispatch table\n");
1154	f_print(stderr, "-T\t\tgenerate code to support RPC dispatch tables\n");
1155	f_print(stderr, "-Y path\t\tdirectory name to find C preprocessor (cpp)\n");
1156
1157	exit(1);
1158}
1159