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 checkfiles(const char *, const char *);
97 /* check if out file already exists */
99 static char **arglist;
100 static int argcount = 0;
101 static int argmax = 0;
103 int nonfatalerrors; /* errors */
104 int inetdflag = 0; /* Support for inetd is disabled by default, use -I */
105 int pmflag = 0; /* Support for port monitors is disabled by default */
106 int tirpc_socket = 1; /* TI-RPC on socket, no TLI library */
107 int logflag; /* Use syslog instead of fprintf for errors */
108 int tblflag; /* Support for dispatch table file */
109 int mtflag = 0; /* Support for MT */
112 /* length at which to start doing an inline */
114 int inline_size = INLINE;
116 * Length at which to start doing an inline. INLINE = default
117 * if 0, no xdr_inline code
120 int indefinitewait; /* If started by port monitors, hang till it wants */
121 int exitnow; /* If started by port monitors, exit after the call */
122 int timerflag; /* TRUE if !indefinite && !exitnow */
123 int newstyle; /* newstyle of passing arguments (by value) */
124 int CCflag = 0; /* C++ files */
125 static int allfiles; /* generate all files */
126 int tirpcflag = 1; /* generating code for tirpc, by default */
127 xdrfunc *xdrfunc_head = NULL; /* xdr function list */
128 xdrfunc *xdrfunc_tail = NULL; /* xdr function list */
133 main(int argc, const char *argv[])
135 struct commandline cmd;
137 (void) memset((char *)&cmd, 0, sizeof (struct commandline));
138 if (!parseargs(argc, argv, &cmd))
141 * Only the client and server side stubs are likely to be customized,
142 * so in that case only, check if the outfile exists, and if so,
143 * print an error message and exit.
145 if (cmd.Ssflag || cmd.Scflag || cmd.makefileflag) {
146 checkfiles(cmd.infile, cmd.outfile);
149 checkfiles(cmd.infile, NULL);
152 c_output(cmd.infile, "-DRPC_XDR", DONT_EXTEND, cmd.outfile);
153 } else if (cmd.hflag) {
154 h_output(cmd.infile, "-DRPC_HDR", DONT_EXTEND, cmd.outfile,
156 } else if (cmd.lflag) {
157 l_output(cmd.infile, "-DRPC_CLNT", DONT_EXTEND, cmd.outfile);
158 } else if (cmd.sflag || cmd.mflag || (cmd.nflag)) {
159 s_output(argc, argv, cmd.infile, "-DRPC_SVC", DONT_EXTEND,
160 cmd.outfile, cmd.mflag, cmd.nflag);
161 } else if (cmd.tflag) {
162 t_output(cmd.infile, "-DRPC_TBL", DONT_EXTEND, cmd.outfile);
163 } else if (cmd.Ssflag) {
164 svc_output(cmd.infile, "-DRPC_SERVER", DONT_EXTEND,
166 } else if (cmd.Scflag) {
167 clnt_output(cmd.infile, "-DRPC_CLIENT", DONT_EXTEND,
169 } else if (cmd.makefileflag) {
172 /* the rescans are required, since cpp may effect input */
173 c_output(cmd.infile, "-DRPC_XDR", EXTEND, "_xdr.c");
175 h_output(cmd.infile, "-DRPC_HDR", EXTEND, ".h", cmd.hflag);
177 l_output(cmd.infile, "-DRPC_CLNT", EXTEND, "_clnt.c");
179 if (inetdflag || !tirpcflag)
180 s_output(allc, allv, cmd.infile, "-DRPC_SVC", EXTEND,
181 "_svc.c", cmd.mflag, cmd.nflag);
183 s_output(allnc, allnv, cmd.infile, "-DRPC_SVC",
184 EXTEND, "_svc.c", cmd.mflag, cmd.nflag);
187 t_output(cmd.infile, "-DRPC_TBL", EXTEND, "_tbl.i");
192 svc_output(cmd.infile, "-DRPC_SERVER", EXTEND,
195 clnt_output(cmd.infile, "-DRPC_CLIENT", EXTEND,
199 if (allfiles || (cmd.makefileflag == 1)){
205 exit(nonfatalerrors);
211 * add extension to filename
214 extendfile(const char *path, const char *ext)
220 if ((file = strrchr(path, '/')) == NULL)
224 res = xmalloc(strlen(file) + strlen(ext) + 1);
225 p = strrchr(file, '.');
227 p = file + strlen(file);
229 (void) strcpy(res, file);
230 (void) strcpy(res + (p - file), ext);
235 * Open output file with given extension
238 open_output(const char *infile, const char *outfile)
241 if (outfile == NULL) {
246 if (infile != NULL && streq(outfile, infile)) {
247 warnx("%s already exists. No output generated", infile);
250 fout = fopen(outfile, "w");
252 warn("unable to open %s", outfile);
255 record_open(outfile);
263 f_print(fout, "/*\n");
264 f_print(fout, " * Please do not edit this file.\n");
265 f_print(fout, " * It was generated using rpcgen.\n");
266 f_print(fout, " */\n\n");
269 /* prepend C-preprocessor and flags before arguments */
275 char *dupvar, *s, *t;
279 else if ((var = getenv("RPCGEN_CPP")) == NULL)
280 insarg(0, "/usr/bin/cpp");
282 /* Parse command line in a rudimentary way */
283 dupvar = xstrdup(var);
284 for (s = dupvar, idx = 0; (t = strsep(&s, " \t")) != NULL; ) {
291 insarg(idx, CPPFLAGS);
295 * Open input file with given define for C-preprocessor
298 open_input(const char *infile, const char *define)
302 infilename = (infile == NULL) ? "<stdin>" : infile;
304 switch (childpid = fork()) {
310 addarg((char *)NULL);
312 (void) dup2(pd[1], 1);
314 execvp(arglist[0], arglist);
315 err(1, "execvp %s", arglist[0]);
320 fin = fdopen(pd[0], "r");
322 warn("%s", infilename);
327 /* valid tirpc nettypes */
328 static const char *valid_ti_nettypes[] =
342 /* valid inetd nettypes */
343 static const char *valid_i_nettypes[] =
351 check_nettype(const char *name, const char *list_to_check[])
354 for (i = 0; list_to_check[i] != NULL; i++) {
355 if (strcmp(name, list_to_check[i]) == 0) {
359 warnx("illegal nettype :\'%s\'", name);
364 file_name(const char *file, const char *ext)
367 temp = extendfile(file, ext);
369 if (access(temp, F_OK) != -1)
378 c_output(const char *infile, const char *define, int extend, const char *outfile)
382 const char *outfilename;
386 open_input(infile, define);
387 outfilename = extend ? extendfile(infile, outfile) : outfile;
388 open_output(infile, outfilename);
390 if (infile && (include = extendfile(infile, ".h"))) {
391 f_print(fout, "#include \"%s\"\n", include);
393 /* .h file already contains rpc/rpc.h */
395 f_print(fout, "#include <rpc/rpc.h>\n");
397 while ( (def = get_definition()) ) {
400 if (extend && tell == ftell(fout)) {
401 (void) unlink(outfilename);
410 /* add all the starting basic types */
413 add_type(1, "short");
415 add_type(1, "u_int");
416 add_type(1, "u_long");
417 add_type(1, "u_short");
421 static const char rpcgen_table_dcl[] = "struct rpcgen_table {\n\
423 xdrproc_t xdr_arg; \n\
424 unsigned len_arg; \n\
425 xdrproc_t xdr_res; \n\
426 unsigned len_res; \n\
431 generate_guard(const char *pathname)
433 const char *filename;
434 char *guard, *tmp, *stopat;
436 filename = strrchr(pathname, '/'); /* find last component */
437 filename = ((filename == NULL) ? pathname : filename+1);
438 guard = xstrdup(filename);
439 stopat = strrchr(guard, '.');
442 * Convert to a valid C macro name and make it upper case.
443 * Map macro unfriendly characterss to '_'.
445 for (tmp = guard; *tmp != '\000'; ++tmp) {
447 *tmp = toupper(*tmp);
448 else if (isupper(*tmp) || *tmp == '_')
450 else if (tmp == guard)
452 else if (isdigit(*tmp))
453 /* OK for all but first character */;
454 else if (tmp == stopat) {
461 * Can't have a '_' in front, because it'll end up being "__".
462 * "__" macros shoudln't be used. So, remove all of the
463 * '_' characters from the front.
466 for (tmp = guard; *tmp == '_'; ++tmp)
471 guard = extendfile(guard, "_H_RPCGEN");
477 * Compile into an XDR header file
482 h_output(const char *infile, const char *define, int extend, const char *outfile, int headeronly)
485 const char *outfilename;
492 open_input(infile, define);
493 outfilename = extend ? extendfile(infile, outfile) : outfile;
494 open_output(infile, outfilename);
496 if (outfilename || infile){
497 guard = tmp = generate_guard(outfilename ? outfilename: infile);
501 f_print(fout, "#ifndef _%s\n#define _%s\n\n", guard,
504 f_print(fout, "#include <rpc/rpc.h>\n");
507 f_print(fout, "#include <pthread.h>\n");
509 /* put the C++ support */
511 f_print(fout, "\n#ifdef __cplusplus\n");
512 f_print(fout, "extern \"C\" {\n");
513 f_print(fout, "#endif\n\n");
516 /* put in a typedef for quadprecision. Only with Cflag */
520 /* print data definitions */
521 while ( (def = get_definition()) ) {
522 print_datadef(def, headeronly);
526 * print function declarations.
527 * Do this after data definitions because they might be used as
528 * arguments for functions
530 for (l = defined; l != NULL; l = l->next) {
531 print_funcdef(l->val, headeronly);
533 /* Now print all xdr func declarations */
534 if (xdrfunc_head != NULL){
537 "\n/* the xdr functions */\n");
540 f_print(fout, "\n#ifdef __cplusplus\n");
541 f_print(fout, "extern \"C\" {\n");
542 f_print(fout, "#endif\n");
545 xdrfuncp = xdrfunc_head;
546 while (xdrfuncp != NULL){
547 print_xdr_func_def(xdrfuncp->name, xdrfuncp->pointerp);
548 xdrfuncp = xdrfuncp->next;
552 if (extend && tell == ftell(fout)) {
553 (void) unlink(outfilename);
554 } else if (tblflag) {
555 f_print(fout, rpcgen_table_dcl);
558 f_print(fout, "\n#ifdef __cplusplus\n");
559 f_print(fout, "}\n");
560 f_print(fout, "#endif\n");
562 f_print(fout, "\n#endif /* !_%s */\n", guard);
567 * Compile into an RPC service
570 s_output(int argc, const char *argv[], const char *infile, const char *define,
571 int extend, const char *outfile, int nomain, int netflag)
575 int foundprogram = 0;
576 const char *outfilename;
578 open_input(infile, define);
579 outfilename = extend ? extendfile(infile, outfile) : outfile;
580 open_output(infile, outfilename);
582 if (infile && (include = extendfile(infile, ".h"))) {
583 f_print(fout, "#include \"%s\"\n", include);
586 f_print(fout, "#include <rpc/rpc.h>\n");
588 f_print(fout, "#include <stdio.h>\n");
589 f_print(fout, "#include <stdlib.h> /* getenv, exit */\n");
590 f_print (fout, "#include <rpc/pmap_clnt.h> /* for pmap_unset */\n");
591 f_print (fout, "#include <string.h> /* strcmp */\n");
593 f_print(fout, "#include <rpc/rpc_com.h>\n");
594 if (strcmp(svcclosetime, "-1") == 0)
596 else if (strcmp(svcclosetime, "0") == 0)
598 else if (inetdflag || pmflag) {
599 f_print(fout, "#include <signal.h>\n");
603 if (!tirpcflag && inetdflag)
604 f_print(fout, "#include <sys/ttycom.h> /* TIOCNOTTY */\n");
605 if (inetdflag || pmflag) {
606 f_print(fout, "#ifdef __cplusplus\n");
608 "#include <sys/sysent.h> /* getdtablesize, open */\n");
609 f_print(fout, "#endif /* __cplusplus */\n");
612 f_print(fout, "#include <fcntl.h> /* open */\n");
613 f_print(fout, "#include <unistd.h> /* fork / setsid */\n");
614 f_print(fout, "#include <sys/types.h>\n");
617 f_print(fout, "#include <string.h>\n");
618 if (inetdflag || !tirpcflag) {
619 f_print(fout, "#include <sys/socket.h>\n");
620 f_print(fout, "#include <netinet/in.h>\n");
623 if ((netflag || pmflag) && tirpcflag && !nomain) {
624 f_print(fout, "#include <netconfig.h>\n");
627 f_print(fout, "#include <sys/resource.h> /* rlimit */\n");
628 if (logflag || inetdflag || pmflag || tirpcflag)
629 f_print(fout, "#include <syslog.h>\n");
631 f_print(fout, "\n#ifdef DEBUG\n#define RPC_SVC_FG\n#endif\n");
633 f_print(fout, "\n#define _RPCSVC_CLOSEDOWN %s\n",
635 while ( (def = get_definition()) ) {
636 foundprogram |= (def->def_kind == DEF_PROGRAM);
638 if (extend && !foundprogram) {
639 (void) unlink(outfilename);
642 write_most(infile, netflag, nomain);
644 if (!do_registers(argc, argv)) {
646 (void) unlink(outfilename);
654 * generate client side stubs
657 l_output(const char *infile, const char *define, int extend, const char *outfile)
661 int foundprogram = 0;
662 const char *outfilename;
664 open_input(infile, define);
665 outfilename = extend ? extendfile(infile, outfile) : outfile;
666 open_output(infile, outfilename);
668 f_print (fout, "#include <string.h> /* for memset */\n");
669 if (infile && (include = extendfile(infile, ".h"))) {
670 f_print(fout, "#include \"%s\"\n", include);
673 f_print(fout, "#include <rpc/rpc.h>\n");
674 while ( (def = get_definition()) ) {
675 foundprogram |= (def->def_kind == DEF_PROGRAM);
677 if (extend && !foundprogram) {
678 (void) unlink(outfilename);
685 * generate the dispatch table
688 t_output(const char *infile, const char *define, int extend, const char *outfile)
691 int foundprogram = 0;
692 const char *outfilename;
694 open_input(infile, define);
695 outfilename = extend ? extendfile(infile, outfile) : outfile;
696 open_output(infile, outfilename);
698 while ( (def = get_definition()) ) {
699 foundprogram |= (def->def_kind == DEF_PROGRAM);
701 if (extend && !foundprogram) {
702 (void) unlink(outfilename);
708 /* sample routine for the server template */
710 svc_output(const char *infile, const char *define, int extend, const char *outfile)
714 const char *outfilename;
716 open_input(infile, define);
717 outfilename = extend ? extendfile(infile, outfile) : outfile;
718 checkfiles(infile, outfilename);
720 * Check if outfile already exists.
721 * if so, print an error message and exit
723 open_output(infile, outfilename);
726 if (infile && (include = extendfile(infile, ".h"))) {
727 f_print(fout, "#include \"%s\"\n", include);
730 f_print(fout, "#include <rpc/rpc.h>\n");
733 while ( (def = get_definition()) ) {
734 write_sample_svc(def);
736 if (extend && tell == ftell(fout)) {
737 (void) unlink(outfilename);
741 /* sample main routine for client */
743 clnt_output(const char *infile, const char *define, int extend, const char *outfile)
747 const char *outfilename;
751 open_input(infile, define);
752 outfilename = extend ? extendfile(infile, outfile) : outfile;
753 checkfiles(infile, outfilename);
755 * Check if outfile already exists.
756 * if so, print an error message and exit
759 open_output(infile, outfilename);
761 if (infile && (include = extendfile(infile, ".h"))) {
762 f_print(fout, "#include \"%s\"\n", include);
765 f_print(fout, "#include <rpc/rpc.h>\n");
766 f_print(fout, "#include <stdio.h>\n");
767 f_print(fout, "#include <stdlib.h>\n");
769 while ( (def = get_definition()) ) {
770 has_program += write_sample_clnt(def);
774 write_sample_clnt_main();
776 if (extend && tell == ftell(fout)) {
777 (void) unlink(outfilename);
782 static void mkfile_output(struct commandline *cmd)
784 const char *mkfilename, *clientname, *clntname, *xdrname, *hdrname;
785 const char *servername, *svcname, *servprogname, *clntprogname;
786 char *temp, *mkftemp;
788 svcname = file_name(cmd->infile, "_svc.c");
789 clntname = file_name(cmd->infile, "_clnt.c");
790 xdrname = file_name(cmd->infile, "_xdr.c");
791 hdrname = file_name(cmd->infile, ".h");
795 servername = extendfile(cmd->infile, "_server.c");
796 clientname = extendfile(cmd->infile, "_client.c");
801 servprogname = extendfile(cmd->infile, "_server");
802 clntprogname = extendfile(cmd->infile, "_client");
805 mkftemp = xmalloc(strlen("makefile.") +
806 strlen(cmd->infile) + 1);
807 temp = strrchr(cmd->infile, '.');
808 strcpy(mkftemp, "makefile.");
809 (void) strncat(mkftemp, cmd->infile,
810 (temp - cmd->infile));
811 mkfilename = mkftemp;
813 mkfilename = cmd->outfile;
816 checkfiles(NULL, mkfilename);
817 open_output(NULL, mkfilename);
819 f_print(fout, "\n# This is a template makefile generated\
822 f_print(fout, "\n# Parameters \n\n");
824 f_print(fout, "CLIENT = %s\nSERVER = %s\n\n",
825 clntprogname, servprogname);
826 f_print(fout, "SOURCES_CLNT.c = \nSOURCES_CLNT.h = \n");
827 f_print(fout, "SOURCES_SVC.c = \nSOURCES_SVC.h = \n");
828 f_print(fout, "SOURCES.x = %s\n\n", cmd->infile);
829 f_print(fout, "TARGETS_SVC.c = %s %s %s \n",
830 svcname, servername, xdrname);
831 f_print(fout, "TARGETS_CLNT.c = %s %s %s \n",
832 clntname, clientname, xdrname);
833 f_print(fout, "TARGETS = %s %s %s %s %s %s\n\n",
834 hdrname, xdrname, clntname,
835 svcname, clientname, servername);
837 f_print(fout, "OBJECTS_CLNT = $(SOURCES_CLNT.c:%%.c=%%.o) \
838 $(TARGETS_CLNT.c:%%.c=%%.o) ");
840 f_print(fout, "\nOBJECTS_SVC = $(SOURCES_SVC.c:%%.c=%%.o) \
841 $(TARGETS_SVC.c:%%.c=%%.o) ");
844 f_print(fout, "\n# Compiler flags \n");
846 f_print(fout, "\nCFLAGS += -D_REENTRANT -D_THEAD_SAFE \nLDLIBS += -pthread\n");
848 f_print(fout, "RPCGENFLAGS = \n");
850 f_print(fout, "\n# Targets \n\n");
852 f_print(fout, "all : $(CLIENT) $(SERVER)\n\n");
853 f_print(fout, "$(TARGETS) : $(SOURCES.x) \n");
854 f_print(fout, "\trpcgen $(RPCGENFLAGS) $(SOURCES.x)\n\n");
856 f_print(fout, "\trpcgen -Sc $(RPCGENFLAGS) $(SOURCES.x) -o %s\n\n", clientname);
857 f_print(fout, "\trpcgen -Ss $(RPCGENFLAGS) $(SOURCES.x) -o %s\n\n", servername);
859 f_print(fout, "$(OBJECTS_CLNT) : $(SOURCES_CLNT.c) $(SOURCES_CLNT.h) \
860 $(TARGETS_CLNT.c) \n\n");
862 f_print(fout, "$(OBJECTS_SVC) : $(SOURCES_SVC.c) $(SOURCES_SVC.h) \
863 $(TARGETS_SVC.c) \n\n");
864 f_print(fout, "$(CLIENT) : $(OBJECTS_CLNT) \n");
865 f_print(fout, "\t$(CC) -o $(CLIENT) $(OBJECTS_CLNT) \
867 f_print(fout, "$(SERVER) : $(OBJECTS_SVC) \n");
868 f_print(fout, "\t$(CC) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS)\n\n");
869 f_print(fout, "clean:\n\t rm -f core $(TARGETS) $(OBJECTS_CLNT) \
870 $(OBJECTS_SVC) $(CLIENT) $(SERVER)\n\n");
876 * Perform registrations for service output
877 * Return 0 if failed; 1 otherwise.
880 do_registers(int argc, const char *argv[])
884 if (inetdflag || !tirpcflag) {
885 for (i = 1; i < argc; i++) {
886 if (streq(argv[i], "-s")) {
887 if (!check_nettype(argv[i + 1],
890 write_inetd_register(argv[i + 1]);
895 for (i = 1; i < argc; i++)
896 if (streq(argv[i], "-s")) {
897 if (!check_nettype(argv[i + 1],
900 write_nettype_register(argv[i + 1]);
902 } else if (streq(argv[i], "-n")) {
903 write_netid_register(argv[i + 1]);
911 * Extend the argument list
918 argmax = argmax == 0 ? 32 : argmax << 1;
919 if (argmax > INT_MAX / 4) {
920 warnx("refusing to allocate too many arguments");
923 newarglist = realloc(arglist, argmax * sizeof(*arglist));
924 if (newarglist == NULL) {
925 warnx("unable to allocate arglist");
929 arglist = newarglist;
933 * Add another argument to the arg list
936 addarg(const char *cp)
938 if (argcount >= argmax)
942 arglist[argcount++] = xstrdup(cp);
944 arglist[argcount++] = NULL;
948 * Insert an argument at the specified location
951 insarg(int place, const char *cp)
955 if (argcount >= argmax)
958 /* Move up existing arguments */
959 for (i = argcount - 1; i >= place; i--)
960 arglist[i + 1] = arglist[i];
962 arglist[place] = xstrdup(cp);
967 * if input file is stdin and an output file is specified then complain
968 * if the file already exists. Otherwise the file may get overwritten
969 * If input file does not exist, exit with an error
973 checkfiles(const char *infile, const char *outfile)
978 if (infile) /* infile ! = NULL */
979 if (stat(infile, &buf) < 0)
985 if (stat(outfile, &buf) < 0)
986 return; /* file does not exist */
988 warnx("file '%s' already exists and may be overwritten", outfile);
995 * Parse command line arguments
998 parseargs(int argc, const char *argv[], struct commandline *cmd)
1003 char flag[(1 << 8 * sizeof (char))];
1006 cmd->infile = cmd->outfile = NULL;
1023 for (i = 1; i < argc; i++) {
1024 if (argv[i][0] != '-') {
1026 warnx("cannot specify more than one input file");
1029 cmd->infile = argv[i];
1031 for (j = 1; argv[i][j] != 0; j++) {
1049 * sample flag: Ss or Sc.
1050 * Ss means set flag['S'];
1051 * Sc means set flag['C'];
1052 * Sm means set flag['M'];
1054 ch = argv[i][++j]; /* get next char */
1064 if (flag[(int)ch]) {
1069 case 'C': /* ANSI C syntax */
1070 ch = argv[i][j+1]; /* get next char */
1078 * Turn TIRPC flag off for
1079 * generating backward compatible
1101 svcclosetime = argv[i];
1113 inline_size = atoi(argv[i]);
1118 if (argv[i][j - 1] != '-' ||
1119 argv[i][j + 1] != 0) {
1130 cmd->outfile = argv[i];
1134 if (argv[i][j - 1] != '-') {
1137 (void) addarg(argv[i]);
1143 if (strlcpy(pathbuf, argv[i],
1144 sizeof(pathbuf)) >= sizeof(pathbuf)
1145 || strlcat(pathbuf, "/cpp",
1148 warnx("argument too long");
1165 cmd->cflag = flag['c'];
1166 cmd->hflag = flag['h'];
1167 cmd->lflag = flag['l'];
1168 cmd->mflag = flag['m'];
1169 cmd->nflag = flag['n'];
1170 cmd->sflag = flag['s'];
1171 cmd->tflag = flag['t'];
1172 cmd->Ssflag = flag['S'];
1173 cmd->Scflag = flag['C'];
1174 cmd->makefileflag = flag['M'];
1179 if ((inetdflag && cmd->nflag)) {
1180 /* netid not allowed with inetdflag */
1181 warnx("cannot use netid flag with inetd flag");
1184 } else { /* 4.1 mode */
1185 pmflag = 0; /* set pmflag only in tirpcmode */
1186 if (cmd->nflag) { /* netid needs TIRPC */
1187 warnx("cannot use netid flag without TIRPC");
1192 if (newstyle && (tblflag || cmd->tflag)) {
1193 warnx("cannot use table flags with newstyle");
1197 /* check no conflicts with file generation flags */
1198 nflags = cmd->cflag + cmd->hflag + cmd->lflag + cmd->mflag +
1199 cmd->sflag + cmd->nflag + cmd->tflag + cmd->Ssflag +
1200 cmd->Scflag + cmd->makefileflag;
1203 if (cmd->outfile != NULL || cmd->infile == NULL) {
1206 } else if (cmd->infile == NULL &&
1207 (cmd->Ssflag || cmd->Scflag || cmd->makefileflag)) {
1208 warnx("\"infile\" is required for template generation flags");
1211 warnx("cannot have more than one file generation flag");
1220 f_print(stderr, "%s\n%s\n%s\n%s\n%s\n",
1221 "usage: rpcgen infile",
1222 " rpcgen [-abCLNTM] [-Dname[=value]] [-i size]\
1223 [-I -P [-K seconds]] [-Y path] infile",
1224 " rpcgen [-c | -h | -l | -m | -t | -Sc | -Ss | -Sm]\
1225 [-o outfile] [infile]",
1226 " rpcgen [-s nettype]* [-o outfile] [infile]",
1227 " rpcgen [-n netid]* [-o outfile] [infile]");
1235 f_print(stderr, "options:\n");
1236 f_print(stderr, "-a\t\tgenerate all files, including samples\n");
1237 f_print(stderr, "-b\t\tbackward compatibility mode (generates code \
1238 for FreeBSD 4.X)\n");
1239 f_print(stderr, "-c\t\tgenerate XDR routines\n");
1240 f_print(stderr, "-C\t\tANSI C mode\n");
1241 f_print(stderr, "-Dname[=value]\tdefine a symbol (same as #define)\n");
1242 f_print(stderr, "-h\t\tgenerate header file\n");
1243 f_print(stderr, "-i size\t\tsize at which to start generating\
1245 f_print(stderr, "-I\t\tgenerate code for inetd support in server\n");
1246 f_print(stderr, "-K seconds\tserver exits after K seconds of\
1248 f_print(stderr, "-l\t\tgenerate client side stubs\n");
1249 f_print(stderr, "-L\t\tserver errors will be printed to syslog\n");
1250 f_print(stderr, "-m\t\tgenerate server side stubs\n");
1251 f_print(stderr, "-M\t\tgenerate MT-safe code\n");
1252 f_print(stderr, "-n netid\tgenerate server code that supports\
1254 f_print(stderr, "-N\t\tsupports multiple arguments and\
1256 f_print(stderr, "-o outfile\tname of the output file\n");
1257 f_print(stderr, "-P\t\tgenerate code for port monitoring support in server\n");
1258 f_print(stderr, "-s nettype\tgenerate server code that supports named\
1260 f_print(stderr, "-Sc\t\tgenerate sample client code that uses remote\
1262 f_print(stderr, "-Ss\t\tgenerate sample server code that defines\
1263 remote procedures\n");
1264 f_print(stderr, "-Sm \t\tgenerate makefile template \n");
1266 f_print(stderr, "-t\t\tgenerate RPC dispatch table\n");
1267 f_print(stderr, "-T\t\tgenerate code to support RPC dispatch tables\n");
1268 f_print(stderr, "-Y path\t\tpath where cpp is found\n");