// PupTestParams.bcpl
// Copyright Xerox Corporation 1979, 1982
// Last modified February 15, 1982  6:06 PM by Boggs

get "Pup.decl"
get "PupTest.decl"

external
[
//outgoing procedures
PupTestParams

//incoming procedures
MyFrame; GotoFrame; CallSwat
OpenFile; Closes; Endofs; Gets; Puts
HInsert; Ws; PutTemplate

//incoming statics
dsp; pupRT; ndbQ
]

manifest
[
maxAtomChars = 30
numAtomWords = maxAtomChars rshift 1 +1
]

static [ paramFile; inComment; end; buffer ]

//----------------------------------------------------------------------------
let PupTestParams() be
//----------------------------------------------------------------------------
[
paramFile = OpenFile("GATEPARAMETER.TXT", ksTypeReadOnly, charItem)
test paramFile eq 0
   ifso Ws("*NNo gateway parameter file")
   ifnot
      [
      let v = vec numAtomWords; buffer = v
      end = MyFrame()
      inComment = false
      InterpretParam()
      Closes(paramFile)
      ]
//If we have no param file, but do have an SLA, set the net to 7
// and the host to our Ethernet address mod 17b.  The idea is to pick
// a host number with a high probability of being unique.
CreateRTE(netTypeSLA, 7, (ndbQ!0)>>NDB.localHost & #17)
]

//----------------------------------------------------------------------------
and InterpretParam() be
//----------------------------------------------------------------------------
[
let atom = ReadAtom()
switchon atom>>String.char↑1 into
   [
   case $H: case $h:  //HOST <netType> <device code> <net> <host>
      [
      let netType = ReadNumber()
      let deviceNumber = ReadNumber()
      let net = ReadNumber()
      let host = ReadNumber()
      CreateRTE(netType, net, host)
      endcase
      ]
   default:
      [
      let char = ReadChar()
      if char eq -1 % char eq $*N endcase
      ] repeat
   ]
] repeat

//----------------------------------------------------------------------------
and CreateRTE(netType, net, host) be
//----------------------------------------------------------------------------
[
let ndb = ndbQ!0; while ndb ne 0 do
   [
   if ndb>>NDB.netType eq netType then
      [
      if ndb>>NDB.localHost eq 0 then
         [
         PutTemplate(dsp,"*NNetType $O, Net $O, Host $O",netType,net,host)
         ndb>>NDB.localNet = net
         ndb>>NDB.localHost = host
         let rte = HInsert(pupRT, net)
         rte>>RTE.ndb = ndb
         rte>>RTE.host = host
         ]
      break
      ]
   ndb = ndb!0
   ]
]

//----------------------------------------------------------------------------
and ReadNumber() = valof
//----------------------------------------------------------------------------
[
let atom = ReadAtom()
let number = 0
for i = 1 to atom>>String.length do
   [
   let char = atom>>String.char↑i
   unless char ge $0 & char le $7 do
      CallSwat("[ReadNumber] - illegal character in number")
   number = number lshift 3 + (char-$0)
   ]
resultis number
]

//----------------------------------------------------------------------------
and ReadAtom() = valof
//----------------------------------------------------------------------------
//returns pointer to a string
//goes to top frame if end of file before reading any chars
[
let char = nil
   [  //ignore leading junk
   char = ReadChar()
   if char eq -1 then GotoFrame(end)
   unless IsBreak(char) break
   ] repeat
let count = 0
   [  //collect an atom
   count = count + 1
   if count gr maxAtomChars then
      CallSwat("[ReadAtom] - atom too long")
   buffer>>String.char↑count = char
   char = ReadChar()
   if IsBreak(char) break
   ] repeat
buffer>>String.length = count
resultis buffer
]

//----------------------------------------------------------------------------
and ReadChar() = valof
//----------------------------------------------------------------------------
//returns the next character in the stream
//returns -1 on end of file
[
until Endofs(paramFile) do
   [  //filter out comments here
   let char = Gets(paramFile); Puts(dsp, char)  //debugging
   if char eq $*N then inComment = false
   if inComment loop
   if char eq $; then inComment = true
   resultis char
   ]
resultis -1
]

//----------------------------------------------------------------------------
and IsBreak(char) =
//----------------------------------------------------------------------------
   char eq $*N % char eq $*S % char eq $*T % char eq -1 % char eq $;