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>
56 #ifdef HAVE_SYS_FILE_H
63 #include "rpc_parse.h"
66 #define EXTEND 1 /* alias for TRUE */
87 #define MAXCPPARGS 256 /* maximum number of arguments to cpp */
88 #define MAXCMDLINE 1024 /* MAX chars on a single cmd line */
91 static char *IncludeDir[MAXCPPARGS];
93 char *OutFileFlag = "";
95 char Sflag = 0, Cflag = 0, hflag = 0, cflag = 0, kflag = 0, uflag = 0;
96 char zflag = 0; /* If set, abort server stub if rpc call returns non-zero */
97 char xflag = 0; /* if set, add stats code to stubs */
98 char yflag = 0; /* if set, only emit function name arrays to xdr file */
100 static char *cmdname;
102 static char CPP[] = __PROG_CPP__;
104 static char CPP[] = "/lib/cpp";
106 static char CPPFLAGS[] = "-C";
110 * Running "cpp" directly on DEC OSF/1 does not define anything; the "cc"
111 * driver is responsible. To compensate (and allow for other definitions
112 * which should always be passed to "cpp"), place definitions which whould
113 * always be passed to "rxgen" in this table.
115 static char *XTRA_CPPFLAGS[] = {
126 #include "AFS_component_version_number.c"
128 /* static prototypes */
129 static char *extendfile(char *file, char *ext);
130 static void open_output(char *infile, char *outfile);
131 static void open_input(char *infile, char *define);
132 static void c_output(char *infile, char *define, int extend, char *outfile,
134 static void h_output(char *infile, char *define, int extend, char *outfile,
136 static void s_output(int argc, char *argv[], char *infile, char *define,
137 int extend, char *outfile, int nomain);
138 static void l_output(char *infile, char *define, int extend, char *outfile);
139 static void do_registers(int argc, char *argv[]);
140 static int parseargs(int argc, char *argv[], struct commandline *cmd);
141 static void C_output(char *infile, char *define, int extend, char *outfile,
143 static void S_output(char *infile, char *define, int extend, char *outfile,
145 static char *uppercase(char *str);
148 main(int argc, char *argv[])
150 struct commandline cmd;
154 /* initialize CPP with the correct pre-processor on NT */
155 ep = getenv("RXGEN_CPPCMD");
159 strcpy(CPP, "cl /EP /C /nologo");
163 * The following signal action for AIX is necessary so that in case of a
164 * crash (i.e. core is generated) we can include the user's data section
165 * in the core dump. Unfortunately, by default, only a partial core is
166 * generated which, in many cases, isn't too useful.
168 struct sigaction nsa;
170 sigemptyset(&nsa.sa_mask);
171 nsa.sa_handler = SIG_DFL;
172 nsa.sa_flags = SA_FULLDUMP;
173 sigaction(SIGSEGV, &nsa, NULL);
176 if (!parseargs(argc, argv, &cmd)) {
177 f_print(stderr, "usage: %s infile\n", cmdname);
179 " %s [-c | -h | -l | -m | -C | -S | -r | -k | -R | -p | -d | -z | -u] [-Pprefix] [-Idir] [-o outfile] [infile]\n",
181 f_print(stderr, " %s [-s udp|tcp]* [-o outfile] [infile]\n",
185 OutFileFlag = cmd.outfile;
187 strcpy(OutFile, cmd.outfile);
190 c_output(cmd.infile, "-DRPC_XDR", !EXTEND, cmd.outfile, 0);
191 } else if (cmd.hflag) {
192 h_output(cmd.infile, "-DRPC_HDR", !EXTEND, cmd.outfile, 0);
193 } else if (cmd.lflag) {
194 l_output(cmd.infile, "-DRPC_CLNT", !EXTEND, cmd.outfile);
195 } else if (cmd.sflag || cmd.mflag) {
196 s_output(argc, argv, cmd.infile, "-DRPC_SVC", !EXTEND, cmd.outfile,
198 } else if (cmd.Cflag) {
200 C_output(cmd.infile, "-DRPC_CLIENT", !EXTEND, cmd.outfile, 1);
201 } else if (cmd.Sflag) {
203 S_output(cmd.infile, "-DRPC_SERVER", !EXTEND, cmd.outfile, 1);
205 if (OutFileFlag && (strrchr(OutFile, '.') == NULL))
206 strcat(OutFile, ".");
208 C_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_CLIENT",
211 S_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_SERVER",
216 c_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_XDR",
217 EXTEND, ".xdr.c", 0);
219 h_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_HDR",
222 C_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_CLIENT",
225 S_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_SERVER",
234 * add extension to filename
237 extendfile(char *file, char *ext)
243 res = alloc(strlen(file) + strlen(ext) + 1);
247 p = (char *)strrchr(file, '.');
249 p = file + strlen(file);
251 sname = (char *)strrchr(file, '/');
257 strcpy(res + (p - sname), ext);
262 * Open output file with given extension
265 open_output(char *infile, char *outfile)
267 if (outfile == NULL) {
271 if (infile != NULL && streq(outfile, infile)) {
272 f_print(stderr, "%s: output would overwrite %s\n", cmdname, infile);
275 fout = fopen(outfile, "w");
277 f_print(stderr, "%s: unable to open ", cmdname);
281 record_open(outfile);
285 * Open input file with given define for C-preprocessor
288 open_input(char *infile, char *define)
290 char cpp_cmdline[MAXCMDLINE];
294 infilename = (infile == NULL) ? "<stdin>" : infile;
295 strcpy(cpp_cmdline, CPP);
296 strcat(cpp_cmdline, " ");
297 strcat(cpp_cmdline, CPPFLAGS);
298 strcat(cpp_cmdline, " ");
299 strcat(cpp_cmdline, define);
303 i < (sizeof(XTRA_CPPFLAGS) / sizeof(XTRA_CPPFLAGS[0])) - 1;
305 strcat(cpp_cmdline, " ");
306 strcat(cpp_cmdline, XTRA_CPPFLAGS[i]);
309 for (i = 0; i < nincludes; i++) {
310 strcat(cpp_cmdline, " ");
311 strcat(cpp_cmdline, IncludeDir[i]);
314 strcat(cpp_cmdline, " ");
315 strcat(cpp_cmdline, infile);
317 fin = popen(cpp_cmdline, "r");
322 if (infile == NULL) {
326 fin = fopen(infile, "r");
329 f_print(stderr, "%s: ", cmdname);
336 * Compile into an XDR routine output file
339 c_output(char *infile, char *define, int extend, char *outfile, int append)
346 char *currfile = (OutFileFlag ? OutFile : infile);
349 open_input(infile, define);
351 memset(fullname, 0, sizeof(fullname));
353 strcpy(fullname, prefix);
354 strcat(fullname, infile);
356 strcpy(fullname, infile);
357 outfilename = extend ? extendfile(fullname, outfile) : outfile;
358 open_output(infile, outfilename);
359 f_print(fout, "/* Machine generated file -- Do NOT edit */\n\n");
362 f_print(fout, "#include \"afs/param.h\"\n");
364 f_print(fout, "#include <afs/param.h>\n");
366 f_print(fout, "#ifdef AFS_NT40_ENV\n");
367 f_print(fout, "#define AFS_RXGEN_EXPORT __declspec(dllexport)\n");
368 f_print(fout, "#endif /* AFS_NT40_ENV */\n");
370 if (currfile && (include = extendfile(currfile, ".h"))) {
372 f_print(fout, "#include \"%s\"\n\n", include);
374 f_print(fout, "#include \"%s\"\n\n", include);
377 /* In case we can't include the interface's own header file... */
379 f_print(fout, "#include \"h/types.h\"\n");
380 f_print(fout, "#include \"h/socket.h\"\n");
381 f_print(fout, "#include \"h/file.h\"\n");
382 f_print(fout, "#include \"h/stat.h\"\n");
383 f_print(fout, "#include \"netinet/in.h\"\n");
384 f_print(fout, "#include \"h/time.h\"\n");
385 f_print(fout, "#include \"rx/xdr.h\"\n");
386 f_print(fout, "#include \"rxgen_consts.h\"\n");
388 f_print(fout, "#include <rx/xdr.h>\n");
393 while ((def = get_definition())) {
395 if ((!IsRxgenDefinition(def)) && def->def_kind != DEF_CUSTOMIZED)
401 * Print out array containing list of all functions in the interface
406 for (j = 0; j <= PackageIndex; j++) {
407 f_print(fout, "AFS_RXGEN_EXPORT\n");
408 f_print(fout, "const char *%sfunction_names[] = {\n",
411 for (i = 0; i < no_of_stat_funcs_header[j]; i++) {
413 f_print(fout, "\t\"%s\"", function_list[j][i]);
415 f_print(fout, ",\n\t\"%s\"", function_list[j][i]);
419 f_print(fout, "\n};\n");
423 if (extend && tell == ftell(fout)) {
424 (void)unlink(outfilename);
430 * Compile into an XDR header file
433 h_output(char *infile, char *define, int extend, char *outfile, int append)
438 char fullname[1024], *p;
440 open_input(infile, define);
442 memset(fullname, 0, sizeof(fullname));
444 strcpy(fullname, prefix);
445 strcat(fullname, infile);
447 strcpy(fullname, infile);
448 outfilename = extend ? extendfile(fullname, outfile) : outfile;
449 open_output(infile, outfilename);
450 strcpy(fullname, outfilename);
451 if ((p = strchr(fullname, '.')))
453 f_print(fout, "/* Machine generated file -- Do NOT edit */\n\n");
454 f_print(fout, "#ifndef _RXGEN_%s_\n", uppercase(fullname));
455 f_print(fout, "#define _RXGEN_%s_\n\n", uppercase(fullname));
456 f_print(fout, "#ifdef KERNEL\n");
458 "/* 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");
459 f_print(fout, "#include <afsconfig.h>\n");
460 f_print(fout, "#include \"afs/param.h\"\n");
461 f_print(fout, "#ifdef UKERNEL\n");
462 f_print(fout, "#include \"afs/sysincludes.h\"\n");
463 f_print(fout, "#include \"rx/xdr.h\"\n");
464 f_print(fout, "#include \"rx/rx.h\"\n");
466 f_print(fout, "#include \"rx/rx_globals.h\"\n");
469 f_print(fout, "#include <ubik.h>\n");
470 f_print(fout, "#else /* UKERNEL */\n");
471 f_print(fout, "#include \"h/types.h\"\n");
472 f_print(fout, "#ifndef SOCK_DGRAM /* XXXXX */\n");
473 f_print(fout, "#include \"h/socket.h\"\n");
474 f_print(fout, "#endif\n");
475 f_print(fout, "struct ubik_client;\n");
476 f_print(fout, "#ifndef DTYPE_SOCKET /* XXXXX */\n");
477 f_print(fout, "#ifndef AFS_LINUX22_ENV\n");
478 f_print(fout, "#include \"h/file.h\"\n");
479 f_print(fout, "#endif\n");
480 f_print(fout, "#endif\n");
481 f_print(fout, "#ifndef S_IFMT /* XXXXX */\n");
482 f_print(fout, "#include \"h/stat.h\"\n");
483 f_print(fout, "#endif\n");
484 f_print(fout, "#if defined (AFS_OBSD_ENV) && !defined (MLEN)\n");
485 f_print(fout, "#include \"sys/mbuf.h\"\n");
486 f_print(fout, "#endif\n");
487 f_print(fout, "#ifndef IPPROTO_UDP /* XXXXX */\n");
488 f_print(fout, "#include \"netinet/in.h\"\n");
489 f_print(fout, "#endif\n");
490 f_print(fout, "#ifndef DST_USA /* XXXXX */\n");
491 f_print(fout, "#include \"h/time.h\"\n");
492 f_print(fout, "#endif\n");
493 f_print(fout, "#ifndef AFS_LINUX22_ENV\n");
494 f_print(fout, "#include \"rpc/types.h\"\n");
495 f_print(fout, "#endif /* AFS_LINUX22_ENV */\n");
496 f_print(fout, "#ifndef XDR_GETLONG /* XXXXX */\n");
497 f_print(fout, "#ifdef AFS_LINUX22_ENV\n");
498 f_print(fout, "#ifndef quad_t\n");
499 f_print(fout, "#define quad_t __quad_t\n");
500 f_print(fout, "#define u_quad_t __u_quad_t\n");
501 f_print(fout, "#endif\n");
502 f_print(fout, "#endif\n");
503 f_print(fout, "#include \"rx/xdr.h\"\n");
504 f_print(fout, "#endif /* XDR_GETLONG */\n");
505 f_print(fout, "#endif /* UKERNEL */\n");
506 f_print(fout, "#include \"rxgen_consts.h\"\n");
507 f_print(fout, "#include \"afs_osi.h\"\n");
508 f_print(fout, "#include \"rx/rx.h\"\n");
510 f_print(fout, "#include \"rx/rx_globals.h\"\n");
512 f_print(fout, "#else /* KERNEL */\n");
513 f_print(fout, "#include <afs/param.h>\n");
514 f_print(fout, "#include <afs/stds.h>\n");
515 f_print(fout, "#include <sys/types.h>\n");
516 f_print(fout, "#include <rx/xdr.h>\n");
517 f_print(fout, "#include <rx/rx.h>\n");
519 f_print(fout, "#include <rx/rx_globals.h>\n");
521 f_print(fout, "#include <afs/rxgen_consts.h>\n");
523 f_print(fout, "#include <ubik.h>\n");
524 f_print(fout, "#endif /* KERNEL */\n\n");
525 f_print(fout, "#ifdef AFS_NT40_ENV\n");
526 f_print(fout, "#ifndef AFS_RXGEN_EXPORT\n");
527 f_print(fout, "#define AFS_RXGEN_EXPORT __declspec(dllimport)\n");
528 f_print(fout, "#endif /* AFS_RXGEN_EXPORT */\n");
529 f_print(fout, "#else /* AFS_NT40_ENV */\n");
530 f_print(fout, "#define AFS_RXGEN_EXPORT\n");
531 f_print(fout, "#endif /* AFS_NT40_ENV */\n\n");
533 while ((def = get_definition())) {
538 f_print(fout, "#endif /* _RXGEN_%s_ */\n", uppercase(fullname));
539 if (extend && tell == ftell(fout)) {
540 (void)unlink(outfilename);
545 * Compile into an RPC service
548 s_output(int argc, char *argv[], char *infile, char *define, int extend,
549 char *outfile, int nomain)
556 open_input(infile, define);
557 outfilename = extend ? extendfile(infile, outfile) : outfile;
558 open_output(infile, outfilename);
559 f_print(fout, "#include <stdio.h>\n");
560 f_print(fout, "#include <rpc/rpc.h>\n");
561 if (infile && (include = extendfile(infile, ".h"))) {
562 f_print(fout, "#include \"%s\"\n", include);
566 while ((def = get_definition())) {
567 foundprogram |= (def->def_kind == DEF_PROGRAM);
569 if (extend && !foundprogram) {
570 (void)unlink(outfilename);
574 write_programs((char *)NULL);
577 do_registers(argc, argv);
579 write_programs("static");
584 l_output(char *infile, char *define, int extend, char *outfile)
591 open_input(infile, define);
592 outfilename = extend ? extendfile(infile, outfile) : outfile;
593 open_output(infile, outfilename);
594 f_print(fout, "#include <rpc/rpc.h>\n");
595 f_print(fout, "#include <sys/time.h>\n");
596 if (infile && (include = extendfile(infile, ".h"))) {
597 f_print(fout, "#include \"%s\"\n", include);
601 while ((def = get_definition())) {
602 foundprogram |= (def->def_kind == DEF_PROGRAM);
604 if (extend && !foundprogram) {
605 (void)unlink(outfilename);
612 * Perform registrations for service output
615 do_registers(int argc, char *argv[])
619 for (i = 1; i < argc; i++) {
620 if (streq(argv[i], "-s")) {
621 write_register(argv[i + 1]);
629 C_output(char *infile, char *define, int extend, char *outfile, int append)
635 char *currfile = (OutFileFlag ? OutFile : infile);
638 open_input(infile, define);
639 memset(fullname, 0, sizeof(fullname));
641 strcpy(fullname, prefix);
642 strcat(fullname, infile);
644 strcpy(fullname, infile);
645 outfilename = extend ? extendfile(fullname, outfile) : outfile;
646 open_output(infile, outfilename);
647 f_print(fout, "/* Machine generated file -- Do NOT edit */\n\n");
648 if (currfile && (include = extendfile(currfile, ".h"))) {
650 f_print(fout, "#include \"%s\"\n\n", include);
652 f_print(fout, "#include \"%s\"\n\n", include);
657 f_print(fout, "#include \"h/types.h\"\n");
658 f_print(fout, "#include \"h/socket.h\"\n");
659 f_print(fout, "#include \"h/file.h\"\n");
660 f_print(fout, "#include \"h/stat.h\"\n");
661 f_print(fout, "#include \"netinet/in.h\"\n");
662 f_print(fout, "#include \"h/time.h\"\n");
663 f_print(fout, "#include \"rpc/types.h\"\n");
664 f_print(fout, "#include \"rx/xdr.h\"\n");
665 f_print(fout, "#include \"rxgen_consts.h\"\n");
666 f_print(fout, "#include \"afs/afs_osi.h\"\n");
667 f_print(fout, "#include \"rx/rx.h\"\n");
669 f_print(fout, "#include \"rx/rx_globals.h\"\n");
672 f_print(fout, "#include <sys/types.h>\n");
673 f_print(fout, "#include <rx/xdr.h>\n");
674 f_print(fout, "#include <rx/rx.h>\n");
676 f_print(fout, "#include <rx/rx_globals.h>\n");
678 f_print(fout, "#include <afs/rxgen_consts.h>\n");
683 while (get_definition())
685 if (extend && tell == ftell(fout)) {
686 (void)unlink(outfilename);
693 S_output(char *infile, char *define, int extend, char *outfile, int append)
700 char *currfile = (OutFileFlag ? OutFile : infile);
703 open_input(infile, define);
704 memset(fullname, 0, sizeof(fullname));
706 strcpy(fullname, prefix);
707 strcat(fullname, infile);
709 strcpy(fullname, infile);
710 outfilename = extend ? extendfile(fullname, outfile) : outfile;
711 open_output(infile, outfilename);
712 f_print(fout, "/* Machine generated file -- Do NOT edit */\n\n");
713 if (currfile && (include = extendfile(currfile, ".h"))) {
715 f_print(fout, "#include \"%s\"\n", include);
717 f_print(fout, "#include \"%s\"\n\n", include);
722 f_print(fout, "#include \"h/types.h\"\n");
723 f_print(fout, "#include \"h/socket.h\"\n");
724 f_print(fout, "#include \"h/file.h\"\n");
725 f_print(fout, "#include \"h/stat.h\"\n");
726 f_print(fout, "#include \"netinet/in.h\"\n");
727 f_print(fout, "#include \"h/time.h\"\n");
728 f_print(fout, "#include \"rpc/types.h\"\n");
729 f_print(fout, "#include \"rx/xdr.h\"\n");
730 f_print(fout, "#include \"rxgen_consts.h\"\n");
731 f_print(fout, "#include \"afs/afs_osi.h\"\n");
732 f_print(fout, "#include \"rx/rx.h\"\n");
734 f_print(fout, "#include \"rx/rx_globals.h\"\n");
737 f_print(fout, "#include <sys/types.h>\n");
738 f_print(fout, "#include <rx/xdr.h>\n");
739 f_print(fout, "#include <rx/rx.h>\n");
741 f_print(fout, "#include <rx/rx_globals.h>\n");
743 f_print(fout, "#include <afs/rxgen_consts.h>\n");
749 while ((def = get_definition())) {
754 er_Proc_CodeGeneration();
756 if (extend && tell == ftell(fout)) {
757 (void)unlink(outfilename);
765 static char max_size[100];
767 int len = (int)strlen(str);
769 for (pnt = max_size; len > 0; len--, str++) {
770 *pnt++ = (islower(*str) ? toupper(*str) : *str);
777 * Parse command line arguments
780 parseargs(int argc, char *argv[], struct commandline *cmd)
785 char flag[(1 << (8 * sizeof(char)))];
789 cmd->infile = cmd->outfile = NULL;
793 memset(flag, 0, sizeof(flag));
795 for (i = 1; i < argc; i++) {
796 if (argv[i][0] != '-') {
800 cmd->infile = argv[i];
802 for (j = 1; argv[i][j] != 0; j++) {
827 if (argv[i][j - 1] != '-' || argv[i][j + 1] != 0) {
835 if (!streq(argv[i], "udp") && !streq(argv[i], "tcp")) {
838 } else if (c == 'o') {
842 cmd->outfile = argv[i];
846 if (argv[i][j - 1] != '-')
848 prefix = &argv[i][j + 1];
851 if (argv[i][j - 1] != '-')
853 IncludeDir[nincludes++] = &argv[i][j - 1];
863 cmd->cflag = cflag = flag['c'];
864 cmd->hflag = hflag = flag['h'];
865 cmd->sflag = flag['s'];
866 cmd->lflag = flag['l'];
867 cmd->mflag = flag['m'];
868 cmd->xflag = xflag = flag['x'];
869 cmd->yflag = yflag = flag['y'];
870 cmd->Cflag = Cflag = flag['C'];
871 cmd->Sflag = Sflag = flag['S'];
872 cmd->rflag = flag['r'];
873 cmd->uflag = uflag = flag['u'];
874 cmd->kflag = kflag = flag['k'];
875 cmd->pflag = flag['p'];
876 cmd->dflag = debug = flag['d'];
881 cmd->cflag + cmd->hflag + cmd->sflag + cmd->lflag + cmd->mflag +
882 cmd->Cflag + cmd->Sflag + cmd->rflag;
884 if (cmd->outfile != NULL || cmd->infile == NULL) {
887 } else if (nflags > 1) {