Compare commits

...

3 Commits

Author SHA1 Message Date
sammyette 08a3e75fd1
fix: check for lua defined commands before going to sh interp (mvdan/sh#705) 2021-05-19 18:39:56 -04:00
sammyette d4a595d2a8
feat: run lua in sh interp exec handler 2021-05-19 18:38:15 -04:00
sammyette 9f1ad83c51
feat: print on unhandled err case for fs.cd 2021-05-19 17:00:23 -04:00
2 changed files with 37 additions and 2 deletions

View File

@ -1,6 +1,7 @@
package fs package fs
import ( import (
"fmt"
"os" "os"
"strings" "strings"
@ -31,9 +32,13 @@ func cd(L *lua.LState) int {
err := os.Chdir(strings.TrimSpace(path)) err := os.Chdir(strings.TrimSpace(path))
if err != nil { if err != nil {
switch err.(*os.PathError).Err.Error() { switch e := err.(*os.PathError).Err.Error(); e {
case "no such file or directory": case "no such file or directory":
LuaErr(L, 1) LuaErr(L, 1)
default:
fmt.Printf("Found unhandled error case: %s", e)
fmt.Printf("Report this at https://github.com/Hilbis/Hilbish/issues with the title being: \"fs: unahndled error case %s\", and show what caused it.\n", e)
LuaErr(L, 213)
} }
} }

View File

@ -16,7 +16,7 @@ import (
) )
func RunInput(input string) { func RunInput(input string) {
_, cmdString := splitInput(input) cmdArgs, cmdString := splitInput(input)
// First try to load input, essentially compiling to bytecode // First try to load input, essentially compiling to bytecode
fn, err := l.LoadString(cmdString) fn, err := l.LoadString(cmdString)
@ -38,6 +38,31 @@ func RunInput(input string) {
hooks.Em.Emit("command.exit", 0) hooks.Em.Emit("command.exit", 0)
return 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 // Last option: use sh interpreter
err = execCommand(cmdString) err = execCommand(cmdString)
@ -122,6 +147,11 @@ func execCommand(cmd string) error {
return interp.NewExitStatus(exitcode) return interp.NewExitStatus(exitcode)
} }
err := l.DoString(argstring)
if err == nil {
return nil
}
if _, err := interp.LookPathDir(hc.Dir, hc.Env, args[0]); err != nil { if _, err := interp.LookPathDir(hc.Dir, hc.Env, args[0]); err != nil {
hooks.Em.Emit("command.not-found", args[0]) hooks.Em.Emit("command.not-found", args[0])
return interp.NewExitStatus(127) return interp.NewExitStatus(127)