Handle.hs 26.5 KB
Newer Older
1
{-# LANGUAGE Trustworthy #-}
2 3 4 5 6
{-# LANGUAGE CPP
           , NoImplicitPrelude
           , RecordWildCards
           , NondecreasingIndentation
  #-}
7
{-# OPTIONS_GHC -Wno-unused-matches #-}
8

9 10 11 12 13
-----------------------------------------------------------------------------
-- |
-- Module      :  GHC.IO.Handle
-- Copyright   :  (c) The University of Glasgow, 1994-2009
-- License     :  see libraries/base/LICENSE
14
--
15 16 17 18 19 20 21 22 23 24 25
-- Maintainer  :  libraries@haskell.org
-- Stability   :  provisional
-- Portability :  non-portable
--
-- External API for GHC's Handle implementation
--
-----------------------------------------------------------------------------

module GHC.IO.Handle (
   Handle,
   BufferMode(..),
26

27
   mkFileHandle, mkDuplexHandle,
28

29
   hFileSize, hSetFileSize, hIsEOF, isEOF, hLookAhead,
30
   hSetBuffering, hSetBinaryMode, hSetEncoding, hGetEncoding,
31
   hFlush, hFlushAll, hDuplicate, hDuplicateTo,
32

33
   hClose, hClose_help,
34

35 36
   LockMode(..), hLock, hTryLock,

37 38
   HandlePosition, HandlePosn(..), hGetPosn, hSetPosn,
   SeekMode(..), hSeek, hTell,
39

40 41
   hIsOpen, hIsClosed, hIsReadable, hIsWritable, hGetBuffering, hIsSeekable,
   hSetEcho, hGetEcho, hIsTerminalDevice,
42

43 44 45 46 47
   hSetNewlineMode, Newline(..), NewlineMode(..), nativeNewline,
   noNewlineTranslation, universalNewlineMode, nativeNewlineMode,

   hShow,

48
   hWaitForInput, hGetChar, hGetLine, hGetContents, hGetContents', hPutChar, hPutStr,
49 50 51 52 53 54 55 56 57 58

   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
59 60
import GHC.IO.Handle.FD
import GHC.IO.Handle.Lock
61 62 63
import GHC.IO.Handle.Types
import GHC.IO.Handle.Internals
import GHC.IO.Handle.Text
64
import qualified GHC.IO.BufferedIO as Buffered
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

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

-- ---------------------------------------------------------------------------
-- Closing a handle

-- | Computation 'hClose' @hdl@ makes handle @hdl@ closed.  Before the
-- computation finishes, if @hdl@ is writable its buffer is flushed as
-- for 'hFlush'.
82
-- Performing 'hClose' on a handle that has already been closed has no effect;
83 84 85 86 87 88
-- doing so is not an error.  All other operations on a closed handle will fail.
-- If 'hClose' fails for any reason, any further operations (apart from
-- 'hClose') on the handle will still fail as if @hdl@ had been successfully
-- closed.

hClose :: Handle -> IO ()
89
hClose h@(FileHandle _ m)     = do
90
  mb_exc <- hClose' h m
91
  hClose_maybethrow mb_exc h
92
hClose h@(DuplexHandle _ r w) = do
93 94
  excs <- mapM (hClose' h) [r,w]
  hClose_maybethrow (listToMaybe (catMaybes excs)) h
95 96

hClose_maybethrow :: Maybe SomeException -> Handle -> IO ()
97
hClose_maybethrow Nothing  h = return ()
98
hClose_maybethrow (Just e) h = hClose_rethrow e h
99 100

hClose_rethrow :: SomeException -> Handle -> IO ()
101
hClose_rethrow e h =
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
  case fromException e of
    Just ioe -> ioError (augmentIOError ioe "hClose" h)
    Nothing  -> throwIO e

hClose' :: Handle -> MVar Handle__ -> IO (Maybe SomeException)
hClose' h m = withHandle' "hClose" h m $ hClose_help

-----------------------------------------------------------------------------
-- Detecting and changing the size of a file

-- | For a handle @hdl@ which attached to a physical file,
-- 'hFileSize' @hdl@ returns the size of that file in 8-bit bytes.

hFileSize :: Handle -> IO Integer
hFileSize handle =
    withHandle_ "hFileSize" handle $ \ handle_@Handle__{haDevice=dev} -> do
118
    case haType handle_ of
119
      ClosedHandle              -> ioe_closedHandle
120
      SemiClosedHandle          -> ioe_semiclosedHandle
121 122 123 124 125 126 127 128 129 130 131 132 133
      _ -> do flushWriteBuffer handle_
              r <- IODevice.getSize dev
              if r /= -1
                 then return r
                 else ioException (IOError Nothing InappropriateType "hFileSize"
                                   "not a regular file" Nothing Nothing)


-- | 'hSetFileSize' @hdl@ @size@ truncates the physical file with handle @hdl@ to @size@ bytes.

hSetFileSize :: Handle -> Integer -> IO ()
hSetFileSize handle size =
    withHandle_ "hSetFileSize" handle $ \ handle_@Handle__{haDevice=dev} -> do
134
    case haType handle_ of
135
      ClosedHandle              -> ioe_closedHandle
136
      SemiClosedHandle          -> ioe_semiclosedHandle
137 138 139 140 141 142 143 144 145 146 147 148
      _ -> do flushWriteBuffer handle_
              IODevice.setSize dev size
              return ()

-- ---------------------------------------------------------------------------
-- Detecting the End of Input

-- | For a readable handle @hdl@, 'hIsEOF' @hdl@ returns
-- 'True' if no further input can be taken from @hdl@ or for a
-- physical file, if the current I\/O position is equal to the length of
-- the file.  Otherwise, it returns 'False'.
--
149 150
-- NOTE: 'hIsEOF' may block, because it has to attempt to read from
-- the stream to determine whether there is any more data to be read.
151 152

hIsEOF :: Handle -> IO Bool
153 154 155 156 157 158 159 160 161 162 163 164 165 166
hIsEOF handle = wantReadableHandle_ "hIsEOF" handle $ \Handle__{..} -> do

  cbuf <- readIORef haCharBuffer
  if not (isEmptyBuffer cbuf) then return False else do

  bbuf <- readIORef haByteBuffer
  if not (isEmptyBuffer bbuf) then return False else do

  -- NB. do no decoding, just fill the byte buffer; see #3808
  (r,bbuf') <- Buffered.fillReadBuffer haDevice bbuf
  if r == 0
     then return True
     else do writeIORef haByteBuffer bbuf'
             return False
167

168 169 170 171 172 173 174 175 176
-- ---------------------------------------------------------------------------
-- isEOF

-- | The computation 'isEOF' is identical to 'hIsEOF',
-- except that it works only on 'stdin'.

isEOF :: IO Bool
isEOF = hIsEOF stdin

177 178 179 180 181 182 183 184 185
-- ---------------------------------------------------------------------------
-- Looking ahead

-- | Computation 'hLookAhead' returns the next character from the handle
-- without removing it from the input buffer, blocking until a character
-- is available.
--
-- This operation may fail with:
--
186
--  * 'System.IO.Error.isEOFError' if the end of file has been reached.
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

hLookAhead :: Handle -> IO Char
hLookAhead handle =
  wantReadableHandle_ "hLookAhead"  handle hLookAhead_

-- ---------------------------------------------------------------------------
-- Buffering Operations

-- Three kinds of buffering are supported: line-buffering,
-- block-buffering or no-buffering.  See GHC.IO.Handle for definition and
-- further explanation of what the type represent.

-- | Computation 'hSetBuffering' @hdl mode@ sets the mode of buffering for
-- handle @hdl@ on subsequent reads and writes.
--
-- If the buffer mode is changed from 'BlockBuffering' or
-- 'LineBuffering' to 'NoBuffering', then
--
--  * if @hdl@ is writable, the buffer is flushed as for 'hFlush';
--
--  * if @hdl@ is not writable, the contents of the buffer is discarded.
--
-- This operation may fail with:
--
211 212 213
--  * 'System.IO.Error.isPermissionError' if the handle has already been used
--    for reading or writing and the implementation does not allow the
--    buffering mode to be changed.
214 215 216 217 218 219 220 221 222

hSetBuffering :: Handle -> BufferMode -> IO ()
hSetBuffering handle mode =
  withAllHandles__ "hSetBuffering" handle $ \ handle_@Handle__{..} -> do
  case haType of
    ClosedHandle -> ioe_closedHandle
    _ -> do
         if mode == haBufferMode then return handle_ else do

223
         -- See [note Buffer Sizing] in GHC.IO.Handle.Types
224

225 226 227 228
          -- check for errors:
          case mode of
              BlockBuffering (Just n) | n <= 0    -> ioe_bufsiz n
              _ -> return ()
229 230 231 232 233 234

          -- for input terminals we need to put the terminal into
          -- cooked or raw mode depending on the type of buffering.
          is_tty <- IODevice.isTerminal haDevice
          when (is_tty && isReadableHandleType haType) $
                case mode of
Ben Gamari's avatar
Ben Gamari committed
235
#if !defined(mingw32_HOST_OS)
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
        -- 'raw' mode under win32 is a bit too specialised (and troublesome
        -- for most common uses), so simply disable its use here.
                  NoBuffering -> IODevice.setRaw haDevice True
#else
                  NoBuffering -> return ()
#endif
                  _           -> IODevice.setRaw haDevice False

          -- throw away spare buffers, they might be the wrong size
          writeIORef haBuffers BufferListNil

          return Handle__{ haBufferMode = mode,.. }

-- -----------------------------------------------------------------------------
-- hSetEncoding

-- | The action 'hSetEncoding' @hdl@ @encoding@ changes the text encoding
253
-- for the handle @hdl@ to @encoding@.  The default encoding when a 'Handle' is
254 255
-- created is 'System.IO.localeEncoding', namely the default encoding for the
-- current locale.
256 257 258 259 260
--
-- To create a 'Handle' with no encoding at all, use 'openBinaryFile'.  To
-- stop further encoding or decoding on an existing 'Handle', use
-- 'hSetBinaryMode'.
--
261 262 263
-- 'hSetEncoding' may need to flush buffered data in order to change
-- the encoding.
--
264 265
hSetEncoding :: Handle -> TextEncoding -> IO ()
hSetEncoding hdl encoding = do
266
  withAllHandles__ "hSetEncoding" hdl $ \h_@Handle__{..} -> do
267
    flushCharBuffer h_
268
    closeTextCodecs h_
269 270
    openTextEncoding (Just encoding) haType $ \ mb_encoder mb_decoder -> do
    bbuf <- readIORef haByteBuffer
Eric Seidel's avatar
Eric Seidel committed
271
    ref <- newIORef (errorWithoutStackTrace "last_decode")
272 273
    return (Handle__{ haLastDecode = ref,
                      haDecoder = mb_decoder,
274
                      haEncoder = mb_encoder,
275
                      haCodec   = Just encoding, .. })
276

277 278 279 280 281 282 283 284 285 286 287 288 289
-- | Return the current 'TextEncoding' for the specified 'Handle', or
-- 'Nothing' if the 'Handle' is in binary mode.
--
-- Note that the 'TextEncoding' remembers nothing about the state of
-- the encoder/decoder in use on this 'Handle'.  For example, if the
-- encoding in use is UTF-16, then using 'hGetEncoding' and
-- 'hSetEncoding' to save and restore the encoding may result in an
-- extra byte-order-mark being written to the file.
--
hGetEncoding :: Handle -> IO (Maybe TextEncoding)
hGetEncoding hdl =
  withHandle_ "hGetEncoding" hdl $ \h_@Handle__{..} -> return haCodec

290 291 292 293 294 295 296 297
-- -----------------------------------------------------------------------------
-- hFlush

-- | The action 'hFlush' @hdl@ causes any items buffered for output
-- in handle @hdl@ to be sent immediately to the operating system.
--
-- This operation may fail with:
--
298
--  * 'System.IO.Error.isFullError' if the device is full;
299
--
300 301 302
--  * 'System.IO.Error.isPermissionError' if a system resource limit would be
--    exceeded. It is unspecified whether the characters in the buffer are
--    discarded or retained under these circumstances.
303

304
hFlush :: Handle -> IO ()
305 306
hFlush handle = wantWritableHandle "hFlush" handle flushWriteBuffer

307 308 309 310 311 312 313 314
-- | The action 'hFlushAll' @hdl@ flushes all buffered data in @hdl@,
-- including any buffered read data.  Buffered read data is flushed
-- by seeking the file position back to the point before the bufferred
-- data was read, and hence only works if @hdl@ is seekable (see
-- 'hIsSeekable').
--
-- This operation may fail with:
--
315
--  * 'System.IO.Error.isFullError' if the device is full;
316
--
317 318 319
--  * 'System.IO.Error.isPermissionError' if a system resource limit would be
--    exceeded. It is unspecified whether the characters in the buffer are
--    discarded or retained under these circumstances;
320
--
321 322
--  * 'System.IO.Error.isIllegalOperation' if @hdl@ has buffered read data, and
--    is not seekable.
323

324
hFlushAll :: Handle -> IO ()
325 326
hFlushAll handle = withHandle_ "hFlushAll" handle flushBuffer

327 328 329 330 331
-- -----------------------------------------------------------------------------
-- Repositioning Handles

data HandlePosn = HandlePosn Handle HandlePosition

332
-- | @since 4.1.0.0
333 334 335
instance Eq HandlePosn where
    (HandlePosn h1 p1) == (HandlePosn h2 p2) = p1==p2 && h1==h2

336
-- | @since 4.1.0.0
337
instance Show HandlePosn where
338
   showsPrec p (HandlePosn h pos) =
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
        showsPrec p h . showString " at position " . shows pos

  -- HandlePosition is the Haskell equivalent of POSIX' off_t.
  -- We represent it as an Integer on the Haskell side, but
  -- cheat slightly in that hGetPosn calls upon a C helper
  -- that reports the position back via (merely) an Int.
type HandlePosition = Integer

-- | Computation 'hGetPosn' @hdl@ returns the current I\/O position of
-- @hdl@ as a value of the abstract type 'HandlePosn'.

hGetPosn :: Handle -> IO HandlePosn
hGetPosn handle = do
    posn <- hTell handle
    return (HandlePosn handle posn)

-- | If a call to 'hGetPosn' @hdl@ returns a position @p@,
-- then computation 'hSetPosn' @p@ sets the position of @hdl@
-- to the position it held at the time of the call to 'hGetPosn'.
--
-- This operation may fail with:
--
361 362
--  * 'System.IO.Error.isPermissionError' if a system resource limit would be
--    exceeded.
363

364
hSetPosn :: HandlePosn -> IO ()
365 366 367 368 369
hSetPosn (HandlePosn h i) = hSeek h AbsoluteSeek i

-- ---------------------------------------------------------------------------
-- hSeek

370
{- Note:
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
 - when seeking using `SeekFromEnd', positive offsets (>=0) means
   seeking at or past EOF.

 - we possibly deviate from the report on the issue of seeking within
   the buffer and whether to flush it or not.  The report isn't exactly
   clear here.
-}

-- | Computation 'hSeek' @hdl mode i@ sets the position of handle
-- @hdl@ depending on @mode@.
-- The offset @i@ is given in terms of 8-bit bytes.
--
-- If @hdl@ is block- or line-buffered, then seeking to a position which is not
-- in the current buffer will first cause any items in the output buffer to be
-- written to the device, and then cause the input buffer to be discarded.
-- Some handles may not be seekable (see 'hIsSeekable'), or only support a
-- subset of the possible positioning operations (for instance, it may only
-- be possible to seek to the end of a tape, or to a positive offset from
-- the beginning or current position).
-- It is not possible to set a negative I\/O position, or for
-- a physical file, an I\/O position beyond the current end-of-file.
--
-- This operation may fail with:
--
395 396
--  * 'System.IO.Error.isIllegalOperationError' if the Handle is not seekable,
--    or does not support the requested seek mode.
Simon Marlow's avatar
Simon Marlow committed
397
--
398 399
--  * 'System.IO.Error.isPermissionError' if a system resource limit would be
--    exceeded.
400

401
hSeek :: Handle -> SeekMode -> Integer -> IO ()
402 403 404 405 406 407 408 409 410 411 412
hSeek handle mode offset =
    wantSeekableHandle "hSeek" handle $ \ handle_@Handle__{..} -> do
    debugIO ("hSeek " ++ show (mode,offset))
    buf <- readIORef haCharBuffer

    if isWriteBuffer buf
        then do flushWriteBuffer handle_
                IODevice.seek haDevice mode offset
        else do

    let r = bufL buf; w = bufR buf
413
    if mode == RelativeSeek && isNothing haDecoder &&
414 415
       offset >= 0 && offset < fromIntegral (w - r)
        then writeIORef haCharBuffer buf{ bufL = r + fromIntegral offset }
416
        else do
417 418 419 420 421 422

    flushCharReadBuffer handle_
    flushByteReadBuffer handle_
    IODevice.seek haDevice mode offset


Simon Marlow's avatar
Simon Marlow committed
423 424 425 426
-- | Computation 'hTell' @hdl@ returns the current position of the
-- handle @hdl@, as the number of bytes from the beginning of
-- the file.  The value returned may be subsequently passed to
-- 'hSeek' to reposition the handle to the current position.
427
--
Simon Marlow's avatar
Simon Marlow committed
428 429
-- This operation may fail with:
--
430
--  * 'System.IO.Error.isIllegalOperationError' if the Handle is not seekable.
Simon Marlow's avatar
Simon Marlow committed
431
--
432
hTell :: Handle -> IO Integer
433
hTell handle =
434 435 436 437
    wantSeekableHandle "hGetPosn" handle $ \ handle_@Handle__{..} -> do

      posn <- IODevice.tell haDevice

438 439 440 441
      -- we can't tell the real byte offset if there are buffered
      -- Chars, so must flush first:
      flushCharBuffer handle_

442 443
      bbuf <- readIORef haByteBuffer

444 445 446
      let real_posn
           | isWriteBuffer bbuf = posn + fromIntegral (bufferElems bbuf)
           | otherwise          = posn - fromIntegral (bufferElems bbuf)
447

448
      cbuf <- readIORef haCharBuffer
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
      debugIO ("\nhGetPosn: (posn, real_posn) = " ++ show (posn, real_posn))
      debugIO ("   cbuf: " ++ summaryBuffer cbuf ++
            "   bbuf: " ++ summaryBuffer bbuf)

      return real_posn

-- -----------------------------------------------------------------------------
-- Handle Properties

-- A number of operations return information about the properties of a
-- handle.  Each of these operations returns `True' if the handle has
-- the specified property, and `False' otherwise.

hIsOpen :: Handle -> IO Bool
hIsOpen handle =
    withHandle_ "hIsOpen" handle $ \ handle_ -> do
465
    case haType handle_ of
466 467 468 469 470 471 472
      ClosedHandle         -> return False
      SemiClosedHandle     -> return False
      _                    -> return True

hIsClosed :: Handle -> IO Bool
hIsClosed handle =
    withHandle_ "hIsClosed" handle $ \ handle_ -> do
473
    case haType handle_ of
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
      ClosedHandle         -> return True
      _                    -> return False

{- not defined, nor exported, but mentioned
   here for documentation purposes:

    hSemiClosed :: Handle -> IO Bool
    hSemiClosed h = do
       ho <- hIsOpen h
       hc <- hIsClosed h
       return (not (ho || hc))
-}

hIsReadable :: Handle -> IO Bool
hIsReadable (DuplexHandle _ _ _) = return True
hIsReadable handle =
    withHandle_ "hIsReadable" handle $ \ handle_ -> do
491
    case haType handle_ of
492
      ClosedHandle         -> ioe_closedHandle
493
      SemiClosedHandle     -> ioe_semiclosedHandle
494 495 496 497 498 499
      htype                -> return (isReadableHandleType htype)

hIsWritable :: Handle -> IO Bool
hIsWritable (DuplexHandle _ _ _) = return True
hIsWritable handle =
    withHandle_ "hIsWritable" handle $ \ handle_ -> do
500
    case haType handle_ of
501
      ClosedHandle         -> ioe_closedHandle
502
      SemiClosedHandle     -> ioe_semiclosedHandle
503 504 505 506 507 508
      htype                -> return (isWritableHandleType htype)

-- | Computation 'hGetBuffering' @hdl@ returns the current buffering mode
-- for @hdl@.

hGetBuffering :: Handle -> IO BufferMode
509
hGetBuffering handle =
510
    withHandle_ "hGetBuffering" handle $ \ handle_ -> do
511
    case haType handle_ of
512
      ClosedHandle         -> ioe_closedHandle
513
      _ ->
514 515 516 517 518 519 520
           -- We're being non-standard here, and allow the buffering
           -- of a semi-closed handle to be queried.   -- sof 6/98
          return (haBufferMode handle_)  -- could be stricter..

hIsSeekable :: Handle -> IO Bool
hIsSeekable handle =
    withHandle_ "hIsSeekable" handle $ \ handle_@Handle__{..} -> do
521
    case haType of
522
      ClosedHandle         -> ioe_closedHandle
523
      SemiClosedHandle     -> ioe_semiclosedHandle
524 525 526 527
      AppendHandle         -> return False
      _                    -> IODevice.isSeekable haDevice

-- -----------------------------------------------------------------------------
thomie's avatar
thomie committed
528
-- Changing echo status
529 530 531 532 533 534 535 536 537 538

-- | Set the echoing status of a handle connected to a terminal.

hSetEcho :: Handle -> Bool -> IO ()
hSetEcho handle on = do
    isT   <- hIsTerminalDevice handle
    if not isT
     then return ()
     else
      withHandle_ "hSetEcho" handle $ \ Handle__{..} -> do
539
      case haType of
540 541 542 543 544 545 546 547 548 549 550 551
         ClosedHandle -> ioe_closedHandle
         _            -> IODevice.setEcho haDevice on

-- | Get the echoing status of a handle connected to a terminal.

hGetEcho :: Handle -> IO Bool
hGetEcho handle = do
    isT   <- hIsTerminalDevice handle
    if not isT
     then return False
     else
       withHandle_ "hGetEcho" handle $ \ Handle__{..} -> do
552
       case haType of
553 554 555 556 557 558 559 560
         ClosedHandle -> ioe_closedHandle
         _            -> IODevice.getEcho haDevice

-- | Is the handle connected to a terminal?

hIsTerminalDevice :: Handle -> IO Bool
hIsTerminalDevice handle = do
    withHandle_ "hIsTerminalDevice" handle $ \ Handle__{..} -> do
561
     case haType of
562 563 564 565 566 567 568 569 570
       ClosedHandle -> ioe_closedHandle
       _            -> IODevice.isTerminal haDevice

-- -----------------------------------------------------------------------------
-- hSetBinaryMode

-- | Select binary mode ('True') or text mode ('False') on a open handle.
-- (See also 'openBinaryFile'.)
--
571
-- This has the same effect as calling 'hSetEncoding' with 'char8', together
572 573 574 575 576
-- with 'hSetNewlineMode' with 'noNewlineTranslation'.
--
hSetBinaryMode :: Handle -> Bool -> IO ()
hSetBinaryMode handle bin =
  withAllHandles__ "hSetBinaryMode" handle $ \ h_@Handle__{..} ->
577
    do
578
         flushCharBuffer h_
579
         closeTextCodecs h_
580

581 582
         mb_te <- if bin then return Nothing
                         else fmap Just getLocaleEncoding
583

584 585
         openTextEncoding mb_te haType $ \ mb_encoder mb_decoder -> do

586 587 588 589
         -- should match the default newline mode, whatever that is
         let nl    | bin       = noNewlineTranslation
                   | otherwise = nativeNewlineMode

590
         bbuf <- readIORef haByteBuffer
Eric Seidel's avatar
Eric Seidel committed
591
         ref <- newIORef (errorWithoutStackTrace "codec_state", bbuf)
592 593

         return Handle__{ haLastDecode = ref,
594
                          haEncoder  = mb_encoder,
595
                          haDecoder  = mb_decoder,
596
                          haCodec    = mb_te,
597 598
                          haInputNL  = inputNL nl,
                          haOutputNL = outputNL nl, .. }
599

600 601 602 603 604 605 606
-- -----------------------------------------------------------------------------
-- hSetNewlineMode

-- | Set the 'NewlineMode' on the specified 'Handle'.  All buffered
-- data is flushed first.
hSetNewlineMode :: Handle -> NewlineMode -> IO ()
hSetNewlineMode handle NewlineMode{ inputNL=i, outputNL=o } =
607
  withAllHandles__ "hSetNewlineMode" handle $ \h_@Handle__{} ->
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
    do
         flushBuffer h_
         return h_{ haInputNL=i, haOutputNL=o }

-- -----------------------------------------------------------------------------
-- Duplicating a Handle

-- | Returns a duplicate of the original handle, with its own buffer.
-- The two Handles will share a file pointer, however.  The original
-- handle's buffer is flushed, including discarding any input data,
-- before the handle is duplicated.

hDuplicate :: Handle -> IO Handle
hDuplicate h@(FileHandle path m) = do
  withHandle_' "hDuplicate" h m $ \h_ ->
      dupHandle path h Nothing h_ (Just handleFinalizer)
hDuplicate h@(DuplexHandle path r w) = do
625
  write_side@(FileHandle _ write_m) <-
626 627
     withHandle_' "hDuplicate" h w $ \h_ ->
        dupHandle path h Nothing h_ (Just handleFinalizer)
628
  read_side@(FileHandle _ read_m) <-
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
    withHandle_' "hDuplicate" h r $ \h_ ->
        dupHandle path h (Just write_m) h_  Nothing
  return (DuplexHandle path read_m write_m)

dupHandle :: FilePath
          -> Handle
          -> Maybe (MVar Handle__)
          -> Handle__
          -> Maybe HandleFinalizer
          -> IO Handle
dupHandle filepath h other_side h_@Handle__{..} mb_finalizer = do
  -- flush the buffer first, so we don't have to copy its contents
  flushBuffer h_
  case other_side of
    Nothing -> do
       new_dev <- IODevice.dup haDevice
       dupHandle_ new_dev filepath other_side h_ mb_finalizer
646
    Just r  ->
647 648 649 650 651 652 653 654 655 656 657
       withHandle_' "dupHandle" h r $ \Handle__{haDevice=dev} -> do
         dupHandle_ dev filepath other_side h_ mb_finalizer

dupHandle_ :: (IODevice dev, BufferedIO dev, Typeable dev) => dev
           -> FilePath
           -> Maybe (MVar Handle__)
           -> Handle__
           -> Maybe HandleFinalizer
           -> IO Handle
dupHandle_ new_dev filepath other_side h_@Handle__{..} mb_finalizer = do
   -- XXX wrong!
658
  mb_codec <- if isJust haEncoder then fmap Just getLocaleEncoding else return Nothing
659 660 661 662 663 664 665 666
  mkHandle new_dev filepath haType True{-buffered-} mb_codec
      NewlineMode { inputNL = haInputNL, outputNL = haOutputNL }
      mb_finalizer other_side

-- -----------------------------------------------------------------------------
-- Replacing a Handle

{- |
667
Makes the second handle a duplicate of the first handle.  The second
668 669 670 671 672 673 674 675 676 677 678
handle will be closed first, if it is not already.

This can be used to retarget the standard Handles, for example:

> do h <- openFile "mystdout" WriteMode
>    hDuplicateTo h stdout
-}

hDuplicateTo :: Handle -> Handle -> IO ()
hDuplicateTo h1@(FileHandle path m1) h2@(FileHandle _ m2)  = do
 withHandle__' "hDuplicateTo" h2 m2 $ \h2_ -> do
679
   try $ flushWriteBuffer h2_
680 681 682 683
   withHandle_' "hDuplicateTo" h1 m1 $ \h1_ -> do
     dupHandleTo path h1 Nothing h2_ h1_ (Just handleFinalizer)
hDuplicateTo h1@(DuplexHandle path r1 w1) h2@(DuplexHandle _ r2 w2)  = do
 withHandle__' "hDuplicateTo" h2 w2  $ \w2_ -> do
684
   try $ flushWriteBuffer w2_
685 686 687
   withHandle_' "hDuplicateTo" h1 w1 $ \w1_ -> do
     dupHandleTo path h1 Nothing w2_ w1_ (Just handleFinalizer)
 withHandle__' "hDuplicateTo" h2 r2  $ \r2_ -> do
688
   try $ flushWriteBuffer r2_
689 690
   withHandle_' "hDuplicateTo" h1 r1 $ \r1_ -> do
     dupHandleTo path h1 (Just w1) r2_ r1_ Nothing
691
hDuplicateTo h1 _ =
692 693
  ioe_dupHandlesNotCompatible h1

694 695
try :: IO () -> IO ()
try io = io `catchException` (const (pure ()) :: SomeException -> IO ())
696 697 698

ioe_dupHandlesNotCompatible :: Handle -> IO a
ioe_dupHandlesNotCompatible h =
699
   ioException (IOError (Just h) IllegalOperation "hDuplicateTo"
700 701
                "handles are incompatible" Nothing Nothing)

702
dupHandleTo :: FilePath
703 704 705 706 707 708
            -> Handle
            -> Maybe (MVar Handle__)
            -> Handle__
            -> Handle__
            -> Maybe HandleFinalizer
            -> IO Handle__
709
dupHandleTo filepath h other_side
710
            hto_@Handle__{haDevice=devTo}
711 712 713 714
            h_@Handle__{haDevice=dev} mb_finalizer = do
  flushBuffer h_
  case cast devTo of
    Nothing   -> ioe_dupHandlesNotCompatible h
715
    Just dev' -> do
716
      _ <- IODevice.dup2 dev dev'
717 718 719 720 721 722 723 724 725 726 727 728 729 730
      FileHandle _ m <- dupHandle_ dev' filepath other_side h_ mb_finalizer
      takeMVar m

-- ---------------------------------------------------------------------------
-- showing Handles.
--
-- | 'hShow' is in the 'IO' monad, and gives more comprehensive output
-- than the (pure) instance of 'Show' for 'Handle'.

hShow :: Handle -> IO String
hShow h@(FileHandle path _) = showHandle' path False h
hShow h@(DuplexHandle path _ _) = showHandle' path True h

showHandle' :: String -> Bool -> Handle -> IO String
731
showHandle' filepath is_duplex h =
732 733 734 735 736
  withHandle_ "showHandle" h $ \hdl_ ->
    let
     showType | is_duplex = showString "duplex (read-write)"
              | otherwise = shows (haType hdl_)
    in
737 738 739
    return
      (( showChar '{' .
        showHdl (haType hdl_)
740 741 742 743 744 745 746
            (showString "loc=" . showString filepath . showChar ',' .
             showString "type=" . showType . showChar ',' .
             showString "buffering=" . showBufMode (unsafePerformIO (readIORef (haCharBuffer hdl_))) (haBufferMode hdl_) . showString "}" )
      ) "")
   where

    showHdl :: HandleType -> ShowS -> ShowS
747
    showHdl ht cont =
748 749 750 751 752 753 754 755 756 757 758 759
       case ht of
        ClosedHandle  -> shows ht . showString "}"
        _ -> cont

    showBufMode :: Buffer e -> BufferMode -> ShowS
    showBufMode buf bmo =
      case bmo of
        NoBuffering   -> showString "none"
        LineBuffering -> showString "line"
        BlockBuffering (Just n) -> showString "block " . showParen True (shows n)
        BlockBuffering Nothing  -> showString "block " . showParen True (shows def)
      where
760
       def :: Int
761
       def = bufSize buf
dterei's avatar
dterei committed
762