mirror of https://github.com/Hilbis/Hilbish
fix: reuse sh runner
this makes it so env variables persist, and some other stuff builtin cd actually works now :]pull/69/head
parent
2b5e65e47a
commit
822b2876e9
55
main.go
55
main.go
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
@ -9,12 +10,14 @@ import (
|
||||||
"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"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -30,6 +33,8 @@ var (
|
||||||
|
|
||||||
hooks bait.Bait
|
hooks bait.Bait
|
||||||
defaultConfPath string
|
defaultConfPath string
|
||||||
|
|
||||||
|
runner *interp.Runner
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -146,6 +151,56 @@ 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
|
||||||
|
|
48
shell.go
48
shell.go
|
@ -5,7 +5,6 @@ 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"
|
||||||
|
@ -115,53 +114,6 @@ func execCommand(cmd string) error {
|
||||||
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
|
||||||
|
|
Loading…
Reference in New Issue