(* when a modem call is done, does either async or fossil call *)
unit protcomm;
interface

uses dos,crt,fossil,async;

function  dataavailable:boolean;
Function  GetChar : Byte;
Function  Carrier : Boolean;
Procedure AWrite   (S : String);
Procedure AWriteLn (S : String);
Function  Comm_Init (Baud : LongInt;  ThePort : Byte) : Boolean;
function  sendinit(s:string):boolean;
procedure deinit;
procedure hangup;
procedure flushin;
procedure flushout;

var baud      :  longint;
    comport   :  byte;
    usefossil :  boolean;
    telnet    :  boolean;

implementation

uses records;


procedure flushin;
begin
if comport=0 then exit;
if usefossil=true then fossil.flushinbuffer else
                       async .AsyncPurgeInput;
end;

procedure flushout;
begin
if comport=0 then exit;
if usefossil=true then fossil.flushoutbuffer else
                       async .AsyncFlushOutput;
end;

procedure deinit;
begin
if comport=0 then exit;
if usefossil=true then fossil.comm_deinit else
                       async .modemdeinit;
end;

procedure hangup;
begin
if comport=0 then exit;
if usefossil=true then begin;
                       fossil.comm_DTR(false);
                       repeat
                       until fossil.carrier=false;
                       fossil.comm_DTR(true);
                       delay(100);
{                      fossil.flushinbuffer;}
                       end else
                       sendinit(config.hangup);

end;

function dataavailable:boolean;
begin
if (comport=0) or (localon) then begin; dataavailable:=false; exit; end;
if usefossil=true then dataavailable:=fossil.dataavailable else
                       dataavailable:=async .dataavailable;
end;

Function  GetChar : Byte;
begin
if (comport=0) or (localon) then begin; getchar:=0; exit; end;
if usefossil=true then getchar:=fossil.getchar else
                       getchar:=async .getchar;
end;

Function Carrier : Boolean;
begin
if (comport=0) or (localon) then begin; carrier:=false; exit; end;
if usefossil=true then carrier:=fossil.carrier else
                       carrier:=async .carrier;
end;

Procedure AWrite (S : String);
begin
if (comport=0) or (localon) then exit;
if usefossil=true then fossil.awrite(s) else
                       async .awrite(S)
end;

Procedure AWriteLn (S : String);
begin

if (comport=0) or (localon) then exit;
if usefossil=true then fossil.awriteln(s) else
                       async .awriteln(S);
end;

Function  Comm_Init (Baud : LongInt;  ThePort : Byte) : Boolean;
begin
if comport=0 then begin;
                  comm_init:=true;
                  exit;
                  end;
fossil.comm_port:=theport;
async .usedport :=theport;
if usefossil=true then comm_init:=fossil.comm_init(baud,theport) else
                       comm_init:=async .comm_init(baud,theport);

end;

function sendinit(s:string):boolean;
var x:integer;
begin
if comport=0 then begin; sendinit:=true; exit; end;
for x:=1 to length(s) do begin
if s[x]='|' then protcomm.awrite(#13) else
if s[x]='~' then delay(500)  else
                 protcomm.awrite(s[x]);
end;
end;

begin
end.

