2021-03-19 19:11:59 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-03-20 04:01:24 +00:00
|
|
|
_ "bufio"
|
2021-03-19 19:11:59 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"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 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-19 19:11:59 +00:00
|
|
|
)
|
|
|
|
|
2021-03-20 04:01:24 +00:00
|
|
|
const version = "0.0.4"
|
2021-03-19 23:03:11 +00:00
|
|
|
var l *lua.LState
|
|
|
|
var prompt string
|
|
|
|
|
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,
|
|
|
|
Help: "color palette to use",
|
|
|
|
})
|
|
|
|
|
|
|
|
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-19 19:11:59 +00:00
|
|
|
HandleSignals()
|
2021-03-19 23:03:11 +00:00
|
|
|
LuaInit()
|
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-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)
|
|
|
|
|
|
|
|
if err == nil { continue }
|
|
|
|
|
2021-03-19 19:11:59 +00:00
|
|
|
cmdArgs := strings.Fields(cmdString)
|
|
|
|
|
|
|
|
if len(cmdArgs) == 0 { continue }
|
|
|
|
|
|
|
|
switch cmdArgs[0] {
|
|
|
|
case "exit":
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
}
|
2021-03-20 04:01:24 +00:00
|
|
|
readline.AddHistory(cmdString)
|
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))
|
|
|
|
|
|
|
|
err := l.DoFile(os.Getenv("HOME") + "/.hilbishrc.lua")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-03-19 19:11:59 +00:00
|
|
|
}
|