Wolfgang Jeltsch pushed to branch wip/jeltsch/text-read-uncovering at Glasgow Haskell Compiler / GHC

Commits:

21 changed files:

Changes:

  • libraries/base/src/Data/Data.hs
    ... ... @@ -99,3 +99,38 @@ module Data.Data (
    99 99
     
    
    100 100
     import GHC.Internal.Data.Data
    
    101 101
     import Data.Typeable
    
    102
    +
    
    103
    +import GHC.Real (toRational)
    
    104
    +import GHC.Float (Double)
    
    105
    +import Data.Eq ((==))
    
    106
    +import Data.Function ((.))
    
    107
    +import Data.Maybe (Maybe (Nothing, Just))
    
    108
    +import Data.List (filter)
    
    109
    +import Data.String (String)
    
    110
    +import Text.Read (Read, reads)
    
    111
    +
    
    112
    +-- | Lookup a constructor via a string
    
    113
    +readConstr :: DataType -> String -> Maybe Constr
    
    114
    +readConstr dt str =
    
    115
    +      case dataTypeRep dt of
    
    116
    +        AlgRep cons -> idx cons
    
    117
    +        IntRep      -> mkReadCon (\i -> (mkPrimCon dt str (IntConstr i)))
    
    118
    +        FloatRep    -> mkReadCon ffloat
    
    119
    +        CharRep     -> mkReadCon (\c -> (mkPrimCon dt str (CharConstr c)))
    
    120
    +        NoRep       -> Nothing
    
    121
    +  where
    
    122
    +
    
    123
    +    -- Read a value and build a constructor
    
    124
    +    mkReadCon :: Read t => (t -> Constr) -> Maybe Constr
    
    125
    +    mkReadCon f = case (reads str) of
    
    126
    +                    [(t,"")] -> Just (f t)
    
    127
    +                    _ -> Nothing
    
    128
    +
    
    129
    +    -- Traverse list of algebraic datatype constructors
    
    130
    +    idx :: [Constr] -> Maybe Constr
    
    131
    +    idx cons = case filter ((==) str . showConstr) cons of
    
    132
    +                [] -> Nothing
    
    133
    +                hd : _ -> Just hd
    
    134
    +
    
    135
    +    ffloat :: Double -> Constr
    
    136
    +    ffloat =  mkPrimCon dt str . FloatConstr . toRational

  • libraries/base/src/Data/Version.hs
    1 1
     {-# LANGUAGE Safe #-}
    
    2 2
     
    
    3
    +{-# LANGUAGE StandaloneDeriving #-}
    
    4
    +
    
    3 5
     -- |
    
    4 6
     -- Module      :  Data.Version
    
    5 7
     -- Copyright   :  (c) The University of Glasgow 2004
    
    ... ... @@ -33,3 +35,25 @@ module Data.Version (
    33 35
           ) where
    
    34 36
     
    
    35 37
     import GHC.Internal.Data.Version
    
    38
    +
    
    39
    +import Control.Applicative (pure, (*>))
    
    40
    +import Data.Functor (fmap)
    
    41
    +import Data.Char (isDigit, isAlphaNum)
    
    42
    +import Text.ParserCombinators.ReadP (ReadP, char, munch1, sepBy1, many)
    
    43
    +import Text.Read (Read, read)
    
    44
    +
    
    45
    +{-NOTE:
    
    46
    +    The following instance is technically an orphan, but practically it is not,
    
    47
    +    since ordinary users should not use @ghc-internal@ directly and thus get
    
    48
    +    'Version' only through this module.
    
    49
    +-}
    
    50
    +
    
    51
    +-- | @since base-2.01
    
    52
    +deriving instance Read Version
    
    53
    +
    
    54
    +-- | A parser for versions in the format produced by 'showVersion'.
    
    55
    +--
    
    56
    +parseVersion :: ReadP Version
    
    57
    +parseVersion = do branch <- sepBy1 (fmap read (munch1 isDigit)) (char '.')
    
    58
    +                  tags   <- many (char '-' *> munch1 isAlphaNum)
    
    59
    +                  pure Version{versionBranch=branch, versionTags=tags}

  • libraries/base/src/GHC/ByteOrder.hs
    1 1
     {-# LANGUAGE Safe #-}
    
    2 2
     
    
    3
    +{-# LANGUAGE StandaloneDeriving #-}
    
    4
    +
    
    3 5
     -- |
    
    4 6
     --
    
    5 7
     -- Module      :  GHC.ByteOrder
    
    ... ... @@ -19,4 +21,15 @@ module GHC.ByteOrder
    19 21
          targetByteOrder
    
    20 22
          ) where
    
    21 23
     
    
    22
    -import GHC.Internal.ByteOrder
    \ No newline at end of file
    24
    +import GHC.Internal.ByteOrder
    
    25
    +
    
    26
    +import Text.Read
    
    27
    +
    
    28
    +{-NOTE:
    
    29
    +    The following instance is technically an orphan, but practically it is not,
    
    30
    +    since ordinary users should not use @ghc-internal@ directly and thus get
    
    31
    +    'ByteOrder' only through this module.
    
    32
    +-}
    
    33
    +
    
    34
    +-- | @since base-4.11.0.0
    
    35
    +deriving instance Read ByteOrder

  • libraries/base/src/GHC/IO/Handle.hs
    ... ... @@ -53,6 +53,7 @@ module GHC.IO.Handle
    53 53
          hGetEcho,
    
    54 54
          hIsTerminalDevice,
    
    55 55
          hSetNewlineMode,
    
    56
    +     hGetNewlineMode,
    
    56 57
          Newline(..),
    
    57 58
          NewlineMode(..),
    
    58 59
          nativeNewline,
    

  • libraries/base/src/Numeric.hs
    1
    -{-# LANGUAGE Safe #-}
    
    1
    +{-# LANGUAGE Trustworthy #-}
    
    2
    +{-# LANGUAGE MagicHash #-}
    
    3
    +{-# LANGUAGE ImportQualifiedPost #-}
    
    2 4
     
    
    3 5
     -- |
    
    4 6
     --
    
    ... ... @@ -48,3 +50,279 @@ module Numeric
    48 50
          ) where
    
    49 51
     
    
    50 52
     import GHC.Internal.Numeric
    
    53
    +
    
    54
    +import GHC.Types (Char (C#))
    
    55
    +import GHC.Err (error, errorWithoutStackTrace)
    
    56
    +import GHC.Base (unsafeChr)
    
    57
    +import GHC.Num (Num, (+), (-), (*))
    
    58
    +import GHC.Real
    
    59
    +       (
    
    60
    +           Integral,
    
    61
    +           Real,
    
    62
    +           RealFrac,
    
    63
    +           fromIntegral,
    
    64
    +           fromRational,
    
    65
    +           quotRem,
    
    66
    +           showSigned
    
    67
    +       )
    
    68
    +import GHC.Float
    
    69
    +       (
    
    70
    +           Floating (..),
    
    71
    +           RealFloat,
    
    72
    +           Float,
    
    73
    +           Double,
    
    74
    +           isNegativeZero,
    
    75
    +           isInfinite,
    
    76
    +           isNaN,
    
    77
    +           fromRat,
    
    78
    +           floatToDigits,
    
    79
    +           FFFormat (FFExponent, FFFixed, FFGeneric),
    
    80
    +           formatRealFloat,
    
    81
    +           formatRealFloatAlt,
    
    82
    +           showFloat
    
    83
    +       )
    
    84
    +import GHC.Read (lexDigits)
    
    85
    +import Control.Monad (return)
    
    86
    +import Data.Eq (Eq, (==))
    
    87
    +import Data.Ord ((<))
    
    88
    +import Data.Function (($), (.))
    
    89
    +import Data.Bool (Bool (False, True), otherwise, (||), (&&))
    
    90
    +import Data.Maybe (Maybe)
    
    91
    +import Data.List ((++))
    
    92
    +import Data.Char (ord, intToDigit)
    
    93
    +import Data.Int (Int)
    
    94
    +import Text.ParserCombinators.ReadP (ReadP, pfail, readP_to_S)
    
    95
    +import Text.Read (ReadS, readParen, lex)
    
    96
    +import Text.Read.Lex qualified as L
    
    97
    +       (
    
    98
    +           Lexeme (Number),
    
    99
    +           lex,
    
    100
    +           numberToRational,
    
    101
    +           readIntP,
    
    102
    +           readBinP,
    
    103
    +           readOctP,
    
    104
    +           readDecP,
    
    105
    +           readHexP
    
    106
    +       )
    
    107
    +import Text.Show (ShowS, show, showString)
    
    108
    +
    
    109
    +-- $setup
    
    110
    +-- >>> import Prelude
    
    111
    +
    
    112
    +-- -----------------------------------------------------------------------------
    
    113
    +-- Reading
    
    114
    +
    
    115
    +-- | Reads an /unsigned/ integral value in an arbitrary base.
    
    116
    +readInt :: Num a
    
    117
    +  => a                  -- ^ the base
    
    118
    +  -> (Char -> Bool)     -- ^ a predicate distinguishing valid digits in this base
    
    119
    +  -> (Char -> Int)      -- ^ a function converting a valid digit character to an 'Int'
    
    120
    +  -> ReadS a
    
    121
    +readInt base isDigit valDigit = readP_to_S (L.readIntP base isDigit valDigit)
    
    122
    +
    
    123
    +-- | Read an unsigned number in binary notation.
    
    124
    +--
    
    125
    +-- >>> readBin "10011"
    
    126
    +-- [(19,"")]
    
    127
    +readBin :: (Eq a, Num a) => ReadS a
    
    128
    +readBin = readP_to_S L.readBinP
    
    129
    +
    
    130
    +-- | Read an unsigned number in octal notation.
    
    131
    +--
    
    132
    +-- >>> readOct "0644"
    
    133
    +-- [(420,"")]
    
    134
    +readOct :: (Eq a, Num a) => ReadS a
    
    135
    +readOct = readP_to_S L.readOctP
    
    136
    +
    
    137
    +-- | Read an unsigned number in decimal notation.
    
    138
    +--
    
    139
    +-- >>> readDec "0644"
    
    140
    +-- [(644,"")]
    
    141
    +readDec :: (Eq a, Num a) => ReadS a
    
    142
    +readDec = readP_to_S L.readDecP
    
    143
    +
    
    144
    +-- | Read an unsigned number in hexadecimal notation.
    
    145
    +-- Both upper or lower case letters are allowed.
    
    146
    +--
    
    147
    +-- >>> readHex "deadbeef"
    
    148
    +-- [(3735928559,"")]
    
    149
    +readHex :: (Eq a, Num a) => ReadS a
    
    150
    +readHex = readP_to_S L.readHexP
    
    151
    +
    
    152
    +-- | Reads an /unsigned/ 'RealFrac' value,
    
    153
    +-- expressed in decimal scientific notation.
    
    154
    +--
    
    155
    +-- Note that this function takes time linear in the magnitude of its input
    
    156
    +-- which can scale exponentially with input size (e.g. @"1e100000000"@ is a
    
    157
    +-- very large number while having a very small textual form).
    
    158
    +-- For this reason, users should take care to avoid using this function on
    
    159
    +-- untrusted input. Users needing to parse floating point values
    
    160
    +-- (e.g. 'Float') are encouraged to instead use 'read', which does
    
    161
    +-- not suffer from this issue.
    
    162
    +readFloat :: RealFrac a => ReadS a
    
    163
    +readFloat = readP_to_S readFloatP
    
    164
    +
    
    165
    +readFloatP :: RealFrac a => ReadP a
    
    166
    +readFloatP =
    
    167
    +  do tok <- L.lex
    
    168
    +     case tok of
    
    169
    +       L.Number n -> return $ fromRational $ L.numberToRational n
    
    170
    +       _          -> pfail
    
    171
    +
    
    172
    +-- It's turgid to have readSigned work using list comprehensions,
    
    173
    +-- but it's specified as a ReadS to ReadS transformer
    
    174
    +-- With a bit of luck no one will use it.
    
    175
    +
    
    176
    +-- | Reads a /signed/ 'Real' value, given a reader for an unsigned value.
    
    177
    +readSigned :: (Real a) => ReadS a -> ReadS a
    
    178
    +readSigned readPos = readParen False read'
    
    179
    +                     where read' r  = read'' r ++
    
    180
    +                                      (do
    
    181
    +                                        ("-",s) <- lex r
    
    182
    +                                        (x,t)   <- read'' s
    
    183
    +                                        return (-x,t))
    
    184
    +                           read'' r = do
    
    185
    +                               (str,s) <- lex r
    
    186
    +                               (n,"")  <- readPos str
    
    187
    +                               return (n,s)
    
    188
    +
    
    189
    +-- -----------------------------------------------------------------------------
    
    190
    +-- Showing
    
    191
    +
    
    192
    +-- | Show /non-negative/ 'Integral' numbers in base 10.
    
    193
    +showInt :: Integral a => a -> ShowS
    
    194
    +showInt n0 cs0
    
    195
    +    | n0 < 0    = errorWithoutStackTrace "GHC.Internal.Numeric.showInt: can't show negative numbers"
    
    196
    +    | otherwise = go n0 cs0
    
    197
    +    where
    
    198
    +    go n cs
    
    199
    +        | n < 10    = case unsafeChr (ord '0' + fromIntegral n) of
    
    200
    +            c@(C# _) -> c:cs
    
    201
    +        | otherwise = case unsafeChr (ord '0' + fromIntegral r) of
    
    202
    +            c@(C# _) -> go q (c:cs)
    
    203
    +        where
    
    204
    +        (q,r) = n `quotRem` 10
    
    205
    +
    
    206
    +-- Controlling the format and precision of floats. The code that
    
    207
    +-- implements the formatting itself is in @PrelNum@ to avoid
    
    208
    +-- mutual module deps.
    
    209
    +
    
    210
    +{-# SPECIALIZE showEFloat ::
    
    211
    +        Maybe Int -> Float  -> ShowS #-}
    
    212
    +{-# SPECIALIZE showEFloat ::
    
    213
    +        Maybe Int -> Double -> ShowS #-}
    
    214
    +{-# SPECIALIZE showFFloat ::
    
    215
    +        Maybe Int -> Float  -> ShowS #-}
    
    216
    +{-# SPECIALIZE showFFloat ::
    
    217
    +        Maybe Int -> Double -> ShowS #-}
    
    218
    +{-# SPECIALIZE showGFloat ::
    
    219
    +        Maybe Int -> Float  -> ShowS #-}
    
    220
    +{-# SPECIALIZE showGFloat ::
    
    221
    +        Maybe Int -> Double -> ShowS #-}
    
    222
    +
    
    223
    +-- | Show a signed 'RealFloat' value
    
    224
    +-- using scientific (exponential) notation (e.g. @2.45e2@, @1.5e-3@).
    
    225
    +--
    
    226
    +-- In the call @'showEFloat' digs val@, if @digs@ is 'Nothing',
    
    227
    +-- the value is shown to full precision; if @digs@ is @'Just' d@,
    
    228
    +-- then at most @d@ digits after the decimal point are shown.
    
    229
    +showEFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
    
    230
    +
    
    231
    +-- | Show a signed 'RealFloat' value
    
    232
    +-- using standard decimal notation (e.g. @245000@, @0.0015@).
    
    233
    +--
    
    234
    +-- In the call @'showFFloat' digs val@, if @digs@ is 'Nothing',
    
    235
    +-- the value is shown to full precision; if @digs@ is @'Just' d@,
    
    236
    +-- then at most @d@ digits after the decimal point are shown.
    
    237
    +showFFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
    
    238
    +
    
    239
    +-- | Show a signed 'RealFloat' value
    
    240
    +-- using standard decimal notation for arguments whose absolute value lies
    
    241
    +-- between @0.1@ and @9,999,999@, and scientific notation otherwise.
    
    242
    +--
    
    243
    +-- In the call @'showGFloat' digs val@, if @digs@ is 'Nothing',
    
    244
    +-- the value is shown to full precision; if @digs@ is @'Just' d@,
    
    245
    +-- then at most @d@ digits after the decimal point are shown.
    
    246
    +showGFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
    
    247
    +
    
    248
    +showEFloat d x =  showString (formatRealFloat FFExponent d x)
    
    249
    +showFFloat d x =  showString (formatRealFloat FFFixed d x)
    
    250
    +showGFloat d x =  showString (formatRealFloat FFGeneric d x)
    
    251
    +
    
    252
    +-- | Show a signed 'RealFloat' value
    
    253
    +-- using standard decimal notation (e.g. @245000@, @0.0015@).
    
    254
    +--
    
    255
    +-- This behaves as 'showFFloat', except that a decimal point
    
    256
    +-- is always guaranteed, even if not needed.
    
    257
    +--
    
    258
    +-- @since base-4.7.0.0
    
    259
    +showFFloatAlt    :: (RealFloat a) => Maybe Int -> a -> ShowS
    
    260
    +
    
    261
    +-- | Show a signed 'RealFloat' value
    
    262
    +-- using standard decimal notation for arguments whose absolute value lies
    
    263
    +-- between @0.1@ and @9,999,999@, and scientific notation otherwise.
    
    264
    +--
    
    265
    +-- This behaves as 'showFFloat', except that a decimal point
    
    266
    +-- is always guaranteed, even if not needed.
    
    267
    +--
    
    268
    +-- @since base-4.7.0.0
    
    269
    +showGFloatAlt    :: (RealFloat a) => Maybe Int -> a -> ShowS
    
    270
    +
    
    271
    +showFFloatAlt d x =  showString (formatRealFloatAlt FFFixed d True x)
    
    272
    +showGFloatAlt d x =  showString (formatRealFloatAlt FFGeneric d True x)
    
    273
    +
    
    274
    +{- | Show a floating-point value in the hexadecimal format,
    
    275
    +similar to the @%a@ specifier in C's printf.
    
    276
    +
    
    277
    +  >>> showHFloat (212.21 :: Double) ""
    
    278
    +  "0x1.a86b851eb851fp7"
    
    279
    +  >>> showHFloat (-12.76 :: Float) ""
    
    280
    +  "-0x1.9851ecp3"
    
    281
    +  >>> showHFloat (-0 :: Double) ""
    
    282
    +  "-0x0p+0"
    
    283
    +
    
    284
    +@since base-4.11.0.0
    
    285
    +-}
    
    286
    +showHFloat :: RealFloat a => a -> ShowS
    
    287
    +showHFloat = showString . fmt
    
    288
    +  where
    
    289
    +  fmt x
    
    290
    +    | isNaN x                   = "NaN"
    
    291
    +    | isInfinite x              = (if x < 0 then "-" else "") ++ "Infinity"
    
    292
    +    | x < 0 || isNegativeZero x = '-' : cvt (-x)
    
    293
    +    | otherwise                 = cvt x
    
    294
    +
    
    295
    +  cvt x
    
    296
    +    | x == 0 = "0x0p+0"
    
    297
    +    | otherwise =
    
    298
    +      case floatToDigits 2 x of
    
    299
    +        r@([], _) -> error $ "Impossible happened: showHFloat: " ++ show r
    
    300
    +        (d:ds, e) -> "0x" ++ show d ++ frac ds ++ "p" ++ show (e-1)
    
    301
    +
    
    302
    +  -- Given binary digits, convert them to hex in blocks of 4
    
    303
    +  -- Special case: If all 0's, just drop it.
    
    304
    +  frac digits
    
    305
    +    | allZ digits = ""
    
    306
    +    | otherwise   = "." ++ hex digits
    
    307
    +    where
    
    308
    +    hex ds =
    
    309
    +      case ds of
    
    310
    +        []                -> ""
    
    311
    +        [a]               -> hexDigit a 0 0 0 ""
    
    312
    +        [a,b]             -> hexDigit a b 0 0 ""
    
    313
    +        [a,b,c]           -> hexDigit a b c 0 ""
    
    314
    +        a : b : c : d : r -> hexDigit a b c d (hex r)
    
    315
    +
    
    316
    +  hexDigit a b c d = showHex (8*a + 4*b + 2*c + d)
    
    317
    +
    
    318
    +  allZ xs = case xs of
    
    319
    +              x : more -> x == 0 && allZ more
    
    320
    +              []       -> True
    
    321
    +
    
    322
    +-- | Show /non-negative/ 'Integral' numbers in base 8.
    
    323
    +showOct :: Integral a => a -> ShowS
    
    324
    +showOct = showIntAtBase 8  intToDigit
    
    325
    +
    
    326
    +-- | Show /non-negative/ 'Integral' numbers in base 2.
    
    327
    +showBin :: Integral a => a -> ShowS
    
    328
    +showBin = showIntAtBase 2  intToDigit

  • libraries/base/src/Prelude.hs
    ... ... @@ -165,7 +165,7 @@ module Prelude (
    165 165
       ) where
    
    166 166
     
    
    167 167
     import GHC.Internal.Control.Monad
    
    168
    -import GHC.Internal.System.IO
    
    168
    +import System.IO
    
    169 169
     import GHC.Internal.System.IO.Error
    
    170 170
     import qualified GHC.Internal.Data.List as List
    
    171 171
     import GHC.Internal.Data.Either
    

  • libraries/base/src/System/IO.hs
    1
    -{-# LANGUAGE Safe #-}
    
    1
    +{-# LANGUAGE Trustworthy #-}
    
    2
    +{-# LANGUAGE CPP #-}
    
    3
    +{-# LANGUAGE StandaloneDeriving #-}
    
    2 4
     
    
    3 5
     -- |
    
    4 6
     --
    
    ... ... @@ -184,9 +186,701 @@ module System.IO
    184 186
          nativeNewlineMode
    
    185 187
          ) where
    
    186 188
     
    
    187
    -import GHC.Internal.System.IO
    
    189
    +import Control.Monad (return, (>>=))
    
    190
    +import Control.Exception (ioError)
    
    191
    +import Data.Eq ((==))
    
    192
    +import Data.Ord ((<))
    
    193
    +import Data.Bits ((.|.))
    
    194
    +import Data.Function (($), (.))
    
    195
    +import Data.Maybe (Maybe (Nothing, Just))
    
    196
    +import Data.Char (Char)
    
    197
    +import Data.String (String)
    
    198
    +import Data.Int (Int)
    
    199
    +import Data.IORef (IORef, newIORef)
    
    200
    +import System.IO.Error (userError)
    
    201
    +import System.IO.Unsafe (unsafePerformIO)
    
    202
    +import System.Posix.Internals
    
    203
    +       (
    
    204
    +           c_getpid,
    
    205
    +           c_open,
    
    206
    +           o_CREAT,
    
    207
    +           o_EXCL,
    
    208
    +           o_BINARY,
    
    209
    +           o_NONBLOCK,
    
    210
    +           o_RDWR,
    
    211
    +           o_NOCTTY,
    
    212
    +           withFilePath
    
    213
    +       )
    
    214
    +import System.Posix.Types (CMode)
    
    215
    +import Text.Read (lex, Read, reads)
    
    216
    +import Text.Show (Show, show)
    
    217
    +import Foreign.C.Types (CInt)
    
    218
    +import Foreign.C.Error (Errno, eEXIST, getErrno, errnoToIOError)
    
    219
    +import GHC.Base (Bool (False, True), otherwise, failIO)
    
    220
    +import GHC.Err (errorWithoutStackTrace)
    
    221
    +import GHC.List (null, elem, last, (++), reverse, break)
    
    222
    +import GHC.Num ((+))
    
    223
    +import GHC.IO (IO, FilePath)
    
    224
    +import GHC.IO.IOMode (IOMode (ReadMode, WriteMode, ReadWriteMode, AppendMode))
    
    225
    +import qualified GHC.Internal.IO.FD as FD
    
    226
    +import GHC.IO.Encoding
    
    227
    +       (
    
    228
    +           TextEncoding,
    
    229
    +           mkTextEncoding,
    
    230
    +           getLocaleEncoding,
    
    231
    +           initLocaleEncoding,
    
    232
    +           utf8,
    
    233
    +           utf8_bom,
    
    234
    +           utf16,
    
    235
    +           utf16be,
    
    236
    +           utf16le,
    
    237
    +           utf32,
    
    238
    +           utf32be,
    
    239
    +           utf32le,
    
    240
    +           latin1,
    
    241
    +           char8
    
    242
    +       )
    
    243
    +import GHC.IO.Handle
    
    244
    +       (
    
    245
    +           Handle,
    
    246
    +           hLookAhead,
    
    247
    +           hFlush,
    
    248
    +           hClose,
    
    249
    +           hSetBinaryMode,
    
    250
    +           hSetEncoding,
    
    251
    +           hSetNewlineMode,
    
    252
    +           hSetEcho,
    
    253
    +           hSetFileSize,
    
    254
    +           hGetEncoding,
    
    255
    +           hGetNewlineMode,
    
    256
    +           hGetEcho,
    
    257
    +           hFileSize,
    
    258
    +           hIsOpen,
    
    259
    +           hIsReadable,
    
    260
    +           hIsSeekable,
    
    261
    +           hIsWritable,
    
    262
    +           hIsTerminalDevice,
    
    263
    +           hIsEOF,
    
    264
    +           hIsClosed,
    
    265
    +           hShow,
    
    266
    +           BufferMode (NoBuffering, LineBuffering, BlockBuffering),
    
    267
    +           hSetBuffering,
    
    268
    +           hGetBuffering,
    
    269
    +           HandlePosn,
    
    270
    +           hSetPosn,
    
    271
    +           hGetPosn,
    
    272
    +           SeekMode (AbsoluteSeek, RelativeSeek, SeekFromEnd),
    
    273
    +           hSeek,
    
    274
    +           hTell,
    
    275
    +           Newline (LF, CRLF),
    
    276
    +           nativeNewline,
    
    277
    +           NewlineMode (NewlineMode, inputNL, outputNL),
    
    278
    +           noNewlineTranslation,
    
    279
    +           nativeNewlineMode,
    
    280
    +           universalNewlineMode,
    
    281
    +           isEOF
    
    282
    +       )
    
    283
    +import GHC.IO.Handle.Text
    
    284
    +       (
    
    285
    +           hPutChar,
    
    286
    +           hPutStr,
    
    287
    +           hPutStrLn,
    
    288
    +           hPutBuf,
    
    289
    +           hPutBufNonBlocking,
    
    290
    +           hGetChar,
    
    291
    +           hGetContents,
    
    292
    +           hGetContents',
    
    293
    +           hGetLine,
    
    294
    +           hGetBuf,
    
    295
    +           hGetBufNonBlocking,
    
    296
    +           hGetBufSome,
    
    297
    +           hWaitForInput
    
    298
    +       )
    
    299
    +import qualified GHC.Internal.IO.Handle.FD as POSIX
    
    300
    +import GHC.IO.StdHandles
    
    301
    +       (
    
    302
    +           openBinaryFile,
    
    303
    +           withBinaryFile,
    
    304
    +           openFile,
    
    305
    +           withFile,
    
    306
    +           stdin,
    
    307
    +           stdout,
    
    308
    +           stderr
    
    309
    +       )
    
    310
    +import GHC.IORef (atomicModifyIORef'_)
    
    188 311
     import GHC.Internal.Control.Monad.Fix (fixIO)
    
    189 312
     
    
    313
    +#if defined(mingw32_HOST_OS)
    
    314
    +import Foreign.C.String
    
    315
    +import Foreign.Ptr
    
    316
    +import Foreign.Marshal.Alloc
    
    317
    +import Foreign.Marshal.Utils (with)
    
    318
    +import Foreign.Storable
    
    319
    +import GHC.IO.SubSystem
    
    320
    +import GHC.IO.Windows.Handle (openFileAsTemp)
    
    321
    +import GHC.IO.Handle.Windows (mkHandleFromHANDLE)
    
    322
    +import GHC.IO.Device as IODevice
    
    323
    +import GHC.Internal.Real (fromIntegral)
    
    324
    +#endif
    
    325
    +
    
    326
    +-----------------------------------------------------------------------------
    
    327
    +-- Standard IO
    
    328
    +
    
    329
    +-- | Write a character to the standard output device
    
    330
    +--
    
    331
    +-- 'putChar' is implemented as @'hPutChar' 'stdout'@.
    
    332
    +--
    
    333
    +-- This operation may fail with the same errors as 'hPutChar'.
    
    334
    +--
    
    335
    +-- ==== __Examples__
    
    336
    +--
    
    337
    +-- Note that the following do not put a newline.
    
    338
    +--
    
    339
    +-- >>> putChar 'x'
    
    340
    +-- x
    
    341
    +--
    
    342
    +-- >>> putChar '\0042'
    
    343
    +-- *
    
    344
    +putChar         :: Char -> IO ()
    
    345
    +putChar c       =  hPutChar stdout c
    
    346
    +
    
    347
    +-- | Write a string to the standard output device
    
    348
    +--
    
    349
    +-- 'putStr' is implemented as @'hPutStr' 'stdout'@.
    
    350
    +--
    
    351
    +-- This operation may fail with the same errors, and has the same issues with concurrency, as 'hPutStr'!
    
    352
    +--
    
    353
    +-- ==== __Examples__
    
    354
    +--
    
    355
    +-- Note that the following do not put a newline.
    
    356
    +--
    
    357
    +-- >>> putStr "Hello, World!"
    
    358
    +-- Hello, World!
    
    359
    +--
    
    360
    +-- >>> putStr "\0052\0042\0050"
    
    361
    +-- 4*2
    
    362
    +--
    
    363
    +putStr          :: String -> IO ()
    
    364
    +putStr s        =  hPutStr stdout s
    
    365
    +
    
    366
    +-- | The same as 'putStr', but adds a newline character.
    
    367
    +--
    
    368
    +-- This operation may fail with the same errors, and has the same issues with concurrency, as 'hPutStr'!
    
    369
    +putStrLn        :: String -> IO ()
    
    370
    +putStrLn s      =  hPutStrLn stdout s
    
    371
    +
    
    372
    +-- | The 'print' function outputs a value of any printable type to the
    
    373
    +-- standard output device.
    
    374
    +-- Printable types are those that are instances of class 'Show'; 'print'
    
    375
    +-- converts values to strings for output using the 'show' operation and
    
    376
    +-- adds a newline.
    
    377
    +--
    
    378
    +-- 'print' is implemented as @'putStrLn' '.' 'show'@
    
    379
    +--
    
    380
    +-- This operation may fail with the same errors, and has the same issues with concurrency, as 'hPutStr'!
    
    381
    +--
    
    382
    +-- ==== __Examples__
    
    383
    +--
    
    384
    +-- >>> print [1, 2, 3]
    
    385
    +-- [1,2,3]
    
    386
    +--
    
    387
    +-- Be careful when using 'print' for outputting strings,
    
    388
    +-- as this will invoke 'show' and cause strings to be printed
    
    389
    +-- with quotation marks and non-ascii symbols escaped.
    
    390
    +--
    
    391
    +-- >>> print "λ :D"
    
    392
    +-- "\995 :D"
    
    393
    +--
    
    394
    +-- A program to print the first 8 integers and their
    
    395
    +-- powers of 2 could be written as:
    
    396
    +--
    
    397
    +-- >>> print [(n, 2^n) | n <- [0..8]]
    
    398
    +-- [(0,1),(1,2),(2,4),(3,8),(4,16),(5,32),(6,64),(7,128),(8,256)]
    
    399
    +print           :: Show a => a -> IO ()
    
    400
    +print x         =  putStrLn (show x)
    
    401
    +
    
    402
    +-- | Read a single character from the standard input device.
    
    403
    +--
    
    404
    +-- 'getChar' is implemented as @'hGetChar' 'stdin'@.
    
    405
    +--
    
    406
    +-- This operation may fail with the same errors as 'hGetChar'.
    
    407
    +--
    
    408
    +-- ==== __Examples__
    
    409
    +--
    
    410
    +-- >>> getChar
    
    411
    +-- a'a'
    
    412
    +--
    
    413
    +-- >>> getChar
    
    414
    +-- >
    
    415
    +-- '\n'
    
    416
    +getChar         :: IO Char
    
    417
    +getChar         =  hGetChar stdin
    
    418
    +
    
    419
    +-- | Read a line from the standard input device.
    
    420
    +--
    
    421
    +-- 'getLine' is implemented as @'hGetLine' 'stdin'@.
    
    422
    +--
    
    423
    +-- This operation may fail with the same errors as 'hGetLine'.
    
    424
    +--
    
    425
    +-- ==== __Examples__
    
    426
    +--
    
    427
    +-- >>> getLine
    
    428
    +-- > Hello World!
    
    429
    +-- "Hello World!"
    
    430
    +--
    
    431
    +-- >>> getLine
    
    432
    +-- >
    
    433
    +-- ""
    
    434
    +getLine         :: IO String
    
    435
    +getLine         =  hGetLine stdin
    
    436
    +
    
    437
    +-- | The 'getContents' operation returns all user input as a single string,
    
    438
    +-- which is read lazily as it is needed.
    
    439
    +--
    
    440
    +-- 'getContents' is implemented as @'hGetContents' 'stdin'@.
    
    441
    +--
    
    442
    +-- This operation may fail with the same errors as 'hGetContents'.
    
    443
    +--
    
    444
    +-- ==== __Examples__
    
    445
    +--
    
    446
    +-- >>> getContents >>= putStr
    
    447
    +-- > aaabbbccc :D
    
    448
    +-- aaabbbccc :D
    
    449
    +-- > I hope you have a great day
    
    450
    +-- I hope you have a great day
    
    451
    +-- > ^D
    
    452
    +--
    
    453
    +-- >>> getContents >>= print . length
    
    454
    +-- > abc
    
    455
    +-- > <3
    
    456
    +-- > def ^D
    
    457
    +-- 11
    
    458
    +getContents     :: IO String
    
    459
    +getContents     =  hGetContents stdin
    
    460
    +
    
    461
    +-- | The 'getContents'' operation returns all user input as a single string,
    
    462
    +-- which is fully read before being returned
    
    463
    +--
    
    464
    +-- 'getContents'' is implemented as @'hGetContents'' 'stdin'@.
    
    465
    +--
    
    466
    +-- This operation may fail with the same errors as 'hGetContents''.
    
    467
    +--
    
    468
    +-- ==== __Examples__
    
    469
    +--
    
    470
    +-- >>> getContents' >>= putStr
    
    471
    +-- > aaabbbccc :D
    
    472
    +-- > I hope you have a great day
    
    473
    +-- aaabbbccc :D
    
    474
    +-- I hope you have a great day
    
    475
    +--
    
    476
    +-- >>> getContents' >>= print . length
    
    477
    +-- > abc
    
    478
    +-- > <3
    
    479
    +-- > def ^D
    
    480
    +-- 11
    
    481
    +--
    
    482
    +-- @since base-4.15.0.0
    
    483
    +getContents'    :: IO String
    
    484
    +getContents'    =  hGetContents' stdin
    
    485
    +
    
    486
    +-- | @'interact' f@ takes the entire input from 'stdin' and applies @f@ to it.
    
    487
    +-- The resulting string is written to the 'stdout' device.
    
    488
    +--
    
    489
    +-- Note that this operation is lazy, which allows to produce output
    
    490
    +-- even before all input has been consumed.
    
    491
    +--
    
    492
    +-- This operation may fail with the same errors as 'getContents' and 'putStr'.
    
    493
    +--
    
    494
    +-- If it doesn't produce output the buffering settings may not be
    
    495
    +-- correct, use ^D (ctrl+D) to close stdin which forces
    
    496
    +-- the buffer to be consumed.
    
    497
    +--
    
    498
    +-- You may wish to set the buffering style appropriate to your program's
    
    499
    +-- needs before using this function, for example:
    
    500
    +--
    
    501
    +-- @
    
    502
    +-- main :: IO ()
    
    503
    +-- main = do
    
    504
    +--   hSetBuffering stdin LineBuffering
    
    505
    +--   hSetBuffering stdout NoBuffering
    
    506
    +--   interact (concatMap (\str -> str ++ str) . L.lines)
    
    507
    +-- @
    
    508
    +--
    
    509
    +-- ==== __Examples__
    
    510
    +--
    
    511
    +-- >>> interact (\str -> str ++ str)
    
    512
    +-- > hi :)
    
    513
    +-- hi :)
    
    514
    +-- > ^D
    
    515
    +-- hi :)
    
    516
    +--
    
    517
    +-- >>> interact (const ":D")
    
    518
    +-- :D
    
    519
    +--
    
    520
    +-- >>> interact (show . words)
    
    521
    +-- > hello world!
    
    522
    +-- > I hope you have a great day
    
    523
    +-- > ^D
    
    524
    +-- ["hello","world!","I","hope","you","have","a","great","day"]
    
    525
    +interact        ::  (String -> String) -> IO ()
    
    526
    +interact f      =   do s <- getContents
    
    527
    +                       putStr (f s)
    
    528
    +
    
    529
    +-- | The 'readFile' function reads a file and
    
    530
    +-- returns the contents of the file as a string.
    
    531
    +--
    
    532
    +-- The file is read lazily, on demand, as with 'getContents'.
    
    533
    +--
    
    534
    +-- This operation may fail with the same errors as 'hGetContents' and 'openFile'.
    
    535
    +--
    
    536
    +-- ==== __Examples__
    
    537
    +--
    
    538
    +-- >>> readFile "~/hello_world"
    
    539
    +-- "Greetings!"
    
    540
    +--
    
    541
    +-- >>> take 5 <$> readFile "/dev/zero"
    
    542
    +-- "\NUL\NUL\NUL\NUL\NUL"
    
    543
    +readFile        :: FilePath -> IO String
    
    544
    +readFile name   =  openFile name ReadMode >>= hGetContents
    
    545
    +
    
    546
    +-- | The 'readFile'' function reads a file and
    
    547
    +-- returns the contents of the file as a string.
    
    548
    +--
    
    549
    +-- This is identical to 'readFile', but the file is fully read before being returned,
    
    550
    +-- as with 'getContents''.
    
    551
    +--
    
    552
    +-- @since base-4.15.0.0
    
    553
    +readFile'       :: FilePath -> IO String
    
    554
    +-- There's a bit of overkill here—both withFile and
    
    555
    +-- hGetContents' will close the file in the end.
    
    556
    +readFile' name  =  withFile name ReadMode hGetContents'
    
    557
    +
    
    558
    +-- | The computation @'writeFile' file str@ function writes the string @str@,
    
    559
    +-- to the file @file@.
    
    560
    +--
    
    561
    +-- This operation may fail with the same errors as 'hPutStr' and 'withFile'.
    
    562
    +--
    
    563
    +-- ==== __Examples__
    
    564
    +--
    
    565
    +-- >>> writeFile "hello" "world" >> readFile "hello"
    
    566
    +-- "world"
    
    567
    +--
    
    568
    +-- >>> writeFile "~/" "D:"
    
    569
    +-- *** Exception: ~/: withFile: inappropriate type (Is a directory)
    
    570
    +writeFile :: FilePath -> String -> IO ()
    
    571
    +writeFile f txt = withFile f WriteMode (\ hdl -> hPutStr hdl txt)
    
    572
    +
    
    573
    +-- | The computation @'appendFile' file str@ function appends the string @str@,
    
    574
    +-- to the file @file@.
    
    575
    +--
    
    576
    +-- Note that 'writeFile' and 'appendFile' write a literal string
    
    577
    +-- to a file.  To write a value of any printable type, as with 'print',
    
    578
    +-- use the 'show' function to convert the value to a string first.
    
    579
    +--
    
    580
    +-- This operation may fail with the same errors as 'hPutStr' and 'withFile'.
    
    581
    +--
    
    582
    +-- ==== __Examples__
    
    583
    +--
    
    584
    +-- The following example could be more efficently written by acquiring a handle
    
    585
    +-- instead with 'openFile' and using the computations capable of writing to handles
    
    586
    +-- such as 'hPutStr'.
    
    587
    +--
    
    588
    +-- >>> let fn = "hello_world"
    
    589
    +-- >>> in writeFile fn "hello" >> appendFile fn " world!" >> (readFile fn >>= putStrLn)
    
    590
    +-- "hello world!"
    
    591
    +--
    
    592
    +-- >>> let fn = "foo"; output = readFile' fn >>= putStrLn
    
    593
    +-- >>> in output >> appendFile fn (show [1,2,3]) >> output
    
    594
    +-- this is what's in the file
    
    595
    +-- this is what's in the file[1,2,3]
    
    596
    +appendFile      :: FilePath -> String -> IO ()
    
    597
    +appendFile f txt = withFile f AppendMode (\ hdl -> hPutStr hdl txt)
    
    598
    +
    
    599
    +-- | The 'readLn' function combines 'getLine' and 'readIO'.
    
    600
    +--
    
    601
    +-- This operation may fail with the same errors as 'getLine' and 'readIO'.
    
    602
    +--
    
    603
    +-- ==== __Examples__
    
    604
    +--
    
    605
    +-- >>> fmap (+ 5) readLn
    
    606
    +-- > 25
    
    607
    +-- 30
    
    608
    +--
    
    609
    +-- >>> readLn :: IO String
    
    610
    +-- > this is not a string literal
    
    611
    +-- *** Exception: user error (Prelude.readIO: no parse)
    
    612
    +readLn :: Read a => IO a
    
    613
    +readLn = getLine >>= readIO
    
    614
    +
    
    615
    +-- | The 'readIO' function is similar to 'read' except that it signals
    
    616
    +-- parse failure to the 'IO' monad instead of terminating the program.
    
    617
    +--
    
    618
    +-- This operation may fail with:
    
    619
    +--
    
    620
    +--  * 'GHC.Internal.System.IO.Error.isUserError' if there is no unambiguous parse.
    
    621
    +--
    
    622
    +-- ==== __Examples__
    
    623
    +--
    
    624
    +-- >>> fmap (+ 1) (readIO "1")
    
    625
    +-- 2
    
    626
    +--
    
    627
    +-- >>> readIO "not quite ()" :: IO ()
    
    628
    +-- *** Exception: user error (Prelude.readIO: no parse)
    
    629
    +readIO          :: Read a => String -> IO a
    
    630
    +readIO s        =  case (do { (x,t) <- reads s ;
    
    631
    +                              ("","") <- lex t ;
    
    632
    +                              return x }) of
    
    633
    +                        [x]    -> return x
    
    634
    +                        []     -> ioError (userError "Prelude.readIO: no parse")
    
    635
    +                        _      -> ioError (userError "Prelude.readIO: ambiguous parse")
    
    636
    +
    
    637
    +-- | The encoding of the current locale.
    
    638
    +--
    
    639
    +-- This is the initial locale encoding: if it has been subsequently changed by
    
    640
    +-- 'GHC.Internal.IO.Encoding.setLocaleEncoding' this value will not reflect that change.
    
    641
    +localeEncoding :: TextEncoding
    
    642
    +localeEncoding = initLocaleEncoding
    
    643
    +
    
    644
    +-- | Computation 'hReady' @hdl@ indicates whether at least one item is
    
    645
    +-- available for input from handle @hdl@.
    
    646
    +--
    
    647
    +-- This operation may fail with:
    
    648
    +--
    
    649
    +--  * 'GHC.Internal.System.IO.Error.isEOFError' if the end of file has been reached.
    
    650
    +hReady          :: Handle -> IO Bool
    
    651
    +hReady h        =  hWaitForInput h 0
    
    652
    +
    
    653
    +-- | Computation 'hPrint' @hdl t@ writes the string representation of @t@
    
    654
    +-- given by the 'show' function to the file or channel managed by @hdl@
    
    655
    +-- and appends a newline.
    
    656
    +--
    
    657
    +-- This operation may fail with the same errors as 'hPutStrLn'
    
    658
    +--
    
    659
    +-- ==== __Examples__
    
    660
    +--
    
    661
    +-- >>> hPrint stdout [1,2,3]
    
    662
    +-- [1,2,3]
    
    663
    +--
    
    664
    +-- >>> hPrint stdin [4,5,6]
    
    665
    +-- *** Exception: <stdin>: hPutStr: illegal operation (handle is not open for writing)
    
    666
    +hPrint          :: Show a => Handle -> a -> IO ()
    
    667
    +hPrint hdl      =  hPutStrLn hdl . show
    
    668
    +
    
    669
    +-- | The function creates a temporary file in ReadWrite mode.
    
    670
    +-- The created file isn\'t deleted automatically, so you need to delete it manually.
    
    671
    +--
    
    672
    +-- The file is created with permissions such that only the current
    
    673
    +-- user can read\/write it.
    
    674
    +--
    
    675
    +-- With some exceptions (see below), the file will be created securely
    
    676
    +-- in the sense that an attacker should not be able to cause
    
    677
    +-- openTempFile to overwrite another file on the filesystem using your
    
    678
    +-- credentials, by putting symbolic links (on Unix) in the place where
    
    679
    +-- the temporary file is to be created.  On Unix the @O_CREAT@ and
    
    680
    +-- @O_EXCL@ flags are used to prevent this attack, but note that
    
    681
    +-- @O_EXCL@ is sometimes not supported on NFS filesystems, so if you
    
    682
    +-- rely on this behaviour it is best to use local filesystems only.
    
    683
    +openTempFile :: FilePath   -- ^ Directory in which to create the file
    
    684
    +             -> String     -- ^ File name template. If the template is \"foo.ext\" then
    
    685
    +                           -- the created file will be \"fooXXX.ext\" where XXX is some
    
    686
    +                           -- random number. Note that this should not contain any path
    
    687
    +                           -- separator characters. On Windows, the template prefix may
    
    688
    +                           -- be truncated to 3 chars, e.g. \"foobar.ext\" will be
    
    689
    +                           -- \"fooXXX.ext\".
    
    690
    +             -> IO (FilePath, Handle)
    
    691
    +openTempFile tmp_dir template
    
    692
    +    = openTempFile' "openTempFile" tmp_dir template False 0o600
    
    693
    +
    
    694
    +-- | Like 'openTempFile', but opens the file in binary mode. See 'openBinaryFile' for more comments.
    
    695
    +openBinaryTempFile :: FilePath -> String -> IO (FilePath, Handle)
    
    696
    +openBinaryTempFile tmp_dir template
    
    697
    +    = openTempFile' "openBinaryTempFile" tmp_dir template True 0o600
    
    698
    +
    
    699
    +-- | Like 'openTempFile', but uses the default file permissions
    
    700
    +openTempFileWithDefaultPermissions :: FilePath -> String
    
    701
    +                                   -> IO (FilePath, Handle)
    
    702
    +openTempFileWithDefaultPermissions tmp_dir template
    
    703
    +    = openTempFile' "openTempFileWithDefaultPermissions" tmp_dir template False 0o666
    
    704
    +
    
    705
    +-- | Like 'openBinaryTempFile', but uses the default file permissions
    
    706
    +openBinaryTempFileWithDefaultPermissions :: FilePath -> String
    
    707
    +                                         -> IO (FilePath, Handle)
    
    708
    +openBinaryTempFileWithDefaultPermissions tmp_dir template
    
    709
    +    = openTempFile' "openBinaryTempFileWithDefaultPermissions" tmp_dir template True 0o666
    
    710
    +
    
    711
    +openTempFile' :: String -> FilePath -> String -> Bool -> CMode
    
    712
    +              -> IO (FilePath, Handle)
    
    713
    +openTempFile' loc tmp_dir template binary mode
    
    714
    +    | pathSeparator template
    
    715
    +    = failIO $ "openTempFile': Template string must not contain path separator characters: "++template
    
    716
    +    | otherwise = findTempName
    
    717
    +  where
    
    718
    +    -- We split off the last extension, so we can use .foo.ext files
    
    719
    +    -- for temporary files (hidden on Unix OSes). Unfortunately we're
    
    720
    +    -- below filepath in the hierarchy here.
    
    721
    +    (prefix, suffix) =
    
    722
    +       case break (== '.') $ reverse template of
    
    723
    +         -- First case: template contains no '.'s. Just re-reverse it.
    
    724
    +         (rev_suffix, "")       -> (reverse rev_suffix, "")
    
    725
    +         -- Second case: template contains at least one '.'. Strip the
    
    726
    +         -- dot from the prefix and prepend it to the suffix (if we don't
    
    727
    +         -- do this, the unique number will get added after the '.' and
    
    728
    +         -- thus be part of the extension, which is wrong.)
    
    729
    +         (rev_suffix, '.':rest) -> (reverse rest, '.':reverse rev_suffix)
    
    730
    +         -- Otherwise, something is wrong, because (break (== '.')) should
    
    731
    +         -- always return a pair with either the empty string or a string
    
    732
    +         -- beginning with '.' as the second component.
    
    733
    +         _                      -> errorWithoutStackTrace "bug in GHC.Internal.System.IO.openTempFile"
    
    734
    +#if defined(mingw32_HOST_OS)
    
    735
    +    findTempName = findTempNamePosix <!> findTempNameWinIO
    
    736
    +
    
    737
    +    findTempNameWinIO = do
    
    738
    +      let label = if null prefix then "ghc" else prefix
    
    739
    +      withCWString tmp_dir $ \c_tmp_dir ->
    
    740
    +        withCWString label $ \c_template ->
    
    741
    +          withCWString suffix $ \c_suffix ->
    
    742
    +            with nullPtr $ \c_ptr -> do
    
    743
    +              res <- c_createUUIDTempFileErrNo c_tmp_dir c_template c_suffix c_ptr
    
    744
    +              if not res
    
    745
    +                 then do errno <- getErrno
    
    746
    +                         ioError (errnoToIOError loc errno Nothing (Just tmp_dir))
    
    747
    +                 else do c_p <- peek c_ptr
    
    748
    +                         filename <- peekCWString c_p
    
    749
    +                         free c_p
    
    750
    +                         let flags = fromIntegral mode .&. o_EXCL
    
    751
    +                         handleResultsWinIO filename (flags == o_EXCL)
    
    752
    +
    
    753
    +    findTempNamePosix = do
    
    754
    +      let label = if null prefix then "ghc" else prefix
    
    755
    +      withCWString tmp_dir $ \c_tmp_dir ->
    
    756
    +        withCWString label $ \c_template ->
    
    757
    +          withCWString suffix $ \c_suffix ->
    
    758
    +            allocaBytes (sizeOf (undefined :: CWchar) * 260) $ \c_str -> do
    
    759
    +            res <- c_getTempFileNameErrorNo c_tmp_dir c_template c_suffix 0
    
    760
    +                                            c_str
    
    761
    +            if not res
    
    762
    +               then do errno <- getErrno
    
    763
    +                       ioError (errnoToIOError loc errno Nothing (Just tmp_dir))
    
    764
    +               else do filename <- peekCWString c_str
    
    765
    +                       handleResultsPosix filename
    
    766
    +
    
    767
    +    handleResultsPosix filename = do
    
    768
    +      let oflags1 = rw_flags .|. o_EXCL
    
    769
    +          binary_flags
    
    770
    +              | binary    = o_BINARY
    
    771
    +              | otherwise = 0
    
    772
    +          oflags = oflags1 .|. binary_flags
    
    773
    +      fd <- withFilePath filename $ \ f -> c_open f oflags mode
    
    774
    +      case fd < 0 of
    
    775
    +        True -> do errno <- getErrno
    
    776
    +                   ioError (errnoToIOError loc errno Nothing (Just tmp_dir))
    
    777
    +        False ->
    
    778
    +          do (fD,fd_type) <- FD.mkFD fd ReadWriteMode Nothing{-no stat-}
    
    779
    +                                     False{-is_socket-}
    
    780
    +                                     True{-is_nonblock-}
    
    781
    +
    
    782
    +             enc <- getLocaleEncoding
    
    783
    +             h <- POSIX.mkHandleFromFD fD fd_type filename ReadWriteMode
    
    784
    +                                 False{-set non-block-} (Just enc)
    
    785
    +
    
    786
    +             return (filename, h)
    
    787
    +
    
    788
    +    handleResultsWinIO filename excl = do
    
    789
    +      (hwnd, hwnd_type) <- openFileAsTemp filename True excl
    
    790
    +      mb_codec <- if binary then return Nothing else fmap Just getLocaleEncoding
    
    791
    +
    
    792
    +      -- then use it to make a Handle
    
    793
    +      h <- mkHandleFromHANDLE hwnd hwnd_type filename ReadWriteMode mb_codec
    
    794
    +                `onException` IODevice.close hwnd
    
    795
    +      return (filename, h)
    
    796
    +
    
    797
    +foreign import ccall "getTempFileNameErrorNo" c_getTempFileNameErrorNo
    
    798
    +  :: CWString -> CWString -> CWString -> CUInt -> Ptr CWchar -> IO Bool
    
    799
    +
    
    800
    +foreign import ccall "__createUUIDTempFileErrNo" c_createUUIDTempFileErrNo
    
    801
    +  :: CWString -> CWString -> CWString -> Ptr CWString -> IO Bool
    
    802
    +
    
    803
    +pathSeparator :: String -> Bool
    
    804
    +pathSeparator template = any (\x-> x == '/' || x == '\\') template
    
    805
    +
    
    806
    +output_flags = std_flags
    
    807
    +#else /* else mingw32_HOST_OS */
    
    808
    +    findTempName = do
    
    809
    +      rs <- rand_string
    
    810
    +      let filename = prefix ++ rs ++ suffix
    
    811
    +          filepath = tmp_dir `combine` filename
    
    812
    +      r <- openNewFile filepath binary mode
    
    813
    +      case r of
    
    814
    +        FileExists -> findTempName
    
    815
    +        OpenNewError errno -> ioError (errnoToIOError loc errno Nothing (Just tmp_dir))
    
    816
    +        NewFileCreated fd -> do
    
    817
    +          (fD,fd_type) <- FD.mkFD fd ReadWriteMode Nothing{-no stat-}
    
    818
    +                               False{-is_socket-}
    
    819
    +                               True{-is_nonblock-}
    
    820
    +
    
    821
    +          enc <- getLocaleEncoding
    
    822
    +          h <- POSIX.mkHandleFromFD fD fd_type filepath ReadWriteMode False{-set non-block-} (Just enc)
    
    823
    +
    
    824
    +          return (filepath, h)
    
    825
    +
    
    826
    +      where
    
    827
    +        -- XXX bits copied from System.FilePath, since that's not available here
    
    828
    +        combine a b
    
    829
    +                  | null b = a
    
    830
    +                  | null a = b
    
    831
    +                  | pathSeparator [last a] = a ++ b
    
    832
    +                  | otherwise = a ++ [pathSeparatorChar] ++ b
    
    833
    +
    
    834
    +tempCounter :: IORef Int
    
    835
    +tempCounter = unsafePerformIO $ newIORef 0
    
    836
    +{-# NOINLINE tempCounter #-}
    
    837
    +
    
    838
    +-- build large digit-alike number
    
    839
    +rand_string :: IO String
    
    840
    +rand_string = do
    
    841
    +  r1 <- c_getpid
    
    842
    +  (r2, _) <- atomicModifyIORef'_ tempCounter (+1)
    
    843
    +  return $ show r1 ++ "-" ++ show r2
    
    844
    +
    
    845
    +data OpenNewFileResult
    
    846
    +  = NewFileCreated CInt
    
    847
    +  | FileExists
    
    848
    +  | OpenNewError Errno
    
    849
    +
    
    850
    +openNewFile :: FilePath -> Bool -> CMode -> IO OpenNewFileResult
    
    851
    +openNewFile filepath binary mode = do
    
    852
    +  let oflags1 = rw_flags .|. o_EXCL
    
    853
    +
    
    854
    +      binary_flags
    
    855
    +        | binary    = o_BINARY
    
    856
    +        | otherwise = 0
    
    857
    +
    
    858
    +      oflags = oflags1 .|. binary_flags
    
    859
    +  fd <- withFilePath filepath $ \ f ->
    
    860
    +          c_open f oflags mode
    
    861
    +  if fd < 0
    
    862
    +    then do
    
    863
    +      errno <- getErrno
    
    864
    +      case errno of
    
    865
    +        _ | errno == eEXIST -> return FileExists
    
    866
    +        _ -> return (OpenNewError errno)
    
    867
    +    else return (NewFileCreated fd)
    
    868
    +
    
    869
    +-- XXX Should use filepath library
    
    870
    +pathSeparatorChar :: Char
    
    871
    +pathSeparatorChar = '/'
    
    872
    +
    
    873
    +pathSeparator :: String -> Bool
    
    874
    +pathSeparator template = pathSeparatorChar `elem` template
    
    875
    +
    
    876
    +output_flags = std_flags    .|. o_CREAT
    
    877
    +#endif /* mingw32_HOST_OS */
    
    878
    +
    
    879
    +-- XXX Copied from GHC.Handle
    
    880
    +std_flags, output_flags, rw_flags :: CInt
    
    881
    +std_flags    = o_NONBLOCK   .|. o_NOCTTY
    
    882
    +rw_flags     = output_flags .|. o_RDWR
    
    883
    +
    
    190 884
     -- $locking
    
    191 885
     -- Implementations should enforce as far as possible, at least locally to the
    
    192 886
     -- Haskell process, multiple-reader single-writer locking on files.
    
    ... ... @@ -219,3 +913,24 @@ import GHC.Internal.Control.Monad.Fix (fixIO)
    219 913
     -- output
    
    220 914
     -- > input^D
    
    221 915
     -- output
    
    916
    +
    
    917
    +{-NOTE:
    
    918
    +    The following instances are technically orphans, but practically they are
    
    919
    +    not, since ordinary users should not use @ghc-internal@ directly and thus
    
    920
    +    get the instantiated types only through this module.
    
    921
    +-}
    
    922
    +
    
    923
    +-- | @since base-4.2.0.0
    
    924
    +deriving instance Read IOMode
    
    925
    +
    
    926
    +-- | @since base-4.2.0.0
    
    927
    +deriving instance Read BufferMode
    
    928
    +
    
    929
    +-- | @since base-4.2.0.0
    
    930
    +deriving instance Read SeekMode
    
    931
    +
    
    932
    +-- | @since base-4.3.0.0
    
    933
    +deriving instance Read Newline
    
    934
    +
    
    935
    +-- | @since base-4.3.0.0
    
    936
    +deriving instance Read NewlineMode

  • libraries/base/src/Text/Printf.hs
    ... ... @@ -97,9 +97,9 @@ import Data.Char
    97 97
     import GHC.Internal.Int
    
    98 98
     import GHC.Internal.Data.List (stripPrefix)
    
    99 99
     import GHC.Internal.Word
    
    100
    -import GHC.Internal.Numeric
    
    101 100
     import GHC.Internal.Numeric.Natural
    
    102
    -import GHC.Internal.System.IO
    
    101
    +import Numeric
    
    102
    +import System.IO
    
    103 103
     
    
    104 104
     -- $setup
    
    105 105
     -- >>> import Prelude
    

  • libraries/ghc-internal/ghc-internal.cabal.in
    ... ... @@ -323,7 +323,6 @@ Library
    323 323
             GHC.Internal.Numeric.Natural
    
    324 324
             GHC.Internal.System.Environment
    
    325 325
             GHC.Internal.System.Environment.Blank
    
    326
    -        GHC.Internal.System.IO
    
    327 326
             GHC.Internal.System.IO.Error
    
    328 327
             GHC.Internal.System.Mem
    
    329 328
             GHC.Internal.System.Mem.StableName
    

  • libraries/ghc-internal/src/GHC/Internal/ByteOrder.hs
    ... ... @@ -28,7 +28,6 @@ module GHC.Internal.ByteOrder
    28 28
     import GHC.Internal.Base
    
    29 29
     import GHC.Internal.Enum
    
    30 30
     import GHC.Internal.Generics (Generic)
    
    31
    -import GHC.Internal.Text.Read
    
    32 31
     import GHC.Internal.Text.Show
    
    33 32
     
    
    34 33
     -- | Byte ordering.
    
    ... ... @@ -39,7 +38,6 @@ data ByteOrder
    39 38
                  , Ord     -- ^ @since base-4.11.0.0
    
    40 39
                  , Bounded -- ^ @since base-4.11.0.0
    
    41 40
                  , Enum    -- ^ @since base-4.11.0.0
    
    42
    -             , Read    -- ^ @since base-4.11.0.0
    
    43 41
                  , Show    -- ^ @since base-4.11.0.0
    
    44 42
                  , Generic -- ^ @since base-4.15.0.0
    
    45 43
                  )
    

  • libraries/ghc-internal/src/GHC/Internal/Data/Data.hs
    ... ... @@ -61,6 +61,7 @@ module GHC.Internal.Data.Data (
    61 61
             mkIntType,
    
    62 62
             mkFloatType,
    
    63 63
             mkCharType,
    
    64
    +        mkPrimCon,
    
    64 65
             mkNoRepType,
    
    65 66
             -- ** Observers
    
    66 67
             dataTypeName,
    
    ... ... @@ -94,7 +95,6 @@ module GHC.Internal.Data.Data (
    94 95
             constrIndex,
    
    95 96
             -- ** From strings to constructors and vice versa: all data types
    
    96 97
             showConstr,
    
    97
    -        readConstr,
    
    98 98
     
    
    99 99
             -- * Convenience functions: take type constructors apart
    
    100 100
             tyconUQname,
    
    ... ... @@ -123,10 +123,8 @@ import GHC.Internal.Data.Version( Version(..) )
    123 123
     import GHC.Internal.Base hiding (Any, IntRep, FloatRep, NonEmpty(..))
    
    124 124
     import GHC.Internal.List
    
    125 125
     import GHC.Internal.Num
    
    126
    -import GHC.Internal.Read
    
    127 126
     import GHC.Internal.Show
    
    128 127
     import GHC.Internal.Tuple (Solo (..))
    
    129
    -import GHC.Internal.Text.Read( reads )
    
    130 128
     
    
    131 129
     -- Imports for the instances
    
    132 130
     import GHC.Internal.Data.Functor.Identity -- So we can give Data instance for Identity
    
    ... ... @@ -682,32 +680,6 @@ showConstr :: Constr -> String
    682 680
     showConstr = constring
    
    683 681
     
    
    684 682
     
    
    685
    --- | Lookup a constructor via a string
    
    686
    -readConstr :: DataType -> String -> Maybe Constr
    
    687
    -readConstr dt str =
    
    688
    -      case dataTypeRep dt of
    
    689
    -        AlgRep cons -> idx cons
    
    690
    -        IntRep      -> mkReadCon (\i -> (mkPrimCon dt str (IntConstr i)))
    
    691
    -        FloatRep    -> mkReadCon ffloat
    
    692
    -        CharRep     -> mkReadCon (\c -> (mkPrimCon dt str (CharConstr c)))
    
    693
    -        NoRep       -> Nothing
    
    694
    -  where
    
    695
    -
    
    696
    -    -- Read a value and build a constructor
    
    697
    -    mkReadCon :: Read t => (t -> Constr) -> Maybe Constr
    
    698
    -    mkReadCon f = case (reads str) of
    
    699
    -                    [(t,"")] -> Just (f t)
    
    700
    -                    _ -> Nothing
    
    701
    -
    
    702
    -    -- Traverse list of algebraic datatype constructors
    
    703
    -    idx :: [Constr] -> Maybe Constr
    
    704
    -    idx cons = case filter ((==) str . showConstr) cons of
    
    705
    -                [] -> Nothing
    
    706
    -                hd : _ -> Just hd
    
    707
    -
    
    708
    -    ffloat :: Double -> Constr
    
    709
    -    ffloat =  mkPrimCon dt str . FloatConstr . toRational
    
    710
    -
    
    711 683
     ------------------------------------------------------------------------------
    
    712 684
     --
    
    713 685
     --      Convenience functions: algebraic data types
    

  • libraries/ghc-internal/src/GHC/Internal/Data/Version.hs
    ... ... @@ -10,7 +10,7 @@
    10 10
     --
    
    11 11
     -- Maintainer  :  libraries@haskell.org
    
    12 12
     -- Stability   :  stable
    
    13
    --- Portability :  non-portable (local universal quantification in ReadP)
    
    13
    +-- Portability :  non-portable
    
    14 14
     --
    
    15 15
     -- A general library for representation and manipulation of versions.
    
    16 16
     --
    
    ... ... @@ -31,23 +31,18 @@ module GHC.Internal.Data.Version (
    31 31
             -- * The @Version@ type
    
    32 32
             Version(..),
    
    33 33
             -- * A concrete representation of @Version@
    
    34
    -        showVersion, parseVersion,
    
    34
    +        showVersion,
    
    35 35
             -- * Constructor function
    
    36 36
             makeVersion
    
    37 37
       ) where
    
    38 38
     
    
    39
    -import GHC.Internal.Data.Functor     ( Functor(..) )
    
    40 39
     import GHC.Internal.Data.Eq
    
    41 40
     import GHC.Internal.Int              ( Int )
    
    42 41
     import GHC.Internal.Data.List        ( map, sort, concat, concatMap, intersperse, (++) )
    
    43 42
     import GHC.Internal.Data.Ord
    
    44
    -import GHC.Internal.Base             ( Applicative(..), (&&), String )
    
    43
    +import GHC.Internal.Base             ( (&&), String )
    
    45 44
     import GHC.Internal.Generics
    
    46
    -import GHC.Internal.Unicode          ( isDigit, isAlphaNum )
    
    47
    -import GHC.Internal.Read
    
    48 45
     import GHC.Internal.Show
    
    49
    -import GHC.Internal.Text.ParserCombinators.ReadP
    
    50
    -import GHC.Internal.Text.Read        ( read )
    
    51 46
     
    
    52 47
     {- |
    
    53 48
     A 'Version' represents the version of a software entity.
    
    ... ... @@ -69,8 +64,8 @@ operations are the right thing for every 'Version'.
    69 64
     
    
    70 65
     Similarly, concrete representations of versions may differ.  One
    
    71 66
     possible concrete representation is provided (see 'showVersion' and
    
    72
    -'parseVersion'), but depending on the application a different concrete
    
    73
    -representation may be more appropriate.
    
    67
    +'Data.Version.parseVersion'), but depending on the application a
    
    68
    +different concrete representation may be more appropriate.
    
    74 69
     -}
    
    75 70
     data Version =
    
    76 71
       Version { versionBranch :: [Int],
    
    ... ... @@ -92,8 +87,7 @@ data Version =
    92 87
                     -- The interpretation of the list of tags is entirely dependent
    
    93 88
                     -- on the entity that this version applies to.
    
    94 89
             }
    
    95
    -  deriving ( Read    -- ^ @since base-2.01
    
    96
    -           , Show    -- ^ @since base-2.01
    
    90
    +  deriving ( Show    -- ^ @since base-2.01
    
    97 91
                , Generic -- ^ @since base-4.9.0.0
    
    98 92
                )
    
    99 93
     {-# DEPRECATED versionTags "See GHC ticket #2496" #-}
    
    ... ... @@ -121,13 +115,6 @@ showVersion (Version branch tags)
    121 115
       = concat (intersperse "." (map show branch)) ++
    
    122 116
          concatMap ('-':) tags
    
    123 117
     
    
    124
    --- | A parser for versions in the format produced by 'showVersion'.
    
    125
    ---
    
    126
    -parseVersion :: ReadP Version
    
    127
    -parseVersion = do branch <- sepBy1 (fmap read (munch1 isDigit)) (char '.')
    
    128
    -                  tags   <- many (char '-' *> munch1 isAlphaNum)
    
    129
    -                  pure Version{versionBranch=branch, versionTags=tags}
    
    130
    -
    
    131 118
     -- | Construct tag-less 'Version'
    
    132 119
     --
    
    133 120
     -- @since base-4.8.0.0
    

  • libraries/ghc-internal/src/GHC/Internal/IO/Device.hs
    ... ... @@ -31,7 +31,6 @@ import GHC.Internal.Base
    31 31
     import GHC.Internal.Word
    
    32 32
     import GHC.Internal.Arr
    
    33 33
     import GHC.Internal.Enum
    
    34
    -import GHC.Internal.Read
    
    35 34
     import GHC.Internal.Show
    
    36 35
     import GHC.Internal.Ptr
    
    37 36
     import GHC.Internal.Num
    
    ... ... @@ -179,7 +178,6 @@ data SeekMode
    179 178
                  , Ord  -- ^ @since base-4.2.0.0
    
    180 179
                  , Ix   -- ^ @since base-4.2.0.0
    
    181 180
                  , Enum -- ^ @since base-4.2.0.0
    
    182
    -             , Read -- ^ @since base-4.2.0.0
    
    183 181
                  , Show -- ^ @since base-4.2.0.0
    
    184 182
                  )
    
    185 183
     

  • libraries/ghc-internal/src/GHC/Internal/IO/Handle/Types.hs
    ... ... @@ -48,7 +48,6 @@ import GHC.Internal.IO.BufferedIO
    48 48
     import GHC.Internal.IO.Encoding.Types
    
    49 49
     import GHC.Internal.IORef
    
    50 50
     import GHC.Internal.Show
    
    51
    -import GHC.Internal.Read
    
    52 51
     import GHC.Internal.Word
    
    53 52
     import GHC.Internal.IO.Device
    
    54 53
     import GHC.Internal.Data.Typeable
    
    ... ... @@ -270,7 +269,6 @@ data BufferMode
    270 269
                     -- is 'Just' @n@ and is otherwise implementation-dependent.
    
    271 270
        deriving ( Eq   -- ^ @since base-4.2.0.0
    
    272 271
                 , Ord  -- ^ @since base-4.2.0.0
    
    273
    -            , Read -- ^ @since base-4.2.0.0
    
    274 272
                 , Show -- ^ @since base-4.2.0.0
    
    275 273
                 )
    
    276 274
     
    
    ... ... @@ -376,7 +374,6 @@ data Newline = LF -- ^ @\'\\n\'@
    376 374
                  | CRLF  -- ^ @\'\\r\\n\'@
    
    377 375
                  deriving ( Eq   -- ^ @since base-4.2.0.0
    
    378 376
                           , Ord  -- ^ @since base-4.3.0.0
    
    379
    -                      , Read -- ^ @since base-4.3.0.0
    
    380 377
                           , Show -- ^ @since base-4.3.0.0
    
    381 378
                           )
    
    382 379
     
    
    ... ... @@ -393,7 +390,6 @@ data NewlineMode
    393 390
                      }
    
    394 391
                  deriving ( Eq   -- ^ @since base-4.2.0.0
    
    395 392
                           , Ord  -- ^ @since base-4.3.0.0
    
    396
    -                      , Read -- ^ @since base-4.3.0.0
    
    397 393
                           , Show -- ^ @since base-4.3.0.0
    
    398 394
                           )
    
    399 395
     
    

  • libraries/ghc-internal/src/GHC/Internal/IO/IOMode.hs
    ... ... @@ -20,7 +20,6 @@ module GHC.Internal.IO.IOMode (IOMode(..)) where
    20 20
     
    
    21 21
     import GHC.Internal.Base
    
    22 22
     import GHC.Internal.Show
    
    23
    -import GHC.Internal.Read
    
    24 23
     import GHC.Internal.Arr
    
    25 24
     import GHC.Internal.Enum
    
    26 25
     
    
    ... ... @@ -30,7 +29,6 @@ data IOMode = ReadMode | WriteMode | AppendMode | ReadWriteMode
    30 29
                                  , Ord  -- ^ @since base-4.2.0.0
    
    31 30
                                  , Ix   -- ^ @since base-4.2.0.0
    
    32 31
                                  , Enum -- ^ @since base-4.2.0.0
    
    33
    -                             , Read -- ^ @since base-4.2.0.0
    
    34 32
                                  , Show -- ^ @since base-4.2.0.0
    
    35 33
                                  )
    
    36 34
     

  • libraries/ghc-internal/src/GHC/Internal/Numeric.hs
    1 1
     {-# LANGUAGE Trustworthy #-}
    
    2
    -{-# LANGUAGE NoImplicitPrelude, MagicHash #-}
    
    3 2
     
    
    4 3
     -----------------------------------------------------------------------------
    
    5 4
     -- |
    
    ... ... @@ -16,274 +15,22 @@
    16 15
     --
    
    17 16
     -----------------------------------------------------------------------------
    
    18 17
     
    
    19
    -module GHC.Internal.Numeric (
    
    20
    -
    
    21
    -        -- * Showing
    
    22
    -
    
    23
    -        showSigned,
    
    24
    -
    
    25
    -        showIntAtBase,
    
    26
    -        showInt,
    
    27
    -        showBin,
    
    28
    -        showHex,
    
    29
    -        showOct,
    
    30
    -
    
    31
    -        showEFloat,
    
    32
    -        showFFloat,
    
    33
    -        showGFloat,
    
    34
    -        showFFloatAlt,
    
    35
    -        showGFloatAlt,
    
    36
    -        showFloat,
    
    37
    -        showHFloat,
    
    38
    -
    
    39
    -        floatToDigits,
    
    40
    -
    
    41
    -        -- * Reading
    
    42
    -
    
    43
    -        -- | /NB:/ 'readInt' is the \'dual\' of 'showIntAtBase',
    
    44
    -        -- and 'readDec' is the \`dual\' of 'showInt'.
    
    45
    -        -- The inconsistent naming is a historical accident.
    
    46
    -
    
    47
    -        readSigned,
    
    48
    -
    
    49
    -        readInt,
    
    50
    -        readBin,
    
    51
    -        readDec,
    
    52
    -        readOct,
    
    53
    -        readHex,
    
    54
    -
    
    55
    -        readFloat,
    
    56
    -
    
    57
    -        lexDigits,
    
    58
    -
    
    59
    -        -- * Miscellaneous
    
    60
    -
    
    61
    -        fromRat,
    
    62
    -        Floating(..)
    
    63
    -
    
    64
    -        ) where
    
    18
    +module GHC.Internal.Numeric (showIntAtBase, showHex) where
    
    65 19
     
    
    66 20
     import GHC.Internal.Base
    
    67
    -import GHC.Internal.Read
    
    68
    -import GHC.Internal.Real
    
    69
    -import GHC.Internal.Float
    
    70
    -import GHC.Internal.Num
    
    71
    -import GHC.Internal.Show
    
    72
    -import GHC.Internal.Text.ParserCombinators.ReadP( ReadP, readP_to_S, pfail )
    
    73
    -import qualified GHC.Internal.Text.Read.Lex as L
    
    74
    -
    
    75
    --- $setup
    
    76
    --- >>> import Prelude
    
    77
    -
    
    78
    --- -----------------------------------------------------------------------------
    
    79
    --- Reading
    
    80
    -
    
    81
    --- | Reads an /unsigned/ integral value in an arbitrary base.
    
    82
    -readInt :: Num a
    
    83
    -  => a                  -- ^ the base
    
    84
    -  -> (Char -> Bool)     -- ^ a predicate distinguishing valid digits in this base
    
    85
    -  -> (Char -> Int)      -- ^ a function converting a valid digit character to an 'Int'
    
    86
    -  -> ReadS a
    
    87
    -readInt base isDigit valDigit = readP_to_S (L.readIntP base isDigit valDigit)
    
    88
    -
    
    89
    --- | Read an unsigned number in binary notation.
    
    90
    ---
    
    91
    --- >>> readBin "10011"
    
    92
    --- [(19,"")]
    
    93
    -readBin :: (Eq a, Num a) => ReadS a
    
    94
    -readBin = readP_to_S L.readBinP
    
    95
    -
    
    96
    --- | Read an unsigned number in octal notation.
    
    97
    ---
    
    98
    --- >>> readOct "0644"
    
    99
    --- [(420,"")]
    
    100
    -readOct :: (Eq a, Num a) => ReadS a
    
    101
    -readOct = readP_to_S L.readOctP
    
    102
    -
    
    103
    --- | Read an unsigned number in decimal notation.
    
    104
    ---
    
    105
    --- >>> readDec "0644"
    
    106
    --- [(644,"")]
    
    107
    -readDec :: (Eq a, Num a) => ReadS a
    
    108
    -readDec = readP_to_S L.readDecP
    
    109
    -
    
    110
    --- | Read an unsigned number in hexadecimal notation.
    
    111
    --- Both upper or lower case letters are allowed.
    
    112
    ---
    
    113
    --- >>> readHex "deadbeef"
    
    114
    --- [(3735928559,"")]
    
    115
    -readHex :: (Eq a, Num a) => ReadS a
    
    116
    -readHex = readP_to_S L.readHexP
    
    117
    -
    
    118
    --- | Reads an /unsigned/ 'RealFrac' value,
    
    119
    --- expressed in decimal scientific notation.
    
    120
    ---
    
    121
    --- Note that this function takes time linear in the magnitude of its input
    
    122
    --- which can scale exponentially with input size (e.g. @"1e100000000"@ is a
    
    123
    --- very large number while having a very small textual form).
    
    124
    --- For this reason, users should take care to avoid using this function on
    
    125
    --- untrusted input. Users needing to parse floating point values
    
    126
    --- (e.g. 'Float') are encouraged to instead use 'read', which does
    
    127
    --- not suffer from this issue.
    
    128
    -readFloat :: RealFrac a => ReadS a
    
    129
    -readFloat = readP_to_S readFloatP
    
    130
    -
    
    131
    -readFloatP :: RealFrac a => ReadP a
    
    132
    -readFloatP =
    
    133
    -  do tok <- L.lex
    
    134
    -     case tok of
    
    135
    -       L.Number n -> return $ fromRational $ L.numberToRational n
    
    136
    -       _          -> pfail
    
    137
    -
    
    138
    --- It's turgid to have readSigned work using list comprehensions,
    
    139
    --- but it's specified as a ReadS to ReadS transformer
    
    140
    --- With a bit of luck no one will use it.
    
    141
    -
    
    142
    --- | Reads a /signed/ 'Real' value, given a reader for an unsigned value.
    
    143
    -readSigned :: (Real a) => ReadS a -> ReadS a
    
    144
    -readSigned readPos = readParen False read'
    
    145
    -                     where read' r  = read'' r ++
    
    146
    -                                      (do
    
    147
    -                                        ("-",s) <- lex r
    
    148
    -                                        (x,t)   <- read'' s
    
    149
    -                                        return (-x,t))
    
    150
    -                           read'' r = do
    
    151
    -                               (str,s) <- lex r
    
    152
    -                               (n,"")  <- readPos str
    
    153
    -                               return (n,s)
    
    154
    -
    
    155
    --- -----------------------------------------------------------------------------
    
    156
    --- Showing
    
    157
    -
    
    158
    --- | Show /non-negative/ 'Integral' numbers in base 10.
    
    159
    -showInt :: Integral a => a -> ShowS
    
    160
    -showInt n0 cs0
    
    161
    -    | n0 < 0    = errorWithoutStackTrace "GHC.Internal.Numeric.showInt: can't show negative numbers"
    
    162
    -    | otherwise = go n0 cs0
    
    163
    -    where
    
    164
    -    go n cs
    
    165
    -        | n < 10    = case unsafeChr (ord '0' + fromIntegral n) of
    
    166
    -            c@(C# _) -> c:cs
    
    167
    -        | otherwise = case unsafeChr (ord '0' + fromIntegral r) of
    
    168
    -            c@(C# _) -> go q (c:cs)
    
    169
    -        where
    
    170
    -        (q,r) = n `quotRem` 10
    
    171
    -
    
    172
    --- Controlling the format and precision of floats. The code that
    
    173
    --- implements the formatting itself is in @PrelNum@ to avoid
    
    174
    --- mutual module deps.
    
    175
    -
    
    176
    -{-# SPECIALIZE showEFloat ::
    
    177
    -        Maybe Int -> Float  -> ShowS #-}
    
    178
    -{-# SPECIALIZE showEFloat ::
    
    179
    -        Maybe Int -> Double -> ShowS #-}
    
    180
    -{-# SPECIALIZE showFFloat ::
    
    181
    -        Maybe Int -> Float  -> ShowS #-}
    
    182
    -{-# SPECIALIZE showFFloat ::
    
    183
    -        Maybe Int -> Double -> ShowS #-}
    
    184
    -{-# SPECIALIZE showGFloat ::
    
    185
    -        Maybe Int -> Float  -> ShowS #-}
    
    186
    -{-# SPECIALIZE showGFloat ::
    
    187
    -        Maybe Int -> Double -> ShowS #-}
    
    188
    -
    
    189
    --- | Show a signed 'RealFloat' value
    
    190
    --- using scientific (exponential) notation (e.g. @2.45e2@, @1.5e-3@).
    
    191
    ---
    
    192
    --- In the call @'showEFloat' digs val@, if @digs@ is 'Nothing',
    
    193
    --- the value is shown to full precision; if @digs@ is @'Just' d@,
    
    194
    --- then at most @d@ digits after the decimal point are shown.
    
    195
    -showEFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
    
    196
    -
    
    197
    --- | Show a signed 'RealFloat' value
    
    198
    --- using standard decimal notation (e.g. @245000@, @0.0015@).
    
    199
    ---
    
    200
    --- In the call @'showFFloat' digs val@, if @digs@ is 'Nothing',
    
    201
    --- the value is shown to full precision; if @digs@ is @'Just' d@,
    
    202
    --- then at most @d@ digits after the decimal point are shown.
    
    203
    -showFFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
    
    204
    -
    
    205
    --- | Show a signed 'RealFloat' value
    
    206
    --- using standard decimal notation for arguments whose absolute value lies
    
    207
    --- between @0.1@ and @9,999,999@, and scientific notation otherwise.
    
    208
    ---
    
    209
    --- In the call @'showGFloat' digs val@, if @digs@ is 'Nothing',
    
    210
    --- the value is shown to full precision; if @digs@ is @'Just' d@,
    
    211
    --- then at most @d@ digits after the decimal point are shown.
    
    212
    -showGFloat    :: (RealFloat a) => Maybe Int -> a -> ShowS
    
    213
    -
    
    214
    -showEFloat d x =  showString (formatRealFloat FFExponent d x)
    
    215
    -showFFloat d x =  showString (formatRealFloat FFFixed d x)
    
    216
    -showGFloat d x =  showString (formatRealFloat FFGeneric d x)
    
    217
    -
    
    218
    --- | Show a signed 'RealFloat' value
    
    219
    --- using standard decimal notation (e.g. @245000@, @0.0015@).
    
    220
    ---
    
    221
    --- This behaves as 'showFFloat', except that a decimal point
    
    222
    --- is always guaranteed, even if not needed.
    
    223
    ---
    
    224
    --- @since base-4.7.0.0
    
    225
    -showFFloatAlt    :: (RealFloat a) => Maybe Int -> a -> ShowS
    
    226
    -
    
    227
    --- | Show a signed 'RealFloat' value
    
    228
    --- using standard decimal notation for arguments whose absolute value lies
    
    229
    --- between @0.1@ and @9,999,999@, and scientific notation otherwise.
    
    230
    ---
    
    231
    --- This behaves as 'showFFloat', except that a decimal point
    
    232
    --- is always guaranteed, even if not needed.
    
    233
    ---
    
    234
    --- @since base-4.7.0.0
    
    235
    -showGFloatAlt    :: (RealFloat a) => Maybe Int -> a -> ShowS
    
    236
    -
    
    237
    -showFFloatAlt d x =  showString (formatRealFloatAlt FFFixed d True x)
    
    238
    -showGFloatAlt d x =  showString (formatRealFloatAlt FFGeneric d True x)
    
    239
    -
    
    240
    -{- | Show a floating-point value in the hexadecimal format,
    
    241
    -similar to the @%a@ specifier in C's printf.
    
    242
    -
    
    243
    -  >>> showHFloat (212.21 :: Double) ""
    
    244
    -  "0x1.a86b851eb851fp7"
    
    245
    -  >>> showHFloat (-12.76 :: Float) ""
    
    246
    -  "-0x1.9851ecp3"
    
    247
    -  >>> showHFloat (-0 :: Double) ""
    
    248
    -  "-0x0p+0"
    
    249
    -
    
    250
    -@since base-4.11.0.0
    
    251
    --}
    
    252
    -showHFloat :: RealFloat a => a -> ShowS
    
    253
    -showHFloat = showString . fmt
    
    254
    -  where
    
    255
    -  fmt x
    
    256
    -    | isNaN x                   = "NaN"
    
    257
    -    | isInfinite x              = (if x < 0 then "-" else "") ++ "Infinity"
    
    258
    -    | x < 0 || isNegativeZero x = '-' : cvt (-x)
    
    259
    -    | otherwise                 = cvt x
    
    260
    -
    
    261
    -  cvt x
    
    262
    -    | x == 0 = "0x0p+0"
    
    263
    -    | otherwise =
    
    264
    -      case floatToDigits 2 x of
    
    265
    -        r@([], _) -> error $ "Impossible happened: showHFloat: " ++ show r
    
    266
    -        (d:ds, e) -> "0x" ++ show d ++ frac ds ++ "p" ++ show (e-1)
    
    267
    -
    
    268
    -  -- Given binary digits, convert them to hex in blocks of 4
    
    269
    -  -- Special case: If all 0's, just drop it.
    
    270
    -  frac digits
    
    271
    -    | allZ digits = ""
    
    272
    -    | otherwise   = "." ++ hex digits
    
    273
    -    where
    
    274
    -    hex ds =
    
    275
    -      case ds of
    
    276
    -        []                -> ""
    
    277
    -        [a]               -> hexDigit a 0 0 0 ""
    
    278
    -        [a,b]             -> hexDigit a b 0 0 ""
    
    279
    -        [a,b,c]           -> hexDigit a b c 0 ""
    
    280
    -        a : b : c : d : r -> hexDigit a b c d (hex r)
    
    281
    -
    
    282
    -  hexDigit a b c d = showHex (8*a + 4*b + 2*c + d)
    
    283
    -
    
    284
    -  allZ xs = case xs of
    
    285
    -              x : more -> x == 0 && allZ more
    
    286
    -              []       -> True
    
    21
    +       (
    
    22
    +           seq,
    
    23
    +           ($),
    
    24
    +           otherwise,
    
    25
    +           Char,
    
    26
    +           Int,
    
    27
    +           (<),
    
    28
    +           (<=),
    
    29
    +           errorWithoutStackTrace
    
    30
    +       )
    
    31
    +import GHC.Internal.List ((++))
    
    32
    +import GHC.Internal.Real (Integral, toInteger, fromIntegral, quotRem)
    
    33
    +import GHC.Internal.Show (ShowS, show, intToDigit)
    
    287 34
     
    
    288 35
     -- ---------------------------------------------------------------------------
    
    289 36
     -- Integer printing functions
    
    ... ... @@ -307,11 +54,3 @@ showIntAtBase base toChr n0 r0
    307 54
     -- | Show /non-negative/ 'Integral' numbers in base 16.
    
    308 55
     showHex :: Integral a => a -> ShowS
    
    309 56
     showHex = showIntAtBase 16 intToDigit
    310
    -
    
    311
    --- | Show /non-negative/ 'Integral' numbers in base 8.
    
    312
    -showOct :: Integral a => a -> ShowS
    
    313
    -showOct = showIntAtBase 8  intToDigit
    
    314
    -
    
    315
    --- | Show /non-negative/ 'Integral' numbers in base 2.
    
    316
    -showBin :: Integral a => a -> ShowS
    
    317
    -showBin = showIntAtBase 2  intToDigit

  • libraries/ghc-internal/src/GHC/Internal/System/IO.hs deleted
    1
    -{-# LANGUAGE Trustworthy #-}
    
    2
    -{-# LANGUAGE CPP, NoImplicitPrelude, CApiFFI #-}
    
    3
    -{-# OPTIONS_GHC -Wno-x-partial #-}
    
    4
    -
    
    5
    ------------------------------------------------------------------------------
    
    6
    --- |
    
    7
    --- Module      :  GHC.Internal.System.IO
    
    8
    --- Copyright   :  (c) The University of Glasgow 2001
    
    9
    --- License     :  BSD-style (see the file libraries/base/LICENSE)
    
    10
    ---
    
    11
    --- Maintainer  :  libraries@haskell.org
    
    12
    --- Stability   :  stable
    
    13
    --- Portability :  portable
    
    14
    ---
    
    15
    --- The standard IO library.
    
    16
    ---
    
    17
    ------------------------------------------------------------------------------
    
    18
    -
    
    19
    -module GHC.Internal.System.IO (
    
    20
    -    -- * The IO monad
    
    21
    -
    
    22
    -    IO,
    
    23
    -
    
    24
    -    -- * Files and handles
    
    25
    -
    
    26
    -    FilePath,
    
    27
    -
    
    28
    -    Handle,             -- abstract, instance of: Eq, Show.
    
    29
    -
    
    30
    -    -- | GHC note: a 'Handle' will be automatically closed when the garbage
    
    31
    -    -- collector detects that it has become unreferenced by the program.
    
    32
    -    -- However, relying on this behaviour is not generally recommended:
    
    33
    -    -- the garbage collector is unpredictable.  If possible, use
    
    34
    -    -- an explicit 'hClose' to close 'Handle's when they are no longer
    
    35
    -    -- required.  GHC does not currently attempt to free up file
    
    36
    -    -- descriptors when they have run out, it is your responsibility to
    
    37
    -    -- ensure that this doesn't happen.
    
    38
    -
    
    39
    -    -- ** Standard handles
    
    40
    -
    
    41
    -    -- | Three handles are allocated during program initialisation,
    
    42
    -    -- and are initially open.
    
    43
    -
    
    44
    -    stdin, stdout, stderr,
    
    45
    -
    
    46
    -    -- * Opening and closing files
    
    47
    -
    
    48
    -    -- ** Opening files
    
    49
    -
    
    50
    -    withFile,
    
    51
    -    openFile,
    
    52
    -    IOMode(ReadMode,WriteMode,AppendMode,ReadWriteMode),
    
    53
    -
    
    54
    -    -- ** Closing files
    
    55
    -
    
    56
    -    hClose,
    
    57
    -
    
    58
    -    -- ** Special cases
    
    59
    -
    
    60
    -    -- | These functions are also exported by the "Prelude".
    
    61
    -
    
    62
    -    readFile,
    
    63
    -    readFile',
    
    64
    -    writeFile,
    
    65
    -    appendFile,
    
    66
    -
    
    67
    -    -- * Operations on handles
    
    68
    -
    
    69
    -    -- ** Determining and changing the size of a file
    
    70
    -
    
    71
    -    hFileSize,
    
    72
    -    hSetFileSize,
    
    73
    -
    
    74
    -    -- ** Detecting the end of input
    
    75
    -
    
    76
    -    hIsEOF,
    
    77
    -    isEOF,
    
    78
    -
    
    79
    -    -- ** Buffering operations
    
    80
    -
    
    81
    -    BufferMode(NoBuffering,LineBuffering,BlockBuffering),
    
    82
    -    hSetBuffering,
    
    83
    -    hGetBuffering,
    
    84
    -    hFlush,
    
    85
    -
    
    86
    -    -- ** Repositioning handles
    
    87
    -
    
    88
    -    hGetPosn,
    
    89
    -    hSetPosn,
    
    90
    -    HandlePosn,                -- abstract, instance of: Eq, Show.
    
    91
    -
    
    92
    -    hSeek,
    
    93
    -    SeekMode(AbsoluteSeek,RelativeSeek,SeekFromEnd),
    
    94
    -    hTell,
    
    95
    -
    
    96
    -    -- ** Handle properties
    
    97
    -
    
    98
    -    hIsOpen, hIsClosed,
    
    99
    -    hIsReadable, hIsWritable,
    
    100
    -    hIsSeekable,
    
    101
    -
    
    102
    -    -- ** Terminal operations (not portable: GHC only)
    
    103
    -
    
    104
    -    hIsTerminalDevice,
    
    105
    -
    
    106
    -    hSetEcho,
    
    107
    -    hGetEcho,
    
    108
    -
    
    109
    -    -- ** Showing handle state (not portable: GHC only)
    
    110
    -
    
    111
    -    hShow,
    
    112
    -
    
    113
    -    -- * Text input and output
    
    114
    -
    
    115
    -    -- ** Text input
    
    116
    -
    
    117
    -    hWaitForInput,
    
    118
    -    hReady,
    
    119
    -    hGetChar,
    
    120
    -    hGetLine,
    
    121
    -    hLookAhead,
    
    122
    -    hGetContents,
    
    123
    -    hGetContents',
    
    124
    -
    
    125
    -    -- ** Text output
    
    126
    -
    
    127
    -    hPutChar,
    
    128
    -    hPutStr,
    
    129
    -    hPutStrLn,
    
    130
    -    hPrint,
    
    131
    -
    
    132
    -    -- ** Special cases for standard input and output
    
    133
    -
    
    134
    -    -- | These functions are also exported by the "Prelude".
    
    135
    -
    
    136
    -    interact,
    
    137
    -    putChar,
    
    138
    -    putStr,
    
    139
    -    putStrLn,
    
    140
    -    print,
    
    141
    -    getChar,
    
    142
    -    getLine,
    
    143
    -    getContents,
    
    144
    -    getContents',
    
    145
    -    readIO,
    
    146
    -    readLn,
    
    147
    -
    
    148
    -    -- * Binary input and output
    
    149
    -
    
    150
    -    withBinaryFile,
    
    151
    -    openBinaryFile,
    
    152
    -    hSetBinaryMode,
    
    153
    -    hPutBuf,
    
    154
    -    hGetBuf,
    
    155
    -    hGetBufSome,
    
    156
    -    hPutBufNonBlocking,
    
    157
    -    hGetBufNonBlocking,
    
    158
    -
    
    159
    -    -- * Temporary files
    
    160
    -
    
    161
    -    openTempFile,
    
    162
    -    openBinaryTempFile,
    
    163
    -    openTempFileWithDefaultPermissions,
    
    164
    -    openBinaryTempFileWithDefaultPermissions,
    
    165
    -
    
    166
    -    -- * Unicode encoding\/decoding
    
    167
    -
    
    168
    -    -- | A text-mode 'Handle' has an associated 'TextEncoding', which
    
    169
    -    -- is used to decode bytes into Unicode characters when reading,
    
    170
    -    -- and encode Unicode characters into bytes when writing.
    
    171
    -    --
    
    172
    -    -- The default 'TextEncoding' is the same as the default encoding
    
    173
    -    -- on your system, which is also available as 'localeEncoding'.
    
    174
    -    -- (GHC note: on Windows, we currently do not support double-byte
    
    175
    -    -- encodings; if the console\'s code page is unsupported, then
    
    176
    -    -- 'localeEncoding' will be 'latin1'.)
    
    177
    -    --
    
    178
    -    -- Encoding and decoding errors are always detected and reported,
    
    179
    -    -- except during lazy I/O ('hGetContents', 'getContents', and
    
    180
    -    -- 'readFile'), where a decoding error merely results in
    
    181
    -    -- termination of the character stream, as with other I/O errors.
    
    182
    -
    
    183
    -    hSetEncoding,
    
    184
    -    hGetEncoding,
    
    185
    -
    
    186
    -    -- ** Unicode encodings
    
    187
    -    TextEncoding,
    
    188
    -    latin1,
    
    189
    -    utf8, utf8_bom,
    
    190
    -    utf16, utf16le, utf16be,
    
    191
    -    utf32, utf32le, utf32be,
    
    192
    -    localeEncoding,
    
    193
    -    char8,
    
    194
    -    mkTextEncoding,
    
    195
    -
    
    196
    -    -- * Newline conversion
    
    197
    -
    
    198
    -    -- | In Haskell, a newline is always represented by the character
    
    199
    -    -- @\'\\n\'@.  However, in files and external character streams, a
    
    200
    -    -- newline may be represented by another character sequence, such
    
    201
    -    -- as @\'\\r\\n\'@.
    
    202
    -    --
    
    203
    -    -- A text-mode 'Handle' has an associated 'NewlineMode' that
    
    204
    -    -- specifies how to translate newline characters.  The
    
    205
    -    -- 'NewlineMode' specifies the input and output translation
    
    206
    -    -- separately, so that for instance you can translate @\'\\r\\n\'@
    
    207
    -    -- to @\'\\n\'@ on input, but leave newlines as @\'\\n\'@ on output.
    
    208
    -    --
    
    209
    -    -- The default 'NewlineMode' for a 'Handle' is
    
    210
    -    -- 'nativeNewlineMode', which does no translation on Unix systems,
    
    211
    -    -- but translates @\'\\r\\n\'@ to @\'\\n\'@ and back on Windows.
    
    212
    -    --
    
    213
    -    -- Binary-mode 'Handle's do no newline translation at all.
    
    214
    -    --
    
    215
    -    hSetNewlineMode,
    
    216
    -    hGetNewlineMode,
    
    217
    -    Newline(..), nativeNewline,
    
    218
    -    NewlineMode(..),
    
    219
    -    noNewlineTranslation, universalNewlineMode, nativeNewlineMode,
    
    220
    -  ) where
    
    221
    -
    
    222
    -import GHC.Internal.Control.Exception.Base
    
    223
    -
    
    224
    -import GHC.Internal.Data.Bits
    
    225
    -import GHC.Internal.Data.Maybe
    
    226
    -import GHC.Internal.Foreign.C.Error
    
    227
    -#if defined(mingw32_HOST_OS)
    
    228
    -import GHC.Internal.Foreign.C.String
    
    229
    -import GHC.Internal.Foreign.Ptr
    
    230
    -import GHC.Internal.Foreign.Marshal.Alloc
    
    231
    -import GHC.Internal.Foreign.Marshal.Utils (with)
    
    232
    -import GHC.Internal.Foreign.Storable
    
    233
    -import GHC.Internal.IO.SubSystem
    
    234
    -import GHC.Internal.IO.Windows.Handle (openFileAsTemp)
    
    235
    -import GHC.Internal.IO.Handle.Windows (mkHandleFromHANDLE)
    
    236
    -import GHC.Internal.IO.Device as IODevice
    
    237
    -import GHC.Internal.Real (fromIntegral)
    
    238
    -#endif
    
    239
    -import GHC.Internal.Foreign.C.Types
    
    240
    -import GHC.Internal.System.Posix.Internals
    
    241
    -import GHC.Internal.System.Posix.Types
    
    242
    -
    
    243
    -import GHC.Internal.Base
    
    244
    -import GHC.Internal.List
    
    245
    -#if !defined(mingw32_HOST_OS)
    
    246
    -import GHC.Internal.IORef
    
    247
    -#endif
    
    248
    -import GHC.Internal.Num
    
    249
    -import GHC.Internal.IO hiding ( bracket, onException )
    
    250
    -import GHC.Internal.IO.IOMode
    
    251
    -import qualified GHC.Internal.IO.FD as FD
    
    252
    -import GHC.Internal.IO.Handle
    
    253
    -import qualified GHC.Internal.IO.Handle.FD as POSIX
    
    254
    -import GHC.Internal.IO.Handle.Text ( hGetBufSome, hPutStrLn )
    
    255
    -import GHC.Internal.IO.Exception ( userError )
    
    256
    -import GHC.Internal.IO.Encoding
    
    257
    -import GHC.Internal.Text.Read
    
    258
    -import GHC.Internal.IO.StdHandles
    
    259
    -import GHC.Internal.Show
    
    260
    ------------------------------------------------------------------------------
    
    261
    --- Standard IO
    
    262
    -
    
    263
    --- | Write a character to the standard output device
    
    264
    ---
    
    265
    --- 'putChar' is implemented as @'hPutChar' 'stdout'@.
    
    266
    ---
    
    267
    --- This operation may fail with the same errors as 'hPutChar'.
    
    268
    ---
    
    269
    --- ==== __Examples__
    
    270
    ---
    
    271
    --- Note that the following do not put a newline.
    
    272
    ---
    
    273
    --- >>> putChar 'x'
    
    274
    --- x
    
    275
    ---
    
    276
    --- >>> putChar '\0042'
    
    277
    --- *
    
    278
    -putChar         :: Char -> IO ()
    
    279
    -putChar c       =  hPutChar stdout c
    
    280
    -
    
    281
    --- | Write a string to the standard output device
    
    282
    ---
    
    283
    --- 'putStr' is implemented as @'hPutStr' 'stdout'@.
    
    284
    ---
    
    285
    --- This operation may fail with the same errors, and has the same issues with concurrency, as 'hPutStr'!
    
    286
    ---
    
    287
    --- ==== __Examples__
    
    288
    ---
    
    289
    --- Note that the following do not put a newline.
    
    290
    ---
    
    291
    --- >>> putStr "Hello, World!"
    
    292
    --- Hello, World!
    
    293
    ---
    
    294
    --- >>> putStr "\0052\0042\0050"
    
    295
    --- 4*2
    
    296
    ---
    
    297
    -putStr          :: String -> IO ()
    
    298
    -putStr s        =  hPutStr stdout s
    
    299
    -
    
    300
    --- | The same as 'putStr', but adds a newline character.
    
    301
    ---
    
    302
    --- This operation may fail with the same errors, and has the same issues with concurrency, as 'hPutStr'!
    
    303
    -putStrLn        :: String -> IO ()
    
    304
    -putStrLn s      =  hPutStrLn stdout s
    
    305
    -
    
    306
    --- | The 'print' function outputs a value of any printable type to the
    
    307
    --- standard output device.
    
    308
    --- Printable types are those that are instances of class 'Show'; 'print'
    
    309
    --- converts values to strings for output using the 'show' operation and
    
    310
    --- adds a newline.
    
    311
    ---
    
    312
    --- 'print' is implemented as @'putStrLn' '.' 'show'@
    
    313
    ---
    
    314
    --- This operation may fail with the same errors, and has the same issues with concurrency, as 'hPutStr'!
    
    315
    ---
    
    316
    --- ==== __Examples__
    
    317
    ---
    
    318
    --- >>> print [1, 2, 3]
    
    319
    --- [1,2,3]
    
    320
    ---
    
    321
    --- Be careful when using 'print' for outputting strings,
    
    322
    --- as this will invoke 'show' and cause strings to be printed
    
    323
    --- with quotation marks and non-ascii symbols escaped.
    
    324
    ---
    
    325
    --- >>> print "λ :D"
    
    326
    --- "\995 :D"
    
    327
    ---
    
    328
    --- A program to print the first 8 integers and their
    
    329
    --- powers of 2 could be written as:
    
    330
    ---
    
    331
    --- >>> print [(n, 2^n) | n <- [0..8]]
    
    332
    --- [(0,1),(1,2),(2,4),(3,8),(4,16),(5,32),(6,64),(7,128),(8,256)]
    
    333
    -print           :: Show a => a -> IO ()
    
    334
    -print x         =  putStrLn (show x)
    
    335
    -
    
    336
    --- | Read a single character from the standard input device.
    
    337
    ---
    
    338
    --- 'getChar' is implemented as @'hGetChar' 'stdin'@.
    
    339
    ---
    
    340
    --- This operation may fail with the same errors as 'hGetChar'.
    
    341
    ---
    
    342
    --- ==== __Examples__
    
    343
    ---
    
    344
    --- >>> getChar
    
    345
    --- a'a'
    
    346
    ---
    
    347
    --- >>> getChar
    
    348
    --- >
    
    349
    --- '\n'
    
    350
    -getChar         :: IO Char
    
    351
    -getChar         =  hGetChar stdin
    
    352
    -
    
    353
    --- | Read a line from the standard input device.
    
    354
    ---
    
    355
    --- 'getLine' is implemented as @'hGetLine' 'stdin'@.
    
    356
    ---
    
    357
    --- This operation may fail with the same errors as 'hGetLine'.
    
    358
    ---
    
    359
    --- ==== __Examples__
    
    360
    ---
    
    361
    --- >>> getLine
    
    362
    --- > Hello World!
    
    363
    --- "Hello World!"
    
    364
    ---
    
    365
    --- >>> getLine
    
    366
    --- >
    
    367
    --- ""
    
    368
    -getLine         :: IO String
    
    369
    -getLine         =  hGetLine stdin
    
    370
    -
    
    371
    --- | The 'getContents' operation returns all user input as a single string,
    
    372
    --- which is read lazily as it is needed.
    
    373
    ---
    
    374
    --- 'getContents' is implemented as @'hGetContents' 'stdin'@.
    
    375
    ---
    
    376
    --- This operation may fail with the same errors as 'hGetContents'.
    
    377
    ---
    
    378
    --- ==== __Examples__
    
    379
    ---
    
    380
    --- >>> getContents >>= putStr
    
    381
    --- > aaabbbccc :D
    
    382
    --- aaabbbccc :D
    
    383
    --- > I hope you have a great day
    
    384
    --- I hope you have a great day
    
    385
    --- > ^D
    
    386
    ---
    
    387
    --- >>> getContents >>= print . length
    
    388
    --- > abc
    
    389
    --- > <3
    
    390
    --- > def ^D
    
    391
    --- 11
    
    392
    -getContents     :: IO String
    
    393
    -getContents     =  hGetContents stdin
    
    394
    -
    
    395
    --- | The 'getContents'' operation returns all user input as a single string,
    
    396
    --- which is fully read before being returned
    
    397
    ---
    
    398
    --- 'getContents'' is implemented as @'hGetContents'' 'stdin'@.
    
    399
    ---
    
    400
    --- This operation may fail with the same errors as 'hGetContents''.
    
    401
    ---
    
    402
    --- ==== __Examples__
    
    403
    ---
    
    404
    --- >>> getContents' >>= putStr
    
    405
    --- > aaabbbccc :D
    
    406
    --- > I hope you have a great day
    
    407
    --- aaabbbccc :D
    
    408
    --- I hope you have a great day
    
    409
    ---
    
    410
    --- >>> getContents' >>= print . length
    
    411
    --- > abc
    
    412
    --- > <3
    
    413
    --- > def ^D
    
    414
    --- 11
    
    415
    ---
    
    416
    --- @since base-4.15.0.0
    
    417
    -getContents'    :: IO String
    
    418
    -getContents'    =  hGetContents' stdin
    
    419
    -
    
    420
    --- | @'interact' f@ takes the entire input from 'stdin' and applies @f@ to it.
    
    421
    --- The resulting string is written to the 'stdout' device.
    
    422
    ---
    
    423
    --- Note that this operation is lazy, which allows to produce output
    
    424
    --- even before all input has been consumed.
    
    425
    ---
    
    426
    --- This operation may fail with the same errors as 'getContents' and 'putStr'.
    
    427
    ---
    
    428
    --- If it doesn't produce output the buffering settings may not be
    
    429
    --- correct, use ^D (ctrl+D) to close stdin which forces
    
    430
    --- the buffer to be consumed.
    
    431
    ---
    
    432
    --- You may wish to set the buffering style appropriate to your program's
    
    433
    --- needs before using this function, for example:
    
    434
    ---
    
    435
    --- @
    
    436
    --- main :: IO ()
    
    437
    --- main = do
    
    438
    ---   hSetBuffering stdin LineBuffering
    
    439
    ---   hSetBuffering stdout NoBuffering
    
    440
    ---   interact (concatMap (\str -> str ++ str) . L.lines)
    
    441
    --- @
    
    442
    ---
    
    443
    --- ==== __Examples__
    
    444
    ---
    
    445
    --- >>> interact (\str -> str ++ str)
    
    446
    --- > hi :)
    
    447
    --- hi :)
    
    448
    --- > ^D
    
    449
    --- hi :)
    
    450
    ---
    
    451
    --- >>> interact (const ":D")
    
    452
    --- :D
    
    453
    ---
    
    454
    --- >>> interact (show . words)
    
    455
    --- > hello world!
    
    456
    --- > I hope you have a great day
    
    457
    --- > ^D
    
    458
    --- ["hello","world!","I","hope","you","have","a","great","day"]
    
    459
    -interact        ::  (String -> String) -> IO ()
    
    460
    -interact f      =   do s <- getContents
    
    461
    -                       putStr (f s)
    
    462
    -
    
    463
    --- | The 'readFile' function reads a file and
    
    464
    --- returns the contents of the file as a string.
    
    465
    ---
    
    466
    --- The file is read lazily, on demand, as with 'getContents'.
    
    467
    ---
    
    468
    --- This operation may fail with the same errors as 'hGetContents' and 'openFile'.
    
    469
    ---
    
    470
    --- ==== __Examples__
    
    471
    ---
    
    472
    --- >>> readFile "~/hello_world"
    
    473
    --- "Greetings!"
    
    474
    ---
    
    475
    --- >>> take 5 <$> readFile "/dev/zero"
    
    476
    --- "\NUL\NUL\NUL\NUL\NUL"
    
    477
    -readFile        :: FilePath -> IO String
    
    478
    -readFile name   =  openFile name ReadMode >>= hGetContents
    
    479
    -
    
    480
    --- | The 'readFile'' function reads a file and
    
    481
    --- returns the contents of the file as a string.
    
    482
    ---
    
    483
    --- This is identical to 'readFile', but the file is fully read before being returned,
    
    484
    --- as with 'getContents''.
    
    485
    ---
    
    486
    --- @since base-4.15.0.0
    
    487
    -readFile'       :: FilePath -> IO String
    
    488
    --- There's a bit of overkill here—both withFile and
    
    489
    --- hGetContents' will close the file in the end.
    
    490
    -readFile' name  =  withFile name ReadMode hGetContents'
    
    491
    -
    
    492
    --- | The computation @'writeFile' file str@ function writes the string @str@,
    
    493
    --- to the file @file@.
    
    494
    ---
    
    495
    --- This operation may fail with the same errors as 'hPutStr' and 'withFile'.
    
    496
    ---
    
    497
    --- ==== __Examples__
    
    498
    ---
    
    499
    --- >>> writeFile "hello" "world" >> readFile "hello"
    
    500
    --- "world"
    
    501
    ---
    
    502
    --- >>> writeFile "~/" "D:"
    
    503
    --- *** Exception: ~/: withFile: inappropriate type (Is a directory)
    
    504
    -writeFile :: FilePath -> String -> IO ()
    
    505
    -writeFile f txt = withFile f WriteMode (\ hdl -> hPutStr hdl txt)
    
    506
    -
    
    507
    --- | The computation @'appendFile' file str@ function appends the string @str@,
    
    508
    --- to the file @file@.
    
    509
    ---
    
    510
    --- Note that 'writeFile' and 'appendFile' write a literal string
    
    511
    --- to a file.  To write a value of any printable type, as with 'print',
    
    512
    --- use the 'show' function to convert the value to a string first.
    
    513
    ---
    
    514
    --- This operation may fail with the same errors as 'hPutStr' and 'withFile'.
    
    515
    ---
    
    516
    --- ==== __Examples__
    
    517
    ---
    
    518
    --- The following example could be more efficently written by acquiring a handle
    
    519
    --- instead with 'openFile' and using the computations capable of writing to handles
    
    520
    --- such as 'hPutStr'.
    
    521
    ---
    
    522
    --- >>> let fn = "hello_world"
    
    523
    --- >>> in writeFile fn "hello" >> appendFile fn " world!" >> (readFile fn >>= putStrLn)
    
    524
    --- "hello world!"
    
    525
    ---
    
    526
    --- >>> let fn = "foo"; output = readFile' fn >>= putStrLn
    
    527
    --- >>> in output >> appendFile fn (show [1,2,3]) >> output
    
    528
    --- this is what's in the file
    
    529
    --- this is what's in the file[1,2,3]
    
    530
    -appendFile      :: FilePath -> String -> IO ()
    
    531
    -appendFile f txt = withFile f AppendMode (\ hdl -> hPutStr hdl txt)
    
    532
    -
    
    533
    --- | The 'readLn' function combines 'getLine' and 'readIO'.
    
    534
    ---
    
    535
    --- This operation may fail with the same errors as 'getLine' and 'readIO'.
    
    536
    ---
    
    537
    --- ==== __Examples__
    
    538
    ---
    
    539
    --- >>> fmap (+ 5) readLn
    
    540
    --- > 25
    
    541
    --- 30
    
    542
    ---
    
    543
    --- >>> readLn :: IO String
    
    544
    --- > this is not a string literal
    
    545
    --- *** Exception: user error (Prelude.readIO: no parse)
    
    546
    -readLn :: Read a => IO a
    
    547
    -readLn = getLine >>= readIO
    
    548
    -
    
    549
    --- | The 'readIO' function is similar to 'read' except that it signals
    
    550
    --- parse failure to the 'IO' monad instead of terminating the program.
    
    551
    ---
    
    552
    --- This operation may fail with:
    
    553
    ---
    
    554
    ---  * 'GHC.Internal.System.IO.Error.isUserError' if there is no unambiguous parse.
    
    555
    ---
    
    556
    --- ==== __Examples__
    
    557
    ---
    
    558
    --- >>> fmap (+ 1) (readIO "1")
    
    559
    --- 2
    
    560
    ---
    
    561
    --- >>> readIO "not quite ()" :: IO ()
    
    562
    --- *** Exception: user error (Prelude.readIO: no parse)
    
    563
    -readIO          :: Read a => String -> IO a
    
    564
    -readIO s        =  case (do { (x,t) <- reads s ;
    
    565
    -                              ("","") <- lex t ;
    
    566
    -                              return x }) of
    
    567
    -                        [x]    -> return x
    
    568
    -                        []     -> ioError (userError "Prelude.readIO: no parse")
    
    569
    -                        _      -> ioError (userError "Prelude.readIO: ambiguous parse")
    
    570
    -
    
    571
    --- | The encoding of the current locale.
    
    572
    ---
    
    573
    --- This is the initial locale encoding: if it has been subsequently changed by
    
    574
    --- 'GHC.Internal.IO.Encoding.setLocaleEncoding' this value will not reflect that change.
    
    575
    -localeEncoding :: TextEncoding
    
    576
    -localeEncoding = initLocaleEncoding
    
    577
    -
    
    578
    --- | Computation 'hReady' @hdl@ indicates whether at least one item is
    
    579
    --- available for input from handle @hdl@.
    
    580
    ---
    
    581
    --- This operation may fail with:
    
    582
    ---
    
    583
    ---  * 'GHC.Internal.System.IO.Error.isEOFError' if the end of file has been reached.
    
    584
    -hReady          :: Handle -> IO Bool
    
    585
    -hReady h        =  hWaitForInput h 0
    
    586
    -
    
    587
    --- | Computation 'hPrint' @hdl t@ writes the string representation of @t@
    
    588
    --- given by the 'show' function to the file or channel managed by @hdl@
    
    589
    --- and appends a newline.
    
    590
    ---
    
    591
    --- This operation may fail with the same errors as 'hPutStrLn'
    
    592
    ---
    
    593
    --- ==== __Examples__
    
    594
    ---
    
    595
    --- >>> hPrint stdout [1,2,3]
    
    596
    --- [1,2,3]
    
    597
    ---
    
    598
    --- >>> hPrint stdin [4,5,6]
    
    599
    --- *** Exception: <stdin>: hPutStr: illegal operation (handle is not open for writing)
    
    600
    -hPrint          :: Show a => Handle -> a -> IO ()
    
    601
    -hPrint hdl      =  hPutStrLn hdl . show
    
    602
    -
    
    603
    --- | The function creates a temporary file in ReadWrite mode.
    
    604
    --- The created file isn\'t deleted automatically, so you need to delete it manually.
    
    605
    ---
    
    606
    --- The file is created with permissions such that only the current
    
    607
    --- user can read\/write it.
    
    608
    ---
    
    609
    --- With some exceptions (see below), the file will be created securely
    
    610
    --- in the sense that an attacker should not be able to cause
    
    611
    --- openTempFile to overwrite another file on the filesystem using your
    
    612
    --- credentials, by putting symbolic links (on Unix) in the place where
    
    613
    --- the temporary file is to be created.  On Unix the @O_CREAT@ and
    
    614
    --- @O_EXCL@ flags are used to prevent this attack, but note that
    
    615
    --- @O_EXCL@ is sometimes not supported on NFS filesystems, so if you
    
    616
    --- rely on this behaviour it is best to use local filesystems only.
    
    617
    -openTempFile :: FilePath   -- ^ Directory in which to create the file
    
    618
    -             -> String     -- ^ File name template. If the template is \"foo.ext\" then
    
    619
    -                           -- the created file will be \"fooXXX.ext\" where XXX is some
    
    620
    -                           -- random number. Note that this should not contain any path
    
    621
    -                           -- separator characters. On Windows, the template prefix may
    
    622
    -                           -- be truncated to 3 chars, e.g. \"foobar.ext\" will be
    
    623
    -                           -- \"fooXXX.ext\".
    
    624
    -             -> IO (FilePath, Handle)
    
    625
    -openTempFile tmp_dir template
    
    626
    -    = openTempFile' "openTempFile" tmp_dir template False 0o600
    
    627
    -
    
    628
    --- | Like 'openTempFile', but opens the file in binary mode. See 'openBinaryFile' for more comments.
    
    629
    -openBinaryTempFile :: FilePath -> String -> IO (FilePath, Handle)
    
    630
    -openBinaryTempFile tmp_dir template
    
    631
    -    = openTempFile' "openBinaryTempFile" tmp_dir template True 0o600
    
    632
    -
    
    633
    --- | Like 'openTempFile', but uses the default file permissions
    
    634
    -openTempFileWithDefaultPermissions :: FilePath -> String
    
    635
    -                                   -> IO (FilePath, Handle)
    
    636
    -openTempFileWithDefaultPermissions tmp_dir template
    
    637
    -    = openTempFile' "openTempFileWithDefaultPermissions" tmp_dir template False 0o666
    
    638
    -
    
    639
    --- | Like 'openBinaryTempFile', but uses the default file permissions
    
    640
    -openBinaryTempFileWithDefaultPermissions :: FilePath -> String
    
    641
    -                                         -> IO (FilePath, Handle)
    
    642
    -openBinaryTempFileWithDefaultPermissions tmp_dir template
    
    643
    -    = openTempFile' "openBinaryTempFileWithDefaultPermissions" tmp_dir template True 0o666
    
    644
    -
    
    645
    -openTempFile' :: String -> FilePath -> String -> Bool -> CMode
    
    646
    -              -> IO (FilePath, Handle)
    
    647
    -openTempFile' loc tmp_dir template binary mode
    
    648
    -    | pathSeparator template
    
    649
    -    = failIO $ "openTempFile': Template string must not contain path separator characters: "++template
    
    650
    -    | otherwise = findTempName
    
    651
    -  where
    
    652
    -    -- We split off the last extension, so we can use .foo.ext files
    
    653
    -    -- for temporary files (hidden on Unix OSes). Unfortunately we're
    
    654
    -    -- below filepath in the hierarchy here.
    
    655
    -    (prefix, suffix) =
    
    656
    -       case break (== '.') $ reverse template of
    
    657
    -         -- First case: template contains no '.'s. Just re-reverse it.
    
    658
    -         (rev_suffix, "")       -> (reverse rev_suffix, "")
    
    659
    -         -- Second case: template contains at least one '.'. Strip the
    
    660
    -         -- dot from the prefix and prepend it to the suffix (if we don't
    
    661
    -         -- do this, the unique number will get added after the '.' and
    
    662
    -         -- thus be part of the extension, which is wrong.)
    
    663
    -         (rev_suffix, '.':rest) -> (reverse rest, '.':reverse rev_suffix)
    
    664
    -         -- Otherwise, something is wrong, because (break (== '.')) should
    
    665
    -         -- always return a pair with either the empty string or a string
    
    666
    -         -- beginning with '.' as the second component.
    
    667
    -         _                      -> errorWithoutStackTrace "bug in GHC.Internal.System.IO.openTempFile"
    
    668
    -#if defined(mingw32_HOST_OS)
    
    669
    -    findTempName = findTempNamePosix <!> findTempNameWinIO
    
    670
    -
    
    671
    -    findTempNameWinIO = do
    
    672
    -      let label = if null prefix then "ghc" else prefix
    
    673
    -      withCWString tmp_dir $ \c_tmp_dir ->
    
    674
    -        withCWString label $ \c_template ->
    
    675
    -          withCWString suffix $ \c_suffix ->
    
    676
    -            with nullPtr $ \c_ptr -> do
    
    677
    -              res <- c_createUUIDTempFileErrNo c_tmp_dir c_template c_suffix c_ptr
    
    678
    -              if not res
    
    679
    -                 then do errno <- getErrno
    
    680
    -                         ioError (errnoToIOError loc errno Nothing (Just tmp_dir))
    
    681
    -                 else do c_p <- peek c_ptr
    
    682
    -                         filename <- peekCWString c_p
    
    683
    -                         free c_p
    
    684
    -                         let flags = fromIntegral mode .&. o_EXCL
    
    685
    -                         handleResultsWinIO filename (flags == o_EXCL)
    
    686
    -
    
    687
    -    findTempNamePosix = do
    
    688
    -      let label = if null prefix then "ghc" else prefix
    
    689
    -      withCWString tmp_dir $ \c_tmp_dir ->
    
    690
    -        withCWString label $ \c_template ->
    
    691
    -          withCWString suffix $ \c_suffix ->
    
    692
    -            allocaBytes (sizeOf (undefined :: CWchar) * 260) $ \c_str -> do
    
    693
    -            res <- c_getTempFileNameErrorNo c_tmp_dir c_template c_suffix 0
    
    694
    -                                            c_str
    
    695
    -            if not res
    
    696
    -               then do errno <- getErrno
    
    697
    -                       ioError (errnoToIOError loc errno Nothing (Just tmp_dir))
    
    698
    -               else do filename <- peekCWString c_str
    
    699
    -                       handleResultsPosix filename
    
    700
    -
    
    701
    -    handleResultsPosix filename = do
    
    702
    -      let oflags1 = rw_flags .|. o_EXCL
    
    703
    -          binary_flags
    
    704
    -              | binary    = o_BINARY
    
    705
    -              | otherwise = 0
    
    706
    -          oflags = oflags1 .|. binary_flags
    
    707
    -      fd <- withFilePath filename $ \ f -> c_open f oflags mode
    
    708
    -      case fd < 0 of
    
    709
    -        True -> do errno <- getErrno
    
    710
    -                   ioError (errnoToIOError loc errno Nothing (Just tmp_dir))
    
    711
    -        False ->
    
    712
    -          do (fD,fd_type) <- FD.mkFD fd ReadWriteMode Nothing{-no stat-}
    
    713
    -                                     False{-is_socket-}
    
    714
    -                                     True{-is_nonblock-}
    
    715
    -
    
    716
    -             enc <- getLocaleEncoding
    
    717
    -             h <- POSIX.mkHandleFromFD fD fd_type filename ReadWriteMode
    
    718
    -                                 False{-set non-block-} (Just enc)
    
    719
    -
    
    720
    -             return (filename, h)
    
    721
    -
    
    722
    -    handleResultsWinIO filename excl = do
    
    723
    -      (hwnd, hwnd_type) <- openFileAsTemp filename True excl
    
    724
    -      mb_codec <- if binary then return Nothing else fmap Just getLocaleEncoding
    
    725
    -
    
    726
    -      -- then use it to make a Handle
    
    727
    -      h <- mkHandleFromHANDLE hwnd hwnd_type filename ReadWriteMode mb_codec
    
    728
    -                `onException` IODevice.close hwnd
    
    729
    -      return (filename, h)
    
    730
    -
    
    731
    -foreign import ccall "getTempFileNameErrorNo" c_getTempFileNameErrorNo
    
    732
    -  :: CWString -> CWString -> CWString -> CUInt -> Ptr CWchar -> IO Bool
    
    733
    -
    
    734
    -foreign import ccall "__createUUIDTempFileErrNo" c_createUUIDTempFileErrNo
    
    735
    -  :: CWString -> CWString -> CWString -> Ptr CWString -> IO Bool
    
    736
    -
    
    737
    -pathSeparator :: String -> Bool
    
    738
    -pathSeparator template = any (\x-> x == '/' || x == '\\') template
    
    739
    -
    
    740
    -output_flags = std_flags
    
    741
    -#else /* else mingw32_HOST_OS */
    
    742
    -    findTempName = do
    
    743
    -      rs <- rand_string
    
    744
    -      let filename = prefix ++ rs ++ suffix
    
    745
    -          filepath = tmp_dir `combine` filename
    
    746
    -      r <- openNewFile filepath binary mode
    
    747
    -      case r of
    
    748
    -        FileExists -> findTempName
    
    749
    -        OpenNewError errno -> ioError (errnoToIOError loc errno Nothing (Just tmp_dir))
    
    750
    -        NewFileCreated fd -> do
    
    751
    -          (fD,fd_type) <- FD.mkFD fd ReadWriteMode Nothing{-no stat-}
    
    752
    -                               False{-is_socket-}
    
    753
    -                               True{-is_nonblock-}
    
    754
    -
    
    755
    -          enc <- getLocaleEncoding
    
    756
    -          h <- POSIX.mkHandleFromFD fD fd_type filepath ReadWriteMode False{-set non-block-} (Just enc)
    
    757
    -
    
    758
    -          return (filepath, h)
    
    759
    -
    
    760
    -      where
    
    761
    -        -- XXX bits copied from System.FilePath, since that's not available here
    
    762
    -        combine a b
    
    763
    -                  | null b = a
    
    764
    -                  | null a = b
    
    765
    -                  | pathSeparator [last a] = a ++ b
    
    766
    -                  | otherwise = a ++ [pathSeparatorChar] ++ b
    
    767
    -
    
    768
    -tempCounter :: IORef Int
    
    769
    -tempCounter = unsafePerformIO $ newIORef 0
    
    770
    -{-# NOINLINE tempCounter #-}
    
    771
    -
    
    772
    --- build large digit-alike number
    
    773
    -rand_string :: IO String
    
    774
    -rand_string = do
    
    775
    -  r1 <- c_getpid
    
    776
    -  (r2, _) <- atomicModifyIORef'_ tempCounter (+1)
    
    777
    -  return $ show r1 ++ "-" ++ show r2
    
    778
    -
    
    779
    -data OpenNewFileResult
    
    780
    -  = NewFileCreated CInt
    
    781
    -  | FileExists
    
    782
    -  | OpenNewError Errno
    
    783
    -
    
    784
    -openNewFile :: FilePath -> Bool -> CMode -> IO OpenNewFileResult
    
    785
    -openNewFile filepath binary mode = do
    
    786
    -  let oflags1 = rw_flags .|. o_EXCL
    
    787
    -
    
    788
    -      binary_flags
    
    789
    -        | binary    = o_BINARY
    
    790
    -        | otherwise = 0
    
    791
    -
    
    792
    -      oflags = oflags1 .|. binary_flags
    
    793
    -  fd <- withFilePath filepath $ \ f ->
    
    794
    -          c_open f oflags mode
    
    795
    -  if fd < 0
    
    796
    -    then do
    
    797
    -      errno <- getErrno
    
    798
    -      case errno of
    
    799
    -        _ | errno == eEXIST -> return FileExists
    
    800
    -        _ -> return (OpenNewError errno)
    
    801
    -    else return (NewFileCreated fd)
    
    802
    -
    
    803
    --- XXX Should use filepath library
    
    804
    -pathSeparatorChar :: Char
    
    805
    -pathSeparatorChar = '/'
    
    806
    -
    
    807
    -pathSeparator :: String -> Bool
    
    808
    -pathSeparator template = pathSeparatorChar `elem` template
    
    809
    -
    
    810
    -output_flags = std_flags    .|. o_CREAT
    
    811
    -#endif /* mingw32_HOST_OS */
    
    812
    -
    
    813
    --- XXX Copied from GHC.Handle
    
    814
    -std_flags, output_flags, rw_flags :: CInt
    
    815
    -std_flags    = o_NONBLOCK   .|. o_NOCTTY
    
    816
    -rw_flags     = output_flags .|. o_RDWR

  • testsuite/tests/interface-stability/base-exports.stdout
    ... ... @@ -7848,6 +7848,7 @@ module GHC.IO.Handle where
    7848 7848
       hGetEcho :: Handle -> GHC.Internal.Types.IO GHC.Internal.Types.Bool
    
    7849 7849
       hGetEncoding :: Handle -> GHC.Internal.Types.IO (GHC.Internal.Maybe.Maybe GHC.Internal.IO.Encoding.Types.TextEncoding)
    
    7850 7850
       hGetLine :: Handle -> GHC.Internal.Types.IO GHC.Internal.Base.String
    
    7851
    +  hGetNewlineMode :: Handle -> GHC.Internal.Types.IO NewlineMode
    
    7851 7852
       hGetPosn :: Handle -> GHC.Internal.Types.IO HandlePosn
    
    7852 7853
       hIsClosed :: Handle -> GHC.Internal.Types.IO GHC.Internal.Types.Bool
    
    7853 7854
       hIsEOF :: Handle -> GHC.Internal.Types.IO GHC.Internal.Types.Bool
    
    ... ... @@ -9452,7 +9453,7 @@ module GHC.Word where
    9452 9453
       uncheckedShiftRL64# :: GHC.Internal.Prim.Word64# -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.Word64#
    
    9453 9454
     
    
    9454 9455
     module Numeric where
    
    9455
    -  -- Safety: Safe
    
    9456
    +  -- Safety: Trustworthy
    
    9456 9457
       type Floating :: * -> Constraint
    
    9457 9458
       class GHC.Internal.Real.Fractional a => Floating a where
    
    9458 9459
         pi :: a
    
    ... ... @@ -9883,7 +9884,7 @@ module System.Exit where
    9883 9884
       exitWith :: forall a. ExitCode -> GHC.Internal.Types.IO a
    
    9884 9885
     
    
    9885 9886
     module System.IO where
    
    9886
    -  -- Safety: Safe
    
    9887
    +  -- Safety: Trustworthy
    
    9887 9888
       type BufferMode :: *
    
    9888 9889
       data BufferMode = NoBuffering | LineBuffering | BlockBuffering (GHC.Internal.Maybe.Maybe GHC.Internal.Types.Int)
    
    9889 9890
       type FilePath :: *
    
    ... ... @@ -12495,7 +12496,7 @@ instance forall a. GHC.Internal.Read.Read a => GHC.Internal.Read.Read (Data.Semi
    12495 12496
     instance forall a. GHC.Internal.Read.Read a => GHC.Internal.Read.Read (Data.Semigroup.Min a) -- Defined in ‘Data.Semigroup’
    
    12496 12497
     instance forall m. GHC.Internal.Read.Read m => GHC.Internal.Read.Read (Data.Semigroup.WrappedMonoid m) -- Defined in ‘Data.Semigroup’
    
    12497 12498
     instance forall k (a :: k) (b :: k). Coercible a b => GHC.Internal.Read.Read (GHC.Internal.Data.Type.Coercion.Coercion a b) -- Defined in ‘GHC.Internal.Data.Type.Coercion’
    
    12498
    -instance GHC.Internal.Read.Read GHC.Internal.Data.Version.Version -- Defined in ‘GHC.Internal.Data.Version’
    
    12499
    +instance [safe] GHC.Internal.Read.Read GHC.Internal.Data.Version.Version -- Defined in ‘Data.Version’
    
    12499 12500
     instance GHC.Internal.Read.Read GHC.Internal.Foreign.Ptr.IntPtr -- Defined in ‘GHC.Internal.Foreign.Ptr’
    
    12500 12501
     instance GHC.Internal.Read.Read GHC.Internal.Foreign.Ptr.WordPtr -- Defined in ‘GHC.Internal.Foreign.Ptr’
    
    12501 12502
     instance GHC.Internal.Read.Read GHC.Internal.Foreign.C.Types.CBool -- Defined in ‘GHC.Internal.Foreign.C.Types’
    
    ... ... @@ -12524,7 +12525,7 @@ instance GHC.Internal.Read.Read GHC.Internal.Foreign.C.Types.CULong -- Defined i
    12524 12525
     instance GHC.Internal.Read.Read GHC.Internal.Foreign.C.Types.CUSeconds -- Defined in ‘GHC.Internal.Foreign.C.Types’
    
    12525 12526
     instance GHC.Internal.Read.Read GHC.Internal.Foreign.C.Types.CUShort -- Defined in ‘GHC.Internal.Foreign.C.Types’
    
    12526 12527
     instance GHC.Internal.Read.Read GHC.Internal.Foreign.C.Types.CWchar -- Defined in ‘GHC.Internal.Foreign.C.Types’
    
    12527
    -instance GHC.Internal.Read.Read GHC.Internal.ByteOrder.ByteOrder -- Defined in ‘GHC.Internal.ByteOrder’
    
    12528
    +instance [safe] GHC.Internal.Read.Read GHC.Internal.ByteOrder.ByteOrder -- Defined in ‘GHC.ByteOrder’
    
    12528 12529
     instance forall k (f :: k -> *) (g :: k -> *) (p :: k). (GHC.Internal.Read.Read (f p), GHC.Internal.Read.Read (g p)) => GHC.Internal.Read.Read ((GHC.Internal.Generics.:*:) f g p) -- Defined in ‘GHC.Internal.Generics’
    
    12529 12530
     instance forall k (f :: k -> *) (g :: k -> *) (p :: k). (GHC.Internal.Read.Read (f p), GHC.Internal.Read.Read (g p)) => GHC.Internal.Read.Read ((GHC.Internal.Generics.:+:) f g p) -- Defined in ‘GHC.Internal.Generics’
    
    12530 12531
     instance forall k2 (f :: k2 -> *) k1 (g :: k1 -> k2) (p :: k1). GHC.Internal.Read.Read (f (g p)) => GHC.Internal.Read.Read ((GHC.Internal.Generics.:.:) f g p) -- Defined in ‘GHC.Internal.Generics’
    
    ... ... @@ -12539,16 +12540,16 @@ instance GHC.Internal.Read.Read GHC.Internal.Generics.SourceStrictness -- Define
    12539 12540
     instance GHC.Internal.Read.Read GHC.Internal.Generics.SourceUnpackedness -- Defined in ‘GHC.Internal.Generics’
    
    12540 12541
     instance forall k (p :: k). GHC.Internal.Read.Read (GHC.Internal.Generics.U1 p) -- Defined in ‘GHC.Internal.Generics’
    
    12541 12542
     instance forall k (p :: k). GHC.Internal.Read.Read (GHC.Internal.Generics.V1 p) -- Defined in ‘GHC.Internal.Generics’
    
    12542
    -instance GHC.Internal.Read.Read GHC.Internal.IO.Device.SeekMode -- Defined in ‘GHC.Internal.IO.Device’
    
    12543
    -instance GHC.Internal.Read.Read GHC.Internal.IO.Handle.Types.BufferMode -- Defined in ‘GHC.Internal.IO.Handle.Types’
    
    12544
    -instance GHC.Internal.Read.Read GHC.Internal.IO.Handle.Types.Newline -- Defined in ‘GHC.Internal.IO.Handle.Types’
    
    12545
    -instance GHC.Internal.Read.Read GHC.Internal.IO.Handle.Types.NewlineMode -- Defined in ‘GHC.Internal.IO.Handle.Types’
    
    12546
    -instance GHC.Internal.Read.Read GHC.Internal.IO.IOMode.IOMode -- Defined in ‘GHC.Internal.IO.IOMode’
    
    12547 12543
     instance [safe] GHC.Internal.Read.Read GHC.Stats.GCDetails -- Defined in ‘GHC.Stats’
    
    12548 12544
     instance [safe] GHC.Internal.Read.Read GHC.Stats.RTSStats -- Defined in ‘GHC.Stats’
    
    12549 12545
     instance GHC.Internal.Read.Read GHC.Internal.TypeNats.SomeNat -- Defined in ‘GHC.Internal.TypeNats’
    
    12550 12546
     instance GHC.Internal.Read.Read GHC.Internal.TypeLits.SomeChar -- Defined in ‘GHC.Internal.TypeLits’
    
    12551 12547
     instance GHC.Internal.Read.Read GHC.Internal.TypeLits.SomeSymbol -- Defined in ‘GHC.Internal.TypeLits’
    
    12548
    +instance GHC.Internal.Read.Read GHC.Internal.IO.Handle.Types.BufferMode -- Defined in ‘System.IO’
    
    12549
    +instance GHC.Internal.Read.Read GHC.Internal.IO.IOMode.IOMode -- Defined in ‘System.IO’
    
    12550
    +instance GHC.Internal.Read.Read GHC.Internal.IO.Handle.Types.Newline -- Defined in ‘System.IO’
    
    12551
    +instance GHC.Internal.Read.Read GHC.Internal.IO.Handle.Types.NewlineMode -- Defined in ‘System.IO’
    
    12552
    +instance GHC.Internal.Read.Read GHC.Internal.IO.Device.SeekMode -- Defined in ‘System.IO’
    
    12552 12553
     instance forall a k (b :: k). GHC.Internal.Real.Fractional a => GHC.Internal.Real.Fractional (GHC.Internal.Data.Functor.Const.Const a b) -- Defined in ‘GHC.Internal.Data.Functor.Const’
    
    12553 12554
     instance forall a. GHC.Internal.Float.RealFloat a => GHC.Internal.Real.Fractional (Data.Complex.Complex a) -- Defined in ‘Data.Complex’
    
    12554 12555
     instance forall k (a :: k). Data.Fixed.HasResolution a => GHC.Internal.Real.Fractional (Data.Fixed.Fixed a) -- Defined in ‘Data.Fixed’
    

  • testsuite/tests/interface-stability/base-exports.stdout-javascript-unknown-ghcjs
    ... ... @@ -7820,6 +7820,7 @@ module GHC.IO.Handle where
    7820 7820
       hGetEcho :: Handle -> GHC.Internal.Types.IO GHC.Internal.Types.Bool
    
    7821 7821
       hGetEncoding :: Handle -> GHC.Internal.Types.IO (GHC.Internal.Maybe.Maybe GHC.Internal.IO.Encoding.Types.TextEncoding)
    
    7822 7822
       hGetLine :: Handle -> GHC.Internal.Types.IO GHC.Internal.Base.String
    
    7823
    +  hGetNewlineMode :: Handle -> GHC.Internal.Types.IO NewlineMode
    
    7823 7824
       hGetPosn :: Handle -> GHC.Internal.Types.IO HandlePosn
    
    7824 7825
       hIsClosed :: Handle -> GHC.Internal.Types.IO GHC.Internal.Types.Bool
    
    7825 7826
       hIsEOF :: Handle -> GHC.Internal.Types.IO GHC.Internal.Types.Bool
    
    ... ... @@ -9490,7 +9491,7 @@ module GHC.Word where
    9490 9491
       uncheckedShiftRL64# :: GHC.Internal.Prim.Word64# -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.Word64#
    
    9491 9492
     
    
    9492 9493
     module Numeric where
    
    9493
    -  -- Safety: Safe
    
    9494
    +  -- Safety: Trustworthy
    
    9494 9495
       type Floating :: * -> Constraint
    
    9495 9496
       class GHC.Internal.Real.Fractional a => Floating a where
    
    9496 9497
         pi :: a
    
    ... ... @@ -9921,7 +9922,7 @@ module System.Exit where
    9921 9922
       exitWith :: forall a. ExitCode -> GHC.Internal.Types.IO a
    
    9922 9923
     
    
    9923 9924
     module System.IO where
    
    9924
    -  -- Safety: Safe
    
    9925
    +  -- Safety: Trustworthy
    
    9925 9926
       type BufferMode :: *
    
    9926 9927
       data BufferMode = NoBuffering | LineBuffering | BlockBuffering (GHC.Internal.Maybe.Maybe GHC.Internal.Types.Int)
    
    9927 9928
       type FilePath :: *
    
    ... ... @@ -12524,7 +12525,7 @@ instance forall a. GHC.Internal.Read.Read a => GHC.Internal.Read.Read (Data.Semi
    12524 12525
     instance forall a. GHC.Internal.Read.Read a => GHC.Internal.Read.Read (Data.Semigroup.Min a) -- Defined in ‘Data.Semigroup’
    
    12525 12526
     instance forall m. GHC.Internal.Read.Read m => GHC.Internal.Read.Read (Data.Semigroup.WrappedMonoid m) -- Defined in ‘Data.Semigroup’
    
    12526 12527
     instance forall k (a :: k) (b :: k). Coercible a b => GHC.Internal.Read.Read (GHC.Internal.Data.Type.Coercion.Coercion a b) -- Defined in ‘GHC.Internal.Data.Type.Coercion’
    
    12527
    -instance GHC.Internal.Read.Read GHC.Internal.Data.Version.Version -- Defined in ‘GHC.Internal.Data.Version’
    
    12528
    +instance [safe] GHC.Internal.Read.Read GHC.Internal.Data.Version.Version -- Defined in ‘Data.Version’
    
    12528 12529
     instance GHC.Internal.Read.Read GHC.Internal.Foreign.Ptr.IntPtr -- Defined in ‘GHC.Internal.Foreign.Ptr’
    
    12529 12530
     instance GHC.Internal.Read.Read GHC.Internal.Foreign.Ptr.WordPtr -- Defined in ‘GHC.Internal.Foreign.Ptr’
    
    12530 12531
     instance GHC.Internal.Read.Read GHC.Internal.Foreign.C.Types.CBool -- Defined in ‘GHC.Internal.Foreign.C.Types’
    
    ... ... @@ -12553,7 +12554,7 @@ instance GHC.Internal.Read.Read GHC.Internal.Foreign.C.Types.CULong -- Defined i
    12553 12554
     instance GHC.Internal.Read.Read GHC.Internal.Foreign.C.Types.CUSeconds -- Defined in ‘GHC.Internal.Foreign.C.Types’
    
    12554 12555
     instance GHC.Internal.Read.Read GHC.Internal.Foreign.C.Types.CUShort -- Defined in ‘GHC.Internal.Foreign.C.Types’
    
    12555 12556
     instance GHC.Internal.Read.Read GHC.Internal.Foreign.C.Types.CWchar -- Defined in ‘GHC.Internal.Foreign.C.Types’
    
    12556
    -instance GHC.Internal.Read.Read GHC.Internal.ByteOrder.ByteOrder -- Defined in ‘GHC.Internal.ByteOrder’
    
    12557
    +instance [safe] GHC.Internal.Read.Read GHC.Internal.ByteOrder.ByteOrder -- Defined in ‘GHC.ByteOrder’
    
    12557 12558
     instance forall k (f :: k -> *) (g :: k -> *) (p :: k). (GHC.Internal.Read.Read (f p), GHC.Internal.Read.Read (g p)) => GHC.Internal.Read.Read ((GHC.Internal.Generics.:*:) f g p) -- Defined in ‘GHC.Internal.Generics’
    
    12558 12559
     instance forall k (f :: k -> *) (g :: k -> *) (p :: k). (GHC.Internal.Read.Read (f p), GHC.Internal.Read.Read (g p)) => GHC.Internal.Read.Read ((GHC.Internal.Generics.:+:) f g p) -- Defined in ‘GHC.Internal.Generics’
    
    12559 12560
     instance forall k2 (f :: k2 -> *) k1 (g :: k1 -> k2) (p :: k1). GHC.Internal.Read.Read (f (g p)) => GHC.Internal.Read.Read ((GHC.Internal.Generics.:.:) f g p) -- Defined in ‘GHC.Internal.Generics’
    
    ... ... @@ -12568,16 +12569,16 @@ instance GHC.Internal.Read.Read GHC.Internal.Generics.SourceStrictness -- Define
    12568 12569
     instance GHC.Internal.Read.Read GHC.Internal.Generics.SourceUnpackedness -- Defined in ‘GHC.Internal.Generics’
    
    12569 12570
     instance forall k (p :: k). GHC.Internal.Read.Read (GHC.Internal.Generics.U1 p) -- Defined in ‘GHC.Internal.Generics’
    
    12570 12571
     instance forall k (p :: k). GHC.Internal.Read.Read (GHC.Internal.Generics.V1 p) -- Defined in ‘GHC.Internal.Generics’
    
    12571
    -instance GHC.Internal.Read.Read GHC.Internal.IO.Device.SeekMode -- Defined in ‘GHC.Internal.IO.Device’
    
    12572
    -instance GHC.Internal.Read.Read GHC.Internal.IO.Handle.Types.BufferMode -- Defined in ‘GHC.Internal.IO.Handle.Types’
    
    12573
    -instance GHC.Internal.Read.Read GHC.Internal.IO.Handle.Types.Newline -- Defined in ‘GHC.Internal.IO.Handle.Types’
    
    12574
    -instance GHC.Internal.Read.Read GHC.Internal.IO.Handle.Types.NewlineMode -- Defined in ‘GHC.Internal.IO.Handle.Types’
    
    12575
    -instance GHC.Internal.Read.Read GHC.Internal.IO.IOMode.IOMode -- Defined in ‘GHC.Internal.IO.IOMode’
    
    12576 12572
     instance [safe] GHC.Internal.Read.Read GHC.Stats.GCDetails -- Defined in ‘GHC.Stats’
    
    12577 12573
     instance [safe] GHC.Internal.Read.Read GHC.Stats.RTSStats -- Defined in ‘GHC.Stats’
    
    12578 12574
     instance GHC.Internal.Read.Read GHC.Internal.TypeNats.SomeNat -- Defined in ‘GHC.Internal.TypeNats’
    
    12579 12575
     instance GHC.Internal.Read.Read GHC.Internal.TypeLits.SomeChar -- Defined in ‘GHC.Internal.TypeLits’
    
    12580 12576
     instance GHC.Internal.Read.Read GHC.Internal.TypeLits.SomeSymbol -- Defined in ‘GHC.Internal.TypeLits’
    
    12577
    +instance GHC.Internal.Read.Read GHC.Internal.IO.Handle.Types.BufferMode -- Defined in ‘System.IO’
    
    12578
    +instance GHC.Internal.Read.Read GHC.Internal.IO.IOMode.IOMode -- Defined in ‘System.IO’
    
    12579
    +instance GHC.Internal.Read.Read GHC.Internal.IO.Handle.Types.Newline -- Defined in ‘System.IO’
    
    12580
    +instance GHC.Internal.Read.Read GHC.Internal.IO.Handle.Types.NewlineMode -- Defined in ‘System.IO’
    
    12581
    +instance GHC.Internal.Read.Read GHC.Internal.IO.Device.SeekMode -- Defined in ‘System.IO’
    
    12581 12582
     instance forall a k (b :: k). GHC.Internal.Real.Fractional a => GHC.Internal.Real.Fractional (GHC.Internal.Data.Functor.Const.Const a b) -- Defined in ‘GHC.Internal.Data.Functor.Const’
    
    12582 12583
     instance forall a. GHC.Internal.Float.RealFloat a => GHC.Internal.Real.Fractional (Data.Complex.Complex a) -- Defined in ‘Data.Complex’
    
    12583 12584
     instance forall k (a :: k). Data.Fixed.HasResolution a => GHC.Internal.Real.Fractional (Data.Fixed.Fixed a) -- Defined in ‘Data.Fixed’
    

  • testsuite/tests/interface-stability/base-exports.stdout-mingw32
    ... ... @@ -8012,6 +8012,7 @@ module GHC.IO.Handle where
    8012 8012
       hGetEcho :: Handle -> GHC.Internal.Types.IO GHC.Internal.Types.Bool
    
    8013 8013
       hGetEncoding :: Handle -> GHC.Internal.Types.IO (GHC.Internal.Maybe.Maybe GHC.Internal.IO.Encoding.Types.TextEncoding)
    
    8014 8014
       hGetLine :: Handle -> GHC.Internal.Types.IO GHC.Internal.Base.String
    
    8015
    +  hGetNewlineMode :: Handle -> GHC.Internal.Types.IO NewlineMode
    
    8015 8016
       hGetPosn :: Handle -> GHC.Internal.Types.IO HandlePosn
    
    8016 8017
       hIsClosed :: Handle -> GHC.Internal.Types.IO GHC.Internal.Types.Bool
    
    8017 8018
       hIsEOF :: Handle -> GHC.Internal.Types.IO GHC.Internal.Types.Bool
    
    ... ... @@ -9732,7 +9733,7 @@ module GHC.Word where
    9732 9733
       uncheckedShiftRL64# :: GHC.Internal.Prim.Word64# -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.Word64#
    
    9733 9734
     
    
    9734 9735
     module Numeric where
    
    9735
    -  -- Safety: Safe
    
    9736
    +  -- Safety: Trustworthy
    
    9736 9737
       type Floating :: * -> Constraint
    
    9737 9738
       class GHC.Internal.Real.Fractional a => Floating a where
    
    9738 9739
         pi :: a
    
    ... ... @@ -10163,7 +10164,7 @@ module System.Exit where
    10163 10164
       exitWith :: forall a. ExitCode -> GHC.Internal.Types.IO a
    
    10164 10165
     
    
    10165 10166
     module System.IO where
    
    10166
    -  -- Safety: Safe
    
    10167
    +  -- Safety: Trustworthy
    
    10167 10168
       type BufferMode :: *
    
    10168 10169
       data BufferMode = NoBuffering | LineBuffering | BlockBuffering (GHC.Internal.Maybe.Maybe GHC.Internal.Types.Int)
    
    10169 10170
       type FilePath :: *
    
    ... ... @@ -12766,7 +12767,7 @@ instance forall a. GHC.Internal.Read.Read a => GHC.Internal.Read.Read (Data.Semi
    12766 12767
     instance forall a. GHC.Internal.Read.Read a => GHC.Internal.Read.Read (Data.Semigroup.Min a) -- Defined in ‘Data.Semigroup’
    
    12767 12768
     instance forall m. GHC.Internal.Read.Read m => GHC.Internal.Read.Read (Data.Semigroup.WrappedMonoid m) -- Defined in ‘Data.Semigroup’
    
    12768 12769
     instance forall k (a :: k) (b :: k). Coercible a b => GHC.Internal.Read.Read (GHC.Internal.Data.Type.Coercion.Coercion a b) -- Defined in ‘GHC.Internal.Data.Type.Coercion’
    
    12769
    -instance GHC.Internal.Read.Read GHC.Internal.Data.Version.Version -- Defined in ‘GHC.Internal.Data.Version’
    
    12770
    +instance [safe] GHC.Internal.Read.Read GHC.Internal.Data.Version.Version -- Defined in ‘Data.Version’
    
    12770 12771
     instance GHC.Internal.Read.Read GHC.Internal.Foreign.Ptr.IntPtr -- Defined in ‘GHC.Internal.Foreign.Ptr’
    
    12771 12772
     instance GHC.Internal.Read.Read GHC.Internal.Foreign.Ptr.WordPtr -- Defined in ‘GHC.Internal.Foreign.Ptr’
    
    12772 12773
     instance GHC.Internal.Read.Read GHC.Internal.Foreign.C.Types.CBool -- Defined in ‘GHC.Internal.Foreign.C.Types’
    
    ... ... @@ -12795,7 +12796,7 @@ instance GHC.Internal.Read.Read GHC.Internal.Foreign.C.Types.CULong -- Defined i
    12795 12796
     instance GHC.Internal.Read.Read GHC.Internal.Foreign.C.Types.CUSeconds -- Defined in ‘GHC.Internal.Foreign.C.Types’
    
    12796 12797
     instance GHC.Internal.Read.Read GHC.Internal.Foreign.C.Types.CUShort -- Defined in ‘GHC.Internal.Foreign.C.Types’
    
    12797 12798
     instance GHC.Internal.Read.Read GHC.Internal.Foreign.C.Types.CWchar -- Defined in ‘GHC.Internal.Foreign.C.Types’
    
    12798
    -instance GHC.Internal.Read.Read GHC.Internal.ByteOrder.ByteOrder -- Defined in ‘GHC.Internal.ByteOrder’
    
    12799
    +instance [safe] GHC.Internal.Read.Read GHC.Internal.ByteOrder.ByteOrder -- Defined in ‘GHC.ByteOrder’
    
    12799 12800
     instance GHC.Internal.Read.Read GHC.Internal.Event.Windows.ConsoleEvent.ConsoleEvent -- Defined in ‘GHC.Internal.Event.Windows.ConsoleEvent’
    
    12800 12801
     instance forall k (f :: k -> *) (g :: k -> *) (p :: k). (GHC.Internal.Read.Read (f p), GHC.Internal.Read.Read (g p)) => GHC.Internal.Read.Read ((GHC.Internal.Generics.:*:) f g p) -- Defined in ‘GHC.Internal.Generics’
    
    12801 12802
     instance forall k (f :: k -> *) (g :: k -> *) (p :: k). (GHC.Internal.Read.Read (f p), GHC.Internal.Read.Read (g p)) => GHC.Internal.Read.Read ((GHC.Internal.Generics.:+:) f g p) -- Defined in ‘GHC.Internal.Generics’
    
    ... ... @@ -12811,16 +12812,16 @@ instance GHC.Internal.Read.Read GHC.Internal.Generics.SourceStrictness -- Define
    12811 12812
     instance GHC.Internal.Read.Read GHC.Internal.Generics.SourceUnpackedness -- Defined in ‘GHC.Internal.Generics’
    
    12812 12813
     instance forall k (p :: k). GHC.Internal.Read.Read (GHC.Internal.Generics.U1 p) -- Defined in ‘GHC.Internal.Generics’
    
    12813 12814
     instance forall k (p :: k). GHC.Internal.Read.Read (GHC.Internal.Generics.V1 p) -- Defined in ‘GHC.Internal.Generics’
    
    12814
    -instance GHC.Internal.Read.Read GHC.Internal.IO.Device.SeekMode -- Defined in ‘GHC.Internal.IO.Device’
    
    12815
    -instance GHC.Internal.Read.Read GHC.Internal.IO.Handle.Types.BufferMode -- Defined in ‘GHC.Internal.IO.Handle.Types’
    
    12816
    -instance GHC.Internal.Read.Read GHC.Internal.IO.Handle.Types.Newline -- Defined in ‘GHC.Internal.IO.Handle.Types’
    
    12817
    -instance GHC.Internal.Read.Read GHC.Internal.IO.Handle.Types.NewlineMode -- Defined in ‘GHC.Internal.IO.Handle.Types’
    
    12818
    -instance GHC.Internal.Read.Read GHC.Internal.IO.IOMode.IOMode -- Defined in ‘GHC.Internal.IO.IOMode’
    
    12819 12815
     instance [safe] GHC.Internal.Read.Read GHC.Stats.GCDetails -- Defined in ‘GHC.Stats’
    
    12820 12816
     instance [safe] GHC.Internal.Read.Read GHC.Stats.RTSStats -- Defined in ‘GHC.Stats’
    
    12821 12817
     instance GHC.Internal.Read.Read GHC.Internal.TypeNats.SomeNat -- Defined in ‘GHC.Internal.TypeNats’
    
    12822 12818
     instance GHC.Internal.Read.Read GHC.Internal.TypeLits.SomeChar -- Defined in ‘GHC.Internal.TypeLits’
    
    12823 12819
     instance GHC.Internal.Read.Read GHC.Internal.TypeLits.SomeSymbol -- Defined in ‘GHC.Internal.TypeLits’
    
    12820
    +instance GHC.Internal.Read.Read GHC.Internal.IO.Handle.Types.BufferMode -- Defined in ‘System.IO’
    
    12821
    +instance GHC.Internal.Read.Read GHC.Internal.IO.IOMode.IOMode -- Defined in ‘System.IO’
    
    12822
    +instance GHC.Internal.Read.Read GHC.Internal.IO.Handle.Types.Newline -- Defined in ‘System.IO’
    
    12823
    +instance GHC.Internal.Read.Read GHC.Internal.IO.Handle.Types.NewlineMode -- Defined in ‘System.IO’
    
    12824
    +instance GHC.Internal.Read.Read GHC.Internal.IO.Device.SeekMode -- Defined in ‘System.IO’
    
    12824 12825
     instance forall a k (b :: k). GHC.Internal.Real.Fractional a => GHC.Internal.Real.Fractional (GHC.Internal.Data.Functor.Const.Const a b) -- Defined in ‘GHC.Internal.Data.Functor.Const’
    
    12825 12826
     instance forall a. GHC.Internal.Float.RealFloat a => GHC.Internal.Real.Fractional (Data.Complex.Complex a) -- Defined in ‘Data.Complex’
    
    12826 12827
     instance forall k (a :: k). Data.Fixed.HasResolution a => GHC.Internal.Real.Fractional (Data.Fixed.Fixed a) -- Defined in ‘Data.Fixed’
    

  • testsuite/tests/interface-stability/base-exports.stdout-ws-32
    ... ... @@ -7848,6 +7848,7 @@ module GHC.IO.Handle where
    7848 7848
       hGetEcho :: Handle -> GHC.Internal.Types.IO GHC.Internal.Types.Bool
    
    7849 7849
       hGetEncoding :: Handle -> GHC.Internal.Types.IO (GHC.Internal.Maybe.Maybe GHC.Internal.IO.Encoding.Types.TextEncoding)
    
    7850 7850
       hGetLine :: Handle -> GHC.Internal.Types.IO GHC.Internal.Base.String
    
    7851
    +  hGetNewlineMode :: Handle -> GHC.Internal.Types.IO NewlineMode
    
    7851 7852
       hGetPosn :: Handle -> GHC.Internal.Types.IO HandlePosn
    
    7852 7853
       hIsClosed :: Handle -> GHC.Internal.Types.IO GHC.Internal.Types.Bool
    
    7853 7854
       hIsEOF :: Handle -> GHC.Internal.Types.IO GHC.Internal.Types.Bool
    
    ... ... @@ -9452,7 +9453,7 @@ module GHC.Word where
    9452 9453
       uncheckedShiftRL64# :: GHC.Internal.Prim.Word64# -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.Word64#
    
    9453 9454
     
    
    9454 9455
     module Numeric where
    
    9455
    -  -- Safety: Safe
    
    9456
    +  -- Safety: Trustworthy
    
    9456 9457
       type Floating :: * -> Constraint
    
    9457 9458
       class GHC.Internal.Real.Fractional a => Floating a where
    
    9458 9459
         pi :: a
    
    ... ... @@ -9883,7 +9884,7 @@ module System.Exit where
    9883 9884
       exitWith :: forall a. ExitCode -> GHC.Internal.Types.IO a
    
    9884 9885
     
    
    9885 9886
     module System.IO where
    
    9886
    -  -- Safety: Safe
    
    9887
    +  -- Safety: Trustworthy
    
    9887 9888
       type BufferMode :: *
    
    9888 9889
       data BufferMode = NoBuffering | LineBuffering | BlockBuffering (GHC.Internal.Maybe.Maybe GHC.Internal.Types.Int)
    
    9889 9890
       type FilePath :: *
    
    ... ... @@ -12495,7 +12496,7 @@ instance forall a. GHC.Internal.Read.Read a => GHC.Internal.Read.Read (Data.Semi
    12495 12496
     instance forall a. GHC.Internal.Read.Read a => GHC.Internal.Read.Read (Data.Semigroup.Min a) -- Defined in ‘Data.Semigroup’
    
    12496 12497
     instance forall m. GHC.Internal.Read.Read m => GHC.Internal.Read.Read (Data.Semigroup.WrappedMonoid m) -- Defined in ‘Data.Semigroup’
    
    12497 12498
     instance forall k (a :: k) (b :: k). Coercible a b => GHC.Internal.Read.Read (GHC.Internal.Data.Type.Coercion.Coercion a b) -- Defined in ‘GHC.Internal.Data.Type.Coercion’
    
    12498
    -instance GHC.Internal.Read.Read GHC.Internal.Data.Version.Version -- Defined in ‘GHC.Internal.Data.Version’
    
    12499
    +instance [safe] GHC.Internal.Read.Read GHC.Internal.Data.Version.Version -- Defined in ‘Data.Version’
    
    12499 12500
     instance GHC.Internal.Read.Read GHC.Internal.Foreign.Ptr.IntPtr -- Defined in ‘GHC.Internal.Foreign.Ptr’
    
    12500 12501
     instance GHC.Internal.Read.Read GHC.Internal.Foreign.Ptr.WordPtr -- Defined in ‘GHC.Internal.Foreign.Ptr’
    
    12501 12502
     instance GHC.Internal.Read.Read GHC.Internal.Foreign.C.Types.CBool -- Defined in ‘GHC.Internal.Foreign.C.Types’
    
    ... ... @@ -12524,7 +12525,7 @@ instance GHC.Internal.Read.Read GHC.Internal.Foreign.C.Types.CULong -- Defined i
    12524 12525
     instance GHC.Internal.Read.Read GHC.Internal.Foreign.C.Types.CUSeconds -- Defined in ‘GHC.Internal.Foreign.C.Types’
    
    12525 12526
     instance GHC.Internal.Read.Read GHC.Internal.Foreign.C.Types.CUShort -- Defined in ‘GHC.Internal.Foreign.C.Types’
    
    12526 12527
     instance GHC.Internal.Read.Read GHC.Internal.Foreign.C.Types.CWchar -- Defined in ‘GHC.Internal.Foreign.C.Types’
    
    12527
    -instance GHC.Internal.Read.Read GHC.Internal.ByteOrder.ByteOrder -- Defined in ‘GHC.Internal.ByteOrder’
    
    12528
    +instance [safe] GHC.Internal.Read.Read GHC.Internal.ByteOrder.ByteOrder -- Defined in ‘GHC.ByteOrder’
    
    12528 12529
     instance forall k (f :: k -> *) (g :: k -> *) (p :: k). (GHC.Internal.Read.Read (f p), GHC.Internal.Read.Read (g p)) => GHC.Internal.Read.Read ((GHC.Internal.Generics.:*:) f g p) -- Defined in ‘GHC.Internal.Generics’
    
    12529 12530
     instance forall k (f :: k -> *) (g :: k -> *) (p :: k). (GHC.Internal.Read.Read (f p), GHC.Internal.Read.Read (g p)) => GHC.Internal.Read.Read ((GHC.Internal.Generics.:+:) f g p) -- Defined in ‘GHC.Internal.Generics’
    
    12530 12531
     instance forall k2 (f :: k2 -> *) k1 (g :: k1 -> k2) (p :: k1). GHC.Internal.Read.Read (f (g p)) => GHC.Internal.Read.Read ((GHC.Internal.Generics.:.:) f g p) -- Defined in ‘GHC.Internal.Generics’
    
    ... ... @@ -12539,16 +12540,16 @@ instance GHC.Internal.Read.Read GHC.Internal.Generics.SourceStrictness -- Define
    12539 12540
     instance GHC.Internal.Read.Read GHC.Internal.Generics.SourceUnpackedness -- Defined in ‘GHC.Internal.Generics’
    
    12540 12541
     instance forall k (p :: k). GHC.Internal.Read.Read (GHC.Internal.Generics.U1 p) -- Defined in ‘GHC.Internal.Generics’
    
    12541 12542
     instance forall k (p :: k). GHC.Internal.Read.Read (GHC.Internal.Generics.V1 p) -- Defined in ‘GHC.Internal.Generics’
    
    12542
    -instance GHC.Internal.Read.Read GHC.Internal.IO.Device.SeekMode -- Defined in ‘GHC.Internal.IO.Device’
    
    12543
    -instance GHC.Internal.Read.Read GHC.Internal.IO.Handle.Types.BufferMode -- Defined in ‘GHC.Internal.IO.Handle.Types’
    
    12544
    -instance GHC.Internal.Read.Read GHC.Internal.IO.Handle.Types.Newline -- Defined in ‘GHC.Internal.IO.Handle.Types’
    
    12545
    -instance GHC.Internal.Read.Read GHC.Internal.IO.Handle.Types.NewlineMode -- Defined in ‘GHC.Internal.IO.Handle.Types’
    
    12546
    -instance GHC.Internal.Read.Read GHC.Internal.IO.IOMode.IOMode -- Defined in ‘GHC.Internal.IO.IOMode’
    
    12547 12543
     instance [safe] GHC.Internal.Read.Read GHC.Stats.GCDetails -- Defined in ‘GHC.Stats’
    
    12548 12544
     instance [safe] GHC.Internal.Read.Read GHC.Stats.RTSStats -- Defined in ‘GHC.Stats’
    
    12549 12545
     instance GHC.Internal.Read.Read GHC.Internal.TypeNats.SomeNat -- Defined in ‘GHC.Internal.TypeNats’
    
    12550 12546
     instance GHC.Internal.Read.Read GHC.Internal.TypeLits.SomeChar -- Defined in ‘GHC.Internal.TypeLits’
    
    12551 12547
     instance GHC.Internal.Read.Read GHC.Internal.TypeLits.SomeSymbol -- Defined in ‘GHC.Internal.TypeLits’
    
    12548
    +instance GHC.Internal.Read.Read GHC.Internal.IO.Handle.Types.BufferMode -- Defined in ‘System.IO’
    
    12549
    +instance GHC.Internal.Read.Read GHC.Internal.IO.IOMode.IOMode -- Defined in ‘System.IO’
    
    12550
    +instance GHC.Internal.Read.Read GHC.Internal.IO.Handle.Types.Newline -- Defined in ‘System.IO’
    
    12551
    +instance GHC.Internal.Read.Read GHC.Internal.IO.Handle.Types.NewlineMode -- Defined in ‘System.IO’
    
    12552
    +instance GHC.Internal.Read.Read GHC.Internal.IO.Device.SeekMode -- Defined in ‘System.IO’
    
    12552 12553
     instance forall a k (b :: k). GHC.Internal.Real.Fractional a => GHC.Internal.Real.Fractional (GHC.Internal.Data.Functor.Const.Const a b) -- Defined in ‘GHC.Internal.Data.Functor.Const’
    
    12553 12554
     instance forall a. GHC.Internal.Float.RealFloat a => GHC.Internal.Real.Fractional (Data.Complex.Complex a) -- Defined in ‘Data.Complex’
    
    12554 12555
     instance forall k (a :: k). Data.Fixed.HasResolution a => GHC.Internal.Real.Fractional (Data.Fixed.Fixed a) -- Defined in ‘Data.Fixed’