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