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