[project @ 2000-04-14 16:17:47 by simonmar]
[ghc-hetmet.git] / ghc / lib / std / PrelIO.lhs
1 %
2 % (c) The GRAP/AQUA Project, Glasgow University, 1992-1996
3 %
4 \section[PrelIO]{Module @PrelIO@}
5
6 This module defines all basic IO operations.
7 These are needed for the IO operations exported by Prelude,
8 but as it happens they also do everything required by library
9 module IO.
10
11
12 \begin{code}
13 {-# OPTIONS -fno-implicit-prelude -#include "cbits/stgio.h" #-}
14
15 module PrelIO where
16
17 import PrelBase
18
19 import PrelIOBase
20 import PrelHandle       -- much of the real stuff is in here
21
22 import PrelNum
23 import PrelRead         ( readParen, Read(..), reads, lex,
24                           readIO 
25                         )
26 import PrelShow
27 import PrelMaybe        ( Either(..), Maybe(..) )
28 import PrelAddr         ( Addr(..), AddrOff(..), nullAddr, plusAddr )
29 import PrelByteArr      ( ByteArray )
30 import PrelPack         ( unpackNBytesAccST )
31 import PrelException    ( ioError, catch, catchException, throw, blockAsyncExceptions )
32 import PrelConc
33 \end{code}
34
35
36 %*********************************************************
37 %*                                                       *
38 \subsection{Standard IO}
39 %*                                                       *
40 %*********************************************************
41
42 The Prelude has from Day 1 provided a collection of common
43 IO functions. We define these here, but let the Prelude
44 export them.
45
46 \begin{code}
47 putChar         :: Char -> IO ()
48 putChar c       =  hPutChar stdout c
49
50 putStr          :: String -> IO ()
51 putStr s        =  hPutStr stdout s
52
53 putStrLn        :: String -> IO ()
54 putStrLn s      =  do putStr s
55                       putChar '\n'
56
57 print           :: Show a => a -> IO ()
58 print x         =  putStrLn (show x)
59
60 getChar         :: IO Char
61 getChar         =  hGetChar stdin
62
63 getLine         :: IO String
64 getLine         =  hGetLine stdin
65             
66 getContents     :: IO String
67 getContents     =  hGetContents stdin
68
69 interact        ::  (String -> String) -> IO ()
70 interact f      =   do s <- getContents
71                        putStr (f s)
72
73 readFile        :: FilePath -> IO String
74 readFile name   =  openFile name ReadMode >>= hGetContents
75
76 writeFile       :: FilePath -> String -> IO ()
77 writeFile name str = do
78     hdl <- openFile name WriteMode
79     hPutStr hdl str
80     hClose hdl
81
82 appendFile      :: FilePath -> String -> IO ()
83 appendFile name str = do
84     hdl <- openFile name AppendMode
85     hPutStr hdl str
86     hClose hdl
87
88 readLn          :: Read a => IO a
89 readLn          =  do l <- getLine
90                       r <- readIO l
91                       return r
92 \end{code}
93
94
95 %*********************************************************
96 %*                                                      *
97 \subsection{Simple input operations}
98 %*                                                      *
99 %*********************************************************
100
101 Computation @hReady hdl@ indicates whether at least
102 one item is available for input from handle {\em hdl}.
103
104 @hWaitForInput@ is the generalisation, wait for \tr{n} milliseconds
105 before deciding whether the Handle has run dry or not.
106
107 If @hWaitForInput@ finds anything in the Handle's buffer, it immediately returns.
108 If not, it tries to read from the underlying OS handle. Notice that
109 for buffered Handles connected to terminals this means waiting until a complete
110 line is available.
111
112 \begin{code}
113 hReady :: Handle -> IO Bool
114 hReady h = hWaitForInput h 0
115
116 hWaitForInput :: Handle -> Int -> IO Bool 
117 hWaitForInput handle msecs =
118     wantReadableHandle "hWaitForInput" handle $ \ handle_ -> do
119     rc       <- inputReady (haFO__ handle_) (msecs::Int)     -- ConcHask: SAFE, won't block
120     case (rc::Int) of
121       0 -> return False
122       1 -> return True
123       _ -> constructErrorAndFail "hWaitForInput"
124 \end{code}
125
126 @hGetChar hdl@ reads the next character from handle @hdl@,
127 blocking until a character is available.
128
129 \begin{code}
130 hGetChar :: Handle -> IO Char
131 hGetChar handle = do
132   c <- mayBlockRead "hGetChar" handle fileGetc
133   return (chr c)
134
135 {-
136   If EOF is reached before EOL is encountered, ignore the
137   EOF and return the partial line. Next attempt at calling
138   hGetLine on the handle will yield an EOF IO exception though.
139 -}
140 hGetLine :: Handle -> IO String
141 hGetLine h = do
142   c <- hGetChar h
143   if c == '\n' then
144      return ""
145    else do
146     l <- getRest
147     return (c:l)
148  where
149   getRest = do
150     c <- 
151       catch 
152         (hGetChar h)
153         (\ err -> do
154           if isEOFError err then
155              return '\n'
156            else
157              ioError err)
158     if c == '\n' then
159        return ""
160      else do
161        s <- getRest
162        return (c:s)
163
164 \end{code}
165
166 @hLookahead hdl@ returns the next character from handle @hdl@
167 without removing it from the input buffer, blocking until a
168 character is available.
169
170 \begin{code}
171 hLookAhead :: Handle -> IO Char
172 hLookAhead handle = do
173   rc <- mayBlockRead "hLookAhead" handle fileLookAhead
174   return (chr rc)
175 \end{code}
176
177
178 %*********************************************************
179 %*                                                      *
180 \subsection{Getting the entire contents of a handle}
181 %*                                                      *
182 %*********************************************************
183
184 @hGetContents hdl@ returns the list of characters corresponding
185 to the unread portion of the channel or file managed by @hdl@,
186 which is made semi-closed.
187
188 \begin{code}
189 hGetContents :: Handle -> IO String
190 hGetContents handle = 
191         -- can't use wantReadableHandle here, because we want to side effect
192         -- the handle.
193     withHandle handle $ \ handle_ -> do
194     case haType__ handle_ of 
195       ErrorHandle theError -> ioError theError
196       ClosedHandle         -> ioe_closedHandle "hGetContents" handle
197       SemiClosedHandle     -> ioe_closedHandle "hGetContents" handle
198       AppendHandle         -> ioError not_readable_error
199       WriteHandle          -> ioError not_readable_error
200       _ -> do
201           {- 
202             To avoid introducing an extra layer of buffering here,
203             we provide three lazy read methods, based on character,
204             line, and block buffering.
205           -}
206         let handle_' = handle_{ haType__ = SemiClosedHandle }
207         case (haBufferMode__ handle_) of
208          LineBuffering    -> do
209             str <- unsafeInterleaveIO (lazyReadLine handle (haFO__ handle_))
210             return (handle_', str)
211          BlockBuffering _ -> do
212             str <- unsafeInterleaveIO (lazyReadBlock handle (haFO__ handle_))
213             return (handle_', str)
214          NoBuffering      -> do
215             str <- unsafeInterleaveIO (lazyReadChar handle (haFO__ handle_))
216             return (handle_', str)
217   where
218    not_readable_error = 
219            IOError (Just handle) IllegalOperation "hGetContents"
220                    ("handle is not open for reading")
221 \end{code}
222
223 Note that someone may close the semi-closed handle (or change its buffering), 
224 so each these lazy read functions are pulled on, they have to check whether
225 the handle has indeed been closed.
226
227 \begin{code}
228 #ifndef __PARALLEL_HASKELL__
229 lazyReadBlock :: Handle -> ForeignObj -> IO String
230 lazyReadLine  :: Handle -> ForeignObj -> IO String
231 lazyReadChar  :: Handle -> ForeignObj -> IO String
232 #else
233 lazyReadBlock :: Handle -> Addr -> IO String
234 lazyReadLine  :: Handle -> Addr -> IO String
235 lazyReadChar  :: Handle -> Addr -> IO String
236 #endif
237
238 lazyReadBlock handle fo = do
239    buf   <- getBufStart fo 0
240    bytes <- mayBlock fo (readBlock fo) -- ConcHask: UNSAFE, may block.
241    case (bytes::Int) of
242      -3 -> -- buffering has been turned off, use lazyReadChar instead
243            lazyReadChar handle fo
244      -2 -> return ""
245      -1 -> -- an error occurred, close the handle
246           withHandle handle $ \ handle_ -> do
247           closeFile (haFO__ handle_) 0{-don't bother flushing-}  -- ConcHask: SAFE, won't block.
248           return (handle_ { haType__    = ClosedHandle,
249                             haFO__      = nullFile__ }, 
250                   "")
251      _ -> do
252       more <- unsafeInterleaveIO (lazyReadBlock handle fo)
253       stToIO (unpackNBytesAccST buf bytes more)
254
255 lazyReadLine handle fo = do
256      bytes <- mayBlock fo (readLine fo)   -- ConcHask: UNSAFE, may block.
257      case (bytes::Int) of
258        -3 -> -- buffering has been turned off, use lazyReadChar instead
259              lazyReadChar handle fo
260        -2 -> return "" -- handle closed by someone else, stop reading.
261        -1 -> -- an error occurred, close the handle
262              withHandle handle $ \ handle_ -> do
263              closeFile (haFO__ handle_) 0{- don't bother flushing-}  -- ConcHask: SAFE, won't block
264              return (handle_ { haType__    = ClosedHandle,
265                                haFO__      = nullFile__ },
266                      "")
267        _ -> do
268           more <- unsafeInterleaveIO (lazyReadLine handle fo)
269           buf  <- getBufStart fo bytes  -- ConcHask: won't block
270           stToIO (unpackNBytesAccST buf bytes more)
271
272 lazyReadChar handle fo = do
273     char <- mayBlock fo (readChar fo)   -- ConcHask: UNSAFE, may block.
274     case (char::Int) of
275       -4 -> -- buffering is now block-buffered, use lazyReadBlock instead
276             lazyReadBlock handle fo
277             
278       -3 -> -- buffering is now line-buffered, use lazyReadLine instead
279             lazyReadLine handle fo
280       -2 -> return ""
281       -1 -> -- error, silently close handle.
282          withHandle handle $ \ handle_ -> do
283          closeFile (haFO__ handle_) 0{-don't bother flusing-}  -- ConcHask: SAFE, won't block
284          return (handle_{ haType__  = ClosedHandle,
285                           haFO__    = nullFile__ },
286                  "")
287       _ -> do
288          more <- unsafeInterleaveIO (lazyReadChar handle fo)
289          return (chr char : more)
290
291 \end{code}
292
293
294 %*********************************************************
295 %*                                                      *
296 \subsection{Simple output functions}
297 %*                                                      *
298 %*********************************************************
299
300 @hPutChar hdl ch@ writes the character @ch@ to the file
301 or channel managed by @hdl@.  Characters may be buffered if
302 buffering is enabled for @hdl@
303
304 \begin{code}
305 hPutChar :: Handle -> Char -> IO ()
306 hPutChar handle c = 
307     c `seq` do   -- must evaluate c before grabbing the handle lock
308     wantWriteableHandle "hPutChar" handle $ \ handle_  -> do
309     let fo = haFO__ handle_
310     flushConnectedBuf fo
311     rc <- mayBlock fo (filePutc fo c)   -- ConcHask: UNSAFE, may block.
312     if rc == 0
313      then return ()
314      else constructErrorAndFail "hPutChar"
315
316 hPutChars :: Handle -> [Char] -> IO ()
317 hPutChars handle [] = return ()
318 hPutChars handle (c:cs) = hPutChar handle c >> hPutChars handle cs
319 \end{code}
320
321 @hPutStr hdl s@ writes the string @s@ to the file or
322 channel managed by @hdl@, buffering the output if needs be.
323
324
325 \begin{code}
326 hPutStr :: Handle -> String -> IO ()
327 hPutStr handle str = do
328     buffer_mode <- wantWriteableHandle_ "hPutStr" handle 
329                         (\ handle_ -> do getBuffer handle_)
330     case buffer_mode of
331        (NoBuffering, _, _) -> do
332             hPutChars handle str        -- v. slow, but we don't care
333        (LineBuffering, buf, bsz) -> do
334             writeLines handle buf bsz str
335        (BlockBuffering _, buf, bsz) -> do
336             writeBlocks handle buf bsz str
337         -- ToDo: async exceptions during writeLines & writeBlocks will cause
338         -- the buffer to get lost in the void.  Using ByteArrays instead of
339         -- malloced buffers is one way around this, but we really ought to
340         -- be able to handle it with exception handlers/block/unblock etc.
341
342 getBuffer :: Handle__ -> IO (Handle__, (BufferMode, Addr, Int))
343 getBuffer handle_ = do
344    let bufs = haBuffers__ handle_
345        fo   = haFO__ handle_
346        mode = haBufferMode__ handle_    
347    sz <- getBufSize fo
348    case mode of
349         NoBuffering -> return (handle_, (mode, nullAddr, 0))
350         _ -> case bufs of
351                 [] -> do  buf <- allocMemory__ sz
352                           return (handle_, (mode, buf, sz))
353                 (b:bs) -> return (handle_{ haBuffers__ = bs }, (mode, b, sz))
354
355 freeBuffer :: Handle__ -> Addr -> Int -> IO Handle__
356 freeBuffer handle_ buf sz = do
357    fo_sz <- getBufSize (haFO__ handle_)
358    if (sz /= fo_sz) 
359         then do { free buf; return handle_ }
360         else do { return handle_{ haBuffers__ = buf : haBuffers__ handle_ } }
361
362 swapBuffers :: Handle__ -> Addr -> Int -> IO Handle__
363 swapBuffers handle_ buf sz = do
364    let fo = haFO__ handle_
365    fo_buf <- getBuf fo
366    setBuf fo buf sz
367    return (handle_{ haBuffers__ = fo_buf : haBuffers__ handle_ })
368
369 -----------------------------------------------------------------------------------
370 -- commitAndReleaseBuffer handle buf sz count flush
371 -- 
372 -- Write the contents of the buffer 'buf' ('sz' bytes long, containing
373 -- 'count' bytes of data) to handle (handle must be block or line buffered).
374 -- 
375 -- Implementation:
376 -- 
377 --    for block/line buffering,
378 --       1. If there isn't room in the handle buffer, flush the handle
379 --          buffer.
380 -- 
381 --       2. If the handle buffer is empty,
382 --               if flush, 
383 --                   then write buf directly to the device.
384 --                   else swap the handle buffer with buf.
385 -- 
386 --       3. If the handle buffer is non-empty, copy buf into the
387 --          handle buffer.  Then, if flush != 0, flush
388 --          the buffer.
389
390 commitAndReleaseBuffer
391         :: Handle                       -- handle to commit to
392         -> Addr -> Int                  -- address and size (in bytes) of buffer
393         -> Int                          -- number of bytes of data in buffer
394         -> Bool                         -- flush the handle afterward?
395         -> IO ()
396
397 commitAndReleaseBuffer hdl@(Handle h) buf sz count flush = do
398       h_ <- takeMVar h
399
400         -- First deal with any possible exceptions by freeing the buffer.
401         -- Async exceptions are blocked, but there are still some interruptible
402         -- ops below.
403
404         -- note that commit doesn't *always* free the buffer, it might
405         -- swap it for the current handle buffer instead.  This makes things
406         -- a whole lot more complicated, because we can't just do 
407         -- "finally (... free buffer ...)" here.
408       catchException (commit hdl h_) 
409                      (\e -> do { h_ <- freeBuffer h_ buf sz; putMVar h h_ })
410
411   where
412    commit hdl@(Handle h) handle_ = 
413      checkWriteableHandle "commitAndReleaseBuffer" hdl handle_ $ do
414       let fo = haFO__ handle_
415       flushConnectedBuf fo              -- ????  -SDM
416       getWriteableBuf fo                -- flush read buf if necessary
417       fo_buf     <- getBuf fo
418       fo_wptr    <- getBufWPtr fo
419       fo_bufSize <- getBufSize fo
420
421       let ok    h_ = putMVar h h_ >> return ()
422
423       if (flush || fo_bufSize - fo_wptr < count)  -- not enough room in handle buffer?
424
425             then do rc <- mayBlock fo (flushFile fo)
426                     if (rc < 0) 
427                         then constructErrorAndFail "commitAndReleaseBuffer"
428                         else
429                      if (flush || sz /= fo_bufSize)
430                         then do rc <- write_buf fo buf count
431                                 if (rc < 0)
432                                     then constructErrorAndFail "commitAndReleaseBuffer"
433                                     else do handle_ <- freeBuffer handle_ buf sz
434                                             ok handle_
435
436                         -- don't have to flush, and the new buffer is the
437                         -- same size as the old one, so just swap them...
438                         else do handle_ <- swapBuffers handle_ buf sz
439                                 setBufWPtr fo count
440                                 ok handle_
441
442                 -- not flushing, and there's enough room in the buffer:
443                 -- just copy the data in and update bufWPtr.
444             else do memcpy (plusAddr fo_buf (AddrOff# fo_wptr)) buf count
445                     setBufWPtr fo (fo_wptr + count)
446                     handle_ <- freeBuffer handle_ buf sz
447                     ok handle_
448
449 ------------------------------------------------------------------------------------
450 -- commitBuffer handle buf sz count flush
451 -- 
452 -- Flushes 'count' bytes from the buffer 'buf' (size 'sz') to 'handle'.
453 -- There are several cases to consider altogether:
454 -- 
455 -- If flush, 
456 --         - flush handle buffer,
457 --         - write out new buffer directly
458 -- 
459 -- else
460 --         - if there's enough room in the handle buffer, then copy new buf into it
461 --           else flush handle buffer, then copy new buffer into it
462
463 commitBuffer
464         :: Handle                       -- handle to commit to
465         -> Addr -> Int                  -- address and size (in bytes) of buffer
466         -> Int                          -- number of bytes of data in buffer
467         -> Bool                         -- flush the handle afterward?
468         -> IO ()
469
470 commitBuffer handle buf sz count flush = do
471     wantWriteableHandle "commitBuffer" handle $ \handle_ -> do
472       let fo = haFO__ handle_
473       flushConnectedBuf fo              -- ????  -SDM
474       getWriteableBuf fo                -- flush read buf if necessary
475       fo_buf     <- getBuf fo
476       fo_wptr    <- getBufWPtr fo
477       fo_bufSize <- getBufSize fo
478
479       new_wptr <-                       -- not enough room in handle buffer?
480         (if flush || (fo_bufSize - fo_wptr < count)
481             then do rc <- mayBlock fo (flushFile fo)
482                     if (rc < 0) then constructErrorAndFail "commitBuffer"
483                                 else return 0
484             else return fo_wptr )
485
486       if (flush || fo_bufSize < count)  -- committed buffer too large?
487
488             then do rc <- write_buf fo buf count
489                     if (rc < 0) then constructErrorAndFail "commitBuffer"
490                                 else return ()
491
492             else do memcpy (plusAddr fo_buf (AddrOff# new_wptr)) buf count
493                     setBufWPtr fo (new_wptr + count)
494                     return ()
495
496 write_buf fo buf 0 = return 0
497 write_buf fo buf count = do
498   rc <- mayBlock fo (write_ fo buf count)
499   if (rc > 0)
500         then  write_buf fo buf (count - rc) -- partial write
501         else  return rc
502
503 -- a version of commitBuffer that will free the buffer if an exception is received.
504 -- DON'T use this if you intend to use the buffer again!
505 checkedCommitBuffer handle buf sz count flush 
506   = catchException (commitBuffer handle buf sz count flush) 
507                    (\e -> do withHandle__ handle (\h_ -> freeBuffer h_ buf sz)
508                              throw e)
509
510 foreign import "memcpy" unsafe memcpy :: Addr -> Addr -> Int -> IO ()
511 \end{code}
512
513 Going across the border between Haskell and C is relatively costly,
514 so for block writes we pack the character strings on the Haskell-side
515 before passing the external write routine a pointer to the buffer.
516
517 \begin{code}
518 #ifdef __HUGS__
519
520 #ifdef __CONCURRENT_HASKELL__
521 /* See comment in shoveString below for explanation */
522 #warning delayed update of buffer disnae work with killThread
523 #endif
524
525 writeLines :: Handle -> Addr -> Int -> String -> IO ()
526 writeLines handle buf bufLen s =
527   let
528    shoveString :: Int -> [Char] -> IO ()
529    shoveString n ls = 
530      case ls of
531       [] -> commitAndReleaseBuffer handle buf buflen n False{-no need to flush-}
532
533       (x:xs) -> do
534         primWriteCharOffAddr buf n x
535           {- Flushing on buffer exhaustion or newlines (even if it isn't the last one) -}
536         let next_n = n + 1
537         if next_n == bufLen || x == '\n'
538          then do
539            checkedCommitBuffer hdl buf len next_n True{-needs flush-} 
540            shoveString 0 xs
541          else
542            shoveString next_n xs
543   in
544   shoveString 0 s
545
546 #else /* ndef __HUGS__ */
547
548 writeLines :: Handle -> Addr -> Int -> String -> IO ()
549 writeLines hdl buf len@(I# bufLen) s =
550   let
551    shoveString :: Int# -> [Char] -> IO ()
552    shoveString n ls = 
553      case ls of
554       [] -> commitAndReleaseBuffer hdl buf len (I# n) False{-no need to flush-}
555
556       ((C# x):xs) -> do
557         write_char buf n x
558           -- Flushing on buffer exhaustion or newlines 
559           -- (even if it isn't the last one)
560         let next_n = n +# 1#
561         if next_n ==# bufLen || x `eqChar#` '\n'#
562          then do
563            checkedCommitBuffer hdl buf len (I# next_n) True{-needs flush-} 
564            shoveString 0# xs
565          else
566            shoveString next_n xs
567   in
568   shoveString 0# s
569 #endif /* ndef __HUGS__ */
570
571 #ifdef __HUGS__
572 writeBlocks :: Handle -> Addr -> Int -> String -> IO ()
573 writeBlocks hdl buf bufLen s =
574   let
575    shoveString :: Int -> [Char] -> IO ()
576    shoveString n ls = 
577      case ls of
578       [] -> commitAndReleaseBuffer hdl buf len n False{-no need to flush-} 
579
580       (x:xs) -> do
581         primWriteCharOffAddr buf n x
582         let next_n = n + 1
583         if next_n == bufLen
584          then do
585            checkedCommitBuffer hdl buf len next_n True{-needs flush-}
586            shoveString 0 xs
587          else
588            shoveString next_n xs
589   in
590   shoveString 0 s
591
592 #else /* ndef __HUGS__ */
593
594 writeBlocks :: Handle -> Addr -> Int -> String -> IO ()
595 writeBlocks hdl buf len@(I# bufLen) s =
596   let
597    shoveString :: Int# -> [Char] -> IO ()
598    shoveString n ls = 
599      case ls of
600       [] -> commitAndReleaseBuffer hdl buf len (I# n) False{-no need to flush-} 
601
602       ((C# x):xs) -> do
603         write_char buf n x
604         let next_n = n +# 1#
605         if next_n ==# bufLen
606          then do
607            checkedCommitBuffer hdl buf len (I# next_n) True{-needs flush-}
608            shoveString 0# xs
609          else
610            shoveString next_n xs
611   in
612   shoveString 0# s
613
614 write_char :: Addr -> Int# -> Char# -> IO ()
615 write_char (A# buf#) n# c# =
616    IO $ \ s# ->
617    case (writeCharOffAddr# buf# n# c# s#) of s2# -> (# s2#, () #)
618 #endif /* ndef __HUGS__ */
619 \end{code}
620
621 Computation @hPrint hdl t@ writes the string representation of {\em t}
622 given by the @shows@ function to the file or channel managed by {\em
623 hdl}.
624
625 [ Seem to have disappeared from the 1.4 interface  - SOF 2/97 ]
626
627 \begin{code}
628 hPrint :: Show a => Handle -> a -> IO ()
629 hPrint hdl = hPutStrLn hdl . show
630 \end{code}
631
632 Derived action @hPutStrLn hdl str@ writes the string \tr{str} to
633 the handle \tr{hdl}, adding a newline at the end.
634
635 \begin{code}
636 hPutStrLn :: Handle -> String -> IO ()
637 hPutStrLn hndl str = do
638  hPutStr  hndl str
639  hPutChar hndl '\n'
640 \end{code}