// CmdScanTty.bcpl -- Command scanner environment-specific procedures
//			for dealing with sequential terminal streams
// Copyright Xerox Corporation 1979

//	Last modified July 14, 1977  6:54 PM

get "cmdscan.decl"

external
[
//outgoing procedures
DefErase; DefError; DefBreak; DefEcho; CmdError

//incoming procedures
Puts; Resets; Wss; SysErr
CurrentPhrase; BackupPhrase; Dismiss
]


//---------------------------------------------------------------------------
let DefErase(cs, first, last, context) be
//---------------------------------------------------------------------------
[
let ds = cs>>CS.dspS
switchon context into
   [
   case eraseChar:
      [
      Puts(ds, $\)
      if cs>>CS.buf>>Buf↑first ls #200 then Puts(ds, cs>>CS.buf>>Buf↑first)
      endcase
      ]
   case eraseWord:
      [ Wss(ds, "← "); endcase ]
   ]
]


//---------------------------------------------------------------------------
and DefBreak(cs, char) = char eq $*n % char eq $*s % char eq $*033
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
and DefEcho(cs, char) = not CurrentPhrase(cs)>>PD.PhraseTerminator(cs, char)
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
and DefError(cs, ec) be
//---------------------------------------------------------------------------
[
cs>>CS.errorCode = ec
switchon ec into
   [
   case ecCmdDelete:
      Wss(cs>>CS.dspS, " XXX")  //fall into next case
   case ecCmdDestroy:
      [ cs>>CS.destroy = true; BackupPhrase(cs, cs>>CS.iPhOut) ]
   case ecCmdTooLong:  //just ding and do nothing
      [ CmdError(cs); endcase ]
   case ecKeyAmbiguous:
   case ecBackupAppend:
      [ CmdError(cs); BackupPhrase(cs, 0, editAppend) ]
   case ecTooManyPhrases:
   case ecEndOfPhrase:
      SysErr(cs, ec)
   default:  //all types of syntax errors get same handling
      CmdError(cs, " ?")  //fall into next case
   case ecBackupReplace:
      BackupPhrase(cs)
   ]
]


//---------------------------------------------------------------------------
and CmdError(cs, string; numargs na) be
//---------------------------------------------------------------------------
//Displays string if present, rings bell, and resets keyboard stream
[
if na gr 1 & string ne 0 then Wss(cs, string)
Puts(cs>>CS.dspS, $*007)
Dismiss(50)
Resets(cs>>CS.keyS)
]