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