1 {-# OPTIONS_GHC -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
5 -- Copyright : (c) The University of Glasgow 2001
6 -- License : BSD-style (see the file libraries/base/LICENSE)
8 -- Maintainer : libraries@haskell.org
10 -- Portability : portable
12 -- The standard IO library.
14 -----------------------------------------------------------------------------
19 IO, -- instance MonadFix
20 fixIO, -- :: (a -> IO a) -> IO a
22 -- * Files and handles
24 FilePath, -- :: String
26 Handle, -- abstract, instance of: Eq, Show.
28 -- ** Standard handles
30 -- | Three handles are allocated during program initialisation,
31 -- and are initially open.
33 stdin, stdout, stderr, -- :: Handle
35 -- * Opening and closing files
39 openFile, -- :: FilePath -> IOMode -> IO Handle
40 IOMode(ReadMode,WriteMode,AppendMode,ReadWriteMode),
44 hClose, -- :: Handle -> IO ()
48 -- | These functions are also exported by the "Prelude".
50 readFile, -- :: FilePath -> IO String
51 writeFile, -- :: FilePath -> String -> IO ()
52 appendFile, -- :: FilePath -> String -> IO ()
58 -- * Operations on handles
60 -- ** Determining and changing the size of a file
62 hFileSize, -- :: Handle -> IO Integer
63 #ifdef __GLASGOW_HASKELL__
64 hSetFileSize, -- :: Handle -> Integer -> IO ()
67 -- ** Detecting the end of input
69 hIsEOF, -- :: Handle -> IO Bool
72 -- ** Buffering operations
74 BufferMode(NoBuffering,LineBuffering,BlockBuffering),
75 hSetBuffering, -- :: Handle -> BufferMode -> IO ()
76 hGetBuffering, -- :: Handle -> IO BufferMode
77 hFlush, -- :: Handle -> IO ()
79 -- ** Repositioning handles
81 hGetPosn, -- :: Handle -> IO HandlePosn
82 hSetPosn, -- :: HandlePosn -> IO ()
83 HandlePosn, -- abstract, instance of: Eq, Show.
85 hSeek, -- :: Handle -> SeekMode -> Integer -> IO ()
86 SeekMode(AbsoluteSeek,RelativeSeek,SeekFromEnd),
88 hTell, -- :: Handle -> IO Integer
91 -- ** Handle properties
93 hIsOpen, hIsClosed, -- :: Handle -> IO Bool
94 hIsReadable, hIsWritable, -- :: Handle -> IO Bool
95 hIsSeekable, -- :: Handle -> IO Bool
97 -- ** Terminal operations
100 hIsTerminalDevice, -- :: Handle -> IO Bool
102 hSetEcho, -- :: Handle -> Bool -> IO ()
103 hGetEcho, -- :: Handle -> IO Bool
106 -- ** Showing handle state
108 #ifdef __GLASGOW_HASKELL__
109 hShow, -- :: Handle -> IO String
112 -- * Text input and output
116 hWaitForInput, -- :: Handle -> Int -> IO Bool
117 hReady, -- :: Handle -> IO Bool
118 hGetChar, -- :: Handle -> IO Char
119 hGetLine, -- :: Handle -> IO [Char]
120 hLookAhead, -- :: Handle -> IO Char
121 hGetContents, -- :: Handle -> IO [Char]
125 hPutChar, -- :: Handle -> Char -> IO ()
126 hPutStr, -- :: Handle -> [Char] -> IO ()
127 hPutStrLn, -- :: Handle -> [Char] -> IO ()
128 hPrint, -- :: Show a => Handle -> a -> IO ()
130 -- ** Special cases for standard input and output
132 -- | These functions are also exported by the "Prelude".
134 interact, -- :: (String -> String) -> IO ()
135 putChar, -- :: Char -> IO ()
136 putStr, -- :: String -> IO ()
137 putStrLn, -- :: String -> IO ()
138 print, -- :: Show a => a -> IO ()
139 getChar, -- :: IO Char
140 getLine, -- :: IO String
141 getContents, -- :: IO String
142 readIO, -- :: Read a => String -> IO a
143 readLn, -- :: Read a => IO a
145 -- * Binary input and output
147 openBinaryFile, -- :: FilePath -> IOMode -> IO Handle
148 hSetBinaryMode, -- :: Handle -> Bool -> IO ()
149 #if !defined(__NHC__)
150 hPutBuf, -- :: Handle -> Ptr a -> Int -> IO ()
151 hGetBuf, -- :: Handle -> Ptr a -> Int -> IO Int
153 #if !defined(__NHC__) && !defined(__HUGS__)
154 hPutBufNonBlocking, -- :: Handle -> Ptr a -> Int -> IO Int
155 hGetBufNonBlocking, -- :: Handle -> Ptr a -> Int -> IO Int
160 #ifdef __GLASGOW_HASKELL__
166 #ifdef __GLASGOW_HASKELL__
168 import GHC.IOBase -- Together these four Prelude modules define
169 import GHC.Handle -- all the stuff exported by IO for the GHC version
181 import Hugs.Prelude ( throw, Exception(NonTermination) )
182 import System.IO.Unsafe ( unsafeInterleaveIO )
189 , IOMode (ReadMode,WriteMode,AppendMode,ReadWriteMode)
190 , BufferMode (NoBuffering,LineBuffering,BlockBuffering)
191 , SeekMode (AbsoluteSeek,RelativeSeek,SeekFromEnd)
192 , stdin, stdout, stderr
193 , openFile -- :: FilePath -> IOMode -> IO Handle
194 , hClose -- :: Handle -> IO ()
195 , hFileSize -- :: Handle -> IO Integer
196 , hIsEOF -- :: Handle -> IO Bool
197 , isEOF -- :: IO Bool
198 , hSetBuffering -- :: Handle -> BufferMode -> IO ()
199 , hGetBuffering -- :: Handle -> IO BufferMode
200 , hFlush -- :: Handle -> IO ()
201 , hGetPosn -- :: Handle -> IO HandlePosn
202 , hSetPosn -- :: HandlePosn -> IO ()
203 , hSeek -- :: Handle -> SeekMode -> Integer -> IO ()
204 , hWaitForInput -- :: Handle -> Int -> IO Bool
205 , hGetChar -- :: Handle -> IO Char
206 , hGetLine -- :: Handle -> IO [Char]
207 , hLookAhead -- :: Handle -> IO Char
208 , hGetContents -- :: Handle -> IO [Char]
209 , hPutChar -- :: Handle -> Char -> IO ()
210 , hPutStr -- :: Handle -> [Char] -> IO ()
211 , hPutStrLn -- :: Handle -> [Char] -> IO ()
212 , hPrint -- :: Handle -> [Char] -> IO ()
213 , hReady -- :: Handle -> [Char] -> IO ()
214 , hIsOpen, hIsClosed -- :: Handle -> IO Bool
215 , hIsReadable, hIsWritable -- :: Handle -> IO Bool
216 , hIsSeekable -- :: Handle -> IO Bool
219 , FilePath -- :: String
221 import NHC.IOExtras (fixIO)
224 -- -----------------------------------------------------------------------------
227 #ifdef __GLASGOW_HASKELL__
228 -- | Write a character to the standard output device
229 -- (same as 'hPutChar' 'stdout').
231 putChar :: Char -> IO ()
232 putChar c = hPutChar stdout c
234 -- | Write a string to the standard output device
235 -- (same as 'hPutStr' 'stdout').
237 putStr :: String -> IO ()
238 putStr s = hPutStr stdout s
240 -- | The same as 'putStr', but adds a newline character.
242 putStrLn :: String -> IO ()
243 putStrLn s = do putStr s
246 -- | The 'print' function outputs a value of any printable type to the
247 -- standard output device.
248 -- Printable types are those that are instances of class 'Show'; 'print'
249 -- converts values to strings for output using the 'show' operation and
252 -- For example, a program to print the first 20 integers and their
253 -- powers of 2 could be written as:
255 -- > main = print ([(n, 2^n) | n <- [0..19]])
257 print :: Show a => a -> IO ()
258 print x = putStrLn (show x)
260 -- | Read a character from the standard input device
261 -- (same as 'hGetChar' 'stdin').
264 getChar = hGetChar stdin
266 -- | Read a line from the standard input device
267 -- (same as 'hGetLine' 'stdin').
270 getLine = hGetLine stdin
272 -- | The 'getContents' operation returns all user input as a single string,
273 -- which is read lazily as it is needed
274 -- (same as 'hGetContents' 'stdin').
276 getContents :: IO String
277 getContents = hGetContents stdin
279 -- | The 'interact' function takes a function of type @String->String@
280 -- as its argument. The entire input from the standard input device is
281 -- passed to this function as its argument, and the resulting string is
282 -- output on the standard output device.
284 interact :: (String -> String) -> IO ()
285 interact f = do s <- getContents
288 -- | The 'readFile' function reads a file and
289 -- returns the contents of the file as a string.
290 -- The file is read lazily, on demand, as with 'getContents'.
292 readFile :: FilePath -> IO String
293 readFile name = openFile name ReadMode >>= hGetContents
295 -- | The computation 'writeFile' @file str@ function writes the string @str@,
296 -- to the file @file@.
298 writeFile :: FilePath -> String -> IO ()
299 writeFile name str = do
300 hdl <- openFile name WriteMode
304 -- | The computation 'appendFile' @file str@ function appends the string @str@,
305 -- to the file @file@.
307 -- Note that 'writeFile' and 'appendFile' write a literal string
308 -- to a file. To write a value of any printable type, as with 'print',
309 -- use the 'show' function to convert the value to a string first.
311 -- > main = appendFile "squares" (show [(x,x*x) | x <- [0,0.1..2]])
313 appendFile :: FilePath -> String -> IO ()
314 appendFile name str = do
315 hdl <- openFile name AppendMode
319 -- | The 'readLn' function combines 'getLine' and 'readIO'.
321 readLn :: Read a => IO a
322 readLn = do l <- getLine
326 -- | The 'readIO' function is similar to 'read' except that it signals
327 -- parse failure to the 'IO' monad instead of terminating the program.
329 readIO :: Read a => String -> IO a
330 readIO s = case (do { (x,t) <- reads s ;
334 [] -> ioError (userError "Prelude.readIO: no parse")
335 _ -> ioError (userError "Prelude.readIO: ambiguous parse")
336 #endif /* __GLASGOW_HASKELL__ */
339 -- | Computation 'hReady' @hdl@ indicates whether at least one item is
340 -- available for input from handle @hdl@.
342 -- This operation may fail with:
344 -- * 'isEOFError' if the end of file has been reached.
346 hReady :: Handle -> IO Bool
347 hReady h = hWaitForInput h 0
349 -- | The same as 'hPutStr', but adds a newline character.
351 hPutStrLn :: Handle -> String -> IO ()
352 hPutStrLn hndl str = do
356 -- | Computation 'hPrint' @hdl t@ writes the string representation of @t@
357 -- given by the 'shows' function to the file or channel managed by @hdl@
358 -- and appends a newline.
360 -- This operation may fail with:
362 -- * 'isFullError' if the device is full; or
364 -- * 'isPermissionError' if another system resource limit would be exceeded.
366 hPrint :: Show a => Handle -> a -> IO ()
367 hPrint hdl = hPutStrLn hdl . show
368 #endif /* !__NHC__ */
370 -- ---------------------------------------------------------------------------
373 #if defined(__GLASGOW_HASKELL__) || defined(__HUGS__)
374 fixIO :: (a -> IO a) -> IO a
376 ref <- newIORef (throw NonTermination)
377 ans <- unsafeInterleaveIO (readIORef ref)
379 writeIORef ref result
382 -- NOTE: we do our own explicit black holing here, because GHC's lazy
383 -- blackholing isn't enough. In an infinite loop, GHC may run the IO
384 -- computation a few times before it notices the loop, which is wrong.
388 -- Assume a unix platform, where text and binary I/O are identical.
389 openBinaryFile = openFile
390 hSetBinaryMode _ _ = return ()
394 -- Implementations should enforce as far as possible, at least locally to the
395 -- Haskell process, multiple-reader single-writer locking on files.
396 -- That is, /there may either be many handles on the same file which manage
397 -- input, or just one handle on the file which manages output/. If any
398 -- open or semi-closed handle is managing a file for output, no new
399 -- handle can be allocated for that file. If any open or semi-closed
400 -- handle is managing a file for input, new handles can only be allocated
401 -- if they do not manage output. Whether two files are the same is
402 -- implementation-dependent, but they should normally be the same if they
403 -- have the same absolute path name and neither has been renamed, for
406 -- /Warning/: the 'readFile' operation holds a semi-closed handle on
407 -- the file until the entire contents of the file have been consumed.
408 -- It follows that an attempt to write to a file (using 'writeFile', for
409 -- example) that was earlier opened by 'readFile' will usually result in
410 -- failure with 'isAlreadyInUseError'.