uses dos,crt,genstr,records,init,filehandle,strl,IO;

Const maxbars=50;

Type PullModerec = Record
                    OnString,OffString : Array[1..maxbars] of String[40];
                    X           : Array[1..Maxbars] of 1..80;
                    Y           : Array[1..Maxbars] of 1..25;
                    Choices     : 1..Maxbars;
                    pointercmd  : Array[1..maxbars] of 1..50;
                    FirstOption : 1..Maxbars;
                   end;

var protf   : file;
    prot    : array[1..30] of  protorec;
    current : byte;
    dummy   : byte;

Procedure print(S : String);
Var x,y:integer;
    r:string;
    g:integer;
    ff:byte;
begin
g:=1;
r:='';
y:=0;
ff:=0;
for X:=1 TO LENGTH(S) DO
   BEGIN
      If ff >= wleng then Exit;
   case S[X] OF '|':
      BEGIN
        case S[X+1] OF 'B':
           BEGIN
           Y:=VALU(S[X+2]);
           CRT.TEXTBACKGROUND(Y);
           INC(X,2);
           END ELSE
          BEGIN
          R:=S[X+1]+S[X+2];
          Y:=VALU(R);
          CRT.TEXTCOLOR(Y);
          INC(X,2);
          END;
      END;
   END ELSE begin
            WRITE(S[X]);
            inc(ff);
            end;
   END;
   END;
END;

Function Pullbars(Pull:pullmoderec):integer;
var On:1..maxbars;
    ch:char;

Procedure Write_Options;
Var dummy:1..maxbars;
Begin
 For dummy := 1 to pull.choices do
  begin
   gotoxy(pull.x[dummy],pull.y[dummy]);
   print(pull.offstring[Dummy]);
  end;
  on:=pull.firstoption;
  gotoxy(pull.x[on],pull.y[on]);
  print(pull.onstring[on]);
End;

procedure Update_Options(Down:boolean); {true:forward/false:backwards}
Begin
 gotoxy(pull.x[on],pull.y[on]);
 print(pull.offstring[on]);
 If Down then
  Begin
   If On < Pull.choices then Inc(on) else On := 1;
  End else
   Begin
    If On > 1 then Dec(On) else On := Pull.Choices;
   End;
 gotoxy(pull.x[on],pull.y[on]);
 print(pull.onstring[on]);
End;

Function Get_Input : Byte;
Var CH : Char;
Begin
 CH := ReadKey;
 If CH = #0 then CH :=Readkey;
 if ch = #27 then begin
                  ch:=readkey;
                  if ch='[' then ch:=readkey;
                  end;
 Case CH of
  'A' : Get_Input := 8;
  'B' : Get_Input := 2;
  #75 : Get_Input := 8;
  #77 : Get_Input := 2;
  #72 : Get_Input := 8;
  #80 : Get_Input := 2;
  #13 : Get_Input := 5;
 End;
End;

Var Key : Byte;

begin
 On := 1;
 Write_Options;
  Repeat
   Key := Get_input;
   Case Key of
    8 : update_options(False);
    2 : update_options(True)
   End;
  Until (Key = 5);
  Pullbars := On;
end;

Procedure DisplayADF2(Filename : String);
Var    X : word;
 NumRead : Integer;
       F : File;
 ADF_Version  : Char;
 ADF_Pallette : Array[0..63,1..3] of Byte;
 ADF_Font     : Array[1..4096] of Char;
 ADF_Handle   : File;
 Regs : Registers;
