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