! Program to list through a series of files in paged form
! MDP Feb'84
! Modded by AJS so that filename is printed before connecting file 14/4/84
! Ported to FMAC by JHB with minimal mods to LOAD DIRECTORY and main prog.
! Modified by RWT to take account of V200EM-supplied screensize 11/4/85
! Modified by AJS to use VTLIB so will work with Wyses as well 11/7/85

!Parameters:          Filespec

!Commands:
!s, ' ', Cursor-down: Next page       -, <Cursor-Up>:     Last page
!r or Cursor-Home:    Restart         q:                  Quit
!b:                   Back one file   n:                  Next file

%include "Inc:Util.Imp"
%include "inc:fs.imp"
%include "inc:fsutil.imp"
%Include "inc:vtlib.imp"
@16_3fa0 %byte screenrows{,screencols;@16_3fa2 %routine screenput(%integer sym)

%begin
   %string (31) %array File Names (1 : 110)
   %integer %array Page Starts (1 : 1000)
   %integer Number of Files = 0
   %string (63) This File, A, B, Base File Name, Directory, Line
   %integer Ptr 1, Ptr 2, Index

   %routine Load Directory (%string (7) Directory)

      %string (127) Line

      %on 3,9 %start
         -> End Load
      %finish

      Directory = Directory . ":" %if Directory # ""
      Open Input (1, Directory."Directory")
      Select Input (1)

      Number of Files = 0
      %cycle
         Read Line (Line)
         Number of Files = Number of Files + 1
         File Names (Number of Files) = Line
      %repeat

 End Load:
      Close Input
      Select Input (0)
      %if Number of files = 0 %start
         Print String ("PERUSE : - can't get at directory - ".Event_Message); New Line
         %stop
      %finish
   %end

   %routine Display Line (%integer %name Start, F End, Print)

      %integer Sym

      %cycle
         Sym = Byte Integer (Start)
         Sym = Sym & 127
         Sym = '_' %if (Sym < ' ' %or Sym = 127) %and Sym # Nl
         Print Symbol (Sym) %if Print = 1
         Start = Start + 1
         Start = -1 %and %return %if Start > F End
      %repeat %until Sym = Nl
   %end

   %routine Display Page (%integer P Start, %integer %name NP Start,
                          %integer P End,%integer %name Print)

      %integer Lines = 0

      NP Start = P Start
      %cycle
         Lines = Lines + 1
         Display Line (NP Start, P End, Print)
         %exit %if NP Start < 0
      %repeat %until Lines > {was 21} screenrows-3
      Print = 1
   %end


   %routine  Peruse File (%string (31) Directory, File)

      %integer Previous Page, Page Start, New Page Start
      %integer Addr, Len, File End
      %integer Sym, Page Number = 1, Print = 1
      %Byte    First = 1

!!      %integerfn last page
!!         !Return the base address of the last page
!!         !Uses globals PAGE NUMBER, FILE END
!!         %integer lines, pointer, page
!!         page = page number
!!         %cycle
!!            pointer = page start
!!            %for lines = 1, 1, 21 %cycle
!!                pointer = pointer + 1 %until byteinteger(pointer) = nl %or %c
!!                pointer > file end
!!            %repeat
!!            page = page + 1
!!         %repeatuntil pointer > file end
!!         %result = page - 1
!!      %end

      File = Directory .":". File %if Directory # ""
      clear frame
      Print String (File)

      Connect File (File, 0, Addr, Len)
      File End = Addr + Len - 1
      Page Start = Addr

      %cycle
          %exit %if Page Start < 0

          %if first = 1 %Start  ;! Don't clear screen first time round
             first = 0
          %Else
             clear frame
             Print String (File)
         %Finish

         Spaces (70 - Length (File))
         Print String ("Page ")
         Write (Page Number, 0)
         New Line
         Display Page (Page Start, New Page Start, File End, Print)
!         Print Symbol (27); Print Symbol ('Y')
!         Print Symbol ({was 23}screenrows-1 + ' '); Print Symbol (0 + ' ')
         Gotoxy (screenrows-1,0)
         Print String ("** End **") %if New Page Start < 0
         %cycle
            Sym = 1
            Sym = Test Symbol %while Sym > 0 ;!Flush input buffer
            Sym = Test Symbol %until Sym >= 10;!Skip till first printable char
            %if sym = 27 %start ;!Cursor key
               sym = testsymbol %until sym >= 0
               sym = sym ! 128
            %finish
            Sym = Sym + ' ' %if 'A' <= Sym <= 'Z'
            %if Sym = 'q' %start     ;! quit
               New Line
               %stop
            %else %if Sym = 'b'      ;! back one file
               heapput(addr)
               %signal 11
            %else %if Sym = 'n'      ;! next file
               heapput(addr)
               %signal 12
            %else %if Sym = 'r' %or sym = 'H'+128      ;! restart
               Page Start = Addr
               Page Number = 1
               %exit
            %else %if Sym <= ' ' %or Sym = 's' %or sym = 'B'+128      ;! more %or skip
               Print = 0 %if Sym = 's'
               Page Starts (Page Number) = Page Start
               Page Start = New Page Start
               Page Number = Page Number + 1
               %exit
            %else %if Sym = '-' %or sym = 'A'+128
               %exit %if Page Number = 1
               Page Number = Page Number - 1
               Page Start = Page Starts (Page Number)
               %exit
!!            %elseif sym = 'e'
!!               page starts(page number) = page start
!!               page start = new page start
!!               page number = last page
!!               %exit
            %else
               Print Symbol (7)
            %finish
         %repeat
      %repeat
      heapput(addr)
   %end

   %on 3,9,11,12 %start
      newline %and %stop %if event_event = 9
      Index = Index - 1 %if Index # 1 %and Event_Event = 11
      Index = Index + 1 %if Event_Event = 12
      -> Next File
   %finish

   Set Terminal Mode (No Page)
   Base File Name = CLI Param
   %while Base File Name = "" %cycle
      Prompt ("File :")
      Read Line (Base File Name)
   %repeat

   Directory = "" %unless Base File Name -> Directory .(":"). Base File Name
   Load Directory (Directory)

   Ptr 1 = 1; Ptr 2 = 1
   %while Ptr 1 <= Number of Files %cycle
      This File = File Names (Ptr 1)
      %if Matches (This File, Base File Name) %start
         %if Base File Name = "*" %start
            Ptr 1 = Ptr 1 + 1 %and %continue %if This File -> A .("."). B
         %finish
         File Names (Ptr 2) = This File
         Ptr 2 = Ptr 2 + 1
      %finish
      Ptr 1 = Ptr 1 + 1
   %repeat

   Number of Files = Ptr 2 - 1
   %if Number of Files = 0 %start
      Print String ("No files matched file specification.")
      New Line
      %stop
   %finish
   Index = 1

Next File:
   %while Index <= Number of Files %cycle
      Print String (File Names (Index)); New Line
      Peruse File (Directory, File Names (Index))
      Index = Index + 1
   %repeat
   New Line
%end %of %program
