! NLINES (or SIZES)
! -- JGH Hacked it in May 1983
! -- MDP reHacked it in May 1984

%include "I:Util.Inc"

%begin
   %string (80) %array Files (1 : 110)
   %integer Number of Files
   %string (127) File, File 1
   %integer I, cc, lc, sym
   %integer File Number = 0, Wild Lines = 0, Wild Chars = 0

   %routine open file (%string (80) File)
      %on 9 %start
          Print string ("NLINES fails - ".event_message)
          Newline
          %return
      %finish
      open input (1, file)
      select input (1)
   %end

   ! -- Wildcard stuff from EFTP

   %integer %fn wildness (%string (*) %name s)
      %integer k,i,w=0

      %for i=1, 1, length (s) %cycle
         k = charno(s,i)
         w = w+1 %if k='*' %or k='%'
      %repeat
      %result = w
   %end

   %predicate Matches (%string (*) %name s, p)
   ! S=Subject, P=Pattern. Pattern is the one with the stars in it.
      %integer slen = 0, plen = 0

      %predicate m (%integer spos,ppos)
         %integer psym = 0,ssym = 0

         %cycle
            %if ppos=plen %start
               %true %if spos=slen
               %false
            %finish
            ppos = ppos+1; psym = charno(p,ppos)
            %exitif psym='*'
            %falseif spos=slen
            spos = spos+1; ssym = charno(s,spos)
            psym = ssym %if ssym!32=psym!32 %and 'a'<=psym!32<='z'
            %unless ssym=psym %start
               %falseunless psym='%'
            %finish
         %repeat

         %cycle
            %trueif m(spos,ppos)
            %exitif spos=slen
            spos = spos+1; ssym = charno(s,spos)
         %repeat
         %false
      %end

      slen = length(s)
      plen = length(p)
      %true %if m(0,0)
      %false
   %end

   %routine Do one File (%string (80) File)

      %routine output results

         Print string (file)
         print string (" contains ")
         write (cc, 0)
         print string (" character")
         print symbol ('s') %if cc # 1
         print string (", on ")
         write (lc, 0)
         print string (" line")
         print symbol ('s') %if lc # 1
         print symbol ('.')
         newline
         Wild Lines = Wild Lines + Lc
         Wild Chars = Wild Chars + Cc
      %end

      %on 9 %start
         output results
         %return
      %finish

      File Number = File Number + 1
      cc = 0
      lc = 0
      Open File (File)

      %cycle
         read symbol (sym)
         cc = cc + 1
         lc = lc + 1 %if sym = nl
      %repeat
   %end

   %routine Do Wild Files (%string (80) Wild File)
      %string (127) Line, A, B, Directory
      %integer Flag = 0, Index


      %on 9 %start
         %if Flag = 0 %start
            Event_Message = "Rainer stole event_message again" %if Event_Message = ""
            Print String ("NLINES : ".Event_Message)
            New Line
            %return
         %finish
         -> End Load
      %finish

      Directory = ""
      %if Wild File -> Directory .(":"). Wild File %start; %finish
      Directory = Directory . ":" %if Directory # ""
      Open Input (3, Directory."Directory")
      Select Input (3)
      Flag = 1

      Index = 1
      Number of Files = 0
      %cycle
         Read Line (Line)
         %if Matches (Line, Wild File) %start
            %if Wild File = "*" %start
               %continue %if Line -> A .("."). B
            %finish
            Number of Files = Number of Files + 1
            Files (Number of Files) = Directory.Line
         %finish
         Index = Index + 1
      %repeat

End Load:
      Close Input
      Select Input (0)

      Index = 1
      %while Index <= Number of Files %cycle
         Do One File (Files (Index))
         Index = Index + 1
      %repeat
   %end

   File = CLI Param
   prompt ("$_file:")
   Read Line (File) %while File = ""

   %for I = 1, 1, length (File) %cycle
      Charno (File, I) = Charno (File, I) - ' ' %if 'a' <= Charno (File, I) <= 'z'
   %repeat

   %while File -> File 1 .(","). File %cycle
      %if Wildness (File 1) # 0 %then Do Wild Files (File 1) %c
                                %else Do One File (File 1)
   %repeat

   %if File # "" %start
      %if Wildness (File) # 0 %then Do Wild Files (File) %c
                                %else Do One File (File)
   %finish

   %if File Number > 1 %start
      Print String ("Total of ")
      Write (Wild Chars, 0)
      Print String (" character")
      Print Symbol ('s') %if Wild Chars # 1
      Print String (" on ")
      Write (Wild Lines, 0)
      Print String (" line")
      Print Symbol ('s') %if Wild Lines # 1
      Print Symbol ('.')
      New Line
   %finish
%end %of %program
