[project @ 2002-10-09 17:24:12 by malcolm]
[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     hClose,                    -- :: Handle -> IO ()
28     hFileSize,                 -- :: Handle -> IO Integer
29     hIsEOF,                    -- :: Handle -> IO Bool
30     isEOF,                     -- :: IO Bool
31
32     hSetBuffering,             -- :: Handle -> BufferMode -> IO ()
33     hGetBuffering,             -- :: Handle -> IO BufferMode
34     hFlush,                    -- :: Handle -> IO ()
35     hGetPosn,                  -- :: Handle -> IO HandlePosn
36     hSetPosn,                  -- :: HandlePosn -> IO ()
37     hSeek,                     -- :: Handle -> SeekMode -> Integer -> IO ()
38 #if !defined(__NHC__)
39     hTell,                     -- :: Handle -> IO Integer
40 #endif
41     hWaitForInput,             -- :: Handle -> Int -> IO Bool
42     hReady,                    -- :: Handle -> IO Bool
43     hGetChar,                  -- :: Handle -> IO Char
44     hGetLine,                  -- :: Handle -> IO [Char]
45     hLookAhead,                -- :: Handle -> IO Char
46     hGetContents,              -- :: Handle -> IO [Char]
47     hPutChar,                  -- :: Handle -> Char -> IO ()
48     hPutStr,                   -- :: Handle -> [Char] -> IO ()
49     hPutStrLn,                 -- :: Handle -> [Char] -> IO ()
50     hPrint,                    -- :: Show a => Handle -> a -> IO ()
51     hIsOpen, hIsClosed,        -- :: Handle -> IO Bool
52     hIsReadable, hIsWritable,  -- :: Handle -> IO Bool
53     hIsSeekable,               -- :: Handle -> IO Bool
54
55     isAlreadyExistsError, isDoesNotExistError,  -- :: IOError -> Bool
56     isAlreadyInUseError, isFullError, 
57     isEOFError, isIllegalOperation, 
58     isPermissionError, isUserError, 
59
60     ioeGetErrorString,         -- :: IOError -> String
61     ioeGetHandle,              -- :: IOError -> Maybe Handle
62     ioeGetFileName,            -- :: IOError -> Maybe FilePath
63
64     try,                       -- :: IO a -> IO (Either IOError a)
65     bracket,                   -- :: IO a -> (a -> IO b) -> (a -> IO c) -> IO c
66     bracket_,                  -- :: IO a -> (a -> IO b) -> IO c -> IO c
67
68     -- Non-standard extension (but will hopefully become standard with 1.5) is
69     -- to export the Prelude io functions via IO (in addition to exporting them
70     -- from the prelude...for now.) 
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   , try                       -- :: IO a -> IO (Either IOError a)
161   , bracket                   -- :: IO a -> (a -> IO b) -> (a -> IO c) -> IO c
162   , bracket_                  -- :: IO a -> (a -> IO b) -> IO c -> IO c
163
164   , IO ()
165   , FilePath                  -- :: String
166   , IOError
167   , ioError                   -- :: IOError -> IO a
168   , userError                 -- :: String  -> IOError
169   , catch                     -- :: IO a    -> (IOError -> IO a) -> IO a
170   )
171 import NHC.Internal (unsafePerformIO)
172 #endif
173
174 import System.IO.Error
175
176 -- -----------------------------------------------------------------------------
177 -- Standard IO
178
179 #ifndef __HUGS__
180 putChar         :: Char -> IO ()
181 putChar c       =  hPutChar stdout c
182
183 putStr          :: String -> IO ()
184 putStr s        =  hPutStr stdout s
185
186 putStrLn        :: String -> IO ()
187 putStrLn s      =  do putStr s
188                       putChar '\n'
189
190 print           :: Show a => a -> IO ()
191 print x         =  putStrLn (show x)
192
193 getChar         :: IO Char
194 getChar         =  hGetChar stdin
195
196 getLine         :: IO String
197 getLine         =  hGetLine stdin
198
199 getContents     :: IO String
200 getContents     =  hGetContents stdin
201
202 interact        ::  (String -> String) -> IO ()
203 interact f      =   do s <- getContents
204                        putStr (f s)
205
206 readFile        :: FilePath -> IO String
207 readFile name   =  openFile name ReadMode >>= hGetContents
208
209 writeFile       :: FilePath -> String -> IO ()
210 writeFile name str = do
211     hdl <- openFile name WriteMode
212     hPutStr hdl str
213     hClose hdl
214
215 appendFile      :: FilePath -> String -> IO ()
216 appendFile name str = do
217     hdl <- openFile name AppendMode
218     hPutStr hdl str
219     hClose hdl
220
221 readLn          :: Read a => IO a
222 readLn          =  do l <- getLine
223                       r <- readIO l
224                       return r
225
226 -- raises an exception instead of an error
227 readIO          :: Read a => String -> IO a
228 readIO s        =  case (do { (x,t) <- reads s ;
229                               ("","") <- lex t ;
230                               return x }) of
231                         [x]    -> return x
232                         []     -> ioError (userError "Prelude.readIO: no parse")
233                         _      -> ioError (userError "Prelude.readIO: ambiguous parse")
234 #endif  /* __HUGS__ */
235
236 hReady          :: Handle -> IO Bool
237 hReady h        =  hWaitForInput h 0
238
239 hPutStrLn       :: Handle -> String -> IO ()
240 hPutStrLn hndl str = do
241  hPutStr  hndl str
242  hPutChar hndl '\n'
243
244 hPrint          :: Show a => Handle -> a -> IO ()
245 hPrint hdl      =  hPutStrLn hdl . show
246
247 -- ---------------------------------------------------------------------------
248 -- fixIO
249
250 #ifdef __GLASGOW_HASKELL__
251 fixIO           :: (a -> IO a) -> IO a
252 fixIO m         = stToIO (fixST (ioToST . m))
253 #endif
254 #ifdef __NHC__
255 fixIO           :: (a -> IO a) -> IO a
256 fixIO f         = let x = unsafePerformIO (f x) in return x
257 #endif