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