xref: /freebsd/usr.bin/rpcgen/rpc_main.c (revision a8445737e740901f5f2c8d24c12ef7fc8b00134e)
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 	"$Id: rpc_main.c,v 1.8 1998/01/15 08:46:58 jb Exp $";
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 #ifdef __FreeBSD__
88 #define	SUNOS_CPP "/usr/libexec/cpp"
89 #elif defined(__NetBSD__)
90 #define SUNOS_CPP "/usr/bin/cpp"
91 #else
92 #define	SUNOS_CPP "/usr/lib/cpp"
93 #endif
94 
95 static int cppDefined = 0;	/* explicit path for C preprocessor */
96 
97 static char *svcclosetime = "120";
98 static char *CPP = SVR4_CPP;
99 static char CPPFLAGS[] = "-C";
100 static char pathbuf[MAXPATHLEN + 1];
101 static char *allv[] = {
102 	"rpcgen", "-s", "udp", "-s", "tcp",
103 };
104 static int allc = sizeof (allv)/sizeof (allv[0]);
105 static char *allnv[] = {
106 	"rpcgen", "-s", "netpath",
107 };
108 static int allnc = sizeof (allnv)/sizeof (allnv[0]);
109 
110 /*
111  * machinations for handling expanding argument list
112  */
113 static void addarg();		/* add another argument to the list */
114 static void putarg();		/* put argument at specified location  */
115 static void clear_args();	/* clear argument list */
116 static void checkfiles();	/* check if out file already exists */
117 
118 
119 
120 #define	ARGLISTLEN	20
121 #define	FIXEDARGS	2
122 
123 static char *arglist[ARGLISTLEN];
124 static int argcount = FIXEDARGS;
125 
126 
127 int nonfatalerrors;	/* errors */
128 #if defined(__FreeBSD__) || defined(__NetBSD__)
129 int inetdflag = 0;	/* Support for inetd  is now the default */
130 #else
131 int inetdflag;	/* Support for inetd  is now the default */
132 #endif
133 int pmflag;		/* Support for port monitors */
134 int logflag;		/* Use syslog instead of fprintf for errors */
135 int tblflag;		/* Support for dispatch table file */
136 int mtflag = 0;		/* Support for MT */
137 #if defined(__FreeBSD__) || defined(__NetBSD__)
138 #define INLINE 0
139 #else
140 #define	INLINE 5
141 #endif
142 /* length at which to start doing an inline */
143 
144 int inline = INLINE;
145 /*
146  * Length at which to start doing an inline. INLINE = default
147  * if 0, no xdr_inline code
148  */
149 
150 int indefinitewait;	/* If started by port monitors, hang till it wants */
151 int exitnow;		/* If started by port monitors, exit after the call */
152 int timerflag;		/* TRUE if !indefinite && !exitnow */
153 int newstyle;		/* newstyle of passing arguments (by value) */
154 int Cflag = 0;		/* ANSI C syntax */
155 int CCflag = 0;		/* C++ files */
156 static int allfiles;   /* generate all files */
157 #if defined(__FreeBSD__) || defined(__NetBSD__)
158 int tirpcflag = 0;    /* generating code for tirpc, by default */
159 #else
160 int tirpcflag = 1;    /* generating code for tirpc, by default */
161 #endif
162 xdrfunc *xdrfunc_head = NULL; /* xdr function list */
163 xdrfunc *xdrfunc_tail = NULL; /* xdr function list */
164 pid_t childpid;
165 
166 
167 int
168 main(argc, argv)
169 	int argc;
170 	char *argv[];
171 {
172 	struct commandline cmd;
173 
174 	(void) memset((char *)&cmd, 0, sizeof (struct commandline));
175 	clear_args();
176 	if (!parseargs(argc, argv, &cmd))
177 		usage();
178 	/*
179 	 * Only the client and server side stubs are likely to be customized,
180 	 *  so in that case only, check if the outfile exists, and if so,
181 	 *  print an error message and exit.
182 	 */
183 	if (cmd.Ssflag || cmd.Scflag || cmd.makefileflag) {
184 		checkfiles(cmd.infile, cmd.outfile);
185 	}
186 	else
187 		checkfiles(cmd.infile, NULL);
188 
189 	if (cmd.cflag) {
190 		c_output(cmd.infile, "-DRPC_XDR", DONT_EXTEND, cmd.outfile);
191 	} else if (cmd.hflag) {
192 		h_output(cmd.infile, "-DRPC_HDR", DONT_EXTEND, cmd.outfile);
193 	} else if (cmd.lflag) {
194 		l_output(cmd.infile, "-DRPC_CLNT", DONT_EXTEND, cmd.outfile);
195 	} else if (cmd.sflag || cmd.mflag || (cmd.nflag)) {
196 		s_output(argc, argv, cmd.infile, "-DRPC_SVC", DONT_EXTEND,
197 			cmd.outfile, cmd.mflag, cmd.nflag);
198 	} else if (cmd.tflag) {
199 		t_output(cmd.infile, "-DRPC_TBL", DONT_EXTEND, cmd.outfile);
200 	} else if  (cmd.Ssflag) {
201 		svc_output(cmd.infile, "-DRPC_SERVER", DONT_EXTEND,
202 			cmd.outfile);
203 	} else if (cmd.Scflag) {
204 		clnt_output(cmd.infile, "-DRPC_CLIENT", DONT_EXTEND,
205 			    cmd.outfile);
206 	} else if (cmd.makefileflag) {
207 		mkfile_output(&cmd);
208 	} else {
209 		/* the rescans are required, since cpp may effect input */
210 		c_output(cmd.infile, "-DRPC_XDR", EXTEND, "_xdr.c");
211 		reinitialize();
212 		h_output(cmd.infile, "-DRPC_HDR", EXTEND, ".h");
213 		reinitialize();
214 		l_output(cmd.infile, "-DRPC_CLNT", EXTEND, "_clnt.c");
215 		reinitialize();
216 		if (inetdflag || !tirpcflag)
217 			s_output(allc, allv, cmd.infile, "-DRPC_SVC", EXTEND,
218 			"_svc.c", cmd.mflag, cmd.nflag);
219 		else
220 			s_output(allnc, allnv, cmd.infile, "-DRPC_SVC",
221 				EXTEND, "_svc.c", cmd.mflag, cmd.nflag);
222 		if (tblflag) {
223 			reinitialize();
224 		t_output(cmd.infile, "-DRPC_TBL", EXTEND, "_tbl.i");
225 		}
226 
227 		if (allfiles) {
228 			reinitialize();
229 			svc_output(cmd.infile, "-DRPC_SERVER", EXTEND,
230 				"_server.c");
231 			reinitialize();
232 			clnt_output(cmd.infile, "-DRPC_CLIENT", EXTEND,
233 				"_client.c");
234 
235 		}
236 		if (allfiles || (cmd.makefileflag == 1)){
237 			reinitialize();
238 			mkfile_output(&cmd);
239 		}
240 
241 	}
242 	exit(nonfatalerrors);
243 	/* NOTREACHED */
244 }
245 
246 
247 /*
248  * add extension to filename
249  */
250 static char *
251 #if defined(__FreeBSD__) || defined(__NetBSD__)
252 extendfile(path, ext)
253 	char *path;
254 #else
255 extendfile(file, ext)
256 	char *file;
257 #endif
258 	char *ext;
259 {
260 	char *res;
261 	char *p;
262 #if defined(__FreeBSD__) || defined(__NetBSD__)
263 	char *file;
264 
265 	if ((file = rindex(path, '/')) == NULL)
266 		file = path;
267 	else
268 		file++;
269 #endif
270 	res = alloc(strlen(file) + strlen(ext) + 1);
271 	if (res == NULL) {
272 		abort();
273 	}
274 	p = strrchr(file, '.');
275 	if (p == NULL) {
276 		p = file + strlen(file);
277 	}
278 	(void) strcpy(res, file);
279 	(void) strcpy(res + (p - file), ext);
280 	return (res);
281 }
282 
283 /*
284  * Open output file with given extension
285  */
286 static void
287 open_output(infile, outfile)
288 	char *infile;
289 	char *outfile;
290 {
291 
292 	if (outfile == NULL) {
293 		fout = stdout;
294 		return;
295 	}
296 
297 	if (infile != NULL && streq(outfile, infile)) {
298 		warnx("%s already exists. No output generated", infile);
299 		crash();
300 	}
301 	fout = fopen(outfile, "w");
302 	if (fout == NULL) {
303 		warn("unable to open %s", outfile);
304 		crash();
305 	}
306 	record_open(outfile);
307 
308 	return;
309 }
310 
311 static void
312 add_warning()
313 {
314 	f_print(fout, "/*\n");
315 	f_print(fout, " * Please do not edit this file.\n");
316 	f_print(fout, " * It was generated using rpcgen.\n");
317 	f_print(fout, " */\n\n");
318 }
319 
320 /* clear list of arguments */
321 static void clear_args()
322 {
323 	int i;
324 	for (i = FIXEDARGS; i < ARGLISTLEN; i++)
325 		arglist[i] = NULL;
326 	argcount = FIXEDARGS;
327 }
328 
329 /* make sure that a CPP exists */
330 static void find_cpp()
331 {
332 	struct stat buf;
333 
334 	if (stat(CPP, &buf) < 0)  { /* SVR4 or explicit cpp does not exist */
335 		if (cppDefined) {
336 			warnx("cannot find C preprocessor: %s", CPP);
337 			crash();
338 		} else {	/* try the other one */
339 			CPP = SUNOS_CPP;
340 			if (stat(CPP, &buf) < 0) { /* can't find any cpp */
341 				warnx("cannot find any C preprocessor (cpp)");
342 				crash();
343 			}
344 		}
345 	}
346 }
347 
348 /*
349  * Open input file with given define for C-preprocessor
350  */
351 static void
352 open_input(infile, define)
353 	char *infile;
354 	char *define;
355 {
356 	int pd[2];
357 
358 	infilename = (infile == NULL) ? "<stdin>" : infile;
359 	(void) pipe(pd);
360 	switch (childpid = fork()) {
361 	case 0:
362 		find_cpp();
363 		putarg(0, CPP);
364 		putarg(1, CPPFLAGS);
365 		addarg(define);
366 		if (infile)
367 			addarg(infile);
368 		addarg((char *)NULL);
369 		(void) close(1);
370 		(void) dup2(pd[1], 1);
371 		(void) close(pd[0]);
372 		execv(arglist[0], arglist);
373 		warn("execv");
374 		exit(1);
375 	case -1:
376 		warn("fork");
377 		exit(1);
378 	}
379 	(void) close(pd[1]);
380 	fin = fdopen(pd[0], "r");
381 	if (fin == NULL) {
382 		warn("%s", infilename);
383 		crash();
384 	}
385 }
386 
387 /* valid tirpc nettypes */
388 static char* valid_ti_nettypes[] =
389 {
390 	"netpath",
391 	"visible",
392 	"circuit_v",
393 	"datagram_v",
394 	"circuit_n",
395 	"datagram_n",
396 	"udp",
397 	"tcp",
398 	"raw",
399 	NULL
400 	};
401 
402 /* valid inetd nettypes */
403 static char* valid_i_nettypes[] =
404 {
405 	"udp",
406 	"tcp",
407 	NULL
408 	};
409 
410 static int check_nettype(name, list_to_check)
411 char* name;
412 char* list_to_check[];
413 {
414 	int i;
415 	for (i = 0; list_to_check[i] != NULL; i++) {
416 		if (strcmp(name, list_to_check[i]) == 0) {
417 			return (1);
418 		}
419 	}
420 	warnx("illegal nettype :\'%s\'", name);
421 	return (0);
422 }
423 
424 static char *
425 file_name(file, ext)
426 char *file;
427 char *ext;
428 {
429 	char *temp;
430 	temp = extendfile(file, ext);
431 
432 	if (access(temp, F_OK) != -1)
433 		return (temp);
434 	else
435 		return ((char *)" ");
436 
437 }
438 
439 
440 static void
441 c_output(infile, define, extend, outfile)
442 	char *infile;
443 	char *define;
444 	int extend;
445 	char *outfile;
446 {
447 	definition *def;
448 	char *include;
449 	char *outfilename;
450 	long tell;
451 
452 	c_initialize();
453 	open_input(infile, define);
454 	outfilename = extend ? extendfile(infile, outfile) : outfile;
455 	open_output(infile, outfilename);
456 	add_warning();
457 	if (infile && (include = extendfile(infile, ".h"))) {
458 		f_print(fout, "#include \"%s\"\n", include);
459 		free(include);
460 		/* .h file already contains rpc/rpc.h */
461 	} else
462 		f_print(fout, "#include <rpc/rpc.h>\n");
463 	tell = ftell(fout);
464 	while ( (def = get_definition()) ) {
465 		emit(def);
466 	}
467 	if (extend && tell == ftell(fout)) {
468 		(void) unlink(outfilename);
469 	}
470 }
471 
472 
473 void
474 c_initialize()
475 {
476 
477 	/* add all the starting basic types */
478 	add_type(1, "int");
479 	add_type(1, "long");
480 	add_type(1, "short");
481 	add_type(1, "bool");
482 	add_type(1, "u_int");
483 	add_type(1, "u_long");
484 	add_type(1, "u_short");
485 
486 }
487 
488 char rpcgen_table_dcl[] = "struct rpcgen_table {\n\
489 	char	*(*proc)(); \n\
490 	xdrproc_t	xdr_arg; \n\
491 	unsigned	len_arg; \n\
492 	xdrproc_t	xdr_res; \n\
493 	unsigned	len_res; \n\
494 }; \n";
495 
496 
497 char *generate_guard(pathname)
498 	char* pathname;
499 {
500 	char* filename, *guard, *tmp;
501 
502 	filename = strrchr(pathname, '/');  /* find last component */
503 	filename = ((filename == 0) ? pathname : filename+1);
504 	guard = strdup(filename);
505 	/* convert to upper case */
506 	tmp = guard;
507 	while (*tmp) {
508 		if (islower(*tmp))
509 			*tmp = toupper(*tmp);
510 		tmp++;
511 	}
512 	guard = extendfile(guard, "_H_RPCGEN");
513 	return (guard);
514 }
515 
516 /*
517  * Compile into an XDR header file
518  */
519 
520 
521 static void
522 h_output(infile, define, extend, outfile)
523 	char *infile;
524 	char *define;
525 	int extend;
526 	char *outfile;
527 {
528 	definition *def;
529 	char *outfilename;
530 	long tell;
531 	char *guard;
532 	list *l;
533 	xdrfunc *xdrfuncp;
534 	int i;
535 
536 	open_input(infile, define);
537 	outfilename =  extend ? extendfile(infile, outfile) : outfile;
538 	open_output(infile, outfilename);
539 	add_warning();
540 	if (outfilename || infile){
541 		guard = generate_guard(outfilename ? outfilename: infile);
542 	} else
543 		guard = "STDIN_";
544 
545 	f_print(fout, "#ifndef _%s\n#define	_%s\n\n", guard,
546 		guard);
547 
548 	f_print(fout, "#include <rpc/rpc.h>\n");
549 
550 	if (mtflag) {
551 		f_print(fout, "#include <synch.h>\n");
552 		f_print(fout, "#include <thread.h>\n");
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__)
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__)
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__)
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__)
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__)
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__)
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__)
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