2021-04-03 17:13:45 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-03-19 17:10:50 +00:00
|
|
|
"bytes"
|
2021-04-03 17:13:45 +00:00
|
|
|
"context"
|
2022-03-16 23:44:32 +00:00
|
|
|
"errors"
|
2022-03-19 17:10:50 +00:00
|
|
|
"os/exec"
|
2021-04-19 02:09:27 +00:00
|
|
|
"fmt"
|
2022-04-04 10:40:02 +00:00
|
|
|
"io"
|
2021-04-19 02:09:27 +00:00
|
|
|
"os"
|
2024-07-19 22:46:46 +00:00
|
|
|
"os/signal"
|
2021-11-21 23:45:44 +00:00
|
|
|
"path/filepath"
|
2022-03-18 00:22:30 +00:00
|
|
|
"runtime"
|
2021-04-03 17:13:45 +00:00
|
|
|
"strings"
|
2022-03-19 17:10:50 +00:00
|
|
|
"syscall"
|
2021-09-26 02:45:31 +00:00
|
|
|
"time"
|
2021-04-03 17:13:45 +00:00
|
|
|
|
2024-07-19 20:54:15 +00:00
|
|
|
"hilbish/moonlight"
|
|
|
|
//"hilbish/util"
|
2022-03-07 23:10:21 +00:00
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
rt "github.com/arnodel/golua/runtime"
|
2021-12-31 18:11:11 +00:00
|
|
|
"mvdan.cc/sh/v3/shell"
|
2021-11-21 23:50:35 +00:00
|
|
|
//"github.com/yuin/gopher-lua/parse"
|
2021-04-03 17:13:45 +00:00
|
|
|
"mvdan.cc/sh/v3/interp"
|
|
|
|
"mvdan.cc/sh/v3/syntax"
|
2022-03-19 17:10:50 +00:00
|
|
|
"mvdan.cc/sh/v3/expand"
|
2021-04-03 17:13:45 +00:00
|
|
|
)
|
|
|
|
|
2022-03-16 23:44:32 +00:00
|
|
|
var errNotExec = errors.New("not executable")
|
2022-05-01 01:02:36 +00:00
|
|
|
var errNotFound = errors.New("not found")
|
2024-07-21 15:37:08 +00:00
|
|
|
var runnerMode moonlight.Value = moonlight.StringValue("hybrid")
|
2022-03-16 23:44:32 +00:00
|
|
|
|
2024-04-28 01:03:54 +00:00
|
|
|
type streams struct {
|
|
|
|
stdout io.Writer
|
|
|
|
stderr io.Writer
|
|
|
|
stdin io.Reader
|
|
|
|
}
|
|
|
|
|
2022-05-01 01:02:36 +00:00
|
|
|
type execError struct{
|
|
|
|
typ string
|
|
|
|
cmd string
|
|
|
|
code int
|
|
|
|
colon bool
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e execError) Error() string {
|
|
|
|
return fmt.Sprintf("%s: %s", e.cmd, e.typ)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e execError) sprint() error {
|
|
|
|
sep := " "
|
|
|
|
if e.colon {
|
|
|
|
sep = ": "
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("hilbish: %s%s%s", e.cmd, sep, e.err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func isExecError(err error) (execError, bool) {
|
|
|
|
if exErr, ok := err.(execError); ok {
|
|
|
|
return exErr, true
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := strings.Split(err.Error(), ": ")
|
|
|
|
knownTypes := []string{
|
|
|
|
"not-found",
|
|
|
|
"not-executable",
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(fields) > 1 && contains(knownTypes, fields[1]) {
|
|
|
|
var colon bool
|
|
|
|
var e error
|
|
|
|
switch fields[1] {
|
|
|
|
case "not-found":
|
|
|
|
e = errNotFound
|
|
|
|
case "not-executable":
|
|
|
|
colon = true
|
|
|
|
e = errNotExec
|
|
|
|
}
|
|
|
|
|
|
|
|
return execError{
|
|
|
|
cmd: fields[0],
|
|
|
|
typ: fields[1],
|
|
|
|
colon: colon,
|
|
|
|
err: e,
|
|
|
|
}, true
|
|
|
|
}
|
|
|
|
|
|
|
|
return execError{}, false
|
|
|
|
}
|
|
|
|
|
2022-03-19 22:48:03 +00:00
|
|
|
func runInput(input string, priv bool) {
|
2021-12-06 19:45:35 +00:00
|
|
|
running = true
|
2021-12-15 00:54:23 +00:00
|
|
|
cmdString := aliases.Resolve(input)
|
2022-08-17 22:01:32 +00:00
|
|
|
hooks.Emit("command.preexec", input, cmdString)
|
2021-06-09 23:16:08 +00:00
|
|
|
|
2022-06-03 02:33:30 +00:00
|
|
|
rerun:
|
2022-04-13 14:11:38 +00:00
|
|
|
var exitCode uint8
|
|
|
|
var err error
|
2022-06-03 02:33:30 +00:00
|
|
|
var cont bool
|
|
|
|
// save incase it changes while prompting (For some reason)
|
|
|
|
currentRunner := runnerMode
|
2024-07-21 15:37:08 +00:00
|
|
|
if currentRunner.Type() == moonlight.StringType {
|
2022-06-03 02:33:30 +00:00
|
|
|
switch currentRunner.AsString() {
|
2022-03-20 19:15:44 +00:00
|
|
|
case "hybrid":
|
2022-09-18 01:00:28 +00:00
|
|
|
_, _, err = handleLua(input)
|
2022-03-20 19:15:44 +00:00
|
|
|
if err == nil {
|
2022-04-05 01:21:46 +00:00
|
|
|
cmdFinish(0, input, priv)
|
2022-03-20 19:15:44 +00:00
|
|
|
return
|
|
|
|
}
|
2022-09-18 01:00:28 +00:00
|
|
|
input, exitCode, cont, err = handleSh(input)
|
2022-03-20 19:15:44 +00:00
|
|
|
case "hybridRev":
|
2022-06-03 02:33:30 +00:00
|
|
|
_, _, _, err = handleSh(input)
|
2022-03-20 19:15:44 +00:00
|
|
|
if err == nil {
|
2022-04-05 01:21:46 +00:00
|
|
|
cmdFinish(0, input, priv)
|
2022-03-20 19:15:44 +00:00
|
|
|
return
|
|
|
|
}
|
2022-09-18 01:00:28 +00:00
|
|
|
input, exitCode, err = handleLua(input)
|
2022-03-20 19:15:44 +00:00
|
|
|
case "lua":
|
2022-09-18 01:00:28 +00:00
|
|
|
input, exitCode, err = handleLua(input)
|
2022-03-20 19:15:44 +00:00
|
|
|
case "sh":
|
2022-09-18 01:00:28 +00:00
|
|
|
input, exitCode, cont, err = handleSh(input)
|
2022-03-20 19:15:44 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// can only be a string or function so
|
2022-06-20 19:47:19 +00:00
|
|
|
var runnerErr error
|
|
|
|
input, exitCode, cont, runnerErr, err = runLuaRunner(currentRunner, input)
|
2022-03-20 19:15:44 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
2022-04-05 01:21:46 +00:00
|
|
|
cmdFinish(124, input, priv)
|
2022-03-20 19:15:44 +00:00
|
|
|
return
|
|
|
|
}
|
2022-06-20 19:47:19 +00:00
|
|
|
// yep, we only use `err` to check for lua eval error
|
|
|
|
// our actual error should only be a runner provided error at this point
|
|
|
|
// command not found type, etc
|
|
|
|
err = runnerErr
|
2022-06-03 02:33:30 +00:00
|
|
|
}
|
2022-03-20 19:15:44 +00:00
|
|
|
|
2022-06-03 02:33:30 +00:00
|
|
|
if cont {
|
2022-09-18 01:00:28 +00:00
|
|
|
input, err = reprompt(input)
|
2022-06-03 02:33:30 +00:00
|
|
|
if err == nil {
|
|
|
|
goto rerun
|
|
|
|
} else if err == io.EOF {
|
|
|
|
return
|
2022-05-01 01:02:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if exErr, ok := isExecError(err); ok {
|
2022-08-17 22:01:32 +00:00
|
|
|
hooks.Emit("command." + exErr.typ, exErr.cmd)
|
2022-12-13 16:57:27 +00:00
|
|
|
} else {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
2022-03-20 19:15:44 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-01 01:02:36 +00:00
|
|
|
cmdFinish(exitCode, input, priv)
|
2022-03-20 19:15:44 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 02:33:30 +00:00
|
|
|
func reprompt(input string) (string, error) {
|
|
|
|
for {
|
|
|
|
in, err := continuePrompt(strings.TrimSuffix(input, "\\"))
|
|
|
|
if err != nil {
|
2022-08-31 01:52:07 +00:00
|
|
|
lr.SetPrompt(fmtPrompt(prompt))
|
2022-06-03 02:33:30 +00:00
|
|
|
return input, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasSuffix(in, "\\") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return in, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-21 15:37:08 +00:00
|
|
|
func runLuaRunner(runr moonlight.Value, userInput string) (input string, exitCode uint8, continued bool, runnerErr, err error) {
|
2024-07-19 20:54:15 +00:00
|
|
|
runnerRet, err := l.Call1(runr, moonlight.StringValue(userInput))
|
2022-06-03 02:33:30 +00:00
|
|
|
if err != nil {
|
2022-06-20 19:47:19 +00:00
|
|
|
return "", 124, false, nil, err
|
2022-06-03 02:33:30 +00:00
|
|
|
}
|
|
|
|
|
2024-07-21 15:37:08 +00:00
|
|
|
var runner *moonlight.Table
|
2022-06-03 02:33:30 +00:00
|
|
|
var ok bool
|
2024-07-21 15:37:08 +00:00
|
|
|
if runner, ok = moonlight.TryTable(runnerRet); !ok {
|
2022-06-03 02:33:30 +00:00
|
|
|
fmt.Fprintln(os.Stderr, "runner did not return a table")
|
2023-12-07 02:20:41 +00:00
|
|
|
exitCode = 125
|
|
|
|
input = userInput
|
|
|
|
return
|
2022-06-03 02:33:30 +00:00
|
|
|
}
|
|
|
|
|
2024-07-21 15:37:08 +00:00
|
|
|
if code, ok := runner.Get(moonlight.StringValue("exitCode")).TryInt(); ok {
|
2022-06-03 02:33:30 +00:00
|
|
|
exitCode = uint8(code)
|
|
|
|
}
|
|
|
|
|
2024-07-21 15:37:08 +00:00
|
|
|
if inp, ok := runner.Get(moonlight.StringValue("input")).TryString(); ok {
|
2022-06-03 02:33:30 +00:00
|
|
|
input = inp
|
|
|
|
}
|
|
|
|
|
2024-07-21 15:37:08 +00:00
|
|
|
if errStr, ok := runner.Get(moonlight.StringValue("err")).TryString(); ok {
|
2022-06-20 19:47:19 +00:00
|
|
|
runnerErr = fmt.Errorf("%s", errStr)
|
2022-06-03 02:33:30 +00:00
|
|
|
}
|
|
|
|
|
2024-07-21 15:37:08 +00:00
|
|
|
if c, ok := runner.Get(moonlight.StringValue("continue")).TryBool(); ok {
|
2022-06-03 02:33:30 +00:00
|
|
|
continued = c
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-31 03:07:24 +00:00
|
|
|
func handleSh(cmdString string) (input string, exitCode uint8, cont bool, runErr error) {
|
2024-07-21 15:37:08 +00:00
|
|
|
shRunner := hshMod.Get(moonlight.StringValue("runner")).AsTable().Get(moonlight.StringValue("sh"))
|
2022-08-31 03:07:24 +00:00
|
|
|
var err error
|
|
|
|
input, exitCode, cont, runErr, err = runLuaRunner(shRunner, cmdString)
|
|
|
|
if err != nil {
|
|
|
|
runErr = err
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func execSh(cmdString string) (string, uint8, bool, error) {
|
2024-04-28 01:03:54 +00:00
|
|
|
_, _, err := execCommand(cmdString, nil)
|
2021-04-05 19:21:44 +00:00
|
|
|
if err != nil {
|
|
|
|
// If input is incomplete, start multiline prompting
|
|
|
|
if syntax.IsIncomplete(err) {
|
2022-04-12 23:42:57 +00:00
|
|
|
if !interactive {
|
2022-06-03 02:33:30 +00:00
|
|
|
return cmdString, 126, false, err
|
2021-04-05 00:30:47 +00:00
|
|
|
}
|
2022-06-03 02:33:30 +00:00
|
|
|
return cmdString, 126, true, err
|
2021-04-05 00:30:47 +00:00
|
|
|
} else {
|
2021-04-05 19:21:44 +00:00
|
|
|
if code, ok := interp.IsExitStatus(err); ok {
|
2022-06-03 02:33:30 +00:00
|
|
|
return cmdString, code, false, nil
|
2021-04-19 02:09:27 +00:00
|
|
|
} else {
|
2022-06-03 02:33:30 +00:00
|
|
|
return cmdString, 126, false, err
|
2021-04-19 02:09:27 +00:00
|
|
|
}
|
2021-04-05 00:30:47 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-20 19:15:44 +00:00
|
|
|
|
2022-06-03 02:33:30 +00:00
|
|
|
return cmdString, 0, false, nil
|
2021-04-03 17:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run command in sh interpreter
|
2024-04-28 01:03:54 +00:00
|
|
|
func execCommand(cmd string, strms *streams) (io.Writer, io.Writer, error) {
|
2021-04-03 17:13:45 +00:00
|
|
|
file, err := syntax.NewParser().Parse(strings.NewReader(cmd), "")
|
|
|
|
if err != nil {
|
2022-04-04 10:40:02 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
2022-04-05 01:20:02 +00:00
|
|
|
|
2024-04-28 01:03:54 +00:00
|
|
|
if strms == nil {
|
|
|
|
strms = &streams{}
|
2021-04-03 17:13:45 +00:00
|
|
|
}
|
2024-04-28 01:03:54 +00:00
|
|
|
|
|
|
|
if strms.stdout == nil {
|
|
|
|
strms.stdout = os.Stdout
|
|
|
|
}
|
|
|
|
|
|
|
|
if strms.stderr == nil {
|
|
|
|
strms.stderr = os.Stderr
|
|
|
|
}
|
|
|
|
|
|
|
|
if strms.stdin == nil {
|
|
|
|
strms.stdin = os.Stdin
|
|
|
|
}
|
|
|
|
|
|
|
|
interp.StdIO(strms.stdin, strms.stdout, strms.stderr)(runner)
|
2024-07-30 21:57:53 +00:00
|
|
|
interp.Env(nil)(runner)
|
2024-04-28 01:03:54 +00:00
|
|
|
|
2022-04-05 01:20:02 +00:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
printer := syntax.NewPrinter()
|
2021-09-26 02:45:31 +00:00
|
|
|
|
2022-03-19 17:10:50 +00:00
|
|
|
var bg bool
|
2022-04-05 01:20:02 +00:00
|
|
|
for _, stmt := range file.Stmts {
|
|
|
|
bg = false
|
|
|
|
if stmt.Background {
|
|
|
|
bg = true
|
|
|
|
printer.Print(buf, stmt.Cmd)
|
|
|
|
|
|
|
|
stmtStr := buf.String()
|
|
|
|
buf.Reset()
|
2022-05-22 00:53:36 +00:00
|
|
|
jobs.add(stmtStr, []string{}, "")
|
2022-04-05 01:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interp.ExecHandler(execHandle(bg))(runner)
|
|
|
|
err = runner.Run(context.TODO(), stmt)
|
|
|
|
if err != nil {
|
2024-04-28 01:03:54 +00:00
|
|
|
return strms.stdout, strms.stderr, err
|
2022-04-05 01:20:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-28 01:03:54 +00:00
|
|
|
return strms.stdout, strms.stderr, nil
|
2022-04-05 01:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func execHandle(bg bool) interp.ExecHandlerFunc {
|
|
|
|
return func(ctx context.Context, args []string) error {
|
2021-09-26 02:45:31 +00:00
|
|
|
_, argstring := splitInput(strings.Join(args, " "))
|
2021-12-31 18:11:11 +00:00
|
|
|
// i dont really like this but it works
|
|
|
|
if aliases.All()[args[0]] != "" {
|
|
|
|
for i, arg := range args {
|
|
|
|
if strings.Contains(arg, " ") {
|
|
|
|
args[i] = fmt.Sprintf("\"%s\"", arg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, argstring = splitInput(strings.Join(args, " "))
|
2021-09-26 02:45:31 +00:00
|
|
|
|
2021-12-31 18:11:11 +00:00
|
|
|
// If alias was found, use command alias
|
|
|
|
argstring = aliases.Resolve(argstring)
|
2022-04-18 02:58:29 +00:00
|
|
|
var err error
|
|
|
|
args, err = shell.Fields(argstring, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-31 18:11:11 +00:00
|
|
|
}
|
2021-09-26 02:45:31 +00:00
|
|
|
|
|
|
|
// If command is defined in Lua then run it
|
2022-04-04 10:40:02 +00:00
|
|
|
luacmdArgs := rt.NewTable()
|
|
|
|
for i, str := range args[1:] {
|
|
|
|
luacmdArgs.Set(rt.IntValue(int64(i + 1)), rt.StringValue(str))
|
2021-10-17 23:37:37 +00:00
|
|
|
}
|
|
|
|
|
2023-01-20 23:07:42 +00:00
|
|
|
hc := interp.HandlerCtx(ctx)
|
2024-04-28 01:29:06 +00:00
|
|
|
if cmd := cmds.Commands[args[0]]; cmd != nil {
|
2023-01-20 23:07:42 +00:00
|
|
|
stdin := newSinkInput(hc.Stdin)
|
|
|
|
stdout := newSinkOutput(hc.Stdout)
|
|
|
|
stderr := newSinkOutput(hc.Stderr)
|
|
|
|
|
|
|
|
sinks := rt.NewTable()
|
|
|
|
sinks.Set(rt.StringValue("in"), rt.UserDataValue(stdin.ud))
|
2024-04-28 01:03:54 +00:00
|
|
|
sinks.Set(rt.StringValue("input"), rt.UserDataValue(stdin.ud))
|
2023-01-20 23:07:42 +00:00
|
|
|
sinks.Set(rt.StringValue("out"), rt.UserDataValue(stdout.ud))
|
|
|
|
sinks.Set(rt.StringValue("err"), rt.UserDataValue(stderr.ud))
|
|
|
|
|
2024-07-20 15:34:39 +00:00
|
|
|
//t := rt.NewThread(l)
|
2024-07-19 22:46:46 +00:00
|
|
|
sig := make(chan os.Signal)
|
|
|
|
exit := make(chan bool)
|
|
|
|
|
|
|
|
luaexitcode := rt.IntValue(63)
|
|
|
|
var err error
|
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
exit <- true
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
signal.Notify(sig, os.Interrupt)
|
|
|
|
select {
|
|
|
|
case <-sig:
|
2024-07-20 15:34:39 +00:00
|
|
|
//t.KillContext()
|
2024-07-19 22:46:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
2024-07-20 15:00:01 +00:00
|
|
|
// TODO: call in thread function?
|
|
|
|
//luaexitcode, err = l.CallInThread1(t, rt.FunctionValue(cmd), rt.TableValue(luacmdArgs), rt.TableValue(sinks))
|
2024-07-19 22:46:46 +00:00
|
|
|
exit <- true
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-exit
|
2021-11-22 20:40:56 +00:00
|
|
|
if err != nil {
|
2022-04-04 10:40:02 +00:00
|
|
|
fmt.Fprintln(os.Stderr, "Error in command:\n" + err.Error())
|
2021-11-22 20:40:56 +00:00
|
|
|
return interp.NewExitStatus(1)
|
|
|
|
}
|
|
|
|
|
2021-12-07 21:27:07 +00:00
|
|
|
var exitcode uint8
|
2021-09-26 02:45:31 +00:00
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
if code, ok := luaexitcode.TryInt(); ok {
|
2021-09-26 02:45:31 +00:00
|
|
|
exitcode = uint8(code)
|
2022-04-04 10:40:02 +00:00
|
|
|
} else if luaexitcode != rt.NilValue {
|
|
|
|
// deregister commander
|
2024-04-28 01:29:06 +00:00
|
|
|
delete(cmds.Commands, args[0])
|
2022-04-04 10:40:02 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "Commander did not return number for exit code. %s, you're fired.\n", args[0])
|
2021-09-26 02:45:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return interp.NewExitStatus(exitcode)
|
|
|
|
}
|
|
|
|
|
2024-07-30 21:57:53 +00:00
|
|
|
path, err := lookpath(args[0])
|
2022-03-16 23:44:32 +00:00
|
|
|
if err == errNotExec {
|
2022-05-01 01:02:36 +00:00
|
|
|
return execError{
|
|
|
|
typ: "not-executable",
|
|
|
|
cmd: args[0],
|
|
|
|
code: 126,
|
|
|
|
colon: true,
|
|
|
|
err: errNotExec,
|
|
|
|
}
|
2021-11-21 23:45:44 +00:00
|
|
|
} else if err != nil {
|
2022-05-01 01:02:36 +00:00
|
|
|
return execError{
|
|
|
|
typ: "not-found",
|
|
|
|
cmd: args[0],
|
|
|
|
code: 127,
|
|
|
|
err: errNotFound,
|
|
|
|
}
|
2021-09-26 02:45:31 +00:00
|
|
|
}
|
2021-11-21 23:50:35 +00:00
|
|
|
|
2022-03-19 17:10:50 +00:00
|
|
|
killTimeout := 2 * time.Second
|
|
|
|
// from here is basically copy-paste of the default exec handler from
|
|
|
|
// sh/interp but with our job handling
|
|
|
|
|
|
|
|
env := hc.Env
|
|
|
|
envList := make([]string, 0, 64)
|
|
|
|
env.Each(func(name string, vr expand.Variable) bool {
|
2024-07-30 21:57:53 +00:00
|
|
|
if name == "PATH" {
|
|
|
|
pathEnv := os.Getenv("PATH")
|
|
|
|
envList = append(envList, "PATH="+pathEnv)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-03-19 17:10:50 +00:00
|
|
|
if !vr.IsSet() {
|
|
|
|
// If a variable is set globally but unset in the
|
|
|
|
// runner, we need to ensure it's not part of the final
|
|
|
|
// list. Seems like zeroing the element is enough.
|
|
|
|
// This is a linear search, but this scenario should be
|
|
|
|
// rare, and the number of variables shouldn't be large.
|
|
|
|
for i, kv := range envList {
|
|
|
|
if strings.HasPrefix(kv, name+"=") {
|
|
|
|
envList[i] = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if vr.Exported && vr.Kind == expand.String {
|
|
|
|
envList = append(envList, name+"="+vr.String())
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
2024-07-30 21:57:53 +00:00
|
|
|
|
2022-03-19 17:10:50 +00:00
|
|
|
cmd := exec.Cmd{
|
|
|
|
Path: path,
|
|
|
|
Args: args,
|
|
|
|
Env: envList,
|
|
|
|
Dir: hc.Dir,
|
|
|
|
Stdin: hc.Stdin,
|
|
|
|
Stdout: hc.Stdout,
|
|
|
|
Stderr: hc.Stderr,
|
|
|
|
}
|
|
|
|
|
2022-03-22 01:25:43 +00:00
|
|
|
var j *job
|
|
|
|
if bg {
|
|
|
|
j = jobs.getLatest()
|
2022-05-22 00:53:36 +00:00
|
|
|
j.setHandle(&cmd)
|
|
|
|
err = j.start()
|
|
|
|
} else {
|
|
|
|
err = cmd.Start()
|
2022-03-22 01:25:43 +00:00
|
|
|
}
|
2022-05-22 00:53:36 +00:00
|
|
|
|
2022-03-19 17:10:50 +00:00
|
|
|
if err == nil {
|
|
|
|
if done := ctx.Done(); done != nil {
|
|
|
|
go func() {
|
|
|
|
<-done
|
|
|
|
|
|
|
|
if killTimeout <= 0 || runtime.GOOS == "windows" {
|
|
|
|
cmd.Process.Signal(os.Kill)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: don't temporarily leak this goroutine
|
|
|
|
// if the program stops itself with the
|
|
|
|
// interrupt.
|
|
|
|
go func() {
|
|
|
|
time.Sleep(killTimeout)
|
|
|
|
cmd.Process.Signal(os.Kill)
|
|
|
|
}()
|
|
|
|
cmd.Process.Signal(os.Interrupt)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
err = cmd.Wait()
|
|
|
|
}
|
|
|
|
|
2022-05-22 00:53:36 +00:00
|
|
|
exit := handleExecErr(err)
|
|
|
|
|
2022-03-19 17:10:50 +00:00
|
|
|
if bg {
|
2022-03-22 01:25:43 +00:00
|
|
|
j.exitCode = int(exit)
|
|
|
|
j.finish()
|
2022-03-19 17:10:50 +00:00
|
|
|
}
|
|
|
|
return interp.NewExitStatus(exit)
|
2021-09-26 02:45:31 +00:00
|
|
|
}
|
2021-04-03 17:13:45 +00:00
|
|
|
}
|
|
|
|
|
2022-05-22 00:53:36 +00:00
|
|
|
func handleExecErr(err error) (exit uint8) {
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
|
|
|
switch x := err.(type) {
|
|
|
|
case *exec.ExitError:
|
|
|
|
// started, but errored - default to 1 if OS
|
|
|
|
// doesn't have exit statuses
|
|
|
|
if status, ok := x.Sys().(syscall.WaitStatus); ok {
|
|
|
|
if status.Signaled() {
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
exit = uint8(128 + status.Signal())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
exit = uint8(status.ExitStatus())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
exit = 1
|
|
|
|
return
|
|
|
|
case *exec.Error:
|
|
|
|
// did not start
|
|
|
|
//fmt.Fprintf(hc.Stderr, "%v\n", err)
|
|
|
|
exit = 127
|
|
|
|
default: return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2024-07-30 21:57:53 +00:00
|
|
|
func lookpath(file string) (string, error) { // custom lookpath function so we know if a command is found *and* is executable
|
2022-03-18 00:22:30 +00:00
|
|
|
var skip []string
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
skip = []string{"./", "../", "~/", "C:"}
|
|
|
|
} else {
|
|
|
|
skip = []string{"./", "/", "../", "~/"}
|
|
|
|
}
|
2021-11-23 03:49:23 +00:00
|
|
|
for _, s := range skip {
|
|
|
|
if strings.HasPrefix(file, s) {
|
2024-07-30 21:57:53 +00:00
|
|
|
return file, findExecutable(file, false, false)
|
2021-11-23 03:49:23 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-21 23:45:44 +00:00
|
|
|
for _, dir := range filepath.SplitList(os.Getenv("PATH")) {
|
|
|
|
path := filepath.Join(dir, file)
|
2022-03-18 00:22:30 +00:00
|
|
|
err := findExecutable(path, true, false)
|
2022-03-16 23:44:32 +00:00
|
|
|
if err == errNotExec {
|
2024-07-30 21:57:53 +00:00
|
|
|
return "", err
|
2021-11-21 23:45:44 +00:00
|
|
|
} else if err == nil {
|
2024-07-30 21:57:53 +00:00
|
|
|
return path, nil
|
2021-11-21 23:45:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-30 21:57:53 +00:00
|
|
|
return "", os.ErrNotExist
|
2021-11-21 23:45:44 +00:00
|
|
|
}
|
2022-01-29 21:43:12 +00:00
|
|
|
|
2021-04-03 17:13:45 +00:00
|
|
|
func splitInput(input string) ([]string, string) {
|
|
|
|
// end my suffering
|
|
|
|
// TODO: refactor this garbage
|
|
|
|
quoted := false
|
|
|
|
startlastcmd := false
|
|
|
|
lastcmddone := false
|
2021-06-11 22:03:52 +00:00
|
|
|
cmdArgs := []string{}
|
2021-04-03 17:13:45 +00:00
|
|
|
sb := &strings.Builder{}
|
|
|
|
cmdstr := &strings.Builder{}
|
2021-05-17 23:03:56 +00:00
|
|
|
lastcmd := "" //readline.GetHistory(readline.HistorySize() - 1)
|
2021-04-03 17:13:45 +00:00
|
|
|
|
|
|
|
for _, r := range input {
|
|
|
|
if r == '"' {
|
|
|
|
// start quoted input
|
|
|
|
// this determines if other runes are replaced
|
|
|
|
quoted = !quoted
|
|
|
|
// dont add back quotes
|
|
|
|
//sb.WriteRune(r)
|
|
|
|
} else if !quoted && r == '~' {
|
|
|
|
// if not in quotes and ~ is found then make it $HOME
|
|
|
|
sb.WriteString(os.Getenv("HOME"))
|
|
|
|
} else if !quoted && r == ' ' {
|
|
|
|
// if not quoted and there's a space then add to cmdargs
|
|
|
|
cmdArgs = append(cmdArgs, sb.String())
|
|
|
|
sb.Reset()
|
|
|
|
} else if !quoted && r == '^' && startlastcmd && !lastcmddone {
|
|
|
|
// if ^ is found, isnt in quotes and is
|
|
|
|
// the second occurence of the character and is
|
|
|
|
// the first time "^^" has been used
|
|
|
|
cmdstr.WriteString(lastcmd)
|
|
|
|
sb.WriteString(lastcmd)
|
|
|
|
|
|
|
|
startlastcmd = !startlastcmd
|
|
|
|
lastcmddone = !lastcmddone
|
|
|
|
|
|
|
|
continue
|
|
|
|
} else if !quoted && r == '^' && !lastcmddone {
|
|
|
|
// if ^ is found, isnt in quotes and is the
|
|
|
|
// first time of starting "^^"
|
|
|
|
startlastcmd = !startlastcmd
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
sb.WriteRune(r)
|
|
|
|
}
|
|
|
|
cmdstr.WriteRune(r)
|
|
|
|
}
|
|
|
|
if sb.Len() > 0 {
|
|
|
|
cmdArgs = append(cmdArgs, sb.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmdArgs, cmdstr.String()
|
|
|
|
}
|
|
|
|
|
2022-03-19 22:48:03 +00:00
|
|
|
func cmdFinish(code uint8, cmdstr string, private bool) {
|
2024-07-21 15:37:08 +00:00
|
|
|
hshMod.SetField("exitCode", moonlight.IntValue(int64(code)))
|
2022-04-04 10:40:02 +00:00
|
|
|
// using AsValue (to convert to lua type) on an interface which is an int
|
|
|
|
// results in it being unknown in lua .... ????
|
|
|
|
// so we allow the hook handler to take lua runtime Values
|
2024-07-21 15:37:08 +00:00
|
|
|
hooks.Emit("command.exit", moonlight.IntValue(int64(code)), cmdstr, private)
|
2021-10-17 23:37:37 +00:00
|
|
|
}
|