This repository has been archived on 2015-04-29. You can view files and clone it, but cannot push or open issues or pull requests.
jsh/ps/main.go
Fredric Silberberg f21df51bd2 Added the PATH searching algorithm we talked about at the last meeting with Ciaraldi. It will find the location of our program
and search the PATH for that location. If found, it will search all remaining elements for the requested program. If the location
is not found on the PATH, then it will search the entire PATH for the requested program. If found, it returns the absolute path
to the program. If not, err.
2014-09-24 23:32:11 -04:00

51 lines
1.0 KiB
Go

package main
import (
"flag"
"fmt"
"jsh"
"strings"
)
// Converts raw output of "ps" into a slice of Process objects
func PsOutputToProcesses(out string) *[]jsh.Process {
processes := []jsh.Process{}
lines := strings.Split(out, "\n")
header, procs := lines[0], lines[1:]
numFields := len(strings.Fields(header))
for _, proc := range procs {
p, err := jsh.NewProcess(jsh.FieldsN(proc, numFields))
if err == nil {
processes = append(processes, *p)
}
}
return &processes
}
func runJsonMode() {
// Run procps-ng "ps" with full output
psOut := string(*jsh.FallbackWithArgs("ps", []string{"auxww"}))
processesPtr := PsOutputToProcesses(psOut)
frame := jsh.JshFrame{*processesPtr, []string{}}
queue := make(chan *jsh.JshFrame)
done := make(chan bool)
go jsh.OutputFrames(queue, done)
queue <- &frame
close(queue)
<-done
}
func main() {
// Parse for JSON flag.
jsonModePtr := flag.Bool("json", false, "a bool")
flag.Parse()
if !*jsonModePtr {
fmt.Printf("%s", jsh.Fallback("ps"))
} else {
runJsonMode()
}
}