add jobs, kill commands
This commit is contained in:
parent
37c56cb322
commit
e7d25057f6
3 changed files with 85 additions and 1 deletions
|
|
@ -21,6 +21,7 @@ import (
|
|||
"os"
|
||||
"fmt"
|
||||
"bytes"
|
||||
"strconv"
|
||||
"os/exec"
|
||||
"syscall"
|
||||
"os/signal"
|
||||
|
|
@ -154,6 +155,29 @@ func fg(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
|||
return nil
|
||||
}
|
||||
|
||||
func jobs(in *ast.Token, vt ast.VarTable, fg ast.FuncTable) *ast.Token {
|
||||
ret := &ast.Token{
|
||||
Tag: ast.LIST,
|
||||
Inner: &ast.Token{
|
||||
Tag: ast.STRING,
|
||||
Inner: fmt.Sprintf("Total: %d", len(bgProcs)),
|
||||
},
|
||||
}
|
||||
|
||||
ptr := ret.Inner.(*ast.Token)
|
||||
iter := &ptr
|
||||
for i := 0; i < len(bgProcs); i += 1 {
|
||||
(*iter).Next = &ast.Token{
|
||||
Tag: ast.STRING,
|
||||
Inner: fmt.Sprintf("[%d]: %d", i, bgProcs[i].Process.Pid),
|
||||
}
|
||||
|
||||
iter = &(*iter).Next
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func read_cmd(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
||||
if in == nil {
|
||||
return nil
|
||||
|
|
@ -211,3 +235,46 @@ func read_cmd(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
|||
func get_exit(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
||||
return &ast.Token{Tag: ast.NUMBER, Inner: fmt.Sprintf("%d", LastExitCode)}
|
||||
}
|
||||
|
||||
func kill(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
||||
if in.Tag == ast.LIST {
|
||||
log.Log(log.ERR, "non-number argument to kill function", "kill")
|
||||
return nil
|
||||
}
|
||||
|
||||
pid, err := strconv.ParseInt(in.Inner.(string), 10, 64)
|
||||
if err != nil {
|
||||
log.Log(log.ERR, "error parsing arg to kill: " + err.Error(), "kill")
|
||||
return nil
|
||||
}
|
||||
|
||||
found := false
|
||||
newBgProcs := []*exec.Cmd{}
|
||||
for _, i := range bgProcs {
|
||||
if i.Process.Pid != int(pid) {
|
||||
newBgProcs = append(newBgProcs, i)
|
||||
} else {
|
||||
found = true
|
||||
err = i.Process.Kill()
|
||||
if err != nil {
|
||||
log.Log(log.ERR, fmt.Sprintf("error killing process %d: %s",
|
||||
int(pid), err.Error()), "kill")
|
||||
newBgProcs = append(newBgProcs, i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bgProcs = newBgProcs
|
||||
|
||||
if !found {
|
||||
// docs say no error on unix systems
|
||||
proc, _ := os.FindProcess(int(pid))
|
||||
err = proc.Kill()
|
||||
if err != nil {
|
||||
log.Log(log.ERR, fmt.Sprintf("error killing process %d: %s",
|
||||
int(pid), err.Error()), "kill")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue