2 % (c) The AQUA Project, Glasgow University, 1994-1996
5 \section[IO]{Module @IO@}
8 {-# OPTIONS -fno-implicit-prelude -#include "cbits/stgio.h" #-}
13 IOMode(ReadMode,WriteMode,AppendMode,ReadWriteMode),
14 BufferMode(NoBuffering,LineBuffering,BlockBuffering),
15 SeekMode(AbsoluteSeek,RelativeSeek,SeekFromEnd),
17 stdin, stdout, stderr,
20 hFileSize, hIsEOF, isEOF,
21 hSetBuffering, hGetBuffering, hFlush,
22 hGetPosn, hSetPosn, hSeek,
23 hWaitForInput, hReady, hGetChar, hGetLine, hLookAhead, hGetContents,
24 hPutChar, hPutStr, hPutStrLn, hPrint,
25 hIsOpen, hIsClosed, hIsReadable, hIsWritable, hIsSeekable,
27 isAlreadyExistsError, isDoesNotExistError, isAlreadyInUseError,
28 isFullError, isEOFError,
29 isIllegalOperation, isPermissionError, isUserError,
31 ioeGetHandle, ioeGetFileName,
32 try, bracket, bracket_
36 import PrelUnsafe ( unsafePerformIO, unsafeInterleaveIO )
38 import PrelArr ( MutableByteArray(..), newCharArray )
39 import PrelHandle -- much of the real stuff is in here
40 import PrelPack ( unpackNBytesST )
42 import PrelRead ( readParen, Read(..), reads, lex )
48 #ifndef __PARALLEL_HASKELL__
49 import PrelForeign ( ForeignObj, makeForeignObj, writeForeignObj )
53 import Char ( ord, chr )
56 %*********************************************************
58 \subsection{Signatures}
60 %*********************************************************
63 --IOHandle:hClose :: Handle -> IO ()
64 --IOHandle:hFileSize :: Handle -> IO Integer
65 --IOHandle:hFlush :: Handle -> IO ()
66 --IOHandle:hGetBuffering :: Handle -> IO BufferMode
67 hGetChar :: Handle -> IO Char
68 hGetContents :: Handle -> IO String
69 --IOHandle:hGetPosn :: Handle -> IO HandlePosn
70 --IOHandle:hIsClosed :: Handle -> IO Bool
71 --IOHandle:hIsEOF :: Handle -> IO Bool
72 --IOHandle:hIsOpen :: Handle -> IO Bool
73 --IOHandle:hIsReadable :: Handle -> IO Bool
74 --IOHandle:hIsSeekable :: Handle -> IO Bool
75 --IOHandle:hIsWritable :: Handle -> IO Bool
76 hLookAhead :: Handle -> IO Char
77 hPrint :: Show a => Handle -> a -> IO ()
78 hPutChar :: Handle -> Char -> IO ()
79 hPutStr :: Handle -> String -> IO ()
80 hPutStrLn :: Handle -> String -> IO ()
81 hReady :: Handle -> IO Bool
82 hWaitForInput :: Handle -> Int -> IO Bool
84 --IOHandle:hSeek :: Handle -> SeekMode -> Integer -> IO ()
85 --IOHandle:hSetBuffering :: Handle -> BufferMode -> IO ()
86 --IOHandle:hSetPosn :: HandlePosn -> IO ()
87 -- ioeGetFileName :: IOError -> Maybe FilePath
88 -- ioeGetErrorString :: IOError -> Maybe String
89 -- ioeGetHandle :: IOError -> Maybe Handle
90 -- isAlreadyExistsError :: IOError -> Bool
91 -- isAlreadyInUseError :: IOError -> Bool
92 --IOHandle:isEOF :: IO Bool
93 -- isEOFError :: IOError -> Bool
94 -- isFullError :: IOError -> Bool
95 -- isIllegalOperation :: IOError -> Bool
96 -- isPermissionError :: IOError -> Bool
97 -- isUserError :: IOError -> Bool
98 --IOHandle:openFile :: FilePath -> IOMode -> IO Handle
99 --IOHandle:stdin, stdout, stderr :: Handle
102 Standard instances for @Handle@:
105 instance Eq IOError where
106 (IOError h1 e1 str1) == (IOError h2 e2 str2) =
107 e1==e2 && str1==str2 && h1==h2
109 #ifndef __CONCURRENT_HASKELL__
111 instance Eq Handle where
112 (Handle h1) == (Handle h2) = h1 == h2
116 {- OLD equality instance. The simpler one above
117 seems more accurate! This one is still used for concurrent haskell,
118 since there's no equality instance over MVars.
121 instance Eq Handle where
130 (ErrorHandle (IOError h1 _ _), ErrorHandle (IOError h2 _ _)) -> h1 == h2
131 (ClosedHandle, ClosedHandle) -> True
132 (SemiClosedHandle v1 _, SemiClosedHandle v2 _) -> v1 == v2
133 (ReadHandle v1 _ _ , ReadHandle v2 _ _) -> v1 == v2
134 (WriteHandle v1 _ _ , WriteHandle v2 _ _) -> v1 == v2
135 (AppendHandle v1 _ _ , AppendHandle v2 _ _) -> v1 == v2
136 (ReadWriteHandle v1 _ _ , ReadWriteHandle v2 _ _) -> v1 == v2
141 instance Show Handle where {showsPrec p h = showString "<<Handle>>"}
143 --Type declared in IOHandle, instance here because it depends on Eq.Handle
144 instance Eq HandlePosn where
145 (HandlePosn h1 p1) == (HandlePosn h2 p2) = p1==p2 && h1==h2
147 -- Type declared in IOBase, instance here because it
148 -- depends on PrelRead.(Read Maybe) instance.
149 instance Read BufferMode where
152 (\r -> let lr = lex r
154 [(NoBuffering, rest) | ("NoBuffering", rest) <- lr] ++
155 [(LineBuffering,rest) | ("LineBuffering",rest) <- lr] ++
156 [(BlockBuffering mb,rest2) | ("BlockBuffering",rest1) <- lr,
157 (mb, rest2) <- reads rest1])
161 %*********************************************************
163 \subsection{Simple input operations}
165 %*********************************************************
167 Computation @hReady hdl@ indicates whether at least
168 one item is available for input from handle {\em hdl}.
170 @hWaitForInput@ is the generalisation, wait for \tr{n} seconds
171 before deciding whether the Handle has run dry or not.
174 --hReady :: Handle -> IO Bool
175 hReady h = hWaitForInput h 0
177 --hWaitForInput :: Handle -> Int -> IO Bool
178 hWaitForInput handle nsecs = do
179 hdl <- wantReadableHandle handle
180 rc <- _ccall_ inputReady (filePtr hdl) nsecs
181 writeHandle handle (markHandle hdl)
185 _ -> constructErrorAndFail "hWaitForInput"
188 Computation $hGetChar hdl$ reads the next character from handle
189 {\em hdl}, blocking until a character is available.
192 --hGetChar :: Handle -> IO Char
195 hdl <- wantReadableHandle handle
196 intc <- _ccall_ fileGetc (filePtr hdl)
197 writeHandle handle (markHandle hdl)
199 then return (chr intc)
200 else constructErrorAndFail "hGetChar"
202 hGetLine :: Handle -> IO String
213 Computation $hLookahead hdl$ returns the next character from handle
214 {\em hdl} without removing it from the input buffer, blocking until a
215 character is available.
218 --hLookAhead :: Handle -> IO Char
220 hLookAhead handle = do
221 hdl <- wantReadableHandle handle
222 intc <- _ccall_ fileLookAhead (filePtr hdl)
223 writeHandle handle (markHandle hdl)
225 then return (chr intc)
226 else constructErrorAndFail "hLookAhead"
231 %*********************************************************
233 \subsection{Getting the entire contents of a handle}
235 %*********************************************************
237 Computation $hGetContents hdl$ returns the list of characters
238 corresponding to the unread portion of the channel or file managed by
239 {\em hdl}, which is made semi-closed.
242 --hGetContents :: Handle -> IO String
244 hGetContents handle = do
245 hdl_ <- wantReadableHandle handle
247 To avoid introducing an extra layer of buffering here,
248 we provide three lazy read methods, based on character,
249 line, and block buffering.
251 hdl_ <- getBufferMode hdl_
252 case (bufferMode hdl_) of
253 Just LineBuffering -> do
254 buf_info <- allocBuf Nothing
255 writeHandle handle (SemiClosedHandle (filePtr hdl_) buf_info)
256 unsafeInterleaveIO (lazyReadLine handle)
257 Just (BlockBuffering size) -> do
258 buf_info <- allocBuf size
259 writeHandle handle (SemiClosedHandle (filePtr hdl_) buf_info)
260 unsafeInterleaveIO (lazyReadBlock handle)
261 _ -> do -- Nothing is treated pessimistically as NoBuffering
262 writeHandle handle (SemiClosedHandle (filePtr hdl_) (``NULL'', 0))
263 unsafeInterleaveIO (lazyReadChar handle)
265 allocBuf :: Maybe Int -> IO (Addr, Int)
267 buf <- _ccall_ malloc size
269 then return (buf, size)
270 else fail (IOError Nothing ResourceExhausted "not enough virtual memory")
275 Nothing -> ``BUFSIZ''
278 Note that someone may yank our handle out from under us, and then re-use
279 the same FILE * for something else. Therefore, we have to re-examine the
280 handle every time through.
283 lazyReadBlock :: Handle -> IO String
284 lazyReadLine :: Handle -> IO String
285 lazyReadChar :: Handle -> IO String
287 lazyReadBlock handle = do
288 htype <- readHandle handle
290 -- There cannae be an ErrorHandle here
292 writeHandle handle htype
294 SemiClosedHandle fp (buf, size) -> do
295 bytes <- _ccall_ readBlock buf fp size
296 some <- (if bytes <= 0
298 else stToIO (unpackNBytesST buf bytes))
303 #ifndef __PARALLEL_HASKELL__
304 writeForeignObj fp ``NULL''
305 writeHandle handle (SemiClosedHandle fp (``NULL'', 0))
307 writeHandle handle (SemiClosedHandle ``NULL'' (``NULL'', 0))
311 writeHandle handle htype
312 more <- unsafeInterleaveIO (lazyReadBlock handle)
313 return (some ++ more)
315 lazyReadLine handle = do
316 htype <- readHandle handle
318 -- There cannae be an ErrorHandle here
320 writeHandle handle htype
322 SemiClosedHandle fp (buf, size) -> do
323 bytes <- _ccall_ readLine buf fp size
324 some <- (if bytes <= 0
326 else stToIO (unpackNBytesST buf bytes))
331 #ifndef __PARALLEL_HASKELL__
332 writeForeignObj fp ``NULL''
333 writeHandle handle (SemiClosedHandle fp (``NULL'', 0))
335 writeHandle handle (SemiClosedHandle ``NULL'' (``NULL'', 0))
339 writeHandle handle htype
340 more <- unsafeInterleaveIO (lazyReadLine handle)
341 return (some ++ more)
343 lazyReadChar handle = do
344 htype <- readHandle handle
346 -- There cannae be an ErrorHandle here
348 writeHandle handle htype
350 SemiClosedHandle fp buf_info -> do
351 char <- _ccall_ readChar fp
355 #ifndef __PARALLEL_HASKELL__
356 writeForeignObj fp ``NULL''
357 writeHandle handle (SemiClosedHandle fp (``NULL'', 0))
359 writeHandle handle (SemiClosedHandle ``NULL'' (``NULL'', 0))
363 writeHandle handle htype
364 more <- unsafeInterleaveIO (lazyReadChar handle)
365 return (chr char : more)
370 %*********************************************************
372 \subsection{Simple output functions}
374 %*********************************************************
376 Computation $hPutChar hdl c$ writes the character {\em c} to the file
377 or channel managed by {\em hdl}. Characters may be buffered if
378 buffering is enabled for {\em hdl}.
381 --hPutChar :: Handle -> Char -> IO ()
383 hPutChar handle c = do
384 hdl <- wantWriteableHandle handle
385 rc <- _ccall_ filePutc (filePtr hdl) (ord c)
386 writeHandle handle (markHandle hdl)
389 else constructErrorAndFail "hPutChar"
392 Computation $hPutStr hdl s$ writes the string {\em s} to the file or
393 channel managed by {\em hdl}.
396 --hPutStr :: Handle -> String -> IO ()
398 hPutStr handle str = do
399 hdl <- wantWriteableHandle handle
401 The code below is not correct for line-buffered terminal streams,
402 as the output stream is not flushed when terminal input is requested
403 again, just upon seeing a newline character. A temporary fix for the
404 most common line-buffered output stream, stdout, is to assume the
405 buffering it was given when created (no buffering). This is not
406 as bad as it looks, since stdio buffering sits underneath this.
410 hdl <- getBufferMode hdl
412 (case bufferMode hdl of
413 Just LineBuffering ->
414 writeChars (filePtr hdl) str
415 --writeLines (filePtr hdl) str
416 Just (BlockBuffering (Just size)) ->
417 writeBlocks (filePtr hdl) size str
418 Just (BlockBuffering Nothing) ->
419 writeBlocks (filePtr hdl) ``BUFSIZ'' str
420 _ -> -- Nothing is treated pessimistically as NoBuffering
421 writeChars (filePtr hdl) str
423 writeHandle handle (markHandle hdl)
426 else constructErrorAndFail "hPutStr"
428 #ifndef __PARALLEL_HASKELL__
429 writeLines :: ForeignObj -> String -> IO Bool
431 writeLines :: Addr -> String -> IO Bool
433 writeLines = writeChunks ``BUFSIZ'' True
435 #ifndef __PARALLEL_HASKELL__
436 writeBlocks :: ForeignObj -> Int -> String -> IO Bool
438 writeBlocks :: Addr -> Int -> String -> IO Bool
440 writeBlocks fp size s = writeChunks size False fp s
443 The breaking up of output into lines along \n boundaries
444 works fine as long as there are newlines to split by.
445 Avoid the splitting up into lines alltogether (doesn't work
446 for overly long lines like the stuff that showsPrec instances
447 normally return). Instead, we split them up into fixed size
448 chunks before blasting them off to the Real World.
450 Hacked to avoid multiple passes over the strings - unsightly, but
451 a whole lot quicker. -- SOF 3/96
454 #ifndef __PARALLEL_HASKELL__
455 writeChunks :: Int -> Bool -> ForeignObj -> String -> IO Bool
457 writeChunks :: Int -> Bool -> Addr -> String -> IO Bool
459 writeChunks (I# bufLen) chopOnNewLine fp s =
460 stToIO (newCharArray (0,I# bufLen)) >>= \ arr@(MutableByteArray _ arr#) ->
462 write_char :: MutableByteArray# RealWorld -> Int# -> Char# -> IO ()
463 write_char arr# n x = IO $ \ s# ->
464 case (writeCharArray# arr# n x s#) of { s1# ->
467 shoveString :: Int# -> [Char] -> IO Bool
474 rc <- _ccall_ writeFile arr fp (I# n)
480 {- Flushing lines - should we bother? Yes, for line-buffered output. -}
481 if n ==# bufLen || (chopOnNewLine && (x `eqChar#` '\n'#))
483 rc <- _ccall_ writeFile arr fp (I# (n +# 1#))
485 then shoveString 0# xs
488 shoveString (n +# 1#) xs
492 #ifndef __PARALLEL_HASKELL__
493 writeChars :: ForeignObj -> String -> IO Bool
495 writeChars :: Addr -> String -> IO Bool
497 writeChars fp "" = return True
498 writeChars fp (c:cs) = do
499 rc <- _ccall_ filePutc fp (ord c)
501 then writeChars fp cs
506 The @hPutBuf hdl len elt_sz buf@ action writes the buffer @buf@ to
507 the file/channel managed by @hdl@
508 the string {\em s} to the file or
509 channel managed by {\em hdl}.
512 hPutBuf :: Handle -> Int -> Int -> ByteArray Int -> IO ()
513 hPutBuf handle len el_sz buf = do
514 hdl <- wantWriteableHandle handle
516 The code below is not correct for line-buffered terminal streams,
517 as the output stream is not flushed when terminal input is requested
518 again, just upon seeing a newline character. A temporary fix for the
519 most common line-buffered output stream, stdout, is to assume the
520 buffering it was given when created (no buffering). This is not
521 as bad as it looks, since stdio buffering sits underneath this.
525 hdl <- getBufferMode hdl
527 (case bufferMode hdl of
528 Just LineBuffering ->
529 writeChars (filePtr hdl) str
530 --writeLines (filePtr hdl) str
531 Just (BlockBuffering (Just size)) ->
532 writeBlocks (filePtr hdl) size str
533 Just (BlockBuffering Nothing) ->
534 writeBlocks (filePtr hdl) ``BUFSIZ'' str
535 _ -> -- Nothing is treated pessimistically as NoBuffering
536 writeChars (filePtr hdl) str)
537 writeHandle handle (markHandle hdl)
540 else constructErrorAndFail "hPutBuf"
544 Computation $hPrint hdl t$ writes the string representation of {\em t}
545 given by the $shows$ function to the file or channel managed by {\em
548 SOF 2/97: Seem to have disappeared in 1.4 libs.
551 --hPrint :: Show a => Handle -> a -> IO ()
552 hPrint hdl = hPutStr hdl . show
555 Derived action @hPutStrLn hdl str@ writes the string \tr{str} to
556 the handle \tr{hdl}, adding a newline at the end.
559 --hPutStrLn :: Handle -> String -> IO ()
560 hPutStrLn hndl str = do
567 %*********************************************************
569 \subsection{Try and bracket}
571 %*********************************************************
573 The construct $try comp$ exposes errors which occur within a
574 computation, and which are not fully handled. It always succeeds.
577 try :: IO a -> IO (Either IOError a)
578 try f = catch (do r <- f
582 bracket :: IO a -> (a -> IO b) -> (a -> IO c) -> IO c
583 bracket before after m = do
591 -- variant of the above where middle computation doesn't want x
592 bracket_ :: IO a -> (a -> IO b) -> IO c -> IO c
593 bracket_ before after m = do