Begin
 asm mov ah,1;mov cx,2000h; int 10h end;  { No Cursor }
 Assign(F,Filename);
 Reset(F,1);
 Seek(F,0);
 BlockRead(F,ADF_Version,1);
 BlockRead(F,ADF_Pallette,Sizeof(ADF_Pallette));
 BlockRead(F,ADF_Font[1],4096);
 For X := 0 to 63 do
  Begin
   Port[$3c8] := X;
   Port[$3c9] := ADF_Pallette[X,1];
   Port[$3c9] := ADF_Pallette[X,2];
   Port[$3c9] := ADF_Pallette[X,3];
  End;
 Seek(F,4289);
 regs.bx:= 4096;
 regs.es:= seg(ADF_Font);
 regs.bp:= ofs(ADF_Font);
 regs.ax:= 4368;
 regs.cx:= 256;
 regs.dx:= 0;
 Intr(16, Regs);
 asm mov ah,1;mov cx,2000h; int 10h end;  { No Cursor }
 BlockRead(F,Mem[$b800:0000],Filesize(F)-4289);
 Close(F);
End;

procedure showprot(v:protorec);
begin
wleng:=15;
gotoxy(53,2);
print('|B1|15'+v.description);
wleng:=30;
gotoxy(48,3);
print('|B1|15'+v.ul);
wleng:=30;
gotoxy(48,4);
print('|B1|15'+v.dl);
wleng:=2;
gotoxy(48,5);
print('|B1|15'+v.key);
wleng:=30;
gotoxy(48,6);
print('|B1|15'+v.log);
gotoxy(40,11);
print(strr(current));
gotoxy(43,11);
print('|B1|1530');
end;

procedure doedit;
var p:pullmoderec;
    x:byte;
begin
wleng:=255;
fillchar(p,sizeof(p),#0);
p. onstring[1]:='|B4|15 Protocol Name ';
p.offstring[1]:='|B1|15 Protocol Name ';
p.x[1]:=37;
p.y[1]:=2;
p. onstring[2]:='|B4|15 Receive  ';
p.offstring[2]:='|B1|15 Receive  ';
p.x[2]:=37;
p.y[2]:=3;
p. onstring[3]:='|B4|15 Send     ';
p.offstring[3]:='|B1|15 Send     ';
p.x[3]:=37;
p.y[3]:=4;
p. onstring[4]:='|B4|15 Key      ';
p.offstring[4]:='|B1|15 Key      ';
p.x[4]:=37;
p.y[4]:=5;
p. onstring[5]:='|B4|15 Log File ';
p.offstring[5]:='|B1|15 Log File ';
p.x[5]:=37;
p.y[5]:=6;
p. onstring[6]:='|B4|15 Done ';
p.offstring[6]:='|B1|15 Done ';
p.x[6]:=37;
p.y[6]:=7;
p.choices:=6;
p.firstoption:=1;
repeat
WLENG:=255;
x:=pullbars(p);
WLENG:=255;
PRINT('|B1|15');
SHOWCURSOR;
case x of
 1:edstr(prot[current].description,53,2,25,15,7,25);
 2:edstr(prot[current].ul         ,48,3,30,15,7,90);
 3:edstr(prot[current].dl         ,48,4,30,15,7,90);
 4:edstr(prot[current].key        ,48,5,2,15,7,2);
 5:edstr(prot[current].log        ,48,6,30,15,7,255);
 end;
HIDECURSOR;
showprot(prot[current]);
P.FIRSTOPTION:=X;
until x=6;
end;

var c:char;
    fp:longint;
begin
read_in_config;
textattr:=7;
clrscr;
nassign(protf,config.data_dir+'protocol.dat');
dummy:=0;
current:=0;
fillchar(prot,sizeof(prot),#0);

if filesize(protf)<>0 then
repeat
inc(dummy);
nread(protf,prot[dummy],sizeof(prot[dummy]));
until eof(protf);

current:=1;
displayadf(config.lucid_dir+'prot.adf');
repeat
delay(10);
displayadf2('prot.adf');
showprot(prot[current]);
c:=readkey;

case c of
#77:inc(current);
#75:dec(current);
#13:doedit;
end;
if current=31 then current:=1 else
if current=0 then current:=30;
until c=#27;
nreset(protf);
nwrite(protf,prot,sizeof(prot));
end.