.
[glibc/history.git] / sunrpc / rpc_main.c
blob2679720a7f71124e164ebb91a71c62ef80994f47
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;
36 * rpc_main.c, Top level of the RPC protocol compiler.
39 #include <errno.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <libintl.h>
44 #include <ctype.h>
45 #include <sys/types.h>
46 #include <sys/param.h>
47 #include <sys/file.h>
48 #include <sys/stat.h>
49 #include <sys/wait.h>
50 #include "rpc_parse.h"
51 #include "rpc_util.h"
52 #include "rpc_scan.h"
53 #include "proto.h"
55 #define EXTEND 1 /* alias for TRUE */
56 #define DONT_EXTEND 0 /* alias for FALSE */
58 struct commandline
60 int cflag; /* xdr C routines */
61 int hflag; /* header file */
62 int lflag; /* client side stubs */
63 int mflag; /* server side stubs */
64 int nflag; /* netid flag */
65 int sflag; /* server stubs for the given transport */
66 int tflag; /* dispatch Table file */
67 int Ssflag; /* produce server sample code */
68 int Scflag; /* produce client sample code */
69 int makefileflag; /* Generate a template Makefile */
70 const char *infile; /* input module name */
71 const char *outfile; /* output module name */
75 static const char *cmdname;
77 #define SVR4_CPP "/usr/ccs/lib/cpp"
78 #define SUNOS_CPP "/lib/cpp"
80 static const char *svcclosetime = "120";
81 static int cppDefined; /* explicit path for C preprocessor */
82 static const char *CPP = SUNOS_CPP;
83 static const char CPPFLAGS[] = "-C";
84 static char *pathbuf;
85 static int cpp_pid;
86 static const char *allv[] =
88 "rpcgen", "-s", "udp", "-s", "tcp"
90 static int allc = sizeof (allv) / sizeof (allv[0]);
91 static const char *allnv[] =
93 "rpcgen", "-s", "netpath",
95 static int allnc = sizeof (allnv) / sizeof (allnv[0]);
98 * machinations for handling expanding argument list
100 static void addarg (const char *); /* add another argument to the list */
101 static void putarg (int, const char *); /* put argument at specified location */
102 static void clear_args (void); /* clear argument list */
103 static void checkfiles (const char *, const char *);
104 /* check if out file already exists */
106 static void clear_args (void);
107 static char *extendfile (const char *file, const char *ext);
108 static void open_output (const char *infile, const char *outfile);
109 static void add_warning (void);
110 static void clear_args (void);
111 static void find_cpp (void);
112 static void open_input (const char *infile, const char *define);
113 static int check_nettype (const char *name, const char *list_to_check[]);
114 static void c_output (const char *infile, const char *define,
115 int extend, const char *outfile);
116 static void h_output (const char *infile, const char *define,
117 int extend, const char *outfile);
118 static void s_output (int argc, const char *argv[], const char *infile,
119 const char *define, int extend,
120 const char *outfile, int nomain, int netflag);
121 static void l_output (const char *infile, const char *define,
122 int extend, const char *outfile);
123 static void t_output (const char *infile, const char *define,
124 int extend, const char *outfile);
125 static void svc_output (const char *infile, const char *define,
126 int extend, const char *outfile);
127 static void clnt_output (const char *infile, const char *define,
128 int extend, const char *outfile);
129 static void mkfile_output (struct commandline *cmd);
130 static int do_registers (int argc, const char *argv[]);
131 static void addarg (const char *cp);
132 static void putarg (int whereto, const char *cp);
133 static void checkfiles (const char *infile, const char *outfile);
134 static int parseargs (int argc, const char *argv[], struct commandline *cmd);
135 static void usage (void) __attribute__ ((noreturn));
136 static void options_usage (void) __attribute__ ((noreturn));
137 static void c_initialize (void);
138 static char *generate_guard (const char *pathname);
141 #define ARGLISTLEN 20
142 #define FIXEDARGS 2
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 */
155 #define INLINE 3
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 #ifdef __GNU_LIBRARY__
166 int Cflag = 1; /* ANSI C syntax */
167 #else
168 int Cflag; /* ANSI C/C++ syntax */
169 #endif
170 int CCflag; /* C++ files */
171 static int allfiles; /* generate all files */
172 #ifdef __GNU_LIBRARY__
173 int tirpcflag; /* generating code for tirpc, by default */
174 #else
175 int tirpcflag = 1; /* generating code for tirpc, by default */
176 #endif
177 xdrfunc *xdrfunc_head; /* xdr function list */
178 xdrfunc *xdrfunc_tail; /* xdr function list */
181 main (int argc, const char *argv[])
183 struct commandline cmd;
185 (void) memset ((char *) &cmd, 0, sizeof (struct commandline));
186 clear_args ();
187 if (!parseargs (argc, argv, &cmd))
188 usage ();
190 if (cmd.cflag || cmd.hflag || cmd.lflag || cmd.tflag || cmd.sflag ||
191 cmd.mflag || cmd.nflag || cmd.Ssflag || cmd.Scflag)
193 checkfiles (cmd.infile, cmd.outfile);
195 else
196 checkfiles (cmd.infile, NULL);
198 if (cmd.cflag)
199 c_output (cmd.infile, "-DRPC_XDR", DONT_EXTEND, cmd.outfile);
200 else if (cmd.hflag)
201 h_output (cmd.infile, "-DRPC_HDR", DONT_EXTEND, cmd.outfile);
202 else if (cmd.lflag)
203 l_output (cmd.infile, "-DRPC_CLNT", DONT_EXTEND, cmd.outfile);
204 else if (cmd.sflag || cmd.mflag || (cmd.nflag))
205 s_output (argc, argv, cmd.infile, "-DRPC_SVC", DONT_EXTEND,
206 cmd.outfile, cmd.mflag, cmd.nflag);
207 else if (cmd.tflag)
208 t_output (cmd.infile, "-DRPC_TBL", DONT_EXTEND, cmd.outfile);
209 else if (cmd.Ssflag)
210 svc_output (cmd.infile, "-DRPC_SERVER", DONT_EXTEND, cmd.outfile);
211 else if (cmd.Scflag)
212 clnt_output (cmd.infile, "-DRPC_CLIENT", DONT_EXTEND, cmd.outfile);
213 else if (cmd.makefileflag)
214 mkfile_output (&cmd);
215 else
217 /* the rescans are required, since cpp may effect input */
218 c_output (cmd.infile, "-DRPC_XDR", EXTEND, "_xdr.c");
219 reinitialize ();
220 h_output (cmd.infile, "-DRPC_HDR", EXTEND, ".h");
221 reinitialize ();
222 l_output (cmd.infile, "-DRPC_CLNT", EXTEND, "_clnt.c");
223 reinitialize ();
224 if (inetdflag || !tirpcflag)
225 s_output (allc, allv, cmd.infile, "-DRPC_SVC", EXTEND,
226 "_svc.c", cmd.mflag, cmd.nflag);
227 else
228 s_output (allnc, allnv, cmd.infile, "-DRPC_SVC",
229 EXTEND, "_svc.c", cmd.mflag, cmd.nflag);
230 if (tblflag)
232 reinitialize ();
233 t_output (cmd.infile, "-DRPC_TBL", EXTEND, "_tbl.i");
235 if (allfiles)
237 reinitialize ();
238 svc_output (cmd.infile, "-DRPC_SERVER", EXTEND, "_server.c");
239 reinitialize ();
240 clnt_output (cmd.infile, "-DRPC_CLIENT", EXTEND, "_client.c");
242 if (allfiles || (cmd.makefileflag == 1))
244 reinitialize ();
245 mkfile_output (&cmd);
249 return nonfatalerrors;
253 * add extension to filename
255 static char *
256 extendfile (const char *file, const char *ext)
258 char *res;
259 const char *p;
261 res = alloc (strlen (file) + strlen (ext) + 1);
262 if (res == NULL)
263 abort ();
264 p = strrchr (file, '.');
265 if (p == NULL)
266 p = file + strlen (file);
267 strcpy (res, file);
268 strcpy (res + (p - file), ext);
269 return res;
273 * Open output file with given extension
275 static void
276 open_output (const char *infile, const char *outfile)
278 if (outfile == NULL)
280 fout = stdout;
281 return;
284 if (infile != NULL && streq (outfile, infile))
286 fprintf (stderr, _ ("%s: output would overwrite %s\n"), cmdname,
287 infile);
288 crash ();
290 fout = fopen (outfile, "w");
291 if (fout == NULL)
293 fprintf (stderr, _ ("%s: unable to open %s: %m\n"), cmdname, outfile);
294 crash ();
296 record_open (outfile);
299 /* Close the output file and check for write errors. */
300 static void
301 close_output (const char *outfile)
303 if (fclose (fout) == EOF)
305 fprintf (stderr, _("%s: while writing output %s: %m"), cmdname,
306 outfile ?: "<stdout>");
307 crash ();
311 static void
312 add_warning (void)
314 fprintf (fout, "/*\n");
315 fprintf (fout, " * Please do not edit this file.\n");
316 fprintf (fout, " * It was generated using rpcgen.\n");
317 fprintf (fout, " */\n\n");
320 /* clear list of arguments */
321 static void
322 clear_args (void)
324 int i;
325 for (i = FIXEDARGS; i < ARGLISTLEN; ++i)
326 arglist[i] = NULL;
327 argcount = FIXEDARGS;
330 /* make sure that a CPP exists */
331 static void
332 find_cpp (void)
334 struct stat buf;
336 if (stat (CPP, &buf) < 0)
337 { /* /lib/cpp or explicit cpp does not exist */
338 if (cppDefined)
340 fprintf (stderr, _ ("cannot find C preprocessor: %s \n"), CPP);
341 crash ();
343 else
344 { /* try the other one */
345 CPP = SVR4_CPP;
346 if (stat (CPP, &buf) < 0)
347 { /* can't find any cpp */
348 fputs (_ ("cannot find any C preprocessor (cpp)\n"), stdout);
349 crash ();
356 * Open input file with given define for C-preprocessor
358 static void
359 open_input (const char *infile, const char *define)
361 int pd[2];
363 infilename = (infile == NULL) ? "<stdin>" : infile;
364 if (pipe (pd) != 0)
366 perror ("pipe");
367 exit (1);
369 cpp_pid = fork ();
370 switch (cpp_pid)
372 case 0:
373 find_cpp ();
374 putarg (0, CPP);
375 putarg (1, CPPFLAGS);
376 addarg (define);
377 if (infile)
378 addarg (infile);
379 addarg ((char *) NULL);
380 close (1);
381 dup2 (pd[1], 1);
382 close (pd[0]);
383 execv (arglist[0], (char **) arglist);
384 perror ("execv");
385 exit (1);
386 case -1:
387 perror ("fork");
388 exit (1);
390 close (pd[1]);
391 fin = fdopen (pd[0], "r");
392 if (fin == NULL)
394 fprintf (stderr, "%s: ", cmdname);
395 perror (infilename);
396 crash ();
400 /* Close the connection to the C-preprocessor and check for successfull
401 termination. */
402 static void
403 close_input (void)
405 int status;
407 fclose (fin);
408 /* Check the termination status. */
409 if (waitpid (cpp_pid, &status, 0) < 0)
411 perror ("waitpid");
412 crash ();
414 if (WIFSIGNALED (status) || WEXITSTATUS (status) != 0)
416 if (WIFSIGNALED (status))
417 fprintf (stderr, _("%s: C preprocessor failed with signal %d\n"),
418 cmdname, WTERMSIG (status));
419 else
420 fprintf (stderr, _("%s: C preprocessor failed with exit code %d\n"),
421 cmdname, WEXITSTATUS (status));
422 crash ();
426 /* valid tirpc nettypes */
427 static const char *valid_ti_nettypes[] =
429 "netpath",
430 "visible",
431 "circuit_v",
432 "datagram_v",
433 "circuit_n",
434 "datagram_n",
435 "udp",
436 "tcp",
437 "raw",
438 NULL
441 /* valid inetd nettypes */
442 static const char *valid_i_nettypes[] =
444 "udp",
445 "tcp",
446 NULL
449 static int
450 check_nettype (const char *name, const char *list_to_check[])
452 int i;
453 for (i = 0; list_to_check[i] != NULL; i++)
455 if (strcmp (name, list_to_check[i]) == 0)
457 return 1;
460 fprintf (stderr, _ ("illegal nettype :`%s'\n"), name);
461 return 0;
465 * Compile into an XDR routine output file
468 static void
469 c_output (const char *infile, const char *define, int extend,
470 const char *outfile)
472 definition *def;
473 char *include;
474 const char *outfilename;
475 long tell;
477 c_initialize ();
478 open_input (infile, define);
479 outfilename = extend ? extendfile (infile, outfile) : outfile;
480 open_output (infile, outfilename);
481 add_warning ();
482 if (infile && (include = extendfile (infile, ".h")))
484 fprintf (fout, "#include \"%s\"\n", include);
485 free (include);
486 /* .h file already contains rpc/rpc.h */
488 else
489 fprintf (fout, "#include <rpc/rpc.h>\n");
490 tell = ftell (fout);
491 while ((def = get_definition ()) != NULL)
492 emit (def);
494 if (extend && tell == ftell (fout))
495 unlink (outfilename);
496 close_input ();
497 close_output (outfilename);
500 void
501 c_initialize (void)
504 /* add all the starting basic types */
506 add_type (1, "int");
507 add_type (1, "long");
508 add_type (1, "short");
509 add_type (1, "bool");
511 add_type (1, "u_int");
512 add_type (1, "u_long");
513 add_type (1, "u_short");
517 char rpcgen_table_dcl[] = "struct rpcgen_table {\n\
518 char *(*proc)();\n\
519 xdrproc_t xdr_arg;\n\
520 unsigned len_arg;\n\
521 xdrproc_t xdr_res;\n\
522 unsigned len_res;\n\
523 };\n";
526 static char *
527 generate_guard (const char *pathname)
529 const char *filename;
530 char *guard, *tmp;
532 filename = strrchr (pathname, '/'); /* find last component */
533 filename = ((filename == NULL) ? pathname : filename + 1);
534 guard = strdup (filename);
535 /* convert to upper case */
536 tmp = guard;
537 while (*tmp)
539 if (islower (*tmp))
540 *tmp = toupper (*tmp);
541 tmp++;
544 guard = extendfile (guard, "_H_RPCGEN");
545 return guard;
549 * Compile into an XDR header file
553 static void
554 h_output (const char *infile, const char *define, int extend,
555 const char *outfile)
557 xdrfunc *xdrfuncp;
558 definition *def;
559 const char *ifilename;
560 const char *outfilename;
561 long tell;
562 char *guard;
563 list *l;
565 open_input (infile, define);
566 outfilename = extend ? extendfile (infile, outfile) : outfile;
567 open_output (infile, outfilename);
568 add_warning ();
569 ifilename = (infile == NULL) ? "STDIN" : infile;
570 guard = generate_guard (outfilename ? outfilename : ifilename);
572 fprintf (fout, "#ifndef _%s\n#define _%s\n\n", guard,
573 guard);
575 fprintf (fout, "#include <rpc/rpc.h>\n\n");
577 if (mtflag)
579 fprintf (fout, "#include <pthread.h>\n");
582 /* put the C++ support */
583 if (Cflag && !CCflag)
585 fprintf (fout, "\n#ifdef __cplusplus\n");
586 fprintf (fout, "extern \"C\" {\n");
587 fprintf (fout, "#endif\n\n");
590 tell = ftell (fout);
591 /* print data definitions */
592 while ((def = get_definition ()) != NULL)
594 print_datadef (def);
597 /* print function declarations.
598 Do this after data definitions because they might be used as
599 arguments for functions */
600 for (l = defined; l != NULL; l = l->next)
602 print_funcdef (l->val);
604 /* Now print all xdr func declarations */
605 if (xdrfunc_head != NULL)
607 fprintf (fout, "\n/* the xdr functions */\n");
608 if (CCflag)
610 fprintf (fout, "\n#ifdef __cplusplus\n");
611 fprintf (fout, "extern \"C\" {\n");
612 fprintf (fout, "#endif\n");
614 if (!Cflag)
616 xdrfuncp = xdrfunc_head;
617 while (xdrfuncp != NULL)
619 print_xdr_func_def (xdrfuncp->name,
620 xdrfuncp->pointerp, 2);
621 xdrfuncp = xdrfuncp->next;
624 else
626 int i;
628 for (i = 1; i < 3; ++i)
630 if (i == 1)
631 fprintf (fout, "\n#if defined(__STDC__) || defined(__cplusplus)\n");
632 else
633 fprintf (fout, "\n#else /* K&R C */\n");
635 xdrfuncp = xdrfunc_head;
636 while (xdrfuncp != NULL)
638 print_xdr_func_def (xdrfuncp->name,
639 xdrfuncp->pointerp, i);
640 xdrfuncp = xdrfuncp->next;
643 fprintf (fout, "\n#endif /* K&R C */\n");
647 if (extend && tell == ftell (fout))
649 unlink (outfilename);
651 else if (tblflag)
653 fprintf (fout, rpcgen_table_dcl);
656 if (Cflag)
658 fprintf (fout, "\n#ifdef __cplusplus\n");
659 fprintf (fout, "}\n");
660 fprintf (fout, "#endif\n");
663 fprintf (fout, "\n#endif /* !_%s */\n", guard);
664 close_input ();
665 close_output (outfilename);
669 * Compile into an RPC service
671 static void
672 s_output (int argc, const char *argv[], const char *infile, const char *define,
673 int extend, const char *outfile, int nomain, int netflag)
675 char *include;
676 definition *def;
677 int foundprogram = 0;
678 const char *outfilename;
680 open_input (infile, define);
681 outfilename = extend ? extendfile (infile, outfile) : outfile;
682 open_output (infile, outfilename);
683 add_warning ();
684 if (infile && (include = extendfile (infile, ".h")))
686 fprintf (fout, "#include \"%s\"\n", include);
687 free (include);
689 else
690 fprintf (fout, "#include <rpc/rpc.h>\n");
692 fprintf (fout, "#include <stdio.h>\n");
693 fprintf (fout, "#include <stdlib.h>\n");
694 fprintf (fout, "#include <rpc/pmap_clnt.h>\n");
695 if (Cflag)
696 fprintf (fout, "#include <string.h>\n");
697 if (strcmp (svcclosetime, "-1") == 0)
698 indefinitewait = 1;
699 else if (strcmp (svcclosetime, "0") == 0)
700 exitnow = 1;
701 else if (inetdflag || pmflag)
703 fprintf (fout, "#include <signal.h>\n");
704 timerflag = 1;
707 if (!tirpcflag && inetdflag)
708 #ifdef __GNU_LIBRARY__
709 fprintf (fout, "#include <sys/ioctl.h> /* ioctl, TIOCNOTTY */\n");
710 #else
711 fprintf (fout, "#include <sys/ttycom.h>/* TIOCNOTTY */\n");
712 #endif
713 if (Cflag && (inetdflag || pmflag))
715 #ifdef __GNU_LIBRARY__
716 fprintf (fout, "#include <sys/types.h> /* open */\n");
717 fprintf (fout, "#include <sys/stat.h> /* open */\n");
718 fprintf (fout, "#include <fcntl.h> /* open */\n");
719 fprintf (fout, "#include <unistd.h> /* getdtablesize */\n");
720 #else
721 fprintf (fout, "#ifdef __cplusplus\n");
722 fprintf (fout, "#include <sysent.h> /* getdtablesize, open */\n");
723 fprintf (fout, "#endif /* __cplusplus */\n");
724 if (tirpcflag)
725 fprintf (fout, "#include <unistd.h> /* setsid */\n");
726 #endif
728 #ifdef __GNU_LIBRARY__
729 if (tirpcflag && !(Cflag && (inetdflag || pmflag)))
730 #else
731 if (tirpcflag)
732 #endif
733 fprintf (fout, "#include <sys/types.h>\n");
735 fprintf (fout, "#include <memory.h>\n");
736 #ifndef __GNU_LIBRARY__
737 fprintf (fout, "#include <stropts.h>\n");
738 #endif
739 if (inetdflag || !tirpcflag)
741 fprintf (fout, "#include <sys/socket.h>\n");
742 fprintf (fout, "#include <netinet/in.h>\n");
745 if ((netflag || pmflag) && tirpcflag && !nomain)
747 fprintf (fout, "#include <netconfig.h>\n");
749 if ( /*timerflag && */ tirpcflag)
750 fprintf (fout, "#include <sys/resource.h> /* rlimit */\n");
751 if (logflag || inetdflag || pmflag)
753 #ifdef __GNU_LIBRARY__
754 fprintf (fout, "#include <syslog.h>\n");
755 #else
756 fprintf (fout, "#ifdef SYSLOG\n");
757 fprintf (fout, "#include <syslog.h>\n");
758 fprintf (fout, "#else\n");
759 fprintf (fout, "#define LOG_ERR 1\n");
760 fprintf (fout, "#define openlog(a, b, c)\n");
761 fprintf (fout, "#endif\n");
762 #endif
765 /* for ANSI-C */
766 if (Cflag)
767 fprintf (fout, "\n#ifndef SIG_PF\n#define SIG_PF void(*)(int)\n#endif\n");
769 #ifndef __GNU_LIBRARY__
770 fprintf (fout, "\n#ifdef DEBUG\n#define RPC_SVC_FG\n#endif\n");
771 #endif
772 if (timerflag)
773 fprintf (fout, "\n#define _RPCSVC_CLOSEDOWN %s\n", svcclosetime);
774 while ((def = get_definition ()) != NULL)
776 foundprogram |= (def->def_kind == DEF_PROGRAM);
778 if (extend && !foundprogram)
780 unlink (outfilename);
781 return;
783 write_most (infile, netflag, nomain);
784 if (!nomain)
786 if (!do_registers (argc, argv))
788 if (outfilename)
789 unlink (outfilename);
790 usage ();
792 write_rest ();
794 close_input ();
795 close_output (outfilename);
799 * generate client side stubs
801 static void
802 l_output (const char *infile, const char *define, int extend,
803 const char *outfile)
805 char *include;
806 definition *def;
807 int foundprogram = 0;
808 const char *outfilename;
810 open_input (infile, define);
811 outfilename = extend ? extendfile (infile, outfile) : outfile;
812 open_output (infile, outfilename);
813 add_warning ();
814 if (Cflag)
815 fprintf (fout, "#include <memory.h> /* for memset */\n");
816 if (infile && (include = extendfile (infile, ".h")))
818 fprintf (fout, "#include \"%s\"\n", include);
819 free (include);
821 else
822 fprintf (fout, "#include <rpc/rpc.h>\n");
823 while ((def = get_definition ()) != NULL)
825 foundprogram |= (def->def_kind == DEF_PROGRAM);
827 if (extend && !foundprogram)
829 unlink (outfilename);
830 return;
832 write_stubs ();
833 close_input ();
834 close_output (outfilename);
838 * generate the dispatch table
840 static void
841 t_output (const char *infile, const char *define, int extend,
842 const char *outfile)
844 definition *def;
845 int foundprogram = 0;
846 const char *outfilename;
848 open_input (infile, define);
849 outfilename = extend ? extendfile (infile, outfile) : outfile;
850 open_output (infile, outfilename);
851 add_warning ();
852 while ((def = get_definition ()) != NULL)
854 foundprogram |= (def->def_kind == DEF_PROGRAM);
856 if (extend && !foundprogram)
858 unlink (outfilename);
859 return;
861 write_tables ();
862 close_input ();
863 close_output (outfilename);
866 /* sample routine for the server template */
867 static void
868 svc_output (const char *infile, const char *define, int extend,
869 const char *outfile)
871 definition *def;
872 char *include;
873 const char *outfilename;
874 long tell;
876 open_input (infile, define);
877 outfilename = extend ? extendfile (infile, outfile) : outfile;
878 checkfiles (infile, outfilename);
879 /*check if outfile already exists.
880 if so, print an error message and exit */
881 open_output (infile, outfilename);
882 add_sample_msg ();
884 if (infile && (include = extendfile (infile, ".h")))
886 fprintf (fout, "#include \"%s\"\n", include);
887 free (include);
889 else
890 fprintf (fout, "#include <rpc/rpc.h>\n");
892 tell = ftell (fout);
893 while ((def = get_definition ()) != NULL)
895 write_sample_svc (def);
897 if (extend && tell == ftell (fout))
899 unlink (outfilename);
901 close_input ();
902 close_output (outfilename);
906 /* sample main routine for client */
907 static void
908 clnt_output (const char *infile, const char *define, int extend,
909 const char *outfile)
911 definition *def;
912 char *include;
913 const char *outfilename;
914 long tell;
915 int has_program = 0;
917 open_input (infile, define);
918 outfilename = extend ? extendfile (infile, outfile) : outfile;
919 checkfiles (infile, outfilename);
920 /*check if outfile already exists.
921 if so, print an error message and exit */
923 open_output (infile, outfilename);
924 add_sample_msg ();
925 if (infile && (include = extendfile (infile, ".h")))
927 fprintf (fout, "#include \"%s\"\n", include);
928 free (include);
930 else
931 fprintf (fout, "#include <rpc/rpc.h>\n");
932 tell = ftell (fout);
933 while ((def = get_definition ()) != NULL)
935 has_program += write_sample_clnt (def);
938 if (has_program)
939 write_sample_clnt_main ();
941 if (extend && tell == ftell (fout))
943 unlink (outfilename);
945 close_input ();
946 close_output (outfilename);
949 static char *
950 file_name (const char *file, const char *ext)
952 char *temp;
953 temp = extendfile (file, ext);
955 if (access (temp, F_OK) != -1)
956 return (temp);
957 else
958 return ((char *) " ");
961 static void
962 mkfile_output (struct commandline *cmd)
964 char *mkfilename;
965 const char *clientname, *clntname, *xdrname, *hdrname;
966 const char *servername, *svcname, *servprogname, *clntprogname;
968 svcname = file_name (cmd->infile, "_svc.c");
969 clntname = file_name (cmd->infile, "_clnt.c");
970 xdrname = file_name (cmd->infile, "_xdr.c");
971 hdrname = file_name (cmd->infile, ".h");
973 if (allfiles)
975 servername = extendfile (cmd->infile, "_server.c");
976 clientname = extendfile (cmd->infile, "_client.c");
978 else
980 servername = " ";
981 clientname = " ";
983 servprogname = extendfile (cmd->infile, "_server");
984 clntprogname = extendfile (cmd->infile, "_client");
986 if (allfiles)
988 char *cp, *temp;
990 mkfilename = alloc (strlen ("Makefile.") + strlen (cmd->infile) + 1);
991 temp = rindex (cmd->infile, '.');
992 cp = stpcpy (mkfilename, "Makefile.");
993 strncpy (cp, cmd->infile, (temp - cmd->infile));
995 else
996 mkfilename = (char *) cmd->outfile;
998 checkfiles (NULL, mkfilename);
999 open_output (NULL, mkfilename);
1001 fprintf (fout, "\n# This is a template Makefile generated by rpcgen\n");
1003 f_print (fout, "\n# Parameters\n\n");
1005 f_print (fout, "CLIENT = %s\nSERVER = %s\n\n", clntprogname, servprogname);
1006 f_print (fout, "SOURCES_CLNT.c = \nSOURCES_CLNT.h = \n");
1007 f_print (fout, "SOURCES_SVC.c = \nSOURCES_SVC.h = \n");
1008 f_print (fout, "SOURCES.x = %s\n\n", cmd->infile);
1009 f_print (fout, "TARGETS_SVC.c = %s %s %s \n",
1010 svcname, servername, xdrname);
1011 f_print (fout, "TARGETS_CLNT.c = %s %s %s \n",
1012 clntname, clientname, xdrname);
1013 f_print (fout, "TARGETS = %s %s %s %s %s %s\n\n",
1014 hdrname, xdrname, clntname,
1015 svcname, clientname, servername);
1017 f_print (fout, "OBJECTS_CLNT = $(SOURCES_CLNT.c:%%.c=%%.o) \
1018 $(TARGETS_CLNT.c:%%.c=%%.o)");
1020 f_print (fout, "\nOBJECTS_SVC = $(SOURCES_SVC.c:%%.c=%%.o) \
1021 $(TARGETS_SVC.c:%%.c=%%.o)");
1023 f_print (fout, "\n# Compiler flags \n");
1024 if (mtflag)
1025 fprintf (fout, "\nCPPFLAGS += -D_REENTRANT\nCFLAGS += -g \nLDLIBS \
1026 += -lnsl -lpthread \n ");
1027 else
1028 f_print (fout, "\nCFLAGS += -g \nLDLIBS += -lnsl\n");
1029 f_print (fout, "RPCGENFLAGS = \n");
1031 f_print (fout, "\n# Targets \n\n");
1033 f_print (fout, "all : $(CLIENT) $(SERVER)\n\n");
1034 f_print (fout, "$(TARGETS) : $(SOURCES.x) \n");
1035 f_print (fout, "\trpcgen $(RPCGENFLAGS) $(SOURCES.x)\n\n");
1036 f_print (fout, "$(OBJECTS_CLNT) : $(SOURCES_CLNT.c) $(SOURCES_CLNT.h) \
1037 $(TARGETS_CLNT.c) \n\n");
1039 f_print (fout, "$(OBJECTS_SVC) : $(SOURCES_SVC.c) $(SOURCES_SVC.h) \
1040 $(TARGETS_SVC.c) \n\n");
1041 f_print (fout, "$(CLIENT) : $(OBJECTS_CLNT) \n");
1042 f_print (fout, "\t$(LINK.c) -o $(CLIENT) $(OBJECTS_CLNT) \
1043 $(LDLIBS) \n\n");
1044 f_print (fout, "$(SERVER) : $(OBJECTS_SVC) \n");
1045 f_print (fout, "\t$(LINK.c) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS)\n\n ");
1046 f_print (fout, "clean:\n\t $(RM) core $(TARGETS) $(OBJECTS_CLNT) \
1047 $(OBJECTS_SVC) $(CLIENT) $(SERVER)\n\n");
1048 close_output (mkfilename);
1052 * Perform registrations for service output
1053 * Return 0 if failed; 1 otherwise.
1055 static int
1056 do_registers (int argc, const char *argv[])
1058 int i;
1060 if (inetdflag || !tirpcflag)
1062 for (i = 1; i < argc; i++)
1064 if (streq (argv[i], "-s"))
1066 if (!check_nettype (argv[i + 1], valid_i_nettypes))
1067 return 0;
1068 write_inetd_register (argv[i + 1]);
1069 i++;
1073 else
1075 for (i = 1; i < argc; i++)
1076 if (streq (argv[i], "-s"))
1078 if (!check_nettype (argv[i + 1], valid_ti_nettypes))
1079 return 0;
1080 write_nettype_register (argv[i + 1]);
1081 i++;
1083 else if (streq (argv[i], "-n"))
1085 write_netid_register (argv[i + 1]);
1086 i++;
1089 return 1;
1093 * Add another argument to the arg list
1095 static void
1096 addarg (const char *cp)
1098 if (argcount >= ARGLISTLEN)
1100 fprintf (stderr, _("rpcgen: too many defines\n"));
1101 crash ();
1102 /*NOTREACHED */
1104 arglist[argcount++] = cp;
1107 static void
1108 putarg (int whereto, const char *cp)
1110 if (whereto >= ARGLISTLEN)
1112 fprintf (stderr, _("rpcgen: arglist coding error\n"));
1113 crash ();
1114 /*NOTREACHED */
1116 arglist[whereto] = cp;
1120 * if input file is stdin and an output file is specified then complain
1121 * if the file already exists. Otherwise the file may get overwritten
1122 * If input file does not exist, exit with an error
1125 static void
1126 checkfiles (const char *infile, const char *outfile)
1128 struct stat buf;
1130 if (infile) /* infile ! = NULL */
1131 if (stat (infile, &buf) < 0)
1133 perror (infile);
1134 crash ();
1136 if (outfile)
1138 if (stat (outfile, &buf) < 0)
1139 return; /* file does not exist */
1140 else
1142 fprintf (stderr,
1143 /* TRANS: the file will not be removed; this is an
1144 TRANS: informative message. */
1145 _("file `%s' already exists and may be overwritten\n"),
1146 outfile);
1147 crash ();
1153 * Parse command line arguments
1155 static int
1156 parseargs (int argc, const char *argv[], struct commandline *cmd)
1158 int i;
1159 int j;
1160 int c;
1161 char flag[(1 << 8 * sizeof (char))];
1162 int nflags;
1164 cmdname = argv[0];
1165 cmd->infile = cmd->outfile = NULL;
1166 if (argc < 2)
1168 return (0);
1170 allfiles = 0;
1171 flag['c'] = 0;
1172 flag['h'] = 0;
1173 flag['l'] = 0;
1174 flag['m'] = 0;
1175 flag['o'] = 0;
1176 flag['s'] = 0;
1177 flag['n'] = 0;
1178 flag['t'] = 0;
1179 flag['S'] = 0;
1180 flag['C'] = 0;
1181 flag['M'] = 0;
1183 for (i = 1; i < argc; i++)
1185 if (argv[i][0] != '-')
1187 if (cmd->infile)
1189 fprintf (stderr,
1190 _("Cannot specify more than one input file!\n"));
1191 return 0;
1193 cmd->infile = argv[i];
1195 else
1197 for (j = 1; argv[i][j] != 0; j++)
1199 c = argv[i][j];
1200 switch (c)
1202 case 'a':
1203 allfiles = 1;
1204 break;
1205 case 'c':
1206 case 'h':
1207 case 'l':
1208 case 'm':
1209 case 't':
1210 if (flag[c])
1211 return 0;
1212 flag[c] = 1;
1213 break;
1214 case 'S':
1215 /* sample flag: Ss or Sc.
1216 Ss means set flag['S'];
1217 Sc means set flag['C'];
1218 Sm means set flag['M']; */
1219 c = argv[i][++j]; /* get next char */
1220 if (c == 's')
1221 c = 'S';
1222 else if (c == 'c')
1223 c = 'C';
1224 else if (c == 'm')
1225 c = 'M';
1226 else
1227 return 0;
1229 if (flag[c])
1230 return 0;
1231 flag[c] = 1;
1232 break;
1233 case 'C': /* ANSI C syntax */
1234 Cflag = 1;
1235 break;
1237 #ifdef __GNU_LIBRARY__
1238 case 'k': /* K&R C syntax */
1239 Cflag = 0;
1240 break;
1242 #endif
1243 case 'b': /* turn TIRPC flag off for
1244 generating backward compatible
1246 tirpcflag = 0;
1247 break;
1249 #ifdef __GNU_LIBRARY__
1250 case '5': /* turn TIRPC flag on for
1251 generating SysVr4 compatible
1253 tirpcflag = 1;
1254 break;
1255 #endif
1256 case 'I':
1257 inetdflag = 1;
1258 break;
1259 case 'N':
1260 newstyle = 1;
1261 break;
1262 case 'L':
1263 logflag = 1;
1264 break;
1265 case 'K':
1266 if (++i == argc)
1268 return (0);
1270 svcclosetime = argv[i];
1271 goto nextarg;
1272 case 'T':
1273 tblflag = 1;
1274 break;
1275 case 'M':
1276 mtflag = 1;
1277 break;
1278 case 'i':
1279 if (++i == argc)
1281 return (0);
1283 inlineflag = atoi (argv[i]);
1284 goto nextarg;
1285 case 'n':
1286 case 'o':
1287 case 's':
1288 if (argv[i][j - 1] != '-' ||
1289 argv[i][j + 1] != 0)
1291 return (0);
1293 flag[c] = 1;
1294 if (++i == argc)
1296 return (0);
1298 if (c == 's')
1300 if (!streq (argv[i], "udp") &&
1301 !streq (argv[i], "tcp"))
1302 return 0;
1304 else if (c == 'o')
1306 if (cmd->outfile)
1307 return 0;
1308 cmd->outfile = argv[i];
1310 goto nextarg;
1311 case 'D':
1312 if (argv[i][j - 1] != '-')
1313 return 0;
1314 addarg (argv[i]);
1315 goto nextarg;
1316 case 'Y':
1317 if (++i == argc)
1318 return 0;
1320 size_t len = strlen (argv[i]);
1321 pathbuf = malloc (len + 5);
1322 if (pathbuf == NULL)
1324 perror (cmdname);
1325 crash ();
1327 stpcpy (stpcpy (pathbuf,
1328 argv[i]),
1329 "/cpp");
1330 CPP = pathbuf;
1331 cppDefined = 1;
1332 goto nextarg;
1335 default:
1336 return 0;
1339 nextarg:
1344 cmd->cflag = flag['c'];
1345 cmd->hflag = flag['h'];
1346 cmd->lflag = flag['l'];
1347 cmd->mflag = flag['m'];
1348 cmd->nflag = flag['n'];
1349 cmd->sflag = flag['s'];
1350 cmd->tflag = flag['t'];
1351 cmd->Ssflag = flag['S'];
1352 cmd->Scflag = flag['C'];
1353 cmd->makefileflag = flag['M'];
1355 #ifndef _RPC_THREAD_SAFE_
1356 if (mtflag || newstyle)
1358 /* glibc doesn't support these flags. */
1359 f_print (stderr,
1360 _("This implementation doesn't support newstyle or MT-safe code!\n"));
1361 return (0);
1363 #endif
1364 if (tirpcflag)
1366 pmflag = inetdflag ? 0 : 1; /* pmflag or inetdflag is always TRUE */
1367 if ((inetdflag && cmd->nflag))
1368 { /* netid not allowed with inetdflag */
1369 fprintf (stderr, _("Cannot use netid flag with inetd flag!\n"));
1370 return 0;
1373 else
1374 { /* 4.1 mode */
1375 pmflag = 0; /* set pmflag only in tirpcmode */
1376 #ifndef __GNU_LIBRARY__
1377 inetdflag = 1; /* inetdflag is TRUE by default */
1378 #endif
1379 if (cmd->nflag)
1380 { /* netid needs TIRPC */
1381 f_print (stderr, _("Cannot use netid flag without TIRPC!\n"));
1382 return (0);
1386 if (newstyle && (tblflag || cmd->tflag))
1388 f_print (stderr, _("Cannot use table flags with newstyle!\n"));
1389 return (0);
1392 /* check no conflicts with file generation flags */
1393 nflags = cmd->cflag + cmd->hflag + cmd->lflag + cmd->mflag +
1394 cmd->sflag + cmd->nflag + cmd->tflag + cmd->Ssflag + cmd->Scflag;
1396 if (nflags == 0)
1398 if (cmd->outfile != NULL || cmd->infile == NULL)
1400 return (0);
1403 else if (cmd->infile == NULL &&
1404 (cmd->Ssflag || cmd->Scflag || cmd->makefileflag))
1406 fprintf (stderr,
1407 _("\"infile\" is required for template generation flags.\n"));
1408 return 0;
1410 if (nflags > 1)
1412 fprintf (stderr, _("Cannot have more than one file generation flag!\n"));
1413 return 0;
1415 return 1;
1418 static void
1419 usage (void)
1421 fprintf (stderr, _("usage: %s infile\n"), cmdname);
1422 fprintf (stderr, _("\t%s [-abkCLNTM][-Dname[=value]] [-i size] \
1423 [-I [-K seconds]] [-Y path] infile\n"), cmdname);
1424 fprintf (stderr, _("\t%s [-c | -h | -l | -m | -t | -Sc | -Ss | -Sm] \
1425 [-o outfile] [infile]\n"), cmdname);
1426 fprintf (stderr, _("\t%s [-s nettype]* [-o outfile] [infile]\n"), cmdname);
1427 fprintf (stderr, _("\t%s [-n netid]* [-o outfile] [infile]\n"), cmdname);
1428 options_usage ();
1429 exit (1);
1432 static void
1433 options_usage (void)
1435 f_print (stderr, "options:\n");
1436 f_print (stderr, "-a\t\tgenerate all files, including samples\n");
1437 f_print (stderr, "-b\t\tbackward compatibility mode (generates code for SunOS 4.1)\n");
1438 f_print (stderr, "-c\t\tgenerate XDR routines\n");
1439 f_print (stderr, "-C\t\tANSI C mode\n");
1440 f_print (stderr, "-Dname[=value]\tdefine a symbol (same as #define)\n");
1441 f_print (stderr, "-h\t\tgenerate header file\n");
1442 f_print (stderr, "-i size\t\tsize at which to start generating inline code\n");
1443 f_print (stderr, "-I\t\tgenerate code for inetd support in server (for SunOS 4.1)\n");
1444 f_print (stderr, "-K seconds\tserver exits after K seconds of inactivity\n");
1445 f_print (stderr, "-l\t\tgenerate client side stubs\n");
1446 f_print (stderr, "-L\t\tserver errors will be printed to syslog\n");
1447 f_print (stderr, "-m\t\tgenerate server side stubs\n");
1448 f_print (stderr, "-M\t\tgenerate MT-safe code\n");
1449 f_print (stderr, "-n netid\tgenerate server code that supports named netid\n");
1450 f_print (stderr, "-N\t\tsupports multiple arguments and call-by-value\n");
1451 f_print (stderr, "-o outfile\tname of the output file\n");
1452 f_print (stderr, "-s nettype\tgenerate server code that supports named nettype\n");
1453 f_print (stderr, "-Sc\t\tgenerate sample client code that uses remote procedures\n");
1454 f_print (stderr, "-Ss\t\tgenerate sample server code that defines remote procedures\n");
1455 f_print (stderr, "-Sm \t\tgenerate makefile template \n");
1456 f_print (stderr, "-t\t\tgenerate RPC dispatch table\n");
1457 f_print (stderr, "-T\t\tgenerate code to support RPC dispatch tables\n");
1458 f_print (stderr, "-Y path\t\tdirectory name to find C preprocessor (cpp)\n");
1460 exit (1);