{-# LANGUAGE Safe #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE RankNTypes #-}
module GHC.Internal.System.IO.OS
(
withFileDescriptorReadingBiased,
withFileDescriptorWritingBiased,
withWindowsHandleReadingBiased,
withWindowsHandleWritingBiased,
withFileDescriptorReadingBiasedRaw,
withFileDescriptorWritingBiasedRaw,
withWindowsHandleReadingBiasedRaw,
withWindowsHandleWritingBiasedRaw
)
where
import GHC.Internal.Control.Monad (return)
import GHC.Internal.Control.Concurrent.MVar (MVar)
import GHC.Internal.Control.Exception (mask)
import GHC.Internal.Data.Function (const, (.), ($))
import GHC.Internal.Data.Functor (fmap)
#if defined(mingw32_HOST_OS)
import GHC.Internal.Data.Bool (otherwise)
#endif
import GHC.Internal.Data.Maybe (Maybe (Nothing), maybe)
#if defined(mingw32_HOST_OS)
import GHC.Internal.Data.Maybe (Maybe (Just))
#endif
import GHC.Internal.Data.List ((++))
import GHC.Internal.Data.String (String)
import GHC.Internal.Data.Typeable (Typeable, cast)
import GHC.Internal.System.IO (IO)
import GHC.Internal.IO.FD (fdFD)
#if defined(mingw32_HOST_OS)
import GHC.Internal.IO.Windows.Handle
(
NativeHandle,
ConsoleHandle,
IoHandle,
toHANDLE
)
#endif
import GHC.Internal.IO.Handle.Types
(
Handle (FileHandle, DuplexHandle),
Handle__ (Handle__, haDevice)
)
import GHC.Internal.IO.Handle.Internals (withHandle_', flushBuffer)
import GHC.Internal.IO.Exception
(
IOErrorType (InappropriateType),
IOException (IOError),
ioException
)
import GHC.Internal.Foreign.Ptr (Ptr)
import GHC.Internal.Foreign.C.Types (CInt)
withOSHandle :: String
-> (Handle -> MVar Handle__)
-> (forall d. Typeable d => d -> IO a)
-> (Handle__ -> IO ())
-> Handle
-> (a -> IO r)
-> IO r
withOSHandle :: forall a r.
String
-> (Handle -> MVar Handle__)
-> (forall d. Typeable d => d -> IO a)
-> (Handle__ -> IO ())
-> Handle
-> (a -> IO r)
-> IO r
withOSHandle String
opName Handle -> MVar Handle__
handleStateVar forall d. Typeable d => d -> IO a
getOSHandle Handle__ -> IO ()
prepare Handle
handle a -> IO r
act
= ((forall a. IO a -> IO a) -> IO r) -> IO r
forall b. ((forall a. IO a -> IO a) -> IO b) -> IO b
mask (((forall a. IO a -> IO a) -> IO r) -> IO r)
-> ((forall a. IO a -> IO a) -> IO r) -> IO r
forall a b. (a -> b) -> a -> b
$ \ forall a. IO a -> IO a
withOriginalMaskingState ->
(Handle__ -> IO r) -> IO r
forall {a}. (Handle__ -> IO a) -> IO a
withHandleState ((Handle__ -> IO r) -> IO r) -> (Handle__ -> IO r) -> IO r
forall a b. (a -> b) -> a -> b
$ \ handleState :: Handle__
handleState@Handle__ {haDevice :: ()
haDevice = dev
dev} -> do
osHandle <- dev -> IO a
forall d. Typeable d => d -> IO a
getOSHandle dev
dev
prepare handleState
withOriginalMaskingState $ act osHandle
where
withHandleState :: (Handle__ -> IO a) -> IO a
withHandleState = String -> Handle -> MVar Handle__ -> (Handle__ -> IO a) -> IO a
forall a.
String -> Handle -> MVar Handle__ -> (Handle__ -> IO a) -> IO a
withHandle_' String
opName Handle
handle (Handle -> MVar Handle__
handleStateVar Handle
handle)
handleStateVarReadingBiased :: Handle -> MVar Handle__
handleStateVarReadingBiased :: Handle -> MVar Handle__
handleStateVarReadingBiased (FileHandle String
_ MVar Handle__
var) = MVar Handle__
var
handleStateVarReadingBiased (DuplexHandle String
_ MVar Handle__
readingVar MVar Handle__
_) = MVar Handle__
readingVar
handleStateVarWritingBiased :: Handle -> MVar Handle__
handleStateVarWritingBiased :: Handle -> MVar Handle__
handleStateVarWritingBiased (FileHandle String
_ MVar Handle__
var) = MVar Handle__
var
handleStateVarWritingBiased (DuplexHandle String
_ MVar Handle__
_ MVar Handle__
writingVar) = MVar Handle__
writingVar
requiringOSHandleOfType :: String
-> Maybe a
-> IO a
requiringOSHandleOfType :: forall a. String -> Maybe a -> IO a
requiringOSHandleOfType String
osHandleTypeName
= IO a -> (a -> IO a) -> Maybe a -> IO a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (IOException -> IO a
forall a. HasCallStack => IOException -> IO a
ioException IOException
osHandleOfTypeRequired) a -> IO a
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return
where
osHandleOfTypeRequired :: IOException
osHandleOfTypeRequired :: IOException
osHandleOfTypeRequired
= Maybe Handle
-> IOErrorType
-> String
-> String
-> Maybe CInt
-> Maybe String
-> IOException
IOError Maybe Handle
forall a. Maybe a
Nothing
IOErrorType
InappropriateType
String
""
(String
"handle does not use " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
osHandleTypeName String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"s")
Maybe CInt
forall a. Maybe a
Nothing
Maybe String
forall a. Maybe a
Nothing
getFileDescriptor :: Typeable d => d -> IO CInt
getFileDescriptor :: forall d. Typeable d => d -> IO CInt
getFileDescriptor = String -> Maybe CInt -> IO CInt
forall a. String -> Maybe a -> IO a
requiringOSHandleOfType String
"POSIX file descriptor" (Maybe CInt -> IO CInt) -> (d -> Maybe CInt) -> d -> IO CInt
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
(FD -> CInt) -> Maybe FD -> Maybe CInt
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap FD -> CInt
fdFD (Maybe FD -> Maybe CInt) -> (d -> Maybe FD) -> d -> Maybe CInt
forall b c a. (b -> c) -> (a -> b) -> a -> c
. d -> Maybe FD
forall a b. (Typeable a, Typeable b) => a -> Maybe b
cast
getWindowsHandle :: Typeable d => d -> IO (Ptr ())
getWindowsHandle :: forall d. Typeable d => d -> IO (Ptr ())
getWindowsHandle = String -> Maybe (Ptr ()) -> IO (Ptr ())
forall a. String -> Maybe a -> IO a
requiringOSHandleOfType String
"Windows handle" (Maybe (Ptr ()) -> IO (Ptr ()))
-> (d -> Maybe (Ptr ())) -> d -> IO (Ptr ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
d -> Maybe (Ptr ())
forall d. Typeable d => d -> Maybe (Ptr ())
toMaybeWindowsHandle
where
toMaybeWindowsHandle :: Typeable d => d -> Maybe (Ptr ())
#if defined(mingw32_HOST_OS)
toMaybeWindowsHandle dev
| Just nativeHandle <- cast dev :: Maybe (IoHandle NativeHandle)
= Just (toHANDLE nativeHandle)
| Just consoleHandle <- cast dev :: Maybe (IoHandle ConsoleHandle)
= Just (toHANDLE consoleHandle)
| otherwise
= Nothing
#else
toMaybeWindowsHandle :: forall d. Typeable d => d -> Maybe (Ptr ())
toMaybeWindowsHandle d
_ = Maybe (Ptr ())
forall a. Maybe a
Nothing
#endif
withFileDescriptorReadingBiased :: Handle -> (CInt -> IO r) -> IO r
withFileDescriptorReadingBiased :: forall r. Handle -> (CInt -> IO r) -> IO r
withFileDescriptorReadingBiased = String
-> (Handle -> MVar Handle__)
-> (forall d. Typeable d => d -> IO CInt)
-> (Handle__ -> IO ())
-> Handle
-> (CInt -> IO r)
-> IO r
forall a r.
String
-> (Handle -> MVar Handle__)
-> (forall d. Typeable d => d -> IO a)
-> (Handle__ -> IO ())
-> Handle
-> (a -> IO r)
-> IO r
withOSHandle String
"withFileDescriptorReadingBiased"
Handle -> MVar Handle__
handleStateVarReadingBiased
d -> IO CInt
forall d. Typeable d => d -> IO CInt
getFileDescriptor
Handle__ -> IO ()
flushBuffer
withFileDescriptorWritingBiased :: Handle -> (CInt -> IO r) -> IO r
withFileDescriptorWritingBiased :: forall r. Handle -> (CInt -> IO r) -> IO r
withFileDescriptorWritingBiased = String
-> (Handle -> MVar Handle__)
-> (forall d. Typeable d => d -> IO CInt)
-> (Handle__ -> IO ())
-> Handle
-> (CInt -> IO r)
-> IO r
forall a r.
String
-> (Handle -> MVar Handle__)
-> (forall d. Typeable d => d -> IO a)
-> (Handle__ -> IO ())
-> Handle
-> (a -> IO r)
-> IO r
withOSHandle String
"withFileDescriptorWritingBiased"
Handle -> MVar Handle__
handleStateVarWritingBiased
d -> IO CInt
forall d. Typeable d => d -> IO CInt
getFileDescriptor
Handle__ -> IO ()
flushBuffer
withWindowsHandleReadingBiased :: Handle -> (Ptr () -> IO r) -> IO r
withWindowsHandleReadingBiased :: forall r. Handle -> (Ptr () -> IO r) -> IO r
withWindowsHandleReadingBiased = String
-> (Handle -> MVar Handle__)
-> (forall d. Typeable d => d -> IO (Ptr ()))
-> (Handle__ -> IO ())
-> Handle
-> (Ptr () -> IO r)
-> IO r
forall a r.
String
-> (Handle -> MVar Handle__)
-> (forall d. Typeable d => d -> IO a)
-> (Handle__ -> IO ())
-> Handle
-> (a -> IO r)
-> IO r
withOSHandle String
"withWindowsHandleReadingBiased"
Handle -> MVar Handle__
handleStateVarReadingBiased
d -> IO (Ptr ())
forall d. Typeable d => d -> IO (Ptr ())
getWindowsHandle
Handle__ -> IO ()
flushBuffer
withWindowsHandleWritingBiased :: Handle -> (Ptr () -> IO r) -> IO r
withWindowsHandleWritingBiased :: forall r. Handle -> (Ptr () -> IO r) -> IO r
withWindowsHandleWritingBiased = String
-> (Handle -> MVar Handle__)
-> (forall d. Typeable d => d -> IO (Ptr ()))
-> (Handle__ -> IO ())
-> Handle
-> (Ptr () -> IO r)
-> IO r
forall a r.
String
-> (Handle -> MVar Handle__)
-> (forall d. Typeable d => d -> IO a)
-> (Handle__ -> IO ())
-> Handle
-> (a -> IO r)
-> IO r
withOSHandle String
"withWindowsHandleWritingBiased"
Handle -> MVar Handle__
handleStateVarWritingBiased
d -> IO (Ptr ())
forall d. Typeable d => d -> IO (Ptr ())
getWindowsHandle
Handle__ -> IO ()
flushBuffer
withFileDescriptorReadingBiasedRaw :: Handle -> (CInt -> IO r) -> IO r
withFileDescriptorReadingBiasedRaw :: forall r. Handle -> (CInt -> IO r) -> IO r
withFileDescriptorReadingBiasedRaw
= String
-> (Handle -> MVar Handle__)
-> (forall d. Typeable d => d -> IO CInt)
-> (Handle__ -> IO ())
-> Handle
-> (CInt -> IO r)
-> IO r
forall a r.
String
-> (Handle -> MVar Handle__)
-> (forall d. Typeable d => d -> IO a)
-> (Handle__ -> IO ())
-> Handle
-> (a -> IO r)
-> IO r
withOSHandle String
"withFileDescriptorReadingBiasedRaw"
Handle -> MVar Handle__
handleStateVarReadingBiased
d -> IO CInt
forall d. Typeable d => d -> IO CInt
getFileDescriptor
(IO () -> Handle__ -> IO ()
forall a b. a -> b -> a
const (IO () -> Handle__ -> IO ()) -> IO () -> Handle__ -> IO ()
forall a b. (a -> b) -> a -> b
$ () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ())
withFileDescriptorWritingBiasedRaw :: Handle -> (CInt -> IO r) -> IO r
withFileDescriptorWritingBiasedRaw :: forall r. Handle -> (CInt -> IO r) -> IO r
withFileDescriptorWritingBiasedRaw
= String
-> (Handle -> MVar Handle__)
-> (forall d. Typeable d => d -> IO CInt)
-> (Handle__ -> IO ())
-> Handle
-> (CInt -> IO r)
-> IO r
forall a r.
String
-> (Handle -> MVar Handle__)
-> (forall d. Typeable d => d -> IO a)
-> (Handle__ -> IO ())
-> Handle
-> (a -> IO r)
-> IO r
withOSHandle String
"withFileDescriptorWritingBiasedRaw"
Handle -> MVar Handle__
handleStateVarWritingBiased
d -> IO CInt
forall d. Typeable d => d -> IO CInt
getFileDescriptor
(IO () -> Handle__ -> IO ()
forall a b. a -> b -> a
const (IO () -> Handle__ -> IO ()) -> IO () -> Handle__ -> IO ()
forall a b. (a -> b) -> a -> b
$ () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ())
withWindowsHandleReadingBiasedRaw :: Handle -> (Ptr () -> IO r) -> IO r
withWindowsHandleReadingBiasedRaw :: forall r. Handle -> (Ptr () -> IO r) -> IO r
withWindowsHandleReadingBiasedRaw
= String
-> (Handle -> MVar Handle__)
-> (forall d. Typeable d => d -> IO (Ptr ()))
-> (Handle__ -> IO ())
-> Handle
-> (Ptr () -> IO r)
-> IO r
forall a r.
String
-> (Handle -> MVar Handle__)
-> (forall d. Typeable d => d -> IO a)
-> (Handle__ -> IO ())
-> Handle
-> (a -> IO r)
-> IO r
withOSHandle String
"withWindowsHandleReadingBiasedRaw"
Handle -> MVar Handle__
handleStateVarReadingBiased
d -> IO (Ptr ())
forall d. Typeable d => d -> IO (Ptr ())
getWindowsHandle
(IO () -> Handle__ -> IO ()
forall a b. a -> b -> a
const (IO () -> Handle__ -> IO ()) -> IO () -> Handle__ -> IO ()
forall a b. (a -> b) -> a -> b
$ () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ())
withWindowsHandleWritingBiasedRaw :: Handle -> (Ptr () -> IO r) -> IO r
withWindowsHandleWritingBiasedRaw :: forall r. Handle -> (Ptr () -> IO r) -> IO r
withWindowsHandleWritingBiasedRaw
= String
-> (Handle -> MVar Handle__)
-> (forall d. Typeable d => d -> IO (Ptr ()))
-> (Handle__ -> IO ())
-> Handle
-> (Ptr () -> IO r)
-> IO r
forall a r.
String
-> (Handle -> MVar Handle__)
-> (forall d. Typeable d => d -> IO a)
-> (Handle__ -> IO ())
-> Handle
-> (a -> IO r)
-> IO r
withOSHandle String
"withWindowsHandleWritingBiasedRaw"
Handle -> MVar Handle__
handleStateVarWritingBiased
d -> IO (Ptr ())
forall d. Typeable d => d -> IO (Ptr ())
getWindowsHandle
(IO () -> Handle__ -> IO ()
forall a b. a -> b -> a
const (IO () -> Handle__ -> IO ()) -> IO () -> Handle__ -> IO ()
forall a b. (a -> b) -> a -> b
$ () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ())