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-24 18:50:22 +00:00
|
|
|
_ "os/exec"
|
2021-03-19 23:03:11 +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-20 22:51:05 +00:00
|
|
|
lfs "hilbish/golibs/fs"
|
|
|
|
cmds "hilbish/golibs/commander"
|
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-25 00:22:25 +00:00
|
|
|
const version = "0.1.2"
|
2021-03-19 23:03:11 +00:00
|
|
|
var l *lua.LState
|
|
|
|
var prompt string
|
2021-03-20 22:51:05 +00:00
|
|
|
var commands = map[string]bool{}
|
2021-03-21 21:19:51 +00:00
|
|
|
var aliases = map[string]string{}
|
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-20 07:36:34 +00:00
|
|
|
Help: "prints hilbish version",
|
2021-03-20 04:01:24 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
err := parser.Parse(os.Args)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Print(parser.Usage(err))
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
if *verflag {
|
|
|
|
fmt.Printf("Hilbish v%s\n", version)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Setenv("SHELL", os.Args[0])
|
2021-03-21 07:51:44 +00:00
|
|
|
|
2021-03-21 07:58:04 +00:00
|
|
|
input, err := os.ReadFile(".hilbishrc.lua")
|
2021-03-21 07:51:44 +00:00
|
|
|
if err != nil {
|
2021-03-21 07:58:04 +00:00
|
|
|
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-21 07:51:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
homedir, _ := os.UserHomeDir()
|
2021-03-25 16:07:51 +00:00
|
|
|
if _, err := os.Stat(homedir + "/.hilbishrc.lua"); os.IsNotExist(err) {
|
|
|
|
err = os.WriteFile(homedir + "/.hilbishrc.lua", input, 0644)
|
|
|
|
if err != nil {
|
|
|
|
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-19 19:11:59 +00:00
|
|
|
for {
|
2021-03-19 23:03:11 +00:00
|
|
|
//user, _ := user.Current()
|
|
|
|
//dir, _ := os.Getwd()
|
|
|
|
//host, _ := os.Hostname()
|
2021-03-19 19:11:59 +00:00
|
|
|
|
2021-03-20 04:01:24 +00:00
|
|
|
//reader := bufio.NewReader(os.Stdin)
|
2021-03-19 19:11:59 +00:00
|
|
|
|
2021-03-20 04:01:24 +00:00
|
|
|
//fmt.Printf(prompt)
|
2021-03-19 19:11:59 +00:00
|
|
|
|
2021-03-20 04:01:24 +00:00
|
|
|
cmdString, err := readline.String(prompt)
|
2021-03-20 16:57:18 +00:00
|
|
|
if err == io.EOF {
|
|
|
|
fmt.Println("")
|
|
|
|
break
|
|
|
|
}
|
2021-03-19 19:11:59 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
}
|
|
|
|
cmdString = strings.TrimSuffix(cmdString, "\n")
|
2021-03-19 23:03:11 +00:00
|
|
|
err = l.DoString(cmdString)
|
|
|
|
|
2021-03-25 00:21:55 +00:00
|
|
|
if err == nil {
|
|
|
|
readline.AddHistory(cmdString)
|
|
|
|
continue
|
|
|
|
}
|
2021-03-19 23:03:11 +00:00
|
|
|
|
2021-03-21 21:19:51 +00:00
|
|
|
cmdArgs := splitInput(cmdString)
|
2021-03-19 19:11:59 +00:00
|
|
|
if len(cmdArgs) == 0 { continue }
|
|
|
|
|
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
|
|
|
//cmdArgs := splitInput(cmdString)
|
|
|
|
execCommand(cmdString)
|
2021-03-21 21:19:51 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
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-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-24 23:18:30 +00:00
|
|
|
if syntax.IsIncomplete(err) {
|
|
|
|
sb := &strings.Builder{}
|
|
|
|
for {
|
|
|
|
done := StartMultiline(cmdString, sb)
|
|
|
|
if done {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
}
|
2021-03-20 05:22:43 +00:00
|
|
|
}
|
2021-03-19 19:11:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-24 23:18:30 +00:00
|
|
|
func StartMultiline(prev string, sb *strings.Builder) bool {
|
|
|
|
if sb.String() == "" { sb.WriteString(prev + "\n") }
|
|
|
|
|
|
|
|
fmt.Printf("... ")
|
|
|
|
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
|
|
|
|
cont, err := reader.ReadString('\n')
|
|
|
|
if err == io.EOF {
|
|
|
|
fmt.Println("")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
sb.WriteString(cont)
|
|
|
|
|
|
|
|
err = execCommand(sb.String())
|
|
|
|
if err != nil && syntax.IsIncomplete(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-03-21 21:19:51 +00:00
|
|
|
func splitInput(input string) []string {
|
|
|
|
quoted := false
|
|
|
|
cmdArgs := []string{}
|
|
|
|
sb := &strings.Builder{}
|
|
|
|
|
|
|
|
for _, r := range input {
|
|
|
|
if r == '"' {
|
|
|
|
quoted = !quoted
|
|
|
|
// dont add back quotes
|
|
|
|
//sb.WriteRune(r)
|
|
|
|
} else if !quoted && r == '~' {
|
|
|
|
sb.WriteString(os.Getenv("HOME"))
|
|
|
|
} else if !quoted && r == ' ' {
|
|
|
|
cmdArgs = append(cmdArgs, sb.String())
|
|
|
|
sb.Reset()
|
|
|
|
} else {
|
|
|
|
sb.WriteRune(r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if sb.Len() > 0 {
|
|
|
|
cmdArgs = append(cmdArgs, sb.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
readline.AddHistory(input)
|
|
|
|
return cmdArgs
|
|
|
|
}
|
|
|
|
|
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),
|
|
|
|
)
|
|
|
|
runner.Run(context.TODO(), file)
|
|
|
|
|
2021-03-21 21:19:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-03-24 18:50:22 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func LuaInit() {
|
|
|
|
l = lua.NewState()
|
|
|
|
|
|
|
|
l.OpenLibs()
|
|
|
|
|
|
|
|
l.SetGlobal("prompt", l.NewFunction(hshprompt))
|
2021-03-21 21:19:51 +00:00
|
|
|
l.SetGlobal("alias", l.NewFunction(hshalias))
|
2021-03-19 23:03:11 +00:00
|
|
|
|
2021-03-20 22:51:05 +00:00
|
|
|
l.PreloadModule("fs", lfs.Loader)
|
|
|
|
|
|
|
|
commander := cmds.New()
|
|
|
|
commander.Events.On("commandRegister",
|
|
|
|
func (cmdName string, cmd *lua.LFunction) {
|
|
|
|
commands[cmdName] = true
|
|
|
|
l.SetField(
|
2021-03-21 16:42:04 +00:00
|
|
|
l.GetTable(l.GetGlobal("commanding"),
|
2021-03-20 22:51:05 +00:00
|
|
|
lua.LString("__commands")),
|
|
|
|
cmdName,
|
|
|
|
cmd)
|
|
|
|
})
|
|
|
|
|
|
|
|
l.PreloadModule("commander", commander.Loader)
|
|
|
|
|
2021-03-21 16:42:04 +00:00
|
|
|
l.DoString("package.path = package.path .. ';./libs/?/init.lua;/usr/share/hilbish/libs/?/init.lua'")
|
|
|
|
|
|
|
|
err := l.DoFile("/usr/share/hilbish/preload.lua")
|
|
|
|
if err != nil {
|
2021-03-21 18:47:27 +00:00
|
|
|
err = l.DoFile("preload.lua")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr,
|
|
|
|
"Missing preload file, builtins may be missing.")
|
|
|
|
}
|
2021-03-21 16:42:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
homedir, _ := os.UserHomeDir()
|
|
|
|
err = l.DoFile(homedir + "/.hilbishrc.lua")
|
2021-03-19 23:03:11 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-03-19 19:11:59 +00:00
|
|
|
}
|