
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include "file.h"

#define	EATAB {while (isascii((unsigned char) *l) && \
		      isspace((unsigned char) *l))  ++l;}
#define LOWCASE(l) (isupper((unsigned char) (l)) ? \
			tolower((unsigned char) (l)) : (l))

int parse_ascii_or_special_magic(l, which, check)
     char *l; int which, check;
{
  struct magic *m;
  int idx;
  char *s, buf[256];
  
  while(isascii((unsigned char)*l) && !isspace((unsigned char)*l))  ++l;
  EATAB;

  /* get description, skip it */
  s = buf;
  while(isascii((unsigned char)*l) && !isspace((unsigned char)*l))
    *s++ = *l++;
  *s = 0;
  EATAB;
  s = buf;
  while(isascii((unsigned char)*l) && isdigit((unsigned char)*l))
    *s++ = *l++;
  *s = 0;
  idx = atoi(buf);
  if(idx > 0 && idx < 200)
    {
      if(specialMagicTbSizes[which] <= idx+1)
	{
	  if(specialMagicTb[which] == NULL)
	    {
	      specialMagicTb[which] = (struct magic *)malloc(20*sizeof(struct magic));
	      memset(specialMagicTb[which], 0, sizeof(struct magic) * 20);
	      specialMagicTbSizes[which] = 20;  
	    }
	  else
	    {
	      specialMagicTb[which] = (struct magic *)
		realloc(specialMagicTb[which],
			(specialMagicTbSizes[which]+20)*sizeof(struct magic));
	      memset( specialMagicTb[which] + specialMagicTbSizes[which],
		      0, sizeof(struct magic) * 20);
	      specialMagicTbSizes[which] += 20;
	    }
	}      
      m = &(specialMagicTb[which][idx]);
      m->type = (which == 0 ? BYTE: (which == 1? SHORT: LONG));
      m->cont_level = 1;
      m->in.type = (which == 0 ? BYTE: (which == 1? SHORT: LONG));
      m->offset = 1;
      m->flag = 1;
      EATAB;
      strncpy(m->desc, l, MAXDESC-1); m->desc[MAXDESC-1] = 0;
      if(check) mdump(m);
    }
}

dumpTb()
{
  int i, j;
  for(i = 0; i < 3; i++)
    for(j = 0; j < specialMagicTbSizes[i]; j++)
      {
	struct magic *m = &(specialMagicTb[i][j]);
	fprintf(stderr, "j= %d :: ", j); mdump(m);
      }
}
