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