From 08a3e75fd1e30e8cc6fb7803fc951c9788339468 Mon Sep 17 00:00:00 2001 From: sammyette <38820196+TorchedSammy@users.noreply.github.com> Date: Wed, 19 May 2021 18:39:56 -0400 Subject: [PATCH] fix: check for lua defined commands before going to sh interp (mvdan/sh#705) --- shell.go | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/shell.go b/shell.go index 8f8f1ab..53b5e67 100644 --- a/shell.go +++ b/shell.go @@ -16,7 +16,7 @@ import ( ) func RunInput(input string) { - _, cmdString := splitInput(input) + cmdArgs, cmdString := splitInput(input) // First try to load input, essentially compiling to bytecode fn, err := l.LoadString(cmdString) @@ -38,6 +38,31 @@ func RunInput(input string) { hooks.Em.Emit("command.exit", 0) return } + if commands[cmdArgs[0]] { + err := l.CallByParam(lua.P{ + Fn: l.GetField( + l.GetTable( + l.GetGlobal("commanding"), + lua.LString("__commands")), + cmdArgs[0]), + NRet: 1, + Protect: true, + }, luar.New(l, cmdArgs[1:])) + luaexitcode := l.Get(-1) + var exitcode uint8 = 0 + + l.Pop(1) + + if code, ok := luaexitcode.(lua.LNumber); luaexitcode != lua.LNil && ok { + exitcode = uint8(code) + } + + if err != nil { + fmt.Fprintln(os.Stderr, + "Error in command:\n\n" + err.Error()) + } + hooks.Em.Emit("command.exit", exitcode) + } // Last option: use sh interpreter err = execCommand(cmdString)