Ejemplo n.º 1
0
void
main(int argc, char **argv)
{
  /* build the command line options database */
  odb = opt_new(/* no orphan fn */NULL);
  opt_reg_flag(odb, "-h", "print help message",
               &help_me, /* default */FALSE, /* !print */FALSE, NULL);
  opt_reg_flag(odb, "-defs", "make internal defs",
               &make_defs, /* default */FALSE, /* print */TRUE, NULL);
  opt_reg_string(odb, "-load", "load an EXO file",
		 &load_file, /* default */NULL,
		 /* print */TRUE, /* format */NULL);
  opt_reg_string(odb, "-save", "save an EXO file",
		 &save_file, /* default */NULL,
		 /* print */TRUE, /* format */NULL);
  opt_reg_flag(odb, "-print", "print the EXO DB to stdout",
	       &print_db, /* default */FALSE,
	       /* print */TRUE, /* format */NULL);

  /* process the command line options */
  opt_process_options(odb, argc, argv);

  if (help_me)
    {
      /* print help message and exit */
      usage(stderr, argc, argv);
      exit(1);
    }

  /* print options used */
  opt_print_options(odb, stderr, /* short */TRUE, /* notes */TRUE);

  if (load_file)
    {
      ZFILE *exo_stream;
      struct exo_term_t *exo;

      exo_stream = myzfopen(load_file, "r");
      if (!exo_stream)
	fatal("could not open EXO file `%s'", load_file);

      while ((exo = exo_read(exo_stream->fd)) != NULL)
	exo_db = exo_chain(exo_db, exo);

      myzfclose(exo_stream);
    }

  if (make_defs)
    {
      struct exo_term_t *list, *array, *a, *b, *c, *d, *e, *f, *g, *h, *i;
      char *data = "This is a test to see if blobs really work...";
      char *data1 = "This is a test to see if blobs really work..."
	"This is a test to see if blobs really work..."
	  "This is a test to see if blobs really work..."
	    "This is a test to see if blobs really work..."
	      "This is a test to see if blobs really work..."
		"This is a test to see if blobs really work..."
		  "This is a test to see if blobs really work..."
		    "This is a test to see if blobs really work...";
      unsigned char data2[16] =
	{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };

      exo_db =
	exo_chain(exo_db, exo_new(ec_string, "** basic types tests **"));
      exo_db = exo_chain(exo_db, a = exo_new(ec_integer, (exo_integer_t)42));
      exo_db = exo_chain(b = exo_new(ec_float, (exo_float_t)42.0), exo_db);
      exo_db = exo_chain(exo_db, c = exo_new(ec_char, (int)'x'));
      exo_db = exo_chain(exo_db, exo_new(ec_char, (int)'\n'));
      exo_db = exo_chain(exo_db, exo_new(ec_char, (int)'\b'));
      exo_db = exo_chain(exo_db, exo_new(ec_char, (int)'\x02'));
      exo_db = exo_chain(exo_db, exo_new(ec_char, (int)'\xab'));
      exo_db = exo_chain(exo_db, exo_new(ec_string, "this is a test..."));
      exo_db =
	exo_chain(exo_db, d = exo_new(ec_string, "this is\na test...\n"));
      exo_db = exo_chain(exo_db, exo_new(ec_string, "a test... <<\\\b>>\n"));

      exo_db = exo_chain(exo_db, exo_new(ec_string, "** deep copy tests **"));
      exo_db = exo_chain(exo_db, exo_deepcopy(d));
      exo_db = exo_chain(exo_db, exo_deepcopy(c));
      exo_db = exo_chain(exo_db, exo_deepcopy(b));
      exo_db = exo_chain(exo_db, exo_deepcopy(a));

      exo_db = exo_chain(exo_db, exo_new(ec_string, "** list tests **"));
      exo_db =
	exo_chain(exo_db, exo_new(ec_list,
				  exo_deepcopy(d), exo_deepcopy(c),
				  exo_deepcopy(b), exo_deepcopy(a), NULL));
      exo_db = exo_chain(exo_db, exo_new(ec_list, NULL));
      exo_db =
	exo_chain(exo_db, exo_new(ec_list,
				  exo_new(ec_list, NULL),
				  exo_new(ec_list, NULL),
				  exo_new(ec_list, exo_deepcopy(a), NULL),
				  NULL));
      list = exo_deepcopy(a);
      list = exo_chain(list, exo_deepcopy(b));
      list = exo_chain(list, exo_deepcopy(a));
      list = exo_chain(list, exo_deepcopy(b));
      list = exo_chain(exo_deepcopy(c), list);
      exo_db = exo_chain(exo_db, e = exo_new(ec_list, list, NULL));
      exo_db = exo_chain(exo_db, exo_new(ec_list,
					 exo_deepcopy(e),
					 exo_new(ec_list, NULL),
					 exo_deepcopy(e),
					 exo_deepcopy(a), NULL));

      exo_db = exo_chain(exo_db, exo_new(ec_string, "** array tests **"));
      exo_db = exo_chain(exo_db, exo_new(ec_array, 16, NULL));
      f = array = exo_new(ec_array, 16, NULL);
      EXO_ARR(array, 2) = exo_deepcopy(e);
      EXO_ARR(array, 3) = exo_deepcopy(a);
      EXO_ARR(array, 4) = exo_deepcopy(c);
      EXO_ARR(array, 6) = exo_deepcopy(EXO_ARR(array, 2));
      EXO_ARR(array, 7) = exo_deepcopy(EXO_ARR(array, 1));
      exo_db = exo_chain(exo_db, array);
      exo_db =
	exo_chain(exo_db, exo_new(ec_array, 4,
				  exo_deepcopy(a),
				  exo_deepcopy(e),
				  exo_deepcopy(c),
				  exo_deepcopy(f),
				  NULL));

      exo_db = exo_chain(exo_db, exo_new(ec_string, "** token tests **"));
#define SYM1		1
#define SYM2		2
      exo_intern_as("sym1", SYM1);
      exo_intern_as("sym2", SYM2);
      g = exo_new(ec_token, "sym1"),
      exo_db = exo_chain(exo_db,
			 exo_new(ec_list,
				 g,
				 exo_new(ec_integer,
					 (exo_integer_t)
					 g->as_token.ent->token),
				 NULL));
      h = exo_new(ec_token, "sym2"),
      exo_db = exo_chain(exo_db,
			 exo_new(ec_list,
				 h,
				 exo_new(ec_integer,
					 (exo_integer_t)
					 h->as_token.ent->token),
				 NULL));
      i = exo_new(ec_token, "sym3"),
      exo_db = exo_chain(exo_db,
			 exo_new(ec_list,
				 i,
				 exo_new(ec_integer,
					 (exo_integer_t)
					 i->as_token.ent->token),
				 NULL));

      /* das blobs */
      exo_db = exo_chain(exo_db, exo_new(ec_blob, strlen(data), data));
      exo_db = exo_chain(exo_db, exo_new(ec_blob, strlen(data1), data1));
      exo_db = exo_chain(exo_db, exo_new(ec_blob, sizeof(data2), data2));
    }

  if (print_db)
    {
      struct exo_term_t *exo;

      /* emit header comment */
      fprintf(stdout, "\n/* EXO DB */\n\n");
      fprintf(stdout,
	      "/* EXO save file, file format version %d.%d */\n\n",
	      EXO_FMT_MAJOR, EXO_FMT_MINOR);

      /* emit all defs */
      for (exo=exo_db; exo != NULL; exo=exo->next)
	{
	  exo_print(exo, stdout);
	  fprintf(stdout, "\n\n");
	}
    }

  if (save_file)
    {
      ZFILE *exo_stream;
      struct exo_term_t *exo;

      exo_stream = myzfopen(save_file, "w");
      if (!exo_stream)
	fatal("could not open EXO file `%s'", save_file);

      /* emit header comment */
      fprintf(exo_stream->fd,
	      "/* EXO save file, file format version %d.%d */\n\n",
	      EXO_FMT_MAJOR, EXO_FMT_MINOR);

      /* emit all defs */
      for (exo=exo_db; exo != NULL; exo=exo->next)
	{
	  exo_print(exo, exo_stream->fd);
	  fprintf(exo_stream->fd, "\n\n");
	}
      myzfclose(exo_stream);
    }
}
Ejemplo n.º 2
0
Archivo: libexo.c Proyecto: palmerc/lab
/* read one EXO term from STREAM */
struct exo_term_t *
exo_read(FILE *stream)
{
  int tok;
  char tok_buf[1024], *endp;
  struct exo_term_t *ent = NULL;
  extern int errno;
  extern void yy_setstream(FILE *);

  /* make sure we have a valid stream */
  if (!stream)
    stream = stdin;
  yy_setstream(stream);

  /* make local copies of everything, allows arbitrary recursion */
  tok = yylex();
  strcpy(tok_buf, yytext);

  switch (tok)
    {
    case lex_integer:
      {
	exo_integer_t int_val;

	/* attempt integer conversion */
	errno = 0;
#ifdef HOST_HAS_QUAD
	int_val = myatoq(tok_buf, &endp, /* parse base */10);
#else /* !HOST_HAS_QUAD */
	int_val = strtoul(tok_buf, &endp, /* parse base */10);
#endif /* HOST_HAS_QUAD */
	if (!errno && !*endp)
	  {
	    /* good conversion */
	    ent = exo_new(ec_integer, int_val);
	  }
	else
	  exo_err("cannot parse integer literal");
      }
      break;

    case lex_address:
      {
	exo_address_t addr_val;

	/* attempt address conversion */
	errno = 0;
#ifdef HOST_HAS_QUAD
	addr_val = myatoq(tok_buf, &endp, /* parse base */16);
#else /* !HOST_HAS_QUAD */
	addr_val = strtoul(tok_buf, &endp, /* parse base */16);
#endif /* HOST_HAS_QUAD */
	if (!errno && !*endp)
	  {
	    /* good conversion */
	    ent = exo_new(ec_address, addr_val);
	  }
	else
	  exo_err("cannot parse address literal");
      }
      break;

    case lex_float:
      {
	exo_float_t float_val;

	/* attempt double conversion */
	errno = 0;
	float_val = strtod(tok_buf, &endp);
	if (!errno && !*endp)
	  {
	    /* good conversion */
	    ent = exo_new(ec_float, float_val);
	  }
	else
	  exo_err("cannot parse floating point literal");
      }
      break;

    case lex_char:
      {
	int c;

	c = intern_char(tok_buf, &endp);
	if (!endp)
	  exo_err("cannot convert character literal");
	ent = exo_new(ec_char, c);
      }
      break;

    case lex_string:
      {
	char *s;

	s = intern_string(tok_buf);
	ent = exo_new(ec_string, s);
	free(s);
      }
      break;

    case lex_token:
      ent = exo_new(ec_token, tok_buf);
      break;

    case lex_byte:
      exo_err("unexpected blob byte encountered");
      break;

    case '(':
      {
	struct exo_term_t *elt;

	ent = exo_new(ec_list, NULL);

	if (yy_nextchar() != ')')
	  {
	    /* not an empty list */
	    do {
	      elt = exo_read(stream);
	      if (!elt)
		exo_err("unexpected end-of-file");
	      ent->as_list.head =
		exo_chain(ent->as_list.head, elt);

	      /* consume optional commas */
	      if (yy_nextchar() == ',')
		yylex();
	    } while (yy_nextchar() != ')');
	  }

	/* read tail delimiter */
	tok = yylex();
	if (tok != ')')
	  exo_err("expected ')'");
      }
      break;

    case ')':
      exo_err("unexpected ')' encountered");
      break;

    case '<':
      exo_err("unexpected '<' encountered");
      break;

    case '>':
      exo_err("unexpected '>' encountered");
      break;

    case '{':
      {
	int cnt, size;
	struct exo_term_t *elt;

	/* get the size */
	elt = exo_read(stream);
	if (!elt || elt->ec != ec_integer)
	  exo_err("badly formed array size");

	/* record the size of the array/blob */
	size = (int)elt->as_integer.val;

	/* done with the EXO integer */
	exo_delete(elt);

	/* read the array delimiters */
	tok = yylex();
	if (tok != '}')
	  exo_err("expected '}'");

	tok = yylex();
	switch (tok)
	  {
	  case '[': /* array definition */
	    /* allocate an array definition */
	    ent = exo_new(ec_array, size, NULL);

	    /* read until array is full or tail delimiter encountered */
	    if (yy_nextchar() != ']')
	      {
		/* not an empty array */
		cnt = 0;
		do {
		  if (cnt == ent->as_array.size)
		    exo_err("too many initializers for array");

		  /* NULL element? */
		  if (yy_nextchar() == ',')
		    {
		      elt = NULL;
		    }
		  else
		    {
		      elt = exo_read(stream);
		      if (!elt)
			exo_err("unexpected end-of-file");
		    }
		  SET_EXO_ARR(ent, cnt, elt);
		  cnt++;

		  /* consume optional commas */
		  if (yy_nextchar() == ',')
		    yylex();
		} while (yy_nextchar() != ']');
	      }

	    /* read tail delimiter */
	    tok = yylex();
	    if (tok != ']')
	      exo_err("expected ']'");
	    break;

	  case '<': /* blob definition */
	    /* allocate an array definition */
	    ent = exo_new(ec_blob, size, /* zero contents */NULL);

	    /* read until blob is full */
	    if (yy_nextchar() != '>')
	      {
		unsigned int byte_val;

		/* not an empty array */
		cnt = 0;
		for (;;) {
		  /* read next blob byte */
		  tok = yylex();

		  if (tok == lex_byte)
		    {
		      if (cnt == ent->as_blob.size)
			exo_err("too many initializers for blob");

		      /* attempt hex conversion */
		      errno = 0;
		      byte_val = strtoul(yytext, &endp, /* parse base */16);
		      if (errno != 0 || *endp != '\0')
			exo_err("cannot parse blob byte literal");
		      if (byte_val > 255)
			panic("bogus byte value");
		      ent->as_blob.data[cnt] = byte_val;
		      cnt++;
		    }
		  else if (tok == '>')
		    break;
		  else
		    exo_err("unexpected character in blob");
		}
	      }

#if 0 /* zero tail is OK... */
	    if (cnt != ent->as_blob.size)
	      exo_err("not enough initializers for blob");
#endif
	    break;

	  default:
	    exo_err("expected '[' or '<'");
	  }
      }
      break;

    case '}':
      exo_err("unexpected '}' encountered");
      break;

    case ',':
      exo_err("unexpected ',' encountered");
      break;

    case '[':
      {
	int i, cnt;
	struct exo_term_t *list, *elt, *next_elt;

	/* compute the array size */
	list = NULL;
	if (yy_nextchar() == ']')
	  exo_err("unsized array has no initializers");

	cnt = 0;
	do {
	  /* NULL element? */
	  if (yy_nextchar() == ',')
	    {
	      elt = exo_new(ec_null);
	    }
	  else
	    {
	      elt = exo_read(stream);
	      if (!elt)
		exo_err("unexpected end-of-file");
	    }
	  cnt++;
	  list = exo_chain(list, elt);

	  /* consume optional commas */
	  if (yy_nextchar() == ',')
	    yylex();
	} while (yy_nextchar() != ']');

	/* read tail delimiter */
	tok = yylex();
	if (tok != ']')
	  exo_err("expected ']'");

	/* create the array */
	assert(cnt > 0);
	ent = exo_new(ec_array, cnt, NULL);

	/* fill up the array */
	for (i=0,elt=list; i<cnt; i++,elt=next_elt)
	  {
	    assert(elt != NULL);
	    next_elt = elt->next;
	    if (elt->ec == ec_null)
	      {
		SET_EXO_ARR(ent, cnt, NULL);
		exo_delete(ent);
	      }
	    else
	      {
		SET_EXO_ARR(ent, cnt, elt);
		elt->next = NULL;
	      }
	  }
      }
      break;

    case ']':
      exo_err("unexpected ']' encountered");
      break;

    case lex_eof:
      /* nothing to read */
      ent = NULL;
      break;

    default:
      panic("bogus token");
    }

  return ent;
}
Ejemplo n.º 3
0
Archivo: libexo.c Proyecto: palmerc/lab
/*
 * create a new EXO term, usage:
 *
 *	exo_new(ec_integer, (exo_integer_t)<int>);
 *	exo_new(ec_address, (exo_integer_t)<int>);
 *	exo_new(ec_float, (exo_float_t)<float>);
 *	exo_new(ec_char, (int)<char>);
 *      exo_new(ec_string, "<string>");
 *      exo_new(ec_list, <list_ent>..., NULL);
 *      exo_new(ec_array, <size>, <array_ent>..., NULL);
 *	exo_new(ec_token, "<token>"); 
*/
struct exo_term_t *
exo_new(enum exo_class_t ec, ...)
{
  struct exo_term_t *exo;
  va_list v;
  va_start(v, ec);

  exo = exo_alloc(ec);
  switch (ec)
    {
    case ec_integer:
      exo->as_integer.val = va_arg(v, exo_integer_t);
      break;

    case ec_address:
      exo->as_address.val = va_arg(v, exo_address_t);
      break;

    case ec_float:
      exo->as_float.val = va_arg(v, exo_float_t);
      break;

    case ec_char:
      exo->as_char.val = va_arg(v, int);
      break;

    case ec_string:
      {
	char *str;

	str = va_arg(v, char *);
	exo->as_string.str = (unsigned char *)mystrdup(str);
      }
      break;

    case ec_list:
      {
	struct exo_term_t *ent;

	exo->as_list.head = NULL;
	do {
	  ent = va_arg(v, struct exo_term_t *);
	  exo->as_list.head = exo_chain(exo->as_list.head, ent);
	} while (ent != NULL);
      }
      break;

    case ec_array:
      {
	int i;
	struct exo_term_t *ent;

	exo->as_array.size = va_arg(v, int);
	exo->as_array.array = (struct exo_term_t **)
	  calloc(exo->as_array.size, sizeof(struct exo_term_t *));
	if (!exo->as_array.array)
	  fatal("out of virtual memory");
	i = 0;
	do {
	  ent = va_arg(v, struct exo_term_t *);
	  if (ent != NULL)
	    {
	      if (i == exo->as_array.size)
		fatal("array constructor overflow");
	      SET_EXO_ARR(exo, i, ent);
	    }
	  i++;
	} while (ent != NULL);
      }
      break;

    case ec_token:
      {
	char *str;

	str = va_arg(v, char *);
	exo->as_token.ent = exo_intern(str);
      }
      break;

    case ec_blob:
      {
	unsigned size;
	unsigned char *data;

	size = va_arg(v, unsigned);
	data = va_arg(v, unsigned char *);

	exo->as_blob.size = size;
	exo->as_blob.data = malloc(size);
	if (data != NULL)
	  memcpy(exo->as_blob.data, data, size);
	else
	  memset(exo->as_blob.data, 0, size);
      }
      break;

    case ec_null:
      break;

    default:
      panic("bogus EXO class");
    }

  va_end(v);
  return exo;
}
Ejemplo n.º 4
0
Archivo: libexo.c Proyecto: palmerc/lab
/* deep copy an EXO structure */
struct exo_term_t *
exo_deepcopy(struct exo_term_t *exo)
{
  struct exo_term_t *new_exo;

  /* NULL copy */
  if (!exo)
    return NULL;

  new_exo = exo_copy(exo);
  switch (new_exo->ec)
    {
    case ec_integer:
    case ec_address:
    case ec_float:
    case ec_char:
    case ec_token:
      /* exo_copy() == exo_deepcopy() for these node classes */
      break;

    case ec_string:
      /* copy the referenced string */
      new_exo->as_string.str =
	(unsigned char *)mystrdup((char *)exo->as_string.str);
      break;

    case ec_list:
      /* copy all list elements */
      {
	struct exo_term_t *elt, *new_elt, *new_list;

	new_list = NULL;
	for (elt=new_exo->as_list.head; elt != NULL; elt=elt->next)
	  {
	    new_elt = exo_deepcopy(elt);
	    new_list = exo_chain(new_list, new_elt);
	  }
	new_exo->as_list.head = new_list;
      }
      break;

    case ec_array:
      /* copy all array elements */
      {
	int i;

	for (i=0; i<new_exo->as_array.size; i++)
	  {
	    SET_EXO_ARR(new_exo, i, exo_deepcopy(EXO_ARR(exo, i)));
	  }
      }
      break;

    case ec_blob:
      new_exo->as_blob.data = malloc(new_exo->as_array.size);
      memcpy(new_exo->as_blob.data, exo->as_blob.data, new_exo->as_array.size);
      break;

    default:
      panic("bogus EXO class");
    }

  return new_exo;
}
Ejemplo n.º 5
0
/* check point current architected state to stream FD, returns
   EIO transaction count (an EIO file pointer) */
