rpc_main.c revision 71a535e3b24d87fb6be3793380f86bd09f2768ac
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* University Copyright- Copyright (c) 1982, 1986, 1988
* The Regents of the University of California
* All Rights Reserved
*
* University Acknowledgment- Portions of this document are derived from
* software developed by the University of California, Berkeley, and its
* contributors.
*/
/*
* rpc_main.c, Top level of the RPC protocol compiler.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <ctype.h>
#include "rpc_parse.h"
#include "rpc_util.h"
#include "rpc_scan.h"
extern void write_sample_svc(definition *);
extern int write_sample_clnt(definition *);
extern void write_sample_clnt_main(void);
extern void reinitialize(void);
extern void crash(void);
extern void add_type(int, char *);
extern void add_sample_msg(void);
static void svc_output(char *, char *, int, char *);
static void clnt_output(char *, char *, int, char *);
static void c_output(char *, char *, int, char *);
static void mkfile_output(struct commandline *);
static void c_initialize(void);
static void h_output(char *, char *, int, char *);
static void s_output(int, char *[], char *, char *, int, char *, int, int);
static void l_output(char *, char *, int, char *);
static void t_output(char *, char *, int, char *);
static int do_registers(int, char *[]);
static void usage(void);
static void version_info(void);
static void options_usage(void);
#define DONT_EXTEND 0 /* alias for FALSE */
static int cppDefined = 0; /* explicit path for C preprocessor */
static char *cmdname;
static char *svcclosetime = "120";
static char CPPFLAGS[] = "-C";
static char *allv[] = {
"rpcgen", "-s", "udp", "-s", "tcp",
};
static char *allnv[] = {
"rpcgen", "-s", "netpath",
};
/*
* machinations for handling expanding argument list
*/
static void addarg(char *); /* add another argument to the list */
static void putarg(int, char *); /* put argument at specified location */
static void clear_args(void); /* clear argument list */
static void checkfiles(char *, char *); /* check if out file already exists */
#define ARGLISTLEN 20
#define FIXEDARGS 2
static char *arglist[ARGLISTLEN];
int nonfatalerrors; /* errors */
int inetdflag; /* 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 mtflag = 0; /* Support for MT */
int mtauto = 0; /* Enable automatic mode */
#define INLINE 5
/* length at which to start doing an inline */
/*
* Length at which to start doing an inline. INLINE = 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 Cflag = 0; /* ANSI C syntax */
int CCflag = 0; /* C++ files */
static int allfiles; /* generate all files */
int
{
struct commandline cmd;
clear_args();
usage();
/*
* Only the client and server side stubs are likely to be customized,
* so in that case only, check if the outfile exists, and if so,
* print an error message and exit.
*/
else
} else if (cmd.makefileflag) {
mkfile_output(&cmd);
} else {
/* the rescans are required, since cpp may effect input */
reinitialize();
reinitialize();
reinitialize();
else
if (tblflag) {
reinitialize();
}
if (allfiles) {
reinitialize();
"_server.c");
reinitialize();
"_client.c");
}
reinitialize();
mkfile_output(&cmd);
}
}
return (nonfatalerrors);
}
/*
* add extension to filename
*/
static char *
{
char *res;
char *p;
abort();
if (p == NULL)
return (res);
}
/*
* Open output file with given extension
*/
static void
{
return;
}
"%s: %s already exists. No output generated.\n",
crash();
}
crash();
}
}
static void
add_warning(void)
{
}
/* clear list of arguments */
static void
clear_args(void)
{
int i;
for (i = FIXEDARGS; i < ARGLISTLEN; i++)
}
/* make sure that a CPP exists */
static void
find_cpp(void)
{
if (cppDefined) {
"cannot find C preprocessor: %s \n", CPP);
crash();
} else { /* try the other one */
"cannot find any C preprocessor (cpp)\n");
crash();
}
}
}
}
/*
* Open input file with given define for C-preprocessor
*/
static void
open_input(char *infile, char *define)
{
int pd[2];
case 0:
find_cpp();
addarg(define);
if (infile)
(void) close(1);
perror("execv");
exit(1);
/* NOTREACHED */
case -1:
perror("fork");
exit(1);
}
crash();
}
}
/* valid tirpc nettypes */
static char *valid_ti_nettypes[] = {
"netpath",
"visible",
"circuit_v",
"datagram_v",
"circuit_n",
"datagram_n",
"udp",
"tcp",
"raw",
};
/* valid inetd nettypes */
static char *valid_i_nettypes[] = {
"udp",
"tcp",
};
static int
{
int i;
for (i = 0; list_to_check[i] != NULL; i++) {
return (1);
}
}
return (0);
}
static char *
{
char *temp;
return (temp);
else
return ((char *)" ");
}
static void
{
char *include;
char *outfilename;
long tell;
c_initialize();
open_input(infile, define);
add_warning();
free(include);
} else
/*
* Include stdlib.h to support mem_alloc calls.
*/
while (def = get_definition()) {
}
(void) unlink(outfilename);
}
}
static void
c_initialize(void)
{
/*
* add all the starting basic types.
* We may need to add some derived types
* if we need to generate INLINE macros.
*/
}
char rpcgen_table_dcl1[] = "struct rpcgen_table {\n";
char rpcgen_table_dcl2[] = "\txdrproc_t\txdr_arg;\n"
"\tunsigned\tlen_arg;\n"
"\txdrproc_t\txdr_res;\n"
"\tunsigned\tlen_res;\n"
"};\n";
char rpcgen_table_proc[] = "\tvoid\t*(*proc)();\n";
char rpcgen_table_proc_b[] = "\tchar\t*(*proc)();\n";
char *
generate_guard(char *pathname)
{
/*
* Guard must be an ANSI C identifier composed of
* upper case letters, digits, or '_'.
* Convert invalid characters to '_'.
*/
*tmp = '_';
continue;
}
}
/*
* The first character must be a letter; the underscore '_'
* counts as a letter.
*/
guard[0] = '_';
return (guard);
}
/*
* Compile into an XDR header file
*/
static void
{
char *outfilename;
long tell;
char *guard;
list *l;
int i;
open_input(infile, define);
add_warning();
if (outfilename || infile)
else
guard = "STDIN_";
if (mtflag) {
};
/* put the C++ support */
}
/* put in a typedef for quadprecision. Only with Cflag */
/* print data definitions */
while (def = get_definition())
/*
* print function declarations.
* Do this after data definitions because they might be used as
* arguments for functions
*/
print_funcdef(l->val);
/* Now print all xdr func declarations */
if (xdrfunc_head != NULL) {
if (CCflag) {
}
if (!Cflag) {
}
} else {
for (i = 1; i < 3; i++) {
if (i == 1)
"\n#if defined(__STDC__) || defined(__cplusplus)\n");
else
}
}
}
}
(void) unlink(outfilename);
} else if (tblflag) {
if (tirpcflag)
else
}
if (Cflag) {
}
}
/*
* Compile into an RPC service
*/
static void
{
char *include;
int foundprogram = 0;
char *outfilename;
open_input(infile, define);
add_warning();
free(include);
} else
if (Cflag) {
"#include <rpc/pmap_clnt.h> /* for pmap_unset */\n");
}
indefinitewait = 1;
exitnow = 1;
timerflag = 1;
if (tirpcflag)
if (tirpcflag)
}
if (tirpcflag)
/* for ANSI-C */
if (Cflag)
"\n#ifndef SIG_PF\n#define SIG_PF void(*)\
(int)\n#endif\n");
if (timerflag)
while (def = get_definition())
if (extend && !foundprogram) {
(void) unlink(outfilename);
return;
}
if (!nomain) {
if (outfilename)
(void) unlink(outfilename);
usage();
}
write_rest();
}
}
/*
* generate client side stubs
*/
static void
{
char *include;
int foundprogram = 0;
char *outfilename;
open_input(infile, define);
add_warning();
if (Cflag)
free(include);
} else
while (def = get_definition())
if (extend && !foundprogram) {
(void) unlink(outfilename);
return;
}
write_stubs();
}
/*
* generate the dispatch table
*/
static void
{
int foundprogram = 0;
char *outfilename;
open_input(infile, define);
add_warning();
while (def = get_definition()) {
}
if (extend && !foundprogram) {
(void) unlink(outfilename);
return;
}
write_tables();
}
/* sample routine for the server template */
static void
{
char *include;
char *outfilename;
long tell;
open_input(infile, define);
/*
* Check if outfile already exists.
* if so, print an error message and exit
*/
free(include);
} else {
}
while (def = get_definition())
(void) unlink(outfilename);
}
/* sample main routine for client */
static void
{
char *include;
char *outfilename;
long tell;
int has_program = 0;
open_input(infile, define);
/*
* Check if outfile already exists.
* if so, print an error message and exit
*/
free(include);
} else
while (def = get_definition())
if (has_program)
(void) unlink(outfilename);
}
static void
{
char *temp;
if (allfiles) {
} else {
servername = " ";
clientname = " ";
}
if (allfiles) {
if (mkfilename == NULL) {
return;
}
} else
by rpcgen \n");
"$(TARGETS_CLNT.c:%%.c=%%.o) ");
"$(TARGETS_SVC.c:%%.c=%%.o) ");
if (mtflag)
"CFLAGS += -g\nLDLIBS += -lnsl\n");
else
$(TARGETS_CLNT.c) \n\n");
$(TARGETS_SVC.c) \n\n");
$(LDLIBS) \n\n");
$(OBJECTS_SVC) $(CLIENT) $(SERVER)\n\n");
}
/*
* Perform registrations for service output
* Return 0 if failed; 1 otherwise.
*/
static int
{
int i;
for (i = 1; i < argc; i++) {
return (0);
i++;
}
}
} else {
for (i = 1; i < argc; i++)
return (0);
i++;
i++;
}
}
return (1);
}
/*
* Add another argument to the arg list
*/
static void
{
if (argcount >= ARGLISTLEN) {
crash();
/*NOTREACHED*/
}
}
static void
{
if (where >= ARGLISTLEN) {
crash();
/*NOTREACHED*/
}
}
/*
* 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
*/
static void
{
if (infile) { /* infile ! = NULL */
crash();
}
}
if (outfile) {
return; /* file does not exist */
"file '%s' already exists and may be overwritten\n",
outfile);
crash();
}
}
/*
* Parse command line arguments
*/
static uint_t
{
int i;
int j;
char c, ch;
int nflags;
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;
flag['M'] = 0;
for (i = 1; i < argc; i++) {
if (argv[i][0] != '-') {
"Cannot specify more than one input file.\n");
return (0);
}
} else {
for (j = 1; argv[i][j] != 0; j++) {
c = argv[i][j];
switch (c) {
case 'a':
allfiles = 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'];
* Sm means set flag['M'];
*/
if (ch == 's')
ch = 'S';
else if (ch == 'c')
ch = 'C';
else if (ch == 'm')
ch = 'M';
else
return (0);
return (0);
break;
case 'C': /* ANSI C syntax */
Cflag = 1;
if (ch != 'C')
break;
CCflag = 1;
break;
case 'b':
/*
* Turn TIRPC flag off for
* generating backward compatible
* code
*/
tirpcflag = 0;
break;
case 'I':
inetdflag = 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 'A':
mtauto = 1;
/* FALLTHRU */
case 'M':
mtflag = 1;
break;
case 'i' :
if (++i == argc)
return (0);
goto nextarg;
case 'n':
case 'o':
case 's':
argv[i][j + 1] != 0)
return (0);
flag[c] = 1;
if (++i == argc)
return (0);
if (c == 'o') {
return (0);
}
goto nextarg;
case 'D':
return (0);
goto nextarg;
case 'v':
version_info();
return (0);
case 'Y':
if (++i == argc)
return (0);
cppDefined = 1;
goto nextarg;
case 'r':
break;
default:
return (0);
}
}
;
}
}
if (tirpcflag) {
if (inetdflag) {
"Cannot use -I flag without -b flag.\n");
return (0);
}
pmflag = 1;
} else { /* 4.1 mode */
pmflag = 0; /* set pmflag only in tirpcmode */
"Cannot use netid flag without TIRPC.\n");
return (0);
}
}
return (0);
}
/* check no conflicts with file generation flags */
if (nflags == 0) {
return (0);
" generation flags.\n");
return (0);
}
if (nflags > 1) {
"Cannot have more than one file generation flag.\n");
return (0);
}
return (1);
}
static void
usage(void)
{
" [-I [-K seconds]] [-Y path] infile\n", cmdname);
" [-o outfile] [infile]\n", cmdname);
exit(1);
}
static void
version_info(void)
{
exit(1);
}
static void
options_usage(void)
{
/* BEGIN CSTYLED */
" for SunOS 4.X)\n");
" inline code\n");
" (for SunOS 4.X)\n");
" inactivity\n");
" named netid\n");
" call-by-value\n");
" nettype\n");
" procedures\n");
" remote procedures\n");
/* END CSTYLED */
exit(1);
}