xref: /netbsd-src/usr.bin/rpcgen/rpc_main.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: rpc_main.c,v 1.27 2004/11/01 21:39:32 dsl 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
40 static char sccsid[] = "@(#)rpc_main.c 1.30 89/03/30 (C) 1987 SMI";
41 #else
42 __RCSID("$NetBSD: rpc_main.c,v 1.27 2004/11/01 21:39:32 dsl 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 
69 struct 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 
84 static char *cmdname;
85 
86 static char *svcclosetime = "120";
87 static char *CPP;
88 static char CPPFLAGS[] = "-C";
89 static char pathbuf[MAXPATHLEN + 1];
90 static char *allv[] = {
91 	"rpcgen", "-s", "udp", "-s", "tcp",
92 };
93 static int allc = sizeof(allv) / sizeof(allv[0]);
94 static char *allnv[] = {
95 	"rpcgen", "-s", "netpath",
96 };
97 static int allnc = sizeof(allnv) / sizeof(allnv[0]);
98 
99 #define ARGLISTLEN	20
100 #define FIXEDARGS         2
101 
102 static char *arglist[ARGLISTLEN];
103 static int argcount = FIXEDARGS;
104 
105 
106 int     nonfatalerrors;		/* errors */
107 int     inetdflag /* = 1 */ ;	/* Support for inetd *//* is now the default */
108 int     pmflag;			/* Support for port monitors */
109 int     logflag;		/* Use syslog instead of fprintf for errors */
110 int     tblflag;		/* Support for dispatch table file */
111 int     callerflag;		/* Generate svc_caller() function */
112 
113 #define INLINE 3
114 /*length at which to start doing an inline */
115 
116 int     doinline = INLINE;	/* length at which to start doing an inline. 3
117 				 * = default if 0, no xdr_inline code */
118 
119 int     indefinitewait;		/* If started by port monitors, hang till it
120 				 * wants */
121 int     exitnow;		/* If started by port monitors, exit after the
122 				 * call */
123 int     timerflag;		/* TRUE if !indefinite && !exitnow */
124 int     newstyle;		/* newstyle of passing arguments (by value) */
125 int     Cflag = 0;		/* ANSI C syntax */
126 int	Mflag = 0;		/* multithread safe */
127 static int allfiles;		/* generate all files */
128 int     tirpcflag = 1;		/* generating code for tirpc, by default */
129 
130 #ifdef __MSDOS__
131 static char *dos_cppfile = NULL;
132 #endif
133 
134 int main __P((int, char *[]));
135 
136 static char *extendfile __P((char *, char *));
137 static void open_output __P((char *, char *));
138 static void add_warning __P((void));
139 static void clear_args __P((void));
140 static void open_input __P((char *, char *));
141 static int check_nettype __P((char *, char *[]));
142 static void c_output __P((char *, char *, int, char *));
143 static void c_initialize __P((void));
144 static char *generate_guard __P((char *));
145 static void h_output __P((char *, char *, int, char *));
146 static void s_output __P((int, char *[], char *, char *, int, char *, int, int));
147 static void l_output __P((char *, char *, int, char *));
148 static void t_output __P((char *, char *, int, char *));
149 static void svc_output __P((char *, char *, int, char *));
150 static void clnt_output __P((char *, char *, int, char *));
151 static int do_registers __P((int, char *[]));
152 static void addarg __P((char *));
153 static void putarg __P((int, char *));
154 static void checkfiles __P((char *, char *));
155 static int parseargs __P((int, char *[], struct commandline *));
156 static void usage __P((void));
157 static void options_usage __P((void));
158 
159 int
160 main(argc, argv)
161 	int     argc;
162 	char   *argv[];
163 {
164 	struct commandline cmd;
165 
166 	setprogname(argv[0]);
167 	if (!(CPP = getenv("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  */
246 static char *
247 extendfile(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  */
275 static void
276 open_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 
300 static void
301 add_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 */
309 static void
310 clear_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  */
321 static void
322 open_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 = searchpath("cpp.exe")) == NULL
338 		    && (cpp = getenv("RPCGENCPP")) == 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((char *) NULL);
377 		(void) close(1);
378 		(void) dup2(pd[1], 1);
379 		(void) close(pd[0]);
380 		execvp(arglist[0], arglist);
381 		err(1, "$CPP: %s", CPP);
382 	case -1:
383 		perror("fork");
384 		exit(1);
385 	}
386 	(void) close(pd[1]);
387 	fin = fdopen(pd[0], "r");
388 #endif
389 	if (fin == NULL) {
390 		f_print(stderr, "%s: ", cmdname);
391 		perror(infilename);
392 		crash();
393 	}
394 }
395 /* valid tirpc nettypes */
396 static char *valid_ti_nettypes[] =
397 {
398 	"netpath",
399 	"visible",
400 	"circuit_v",
401 	"datagram_v",
402 	"circuit_n",
403 	"datagram_n",
404 	"udp",
405 	"tcp",
406 	"raw",
407 	NULL
408 };
409 /* valid inetd nettypes */
410 static char *valid_i_nettypes[] =
411 {
412 	"udp",
413 	"tcp",
414 	NULL
415 };
416 
417 static int
418 check_nettype(name, list_to_check)
419 	char   *name;
420 	char   *list_to_check[];
421 {
422 	int     i;
423 	for (i = 0; list_to_check[i] != NULL; i++) {
424 		if (strcmp(name, list_to_check[i]) == 0) {
425 			return 1;
426 		}
427 	}
428 	f_print(stderr, "illegal nettype :\'%s\'\n", name);
429 	return 0;
430 }
431 /*
432  * Compile into an XDR routine output file
433  */
434 
435 static void
436 c_output(infile, define, extend, outfile)
437 	char   *infile;
438 	char   *define;
439 	int     extend;
440 	char   *outfile;
441 {
442 	definition *def;
443 	char   *include;
444 	char   *outfilename;
445 	long    tell;
446 
447 	c_initialize();
448 	open_input(infile, define);
449 	outfilename = extend ? extendfile(infile, outfile) : outfile;
450 	open_output(infile, outfilename);
451 	add_warning();
452 	if (infile && (include = extendfile(infile, ".h"))) {
453 		f_print(fout, "#include \"%s\"\n", include);
454 		free(include);
455 		/* .h file already contains rpc/rpc.h */
456 	} else
457 		f_print(fout, "#include <rpc/rpc.h>\n");
458 	tell = ftell(fout);
459 	while ((def = get_definition()) != NULL) {
460 		emit(def);
461 	}
462 	if (extend && tell == ftell(fout)) {
463 		(void) unlink(outfilename);
464 	}
465 }
466 
467 
468 static void
469 c_initialize()
470 {
471 
472 	/* add all the starting basic types */
473 
474 	add_type(1, "int");
475 	add_type(1, "long");
476 	add_type(1, "short");
477 	add_type(1, "bool");
478 
479 	add_type(1, "u_int");
480 	add_type(1, "u_long");
481 	add_type(1, "u_short");
482 
483 }
484 
485 const char    rpcgen_table_dcl[] = "struct rpcgen_table {\n\
486 	char	*(*proc)();\n\
487 	xdrproc_t	xdr_arg;\n\
488 	unsigned	len_arg;\n\
489 	xdrproc_t	xdr_res;\n\
490 	unsigned	len_res;\n\
491 };\n";
492 
493 
494 static char *
495 generate_guard(pathname)
496 	char   *pathname;
497 {
498 	char   *filename, *guard, *tmp;
499 
500 	filename = strrchr(pathname, '/');	/* find last component */
501 	filename = ((filename == 0) ? pathname : filename + 1);
502 	guard = strdup(filename);
503 	/* convert to upper case */
504 	tmp = guard;
505 	while (*tmp) {
506 		*tmp = toupper((unsigned char)*tmp);
507 		tmp++;
508 	}
509 
510 	guard = extendfile(guard, "_H_RPCGEN");
511 	return (guard);
512 }
513 /*
514  * Compile into an XDR header file
515  */
516 
517 static void
518 h_output(infile, define, extend, outfile)
519 	char   *infile;
520 	char   *define;
521 	int     extend;
522 	char   *outfile;
523 {
524 	definition *def;
525 	char   *outfilename;
526 	long    tell;
527 	char   *guard;
528 	list   *l;
529 
530 	open_input(infile, define);
531 	outfilename = extend ? extendfile(infile, outfile) : outfile;
532 	open_output(infile, outfilename);
533 	add_warning();
534 	guard = generate_guard(outfilename ? outfilename : infile);
535 
536 	f_print(fout, "#ifndef _%s\n#define _%s\n\n", guard,
537 	    guard);
538 
539 	f_print(fout, "#define RPCGEN_VERSION\t%s\n\n", RPCGEN_VERSION);
540 	f_print(fout, "#include <rpc/rpc.h>\n\n");
541 
542 	tell = ftell(fout);
543 	/* print data definitions */
544 	while ((def = get_definition()) != NULL) {
545 		print_datadef(def);
546 	}
547 
548 	/* print function declarations.  Do this after data definitions
549 	 * because they might be used as arguments for functions */
550 	for (l = defined; l != NULL; l = l->next) {
551 		print_funcdef(l->val);
552 	}
553 	if (extend && tell == ftell(fout)) {
554 		(void) unlink(outfilename);
555 	} else
556 		if (tblflag) {
557 			f_print(fout, rpcgen_table_dcl);
558 		}
559 	f_print(fout, "\n#endif /* !_%s */\n", guard);
560 }
561 /*
562  * Compile into an RPC service
563  */
564 static void
565 s_output(argc, argv, infile, define, extend, outfile, nomain, netflag)
566 	int     argc;
567 	char   *argv[];
568 	char   *infile;
569 	char   *define;
570 	int     extend;
571 	char   *outfile;
572 	int     nomain;
573 	int     netflag;
574 {
575 	char   *include;
576 	definition *def;
577 	int     foundprogram = 0;
578 	char   *outfilename;
579 
580 	open_input(infile, define);
581 	outfilename = extend ? extendfile(infile, outfile) : outfile;
582 	open_output(infile, outfilename);
583 	add_warning();
584 	if (infile && (include = extendfile(infile, ".h"))) {
585 		f_print(fout, "#include \"%s\"\n", include);
586 		free(include);
587 	} else
588 		f_print(fout, "#include <rpc/rpc.h>\n");
589 
590 	f_print(fout, "#include <sys/ioctl.h>\n");
591 	f_print(fout, "#include <fcntl.h>\n");
592 	f_print(fout, "#include <stdio.h>\n");
593 	f_print(fout, "#include <stdlib.h>\n");
594 	if (Cflag) {
595 		f_print(fout, "#include <unistd.h>\n");
596 		f_print(fout,
597 		    "#include <rpc/pmap_clnt.h>\n");
598 		f_print(fout, "#include <string.h>\n");
599 	}
600 	f_print(fout, "#include <netdb.h>\n");
601 	if (strcmp(svcclosetime, "-1") == 0)
602 		indefinitewait = 1;
603 	else
604 		if (strcmp(svcclosetime, "0") == 0)
605 			exitnow = 1;
606 		else
607 			if (inetdflag || pmflag) {
608 				f_print(fout, "#include <signal.h>\n");
609 				timerflag = 1;
610 			}
611 	if (!tirpcflag && inetdflag)
612 		f_print(fout, "#include <sys/ttycom.h>\n");
613 	if (Cflag && (inetdflag || pmflag)) {
614 		f_print(fout, "#ifdef __cplusplus\n");
615 		f_print(fout, "#include <sysent.h>\n");
616 		f_print(fout, "#endif /* __cplusplus */\n");
617 	}
618 	if (tirpcflag)
619 		f_print(fout, "#include <sys/types.h>\n");
620 
621 	f_print(fout, "#include <memory.h>\n");
622 
623 	if (inetdflag || !tirpcflag) {
624 		f_print(fout, "#include <sys/socket.h>\n");
625 		f_print(fout, "#include <netinet/in.h>\n");
626 	}
627 	if ((netflag || pmflag) && tirpcflag) {
628 		f_print(fout, "#include <netconfig.h>\n");
629 	}
630 	if ( /* timerflag && */ tirpcflag)
631 		f_print(fout, "#include <sys/resource.h>\n");
632 	if (logflag || inetdflag || pmflag)
633 		f_print(fout, "#include <syslog.h>\n");
634 
635 	/* for ANSI-C */
636 	f_print(fout, "\n#ifdef __STDC__\n#define SIG_PF void(*)(int)\n#endif\n");
637 
638 	f_print(fout, "\n#ifdef DEBUG\n#define RPC_SVC_FG\n#endif\n");
639 	if (timerflag)
640 		f_print(fout, "\n#define _RPCSVC_CLOSEDOWN %s\n", svcclosetime);
641 	while ((def = get_definition()) != NULL) {
642 		foundprogram |= (def->def_kind == DEF_PROGRAM);
643 	}
644 	if (extend && !foundprogram) {
645 		(void) unlink(outfilename);
646 		return;
647 	}
648 	if (callerflag)		/* EVAS */
649 		f_print(fout, "\nstatic SVCXPRT *caller;\n");	/* EVAS */
650 	write_most(infile, netflag, nomain);
651 	if (!nomain) {
652 		if (!do_registers(argc, argv)) {
653 			if (outfilename)
654 				(void) unlink(outfilename);
655 			usage();
656 		}
657 		write_rest();
658 	}
659 }
660 /*
661  * generate client side stubs
662  */
663 static void
664 l_output(infile, define, extend, outfile)
665 	char   *infile;
666 	char   *define;
667 	int     extend;
668 	char   *outfile;
669 {
670 	char   *include;
671 	definition *def;
672 	int     foundprogram = 0;
673 	char   *outfilename;
674 
675 	open_input(infile, define);
676 	outfilename = extend ? extendfile(infile, outfile) : outfile;
677 	open_output(infile, outfilename);
678 	add_warning();
679 	if (Cflag)
680 		f_print(fout, "#include <memory.h>\n");
681 	if (infile && (include = extendfile(infile, ".h"))) {
682 		f_print(fout, "#include \"%s\"\n", include);
683 		free(include);
684 	} else
685 		f_print(fout, "#include <rpc/rpc.h>\n");
686 	while ((def = get_definition()) != NULL) {
687 		foundprogram |= (def->def_kind == DEF_PROGRAM);
688 	}
689 	if (extend && !foundprogram) {
690 		(void) unlink(outfilename);
691 		return;
692 	}
693 	write_stubs();
694 }
695 /*
696  * generate the dispatch table
697  */
698 static void
699 t_output(infile, define, extend, outfile)
700 	char   *infile;
701 	char   *define;
702 	int     extend;
703 	char   *outfile;
704 {
705 	definition *def;
706 	int     foundprogram = 0;
707 	char   *outfilename;
708 
709 	open_input(infile, define);
710 	outfilename = extend ? extendfile(infile, outfile) : outfile;
711 	open_output(infile, outfilename);
712 	add_warning();
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_tables();
721 }
722 /* sample routine for the server template */
723 static void
724 svc_output(infile, define, extend, outfile)
725 	char   *infile;
726 	char   *define;
727 	int     extend;
728 	char   *outfile;
729 {
730 	definition *def;
731 	char   *include;
732 	char   *outfilename;
733 	long    tell;
734 
735 	open_input(infile, define);
736 	outfilename = extend ? extendfile(infile, outfile) : outfile;
737 	checkfiles(infile, outfilename);	/* check if outfile already
738 						 * exists. if so, print an
739 						 * error message and exit */
740 	open_output(infile, outfilename);
741 	add_sample_msg();
742 
743 	if (infile && (include = extendfile(infile, ".h"))) {
744 		f_print(fout, "#include \"%s\"\n", include);
745 		free(include);
746 	} else
747 		f_print(fout, "#include <rpc/rpc.h>\n");
748 
749 	tell = ftell(fout);
750 	while ((def = get_definition()) != NULL) {
751 		write_sample_svc(def);
752 	}
753 	if (extend && tell == ftell(fout)) {
754 		(void) unlink(outfilename);
755 	}
756 }
757 
758 
759 /* sample main routine for client */
760 static void
761 clnt_output(infile, define, extend, outfile)
762 	char   *infile;
763 	char   *define;
764 	int     extend;
765 	char   *outfile;
766 {
767 	definition *def;
768 	char   *include;
769 	char   *outfilename;
770 	long    tell;
771 	int     has_program = 0;
772 
773 	open_input(infile, define);
774 	outfilename = extend ? extendfile(infile, outfile) : outfile;
775 	checkfiles(infile, outfilename);	/* check if outfile already
776 						 * exists. if so, print an
777 						 * error message and exit */
778 
779 	open_output(infile, outfilename);
780 	add_sample_msg();
781 	if (Cflag)
782 		f_print(fout, "#include <stdio.h>\n");
783 	if (infile && (include = extendfile(infile, ".h"))) {
784 		f_print(fout, "#include \"%s\"\n", include);
785 		free(include);
786 	} else
787 		f_print(fout, "#include <rpc/rpc.h>\n");
788 	tell = ftell(fout);
789 	while ((def = get_definition()) != NULL) {
790 		has_program += write_sample_clnt(def);
791 	}
792 
793 	if (has_program)
794 		write_sample_clnt_main();
795 
796 	if (extend && tell == ftell(fout)) {
797 		(void) unlink(outfilename);
798 	}
799 }
800 /*
801  * Perform registrations for service output
802  * Return 0 if failed; 1 otherwise.
803  */
804 static int
805 do_registers(argc, argv)
806 	int     argc;
807 	char   *argv[];
808 {
809 	int     i;
810 
811 	if (inetdflag || !tirpcflag) {
812 		for (i = 1; i < argc; i++) {
813 			if (streq(argv[i], "-s")) {
814 				if (!check_nettype(argv[i + 1], valid_i_nettypes))
815 					return 0;
816 				write_inetd_register(argv[i + 1]);
817 				i++;
818 			}
819 		}
820 	} else {
821 		for (i = 1; i < argc; i++)
822 			if (streq(argv[i], "-s")) {
823 				if (!check_nettype(argv[i + 1], valid_ti_nettypes))
824 					return 0;
825 				write_nettype_register(argv[i + 1]);
826 				i++;
827 			} else
828 				if (streq(argv[i], "-n")) {
829 					write_netid_register(argv[i + 1]);
830 					i++;
831 				}
832 	}
833 	return 1;
834 }
835 /*
836  * Add another argument to the arg list
837  */
838 static void
839 addarg(cp)
840 	char   *cp;
841 {
842 	if (argcount >= ARGLISTLEN) {
843 		f_print(stderr, "rpcgen: too many defines\n");
844 		crash();
845 		/* NOTREACHED */
846 	}
847 	arglist[argcount++] = cp;
848 
849 }
850 
851 static void
852 putarg(where, cp)
853 	char   *cp;
854 	int     where;
855 {
856 	if (where >= ARGLISTLEN) {
857 		f_print(stderr, "rpcgen: arglist coding error\n");
858 		crash();
859 		/* NOTREACHED */
860 	}
861 	arglist[where] = cp;
862 
863 }
864 /*
865  * if input file is stdin and an output file is specified then complain
866  * if the file already exists. Otherwise the file may get overwritten
867  * If input file does not exist, exit with an error
868  */
869 
870 static void
871 checkfiles(infile, outfile)
872 	char   *infile;
873 	char   *outfile;
874 {
875 
876 	struct stat buf;
877 
878 	if (infile)		/* infile ! = NULL */
879 		if (stat(infile, &buf) < 0) {
880 			perror(infile);
881 			crash();
882 		};
883 #if 0
884 	if (outfile) {
885 		if (stat(outfile, &buf) < 0)
886 			return;	/* file does not exist */
887 		else {
888 			f_print(stderr,
889 			    "file '%s' already exists and may be overwritten\n", outfile);
890 			crash();
891 		}
892 	}
893 #endif
894 }
895 /*
896  * Parse command line arguments
897  */
898 static int
899 parseargs(argc, argv, cmd)
900 	int     argc;
901 	char   *argv[];
902 	struct commandline *cmd;
903 {
904 	int     i;
905 	int     j;
906 	int     c;
907 	char    flag[(1 << 8 * sizeof(char))];
908 	int     nflags;
909 
910 	cmdname = argv[0];
911 	cmd->infile = cmd->outfile = NULL;
912 	if (argc < 2) {
913 		return (0);
914 	}
915 	allfiles = 0;
916 	flag['c'] = 0;
917 	flag['h'] = 0;
918 	flag['l'] = 0;
919 	flag['m'] = 0;
920 	flag['o'] = 0;
921 	flag['s'] = 0;
922 	flag['n'] = 0;
923 	flag['t'] = 0;
924 	flag['S'] = 0;
925 	flag['C'] = 0;
926 	for (i = 1; i < argc; i++) {
927 		if (argv[i][0] != '-') {
928 			if (cmd->infile) {
929 				f_print(stderr, "Cannot specify more than one input file!\n");
930 
931 				return (0);
932 			}
933 			cmd->infile = argv[i];
934 		} else {
935 			for (j = 1; argv[i][j] != 0; j++) {
936 				c = argv[i][j];
937 				switch (c) {
938 				case 'A':
939 					callerflag = 1;
940 					break;
941 				case 'a':
942 					allfiles = 1;
943 					break;
944 				case 'c':
945 				case 'h':
946 				case 'l':
947 				case 'm':
948 				case 't':
949 					if (flag[c]) {
950 						return (0);
951 					}
952 					flag[c] = 1;
953 					break;
954 				case 'S':
955 					/* sample flag: Ss or Sc. Ss means set
956 					 * flag['S']; Sc means set flag['C']; */
957 					c = argv[i][++j];	/* get next char */
958 					if (c == 's')
959 						c = 'S';
960 					else
961 						if (c == 'c')
962 							c = 'C';
963 						else
964 							return (0);
965 
966 					if (flag[c]) {
967 						return (0);
968 					}
969 					flag[c] = 1;
970 					break;
971 				case 'C':	/* ANSI C syntax */
972 					Cflag = 1;
973 					break;
974 
975 				case 'b':	/* turn TIRPC flag off for
976 						 * generating backward
977 						 * compatible */
978 					tirpcflag = 0;
979 					break;
980 
981 				case 'I':
982 					inetdflag = 1;
983 					break;
984 				case 'M':
985 					Mflag = 1;
986 					break;
987 				case 'N':
988 					newstyle = 1;
989 					break;
990 				case 'L':
991 					logflag = 1;
992 					break;
993 				case 'K':
994 					if (++i == argc) {
995 						return (0);
996 					}
997 					svcclosetime = argv[i];
998 					goto nextarg;
999 				case 'T':
1000 					tblflag = 1;
1001 					break;
1002 				case 'i':
1003 					if (++i == argc) {
1004 						return (0);
1005 					}
1006 					doinline = atoi(argv[i]);
1007 					goto nextarg;
1008 				case 'n':
1009 				case 'o':
1010 				case 's':
1011 					if (argv[i][j - 1] != '-' ||
1012 					    argv[i][j + 1] != 0) {
1013 						return (0);
1014 					}
1015 					flag[c] = 1;
1016 					if (++i == argc) {
1017 						return (0);
1018 					}
1019 					if (c == 's') {
1020 						if (!streq(argv[i], "udp") &&
1021 						    !streq(argv[i], "tcp")) {
1022 							return (0);
1023 						}
1024 					} else
1025 						if (c == 'o') {
1026 							if (cmd->outfile) {
1027 								return (0);
1028 							}
1029 							cmd->outfile = argv[i];
1030 						}
1031 					goto nextarg;
1032 				case 'D':
1033 					if (argv[i][j - 1] != '-') {
1034 						return (0);
1035 					}
1036 					(void) addarg(argv[i]);
1037 					goto nextarg;
1038 				case 'Y':
1039 					if (++i == argc) {
1040 						return (0);
1041 					}
1042 					(void) strlcpy(pathbuf, argv[i],
1043 					    sizeof(pathbuf));
1044 					(void) strlcat(pathbuf, "/cpp",
1045 					    sizeof(pathbuf));
1046 					CPP = pathbuf;
1047 					goto nextarg;
1048 
1049 				case 'v':
1050 					printf("version 1.0\n");
1051 					exit(0);
1052 
1053 				default:
1054 					return (0);
1055 				}
1056 			}
1057 	nextarg:
1058 			;
1059 		}
1060 	}
1061 
1062 	cmd->cflag = flag['c'];
1063 	cmd->hflag = flag['h'];
1064 	cmd->lflag = flag['l'];
1065 	cmd->mflag = flag['m'];
1066 	cmd->nflag = flag['n'];
1067 	cmd->sflag = flag['s'];
1068 	cmd->tflag = flag['t'];
1069 	cmd->Ssflag = flag['S'];
1070 	cmd->Scflag = flag['C'];
1071 
1072 	if (tirpcflag) {
1073 		pmflag = inetdflag ? 0 : 1;	/* pmflag or inetdflag is
1074 						 * always TRUE */
1075 		if ((inetdflag && cmd->nflag)) {	/* netid not allowed
1076 							 * with inetdflag */
1077 			f_print(stderr, "Cannot use netid flag with inetd flag!\n");
1078 			return (0);
1079 		}
1080 	} else {		/* 4.1 mode */
1081 		pmflag = 0;	/* set pmflag only in tirpcmode */
1082 		inetdflag = 1;	/* inetdflag is TRUE by default */
1083 		if (cmd->nflag) {	/* netid needs TIRPC */
1084 			f_print(stderr, "Cannot use netid flag without TIRPC!\n");
1085 			return (0);
1086 		}
1087 	}
1088 
1089 	if (newstyle && (tblflag || cmd->tflag)) {
1090 		f_print(stderr, "Cannot use table flags with newstyle!\n");
1091 		return (0);
1092 	}
1093 	/* check no conflicts with file generation flags */
1094 	nflags = cmd->cflag + cmd->hflag + cmd->lflag + cmd->mflag +
1095 	    cmd->sflag + cmd->nflag + cmd->tflag + cmd->Ssflag + cmd->Scflag;
1096 
1097 	if (nflags == 0) {
1098 		if (cmd->outfile != NULL || cmd->infile == NULL) {
1099 			return (0);
1100 		}
1101 	} else
1102 		if (nflags > 1) {
1103 			f_print(stderr, "Cannot have more than one file generation flag!\n");
1104 			return (0);
1105 		}
1106 	return (1);
1107 }
1108 
1109 static void
1110 usage()
1111 {
1112 	f_print(stderr, "usage:  %s infile\n", cmdname);
1113 	f_print(stderr, "\t%s [-a][-b][-C][-Dname[=value]] -i size [-I [-K seconds]] [-A] [-M] [-N] [-T] infile\n",
1114 	    cmdname);
1115 	f_print(stderr, "\t%s [-L] [-M] [-c | -h | -l | -m | -t | -Sc | -Ss] [-o outfile] [infile]\n",
1116 	    cmdname);
1117 	f_print(stderr, "\t%s [-s nettype]* [-o outfile] [infile]\n", cmdname);
1118 	f_print(stderr, "\t%s [-n netid]* [-o outfile] [infile]\n", cmdname);
1119 	options_usage();
1120 	exit(1);
1121 }
1122 
1123 static void
1124 options_usage()
1125 {
1126 	f_print(stderr, "options:\n");
1127 	f_print(stderr, "-A\t\tgenerate svc_caller() function\n");
1128 	f_print(stderr, "-a\t\tgenerate all files, including samples\n");
1129 	f_print(stderr, "-b\t\tbackward compatibility mode (generates code for SunOS 4.1)\n");
1130 	f_print(stderr, "-c\t\tgenerate XDR routines\n");
1131 	f_print(stderr, "-C\t\tANSI C mode\n");
1132 	f_print(stderr, "-Dname[=value]\tdefine a symbol (same as #define)\n");
1133 	f_print(stderr, "-h\t\tgenerate header file\n");
1134 	f_print(stderr, "-i size\t\tsize at which to start generating inline code\n");
1135 	f_print(stderr, "-I\t\tgenerate code for inetd support in server (for SunOS 4.1)\n");
1136 	f_print(stderr, "-K seconds\tserver exits after K seconds of inactivity\n");
1137 	f_print(stderr, "-l\t\tgenerate client side stubs\n");
1138 	f_print(stderr, "-L\t\tserver errors will be printed to syslog\n");
1139 	f_print(stderr, "-m\t\tgenerate server side stubs\n");
1140 	f_print(stderr, "-M\t\tgenerate thread-safe stubs\n");
1141 	f_print(stderr, "-n netid\tgenerate server code that supports named netid\n");
1142 	f_print(stderr, "-N\t\tsupports multiple arguments and call-by-value\n");
1143 	f_print(stderr, "-o outfile\tname of the output file\n");
1144 	f_print(stderr, "-s nettype\tgenerate server code that supports named nettype\n");
1145 	f_print(stderr, "-Sc\t\tgenerate sample client code that uses remote procedures\n");
1146 	f_print(stderr, "-Ss\t\tgenerate sample server code that defines remote procedures\n");
1147 	f_print(stderr, "-t\t\tgenerate RPC dispatch table\n");
1148 	f_print(stderr, "-T\t\tgenerate code to support RPC dispatch tables\n");
1149 	f_print(stderr, "-Y path\t\tdirectory name to find C preprocessor (cpp)\n");
1150 
1151 	exit(1);
1152 }
1153