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
29 * @(#)rpc_main.c 1.21 94/04/25 SMI; 1.30 89/03/30 (C) 1987 SMI
30 * $FreeBSD: src/usr.bin/rpcgen/rpc_main.c,v 1.33 2007/08/23 09:38:26 delphij Exp $
35 * rpc_main.c, Top level of the RPC protocol compiler.
36 * Copyright (C) 1987, Sun Microsystems, Inc.
44 #include <sys/types.h>
45 #include <sys/param.h>
48 #include "rpc_parse.h"
52 static void c_output(const char *, const char *, int, const char *);
53 static void h_output(const char *, const char *, int, const char *, int);
54 static void l_output(const char *, const char *, int, const char *);
55 static void t_output(const char *, const char *, int, const char *);
56 static void clnt_output(const char *, const char *, int, const char *);
57 static char *generate_guard(const char *);
58 static void c_initialize(void);
60 static void usage(void);
61 static void options_usage(void);
62 static int do_registers(int, const char **);
63 static int parseargs(int, const char **, struct commandline
*);
64 static void svc_output(const char *, const char *, int, const char *);
65 static void mkfile_output(struct commandline
*);
66 static void s_output(int, const char **, const char *, const char *, int,
67 const char *, int, int);
69 #define EXTEND 1 /* alias for TRUE */
70 #define DONT_EXTEND 0 /* alias for FALSE */
72 static int cppDefined
= 0; /* explicit path for C preprocessor */
74 static const char *svcclosetime
= "120";
75 static const char *CPP
= NULL
;
76 static const char CPPFLAGS
[] = "-C";
77 static char pathbuf
[MAXPATHLEN
+ 1];
78 static const char *allv
[] = {
79 "rpcgen", "-s", "udp", "-s", "tcp",
81 static int allc
= sizeof (allv
)/sizeof (allv
[0]);
82 static const char *allnv
[] = {
83 "rpcgen", "-s", "netpath",
85 static int allnc
= sizeof (allnv
)/sizeof (allnv
[0]);
88 * machinations for handling expanding argument list
90 static void addarg(const char *); /* add another argument to the list */
91 static void putarg(int, const char *); /* put argument at specified location */
92 static void clear_args(void); /* clear argument list */
93 static void checkfiles(const char *, const char *);
94 /* check if out file already exists */
101 static char *arglist
[ARGLISTLEN
];
102 static int argcount
= FIXEDARGS
;
105 int nonfatalerrors
; /* errors */
106 int inetdflag
= 0; /* Support for inetd is disabled by default, use -I */
107 int pmflag
= 0; /* Support for port monitors is disabled by default */
108 int tirpc_socket
= 1; /* TI-RPC on socket, no TLI library */
109 int logflag
; /* Use syslog instead of fprintf for errors */
110 int tblflag
; /* Support for dispatch table file */
111 int mtflag
= 0; /* Support for MT */
114 /* length at which to start doing an inline */
116 int inline_size
= INLINE
;
118 * Length at which to start doing an inline. INLINE = default
119 * if 0, no xdr_inline code
122 int indefinitewait
; /* If started by port monitors, hang till it wants */
123 int exitnow
; /* If started by port monitors, exit after the call */
124 int timerflag
; /* TRUE if !indefinite && !exitnow */
125 int newstyle
; /* newstyle of passing arguments (by value) */
126 int CCflag
= 0; /* C++ files */
127 static int allfiles
; /* generate all files */
128 int tirpcflag
= 1; /* generating code for tirpc, by default */
129 xdrfunc
*xdrfunc_head
= NULL
; /* xdr function list */
130 xdrfunc
*xdrfunc_tail
= NULL
; /* xdr function list */
135 main(int argc
, const char **argv
)
137 struct commandline cmd
;
139 memset(&cmd
, 0, sizeof (struct commandline
));
141 if (!parseargs(argc
, argv
, &cmd
))
144 * Only the client and server side stubs are likely to be customized,
145 * so in that case only, check if the outfile exists, and if so,
146 * print an error message and exit.
148 if (cmd
.Ssflag
|| cmd
.Scflag
|| cmd
.makefileflag
)
149 checkfiles(cmd
.infile
, cmd
.outfile
);
151 checkfiles(cmd
.infile
, NULL
);
154 c_output(cmd
.infile
, "-DRPC_XDR", DONT_EXTEND
, cmd
.outfile
);
155 } else if (cmd
.hflag
) {
156 h_output(cmd
.infile
, "-DRPC_HDR", DONT_EXTEND
, cmd
.outfile
,
158 } else if (cmd
.lflag
) {
159 l_output(cmd
.infile
, "-DRPC_CLNT", DONT_EXTEND
, cmd
.outfile
);
160 } else if (cmd
.sflag
|| cmd
.mflag
|| (cmd
.nflag
)) {
161 s_output(argc
, argv
, cmd
.infile
, "-DRPC_SVC", DONT_EXTEND
,
162 cmd
.outfile
, cmd
.mflag
, cmd
.nflag
);
163 } else if (cmd
.tflag
) {
164 t_output(cmd
.infile
, "-DRPC_TBL", DONT_EXTEND
, cmd
.outfile
);
165 } else if (cmd
.Ssflag
) {
166 svc_output(cmd
.infile
, "-DRPC_SERVER", DONT_EXTEND
,
168 } else if (cmd
.Scflag
) {
169 clnt_output(cmd
.infile
, "-DRPC_CLIENT", DONT_EXTEND
,
171 } else if (cmd
.makefileflag
) {
174 /* the rescans are required, since cpp may effect input */
175 c_output(cmd
.infile
, "-DRPC_XDR", EXTEND
, "_xdr.c");
177 h_output(cmd
.infile
, "-DRPC_HDR", EXTEND
, ".h", cmd
.hflag
);
179 l_output(cmd
.infile
, "-DRPC_CLNT", EXTEND
, "_clnt.c");
181 if (inetdflag
|| !tirpcflag
)
182 s_output(allc
, allv
, cmd
.infile
, "-DRPC_SVC", EXTEND
,
183 "_svc.c", cmd
.mflag
, cmd
.nflag
);
185 s_output(allnc
, allnv
, cmd
.infile
, "-DRPC_SVC",
186 EXTEND
, "_svc.c", cmd
.mflag
, cmd
.nflag
);
189 t_output(cmd
.infile
, "-DRPC_TBL", EXTEND
, "_tbl.i");
194 svc_output(cmd
.infile
, "-DRPC_SERVER", EXTEND
,
197 clnt_output(cmd
.infile
, "-DRPC_CLIENT", EXTEND
,
201 if (allfiles
|| (cmd
.makefileflag
== 1)){
207 exit(nonfatalerrors
);
213 * add extension to filename
216 extendfile(const char *path
, const char *ext
)
222 if ((file
= strrchr(path
, '/')) == NULL
)
226 res
= xmalloc(strlen(file
) + strlen(ext
) + 1);
227 p
= strrchr(file
, '.');
229 p
= file
+ strlen(file
);
231 strcpy(res
+ (p
- file
), ext
);
236 * Open output file with given extension
239 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
);
261 f_print(fout
, "/*\n");
262 f_print(fout
, " * Please do not edit this file.\n");
263 f_print(fout
, " * It was generated using rpcgen.\n");
264 f_print(fout
, " */\n\n");
267 /* clear list of arguments */
273 for (i
= FIXEDARGS
; i
< ARGLISTLEN
; i
++)
275 argcount
= FIXEDARGS
;
278 /* make sure that a CPP exists */
289 * Open input file with given define for C-preprocessor
292 open_input(const char *infile
, const char *define
)
297 infilename
= (infile
== NULL
) ? "<stdin>" : infile
;
299 switch (childpid
= fork()) {
312 execvp(arglist
[0], arglist
);
314 execv(arglist
[0], arglist
);
315 err(1, "execv(%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)
358 warnx("illegal nettype :\'%s\'", name
);
363 file_name(const char *file
, const char *ext
)
366 temp
= extendfile(file
, ext
);
368 if (access(temp
, F_OK
) != -1)
376 c_output(const char *infile
, const char *define
, int extend
, const char *outfile
)
380 const char *outfilename
;
384 open_input(infile
, define
);
385 outfilename
= extend
? extendfile(infile
, outfile
) : outfile
;
386 open_output(infile
, outfilename
);
388 if (infile
&& (include
= extendfile(infile
, ".h"))) {
389 f_print(fout
, "#include \"%s\"\n", include
);
391 /* .h file already contains rpc/rpc.h */
393 f_print(fout
, "#include <rpc/rpc.h>\n");
395 while ( (def
= get_definition()) )
397 if (extend
&& tell
== ftell(fout
))
404 /* add all the starting basic types */
407 add_type(1, "short");
409 add_type(1, "u_int");
410 add_type(1, "u_long");
411 add_type(1, "u_short");
414 const char rpcgen_table_dcl
[] = "struct rpcgen_table {\n\
416 xdrproc_t xdr_arg; \n\
417 unsigned len_arg; \n\
418 xdrproc_t xdr_res; \n\
419 unsigned len_res; \n\
423 generate_guard(const char *pathname
)
425 const char *filename
;
426 char *guard
, *tmp
, *stopat
;
428 filename
= strrchr(pathname
, '/'); /* find last component */
429 filename
= ((filename
== NULL
) ? pathname
: filename
+1);
430 guard
= xstrdup(filename
);
431 stopat
= strrchr(guard
, '.');
434 * Convert to a valid C macro name and make it upper case.
435 * Map macro unfriendly characterss to '_'.
437 for (tmp
= guard
; *tmp
!= '\000'; ++tmp
) {
439 *tmp
= toupper(*tmp
);
440 else if (isupper(*tmp
) || *tmp
== '_')
442 else if (tmp
== guard
)
444 else if (isdigit(*tmp
))
445 /* OK for all but first character */;
446 else if (tmp
== stopat
) {
453 * Can't have a '_' in front, because it'll end up being "__".
454 * "__" macros shoudln't be used. So, remove all of the
455 * '_' characters from the front.
458 for (tmp
= guard
; *tmp
== '_'; ++tmp
)
462 guard
= extendfile(guard
, "_H_RPCGEN");
467 * Compile into an XDR header file
471 h_output(const char *infile
, const char *define
, int extend
, const char *outfile
, int headeronly
)
474 const char *outfilename
;
480 open_input(infile
, define
);
481 outfilename
= extend
? extendfile(infile
, outfile
) : outfile
;
482 open_output(infile
, outfilename
);
484 if (outfilename
|| infile
){
485 guard
= generate_guard(outfilename
? outfilename
: infile
);
489 f_print(fout
, "#ifndef _%s\n#define _%s\n\n", guard
,
492 f_print(fout
, "#include <rpc/rpc.h>\n");
495 f_print(fout
, "#include <pthread.h>\n");
497 /* put the C++ support */
499 f_print(fout
, "\n#ifdef __cplusplus\n");
500 f_print(fout
, "extern \"C\" {\n");
501 f_print(fout
, "#endif\n\n");
504 /* put in a typedef for quadprecision. Only with Cflag */
508 /* print data definitions */
509 while ((def
= get_definition()) != NULL
)
510 print_datadef(def
, headeronly
);
513 * print function declarations.
514 * Do this after data definitions because they might be used as
515 * arguments for functions
517 for (l
= defined
; l
!= NULL
; l
= l
->next
)
518 print_funcdef(l
->val
, headeronly
);
519 /* Now print all xdr func declarations */
520 if (xdrfunc_head
!= NULL
) {
521 f_print(fout
, "\n/* the xdr functions */\n");
524 f_print(fout
, "\n#ifdef __cplusplus\n");
525 f_print(fout
, "extern \"C\" {\n");
526 f_print(fout
, "#endif\n");
529 xdrfuncp
= xdrfunc_head
;
530 while (xdrfuncp
!= NULL
) {
531 print_xdr_func_def(xdrfuncp
->name
, xdrfuncp
->pointerp
);
532 xdrfuncp
= xdrfuncp
->next
;
536 if (extend
&& tell
== ftell(fout
))
539 f_print(fout
, rpcgen_table_dcl
);
541 f_print(fout
, "\n#ifdef __cplusplus\n");
542 f_print(fout
, "}\n");
543 f_print(fout
, "#endif\n");
545 f_print(fout
, "\n#endif /* !_%s */\n", guard
);
549 * Compile into an RPC service
552 s_output(int argc
, const char **argv
, const char *infile
, const char *define
,
553 int extend
, const char *outfile
, int nomain
, int netflag
)
557 int foundprogram
= 0;
558 const char *outfilename
;
560 open_input(infile
, define
);
561 outfilename
= extend
? extendfile(infile
, outfile
) : outfile
;
562 open_output(infile
, outfilename
);
564 if (infile
&& (include
= extendfile(infile
, ".h"))) {
565 f_print(fout
, "#include \"%s\"\n", include
);
568 f_print(fout
, "#include <rpc/rpc.h>\n");
570 f_print(fout
, "#include <stdio.h>\n");
571 f_print(fout
, "#include <stdlib.h> /* getenv, exit */\n");
572 f_print(fout
, "#include <rpc/pmap_clnt.h> /* for pmap_unset */\n");
573 f_print(fout
, "#include <string.h> /* strcmp */\n");
575 f_print(fout
, "#include <rpc/rpc_com.h>\n");
576 if (strcmp(svcclosetime
, "-1") == 0)
578 else if (strcmp(svcclosetime
, "0") == 0)
580 else if (inetdflag
|| pmflag
) {
581 f_print(fout
, "#include <signal.h>\n");
585 if (!tirpcflag
&& inetdflag
)
586 f_print(fout
, "#include <sys/ttycom.h> /* TIOCNOTTY */\n");
587 if (inetdflag
|| pmflag
) {
588 f_print(fout
, "#ifdef __cplusplus\n");
590 "#include <sys/sysent.h> /* getdtablesize, open */\n");
591 f_print(fout
, "#endif /* __cplusplus */\n");
594 f_print(fout
, "#include <fcntl.h> /* open */\n");
595 f_print(fout
, "#include <unistd.h> /* fork / setsid */\n");
596 f_print(fout
, "#include <sys/types.h>\n");
599 f_print(fout
, "#include <string.h>\n");
600 if (inetdflag
|| !tirpcflag
) {
601 f_print(fout
, "#include <sys/socket.h>\n");
602 f_print(fout
, "#include <netinet/in.h>\n");
605 if ((netflag
|| pmflag
) && tirpcflag
&& !nomain
)
606 f_print(fout
, "#include <netconfig.h>\n");
608 f_print(fout
, "#include <sys/resource.h> /* rlimit */\n");
609 if (logflag
|| inetdflag
|| pmflag
|| tirpcflag
)
610 f_print(fout
, "#include <syslog.h>\n");
612 f_print(fout
, "\n#ifdef DEBUG\n#define RPC_SVC_FG\n#endif\n");
614 f_print(fout
, "\n#define _RPCSVC_CLOSEDOWN %s\n",
616 while ((def
= get_definition()) != NULL
)
617 foundprogram
|= (def
->def_kind
== DEF_PROGRAM
);
618 if (extend
&& !foundprogram
) {
622 write_most(infile
, netflag
, nomain
);
624 if (!do_registers(argc
, argv
)) {
634 * generate client side stubs
637 l_output(const char *infile
, const char *define
, int extend
, const char *outfile
)
641 int foundprogram
= 0;
642 const char *outfilename
;
644 open_input(infile
, define
);
645 outfilename
= extend
? extendfile(infile
, outfile
) : outfile
;
646 open_output(infile
, outfilename
);
648 f_print(fout
, "#include <string.h> /* for memset */\n");
649 if (infile
&& (include
= extendfile(infile
, ".h"))) {
650 f_print(fout
, "#include \"%s\"\n", include
);
653 f_print(fout
, "#include <rpc/rpc.h>\n");
654 while ((def
= get_definition()) != NULL
)
655 foundprogram
|= (def
->def_kind
== DEF_PROGRAM
);
656 if (extend
&& !foundprogram
)
663 * generate the dispatch table
666 t_output(const char *infile
, const char *define
, int extend
, const char *outfile
)
669 int foundprogram
= 0;
670 const char *outfilename
;
672 open_input(infile
, define
);
673 outfilename
= extend
? extendfile(infile
, outfile
) : outfile
;
674 open_output(infile
, outfilename
);
676 while ((def
= get_definition()) != NULL
)
677 foundprogram
|= (def
->def_kind
== DEF_PROGRAM
);
678 if (extend
&& !foundprogram
)
684 /* sample routine for the server template */
686 svc_output(const char *infile
, const char *define
, int extend
, const char *outfile
)
690 const char *outfilename
;
692 open_input(infile
, define
);
693 outfilename
= extend
? extendfile(infile
, outfile
) : outfile
;
694 checkfiles(infile
, outfilename
);
696 * Check if outfile already exists.
697 * if so, print an error message and exit
699 open_output(infile
, outfilename
);
702 if (infile
&& (include
= extendfile(infile
, ".h"))) {
703 f_print(fout
, "#include \"%s\"\n", include
);
706 f_print(fout
, "#include <rpc/rpc.h>\n");
709 while ((def
= get_definition()) != NULL
)
710 write_sample_svc(def
);
711 if (extend
&& tell
== ftell(fout
))
715 /* sample main routine for client */
717 clnt_output(const char *infile
, const char *define
, int extend
, const char *outfile
)
721 const char *outfilename
;
725 open_input(infile
, define
);
726 outfilename
= extend
? extendfile(infile
, outfile
) : outfile
;
727 checkfiles(infile
, outfilename
);
729 * Check if outfile already exists.
730 * if so, print an error message and exit
733 open_output(infile
, outfilename
);
735 if (infile
&& (include
= extendfile(infile
, ".h"))) {
736 f_print(fout
, "#include \"%s\"\n", include
);
739 f_print(fout
, "#include <rpc/rpc.h>\n");
741 while ((def
= get_definition()) != NULL
)
742 has_program
+= write_sample_clnt(def
);
745 write_sample_clnt_main();
747 if (extend
&& tell
== ftell(fout
))
753 mkfile_output(struct commandline
*cmd
)
755 const char *mkfilename
, *clientname
, *clntname
, *xdrname
, *hdrname
;
756 const char *servername
, *svcname
, *servprogname
, *clntprogname
;
757 char *temp
, *mkftemp
;
759 svcname
= file_name(cmd
->infile
, "_svc.c");
760 clntname
= file_name(cmd
->infile
, "_clnt.c");
761 xdrname
= file_name(cmd
->infile
, "_xdr.c");
762 hdrname
= file_name(cmd
->infile
, ".h");
765 servername
= extendfile(cmd
->infile
, "_server.c");
766 clientname
= extendfile(cmd
->infile
, "_client.c");
771 servprogname
= extendfile(cmd
->infile
, "_server");
772 clntprogname
= extendfile(cmd
->infile
, "_client");
775 mkftemp
= xmalloc(strlen("makefile.") +
776 strlen(cmd
->infile
) + 1);
777 temp
= strrchr(cmd
->infile
, '.');
778 strcpy(mkftemp
, "makefile.");
779 strncat(mkftemp
, cmd
->infile
, (temp
- cmd
->infile
));
780 mkfilename
= mkftemp
;
782 mkfilename
= cmd
->outfile
;
784 checkfiles(NULL
, mkfilename
);
785 open_output(NULL
, mkfilename
);
787 f_print(fout
, "\n# This is a template makefile generated "
790 f_print(fout
, "\n# Parameters \n\n");
792 f_print(fout
, "CLIENT = %s\nSERVER = %s\n\n",
793 clntprogname
, servprogname
);
794 f_print(fout
, "SOURCES_CLNT.c = \nSOURCES_CLNT.h = \n");
795 f_print(fout
, "SOURCES_SVC.c = \nSOURCES_SVC.h = \n");
796 f_print(fout
, "SOURCES.x = %s\n\n", cmd
->infile
);
797 f_print(fout
, "TARGETS_SVC.c = %s %s %s \n",
798 svcname
, servername
, xdrname
);
799 f_print(fout
, "TARGETS_CLNT.c = %s %s %s \n",
800 clntname
, clientname
, xdrname
);
801 f_print(fout
, "TARGETS = %s %s %s %s %s %s\n\n",
802 hdrname
, xdrname
, clntname
,
803 svcname
, clientname
, servername
);
805 f_print(fout
, "OBJECTS_CLNT = $(SOURCES_CLNT.c:%%.c=%%.o) "
806 "$(TARGETS_CLNT.c:%%.c=%%.o) ");
808 f_print(fout
, "\nOBJECTS_SVC = $(SOURCES_SVC.c:%%.c=%%.o) "
809 "$(TARGETS_SVC.c:%%.c=%%.o) ");
812 f_print(fout
, "\n# Compiler flags \n");
814 f_print(fout
, "\nCFLAGS += -D_REENTRANT -D_THEAD_SAFE \nLDLIBS += -pthread\n");
816 f_print(fout
, "RPCGENFLAGS = \n");
818 f_print(fout
, "\n# Targets \n\n");
820 f_print(fout
, "all : $(CLIENT) $(SERVER)\n\n");
821 f_print(fout
, "$(TARGETS) : $(SOURCES.x) \n");
822 f_print(fout
, "\trpcgen $(RPCGENFLAGS) $(SOURCES.x)\n\n");
823 f_print(fout
, "$(OBJECTS_CLNT) : $(SOURCES_CLNT.c) $(SOURCES_CLNT.h) "
824 "$(TARGETS_CLNT.c) \n\n");
826 f_print(fout
, "$(OBJECTS_SVC) : $(SOURCES_SVC.c) $(SOURCES_SVC.h) "
827 "$(TARGETS_SVC.c) \n\n");
828 f_print(fout
, "$(CLIENT) : $(OBJECTS_CLNT) \n");
829 f_print(fout
, "\t$(CC) -o $(CLIENT) $(OBJECTS_CLNT) $(LDLIBS) \n\n");
830 f_print(fout
, "$(SERVER) : $(OBJECTS_SVC) \n");
831 f_print(fout
, "\t$(CC) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS)\n\n ");
832 f_print(fout
, "clean:\n\t $(RM) -f core $(TARGETS) $(OBJECTS_CLNT) "
833 "$(OBJECTS_SVC) $(CLIENT) $(SERVER)\n\n");
839 * Perform registrations for service output
840 * Return 0 if failed; 1 otherwise.
843 do_registers(int argc
, const char **argv
)
847 if (inetdflag
|| !tirpcflag
) {
848 for (i
= 1; i
< argc
; i
++) {
849 if (streq(argv
[i
], "-s")) {
850 if (!check_nettype(argv
[i
+ 1],
853 write_inetd_register(argv
[i
+ 1]);
858 for (i
= 1; i
< argc
; i
++)
859 if (streq(argv
[i
], "-s")) {
860 if (!check_nettype(argv
[i
+ 1],
863 write_nettype_register(argv
[i
+ 1]);
865 } else if (streq(argv
[i
], "-n")) {
866 write_netid_register(argv
[i
+ 1]);
874 * Add another argument to the arg list
877 addarg(const char *cp
)
879 if (argcount
>= ARGLISTLEN
) {
880 warnx("too many defines");
885 arglist
[argcount
++] = xstrdup(cp
);
887 arglist
[argcount
++] = NULL
;
892 putarg(int place
, const char *cp
)
894 if (place
>= ARGLISTLEN
) {
895 warnx("arglist coding error");
900 arglist
[place
] = xstrdup(cp
);
902 arglist
[place
] = NULL
;
906 * if input file is stdin and an output file is specified then complain
907 * if the file already exists. Otherwise the file may get overwritten
908 * If input file does not exist, exit with an error
912 checkfiles(const char *infile
, const char *outfile
)
916 if (infile
!= NULL
&& stat(infile
, &buf
) < 0) {
921 if (stat(outfile
, &buf
) < 0)
922 return; /* file does not exist */
924 warnx("file '%s' already exists and may be overwritten", outfile
);
931 * Parse command line arguments
934 parseargs(int argc
, const char **argv
, struct commandline
*cmd
)
939 char flag
[(1 << 8 * sizeof (char))];
942 cmd
->infile
= cmd
->outfile
= NULL
;
959 for (i
= 1; i
< argc
; i
++) {
960 if (argv
[i
][0] != '-') {
962 warnx("cannot specify more than one input file");
965 cmd
->infile
= argv
[i
];
967 for (j
= 1; argv
[i
][j
] != 0; j
++) {
984 * sample flag: Ss or Sc.
985 * Ss means set flag['S'];
986 * Sc means set flag['C'];
987 * Sm means set flag['M'];
989 ch
= argv
[i
][++j
]; /* get next char */
1003 case 'C': /* ANSI C syntax */
1004 ch
= argv
[i
][j
+1]; /* get next char */
1012 * Turn TIRPC flag off for
1013 * generating backward compatible
1034 svcclosetime
= argv
[i
];
1045 inline_size
= atoi(argv
[i
]);
1050 if (argv
[i
][j
- 1] != '-' ||
1051 argv
[i
][j
+ 1] != 0)
1059 cmd
->outfile
= argv
[i
];
1063 if (argv
[i
][j
- 1] != '-')
1070 strlcpy(pathbuf
, argv
[i
], sizeof(pathbuf
));
1071 if (strlcat(pathbuf
, "/cpp", sizeof(pathbuf
))
1072 >= sizeof(pathbuf
)) {
1073 warnx("argument too long");
1089 cmd
->cflag
= flag
['c'];
1090 cmd
->hflag
= flag
['h'];
1091 cmd
->lflag
= flag
['l'];
1092 cmd
->mflag
= flag
['m'];
1093 cmd
->nflag
= flag
['n'];
1094 cmd
->sflag
= flag
['s'];
1095 cmd
->tflag
= flag
['t'];
1096 cmd
->Ssflag
= flag
['S'];
1097 cmd
->Scflag
= flag
['C'];
1098 cmd
->makefileflag
= flag
['M'];
1103 if ((inetdflag
&& cmd
->nflag
)) {
1104 /* netid not allowed with inetdflag */
1105 warnx("cannot use netid flag with inetd flag");
1108 } else { /* 4.1 mode */
1109 pmflag
= 0; /* set pmflag only in tirpcmode */
1110 if (cmd
->nflag
) { /* netid needs TIRPC */
1111 warnx("cannot use netid flag without TIRPC");
1116 if (newstyle
&& (tblflag
|| cmd
->tflag
)) {
1117 warnx("cannot use table flags with newstyle");
1121 /* check no conflicts with file generation flags */
1122 nflags
= cmd
->cflag
+ cmd
->hflag
+ cmd
->lflag
+ cmd
->mflag
+
1123 cmd
->sflag
+ cmd
->nflag
+ cmd
->tflag
+ cmd
->Ssflag
+
1124 cmd
->Scflag
+ cmd
->makefileflag
;
1127 if (cmd
->outfile
!= NULL
|| cmd
->infile
== NULL
)
1129 } else if (cmd
->infile
== NULL
&&
1130 (cmd
->Ssflag
|| cmd
->Scflag
|| cmd
->makefileflag
)) {
1131 warnx("\"infile\" is required for template generation flags");
1134 warnx("cannot have more than one file generation flag");
1143 f_print(stderr
, "%s\n%s\n%s\n%s\n%s\n",
1144 "usage: rpcgen infile",
1145 " rpcgen [-abCLNTM] [-Dname[=value]] [-i size]"
1146 "[-I -P [-K seconds]] [-Y path] infile",
1147 " rpcgen [-c | -h | -l | -m | -t | -Sc | -Ss | -Sm]"
1148 "[-o outfile] [infile]",
1149 " rpcgen [-s nettype]* [-o outfile] [infile]",
1150 " rpcgen [-n netid]* [-o outfile] [infile]");
1158 f_print(stderr
, "options:\n");
1159 f_print(stderr
, "-a\t\tgenerate all files, including samples\n");
1160 f_print(stderr
, "-b\t\tbackward compatibility mode (generates code"
1161 "for SunOS 4.X)\n");
1162 f_print(stderr
, "-c\t\tgenerate XDR routines\n");
1163 f_print(stderr
, "-C\t\tANSI C mode\n");
1164 f_print(stderr
, "-Dname[=value]\tdefine a symbol (same as #define)\n");
1165 f_print(stderr
, "-h\t\tgenerate header file\n");
1166 f_print(stderr
, "-i size\t\tsize at which to start generating"
1168 f_print(stderr
, "-I\t\tgenerate code for inetd support in server\n");
1169 f_print(stderr
, "-K seconds\tserver exits after K seconds of"
1171 f_print(stderr
, "-l\t\tgenerate client side stubs\n");
1172 f_print(stderr
, "-L\t\tserver errors will be printed to syslog\n");
1173 f_print(stderr
, "-m\t\tgenerate server side stubs\n");
1174 f_print(stderr
, "-M\t\tgenerate MT-safe code\n");
1175 f_print(stderr
, "-n netid\tgenerate server code that supports"
1177 f_print(stderr
, "-N\t\tsupports multiple arguments and"
1179 f_print(stderr
, "-o outfile\tname of the output file\n");
1180 f_print(stderr
, "-P\t\tgenerate code for port monitoring support in server\n");
1181 f_print(stderr
, "-s nettype\tgenerate server code that supports named"
1183 f_print(stderr
, "-Sc\t\tgenerate sample client code that uses remote"
1185 f_print(stderr
, "-Ss\t\tgenerate sample server code that defines"
1186 "remote procedures\n");
1187 f_print(stderr
, "-Sm \t\tgenerate makefile template \n");
1189 f_print(stderr
, "-t\t\tgenerate RPC dispatch table\n");
1190 f_print(stderr
, "-T\t\tgenerate code to support RPC dispatch tables\n");
1191 f_print(stderr
, "-Y path\t\tpath where cpp is found\n");