$title ('MP/M 1.1  Type a File')
type:
do;

$include (copyrt.lit)
/*
  Revised:
    19 Jan 80  by Thomas Rolander
*/

  declare start label;
  declare jmp$to$start structure (
    jmp$instr byte,
    jmp$location address ) data  (
    0C3H,
    .start-3);

  mon1:
    procedure (func,info) external;
      declare func byte;
      declare info address;
    end mon1;

  mon2:
    procedure (func,info) byte external;
      declare func byte;
      declare info address;
    end mon2;

  declare fcb (1) byte external;
  declare fcb16 (1) byte external;
  declare tbuff (1) byte external;

  /**************************************
   *                                    *
   *       B D O S   Externals          *
   *                                    *
   **************************************/

  read$console:
    procedure byte;
      return mon2 (1,0);
    end read$console;

  write$console:
    procedure (char);
      declare char byte;
      call mon1 (2,char);
    end write$console;

  print$console$buffer:
    procedure (buff$adr);
      declare buff$adr address;
      call mon1 (9,buff$adr);
    end print$console$buffer;

  check$console$status:
    procedure byte;
      return mon2 (11,0);
    end check$console$status;

  open$file:
    procedure (fcb$address) byte;
      declare fcb$address address;
      return mon2 (15,fcb$address);
    end open$file;

  close$file:
    procedure (fcb$address) byte;
      declare fcb$address address;
      return mon2 (16,fcb$address);
    end close$file;

  read$record:
    procedure (fcb$address) byte;
      declare fcb$address address;
      return mon2 (20,fcb$address);
    end read$record;

  terminate:
    procedure;
      call mon1 (143,0);
    end terminate;

  declare (eod,i,char) byte;

  declare control$z literally '1AH';
  declare true literally '0FFH';

  /*
    Main Program
  */

declare (cnt,tcnt) byte;

declare last$dseg$byte byte
  initial (0);

start:
  do;
    tcnt,
    cnt = 0;
    if fcb16(1) = 'P' then
    do;
      cnt = (fcb16(2)-'0')*10
           +(fcb16(3)-'0');
    end;
    if open$file (.fcb) <> 0FFH then
    do;
      fcb(32) = 0;
      eod = 0;
      do while (not eod) and (read$record (.fcb) = 0);
        do i = 0 to 127;
          if (char := tbuff(i)) = control$z
            then eod = true;
          if not eod then
          do;
            if check$console$status then
            do;
              i = read$console;
              call terminate;
            end;
            if cnt <> 0 then
            do;
              if char = 0ah then
              do;
                if (tcnt:=tcnt+1) = cnt then
                do;
                  tcnt = read$console;
                  tcnt = 0;
                end;
              end;
            end;
            call write$console (char);
          end;
        end;
      end;
    /*
      call close (.fcb);
      *** Warning ***
      If this call is left in, the file can be destroyed.
    */
    end;
    else
    do;
      call print$console$buffer (.(
        'No file.','$'));
    end;
    call terminate;
  end;

end type;
