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
32 * rpc_main.c, Top level of the RPC protocol compiler.
33 * Copyright (C) 1987, Sun Microsystems, Inc.
42 #include <sys/types.h>
43 #include <sys/param.h>
46 #include "rpc_parse.h"
50 static void c_output(const char *, const char *, int, const char *);
51 static void h_output(const char *, const char *, int, const char *, int);
52 static void l_output(const char *, const char *, int, const char *);
53 static void t_output(const char *, const char *, int, const char *);
54 static void clnt_output(const char *, const char *, int, const char * );
55 static char *generate_guard(const char *);
56 static void c_initialize(void);
58 static void usage(void) __dead2;
59 static void options_usage(void);
60 static int do_registers(int, const char **);
61 static int parseargs(int, const char **, struct commandline *);
62 static void svc_output(const char *, const char *, int, const char *);
63 static void mkfile_output(struct commandline *);
64 static void s_output(int, const char **, const char *, const char *, int, const char *, int, int);
66 #define EXTEND 1 /* alias for TRUE */
67 #define DONT_EXTEND 0 /* alias for FALSE */
69 static const char *svcclosetime = "120";
70 static const char *CPP = NULL;
71 static const char CPPFLAGS[] = "-C";
72 static char pathbuf[MAXPATHLEN + 1];
73 static const char *allv[] = {
74 "rpcgen", "-s", "udp", "-s", "tcp",
76 static int allc = nitems(allv);
77 static const char *allnv[] = {
78 "rpcgen", "-s", "netpath",
80 static int allnc = nitems(allnv);
83 * machinations for handling expanding argument list
85 static void addarg(const char *); /* add another argument to the list */
86 static void insarg(int, const char *); /* insert arg at specified location */
87 static void checkfiles(const char *, const char *);
88 /* check if out file already exists */
90 static char **arglist;
91 static int argcount = 0;
92 static int argmax = 0;
94 int nonfatalerrors; /* errors */
95 int inetdflag = 0; /* Support for inetd is disabled by default, use -I */
96 int pmflag = 0; /* Support for port monitors is disabled by default */
97 int tirpc_socket = 1; /* TI-RPC on socket, no TLI library */
98 int logflag; /* Use syslog instead of fprintf for errors */
99 int tblflag; /* Support for dispatch table file */
100 int mtflag = 0; /* Support for MT */
103 /* length at which to start doing an inline */
105 int inline_size = INLINE;
107 * Length at which to start doing an inline. INLINE = default
108 * if 0, no xdr_inline code
111 int indefinitewait; /* If started by port monitors, hang till it wants */
112 int exitnow; /* If started by port monitors, exit after the call */
113 int timerflag; /* TRUE if !indefinite && !exitnow */
114 int newstyle; /* newstyle of passing arguments (by value) */
115 int CCflag = 0; /* C++ files */
116 static int allfiles; /* generate all files */
117 int tirpcflag = 1; /* generating code for tirpc, by default */
118 xdrfunc *xdrfunc_head = NULL; /* xdr function list */
119 xdrfunc *xdrfunc_tail = NULL; /* xdr function list */
124 main(int argc, const char *argv[])
126 struct commandline cmd;
128 (void) memset((char *)&cmd, 0, sizeof (struct commandline));
129 if (!parseargs(argc, argv, &cmd))
132 * Only the client and server side stubs are likely to be customized,
133 * so in that case only, check if the outfile exists, and if so,
134 * print an error message and exit.
136 if (cmd.Ssflag || cmd.Scflag || cmd.makefileflag) {
137 checkfiles(cmd.infile, cmd.outfile);
140 checkfiles(cmd.infile, NULL);
143 c_output(cmd.infile, "-DRPC_XDR", DONT_EXTEND, cmd.outfile);
144 } else if (cmd.hflag) {
145 h_output(cmd.infile, "-DRPC_HDR", DONT_EXTEND, cmd.outfile,
147 } else if (cmd.lflag) {
148 l_output(cmd.infile, "-DRPC_CLNT", DONT_EXTEND, cmd.outfile);
149 } else if (cmd.sflag || cmd.mflag || (cmd.nflag)) {
150 s_output(argc, argv, cmd.infile, "-DRPC_SVC", DONT_EXTEND,
151 cmd.outfile, cmd.mflag, cmd.nflag);
152 } else if (cmd.tflag) {
153 t_output(cmd.infile, "-DRPC_TBL", DONT_EXTEND, cmd.outfile);
154 } else if (cmd.Ssflag) {
155 svc_output(cmd.infile, "-DRPC_SERVER", DONT_EXTEND,
157 } else if (cmd.Scflag) {
158 clnt_output(cmd.infile, "-DRPC_CLIENT", DONT_EXTEND,
160 } else if (cmd.makefileflag) {
163 /* the rescans are required, since cpp may effect input */
164 c_output(cmd.infile, "-DRPC_XDR", EXTEND, "_xdr.c");
166 h_output(cmd.infile, "-DRPC_HDR", EXTEND, ".h", cmd.hflag);
168 l_output(cmd.infile, "-DRPC_CLNT", EXTEND, "_clnt.c");
170 if (inetdflag || !tirpcflag)
171 s_output(allc, allv, cmd.infile, "-DRPC_SVC", EXTEND,
172 "_svc.c", cmd.mflag, cmd.nflag);
174 s_output(allnc, allnv, cmd.infile, "-DRPC_SVC",
175 EXTEND, "_svc.c", cmd.mflag, cmd.nflag);
178 t_output(cmd.infile, "-DRPC_TBL", EXTEND, "_tbl.i");
183 svc_output(cmd.infile, "-DRPC_SERVER", EXTEND,
186 clnt_output(cmd.infile, "-DRPC_CLIENT", EXTEND,
190 if (allfiles || (cmd.makefileflag == 1)){
196 exit(nonfatalerrors);
202 * add extension to filename
205 extendfile(const char *path, const char *ext)
211 if ((file = strrchr(path, '/')) == NULL)
215 res = xmalloc(strlen(file) + strlen(ext) + 1);
216 p = strrchr(file, '.');
218 p = file + strlen(file);
220 (void) strcpy(res, file);
221 (void) strcpy(res + (p - file), ext);
226 * Open output file with given extension
229 open_output(const char *infile, const char *outfile)
232 if (outfile == NULL) {
237 if (infile != NULL && streq(outfile, infile)) {
238 warnx("%s already exists. No output generated", infile);
241 fout = fopen(outfile, "w");
243 warn("unable to open %s", outfile);
246 record_open(outfile);
254 f_print(fout, "/*\n");
255 f_print(fout, " * Please do not edit this file.\n");
256 f_print(fout, " * It was generated using rpcgen.\n");
257 f_print(fout, " */\n\n");
260 /* prepend C-preprocessor and flags before arguments */
266 char *dupvar, *t, *word;
270 else if ((var = getenv("RPCGEN_CPP")) == NULL)
271 insarg(idx++, "/usr/bin/cpp");
274 * Parse command line like a shell (but only handle whitespace,
275 * quotes and backslash).
277 dupvar = malloc(strlen(var) + 1);
280 for (s = var, t = dupvar; *s; ++s) {
367 errx(1, "RPCGEN_CPP: unterminated %c",
368 quoted == 1 ? '\'' : '"');
376 insarg(idx, CPPFLAGS);
380 * Open input file with given define for C-preprocessor
383 open_input(const char *infile, const char *define)
387 infilename = (infile == NULL) ? "<stdin>" : infile;
389 switch (childpid = fork()) {
395 addarg((char *)NULL);
397 (void) dup2(pd[1], 1);
399 execvp(arglist[0], arglist);
400 err(1, "execvp %s", arglist[0]);
405 fin = fdopen(pd[0], "r");
407 warn("%s", infilename);
412 /* valid tirpc nettypes */
413 static const char *valid_ti_nettypes[] =
427 /* valid inetd nettypes */
428 static const char *valid_i_nettypes[] =
436 check_nettype(const char *name, const char *list_to_check[])
439 for (i = 0; list_to_check[i] != NULL; i++) {
440 if (strcmp(name, list_to_check[i]) == 0) {
444 warnx("illegal nettype :\'%s\'", name);
449 file_name(const char *file, const char *ext)
452 temp = extendfile(file, ext);
454 if (access(temp, F_OK) != -1)
463 c_output(const char *infile, const char *define, int extend, const char *outfile)
467 const char *outfilename;
471 open_input(infile, define);
472 outfilename = extend ? extendfile(infile, outfile) : outfile;
473 open_output(infile, outfilename);
475 if (infile && (include = extendfile(infile, ".h"))) {
476 f_print(fout, "#include \"%s\"\n", include);
478 /* .h file already contains rpc/rpc.h */
480 f_print(fout, "#include <rpc/rpc.h>\n");
482 while ( (def = get_definition()) ) {
485 if (extend && tell == ftell(fout)) {
486 (void) unlink(outfilename);
495 /* add all the starting basic types */
498 add_type(1, "short");
500 add_type(1, "u_int");
501 add_type(1, "u_long");
502 add_type(1, "u_short");
506 static const char rpcgen_table_dcl[] = "struct rpcgen_table {\n\
508 xdrproc_t xdr_arg; \n\
509 unsigned len_arg; \n\
510 xdrproc_t xdr_res; \n\
511 unsigned len_res; \n\
516 generate_guard(const char *pathname)
518 const char *filename;
519 char *guard, *tmp, *stopat;
521 filename = strrchr(pathname, '/'); /* find last component */
522 filename = ((filename == NULL) ? pathname : filename+1);
523 guard = xstrdup(filename);
524 stopat = strrchr(guard, '.');
527 * Convert to a valid C macro name and make it upper case.
528 * Map macro unfriendly characterss to '_'.
530 for (tmp = guard; *tmp != '\000'; ++tmp) {
532 *tmp = toupper(*tmp);
533 else if (isupper(*tmp) || *tmp == '_')
535 else if (tmp == guard)
537 else if (isdigit(*tmp))
538 /* OK for all but first character */;
539 else if (tmp == stopat) {
546 * Can't have a '_' in front, because it'll end up being "__".
547 * "__" macros shoudln't be used. So, remove all of the
548 * '_' characters from the front.
551 for (tmp = guard; *tmp == '_'; ++tmp)
556 guard = extendfile(guard, "_H_RPCGEN");
562 * Compile into an XDR header file
567 h_output(const char *infile, const char *define, int extend, const char *outfile, int headeronly)
570 const char *outfilename;
577 open_input(infile, define);
578 outfilename = extend ? extendfile(infile, outfile) : outfile;
579 open_output(infile, outfilename);
581 if (outfilename || infile){
582 guard = tmp = generate_guard(outfilename ? outfilename: infile);
586 f_print(fout, "#ifndef _%s\n#define _%s\n\n", guard,
589 f_print(fout, "#include <rpc/rpc.h>\n");
592 f_print(fout, "#include <pthread.h>\n");
594 /* put the C++ support */
596 f_print(fout, "\n#ifdef __cplusplus\n");
597 f_print(fout, "extern \"C\" {\n");
598 f_print(fout, "#endif\n\n");
601 /* put in a typedef for quadprecision. Only with Cflag */
605 /* print data definitions */
606 while ( (def = get_definition()) ) {
607 print_datadef(def, headeronly);
611 * print function declarations.
612 * Do this after data definitions because they might be used as
613 * arguments for functions
615 for (l = defined; l != NULL; l = l->next) {
616 print_funcdef(l->val, headeronly);
618 /* Now print all xdr func declarations */
619 if (xdrfunc_head != NULL){
622 "\n/* the xdr functions */\n");
625 f_print(fout, "\n#ifdef __cplusplus\n");
626 f_print(fout, "extern \"C\" {\n");
627 f_print(fout, "#endif\n");
630 xdrfuncp = xdrfunc_head;
631 while (xdrfuncp != NULL){
632 print_xdr_func_def(xdrfuncp->name, xdrfuncp->pointerp);
633 xdrfuncp = xdrfuncp->next;
637 if (extend && tell == ftell(fout)) {
638 (void) unlink(outfilename);
639 } else if (tblflag) {
640 f_print(fout, rpcgen_table_dcl);
643 f_print(fout, "\n#ifdef __cplusplus\n");
644 f_print(fout, "}\n");
645 f_print(fout, "#endif\n");
647 f_print(fout, "\n#endif /* !_%s */\n", guard);
652 * Compile into an RPC service
655 s_output(int argc, const char *argv[], const char *infile, const char *define,
656 int extend, const char *outfile, int nomain, int netflag)
660 int foundprogram = 0;
661 const char *outfilename;
663 open_input(infile, define);
664 outfilename = extend ? extendfile(infile, outfile) : outfile;
665 open_output(infile, outfilename);
667 if (infile && (include = extendfile(infile, ".h"))) {
668 f_print(fout, "#include \"%s\"\n", include);
671 f_print(fout, "#include <rpc/rpc.h>\n");
673 f_print(fout, "#include <stdio.h>\n");
674 f_print(fout, "#include <stdlib.h> /* getenv, exit */\n");
675 f_print (fout, "#include <rpc/pmap_clnt.h> /* for pmap_unset */\n");
676 f_print (fout, "#include <string.h> /* strcmp */\n");
678 f_print(fout, "#include <rpc/rpc_com.h>\n");
679 if (strcmp(svcclosetime, "-1") == 0)
681 else if (strcmp(svcclosetime, "0") == 0)
683 else if (inetdflag || pmflag) {
684 f_print(fout, "#include <signal.h>\n");
688 if (!tirpcflag && inetdflag)
689 f_print(fout, "#include <sys/ttycom.h> /* TIOCNOTTY */\n");
690 if (inetdflag || pmflag) {
691 f_print(fout, "#ifdef __cplusplus\n");
693 "#include <sys/sysent.h> /* getdtablesize, open */\n");
694 f_print(fout, "#endif /* __cplusplus */\n");
697 f_print(fout, "#include <fcntl.h> /* open */\n");
698 f_print(fout, "#include <unistd.h> /* fork / setsid */\n");
699 f_print(fout, "#include <sys/types.h>\n");
702 f_print(fout, "#include <string.h>\n");
703 if (inetdflag || !tirpcflag) {
704 f_print(fout, "#include <sys/socket.h>\n");
705 f_print(fout, "#include <netinet/in.h>\n");
708 if ((netflag || pmflag) && tirpcflag && !nomain) {
709 f_print(fout, "#include <netconfig.h>\n");
712 f_print(fout, "#include <sys/resource.h> /* rlimit */\n");
713 if (logflag || inetdflag || pmflag || tirpcflag)
714 f_print(fout, "#include <syslog.h>\n");
716 f_print(fout, "\n#ifdef DEBUG\n#define RPC_SVC_FG\n#endif\n");
718 f_print(fout, "\n#define _RPCSVC_CLOSEDOWN %s\n",
720 while ( (def = get_definition()) ) {
721 foundprogram |= (def->def_kind == DEF_PROGRAM);
723 if (extend && !foundprogram) {
724 (void) unlink(outfilename);
727 write_most(infile, netflag, nomain);
729 if (!do_registers(argc, argv)) {
731 (void) unlink(outfilename);
739 * generate client side stubs
742 l_output(const char *infile, const char *define, int extend, const char *outfile)
746 int foundprogram = 0;
747 const char *outfilename;
749 open_input(infile, define);
750 outfilename = extend ? extendfile(infile, outfile) : outfile;
751 open_output(infile, outfilename);
753 f_print (fout, "#include <string.h> /* for memset */\n");
754 if (infile && (include = extendfile(infile, ".h"))) {
755 f_print(fout, "#include \"%s\"\n", include);
758 f_print(fout, "#include <rpc/rpc.h>\n");
759 while ( (def = get_definition()) ) {
760 foundprogram |= (def->def_kind == DEF_PROGRAM);
762 if (extend && !foundprogram) {
763 (void) unlink(outfilename);
770 * generate the dispatch table
773 t_output(const char *infile, const char *define, int extend, const char *outfile)
776 int foundprogram = 0;
777 const char *outfilename;
779 open_input(infile, define);
780 outfilename = extend ? extendfile(infile, outfile) : outfile;
781 open_output(infile, outfilename);
783 while ( (def = get_definition()) ) {
784 foundprogram |= (def->def_kind == DEF_PROGRAM);
786 if (extend && !foundprogram) {
787 (void) unlink(outfilename);
793 /* sample routine for the server template */
795 svc_output(const char *infile, const char *define, int extend, const char *outfile)
799 const char *outfilename;
801 open_input(infile, define);
802 outfilename = extend ? extendfile(infile, outfile) : outfile;
803 checkfiles(infile, outfilename);
805 * Check if outfile already exists.
806 * if so, print an error message and exit
808 open_output(infile, outfilename);
811 if (infile && (include = extendfile(infile, ".h"))) {
812 f_print(fout, "#include \"%s\"\n", include);
815 f_print(fout, "#include <rpc/rpc.h>\n");
818 while ( (def = get_definition()) ) {
819 write_sample_svc(def);
821 if (extend && tell == ftell(fout)) {
822 (void) unlink(outfilename);
826 /* sample main routine for client */
828 clnt_output(const char *infile, const char *define, int extend, const char *outfile)
832 const char *outfilename;
836 open_input(infile, define);
837 outfilename = extend ? extendfile(infile, outfile) : outfile;
838 checkfiles(infile, outfilename);
840 * Check if outfile already exists.
841 * if so, print an error message and exit
844 open_output(infile, outfilename);
846 if (infile && (include = extendfile(infile, ".h"))) {
847 f_print(fout, "#include \"%s\"\n", include);
850 f_print(fout, "#include <rpc/rpc.h>\n");
851 f_print(fout, "#include <stdio.h>\n");
852 f_print(fout, "#include <stdlib.h>\n");
854 while ( (def = get_definition()) ) {
855 has_program += write_sample_clnt(def);
859 write_sample_clnt_main();
861 if (extend && tell == ftell(fout)) {
862 (void) unlink(outfilename);
867 static void mkfile_output(struct commandline *cmd)
869 const char *mkfilename, *clientname, *clntname, *xdrname, *hdrname;
870 const char *servername, *svcname, *servprogname, *clntprogname;
871 char *temp, *mkftemp;
873 svcname = file_name(cmd->infile, "_svc.c");
874 clntname = file_name(cmd->infile, "_clnt.c");
875 xdrname = file_name(cmd->infile, "_xdr.c");
876 hdrname = file_name(cmd->infile, ".h");
880 servername = extendfile(cmd->infile, "_server.c");
881 clientname = extendfile(cmd->infile, "_client.c");
886 servprogname = extendfile(cmd->infile, "_server");
887 clntprogname = extendfile(cmd->infile, "_client");
890 mkftemp = xmalloc(strlen("makefile.") +
891 strlen(cmd->infile) + 1);
892 temp = strrchr(cmd->infile, '.');
893 strcpy(mkftemp, "makefile.");
894 (void) strncat(mkftemp, cmd->infile,
895 (temp - cmd->infile));
896 mkfilename = mkftemp;
898 mkfilename = cmd->outfile;
901 checkfiles(NULL, mkfilename);
902 open_output(NULL, mkfilename);
904 f_print(fout, "\n# This is a template makefile generated\
907 f_print(fout, "\n# Parameters \n\n");
909 f_print(fout, "CLIENT = %s\nSERVER = %s\n\n",
910 clntprogname, servprogname);
911 f_print(fout, "SOURCES_CLNT.c = \nSOURCES_CLNT.h = \n");
912 f_print(fout, "SOURCES_SVC.c = \nSOURCES_SVC.h = \n");
913 f_print(fout, "SOURCES.x = %s\n\n", cmd->infile);
914 f_print(fout, "TARGETS_SVC.c = %s %s %s \n",
915 svcname, servername, xdrname);
916 f_print(fout, "TARGETS_CLNT.c = %s %s %s \n",
917 clntname, clientname, xdrname);
918 f_print(fout, "TARGETS = %s %s %s %s %s %s\n\n",
919 hdrname, xdrname, clntname,
920 svcname, clientname, servername);
922 f_print(fout, "OBJECTS_CLNT = $(SOURCES_CLNT.c:%%.c=%%.o) \
923 $(TARGETS_CLNT.c:%%.c=%%.o) ");
925 f_print(fout, "\nOBJECTS_SVC = $(SOURCES_SVC.c:%%.c=%%.o) \
926 $(TARGETS_SVC.c:%%.c=%%.o) ");
929 f_print(fout, "\n# Compiler flags \n");
931 f_print(fout, "\nCFLAGS += -D_REENTRANT -D_THEAD_SAFE \nLDLIBS += -pthread\n");
933 f_print(fout, "RPCGENFLAGS = \n");
935 f_print(fout, "\n# Targets \n\n");
937 f_print(fout, "all : $(CLIENT) $(SERVER)\n\n");
938 f_print(fout, "$(TARGETS) : $(SOURCES.x) \n");
939 f_print(fout, "\trpcgen $(RPCGENFLAGS) $(SOURCES.x)\n\n");
941 f_print(fout, "\trpcgen -Sc $(RPCGENFLAGS) $(SOURCES.x) -o %s\n\n", clientname);
942 f_print(fout, "\trpcgen -Ss $(RPCGENFLAGS) $(SOURCES.x) -o %s\n\n", servername);
944 f_print(fout, "$(OBJECTS_CLNT) : $(SOURCES_CLNT.c) $(SOURCES_CLNT.h) \
945 $(TARGETS_CLNT.c) \n\n");
947 f_print(fout, "$(OBJECTS_SVC) : $(SOURCES_SVC.c) $(SOURCES_SVC.h) \
948 $(TARGETS_SVC.c) \n\n");
949 f_print(fout, "$(CLIENT) : $(OBJECTS_CLNT) \n");
950 f_print(fout, "\t$(CC) -o $(CLIENT) $(OBJECTS_CLNT) \
952 f_print(fout, "$(SERVER) : $(OBJECTS_SVC) \n");
953 f_print(fout, "\t$(CC) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS)\n\n");
954 f_print(fout, "clean:\n\t rm -f core $(TARGETS) $(OBJECTS_CLNT) \
955 $(OBJECTS_SVC) $(CLIENT) $(SERVER)\n\n");
961 * Perform registrations for service output
962 * Return 0 if failed; 1 otherwise.
965 do_registers(int argc, const char *argv[])
969 if (inetdflag || !tirpcflag) {
970 for (i = 1; i < argc; i++) {
971 if (streq(argv[i], "-s")) {
972 if (!check_nettype(argv[i + 1],
975 write_inetd_register(argv[i + 1]);
980 for (i = 1; i < argc; i++)
981 if (streq(argv[i], "-s")) {
982 if (!check_nettype(argv[i + 1],
985 write_nettype_register(argv[i + 1]);
987 } else if (streq(argv[i], "-n")) {
988 write_netid_register(argv[i + 1]);
996 * Extend the argument list
1003 argmax = argmax == 0 ? 32 : argmax << 1;
1004 if (argmax > INT_MAX / 4) {
1005 warnx("refusing to allocate too many arguments");
1008 newarglist = realloc(arglist, argmax * sizeof(*arglist));
1009 if (newarglist == NULL) {
1010 warnx("unable to allocate arglist");
1013 arglist = newarglist;
1017 * Add another argument to the arg list
1020 addarg(const char *cp)
1022 if (argcount >= argmax)
1026 arglist[argcount++] = xstrdup(cp);
1028 arglist[argcount++] = NULL;
1032 * Insert an argument at the specified location
1035 insarg(int place, const char *cp)
1039 if (argcount >= argmax)
1042 /* Move up existing arguments */
1043 for (i = argcount - 1; i >= place; i--)
1044 arglist[i + 1] = arglist[i];
1046 arglist[place] = xstrdup(cp);
1051 * if input file is stdin and an output file is specified then complain
1052 * if the file already exists. Otherwise the file may get overwritten
1053 * If input file does not exist, exit with an error
1057 checkfiles(const char *infile, const char *outfile)
1062 if (infile) /* infile ! = NULL */
1063 if (stat(infile, &buf) < 0)
1069 if (stat(outfile, &buf) < 0)
1070 return; /* file does not exist */
1072 warnx("file '%s' already exists and may be overwritten", outfile);
1079 * Parse command line arguments
1082 parseargs(int argc, const char *argv[], struct commandline *cmd)
1087 char flag[(1 << 8 * sizeof (char))];
1090 cmd->infile = cmd->outfile = NULL;
1107 for (i = 1; i < argc; i++) {
1108 if (argv[i][0] != '-') {
1110 warnx("cannot specify more than one input file");
1113 cmd->infile = argv[i];
1115 for (j = 1; argv[i][j] != 0; j++) {
1133 * sample flag: Ss or Sc.
1134 * Ss means set flag['S'];
1135 * Sc means set flag['C'];
1136 * Sm means set flag['M'];
1138 ch = argv[i][++j]; /* get next char */
1148 if (flag[(int)ch]) {
1153 case 'C': /* ANSI C syntax */
1154 ch = argv[i][j+1]; /* get next char */
1162 * Turn TIRPC flag off for
1163 * generating backward compatible
1185 svcclosetime = argv[i];
1197 inline_size = atoi(argv[i]);
1202 if (argv[i][j - 1] != '-' ||
1203 argv[i][j + 1] != 0) {
1214 cmd->outfile = argv[i];
1218 if (argv[i][j - 1] != '-') {
1221 (void) addarg(argv[i]);
1227 if (strlcpy(pathbuf, argv[i],
1228 sizeof(pathbuf)) >= sizeof(pathbuf)
1229 || strlcat(pathbuf, "/cpp",
1232 warnx("argument too long");
1249 cmd->cflag = flag['c'];
1250 cmd->hflag = flag['h'];
1251 cmd->lflag = flag['l'];
1252 cmd->mflag = flag['m'];
1253 cmd->nflag = flag['n'];
1254 cmd->sflag = flag['s'];
1255 cmd->tflag = flag['t'];
1256 cmd->Ssflag = flag['S'];
1257 cmd->Scflag = flag['C'];
1258 cmd->makefileflag = flag['M'];
1263 if ((inetdflag && cmd->nflag)) {
1264 /* netid not allowed with inetdflag */
1265 warnx("cannot use netid flag with inetd flag");
1268 } else { /* 4.1 mode */
1269 pmflag = 0; /* set pmflag only in tirpcmode */
1270 if (cmd->nflag) { /* netid needs TIRPC */
1271 warnx("cannot use netid flag without TIRPC");
1276 if (newstyle && (tblflag || cmd->tflag)) {
1277 warnx("cannot use table flags with newstyle");
1281 /* check no conflicts with file generation flags */
1282 nflags = cmd->cflag + cmd->hflag + cmd->lflag + cmd->mflag +
1283 cmd->sflag + cmd->nflag + cmd->tflag + cmd->Ssflag +
1284 cmd->Scflag + cmd->makefileflag;
1287 if (cmd->outfile != NULL || cmd->infile == NULL) {
1290 } else if (cmd->infile == NULL &&
1291 (cmd->Ssflag || cmd->Scflag || cmd->makefileflag)) {
1292 warnx("\"infile\" is required for template generation flags");
1295 warnx("cannot have more than one file generation flag");
1304 f_print(stderr, "%s\n%s\n%s\n%s\n%s\n",
1305 "usage: rpcgen infile",
1306 " rpcgen [-abCLNTM] [-Dname[=value]] [-i size]\
1307 [-I -P [-K seconds]] [-Y path] infile",
1308 " rpcgen [-c | -h | -l | -m | -t | -Sc | -Ss | -Sm]\
1309 [-o outfile] [infile]",
1310 " rpcgen [-s nettype]* [-o outfile] [infile]",
1311 " rpcgen [-n netid]* [-o outfile] [infile]");
1319 f_print(stderr, "options:\n");
1320 f_print(stderr, "-a\t\tgenerate all files, including samples\n");
1321 f_print(stderr, "-b\t\tbackward compatibility mode (generates code \
1322 for FreeBSD 4.X)\n");
1323 f_print(stderr, "-c\t\tgenerate XDR routines\n");
1324 f_print(stderr, "-C\t\tANSI C mode\n");
1325 f_print(stderr, "-Dname[=value]\tdefine a symbol (same as #define)\n");
1326 f_print(stderr, "-h\t\tgenerate header file\n");
1327 f_print(stderr, "-i size\t\tsize at which to start generating\
1329 f_print(stderr, "-I\t\tgenerate code for inetd support in server\n");
1330 f_print(stderr, "-K seconds\tserver exits after K seconds of\
1332 f_print(stderr, "-l\t\tgenerate client side stubs\n");
1333 f_print(stderr, "-L\t\tserver errors will be printed to syslog\n");
1334 f_print(stderr, "-m\t\tgenerate server side stubs\n");
1335 f_print(stderr, "-M\t\tgenerate MT-safe code\n");
1336 f_print(stderr, "-n netid\tgenerate server code that supports\
1338 f_print(stderr, "-N\t\tsupports multiple arguments and\
1340 f_print(stderr, "-o outfile\tname of the output file\n");
1341 f_print(stderr, "-P\t\tgenerate code for port monitoring support in server\n");
1342 f_print(stderr, "-s nettype\tgenerate server code that supports named\
1344 f_print(stderr, "-Sc\t\tgenerate sample client code that uses remote\
1346 f_print(stderr, "-Ss\t\tgenerate sample server code that defines\
1347 remote procedures\n");
1348 f_print(stderr, "-Sm \t\tgenerate makefile template \n");
1350 f_print(stderr, "-t\t\tgenerate RPC dispatch table\n");
1351 f_print(stderr, "-T\t\tgenerate code to support RPC dispatch tables\n");
1352 f_print(stderr, "-Y path\t\tpath where cpp is found\n");