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.
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.
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.
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.
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.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
33 #ident "@(#)rpc_main.c 1.21 94/04/25 SMI"
34 static char sccsid[] = "@(#)rpc_main.c 1.30 89/03/30 (C) 1987 SMI";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * rpc_main.c, Top level of the RPC protocol compiler.
43 * Copyright (C) 1987, Sun Microsystems, Inc.
51 #include <sys/types.h>
52 #include <sys/param.h>
55 #include "rpc_parse.h"
59 static void c_output(const char *, const char *, int, const char *);
60 static void h_output(const char *, const char *, int, const char *, int);
61 static void l_output(const char *, const char *, int, const char *);
62 static void t_output(const char *, const char *, int, const char *);
63 static void clnt_output(const char *, const char *, int, const char * );
64 static char *generate_guard(const char *);
65 static void c_initialize(void);
67 static void usage(void);
68 static void options_usage(void);
69 static int do_registers(int, const char **);
70 static int parseargs(int, const char **, struct commandline *);
71 static void svc_output(const char *, const char *, int, const char *);
72 static void mkfile_output(struct commandline *);
73 static void s_output(int, const char **, const char *, const char *, int, const char *, int, int);
75 #define EXTEND 1 /* alias for TRUE */
76 #define DONT_EXTEND 0 /* alias for FALSE */
78 static const char *svcclosetime = "120";
79 static const char *CPP = NULL;
80 static const char CPPFLAGS[] = "-C";
81 static char pathbuf[MAXPATHLEN + 1];
82 static const char *allv[] = {
83 "rpcgen", "-s", "udp", "-s", "tcp",
85 static int allc = nitems(allv);
86 static const char *allnv[] = {
87 "rpcgen", "-s", "netpath",
89 static int allnc = nitems(allnv);
92 * machinations for handling expanding argument list
94 static void addarg(const char *); /* add another argument to the list */
95 static void insarg(int, const char *); /* insert arg at specified location */
96 static void clear_args(void); /* clear argument list */
97 static void checkfiles(const char *, const char *);
98 /* check if out file already exists */
102 #define ARGLISTLEN 20
105 static char *arglist[ARGLISTLEN];
106 static int argcount = FIXEDARGS;
109 int nonfatalerrors; /* errors */
110 int inetdflag = 0; /* Support for inetd is disabled by default, use -I */
111 int pmflag = 0; /* Support for port monitors is disabled by default */
112 int tirpc_socket = 1; /* TI-RPC on socket, no TLI library */
113 int logflag; /* Use syslog instead of fprintf for errors */
114 int tblflag; /* Support for dispatch table file */
115 int mtflag = 0; /* Support for MT */
118 /* length at which to start doing an inline */
120 int inline_size = INLINE;
122 * Length at which to start doing an inline. INLINE = default
123 * if 0, no xdr_inline code
126 int indefinitewait; /* If started by port monitors, hang till it wants */
127 int exitnow; /* If started by port monitors, exit after the call */
128 int timerflag; /* TRUE if !indefinite && !exitnow */
129 int newstyle; /* newstyle of passing arguments (by value) */
130 int CCflag = 0; /* C++ files */
131 static int allfiles; /* generate all files */
132 int tirpcflag = 1; /* generating code for tirpc, by default */
133 xdrfunc *xdrfunc_head = NULL; /* xdr function list */
134 xdrfunc *xdrfunc_tail = NULL; /* xdr function list */
139 main(int argc, const char *argv[])
141 struct commandline cmd;
143 (void) memset((char *)&cmd, 0, sizeof (struct commandline));
145 if (!parseargs(argc, argv, &cmd))
148 * Only the client and server side stubs are likely to be customized,
149 * so in that case only, check if the outfile exists, and if so,
150 * print an error message and exit.
152 if (cmd.Ssflag || cmd.Scflag || cmd.makefileflag) {
153 checkfiles(cmd.infile, cmd.outfile);
156 checkfiles(cmd.infile, NULL);
159 c_output(cmd.infile, "-DRPC_XDR", DONT_EXTEND, cmd.outfile);
160 } else if (cmd.hflag) {
161 h_output(cmd.infile, "-DRPC_HDR", DONT_EXTEND, cmd.outfile,
163 } else if (cmd.lflag) {
164 l_output(cmd.infile, "-DRPC_CLNT", DONT_EXTEND, cmd.outfile);
165 } else if (cmd.sflag || cmd.mflag || (cmd.nflag)) {
166 s_output(argc, argv, cmd.infile, "-DRPC_SVC", DONT_EXTEND,
167 cmd.outfile, cmd.mflag, cmd.nflag);
168 } else if (cmd.tflag) {
169 t_output(cmd.infile, "-DRPC_TBL", DONT_EXTEND, cmd.outfile);
170 } else if (cmd.Ssflag) {
171 svc_output(cmd.infile, "-DRPC_SERVER", DONT_EXTEND,
173 } else if (cmd.Scflag) {
174 clnt_output(cmd.infile, "-DRPC_CLIENT", DONT_EXTEND,
176 } else if (cmd.makefileflag) {
179 /* the rescans are required, since cpp may effect input */
180 c_output(cmd.infile, "-DRPC_XDR", EXTEND, "_xdr.c");
182 h_output(cmd.infile, "-DRPC_HDR", EXTEND, ".h", cmd.hflag);
184 l_output(cmd.infile, "-DRPC_CLNT", EXTEND, "_clnt.c");
186 if (inetdflag || !tirpcflag)
187 s_output(allc, allv, cmd.infile, "-DRPC_SVC", EXTEND,
188 "_svc.c", cmd.mflag, cmd.nflag);
190 s_output(allnc, allnv, cmd.infile, "-DRPC_SVC",
191 EXTEND, "_svc.c", cmd.mflag, cmd.nflag);
194 t_output(cmd.infile, "-DRPC_TBL", EXTEND, "_tbl.i");
199 svc_output(cmd.infile, "-DRPC_SERVER", EXTEND,
202 clnt_output(cmd.infile, "-DRPC_CLIENT", EXTEND,
206 if (allfiles || (cmd.makefileflag == 1)){
212 exit(nonfatalerrors);
218 * add extension to filename
221 extendfile(const char *path, const char *ext)
227 if ((file = strrchr(path, '/')) == NULL)
231 res = xmalloc(strlen(file) + strlen(ext) + 1);
232 p = strrchr(file, '.');
234 p = file + strlen(file);
236 (void) strcpy(res, file);
237 (void) strcpy(res + (p - file), ext);
242 * Open output file with given extension
245 open_output(const char *infile, const char *outfile)
248 if (outfile == NULL) {
253 if (infile != NULL && streq(outfile, infile)) {
254 warnx("%s already exists. No output generated", infile);
257 fout = fopen(outfile, "w");
259 warn("unable to open %s", outfile);
262 record_open(outfile);
270 f_print(fout, "/*\n");
271 f_print(fout, " * Please do not edit this file.\n");
272 f_print(fout, " * It was generated using rpcgen.\n");
273 f_print(fout, " */\n\n");
276 /* clear list of arguments */
281 for (i = FIXEDARGS; i < ARGLISTLEN; i++)
283 argcount = FIXEDARGS;
286 /* prepend C-preprocessor and flags before arguments */
292 char *dupvar, *s, *t;
296 else if ((var = getenv("RPCGEN_CPP")) == NULL)
297 insarg(0, "/usr/bin/cpp");
299 /* Parse command line in a rudimentary way */
300 dupvar = xstrdup(var);
301 for (s = dupvar, idx = 0; (t = strsep(&s, " \t")) != NULL; ) {
308 insarg(idx, CPPFLAGS);
312 * Open input file with given define for C-preprocessor
315 open_input(const char *infile, const char *define)
319 infilename = (infile == NULL) ? "<stdin>" : infile;
321 switch (childpid = fork()) {
327 addarg((char *)NULL);
329 (void) dup2(pd[1], 1);
331 execvp(arglist[0], arglist);
332 err(1, "execvp %s", arglist[0]);
337 fin = fdopen(pd[0], "r");
339 warn("%s", infilename);
344 /* valid tirpc nettypes */
345 static const char *valid_ti_nettypes[] =
359 /* valid inetd nettypes */
360 static const char *valid_i_nettypes[] =
368 check_nettype(const char *name, const char *list_to_check[])
371 for (i = 0; list_to_check[i] != NULL; i++) {
372 if (strcmp(name, list_to_check[i]) == 0) {
376 warnx("illegal nettype :\'%s\'", name);
381 file_name(const char *file, const char *ext)
384 temp = extendfile(file, ext);
386 if (access(temp, F_OK) != -1)
395 c_output(const char *infile, const char *define, int extend, const char *outfile)
399 const char *outfilename;
403 open_input(infile, define);
404 outfilename = extend ? extendfile(infile, outfile) : outfile;
405 open_output(infile, outfilename);
407 if (infile && (include = extendfile(infile, ".h"))) {
408 f_print(fout, "#include \"%s\"\n", include);
410 /* .h file already contains rpc/rpc.h */
412 f_print(fout, "#include <rpc/rpc.h>\n");
414 while ( (def = get_definition()) ) {
417 if (extend && tell == ftell(fout)) {
418 (void) unlink(outfilename);
427 /* add all the starting basic types */
430 add_type(1, "short");
432 add_type(1, "u_int");
433 add_type(1, "u_long");
434 add_type(1, "u_short");
438 static const char rpcgen_table_dcl[] = "struct rpcgen_table {\n\
440 xdrproc_t xdr_arg; \n\
441 unsigned len_arg; \n\
442 xdrproc_t xdr_res; \n\
443 unsigned len_res; \n\
448 generate_guard(const char *pathname)
450 const char *filename;
451 char *guard, *tmp, *stopat;
453 filename = strrchr(pathname, '/'); /* find last component */
454 filename = ((filename == NULL) ? pathname : filename+1);
455 guard = xstrdup(filename);
456 stopat = strrchr(guard, '.');
459 * Convert to a valid C macro name and make it upper case.
460 * Map macro unfriendly characterss to '_'.
462 for (tmp = guard; *tmp != '\000'; ++tmp) {
464 *tmp = toupper(*tmp);
465 else if (isupper(*tmp) || *tmp == '_')
467 else if (tmp == guard)
469 else if (isdigit(*tmp))
470 /* OK for all but first character */;
471 else if (tmp == stopat) {
478 * Can't have a '_' in front, because it'll end up being "__".
479 * "__" macros shoudln't be used. So, remove all of the
480 * '_' characters from the front.
483 for (tmp = guard; *tmp == '_'; ++tmp)
487 guard = extendfile(guard, "_H_RPCGEN");
492 * Compile into an XDR header file
497 h_output(const char *infile, const char *define, int extend, const char *outfile, int headeronly)
500 const char *outfilename;
506 open_input(infile, define);
507 outfilename = extend ? extendfile(infile, outfile) : outfile;
508 open_output(infile, outfilename);
510 if (outfilename || infile){
511 guard = generate_guard(outfilename ? outfilename: infile);
515 f_print(fout, "#ifndef _%s\n#define _%s\n\n", guard,
518 f_print(fout, "#include <rpc/rpc.h>\n");
521 f_print(fout, "#include <pthread.h>\n");
523 /* put the C++ support */
525 f_print(fout, "\n#ifdef __cplusplus\n");
526 f_print(fout, "extern \"C\" {\n");
527 f_print(fout, "#endif\n\n");
530 /* put in a typedef for quadprecision. Only with Cflag */
534 /* print data definitions */
535 while ( (def = get_definition()) ) {
536 print_datadef(def, headeronly);
540 * print function declarations.
541 * Do this after data definitions because they might be used as
542 * arguments for functions
544 for (l = defined; l != NULL; l = l->next) {
545 print_funcdef(l->val, headeronly);
547 /* Now print all xdr func declarations */
548 if (xdrfunc_head != NULL){
551 "\n/* the xdr functions */\n");
554 f_print(fout, "\n#ifdef __cplusplus\n");
555 f_print(fout, "extern \"C\" {\n");
556 f_print(fout, "#endif\n");
559 xdrfuncp = xdrfunc_head;
560 while (xdrfuncp != NULL){
561 print_xdr_func_def(xdrfuncp->name, xdrfuncp->pointerp);
562 xdrfuncp = xdrfuncp->next;
566 if (extend && tell == ftell(fout)) {
567 (void) unlink(outfilename);
568 } else if (tblflag) {
569 f_print(fout, rpcgen_table_dcl);
572 f_print(fout, "\n#ifdef __cplusplus\n");
573 f_print(fout, "}\n");
574 f_print(fout, "#endif\n");
576 f_print(fout, "\n#endif /* !_%s */\n", guard);
580 * Compile into an RPC service
583 s_output(int argc, const char *argv[], const char *infile, const char *define,
584 int extend, const char *outfile, int nomain, int netflag)
588 int foundprogram = 0;
589 const char *outfilename;
591 open_input(infile, define);
592 outfilename = extend ? extendfile(infile, outfile) : outfile;
593 open_output(infile, outfilename);
595 if (infile && (include = extendfile(infile, ".h"))) {
596 f_print(fout, "#include \"%s\"\n", include);
599 f_print(fout, "#include <rpc/rpc.h>\n");
601 f_print(fout, "#include <stdio.h>\n");
602 f_print(fout, "#include <stdlib.h> /* getenv, exit */\n");
603 f_print (fout, "#include <rpc/pmap_clnt.h> /* for pmap_unset */\n");
604 f_print (fout, "#include <string.h> /* strcmp */\n");
606 f_print(fout, "#include <rpc/rpc_com.h>\n");
607 if (strcmp(svcclosetime, "-1") == 0)
609 else if (strcmp(svcclosetime, "0") == 0)
611 else if (inetdflag || pmflag) {
612 f_print(fout, "#include <signal.h>\n");
616 if (!tirpcflag && inetdflag)
617 f_print(fout, "#include <sys/ttycom.h> /* TIOCNOTTY */\n");
618 if (inetdflag || pmflag) {
619 f_print(fout, "#ifdef __cplusplus\n");
621 "#include <sys/sysent.h> /* getdtablesize, open */\n");
622 f_print(fout, "#endif /* __cplusplus */\n");
625 f_print(fout, "#include <fcntl.h> /* open */\n");
626 f_print(fout, "#include <unistd.h> /* fork / setsid */\n");
627 f_print(fout, "#include <sys/types.h>\n");
630 f_print(fout, "#include <string.h>\n");
631 if (inetdflag || !tirpcflag) {
632 f_print(fout, "#include <sys/socket.h>\n");
633 f_print(fout, "#include <netinet/in.h>\n");
636 if ((netflag || pmflag) && tirpcflag && !nomain) {
637 f_print(fout, "#include <netconfig.h>\n");
640 f_print(fout, "#include <sys/resource.h> /* rlimit */\n");
641 if (logflag || inetdflag || pmflag || tirpcflag)
642 f_print(fout, "#include <syslog.h>\n");
644 f_print(fout, "\n#ifdef DEBUG\n#define RPC_SVC_FG\n#endif\n");
646 f_print(fout, "\n#define _RPCSVC_CLOSEDOWN %s\n",
648 while ( (def = get_definition()) ) {
649 foundprogram |= (def->def_kind == DEF_PROGRAM);
651 if (extend && !foundprogram) {
652 (void) unlink(outfilename);
655 write_most(infile, netflag, nomain);
657 if (!do_registers(argc, argv)) {
659 (void) unlink(outfilename);
667 * generate client side stubs
670 l_output(const char *infile, const char *define, int extend, const char *outfile)
674 int foundprogram = 0;
675 const char *outfilename;
677 open_input(infile, define);
678 outfilename = extend ? extendfile(infile, outfile) : outfile;
679 open_output(infile, outfilename);
681 f_print (fout, "#include <string.h> /* for memset */\n");
682 if (infile && (include = extendfile(infile, ".h"))) {
683 f_print(fout, "#include \"%s\"\n", include);
686 f_print(fout, "#include <rpc/rpc.h>\n");
687 while ( (def = get_definition()) ) {
688 foundprogram |= (def->def_kind == DEF_PROGRAM);
690 if (extend && !foundprogram) {
691 (void) unlink(outfilename);
698 * generate the dispatch table
701 t_output(const char *infile, const char *define, int extend, const char *outfile)
704 int foundprogram = 0;
705 const char *outfilename;
707 open_input(infile, define);
708 outfilename = extend ? extendfile(infile, outfile) : outfile;
709 open_output(infile, outfilename);
711 while ( (def = get_definition()) ) {
712 foundprogram |= (def->def_kind == DEF_PROGRAM);
714 if (extend && !foundprogram) {
715 (void) unlink(outfilename);
721 /* sample routine for the server template */
723 svc_output(const char *infile, const char *define, int extend, const char *outfile)
727 const char *outfilename;
729 open_input(infile, define);
730 outfilename = extend ? extendfile(infile, outfile) : outfile;
731 checkfiles(infile, outfilename);
733 * Check if outfile already exists.
734 * if so, print an error message and exit
736 open_output(infile, outfilename);
739 if (infile && (include = extendfile(infile, ".h"))) {
740 f_print(fout, "#include \"%s\"\n", include);
743 f_print(fout, "#include <rpc/rpc.h>\n");
746 while ( (def = get_definition()) ) {
747 write_sample_svc(def);
749 if (extend && tell == ftell(fout)) {
750 (void) unlink(outfilename);
754 /* sample main routine for client */
756 clnt_output(const char *infile, const char *define, int extend, const char *outfile)
760 const char *outfilename;
764 open_input(infile, define);
765 outfilename = extend ? extendfile(infile, outfile) : outfile;
766 checkfiles(infile, outfilename);
768 * Check if outfile already exists.
769 * if so, print an error message and exit
772 open_output(infile, outfilename);
774 if (infile && (include = extendfile(infile, ".h"))) {
775 f_print(fout, "#include \"%s\"\n", include);
778 f_print(fout, "#include <rpc/rpc.h>\n");
779 f_print(fout, "#include <stdio.h>\n");
780 f_print(fout, "#include <stdlib.h>\n");
782 while ( (def = get_definition()) ) {
783 has_program += write_sample_clnt(def);
787 write_sample_clnt_main();
789 if (extend && tell == ftell(fout)) {
790 (void) unlink(outfilename);
795 static void mkfile_output(struct commandline *cmd)
797 const char *mkfilename, *clientname, *clntname, *xdrname, *hdrname;
798 const char *servername, *svcname, *servprogname, *clntprogname;
799 char *temp, *mkftemp;
801 svcname = file_name(cmd->infile, "_svc.c");
802 clntname = file_name(cmd->infile, "_clnt.c");
803 xdrname = file_name(cmd->infile, "_xdr.c");
804 hdrname = file_name(cmd->infile, ".h");
808 servername = extendfile(cmd->infile, "_server.c");
809 clientname = extendfile(cmd->infile, "_client.c");
814 servprogname = extendfile(cmd->infile, "_server");
815 clntprogname = extendfile(cmd->infile, "_client");
818 mkftemp = xmalloc(strlen("makefile.") +
819 strlen(cmd->infile) + 1);
820 temp = strrchr(cmd->infile, '.');
821 strcpy(mkftemp, "makefile.");
822 (void) strncat(mkftemp, cmd->infile,
823 (temp - cmd->infile));
824 mkfilename = mkftemp;
826 mkfilename = cmd->outfile;
829 checkfiles(NULL, mkfilename);
830 open_output(NULL, mkfilename);
832 f_print(fout, "\n# This is a template makefile generated\
835 f_print(fout, "\n# Parameters \n\n");
837 f_print(fout, "CLIENT = %s\nSERVER = %s\n\n",
838 clntprogname, servprogname);
839 f_print(fout, "SOURCES_CLNT.c = \nSOURCES_CLNT.h = \n");
840 f_print(fout, "SOURCES_SVC.c = \nSOURCES_SVC.h = \n");
841 f_print(fout, "SOURCES.x = %s\n\n", cmd->infile);
842 f_print(fout, "TARGETS_SVC.c = %s %s %s \n",
843 svcname, servername, xdrname);
844 f_print(fout, "TARGETS_CLNT.c = %s %s %s \n",
845 clntname, clientname, xdrname);
846 f_print(fout, "TARGETS = %s %s %s %s %s %s\n\n",
847 hdrname, xdrname, clntname,
848 svcname, clientname, servername);
850 f_print(fout, "OBJECTS_CLNT = $(SOURCES_CLNT.c:%%.c=%%.o) \
851 $(TARGETS_CLNT.c:%%.c=%%.o) ");
853 f_print(fout, "\nOBJECTS_SVC = $(SOURCES_SVC.c:%%.c=%%.o) \
854 $(TARGETS_SVC.c:%%.c=%%.o) ");
857 f_print(fout, "\n# Compiler flags \n");
859 f_print(fout, "\nCFLAGS += -D_REENTRANT -D_THEAD_SAFE \nLDLIBS += -pthread\n");
861 f_print(fout, "RPCGENFLAGS = \n");
863 f_print(fout, "\n# Targets \n\n");
865 f_print(fout, "all : $(CLIENT) $(SERVER)\n\n");
866 f_print(fout, "$(TARGETS) : $(SOURCES.x) \n");
867 f_print(fout, "\trpcgen $(RPCGENFLAGS) $(SOURCES.x)\n\n");
869 f_print(fout, "\trpcgen -Sc $(RPCGENFLAGS) $(SOURCES.x) -o %s\n\n", clientname);
870 f_print(fout, "\trpcgen -Ss $(RPCGENFLAGS) $(SOURCES.x) -o %s\n\n", servername);
872 f_print(fout, "$(OBJECTS_CLNT) : $(SOURCES_CLNT.c) $(SOURCES_CLNT.h) \
873 $(TARGETS_CLNT.c) \n\n");
875 f_print(fout, "$(OBJECTS_SVC) : $(SOURCES_SVC.c) $(SOURCES_SVC.h) \
876 $(TARGETS_SVC.c) \n\n");
877 f_print(fout, "$(CLIENT) : $(OBJECTS_CLNT) \n");
878 f_print(fout, "\t$(CC) -o $(CLIENT) $(OBJECTS_CLNT) \
880 f_print(fout, "$(SERVER) : $(OBJECTS_SVC) \n");
881 f_print(fout, "\t$(CC) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS)\n\n");
882 f_print(fout, "clean:\n\t rm -f core $(TARGETS) $(OBJECTS_CLNT) \
883 $(OBJECTS_SVC) $(CLIENT) $(SERVER)\n\n");
889 * Perform registrations for service output
890 * Return 0 if failed; 1 otherwise.
893 do_registers(int argc, const char *argv[])
897 if (inetdflag || !tirpcflag) {
898 for (i = 1; i < argc; i++) {
899 if (streq(argv[i], "-s")) {
900 if (!check_nettype(argv[i + 1],
903 write_inetd_register(argv[i + 1]);
908 for (i = 1; i < argc; i++)
909 if (streq(argv[i], "-s")) {
910 if (!check_nettype(argv[i + 1],
913 write_nettype_register(argv[i + 1]);
915 } else if (streq(argv[i], "-n")) {
916 write_netid_register(argv[i + 1]);
924 * Add another argument to the arg list
927 addarg(const char *cp)
929 if (argcount >= ARGLISTLEN) {
930 warnx("too many defines");
935 arglist[argcount++] = xstrdup(cp);
937 arglist[argcount++] = NULL;
942 * Insert an argument at the specified location
945 insarg(int place, const char *cp)
949 if (argcount >= ARGLISTLEN) {
950 warnx("too many defines");
955 /* Move up existing arguments */
956 for (i = argcount - 1; i >= place; i--)
957 arglist[i + 1] = arglist[i];
959 arglist[place] = xstrdup(cp);
964 * if input file is stdin and an output file is specified then complain
965 * if the file already exists. Otherwise the file may get overwritten
966 * If input file does not exist, exit with an error
970 checkfiles(const char *infile, const char *outfile)
975 if (infile) /* infile ! = NULL */
976 if (stat(infile, &buf) < 0)
982 if (stat(outfile, &buf) < 0)
983 return; /* file does not exist */
985 warnx("file '%s' already exists and may be overwritten", outfile);
992 * Parse command line arguments
995 parseargs(int argc, const char *argv[], struct commandline *cmd)
1000 char flag[(1 << 8 * sizeof (char))];
1003 cmd->infile = cmd->outfile = NULL;
1020 for (i = 1; i < argc; i++) {
1021 if (argv[i][0] != '-') {
1023 warnx("cannot specify more than one input file");
1026 cmd->infile = argv[i];
1028 for (j = 1; argv[i][j] != 0; j++) {
1046 * sample flag: Ss or Sc.
1047 * Ss means set flag['S'];
1048 * Sc means set flag['C'];
1049 * Sm means set flag['M'];
1051 ch = argv[i][++j]; /* get next char */
1061 if (flag[(int)ch]) {
1066 case 'C': /* ANSI C syntax */
1067 ch = argv[i][j+1]; /* get next char */
1075 * Turn TIRPC flag off for
1076 * generating backward compatible
1098 svcclosetime = argv[i];
1110 inline_size = atoi(argv[i]);
1115 if (argv[i][j - 1] != '-' ||
1116 argv[i][j + 1] != 0) {
1127 cmd->outfile = argv[i];
1131 if (argv[i][j - 1] != '-') {
1134 (void) addarg(argv[i]);
1140 if (strlcpy(pathbuf, argv[i],
1141 sizeof(pathbuf)) >= sizeof(pathbuf)
1142 || strlcat(pathbuf, "/cpp",
1145 warnx("argument too long");
1162 cmd->cflag = flag['c'];
1163 cmd->hflag = flag['h'];
1164 cmd->lflag = flag['l'];
1165 cmd->mflag = flag['m'];
1166 cmd->nflag = flag['n'];
1167 cmd->sflag = flag['s'];
1168 cmd->tflag = flag['t'];
1169 cmd->Ssflag = flag['S'];
1170 cmd->Scflag = flag['C'];
1171 cmd->makefileflag = flag['M'];
1176 if ((inetdflag && cmd->nflag)) {
1177 /* netid not allowed with inetdflag */
1178 warnx("cannot use netid flag with inetd flag");
1181 } else { /* 4.1 mode */
1182 pmflag = 0; /* set pmflag only in tirpcmode */
1183 if (cmd->nflag) { /* netid needs TIRPC */
1184 warnx("cannot use netid flag without TIRPC");
1189 if (newstyle && (tblflag || cmd->tflag)) {
1190 warnx("cannot use table flags with newstyle");
1194 /* check no conflicts with file generation flags */
1195 nflags = cmd->cflag + cmd->hflag + cmd->lflag + cmd->mflag +
1196 cmd->sflag + cmd->nflag + cmd->tflag + cmd->Ssflag +
1197 cmd->Scflag + cmd->makefileflag;
1200 if (cmd->outfile != NULL || cmd->infile == NULL) {
1203 } else if (cmd->infile == NULL &&
1204 (cmd->Ssflag || cmd->Scflag || cmd->makefileflag)) {
1205 warnx("\"infile\" is required for template generation flags");
1208 warnx("cannot have more than one file generation flag");
1217 f_print(stderr, "%s\n%s\n%s\n%s\n%s\n",
1218 "usage: rpcgen infile",
1219 " rpcgen [-abCLNTM] [-Dname[=value]] [-i size]\
1220 [-I -P [-K seconds]] [-Y path] infile",
1221 " rpcgen [-c | -h | -l | -m | -t | -Sc | -Ss | -Sm]\
1222 [-o outfile] [infile]",
1223 " rpcgen [-s nettype]* [-o outfile] [infile]",
1224 " rpcgen [-n netid]* [-o outfile] [infile]");
1232 f_print(stderr, "options:\n");
1233 f_print(stderr, "-a\t\tgenerate all files, including samples\n");
1234 f_print(stderr, "-b\t\tbackward compatibility mode (generates code \
1235 for FreeBSD 4.X)\n");
1236 f_print(stderr, "-c\t\tgenerate XDR routines\n");
1237 f_print(stderr, "-C\t\tANSI C mode\n");
1238 f_print(stderr, "-Dname[=value]\tdefine a symbol (same as #define)\n");
1239 f_print(stderr, "-h\t\tgenerate header file\n");
1240 f_print(stderr, "-i size\t\tsize at which to start generating\
1242 f_print(stderr, "-I\t\tgenerate code for inetd support in server\n");
1243 f_print(stderr, "-K seconds\tserver exits after K seconds of\
1245 f_print(stderr, "-l\t\tgenerate client side stubs\n");
1246 f_print(stderr, "-L\t\tserver errors will be printed to syslog\n");
1247 f_print(stderr, "-m\t\tgenerate server side stubs\n");
1248 f_print(stderr, "-M\t\tgenerate MT-safe code\n");
1249 f_print(stderr, "-n netid\tgenerate server code that supports\
1251 f_print(stderr, "-N\t\tsupports multiple arguments and\
1253 f_print(stderr, "-o outfile\tname of the output file\n");
1254 f_print(stderr, "-P\t\tgenerate code for port monitoring support in server\n");
1255 f_print(stderr, "-s nettype\tgenerate server code that supports named\
1257 f_print(stderr, "-Sc\t\tgenerate sample client code that uses remote\
1259 f_print(stderr, "-Ss\t\tgenerate sample server code that defines\
1260 remote procedures\n");
1261 f_print(stderr, "-Sm \t\tgenerate makefile template \n");
1263 f_print(stderr, "-t\t\tgenerate RPC dispatch table\n");
1264 f_print(stderr, "-T\t\tgenerate code to support RPC dispatch tables\n");
1265 f_print(stderr, "-Y path\t\tpath where cpp is found\n");