Blame sunrpc/rpc_cout.c

Packit 6c4009
/*
Packit 6c4009
 * From: @(#)rpc_cout.c 1.13 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_cout.c, XDR routine outputter for the RPC protocol compiler
Packit 6c4009
 */
Packit 6c4009
#include <ctype.h>
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
static void emit_enum (definition * def);
Packit 6c4009
static void emit_program (const definition * def);
Packit 6c4009
static void emit_union (const definition * def);
Packit 6c4009
static void emit_struct (definition * def);
Packit 6c4009
static void emit_typedef (const definition * def);
Packit 6c4009
static void emit_inline (int indent, declaration * decl, int flag);
Packit 6c4009
static void emit_single_in_line (int indent, declaration *decl, int flag,
Packit 6c4009
				 relation rel);
Packit 6c4009
static int findtype (const definition * def, const char *type);
Packit 6c4009
static int undefined (const char *type);
Packit 6c4009
static void print_generic_header (const char *procname, int pointerp);
Packit 6c4009
static void print_ifopen (int indent, const char *name);
Packit 6c4009
static void print_ifarg (const char *arg);
Packit 6c4009
static void print_ifsizeof (int indent, const char *prefix, const char *type);
Packit 6c4009
static void print_ifclose (int indent);
Packit 6c4009
static void print_ifstat (int indent, const char *prefix, const char *type,
Packit 6c4009
			  relation rel, const char *amax,
Packit 6c4009
			  const char *objname, const char *name);
Packit 6c4009
static void print_stat (int indent, const declaration * dec);
Packit 6c4009
static void print_header (const definition * def);
Packit 6c4009
static void print_trailer (void);
Packit 6c4009
static char *upcase (const char *str);
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Emit the C-routine for the given definition
Packit 6c4009
 */
