Update.
[glibc.git] / sunrpc / rpc_main.c
blobba25455be402581766382a7899b31d0f77ac7094
1 /*
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.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
32 * From @(#)rpc_main.c 1.30 89/03/30 (C) 1987 SMI;
34 const char main_rcsid[] =
35 "$Id$";
38 * rpc_main.c, Top level of the RPC protocol compiler.
41 #include <errno.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <libintl.h>
46 #include <ctype.h>
47 #include <sys/types.h>
48 #include <sys/param.h>
49 #include <sys/file.h>
50 #include <sys/stat.h>
51 #include <sys/wait.h>
52 #include "rpc_parse.h"
53 #include "rpc_util.h"
54 #include "rpc_scan.h"
55 #include "proto.h"
57 #define EXTEND 1 /* alias for TRUE */
58 #define DONT_EXTEND 0 /* alias for FALSE */
60 struct commandline
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 = 0; /* explicit path for C preprocessor */
84 static const char *CPP = SUNOS_CPP;
85 static char CPPFLAGS[] = "-C";
86 static char *pathbuf;
87 static int cpp_pid;
88 static const char *allv[] =
90 "rpcgen", "-s", "udp", "-s", "tcp", "-s", "udp6", "-s", "tcp6"
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);
138 static void options_usage (void);
139 static void c_initialize (void);
140 static char *generate_guard (const char *pathname);
143 #define ARGLISTLEN 20
144 #define FIXEDARGS 2
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 */
157 #define INLINE 3
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 */
169 #else
170 int Cflag = 0; /* ANSI C/C++ syntax */
171 #endif
172 int CCflag = 0; /* C++ files */
173 static int allfiles; /* generate all files */
174 #ifdef __GNU_LIBRARY__
175 int tirpcflag = 0; /* generating code for tirpc, by default */
176 #else
177 int tirpcflag = 1; /* generating code for tirpc, by default */
178 #endif
179 xdrfunc *xdrfunc_head = NULL; /* xdr function list */
180 xdrfunc *xdrfunc_tail = NULL; /* xdr function list */
183 main (int argc, const char *argv[])
185 struct commandline cmd;
187 (void) memset ((char *) &cmd, 0, sizeof (struct commandline));
188 clear_args ();
189 if (!parseargs (argc, argv, &cmd))
190 usage ();
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);
197 else
198 checkfiles (cmd.infile, NULL);
200 if (cmd.cflag)
201 c_output (cmd.infile, "-DRPC_XDR", DONT_EXTEND, cmd.outfile);
202 else if (cmd.hflag)
203 h_output (cmd.infile, "-DRPC_HDR", DONT_EXTEND, cmd.outfile);
204 else if (cmd.lflag)
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);
209 else if (cmd.tflag)
210 t_output (cmd.infile, "-DRPC_TBL", DONT_EXTEND, cmd.outfile);
211 else if (cmd.Ssflag)
212 svc_output (cmd.infile, "-DRPC_SERVER", DONT_EXTEND, cmd.outfile);
213 else if (cmd.Scflag)
214 clnt_output (cmd.infile, "-DRPC_CLIENT", DONT_EXTEND, cmd.outfile);
215 else if (cmd.makefileflag)
216 mkfile_output (&cmd);
217 else
219 /* the rescans are required, since cpp may effect input */
220 c_output (cmd.infile, "-DRPC_XDR", EXTEND, "_xdr.c");
221 reinitialize ();
222 h_output (cmd.infile, "-DRPC_HDR", EXTEND, ".h");
223 reinitialize ();
224 l_output (cmd.infile, "-DRPC_CLNT", EXTEND, "_clnt.c");
225 reinitialize ();
226 if (inetdflag || !tirpcflag)
227 s_output (allc, allv, cmd.infile, "-DRPC_SVC", EXTEND,
228 "_svc.c", cmd.mflag, cmd.nflag);
229 else
230 s_output (allnc, allnv, cmd.infile, "-DRPC_SVC",
231 EXTEND, "_svc.c", cmd.mflag, cmd.nflag);
232 if (tblflag)
234 reinitialize ();
235 t_output (cmd.infile, "-DRPC_TBL", EXTEND, "_tbl.i");
237 if (allfiles)
239 reinitialize ();
240 svc_output (cmd.infile, "-DRPC_SERVER", EXTEND, "_server.c");
241 reinitialize ();
242 clnt_output (cmd.infile, "-DRPC_CLIENT", EXTEND, "_client.c");
244 if (allfiles || (cmd.makefileflag == 1))
246 reinitialize ();
247 mkfile_output (&cmd);
250 exit (nonfatalerrors);
251 /* NOTREACHED */
255 * add extension to filename
257 static char *
258 extendfile (const char *file, const char *ext)
260 char *res;
261 const char *p;
263 res = alloc (strlen (file) + strlen (ext) + 1);
264 if (res == NULL)
265 abort ();
266 p = strrchr (file, '.');
267 if (p == NULL)
268 p = file + strlen (file);
269 strcpy (res, file);
270 strcpy (res + (p - file), ext);
271 return res;
275 * Open output file with given extension
277 static void
278 open_output (const char *infile, const char *outfile)
280 if (outfile == NULL)
282 fout = stdout;
283 return;
286 if (infile != NULL && streq (outfile, infile))
288 fprintf (stderr, _ ("%s: output would overwrite %s\n"), cmdname,
289 infile);
290 crash ();
292 fout = fopen (outfile, "w");
293 if (fout == NULL)
295 fprintf (stderr, _ ("%s: unable to open %s: %m\n"), cmdname, outfile);
296 crash ();
298 record_open (outfile);
301 /* Close the output file and check for write errors. */
302 static void
303 close_output (const char *outfile)
305 if (fclose (fout) == EOF)
307 fprintf (stderr, _("%s: while writing output %s: %m"), cmdname,
308 outfile ?: "<stdout>");
309 crash ();
313 static void
314 add_warning (void)
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 */
323 static void
324 clear_args (void)
326 int i;
327 for (i = FIXEDARGS; i < ARGLISTLEN; ++i)
328 arglist[i] = NULL;
329 argcount = FIXEDARGS;
332 /* make sure that a CPP exists */
333 static void
334 find_cpp (void)
336 struct stat buf;
338 if (stat (CPP, &buf) < 0)
339 { /* /lib/cpp or explicit cpp does not exist */
340 if (cppDefined)
342 fprintf (stderr, _ ("cannot find C preprocessor: %s \n"), CPP);
343 crash ();
345 else
346 { /* try the other one */
347 CPP = SVR4_CPP;
348 if (stat (CPP, &buf) < 0)
349 { /* can't find any cpp */
350 fputs (_ ("cannot find any C preprocessor (cpp)\n"), stdout);
351 crash ();
358 * Open input file with given define for C-preprocessor
360 static void
361 open_input (const char *infile, const char *define)
363 int pd[2];
365 infilename = (infile == NULL) ? "<stdin>" : infile;
366 if (pipe (pd) != 0)
368 perror ("pipe");
369 exit (1);
371 cpp_pid = fork ();
372 switch (cpp_pid)
374 case 0:
375 find_cpp ();
376 putarg (0, CPP);
377 putarg (1, CPPFLAGS);
378 addarg (define);
379 if (infile)
380 addarg (infile);
381 addarg ((char *) NULL);
382 close (1);
383 dup2 (pd[1], 1);
384 close (pd[0]);
385 execv (arglist[0], (char **) arglist);
386 perror ("execv");
387 exit (1);
388 case -1:
389 perror ("fork");
390 exit (1);
392 close (pd[1]);
393 fin = fdopen (pd[0], "r");
394 if (fin == NULL)
396 fprintf (stderr, "%s: ", cmdname);
397 perror (infilename);
398 crash ();
402 /* Close the connection to the C-preprocessor and check for successfull
403 termination. */
404 static void
405 close_input (void)
407 int status;
409 fclose (fin);
410 /* Check the termination status. */
411 if (waitpid (cpp_pid, &status, 0) < 0)
413 perror ("waitpid");
414 crash ();
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));
421 else
422 fprintf (stderr, _("%s: C preprocessor failed with exit code %d\n"),
423 cmdname, WEXITSTATUS (status));
424 crash ();
428 /* valid tirpc nettypes */
429 static const char *valid_ti_nettypes[] =
431 "netpath",
432 "visible",
433 "circuit_v",
434 "datagram_v",
435 "circuit_n",
436 "datagram_n",
437 "udp",
438 "tcp",
439 "raw",
440 "udp6",
441 "tcp6",
442 NULL
445 /* valid inetd nettypes */
446 static const char *valid_i_nettypes[] =
448 "udp",
449 "tcp",
450 "udp6",
451 "tcp6",
452 NULL
455 static int
456 check_nettype (const char *name, const char *list_to_check[])
458 int i;
459 for (i = 0; list_to_check[i] != NULL; i++)
461 if (strcmp (name, list_to_check[i]) == 0)
463 return 1;
466 fprintf (stderr, _ ("illegal nettype :`%s'\n"), name);
467 return 0;
471 * Compile into an XDR routine output file
474 static void
475 c_output (const char *infile, const char *define, int extend,
476 const char *outfile)
478 definition *def;
479 char *include;
480 const char *outfilename;
481 long tell;
483 c_initialize ();
484 open_input (infile, define);
485 outfilename = extend ? extendfile (infile, outfile) : outfile;
486 open_output (infile, outfilename);
487 add_warning ();
488 if (infile && (include = extendfile (infile, ".h")))
490 fprintf (fout, "#include \"%s\"\n", include);
491 free (include);
492 /* .h file already contains rpc/rpc.h */
494 else
495 fprintf (fout, "#include <rpc/rpc.h>\n");
496 tell = ftell (fout);
497 while ((def = get_definition ()) != NULL)
498 emit (def);
500 if (extend && tell == ftell (fout))
501 unlink (outfilename);
502 close_input ();
503 close_output (outfilename);
506 void
507 c_initialize (void)
510 /* add all the starting basic types */
512 add_type (1, "int");
513 add_type (1, "long");
514 add_type (1, "short");
515 add_type (1, "bool");
517 add_type (1, "u_int");
518 add_type (1, "u_long");
519 add_type (1, "u_short");
523 char rpcgen_table_dcl[] = "struct rpcgen_table {\n\
524 char *(*proc)();\n\
525 xdrproc_t xdr_arg;\n\
526 unsigned len_arg;\n\
527 xdrproc_t xdr_res;\n\
528 unsigned len_res;\n\
529 };\n";
532 static char *
533 generate_guard (const char *pathname)
535 const char *filename;
536 char *guard, *tmp;
538 filename = strrchr (pathname, '/'); /* find last component */
539 filename = ((filename == NULL) ? pathname : filename + 1);
540 guard = strdup (filename);
541 /* convert to upper case */
542 tmp = guard;
543 while (*tmp)
545 if (islower (*tmp))
546 *tmp = toupper (*tmp);
547 tmp++;
550 guard = extendfile (guard, "_H_RPCGEN");
551 return guard;
555 * Compile into an XDR header file
559 static void
560 h_output (const char *infile, const char *define, int extend,
561 const char *outfile)
563 xdrfunc *xdrfuncp;
564 definition *def;
565 const char *ifilename;
566 const char *outfilename;
567 long tell;
568 char *guard;
569 list *l;
571 open_input (infile, define);
572 outfilename = extend ? extendfile (infile, outfile) : outfile;
573 open_output (infile, outfilename);
574 add_warning ();
575 ifilename = (infile == NULL) ? "STDIN" : infile;
576 guard = generate_guard (outfilename ? outfilename : ifilename);
578 fprintf (fout, "#ifndef _%s\n#define _%s\n\n", guard,
579 guard);
581 fprintf (fout, "#include <rpc/rpc.h>\n\n");
583 if (mtflag)
585 fprintf (fout, "#include <pthread.h>\n");
588 /* put the C++ support */
589 if (Cflag && !CCflag)
591 fprintf (fout, "\n#ifdef __cplusplus\n");
592 fprintf (fout, "extern \"C\" {\n");
593 fprintf (fout, "#endif\n\n");
596 tell = ftell (fout);
597 /* print data definitions */
598 while ((def = get_definition ()) != NULL)
600 print_datadef (def);
603 /* print function declarations.
604 Do this after data definitions because they might be used as
605 arguments for functions */
606 for (l = defined; l != NULL; l = l->next)
608 print_funcdef (l->val);
610 /* Now print all xdr func declarations */
611 if (xdrfunc_head != NULL)
613 fprintf (fout, "\n/* the xdr functions */\n");
614 if (CCflag)
616 fprintf (fout, "\n#ifdef __cplusplus\n");
617 fprintf (fout, "extern \"C\" {\n");
618 fprintf (fout, "#endif\n");
620 if (!Cflag)
622 xdrfuncp = xdrfunc_head;
623 while (xdrfuncp != NULL)
625 print_xdr_func_def (xdrfuncp->name,
626 xdrfuncp->pointerp, 2);
627 xdrfuncp = xdrfuncp->next;
630 else
632 int i;
634 for (i = 1; i < 3; ++i)
636 if (i == 1)
637 fprintf (fout, "\n#if defined(__STDC__) || defined(__cplusplus)\n");
638 else
639 fprintf (fout, "\n#else /* K&R C */\n");
641 xdrfuncp = xdrfunc_head;
642 while (xdrfuncp != NULL)
644 print_xdr_func_def (xdrfuncp->name,
645 xdrfuncp->pointerp, i);
646 xdrfuncp = xdrfuncp->next;
649 fprintf (fout, "\n#endif /* K&R C */\n");
653 if (extend && tell == ftell (fout))
655 unlink (outfilename);
657 else if (tblflag)
659 fprintf (fout, rpcgen_table_dcl);
662 if (Cflag)
664 fprintf (fout, "\n#ifdef __cplusplus\n");
665 fprintf (fout, "}\n");
666 fprintf (fout, "#endif\n");
669 fprintf (fout, "\n#endif /* !_%s */\n", guard);
670 close_input ();
671 close_output (outfilename);
675 * Compile into an RPC service
677 static void
678 s_output (int argc, const char *argv[], const char *infile, const char *define,
679 int extend, const char *outfile, int nomain, int netflag)
681 char *include;
682 definition *def;
683 int foundprogram = 0;
684 const char *outfilename;
686 open_input (infile, define);
687 outfilename = extend ? extendfile (infile, outfile) : outfile;
688 open_output (infile, outfilename);
689 add_warning ();
690 if (infile && (include = extendfile (infile, ".h")))
692 fprintf (fout, "#include \"%s\"\n", include);
693 free (include);
695 else
696 fprintf (fout, "#include <rpc/rpc.h>\n");
698 fprintf (fout, "#include <stdio.h>\n");
699 fprintf (fout, "#include <stdlib.h>\n");
700 if (Cflag)
702 fprintf (fout, "#include <rpc/pmap_clnt.h>\n");
703 fprintf (fout, "#include <string.h>\n");
705 if (strcmp (svcclosetime, "-1") == 0)
706 indefinitewait = 1;
707 else if (strcmp (svcclosetime, "0") == 0)
708 exitnow = 1;
709 else if (inetdflag || pmflag)
711 fprintf (fout, "#include <signal.h>\n");
712 timerflag = 1;
715 if (!tirpcflag && inetdflag)
716 #ifdef __GNU_LIBRARY__
717 fprintf (fout, "#include <sys/ioctl.h> /* ioctl, TIOCNOTTY */\n");
718 #else
719 fprintf (fout, "#include <sys/ttycom.h>/* TIOCNOTTY */\n");
720 #endif
721 if (Cflag && (inetdflag || pmflag))
723 #ifdef __GNU_LIBRARY__
724 fprintf (fout, "#include <sys/types.h> /* open */\n");
725 fprintf (fout, "#include <sys/stat.h> /* open */\n");
726 fprintf (fout, "#include <fcntl.h> /* open */\n");
727 fprintf (fout, "#include <unistd.h> /* getdtablesize */\n");
728 #else
729 fprintf (fout, "#ifdef __cplusplus\n");
730 fprintf (fout, "#include <sysent.h> /* getdtablesize, open */\n");
731 fprintf (fout, "#endif /* __cplusplus */\n");
732 if (tirpcflag)
733 fprintf (fout, "#include <unistd.h> /* setsid */\n");
734 #endif
736 #ifdef __GNU_LIBRARY__
737 if (tirpcflag && !(Cflag && (inetdflag || pmflag)))
738 #else
739 if (tirpcflag)
740 #endif
741 fprintf (fout, "#include <sys/types.h>\n");
743 fprintf (fout, "#include <memory.h>\n");
744 #ifndef __GNU_LIBRARY__
745 fprintf (fout, "#include <stropts.h>\n");
746 #endif
747 if (inetdflag || !tirpcflag)
749 fprintf (fout, "#include <sys/socket.h>\n");
750 fprintf (fout, "#include <netinet/in.h>\n");
753 if ((netflag || pmflag) && tirpcflag && !nomain)
755 fprintf (fout, "#include <netconfig.h>\n");
757 if ( /*timerflag && */ tirpcflag)
758 fprintf (fout, "#include <sys/resource.h> /* rlimit */\n");
759 if (logflag || inetdflag || pmflag)
761 #ifdef __GNU_LIBRARY__
762 fprintf (fout, "#include <syslog.h>\n");
763 #else
764 fprintf (fout, "#ifdef SYSLOG\n");
765 fprintf (fout, "#include <syslog.h>\n");
766 fprintf (fout, "#else\n");
767 fprintf (fout, "#define LOG_ERR 1\n");
768 fprintf (fout, "#define openlog(a, b, c)\n");
769 fprintf (fout, "#endif\n");
770 #endif
773 /* for ANSI-C */
774 if (Cflag)
775 fprintf (fout, "\n#ifndef SIG_PF\n#define SIG_PF void(*)(int)\n#endif\n");
777 #ifndef __GNU_LIBRARY__
778 fprintf (fout, "\n#ifdef DEBUG\n#define RPC_SVC_FG\n#endif\n");
779 #endif
780 if (timerflag)
781 fprintf (fout, "\n#define _RPCSVC_CLOSEDOWN %s\n", svcclosetime);
782 while ((def = get_definition ()) != NULL)
784 foundprogram |= (def->def_kind == DEF_PROGRAM);
786 if (extend && !foundprogram)
788 unlink (outfilename);
789 return;
791 write_most (infile, netflag, nomain);
792 if (!nomain)
794 if (!do_registers (argc, argv))
796 if (outfilename)
797 unlink (outfilename);
798 usage ();
800 write_rest ();
802 close_input ();
803 close_output (outfilename);
807 * generate client side stubs
809 static void
810 l_output (const char *infile, const char *define, int extend,
811 const char *outfile)
813 char *include;
814 definition *def;
815 int foundprogram = 0;
816 const char *outfilename;
818 open_input (infile, define);
819 outfilename = extend ? extendfile (infile, outfile) : outfile;
820 open_output (infile, outfilename);
821 add_warning ();
822 if (Cflag)
823 fprintf (fout, "#include <memory.h> /* for memset */\n");
824 if (infile && (include = extendfile (infile, ".h")))
826 fprintf (fout, "#include \"%s\"\n", include);
827 free (include);
829 else
830 fprintf (fout, "#include <rpc/rpc.h>\n");
831 while ((def = get_definition ()) != NULL)
833 foundprogram |= (def->def_kind == DEF_PROGRAM);
835 if (extend && !foundprogram)
837 unlink (outfilename);
838 return;
840 write_stubs ();
841 close_input ();
842 close_output (outfilename);
846 * generate the dispatch table
848 static void
849 t_output (const char *infile, const char *define, int extend,
850 const char *outfile)
852 definition *def;
853 int foundprogram = 0;
854 const char *outfilename;
856 open_input (infile, define);
857 outfilename = extend ? extendfile (infile, outfile) : outfile;
858 open_output (infile, outfilename);
859 add_warning ();
860 while ((def = get_definition ()) != NULL)
862 foundprogram |= (def->def_kind == DEF_PROGRAM);
864 if (extend && !foundprogram)
866 unlink (outfilename);
867 return;
869 write_tables ();
870 close_input ();
871 close_output (outfilename);
874 /* sample routine for the server template */
875 static void
876 svc_output (const char *infile, const char *define, int extend,
877 const char *outfile)
879 definition *def;
880 char *include;
881 const char *outfilename;
882 long tell;
884 open_input (infile, define);
885 outfilename = extend ? extendfile (infile, outfile) : outfile;
886 checkfiles (infile, outfilename);
887 /*check if outfile already exists.
888 if so, print an error message and exit */
889 open_output (infile, outfilename);
890 add_sample_msg ();
892 if (infile && (include = extendfile (infile, ".h")))
894 fprintf (fout, "#include \"%s\"\n", include);
895 free (include);
897 else
898 fprintf (fout, "#include <rpc/rpc.h>\n");
900 tell = ftell (fout);
901 while ((def = get_definition ()) != NULL)
903 write_sample_svc (def);
905 if (extend && tell == ftell (fout))
907 unlink (outfilename);
909 close_input ();
910 close_output (outfilename);
914 /* sample main routine for client */
915 static void
916 clnt_output (const char *infile, const char *define, int extend,
917 const char *outfile)
919 definition *def;
920 char *include;
921 const char *outfilename;
922 long tell;
923 int has_program = 0;
925 open_input (infile, define);
926 outfilename = extend ? extendfile (infile, outfile) : outfile;
927 checkfiles (infile, outfilename);
928 /*check if outfile already exists.
929 if so, print an error message and exit */
931 open_output (infile, outfilename);
932 add_sample_msg ();
933 if (infile && (include = extendfile (infile, ".h")))
935 fprintf (fout, "#include \"%s\"\n", include);
936 free (include);
938 else
939 fprintf (fout, "#include <rpc/rpc.h>\n");
940 tell = ftell (fout);
941 while ((def = get_definition ()) != NULL)
943 has_program += write_sample_clnt (def);
946 if (has_program)
947 write_sample_clnt_main ();
949 if (extend && tell == ftell (fout))
951 unlink (outfilename);
953 close_input ();
954 close_output (outfilename);
957 static char *
958 file_name (const char *file, const char *ext)
960 char *temp;
961 temp = extendfile (file, ext);
963 if (access (temp, F_OK) != -1)
964 return (temp);
965 else
966 return ((char *) " ");
969 static void
970 mkfile_output (struct commandline *cmd)
972 char *mkfilename;
973 const char *clientname, *clntname, *xdrname, *hdrname;
974 const char *servername, *svcname, *servprogname, *clntprogname;
976 svcname = file_name (cmd->infile, "_svc.c");
977 clntname = file_name (cmd->infile, "_clnt.c");
978 xdrname = file_name (cmd->infile, "_xdr.c");
979 hdrname = file_name (cmd->infile, ".h");
981 if (allfiles)
983 servername = extendfile (cmd->infile, "_server.c");
984 clientname = extendfile (cmd->infile, "_client.c");
986 else
988 servername = " ";
989 clientname = " ";
991 servprogname = extendfile (cmd->infile, "_server");
992 clntprogname = extendfile (cmd->infile, "_client");
994 if (allfiles)
996 char *cp, *temp;
998 mkfilename = alloc (strlen ("Makefile.") + strlen (cmd->infile) + 1);
999 temp = rindex (cmd->infile, '.');
1000 cp = stpcpy (mkfilename, "Makefile.");
1001 strncpy (cp, cmd->infile, (temp - cmd->infile));
1003 else
1004 mkfilename = (char *) cmd->outfile;
1006 checkfiles (NULL, mkfilename);
1007 open_output (NULL, mkfilename);
1009 fprintf (fout, "\n# This is a template Makefile generated by rpcgen\n");
1011 f_print (fout, "\n# Parameters\n\n");
1013 f_print (fout, "CLIENT = %s\nSERVER = %s\n\n", clntprogname, servprogname);
1014 f_print (fout, "SOURCES_CLNT.c = \nSOURCES_CLNT.h = \n");
1015 f_print (fout, "SOURCES_SVC.c = \nSOURCES_SVC.h = \n");
1016 f_print (fout, "SOURCES.x = %s\n\n", cmd->infile);
1017 f_print (fout, "TARGETS_SVC.c = %s %s %s \n",
1018 svcname, servername, xdrname);
1019 f_print (fout, "TARGETS_CLNT.c = %s %s %s \n",
1020 clntname, clientname, xdrname);
1021 f_print (fout, "TARGETS = %s %s %s %s %s %s\n\n",
1022 hdrname, xdrname, clntname,
1023 svcname, clientname, servername);
1025 f_print (fout, "OBJECTS_CLNT = $(SOURCES_CLNT.c:%%.c=%%.o) \
1026 $(TARGETS_CLNT.c:%%.c=%%.o)");
1028 f_print (fout, "\nOBJECTS_SVC = $(SOURCES_SVC.c:%%.c=%%.o) \
1029 $(TARGETS_SVC.c:%%.c=%%.o)");
1031 f_print (fout, "\n# Compiler flags \n");
1032 if (mtflag)
1033 fprintf (fout, "\nCPPFLAGS += -D_REENTRANT\nCFLAGS += -g \nLDLIBS \
1034 += -lnsl -lpthread \n ");
1035 else
1036 f_print (fout, "\nCFLAGS += -g \nLDLIBS += -lnsl\n");
1037 f_print (fout, "RPCGENFLAGS = \n");
1039 f_print (fout, "\n# Targets \n\n");
1041 f_print (fout, "all : $(CLIENT) $(SERVER)\n\n");
1042 f_print (fout, "$(TARGETS) : $(SOURCES.x) \n");
1043 f_print (fout, "\trpcgen $(RPCGENFLAGS) $(SOURCES.x)\n\n");
1044 f_print (fout, "$(OBJECTS_CLNT) : $(SOURCES_CLNT.c) $(SOURCES_CLNT.h) \
1045 $(TARGETS_CLNT.c) \n\n");
1047 f_print (fout, "$(OBJECTS_SVC) : $(SOURCES_SVC.c) $(SOURCES_SVC.h) \
1048 $(TARGETS_SVC.c) \n\n");
1049 f_print (fout, "$(CLIENT) : $(OBJECTS_CLNT) \n");
1050 f_print (fout, "\t$(LINK.c) -o $(CLIENT) $(OBJECTS_CLNT) \
1051 $(LDLIBS) \n\n");
1052 f_print (fout, "$(SERVER) : $(OBJECTS_SVC) \n");
1053 f_print (fout, "\t$(LINK.c) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS)\n\n ");
1054 f_print (fout, "clean:\n\t $(RM) core $(TARGETS) $(OBJECTS_CLNT) \
1055 $(OBJECTS_SVC) $(CLIENT) $(SERVER)\n\n");
1056 close_output (mkfilename);
1060 * Perform registrations for service output
1061 * Return 0 if failed; 1 otherwise.
1063 static int
1064 do_registers (int argc, const char *argv[])
1066 int i;
1068 if (inetdflag || !tirpcflag)
1070 for (i = 1; i < argc; i++)
1072 if (streq (argv[i], "-s"))
1074 if (!check_nettype (argv[i + 1], valid_i_nettypes))
1075 return 0;
1076 write_inetd_register (argv[i + 1]);
1077 i++;
1081 else
1083 for (i = 1; i < argc; i++)
1084 if (streq (argv[i], "-s"))
1086 if (!check_nettype (argv[i + 1], valid_ti_nettypes))
1087 return 0;
1088 write_nettype_register (argv[i + 1]);
1089 i++;
1091 else if (streq (argv[i], "-n"))
1093 write_netid_register (argv[i + 1]);
1094 i++;
1097 return 1;
1101 * Add another argument to the arg list
1103 static void
1104 addarg (const char *cp)
1106 if (argcount >= ARGLISTLEN)
1108 fprintf (stderr, _("rpcgen: too many defines\n"));
1109 crash ();
1110 /*NOTREACHED */
1112 arglist[argcount++] = cp;
1115 static void
1116 putarg (int whereto, const char *cp)
1118 if (whereto >= ARGLISTLEN)
1120 fprintf (stderr, _("rpcgen: arglist coding error\n"));
1121 crash ();
1122 /*NOTREACHED */
1124 arglist[whereto] = cp;
1128 * if input file is stdin and an output file is specified then complain
1129 * if the file already exists. Otherwise the file may get overwritten
1130 * If input file does not exist, exit with an error
1133 static void
1134 checkfiles (const char *infile, const char *outfile)
1136 struct stat buf;
1138 if (infile) /* infile ! = NULL */
1139 if (stat (infile, &buf) < 0)
1141 perror (infile);
1142 crash ();
1144 if (outfile)
1146 if (stat (outfile, &buf) < 0)
1147 return; /* file does not exist */
1148 else
1150 fprintf (stderr,
1151 /* TRANS: the file will not be removed; this is an
1152 TRANS: informative message. */
1153 _("file `%s' already exists and may be overwritten\n"),
1154 outfile);
1155 crash ();
1161 * Parse command line arguments
1163 static int
1164 parseargs (int argc, const char *argv[], struct commandline *cmd)
1166 int i;
1167 int j;
1168 int c;
1169 char flag[(1 << 8 * sizeof (char))];
1170 int nflags;
1172 cmdname = argv[0];
1173 cmd->infile = cmd->outfile = NULL;
1174 if (argc < 2)
1176 return (0);
1178 allfiles = 0;
1179 flag['c'] = 0;
1180 flag['h'] = 0;
1181 flag['l'] = 0;
1182 flag['m'] = 0;
1183 flag['o'] = 0;
1184 flag['s'] = 0;
1185 flag['n'] = 0;
1186 flag['t'] = 0;
1187 flag['S'] = 0;
1188 flag['C'] = 0;
1189 flag['M'] = 0;
1191 for (i = 1; i < argc; i++)
1193 if (argv[i][0] != '-')
1195 if (cmd->infile)
1197 fprintf (stderr,
1198 _("Cannot specify more than one input file!\n"));
1199 return 0;
1201 cmd->infile = argv[i];
1203 else
1205 for (j = 1; argv[i][j] != 0; j++)
1207 c = argv[i][j];
1208 switch (c)
1210 case 'a':
1211 allfiles = 1;
1212 break;
1213 case 'c':
1214 case 'h':
1215 case 'l':
1216 case 'm':
1217 case 't':
1218 if (flag[c])
1219 return 0;
1220 flag[c] = 1;
1221 break;
1222 case 'S':
1223 /* sample flag: Ss or Sc.
1224 Ss means set flag['S'];
1225 Sc means set flag['C'];
1226 Sm means set flag['M']; */
1227 c = argv[i][++j]; /* get next char */
1228 if (c == 's')
1229 c = 'S';
1230 else if (c == 'c')
1231 c = 'C';
1232 else if (c == 'm')
1233 c = 'M';
1234 else
1235 return 0;
1237 if (flag[c])
1238 return 0;
1239 flag[c] = 1;
1240 break;
1241 case 'C': /* ANSI C syntax */
1242 Cflag = 1;
1243 break;
1245 #ifdef __GNU_LIBRARY__
1246 case 'k': /* K&R C syntax */
1247 Cflag = 0;
1248 break;
1250 #endif
1251 case 'b': /* turn TIRPC flag off for
1252 generating backward compatible
1254 tirpcflag = 0;
1255 break;
1257 #ifdef __GNU_LIBRARY__
1258 case '5': /* turn TIRPC flag on for
1259 generating SysVr4 compatible
1261 tirpcflag = 1;
1262 break;
1263 #endif
1264 case 'I':
1265 inetdflag = 1;
1266 break;
1267 case 'N':
1268 newstyle = 1;
1269 break;
1270 case 'L':
1271 logflag = 1;
1272 break;
1273 case 'K':
1274 if (++i == argc)
1276 return (0);
1278 svcclosetime = argv[i];
1279 goto nextarg;
1280 case 'T':
1281 tblflag = 1;
1282 break;
1283 case 'M':
1284 mtflag = 1;
1285 break;
1286 case 'i':
1287 if (++i == argc)
1289 return (0);
1291 inlineflag = atoi (argv[i]);
1292 goto nextarg;
1293 case 'n':
1294 case 'o':
1295 case 's':
1296 if (argv[i][j - 1] != '-' ||
1297 argv[i][j + 1] != 0)
1299 return (0);
1301 flag[c] = 1;
1302 if (++i == argc)
1304 return (0);
1306 if (c == 's')
1308 if (!streq (argv[i], "udp") &&
1309 !streq (argv[i], "tcp") &&
1310 !streq (argv[i], "udp6") &&
1311 !streq (argv[i], "tcp6"))
1312 return 0;
1314 else if (c == 'o')
1316 if (cmd->outfile)
1317 return 0;
1318 cmd->outfile = argv[i];
1320 goto nextarg;
1321 case 'D':
1322 if (argv[i][j - 1] != '-')
1323 return 0;
1324 addarg (argv[i]);
1325 goto nextarg;
1326 case 'Y':
1327 if (++i == argc)
1328 return 0;
1330 size_t len = strlen (argv[i]);
1331 pathbuf = malloc (len + 5);
1332 if (pathbuf == NULL)
1334 perror (cmdname);
1335 crash ();
1337 stpcpy (stpcpy (pathbuf,
1338 argv[i]),
1339 "/cpp");
1340 CPP = pathbuf;
1341 cppDefined = 1;
1342 goto nextarg;
1345 default:
1346 return 0;
1349 nextarg:
1354 cmd->cflag = flag['c'];
1355 cmd->hflag = flag['h'];
1356 cmd->lflag = flag['l'];
1357 cmd->mflag = flag['m'];
1358 cmd->nflag = flag['n'];
1359 cmd->sflag = flag['s'];
1360 cmd->tflag = flag['t'];
1361 cmd->Ssflag = flag['S'];
1362 cmd->Scflag = flag['C'];
1363 cmd->makefileflag = flag['M'];
1365 if (mtflag || newstyle)
1367 /* glibc doesn't support these flags. */
1368 f_print (stderr,
1369 _("This implementation doesn't support newstyle or MT-safe code!\n"));
1370 return (0);
1372 if (tirpcflag)
1374 pmflag = inetdflag ? 0 : 1; /* pmflag or inetdflag is always TRUE */
1375 if ((inetdflag && cmd->nflag))
1376 { /* netid not allowed with inetdflag */
1377 fprintf (stderr, _("Cannot use netid flag with inetd flag!\n"));
1378 return 0;
1381 else
1382 { /* 4.1 mode */
1383 pmflag = 0; /* set pmflag only in tirpcmode */
1384 #ifndef __GNU_LIBRARY__
1385 inetdflag = 1; /* inetdflag is TRUE by default */
1386 #endif
1387 if (cmd->nflag)
1388 { /* netid needs TIRPC */
1389 f_print (stderr, _("Cannot use netid flag without TIRPC!\n"));
1390 return (0);
1394 if (newstyle && (tblflag || cmd->tflag))
1396 f_print (stderr, _("Cannot use table flags with newstyle!\n"));
1397 return (0);
1400 /* check no conflicts with file generation flags */
1401 nflags = cmd->cflag + cmd->hflag + cmd->lflag + cmd->mflag +
1402 cmd->sflag + cmd->nflag + cmd->tflag + cmd->Ssflag + cmd->Scflag;
1404 if (nflags == 0)
1406 if (cmd->outfile != NULL || cmd->infile == NULL)
1408 return (0);
1411 else if (cmd->infile == NULL &&
1412 (cmd->Ssflag || cmd->Scflag || cmd->makefileflag))
1414 fprintf (stderr,
1415 _("\"infile\" is required for template generation flags.\n"));
1416 return 0;
1418 if (nflags > 1)
1420 fprintf (stderr, _("Cannot have more than one file generation flag!\n"));
1421 return 0;
1423 return 1;
1426 static void
1427 usage (void)
1429 fprintf (stderr, _("usage: %s infile\n"), cmdname);
1430 fprintf (stderr, _("\t%s [-abkCLNTM][-Dname[=value]] [-i size] \
1431 [-I [-K seconds]] [-Y path] infile\n"), cmdname);
1432 fprintf (stderr, _("\t%s [-c | -h | -l | -m | -t | -Sc | -Ss | -Sm] \
1433 [-o outfile] [infile]\n"), cmdname);
1434 fprintf (stderr, _("\t%s [-s nettype]* [-o outfile] [infile]\n"), cmdname);
1435 fprintf (stderr, _("\t%s [-n netid]* [-o outfile] [infile]\n"), cmdname);
1436 options_usage ();
1437 exit (1);
1440 static void
1441 options_usage (void)
1443 f_print (stderr, "options:\n");
1444 f_print (stderr, "-a\t\tgenerate all files, including samples\n");
1445 f_print (stderr, "-b\t\tbackward compatibility mode (generates code for SunOS 4.1)\n");
1446 f_print (stderr, "-c\t\tgenerate XDR routines\n");
1447 f_print (stderr, "-C\t\tANSI C mode\n");
1448 f_print (stderr, "-Dname[=value]\tdefine a symbol (same as #define)\n");
1449 f_print (stderr, "-h\t\tgenerate header file\n");
1450 f_print (stderr, "-i size\t\tsize at which to start generating inline code\n");
1451 f_print (stderr, "-I\t\tgenerate code for inetd support in server (for SunOS 4.1)\n");
1452 f_print (stderr, "-K seconds\tserver exits after K seconds of inactivity\n");
1453 f_print (stderr, "-l\t\tgenerate client side stubs\n");
1454 f_print (stderr, "-L\t\tserver errors will be printed to syslog\n");
1455 f_print (stderr, "-m\t\tgenerate server side stubs\n");
1456 f_print (stderr, "-M\t\tgenerate MT-safe code\n");
1457 f_print (stderr, "-n netid\tgenerate server code that supports named netid\n");
1458 f_print (stderr, "-N\t\tsupports multiple arguments and call-by-value\n");
1459 f_print (stderr, "-o outfile\tname of the output file\n");
1460 f_print (stderr, "-s nettype\tgenerate server code that supports named nettype\n");
1461 f_print (stderr, "-Sc\t\tgenerate sample client code that uses remote procedures\n");
1462 f_print (stderr, "-Ss\t\tgenerate sample server code that defines remote procedures\n");
1463 f_print (stderr, "-Sm \t\tgenerate makefile template \n");
1464 f_print (stderr, "-t\t\tgenerate RPC dispatch table\n");
1465 f_print (stderr, "-T\t\tgenerate code to support RPC dispatch tables\n");
1466 f_print (stderr, "-Y path\t\tdirectory name to find C preprocessor (cpp)\n");
1468 exit (1);