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 */
96 static char *CPP = NULL;
97 #else /* AFS_NT40_ENV */
99 static char CPP[] = PATH_CPP;
101 static char CPP[] = "/lib/cpp";
103 #endif /* AFS_NT40_ENV */
104 static char CPPFLAGS[] = "-C";
108 * Running "cpp" directly on DEC OSF/1 does not define anything; the "cc"
109 * driver is responsible. To compensate (and allow for other definitions
110 * which should always be passed to "cpp"), place definitions which whould
111 * always be passed to "rxgen" in this table.
113 static char *XTRA_CPPFLAGS[] = {
124 #include "AFS_component_version_number.c"
126 /* static prototypes */
127 static char *extendfile(char *file, char *ext);
128 static void open_output(char *infile, char *outfile);
129 static void open_input(char *infile, char *define);
130 static void c_output(char *infile, char *define, int extend, char *outfile,
132 static void h_output(char *infile, char *define, int extend, char *outfile,
134 static void s_output(int argc, char *argv[], char *infile, char *define,
135 int extend, char *outfile, int nomain);
136 static void l_output(char *infile, char *define, int extend, char *outfile);
137 static void do_registers(int argc, char *argv[]);
138 static int parseargs(int argc, char *argv[], struct commandline *cmd);
139 static void C_output(char *infile, char *define, int extend, char *outfile,
141 static void S_output(char *infile, char *define, int extend, char *outfile,
143 static char *uppercase(char *str);
146 main(int argc, char *argv[])
148 struct commandline cmd;
151 /* initialize CPP with the correct pre-processor for Windows */
152 CPP = getenv("RXGEN_CPPCMD");
154 CPP = "cl /EP /C /nologo";
155 #endif /* AFS_NT40_ENV */
159 * The following signal action for AIX is necessary so that in case of a
160 * crash (i.e. core is generated) we can include the user's data section
161 * in the core dump. Unfortunately, by default, only a partial core is
162 * generated which, in many cases, isn't too useful.
164 struct sigaction nsa;
166 sigemptyset(&nsa.sa_mask);
167 nsa.sa_handler = SIG_DFL;
168 nsa.sa_flags = SA_FULLDUMP;
169 sigaction(SIGSEGV, &nsa, NULL);
172 if (!parseargs(argc, argv, &cmd)) {
173 f_print(stderr, "usage: %s infile\n", cmdname);
175 " %s [-c | -h | -l | -m | -C | -S | -r | -k | -R | -p | -d | -z | -u] [-Pprefix] [-Idir] [-o outfile] [infile]\n",
177 f_print(stderr, " %s [-s udp|tcp]* [-o outfile] [infile]\n",
181 OutFileFlag = cmd.outfile;
183 strcpy(OutFile, cmd.outfile);
186 c_output(cmd.infile, "-DRPC_XDR", !EXTEND, cmd.outfile, 0);
187 } else if (cmd.hflag) {
188 h_output(cmd.infile, "-DRPC_HDR", !EXTEND, cmd.outfile, 0);
189 } else if (cmd.lflag) {
190 l_output(cmd.infile, "-DRPC_CLNT", !EXTEND, cmd.outfile);
191 } else if (cmd.sflag || cmd.mflag) {
192 s_output(argc, argv, cmd.infile, "-DRPC_SVC", !EXTEND, cmd.outfile,
194 } else if (cmd.Cflag) {
196 C_output(cmd.infile, "-DRPC_CLIENT", !EXTEND, cmd.outfile, 1);
197 } else if (cmd.Sflag) {
199 S_output(cmd.infile, "-DRPC_SERVER", !EXTEND, cmd.outfile, 1);
201 if (OutFileFlag && (strrchr(OutFile, '.') == NULL))
202 strcat(OutFile, ".");
204 C_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_CLIENT",
207 S_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_SERVER",
212 c_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_XDR",
213 EXTEND, ".xdr.c", 0);
215 h_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_HDR",
218 C_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_CLIENT",
221 S_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_SERVER",
230 * add extension to filename
233 extendfile(char *file, char *ext)
239 res = alloc(strlen(file) + strlen(ext) + 1);
243 p = (char *)strrchr(file, '.');
245 p = file + strlen(file);
247 sname = (char *)strrchr(file, '/');
253 strcpy(res + (p - sname), ext);
258 * Open output file with given extension
261 open_output(char *infile, char *outfile)
263 if (outfile == NULL) {
267 if (infile != NULL && streq(outfile, infile)) {
268 f_print(stderr, "%s: output would overwrite %s\n", cmdname, infile);
271 fout = fopen(outfile, "w");
273 f_print(stderr, "%s: unable to open ", cmdname);
277 record_open(outfile);
281 * Open input file with given define for C-preprocessor
284 open_input(char *infile, char *define)
286 char cpp_cmdline[MAXCMDLINE];
290 infilename = (infile == NULL) ? "<stdin>" : infile;
291 strcpy(cpp_cmdline, CPP);
292 strcat(cpp_cmdline, " ");
293 strcat(cpp_cmdline, CPPFLAGS);
294 strcat(cpp_cmdline, " ");
295 strcat(cpp_cmdline, define);
299 i < (sizeof(XTRA_CPPFLAGS) / sizeof(XTRA_CPPFLAGS[0])) - 1;
301 strcat(cpp_cmdline, " ");
302 strcat(cpp_cmdline, XTRA_CPPFLAGS[i]);
305 for (i = 0; i < nincludes; i++) {
306 strcat(cpp_cmdline, " ");
307 strcat(cpp_cmdline, IncludeDir[i]);
310 strcat(cpp_cmdline, " ");
311 strcat(cpp_cmdline, infile);
313 fin = popen(cpp_cmdline, "r");
318 if (infile == NULL) {
322 fin = fopen(infile, "r");
325 f_print(stderr, "%s: ", cmdname);
332 * Compile into an XDR routine output file
335 c_output(char *infile, char *define, int extend, char *outfile, int append)
342 char *currfile = (OutFileFlag ? OutFile : infile);
345 open_input(infile, define);
347 memset(fullname, 0, sizeof(fullname));
349 strcpy(fullname, prefix);
350 strcat(fullname, infile);
352 strcpy(fullname, infile);
353 outfilename = extend ? extendfile(fullname, outfile) : outfile;
354 open_output(infile, outfilename);
355 f_print(fout, "/* Machine generated file -- Do NOT edit */\n\n");
358 f_print(fout, "#include \"afs/param.h\"\n");
360 f_print(fout, "#include <afs/param.h>\n");
362 f_print(fout, "#ifdef AFS_NT40_ENV\n");
363 f_print(fout, "#define AFS_RXGEN_EXPORT __declspec(dllexport)\n");
364 f_print(fout, "#endif /* AFS_NT40_ENV */\n");
366 if (currfile && (include = extendfile(currfile, ".h"))) {
368 f_print(fout, "#include \"%s\"\n\n", include);
370 f_print(fout, "#include \"%s\"\n\n", include);
373 /* In case we can't include the interface's own header file... */
375 f_print(fout, "#include \"h/types.h\"\n");
376 f_print(fout, "#include \"h/socket.h\"\n");
377 f_print(fout, "#include \"h/file.h\"\n");
378 f_print(fout, "#include \"h/stat.h\"\n");
379 f_print(fout, "#include \"netinet/in.h\"\n");
380 f_print(fout, "#include \"h/time.h\"\n");
381 f_print(fout, "#include \"rx/xdr.h\"\n");
382 f_print(fout, "#include \"rxgen_consts.h\"\n");
384 f_print(fout, "#include <rx/xdr.h>\n");
389 while ((def = get_definition())) {
391 if ((!IsRxgenDefinition(def)) && def->def_kind != DEF_CUSTOMIZED)
397 * Print out array containing list of all functions in the interface
402 for (j = 0; j <= PackageIndex; j++) {
403 f_print(fout, "AFS_RXGEN_EXPORT\n");
404 f_print(fout, "const char *%sfunction_names[] = {\n",
407 for (i = 0; i < no_of_stat_funcs_header[j]; i++) {
409 f_print(fout, "\t\"%s\"", function_list[j][i]);
411 f_print(fout, ",\n\t\"%s\"", function_list[j][i]);
415 f_print(fout, "\n};\n");
419 if (extend && tell == ftell(fout)) {
420 (void)unlink(outfilename);
426 * Compile into an XDR header file
429 h_output(char *infile, char *define, int extend, char *outfile, int append)
434 char fullname[1024], *p;
436 open_input(infile, define);
438 memset(fullname, 0, sizeof(fullname));
440 strcpy(fullname, prefix);
441 strcat(fullname, infile);
443 strcpy(fullname, infile);
444 outfilename = extend ? extendfile(fullname, outfile) : outfile;
445 open_output(infile, outfilename);
446 strcpy(fullname, outfilename);
447 if ((p = strchr(fullname, '.')))
449 f_print(fout, "/* Machine generated file -- Do NOT edit */\n\n");
450 f_print(fout, "#ifndef _RXGEN_%s_\n", uppercase(fullname));
451 f_print(fout, "#define _RXGEN_%s_\n\n", uppercase(fullname));
452 f_print(fout, "#ifdef KERNEL\n");
454 "/* The following 'ifndefs' are not a good solution to the vendor's omission of surrounding all system includes with 'ifndef's since it requires that this file is included after the system includes...*/\n");
455 f_print(fout, "#include <afsconfig.h>\n");
456 f_print(fout, "#include \"afs/param.h\"\n");
457 f_print(fout, "#ifdef UKERNEL\n");
458 f_print(fout, "#include \"afs/sysincludes.h\"\n");
459 f_print(fout, "#include \"rx/xdr.h\"\n");
460 f_print(fout, "#include \"rx/rx.h\"\n");
462 f_print(fout, "#include \"rx/rx_globals.h\"\n");
465 f_print(fout, "#include <ubik.h>\n");
466 f_print(fout, "#else /* UKERNEL */\n");
467 f_print(fout, "#include \"h/types.h\"\n");
468 f_print(fout, "#ifndef SOCK_DGRAM /* XXXXX */\n");
469 f_print(fout, "#include \"h/socket.h\"\n");
470 f_print(fout, "#endif\n");
471 f_print(fout, "struct ubik_client;\n");
472 f_print(fout, "#ifndef DTYPE_SOCKET /* XXXXX */\n");
473 f_print(fout, "#ifndef AFS_LINUX22_ENV\n");
474 f_print(fout, "#include \"h/file.h\"\n");
475 f_print(fout, "#endif\n");
476 f_print(fout, "#endif\n");
477 f_print(fout, "#ifndef S_IFMT /* XXXXX */\n");
478 f_print(fout, "#include \"h/stat.h\"\n");
479 f_print(fout, "#endif\n");
480 f_print(fout, "#if defined (AFS_OBSD_ENV) && !defined (MLEN)\n");
481 f_print(fout, "#include \"sys/mbuf.h\"\n");
482 f_print(fout, "#endif\n");
483 f_print(fout, "#ifndef IPPROTO_UDP /* XXXXX */\n");
484 f_print(fout, "#include \"netinet/in.h\"\n");
485 f_print(fout, "#endif\n");
486 f_print(fout, "#ifndef DST_USA /* XXXXX */\n");
487 f_print(fout, "#include \"h/time.h\"\n");
488 f_print(fout, "#endif\n");
489 f_print(fout, "#ifndef AFS_LINUX22_ENV\n");
490 f_print(fout, "#include \"rpc/types.h\"\n");
491 f_print(fout, "#endif /* AFS_LINUX22_ENV */\n");
492 f_print(fout, "#ifndef XDR_GETLONG /* XXXXX */\n");
493 f_print(fout, "#ifdef AFS_LINUX22_ENV\n");
494 f_print(fout, "#ifndef quad_t\n");
495 f_print(fout, "#define quad_t __quad_t\n");
496 f_print(fout, "#define u_quad_t __u_quad_t\n");
497 f_print(fout, "#endif\n");
498 f_print(fout, "#endif\n");
499 f_print(fout, "#include \"rx/xdr.h\"\n");
500 f_print(fout, "#endif /* XDR_GETLONG */\n");
501 f_print(fout, "#endif /* UKERNEL */\n");
502 f_print(fout, "#include \"rxgen_consts.h\"\n");
503 f_print(fout, "#include \"afs_osi.h\"\n");
504 f_print(fout, "#include \"rx/rx.h\"\n");
506 f_print(fout, "#include \"rx/rx_globals.h\"\n");
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");
515 f_print(fout, "#include <rx/rx_globals.h>\n");
517 f_print(fout, "#include <afs/rxgen_consts.h>\n");
519 f_print(fout, "#include <ubik.h>\n");
520 f_print(fout, "#endif /* KERNEL */\n\n");
521 f_print(fout, "#ifdef AFS_NT40_ENV\n");
522 f_print(fout, "#ifndef AFS_RXGEN_EXPORT\n");
523 f_print(fout, "#define AFS_RXGEN_EXPORT __declspec(dllimport)\n");
524 f_print(fout, "#endif /* AFS_RXGEN_EXPORT */\n");
525 f_print(fout, "#else /* AFS_NT40_ENV */\n");
526 f_print(fout, "#define AFS_RXGEN_EXPORT\n");
527 f_print(fout, "#endif /* AFS_NT40_ENV */\n\n");
529 while ((def = get_definition())) {
532 h_Proc_CodeGeneration();
535 f_print(fout, "#endif /* _RXGEN_%s_ */\n", uppercase(fullname));
536 if (extend && tell == ftell(fout)) {
537 (void)unlink(outfilename);
542 * Compile into an RPC service
545 s_output(int argc, char *argv[], char *infile, char *define, int extend,
546 char *outfile, int nomain)
553 open_input(infile, define);
554 outfilename = extend ? extendfile(infile, outfile) : outfile;
555 open_output(infile, outfilename);
556 f_print(fout, "#include <stdio.h>\n");
557 f_print(fout, "#include <rpc/rpc.h>\n");
558 if (infile && (include = extendfile(infile, ".h"))) {
559 f_print(fout, "#include \"%s\"\n", include);
563 while ((def = get_definition())) {
564 foundprogram |= (def->def_kind == DEF_PROGRAM);
566 if (extend && !foundprogram) {
567 (void)unlink(outfilename);
571 write_programs((char *)NULL);
574 do_registers(argc, argv);
576 write_programs("static");
581 l_output(char *infile, char *define, int extend, char *outfile)
588 open_input(infile, define);
589 outfilename = extend ? extendfile(infile, outfile) : outfile;
590 open_output(infile, outfilename);
591 f_print(fout, "#include <rpc/rpc.h>\n");
592 f_print(fout, "#include <sys/time.h>\n");
593 if (infile && (include = extendfile(infile, ".h"))) {
594 f_print(fout, "#include \"%s\"\n", include);
598 while ((def = get_definition())) {
599 foundprogram |= (def->def_kind == DEF_PROGRAM);
601 if (extend && !foundprogram) {
602 (void)unlink(outfilename);
609 * Perform registrations for service output
612 do_registers(int argc, char *argv[])
616 for (i = 1; i < argc; i++) {
617 if (streq(argv[i], "-s")) {
618 write_register(argv[i + 1]);
626 C_output(char *infile, char *define, int extend, char *outfile, int append)
632 char *currfile = (OutFileFlag ? OutFile : infile);
635 open_input(infile, define);
636 memset(fullname, 0, sizeof(fullname));
638 strcpy(fullname, prefix);
639 strcat(fullname, infile);
641 strcpy(fullname, infile);
642 outfilename = extend ? extendfile(fullname, outfile) : outfile;
643 open_output(infile, outfilename);
644 f_print(fout, "/* Machine generated file -- Do NOT edit */\n\n");
645 if (currfile && (include = extendfile(currfile, ".h"))) {
647 f_print(fout, "#include \"%s\"\n\n", include);
649 f_print(fout, "#include \"%s\"\n\n", include);
654 f_print(fout, "#include \"h/types.h\"\n");
655 f_print(fout, "#include \"h/socket.h\"\n");
656 f_print(fout, "#include \"h/file.h\"\n");
657 f_print(fout, "#include \"h/stat.h\"\n");
658 f_print(fout, "#include \"netinet/in.h\"\n");
659 f_print(fout, "#include \"h/time.h\"\n");
660 f_print(fout, "#include \"rpc/types.h\"\n");
661 f_print(fout, "#include \"rx/xdr.h\"\n");
662 f_print(fout, "#include \"rxgen_consts.h\"\n");
663 f_print(fout, "#include \"afs/afs_osi.h\"\n");
664 f_print(fout, "#include \"rx/rx.h\"\n");
666 f_print(fout, "#include \"rx/rx_globals.h\"\n");
669 f_print(fout, "#include <sys/types.h>\n");
670 f_print(fout, "#include <rx/xdr.h>\n");
671 f_print(fout, "#include <rx/rx.h>\n");
673 f_print(fout, "#include <rx/rx_globals.h>\n");
675 f_print(fout, "#include <afs/rxgen_consts.h>\n");
680 while (get_definition())
682 if (extend && tell == ftell(fout)) {
683 (void)unlink(outfilename);
690 S_output(char *infile, char *define, int extend, char *outfile, int append)
697 char *currfile = (OutFileFlag ? OutFile : infile);
700 open_input(infile, define);
701 memset(fullname, 0, sizeof(fullname));
703 strcpy(fullname, prefix);
704 strcat(fullname, infile);
706 strcpy(fullname, infile);
707 outfilename = extend ? extendfile(fullname, outfile) : outfile;
708 open_output(infile, outfilename);
709 f_print(fout, "/* Machine generated file -- Do NOT edit */\n\n");
710 if (currfile && (include = extendfile(currfile, ".h"))) {
712 f_print(fout, "#include \"%s\"\n", include);
714 f_print(fout, "#include \"%s\"\n\n", include);
719 f_print(fout, "#include \"h/types.h\"\n");
720 f_print(fout, "#include \"h/socket.h\"\n");
721 f_print(fout, "#include \"h/file.h\"\n");
722 f_print(fout, "#include \"h/stat.h\"\n");
723 f_print(fout, "#include \"netinet/in.h\"\n");
724 f_print(fout, "#include \"h/time.h\"\n");
725 f_print(fout, "#include \"rpc/types.h\"\n");
726 f_print(fout, "#include \"rx/xdr.h\"\n");
727 f_print(fout, "#include \"rxgen_consts.h\"\n");
728 f_print(fout, "#include \"afs/afs_osi.h\"\n");
729 f_print(fout, "#include \"rx/rx.h\"\n");
731 f_print(fout, "#include \"rx/rx_globals.h\"\n");
734 f_print(fout, "#include <sys/types.h>\n");
735 f_print(fout, "#include <rx/xdr.h>\n");
736 f_print(fout, "#include <rx/rx.h>\n");
738 f_print(fout, "#include <rx/rx_globals.h>\n");
740 f_print(fout, "#include <afs/rxgen_consts.h>\n");
746 while ((def = get_definition())) {
751 er_Proc_CodeGeneration();
753 if (extend && tell == ftell(fout)) {
754 (void)unlink(outfilename);
762 static char max_size[100];
764 int len = (int)strlen(str);
766 for (pnt = max_size; len > 0; len--, str++) {
767 *pnt++ = (islower(*str) ? toupper(*str) : *str);
774 * Parse command line arguments
777 parseargs(int argc, char *argv[], struct commandline *cmd)
782 char flag[(1 << (8 * sizeof(char)))];
786 cmd->infile = cmd->outfile = NULL;
790 memset(flag, 0, sizeof(flag));
792 for (i = 1; i < argc; i++) {
793 if (argv[i][0] != '-') {
797 cmd->infile = argv[i];
799 for (j = 1; argv[i][j] != 0; j++) {
825 if (argv[i][j - 1] != '-' || argv[i][j + 1] != 0) {
833 if (!streq(argv[i], "udp") && !streq(argv[i], "tcp")) {
836 } else if (c == 'o') {
840 cmd->outfile = argv[i];
844 if (argv[i][j - 1] != '-')
846 prefix = &argv[i][j + 1];
849 if (argv[i][j - 1] != '-')
851 IncludeDir[nincludes++] = &argv[i][j - 1];
861 cmd->ansic_flag = ansic_flag = flag['A'];
862 cmd->cflag = cflag = flag['c'];
863 cmd->hflag = hflag = flag['h'];
864 cmd->sflag = flag['s'];
865 cmd->lflag = flag['l'];
866 cmd->mflag = flag['m'];
867 cmd->xflag = xflag = flag['x'];
868 cmd->yflag = yflag = flag['y'];
869 cmd->Cflag = Cflag = flag['C'];
870 cmd->Sflag = Sflag = flag['S'];
871 cmd->rflag = flag['r'];
872 cmd->uflag = uflag = flag['u'];
873 cmd->kflag = kflag = flag['k'];
874 cmd->pflag = flag['p'];
875 cmd->dflag = debug = flag['d'];
880 cmd->cflag + cmd->hflag + cmd->sflag + cmd->lflag + cmd->mflag +
881 cmd->Cflag + cmd->Sflag + cmd->rflag;
883 if (cmd->outfile != NULL || cmd->infile == NULL) {
886 } else if (nflags > 1) {