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 or with the express written consent of
8 * Sun Microsystems, Inc.
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
26 * Sun Microsystems, Inc.
28 * Mountain View, California 94043
32 * From @(#)rpc_main.c 1.30 89/03/30 (C) 1987 SMI;
34 const char main_rcsid
[] =
38 * rpc_main.c, Top level of the RPC protocol compiler.
47 #include <sys/types.h>
48 #include <sys/param.h>
52 #include "rpc_parse.h"
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 #define SVR4_CPP "/usr/ccs/lib/cpp"
80 #define SUNOS_CPP "/lib/cpp"
82 static const char *svcclosetime
= "120";
83 static int cppDefined
; /* explicit path for C preprocessor */
84 static const char *CPP
= SUNOS_CPP
;
85 static const char CPPFLAGS
[] = "-C";
88 static const char *allv
[] =
90 "rpcgen", "-s", "udp", "-s", "tcp"
92 static int allc
= sizeof (allv
) / sizeof (allv
[0]);
93 static const char *allnv
[] =
95 "rpcgen", "-s", "netpath",
97 static int allnc
= sizeof (allnv
) / sizeof (allnv
[0]);
100 * machinations for handling expanding argument list
102 static void addarg (const char *); /* add another argument to the list */
103 static void putarg (int, const char *); /* put argument at specified location */
104 static void clear_args (void); /* clear argument list */
105 static void checkfiles (const char *, const char *);
106 /* check if out file already exists */
108 static void clear_args (void);
109 static char *extendfile (const char *file
, const char *ext
);
110 static void open_output (const char *infile
, const char *outfile
);
111 static void add_warning (void);
112 static void clear_args (void);
113 static void find_cpp (void);
114 static void open_input (const char *infile
, const char *define
);
115 static int check_nettype (const char *name
, const char *list_to_check
[]);
116 static void c_output (const char *infile
, const char *define
,
117 int extend
, const char *outfile
);
118 static void h_output (const char *infile
, const char *define
,
119 int extend
, const char *outfile
);
120 static void s_output (int argc
, const char *argv
[], const char *infile
,
121 const char *define
, int extend
,
122 const char *outfile
, int nomain
, int netflag
);
123 static void l_output (const char *infile
, const char *define
,
124 int extend
, const char *outfile
);
125 static void t_output (const char *infile
, const char *define
,
126 int extend
, const char *outfile
);
127 static void svc_output (const char *infile
, const char *define
,
128 int extend
, const char *outfile
);
129 static void clnt_output (const char *infile
, const char *define
,
130 int extend
, const char *outfile
);
131 static void mkfile_output (struct commandline
*cmd
);
132 static int do_registers (int argc
, const char *argv
[]);
133 static void addarg (const char *cp
);
134 static void putarg (int whereto
, const char *cp
);
135 static void checkfiles (const char *infile
, const char *outfile
);
136 static int parseargs (int argc
, const char *argv
[], struct commandline
*cmd
);
137 static void usage (void) __attribute__ ((noreturn
));
138 static void options_usage (void) __attribute__ ((noreturn
));
139 static void c_initialize (void);
140 static char *generate_guard (const char *pathname
);
143 #define ARGLISTLEN 20
146 static const char *arglist
[ARGLISTLEN
];
147 static int argcount
= FIXEDARGS
;
150 int nonfatalerrors
; /* errors */
151 int inetdflag
/* = 1 */ ; /* Support for inetd *//* is now the default */
152 int pmflag
; /* Support for port monitors */
153 int logflag
; /* Use syslog instead of fprintf for errors */
154 int tblflag
; /* Support for dispatch table file */
155 int mtflag
; /* Support for MT */
158 /*length at which to start doing an inline */
160 int inlineflag
= INLINE
; /* length at which to start doing an inline. 3 = default
161 if 0, no xdr_inline code */
163 int indefinitewait
; /* If started by port monitors, hang till it wants */
164 int exitnow
; /* If started by port monitors, exit after the call */
165 int timerflag
; /* TRUE if !indefinite && !exitnow */
166 int newstyle
; /* newstyle of passing arguments (by value) */
167 #ifdef __GNU_LIBRARY__
168 int Cflag
= 1; /* ANSI C syntax */
170 int Cflag
; /* ANSI C/C++ syntax */
172 int CCflag
; /* C++ files */
173 static int allfiles
; /* generate all files */
174 #ifdef __GNU_LIBRARY__
175 int tirpcflag
; /* generating code for tirpc, by default */
177 int tirpcflag
= 1; /* generating code for tirpc, by default */
179 xdrfunc
*xdrfunc_head
; /* xdr function list */
180 xdrfunc
*xdrfunc_tail
; /* xdr function list */
183 main (int argc
, const char *argv
[])
185 struct commandline cmd
;
187 (void) memset ((char *) &cmd
, 0, sizeof (struct commandline
));
189 if (!parseargs (argc
, argv
, &cmd
))
192 if (cmd
.cflag
|| cmd
.hflag
|| cmd
.lflag
|| cmd
.tflag
|| cmd
.sflag
||
193 cmd
.mflag
|| cmd
.nflag
|| cmd
.Ssflag
|| cmd
.Scflag
)
195 checkfiles (cmd
.infile
, cmd
.outfile
);
198 checkfiles (cmd
.infile
, NULL
);
201 c_output (cmd
.infile
, "-DRPC_XDR", DONT_EXTEND
, cmd
.outfile
);
203 h_output (cmd
.infile
, "-DRPC_HDR", DONT_EXTEND
, cmd
.outfile
);
205 l_output (cmd
.infile
, "-DRPC_CLNT", DONT_EXTEND
, cmd
.outfile
);
206 else if (cmd
.sflag
|| cmd
.mflag
|| (cmd
.nflag
))
207 s_output (argc
, argv
, cmd
.infile
, "-DRPC_SVC", DONT_EXTEND
,
208 cmd
.outfile
, cmd
.mflag
, cmd
.nflag
);
210 t_output (cmd
.infile
, "-DRPC_TBL", DONT_EXTEND
, cmd
.outfile
);
212 svc_output (cmd
.infile
, "-DRPC_SERVER", DONT_EXTEND
, cmd
.outfile
);
214 clnt_output (cmd
.infile
, "-DRPC_CLIENT", DONT_EXTEND
, cmd
.outfile
);
215 else if (cmd
.makefileflag
)
216 mkfile_output (&cmd
);
219 /* the rescans are required, since cpp may effect input */
220 c_output (cmd
.infile
, "-DRPC_XDR", EXTEND
, "_xdr.c");
222 h_output (cmd
.infile
, "-DRPC_HDR", EXTEND
, ".h");
224 l_output (cmd
.infile
, "-DRPC_CLNT", EXTEND
, "_clnt.c");
226 if (inetdflag
|| !tirpcflag
)
227 s_output (allc
, allv
, cmd
.infile
, "-DRPC_SVC", EXTEND
,
228 "_svc.c", cmd
.mflag
, cmd
.nflag
);
230 s_output (allnc
, allnv
, cmd
.infile
, "-DRPC_SVC",
231 EXTEND
, "_svc.c", cmd
.mflag
, cmd
.nflag
);
235 t_output (cmd
.infile
, "-DRPC_TBL", EXTEND
, "_tbl.i");
240 svc_output (cmd
.infile
, "-DRPC_SERVER", EXTEND
, "_server.c");
242 clnt_output (cmd
.infile
, "-DRPC_CLIENT", EXTEND
, "_client.c");
244 if (allfiles
|| (cmd
.makefileflag
== 1))
247 mkfile_output (&cmd
);
251 return nonfatalerrors
;
255 * add extension to filename
258 extendfile (const char *file
, const char *ext
)
263 res
= alloc (strlen (file
) + strlen (ext
) + 1);
266 p
= strrchr (file
, '.');
268 p
= file
+ strlen (file
);
270 strcpy (res
+ (p
- file
), ext
);
275 * Open output file with given extension
278 open_output (const char *infile
, const char *outfile
)
286 if (infile
!= NULL
&& streq (outfile
, infile
))
288 fprintf (stderr
, _ ("%s: output would overwrite %s\n"), cmdname
,
292 fout
= fopen (outfile
, "w");
295 fprintf (stderr
, _ ("%s: unable to open %s: %m\n"), cmdname
, outfile
);
298 record_open (outfile
);
301 /* Close the output file and check for write errors. */
303 close_output (const char *outfile
)
305 if (fclose (fout
) == EOF
)
307 fprintf (stderr
, _("%s: while writing output %s: %m"), cmdname
,
308 outfile
?: "<stdout>");
316 fprintf (fout
, "/*\n");
317 fprintf (fout
, " * Please do not edit this file.\n");
318 fprintf (fout
, " * It was generated using rpcgen.\n");
319 fprintf (fout
, " */\n\n");
322 /* clear list of arguments */
327 for (i
= FIXEDARGS
; i
< ARGLISTLEN
; ++i
)
329 argcount
= FIXEDARGS
;
332 /* make sure that a CPP exists */
338 if (stat (CPP
, &buf
) < 0)
339 { /* /lib/cpp or explicit cpp does not exist */
342 fprintf (stderr
, _ ("cannot find C preprocessor: %s \n"), CPP
);
346 { /* try the other one */
348 if (stat (CPP
, &buf
) < 0)
349 { /* can't find any cpp */
350 fputs (_ ("cannot find any C preprocessor (cpp)\n"), stdout
);
358 * Open input file with given define for C-preprocessor
361 open_input (const char *infile
, const char *define
)
365 infilename
= (infile
== NULL
) ? "<stdin>" : infile
;
377 putarg (1, CPPFLAGS
);
381 addarg ((char *) NULL
);
385 execv (arglist
[0], (char **) arglist
);
393 fin
= fdopen (pd
[0], "r");
396 fprintf (stderr
, "%s: ", cmdname
);
402 /* Close the connection to the C-preprocessor and check for successfull
410 /* Check the termination status. */
411 if (waitpid (cpp_pid
, &status
, 0) < 0)
416 if (WIFSIGNALED (status
) || WEXITSTATUS (status
) != 0)
418 if (WIFSIGNALED (status
))
419 fprintf (stderr
, _("%s: C preprocessor failed with signal %d\n"),
420 cmdname
, WTERMSIG (status
));
422 fprintf (stderr
, _("%s: C preprocessor failed with exit code %d\n"),
423 cmdname
, WEXITSTATUS (status
));
428 /* valid tirpc nettypes */
429 static const char *valid_ti_nettypes
[] =
443 /* valid inetd nettypes */
444 static const char *valid_i_nettypes
[] =
452 check_nettype (const char *name
, const char *list_to_check
[])
455 for (i
= 0; list_to_check
[i
] != NULL
; i
++)
457 if (strcmp (name
, list_to_check
[i
]) == 0)
462 fprintf (stderr
, _ ("illegal nettype :`%s'\n"), name
);
467 * Compile into an XDR routine output file
471 c_output (const char *infile
, const char *define
, int extend
,
476 const char *outfilename
;
480 open_input (infile
, define
);
481 outfilename
= extend
? extendfile (infile
, outfile
) : outfile
;
482 open_output (infile
, outfilename
);
484 if (infile
&& (include
= extendfile (infile
, ".h")))
486 fprintf (fout
, "#include \"%s\"\n", include
);
488 /* .h file already contains rpc/rpc.h */
491 fprintf (fout
, "#include <rpc/rpc.h>\n");
493 while ((def
= get_definition ()) != NULL
)
496 if (extend
&& tell
== ftell (fout
))
497 unlink (outfilename
);
499 close_output (outfilename
);
506 /* add all the starting basic types */
509 add_type (1, "long");
510 add_type (1, "short");
511 add_type (1, "bool");
513 add_type (1, "u_int");
514 add_type (1, "u_long");
515 add_type (1, "u_short");
519 char rpcgen_table_dcl
[] = "struct rpcgen_table {\n\
521 xdrproc_t xdr_arg;\n\
523 xdrproc_t xdr_res;\n\
529 generate_guard (const char *pathname
)
531 const char *filename
;
534 filename
= strrchr (pathname
, '/'); /* find last component */
535 filename
= ((filename
== NULL
) ? pathname
: filename
+ 1);
536 guard
= strdup (filename
);
537 /* convert to upper case */
542 *tmp
= toupper (*tmp
);
546 guard
= extendfile (guard
, "_H_RPCGEN");
551 * Compile into an XDR header file
556 h_output (const char *infile
, const char *define
, int extend
,
561 const char *ifilename
;
562 const char *outfilename
;
567 open_input (infile
, define
);
568 outfilename
= extend
? extendfile (infile
, outfile
) : outfile
;
569 open_output (infile
, outfilename
);
571 ifilename
= (infile
== NULL
) ? "STDIN" : infile
;
572 guard
= generate_guard (outfilename
? outfilename
: ifilename
);
574 fprintf (fout
, "#ifndef _%s\n#define _%s\n\n", guard
,
577 fprintf (fout
, "#include <rpc/rpc.h>\n\n");
581 fprintf (fout
, "#include <pthread.h>\n");
584 /* put the C++ support */
585 if (Cflag
&& !CCflag
)
587 fprintf (fout
, "\n#ifdef __cplusplus\n");
588 fprintf (fout
, "extern \"C\" {\n");
589 fprintf (fout
, "#endif\n\n");
593 /* print data definitions */
594 while ((def
= get_definition ()) != NULL
)
599 /* print function declarations.
600 Do this after data definitions because they might be used as
601 arguments for functions */
602 for (l
= defined
; l
!= NULL
; l
= l
->next
)
604 print_funcdef (l
->val
);
606 /* Now print all xdr func declarations */
607 if (xdrfunc_head
!= NULL
)
609 fprintf (fout
, "\n/* the xdr functions */\n");
612 fprintf (fout
, "\n#ifdef __cplusplus\n");
613 fprintf (fout
, "extern \"C\" {\n");
614 fprintf (fout
, "#endif\n");
618 xdrfuncp
= xdrfunc_head
;
619 while (xdrfuncp
!= NULL
)
621 print_xdr_func_def (xdrfuncp
->name
,
622 xdrfuncp
->pointerp
, 2);
623 xdrfuncp
= xdrfuncp
->next
;
630 for (i
= 1; i
< 3; ++i
)
633 fprintf (fout
, "\n#if defined(__STDC__) || defined(__cplusplus)\n");
635 fprintf (fout
, "\n#else /* K&R C */\n");
637 xdrfuncp
= xdrfunc_head
;
638 while (xdrfuncp
!= NULL
)
640 print_xdr_func_def (xdrfuncp
->name
,
641 xdrfuncp
->pointerp
, i
);
642 xdrfuncp
= xdrfuncp
->next
;
645 fprintf (fout
, "\n#endif /* K&R C */\n");
649 if (extend
&& tell
== ftell (fout
))
651 unlink (outfilename
);
655 fprintf (fout
, rpcgen_table_dcl
);
660 fprintf (fout
, "\n#ifdef __cplusplus\n");
661 fprintf (fout
, "}\n");
662 fprintf (fout
, "#endif\n");
665 fprintf (fout
, "\n#endif /* !_%s */\n", guard
);
667 close_output (outfilename
);
671 * Compile into an RPC service
674 s_output (int argc
, const char *argv
[], const char *infile
, const char *define
,
675 int extend
, const char *outfile
, int nomain
, int netflag
)
679 int foundprogram
= 0;
680 const char *outfilename
;
682 open_input (infile
, define
);
683 outfilename
= extend
? extendfile (infile
, outfile
) : outfile
;
684 open_output (infile
, outfilename
);
686 if (infile
&& (include
= extendfile (infile
, ".h")))
688 fprintf (fout
, "#include \"%s\"\n", include
);
692 fprintf (fout
, "#include <rpc/rpc.h>\n");
694 fprintf (fout
, "#include <stdio.h>\n");
695 fprintf (fout
, "#include <stdlib.h>\n");
698 fprintf (fout
, "#include <rpc/pmap_clnt.h>\n");
699 fprintf (fout
, "#include <string.h>\n");
701 if (strcmp (svcclosetime
, "-1") == 0)
703 else if (strcmp (svcclosetime
, "0") == 0)
705 else if (inetdflag
|| pmflag
)
707 fprintf (fout
, "#include <signal.h>\n");
711 if (!tirpcflag
&& inetdflag
)
712 #ifdef __GNU_LIBRARY__
713 fprintf (fout
, "#include <sys/ioctl.h> /* ioctl, TIOCNOTTY */\n");
715 fprintf (fout
, "#include <sys/ttycom.h>/* TIOCNOTTY */\n");
717 if (Cflag
&& (inetdflag
|| pmflag
))
719 #ifdef __GNU_LIBRARY__
720 fprintf (fout
, "#include <sys/types.h> /* open */\n");
721 fprintf (fout
, "#include <sys/stat.h> /* open */\n");
722 fprintf (fout
, "#include <fcntl.h> /* open */\n");
723 fprintf (fout
, "#include <unistd.h> /* getdtablesize */\n");
725 fprintf (fout
, "#ifdef __cplusplus\n");
726 fprintf (fout
, "#include <sysent.h> /* getdtablesize, open */\n");
727 fprintf (fout
, "#endif /* __cplusplus */\n");
729 fprintf (fout
, "#include <unistd.h> /* setsid */\n");
732 #ifdef __GNU_LIBRARY__
733 if (tirpcflag
&& !(Cflag
&& (inetdflag
|| pmflag
)))
737 fprintf (fout
, "#include <sys/types.h>\n");
739 fprintf (fout
, "#include <memory.h>\n");
740 #ifndef __GNU_LIBRARY__
741 fprintf (fout
, "#include <stropts.h>\n");
743 if (inetdflag
|| !tirpcflag
)
745 fprintf (fout
, "#include <sys/socket.h>\n");
746 fprintf (fout
, "#include <netinet/in.h>\n");
749 if ((netflag
|| pmflag
) && tirpcflag
&& !nomain
)
751 fprintf (fout
, "#include <netconfig.h>\n");
753 if ( /*timerflag && */ tirpcflag
)
754 fprintf (fout
, "#include <sys/resource.h> /* rlimit */\n");
755 if (logflag
|| inetdflag
|| pmflag
)
757 #ifdef __GNU_LIBRARY__
758 fprintf (fout
, "#include <syslog.h>\n");
760 fprintf (fout
, "#ifdef SYSLOG\n");
761 fprintf (fout
, "#include <syslog.h>\n");
762 fprintf (fout
, "#else\n");
763 fprintf (fout
, "#define LOG_ERR 1\n");
764 fprintf (fout
, "#define openlog(a, b, c)\n");
765 fprintf (fout
, "#endif\n");
771 fprintf (fout
, "\n#ifndef SIG_PF\n#define SIG_PF void(*)(int)\n#endif\n");
773 #ifndef __GNU_LIBRARY__
774 fprintf (fout
, "\n#ifdef DEBUG\n#define RPC_SVC_FG\n#endif\n");
777 fprintf (fout
, "\n#define _RPCSVC_CLOSEDOWN %s\n", svcclosetime
);
778 while ((def
= get_definition ()) != NULL
)
780 foundprogram
|= (def
->def_kind
== DEF_PROGRAM
);
782 if (extend
&& !foundprogram
)
784 unlink (outfilename
);
787 write_most (infile
, netflag
, nomain
);
790 if (!do_registers (argc
, argv
))
793 unlink (outfilename
);
799 close_output (outfilename
);
803 * generate client side stubs
806 l_output (const char *infile
, const char *define
, int extend
,
811 int foundprogram
= 0;
812 const char *outfilename
;
814 open_input (infile
, define
);
815 outfilename
= extend
? extendfile (infile
, outfile
) : outfile
;
816 open_output (infile
, outfilename
);
819 fprintf (fout
, "#include <memory.h> /* for memset */\n");
820 if (infile
&& (include
= extendfile (infile
, ".h")))
822 fprintf (fout
, "#include \"%s\"\n", include
);
826 fprintf (fout
, "#include <rpc/rpc.h>\n");
827 while ((def
= get_definition ()) != NULL
)
829 foundprogram
|= (def
->def_kind
== DEF_PROGRAM
);
831 if (extend
&& !foundprogram
)
833 unlink (outfilename
);
838 close_output (outfilename
);
842 * generate the dispatch table
845 t_output (const char *infile
, const char *define
, int extend
,
849 int foundprogram
= 0;
850 const char *outfilename
;
852 open_input (infile
, define
);
853 outfilename
= extend
? extendfile (infile
, outfile
) : outfile
;
854 open_output (infile
, outfilename
);
856 while ((def
= get_definition ()) != NULL
)
858 foundprogram
|= (def
->def_kind
== DEF_PROGRAM
);
860 if (extend
&& !foundprogram
)
862 unlink (outfilename
);
867 close_output (outfilename
);
870 /* sample routine for the server template */
872 svc_output (const char *infile
, const char *define
, int extend
,
877 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 */
885 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");
897 while ((def
= get_definition ()) != NULL
)
899 write_sample_svc (def
);
901 if (extend
&& tell
== ftell (fout
))
903 unlink (outfilename
);
906 close_output (outfilename
);
910 /* sample main routine for client */
912 clnt_output (const char *infile
, const char *define
, int extend
,
917 const char *outfilename
;
921 open_input (infile
, define
);
922 outfilename
= extend
? extendfile (infile
, outfile
) : outfile
;
923 checkfiles (infile
, outfilename
);
924 /*check if outfile already exists.
925 if so, print an error message and exit */
927 open_output (infile
, outfilename
);
929 if (infile
&& (include
= extendfile (infile
, ".h")))
931 fprintf (fout
, "#include \"%s\"\n", include
);
935 fprintf (fout
, "#include <rpc/rpc.h>\n");
937 while ((def
= get_definition ()) != NULL
)
939 has_program
+= write_sample_clnt (def
);
943 write_sample_clnt_main ();
945 if (extend
&& tell
== ftell (fout
))
947 unlink (outfilename
);
950 close_output (outfilename
);
954 file_name (const char *file
, const char *ext
)
957 temp
= extendfile (file
, ext
);
959 if (access (temp
, F_OK
) != -1)
962 return ((char *) " ");
966 mkfile_output (struct commandline
*cmd
)
969 const char *clientname
, *clntname
, *xdrname
, *hdrname
;
970 const char *servername
, *svcname
, *servprogname
, *clntprogname
;
972 svcname
= file_name (cmd
->infile
, "_svc.c");
973 clntname
= file_name (cmd
->infile
, "_clnt.c");
974 xdrname
= file_name (cmd
->infile
, "_xdr.c");
975 hdrname
= file_name (cmd
->infile
, ".h");
979 servername
= extendfile (cmd
->infile
, "_server.c");
980 clientname
= extendfile (cmd
->infile
, "_client.c");
987 servprogname
= extendfile (cmd
->infile
, "_server");
988 clntprogname
= extendfile (cmd
->infile
, "_client");
994 mkfilename
= alloc (strlen ("Makefile.") + strlen (cmd
->infile
) + 1);
995 temp
= rindex (cmd
->infile
, '.');
996 cp
= stpcpy (mkfilename
, "Makefile.");
997 strncpy (cp
, cmd
->infile
, (temp
- cmd
->infile
));
1000 mkfilename
= (char *) cmd
->outfile
;
1002 checkfiles (NULL
, mkfilename
);
1003 open_output (NULL
, mkfilename
);
1005 fprintf (fout
, "\n# This is a template Makefile generated by rpcgen\n");
1007 f_print (fout
, "\n# Parameters\n\n");
1009 f_print (fout
, "CLIENT = %s\nSERVER = %s\n\n", clntprogname
, servprogname
);
1010 f_print (fout
, "SOURCES_CLNT.c = \nSOURCES_CLNT.h = \n");
1011 f_print (fout
, "SOURCES_SVC.c = \nSOURCES_SVC.h = \n");
1012 f_print (fout
, "SOURCES.x = %s\n\n", cmd
->infile
);
1013 f_print (fout
, "TARGETS_SVC.c = %s %s %s \n",
1014 svcname
, servername
, xdrname
);
1015 f_print (fout
, "TARGETS_CLNT.c = %s %s %s \n",
1016 clntname
, clientname
, xdrname
);
1017 f_print (fout
, "TARGETS = %s %s %s %s %s %s\n\n",
1018 hdrname
, xdrname
, clntname
,
1019 svcname
, clientname
, servername
);
1021 f_print (fout
, "OBJECTS_CLNT = $(SOURCES_CLNT.c:%%.c=%%.o) \
1022 $(TARGETS_CLNT.c:%%.c=%%.o)");
1024 f_print (fout
, "\nOBJECTS_SVC = $(SOURCES_SVC.c:%%.c=%%.o) \
1025 $(TARGETS_SVC.c:%%.c=%%.o)");
1027 f_print (fout
, "\n# Compiler flags \n");
1029 fprintf (fout
, "\nCPPFLAGS += -D_REENTRANT\nCFLAGS += -g \nLDLIBS \
1030 += -lnsl -lpthread \n ");
1032 f_print (fout
, "\nCFLAGS += -g \nLDLIBS += -lnsl\n");
1033 f_print (fout
, "RPCGENFLAGS = \n");
1035 f_print (fout
, "\n# Targets \n\n");
1037 f_print (fout
, "all : $(CLIENT) $(SERVER)\n\n");
1038 f_print (fout
, "$(TARGETS) : $(SOURCES.x) \n");
1039 f_print (fout
, "\trpcgen $(RPCGENFLAGS) $(SOURCES.x)\n\n");
1040 f_print (fout
, "$(OBJECTS_CLNT) : $(SOURCES_CLNT.c) $(SOURCES_CLNT.h) \
1041 $(TARGETS_CLNT.c) \n\n");
1043 f_print (fout
, "$(OBJECTS_SVC) : $(SOURCES_SVC.c) $(SOURCES_SVC.h) \
1044 $(TARGETS_SVC.c) \n\n");
1045 f_print (fout
, "$(CLIENT) : $(OBJECTS_CLNT) \n");
1046 f_print (fout
, "\t$(LINK.c) -o $(CLIENT) $(OBJECTS_CLNT) \
1048 f_print (fout
, "$(SERVER) : $(OBJECTS_SVC) \n");
1049 f_print (fout
, "\t$(LINK.c) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS)\n\n ");
1050 f_print (fout
, "clean:\n\t $(RM) core $(TARGETS) $(OBJECTS_CLNT) \
1051 $(OBJECTS_SVC) $(CLIENT) $(SERVER)\n\n");
1052 close_output (mkfilename
);
1056 * Perform registrations for service output
1057 * Return 0 if failed; 1 otherwise.
1060 do_registers (int argc
, const char *argv
[])
1064 if (inetdflag
|| !tirpcflag
)
1066 for (i
= 1; i
< argc
; i
++)
1068 if (streq (argv
[i
], "-s"))
1070 if (!check_nettype (argv
[i
+ 1], valid_i_nettypes
))
1072 write_inetd_register (argv
[i
+ 1]);
1079 for (i
= 1; i
< argc
; i
++)
1080 if (streq (argv
[i
], "-s"))
1082 if (!check_nettype (argv
[i
+ 1], valid_ti_nettypes
))
1084 write_nettype_register (argv
[i
+ 1]);
1087 else if (streq (argv
[i
], "-n"))
1089 write_netid_register (argv
[i
+ 1]);
1097 * Add another argument to the arg list
1100 addarg (const char *cp
)
1102 if (argcount
>= ARGLISTLEN
)
1104 fprintf (stderr
, _("rpcgen: too many defines\n"));
1108 arglist
[argcount
++] = cp
;
1112 putarg (int whereto
, const char *cp
)
1114 if (whereto
>= ARGLISTLEN
)
1116 fprintf (stderr
, _("rpcgen: arglist coding error\n"));
1120 arglist
[whereto
] = cp
;
1124 * if input file is stdin and an output file is specified then complain
1125 * if the file already exists. Otherwise the file may get overwritten
1126 * If input file does not exist, exit with an error
1130 checkfiles (const char *infile
, const char *outfile
)
1134 if (infile
) /* infile ! = NULL */
1135 if (stat (infile
, &buf
) < 0)
1142 if (stat (outfile
, &buf
) < 0)
1143 return; /* file does not exist */
1147 /* TRANS: the file will not be removed; this is an
1148 TRANS: informative message. */
1149 _("file `%s' already exists and may be overwritten\n"),
1157 * Parse command line arguments
1160 parseargs (int argc
, const char *argv
[], struct commandline
*cmd
)
1165 char flag
[(1 << 8 * sizeof (char))];
1169 cmd
->infile
= cmd
->outfile
= NULL
;
1187 for (i
= 1; i
< argc
; i
++)
1189 if (argv
[i
][0] != '-')
1194 _("Cannot specify more than one input file!\n"));
1197 cmd
->infile
= argv
[i
];
1201 for (j
= 1; argv
[i
][j
] != 0; j
++)
1219 /* sample flag: Ss or Sc.
1220 Ss means set flag['S'];
1221 Sc means set flag['C'];
1222 Sm means set flag['M']; */
1223 c
= argv
[i
][++j
]; /* get next char */
1237 case 'C': /* ANSI C syntax */
1241 #ifdef __GNU_LIBRARY__
1242 case 'k': /* K&R C syntax */
1247 case 'b': /* turn TIRPC flag off for
1248 generating backward compatible
1253 #ifdef __GNU_LIBRARY__
1254 case '5': /* turn TIRPC flag on for
1255 generating SysVr4 compatible
1274 svcclosetime
= argv
[i
];
1287 inlineflag
= atoi (argv
[i
]);
1292 if (argv
[i
][j
- 1] != '-' ||
1293 argv
[i
][j
+ 1] != 0)
1304 if (!streq (argv
[i
], "udp") &&
1305 !streq (argv
[i
], "tcp"))
1312 cmd
->outfile
= argv
[i
];
1316 if (argv
[i
][j
- 1] != '-')
1324 size_t len
= strlen (argv
[i
]);
1325 pathbuf
= malloc (len
+ 5);
1326 if (pathbuf
== NULL
)
1331 stpcpy (stpcpy (pathbuf
,
1348 cmd
->cflag
= flag
['c'];
1349 cmd
->hflag
= flag
['h'];
1350 cmd
->lflag
= flag
['l'];
1351 cmd
->mflag
= flag
['m'];
1352 cmd
->nflag
= flag
['n'];
1353 cmd
->sflag
= flag
['s'];
1354 cmd
->tflag
= flag
['t'];
1355 cmd
->Ssflag
= flag
['S'];
1356 cmd
->Scflag
= flag
['C'];
1357 cmd
->makefileflag
= flag
['M'];
1359 #ifndef _RPC_THREAD_SAFE_
1360 if (mtflag
|| newstyle
)
1362 /* glibc doesn't support these flags. */
1364 _("This implementation doesn't support newstyle or MT-safe code!\n"));
1370 pmflag
= inetdflag
? 0 : 1; /* pmflag or inetdflag is always TRUE */
1371 if ((inetdflag
&& cmd
->nflag
))
1372 { /* netid not allowed with inetdflag */
1373 fprintf (stderr
, _("Cannot use netid flag with inetd flag!\n"));
1379 pmflag
= 0; /* set pmflag only in tirpcmode */
1380 #ifndef __GNU_LIBRARY__
1381 inetdflag
= 1; /* inetdflag is TRUE by default */
1384 { /* netid needs TIRPC */
1385 f_print (stderr
, _("Cannot use netid flag without TIRPC!\n"));
1390 if (newstyle
&& (tblflag
|| cmd
->tflag
))
1392 f_print (stderr
, _("Cannot use table flags with newstyle!\n"));
1396 /* check no conflicts with file generation flags */
1397 nflags
= cmd
->cflag
+ cmd
->hflag
+ cmd
->lflag
+ cmd
->mflag
+
1398 cmd
->sflag
+ cmd
->nflag
+ cmd
->tflag
+ cmd
->Ssflag
+ cmd
->Scflag
;
1402 if (cmd
->outfile
!= NULL
|| cmd
->infile
== NULL
)
1407 else if (cmd
->infile
== NULL
&&
1408 (cmd
->Ssflag
|| cmd
->Scflag
|| cmd
->makefileflag
))
1411 _("\"infile\" is required for template generation flags.\n"));
1416 fprintf (stderr
, _("Cannot have more than one file generation flag!\n"));
1425 fprintf (stderr
, _("usage: %s infile\n"), cmdname
);
1426 fprintf (stderr
, _("\t%s [-abkCLNTM][-Dname[=value]] [-i size] \
1427 [-I [-K seconds]] [-Y path] infile\n"), cmdname
);
1428 fprintf (stderr
, _("\t%s [-c | -h | -l | -m | -t | -Sc | -Ss | -Sm] \
1429 [-o outfile] [infile]\n"), cmdname
);
1430 fprintf (stderr
, _("\t%s [-s nettype]* [-o outfile] [infile]\n"), cmdname
);
1431 fprintf (stderr
, _("\t%s [-n netid]* [-o outfile] [infile]\n"), cmdname
);
1437 options_usage (void)
1439 f_print (stderr
, "options:\n");
1440 f_print (stderr
, "-a\t\tgenerate all files, including samples\n");
1441 f_print (stderr
, "-b\t\tbackward compatibility mode (generates code for SunOS 4.1)\n");
1442 f_print (stderr
, "-c\t\tgenerate XDR routines\n");
1443 f_print (stderr
, "-C\t\tANSI C mode\n");
1444 f_print (stderr
, "-Dname[=value]\tdefine a symbol (same as #define)\n");
1445 f_print (stderr
, "-h\t\tgenerate header file\n");
1446 f_print (stderr
, "-i size\t\tsize at which to start generating inline code\n");
1447 f_print (stderr
, "-I\t\tgenerate code for inetd support in server (for SunOS 4.1)\n");
1448 f_print (stderr
, "-K seconds\tserver exits after K seconds of inactivity\n");
1449 f_print (stderr
, "-l\t\tgenerate client side stubs\n");
1450 f_print (stderr
, "-L\t\tserver errors will be printed to syslog\n");
1451 f_print (stderr
, "-m\t\tgenerate server side stubs\n");
1452 f_print (stderr
, "-M\t\tgenerate MT-safe code\n");
1453 f_print (stderr
, "-n netid\tgenerate server code that supports named netid\n");
1454 f_print (stderr
, "-N\t\tsupports multiple arguments and call-by-value\n");
1455 f_print (stderr
, "-o outfile\tname of the output file\n");
1456 f_print (stderr
, "-s nettype\tgenerate server code that supports named nettype\n");
1457 f_print (stderr
, "-Sc\t\tgenerate sample client code that uses remote procedures\n");
1458 f_print (stderr
, "-Ss\t\tgenerate sample server code that defines remote procedures\n");
1459 f_print (stderr
, "-Sm \t\tgenerate makefile template \n");
1460 f_print (stderr
, "-t\t\tgenerate RPC dispatch table\n");
1461 f_print (stderr
, "-T\t\tgenerate code to support RPC dispatch tables\n");
1462 f_print (stderr
, "-Y path\t\tdirectory name to find C preprocessor (cpp)\n");