%own %string (31) Sys Time = "",
                  Sys Date = ""

%external %routine Upper Case %alias "USE_UpCase" (%string(*) %name it)
   to upper(it)
%end

%external %routine Lower Case %alias "USE_LowCase" (%string(*) %name it)
   to lower(it)
%end

%external %integer %fn Find Index %alias "USE_FindIdx" (%string (7) who)
   %integer i

   %for i = 1, 1, max fs users %cycle
      %if fs users (i) = who %start
         %result = i
      %finish
   %repeat

   %result = Max Fs Users + 1
%end

%external %string (255) %fn Current Directory %alias "USE_CurrDir"
   %string (255) Me
   %integer I = 1

   Length (Me) = Fcommr ('F'<<8, "", Charno (Me, 1), 255)
   I = I + 1 %while Charno (Me, I) # ' '
   Length (Me) = I - 1
   %result = String (Addr (Me))
%end

%external %string (255) %fn Current User %alias "USE_CurrUser"
   %string (7) This Dir, This User
   %integer I

   %on 9 %start
      %result = ""
   %finish

   This Dir  = Current Directory
   I = Fcomm ('J'<<8,"")
   This User = Current Directory
   I = Fcomm ('J'<<8, This dir)
   %result = String (Addr (This User))
%end

%external %string (2) %fn Hex 2 %alias "USE_Hex2" (%integer xx)

   %const %byte %array Hex Sym (0 : 15) = %c
      '0', '1', '2', '3', '4', '5', '6', '7',
      '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
   %string (2) Frig = To String (Hex Sym ((Xx >> 4) & 15)) . %c
                      To String (Hex Sym (Xx & 15))

   %result = String (Addr (Frig))
%end

%external %routine Format %alias "USE_Format" (%string (255) Line)
   %string (80) This

   %cycle
      %if Length (Line) > 80 %start
         This = Sub String (Line, 1, 80)
         Line = Sub String (Line, 81, Length (Line))
      %else
         This = Line
         Line = ""
      %finish
      Print String (This)
      New Line %if Line # ""
   %repeat %until Line = ""
%end

! -- Wildcard stuff from EFTP

%integerfn 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

%external %predicate Matches %alias "USE_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

%external %routine Announce %alias "USE_Announce" %c
   (%string (80) What, %integer Count)

   Write (Count ,0)
   Space
   Print String (What)
   Print Symbol ('s') %if Count # 1
%end

%integer %fn Day Difference (%string (7) Lower, Upper)

   %string (2) Lo Hrs = Sub String (Lower, 1, 2),
               Lo Min = Sub String (Lower, 4, 5)
   %string (2) Hi Hrs = Sub String (Upper, 1, 2),
               Hi Min = Sub String (Upper, 4, 5)
   %integer Total

   %if Lo Hrs = Hi Hrs %start
      Total = StoI (Hi Min) - StoI (Lo Min)
   %else
      Total = 60 * (S to I (Hi Hrs) - S to I (Lo Hrs))
      Total = Total + (S to I (Hi Min) - S to I (Lo Min))
   %finish
   %result = Total
%end

%integer %fn Time Difference (%string (31) Time Rating)

   %string (15) Day
   %string (7)  Tim

   %if Time Rating -> Day .(" "). Tim %start; %finish
   Tim = Sub String (Tim, 2, Length (Tim)) %while Charno (Tim, 1) = ' '

   %if Sys Date = Day %start
      %result = Day Difference (Tim, Sys Time)
   %else
      %result = 24 * 60 - Day Difference (Sys Time, Tim)
   %finish
%end

%external %integer %fn Minimum Difference %alias "USE_MinDiff" %c
   (%string (31) One, Two)

   %integer I One,
            I Two

   Sys Time = Time %if Sys Time = ""
   Sys Date = Date %if Sys Date = ""

   I One = Time Difference (One)
   I Two = Time Difference (Two)

   %result = I One %if I One < I Two
   %result = I Two
%end

%record %format User Fm (%string (6) User Name, Directory,
                         %string (21) Login Time,
                         %byte Uno, Pno, Flags, Location,
                         %short Difference)

