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:
Sekulum Forka 2021-05-18 15:01:13 +02:00
parent 75819e9abc
commit 9e810a5929
1 changed files with 19 additions and 7 deletions

View File

@ -255,7 +255,8 @@ proc parseCommand(cmd: string): seq[string] =
else: newitem.add(c) else: newitem.add(c)
# runBuiltin runs a shell builtin # 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: case builtin:
of "set": of "set":
if args.len < 2: 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 # Check if this is the last item, if so just execute it
# It's an else branch basically # It's an else branch basically
if i == args.high: 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) # Get the exitcode of the current arg (the condition)
let exitcode = args[i].execute let exitcode = args[i].execute
if exitcode == 0: if exitcode == 0:
# if the condition is true # if the condition is true
return ("", args[i+1].execute) 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: else:
# if the condition is false # if the condition is false
i.inc i.inc
@ -345,10 +353,14 @@ proc runBuiltin(builtin: string, args: openArray[string]): (string, int) =
let vari = varval_parsed[0] let vari = varval_parsed[0]
let val = varval_parsed[1] let val = varval_parsed[1]
shellVariables[numframes-1][vari]=val shellVariables[numframes-1][vari]=val
result[0] = args[1].eval if mode == 0:
result[0] = args[1].eval
result[1] = 0
elif mode == 1:
result[0]=""
result[1]=args[1].execute
discard shellVariables.pop discard shellVariables.pop
numframes.dec numframes.dec
result[1] = 0
else: else:
raise newShellCommandException("No such builtin implemented", builtin) raise newShellCommandException("No such builtin implemented", builtin)
@ -362,7 +374,7 @@ proc execute(cmd: string): int =
let progname = parsed[0] let progname = parsed[0]
let args = parsed[1..parsed.high] let args = parsed[1..parsed.high]
if progname in builtins: if progname in builtins:
let res=runBuiltin(progname, args) let res=runBuiltin(progname, args, 1)
stdout.write(res[0]) stdout.write(res[0])
result=res[1] result=res[1]
continue continue
@ -384,7 +396,7 @@ proc eval(cmd: string): string =
let progname=parsed[0] let progname=parsed[0]
let args = parsed[1..parsed.high] let args = parsed[1..parsed.high]
if progname in builtins: if progname in builtins:
result = runBuiltin(progname, args)[0] result = runBuiltin(progname, args, 0)[0]
continue continue
try: try:
let ps = startProcess(progname, args=args, options={poUsePath}) let ps = startProcess(progname, args=args, options={poUsePath})