Skip to content

Commit

Permalink
Move createPipe to per-OS modules
Browse files Browse the repository at this point in the history
  • Loading branch information
snoyberg committed Nov 2, 2015
1 parent 5d81377 commit e63f474
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 38 deletions.
38 changes: 0 additions & 38 deletions System/Process.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,9 @@ import System.IO
import System.IO.Error (mkIOError, ioeSetErrorString)

#if defined(mingw32_HOST_OS)
# include <io.h> /* for _close and _pipe */
# include <fcntl.h> /* for _O_BINARY */
import Control.Exception (onException)
#else
import System.Posix.Process (getProcessGroupIDOf)
import qualified System.Posix.IO as Posix
#if MIN_VERSION_base(4,5,0)
import System.Posix.Types
#endif
Expand Down Expand Up @@ -934,38 +931,3 @@ rawSystem cmd args = system (showCommandForUser cmd args)
#else
rawSystem cmd args = system (showCommandForUser cmd args)
#endif

-- ---------------------------------------------------------------------------
-- createPipe

-- | Create a pipe for interprocess communication and return a
-- @(readEnd, writeEnd)@ `Handle` pair.
--
-- @since 1.2.1.0
createPipe :: IO (Handle, Handle)
#if !mingw32_HOST_OS
createPipe = do
(readfd, writefd) <- Posix.createPipe
readh <- Posix.fdToHandle readfd
writeh <- Posix.fdToHandle writefd
return (readh, writeh)
#else
createPipe = do
(readfd, writefd) <- allocaArray 2 $ \ pfds -> do
throwErrnoIfMinus1_ "_pipe" $ c__pipe pfds 2 (#const _O_BINARY)
readfd <- peek pfds
writefd <- peekElemOff pfds 1
return (readfd, writefd)
(do readh <- fdToHandle readfd
writeh <- fdToHandle writefd
return (readh, writeh)) `onException` (close readfd >> close writefd)

close :: CInt -> IO ()
close = throwErrnoIfMinus1_ "_close" . c__close

foreign import ccall "io.h _pipe" c__pipe ::
Ptr CInt -> CUInt -> CInt -> IO CInt

foreign import ccall "io.h _close" c__close ::
CInt -> IO CInt
#endif
12 changes: 12 additions & 0 deletions System/Process/Internals.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ module System.Process.Internals (
#endif
withFilePathException, withCEnvironment,
translate,
createPipe,
) where

import Foreign.C
Expand Down Expand Up @@ -158,3 +159,14 @@ runGenProcess_ fun c (Just sig) (Just sig') | isDefaultSignal sig && sig == sig'
runGenProcess_ fun c _ _ = createProcess_ fun c

#endif

-- ---------------------------------------------------------------------------
-- createPipe

-- | Create a pipe for interprocess communication and return a
-- @(readEnd, writeEnd)@ `Handle` pair.
--
-- @since 1.2.1.0
createPipe :: IO (Handle, Handle)
createPipe = createPipeInternal
{-# INLINE createPipe #-}
9 changes: 9 additions & 0 deletions System/Process/Posix.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module System.Process.Posix
, defaultSignal
, c_execvpe
, pPrPr_disableITimers
, createPipeInternal
) where

import Control.Concurrent
Expand All @@ -34,6 +35,7 @@ import System.Posix.Types
import System.Posix.Internals
import GHC.IO.Exception
import System.Posix.Signals as Sig
import qualified System.Posix.IO as Posix

import System.Process.Common

Expand Down Expand Up @@ -271,3 +273,10 @@ defaultSignal = CONST_SIG_DFL

isDefaultSignal :: CLong -> Bool
isDefaultSignal = (== defaultSignal)

createPipeInternal :: IO (Handle, Handle)
createPipeInternal = do
(readfd, writefd) <- Posix.createPipe
readh <- Posix.fdToHandle readfd
writeh <- Posix.fdToHandle writefd
return (readh, writeh)
22 changes: 22 additions & 0 deletions System/Process/Windows.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module System.Process.Windows
, endDelegateControlC
, stopDelegateControlC
, isDefaultSignal
, createPipeInternal
) where

import System.Process.Common
Expand Down Expand Up @@ -45,6 +46,7 @@ import System.Process.Common
# define WINDOWS_CCONV ccall
#endif

#include <io.h> /* for _close and _pipe */
#include "HsProcessConfig.h"
#include "processFlags.h"

Expand Down Expand Up @@ -240,3 +242,23 @@ withCEnvironment envir act =

isDefaultSignal :: CLong -> Bool
isDefaultSignal = const False

createPipeInternal :: IO (Handle, Handle)
createPipeInternal = do
(readfd, writefd) <- allocaArray 2 $ \ pfds -> do
throwErrnoIfMinus1_ "_pipe" $ c__pipe pfds 2 (#const _O_BINARY)
readfd <- peek pfds
writefd <- peekElemOff pfds 1
return (readfd, writefd)
(do readh <- fdToHandle readfd
writeh <- fdToHandle writefd
return (readh, writeh)) `onException` (close readfd >> close writefd)

close :: CInt -> IO ()
close = throwErrnoIfMinus1_ "_close" . c__close

foreign import ccall "io.h _pipe" c__pipe ::
Ptr CInt -> CUInt -> CInt -> IO CInt

foreign import ccall "io.h _close" c__close ::
CInt -> IO CInt

0 comments on commit e63f474

Please sign in to comment.