xref: /freebsd/usr.bin/rpcgen/rpc_main.c (revision 6de306ecee3831f48debaad1d0b22418faa48e10)
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 __P(( definition * ));
60 extern int write_sample_clnt __P(( definition * ));
61 extern void write_sample_clnt_main __P(( void ));
62 extern void add_sample_msg __P(( void ));
63 static void c_output __P(( char *, char *, int, char * ));
64 static void h_output __P(( char *, char *, int, char * ));
65 static void l_output __P(( char *, char *, int, char * ));
66 static void t_output __P(( char *, char *, int, char * ));
67 static void clnt_output __P(( char *, char *, int, char * ));
68 
69 void c_initialize __P(( void ));
70 
71 #if !defined(__FreeBSD__) && !defined(__NetBSD__)
72 char * rindex();
73 #endif
74 
75 static void usage __P(( void ));
76 static void options_usage __P (( void ));
77 static int do_registers __P(( int, char ** ));
78 static int parseargs __P(( int, char **, struct commandline * ));
79 static void svc_output __P(( char *, char *, int, char * ));
80 static void mkfile_output __P(( struct commandline * ));
81 static void s_output __P(( 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 	if (tirpcflag)
696 #endif
697 	f_print(fout, "#include <stropts.h>\n");
698 	if (inetdflag || !tirpcflag) {
699 		f_print(fout, "#include <sys/socket.h>\n");
700 		f_print(fout, "#include <netinet/in.h>\n");
701 	}
702 
703 	if ((netflag || pmflag) && tirpcflag && !nomain) {
704 		f_print(fout, "#include <netconfig.h>\n");
705 	}
706 	if (tirpcflag)
707 		f_print(fout, "#include <sys/resource.h> /* rlimit */\n");
708 	if (logflag || inetdflag || pmflag)
709 		f_print(fout, "#include <syslog.h>\n");
710 
711 	/* for ANSI-C */
712 	if (Cflag)
713 		f_print(fout,
714 			"\n#ifndef SIG_PF\n#define	SIG_PF void(*)\
715 (int)\n#endif\n");
716 
717 	f_print(fout, "\n#ifdef DEBUG\n#define	RPC_SVC_FG\n#endif\n");
718 	if (timerflag)
719 		f_print(fout, "\n#define	_RPCSVC_CLOSEDOWN %s\n",
720 			svcclosetime);
721 	while ( (def = get_definition()) ) {
722 		foundprogram |= (def->def_kind == DEF_PROGRAM);
723 	}
724 	if (extend && !foundprogram) {
725 		(void) unlink(outfilename);
726 		return;
727 	}
728 	write_most(infile, netflag, nomain);
729 	if (!nomain) {
730 		if (!do_registers(argc, argv)) {
731 			if (outfilename)
732 				(void) unlink(outfilename);
733 			usage();
734 		}
735 		write_rest();
736 	}
737 }
738 
739 /*
740  * generate client side stubs
741  */
742 static void
743 l_output(infile, define, extend, outfile)
744 	char *infile;
745 	char *define;
746 	int extend;
747 	char *outfile;
748 {
749 	char *include;
750 	definition *def;
751 	int foundprogram = 0;
752 	char *outfilename;
753 
754 	open_input(infile, define);
755 	outfilename = extend ? extendfile(infile, outfile) : outfile;
756 	open_output(infile, outfilename);
757 	add_warning();
758 	if (Cflag)
759 		f_print (fout, "#include <memory.h> /* for memset */\n");
760 	if (infile && (include = extendfile(infile, ".h"))) {
761 		f_print(fout, "#include \"%s\"\n", include);
762 		free(include);
763 	} else
764 		f_print(fout, "#include <rpc/rpc.h>\n");
765 	while ( (def = get_definition()) ) {
766 		foundprogram |= (def->def_kind == DEF_PROGRAM);
767 	}
768 	if (extend && !foundprogram) {
769 		(void) unlink(outfilename);
770 		return;
771 	}
772 	write_stubs();
773 }
774 
775 /*
776  * generate the dispatch table
777  */
778 static void
779 t_output(infile, define, extend, outfile)
780 	char *infile;
781 	char *define;
782 	int extend;
783 	char *outfile;
784 {
785 	definition *def;
786 	int foundprogram = 0;
787 	char *outfilename;
788 
789 	open_input(infile, define);
790 	outfilename = extend ? extendfile(infile, outfile) : outfile;
791 	open_output(infile, outfilename);
792 	add_warning();
793 	while ( (def = get_definition()) ) {
794 		foundprogram |= (def->def_kind == DEF_PROGRAM);
795 	}
796 	if (extend && !foundprogram) {
797 		(void) unlink(outfilename);
798 		return;
799 	}
800 	write_tables();
801 }
802 
803 /* sample routine for the server template */
804 static void
805 svc_output(infile, define, extend, outfile)
806 	char *infile;
807 	char *define;
808 	int extend;
809 	char *outfile;
810 {
811 	definition *def;
812 	char *include;
813 	char *outfilename;
814 	long tell;
815 	open_input(infile, define);
816 	outfilename = extend ? extendfile(infile, outfile) : outfile;
817 	checkfiles(infile, outfilename);
818 	/*
819 	 * Check if outfile already exists.
820 	 * if so, print an error message and exit
821 	 */
822 	open_output(infile, outfilename);
823 	add_sample_msg();
824 
825 	if (infile && (include = extendfile(infile, ".h"))) {
826 		f_print(fout, "#include \"%s\"\n", include);
827 		free(include);
828 	} else
829 		f_print(fout, "#include <rpc/rpc.h>\n");
830 
831 	tell = ftell(fout);
832 	while ( (def = get_definition()) ) {
833 		write_sample_svc(def);
834 	}
835 	if (extend && tell == ftell(fout)) {
836 		(void) unlink(outfilename);
837 	}
838 }
839 
840 /* sample main routine for client */
841 static void
842 clnt_output(infile, define, extend, outfile)
843 	char *infile;
844 	char *define;
845 	int extend;
846 	char *outfile;
847 {
848 	definition *def;
849 	char *include;
850 	char *outfilename;
851 	long tell;
852 	int has_program = 0;
853 
854 	open_input(infile, define);
855 	outfilename = extend ? extendfile(infile, outfile) : outfile;
856 	checkfiles(infile, outfilename);
857 	/*
858 	 * Check if outfile already exists.
859 	 * if so, print an error message and exit
860 	 */
861 
862 	open_output(infile, outfilename);
863 	add_sample_msg();
864 	if (infile && (include = extendfile(infile, ".h"))) {
865 		f_print(fout, "#include \"%s\"\n", include);
866 		free(include);
867 	} else
868 		f_print(fout, "#include <rpc/rpc.h>\n");
869 	tell = ftell(fout);
870 	while ( (def = get_definition()) ) {
871 		has_program += write_sample_clnt(def);
872 	}
873 
874 	if (has_program)
875 		write_sample_clnt_main();
876 
877 	if (extend && tell == ftell(fout)) {
878 		(void) unlink(outfilename);
879 	}
880 }
881 
882 
883 static void mkfile_output(cmd)
884 struct commandline *cmd;
885 {
886 	char *mkfilename, *clientname, *clntname, *xdrname, *hdrname;
887 	char *servername, *svcname, *servprogname, *clntprogname;
888 	char *temp;
889 
890 	svcname = file_name(cmd->infile, "_svc.c");
891 	clntname = file_name(cmd->infile, "_clnt.c");
892 	xdrname = file_name(cmd->infile, "_xdr.c");
893 	hdrname = file_name(cmd->infile, ".h");
894 
895 
896 	if (allfiles){
897 		servername = extendfile(cmd->infile, "_server.c");
898 		clientname = extendfile(cmd->infile, "_client.c");
899 	}else{
900 		servername = " ";
901 		clientname = " ";
902 	}
903 	servprogname = extendfile(cmd->infile, "_server");
904 	clntprogname = extendfile(cmd->infile, "_client");
905 
906 	if (allfiles){
907 		mkfilename = alloc(strlen("makefile.") +
908 			strlen(cmd->infile) + 1);
909 		temp = (char *)rindex(cmd->infile, '.');
910 		strcat(mkfilename, "makefile.");
911 		(void) strncat(mkfilename, cmd->infile,
912 			(temp - cmd->infile));
913 	} else
914 		mkfilename = cmd->outfile;
915 
916 
917 	checkfiles(NULL, mkfilename);
918 	open_output(NULL, mkfilename);
919 
920 	f_print(fout, "\n# This is a template makefile generated\
921 		by rpcgen \n");
922 
923 	f_print(fout, "\n# Parameters \n\n");
924 
925 	f_print(fout, "CLIENT = %s\nSERVER = %s\n\n",
926 		clntprogname, servprogname);
927 	f_print(fout, "SOURCES_CLNT.c = \nSOURCES_CLNT.h = \n");
928 	f_print(fout, "SOURCES_SVC.c = \nSOURCES_SVC.h = \n");
929 	f_print(fout, "SOURCES.x = %s\n\n", cmd->infile);
930 	f_print(fout, "TARGETS_SVC.c = %s %s %s \n",
931 		svcname, servername, xdrname);
932 	f_print(fout, "TARGETS_CLNT.c = %s %s %s \n",
933 		clntname, clientname, xdrname);
934 	f_print(fout, "TARGETS = %s %s %s %s %s %s\n\n",
935 		hdrname, xdrname, clntname,
936 		svcname, clientname, servername);
937 
938 	f_print(fout, "OBJECTS_CLNT = $(SOURCES_CLNT.c:%%.c=%%.o) \
939 $(TARGETS_CLNT.c:%%.c=%%.o) ");
940 
941 	f_print(fout, "\nOBJECTS_SVC = $(SOURCES_SVC.c:%%.c=%%.o) \
942 $(TARGETS_SVC.c:%%.c=%%.o) ");
943 
944 
945 	f_print(fout, "\n# Compiler flags \n");
946 	if (mtflag)
947 		f_print(fout, "\nCPPFLAGS += -D_REENTRANT\nCFLAGS += -g \nLDLIBS += -lnsl -lthread\n");
948 	else
949 #if defined(__FreeBSD__) || defined(__NetBSD__)
950 		f_print(fout, "\nCFLAGS += -g \nLDLIBS +=\n");
951 #else
952 		f_print(fout, "\nCFLAGS += -g \nLDLIBS += -lnsl\n");
953 #endif
954 	f_print(fout, "RPCGENFLAGS = \n");
955 
956 	f_print(fout, "\n# Targets \n\n");
957 
958 	f_print(fout, "all : $(CLIENT) $(SERVER)\n\n");
959 	f_print(fout, "$(TARGETS) : $(SOURCES.x) \n");
960 	f_print(fout, "\trpcgen $(RPCGENFLAGS) $(SOURCES.x)\n\n");
961 	f_print(fout, "$(OBJECTS_CLNT) : $(SOURCES_CLNT.c) $(SOURCES_CLNT.h) \
962 $(TARGETS_CLNT.c) \n\n");
963 
964 	f_print(fout, "$(OBJECTS_SVC) : $(SOURCES_SVC.c) $(SOURCES_SVC.h) \
965 $(TARGETS_SVC.c) \n\n");
966 	f_print(fout, "$(CLIENT) : $(OBJECTS_CLNT) \n");
967 #if defined(__FreeBSD__) || defined(__NetBSD__)
968 	f_print(fout, "\t$(CC) -o $(CLIENT) $(OBJECTS_CLNT) \
969 $(LDLIBS) \n\n");
970 #else
971 	f_print(fout, "\t$(LINK.c) -o $(CLIENT) $(OBJECTS_CLNT) \
972 $(LDLIBS) \n\n");
973 #endif
974 	f_print(fout, "$(SERVER) : $(OBJECTS_SVC) \n");
975 #if defined(__FreeBSD__) || defined(__NetBSD__)
976 	f_print(fout, "\t$(CC) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS)\n\n ");
977 	f_print(fout, "clean:\n\t $(RM) -f core $(TARGETS) $(OBJECTS_CLNT) \
978 $(OBJECTS_SVC) $(CLIENT) $(SERVER)\n\n");
979 #else
980 	f_print(fout, "\t$(LINK.c) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS)\n\n ");
981 	f_print(fout, "clean:\n\t $(RM) core $(TARGETS) $(OBJECTS_CLNT) \
982 $(OBJECTS_SVC) $(CLIENT) $(SERVER)\n\n");
983 #endif
984 }
985 
986 
987 
988 /*
989  * Perform registrations for service output
990  * Return 0 if failed; 1 otherwise.
991  */
992 static int
993 do_registers(argc, argv)
994 	int argc;
995 	char *argv[];
996 {
997 	int i;
998 
999 	if (inetdflag || !tirpcflag) {
1000 		for (i = 1; i < argc; i++) {
1001 			if (streq(argv[i], "-s")) {
1002 				if (!check_nettype(argv[i + 1],
1003 						    valid_i_nettypes))
1004 					return (0);
1005 				write_inetd_register(argv[i + 1]);
1006 				i++;
1007 			}
1008 		}
1009 	} else {
1010 		for (i = 1; i < argc; i++)
1011 			if (streq(argv[i], "-s")) {
1012 				if (!check_nettype(argv[i + 1],
1013 						    valid_ti_nettypes))
1014 					return (0);
1015 				write_nettype_register(argv[i + 1]);
1016 				i++;
1017 			} else if (streq(argv[i], "-n")) {
1018 				write_netid_register(argv[i + 1]);
1019 				i++;
1020 			}
1021 	}
1022 	return (1);
1023 }
1024 
1025 /*
1026  * Add another argument to the arg list
1027  */
1028 static void
1029 addarg(cp)
1030 	char *cp;
1031 {
1032 	if (argcount >= ARGLISTLEN) {
1033 		warnx("too many defines");
1034 		crash();
1035 		/*NOTREACHED*/
1036 	}
1037 	arglist[argcount++] = cp;
1038 
1039 }
1040 
1041 static void
1042 putarg(where, cp)
1043 	char *cp;
1044 	int where;
1045 {
1046 	if (where >= ARGLISTLEN) {
1047 		warnx("arglist coding error");
1048 		crash();
1049 		/*NOTREACHED*/
1050 	}
1051 	arglist[where] = cp;
1052 }
1053 
1054 /*
1055  * if input file is stdin and an output file is specified then complain
1056  * if the file already exists. Otherwise the file may get overwritten
1057  * If input file does not exist, exit with an error
1058  */
1059 
1060 static void
1061 checkfiles(infile, outfile)
1062 char *infile;
1063 char *outfile;
1064 {
1065 
1066 	struct stat buf;
1067 
1068 	if (infile)		/* infile ! = NULL */
1069 		if (stat(infile, &buf) < 0)
1070 		{
1071 			warn("%s", infile);
1072 			crash();
1073 		};
1074 	if (outfile) {
1075 		if (stat(outfile, &buf) < 0)
1076 			return;	/* file does not exist */
1077 		else {
1078 			warnx("file '%s' already exists and may be overwritten", outfile);
1079 			crash();
1080 		}
1081 	}
1082 }
1083 
1084 /*
1085  * Parse command line arguments
1086  */
1087 static int
1088 parseargs(argc, argv, cmd)
1089 	int argc;
1090 	char *argv[];
1091 	struct commandline *cmd;
1092 {
1093 	int i;
1094 	int j;
1095 	char c, ch;
1096 	char flag[(1 << 8 * sizeof (char))];
1097 	int nflags;
1098 
1099 	cmd->infile = cmd->outfile = NULL;
1100 	if (argc < 2) {
1101 		return (0);
1102 	}
1103 	allfiles = 0;
1104 	flag['c'] = 0;
1105 	flag['h'] = 0;
1106 	flag['l'] = 0;
1107 	flag['m'] = 0;
1108 	flag['o'] = 0;
1109 	flag['s'] = 0;
1110 	flag['n'] = 0;
1111 	flag['t'] = 0;
1112 	flag['S'] = 0;
1113 	flag['C'] = 0;
1114 	flag['M'] = 0;
1115 
1116 	for (i = 1; i < argc; i++) {
1117 		if (argv[i][0] != '-') {
1118 			if (cmd->infile) {
1119 				warnx("cannot specify more than one input file");
1120 				return (0);
1121 			}
1122 			cmd->infile = argv[i];
1123 		} else {
1124 			for (j = 1; argv[i][j] != 0; j++) {
1125 				c = argv[i][j];
1126 				switch (c) {
1127 				case 'a':
1128 					allfiles = 1;
1129 					break;
1130 				case 'c':
1131 				case 'h':
1132 				case 'l':
1133 				case 'm':
1134 				case 't':
1135 					if (flag[(int)c]) {
1136 						return (0);
1137 					}
1138 					flag[(int)c] = 1;
1139 					break;
1140 				case 'S':
1141 					/*
1142 					 * sample flag: Ss or Sc.
1143 					 *  Ss means set flag['S'];
1144 					 *  Sc means set flag['C'];
1145 					 *  Sm means set flag['M'];
1146 					 */
1147 					ch = argv[i][++j]; /* get next char */
1148 					if (ch == 's')
1149 						ch = 'S';
1150 					else if (ch == 'c')
1151 						ch = 'C';
1152 					else if (ch == 'm')
1153 						ch = 'M';
1154 					else
1155 						return (0);
1156 
1157 					if (flag[(int)ch]) {
1158 						return (0);
1159 					}
1160 					flag[(int)ch] = 1;
1161 					break;
1162 				case 'C': /* ANSI C syntax */
1163 					Cflag = 1;
1164 					ch = argv[i][j+1]; /* get next char */
1165 
1166 					if (ch != 'C')
1167 						break;
1168 					CCflag = 1;
1169 					break;
1170 				case 'b':
1171 					/*
1172 					 *  Turn TIRPC flag off for
1173 					 *  generating backward compatible
1174 					 *  code
1175 					 */
1176 #if defined(__FreeBSD__) || defined(__NetBSD__)
1177 					tirpcflag = 1;
1178 #else
1179 					tirpcflag = 0;
1180 #endif
1181 					break;
1182 
1183 				case 'I':
1184 					inetdflag = 1;
1185 					break;
1186 				case 'N':
1187 					newstyle = 1;
1188 					break;
1189 				case 'L':
1190 					logflag = 1;
1191 					break;
1192 				case 'K':
1193 					if (++i == argc) {
1194 						return (0);
1195 					}
1196 					svcclosetime = argv[i];
1197 					goto nextarg;
1198 				case 'T':
1199 					tblflag = 1;
1200 					break;
1201 				case 'M':
1202 					mtflag = 1;
1203 					break;
1204 				case 'i' :
1205 					if (++i == argc) {
1206 						return (0);
1207 					}
1208 					inline = atoi(argv[i]);
1209 					goto nextarg;
1210 				case 'n':
1211 				case 'o':
1212 				case 's':
1213 					if (argv[i][j - 1] != '-' ||
1214 					    argv[i][j + 1] != 0) {
1215 						return (0);
1216 					}
1217 					flag[(int)c] = 1;
1218 					if (++i == argc) {
1219 						return (0);
1220 					}
1221 					if (c == 'o') {
1222 						if (cmd->outfile) {
1223 							return (0);
1224 						}
1225 						cmd->outfile = argv[i];
1226 					}
1227 					goto nextarg;
1228 				case 'D':
1229 					if (argv[i][j - 1] != '-') {
1230 						return (0);
1231 					}
1232 					(void) addarg(argv[i]);
1233 					goto nextarg;
1234 				case 'Y':
1235 					if (++i == argc) {
1236 						return (0);
1237 					}
1238 					(void) strcpy(pathbuf, argv[i]);
1239 					(void) strcat(pathbuf, "/cpp");
1240 					CPP = pathbuf;
1241 					cppDefined = 1;
1242 					goto nextarg;
1243 
1244 
1245 
1246 				default:
1247 					return (0);
1248 				}
1249 			}
1250 		nextarg:
1251 			;
1252 		}
1253 	}
1254 
1255 	cmd->cflag = flag['c'];
1256 	cmd->hflag = flag['h'];
1257 	cmd->lflag = flag['l'];
1258 	cmd->mflag = flag['m'];
1259 	cmd->nflag = flag['n'];
1260 	cmd->sflag = flag['s'];
1261 	cmd->tflag = flag['t'];
1262 	cmd->Ssflag = flag['S'];
1263 	cmd->Scflag = flag['C'];
1264 	cmd->makefileflag = flag['M'];
1265 
1266 	if (tirpcflag) {
1267 		pmflag = inetdflag ? 0 : 1;
1268 		/* pmflag or inetdflag is always TRUE */
1269 		if ((inetdflag && cmd->nflag)) {
1270 			/* netid not allowed with inetdflag */
1271 			warnx("cannot use netid flag with inetd flag");
1272 			return (0);
1273 		}
1274 	} else {		/* 4.1 mode */
1275 		pmflag = 0;	/* set pmflag only in tirpcmode */
1276 #if !defined(__FreeBSD__) && !defined(__NetBSD__)
1277 		inetdflag = 1;	/* inetdflag is TRUE by default */
1278 #endif
1279 		if (cmd->nflag) { /* netid needs TIRPC */
1280 			warnx("cannot use netid flag without TIRPC");
1281 			return (0);
1282 		}
1283 	}
1284 
1285 	if (newstyle && (tblflag || cmd->tflag)) {
1286 		warnx("cannot use table flags with newstyle");
1287 		return (0);
1288 	}
1289 
1290 	/* check no conflicts with file generation flags */
1291 	nflags = cmd->cflag + cmd->hflag + cmd->lflag + cmd->mflag +
1292 		cmd->sflag + cmd->nflag + cmd->tflag + cmd->Ssflag +
1293 			cmd->Scflag + cmd->makefileflag;
1294 
1295 	if (nflags == 0) {
1296 		if (cmd->outfile != NULL || cmd->infile == NULL) {
1297 			return (0);
1298 		}
1299 	} else if (cmd->infile == NULL &&
1300 	    (cmd->Ssflag || cmd->Scflag || cmd->makefileflag)) {
1301 		warnx("\"infile\" is required for template generation flags");
1302 		return (0);
1303 	} if (nflags > 1) {
1304 		warnx("cannot have more than one file generation flag");
1305 		return (0);
1306 	}
1307 	return (1);
1308 }
1309 
1310 static void
1311 usage()
1312 {
1313 	f_print(stderr, "%s\n%s\n%s\n%s\n%s\n",
1314 		"usage: rpcgen infile",
1315 		"       rpcgen [-abCLNTM] [-Dname[=value]] [-i size]\
1316 [-I [-K seconds]] [-Y path] infile",
1317 		"       rpcgen [-c | -h | -l | -m | -t | -Sc | -Ss | -Sm]\
1318 [-o outfile] [infile]",
1319 		"       rpcgen [-s nettype]* [-o outfile] [infile]",
1320 		"       rpcgen [-n netid]* [-o outfile] [infile]");
1321 	options_usage();
1322 	exit(1);
1323 }
1324 
1325 static void
1326 options_usage()
1327 {
1328 	f_print(stderr, "options:\n");
1329 	f_print(stderr, "-a\t\tgenerate all files, including samples\n");
1330 	f_print(stderr, "-b\t\tbackward compatibility mode (generates code\
1331 for SunOS 4.X)\n");
1332 	f_print(stderr, "-c\t\tgenerate XDR routines\n");
1333 	f_print(stderr, "-C\t\tANSI C mode\n");
1334 	f_print(stderr, "-Dname[=value]\tdefine a symbol (same as #define)\n");
1335 	f_print(stderr, "-h\t\tgenerate header file\n");
1336 	f_print(stderr, "-i size\t\tsize at which to start generating\
1337 inline code\n");
1338 	f_print(stderr, "-I\t\tgenerate code for inetd support in server\
1339 (for SunOS 4.X)\n");
1340 	f_print(stderr, "-K seconds\tserver exits after K seconds of\
1341 inactivity\n");
1342 	f_print(stderr, "-l\t\tgenerate client side stubs\n");
1343 	f_print(stderr, "-L\t\tserver errors will be printed to syslog\n");
1344 	f_print(stderr, "-m\t\tgenerate server side stubs\n");
1345 	f_print(stderr, "-M\t\tgenerate MT-safe code\n");
1346 	f_print(stderr, "-n netid\tgenerate server code that supports\
1347 named netid\n");
1348 	f_print(stderr, "-N\t\tsupports multiple arguments and\
1349 call-by-value\n");
1350 	f_print(stderr, "-o outfile\tname of the output file\n");
1351 	f_print(stderr, "-s nettype\tgenerate server code that supports named\
1352 nettype\n");
1353 	f_print(stderr, "-Sc\t\tgenerate sample client code that uses remote\
1354 procedures\n");
1355 	f_print(stderr, "-Ss\t\tgenerate sample server code that defines\
1356 remote procedures\n");
1357 	f_print(stderr, "-Sm \t\tgenerate makefile template \n");
1358 
1359 	f_print(stderr, "-t\t\tgenerate RPC dispatch table\n");
1360 	f_print(stderr, "-T\t\tgenerate code to support RPC dispatch tables\n");
1361 	f_print(stderr, "-Y path\t\tpath where cpp is found\n");
1362 	exit(1);
1363 }
1364 
1365 #if !defined(__FreeBSD__) && !defined(__NetBSD__)
1366 char *
1367 rindex(sp, c)
1368 	register char *sp, c;
1369 {
1370 	register char *r;
1371 
1372 	r = NULL;
1373 	do {
1374 		if (*sp == c)
1375 			r = sp;
1376 	} while (*sp++);
1377 	return (r);
1378 }
1379 #endif
1380