Compare commits

..

No commits in common. "8fdde4aa58b176d32e0e92d09bcb4bd1b6cbf978" and "4bc94ce916cf0b20f7bb1725c4ad195a09a4cc0a" have entirely different histories.

4 changed files with 13 additions and 105 deletions

75
job.go
View File

@ -1,16 +1,15 @@
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
@ -74,10 +73,6 @@ 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
@ -96,6 +91,14 @@ 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},
@ -175,41 +178,11 @@ 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()
@ -274,33 +247,3 @@ 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
View File

@ -221,8 +221,6 @@ input:
}
fmt.Printf("\u001b[7m∆\u001b[0m" + strings.Repeat(" ", termwidth - 1) + "\r")
}
exit(0)
}
func continuePrompt(prev string) (string, error) {
@ -298,17 +296,10 @@ func contains(s []string, e string) bool {
}
func exit(code int) {
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)
}
// wait for all timers to finish before exiting
for {
if timers.running == 0 {
os.Exit(code)
}
}
os.Exit(code)
}

View File

@ -1,25 +0,0 @@
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)

View File

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