2021-04-03 17:13:45 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-04-19 02:09:27 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2021-11-21 23:45:44 +00:00
|
|
|
"path/filepath"
|
2021-04-03 17:13:45 +00:00
|
|
|
"strings"
|
2021-09-26 02:45:31 +00:00
|
|
|
"time"
|
2021-04-03 17:13:45 +00:00
|
|
|
|
2021-04-28 22:52:42 +00:00
|
|
|
"github.com/yuin/gopher-lua"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func RunInput(input string) {
|
2021-12-06 19:45:35 +00:00
|
|
|
running = true
|
2021-05-19 22:39:56 +00:00
|
|
|
cmdArgs, cmdString := splitInput(input)
|
2021-05-16 12:43:19 +00:00
|
|
|
|
2021-06-09 23:16:08 +00:00
|
|
|
// If alias was found, use command alias
|
|
|
|
for aliases[cmdArgs[0]] != "" {
|
|
|
|
alias := aliases[cmdArgs[0]]
|
|
|
|
cmdString = alias + strings.TrimPrefix(cmdString, cmdArgs[0])
|
|
|
|
cmdArgs, cmdString = splitInput(cmdString)
|
|
|
|
|
2021-10-12 01:45:52 +00:00
|
|
|
if aliases[cmdArgs[0]] == alias {
|
|
|
|
break
|
|
|
|
}
|
2021-06-09 23:16:08 +00:00
|
|
|
if aliases[cmdArgs[0]] != "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2021-10-14 12:51:29 +00:00
|
|
|
hooks.Em.Emit("command.preexec", input, cmdString)
|
2021-06-09 23:16:08 +00:00
|
|
|
|
2021-05-08 12:56:24 +00:00
|
|
|
// First try to load input, essentially compiling to bytecode
|
2021-05-16 12:43:19 +00:00
|
|
|
fn, err := l.LoadString(cmdString)
|
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
|
|
|
*/
|
2021-05-08 12:56:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// And if there's no syntax errors and -n isnt provided, run
|
|
|
|
if !noexecute {
|
|
|
|
l.Push(fn)
|
|
|
|
err = l.PCall(0, lua.MultRet, nil)
|
|
|
|
}
|
2021-04-03 17:13:45 +00:00
|
|
|
if err == nil {
|
2021-10-17 23:37:37 +00:00
|
|
|
cmdFinish(0, cmdString)
|
2021-04-05 00:30:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Last option: use sh interpreter
|
2021-04-05 19:21:44 +00:00
|
|
|
err = execCommand(cmdString)
|
|
|
|
if err != nil {
|
|
|
|
// If input is incomplete, start multiline prompting
|
|
|
|
if syntax.IsIncomplete(err) {
|
|
|
|
for {
|
|
|
|
cmdString, err = ContinuePrompt(strings.TrimSuffix(cmdString, "\\"))
|
2021-04-19 02:09:27 +00:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2021-04-05 19:21:44 +00:00
|
|
|
err = execCommand(cmdString)
|
2021-04-19 02:09:27 +00:00
|
|
|
if syntax.IsIncomplete(err) || strings.HasSuffix(input, "\\") {
|
2021-04-05 19:21:44 +00:00
|
|
|
continue
|
|
|
|
} else if code, ok := interp.IsExitStatus(err); ok {
|
2021-10-17 23:37:37 +00:00
|
|
|
cmdFinish(code, cmdString)
|
2021-04-05 19:21:44 +00:00
|
|
|
} else if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
2021-10-17 23:37:37 +00:00
|
|
|
cmdFinish(1, cmdString)
|
2021-04-05 19:21:44 +00:00
|
|
|
}
|
|
|
|
break
|
2021-04-05 00:30:47 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-04-05 19:21:44 +00:00
|
|
|
if code, ok := interp.IsExitStatus(err); ok {
|
2021-10-17 23:37:37 +00:00
|
|
|
cmdFinish(code, cmdString)
|
2021-04-19 02:09:27 +00:00
|
|
|
} else {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
}
|
2021-04-05 00:30:47 +00:00
|
|
|
}
|
2021-04-05 19:21:44 +00:00
|
|
|
} else {
|
2021-10-17 23:37:37 +00:00
|
|
|
cmdFinish(0, cmdString)
|
2021-04-05 00:30:47 +00:00
|
|
|
}
|
2021-04-03 17:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run command in sh interpreter
|
|
|
|
func execCommand(cmd string) error {
|
|
|
|
file, err := syntax.NewParser().Parse(strings.NewReader(cmd), "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-09-26 02:45:31 +00:00
|
|
|
|
|
|
|
exechandle := func(ctx context.Context, args []string) error {
|
|
|
|
_, argstring := splitInput(strings.Join(args, " "))
|
|
|
|
|
|
|
|
// If alias was found, use command alias
|
2021-10-17 23:37:37 +00:00
|
|
|
for aliases[args[0]] != "" {
|
2021-09-26 02:45:31 +00:00
|
|
|
alias := aliases[args[0]]
|
|
|
|
argstring = alias + strings.TrimPrefix(argstring, args[0])
|
|
|
|
cmdArgs, _ := splitInput(argstring)
|
|
|
|
args = cmdArgs
|
2021-10-17 23:37:37 +00:00
|
|
|
|
|
|
|
if aliases[args[0]] == alias {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if aliases[args[0]] != "" {
|
|
|
|
continue
|
|
|
|
}
|
2021-09-26 02:45:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If command is defined in Lua then run it
|
2021-10-17 23:37:37 +00:00
|
|
|
luacmdArgs := l.NewTable()
|
|
|
|
for _, str := range args[1:] {
|
|
|
|
luacmdArgs.Append(lua.LString(str))
|
|
|
|
}
|
|
|
|
|
2021-09-26 02:45:31 +00:00
|
|
|
if commands[args[0]] != nil {
|
|
|
|
err := l.CallByParam(lua.P{
|
|
|
|
Fn: commands[args[0]],
|
|
|
|
NRet: 1,
|
|
|
|
Protect: true,
|
2021-10-17 23:37:37 +00:00
|
|
|
}, luacmdArgs)
|
2021-11-22 20:40:56 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr,
|
|
|
|
"Error in command:\n\n" + err.Error())
|
|
|
|
return interp.NewExitStatus(1)
|
|
|
|
}
|
|
|
|
|
2021-09-26 02:45:31 +00:00
|
|
|
luaexitcode := l.Get(-1)
|
|
|
|
var exitcode uint8 = 0
|
|
|
|
|
|
|
|
l.Pop(1)
|
|
|
|
|
|
|
|
if code, ok := luaexitcode.(lua.LNumber); luaexitcode != lua.LNil && ok {
|
|
|
|
exitcode = uint8(code)
|
|
|
|
}
|
|
|
|
|
2021-10-17 23:37:37 +00:00
|
|
|
cmdFinish(exitcode, argstring)
|
2021-09-26 02:45:31 +00:00
|
|
|
return interp.NewExitStatus(exitcode)
|
|
|
|
}
|
|
|
|
|
2021-11-21 23:45:44 +00:00
|
|
|
err := lookpath(args[0])
|
|
|
|
if err == os.ErrPermission {
|
|
|
|
hooks.Em.Emit("command.no-perm", args[0])
|
|
|
|
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
|
|
|
|
2021-09-26 02:45:31 +00:00
|
|
|
return interp.DefaultExecHandler(2 * time.Second)(ctx, args)
|
|
|
|
}
|
|
|
|
runner, _ := interp.New(
|
|
|
|
interp.StdIO(os.Stdin, os.Stdout, os.Stderr),
|
|
|
|
interp.ExecHandler(exechandle),
|
|
|
|
)
|
2021-04-03 17:13:45 +00:00
|
|
|
err = runner.Run(context.TODO(), file)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-21 23:45:44 +00:00
|
|
|
// custom lookpath function so we know if a command is found *and* has execute permission
|
|
|
|
func lookpath(file string) error {
|
2021-11-23 03:49:23 +00:00
|
|
|
skip := []string{"./", "/", "../", "~/"}
|
|
|
|
for _, s := range skip {
|
|
|
|
if strings.HasPrefix(file, s) {
|
|
|
|
err := findExecutable(file)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-11-21 23:45:44 +00:00
|
|
|
for _, dir := range filepath.SplitList(os.Getenv("PATH")) {
|
|
|
|
path := filepath.Join(dir, file)
|
|
|
|
err := findExecutable(path)
|
|
|
|
if err == os.ErrPermission {
|
|
|
|
return err
|
|
|
|
} else if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2021-10-17 23:37:37 +00:00
|
|
|
func cmdFinish(code uint8, cmdstr string) {
|
|
|
|
hooks.Em.Emit("command.exit", code, cmdstr)
|
|
|
|
}
|