home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / sigm / vol132 / zdump.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1984-04-29  |  5.5 KB  |  186 lines

  1. PROGRAM dump;
  2. {
  3. *****************************************************************
  4. *                                *
  5. *    Hex-Ascii file dump utility                *
  6. *                                *
  7. *    Written by:    Bob Longoria                *
  8. *            1024 Lawrence Dr. N.E.            *
  9. *            Albuquerque, New Mexico 87123        *
  10. *            (505)-298-7231                *
  11. *                                *
  12. *    Description:                        *
  13. *         This program is a slightly improved dump utility    *
  14. *    over the standard dump program which comes with CP/M    *
  15. *    distribution.  When dumping, the program will dump    *
  16. *    both in hex (as done by dump.com) and in ascii and    *
  17. *    at a standard CP/M sector (128 bytes) at a time.  Also    *
  18. *    each sector is numbered beginning with sector 1.    *
  19. *         Output of the dump can be directed to any desired  *
  20. *    file or output device.  For example:            *
  21. *        zdump foo.rel    - dumps to the console        *
  22. *        zdump foo.rel con:   - also dumps to console    *
  23. *        zdump foo.rel lst:   - dumps to listing device  *
  24. *        zdump foo.rel dumb.dmp   - dumps to a file    *
  25. *            called dump.dmp                 *
  26. *                                *
  27. ***************************************************************** }
  28.  
  29. CONST
  30.     MAXARGS = 4;    { Maximum command line arguments }
  31. TYPE
  32.     byte = 0..255;
  33.     buffer = array[1..8,1..16] of 0..255;
  34.     ch2 = packed array[1..2] of char;
  35.     args = array[1..MAXARGS] of string 14;
  36. VAR
  37.     i         : integer;        { General purpose index variable }
  38.     infile   : file of buffer;    { The input file to be dumped }
  39.     outfile  : text;        { The output file to receive the dump }
  40.     inbuf    : buffer;        { Buffer which holds a standard sector }
  41.     reccount : integer;        { The current sector being processed }
  42.     argc     : integer;        { The number of command line arguments }
  43.     argv     : args;        { The command line argument array }
  44.  
  45. FUNCTION tohex(x : byte) : char;
  46. {    Description:
  47.      receives a byte within the hex range (0-15) and returns
  48.     the hex character equivalent. }
  49. BEGIN
  50.     case x of
  51.     0,1,2,3,4,5,6,7,8,9 :
  52.         tohex := chr(x+ord('0'));
  53.     { If it is in this range, must be a letter character
  54.       of A-F }
  55.         10,11,12,13,14,15:
  56.         tohex := chr(x+ord('A')-10);
  57.     end
  58. end;
  59.  
  60. FUNCTION cnvrthex(operand : byte) : ch2;
  61. {    DESCRIPTION:
  62.     This function takes a byte and converts it into it's
  63.     two equivalent hex characters. }
  64. VAR
  65.     remain : byte;
  66.  
  67. BEGIN
  68.     remain := operand-(operand div 16)*16;
  69.     cnvrthex[2] := tohex(remain);    { First hex character }
  70.     operand := operand div 16;
  71.     remain := operand-(operand div 16)*16;
  72.     cnvrthex[1] := tohex(remain)    { Second hex character }
  73. END;
  74.  
  75. PROCEDURE display(recno : integer; inbuf : buffer);
  76. {    DESCRIPTION:
  77.     This routine takes care of displaying in both hex and
  78.     ascii the input buffer (128 byte record). }
  79. VAR
  80.     i, j : integer;
  81.     hexchar : ch2;
  82.  
  83. BEGIN
  84.     { Begin by labeling the record number }
  85.     write(outfile,recno:3,':  ');
  86.     { This buffer is processed into 8 lines of output }
  87.     for i := 1 to 8 do
  88.     begin
  89.         { Each line has 16 bytes to process }
  90.         { Begin by writing out the hexidecimal equivalents
  91.           of each of the bytes }
  92.         for j := 1 to 16 do
  93.             begin
  94.                 hexchar := cnvrthex(inbuf[i,j]);
  95.             write(outfile,hexchar,' ')
  96.             end;
  97.         { Now convert the same 16 bytes into their ascii eqivalents }
  98.         write(outfile,'  ');
  99.         for j := 1 to 16 do
  100.             begin
  101.             { If the byte is a non-printing character, sub-
  102.               stitute an ascii "." in its place }
  103.             if (inbuf[i,j] < 32) or (inbuf[i,j] > 126)
  104.                 then write(outfile,'.')
  105.             { Otherwise print the actual ascii equivalent }
  106.             else
  107.                 write(outfile,chr(inbuf[i,j]))
  108.             end;
  109.         writeln(outfile);
  110.         write(outfile,'      ')
  111.     end;
  112.     writeln(outfile)
  113. END;
  114.  
  115. PROCEDURE cmdline(var count : integer; var token : args);
  116. {    DESCRIPTION
  117.     This routine performs several functions.  It reads
  118.     the CP/M command tail if any and breaks the command 
  119.     tail into tokens.  A token is any string of characters
  120.     delimited by either the beginning of the command
  121.     tail, the end of the command tail, or a space.  The
  122.     routine returns the token count and all tokens found. }
  123.  
  124. VAR
  125.     cmd_line : packed array[1..80] of char;
  126.     i, j : integer;
  127.  
  128. BEGIN { cmdline }
  129.     { Make sure the command line is clean }
  130.     for i := 1 to 80 do
  131.     cmd_line[i] := ' ';
  132.     count := 0;
  133.     { if the following is true there is a command tail, otherwise
  134.       leave the count set to 0 and do not parse the command line }
  135.     if not eoln(0) then
  136.     begin
  137.         readln(cmd_line);
  138.             i := 0;
  139.             repeat
  140.              i := i+1;
  141.         if cmd_line[i] <> ' ' then
  142.                 begin
  143.             count := count+1;
  144.             j := 1;
  145.             token[count,j] := cmd_line[i];
  146.             while cmd_line[i+1] <> ' ' do
  147.                     begin
  148.                 i := i+1;
  149.                 j := j+1;
  150.                 token[count,j] := cmd_line[i]
  151.                     end { while }
  152.             end { if }
  153.             until i = 80
  154.     end { if not eoln }
  155. END; { cmdline }
  156.  
  157. BEGIN  { MAIN CODE }
  158.     writeln;
  159.     { Make sure command line arguments are clean }
  160.     for i := 1 to MAXARGS do
  161.     argv[i] := '              ';
  162.     reccount := 1;    { Begin numbering records at 1 }
  163.     cmdline(argc,argv);    { Get and parse the command line }
  164.     if argc = 0 then
  165.     writeln('No INPUT file specified')
  166.     else if argc > 2 then
  167.     writeln('Too many command line arguments')
  168.     else
  169.     begin
  170.         { Default output goes to the console }
  171.         if argc = 1 then
  172.         argv[2] := 'CON:';
  173.         reset(argv[1],infile);
  174.         rewrite(argv[2],outfile);
  175.             writeln(outfile,'                Hex-Ascii file dump -- Vers. 1.1');
  176.             writeln(outfile);
  177.         { Process until EOF detected on input file }
  178.             while not(eof(infile)) do
  179.             begin
  180.                 read(infile,inbuf);
  181.                 display(reccount,inbuf);
  182.                 reccount := reccount+1
  183.             end
  184.     end
  185. END.
  186.