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