[project @ 1997-05-18 04:07:49 by sof]
[ghc-hetmet.git] / ghc / lib / required / IO.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1994-1996
3 %
4
5 \section[IO]{Module @IO@}
6
7 \begin{code}
8 {-# OPTIONS -fno-implicit-prelude -#include "cbits/stgio.h" #-}
9
10 module IO (
11     Handle, HandlePosn,
12
13     IOMode(ReadMode,WriteMode,AppendMode,ReadWriteMode),
14     BufferMode(NoBuffering,LineBuffering,BlockBuffering),
15     SeekMode(AbsoluteSeek,RelativeSeek,SeekFromEnd),
16
17     stdin, stdout, stderr, 
18
19     openFile, hClose, 
20     hFileSize, hIsEOF, isEOF,
21     hSetBuffering, hGetBuffering, hFlush, 
22     hGetPosn, hSetPosn, hSeek, 
23     hWaitForInput, hReady, hGetChar, hGetLine, hLookAhead, hGetContents, 
24     hPutChar, hPutStr, hPutStrLn, hPrint,
25     hIsOpen, hIsClosed, hIsReadable, hIsWritable, hIsSeekable,
26
27     isAlreadyExistsError, isDoesNotExistError, isAlreadyInUseError, 
28     isFullError, isEOFError,
29     isIllegalOperation, isPermissionError, isUserError, 
30     ioeGetErrorString, 
31     ioeGetHandle, ioeGetFileName,
32     try, bracket, bracket_
33   ) where
34
35 import Ix
36 import STBase
37 import IOBase
38 import ArrBase          ( MutableByteArray(..), newCharArray )
39 import IOHandle         -- much of the real stuff is in here
40 import PackedString     ( nilPS, packCBytesST, unpackPS )
41 import PrelBase
42 import GHC
43 import Foreign          ( ForeignObj, Addr, makeForeignObj, writeForeignObj )
44 \end{code}
45
46 %*********************************************************
47 %*                                                      *
48 \subsection{Signatures}
49 %*                                                      *
50 %*********************************************************
51
52 \begin{code}
53 --IOHandle:hClose                :: Handle -> IO () 
54 --IOHandle:hFileSize             :: Handle -> IO Integer
55 --IOHandle:hFlush                :: Handle -> IO () 
56 --IOHandle:hGetBuffering         :: Handle -> IO BufferMode
57 hGetChar              :: Handle -> IO Char
58 hGetContents          :: Handle -> IO String
59 --IOHandle:hGetPosn              :: Handle -> IO HandlePosn
60 --IOHandle:hIsClosed             :: Handle -> IO Bool
61 --IOHandle:hIsEOF                :: Handle -> IO Bool
62 --IOHandle:hIsOpen               :: Handle -> IO Bool
63 --IOHandle:hIsReadable           :: Handle -> IO Bool
64 --IOHandle:hIsSeekable           :: Handle -> IO Bool
65 --IOHandle:hIsWritable           :: Handle -> IO Bool
66 hLookAhead            :: Handle -> IO Char
67 hPrint                :: Show a => Handle -> a -> IO ()
68 hPutChar              :: Handle -> Char -> IO ()
69 hPutStr               :: Handle -> String -> IO ()
70 hPutStrLn             :: Handle -> String -> IO ()
71 hReady                :: Handle -> IO Bool 
72 hWaitForInput         :: Handle -> Int -> IO Bool
73
74 --IOHandle:hSeek                 :: Handle -> SeekMode -> Integer -> IO () 
75 --IOHandle:hSetBuffering         :: Handle -> BufferMode -> IO ()
76 --IOHandle:hSetPosn              :: HandlePosn -> IO () 
77 -- ioeGetFileName        :: IOError -> Maybe FilePath
78 -- ioeGetErrorString     :: IOError -> Maybe String
79 -- ioeGetHandle          :: IOError -> Maybe Handle
80 -- isAlreadyExistsError  :: IOError -> Bool
81 -- isAlreadyInUseError   :: IOError -> Bool
82 --IOHandle:isEOF                 :: IO Bool
83 -- isEOFError            :: IOError -> Bool
84 -- isFullError           :: IOError -> Bool
85 -- isIllegalOperation    :: IOError -> Bool
86 -- isPermissionError     :: IOError -> Bool
87 -- isUserError           :: IOError -> Maybe String
88 --IOHandle:openFile              :: FilePath -> IOMode -> IO Handle
89 --IOHandle:stdin, stdout, stderr :: Handle
90 \end{code}
91
92 Standard instances for @Handle@:
93
94 \begin{code}
95 instance Eq IOError where
96   (IOError h1 e1 str1) == (IOError h2 e2 str2) = 
97     e1==e2 && str1==str2 && h1==h2
98
99 instance Eq Handle where
100  h1 == h2 =
101   unsafePerformPrimIO (
102     ioToPrimIO (readHandle h1)      >>= \ h1_ ->
103     ioToPrimIO (writeHandle h1 h1_) >>
104     ioToPrimIO (readHandle h2)      >>= \ h2_ ->
105     ioToPrimIO (writeHandle h2 h2_) >>
106     return (
107      case (h1_,h2_) of
108       (ErrorHandle (IOError h1 _ _), ErrorHandle (IOError h2 _ _)) -> h1 == h2
109       (ClosedHandle, ClosedHandle) -> True
110       (SemiClosedHandle v1 _, SemiClosedHandle v2 _) -> v1 == v2
111       (ReadHandle v1 _ _ ,      ReadHandle v2 _ _)   -> v1 == v2
112       (WriteHandle v1 _ _ ,     WriteHandle v2 _ _)  -> v1 == v2
113       (AppendHandle v1 _ _ ,    AppendHandle v2 _ _) -> v1 == v2
114       (ReadWriteHandle v1 _ _ , ReadWriteHandle v2 _ _) -> v1 == v2
115       _ -> False))
116
117 instance Show Handle where {showsPrec p h = showString "<<Handle>>"}
118
119 \end{code}
120
121 %*********************************************************
122 %*                                                      *
123 \subsection{Simple input operations}
124 %*                                                      *
125 %*********************************************************
126
127 Computation @hReady hdl@ indicates whether at least
128 one item is available for input from handle {\em hdl}.
129
130 @hWaitForInput@ is the generalisation, wait for \tr{n} seconds
131 before deciding whether the Handle has run dry or not.
132
133 \begin{code}
134 --hReady :: Handle -> IO Bool
135 hReady h = hWaitForInput h 0
136
137 --hWaitForInput :: Handle -> Int -> IO Bool 
138 hWaitForInput handle nsecs = 
139     readHandle handle                               >>= \ htype ->
140     case htype of 
141       ErrorHandle ioError ->
142           writeHandle handle htype                  >>
143           fail ioError
144       ClosedHandle ->
145           writeHandle handle htype                  >>
146           ioe_closedHandle handle
147       SemiClosedHandle _ _ ->
148           writeHandle handle htype                  >>
149           ioe_closedHandle handle
150       AppendHandle _ _ _ ->
151           writeHandle handle htype                  >>
152           fail (IOError (Just handle) IllegalOperation "handle is not open for reading")
153       WriteHandle _ _ _ ->
154           writeHandle handle htype                  >>
155           fail (IOError (Just handle) IllegalOperation "handle is not open for reading")
156       other -> 
157           _ccall_ inputReady (filePtr other) nsecs  `thenIO_Prim` \ rc ->
158           writeHandle handle (markHandle htype)     >>
159           case rc of
160             0 -> return False
161             1 -> return True
162             _ -> constructErrorAndFail "hWaitForInput"
163 \end{code}
164
165 Computation $hGetChar hdl$ reads the next character from handle 
166 {\em hdl}, blocking until a character is available.
167
168 \begin{code}
169 --hGetChar :: Handle -> IO Char
170
171 hGetChar handle = 
172     readHandle handle                               >>= \ htype ->
173     case htype of 
174       ErrorHandle ioError ->
175           writeHandle handle htype                  >>
176           fail ioError
177       ClosedHandle ->
178           writeHandle handle htype                  >>
179           ioe_closedHandle handle
180       SemiClosedHandle _ _ ->
181           writeHandle handle htype                  >>
182           ioe_closedHandle handle
183       AppendHandle _ _ _ ->
184           writeHandle handle htype                  >>
185           fail (IOError (Just handle) IllegalOperation "handle is not open for reading")
186       WriteHandle _ _ _ ->
187           writeHandle handle htype                  >>
188           fail (IOError (Just handle) IllegalOperation "handle is not open for reading")
189       other -> 
190           _ccall_ fileGetc (filePtr other)          `thenIO_Prim` \ intc ->
191           writeHandle handle (markHandle htype)   >>
192           if intc /= ``EOF'' then
193               return (chr intc)
194           else
195               constructErrorAndFail "hGetChar"
196
197 hGetLine :: Handle -> IO String
198 hGetLine h = 
199  hGetChar h >>= \ c ->
200  if c == '\n' then 
201     return "" 
202  else 
203     hGetLine h >>= \ s -> return (c:s)
204 \end{code}
205
206 Computation $hLookahead hdl$ returns the next character from handle
207 {\em hdl} without removing it from the input buffer, blocking until a
208 character is available.
209
210 \begin{code}
211 --hLookAhead :: Handle -> IO Char
212
213 hLookAhead handle = 
214     readHandle handle                               >>= \ htype ->
215     case htype of 
216       ErrorHandle ioError ->
217           writeHandle handle htype                  >>
218           fail ioError
219       ClosedHandle ->
220           writeHandle handle htype                  >>
221           ioe_closedHandle handle
222       SemiClosedHandle _ _ ->
223           writeHandle handle htype                  >>
224           ioe_closedHandle handle
225       AppendHandle _ _ _ ->
226           writeHandle handle htype                  >>
227           fail (IOError (Just handle) IllegalOperation "handle is not open for reading")
228       WriteHandle _ _ _ ->
229           writeHandle handle htype                  >>
230           fail (IOError (Just handle) IllegalOperation "handle is not open for reading")
231       other -> 
232           _ccall_ fileLookAhead (filePtr other)    `thenIO_Prim` \ intc ->
233           writeHandle handle (markHandle htype)   >>
234           if intc /= ``EOF'' then
235               return (chr intc)
236           else
237               constructErrorAndFail "hLookAhead"
238 \end{code}
239
240
241 %*********************************************************
242 %*                                                      *
243 \subsection{Getting the entire contents of a handle}
244 %*                                                      *
245 %*********************************************************
246
247 Computation $hGetContents hdl$ returns the list of characters
248 corresponding to the unread portion of the channel or file managed by
249 {\em hdl}, which is made semi-closed.
250
251 \begin{code}
252 --hGetContents :: Handle -> IO String
253
254 hGetContents handle =
255     readHandle handle                               >>= \ htype ->
256     case htype of 
257       ErrorHandle ioError ->
258           writeHandle handle htype                  >>
259           fail ioError
260       ClosedHandle ->
261           writeHandle handle htype                  >>
262           ioe_closedHandle handle
263       SemiClosedHandle _ _ ->
264           writeHandle handle htype                  >>
265           ioe_closedHandle handle
266       AppendHandle _ _ _ ->
267           writeHandle handle htype                  >>
268           fail (IOError (Just handle) IllegalOperation "handle is not open for reading")
269       WriteHandle _ _ _ ->
270           writeHandle handle htype                  >>
271           fail (IOError (Just handle) IllegalOperation "handle is not open for reading")
272       other -> 
273           {- 
274              To avoid introducing an extra layer of buffering here,
275              we provide three lazy read methods, based on character,
276              line, and block buffering.
277           -}
278           stToIO (getBufferMode other)  >>= \ other ->
279           case (bufferMode other) of
280             Just LineBuffering ->
281                 allocBuf Nothing                    >>= \ buf_info ->
282                 writeHandle handle (SemiClosedHandle (filePtr other) buf_info)
283                                                     >>
284                 unsafeInterleavePrimIO (lazyReadLine handle)
285                                                     `thenIO_Prim` \ contents ->
286                 return contents
287
288             Just (BlockBuffering size) ->
289                 allocBuf size                       >>= \ buf_info ->
290                 writeHandle handle (SemiClosedHandle (filePtr other) buf_info)
291                                                     >>
292                 unsafeInterleavePrimIO (lazyReadBlock handle)
293                                                     `thenIO_Prim` \ contents ->
294                 return contents
295             _ -> -- Nothing is treated pessimistically as NoBuffering
296                 writeHandle handle (SemiClosedHandle (filePtr other) (``NULL'', 0))
297                                                     >>
298                 unsafeInterleavePrimIO (lazyReadChar handle)
299                                                     `thenIO_Prim` \ contents ->
300                 return contents
301   where
302     allocBuf :: Maybe Int -> IO (Addr, Int)
303     allocBuf msize =
304         _ccall_ malloc size                         `thenIO_Prim` \ buf ->
305         if buf /= ``NULL'' then
306             return (buf, size)
307         else
308             fail (IOError Nothing ResourceExhausted "not enough virtual memory")
309       where
310         size = 
311             case msize of
312               Just x -> x
313               Nothing -> ``BUFSIZ''
314 \end{code}
315
316 Note that someone may yank our handle out from under us, and then re-use
317 the same FILE * for something else.  Therefore, we have to re-examine the
318 handle every time through.
319
320 \begin{code}
321 lazyReadBlock :: Handle -> PrimIO String
322 lazyReadLine  :: Handle -> PrimIO String
323 lazyReadChar  :: Handle -> PrimIO String
324
325 lazyReadBlock handle =
326     ioToST (readHandle handle)                  >>= \ htype ->
327     case htype of 
328       -- There cannae be an ErrorHandle here
329       ClosedHandle ->
330           ioToST (writeHandle handle htype)     >>
331           returnPrimIO ""
332       SemiClosedHandle fp (buf, size) ->
333           _ccall_ readBlock buf fp size             >>= \ bytes ->
334           (if bytes <= 0
335           then return nilPS
336           else packCBytesST bytes buf)              >>= \ some ->
337           if bytes < 0 then
338               _ccall_ free buf                      >>= \ () ->
339               _ccall_ closeFile fp                  >>
340 #ifndef PAR
341               writeForeignObj fp ``NULL''           >>
342               ioToST (writeHandle handle (SemiClosedHandle fp (``NULL'', 0))) >>
343 #else
344               ioToST (writeHandle handle (SemiClosedHandle ``NULL'' (``NULL'', 0))) >>
345 #endif
346               returnPrimIO (unpackPS some)
347           else
348               ioToST (writeHandle handle htype)     >>
349               unsafeInterleavePrimIO (lazyReadBlock handle)
350                                                     >>= \ more ->
351               returnPrimIO (unpackPS some ++ more)
352
353 lazyReadLine handle =
354     ioToST (readHandle handle)                      >>= \ htype ->
355     case htype of 
356       -- There cannae be an ErrorHandle here
357       ClosedHandle ->
358           ioToST (writeHandle handle htype) >>
359           returnPrimIO ""
360       SemiClosedHandle fp (buf, size) ->
361           _ccall_ readLine buf fp size              >>= \ bytes ->
362           (if bytes <= 0
363           then return nilPS
364           else packCBytesST bytes buf)              >>= \ some ->
365           if bytes < 0 then
366               _ccall_ free buf                      >>= \ () ->
367               _ccall_ closeFile fp                  >>
368 #ifndef PAR
369               writeForeignObj fp ``NULL''           >>
370               ioToST (writeHandle handle (SemiClosedHandle fp (``NULL'', 0))) >>
371 #else
372               ioToST (writeHandle handle (SemiClosedHandle ``NULL'' (``NULL'', 0))) >>
373 #endif
374               returnPrimIO (unpackPS some)
375           else
376               ioToST (writeHandle handle htype)     >>
377               unsafeInterleavePrimIO (lazyReadLine handle)
378                                                     >>= \ more ->
379               returnPrimIO (unpackPS some ++ more)
380
381 lazyReadChar handle =
382     ioToST (readHandle handle)                      >>= \ htype ->
383     case htype of 
384       -- There cannae be an ErrorHandle here
385       ClosedHandle ->
386           ioToST (writeHandle handle htype)         >>
387           returnPrimIO ""
388       SemiClosedHandle fp buf_info ->
389           _ccall_ readChar fp                       >>= \ char ->
390           if char == ``EOF'' then
391               _ccall_ closeFile fp                  >>
392 #ifndef PAR
393               writeForeignObj fp ``NULL''           >>
394               ioToST (writeHandle handle (SemiClosedHandle fp (``NULL'', 0))) >>
395 #else
396               ioToST (writeHandle handle (SemiClosedHandle ``NULL'' (``NULL'', 0))) >>
397 #endif
398               returnPrimIO ""
399           else
400               ioToST (writeHandle handle htype)     >>
401               unsafeInterleavePrimIO (lazyReadChar handle)
402                                                     >>= \ more ->
403               returnPrimIO (chr char : more)
404
405 \end{code}
406
407
408 %*********************************************************
409 %*                                                      *
410 \subsection{Simple output functions}
411 %*                                                      *
412 %*********************************************************
413
414 Computation $hPutChar hdl c$ writes the character {\em c} to the file
415 or channel managed by {\em hdl}.  Characters may be buffered if
416 buffering is enabled for {\em hdl}.
417
418 \begin{code}
419 --hPutChar :: Handle -> Char -> IO ()
420
421 hPutChar handle c =
422     readHandle handle                               >>= \ htype ->
423     case htype of 
424       ErrorHandle ioError ->
425           writeHandle handle htype                  >>
426           fail ioError
427       ClosedHandle ->
428           writeHandle handle htype                  >>
429           ioe_closedHandle handle
430       SemiClosedHandle _ _ ->
431           writeHandle handle htype                  >>
432           ioe_closedHandle handle
433       ReadHandle _ _ _ ->
434           writeHandle handle htype                  >>
435           fail (IOError (Just handle) IllegalOperation "handle is not open for writing")
436       other -> 
437           _ccall_ filePutc (filePtr other) (ord c) `thenIO_Prim` \ rc ->
438           writeHandle handle (markHandle htype)   >>
439           if rc == 0 then
440               return ()
441           else
442               constructErrorAndFail "hPutChar"
443 \end{code}
444
445 Computation $hPutStr hdl s$ writes the string {\em s} to the file or
446 channel managed by {\em hdl}.
447
448 \begin{code}
449 --hPutStr :: Handle -> String -> IO ()
450
451 hPutStr handle str = 
452     readHandle handle                               >>= \ htype ->
453     case htype of 
454       ErrorHandle ioError ->
455           writeHandle handle htype                  >>
456           fail ioError
457       ClosedHandle ->
458           writeHandle handle htype                  >>
459           ioe_closedHandle handle
460       SemiClosedHandle _ _ ->
461           writeHandle handle htype                  >>
462           ioe_closedHandle handle
463       ReadHandle _ _ _ ->
464           writeHandle handle htype                  >>
465           fail (IOError (Just handle) IllegalOperation "handle is not open for writing")
466       other -> 
467           {-
468            The code below is not correct for line-buffered terminal streams,
469            as the output stream is not flushed when terminal input is requested
470            again, just upon seeing a newline character. A temporary fix for the
471            most common line-buffered output stream, stdout, is to assume the
472            buffering it was given when created (no buffering). This is not
473            as bad as it looks, since stdio buffering sits underneath this.
474
475            ToDo: fix me
476           -}
477           getBufferMode other                       `thenIO_Prim` \ other ->
478           (case bufferMode other of
479             Just LineBuffering ->
480                 writeChars (filePtr other) str
481                 --writeLines (filePtr other) str
482             Just (BlockBuffering (Just size)) ->
483                 writeBlocks (filePtr other) size str
484             Just (BlockBuffering Nothing) ->
485                 writeBlocks (filePtr other) ``BUFSIZ'' str
486             _ -> -- Nothing is treated pessimistically as NoBuffering
487                 writeChars (filePtr other) str
488           )                                         `thenIO_Prim` \ success ->
489           writeHandle handle (markHandle other) >>
490           if success then
491               return ()
492           else
493               constructErrorAndFail "hPutStr"
494   where
495 #ifndef PAR
496     writeLines :: ForeignObj -> String -> PrimIO Bool
497 #else
498     writeLines :: Addr -> String -> PrimIO Bool
499 #endif
500     writeLines = writeChunks ``BUFSIZ'' True 
501
502 #ifndef PAR
503     writeBlocks :: ForeignObj -> Int -> String -> PrimIO Bool
504 #else
505     writeBlocks :: Addr -> Int -> String -> PrimIO Bool
506 #endif
507     writeBlocks fp size s = writeChunks size False fp s
508  
509     {-
510       The breaking up of output into lines along \n boundaries
511       works fine as long as there are newlines to split by.
512       Avoid the splitting up into lines alltogether (doesn't work
513       for overly long lines like the stuff that showsPrec instances
514       normally return). Instead, we split them up into fixed size
515       chunks before blasting them off to the Real World.
516
517       Hacked to avoid multiple passes over the strings - unsightly, but
518       a whole lot quicker. -- SOF 3/96
519     -}
520
521 #ifndef PAR
522     writeChunks :: Int -> Bool -> ForeignObj -> String -> PrimIO Bool
523 #else
524     writeChunks :: Int -> Bool -> Addr -> String -> PrimIO Bool
525 #endif
526     writeChunks (I# bufLen) chopOnNewLine fp s =
527      newCharArray (0,I# bufLen) >>= \ arr@(MutableByteArray _ arr#) ->
528      let
529       write_char :: MutableByteArray# RealWorld -> Int# -> Char# -> PrimIO ()
530       write_char arr# n x = ST $ \ (S# s#) ->
531           case (writeCharArray# arr# n x s#) of { s1# ->
532           ( (), S# s1# ) }
533
534       shoveString :: Int# -> [Char] -> PrimIO Bool
535       shoveString n ls = 
536        case ls of
537          [] ->   
538            if n ==# 0# then
539               returnPrimIO True
540            else
541              _ccall_ writeFile arr fp (I# n) >>= \rc ->
542              returnPrimIO (rc==0)
543
544          ((C# x):xs) ->
545            write_char arr# n x  >>
546            
547            {- Flushing lines - should we bother? Yes, for line-buffered output. -}
548            if n ==# bufLen || (chopOnNewLine && (x `eqChar#` '\n'#)) then
549               _ccall_ writeFile arr fp (I# (n +# 1#)) >>= \ rc ->
550               if rc == 0 then
551                  shoveString 0# xs
552                else
553                  return False
554             else
555                shoveString (n +# 1#) xs
556      in
557      shoveString 0# s
558
559 #ifndef PAR
560     writeChars :: ForeignObj -> String -> PrimIO Bool
561 #else
562     writeChars :: Addr -> String -> PrimIO Bool
563 #endif
564     writeChars fp "" = returnPrimIO True
565     writeChars fp (c:cs) =
566         _ccall_ filePutc fp (ord c) >>= \ rc ->
567         if rc == 0 then
568             writeChars fp cs
569         else
570             returnPrimIO False
571 \end{code}
572
573 Computation $hPrint hdl t$ writes the string representation of {\em t}
574 given by the $shows$ function to the file or channel managed by {\em
575 hdl}.
576
577 SOF 2/97: Seem to have disappeared in 1.4 libs.
578
579 \begin{code}
580 --hPrint :: Show a => Handle -> a -> IO ()
581 hPrint hdl = hPutStr hdl . show
582 \end{code}
583
584 Derived action @hPutStrLn hdl str@ writes the string \tr{str} to
585 the handle \tr{hdl}, adding a newline at the end.
586
587 \begin{code}
588 --hPutStrLn :: Handle -> String -> IO ()
589 hPutStrLn hndl str = do
590  hPutStr  hndl str
591  hPutChar hndl '\n'
592
593 \end{code}
594
595
596 %*********************************************************
597 %*                                                      *
598 \subsection{Try and bracket}
599 %*                                                      *
600 %*********************************************************
601
602 The construct $try comp$ exposes errors which occur within a
603 computation, and which are not fully handled.  It always succeeds.
604
605 \begin{code}
606 try            :: IO a -> IO (Either IOError a)
607 try f          =  catch (do r <- f
608                             return (Right r))
609                         (return . Left)
610
611 bracket        :: IO a -> (a -> IO b) -> (a -> IO c) -> IO c
612 bracket before after m = do
613         x  <- before
614         rs <- try (m x)
615         after x
616         case rs of
617            Right r -> return r
618            Left  e -> fail e
619
620 -- variant of the above where middle computation doesn't want x
621 bracket_        :: IO a -> (a -> IO b) -> IO c -> IO c
622 bracket_ before after m = do
623          x  <- before
624          rs <- try m
625          after x
626          case rs of
627             Right r -> return r
628             Left  e -> fail e
629 \end{code}
630