2021-03-19 19:11:59 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-03-24 23:18:30 +00:00
|
|
|
"bufio"
|
2021-03-19 19:11:59 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2021-03-28 21:11:57 +00:00
|
|
|
"os/user"
|
2021-03-20 04:01:24 +00:00
|
|
|
"syscall"
|
2021-03-19 19:11:59 +00:00
|
|
|
"os/signal"
|
|
|
|
"strings"
|
2021-03-20 16:57:18 +00:00
|
|
|
"io"
|
2021-03-24 18:50:22 +00:00
|
|
|
"context"
|
2021-03-30 23:47:02 +00:00
|
|
|
hooks "hilbish/golibs/bait"
|
2021-03-20 04:01:24 +00:00
|
|
|
|
|
|
|
"github.com/akamensky/argparse"
|
|
|
|
"github.com/bobappleyard/readline"
|
2021-03-19 23:03:11 +00:00
|
|
|
"github.com/yuin/gopher-lua"
|
2021-03-20 22:51:05 +00:00
|
|
|
"layeh.com/gopher-luar"
|
2021-03-24 18:50:22 +00:00
|
|
|
"mvdan.cc/sh/v3/interp"
|
|
|
|
"mvdan.cc/sh/v3/syntax"
|
|
|
|
|
2021-03-19 19:11:59 +00:00
|
|
|
)
|
|
|
|
|
2021-03-30 23:50:35 +00:00
|
|
|
const version = "0.2.0"
|
2021-03-19 23:03:11 +00:00
|
|
|
var l *lua.LState
|
2021-03-28 22:58:58 +00:00
|
|
|
// User's prompt, this will get set when lua side is initialized
|
2021-03-19 23:03:11 +00:00
|
|
|
var prompt string
|
2021-03-28 22:58:58 +00:00
|
|
|
// Map of builtin/custom commands defined in the commander lua module
|
2021-03-20 22:51:05 +00:00
|
|
|
var commands = map[string]bool{}
|
2021-03-28 22:58:58 +00:00
|
|
|
// Command aliases
|
2021-03-21 21:19:51 +00:00
|
|
|
var aliases = map[string]string{}
|
2021-03-30 23:47:02 +00:00
|
|
|
var bait hooks.Bait
|
2021-03-19 23:03:11 +00:00
|
|
|
|
2021-03-19 19:11:59 +00:00
|
|
|
func main() {
|
2021-03-20 04:01:24 +00:00
|
|
|
parser := argparse.NewParser("hilbish", "A shell for lua and flower lovers")
|
|
|
|
verflag := parser.Flag("v", "version", &argparse.Options{
|
|
|
|
Required: false,
|
2021-03-25 00:58:31 +00:00
|
|
|
Help: "Prints Hilbish version",
|
|
|
|
})
|
|
|
|
setshflag := parser.Flag("S", "set-shell-env", &argparse.Options{
|
|
|
|
Required: false,
|
|
|
|
Help: "Sets $SHELL to Hilbish's executed path",
|
2021-03-20 04:01:24 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
err := parser.Parse(os.Args)
|
2021-03-28 22:58:58 +00:00
|
|
|
// If invalid flags or --help/-h,
|
2021-03-20 04:01:24 +00:00
|
|
|
if err != nil {
|
2021-03-28 22:58:58 +00:00
|
|
|
// Print usage
|
2021-03-20 04:01:24 +00:00
|
|
|
fmt.Print(parser.Usage(err))
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
if *verflag {
|
|
|
|
fmt.Printf("Hilbish v%s\n", version)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2021-03-28 22:58:58 +00:00
|
|
|
// Set $SHELL if the user wants to
|
2021-03-25 00:58:31 +00:00
|
|
|
if *setshflag { os.Setenv("SHELL", os.Args[0]) }
|
2021-03-21 07:51:44 +00:00
|
|
|
|
|
|
|
homedir, _ := os.UserHomeDir()
|
2021-03-28 22:58:58 +00:00
|
|
|
// If user's config doesn't exixt,
|
2021-03-25 16:07:51 +00:00
|
|
|
if _, err := os.Stat(homedir + "/.hilbishrc.lua"); os.IsNotExist(err) {
|
2021-03-30 23:47:02 +00:00
|
|
|
// Read default from current directory
|
|
|
|
// (this is assuming the current dir is Hilbish's git)
|
|
|
|
input, err := os.ReadFile(".hilbishrc.lua")
|
|
|
|
if err != nil {
|
|
|
|
// If it wasnt found, go to "real default"
|
|
|
|
input, err = os.ReadFile("/usr/share/hilbish/.hilbishrc.lua")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("could not find .hilbishrc.lua or /usr/share/hilbish/.hilbishrc.lua")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-28 22:58:58 +00:00
|
|
|
// Create it using either default config we found
|
2021-03-25 16:07:51 +00:00
|
|
|
err = os.WriteFile(homedir + "/.hilbishrc.lua", input, 0644)
|
|
|
|
if err != nil {
|
2021-03-28 22:58:58 +00:00
|
|
|
// If that fails, bail
|
2021-03-25 16:07:51 +00:00
|
|
|
fmt.Println("Error creating config file")
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2021-03-21 07:51:44 +00:00
|
|
|
|
2021-03-19 19:11:59 +00:00
|
|
|
HandleSignals()
|
2021-03-19 23:03:11 +00:00
|
|
|
LuaInit()
|
2021-03-19 19:11:59 +00:00
|
|
|
|
2021-03-24 18:50:22 +00:00
|
|
|
readline.Completer = readline.FilenameCompleter
|
2021-03-28 21:11:57 +00:00
|
|
|
|
2021-03-19 19:11:59 +00:00
|
|
|
for {
|
2021-03-26 05:06:14 +00:00
|
|
|
cmdString, err := readline.String(fmtPrompt())
|
2021-03-20 16:57:18 +00:00
|
|
|
if err == io.EOF {
|
2021-03-28 22:58:58 +00:00
|
|
|
// Exit if user presses ^D (ctrl + d)
|
2021-03-20 16:57:18 +00:00
|
|
|
fmt.Println("")
|
|
|
|
break
|
|
|
|
}
|
2021-03-19 19:11:59 +00:00
|
|
|
if err != nil {
|
2021-03-28 22:58:58 +00:00
|
|
|
// If we get a completely random error, print
|
2021-03-19 19:11:59 +00:00
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
}
|
2021-03-28 22:58:58 +00:00
|
|
|
|
|
|
|
// I have no idea if we need this anymore
|
2021-03-19 19:11:59 +00:00
|
|
|
cmdString = strings.TrimSuffix(cmdString, "\n")
|
2021-03-28 22:58:58 +00:00
|
|
|
// First try to run user input in Lua
|
2021-03-19 23:03:11 +00:00
|
|
|
err = l.DoString(cmdString)
|
|
|
|
|
2021-03-25 00:21:55 +00:00
|
|
|
if err == nil {
|
2021-03-28 22:58:58 +00:00
|
|
|
// If it succeeds, add to history and prompt again
|
2021-03-25 00:21:55 +00:00
|
|
|
readline.AddHistory(cmdString)
|
2021-03-31 16:53:33 +00:00
|
|
|
bait.Em.Emit("command.success", nil)
|
2021-03-25 00:21:55 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-03-19 23:03:11 +00:00
|
|
|
|
2021-03-28 22:58:58 +00:00
|
|
|
// Split up the input
|
2021-03-28 22:15:51 +00:00
|
|
|
cmdArgs, cmdString := splitInput(cmdString)
|
2021-03-28 22:58:58 +00:00
|
|
|
// If there's actually no input, prompt again
|
2021-03-19 19:11:59 +00:00
|
|
|
if len(cmdArgs) == 0 { continue }
|
|
|
|
|
2021-03-28 22:58:58 +00:00
|
|
|
// If alias was found, use command alias
|
2021-03-21 21:19:51 +00:00
|
|
|
if aliases[cmdArgs[0]] != "" {
|
|
|
|
cmdString = aliases[cmdArgs[0]] + strings.Trim(cmdString, cmdArgs[0])
|
2021-03-24 18:50:22 +00:00
|
|
|
execCommand(cmdString)
|
2021-03-21 21:19:51 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-03-28 22:58:58 +00:00
|
|
|
// If command is defined in Lua then run it
|
2021-03-20 22:51:05 +00:00
|
|
|
if commands[cmdArgs[0]] {
|
|
|
|
err := l.CallByParam(lua.P{
|
|
|
|
Fn: l.GetField(
|
|
|
|
l.GetTable(
|
2021-03-21 16:42:04 +00:00
|
|
|
l.GetGlobal("commanding"),
|
2021-03-20 22:51:05 +00:00
|
|
|
lua.LString("__commands")),
|
|
|
|
cmdArgs[0]),
|
|
|
|
NRet: 0,
|
|
|
|
Protect: true,
|
|
|
|
}, luar.New(l, cmdArgs[1:]))
|
|
|
|
if err != nil {
|
|
|
|
// TODO: dont panic
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-03-24 18:50:22 +00:00
|
|
|
readline.AddHistory(cmdString)
|
2021-03-20 22:51:05 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-03-28 22:58:58 +00:00
|
|
|
|
|
|
|
// Last option: use sh interpreter
|
2021-03-19 19:11:59 +00:00
|
|
|
switch cmdArgs[0] {
|
|
|
|
case "exit":
|
|
|
|
os.Exit(0)
|
2021-03-20 05:22:43 +00:00
|
|
|
default:
|
2021-03-24 18:50:22 +00:00
|
|
|
err := execCommand(cmdString)
|
2021-03-21 21:19:51 +00:00
|
|
|
if err != nil {
|
2021-03-28 22:58:58 +00:00
|
|
|
// If input is incomplete, start multiline prompting
|
2021-03-24 23:18:30 +00:00
|
|
|
if syntax.IsIncomplete(err) {
|
|
|
|
sb := &strings.Builder{}
|
|
|
|
for {
|
|
|
|
done := StartMultiline(cmdString, sb)
|
|
|
|
if done {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-03-30 23:47:02 +00:00
|
|
|
if code, ok := interp.IsExitStatus(err); ok {
|
|
|
|
if code > 0 {
|
|
|
|
bait.Em.Emit("command.fail", code)
|
|
|
|
}
|
|
|
|
}
|
2021-03-24 23:18:30 +00:00
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
}
|
2021-03-30 23:47:02 +00:00
|
|
|
} else {
|
|
|
|
bait.Em.Emit("command.success", nil)
|
2021-03-20 05:22:43 +00:00
|
|
|
}
|
2021-03-19 19:11:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-28 22:58:58 +00:00
|
|
|
// This semi cursed function formats our prompt (obviously)
|
2021-03-26 05:06:14 +00:00
|
|
|
func fmtPrompt() string {
|
2021-03-28 21:11:57 +00:00
|
|
|
user, _ := user.Current()
|
|
|
|
host, _ := os.Hostname()
|
|
|
|
cwd, _ := os.Getwd()
|
2021-03-28 22:58:58 +00:00
|
|
|
|
2021-03-28 21:11:57 +00:00
|
|
|
cwd = strings.Replace(cwd, user.HomeDir, "~", 1)
|
2021-03-28 22:58:58 +00:00
|
|
|
|
2021-03-28 21:11:57 +00:00
|
|
|
args := []string{
|
2021-03-28 22:58:58 +00:00
|
|
|
"d", cwd,
|
|
|
|
"h", host,
|
|
|
|
"u", user.Name,
|
2021-03-28 21:11:57 +00:00
|
|
|
}
|
2021-03-28 22:58:58 +00:00
|
|
|
|
2021-03-28 21:11:57 +00:00
|
|
|
for i, v := range args {
|
|
|
|
if i % 2 == 0 {
|
|
|
|
args[i] = "%" + v
|
|
|
|
}
|
|
|
|
}
|
2021-03-28 22:58:58 +00:00
|
|
|
|
2021-03-28 21:11:57 +00:00
|
|
|
r := strings.NewReplacer(args...)
|
|
|
|
nprompt := r.Replace(prompt)
|
2021-03-28 22:58:58 +00:00
|
|
|
|
2021-03-28 21:11:57 +00:00
|
|
|
return nprompt
|
2021-03-26 05:06:14 +00:00
|
|
|
}
|
|
|
|
|
2021-03-24 23:18:30 +00:00
|
|
|
func StartMultiline(prev string, sb *strings.Builder) bool {
|
2021-03-28 22:58:58 +00:00
|
|
|
// sb fromt outside is passed so we can
|
|
|
|
// save input from previous prompts
|
2021-03-24 23:18:30 +00:00
|
|
|
if sb.String() == "" { sb.WriteString(prev + "\n") }
|
|
|
|
|
|
|
|
fmt.Printf("... ")
|
|
|
|
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
|
|
|
|
cont, err := reader.ReadString('\n')
|
|
|
|
if err == io.EOF {
|
2021-03-28 22:58:58 +00:00
|
|
|
// Exit when ^D
|
2021-03-24 23:18:30 +00:00
|
|
|
fmt.Println("")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
sb.WriteString(cont)
|
|
|
|
|
|
|
|
err = execCommand(sb.String())
|
|
|
|
if err != nil && syntax.IsIncomplete(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-03-28 22:58:58 +00:00
|
|
|
return true
|
2021-03-24 23:18:30 +00:00
|
|
|
}
|
|
|
|
|
2021-03-28 22:15:51 +00:00
|
|
|
func splitInput(input string) ([]string, string) {
|
|
|
|
// end my suffering
|
|
|
|
// TODO: refactor this garbage
|
2021-03-21 21:19:51 +00:00
|
|
|
quoted := false
|
2021-03-28 22:15:51 +00:00
|
|
|
startlastcmd := false
|
|
|
|
lastcmddone := false
|
2021-03-21 21:19:51 +00:00
|
|
|
cmdArgs := []string{}
|
|
|
|
sb := &strings.Builder{}
|
2021-03-28 22:15:51 +00:00
|
|
|
cmdstr := &strings.Builder{}
|
|
|
|
lastcmd := readline.GetHistory(readline.HistorySize() - 1)
|
2021-03-21 21:19:51 +00:00
|
|
|
|
|
|
|
for _, r := range input {
|
|
|
|
if r == '"' {
|
2021-03-28 22:58:58 +00:00
|
|
|
// start quoted input
|
|
|
|
// this determines if other runes are replaced
|
2021-03-21 21:19:51 +00:00
|
|
|
quoted = !quoted
|
|
|
|
// dont add back quotes
|
|
|
|
//sb.WriteRune(r)
|
|
|
|
} else if !quoted && r == '~' {
|
2021-03-28 22:58:58 +00:00
|
|
|
// if not in quotes and ~ is found then make it $HOME
|
2021-03-21 21:19:51 +00:00
|
|
|
sb.WriteString(os.Getenv("HOME"))
|
|
|
|
} else if !quoted && r == ' ' {
|
2021-03-28 22:58:58 +00:00
|
|
|
// if not quoted and there's a space then add to cmdargs
|
2021-03-21 21:19:51 +00:00
|
|
|
cmdArgs = append(cmdArgs, sb.String())
|
|
|
|
sb.Reset()
|
2021-03-28 22:15:51 +00:00
|
|
|
} else if !quoted && r == '^' && startlastcmd && !lastcmddone {
|
2021-03-28 22:58:58 +00:00
|
|
|
// if ^ is found, isnt in quotes and is
|
|
|
|
// the second occurence of the character and is
|
|
|
|
// the first time "^^" has been used
|
2021-03-28 22:15:51 +00:00
|
|
|
cmdstr.WriteString(lastcmd)
|
|
|
|
sb.WriteString(lastcmd)
|
2021-03-28 22:58:58 +00:00
|
|
|
|
2021-03-28 22:15:51 +00:00
|
|
|
startlastcmd = !startlastcmd
|
|
|
|
lastcmddone = !lastcmddone
|
2021-03-28 22:58:58 +00:00
|
|
|
|
2021-03-28 22:15:51 +00:00
|
|
|
continue
|
|
|
|
} else if !quoted && r == '^' && !lastcmddone {
|
2021-03-28 22:58:58 +00:00
|
|
|
// if ^ is found, isnt in quotes and is the
|
|
|
|
// first time of starting "^^"
|
2021-03-28 22:15:51 +00:00
|
|
|
startlastcmd = !startlastcmd
|
|
|
|
continue
|
2021-03-21 21:19:51 +00:00
|
|
|
} else {
|
|
|
|
sb.WriteRune(r)
|
|
|
|
}
|
2021-03-28 22:15:51 +00:00
|
|
|
cmdstr.WriteRune(r)
|
2021-03-21 21:19:51 +00:00
|
|
|
}
|
|
|
|
if sb.Len() > 0 {
|
|
|
|
cmdArgs = append(cmdArgs, sb.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
readline.AddHistory(input)
|
2021-03-28 22:15:51 +00:00
|
|
|
return cmdArgs, cmdstr.String()
|
2021-03-21 21:19:51 +00:00
|
|
|
}
|
|
|
|
|
2021-03-28 22:58:58 +00:00
|
|
|
// Run command in sh interpreter
|
2021-03-24 18:50:22 +00:00
|
|
|
func execCommand(cmd string) error {
|
|
|
|
file, err := syntax.NewParser().Parse(strings.NewReader(cmd), "")
|
|
|
|
if err != nil {
|
2021-03-24 23:18:30 +00:00
|
|
|
return err
|
2021-03-21 21:19:51 +00:00
|
|
|
}
|
2021-03-24 18:50:22 +00:00
|
|
|
runner, _ := interp.New(
|
|
|
|
interp.StdIO(os.Stdin, os.Stdout, os.Stderr),
|
|
|
|
)
|
2021-03-30 23:47:02 +00:00
|
|
|
err = runner.Run(context.TODO(), file)
|
2021-03-24 18:50:22 +00:00
|
|
|
|
2021-03-30 23:47:02 +00:00
|
|
|
return err
|
2021-03-21 21:19:51 +00:00
|
|
|
}
|
2021-03-24 18:50:22 +00:00
|
|
|
|
2021-03-28 22:58:58 +00:00
|
|
|
// do i even have to say
|
2021-03-19 19:11:59 +00:00
|
|
|
func HandleSignals() {
|
2021-03-20 04:01:24 +00:00
|
|
|
c := make(chan os.Signal)
|
|
|
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
|
|
|
go func() {
|
|
|
|
<-c
|
|
|
|
}()
|
2021-03-19 23:03:11 +00:00
|
|
|
}
|
|
|
|
|