// GatewayInit.bcpl -- initialization for Gateway.bcpl

// Last modified February 9, 1979  4:04 PM by Boggs

get "Streams.d"

external
[
// outgoing procedures
GatewayInit

// incoming procedures
CreateMiscServ; CreateEchoServ; InitPupLevel1
CreateGateConServ; CreateStatServ; ReadGateParam
InitEFTPPackage; InitNovaAlto
InitializeZone; AddToZone; Allocate; Zero
InitializeContext; Block; MyFrame; Enqueue
EnterDebugger; DosBreak; BreakLoc; CallDos; MakeDosString
OpenFile; Gets; Closes; Endofs; CallSwat
Puts; Ws; Wss; Wns

// outgoing statics
ctxQ; preserveDebugger

// incoming statics
sysZone; dosAc; dsp
]

static
[
ctxQ
sawCR = false
preserveDebugger = false
]

manifest
[
stackEnd = 335b
exec = 63400b
pchar = 70000b
gatewayQuitCode = 12345  //Dos ".ertn" code to signal normal Quit
]

//----------------------------------------------------------------------------
let GatewayInit(blv) be
//----------------------------------------------------------------------------
[
dsp = lv DosDspPuts - offset ST.puts/16
if @406b ne -1 & @BreakLoc eq 401b then
   [
   Ws("No breakpoint*N*Lto make a break file type:*N*L   ")
   Wns(dsp, BreakLoc, 1, 8); Ws("$B*N*L   ")
   Wns(dsp, DosBreak, 1, 8); Ws("$R*N*L")
   Ws("Then type: SAVE GATEWAY <CR>*N*LEntering debugger...")
   @404b = blv!29  //reset NMAX to top of code
   EnterDebugger()
   ]

let codeEnd = blv!29
let freeEnd = @stackEnd
let freeBegin = MyFrame() +200
@stackEnd = freeBegin
sysZone = InitializeZone(freeBegin, freeEnd-freeBegin)
ctxQ = Allocate(sysZone, 2); ctxQ!0 = 0

// If /I global switch is present, then we are the "inferior"
// gateway program and we should go ahead and run.  If /I is not
// present, then we should write a COM.CM with /I set and overlay
// the gateway program recursively.
// If /D global switch is present, set the preserveDebugger flag.
let str = OpenFile("COM.CM", ksTypeReadOnly, charItem)
until Gets(str) eq 0 loop  //skip over subsystem name
if (Gets(str) & #20) ne 0 then preserveDebugger = true  // test /D
let inferiorGateway = (Gets(str) & #200) ne 0  // test /I
Closes(str)

unless inferiorGateway do
   [  //we are the "superior".  Do recursive overlay
   Ws("*n*l**** Gateway restarting *****n*l")
   str = OpenFile("COM.CM", ksTypeWriteOnly, charItem)
   Wss(str, "GATEWAY*000")
   Puts(str, (preserveDebugger? #20, 0))  // set /D if appropriate
   Wss(str, "*200*000*000*377")  // set /I, terminate COM.CM
   Closes(str)
   let s = vec 5
   s = MakeDosString("GATEWAY.SV", s)
   CallDos(exec, s, 0, -1)
   if dosAc!2 eq gatewayQuitCode finish  //normal "Quit"
   ] repeat  //crash or forced restart

// GatewayInit (cont'd)

InitNovaAlto(sysZone)
InitPupLevel1(sysZone, ctxQ, 0)
InitEFTPPackage(sysZone)
CreateEchoServ(sysZone, ctxQ)
CreateMiscServ(sysZone, ctxQ)
CreateStatServ(sysZone, ctxQ)
CreateGateConServ()
ReadGateParam()	// must FOLLOW server initialization

unless preserveDebugger % @406b eq -1 do
   [
   //flush the debugger.
   //We know that @406b is a pointer to the first word of
   //the debugger, that BUTIL is the next thing loaded after
   //it, and that the load vector is the first thing in BUTIL.
   AddToZone(sysZone, @406b, blv-@406b)
   @406b = -1
   ]
]


//----------------------------------------------------------------------------
and DosDspPuts(nil, char) be
//----------------------------------------------------------------------------
[
if sawCR & char ne $*L then CallDos(pchar, $*L)
CallDos(pchar, char)
sawCR = char eq $*N
]