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