{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE CPP
, NoImplicitPrelude
, RecordWildCards
, NondecreasingIndentation
#-}
{-# OPTIONS_GHC -Wno-unused-matches #-}
module GHC.IO.Handle (
Handle,
BufferMode(..),
mkFileHandle, mkDuplexHandle,
hFileSize, hSetFileSize, hIsEOF, isEOF, hLookAhead,
hSetBuffering, hSetBinaryMode, hSetEncoding, hGetEncoding,
hFlush, hFlushAll, hDuplicate, hDuplicateTo,
hClose, hClose_help,
LockMode(..), hLock, hTryLock,
HandlePosition, HandlePosn(..), hGetPosn, hSetPosn,
SeekMode(..), hSeek, hTell,
hIsOpen, hIsClosed, hIsReadable, hIsWritable, hGetBuffering, hIsSeekable,
hSetEcho, hGetEcho, hIsTerminalDevice,
hSetNewlineMode, Newline(..), NewlineMode(..), nativeNewline,
noNewlineTranslation, universalNewlineMode, nativeNewlineMode,
hShow,
hWaitForInput, hGetChar, hGetLine, hGetContents, hGetContents', hPutChar, hPutStr,
hGetBuf, hGetBufNonBlocking, hPutBuf, hPutBufNonBlocking
) where
import GHC.IO
import GHC.IO.Exception
import GHC.IO.Encoding
import GHC.IO.Buffer
import GHC.IO.BufferedIO ( BufferedIO )
import GHC.IO.Device as IODevice
import GHC.IO.StdHandles
import GHC.IO.SubSystem
import GHC.IO.Handle.Lock
import GHC.IO.Handle.Types
import GHC.IO.Handle.Internals
import GHC.IO.Handle.Text
import qualified GHC.IO.BufferedIO as Buffered
import GHC.Base
import GHC.Exception
import GHC.MVar
import GHC.IORef
import GHC.Show
import GHC.Num
import GHC.Real
import Data.Maybe
import Data.Typeable
hClose :: Handle -> IO ()
hClose :: Handle -> IO ()
hClose h :: Handle
h@(FileHandle FilePath
_ MVar Handle__
m) = do
Maybe SomeException
mb_exc <- Handle -> MVar Handle__ -> IO (Maybe SomeException)
hClose' Handle
h MVar Handle__
m
Maybe SomeException -> Handle -> IO ()
hClose_maybethrow Maybe SomeException
mb_exc Handle
h
hClose h :: Handle
h@(DuplexHandle FilePath
_ MVar Handle__
r MVar Handle__
w) = do
[Maybe SomeException]
excs <- (MVar Handle__ -> IO (Maybe SomeException))
-> [MVar Handle__] -> IO [Maybe SomeException]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (Handle -> MVar Handle__ -> IO (Maybe SomeException)
hClose' Handle
h) [MVar Handle__
r,MVar Handle__
w]
Maybe SomeException -> Handle -> IO ()
hClose_maybethrow ([SomeException] -> Maybe SomeException
forall a. [a] -> Maybe a
listToMaybe ([Maybe SomeException] -> [SomeException]
forall a. [Maybe a] -> [a]
catMaybes [Maybe SomeException]
excs)) Handle
h
hClose_maybethrow :: Maybe SomeException -> Handle -> IO ()
hClose_maybethrow :: Maybe SomeException -> Handle -> IO ()
hClose_maybethrow Maybe SomeException
Nothing Handle
h = () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
hClose_maybethrow (Just SomeException
e) Handle
h = SomeException -> Handle -> IO ()
hClose_rethrow SomeException
e Handle
h
hClose_rethrow :: SomeException -> Handle -> IO ()
hClose_rethrow :: SomeException -> Handle -> IO ()
hClose_rethrow SomeException
e Handle
h =
case SomeException -> Maybe IOException
forall e. Exception e => SomeException -> Maybe e
fromException SomeException
e of
Just IOException
ioe -> IOException -> IO ()
forall a. IOException -> IO a
ioError (IOException -> FilePath -> Handle -> IOException
augmentIOError IOException
ioe FilePath
"hClose" Handle
h)
Maybe IOException
Nothing -> SomeException -> IO ()
forall e a. Exception e => e -> IO a
throwIO SomeException
e
hClose' :: Handle -> MVar Handle__ -> IO (Maybe SomeException)
hClose' :: Handle -> MVar Handle__ -> IO (Maybe SomeException)
hClose' Handle
h MVar Handle__
m = FilePath
-> Handle
-> MVar Handle__
-> (Handle__ -> IO (Handle__, Maybe SomeException))
-> IO (Maybe SomeException)
forall a.
FilePath
-> Handle
-> MVar Handle__
-> (Handle__ -> IO (Handle__, a))
-> IO a
withHandle' FilePath
"hClose" Handle
h MVar Handle__
m ((Handle__ -> IO (Handle__, Maybe SomeException))
-> IO (Maybe SomeException))
-> (Handle__ -> IO (Handle__, Maybe SomeException))
-> IO (Maybe SomeException)
forall a b. (a -> b) -> a -> b
$ Handle__ -> IO (Handle__, Maybe SomeException)
hClose_help
hFileSize :: Handle -> IO Integer
hFileSize :: Handle -> IO Integer
hFileSize Handle
handle =
FilePath -> Handle -> (Handle__ -> IO Integer) -> IO Integer
forall a. FilePath -> Handle -> (Handle__ -> IO a) -> IO a
withHandle_ FilePath
"hFileSize" Handle
handle ((Handle__ -> IO Integer) -> IO Integer)
-> (Handle__ -> IO Integer) -> IO Integer
forall a b. (a -> b) -> a -> b
$ \ handle_ :: Handle__
handle_@Handle__{haDevice :: ()
haDevice=dev
dev} -> do
case Handle__ -> HandleType
haType Handle__
handle_ of
HandleType
ClosedHandle -> IO Integer
forall a. IO a
ioe_closedHandle
HandleType
SemiClosedHandle -> IO Integer
forall a. IO a
ioe_semiclosedHandle
HandleType
_ -> do Handle__ -> IO ()
flushWriteBuffer Handle__
handle_
Integer
r <- dev -> IO Integer
forall a. IODevice a => a -> IO Integer
IODevice.getSize dev
dev
FilePath -> IO ()
debugIO (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ FilePath
"hFileSize: " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Integer -> FilePath
forall a. Show a => a -> FilePath
show Integer
r FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
" " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Handle -> FilePath
forall a. Show a => a -> FilePath
show Handle
handle
if Integer
r Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
/= -Integer
1
then Integer -> IO Integer
forall (m :: * -> *) a. Monad m => a -> m a
return Integer
r
else IOException -> IO Integer
forall a. IOException -> IO a
ioException (Maybe Handle
-> IOErrorType
-> FilePath
-> FilePath
-> Maybe CInt
-> Maybe FilePath
-> IOException
IOError Maybe Handle
forall a. Maybe a
Nothing IOErrorType
InappropriateType FilePath
"hFileSize"
FilePath
"not a regular file" Maybe CInt
forall a. Maybe a
Nothing Maybe FilePath
forall a. Maybe a
Nothing)
hSetFileSize :: Handle -> Integer -> IO ()
hSetFileSize :: Handle -> Integer -> IO ()
hSetFileSize Handle
handle Integer
size =
FilePath -> Handle -> (Handle__ -> IO ()) -> IO ()
forall a. FilePath -> Handle -> (Handle__ -> IO a) -> IO a
withHandle_ FilePath
"hSetFileSize" Handle
handle ((Handle__ -> IO ()) -> IO ()) -> (Handle__ -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ handle_ :: Handle__
handle_@Handle__{haDevice :: ()
haDevice=dev
dev} -> do
case Handle__ -> HandleType
haType Handle__
handle_ of
HandleType
ClosedHandle -> IO ()
forall a. IO a
ioe_closedHandle
HandleType
SemiClosedHandle -> IO ()
forall a. IO a
ioe_semiclosedHandle
HandleType
_ -> do Handle__ -> IO ()
flushWriteBuffer Handle__
handle_
dev -> Integer -> IO ()
forall a. IODevice a => a -> Integer -> IO ()
IODevice.setSize dev
dev Integer
size
() -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
hIsEOF :: Handle -> IO Bool
hIsEOF :: Handle -> IO Bool
hIsEOF Handle
handle = FilePath -> Handle -> (Handle__ -> IO Bool) -> IO Bool
forall a. FilePath -> Handle -> (Handle__ -> IO a) -> IO a
wantReadableHandle_ FilePath
"hIsEOF" Handle
handle ((Handle__ -> IO Bool) -> IO Bool)
-> (Handle__ -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer CharBufElem)
IORef (Buffer Word8)
IORef (BufferList CharBufElem)
BufferMode
HandleType
Newline
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer CharBufElem)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList CharBufElem)
haBufferMode :: Handle__ -> BufferMode
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList CharBufElem)
haCharBuffer :: IORef (Buffer CharBufElem)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haType :: Handle__ -> HandleType
haDevice :: ()
..} -> do
Buffer CharBufElem
cbuf <- IORef (Buffer CharBufElem) -> IO (Buffer CharBufElem)
forall a. IORef a -> IO a
readIORef IORef (Buffer CharBufElem)
haCharBuffer
if Bool -> Bool
not (Buffer CharBufElem -> Bool
forall e. Buffer e -> Bool
isEmptyBuffer Buffer CharBufElem
cbuf) then Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False else do
Buffer Word8
bbuf <- IORef (Buffer Word8) -> IO (Buffer Word8)
forall a. IORef a -> IO a
readIORef IORef (Buffer Word8)
haByteBuffer
if Bool -> Bool
not (Buffer Word8 -> Bool
forall e. Buffer e -> Bool
isEmptyBuffer Buffer Word8
bbuf) then Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False else do
(Int
r,Buffer Word8
bbuf') <- dev -> Buffer Word8 -> IO (Int, Buffer Word8)
forall dev.
BufferedIO dev =>
dev -> Buffer Word8 -> IO (Int, Buffer Word8)
Buffered.fillReadBuffer dev
haDevice Buffer Word8
bbuf
if Int
r Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
then Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
else do IORef (Buffer Word8) -> Buffer Word8 -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef IORef (Buffer Word8)
haByteBuffer Buffer Word8
bbuf'
Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
isEOF :: IO Bool
isEOF :: IO Bool
isEOF = Handle -> IO Bool
hIsEOF Handle
stdin
hLookAhead :: Handle -> IO Char
hLookAhead :: Handle -> IO CharBufElem
hLookAhead Handle
handle =
FilePath
-> Handle -> (Handle__ -> IO CharBufElem) -> IO CharBufElem
forall a. FilePath -> Handle -> (Handle__ -> IO a) -> IO a
wantReadableHandle_ FilePath
"hLookAhead" Handle
handle Handle__ -> IO CharBufElem
hLookAhead_
hSetBuffering :: Handle -> BufferMode -> IO ()
hSetBuffering :: Handle -> BufferMode -> IO ()
hSetBuffering Handle
handle BufferMode
mode =
FilePath -> Handle -> (Handle__ -> IO Handle__) -> IO ()
withAllHandles__ FilePath
"hSetBuffering" Handle
handle ((Handle__ -> IO Handle__) -> IO ())
-> (Handle__ -> IO Handle__) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ handle_ :: Handle__
handle_@Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer CharBufElem)
IORef (Buffer Word8)
IORef (BufferList CharBufElem)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList CharBufElem)
haCharBuffer :: IORef (Buffer CharBufElem)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer CharBufElem)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList CharBufElem)
haBufferMode :: Handle__ -> BufferMode
haType :: Handle__ -> HandleType
haDevice :: ()
..} -> do
case HandleType
haType of
HandleType
ClosedHandle -> IO Handle__
forall a. IO a
ioe_closedHandle
HandleType
_ -> do
if BufferMode
mode BufferMode -> BufferMode -> Bool
forall a. Eq a => a -> a -> Bool
== BufferMode
haBufferMode then Handle__ -> IO Handle__
forall (m :: * -> *) a. Monad m => a -> m a
return Handle__
handle_ else do
case BufferMode
mode of
BlockBuffering (Just Int
n) | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 -> Int -> IO ()
forall a. Int -> IO a
ioe_bufsiz Int
n
BufferMode
_ -> () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Bool
is_tty <- dev -> IO Bool
forall a. IODevice a => a -> IO Bool
IODevice.isTerminal dev
haDevice
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool
is_tty Bool -> Bool -> Bool
&& HandleType -> Bool
isReadableHandleType HandleType
haType) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
case BufferMode
mode of
#if !defined(mingw32_HOST_OS)
BufferMode
NoBuffering -> dev -> Bool -> IO ()
forall a. IODevice a => a -> Bool -> IO ()
IODevice.setRaw dev
haDevice Bool
True
#else
NoBuffering -> return () <!> IODevice.setRaw haDevice True
#endif
BufferMode
_ -> dev -> Bool -> IO ()
forall a. IODevice a => a -> Bool -> IO ()
IODevice.setRaw dev
haDevice Bool
False
IORef (BufferList CharBufElem) -> BufferList CharBufElem -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef IORef (BufferList CharBufElem)
haBuffers BufferList CharBufElem
forall e. BufferList e
BufferListNil
Handle__ -> IO Handle__
forall (m :: * -> *) a. Monad m => a -> m a
return Handle__ :: forall dev enc_state dec_state.
(RawIO dev, IODevice dev, BufferedIO dev, Typeable dev) =>
dev
-> HandleType
-> IORef (Buffer Word8)
-> BufferMode
-> IORef (dec_state, Buffer Word8)
-> IORef (Buffer CharBufElem)
-> IORef (BufferList CharBufElem)
-> Maybe (TextEncoder enc_state)
-> Maybe (TextDecoder dec_state)
-> Maybe TextEncoding
-> Newline
-> Newline
-> Maybe (MVar Handle__)
-> Handle__
Handle__{ haBufferMode :: BufferMode
haBufferMode = BufferMode
mode,dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer CharBufElem)
IORef (Buffer Word8)
IORef (BufferList CharBufElem)
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList CharBufElem)
haCharBuffer :: IORef (Buffer CharBufElem)
haLastDecode :: IORef (dec_state, Buffer Word8)
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haOutputNL :: Newline
haOtherSide :: Maybe (MVar Handle__)
haLastDecode :: IORef (dec_state, Buffer Word8)
haInputNL :: Newline
haEncoder :: Maybe (TextEncoder enc_state)
haDecoder :: Maybe (TextDecoder dec_state)
haCodec :: Maybe TextEncoding
haCharBuffer :: IORef (Buffer CharBufElem)
haByteBuffer :: IORef (Buffer Word8)
haBuffers :: IORef (BufferList CharBufElem)
haType :: HandleType
haDevice :: dev
.. }
hSetEncoding :: Handle -> TextEncoding -> IO ()
hSetEncoding :: Handle -> TextEncoding -> IO ()
hSetEncoding Handle
hdl TextEncoding
encoding = do
FilePath -> Handle -> (Handle__ -> IO Handle__) -> IO ()
withAllHandles__ FilePath
"hSetEncoding" Handle
hdl ((Handle__ -> IO Handle__) -> IO ())
-> (Handle__ -> IO Handle__) -> IO ()
forall a b. (a -> b) -> a -> b
$ \h_ :: Handle__
h_@Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer CharBufElem)
IORef (Buffer Word8)
IORef (BufferList CharBufElem)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList CharBufElem)
haCharBuffer :: IORef (Buffer CharBufElem)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer CharBufElem)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList CharBufElem)
haBufferMode :: Handle__ -> BufferMode
haType :: Handle__ -> HandleType
haDevice :: ()
..} -> do
Handle__ -> IO ()
flushCharBuffer Handle__
h_
Handle__ -> IO ()
closeTextCodecs Handle__
h_
Maybe TextEncoding
-> HandleType
-> (forall es ds.
Maybe (TextEncoder es) -> Maybe (TextDecoder ds) -> IO Handle__)
-> IO Handle__
forall a.
Maybe TextEncoding
-> HandleType
-> (forall es ds.
Maybe (TextEncoder es) -> Maybe (TextDecoder ds) -> IO a)
-> IO a
openTextEncoding (TextEncoding -> Maybe TextEncoding
forall a. a -> Maybe a
Just TextEncoding
encoding) HandleType
haType ((forall es ds.
Maybe (TextEncoder es) -> Maybe (TextDecoder ds) -> IO Handle__)
-> IO Handle__)
-> (forall es ds.
Maybe (TextEncoder es) -> Maybe (TextDecoder ds) -> IO Handle__)
-> IO Handle__
forall a b. (a -> b) -> a -> b
$ \ Maybe (TextEncoder es)
mb_encoder Maybe (TextDecoder ds)
mb_decoder -> do
Buffer Word8
bbuf <- IORef (Buffer Word8) -> IO (Buffer Word8)
forall a. IORef a -> IO a
readIORef IORef (Buffer Word8)
haByteBuffer
IORef (ds, Buffer Word8)
ref <- (ds, Buffer Word8) -> IO (IORef (ds, Buffer Word8))
forall a. a -> IO (IORef a)
newIORef (FilePath -> (ds, Buffer Word8)
forall a. FilePath -> a
errorWithoutStackTrace FilePath
"last_decode")
Handle__ -> IO Handle__
forall (m :: * -> *) a. Monad m => a -> m a
return (Handle__ :: forall dev enc_state dec_state.
(RawIO dev, IODevice dev, BufferedIO dev, Typeable dev) =>
dev
-> HandleType
-> IORef (Buffer Word8)
-> BufferMode
-> IORef (dec_state, Buffer Word8)
-> IORef (Buffer CharBufElem)
-> IORef (BufferList CharBufElem)
-> Maybe (TextEncoder enc_state)
-> Maybe (TextDecoder dec_state)
-> Maybe TextEncoding
-> Newline
-> Newline
-> Maybe (MVar Handle__)
-> Handle__
Handle__{ haLastDecode :: IORef (ds, Buffer Word8)
haLastDecode = IORef (ds, Buffer Word8)
ref,
haDecoder :: Maybe (TextDecoder ds)
haDecoder = Maybe (TextDecoder ds)
mb_decoder,
haEncoder :: Maybe (TextEncoder es)
haEncoder = Maybe (TextEncoder es)
mb_encoder,
haCodec :: Maybe TextEncoding
haCodec = TextEncoding -> Maybe TextEncoding
forall a. a -> Maybe a
Just TextEncoding
encoding, dev
Maybe (MVar Handle__)
IORef (Buffer CharBufElem)
IORef (Buffer Word8)
IORef (BufferList CharBufElem)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haBuffers :: IORef (BufferList CharBufElem)
haCharBuffer :: IORef (Buffer CharBufElem)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haOutputNL :: Newline
haOtherSide :: Maybe (MVar Handle__)
haInputNL :: Newline
haCharBuffer :: IORef (Buffer CharBufElem)
haByteBuffer :: IORef (Buffer Word8)
haBuffers :: IORef (BufferList CharBufElem)
haBufferMode :: BufferMode
haType :: HandleType
haDevice :: dev
.. })
hGetEncoding :: Handle -> IO (Maybe TextEncoding)
hGetEncoding :: Handle -> IO (Maybe TextEncoding)
hGetEncoding Handle
hdl =
FilePath
-> Handle
-> (Handle__ -> IO (Maybe TextEncoding))
-> IO (Maybe TextEncoding)
forall a. FilePath -> Handle -> (Handle__ -> IO a) -> IO a
withHandle_ FilePath
"hGetEncoding" Handle
hdl ((Handle__ -> IO (Maybe TextEncoding)) -> IO (Maybe TextEncoding))
-> (Handle__ -> IO (Maybe TextEncoding)) -> IO (Maybe TextEncoding)
forall a b. (a -> b) -> a -> b
$ \h_ :: Handle__
h_@Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer CharBufElem)
IORef (Buffer Word8)
IORef (BufferList CharBufElem)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList CharBufElem)
haCharBuffer :: IORef (Buffer CharBufElem)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer CharBufElem)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList CharBufElem)
haBufferMode :: Handle__ -> BufferMode
haType :: Handle__ -> HandleType
haDevice :: ()
..} -> Maybe TextEncoding -> IO (Maybe TextEncoding)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe TextEncoding
haCodec
hFlush :: Handle -> IO ()
hFlush :: Handle -> IO ()
hFlush Handle
handle = FilePath -> Handle -> (Handle__ -> IO ()) -> IO ()
forall a. FilePath -> Handle -> (Handle__ -> IO a) -> IO a
wantWritableHandle FilePath
"hFlush" Handle
handle Handle__ -> IO ()
flushWriteBuffer
hFlushAll :: Handle -> IO ()
hFlushAll :: Handle -> IO ()
hFlushAll Handle
handle = FilePath -> Handle -> (Handle__ -> IO ()) -> IO ()
forall a. FilePath -> Handle -> (Handle__ -> IO a) -> IO a
withHandle_ FilePath
"hFlushAll" Handle
handle Handle__ -> IO ()
flushBuffer
data HandlePosn = HandlePosn Handle HandlePosition
instance Eq HandlePosn where
(HandlePosn Handle
h1 Integer
p1) == :: HandlePosn -> HandlePosn -> Bool
== (HandlePosn Handle
h2 Integer
p2) = Integer
p1Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
==Integer
p2 Bool -> Bool -> Bool
&& Handle
h1Handle -> Handle -> Bool
forall a. Eq a => a -> a -> Bool
==Handle
h2
instance Show HandlePosn where
showsPrec :: Int -> HandlePosn -> FilePath -> FilePath
showsPrec Int
p (HandlePosn Handle
h Integer
pos) =
Int -> Handle -> FilePath -> FilePath
forall a. Show a => Int -> a -> FilePath -> FilePath
showsPrec Int
p Handle
h (FilePath -> FilePath)
-> (FilePath -> FilePath) -> FilePath -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> FilePath -> FilePath
showString FilePath
" at position " (FilePath -> FilePath)
-> (FilePath -> FilePath) -> FilePath -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> FilePath -> FilePath
forall a. Show a => a -> FilePath -> FilePath
shows Integer
pos
type HandlePosition = Integer
hGetPosn :: Handle -> IO HandlePosn
hGetPosn :: Handle -> IO HandlePosn
hGetPosn Handle
handle = do
Integer
posn <- Handle -> IO Integer
hTell Handle
handle
HandlePosn -> IO HandlePosn
forall (m :: * -> *) a. Monad m => a -> m a
return (Handle -> Integer -> HandlePosn
HandlePosn Handle
handle Integer
posn)
hSetPosn :: HandlePosn -> IO ()
hSetPosn :: HandlePosn -> IO ()
hSetPosn (HandlePosn Handle
h Integer
i) = Handle -> SeekMode -> Integer -> IO ()
hSeek Handle
h SeekMode
AbsoluteSeek Integer
i
hSeek :: Handle -> SeekMode -> Integer -> IO ()
hSeek :: Handle -> SeekMode -> Integer -> IO ()
hSeek Handle
handle SeekMode
mode Integer
offset =
FilePath -> Handle -> (Handle__ -> IO ()) -> IO ()
forall a. FilePath -> Handle -> (Handle__ -> IO a) -> IO a
wantSeekableHandle FilePath
"hSeek" Handle
handle ((Handle__ -> IO ()) -> IO ()) -> (Handle__ -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ handle_ :: Handle__
handle_@Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer CharBufElem)
IORef (Buffer Word8)
IORef (BufferList CharBufElem)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList CharBufElem)
haCharBuffer :: IORef (Buffer CharBufElem)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer CharBufElem)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList CharBufElem)
haBufferMode :: Handle__ -> BufferMode
haType :: Handle__ -> HandleType
haDevice :: ()
..} -> do
FilePath -> IO ()
debugIO (FilePath
"hSeek " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ (SeekMode, Integer) -> FilePath
forall a. Show a => a -> FilePath
show (SeekMode
mode,Integer
offset))
Buffer CharBufElem
cbuf <- IORef (Buffer CharBufElem) -> IO (Buffer CharBufElem)
forall a. IORef a -> IO a
readIORef IORef (Buffer CharBufElem)
haCharBuffer
Buffer Word8
bbuf <- IORef (Buffer Word8) -> IO (Buffer Word8)
forall a. IORef a -> IO a
readIORef IORef (Buffer Word8)
haByteBuffer
FilePath -> IO ()
debugIO (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ FilePath
"hSeek - bbuf:" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Buffer Word8 -> FilePath
forall a. Buffer a -> FilePath
summaryBuffer Buffer Word8
bbuf
FilePath -> IO ()
debugIO (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ FilePath
"hSeek - cbuf:" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Buffer CharBufElem -> FilePath
forall a. Buffer a -> FilePath
summaryBuffer Buffer CharBufElem
cbuf
if Buffer CharBufElem -> Bool
forall e. Buffer e -> Bool
isWriteBuffer Buffer CharBufElem
cbuf
then do Handle__ -> IO ()
flushWriteBuffer Handle__
handle_
Integer
new_offset <- dev -> SeekMode -> Integer -> IO Integer
forall a. IODevice a => a -> SeekMode -> Integer -> IO Integer
IODevice.seek dev
haDevice SeekMode
mode Integer
offset
Buffer Word8
bbuf1 <- IORef (Buffer Word8) -> IO (Buffer Word8)
forall a. IORef a -> IO a
readIORef IORef (Buffer Word8)
haByteBuffer
let bbuf2 :: Buffer Word8
bbuf2 = Buffer Word8
bbuf1{ bufOffset :: Word64
bufOffset = Integer -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
new_offset }
FilePath -> IO ()
debugIO (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ FilePath
"hSeek - seek:: " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Integer -> FilePath
forall a. Show a => a -> FilePath
show Integer
offset FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++
FilePath
" - " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Integer -> FilePath
forall a. Show a => a -> FilePath
show Integer
new_offset
FilePath -> IO ()
debugIO (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ FilePath
"hSeek - wr flush bbuf1:" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Buffer Word8 -> FilePath
forall a. Buffer a -> FilePath
summaryBuffer Buffer Word8
bbuf2
IORef (Buffer Word8) -> Buffer Word8 -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef IORef (Buffer Word8)
haByteBuffer Buffer Word8
bbuf2
else do
let r :: Int
r = Buffer CharBufElem -> Int
forall e. Buffer e -> Int
bufL Buffer CharBufElem
cbuf; w :: Int
w = Buffer CharBufElem -> Int
forall e. Buffer e -> Int
bufR Buffer CharBufElem
cbuf
if SeekMode
mode SeekMode -> SeekMode -> Bool
forall a. Eq a => a -> a -> Bool
== SeekMode
RelativeSeek Bool -> Bool -> Bool
&& Maybe (TextDecoder dec_state) -> Bool
forall a. Maybe a -> Bool
isNothing Maybe (TextDecoder dec_state)
haDecoder Bool -> Bool -> Bool
&&
Integer
offset Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
0 Bool -> Bool -> Bool
&& Integer
offset Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
w Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
r)
then IORef (Buffer CharBufElem) -> Buffer CharBufElem -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef IORef (Buffer CharBufElem)
haCharBuffer Buffer CharBufElem
cbuf{ bufL :: Int
bufL = Int
r Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
offset }
else do
Handle__ -> IO ()
flushCharReadBuffer Handle__
handle_
Handle__ -> IO ()
flushByteReadBuffer Handle__
handle_
Buffer Word8
bbuf2 <- IORef (Buffer Word8) -> IO (Buffer Word8)
forall a. IORef a -> IO a
readIORef IORef (Buffer Word8)
haByteBuffer
Integer
new_offset <- dev -> SeekMode -> Integer -> IO Integer
forall a. IODevice a => a -> SeekMode -> Integer -> IO Integer
IODevice.seek dev
haDevice SeekMode
mode Integer
offset
FilePath -> IO ()
debugIO (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ FilePath
"hSeek after: " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Integer -> FilePath
forall a. Show a => a -> FilePath
show Integer
new_offset
IORef (Buffer Word8) -> Buffer Word8 -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef IORef (Buffer Word8)
haByteBuffer Buffer Word8
bbuf2{ bufOffset :: Word64
bufOffset = Integer -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
new_offset }
hTell :: Handle -> IO Integer
hTell :: Handle -> IO Integer
hTell Handle
handle =
FilePath -> Handle -> (Handle__ -> IO Integer) -> IO Integer
forall a. FilePath -> Handle -> (Handle__ -> IO a) -> IO a
wantSeekableHandle FilePath
"hGetPosn" Handle
handle ((Handle__ -> IO Integer) -> IO Integer)
-> (Handle__ -> IO Integer) -> IO Integer
forall a b. (a -> b) -> a -> b
$ \ handle_ :: Handle__
handle_@Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer CharBufElem)
IORef (Buffer Word8)
IORef (BufferList CharBufElem)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList CharBufElem)
haCharBuffer :: IORef (Buffer CharBufElem)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer CharBufElem)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList CharBufElem)
haBufferMode :: Handle__ -> BufferMode
haType :: Handle__ -> HandleType
haDevice :: ()
..} -> do
Integer
posn <- if IoSubSystem
ioSubSystem IoSubSystem -> IoSubSystem -> Bool
forall a. Eq a => a -> a -> Bool
== IoSubSystem
IoNative
then (Word64 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Integer)
-> (Buffer Word8 -> Word64) -> Buffer Word8 -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Buffer Word8 -> Word64
forall e. Buffer e -> Word64
bufOffset) (Buffer Word8 -> Integer) -> IO (Buffer Word8) -> IO Integer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` IORef (Buffer Word8) -> IO (Buffer Word8)
forall a. IORef a -> IO a
readIORef IORef (Buffer Word8)
haByteBuffer
else dev -> IO Integer
forall a. IODevice a => a -> IO Integer
IODevice.tell dev
haDevice
Handle__ -> IO ()
flushCharBuffer Handle__
handle_
Buffer Word8
bbuf <- IORef (Buffer Word8) -> IO (Buffer Word8)
forall a. IORef a -> IO a
readIORef IORef (Buffer Word8)
haByteBuffer
FilePath -> IO ()
debugIO (FilePath
"hTell bbuf (elems=" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Int -> FilePath
forall a. Show a => a -> FilePath
show (Buffer Word8 -> Int
forall e. Buffer e -> Int
bufferElems Buffer Word8
bbuf) FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
")"
FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Buffer Word8 -> FilePath
forall a. Buffer a -> FilePath
summaryBuffer Buffer Word8
bbuf)
let real_posn :: Integer
real_posn
| Buffer Word8 -> Bool
forall e. Buffer e -> Bool
isWriteBuffer Buffer Word8
bbuf = Integer
posn Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Buffer Word8 -> Int
forall e. Buffer e -> Int
bufferElems Buffer Word8
bbuf)
| Bool
otherwise = Integer
posn Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Buffer Word8 -> Int
forall e. Buffer e -> Int
bufferElems Buffer Word8
bbuf)
Buffer CharBufElem
cbuf <- IORef (Buffer CharBufElem) -> IO (Buffer CharBufElem)
forall a. IORef a -> IO a
readIORef IORef (Buffer CharBufElem)
haCharBuffer
FilePath -> IO ()
debugIO (FilePath
"\nhGetPosn: (posn, real_posn) = " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ (Integer, Integer) -> FilePath
forall a. Show a => a -> FilePath
show (Integer
posn, Integer
real_posn))
FilePath -> IO ()
debugIO (FilePath
" cbuf: " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Buffer CharBufElem -> FilePath
forall a. Buffer a -> FilePath
summaryBuffer Buffer CharBufElem
cbuf FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++
FilePath
" bbuf: " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Buffer Word8 -> FilePath
forall a. Buffer a -> FilePath
summaryBuffer Buffer Word8
bbuf)
Integer -> IO Integer
forall (m :: * -> *) a. Monad m => a -> m a
return Integer
real_posn
hIsOpen :: Handle -> IO Bool
hIsOpen :: Handle -> IO Bool
hIsOpen Handle
handle =
FilePath -> Handle -> (Handle__ -> IO Bool) -> IO Bool
forall a. FilePath -> Handle -> (Handle__ -> IO a) -> IO a
withHandle_ FilePath
"hIsOpen" Handle
handle ((Handle__ -> IO Bool) -> IO Bool)
-> (Handle__ -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \ Handle__
handle_ -> do
case Handle__ -> HandleType
haType Handle__
handle_ of
HandleType
ClosedHandle -> Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
HandleType
SemiClosedHandle -> Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
HandleType
_ -> Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
hIsClosed :: Handle -> IO Bool
hIsClosed :: Handle -> IO Bool
hIsClosed Handle
handle =
FilePath -> Handle -> (Handle__ -> IO Bool) -> IO Bool
forall a. FilePath -> Handle -> (Handle__ -> IO a) -> IO a
withHandle_ FilePath
"hIsClosed" Handle
handle ((Handle__ -> IO Bool) -> IO Bool)
-> (Handle__ -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \ Handle__
handle_ -> do
case Handle__ -> HandleType
haType Handle__
handle_ of
HandleType
ClosedHandle -> Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
HandleType
_ -> Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
hIsReadable :: Handle -> IO Bool
hIsReadable :: Handle -> IO Bool
hIsReadable (DuplexHandle FilePath
_ MVar Handle__
_ MVar Handle__
_) = Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
hIsReadable Handle
handle =
FilePath -> Handle -> (Handle__ -> IO Bool) -> IO Bool
forall a. FilePath -> Handle -> (Handle__ -> IO a) -> IO a
withHandle_ FilePath
"hIsReadable" Handle
handle ((Handle__ -> IO Bool) -> IO Bool)
-> (Handle__ -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \ Handle__
handle_ -> do
case Handle__ -> HandleType
haType Handle__
handle_ of
HandleType
ClosedHandle -> IO Bool
forall a. IO a
ioe_closedHandle
HandleType
SemiClosedHandle -> IO Bool
forall a. IO a
ioe_semiclosedHandle
HandleType
htype -> Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (HandleType -> Bool
isReadableHandleType HandleType
htype)
hIsWritable :: Handle -> IO Bool
hIsWritable :: Handle -> IO Bool
hIsWritable (DuplexHandle FilePath
_ MVar Handle__
_ MVar Handle__
_) = Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
hIsWritable Handle
handle =
FilePath -> Handle -> (Handle__ -> IO Bool) -> IO Bool
forall a. FilePath -> Handle -> (Handle__ -> IO a) -> IO a
withHandle_ FilePath
"hIsWritable" Handle
handle ((Handle__ -> IO Bool) -> IO Bool)
-> (Handle__ -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \ Handle__
handle_ -> do
case Handle__ -> HandleType
haType Handle__
handle_ of
HandleType
ClosedHandle -> IO Bool
forall a. IO a
ioe_closedHandle
HandleType
SemiClosedHandle -> IO Bool
forall a. IO a
ioe_semiclosedHandle
HandleType
htype -> Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (HandleType -> Bool
isWritableHandleType HandleType
htype)
hGetBuffering :: Handle -> IO BufferMode
hGetBuffering :: Handle -> IO BufferMode
hGetBuffering Handle
handle =
FilePath -> Handle -> (Handle__ -> IO BufferMode) -> IO BufferMode
forall a. FilePath -> Handle -> (Handle__ -> IO a) -> IO a
withHandle_ FilePath
"hGetBuffering" Handle
handle ((Handle__ -> IO BufferMode) -> IO BufferMode)
-> (Handle__ -> IO BufferMode) -> IO BufferMode
forall a b. (a -> b) -> a -> b
$ \ Handle__
handle_ -> do
case Handle__ -> HandleType
haType Handle__
handle_ of
HandleType
ClosedHandle -> IO BufferMode
forall a. IO a
ioe_closedHandle
HandleType
_ ->
BufferMode -> IO BufferMode
forall (m :: * -> *) a. Monad m => a -> m a
return (Handle__ -> BufferMode
haBufferMode Handle__
handle_)
hIsSeekable :: Handle -> IO Bool
hIsSeekable :: Handle -> IO Bool
hIsSeekable Handle
handle =
FilePath -> Handle -> (Handle__ -> IO Bool) -> IO Bool
forall a. FilePath -> Handle -> (Handle__ -> IO a) -> IO a
withHandle_ FilePath
"hIsSeekable" Handle
handle ((Handle__ -> IO Bool) -> IO Bool)
-> (Handle__ -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \ handle_ :: Handle__
handle_@Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer CharBufElem)
IORef (Buffer Word8)
IORef (BufferList CharBufElem)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList CharBufElem)
haCharBuffer :: IORef (Buffer CharBufElem)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer CharBufElem)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList CharBufElem)
haBufferMode :: Handle__ -> BufferMode
haType :: Handle__ -> HandleType
haDevice :: ()
..} -> do
case HandleType
haType of
HandleType
ClosedHandle -> IO Bool
forall a. IO a
ioe_closedHandle
HandleType
SemiClosedHandle -> IO Bool
forall a. IO a
ioe_semiclosedHandle
HandleType
AppendHandle -> Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
HandleType
_ -> dev -> IO Bool
forall a. IODevice a => a -> IO Bool
IODevice.isSeekable dev
haDevice
hSetEcho :: Handle -> Bool -> IO ()
hSetEcho :: Handle -> Bool -> IO ()
hSetEcho Handle
handle Bool
on = do
Bool
isT <- Handle -> IO Bool
hIsTerminalDevice Handle
handle
if Bool -> Bool
not Bool
isT
then () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
else
FilePath -> Handle -> (Handle__ -> IO ()) -> IO ()
forall a. FilePath -> Handle -> (Handle__ -> IO a) -> IO a
withHandle_ FilePath
"hSetEcho" Handle
handle ((Handle__ -> IO ()) -> IO ()) -> (Handle__ -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer CharBufElem)
IORef (Buffer Word8)
IORef (BufferList CharBufElem)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList CharBufElem)
haCharBuffer :: IORef (Buffer CharBufElem)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer CharBufElem)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList CharBufElem)
haBufferMode :: Handle__ -> BufferMode
haType :: Handle__ -> HandleType
haDevice :: ()
..} -> do
case HandleType
haType of
HandleType
ClosedHandle -> IO ()
forall a. IO a
ioe_closedHandle
HandleType
_ -> dev -> Bool -> IO ()
forall a. IODevice a => a -> Bool -> IO ()
IODevice.setEcho dev
haDevice Bool
on
hGetEcho :: Handle -> IO Bool
hGetEcho :: Handle -> IO Bool
hGetEcho Handle
handle = do
Bool
isT <- Handle -> IO Bool
hIsTerminalDevice Handle
handle
if Bool -> Bool
not Bool
isT
then Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
else
FilePath -> Handle -> (Handle__ -> IO Bool) -> IO Bool
forall a. FilePath -> Handle -> (Handle__ -> IO a) -> IO a
withHandle_ FilePath
"hGetEcho" Handle
handle ((Handle__ -> IO Bool) -> IO Bool)
-> (Handle__ -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \ Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer CharBufElem)
IORef (Buffer Word8)
IORef (BufferList CharBufElem)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList CharBufElem)
haCharBuffer :: IORef (Buffer CharBufElem)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer CharBufElem)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList CharBufElem)
haBufferMode :: Handle__ -> BufferMode
haType :: Handle__ -> HandleType
haDevice :: ()
..} -> do
case HandleType
haType of
HandleType
ClosedHandle -> IO Bool
forall a. IO a
ioe_closedHandle
HandleType
_ -> dev -> IO Bool
forall a. IODevice a => a -> IO Bool
IODevice.getEcho dev
haDevice
hIsTerminalDevice :: Handle -> IO Bool
hIsTerminalDevice :: Handle -> IO Bool
hIsTerminalDevice Handle
handle = do
FilePath -> Handle -> (Handle__ -> IO Bool) -> IO Bool
forall a. FilePath -> Handle -> (Handle__ -> IO a) -> IO a
withHandle_ FilePath
"hIsTerminalDevice" Handle
handle ((Handle__ -> IO Bool) -> IO Bool)
-> (Handle__ -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \ Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer CharBufElem)
IORef (Buffer Word8)
IORef (BufferList CharBufElem)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList CharBufElem)
haCharBuffer :: IORef (Buffer CharBufElem)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer CharBufElem)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList CharBufElem)
haBufferMode :: Handle__ -> BufferMode
haType :: Handle__ -> HandleType
haDevice :: ()
..} -> do
case HandleType
haType of
HandleType
ClosedHandle -> IO Bool
forall a. IO a
ioe_closedHandle
HandleType
_ -> dev -> IO Bool
forall a. IODevice a => a -> IO Bool
IODevice.isTerminal dev
haDevice
hSetBinaryMode :: Handle -> Bool -> IO ()
hSetBinaryMode :: Handle -> Bool -> IO ()
hSetBinaryMode Handle
handle Bool
bin =
FilePath -> Handle -> (Handle__ -> IO Handle__) -> IO ()
withAllHandles__ FilePath
"hSetBinaryMode" Handle
handle ((Handle__ -> IO Handle__) -> IO ())
-> (Handle__ -> IO Handle__) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ h_ :: Handle__
h_@Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer CharBufElem)
IORef (Buffer Word8)
IORef (BufferList CharBufElem)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList CharBufElem)
haCharBuffer :: IORef (Buffer CharBufElem)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer CharBufElem)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList CharBufElem)
haBufferMode :: Handle__ -> BufferMode
haType :: Handle__ -> HandleType
haDevice :: ()
..} ->
do
Handle__ -> IO ()
flushCharBuffer Handle__
h_
Handle__ -> IO ()
closeTextCodecs Handle__
h_
Maybe TextEncoding
mb_te <- if Bool
bin then Maybe TextEncoding -> IO (Maybe TextEncoding)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe TextEncoding
forall a. Maybe a
Nothing
else (TextEncoding -> Maybe TextEncoding)
-> IO TextEncoding -> IO (Maybe TextEncoding)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap TextEncoding -> Maybe TextEncoding
forall a. a -> Maybe a
Just IO TextEncoding
getLocaleEncoding
Maybe TextEncoding
-> HandleType
-> (forall es ds.
Maybe (TextEncoder es) -> Maybe (TextDecoder ds) -> IO Handle__)
-> IO Handle__
forall a.
Maybe TextEncoding
-> HandleType
-> (forall es ds.
Maybe (TextEncoder es) -> Maybe (TextDecoder ds) -> IO a)
-> IO a
openTextEncoding Maybe TextEncoding
mb_te HandleType
haType ((forall es ds.
Maybe (TextEncoder es) -> Maybe (TextDecoder ds) -> IO Handle__)
-> IO Handle__)
-> (forall es ds.
Maybe (TextEncoder es) -> Maybe (TextDecoder ds) -> IO Handle__)
-> IO Handle__
forall a b. (a -> b) -> a -> b
$ \ Maybe (TextEncoder es)
mb_encoder Maybe (TextDecoder ds)
mb_decoder -> do
let nl :: NewlineMode
nl | Bool
bin = NewlineMode
noNewlineTranslation
| Bool
otherwise = NewlineMode
nativeNewlineMode
Buffer Word8
bbuf <- IORef (Buffer Word8) -> IO (Buffer Word8)
forall a. IORef a -> IO a
readIORef IORef (Buffer Word8)
haByteBuffer
IORef (ds, Buffer Word8)
ref <- (ds, Buffer Word8) -> IO (IORef (ds, Buffer Word8))
forall a. a -> IO (IORef a)
newIORef (FilePath -> ds
forall a. FilePath -> a
errorWithoutStackTrace FilePath
"codec_state", Buffer Word8
bbuf)
Handle__ -> IO Handle__
forall (m :: * -> *) a. Monad m => a -> m a
return Handle__ :: forall dev enc_state dec_state.
(RawIO dev, IODevice dev, BufferedIO dev, Typeable dev) =>
dev
-> HandleType
-> IORef (Buffer Word8)
-> BufferMode
-> IORef (dec_state, Buffer Word8)
-> IORef (Buffer CharBufElem)
-> IORef (BufferList CharBufElem)
-> Maybe (TextEncoder enc_state)
-> Maybe (TextDecoder dec_state)
-> Maybe TextEncoding
-> Newline
-> Newline
-> Maybe (MVar Handle__)
-> Handle__
Handle__{ haLastDecode :: IORef (ds, Buffer Word8)
haLastDecode = IORef (ds, Buffer Word8)
ref,
haEncoder :: Maybe (TextEncoder es)
haEncoder = Maybe (TextEncoder es)
mb_encoder,
haDecoder :: Maybe (TextDecoder ds)
haDecoder = Maybe (TextDecoder ds)
mb_decoder,
haCodec :: Maybe TextEncoding
haCodec = Maybe TextEncoding
mb_te,
haInputNL :: Newline
haInputNL = NewlineMode -> Newline
inputNL NewlineMode
nl,
haOutputNL :: Newline
haOutputNL = NewlineMode -> Newline
outputNL NewlineMode
nl, dev
Maybe (MVar Handle__)
IORef (Buffer CharBufElem)
IORef (Buffer Word8)
IORef (BufferList CharBufElem)
BufferMode
HandleType
haOtherSide :: Maybe (MVar Handle__)
haBuffers :: IORef (BufferList CharBufElem)
haCharBuffer :: IORef (Buffer CharBufElem)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haOtherSide :: Maybe (MVar Handle__)
haCharBuffer :: IORef (Buffer CharBufElem)
haByteBuffer :: IORef (Buffer Word8)
haBuffers :: IORef (BufferList CharBufElem)
haBufferMode :: BufferMode
haType :: HandleType
haDevice :: dev
.. }
hSetNewlineMode :: Handle -> NewlineMode -> IO ()
hSetNewlineMode :: Handle -> NewlineMode -> IO ()
hSetNewlineMode Handle
handle NewlineMode{ inputNL :: NewlineMode -> Newline
inputNL=Newline
i, outputNL :: NewlineMode -> Newline
outputNL=Newline
o } =
FilePath -> Handle -> (Handle__ -> IO Handle__) -> IO ()
withAllHandles__ FilePath
"hSetNewlineMode" Handle
handle ((Handle__ -> IO Handle__) -> IO ())
-> (Handle__ -> IO Handle__) -> IO ()
forall a b. (a -> b) -> a -> b
$ \h_ :: Handle__
h_@Handle__{} ->
do
Handle__ -> IO ()
flushBuffer Handle__
h_
Handle__ -> IO Handle__
forall (m :: * -> *) a. Monad m => a -> m a
return Handle__
h_{ haInputNL :: Newline
haInputNL=Newline
i, haOutputNL :: Newline
haOutputNL=Newline
o }
hDuplicate :: Handle -> IO Handle
hDuplicate :: Handle -> IO Handle
hDuplicate h :: Handle
h@(FileHandle FilePath
path MVar Handle__
m) = do
FilePath
-> Handle -> MVar Handle__ -> (Handle__ -> IO Handle) -> IO Handle
forall a.
FilePath -> Handle -> MVar Handle__ -> (Handle__ -> IO a) -> IO a
withHandle_' FilePath
"hDuplicate" Handle
h MVar Handle__
m ((Handle__ -> IO Handle) -> IO Handle)
-> (Handle__ -> IO Handle) -> IO Handle
forall a b. (a -> b) -> a -> b
$ \Handle__
h_ ->
FilePath
-> Handle
-> Maybe (MVar Handle__)
-> Handle__
-> Maybe HandleFinalizer
-> IO Handle
dupHandle FilePath
path Handle
h Maybe (MVar Handle__)
forall a. Maybe a
Nothing Handle__
h_ (HandleFinalizer -> Maybe HandleFinalizer
forall a. a -> Maybe a
Just HandleFinalizer
handleFinalizer)
hDuplicate h :: Handle
h@(DuplexHandle FilePath
path MVar Handle__
r MVar Handle__
w) = do
write_side :: Handle
write_side@(FileHandle FilePath
_ MVar Handle__
write_m) <-
FilePath
-> Handle -> MVar Handle__ -> (Handle__ -> IO Handle) -> IO Handle
forall a.
FilePath -> Handle -> MVar Handle__ -> (Handle__ -> IO a) -> IO a
withHandle_' FilePath
"hDuplicate" Handle
h MVar Handle__
w ((Handle__ -> IO Handle) -> IO Handle)
-> (Handle__ -> IO Handle) -> IO Handle
forall a b. (a -> b) -> a -> b
$ \Handle__
h_ ->
FilePath
-> Handle
-> Maybe (MVar Handle__)
-> Handle__
-> Maybe HandleFinalizer
-> IO Handle
dupHandle FilePath
path Handle
h Maybe (MVar Handle__)
forall a. Maybe a
Nothing Handle__
h_ (HandleFinalizer -> Maybe HandleFinalizer
forall a. a -> Maybe a
Just HandleFinalizer
handleFinalizer)
read_side :: Handle
read_side@(FileHandle FilePath
_ MVar Handle__
read_m) <-
FilePath
-> Handle -> MVar Handle__ -> (Handle__ -> IO Handle) -> IO Handle
forall a.
FilePath -> Handle -> MVar Handle__ -> (Handle__ -> IO a) -> IO a
withHandle_' FilePath
"hDuplicate" Handle
h MVar Handle__
r ((Handle__ -> IO Handle) -> IO Handle)
-> (Handle__ -> IO Handle) -> IO Handle
forall a b. (a -> b) -> a -> b
$ \Handle__
h_ ->
FilePath
-> Handle
-> Maybe (MVar Handle__)
-> Handle__
-> Maybe HandleFinalizer
-> IO Handle
dupHandle FilePath
path Handle
h (MVar Handle__ -> Maybe (MVar Handle__)
forall a. a -> Maybe a
Just MVar Handle__
write_m) Handle__
h_ Maybe HandleFinalizer
forall a. Maybe a
Nothing
Handle -> IO Handle
forall (m :: * -> *) a. Monad m => a -> m a
return (FilePath -> MVar Handle__ -> MVar Handle__ -> Handle
DuplexHandle FilePath
path MVar Handle__
read_m MVar Handle__
write_m)
dupHandle :: FilePath
-> Handle
-> Maybe (MVar Handle__)
-> Handle__
-> Maybe HandleFinalizer
-> IO Handle
dupHandle :: FilePath
-> Handle
-> Maybe (MVar Handle__)
-> Handle__
-> Maybe HandleFinalizer
-> IO Handle
dupHandle FilePath
filepath Handle
h Maybe (MVar Handle__)
other_side h_ :: Handle__
h_@Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer CharBufElem)
IORef (Buffer Word8)
IORef (BufferList CharBufElem)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList CharBufElem)
haCharBuffer :: IORef (Buffer CharBufElem)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer CharBufElem)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList CharBufElem)
haBufferMode :: Handle__ -> BufferMode
haType :: Handle__ -> HandleType
haDevice :: ()
..} Maybe HandleFinalizer
mb_finalizer = do
Handle__ -> IO ()
flushBuffer Handle__
h_
case Maybe (MVar Handle__)
other_side of
Maybe (MVar Handle__)
Nothing -> do
dev
new_dev <- dev -> IO dev
forall a. IODevice a => a -> IO a
IODevice.dup dev
haDevice
dev
-> FilePath
-> Maybe (MVar Handle__)
-> Handle__
-> Maybe HandleFinalizer
-> IO Handle
forall dev.
(RawIO dev, IODevice dev, BufferedIO dev, Typeable dev) =>
dev
-> FilePath
-> Maybe (MVar Handle__)
-> Handle__
-> Maybe HandleFinalizer
-> IO Handle
dupHandle_ dev
new_dev FilePath
filepath Maybe (MVar Handle__)
other_side Handle__
h_ Maybe HandleFinalizer
mb_finalizer
Just MVar Handle__
r ->
FilePath
-> Handle -> MVar Handle__ -> (Handle__ -> IO Handle) -> IO Handle
forall a.
FilePath -> Handle -> MVar Handle__ -> (Handle__ -> IO a) -> IO a
withHandle_' FilePath
"dupHandle" Handle
h MVar Handle__
r ((Handle__ -> IO Handle) -> IO Handle)
-> (Handle__ -> IO Handle) -> IO Handle
forall a b. (a -> b) -> a -> b
$ \Handle__{haDevice :: ()
haDevice=dev
dev} -> do
dev
-> FilePath
-> Maybe (MVar Handle__)
-> Handle__
-> Maybe HandleFinalizer
-> IO Handle
forall dev.
(RawIO dev, IODevice dev, BufferedIO dev, Typeable dev) =>
dev
-> FilePath
-> Maybe (MVar Handle__)
-> Handle__
-> Maybe HandleFinalizer
-> IO Handle
dupHandle_ dev
dev FilePath
filepath Maybe (MVar Handle__)
other_side Handle__
h_ Maybe HandleFinalizer
mb_finalizer
dupHandle_ :: (RawIO dev, IODevice dev, BufferedIO dev, Typeable dev) => dev
-> FilePath
-> Maybe (MVar Handle__)
-> Handle__
-> Maybe HandleFinalizer
-> IO Handle
dupHandle_ :: forall dev.
(RawIO dev, IODevice dev, BufferedIO dev, Typeable dev) =>
dev
-> FilePath
-> Maybe (MVar Handle__)
-> Handle__
-> Maybe HandleFinalizer
-> IO Handle
dupHandle_ dev
new_dev FilePath
filepath Maybe (MVar Handle__)
other_side h_ :: Handle__
h_@Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer CharBufElem)
IORef (Buffer Word8)
IORef (BufferList CharBufElem)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList CharBufElem)
haCharBuffer :: IORef (Buffer CharBufElem)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer CharBufElem)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList CharBufElem)
haBufferMode :: Handle__ -> BufferMode
haType :: Handle__ -> HandleType
haDevice :: ()
..} Maybe HandleFinalizer
mb_finalizer = do
Maybe TextEncoding
mb_codec <- if Maybe (TextEncoder enc_state) -> Bool
forall a. Maybe a -> Bool
isJust Maybe (TextEncoder enc_state)
haEncoder then (TextEncoding -> Maybe TextEncoding)
-> IO TextEncoding -> IO (Maybe TextEncoding)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap TextEncoding -> Maybe TextEncoding
forall a. a -> Maybe a
Just IO TextEncoding
getLocaleEncoding else Maybe TextEncoding -> IO (Maybe TextEncoding)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe TextEncoding
forall a. Maybe a
Nothing
dev
-> FilePath
-> HandleType
-> Bool
-> Maybe TextEncoding
-> NewlineMode
-> Maybe HandleFinalizer
-> Maybe (MVar Handle__)
-> IO Handle
forall dev.
(RawIO dev, IODevice dev, BufferedIO dev, Typeable dev) =>
dev
-> FilePath
-> HandleType
-> Bool
-> Maybe TextEncoding
-> NewlineMode
-> Maybe HandleFinalizer
-> Maybe (MVar Handle__)
-> IO Handle
mkHandle dev
new_dev FilePath
filepath HandleType
haType Bool
True Maybe TextEncoding
mb_codec
NewlineMode :: Newline -> Newline -> NewlineMode
NewlineMode { inputNL :: Newline
inputNL = Newline
haInputNL, outputNL :: Newline
outputNL = Newline
haOutputNL }
Maybe HandleFinalizer
mb_finalizer Maybe (MVar Handle__)
other_side
hDuplicateTo :: Handle -> Handle -> IO ()
hDuplicateTo :: Handle -> Handle -> IO ()
hDuplicateTo h1 :: Handle
h1@(FileHandle FilePath
path MVar Handle__
m1) h2 :: Handle
h2@(FileHandle FilePath
_ MVar Handle__
m2) = do
FilePath
-> Handle -> MVar Handle__ -> (Handle__ -> IO Handle__) -> IO ()
withHandle__' FilePath
"hDuplicateTo" Handle
h2 MVar Handle__
m2 ((Handle__ -> IO Handle__) -> IO ())
-> (Handle__ -> IO Handle__) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Handle__
h2_ -> do
IO () -> IO ()
try (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ Handle__ -> IO ()
flushWriteBuffer Handle__
h2_
FilePath
-> Handle
-> MVar Handle__
-> (Handle__ -> IO Handle__)
-> IO Handle__
forall a.
FilePath -> Handle -> MVar Handle__ -> (Handle__ -> IO a) -> IO a
withHandle_' FilePath
"hDuplicateTo" Handle
h1 MVar Handle__
m1 ((Handle__ -> IO Handle__) -> IO Handle__)
-> (Handle__ -> IO Handle__) -> IO Handle__
forall a b. (a -> b) -> a -> b
$ \Handle__
h1_ -> do
FilePath
-> Handle
-> Maybe (MVar Handle__)
-> Handle__
-> Handle__
-> Maybe HandleFinalizer
-> IO Handle__
dupHandleTo FilePath
path Handle
h1 Maybe (MVar Handle__)
forall a. Maybe a
Nothing Handle__
h2_ Handle__
h1_ (HandleFinalizer -> Maybe HandleFinalizer
forall a. a -> Maybe a
Just HandleFinalizer
handleFinalizer)
hDuplicateTo h1 :: Handle
h1@(DuplexHandle FilePath
path MVar Handle__
r1 MVar Handle__
w1) h2 :: Handle
h2@(DuplexHandle FilePath
_ MVar Handle__
r2 MVar Handle__
w2) = do
FilePath
-> Handle -> MVar Handle__ -> (Handle__ -> IO Handle__) -> IO ()
withHandle__' FilePath
"hDuplicateTo" Handle
h2 MVar Handle__
w2 ((Handle__ -> IO Handle__) -> IO ())
-> (Handle__ -> IO Handle__) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Handle__
w2_ -> do
IO () -> IO ()
try (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ Handle__ -> IO ()
flushWriteBuffer Handle__
w2_
FilePath
-> Handle
-> MVar Handle__
-> (Handle__ -> IO Handle__)
-> IO Handle__
forall a.
FilePath -> Handle -> MVar Handle__ -> (Handle__ -> IO a) -> IO a
withHandle_' FilePath
"hDuplicateTo" Handle
h1 MVar Handle__
w1 ((Handle__ -> IO Handle__) -> IO Handle__)
-> (Handle__ -> IO Handle__) -> IO Handle__
forall a b. (a -> b) -> a -> b
$ \Handle__
w1_ -> do
FilePath
-> Handle
-> Maybe (MVar Handle__)
-> Handle__
-> Handle__
-> Maybe HandleFinalizer
-> IO Handle__
dupHandleTo FilePath
path Handle
h1 Maybe (MVar Handle__)
forall a. Maybe a
Nothing Handle__
w2_ Handle__
w1_ (HandleFinalizer -> Maybe HandleFinalizer
forall a. a -> Maybe a
Just HandleFinalizer
handleFinalizer)
FilePath
-> Handle -> MVar Handle__ -> (Handle__ -> IO Handle__) -> IO ()
withHandle__' FilePath
"hDuplicateTo" Handle
h2 MVar Handle__
r2 ((Handle__ -> IO Handle__) -> IO ())
-> (Handle__ -> IO Handle__) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Handle__
r2_ -> do
IO () -> IO ()
try (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ Handle__ -> IO ()
flushWriteBuffer Handle__
r2_
FilePath
-> Handle
-> MVar Handle__
-> (Handle__ -> IO Handle__)
-> IO Handle__
forall a.
FilePath -> Handle -> MVar Handle__ -> (Handle__ -> IO a) -> IO a
withHandle_' FilePath
"hDuplicateTo" Handle
h1 MVar Handle__
r1 ((Handle__ -> IO Handle__) -> IO Handle__)
-> (Handle__ -> IO Handle__) -> IO Handle__
forall a b. (a -> b) -> a -> b
$ \Handle__
r1_ -> do
FilePath
-> Handle
-> Maybe (MVar Handle__)
-> Handle__
-> Handle__
-> Maybe HandleFinalizer
-> IO Handle__
dupHandleTo FilePath
path Handle
h1 (MVar Handle__ -> Maybe (MVar Handle__)
forall a. a -> Maybe a
Just MVar Handle__
w1) Handle__
r2_ Handle__
r1_ Maybe HandleFinalizer
forall a. Maybe a
Nothing
hDuplicateTo Handle
h1 Handle
_ =
Handle -> IO ()
forall a. Handle -> IO a
ioe_dupHandlesNotCompatible Handle
h1
try :: IO () -> IO ()
try :: IO () -> IO ()
try IO ()
io = IO ()
io IO () -> (SomeException -> IO ()) -> IO ()
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`catchException` (IO () -> SomeException -> IO ()
forall a b. a -> b -> a
const (() -> IO ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()) :: SomeException -> IO ())
ioe_dupHandlesNotCompatible :: Handle -> IO a
ioe_dupHandlesNotCompatible :: forall a. Handle -> IO a
ioe_dupHandlesNotCompatible Handle
h =
IOException -> IO a
forall a. IOException -> IO a
ioException (Maybe Handle
-> IOErrorType
-> FilePath
-> FilePath
-> Maybe CInt
-> Maybe FilePath
-> IOException
IOError (Handle -> Maybe Handle
forall a. a -> Maybe a
Just Handle
h) IOErrorType
IllegalOperation FilePath
"hDuplicateTo"
FilePath
"handles are incompatible" Maybe CInt
forall a. Maybe a
Nothing Maybe FilePath
forall a. Maybe a
Nothing)
dupHandleTo :: FilePath
-> Handle
-> Maybe (MVar Handle__)
-> Handle__
-> Handle__
-> Maybe HandleFinalizer
-> IO Handle__
dupHandleTo :: FilePath
-> Handle
-> Maybe (MVar Handle__)
-> Handle__
-> Handle__
-> Maybe HandleFinalizer
-> IO Handle__
dupHandleTo FilePath
filepath Handle
h Maybe (MVar Handle__)
other_side
hto_ :: Handle__
hto_@Handle__{haDevice :: ()
haDevice=dev
devTo}
h_ :: Handle__
h_@Handle__{haDevice :: ()
haDevice=dev
dev} Maybe HandleFinalizer
mb_finalizer = do
Handle__ -> IO ()
flushBuffer Handle__
h_
case dev -> Maybe dev
forall a b. (Typeable a, Typeable b) => a -> Maybe b
cast dev
devTo of
Maybe dev
Nothing -> Handle -> IO Handle__
forall a. Handle -> IO a
ioe_dupHandlesNotCompatible Handle
h
Just dev
dev' -> do
dev
_ <- dev -> dev -> IO dev
forall a. IODevice a => a -> a -> IO a
IODevice.dup2 dev
dev dev
dev'
FileHandle FilePath
_ MVar Handle__
m <- dev
-> FilePath
-> Maybe (MVar Handle__)
-> Handle__
-> Maybe HandleFinalizer
-> IO Handle
forall dev.
(RawIO dev, IODevice dev, BufferedIO dev, Typeable dev) =>
dev
-> FilePath
-> Maybe (MVar Handle__)
-> Handle__
-> Maybe HandleFinalizer
-> IO Handle
dupHandle_ dev
dev' FilePath
filepath Maybe (MVar Handle__)
other_side Handle__
h_ Maybe HandleFinalizer
mb_finalizer
MVar Handle__ -> IO Handle__
forall a. MVar a -> IO a
takeMVar MVar Handle__
m
hShow :: Handle -> IO String
hShow :: Handle -> IO FilePath
hShow h :: Handle
h@(FileHandle FilePath
path MVar Handle__
_) = FilePath -> Bool -> Handle -> IO FilePath
showHandle' FilePath
path Bool
False Handle
h
hShow h :: Handle
h@(DuplexHandle FilePath
path MVar Handle__
_ MVar Handle__
_) = FilePath -> Bool -> Handle -> IO FilePath
showHandle' FilePath
path Bool
True Handle
h
showHandle' :: String -> Bool -> Handle -> IO String
showHandle' :: FilePath -> Bool -> Handle -> IO FilePath
showHandle' FilePath
filepath Bool
is_duplex Handle
h =
FilePath -> Handle -> (Handle__ -> IO FilePath) -> IO FilePath
forall a. FilePath -> Handle -> (Handle__ -> IO a) -> IO a
withHandle_ FilePath
"showHandle" Handle
h ((Handle__ -> IO FilePath) -> IO FilePath)
-> (Handle__ -> IO FilePath) -> IO FilePath
forall a b. (a -> b) -> a -> b
$ \Handle__
hdl_ ->
let
showType :: FilePath -> FilePath
showType | Bool
is_duplex = FilePath -> FilePath -> FilePath
showString FilePath
"duplex (read-write)"
| Bool
otherwise = HandleType -> FilePath -> FilePath
forall a. Show a => a -> FilePath -> FilePath
shows (Handle__ -> HandleType
haType Handle__
hdl_)
in
FilePath -> IO FilePath
forall (m :: * -> *) a. Monad m => a -> m a
return
(( CharBufElem -> FilePath -> FilePath
showChar CharBufElem
'{' (FilePath -> FilePath)
-> (FilePath -> FilePath) -> FilePath -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
HandleType -> (FilePath -> FilePath) -> FilePath -> FilePath
showHdl (Handle__ -> HandleType
haType Handle__
hdl_)
(FilePath -> FilePath -> FilePath
showString FilePath
"loc=" (FilePath -> FilePath)
-> (FilePath -> FilePath) -> FilePath -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> FilePath -> FilePath
showString FilePath
filepath (FilePath -> FilePath)
-> (FilePath -> FilePath) -> FilePath -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CharBufElem -> FilePath -> FilePath
showChar CharBufElem
',' (FilePath -> FilePath)
-> (FilePath -> FilePath) -> FilePath -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
FilePath -> FilePath -> FilePath
showString FilePath
"type=" (FilePath -> FilePath)
-> (FilePath -> FilePath) -> FilePath -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> FilePath
showType (FilePath -> FilePath)
-> (FilePath -> FilePath) -> FilePath -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CharBufElem -> FilePath -> FilePath
showChar CharBufElem
',' (FilePath -> FilePath)
-> (FilePath -> FilePath) -> FilePath -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
FilePath -> FilePath -> FilePath
showString FilePath
"buffering=" (FilePath -> FilePath)
-> (FilePath -> FilePath) -> FilePath -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Buffer CharBufElem -> BufferMode -> FilePath -> FilePath
forall e. Buffer e -> BufferMode -> FilePath -> FilePath
showBufMode (IO (Buffer CharBufElem) -> Buffer CharBufElem
forall a. IO a -> a
unsafePerformIO (IORef (Buffer CharBufElem) -> IO (Buffer CharBufElem)
forall a. IORef a -> IO a
readIORef (Handle__ -> IORef (Buffer CharBufElem)
haCharBuffer Handle__
hdl_))) (Handle__ -> BufferMode
haBufferMode Handle__
hdl_) (FilePath -> FilePath)
-> (FilePath -> FilePath) -> FilePath -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> FilePath -> FilePath
showString FilePath
"}" )
) FilePath
"")
where
showHdl :: HandleType -> ShowS -> ShowS
showHdl :: HandleType -> (FilePath -> FilePath) -> FilePath -> FilePath
showHdl HandleType
ht FilePath -> FilePath
cont =
case HandleType
ht of
HandleType
ClosedHandle -> HandleType -> FilePath -> FilePath
forall a. Show a => a -> FilePath -> FilePath
shows HandleType
ht (FilePath -> FilePath)
-> (FilePath -> FilePath) -> FilePath -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> FilePath -> FilePath
showString FilePath
"}"
HandleType
_ -> FilePath -> FilePath
cont
showBufMode :: Buffer e -> BufferMode -> ShowS
showBufMode :: forall e. Buffer e -> BufferMode -> FilePath -> FilePath
showBufMode Buffer e
buf BufferMode
bmo =
case BufferMode
bmo of
BufferMode
NoBuffering -> FilePath -> FilePath -> FilePath
showString FilePath
"none"
BufferMode
LineBuffering -> FilePath -> FilePath -> FilePath
showString FilePath
"line"
BlockBuffering (Just Int
n) -> FilePath -> FilePath -> FilePath
showString FilePath
"block " (FilePath -> FilePath)
-> (FilePath -> FilePath) -> FilePath -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> (FilePath -> FilePath) -> FilePath -> FilePath
showParen Bool
True (Int -> FilePath -> FilePath
forall a. Show a => a -> FilePath -> FilePath
shows Int
n)
BlockBuffering Maybe Int
Nothing -> FilePath -> FilePath -> FilePath
showString FilePath
"block " (FilePath -> FilePath)
-> (FilePath -> FilePath) -> FilePath -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> (FilePath -> FilePath) -> FilePath -> FilePath
showParen Bool
True (Int -> FilePath -> FilePath
forall a. Show a => a -> FilePath -> FilePath
shows Int
def)
where
def :: Int
def :: Int
def = Buffer e -> Int
forall e. Buffer e -> Int
bufSize Buffer e
buf