counter_t
eio_write_chkpt(struct regs_t *regs,		/* regs to dump */
		struct mem_t *mem,		/* memory to dump */
		FILE *fd)			/* stream to write to */
{
  int i;
  struct exo_term_t *exo;
  struct mem_pte_t *pte;

  myfprintf(fd, "/* ** start checkpoint @ %n... */\n\n", eio_trans_icnt);

  myfprintf(fd, "/* EIO file pointer: %n... */\n", eio_trans_icnt);
  exo = exo_new(ec_integer, (exo_integer_t)eio_trans_icnt);
  exo_print(exo, fd);
  fprintf(fd, "\n\n");
  exo_delete(exo);

  /* dump misc regs: icnt, PC, NPC, etc... */
  fprintf(fd, "/* misc regs icnt, PC, NPC, etc... */\n");
  exo = MD_MISC_REGS_TO_EXO(regs);
  exo_print(exo, fd);
  fprintf(fd, "\n\n");
  exo_delete(exo);

  /* dump integer registers */
  fprintf(fd, "/* integer regs */\n");
  exo = exo_new(ec_list, NULL);
  for (i=0; i < MD_NUM_IREGS; i++)
    exo->as_list.head = exo_chain(exo->as_list.head, MD_IREG_TO_EXO(regs, i));
  exo_print(exo, fd);
  fprintf(fd, "\n\n");
  exo_delete(exo);

  /* dump FP registers */
  fprintf(fd, "/* FP regs (integer format) */\n");
  exo = exo_new(ec_list, NULL);
  for (i=0; i < MD_NUM_FREGS; i++)
    exo->as_list.head = exo_chain(exo->as_list.head, MD_FREG_TO_EXO(regs, i));
  exo_print(exo, fd);
  fprintf(fd, "\n\n");
  exo_delete(exo);

  fprintf(fd, "/* writing `%d' memory pages... */\n", (int)mem->page_count);
  exo = exo_new(ec_list,
		exo_new(ec_integer, (exo_integer_t)mem->page_count),
		exo_new(ec_address, (exo_integer_t)ld_brk_point),
		exo_new(ec_address, (exo_integer_t)ld_stack_min),
		NULL);
  exo_print(exo, fd);
  fprintf(fd, "\n\n");
  exo_delete(exo);

  fprintf(fd, "/* text segment specifiers (base & size) */\n");
  exo = exo_new(ec_list,
		exo_new(ec_address, (exo_integer_t)ld_text_base),
		exo_new(ec_integer, (exo_integer_t)ld_text_size),
		NULL);
  exo_print(exo, fd);
  fprintf(fd, "\n\n");
  exo_delete(exo);

  fprintf(fd, "/* data segment specifiers (base & size) */\n");
  exo = exo_new(ec_list,
		exo_new(ec_address, (exo_integer_t)ld_data_base),
		exo_new(ec_integer, (exo_integer_t)ld_data_size),
		NULL);
  exo_print(exo, fd);
  fprintf(fd, "\n\n");
  exo_delete(exo);

  fprintf(fd, "/* stack segment specifiers (base & size) */\n");
  exo = exo_new(ec_list,
		exo_new(ec_address, (exo_integer_t)ld_stack_base),
		exo_new(ec_integer, (exo_integer_t)ld_stack_size),
		NULL);
  exo_print(exo, fd);
  fprintf(fd, "\n\n");
  exo_delete(exo);

  /* visit all active memory pages, and dump them to the checkpoint file */
  MEM_FORALL(mem, i, pte)
    {
      /* dump this page... */
      exo = exo_new(ec_list,
		    exo_new(ec_address, (exo_integer_t)MEM_PTE_ADDR(pte, i)),
		    exo_new(ec_blob, MD_PAGE_SIZE, pte->page),
		    NULL);
      exo_print(exo, fd);
      fprintf(fd, "\n\n");
      exo_delete(exo);
    }