Update.
[glibc.git] / sunrpc / rpc_main.c
blobc6fbad18b2e5a24c9cb657bba789e4b8fb401c0e
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 or with the express written consent of
8 * Sun Microsystems, Inc.
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
26 * Sun Microsystems, Inc.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
32 * From @(#)rpc_main.c 1.30 89/03/30 (C) 1987 SMI;
34 char main_rcsid[] =
35 "$Id$";
38 * rpc_main.c, Top level of the RPC protocol compiler.
41 #include <stdio.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <ctype.h>
45 #include <sys/types.h>
46 #include <sys/param.h>
47 #include <sys/file.h>
48 #include <sys/stat.h>
49 #include "rpc_parse.h"
50 #include "rpc_util.h"
51 #include "rpc_scan.h"
52 #include "proto.h"
55 #define EXTEND 1 /* alias for TRUE */
56 #define DONT_EXTEND 0 /* alias for FALSE */
58 #define SVR4_CPP "/usr/ccs/lib/cpp"
59 #define SUNOS_CPP "/lib/cpp"
60 static int cppDefined = 0; /* explicit path for C preprocessor */
62 struct commandline {
63 int cflag; /* xdr C routines */
64 int hflag; /* header file */
65 int lflag; /* client side stubs */
66 int mflag; /* server side stubs */
67 int nflag; /* netid flag */
68 int sflag; /* server stubs for the given transport */
69 int tflag; /* dispatch Table file */
70 int Ssflag; /* produce server sample code */
71 int Scflag; /* produce client sample code */
72 const char *infile; /* input module name */
73 const char *outfile; /* output module name */
77 static const char *cmdname;
79 static const char *svcclosetime = "120";
80 static const char *CPP = SVR4_CPP;
81 static char CPPFLAGS[] = "-C";
82 static char pathbuf[MAXPATHLEN + 1];
83 static const char *allv[] = {
84 "rpcgen", "-s", "udp", "-s", "tcp",
86 static int allc = sizeof(allv)/sizeof(allv[0]);
87 static const char *allnv[] = {
88 "rpcgen", "-s", "netpath",
90 static int allnc = sizeof(allnv)/sizeof(allnv[0]);
93 * machinations for handling expanding argument list
95 static void addarg(const char *); /* add another argument to the list */
96 static void putarg(int, const char *); /* put argument at specified location */
97 static void clear_args(void); /* clear argument list */
98 static void checkfiles(const char *, const char *);
99 /* check if out file already exists */
101 static void clear_args(void);
102 static char *extendfile(const char *file, const char *ext);
103 static void open_output(const char *infile, const char *outfile);
104 static void add_warning(void);
105 static void clear_args(void);
106 static void find_cpp(void);
107 static void open_input(const char *infile, const char *define);
108 static int check_nettype(const char *name, const char *list_to_check[]);
109 static void c_output(const char *infile, const char *define,
110 int extend, const char *outfile);
111 static void h_output(const char *infile, const char *define,
112 int extend, const char *outfile);
113 static void s_output(int argc, const char *argv[], const char *infile,
114 const char *define, int extend,
115 const char *outfile, int nomain, int netflag);
116 static void l_output(const char *infile, const char *define,
117 int extend, const char *outfile);
118 static void t_output(const char *infile, const char *define,
119 int extend, const char *outfile);
120 static void svc_output(const char *infile, const char *define,
121 int extend, const char *outfile);
122 static void clnt_output(const char *infile, const char *define,
123 int extend, const char *outfile);
124 static int do_registers(int argc, const char *argv[]);
125 static void addarg(const char *cp);
126 static void putarg(int whereto, const char *cp);
127 static void checkfiles(const char *infile, const char *outfile);
128 static int parseargs(int argc, const char *argv[], struct commandline *cmd);
129 static void usage(void);
130 static void options_usage(void);
131 static void c_initialize(void);
132 static char *generate_guard(const char *pathname);
135 #define ARGLISTLEN 20
136 #define FIXEDARGS 2
138 static const char *arglist[ARGLISTLEN];
139 static int argcount = FIXEDARGS;
142 int nonfatalerrors; /* errors */
143 int inetdflag/* = 1*/; /* Support for inetd */ /* is now the default */
144 int pmflag; /* Support for port monitors */
145 int logflag; /* Use syslog instead of fprintf for errors */
146 int tblflag; /* Support for dispatch table file */
148 #define INLINE 3
149 /*length at which to start doing an inline */
151 int inlineflag=INLINE; /* length at which to start doing an inline. 3 = default
152 if 0, no xdr_inline code */
154 int indefinitewait; /* If started by port monitors, hang till it wants */
155 int exitnow; /* If started by port monitors, exit after the call */
156 int timerflag; /* TRUE if !indefinite && !exitnow */
157 int newstyle; /* newstyle of passing arguments (by value) */
158 #ifdef __GNU_LIBRARY__
159 int Cflag = 1 ; /* ANSI C syntax */
160 #else
161 int Cflag = 0 ; /* ANSI C syntax */
162 #endif
163 static int allfiles; /* generate all files */
164 #ifdef __GNU_LIBRARY__
165 int tirpcflag = 0; /* generating code for tirpc, by default */
166 #else
167 int tirpcflag = 1; /* generating code for tirpc, by default */
168 #endif
171 main(int argc, const char *argv[])
173 struct commandline cmd;
175 (void) memset((char *)&cmd, 0, sizeof (struct commandline));
176 clear_args();
177 if (!parseargs(argc, argv, &cmd))
178 usage();
180 if (cmd.cflag || cmd.hflag || cmd.lflag || cmd.tflag || cmd.sflag ||
181 cmd.mflag || cmd.nflag || cmd.Ssflag || cmd.Scflag ) {
182 checkfiles(cmd.infile, cmd.outfile);
184 else
185 checkfiles(cmd.infile,NULL);
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, cmd.outfile);
200 } else if (cmd.Scflag) {
201 clnt_output(cmd.infile, "-DRPC_CLIENT", DONT_EXTEND, cmd.outfile);
202 } else {
203 /* the rescans are required, since cpp may effect input */
204 c_output(cmd.infile, "-DRPC_XDR", EXTEND, "_xdr.c");
205 reinitialize();
206 h_output(cmd.infile, "-DRPC_HDR", EXTEND, ".h");
207 reinitialize();
208 l_output(cmd.infile, "-DRPC_CLNT", EXTEND, "_clnt.c");
209 reinitialize();
210 if (inetdflag || !tirpcflag )
211 s_output(allc, allv, cmd.infile, "-DRPC_SVC", EXTEND,
212 "_svc.c", cmd.mflag, cmd.nflag);
213 else
214 s_output(allnc, allnv, cmd.infile, "-DRPC_SVC",
215 EXTEND, "_svc.c", cmd.mflag, cmd.nflag);
216 if (tblflag) {
217 reinitialize();
218 t_output(cmd.infile, "-DRPC_TBL", EXTEND, "_tbl.i");
220 if (allfiles) {
221 reinitialize();
222 svc_output(cmd.infile, "-DRPC_SERVER", EXTEND, "_server.c");
224 if (allfiles) {
225 reinitialize();
226 clnt_output(cmd.infile, "-DRPC_CLIENT", EXTEND, "_client.c");
229 exit(nonfatalerrors);
230 /* NOTREACHED */
234 * add extension to filename
236 static char *
237 extendfile(const char *file, const char *ext)
239 char *res;
240 const char *p;
242 res = alloc(strlen(file) + strlen(ext) + 1);
243 if (res == NULL) {
244 abort();
246 p = strrchr(file, '.');
247 if (p == NULL) {
248 p = file + strlen(file);
250 (void) strcpy(res, file);
251 (void) strcpy(res + (p - file), ext);
252 return (res);
256 * Open output file with given extension
258 static void
259 open_output(const char *infile, const char *outfile)
262 if (outfile == NULL) {
263 fout = stdout;
264 return;
267 if (infile != NULL && streq(outfile, infile)) {
268 f_print(stderr, "%s: output would overwrite %s\n", cmdname,
269 infile);
270 crash();
272 fout = fopen(outfile, "w");
273 if (fout == NULL) {
274 f_print(stderr, "%s: unable to open ", cmdname);
275 perror(outfile);
276 crash();
278 record_open(outfile);
281 static void
282 add_warning(void)
284 f_print(fout, "/*\n");
285 f_print(fout, " * Please do not edit this file.\n");
286 f_print(fout, " * It was generated using rpcgen.\n");
287 f_print(fout, " */\n\n");
290 static void
291 add_stdheaders(void)
293 f_print(fout, "#include <rpc/types.h>\n");
294 f_print(fout, "#include <rpc/xdr.h>\n\n");
297 /* clear list of arguments */
298 static void clear_args(void)
300 int i;
301 for( i=FIXEDARGS; i<ARGLISTLEN; i++ )
302 arglist[i] = NULL;
303 argcount = FIXEDARGS;
306 /* make sure that a CPP exists */
307 static void find_cpp(void)
309 struct stat buf;
311 if (stat(CPP, &buf) < 0 ) { /* SVR4 or explicit cpp does not exist */
312 if (cppDefined) {
313 fprintf( stderr, "cannot find C preprocessor: %s \n", CPP );
314 crash();
315 } else { /* try the other one */
316 CPP = SUNOS_CPP;
317 if( stat( CPP, &buf ) < 0 ) { /* can't find any cpp */
318 fprintf( stderr, "cannot find any C preprocessor (cpp)\n" );
319 crash();
326 * Open input file with given define for C-preprocessor
328 static void
329 open_input(const char *infile, const char *define)
331 int pd[2];
333 infilename = (infile == NULL) ? "<stdin>" : infile;
334 (void) pipe(pd);
335 switch (fork()) {
336 case 0:
337 find_cpp();
338 putarg(0, CPP);
339 putarg(1, CPPFLAGS);
340 addarg(define);
341 addarg(infile);
342 addarg((char *)NULL);
343 (void) close(1);
344 (void) dup2(pd[1], 1);
345 (void) close(pd[0]);
346 execv(arglist[0], (char **)arglist);
347 perror("execv");
348 exit(1);
349 case -1:
350 perror("fork");
351 exit(1);
353 (void) close(pd[1]);
354 fin = fdopen(pd[0], "r");
355 if (fin == NULL) {
356 f_print(stderr, "%s: ", cmdname);
357 perror(infilename);
358 crash();
362 /* valid tirpc nettypes */
363 static const char *valid_ti_nettypes[] =
365 "netpath",
366 "visible",
367 "circuit_v",
368 "datagram_v",
369 "circuit_n",
370 "datagram_n",
371 "udp",
372 "tcp",
373 "raw",
374 NULL
377 /* valid inetd nettypes */
378 static const char *valid_i_nettypes[] =
380 "udp",
381 "tcp",
382 NULL
385 static int check_nettype(const char *name, const char *list_to_check[]) {
386 int i;
387 for( i = 0; list_to_check[i] != NULL; i++ ) {
388 if( strcmp( name, list_to_check[i] ) == 0 ) {
389 return 1;
392 f_print( stderr, "illegal nettype :\'%s\'\n", name );
393 return 0;
397 * Compile into an XDR routine output file
400 static void
401 c_output(const char *infile, const char *define, int extend,
402 const char *outfile)
404 definition *def;
405 char *include;
406 const char *outfilename;
407 long tell;
409 c_initialize();
410 open_input(infile, define);
411 outfilename = extend ? extendfile(infile, outfile) : outfile;
412 open_output(infile, outfilename);
413 add_warning();
414 add_stdheaders();
415 if (infile && (include = extendfile(infile, ".h"))) {
416 f_print(fout, "#include \"%s\"\n", include);
417 free(include);
418 /* .h file already contains rpc/rpc.h */
419 } else
420 f_print(fout, "#include <rpc/rpc.h>\n");
421 tell = ftell(fout);
422 while ((def = get_definition())!=NULL) {
423 emit(def);
425 if (extend && tell == ftell(fout)) {
426 (void) unlink(outfilename);
430 void
431 c_initialize(void)
434 /* add all the starting basic types */
436 add_type(1,"int");
437 add_type(1,"long");
438 add_type(1,"short");
439 add_type(1,"bool");
441 add_type(1,"u_int");
442 add_type(1,"u_long");
443 add_type(1,"u_short");
447 char rpcgen_table_dcl[] = "struct rpcgen_table {\n\
448 char *(*proc)();\n\
449 xdrproc_t xdr_arg;\n\
450 unsigned len_arg;\n\
451 xdrproc_t xdr_res;\n\
452 unsigned len_res;\n\
453 };\n";
456 static char *generate_guard(const char *pathname) {
457 const char *filename;
458 char *guard, *tmp;
460 filename = strrchr(pathname, '/' ); /* find last component */
461 filename = ((filename == NULL) ? pathname : filename+1);
462 guard = strdup(filename);
463 /* convert to upper case */
464 tmp = guard;
465 while (*tmp) {
466 if (islower(*tmp))
467 *tmp = toupper(*tmp);
468 tmp++;
471 guard = extendfile(guard, "_H_RPCGEN");
472 return guard;
476 * Compile into an XDR header file
480 static void
481 h_output(const char *infile, const char *define, int extend,
482 const char *outfile)
484 definition *def;
485 const char *ifilename;
486 const char *outfilename;
487 long tell;
488 char *guard;
489 list *l;
491 open_input(infile, define);
492 outfilename = extend ? extendfile(infile, outfile) : outfile;
493 open_output(infile, outfilename);
494 add_warning();
495 ifilename = (infile == NULL) ? "STDIN" : infile;
496 guard = generate_guard( outfilename ? outfilename: ifilename );
498 f_print(fout,"#ifndef _%s\n#define _%s\n\n", guard,
499 guard);
501 f_print(fout, "#include <rpc/rpc.h>\n\n");
503 tell = ftell(fout);
504 /* print data definitions */
505 while ((def = get_definition())!=NULL) {
506 print_datadef(def);
509 /* print function declarations.
510 Do this after data definitions because they might be used as
511 arguments for functions */
512 for (l = defined; l != NULL; l = l->next) {
513 print_funcdef(l->val);
515 if (extend && tell == ftell(fout)) {
516 (void) unlink(outfilename);
517 } else if (tblflag) {
518 f_print(fout, rpcgen_table_dcl);
520 f_print(fout, "\n#endif /* !_%s */\n", guard);
524 * Compile into an RPC service
526 static void
527 s_output(int argc, const char *argv[], const char *infile, const char *define,
528 int extend, const char *outfile, int nomain, int netflag)
530 char *include;
531 definition *def;
532 int foundprogram = 0;
533 const char *outfilename;
535 open_input(infile, define);
536 outfilename = extend ? extendfile(infile, outfile) : outfile;
537 open_output(infile, outfilename);
538 add_warning();
539 if (infile && (include = extendfile(infile, ".h"))) {
540 f_print(fout, "#include \"%s\"\n", include);
541 free(include);
542 } else
543 f_print(fout, "#include <rpc/rpc.h>\n");
545 f_print(fout, "#include <stdio.h>\n");
546 f_print(fout, "#include <stdlib.h>/* getenv, exit */\n");
547 if (Cflag) {
548 f_print (fout, "#include <rpc/pmap_clnt.h> /* for pmap_unset */\n");
549 f_print (fout, "#include <string.h> /* strcmp */ \n");
551 if (strcmp(svcclosetime, "-1") == 0)
552 indefinitewait = 1;
553 else if (strcmp(svcclosetime, "0") == 0)
554 exitnow = 1;
555 else if (inetdflag || pmflag) {
556 f_print(fout, "#include <signal.h>\n");
557 timerflag = 1;
560 if( !tirpcflag && inetdflag )
561 #ifdef __GNU_LIBRARY__
562 f_print(fout, "#include <sys/ioctl.h> /* ioctl, TIOCNOTTY */\n");
563 #else
564 f_print(fout, "#include <sys/ttycom.h>/* TIOCNOTTY */\n");
565 #endif
566 if( Cflag && (inetdflag || pmflag ) ) {
567 #ifdef __GNU_LIBRARY__
568 f_print(fout, "#include <sys/types.h> /* open */\n");
569 f_print(fout, "#include <sys/stat.h> /* open */\n");
570 f_print(fout, "#include <fcntl.h> /* open */\n");
571 f_print(fout, "#include <unistd.h> /* getdtablesize */\n");
572 #else
573 f_print(fout, "#ifdef __cplusplus\n");
574 f_print(fout, "#include <sysent.h> /* getdtablesize, open */\n");
575 f_print(fout, "#endif /* __cplusplus */\n");
576 #endif
577 if( tirpcflag )
578 f_print(fout, "#include <unistd.h> /* setsid */\n");
580 if( tirpcflag )
581 f_print(fout, "#include <sys/types.h>\n");
583 f_print(fout, "#include <memory.h>\n");
584 #ifndef __GNU_LIBRARY__
585 f_print(fout, "#include <stropts.h>\n");
586 #endif
587 if (inetdflag || !tirpcflag ) {
588 f_print(fout, "#include <sys/socket.h>\n");
589 f_print(fout, "#include <netinet/in.h>\n");
592 if ( (netflag || pmflag) && tirpcflag ) {
593 f_print(fout, "#include <netconfig.h>\n");
595 if (/*timerflag &&*/ tirpcflag)
596 f_print(fout, "#include <sys/resource.h> /* rlimit */\n");
597 if (logflag || inetdflag || pmflag) {
598 #ifdef __GNU_LIBRARY__
599 f_print(fout, "#include <syslog.h>\n");
600 #else
601 f_print(fout, "#ifdef SYSLOG\n");
602 f_print(fout, "#include <syslog.h>\n");
603 f_print(fout, "#else\n");
604 f_print(fout, "#define LOG_ERR 1\n");
605 f_print(fout, "#define openlog(a, b, c)\n");
606 f_print(fout, "#endif\n");
607 #endif
610 /* for ANSI-C */
611 f_print(fout, "\n#ifdef __STDC__\n#define SIG_PF void(*)(int)\n#endif\n");
613 #ifndef __GNU_LIBRARY__
614 f_print(fout, "\n#ifdef DEBUG\n#define RPC_SVC_FG\n#endif\n");
615 #endif
616 if (timerflag)
617 f_print(fout, "\n#define _RPCSVC_CLOSEDOWN %s\n", svcclosetime);
618 while ((def = get_definition())!=NULL) {
619 foundprogram |= (def->def_kind == DEF_PROGRAM);
621 if (extend && !foundprogram) {
622 (void) unlink(outfilename);
623 return;
625 write_most(infile, netflag, nomain);
626 if (!nomain) {
627 if( !do_registers(argc, argv) ) {
628 if (outfilename)
629 (void) unlink(outfilename);
630 usage();
632 write_rest();
637 * generate client side stubs
639 static void
640 l_output(const char *infile, const char *define, int extend,
641 const char *outfile)
643 char *include;
644 definition *def;
645 int foundprogram = 0;
646 const char *outfilename;
648 open_input(infile, define);
649 outfilename = extend ? extendfile(infile, outfile) : outfile;
650 open_output(infile, outfilename);
651 add_warning();
652 if (Cflag)
653 f_print (fout, "#include <memory.h> /* for memset */\n");
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 while ((def = get_definition())!=NULL) {
660 foundprogram |= (def->def_kind == DEF_PROGRAM);
662 if (extend && !foundprogram) {
663 (void) unlink(outfilename);
664 return;
666 write_stubs();
670 * generate the dispatch table
672 static void
673 t_output(const char *infile, const char *define, int extend,
674 const char *outfile)
676 definition *def;
677 int foundprogram = 0;
678 const char *outfilename;
680 open_input(infile, define);
681 outfilename = extend ? extendfile(infile, outfile) : outfile;
682 open_output(infile, outfilename);
683 add_warning();
684 while ((def = get_definition())!=NULL) {
685 foundprogram |= (def->def_kind == DEF_PROGRAM);
687 if (extend && !foundprogram) {
688 (void) unlink(outfilename);
689 return;
691 write_tables();
694 /* sample routine for the server template */
695 static void
696 svc_output(const char *infile, const char *define, int extend,
697 const char *outfile)
699 definition *def;
700 char *include;
701 const char *outfilename;
702 long tell;
704 open_input(infile, define);
705 outfilename = extend ? extendfile(infile, outfile) : outfile;
706 checkfiles(infile,outfilename); /*check if outfile already exists.
707 if so, print an error message and exit*/
708 open_output(infile, outfilename);
709 add_sample_msg();
711 if (infile && (include = extendfile(infile, ".h"))) {
712 f_print(fout, "#include \"%s\"\n", include);
713 free(include);
714 } else
715 f_print(fout, "#include <rpc/rpc.h>\n");
717 tell = ftell(fout);
718 while ((def = get_definition())!=NULL) {
719 write_sample_svc(def);
721 if (extend && tell == ftell(fout)) {
722 (void) unlink(outfilename);
727 /* sample main routine for client */
728 static void
729 clnt_output(const char *infile, const char *define, int extend,
730 const char *outfile)
732 definition *def;
733 char *include;
734 const char *outfilename;
735 long tell;
736 int has_program = 0;
738 open_input(infile, define);
739 outfilename = extend ? extendfile(infile, outfile) : outfile;
740 checkfiles(infile,outfilename); /*check if outfile already exists.
741 if so, print an error message and exit*/
743 open_output(infile, outfilename);
744 add_sample_msg();
745 if (infile && (include = extendfile(infile, ".h"))) {
746 f_print(fout, "#include \"%s\"\n", include);
747 free(include);
748 } else
749 f_print(fout, "#include <rpc/rpc.h>\n");
750 tell = ftell(fout);
751 while ((def = get_definition())!=NULL) {
752 has_program += write_sample_clnt(def);
755 if( has_program )
756 write_sample_clnt_main();
758 if (extend && tell == ftell(fout)) {
759 (void) unlink(outfilename);
764 * Perform registrations for service output
765 * Return 0 if failed; 1 otherwise.
767 static int do_registers(int argc, const char *argv[])
769 int i;
771 if ( inetdflag || !tirpcflag) {
772 for (i = 1; i < argc; i++) {
773 if (streq(argv[i], "-s")) {
774 if(!check_nettype( argv[i + 1], valid_i_nettypes ))
775 return 0;
776 write_inetd_register(argv[i + 1]);
777 i++;
780 } else {
781 for (i = 1; i < argc; i++)
782 if (streq(argv[i], "-s")) {
783 if(!check_nettype( argv[i + 1], valid_ti_nettypes ))
784 return 0;
785 write_nettype_register(argv[i + 1]);
786 i++;
787 } else if (streq(argv[i], "-n")) {
788 write_netid_register(argv[i + 1]);
789 i++;
792 return 1;
796 * Add another argument to the arg list
798 static void
799 addarg(const char *cp)
801 if (argcount >= ARGLISTLEN) {
802 f_print(stderr, "rpcgen: too many defines\n");
803 crash();
804 /*NOTREACHED*/
806 arglist[argcount++] = cp;
810 static void
811 putarg(int whereto, const char *cp)
813 if (whereto >= ARGLISTLEN) {
814 f_print(stderr, "rpcgen: arglist coding error\n");
815 crash();
816 /*NOTREACHED*/
818 arglist[whereto] = cp;
822 * if input file is stdin and an output file is specified then complain
823 * if the file already exists. Otherwise the file may get overwritten
824 * If input file does not exist, exit with an error
827 static void
828 checkfiles(const char *infile, const char *outfile)
831 struct stat buf;
833 if(infile) /* infile ! = NULL */
834 if(stat(infile,&buf) < 0)
836 perror(infile);
837 crash();
839 if (outfile) {
840 if (stat(outfile, &buf) < 0)
841 return; /* file does not exist */
842 else {
843 f_print(stderr,
844 "file '%s' already exists and may be overwritten\n", outfile);
845 crash();
851 * Parse command line arguments
853 static int
854 parseargs(int argc, const char *argv[], struct commandline *cmd)
856 int i;
857 int j;
858 int c;
859 char flag[(1 << 8 * sizeof(char))];
860 int nflags;
862 cmdname = argv[0];
863 cmd->infile = cmd->outfile = NULL;
864 if (argc < 2) {
865 return (0);
867 allfiles = 0;
868 flag['c'] = 0;
869 flag['h'] = 0;
870 flag['l'] = 0;
871 flag['m'] = 0;
872 flag['o'] = 0;
873 flag['s'] = 0;
874 flag['n'] = 0;
875 flag['t'] = 0;
876 flag['S'] = 0;
877 flag['C'] = 0;
878 for (i = 1; i < argc; i++) {
879 if (argv[i][0] != '-') {
880 if (cmd->infile) {
881 f_print( stderr, "Cannot specify more than one input file!\n");
883 return (0);
885 cmd->infile = argv[i];
886 } else {
887 for (j = 1; argv[i][j] != 0; j++) {
888 c = argv[i][j];
889 switch (c) {
890 case 'a':
891 allfiles = 1;
892 break;
893 case 'c':
894 case 'h':
895 case 'l':
896 case 'm':
897 case 't':
898 if (flag[c]) {
899 return (0);
901 flag[c] = 1;
902 break;
903 case 'S':
904 /* sample flag: Ss or Sc.
905 Ss means set flag['S'];
906 Sc means set flag['C']; */
907 c = argv[i][++j]; /* get next char */
908 if( c == 's' )
909 c = 'S';
910 else if( c == 'c' )
911 c = 'C';
912 else
913 return( 0 );
915 if (flag[c]) {
916 return (0);
918 flag[c] = 1;
919 break;
920 case 'C': /* ANSI C syntax */
921 Cflag = 1;
922 break;
924 #ifdef __GNU_LIBRARY__
925 case 'k': /* K&R C syntax */
926 Cflag = 0;
927 break;
929 #endif
930 case 'b': /* turn TIRPC flag off for
931 generating backward compatible
933 tirpcflag = 0;
934 break;
936 #ifdef __GNU_LIBRARY__
937 case '5': /* turn TIRPC flag on for
938 generating SysVr4 compatible
940 tirpcflag = 1;
941 break;
943 #endif
944 case 'I':
945 inetdflag = 1;
946 break;
947 case 'N':
948 newstyle = 1;
949 break;
950 case 'L':
951 logflag = 1;
952 break;
953 case 'K':
954 if (++i == argc) {
955 return (0);
957 svcclosetime = argv[i];
958 goto nextarg;
959 case 'T':
960 tblflag = 1;
961 break;
962 case 'i' :
963 if (++i == argc) {
964 return (0);
966 inlineflag = atoi(argv[i]);
967 goto nextarg;
968 case 'n':
969 case 'o':
970 case 's':
971 if (argv[i][j - 1] != '-' ||
972 argv[i][j + 1] != 0) {
973 return (0);
975 flag[c] = 1;
976 if (++i == argc) {
977 return (0);
979 if (c == 's') {
980 if (!streq(argv[i], "udp") &&
981 !streq(argv[i], "tcp")) {
982 return (0);
984 } else if (c == 'o') {
985 if (cmd->outfile) {
986 return (0);
988 cmd->outfile = argv[i];
990 goto nextarg;
991 case 'D':
992 if (argv[i][j - 1] != '-') {
993 return (0);
995 (void) addarg(argv[i]);
996 goto nextarg;
997 case 'Y':
998 if (++i == argc) {
999 return (0);
1001 (void) strcpy(pathbuf, argv[i]);
1002 (void) strcat(pathbuf, "/cpp");
1003 CPP = pathbuf;
1004 cppDefined = 1;
1005 goto nextarg;
1009 default:
1010 return (0);
1013 nextarg:
1018 cmd->cflag = flag['c'];
1019 cmd->hflag = flag['h'];
1020 cmd->lflag = flag['l'];
1021 cmd->mflag = flag['m'];
1022 cmd->nflag = flag['n'];
1023 cmd->sflag = flag['s'];
1024 cmd->tflag = flag['t'];
1025 cmd->Ssflag = flag['S'];
1026 cmd->Scflag = flag['C'];
1028 if( tirpcflag ) {
1029 pmflag = inetdflag ? 0 : 1; /* pmflag or inetdflag is always TRUE */
1030 if( (inetdflag && cmd->nflag)) { /* netid not allowed with inetdflag */
1031 f_print(stderr, "Cannot use netid flag with inetd flag!\n");
1032 return (0);
1034 } else { /* 4.1 mode */
1035 pmflag = 0; /* set pmflag only in tirpcmode */
1036 #ifndef __GNU_LIBRARY__
1037 inetdflag = 1; /* inetdflag is TRUE by default */
1038 #endif
1039 if( cmd->nflag ) { /* netid needs TIRPC */
1040 f_print( stderr, "Cannot use netid flag without TIRPC!\n");
1041 return( 0 );
1045 if( newstyle && ( tblflag || cmd->tflag) ) {
1046 f_print( stderr, "Cannot use table flags with newstyle!\n");
1047 return( 0 );
1050 /* check no conflicts with file generation flags */
1051 nflags = cmd->cflag + cmd->hflag + cmd->lflag + cmd->mflag +
1052 cmd->sflag + cmd->nflag + cmd->tflag + cmd->Ssflag + cmd->Scflag;
1054 if (nflags == 0) {
1055 if (cmd->outfile != NULL || cmd->infile == NULL) {
1056 return (0);
1058 } else if (nflags > 1) {
1059 f_print( stderr, "Cannot have more than one file generation flag!\n");
1060 return (0);
1062 return (1);
1065 static void
1066 usage(void)
1068 f_print(stderr, "usage: %s infile\n", cmdname);
1069 f_print(stderr, "\t%s [-a][-b][-C][-Dname[=value]] -i size [-I [-K seconds]] [-L][-N][-T] infile\n",
1070 cmdname);
1071 f_print(stderr, "\t%s [-c | -h | -l | -m | -t | -Sc | -Ss] [-o outfile] [infile]\n",
1072 cmdname);
1073 f_print(stderr, "\t%s [-s nettype]* [-o outfile] [infile]\n", cmdname);
1074 f_print(stderr, "\t%s [-n netid]* [-o outfile] [infile]\n", cmdname);
1075 options_usage();
1076 exit(1);
1079 static void
1080 options_usage(void)
1082 f_print(stderr, "options:\n");
1083 f_print(stderr, "-a\t\tgenerate all files, including samples\n");
1084 f_print(stderr, "-b\t\tbackward compatibility mode (generates code for SunOS 4.1)\n");
1085 f_print(stderr, "-c\t\tgenerate XDR routines\n");
1086 f_print(stderr, "-C\t\tANSI C mode\n");
1087 f_print(stderr, "-Dname[=value]\tdefine a symbol (same as #define)\n");
1088 f_print(stderr, "-h\t\tgenerate header file\n");
1089 f_print(stderr, "-i size\t\tsize at which to start generating inline code\n");
1090 f_print(stderr, "-I\t\tgenerate code for inetd support in server (for SunOS 4.1)\n");
1091 f_print(stderr, "-K seconds\tserver exits after K seconds of inactivity\n");
1092 f_print(stderr, "-l\t\tgenerate client side stubs\n");
1093 f_print(stderr, "-L\t\tserver errors will be printed to syslog\n");
1094 f_print(stderr, "-m\t\tgenerate server side stubs\n");
1095 f_print(stderr, "-n netid\tgenerate server code that supports named netid\n");
1096 f_print(stderr, "-N\t\tsupports multiple arguments and call-by-value\n");
1097 f_print(stderr, "-o outfile\tname of the output file\n");
1098 f_print(stderr, "-s nettype\tgenerate server code that supports named nettype\n");
1099 f_print(stderr, "-Sc\t\tgenerate sample client code that uses remote procedures\n");
1100 f_print(stderr, "-Ss\t\tgenerate sample server code that defines remote procedures\n");
1101 f_print(stderr, "-t\t\tgenerate RPC dispatch table\n");
1102 f_print(stderr, "-T\t\tgenerate code to support RPC dispatch tables\n");
1103 f_print(stderr, "-Y path\t\tdirectory name to find C preprocessor (cpp)\n");
1105 exit(1);