Packit 6c4009
void
Packit 6c4009
emit (definition * def)
Packit 6c4009
{
Packit 6c4009
  if (def->def_kind == DEF_CONST)
Packit 6c4009
    {
Packit 6c4009
      return;
Packit 6c4009
    }
Packit 6c4009
  if (def->def_kind == DEF_PROGRAM)
Packit 6c4009
    {
Packit 6c4009
      emit_program (def);
Packit 6c4009
      return;
Packit 6c4009
    }
Packit 6c4009
  if (def->def_kind == DEF_TYPEDEF)
Packit 6c4009
    {
Packit 6c4009
      /* now we need to handle declarations like
Packit 6c4009
	 struct typedef foo foo;
Packit 6c4009
	 since we don't want this to be expanded
Packit 6c4009
	 into 2 calls to xdr_foo */
Packit 6c4009
Packit 6c4009
      if (strcmp (def->def.ty.old_type, def->def_name) == 0)
Packit 6c4009
	return;
Packit 6c4009
    };
Packit 6c4009
Packit 6c4009
  print_header (def);
Packit 6c4009
  switch (def->def_kind)
Packit 6c4009
    {
Packit 6c4009
    case DEF_UNION:
Packit 6c4009
      emit_union (def);
Packit 6c4009
      break;
Packit 6c4009
    case DEF_ENUM:
Packit 6c4009
      emit_enum (def);
Packit 6c4009
      break;
Packit 6c4009
    case DEF_STRUCT:
Packit 6c4009
      emit_struct (def);
Packit 6c4009
      break;
Packit 6c4009
    case DEF_TYPEDEF:
Packit 6c4009
      emit_typedef (def);
Packit 6c4009
      break;
Packit 6c4009
    default:
Packit 6c4009
      /* can't happen */
Packit 6c4009
      break;
Packit 6c4009
    }
Packit 6c4009
  print_trailer ();
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
findtype (const definition * def, const char *type)
Packit 6c4009
{
Packit 6c4009
  if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST)
Packit 6c4009
    {
Packit 6c4009
      return 0;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      return (streq (def->def_name, type));
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
undefined (const char *type)
Packit 6c4009
{
Packit 6c4009
  definition *def;
Packit 6c4009
  def = (definition *) FINDVAL (defined, type, findtype);
Packit 6c4009
  return (def == NULL);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
print_generic_header (const char *procname, int pointerp)
Packit 6c4009
{
Packit 6c4009
  f_print (fout, "\n");
Packit 6c4009
  f_print (fout, "bool_t\n");
Packit 6c4009
  if (Cflag)
Packit 6c4009
    {
Packit 6c4009
      f_print (fout, "xdr_%s (", procname);
Packit 6c4009
      f_print (fout, "XDR *xdrs, ");
Packit 6c4009
      f_print (fout, "%s ", procname);
Packit 6c4009
      if (pointerp)
Packit 6c4009
	f_print (fout, "*");
Packit 6c4009
      f_print (fout, "objp)\n{\n");
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      f_print (fout, "xdr_%s (xdrs, objp)\n", procname);
Packit 6c4009
      f_print (fout, "\tXDR *xdrs;\n");
Packit 6c4009
      f_print (fout, "\t%s ", procname);
Packit 6c4009
      if (pointerp)
Packit 6c4009
	f_print (fout, "*");
Packit 6c4009
      f_print (fout, "objp;\n{\n");
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
print_header (const definition * def)
Packit 6c4009
{
Packit 6c4009
  print_generic_header (def->def_name,
Packit 6c4009
			def->def_kind != DEF_TYPEDEF ||
Packit 6c4009
			!isvectordef (def->def.ty.old_type,
Packit 6c4009
				      def->def.ty.rel));
Packit 6c4009
Packit 6c4009
  /* Now add Inline support */
Packit 6c4009
Packit 6c4009
  if (inlineflag == 0)
Packit 6c4009
    return;
Packit 6c4009
  /*May cause lint to complain. but  ... */
Packit 6c4009
  f_print (fout, "\tregister int32_t *buf;\n\n");
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
print_prog_header (const proc_list * plist)
Packit 6c4009
{
Packit 6c4009
  print_generic_header (plist->args.argname, 1);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
print_trailer (void)
Packit 6c4009
{
Packit 6c4009
  f_print (fout, "\treturn TRUE;\n");
Packit 6c4009
  f_print (fout, "}\n");
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
print_ifopen (int indent, const char *name)
Packit 6c4009
{
Packit 6c4009
  tabify (fout, indent);
Packit 6c4009
  f_print (fout, " if (!xdr_%s (xdrs", name);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
print_ifarg (const char *arg)
Packit 6c4009
{
Packit 6c4009
  f_print (fout, ", %s", arg);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
print_ifsizeof (int indent, const char *prefix, const char *type)
Packit 6c4009
{
Packit 6c4009
  if (indent)
Packit 6c4009
    {
Packit 6c4009
      fprintf (fout, ",\n");
Packit 6c4009
      tabify (fout, indent);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    fprintf (fout, ", ");
Packit 6c4009
Packit 6c4009
  if (streq (type, "bool"))
Packit 6c4009
    fprintf (fout, "sizeof (bool_t), (xdrproc_t) xdr_bool");
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      fprintf (fout, "sizeof (");
Packit 6c4009
      if (undefined (type) && prefix)
Packit 6c4009
	{
Packit 6c4009
	  f_print (fout, "%s ", prefix);
Packit 6c4009
	}
Packit 6c4009
      fprintf (fout, "%s), (xdrproc_t) xdr_%s", type, type);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
print_ifclose (int indent)
Packit 6c4009
{
Packit 6c4009
  f_print (fout, "))\n");
Packit 6c4009
  tabify (fout, indent);
Packit 6c4009
  f_print (fout, "\t return FALSE;\n");
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
print_ifstat (int indent, const char *prefix, const char *type, relation rel,
Packit 6c4009
	      const char *amax, const char *objname, const char *name)
Packit 6c4009
{
Packit 6c4009
  const char *alt = NULL;
Packit 6c4009
Packit 6c4009
  switch (rel)
Packit 6c4009
    {
Packit 6c4009
    case REL_POINTER:
Packit 6c4009
      print_ifopen (indent, "pointer");
Packit 6c4009
      print_ifarg ("(char **)");
Packit 6c4009
      f_print (fout, "%s", objname);
Packit 6c4009
      print_ifsizeof (0, prefix, type);
Packit 6c4009
      break;
Packit 6c4009
    case REL_VECTOR:
Packit 6c4009
      if (streq (type, "string"))
Packit 6c4009
	{
Packit 6c4009
	  alt = "string";
Packit 6c4009
	}
Packit 6c4009
      else if (streq (type, "opaque"))
Packit 6c4009
	{
Packit 6c4009
	  alt = "opaque";
Packit 6c4009
	}
Packit 6c4009
      if (alt)
Packit 6c4009
	{
Packit 6c4009
	  print_ifopen (indent, alt);
Packit 6c4009
	  print_ifarg (objname);
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  print_ifopen (indent, "vector");
Packit 6c4009
	  print_ifarg ("(char *)");
Packit 6c4009
	  f_print (fout, "%s", objname);
Packit 6c4009
	}
Packit 6c4009
      print_ifarg (amax);
Packit 6c4009
      if (!alt)
Packit 6c4009
	{
Packit 6c4009
	  print_ifsizeof (indent + 1, prefix, type);
Packit 6c4009
	}
Packit 6c4009
      break;
Packit 6c4009
    case REL_ARRAY:
Packit 6c4009
      if (streq (type, "string"))
Packit 6c4009
	{
Packit 6c4009
	  alt = "string";
Packit 6c4009
	}
Packit 6c4009
      else if (streq (type, "opaque"))
Packit 6c4009
	{
Packit 6c4009
	  alt = "bytes";
Packit 6c4009
	}
Packit 6c4009
      if (streq (type, "string"))
Packit 6c4009
	{
Packit 6c4009
	  print_ifopen (indent, alt);
Packit 6c4009
	  print_ifarg (objname);
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  if (alt)
Packit 6c4009
	    {
Packit 6c4009
	      print_ifopen (indent, alt);
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    {
Packit 6c4009
	      print_ifopen (indent, "array");
Packit 6c4009
	    }
Packit 6c4009
	  print_ifarg ("(char **)");
Packit 6c4009
	  if (*objname == '&')
Packit 6c4009
	    {
Packit 6c4009
	      f_print (fout, "%s.%s_val, (u_int *) %s.%s_len",
Packit 6c4009
		       objname, name, objname, name);
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    {
Packit 6c4009
	      f_print (fout, "&%s->%s_val, (u_int *) &%s->%s_len",
Packit 6c4009
		       objname, name, objname, name);
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      print_ifarg (amax);
Packit 6c4009
      if (!alt)
Packit 6c4009
	{
Packit 6c4009
	  print_ifsizeof (indent + 1, prefix, type);
Packit 6c4009
	}
Packit 6c4009
      break;
Packit 6c4009
    case REL_ALIAS:
Packit 6c4009
      print_ifopen (indent, type);
Packit 6c4009
      print_ifarg (objname);
Packit 6c4009
      break;
Packit 6c4009
    }
Packit 6c4009
  print_ifclose (indent);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
emit_enum (definition * def)
Packit 6c4009
{
Packit 6c4009
  (void) def;
Packit 6c4009
Packit 6c4009
  print_ifopen (1, "enum");
Packit 6c4009
  print_ifarg ("(enum_t *) objp");
Packit 6c4009
  print_ifclose (1);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
emit_program (const definition * def)
Packit 6c4009
{
Packit 6c4009
  decl_list *dl;
Packit 6c4009
  version_list *vlist;
Packit 6c4009
  proc_list *plist;
Packit 6c4009
Packit 6c4009
  for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next)
Packit 6c4009
    for (plist = vlist->procs; plist != NULL; plist = plist->next)
Packit 6c4009
      {
Packit 6c4009
	if (!newstyle || plist->arg_num < 2)
Packit 6c4009
	  continue;		/* old style, or single argument */
Packit 6c4009
	print_prog_header (plist);
Packit 6c4009
	for (dl = plist->args.decls; dl != NULL;
Packit 6c4009
	     dl = dl->next)
Packit 6c4009
	  print_stat (1, &dl->decl);
Packit 6c4009
	print_trailer ();
Packit 6c4009
      }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
emit_union (const definition * def)
Packit 6c4009
{
Packit 6c4009
  declaration *dflt;
Packit 6c4009
  case_list *cl;
Packit 6c4009
  declaration *cs;
Packit 6c4009
  char *object;
Packit 6c4009
  const char *vecformat = "objp->%s_u.%s";
Packit 6c4009
  const char *format = "&objp->%s_u.%s";
Packit 6c4009
Packit 6c4009
  print_stat (1, &def->def.un.enum_decl);
Packit 6c4009
  f_print (fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
Packit 6c4009
  for (cl = def->def.un.cases; cl != NULL; cl = cl->next)
Packit 6c4009
    {
Packit 6c4009
Packit 6c4009
      f_print (fout, "\tcase %s:\n", cl->case_name);
Packit 6c4009
      if (cl->contflag == 1)	/* a continued case statement */
Packit 6c4009
	continue;
Packit 6c4009
      cs = &cl->case_decl;
Packit 6c4009
      if (!streq (cs->type, "void"))
Packit 6c4009
	{
Packit 6c4009
	  object = alloc (strlen (def->def_name) + strlen (format) +
Packit 6c4009
			  strlen (cs->name) + 1);
Packit 6c4009
	  if (isvectordef (cs->type, cs->rel))
Packit 6c4009
	    {
Packit 6c4009
	      s_print (object, vecformat, def->def_name,
Packit 6c4009
		       cs->name);
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    {
Packit 6c4009
	      s_print (object, format, def->def_name,
Packit 6c4009
		       cs->name);
Packit 6c4009
	    }
Packit 6c4009
	  print_ifstat (2, cs->prefix, cs->type, cs->rel, cs->array_max,
Packit 6c4009
			object, cs->name);
Packit 6c4009
	  free (object);
Packit 6c4009
	}
Packit 6c4009
      f_print (fout, "\t\tbreak;\n");
Packit 6c4009
    }
Packit 6c4009
  dflt = def->def.un.default_decl;
Packit 6c4009
  if (dflt != NULL)
Packit 6c4009
    {
Packit 6c4009
      if (!streq (dflt->type, "void"))
Packit 6c4009
	{
Packit 6c4009
	  f_print (fout, "\tdefault:\n");
Packit 6c4009
	  object = alloc (strlen (def->def_name) + strlen (format) +
Packit 6c4009
			  strlen (dflt->name) + 1);
Packit 6c4009
	  if (isvectordef (dflt->type, dflt->rel))
Packit 6c4009
	    {
Packit 6c4009
	      s_print (object, vecformat, def->def_name,
Packit 6c4009
		       dflt->name);
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    {
Packit 6c4009
	      s_print (object, format, def->def_name,
Packit 6c4009
		       dflt->name);
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  print_ifstat (2, dflt->prefix, dflt->type, dflt->rel,
Packit 6c4009
			dflt->array_max, object, dflt->name);
Packit 6c4009
	  free (object);
Packit 6c4009
	  f_print (fout, "\t\tbreak;\n");
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  f_print (fout, "\tdefault:\n");
Packit 6c4009
	  f_print (fout, "\t\tbreak;\n");
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      f_print (fout, "\tdefault:\n");
Packit 6c4009
      f_print (fout, "\t\treturn FALSE;\n");
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  f_print (fout, "\t}\n");
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
inline_struct (definition *def, int flag)
Packit 6c4009
{
Packit 6c4009
  decl_list *dl;
Packit 6c4009
  int i, size;
Packit 6c4009
  decl_list *cur = NULL;
Packit 6c4009
  decl_list *psav;
Packit 6c4009
  bas_type *ptr;
Packit 6c4009
  char *sizestr;
Packit 6c4009
  const char *plus;
Packit 6c4009
  char ptemp[256];
Packit 6c4009
  int indent = 1;
Packit 6c4009
Packit 6c4009
  if (flag == PUT)
Packit 6c4009
    f_print (fout, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
Packit 6c4009
  else
Packit 6c4009
    f_print (fout,
Packit 6c4009
	     "\t\treturn TRUE;\n\t} else if (xdrs->x_op == XDR_DECODE) {\n");
Packit 6c4009
Packit 6c4009
  i = 0;
Packit 6c4009
  size = 0;
Packit 6c4009
  sizestr = NULL;
Packit 6c4009
  for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
Packit 6c4009
    {			/* xxx */
Packit 6c4009
      /* now walk down the list and check for basic types */
Packit 6c4009
      if ((dl->decl.prefix == NULL) &&
Packit 6c4009
	  ((ptr = find_type (dl->decl.type)) != NULL) &&
Packit 6c4009
	  ((dl->decl.rel == REL_ALIAS) || (dl->decl.rel == REL_VECTOR)))
Packit 6c4009
	{
Packit 6c4009
	  if (i == 0)
Packit 6c4009
	    cur = dl;
Packit 6c4009
	  ++i;
Packit 6c4009
Packit 6c4009
	  if (dl->decl.rel == REL_ALIAS)
Packit 6c4009
	    size += ptr->length;
Packit 6c4009
	  else
Packit 6c4009
	    {
Packit 6c4009
	      /* this is required to handle arrays */
Packit 6c4009
	      if (sizestr == NULL)
Packit 6c4009
		plus = "";
Packit 6c4009
	      else
Packit 6c4009
		plus = "+ ";
Packit 6c4009
Packit 6c4009
	      if (ptr->length != 1)
Packit 6c4009
		s_print (ptemp, " %s %s * %d", plus, dl->decl.array_max,
Packit 6c4009
			 ptr->length);
Packit 6c4009
	      else
Packit 6c4009
		s_print (ptemp, " %s%s ", plus, dl->decl.array_max);
Packit 6c4009
Packit 6c4009
	      /*now concatenate to sizestr !!!! */
Packit 6c4009
	      if (sizestr == NULL)
Packit 6c4009
		sizestr = strdup (ptemp);
Packit 6c4009
	      else
Packit 6c4009
		{
Packit 6c4009
		  sizestr = realloc (sizestr, strlen (sizestr) +
Packit 6c4009
				     strlen (ptemp) + 1);
Packit 6c4009
		  if (sizestr == NULL)
Packit 6c4009
		    {
Packit 6c4009
		      f_print (stderr, "Fatal error : no memory \n");
Packit 6c4009
		      crash ();
Packit 6c4009
		    };
Packit 6c4009
		  sizestr = strcat (sizestr, ptemp);
Packit 6c4009
		  /*build up length of array */
Packit 6c4009
		}
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  if (i > 0)
Packit 6c4009
	    {
Packit 6c4009
	      if (sizestr == NULL && size < inlineflag)
Packit 6c4009
		{
Packit 6c4009
		  /* don't expand into inline code if size < inlineflag */
Packit 6c4009
		  while (cur != dl)
Packit 6c4009
		    {
Packit 6c4009
		      print_stat (indent + 1, &cur->decl);
Packit 6c4009
		      cur = cur->next;
Packit 6c4009
		    }
Packit 6c4009
		}
Packit 6c4009
	      else
Packit 6c4009
		{
Packit 6c4009
		  /* were already looking at a xdr_inlineable structure */
Packit 6c4009
		  tabify (fout, indent + 1);
Packit 6c4009
		  if (sizestr == NULL)
Packit 6c4009
		    f_print (fout, "buf = XDR_INLINE (xdrs, %d * BYTES_PER_XDR_UNIT);", size);
Packit 6c4009
		  else if (size == 0)
Packit 6c4009
		    f_print (fout,
Packit 6c4009
			     "buf = XDR_INLINE (xdrs, (%s) * BYTES_PER_XDR_UNIT);",
Packit 6c4009
			     sizestr);
Packit 6c4009
		  else
Packit 6c4009
		    f_print (fout,
Packit 6c4009
			     "buf = XDR_INLINE (xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
Packit 6c4009
			     size, sizestr);
Packit 6c4009
		  f_print (fout, "\n");
Packit 6c4009
		  tabify (fout, indent + 1);
Packit 6c4009
		  fprintf (fout, "if (buf == NULL) {\n");
Packit 6c4009
		  psav = cur;
Packit 6c4009
		  while (cur != dl)
Packit 6c4009
		    {
Packit 6c4009
		      print_stat (indent + 2, &cur->decl);
Packit 6c4009
		      cur = cur->next;
Packit 6c4009
		    }
Packit 6c4009
Packit 6c4009
		  f_print (fout, "\n\t\t} else {\n");
Packit 6c4009
		  cur = psav;
Packit 6c4009
		  while (cur != dl)
Packit 6c4009
		    {
Packit 6c4009
		      emit_inline (indent + 1, &cur->decl, flag);
Packit 6c4009
		      cur = cur->next;
Packit 6c4009
		    }
Packit 6c4009
		  tabify (fout, indent + 1);
Packit 6c4009
		  f_print (fout, "}\n");
Packit 6c4009
		}
Packit 6c4009
	    }
Packit 6c4009
	  size = 0;
Packit 6c4009
	  i = 0;
Packit 6c4009
	  free (sizestr);
Packit 6c4009
	  sizestr = NULL;
Packit 6c4009
	  print_stat (indent + 1, &dl->decl);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  if (i > 0)
Packit 6c4009
    {
Packit 6c4009
      if (sizestr == NULL && size < inlineflag)
Packit 6c4009
	{
Packit 6c4009
	  /* don't expand into inline code if size < inlineflag */
Packit 6c4009
	  while (cur != dl)
Packit 6c4009
	    {
Packit 6c4009
	      print_stat (indent + 1, &cur->decl);
Packit 6c4009
	      cur = cur->next;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  /* were already looking at a xdr_inlineable structure */
Packit 6c4009
	  if (sizestr == NULL)
Packit 6c4009
	    f_print (fout,
Packit 6c4009
		     "\t\tbuf = XDR_INLINE (xdrs, %d * BYTES_PER_XDR_UNIT);",
Packit 6c4009
		     size);
Packit 6c4009
	  else if (size == 0)
Packit 6c4009
	    f_print (fout,
Packit 6c4009
		     "\t\tbuf = XDR_INLINE (xdrs, (%s) * BYTES_PER_XDR_UNIT);",
Packit 6c4009
		     sizestr);
Packit 6c4009
	  else
Packit 6c4009
	    f_print (fout,
Packit 6c4009
		     "\t\tbuf = XDR_INLINE (xdrs, (%d + %s)* BYTES_PER_XDR_UNIT);",
Packit 6c4009
		     size, sizestr);
Packit 6c4009
	  f_print (fout, "\n\t\tif (buf == NULL) {\n");
Packit 6c4009
	  psav = cur;
Packit 6c4009
	  while (cur != NULL)
Packit 6c4009
	    {
Packit 6c4009
	      print_stat (indent + 2, &cur->decl);
Packit 6c4009
	      cur = cur->next;
Packit 6c4009
	    }
Packit 6c4009
	  f_print (fout, "\t\t} else {\n");
Packit 6c4009
Packit 6c4009
	  cur = psav;
Packit 6c4009
	  while (cur != dl)
Packit 6c4009
	    {
Packit 6c4009
	      emit_inline (indent + 2, &cur->decl, flag);
Packit 6c4009
	      cur = cur->next;
Packit 6c4009
	    }
Packit 6c4009
	  f_print (fout, "\t\t}\n");
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* this may be const.  i haven't traced this one through yet. */
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
emit_struct (definition * def)
Packit 6c4009
{
Packit 6c4009
  decl_list *dl;
Packit 6c4009
  int j, size, flag;
Packit 6c4009
  bas_type *ptr;
Packit 6c4009
  int can_inline;
Packit 6c4009
Packit 6c4009
Packit 6c4009
  if (inlineflag == 0)
Packit 6c4009
    {
Packit 6c4009
      /* No xdr_inlining at all */
Packit 6c4009
      for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
Packit 6c4009
	print_stat (1, &dl->decl);
Packit 6c4009
      return;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
Packit 6c4009
    if (dl->decl.rel == REL_VECTOR)
Packit 6c4009
      {
Packit 6c4009
	f_print (fout, "\tint i;\n");
Packit 6c4009
	break;
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
  size = 0;
Packit 6c4009
  can_inline = 0;
Packit 6c4009
  /*
Packit 6c4009
   * Make a first pass and see if inling is possible.
Packit 6c4009
   */
Packit 6c4009
  for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
Packit 6c4009
    if ((dl->decl.prefix == NULL) &&
Packit 6c4009
	((ptr = find_type (dl->decl.type)) != NULL) &&
Packit 6c4009
	((dl->decl.rel == REL_ALIAS) || (dl->decl.rel == REL_VECTOR)))
Packit 6c4009
      {
Packit 6c4009
	if (dl->decl.rel == REL_ALIAS)
Packit 6c4009
	  size += ptr->length;
Packit 6c4009
	else
Packit 6c4009
	  {
Packit 6c4009
	    can_inline = 1;
Packit 6c4009
	    break;		/* can be inlined */
Packit 6c4009
	  }
Packit 6c4009
      }
Packit 6c4009
    else
Packit 6c4009
      {
Packit 6c4009
	if (size >= inlineflag)
Packit 6c4009
	  {
Packit 6c4009
	    can_inline = 1;
Packit 6c4009
	    break;		/* can be inlined */
Packit 6c4009
	  }
Packit 6c4009
	size = 0;
Packit 6c4009
      }
Packit 6c4009
  if (size > inlineflag)
Packit 6c4009
    can_inline = 1;
Packit 6c4009
Packit 6c4009
  if (can_inline == 0)
Packit 6c4009
    {			/* can not inline, drop back to old mode */
Packit 6c4009
      for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
Packit 6c4009
	print_stat (1, &dl->decl);
Packit 6c4009
      return;
Packit 6c4009
    };
Packit 6c4009
Packit 6c4009
  flag = PUT;
Packit 6c4009
  for (j = 0; j < 2; j++)
Packit 6c4009
    {
Packit 6c4009
      inline_struct (def, flag);
Packit 6c4009
      if (flag == PUT)
Packit 6c4009
	flag = GET;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  f_print (fout, "\t return TRUE;\n\t}\n\n");
Packit 6c4009
Packit 6c4009
  /* now take care of XDR_FREE case */
Packit 6c4009
Packit 6c4009
  for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
Packit 6c4009
    print_stat (1, &dl->decl);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
emit_typedef (const definition * def)
Packit 6c4009
{
Packit 6c4009
  const char *prefix = def->def.ty.old_prefix;
Packit 6c4009
  const char *type = def->def.ty.old_type;
Packit 6c4009
  const char *amax = def->def.ty.array_max;
Packit 6c4009
  relation rel = def->def.ty.rel;
Packit 6c4009
Packit 6c4009
  print_ifstat (1, prefix, type, rel, amax, "objp", def->def_name);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
print_stat (int indent, const declaration * dec)
Packit 6c4009
{
Packit 6c4009
  const char *prefix = dec->prefix;
Packit 6c4009
  const char *type = dec->type;
Packit 6c4009
  const char *amax = dec->array_max;
Packit 6c4009
  relation rel = dec->rel;
Packit 6c4009
  char name[256];
Packit 6c4009
Packit 6c4009
  if (isvectordef (type, rel))
Packit 6c4009
    {
Packit 6c4009
      s_print (name, "objp->%s", dec->name);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      s_print (name, "&objp->%s", dec->name);
Packit 6c4009
    }
Packit 6c4009
  print_ifstat (indent, prefix, type, rel, amax, name, dec->name);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
emit_inline (int indent, declaration * decl, int flag)
Packit 6c4009
{
Packit 6c4009
  switch (decl->rel)
Packit 6c4009
    {
Packit 6c4009
    case REL_ALIAS:
Packit 6c4009
      emit_single_in_line (indent, decl, flag, REL_ALIAS);
Packit 6c4009
      break;
Packit 6c4009
    case REL_VECTOR:
Packit 6c4009
      tabify (fout, indent);
Packit 6c4009
      f_print (fout, "{\n");
Packit 6c4009
      tabify (fout, indent + 1);
Packit 6c4009
      f_print (fout, "register %s *genp;\n\n", decl->type);
Packit 6c4009
      tabify (fout, indent + 1);
Packit 6c4009
      f_print (fout,
Packit 6c4009
	       "for (i = 0, genp = objp->%s;\n", decl->name);
Packit 6c4009
      tabify (fout, indent + 2);
Packit 6c4009
      f_print (fout, "i < %s; ++i) {\n", decl->array_max);
Packit 6c4009
      emit_single_in_line (indent + 2, decl, flag, REL_VECTOR);
Packit 6c4009
      tabify (fout, indent + 1);
Packit 6c4009
      f_print (fout, "}\n");
Packit 6c4009
      tabify (fout, indent);
Packit 6c4009
      f_print (fout, "}\n");
Packit 6c4009
      break;
Packit 6c4009
    default:
Packit 6c4009
      break;
Packit 6c4009
      /* ?... do nothing I guess */
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
emit_single_in_line (int indent, declaration *decl, int flag, relation rel)
Packit 6c4009
{
Packit 6c4009
  char *upp_case;
Packit 6c4009
  int freed = 0;
Packit 6c4009
Packit 6c4009
  tabify (fout, indent);
Packit 6c4009
  if (flag == PUT)
Packit 6c4009
    f_print (fout, "IXDR_PUT_");
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      if (rel == REL_ALIAS)
Packit 6c4009
	f_print (fout, "objp->%s = IXDR_GET_", decl->name);
Packit 6c4009
      else
Packit 6c4009
	f_print (fout, "*genp++ = IXDR_GET_");
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  upp_case = upcase (decl->type);
Packit 6c4009
Packit 6c4009
  /* hack  - XX */
Packit 6c4009
  if (!strcmp (upp_case, "INT"))
Packit 6c4009
    {
Packit 6c4009
      free (upp_case);
Packit 6c4009
      freed = 1;
Packit 6c4009
      /* Casting is safe since the `freed' flag is set.  */
Packit 6c4009
      upp_case = (char *) "LONG";
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (!strcmp (upp_case, "U_INT"))
Packit 6c4009
    {
Packit 6c4009
      free (upp_case);
Packit 6c4009
      freed = 1;
Packit 6c4009
      /* Casting is safe since the `freed' flag is set.  */
Packit 6c4009
      upp_case = (char *) "U_LONG";
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (flag == PUT)
Packit 6c4009
    {
Packit 6c4009
      if (rel == REL_ALIAS)
Packit 6c4009
	f_print (fout, "%s(buf, objp->%s);\n", upp_case, decl->name);
Packit 6c4009
      else
Packit 6c4009
	f_print (fout, "%s(buf, *genp++);\n", upp_case);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      f_print (fout, "%s(buf);\n", upp_case);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (!freed)
Packit 6c4009
    free (upp_case);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static char *
Packit 6c4009
upcase (const char *str)
Packit 6c4009
{
Packit 6c4009
  char *ptr, *hptr;
Packit 6c4009
  ptr = malloc (strlen (str) + 1);
Packit 6c4009
  if (ptr == NULL)
Packit 6c4009
    {
Packit 6c4009
      f_print (stderr, "malloc failed\n");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  hptr = ptr;
Packit 6c4009
  while (*str != '\0')
Packit 6c4009
    *ptr++ = toupper (*str++);
Packit 6c4009
Packit 6c4009
  *ptr = '\0';
Packit 6c4009
  return hptr;
Packit 6c4009
}