// FtpTelnetInit.bcpl - Telnet window for the Alto FTP
// Copyright Xerox Corporation 1979, 1980, 1981, 9182
// Last modified October 10, 1982  10:10 PM by Boggs

get "Streams.d"
get "AltoDefs.d"

external
[
// outgoing procedures
InitFtpTelnet; TelnetTop

// incoming procedures
TelnetKey; TelnetFinishProc
InitializeContext; Block; Enqueue
CreateDisplayStream; CreateKeyStream
Allocate; Free; CharWidth; PutTemplate; Wss
DestroyKeywordTable; DeleteKeyword

// outgoing statics
numTelLines; savedTelnetUFP
telnetDsp; telnetSoc; telnetKeys; telnetCode

// incoming statics
sysZone; ctxQ; lBSPSoc; lvUserFinishProc; debugFlag
sysFont; userDsp; lastShownStream; fontHeight; lineWords
chatKT; kbdKT
]

static
[
numTelLines; savedTelnetUFP
telnetDsp; telnetSoc; telnetKeys; telnetCode
]

//-----------------------------------------------------------------------------------------
let InitFtpTelnet() be
//-----------------------------------------------------------------------------------------
[
let telnetCtx = InitializeContext(Allocate(sysZone, 550), 550, TelnetTop)
let freeSlop = Allocate(sysZone, 1000)
let sizeTelDis = nil; Allocate(sysZone, 32767, lv sizeTelDis)
if sizeTelDis ls 2 * lineWords then
   [
   Free(sysZone, freeSlop)
   Free(sysZone, telnetCtx)
   if telnetCode ne 0 then Free(sysZone, telnetCode)
   Wss(userDsp, "Sorry, there isn't enough memory for a Telnet window.")
   userDsp>>DS.ldcb>>DCB.next = lastShownStream>>DS.fdcb
   DeleteKeyword(kbdKT, "Telnet")
   DestroyKeywordTable(chatKT)
   return
   ]
Enqueue(ctxQ, telnetCtx)
let td = Allocate(sysZone, sizeTelDis)
numTelLines = (telnetDsp>>DS.fdcb>>DCB.height*2)/fontHeight
let ds = CreateDisplayStream(numTelLines, td, sizeTelDis-20, sysFont)
let dcb = @displayListHead; while dcb ne 0 do
   [
   if dcb>>DCB.next eq telnetDsp>>DS.fdcb then
      [
      ds>>DS.ldcb>>DCB.next = telnetDsp>>DS.ldcb>>DCB.next
      dcb>>DCB.next = ds>>DS.fdcb
      Free(sysZone, telnetDsp); telnetDsp = ds
      break
      ]
   dcb = dcb>>DCB.next
   ]
if debugFlag then
   PutTemplate(telnetDsp, "$UD. bitmap words = $UD. full lines",
    sizeTelDis, sizeTelDis/lineWords)

telnetSoc = Allocate(sysZone, lBSPSoc)
telnetKeys = CreateKeyStream(TelnetKey, telnetDsp)
savedTelnetUFP = @lvUserFinishProc
@lvUserFinishProc = TelnetFinishProc
Free(sysZone, freeSlop)
]