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"
|
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
|
|
|
|
2022-03-06 17:31:50 +00:00
|
|
|
"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-04-04 10:40:02 +00:00
|
|
|
var runnerMode rt.Value = rt.StringValue("hybrid")
|
2022-03-16 23:44:32 +00:00
|
|
|
|
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)
|
2021-10-14 12:51:29 +00:00
|
|
|
hooks.Em.Emit("command.preexec", input, cmdString)
|
2021-06-09 23:16:08 +00:00
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
if runnerMode.Type() == rt.StringType {
|
|
|
|
switch runnerMode.AsString() {
|
2022-03-20 19:15:44 +00:00
|
|
|
case "hybrid":
|
|
|
|
_, err := handleLua(cmdString)
|
|
|
|
if err == nil {
|
2022-04-05 01:21:46 +00:00
|
|
|
cmdFinish(0, input, priv)
|
2022-03-20 19:15:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
exitCode, err := handleSh(cmdString)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
}
|
2022-04-05 01:21:46 +00:00
|
|
|
cmdFinish(exitCode, input, priv)
|
2022-03-20 19:15:44 +00:00
|
|
|
case "hybridRev":
|
|
|
|
_, err := handleSh(cmdString)
|
|
|
|
if err == nil {
|
2022-04-05 01:21:46 +00:00
|
|
|
cmdFinish(0, input, priv)
|
2022-03-20 19:15:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
exitCode, err := handleLua(cmdString)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
}
|
2022-04-05 01:21:46 +00:00
|
|
|
cmdFinish(exitCode, input, priv)
|
2022-03-20 19:15:44 +00:00
|
|
|
case "lua":
|
|
|
|
exitCode, err := handleLua(cmdString)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
}
|
2022-04-05 01:21:46 +00:00
|
|
|
cmdFinish(exitCode, input, priv)
|
2022-03-20 19:15:44 +00:00
|
|
|
case "sh":
|
|
|
|
exitCode, err := handleSh(cmdString)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
}
|
2022-04-05 01:21:46 +00:00
|
|
|
cmdFinish(exitCode, input, priv)
|
2022-03-20 19:15:44 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// can only be a string or function so
|
2022-04-04 10:40:02 +00:00
|
|
|
term := rt.NewTerminationWith(l.MainThread().CurrentCont(), 2, false)
|
|
|
|
err := rt.Call(l.MainThread(), runnerMode, []rt.Value{rt.StringValue(cmdString)}, term)
|
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-04-04 10:40:02 +00:00
|
|
|
luaexitcode := term.Get(0) // first return value (makes sense right i love stacks)
|
|
|
|
runErr := term.Get(1)
|
2022-03-20 19:15:44 +00:00
|
|
|
|
|
|
|
var exitCode uint8
|
2022-04-04 10:40:02 +00:00
|
|
|
if code, ok := luaexitcode.TryInt(); ok {
|
2022-03-20 19:15:44 +00:00
|
|
|
exitCode = uint8(code)
|
|
|
|
}
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
if runErr != rt.NilValue {
|
2022-03-20 19:15:44 +00:00
|
|
|
fmt.Fprintln(os.Stderr, runErr)
|
|
|
|
}
|
2022-04-05 01:21:46 +00:00
|
|
|
cmdFinish(exitCode, input, priv)
|
2022-03-20 19:15:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleLua(cmdString string) (uint8, error) {
|
2021-05-08 12:56:24 +00:00
|
|
|
// First try to load input, essentially compiling to bytecode
|
2022-04-04 10:40:02 +00:00
|
|
|
chunk, err := l.CompileAndLoadLuaChunk("", []byte(cmdString), rt.TableValue(l.GlobalEnv()))
|
2021-05-08 12:56:24 +00:00
|
|
|
if err != nil && noexecute {
|
|
|
|
fmt.Println(err)
|
2021-06-12 14:41:51 +00:00
|
|
|
/* if lerr, ok := err.(*lua.ApiError); ok {
|
2021-05-16 19:50:49 +00:00
|
|
|
if perr, ok := lerr.Cause.(*parse.Error); ok {
|
|
|
|
print(perr.Pos.Line == parse.EOF)
|
|
|
|
}
|
|
|
|
}
|
2021-06-12 14:41:51 +00:00
|
|
|
*/
|
2022-03-20 19:15:44 +00:00
|
|
|
return 125, err
|
2021-05-08 12:56:24 +00:00
|
|
|
}
|
|
|
|
// And if there's no syntax errors and -n isnt provided, run
|
|
|
|
if !noexecute {
|
2022-04-04 10:40:02 +00:00
|
|
|
if chunk != nil {
|
|
|
|
_, err = rt.Call1(l.MainThread(), rt.FunctionValue(chunk))
|
|
|
|
}
|
2021-05-08 12:56:24 +00:00
|
|
|
}
|
2021-04-03 17:13:45 +00:00
|
|
|
if err == nil {
|
2022-03-20 19:15:44 +00:00
|
|
|
return 0, nil
|
2021-04-05 00:30:47 +00:00
|
|
|
}
|
|
|
|
|
2022-03-20 19:15:44 +00:00
|
|
|
return 125, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleSh(cmdString string) (uint8, error) {
|
2022-04-04 10:40:02 +00:00
|
|
|
_, _, err := execCommand(cmdString, true)
|
2021-04-05 19:21:44 +00:00
|
|
|
if err != nil {
|
|
|
|
// If input is incomplete, start multiline prompting
|
|
|
|
if syntax.IsIncomplete(err) {
|
|
|
|
for {
|
2021-12-06 21:21:31 +00:00
|
|
|
cmdString, err = continuePrompt(strings.TrimSuffix(cmdString, "\\"))
|
2021-04-19 02:09:27 +00:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2022-04-04 10:40:02 +00:00
|
|
|
_, _, err = execCommand(cmdString, true)
|
2022-03-20 19:15:44 +00:00
|
|
|
if syntax.IsIncomplete(err) || strings.HasSuffix(cmdString, "\\") {
|
2021-04-05 19:21:44 +00:00
|
|
|
continue
|
|
|
|
} else if code, ok := interp.IsExitStatus(err); ok {
|
2022-03-20 19:15:44 +00:00
|
|
|
return code, nil
|
2021-04-05 19:21:44 +00:00
|
|
|
} else if err != nil {
|
2022-03-20 19:15:44 +00:00
|
|
|
return 126, err
|
2022-03-06 01:25:32 +00:00
|
|
|
} else {
|
2022-03-20 19:15:44 +00:00
|
|
|
return 0, nil
|
2021-04-05 19:21:44 +00:00
|
|
|
}
|
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-03-20 19:15:44 +00:00
|
|
|
return code, nil
|
2021-04-19 02:09:27 +00:00
|
|
|
} else {
|
2022-03-20 19:15:44 +00:00
|
|
|
return 126, 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
|
|
|
|
|
|
|
return 0, nil
|
2021-04-03 17:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run command in sh interpreter
|
2022-04-04 10:40:02 +00:00
|
|
|
func execCommand(cmd string, terminalOut bool) (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
|
|
|
|
|
|
|
runner, _ := interp.New()
|
|
|
|
|
|
|
|
var stdout io.Writer
|
|
|
|
var stderr io.Writer
|
|
|
|
if terminalOut {
|
|
|
|
interp.StdIO(os.Stdin, os.Stdout, os.Stderr)(runner)
|
|
|
|
} else {
|
2022-04-04 10:40:02 +00:00
|
|
|
stdout = new(bytes.Buffer)
|
|
|
|
stderr = new(bytes.Buffer)
|
2022-04-05 01:20:02 +00:00
|
|
|
interp.StdIO(os.Stdin, stdout, stderr)(runner)
|
2021-04-03 17:13:45 +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()
|
|
|
|
jobs.add(stmtStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
interp.ExecHandler(execHandle(bg))(runner)
|
|
|
|
err = runner.Run(context.TODO(), stmt)
|
|
|
|
if err != nil {
|
|
|
|
return stdout, stderr, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stdout, stderr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
args, _ = shell.Fields(argstring, nil)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2021-09-26 02:45:31 +00:00
|
|
|
if commands[args[0]] != nil {
|
2022-04-04 10:40:02 +00:00
|
|
|
luaexitcode, err := rt.Call1(l.MainThread(), rt.FunctionValue(commands[args[0]]), rt.TableValue(luacmdArgs))
|
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
|
|
|
|
delete(commands, args[0])
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-11-21 23:45:44 +00:00
|
|
|
err := lookpath(args[0])
|
2022-03-16 23:44:32 +00:00
|
|
|
if err == errNotExec {
|
2021-11-21 23:45:44 +00:00
|
|
|
hooks.Em.Emit("command.no-perm", args[0])
|
2022-03-16 23:45:55 +00:00
|
|
|
hooks.Em.Emit("command.not-executable", args[0])
|
2021-11-21 23:45:44 +00:00
|
|
|
return interp.NewExitStatus(126)
|
|
|
|
} else if err != nil {
|
2021-09-26 02:45:31 +00:00
|
|
|
hooks.Em.Emit("command.not-found", args[0])
|
|
|
|
return interp.NewExitStatus(127)
|
|
|
|
}
|
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
|
|
|
|
hc := interp.HandlerCtx(ctx)
|
|
|
|
path, err := interp.LookPathDir(hc.Dir, hc.Env, args[0])
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(hc.Stderr, err)
|
|
|
|
return interp.NewExitStatus(127)
|
|
|
|
}
|
|
|
|
|
|
|
|
env := hc.Env
|
|
|
|
envList := make([]string, 0, 64)
|
|
|
|
env.Each(func(name string, vr expand.Variable) bool {
|
|
|
|
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
|
|
|
|
})
|
|
|
|
cmd := exec.Cmd{
|
|
|
|
Path: path,
|
|
|
|
Args: args,
|
|
|
|
Env: envList,
|
|
|
|
Dir: hc.Dir,
|
|
|
|
Stdin: hc.Stdin,
|
|
|
|
Stdout: hc.Stdout,
|
|
|
|
Stderr: hc.Stderr,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = cmd.Start()
|
2022-03-22 01:25:43 +00:00
|
|
|
var j *job
|
|
|
|
if bg {
|
|
|
|
j = jobs.getLatest()
|
|
|
|
j.setHandle(cmd.Process)
|
|
|
|
}
|
2022-03-19 17:10:50 +00:00
|
|
|
if err == nil {
|
|
|
|
if bg {
|
2022-03-22 01:25:43 +00:00
|
|
|
j.start(cmd.Process.Pid)
|
2022-03-19 17:10:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
var exit uint8
|
|
|
|
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 ctx.Err()
|
|
|
|
}
|
|
|
|
exit = uint8(128 + status.Signal())
|
|
|
|
goto end
|
|
|
|
}
|
|
|
|
exit = uint8(status.ExitStatus())
|
|
|
|
goto end
|
|
|
|
}
|
|
|
|
exit = 1
|
|
|
|
goto end
|
|
|
|
case *exec.Error:
|
|
|
|
// did not start
|
|
|
|
fmt.Fprintf(hc.Stderr, "%v\n", err)
|
|
|
|
exit = 127
|
|
|
|
goto end
|
|
|
|
case nil:
|
|
|
|
goto end
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
end:
|
|
|
|
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-03-16 23:44:32 +00:00
|
|
|
func lookpath(file 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) {
|
2022-03-18 00:22:30 +00:00
|
|
|
return 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 {
|
2021-11-21 23:45:44 +00:00
|
|
|
return err
|
|
|
|
} else if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return os.ErrNotExist
|
|
|
|
}
|
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) {
|
2022-02-27 23:12:58 +00:00
|
|
|
// if input has space at the beginning, dont put in history
|
2022-03-19 22:48:03 +00:00
|
|
|
if interactive && !private {
|
|
|
|
handleHistory(cmdstr)
|
2022-02-27 23:12:58 +00:00
|
|
|
}
|
2022-04-04 10:40:02 +00:00
|
|
|
util.SetField(l, hshMod, "exitCode", rt.IntValue(int64(code)), "Exit code of last exected command")
|
|
|
|
// 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
|
|
|
|
hooks.Em.Emit("command.exit", rt.IntValue(int64(code)), cmdstr)
|
2021-10-17 23:37:37 +00:00
|
|
|
}
|