! Filestore port analyser
! MDP Feb'84

%begin

%include "clients.inc"
%include "inc:fs.imp"
%include "inc:util.imp"
!%include "Useful:WhoDat.Inc"

%const %integer Vax Station 1 = 16_03,
                Vax Station 2 = 16_72

%record %format Port Fm (%string (2) Local,
                         %integer Remote Id,
                         %string (2) Remote P,
                         %integer Difference)

%record (Port Fm) %array Ports (1:48)
%integer Number of Ports = 0
%record (User Fm) %array Users (1 : 48)
%integer Number of Users = 0

%string (31) %fn Sleep State (%integer Minutes)
   %const %string (31) %array Comments (0:7) = %c
      "",             "beginning to yawn ",
      "drowsing ",    "dropping off ",
      "asleep ",      "snoring gently ",
      "fast asleep ", "dead to the world "

   Minutes = Minutes // 10
   Minutes = 0 %if Minutes < 0         ;! ???
   Minutes = 7 %if Minutes > 7
   %result = Comments (Minutes)
%end

%Routine Get Table From FS

   %string (80) Line
   %string (19) Read Time, Write Time
   %string (2) Loc P, Rem P, Temp I
   %integer Rem I
   %record (Port Fm) %name Port

   %integer %fn Hex Char (%integer Sym)

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

   %on 9 %start
      -> End Load
   %finish

   Number of Ports = 0
   Open Input (1, "$:Ports")
   Select Input (1)

   %cycle
      Read Line (Line)

      Loc P = Sub String (Line, 2, 3)
      Temp I = Sub String (Line, 6, 7)
      Rem I = Hex Char (Charno (Temp I, 2))
      Rem I = 16 * Hex Char (Charno (Temp I, 1)) + Rem I %if Charno (Temp I, 1) # ' '
      Rem P = Sub String (Line, 9, 10)

      Read Time = Sub String (Line, 13, 26)
      Write Time = Sub String (Line, 29, 42)

      Number of Ports = Number of Ports + 1
      Port == Ports (Number of Ports)

      Port_Local      = Loc P
      Port_Remote Id  = Rem I
      Port_Remote P   = Rem P
      Port_Difference = Minimum Difference (Read Time, Write Time)
   %repeat

End Load:
   Close Input
   Select Input (0)
%end

   %record (Port Fm) %name Port
   %record (User Fm) %name User
   %string (255) Temp Res, Res, Parm = CLI Param
   %string (7) Whos Using, S Pno
   %integer Pno, Uno, Len, Nic

   Get Table From Fs
   Get Unos From Fs (Users, Number of Users, Nic)
   Res = ""

   %for Pno = 1,1, Number of Ports %cycle
      Port == Ports (Pno)
      Res = Port_Local ." ".Hex2 (Port_Remote Id)." ".Port_Remote P." : "

      Whos using = "*Free*"

      %for Uno = 1, 1, Number of Users %cycle
         User == Users (Uno)
         %if User_Pno < 10 %then SPno = I To S (User_Pno, 1) %c
                           %else SPno = I To S (User_Pno, 0)

         %if Port_Local = S Pno %start
            Whos Using = User_User Name
            %exit
         %finish
      %repeat

      Whos Using = " " . Whos Using %while Length (Whos Using) < 6
      Res = Res . Whos Using . " " . Sleep State (Port_Difference)

      %if Vax Station 1 # Port_Remote Id # Vax Station 2 %start
          %if 0 <= Port_Remote Id <= Client Max %c
             %then Res = Res . Where Abouts (Port_Remote Id) %c
             %else Res = Res . "on an uncharted machine"
       %else
          Res = Res . "on ECSVAX"
       %finish

       Format (Res)
       New Line
    %repeat

   Announce ("port", Number of Ports)
   Print String (" allocated with ")
   Write (Number of Users, 0)
   Print String (" in use.")
   New Line
%end %of %program
