* Makeconfig ($(common-objpfx)shlib-versions.v.i): Check also
[glibc.git] / sunrpc / rpc_main.c
blobfee83514d1a7d394b325c44879c782913b2f577d
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 #if defined(LIBC_SCCS) && !defined(lint)
35 static const char main_rcsid[] =
36 "$Id$";
37 #endif
40 * rpc_main.c, Top level of the RPC protocol compiler.
43 #include <errno.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <libintl.h>
48 #include <ctype.h>
49 #include <sys/types.h>
50 #include <sys/param.h>
51 #include <sys/file.h>
52 #include <sys/stat.h>
53 #include <sys/wait.h>
54 #include "rpc_parse.h"
55 #include "rpc_util.h"
56 #include "rpc_scan.h"
57 #include "proto.h"
59 #define EXTEND 1 /* alias for TRUE */
60 #define DONT_EXTEND 0 /* alias for FALSE */
62 struct commandline
64 int cflag; /* xdr C routines */
65 int hflag; /* header file */
66 int lflag; /* client side stubs */
67 int mflag; /* server side stubs */
68 int nflag; /* netid flag */
69 int sflag; /* server stubs for the given transport */
70 int tflag; /* dispatch Table file */
71 int Ssflag; /* produce server sample code */
72 int Scflag; /* produce client sample code */
73 int makefileflag; /* Generate a template Makefile */
74 const char *infile; /* input module name */
75 const char *outfile; /* output module name */
79 static const char *cmdname;
81 #define SVR4_CPP "/usr/ccs/lib/cpp"
82 #define SUNOS_CPP "/lib/cpp"
84 static const char *svcclosetime = "120";
85 static int cppDefined; /* explicit path for C preprocessor */
86 static const char *CPP = SUNOS_CPP;
87 static const char CPPFLAGS[] = "-C";
88 static char *pathbuf;
89 static int cpp_pid;
90 static const char *allv[] =
92 "rpcgen", "-s", "udp", "-s", "tcp"
94 static int allc = sizeof (allv) / sizeof (allv[0]);
95 static const char *allnv[] =
97 "rpcgen", "-s", "netpath",
99 static int allnc = sizeof (allnv) / sizeof (allnv[0]);
102 * machinations for handling expanding argument list
104 static void addarg (const char *); /* add another argument to the list */
105 static void putarg (int, const char *); /* put argument at specified location */
106 static void clear_args (void); /* clear argument list */
107 static void checkfiles (const char *, const char *);
108 /* check if out file already exists */
110 static void clear_args (void);
111 static char *extendfile (const char *file, const char *ext);
112 static void open_output (const char *infile, const char *outfile);
113 static void add_warning (void);
114 static void clear_args (void);
115 static void find_cpp (void);
116 static void open_input (const char *infile, const char *define);
117 static int check_nettype (const char *name, const char *list_to_check[]);
118 static void c_output (const char *infile, const char *define,
119 int extend, const char *outfile);
120 static void h_output (const char *infile, const char *define,
121 int extend, const char *outfile);
122 static void s_output (int argc, const char *argv[], const char *infile,
123 const char *define, int extend,
124 const char *outfile, int nomain, int netflag);
125 static void l_output (const char *infile, const char *define,
126 int extend, const char *outfile);
127 static void t_output (const char *infile, const char *define,
128 int extend, const char *outfile);
129 static void svc_output (const char *infile, const char *define,
130 int extend, const char *outfile);
131 static void clnt_output (const char *infile, const char *define,
132 int extend, const char *outfile);
133 static void mkfile_output (struct commandline *cmd);
134 static int do_registers (int argc, const char *argv[]);
135 static void addarg (const char *cp);
136 static void putarg (int whereto, const char *cp);
137 static void checkfiles (const char *infile, const char *outfile);
138 static int parseargs (int argc, const char *argv[], struct commandline *cmd);
139 static void usage (void) __attribute__ ((noreturn));
140 static void options_usage (void) __attribute__ ((noreturn));
141 static void c_initialize (void);
142 static char *generate_guard (const char *pathname);
145 #define ARGLISTLEN 20
146 #define FIXEDARGS 2
148 static const char *arglist[ARGLISTLEN];
149 static int argcount = FIXEDARGS;
152 int nonfatalerrors; /* errors */
153 int inetdflag /* = 1 */ ; /* Support for inetd *//* is now the default */
154 int pmflag; /* Support for port monitors */
155 int logflag; /* Use syslog instead of fprintf for errors */
156 int tblflag; /* Support for dispatch table file */
157 int mtflag; /* Support for MT */
159 #define INLINE 3
160 /*length at which to start doing an inline */
162 int inlineflag = INLINE; /* length at which to start doing an inline. 3 = default
163 if 0, no xdr_inline code */
165 int indefinitewait; /* If started by port monitors, hang till it wants */
166 int exitnow; /* If started by port monitors, exit after the call */
167 int timerflag; /* TRUE if !indefinite && !exitnow */
168 int newstyle; /* newstyle of passing arguments (by value) */
169 #ifdef __GNU_LIBRARY__
170 int Cflag = 1; /* ANSI C syntax */
171 #else
172 int Cflag; /* ANSI C/C++ syntax */
173 #endif
174 int CCflag; /* C++ files */
175 static int allfiles; /* generate all files */
176 #ifdef __GNU_LIBRARY__
177 int tirpcflag; /* generating code for tirpc, by default */
178 #else
179 int tirpcflag = 1; /* generating code for tirpc, by default */
180 #endif
181 xdrfunc *xdrfunc_head; /* xdr function list */
182 xdrfunc *xdrfunc_tail; /* xdr function list */
185 main (int argc, const char *argv[])
187 struct commandline cmd;
189 (void) memset ((char *) &cmd, 0, sizeof (struct commandline));
190 clear_args ();
191 if (!parseargs (argc, argv, &cmd))
192 usage ();
194 if (cmd.cflag || cmd.hflag || cmd.lflag || cmd.tflag || cmd.sflag ||
195 cmd.mflag || cmd.nflag || cmd.Ssflag || cmd.Scflag)
197 checkfiles (cmd.infile, cmd.outfile);
199 else
200 checkfiles (cmd.infile, NULL);
202 if (cmd.cflag)
203 c_output (cmd.infile, "-DRPC_XDR", DONT_EXTEND, cmd.outfile);
204 else if (cmd.hflag)
205 h_output (cmd.infile, "-DRPC_HDR", DONT_EXTEND, cmd.outfile);
206 else if (cmd.lflag)
207 l_output (cmd.infile, "-DRPC_CLNT", DONT_EXTEND, cmd.outfile);
208 else if (cmd.sflag || cmd.mflag || (cmd.nflag))
209 s_output (argc, argv, cmd.infile, "-DRPC_SVC", DONT_EXTEND,
210 cmd.outfile, cmd.mflag, cmd.nflag);
211 else if (cmd.tflag)
212 t_output (cmd.infile, "-DRPC_TBL", DONT_EXTEND, cmd.outfile);
213 else if (cmd.Ssflag)
214 svc_output (cmd.infile, "-DRPC_SERVER", DONT_EXTEND, cmd.outfile);
215 else if (cmd.Scflag)
216 clnt_output (cmd.infile, "-DRPC_CLIENT", DONT_EXTEND, cmd.outfile);
217 else if (cmd.makefileflag)
218 mkfile_output (&cmd);
219 else
221 /* the rescans are required, since cpp may effect input */
222 c_output (cmd.infile, "-DRPC_XDR", EXTEND, "_xdr.c");
223 reinitialize ();
224 h_output (cmd.infile, "-DRPC_HDR", EXTEND, ".h");
225 reinitialize ();
226 l_output (cmd.infile, "-DRPC_CLNT", EXTEND, "_clnt.c");
227 reinitialize ();
228 if (inetdflag || !tirpcflag)
229 s_output (allc, allv, cmd.infile, "-DRPC_SVC", EXTEND,
230 "_svc.c", cmd.mflag, cmd.nflag);
231 else
232 s_output (allnc, allnv, cmd.infile, "-DRPC_SVC",
233 EXTEND, "_svc.c", cmd.mflag, cmd.nflag);
234 if (tblflag)
236 reinitialize ();
237 t_output (cmd.infile, "-DRPC_TBL", EXTEND, "_tbl.i");
239 if (allfiles)
241 reinitialize ();
242 svc_output (cmd.infile, "-DRPC_SERVER", EXTEND, "_server.c");
243 reinitialize ();
244 clnt_output (cmd.infile, "-DRPC_CLIENT", EXTEND, "_client.c");
246 if (allfiles || (cmd.makefileflag == 1))
248 reinitialize ();
249 mkfile_output (&cmd);
253 return nonfatalerrors;
257 * add extension to filename
259 static char *
260 extendfile (const char *file, const char *ext)
262 char *res;
263 const char *p;
265 res = alloc (strlen (file) + strlen (ext) + 1);
266 if (res == NULL)
267 abort ();
268 p = strrchr (file, '.');
269 if (p == NULL)
270 p = file + strlen (file);
271 strcpy (res, file);
272 strcpy (res + (p - file), ext);
273 return res;
277 * Open output file with given extension
279 static void
280 open_output (const char *infile, const char *outfile)
282 if (outfile == NULL)
284 fout = stdout;
285 return;
288 if (infile != NULL && streq (outfile, infile))
290 fprintf (stderr, _ ("%s: output would overwrite %s\n"), cmdname,
291 infile);
292 crash ();
294 fout = fopen (outfile, "w");
295 if (fout == NULL)
297 fprintf (stderr, _ ("%s: unable to open %s: %m\n"), cmdname, outfile);
298 crash ();
300 record_open (outfile);
303 /* Close the output file and check for write errors. */
304 static void
305 close_output (const char *outfile)
307 if (fclose (fout) == EOF)
309 fprintf (stderr, _("%s: while writing output %s: %m"), cmdname,
310 outfile ?: "<stdout>");
311 crash ();
315 static void
316 add_warning (void)
318 fprintf (fout, "/*\n");
319 fprintf (fout, " * Please do not edit this file.\n");
320 fprintf (fout, " * It was generated using rpcgen.\n");
321 fprintf (fout, " */\n\n");
324 /* clear list of arguments */
325 static void
326 clear_args (void)
328 int i;
329 for (i = FIXEDARGS; i < ARGLISTLEN; ++i)
330 arglist[i] = NULL;
331 argcount = FIXEDARGS;
334 /* make sure that a CPP exists */
335 static void
336 find_cpp (void)
338 struct stat buf;
340 if (stat (CPP, &buf) < 0)
341 { /* /lib/cpp or explicit cpp does not exist */
342 if (cppDefined)
344 fprintf (stderr, _ ("cannot find C preprocessor: %s \n"), CPP);
345 crash ();
347 else
348 { /* try the other one */
349 CPP = SVR4_CPP;
350 if (stat (CPP, &buf) < 0)
351 { /* can't find any cpp */
352 fputs (_ ("cannot find any C preprocessor (cpp)\n"), stdout);
353 crash ();
360 * Open input file with given define for C-preprocessor
362 static void
363 open_input (const char *infile, const char *define)
365 int pd[2];
367 infilename = (infile == NULL) ? "<stdin>" : infile;
368 if (pipe (pd) != 0)
370 perror ("pipe");
371 exit (1);
373 cpp_pid = fork ();
374 switch (cpp_pid)
376 case 0:
377 find_cpp ();
378 putarg (0, CPP);
379 putarg (1, CPPFLAGS);
380 addarg (define);
381 if (infile)
382 addarg (infile);
383 addarg ((char *) NULL);
384 close (1);
385 dup2 (pd[1], 1);
386 close (pd[0]);
387 execv (arglist[0], (char **) arglist);
388 perror ("execv");
389 exit (1);
390 case -1:
391 perror ("fork");
392 exit (1);
394 close (pd[1]);
395 fin = fdopen (pd[0], "r");
396 if (fin == NULL)
398 fprintf (stderr, "%s: ", cmdname);
399 perror (infilename);
400 crash ();
404 /* Close the connection to the C-preprocessor and check for successfull
405 termination. */
406 static void
407 close_input (void)
409 int status;
411 fclose (fin);
412 /* Check the termination status. */
413 if (waitpid (cpp_pid, &status, 0) < 0)
415 perror ("waitpid");
416 crash ();
418 if (WIFSIGNALED (status) || WEXITSTATUS (status) != 0)
420 if (WIFSIGNALED (status))
421 fprintf (stderr, _("%s: C preprocessor failed with signal %d\n"),
422 cmdname, WTERMSIG (status));
423 else
424 fprintf (stderr, _("%s: C preprocessor failed with exit code %d\n"),
425 cmdname, WEXITSTATUS (status));
426 crash ();
430 /* valid tirpc nettypes */
431 static const char *valid_ti_nettypes[] =
433 "netpath",
434 "visible",
435 "circuit_v",
436 "datagram_v",
437 "circuit_n",
438 "datagram_n",
439 "udp",
440 "tcp",
441 "raw",
442 NULL
445 /* valid inetd nettypes */
446 static const char *valid_i_nettypes[] =
448 "udp",
449 "tcp",
450 NULL
453 static int
454 check_nettype (const char *name, const char *list_to_check[])
456 int i;
457 for (i = 0; list_to_check[i] != NULL; i++)
459 if (strcmp (name, list_to_check[i]) == 0)
461 return 1;
464 fprintf (stderr, _ ("illegal nettype :`%s'\n"), name);
465 return 0;
469 * Compile into an XDR routine output file
472 static void
473 c_output (const char *infile, const char *define, int extend,
474 const char *outfile)
476 definition *def;
477 char *include;
478 const char *outfilename;
479 long tell;
481 c_initialize ();
482 open_input (infile, define);
483 outfilename = extend ? extendfile (infile, outfile) : outfile;
484 open_output (infile, outfilename);
485 add_warning ();
486 if (infile && (include = extendfile (infile, ".h")))
488 fprintf (fout, "#include \"%s\"\n", include);
489 free (include);
490 /* .h file already contains rpc/rpc.h */
492 else
493 fprintf (fout, "#include <rpc/rpc.h>\n");
494 tell = ftell (fout);
495 while ((def = get_definition ()) != NULL)
496 emit (def);
498 if (extend && tell == ftell (fout))
499 unlink (outfilename);
500 close_input ();
501 close_output (outfilename);
504 void
505 c_initialize (void)
508 /* add all the starting basic types */
510 add_type (1, "int");
511 add_type (1, "long");
512 add_type (1, "short");
513 add_type (1, "bool");
515 add_type (1, "u_int");
516 add_type (1, "u_long");
517 add_type (1, "u_short");
521 char rpcgen_table_dcl[] = "struct rpcgen_table {\n\
522 char *(*proc)();\n\
523 xdrproc_t xdr_arg;\n\
524 unsigned len_arg;\n\
525 xdrproc_t xdr_res;\n\
526 unsigned len_res;\n\
527 };\n";
530 static char *
531 generate_guard (const char *pathname)
533 const char *filename;
534 char *guard, *tmp;
536 filename = strrchr (pathname, '/'); /* find last component */
537 filename = ((filename == NULL) ? pathname : filename + 1);
538 guard = strdup (filename);
539 /* convert to upper case */
540 tmp = guard;
541 while (*tmp)
543 if (islower (*tmp))
544 *tmp = toupper (*tmp);
545 tmp++;
548 guard = extendfile (guard, "_H_RPCGEN");
549 return guard;
553 * Compile into an XDR header file
557 static void
558 h_output (const char *infile, const char *define, int extend,
559 const char *outfile)
561 xdrfunc *xdrfuncp;
562 definition *def;
563 const char *ifilename;
564 const char *outfilename;
565 long tell;
566 char *guard;
567 list *l;
569 open_input (infile, define);
570 outfilename = extend ? extendfile (infile, outfile) : outfile;
571 open_output (infile, outfilename);
572 add_warning ();
573 ifilename = (infile == NULL) ? "STDIN" : infile;
574 guard = generate_guard (outfilename ? outfilename : ifilename);
576 fprintf (fout, "#ifndef _%s\n#define _%s\n\n", guard,
577 guard);
579 fprintf (fout, "#include <rpc/rpc.h>\n\n");
581 if (mtflag)
583 fprintf (fout, "#include <pthread.h>\n");
586 /* put the C++ support */
587 if (Cflag && !CCflag)
589 fprintf (fout, "\n#ifdef __cplusplus\n");
590 fprintf (fout, "extern \"C\" {\n");
591 fprintf (fout, "#endif\n\n");
594 tell = ftell (fout);
595 /* print data definitions */
596 while ((def = get_definition ()) != NULL)
598 print_datadef (def);
601 /* print function declarations.
602 Do this after data definitions because they might be used as
603 arguments for functions */
604 for (l = defined; l != NULL; l = l->next)
606 print_funcdef (l->val);
608 /* Now print all xdr func declarations */
609 if (xdrfunc_head != NULL)
611 fprintf (fout, "\n/* the xdr functions */\n");
612 if (CCflag)
614 fprintf (fout, "\n#ifdef __cplusplus\n");
615 fprintf (fout, "extern \"C\" {\n");
616 fprintf (fout, "#endif\n");
618 if (!Cflag)
620 xdrfuncp = xdrfunc_head;
621 while (xdrfuncp != NULL)
623 print_xdr_func_def (xdrfuncp->name,
624 xdrfuncp->pointerp, 2);
625 xdrfuncp = xdrfuncp->next;
628 else
630 int i;
632 for (i = 1; i < 3; ++i)
634 if (i == 1)
635 fprintf (fout, "\n#if defined(__STDC__) || defined(__cplusplus)\n");
636 else
637 fprintf (fout, "\n#else /* K&R C */\n");
639 xdrfuncp = xdrfunc_head;
640 while (xdrfuncp != NULL)
642 print_xdr_func_def (xdrfuncp->name,
643 xdrfuncp->pointerp, i);
644 xdrfuncp = xdrfuncp->next;
647 fprintf (fout, "\n#endif /* K&R C */\n");
651 if (extend && tell == ftell (fout))
653 unlink (outfilename);
655 else if (tblflag)
657 fprintf (fout, rpcgen_table_dcl);
660 if (Cflag)
662 fprintf (fout, "\n#ifdef __cplusplus\n");
663 fprintf (fout, "}\n");
664 fprintf (fout, "#endif\n");
667 fprintf (fout, "\n#endif /* !_%s */\n", guard);
668 close_input ();
669 close_output (outfilename);
673 * Compile into an RPC service
675 static void
676 s_output (int argc, const char *argv[], const char *infile, const char *define,
677 int extend, const char *outfile, int nomain, int netflag)
679 char *include;
680 definition *def;
681 int foundprogram = 0;
682 const char *outfilename;
684 open_input (infile, define);
685 outfilename = extend ? extendfile (infile, outfile) : outfile;
686 open_output (infile, outfilename);
687 add_warning ();
688 if (infile && (include = extendfile (infile, ".h")))
690 fprintf (fout, "#include \"%s\"\n", include);
691 free (include);
693 else
694 fprintf (fout, "#include <rpc/rpc.h>\n");
696 fprintf (fout, "#include <stdio.h>\n");
697 fprintf (fout, "#include <stdlib.h>\n");
698 if (Cflag)
700 fprintf (fout, "#include <rpc/pmap_clnt.h>\n");
701 fprintf (fout, "#include <string.h>\n");
703 if (strcmp (svcclosetime, "-1") == 0)
704 indefinitewait = 1;
705 else if (strcmp (svcclosetime, "0") == 0)
706 exitnow = 1;
707 else if (inetdflag || pmflag)
709 fprintf (fout, "#include <signal.h>\n");
710 timerflag = 1;
713 if (!tirpcflag && inetdflag)
714 #ifdef __GNU_LIBRARY__
715 fprintf (fout, "#include <sys/ioctl.h> /* ioctl, TIOCNOTTY */\n");
716 #else
717 fprintf (fout, "#include <sys/ttycom.h>/* TIOCNOTTY */\n");
718 #endif
719 if (Cflag && (inetdflag || pmflag))
721 #ifdef __GNU_LIBRARY__
722 fprintf (fout, "#include <sys/types.h> /* open */\n");
723 fprintf (fout, "#include <sys/stat.h> /* open */\n");
724 fprintf (fout, "#include <fcntl.h> /* open */\n");
725 fprintf (fout, "#include <unistd.h> /* getdtablesize */\n");
726 #else
727 fprintf (fout, "#ifdef __cplusplus\n");
728 fprintf (fout, "#include <sysent.h> /* getdtablesize, open */\n");
729 fprintf (fout, "#endif /* __cplusplus */\n");
730 if (tirpcflag)
731 fprintf (fout, "#include <unistd.h> /* setsid */\n");
732 #endif
734 #ifdef __GNU_LIBRARY__
735 if (tirpcflag && !(Cflag && (inetdflag || pmflag)))
736 #else
737 if (tirpcflag)
738 #endif
739 fprintf (fout, "#include <sys/types.h>\n");
741 fprintf (fout, "#include <memory.h>\n");
742 #ifndef __GNU_LIBRARY__
743 fprintf (fout, "#include <stropts.h>\n");
744 #endif
745 if (inetdflag || !tirpcflag)
747 fprintf (fout, "#include <sys/socket.h>\n");
748 fprintf (fout, "#include <netinet/in.h>\n");
751 if ((netflag || pmflag) && tirpcflag && !nomain)
753 fprintf (fout, "#include <netconfig.h>\n");
755 if ( /*timerflag && */ tirpcflag)
756 fprintf (fout, "#include <sys/resource.h> /* rlimit */\n");
757 if (logflag || inetdflag || pmflag)
759 #ifdef __GNU_LIBRARY__
760 fprintf (fout, "#include <syslog.h>\n");
761 #else
762 fprintf (fout, "#ifdef SYSLOG\n");
763 fprintf (fout, "#include <syslog.h>\n");
764 fprintf (fout, "#else\n");
765 fprintf (fout, "#define LOG_ERR 1\n");
766 fprintf (fout, "#define openlog(a, b, c)\n");
767 fprintf (fout, "#endif\n");
768 #endif
771 /* for ANSI-C */
772 if (Cflag)
773 fprintf (fout, "\n#ifndef SIG_PF\n#define SIG_PF void(*)(int)\n#endif\n");
775 #ifndef __GNU_LIBRARY__
776 fprintf (fout, "\n#ifdef DEBUG\n#define RPC_SVC_FG\n#endif\n");
777 #endif
778 if (timerflag)
779 fprintf (fout, "\n#define _RPCSVC_CLOSEDOWN %s\n", svcclosetime);
780 while ((def = get_definition ()) != NULL)
782 foundprogram |= (def->def_kind == DEF_PROGRAM);
784 if (extend && !foundprogram)
786 unlink (outfilename);
787 return;
789 write_most (infile, netflag, nomain);
790 if (!nomain)
792 if (!do_registers (argc, argv))
794 if (outfilename)
795 unlink (outfilename);
796 usage ();
798 write_rest ();
800 close_input ();
801 close_output (outfilename);
805 * generate client side stubs
807 static void
808 l_output (const char *infile, const char *define, int extend,
809 const char *outfile)
811 char *include;
812 definition *def;
813 int foundprogram = 0;
814 const char *outfilename;
816 open_input (infile, define);
817 outfilename = extend ? extendfile (infile, outfile) : outfile;
818 open_output (infile, outfilename);
819 add_warning ();
820 if (Cflag)
821 fprintf (fout, "#include <memory.h> /* for memset */\n");
822 if (infile && (include = extendfile (infile, ".h")))
824 fprintf (fout, "#include \"%s\"\n", include);
825 free (include);
827 else
828 fprintf (fout, "#include <rpc/rpc.h>\n");
829 while ((def = get_definition ()) != NULL)
831 foundprogram |= (def->def_kind == DEF_PROGRAM);
833 if (extend && !foundprogram)
835 unlink (outfilename);
836 return;
838 write_stubs ();
839 close_input ();
840 close_output (outfilename);
844 * generate the dispatch table
846 static void
847 t_output (const char *infile, const char *define, int extend,
848 const char *outfile)
850 definition *def;
851 int foundprogram = 0;
852 const char *outfilename;
854 open_input (infile, define);
855 outfilename = extend ? extendfile (infile, outfile) : outfile;
856 open_output (infile, outfilename);
857 add_warning ();
858 while ((def = get_definition ()) != NULL)
860 foundprogram |= (def->def_kind == DEF_PROGRAM);
862 if (extend && !foundprogram)
864 unlink (outfilename);
865 return;
867 write_tables ();
868 close_input ();
869 close_output (outfilename);
872 /* sample routine for the server template */
873 static void
874 svc_output (const char *infile, const char *define, int extend,
875 const char *outfile)
877 definition *def;
878 char *include;
879 const char *outfilename;
880 long tell;
882 open_input (infile, define);
883 outfilename = extend ? extendfile (infile, outfile) : outfile;
884 checkfiles (infile, outfilename);
885 /*check if outfile already exists.
886 if so, print an error message and exit */
887 open_output (infile, outfilename);
888 add_sample_msg ();
890 if (infile && (include = extendfile (infile, ".h")))
892 fprintf (fout, "#include \"%s\"\n", include);
893 free (include);
895 else
896 fprintf (fout, "#include <rpc/rpc.h>\n");
898 tell = ftell (fout);
899 while ((def = get_definition ()) != NULL)
901 write_sample_svc (def);
903 if (extend && tell == ftell (fout))
905 unlink (outfilename);
907 close_input ();
908 close_output (outfilename);
912 /* sample main routine for client */
913 static void
914 clnt_output (const char *infile, const char *define, int extend,
915 const char *outfile)
917 definition *def;
918 char *include;
919 const char *outfilename;
920 long tell;
921 int has_program = 0;
923 open_input (infile, define);
924 outfilename = extend ? extendfile (infile, outfile) : outfile;
925 checkfiles (infile, outfilename);
926 /*check if outfile already exists.
927 if so, print an error message and exit */
929 open_output (infile, outfilename);
930 add_sample_msg ();
931 if (infile && (include = extendfile (infile, ".h")))
933 fprintf (fout, "#include \"%s\"\n", include);
934 free (include);
936 else
937 fprintf (fout, "#include <rpc/rpc.h>\n");
938 tell = ftell (fout);
939 while ((def = get_definition ()) != NULL)
941 has_program += write_sample_clnt (def);
944 if (has_program)
945 write_sample_clnt_main ();
947 if (extend && tell == ftell (fout))
949 unlink (outfilename);
951 close_input ();
952 close_output (outfilename);
955 static char *
956 file_name (const char *file, const char *ext)
958 char *temp;
959 temp = extendfile (file, ext);
961 if (access (temp, F_OK) != -1)
962 return (temp);
963 else
964 return ((char *) " ");
967 static void
968 mkfile_output (struct commandline *cmd)
970 char *mkfilename;
971 const char *clientname, *clntname, *xdrname, *hdrname;
972 const char *servername, *svcname, *servprogname, *clntprogname;
974 svcname = file_name (cmd->infile, "_svc.c");
975 clntname = file_name (cmd->infile, "_clnt.c");
976 xdrname = file_name (cmd->infile, "_xdr.c");
977 hdrname = file_name (cmd->infile, ".h");
979 if (allfiles)
981 servername = extendfile (cmd->infile, "_server.c");
982 clientname = extendfile (cmd->infile, "_client.c");
984 else
986 servername = " ";
987 clientname = " ";
989 servprogname = extendfile (cmd->infile, "_server");
990 clntprogname = extendfile (cmd->infile, "_client");
992 if (allfiles)
994 char *cp, *temp;
996 mkfilename = alloc (strlen ("Makefile.") + strlen (cmd->infile) + 1);
997 temp = rindex (cmd->infile, '.');
998 cp = stpcpy (mkfilename, "Makefile.");
999 strncpy (cp, cmd->infile, (temp - cmd->infile));
1001 else
1002 mkfilename = (char *) cmd->outfile;
1004 checkfiles (NULL, mkfilename);
1005 open_output (NULL, mkfilename);
1007 fprintf (fout, "\n# This is a template Makefile generated by rpcgen\n");
1009 f_print (fout, "\n# Parameters\n\n");
1011 f_print (fout, "CLIENT = %s\nSERVER = %s\n\n", clntprogname, servprogname);
1012 f_print (fout, "SOURCES_CLNT.c = \nSOURCES_CLNT.h = \n");
1013 f_print (fout, "SOURCES_SVC.c = \nSOURCES_SVC.h = \n");
1014 f_print (fout, "SOURCES.x = %s\n\n", cmd->infile);
1015 f_print (fout, "TARGETS_SVC.c = %s %s %s \n",
1016 svcname, servername, xdrname);
1017 f_print (fout, "TARGETS_CLNT.c = %s %s %s \n",
1018 clntname, clientname, xdrname);
1019 f_print (fout, "TARGETS = %s %s %s %s %s %s\n\n",
1020 hdrname, xdrname, clntname,
1021 svcname, clientname, servername);
1023 f_print (fout, "OBJECTS_CLNT = $(SOURCES_CLNT.c:%%.c=%%.o) \
1024 $(TARGETS_CLNT.c:%%.c=%%.o)");
1026 f_print (fout, "\nOBJECTS_SVC = $(SOURCES_SVC.c:%%.c=%%.o) \
1027 $(TARGETS_SVC.c:%%.c=%%.o)");
1029 f_print (fout, "\n# Compiler flags \n");
1030 if (mtflag)
1031 fprintf (fout, "\nCPPFLAGS += -D_REENTRANT\nCFLAGS += -g \nLDLIBS \
1032 += -lnsl -lpthread \n ");
1033 else
1034 f_print (fout, "\nCFLAGS += -g \nLDLIBS += -lnsl\n");
1035 f_print (fout, "RPCGENFLAGS = \n");
1037 f_print (fout, "\n# Targets \n\n");
1039 f_print (fout, "all : $(CLIENT) $(SERVER)\n\n");
1040 f_print (fout, "$(TARGETS) : $(SOURCES.x) \n");
1041 f_print (fout, "\trpcgen $(RPCGENFLAGS) $(SOURCES.x)\n\n");
1042 f_print (fout, "$(OBJECTS_CLNT) : $(SOURCES_CLNT.c) $(SOURCES_CLNT.h) \
1043 $(TARGETS_CLNT.c) \n\n");
1045 f_print (fout, "$(OBJECTS_SVC) : $(SOURCES_SVC.c) $(SOURCES_SVC.h) \
1046 $(TARGETS_SVC.c) \n\n");
1047 f_print (fout, "$(CLIENT) : $(OBJECTS_CLNT) \n");
1048 f_print (fout, "\t$(LINK.c) -o $(CLIENT) $(OBJECTS_CLNT) \
1049 $(LDLIBS) \n\n");
1050 f_print (fout, "$(SERVER) : $(OBJECTS_SVC) \n");
1051 f_print (fout, "\t$(LINK.c) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS)\n\n ");
1052 f_print (fout, "clean:\n\t $(RM) core $(TARGETS) $(OBJECTS_CLNT) \
1053 $(OBJECTS_SVC) $(CLIENT) $(SERVER)\n\n");
1054 close_output (mkfilename);
1058 * Perform registrations for service output
1059 * Return 0 if failed; 1 otherwise.
1061 static int
1062 do_registers (int argc, const char *argv[])
1064 int i;
1066 if (inetdflag || !tirpcflag)
1068 for (i = 1; i < argc; i++)
1070 if (streq (argv[i], "-s"))
1072 if (!check_nettype (argv[i + 1], valid_i_nettypes))
1073 return 0;
1074 write_inetd_register (argv[i + 1]);
1075 i++;
1079 else
1081 for (i = 1; i < argc; i++)
1082 if (streq (argv[i], "-s"))
1084 if (!check_nettype (argv[i + 1], valid_ti_nettypes))
1085 return 0;
1086 write_nettype_register (argv[i + 1]);
1087 i++;
1089 else if (streq (argv[i], "-n"))
1091 write_netid_register (argv[i + 1]);
1092 i++;
1095 return 1;
1099 * Add another argument to the arg list
1101 static void
1102 addarg (const char *cp)
1104 if (argcount >= ARGLISTLEN)
1106 fprintf (stderr, _("rpcgen: too many defines\n"));
1107 crash ();
1108 /*NOTREACHED */
1110 arglist[argcount++] = cp;
1113 static void
1114 putarg (int whereto, const char *cp)
1116 if (whereto >= ARGLISTLEN)
1118 fprintf (stderr, _("rpcgen: arglist coding error\n"));
1119 crash ();
1120 /*NOTREACHED */
1122 arglist[whereto] = cp;
1126 * if input file is stdin and an output file is specified then complain
1127 * if the file already exists. Otherwise the file may get overwritten
1128 * If input file does not exist, exit with an error
1131 static void
1132 checkfiles (const char *infile, const char *outfile)
1134 struct stat buf;
1136 if (infile) /* infile ! = NULL */
1137 if (stat (infile, &buf) < 0)
1139 perror (infile);
1140 crash ();
1142 if (outfile)
1144 if (stat (outfile, &buf) < 0)
1145 return; /* file does not exist */
1146 else
1148 fprintf (stderr,
1149 /* TRANS: the file will not be removed; this is an
1150 TRANS: informative message. */
1151 _("file `%s' already exists and may be overwritten\n"),
1152 outfile);
1153 crash ();
1159 * Parse command line arguments
1161 static int
1162 parseargs (int argc, const char *argv[], struct commandline *cmd)
1164 int i;
1165 int j;
1166 int c;
1167 char flag[(1 << 8 * sizeof (char))];
1168 int nflags;
1170 cmdname = argv[0];
1171 cmd->infile = cmd->outfile = NULL;
1172 if (argc < 2)
1174 return (0);
1176 allfiles = 0;
1177 flag['c'] = 0;
1178 flag['h'] = 0;
1179 flag['l'] = 0;
1180 flag['m'] = 0;
1181 flag['o'] = 0;
1182 flag['s'] = 0;
1183 flag['n'] = 0;
1184 flag['t'] = 0;
1185 flag['S'] = 0;
1186 flag['C'] = 0;
1187 flag['M'] = 0;
1189 for (i = 1; i < argc; i++)
1191 if (argv[i][0] != '-')
1193 if (cmd->infile)
1195 fprintf (stderr,
1196 _("Cannot specify more than one input file!\n"));
1197 return 0;
1199 cmd->infile = argv[i];
1201 else
1203 for (j = 1; argv[i][j] != 0; j++)
1205 c = argv[i][j];
1206 switch (c)
1208 case 'a':
1209 allfiles = 1;
1210 break;
1211 case 'c':
1212 case 'h':
1213 case 'l':
1214 case 'm':
1215 case 't':
1216 if (flag[c])
1217 return 0;
1218 flag[c] = 1;
1219 break;
1220 case 'S':
1221 /* sample flag: Ss or Sc.
1222 Ss means set flag['S'];
1223 Sc means set flag['C'];
1224 Sm means set flag['M']; */
1225 c = argv[i][++j]; /* get next char */
1226 if (c == 's')
1227 c = 'S';
1228 else if (c == 'c')
1229 c = 'C';
1230 else if (c == 'm')
1231 c = 'M';
1232 else
1233 return 0;
1235 if (flag[c])
1236 return 0;
1237 flag[c] = 1;
1238 break;
1239 case 'C': /* ANSI C syntax */
1240 Cflag = 1;
1241 break;
1243 #ifdef __GNU_LIBRARY__
1244 case 'k': /* K&R C syntax */
1245 Cflag = 0;
1246 break;
1248 #endif
1249 case 'b': /* turn TIRPC flag off for
1250 generating backward compatible
1252 tirpcflag = 0;
1253 break;
1255 #ifdef __GNU_LIBRARY__
1256 case '5': /* turn TIRPC flag on for
1257 generating SysVr4 compatible
1259 tirpcflag = 1;
1260 break;
1261 #endif
1262 case 'I':
1263 inetdflag = 1;
1264 break;
1265 case 'N':
1266 newstyle = 1;
1267 break;
1268 case 'L':
1269 logflag = 1;
1270 break;
1271 case 'K':
1272 if (++i == argc)
1274 return (0);
1276 svcclosetime = argv[i];
1277 goto nextarg;
1278 case 'T':
1279 tblflag = 1;
1280 break;
1281 case 'M':
1282 mtflag = 1;
1283 break;
1284 case 'i':
1285 if (++i == argc)
1287 return (0);
1289 inlineflag = atoi (argv[i]);
1290 goto nextarg;
1291 case 'n':
1292 case 'o':
1293 case 's':
1294 if (argv[i][j - 1] != '-' ||
1295 argv[i][j + 1] != 0)
1297 return (0);
1299 flag[c] = 1;
1300 if (++i == argc)
1302 return (0);
1304 if (c == 's')
1306 if (!streq (argv[i], "udp") &&
1307 !streq (argv[i], "tcp"))
1308 return 0;
1310 else if (c == 'o')
1312 if (cmd->outfile)
1313 return 0;
1314 cmd->outfile = argv[i];
1316 goto nextarg;
1317 case 'D':
1318 if (argv[i][j - 1] != '-')
1319 return 0;
1320 addarg (argv[i]);
1321 goto nextarg;
1322 case 'Y':
1323 if (++i == argc)
1324 return 0;
1326 size_t len = strlen (argv[i]);
1327 pathbuf = malloc (len + 5);
1328 if (pathbuf == NULL)
1330 perror (cmdname);
1331 crash ();
1333 stpcpy (stpcpy (pathbuf,
1334 argv[i]),
1335 "/cpp");
1336 CPP = pathbuf;
1337 cppDefined = 1;
1338 goto nextarg;
1341 default:
1342 return 0;
1345 nextarg:
1350 cmd->cflag = flag['c'];
1351 cmd->hflag = flag['h'];
1352 cmd->lflag = flag['l'];
1353 cmd->mflag = flag['m'];
1354 cmd->nflag = flag['n'];
1355 cmd->sflag = flag['s'];
1356 cmd->tflag = flag['t'];
1357 cmd->Ssflag = flag['S'];
1358 cmd->Scflag = flag['C'];
1359 cmd->makefileflag = flag['M'];
1361 #ifndef _RPC_THREAD_SAFE_
1362 if (mtflag || newstyle)
1364 /* glibc doesn't support these flags. */
1365 f_print (stderr,
1366 _("This implementation doesn't support newstyle or MT-safe code!\n"));
1367 return (0);
1369 #endif
1370 if (tirpcflag)
1372 pmflag = inetdflag ? 0 : 1; /* pmflag or inetdflag is always TRUE */
1373 if ((inetdflag && cmd->nflag))
1374 { /* netid not allowed with inetdflag */
1375 fprintf (stderr, _("Cannot use netid flag with inetd flag!\n"));
1376 return 0;
1379 else
1380 { /* 4.1 mode */
1381 pmflag = 0; /* set pmflag only in tirpcmode */
1382 #ifndef __GNU_LIBRARY__
1383 inetdflag = 1; /* inetdflag is TRUE by default */
1384 #endif
1385 if (cmd->nflag)
1386 { /* netid needs TIRPC */
1387 f_print (stderr, _("Cannot use netid flag without TIRPC!\n"));
1388 return (0);
1392 if (newstyle && (tblflag || cmd->tflag))
1394 f_print (stderr, _("Cannot use table flags with newstyle!\n"));
1395 return (0);
1398 /* check no conflicts with file generation flags */
1399 nflags = cmd->cflag + cmd->hflag + cmd->lflag + cmd->mflag +
1400 cmd->sflag + cmd->nflag + cmd->tflag + cmd->Ssflag + cmd->Scflag;
1402 if (nflags == 0)
1404 if (cmd->outfile != NULL || cmd->infile == NULL)
1406 return (0);
1409 else if (cmd->infile == NULL &&
1410 (cmd->Ssflag || cmd->Scflag || cmd->makefileflag))
1412 fprintf (stderr,
1413 _("\"infile\" is required for template generation flags.\n"));
1414 return 0;
1416 if (nflags > 1)
1418 fprintf (stderr, _("Cannot have more than one file generation flag!\n"));
1419 return 0;
1421 return 1;
1424 static void
1425 usage (void)
1427 fprintf (stderr, _("usage: %s infile\n"), cmdname);
1428 fprintf (stderr, _("\t%s [-abkCLNTM][-Dname[=value]] [-i size] \
1429 [-I [-K seconds]] [-Y path] infile\n"), cmdname);
1430 fprintf (stderr, _("\t%s [-c | -h | -l | -m | -t | -Sc | -Ss | -Sm] \
1431 [-o outfile] [infile]\n"), cmdname);
1432 fprintf (stderr, _("\t%s [-s nettype]* [-o outfile] [infile]\n"), cmdname);
1433 fprintf (stderr, _("\t%s [-n netid]* [-o outfile] [infile]\n"), cmdname);
1434 options_usage ();
1435 exit (1);
1438 static void
1439 options_usage (void)
1441 f_print (stderr, "options:\n");
1442 f_print (stderr, "-a\t\tgenerate all files, including samples\n");
1443 f_print (stderr, "-b\t\tbackward compatibility mode (generates code for SunOS 4.1)\n");
1444 f_print (stderr, "-c\t\tgenerate XDR routines\n");
1445 f_print (stderr, "-C\t\tANSI C mode\n");
1446 f_print (stderr, "-Dname[=value]\tdefine a symbol (same as #define)\n");
1447 f_print (stderr, "-h\t\tgenerate header file\n");
1448 f_print (stderr, "-i size\t\tsize at which to start generating inline code\n");
1449 f_print (stderr, "-I\t\tgenerate code for inetd support in server (for SunOS 4.1)\n");
1450 f_print (stderr, "-K seconds\tserver exits after K seconds of inactivity\n");
1451 f_print (stderr, "-l\t\tgenerate client side stubs\n");
1452 f_print (stderr, "-L\t\tserver errors will be printed to syslog\n");
1453 f_print (stderr, "-m\t\tgenerate server side stubs\n");
1454 f_print (stderr, "-M\t\tgenerate MT-safe code\n");
1455 f_print (stderr, "-n netid\tgenerate server code that supports named netid\n");
1456 f_print (stderr, "-N\t\tsupports multiple arguments and call-by-value\n");
1457 f_print (stderr, "-o outfile\tname of the output file\n");
1458 f_print (stderr, "-s nettype\tgenerate server code that supports named nettype\n");
1459 f_print (stderr, "-Sc\t\tgenerate sample client code that uses remote procedures\n");
1460 f_print (stderr, "-Ss\t\tgenerate sample server code that defines remote procedures\n");
1461 f_print (stderr, "-Sm \t\tgenerate makefile template \n");
1462 f_print (stderr, "-t\t\tgenerate RPC dispatch table\n");
1463 f_print (stderr, "-T\t\tgenerate code to support RPC dispatch tables\n");
1464 f_print (stderr, "-Y path\t\tdirectory name to find C preprocessor (cpp)\n");
1466 exit (1);