1 {-# OPTIONS_GHC -fno-implicit-prelude #-}
2 {-# OPTIONS_GHC -XRecordWildCards #-}
3 {-# OPTIONS_GHC -fno-warn-unused-matches #-}
4 -----------------------------------------------------------------------------
6 -- Module : GHC.IO.Handle
7 -- Copyright : (c) The University of Glasgow, 1994-2009
8 -- License : see libraries/base/LICENSE
10 -- Maintainer : libraries@haskell.org
11 -- Stability : provisional
12 -- Portability : non-portable
14 -- External API for GHC's Handle implementation
16 -----------------------------------------------------------------------------
18 module GHC.IO.Handle (
22 mkFileHandle, mkDuplexHandle,
24 hFileSize, hSetFileSize, hIsEOF, hLookAhead,
25 hSetBuffering, hSetBinaryMode, hSetEncoding, hGetEncoding,
26 hFlush, hFlushAll, hDuplicate, hDuplicateTo,
30 HandlePosition, HandlePosn(..), hGetPosn, hSetPosn,
31 SeekMode(..), hSeek, hTell,
33 hIsOpen, hIsClosed, hIsReadable, hIsWritable, hGetBuffering, hIsSeekable,
34 hSetEcho, hGetEcho, hIsTerminalDevice,
36 hSetNewlineMode, Newline(..), NewlineMode(..), nativeNewline,
37 noNewlineTranslation, universalNewlineMode, nativeNewlineMode,
41 hWaitForInput, hGetChar, hGetLine, hGetContents, hPutChar, hPutStr,
43 hGetBuf, hGetBufNonBlocking, hPutBuf, hPutBufNonBlocking
47 import GHC.IO.Exception
48 import GHC.IO.Encoding
50 import GHC.IO.BufferedIO ( BufferedIO )
51 import GHC.IO.Device as IODevice
52 import GHC.IO.Handle.Types
53 import GHC.IO.Handle.Internals
54 import GHC.IO.Handle.Text
55 import System.IO.Error
68 -- ---------------------------------------------------------------------------
71 -- | Computation 'hClose' @hdl@ makes handle @hdl@ closed. Before the
72 -- computation finishes, if @hdl@ is writable its buffer is flushed as
74 -- Performing 'hClose' on a handle that has already been closed has no effect;
75 -- doing so is not an error. All other operations on a closed handle will fail.
76 -- If 'hClose' fails for any reason, any further operations (apart from
77 -- 'hClose') on the handle will still fail as if @hdl@ had been successfully
80 hClose :: Handle -> IO ()
81 hClose h@(FileHandle _ m) = do
83 hClose_maybethrow mb_exc h
84 hClose h@(DuplexHandle _ r w) = do
85 mb_exc1 <- hClose' h w
86 mb_exc2 <- hClose' h r
89 Just e -> hClose_maybethrow mb_exc2 h
91 hClose_maybethrow :: Maybe SomeException -> Handle -> IO ()
92 hClose_maybethrow Nothing h = return ()
93 hClose_maybethrow (Just e) h = hClose_rethrow e h
95 hClose_rethrow :: SomeException -> Handle -> IO ()
97 case fromException e of
98 Just ioe -> ioError (augmentIOError ioe "hClose" h)
101 hClose' :: Handle -> MVar Handle__ -> IO (Maybe SomeException)
102 hClose' h m = withHandle' "hClose" h m $ hClose_help
104 -----------------------------------------------------------------------------
105 -- Detecting and changing the size of a file
107 -- | For a handle @hdl@ which attached to a physical file,
108 -- 'hFileSize' @hdl@ returns the size of that file in 8-bit bytes.
110 hFileSize :: Handle -> IO Integer
112 withHandle_ "hFileSize" handle $ \ handle_@Handle__{haDevice=dev} -> do
113 case haType handle_ of
114 ClosedHandle -> ioe_closedHandle
115 SemiClosedHandle -> ioe_closedHandle
116 _ -> do flushWriteBuffer handle_
117 r <- IODevice.getSize dev
120 else ioException (IOError Nothing InappropriateType "hFileSize"
121 "not a regular file" Nothing Nothing)
124 -- | 'hSetFileSize' @hdl@ @size@ truncates the physical file with handle @hdl@ to @size@ bytes.
126 hSetFileSize :: Handle -> Integer -> IO ()
127 hSetFileSize handle size =
128 withHandle_ "hSetFileSize" handle $ \ handle_@Handle__{haDevice=dev} -> do
129 case haType handle_ of
130 ClosedHandle -> ioe_closedHandle
131 SemiClosedHandle -> ioe_closedHandle
132 _ -> do flushWriteBuffer handle_
133 IODevice.setSize dev size
136 -- ---------------------------------------------------------------------------
137 -- Detecting the End of Input
139 -- | For a readable handle @hdl@, 'hIsEOF' @hdl@ returns
140 -- 'True' if no further input can be taken from @hdl@ or for a
141 -- physical file, if the current I\/O position is equal to the length of
142 -- the file. Otherwise, it returns 'False'.
144 -- NOTE: 'hIsEOF' may block, because it is the same as calling
145 -- 'hLookAhead' and checking for an EOF exception.
147 hIsEOF :: Handle -> IO Bool
150 (hLookAhead handle >> return False)
151 (\e -> if isEOFError e then return True else ioError e)
153 -- ---------------------------------------------------------------------------
156 -- | Computation 'hLookAhead' returns the next character from the handle
157 -- without removing it from the input buffer, blocking until a character
160 -- This operation may fail with:
162 -- * 'isEOFError' if the end of file has been reached.
164 hLookAhead :: Handle -> IO Char
166 wantReadableHandle_ "hLookAhead" handle hLookAhead_
168 -- ---------------------------------------------------------------------------
169 -- Buffering Operations
171 -- Three kinds of buffering are supported: line-buffering,
172 -- block-buffering or no-buffering. See GHC.IO.Handle for definition and
173 -- further explanation of what the type represent.
175 -- | Computation 'hSetBuffering' @hdl mode@ sets the mode of buffering for
176 -- handle @hdl@ on subsequent reads and writes.
178 -- If the buffer mode is changed from 'BlockBuffering' or
179 -- 'LineBuffering' to 'NoBuffering', then
181 -- * if @hdl@ is writable, the buffer is flushed as for 'hFlush';
183 -- * if @hdl@ is not writable, the contents of the buffer is discarded.
185 -- This operation may fail with:
187 -- * 'isPermissionError' if the handle has already been used for reading
188 -- or writing and the implementation does not allow the buffering mode
191 hSetBuffering :: Handle -> BufferMode -> IO ()
192 hSetBuffering handle mode =
193 withAllHandles__ "hSetBuffering" handle $ \ handle_@Handle__{..} -> do
195 ClosedHandle -> ioe_closedHandle
197 if mode == haBufferMode then return handle_ else do
200 - we flush the old buffer regardless of whether
201 the new buffer could fit the contents of the old buffer
203 - allow a handle's buffering to change even if IO has
204 occurred (ANSI C spec. does not allow this, nor did
205 the previous implementation of IO.hSetBuffering).
206 - a non-standard extension is to allow the buffering
207 of semi-closed handles to change [sof 6/98]
209 flushCharBuffer handle_
211 let state = initBufferState haType
212 reading = not (isWritableHandleType haType)
216 -- See [note Buffer Sizing], GHC.IO.Handle.Types
217 NoBuffering | reading -> newCharBuffer dEFAULT_CHAR_BUFFER_SIZE state
218 | otherwise -> newCharBuffer 1 state
219 LineBuffering -> newCharBuffer dEFAULT_CHAR_BUFFER_SIZE state
220 BlockBuffering Nothing -> newCharBuffer dEFAULT_CHAR_BUFFER_SIZE state
221 BlockBuffering (Just n) | n <= 0 -> ioe_bufsiz n
222 | otherwise -> newCharBuffer n state
224 writeIORef haCharBuffer new_buf
226 -- for input terminals we need to put the terminal into
227 -- cooked or raw mode depending on the type of buffering.
228 is_tty <- IODevice.isTerminal haDevice
229 when (is_tty && isReadableHandleType haType) $
231 #ifndef mingw32_HOST_OS
232 -- 'raw' mode under win32 is a bit too specialised (and troublesome
233 -- for most common uses), so simply disable its use here.
234 NoBuffering -> IODevice.setRaw haDevice True
236 NoBuffering -> return ()
238 _ -> IODevice.setRaw haDevice False
240 -- throw away spare buffers, they might be the wrong size
241 writeIORef haBuffers BufferListNil
243 return Handle__{ haBufferMode = mode,.. }
245 -- -----------------------------------------------------------------------------
248 -- | The action 'hSetEncoding' @hdl@ @encoding@ changes the text encoding
249 -- for the handle @hdl@ to @encoding@. The default encoding when a 'Handle' is
250 -- created is 'localeEncoding', namely the default encoding for the current
253 -- To create a 'Handle' with no encoding at all, use 'openBinaryFile'. To
254 -- stop further encoding or decoding on an existing 'Handle', use
257 -- 'hSetEncoding' may need to flush buffered data in order to change
260 hSetEncoding :: Handle -> TextEncoding -> IO ()
261 hSetEncoding hdl encoding = do
262 withHandle "hSetEncoding" hdl $ \h_@Handle__{..} -> do
264 openTextEncoding (Just encoding) haType $ \ mb_encoder mb_decoder -> do
265 bbuf <- readIORef haByteBuffer
266 ref <- newIORef (error "last_decode")
267 return (Handle__{ haLastDecode = ref,
268 haDecoder = mb_decoder,
269 haEncoder = mb_encoder,
270 haCodec = Just encoding, .. },
273 -- | Return the current 'TextEncoding' for the specified 'Handle', or
274 -- 'Nothing' if the 'Handle' is in binary mode.
276 -- Note that the 'TextEncoding' remembers nothing about the state of
277 -- the encoder/decoder in use on this 'Handle'. For example, if the
278 -- encoding in use is UTF-16, then using 'hGetEncoding' and
279 -- 'hSetEncoding' to save and restore the encoding may result in an
280 -- extra byte-order-mark being written to the file.
282 hGetEncoding :: Handle -> IO (Maybe TextEncoding)
284 withHandle_ "hGetEncoding" hdl $ \h_@Handle__{..} -> return haCodec
286 -- -----------------------------------------------------------------------------
289 -- | The action 'hFlush' @hdl@ causes any items buffered for output
290 -- in handle @hdl@ to be sent immediately to the operating system.
292 -- This operation may fail with:
294 -- * 'isFullError' if the device is full;
296 -- * 'isPermissionError' if a system resource limit would be exceeded.
297 -- It is unspecified whether the characters in the buffer are discarded
298 -- or retained under these circumstances.
300 hFlush :: Handle -> IO ()
301 hFlush handle = wantWritableHandle "hFlush" handle flushWriteBuffer
303 -- | The action 'hFlushAll' @hdl@ flushes all buffered data in @hdl@,
304 -- including any buffered read data. Buffered read data is flushed
305 -- by seeking the file position back to the point before the bufferred
306 -- data was read, and hence only works if @hdl@ is seekable (see
309 -- This operation may fail with:
311 -- * 'isFullError' if the device is full;
313 -- * 'isPermissionError' if a system resource limit would be exceeded.
314 -- It is unspecified whether the characters in the buffer are discarded
315 -- or retained under these circumstances;
317 -- * 'isIllegalOperation' if @hdl@ has buffered read data, and is not
320 hFlushAll :: Handle -> IO ()
321 hFlushAll handle = withHandle_ "hFlushAll" handle flushBuffer
323 -- -----------------------------------------------------------------------------
324 -- Repositioning Handles
326 data HandlePosn = HandlePosn Handle HandlePosition
328 instance Eq HandlePosn where
329 (HandlePosn h1 p1) == (HandlePosn h2 p2) = p1==p2 && h1==h2
331 instance Show HandlePosn where
332 showsPrec p (HandlePosn h pos) =
333 showsPrec p h . showString " at position " . shows pos
335 -- HandlePosition is the Haskell equivalent of POSIX' off_t.
336 -- We represent it as an Integer on the Haskell side, but
337 -- cheat slightly in that hGetPosn calls upon a C helper
338 -- that reports the position back via (merely) an Int.
339 type HandlePosition = Integer
341 -- | Computation 'hGetPosn' @hdl@ returns the current I\/O position of
342 -- @hdl@ as a value of the abstract type 'HandlePosn'.
344 hGetPosn :: Handle -> IO HandlePosn
347 return (HandlePosn handle posn)
349 -- | If a call to 'hGetPosn' @hdl@ returns a position @p@,
350 -- then computation 'hSetPosn' @p@ sets the position of @hdl@
351 -- to the position it held at the time of the call to 'hGetPosn'.
353 -- This operation may fail with:
355 -- * 'isPermissionError' if a system resource limit would be exceeded.
357 hSetPosn :: HandlePosn -> IO ()
358 hSetPosn (HandlePosn h i) = hSeek h AbsoluteSeek i
360 -- ---------------------------------------------------------------------------
364 - when seeking using `SeekFromEnd', positive offsets (>=0) means
365 seeking at or past EOF.
367 - we possibly deviate from the report on the issue of seeking within
368 the buffer and whether to flush it or not. The report isn't exactly
372 -- | Computation 'hSeek' @hdl mode i@ sets the position of handle
373 -- @hdl@ depending on @mode@.
374 -- The offset @i@ is given in terms of 8-bit bytes.
376 -- If @hdl@ is block- or line-buffered, then seeking to a position which is not
377 -- in the current buffer will first cause any items in the output buffer to be
378 -- written to the device, and then cause the input buffer to be discarded.
379 -- Some handles may not be seekable (see 'hIsSeekable'), or only support a
380 -- subset of the possible positioning operations (for instance, it may only
381 -- be possible to seek to the end of a tape, or to a positive offset from
382 -- the beginning or current position).
383 -- It is not possible to set a negative I\/O position, or for
384 -- a physical file, an I\/O position beyond the current end-of-file.
386 -- This operation may fail with:
388 -- * 'isPermissionError' if a system resource limit would be exceeded.
390 hSeek :: Handle -> SeekMode -> Integer -> IO ()
391 hSeek handle mode offset =
392 wantSeekableHandle "hSeek" handle $ \ handle_@Handle__{..} -> do
393 debugIO ("hSeek " ++ show (mode,offset))
394 buf <- readIORef haCharBuffer
397 then do flushWriteBuffer handle_
398 IODevice.seek haDevice mode offset
401 let r = bufL buf; w = bufR buf
402 if mode == RelativeSeek && isNothing haDecoder &&
403 offset >= 0 && offset < fromIntegral (w - r)
404 then writeIORef haCharBuffer buf{ bufL = r + fromIntegral offset }
407 flushCharReadBuffer handle_
408 flushByteReadBuffer handle_
409 IODevice.seek haDevice mode offset
412 hTell :: Handle -> IO Integer
414 wantSeekableHandle "hGetPosn" handle $ \ handle_@Handle__{..} -> do
416 posn <- IODevice.tell haDevice
418 cbuf <- readIORef haCharBuffer
419 bbuf <- readIORef haByteBuffer
422 | isWriteBuffer cbuf = posn + fromIntegral (bufR cbuf)
423 | otherwise = posn - fromIntegral (bufR cbuf - bufL cbuf)
424 - fromIntegral (bufR bbuf - bufL bbuf)
426 debugIO ("\nhGetPosn: (posn, real_posn) = " ++ show (posn, real_posn))
427 debugIO (" cbuf: " ++ summaryBuffer cbuf ++
428 " bbuf: " ++ summaryBuffer bbuf)
432 -- -----------------------------------------------------------------------------
435 -- A number of operations return information about the properties of a
436 -- handle. Each of these operations returns `True' if the handle has
437 -- the specified property, and `False' otherwise.
439 hIsOpen :: Handle -> IO Bool
441 withHandle_ "hIsOpen" handle $ \ handle_ -> do
442 case haType handle_ of
443 ClosedHandle -> return False
444 SemiClosedHandle -> return False
447 hIsClosed :: Handle -> IO Bool
449 withHandle_ "hIsClosed" handle $ \ handle_ -> do
450 case haType handle_ of
451 ClosedHandle -> return True
454 {- not defined, nor exported, but mentioned
455 here for documentation purposes:
457 hSemiClosed :: Handle -> IO Bool
461 return (not (ho || hc))
464 hIsReadable :: Handle -> IO Bool
465 hIsReadable (DuplexHandle _ _ _) = return True
467 withHandle_ "hIsReadable" handle $ \ handle_ -> do
468 case haType handle_ of
469 ClosedHandle -> ioe_closedHandle
470 SemiClosedHandle -> ioe_closedHandle
471 htype -> return (isReadableHandleType htype)
473 hIsWritable :: Handle -> IO Bool
474 hIsWritable (DuplexHandle _ _ _) = return True
476 withHandle_ "hIsWritable" handle $ \ handle_ -> do
477 case haType handle_ of
478 ClosedHandle -> ioe_closedHandle
479 SemiClosedHandle -> ioe_closedHandle
480 htype -> return (isWritableHandleType htype)
482 -- | Computation 'hGetBuffering' @hdl@ returns the current buffering mode
485 hGetBuffering :: Handle -> IO BufferMode
486 hGetBuffering handle =
487 withHandle_ "hGetBuffering" handle $ \ handle_ -> do
488 case haType handle_ of
489 ClosedHandle -> ioe_closedHandle
491 -- We're being non-standard here, and allow the buffering
492 -- of a semi-closed handle to be queried. -- sof 6/98
493 return (haBufferMode handle_) -- could be stricter..
495 hIsSeekable :: Handle -> IO Bool
497 withHandle_ "hIsSeekable" handle $ \ handle_@Handle__{..} -> do
499 ClosedHandle -> ioe_closedHandle
500 SemiClosedHandle -> ioe_closedHandle
501 AppendHandle -> return False
502 _ -> IODevice.isSeekable haDevice
504 -- -----------------------------------------------------------------------------
505 -- Changing echo status (Non-standard GHC extensions)
507 -- | Set the echoing status of a handle connected to a terminal.
509 hSetEcho :: Handle -> Bool -> IO ()
510 hSetEcho handle on = do
511 isT <- hIsTerminalDevice handle
515 withHandle_ "hSetEcho" handle $ \ Handle__{..} -> do
517 ClosedHandle -> ioe_closedHandle
518 _ -> IODevice.setEcho haDevice on
520 -- | Get the echoing status of a handle connected to a terminal.
522 hGetEcho :: Handle -> IO Bool
524 isT <- hIsTerminalDevice handle
528 withHandle_ "hGetEcho" handle $ \ Handle__{..} -> do
530 ClosedHandle -> ioe_closedHandle
531 _ -> IODevice.getEcho haDevice
533 -- | Is the handle connected to a terminal?
535 hIsTerminalDevice :: Handle -> IO Bool
536 hIsTerminalDevice handle = do
537 withHandle_ "hIsTerminalDevice" handle $ \ Handle__{..} -> do
539 ClosedHandle -> ioe_closedHandle
540 _ -> IODevice.isTerminal haDevice
542 -- -----------------------------------------------------------------------------
545 -- | Select binary mode ('True') or text mode ('False') on a open handle.
546 -- (See also 'openBinaryFile'.)
548 -- This has the same effect as calling 'hSetEncoding' with 'latin1', together
549 -- with 'hSetNewlineMode' with 'noNewlineTranslation'.
551 hSetBinaryMode :: Handle -> Bool -> IO ()
552 hSetBinaryMode handle bin =
553 withAllHandles__ "hSetBinaryMode" handle $ \ h_@Handle__{..} ->
557 let mb_te | bin = Nothing
558 | otherwise = Just localeEncoding
560 openTextEncoding mb_te haType $ \ mb_encoder mb_decoder -> do
562 -- should match the default newline mode, whatever that is
563 let nl | bin = noNewlineTranslation
564 | otherwise = nativeNewlineMode
566 bbuf <- readIORef haByteBuffer
567 ref <- newIORef (error "codec_state", bbuf)
569 return Handle__{ haLastDecode = ref,
570 haEncoder = mb_encoder,
571 haDecoder = mb_decoder,
573 haInputNL = inputNL nl,
574 haOutputNL = outputNL nl, .. }
576 -- -----------------------------------------------------------------------------
579 -- | Set the 'NewlineMode' on the specified 'Handle'. All buffered
580 -- data is flushed first.
581 hSetNewlineMode :: Handle -> NewlineMode -> IO ()
582 hSetNewlineMode handle NewlineMode{ inputNL=i, outputNL=o } =
583 withAllHandles__ "hSetNewlineMode" handle $ \h_@Handle__{..} ->
586 return h_{ haInputNL=i, haOutputNL=o }
588 -- -----------------------------------------------------------------------------
589 -- Duplicating a Handle
591 -- | Returns a duplicate of the original handle, with its own buffer.
592 -- The two Handles will share a file pointer, however. The original
593 -- handle's buffer is flushed, including discarding any input data,
594 -- before the handle is duplicated.
596 hDuplicate :: Handle -> IO Handle
597 hDuplicate h@(FileHandle path m) = do
598 withHandle_' "hDuplicate" h m $ \h_ ->
599 dupHandle path h Nothing h_ (Just handleFinalizer)
600 hDuplicate h@(DuplexHandle path r w) = do
601 write_side@(FileHandle _ write_m) <-
602 withHandle_' "hDuplicate" h w $ \h_ ->
603 dupHandle path h Nothing h_ (Just handleFinalizer)
604 read_side@(FileHandle _ read_m) <-
605 withHandle_' "hDuplicate" h r $ \h_ ->
606 dupHandle path h (Just write_m) h_ Nothing
607 return (DuplexHandle path read_m write_m)
609 dupHandle :: FilePath
611 -> Maybe (MVar Handle__)
613 -> Maybe HandleFinalizer
615 dupHandle filepath h other_side h_@Handle__{..} mb_finalizer = do
616 -- flush the buffer first, so we don't have to copy its contents
620 new_dev <- IODevice.dup haDevice
621 dupHandle_ new_dev filepath other_side h_ mb_finalizer
623 withHandle_' "dupHandle" h r $ \Handle__{haDevice=dev} -> do
624 dupHandle_ dev filepath other_side h_ mb_finalizer
626 dupHandle_ :: (IODevice dev, BufferedIO dev, Typeable dev) => dev
628 -> Maybe (MVar Handle__)
630 -> Maybe HandleFinalizer
632 dupHandle_ new_dev filepath other_side h_@Handle__{..} mb_finalizer = do
634 let mb_codec = if isJust haEncoder then Just localeEncoding else Nothing
635 mkHandle new_dev filepath haType True{-buffered-} mb_codec
636 NewlineMode { inputNL = haInputNL, outputNL = haOutputNL }
637 mb_finalizer other_side
639 -- -----------------------------------------------------------------------------
640 -- Replacing a Handle
643 Makes the second handle a duplicate of the first handle. The second
644 handle will be closed first, if it is not already.
646 This can be used to retarget the standard Handles, for example:
648 > do h <- openFile "mystdout" WriteMode
649 > hDuplicateTo h stdout
652 hDuplicateTo :: Handle -> Handle -> IO ()
653 hDuplicateTo h1@(FileHandle path m1) h2@(FileHandle _ m2) = do
654 withHandle__' "hDuplicateTo" h2 m2 $ \h2_ -> do
656 withHandle_' "hDuplicateTo" h1 m1 $ \h1_ -> do
657 dupHandleTo path h1 Nothing h2_ h1_ (Just handleFinalizer)
658 hDuplicateTo h1@(DuplexHandle path r1 w1) h2@(DuplexHandle _ r2 w2) = do
659 withHandle__' "hDuplicateTo" h2 w2 $ \w2_ -> do
661 withHandle_' "hDuplicateTo" h1 w1 $ \w1_ -> do
662 dupHandleTo path h1 Nothing w2_ w1_ (Just handleFinalizer)
663 withHandle__' "hDuplicateTo" h2 r2 $ \r2_ -> do
665 withHandle_' "hDuplicateTo" h1 r1 $ \r1_ -> do
666 dupHandleTo path h1 (Just w1) r2_ r1_ Nothing
668 ioe_dupHandlesNotCompatible h1
671 ioe_dupHandlesNotCompatible :: Handle -> IO a
672 ioe_dupHandlesNotCompatible h =
673 ioException (IOError (Just h) IllegalOperation "hDuplicateTo"
674 "handles are incompatible" Nothing Nothing)
676 dupHandleTo :: FilePath
678 -> Maybe (MVar Handle__)
681 -> Maybe HandleFinalizer
683 dupHandleTo filepath h other_side
684 hto_@Handle__{haDevice=devTo,..}
685 h_@Handle__{haDevice=dev} mb_finalizer = do
688 Nothing -> ioe_dupHandlesNotCompatible h
690 _ <- IODevice.dup2 dev dev'
691 FileHandle _ m <- dupHandle_ dev' filepath other_side h_ mb_finalizer
694 -- ---------------------------------------------------------------------------
697 -- | 'hShow' is in the 'IO' monad, and gives more comprehensive output
698 -- than the (pure) instance of 'Show' for 'Handle'.
700 hShow :: Handle -> IO String
701 hShow h@(FileHandle path _) = showHandle' path False h
702 hShow h@(DuplexHandle path _ _) = showHandle' path True h
704 showHandle' :: String -> Bool -> Handle -> IO String
705 showHandle' filepath is_duplex h =
706 withHandle_ "showHandle" h $ \hdl_ ->
708 showType | is_duplex = showString "duplex (read-write)"
709 | otherwise = shows (haType hdl_)
713 showHdl (haType hdl_)
714 (showString "loc=" . showString filepath . showChar ',' .
715 showString "type=" . showType . showChar ',' .
716 showString "buffering=" . showBufMode (unsafePerformIO (readIORef (haCharBuffer hdl_))) (haBufferMode hdl_) . showString "}" )
720 showHdl :: HandleType -> ShowS -> ShowS
723 ClosedHandle -> shows ht . showString "}"
726 showBufMode :: Buffer e -> BufferMode -> ShowS
727 showBufMode buf bmo =
729 NoBuffering -> showString "none"
730 LineBuffering -> showString "line"
731 BlockBuffering (Just n) -> showString "block " . showParen True (shows n)
732 BlockBuffering Nothing -> showString "block " . showParen True (shows def)