Correctly handle m68k long double format.
[glibc/pb-stable.git] / sunrpc / rpc_main.c
blobd43b69d8bc73be4a56230a2d0af8b547d79252c6
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 <libintl.h>
46 #include <ctype.h>
47 #include <sys/types.h>
48 #include <sys/param.h>
49 #include <sys/file.h>
50 #include <sys/stat.h>
51 #include <sys/wait.h>
52 #include "rpc_parse.h"
53 #include "rpc_util.h"
54 #include "rpc_scan.h"
55 #include "proto.h"
57 #define EXTEND 1 /* alias for TRUE */
58 #define DONT_EXTEND 0 /* alias for FALSE */
60 struct commandline
62 int cflag; /* xdr C routines */
63 int hflag; /* header file */
64 int lflag; /* client side stubs */
65 int mflag; /* server side stubs */
66 int nflag; /* netid flag */
67 int sflag; /* server stubs for the given transport */
68 int tflag; /* dispatch Table file */
69 int Ssflag; /* produce server sample code */
70 int Scflag; /* produce client sample code */
71 int makefileflag; /* Generate a template Makefile */
72 const char *infile; /* input module name */
73 const char *outfile; /* output module name */
77 static const char *cmdname;
79 #define SVR4_CPP "/usr/ccs/lib/cpp"
80 #define SUNOS_CPP "/lib/cpp"
82 static const char *svcclosetime = "120";
83 static int cppDefined = 0; /* explicit path for C preprocessor */
84 static const char *CPP = SUNOS_CPP;
85 static char CPPFLAGS[] = "-C";
86 static char *pathbuf;
87 static int cpp_pid;
88 static const char *allv[] =
90 "rpcgen", "-s", "udp", "-s", "tcp"
92 static int allc = sizeof (allv) / sizeof (allv[0]);
93 static const char *allnv[] =
95 "rpcgen", "-s", "netpath",
97 static int allnc = sizeof (allnv) / sizeof (allnv[0]);
100 * machinations for handling expanding argument list
102 static void addarg (const char *); /* add another argument to the list */
103 static void putarg (int, const char *); /* put argument at specified location */
104 static void clear_args (void); /* clear argument list */
105 static void checkfiles (const char *, const char *);
106 /* check if out file already exists */
108 static void clear_args (void);
109 static char *extendfile (const char *file, const char *ext);
110 static void open_output (const char *infile, const char *outfile);
111 static void add_warning (void);
112 static void clear_args (void);
113 static void find_cpp (void);
114 static void open_input (const char *infile, const char *define);
115 static int check_nettype (const char *name, const char *list_to_check[]);
116 static void c_output (const char *infile, const char *define,
117 int extend, const char *outfile);
118 static void h_output (const char *infile, const char *define,
119 int extend, const char *outfile);
120 static void s_output (int argc, const char *argv[], const char *infile,
121 const char *define, int extend,
122 const char *outfile, int nomain, int netflag);
123 static void l_output (const char *infile, const char *define,
124 int extend, const char *outfile);
125 static void t_output (const char *infile, const char *define,
126 int extend, const char *outfile);
127 static void svc_output (const char *infile, const char *define,
128 int extend, const char *outfile);
129 static void clnt_output (const char *infile, const char *define,
130 int extend, const char *outfile);
131 static void mkfile_output (struct commandline *cmd);
132 static int do_registers (int argc, const char *argv[]);
133 static void addarg (const char *cp);
134 static void putarg (int whereto, const char *cp);
135 static void checkfiles (const char *infile, const char *outfile);
136 static int parseargs (int argc, const char *argv[], struct commandline *cmd);
137 static void usage (void) __attribute__ ((noreturn));
138 static void options_usage (void) __attribute__ ((noreturn));
139 static void c_initialize (void);
140 static char *generate_guard (const char *pathname);
143 #define ARGLISTLEN 20
144 #define FIXEDARGS 2
146 static const char *arglist[ARGLISTLEN];
147 static int argcount = FIXEDARGS;
150 int nonfatalerrors; /* errors */
151 int inetdflag /* = 1 */ ; /* Support for inetd *//* is now the default */
152 int pmflag; /* Support for port monitors */
153 int logflag; /* Use syslog instead of fprintf for errors */
154 int tblflag; /* Support for dispatch table file */
155 int mtflag; /* Support for MT */
157 #define INLINE 3
158 /*length at which to start doing an inline */
160 int inlineflag = INLINE; /* length at which to start doing an inline. 3 = default
161 if 0, no xdr_inline code */
163 int indefinitewait; /* If started by port monitors, hang till it wants */
164 int exitnow; /* If started by port monitors, exit after the call */
165 int timerflag; /* TRUE if !indefinite && !exitnow */
166 int newstyle; /* newstyle of passing arguments (by value) */
167 #ifdef __GNU_LIBRARY__
168 int Cflag = 1; /* ANSI C syntax */
169 #else
170 int Cflag = 0; /* ANSI C/C++ syntax */
171 #endif
172 int CCflag = 0; /* C++ files */
173 static int allfiles; /* generate all files */
174 #ifdef __GNU_LIBRARY__
175 int tirpcflag = 0; /* generating code for tirpc, by default */
176 #else
177 int tirpcflag = 1; /* generating code for tirpc, by default */
178 #endif
179 xdrfunc *xdrfunc_head = NULL; /* xdr function list */
180 xdrfunc *xdrfunc_tail = NULL; /* xdr function list */
183 __attribute__ ((noreturn))
184 main (int argc, const char *argv[])
186 struct commandline cmd;
188 (void) memset ((char *) &cmd, 0, sizeof (struct commandline));
189 clear_args ();
190 if (!parseargs (argc, argv, &cmd))
191 usage ();
193 if (cmd.cflag || cmd.hflag || cmd.lflag || cmd.tflag || cmd.sflag ||
194 cmd.mflag || cmd.nflag || cmd.Ssflag || cmd.Scflag)
196 checkfiles (cmd.infile, cmd.outfile);
198 else
199 checkfiles (cmd.infile, NULL);
201 if (cmd.cflag)
202 c_output (cmd.infile, "-DRPC_XDR", DONT_EXTEND, cmd.outfile);
203 else if (cmd.hflag)
204 h_output (cmd.infile, "-DRPC_HDR", DONT_EXTEND, cmd.outfile);
205 else if (cmd.lflag)
206 l_output (cmd.infile, "-DRPC_CLNT", DONT_EXTEND, cmd.outfile);
207 else if (cmd.sflag || cmd.mflag || (cmd.nflag))
208 s_output (argc, argv, cmd.infile, "-DRPC_SVC", DONT_EXTEND,
209 cmd.outfile, cmd.mflag, cmd.nflag);
210 else if (cmd.tflag)
211 t_output (cmd.infile, "-DRPC_TBL", DONT_EXTEND, cmd.outfile);
212 else if (cmd.Ssflag)
213 svc_output (cmd.infile, "-DRPC_SERVER", DONT_EXTEND, cmd.outfile);
214 else if (cmd.Scflag)
215 clnt_output (cmd.infile, "-DRPC_CLIENT", DONT_EXTEND, cmd.outfile);
216 else if (cmd.makefileflag)
217 mkfile_output (&cmd);
218 else
220 /* the rescans are required, since cpp may effect input */
221 c_output (cmd.infile, "-DRPC_XDR", EXTEND, "_xdr.c");
222 reinitialize ();
223 h_output (cmd.infile, "-DRPC_HDR", EXTEND, ".h");
224 reinitialize ();
225 l_output (cmd.infile, "-DRPC_CLNT", EXTEND, "_clnt.c");
226 reinitialize ();
227 if (inetdflag || !tirpcflag)
228 s_output (allc, allv, cmd.infile, "-DRPC_SVC", EXTEND,
229 "_svc.c", cmd.mflag, cmd.nflag);
230 else
231 s_output (allnc, allnv, cmd.infile, "-DRPC_SVC",
232 EXTEND, "_svc.c", cmd.mflag, cmd.nflag);
233 if (tblflag)
235 reinitialize ();
236 t_output (cmd.infile, "-DRPC_TBL", EXTEND, "_tbl.i");
238 if (allfiles)
240 reinitialize ();
241 svc_output (cmd.infile, "-DRPC_SERVER", EXTEND, "_server.c");
242 reinitialize ();
243 clnt_output (cmd.infile, "-DRPC_CLIENT", EXTEND, "_client.c");
245 if (allfiles || (cmd.makefileflag == 1))
247 reinitialize ();
248 mkfile_output (&cmd);
251 exit (nonfatalerrors);
252 /* NOTREACHED */
256 * add extension to filename
258 static char *
259 extendfile (const char *file, const char *ext)
261 char *res;
262 const char *p;
264 res = alloc (strlen (file) + strlen (ext) + 1);
265 if (res == NULL)
266 abort ();
267 p = strrchr (file, '.');
268 if (p == NULL)
269 p = file + strlen (file);
270 strcpy (res, file);
271 strcpy (res + (p - file), ext);
272 return res;
276 * Open output file with given extension
278 static void
279 open_output (const char *infile, const char *outfile)
281 if (outfile == NULL)
283 fout = stdout;
284 return;
287 if (infile != NULL && streq (outfile, infile))
289 fprintf (stderr, _ ("%s: output would overwrite %s\n"), cmdname,
290 infile);
291 crash ();
293 fout = fopen (outfile, "w");
294 if (fout == NULL)
296 fprintf (stderr, _ ("%s: unable to open %s: %m\n"), cmdname, outfile);
297 crash ();
299 record_open (outfile);
302 /* Close the output file and check for write errors. */
303 static void
304 close_output (const char *outfile)
306 if (fclose (fout) == EOF)
308 fprintf (stderr, _("%s: while writing output %s: %m"), cmdname,
309 outfile ?: "<stdout>");
310 crash ();
314 static void
315 add_warning (void)
317 fprintf (fout, "/*\n");
318 fprintf (fout, " * Please do not edit this file.\n");
319 fprintf (fout, " * It was generated using rpcgen.\n");
320 fprintf (fout, " */\n\n");
323 /* clear list of arguments */
324 static void
325 clear_args (void)
327 int i;
328 for (i = FIXEDARGS; i < ARGLISTLEN; ++i)
329 arglist[i] = NULL;
330 argcount = FIXEDARGS;
333 /* make sure that a CPP exists */
334 static void
335 find_cpp (void)
337 struct stat buf;
339 if (stat (CPP, &buf) < 0)
340 { /* /lib/cpp or explicit cpp does not exist */
341 if (cppDefined)
343 fprintf (stderr, _ ("cannot find C preprocessor: %s \n"), CPP);
344 crash ();
346 else
347 { /* try the other one */
348 CPP = SVR4_CPP;
349 if (stat (CPP, &buf) < 0)
350 { /* can't find any cpp */
351 fputs (_ ("cannot find any C preprocessor (cpp)\n"), stdout);
352 crash ();
359 * Open input file with given define for C-preprocessor
361 static void
362 open_input (const char *infile, const char *define)
364 int pd[2];
366 infilename = (infile == NULL) ? "<stdin>" : infile;
367 if (pipe (pd) != 0)
369 perror ("pipe");
370 exit (1);
372 cpp_pid = fork ();
373 switch (cpp_pid)
375 case 0:
376 find_cpp ();
377 putarg (0, CPP);
378 putarg (1, CPPFLAGS);
379 addarg (define);
380 if (infile)
381 addarg (infile);
382 addarg ((char *) NULL);
383 close (1);
384 dup2 (pd[1], 1);
385 close (pd[0]);
386 execv (arglist[0], (char **) arglist);
387 perror ("execv");
388 exit (1);
389 case -1:
390 perror ("fork");
391 exit (1);
393 close (pd[1]);
394 fin = fdopen (pd[0], "r");
395 if (fin == NULL)
397 fprintf (stderr, "%s: ", cmdname);
398 perror (infilename);
399 crash ();
403 /* Close the connection to the C-preprocessor and check for successfull
404 termination. */
405 static void
406 close_input (void)
408 int status;
410 fclose (fin);
411 /* Check the termination status. */
412 if (waitpid (cpp_pid, &status, 0) < 0)
414 perror ("waitpid");
415 crash ();
417 if (WIFSIGNALED (status) || WEXITSTATUS (status) != 0)
419 if (WIFSIGNALED (status))
420 fprintf (stderr, _("%s: C preprocessor failed with signal %d\n"),
421 cmdname, WTERMSIG (status));
422 else
423 fprintf (stderr, _("%s: C preprocessor failed with exit code %d\n"),
424 cmdname, WEXITSTATUS (status));
425 crash ();
429 /* valid tirpc nettypes */
430 static const char *valid_ti_nettypes[] =
432 "netpath",
433 "visible",
434 "circuit_v",
435 "datagram_v",
436 "circuit_n",
437 "datagram_n",
438 "udp",
439 "tcp",
440 "raw",
441 NULL
444 /* valid inetd nettypes */
445 static const char *valid_i_nettypes[] =
447 "udp",
448 "tcp",
449 NULL
452 static int
453 check_nettype (const char *name, const char *list_to_check[])
455 int i;
456 for (i = 0; list_to_check[i] != NULL; i++)
458 if (strcmp (name, list_to_check[i]) == 0)
460 return 1;
463 fprintf (stderr, _ ("illegal nettype :`%s'\n"), name);
464 return 0;
468 * Compile into an XDR routine output file
471 static void
472 c_output (const char *infile, const char *define, int extend,
473 const char *outfile)
475 definition *def;
476 char *include;
477 const char *outfilename;
478 long tell;
480 c_initialize ();
481 open_input (infile, define);
482 outfilename = extend ? extendfile (infile, outfile) : outfile;
483 open_output (infile, outfilename);
484 add_warning ();
485 if (infile && (include = extendfile (infile, ".h")))
487 fprintf (fout, "#include \"%s\"\n", include);
488 free (include);
489 /* .h file already contains rpc/rpc.h */
491 else
492 fprintf (fout, "#include <rpc/rpc.h>\n");
493 tell = ftell (fout);
494 while ((def = get_definition ()) != NULL)
495 emit (def);
497 if (extend && tell == ftell (fout))
498 unlink (outfilename);
499 close_input ();
500 close_output (outfilename);
503 void
504 c_initialize (void)
507 /* add all the starting basic types */
509 add_type (1, "int");
510 add_type (1, "long");
511 add_type (1, "short");
512 add_type (1, "bool");
514 add_type (1, "u_int");
515 add_type (1, "u_long");
516 add_type (1, "u_short");
520 char rpcgen_table_dcl[] = "struct rpcgen_table {\n\
521 char *(*proc)();\n\
522 xdrproc_t xdr_arg;\n\
523 unsigned len_arg;\n\
524 xdrproc_t xdr_res;\n\
525 unsigned len_res;\n\
526 };\n";
529 static char *
530 generate_guard (const char *pathname)
532 const char *filename;
533 char *guard, *tmp;
535 filename = strrchr (pathname, '/'); /* find last component */
536 filename = ((filename == NULL) ? pathname : filename + 1);
537 guard = strdup (filename);
538 /* convert to upper case */
539 tmp = guard;
540 while (*tmp)
542 if (islower (*tmp))
543 *tmp = toupper (*tmp);
544 tmp++;
547 guard = extendfile (guard, "_H_RPCGEN");
548 return guard;
552 * Compile into an XDR header file
556 static void
557 h_output (const char *infile, const char *define, int extend,
558 const char *outfile)
560 xdrfunc *xdrfuncp;
561 definition *def;
562 const char *ifilename;
563 const char *outfilename;
564 long tell;
565 char *guard;
566 list *l;
568 open_input (infile, define);
569 outfilename = extend ? extendfile (infile, outfile) : outfile;
570 open_output (infile, outfilename);
571 add_warning ();
572 ifilename = (infile == NULL) ? "STDIN" : infile;
573 guard = generate_guard (outfilename ? outfilename : ifilename);
575 fprintf (fout, "#ifndef _%s\n#define _%s\n\n", guard,
576 guard);
578 fprintf (fout, "#include <rpc/rpc.h>\n\n");
580 if (mtflag)
582 fprintf (fout, "#include <pthread.h>\n");
585 /* put the C++ support */
586 if (Cflag && !CCflag)
588 fprintf (fout, "\n#ifdef __cplusplus\n");
589 fprintf (fout, "extern \"C\" {\n");
590 fprintf (fout, "#endif\n\n");
593 tell = ftell (fout);
594 /* print data definitions */
595 while ((def = get_definition ()) != NULL)
597 print_datadef (def);
600 /* print function declarations.
601 Do this after data definitions because they might be used as
602 arguments for functions */
603 for (l = defined; l != NULL; l = l->next)
605 print_funcdef (l->val);
607 /* Now print all xdr func declarations */
608 if (xdrfunc_head != NULL)
610 fprintf (fout, "\n/* the xdr functions */\n");
611 if (CCflag)
613 fprintf (fout, "\n#ifdef __cplusplus\n");
614 fprintf (fout, "extern \"C\" {\n");
615 fprintf (fout, "#endif\n");
617 if (!Cflag)
619 xdrfuncp = xdrfunc_head;
620 while (xdrfuncp != NULL)
622 print_xdr_func_def (xdrfuncp->name,
623 xdrfuncp->pointerp, 2);
624 xdrfuncp = xdrfuncp->next;
627 else
629 int i;
631 for (i = 1; i < 3; ++i)
633 if (i == 1)
634 fprintf (fout, "\n#if defined(__STDC__) || defined(__cplusplus)\n");
635 else
636 fprintf (fout, "\n#else /* K&R C */\n");
638 xdrfuncp = xdrfunc_head;
639 while (xdrfuncp != NULL)
641 print_xdr_func_def (xdrfuncp->name,
642 xdrfuncp->pointerp, i);
643 xdrfuncp = xdrfuncp->next;
646 fprintf (fout, "\n#endif /* K&R C */\n");
650 if (extend && tell == ftell (fout))
652 unlink (outfilename);
654 else if (tblflag)
656 fprintf (fout, rpcgen_table_dcl);
659 if (Cflag)
661 fprintf (fout, "\n#ifdef __cplusplus\n");
662 fprintf (fout, "}\n");
663 fprintf (fout, "#endif\n");
666 fprintf (fout, "\n#endif /* !_%s */\n", guard);
667 close_input ();
668 close_output (outfilename);
672 * Compile into an RPC service
674 static void
675 s_output (int argc, const char *argv[], const char *infile, const char *define,
676 int extend, const char *outfile, int nomain, int netflag)
678 char *include;
679 definition *def;
680 int foundprogram = 0;
681 const char *outfilename;
683 open_input (infile, define);
684 outfilename = extend ? extendfile (infile, outfile) : outfile;
685 open_output (infile, outfilename);
686 add_warning ();
687 if (infile && (include = extendfile (infile, ".h")))
689 fprintf (fout, "#include \"%s\"\n", include);
690 free (include);
692 else
693 fprintf (fout, "#include <rpc/rpc.h>\n");
695 fprintf (fout, "#include <stdio.h>\n");
696 fprintf (fout, "#include <stdlib.h>\n");
697 if (Cflag)
699 fprintf (fout, "#include <rpc/pmap_clnt.h>\n");
700 fprintf (fout, "#include <string.h>\n");
702 if (strcmp (svcclosetime, "-1") == 0)
703 indefinitewait = 1;
704 else if (strcmp (svcclosetime, "0") == 0)
705 exitnow = 1;
706 else if (inetdflag || pmflag)
708 fprintf (fout, "#include <signal.h>\n");
709 timerflag = 1;
712 if (!tirpcflag && inetdflag)
713 #ifdef __GNU_LIBRARY__
714 fprintf (fout, "#include <sys/ioctl.h> /* ioctl, TIOCNOTTY */\n");
715 #else
716 fprintf (fout, "#include <sys/ttycom.h>/* TIOCNOTTY */\n");
717 #endif
718 if (Cflag && (inetdflag || pmflag))
720 #ifdef __GNU_LIBRARY__
721 fprintf (fout, "#include <sys/types.h> /* open */\n");
722 fprintf (fout, "#include <sys/stat.h> /* open */\n");
723 fprintf (fout, "#include <fcntl.h> /* open */\n");
724 fprintf (fout, "#include <unistd.h> /* getdtablesize */\n");
725 #else
726 fprintf (fout, "#ifdef __cplusplus\n");
727 fprintf (fout, "#include <sysent.h> /* getdtablesize, open */\n");
728 fprintf (fout, "#endif /* __cplusplus */\n");
729 if (tirpcflag)
730 fprintf (fout, "#include <unistd.h> /* setsid */\n");
731 #endif
733 #ifdef __GNU_LIBRARY__
734 if (tirpcflag && !(Cflag && (inetdflag || pmflag)))
735 #else
736 if (tirpcflag)
737 #endif
738 fprintf (fout, "#include <sys/types.h>\n");
740 fprintf (fout, "#include <memory.h>\n");
741 #ifndef __GNU_LIBRARY__
742 fprintf (fout, "#include <stropts.h>\n");
743 #endif
744 if (inetdflag || !tirpcflag)
746 fprintf (fout, "#include <sys/socket.h>\n");
747 fprintf (fout, "#include <netinet/in.h>\n");
750 if ((netflag || pmflag) && tirpcflag && !nomain)
752 fprintf (fout, "#include <netconfig.h>\n");
754 if ( /*timerflag && */ tirpcflag)
755 fprintf (fout, "#include <sys/resource.h> /* rlimit */\n");
756 if (logflag || inetdflag || pmflag)
758 #ifdef __GNU_LIBRARY__
759 fprintf (fout, "#include <syslog.h>\n");
760 #else
761 fprintf (fout, "#ifdef SYSLOG\n");
762 fprintf (fout, "#include <syslog.h>\n");
763 fprintf (fout, "#else\n");
764 fprintf (fout, "#define LOG_ERR 1\n");
765 fprintf (fout, "#define openlog(a, b, c)\n");
766 fprintf (fout, "#endif\n");
767 #endif
770 /* for ANSI-C */
771 if (Cflag)
772 fprintf (fout, "\n#ifndef SIG_PF\n#define SIG_PF void(*)(int)\n#endif\n");
774 #ifndef __GNU_LIBRARY__
775 fprintf (fout, "\n#ifdef DEBUG\n#define RPC_SVC_FG\n#endif\n");
776 #endif
777 if (timerflag)
778 fprintf (fout, "\n#define _RPCSVC_CLOSEDOWN %s\n", svcclosetime);
779 while ((def = get_definition ()) != NULL)
781 foundprogram |= (def->def_kind == DEF_PROGRAM);
783 if (extend && !foundprogram)
785 unlink (outfilename);
786 return;
788 write_most (infile, netflag, nomain);
789 if (!nomain)
791 if (!do_registers (argc, argv))
793 if (outfilename)
794 unlink (outfilename);
795 usage ();
797 write_rest ();
799 close_input ();
800 close_output (outfilename);
804 * generate client side stubs
806 static void
807 l_output (const char *infile, const char *define, int extend,
808 const char *outfile)
810 char *include;
811 definition *def;
812 int foundprogram = 0;
813 const char *outfilename;
815 open_input (infile, define);
816 outfilename = extend ? extendfile (infile, outfile) : outfile;
817 open_output (infile, outfilename);
818 add_warning ();
819 if (Cflag)
820 fprintf (fout, "#include <memory.h> /* for memset */\n");
821 if (infile && (include = extendfile (infile, ".h")))
823 fprintf (fout, "#include \"%s\"\n", include);
824 free (include);
826 else
827 fprintf (fout, "#include <rpc/rpc.h>\n");
828 while ((def = get_definition ()) != NULL)
830 foundprogram |= (def->def_kind == DEF_PROGRAM);
832 if (extend && !foundprogram)
834 unlink (outfilename);
835 return;
837 write_stubs ();
838 close_input ();
839 close_output (outfilename);
843 * generate the dispatch table
845 static void
846 t_output (const char *infile, const char *define, int extend,
847 const char *outfile)
849 definition *def;
850 int foundprogram = 0;
851 const char *outfilename;
853 open_input (infile, define);
854 outfilename = extend ? extendfile (infile, outfile) : outfile;
855 open_output (infile, outfilename);
856 add_warning ();
857 while ((def = get_definition ()) != NULL)
859 foundprogram |= (def->def_kind == DEF_PROGRAM);
861 if (extend && !foundprogram)
863 unlink (outfilename);
864 return;
866 write_tables ();
867 close_input ();
868 close_output (outfilename);
871 /* sample routine for the server template */
872 static void
873 svc_output (const char *infile, const char *define, int extend,
874 const char *outfile)
876 definition *def;
877 char *include;
878 const char *outfilename;
879 long tell;
881 open_input (infile, define);
882 outfilename = extend ? extendfile (infile, outfile) : outfile;
883 checkfiles (infile, outfilename);
884 /*check if outfile already exists.
885 if so, print an error message and exit */
886 open_output (infile, outfilename);
887 add_sample_msg ();
889 if (infile && (include = extendfile (infile, ".h")))
891 fprintf (fout, "#include \"%s\"\n", include);
892 free (include);
894 else
895 fprintf (fout, "#include <rpc/rpc.h>\n");
897 tell = ftell (fout);
898 while ((def = get_definition ()) != NULL)
900 write_sample_svc (def);
902 if (extend && tell == ftell (fout))
904 unlink (outfilename);
906 close_input ();
907 close_output (outfilename);
911 /* sample main routine for client */
912 static void
913 clnt_output (const char *infile, const char *define, int extend,
914 const char *outfile)
916 definition *def;
917 char *include;
918 const char *outfilename;
919 long tell;
920 int has_program = 0;
922 open_input (infile, define);
923 outfilename = extend ? extendfile (infile, outfile) : outfile;
924 checkfiles (infile, outfilename);
925 /*check if outfile already exists.
926 if so, print an error message and exit */
928 open_output (infile, outfilename);
929 add_sample_msg ();
930 if (infile && (include = extendfile (infile, ".h")))
932 fprintf (fout, "#include \"%s\"\n", include);
933 free (include);
935 else
936 fprintf (fout, "#include <rpc/rpc.h>\n");
937 tell = ftell (fout);
938 while ((def = get_definition ()) != NULL)
940 has_program += write_sample_clnt (def);
943 if (has_program)
944 write_sample_clnt_main ();
946 if (extend && tell == ftell (fout))
948 unlink (outfilename);
950 close_input ();
951 close_output (outfilename);
954 static char *
955 file_name (const char *file, const char *ext)
957 char *temp;
958 temp = extendfile (file, ext);
960 if (access (temp, F_OK) != -1)
961 return (temp);
962 else
963 return ((char *) " ");
966 static void
967 mkfile_output (struct commandline *cmd)
969 char *mkfilename;
970 const char *clientname, *clntname, *xdrname, *hdrname;
971 const char *servername, *svcname, *servprogname, *clntprogname;
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 char *cp, *temp;
995 mkfilename = alloc (strlen ("Makefile.") + strlen (cmd->infile) + 1);
996 temp = rindex (cmd->infile, '.');
997 cp = stpcpy (mkfilename, "Makefile.");
998 strncpy (cp, cmd->infile, (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 /* TRANS: the file will not be removed; this is an
1149 TRANS: informative message. */
1150 _("file `%s' already exists and may be overwritten\n"),
1151 outfile);
1152 crash ();
1158 * Parse command line arguments
1160 static int
1161 parseargs (int argc, const char *argv[], struct commandline *cmd)
1163 int i;
1164 int j;
1165 int c;
1166 char flag[(1 << 8 * sizeof (char))];
1167 int nflags;
1169 cmdname = argv[0];
1170 cmd->infile = cmd->outfile = NULL;
1171 if (argc < 2)
1173 return (0);
1175 allfiles = 0;
1176 flag['c'] = 0;
1177 flag['h'] = 0;
1178 flag['l'] = 0;
1179 flag['m'] = 0;
1180 flag['o'] = 0;
1181 flag['s'] = 0;
1182 flag['n'] = 0;
1183 flag['t'] = 0;
1184 flag['S'] = 0;
1185 flag['C'] = 0;
1186 flag['M'] = 0;
1188 for (i = 1; i < argc; i++)
1190 if (argv[i][0] != '-')
1192 if (cmd->infile)
1194 fprintf (stderr,
1195 _("Cannot specify more than one input file!\n"));
1196 return 0;
1198 cmd->infile = argv[i];
1200 else
1202 for (j = 1; argv[i][j] != 0; j++)
1204 c = argv[i][j];
1205 switch (c)
1207 case 'a':
1208 allfiles = 1;
1209 break;
1210 case 'c':
1211 case 'h':
1212 case 'l':
1213 case 'm':
1214 case 't':
1215 if (flag[c])
1216 return 0;
1217 flag[c] = 1;
1218 break;
1219 case 'S':
1220 /* sample flag: Ss or Sc.
1221 Ss means set flag['S'];
1222 Sc means set flag['C'];
1223 Sm means set flag['M']; */
1224 c = argv[i][++j]; /* get next char */
1225 if (c == 's')
1226 c = 'S';
1227 else if (c == 'c')
1228 c = 'C';
1229 else if (c == 'm')
1230 c = 'M';
1231 else
1232 return 0;
1234 if (flag[c])
1235 return 0;
1236 flag[c] = 1;
1237 break;
1238 case 'C': /* ANSI C syntax */
1239 Cflag = 1;
1240 break;
1242 #ifdef __GNU_LIBRARY__
1243 case 'k': /* K&R C syntax */
1244 Cflag = 0;
1245 break;
1247 #endif
1248 case 'b': /* turn TIRPC flag off for
1249 generating backward compatible
1251 tirpcflag = 0;
1252 break;
1254 #ifdef __GNU_LIBRARY__
1255 case '5': /* turn TIRPC flag on for
1256 generating SysVr4 compatible
1258 tirpcflag = 1;
1259 break;
1260 #endif
1261 case 'I':
1262 inetdflag = 1;
1263 break;
1264 case 'N':
1265 newstyle = 1;
1266 break;
1267 case 'L':
1268 logflag = 1;
1269 break;
1270 case 'K':
1271 if (++i == argc)
1273 return (0);
1275 svcclosetime = argv[i];
1276 goto nextarg;
1277 case 'T':
1278 tblflag = 1;
1279 break;
1280 case 'M':
1281 mtflag = 1;
1282 break;
1283 case 'i':
1284 if (++i == argc)
1286 return (0);
1288 inlineflag = atoi (argv[i]);
1289 goto nextarg;
1290 case 'n':
1291 case 'o':
1292 case 's':
1293 if (argv[i][j - 1] != '-' ||
1294 argv[i][j + 1] != 0)
1296 return (0);
1298 flag[c] = 1;
1299 if (++i == argc)
1301 return (0);
1303 if (c == 's')
1305 if (!streq (argv[i], "udp") &&
1306 !streq (argv[i], "tcp"))
1307 return 0;
1309 else if (c == 'o')
1311 if (cmd->outfile)
1312 return 0;
1313 cmd->outfile = argv[i];
1315 goto nextarg;
1316 case 'D':
1317 if (argv[i][j - 1] != '-')
1318 return 0;
1319 addarg (argv[i]);
1320 goto nextarg;
1321 case 'Y':
1322 if (++i == argc)
1323 return 0;
1325 size_t len = strlen (argv[i]);
1326 pathbuf = malloc (len + 5);
1327 if (pathbuf == NULL)
1329 perror (cmdname);
1330 crash ();
1332 stpcpy (stpcpy (pathbuf,
1333 argv[i]),
1334 "/cpp");
1335 CPP = pathbuf;
1336 cppDefined = 1;
1337 goto nextarg;
1340 default:
1341 return 0;
1344 nextarg:
1349 cmd->cflag = flag['c'];
1350 cmd->hflag = flag['h'];
1351 cmd->lflag = flag['l'];
1352 cmd->mflag = flag['m'];
1353 cmd->nflag = flag['n'];
1354 cmd->sflag = flag['s'];
1355 cmd->tflag = flag['t'];
1356 cmd->Ssflag = flag['S'];
1357 cmd->Scflag = flag['C'];
1358 cmd->makefileflag = flag['M'];
1360 if (mtflag || newstyle)
1362 /* glibc doesn't support these flags. */
1363 f_print (stderr,
1364 _("This implementation doesn't support newstyle or MT-safe code!\n"));
1365 return (0);
1367 if (tirpcflag)
1369 pmflag = inetdflag ? 0 : 1; /* pmflag or inetdflag is always TRUE */
1370 if ((inetdflag && cmd->nflag))
1371 { /* netid not allowed with inetdflag */
1372 fprintf (stderr, _("Cannot use netid flag with inetd flag!\n"));
1373 return 0;
1376 else
1377 { /* 4.1 mode */
1378 pmflag = 0; /* set pmflag only in tirpcmode */
1379 #ifndef __GNU_LIBRARY__
1380 inetdflag = 1; /* inetdflag is TRUE by default */
1381 #endif
1382 if (cmd->nflag)
1383 { /* netid needs TIRPC */
1384 f_print (stderr, _("Cannot use netid flag without TIRPC!\n"));
1385 return (0);
1389 if (newstyle && (tblflag || cmd->tflag))
1391 f_print (stderr, _("Cannot use table flags with newstyle!\n"));
1392 return (0);
1395 /* check no conflicts with file generation flags */
1396 nflags = cmd->cflag + cmd->hflag + cmd->lflag + cmd->mflag +
1397 cmd->sflag + cmd->nflag + cmd->tflag + cmd->Ssflag + cmd->Scflag;
1399 if (nflags == 0)
1401 if (cmd->outfile != NULL || cmd->infile == NULL)
1403 return (0);
1406 else if (cmd->infile == NULL &&
1407 (cmd->Ssflag || cmd->Scflag || cmd->makefileflag))
1409 fprintf (stderr,
1410 _("\"infile\" is required for template generation flags.\n"));
1411 return 0;
1413 if (nflags > 1)
1415 fprintf (stderr, _("Cannot have more than one file generation flag!\n"));
1416 return 0;
1418 return 1;
1421 static void
1422 usage (void)
1424 fprintf (stderr, _("usage: %s infile\n"), cmdname);
1425 fprintf (stderr, _("\t%s [-abkCLNTM][-Dname[=value]] [-i size] \
1426 [-I [-K seconds]] [-Y path] infile\n"), cmdname);
1427 fprintf (stderr, _("\t%s [-c | -h | -l | -m | -t | -Sc | -Ss | -Sm] \
1428 [-o outfile] [infile]\n"), cmdname);
1429 fprintf (stderr, _("\t%s [-s nettype]* [-o outfile] [infile]\n"), cmdname);
1430 fprintf (stderr, _("\t%s [-n netid]* [-o outfile] [infile]\n"), cmdname);
1431 options_usage ();
1432 exit (1);
1435 static void
1436 options_usage (void)
1438 f_print (stderr, "options:\n");
1439 f_print (stderr, "-a\t\tgenerate all files, including samples\n");
1440 f_print (stderr, "-b\t\tbackward compatibility mode (generates code for SunOS 4.1)\n");
1441 f_print (stderr, "-c\t\tgenerate XDR routines\n");
1442 f_print (stderr, "-C\t\tANSI C mode\n");
1443 f_print (stderr, "-Dname[=value]\tdefine a symbol (same as #define)\n");
1444 f_print (stderr, "-h\t\tgenerate header file\n");
1445 f_print (stderr, "-i size\t\tsize at which to start generating inline code\n");
1446 f_print (stderr, "-I\t\tgenerate code for inetd support in server (for SunOS 4.1)\n");
1447 f_print (stderr, "-K seconds\tserver exits after K seconds of inactivity\n");
1448 f_print (stderr, "-l\t\tgenerate client side stubs\n");
1449 f_print (stderr, "-L\t\tserver errors will be printed to syslog\n");
1450 f_print (stderr, "-m\t\tgenerate server side stubs\n");
1451 f_print (stderr, "-M\t\tgenerate MT-safe code\n");
1452 f_print (stderr, "-n netid\tgenerate server code that supports named netid\n");
1453 f_print (stderr, "-N\t\tsupports multiple arguments and call-by-value\n");
1454 f_print (stderr, "-o outfile\tname of the output file\n");
1455 f_print (stderr, "-s nettype\tgenerate server code that supports named nettype\n");
1456 f_print (stderr, "-Sc\t\tgenerate sample client code that uses remote procedures\n");
1457 f_print (stderr, "-Ss\t\tgenerate sample server code that defines remote procedures\n");
1458 f_print (stderr, "-Sm \t\tgenerate makefile template \n");
1459 f_print (stderr, "-t\t\tgenerate RPC dispatch table\n");
1460 f_print (stderr, "-T\t\tgenerate code to support RPC dispatch tables\n");
1461 f_print (stderr, "-Y path\t\tdirectory name to find C preprocessor (cpp)\n");
1463 exit (1);