Unit MiscComm ;
Interface
  Uses Dos,          (* Standard Turbo Pascal Unit *)
       KGlobals,     (* Kermit Globals *)
       ModemPro,
       Packets,
       Setshow,
       Vt100 ;
     Procedure Logit  (filename : String);
     Procedure Takeit  (filename : String);
     Procedure QuitExit  (QuitOption : String);
Implementation
(* ================================================================== *)
(* LOGIT - creates a Log file to record all incoming data from the    *)
(*       remote line.                                                 *)
(*           The file name is specified in the Parameter .            *)
(*               if no parameter specified logging is turned off.     *)
(* ================================================================== *)
Procedure Logit  (filename : String);
Begin (* Logit Procedure *)
If (length(filename) < 3) or (filename='OFF') then
    Begin (* Turn off Logging *)
    Logging := false ;
    {$I-} Close (Logfile); {$I+}
    If IoResult = 0 then
         Writeln (' Logging is turned off ');
    End   (* Turn off Logging *)
                        else
    Begin (* Turn on Logging *)
    If Logging then Close (Logfile);
    Logging := True ;
    Assign(Logfile,Filename);
    {$I-} Rewrite(Logfile); {$I+}
    If IoResult = 0 then
        Writeln(' Logging data to file ',filename)
                    else
        Writeln(' Unable to open file',filename,' for Logging') ;
    LogName := filename ;
    End ; (* Turn on Logging *)
End ; (* Logit Procedure)

(* ================================================================== *)
(* Takeit - read commands from a file and executes them.              *)
(*          if no file specified or file is not there if does nothing *)
(* ================================================================== *)
Procedure Takeit  (filename : String);
Var FileInfo : SearchRec ;
Begin (* Takeit Procedure *)
If length(filename) > 1 then
    FindFirst(filename,anyfile,FileInfo) ;
     If DosError = 0 then
         Begin (* Active file *)
         Writeln ('Activating Command file ',filename);
         TakeActive := true ;
         Assign(Commandfile,filename);
         Reset(Commandfile);
         End  (* Active file *)
                                else
         Writeln('No file ',filename) ;
End ; (* Takeit Procedure)

(* ================================================================== *)
(* QuitExit    - Terminates the KERMIT.                               *)
(*             the QuitOptions are:                                   *)
(*                  LOCAL,REMOTE,DISCONnect,ALL                       *)
(*               if LOCAL or noparms only the local kermit terminates.*)
(*               if REMOTE then  only the remote kermit terminates.   *)
(*               if DISCONect then the remote kermit is terminated    *)
(*                       and the remote is logged off.                *)
(*               if ALL  then both kermits are terminated and remote  *)
(*                        is logged off.                              *)
(*                                                                    *)
(* ================================================================== *)
Procedure QuitExit  (QuitOption : String);
Const
    QuitTable : String[35] = '       LOCAL  REMOTE DISCON ALL    ' ;
Type QuitType = (zero,local,remote,discon,all);
Var
    Qix : integer  ;
Begin (* QuitExit Procedure *)
QuitOption := Uppercase(Concat(' ',QuitOption));
Qix := Pos(QuitOption,QuitTable) div 7 ;
Case QuitType(Qix) of    (* Quit Type *)
 zero,
 local:  Running := false ;
 remote :
         Begin (* terminate remote kermit *)
        (* Send a Finish packet *)
         OutDataCount := 1 ;
         OutSeq := OutSeq + 1 ;
         If OutSeq > 64 then OutSeq := 0 ;
         OutPacketType := Ord('G');
         SendData[1] := Ord('F');
         WaitXon := False ;
         SendPacket ;
         If RecvPacket and (InPacketType = Ord('Y')) then
               Writeln (' Remote Kermit terminated. ')
                                                     else
               Writeln(' Unable to terminate Remote Kermit. ');
         End ; (* terminate remote kermit *)
discon,
all:
         Begin (* logoff Remote  *)
         (* Send a Logoff packet *)
         OutDataCount := 1 ;
         OutSeq := OutSeq + 1 ;
         If OutSeq > 64 then OutSeq := 0 ;
         OutPacketType := Ord('G');
         SendData[1] := Ord('L');
         WaitXon := false ;
         SendPacket ;
         If RecvPacket and (InPacketType = Ord('Y')) then
              Writeln (' Remote host is logging off ')
                                                     else
              Writeln(' Remote host unable to execute a log off ');
         If (Qix = Ord(all))  then Running := False ;
         End;  (* Logoff Remote *)
    End ; (* Case Quit Type *)
End; (* QuitExit Procedure *)
Begin (* MiscComm Unit *)
End.  (* MiscComm Unit *)
