[project @ 2002-04-26 13:34:05 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/core/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     hTell,                     -- :: Handle -> IO Integer
39     hWaitForInput,             -- :: Handle -> Int -> IO Bool
40     hReady,                    -- :: Handle -> IO Bool
41     hGetChar,                  -- :: Handle -> IO Char
42     hGetLine,                  -- :: Handle -> IO [Char]
43     hLookAhead,                -- :: Handle -> IO Char
44     hGetContents,              -- :: Handle -> IO [Char]
45     hPutChar,                  -- :: Handle -> Char -> IO ()
46     hPutStr,                   -- :: Handle -> [Char] -> IO ()
47     hPutStrLn,                 -- :: Handle -> [Char] -> IO ()
48     hPrint,                    -- :: Show a => Handle -> a -> IO ()
49     hIsOpen, hIsClosed,        -- :: Handle -> IO Bool
50     hIsReadable, hIsWritable,  -- :: Handle -> IO Bool
51     hIsSeekable,               -- :: Handle -> IO Bool
52
53     isAlreadyExistsError, isDoesNotExistError,  -- :: IOError -> Bool
54     isAlreadyInUseError, isFullError, 
55     isEOFError, isIllegalOperation, 
56     isPermissionError, isUserError, 
57
58     ioeGetErrorString,         -- :: IOError -> String
59     ioeGetHandle,              -- :: IOError -> Maybe Handle
60     ioeGetFileName,            -- :: IOError -> Maybe FilePath
61
62     try,                       -- :: IO a -> IO (Either IOError a)
63     bracket,                   -- :: IO a -> (a -> IO b) -> (a -> IO c) -> IO c
64     bracket_,                  -- :: IO a -> (a -> IO b) -> IO c -> IO c
65
66     -- Non-standard extension (but will hopefully become standard with 1.5) is
67     -- to export the Prelude io functions via IO (in addition to exporting them
68     -- from the prelude...for now.) 
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     hPutBuf,                   -- :: Handle -> Ptr a -> Int -> IO ()
91     hGetBuf,                   -- :: Handle -> Ptr a -> Int -> IO Int
92  
93     fixIO,                     -- :: (a -> IO a) -> IO a
94
95     hSetEcho,                   -- :: Handle -> Bool -> IO ()
96     hGetEcho,                   -- :: Handle -> IO Bool
97
98     hIsTerminalDevice,          -- :: Handle -> IO Bool
99   ) where
100
101 #ifdef __GLASGOW_HASKELL__
102 import GHC.Base
103 import GHC.IOBase       -- Together these four Prelude modules define
104 import GHC.Handle       -- all the stuff exported by IO for the GHC version
105 import GHC.IO
106 import GHC.ST           ( fixST )
107 import GHC.Exception
108 import GHC.Num
109 import GHC.Read
110 import GHC.Show
111 #endif
112
113 import System.IO.Error
114
115 -- -----------------------------------------------------------------------------
116 -- Standard IO
117
118 putChar         :: Char -> IO ()
119 putChar c       =  hPutChar stdout c
120
121 putStr          :: String -> IO ()
122 putStr s        =  hPutStr stdout s
123
124 putStrLn        :: String -> IO ()
125 putStrLn s      =  do putStr s
126                       putChar '\n'
127
128 print           :: Show a => a -> IO ()
129 print x         =  putStrLn (show x)
130
131 getChar         :: IO Char
132 getChar         =  hGetChar stdin
133
134 getLine         :: IO String
135 getLine         =  hGetLine stdin
136
137 getContents     :: IO String
138 getContents     =  hGetContents stdin
139
140 interact        ::  (String -> String) -> IO ()
141 interact f      =   do s <- getContents
142                        putStr (f s)
143
144 readFile        :: FilePath -> IO String
145 readFile name   =  openFile name ReadMode >>= hGetContents
146
147 writeFile       :: FilePath -> String -> IO ()
148 writeFile name str = do
149     hdl <- openFile name WriteMode
150     hPutStr hdl str
151     hClose hdl
152
153 appendFile      :: FilePath -> String -> IO ()
154 appendFile name str = do
155     hdl <- openFile name AppendMode
156     hPutStr hdl str
157     hClose hdl
158
159 readLn          :: Read a => IO a
160 readLn          =  do l <- getLine
161                       r <- readIO l
162                       return r
163
164 -- raises an exception instead of an error
165 readIO          :: Read a => String -> IO a
166 readIO s        =  case (do { (x,t) <- reads s ;
167                               ("","") <- lex t ;
168                               return x }) of
169                         [x]    -> return x
170                         []     -> ioError (userError "Prelude.readIO: no parse")
171                         _      -> ioError (userError "Prelude.readIO: ambiguous parse")
172
173 hReady          :: Handle -> IO Bool
174 hReady h        =  hWaitForInput h 0
175
176 hPutStrLn       :: Handle -> String -> IO ()
177 hPutStrLn hndl str = do
178  hPutStr  hndl str
179  hPutChar hndl '\n'
180
181 hPrint          :: Show a => Handle -> a -> IO ()
182 hPrint hdl      =  hPutStrLn hdl . show
183
184 -- ---------------------------------------------------------------------------
185 -- fixIO
186
187 #ifdef __GLASGOW_HASKELL__
188 fixIO           :: (a -> IO a) -> IO a
189 fixIO m         = stToIO (fixST (ioToST . m))
190 #endif