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