mirror of https://github.com/Hilbis/Hilbish
Compare commits
6 Commits
4bc94ce916
...
8fdde4aa58
Author | SHA1 | Date |
---|---|---|
TorchedSammy | 8fdde4aa58 | |
TorchedSammy | dde56cbb4f | |
TorchedSammy | bbff7098c2 | |
TorchedSammy | b3a5c0e67c | |
TorchedSammy | fc40bad092 | |
TorchedSammy | db6817e4ca |
75
job.go
75
job.go
|
@ -1,15 +1,16 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"sync"
|
||||
"syscall"
|
||||
|
||||
"hilbish/util"
|
||||
|
||||
rt "github.com/arnodel/golua/runtime"
|
||||
"github.com/arnodel/golua/lib/iolib"
|
||||
)
|
||||
|
||||
var jobs *jobHandler
|
||||
|
@ -73,6 +74,10 @@ func (j *job) finish() {
|
|||
hooks.Em.Emit("job.done", j.lua())
|
||||
}
|
||||
|
||||
func (j *job) wait() {
|
||||
j.handle.Wait()
|
||||
}
|
||||
|
||||
func (j *job) setHandle(handle *exec.Cmd) {
|
||||
j.handle = handle
|
||||
j.args = handle.Args
|
||||
|
@ -91,14 +96,6 @@ func (j *job) getProc() *os.Process {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (j *job) setStdio(typ string, f *iolib.File) {
|
||||
switch typ {
|
||||
case "in": j.stdin = f.File
|
||||
case "out": j.stdout = f.File
|
||||
case "err": j.stderr = f.File
|
||||
}
|
||||
}
|
||||
|
||||
func (j *job) lua() rt.Value {
|
||||
jobFuncs := map[string]util.LuaExport{
|
||||
"stop": {j.luaStop, 0, false},
|
||||
|
@ -178,11 +175,41 @@ func (j *jobHandler) getLatest() *job {
|
|||
return j.jobs[j.latestID]
|
||||
}
|
||||
|
||||
func (j *jobHandler) disown(id int) error {
|
||||
j.mu.RLock()
|
||||
if j.jobs[id] == nil {
|
||||
return errors.New("job doesnt exist")
|
||||
}
|
||||
j.mu.RUnlock()
|
||||
|
||||
j.mu.Lock()
|
||||
delete(j.jobs, id)
|
||||
j.mu.Unlock()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (j *jobHandler) stopAll() {
|
||||
j.mu.RLock()
|
||||
defer j.mu.RUnlock()
|
||||
|
||||
for _, jb := range j.jobs {
|
||||
// on exit, unix shell should send sighup to all jobs
|
||||
if jb.running {
|
||||
proc := jb.getProc()
|
||||
proc.Signal(syscall.SIGHUP)
|
||||
jb.wait() // waits for program to exit due to sighup
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (j *jobHandler) loader(rtm *rt.Runtime) *rt.Table {
|
||||
jobFuncs := map[string]util.LuaExport{
|
||||
"all": {j.luaAllJobs, 0, false},
|
||||
"last": {j.luaLastJob, 0, false},
|
||||
"get": {j.luaGetJob, 1, false},
|
||||
"add": {j.luaAddJob, 2, false},
|
||||
"disown": {j.luaDisownJob, 1, false},
|
||||
}
|
||||
|
||||
luaJob := rt.NewTable()
|
||||
|
@ -247,3 +274,33 @@ func (j *jobHandler) luaAllJobs(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|||
|
||||
return c.PushingNext1(t.Runtime, rt.TableValue(jobTbl)), nil
|
||||
}
|
||||
|
||||
func (j *jobHandler) luaDisownJob(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||
if err := c.Check1Arg(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
jobID, err := c.IntArg(0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = j.disown(int(jobID))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return c.Next(), nil
|
||||
}
|
||||
|
||||
func (j *jobHandler) luaLastJob(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||
j.mu.RLock()
|
||||
defer j.mu.RUnlock()
|
||||
|
||||
job := j.jobs[j.latestID]
|
||||
if job == nil { // incase we dont have any jobs yet
|
||||
return c.Next(), nil
|
||||
}
|
||||
|
||||
return c.PushingNext1(t.Runtime, job.lua()), nil
|
||||
}
|
||||
|
||||
|
|
17
main.go
17
main.go
|
@ -221,6 +221,8 @@ input:
|
|||
}
|
||||
fmt.Printf("\u001b[7m∆\u001b[0m" + strings.Repeat(" ", termwidth - 1) + "\r")
|
||||
}
|
||||
|
||||
exit(0)
|
||||
}
|
||||
|
||||
func continuePrompt(prev string) (string, error) {
|
||||
|
@ -296,10 +298,17 @@ func contains(s []string, e string) bool {
|
|||
}
|
||||
|
||||
func exit(code int) {
|
||||
// wait for all timers to finish before exiting
|
||||
for {
|
||||
if timers.running == 0 {
|
||||
os.Exit(code)
|
||||
jobs.stopAll()
|
||||
|
||||
// wait for all timers to finish before exiting.
|
||||
// only do that when not interactive
|
||||
if !interactive {
|
||||
for {
|
||||
if timers.running == 0 {
|
||||
os.Exit(code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
os.Exit(code)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
local commander = require 'commander'
|
||||
|
||||
commander.register('disown', function(args)
|
||||
if #hilbish.jobs.all() == 0 then
|
||||
print 'disown: no current job'
|
||||
return 1
|
||||
end
|
||||
|
||||
local id
|
||||
if #args < 0 then
|
||||
id = tonumber(args[1])
|
||||
if not id then
|
||||
print 'disown: invalid id for job'
|
||||
return 1
|
||||
end
|
||||
else
|
||||
id = hilbish.jobs.last().id
|
||||
end
|
||||
|
||||
local ok = pcall(hilbish.jobs.disown, id)
|
||||
if not ok then
|
||||
print 'disown: job does not exist'
|
||||
return 2
|
||||
end
|
||||
end)
|
|
@ -4,3 +4,4 @@ require 'nature.commands.cdr'
|
|||
require 'nature.commands.doc'
|
||||
require 'nature.commands.exit'
|
||||
require 'nature.commands.guide'
|
||||
require 'nature.commands.disown'
|
||||
|
|
Loading…
Reference in New Issue