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