/*  Dumps a ZIP Heap Image */
/*  Modified for Orion to cope with printing Orion addresses */

#include "zap.h"

#define LENHASH 199

FILE *file;
char *tagname[] =
{
  "   int"," float", "  atom", "   box", "  term", "  link", "UNUSED",
  " undef",
  " funct"," block", " empty", "termin", "  proc", " table", "tabref",
  "clause"
};


main(argc,argv)
int argc;
char *argv[];
{
  int i,w,b;
  if ((file = fopen(*++argv,"r")) == NULL)
  {
    printf("cannot open input file\n");
    exit(0);
  }
  for (i=0; i < LENHASH; i++)
  {
    printf("%5d  ",i);
    writeword(readword());
  }
  printf("\n");
  for (i=0; i < SYSWSIZE; i++)
  {
    printf("%5d  ",i);
    writeword(readword());
  }
  printf("\n");
  i = 0;
  while ((w = readword()) != EOF)
  {
    printf("%5d  ",i++);
    writeword(w);
    if (tag(w) == BLOCK)
    {
      b = blocksize(val(w)) - 1;
      while (b--)
      {
        printf("%5d  ",i++);
        writebytes(readword());
      }
    }
  }
}


/* read a word from the Heap Image */

int readword()
{
  int c,i;
  c = getc(file);
  if (c != EOF)  for (i = 0; i < 3; i++) c = (c << 8) | (getc(file) & 0377);
  return(c);
}


/* write a word in readable form */

writeword(w)
{
  printf("%s | %-5d\n",tagname[(tag(w) >> 28) & 15],val(w));
}


/* write a word in byte form */

writebytes(w)
{
  wbyte((w >> 24) & 0377);
  wbyte((w >> 16) & 0377);
  wbyte((w >>  8) & 0377);
  wbyte(       w & 0377);
  printf("\n");
}


wbyte(i)
{
  printf("%3d ",i);
  if ((i < '!') || (i > 127)) printf("    "); else printf("(%c) ",i);
}
