bc1d254c9528d9ee151bca74f471e0c574dafa0d
[ghc-base.git] / System / IO.hs
1 {-# OPTIONS -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module      :  System.IO
5 -- Copyright   :  (c) The University of Glasgow 2001
6 -- License     :  BSD-style (see the file libraries/base/LICENSE)
7 -- 
8 -- Maintainer  :  libraries@haskell.org
9 -- Stability   :  provisional
10 -- Portability :  portable
11 --
12 -- The standard IO library.
13 --
14 -----------------------------------------------------------------------------
15
16 module System.IO (
17     Handle,             -- abstract, instance of: Eq, Show.
18     HandlePosn(..),     -- abstract, instance of: Eq, Show.
19
20     IOMode(ReadMode,WriteMode,AppendMode,ReadWriteMode),
21     BufferMode(NoBuffering,LineBuffering,BlockBuffering),
22     SeekMode(AbsoluteSeek,RelativeSeek,SeekFromEnd),
23
24     stdin, stdout, stderr,   -- :: Handle
25
26     openFile,                  -- :: FilePath -> IOMode -> IO Handle
27     openBinaryFile,            -- :: FilePath -> IOMode -> IO Handle
28     hClose,                    -- :: Handle -> IO ()
29     hFileSize,                 -- :: Handle -> IO Integer
30     hIsEOF,                    -- :: Handle -> IO Bool
31     isEOF,                     -- :: IO Bool
32
33     hSetBuffering,             -- :: Handle -> BufferMode -> IO ()
34     hGetBuffering,             -- :: Handle -> IO BufferMode
35 #ifndef __HUGS__
36     hSetBinaryMode,            -- :: Handle -> Bool -> IO ()
37 #endif
38     hFlush,                    -- :: Handle -> IO ()
39     hGetPosn,                  -- :: Handle -> IO HandlePosn
40     hSetPosn,                  -- :: HandlePosn -> IO ()
41     hSeek,                     -- :: Handle -> SeekMode -> Integer -> IO ()
42 #if !defined(__NHC__)
43     hTell,                     -- :: Handle -> IO Integer
44 #endif
45     hWaitForInput,             -- :: Handle -> Int -> IO Bool
46     hReady,                    -- :: Handle -> IO Bool
47     hGetChar,                  -- :: Handle -> IO Char
48     hGetLine,                  -- :: Handle -> IO [Char]
49     hLookAhead,                -- :: Handle -> IO Char
50     hGetContents,              -- :: Handle -> IO [Char]
51     hPutChar,                  -- :: Handle -> Char -> IO ()
52     hPutStr,                   -- :: Handle -> [Char] -> IO ()
53     hPutStrLn,                 -- :: Handle -> [Char] -> IO ()
54     hPrint,                    -- :: Show a => Handle -> a -> IO ()
55     hIsOpen, hIsClosed,        -- :: Handle -> IO Bool
56     hIsReadable, hIsWritable,  -- :: Handle -> IO Bool
57     hIsSeekable,               -- :: Handle -> IO Bool
58
59     isAlreadyExistsError, isDoesNotExistError,  -- :: IOError -> Bool
60     isAlreadyInUseError, isFullError, 
61     isEOFError, isIllegalOperation, 
62     isPermissionError, isUserError, 
63
64     ioeGetErrorString,         -- :: IOError -> String
65     ioeGetHandle,              -- :: IOError -> Maybe Handle
66     ioeGetFileName,            -- :: IOError -> Maybe FilePath
67
68     try,                       -- :: IO a -> IO (Either IOError a)
69
70     -- re-exports of Prelude names
71     IO,                        -- instance MonadFix
72     FilePath,                  -- :: String
73     IOError,
74     ioError,                   -- :: IOError -> IO a
75     userError,                 -- :: String  -> IOError
76     catch,                     -- :: IO a    -> (IOError -> IO a) -> IO a
77     interact,                  -- :: (String -> String) -> IO ()
78
79     putChar,                   -- :: Char   -> IO ()
80     putStr,                    -- :: String -> IO () 
81     putStrLn,                  -- :: String -> IO ()
82     print,                     -- :: Show a => a -> IO ()
83     getChar,                   -- :: IO Char
84     getLine,                   -- :: IO String
85     getContents,               -- :: IO String
86     readFile,                  -- :: FilePath -> IO String
87     writeFile,                 -- :: FilePath -> String -> IO ()
88     appendFile,                -- :: FilePath -> String -> IO ()
89     readIO,                    -- :: Read a => String -> IO a
90     readLn,                    -- :: Read a => IO a
91
92 #if !defined(__HUGS__) && !defined(__NHC__)
93     hPutBuf,                   -- :: Handle -> Ptr a -> Int -> IO ()
94     hGetBuf,                   -- :: Handle -> Ptr a -> Int -> IO Int
95 #endif
96  
97     fixIO,                     -- :: (a -> IO a) -> IO a
98
99 #if !defined(__HUGS__) && !defined(__NHC__)
100     hSetEcho,                   -- :: Handle -> Bool -> IO ()
101     hGetEcho,                   -- :: Handle -> IO Bool
102
103     hIsTerminalDevice,          -- :: Handle -> IO Bool
104 #endif
105   ) where
106
107 #ifdef __GLASGOW_HASKELL__
108 import GHC.Base
109 import GHC.IOBase       -- Together these four Prelude modules define
110 import GHC.Handle       -- all the stuff exported by IO for the GHC version
111 import GHC.IO
112 import GHC.ST           ( fixST )
113 import GHC.Exception
114 import GHC.Num
115 import GHC.Read
116 import GHC.Show
117 #endif
118
119 #ifdef __HUGS__
120 import Hugs.IO
121 import Hugs.IOExts
122 #endif
123
124 #ifdef __NHC__
125 import IO
126   ( Handle ()
127   , HandlePosn ()
128   , IOMode (ReadMode,WriteMode,AppendMode,ReadWriteMode)
129   , BufferMode (NoBuffering,LineBuffering,BlockBuffering)
130   , SeekMode (AbsoluteSeek,RelativeSeek,SeekFromEnd)
131   , stdin, stdout, stderr
132   , openFile                  -- :: FilePath -> IOMode -> IO Handle
133   , hClose                    -- :: Handle -> IO ()
134   , hFileSize                 -- :: Handle -> IO Integer
135   , hIsEOF                    -- :: Handle -> IO Bool
136   , isEOF                     -- :: IO Bool
137   , hSetBuffering             -- :: Handle -> BufferMode -> IO ()
138   , hGetBuffering             -- :: Handle -> IO BufferMode
139   , hFlush                    -- :: Handle -> IO ()
140   , hGetPosn                  -- :: Handle -> IO HandlePosn
141   , hSetPosn                  -- :: HandlePosn -> IO ()
142   , hSeek                     -- :: Handle -> SeekMode -> Integer -> IO ()
143   , hWaitForInput             -- :: Handle -> Int -> IO Bool
144   , hGetChar                  -- :: Handle -> IO Char
145   , hGetLine                  -- :: Handle -> IO [Char]
146   , hLookAhead                -- :: Handle -> IO Char
147   , hGetContents              -- :: Handle -> IO [Char]
148   , hPutChar                  -- :: Handle -> Char -> IO ()
149   , hPutStr                   -- :: Handle -> [Char] -> IO ()
150   , hIsOpen, hIsClosed        -- :: Handle -> IO Bool
151   , hIsReadable, hIsWritable  -- :: Handle -> IO Bool
152   , hIsSeekable               -- :: Handle -> IO Bool
153   , isAlreadyExistsError, isDoesNotExistError  -- :: IOError -> Bool
154   , isAlreadyInUseError, isFullError
155   , isEOFError, isIllegalOperation
156   , isPermissionError, isUserError
157   , ioeGetErrorString         -- :: IOError -> String
158   , ioeGetHandle              -- :: IOError -> Maybe Handle
159   , ioeGetFileName            -- :: IOError -> Maybe FilePath
160
161   , IO ()
162   , FilePath                  -- :: String
163   , IOError
164   , ioError                   -- :: IOError -> IO a
165   , userError                 -- :: String  -> IOError
166   , catch                     -- :: IO a    -> (IOError -> IO a) -> IO a
167   )
168 import NHC.Internal (unsafePerformIO)
169 #endif
170
171 import System.IO.Error
172
173 -- -----------------------------------------------------------------------------
174 -- Standard IO
175
176 #ifndef __HUGS__
177 putChar         :: Char -> IO ()
178 putChar c       =  hPutChar stdout c
179
180 putStr          :: String -> IO ()
181 putStr s        =  hPutStr stdout s
182
183 putStrLn        :: String -> IO ()
184 putStrLn s      =  do putStr s
185                       putChar '\n'
186
187 print           :: Show a => a -> IO ()
188 print x         =  putStrLn (show x)
189
190 getChar         :: IO Char
191 getChar         =  hGetChar stdin
192
193 getLine         :: IO String
194 getLine         =  hGetLine stdin
195
196 getContents     :: IO String
197 getContents     =  hGetContents stdin
198
199 interact        ::  (String -> String) -> IO ()
200 interact f      =   do s <- getContents
201                        putStr (f s)
202
203 readFile        :: FilePath -> IO String
204 readFile name   =  openFile name ReadMode >>= hGetContents
205
206 writeFile       :: FilePath -> String -> IO ()
207 writeFile name str = do
208     hdl <- openFile name WriteMode
209     hPutStr hdl str
210     hClose hdl
211
212 appendFile      :: FilePath -> String -> IO ()
213 appendFile name str = do
214     hdl <- openFile name AppendMode
215     hPutStr hdl str
216     hClose hdl
217
218 readLn          :: Read a => IO a
219 readLn          =  do l <- getLine
220                       r <- readIO l
221                       return r
222
223 -- raises an exception instead of an error
224 readIO          :: Read a => String -> IO a
225 readIO s        =  case (do { (x,t) <- reads s ;
226                               ("","") <- lex t ;
227                               return x }) of
228                         [x]    -> return x
229                         []     -> ioError (userError "Prelude.readIO: no parse")
230                         _      -> ioError (userError "Prelude.readIO: ambiguous parse")
231 #endif  /* __HUGS__ */
232
233 hReady          :: Handle -> IO Bool
234 hReady h        =  hWaitForInput h 0
235
236 hPutStrLn       :: Handle -> String -> IO ()
237 hPutStrLn hndl str = do
238  hPutStr  hndl str
239  hPutChar hndl '\n'
240
241 hPrint          :: Show a => Handle -> a -> IO ()
242 hPrint hdl      =  hPutStrLn hdl . show
243
244 -- ---------------------------------------------------------------------------
245 -- fixIO
246
247 #ifdef __GLASGOW_HASKELL__
248 fixIO           :: (a -> IO a) -> IO a
249 fixIO m         = stToIO (fixST (ioToST . m))
250 #endif
251 #ifdef __NHC__
252 fixIO           :: (a -> IO a) -> IO a
253 fixIO f         = let x = unsafePerformIO (f x) in return x
254 #endif