/*
**                HEX FILE DUMP UTILITY
**   Copyright 1987-90  Steven E. Margison
**   Displays any file in hex and character representation
**   in color or monochrome, depending upon type of video
**   card installed.  Displays 20 lines per screen and waits
**   for a keypress.
**
**   Rev 1.20   03-12-90 for Turbo-C 2.0 and later  S.E. Margison
**
**   As distributed, this program requires (for compilation):
**     "Steve's Turbo-C Library" version 1.50 or later
**   which may be obtained without registration from many Bulletin
**   Board Systems including:
**      Compuserve IBMSW
**      GEnie
**   and software library houses including:
**      Public (Software) Library (Houston, TX.)
**
**   or by registration:
**      $25 for Docs, C, S, M, L, H libraries, and complete library source
**              in C and Assembler
**     Steven E. Margison
**     124 Sixth Street
**     Downers Grove, IL, 60515
**
**
** Program requires at least one parameter, the name of the file to dump.
** Other options:
**     -snnnn        Start at hex offset nnnn into file (default is 0000)
**     -hnn          Highlite all bytes of hex value nn in display
**     -r            Right side display is actual IBM screen character.
**                    Without this option, only valid ASCII characters
**                    are displayed.  All others display as a period.
**
** More than a utility, this is a good demonstration of the direct
** video subroutines.
*/

#include <stdio.h>
#include <ctype.h>
#include <smdefs.h>
#include <screen.h>
#include <keys.h>

int adrclr,    /* address field color */
    hexclr,    /* hex data color */
    highclr,   /* highlite color */
    prompt,    /* prompt line color */
    ascclr;    /* ascii color */

char tbuf[82];
int buffer[16];
unsigned long offset = 0;
FILE *f = NULL;

int mono, eoflag, rawflag;


void bad_options()
{
      error("Use: dump file [-snnnn -hnn -r]");
   }


main(argc,argv)
int argc;
char *argv[];
{
   int i, lines, view, vflag;

   eoflag = vflag = NO;      /* default highlite off */
   rawflag = NO;             /* default normal display mode */

   if ((argc < 2) or (argc > 5)) bad_options();

   while(--argc > 0) {
      if(argv[argc][0] is '-') {
         switch(argv[argc][1]) {
            case 'r':   case 'R':
               rawflag = YES;
               break;
            case 'h':   case 'H':
               sscanf(&argv[argc][2], "%02x", &view);
               vflag = YES;
               break;
            case 's':   case 'S':
               sscanf(&argv[argc][2], "%lx", &offset);
               break;
            }     /* end of switch */
         continue;
         }
      else {
         if(f isnot NULL) error("Too many filenames specified");
         if((f = fopen(argv[argc], "rb")) is NULL) cant(argv[argc]);
         }
      }   /* end of while statement */

   if(f is NULL) bad_options();
   fseek(f, offset, 0);

   i = stuff(0);   /* check video type */
   if(i is MONO) {
      mono = TRUE;
      prompt = BLINKING;
      adrclr = WHITE;
      hexclr = HIWHITE;
      ascclr = WHITE;
      highclr = HIGHBLINK;
      }
   else {         /* for CGA/VGA/EGA cards */
      vmode(CLR80);
      mono = FALSE;
      prompt = BROWN;
      adrclr = LTGREEN;
      hexclr = YELLOW;
      ascclr = LTCYAN;
      highclr = HIWHITE;
      }

   dvid_init();    /* use direct video access routines */
   dvid_cls();
   dvid_move(0,0);
   dvid_flush();
   dvid_attrib(hexclr);   /* set hex value color, our default */
   cursor_style(5, 0, 0);    /* kill the cursor */
   lines = 20;

   for ever {
        /* if this were a level 1 file using read() and open()
        ** the program would be faster. */
      for (i = 0; i < 16; i++) buffer[i] = fgetc(f);

      if(buffer[0] is -1) {
         if(lines is 20) break;    /* we must've ended on even page */
         eoflag = TRUE;
         goto alldone;
         }

      sprintf(tbuf, "%04lx: ",offset);  /* display address */
      dvid_putsa(tbuf, adrclr);
      for (i = 0; i < 16; i++) {
         if (buffer[i] isnot -1) {
            if(vflag and (buffer[i] is view)) {  /* highlite this byte? */
               sprintf(tbuf, "%02x ", buffer[i]);
               dvid_putsa(tbuf, highclr);
               }
            else {
               sprintf(tbuf, "%02x ", buffer[i]);
               dvid_putstr(tbuf);
               }
            }
         else dvid_putstr("   ");
         }
      dvid_raw(rawflag);     /* select our mode */
      dvid_attrib(ascclr);  /* display ascii data color */
      dvid_putstr("   ");
      for (i = 0; i < 16; i++) {
         if (buffer[i] isnot -1) {
            if (!isprint(buffer[i]) and !rawflag) dvid_putchr('.');
            else dvid_putchr(buffer[i]);
            }
         else dvid_putchr(' ');
         }
      dvid_raw(NO);         /* force non-raw mode */
      dvid_putchr('\n');
      if(lines-- is 0) {
         alldone:
         dvid_attrib(prompt);
         dvid_say(22, 10, "Press any key to continue, ESCape to quit...");
         if(getkey() is ESC) {
            if(!mono) cursor_style(1, 0, 0);  /* restore cursor */
            else cursor_style(2, 0, 0);
            aabort(1);
            }
         if(eoflag) break;
         dvid_cls();
         dvid_flush();
         lines = 20;
         }
      offset += 16;
      dvid_attrib(hexclr);
      }  /* end of forever loop */

   dvid_cls();
   dvid_attrib(prompt);
   dvid_say(22, 10, "End of File Encountered");
   if(!mono) cursor_style(1, 0, 0);  /* restore cursor */
   else cursor_style(2, 0, 0);
   dvid_flush();
   dvid_done();
}

