diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ProcessUtils.hs | 39 |
1 files changed, 38 insertions, 1 deletions
diff --git a/lib/ProcessUtils.hs b/lib/ProcessUtils.hs index 4e3ac38..49483b5 100644 --- a/lib/ProcessUtils.hs +++ b/lib/ProcessUtils.hs | |||
@@ -42,4 +42,41 @@ systemEnv vars cmd = do | |||
42 | _ <- installHandler sigQUIT old_quit Nothing | 42 | _ <- installHandler sigQUIT old_quit Nothing |
43 | return r | 43 | return r |
44 | 44 | ||
45 | 45 | -- | readPipe | |
46 | -- | ||
47 | -- This is like System.Process.readProcess but instead of a single | ||
48 | -- command with arguments, you pass it a list which it then executes | ||
49 | -- piping the the output from the prior command into the input for | ||
50 | -- the next command. Like readProcess the next parameter is a string | ||
51 | -- which is sent as input into the initial standard in. | ||
52 | -- | ||
53 | -- > readPipe [("grep",["HostConfig"]),("wc",["-l"]) =<< readFile "input" | ||
54 | -- | ||
55 | -- is equivalent to shell code: | ||
56 | -- | ||
57 | -- > grep HostConfig input | wc -l | ||
58 | -- | ||
59 | readPipe :: [(FilePath,[String])] -> String -> IO String | ||
60 | readPipe [(cmd,args)] input = readProcess cmd args input | ||
61 | readPipe ((cmd,args):xs) stdin0 = do | ||
62 | let p = (shell cmd) { std_in = CreatePipe | ||
63 | , std_out = CreatePipe | ||
64 | , std_err = CreatePipe | ||
65 | , cmdspec = RawCommand cmd args | ||
66 | } | ||
67 | (Just sinh,Just sout,Just serr, ph) <- createProcess p | ||
68 | hPutStr sinh stdin0 | ||
69 | hClose sinh | ||
70 | readPipe0 xs sout | ||
71 | where | ||
72 | readPipe0 :: [(FilePath,[String])] -> Handle -> IO String | ||
73 | readPipe0 ((cmd,args):xs) stdin0 = do | ||
74 | let p = (shell cmd) | ||
75 | { std_in = UseHandle stdin0 | ||
76 | , std_out = CreatePipe | ||
77 | , std_err = CreatePipe | ||
78 | , cmdspec = RawCommand cmd args | ||
79 | } | ||
80 | (Nothing,Just sout,Just serr, ph) <- createProcess p | ||
81 | readPipe0 xs sout | ||
82 | readPipe0 [] h = hGetContents h | ||