refactor: implement fg in lua

fg-job
TorchedSammy 2022-05-24 18:07:31 -04:00
parent 4c8cb686a0
commit 43dc00c120
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
3 changed files with 16 additions and 20 deletions

20
exec.go
View File

@ -17,7 +17,6 @@ import (
"hilbish/util"
rt "github.com/arnodel/golua/runtime"
"golang.org/x/sys/unix"
"mvdan.cc/sh/v3/shell"
//"github.com/yuin/gopher-lua/parse"
"mvdan.cc/sh/v3/interp"
@ -274,25 +273,6 @@ func execHandle(bg bool) interp.ExecHandlerFunc {
}
}
// sh interp has "fg" as unimplemented builtin, which panics
if args[0] == "fg" {
j := jobs.getLatest()
if j == nil || j.pid == 0 {
return interp.NewExitStatus(1)
}
pgid, _ := syscall.Getpgid(j.pid)
hshPgid, _ := syscall.Getpgid(os.Getpid())
// tcsetpgrp
unix.IoctlSetPointerInt(0, unix.TIOCSPGRP, pgid)
proc, _ := os.FindProcess(j.pid)
proc.Wait()
unix.IoctlSetPointerInt(0, unix.TIOCSPGRP, hshPgid)
return interp.NewExitStatus(0)
}
// If command is defined in Lua then run it
luacmdArgs := rt.NewTable()
for i, str := range args[1:] {

View File

@ -0,0 +1,15 @@
local commander = require 'commander'
commander.register('fg', function()
local job = hilbish.jobs.last()
if not job then
print 'fg: no last job'
return 1
end
local err = job.foreground() -- waits for job; blocks
if err then
print('fg: ' .. err)
return 2
end
end)

View File

@ -5,3 +5,4 @@ require 'nature.commands.doc'
require 'nature.commands.exit'
require 'nature.commands.guide'
require 'nature.commands.disown'
require 'nature.commands.fg'