Compare commits

...

3 Commits

Author SHA1 Message Date
TorchedSammy 8b5dc69950
feat: add command.not-executable hook (closes #119) 2022-03-16 19:45:55 -04:00
sammyette 20a4cdb505
fix: handle path binaries properly on windows (closes #117, #118) (#120)
* fix: handle path binaries properly on windows (closes #117, #118)

* refactor: dont return exec name since it isnt needed

* fix: return correct error in find exec function and stat always

* fix: remove filepath import for exec file check on unix
2022-03-16 19:44:32 -04:00
TorchedSammy 01d937afd8
fix: correct username in greeting on windows 2022-03-16 18:42:38 -04:00
4 changed files with 58 additions and 22 deletions

10
api.go
View File

@ -48,15 +48,15 @@ func hilbishLoader(L *lua.LState) int {
host, _ := os.Hostname() host, _ := os.Hostname()
username := curuser.Username username := curuser.Username
greeting = `Welcome to {magenta}Hilbish{reset}, {cyan}` + curuser.Username + `{reset}.
The nice lil shell for {blue}Lua{reset} fanatics!
Check out the {blue}{bold}guide{reset} command to get started.
`
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
username = strings.Split(username, "\\")[1] // for some reason Username includes the hostname on windows username = strings.Split(username, "\\")[1] // for some reason Username includes the hostname on windows
} }
greeting = `Welcome to {magenta}Hilbish{reset}, {cyan}` + username + `{reset}.
The nice lil shell for {blue}Lua{reset} fanatics!
Check out the {blue}{bold}guide{reset} command to get started.
`
util.SetField(L, mod, "ver", lua.LString(version), "Hilbish version") util.SetField(L, mod, "ver", lua.LString(version), "Hilbish version")
util.SetField(L, mod, "user", lua.LString(username), "Username of user") util.SetField(L, mod, "user", lua.LString(username), "Username of user")
util.SetField(L, mod, "host", lua.LString(host), "Host name of the machine") util.SetField(L, mod, "host", lua.LString(host), "Host name of the machine")

25
exec.go
View File

@ -2,6 +2,7 @@ package main
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
@ -17,6 +18,8 @@ import (
"mvdan.cc/sh/v3/syntax" "mvdan.cc/sh/v3/syntax"
) )
var errNotExec = errors.New("not executable")
func runInput(input, origInput string) { func runInput(input, origInput string) {
running = true running = true
cmdString := aliases.Resolve(input) cmdString := aliases.Resolve(input)
@ -137,8 +140,9 @@ func execCommand(cmd, old string) error {
} }
err := lookpath(args[0]) err := lookpath(args[0])
if err == os.ErrPermission { if err == errNotExec {
hooks.Em.Emit("command.no-perm", args[0]) hooks.Em.Emit("command.no-perm", args[0])
hooks.Em.Emit("command.not-executable", args[0])
return interp.NewExitStatus(126) return interp.NewExitStatus(126)
} else if err != nil { } else if err != nil {
hooks.Em.Emit("command.not-found", args[0]) hooks.Em.Emit("command.not-found", args[0])
@ -156,19 +160,17 @@ func execCommand(cmd, old 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 { // custom lookpath function so we know if a command is found *and* is executable
func lookpath(file string) error {
skip := []string{"./", "/", "../", "~/"} skip := []string{"./", "/", "../", "~/"}
for _, s := range skip { for _, s := range skip {
if strings.HasPrefix(file, s) { if strings.HasPrefix(file, s) {
err := findExecutable(file) return findExecutable(file)
return err
} }
} }
for _, dir := range filepath.SplitList(os.Getenv("PATH")) { for _, dir := range filepath.SplitList(os.Getenv("PATH")) {
path := filepath.Join(dir, file) path := filepath.Join(dir, file)
err := findExecutable(path) err := findExecutable(path)
if err == os.ErrPermission { if err == errNotExec {
return err return err
} else if err == nil { } else if err == nil {
return nil return nil
@ -178,17 +180,6 @@ func lookpath(file string) error {
return os.ErrNotExist 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

18
execfile_unix.go 100644
View File

@ -0,0 +1,18 @@
// +build linux darwin
package main
import (
"os"
)
func findExecutable(path string) error {
f, err := os.Stat(path)
if err != nil {
return err
}
if m := f.Mode(); !m.IsDir() && m & 0111 != 0 {
return nil
}
return errNotExec
}

View File

@ -0,0 +1,27 @@
// +build windows
package main
import (
"path/filepath"
"os"
)
func findExecutable(path string) error {
nameExt := filepath.Ext(path)
if nameExt == "" {
for _, ext := range filepath.SplitList(os.Getenv("PATHEXT")) {
_, err := os.Stat(path + ext)
if err == nil {
return nil
}
}
}
_, err := os.Stat(path)
if err == nil {
return errNotExec
}
return os.ErrNotExist
}