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 */
86 #define MAXCPPARGS 256 /* maximum number of arguments to cpp */
87 #define MAXCMDLINE 1024 /* MAX chars on a single cmd line */
90 static char *IncludeDir[MAXCPPARGS];
92 char *OutFileFlag = "";
94 char Sflag = 0, Cflag = 0, hflag = 0, cflag = 0, kflag = 0;
95 char zflag = 0; /* If set, abort server stub if rpc call returns non-zero */
96 char xflag = 0; /* if set, add stats code to stubs */
97 char yflag = 0; /* if set, only emit function name arrays to xdr file */
101 static char CPP[] = "/usr/ccs/lib/cpp";
102 #elif defined(AFS_XBSD_ENV) || defined(AFS_DARWIN60_ENV)
103 static char CPP[] = "/usr/bin/cpp";
104 #elif defined(AFS_NT40_ENV)
105 static char CPP[MAXCMDLINE];
106 #elif defined(AFS_DARWIN_ENV)
107 static char CPP[] = "cc -E";
109 static char CPP[] = "/lib/cpp";
111 static char CPPFLAGS[] = "-C";
115 * Running "cpp" directly on DEC OSF/1 does not define anything; the "cc"
116 * driver is responsible. To compensate (and allow for other definitions
117 * which should always be passed to "cpp"), place definitions which whould
118 * always be passed to "rxgen" in this table.
120 static char *XTRA_CPPFLAGS[] = {
131 #include "AFS_component_version_number.c"
133 /* static prototypes */
134 static char *extendfile(char *file, char *ext);
135 static void open_output(char *infile, char *outfile);
136 static void open_input(char *infile, char *define);
137 static void c_output(char *infile, char *define, int extend, char *outfile,
139 static void h_output(char *infile, char *define, int extend, char *outfile,
141 static void s_output(int argc, char *argv[], char *infile, char *define,
142 int extend, char *outfile, int nomain);
143 static void l_output(char *infile, char *define, int extend, char *outfile);
144 static void do_registers(int argc, char *argv[]);
145 static int parseargs(int argc, char *argv[], struct commandline *cmd);
146 static void C_output(char *infile, char *define, int extend, char *outfile,
148 static void S_output(char *infile, char *define, int extend, char *outfile,
150 static char *uppercase(char *str);
153 main(int argc, char *argv[])
155 struct commandline cmd;
159 /* initialize CPP with the correct pre-processor on NT */
160 ep = getenv("RXGEN_CPPCMD");
164 strcpy(CPP, "cl /EP /C /nologo");
168 * The following signal action for AIX is necessary so that in case of a
169 * crash (i.e. core is generated) we can include the user's data section
170 * in the core dump. Unfortunately, by default, only a partial core is
171 * generated which, in many cases, isn't too useful.
173 struct sigaction nsa;
175 sigemptyset(&nsa.sa_mask);
176 nsa.sa_handler = SIG_DFL;
177 nsa.sa_flags = SA_FULLDUMP;
178 sigaction(SIGSEGV, &nsa, NULL);
181 if (!parseargs(argc, argv, &cmd)) {
182 f_print(stderr, "usage: %s infile\n", cmdname);
184 " %s [-c | -h | -l | -m | -C | -S | -r | -k | -R | -p | -d | -z] [-Pprefix] [-Idir] [-o outfile] [infile]\n",
186 f_print(stderr, " %s [-s udp|tcp]* [-o outfile] [infile]\n",
190 OutFileFlag = cmd.outfile;
192 strcpy(OutFile, cmd.outfile);
195 c_output(cmd.infile, "-DRPC_XDR", !EXTEND, cmd.outfile, 0);
196 } else if (cmd.hflag) {
197 h_output(cmd.infile, "-DRPC_HDR", !EXTEND, cmd.outfile, 0);
198 } else if (cmd.lflag) {
199 l_output(cmd.infile, "-DRPC_CLNT", !EXTEND, cmd.outfile);
200 } else if (cmd.sflag || cmd.mflag) {
201 s_output(argc, argv, cmd.infile, "-DRPC_SVC", !EXTEND, cmd.outfile,
203 } else if (cmd.Cflag) {
205 C_output(cmd.infile, "-DRPC_CLIENT", !EXTEND, cmd.outfile, 1);
206 } else if (cmd.Sflag) {
208 S_output(cmd.infile, "-DRPC_SERVER", !EXTEND, cmd.outfile, 1);
210 if (OutFileFlag && (strrchr(OutFile, '.') == NULL))
211 strcat(OutFile, ".");
213 C_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_CLIENT",
216 S_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_SERVER",
221 c_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_XDR",
222 EXTEND, ".xdr.c", 0);
224 h_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_HDR",
227 C_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_CLIENT",
230 S_output((OutFileFlag ? OutFile : cmd.infile), "-DRPC_SERVER",
239 * add extension to filename
242 extendfile(char *file, char *ext)
248 res = alloc(strlen(file) + strlen(ext) + 1);
252 p = (char *)strrchr(file, '.');
254 p = file + strlen(file);
256 sname = (char *)strrchr(file, '/');
262 strcpy(res + (p - sname), ext);
267 * Open output file with given extension
270 open_output(char *infile, char *outfile)
272 if (outfile == NULL) {
276 if (infile != NULL && streq(outfile, infile)) {
277 f_print(stderr, "%s: output would overwrite %s\n", cmdname, infile);
280 fout = fopen(outfile, "w");
282 f_print(stderr, "%s: unable to open ", cmdname);
286 record_open(outfile);
290 * Open input file with given define for C-preprocessor
293 open_input(char *infile, char *define)
295 char cpp_cmdline[MAXCMDLINE];
299 infilename = (infile == NULL) ? "<stdin>" : infile;
300 strcpy(cpp_cmdline, CPP);
301 strcat(cpp_cmdline, " ");
302 strcat(cpp_cmdline, CPPFLAGS);
303 strcat(cpp_cmdline, " ");
304 strcat(cpp_cmdline, define);
308 i < (sizeof(XTRA_CPPFLAGS) / sizeof(XTRA_CPPFLAGS[0])) - 1;
310 strcat(cpp_cmdline, " ");
311 strcat(cpp_cmdline, XTRA_CPPFLAGS[i]);
314 for (i = 0; i < nincludes; i++) {
315 strcat(cpp_cmdline, " ");
316 strcat(cpp_cmdline, IncludeDir[i]);
319 strcat(cpp_cmdline, " ");
320 strcat(cpp_cmdline, infile);
322 fin = popen(cpp_cmdline, "r");
327 if (infile == NULL) {
331 fin = fopen(infile, "r");
334 f_print(stderr, "%s: ", cmdname);
341 * Compile into an XDR routine output file
344 c_output(char *infile, char *define, int extend, char *outfile, int append)
351 char *currfile = (OutFileFlag ? OutFile : infile);
354 open_input(infile, define);
356 memset(fullname, 0, sizeof(fullname));
358 strcpy(fullname, prefix);
359 strcat(fullname, infile);
361 strcpy(fullname, infile);
362 outfilename = extend ? extendfile(fullname, outfile) : outfile;
363 open_output(infile, outfilename);
364 f_print(fout, "/* Machine generated file -- Do NOT edit */\n\n");
367 f_print(fout, "#include \"afs/param.h\"\n");
369 f_print(fout, "#include <afs/param.h>\n");
371 f_print(fout, "#ifdef AFS_NT40_ENV\n");
372 f_print(fout, "#define AFS_RXGEN_EXPORT __declspec(dllexport)\n");
373 f_print(fout, "#endif /* AFS_NT40_ENV */\n");
375 if (currfile && (include = extendfile(currfile, ".h"))) {
377 f_print(fout, "#include \"%s\"\n\n", include);
379 f_print(fout, "#include \"%s\"\n\n", include);
382 /* In case we can't include the interface's own header file... */
384 f_print(fout, "#include \"h/types.h\"\n");
385 f_print(fout, "#include \"h/socket.h\"\n");
386 f_print(fout, "#include \"h/file.h\"\n");
387 f_print(fout, "#include \"h/stat.h\"\n");
388 f_print(fout, "#include \"netinet/in.h\"\n");
389 f_print(fout, "#include \"h/time.h\"\n");
390 f_print(fout, "#include \"rx/xdr.h\"\n");
391 f_print(fout, "#include \"rxgen_consts.h\"\n");
393 f_print(fout, "#include <rx/xdr.h>\n");
398 while ((def = get_definition())) {
400 if ((!IsRxgenDefinition(def)) && def->def_kind != DEF_CUSTOMIZED)
406 * Print out array containing list of all functions in the interface
411 for (j = 0; j <= PackageIndex; j++) {
412 f_print(fout, "AFS_RXGEN_EXPORT\n");
413 f_print(fout, "const char *%sfunction_names[] = {\n",
416 for (i = 0; i < no_of_stat_funcs_header[j]; i++) {
418 f_print(fout, "\t\"%s\"", function_list[j][i]);
420 f_print(fout, ",\n\t\"%s\"", function_list[j][i]);
424 f_print(fout, "\n};\n");
428 if (extend && tell == ftell(fout)) {
429 (void)unlink(outfilename);
435 * Compile into an XDR header file
438 h_output(char *infile, char *define, int extend, char *outfile, int append)
443 char fullname[1024], *p;
445 open_input(infile, define);
447 memset(fullname, 0, sizeof(fullname));
449 strcpy(fullname, prefix);
450 strcat(fullname, infile);
452 strcpy(fullname, infile);
453 outfilename = extend ? extendfile(fullname, outfile) : outfile;
454 open_output(infile, outfilename);
455 strcpy(fullname, outfilename);
456 if ((p = strchr(fullname, '.')))
458 f_print(fout, "/* Machine generated file -- Do NOT edit */\n\n");
459 f_print(fout, "#ifndef _RXGEN_%s_\n", uppercase(fullname));
460 f_print(fout, "#define _RXGEN_%s_\n\n", uppercase(fullname));
461 f_print(fout, "#ifdef KERNEL\n");
463 "/* 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");
464 f_print(fout, "#include <afsconfig.h>\n");
465 f_print(fout, "#include \"afs/param.h\"\n");
466 f_print(fout, "#ifdef UKERNEL\n");
467 f_print(fout, "#include \"afs/sysincludes.h\"\n");
468 f_print(fout, "#include \"rx/xdr.h\"\n");
469 f_print(fout, "#include \"rx/rx.h\"\n");
471 f_print(fout, "#include \"rx/rx_globals.h\"\n");
473 f_print(fout, "#else /* UKERNEL */\n");
474 f_print(fout, "#include \"h/types.h\"\n");
475 f_print(fout, "#ifndef SOCK_DGRAM /* XXXXX */\n");
476 f_print(fout, "#include \"h/socket.h\"\n");
477 f_print(fout, "#endif\n");
478 f_print(fout, "#ifndef DTYPE_SOCKET /* XXXXX */\n");
479 f_print(fout, "#ifdef AFS_DEC_ENV\n");
480 f_print(fout, "#include \"h/smp_lock.h\"\n");
481 f_print(fout, "#endif\n");
482 f_print(fout, "#ifndef AFS_LINUX22_ENV\n");
483 f_print(fout, "#include \"h/file.h\"\n");
484 f_print(fout, "#endif\n");
485 f_print(fout, "#endif\n");
486 f_print(fout, "#ifndef S_IFMT /* XXXXX */\n");
487 f_print(fout, "#include \"h/stat.h\"\n");
488 f_print(fout, "#endif\n");
489 f_print(fout, "#if defined (AFS_OBSD_ENV) && !defined (MLEN)\n");
490 f_print(fout, "#include \"sys/mbuf.h\"\n");
491 f_print(fout, "#endif\n");
492 f_print(fout, "#ifndef IPPROTO_UDP /* XXXXX */\n");
493 f_print(fout, "#include \"netinet/in.h\"\n");
494 f_print(fout, "#endif\n");
495 f_print(fout, "#ifndef DST_USA /* XXXXX */\n");
496 f_print(fout, "#include \"h/time.h\"\n");
497 f_print(fout, "#endif\n");
498 f_print(fout, "#ifndef AFS_LINUX22_ENV\n");
499 f_print(fout, "#include \"rpc/types.h\"\n");
500 f_print(fout, "#endif /* AFS_LINUX22_ENV */\n");
501 f_print(fout, "#ifndef XDR_GETLONG /* XXXXX */\n");
502 f_print(fout, "#ifdef AFS_LINUX22_ENV\n");
503 f_print(fout, "#ifndef quad_t\n");
504 f_print(fout, "#define quad_t __quad_t\n");
505 f_print(fout, "#define u_quad_t __u_quad_t\n");
506 f_print(fout, "#endif\n");
507 f_print(fout, "#endif\n");
508 f_print(fout, "#include \"rx/xdr.h\"\n");
509 f_print(fout, "#endif /* XDR_GETLONG */\n");
510 f_print(fout, "#endif /* UKERNEL */\n");
511 f_print(fout, "#include \"rxgen_consts.h\"\n");
512 f_print(fout, "#include \"afs_osi.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, "#else /* KERNEL */\n");
518 f_print(fout, "#include <afs/param.h>\n");
519 f_print(fout, "#include <afs/stds.h>\n");
520 f_print(fout, "#include <sys/types.h>\n");
521 f_print(fout, "#include <rx/xdr.h>\n");
522 f_print(fout, "#include <rx/rx.h>\n");
524 f_print(fout, "#include <rx/rx_globals.h>\n");
526 f_print(fout, "#include <afs/rxgen_consts.h>\n");
527 f_print(fout, "#endif /* KERNEL */\n\n");
528 f_print(fout, "#ifdef AFS_NT40_ENV\n");
529 f_print(fout, "#ifndef AFS_RXGEN_EXPORT\n");
530 f_print(fout, "#define AFS_RXGEN_EXPORT __declspec(dllimport)\n");
531 f_print(fout, "#endif /* AFS_RXGEN_EXPORT */\n");
532 f_print(fout, "#else /* AFS_NT40_ENV */\n");
533 f_print(fout, "#define AFS_RXGEN_EXPORT\n");
534 f_print(fout, "#endif /* AFS_NT40_ENV */\n\n");
536 while ((def = get_definition())) {
541 f_print(fout, "#endif /* _RXGEN_%s_ */\n", uppercase(fullname));
542 if (extend && tell == ftell(fout)) {
543 (void)unlink(outfilename);
548 * Compile into an RPC service
551 s_output(int argc, char *argv[], char *infile, char *define, int extend,
552 char *outfile, int nomain)
559 open_input(infile, define);
560 outfilename = extend ? extendfile(infile, outfile) : outfile;
561 open_output(infile, outfilename);
562 f_print(fout, "#include <stdio.h>\n");
563 f_print(fout, "#include <rpc/rpc.h>\n");
564 if (infile && (include = extendfile(infile, ".h"))) {
565 f_print(fout, "#include \"%s\"\n", include);
569 while ((def = get_definition())) {
570 foundprogram |= (def->def_kind == DEF_PROGRAM);
572 if (extend && !foundprogram) {
573 (void)unlink(outfilename);
577 write_programs((char *)NULL);
580 do_registers(argc, argv);
582 write_programs("static");
587 l_output(char *infile, char *define, int extend, char *outfile)
594 open_input(infile, define);
595 outfilename = extend ? extendfile(infile, outfile) : outfile;
596 open_output(infile, outfilename);
597 f_print(fout, "#include <rpc/rpc.h>\n");
598 f_print(fout, "#include <sys/time.h>\n");
599 if (infile && (include = extendfile(infile, ".h"))) {
600 f_print(fout, "#include \"%s\"\n", include);
604 while ((def = get_definition())) {
605 foundprogram |= (def->def_kind == DEF_PROGRAM);
607 if (extend && !foundprogram) {
608 (void)unlink(outfilename);
615 * Perform registrations for service output
618 do_registers(int argc, char *argv[])
622 for (i = 1; i < argc; i++) {
623 if (streq(argv[i], "-s")) {
624 write_register(argv[i + 1]);
632 C_output(char *infile, char *define, int extend, char *outfile, int append)
638 char *currfile = (OutFileFlag ? OutFile : infile);
641 open_input(infile, define);
642 memset(fullname, 0, sizeof(fullname));
644 strcpy(fullname, prefix);
645 strcat(fullname, infile);
647 strcpy(fullname, infile);
648 outfilename = extend ? extendfile(fullname, outfile) : outfile;
649 open_output(infile, outfilename);
650 f_print(fout, "/* Machine generated file -- Do NOT edit */\n\n");
651 if (currfile && (include = extendfile(currfile, ".h"))) {
653 f_print(fout, "#include \"%s\"\n\n", include);
655 f_print(fout, "#include \"%s\"\n\n", include);
660 f_print(fout, "#include \"h/types.h\"\n");
661 f_print(fout, "#include \"h/socket.h\"\n");
662 f_print(fout, "#include \"h/file.h\"\n");
663 f_print(fout, "#include \"h/stat.h\"\n");
664 f_print(fout, "#include \"netinet/in.h\"\n");
665 f_print(fout, "#include \"h/time.h\"\n");
666 f_print(fout, "#include \"rpc/types.h\"\n");
667 f_print(fout, "#include \"rx/xdr.h\"\n");
668 f_print(fout, "#include \"rxgen_consts.h\"\n");
669 f_print(fout, "#include \"afs/afs_osi.h\"\n");
670 f_print(fout, "#include \"rx/rx.h\"\n");
672 f_print(fout, "#include \"rx/rx_globals.h\"\n");
675 f_print(fout, "#include <sys/types.h>\n");
676 f_print(fout, "#include <rx/xdr.h>\n");
677 f_print(fout, "#include <rx/rx.h>\n");
679 f_print(fout, "#include <rx/rx_globals.h>\n");
681 f_print(fout, "#include <afs/rxgen_consts.h>\n");
686 while (get_definition())
688 if (extend && tell == ftell(fout)) {
689 (void)unlink(outfilename);
696 S_output(char *infile, char *define, int extend, char *outfile, int append)
703 char *currfile = (OutFileFlag ? OutFile : infile);
706 open_input(infile, define);
707 memset(fullname, 0, sizeof(fullname));
709 strcpy(fullname, prefix);
710 strcat(fullname, infile);
712 strcpy(fullname, infile);
713 outfilename = extend ? extendfile(fullname, outfile) : outfile;
714 open_output(infile, outfilename);
715 f_print(fout, "/* Machine generated file -- Do NOT edit */\n\n");
716 if (currfile && (include = extendfile(currfile, ".h"))) {
718 f_print(fout, "#include \"%s\"\n", include);
720 f_print(fout, "#include \"%s\"\n\n", include);
725 f_print(fout, "#include \"h/types.h\"\n");
726 f_print(fout, "#include \"h/socket.h\"\n");
727 f_print(fout, "#include \"h/file.h\"\n");
728 f_print(fout, "#include \"h/stat.h\"\n");
729 f_print(fout, "#include \"netinet/in.h\"\n");
730 f_print(fout, "#include \"h/time.h\"\n");
731 f_print(fout, "#include \"rpc/types.h\"\n");
732 f_print(fout, "#include \"rx/xdr.h\"\n");
733 f_print(fout, "#include \"rxgen_consts.h\"\n");
734 f_print(fout, "#include \"afs/afs_osi.h\"\n");
735 f_print(fout, "#include \"rx/rx.h\"\n");
737 f_print(fout, "#include \"rx/rx_globals.h\"\n");
740 f_print(fout, "#include <sys/types.h>\n");
741 f_print(fout, "#include <rx/xdr.h>\n");
742 f_print(fout, "#include <rx/rx.h>\n");
744 f_print(fout, "#include <rx/rx_globals.h>\n");
746 f_print(fout, "#include <afs/rxgen_consts.h>\n");
752 while ((def = get_definition())) {
757 er_Proc_CodeGeneration();
759 if (extend && tell == ftell(fout)) {
760 (void)unlink(outfilename);
768 static char max_size[100];
770 int len = strlen(str);
772 for (pnt = max_size; len > 0; len--, str++) {
773 *pnt++ = (islower(*str) ? toupper(*str) : *str);
780 * Parse command line arguments
783 parseargs(int argc, char *argv[], struct commandline *cmd)
788 char flag[(1 << (8 * sizeof(char)))];
792 cmd->infile = cmd->outfile = NULL;
796 memset(flag, 0, sizeof(flag));
798 for (i = 1; i < argc; i++) {
799 if (argv[i][0] != '-') {
803 cmd->infile = argv[i];
805 for (j = 1; argv[i][j] != 0; j++) {
829 if (argv[i][j - 1] != '-' || argv[i][j + 1] != 0) {
837 if (!streq(argv[i], "udp") && !streq(argv[i], "tcp")) {
840 } else if (c == 'o') {
844 cmd->outfile = argv[i];
848 if (argv[i][j - 1] != '-')
850 prefix = &argv[i][j + 1];
853 if (argv[i][j - 1] != '-')
855 IncludeDir[nincludes++] = &argv[i][j - 1];
865 cmd->cflag = cflag = flag['c'];
866 cmd->hflag = hflag = flag['h'];
867 cmd->sflag = flag['s'];
868 cmd->lflag = flag['l'];
869 cmd->mflag = flag['m'];
870 cmd->xflag = xflag = flag['x'];
871 cmd->yflag = yflag = flag['y'];
872 cmd->Cflag = Cflag = flag['C'];
873 cmd->Sflag = Sflag = flag['S'];
874 cmd->rflag = flag['r'];
875 cmd->kflag = kflag = flag['k'];
876 cmd->pflag = flag['p'];
877 cmd->dflag = debug = flag['d'];
882 cmd->cflag + cmd->hflag + cmd->sflag + cmd->lflag + cmd->mflag +
883 cmd->Cflag + cmd->Sflag + cmd->rflag;
885 if (cmd->outfile != NULL || cmd->infile == NULL) {
888 } else if (nflags > 1) {