#include <exec/types.h>
#include <exec/memory.h>
#include <stdio.h>
#include <proto/exec.h>
#include <string.h>
#include <stdlib.h>

#define MWDEBUG 1

#include "memwatch.h"

void main(int, char **);

void main(argc, argv)
int argc;
char **argv;
{
   char *tmpchar = NULL;
   int doit = 1;
   int offset = 0;
   long tmplong;
   char buffer[100];

   MWInit(NULL, 0);

   while(doit)
   {
      printf("\nEnter command (h for help): ");
      fgets(buffer, 100, stdin);
      offset = 0;
      switch(buffer[0])
      {
         case 'a': case 'A':  /* Allocate memory */
            while(buffer[++offset] == ' ' && buffer[offset]);
            stcd_l(buffer+offset, &tmplong);
            tmpchar=AllocMem(tmplong, 0);
            printf("%ld bytes allocated, value 0x%08lx\n", 
               tmplong, tmpchar);
            break;

         case 'w': case 'W':  /* Write to memory */
            while(buffer[++offset] == ' ' && buffer[offset]);
            stcd_l(buffer+offset, &tmplong);
            if(tmpchar) memset(tmpchar, 0, tmplong);
            else printf("Can't write, no memory allocated\n");
            printf("%d bytes cleared\n", tmplong);
            break;

         case 'r': case 'R':  /* Report */
            while(buffer[++offset] == ' ' && buffer[offset]);
            offset+=stcd_l(buffer+offset, &tmplong);
            MWReport(buffer+offset, tmplong);
            printf("Report complete\n\n\n");
            break;

         case 'c': case 'C':  /* Check */
            MWCheck();
            printf("Check complete\n\n\n");
            break;

         case 'f': case 'F':  /* Free */
            while(buffer[++offset] == ' ' && buffer[offset]);
            stcd_l(buffer+offset, &tmplong);
            FreeMem(tmpchar, tmplong);
            printf("%d bytes freed\n", tmplong);
            break;

         case 'q': case 'Q':  /* QUIT */
            doit = 0;
            break;

         default:
printf("Commands are:\n");
printf("A <n>     --> Allocate 'n' bytes, replace current allocation\n");
printf("W <n>     --> Write 'n' bytes to most recent allocation\n");
printf("F <n>     --> Free 'n' bytes starting at most recent allocation\n");
printf("R <lvl>   --> Report on usage; <lvl> is 1 for less, 2 for more detail\n");
printf("C         --> Check all allocations, report errors\n");
printf("Q         --> Quit program\n");
            break;
      }
   }

   MWTerm();

   exit(0);
}


