Compare commits

...

7 Commits

Author SHA1 Message Date
sammy 8f94990fc3
feat: add exec function
this is basically the `exec` "command" in sh
itll replace hilbish with whatever provided
simple usage:
exec 'sleep 100'
2021-05-08 11:50:09 -04:00
sammy 1bacbfc778
fix: correct description for -n 2021-05-08 09:30:32 -04:00
sammy 8d5e36bcde
fix: reorder init code, making hilbish faster when used non-interactively 2021-05-08 09:30:14 -04:00
sammy 941902f43b
style: remove unneeded assign to _ 2021-05-08 09:12:21 -04:00
sammy cdd9dc1544
chore: merge from master 2021-05-08 08:57:16 -04:00
sammy e6382d454d
feat: add -n flag (closes #47) 2021-05-08 08:56:24 -04:00
Jim Tittsler 70a9e471eb
fix: typo in README (#46) 2021-05-03 06:20:52 -04:00
4 changed files with 48 additions and 14 deletions

View File

@ -60,11 +60,11 @@ sudo make install
sudo make all sudo make all
``` ```
Alternativly, if you use Arch Linux, you can compile Hilbish with an **(unofficial)** AUR package Alternatively, if you use Arch Linux, you can compile Hilbish with an **(unofficial)** AUR package:
```sh ```sh
yay -S hilbish yay -S hilbish
``` ```
If you want the latest and greatest, you can install and compile from latest git commit If you want the latest and greatest, you can install and compile from latest git commit:
```sh ```sh
yay -S hilbish-git yay -S hilbish-git
``` ```

19
lua.go
View File

@ -3,7 +3,9 @@ package main
import ( import (
"fmt" "fmt"
"os" "os"
"os/exec"
"strings" "strings"
"syscall"
"hilbish/golibs/bait" "hilbish/golibs/bait"
"hilbish/golibs/commander" "hilbish/golibs/commander"
@ -31,6 +33,7 @@ func LuaInit() {
l.SetGlobal("multiprompt", l.NewFunction(hshmlprompt)) l.SetGlobal("multiprompt", l.NewFunction(hshmlprompt))
l.SetGlobal("alias", l.NewFunction(hshalias)) l.SetGlobal("alias", l.NewFunction(hshalias))
l.SetGlobal("appendPath", l.NewFunction(hshappendPath)) l.SetGlobal("appendPath", l.NewFunction(hshappendPath))
l.SetGlobal("exec", l.NewFunction(hshexec))
// Add fs module to Lua // Add fs module to Lua
l.PreloadModule("fs", fs.Loader) l.PreloadModule("fs", fs.Loader)
@ -127,3 +130,19 @@ func hshappendPath(L *lua.LState) int {
return 0 return 0
} }
func hshexec(L *lua.LState) int {
cmd := L.CheckString(1)
cmdArgs, _ := splitInput(cmd)
cmdPath, err := exec.LookPath(cmdArgs[0])
if err != nil {
fmt.Println(err)
// if we get here, cmdPath will be nothing
// therefore nothing will run
}
// syscall.Exec requires an absolute path to a binary
// path, args, string slice of environments
syscall.Exec(cmdPath, cmdArgs, os.Environ())
return 0
}

25
main.go
View File

@ -35,6 +35,7 @@ var (
hooks bait.Bait hooks bait.Bait
interactive bool interactive bool
login bool // Are we the login shell? login bool // Are we the login shell?
noexecute bool // Should we run Lua or only report syntax errors
) )
func main() { func main() {
@ -47,14 +48,14 @@ func main() {
setshflag := getopt.BoolLong("setshellenv", 'S', "Sets $SHELL to Hilbish's executed path") setshflag := getopt.BoolLong("setshellenv", 'S', "Sets $SHELL to Hilbish's executed path")
cmdflag := getopt.StringLong("command", 'c', "", /*TODO: Help description*/ "") cmdflag := getopt.StringLong("command", 'c', "", /*TODO: Help description*/ "")
configflag := getopt.StringLong("config", 'C', defaultconfpath, "Sets the path to Hilbish's config") configflag := getopt.StringLong("config", 'C', defaultconfpath, "Sets the path to Hilbish's config")
// loginshflag getopt.BoolLong("login", 'l', "Makes Hilbish act like a login shell")
// TODO: issue #37 getopt.BoolLong("interactive", 'i', "Force Hilbish to be an interactive shell")
_ = getopt.BoolLong("login", 'l', "Makes Hilbish act like a login shell") getopt.BoolLong("noexec", 'n', "Don't execute and only report Lua syntax errors")
_ = getopt.BoolLong("interactive", 'i', "Force Hilbish to be an interactive shell")
getopt.Parse() getopt.Parse()
loginshflag := getopt.Lookup('l').Seen() loginshflag := getopt.Lookup('l').Seen()
interactiveflag := getopt.Lookup('i').Seen() interactiveflag := getopt.Lookup('i').Seen()
noexecflag := getopt.Lookup('n').Seen()
if *cmdflag == "" || interactiveflag { if *cmdflag == "" || interactiveflag {
interactive = true interactive = true
@ -64,6 +65,10 @@ func main() {
interactive = false interactive = false
} }
if noexecflag {
noexecute = true
}
// first arg, first character // first arg, first character
if loginshflag || os.Args[0][0] == '-' { if loginshflag || os.Args[0][0] == '-' {
login = true login = true
@ -103,15 +108,12 @@ func main() {
} }
} }
go HandleSignals()
LuaInit() LuaInit()
RunLogin() RunLogin()
RunInput(*cmdflag)
RunConfig(*configflag) RunConfig(*configflag)
readline.Completer = readline.FilenameCompleter
readline.LoadHistory(homedir + "/.hilbish-history")
RunInput(*cmdflag)
if getopt.NArgs() > 0 { if getopt.NArgs() > 0 {
err := l.DoFile(getopt.Arg(0)) err := l.DoFile(getopt.Arg(0))
if err != nil { if err != nil {
@ -121,6 +123,11 @@ func main() {
os.Exit(0) os.Exit(0)
} }
readline.Completer = readline.FilenameCompleter
readline.LoadHistory(homedir + "/.hilbish-history")
go HandleSignals()
for interactive { for interactive {
running = false running = false

View File

@ -16,9 +16,17 @@ import (
) )
func RunInput(input string) { func RunInput(input string) {
// First try to run user input in Lua // First try to load input, essentially compiling to bytecode
err := l.DoString(input) fn, err := l.LoadString(input)
if err != nil && noexecute {
fmt.Println(err)
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)
}
if err == nil { if err == nil {
// If it succeeds, add to history and prompt again // If it succeeds, add to history and prompt again
HandleHistory(input) HandleHistory(input)