module System.Unix.ProcessStrict
(
simpleProcess
, processResult
, processOutput
, simpleCommand
, commandResult
, commandOutput
) where
import Control.Exception hiding (catch)
import Control.Parallel.Strategies (rdeepseq)
import System.Process (waitForProcess, runInteractiveProcess, runInteractiveCommand)
import System.IO (hSetBinaryMode, hClose, hGetContents)
import System.Unix.Process
simpleProcess :: FilePath -> [String] -> IO (String, String, ExitCode)
simpleProcess exec args =
do (inp,out,err,pid) <- runInteractiveProcess exec args Nothing Nothing
hSetBinaryMode out True
hSetBinaryMode err True
hClose inp
outStr <- hGetContents out
errStr <- hGetContents err
_ <- evaluate (rdeepseq outStr)
_ <- evaluate (rdeepseq errStr)
ec <- waitForProcess pid
return (outStr, errStr, ec)
processResult :: FilePath -> [String] -> IO (Either Int (String, String))
processResult exec args =
simpleProcess exec args >>= return . resultOrCode
where
resultOrCode (_, _, ExitFailure n) = Left n
resultOrCode (out, err, ExitSuccess) = Right (out, err)
processOutput :: FilePath -> [String] -> IO (Either Int String)
processOutput exec args =
simpleProcess exec args >>= return . outputOrCode
where
outputOrCode (_, _, ExitFailure n) = Left n
outputOrCode (out, _, ExitSuccess) = Right out
simpleCommand :: String -> IO (String, String, ExitCode)
simpleCommand cmd =
do (inp,out,err,pid) <- runInteractiveCommand cmd
hClose inp
outStr <- hGetContents out
errStr <- hGetContents err
_ <- evaluate (rdeepseq outStr)
_ <- evaluate (rdeepseq errStr)
ec <- waitForProcess pid
return (outStr, errStr, ec)
commandResult :: String -> IO (Either Int (String, String))
commandResult cmd =
simpleCommand cmd >>= return . resultOrCode
where
resultOrCode (_, _, ExitFailure n) = Left n
resultOrCode (out, err, ExitSuccess) = Right (out, err)
commandOutput :: String -> IO (Either Int String)
commandOutput cmd =
simpleCommand cmd >>= return . outputOrCode
where
outputOrCode (_, _, ExitFailure n) = Left n
outputOrCode (out, _, ExitSuccess) = Right out