Blame sunrpc/rpc_util.c

Packit 6c4009
/*
Packit 6c4009
 * From: @(#)rpc_util.c 1.11 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_util.c, Utility routines for the RPC protocol compiler
Packit 6c4009
 */
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <ctype.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include "rpc_scan.h"
Packit 6c4009
#include "rpc_parse.h"
Packit 6c4009
#include "rpc_util.h"
Packit 6c4009
#include "proto.h"
Packit 6c4009
Packit 6c4009
#define ARGEXT "argument"
Packit 6c4009
Packit 6c4009
char curline[MAXLINESIZE];	/* current read line */
Packit 6c4009
const char *where = curline;	/* current point in line */
Packit 6c4009
int linenum = 0;		/* current line number */
Packit 6c4009
Packit 6c4009
const char *infilename;		/* input filename */
Packit 6c4009
Packit 6c4009
#define NFILES 7
Packit 6c4009
const char *outfiles[NFILES];	/* output file names */
Packit 6c4009
int nfiles;
Packit 6c4009
Packit 6c4009
FILE *fout;			/* file pointer of current output */
Packit 6c4009
FILE *fin;			/* file pointer of current input */
Packit 6c4009
Packit 6c4009
list *defined;			/* list of defined things */
Packit 6c4009
Packit 6c4009
static int findit (const definition * def, const char *type);
Packit 6c4009
static const char *fixit (const char *type, const char *orig);
Packit 6c4009
static int typedefed (const definition * def, const char *type);
Packit 6c4009
static const char *toktostr (tok_kind kind);
Packit 6c4009
static void printbuf (void);
Packit 6c4009
static void printwhere (void);
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Reinitialize the world
Packit 6c4009
 */
