{-# LANGUAGE CPP, MultiParamTypeClasses #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module System.Process.ByteString.Lazy where

#if __GLASGOW_HASKELL__ <= 709
import Control.Applicative ((<$>))
#endif
import Control.DeepSeq (force)
import qualified Control.Exception as C (evaluate)
import Data.ByteString.Lazy (ByteString, toChunks, fromChunks)
import Data.ListLike.IO (hGetContents)
import Data.Word (Word8)
import Prelude hiding (null)
import System.Process
import System.Process.Common
import System.Exit (ExitCode)

#if !MIN_VERSION_bytestring(0,10,0)
import Control.DeepSeq (NFData)

instance NFData ByteString
#endif

instance ProcessText ByteString Word8

-- | Like 'System.Process.readProcessWithExitCode', but using 'ByteString'
instance ListLikeProcessIO ByteString Word8 where
    forceOutput :: ByteString -> IO ByteString
forceOutput = ByteString -> IO ByteString
forall a. a -> IO a
C.evaluate (ByteString -> IO ByteString)
-> (ByteString -> ByteString) -> ByteString -> IO ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
forall a. NFData a => a -> a
force
    readChunks :: Handle -> IO [ByteString]
readChunks Handle
h = ((StrictByteString -> ByteString)
-> [StrictByteString] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
map ([StrictByteString] -> ByteString
fromChunks ([StrictByteString] -> ByteString)
-> (StrictByteString -> [StrictByteString])
-> StrictByteString
-> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (StrictByteString -> [StrictByteString] -> [StrictByteString]
forall a. a -> [a] -> [a]
: [])) ([StrictByteString] -> [ByteString])
-> (ByteString -> [StrictByteString]) -> ByteString -> [ByteString]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [StrictByteString]
toChunks) (ByteString -> [ByteString]) -> IO ByteString -> IO [ByteString]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Handle -> IO ByteString
forall full item. ListLikeIO full item => Handle -> IO full
hGetContents Handle
h

-- | Specialized version for backwards compatibility.
readProcessWithExitCode
    :: FilePath                              -- ^ command to run
    -> [String]                              -- ^ any arguments
    -> ByteString                            -- ^ standard input
    -> IO (ExitCode, ByteString, ByteString) -- ^ exitcode, stdout, stderr
readProcessWithExitCode :: FilePath
-> [FilePath]
-> ByteString
-> IO (ExitCode, ByteString, ByteString)
readProcessWithExitCode = FilePath
-> [FilePath]
-> ByteString
-> IO (ExitCode, ByteString, ByteString)
forall text char.
ListLikeProcessIO text char =>
FilePath -> [FilePath] -> text -> IO (ExitCode, text, text)
System.Process.Common.readProcessWithExitCode

readCreateProcessWithExitCode
    :: CreateProcess                         -- ^ command and arguments to run
    -> ByteString                            -- ^ standard input
    -> IO (ExitCode, ByteString, ByteString) -- ^ exitcode, stdout, stderr
readCreateProcessWithExitCode :: CreateProcess
-> ByteString -> IO (ExitCode, ByteString, ByteString)
readCreateProcessWithExitCode = CreateProcess
-> ByteString -> IO (ExitCode, ByteString, ByteString)
forall maker text char.
(ProcessMaker maker, ListLikeProcessIO text char) =>
maker -> text -> IO (ExitCode, text, text)
System.Process.Common.readCreateProcessWithExitCode