mirror of https://github.com/Hilbis/Hilbish
feat: implement job foregrounding/backgrounding (#155)
parent
d65b574cb2
commit
8697344f98
@ -0,0 +1,38 @@
|
||||
// +build darwin linux
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"syscall"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func (j *job) foreground() error {
|
||||
if jobs.foreground {
|
||||
return errors.New("(another) job already foregrounded")
|
||||
}
|
||||
|
||||
pgid, _ := syscall.Getpgid(j.pid)
|
||||
// tcsetpgrp
|
||||
unix.IoctlSetPointerInt(0, unix.TIOCSPGRP, pgid)
|
||||
proc, _ := os.FindProcess(j.pid)
|
||||
proc.Wait()
|
||||
|
||||
hshPgid, _ := syscall.Getpgid(os.Getpid())
|
||||
unix.IoctlSetPointerInt(0, unix.TIOCSPGRP, hshPgid)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (j *job) background() error {
|
||||
proc := j.handle.Process
|
||||
if proc == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
proc.Signal(syscall.SIGCONT)
|
||||
return nil
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
// +build windows
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
func (j *job) foreground() error {
|
||||
return errors.New("not supported on windows")
|
||||
}
|
||||
|
||||
func (j *job) background() error {
|
||||
return errors.New("not supported on windows")
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
local commander = require 'commander'
|
||||
|
||||
commander.register('bg', function()
|
||||
local job = hilbish.jobs.last()
|
||||
if not job then
|
||||
print 'bg: no last job'
|
||||
return 1
|
||||
end
|
||||
|
||||
local err = job.background()
|
||||
if err then
|
||||
print('bg: ' .. err)
|
||||
return 2
|
||||
end
|
||||
end)
|
@ -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)
|
Loading…
Reference in new issue