1 {-# OPTIONS -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 the size of a file
62 hFileSize, -- :: Handle -> IO Integer
64 -- ** Detecting the end of input
66 hIsEOF, -- :: Handle -> IO Bool
69 -- ** Buffering operations
71 BufferMode(NoBuffering,LineBuffering,BlockBuffering),
72 hSetBuffering, -- :: Handle -> BufferMode -> IO ()
73 hGetBuffering, -- :: Handle -> IO BufferMode
74 hFlush, -- :: Handle -> IO ()
76 -- ** Repositioning handles
78 hGetPosn, -- :: Handle -> IO HandlePosn
79 hSetPosn, -- :: HandlePosn -> IO ()
80 HandlePosn, -- abstract, instance of: Eq, Show.
82 hSeek, -- :: Handle -> SeekMode -> Integer -> IO ()
83 SeekMode(AbsoluteSeek,RelativeSeek,SeekFromEnd),
85 hTell, -- :: Handle -> IO Integer
88 -- ** Handle properties
90 hIsOpen, hIsClosed, -- :: Handle -> IO Bool
91 hIsReadable, hIsWritable, -- :: Handle -> IO Bool
92 hIsSeekable, -- :: Handle -> IO Bool
94 -- ** Terminal operations
97 hIsTerminalDevice, -- :: Handle -> IO Bool
99 hSetEcho, -- :: Handle -> Bool -> IO ()
100 hGetEcho, -- :: Handle -> IO Bool
103 -- ** Showing handle state
105 #ifdef __GLASGOW_HASKELL__
106 hShow, -- :: Handle -> IO String
109 -- * Text input and output
113 hWaitForInput, -- :: Handle -> Int -> IO Bool
114 hReady, -- :: Handle -> IO Bool
115 hGetChar, -- :: Handle -> IO Char
116 hGetLine, -- :: Handle -> IO [Char]
117 hLookAhead, -- :: Handle -> IO Char
118 hGetContents, -- :: Handle -> IO [Char]
122 hPutChar, -- :: Handle -> Char -> IO ()
123 hPutStr, -- :: Handle -> [Char] -> IO ()
124 hPutStrLn, -- :: Handle -> [Char] -> IO ()
125 hPrint, -- :: Show a => Handle -> a -> IO ()
127 -- ** Special cases for standard input and output
129 -- | These functions are also exported by the "Prelude".
131 interact, -- :: (String -> String) -> IO ()
132 putChar, -- :: Char -> IO ()
133 putStr, -- :: String -> IO ()
134 putStrLn, -- :: String -> IO ()
135 print, -- :: Show a => a -> IO ()
136 getChar, -- :: IO Char
137 getLine, -- :: IO String
138 getContents, -- :: IO String
139 readIO, -- :: Read a => String -> IO a
140 readLn, -- :: Read a => IO a
142 -- * Binary input and output
144 #if !defined(__NHC__)
145 openBinaryFile, -- :: FilePath -> IOMode -> IO Handle
146 hSetBinaryMode, -- :: Handle -> Bool -> IO ()
147 hPutBuf, -- :: Handle -> Ptr a -> Int -> IO ()
148 hGetBuf, -- :: Handle -> Ptr a -> Int -> IO Int
150 #if !defined(__NHC__) && !defined(__HUGS__)
151 hPutBufNonBlocking, -- :: Handle -> Ptr a -> Int -> IO Int
152 hGetBufNonBlocking, -- :: Handle -> Ptr a -> Int -> IO Int
155 module System.IO.Error,
158 #ifdef __GLASGOW_HASKELL__
160 import GHC.IOBase -- Together these four Prelude modules define
161 import GHC.Handle -- all the stuff exported by IO for the GHC version
163 import GHC.ST ( fixST )
174 import Hugs.Prelude ( throw, Exception(NonTermination) )
175 import System.IO.Unsafe ( unsafeInterleaveIO )
182 , IOMode (ReadMode,WriteMode,AppendMode,ReadWriteMode)
183 , BufferMode (NoBuffering,LineBuffering,BlockBuffering)
184 , SeekMode (AbsoluteSeek,RelativeSeek,SeekFromEnd)
185 , stdin, stdout, stderr
186 , openFile -- :: FilePath -> IOMode -> IO Handle
187 , hClose -- :: Handle -> IO ()
188 , hFileSize -- :: Handle -> IO Integer
189 , hIsEOF -- :: Handle -> IO Bool
190 , isEOF -- :: IO Bool
191 , hSetBuffering -- :: Handle -> BufferMode -> IO ()
192 , hGetBuffering -- :: Handle -> IO BufferMode
193 , hFlush -- :: Handle -> IO ()
194 , hGetPosn -- :: Handle -> IO HandlePosn
195 , hSetPosn -- :: HandlePosn -> IO ()
196 , hSeek -- :: Handle -> SeekMode -> Integer -> IO ()
197 , hWaitForInput -- :: Handle -> Int -> IO Bool
198 , hGetChar -- :: Handle -> IO Char
199 , hGetLine -- :: Handle -> IO [Char]
200 , hLookAhead -- :: Handle -> IO Char
201 , hGetContents -- :: Handle -> IO [Char]
202 , hPutChar -- :: Handle -> Char -> IO ()
203 , hPutStr -- :: Handle -> [Char] -> IO ()
204 , hIsOpen, hIsClosed -- :: Handle -> IO Bool
205 , hIsReadable, hIsWritable -- :: Handle -> IO Bool
206 , hIsSeekable -- :: Handle -> IO Bool
209 , FilePath -- :: String
211 import NHC.IOExtras (fixIO)
214 import System.IO.Error (
215 isAlreadyExistsError, isDoesNotExistError, -- :: IOError -> Bool
216 isAlreadyInUseError, isFullError,
217 isEOFError, isIllegalOperation,
218 isPermissionError, isUserError,
220 ioeGetErrorString, -- :: IOError -> String
221 ioeGetHandle, -- :: IOError -> Maybe Handle
222 ioeGetFileName, -- :: IOError -> Maybe FilePath
224 try, -- :: IO a -> IO (Either IOError a)
226 -- re-exports of Prelude names
228 ioError, -- :: IOError -> IO a
229 userError, -- :: String -> IOError
230 catch -- :: IO a -> (IOError -> IO a) -> IO a
233 -- -----------------------------------------------------------------------------
237 -- | Write a character to the standard output device
238 -- (same as 'hPutChar' 'stdout').
240 putChar :: Char -> IO ()
241 putChar c = hPutChar stdout c
243 -- | Write a string to the standard output device
244 -- (same as 'hPutStr' 'stdout').
246 putStr :: String -> IO ()
247 putStr s = hPutStr stdout s
249 -- | The same as 'putStr', but adds a newline character.
251 putStrLn :: String -> IO ()
252 putStrLn s = do putStr s
255 -- | The 'print' function outputs a value of any printable type to the
256 -- standard output device.
257 -- Printable types are those that are instances of class 'Show'; 'print'
258 -- converts values to strings for output using the 'show' operation and
261 -- For example, a program to print the first 20 integers and their
262 -- powers of 2 could be written as:
264 -- > main = print ([(n, 2^n) | n <- [0..19]])
266 print :: Show a => a -> IO ()
267 print x = putStrLn (show x)
269 -- | Read a character from the standard input device
270 -- (same as 'hGetChar' 'stdin').
273 getChar = hGetChar stdin
275 -- | Read a line from the standard input device
276 -- (same as 'hGetLine' 'stdin').
279 getLine = hGetLine stdin
281 -- | The 'getContents' operation returns all user input as a single string,
282 -- which is read lazily as it is needed
283 -- (same as 'hGetContents' 'stdin').
285 getContents :: IO String
286 getContents = hGetContents stdin
288 -- | The 'interact' function takes a function of type @String->String@
289 -- as its argument. The entire input from the standard input device is
290 -- passed to this function as its argument, and the resulting string is
291 -- output on the standard output device.
293 interact :: (String -> String) -> IO ()
294 interact f = do s <- getContents
297 -- | The 'readFile' function reads a file and
298 -- returns the contents of the file as a string.
299 -- The file is read lazily, on demand, as with 'getContents'.
301 readFile :: FilePath -> IO String
302 readFile name = openFile name ReadMode >>= hGetContents
304 -- | The computation 'writeFile' @file str@ function writes the string @str@,
305 -- to the file @file@.
307 writeFile :: FilePath -> String -> IO ()
308 writeFile name str = do
309 hdl <- openFile name WriteMode
313 -- | The computation 'appendFile' @file str@ function appends the string @str@,
314 -- to the file @file@.
316 -- Note that 'writeFile' and 'appendFile' write a literal string
317 -- to a file. To write a value of any printable type, as with 'print',
318 -- use the 'show' function to convert the value to a string first.
320 -- > main = appendFile "squares" (show [(x,x*x) | x <- [0,0.1..2]])
322 appendFile :: FilePath -> String -> IO ()
323 appendFile name str = do
324 hdl <- openFile name AppendMode
328 -- | The 'readLn' function combines 'getLine' and 'readIO'.
330 readLn :: Read a => IO a
331 readLn = do l <- getLine
335 -- | The 'readIO' function is similar to 'read' except that it signals
336 -- parse failure to the 'IO' monad instead of terminating the program.
338 readIO :: Read a => String -> IO a
339 readIO s = case (do { (x,t) <- reads s ;
343 [] -> ioError (userError "Prelude.readIO: no parse")
344 _ -> ioError (userError "Prelude.readIO: ambiguous parse")
345 #endif /* __HUGS__ */
347 -- | Computation 'hReady' @hdl@ indicates whether at least one item is
348 -- available for input from handle @hdl@.
350 -- This operation may fail with:
352 -- * 'isEOFError' if the end of file has been reached.
354 hReady :: Handle -> IO Bool
355 hReady h = hWaitForInput h 0
357 -- | The same as 'hPutStr', but adds a newline character.
359 hPutStrLn :: Handle -> String -> IO ()
360 hPutStrLn hndl str = do
364 -- | Computation 'hPrint' @hdl t@ writes the string representation of @t@
365 -- given by the 'shows' function to the file or channel managed by @hdl@
366 -- and appends a newline.
368 -- This operation may fail with:
370 -- * 'isFullError' if the device is full; or
372 -- * 'isPermissionError' if another system resource limit would be exceeded.
374 hPrint :: Show a => Handle -> a -> IO ()
375 hPrint hdl = hPutStrLn hdl . show
377 -- ---------------------------------------------------------------------------
380 #if defined(__GLASGOW_HASKELL__) || defined(__HUGS__)
381 fixIO :: (a -> IO a) -> IO a
383 ref <- newIORef (throw NonTermination)
384 ans <- unsafeInterleaveIO (readIORef ref)
386 writeIORef ref result
389 -- NOTE: we do our own explicit black holing here, because GHC's lazy
390 -- blackholing isn't enough. In an infinite loop, GHC may run the IO
391 -- computation a few times before it notices the loop, which is wrong.
395 -- Implementations should enforce as far as possible, at least locally to the
396 -- Haskell process, multiple-reader single-writer locking on files.
397 -- That is, /there may either be many handles on the same file which manage
398 -- input, or just one handle on the file which manages output/. If any
399 -- open or semi-closed handle is managing a file for output, no new
400 -- handle can be allocated for that file. If any open or semi-closed
401 -- handle is managing a file for input, new handles can only be allocated
402 -- if they do not manage output. Whether two files are the same is
403 -- implementation-dependent, but they should normally be the same if they
404 -- have the same absolute path name and neither has been renamed, for
407 -- /Warning/: the 'readFile' operation holds a semi-closed handle on
408 -- the file until the entire contents of the file have been consumed.
409 -- It follows that an attempt to write to a file (using 'writeFile', for
410 -- example) that was earlier opened by 'readFile' will usually result in
411 -- failure with 'isAlreadyInUseError'.