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>
48 #ifdef HAVE_SYS_FILE_H
55 #include "rpc_parse.h"
58 #define EXTEND 1 /* alias for TRUE */
80 #define MAXCPPARGS 256 /* maximum number of arguments to cpp */
81 #define MAXCMDLINE 1024 /* MAX chars on a single cmd line */
84 static char *IncludeDir[MAXCPPARGS];
86 char *OutFileFlag = "";
88 char Sflag = 0, Cflag = 0, hflag = 0, cflag = 0, kflag = 0, uflag = 0;
89 char ansic_flag = 0; /* If set, build ANSI C style prototypes */
90 char zflag = 0; /* If set, abort server stub if rpc call returns non-zero */
91 char xflag = 0; /* if set, add stats code to stubs */
92 char yflag = 0; /* if set, only emit function name arrays to xdr file */
94 static int pclose_fin = 0;
97 static char *CPP = NULL;
98 #else /* AFS_NT40_ENV */
100 static char CPP[] = PATH_CPP;
102 static char CPP[] = "/lib/cpp";
104 #endif /* AFS_NT40_ENV */
105 static char CPPFLAGS[] = "-C";
109 * Running "cpp" directly on DEC OSF/1 does not define anything; the "cc"
110 * driver is responsible. To compensate (and allow for other definitions
111 * which should always be passed to "cpp"), place definitions which whould
112 * always be passed to "rxgen" in this table.
114 static char *XTRA_CPPFLAGS[] = {
125 #include "AFS_component_version_number.c"
127 /* static prototypes */
128 static char *extendfile(char *file, char *ext);
129 static void open_output(char *infile, char *outfile);
130 static void open_input(char *infile, char *define);
131 static void c_output(char *infile, char *define, int extend, char *outfile,
133 static void h_output(char *infile, char *define, int extend, char *outfile,
135 static void s_output(int argc, char *argv[], char *infile, char *define,
136 int extend, char *outfile, int nomain);
137 static void l_output(char *infile, char *define, int extend, char *outfile);
138 static void do_registers(int argc, char *argv[]);
139 static int parseargs(int argc, char *argv[], struct commandline *cmd);
140 static void C_output(char *infile, char *define, int extend, char *outfile,
142 static void S_output(char *infile, char *define, int extend, char *outfile,
144 static char *uppercase(char *str);
147 main(int argc, char *argv[])
149 struct commandline cmd;
152 /* initialize CPP with the correct pre-processor for Windows */
153 CPP = getenv("RXGEN_CPPCMD");
155 CPP = "cl /EP /C /nologo";
156 #endif /* AFS_NT40_ENV */
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.
165 struct sigaction nsa;
167 sigemptyset(&nsa.sa_mask);
168 nsa.sa_handler = SIG_DFL;
169 nsa.sa_flags = SA_FULLDUMP;
170 sigaction(SIGSEGV, &nsa, NULL);
173 if (!parseargs(argc, argv, &cmd)) {
174 f_print(stderr, "usage: %s infile\n", cmdname);
176 " %s [-c | -h | -l | -m | -C | -S | -r | -k | -R | -p | -d | -z | -u] [-Pprefix] [-Idir] [-o outfile] [infile]\n",
178 f_print(stderr, " %s [-s udp|tcp]* [-o outfile] [infile]\n",
182 OutFileFlag = cmd.outfile;
184 strcpy(OutFile, cmd.outfile);
187 c_output(cmd.infile, "-DRPC_XDR", !EXTEND, cmd.outfile, 0);
188 } else if (cmd.hflag) {
189 h_output(cmd.infile, "-DRPC_HDR", !EXTEND, cmd.outfile, 0);
190 } else if (cmd.lflag) {
191 l_output(cmd.infile, "-DRPC_CLNT", !EXTEND, cmd.outfile);
192 } else if (cmd.sflag || cmd.mflag) {
193 s_output(argc, argv, cmd.infile, "-DRPC_SVC", !EXTEND, cmd.outfile,
195 } else if (cmd.Cflag) {
197 C_output(cmd.infile, "-DRPC_CLIENT", !EXTEND, cmd.outfile, 1);
198 } else if (cmd.Sflag) {
200 S_output(cmd.infile, "-DRPC_SERVER", !EXTEND, cmd.outfile, 1);
202 if (OutFileFlag && (strrchr(OutFile, '.') == NULL))
203 strcat(OutFile, ".");
205 C_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_CLIENT",
208 S_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_SERVER",
213 c_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_XDR",
214 EXTEND, ".xdr.c", 0);
216 h_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_HDR",
219 C_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_CLIENT",
222 S_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_SERVER",
227 if (fin && pclose_fin) {
228 /* the cpp command we called returned a non-zero exit status */
237 * add extension to filename
240 extendfile(char *file, char *ext)
246 res = alloc(strlen(file) + strlen(ext) + 1);
250 p = (char *)strrchr(file, '.');
252 p = file + strlen(file);
254 sname = (char *)strrchr(file, '/');
260 strcpy(res + (p - sname), ext);
265 * Open output file with given extension
268 open_output(char *infile, char *outfile)
270 if (outfile == NULL) {
274 if (infile != NULL && streq(outfile, infile)) {
275 f_print(stderr, "%s: output would overwrite %s\n", cmdname, infile);
278 fout = fopen(outfile, "w");
280 f_print(stderr, "%s: unable to open ", cmdname);
284 record_open(outfile);
288 * Open input file with given define for C-preprocessor
291 open_input(char *infile, char *define)
293 char cpp_cmdline[MAXCMDLINE];
297 infilename = (infile == NULL) ? "<stdin>" : infile;
298 strcpy(cpp_cmdline, CPP);
299 strcat(cpp_cmdline, " ");
300 strcat(cpp_cmdline, CPPFLAGS);
301 strcat(cpp_cmdline, " ");
302 strcat(cpp_cmdline, define);
306 i < (sizeof(XTRA_CPPFLAGS) / sizeof(XTRA_CPPFLAGS[0])) - 1;
308 strcat(cpp_cmdline, " ");
309 strcat(cpp_cmdline, XTRA_CPPFLAGS[i]);
312 for (i = 0; i < nincludes; i++) {
313 strcat(cpp_cmdline, " ");
314 strcat(cpp_cmdline, IncludeDir[i]);
317 strcat(cpp_cmdline, " ");
318 strcat(cpp_cmdline, infile);
320 fin = popen(cpp_cmdline, "r");
326 if (infile == NULL) {
330 fin = fopen(infile, "r");
333 f_print(stderr, "%s: ", cmdname);
340 * Compile into an XDR routine output file
343 c_output(char *infile, char *define, int extend, char *outfile, int append)
350 char *currfile = (OutFileFlag ? OutFile : infile);
353 open_input(infile, define);
355 memset(fullname, 0, sizeof(fullname));
357 strcpy(fullname, prefix);
358 strcat(fullname, infile);
360 strcpy(fullname, infile);
361 outfilename = extend ? extendfile(fullname, outfile) : outfile;
362 open_output(infile, outfilename);
363 f_print(fout, "/* Machine generated file -- Do NOT edit */\n\n");
366 f_print(fout, "#include \"afsconfig.h\"\n");
367 f_print(fout, "#include \"afs/param.h\"\n");
369 f_print(fout, "#include <afsconfig.h>\n");
370 f_print(fout, "#include <afs/param.h>\n");
372 f_print(fout, "#ifdef AFS_NT40_ENV\n");
373 f_print(fout, "#define AFS_RXGEN_EXPORT __declspec(dllexport)\n");
374 f_print(fout, "#endif /* AFS_NT40_ENV */\n");
376 if (currfile && (include = extendfile(currfile, ".h"))) {
378 f_print(fout, "#include \"%s\"\n\n", include);
380 f_print(fout, "#include \"%s\"\n\n", include);
383 /* In case we can't include the interface's own header file... */
385 f_print(fout, "#include \"h/types.h\"\n");
386 f_print(fout, "#include \"h/socket.h\"\n");
387 f_print(fout, "#include \"h/file.h\"\n");
388 f_print(fout, "#include \"h/stat.h\"\n");
389 f_print(fout, "#include \"netinet/in.h\"\n");
390 f_print(fout, "#include \"h/time.h\"\n");
391 f_print(fout, "#include \"rx/xdr.h\"\n");
392 f_print(fout, "#include \"rxgen_consts.h\"\n");
394 f_print(fout, "#include <rx/xdr.h>\n");
399 while ((def = get_definition())) {
401 if ((!IsRxgenDefinition(def)) && def->def_kind != DEF_CUSTOMIZED)
407 * Print out array containing list of all functions in the interface
412 for (j = 0; j <= PackageIndex; j++) {
413 f_print(fout, "AFS_RXGEN_EXPORT\n");
414 f_print(fout, "const char *%sfunction_names[] = {\n",
417 for (i = 0; i < no_of_stat_funcs_header[j]; i++) {
419 f_print(fout, "\t\"%s\"", function_list[j][i]);
421 f_print(fout, ",\n\t\"%s\"", function_list[j][i]);
425 f_print(fout, "\n};\n");
429 if (extend && tell == ftell(fout)) {
430 (void)unlink(outfilename);
436 * Compile into an XDR header file
439 h_output(char *infile, char *define, int extend, char *outfile, int append)
444 char fullname[1024], *p;
446 open_input(infile, define);
448 memset(fullname, 0, sizeof(fullname));
450 strcpy(fullname, prefix);
451 strcat(fullname, infile);
453 strcpy(fullname, infile);
454 outfilename = extend ? extendfile(fullname, outfile) : outfile;
455 open_output(infile, outfilename);
456 strcpy(fullname, outfilename);
457 if ((p = strchr(fullname, '.')))
459 f_print(fout, "/* Machine generated file -- Do NOT edit */\n\n");
460 f_print(fout, "#ifndef _RXGEN_%s_\n", uppercase(fullname));
461 f_print(fout, "#define _RXGEN_%s_\n\n", uppercase(fullname));
462 f_print(fout, "#ifdef KERNEL\n");
464 "/* The following 'ifndefs' are not a good solution to the vendor's omission of surrounding all system includes with 'ifndef's since it requires that this file is included after the system includes...*/\n");
465 f_print(fout, "#include <afsconfig.h>\n");
466 f_print(fout, "#include \"afs/param.h\"\n");
467 f_print(fout, "#ifdef UKERNEL\n");
468 f_print(fout, "#include \"afs/sysincludes.h\"\n");
469 f_print(fout, "#include \"rx/xdr.h\"\n");
470 f_print(fout, "#include \"rx/rx.h\"\n");
472 f_print(fout, "#include \"rx/rx_globals.h\"\n");
475 f_print(fout, "#include <ubik.h>\n");
476 f_print(fout, "#else /* UKERNEL */\n");
477 f_print(fout, "#include \"h/types.h\"\n");
478 f_print(fout, "#ifndef SOCK_DGRAM /* XXXXX */\n");
479 f_print(fout, "#include \"h/socket.h\"\n");
480 f_print(fout, "#endif\n");
481 f_print(fout, "struct ubik_client;\n");
482 f_print(fout, "#ifndef DTYPE_SOCKET /* XXXXX */\n");
483 f_print(fout, "#ifndef AFS_LINUX22_ENV\n");
484 f_print(fout, "#include \"h/file.h\"\n");
485 f_print(fout, "#endif\n");
486 f_print(fout, "#endif\n");
487 f_print(fout, "#ifndef S_IFMT /* XXXXX */\n");
488 f_print(fout, "#include \"h/stat.h\"\n");
489 f_print(fout, "#endif\n");
490 f_print(fout, "#if defined (AFS_OBSD_ENV) && !defined (MLEN)\n");
491 f_print(fout, "#include \"sys/mbuf.h\"\n");
492 f_print(fout, "#endif\n");
493 f_print(fout, "#ifndef IPPROTO_UDP /* XXXXX */\n");
494 f_print(fout, "#include \"netinet/in.h\"\n");
495 f_print(fout, "#endif\n");
496 f_print(fout, "#ifndef DST_USA /* XXXXX */\n");
497 f_print(fout, "#include \"h/time.h\"\n");
498 f_print(fout, "#endif\n");
499 f_print(fout, "#ifndef AFS_LINUX22_ENV\n");
500 f_print(fout, "#include \"rpc/types.h\"\n");
501 f_print(fout, "#endif /* AFS_LINUX22_ENV */\n");
502 f_print(fout, "#ifndef XDR_GETLONG /* XXXXX */\n");
503 f_print(fout, "#ifdef AFS_LINUX22_ENV\n");
504 f_print(fout, "#ifndef quad_t\n");
505 f_print(fout, "#define quad_t __quad_t\n");
506 f_print(fout, "#define u_quad_t __u_quad_t\n");
507 f_print(fout, "#endif\n");
508 f_print(fout, "#endif\n");
509 f_print(fout, "#include \"rx/xdr.h\"\n");
510 f_print(fout, "#endif /* XDR_GETLONG */\n");
511 f_print(fout, "#endif /* UKERNEL */\n");
512 f_print(fout, "#include \"rxgen_consts.h\"\n");
513 f_print(fout, "#include \"afs_osi.h\"\n");
514 f_print(fout, "#include \"rx/rx.h\"\n");
516 f_print(fout, "#include \"rx/rx_globals.h\"\n");
518 f_print(fout, "#else /* KERNEL */\n");
519 f_print(fout, "#include <afsconfig.h>\n");
520 f_print(fout, "#include <afs/param.h>\n");
521 f_print(fout, "#include <afs/stds.h>\n");
522 f_print(fout, "#include <sys/types.h>\n");
523 f_print(fout, "#include <rx/xdr.h>\n");
524 f_print(fout, "#include <rx/rx.h>\n");
526 f_print(fout, "#include <rx/rx_globals.h>\n");
528 f_print(fout, "#include <afs/rxgen_consts.h>\n");
530 f_print(fout, "#include <ubik.h>\n");
531 f_print(fout, "#endif /* KERNEL */\n\n");
532 f_print(fout, "#ifdef AFS_NT40_ENV\n");
533 f_print(fout, "#ifndef AFS_RXGEN_EXPORT\n");
534 f_print(fout, "#define AFS_RXGEN_EXPORT __declspec(dllimport)\n");
535 f_print(fout, "#endif /* AFS_RXGEN_EXPORT */\n");
536 f_print(fout, "#else /* AFS_NT40_ENV */\n");
537 f_print(fout, "#define AFS_RXGEN_EXPORT\n");
538 f_print(fout, "#endif /* AFS_NT40_ENV */\n\n");
540 while ((def = get_definition())) {
543 h_Proc_CodeGeneration();
546 f_print(fout, "#endif /* _RXGEN_%s_ */\n", uppercase(fullname));
547 if (extend && tell == ftell(fout)) {
548 (void)unlink(outfilename);
553 * Compile into an RPC service
556 s_output(int argc, char *argv[], char *infile, char *define, int extend,
557 char *outfile, int nomain)
564 open_input(infile, define);
565 outfilename = extend ? extendfile(infile, outfile) : outfile;
566 open_output(infile, outfilename);
567 f_print(fout, "#include <stdio.h>\n");
568 f_print(fout, "#include <rpc/rpc.h>\n");
569 if (infile && (include = extendfile(infile, ".h"))) {
570 f_print(fout, "#include \"%s\"\n", include);
574 while ((def = get_definition())) {
575 foundprogram |= (def->def_kind == DEF_PROGRAM);
577 if (extend && !foundprogram) {
578 (void)unlink(outfilename);
582 write_programs((char *)NULL);
585 do_registers(argc, argv);
587 write_programs("static");
592 l_output(char *infile, char *define, int extend, char *outfile)
599 open_input(infile, define);
600 outfilename = extend ? extendfile(infile, outfile) : outfile;
601 open_output(infile, outfilename);
602 f_print(fout, "#include <rpc/rpc.h>\n");
603 f_print(fout, "#include <sys/time.h>\n");
604 if (infile && (include = extendfile(infile, ".h"))) {
605 f_print(fout, "#include \"%s\"\n", include);
609 while ((def = get_definition())) {
610 foundprogram |= (def->def_kind == DEF_PROGRAM);
612 if (extend && !foundprogram) {
613 (void)unlink(outfilename);
620 * Perform registrations for service output
623 do_registers(int argc, char *argv[])
627 for (i = 1; i < argc; i++) {
628 if (streq(argv[i], "-s")) {
629 write_register(argv[i + 1]);
637 C_output(char *infile, char *define, int extend, char *outfile, int append)
643 char *currfile = (OutFileFlag ? OutFile : infile);
646 open_input(infile, define);
647 memset(fullname, 0, sizeof(fullname));
649 strcpy(fullname, prefix);
650 strcat(fullname, infile);
652 strcpy(fullname, infile);
653 outfilename = extend ? extendfile(fullname, outfile) : outfile;
654 open_output(infile, outfilename);
655 f_print(fout, "/* Machine generated file -- Do NOT edit */\n\n");
656 if (currfile && (include = extendfile(currfile, ".h"))) {
658 f_print(fout, "#include \"%s\"\n\n", include);
660 f_print(fout, "#include \"%s\"\n\n", include);
665 f_print(fout, "#include \"h/types.h\"\n");
666 f_print(fout, "#include \"h/socket.h\"\n");
667 f_print(fout, "#include \"h/file.h\"\n");
668 f_print(fout, "#include \"h/stat.h\"\n");
669 f_print(fout, "#include \"netinet/in.h\"\n");
670 f_print(fout, "#include \"h/time.h\"\n");
671 f_print(fout, "#include \"rpc/types.h\"\n");
672 f_print(fout, "#include \"rx/xdr.h\"\n");
673 f_print(fout, "#include \"rxgen_consts.h\"\n");
674 f_print(fout, "#include \"afs/afs_osi.h\"\n");
675 f_print(fout, "#include \"rx/rx.h\"\n");
677 f_print(fout, "#include \"rx/rx_globals.h\"\n");
680 f_print(fout, "#include <sys/types.h>\n");
681 f_print(fout, "#include <rx/xdr.h>\n");
682 f_print(fout, "#include <rx/rx.h>\n");
684 f_print(fout, "#include <rx/rx_globals.h>\n");
686 f_print(fout, "#include <afs/rxgen_consts.h>\n");
691 while (get_definition())
693 if (extend && tell == ftell(fout)) {
694 (void)unlink(outfilename);
701 S_output(char *infile, char *define, int extend, char *outfile, int append)
708 char *currfile = (OutFileFlag ? OutFile : infile);
711 open_input(infile, define);
712 memset(fullname, 0, sizeof(fullname));
714 strcpy(fullname, prefix);
715 strcat(fullname, infile);
717 strcpy(fullname, infile);
718 outfilename = extend ? extendfile(fullname, outfile) : outfile;
719 open_output(infile, outfilename);
720 f_print(fout, "/* Machine generated file -- Do NOT edit */\n\n");
721 if (currfile && (include = extendfile(currfile, ".h"))) {
723 f_print(fout, "#include \"%s\"\n", include);
725 f_print(fout, "#include \"%s\"\n\n", include);
730 f_print(fout, "#include \"h/types.h\"\n");
731 f_print(fout, "#include \"h/socket.h\"\n");
732 f_print(fout, "#include \"h/file.h\"\n");
733 f_print(fout, "#include \"h/stat.h\"\n");
734 f_print(fout, "#include \"netinet/in.h\"\n");
735 f_print(fout, "#include \"h/time.h\"\n");
736 f_print(fout, "#include \"rpc/types.h\"\n");
737 f_print(fout, "#include \"rx/xdr.h\"\n");
738 f_print(fout, "#include \"rxgen_consts.h\"\n");
739 f_print(fout, "#include \"afs/afs_osi.h\"\n");
740 f_print(fout, "#include \"rx/rx.h\"\n");
742 f_print(fout, "#include \"rx/rx_globals.h\"\n");
745 f_print(fout, "#include <sys/types.h>\n");
746 f_print(fout, "#include <rx/xdr.h>\n");
747 f_print(fout, "#include <rx/rx.h>\n");
749 f_print(fout, "#include <rx/rx_globals.h>\n");
751 f_print(fout, "#include <afs/rxgen_consts.h>\n");
757 while ((def = get_definition())) {
762 er_Proc_CodeGeneration();
764 if (extend && tell == ftell(fout)) {
765 (void)unlink(outfilename);
773 static char max_size[100];
775 int len = (int)strlen(str);
777 for (pnt = max_size; len > 0; len--, str++) {
778 *pnt++ = (islower(*str) ? toupper(*str) : *str);
785 * Parse command line arguments
788 parseargs(int argc, char *argv[], struct commandline *cmd)
793 char flag[(1 << (8 * sizeof(char)))];
797 cmd->infile = cmd->outfile = NULL;
801 memset(flag, 0, sizeof(flag));
803 for (i = 1; i < argc; i++) {
804 if (argv[i][0] != '-') {
808 cmd->infile = argv[i];
810 for (j = 1; argv[i][j] != 0; j++) {
836 if (argv[i][j - 1] != '-' || argv[i][j + 1] != 0) {
844 if (!streq(argv[i], "udp") && !streq(argv[i], "tcp")) {
847 } else if (c == 'o') {
851 cmd->outfile = argv[i];
855 if (argv[i][j - 1] != '-')
857 prefix = &argv[i][j + 1];
860 if (argv[i][j - 1] != '-')
862 IncludeDir[nincludes++] = &argv[i][j - 1];
872 cmd->ansic_flag = ansic_flag = flag['A'];
873 cmd->cflag = cflag = flag['c'];
874 cmd->hflag = hflag = flag['h'];
875 cmd->sflag = flag['s'];
876 cmd->lflag = flag['l'];
877 cmd->mflag = flag['m'];
878 cmd->xflag = xflag = flag['x'];
879 cmd->yflag = yflag = flag['y'];
880 cmd->Cflag = Cflag = flag['C'];
881 cmd->Sflag = Sflag = flag['S'];
882 cmd->rflag = flag['r'];
883 cmd->uflag = uflag = flag['u'];
884 cmd->kflag = kflag = flag['k'];
885 cmd->pflag = flag['p'];
886 cmd->dflag = debug = flag['d'];
891 cmd->cflag + cmd->hflag + cmd->sflag + cmd->lflag + cmd->mflag +
892 cmd->Cflag + cmd->Sflag + cmd->rflag;
894 if (cmd->outfile != NULL || cmd->infile == NULL) {
897 } else if (nflags > 1) {