! Process killing program
! 1. allows users to delete their own processes which are stuck
! 2. allows priviliged users to delete anyone...
!
! MDP Feb'84
! 
! Command sent to filestore :
! 
! Form : ]<uno><selector>,<uno/xno/pno of victim>
! *** Where selector = 1 for kill UNO
! ***                = 2 for kill XNO
! ***                = 3 for kill PORT

%include "Fmacs:Clients.Inc"
%include "Inc:Util.Imp"
%include "Inc:Fs.Imp"
!%include "Useful:WhoDat.Inc"

! "High-density" hex.  Usually just "normal" hex, except that
! the high order hexit isn't forced to be modulo 16, and the
! sequence goes ...8, 9, :, ;, .... rather than the more
! normal ...8, 9, A, B, ....

%string(2)%fn hdx2(%integer i)
   ! two hexit version
   %result = tostring(i >> 4 + '0') . tostring(i & 15 + '0')
%end

%begin

   %record (User Fm) %array Users (1 : 50)
   %record (User Fm) %name Me, User

   %string (80) I Am = CLI Param
   %integer Number of Users = 0
   %integer Index, Ptr 1, Ptr 2, This Person

   %routine Try to Kill User (%record (User Fm) %name User)

      %string (80) Line
      %integer I

      %on 3, 4, 9 %start
         ! Print String ("Event : ")
         ! Write (Event_Event,2);Write (Event_Sub, 3)
         ! Write (Event_Extra, 3); Space
         Print String (Event_Message)
         New Line
         %return
      %finish

      %cycle
         %if Me_Flags # 0 %start
            Print String (User_User Name)
            Spaces (7 - Length (User_User Name))
         %finish

         Write (User_Uno, 2)
         Spaces (1 + 2)
         PHex 2 (User_Pno)
         Spaces (1 + 0)
         PHex 2 (User_Location)
         Print String (" : ")

         %if User_Location <= Client Max %c
            %then Print String (Whereabouts (User_Location)) %c
            %else Print String ("on an uncharted machine")
         Space
         Print String (" (you)") %if User_User Name = I Am %c
                                %and User_Uno       = UserNo %c
                                %and User_Location  = Ether Station
         Space
         Read Line (Line)
         %return %if Line = ""
         Upper Case (Line)
         %return %if 'K' # Charno (Line, 1) # 'Y'
         %if Me_Flags # 0 %and User_User Name # I Am %start
            Print Line ("Privileged users type 'KILL' or 'YES' to kill other processes")%c
               %and %continue %if "KILL" # Line # "YES"
         %finish
         Line = "1," . Hdx2 (User_Uno)
         I = FComm (']'<<8 + UserNo + '0', Line)
         %exit
      %repeat
   %end
   
   I Am = Current User
   %if I Am = "" %start
      Print String ("SUICIDE can only be used when logged on, you aren't.")
      New Line
      %stop
   %finish

   Get Unos from FS (Users, Number of Users, Index {Internal copies})

   This Person = 0
   Index       = 1

   %while Index <= Number of Users %cycle
      User == Users (Index)
      %if User_Uno = UserNo %start
         %if This Person # 0 %start
            Print Line ("There is more than one of 'YOU' logged onto this terminal, will take")
            Print Line ("the first one as 'YOU'.")
            %exit
         %else
            This Person = Index
         %finish
      %finish
      Index = Index + 1
   %repeat

   %if This Person = 0 %start
      Print String ("This Person = 0 : Catastrophic Error...Tell 'USEFUL'")
      New Line
      %stop
   %finish

   Me == Users (This Person)
   %if Me_Uno # UserNo %start
      Print String ("Me_Uno # UserNo, Tell USEFUL.....")
      New Line
      %stop
   %finish

   %if Me_Flags = 0 %start                          ;! non privileged
      Ptr 1 = 1
      Ptr 2 = 1                                     ;! so remove dross
      %while Ptr 1 <= Number of Users %cycle
         User == Users (Ptr 1)
         %if User_User Name = I Am %start
            Users (Ptr 2) = User
            Ptr 2 = Ptr 2 + 1
         %finish
         Ptr 1 = Ptr 1 + 1
      %repeat

      Number of Users = Ptr 2 - 1
   %finish

   Index = 1
   Print Line ("Uno Port Id : Location") %if Number of Users # 0
   %while Index <= Number of Users %cycle
      Try to Kill User (Users (Index))
      Index = Index + 1
   %repeat
%end %of %program
