{-# LANGUAGE Trustworthy       #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE CPP               #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  GHC.Internal.IO.StdHandles
-- Copyright   :  (c) The University of Glasgow, 2017
-- License     :  see libraries/base/LICENSE
--
-- Maintainer  :  libraries@haskell.org
-- Stability   :  internal
-- Portability :  non-portable
--
-- This model abstracts away the platform specific handles that can be toggled
-- through the RTS.
--
-----------------------------------------------------------------------------

module GHC.Internal.IO.StdHandles
  ( -- std handles
    stdin, stdout, stderr,
    openFile, openBinaryFile, openFileBlocking,
    withFile, withBinaryFile, withFileBlocking
  ) where

import GHC.Internal.IO
import GHC.Internal.IO.IOMode
import GHC.Internal.IO.Handle.Types

import qualified GHC.Internal.IO.Handle.FD as POSIX
#if defined(mingw32_HOST_OS)
import GHC.Internal.IO.SubSystem
import qualified GHC.Internal.IO.Handle.Windows as Win
import GHC.Internal.IO.Handle.Internals (hClose_impl)

stdin :: Handle
stdin :: Handle
stdin = Handle
POSIX.stdin Handle -> Handle -> Handle
forall a. a -> a -> a
<!> Handle
Win.stdin

stdout :: Handle
stdout :: Handle
stdout = Handle
POSIX.stdout Handle -> Handle -> Handle
forall a. a -> a -> a
<!> Handle
Win.stdout

stderr :: Handle
stderr :: Handle
stderr = Handle
POSIX.stderr Handle -> Handle -> Handle
forall a. a -> a -> a
<!> Handle
Win.stderr

openFile :: FilePath -> IOMode -> IO Handle
openFile :: FilePath -> IOMode -> IO Handle
openFile = FilePath -> IOMode -> IO Handle
POSIX.openFile (FilePath -> IOMode -> IO Handle)
-> (FilePath -> IOMode -> IO Handle)
-> FilePath
-> IOMode
-> IO Handle
forall a. a -> a -> a
<!> FilePath -> IOMode -> IO Handle
Win.openFile

-- TODO: implement as for POSIX
withFile :: FilePath -> IOMode -> (Handle -> IO r) -> IO r
withFile :: forall r. FilePath -> IOMode -> (Handle -> IO r) -> IO r
withFile = FilePath -> IOMode -> (Handle -> IO r) -> IO r
forall r. FilePath -> IOMode -> (Handle -> IO r) -> IO r
POSIX.withFile (FilePath -> IOMode -> (Handle -> IO r) -> IO r)
-> (FilePath -> IOMode -> (Handle -> IO r) -> IO r)
-> FilePath
-> IOMode
-> (Handle -> IO r)
-> IO r
forall a. a -> a -> a
<!> FilePath -> IOMode -> (Handle -> IO r) -> IO r
forall r. FilePath -> IOMode -> (Handle -> IO r) -> IO r
wf
  where
    wf :: FilePath -> IOMode -> (Handle -> IO c) -> IO c
wf FilePath
path IOMode
mode Handle -> IO c
act = IO Handle -> (Handle -> IO ()) -> (Handle -> IO c) -> IO c
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket (FilePath -> IOMode -> IO Handle
Win.openFile FilePath
path IOMode
mode) Handle -> IO ()
hClose_impl Handle -> IO c
act

openBinaryFile :: FilePath -> IOMode -> IO Handle
openBinaryFile :: FilePath -> IOMode -> IO Handle
openBinaryFile = FilePath -> IOMode -> IO Handle
POSIX.openBinaryFile (FilePath -> IOMode -> IO Handle)
-> (FilePath -> IOMode -> IO Handle)
-> FilePath
-> IOMode
-> IO Handle
forall a. a -> a -> a
<!> FilePath -> IOMode -> IO Handle
Win.openBinaryFile

withBinaryFile :: FilePath -> IOMode -> (Handle -> IO r) -> IO r
withBinaryFile :: forall r. FilePath -> IOMode -> (Handle -> IO r) -> IO r
withBinaryFile = FilePath -> IOMode -> (Handle -> IO r) -> IO r
forall r. FilePath -> IOMode -> (Handle -> IO r) -> IO r
POSIX.withBinaryFile (FilePath -> IOMode -> (Handle -> IO r) -> IO r)
-> (FilePath -> IOMode -> (Handle -> IO r) -> IO r)
-> FilePath
-> IOMode
-> (Handle -> IO r)
-> IO r
forall a. a -> a -> a
<!> FilePath -> IOMode -> (Handle -> IO r) -> IO r
forall r. FilePath -> IOMode -> (Handle -> IO r) -> IO r
wf
  where
    wf :: FilePath -> IOMode -> (Handle -> IO c) -> IO c
wf FilePath
path IOMode
mode Handle -> IO c
act = IO Handle -> (Handle -> IO ()) -> (Handle -> IO c) -> IO c
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket (FilePath -> IOMode -> IO Handle
Win.openBinaryFile FilePath
path IOMode
mode) Handle -> IO ()
hClose_impl Handle -> IO c
act

openFileBlocking :: FilePath -> IOMode -> IO Handle
openFileBlocking :: FilePath -> IOMode -> IO Handle
openFileBlocking = FilePath -> IOMode -> IO Handle
POSIX.openFileBlocking (FilePath -> IOMode -> IO Handle)
-> (FilePath -> IOMode -> IO Handle)
-> FilePath
-> IOMode
-> IO Handle
forall a. a -> a -> a
<!> FilePath -> IOMode -> IO Handle
Win.openFileBlocking

withFileBlocking :: FilePath -> IOMode -> (Handle -> IO r) -> IO r
withFileBlocking :: forall r. FilePath -> IOMode -> (Handle -> IO r) -> IO r
withFileBlocking = FilePath -> IOMode -> (Handle -> IO r) -> IO r
forall r. FilePath -> IOMode -> (Handle -> IO r) -> IO r
POSIX.withFileBlocking (FilePath -> IOMode -> (Handle -> IO r) -> IO r)
-> (FilePath -> IOMode -> (Handle -> IO r) -> IO r)
-> FilePath
-> IOMode
-> (Handle -> IO r)
-> IO r
forall a. a -> a -> a
<!> FilePath -> IOMode -> (Handle -> IO r) -> IO r
forall r. FilePath -> IOMode -> (Handle -> IO r) -> IO r
wf
  where
    wf :: FilePath -> IOMode -> (Handle -> IO c) -> IO c
wf FilePath
path IOMode
mode Handle -> IO c
act = IO Handle -> (Handle -> IO ()) -> (Handle -> IO c) -> IO c
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket (FilePath -> IOMode -> IO Handle
Win.openFileBlocking FilePath
path IOMode
mode) Handle -> IO ()
hClose_impl Handle -> IO c
act

#else

stdin :: Handle
stdin = POSIX.stdin

stdout :: Handle
stdout = POSIX.stdout

stderr :: Handle
stderr = POSIX.stderr

openFile :: FilePath -> IOMode -> IO Handle
openFile = POSIX.openFile

withFile :: FilePath -> IOMode -> (Handle -> IO r) -> IO r
withFile = POSIX.withFile

openBinaryFile :: FilePath -> IOMode -> IO Handle
openBinaryFile = POSIX.openBinaryFile

withBinaryFile :: FilePath -> IOMode -> (Handle -> IO r) -> IO r
withBinaryFile = POSIX.withBinaryFile

openFileBlocking :: FilePath -> IOMode -> IO Handle
openFileBlocking = POSIX.openFileBlocking

withFileBlocking :: FilePath -> IOMode -> (Handle -> IO r) -> IO r
withFileBlocking = POSIX.withFileBlocking

#endif