rxgen: Tidy up header includes
[openafs.git] / src / rxgen / rpc_main.c
1 /* @(#)rpc_main.c       1.4 87/11/30 3.9 RPCSRC */
2 /*
3  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4  * unrestricted use provided that this legend is included on all tape
5  * media and as a part of the software program in whole or part.  Users
6  * may copy or modify Sun RPC without charge, but are not authorized
7  * to license or distribute it to anyone else except as part of a product or
8  * program developed by the user.
9  *
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.
13  *
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.
17  *
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.
21  *
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.
25  *
26  * Sun Microsystems, Inc.
27  * 2550 Garcia Avenue
28  * Mountain View, California  94043
29  */
30
31 /*
32  * rpc_main.c, Top level of the RPC protocol compiler.
33  * Copyright (C) 1987, Sun Microsystems, Inc.
34  */
35
36 #include <afsconfig.h>
37 #include <afs/param.h>
38
39 #include <roken.h>
40
41 #include <limits.h>
42 #include <ctype.h>
43
44 #include "rpc_scan.h"
45 #include "rpc_parse.h"
46 #include "rpc_util.h"
47
48 #define EXTEND  1               /* alias for TRUE */
49
50 struct commandline {
51     int ansic_flag;
52     int brief_flag;
53     int cflag;
54     int hflag;
55     int lflag;
56     int sflag;
57     int mflag;
58     int Cflag;
59     int Sflag;
60     int rflag;
61     int kflag;
62     int pflag;
63     int dflag;
64     int xflag;
65     int yflag;
66     int uflag;
67     char *infile;
68     char *outfile;
69 };
70
71 #define MAXCPPARGS      256     /* maximum number of arguments to cpp */
72 #define MAXCMDLINE      1024    /* MAX chars on a single  cmd line */
73
74 char *prefix = "";
75 static char *IncludeDir[MAXCPPARGS];
76 int nincludes = 0;
77 char *OutFileFlag = "";
78 char OutFile[256];
79 char Sflag = 0, Cflag = 0, hflag = 0, cflag = 0, kflag = 0, uflag = 0;
80 char ansic_flag = 0;            /* If set, build ANSI C style prototypes */
81 char brief_flag = 0;            /* If set, shorten names */
82 char zflag = 0;                 /* If set, abort server stub if rpc call returns non-zero */
83 char xflag = 0;                 /* if set, add stats code to stubs */
84 char yflag = 0;                 /* if set, only emit function name arrays to xdr file */
85 int debug = 0;
86 static int pclose_fin = 0;
87 static char *cmdname;
88 #ifdef AFS_NT40_ENV
89 static char *CPP = NULL;
90 #else /* AFS_NT40_ENV */
91 #ifdef PATH_CPP
92 static char CPP[] = PATH_CPP;
93 #else
94 static char CPP[] = "/lib/cpp";
95 #endif
96 #endif /* AFS_NT40_ENV */
97 static char CPPFLAGS[] = "-C";
98
99 #ifdef  AFS_ALPHA_ENV
100 /*
101  * Running "cpp" directly on DEC OSF/1 does not define anything; the "cc"
102  * driver is responsible.  To compensate (and allow for other definitions
103  * which should always be passed to "cpp"), place definitions which whould
104  * always be passed to "rxgen" in this table.
105  */
106 static char *XTRA_CPPFLAGS[] = {
107 #ifdef  __alpha
108     "-D__alpha",
109 #endif /* __alpha */
110 #ifdef  OSF
111     "-DOSF",
112 #endif /* OSF */
113     NULL
114 };
115 #endif
116
117 #include "AFS_component_version_number.c"
118
119 /* static prototypes */
120 static char *extendfile(char *file, char *ext);
121 static void open_output(char *infile, char *outfile);
122 static void open_input(char *infile, char *define);
123 static void c_output(char *infile, char *define, int extend, char *outfile,
124                      int append);
125 static void h_output(char *infile, char *define, int extend, char *outfile,
126                      int append);
127 static void s_output(int argc, char *argv[], char *infile, char *define,
128                      int extend, char *outfile, int nomain);
129 static void l_output(char *infile, char *define, int extend, char *outfile);
130 static void do_registers(int argc, char *argv[]);
131 static int parseargs(int argc, char *argv[], struct commandline *cmd);
132 static void C_output(char *infile, char *define, int extend, char *outfile,
133                      int append);
134 static void S_output(char *infile, char *define, int extend, char *outfile,
135                      int append);
136 static char *uppercase(char *str);
137
138 int
139 main(int argc, char *argv[])
140 {
141     struct commandline cmd;
142
143 #ifdef AFS_NT40_ENV
144     /* initialize CPP with the correct pre-processor for Windows */
145     CPP = getenv("RXGEN_CPPCMD");
146     if (!CPP)
147         CPP = "cl /EP /C /nologo";
148 #endif /* AFS_NT40_ENV */
149
150 #ifdef  AFS_AIX32_ENV
151     /*
152      * The following signal action for AIX is necessary so that in case of a
153      * crash (i.e. core is generated) we can include the user's data section
154      * in the core dump. Unfortunately, by default, only a partial core is
155      * generated which, in many cases, isn't too useful.
156      */
157     struct sigaction nsa;
158
159     sigemptyset(&nsa.sa_mask);
160     nsa.sa_handler = SIG_DFL;
161     nsa.sa_flags = SA_FULLDUMP;
162     sigaction(SIGSEGV, &nsa, NULL);
163 #endif
164     reinitialize();
165     if (!parseargs(argc, argv, &cmd)) {
166         f_print(stderr, "usage: %s infile\n", cmdname);
167         f_print(stderr,
168                 "       %s [-c | -h | -l | -m | -C | -S | -r | -b | -k | -R | -p | -d | -z | -u] [-Pprefix] [-Idir] [-o outfile] [infile]\n",
169                 cmdname);
170         f_print(stderr, "       %s [-s udp|tcp]* [-o outfile] [infile]\n",
171                 cmdname);
172         exit(1);
173     }
174     OutFileFlag = cmd.outfile;
175     if (OutFileFlag)
176         strcpy(OutFile, cmd.outfile);
177     if (cmd.cflag) {
178         OutFileFlag = NULL;
179         c_output(cmd.infile, "-DRPC_XDR", !EXTEND, cmd.outfile, 0);
180     } else if (cmd.hflag) {
181         h_output(cmd.infile, "-DRPC_HDR", !EXTEND, cmd.outfile, 0);
182     } else if (cmd.lflag) {
183         l_output(cmd.infile, "-DRPC_CLNT", !EXTEND, cmd.outfile);
184     } else if (cmd.sflag || cmd.mflag) {
185         s_output(argc, argv, cmd.infile, "-DRPC_SVC", !EXTEND, cmd.outfile,
186                  cmd.mflag);
187     } else if (cmd.Cflag) {
188         OutFileFlag = NULL;
189         C_output(cmd.infile, "-DRPC_CLIENT", !EXTEND, cmd.outfile, 1);
190     } else if (cmd.Sflag) {
191         OutFileFlag = NULL;
192         S_output(cmd.infile, "-DRPC_SERVER", !EXTEND, cmd.outfile, 1);
193     } else {
194         if (OutFileFlag && (strrchr(OutFile, '.') == NULL))
195             strcat(OutFile, ".");
196         if (cmd.rflag) {
197             C_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_CLIENT",
198                      EXTEND, ".cs.c", 1);
199             reinitialize();
200             S_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_SERVER",
201                      EXTEND, ".ss.c", 1);
202             reinitialize();
203         } else {
204             reinitialize();
205             c_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_XDR",
206                      EXTEND, ".xdr.c", 0);
207             reinitialize();
208             h_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_HDR",
209                      EXTEND, ".h", 0);
210             reinitialize();
211             C_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_CLIENT",
212                      EXTEND, ".cs.c", 1);
213             reinitialize();
214             S_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_SERVER",
215                      EXTEND, ".ss.c", 1);
216             reinitialize();
217         }
218     }
219     if (fin && pclose_fin) {
220         /* the cpp command we called returned a non-zero exit status */
221         if (pclose(fin)) {
222             crash();
223         }
224     }
225     exit(0);
226 }
227
228 /*
229  * add extension to filename
230  */
231 static char *
232 extendfile(char *file, char *ext)
233 {
234     char *res;
235     char *p;
236     char *sname;
237
238     res = alloc(strlen(file) + strlen(ext) + 1);
239     if (res == NULL) {
240         abort();
241     }
242     p = (char *)strrchr(file, '.');
243     if (p == NULL) {
244         p = file + strlen(file);
245     }
246     sname = (char *)strrchr(file, '/');
247     if (sname == NULL)
248         sname = file;
249     else
250         sname++;
251     strcpy(res, sname);
252     strcpy(res + (p - sname), ext);
253     return (res);
254 }
255
256 /*
257  * Open output file with given extension
258  */
259 static void
260 open_output(char *infile, char *outfile)
261 {
262     if (outfile == NULL) {
263         fout = stdout;
264         return;
265     }
266     if (infile != NULL && streq(outfile, infile)) {
267         f_print(stderr, "%s: output would overwrite %s\n", cmdname, infile);
268         crash();
269     }
270     fout = fopen(outfile, "w");
271     if (fout == NULL) {
272         f_print(stderr, "%s: unable to open ", cmdname);
273         perror(outfile);
274         crash();
275     }
276     record_open(outfile);
277 }
278
279 /*
280  * Open input file with given define for C-preprocessor
281  */
282 static void
283 open_input(char *infile, char *define)
284 {
285     char cpp_cmdline[MAXCMDLINE];
286
287     int i;
288     if (debug == 0) {
289         infilename = (infile == NULL) ? "<stdin>" : infile;
290         strcpy(cpp_cmdline, CPP);
291         strcat(cpp_cmdline, " ");
292         strcat(cpp_cmdline, CPPFLAGS);
293         strcat(cpp_cmdline, " ");
294         strcat(cpp_cmdline, define);
295
296 #ifdef  AFS_ALPHA_ENV
297         for (i = 0;
298              i < (sizeof(XTRA_CPPFLAGS) / sizeof(XTRA_CPPFLAGS[0])) - 1;
299              i++) {
300             strcat(cpp_cmdline, " ");
301             strcat(cpp_cmdline, XTRA_CPPFLAGS[i]);
302         }
303 #endif
304         for (i = 0; i < nincludes; i++) {
305             strcat(cpp_cmdline, " ");
306             strcat(cpp_cmdline, IncludeDir[i]);
307         }
308
309         strcat(cpp_cmdline, " ");
310         strcat(cpp_cmdline, infile);
311
312         fin = popen(cpp_cmdline, "r");
313         if (fin == NULL)
314             perror("popen");
315         pclose_fin = 1;
316
317     } else {
318         if (infile == NULL) {
319             fin = stdin;
320             return;
321         }
322         fin = fopen(infile, "r");
323     }
324     if (fin == NULL) {
325         f_print(stderr, "%s: ", cmdname);
326         perror(infilename);
327         crash();
328     }
329 }
330
331 /*
332  * Compile into an XDR routine output file
333  */
334 static void
335 c_output(char *infile, char *define, int extend, char *outfile, int append)
336 {
337     definition *def;
338     char *include;
339     char *outfilename;
340     long tell;
341     char fullname[1024];
342     char *currfile = (OutFileFlag ? OutFile : infile);
343     int i, j;
344
345     open_input(infile, define);
346     cflag = 1;
347     memset(fullname, 0, sizeof(fullname));
348     if (append) {
349         strcpy(fullname, prefix);
350         strcat(fullname, infile);
351     } else
352         strcpy(fullname, infile);
353     outfilename = extend ? extendfile(fullname, outfile) : outfile;
354     open_output(infile, outfilename);
355     f_print(fout, "/* Machine generated file -- Do NOT edit */\n\n");
356     if (xflag) {
357         if (kflag) {
358             f_print(fout, "#include \"afsconfig.h\"\n");
359             f_print(fout, "#include \"afs/param.h\"\n");
360         } else {
361             f_print(fout, "#include <afsconfig.h>\n");
362             f_print(fout, "#include <afs/param.h>\n");
363             f_print(fout, "#include <roken.h>\n");
364         }
365         f_print(fout, "#ifdef AFS_NT40_ENV\n");
366         f_print(fout, "#define AFS_RXGEN_EXPORT __declspec(dllexport)\n");
367         f_print(fout, "#include <winsock2.h>\n");
368         f_print(fout, "#endif /* AFS_NT40_ENV */\n");
369     }
370     if (currfile && (include = extendfile(currfile, ".h"))) {
371         if (kflag) {
372             f_print(fout, "#include \"%s\"\n\n", include);
373         } else
374             f_print(fout, "#include \"%s\"\n\n", include);
375         free(include);
376     } else {
377         /* In case we can't include the interface's own header file... */
378         if (kflag) {
379             f_print(fout, "#include \"h/types.h\"\n");
380             f_print(fout, "#include \"h/socket.h\"\n");
381             f_print(fout, "#include \"h/file.h\"\n");
382             f_print(fout, "#include \"h/stat.h\"\n");
383             f_print(fout, "#include \"netinet/in.h\"\n");
384             f_print(fout, "#include \"h/time.h\"\n");
385             f_print(fout, "#include \"rx/xdr.h\"\n");
386             f_print(fout, "#include \"afs/rxgen_consts.h\"\n");
387         } else {
388             f_print(fout, "#include <rx/xdr.h>\n");
389         }
390     }
391
392     tell = ftell(fout);
393     while ((def = get_definition())) {
394         if (!yflag) {
395             if ((!IsRxgenDefinition(def)) && def->def_kind != DEF_CUSTOMIZED)
396                 emit(def);
397         }
398     }
399
400     /*
401      * Print out array containing list of all functions in the interface
402      * in order
403      */
404
405     if (xflag) {
406         for (j = 0; j <= PackageIndex; j++) {
407             f_print(fout, "AFS_RXGEN_EXPORT\n");
408             f_print(fout, "const char *%sfunction_names[] = {\n",
409                     PackagePrefix[j]);
410
411             for (i = 0; i < no_of_stat_funcs_header[j]; i++) {
412                 if (i == 0) {
413                     f_print(fout, "\t\"%s\"", function_list[j][i]);
414                 } else {
415                     f_print(fout, ",\n\t\"%s\"", function_list[j][i]);
416                 }
417             }
418
419
420             f_print(fout, "\n};\n");
421         }
422         er_Proc_CodeGeneration();
423     }
424
425     if (extend && tell == ftell(fout)) {
426         (void)unlink(outfilename);
427     }
428     cflag = 0;
429 }
430
431 /*
432  * Compile into an XDR header file
433  */
434 static void
435 h_output(char *infile, char *define, int extend, char *outfile, int append)
436 {
437     definition *def;
438     char *outfilename;
439     long tell;
440     char fullname[1024], *p;
441
442     open_input(infile, define);
443     hflag = 1;
444     memset(fullname, 0, sizeof(fullname));
445     if (append) {
446         strcpy(fullname, prefix);
447         strcat(fullname, infile);
448     } else
449         strcpy(fullname, infile);
450     outfilename = extend ? extendfile(fullname, outfile) : outfile;
451     open_output(infile, outfilename);
452     strcpy(fullname, outfilename);
453     if ((p = strchr(fullname, '.')))
454         *p = '\0';
455     f_print(fout, "/* Machine generated file -- Do NOT edit */\n\n");
456     f_print(fout, "#ifndef      _RXGEN_%s_\n", uppercase(fullname));
457     f_print(fout, "#define      _RXGEN_%s_\n\n", uppercase(fullname));
458     f_print(fout, "#ifdef       KERNEL\n");
459     f_print(fout,
460             "/* The following 'ifndefs' are not a good solution to the vendor's omission of surrounding all system includes with 'ifndef's since it requires that this file is included after the system includes...*/\n");
461     f_print(fout, "#include <afsconfig.h>\n");
462     f_print(fout, "#include \"afs/param.h\"\n");
463     f_print(fout, "#ifdef       UKERNEL\n");
464     f_print(fout, "#include \"afs/sysincludes.h\"\n");
465     f_print(fout, "#include \"rx/xdr.h\"\n");
466     f_print(fout, "#include \"rx/rx.h\"\n");
467     if (xflag) {
468         f_print(fout, "#include \"rx/rx_globals.h\"\n");
469     }
470     if (brief_flag) {
471         f_print(fout, "#include \"rx/rx_opaque.h\"\n");
472     }
473     if (uflag)
474         f_print(fout, "#include <ubik.h>\n");
475     f_print(fout, "#else        /* UKERNEL */\n");
476     f_print(fout, "#include \"h/types.h\"\n");
477     f_print(fout, "#ifndef      SOCK_DGRAM  /* XXXXX */\n");
478     f_print(fout, "#include \"h/socket.h\"\n");
479     f_print(fout, "#endif\n");
480     f_print(fout, "struct ubik_client;\n");
481     f_print(fout, "#ifndef      DTYPE_SOCKET  /* XXXXX */\n");
482     f_print(fout, "#ifndef AFS_LINUX22_ENV\n");
483     f_print(fout, "#include \"h/file.h\"\n");
484     f_print(fout, "#endif\n");
485     f_print(fout, "#endif\n");
486     f_print(fout, "#ifndef      S_IFMT  /* XXXXX */\n");
487     f_print(fout, "#include \"h/stat.h\"\n");
488     f_print(fout, "#endif\n");
489     f_print(fout, "#if defined (AFS_OBSD_ENV) && !defined (MLEN)\n");
490     f_print(fout, "#include \"sys/mbuf.h\"\n");
491     f_print(fout, "#endif\n");
492     f_print(fout, "#ifndef      IPPROTO_UDP /* XXXXX */\n");
493     f_print(fout, "#include \"netinet/in.h\"\n");
494     f_print(fout, "#endif\n");
495     f_print(fout, "#ifndef      DST_USA  /* XXXXX */\n");
496     f_print(fout, "#include \"h/time.h\"\n");
497     f_print(fout, "#endif\n");
498     f_print(fout, "#ifndef AFS_LINUX22_ENV\n");
499     f_print(fout, "#include \"rpc/types.h\"\n");
500     f_print(fout, "#endif /* AFS_LINUX22_ENV */\n");
501     f_print(fout, "#ifndef      XDR_GETLONG /* XXXXX */\n");
502     f_print(fout, "#ifdef AFS_LINUX22_ENV\n");
503     f_print(fout, "#ifndef quad_t\n");
504     f_print(fout, "#define quad_t __quad_t\n");
505     f_print(fout, "#define u_quad_t __u_quad_t\n");
506     f_print(fout, "#endif\n");
507     f_print(fout, "#endif\n");
508     f_print(fout, "#include \"rx/xdr.h\"\n");
509     f_print(fout, "#endif /* XDR_GETLONG */\n");
510     f_print(fout, "#endif   /* UKERNEL */\n");
511     f_print(fout, "#include \"afs/rxgen_consts.h\"\n");
512     f_print(fout, "#include \"afs_osi.h\"\n");
513     f_print(fout, "#include \"rx/rx.h\"\n");
514     if (xflag) {
515         f_print(fout, "#include \"rx/rx_globals.h\"\n");
516     }
517     if (brief_flag) {
518         f_print(fout, "#include \"rx/rx_opaque.h\"\n");
519     }
520     f_print(fout, "#else        /* KERNEL */\n");
521     f_print(fout, "#include <afs/param.h>\n");
522     f_print(fout, "#include <afs/stds.h>\n");
523     f_print(fout, "#include <sys/types.h>\n");
524     f_print(fout, "#include <rx/xdr.h>\n");
525     f_print(fout, "#include <rx/rx.h>\n");
526     if (xflag) {
527         f_print(fout, "#include <rx/rx_globals.h>\n");
528     }
529     if (brief_flag) {
530         f_print(fout, "#include <rx/rx_opaque.h>\n");
531     }
532     f_print(fout, "#include <afs/rxgen_consts.h>\n");
533     if (uflag)
534         f_print(fout, "#include <ubik.h>\n");
535     f_print(fout, "#endif       /* KERNEL */\n\n");
536     f_print(fout, "#ifdef AFS_NT40_ENV\n");
537     f_print(fout, "#ifndef AFS_RXGEN_EXPORT\n");
538     f_print(fout, "#define AFS_RXGEN_EXPORT __declspec(dllimport)\n");
539     f_print(fout, "#endif /* AFS_RXGEN_EXPORT */\n");
540     f_print(fout, "#else /* AFS_NT40_ENV */\n");
541     f_print(fout, "#define AFS_RXGEN_EXPORT\n");
542     f_print(fout, "#endif /* AFS_NT40_ENV */\n\n");
543     tell = ftell(fout);
544     while ((def = get_definition())) {
545         print_datadef(def);
546     }
547     h_Proc_CodeGeneration();
548     h_opcode_stats();
549     hflag = 0;
550     f_print(fout, "#endif       /* _RXGEN_%s_ */\n", uppercase(fullname));
551     if (extend && tell == ftell(fout)) {
552         (void)unlink(outfilename);
553     }
554 }
555
556 /*
557  * Compile into an RPC service
558  */
559 static void
560 s_output(int argc, char *argv[], char *infile, char *define, int extend,
561          char *outfile, int nomain)
562 {
563     char *include;
564     definition *def;
565     int foundprogram;
566     char *outfilename;
567
568     open_input(infile, define);
569     outfilename = extend ? extendfile(infile, outfile) : outfile;
570     open_output(infile, outfilename);
571     f_print(fout, "#include <stdio.h>\n");
572     f_print(fout, "#include <rpc/rpc.h>\n");
573     if (infile && (include = extendfile(infile, ".h"))) {
574         f_print(fout, "#include \"%s\"\n", include);
575         free(include);
576     }
577     foundprogram = 0;
578     while ((def = get_definition())) {
579         foundprogram |= (def->def_kind == DEF_PROGRAM);
580     }
581     if (extend && !foundprogram) {
582         (void)unlink(outfilename);
583         return;
584     }
585     if (nomain) {
586         write_programs((char *)NULL);
587     } else {
588         write_most();
589         do_registers(argc, argv);
590         write_rest();
591         write_programs("static");
592     }
593 }
594
595 static void
596 l_output(char *infile, char *define, int extend, char *outfile)
597 {
598     char *include;
599     definition *def;
600     int foundprogram;
601     char *outfilename;
602
603     open_input(infile, define);
604     outfilename = extend ? extendfile(infile, outfile) : outfile;
605     open_output(infile, outfilename);
606     f_print(fout, "#include <rpc/rpc.h>\n");
607     f_print(fout, "#include <sys/time.h>\n");
608     if (infile && (include = extendfile(infile, ".h"))) {
609         f_print(fout, "#include \"%s\"\n", include);
610         free(include);
611     }
612     foundprogram = 0;
613     while ((def = get_definition())) {
614         foundprogram |= (def->def_kind == DEF_PROGRAM);
615     }
616     if (extend && !foundprogram) {
617         (void)unlink(outfilename);
618         return;
619     }
620     write_stubs();
621 }
622
623 /*
624  * Perform registrations for service output
625  */
626 static void
627 do_registers(int argc, char *argv[])
628 {
629     int i;
630
631     for (i = 1; i < argc; i++) {
632         if (streq(argv[i], "-s")) {
633             write_register(argv[i + 1]);
634             i++;
635         }
636     }
637 }
638
639
640 static void
641 C_output(char *infile, char *define, int extend, char *outfile, int append)
642 {
643     char *include;
644     char *outfilename;
645     char fullname[1024];
646     long tell;
647     char *currfile = (OutFileFlag ? OutFile : infile);
648
649     Cflag = 1;
650     open_input(infile, define);
651     memset(fullname, 0, sizeof(fullname));
652     if (append) {
653         strcpy(fullname, prefix);
654         strcat(fullname, infile);
655     } else
656         strcpy(fullname, infile);
657     outfilename = extend ? extendfile(fullname, outfile) : outfile;
658     open_output(infile, outfilename);
659     f_print(fout, "/* Machine generated file -- Do NOT edit */\n\n");
660     if (currfile && (include = extendfile(currfile, ".h"))) {
661         if (kflag) {
662             f_print(fout, "#include \"%s\"\n\n", include);
663         } else {
664             f_print(fout, "#include <afsconfig.h>\n");
665             f_print(fout, "#include <afs/param.h>\n");
666             f_print(fout, "#include <roken.h>\n");
667             f_print(fout, "#include \"%s\"\n\n", include);
668         }
669         free(include);
670     } else {
671         if (kflag) {
672             f_print(fout, "#include \"h/types.h\"\n");
673             f_print(fout, "#include \"h/socket.h\"\n");
674             f_print(fout, "#include \"h/file.h\"\n");
675             f_print(fout, "#include \"h/stat.h\"\n");
676             f_print(fout, "#include \"netinet/in.h\"\n");
677             f_print(fout, "#include \"h/time.h\"\n");
678             f_print(fout, "#include \"rpc/types.h\"\n");
679             f_print(fout, "#include \"rx/xdr.h\"\n");
680             f_print(fout, "#include \"afs/rxgen_consts.h\"\n");
681             f_print(fout, "#include \"afs/afs_osi.h\"\n");
682             f_print(fout, "#include \"rx/rx.h\"\n");
683             if (xflag) {
684                 f_print(fout, "#include \"rx/rx_globals.h\"\n");
685             }
686             if (brief_flag) {
687                 f_print(fout, "#include \"rx/rx_opaque.h\"\n");
688             }
689         } else {
690             f_print(fout, "#include <sys/types.h>\n");
691             f_print(fout, "#include <rx/xdr.h>\n");
692             f_print(fout, "#include <rx/rx.h>\n");
693             if (xflag) {
694                 f_print(fout, "#include <rx/rx_globals.h>\n");
695             }
696             if (brief_flag) {
697                 f_print(fout, "#include <rx/rx_opaque.h\"\n");
698             }
699             f_print(fout, "#include <afs/rxgen_consts.h>\n");
700         }
701     }
702
703     tell = ftell(fout);
704     while (get_definition())
705         continue;
706     if (extend && tell == ftell(fout)) {
707         (void)unlink(outfilename);
708     }
709
710     Cflag = 0;
711 }
712
713 static void
714 S_output(char *infile, char *define, int extend, char *outfile, int append)
715 {
716     char *include;
717     char *outfilename;
718     char fullname[1024];
719     definition *def;
720     long tell;
721     char *currfile = (OutFileFlag ? OutFile : infile);
722
723     Sflag = 1;
724     open_input(infile, define);
725     memset(fullname, 0, sizeof(fullname));
726     if (append) {
727         strcpy(fullname, prefix);
728         strcat(fullname, infile);
729     } else
730         strcpy(fullname, infile);
731     outfilename = extend ? extendfile(fullname, outfile) : outfile;
732     open_output(infile, outfilename);
733     f_print(fout, "/* Machine generated file -- Do NOT edit */\n\n");
734     if (currfile && (include = extendfile(currfile, ".h"))) {
735         if (kflag) {
736             f_print(fout, "#include \"%s\"\n", include);
737         } else {
738             f_print(fout, "#include <afsconfig.h>\n");
739             f_print(fout, "#include <afs/param.h>\n");
740             f_print(fout, "#include <roken.h>\n");
741             f_print(fout, "#include \"%s\"\n\n", include);
742         }
743         free(include);
744     } else {
745         if (kflag) {
746             f_print(fout, "#include \"h/types.h\"\n");
747             f_print(fout, "#include \"h/socket.h\"\n");
748             f_print(fout, "#include \"h/file.h\"\n");
749             f_print(fout, "#include \"h/stat.h\"\n");
750             f_print(fout, "#include \"netinet/in.h\"\n");
751             f_print(fout, "#include \"h/time.h\"\n");
752             f_print(fout, "#include \"rpc/types.h\"\n");
753             f_print(fout, "#include \"rx/xdr.h\"\n");
754             f_print(fout, "#include \"afs/rxgen_consts.h\"\n");
755             f_print(fout, "#include \"afs/afs_osi.h\"\n");
756             f_print(fout, "#include \"rx/rx.h\"\n");
757             if (xflag) {
758                 f_print(fout, "#include \"rx/rx_globals.h\"\n");
759             }
760             if (brief_flag) {
761                 f_print(fout, "#include \"rx/rx_opaque.h\"\n");
762             }
763         } else {
764             f_print(fout, "#include <sys/types.h>\n");
765             f_print(fout, "#include <rx/xdr.h>\n");
766             f_print(fout, "#include <rx/rx.h>\n");
767             if (xflag) {
768                 f_print(fout, "#include <rx/rx_globals.h>\n");
769             }
770             if (brief_flag) {
771                 f_print(fout, "#include <rx/rx_opaque.h>\n");
772             }
773             f_print(fout, "#include <afs/rxgen_consts.h>\n");
774         }
775     }
776
777     tell = ftell(fout);
778     fflush(fout);
779     while ((def = get_definition())) {
780         fflush(fout);
781         print_datadef(def);
782     }
783
784     er_Proc_CodeGeneration();
785
786     if (extend && tell == ftell(fout)) {
787         (void)unlink(outfilename);
788     }
789     Sflag = 0;
790 }
791
792 static char *
793 uppercase(char *str)
794 {
795     static char max_size[100];
796     char *pnt;
797     int len = (int)strlen(str);
798
799     for (pnt = max_size; len > 0; len--, str++) {
800         *pnt++ = (islower(*str) ? toupper(*str) : *str);
801     }
802     *pnt = '\0';
803     return max_size;
804 }
805
806 /*
807  * Parse command line arguments
808  */
809 static int
810 parseargs(int argc, char *argv[], struct commandline *cmd)
811 {
812     int i;
813     int j;
814     char c;
815     char flag[(1 << (8 * sizeof(char)))];
816     int nflags;
817
818     cmdname = argv[0];
819     cmd->infile = cmd->outfile = NULL;
820     if (argc < 2) {
821         return (0);
822     }
823     memset(flag, 0, sizeof(flag));
824     cmd->outfile = NULL;
825     for (i = 1; i < argc; i++) {
826         if (argv[i][0] != '-') {
827             if (cmd->infile) {
828                 return (0);
829             }
830             cmd->infile = argv[i];
831         } else {
832             for (j = 1; argv[i][j] != 0; j++) {
833                 c = argv[i][j];
834                 switch (c) {
835                 case 'A':
836                 case 'c':
837                 case 'h':
838                 case 'l':
839                 case 'm':
840                 case 'C':
841                 case 'S':
842                 case 'b':
843                 case 'r':
844                 case 'R':
845                 case 'k':
846                 case 'p':
847                 case 'd':
848                 case 'u':
849                 case 'x':
850                 case 'y':
851                 case 'z':
852                     if (flag[(int)c]) {
853                         return (0);
854                     }
855                     flag[(int)c] = 1;
856                     break;
857                 case 'o':
858                 case 's':
859                     if (argv[i][j - 1] != '-' || argv[i][j + 1] != 0) {
860                         return (0);
861                     }
862                     flag[(int)c] = 1;
863                     if (++i == argc) {
864                         return (0);
865                     }
866                     if (c == 's') {
867                         if (!streq(argv[i], "udp") && !streq(argv[i], "tcp")) {
868                             return (0);
869                         }
870                     } else if (c == 'o') {
871                         if (cmd->outfile) {
872                             return (0);
873                         }
874                         cmd->outfile = argv[i];
875                     }
876                     goto nextarg;
877                 case 'P':
878                     if (argv[i][j - 1] != '-')
879                         return (0);
880                     prefix = &argv[i][j + 1];
881                     goto nextarg;
882                 case 'I':
883                     if (argv[i][j - 1] != '-')
884                         return (0);
885                     IncludeDir[nincludes++] = &argv[i][j - 1];
886                     goto nextarg;
887                 default:
888                     return (0);
889                 }
890             }
891           nextarg:
892             ;
893         }
894     }
895     cmd->ansic_flag = ansic_flag = flag['A'];
896     cmd->brief_flag = brief_flag = flag['b'];
897     cmd->cflag = cflag = flag['c'];
898     cmd->hflag = hflag = flag['h'];
899     cmd->sflag = flag['s'];
900     cmd->lflag = flag['l'];
901     cmd->mflag = flag['m'];
902     cmd->xflag = xflag = flag['x'];
903     cmd->yflag = yflag = flag['y'];
904     cmd->Cflag = Cflag = flag['C'];
905     cmd->Sflag = Sflag = flag['S'];
906     cmd->rflag = flag['r'];
907     cmd->uflag = uflag = flag['u'];
908     cmd->kflag = kflag = flag['k'];
909     cmd->pflag = flag['p'];
910     cmd->dflag = debug = flag['d'];
911     zflag = flag['z'];
912     if (cmd->pflag)
913         combinepackages = 1;
914     nflags =
915         cmd->cflag + cmd->hflag + cmd->sflag + cmd->lflag + cmd->mflag +
916         cmd->Cflag + cmd->Sflag + cmd->rflag;
917     if (nflags == 0) {
918         if (cmd->outfile != NULL || cmd->infile == NULL) {
919             return (0);
920         }
921     } else if (nflags > 1) {
922         return (0);
923     }
924     return (1);
925 }