2 * From @(#)rpc_main.c 1.30 89/03/30
4 * Copyright (c) 2010, Oracle America, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 * * Neither the name of the "Oracle America, Inc." nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
26 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * rpc_main.c, Top level of the RPC protocol compiler.
43 #include <sys/types.h>
44 #include <sys/param.h>
48 #include "rpc_parse.h"
53 #include "../version.h"
54 #define PACKAGE _libc_intl_domainname
56 #define EXTEND 1 /* alias for TRUE */
57 #define DONT_EXTEND 0 /* alias for FALSE */
61 int cflag
; /* xdr C routines */
62 int hflag
; /* header file */
63 int lflag
; /* client side stubs */
64 int mflag
; /* server side stubs */
65 int nflag
; /* netid flag */
66 int sflag
; /* server stubs for the given transport */
67 int tflag
; /* dispatch Table file */
68 int Ssflag
; /* produce server sample code */
69 int Scflag
; /* produce client sample code */
70 int makefileflag
; /* Generate a template Makefile */
71 const char *infile
; /* input module name */
72 const char *outfile
; /* output module name */
76 static const char *cmdname
;
78 static const char *svcclosetime
= "120";
79 static int cppDefined
; /* explicit path for C preprocessor */
80 static const char *CPP
= "/lib/cpp";
81 static const char CPPFLAGS
[] = "-C";
84 static const char *allv
[] =
86 "rpcgen", "-s", "udp", "-s", "tcp"
88 static int allc
= sizeof (allv
) / sizeof (allv
[0]);
89 static const char *allnv
[] =
91 "rpcgen", "-s", "netpath",
93 static int allnc
= sizeof (allnv
) / sizeof (allnv
[0]);
96 * machinations for handling expanding argument list
98 static void addarg (const char *); /* add another argument to the list */
99 static void putarg (int, const char *); /* put argument at specified location */
100 static void clear_args (void); /* clear argument list */
101 static void checkfiles (const char *, const char *);
102 /* check if out file already exists */
104 static void clear_args (void);
105 static char *extendfile (const char *file
, const char *ext
);
106 static void open_output (const char *infile
, const char *outfile
);
107 static void add_warning (void);
108 static void clear_args (void);
109 static void find_cpp (void);
110 static void open_input (const char *infile
, const char *define
);
111 static int check_nettype (const char *name
, const char *list_to_check
[]);
112 static void c_output (const char *infile
, const char *define
,
113 int extend
, const char *outfile
);
114 static void h_output (const char *infile
, const char *define
,
115 int extend
, const char *outfile
);
116 static void s_output (int argc
, const char *argv
[], const char *infile
,
117 const char *define
, int extend
,
118 const char *outfile
, int nomain
, int netflag
);
119 static void l_output (const char *infile
, const char *define
,
120 int extend
, const char *outfile
);
121 static void t_output (const char *infile
, const char *define
,
122 int extend
, const char *outfile
);
123 static void svc_output (const char *infile
, const char *define
,
124 int extend
, const char *outfile
);
125 static void clnt_output (const char *infile
, const char *define
,
126 int extend
, const char *outfile
);
127 static void mkfile_output (struct commandline
*cmd
);
128 static int do_registers (int argc
, const char *argv
[]);
129 static void addarg (const char *cp
);
130 static void putarg (int whereto
, const char *cp
);
131 static void checkfiles (const char *infile
, const char *outfile
);
132 static int parseargs (int argc
, const char *argv
[], struct commandline
*cmd
);
133 static void usage (FILE *stream
, int status
) __attribute__ ((noreturn
));
134 static void options_usage (FILE *stream
, int status
) __attribute__ ((noreturn
));
135 static void print_version (void);
136 static void c_initialize (void);
137 static char *generate_guard (const char *pathname
);
140 #define ARGLISTLEN 20
143 static const char *arglist
[ARGLISTLEN
];
144 static int argcount
= FIXEDARGS
;
147 int nonfatalerrors
; /* errors */
148 int inetdflag
/* = 1 */ ; /* Support for inetd *//* is now the default */
149 int pmflag
; /* Support for port monitors */
150 int logflag
; /* Use syslog instead of fprintf for errors */
151 int tblflag
; /* Support for dispatch table file */
152 int mtflag
; /* Support for MT */
155 /*length at which to start doing an inline */
157 int inlineflag
= INLINE
; /* length at which to start doing an inline. 3 = default
158 if 0, no xdr_inline code */
160 int indefinitewait
; /* If started by port monitors, hang till it wants */
161 int exitnow
; /* If started by port monitors, exit after the call */
162 int timerflag
; /* TRUE if !indefinite && !exitnow */
163 int newstyle
; /* newstyle of passing arguments (by value) */
164 int Cflag
= 1; /* ANSI C syntax */
165 int CCflag
; /* C++ files */
166 static int allfiles
; /* generate all files */
167 int tirpcflag
; /* generating code for tirpc, by default */
168 xdrfunc
*xdrfunc_head
; /* xdr function list */
169 xdrfunc
*xdrfunc_tail
; /* xdr function list */
172 main (int argc
, const char *argv
[])
174 struct commandline cmd
;
176 (void) memset ((char *) &cmd
, 0, sizeof (struct commandline
));
178 if (!parseargs (argc
, argv
, &cmd
))
181 if (cmd
.cflag
|| cmd
.hflag
|| cmd
.lflag
|| cmd
.tflag
|| cmd
.sflag
||
182 cmd
.mflag
|| cmd
.nflag
|| cmd
.Ssflag
|| cmd
.Scflag
)
184 checkfiles (cmd
.infile
, cmd
.outfile
);
187 checkfiles (cmd
.infile
, NULL
);
190 c_output (cmd
.infile
, "-DRPC_XDR", DONT_EXTEND
, cmd
.outfile
);
192 h_output (cmd
.infile
, "-DRPC_HDR", DONT_EXTEND
, cmd
.outfile
);
194 l_output (cmd
.infile
, "-DRPC_CLNT", DONT_EXTEND
, cmd
.outfile
);
195 else if (cmd
.sflag
|| cmd
.mflag
|| (cmd
.nflag
))
196 s_output (argc
, argv
, cmd
.infile
, "-DRPC_SVC", DONT_EXTEND
,
197 cmd
.outfile
, cmd
.mflag
, cmd
.nflag
);
199 t_output (cmd
.infile
, "-DRPC_TBL", DONT_EXTEND
, cmd
.outfile
);
201 svc_output (cmd
.infile
, "-DRPC_SERVER", DONT_EXTEND
, cmd
.outfile
);
203 clnt_output (cmd
.infile
, "-DRPC_CLIENT", DONT_EXTEND
, cmd
.outfile
);
204 else if (cmd
.makefileflag
)
205 mkfile_output (&cmd
);
208 /* the rescans are required, since cpp may effect input */
209 c_output (cmd
.infile
, "-DRPC_XDR", EXTEND
, "_xdr.c");
211 h_output (cmd
.infile
, "-DRPC_HDR", EXTEND
, ".h");
213 l_output (cmd
.infile
, "-DRPC_CLNT", EXTEND
, "_clnt.c");
215 if (inetdflag
|| !tirpcflag
)
216 s_output (allc
, allv
, cmd
.infile
, "-DRPC_SVC", EXTEND
,
217 "_svc.c", cmd
.mflag
, cmd
.nflag
);
219 s_output (allnc
, allnv
, cmd
.infile
, "-DRPC_SVC",
220 EXTEND
, "_svc.c", cmd
.mflag
, cmd
.nflag
);
224 t_output (cmd
.infile
, "-DRPC_TBL", EXTEND
, "_tbl.i");
229 svc_output (cmd
.infile
, "-DRPC_SERVER", EXTEND
, "_server.c");
231 clnt_output (cmd
.infile
, "-DRPC_CLIENT", EXTEND
, "_client.c");
233 if (allfiles
|| (cmd
.makefileflag
== 1))
236 mkfile_output (&cmd
);
240 return nonfatalerrors
;
244 * add extension to filename
247 extendfile (const char *file
, const char *ext
)
252 res
= alloc (strlen (file
) + strlen (ext
) + 1);
255 p
= strrchr (file
, '.');
257 p
= file
+ strlen (file
);
259 strcpy (res
+ (p
- file
), ext
);
264 * Open output file with given extension
267 open_output (const char *infile
, const char *outfile
)
275 if (infile
!= NULL
&& streq (outfile
, infile
))
277 fprintf (stderr
, _ ("%s: output would overwrite %s\n"), cmdname
,
281 fout
= fopen (outfile
, "w");
284 fprintf (stderr
, _ ("%s: unable to open %s: %m\n"), cmdname
, outfile
);
287 record_open (outfile
);
290 /* Close the output file and check for write errors. */
292 close_output (const char *outfile
)
294 if (fclose (fout
) == EOF
)
296 fprintf (stderr
, _("%s: while writing output %s: %m"), cmdname
,
297 outfile
?: "<stdout>");
305 fprintf (fout
, "/*\n");
306 fprintf (fout
, " * Please do not edit this file.\n");
307 fprintf (fout
, " * It was generated using rpcgen.\n");
308 fprintf (fout
, " */\n\n");
311 /* clear list of arguments */
316 for (i
= FIXEDARGS
; i
< ARGLISTLEN
; ++i
)
318 argcount
= FIXEDARGS
;
321 /* make sure that a CPP exists */
327 if (stat (CPP
, &buf
) == 0)
330 if (cppDefined
) /* user specified cpp but it does not exist */
332 fprintf (stderr
, _ ("cannot find C preprocessor: %s\n"), CPP
);
336 /* fall back to system CPP */
341 * Open input file with given define for C-preprocessor
344 open_input (const char *infile
, const char *define
)
348 infilename
= (infile
== NULL
) ? "<stdin>" : infile
;
360 putarg (1, CPPFLAGS
);
364 addarg ((char *) NULL
);
368 execvp (arglist
[0], (char **) arglist
);
371 fprintf (stderr
, _ ("cannot find C preprocessor: %s\n"), CPP
);
381 fin
= fdopen (pd
[0], "r");
384 fprintf (stderr
, "%s: ", cmdname
);
390 /* Close the connection to the C-preprocessor and check for successfull
398 /* Check the termination status. */
399 if (waitpid (cpp_pid
, &status
, 0) < 0)
404 if (WIFSIGNALED (status
) || WEXITSTATUS (status
) != 0)
406 if (WIFSIGNALED (status
))
407 fprintf (stderr
, _("%s: C preprocessor failed with signal %d\n"),
408 cmdname
, WTERMSIG (status
));
410 fprintf (stderr
, _("%s: C preprocessor failed with exit code %d\n"),
411 cmdname
, WEXITSTATUS (status
));
416 /* valid tirpc nettypes */
417 static const char *valid_ti_nettypes
[] =
431 /* valid inetd nettypes */
432 static const char *valid_i_nettypes
[] =
440 check_nettype (const char *name
, const char *list_to_check
[])
443 for (i
= 0; list_to_check
[i
] != NULL
; i
++)
445 if (strcmp (name
, list_to_check
[i
]) == 0)
450 fprintf (stderr
, _ ("illegal nettype: `%s'\n"), name
);
455 * Compile into an XDR routine output file
459 c_output (const char *infile
, const char *define
, int extend
,
464 const char *outfilename
;
468 open_input (infile
, define
);
469 outfilename
= extend
? extendfile (infile
, outfile
) : outfile
;
470 open_output (infile
, outfilename
);
472 if (infile
&& (include
= extendfile (infile
, ".h")))
474 fprintf (fout
, "#include \"%s\"\n", include
);
476 /* .h file already contains rpc/rpc.h */
479 fprintf (fout
, "#include <rpc/rpc.h>\n");
481 while ((def
= get_definition ()) != NULL
)
484 if (extend
&& tell
== ftell (fout
))
485 unlink (outfilename
);
487 close_output (outfilename
);
494 /* add all the starting basic types */
497 add_type (1, "long");
498 add_type (1, "short");
499 add_type (1, "bool");
501 add_type (1, "u_int");
502 add_type (1, "u_long");
503 add_type (1, "u_short");
507 char rpcgen_table_dcl
[] = "struct rpcgen_table {\n\
509 xdrproc_t xdr_arg;\n\
511 xdrproc_t xdr_res;\n\
517 generate_guard (const char *pathname
)
519 const char *filename
;
522 filename
= strrchr (pathname
, '/'); /* find last component */
523 filename
= ((filename
== NULL
) ? pathname
: filename
+ 1);
524 guard
= extendfile (filename
, "_H_RPCGEN");
525 /* convert to upper case */
530 *tmp
= toupper (*tmp
);
538 * Compile into an XDR header file
543 h_output (const char *infile
, const char *define
, int extend
,
548 const char *ifilename
;
549 const char *outfilename
;
554 open_input (infile
, define
);
555 outfilename
= extend
? extendfile (infile
, outfile
) : outfile
;
556 open_output (infile
, outfilename
);
558 ifilename
= (infile
== NULL
) ? "STDIN" : infile
;
559 guard
= generate_guard (outfilename
? outfilename
: ifilename
);
561 fprintf (fout
, "#ifndef _%s\n#define _%s\n\n", guard
,
564 fprintf (fout
, "#include <rpc/rpc.h>\n\n");
568 fprintf (fout
, "#include <pthread.h>\n");
571 /* put the C++ support */
572 if (Cflag
&& !CCflag
)
574 fprintf (fout
, "\n#ifdef __cplusplus\n");
575 fprintf (fout
, "extern \"C\" {\n");
576 fprintf (fout
, "#endif\n\n");
580 /* print data definitions */
581 while ((def
= get_definition ()) != NULL
)
586 /* print function declarations.
587 Do this after data definitions because they might be used as
588 arguments for functions */
589 for (l
= defined
; l
!= NULL
; l
= l
->next
)
591 print_funcdef (l
->val
);
593 /* Now print all xdr func declarations */
594 if (xdrfunc_head
!= NULL
)
596 fprintf (fout
, "\n/* the xdr functions */\n");
599 fprintf (fout
, "\n#ifdef __cplusplus\n");
600 fprintf (fout
, "extern \"C\" {\n");
601 fprintf (fout
, "#endif\n");
605 xdrfuncp
= xdrfunc_head
;
606 while (xdrfuncp
!= NULL
)
608 print_xdr_func_def (xdrfuncp
->name
,
609 xdrfuncp
->pointerp
, 2);
610 xdrfuncp
= xdrfuncp
->next
;
617 for (i
= 1; i
< 3; ++i
)
620 fprintf (fout
, "\n#if defined(__STDC__) || defined(__cplusplus)\n");
622 fprintf (fout
, "\n#else /* K&R C */\n");
624 xdrfuncp
= xdrfunc_head
;
625 while (xdrfuncp
!= NULL
)
627 print_xdr_func_def (xdrfuncp
->name
,
628 xdrfuncp
->pointerp
, i
);
629 xdrfuncp
= xdrfuncp
->next
;
632 fprintf (fout
, "\n#endif /* K&R C */\n");
636 if (extend
&& tell
== ftell (fout
))
638 unlink (outfilename
);
642 fprintf (fout
, "%s", rpcgen_table_dcl
);
647 fprintf (fout
, "\n#ifdef __cplusplus\n");
648 fprintf (fout
, "}\n");
649 fprintf (fout
, "#endif\n");
652 fprintf (fout
, "\n#endif /* !_%s */\n", guard
);
655 close_output (outfilename
);
659 * Compile into an RPC service
662 s_output (int argc
, const char *argv
[], const char *infile
, const char *define
,
663 int extend
, const char *outfile
, int nomain
, int netflag
)
667 int foundprogram
= 0;
668 const char *outfilename
;
670 open_input (infile
, define
);
671 outfilename
= extend
? extendfile (infile
, outfile
) : outfile
;
672 open_output (infile
, outfilename
);
674 if (infile
&& (include
= extendfile (infile
, ".h")))
676 fprintf (fout
, "#include \"%s\"\n", include
);
680 fprintf (fout
, "#include <rpc/rpc.h>\n");
682 fprintf (fout
, "#include <stdio.h>\n");
683 fprintf (fout
, "#include <stdlib.h>\n");
684 fprintf (fout
, "#include <rpc/pmap_clnt.h>\n");
686 fprintf (fout
, "#include <string.h>\n");
687 if (strcmp (svcclosetime
, "-1") == 0)
689 else if (strcmp (svcclosetime
, "0") == 0)
691 else if (inetdflag
|| pmflag
)
693 fprintf (fout
, "#include <signal.h>\n");
697 if (!tirpcflag
&& inetdflag
)
698 fprintf (fout
, "#include <sys/ioctl.h> /* ioctl, TIOCNOTTY */\n");
699 if (Cflag
&& (inetdflag
|| pmflag
))
701 fprintf (fout
, "#include <sys/types.h> /* open */\n");
702 fprintf (fout
, "#include <sys/stat.h> /* open */\n");
703 fprintf (fout
, "#include <fcntl.h> /* open */\n");
704 fprintf (fout
, "#include <unistd.h> /* getdtablesize */\n");
706 if (tirpcflag
&& !(Cflag
&& (inetdflag
|| pmflag
)))
707 fprintf (fout
, "#include <sys/types.h>\n");
709 fprintf (fout
, "#include <memory.h>\n");
710 if (inetdflag
|| !tirpcflag
)
712 fprintf (fout
, "#include <sys/socket.h>\n");
713 fprintf (fout
, "#include <netinet/in.h>\n");
716 if ((netflag
|| pmflag
) && tirpcflag
&& !nomain
)
718 fprintf (fout
, "#include <netconfig.h>\n");
720 if ( /*timerflag && */ tirpcflag
)
721 fprintf (fout
, "#include <sys/resource.h> /* rlimit */\n");
722 if (logflag
|| inetdflag
|| pmflag
)
724 fprintf (fout
, "#include <syslog.h>\n");
729 fprintf (fout
, "\n#ifndef SIG_PF\n#define SIG_PF void(*)(int)\n#endif\n");
732 fprintf (fout
, "\n#define _RPCSVC_CLOSEDOWN %s\n", svcclosetime
);
733 while ((def
= get_definition ()) != NULL
)
735 foundprogram
|= (def
->def_kind
== DEF_PROGRAM
);
737 if (extend
&& !foundprogram
)
739 unlink (outfilename
);
742 write_most (infile
, netflag
, nomain
);
745 if (!do_registers (argc
, argv
))
748 unlink (outfilename
);
754 close_output (outfilename
);
758 * generate client side stubs
761 l_output (const char *infile
, const char *define
, int extend
,
766 int foundprogram
= 0;
767 const char *outfilename
;
769 open_input (infile
, define
);
770 outfilename
= extend
? extendfile (infile
, outfile
) : outfile
;
771 open_output (infile
, outfilename
);
774 fprintf (fout
, "#include <memory.h> /* for memset */\n");
775 if (infile
&& (include
= extendfile (infile
, ".h")))
777 fprintf (fout
, "#include \"%s\"\n", include
);
781 fprintf (fout
, "#include <rpc/rpc.h>\n");
782 while ((def
= get_definition ()) != NULL
)
784 foundprogram
|= (def
->def_kind
== DEF_PROGRAM
);
786 if (extend
&& !foundprogram
)
788 unlink (outfilename
);
793 close_output (outfilename
);
797 * generate the dispatch table
800 t_output (const char *infile
, const char *define
, int extend
,
804 int foundprogram
= 0;
805 const char *outfilename
;
807 open_input (infile
, define
);
808 outfilename
= extend
? extendfile (infile
, outfile
) : outfile
;
809 open_output (infile
, outfilename
);
811 while ((def
= get_definition ()) != NULL
)
813 foundprogram
|= (def
->def_kind
== DEF_PROGRAM
);
815 if (extend
&& !foundprogram
)
817 unlink (outfilename
);
822 close_output (outfilename
);
825 /* sample routine for the server template */
827 svc_output (const char *infile
, const char *define
, int extend
,
832 const char *outfilename
;
835 open_input (infile
, define
);
836 outfilename
= extend
? extendfile (infile
, outfile
) : outfile
;
837 checkfiles (infile
, outfilename
);
838 /*check if outfile already exists.
839 if so, print an error message and exit */
840 open_output (infile
, outfilename
);
843 if (infile
&& (include
= extendfile (infile
, ".h")))
845 fprintf (fout
, "#include \"%s\"\n", include
);
849 fprintf (fout
, "#include <rpc/rpc.h>\n");
852 while ((def
= get_definition ()) != NULL
)
854 write_sample_svc (def
);
856 if (extend
&& tell
== ftell (fout
))
858 unlink (outfilename
);
861 close_output (outfilename
);
865 /* sample main routine for client */
867 clnt_output (const char *infile
, const char *define
, int extend
,
872 const char *outfilename
;
876 open_input (infile
, define
);
877 outfilename
= extend
? extendfile (infile
, outfile
) : outfile
;
878 checkfiles (infile
, outfilename
);
879 /*check if outfile already exists.
880 if so, print an error message and exit */
882 open_output (infile
, outfilename
);
884 if (infile
&& (include
= extendfile (infile
, ".h")))
886 fprintf (fout
, "#include \"%s\"\n", include
);
890 fprintf (fout
, "#include <rpc/rpc.h>\n");
892 while ((def
= get_definition ()) != NULL
)
894 has_program
+= write_sample_clnt (def
);
898 write_sample_clnt_main ();
900 if (extend
&& tell
== ftell (fout
))
902 unlink (outfilename
);
905 close_output (outfilename
);
908 static const char space
[] = " ";
911 file_name (const char *file
, const char *ext
)
914 temp
= extendfile (file
, ext
);
916 if (access (temp
, F_OK
) != -1)
920 return (char *) space
;
924 mkfile_output (struct commandline
*cmd
)
927 char *clientname
, *clntname
, *xdrname
, *hdrname
;
928 char *servername
, *svcname
, *servprogname
, *clntprogname
;
930 svcname
= file_name (cmd
->infile
, "_svc.c");
931 clntname
= file_name (cmd
->infile
, "_clnt.c");
932 xdrname
= file_name (cmd
->infile
, "_xdr.c");
933 hdrname
= file_name (cmd
->infile
, ".h");
937 servername
= extendfile (cmd
->infile
, "_server.c");
938 clientname
= extendfile (cmd
->infile
, "_client.c");
942 servername
= (char *) space
;
943 clientname
= (char *) space
;
945 servprogname
= extendfile (cmd
->infile
, "_server");
946 clntprogname
= extendfile (cmd
->infile
, "_client");
952 mkfilename
= alloc (strlen ("Makefile.") + strlen (cmd
->infile
) + 1);
953 if (mkfilename
== NULL
)
955 temp
= rindex (cmd
->infile
, '.');
956 cp
= stpcpy (mkfilename
, "Makefile.");
958 *((char *) stpncpy (cp
, cmd
->infile
, temp
- cmd
->infile
)) = '\0';
960 stpcpy (cp
, cmd
->infile
);
964 mkfilename
= (char *) cmd
->outfile
;
966 checkfiles (NULL
, mkfilename
);
967 open_output (NULL
, mkfilename
);
969 fprintf (fout
, "\n# This is a template Makefile generated by rpcgen\n");
971 f_print (fout
, "\n# Parameters\n\n");
973 f_print (fout
, "CLIENT = %s\nSERVER = %s\n\n", clntprogname
, servprogname
);
974 f_print (fout
, "SOURCES_CLNT.c = \nSOURCES_CLNT.h = \n");
975 f_print (fout
, "SOURCES_SVC.c = \nSOURCES_SVC.h = \n");
976 f_print (fout
, "SOURCES.x = %s\n\n", cmd
->infile
);
977 f_print (fout
, "TARGETS_SVC.c = %s %s %s \n",
978 svcname
, servername
, xdrname
);
979 f_print (fout
, "TARGETS_CLNT.c = %s %s %s \n",
980 clntname
, clientname
, xdrname
);
981 f_print (fout
, "TARGETS = %s %s %s %s %s %s\n\n",
982 hdrname
, xdrname
, clntname
,
983 svcname
, clientname
, servername
);
985 f_print (fout
, "OBJECTS_CLNT = $(SOURCES_CLNT.c:%%.c=%%.o) \
986 $(TARGETS_CLNT.c:%%.c=%%.o)");
988 f_print (fout
, "\nOBJECTS_SVC = $(SOURCES_SVC.c:%%.c=%%.o) \
989 $(TARGETS_SVC.c:%%.c=%%.o)");
991 f_print (fout
, "\n# Compiler flags \n");
993 fprintf (fout
, "\nCPPFLAGS += -D_REENTRANT\nCFLAGS += -g \nLDLIBS \
994 += -lnsl -lpthread \n ");
996 f_print (fout
, "\nCFLAGS += -g \nLDLIBS += -lnsl\n");
997 f_print (fout
, "RPCGENFLAGS = \n");
999 f_print (fout
, "\n# Targets \n\n");
1001 f_print (fout
, "all : $(CLIENT) $(SERVER)\n\n");
1002 f_print (fout
, "$(TARGETS) : $(SOURCES.x) \n");
1003 f_print (fout
, "\trpcgen $(RPCGENFLAGS) $(SOURCES.x)\n\n");
1004 f_print (fout
, "$(OBJECTS_CLNT) : $(SOURCES_CLNT.c) $(SOURCES_CLNT.h) \
1005 $(TARGETS_CLNT.c) \n\n");
1007 f_print (fout
, "$(OBJECTS_SVC) : $(SOURCES_SVC.c) $(SOURCES_SVC.h) \
1008 $(TARGETS_SVC.c) \n\n");
1009 f_print (fout
, "$(CLIENT) : $(OBJECTS_CLNT) \n");
1010 f_print (fout
, "\t$(LINK.c) -o $(CLIENT) $(OBJECTS_CLNT) \
1012 f_print (fout
, "$(SERVER) : $(OBJECTS_SVC) \n");
1013 f_print (fout
, "\t$(LINK.c) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS)\n\n ");
1014 f_print (fout
, "clean:\n\t $(RM) core $(TARGETS) $(OBJECTS_CLNT) \
1015 $(OBJECTS_SVC) $(CLIENT) $(SERVER)\n\n");
1016 close_output (mkfilename
);
1018 free (clntprogname
);
1019 free (servprogname
);
1020 if (servername
!= space
)
1022 if (clientname
!= space
)
1024 if (mkfilename
!= (char *) cmd
->outfile
)
1026 if (svcname
!= space
)
1028 if (clntname
!= space
)
1030 if (xdrname
!= space
)
1032 if (hdrname
!= space
)
1037 * Perform registrations for service output
1038 * Return 0 if failed; 1 otherwise.
1041 do_registers (int argc
, const char *argv
[])
1045 if (inetdflag
|| !tirpcflag
)
1047 for (i
= 1; i
< argc
; i
++)
1049 if (streq (argv
[i
], "-s"))
1051 if (!check_nettype (argv
[i
+ 1], valid_i_nettypes
))
1053 write_inetd_register (argv
[i
+ 1]);
1060 for (i
= 1; i
< argc
; i
++)
1061 if (streq (argv
[i
], "-s"))
1063 if (!check_nettype (argv
[i
+ 1], valid_ti_nettypes
))
1065 write_nettype_register (argv
[i
+ 1]);
1068 else if (streq (argv
[i
], "-n"))
1070 write_netid_register (argv
[i
+ 1]);
1078 * Add another argument to the arg list
1081 addarg (const char *cp
)
1083 if (argcount
>= ARGLISTLEN
)
1085 fprintf (stderr
, _("rpcgen: too many defines\n"));
1089 arglist
[argcount
++] = cp
;
1093 putarg (int whereto
, const char *cp
)
1095 if (whereto
>= ARGLISTLEN
)
1097 fprintf (stderr
, _("rpcgen: arglist coding error\n"));
1101 arglist
[whereto
] = cp
;
1105 * if input file is stdin and an output file is specified then complain
1106 * if the file already exists. Otherwise the file may get overwritten
1107 * If input file does not exist, exit with an error
1111 checkfiles (const char *infile
, const char *outfile
)
1115 if (infile
) /* infile ! = NULL */
1116 if (stat (infile
, &buf
) < 0)
1123 if (stat (outfile
, &buf
) < 0)
1124 return; /* file does not exist */
1128 /* TRANS: the file will not be removed; this is an
1129 TRANS: informative message. */
1130 _("file `%s' already exists and may be overwritten\n"),
1138 * Parse command line arguments
1141 parseargs (int argc
, const char *argv
[], struct commandline
*cmd
)
1146 char flag
[(1 << 8 * sizeof (char))];
1150 cmd
->infile
= cmd
->outfile
= NULL
;
1168 for (i
= 1; i
< argc
; i
++)
1170 if (argv
[i
][0] != '-')
1175 _("Cannot specify more than one input file!\n"));
1178 cmd
->infile
= argv
[i
];
1180 else if (strcmp (argv
[i
], "--help") == 0)
1182 else if (strcmp (argv
[i
], "--version") == 0)
1186 for (j
= 1; argv
[i
][j
] != 0; j
++)
1204 /* sample flag: Ss or Sc.
1205 Ss means set flag['S'];
1206 Sc means set flag['C'];
1207 Sm means set flag['M']; */
1208 c
= argv
[i
][++j
]; /* get next char */
1222 case 'C': /* ANSI C syntax */
1226 case 'k': /* K&R C syntax */
1230 case 'b': /* turn TIRPC flag off for
1231 generating backward compatible
1236 case '5': /* turn TIRPC flag on for
1237 generating SysVr4 compatible
1255 svcclosetime
= argv
[i
];
1268 inlineflag
= atoi (argv
[i
]);
1273 if (argv
[i
][j
- 1] != '-' ||
1274 argv
[i
][j
+ 1] != 0)
1285 if (!streq (argv
[i
], "udp") &&
1286 !streq (argv
[i
], "tcp"))
1293 cmd
->outfile
= argv
[i
];
1297 if (argv
[i
][j
- 1] != '-')
1305 size_t len
= strlen (argv
[i
]);
1306 pathbuf
= malloc (len
+ 5);
1307 if (pathbuf
== NULL
)
1312 stpcpy (stpcpy (pathbuf
,
1329 cmd
->cflag
= flag
['c'];
1330 cmd
->hflag
= flag
['h'];
1331 cmd
->lflag
= flag
['l'];
1332 cmd
->mflag
= flag
['m'];
1333 cmd
->nflag
= flag
['n'];
1334 cmd
->sflag
= flag
['s'];
1335 cmd
->tflag
= flag
['t'];
1336 cmd
->Ssflag
= flag
['S'];
1337 cmd
->Scflag
= flag
['C'];
1338 cmd
->makefileflag
= flag
['M'];
1340 #ifndef _RPC_THREAD_SAFE_
1341 if (mtflag
|| newstyle
)
1343 /* glibc doesn't support these flags. */
1345 _("This implementation doesn't support newstyle or MT-safe code!\n"));
1351 pmflag
= inetdflag
? 0 : 1; /* pmflag or inetdflag is always TRUE */
1352 if ((inetdflag
&& cmd
->nflag
))
1353 { /* netid not allowed with inetdflag */
1354 fprintf (stderr
, _("Cannot use netid flag with inetd flag!\n"));
1360 pmflag
= 0; /* set pmflag only in tirpcmode */
1362 { /* netid needs TIRPC */
1363 f_print (stderr
, _("Cannot use netid flag without TIRPC!\n"));
1368 if (newstyle
&& (tblflag
|| cmd
->tflag
))
1370 f_print (stderr
, _("Cannot use table flags with newstyle!\n"));
1374 /* check no conflicts with file generation flags */
1375 nflags
= cmd
->cflag
+ cmd
->hflag
+ cmd
->lflag
+ cmd
->mflag
+
1376 cmd
->sflag
+ cmd
->nflag
+ cmd
->tflag
+ cmd
->Ssflag
+ cmd
->Scflag
;
1380 if (cmd
->outfile
!= NULL
|| cmd
->infile
== NULL
)
1385 else if (cmd
->infile
== NULL
&&
1386 (cmd
->Ssflag
|| cmd
->Scflag
|| cmd
->makefileflag
))
1389 _("\"infile\" is required for template generation flags.\n"));
1394 fprintf (stderr
, _("Cannot have more than one file generation flag!\n"));
1401 usage (FILE *stream
, int status
)
1403 fprintf (stream
, _("usage: %s infile\n"), cmdname
);
1404 fprintf (stream
, _("\t%s [-abkCLNTM][-Dname[=value]] [-i size] \
1405 [-I [-K seconds]] [-Y path] infile\n"), cmdname
);
1406 fprintf (stream
, _("\t%s [-c | -h | -l | -m | -t | -Sc | -Ss | -Sm] \
1407 [-o outfile] [infile]\n"), cmdname
);
1408 fprintf (stream
, _("\t%s [-s nettype]* [-o outfile] [infile]\n"), cmdname
);
1409 fprintf (stream
, _("\t%s [-n netid]* [-o outfile] [infile]\n"), cmdname
);
1410 options_usage (stream
, status
);
1415 options_usage (FILE *stream
, int status
)
1417 f_print (stream
, _("options:\n"));
1418 f_print (stream
, _("-a\t\tgenerate all files, including samples\n"));
1419 f_print (stream
, _("-b\t\tbackward compatibility mode (generates code for SunOS 4.1)\n"));
1420 f_print (stream
, _("-c\t\tgenerate XDR routines\n"));
1421 f_print (stream
, _("-C\t\tANSI C mode\n"));
1422 f_print (stream
, _("-Dname[=value]\tdefine a symbol (same as #define)\n"));
1423 f_print (stream
, _("-h\t\tgenerate header file\n"));
1424 f_print (stream
, _("-i size\t\tsize at which to start generating inline code\n"));
1425 f_print (stream
, _("-I\t\tgenerate code for inetd support in server (for SunOS 4.1)\n"));
1426 f_print (stream
, _("-K seconds\tserver exits after K seconds of inactivity\n"));
1427 f_print (stream
, _("-l\t\tgenerate client side stubs\n"));
1428 f_print (stream
, _("-L\t\tserver errors will be printed to syslog\n"));
1429 f_print (stream
, _("-m\t\tgenerate server side stubs\n"));
1430 f_print (stream
, _("-M\t\tgenerate MT-safe code\n"));
1431 f_print (stream
, _("-n netid\tgenerate server code that supports named netid\n"));
1432 f_print (stream
, _("-N\t\tsupports multiple arguments and call-by-value\n"));
1433 f_print (stream
, _("-o outfile\tname of the output file\n"));
1434 f_print (stream
, _("-s nettype\tgenerate server code that supports named nettype\n"));
1435 f_print (stream
, _("-Sc\t\tgenerate sample client code that uses remote procedures\n"));
1436 f_print (stream
, _("-Ss\t\tgenerate sample server code that defines remote procedures\n"));
1437 f_print (stream
, _("-Sm \t\tgenerate makefile template \n"));
1438 f_print (stream
, _("-t\t\tgenerate RPC dispatch table\n"));
1439 f_print (stream
, _("-T\t\tgenerate code to support RPC dispatch tables\n"));
1440 f_print (stream
, _("-Y path\t\tdirectory name to find C preprocessor (cpp)\n"));
1442 f_print (stream
, _("\n\
1443 For bug reporting instructions, please see:\n\
1444 %s.\n"), REPORT_BUGS_TO
);
1449 print_version (void)
1451 printf ("rpcgen %s%s\n", PKGVERSION
, VERSION
);