1 /* @(#)rpc_main.c 1.4 87/11/30 3.9 RPCSRC */
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.
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.
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.
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.
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.
26 * Sun Microsystems, Inc.
28 * Mountain View, California 94043
32 * rpc_main.c, Top level of the RPC protocol compiler.
33 * Copyright (C) 1987, Sun Microsystems, Inc.
36 #include <afsconfig.h>
37 #include <afs/param.h>
50 #ifdef HAVE_SYS_FILE_H
57 #include "rpc_parse.h"
60 #define EXTEND 1 /* alias for TRUE */
81 #define MAXCPPARGS 256 /* maximum number of arguments to cpp */
82 #define MAXCMDLINE 1024 /* MAX chars on a single cmd line */
85 static char *IncludeDir[MAXCPPARGS];
87 char *OutFileFlag = "";
89 char Sflag = 0, Cflag = 0, hflag = 0, cflag = 0, kflag = 0, uflag = 0;
90 char zflag = 0; /* If set, abort server stub if rpc call returns non-zero */
91 char xflag = 0; /* if set, add stats code to stubs */
92 char yflag = 0; /* if set, only emit function name arrays to xdr file */
96 static char CPP[] = __PROG_CPP__;
98 static char CPP[] = "/lib/cpp";
100 static char CPPFLAGS[] = "-C";
104 * Running "cpp" directly on DEC OSF/1 does not define anything; the "cc"
105 * driver is responsible. To compensate (and allow for other definitions
106 * which should always be passed to "cpp"), place definitions which whould
107 * always be passed to "rxgen" in this table.
109 static char *XTRA_CPPFLAGS[] = {
120 #include "AFS_component_version_number.c"
122 /* static prototypes */
123 static char *extendfile(char *file, char *ext);
124 static void open_output(char *infile, char *outfile);
125 static void open_input(char *infile, char *define);
126 static void c_output(char *infile, char *define, int extend, char *outfile,
128 static void h_output(char *infile, char *define, int extend, char *outfile,
130 static void s_output(int argc, char *argv[], char *infile, char *define,
131 int extend, char *outfile, int nomain);
132 static void l_output(char *infile, char *define, int extend, char *outfile);
133 static void do_registers(int argc, char *argv[]);
134 static int parseargs(int argc, char *argv[], struct commandline *cmd);
135 static void C_output(char *infile, char *define, int extend, char *outfile,
137 static void S_output(char *infile, char *define, int extend, char *outfile,
139 static char *uppercase(char *str);
142 main(int argc, char *argv[])
144 struct commandline cmd;
148 /* initialize CPP with the correct pre-processor on NT */
149 ep = getenv("RXGEN_CPPCMD");
153 strcpy(CPP, "cl /EP /C /nologo");
157 * The following signal action for AIX is necessary so that in case of a
158 * crash (i.e. core is generated) we can include the user's data section
159 * in the core dump. Unfortunately, by default, only a partial core is
160 * generated which, in many cases, isn't too useful.
162 struct sigaction nsa;
164 sigemptyset(&nsa.sa_mask);
165 nsa.sa_handler = SIG_DFL;
166 nsa.sa_flags = SA_FULLDUMP;
167 sigaction(SIGSEGV, &nsa, NULL);
170 if (!parseargs(argc, argv, &cmd)) {
171 f_print(stderr, "usage: %s infile\n", cmdname);
173 " %s [-c | -h | -l | -m | -C | -S | -r | -k | -R | -p | -d | -z | -u] [-Pprefix] [-Idir] [-o outfile] [infile]\n",
175 f_print(stderr, " %s [-s udp|tcp]* [-o outfile] [infile]\n",
179 OutFileFlag = cmd.outfile;
181 strcpy(OutFile, cmd.outfile);
184 c_output(cmd.infile, "-DRPC_XDR", !EXTEND, cmd.outfile, 0);
185 } else if (cmd.hflag) {
186 h_output(cmd.infile, "-DRPC_HDR", !EXTEND, cmd.outfile, 0);
187 } else if (cmd.lflag) {
188 l_output(cmd.infile, "-DRPC_CLNT", !EXTEND, cmd.outfile);
189 } else if (cmd.sflag || cmd.mflag) {
190 s_output(argc, argv, cmd.infile, "-DRPC_SVC", !EXTEND, cmd.outfile,
192 } else if (cmd.Cflag) {
194 C_output(cmd.infile, "-DRPC_CLIENT", !EXTEND, cmd.outfile, 1);
195 } else if (cmd.Sflag) {
197 S_output(cmd.infile, "-DRPC_SERVER", !EXTEND, cmd.outfile, 1);
199 if (OutFileFlag && (strrchr(OutFile, '.') == NULL))
200 strcat(OutFile, ".");
202 C_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_CLIENT",
205 S_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_SERVER",
210 c_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_XDR",
211 EXTEND, ".xdr.c", 0);
213 h_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_HDR",
216 C_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_CLIENT",
219 S_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_SERVER",
228 * add extension to filename
231 extendfile(char *file, char *ext)
237 res = alloc(strlen(file) + strlen(ext) + 1);
241 p = (char *)strrchr(file, '.');
243 p = file + strlen(file);
245 sname = (char *)strrchr(file, '/');
251 strcpy(res + (p - sname), ext);
256 * Open output file with given extension
259 open_output(char *infile, char *outfile)
261 if (outfile == NULL) {
265 if (infile != NULL && streq(outfile, infile)) {
266 f_print(stderr, "%s: output would overwrite %s\n", cmdname, infile);
269 fout = fopen(outfile, "w");
271 f_print(stderr, "%s: unable to open ", cmdname);
275 record_open(outfile);
279 * Open input file with given define for C-preprocessor
282 open_input(char *infile, char *define)
284 char cpp_cmdline[MAXCMDLINE];
288 infilename = (infile == NULL) ? "<stdin>" : infile;
289 strcpy(cpp_cmdline, CPP);
290 strcat(cpp_cmdline, " ");
291 strcat(cpp_cmdline, CPPFLAGS);
292 strcat(cpp_cmdline, " ");
293 strcat(cpp_cmdline, define);
297 i < (sizeof(XTRA_CPPFLAGS) / sizeof(XTRA_CPPFLAGS[0])) - 1;
299 strcat(cpp_cmdline, " ");
300 strcat(cpp_cmdline, XTRA_CPPFLAGS[i]);
303 for (i = 0; i < nincludes; i++) {
304 strcat(cpp_cmdline, " ");
305 strcat(cpp_cmdline, IncludeDir[i]);
308 strcat(cpp_cmdline, " ");
309 strcat(cpp_cmdline, infile);
311 fin = popen(cpp_cmdline, "r");
316 if (infile == NULL) {
320 fin = fopen(infile, "r");
323 f_print(stderr, "%s: ", cmdname);
330 * Compile into an XDR routine output file
333 c_output(char *infile, char *define, int extend, char *outfile, int append)
340 char *currfile = (OutFileFlag ? OutFile : infile);
343 open_input(infile, define);
345 memset(fullname, 0, sizeof(fullname));
347 strcpy(fullname, prefix);
348 strcat(fullname, infile);
350 strcpy(fullname, infile);
351 outfilename = extend ? extendfile(fullname, outfile) : outfile;
352 open_output(infile, outfilename);
353 f_print(fout, "/* Machine generated file -- Do NOT edit */\n\n");
356 f_print(fout, "#include \"afs/param.h\"\n");
358 f_print(fout, "#include <afs/param.h>\n");
360 f_print(fout, "#ifdef AFS_NT40_ENV\n");
361 f_print(fout, "#define AFS_RXGEN_EXPORT __declspec(dllexport)\n");
362 f_print(fout, "#endif /* AFS_NT40_ENV */\n");
364 if (currfile && (include = extendfile(currfile, ".h"))) {
366 f_print(fout, "#include \"%s\"\n\n", include);
368 f_print(fout, "#include \"%s\"\n\n", include);
371 /* In case we can't include the interface's own header file... */
373 f_print(fout, "#include \"h/types.h\"\n");
374 f_print(fout, "#include \"h/socket.h\"\n");
375 f_print(fout, "#include \"h/file.h\"\n");
376 f_print(fout, "#include \"h/stat.h\"\n");
377 f_print(fout, "#include \"netinet/in.h\"\n");
378 f_print(fout, "#include \"h/time.h\"\n");
379 f_print(fout, "#include \"rx/xdr.h\"\n");
380 f_print(fout, "#include \"rxgen_consts.h\"\n");
382 f_print(fout, "#include <rx/xdr.h>\n");
387 while ((def = get_definition())) {
389 if ((!IsRxgenDefinition(def)) && def->def_kind != DEF_CUSTOMIZED)
395 * Print out array containing list of all functions in the interface
400 for (j = 0; j <= PackageIndex; j++) {
401 f_print(fout, "AFS_RXGEN_EXPORT\n");
402 f_print(fout, "const char *%sfunction_names[] = {\n",
405 for (i = 0; i < no_of_stat_funcs_header[j]; i++) {
407 f_print(fout, "\t\"%s\"", function_list[j][i]);
409 f_print(fout, ",\n\t\"%s\"", function_list[j][i]);
413 f_print(fout, "\n};\n");
417 if (extend && tell == ftell(fout)) {
418 (void)unlink(outfilename);
424 * Compile into an XDR header file
427 h_output(char *infile, char *define, int extend, char *outfile, int append)
432 char fullname[1024], *p;
434 open_input(infile, define);
436 memset(fullname, 0, sizeof(fullname));
438 strcpy(fullname, prefix);
439 strcat(fullname, infile);
441 strcpy(fullname, infile);
442 outfilename = extend ? extendfile(fullname, outfile) : outfile;
443 open_output(infile, outfilename);
444 strcpy(fullname, outfilename);
445 if ((p = strchr(fullname, '.')))
447 f_print(fout, "/* Machine generated file -- Do NOT edit */\n\n");
448 f_print(fout, "#ifndef _RXGEN_%s_\n", uppercase(fullname));
449 f_print(fout, "#define _RXGEN_%s_\n\n", uppercase(fullname));
450 f_print(fout, "#ifdef KERNEL\n");
452 "/* 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");
453 f_print(fout, "#include <afsconfig.h>\n");
454 f_print(fout, "#include \"afs/param.h\"\n");
455 f_print(fout, "#ifdef UKERNEL\n");
456 f_print(fout, "#include \"afs/sysincludes.h\"\n");
457 f_print(fout, "#include \"rx/xdr.h\"\n");
458 f_print(fout, "#include \"rx/rx.h\"\n");
460 f_print(fout, "#include \"rx/rx_globals.h\"\n");
463 f_print(fout, "#include <ubik.h>\n");
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, "struct ubik_client;\n");
470 f_print(fout, "#ifndef DTYPE_SOCKET /* XXXXX */\n");
471 f_print(fout, "#ifndef AFS_LINUX22_ENV\n");
472 f_print(fout, "#include \"h/file.h\"\n");
473 f_print(fout, "#endif\n");
474 f_print(fout, "#endif\n");
475 f_print(fout, "#ifndef S_IFMT /* XXXXX */\n");
476 f_print(fout, "#include \"h/stat.h\"\n");
477 f_print(fout, "#endif\n");
478 f_print(fout, "#if defined (AFS_OBSD_ENV) && !defined (MLEN)\n");
479 f_print(fout, "#include \"sys/mbuf.h\"\n");
480 f_print(fout, "#endif\n");
481 f_print(fout, "#ifndef IPPROTO_UDP /* XXXXX */\n");
482 f_print(fout, "#include \"netinet/in.h\"\n");
483 f_print(fout, "#endif\n");
484 f_print(fout, "#ifndef DST_USA /* XXXXX */\n");
485 f_print(fout, "#include \"h/time.h\"\n");
486 f_print(fout, "#endif\n");
487 f_print(fout, "#ifndef AFS_LINUX22_ENV\n");
488 f_print(fout, "#include \"rpc/types.h\"\n");
489 f_print(fout, "#endif /* AFS_LINUX22_ENV */\n");
490 f_print(fout, "#ifndef XDR_GETLONG /* XXXXX */\n");
491 f_print(fout, "#ifdef AFS_LINUX22_ENV\n");
492 f_print(fout, "#ifndef quad_t\n");
493 f_print(fout, "#define quad_t __quad_t\n");
494 f_print(fout, "#define u_quad_t __u_quad_t\n");
495 f_print(fout, "#endif\n");
496 f_print(fout, "#endif\n");
497 f_print(fout, "#include \"rx/xdr.h\"\n");
498 f_print(fout, "#endif /* XDR_GETLONG */\n");
499 f_print(fout, "#endif /* UKERNEL */\n");
500 f_print(fout, "#include \"rxgen_consts.h\"\n");
501 f_print(fout, "#include \"afs_osi.h\"\n");
502 f_print(fout, "#include \"rx/rx.h\"\n");
504 f_print(fout, "#include \"rx/rx_globals.h\"\n");
506 f_print(fout, "#else /* KERNEL */\n");
507 f_print(fout, "#include <afs/param.h>\n");
508 f_print(fout, "#include <afs/stds.h>\n");
509 f_print(fout, "#include <sys/types.h>\n");
510 f_print(fout, "#include <rx/xdr.h>\n");
511 f_print(fout, "#include <rx/rx.h>\n");
513 f_print(fout, "#include <rx/rx_globals.h>\n");
515 f_print(fout, "#include <afs/rxgen_consts.h>\n");
517 f_print(fout, "#include <ubik.h>\n");
518 f_print(fout, "#endif /* KERNEL */\n\n");
519 f_print(fout, "#ifdef AFS_NT40_ENV\n");
520 f_print(fout, "#ifndef AFS_RXGEN_EXPORT\n");
521 f_print(fout, "#define AFS_RXGEN_EXPORT __declspec(dllimport)\n");
522 f_print(fout, "#endif /* AFS_RXGEN_EXPORT */\n");
523 f_print(fout, "#else /* AFS_NT40_ENV */\n");
524 f_print(fout, "#define AFS_RXGEN_EXPORT\n");
525 f_print(fout, "#endif /* AFS_NT40_ENV */\n\n");
527 while ((def = get_definition())) {
532 f_print(fout, "#endif /* _RXGEN_%s_ */\n", uppercase(fullname));
533 if (extend && tell == ftell(fout)) {
534 (void)unlink(outfilename);
539 * Compile into an RPC service
542 s_output(int argc, char *argv[], char *infile, char *define, int extend,
543 char *outfile, int nomain)
550 open_input(infile, define);
551 outfilename = extend ? extendfile(infile, outfile) : outfile;
552 open_output(infile, outfilename);
553 f_print(fout, "#include <stdio.h>\n");
554 f_print(fout, "#include <rpc/rpc.h>\n");
555 if (infile && (include = extendfile(infile, ".h"))) {
556 f_print(fout, "#include \"%s\"\n", include);
560 while ((def = get_definition())) {
561 foundprogram |= (def->def_kind == DEF_PROGRAM);
563 if (extend && !foundprogram) {
564 (void)unlink(outfilename);
568 write_programs((char *)NULL);
571 do_registers(argc, argv);
573 write_programs("static");
578 l_output(char *infile, char *define, int extend, char *outfile)
585 open_input(infile, define);
586 outfilename = extend ? extendfile(infile, outfile) : outfile;
587 open_output(infile, outfilename);
588 f_print(fout, "#include <rpc/rpc.h>\n");
589 f_print(fout, "#include <sys/time.h>\n");
590 if (infile && (include = extendfile(infile, ".h"))) {
591 f_print(fout, "#include \"%s\"\n", include);
595 while ((def = get_definition())) {
596 foundprogram |= (def->def_kind == DEF_PROGRAM);
598 if (extend && !foundprogram) {
599 (void)unlink(outfilename);
606 * Perform registrations for service output
609 do_registers(int argc, char *argv[])
613 for (i = 1; i < argc; i++) {
614 if (streq(argv[i], "-s")) {
615 write_register(argv[i + 1]);
623 C_output(char *infile, char *define, int extend, char *outfile, int append)
629 char *currfile = (OutFileFlag ? OutFile : infile);
632 open_input(infile, define);
633 memset(fullname, 0, sizeof(fullname));
635 strcpy(fullname, prefix);
636 strcat(fullname, infile);
638 strcpy(fullname, infile);
639 outfilename = extend ? extendfile(fullname, outfile) : outfile;
640 open_output(infile, outfilename);
641 f_print(fout, "/* Machine generated file -- Do NOT edit */\n\n");
642 if (currfile && (include = extendfile(currfile, ".h"))) {
644 f_print(fout, "#include \"%s\"\n\n", include);
646 f_print(fout, "#include \"%s\"\n\n", include);
651 f_print(fout, "#include \"h/types.h\"\n");
652 f_print(fout, "#include \"h/socket.h\"\n");
653 f_print(fout, "#include \"h/file.h\"\n");
654 f_print(fout, "#include \"h/stat.h\"\n");
655 f_print(fout, "#include \"netinet/in.h\"\n");
656 f_print(fout, "#include \"h/time.h\"\n");
657 f_print(fout, "#include \"rpc/types.h\"\n");
658 f_print(fout, "#include \"rx/xdr.h\"\n");
659 f_print(fout, "#include \"rxgen_consts.h\"\n");
660 f_print(fout, "#include \"afs/afs_osi.h\"\n");
661 f_print(fout, "#include \"rx/rx.h\"\n");
663 f_print(fout, "#include \"rx/rx_globals.h\"\n");
666 f_print(fout, "#include <sys/types.h>\n");
667 f_print(fout, "#include <rx/xdr.h>\n");
668 f_print(fout, "#include <rx/rx.h>\n");
670 f_print(fout, "#include <rx/rx_globals.h>\n");
672 f_print(fout, "#include <afs/rxgen_consts.h>\n");
677 while (get_definition())
679 if (extend && tell == ftell(fout)) {
680 (void)unlink(outfilename);
687 S_output(char *infile, char *define, int extend, char *outfile, int append)
694 char *currfile = (OutFileFlag ? OutFile : infile);
697 open_input(infile, define);
698 memset(fullname, 0, sizeof(fullname));
700 strcpy(fullname, prefix);
701 strcat(fullname, infile);
703 strcpy(fullname, infile);
704 outfilename = extend ? extendfile(fullname, outfile) : outfile;
705 open_output(infile, outfilename);
706 f_print(fout, "/* Machine generated file -- Do NOT edit */\n\n");
707 if (currfile && (include = extendfile(currfile, ".h"))) {
709 f_print(fout, "#include \"%s\"\n", include);
711 f_print(fout, "#include \"%s\"\n\n", include);
716 f_print(fout, "#include \"h/types.h\"\n");
717 f_print(fout, "#include \"h/socket.h\"\n");
718 f_print(fout, "#include \"h/file.h\"\n");
719 f_print(fout, "#include \"h/stat.h\"\n");
720 f_print(fout, "#include \"netinet/in.h\"\n");
721 f_print(fout, "#include \"h/time.h\"\n");
722 f_print(fout, "#include \"rpc/types.h\"\n");
723 f_print(fout, "#include \"rx/xdr.h\"\n");
724 f_print(fout, "#include \"rxgen_consts.h\"\n");
725 f_print(fout, "#include \"afs/afs_osi.h\"\n");
726 f_print(fout, "#include \"rx/rx.h\"\n");
728 f_print(fout, "#include \"rx/rx_globals.h\"\n");
731 f_print(fout, "#include <sys/types.h>\n");
732 f_print(fout, "#include <rx/xdr.h>\n");
733 f_print(fout, "#include <rx/rx.h>\n");
735 f_print(fout, "#include <rx/rx_globals.h>\n");
737 f_print(fout, "#include <afs/rxgen_consts.h>\n");
743 while ((def = get_definition())) {
748 er_Proc_CodeGeneration();
750 if (extend && tell == ftell(fout)) {
751 (void)unlink(outfilename);
759 static char max_size[100];
761 int len = (int)strlen(str);
763 for (pnt = max_size; len > 0; len--, str++) {
764 *pnt++ = (islower(*str) ? toupper(*str) : *str);
771 * Parse command line arguments
774 parseargs(int argc, char *argv[], struct commandline *cmd)
779 char flag[(1 << (8 * sizeof(char)))];
783 cmd->infile = cmd->outfile = NULL;
787 memset(flag, 0, sizeof(flag));
789 for (i = 1; i < argc; i++) {
790 if (argv[i][0] != '-') {
794 cmd->infile = argv[i];
796 for (j = 1; argv[i][j] != 0; j++) {
821 if (argv[i][j - 1] != '-' || argv[i][j + 1] != 0) {
829 if (!streq(argv[i], "udp") && !streq(argv[i], "tcp")) {
832 } else if (c == 'o') {
836 cmd->outfile = argv[i];
840 if (argv[i][j - 1] != '-')
842 prefix = &argv[i][j + 1];
845 if (argv[i][j - 1] != '-')
847 IncludeDir[nincludes++] = &argv[i][j - 1];
857 cmd->cflag = cflag = flag['c'];
858 cmd->hflag = hflag = flag['h'];
859 cmd->sflag = flag['s'];
860 cmd->lflag = flag['l'];
861 cmd->mflag = flag['m'];
862 cmd->xflag = xflag = flag['x'];
863 cmd->yflag = yflag = flag['y'];
864 cmd->Cflag = Cflag = flag['C'];
865 cmd->Sflag = Sflag = flag['S'];
866 cmd->rflag = flag['r'];
867 cmd->uflag = uflag = flag['u'];
868 cmd->kflag = kflag = flag['k'];
869 cmd->pflag = flag['p'];
870 cmd->dflag = debug = flag['d'];
875 cmd->cflag + cmd->hflag + cmd->sflag + cmd->lflag + cmd->mflag +
876 cmd->Cflag + cmd->Sflag + cmd->rflag;
878 if (cmd->outfile != NULL || cmd->infile == NULL) {
881 } else if (nflags > 1) {