%begin; ! Enquire terminal type and install L:<V,W>TLIB

%include "inc:util.imp"
%include "inc:dict.imp"
%externalroutinespec define command symbol(%string(255)s,t)
%externalroutinespec obey command(%string(255)program)

%integer terminal

%constinteger maxdelay=200,esc=27,types=6
%conststring(17)%array codes(1-3:3*types)=-
"",                 "PE550" {Bantam},              "l:btlib",
"/K",               "V200",                        "l:vtlib",
"/Z",               "VT52"  {or WY75-ATS},         "l:vtlib",
"[?1;0c",           "WY75",                        "l:wtlib",
"[?1;2c",           "VT100",                       "l:wtlib",
"[?62;1;2;6;7;8;9c","WY85",                        "l:wtlib",
"[?62;1;2;6;7;8c",  "VT220",                       "l:wtlib"

%string(255)%fn filtered enq(%string(255)s)
%integer k,n
  prompt("")
  set terminalmode(single!noecho)
  k = testsymbol %until k<0
  printsymbol(esc); printstring(s); s = ""
  %cycle
    n = maxdelay
    %cycle
      k = testsymbol&(\128); %exitif k>=0
      n = n-1
    %repeatuntil n<=0
    %exitif k<0
    s = s.tostring(k) %unless k<' '
  %repeat
  set terminalmode(0)
  %result = s
%end

%integerfn terminal type
%string(255)s
%integer i
  s = filtered enq("Z")
  %for i = 1,1,types %cycle
    %result = i %if s=codes(i*3-2)
  %repeat
  s = filtered enq("[c")
  %for i = 1,1,types %cycle
    %result = i %if s=codes(i*3-2)
  %repeat
  %result = 0
%end

%routine define symbol(%string(255)symbol,value)
%integer stag,vtag
  stag = defname(symbol,comdict,8); %returnif stag=0
  vtag = defname(value,fildict,8); %returnif vtag=0
  integer(stag<<1>>1) = vtag<<1>>1
%end

  terminal = terminaltype
  %if terminal=0 %start
    printsymbol(':'); %cycle; %repeatuntil testsymbol>=0
    terminal = terminaltype
  %finish
! %unless terminal=0 %start
    define command symbol("TERMINAL",codes(terminal*3-1))
    obey command("preload ".codes(terminal*3).".mob")
! %finish
%end
