[project @ 2002-02-12 10:51:06 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 -- $Id: IO.hs,v 1.4 2002/02/12 10:51:06 simonmar Exp $
13 --
14 -- The standard IO library.
15 --
16 -----------------------------------------------------------------------------
17
18 module System.IO (
19     Handle,             -- abstract, instance of: Eq, Show.
20     HandlePosn(..),     -- abstract, instance of: Eq, Show.
21
22     IOMode(ReadMode,WriteMode,AppendMode,ReadWriteMode),
23     BufferMode(NoBuffering,LineBuffering,BlockBuffering),
24     SeekMode(AbsoluteSeek,RelativeSeek,SeekFromEnd),
25
26     stdin, stdout, stderr,   -- :: Handle
27
28     openFile,                  -- :: FilePath -> IOMode -> IO Handle
29     hClose,                    -- :: Handle -> IO ()
30     hFileSize,                 -- :: Handle -> IO Integer
31     hIsEOF,                    -- :: Handle -> IO Bool
32     isEOF,                     -- :: IO Bool
33
34     hSetBuffering,             -- :: Handle -> BufferMode -> IO ()
35     hGetBuffering,             -- :: Handle -> IO BufferMode
36     hFlush,                    -- :: Handle -> IO ()
37     hGetPosn,                  -- :: Handle -> IO HandlePosn
38     hSetPosn,                  -- :: HandlePosn -> IO ()
39     hSeek,                     -- :: Handle -> SeekMode -> Integer -> IO ()
40     hWaitForInput,             -- :: Handle -> Int -> IO Bool
41     hReady,                    -- :: Handle -> IO Bool
42     hGetChar,                  -- :: Handle -> IO Char
43     hGetLine,                  -- :: Handle -> IO [Char]
44     hLookAhead,                -- :: Handle -> IO Char
45     hGetContents,              -- :: Handle -> IO [Char]
46     hPutChar,                  -- :: Handle -> Char -> IO ()
47     hPutStr,                   -- :: Handle -> [Char] -> IO ()
48     hPutStrLn,                 -- :: Handle -> [Char] -> IO ()
49     hPrint,                    -- :: Show a => Handle -> a -> IO ()
50     hIsOpen, hIsClosed,        -- :: Handle -> IO Bool
51     hIsReadable, hIsWritable,  -- :: Handle -> IO Bool
52     hIsSeekable,               -- :: Handle -> IO Bool
53
54     isAlreadyExistsError, isDoesNotExistError,  -- :: IOError -> Bool
55     isAlreadyInUseError, isFullError, 
56     isEOFError, isIllegalOperation, 
57     isPermissionError, isUserError, 
58
59     ioeGetErrorString,         -- :: IOError -> String
60     ioeGetHandle,              -- :: IOError -> Maybe Handle
61     ioeGetFileName,            -- :: IOError -> Maybe FilePath
62
63     try,                       -- :: IO a -> IO (Either IOError a)
64     bracket,                   -- :: IO a -> (a -> IO b) -> (a -> IO c) -> IO c
65     bracket_,                  -- :: IO a -> (a -> IO b) -> IO c -> IO c
66
67     -- Non-standard extension (but will hopefully become standard with 1.5) is
68     -- to export the Prelude io functions via IO (in addition to exporting them
69     -- from the prelude...for now.) 
70     IO,                        -- instance MonadFix
71     FilePath,                  -- :: String
72     IOError,
73     ioError,                   -- :: IOError -> IO a
74     userError,                 -- :: String  -> IOError
75     catch,                     -- :: IO a    -> (IOError -> IO a) -> IO a
76     interact,                  -- :: (String -> String) -> IO ()
77
78     putChar,                   -- :: Char   -> IO ()
79     putStr,                    -- :: String -> IO () 
80     putStrLn,                  -- :: String -> IO ()
81     print,                     -- :: Show a => a -> IO ()
82     getChar,                   -- :: IO Char
83     getLine,                   -- :: IO String
84     getContents,               -- :: IO String
85     readFile,                  -- :: FilePath -> IO String
86     writeFile,                 -- :: FilePath -> String -> IO ()
87     appendFile,                -- :: FilePath -> String -> IO ()
88     readIO,                    -- :: Read a => String -> IO a
89     readLn,                    -- :: Read a => IO a
90
91     hPutBuf,                   -- :: Handle -> Ptr a -> Int -> IO ()
92     hGetBuf,                   -- :: Handle -> Ptr a -> Int -> IO Int
93  
94     fixIO,                     -- :: (a -> IO a) -> IO a
95
96     hSetEcho,                   -- :: Handle -> Bool -> IO ()
97     hGetEcho,                   -- :: Handle -> IO Bool
98
99     hIsTerminalDevice,          -- :: Handle -> IO Bool
100   ) where
101
102 #ifdef __GLASGOW_HASKELL__
103 import GHC.Base
104 import GHC.IOBase       -- Together these four Prelude modules define
105 import GHC.Handle       -- all the stuff exported by IO for the GHC version
106 import GHC.IO
107 import GHC.ST           ( fixST )
108 import GHC.Exception
109 import GHC.Num
110 import GHC.Read
111 import GHC.Show
112 #endif
113
114 import Data.Dynamic
115 import Control.Monad.Fix
116 import System.IO.Error
117
118 -- -----------------------------------------------------------------------------
119 -- MonadFix instance
120
121 instance MonadFix IO where
122         mfix = fixIO
123
124 -- -----------------------------------------------------------------------------
125 -- Typeable instance for Handle
126
127 #include "Dynamic.h"
128 INSTANCE_TYPEABLE0(Handle,handleTc,"Handle")
129
130 -- -----------------------------------------------------------------------------
131 -- Standard IO
132
133 putChar         :: Char -> IO ()
134 putChar c       =  hPutChar stdout c
135
136 putStr          :: String -> IO ()
137 putStr s        =  hPutStr stdout s
138
139 putStrLn        :: String -> IO ()
140 putStrLn s      =  do putStr s
141                       putChar '\n'
142
143 print           :: Show a => a -> IO ()
144 print x         =  putStrLn (show x)
145
146 getChar         :: IO Char
147 getChar         =  hGetChar stdin
148
149 getLine         :: IO String
150 getLine         =  hGetLine stdin
151
152 getContents     :: IO String
153 getContents     =  hGetContents stdin
154
155 interact        ::  (String -> String) -> IO ()
156 interact f      =   do s <- getContents
157                        putStr (f s)
158
159 readFile        :: FilePath -> IO String
160 readFile name   =  openFile name ReadMode >>= hGetContents
161
162 writeFile       :: FilePath -> String -> IO ()
163 writeFile name str = do
164     hdl <- openFile name WriteMode
165     hPutStr hdl str
166     hClose hdl
167
168 appendFile      :: FilePath -> String -> IO ()
169 appendFile name str = do
170     hdl <- openFile name AppendMode
171     hPutStr hdl str
172     hClose hdl
173
174 readLn          :: Read a => IO a
175 readLn          =  do l <- getLine
176                       r <- readIO l
177                       return r
178
179 -- raises an exception instead of an error
180 readIO          :: Read a => String -> IO a
181 readIO s        =  case (do { (x,t) <- reads s ;
182                               ("","") <- lex t ;
183                               return x }) of
184                         [x]    -> return x
185                         []     -> ioError (userError "Prelude.readIO: no parse")
186                         _      -> ioError (userError "Prelude.readIO: ambiguous parse")
187
188 hReady          :: Handle -> IO Bool
189 hReady h        =  hWaitForInput h 0
190
191 hPutStrLn       :: Handle -> String -> IO ()
192 hPutStrLn hndl str = do
193  hPutStr  hndl str
194  hPutChar hndl '\n'
195
196 hPrint          :: Show a => Handle -> a -> IO ()
197 hPrint hdl      =  hPutStrLn hdl . show
198
199 -- ---------------------------------------------------------------------------
200 -- fixIO
201
202 #ifdef __GLASGOW_HASKELL__
203 fixIO           :: (a -> IO a) -> IO a
204 fixIO m         = stToIO (fixST (ioToST . m))
205 #endif