Compare commits

...

2 Commits

Author SHA1 Message Date
sammyette bc4e0f835f
fix: revert "fix: reuse sh runner"
This reverts commit 822b2876e9.
this broke a few things with the shell so probably shouldnt do this yet
2021-09-25 22:46:06 -04:00
sammyette c95b0b55be
fix: runtime errors on linux
this fixes out of bounds error since we tried to split the username
for '\' which is only valid on windows, so i added it for windows only
2021-09-25 21:39:06 -04:00
3 changed files with 61 additions and 58 deletions

View File

@ -5,6 +5,7 @@ package main
import ( import (
"os" "os"
"runtime"
"strings" "strings"
"github.com/pborman/getopt" "github.com/pborman/getopt"
@ -22,7 +23,11 @@ func HilbishLoader(L *lua.LState) int {
mod := L.SetFuncs(L.NewTable(), exports) mod := L.SetFuncs(L.NewTable(), exports)
host, _ := os.Hostname() host, _ := os.Hostname()
username := strings.Split(curuser.Username, "\\")[1] // for some reason Username includes the hostname on windows username := curuser.Username
// this will be baked into binary since GOOS is a constant
if runtime.GOOS == "windows" {
username = strings.Split(username, "\\")[1] // for some reason Username includes the hostname on windows
}
L.SetField(mod, "ver", lua.LString(version)) L.SetField(mod, "ver", lua.LString(version))
L.SetField(mod, "user", lua.LString(username)) L.SetField(mod, "user", lua.LString(username))

62
main.go
View File

@ -2,22 +2,20 @@ package main
import ( import (
"bufio" "bufio"
"context"
"fmt" "fmt"
"io" "io"
"os" "os"
"runtime"
"strings" "strings"
"os/signal" "os/signal"
"os/user" "os/user"
"path/filepath" "path/filepath"
"time"
"hilbish/golibs/bait" "hilbish/golibs/bait"
"github.com/pborman/getopt" "github.com/pborman/getopt"
"github.com/yuin/gopher-lua" "github.com/yuin/gopher-lua"
"layeh.com/gopher-luar" "layeh.com/gopher-luar"
"mvdan.cc/sh/v3/interp"
"golang.org/x/term" "golang.org/x/term"
) )
@ -33,8 +31,6 @@ var (
hooks bait.Bait hooks bait.Bait
defaultConfPath string defaultConfPath string
runner *interp.Runner
) )
func main() { func main() {
@ -153,56 +149,6 @@ func main() {
os.Exit(0) os.Exit(0)
} }
// Execute handler for sh runner
exechandle := func(ctx context.Context, args []string) error {
hc := interp.HandlerCtx(ctx)
_, argstring := splitInput(strings.Join(args, " "))
// If alias was found, use command alias
if aliases[args[0]] != "" {
alias := aliases[args[0]]
argstring = alias + strings.TrimPrefix(argstring, args[0])
cmdArgs, _ := splitInput(argstring)
args = cmdArgs
}
// If command is defined in Lua then run it
if commands[args[0]] != nil {
err := l.CallByParam(lua.P{
Fn: commands[args[0]],
NRet: 1,
Protect: true,
}, luar.New(l, args[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)
return interp.NewExitStatus(exitcode)
}
if _, err := interp.LookPathDir(hc.Dir, hc.Env, args[0]); err != nil {
hooks.Em.Emit("command.not-found", args[0])
return interp.NewExitStatus(127)
}
return interp.DefaultExecHandler(2 * time.Second)(ctx, args)
}
// Setup sh runner outside of input label
runner, _ = interp.New(
interp.StdIO(os.Stdin, os.Stdout, os.Stderr),
interp.ExecHandler(exechandle),
)
input: input:
for interactive { for interactive {
running = false running = false
@ -265,7 +211,11 @@ func fmtPrompt() string {
cwd, _ := os.Getwd() cwd, _ := os.Getwd()
cwd = strings.Replace(cwd, curuser.HomeDir, "~", 1) cwd = strings.Replace(cwd, curuser.HomeDir, "~", 1)
username := strings.Split(curuser.Username, "\\")[1] // for some reason Username includes the hostname on windows username := curuser.Username
// this will be baked into binary since GOOS is a constant
if runtime.GOOS == "windows" {
username = strings.Split(username, "\\")[1] // for some reason Username includes the hostname on windows
}
args := []string{ args := []string{
"d", cwd, "d", cwd,

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"os" "os"
"strings" "strings"
"time"
// "github.com/bobappleyard/readline" // "github.com/bobappleyard/readline"
"github.com/yuin/gopher-lua" "github.com/yuin/gopher-lua"
@ -113,7 +114,54 @@ func execCommand(cmd string) error {
if err != nil { if err != nil {
return err return err
} }
exechandle := func(ctx context.Context, args []string) error {
hc := interp.HandlerCtx(ctx)
_, argstring := splitInput(strings.Join(args, " "))
// If alias was found, use command alias
if aliases[args[0]] != "" {
alias := aliases[args[0]]
argstring = alias + strings.TrimPrefix(argstring, args[0])
cmdArgs, _ := splitInput(argstring)
args = cmdArgs
}
// If command is defined in Lua then run it
if commands[args[0]] != nil {
err := l.CallByParam(lua.P{
Fn: commands[args[0]],
NRet: 1,
Protect: true,
}, luar.New(l, args[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)
return interp.NewExitStatus(exitcode)
}
if _, err := interp.LookPathDir(hc.Dir, hc.Env, args[0]); err != nil {
hooks.Em.Emit("command.not-found", args[0])
return interp.NewExitStatus(127)
}
return interp.DefaultExecHandler(2 * time.Second)(ctx, args)
}
runner, _ := interp.New(
interp.StdIO(os.Stdin, os.Stdout, os.Stderr),
interp.ExecHandler(exechandle),
)
err = runner.Run(context.TODO(), file) err = runner.Run(context.TODO(), file)
return err return err