Blame sunrpc/rpc_clntout.c

Packit 6c4009
/*
Packit 6c4009
 * rpc_clntout.c, Client-stub outputter for the RPC protocol compiler
Packit 6c4009
 * Copyright (c) 2010, Oracle America, Inc.
Packit 6c4009
 *
Packit 6c4009
 * Redistribution and use in source and binary forms, with or without
Packit 6c4009
 * modification, are permitted provided that the following conditions are
Packit 6c4009
 * met:
Packit 6c4009
 *
Packit 6c4009
 *     * Redistributions of source code must retain the above copyright
Packit 6c4009
 *       notice, this list of conditions and the following disclaimer.
Packit 6c4009
 *     * Redistributions in binary form must reproduce the above
Packit 6c4009
 *       copyright notice, this list of conditions and the following
Packit 6c4009
 *       disclaimer in the documentation and/or other materials
Packit 6c4009
 *       provided with the distribution.
Packit 6c4009
 *     * Neither the name of the "Oracle America, Inc." nor the names of its
Packit 6c4009
 *       contributors may be used to endorse or promote products derived
Packit 6c4009
 *       from this software without specific prior written permission.
Packit 6c4009
 *
Packit 6c4009
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 6c4009
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 6c4009
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
Packit 6c4009
 *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
Packit 6c4009
 *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
Packit 6c4009
 *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 6c4009
 *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
Packit 6c4009
 *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit 6c4009
 *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
Packit 6c4009
 *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
Packit 6c4009
 *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 6c4009
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 6c4009
 */
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include "rpc/types.h"
Packit 6c4009
#include "rpc_parse.h"
Packit 6c4009
#include "rpc_util.h"
Packit 6c4009
#include "proto.h"
Packit 6c4009
Packit 6c4009
#define DEFAULT_TIMEOUT 25	/* in seconds */
Packit 6c4009
static const char RESULT[] = "clnt_res";
Packit 6c4009
Packit 6c4009
static void write_program (definition * def);
Packit 6c4009
static void printbody (proc_list * proc);
Packit 6c4009
static const char *ampr (const char *type);
Packit 6c4009
static void printbody (proc_list * proc);
Packit 6c4009
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
write_stubs (void)
Packit 6c4009
{
Packit 6c4009
  list *l;
Packit 6c4009
  definition *def;
Packit 6c4009
Packit 6c4009
  fprintf (fout,
Packit 6c4009
	   "\n/* Default timeout can be changed using clnt_control() */\n");
Packit 6c4009
  fprintf (fout, "static struct timeval TIMEOUT = { %d, 0 };\n",
Packit 6c4009
	   DEFAULT_TIMEOUT);
Packit 6c4009
  for (l = defined; l != NULL; l = l->next)
Packit 6c4009
    {
Packit 6c4009
      def = (definition *) l->val;
Packit 6c4009
      if (def->def_kind == DEF_PROGRAM)
Packit 6c4009
	{
Packit 6c4009
	  write_program (def);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
write_program (definition * def)
Packit 6c4009
{
Packit 6c4009
  version_list *vp;
Packit 6c4009
  proc_list *proc;
Packit 6c4009
Packit 6c4009
  for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
Packit 6c4009
    {
Packit 6c4009
      for (proc = vp->procs; proc != NULL; proc = proc->next)
Packit 6c4009
	{
Packit 6c4009
	  fprintf (fout, "\n");
Packit 6c4009
	  if (mtflag == 0)
Packit 6c4009
	    {
Packit 6c4009
	      ptype (proc->res_prefix, proc->res_type, 1);
Packit 6c4009
	      fprintf (fout, "*\n");
Packit 6c4009
	      pvname (proc->proc_name, vp->vers_num);
Packit 6c4009
	      printarglist (proc, RESULT, "clnt", "CLIENT *");
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    {
Packit 6c4009
	      fprintf (fout, "enum clnt_stat \n");
Packit 6c4009
	      pvname (proc->proc_name, vp->vers_num);
Packit 6c4009
	      printarglist (proc, RESULT, "clnt", "CLIENT *");
Packit 6c4009
	    }
Packit 6c4009
	  fprintf (fout, "{\n");
Packit 6c4009
	  printbody (proc);
Packit 6c4009
	  fprintf (fout, "}\n");
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Writes out declarations of procedure's argument list.
Packit 6c4009
   In either ANSI C style, in one of old rpcgen style (pass by reference),
Packit 6c4009
   or new rpcgen style (multiple arguments, pass by value);
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
/* sample addargname = "clnt"; sample addargtype = "CLIENT * " */
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
printarglist (proc_list * proc,  const char *result,
Packit 6c4009
	      const char *addargname, const char *addargtype)
Packit 6c4009
{
Packit 6c4009
Packit 6c4009
  decl_list *l;
Packit 6c4009
Packit 6c4009
  if (!newstyle)
Packit 6c4009
    { /* old style: always pass argument by reference */
Packit 6c4009
      if (Cflag)
Packit 6c4009
	{			/* C++ style heading */
Packit 6c4009
	  fprintf (fout, "(");
Packit 6c4009
	  ptype (proc->args.decls->decl.prefix,
Packit 6c4009
		 proc->args.decls->decl.type, 1);
Packit 6c4009
Packit 6c4009
	  if (mtflag)
Packit 6c4009
	    {/* Generate result field */
Packit 6c4009
	      fprintf (fout, "*argp, ");
Packit 6c4009
	      ptype(proc->res_prefix, proc->res_type, 1);
Packit 6c4009
	      fprintf (fout, "*%s, %s%s)\n", result, addargtype, addargname);
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    fprintf (fout, "*argp, %s%s)\n", addargtype, addargname);
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  if (!mtflag)
Packit 6c4009
	    fprintf (fout, "(argp, %s)\n", addargname);
Packit 6c4009
	  else
Packit 6c4009
	    fprintf (fout, "(argp, %s, %s)\n", result, addargname);
Packit 6c4009
	  fprintf (fout, "\t");
Packit 6c4009
	  ptype (proc->args.decls->decl.prefix,
Packit 6c4009
		 proc->args.decls->decl.type, 1);
Packit 6c4009
	  fprintf (fout, "*argp;\n");
Packit 6c4009
	  if (mtflag)
Packit 6c4009
	    {
Packit 6c4009
	      fprintf (fout, "\t");
Packit 6c4009
	      ptype (proc->res_prefix, proc->res_type, 1);
Packit 6c4009
	      fprintf (fout, "*%s;\n", result);
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  else if (streq (proc->args.decls->decl.type, "void"))
Packit 6c4009
    {
Packit 6c4009
      /* newstyle, 0 argument */
Packit 6c4009
      if (mtflag)
Packit 6c4009
	{
Packit 6c4009
	  fprintf (fout, "(");
Packit 6c4009
	  if (Cflag)
Packit 6c4009
	    {
Packit 6c4009
	      ptype(proc->res_prefix, proc->res_type, 1);
Packit 6c4009
	      fprintf (fout, "*%s, %s%s)\n", result, addargtype, addargname);
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    fprintf (fout, "(%s)\n", addargname);
Packit 6c4009
	}
Packit 6c4009
      else if (Cflag)
Packit 6c4009
	fprintf (fout, "(%s%s)\n", addargtype, addargname);
Packit 6c4009
      else
Packit 6c4009
	fprintf (fout, "(%s)\n", addargname);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      /* new style, 1 or multiple arguments */
Packit 6c4009
      if (!Cflag)
Packit 6c4009
	{
Packit 6c4009
	  fprintf (fout, "(");
Packit 6c4009
	  for (l = proc->args.decls; l != NULL; l = l->next)
Packit 6c4009
	    fprintf (fout, "%s, ", l->decl.name);
Packit 6c4009
	  if (mtflag)
Packit 6c4009
	    fprintf (fout, "%s, ", result);
Packit 6c4009
 	  fprintf (fout, "%s)\n", addargname);
Packit 6c4009
	  for (l = proc->args.decls; l != NULL; l = l->next)
Packit 6c4009
	    {
Packit 6c4009
	      pdeclaration (proc->args.argname, &l->decl, 1, ";\n");
Packit 6c4009
	    }
Packit 6c4009
	  if (mtflag)
Packit 6c4009
	    {
Packit 6c4009
	      fprintf (fout, "\t");
Packit 6c4009
	      ptype (proc->res_prefix, proc->res_type, 1);
Packit 6c4009
	      fprintf (fout, "*%s;\n", result);
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{			/* C++ style header */
Packit 6c4009
	  fprintf (fout, "(");
Packit 6c4009
	  for (l = proc->args.decls; l != NULL; l = l->next)
Packit 6c4009
	    {
Packit 6c4009
	      pdeclaration (proc->args.argname, &l->decl, 0, ", ");
Packit 6c4009
	    }
Packit 6c4009
	  if (mtflag)
Packit 6c4009
	    {
Packit 6c4009
	      ptype (proc->res_prefix, proc->res_type, 1);
Packit 6c4009
	      fprintf (fout, "*%s, ", result);
Packit 6c4009
	    }
Packit 6c4009
 	  fprintf (fout, " %s%s)\n", addargtype, addargname);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (!Cflag)
Packit 6c4009
    fprintf (fout, "\t%s%s;\n", addargtype, addargname);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static
Packit 6c4009
const char *
Packit 6c4009
ampr (const char *type)
Packit 6c4009
{
Packit 6c4009
  if (isvectordef (type, REL_ALIAS))
Packit 6c4009
    {
Packit 6c4009
      return "";
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      return "&";
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
printbody (proc_list * proc)
Packit 6c4009
{
Packit 6c4009
  decl_list *l;
Packit 6c4009
  bool_t args2 = (proc->arg_num > 1);
Packit 6c4009
/*  int i; */
Packit 6c4009
Packit 6c4009
  /* For new style with multiple arguments, need a structure in which
Packit 6c4009
     to stuff the arguments. */
Packit 6c4009
  if (newstyle && args2)
Packit 6c4009
    {
Packit 6c4009
      fprintf (fout, "\t%s", proc->args.argname);
Packit 6c4009
      fprintf (fout, " arg;\n");
Packit 6c4009
    }
Packit 6c4009
  if (!mtflag)
Packit 6c4009
    {
Packit 6c4009
      fprintf (fout, "\tstatic ");
Packit 6c4009
      if (streq (proc->res_type, "void"))
Packit 6c4009
	{
Packit 6c4009
	  fprintf (fout, "char ");
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  ptype (proc->res_prefix, proc->res_type, 0);
Packit 6c4009
	}
Packit 6c4009
      fprintf (fout, "%s;\n", RESULT);
Packit 6c4009
      fprintf (fout, "\n");
Packit 6c4009
      fprintf (fout, "\tmemset((char *)%s%s, 0, sizeof(%s));\n",
Packit 6c4009
	       ampr (proc->res_type), RESULT, RESULT);
Packit 6c4009
    }
Packit 6c4009
  if (newstyle && !args2 && (streq (proc->args.decls->decl.type, "void")))
Packit 6c4009
    {
Packit 6c4009
      /* newstyle, 0 arguments */
Packit 6c4009
      if (mtflag)
Packit 6c4009
	fprintf (fout, "\t return ");
Packit 6c4009
      else
Packit 6c4009
	fprintf (fout, "\t if ");
Packit 6c4009
      fprintf (fout,
Packit 6c4009
	       "(clnt_call (clnt, %s, (xdrproc_t) xdr_void, ", proc->proc_name);
Packit 6c4009
Packit 6c4009
      fprintf (fout,
Packit 6c4009
	       "(caddr_t) NULL,\n\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,",
Packit 6c4009
	       stringfix(proc->res_type), (mtflag)?"":ampr(proc->res_type),
Packit 6c4009
	       RESULT);
Packit 6c4009
      if (mtflag)
Packit 6c4009
	fprintf (fout, "\n\t\tTIMEOUT));\n\n");
Packit 6c4009
      else
Packit 6c4009
	fprintf (fout, "\n\t\tTIMEOUT) != RPC_SUCCESS) {\n");
Packit 6c4009
    }
Packit 6c4009
  else if (newstyle && args2)
Packit 6c4009
    {
Packit 6c4009
      /* newstyle, multiple arguments:  stuff arguments into structure */
Packit 6c4009
      for (l = proc->args.decls; l != NULL; l = l->next)
Packit 6c4009
	{
Packit 6c4009
	  fprintf (fout, "\targ.%s = %s;\n",
Packit 6c4009
		   l->decl.name, l->decl.name);
Packit 6c4009
	}
Packit 6c4009
      if (mtflag)
Packit 6c4009
	fprintf (fout, "\treturn ");
Packit 6c4009
      else
Packit 6c4009
	fprintf (fout, "\tif ");
Packit 6c4009
Packit 6c4009
      fprintf (fout,
Packit 6c4009
	       "(clnt_call (clnt, %s, (xdrproc_t) xdr_%s", proc->proc_name,
Packit 6c4009
	       proc->args.argname);
Packit 6c4009
      fprintf (fout,
Packit 6c4009
	       ", (caddr_t) &arg,\n\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,",
Packit 6c4009
	       stringfix(proc->res_type), (mtflag)?"":ampr(proc->res_type),
Packit 6c4009
	       RESULT);
Packit 6c4009
      if (mtflag)
Packit 6c4009
	fprintf (fout, "\n\t\tTIMEOUT));\n");
Packit 6c4009
      else
Packit 6c4009
	fprintf (fout, "\n\t\tTIMEOUT) != RPC_SUCCESS) {\n");
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {				/* single argument, new or old style */
Packit 6c4009
      if (!mtflag)
Packit 6c4009
	fprintf (fout,
Packit 6c4009
		 "\tif (clnt_call (clnt, %s,\n\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,\n\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,\n\t\tTIMEOUT) != RPC_SUCCESS) {\n",
Packit 6c4009
		 proc->proc_name,
Packit 6c4009
		 stringfix (proc->args.decls->decl.type),
Packit 6c4009
		 (newstyle ? "&" : ""),
Packit 6c4009
		 (newstyle ? proc->args.decls->decl.name : "argp"),
Packit 6c4009
		 stringfix (proc->res_type), ampr (proc->res_type),
Packit 6c4009
		 RESULT);
Packit 6c4009
      else
Packit 6c4009
	fprintf(fout,
Packit 6c4009
		"\treturn (clnt_call(clnt, %s,\n\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,\n\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,\n\t\tTIMEOUT));\n",
Packit 6c4009
		proc->proc_name,
Packit 6c4009
		stringfix (proc->args.decls->decl.type),
Packit 6c4009
		(newstyle ? "&" : ""),
Packit 6c4009
		(newstyle ? proc->args.decls->decl.name : "argp"),
Packit 6c4009
		stringfix (proc->res_type), "",
Packit 6c4009
		RESULT);
Packit 6c4009
    }
Packit 6c4009
  if (!mtflag)
Packit 6c4009
    {
Packit 6c4009
      fprintf (fout, "\t\treturn (NULL);\n");
Packit 6c4009
      fprintf (fout, "\t}\n");
Packit 6c4009
      if (streq (proc->res_type, "void"))
Packit 6c4009
	{
Packit 6c4009
	  fprintf (fout, "\treturn ((void *)%s%s);\n",
Packit 6c4009
		   ampr (proc->res_type), RESULT);
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  fprintf (fout, "\treturn (%s%s);\n", ampr (proc->res_type), RESULT);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
}