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 */
82 #define MAXCPPARGS 256 /* maximum number of arguments to cpp */
83 #define MAXCMDLINE 1024 /* MAX chars on a single cmd line */
86 static char *IncludeDir[MAXCPPARGS];
88 char *OutFileFlag = "";
90 char Sflag = 0, Cflag = 0, hflag = 0, cflag = 0, kflag = 0, uflag = 0;
91 char ansic_flag = 0; /* If set, build ANSI C style prototypes */
92 char zflag = 0; /* If set, abort server stub if rpc call returns non-zero */
93 char xflag = 0; /* if set, add stats code to stubs */
94 char yflag = 0; /* if set, only emit function name arrays to xdr file */
98 static char CPP[] = PATH_CPP;
100 static char CPP[] = "/lib/cpp";
102 static char CPPFLAGS[] = "-C";
106 * Running "cpp" directly on DEC OSF/1 does not define anything; the "cc"
107 * driver is responsible. To compensate (and allow for other definitions
108 * which should always be passed to "cpp"), place definitions which whould
109 * always be passed to "rxgen" in this table.
111 static char *XTRA_CPPFLAGS[] = {
122 #include "AFS_component_version_number.c"
124 /* static prototypes */
125 static char *extendfile(char *file, char *ext);
126 static void open_output(char *infile, char *outfile);
127 static void open_input(char *infile, char *define);
128 static void c_output(char *infile, char *define, int extend, char *outfile,
130 static void h_output(char *infile, char *define, int extend, char *outfile,
132 static void s_output(int argc, char *argv[], char *infile, char *define,
133 int extend, char *outfile, int nomain);
134 static void l_output(char *infile, char *define, int extend, char *outfile);
135 static void do_registers(int argc, char *argv[]);
136 static int parseargs(int argc, char *argv[], struct commandline *cmd);
137 static void C_output(char *infile, char *define, int extend, char *outfile,
139 static void S_output(char *infile, char *define, int extend, char *outfile,
141 static char *uppercase(char *str);
144 main(int argc, char *argv[])
146 struct commandline cmd;
150 /* initialize CPP with the correct pre-processor on NT */
151 ep = getenv("RXGEN_CPPCMD");
155 strcpy(CPP, "cl /EP /C /nologo");
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())) {
534 f_print(fout, "#endif /* _RXGEN_%s_ */\n", uppercase(fullname));
535 if (extend && tell == ftell(fout)) {
536 (void)unlink(outfilename);
541 * Compile into an RPC service
544 s_output(int argc, char *argv[], char *infile, char *define, int extend,
545 char *outfile, int nomain)
552 open_input(infile, define);
553 outfilename = extend ? extendfile(infile, outfile) : outfile;
554 open_output(infile, outfilename);
555 f_print(fout, "#include <stdio.h>\n");
556 f_print(fout, "#include <rpc/rpc.h>\n");
557 if (infile && (include = extendfile(infile, ".h"))) {
558 f_print(fout, "#include \"%s\"\n", include);
562 while ((def = get_definition())) {
563 foundprogram |= (def->def_kind == DEF_PROGRAM);
565 if (extend && !foundprogram) {
566 (void)unlink(outfilename);
570 write_programs((char *)NULL);
573 do_registers(argc, argv);
575 write_programs("static");
580 l_output(char *infile, char *define, int extend, char *outfile)
587 open_input(infile, define);
588 outfilename = extend ? extendfile(infile, outfile) : outfile;
589 open_output(infile, outfilename);
590 f_print(fout, "#include <rpc/rpc.h>\n");
591 f_print(fout, "#include <sys/time.h>\n");
592 if (infile && (include = extendfile(infile, ".h"))) {
593 f_print(fout, "#include \"%s\"\n", include);
597 while ((def = get_definition())) {
598 foundprogram |= (def->def_kind == DEF_PROGRAM);
600 if (extend && !foundprogram) {
601 (void)unlink(outfilename);
608 * Perform registrations for service output
611 do_registers(int argc, char *argv[])
615 for (i = 1; i < argc; i++) {
616 if (streq(argv[i], "-s")) {
617 write_register(argv[i + 1]);
625 C_output(char *infile, char *define, int extend, char *outfile, int append)
631 char *currfile = (OutFileFlag ? OutFile : infile);
634 open_input(infile, define);
635 memset(fullname, 0, sizeof(fullname));
637 strcpy(fullname, prefix);
638 strcat(fullname, infile);
640 strcpy(fullname, infile);
641 outfilename = extend ? extendfile(fullname, outfile) : outfile;
642 open_output(infile, outfilename);
643 f_print(fout, "/* Machine generated file -- Do NOT edit */\n\n");
644 if (currfile && (include = extendfile(currfile, ".h"))) {
646 f_print(fout, "#include \"%s\"\n\n", include);
648 f_print(fout, "#include \"%s\"\n\n", include);
653 f_print(fout, "#include \"h/types.h\"\n");
654 f_print(fout, "#include \"h/socket.h\"\n");
655 f_print(fout, "#include \"h/file.h\"\n");
656 f_print(fout, "#include \"h/stat.h\"\n");
657 f_print(fout, "#include \"netinet/in.h\"\n");
658 f_print(fout, "#include \"h/time.h\"\n");
659 f_print(fout, "#include \"rpc/types.h\"\n");
660 f_print(fout, "#include \"rx/xdr.h\"\n");
661 f_print(fout, "#include \"rxgen_consts.h\"\n");
662 f_print(fout, "#include \"afs/afs_osi.h\"\n");
663 f_print(fout, "#include \"rx/rx.h\"\n");
665 f_print(fout, "#include \"rx/rx_globals.h\"\n");
668 f_print(fout, "#include <sys/types.h>\n");
669 f_print(fout, "#include <rx/xdr.h>\n");
670 f_print(fout, "#include <rx/rx.h>\n");
672 f_print(fout, "#include <rx/rx_globals.h>\n");
674 f_print(fout, "#include <afs/rxgen_consts.h>\n");
679 while (get_definition())
681 if (extend && tell == ftell(fout)) {
682 (void)unlink(outfilename);
689 S_output(char *infile, char *define, int extend, char *outfile, int append)
696 char *currfile = (OutFileFlag ? OutFile : infile);
699 open_input(infile, define);
700 memset(fullname, 0, sizeof(fullname));
702 strcpy(fullname, prefix);
703 strcat(fullname, infile);
705 strcpy(fullname, infile);
706 outfilename = extend ? extendfile(fullname, outfile) : outfile;
707 open_output(infile, outfilename);
708 f_print(fout, "/* Machine generated file -- Do NOT edit */\n\n");
709 if (currfile && (include = extendfile(currfile, ".h"))) {
711 f_print(fout, "#include \"%s\"\n", include);
713 f_print(fout, "#include \"%s\"\n\n", include);
718 f_print(fout, "#include \"h/types.h\"\n");
719 f_print(fout, "#include \"h/socket.h\"\n");
720 f_print(fout, "#include \"h/file.h\"\n");
721 f_print(fout, "#include \"h/stat.h\"\n");
722 f_print(fout, "#include \"netinet/in.h\"\n");
723 f_print(fout, "#include \"h/time.h\"\n");
724 f_print(fout, "#include \"rpc/types.h\"\n");
725 f_print(fout, "#include \"rx/xdr.h\"\n");
726 f_print(fout, "#include \"rxgen_consts.h\"\n");
727 f_print(fout, "#include \"afs/afs_osi.h\"\n");
728 f_print(fout, "#include \"rx/rx.h\"\n");
730 f_print(fout, "#include \"rx/rx_globals.h\"\n");
733 f_print(fout, "#include <sys/types.h>\n");
734 f_print(fout, "#include <rx/xdr.h>\n");
735 f_print(fout, "#include <rx/rx.h>\n");
737 f_print(fout, "#include <rx/rx_globals.h>\n");
739 f_print(fout, "#include <afs/rxgen_consts.h>\n");
745 while ((def = get_definition())) {
750 er_Proc_CodeGeneration();
752 if (extend && tell == ftell(fout)) {
753 (void)unlink(outfilename);
761 static char max_size[100];
763 int len = (int)strlen(str);
765 for (pnt = max_size; len > 0; len--, str++) {
766 *pnt++ = (islower(*str) ? toupper(*str) : *str);
773 * Parse command line arguments
776 parseargs(int argc, char *argv[], struct commandline *cmd)
781 char flag[(1 << (8 * sizeof(char)))];
785 cmd->infile = cmd->outfile = NULL;
789 memset(flag, 0, sizeof(flag));
791 for (i = 1; i < argc; i++) {
792 if (argv[i][0] != '-') {
796 cmd->infile = argv[i];
798 for (j = 1; argv[i][j] != 0; j++) {
824 if (argv[i][j - 1] != '-' || argv[i][j + 1] != 0) {
832 if (!streq(argv[i], "udp") && !streq(argv[i], "tcp")) {
835 } else if (c == 'o') {
839 cmd->outfile = argv[i];
843 if (argv[i][j - 1] != '-')
845 prefix = &argv[i][j + 1];
848 if (argv[i][j - 1] != '-')
850 IncludeDir[nincludes++] = &argv[i][j - 1];
860 cmd->ansic_flag = ansic_flag = flag['A'];
861 cmd->cflag = cflag = flag['c'];
862 cmd->hflag = hflag = flag['h'];
863 cmd->sflag = flag['s'];
864 cmd->lflag = flag['l'];
865 cmd->mflag = flag['m'];
866 cmd->xflag = xflag = flag['x'];
867 cmd->yflag = yflag = flag['y'];
868 cmd->Cflag = Cflag = flag['C'];
869 cmd->Sflag = Sflag = flag['S'];
870 cmd->rflag = flag['r'];
871 cmd->uflag = uflag = flag['u'];
872 cmd->kflag = kflag = flag['k'];
873 cmd->pflag = flag['p'];
874 cmd->dflag = debug = flag['d'];
879 cmd->cflag + cmd->hflag + cmd->sflag + cmd->lflag + cmd->mflag +
880 cmd->Cflag + cmd->Sflag + cmd->rflag;
882 if (cmd->outfile != NULL || cmd->infile == NULL) {
885 } else if (nflags > 1) {