rxgen: Error out if cpp exits with error
[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 \"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 \"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 <afsconfig.h>\n");
520     f_print(fout, "#include <afs/param.h>\n");
521     f_print(fout, "#include <afs/stds.h>\n");
522     f_print(fout, "#include <sys/types.h>\n");
523     f_print(fout, "#include <rx/xdr.h>\n");
524     f_print(fout, "#include <rx/rx.h>\n");
525     if (xflag) {
526         f_print(fout, "#include <rx/rx_globals.h>\n");
527     }
528     f_print(fout, "#include <afs/rxgen_consts.h>\n");
529     if (uflag)
530         f_print(fout, "#include <ubik.h>\n");
531     f_print(fout, "#endif       /* KERNEL */\n\n");
532     f_print(fout, "#ifdef AFS_NT40_ENV\n");
533     f_print(fout, "#ifndef AFS_RXGEN_EXPORT\n");
534     f_print(fout, "#define AFS_RXGEN_EXPORT __declspec(dllimport)\n");
535     f_print(fout, "#endif /* AFS_RXGEN_EXPORT */\n");
536     f_print(fout, "#else /* AFS_NT40_ENV */\n");
537     f_print(fout, "#define AFS_RXGEN_EXPORT\n");
538     f_print(fout, "#endif /* AFS_NT40_ENV */\n\n");
539     tell = ftell(fout);
540     while ((def = get_definition())) {
541         print_datadef(def);
542     }
543     h_Proc_CodeGeneration();
544     h_opcode_stats();
545     hflag = 0;
546     f_print(fout, "#endif       /* _RXGEN_%s_ */\n", uppercase(fullname));
547     if (extend && tell == ftell(fout)) {
548         (void)unlink(outfilename);
549     }
550 }
551
552 /*
553  * Compile into an RPC service
554  */
555 static void
556 s_output(int argc, char *argv[], char *infile, char *define, int extend,
557          char *outfile, int nomain)
558 {
559     char *include;
560     definition *def;
561     int foundprogram;
562     char *outfilename;
563
564     open_input(infile, define);
565     outfilename = extend ? extendfile(infile, outfile) : outfile;
566     open_output(infile, outfilename);
567     f_print(fout, "#include <stdio.h>\n");
568     f_print(fout, "#include <rpc/rpc.h>\n");
569     if (infile && (include = extendfile(infile, ".h"))) {
570         f_print(fout, "#include \"%s\"\n", include);
571         free(include);
572     }
573     foundprogram = 0;
574     while ((def = get_definition())) {
575         foundprogram |= (def->def_kind == DEF_PROGRAM);
576     }
577     if (extend && !foundprogram) {
578         (void)unlink(outfilename);
579         return;
580     }
581     if (nomain) {
582         write_programs((char *)NULL);
583     } else {
584         write_most();
585         do_registers(argc, argv);
586         write_rest();
587         write_programs("static");
588     }
589 }
590
591 static void
592 l_output(char *infile, char *define, int extend, char *outfile)
593 {
594     char *include;
595     definition *def;
596     int foundprogram;
597     char *outfilename;
598
599     open_input(infile, define);
600     outfilename = extend ? extendfile(infile, outfile) : outfile;
601     open_output(infile, outfilename);
602     f_print(fout, "#include <rpc/rpc.h>\n");
603     f_print(fout, "#include <sys/time.h>\n");
604     if (infile && (include = extendfile(infile, ".h"))) {
605         f_print(fout, "#include \"%s\"\n", include);
606         free(include);
607     }
608     foundprogram = 0;
609     while ((def = get_definition())) {
610         foundprogram |= (def->def_kind == DEF_PROGRAM);
611     }
612     if (extend && !foundprogram) {
613         (void)unlink(outfilename);
614         return;
615     }
616     write_stubs();
617 }
618
619 /*
620  * Perform registrations for service output 
621  */
622 static void
623 do_registers(int argc, char *argv[])
624 {
625     int i;
626
627     for (i = 1; i < argc; i++) {
628         if (streq(argv[i], "-s")) {
629             write_register(argv[i + 1]);
630             i++;
631         }
632     }
633 }
634
635
636 static void
637 C_output(char *infile, char *define, int extend, char *outfile, int append)
638 {
639     char *include;
640     char *outfilename;
641     char fullname[1024];
642     long tell;
643     char *currfile = (OutFileFlag ? OutFile : infile);
644
645     Cflag = 1;
646     open_input(infile, define);
647     memset(fullname, 0, sizeof(fullname));
648     if (append) {
649         strcpy(fullname, prefix);
650         strcat(fullname, infile);
651     } else
652         strcpy(fullname, infile);
653     outfilename = extend ? extendfile(fullname, outfile) : outfile;
654     open_output(infile, outfilename);
655     f_print(fout, "/* Machine generated file -- Do NOT edit */\n\n");
656     if (currfile && (include = extendfile(currfile, ".h"))) {
657         if (kflag) {
658             f_print(fout, "#include \"%s\"\n\n", include);
659         } else {
660             f_print(fout, "#include \"%s\"\n\n", include);
661         }
662         free(include);
663     } else {
664         if (kflag) {
665             f_print(fout, "#include \"h/types.h\"\n");
666             f_print(fout, "#include \"h/socket.h\"\n");
667             f_print(fout, "#include \"h/file.h\"\n");
668             f_print(fout, "#include \"h/stat.h\"\n");
669             f_print(fout, "#include \"netinet/in.h\"\n");
670             f_print(fout, "#include \"h/time.h\"\n");
671             f_print(fout, "#include \"rpc/types.h\"\n");
672             f_print(fout, "#include \"rx/xdr.h\"\n");
673             f_print(fout, "#include \"rxgen_consts.h\"\n");
674             f_print(fout, "#include \"afs/afs_osi.h\"\n");
675             f_print(fout, "#include \"rx/rx.h\"\n");
676             if (xflag) {
677                 f_print(fout, "#include \"rx/rx_globals.h\"\n");
678             }
679         } else {
680             f_print(fout, "#include <sys/types.h>\n");
681             f_print(fout, "#include <rx/xdr.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             f_print(fout, "#include <afs/rxgen_consts.h>\n");
687         }
688     }
689
690     tell = ftell(fout);
691     while (get_definition())
692         continue;
693     if (extend && tell == ftell(fout)) {
694         (void)unlink(outfilename);
695     }
696
697     Cflag = 0;
698 }
699
700 static void
701 S_output(char *infile, char *define, int extend, char *outfile, int append)
702 {
703     char *include;
704     char *outfilename;
705     char fullname[1024];
706     definition *def;
707     long tell;
708     char *currfile = (OutFileFlag ? OutFile : infile);
709
710     Sflag = 1;
711     open_input(infile, define);
712     memset(fullname, 0, sizeof(fullname));
713     if (append) {
714         strcpy(fullname, prefix);
715         strcat(fullname, infile);
716     } else
717         strcpy(fullname, infile);
718     outfilename = extend ? extendfile(fullname, outfile) : outfile;
719     open_output(infile, outfilename);
720     f_print(fout, "/* Machine generated file -- Do NOT edit */\n\n");
721     if (currfile && (include = extendfile(currfile, ".h"))) {
722         if (kflag) {
723             f_print(fout, "#include \"%s\"\n", include);
724         } else {
725             f_print(fout, "#include \"%s\"\n\n", include);
726         }
727         free(include);
728     } else {
729         if (kflag) {
730             f_print(fout, "#include \"h/types.h\"\n");
731             f_print(fout, "#include \"h/socket.h\"\n");
732             f_print(fout, "#include \"h/file.h\"\n");
733             f_print(fout, "#include \"h/stat.h\"\n");
734             f_print(fout, "#include \"netinet/in.h\"\n");
735             f_print(fout, "#include \"h/time.h\"\n");
736             f_print(fout, "#include \"rpc/types.h\"\n");
737             f_print(fout, "#include \"rx/xdr.h\"\n");
738             f_print(fout, "#include \"rxgen_consts.h\"\n");
739             f_print(fout, "#include \"afs/afs_osi.h\"\n");
740             f_print(fout, "#include \"rx/rx.h\"\n");
741             if (xflag) {
742                 f_print(fout, "#include \"rx/rx_globals.h\"\n");
743             }
744         } else {
745             f_print(fout, "#include <sys/types.h>\n");
746             f_print(fout, "#include <rx/xdr.h>\n");
747             f_print(fout, "#include <rx/rx.h>\n");
748             if (xflag) {
749                 f_print(fout, "#include <rx/rx_globals.h>\n");
750             }
751             f_print(fout, "#include <afs/rxgen_consts.h>\n");
752         }
753     }
754
755     tell = ftell(fout);
756     fflush(fout);
757     while ((def = get_definition())) {
758         fflush(fout);
759         print_datadef(def);
760     }
761
762     er_Proc_CodeGeneration();
763
764     if (extend && tell == ftell(fout)) {
765         (void)unlink(outfilename);
766     }
767     Sflag = 0;
768 }
769
770 static char *
771 uppercase(char *str)
772 {
773     static char max_size[100];
774     char *pnt;
775     int len = (int)strlen(str);
776
777     for (pnt = max_size; len > 0; len--, str++) {
778         *pnt++ = (islower(*str) ? toupper(*str) : *str);
779     }
780     *pnt = '\0';
781     return max_size;
782 }
783
784 /*
785  * Parse command line arguments 
786  */
787 static int
788 parseargs(int argc, char *argv[], struct commandline *cmd)
789 {
790     int i;
791     int j;
792     char c;
793     char flag[(1 << (8 * sizeof(char)))];
794     int nflags;
795
796     cmdname = argv[0];
797     cmd->infile = cmd->outfile = NULL;
798     if (argc < 2) {
799         return (0);
800     }
801     memset(flag, 0, sizeof(flag));
802     cmd->outfile = NULL;
803     for (i = 1; i < argc; i++) {
804         if (argv[i][0] != '-') {
805             if (cmd->infile) {
806                 return (0);
807             }
808             cmd->infile = argv[i];
809         } else {
810             for (j = 1; argv[i][j] != 0; j++) {
811                 c = argv[i][j];
812                 switch (c) {
813                 case 'A':
814                 case 'c':
815                 case 'h':
816                 case 'l':
817                 case 'm':
818                 case 'C':
819                 case 'S':
820                 case 'r':
821                 case 'R':
822                 case 'k':
823                 case 'p':
824                 case 'd':
825                 case 'u':
826                 case 'x':
827                 case 'y':
828                 case 'z':
829                     if (flag[(int)c]) {
830                         return (0);
831                     }
832                     flag[(int)c] = 1;
833                     break;
834                 case 'o':
835                 case 's':
836                     if (argv[i][j - 1] != '-' || argv[i][j + 1] != 0) {
837                         return (0);
838                     }
839                     flag[(int)c] = 1;
840                     if (++i == argc) {
841                         return (0);
842                     }
843                     if (c == 's') {
844                         if (!streq(argv[i], "udp") && !streq(argv[i], "tcp")) {
845                             return (0);
846                         }
847                     } else if (c == 'o') {
848                         if (cmd->outfile) {
849                             return (0);
850                         }
851                         cmd->outfile = argv[i];
852                     }
853                     goto nextarg;
854                 case 'P':
855                     if (argv[i][j - 1] != '-')
856                         return (0);
857                     prefix = &argv[i][j + 1];
858                     goto nextarg;
859                 case 'I':
860                     if (argv[i][j - 1] != '-')
861                         return (0);
862                     IncludeDir[nincludes++] = &argv[i][j - 1];
863                     goto nextarg;
864                 default:
865                     return (0);
866                 }
867             }
868           nextarg:
869             ;
870         }
871     }
872     cmd->ansic_flag = ansic_flag = flag['A'];
873     cmd->cflag = cflag = flag['c'];
874     cmd->hflag = hflag = flag['h'];
875     cmd->sflag = flag['s'];
876     cmd->lflag = flag['l'];
877     cmd->mflag = flag['m'];
878     cmd->xflag = xflag = flag['x'];
879     cmd->yflag = yflag = flag['y'];
880     cmd->Cflag = Cflag = flag['C'];
881     cmd->Sflag = Sflag = flag['S'];
882     cmd->rflag = flag['r'];
883     cmd->uflag = uflag = flag['u'];
884     cmd->kflag = kflag = flag['k'];
885     cmd->pflag = flag['p'];
886     cmd->dflag = debug = flag['d'];
887     zflag = flag['z'];
888     if (cmd->pflag)
889         combinepackages = 1;
890     nflags =
891         cmd->cflag + cmd->hflag + cmd->sflag + cmd->lflag + cmd->mflag +
892         cmd->Cflag + cmd->Sflag + cmd->rflag;
893     if (nflags == 0) {
894         if (cmd->outfile != NULL || cmd->infile == NULL) {
895             return (0);
896         }
897     } else if (nflags > 1) {
898         return (0);
899     }
900     return (1);
901 }