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