//CmdScanTest.bcpl
// Copyright Xerox Corporation 1979

get "CmdScan.decl"

external
[
InitCmd; BeginDefaultPhrase; EndDefaultPhrase; DefaultPhrase
EnableCatch; EndCatch; GetNumber; GetFile; GetString; Confirm
Gets; Puts; Closes; Endofs; Wss; Wns; Free; OpenFile
TruePredicate; FalsePredicate; TerminatingChar; ErasePhrase; GetPhrase
CreateKeywordTable; DestroyKeywordTable; InsertKeyword; GetKeyword
sysZone
]

let CmdScanTest() be
[
let kt = CreateKeywordTable(10)
InsertKeyword(kt, "Number")!0 = 1
InsertKeyword(kt, "Copy")!0 = 2
InsertKeyword(kt, "Quit")!0 = 3
InsertKeyword(kt, "Comment")!0 = 4
let cs = nil
cs = InitCmd(256, 10) repeatuntil cs ne 0
Wss(cs, "*n**")
switchon GetKeyword(cs, kt)!0 into
   [
   case 1:  //number test
      [
      Puts(cs, $*s)
      let n1 = GetNumber(cs)
      Wss(cs, " Another: ")
      BeginDefaultPhrase(cs)
      Wns(cs, n1+1)
      EndDefaultPhrase(cs)
      let n2 = GetNumber(cs)
      if Confirm(cs) then
         [ Wss(cs, " = "); Wns(cs, n1); Wss(cs, ", "); Wns(cs, n2) ]
      endcase
      ]
   case 2:  //file test
      [
      Wss(cs, " file ")
      let s = nil
      if EnableCatch(cs) then [ if s ne 0 then Closes(s); EndCatch(cs) ]
      s = 0
      s = GetFile(cs, ksTypeReadOnly, charItem)
      Wss(cs, " to file ")
      DefaultPhrase(cs, "foo.baz", $*s)
      let str, s2 = nil, nil
      if EnableCatch(cs) then
         [
         if str ne 0 then Free(sysZone, str)
         if s2 ne 0 then Closes(s2)
         EndCatch(cs)
         ]
      str, s2 = 0, 0
      str = GetString(cs)
      s2 = OpenFile(str, ksTypeWriteOnly, charItem, verLatest)
      Wss(cs, (s2 eq 0? " [New file]", " [Old file]"))
      GetPhrase(cs, TruePredicate, TruePredicate, FalsePredicate, Wss,
       "return to confirm, or type another filename")
      if TerminatingChar(cs) ne $*n then
         ErasePhrase(cs, 1, 0, TerminatingChar(cs))
      if s2 eq 0 then s2 = OpenFile(str, ksTypeWriteOnly, charItem)
      Free(sysZone, str)
      until Endofs(s) do Puts(s2, Gets(s))
      Closes(s); Closes(s2)
      endcase
      ]
   case 3:  //quit
      finish
   case 4:  //comment
      [
      Puts(cs, $*s)
      GetPhrase(cs, 0, 0, TruePredicate) repeatuntil
         TerminatingChar(cs) eq $*n
      endcase
      ]
   ]
Closes(cs)
DestroyKeywordTable(kt)
] repeat