!  VAX NLINES program, GDMR/NJR

! Utility to count the number of records in a VMS file
!
! Revision History, in reverse order
!
! 2.1.0  18-Jul-1986 JGH Converted to run from VMS .CLD file
! 

external  string (31) Product Code = "NLINES",
                      Version      = "2",
                      Release      = "1",
                      Revision     = "0"

from  Imp include  Cliparse, RMS, Processes, Log Names, Sys Misc

begin 
   string (127) file
   const  integer  EOF     =    16_1827A,
                   NSC     =    16_10001, 
                   NMF     =    16_182CA,
                   top bit = 16_10000000
   string (15) defnam
   record (FABFM) F = 0
   record (RABFM) R = 0
   record (NAMFM) N = 0
   integer  status, count, files, total
   string (255) junk
   string (255) ES, RS

   if  Qualifier Present ("IDENTIFY") start 
      Print String (Product Code." version ".version.".".release)
      Print String (".".Revision) if  Revision#"0"
      Newline
      return  unless  Qualifier Present ("FILE")
   finish 

   defnam = "*" . translate ("EXTENSION")
   F_bid = FAB BID;  F_bln = FAB BLN
   R_bid = RAB BID;  R_bln = RAB BLN
   R_fab == F
   R_rop = RAB ROP LOC
   F_fop = FAB FOP SQO ! FAB FOP NAM
   F_fac = FAB FAC GET
   F_dna = addr (defnam) + 1;  F_dns = length (defnam)
   F_nam == N
   N_bid = NAM BID;  N_bln = NAM BLN
   N_rsa = addr (RS) + 1;   N_rss = 255
   N_esa = addr (ES) + 1;   N_ess = 255

   File = Qualifier S ("FILE")
   F_fna = addr (file) + 1;  F_fns = length (file)

   status = RMS PARSE (F)
   EXIT (status) if  status & 1 = 0

   total = 0
   files = 0
   cycle 
      status = RMS SEARCH (F)
      exit  if  status = NMF
      EXIT (status) if  status & 1 = 0

      status = RMS OPEN (F)

      if  status & 1 = 0 start 
         length (RS) = N_rsl;  print string (RS);  print string(" - ")
         Print String(Get Message(Status))
         new line
         continue 
      finish 

      if  F_RFM = FAB RFM UDF start 
         Length(RS) = N_RSL ; Print String(RS)
         Print String(" - UDF format files have no lines")
         New Line
         Status = RMS CLOSE(F)
         continue 
      finish 

      status = RMS CONNECT (R)
      if  status&1 = 0 start 
         Print String(Get Message(Status))
         new line
         continue 
      finish 

      length (RS) = N_rsl
      files = files + 1
      count = 0

      count = count + 1 and  status = RMS FIND (R) until  status & 1 = 0
      count = count - 1

      if  status = EOF start 
         print string (RS . " contains")
         write (count, 1)
         print string(" record")
         print symbol('s') unless  count = 1
         new line
      else 
         Print String(Get Message(Status)." - ")
         write(count, 1)
         print string (" lines were counted")
         new line
      finish 

      total = total + count

      status = RMS CLOSE (F)
      EXIT (status) if  status & 1 = 0

   repeat 

   if  files > 1 start 
      print string ("Total of ");   write (total, 0)
      print string (" records")
      new line
   finish 
end