Builtins: Added a hack so that builtins would behave correctly no matter if they're called from eval or execute
This commit is contained in:
parent
75819e9abc
commit
9e810a5929
22
src/fsh.nim
22
src/fsh.nim
|
@ -255,7 +255,8 @@ proc parseCommand(cmd: string): seq[string] =
|
|||
else: newitem.add(c)
|
||||
|
||||
# runBuiltin runs a shell builtin
|
||||
proc runBuiltin(builtin: string, args: openArray[string]): (string, int) =
|
||||
# the mode is 0 for eval and 1 for execute
|
||||
proc runBuiltin(builtin: string, args: openArray[string], mode = 0): (string, int) =
|
||||
case builtin:
|
||||
of "set":
|
||||
if args.len < 2:
|
||||
|
@ -304,12 +305,19 @@ proc runBuiltin(builtin: string, args: openArray[string]): (string, int) =
|
|||
# Check if this is the last item, if so just execute it
|
||||
# It's an else branch basically
|
||||
if i == args.high:
|
||||
return ("", execute(args[i]))
|
||||
if mode == 0:
|
||||
return (args[i].eval, 0)
|
||||
elif mode == 1:
|
||||
return ("", args[i].execute)
|
||||
# Get the exitcode of the current arg (the condition)
|
||||
let exitcode = args[i].execute
|
||||
if exitcode == 0:
|
||||
# if the condition is true
|
||||
if mode == 0:
|
||||
return (args[i+1].eval, 0)
|
||||
elif mode == 1:
|
||||
return ("", args[i+1].execute)
|
||||
else: raise newException(Exception, "mode must be either 0 or 1")
|
||||
else:
|
||||
# if the condition is false
|
||||
i.inc
|
||||
|
@ -345,10 +353,14 @@ proc runBuiltin(builtin: string, args: openArray[string]): (string, int) =
|
|||
let vari = varval_parsed[0]
|
||||
let val = varval_parsed[1]
|
||||
shellVariables[numframes-1][vari]=val
|
||||
if mode == 0:
|
||||
result[0] = args[1].eval
|
||||
result[1] = 0
|
||||
elif mode == 1:
|
||||
result[0]=""
|
||||
result[1]=args[1].execute
|
||||
discard shellVariables.pop
|
||||
numframes.dec
|
||||
result[1] = 0
|
||||
else:
|
||||
raise newShellCommandException("No such builtin implemented", builtin)
|
||||
|
||||
|
@ -362,7 +374,7 @@ proc execute(cmd: string): int =
|
|||
let progname = parsed[0]
|
||||
let args = parsed[1..parsed.high]
|
||||
if progname in builtins:
|
||||
let res=runBuiltin(progname, args)
|
||||
let res=runBuiltin(progname, args, 1)
|
||||
stdout.write(res[0])
|
||||
result=res[1]
|
||||
continue
|
||||
|
@ -384,7 +396,7 @@ proc eval(cmd: string): string =
|
|||
let progname=parsed[0]
|
||||
let args = parsed[1..parsed.high]
|
||||
if progname in builtins:
|
||||
result = runBuiltin(progname, args)[0]
|
||||
result = runBuiltin(progname, args, 0)[0]
|
||||
continue
|
||||
try:
|
||||
let ps = startProcess(progname, args=args, options={poUsePath})
|
||||
|
|
Loading…
Reference in New Issue