Update.
[glibc.git] / sunrpc / rpc_main.c
blob6cdef73d7989c5ac086831751e19390a255f10f1
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 "), cmdname);
295 perror (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: "), cmdname);
308 perror (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 NULL
443 /* valid inetd nettypes */
444 static const char *valid_i_nettypes[] =
446 "udp",
447 "tcp",
448 NULL
451 static int
452 check_nettype (const char *name, const char *list_to_check[])
454 int i;
455 for (i = 0; list_to_check[i] != NULL; i++)
457 if (strcmp (name, list_to_check[i]) == 0)
459 return 1;
462 fprintf (stderr, _ ("illegal nettype :\'%s\'\n"), name);
463 return 0;
467 * Compile into an XDR routine output file
470 static void
471 c_output (const char *infile, const char *define, int extend,
472 const char *outfile)
474 definition *def;
475 char *include;
476 const char *outfilename;
477 long tell;
479 c_initialize ();
480 open_input (infile, define);
481 outfilename = extend ? extendfile (infile, outfile) : outfile;
482 open_output (infile, outfilename);
483 add_warning ();
484 if (infile && (include = extendfile (infile, ".h")))
486 fprintf (fout, "#include \"%s\"\n", include);
487 free (include);
488 /* .h file already contains rpc/rpc.h */
490 else
491 fprintf (fout, "#include <rpc/rpc.h>\n");
492 tell = ftell (fout);
493 while ((def = get_definition ()) != NULL)
494 emit (def);
496 if (extend && tell == ftell (fout))
497 unlink (outfilename);
498 close_input ();
499 close_output (outfilename);
502 void
503 c_initialize (void)
506 /* add all the starting basic types */
508 add_type (1, "int");
509 add_type (1, "long");
510 add_type (1, "short");
511 add_type (1, "bool");
513 add_type (1, "u_int");
514 add_type (1, "u_long");
515 add_type (1, "u_short");
519 char rpcgen_table_dcl[] = "struct rpcgen_table {\n\
520 char *(*proc)();\n\
521 xdrproc_t xdr_arg;\n\
522 unsigned len_arg;\n\
523 xdrproc_t xdr_res;\n\
524 unsigned len_res;\n\
525 };\n";
528 static char *
529 generate_guard (const char *pathname)
531 const char *filename;
532 char *guard, *tmp;
534 filename = strrchr (pathname, '/'); /* find last component */
535 filename = ((filename == NULL) ? pathname : filename + 1);
536 guard = strdup (filename);
537 /* convert to upper case */
538 tmp = guard;
539 while (*tmp)
541 if (islower (*tmp))
542 *tmp = toupper (*tmp);
543 tmp++;
546 guard = extendfile (guard, "_H_RPCGEN");
547 return guard;
551 * Compile into an XDR header file
555 static void
556 h_output (const char *infile, const char *define, int extend,
557 const char *outfile)
559 xdrfunc *xdrfuncp;
560 definition *def;
561 const char *ifilename;
562 const char *outfilename;
563 long tell;
564 char *guard;
565 list *l;
567 open_input (infile, define);
568 outfilename = extend ? extendfile (infile, outfile) : outfile;
569 open_output (infile, outfilename);
570 add_warning ();
571 ifilename = (infile == NULL) ? "STDIN" : infile;
572 guard = generate_guard (outfilename ? outfilename : ifilename);
574 fprintf (fout, "#ifndef _%s\n#define _%s\n\n", guard,
575 guard);
577 fprintf (fout, "#include <rpc/rpc.h>\n\n");
579 if (mtflag)
581 fprintf (fout, "#include <pthread.h>\n");
584 /* put the C++ support */
585 if (Cflag && !CCflag)
587 fprintf (fout, "\n#ifdef __cplusplus\n");
588 fprintf (fout, "extern \"C\" {\n");
589 fprintf (fout, "#endif\n\n");
592 tell = ftell (fout);
593 /* print data definitions */
594 while ((def = get_definition ()) != NULL)
596 print_datadef (def);
599 /* print function declarations.
600 Do this after data definitions because they might be used as
601 arguments for functions */
602 for (l = defined; l != NULL; l = l->next)
604 print_funcdef (l->val);
606 /* Now print all xdr func declarations */
607 if (xdrfunc_head != NULL)
609 fprintf (fout, "\n/* the xdr functions */\n");
610 if (CCflag)
612 fprintf (fout, "\n#ifdef __cplusplus\n");
613 fprintf (fout, "extern \"C\" {\n");
614 fprintf (fout, "#endif\n");
616 if (!Cflag)
618 xdrfuncp = xdrfunc_head;
619 while (xdrfuncp != NULL)
621 print_xdr_func_def (xdrfuncp->name,
622 xdrfuncp->pointerp, 2);
623 xdrfuncp = xdrfuncp->next;
626 else
628 int i;
630 for (i = 1; i < 3; ++i)
632 if (i == 1)
633 fprintf (fout, "\n#if defined(__STDC__) || defined(__cplusplus)\n");
634 else
635 fprintf (fout, "\n#else /* K&R C */\n");
637 xdrfuncp = xdrfunc_head;
638 while (xdrfuncp != NULL)
640 print_xdr_func_def (xdrfuncp->name,
641 xdrfuncp->pointerp, i);
642 xdrfuncp = xdrfuncp->next;
645 fprintf (fout, "\n#endif /* K&R C */\n");
649 if (extend && tell == ftell (fout))
651 unlink (outfilename);
653 else if (tblflag)
655 fprintf (fout, rpcgen_table_dcl);
658 if (Cflag)
660 fprintf (fout, "\n#ifdef __cplusplus\n");
661 fprintf (fout, "}\n");
662 fprintf (fout, "#endif\n");
665 fprintf (fout, "\n#endif /* !_%s */\n", guard);
666 close_input ();
667 close_output (outfilename);
671 * Compile into an RPC service
673 static void
674 s_output (int argc, const char *argv[], const char *infile, const char *define,
675 int extend, const char *outfile, int nomain, int netflag)
677 char *include;
678 definition *def;
679 int foundprogram = 0;
680 const char *outfilename;
682 open_input (infile, define);
683 outfilename = extend ? extendfile (infile, outfile) : outfile;
684 open_output (infile, outfilename);
685 add_warning ();
686 if (infile && (include = extendfile (infile, ".h")))
688 fprintf (fout, "#include \"%s\"\n", include);
689 free (include);
691 else
692 fprintf (fout, "#include <rpc/rpc.h>\n");
694 fprintf (fout, "#include <stdio.h>\n");
695 fprintf (fout, "#include <stdlib.h>\n");
696 if (Cflag)
698 fprintf (fout, "#include <rpc/pmap_clnt.h>\n");
699 fprintf (fout, "#include <string.h>\n");
701 if (strcmp (svcclosetime, "-1") == 0)
702 indefinitewait = 1;
703 else if (strcmp (svcclosetime, "0") == 0)
704 exitnow = 1;
705 else if (inetdflag || pmflag)
707 fprintf (fout, "#include <signal.h>\n");
708 timerflag = 1;
711 if (!tirpcflag && inetdflag)
712 #ifdef __GNU_LIBRARY__
713 fprintf (fout, "#include <sys/ioctl.h> /* ioctl, TIOCNOTTY */\n");
714 #else
715 fprintf (fout, "#include <sys/ttycom.h>/* TIOCNOTTY */\n");
716 #endif
717 if (Cflag && (inetdflag || pmflag))
719 #ifdef __GNU_LIBRARY__
720 fprintf (fout, "#include <sys/types.h> /* open */\n");
721 fprintf (fout, "#include <sys/stat.h> /* open */\n");
722 fprintf (fout, "#include <fcntl.h> /* open */\n");
723 fprintf (fout, "#include <unistd.h> /* getdtablesize */\n");
724 #else
725 fprintf (fout, "#ifdef __cplusplus\n");
726 fprintf (fout, "#include <sysent.h> /* getdtablesize, open */\n");
727 fprintf (fout, "#endif /* __cplusplus */\n");
728 if (tirpcflag)
729 fprintf (fout, "#include <unistd.h> /* setsid */\n");
730 #endif
732 #ifdef __GNU_LIBRARY__
733 if (tirpcflag && !(Cflag && (inetdflag || pmflag)))
734 #else
735 if (tirpcflag)
736 #endif
737 fprintf (fout, "#include <sys/types.h>\n");
739 fprintf (fout, "#include <memory.h>\n");
740 #ifndef __GNU_LIBRARY__
741 fprintf (fout, "#include <stropts.h>\n");
742 #endif
743 if (inetdflag || !tirpcflag)
745 fprintf (fout, "#include <sys/socket.h>\n");
746 fprintf (fout, "#include <netinet/in.h>\n");
749 if ((netflag || pmflag) && tirpcflag && !nomain)
751 fprintf (fout, "#include <netconfig.h>\n");
753 if ( /*timerflag && */ tirpcflag)
754 fprintf (fout, "#include <sys/resource.h> /* rlimit */\n");
755 if (logflag || inetdflag || pmflag)
757 #ifdef __GNU_LIBRARY__
758 fprintf (fout, "#include <syslog.h>\n");
759 #else
760 fprintf (fout, "#ifdef SYSLOG\n");
761 fprintf (fout, "#include <syslog.h>\n");
762 fprintf (fout, "#else\n");
763 fprintf (fout, "#define LOG_ERR 1\n");
764 fprintf (fout, "#define openlog(a, b, c)\n");
765 fprintf (fout, "#endif\n");
766 #endif
769 /* for ANSI-C */
770 if (Cflag)
771 fprintf (fout, "\n#ifndef SIG_PF\n#define SIG_PF void(*)(int)\n#endif\n");
773 #ifndef __GNU_LIBRARY__
774 fprintf (fout, "\n#ifdef DEBUG\n#define RPC_SVC_FG\n#endif\n");
775 #endif
776 if (timerflag)
777 fprintf (fout, "\n#define _RPCSVC_CLOSEDOWN %s\n", svcclosetime);
778 while ((def = get_definition ()) != NULL)
780 foundprogram |= (def->def_kind == DEF_PROGRAM);
782 if (extend && !foundprogram)
784 unlink (outfilename);
785 return;
787 write_most (infile, netflag, nomain);
788 if (!nomain)
790 if (!do_registers (argc, argv))
792 if (outfilename)
793 unlink (outfilename);
794 usage ();
796 write_rest ();
798 close_input ();
799 close_output (outfilename);
803 * generate client side stubs
805 static void
806 l_output (const char *infile, const char *define, int extend,
807 const char *outfile)
809 char *include;
810 definition *def;
811 int foundprogram = 0;
812 const char *outfilename;
814 open_input (infile, define);
815 outfilename = extend ? extendfile (infile, outfile) : outfile;
816 open_output (infile, outfilename);
817 add_warning ();
818 if (Cflag)
819 fprintf (fout, "#include <memory.h> /* for memset */\n");
820 if (infile && (include = extendfile (infile, ".h")))
822 fprintf (fout, "#include \"%s\"\n", include);
823 free (include);
825 else
826 fprintf (fout, "#include <rpc/rpc.h>\n");
827 while ((def = get_definition ()) != NULL)
829 foundprogram |= (def->def_kind == DEF_PROGRAM);
831 if (extend && !foundprogram)
833 unlink (outfilename);
834 return;
836 write_stubs ();
837 close_input ();
838 close_output (outfilename);
842 * generate the dispatch table
844 static void
845 t_output (const char *infile, const char *define, int extend,
846 const char *outfile)
848 definition *def;
849 int foundprogram = 0;
850 const char *outfilename;
852 open_input (infile, define);
853 outfilename = extend ? extendfile (infile, outfile) : outfile;
854 open_output (infile, outfilename);
855 add_warning ();
856 while ((def = get_definition ()) != NULL)
858 foundprogram |= (def->def_kind == DEF_PROGRAM);
860 if (extend && !foundprogram)
862 unlink (outfilename);
863 return;
865 write_tables ();
866 close_input ();
867 close_output (outfilename);
870 /* sample routine for the server template */
871 static void
872 svc_output (const char *infile, const char *define, int extend,
873 const char *outfile)
875 definition *def;
876 char *include;
877 const char *outfilename;
878 long tell;
880 open_input (infile, define);
881 outfilename = extend ? extendfile (infile, outfile) : outfile;
882 checkfiles (infile, outfilename);
883 /*check if outfile already exists.
884 if so, print an error message and exit */
885 open_output (infile, outfilename);
886 add_sample_msg ();
888 if (infile && (include = extendfile (infile, ".h")))
890 fprintf (fout, "#include \"%s\"\n", include);
891 free (include);
893 else
894 fprintf (fout, "#include <rpc/rpc.h>\n");
896 tell = ftell (fout);
897 while ((def = get_definition ()) != NULL)
899 write_sample_svc (def);
901 if (extend && tell == ftell (fout))
903 unlink (outfilename);
905 close_input ();
906 close_output (outfilename);
910 /* sample main routine for client */
911 static void
912 clnt_output (const char *infile, const char *define, int extend,
913 const char *outfile)
915 definition *def;
916 char *include;
917 const char *outfilename;
918 long tell;
919 int has_program = 0;
921 open_input (infile, define);
922 outfilename = extend ? extendfile (infile, outfile) : outfile;
923 checkfiles (infile, outfilename);
924 /*check if outfile already exists.
925 if so, print an error message and exit */
927 open_output (infile, outfilename);
928 add_sample_msg ();
929 if (infile && (include = extendfile (infile, ".h")))
931 fprintf (fout, "#include \"%s\"\n", include);
932 free (include);
934 else
935 fprintf (fout, "#include <rpc/rpc.h>\n");
936 tell = ftell (fout);
937 while ((def = get_definition ()) != NULL)
939 has_program += write_sample_clnt (def);
942 if (has_program)
943 write_sample_clnt_main ();
945 if (extend && tell == ftell (fout))
947 unlink (outfilename);
949 close_input ();
950 close_output (outfilename);
953 static char *
954 file_name (const char *file, const char *ext)
956 char *temp;
957 temp = extendfile (file, ext);
959 if (access (temp, F_OK) != -1)
960 return (temp);
961 else
962 return ((char *) " ");
965 static void
966 mkfile_output (struct commandline *cmd)
968 char *mkfilename;
969 const char *clientname, *clntname, *xdrname, *hdrname;
970 const char *servername, *svcname, *servprogname, *clntprogname;
971 char *temp;
973 svcname = file_name (cmd->infile, "_svc.c");
974 clntname = file_name (cmd->infile, "_clnt.c");
975 xdrname = file_name (cmd->infile, "_xdr.c");
976 hdrname = file_name (cmd->infile, ".h");
978 if (allfiles)
980 servername = extendfile (cmd->infile, "_server.c");
981 clientname = extendfile (cmd->infile, "_client.c");
983 else
985 servername = " ";
986 clientname = " ";
988 servprogname = extendfile (cmd->infile, "_server");
989 clntprogname = extendfile (cmd->infile, "_client");
991 if (allfiles)
993 mkfilename = alloc (strlen ("Makefile.") +
994 strlen (cmd->infile) + 1);
995 temp = (char *) rindex (cmd->infile, '.');
996 strcat (mkfilename, "Makefile.");
997 strncat (mkfilename, cmd->infile,
998 (temp - cmd->infile));
1000 else
1001 mkfilename = (char *) cmd->outfile;
1003 checkfiles (NULL, mkfilename);
1004 open_output (NULL, mkfilename);
1006 fprintf (fout, "\n# This is a template Makefile generated by rpcgen\n");
1008 f_print (fout, "\n# Parameters\n\n");
1010 f_print (fout, "CLIENT = %s\nSERVER = %s\n\n", clntprogname, servprogname);
1011 f_print (fout, "SOURCES_CLNT.c = \nSOURCES_CLNT.h = \n");
1012 f_print (fout, "SOURCES_SVC.c = \nSOURCES_SVC.h = \n");
1013 f_print (fout, "SOURCES.x = %s\n\n", cmd->infile);
1014 f_print (fout, "TARGETS_SVC.c = %s %s %s \n",
1015 svcname, servername, xdrname);
1016 f_print (fout, "TARGETS_CLNT.c = %s %s %s \n",
1017 clntname, clientname, xdrname);
1018 f_print (fout, "TARGETS = %s %s %s %s %s %s\n\n",
1019 hdrname, xdrname, clntname,
1020 svcname, clientname, servername);
1022 f_print (fout, "OBJECTS_CLNT = $(SOURCES_CLNT.c:%%.c=%%.o) \
1023 $(TARGETS_CLNT.c:%%.c=%%.o)");
1025 f_print (fout, "\nOBJECTS_SVC = $(SOURCES_SVC.c:%%.c=%%.o) \
1026 $(TARGETS_SVC.c:%%.c=%%.o)");
1028 f_print (fout, "\n# Compiler flags \n");
1029 if (mtflag)
1030 fprintf (fout, "\nCPPFLAGS += -D_REENTRANT\nCFLAGS += -g \nLDLIBS \
1031 += -lnsl -lpthread \n ");
1032 else
1033 f_print (fout, "\nCFLAGS += -g \nLDLIBS += -lnsl\n");
1034 f_print (fout, "RPCGENFLAGS = \n");
1036 f_print (fout, "\n# Targets \n\n");
1038 f_print (fout, "all : $(CLIENT) $(SERVER)\n\n");
1039 f_print (fout, "$(TARGETS) : $(SOURCES.x) \n");
1040 f_print (fout, "\trpcgen $(RPCGENFLAGS) $(SOURCES.x)\n\n");
1041 f_print (fout, "$(OBJECTS_CLNT) : $(SOURCES_CLNT.c) $(SOURCES_CLNT.h) \
1042 $(TARGETS_CLNT.c) \n\n");
1044 f_print (fout, "$(OBJECTS_SVC) : $(SOURCES_SVC.c) $(SOURCES_SVC.h) \
1045 $(TARGETS_SVC.c) \n\n");
1046 f_print (fout, "$(CLIENT) : $(OBJECTS_CLNT) \n");
1047 f_print (fout, "\t$(LINK.c) -o $(CLIENT) $(OBJECTS_CLNT) \
1048 $(LDLIBS) \n\n");
1049 f_print (fout, "$(SERVER) : $(OBJECTS_SVC) \n");
1050 f_print (fout, "\t$(LINK.c) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS)\n\n ");
1051 f_print (fout, "clean:\n\t $(RM) core $(TARGETS) $(OBJECTS_CLNT) \
1052 $(OBJECTS_SVC) $(CLIENT) $(SERVER)\n\n");
1053 close_output (mkfilename);
1057 * Perform registrations for service output
1058 * Return 0 if failed; 1 otherwise.
1060 static int
1061 do_registers (int argc, const char *argv[])
1063 int i;
1065 if (inetdflag || !tirpcflag)
1067 for (i = 1; i < argc; i++)
1069 if (streq (argv[i], "-s"))
1071 if (!check_nettype (argv[i + 1], valid_i_nettypes))
1072 return 0;
1073 write_inetd_register (argv[i + 1]);
1074 i++;
1078 else
1080 for (i = 1; i < argc; i++)
1081 if (streq (argv[i], "-s"))
1083 if (!check_nettype (argv[i + 1], valid_ti_nettypes))
1084 return 0;
1085 write_nettype_register (argv[i + 1]);
1086 i++;
1088 else if (streq (argv[i], "-n"))
1090 write_netid_register (argv[i + 1]);
1091 i++;
1094 return 1;
1098 * Add another argument to the arg list
1100 static void
1101 addarg (const char *cp)
1103 if (argcount >= ARGLISTLEN)
1105 fprintf (stderr, _("rpcgen: too many defines\n"));
1106 crash ();
1107 /*NOTREACHED */
1109 arglist[argcount++] = cp;
1112 static void
1113 putarg (int whereto, const char *cp)
1115 if (whereto >= ARGLISTLEN)
1117 fprintf (stderr, _("rpcgen: arglist coding error\n"));
1118 crash ();
1119 /*NOTREACHED */
1121 arglist[whereto] = cp;
1125 * if input file is stdin and an output file is specified then complain
1126 * if the file already exists. Otherwise the file may get overwritten
1127 * If input file does not exist, exit with an error
1130 static void
1131 checkfiles (const char *infile, const char *outfile)
1133 struct stat buf;
1135 if (infile) /* infile ! = NULL */
1136 if (stat (infile, &buf) < 0)
1138 perror (infile);
1139 crash ();
1141 if (outfile)
1143 if (stat (outfile, &buf) < 0)
1144 return; /* file does not exist */
1145 else
1147 fprintf (stderr,
1148 _("file '%s' already exists and may be overwritten\n"),
1149 outfile);
1150 crash ();
1156 * Parse command line arguments
1158 static int
1159 parseargs (int argc, const char *argv[], struct commandline *cmd)
1161 int i;
1162 int j;
1163 int c;
1164 char flag[(1 << 8 * sizeof (char))];
1165 int nflags;
1167 cmdname = argv[0];
1168 cmd->infile = cmd->outfile = NULL;
1169 if (argc < 2)
1171 return (0);
1173 allfiles = 0;
1174 flag['c'] = 0;
1175 flag['h'] = 0;
1176 flag['l'] = 0;
1177 flag['m'] = 0;
1178 flag['o'] = 0;
1179 flag['s'] = 0;
1180 flag['n'] = 0;
1181 flag['t'] = 0;
1182 flag['S'] = 0;
1183 flag['C'] = 0;
1184 flag['M'] = 0;
1186 for (i = 1; i < argc; i++)
1188 if (argv[i][0] != '-')
1190 if (cmd->infile)
1192 fprintf (stderr,
1193 _("Cannot specify more than one input file!\n"));
1194 return 0;
1196 cmd->infile = argv[i];
1198 else
1200 for (j = 1; argv[i][j] != 0; j++)
1202 c = argv[i][j];
1203 switch (c)
1205 case 'a':
1206 allfiles = 1;
1207 break;
1208 case 'c':
1209 case 'h':
1210 case 'l':
1211 case 'm':
1212 case 't':
1213 if (flag[c])
1214 return 0;
1215 flag[c] = 1;
1216 break;
1217 case 'S':
1218 /* sample flag: Ss or Sc.
1219 Ss means set flag['S'];
1220 Sc means set flag['C'];
1221 Sm means set flag['M']; */
1222 c = argv[i][++j]; /* get next char */
1223 if (c == 's')
1224 c = 'S';
1225 else if (c == 'c')
1226 c = 'C';
1227 else if (c == 'm')
1228 c = 'M';
1229 else
1230 return 0;
1232 if (flag[c])
1233 return 0;
1234 flag[c] = 1;
1235 break;
1236 case 'C': /* ANSI C syntax */
1237 Cflag = 1;
1238 break;
1240 #ifdef __GNU_LIBRARY__
1241 case 'k': /* K&R C syntax */
1242 Cflag = 0;
1243 break;
1245 #endif
1246 case 'b': /* turn TIRPC flag off for
1247 generating backward compatible
1249 tirpcflag = 0;
1250 break;
1252 #ifdef __GNU_LIBRARY__
1253 case '5': /* turn TIRPC flag on for
1254 generating SysVr4 compatible
1256 tirpcflag = 1;
1257 break;
1258 #endif
1259 case 'I':
1260 inetdflag = 1;
1261 break;
1262 case 'N':
1263 newstyle = 1;
1264 break;
1265 case 'L':
1266 logflag = 1;
1267 break;
1268 case 'K':
1269 if (++i == argc)
1271 return (0);
1273 svcclosetime = argv[i];
1274 goto nextarg;
1275 case 'T':
1276 tblflag = 1;
1277 break;
1278 case 'M':
1279 mtflag = 1;
1280 break;
1281 case 'i':
1282 if (++i == argc)
1284 return (0);
1286 inlineflag = atoi (argv[i]);
1287 goto nextarg;
1288 case 'n':
1289 case 'o':
1290 case 's':
1291 if (argv[i][j - 1] != '-' ||
1292 argv[i][j + 1] != 0)
1294 return (0);
1296 flag[c] = 1;
1297 if (++i == argc)
1299 return (0);
1301 if (c == 's')
1303 if (!streq (argv[i], "udp") &&
1304 !streq (argv[i], "tcp"))
1305 return 0;
1307 else if (c == 'o')
1309 if (cmd->outfile)
1310 return 0;
1311 cmd->outfile = argv[i];
1313 goto nextarg;
1314 case 'D':
1315 if (argv[i][j - 1] != '-')
1316 return 0;
1317 addarg (argv[i]);
1318 goto nextarg;
1319 case 'Y':
1320 if (++i == argc)
1321 return 0;
1323 size_t len = strlen (argv[i]);
1324 pathbuf = malloc (len + 5);
1325 if (pathbuf == NULL)
1327 perror (cmdname);
1328 crash ();
1330 stpcpy (stpcpy (pathbuf,
1331 argv[i]),
1332 "/cpp");
1333 CPP = pathbuf;
1334 cppDefined = 1;
1335 goto nextarg;
1338 default:
1339 return 0;
1342 nextarg:
1347 cmd->cflag = flag['c'];
1348 cmd->hflag = flag['h'];
1349 cmd->lflag = flag['l'];
1350 cmd->mflag = flag['m'];
1351 cmd->nflag = flag['n'];
1352 cmd->sflag = flag['s'];
1353 cmd->tflag = flag['t'];
1354 cmd->Ssflag = flag['S'];
1355 cmd->Scflag = flag['C'];
1356 cmd->makefileflag = flag['M'];
1358 if (tirpcflag)
1360 pmflag = inetdflag ? 0 : 1; /* pmflag or inetdflag is always TRUE */
1361 if ((inetdflag && cmd->nflag))
1362 { /* netid not allowed with inetdflag */
1363 fprintf (stderr, _("Cannot use netid flag with inetd flag!\n"));
1364 return 0;
1367 else
1368 { /* 4.1 mode */
1369 pmflag = 0; /* set pmflag only in tirpcmode */
1370 #ifndef __GNU_LIBRARY__
1371 inetdflag = 1; /* inetdflag is TRUE by default */
1372 #endif
1373 if (cmd->nflag)
1374 { /* netid needs TIRPC */
1375 f_print (stderr, _("Cannot use netid flag without TIRPC!\n"));
1376 return (0);
1380 if (newstyle && (tblflag || cmd->tflag))
1382 f_print (stderr, _("Cannot use table flags with newstyle!\n"));
1383 return (0);
1386 /* check no conflicts with file generation flags */
1387 nflags = cmd->cflag + cmd->hflag + cmd->lflag + cmd->mflag +
1388 cmd->sflag + cmd->nflag + cmd->tflag + cmd->Ssflag + cmd->Scflag;
1390 if (nflags == 0)
1392 if (cmd->outfile != NULL || cmd->infile == NULL)
1394 return (0);
1397 else if (cmd->infile == NULL &&
1398 (cmd->Ssflag || cmd->Scflag || cmd->makefileflag))
1400 fprintf (stderr,
1401 _("\"infile\" is required for template generation flags.\n"));
1402 return 0;
1404 if (nflags > 1)
1406 fprintf (stderr, _("Cannot have more than one file generation flag!\n"));
1407 return 0;
1409 return 1;
1412 static void
1413 usage (void)
1415 fprintf (stderr, _("usage: %s infile\n"), cmdname);
1416 fprintf (stderr, _("\t%s [-abkCLNTM][-Dname[=value]] [-i size] \
1417 [-I [-K seconds]] [-Y path] infile\n"), cmdname);
1418 fprintf (stderr, _("\t%s [-c | -h | -l | -m | -t | -Sc | -Ss | -Sm] \
1419 [-o outfile] [infile]\n"), cmdname);
1420 fprintf (stderr, _("\t%s [-s nettype]* [-o outfile] [infile]\n"), cmdname);
1421 fprintf (stderr, _("\t%s [-n netid]* [-o outfile] [infile]\n"), cmdname);
1422 options_usage ();
1423 exit (1);
1426 static void
1427 options_usage (void)
1429 f_print (stderr, "options:\n");
1430 f_print (stderr, "-a\t\tgenerate all files, including samples\n");
1431 f_print (stderr, "-b\t\tbackward compatibility mode (generates code for SunOS 4.1)\n");
1432 f_print (stderr, "-c\t\tgenerate XDR routines\n");
1433 f_print (stderr, "-C\t\tANSI C mode\n");
1434 f_print (stderr, "-Dname[=value]\tdefine a symbol (same as #define)\n");
1435 f_print (stderr, "-h\t\tgenerate header file\n");
1436 f_print (stderr, "-i size\t\tsize at which to start generating inline code\n");
1437 f_print (stderr, "-I\t\tgenerate code for inetd support in server (for SunOS 4.1)\n");
1438 f_print (stderr, "-K seconds\tserver exits after K seconds of inactivity\n");
1439 f_print (stderr, "-l\t\tgenerate client side stubs\n");
1440 f_print (stderr, "-L\t\tserver errors will be printed to syslog\n");
1441 f_print (stderr, "-m\t\tgenerate server side stubs\n");
1442 f_print (stderr, "-M\t\tgenerate MT-safe code\n");
1443 f_print (stderr, "-n netid\tgenerate server code that supports named netid\n");
1444 f_print (stderr, "-N\t\tsupports multiple arguments and call-by-value\n");
1445 f_print (stderr, "-o outfile\tname of the output file\n");
1446 f_print (stderr, "-s nettype\tgenerate server code that supports named nettype\n");
1447 f_print (stderr, "-Sc\t\tgenerate sample client code that uses remote procedures\n");
1448 f_print (stderr, "-Ss\t\tgenerate sample server code that defines remote procedures\n");
1449 f_print (stderr, "-Sm \t\tgenerate makefile template \n");
1450 f_print (stderr, "-t\t\tgenerate RPC dispatch table\n");
1451 f_print (stderr, "-T\t\tgenerate code to support RPC dispatch tables\n");
1452 f_print (stderr, "-Y path\t\tdirectory name to find C preprocessor (cpp)\n");
1454 exit (1);