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>
40 * rpc_main.c, Top level of the RPC protocol compiler.
41 * Copyright (C) 1987, Sun Microsystems, Inc.
50 #include <sys/types.h>
51 #include <sys/param.h>
54 #include "rpc_parse.h"
58 static void c_output(const char *, const char *, int, const char *);
59 static void h_output(const char *, const char *, int, const char *, int);
60 static void l_output(const char *, const char *, int, const char *);
61 static void t_output(const char *, const char *, int, const char *);
62 static void clnt_output(const char *, const char *, int, const char * );
63 static char *generate_guard(const char *);
64 static void c_initialize(void);
66 static void usage(void) __dead2;
67 static void options_usage(void);
68 static int do_registers(int, const char **);
69 static int parseargs(int, const char **, struct commandline *);
70 static void svc_output(const char *, const char *, int, const char *);
71 static void mkfile_output(struct commandline *);
72 static void s_output(int, const char **, const char *, const char *, int, const char *, int, int);
74 #define EXTEND 1 /* alias for TRUE */
75 #define DONT_EXTEND 0 /* alias for FALSE */
77 static const char *svcclosetime = "120";
78 static const char *CPP = NULL;
79 static const char CPPFLAGS[] = "-C";
80 static char pathbuf[MAXPATHLEN + 1];
81 static const char *allv[] = {
82 "rpcgen", "-s", "udp", "-s", "tcp",
84 static int allc = nitems(allv);
85 static const char *allnv[] = {
86 "rpcgen", "-s", "netpath",
88 static int allnc = nitems(allnv);
91 * machinations for handling expanding argument list
93 static void addarg(const char *); /* add another argument to the list */
94 static void insarg(int, const char *); /* insert arg at specified location */
95 static void checkfiles(const char *, const char *);
96 /* check if out file already exists */
98 static char **arglist;
99 static int argcount = 0;
100 static int argmax = 0;
102 int nonfatalerrors; /* errors */
103 int inetdflag = 0; /* Support for inetd is disabled by default, use -I */
104 int pmflag = 0; /* Support for port monitors is disabled by default */
105 int tirpc_socket = 1; /* TI-RPC on socket, no TLI library */
106 int logflag; /* Use syslog instead of fprintf for errors */
107 int tblflag; /* Support for dispatch table file */
108 int mtflag = 0; /* Support for MT */
111 /* length at which to start doing an inline */
113 int inline_size = INLINE;
115 * Length at which to start doing an inline. INLINE = default
116 * if 0, no xdr_inline code
119 int indefinitewait; /* If started by port monitors, hang till it wants */
120 int exitnow; /* If started by port monitors, exit after the call */
121 int timerflag; /* TRUE if !indefinite && !exitnow */
122 int newstyle; /* newstyle of passing arguments (by value) */
123 int CCflag = 0; /* C++ files */
124 static int allfiles; /* generate all files */
125 int tirpcflag = 1; /* generating code for tirpc, by default */
126 xdrfunc *xdrfunc_head = NULL; /* xdr function list */
127 xdrfunc *xdrfunc_tail = NULL; /* xdr function list */
132 main(int argc, const char *argv[])
134 struct commandline cmd;
136 (void) memset((char *)&cmd, 0, sizeof (struct commandline));
137 if (!parseargs(argc, argv, &cmd))
140 * Only the client and server side stubs are likely to be customized,
141 * so in that case only, check if the outfile exists, and if so,
142 * print an error message and exit.
144 if (cmd.Ssflag || cmd.Scflag || cmd.makefileflag) {
145 checkfiles(cmd.infile, cmd.outfile);
148 checkfiles(cmd.infile, NULL);
151 c_output(cmd.infile, "-DRPC_XDR", DONT_EXTEND, cmd.outfile);
152 } else if (cmd.hflag) {
153 h_output(cmd.infile, "-DRPC_HDR", DONT_EXTEND, cmd.outfile,
155 } else if (cmd.lflag) {
156 l_output(cmd.infile, "-DRPC_CLNT", DONT_EXTEND, cmd.outfile);
157 } else if (cmd.sflag || cmd.mflag || (cmd.nflag)) {
158 s_output(argc, argv, cmd.infile, "-DRPC_SVC", DONT_EXTEND,
159 cmd.outfile, cmd.mflag, cmd.nflag);
160 } else if (cmd.tflag) {
161 t_output(cmd.infile, "-DRPC_TBL", DONT_EXTEND, cmd.outfile);
162 } else if (cmd.Ssflag) {
163 svc_output(cmd.infile, "-DRPC_SERVER", DONT_EXTEND,
165 } else if (cmd.Scflag) {
166 clnt_output(cmd.infile, "-DRPC_CLIENT", DONT_EXTEND,
168 } else if (cmd.makefileflag) {
171 /* the rescans are required, since cpp may effect input */
172 c_output(cmd.infile, "-DRPC_XDR", EXTEND, "_xdr.c");
174 h_output(cmd.infile, "-DRPC_HDR", EXTEND, ".h", cmd.hflag);
176 l_output(cmd.infile, "-DRPC_CLNT", EXTEND, "_clnt.c");
178 if (inetdflag || !tirpcflag)
179 s_output(allc, allv, cmd.infile, "-DRPC_SVC", EXTEND,
180 "_svc.c", cmd.mflag, cmd.nflag);
182 s_output(allnc, allnv, cmd.infile, "-DRPC_SVC",
183 EXTEND, "_svc.c", cmd.mflag, cmd.nflag);
186 t_output(cmd.infile, "-DRPC_TBL", EXTEND, "_tbl.i");
191 svc_output(cmd.infile, "-DRPC_SERVER", EXTEND,
194 clnt_output(cmd.infile, "-DRPC_CLIENT", EXTEND,
198 if (allfiles || (cmd.makefileflag == 1)){
204 exit(nonfatalerrors);
210 * add extension to filename
213 extendfile(const char *path, const char *ext)
219 if ((file = strrchr(path, '/')) == NULL)
223 res = xmalloc(strlen(file) + strlen(ext) + 1);
224 p = strrchr(file, '.');
226 p = file + strlen(file);
228 (void) strcpy(res, file);
229 (void) strcpy(res + (p - file), ext);
234 * Open output file with given extension
237 open_output(const char *infile, const char *outfile)
240 if (outfile == NULL) {
245 if (infile != NULL && streq(outfile, infile)) {
246 warnx("%s already exists. No output generated", infile);
249 fout = fopen(outfile, "w");
251 warn("unable to open %s", outfile);
254 record_open(outfile);
262 f_print(fout, "/*\n");
263 f_print(fout, " * Please do not edit this file.\n");
264 f_print(fout, " * It was generated using rpcgen.\n");
265 f_print(fout, " */\n\n");
268 /* prepend C-preprocessor and flags before arguments */
274 char *dupvar, *t, *word;
278 else if ((var = getenv("RPCGEN_CPP")) == NULL)
279 insarg(idx++, "/usr/bin/cpp");
282 * Parse command line like a shell (but only handle whitespace,
283 * quotes and backslash).
285 dupvar = malloc(strlen(var) + 1);
288 for (s = var, t = dupvar; *s; ++s) {
375 errx(1, "RPCGEN_CPP: unterminated %c",
376 quoted == 1 ? '\'' : '"');
384 insarg(idx, CPPFLAGS);
388 * Open input file with given define for C-preprocessor
391 open_input(const char *infile, const char *define)
395 infilename = (infile == NULL) ? "<stdin>" : infile;
397 switch (childpid = fork()) {
403 addarg((char *)NULL);
405 (void) dup2(pd[1], 1);
407 execvp(arglist[0], arglist);
408 err(1, "execvp %s", arglist[0]);
413 fin = fdopen(pd[0], "r");
415 warn("%s", infilename);
420 /* valid tirpc nettypes */
421 static const char *valid_ti_nettypes[] =
435 /* valid inetd nettypes */
436 static const char *valid_i_nettypes[] =
444 check_nettype(const char *name, const char *list_to_check[])
447 for (i = 0; list_to_check[i] != NULL; i++) {
448 if (strcmp(name, list_to_check[i]) == 0) {
452 warnx("illegal nettype :\'%s\'", name);
457 file_name(const char *file, const char *ext)
460 temp = extendfile(file, ext);
462 if (access(temp, F_OK) != -1)
471 c_output(const char *infile, const char *define, int extend, const char *outfile)
475 const char *outfilename;
479 open_input(infile, define);
480 outfilename = extend ? extendfile(infile, outfile) : outfile;
481 open_output(infile, outfilename);
483 if (infile && (include = extendfile(infile, ".h"))) {
484 f_print(fout, "#include \"%s\"\n", include);
486 /* .h file already contains rpc/rpc.h */
488 f_print(fout, "#include <rpc/rpc.h>\n");
490 while ( (def = get_definition()) ) {
493 if (extend && tell == ftell(fout)) {
494 (void) unlink(outfilename);
503 /* add all the starting basic types */
506 add_type(1, "short");
508 add_type(1, "u_int");
509 add_type(1, "u_long");
510 add_type(1, "u_short");
514 static const char rpcgen_table_dcl[] = "struct rpcgen_table {\n\
516 xdrproc_t xdr_arg; \n\
517 unsigned len_arg; \n\
518 xdrproc_t xdr_res; \n\
519 unsigned len_res; \n\
524 generate_guard(const char *pathname)
526 const char *filename;
527 char *guard, *tmp, *stopat;
529 filename = strrchr(pathname, '/'); /* find last component */
530 filename = ((filename == NULL) ? pathname : filename+1);
531 guard = xstrdup(filename);
532 stopat = strrchr(guard, '.');
535 * Convert to a valid C macro name and make it upper case.
536 * Map macro unfriendly characterss to '_'.
538 for (tmp = guard; *tmp != '\000'; ++tmp) {
540 *tmp = toupper(*tmp);
541 else if (isupper(*tmp) || *tmp == '_')
543 else if (tmp == guard)
545 else if (isdigit(*tmp))
546 /* OK for all but first character */;
547 else if (tmp == stopat) {
554 * Can't have a '_' in front, because it'll end up being "__".
555 * "__" macros shoudln't be used. So, remove all of the
556 * '_' characters from the front.
559 for (tmp = guard; *tmp == '_'; ++tmp)
564 guard = extendfile(guard, "_H_RPCGEN");
570 * Compile into an XDR header file
575 h_output(const char *infile, const char *define, int extend, const char *outfile, int headeronly)
578 const char *outfilename;
585 open_input(infile, define);
586 outfilename = extend ? extendfile(infile, outfile) : outfile;
587 open_output(infile, outfilename);
589 if (outfilename || infile){
590 guard = tmp = generate_guard(outfilename ? outfilename: infile);
594 f_print(fout, "#ifndef _%s\n#define _%s\n\n", guard,
597 f_print(fout, "#include <rpc/rpc.h>\n");
600 f_print(fout, "#include <pthread.h>\n");
602 /* put the C++ support */
604 f_print(fout, "\n#ifdef __cplusplus\n");
605 f_print(fout, "extern \"C\" {\n");
606 f_print(fout, "#endif\n\n");
609 /* put in a typedef for quadprecision. Only with Cflag */
613 /* print data definitions */
614 while ( (def = get_definition()) ) {
615 print_datadef(def, headeronly);
619 * print function declarations.
620 * Do this after data definitions because they might be used as
621 * arguments for functions
623 for (l = defined; l != NULL; l = l->next) {
624 print_funcdef(l->val, headeronly);
626 /* Now print all xdr func declarations */
627 if (xdrfunc_head != NULL){
630 "\n/* the xdr functions */\n");
633 f_print(fout, "\n#ifdef __cplusplus\n");
634 f_print(fout, "extern \"C\" {\n");
635 f_print(fout, "#endif\n");
638 xdrfuncp = xdrfunc_head;
639 while (xdrfuncp != NULL){
640 print_xdr_func_def(xdrfuncp->name, xdrfuncp->pointerp);
641 xdrfuncp = xdrfuncp->next;
645 if (extend && tell == ftell(fout)) {
646 (void) unlink(outfilename);
647 } else if (tblflag) {
648 f_print(fout, rpcgen_table_dcl);
651 f_print(fout, "\n#ifdef __cplusplus\n");
652 f_print(fout, "}\n");
653 f_print(fout, "#endif\n");
655 f_print(fout, "\n#endif /* !_%s */\n", guard);
660 * Compile into an RPC service
663 s_output(int argc, const char *argv[], const char *infile, const char *define,
664 int extend, const char *outfile, int nomain, int netflag)
668 int foundprogram = 0;
669 const char *outfilename;
671 open_input(infile, define);
672 outfilename = extend ? extendfile(infile, outfile) : outfile;
673 open_output(infile, outfilename);
675 if (infile && (include = extendfile(infile, ".h"))) {
676 f_print(fout, "#include \"%s\"\n", include);
679 f_print(fout, "#include <rpc/rpc.h>\n");
681 f_print(fout, "#include <stdio.h>\n");
682 f_print(fout, "#include <stdlib.h> /* getenv, exit */\n");
683 f_print (fout, "#include <rpc/pmap_clnt.h> /* for pmap_unset */\n");
684 f_print (fout, "#include <string.h> /* strcmp */\n");
686 f_print(fout, "#include <rpc/rpc_com.h>\n");
687 if (strcmp(svcclosetime, "-1") == 0)
689 else if (strcmp(svcclosetime, "0") == 0)
691 else if (inetdflag || pmflag) {
692 f_print(fout, "#include <signal.h>\n");
696 if (!tirpcflag && inetdflag)
697 f_print(fout, "#include <sys/ttycom.h> /* TIOCNOTTY */\n");
698 if (inetdflag || pmflag) {
699 f_print(fout, "#ifdef __cplusplus\n");
701 "#include <sys/sysent.h> /* getdtablesize, open */\n");
702 f_print(fout, "#endif /* __cplusplus */\n");
705 f_print(fout, "#include <fcntl.h> /* open */\n");
706 f_print(fout, "#include <unistd.h> /* fork / setsid */\n");
707 f_print(fout, "#include <sys/types.h>\n");
710 f_print(fout, "#include <string.h>\n");
711 if (inetdflag || !tirpcflag) {
712 f_print(fout, "#include <sys/socket.h>\n");
713 f_print(fout, "#include <netinet/in.h>\n");
716 if ((netflag || pmflag) && tirpcflag && !nomain) {
717 f_print(fout, "#include <netconfig.h>\n");
720 f_print(fout, "#include <sys/resource.h> /* rlimit */\n");
721 if (logflag || inetdflag || pmflag || tirpcflag)
722 f_print(fout, "#include <syslog.h>\n");
724 f_print(fout, "\n#ifdef DEBUG\n#define RPC_SVC_FG\n#endif\n");
726 f_print(fout, "\n#define _RPCSVC_CLOSEDOWN %s\n",
728 while ( (def = get_definition()) ) {
729 foundprogram |= (def->def_kind == DEF_PROGRAM);
731 if (extend && !foundprogram) {
732 (void) unlink(outfilename);
735 write_most(infile, netflag, nomain);
737 if (!do_registers(argc, argv)) {
739 (void) unlink(outfilename);
747 * generate client side stubs
750 l_output(const char *infile, const char *define, int extend, const char *outfile)
754 int foundprogram = 0;
755 const char *outfilename;
757 open_input(infile, define);
758 outfilename = extend ? extendfile(infile, outfile) : outfile;
759 open_output(infile, outfilename);
761 f_print (fout, "#include <string.h> /* for memset */\n");
762 if (infile && (include = extendfile(infile, ".h"))) {
763 f_print(fout, "#include \"%s\"\n", include);
766 f_print(fout, "#include <rpc/rpc.h>\n");
767 while ( (def = get_definition()) ) {
768 foundprogram |= (def->def_kind == DEF_PROGRAM);
770 if (extend && !foundprogram) {
771 (void) unlink(outfilename);
778 * generate the dispatch table
781 t_output(const char *infile, const char *define, int extend, const char *outfile)
784 int foundprogram = 0;
785 const char *outfilename;
787 open_input(infile, define);
788 outfilename = extend ? extendfile(infile, outfile) : outfile;
789 open_output(infile, outfilename);
791 while ( (def = get_definition()) ) {
792 foundprogram |= (def->def_kind == DEF_PROGRAM);
794 if (extend && !foundprogram) {
795 (void) unlink(outfilename);
801 /* sample routine for the server template */
803 svc_output(const char *infile, const char *define, int extend, const char *outfile)
807 const char *outfilename;
809 open_input(infile, define);
810 outfilename = extend ? extendfile(infile, outfile) : outfile;
811 checkfiles(infile, outfilename);
813 * Check if outfile already exists.
814 * if so, print an error message and exit
816 open_output(infile, outfilename);
819 if (infile && (include = extendfile(infile, ".h"))) {
820 f_print(fout, "#include \"%s\"\n", include);
823 f_print(fout, "#include <rpc/rpc.h>\n");
826 while ( (def = get_definition()) ) {
827 write_sample_svc(def);
829 if (extend && tell == ftell(fout)) {
830 (void) unlink(outfilename);
834 /* sample main routine for client */
836 clnt_output(const char *infile, const char *define, int extend, const char *outfile)
840 const char *outfilename;
844 open_input(infile, define);
845 outfilename = extend ? extendfile(infile, outfile) : outfile;
846 checkfiles(infile, outfilename);
848 * Check if outfile already exists.
849 * if so, print an error message and exit
852 open_output(infile, outfilename);
854 if (infile && (include = extendfile(infile, ".h"))) {
855 f_print(fout, "#include \"%s\"\n", include);
858 f_print(fout, "#include <rpc/rpc.h>\n");
859 f_print(fout, "#include <stdio.h>\n");
860 f_print(fout, "#include <stdlib.h>\n");
862 while ( (def = get_definition()) ) {
863 has_program += write_sample_clnt(def);
867 write_sample_clnt_main();
869 if (extend && tell == ftell(fout)) {
870 (void) unlink(outfilename);
875 static void mkfile_output(struct commandline *cmd)
877 const char *mkfilename, *clientname, *clntname, *xdrname, *hdrname;
878 const char *servername, *svcname, *servprogname, *clntprogname;
879 char *temp, *mkftemp;
881 svcname = file_name(cmd->infile, "_svc.c");
882 clntname = file_name(cmd->infile, "_clnt.c");
883 xdrname = file_name(cmd->infile, "_xdr.c");
884 hdrname = file_name(cmd->infile, ".h");
888 servername = extendfile(cmd->infile, "_server.c");
889 clientname = extendfile(cmd->infile, "_client.c");
894 servprogname = extendfile(cmd->infile, "_server");
895 clntprogname = extendfile(cmd->infile, "_client");
898 mkftemp = xmalloc(strlen("makefile.") +
899 strlen(cmd->infile) + 1);
900 temp = strrchr(cmd->infile, '.');
901 strcpy(mkftemp, "makefile.");
902 (void) strncat(mkftemp, cmd->infile,
903 (temp - cmd->infile));
904 mkfilename = mkftemp;
906 mkfilename = cmd->outfile;
909 checkfiles(NULL, mkfilename);
910 open_output(NULL, mkfilename);
912 f_print(fout, "\n# This is a template makefile generated\
915 f_print(fout, "\n# Parameters \n\n");
917 f_print(fout, "CLIENT = %s\nSERVER = %s\n\n",
918 clntprogname, servprogname);
919 f_print(fout, "SOURCES_CLNT.c = \nSOURCES_CLNT.h = \n");
920 f_print(fout, "SOURCES_SVC.c = \nSOURCES_SVC.h = \n");
921 f_print(fout, "SOURCES.x = %s\n\n", cmd->infile);
922 f_print(fout, "TARGETS_SVC.c = %s %s %s \n",
923 svcname, servername, xdrname);
924 f_print(fout, "TARGETS_CLNT.c = %s %s %s \n",
925 clntname, clientname, xdrname);
926 f_print(fout, "TARGETS = %s %s %s %s %s %s\n\n",
927 hdrname, xdrname, clntname,
928 svcname, clientname, servername);
930 f_print(fout, "OBJECTS_CLNT = $(SOURCES_CLNT.c:%%.c=%%.o) \
931 $(TARGETS_CLNT.c:%%.c=%%.o) ");
933 f_print(fout, "\nOBJECTS_SVC = $(SOURCES_SVC.c:%%.c=%%.o) \
934 $(TARGETS_SVC.c:%%.c=%%.o) ");
937 f_print(fout, "\n# Compiler flags \n");
939 f_print(fout, "\nCFLAGS += -D_REENTRANT -D_THEAD_SAFE \nLDLIBS += -pthread\n");
941 f_print(fout, "RPCGENFLAGS = \n");
943 f_print(fout, "\n# Targets \n\n");
945 f_print(fout, "all : $(CLIENT) $(SERVER)\n\n");
946 f_print(fout, "$(TARGETS) : $(SOURCES.x) \n");
947 f_print(fout, "\trpcgen $(RPCGENFLAGS) $(SOURCES.x)\n\n");
949 f_print(fout, "\trpcgen -Sc $(RPCGENFLAGS) $(SOURCES.x) -o %s\n\n", clientname);
950 f_print(fout, "\trpcgen -Ss $(RPCGENFLAGS) $(SOURCES.x) -o %s\n\n", servername);
952 f_print(fout, "$(OBJECTS_CLNT) : $(SOURCES_CLNT.c) $(SOURCES_CLNT.h) \
953 $(TARGETS_CLNT.c) \n\n");
955 f_print(fout, "$(OBJECTS_SVC) : $(SOURCES_SVC.c) $(SOURCES_SVC.h) \
956 $(TARGETS_SVC.c) \n\n");
957 f_print(fout, "$(CLIENT) : $(OBJECTS_CLNT) \n");
958 f_print(fout, "\t$(CC) -o $(CLIENT) $(OBJECTS_CLNT) \
960 f_print(fout, "$(SERVER) : $(OBJECTS_SVC) \n");
961 f_print(fout, "\t$(CC) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS)\n\n");
962 f_print(fout, "clean:\n\t rm -f core $(TARGETS) $(OBJECTS_CLNT) \
963 $(OBJECTS_SVC) $(CLIENT) $(SERVER)\n\n");
969 * Perform registrations for service output
970 * Return 0 if failed; 1 otherwise.
973 do_registers(int argc, const char *argv[])
977 if (inetdflag || !tirpcflag) {
978 for (i = 1; i < argc; i++) {
979 if (streq(argv[i], "-s")) {
980 if (!check_nettype(argv[i + 1],
983 write_inetd_register(argv[i + 1]);
988 for (i = 1; i < argc; i++)
989 if (streq(argv[i], "-s")) {
990 if (!check_nettype(argv[i + 1],
993 write_nettype_register(argv[i + 1]);
995 } else if (streq(argv[i], "-n")) {
996 write_netid_register(argv[i + 1]);
1004 * Extend the argument list
1011 argmax = argmax == 0 ? 32 : argmax << 1;
1012 if (argmax > INT_MAX / 4) {
1013 warnx("refusing to allocate too many arguments");
1016 newarglist = realloc(arglist, argmax * sizeof(*arglist));
1017 if (newarglist == NULL) {
1018 warnx("unable to allocate arglist");
1021 arglist = newarglist;
1025 * Add another argument to the arg list
1028 addarg(const char *cp)
1030 if (argcount >= argmax)
1034 arglist[argcount++] = xstrdup(cp);
1036 arglist[argcount++] = NULL;
1040 * Insert an argument at the specified location
1043 insarg(int place, const char *cp)
1047 if (argcount >= argmax)
1050 /* Move up existing arguments */
1051 for (i = argcount - 1; i >= place; i--)
1052 arglist[i + 1] = arglist[i];
1054 arglist[place] = xstrdup(cp);
1059 * if input file is stdin and an output file is specified then complain
1060 * if the file already exists. Otherwise the file may get overwritten
1061 * If input file does not exist, exit with an error
1065 checkfiles(const char *infile, const char *outfile)
1070 if (infile) /* infile ! = NULL */
1071 if (stat(infile, &buf) < 0)
1077 if (stat(outfile, &buf) < 0)
1078 return; /* file does not exist */
1080 warnx("file '%s' already exists and may be overwritten", outfile);
1087 * Parse command line arguments
1090 parseargs(int argc, const char *argv[], struct commandline *cmd)
1095 char flag[(1 << 8 * sizeof (char))];
1098 cmd->infile = cmd->outfile = NULL;
1115 for (i = 1; i < argc; i++) {
1116 if (argv[i][0] != '-') {
1118 warnx("cannot specify more than one input file");
1121 cmd->infile = argv[i];
1123 for (j = 1; argv[i][j] != 0; j++) {
1141 * sample flag: Ss or Sc.
1142 * Ss means set flag['S'];
1143 * Sc means set flag['C'];
1144 * Sm means set flag['M'];
1146 ch = argv[i][++j]; /* get next char */
1156 if (flag[(int)ch]) {
1161 case 'C': /* ANSI C syntax */
1162 ch = argv[i][j+1]; /* get next char */
1170 * Turn TIRPC flag off for
1171 * generating backward compatible
1193 svcclosetime = argv[i];
1205 inline_size = atoi(argv[i]);
1210 if (argv[i][j - 1] != '-' ||
1211 argv[i][j + 1] != 0) {
1222 cmd->outfile = argv[i];
1226 if (argv[i][j - 1] != '-') {
1229 (void) addarg(argv[i]);
1235 if (strlcpy(pathbuf, argv[i],
1236 sizeof(pathbuf)) >= sizeof(pathbuf)
1237 || strlcat(pathbuf, "/cpp",
1240 warnx("argument too long");
1257 cmd->cflag = flag['c'];
1258 cmd->hflag = flag['h'];
1259 cmd->lflag = flag['l'];
1260 cmd->mflag = flag['m'];
1261 cmd->nflag = flag['n'];
1262 cmd->sflag = flag['s'];
1263 cmd->tflag = flag['t'];
1264 cmd->Ssflag = flag['S'];
1265 cmd->Scflag = flag['C'];
1266 cmd->makefileflag = flag['M'];
1271 if ((inetdflag && cmd->nflag)) {
1272 /* netid not allowed with inetdflag */
1273 warnx("cannot use netid flag with inetd flag");
1276 } else { /* 4.1 mode */
1277 pmflag = 0; /* set pmflag only in tirpcmode */
1278 if (cmd->nflag) { /* netid needs TIRPC */
1279 warnx("cannot use netid flag without TIRPC");
1284 if (newstyle && (tblflag || cmd->tflag)) {
1285 warnx("cannot use table flags with newstyle");
1289 /* check no conflicts with file generation flags */
1290 nflags = cmd->cflag + cmd->hflag + cmd->lflag + cmd->mflag +
1291 cmd->sflag + cmd->nflag + cmd->tflag + cmd->Ssflag +
1292 cmd->Scflag + cmd->makefileflag;
1295 if (cmd->outfile != NULL || cmd->infile == NULL) {
1298 } else if (cmd->infile == NULL &&
1299 (cmd->Ssflag || cmd->Scflag || cmd->makefileflag)) {
1300 warnx("\"infile\" is required for template generation flags");
1303 warnx("cannot have more than one file generation flag");
1312 f_print(stderr, "%s\n%s\n%s\n%s\n%s\n",
1313 "usage: rpcgen infile",
1314 " rpcgen [-abCLNTM] [-Dname[=value]] [-i size]\
1315 [-I -P [-K seconds]] [-Y path] infile",
1316 " rpcgen [-c | -h | -l | -m | -t | -Sc | -Ss | -Sm]\
1317 [-o outfile] [infile]",
1318 " rpcgen [-s nettype]* [-o outfile] [infile]",
1319 " rpcgen [-n netid]* [-o outfile] [infile]");
1327 f_print(stderr, "options:\n");
1328 f_print(stderr, "-a\t\tgenerate all files, including samples\n");
1329 f_print(stderr, "-b\t\tbackward compatibility mode (generates code \
1330 for FreeBSD 4.X)\n");
1331 f_print(stderr, "-c\t\tgenerate XDR routines\n");
1332 f_print(stderr, "-C\t\tANSI C mode\n");
1333 f_print(stderr, "-Dname[=value]\tdefine a symbol (same as #define)\n");
1334 f_print(stderr, "-h\t\tgenerate header file\n");
1335 f_print(stderr, "-i size\t\tsize at which to start generating\
1337 f_print(stderr, "-I\t\tgenerate code for inetd support in server\n");
1338 f_print(stderr, "-K seconds\tserver exits after K seconds of\
1340 f_print(stderr, "-l\t\tgenerate client side stubs\n");
1341 f_print(stderr, "-L\t\tserver errors will be printed to syslog\n");
1342 f_print(stderr, "-m\t\tgenerate server side stubs\n");
1343 f_print(stderr, "-M\t\tgenerate MT-safe code\n");
1344 f_print(stderr, "-n netid\tgenerate server code that supports\
1346 f_print(stderr, "-N\t\tsupports multiple arguments and\
1348 f_print(stderr, "-o outfile\tname of the output file\n");
1349 f_print(stderr, "-P\t\tgenerate code for port monitoring support in server\n");
1350 f_print(stderr, "-s nettype\tgenerate server code that supports named\
1352 f_print(stderr, "-Sc\t\tgenerate sample client code that uses remote\
1354 f_print(stderr, "-Ss\t\tgenerate sample server code that defines\
1355 remote procedures\n");
1356 f_print(stderr, "-Sm \t\tgenerate makefile template \n");
1358 f_print(stderr, "-t\t\tgenerate RPC dispatch table\n");
1359 f_print(stderr, "-T\t\tgenerate code to support RPC dispatch tables\n");
1360 f_print(stderr, "-Y path\t\tpath where cpp is found\n");