Compare commits

..

No commits in common. "35e648b7bc3269c3c52d804a4ce4627e58d03abd" and "2f816c33cde764e18ca87142692f687834828c68" have entirely different histories.

2 changed files with 4 additions and 36 deletions

View File

@ -166,7 +166,3 @@ bait.catch('command.not-found', function(cmd)
print(string.format('hilbish: %s not found', cmd)) print(string.format('hilbish: %s not found', cmd))
end) end)
bait.catch('command.no-perm', function(cmd)
print(string.format('hilbish: %s: no permission', cmd))
end)

View File

@ -4,10 +4,10 @@ import (
"context" "context"
"fmt" "fmt"
"os" "os"
"path/filepath"
"strings" "strings"
"time" "time"
// "github.com/bobappleyard/readline"
"github.com/yuin/gopher-lua" "github.com/yuin/gopher-lua"
// "github.com/yuin/gopher-lua/parse" // "github.com/yuin/gopher-lua/parse"
"mvdan.cc/sh/v3/interp" "mvdan.cc/sh/v3/interp"
@ -95,6 +95,7 @@ func execCommand(cmd string) error {
} }
exechandle := func(ctx context.Context, args []string) error { exechandle := func(ctx context.Context, args []string) error {
hc := interp.HandlerCtx(ctx)
_, argstring := splitInput(strings.Join(args, " ")) _, argstring := splitInput(strings.Join(args, " "))
// If alias was found, use command alias // If alias was found, use command alias
@ -141,11 +142,7 @@ func execCommand(cmd string) error {
return interp.NewExitStatus(exitcode) return interp.NewExitStatus(exitcode)
} }
err := lookpath(args[0]) if _, err := interp.LookPathDir(hc.Dir, hc.Env, args[0]); err != nil {
if err == os.ErrPermission {
hooks.Em.Emit("command.no-perm", args[0])
return interp.NewExitStatus(126)
} else if 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)
} }
@ -161,31 +158,6 @@ func execCommand(cmd string) error {
return err return err
} }
// custom lookpath function so we know if a command is found *and* has execute permission
func lookpath(file string) error {
for _, dir := range filepath.SplitList(os.Getenv("PATH")) {
path := filepath.Join(dir, file)
err := findExecutable(path)
if err == os.ErrPermission {
return err
} else if err == nil {
return nil
}
}
return os.ErrNotExist
}
func findExecutable(name string) error {
f, err := os.Stat(name)
if err != nil {
return err
}
if m := f.Mode(); !m.IsDir() && m & 0111 != 0 {
return nil
}
return os.ErrPermission
}
func splitInput(input string) ([]string, string) { func splitInput(input string) ([]string, string) {
// end my suffering // end my suffering
// TODO: refactor this garbage // TODO: refactor this garbage