fix: check for lua defined commands before going to sh interp (mvdan/sh#705)

pull/59/head
sammyette 2021-05-19 18:39:56 -04:00
parent d4a595d2a8
commit 08a3e75fd1
No known key found for this signature in database
GPG Key ID: 50EE40A2809851F5
1 changed files with 26 additions and 1 deletions

View File

@ -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)