mirror of https://github.com/Hilbis/Hilbish
Compare commits
No commits in common. "8fdde4aa58b176d32e0e92d09bcb4bd1b6cbf978" and "4bc94ce916cf0b20f7bb1725c4ad195a09a4cc0a" have entirely different histories.
8fdde4aa58
...
4bc94ce916
75
job.go
75
job.go
|
@ -1,16 +1,15 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
|
||||||
|
|
||||||
"hilbish/util"
|
"hilbish/util"
|
||||||
|
|
||||||
rt "github.com/arnodel/golua/runtime"
|
rt "github.com/arnodel/golua/runtime"
|
||||||
|
"github.com/arnodel/golua/lib/iolib"
|
||||||
)
|
)
|
||||||
|
|
||||||
var jobs *jobHandler
|
var jobs *jobHandler
|
||||||
|
@ -74,10 +73,6 @@ func (j *job) finish() {
|
||||||
hooks.Em.Emit("job.done", j.lua())
|
hooks.Em.Emit("job.done", j.lua())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *job) wait() {
|
|
||||||
j.handle.Wait()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (j *job) setHandle(handle *exec.Cmd) {
|
func (j *job) setHandle(handle *exec.Cmd) {
|
||||||
j.handle = handle
|
j.handle = handle
|
||||||
j.args = handle.Args
|
j.args = handle.Args
|
||||||
|
@ -96,6 +91,14 @@ func (j *job) getProc() *os.Process {
|
||||||
return nil
|
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 {
|
func (j *job) lua() rt.Value {
|
||||||
jobFuncs := map[string]util.LuaExport{
|
jobFuncs := map[string]util.LuaExport{
|
||||||
"stop": {j.luaStop, 0, false},
|
"stop": {j.luaStop, 0, false},
|
||||||
|
@ -175,41 +178,11 @@ func (j *jobHandler) getLatest() *job {
|
||||||
return j.jobs[j.latestID]
|
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 {
|
func (j *jobHandler) loader(rtm *rt.Runtime) *rt.Table {
|
||||||
jobFuncs := map[string]util.LuaExport{
|
jobFuncs := map[string]util.LuaExport{
|
||||||
"all": {j.luaAllJobs, 0, false},
|
"all": {j.luaAllJobs, 0, false},
|
||||||
"last": {j.luaLastJob, 0, false},
|
|
||||||
"get": {j.luaGetJob, 1, false},
|
"get": {j.luaGetJob, 1, false},
|
||||||
"add": {j.luaAddJob, 2, false},
|
"add": {j.luaAddJob, 2, false},
|
||||||
"disown": {j.luaDisownJob, 1, false},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
luaJob := rt.NewTable()
|
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
|
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,8 +221,6 @@ input:
|
||||||
}
|
}
|
||||||
fmt.Printf("\u001b[7m∆\u001b[0m" + strings.Repeat(" ", termwidth - 1) + "\r")
|
fmt.Printf("\u001b[7m∆\u001b[0m" + strings.Repeat(" ", termwidth - 1) + "\r")
|
||||||
}
|
}
|
||||||
|
|
||||||
exit(0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func continuePrompt(prev string) (string, error) {
|
func continuePrompt(prev string) (string, error) {
|
||||||
|
@ -298,17 +296,10 @@ func contains(s []string, e string) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func exit(code int) {
|
func exit(code int) {
|
||||||
jobs.stopAll()
|
// wait for all timers to finish before exiting
|
||||||
|
for {
|
||||||
// wait for all timers to finish before exiting.
|
if timers.running == 0 {
|
||||||
// only do that when not interactive
|
os.Exit(code)
|
||||||
if !interactive {
|
|
||||||
for {
|
|
||||||
if timers.running == 0 {
|
|
||||||
os.Exit(code)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
os.Exit(code)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
|
|
@ -4,4 +4,3 @@ require 'nature.commands.cdr'
|
||||||
require 'nature.commands.doc'
|
require 'nature.commands.doc'
|
||||||
require 'nature.commands.exit'
|
require 'nature.commands.exit'
|
||||||
require 'nature.commands.guide'
|
require 'nature.commands.guide'
|
||||||
require 'nature.commands.disown'
|
|
||||||
|
|
Loading…
Reference in New Issue