/*
* Sun RPC is a product of Sun Microsystems, Inc. and is provided for
* unrestricted use provided that this legend is included on all tape
* media and as a part of the software program in whole or part. Users
* may copy or modify Sun RPC without charge, but are not authorized
* to license or distribute it to anyone else except as part of a product or
* program developed by the user or with the express written consent of
* Sun Microsystems, Inc.
*
* SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
* WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
* PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
*
* Sun RPC is provided with no support and without any obligation on the
* part of Sun Microsystems, Inc. to assist in its use, correction,
* modification or enhancement.
*
* SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
* INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
* OR ANY PART THEREOF.
*
* In no event will Sun Microsystems, Inc. be liable for any lost revenue
* or profits or other special, indirect and consequential damages, even if
* Sun has been advised of the possibility of such damages.
*
* Sun Microsystems, Inc.
* 2550 Garcia Avenue
* Mountain View, California 94043
*/
#define EXTEND 1 /* alias for TRUE */
#define DONT_EXTEND 0 /* alias for FALSE */
struct commandline {
int cflag; /* xdr C routines */
int hflag; /* header file */
int lflag; /* client side stubs */
int mflag; /* server side stubs */
int nflag; /* netid flag */
int sflag; /* server stubs for the given transport */
int tflag; /* dispatch Table file */
int Ssflag; /* produce server sample code */
int Scflag; /* produce client sample code */
char *infile; /* input module name */
char *outfile; /* output module name */
};
static const char *arglist[ARGLISTLEN];
static int argcount = FIXEDARGS;
int nonfatalerrors; /* errors */
int inetdflag /* = 1 */ ; /* Support for inetd *//* is now the default */
int pmflag; /* Support for port monitors */
int logflag; /* Use syslog instead of fprintf for errors */
int tblflag; /* Support for dispatch table file */
int BSDflag; /* use BSD cplusplus macros */
int callerflag; /* Generate svc_caller() function */
int docleanup = 1; /* cause atexit to remove files */
#define INLINE 3
/*length at which to start doing an inline */
int doinline = INLINE; /* length at which to start doing an inline. 3
* = default if 0, no xdr_inline code */
int indefinitewait; /* If started by port monitors, hang till it
* wants */
int exitnow; /* If started by port monitors, exit after the
* call */
int timerflag; /* TRUE if !indefinite && !exitnow */
int newstyle; /* newstyle of passing arguments (by value) */
int Mflag = 0; /* multithread safe */
static int allfiles; /* generate all files */
int tirpcflag = 1; /* generating code for tirpc, by default */
res = alloc(strlen(file) + strlen(ext) + 1);
if (res == NULL) {
err(EXIT_FAILURE, "Out of memory");
}
p = strrchr(file, '.');
if (p == NULL) {
p = file + strlen(file);
}
(void) strcpy(res, file);
(void) strcpy(res + (p - file), ext);
return (res);
}
/*
* Open output file with given extension
*/
static void
open_output(const char *infile, const char *outfile)
{
if (outfile == NULL) {
fout = stdout;
return;
}
if (infile != NULL && streq(outfile, infile)) {
errx(EXIT_FAILURE, "Output would overwrite `%s'", infile);
}
fout = fopen(outfile, "w");
if (fout == NULL) {
err(EXIT_FAILURE, "Can't open `%s'", outfile);
}
record_open(outfile);
}
static void
add_warning(void)
{
f_print(fout, "/*\n");
f_print(fout, " * Please do not edit this file.\n");
f_print(fout, " * It was generated using rpcgen.\n");
f_print(fout, " */\n\n");
}
/* clear list of arguments */
static void
clear_args(void)
{
int i;
for (i = FIXEDARGS; i < ARGLISTLEN; i++)
arglist[i] = NULL;
argcount = FIXEDARGS;
}
/*
* Open input file with given define for C-preprocessor
*/
static void
open_input(const char *infile, const char *define)
{
int pd[2];
/*
* Convert to valid C symbol name and make it upper case.
* Map non alphanumerical characters to '_'.
*
* Leave extension as it is. It will be handled in extendfile().
*/
for (tmp = guard; *tmp; tmp++) {
if (islower((unsigned char)*tmp))
*tmp = toupper((unsigned char)*tmp);
else if (isupper((unsigned char)*tmp))
continue;
else if (isdigit((unsigned char)*tmp))
continue;
else if (*tmp == '_')
continue;
else if (tmp == extdot)
break;
else
*tmp = '_';
}
/*
* Can't have a '_' or '.' at the front of a symbol name, because it
* will end up as "__".
*
* Prefix it with "RPCGEN_".
*/
if (guard[0] == '_' || guard[0] == '.') {
if (asprintf(&tmp2, "RPCGEN_%s", guard) == -1) {
err(EXIT_FAILURE, "asprintf");
}
free(guard);
guard = tmp2;
}
tell = ftell(fout);
/* print data definitions */
while ((def = get_definition()) != NULL) {
print_datadef(def);
}
/* print function declarations. Do this after data definitions
* because they might be used as arguments for functions */
did = 0;
for (l = defined; l != NULL; l = l->next) {
print_funcdef(l->val, &did);
}
print_funcend(did);
for (l = defined; l != NULL; l = l->next) {
print_progdef(l->val);
}
}
/*
* if input file is stdin and an output file is specified then complain
* if the file already exists. Otherwise the file may get overwritten
* If input file does not exist, exit with an error
*/
if (infile) /* infile ! = NULL */
if (stat(infile, &buf) < 0) {
err(EXIT_FAILURE, "Can't stat `%s'", infile);
};
#if 0
if (outfile) {
if (stat(outfile, &buf) < 0)
return; /* file does not exist */
else {
errx(EXIT_FAILURE,
"`%s' already exists and would be overwritten",
outfile);
}
}
#endif
}
/*
* Parse command line arguments
*/
static int
parseargs(int argc, char *argv[], struct commandline *cmd)
{
int i;
int j;
int c;
char flag[1 << CHAR_BIT];
int nflags;
cmdname = argv[0];
cmd->infile = cmd->outfile = NULL;
if (argc < 2) {
return (0);
}
allfiles = 0;
flag['c'] = 0;
flag['h'] = 0;
flag['l'] = 0;
flag['m'] = 0;
flag['o'] = 0;
flag['s'] = 0;
flag['n'] = 0;
flag['t'] = 0;
flag['S'] = 0;
flag['C'] = 0;
for (i = 1; i < argc; i++) {
if (argv[i][0] != '-') {
if (cmd->infile) {
f_print(stderr, "Cannot specify more than one input file!\n");
return (0);
}
cmd->infile = argv[i];
} else {
for (j = 1; argv[i][j] != 0; j++) {
c = argv[i][j];
switch (c) {
case 'A':
callerflag = 1;
break;
case 'a':
allfiles = 1;
break;
case 'B':
BSDflag = 1;
break;
case 'c':
case 'h':
case 'l':
case 'm':
case 't':
if (flag[c]) {
return (0);
}
flag[c] = 1;
break;
case 'S':
/* sample flag: Ss or Sc. Ss means set
* flag['S']; Sc means set flag['C']; */
c = argv[i][++j]; /* get next char */
if (c == 's')
c = 'S';
else
if (c == 'c')
c = 'C';
else
return (0);
if (flag[c]) {
return (0);
}
flag[c] = 1;
break;
case 'C': /* deprecated ANSI C syntax */
break;
case 'b': /* turn TIRPC flag off for
* generating backward
* compatible */
tirpcflag = 0;
break;
case 'I':
inetdflag = 1;
break;
case 'M':
Mflag = 1;
break;
case 'N':
newstyle = 1;
break;
case 'L':
logflag = 1;
break;
case 'K':
if (++i == argc) {
return (0);
}
svcclosetime = argv[i];
goto nextarg;
case 'T':
tblflag = 1;
break;
case 'i':
if (++i == argc) {
return (0);
}
doinline = atoi(argv[i]);
goto nextarg;
case 'n':
case 'o':
case 's':
if (argv[i][j - 1] != '-' ||
argv[i][j + 1] != 0) {
return (0);
}
flag[c] = 1;
if (++i == argc) {
return (0);
}
if (c == 's') {
if (!streq(argv[i], "udp") &&
!streq(argv[i], "tcp")) {
return (0);
}
} else
if (c == 'o') {
if (cmd->outfile) {
return (0);
}
cmd->outfile = argv[i];
}
goto nextarg;
case 'D':
if (argv[i][j - 1] != '-') {
return (0);
}
(void) addarg(argv[i]);
goto nextarg;
case 'Y':
if (++i == argc) {
return (0);
}
(void) strlcpy(pathbuf, argv[i],
sizeof(pathbuf));
(void) strlcat(pathbuf, "/cpp",
sizeof(pathbuf));
CPP = pathbuf;
goto nextarg;
if (tirpcflag) {
pmflag = inetdflag ? 0 : 1; /* pmflag or inetdflag is
* always TRUE */
if ((inetdflag && cmd->nflag)) { /* netid not allowed
* with inetdflag */
f_print(stderr, "Cannot use netid flag with inetd flag!\n");
return (0);
}
} else { /* 4.1 mode */
pmflag = 0; /* set pmflag only in tirpcmode */
inetdflag = 1; /* inetdflag is TRUE by default */
if (cmd->nflag) { /* netid needs TIRPC */
f_print(stderr, "Cannot use netid flag without TIRPC!\n");
return (0);
}
}
if (newstyle && (tblflag || cmd->tflag)) {
f_print(stderr, "Cannot use table flags with newstyle!\n");
return (0);
}
/* check no conflicts with file generation flags */
nflags = cmd->cflag + cmd->hflag + cmd->lflag + cmd->mflag +
cmd->sflag + cmd->nflag + cmd->tflag + cmd->Ssflag + cmd->Scflag;
if (nflags == 0) {
if (cmd->outfile != NULL || cmd->infile == NULL) {
return (0);
}
} else
if (nflags > 1) {
f_print(stderr, "Cannot have more than one file generation flag!\n");
return (0);
}
return (1);
}
static void
options_usage(void)
{
f_print(stderr, "options:\n");
f_print(stderr, "-A\t\tgenerate svc_caller() function\n");
f_print(stderr, "-a\t\tgenerate all files, including samples\n");
f_print(stderr, "-B\t\tgenerate BSD c++ macros\n");
f_print(stderr, "-b\t\tbackward compatibility mode (generates code for SunOS 4.1)\n");
f_print(stderr, "-c\t\tgenerate XDR routines\n");
f_print(stderr, "-Dname[=value]\tdefine a symbol (same as #define)\n");
f_print(stderr, "-h\t\tgenerate header file\n");
f_print(stderr, "-I\t\tgenerate code for inetd support in server (for SunOS 4.1)\n");
f_print(stderr, "-i size\t\tsize at which to start generating inline code\n");
f_print(stderr, "-K seconds\tserver exits after K seconds of inactivity\n");
f_print(stderr, "-L\t\tserver errors will be printed to syslog\n");
f_print(stderr, "-l\t\tgenerate client side stubs\n");
f_print(stderr, "-M\t\tgenerate thread-safe stubs\n");
f_print(stderr, "-m\t\tgenerate server side stubs\n");
f_print(stderr, "-N\t\tsupports multiple arguments and call-by-value\n");
f_print(stderr, "-n netid\tgenerate server code that supports named netid\n");
f_print(stderr, "-o outfile\tname of the output file\n");
f_print(stderr, "-s nettype\tgenerate server code that supports named nettype\n");
f_print(stderr, "-Sc\t\tgenerate sample client code that uses remote procedures\n");
f_print(stderr, "-Ss\t\tgenerate sample server code that defines remote procedures\n");
f_print(stderr, "-T\t\tgenerate code to support RPC dispatch tables\n");
f_print(stderr, "-t\t\tgenerate RPC dispatch table\n");
f_print(stderr, "-v\t\tdisplay version number\n");
f_print(stderr, "-Y path\t\tdirectory name to find C preprocessor (cpp)\n");