2017-12-31 20:08:31 +00:00
|
|
|
{- Process transcript
|
|
|
|
-
|
2018-03-07 21:25:42 +00:00
|
|
|
- Copyright 2012-2018 Joey Hess <id@joeyh.name>
|
2017-12-31 20:08:31 +00:00
|
|
|
-
|
|
|
|
- License: BSD-2-clause
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
{-# OPTIONS_GHC -fno-warn-tabs #-}
|
|
|
|
|
2019-11-21 19:38:06 +00:00
|
|
|
module Utility.Process.Transcript (
|
|
|
|
processTranscript,
|
|
|
|
processTranscript',
|
|
|
|
processTranscript'',
|
|
|
|
) where
|
2017-12-31 20:08:31 +00:00
|
|
|
|
|
|
|
import Utility.Process
|
2018-03-15 19:34:25 +00:00
|
|
|
import Utility.Misc
|
2017-12-31 20:08:31 +00:00
|
|
|
|
|
|
|
import System.IO
|
2018-03-07 21:25:42 +00:00
|
|
|
import System.Exit
|
2018-03-15 19:34:25 +00:00
|
|
|
import Control.Concurrent.Async
|
2017-12-31 20:08:31 +00:00
|
|
|
import Control.Monad
|
|
|
|
#ifndef mingw32_HOST_OS
|
|
|
|
import qualified System.Posix.IO
|
|
|
|
#else
|
|
|
|
import Control.Applicative
|
|
|
|
#endif
|
|
|
|
import Data.Maybe
|
|
|
|
import Prelude
|
|
|
|
|
2018-03-07 21:25:42 +00:00
|
|
|
-- | Runs a process and returns a transcript combining its stdout and
|
|
|
|
-- stderr, and whether it succeeded or failed.
|
2017-12-31 20:08:31 +00:00
|
|
|
processTranscript :: String -> [String] -> (Maybe String) -> IO (String, Bool)
|
|
|
|
processTranscript cmd opts = processTranscript' (proc cmd opts)
|
|
|
|
|
2018-03-07 21:25:42 +00:00
|
|
|
-- | Also feeds the process some input.
|
2017-12-31 20:08:31 +00:00
|
|
|
processTranscript' :: CreateProcess -> Maybe String -> IO (String, Bool)
|
|
|
|
processTranscript' cp input = do
|
2018-03-07 21:25:42 +00:00
|
|
|
(t, c) <- processTranscript'' cp input
|
|
|
|
return (t, c == ExitSuccess)
|
|
|
|
|
|
|
|
processTranscript'' :: CreateProcess -> Maybe String -> IO (String, ExitCode)
|
|
|
|
processTranscript'' cp input = do
|
2017-12-31 20:08:31 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
|
|
|
{- This implementation interleves stdout and stderr in exactly the order
|
|
|
|
- the process writes them. -}
|
|
|
|
(readf, writef) <- System.Posix.IO.createPipe
|
2018-03-15 20:14:22 +00:00
|
|
|
System.Posix.IO.setFdOption readf System.Posix.IO.CloseOnExec True
|
|
|
|
System.Posix.IO.setFdOption writef System.Posix.IO.CloseOnExec True
|
2017-12-31 20:08:31 +00:00
|
|
|
readh <- System.Posix.IO.fdToHandle readf
|
|
|
|
writeh <- System.Posix.IO.fdToHandle writef
|
|
|
|
p@(_, _, _, pid) <- createProcess $ cp
|
|
|
|
{ std_in = if isJust input then CreatePipe else Inherit
|
|
|
|
, std_out = UseHandle writeh
|
|
|
|
, std_err = UseHandle writeh
|
|
|
|
}
|
|
|
|
hClose writeh
|
|
|
|
|
2018-03-15 19:34:25 +00:00
|
|
|
get <- asyncreader readh
|
2017-12-31 20:08:31 +00:00
|
|
|
writeinput input p
|
2018-03-15 19:34:25 +00:00
|
|
|
transcript <- wait get
|
2017-12-31 20:08:31 +00:00
|
|
|
#else
|
|
|
|
{- This implementation for Windows puts stderr after stdout. -}
|
|
|
|
p@(_, _, _, pid) <- createProcess $ cp
|
|
|
|
{ std_in = if isJust input then CreatePipe else Inherit
|
|
|
|
, std_out = CreatePipe
|
|
|
|
, std_err = CreatePipe
|
|
|
|
}
|
|
|
|
|
2018-03-15 19:34:25 +00:00
|
|
|
getout <- asyncreader (stdoutHandle p)
|
|
|
|
geterr <- asyncreader (stderrHandle p)
|
2017-12-31 20:08:31 +00:00
|
|
|
writeinput input p
|
2018-03-15 19:34:25 +00:00
|
|
|
transcript <- (++) <$> wait getout <*> wait geterr
|
2017-12-31 20:08:31 +00:00
|
|
|
#endif
|
2018-03-07 21:25:42 +00:00
|
|
|
code <- waitForProcess pid
|
|
|
|
return (transcript, code)
|
2017-12-31 20:08:31 +00:00
|
|
|
where
|
2018-03-15 19:34:25 +00:00
|
|
|
asyncreader = async . hGetContentsStrict
|
2017-12-31 20:08:31 +00:00
|
|
|
|
|
|
|
writeinput (Just s) p = do
|
|
|
|
let inh = stdinHandle p
|
|
|
|
unless (null s) $ do
|
|
|
|
hPutStr inh s
|
|
|
|
hFlush inh
|
|
|
|
hClose inh
|
|
|
|
writeinput Nothing _ = return ()
|