3 , ExistentialQuantification
6 {-# OPTIONS_GHC -funbox-strict-fields #-}
7 {-# OPTIONS_HADDOCK hide #-}
9 -----------------------------------------------------------------------------
11 -- Module : GHC.IO.Handle.Types
12 -- Copyright : (c) The University of Glasgow, 1994-2009
13 -- License : see libraries/base/LICENSE
15 -- Maintainer : libraries@haskell.org
16 -- Stability : internal
17 -- Portability : non-portable
19 -- Basic types for the implementation of IO Handles.
21 -----------------------------------------------------------------------------
23 module GHC.IO.Handle.Types (
24 Handle(..), Handle__(..), showHandle,
25 checkHandleInvariants,
28 isReadableHandleType, isWritableHandleType, isReadWriteHandleType,
31 NewlineMode(..), Newline(..), nativeNewline,
32 universalNewlineMode, noNewlineTranslation, nativeNewlineMode
41 import GHC.IO.BufferedIO
42 import GHC.IO.Encoding.Types
54 -- ---------------------------------------------------------------------------
57 -- A Handle is represented by (a reference to) a record
58 -- containing the state of the I/O port/device. We record
59 -- the following pieces of info:
61 -- * type (read,write,closed etc.)
62 -- * the underlying file descriptor
64 -- * buffer, and spare buffers
65 -- * user-friendly name (usually the
66 -- FilePath used when IO.openFile was called)
68 -- Note: when a Handle is garbage collected, we want to flush its buffer
69 -- and close the OS file handle, so as to free up a (precious) resource.
71 -- | Haskell defines operations to read and write characters from and to files,
72 -- represented by values of type @Handle@. Each value of this type is a
73 -- /handle/: a record used by the Haskell run-time system to /manage/ I\/O
74 -- with file system objects. A handle has at least the following properties:
76 -- * whether it manages input or output or both;
78 -- * whether it is /open/, /closed/ or /semi-closed/;
80 -- * whether the object is seekable;
82 -- * whether buffering is disabled, or enabled on a line or block basis;
84 -- * a buffer (whose length may be zero).
86 -- Most handles will also have a current I\/O position indicating where the next
87 -- input or output operation will occur. A handle is /readable/ if it
88 -- manages only input or both input and output; likewise, it is /writable/ if
89 -- it manages only output or both input and output. A handle is /open/ when
91 -- Once it is closed it can no longer be used for either input or output,
92 -- though an implementation cannot re-use its storage while references
93 -- remain to it. Handles are in the 'Show' and 'Eq' classes. The string
94 -- produced by showing a handle is system dependent; it should include
95 -- enough information to identify the handle for debugging. A handle is
96 -- equal according to '==' only to itself; no attempt
97 -- is made to compare the internal state of different handles for equality.
100 = FileHandle -- A normal handle to a file
101 FilePath -- the file (used for error messages
105 | DuplexHandle -- A handle to a read/write stream
106 FilePath -- file for a FIFO, otherwise some
107 -- descriptive string (used for error
109 !(MVar Handle__) -- The read side
110 !(MVar Handle__) -- The write side
115 -- * A 'FileHandle' is seekable. A 'DuplexHandle' may or may not be
118 instance Eq Handle where
119 (FileHandle _ h1) == (FileHandle _ h2) = h1 == h2
120 (DuplexHandle _ h1 _) == (DuplexHandle _ h2 _) = h1 == h2
124 = forall dev enc_state dec_state . (IODevice dev, BufferedIO dev, Typeable dev) =>
127 haType :: HandleType, -- type (read/write/append etc.)
128 haByteBuffer :: !(IORef (Buffer Word8)),
129 haBufferMode :: BufferMode,
130 haLastDecode :: !(IORef (dec_state, Buffer Word8)),
131 haCharBuffer :: !(IORef (Buffer CharBufElem)), -- the current buffer
132 haBuffers :: !(IORef (BufferList CharBufElem)), -- spare buffers
133 haEncoder :: Maybe (TextEncoder enc_state),
134 haDecoder :: Maybe (TextDecoder dec_state),
135 haCodec :: Maybe TextEncoding,
136 haInputNL :: Newline,
137 haOutputNL :: Newline,
138 haOtherSide :: Maybe (MVar Handle__) -- ptr to the write side of a
143 -- we keep a few spare buffers around in a handle to avoid allocating
144 -- a new one for each hPutStr. These buffers are *guaranteed* to be the
145 -- same size as the main buffer.
148 | BufferListCons (RawBuffer e) (BufferList e)
150 -- Internally, we classify handles as being one
161 isReadableHandleType :: HandleType -> Bool
162 isReadableHandleType ReadHandle = True
163 isReadableHandleType ReadWriteHandle = True
164 isReadableHandleType _ = False
166 isWritableHandleType :: HandleType -> Bool
167 isWritableHandleType AppendHandle = True
168 isWritableHandleType WriteHandle = True
169 isWritableHandleType ReadWriteHandle = True
170 isWritableHandleType _ = False
172 isReadWriteHandleType :: HandleType -> Bool
173 isReadWriteHandleType ReadWriteHandle{} = True
174 isReadWriteHandleType _ = False
176 -- INVARIANTS on Handles:
178 -- * A handle *always* has a buffer, even if it is only 1 character long
179 -- (an unbuffered handle needs a 1 character buffer in order to support
180 -- hLookAhead and hIsEOF).
181 -- * In a read Handle, the byte buffer is always empty (we decode when reading)
182 -- * In a wriite Handle, the Char buffer is always empty (we encode when writing)
184 checkHandleInvariants :: Handle__ -> IO ()
186 checkHandleInvariants h_ = do
187 bbuf <- readIORef (haByteBuffer h_)
189 cbuf <- readIORef (haCharBuffer h_)
191 when (isWriteBuffer cbuf && not (isEmptyBuffer cbuf)) $
192 error ("checkHandleInvariants: char write buffer non-empty: " ++
193 summaryBuffer bbuf ++ ", " ++ summaryBuffer cbuf)
194 when (isWriteBuffer bbuf /= isWriteBuffer cbuf) $
195 error ("checkHandleInvariants: buffer modes differ: " ++
196 summaryBuffer bbuf ++ ", " ++ summaryBuffer cbuf)
199 checkHandleInvariants _ = return ()
202 -- ---------------------------------------------------------------------------
205 -- | Three kinds of buffering are supported: line-buffering,
206 -- block-buffering or no-buffering. These modes have the following
207 -- effects. For output, items are written out, or /flushed/,
208 -- from the internal buffer according to the buffer mode:
210 -- * /line-buffering/: the entire output buffer is flushed
211 -- whenever a newline is output, the buffer overflows,
212 -- a 'System.IO.hFlush' is issued, or the handle is closed.
214 -- * /block-buffering/: the entire buffer is written out whenever it
215 -- overflows, a 'System.IO.hFlush' is issued, or the handle is closed.
217 -- * /no-buffering/: output is written immediately, and never stored
220 -- An implementation is free to flush the buffer more frequently,
221 -- but not less frequently, than specified above.
222 -- The output buffer is emptied as soon as it has been written out.
224 -- Similarly, input occurs according to the buffer mode for the handle:
226 -- * /line-buffering/: when the buffer for the handle is not empty,
227 -- the next item is obtained from the buffer; otherwise, when the
228 -- buffer is empty, characters up to and including the next newline
229 -- character are read into the buffer. No characters are available
230 -- until the newline character is available or the buffer is full.
232 -- * /block-buffering/: when the buffer for the handle becomes empty,
233 -- the next block of data is read into the buffer.
235 -- * /no-buffering/: the next input item is read and returned.
236 -- The 'System.IO.hLookAhead' operation implies that even a no-buffered
237 -- handle may require a one-character buffer.
239 -- The default buffering mode when a handle is opened is
240 -- implementation-dependent and may depend on the file system object
241 -- which is attached to that handle.
242 -- For most implementations, physical files will normally be block-buffered
243 -- and terminals will normally be line-buffered.
246 = NoBuffering -- ^ buffering is disabled if possible.
248 -- ^ line-buffering should be enabled if possible.
249 | BlockBuffering (Maybe Int)
250 -- ^ block-buffering should be enabled if possible.
251 -- The size of the buffer is @n@ items if the argument
252 -- is 'Just' @n@ and is otherwise implementation-dependent.
253 deriving (Eq, Ord, Read, Show)
256 [note Buffering Implementation]
258 Each Handle has two buffers: a byte buffer (haByteBuffer) and a Char
259 buffer (haCharBuffer).
261 [note Buffered Reading]
263 For read Handles, bytes are read into the byte buffer, and immediately
264 decoded into the Char buffer (see
265 GHC.IO.Handle.Internals.readTextDevice). The only way there might be
266 some data left in the byte buffer is if there is a partial multi-byte
267 character sequence that cannot be decoded into a full character.
269 Note that the buffering mode (haBufferMode) makes no difference when
270 reading data into a Handle. When reading, we can always just read all
271 the data there is available without blocking, decode it into the Char
272 buffer, and then provide it immediately to the caller.
274 [note Buffered Writing]
276 Characters are written into the Char buffer by e.g. hPutStr. At the
277 end of the operation, or when the char buffer is full, the buffer is
278 decoded to the byte buffer (see writeCharBuffer). This is so that we
279 can detect encoding errors at the right point.
281 Hence, the Char buffer is always empty between Handle operations.
285 The char buffer is always a default size (dEFAULT_CHAR_BUFFER_SIZE).
286 The byte buffer size is chosen by the underlying device (via its
287 IODevice.newBuffer). Hence the size of these buffers is not under
290 There are certain minimum sizes for these buffers imposed by the
291 library (but not checked):
293 - we must be able to buffer at least one character, so that
296 - the byte buffer must be able to store at least one encoded
297 character in the current encoding (6 bytes?)
299 - when reading, the char buffer must have room for two characters, so
300 that we can spot the \r\n sequence.
302 How do we implement hSetBuffering?
304 For reading, we have never used the user-supplied buffer size, because
305 there's no point: we always pass all available data to the reader
306 immediately. Buffering would imply waiting until a certain amount of
307 data is available, which has no advantages. So hSetBuffering is
308 essentially a no-op for read handles, except that it turns on/off raw
309 mode for the underlying device if necessary.
311 For writing, the buffering mode is handled by the write operations
312 themselves (hPutChar and hPutStr). Every write ends with
313 writeCharBuffer, which checks whether the buffer should be flushed
314 according to the current buffering mode. Additionally, we look for
315 newlines and flush if the mode is LineBuffering.
317 [note Buffer Flushing]
319 ** Flushing the Char buffer
321 We must be able to flush the Char buffer, in order to implement
322 hSetEncoding, and things like hGetBuf which want to read raw bytes.
324 Flushing the Char buffer on a write Handle is easy: it is always empty.
326 Flushing the Char buffer on a read Handle involves rewinding the byte
327 buffer to the point representing the next Char in the Char buffer.
330 - remembering the state of the byte buffer *before* the last decode
332 - re-decoding the bytes that represent the chars already read from the
333 Char buffer. This gives us the point in the byte buffer that
334 represents the *next* Char to be read.
336 In order for this to work, after readTextHandle we must NOT MODIFY THE
337 CONTENTS OF THE BYTE OR CHAR BUFFERS, except to remove characters from
340 ** Flushing the byte buffer
342 The byte buffer can be flushed if the Char buffer has already been
343 flushed (see above). For a read Handle, flushing the byte buffer
344 means seeking the device back by the number of bytes in the buffer,
345 and hence it is only possible on a seekable Handle.
349 -- ---------------------------------------------------------------------------
350 -- Newline translation
352 -- | The representation of a newline in the external file or stream.
353 data Newline = LF -- ^ '\n'
355 deriving (Eq, Ord, Read, Show)
357 -- | Specifies the translation, if any, of newline characters between
358 -- internal Strings and the external file or stream. Haskell Strings
359 -- are assumed to represent newlines with the '\n' character; the
360 -- newline mode specifies how to translate '\n' on output, and what to
361 -- translate into '\n' on input.
363 = NewlineMode { inputNL :: Newline,
364 -- ^ the representation of newlines on input
366 -- ^ the representation of newlines on output
368 deriving (Eq, Ord, Read, Show)
370 -- | The native newline representation for the current platform: 'LF'
371 -- on Unix systems, 'CRLF' on Windows.
372 nativeNewline :: Newline
373 #ifdef mingw32_HOST_OS
379 -- | Map '\r\n' into '\n' on input, and '\n' to the native newline
380 -- represetnation on output. This mode can be used on any platform, and
381 -- works with text files using any newline convention. The downside is
382 -- that @readFile >>= writeFile@ might yield a different file.
384 -- > universalNewlineMode = NewlineMode { inputNL = CRLF,
385 -- > outputNL = nativeNewline }
387 universalNewlineMode :: NewlineMode
388 universalNewlineMode = NewlineMode { inputNL = CRLF,
389 outputNL = nativeNewline }
391 -- | Use the native newline representation on both input and output
393 -- > nativeNewlineMode = NewlineMode { inputNL = nativeNewline
394 -- > outputNL = nativeNewline }
396 nativeNewlineMode :: NewlineMode
397 nativeNewlineMode = NewlineMode { inputNL = nativeNewline,
398 outputNL = nativeNewline }
400 -- | Do no newline translation at all.
402 -- > noNewlineTranslation = NewlineMode { inputNL = LF, outputNL = LF }
404 noNewlineTranslation :: NewlineMode
405 noNewlineTranslation = NewlineMode { inputNL = LF, outputNL = LF }
407 -- ---------------------------------------------------------------------------
408 -- Show instance for Handles
410 -- handle types are 'show'n when printing error msgs, so
411 -- we provide a more user-friendly Show instance for it
412 -- than the derived one.
414 instance Show HandleType where
417 ClosedHandle -> showString "closed"
418 SemiClosedHandle -> showString "semi-closed"
419 ReadHandle -> showString "readable"
420 WriteHandle -> showString "writable"
421 AppendHandle -> showString "writable (append)"
422 ReadWriteHandle -> showString "read-writable"
424 instance Show Handle where
425 showsPrec _ (FileHandle file _) = showHandle file
426 showsPrec _ (DuplexHandle file _ _) = showHandle file
428 showHandle :: FilePath -> String -> String
429 showHandle file = showString "{handle: " . showString file . showString "}"