Compare commits

...

2 Commits

Author SHA1 Message Date
Sekulum Forka 8e21117986 Ctrl-C: Handles sigint much more elegantly 2021-05-13 22:40:06 +02:00
Sekulum Forka 87982fdcbc Exceptions: Add an exception for ^c 2021-05-13 22:05:15 +02:00
1 changed files with 14 additions and 1 deletions

View File

@ -35,6 +35,7 @@ type
variable: string
ShellCommandError = object of ShellError
command: string
InterruptCtrlC = object of CatchableError
# procs for making procedures
proc newShellException(msg: string, line=0, col=0, whileExec=""): ref ShellError =
@ -64,6 +65,10 @@ proc newShellCommandException(msg: string, command: string, line=0, col=0, while
proc eval(cmd: string): string
proc substitute(strm: Stream, delim = ";\p"): string
# ctrlc is the ctrl-c handler
proc ctrlc() {.noconv.} =
raise newException(InterruptCtrlC, "")
# skipSpaces reads the stream until a non-space character is found
proc skipSpaces(strm: Stream): int =
while not strm.atEnd():
@ -321,6 +326,14 @@ proc eval(cmd: string): string =
strm.close
when isMainModule:
# ctrlc handler
setControlCHook(ctrlc)
let stdinstrm = stdin.newFileStream
while not stdinstrm.atEnd():
stdinstrm.readCommand.execute.echo
try:
discard stdinstrm.readCommand.execute
except ShellError, ShellCommandError, ShellVariableError:
let e = getCurrentException()
stderr.write(e.msg, "\n")
except InterruptCtrlC:
continue