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