Blame sunrpc/rpc_tblout.c

Packit 6c4009
/*
Packit 6c4009
 * From: @(#)rpc_tblout.c 1.4 89/02/22
Packit 6c4009
 *
Packit 6c4009
 * Copyright (c) 2010, Oracle America, Inc.
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
Packit 6c4009
/*
Packit 6c4009
 * rpc_tblout.c, Dispatch table outputter for the RPC protocol compiler
Packit 6c4009
 */
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include "rpc_parse.h"
Packit 6c4009
#include "rpc_util.h"
Packit 6c4009
#include "proto.h"
Packit 6c4009
Packit 6c4009
#define TABSIZE		8
Packit 6c4009
#define TABCOUNT	5
Packit 6c4009
#define TABSTOP		(TABSIZE*TABCOUNT)
Packit 6c4009
Packit 6c4009
static const char tabstr[TABCOUNT + 1] = "\t\t\t\t\t";
Packit 6c4009
Packit 6c4009
static const char tbl_hdr[] = "struct rpcgen_table %s_table[] = {\n";
Packit 6c4009
static const char tbl_end[] = "};\n";
Packit 6c4009
Packit 6c4009
static const char null_entry[] = "\n\t(char *(*)())0,\n\
Packit 6c4009
 \t(xdrproc_t) xdr_void,\t\t\t0,\n\
Packit 6c4009
 \t(xdrproc_t) xdr_void,\t\t\t0,\n";
Packit 6c4009
Packit 6c4009
Packit 6c4009
static const char tbl_nproc[] = "int %s_nproc =\n\tsizeof(%s_table)/sizeof(%s_table[0]);\n\n";
Packit 6c4009
Packit 6c4009
static void write_table (const definition * def);
Packit 6c4009
static void printit (const char *prefix, const char *type);
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
write_tables (void)
Packit 6c4009
{
Packit 6c4009
  list *l;
Packit 6c4009
  definition *def;
Packit 6c4009
Packit 6c4009
  f_print (fout, "\n");
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_table (def);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
write_table (const definition * def)
Packit 6c4009
{
Packit 6c4009
  version_list *vp;
Packit 6c4009
  proc_list *proc;
Packit 6c4009
  int current;
Packit 6c4009
  int expected;
Packit 6c4009
  char progvers[100];
Packit 6c4009
  int warning;
Packit 6c4009
Packit 6c4009
  for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
Packit 6c4009
    {
Packit 6c4009
      warning = 0;
Packit 6c4009
      s_print (progvers, "%s_%s",
Packit 6c4009
	       locase (def->def_name), vp->vers_num);
Packit 6c4009
      /* print the table header */
Packit 6c4009
      f_print (fout, tbl_hdr, progvers);
Packit 6c4009
Packit 6c4009
      if (nullproc (vp->procs))
Packit 6c4009
	{
Packit 6c4009
	  expected = 0;
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  expected = 1;
Packit 6c4009
	  f_print (fout, null_entry);
Packit 6c4009
	}
Packit 6c4009
      for (proc = vp->procs; proc != NULL; proc = proc->next)
Packit 6c4009
	{
Packit 6c4009
	  current = atoi (proc->proc_num);
Packit 6c4009
	  if (current != expected++)
Packit 6c4009
	    {
Packit 6c4009
	      f_print (fout,
Packit 6c4009
		       "\n/*\n * WARNING: table out of order\n */\n");
Packit 6c4009
	      if (warning == 0)
Packit 6c4009
		{
Packit 6c4009
		  f_print (stderr,
Packit 6c4009
			   "WARNING %s table is out of order\n",
Packit 6c4009
			   progvers);
Packit 6c4009
		  warning = 1;
Packit 6c4009
		  nonfatalerrors = 1;
Packit 6c4009
		}
Packit 6c4009
	      expected = current + 1;
Packit 6c4009
	    }
Packit 6c4009
	  f_print (fout, "\n\t(char *(*)())RPCGEN_ACTION(");
Packit 6c4009
Packit 6c4009
	  /* routine to invoke */
Packit 6c4009
	  if (Cflag && !newstyle)
Packit 6c4009
	    pvname_svc (proc->proc_name, vp->vers_num);
Packit 6c4009
	  else
Packit 6c4009
	    {
Packit 6c4009
	      if (newstyle)
Packit 6c4009
		f_print (fout, "_");	/* calls internal func */
Packit 6c4009
	      pvname (proc->proc_name, vp->vers_num);
Packit 6c4009
	    }
Packit 6c4009
	  f_print (fout, "),\n");
Packit 6c4009
Packit 6c4009
	  /* argument info */
Packit 6c4009
	  if (proc->arg_num > 1)
Packit 6c4009
	    printit ((char *) NULL, proc->args.argname);
Packit 6c4009
	  else
Packit 6c4009
	    /* do we have to do something special for newstyle */
Packit 6c4009
	    printit (proc->args.decls->decl.prefix,
Packit 6c4009
		     proc->args.decls->decl.type);
Packit 6c4009
	  /* result info */
Packit 6c4009
	  printit (proc->res_prefix, proc->res_type);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* print the table trailer */
Packit 6c4009
      f_print (fout, tbl_end);
Packit 6c4009
      f_print (fout, tbl_nproc, progvers, progvers, progvers);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
printit (const char *prefix, const char *type)
Packit 6c4009
{
Packit 6c4009
  int len;
Packit 6c4009
  int tabs;
Packit 6c4009
Packit 6c4009
Packit 6c4009
  len = fprintf (fout, "\txdr_%s,", stringfix (type));
Packit 6c4009
  /* account for leading tab expansion */
Packit 6c4009
  len += TABSIZE - 1;
Packit 6c4009
  /* round up to tabs required */
Packit 6c4009
  tabs = (TABSTOP - len + TABSIZE - 1) / TABSIZE;
Packit 6c4009
  f_print (fout, "%s", &tabstr[TABCOUNT - tabs]);
Packit 6c4009
Packit 6c4009
  if (streq (type, "void"))
Packit 6c4009
    {
Packit 6c4009
      f_print (fout, "0");
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      f_print (fout, "sizeof ( ");
Packit 6c4009
      /* XXX: should "follow" be 1 ??? */
Packit 6c4009
      ptype (prefix, type, 0);
Packit 6c4009
      f_print (fout, ")");
Packit 6c4009
    }
Packit 6c4009
  f_print (fout, ",\n");
Packit 6c4009
}