/* testalloc.c   Swinehart, June 18, 1982  4:08 PM   */

#include <Alloc.h>

char leftover;
int args[10];
int mlerr[3];
int any;

extern struct ZN *GetAll();
extern int *Alloc();
extern Free();
extern CheckZone();

main()
  {
  char c;
  int i, result;
  wf("\rAllocTest.\r");
  result = ssenable(mlerr);
  if (result) wf(" ##\r");
  for (;;) {
      wf("% ");
      c = getchar() & 0177;
      switch (c) {
        case 'q': {
          wf("\rQuit!\r");
          return;
          };
        case '1': { getarg(0); break; };	/* Zone */
        case '2': { getarg(1); break; };	/* Which arg has result */
        case '3': { getarg(2); break; };	/* Size of new block */
        case '4': { getarg(3); break; };	/* Unassigned */
        case 'p': { printargs(); break; };
	case 'i': { initialize(); break; };
        case 'a': { allocate(); break; };
	case 'f': { release(); break; };
	case 'c': { diagnose(); break; };
        default: wf("\r unknown command\r");
        };
 };  /* end of forever loop */
 };

getarg(an)
  int an;
  {
  wf1("arg word %d: ", an);
  args[an] = gethex();
  wfcr();
  printarg(an);
  };

printargs()
  {
  int i;
  for (i = 0; i<10; i+=1) printarg(i);
  };

printarg(i)
  int i;
  {
  wf3("arg %d = %xH, %d\r", i, args[i], args[i]);
  };

initialize()
  {
  args[0] = (int) GetAll();
  printarg(0);
  };

allocate()
  {
  args[args[1]] = (int) Alloc(args[0], args[2]);
  printarg(args[1]);
  };

release()
  {
  Free(args[0], args[args[1]]);
  };

diagnose()
  {
  CheckZone(args[0], 1);
  };

mlabort()
  {
  ssreturn(mlerr, 1);
  };

int egetchar()  /* getchar with echo */
  {
  leftover = getchar() & 0177;
  putcrchar(leftover);
  if (leftover == 0177) mlabort();
  return(leftover);
  };

int gethex()
  {
  int v;
  v = 0;
  any = 0;
  for (;;) {
    egetchar();
    if (isdigit(leftover)) {
      v = (v<<4);
      v += (leftover-'0');
      any = 1;
      continue;
      };
    leftover = lc(leftover);
    if (leftover>='a' && leftover<='f') {
      v = (v<<4);
      v += (leftover-'a'+10);
      any = 1;
      continue;
      };
    break;
    };
  return(v);
  };