packages feed

process-extras 0.1.1 → 0.1.2

raw patch · 2 files changed

+54/−1 lines, 2 files

Files

process-extras.cabal view
@@ -1,5 +1,5 @@ Name:               process-extras-Version:            0.1.1+Version:            0.1.2 Synopsis:           Process extras Description:        Extra functionality for the Process library                     <http://hackage.haskell.org/package/process>.@@ -25,6 +25,7 @@    Exposed-modules:     System.Process.ByteString+    System.Process.ByteString.Lazy     System.Process.Text    Build-depends:
+ src/System/Process/ByteString/Lazy.hs view
@@ -0,0 +1,52 @@+module System.Process.ByteString.Lazy where++import Control.Concurrent+import Control.Monad+import Data.ByteString.Lazy (ByteString)+import qualified Data.ByteString.Lazy as B+import System.Process+import System.Exit (ExitCode)+import System.IO++-- | Like 'System.Process.readProcessWithExitCode', but using 'ByteString'+readProcessWithExitCode+    :: FilePath                 -- ^ command to run+    -> [String]                 -- ^ any arguments+    -> ByteString               -- ^ standard input+    -> IO (ExitCode, ByteString, ByteString) -- ^ exitcode, stdout, stderr+readProcessWithExitCode cmd args input = do+    (Just inh, Just outh, Just errh, pid) <-+        createProcess (proc cmd args){ std_in  = CreatePipe,+                                       std_out = CreatePipe,+                                       std_err = CreatePipe }+    outMVar <- newEmptyMVar+    outM <- newEmptyMVar+    errM <- newEmptyMVar++    -- fork off a thread to start consuming stdout+    _ <- forkIO $ do+        out <- B.hGetContents outh+        putMVar outM out+        putMVar outMVar ()++    -- fork off a thread to start consuming stderr+    _ <- forkIO $ do+        err  <- B.hGetContents errh+        putMVar errM err+        putMVar outMVar ()++    -- now write and flush any input+    when (not (B.null input)) $ do B.hPutStr inh input; hFlush inh+    hClose inh -- done with stdin++    -- wait on the output+    takeMVar outMVar+    takeMVar outMVar+    hClose outh++    -- wait on the process+    ex <- waitForProcess pid+    out <- readMVar outM+    err <- readMVar errM++    return (ex, out, err)