// FtpOS.bcpl - the main program, such as it is
// Copyright Xerox Corporation 1979, 1980, 1982
// Last modified October 2, 1982  2:35 AM by Boggs

get "FtpProt.decl"
get "AltoDefs.d"

external
[
// outgoing procedures
AfterJunta

// incoming procedures
InitFtpOS; BeforeJuntaInit; InitFtpUser
CallContextList; Block; SetTimer; TimerHasExpired
Closes; FlushKeyboard
MoveBlock; StartIO; Min
AddToZone

// incoming statics
ctxQ; sysZone; cliStream; logStream
UserName; UserPassword; defaultPL
lvIdle; lvUserFinishProc
]

static [ savedIdle; savedUserFinishProc ]

structure String [ length byte; char↑1,1 byte ]

//-----------------------------------------------------------------------------------------
let FtpOS(blv, upe, nil) be BeforeJuntaInit(blv, upe, nil)
//-----------------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------------
and AfterJunta() be
//-----------------------------------------------------------------------------------------
[
savedUserFinishProc = @lvUserFinishProc
@lvUserFinishProc = FtpFinishProc
savedIdle = @lvIdle; @lvIdle = Block

InitFtpOS()
AddToZone(sysZone, InitFtpOS, sysZone-InitFtpOS)
InitFtpUser()

CallContextList(ctxQ!0) repeat  //forever
]

//-----------------------------------------------------------------------------------------
and FtpFinishProc() be
//-----------------------------------------------------------------------------------------
// This is the last user finish procedure.
[
if UserPassword>>String.length eq 0 then
   [
   if defaultPL ne 0 & defaultPL>>PL.UNAM ne 0 then
      MoveBlock(UserName, defaultPL>>PL.UNAM,
       Min(defaultPL>>PL.UNAM>>String.length/2+1, UserName!-1))
   if defaultPL ne 0 & defaultPL>>PL.UPSW ne 0 then
      MoveBlock(UserPassword, defaultPL>>PL.UPSW,
       Min(defaultPL>>PL.UPSW>>String.length/2+1, UserPassword!-1))
   ]

@displayListHead = 0
let timer = 0; SetTimer(lv timer, 2)
if logStream ne 0 then Closes(logStream)
if cliStream ne 0 then Closes(cliStream)
until TimerHasExpired(lv timer) loop

@lvIdle = savedIdle
FlushKeyboard()
@lvUserFinishProc = savedUserFinishProc  //rumble rumble
]