From 9e810a5929221fe250cc36837a85dc32ad77e0a4 Mon Sep 17 00:00:00 2001 From: Sekulum Forka Date: Tue, 18 May 2021 15:01:13 +0200 Subject: [PATCH] Builtins: Added a hack so that builtins would behave correctly no matter if they're called from eval or execute --- src/fsh.nim | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/fsh.nim b/src/fsh.nim index 875978c..77bce09 100644 --- a/src/fsh.nim +++ b/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 - 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: # 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 - 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 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})