Packit 6c4009
void
Packit 6c4009
reinitialize (void)
Packit 6c4009
{
Packit 6c4009
  memset (curline, 0, MAXLINESIZE);
Packit 6c4009
  where = curline;
Packit 6c4009
  linenum = 0;
Packit 6c4009
  defined = NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * string equality
Packit 6c4009
 */
Packit 6c4009
int
Packit 6c4009
streq (const char *a, const char *b)
Packit 6c4009
{
Packit 6c4009
  return strcmp (a, b) == 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * find a value in a list
Packit 6c4009
 */
Packit 6c4009
definition *
Packit 6c4009
findval (list *lst, const char *val,
Packit 6c4009
	 int (*cmp) (const definition *, const char *))
Packit 6c4009
{
Packit 6c4009
Packit 6c4009
  for (; lst != NULL; lst = lst->next)
Packit 6c4009
    {
Packit 6c4009
      if (cmp (lst->val, val))
Packit 6c4009
	{
Packit 6c4009
	  return lst->val;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * store a value in a list
Packit 6c4009
 */
Packit 6c4009
void
Packit 6c4009
storeval (list **lstp, definition *val)
Packit 6c4009
{
Packit 6c4009
  list **l;
Packit 6c4009
  list *lst;
Packit 6c4009
Packit 6c4009
Packit 6c4009
  for (l = lstp; *l != NULL; l = (list **) & (*l)->next);
Packit 6c4009
  lst = ALLOC (list);
Packit 6c4009
  lst->val = val;
Packit 6c4009
  lst->next = NULL;
Packit 6c4009
  *l = lst;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
findit (const definition * def, const char *type)
Packit 6c4009
{
Packit 6c4009
  return streq (def->def_name, type);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static const char *
Packit 6c4009
fixit (const char *type, const char *orig)
Packit 6c4009
{
Packit 6c4009
  definition *def;
Packit 6c4009
Packit 6c4009
  def = findval (defined, type, findit);
Packit 6c4009
  if (def == NULL || def->def_kind != DEF_TYPEDEF)
Packit 6c4009
    {
Packit 6c4009
      return orig;
Packit 6c4009
    }
Packit 6c4009
  switch (def->def.ty.rel)
Packit 6c4009
    {
Packit 6c4009
    case REL_VECTOR:
Packit 6c4009
      if (streq (def->def.ty.old_type, "opaque"))
Packit 6c4009
	return ("char");
Packit 6c4009
      else
Packit 6c4009
	return (def->def.ty.old_type);
Packit 6c4009
    case REL_ALIAS:
Packit 6c4009
      return (fixit (def->def.ty.old_type, orig));
Packit 6c4009
    default:
Packit 6c4009
      return orig;
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
const char *
Packit 6c4009
fixtype (const char *type)
Packit 6c4009
{
Packit 6c4009
  return fixit (type, type);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
const char *
Packit 6c4009
stringfix (const char *type)
Packit 6c4009
{
Packit 6c4009
  if (streq (type, "string"))
Packit 6c4009
    {
Packit 6c4009
      return "wrapstring";
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      return type;
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
ptype (const char *prefix, const char *type, int follow)
Packit 6c4009
{
Packit 6c4009
  if (prefix != NULL)
Packit 6c4009
    {
Packit 6c4009
      if (streq (prefix, "enum"))
Packit 6c4009
	{
Packit 6c4009
	  f_print (fout, "enum ");
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  f_print (fout, "struct ");
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  if (streq (type, "bool"))
Packit 6c4009
    {
Packit 6c4009
      f_print (fout, "bool_t ");
Packit 6c4009
    }
Packit 6c4009
  else if (streq (type, "string"))
Packit 6c4009
    {
Packit 6c4009
      f_print (fout, "char *");
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      f_print (fout, "%s ", follow ? fixtype (type) : type);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
typedefed (const definition * def, const char *type)
Packit 6c4009
{
Packit 6c4009
  if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL)
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
int
Packit 6c4009
isvectordef (const char *type, relation rel)
Packit 6c4009
{
Packit 6c4009
  definition *def;
Packit 6c4009
Packit 6c4009
  for (;;)
Packit 6c4009
    {
Packit 6c4009
      switch (rel)
Packit 6c4009
	{
Packit 6c4009
	case REL_VECTOR:
Packit 6c4009
	  return !streq (type, "string");
Packit 6c4009
	case REL_ARRAY:
Packit 6c4009
	  return 0;
Packit 6c4009
	case REL_POINTER:
Packit 6c4009
	  return 0;
Packit 6c4009
	case REL_ALIAS:
Packit 6c4009
	  def = findval (defined, type, typedefed);
Packit 6c4009
	  if (def == NULL)
Packit 6c4009
	    {
Packit 6c4009
	      return 0;
Packit 6c4009
	    }
Packit 6c4009
	  type = def->def.ty.old_type;
Packit 6c4009
	  rel = def->def.ty.rel;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
char *
Packit 6c4009
locase (const char *str)
Packit 6c4009
{
Packit 6c4009
  char c;
Packit 6c4009
  static char buf[100];
Packit 6c4009
  char *p = buf;
Packit 6c4009
Packit 6c4009
  while ((c = *str++) != 0)
Packit 6c4009
    {
Packit 6c4009
      *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
Packit 6c4009
    }
Packit 6c4009
  *p = 0;
Packit 6c4009
  return buf;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
pvname_svc (const char *pname, const char *vnum)
Packit 6c4009
{
Packit 6c4009
  f_print (fout, "%s_%s_svc", locase (pname), vnum);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
pvname (const char *pname, const char *vnum)
Packit 6c4009
{
Packit 6c4009
  f_print (fout, "%s_%s", locase (pname), vnum);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * print a useful (?) error message, and then die
Packit 6c4009
 */
Packit 6c4009
void
Packit 6c4009
error (const char *msg)
Packit 6c4009
{
Packit 6c4009
  printwhere ();
Packit 6c4009
  f_print (stderr, "%s, line %d: ", infilename, linenum);
Packit 6c4009
  f_print (stderr, "%s\n", msg);
Packit 6c4009
  crash ();
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Something went wrong, unlink any files that we may have created and then
Packit 6c4009
 * die.
Packit 6c4009
 */
Packit 6c4009
void
Packit 6c4009
crash (void)
Packit 6c4009
{
Packit 6c4009
  int i;
Packit 6c4009
Packit 6c4009
  for (i = 0; i < nfiles; i++)
Packit 6c4009
    {
Packit 6c4009
      unlink (outfiles[i]);
Packit 6c4009
    }
Packit 6c4009
  exit (1);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
record_open (const char *file)
Packit 6c4009
{
Packit 6c4009
  if (nfiles < NFILES)
Packit 6c4009
    {
Packit 6c4009
      outfiles[nfiles++] = file;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      f_print (stderr, "too many files!\n");
Packit 6c4009
      crash ();
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static char expectbuf[100];
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * error, token encountered was not the expected one
Packit 6c4009
 */
Packit 6c4009
void
Packit 6c4009
expected1 (tok_kind exp1)
Packit 6c4009
{
Packit 6c4009
  s_print (expectbuf, "expected '%s'",
Packit 6c4009
	   toktostr (exp1));
Packit 6c4009
  error (expectbuf);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * error, token encountered was not one of two expected ones
Packit 6c4009
 */
Packit 6c4009
void
Packit 6c4009
expected2 (tok_kind exp1, tok_kind exp2)
Packit 6c4009
{
Packit 6c4009
  s_print (expectbuf, "expected '%s' or '%s'",
Packit 6c4009
	   toktostr (exp1),
Packit 6c4009
	   toktostr (exp2));
Packit 6c4009
  error (expectbuf);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * error, token encountered was not one of 3 expected ones
Packit 6c4009
 */
Packit 6c4009
void
Packit 6c4009
expected3 (tok_kind exp1, tok_kind exp2, tok_kind exp3)
Packit 6c4009
{
Packit 6c4009
  s_print (expectbuf, "expected '%s', '%s' or '%s'",
Packit 6c4009
	   toktostr (exp1),
Packit 6c4009
	   toktostr (exp2),
Packit 6c4009
	   toktostr (exp3));
Packit 6c4009
  error (expectbuf);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
tabify (FILE * f, int tab)
Packit 6c4009
{
Packit 6c4009
  while (tab--)
Packit 6c4009
    {
Packit 6c4009
      (void) fputc ('\t', f);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static const token tokstrings[] =
Packit 6c4009
{
Packit 6c4009
  {TOK_IDENT, "identifier"},
Packit 6c4009
  {TOK_CONST, "const"},
Packit 6c4009
  {TOK_RPAREN, ")"},
Packit 6c4009
  {TOK_LPAREN, "("},
Packit 6c4009
  {TOK_RBRACE, "}"},
Packit 6c4009
  {TOK_LBRACE, "{"},
Packit 6c4009
  {TOK_LBRACKET, "["},
Packit 6c4009
  {TOK_RBRACKET, "]"},
Packit 6c4009
  {TOK_STAR, "*"},
Packit 6c4009
  {TOK_COMMA, ","},
Packit 6c4009
  {TOK_EQUAL, "="},
Packit 6c4009
  {TOK_COLON, ":"},
Packit 6c4009
  {TOK_SEMICOLON, ";"},
Packit 6c4009
  {TOK_UNION, "union"},
Packit 6c4009
  {TOK_STRUCT, "struct"},
Packit 6c4009
  {TOK_SWITCH, "switch"},
Packit 6c4009
  {TOK_CASE, "case"},
Packit 6c4009
  {TOK_DEFAULT, "default"},
Packit 6c4009
  {TOK_ENUM, "enum"},
Packit 6c4009
  {TOK_TYPEDEF, "typedef"},
Packit 6c4009
  {TOK_INT, "int"},
Packit 6c4009
  {TOK_SHORT, "short"},
Packit 6c4009
  {TOK_LONG, "long"},
Packit 6c4009
  {TOK_UNSIGNED, "unsigned"},
Packit 6c4009
  {TOK_DOUBLE, "double"},
Packit 6c4009
  {TOK_FLOAT, "float"},
Packit 6c4009
  {TOK_CHAR, "char"},
Packit 6c4009
  {TOK_STRING, "string"},
Packit 6c4009
  {TOK_OPAQUE, "opaque"},
Packit 6c4009
  {TOK_BOOL, "bool"},
Packit 6c4009
  {TOK_VOID, "void"},
Packit 6c4009
  {TOK_PROGRAM, "program"},
Packit 6c4009
  {TOK_VERSION, "version"},
Packit 6c4009
  {TOK_EOF, "??????"}
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
static const char *
Packit 6c4009
toktostr (tok_kind kind)
Packit 6c4009
{
Packit 6c4009
  const token *sp;
Packit 6c4009
Packit 6c4009
  for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++);
Packit 6c4009
  return sp->str;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
printbuf (void)
Packit 6c4009
{
Packit 6c4009
  char c;
Packit 6c4009
  int i;
Packit 6c4009
  int cnt;
Packit 6c4009
Packit 6c4009
#define TABSIZE 4
Packit 6c4009
Packit 6c4009
  for (i = 0; (c = curline[i]) != 0; i++)
Packit 6c4009
    {
Packit 6c4009
      if (c == '\t')
Packit 6c4009
	{
Packit 6c4009
	  cnt = 8 - (i % TABSIZE);
Packit 6c4009
	  c = ' ';
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  cnt = 1;
Packit 6c4009
	}
Packit 6c4009
      while (cnt--)
Packit 6c4009
	{
Packit 6c4009
	  (void) fputc (c, stderr);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
printwhere (void)
Packit 6c4009
{
Packit 6c4009
  int i;
Packit 6c4009
  char c;
Packit 6c4009
  int cnt;
Packit 6c4009
Packit 6c4009
  printbuf ();
Packit 6c4009
  for (i = 0; i < where - curline; i++)
Packit 6c4009
    {
Packit 6c4009
      c = curline[i];
Packit 6c4009
      if (c == '\t')
Packit 6c4009
	{
Packit 6c4009
	  cnt = 8 - (i % TABSIZE);
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  cnt = 1;
Packit 6c4009
	}
Packit 6c4009
      while (cnt--)
Packit 6c4009
	{
Packit 6c4009
	  (void) fputc ('^', stderr);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  (void) fputc ('\n', stderr);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
char *
Packit 6c4009
make_argname (const char *pname, const char *vname)
Packit 6c4009
{
Packit 6c4009
  char *name;
Packit 6c4009
Packit 6c4009
  name = malloc (strlen (pname) + strlen (vname) + strlen (ARGEXT) + 3);
Packit 6c4009
  if (!name)
Packit 6c4009
    {
Packit 6c4009
      fprintf (stderr, "failed in malloc");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  sprintf (name, "%s_%s_%s", locase (pname), vname, ARGEXT);
Packit 6c4009
  return name;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
bas_type *typ_list_h;
Packit 6c4009
bas_type *typ_list_t;
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
add_type (int len, const char *type)
Packit 6c4009
{
Packit 6c4009
  bas_type *ptr;
Packit 6c4009
Packit 6c4009
Packit 6c4009
  if ((ptr = malloc (sizeof (bas_type))) == NULL)
Packit 6c4009
    {
Packit 6c4009
      fprintf (stderr, "failed in malloc");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  ptr->name = type;
Packit 6c4009
  ptr->length = len;
Packit 6c4009
  ptr->next = NULL;
Packit 6c4009
  if (typ_list_t == NULL)
Packit 6c4009
    {
Packit 6c4009
Packit 6c4009
      typ_list_t = ptr;
Packit 6c4009
      typ_list_h = ptr;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
Packit 6c4009
      typ_list_t->next = ptr;
Packit 6c4009
      typ_list_t = ptr;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
bas_type *
Packit 6c4009
find_type (const char *type)
Packit 6c4009
{
Packit 6c4009
  bas_type *ptr;
Packit 6c4009
Packit 6c4009
  ptr = typ_list_h;
Packit 6c4009
Packit 6c4009
Packit 6c4009
  while (ptr != NULL)
Packit 6c4009
    {
Packit 6c4009
      if (strcmp (ptr->name, type) == 0)
Packit 6c4009
	return ptr;
Packit 6c4009
      else
Packit 6c4009
	ptr = ptr->next;
Packit 6c4009
    };
Packit 6c4009
  return NULL;
Packit 6c4009
}