Update.
[glibc.git] / sunrpc / rpc_main.c
blobaec153faaa831513673a873b844aee8b2e565ec0
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 <ctype.h>
46 #include <sys/types.h>
47 #include <sys/param.h>
48 #include <sys/file.h>
49 #include <sys/stat.h>
50 #include <sys/wait.h>
51 #include "rpc_parse.h"
52 #include "rpc_util.h"
53 #include "rpc_scan.h"
54 #include "proto.h"
56 #define EXTEND 1 /* alias for TRUE */
57 #define DONT_EXTEND 0 /* alias for FALSE */
59 struct commandline
61 int cflag; /* xdr C routines */
62 int hflag; /* header file */
63 int lflag; /* client side stubs */
64 int mflag; /* server side stubs */
65 int nflag; /* netid flag */
66 int sflag; /* server stubs for the given transport */
67 int tflag; /* dispatch Table file */
68 int Ssflag; /* produce server sample code */
69 int Scflag; /* produce client sample code */
70 int makefileflag; /* Generate a template Makefile */
71 const char *infile; /* input module name */
72 const char *outfile; /* output module name */
76 static const char *cmdname;
78 #define SVR4_CPP "/usr/ccs/lib/cpp"
79 #define SUNOS_CPP "/lib/cpp"
81 static const char *svcclosetime = "120";
82 static int cppDefined = 0; /* explicit path for C preprocessor */
83 static const char *CPP = SUNOS_CPP;
84 static char CPPFLAGS[] = "-C";
85 static char *pathbuf;
86 static int cpp_pid;
87 static const char *allv[] =
89 "rpcgen", "-s", "udp", "-s", "tcp",
91 static int allc = sizeof (allv) / sizeof (allv[0]);
92 static const char *allnv[] =
94 "rpcgen", "-s", "netpath",
96 static int allnc = sizeof (allnv) / sizeof (allnv[0]);
99 * machinations for handling expanding argument list
101 static void addarg (const char *); /* add another argument to the list */
102 static void putarg (int, const char *); /* put argument at specified location */
103 static void clear_args (void); /* clear argument list */
104 static void checkfiles (const char *, const char *);
105 /* check if out file already exists */
107 static void clear_args (void);
108 static char *extendfile (const char *file, const char *ext);
109 static void open_output (const char *infile, const char *outfile);
110 static void add_warning (void);
111 static void clear_args (void);
112 static void find_cpp (void);
113 static void open_input (const char *infile, const char *define);
114 static int check_nettype (const char *name, const char *list_to_check[]);
115 static void c_output (const char *infile, const char *define,
116 int extend, const char *outfile);
117 static void h_output (const char *infile, const char *define,
118 int extend, const char *outfile);
119 static void s_output (int argc, const char *argv[], const char *infile,
120 const char *define, int extend,
121 const char *outfile, int nomain, int netflag);
122 static void l_output (const char *infile, const char *define,
123 int extend, const char *outfile);
124 static void t_output (const char *infile, const char *define,
125 int extend, const char *outfile);
126 static void svc_output (const char *infile, const char *define,
127 int extend, const char *outfile);
128 static void clnt_output (const char *infile, const char *define,
129 int extend, const char *outfile);
130 static void mkfile_output (struct commandline *cmd);
131 static int do_registers (int argc, const char *argv[]);
132 static void addarg (const char *cp);
133 static void putarg (int whereto, const char *cp);
134 static void checkfiles (const char *infile, const char *outfile);
135 static int parseargs (int argc, const char *argv[], struct commandline *cmd);
136 static void usage (void);
137 static void options_usage (void);
138 static void c_initialize (void);
139 static char *generate_guard (const char *pathname);
142 #define ARGLISTLEN 20
143 #define FIXEDARGS 2
145 static const char *arglist[ARGLISTLEN];
146 static int argcount = FIXEDARGS;
149 int nonfatalerrors; /* errors */
150 int inetdflag /* = 1 */ ; /* Support for inetd *//* is now the default */
151 int pmflag; /* Support for port monitors */
152 int logflag; /* Use syslog instead of fprintf for errors */
153 int tblflag; /* Support for dispatch table file */
154 int mtflag; /* Support for MT */
156 #define INLINE 3
157 /*length at which to start doing an inline */
159 int inlineflag = INLINE; /* length at which to start doing an inline. 3 = default
160 if 0, no xdr_inline code */
162 int indefinitewait; /* If started by port monitors, hang till it wants */
163 int exitnow; /* If started by port monitors, exit after the call */
164 int timerflag; /* TRUE if !indefinite && !exitnow */
165 int newstyle; /* newstyle of passing arguments (by value) */
166 #ifdef __GNU_LIBRARY__
167 int Cflag = 1; /* ANSI C syntax */
168 #else
169 int Cflag = 0; /* ANSI C/C++ syntax */
170 #endif
171 int CCflag = 0; /* C++ files */
172 static int allfiles; /* generate all files */
173 #ifdef __GNU_LIBRARY__
174 int tirpcflag = 0; /* generating code for tirpc, by default */
175 #else
176 int tirpcflag = 1; /* generating code for tirpc, by default */
177 #endif
178 xdrfunc *xdrfunc_head = NULL; /* xdr function list */
179 xdrfunc *xdrfunc_tail = NULL; /* xdr function list */
182 main (int argc, const char *argv[])
184 struct commandline cmd;
186 (void) memset ((char *) &cmd, 0, sizeof (struct commandline));
187 clear_args ();
188 if (!parseargs (argc, argv, &cmd))
189 usage ();
191 if (cmd.cflag || cmd.hflag || cmd.lflag || cmd.tflag || cmd.sflag ||
192 cmd.mflag || cmd.nflag || cmd.Ssflag || cmd.Scflag)
194 checkfiles (cmd.infile, cmd.outfile);
196 else
197 checkfiles (cmd.infile, NULL);
199 if (cmd.cflag)
200 c_output (cmd.infile, "-DRPC_XDR", DONT_EXTEND, cmd.outfile);
201 else if (cmd.hflag)
202 h_output (cmd.infile, "-DRPC_HDR", DONT_EXTEND, cmd.outfile);
203 else if (cmd.lflag)
204 l_output (cmd.infile, "-DRPC_CLNT", DONT_EXTEND, cmd.outfile);
205 else if (cmd.sflag || cmd.mflag || (cmd.nflag))
206 s_output (argc, argv, cmd.infile, "-DRPC_SVC", DONT_EXTEND,
207 cmd.outfile, cmd.mflag, cmd.nflag);
208 else if (cmd.tflag)
209 t_output (cmd.infile, "-DRPC_TBL", DONT_EXTEND, cmd.outfile);
210 else if (cmd.Ssflag)
211 svc_output (cmd.infile, "-DRPC_SERVER", DONT_EXTEND, cmd.outfile);
212 else if (cmd.Scflag)
213 clnt_output (cmd.infile, "-DRPC_CLIENT", DONT_EXTEND, cmd.outfile);
214 else if (cmd.makefileflag)
215 mkfile_output (&cmd);
216 else
218 /* the rescans are required, since cpp may effect input */
219 c_output (cmd.infile, "-DRPC_XDR", EXTEND, "_xdr.c");
220 reinitialize ();
221 h_output (cmd.infile, "-DRPC_HDR", EXTEND, ".h");
222 reinitialize ();
223 l_output (cmd.infile, "-DRPC_CLNT", EXTEND, "_clnt.c");
224 reinitialize ();
225 if (inetdflag || !tirpcflag)
226 s_output (allc, allv, cmd.infile, "-DRPC_SVC", EXTEND,
227 "_svc.c", cmd.mflag, cmd.nflag);
228 else
229 s_output (allnc, allnv, cmd.infile, "-DRPC_SVC",
230 EXTEND, "_svc.c", cmd.mflag, cmd.nflag);
231 if (tblflag)
233 reinitialize ();
234 t_output (cmd.infile, "-DRPC_TBL", EXTEND, "_tbl.i");
236 if (allfiles)
238 reinitialize ();
239 svc_output (cmd.infile, "-DRPC_SERVER", EXTEND, "_server.c");
240 reinitialize ();
241 clnt_output (cmd.infile, "-DRPC_CLIENT", EXTEND, "_client.c");
243 if (allfiles || (cmd.makefileflag == 1))
245 reinitialize ();
246 mkfile_output (&cmd);
249 exit (nonfatalerrors);
250 /* NOTREACHED */
254 * add extension to filename
256 static char *
257 extendfile (const char *file, const char *ext)
259 char *res;
260 const char *p;
262 res = alloc (strlen (file) + strlen (ext) + 1);
263 if (res == NULL)
264 abort ();
265 p = strrchr (file, '.');
266 if (p == NULL)
267 p = file + strlen (file);
268 strcpy (res, file);
269 strcpy (res + (p - file), ext);
270 return res;
274 * Open output file with given extension
276 static void
277 open_output (const char *infile, const char *outfile)
279 if (outfile == NULL)
281 fout = stdout;
282 return;
285 if (infile != NULL && streq (outfile, infile))
287 fprintf (stderr, _ ("%s: output would overwrite %s\n"), cmdname,
288 infile);
289 crash ();
291 fout = fopen (outfile, "w");
292 if (fout == NULL)
294 fprintf (stderr, _ ("%s: unable to open %s: %m\n"), cmdname, outfile);
295 crash ();
297 record_open (outfile);
300 /* Close the output file and check for write errors. */
301 static void
302 close_output (const char *outfile)
304 if (fclose (fout) == EOF)
306 fprintf (stderr, _("%s: while writing output %s: %m"), cmdname,
307 outfile ?: "<stdout>");
308 crash ();
312 static void
313 add_warning (void)
315 fprintf (fout, "/*\n");
316 fprintf (fout, " * Please do not edit this file.\n");
317 fprintf (fout, " * It was generated using rpcgen.\n");
318 fprintf (fout, " */\n\n");
321 /* clear list of arguments */
322 static void
323 clear_args (void)
325 int i;
326 for (i = FIXEDARGS; i < ARGLISTLEN; ++i)
327 arglist[i] = NULL;
328 argcount = FIXEDARGS;
331 /* make sure that a CPP exists */
332 static void
333 find_cpp (void)
335 struct stat buf;
337 if (stat (CPP, &buf) < 0)
338 { /* /lib/cpp or explicit cpp does not exist */
339 if (cppDefined)
341 fprintf (stderr, _ ("cannot find C preprocessor: %s \n"), CPP);
342 crash ();
344 else
345 { /* try the other one */
346 CPP = SVR4_CPP;
347 if (stat (CPP, &buf) < 0)
348 { /* can't find any cpp */
349 fputs (_ ("cannot find any C preprocessor (cpp)\n"), stdout);
350 crash ();
357 * Open input file with given define for C-preprocessor
359 static void
360 open_input (const char *infile, const char *define)
362 int pd[2];
364 infilename = (infile == NULL) ? "<stdin>" : infile;
365 if (pipe (pd) != 0)
367 perror ("pipe");
368 exit (1);
370 cpp_pid = fork ();
371 switch (cpp_pid)
373 case 0:
374 find_cpp ();
375 putarg (0, CPP);
376 putarg (1, CPPFLAGS);
377 addarg (define);
378 if (infile)
379 addarg (infile);
380 addarg ((char *) NULL);
381 close (1);
382 dup2 (pd[1], 1);
383 close (pd[0]);
384 execv (arglist[0], (char **) arglist);
385 perror ("execv");
386 exit (1);
387 case -1:
388 perror ("fork");
389 exit (1);
391 close (pd[1]);
392 fin = fdopen (pd[0], "r");
393 if (fin == NULL)
395 fprintf (stderr, "%s: ", cmdname);
396 perror (infilename);
397 crash ();
401 /* Close the connection to the C-preprocessor and check for successfull
402 termination. */
403 static void
404 close_input (void)
406 int status;
408 fclose (fin);
409 /* Check the termination status. */
410 if (waitpid (cpp_pid, &status, 0) < 0)
412 perror ("waitpid");
413 crash ();
415 if (WIFSIGNALED (status) || WEXITSTATUS (status) != 0)
417 if (WIFSIGNALED (status))
418 fprintf (stderr, _("%s: C preprocessor failed with signal %d\n"),
419 cmdname, WTERMSIG (status));
420 else
421 fprintf (stderr, _("%s: C preprocessor failed with exit code %d\n"),
422 cmdname, WEXITSTATUS (status));
423 crash ();
427 /* valid tirpc nettypes */
428 static const char *valid_ti_nettypes[] =
430 "netpath",
431 "visible",
432 "circuit_v",
433 "datagram_v",
434 "circuit_n",
435 "datagram_n",
436 "udp",
437 "tcp",
438 "raw",
439 NULL
442 /* valid inetd nettypes */
443 static const char *valid_i_nettypes[] =
445 "udp",
446 "tcp",
447 NULL
450 static int
451 check_nettype (const char *name, const char *list_to_check[])
453 int i;
454 for (i = 0; list_to_check[i] != NULL; i++)
456 if (strcmp (name, list_to_check[i]) == 0)
458 return 1;
461 fprintf (stderr, _ ("illegal nettype :`%s'\n"), name);
462 return 0;
466 * Compile into an XDR routine output file
469 static void
470 c_output (const char *infile, const char *define, int extend,
471 const char *outfile)
473 definition *def;
474 char *include;
475 const char *outfilename;
476 long tell;
478 c_initialize ();
479 open_input (infile, define);
480 outfilename = extend ? extendfile (infile, outfile) : outfile;
481 open_output (infile, outfilename);
482 add_warning ();
483 if (infile && (include = extendfile (infile, ".h")))
485 fprintf (fout, "#include \"%s\"\n", include);
486 free (include);
487 /* .h file already contains rpc/rpc.h */
489 else
490 fprintf (fout, "#include <rpc/rpc.h>\n");
491 tell = ftell (fout);
492 while ((def = get_definition ()) != NULL)
493 emit (def);
495 if (extend && tell == ftell (fout))
496 unlink (outfilename);
497 close_input ();
498 close_output (outfilename);
501 void
502 c_initialize (void)
505 /* add all the starting basic types */
507 add_type (1, "int");
508 add_type (1, "long");
509 add_type (1, "short");
510 add_type (1, "bool");
512 add_type (1, "u_int");
513 add_type (1, "u_long");
514 add_type (1, "u_short");
518 char rpcgen_table_dcl[] = "struct rpcgen_table {\n\
519 char *(*proc)();\n\
520 xdrproc_t xdr_arg;\n\
521 unsigned len_arg;\n\
522 xdrproc_t xdr_res;\n\
523 unsigned len_res;\n\
524 };\n";
527 static char *
528 generate_guard (const char *pathname)
530 const char *filename;
531 char *guard, *tmp;
533 filename = strrchr (pathname, '/'); /* find last component */
534 filename = ((filename == NULL) ? pathname : filename + 1);
535 guard = strdup (filename);
536 /* convert to upper case */
537 tmp = guard;
538 while (*tmp)
540 if (islower (*tmp))
541 *tmp = toupper (*tmp);
542 tmp++;
545 guard = extendfile (guard, "_H_RPCGEN");
546 return guard;
550 * Compile into an XDR header file
554 static void
555 h_output (const char *infile, const char *define, int extend,
556 const char *outfile)
558 xdrfunc *xdrfuncp;
559 definition *def;
560 const char *ifilename;
561 const char *outfilename;
562 long tell;
563 char *guard;
564 list *l;
566 open_input (infile, define);
567 outfilename = extend ? extendfile (infile, outfile) : outfile;
568 open_output (infile, outfilename);
569 add_warning ();
570 ifilename = (infile == NULL) ? "STDIN" : infile;
571 guard = generate_guard (outfilename ? outfilename : ifilename);
573 fprintf (fout, "#ifndef _%s\n#define _%s\n\n", guard,
574 guard);
576 fprintf (fout, "#include <rpc/rpc.h>\n\n");
578 if (mtflag)
580 fprintf (fout, "#include <pthread.h>\n");
583 /* put the C++ support */
584 if (Cflag && !CCflag)
586 fprintf (fout, "\n#ifdef __cplusplus\n");
587 fprintf (fout, "extern \"C\" {\n");
588 fprintf (fout, "#endif\n\n");
591 tell = ftell (fout);
592 /* print data definitions */
593 while ((def = get_definition ()) != NULL)
595 print_datadef (def);
598 /* print function declarations.
599 Do this after data definitions because they might be used as
600 arguments for functions */
601 for (l = defined; l != NULL; l = l->next)
603 print_funcdef (l->val);
605 /* Now print all xdr func declarations */
606 if (xdrfunc_head != NULL)
608 fprintf (fout, "\n/* the xdr functions */\n");
609 if (CCflag)
611 fprintf (fout, "\n#ifdef __cplusplus\n");
612 fprintf (fout, "extern \"C\" {\n");
613 fprintf (fout, "#endif\n");
615 if (!Cflag)
617 xdrfuncp = xdrfunc_head;
618 while (xdrfuncp != NULL)
620 print_xdr_func_def (xdrfuncp->name,
621 xdrfuncp->pointerp, 2);
622 xdrfuncp = xdrfuncp->next;
625 else
627 int i;
629 for (i = 1; i < 3; ++i)
631 if (i == 1)
632 fprintf (fout, "\n#if defined(__STDC__) || defined(__cplusplus)\n");
633 else
634 fprintf (fout, "\n#else /* K&R C */\n");
636 xdrfuncp = xdrfunc_head;
637 while (xdrfuncp != NULL)
639 print_xdr_func_def (xdrfuncp->name,
640 xdrfuncp->pointerp, i);
641 xdrfuncp = xdrfuncp->next;
644 fprintf (fout, "\n#endif /* K&R C */\n");
648 if (extend && tell == ftell (fout))
650 unlink (outfilename);
652 else if (tblflag)
654 fprintf (fout, rpcgen_table_dcl);
657 if (Cflag)
659 fprintf (fout, "\n#ifdef __cplusplus\n");
660 fprintf (fout, "}\n");
661 fprintf (fout, "#endif\n");
664 fprintf (fout, "\n#endif /* !_%s */\n", guard);
665 close_input ();
666 close_output (outfilename);
670 * Compile into an RPC service
672 static void
673 s_output (int argc, const char *argv[], const char *infile, const char *define,
674 int extend, const char *outfile, int nomain, int netflag)
676 char *include;
677 definition *def;
678 int foundprogram = 0;
679 const char *outfilename;
681 open_input (infile, define);
682 outfilename = extend ? extendfile (infile, outfile) : outfile;
683 open_output (infile, outfilename);
684 add_warning ();
685 if (infile && (include = extendfile (infile, ".h")))
687 fprintf (fout, "#include \"%s\"\n", include);
688 free (include);
690 else
691 fprintf (fout, "#include <rpc/rpc.h>\n");
693 fprintf (fout, "#include <stdio.h>\n");
694 fprintf (fout, "#include <stdlib.h>\n");
695 if (Cflag)
697 fprintf (fout, "#include <rpc/pmap_clnt.h>\n");
698 fprintf (fout, "#include <string.h>\n");
700 if (strcmp (svcclosetime, "-1") == 0)
701 indefinitewait = 1;
702 else if (strcmp (svcclosetime, "0") == 0)
703 exitnow = 1;
704 else if (inetdflag || pmflag)
706 fprintf (fout, "#include <signal.h>\n");
707 timerflag = 1;
710 if (!tirpcflag && inetdflag)
711 #ifdef __GNU_LIBRARY__
712 fprintf (fout, "#include <sys/ioctl.h> /* ioctl, TIOCNOTTY */\n");
713 #else
714 fprintf (fout, "#include <sys/ttycom.h>/* TIOCNOTTY */\n");
715 #endif
716 if (Cflag && (inetdflag || pmflag))
718 #ifdef __GNU_LIBRARY__
719 fprintf (fout, "#include <sys/types.h> /* open */\n");
720 fprintf (fout, "#include <sys/stat.h> /* open */\n");
721 fprintf (fout, "#include <fcntl.h> /* open */\n");
722 fprintf (fout, "#include <unistd.h> /* getdtablesize */\n");
723 #else
724 fprintf (fout, "#ifdef __cplusplus\n");
725 fprintf (fout, "#include <sysent.h> /* getdtablesize, open */\n");
726 fprintf (fout, "#endif /* __cplusplus */\n");
727 if (tirpcflag)
728 fprintf (fout, "#include <unistd.h> /* setsid */\n");
729 #endif
731 #ifdef __GNU_LIBRARY__
732 if (tirpcflag && !(Cflag && (inetdflag || pmflag)))
733 #else
734 if (tirpcflag)
735 #endif
736 fprintf (fout, "#include <sys/types.h>\n");
738 fprintf (fout, "#include <memory.h>\n");
739 #ifndef __GNU_LIBRARY__
740 fprintf (fout, "#include <stropts.h>\n");
741 #endif
742 if (inetdflag || !tirpcflag)
744 fprintf (fout, "#include <sys/socket.h>\n");
745 fprintf (fout, "#include <netinet/in.h>\n");
748 if ((netflag || pmflag) && tirpcflag && !nomain)
750 fprintf (fout, "#include <netconfig.h>\n");
752 if ( /*timerflag && */ tirpcflag)
753 fprintf (fout, "#include <sys/resource.h> /* rlimit */\n");
754 if (logflag || inetdflag || pmflag)
756 #ifdef __GNU_LIBRARY__
757 fprintf (fout, "#include <syslog.h>\n");
758 #else
759 fprintf (fout, "#ifdef SYSLOG\n");
760 fprintf (fout, "#include <syslog.h>\n");
761 fprintf (fout, "#else\n");
762 fprintf (fout, "#define LOG_ERR 1\n");
763 fprintf (fout, "#define openlog(a, b, c)\n");
764 fprintf (fout, "#endif\n");
765 #endif
768 /* for ANSI-C */
769 if (Cflag)
770 fprintf (fout, "\n#ifndef SIG_PF\n#define SIG_PF void(*)(int)\n#endif\n");
772 #ifndef __GNU_LIBRARY__
773 fprintf (fout, "\n#ifdef DEBUG\n#define RPC_SVC_FG\n#endif\n");
774 #endif
775 if (timerflag)
776 fprintf (fout, "\n#define _RPCSVC_CLOSEDOWN %s\n", svcclosetime);
777 while ((def = get_definition ()) != NULL)
779 foundprogram |= (def->def_kind == DEF_PROGRAM);
781 if (extend && !foundprogram)
783 unlink (outfilename);
784 return;
786 write_most (infile, netflag, nomain);
787 if (!nomain)
789 if (!do_registers (argc, argv))
791 if (outfilename)
792 unlink (outfilename);
793 usage ();
795 write_rest ();
797 close_input ();
798 close_output (outfilename);
802 * generate client side stubs
804 static void
805 l_output (const char *infile, const char *define, int extend,
806 const char *outfile)
808 char *include;
809 definition *def;
810 int foundprogram = 0;
811 const char *outfilename;
813 open_input (infile, define);
814 outfilename = extend ? extendfile (infile, outfile) : outfile;
815 open_output (infile, outfilename);
816 add_warning ();
817 if (Cflag)
818 fprintf (fout, "#include <memory.h> /* for memset */\n");
819 if (infile && (include = extendfile (infile, ".h")))
821 fprintf (fout, "#include \"%s\"\n", include);
822 free (include);
824 else
825 fprintf (fout, "#include <rpc/rpc.h>\n");
826 while ((def = get_definition ()) != NULL)
828 foundprogram |= (def->def_kind == DEF_PROGRAM);
830 if (extend && !foundprogram)
832 unlink (outfilename);
833 return;
835 write_stubs ();
836 close_input ();
837 close_output (outfilename);
841 * generate the dispatch table
843 static void
844 t_output (const char *infile, const char *define, int extend,
845 const char *outfile)
847 definition *def;
848 int foundprogram = 0;
849 const char *outfilename;
851 open_input (infile, define);
852 outfilename = extend ? extendfile (infile, outfile) : outfile;
853 open_output (infile, outfilename);
854 add_warning ();
855 while ((def = get_definition ()) != NULL)
857 foundprogram |= (def->def_kind == DEF_PROGRAM);
859 if (extend && !foundprogram)
861 unlink (outfilename);
862 return;
864 write_tables ();
865 close_input ();
866 close_output (outfilename);
869 /* sample routine for the server template */
870 static void
871 svc_output (const char *infile, const char *define, int extend,
872 const char *outfile)
874 definition *def;
875 char *include;
876 const char *outfilename;
877 long tell;
879 open_input (infile, define);
880 outfilename = extend ? extendfile (infile, outfile) : outfile;
881 checkfiles (infile, outfilename);
882 /*check if outfile already exists.
883 if so, print an error message and exit */
884 open_output (infile, outfilename);
885 add_sample_msg ();
887 if (infile && (include = extendfile (infile, ".h")))
889 fprintf (fout, "#include \"%s\"\n", include);
890 free (include);
892 else
893 fprintf (fout, "#include <rpc/rpc.h>\n");
895 tell = ftell (fout);
896 while ((def = get_definition ()) != NULL)
898 write_sample_svc (def);
900 if (extend && tell == ftell (fout))
902 unlink (outfilename);
904 close_input ();
905 close_output (outfilename);
909 /* sample main routine for client */
910 static void
911 clnt_output (const char *infile, const char *define, int extend,
912 const char *outfile)
914 definition *def;
915 char *include;
916 const char *outfilename;
917 long tell;
918 int has_program = 0;
920 open_input (infile, define);
921 outfilename = extend ? extendfile (infile, outfile) : outfile;
922 checkfiles (infile, outfilename);
923 /*check if outfile already exists.
924 if so, print an error message and exit */
926 open_output (infile, outfilename);
927 add_sample_msg ();
928 if (infile && (include = extendfile (infile, ".h")))
930 fprintf (fout, "#include \"%s\"\n", include);
931 free (include);
933 else
934 fprintf (fout, "#include <rpc/rpc.h>\n");
935 tell = ftell (fout);
936 while ((def = get_definition ()) != NULL)
938 has_program += write_sample_clnt (def);
941 if (has_program)
942 write_sample_clnt_main ();
944 if (extend && tell == ftell (fout))
946 unlink (outfilename);
948 close_input ();
949 close_output (outfilename);
952 static char *
953 file_name (const char *file, const char *ext)
955 char *temp;
956 temp = extendfile (file, ext);
958 if (access (temp, F_OK) != -1)
959 return (temp);
960 else
961 return ((char *) " ");
964 static void
965 mkfile_output (struct commandline *cmd)
967 char *mkfilename;
968 const char *clientname, *clntname, *xdrname, *hdrname;
969 const char *servername, *svcname, *servprogname, *clntprogname;
971 svcname = file_name (cmd->infile, "_svc.c");
972 clntname = file_name (cmd->infile, "_clnt.c");
973 xdrname = file_name (cmd->infile, "_xdr.c");
974 hdrname = file_name (cmd->infile, ".h");
976 if (allfiles)
978 servername = extendfile (cmd->infile, "_server.c");
979 clientname = extendfile (cmd->infile, "_client.c");
981 else
983 servername = " ";
984 clientname = " ";
986 servprogname = extendfile (cmd->infile, "_server");
987 clntprogname = extendfile (cmd->infile, "_client");
989 if (allfiles)
991 char *cp, *temp;
993 mkfilename = alloc (strlen ("Makefile.") + strlen (cmd->infile) + 1);
994 temp = rindex (cmd->infile, '.');
995 cp = stpcpy (mkfilename, "Makefile.");
996 strncpy (cp, cmd->infile, (temp - cmd->infile));
998 else
999 mkfilename = (char *) cmd->outfile;
1001 checkfiles (NULL, mkfilename);
1002 open_output (NULL, mkfilename);
1004 fprintf (fout, "\n# This is a template Makefile generated by rpcgen\n");
1006 f_print (fout, "\n# Parameters\n\n");
1008 f_print (fout, "CLIENT = %s\nSERVER = %s\n\n", clntprogname, servprogname);
1009 f_print (fout, "SOURCES_CLNT.c = \nSOURCES_CLNT.h = \n");
1010 f_print (fout, "SOURCES_SVC.c = \nSOURCES_SVC.h = \n");
1011 f_print (fout, "SOURCES.x = %s\n\n", cmd->infile);
1012 f_print (fout, "TARGETS_SVC.c = %s %s %s \n",
1013 svcname, servername, xdrname);
1014 f_print (fout, "TARGETS_CLNT.c = %s %s %s \n",
1015 clntname, clientname, xdrname);
1016 f_print (fout, "TARGETS = %s %s %s %s %s %s\n\n",
1017 hdrname, xdrname, clntname,
1018 svcname, clientname, servername);
1020 f_print (fout, "OBJECTS_CLNT = $(SOURCES_CLNT.c:%%.c=%%.o) \
1021 $(TARGETS_CLNT.c:%%.c=%%.o)");
1023 f_print (fout, "\nOBJECTS_SVC = $(SOURCES_SVC.c:%%.c=%%.o) \
1024 $(TARGETS_SVC.c:%%.c=%%.o)");
1026 f_print (fout, "\n# Compiler flags \n");
1027 if (mtflag)
1028 fprintf (fout, "\nCPPFLAGS += -D_REENTRANT\nCFLAGS += -g \nLDLIBS \
1029 += -lnsl -lpthread \n ");
1030 else
1031 f_print (fout, "\nCFLAGS += -g \nLDLIBS += -lnsl\n");
1032 f_print (fout, "RPCGENFLAGS = \n");
1034 f_print (fout, "\n# Targets \n\n");
1036 f_print (fout, "all : $(CLIENT) $(SERVER)\n\n");
1037 f_print (fout, "$(TARGETS) : $(SOURCES.x) \n");
1038 f_print (fout, "\trpcgen $(RPCGENFLAGS) $(SOURCES.x)\n\n");
1039 f_print (fout, "$(OBJECTS_CLNT) : $(SOURCES_CLNT.c) $(SOURCES_CLNT.h) \
1040 $(TARGETS_CLNT.c) \n\n");
1042 f_print (fout, "$(OBJECTS_SVC) : $(SOURCES_SVC.c) $(SOURCES_SVC.h) \
1043 $(TARGETS_SVC.c) \n\n");
1044 f_print (fout, "$(CLIENT) : $(OBJECTS_CLNT) \n");
1045 f_print (fout, "\t$(LINK.c) -o $(CLIENT) $(OBJECTS_CLNT) \
1046 $(LDLIBS) \n\n");
1047 f_print (fout, "$(SERVER) : $(OBJECTS_SVC) \n");
1048 f_print (fout, "\t$(LINK.c) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS)\n\n ");
1049 f_print (fout, "clean:\n\t $(RM) core $(TARGETS) $(OBJECTS_CLNT) \
1050 $(OBJECTS_SVC) $(CLIENT) $(SERVER)\n\n");
1051 close_output (mkfilename);
1055 * Perform registrations for service output
1056 * Return 0 if failed; 1 otherwise.
1058 static int
1059 do_registers (int argc, const char *argv[])
1061 int i;
1063 if (inetdflag || !tirpcflag)
1065 for (i = 1; i < argc; i++)
1067 if (streq (argv[i], "-s"))
1069 if (!check_nettype (argv[i + 1], valid_i_nettypes))
1070 return 0;
1071 write_inetd_register (argv[i + 1]);
1072 i++;
1076 else
1078 for (i = 1; i < argc; i++)
1079 if (streq (argv[i], "-s"))
1081 if (!check_nettype (argv[i + 1], valid_ti_nettypes))
1082 return 0;
1083 write_nettype_register (argv[i + 1]);
1084 i++;
1086 else if (streq (argv[i], "-n"))
1088 write_netid_register (argv[i + 1]);
1089 i++;
1092 return 1;
1096 * Add another argument to the arg list
1098 static void
1099 addarg (const char *cp)
1101 if (argcount >= ARGLISTLEN)
1103 fprintf (stderr, _("rpcgen: too many defines\n"));
1104 crash ();
1105 /*NOTREACHED */
1107 arglist[argcount++] = cp;
1110 static void
1111 putarg (int whereto, const char *cp)
1113 if (whereto >= ARGLISTLEN)
1115 fprintf (stderr, _("rpcgen: arglist coding error\n"));
1116 crash ();
1117 /*NOTREACHED */
1119 arglist[whereto] = cp;
1123 * if input file is stdin and an output file is specified then complain
1124 * if the file already exists. Otherwise the file may get overwritten
1125 * If input file does not exist, exit with an error
1128 static void
1129 checkfiles (const char *infile, const char *outfile)
1131 struct stat buf;
1133 if (infile) /* infile ! = NULL */
1134 if (stat (infile, &buf) < 0)
1136 perror (infile);
1137 crash ();
1139 if (outfile)
1141 if (stat (outfile, &buf) < 0)
1142 return; /* file does not exist */
1143 else
1145 fprintf (stderr,
1146 _("file `%s' already exists and may be overwritten\n"),
1147 outfile);
1148 crash ();
1154 * Parse command line arguments
1156 static int
1157 parseargs (int argc, const char *argv[], struct commandline *cmd)
1159 int i;
1160 int j;
1161 int c;
1162 char flag[(1 << 8 * sizeof (char))];
1163 int nflags;
1165 cmdname = argv[0];
1166 cmd->infile = cmd->outfile = NULL;
1167 if (argc < 2)
1169 return (0);
1171 allfiles = 0;
1172 flag['c'] = 0;
1173 flag['h'] = 0;
1174 flag['l'] = 0;
1175 flag['m'] = 0;
1176 flag['o'] = 0;
1177 flag['s'] = 0;
1178 flag['n'] = 0;
1179 flag['t'] = 0;
1180 flag['S'] = 0;
1181 flag['C'] = 0;
1182 flag['M'] = 0;
1184 for (i = 1; i < argc; i++)
1186 if (argv[i][0] != '-')
1188 if (cmd->infile)
1190 fprintf (stderr,
1191 _("Cannot specify more than one input file!\n"));
1192 return 0;
1194 cmd->infile = argv[i];
1196 else
1198 for (j = 1; argv[i][j] != 0; j++)
1200 c = argv[i][j];
1201 switch (c)
1203 case 'a':
1204 allfiles = 1;
1205 break;
1206 case 'c':
1207 case 'h':
1208 case 'l':
1209 case 'm':
1210 case 't':
1211 if (flag[c])
1212 return 0;
1213 flag[c] = 1;
1214 break;
1215 case 'S':
1216 /* sample flag: Ss or Sc.
1217 Ss means set flag['S'];
1218 Sc means set flag['C'];
1219 Sm means set flag['M']; */
1220 c = argv[i][++j]; /* get next char */
1221 if (c == 's')
1222 c = 'S';
1223 else if (c == 'c')
1224 c = 'C';
1225 else if (c == 'm')
1226 c = 'M';
1227 else
1228 return 0;
1230 if (flag[c])
1231 return 0;
1232 flag[c] = 1;
1233 break;
1234 case 'C': /* ANSI C syntax */
1235 Cflag = 1;
1236 break;
1238 #ifdef __GNU_LIBRARY__
1239 case 'k': /* K&R C syntax */
1240 Cflag = 0;
1241 break;
1243 #endif
1244 case 'b': /* turn TIRPC flag off for
1245 generating backward compatible
1247 tirpcflag = 0;
1248 break;
1250 #ifdef __GNU_LIBRARY__
1251 case '5': /* turn TIRPC flag on for
1252 generating SysVr4 compatible
1254 tirpcflag = 1;
1255 break;
1256 #endif
1257 case 'I':
1258 inetdflag = 1;
1259 break;
1260 case 'N':
1261 newstyle = 1;
1262 break;
1263 case 'L':
1264 logflag = 1;
1265 break;
1266 case 'K':
1267 if (++i == argc)
1269 return (0);
1271 svcclosetime = argv[i];
1272 goto nextarg;
1273 case 'T':
1274 tblflag = 1;
1275 break;
1276 case 'M':
1277 mtflag = 1;
1278 break;
1279 case 'i':
1280 if (++i == argc)
1282 return (0);
1284 inlineflag = atoi (argv[i]);
1285 goto nextarg;
1286 case 'n':
1287 case 'o':
1288 case 's':
1289 if (argv[i][j - 1] != '-' ||
1290 argv[i][j + 1] != 0)
1292 return (0);
1294 flag[c] = 1;
1295 if (++i == argc)
1297 return (0);
1299 if (c == 's')
1301 if (!streq (argv[i], "udp") &&
1302 !streq (argv[i], "tcp"))
1303 return 0;
1305 else if (c == 'o')
1307 if (cmd->outfile)
1308 return 0;
1309 cmd->outfile = argv[i];
1311 goto nextarg;
1312 case 'D':
1313 if (argv[i][j - 1] != '-')
1314 return 0;
1315 addarg (argv[i]);
1316 goto nextarg;
1317 case 'Y':
1318 if (++i == argc)
1319 return 0;
1321 size_t len = strlen (argv[i]);
1322 pathbuf = malloc (len + 5);
1323 if (pathbuf == NULL)
1325 perror (cmdname);
1326 crash ();
1328 stpcpy (stpcpy (pathbuf,
1329 argv[i]),
1330 "/cpp");
1331 CPP = pathbuf;
1332 cppDefined = 1;
1333 goto nextarg;
1336 default:
1337 return 0;
1340 nextarg:
1345 cmd->cflag = flag['c'];
1346 cmd->hflag = flag['h'];
1347 cmd->lflag = flag['l'];
1348 cmd->mflag = flag['m'];
1349 cmd->nflag = flag['n'];
1350 cmd->sflag = flag['s'];
1351 cmd->tflag = flag['t'];
1352 cmd->Ssflag = flag['S'];
1353 cmd->Scflag = flag['C'];
1354 cmd->makefileflag = flag['M'];
1356 if (tirpcflag)
1358 pmflag = inetdflag ? 0 : 1; /* pmflag or inetdflag is always TRUE */
1359 if ((inetdflag && cmd->nflag))
1360 { /* netid not allowed with inetdflag */
1361 fprintf (stderr, _("Cannot use netid flag with inetd flag!\n"));
1362 return 0;
1365 else
1366 { /* 4.1 mode */
1367 pmflag = 0; /* set pmflag only in tirpcmode */
1368 #ifndef __GNU_LIBRARY__
1369 inetdflag = 1; /* inetdflag is TRUE by default */
1370 #endif
1371 if (cmd->nflag)
1372 { /* netid needs TIRPC */
1373 f_print (stderr, _("Cannot use netid flag without TIRPC!\n"));
1374 return (0);
1378 if (newstyle && (tblflag || cmd->tflag))
1380 f_print (stderr, _("Cannot use table flags with newstyle!\n"));
1381 return (0);
1384 /* check no conflicts with file generation flags */
1385 nflags = cmd->cflag + cmd->hflag + cmd->lflag + cmd->mflag +
1386 cmd->sflag + cmd->nflag + cmd->tflag + cmd->Ssflag + cmd->Scflag;
1388 if (nflags == 0)
1390 if (cmd->outfile != NULL || cmd->infile == NULL)
1392 return (0);
1395 else if (cmd->infile == NULL &&
1396 (cmd->Ssflag || cmd->Scflag || cmd->makefileflag))
1398 fprintf (stderr,
1399 _("\"infile\" is required for template generation flags.\n"));
1400 return 0;
1402 if (nflags > 1)
1404 fprintf (stderr, _("Cannot have more than one file generation flag!\n"));
1405 return 0;
1407 return 1;
1410 static void
1411 usage (void)
1413 fprintf (stderr, _("usage: %s infile\n"), cmdname);
1414 fprintf (stderr, _("\t%s [-abkCLNTM][-Dname[=value]] [-i size] \
1415 [-I [-K seconds]] [-Y path] infile\n"), cmdname);
1416 fprintf (stderr, _("\t%s [-c | -h | -l | -m | -t | -Sc | -Ss | -Sm] \
1417 [-o outfile] [infile]\n"), cmdname);
1418 fprintf (stderr, _("\t%s [-s nettype]* [-o outfile] [infile]\n"), cmdname);
1419 fprintf (stderr, _("\t%s [-n netid]* [-o outfile] [infile]\n"), cmdname);
1420 options_usage ();
1421 exit (1);
1424 static void
1425 options_usage (void)
1427 f_print (stderr, "options:\n");
1428 f_print (stderr, "-a\t\tgenerate all files, including samples\n");
1429 f_print (stderr, "-b\t\tbackward compatibility mode (generates code for SunOS 4.1)\n");
1430 f_print (stderr, "-c\t\tgenerate XDR routines\n");
1431 f_print (stderr, "-C\t\tANSI C mode\n");
1432 f_print (stderr, "-Dname[=value]\tdefine a symbol (same as #define)\n");
1433 f_print (stderr, "-h\t\tgenerate header file\n");
1434 f_print (stderr, "-i size\t\tsize at which to start generating inline code\n");
1435 f_print (stderr, "-I\t\tgenerate code for inetd support in server (for SunOS 4.1)\n");
1436 f_print (stderr, "-K seconds\tserver exits after K seconds of inactivity\n");
1437 f_print (stderr, "-l\t\tgenerate client side stubs\n");
1438 f_print (stderr, "-L\t\tserver errors will be printed to syslog\n");
1439 f_print (stderr, "-m\t\tgenerate server side stubs\n");
1440 f_print (stderr, "-M\t\tgenerate MT-safe code\n");
1441 f_print (stderr, "-n netid\tgenerate server code that supports named netid\n");
1442 f_print (stderr, "-N\t\tsupports multiple arguments and call-by-value\n");
1443 f_print (stderr, "-o outfile\tname of the output file\n");
1444 f_print (stderr, "-s nettype\tgenerate server code that supports named nettype\n");
1445 f_print (stderr, "-Sc\t\tgenerate sample client code that uses remote procedures\n");
1446 f_print (stderr, "-Ss\t\tgenerate sample server code that defines remote procedures\n");
1447 f_print (stderr, "-Sm \t\tgenerate makefile template \n");
1448 f_print (stderr, "-t\t\tgenerate RPC dispatch table\n");
1449 f_print (stderr, "-T\t\tgenerate code to support RPC dispatch tables\n");
1450 f_print (stderr, "-Y path\t\tdirectory name to find C preprocessor (cpp)\n");
1452 exit (1);