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.
44 #include <sys/types.h>
45 #include <sys/param.h>
49 #include "rpc_parse.h"
54 #include "../version.h"
55 #define PACKAGE _libc_intl_domainname
57 #define EXTEND 1 /* alias for TRUE */
58 #define DONT_EXTEND 0 /* alias for FALSE */
62 int cflag
; /* xdr C routines */
63 int hflag
; /* header file */
64 int lflag
; /* client side stubs */
65 int mflag
; /* server side stubs */
66 int nflag
; /* netid flag */
67 int sflag
; /* server stubs for the given transport */
68 int tflag
; /* dispatch Table file */
69 int Ssflag
; /* produce server sample code */
70 int Scflag
; /* produce client sample code */
71 int makefileflag
; /* Generate a template Makefile */
72 const char *infile
; /* input module name */
73 const char *outfile
; /* output module name */
77 static const char *cmdname
;
79 static const char *svcclosetime
= "120";
80 static int cppDefined
; /* explicit path for C preprocessor */
81 static const char *CPP
= "/lib/cpp";
82 static const char CPPFLAGS
[] = "-C";
85 static const char *allv
[] =
87 "rpcgen", "-s", "udp", "-s", "tcp"
89 static int allc
= sizeof (allv
) / sizeof (allv
[0]);
90 static const char *allnv
[] =
92 "rpcgen", "-s", "netpath",
94 static int allnc
= sizeof (allnv
) / sizeof (allnv
[0]);
97 * machinations for handling expanding argument list
99 static void addarg (const char *); /* add another argument to the list */
100 static void putarg (int, const char *); /* put argument at specified location */
101 static void clear_args (void); /* clear argument list */
102 static void checkfiles (const char *, const char *);
103 /* check if out file already exists */
105 static void clear_args (void);
106 static char *extendfile (const char *file
, const char *ext
);
107 static void open_output (const char *infile
, const char *outfile
);
108 static void add_warning (void);
109 static void clear_args (void);
110 static void find_cpp (void);
111 static void open_input (const char *infile
, const char *define
);
112 static int check_nettype (const char *name
, const char *list_to_check
[]);
113 static void c_output (const char *infile
, const char *define
,
114 int extend
, const char *outfile
);
115 static void h_output (const char *infile
, const char *define
,
116 int extend
, const char *outfile
);
117 static void s_output (int argc
, const char *argv
[], const char *infile
,
118 const char *define
, int extend
,
119 const char *outfile
, int nomain
, int netflag
);
120 static void l_output (const char *infile
, const char *define
,
121 int extend
, const char *outfile
);
122 static void t_output (const char *infile
, const char *define
,
123 int extend
, const char *outfile
);
124 static void svc_output (const char *infile
, const char *define
,
125 int extend
, const char *outfile
);
126 static void clnt_output (const char *infile
, const char *define
,
127 int extend
, const char *outfile
);
128 static void mkfile_output (struct commandline
*cmd
);
129 static int do_registers (int argc
, const char *argv
[]);
130 static void addarg (const char *cp
);
131 static void putarg (int whereto
, const char *cp
);
132 static void checkfiles (const char *infile
, const char *outfile
);
133 static int parseargs (int argc
, const char *argv
[], struct commandline
*cmd
);
134 static void usage (FILE *stream
, int status
) __attribute__ ((noreturn
));
135 static void options_usage (FILE *stream
, int status
) __attribute__ ((noreturn
));
136 static void print_version (void);
137 static void c_initialize (void);
138 static char *generate_guard (const char *pathname
);
141 #define ARGLISTLEN 20
144 static const char *arglist
[ARGLISTLEN
];
145 static int argcount
= FIXEDARGS
;
148 int nonfatalerrors
; /* errors */
149 int inetdflag
/* = 1 */ ; /* Support for inetd *//* is now the default */
150 int pmflag
; /* Support for port monitors */
151 int logflag
; /* Use syslog instead of fprintf for errors */
152 int tblflag
; /* Support for dispatch table file */
153 int mtflag
; /* Support for MT */
156 /*length at which to start doing an inline */
158 int inlineflag
= INLINE
; /* length at which to start doing an inline. 3 = default
159 if 0, no xdr_inline code */
161 int indefinitewait
; /* If started by port monitors, hang till it wants */
162 int exitnow
; /* If started by port monitors, exit after the call */
163 int timerflag
; /* TRUE if !indefinite && !exitnow */
164 int newstyle
; /* newstyle of passing arguments (by value) */
165 int Cflag
= 1; /* ANSI C syntax */
166 int CCflag
; /* C++ files */
167 static int allfiles
; /* generate all files */
168 int tirpcflag
; /* generating code for tirpc, by default */
169 xdrfunc
*xdrfunc_head
; /* xdr function list */
170 xdrfunc
*xdrfunc_tail
; /* xdr function list */
173 main (int argc
, const char *argv
[])
175 struct commandline cmd
;
177 setlocale (LC_ALL
, "");
178 textdomain (_libc_intl_domainname
);
180 (void) memset ((char *) &cmd
, 0, sizeof (struct commandline
));
182 if (!parseargs (argc
, argv
, &cmd
))
185 if (cmd
.cflag
|| cmd
.hflag
|| cmd
.lflag
|| cmd
.tflag
|| cmd
.sflag
||
186 cmd
.mflag
|| cmd
.nflag
|| cmd
.Ssflag
|| cmd
.Scflag
)
188 checkfiles (cmd
.infile
, cmd
.outfile
);
191 checkfiles (cmd
.infile
, NULL
);
194 c_output (cmd
.infile
, "-DRPC_XDR", DONT_EXTEND
, cmd
.outfile
);
196 h_output (cmd
.infile
, "-DRPC_HDR", DONT_EXTEND
, cmd
.outfile
);
198 l_output (cmd
.infile
, "-DRPC_CLNT", DONT_EXTEND
, cmd
.outfile
);
199 else if (cmd
.sflag
|| cmd
.mflag
|| (cmd
.nflag
))
200 s_output (argc
, argv
, cmd
.infile
, "-DRPC_SVC", DONT_EXTEND
,
201 cmd
.outfile
, cmd
.mflag
, cmd
.nflag
);
203 t_output (cmd
.infile
, "-DRPC_TBL", DONT_EXTEND
, cmd
.outfile
);
205 svc_output (cmd
.infile
, "-DRPC_SERVER", DONT_EXTEND
, cmd
.outfile
);
207 clnt_output (cmd
.infile
, "-DRPC_CLIENT", DONT_EXTEND
, cmd
.outfile
);
208 else if (cmd
.makefileflag
)
209 mkfile_output (&cmd
);
212 /* the rescans are required, since cpp may effect input */
213 c_output (cmd
.infile
, "-DRPC_XDR", EXTEND
, "_xdr.c");
215 h_output (cmd
.infile
, "-DRPC_HDR", EXTEND
, ".h");
217 l_output (cmd
.infile
, "-DRPC_CLNT", EXTEND
, "_clnt.c");
219 if (inetdflag
|| !tirpcflag
)
220 s_output (allc
, allv
, cmd
.infile
, "-DRPC_SVC", EXTEND
,
221 "_svc.c", cmd
.mflag
, cmd
.nflag
);
223 s_output (allnc
, allnv
, cmd
.infile
, "-DRPC_SVC",
224 EXTEND
, "_svc.c", cmd
.mflag
, cmd
.nflag
);
228 t_output (cmd
.infile
, "-DRPC_TBL", EXTEND
, "_tbl.i");
233 svc_output (cmd
.infile
, "-DRPC_SERVER", EXTEND
, "_server.c");
235 clnt_output (cmd
.infile
, "-DRPC_CLIENT", EXTEND
, "_client.c");
237 if (allfiles
|| (cmd
.makefileflag
== 1))
240 mkfile_output (&cmd
);
244 return nonfatalerrors
;
248 * add extension to filename
251 extendfile (const char *file
, const char *ext
)
256 res
= alloc (strlen (file
) + strlen (ext
) + 1);
259 p
= strrchr (file
, '.');
261 p
= file
+ strlen (file
);
263 strcpy (res
+ (p
- file
), ext
);
268 * Open output file with given extension
271 open_output (const char *infile
, const char *outfile
)
279 if (infile
!= NULL
&& streq (outfile
, infile
))
281 fprintf (stderr
, _ ("%s: output would overwrite %s\n"), cmdname
,
285 fout
= fopen (outfile
, "w");
288 fprintf (stderr
, _ ("%s: unable to open %s: %m\n"), cmdname
, outfile
);
291 record_open (outfile
);
294 /* Close the output file and check for write errors. */
296 close_output (const char *outfile
)
298 if (fclose (fout
) == EOF
)
300 fprintf (stderr
, _("%s: while writing output %s: %m"), cmdname
,
301 outfile
?: "<stdout>");
309 fprintf (fout
, "/*\n");
310 fprintf (fout
, " * Please do not edit this file.\n");
311 fprintf (fout
, " * It was generated using rpcgen.\n");
312 fprintf (fout
, " */\n\n");
315 /* clear list of arguments */
320 for (i
= FIXEDARGS
; i
< ARGLISTLEN
; ++i
)
322 argcount
= FIXEDARGS
;
325 /* make sure that a CPP exists */
331 if (stat64 (CPP
, &buf
) == 0)
334 if (cppDefined
) /* user specified cpp but it does not exist */
336 fprintf (stderr
, _ ("cannot find C preprocessor: %s\n"), CPP
);
340 /* fall back to system CPP */
345 * Open input file with given define for C-preprocessor
348 open_input (const char *infile
, const char *define
)
352 infilename
= (infile
== NULL
) ? "<stdin>" : infile
;
364 putarg (1, CPPFLAGS
);
368 addarg ((char *) NULL
);
372 execvp (arglist
[0], (char **) arglist
);
375 fprintf (stderr
, _ ("cannot find C preprocessor: %s\n"), CPP
);
385 fin
= fdopen (pd
[0], "r");
388 fprintf (stderr
, "%s: ", cmdname
);
394 /* Close the connection to the C-preprocessor and check for successfull
402 /* Check the termination status. */
403 if (waitpid (cpp_pid
, &status
, 0) < 0)
408 if (WIFSIGNALED (status
) || WEXITSTATUS (status
) != 0)
410 if (WIFSIGNALED (status
))
411 fprintf (stderr
, _("%s: C preprocessor failed with signal %d\n"),
412 cmdname
, WTERMSIG (status
));
414 fprintf (stderr
, _("%s: C preprocessor failed with exit code %d\n"),
415 cmdname
, WEXITSTATUS (status
));
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
++)
449 if (strcmp (name
, list_to_check
[i
]) == 0)
454 fprintf (stderr
, _ ("illegal nettype: `%s'\n"), name
);
459 * Compile into an XDR routine output file
463 c_output (const char *infile
, const char *define
, int extend
,
468 const char *outfilename
;
472 open_input (infile
, define
);
473 outfilename
= extend
? extendfile (infile
, outfile
) : outfile
;
474 open_output (infile
, outfilename
);
476 if (infile
&& (include
= extendfile (infile
, ".h")))
478 fprintf (fout
, "#include \"%s\"\n", include
);
480 /* .h file already contains rpc/rpc.h */
483 fprintf (fout
, "#include <rpc/rpc.h>\n");
485 while ((def
= get_definition ()) != NULL
)
488 if (extend
&& tell
== ftell (fout
))
489 unlink (outfilename
);
491 close_output (outfilename
);
498 /* add all the starting basic types */
501 add_type (1, "long");
502 add_type (1, "short");
503 add_type (1, "bool");
505 add_type (1, "u_int");
506 add_type (1, "u_long");
507 add_type (1, "u_short");
511 char rpcgen_table_dcl
[] = "struct rpcgen_table {\n\
513 xdrproc_t xdr_arg;\n\
515 xdrproc_t xdr_res;\n\
521 generate_guard (const char *pathname
)
523 const char *filename
;
526 filename
= strrchr (pathname
, '/'); /* find last component */
527 filename
= ((filename
== NULL
) ? pathname
: filename
+ 1);
528 guard
= extendfile (filename
, "_H_RPCGEN");
529 /* convert to upper case */
534 *tmp
= toupper (*tmp
);
542 * Compile into an XDR header file
547 h_output (const char *infile
, const char *define
, int extend
,
552 const char *ifilename
;
553 const char *outfilename
;
558 open_input (infile
, define
);
559 outfilename
= extend
? extendfile (infile
, outfile
) : outfile
;
560 open_output (infile
, outfilename
);
562 ifilename
= (infile
== NULL
) ? "STDIN" : infile
;
563 guard
= generate_guard (outfilename
? outfilename
: ifilename
);
565 fprintf (fout
, "#ifndef _%s\n#define _%s\n\n", guard
,
568 fprintf (fout
, "#include <rpc/rpc.h>\n\n");
572 fprintf (fout
, "#include <pthread.h>\n");
575 /* put the C++ support */
576 if (Cflag
&& !CCflag
)
578 fprintf (fout
, "\n#ifdef __cplusplus\n");
579 fprintf (fout
, "extern \"C\" {\n");
580 fprintf (fout
, "#endif\n\n");
584 /* print data definitions */
585 while ((def
= get_definition ()) != NULL
)
590 /* print function declarations.
591 Do this after data definitions because they might be used as
592 arguments for functions */
593 for (l
= defined
; l
!= NULL
; l
= l
->next
)
595 print_funcdef (l
->val
);
597 /* Now print all xdr func declarations */
598 if (xdrfunc_head
!= NULL
)
600 fprintf (fout
, "\n/* the xdr functions */\n");
603 fprintf (fout
, "\n#ifdef __cplusplus\n");
604 fprintf (fout
, "extern \"C\" {\n");
605 fprintf (fout
, "#endif\n");
609 xdrfuncp
= xdrfunc_head
;
610 while (xdrfuncp
!= NULL
)
612 print_xdr_func_def (xdrfuncp
->name
,
613 xdrfuncp
->pointerp
, 2);
614 xdrfuncp
= xdrfuncp
->next
;
621 for (i
= 1; i
< 3; ++i
)
624 fprintf (fout
, "\n#if defined(__STDC__) || defined(__cplusplus)\n");
626 fprintf (fout
, "\n#else /* K&R C */\n");
628 xdrfuncp
= xdrfunc_head
;
629 while (xdrfuncp
!= NULL
)
631 print_xdr_func_def (xdrfuncp
->name
,
632 xdrfuncp
->pointerp
, i
);
633 xdrfuncp
= xdrfuncp
->next
;
636 fprintf (fout
, "\n#endif /* K&R C */\n");
640 if (extend
&& tell
== ftell (fout
))
642 unlink (outfilename
);
646 fprintf (fout
, "%s", rpcgen_table_dcl
);
651 fprintf (fout
, "\n#ifdef __cplusplus\n");
652 fprintf (fout
, "}\n");
653 fprintf (fout
, "#endif\n");
656 fprintf (fout
, "\n#endif /* !_%s */\n", guard
);
659 close_output (outfilename
);
663 * Compile into an RPC service
666 s_output (int argc
, const char *argv
[], const char *infile
, const char *define
,
667 int extend
, const char *outfile
, int nomain
, int netflag
)
671 int foundprogram
= 0;
672 const char *outfilename
;
674 open_input (infile
, define
);
675 outfilename
= extend
? extendfile (infile
, outfile
) : outfile
;
676 open_output (infile
, outfilename
);
678 if (infile
&& (include
= extendfile (infile
, ".h")))
680 fprintf (fout
, "#include \"%s\"\n", include
);
684 fprintf (fout
, "#include <rpc/rpc.h>\n");
686 fprintf (fout
, "#include <stdio.h>\n");
687 fprintf (fout
, "#include <stdlib.h>\n");
688 fprintf (fout
, "#include <rpc/pmap_clnt.h>\n");
690 fprintf (fout
, "#include <string.h>\n");
691 if (strcmp (svcclosetime
, "-1") == 0)
693 else if (strcmp (svcclosetime
, "0") == 0)
695 else if (inetdflag
|| pmflag
)
697 fprintf (fout
, "#include <signal.h>\n");
701 if (!tirpcflag
&& inetdflag
)
702 fprintf (fout
, "#include <sys/ioctl.h> /* ioctl, TIOCNOTTY */\n");
703 if (Cflag
&& (inetdflag
|| pmflag
))
705 fprintf (fout
, "#include <sys/types.h> /* open */\n");
706 fprintf (fout
, "#include <sys/stat.h> /* open */\n");
707 fprintf (fout
, "#include <fcntl.h> /* open */\n");
708 fprintf (fout
, "#include <unistd.h> /* getdtablesize */\n");
710 if (tirpcflag
&& !(Cflag
&& (inetdflag
|| pmflag
)))
711 fprintf (fout
, "#include <sys/types.h>\n");
713 fprintf (fout
, "#include <memory.h>\n");
714 if (inetdflag
|| !tirpcflag
)
716 fprintf (fout
, "#include <sys/socket.h>\n");
717 fprintf (fout
, "#include <netinet/in.h>\n");
720 if ((netflag
|| pmflag
) && tirpcflag
&& !nomain
)
722 fprintf (fout
, "#include <netconfig.h>\n");
724 if ( /*timerflag && */ tirpcflag
)
725 fprintf (fout
, "#include <sys/resource.h> /* rlimit */\n");
726 if (logflag
|| inetdflag
|| pmflag
)
728 fprintf (fout
, "#include <syslog.h>\n");
733 fprintf (fout
, "\n#ifndef SIG_PF\n#define SIG_PF void(*)(int)\n#endif\n");
736 fprintf (fout
, "\n#define _RPCSVC_CLOSEDOWN %s\n", svcclosetime
);
737 while ((def
= get_definition ()) != NULL
)
739 foundprogram
|= (def
->def_kind
== DEF_PROGRAM
);
741 if (extend
&& !foundprogram
)
743 unlink (outfilename
);
746 write_most (infile
, netflag
, nomain
);
749 if (!do_registers (argc
, argv
))
752 unlink (outfilename
);
758 close_output (outfilename
);
762 * generate client side stubs
765 l_output (const char *infile
, const char *define
, int extend
,
770 int foundprogram
= 0;
771 const char *outfilename
;
773 open_input (infile
, define
);
774 outfilename
= extend
? extendfile (infile
, outfile
) : outfile
;
775 open_output (infile
, outfilename
);
778 fprintf (fout
, "#include <memory.h> /* for memset */\n");
779 if (infile
&& (include
= extendfile (infile
, ".h")))
781 fprintf (fout
, "#include \"%s\"\n", include
);
785 fprintf (fout
, "#include <rpc/rpc.h>\n");
786 while ((def
= get_definition ()) != NULL
)
788 foundprogram
|= (def
->def_kind
== DEF_PROGRAM
);
790 if (extend
&& !foundprogram
)
792 unlink (outfilename
);
797 close_output (outfilename
);
801 * generate the dispatch table
804 t_output (const char *infile
, const char *define
, int extend
,
808 int foundprogram
= 0;
809 const char *outfilename
;
811 open_input (infile
, define
);
812 outfilename
= extend
? extendfile (infile
, outfile
) : outfile
;
813 open_output (infile
, outfilename
);
815 while ((def
= get_definition ()) != NULL
)
817 foundprogram
|= (def
->def_kind
== DEF_PROGRAM
);
819 if (extend
&& !foundprogram
)
821 unlink (outfilename
);
826 close_output (outfilename
);
829 /* sample routine for the server template */
831 svc_output (const char *infile
, const char *define
, int extend
,
836 const char *outfilename
;
839 open_input (infile
, define
);
840 outfilename
= extend
? extendfile (infile
, outfile
) : outfile
;
841 checkfiles (infile
, outfilename
);
842 /*check if outfile already exists.
843 if so, print an error message and exit */
844 open_output (infile
, outfilename
);
847 if (infile
&& (include
= extendfile (infile
, ".h")))
849 fprintf (fout
, "#include \"%s\"\n", include
);
853 fprintf (fout
, "#include <rpc/rpc.h>\n");
856 while ((def
= get_definition ()) != NULL
)
858 write_sample_svc (def
);
860 if (extend
&& tell
== ftell (fout
))
862 unlink (outfilename
);
865 close_output (outfilename
);
869 /* sample main routine for client */
871 clnt_output (const char *infile
, const char *define
, int extend
,
876 const char *outfilename
;
880 open_input (infile
, define
);
881 outfilename
= extend
? extendfile (infile
, outfile
) : outfile
;
882 checkfiles (infile
, outfilename
);
883 /*check if outfile already exists.
884 if so, print an error message and exit */
886 open_output (infile
, outfilename
);
888 if (infile
&& (include
= extendfile (infile
, ".h")))
890 fprintf (fout
, "#include \"%s\"\n", include
);
894 fprintf (fout
, "#include <rpc/rpc.h>\n");
896 while ((def
= get_definition ()) != NULL
)
898 has_program
+= write_sample_clnt (def
);
902 write_sample_clnt_main ();
904 if (extend
&& tell
== ftell (fout
))
906 unlink (outfilename
);
909 close_output (outfilename
);
912 static const char space
[] = " ";
915 file_name (const char *file
, const char *ext
)
918 temp
= extendfile (file
, ext
);
920 if (access (temp
, F_OK
) != -1)
924 return (char *) space
;
928 mkfile_output (struct commandline
*cmd
)
931 char *clientname
, *clntname
, *xdrname
, *hdrname
;
932 char *servername
, *svcname
, *servprogname
, *clntprogname
;
934 svcname
= file_name (cmd
->infile
, "_svc.c");
935 clntname
= file_name (cmd
->infile
, "_clnt.c");
936 xdrname
= file_name (cmd
->infile
, "_xdr.c");
937 hdrname
= file_name (cmd
->infile
, ".h");
941 servername
= extendfile (cmd
->infile
, "_server.c");
942 clientname
= extendfile (cmd
->infile
, "_client.c");
946 servername
= (char *) space
;
947 clientname
= (char *) space
;
949 servprogname
= extendfile (cmd
->infile
, "_server");
950 clntprogname
= extendfile (cmd
->infile
, "_client");
956 mkfilename
= alloc (strlen ("Makefile.") + strlen (cmd
->infile
) + 1);
957 if (mkfilename
== NULL
)
959 temp
= strrchr (cmd
->infile
, '.');
960 cp
= stpcpy (mkfilename
, "Makefile.");
962 *((char *) stpncpy (cp
, cmd
->infile
, temp
- cmd
->infile
)) = '\0';
964 stpcpy (cp
, cmd
->infile
);
968 mkfilename
= (char *) cmd
->outfile
;
970 checkfiles (NULL
, mkfilename
);
971 open_output (NULL
, mkfilename
);
973 fprintf (fout
, "\n# This is a template Makefile generated by rpcgen\n");
975 f_print (fout
, "\n# Parameters\n\n");
977 f_print (fout
, "CLIENT = %s\nSERVER = %s\n\n", clntprogname
, servprogname
);
978 f_print (fout
, "SOURCES_CLNT.c = \nSOURCES_CLNT.h = \n");
979 f_print (fout
, "SOURCES_SVC.c = \nSOURCES_SVC.h = \n");
980 f_print (fout
, "SOURCES.x = %s\n\n", cmd
->infile
);
981 f_print (fout
, "TARGETS_SVC.c = %s %s %s \n",
982 svcname
, servername
, xdrname
);
983 f_print (fout
, "TARGETS_CLNT.c = %s %s %s \n",
984 clntname
, clientname
, xdrname
);
985 f_print (fout
, "TARGETS = %s %s %s %s %s %s\n\n",
986 hdrname
, xdrname
, clntname
,
987 svcname
, clientname
, servername
);
989 f_print (fout
, "OBJECTS_CLNT = $(SOURCES_CLNT.c:%%.c=%%.o) \
990 $(TARGETS_CLNT.c:%%.c=%%.o)");
992 f_print (fout
, "\nOBJECTS_SVC = $(SOURCES_SVC.c:%%.c=%%.o) \
993 $(TARGETS_SVC.c:%%.c=%%.o)");
995 f_print (fout
, "\n# Compiler flags \n");
997 fprintf (fout
, "\nCPPFLAGS += -D_REENTRANT\nCFLAGS += -g \nLDLIBS \
998 += -lnsl -lpthread \n ");
1000 f_print (fout
, "\nCFLAGS += -g \nLDLIBS += -lnsl\n");
1001 f_print (fout
, "RPCGENFLAGS = \n");
1003 f_print (fout
, "\n# Targets \n\n");
1005 f_print (fout
, "all : $(CLIENT) $(SERVER)\n\n");
1006 f_print (fout
, "$(TARGETS) : $(SOURCES.x) \n");
1007 f_print (fout
, "\trpcgen $(RPCGENFLAGS) $(SOURCES.x)\n\n");
1008 f_print (fout
, "$(OBJECTS_CLNT) : $(SOURCES_CLNT.c) $(SOURCES_CLNT.h) \
1009 $(TARGETS_CLNT.c) \n\n");
1011 f_print (fout
, "$(OBJECTS_SVC) : $(SOURCES_SVC.c) $(SOURCES_SVC.h) \
1012 $(TARGETS_SVC.c) \n\n");
1013 f_print (fout
, "$(CLIENT) : $(OBJECTS_CLNT) \n");
1014 f_print (fout
, "\t$(LINK.c) -o $(CLIENT) $(OBJECTS_CLNT) \
1016 f_print (fout
, "$(SERVER) : $(OBJECTS_SVC) \n");
1017 f_print (fout
, "\t$(LINK.c) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS)\n\n ");
1018 f_print (fout
, "clean:\n\t $(RM) core $(TARGETS) $(OBJECTS_CLNT) \
1019 $(OBJECTS_SVC) $(CLIENT) $(SERVER)\n\n");
1020 close_output (mkfilename
);
1022 free (clntprogname
);
1023 free (servprogname
);
1024 if (servername
!= space
)
1026 if (clientname
!= space
)
1028 if (mkfilename
!= (char *) cmd
->outfile
)
1030 if (svcname
!= space
)
1032 if (clntname
!= space
)
1034 if (xdrname
!= space
)
1036 if (hdrname
!= space
)
1041 * Perform registrations for service output
1042 * Return 0 if failed; 1 otherwise.
1045 do_registers (int argc
, const char *argv
[])
1049 if (inetdflag
|| !tirpcflag
)
1051 for (i
= 1; i
< argc
; i
++)
1053 if (streq (argv
[i
], "-s"))
1055 if (!check_nettype (argv
[i
+ 1], valid_i_nettypes
))
1057 write_inetd_register (argv
[i
+ 1]);
1064 for (i
= 1; i
< argc
; i
++)
1065 if (streq (argv
[i
], "-s"))
1067 if (!check_nettype (argv
[i
+ 1], valid_ti_nettypes
))
1069 write_nettype_register (argv
[i
+ 1]);
1072 else if (streq (argv
[i
], "-n"))
1074 write_netid_register (argv
[i
+ 1]);
1082 * Add another argument to the arg list
1085 addarg (const char *cp
)
1087 if (argcount
>= ARGLISTLEN
)
1089 fprintf (stderr
, _("rpcgen: too many defines\n"));
1093 arglist
[argcount
++] = cp
;
1097 putarg (int whereto
, const char *cp
)
1099 if (whereto
>= ARGLISTLEN
)
1101 fprintf (stderr
, _("rpcgen: arglist coding error\n"));
1105 arglist
[whereto
] = cp
;
1109 * if input file is stdin and an output file is specified then complain
1110 * if the file already exists. Otherwise the file may get overwritten
1111 * If input file does not exist, exit with an error
1115 checkfiles (const char *infile
, const char *outfile
)
1119 if (infile
) /* infile ! = NULL */
1120 if (stat64 (infile
, &buf
) < 0)
1127 if (stat64 (outfile
, &buf
) < 0)
1128 return; /* file does not exist */
1132 /* TRANS: the file will not be removed; this is an
1133 TRANS: informative message. */
1134 _("file `%s' already exists and may be overwritten\n"),
1142 * Parse command line arguments
1145 parseargs (int argc
, const char *argv
[], struct commandline
*cmd
)
1150 char flag
[(1 << 8 * sizeof (char))];
1154 cmd
->infile
= cmd
->outfile
= NULL
;
1172 for (i
= 1; i
< argc
; i
++)
1174 if (argv
[i
][0] != '-')
1179 _("Cannot specify more than one input file!\n"));
1182 cmd
->infile
= argv
[i
];
1184 else if (strcmp (argv
[i
], "--help") == 0)
1186 else if (strcmp (argv
[i
], "--version") == 0)
1190 for (j
= 1; argv
[i
][j
] != 0; j
++)
1208 /* sample flag: Ss or Sc.
1209 Ss means set flag['S'];
1210 Sc means set flag['C'];
1211 Sm means set flag['M']; */
1212 c
= argv
[i
][++j
]; /* get next char */
1226 case 'C': /* ANSI C syntax */
1230 case 'k': /* K&R C syntax */
1234 case 'b': /* turn TIRPC flag off for
1235 generating backward compatible
1240 case '5': /* turn TIRPC flag on for
1241 generating SysVr4 compatible
1259 svcclosetime
= argv
[i
];
1272 inlineflag
= atoi (argv
[i
]);
1277 if (argv
[i
][j
- 1] != '-' ||
1278 argv
[i
][j
+ 1] != 0)
1289 if (!streq (argv
[i
], "udp") &&
1290 !streq (argv
[i
], "tcp"))
1297 cmd
->outfile
= argv
[i
];
1301 if (argv
[i
][j
- 1] != '-')
1309 size_t len
= strlen (argv
[i
]);
1310 pathbuf
= malloc (len
+ 5);
1311 if (pathbuf
== NULL
)
1316 stpcpy (stpcpy (pathbuf
,
1333 cmd
->cflag
= flag
['c'];
1334 cmd
->hflag
= flag
['h'];
1335 cmd
->lflag
= flag
['l'];
1336 cmd
->mflag
= flag
['m'];
1337 cmd
->nflag
= flag
['n'];
1338 cmd
->sflag
= flag
['s'];
1339 cmd
->tflag
= flag
['t'];
1340 cmd
->Ssflag
= flag
['S'];
1341 cmd
->Scflag
= flag
['C'];
1342 cmd
->makefileflag
= flag
['M'];
1346 pmflag
= inetdflag
? 0 : 1; /* pmflag or inetdflag is always TRUE */
1347 if ((inetdflag
&& cmd
->nflag
))
1348 { /* netid not allowed with inetdflag */
1349 fprintf (stderr
, _("Cannot use netid flag with inetd flag!\n"));
1355 pmflag
= 0; /* set pmflag only in tirpcmode */
1357 { /* netid needs TIRPC */
1358 f_print (stderr
, _("Cannot use netid flag without TIRPC!\n"));
1363 if (newstyle
&& (tblflag
|| cmd
->tflag
))
1365 f_print (stderr
, _("Cannot use table flags with newstyle!\n"));
1369 /* check no conflicts with file generation flags */
1370 nflags
= cmd
->cflag
+ cmd
->hflag
+ cmd
->lflag
+ cmd
->mflag
+
1371 cmd
->sflag
+ cmd
->nflag
+ cmd
->tflag
+ cmd
->Ssflag
+ cmd
->Scflag
;
1375 if (cmd
->outfile
!= NULL
|| cmd
->infile
== NULL
)
1380 else if (cmd
->infile
== NULL
&&
1381 (cmd
->Ssflag
|| cmd
->Scflag
|| cmd
->makefileflag
))
1384 _("\"infile\" is required for template generation flags.\n"));
1389 fprintf (stderr
, _("Cannot have more than one file generation flag!\n"));
1396 usage (FILE *stream
, int status
)
1398 fprintf (stream
, _("usage: %s infile\n"), cmdname
);
1399 fprintf (stream
, _("\t%s [-abkCLNTM][-Dname[=value]] [-i size] \
1400 [-I [-K seconds]] [-Y path] infile\n"), cmdname
);
1401 fprintf (stream
, _("\t%s [-c | -h | -l | -m | -t | -Sc | -Ss | -Sm] \
1402 [-o outfile] [infile]\n"), cmdname
);
1403 fprintf (stream
, _("\t%s [-s nettype]* [-o outfile] [infile]\n"), cmdname
);
1404 fprintf (stream
, _("\t%s [-n netid]* [-o outfile] [infile]\n"), cmdname
);
1405 options_usage (stream
, status
);
1410 options_usage (FILE *stream
, int status
)
1412 f_print (stream
, _("options:\n"));
1413 f_print (stream
, _("-a\t\tgenerate all files, including samples\n"));
1414 f_print (stream
, _("-b\t\tbackward compatibility mode (generates code for SunOS 4.1)\n"));
1415 f_print (stream
, _("-c\t\tgenerate XDR routines\n"));
1416 f_print (stream
, _("-C\t\tANSI C mode\n"));
1417 f_print (stream
, _("-Dname[=value]\tdefine a symbol (same as #define)\n"));
1418 f_print (stream
, _("-h\t\tgenerate header file\n"));
1419 f_print (stream
, _("-i size\t\tsize at which to start generating inline code\n"));
1420 f_print (stream
, _("-I\t\tgenerate code for inetd support in server (for SunOS 4.1)\n"));
1421 f_print (stream
, _("-K seconds\tserver exits after K seconds of inactivity\n"));
1422 f_print (stream
, _("-l\t\tgenerate client side stubs\n"));
1423 f_print (stream
, _("-L\t\tserver errors will be printed to syslog\n"));
1424 f_print (stream
, _("-m\t\tgenerate server side stubs\n"));
1425 f_print (stream
, _("-M\t\tgenerate MT-safe code\n"));
1426 f_print (stream
, _("-n netid\tgenerate server code that supports named netid\n"));
1427 f_print (stream
, _("-N\t\tsupports multiple arguments and call-by-value\n"));
1428 f_print (stream
, _("-o outfile\tname of the output file\n"));
1429 f_print (stream
, _("-s nettype\tgenerate server code that supports named nettype\n"));
1430 f_print (stream
, _("-Sc\t\tgenerate sample client code that uses remote procedures\n"));
1431 f_print (stream
, _("-Ss\t\tgenerate sample server code that defines remote procedures\n"));
1432 f_print (stream
, _("-Sm \t\tgenerate makefile template \n"));
1433 f_print (stream
, _("-t\t\tgenerate RPC dispatch table\n"));
1434 f_print (stream
, _("-T\t\tgenerate code to support RPC dispatch tables\n"));
1435 f_print (stream
, _("-Y path\t\tdirectory name to find C preprocessor (cpp)\n"));
1436 f_print (stream
, _("-5\t\tSysVr4 compatibility mode\n"));
1437 f_print (stream
, _("--help\t\tgive this help list\n"));
1438 f_print (stream
, _("--version\tprint program version\n"));
1440 f_print (stream
, _("\n\
1441 For bug reporting instructions, please see:\n\
1442 %s.\n"), REPORT_BUGS_TO
);
1447 print_version (void)
1449 printf ("rpcgen %s%s\n", PKGVERSION
, VERSION
);