2021-03-19 19:11:59 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-06-02 02:13:33 +00:00
|
|
|
"bufio"
|
2021-03-19 19:11:59 +00:00
|
|
|
"fmt"
|
2021-04-19 02:09:27 +00:00
|
|
|
"io"
|
2021-03-19 19:11:59 +00:00
|
|
|
"os"
|
2021-04-19 02:09:27 +00:00
|
|
|
"os/user"
|
2021-05-01 18:51:57 +00:00
|
|
|
"path/filepath"
|
2021-11-22 15:41:27 +00:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
2021-03-20 04:01:24 +00:00
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
"hilbish/util"
|
2021-04-28 22:57:28 +00:00
|
|
|
"hilbish/golibs/bait"
|
2021-03-20 04:01:24 +00:00
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
rt "github.com/arnodel/golua/runtime"
|
2021-05-01 05:08:55 +00:00
|
|
|
"github.com/pborman/getopt"
|
2022-02-28 22:32:57 +00:00
|
|
|
"github.com/maxlandon/readline"
|
2021-04-07 01:12:19 +00:00
|
|
|
"golang.org/x/term"
|
2021-03-19 19:11:59 +00:00
|
|
|
)
|
|
|
|
|
2021-05-15 13:04:27 +00:00
|
|
|
var (
|
2022-04-04 10:40:02 +00:00
|
|
|
l *rt.Runtime
|
2021-12-06 21:21:31 +00:00
|
|
|
lr *lineReader
|
2021-04-19 02:09:27 +00:00
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
commands = map[string]*rt.Closure{}
|
|
|
|
luaCompletions = map[string]*rt.Closure{}
|
2021-04-19 02:09:27 +00:00
|
|
|
|
2021-10-08 00:58:07 +00:00
|
|
|
confDir string
|
2022-02-24 02:19:54 +00:00
|
|
|
userDataDir string
|
2021-05-01 18:51:57 +00:00
|
|
|
curuser *user.User
|
|
|
|
|
|
|
|
hooks bait.Bait
|
2021-06-10 00:30:53 +00:00
|
|
|
defaultConfPath string
|
2021-10-13 14:42:35 +00:00
|
|
|
defaultHistPath string
|
2021-04-20 21:51:12 +00:00
|
|
|
)
|
2021-03-19 23:03:11 +00:00
|
|
|
|
2021-03-19 19:11:59 +00:00
|
|
|
func main() {
|
2021-05-01 18:51:57 +00:00
|
|
|
curuser, _ = user.Current()
|
2021-12-01 22:31:04 +00:00
|
|
|
homedir := curuser.HomeDir
|
2022-02-24 02:01:35 +00:00
|
|
|
confDir, _ = os.UserConfigDir()
|
2021-07-08 10:44:11 +00:00
|
|
|
preloadPath = strings.Replace(preloadPath, "~", homedir, 1)
|
|
|
|
sampleConfPath = strings.Replace(sampleConfPath, "~", homedir, 1)
|
2022-03-05 19:46:38 +00:00
|
|
|
|
2022-02-24 02:19:54 +00:00
|
|
|
// i honestly dont know what directories to use for this
|
|
|
|
switch runtime.GOOS {
|
2022-04-20 02:12:05 +00:00
|
|
|
case "linux", "darwin":
|
2022-02-24 02:19:54 +00:00
|
|
|
userDataDir = getenv("XDG_DATA_HOME", curuser.HomeDir + "/.local/share")
|
|
|
|
default:
|
|
|
|
// this is fine on windows, dont know about others
|
|
|
|
userDataDir = confDir
|
|
|
|
}
|
2021-06-10 00:30:53 +00:00
|
|
|
|
|
|
|
if defaultConfDir == "" {
|
|
|
|
// we'll add *our* default if its empty (wont be if its changed comptime)
|
2022-03-13 01:50:57 +00:00
|
|
|
defaultConfDir = filepath.Join(confDir, "hilbish")
|
2021-06-10 00:30:53 +00:00
|
|
|
} else {
|
|
|
|
// else do ~ substitution
|
2022-05-01 04:49:59 +00:00
|
|
|
defaultConfDir = filepath.Join(util.ExpandHome(defaultConfDir), "hilbish")
|
2021-06-10 00:30:53 +00:00
|
|
|
}
|
2022-03-13 01:50:57 +00:00
|
|
|
defaultConfPath = filepath.Join(defaultConfDir, "init.lua")
|
2021-10-13 14:42:35 +00:00
|
|
|
if defaultHistDir == "" {
|
2022-03-13 01:50:57 +00:00
|
|
|
defaultHistDir = filepath.Join(userDataDir, "hilbish")
|
2021-10-13 14:42:35 +00:00
|
|
|
} else {
|
2022-05-01 04:49:59 +00:00
|
|
|
defaultHistDir = filepath.Join(util.ExpandHome(defaultHistDir), "hilbish")
|
2021-10-13 14:42:35 +00:00
|
|
|
}
|
2022-03-13 01:50:57 +00:00
|
|
|
defaultHistPath = filepath.Join(defaultHistDir, ".hilbish-history")
|
2021-06-12 14:48:57 +00:00
|
|
|
helpflag := getopt.BoolLong("help", 'h', "Prints Hilbish flags")
|
2021-05-01 05:08:55 +00:00
|
|
|
verflag := getopt.BoolLong("version", 'v', "Prints Hilbish version")
|
|
|
|
setshflag := getopt.BoolLong("setshellenv", 'S', "Sets $SHELL to Hilbish's executed path")
|
2021-05-11 22:19:53 +00:00
|
|
|
cmdflag := getopt.StringLong("command", 'c', "", "Executes a command on startup")
|
2021-06-10 00:30:53 +00:00
|
|
|
configflag := getopt.StringLong("config", 'C', defaultConfPath, "Sets the path to Hilbish's config")
|
2021-06-12 14:49:22 +00:00
|
|
|
getopt.BoolLong("login", 'l', "Force Hilbish to be a login shell")
|
2021-05-08 13:12:21 +00:00
|
|
|
getopt.BoolLong("interactive", 'i', "Force Hilbish to be an interactive shell")
|
2021-05-08 13:30:32 +00:00
|
|
|
getopt.BoolLong("noexec", 'n', "Don't execute and only report Lua syntax errors")
|
2021-03-20 04:01:24 +00:00
|
|
|
|
2021-05-01 05:08:55 +00:00
|
|
|
getopt.Parse()
|
2021-05-01 18:08:42 +00:00
|
|
|
loginshflag := getopt.Lookup('l').Seen()
|
2021-05-01 05:50:22 +00:00
|
|
|
interactiveflag := getopt.Lookup('i').Seen()
|
2021-05-08 12:56:24 +00:00
|
|
|
noexecflag := getopt.Lookup('n').Seen()
|
2021-05-01 05:08:55 +00:00
|
|
|
|
2021-06-12 14:48:57 +00:00
|
|
|
if *helpflag {
|
|
|
|
getopt.PrintUsage(os.Stdout)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2021-05-01 05:50:22 +00:00
|
|
|
if *cmdflag == "" || interactiveflag {
|
2021-04-24 04:12:18 +00:00
|
|
|
interactive = true
|
|
|
|
}
|
|
|
|
|
2021-06-02 02:13:33 +00:00
|
|
|
if fileInfo, _ := os.Stdin.Stat(); (fileInfo.Mode() & os.ModeCharDevice) == 0 {
|
|
|
|
interactive = false
|
|
|
|
}
|
|
|
|
|
2021-05-01 14:44:15 +00:00
|
|
|
if getopt.NArgs() > 0 {
|
|
|
|
interactive = false
|
|
|
|
}
|
|
|
|
|
2021-05-08 12:56:24 +00:00
|
|
|
if noexecflag {
|
|
|
|
noexecute = true
|
|
|
|
}
|
|
|
|
|
2021-05-01 18:08:42 +00:00
|
|
|
// first arg, first character
|
2021-05-01 18:17:14 +00:00
|
|
|
if loginshflag || os.Args[0][0] == '-' {
|
2021-05-01 18:08:42 +00:00
|
|
|
login = true
|
|
|
|
}
|
|
|
|
|
2021-03-20 04:01:24 +00:00
|
|
|
if *verflag {
|
2021-05-14 01:02:56 +00:00
|
|
|
fmt.Printf("Hilbish %s\n", version)
|
2021-03-20 04:01:24 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2021-03-28 22:58:58 +00:00
|
|
|
// Set $SHELL if the user wants to
|
2021-04-19 02:09:27 +00:00
|
|
|
if *setshflag {
|
|
|
|
os.Setenv("SHELL", os.Args[0])
|
|
|
|
}
|
2021-03-21 07:51:44 +00:00
|
|
|
|
2022-02-24 02:20:11 +00:00
|
|
|
go handleSignals()
|
|
|
|
luaInit()
|
2022-03-06 18:38:27 +00:00
|
|
|
lr = newLineReader("", false)
|
2021-03-28 22:58:58 +00:00
|
|
|
// If user's config doesn't exixt,
|
2022-02-24 02:20:11 +00:00
|
|
|
if _, err := os.Stat(defaultConfPath); os.IsNotExist(err) && *configflag == defaultConfPath {
|
2021-03-30 23:47:02 +00:00
|
|
|
// Read default from current directory
|
|
|
|
// (this is assuming the current dir is Hilbish's git)
|
2022-02-24 02:20:11 +00:00
|
|
|
_, err := os.ReadFile(".hilbishrc.lua")
|
|
|
|
confpath := ".hilbishrc.lua"
|
2021-03-30 23:47:02 +00:00
|
|
|
if err != nil {
|
2021-06-10 00:30:53 +00:00
|
|
|
// If it wasnt found, go to the real sample conf
|
2022-02-24 02:20:11 +00:00
|
|
|
_, err = os.ReadFile(sampleConfPath)
|
|
|
|
confpath = sampleConfPath
|
2021-03-30 23:47:02 +00:00
|
|
|
if err != nil {
|
2021-06-10 00:33:30 +00:00
|
|
|
fmt.Println("could not find .hilbishrc.lua or", sampleConfPath)
|
2021-03-30 23:47:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-24 02:20:11 +00:00
|
|
|
runConfig(confpath)
|
|
|
|
} else {
|
|
|
|
runConfig(*configflag)
|
2021-03-25 16:07:51 +00:00
|
|
|
}
|
2021-03-21 07:51:44 +00:00
|
|
|
|
2021-06-02 02:13:33 +00:00
|
|
|
if fileInfo, _ := os.Stdin.Stat(); (fileInfo.Mode() & os.ModeCharDevice) == 0 {
|
|
|
|
scanner := bufio.NewScanner(bufio.NewReader(os.Stdin))
|
|
|
|
for scanner.Scan() {
|
2022-02-27 23:12:58 +00:00
|
|
|
text := scanner.Text()
|
2022-03-19 22:48:03 +00:00
|
|
|
runInput(text, true)
|
2021-06-02 02:13:33 +00:00
|
|
|
}
|
2022-04-12 23:28:25 +00:00
|
|
|
exit(0)
|
2021-06-02 02:13:33 +00:00
|
|
|
}
|
|
|
|
|
2021-05-16 12:42:20 +00:00
|
|
|
if *cmdflag != "" {
|
2022-03-19 22:48:03 +00:00
|
|
|
runInput(*cmdflag, true)
|
2021-05-16 12:42:20 +00:00
|
|
|
}
|
|
|
|
|
2021-05-01 14:44:15 +00:00
|
|
|
if getopt.NArgs() > 0 {
|
2022-04-04 10:40:02 +00:00
|
|
|
luaArgs := rt.NewTable()
|
|
|
|
for i, arg := range getopt.Args() {
|
2022-04-14 00:19:28 +00:00
|
|
|
luaArgs.Set(rt.IntValue(int64(i)), rt.StringValue(arg))
|
2021-12-06 20:32:34 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
l.GlobalEnv().Set(rt.StringValue("args"), rt.TableValue(luaArgs))
|
|
|
|
err := util.DoFile(l, getopt.Arg(0))
|
2021-05-01 05:08:55 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
2022-04-12 23:28:25 +00:00
|
|
|
exit(1)
|
2021-05-01 05:08:55 +00:00
|
|
|
}
|
2022-04-12 23:28:25 +00:00
|
|
|
exit(0)
|
2021-05-01 05:08:55 +00:00
|
|
|
}
|
|
|
|
|
2022-03-06 21:20:41 +00:00
|
|
|
initialized = true
|
2021-06-11 22:26:34 +00:00
|
|
|
input:
|
2021-04-24 04:12:18 +00:00
|
|
|
for interactive {
|
2021-04-14 17:20:03 +00:00
|
|
|
running = false
|
2021-04-16 14:27:11 +00:00
|
|
|
|
2021-05-17 23:03:56 +00:00
|
|
|
input, err := lr.Read()
|
2021-04-16 14:27:11 +00:00
|
|
|
|
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)
|
2022-03-06 14:23:32 +00:00
|
|
|
hooks.Em.Emit("hilbish.exit")
|
2021-03-20 16:57:18 +00:00
|
|
|
break
|
|
|
|
}
|
2021-03-19 19:11:59 +00:00
|
|
|
if err != nil {
|
2022-02-28 22:32:57 +00:00
|
|
|
if err != readline.CtrlC {
|
|
|
|
// If we get a completely random error, print
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
}
|
|
|
|
fmt.Println("^C")
|
|
|
|
continue
|
2021-03-19 19:11:59 +00:00
|
|
|
}
|
2022-03-19 22:48:03 +00:00
|
|
|
var priv bool
|
|
|
|
if strings.HasPrefix(input, " ") {
|
|
|
|
priv = true
|
|
|
|
}
|
2021-03-28 22:58:58 +00:00
|
|
|
|
2021-04-05 00:30:03 +00:00
|
|
|
input = strings.TrimSpace(input)
|
2021-10-14 02:51:38 +00:00
|
|
|
if len(input) == 0 {
|
2022-03-08 01:01:21 +00:00
|
|
|
running = true
|
2021-10-14 02:51:38 +00:00
|
|
|
hooks.Em.Emit("command.exit", 0)
|
|
|
|
continue
|
|
|
|
}
|
2021-04-05 00:30:03 +00:00
|
|
|
|
2021-04-04 22:42:56 +00:00
|
|
|
if strings.HasSuffix(input, "\\") {
|
|
|
|
for {
|
2022-02-27 23:12:58 +00:00
|
|
|
input, err = continuePrompt(input)
|
2021-06-11 22:26:34 +00:00
|
|
|
if err != nil {
|
2022-03-17 23:49:33 +00:00
|
|
|
running = true
|
|
|
|
lr.SetPrompt(fmtPrompt(prompt))
|
2021-06-11 22:26:34 +00:00
|
|
|
goto input // continue inside nested loop
|
|
|
|
}
|
|
|
|
if !strings.HasSuffix(input, "\\") {
|
2021-04-19 02:09:27 +00:00
|
|
|
break
|
|
|
|
}
|
2021-04-04 22:42:56 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-27 20:28:29 +00:00
|
|
|
|
2022-03-19 22:48:03 +00:00
|
|
|
runInput(input, priv)
|
2021-04-07 01:12:19 +00:00
|
|
|
|
|
|
|
termwidth, _, err := term.GetSize(0)
|
2021-04-19 02:09:27 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2021-04-24 03:44:59 +00:00
|
|
|
fmt.Printf("\u001b[7m∆\u001b[0m" + strings.Repeat(" ", termwidth - 1) + "\r")
|
2021-04-04 22:42:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:21:31 +00:00
|
|
|
func continuePrompt(prev string) (string, error) {
|
2021-05-01 18:25:35 +00:00
|
|
|
hooks.Em.Emit("multiline", nil)
|
2021-05-17 23:03:56 +00:00
|
|
|
lr.SetPrompt(multilinePrompt)
|
|
|
|
cont, err := lr.Read()
|
2021-04-04 22:42:56 +00:00
|
|
|
if err != nil {
|
2021-06-11 22:25:24 +00:00
|
|
|
return "", err
|
2021-03-19 19:11:59 +00:00
|
|
|
}
|
2021-06-11 22:08:31 +00:00
|
|
|
cont = strings.TrimSpace(cont)
|
2021-06-11 22:25:24 +00:00
|
|
|
|
2021-05-16 02:25:29 +00:00
|
|
|
return prev + strings.TrimSuffix(cont, "\n"), nil
|
2021-03-19 19:11:59 +00:00
|
|
|
}
|
|
|
|
|
2021-03-28 22:58:58 +00:00
|
|
|
// This semi cursed function formats our prompt (obviously)
|
2022-03-05 01:54:47 +00:00
|
|
|
func fmtPrompt(prompt string) string {
|
2021-03-28 21:11:57 +00:00
|
|
|
host, _ := os.Hostname()
|
|
|
|
cwd, _ := os.Getwd()
|
2022-04-20 01:23:48 +00:00
|
|
|
|
2022-04-20 02:05:48 +00:00
|
|
|
if strings.HasPrefix(cwd, curuser.HomeDir) {
|
|
|
|
cwd = "~" + strings.TrimPrefix(cwd, curuser.HomeDir)
|
|
|
|
}
|
|
|
|
username := curuser.Username
|
2021-09-26 01:39:06 +00:00
|
|
|
// this will be baked into binary since GOOS is a constant
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
username = strings.Split(username, "\\")[1] // for some reason Username includes the hostname on windows
|
|
|
|
}
|
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,
|
2021-05-01 18:51:57 +00:00
|
|
|
"D", filepath.Base(cwd),
|
2021-03-28 22:58:58 +00:00
|
|
|
"h", host,
|
2021-07-08 10:44:57 +00:00
|
|
|
"u", username,
|
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-12-06 21:21:31 +00:00
|
|
|
func handleHistory(cmd string) {
|
2021-05-17 23:03:56 +00:00
|
|
|
lr.AddHistory(cmd)
|
2021-05-17 00:09:47 +00:00
|
|
|
// TODO: load history again (history shared between sessions like this ye)
|
|
|
|
}
|
|
|
|
|
2022-03-07 22:56:22 +00:00
|
|
|
func removeDupes(slice []string) []string {
|
|
|
|
all := make(map[string]bool)
|
|
|
|
newSlice := []string{}
|
|
|
|
for _, item := range slice {
|
|
|
|
if _, val := all[item]; !val {
|
|
|
|
all[item] = true
|
|
|
|
newSlice = append(newSlice, item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newSlice
|
|
|
|
}
|
2022-03-18 00:22:30 +00:00
|
|
|
|
|
|
|
func contains(s []string, e string) bool {
|
|
|
|
for _, a := range s {
|
|
|
|
if a == e {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2022-04-12 23:28:25 +00:00
|
|
|
|
|
|
|
func exit(code int) {
|
|
|
|
// wait for all timers to finish before exiting
|
|
|
|
for {
|
|
|
|
if timers.running == 0 {
|
|
|
|
os.Exit(code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|