Compare commits

...

5 Commits

1 changed files with 36 additions and 18 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)
@ -189,9 +191,13 @@ proc parseCommand(cmd: string): seq[string] =
newitem.add(strm.readSubstitution.newStringStream.substitute)
of '$': result.add(strm.readVariable.evalVariable)
of ' ':
if (result == @[]) and (newitem == ""): continue
result.add(newitem)
newitem = ""
discard strm.skipSpaces()
of ';', '\n', '\r':
result.add(newitem)
break
else: newitem.add(c)
# runBuiltin runs a shell builtin
@ -239,26 +245,38 @@ proc runBuiltin(builtin: string, args: openArray[string]): string =
# executes executes the command. For now it involves catting the command and printing it
proc execute(cmd: string): int =
let parsed = cmd.parseCommand
let progname = parsed[0]
let args = parsed[1..parsed.high]
if progname in builtins:
stdout.write(runBuiltin(progname, args))
return 0
let ps = startProcess(progname, args=args, options={poUsePath, poParentStreams})
result = ps.waitForExit
ps.close
let strm=cmd.newStringStream
while not strm.atEnd:
let parsed = strm.readCommand.parseCommand
if parsed == @[]:
continue
let progname = parsed[0]
let args = parsed[1..parsed.high]
if progname in builtins:
stdout.write(runBuiltin(progname, args))
result = 0
continue
let ps = startProcess(progname, args=args, options={poUsePath, poParentStreams})
result = ps.waitForExit
ps.close
strm.close
# 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
if parsed == @[]:
continue
let progname=parsed[0]
let args = parsed[1..parsed.high]
if progname in builtins:
result = runBuiltin(progname, args)
continue
let ps = startProcess(progname, args=args, options={poUsePath})
result=ps.outputStream.readAll
ps.close
strm.close
when isMainModule:
let stdinstrm = stdin.newFileStream