eval: Eval can now evaluate multiple commands at once

This commit is contained in:
Sekulum Forka 2021-05-12 21:04:29 +02:00
parent 15df6c4a82
commit a83e5d3c79
1 changed files with 17 additions and 9 deletions

View File

@ -120,7 +120,8 @@ proc readCommand(strm: Stream): string =
of '\\':
result.add("\\")
result.add(strm.readChar())
of '\n', '\r', ';': break
of '\n', '\r', ';':
break
else: result.add(c)
# readVariable reads a variable
@ -168,6 +169,7 @@ proc substitute(strm: Stream, delim=";\p"): string =
of '\\':
result.add(strm.readChar())
of '$': result.add(strm.readVariable.evalVariable)
of ';', '\n', '\r': break
else:
result.add(c)
@ -192,6 +194,9 @@ proc parseCommand(cmd: string): seq[string] =
result.add(newitem)
newitem = ""
discard strm.skipSpaces()
of ';', '\n', '\r':
result.add(newitem)
break
else: newitem.add(c)
# runBuiltin runs a shell builtin
@ -251,14 +256,17 @@ proc execute(cmd: string): int =
# eval evaluates the given string
proc eval(cmd: string): string =
let parsed = cmd.parseCommand
let progname=parsed[0]
let args = parsed[1..parsed.high]
if progname in builtins:
return runBuiltin(progname, args)
let ps = startProcess(progname, args=args, options={poUsePath})
result=ps.outputStream.readAll
ps.close
let strm=cmd.newStringStream
while not strm.atEnd():
let parsed = strm.readCommand.parseCommand
let progname=parsed[0]
let args = parsed[1..parsed.high]
if progname in builtins:
result = runBuiltin(progname, args)
let ps = startProcess(progname, args=args, options={poUsePath})
result=ps.outputStream.readAll
ps.close
strm.close
when isMainModule:
let stdinstrm = stdin.newFileStream