[project @ 2003-06-21 20:21:04 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 #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   ) where
108
109 #ifdef __GLASGOW_HASKELL__
110 import GHC.Base
111 import GHC.IOBase       -- Together these four Prelude modules define
112 import GHC.Handle       -- all the stuff exported by IO for the GHC version
113 import GHC.IO
114 import GHC.ST           ( fixST )
115 import GHC.Exception
116 import GHC.Num
117 import GHC.Read
118 import GHC.Show
119 #endif
120
121 #ifdef __HUGS__
122 import Hugs.IO
123 import Hugs.IOExts
124 #endif
125
126 #ifdef __NHC__
127 import IO
128   ( Handle ()
129   , HandlePosn ()
130   , IOMode (ReadMode,WriteMode,AppendMode,ReadWriteMode)
131   , BufferMode (NoBuffering,LineBuffering,BlockBuffering)
132   , SeekMode (AbsoluteSeek,RelativeSeek,SeekFromEnd)
133   , stdin, stdout, stderr
134   , openFile                  -- :: FilePath -> IOMode -> IO Handle
135   , hClose                    -- :: Handle -> IO ()
136   , hFileSize                 -- :: Handle -> IO Integer
137   , hIsEOF                    -- :: Handle -> IO Bool
138   , isEOF                     -- :: IO Bool
139   , hSetBuffering             -- :: Handle -> BufferMode -> IO ()
140   , hGetBuffering             -- :: Handle -> IO BufferMode
141   , hFlush                    -- :: Handle -> IO ()
142   , hGetPosn                  -- :: Handle -> IO HandlePosn
143   , hSetPosn                  -- :: HandlePosn -> IO ()
144   , hSeek                     -- :: Handle -> SeekMode -> Integer -> IO ()
145   , hWaitForInput             -- :: Handle -> Int -> IO Bool
146   , hGetChar                  -- :: Handle -> IO Char
147   , hGetLine                  -- :: Handle -> IO [Char]
148   , hLookAhead                -- :: Handle -> IO Char
149   , hGetContents              -- :: Handle -> IO [Char]
150   , hPutChar                  -- :: Handle -> Char -> IO ()
151   , hPutStr                   -- :: Handle -> [Char] -> IO ()
152   , hIsOpen, hIsClosed        -- :: Handle -> IO Bool
153   , hIsReadable, hIsWritable  -- :: Handle -> IO Bool
154   , hIsSeekable               -- :: Handle -> IO Bool
155   , isAlreadyExistsError, isDoesNotExistError  -- :: IOError -> Bool
156   , isAlreadyInUseError, isFullError
157   , isEOFError, isIllegalOperation
158   , isPermissionError, isUserError
159   , ioeGetErrorString         -- :: IOError -> String
160   , ioeGetHandle              -- :: IOError -> Maybe Handle
161   , ioeGetFileName            -- :: IOError -> Maybe FilePath
162
163   , IO ()
164   , FilePath                  -- :: String
165   , IOError
166   , ioError                   -- :: IOError -> IO a
167   , userError                 -- :: String  -> IOError
168   , catch                     -- :: IO a    -> (IOError -> IO a) -> IO a
169   )
170 import NHC.Internal (unsafePerformIO)
171 #endif
172
173 import System.IO.Error
174
175 -- -----------------------------------------------------------------------------
176 -- Standard IO
177
178 #ifndef __HUGS__
179 putChar         :: Char -> IO ()
180 putChar c       =  hPutChar stdout c
181
182 putStr          :: String -> IO ()
183 putStr s        =  hPutStr stdout s
184
185 putStrLn        :: String -> IO ()
186 putStrLn s      =  do putStr s
187                       putChar '\n'
188
189 print           :: Show a => a -> IO ()
190 print x         =  putStrLn (show x)
191
192 getChar         :: IO Char
193 getChar         =  hGetChar stdin
194
195 getLine         :: IO String
196 getLine         =  hGetLine stdin
197
198 getContents     :: IO String
199 getContents     =  hGetContents stdin
200
201 interact        ::  (String -> String) -> IO ()
202 interact f      =   do s <- getContents
203                        putStr (f s)
204
205 readFile        :: FilePath -> IO String
206 readFile name   =  openFile name ReadMode >>= hGetContents
207
208 writeFile       :: FilePath -> String -> IO ()
209 writeFile name str = do
210     hdl <- openFile name WriteMode
211     hPutStr hdl str
212     hClose hdl
213
214 appendFile      :: FilePath -> String -> IO ()
215 appendFile name str = do
216     hdl <- openFile name AppendMode
217     hPutStr hdl str
218     hClose hdl
219
220 readLn          :: Read a => IO a
221 readLn          =  do l <- getLine
222                       r <- readIO l
223                       return r
224
225 -- raises an exception instead of an error
226 readIO          :: Read a => String -> IO a
227 readIO s        =  case (do { (x,t) <- reads s ;
228                               ("","") <- lex t ;
229                               return x }) of
230                         [x]    -> return x
231                         []     -> ioError (userError "Prelude.readIO: no parse")
232                         _      -> ioError (userError "Prelude.readIO: ambiguous parse")
233 #endif  /* __HUGS__ */
234
235 hReady          :: Handle -> IO Bool
236 hReady h        =  hWaitForInput h 0
237
238 hPutStrLn       :: Handle -> String -> IO ()
239 hPutStrLn hndl str = do
240  hPutStr  hndl str
241  hPutChar hndl '\n'
242
243 hPrint          :: Show a => Handle -> a -> IO ()
244 hPrint hdl      =  hPutStrLn hdl . show
245
246 -- ---------------------------------------------------------------------------
247 -- fixIO
248
249 #ifdef __GLASGOW_HASKELL__
250 fixIO           :: (a -> IO a) -> IO a
251 fixIO m         = stToIO (fixST (ioToST . m))
252 #endif
253 #ifdef __NHC__
254 fixIO           :: (a -> IO a) -> IO a
255 fixIO f         = let x = unsafePerformIO (f x) in return x
256 #endif