2
2
mirror of https://github.com/Hilbis/Hilbish synced 2025-04-21 21:13:22 +00:00

Compare commits

...

13 Commits

Author SHA1 Message Date
sammy
f9c1eced68
chore: bump version 2021-05-11 19:39:51 -04:00
sammy
fd478e883f
feat: args variable (resolves #49)
anytime hilbish is ran as shebang or given a script to run,
itll provide an `args` variable which will be the args a user
provides to the script.
it includes the execute path, as most argv's do
2021-05-11 19:37:00 -04:00
sammy
0f9d3732fb
fix: check if type assertion of return value is valid 2021-05-11 19:00:07 -04:00
sammy
accd4beeba
fix(cd): return err code of fs.cd instead of 1 specifically 2021-05-11 18:57:54 -04:00
sammy
a140db1610
fix(cd): make it return 1 exit code on err 2021-05-11 18:55:22 -04:00
sammy
630d7dde9c
fix: prefer to run preload in cwd first 2021-05-11 18:55:05 -04:00
sammy
0ddfc5bea0
feat: allow command defined by commander to return exit code 2021-05-11 18:53:24 -04:00
sammy
cc995e0f34
chore: bump version 2021-05-11 18:52:49 -04:00
sammy
05f5bbcfdd
fix: add -c description 2021-05-11 18:19:53 -04:00
sammy
0acae37704
chore: revert "fix: reordering init code [...]"
this reverts commit 8d5e36bcdebdbb8c00dfb02cd3175ce89dbd3a7d.
apparently, when i reordered the code it caused hilbish's history
to be emptied and not work when used interactively
i literally have no idea why
2021-05-11 18:12:58 -04:00
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
4 changed files with 38 additions and 8 deletions

23
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)
@ -62,9 +65,9 @@ func LuaInit() {
.. os.getenv 'HOME' .. '/.local/share/hilbish/libs/?.lua' .. os.getenv 'HOME' .. '/.local/share/hilbish/libs/?.lua'
`) `)
err := l.DoFile("/usr/share/hilbish/preload.lua") err := l.DoFile("preload.lua")
if err != nil { if err != nil {
err = l.DoFile("preload.lua") err = l.DoFile("/usr/share/hilbish/preload.lua")
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, fmt.Fprintln(os.Stderr,
"Missing preload file, builtins may be missing.") "Missing preload file, builtins may be missing.")
@ -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
}

View File

@ -14,10 +14,11 @@ import (
"github.com/pborman/getopt" "github.com/pborman/getopt"
"github.com/bobappleyard/readline" "github.com/bobappleyard/readline"
"github.com/yuin/gopher-lua" "github.com/yuin/gopher-lua"
"layeh.com/gopher-luar"
"golang.org/x/term" "golang.org/x/term"
) )
const version = "0.4.0" const version = "0.4.1-dev.8"
var ( var (
l *lua.LState l *lua.LState
@ -46,11 +47,11 @@ func main() {
// parser := argparse.NewParser("hilbish", "A shell for lua and flower lovers") // parser := argparse.NewParser("hilbish", "A shell for lua and flower lovers")
verflag := getopt.BoolLong("version", 'v', "Prints Hilbish version") verflag := getopt.BoolLong("version", 'v', "Prints Hilbish version")
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', "", "Executes a command on startup")
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")
getopt.BoolLong("login", 'l', "Makes Hilbish act like a login shell") getopt.BoolLong("login", 'l', "Makes Hilbish act like a login shell")
getopt.BoolLong("interactive", 'i', "Force Hilbish to be an interactive shell") getopt.BoolLong("interactive", 'i', "Force Hilbish to be an interactive shell")
getopt.BoolLong("noexec", 'n', "Force Hilbish to be an interactive shell") getopt.BoolLong("noexec", 'n', "Don't execute and only report Lua syntax errors")
getopt.Parse() getopt.Parse()
loginshflag := getopt.Lookup('l').Seen() loginshflag := getopt.Lookup('l').Seen()
@ -118,6 +119,7 @@ func main() {
RunInput(*cmdflag) RunInput(*cmdflag)
if getopt.NArgs() > 0 { if getopt.NArgs() > 0 {
l.SetGlobal("args", luar.New(l, getopt.Args()))
err := l.DoFile(getopt.Arg(0)) err := l.DoFile(getopt.Arg(0))
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)

View File

@ -20,8 +20,8 @@ commander.register('cd', function (args)
if err == 1 then if err == 1 then
print('directory does not exist') print('directory does not exist')
end end
bait.throw('command.exit', err) return err
else bait.throw('command.exit', 0) end end
return return
end end
fs.cd(os.getenv 'HOME') fs.cd(os.getenv 'HOME')

View File

@ -52,9 +52,17 @@ func RunInput(input string) {
l.GetGlobal("commanding"), l.GetGlobal("commanding"),
lua.LString("__commands")), lua.LString("__commands")),
cmdArgs[0]), cmdArgs[0]),
NRet: 0, NRet: 1,
Protect: true, Protect: true,
}, luar.New(l, cmdArgs[1:])) }, luar.New(l, cmdArgs[1:]))
luaexitcode := l.Get(-1)
exitcode := lua.LNumber(0)
l.Pop(1)
if code, ok := luaexitcode.(lua.LNumber); luaexitcode != lua.LNil && ok {
exitcode = code
}
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, fmt.Fprintln(os.Stderr,
"Error in command:\n\n" + err.Error()) "Error in command:\n\n" + err.Error())
@ -62,6 +70,7 @@ func RunInput(input string) {
if cmdArgs[0] != "exit" { if cmdArgs[0] != "exit" {
HandleHistory(cmdString) HandleHistory(cmdString)
} }
hooks.Em.Emit("command.exit", exitcode)
return return
} }