1 {-# OPTIONS_GHC -XNoImplicitPrelude -XBangPatterns #-}
2 {-# OPTIONS_HADDOCK hide #-}
3 -----------------------------------------------------------------------------
5 -- Module : GHC.IO.Device
6 -- Copyright : (c) The University of Glasgow, 1994-2008
7 -- License : see libraries/base/LICENSE
9 -- Maintainer : libraries@haskell.org
10 -- Stability : internal
11 -- Portability : non-portable
13 -- Type classes for I/O providers.
15 -----------------------------------------------------------------------------
17 module GHC.IO.Device (
34 import {-# SOURCE #-} GHC.IO.Exception ( unsupportedOperation )
36 -- | A low-level I/O provider where the data is bytes in memory.
38 -- | Read up to the specified number of bytes, returning the number
39 -- of bytes actually read. This function should only block if there
40 -- is no data available. If there is not enough data available,
41 -- then the function should just return the available data. A return
42 -- value of zero indicates that the end of the data stream (e.g. end
43 -- of file) has been reached.
44 read :: a -> Ptr Word8 -> Int -> IO Int
46 -- | Read up to the specified number of bytes, returning the number
47 -- of bytes actually read, or 'Nothing' if the end of the stream has
49 readNonBlocking :: a -> Ptr Word8 -> Int -> IO (Maybe Int)
51 -- | Write the specified number of bytes.
52 write :: a -> Ptr Word8 -> Int -> IO ()
54 -- | Write up to the specified number of bytes without blocking. Returns
55 -- the actual number of bytes written.
56 writeNonBlocking :: a -> Ptr Word8 -> Int -> IO Int
59 -- | I/O operations required for implementing a 'Handle'.
60 class IODevice a where
61 -- | @ready dev write msecs@ returns 'True' if the device has data
62 -- to read (if @write@ is 'False') or space to write new data (if
63 -- @write@ is 'True'). @msecs@ specifies how long to wait, in
66 ready :: a -> Bool -> Int -> IO Bool
68 -- | closes the device. Further operations on the device should
69 -- produce exceptions.
72 -- | returns 'True' if the device is a terminal or console.
73 isTerminal :: a -> IO Bool
74 isTerminal _ = return False
76 -- | returns 'True' if the device supports 'seek' operations.
77 isSeekable :: a -> IO Bool
78 isSeekable _ = return False
80 -- | seek to the specified positing in the data.
81 seek :: a -> SeekMode -> Integer -> IO ()
82 seek _ _ _ = ioe_unsupportedOperation
84 -- | return the current position in the data.
85 tell :: a -> IO Integer
86 tell _ = ioe_unsupportedOperation
88 -- | return the size of the data.
89 getSize :: a -> IO Integer
90 getSize _ = ioe_unsupportedOperation
92 -- | change the size of the data.
93 setSize :: a -> Integer -> IO ()
94 setSize _ _ = ioe_unsupportedOperation
96 -- | for terminal devices, changes whether characters are echoed on
98 setEcho :: a -> Bool -> IO ()
99 setEcho _ _ = ioe_unsupportedOperation
101 -- | returns the current echoing status.
102 getEcho :: a -> IO Bool
103 getEcho _ = ioe_unsupportedOperation
105 -- | some devices (e.g. terminals) support a "raw" mode where
106 -- characters entered are immediately made available to the program.
107 -- If available, this operations enables raw mode.
108 setRaw :: a -> Bool -> IO ()
109 setRaw _ _ = ioe_unsupportedOperation
111 -- | returns the 'IODeviceType' corresponding to this device.
112 devType :: a -> IO IODeviceType
114 -- | duplicates the device, if possible. The new device is expected
115 -- to share a file pointer with the original device (like Unix @dup@).
117 dup _ = ioe_unsupportedOperation
119 -- | @dup2 source target@ replaces the target device with the source
120 -- device. The target device is closed first, if necessary, and then
121 -- it is made into a duplicate of the first device (like Unix @dup2@).
122 dup2 :: a -> a -> IO a
123 dup2 _ _ = ioe_unsupportedOperation
125 ioe_unsupportedOperation :: IO a
126 ioe_unsupportedOperation = throwIO unsupportedOperation
135 -- -----------------------------------------------------------------------------
138 -- | A mode that determines the effect of 'hSeek' @hdl mode i@, as follows:
140 = AbsoluteSeek -- ^ the position of @hdl@ is set to @i@.
141 | RelativeSeek -- ^ the position of @hdl@ is set to offset @i@
142 -- from the current position.
143 | SeekFromEnd -- ^ the position of @hdl@ is set to offset @i@
144 -- from the end of the file.
145 deriving (Eq, Ord, Ix, Enum, Read, Show)