%external %routine Get Unos From FS %alias "USE_GFsUnos" %c
   (%record (User Fm) %array %name Users (1 : *),
    %integer %name Number of Users,
    %integer %name Number of Internal Copies)   ;! -- for Users

   %record (User Fm) %name Element
   %string (19) User, Dir, Login
   %string (19) Read Time, Write Time
   %string (8) Today = Date
   %integer Id, Sym, Flag, Uno, Pno
   %integer nl flag = 0
   %integer tptr,tlen,taddr

   %routine Read Sym (%integer %name sym)

      %signal 9 %if tptr = taddr+tlen
      sym = byteinteger(tptr)
      tptr = tptr+1
   %end
         
   %routine Skip Symbol
      %integer sym

      read sym (sym)
   %end

   %integer %fn Hex Sym (%integer Sym)

      %result = Sym - '0' %if '0' <= Sym <= '9'
      %result = Sym - 'A' + 10
   %end

   %routine Read Rest of (%string (19) %name What)

      %integer Sym

      %cycle
         Read Sym (Sym)
         nl flag = 1 %and Sym = ' ' %if sym = nl
         %exit %if Sym = ' '
         What = What . To String (Sym)
      %repeat
   %end

   %routine Skip Spaces (%integer %name Sym)

      Sym = ' '
      Read Sym (Sym) %while Sym = ' '
   %end

   %routine Read Date (%string (*) %name Dat, %integer %name Sym)

      Dat = To String (Sym)
      Read Rest of (Dat)
      Dat = Dat ." "
      Skip Spaces (Sym)
      Dat = Dat . To String (Sym)
      Read Rest of (Dat)
   %end

   %on 9 %start
      -> End Load
   %finish

{ Print String ("In Get FS Unos..."); New Line
   Sys Time = Time
   Sys Date = Date

   Number of Users = 0
   Number of Internal Copies = 0
   Connect File ("$:UNOS",0,taddr,tlen)
{ Print String ("File connected at addr = "); PHex (taddr)
{ Print String (" to of len = "); PHex (TLen); New Line
   tptr = taddr
   
   Read Sym (Sym) %until Sym = Nl      ;! skip ANON

! 6  -2           pub     pub  ---login------  -----read-----  -----write----
   %cycle
      Flag = 0
      Skip Symbol                      ;! leading space

      ! -- Uno

      Read Sym (Sym)
      Read Sym (Uno)
      %if Sym # ' ' %then Uno = 10 * (Sym - '0') + (Uno - '0') %c
                    %else Uno = Uno - '0'

      ! -- 2 Spaces

      Skip Symbol
      Skip Symbol

      ! -- Port Number

      Read Sym (Sym)
      %if Sym = '-' %start
         Number of Internal Copies = Number of Internal Copies + 1
         Read Sym (Sym) %until Sym = Nl
         %continue
      %finish

      Read Sym (Pno)
      %if Sym # ' ' %then Pno = 10 * (Sym - '0') + (Pno - '0') %c
                    %else Pno = Pno - '0'

      ! -- 2 Spaces

      Skip Symbol
      Skip Symbol

      ! -- Terminal Address

      Read Sym (Sym)
      Read Sym (Id)
      Id = 16 * Hex Sym (Sym) + Hex Sym (Id)

      ! -- 3 Spaces

      Skip Symbol %for Sym = 1, 1, 3      ;! skip upto spaces before user name

      ! -- Username

      Skip Spaces (Sym)
      User = To String (Sym)
      Read Rest of (User)

      %if Charno (User, Length (User)) = '!' %start
         Flag = 1
         Length (User) = Length (User) - 1
      %finish

      Skip Spaces (Sym)
      Dir = To String (Sym)
      Read Rest of (Dir)

      Skip Spaces (Sym)
      Login = To String (Sym)
      Read Rest of (Login)
      Skip Spaces (Sym)
      %if Login # Today %then Login = Login . " " . To String (Sym) %c
                        %else Login = To String (Sym)
      Read Rest of (Login)

      Skip Spaces (Sym)
      Read Date (Read Time, Sym)

      Skip Spaces (Sym)
      Read Date (Write Time, Sym)
      %if nl flag = 0 %start
         Read Sym (Sym) %until Sym = Nl
      %finish

      Number of Users    =  Number of Users + 1
      Element            == Users (Number of Users)
      Element_Uno        =  Uno
      Element_Pno        =  Pno
      Element_User Name  =  User
{ Print String ("Read user '".User."'");New Line
      Element_Directory  =  Dir
      Element_Login Time =  Login
      Element_Location   =  Id
      Element_Flags      =  Flag
      Element_Difference =  Minimum Difference (Read Time, Write Time)
   %repeat

End Load:
{ Print String ("Leaving Get FS Unos..."); New Line
   Sys Time = ""
   Sys Date = ""
%end

%end {%of %file
