2 % (c) The AQUA Project, Glasgow University, 1994-1998
4 \section[IO]{Module @IO@}
6 Implementation of the standard Haskell IO interface, see
7 @http://haskell.org/onlinelibrary/io.html@ for the official
11 {-# OPTIONS -fno-implicit-prelude -#include "cbits/stgio.h" #-}
14 Handle, -- abstract, instance of: Eq, Show.
15 HandlePosn(..), -- abstract, instance of: Eq, Show.
17 IOMode(ReadMode,WriteMode,AppendMode,ReadWriteMode),
18 BufferMode(NoBuffering,LineBuffering,BlockBuffering),
19 SeekMode(AbsoluteSeek,RelativeSeek,SeekFromEnd),
21 stdin, stdout, stderr, -- :: Handle
23 openFile, -- :: FilePath -> IOMode -> IO Handle
24 hClose, -- :: Handle -> IO ()
25 hFileSize, -- :: Handle -> IO Integer
26 hIsEOF, -- :: Handle -> IO Bool
29 hSetBuffering, -- :: Handle -> BufferMode -> IO ()
30 hGetBuffering, -- :: Handle -> IO BufferMode
31 hFlush, -- :: Handle -> IO ()
32 hGetPosn, -- :: Handle -> IO HandlePosn
33 hSetPosn, -- :: Handle -> HandlePosn -> IO ()
34 hSeek, -- :: Handle -> SeekMode -> Integer -> IO ()
35 hWaitForInput, -- :: Handle -> Int -> IO Bool
36 hReady, -- :: Handle -> IO Bool
37 hGetChar, -- :: Handle -> IO Char
38 hGetLine, -- :: Handle -> IO [Char]
39 hLookAhead, -- :: Handle -> IO Char
40 hGetContents, -- :: Handle -> IO [Char]
41 hPutChar, -- :: Handle -> Char -> IO ()
42 hPutStr, -- :: Handle -> [Char] -> IO ()
43 hPutStrLn, -- :: Handle -> [Char] -> IO ()
44 hPrint, -- :: Show a => Handle -> a -> IO ()
45 hIsOpen, hIsClosed, -- :: Handle -> IO Bool
46 hIsReadable, hIsWritable, -- :: Handle -> IO Bool
47 hIsSeekable, -- :: Handle -> IO Bool
49 isAlreadyExistsError, isDoesNotExistError, -- :: IOError -> Bool
50 isAlreadyInUseError, isFullError,
51 isEOFError, isIllegalOperation,
52 isPermissionError, isUserError,
54 ioeGetErrorString, -- :: IOError -> String
55 ioeGetHandle, -- :: IOError -> Maybe Handle
56 ioeGetFileName, -- :: IOError -> Maybe FilePath
58 try, -- :: IO a -> IO (Either IOError a)
59 bracket, -- :: IO a -> (a -> IO b) -> (a -> IO c) -> IO c
60 bracket_, -- :: IO a -> (a -> IO b) -> IO c -> IO c
62 -- Non-standard extension (but will hopefully become standard with 1.5) is
63 -- to export the Prelude io functions via IO (in addition to exporting them
64 -- from the prelude...for now.)
66 FilePath, -- :: String
68 ioError, -- :: IOError -> IO a
69 userError, -- :: String -> IOError
70 catch, -- :: IO a -> (IOError -> IO a) -> IO a
71 interact, -- :: (String -> String) -> IO ()
73 putChar, -- :: Char -> IO ()
74 putStr, -- :: String -> IO ()
75 putStrLn, -- :: String -> IO ()
76 print, -- :: Show a => a -> IO ()
77 getChar, -- :: IO Char
78 getLine, -- :: IO String
79 getContents, -- :: IO String
80 readFile, -- :: FilePath -> IO String
81 writeFile, -- :: FilePath -> String -> IO ()
82 appendFile, -- :: FilePath -> String -> IO ()
83 readIO, -- :: Read a => String -> IO a
84 readLn, -- :: Read a => IO a
102 import PrelHandle -- much of the real stuff is in here
104 import PrelRead ( readParen, Read(..), reads, lex,
108 import PrelMaybe ( Either(..), Maybe(..) )
109 import PrelAddr ( Addr(..), nullAddr )
110 import PrelArr ( ByteArray )
111 import PrelPack ( unpackNBytesAccST )
112 import PrelException ( ioError, catch )
114 #ifndef __PARALLEL_HASKELL__
115 import PrelForeign ( ForeignObj )
118 import Char ( ord, chr )
120 #endif /* ndef __HUGS__ */
125 Standard instances for @Handle@:
128 instance Eq IOError where
129 (IOError h1 e1 loc1 str1) == (IOError h2 e2 loc2 str2) =
130 e1==e2 && str1==str2 && h1==h2 && loc1 == loc2
132 instance Eq Handle where
133 (Handle h1) == (Handle h2) = h1 == h2
135 --Type declared in IOHandle, instance here because it depends on Eq.Handle
136 instance Eq HandlePosn where
137 (HandlePosn h1 p1) == (HandlePosn h2 p2) = p1==p2 && h1==h2
139 -- Type declared in IOBase, instance here because it
140 -- depends on PrelRead.(Read Maybe) instance.
141 instance Read BufferMode where
144 (\r -> let lr = lex r
146 [(NoBuffering, rest) | ("NoBuffering", rest) <- lr] ++
147 [(LineBuffering,rest) | ("LineBuffering",rest) <- lr] ++
148 [(BlockBuffering mb,rest2) | ("BlockBuffering",rest1) <- lr,
149 (mb, rest2) <- reads rest1])
153 %*********************************************************
155 \subsection{Simple input operations}
157 %*********************************************************
159 Computation @hReady hdl@ indicates whether at least
160 one item is available for input from handle {\em hdl}.
162 @hWaitForInput@ is the generalisation, wait for \tr{n} milliseconds
163 before deciding whether the Handle has run dry or not.
165 If @hWaitForInput@ finds anything in the Handle's buffer, it immediately returns.
166 If not, it tries to read from the underlying OS handle. Notice that
167 for buffered Handles connected to terminals this means waiting until a complete
171 hReady :: Handle -> IO Bool
172 hReady h = hWaitForInput h 0
174 hWaitForInput :: Handle -> Int -> IO Bool
175 hWaitForInput handle msecs =
176 wantReadableHandle "hWaitForInput" handle $ \ handle_ -> do
177 rc <- inputReady (haFO__ handle_) (msecs::Int) -- ConcHask: SAFE, won't block
181 _ -> constructErrorAndFail "hWaitForInput"
184 @hGetChar hdl@ reads the next character from handle @hdl@,
185 blocking until a character is available.
188 hGetChar :: Handle -> IO Char
190 wantReadableHandle "hGetChar" handle $ \ handle_ -> do
191 let fo = haFO__ handle_
192 intc <- mayBlock fo (fileGetc fo) -- ConcHask: UNSAFE, may block
193 if intc /= ((-1)::Int)
194 then return (chr intc)
195 else constructErrorAndFail "hGetChar"
198 If EOF is reached before EOL is encountered, ignore the
199 EOF and return the partial line. Next attempt at calling
200 hGetLine on the handle will yield an EOF IO exception though.
202 hGetLine :: Handle -> IO String
216 if isEOFError err then
228 @hLookahead hdl@ returns the next character from handle @hdl@
229 without removing it from the input buffer, blocking until a
230 character is available.
233 hLookAhead :: Handle -> IO Char
235 wantReadableHandle "hLookAhead" handle $ \ handle_ -> do
236 let fo = haFO__ handle_
237 intc <- mayBlock fo (fileLookAhead fo) -- ConcHask: UNSAFE, may block
239 then return (chr intc)
240 else constructErrorAndFail "hLookAhead"
245 %*********************************************************
247 \subsection{Getting the entire contents of a handle}
249 %*********************************************************
251 @hGetContents hdl@ returns the list of characters corresponding
252 to the unread portion of the channel or file managed by @hdl@,
253 which is made semi-closed.
256 hGetContents :: Handle -> IO String
257 hGetContents handle =
258 -- can't use wantReadableHandle here, because we want to side effect
260 withHandle handle $ \ handle_ -> do
261 case haType__ handle_ of
262 ErrorHandle theError -> ioError theError
263 ClosedHandle -> ioe_closedHandle "hGetContents" handle
264 SemiClosedHandle -> ioe_closedHandle "hGetContents" handle
265 AppendHandle -> ioError not_readable_error
266 WriteHandle -> ioError not_readable_error
269 To avoid introducing an extra layer of buffering here,
270 we provide three lazy read methods, based on character,
271 line, and block buffering.
273 let handle_' = handle_{ haType__ = SemiClosedHandle }
274 case (haBufferMode__ handle_) of
276 str <- unsafeInterleaveIO (lazyReadLine handle (haFO__ handle_))
277 return (handle_', str)
278 BlockBuffering _ -> do
279 str <- unsafeInterleaveIO (lazyReadBlock handle (haFO__ handle_))
280 return (handle_', str)
282 str <- unsafeInterleaveIO (lazyReadChar handle (haFO__ handle_))
283 return (handle_', str)
286 IOError (Just handle) IllegalOperation "hGetContents"
287 ("handle is not open for reading")
290 Note that someone may close the semi-closed handle (or change its buffering),
291 so each these lazy read functions are pulled on, they have to check whether
292 the handle has indeed been closed.
295 #ifndef __PARALLEL_HASKELL__
296 lazyReadBlock :: Handle -> ForeignObj -> IO String
297 lazyReadLine :: Handle -> ForeignObj -> IO String
298 lazyReadChar :: Handle -> ForeignObj -> IO String
300 lazyReadBlock :: Handle -> Addr -> IO String
301 lazyReadLine :: Handle -> Addr -> IO String
302 lazyReadChar :: Handle -> Addr -> IO String
305 lazyReadBlock handle fo = do
306 buf <- getBufStart fo 0
307 bytes <- mayBlock fo (readBlock fo) -- ConcHask: UNSAFE, may block.
309 -3 -> -- buffering has been turned off, use lazyReadChar instead
310 lazyReadChar handle fo
312 -1 -> -- an error occurred, close the handle
313 withHandle handle $ \ handle_ -> do
314 closeFile (haFO__ handle_) 0{-don't bother flushing-} -- ConcHask: SAFE, won't block.
315 return (handle_ { haType__ = ClosedHandle,
316 haFO__ = nullFile__ },
319 more <- unsafeInterleaveIO (lazyReadBlock handle fo)
320 stToIO (unpackNBytesAccST buf bytes more)
322 lazyReadLine handle fo = do
323 bytes <- mayBlock fo (readLine fo) -- ConcHask: UNSAFE, may block.
325 -3 -> -- buffering has been turned off, use lazyReadChar instead
326 lazyReadChar handle fo
327 -2 -> return "" -- handle closed by someone else, stop reading.
328 -1 -> -- an error occurred, close the handle
329 withHandle handle $ \ handle_ -> do
330 closeFile (haFO__ handle_) 0{- don't bother flushing-} -- ConcHask: SAFE, won't block
331 return (handle_ { haType__ = ClosedHandle,
332 haFO__ = nullFile__ },
335 more <- unsafeInterleaveIO (lazyReadLine handle fo)
336 buf <- getBufStart fo bytes -- ConcHask: won't block
337 stToIO (unpackNBytesAccST buf bytes more)
339 lazyReadChar handle fo = do
340 char <- mayBlock fo (readChar fo) -- ConcHask: UNSAFE, may block.
342 -4 -> -- buffering is now block-buffered, use lazyReadBlock instead
343 lazyReadBlock handle fo
345 -3 -> -- buffering is now line-buffered, use lazyReadLine instead
346 lazyReadLine handle fo
348 -1 -> -- error, silently close handle.
349 withHandle handle $ \ handle_ -> do
350 closeFile (haFO__ handle_) 0{-don't bother flusing-} -- ConcHask: SAFE, won't block
351 return (handle_{ haType__ = ClosedHandle,
352 haFO__ = nullFile__ },
355 more <- unsafeInterleaveIO (lazyReadChar handle fo)
356 return (chr char : more)
361 %*********************************************************
363 \subsection{Simple output functions}
365 %*********************************************************
367 @hPutChar hdl ch@ writes the character @ch@ to the file
368 or channel managed by @hdl@. Characters may be buffered if
369 buffering is enabled for @hdl@
372 hPutChar :: Handle -> Char -> IO ()
374 wantWriteableHandle "hPutChar" handle $ \ handle_ -> do
375 let fo = haFO__ handle_
377 rc <- mayBlock fo (filePutc fo c) -- ConcHask: UNSAFE, may block.
380 else constructErrorAndFail "hPutChar"
384 @hPutStr hdl s@ writes the string @s@ to the file or
385 channel managed by @hdl@, buffering the output if needs be.
388 hPutStr :: Handle -> String -> IO ()
390 wantWriteableHandle "hPutStr" handle $ \ handle_ -> do
391 let fo = haFO__ handle_
393 case haBufferMode__ handle_ of
395 buf <- getWriteableBuf fo
398 writeLines fo buf bsz pos str
399 BlockBuffering _ -> do
400 buf <- getWriteableBuf fo
403 writeBlocks fo buf bsz pos str
408 Going across the border between Haskell and C is relatively costly,
409 so for block writes we pack the character strings on the Haskell-side
410 before passing the external write routine a pointer to the buffer.
415 #ifdef __CONCURRENT_HASKELL__
416 /* See comment in shoveString below for explanation */
417 #warning delayed update of buffer disnae work with killThread
420 #ifndef __PARALLEL_HASKELL__
421 writeLines :: ForeignObj -> Addr -> Int -> Int -> String -> IO ()
423 writeLines :: Addr -> Addr -> Int -> Int -> String -> IO ()
425 writeLines obj buf bufLen initPos s =
427 shoveString :: Int -> [Char] -> IO ()
432 setBufWPtr obj 0{-new pos-}
435 At the end of a buffer write, update the buffer position
436 in the underlying file object, so that if the handle
437 is subsequently dropped by the program, the whole
438 buffer will be properly flushed.
440 There's one case where this delayed up-date of the buffer
441 position can go wrong: if a thread is killed, it might be
442 in the middle of filling up a buffer, with the result that
443 the partial buffer update is lost upon finalisation. Not
444 that killing of threads is supported at the moment.
450 primWriteCharOffAddr buf n x
451 {- Flushing on buffer exhaustion or newlines (even if it isn't the last one) -}
452 if n == bufLen || x == '\n'
454 rc <- mayBlock obj (writeFileObject obj (n + 1)) -- ConcHask: UNSAFE, may block.
456 then shoveString 0 xs
457 else constructErrorAndFail "writeLines"
459 shoveString (n + 1) xs
461 shoveString initPos s
462 #else /* ndef __HUGS__ */
463 #ifndef __PARALLEL_HASKELL__
464 writeLines :: ForeignObj -> Addr -> Int -> Int -> String -> IO ()
466 writeLines :: Addr -> Addr -> Int -> Int -> String -> IO ()
468 writeLines obj buf (I# bufLen) (I# initPos#) s =
470 write_char :: Addr -> Int# -> Char# -> IO ()
471 write_char (A# buf#) n# c# =
473 case (writeCharOffAddr# buf# n# c# s#) of s2# -> (# s2#, () #)
475 shoveString :: Int# -> [Char] -> IO ()
483 At the end of a buffer write, update the buffer position
484 in the underlying file object, so that if the handle
485 is subsequently dropped by the program, the whole
486 buffer will be properly flushed.
488 There's one case where this delayed up-date of the buffer
489 position can go wrong: if a thread is killed, it might be
490 in the middle of filling up a buffer, with the result that
491 the partial buffer update is lost upon finalisation. Not
492 that killing of threads is supported at the moment.
495 setBufWPtr obj (I# n)
499 {- Flushing on buffer exhaustion or newlines (even if it isn't the last one) -}
500 if n ==# bufLen || x `eqChar#` '\n'#
502 rc <- mayBlock obj (writeFileObject obj (I# (n +# 1#))) -- ConcHask: UNSAFE, may block.
504 then shoveString 0# xs
505 else constructErrorAndFail "writeLines"
507 shoveString (n +# 1#) xs
509 shoveString initPos# s
510 #endif /* ndef __HUGS__ */
513 #ifndef __PARALLEL_HASKELL__
514 writeBlocks :: ForeignObj -> Addr -> Int -> Int -> String -> IO ()
516 writeBlocks :: Addr -> Addr -> Int -> Int -> String -> IO ()
518 writeBlocks obj buf bufLen initPos s =
520 shoveString :: Int -> [Char] -> IO ()
525 setBufWPtr obj (0::Int)
528 At the end of a buffer write, update the buffer position
529 in the underlying file object, so that if the handle
530 is subsequently dropped by the program, the whole
531 buffer will be properly flushed.
533 There's one case where this delayed up-date of the buffer
534 position can go wrong: if a thread is killed, it might be
535 in the middle of filling up a buffer, with the result that
536 the partial buffer update is lost upon finalisation. However,
537 by the time killThread is supported, Haskell finalisers are also
538 likely to be in, which means the 'IOFileObject' hack can go
545 primWriteCharOffAddr buf n x
548 rc <- mayBlock obj (writeFileObject obj (n + 1)) -- ConcHask: UNSAFE, may block.
550 then shoveString 0 xs
551 else constructErrorAndFail "writeChunks"
553 shoveString (n + 1) xs
555 shoveString initPos s
556 #else /* ndef __HUGS__ */
557 #ifndef __PARALLEL_HASKELL__
558 writeBlocks :: ForeignObj -> Addr -> Int -> Int -> String -> IO ()
560 writeBlocks :: Addr -> Addr -> Int -> Int -> String -> IO ()
562 writeBlocks obj buf (I# bufLen) (I# initPos#) s =
564 write_char :: Addr -> Int# -> Char# -> IO ()
565 write_char (A# buf#) n# c# =
567 case (writeCharOffAddr# buf# n# c# s#) of s2# -> (# s2#, () #)
569 shoveString :: Int# -> [Char] -> IO ()
574 setBufWPtr obj (0::Int)
577 At the end of a buffer write, update the buffer position
578 in the underlying file object, so that if the handle
579 is subsequently dropped by the program, the whole
580 buffer will be properly flushed.
582 There's one case where this delayed up-date of the buffer
583 position can go wrong: if a thread is killed, it might be
584 in the middle of filling up a buffer, with the result that
585 the partial buffer update is lost upon finalisation. However,
586 by the time killThread is supported, Haskell finalisers are also
587 likely to be in, which means the 'IOFileObject' hack can go
591 setBufWPtr obj (I# n)
597 rc <- mayBlock obj (writeFileObject obj (I# (n +# 1#))) -- ConcHask: UNSAFE, may block.
599 then shoveString 0# xs
600 else constructErrorAndFail "writeChunks"
602 shoveString (n +# 1#) xs
604 shoveString initPos# s
605 #endif /* ndef __HUGS__ */
607 #ifndef __PARALLEL_HASKELL__
608 writeChars :: ForeignObj -> String -> IO ()
610 writeChars :: Addr -> String -> IO ()
612 writeChars _fo "" = return ()
613 writeChars fo (c:cs) = do
614 rc <- mayBlock fo (filePutc fo c) -- ConcHask: UNSAFE, may block.
616 then writeChars fo cs
617 else constructErrorAndFail "writeChars"
621 Computation @hPrint hdl t@ writes the string representation of {\em t}
622 given by the @shows@ function to the file or channel managed by {\em
625 [ Seem to have disappeared from the 1.4 interface - SOF 2/97 ]
628 hPrint :: Show a => Handle -> a -> IO ()
629 hPrint hdl = hPutStrLn hdl . show
632 Derived action @hPutStrLn hdl str@ writes the string \tr{str} to
633 the handle \tr{hdl}, adding a newline at the end.
636 hPutStrLn :: Handle -> String -> IO ()
637 hPutStrLn hndl str = do
644 %*********************************************************
646 \subsection{Try and bracket}
648 %*********************************************************
650 The construct @try comp@ exposes errors which occur within a
651 computation, and which are not fully handled. It always succeeds.
654 try :: IO a -> IO (Either IOError a)
655 try f = catch (do r <- f
659 bracket :: IO a -> (a -> IO b) -> (a -> IO c) -> IO c
660 bracket before after m = do
668 -- variant of the above where middle computation doesn't want x
669 bracket_ :: IO a -> (a -> IO b) -> IO c -> IO c
670 bracket_ before after m = do
679 %*********************************************************
681 \subsection{Standard IO}
683 %*********************************************************
685 The Prelude has from Day 1 provided a collection of common
686 IO functions. We define these here, but let the Prelude
690 putChar :: Char -> IO ()
691 putChar c = hPutChar stdout c
693 putStr :: String -> IO ()
694 putStr s = hPutStr stdout s
696 putStrLn :: String -> IO ()
697 putStrLn s = do putStr s
700 print :: Show a => a -> IO ()
701 print x = putStrLn (show x)
704 getChar = hGetChar stdin
707 getLine = hGetLine stdin
709 getContents :: IO String
710 getContents = hGetContents stdin
712 interact :: (String -> String) -> IO ()
713 interact f = do s <- getContents
716 readFile :: FilePath -> IO String
717 readFile name = openFile name ReadMode >>= hGetContents
719 writeFile :: FilePath -> String -> IO ()
720 writeFile name str = do
721 hdl <- openFile name WriteMode
725 appendFile :: FilePath -> String -> IO ()
726 appendFile name str = do
727 hdl <- openFile name AppendMode
731 readLn :: Read a => IO a
732 readLn = do l <- getLine
742 unimp s = error ("function not implemented: " ++ s)
749 = Handle { name :: FilePath,
750 file :: FILE_STAR, -- C handle
751 state :: HState, -- open/closed/semiclosed
759 instance Eq Handle where
760 h1 == h2 = file h1 == file h2
762 instance Show Handle where
763 showsPrec _ h = showString ("<<handle " ++ name h ++ "=" ++ show (file h) ++ ">>")
770 data IOMode = ReadMode | WriteMode | AppendMode | ReadWriteMode
771 deriving (Eq, Ord, Ix, Bounded, Enum, Read, Show)
773 data BufferMode = NoBuffering | LineBuffering
775 deriving (Eq, Ord, Read, Show)
777 data SeekMode = AbsoluteSeek | RelativeSeek | SeekFromEnd
778 deriving (Eq, Ord, Ix, Bounded, Enum, Read, Show)
780 data HState = HOpen | HSemiClosed | HClosed
783 stdin = Handle "stdin" (primRunST nh_stdin) HOpen ReadMode NoBuffering nULL 0
784 stdout = Handle "stdout" (primRunST nh_stdout) HOpen WriteMode LineBuffering nULL 0
785 stderr = Handle "stderr" (primRunST nh_stderr) HOpen WriteMode NoBuffering nULL 0
787 openFile :: FilePath -> IOMode -> IO Handle
789 = copy_String_to_cstring f >>= \nameptr ->
790 nh_open nameptr (mode2num mode) >>= \fh ->
793 then (ioError.IOError) ("openFile: can't open " ++ f ++ " in " ++ show mode)
794 else return (Handle f fh HOpen mode BlockBuffering nULL 0)
796 mode2num :: IOMode -> Int
797 mode2num ReadMode = 0
798 mode2num WriteMode = 1
799 mode2num AppendMode = 2
801 hClose :: Handle -> IO ()
803 | not (state h == HOpen)
804 = (ioError.IOError) ("hClose on non-open handle " ++ show h)
806 = nh_close (file h) >>
810 else (ioError.IOError) ("hClose: error closing " ++ name h)
812 hFileSize :: Handle -> IO Integer
813 hFileSize = unimp "IO.hFileSize"
814 hIsEOF :: Handle -> IO Bool
815 hIsEOF = unimp "IO.hIsEOF"
819 hSetBuffering :: Handle -> BufferMode -> IO ()
820 hSetBuffering = unimp "IO.hSetBuffering"
821 hGetBuffering :: Handle -> IO BufferMode
822 hGetBuffering = unimp "IO.hGetBuffering"
824 hFlush :: Handle -> IO ()
826 = if state h /= HOpen
827 then (ioError.IOError) ("hFlush on closed/semiclosed file " ++ name h)
828 else nh_flush (file h)
830 hGetPosn :: Handle -> IO HandlePosn
831 hGetPosn = unimp "IO.hGetPosn"
832 hSetPosn :: HandlePosn -> IO ()
833 hSetPosn = unimp "IO.hSetPosn"
834 hSeek :: Handle -> SeekMode -> Integer -> IO ()
835 hSeek = unimp "IO.hSeek"
836 hWaitForInput :: Handle -> Int -> IO Bool
837 hWaitForInput = unimp "hWaitForInput"
838 hReady :: Handle -> IO Bool
839 hReady h = hWaitForInput h 0
841 hGetChar :: Handle -> IO Char
843 = nh_read (file h) >>= \ci ->
844 return (primIntToChar ci)
846 hGetLine :: Handle -> IO String
847 hGetLine h = do c <- hGetChar h
848 if c=='\n' then return ""
849 else do cs <- hGetLine h
852 hLookAhead :: Handle -> IO Char
853 hLookAhead = unimp "IO.hLookAhead"
855 hGetContents :: Handle -> IO String
857 | not (state h == HOpen && mode h == ReadMode)
858 = (ioError.IOError) ("hGetContents on invalid handle " ++ show h)
863 = unsafeInterleaveIO (
866 then hClose h >> return []
867 else read_all f >>= \rest ->
868 return ((primIntToChar ci):rest)
871 hPutStr :: Handle -> String -> IO ()
873 | not (state h == HOpen && mode h /= ReadMode)
874 = (ioError.IOError) ("hPutStr on invalid handle " ++ show h)
876 = write_all (file h) s
881 = nh_write f (primCharToInt c) >>
884 hPutChar :: Handle -> Char -> IO ()
885 hPutChar h c = hPutStr h [c]
887 hPutStrLn :: Handle -> String -> IO ()
888 hPutStrLn h s = do { hPutStr h s; hPutChar h '\n' }
890 hPrint :: Show a => Handle -> a -> IO ()
891 hPrint h = hPutStrLn h . show
893 hIsOpen, hIsClosed, hIsReadable, hIsWritable :: Handle -> IO Bool
894 hIsOpen h = return (state h == HOpen)
895 hIsClosed h = return (state h == HClosed)
896 hIsReadable h = return (mode h == ReadMode)
897 hIsWritable h = return (mode h == WriteMode)
899 hIsSeekable :: Handle -> IO Bool
900 hIsSeekable = unimp "IO.hIsSeekable"
903 isAlreadyExistsError,
909 isUserError :: IOError -> Bool
911 isIllegalOperation = unimp "IO.isIllegalOperation"
912 isAlreadyExistsError = unimp "IO.isAlreadyExistsError"
913 isDoesNotExistError = unimp "IO.isDoesNotExistError"
914 isAlreadyInUseError = unimp "IO.isAlreadyInUseError"
915 isFullError = unimp "IO.isFullError"
916 isEOFError = unimp "IO.isEOFError"
917 isPermissionError = unimp "IO.isPermissionError"
918 isUserError = unimp "IO.isUserError"
921 ioeGetErrorString :: IOError -> String
922 ioeGetErrorString = unimp "ioeGetErrorString"
923 ioeGetHandle :: IOError -> Maybe Handle
924 ioeGetHandle = unimp "ioeGetHandle"
925 ioeGetFileName :: IOError -> Maybe FilePath
926 ioeGetFileName = unimp "ioeGetFileName"
928 try :: IO a -> IO (Either IOError a)
929 try p = catch (p >>= (return . Right)) (return . Left)
931 bracket :: IO a -> (a -> IO b) -> (a -> IO c) -> IO c
932 bracket before after m = do
940 -- variant of the above where middle computation doesn't want x
941 bracket_ :: IO a -> (a -> IO b) -> IO c -> IO c
942 bracket_ before after m = do
950 -- TODO: Hugs/slurbFile
951 slurpFile = unimp "slurpFile"