Update.
[glibc.git] / sunrpc / rpc_main.c
bloba5249e3df70b46f3dd574dc742838238735cf2a1
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 "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 = 0; /* explicit path for C preprocessor */
82 static const char *CPP = SUNOS_CPP;
83 static char CPPFLAGS[] = "-C";
84 static char *pathbuf;
85 static const char *allv[] =
87 "rpcgen", "-s", "udp", "-s", "tcp",
89 static int allc = sizeof (allv) / sizeof (allv[0]);
90 static const char *allnv[] =
92 "rpcgen", "-s", "netpath",
94 static int allnc = sizeof (allnv) / sizeof (allnv[0]);
97 * machinations for handling expanding argument list
99 static void addarg (const char *); /* add another argument to the list */
100 static void putarg (int, const char *); /* put argument at specified location */
101 static void clear_args (void); /* clear argument list */
102 static void checkfiles (const char *, const char *);
103 /* check if out file already exists */
105 static void clear_args (void);
106 static char *extendfile (const char *file, const char *ext);
107 static void open_output (const char *infile, const char *outfile);
108 static void add_warning (void);
109 static void clear_args (void);
110 static void find_cpp (void);
111 static void open_input (const char *infile, const char *define);
112 static int check_nettype (const char *name, const char *list_to_check[]);
113 static void c_output (const char *infile, const char *define,
114 int extend, const char *outfile);
115 static void h_output (const char *infile, const char *define,
116 int extend, const char *outfile);
117 static void s_output (int argc, const char *argv[], const char *infile,
118 const char *define, int extend,
119 const char *outfile, int nomain, int netflag);
120 static void l_output (const char *infile, const char *define,
121 int extend, const char *outfile);
122 static void t_output (const char *infile, const char *define,
123 int extend, const char *outfile);
124 static void svc_output (const char *infile, const char *define,
125 int extend, const char *outfile);
126 static void clnt_output (const char *infile, const char *define,
127 int extend, const char *outfile);
128 static void mkfile_output (struct commandline *cmd);
129 static int do_registers (int argc, const char *argv[]);
130 static void addarg (const char *cp);
131 static void putarg (int whereto, const char *cp);
132 static void checkfiles (const char *infile, const char *outfile);
133 static int parseargs (int argc, const char *argv[], struct commandline *cmd);
134 static void usage (void);
135 static void options_usage (void);
136 static void c_initialize (void);
137 static char *generate_guard (const char *pathname);
140 #define ARGLISTLEN 20
141 #define FIXEDARGS 2
143 static const char *arglist[ARGLISTLEN];
144 static int argcount = FIXEDARGS;
147 int nonfatalerrors; /* errors */
148 int inetdflag /* = 1 */ ; /* Support for inetd *//* is now the default */
149 int pmflag; /* Support for port monitors */
150 int logflag; /* Use syslog instead of fprintf for errors */
151 int tblflag; /* Support for dispatch table file */
152 int mtflag; /* Support for MT */
154 #define INLINE 3
155 /*length at which to start doing an inline */
157 int inlineflag = INLINE; /* length at which to start doing an inline. 3 = default
158 if 0, no xdr_inline code */
160 int indefinitewait; /* If started by port monitors, hang till it wants */
161 int exitnow; /* If started by port monitors, exit after the call */
162 int timerflag; /* TRUE if !indefinite && !exitnow */
163 int newstyle; /* newstyle of passing arguments (by value) */
164 #ifdef __GNU_LIBRARY__
165 int Cflag = 1; /* ANSI C syntax */
166 #else
167 int Cflag = 0; /* ANSI C/C++ syntax */
168 #endif
169 int CCflag = 0; /* C++ files */
170 static int allfiles; /* generate all files */
171 #ifdef __GNU_LIBRARY__
172 int tirpcflag = 0; /* generating code for tirpc, by default */
173 #else
174 int tirpcflag = 1; /* generating code for tirpc, by default */
175 #endif
176 xdrfunc *xdrfunc_head = NULL; /* xdr function list */
177 xdrfunc *xdrfunc_tail = NULL; /* xdr function list */
180 main (int argc, const char *argv[])
182 struct commandline cmd;
184 (void) memset ((char *) &cmd, 0, sizeof (struct commandline));
185 clear_args ();
186 if (!parseargs (argc, argv, &cmd))
187 usage ();
189 if (cmd.cflag || cmd.hflag || cmd.lflag || cmd.tflag || cmd.sflag ||
190 cmd.mflag || cmd.nflag || cmd.Ssflag || cmd.Scflag)
192 checkfiles (cmd.infile, cmd.outfile);
194 else
195 checkfiles (cmd.infile, NULL);
197 if (cmd.cflag)
198 c_output (cmd.infile, "-DRPC_XDR", DONT_EXTEND, cmd.outfile);
199 else if (cmd.hflag)
200 h_output (cmd.infile, "-DRPC_HDR", DONT_EXTEND, cmd.outfile);
201 else if (cmd.lflag)
202 l_output (cmd.infile, "-DRPC_CLNT", DONT_EXTEND, cmd.outfile);
203 else if (cmd.sflag || cmd.mflag || (cmd.nflag))
204 s_output (argc, argv, cmd.infile, "-DRPC_SVC", DONT_EXTEND,
205 cmd.outfile, cmd.mflag, cmd.nflag);
206 else if (cmd.tflag)
207 t_output (cmd.infile, "-DRPC_TBL", DONT_EXTEND, cmd.outfile);
208 else if (cmd.Ssflag)
209 svc_output (cmd.infile, "-DRPC_SERVER", DONT_EXTEND, cmd.outfile);
210 else if (cmd.Scflag)
211 clnt_output (cmd.infile, "-DRPC_CLIENT", DONT_EXTEND, cmd.outfile);
212 else if (cmd.makefileflag)
213 mkfile_output (&cmd);
214 else
216 /* the rescans are required, since cpp may effect input */
217 c_output (cmd.infile, "-DRPC_XDR", EXTEND, "_xdr.c");
218 reinitialize ();
219 h_output (cmd.infile, "-DRPC_HDR", EXTEND, ".h");
220 reinitialize ();
221 l_output (cmd.infile, "-DRPC_CLNT", EXTEND, "_clnt.c");
222 reinitialize ();
223 if (inetdflag || !tirpcflag)
224 s_output (allc, allv, cmd.infile, "-DRPC_SVC", EXTEND,
225 "_svc.c", cmd.mflag, cmd.nflag);
226 else
227 s_output (allnc, allnv, cmd.infile, "-DRPC_SVC",
228 EXTEND, "_svc.c", cmd.mflag, cmd.nflag);
229 if (tblflag)
231 reinitialize ();
232 t_output (cmd.infile, "-DRPC_TBL", EXTEND, "_tbl.i");
234 if (allfiles)
236 reinitialize ();
237 svc_output (cmd.infile, "-DRPC_SERVER", EXTEND, "_server.c");
238 reinitialize ();
239 clnt_output (cmd.infile, "-DRPC_CLIENT", EXTEND, "_client.c");
241 if (allfiles || (cmd.makefileflag == 1))
243 reinitialize ();
244 mkfile_output (&cmd);
247 exit (nonfatalerrors);
248 /* NOTREACHED */
252 * add extension to filename
254 static char *
255 extendfile (const char *file, const char *ext)
257 char *res;
258 const char *p;
260 res = alloc (strlen (file) + strlen (ext) + 1);
261 if (res == NULL)
262 abort ();
263 p = strrchr (file, '.');
264 if (p == NULL)
265 p = file + strlen (file);
266 strcpy (res, file);
267 strcpy (res + (p - file), ext);
268 return res;
272 * Open output file with given extension
274 static void
275 open_output (const char *infile, const char *outfile)
277 if (outfile == NULL)
279 fout = stdout;
280 return;
283 if (infile != NULL && streq (outfile, infile))
285 fprintf (stderr, _ ("%s: output would overwrite %s\n"), cmdname,
286 infile);
287 crash ();
289 fout = fopen (outfile, "w");
290 if (fout == NULL)
292 fprintf (stderr, _ ("%s: unable to open "), cmdname);
293 perror (outfile);
294 crash ();
296 record_open (outfile);
299 static void
300 add_warning (void)
302 fprintf (fout, "/*\n");
303 fprintf (fout, " * Please do not edit this file.\n");
304 fprintf (fout, " * It was generated using rpcgen.\n");
305 fprintf (fout, " */\n\n");
308 /* clear list of arguments */
309 static void
310 clear_args (void)
312 int i;
313 for (i = FIXEDARGS; i < ARGLISTLEN; ++i)
314 arglist[i] = NULL;
315 argcount = FIXEDARGS;
318 /* make sure that a CPP exists */
319 static void
320 find_cpp (void)
322 struct stat buf;
324 if (stat (CPP, &buf) < 0)
325 { /* /lib/cpp or explicit cpp does not exist */
326 if (cppDefined)
328 fprintf (stderr, _ ("cannot find C preprocessor: %s \n"), CPP);
329 crash ();
331 else
332 { /* try the other one */
333 CPP = SVR4_CPP;
334 if (stat (CPP, &buf) < 0)
335 { /* can't find any cpp */
336 fputs (_ ("cannot find any C preprocessor (cpp)\n"), stdout);
337 crash ();
344 * Open input file with given define for C-preprocessor
346 static void
347 open_input (const char *infile, const char *define)
349 int pd[2];
351 infilename = (infile == NULL) ? "<stdin>" : infile;
352 if (pipe (pd) != 0)
354 perror ("pipe");
355 exit (1);
357 switch (fork ())
359 case 0:
360 find_cpp ();
361 putarg (0, CPP);
362 putarg (1, CPPFLAGS);
363 addarg (define);
364 if (infile)
365 addarg (infile);
366 addarg ((char *) NULL);
367 close (1);
368 dup2 (pd[1], 1);
369 close (pd[0]);
370 execv (arglist[0], (char **) arglist);
371 perror ("execv");
372 exit (1);
373 case -1:
374 perror ("fork");
375 exit (1);
377 close (pd[1]);
378 fin = fdopen (pd[0], "r");
379 if (fin == NULL)
381 fprintf (stderr, "%s: ", cmdname);
382 perror (infilename);
383 crash ();
387 /* valid tirpc nettypes */
388 static const char *valid_ti_nettypes[] =
390 "netpath",
391 "visible",
392 "circuit_v",
393 "datagram_v",
394 "circuit_n",
395 "datagram_n",
396 "udp",
397 "tcp",
398 "raw",
399 NULL
402 /* valid inetd nettypes */
403 static const char *valid_i_nettypes[] =
405 "udp",
406 "tcp",
407 NULL
410 static int
411 check_nettype (const char *name, const char *list_to_check[])
413 int i;
414 for (i = 0; list_to_check[i] != NULL; i++)
416 if (strcmp (name, list_to_check[i]) == 0)
418 return 1;
421 fprintf (stderr, _ ("illegal nettype :\'%s\'\n"), name);
422 return 0;
426 * Compile into an XDR routine output file
429 static void
430 c_output (const char *infile, const char *define, int extend,
431 const char *outfile)
433 definition *def;
434 char *include;
435 const char *outfilename;
436 long tell;
438 c_initialize ();
439 open_input (infile, define);
440 outfilename = extend ? extendfile (infile, outfile) : outfile;
441 open_output (infile, outfilename);
442 add_warning ();
443 if (infile && (include = extendfile (infile, ".h")))
445 fprintf (fout, "#include \"%s\"\n", include);
446 free (include);
447 /* .h file already contains rpc/rpc.h */
449 else
450 fprintf (fout, "#include <rpc/rpc.h>\n");
451 tell = ftell (fout);
452 while ((def = get_definition ()) != NULL)
453 emit (def);
455 if (extend && tell == ftell (fout))
456 unlink (outfilename);
459 void
460 c_initialize (void)
463 /* add all the starting basic types */
465 add_type (1, "int");
466 add_type (1, "long");
467 add_type (1, "short");
468 add_type (1, "bool");
470 add_type (1, "u_int");
471 add_type (1, "u_long");
472 add_type (1, "u_short");
476 char rpcgen_table_dcl[] = "struct rpcgen_table {\n\
477 char *(*proc)();\n\
478 xdrproc_t xdr_arg;\n\
479 unsigned len_arg;\n\
480 xdrproc_t xdr_res;\n\
481 unsigned len_res;\n\
482 };\n";
485 static char *
486 generate_guard (const char *pathname)
488 const char *filename;
489 char *guard, *tmp;
491 filename = strrchr (pathname, '/'); /* find last component */
492 filename = ((filename == NULL) ? pathname : filename + 1);
493 guard = strdup (filename);
494 /* convert to upper case */
495 tmp = guard;
496 while (*tmp)
498 if (islower (*tmp))
499 *tmp = toupper (*tmp);
500 tmp++;
503 guard = extendfile (guard, "_H_RPCGEN");
504 return guard;
508 * Compile into an XDR header file
512 static void
513 h_output (const char *infile, const char *define, int extend,
514 const char *outfile)
516 xdrfunc *xdrfuncp;
517 definition *def;
518 const char *ifilename;
519 const char *outfilename;
520 long tell;
521 char *guard;
522 list *l;
524 open_input (infile, define);
525 outfilename = extend ? extendfile (infile, outfile) : outfile;
526 open_output (infile, outfilename);
527 add_warning ();
528 ifilename = (infile == NULL) ? "STDIN" : infile;
529 guard = generate_guard (outfilename ? outfilename : ifilename);
531 fprintf (fout, "#ifndef _%s\n#define _%s\n\n", guard,
532 guard);
534 fprintf (fout, "#include <rpc/rpc.h>\n\n");
536 if (mtflag)
538 fprintf (fout, "#include <pthread.h>\n");
541 /* put the C++ support */
542 if (Cflag && !CCflag)
544 fprintf (fout, "\n#ifdef __cplusplus\n");
545 fprintf (fout, "extern \"C\" {\n");
546 fprintf (fout, "#endif\n\n");
549 tell = ftell (fout);
550 /* print data definitions */
551 while ((def = get_definition ()) != NULL)
553 print_datadef (def);
556 /* print function declarations.
557 Do this after data definitions because they might be used as
558 arguments for functions */
559 for (l = defined; l != NULL; l = l->next)
561 print_funcdef (l->val);
563 /* Now print all xdr func declarations */
564 if (xdrfunc_head != NULL)
566 fprintf (fout, "\n/* the xdr functions */\n");
567 if (CCflag)
569 fprintf (fout, "\n#ifdef __cplusplus\n");
570 fprintf (fout, "extern \"C\" {\n");
571 fprintf (fout, "#endif\n");
573 if (!Cflag)
575 xdrfuncp = xdrfunc_head;
576 while (xdrfuncp != NULL)
578 print_xdr_func_def (xdrfuncp->name,
579 xdrfuncp->pointerp, 2);
580 xdrfuncp = xdrfuncp->next;
583 else
585 int i;
587 for (i = 1; i < 3; ++i)
589 if (i == 1)
590 fprintf (fout, "\n#if defined(__STDC__) || defined(__cplusplus)\n");
591 else
592 fprintf (fout, "\n#else /* K&R C */\n");
594 xdrfuncp = xdrfunc_head;
595 while (xdrfuncp != NULL)
597 print_xdr_func_def (xdrfuncp->name,
598 xdrfuncp->pointerp, i);
599 xdrfuncp = xdrfuncp->next;
602 fprintf (fout, "\n#endif /* K&R C */\n");
606 if (extend && tell == ftell (fout))
608 unlink (outfilename);
610 else if (tblflag)
612 fprintf (fout, rpcgen_table_dcl);
615 if (Cflag)
617 fprintf (fout, "\n#ifdef __cplusplus\n");
618 fprintf (fout, "}\n");
619 fprintf (fout, "#endif\n");
622 fprintf (fout, "\n#endif /* !_%s */\n", guard);
626 * Compile into an RPC service
628 static void
629 s_output (int argc, const char *argv[], const char *infile, const char *define,
630 int extend, const char *outfile, int nomain, int netflag)
632 char *include;
633 definition *def;
634 int foundprogram = 0;
635 const char *outfilename;
637 open_input (infile, define);
638 outfilename = extend ? extendfile (infile, outfile) : outfile;
639 open_output (infile, outfilename);
640 add_warning ();
641 if (infile && (include = extendfile (infile, ".h")))
643 fprintf (fout, "#include \"%s\"\n", include);
644 free (include);
646 else
647 fprintf (fout, "#include <rpc/rpc.h>\n");
649 fprintf (fout, "#include <stdio.h>\n");
650 fprintf (fout, "#include <stdlib.h>\n");
651 if (Cflag)
653 fprintf (fout, "#include <rpc/pmap_clnt.h>\n");
654 fprintf (fout, "#include <string.h>\n");
656 if (strcmp (svcclosetime, "-1") == 0)
657 indefinitewait = 1;
658 else if (strcmp (svcclosetime, "0") == 0)
659 exitnow = 1;
660 else if (inetdflag || pmflag)
662 fprintf (fout, "#include <signal.h>\n");
663 timerflag = 1;
666 if (!tirpcflag && inetdflag)
667 #ifdef __GNU_LIBRARY__
668 fprintf (fout, "#include <sys/ioctl.h> /* ioctl, TIOCNOTTY */\n");
669 #else
670 fprintf (fout, "#include <sys/ttycom.h>/* TIOCNOTTY */\n");
671 #endif
672 if (Cflag && (inetdflag || pmflag))
674 #ifdef __GNU_LIBRARY__
675 fprintf (fout, "#include <sys/types.h> /* open */\n");
676 fprintf (fout, "#include <sys/stat.h> /* open */\n");
677 fprintf (fout, "#include <fcntl.h> /* open */\n");
678 fprintf (fout, "#include <unistd.h> /* getdtablesize */\n");
679 #else
680 fprintf (fout, "#ifdef __cplusplus\n");
681 fprintf (fout, "#include <sysent.h> /* getdtablesize, open */\n");
682 fprintf (fout, "#endif /* __cplusplus */\n");
683 if (tirpcflag)
684 fprintf (fout, "#include <unistd.h> /* setsid */\n");
685 #endif
687 #ifdef __GNU_LIBRARY__
688 if (tirpcflag && !(Cflag && (inetdflag || pmflag)))
689 #else
690 if (tirpcflag)
691 #endif
692 fprintf (fout, "#include <sys/types.h>\n");
694 fprintf (fout, "#include <memory.h>\n");
695 #ifndef __GNU_LIBRARY__
696 fprintf (fout, "#include <stropts.h>\n");
697 #endif
698 if (inetdflag || !tirpcflag)
700 fprintf (fout, "#include <sys/socket.h>\n");
701 fprintf (fout, "#include <netinet/in.h>\n");
704 if ((netflag || pmflag) && tirpcflag && !nomain)
706 fprintf (fout, "#include <netconfig.h>\n");
708 if ( /*timerflag && */ tirpcflag)
709 fprintf (fout, "#include <sys/resource.h> /* rlimit */\n");
710 if (logflag || inetdflag || pmflag)
712 #ifdef __GNU_LIBRARY__
713 fprintf (fout, "#include <syslog.h>\n");
714 #else
715 fprintf (fout, "#ifdef SYSLOG\n");
716 fprintf (fout, "#include <syslog.h>\n");
717 fprintf (fout, "#else\n");
718 fprintf (fout, "#define LOG_ERR 1\n");
719 fprintf (fout, "#define openlog(a, b, c)\n");
720 fprintf (fout, "#endif\n");
721 #endif
724 /* for ANSI-C */
725 if (Cflag)
726 fprintf (fout, "\n#ifndef SIG_PF\n#define SIG_PF void(*)(int)\n#endif\n");
728 #ifndef __GNU_LIBRARY__
729 fprintf (fout, "\n#ifdef DEBUG\n#define RPC_SVC_FG\n#endif\n");
730 #endif
731 if (timerflag)
732 fprintf (fout, "\n#define _RPCSVC_CLOSEDOWN %s\n", svcclosetime);
733 while ((def = get_definition ()) != NULL)
735 foundprogram |= (def->def_kind == DEF_PROGRAM);
737 if (extend && !foundprogram)
739 unlink (outfilename);
740 return;
742 write_most (infile, netflag, nomain);
743 if (!nomain)
745 if (!do_registers (argc, argv))
747 if (outfilename)
748 unlink (outfilename);
749 usage ();
751 write_rest ();
756 * generate client side stubs
758 static void
759 l_output (const char *infile, const char *define, int extend,
760 const char *outfile)
762 char *include;
763 definition *def;
764 int foundprogram = 0;
765 const char *outfilename;
767 open_input (infile, define);
768 outfilename = extend ? extendfile (infile, outfile) : outfile;
769 open_output (infile, outfilename);
770 add_warning ();
771 if (Cflag)
772 fprintf (fout, "#include <memory.h> /* for memset */\n");
773 if (infile && (include = extendfile (infile, ".h")))
775 fprintf (fout, "#include \"%s\"\n", include);
776 free (include);
778 else
779 fprintf (fout, "#include <rpc/rpc.h>\n");
780 while ((def = get_definition ()) != NULL)
782 foundprogram |= (def->def_kind == DEF_PROGRAM);
784 if (extend && !foundprogram)
786 unlink (outfilename);
787 return;
789 write_stubs ();
793 * generate the dispatch table
795 static void
796 t_output (const char *infile, const char *define, int extend,
797 const char *outfile)
799 definition *def;
800 int foundprogram = 0;
801 const char *outfilename;
803 open_input (infile, define);
804 outfilename = extend ? extendfile (infile, outfile) : outfile;
805 open_output (infile, outfilename);
806 add_warning ();
807 while ((def = get_definition ()) != NULL)
809 foundprogram |= (def->def_kind == DEF_PROGRAM);
811 if (extend && !foundprogram)
813 unlink (outfilename);
814 return;
816 write_tables ();
819 /* sample routine for the server template */
820 static void
821 svc_output (const char *infile, const char *define, int extend,
822 const char *outfile)
824 definition *def;
825 char *include;
826 const char *outfilename;
827 long tell;
829 open_input (infile, define);
830 outfilename = extend ? extendfile (infile, outfile) : outfile;
831 checkfiles (infile, outfilename);
832 /*check if outfile already exists.
833 if so, print an error message and exit */
834 open_output (infile, outfilename);
835 add_sample_msg ();
837 if (infile && (include = extendfile (infile, ".h")))
839 fprintf (fout, "#include \"%s\"\n", include);
840 free (include);
842 else
843 fprintf (fout, "#include <rpc/rpc.h>\n");
845 tell = ftell (fout);
846 while ((def = get_definition ()) != NULL)
848 write_sample_svc (def);
850 if (extend && tell == ftell (fout))
852 unlink (outfilename);
857 /* sample main routine for client */
858 static void
859 clnt_output (const char *infile, const char *define, int extend,
860 const char *outfile)
862 definition *def;
863 char *include;
864 const char *outfilename;
865 long tell;
866 int has_program = 0;
868 open_input (infile, define);
869 outfilename = extend ? extendfile (infile, outfile) : outfile;
870 checkfiles (infile, outfilename);
871 /*check if outfile already exists.
872 if so, print an error message and exit */
874 open_output (infile, outfilename);
875 add_sample_msg ();
876 if (infile && (include = extendfile (infile, ".h")))
878 fprintf (fout, "#include \"%s\"\n", include);
879 free (include);
881 else
882 fprintf (fout, "#include <rpc/rpc.h>\n");
883 tell = ftell (fout);
884 while ((def = get_definition ()) != NULL)
886 has_program += write_sample_clnt (def);
889 if (has_program)
890 write_sample_clnt_main ();
892 if (extend && tell == ftell (fout))
894 unlink (outfilename);
898 static char *
899 file_name (const char *file, const char *ext)
901 char *temp;
902 temp = extendfile (file, ext);
904 if (access (temp, F_OK) != -1)
905 return (temp);
906 else
907 return ((char *) " ");
910 static void
911 mkfile_output (struct commandline *cmd)
913 char *mkfilename;
914 const char *clientname, *clntname, *xdrname, *hdrname;
915 const char *servername, *svcname, *servprogname, *clntprogname;
916 char *temp;
918 svcname = file_name (cmd->infile, "_svc.c");
919 clntname = file_name (cmd->infile, "_clnt.c");
920 xdrname = file_name (cmd->infile, "_xdr.c");
921 hdrname = file_name (cmd->infile, ".h");
923 if (allfiles)
925 servername = extendfile (cmd->infile, "_server.c");
926 clientname = extendfile (cmd->infile, "_client.c");
928 else
930 servername = " ";
931 clientname = " ";
933 servprogname = extendfile (cmd->infile, "_server");
934 clntprogname = extendfile (cmd->infile, "_client");
936 if (allfiles)
938 mkfilename = alloc (strlen ("Makefile.") +
939 strlen (cmd->infile) + 1);
940 temp = (char *) rindex (cmd->infile, '.');
941 strcat (mkfilename, "Makefile.");
942 strncat (mkfilename, cmd->infile,
943 (temp - cmd->infile));
945 else
946 mkfilename = (char *) cmd->outfile;
948 checkfiles (NULL, mkfilename);
949 open_output (NULL, mkfilename);
951 fprintf (fout, "\n# This is a template Makefile generated by rpcgen\n");
953 f_print (fout, "\n# Parameters\n\n");
955 f_print (fout, "CLIENT = %s\nSERVER = %s\n\n", clntprogname, servprogname);
956 f_print (fout, "SOURCES_CLNT.c = \nSOURCES_CLNT.h = \n");
957 f_print (fout, "SOURCES_SVC.c = \nSOURCES_SVC.h = \n");
958 f_print (fout, "SOURCES.x = %s\n\n", cmd->infile);
959 f_print (fout, "TARGETS_SVC.c = %s %s %s \n",
960 svcname, servername, xdrname);
961 f_print (fout, "TARGETS_CLNT.c = %s %s %s \n",
962 clntname, clientname, xdrname);
963 f_print (fout, "TARGETS = %s %s %s %s %s %s\n\n",
964 hdrname, xdrname, clntname,
965 svcname, clientname, servername);
967 f_print (fout, "OBJECTS_CLNT = $(SOURCES_CLNT.c:%%.c=%%.o) \
968 $(TARGETS_CLNT.c:%%.c=%%.o)");
970 f_print (fout, "\nOBJECTS_SVC = $(SOURCES_SVC.c:%%.c=%%.o) \
971 $(TARGETS_SVC.c:%%.c=%%.o)");
973 f_print (fout, "\n# Compiler flags \n");
974 if (mtflag)
975 fprintf (fout, "\nCPPFLAGS += -D_REENTRANT\nCFLAGS += -g \nLDLIBS \
976 += -lnsl -lpthread \n ");
977 else
978 f_print (fout, "\nCFLAGS += -g \nLDLIBS += -lnsl\n");
979 f_print (fout, "RPCGENFLAGS = \n");
981 f_print (fout, "\n# Targets \n\n");
983 f_print (fout, "all : $(CLIENT) $(SERVER)\n\n");
984 f_print (fout, "$(TARGETS) : $(SOURCES.x) \n");
985 f_print (fout, "\trpcgen $(RPCGENFLAGS) $(SOURCES.x)\n\n");
986 f_print (fout, "$(OBJECTS_CLNT) : $(SOURCES_CLNT.c) $(SOURCES_CLNT.h) \
987 $(TARGETS_CLNT.c) \n\n");
989 f_print (fout, "$(OBJECTS_SVC) : $(SOURCES_SVC.c) $(SOURCES_SVC.h) \
990 $(TARGETS_SVC.c) \n\n");
991 f_print (fout, "$(CLIENT) : $(OBJECTS_CLNT) \n");
992 f_print (fout, "\t$(LINK.c) -o $(CLIENT) $(OBJECTS_CLNT) \
993 $(LDLIBS) \n\n");
994 f_print (fout, "$(SERVER) : $(OBJECTS_SVC) \n");
995 f_print (fout, "\t$(LINK.c) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS)\n\n ");
996 f_print (fout, "clean:\n\t $(RM) core $(TARGETS) $(OBJECTS_CLNT) \
997 $(OBJECTS_SVC) $(CLIENT) $(SERVER)\n\n");
1001 * Perform registrations for service output
1002 * Return 0 if failed; 1 otherwise.
1004 static int
1005 do_registers (int argc, const char *argv[])
1007 int i;
1009 if (inetdflag || !tirpcflag)
1011 for (i = 1; i < argc; i++)
1013 if (streq (argv[i], "-s"))
1015 if (!check_nettype (argv[i + 1], valid_i_nettypes))
1016 return 0;
1017 write_inetd_register (argv[i + 1]);
1018 i++;
1022 else
1024 for (i = 1; i < argc; i++)
1025 if (streq (argv[i], "-s"))
1027 if (!check_nettype (argv[i + 1], valid_ti_nettypes))
1028 return 0;
1029 write_nettype_register (argv[i + 1]);
1030 i++;
1032 else if (streq (argv[i], "-n"))
1034 write_netid_register (argv[i + 1]);
1035 i++;
1038 return 1;
1042 * Add another argument to the arg list
1044 static void
1045 addarg (const char *cp)
1047 if (argcount >= ARGLISTLEN)
1049 fprintf (stderr, _("rpcgen: too many defines\n"));
1050 crash ();
1051 /*NOTREACHED */
1053 arglist[argcount++] = cp;
1056 static void
1057 putarg (int whereto, const char *cp)
1059 if (whereto >= ARGLISTLEN)
1061 fprintf (stderr, _("rpcgen: arglist coding error\n"));
1062 crash ();
1063 /*NOTREACHED */
1065 arglist[whereto] = cp;
1069 * if input file is stdin and an output file is specified then complain
1070 * if the file already exists. Otherwise the file may get overwritten
1071 * If input file does not exist, exit with an error
1074 static void
1075 checkfiles (const char *infile, const char *outfile)
1077 struct stat buf;
1079 if (infile) /* infile ! = NULL */
1080 if (stat (infile, &buf) < 0)
1082 perror (infile);
1083 crash ();
1085 if (outfile)
1087 if (stat (outfile, &buf) < 0)
1088 return; /* file does not exist */
1089 else
1091 fprintf (stderr,
1092 _("file '%s' already exists and may be overwritten\n"),
1093 outfile);
1094 crash ();
1100 * Parse command line arguments
1102 static int
1103 parseargs (int argc, const char *argv[], struct commandline *cmd)
1105 int i;
1106 int j;
1107 int c;
1108 char flag[(1 << 8 * sizeof (char))];
1109 int nflags;
1111 cmdname = argv[0];
1112 cmd->infile = cmd->outfile = NULL;
1113 if (argc < 2)
1115 return (0);
1117 allfiles = 0;
1118 flag['c'] = 0;
1119 flag['h'] = 0;
1120 flag['l'] = 0;
1121 flag['m'] = 0;
1122 flag['o'] = 0;
1123 flag['s'] = 0;
1124 flag['n'] = 0;
1125 flag['t'] = 0;
1126 flag['S'] = 0;
1127 flag['C'] = 0;
1128 flag['M'] = 0;
1130 for (i = 1; i < argc; i++)
1132 if (argv[i][0] != '-')
1134 if (cmd->infile)
1136 fprintf (stderr,
1137 _("Cannot specify more than one input file!\n"));
1138 return 0;
1140 cmd->infile = argv[i];
1142 else
1144 for (j = 1; argv[i][j] != 0; j++)
1146 c = argv[i][j];
1147 switch (c)
1149 case 'a':
1150 allfiles = 1;
1151 break;
1152 case 'c':
1153 case 'h':
1154 case 'l':
1155 case 'm':
1156 case 't':
1157 if (flag[c])
1158 return 0;
1159 flag[c] = 1;
1160 break;
1161 case 'S':
1162 /* sample flag: Ss or Sc.
1163 Ss means set flag['S'];
1164 Sc means set flag['C'];
1165 Sm means set flag['M']; */
1166 c = argv[i][++j]; /* get next char */
1167 if (c == 's')
1168 c = 'S';
1169 else if (c == 'c')
1170 c = 'C';
1171 else if (c == 'm')
1172 c = 'M';
1173 else
1174 return 0;
1176 if (flag[c])
1177 return 0;
1178 flag[c] = 1;
1179 break;
1180 case 'C': /* ANSI C syntax */
1181 Cflag = 1;
1182 break;
1184 #ifdef __GNU_LIBRARY__
1185 case 'k': /* K&R C syntax */
1186 Cflag = 0;
1187 break;
1189 #endif
1190 case 'b': /* turn TIRPC flag off for
1191 generating backward compatible
1193 tirpcflag = 0;
1194 break;
1196 #ifdef __GNU_LIBRARY__
1197 case '5': /* turn TIRPC flag on for
1198 generating SysVr4 compatible
1200 tirpcflag = 1;
1201 break;
1202 #endif
1203 case 'I':
1204 inetdflag = 1;
1205 break;
1206 case 'N':
1207 newstyle = 1;
1208 break;
1209 case 'L':
1210 logflag = 1;
1211 break;
1212 case 'K':
1213 if (++i == argc)
1215 return (0);
1217 svcclosetime = argv[i];
1218 goto nextarg;
1219 case 'T':
1220 tblflag = 1;
1221 break;
1222 case 'M':
1223 mtflag = 1;
1224 break;
1225 case 'i':
1226 if (++i == argc)
1228 return (0);
1230 inlineflag = atoi (argv[i]);
1231 goto nextarg;
1232 case 'n':
1233 case 'o':
1234 case 's':
1235 if (argv[i][j - 1] != '-' ||
1236 argv[i][j + 1] != 0)
1238 return (0);
1240 flag[c] = 1;
1241 if (++i == argc)
1243 return (0);
1245 if (c == 's')
1247 if (!streq (argv[i], "udp") &&
1248 !streq (argv[i], "tcp"))
1249 return 0;
1251 else if (c == 'o')
1253 if (cmd->outfile)
1254 return 0;
1255 cmd->outfile = argv[i];
1257 goto nextarg;
1258 case 'D':
1259 if (argv[i][j - 1] != '-')
1260 return 0;
1261 addarg (argv[i]);
1262 goto nextarg;
1263 case 'Y':
1264 if (++i == argc)
1265 return 0;
1267 size_t len = strlen (argv[i]);
1268 pathbuf = malloc (len + 5);
1269 if (pathbuf == NULL)
1271 perror (cmdname);
1272 crash ();
1274 stpcpy (stpcpy (pathbuf,
1275 argv[i]),
1276 "/cpp");
1277 CPP = pathbuf;
1278 cppDefined = 1;
1279 goto nextarg;
1282 default:
1283 return 0;
1286 nextarg:
1291 cmd->cflag = flag['c'];
1292 cmd->hflag = flag['h'];
1293 cmd->lflag = flag['l'];
1294 cmd->mflag = flag['m'];
1295 cmd->nflag = flag['n'];
1296 cmd->sflag = flag['s'];
1297 cmd->tflag = flag['t'];
1298 cmd->Ssflag = flag['S'];
1299 cmd->Scflag = flag['C'];
1300 cmd->makefileflag = flag['M'];
1302 if (tirpcflag)
1304 pmflag = inetdflag ? 0 : 1; /* pmflag or inetdflag is always TRUE */
1305 if ((inetdflag && cmd->nflag))
1306 { /* netid not allowed with inetdflag */
1307 fprintf (stderr, _("Cannot use netid flag with inetd flag!\n"));
1308 return 0;
1311 else
1312 { /* 4.1 mode */
1313 pmflag = 0; /* set pmflag only in tirpcmode */
1314 #ifndef __GNU_LIBRARY__
1315 inetdflag = 1; /* inetdflag is TRUE by default */
1316 #endif
1317 if (cmd->nflag)
1318 { /* netid needs TIRPC */
1319 f_print (stderr, _("Cannot use netid flag without TIRPC!\n"));
1320 return (0);
1324 if (newstyle && (tblflag || cmd->tflag))
1326 f_print (stderr, _("Cannot use table flags with newstyle!\n"));
1327 return (0);
1330 /* check no conflicts with file generation flags */
1331 nflags = cmd->cflag + cmd->hflag + cmd->lflag + cmd->mflag +
1332 cmd->sflag + cmd->nflag + cmd->tflag + cmd->Ssflag + cmd->Scflag;
1334 if (nflags == 0)
1336 if (cmd->outfile != NULL || cmd->infile == NULL)
1338 return (0);
1341 else if (cmd->infile == NULL &&
1342 (cmd->Ssflag || cmd->Scflag || cmd->makefileflag))
1344 fprintf (stderr,
1345 _("\"infile\" is required for template generation flags.\n"));
1346 return 0;
1348 if (nflags > 1)
1350 fprintf (stderr, _("Cannot have more than one file generation flag!\n"));
1351 return 0;
1353 return 1;
1356 static void
1357 usage (void)
1359 fprintf (stderr, _("usage: %s infile\n"), cmdname);
1360 fprintf (stderr, _("\t%s [-abkCLNTM][-Dname[=value]] [-i size] \
1361 [-I [-K seconds]] [-Y path] infile\n"), cmdname);
1362 fprintf (stderr, _("\t%s [-c | -h | -l | -m | -t | -Sc | -Ss | -Sm] \
1363 [-o outfile] [infile]\n"), cmdname);
1364 fprintf (stderr, _("\t%s [-s nettype]* [-o outfile] [infile]\n"), cmdname);
1365 fprintf (stderr, _("\t%s [-n netid]* [-o outfile] [infile]\n"), cmdname);
1366 options_usage ();
1367 exit (1);
1370 static void
1371 options_usage (void)
1373 f_print (stderr, "options:\n");
1374 f_print (stderr, "-a\t\tgenerate all files, including samples\n");
1375 f_print (stderr, "-b\t\tbackward compatibility mode (generates code for SunOS 4.1)\n");
1376 f_print (stderr, "-c\t\tgenerate XDR routines\n");
1377 f_print (stderr, "-C\t\tANSI C mode\n");
1378 f_print (stderr, "-Dname[=value]\tdefine a symbol (same as #define)\n");
1379 f_print (stderr, "-h\t\tgenerate header file\n");
1380 f_print (stderr, "-i size\t\tsize at which to start generating inline code\n");
1381 f_print (stderr, "-I\t\tgenerate code for inetd support in server (for SunOS 4.1)\n");
1382 f_print (stderr, "-K seconds\tserver exits after K seconds of inactivity\n");
1383 f_print (stderr, "-l\t\tgenerate client side stubs\n");
1384 f_print (stderr, "-L\t\tserver errors will be printed to syslog\n");
1385 f_print (stderr, "-m\t\tgenerate server side stubs\n");
1386 f_print (stderr, "-M\t\tgenerate MT-safe code\n");
1387 f_print (stderr, "-n netid\tgenerate server code that supports named netid\n");
1388 f_print (stderr, "-N\t\tsupports multiple arguments and call-by-value\n");
1389 f_print (stderr, "-o outfile\tname of the output file\n");
1390 f_print (stderr, "-s nettype\tgenerate server code that supports named nettype\n");
1391 f_print (stderr, "-Sc\t\tgenerate sample client code that uses remote procedures\n");
1392 f_print (stderr, "-Ss\t\tgenerate sample server code that defines remote procedures\n");
1393 f_print (stderr, "-Sm \t\tgenerate makefile template \n");
1394 f_print (stderr, "-t\t\tgenerate RPC dispatch table\n");
1395 f_print (stderr, "-T\t\tgenerate code to support RPC dispatch tables\n");
1396 f_print (stderr, "-Y path\t\tdirectory name to find C preprocessor (cpp)\n");
1398 exit (1);