1 {-# OPTIONS -fno-implicit-prelude #-}
5 -- -----------------------------------------------------------------------------
6 -- $Id: IO.hsc,v 1.2 2001/07/31 12:46:17 simonmar Exp $
8 -- (c) The University of Glasgow, 1992-2001
12 -- This module defines all basic IO operations.
13 -- These are needed for the IO operations exported by Prelude,
14 -- but as it happens they also do everything required by library
20 #include "GHC/Handle_hsc.h"
33 import GHC.Handle -- much of the real stuff is in here
38 import GHC.Exception ( ioError, catch, throw )
41 -- ---------------------------------------------------------------------------
42 -- Simple input operations
44 -- Computation "hReady hdl" indicates whether at least
45 -- one item is available for input from handle "hdl".
47 -- If hWaitForInput finds anything in the Handle's buffer, it
48 -- immediately returns. If not, it tries to read from the underlying
49 -- OS handle. Notice that for buffered Handles connected to terminals
50 -- this means waiting until a complete line is available.
52 hWaitForInput :: Handle -> Int -> IO Bool
53 hWaitForInput h msecs = do
54 wantReadableHandle "hReady" h $ \ handle_ -> do
55 let ref = haBuffer handle_
58 if not (bufferEmpty buf)
62 r <- throwErrnoIfMinus1Retry "hReady"
63 (inputReady (fromIntegral (haFD handle_)) (fromIntegral msecs))
66 foreign import "inputReady"
67 inputReady :: CInt -> CInt -> IO CInt
69 -- ---------------------------------------------------------------------------
72 -- hGetChar reads the next character from a handle,
73 -- blocking until a character is available.
75 hGetChar :: Handle -> IO Char
77 wantReadableHandle "hGetChar" handle $ \handle_ -> do
80 ref = haBuffer handle_
83 if not (bufferEmpty buf)
84 then hGetcBuffered fd ref buf
88 case haBufferMode handle_ of
90 new_buf <- fillReadBuffer fd True buf
91 hGetcBuffered fd ref new_buf
92 BlockBuffering _ -> do
93 new_buf <- fillReadBuffer fd False buf
94 hGetcBuffered fd ref new_buf
96 -- make use of the minimal buffer we already have
98 r <- throwErrnoIfMinus1RetryMayBlock "hGetChar"
99 (read_off (fromIntegral fd) raw 0 1)
103 else do (c,_) <- readCharFromBuffer raw 0
106 hGetcBuffered fd ref buf@Buffer{ bufBuf=b, bufRPtr=r, bufWPtr=w }
107 = do (c,r) <- readCharFromBuffer b r
108 let new_buf | r == w = buf{ bufRPtr=0, bufWPtr=0 }
109 | otherwise = buf{ bufRPtr=r }
110 writeIORef ref new_buf
113 -- ---------------------------------------------------------------------------
116 -- If EOF is reached before EOL is encountered, ignore the EOF and
117 -- return the partial line. Next attempt at calling hGetLine on the
118 -- handle will yield an EOF IO exception though.
120 -- ToDo: the unbuffered case is wrong: it doesn't lock the handle for
122 hGetLine :: Handle -> IO String
124 m <- wantReadableHandle "hGetLine" h $ \ handle_ -> do
125 case haBufferMode handle_ of
126 NoBuffering -> return Nothing
128 l <- hGetLineBuffered handle_
130 BlockBuffering _ -> do
131 l <- hGetLineBuffered handle_
134 Nothing -> hGetLineUnBuffered h
138 hGetLineBuffered handle_ = do
139 let ref = haBuffer handle_
141 hGetLineBufferedLoop handle_ ref buf []
144 hGetLineBufferedLoop handle_ ref
145 buf@Buffer{ bufRPtr=r, bufWPtr=w, bufBuf=raw } xss =
147 -- find the end-of-line character, if there is one
149 | r == w = return (False, w)
151 (c,r') <- readCharFromBuffer raw r
153 then return (True, r) -- NB. not r': don't include the '\n'
156 (eol, off) <- loop raw r
159 puts ("hGetLineBufferedLoop: r=" ++ show r ++ ", w=" ++ show w ++ ", off=" ++ show off ++ "\n")
162 xs <- unpack raw r off
164 then do if w == off + 1
165 then writeIORef ref buf{ bufRPtr=0, bufWPtr=0 }
166 else writeIORef ref buf{ bufRPtr = off + 1 }
167 return (concat (reverse (xs:xss)))
169 maybe_buf <- maybeFillReadBuffer (haFD handle_) True
170 buf{ bufWPtr=0, bufRPtr=0 }
172 -- Nothing indicates we caught an EOF, and we may have a
173 -- partial line to return.
174 Nothing -> let str = concat (reverse (xs:xss)) in
179 hGetLineBufferedLoop handle_ ref new_buf (xs:xss)
182 maybeFillReadBuffer fd is_line buf
184 (do buf <- fillReadBuffer fd is_line buf
187 (\e -> do if isEOFError e
192 unpack :: RawBuffer -> Int -> Int -> IO [Char]
193 unpack buf r 0 = return ""
194 unpack buf (I## r) (I## len) = IO $ \s -> unpack [] (len -## 1##) s
197 | i <## r = (## s, acc ##)
199 case readCharArray## buf i s of
200 (## s, ch ##) -> unpack (C## ch : acc) (i -## 1##) s
203 hGetLineUnBuffered :: Handle -> IO String
204 hGetLineUnBuffered h = do
217 if isEOFError err then
227 -- -----------------------------------------------------------------------------
230 -- hGetContents returns the list of characters corresponding to the
231 -- unread portion of the channel or file managed by the handle, which
232 -- is made semi-closed.
234 -- hGetContents on a DuplexHandle only affects the read side: you can
235 -- carry on writing to it afterwards.
237 hGetContents :: Handle -> IO String
238 hGetContents handle =
239 withHandle "hGetContents" handle $ \handle_ ->
240 case haType handle_ of
241 ClosedHandle -> ioe_closedHandle
242 SemiClosedHandle -> ioe_closedHandle
243 AppendHandle -> ioe_notReadable
244 WriteHandle -> ioe_notReadable
245 _ -> do xs <- lazyRead handle
246 return (handle_{ haType=SemiClosedHandle}, xs )
248 -- Note that someone may close the semi-closed handle (or change its
249 -- buffering), so each time these lazy read functions are pulled on,
250 -- they have to check whether the handle has indeed been closed.
252 lazyRead :: Handle -> IO String
255 withHandle "lazyRead" handle $ \ handle_ -> do
256 case haType handle_ of
257 ClosedHandle -> return (handle_, "")
258 SemiClosedHandle -> lazyRead' handle handle_
260 (IOError (Just handle) IllegalOperation "lazyRead"
261 "illegal handle type" Nothing)
263 lazyRead' h handle_ = do
264 let ref = haBuffer handle_
267 -- even a NoBuffering handle can have a char in the buffer...
270 if not (bufferEmpty buf)
271 then lazyReadHaveBuffer h handle_ fd ref buf
274 case haBufferMode handle_ of
276 -- make use of the minimal buffer we already have
279 r <- throwErrnoIfMinus1RetryMayBlock "lazyRead"
280 (read_off (fromIntegral fd) raw 0 1)
283 then do handle_ <- hClose_help handle_
285 else do (c,_) <- readCharFromBuffer raw 0
287 return (handle_, c : rest)
289 LineBuffering -> lazyReadBuffered h handle_ fd ref buf
290 BlockBuffering _ -> lazyReadBuffered h handle_ fd ref buf
292 -- we never want to block during the read, so we call fillReadBuffer with
293 -- is_line==True, which tells it to "just read what there is".
294 lazyReadBuffered h handle_ fd ref buf = do
296 (do buf <- fillReadBuffer fd True{-is_line-} buf
297 lazyReadHaveBuffer h handle_ fd ref buf
299 -- all I/O errors are discarded. Additionally, we close the handle.
300 (\e -> do handle_ <- hClose_help handle_
304 lazyReadHaveBuffer h handle_ fd ref buf = do
306 writeIORef ref buf{ bufRPtr=0, bufWPtr=0 }
307 s <- unpackAcc (bufBuf buf) (bufRPtr buf) (bufWPtr buf) more
311 unpackAcc :: RawBuffer -> Int -> Int -> [Char] -> IO [Char]
312 unpackAcc buf r 0 acc = return ""
313 unpackAcc buf (I## r) (I## len) acc = IO $ \s -> unpack acc (len -## 1##) s
316 | i <## r = (## s, acc ##)
318 case readCharArray## buf i s of
319 (## s, ch ##) -> unpack (C## ch : acc) (i -## 1##) s
321 -- ---------------------------------------------------------------------------
324 -- `hPutChar hdl ch' writes the character `ch' to the file or channel
325 -- managed by `hdl'. Characters may be buffered if buffering is
326 -- enabled for `hdl'.
328 hPutChar :: Handle -> Char -> IO ()
330 c `seq` do -- must evaluate c before grabbing the handle lock
331 wantWritableHandle "hPutChar" handle $ \ handle_ -> do
332 let fd = haFD handle_
333 case haBufferMode handle_ of
334 LineBuffering -> hPutcBuffered handle_ True c
335 BlockBuffering _ -> hPutcBuffered handle_ False c
337 withObject (castCharToCChar c) $ \buf ->
338 throwErrnoIfMinus1RetryMayBlock_ "hPutChar"
339 (c_write (fromIntegral fd) buf 1)
343 hPutcBuffered handle_ is_line c = do
344 let ref = haBuffer handle_
347 w' <- writeCharIntoBuffer (bufBuf buf) w c
348 let new_buf = buf{ bufWPtr = w' }
349 if bufferFull new_buf || is_line && c == '\n'
351 flushed_buf <- flushWriteBuffer (haFD handle_) new_buf
352 writeIORef ref flushed_buf
354 writeIORef ref new_buf
357 hPutChars :: Handle -> [Char] -> IO ()
358 hPutChars handle [] = return ()
359 hPutChars handle (c:cs) = hPutChar handle c >> hPutChars handle cs
361 -- ---------------------------------------------------------------------------
364 -- `hPutStr hdl s' writes the string `s' to the file or
365 -- hannel managed by `hdl', buffering the output if needs be.
367 -- We go to some trouble to avoid keeping the handle locked while we're
368 -- evaluating the string argument to hPutStr, in case doing so triggers another
369 -- I/O operation on the same handle which would lead to deadlock. The classic
372 -- putStr (trace "hello" "world")
374 -- so the basic scheme is this:
376 -- * copy the string into a fresh buffer,
377 -- * "commit" the buffer to the handle.
379 -- Committing may involve simply copying the contents of the new
380 -- buffer into the handle's buffer, flushing one or both buffers, or
381 -- maybe just swapping the buffers over (if the handle's buffer was
382 -- empty). See commitBuffer below.
384 hPutStr :: Handle -> String -> IO ()
385 hPutStr handle str = do
386 buffer_mode <- wantWritableHandle "hPutStr" handle
387 (\ handle_ -> do getSpareBuffer handle_)
389 (NoBuffering, _) -> do
390 hPutChars handle str -- v. slow, but we don't care
391 (LineBuffering, buf) -> do
392 writeLines handle buf str
393 (BlockBuffering _, buf) -> do
394 writeBlocks handle buf str
397 getSpareBuffer :: Handle__ -> IO (BufferMode, Buffer)
398 getSpareBuffer Handle__{haBuffer=ref,
403 NoBuffering -> return (mode, error "no buffer!")
405 bufs <- readIORef spare_ref
408 BufferListCons b rest -> do
409 writeIORef spare_ref rest
410 return ( mode, newEmptyBuffer b WriteBuffer (bufSize buf))
412 new_buf <- allocateBuffer (bufSize buf) WriteBuffer
413 return (mode, new_buf)
416 writeLines :: Handle -> Buffer -> String -> IO ()
417 writeLines hdl Buffer{ bufBuf=raw, bufSize=len } s =
419 shoveString :: Int -> [Char] -> IO ()
420 -- check n == len first, to ensure that shoveString is strict in n.
421 shoveString n cs | n == len = do
422 new_buf <- commitBuffer hdl raw len n True{-needs flush-} False
423 writeBlocks hdl new_buf cs
424 shoveString n [] = do
425 commitBuffer hdl raw len n False{-no flush-} True{-release-}
427 shoveString n (c:cs) = do
428 n' <- writeCharIntoBuffer raw n c
433 writeBlocks :: Handle -> Buffer -> String -> IO ()
434 writeBlocks hdl Buffer{ bufBuf=raw, bufSize=len } s =
436 shoveString :: Int -> [Char] -> IO ()
437 -- check n == len first, to ensure that shoveString is strict in n.
438 shoveString n cs | n == len = do
439 new_buf <- commitBuffer hdl raw len n True{-needs flush-} False
440 writeBlocks hdl new_buf cs
441 shoveString n [] = do
442 commitBuffer hdl raw len n False{-no flush-} True{-release-}
444 shoveString n (c:cs) = do
445 n' <- writeCharIntoBuffer raw n c
450 -- -----------------------------------------------------------------------------
451 -- commitBuffer handle buf sz count flush release
453 -- Write the contents of the buffer 'buf' ('sz' bytes long, containing
454 -- 'count' bytes of data) to handle (handle must be block or line buffered).
458 -- for block/line buffering,
459 -- 1. If there isn't room in the handle buffer, flush the handle
462 -- 2. If the handle buffer is empty,
464 -- then write buf directly to the device.
465 -- else swap the handle buffer with buf.
467 -- 3. If the handle buffer is non-empty, copy buf into the
468 -- handle buffer. Then, if flush != 0, flush
472 :: Handle -- handle to commit to
473 -> RawBuffer -> Int -- address and size (in bytes) of buffer
474 -> Int -- number of bytes of data in buffer
475 -> Bool -- flush the handle afterward?
476 -> Bool -- release the buffer?
479 commitBuffer hdl raw sz count flush release = do
480 wantWritableHandle "commitAndReleaseBuffer" hdl $
481 \ handle_@Handle__{ haFD=fd, haBuffer=ref, haBuffers=spare_buf_ref } -> do
484 puts ("commitBuffer: sz=" ++ show sz ++ ", count=" ++ show count
485 ++ ", flush=" ++ show flush ++ ", release=" ++ show release ++"\n")
488 old_buf@Buffer{ bufBuf=old_raw, bufRPtr=r, bufWPtr=w, bufSize=size }
492 -- enough room in handle buffer?
493 if (not flush && (size - w > count))
494 -- The > is to be sure that we never exactly fill
495 -- up the buffer, which would require a flush. So
496 -- if copying the new data into the buffer would
497 -- make the buffer full, we just flush the existing
498 -- buffer and the new data immediately, rather than
499 -- copying before flushing.
501 -- not flushing, and there's enough room in the buffer:
502 -- just copy the data in and update bufWPtr.
503 then do memcpy_baoff_ba old_raw w raw (fromIntegral count)
504 writeIORef ref old_buf{ bufWPtr = w + count }
505 return (newEmptyBuffer raw WriteBuffer sz)
507 -- else, we have to flush
508 else do flushed_buf <- flushWriteBuffer fd old_buf
511 Buffer{ bufBuf=raw, bufState=WriteBuffer,
512 bufRPtr=0, bufWPtr=count, bufSize=sz }
514 -- if: (a) we don't have to flush, and
515 -- (b) size(new buffer) == size(old buffer), and
516 -- (c) new buffer is not full,
517 -- we can just just swap them over...
518 if (not flush && sz == size && count /= sz)
520 writeIORef ref this_buf
523 -- otherwise, we have to flush the new data too,
524 -- and start with a fresh buffer
526 flushWriteBuffer fd this_buf
527 writeIORef ref flushed_buf
528 -- if the sizes were different, then allocate
529 -- a new buffer of the correct size.
531 then return (newEmptyBuffer raw WriteBuffer sz)
532 else allocateBuffer size WriteBuffer
534 -- release the buffer if necessary
535 if release && bufSize buf_ret == size
537 spare_bufs <- readIORef spare_buf_ref
538 writeIORef spare_buf_ref
539 (BufferListCons (bufBuf buf_ret) spare_bufs)
544 -- ---------------------------------------------------------------------------
545 -- Reading/writing sequences of bytes.
548 Semantics of hGetBuf:
550 - hGetBuf reads data into the buffer until either
553 (b) the buffer is full
555 It returns the amount of data actually read. This may
556 be zero in case (a). hGetBuf never raises
557 an EOF exception, it always returns zero instead.
559 If the handle is a pipe or socket, and the writing end
560 is closed, hGetBuf will behave as for condition (a).
562 Semantics of hPutBuf:
564 - hPutBuf writes data from the buffer to the handle
565 until the buffer is empty. It returns ().
567 If the handle is a pipe or socket, and the reading end is
568 closed, hPutBuf will raise a ResourceVanished exception.
569 (If this is a POSIX system, and the program has not
570 asked to ignore SIGPIPE, then a SIGPIPE may be delivered
571 instead, whose default action is to terminate the program).
574 -- ---------------------------------------------------------------------------
577 hPutBuf :: Handle -- handle to write to
578 -> Ptr a -- address of buffer
579 -> Int -- number of bytes of data in buffer
581 hPutBuf handle ptr count
582 | count <= 0 = illegalBufferSize handle "hPutBuf" count
584 wantWritableHandle "hPutBuf" handle $
585 \ handle_@Handle__{ haFD=fd, haBuffer=ref } -> do
587 old_buf@Buffer{ bufBuf=old_raw, bufRPtr=r, bufWPtr=w, bufSize=size }
590 -- enough room in handle buffer?
591 if (size - w > count)
592 -- There's enough room in the buffer:
593 -- just copy the data in and update bufWPtr.
594 then do memcpy_baoff_ptr old_raw w ptr (fromIntegral count)
595 writeIORef ref old_buf{ bufWPtr = w + count }
598 -- else, we have to flush
599 else do flushed_buf <- flushWriteBuffer fd old_buf
600 writeIORef ref flushed_buf
601 -- ToDo: should just memcpy instead of writing if possible
602 writeChunk fd ptr count
604 writeChunk :: FD -> Ptr a -> Int -> IO ()
605 writeChunk fd ptr bytes = loop 0 bytes
607 loop :: Int -> Int -> IO ()
608 loop _ bytes | bytes <= 0 = return ()
610 r <- fromIntegral `liftM`
611 throwErrnoIfMinus1RetryMayBlock "writeChunk"
612 (c_write (fromIntegral fd) (ptr `plusPtr` off) (fromIntegral bytes))
614 -- write can't return 0
615 loop (off + r) (bytes - r)
617 -- ---------------------------------------------------------------------------
620 hGetBuf :: Handle -> Ptr a -> Int -> IO Int
621 hGetBuf handle ptr count
622 | count <= 0 = illegalBufferSize handle "hGetBuf" count
624 wantReadableHandle "hGetBuf" handle $
625 \ handle_@Handle__{ haFD=fd, haBuffer=ref } -> do
626 buf@Buffer{ bufBuf=raw, bufWPtr=w, bufRPtr=r } <- readIORef ref
628 then readChunk fd ptr count
631 copied <- if (count >= avail)
633 memcpy_ptr_baoff ptr raw r (fromIntegral avail)
634 writeIORef ref buf{ bufWPtr=0, bufRPtr=0 }
637 memcpy_ptr_baoff ptr raw r (fromIntegral count)
638 writeIORef ref buf{ bufRPtr = r + count }
641 let remaining = count - copied
643 then do rest <- readChunk fd (ptr `plusPtr` copied) remaining
644 return (rest + count)
647 readChunk :: FD -> Ptr a -> Int -> IO Int
648 readChunk fd ptr bytes = loop 0 bytes
650 loop :: Int -> Int -> IO Int
651 loop off bytes | bytes <= 0 = return off
653 r <- fromIntegral `liftM`
654 throwErrnoIfMinus1RetryMayBlock "readChunk"
655 (c_read (fromIntegral fd) (ptr `plusPtr` off) (fromIntegral bytes))
659 else loop (off + r) (bytes - r)
661 slurpFile :: FilePath -> IO (Ptr (), Int)
663 handle <- openFile fname ReadMode
664 sz <- hFileSize handle
665 if sz > fromIntegral (maxBound::Int) then
666 ioError (userError "slurpFile: file too big")
668 let sz_i = fromIntegral sz
669 chunk <- mallocBytes sz_i
670 r <- hGetBuf handle chunk sz_i
674 -- ---------------------------------------------------------------------------
677 hGetBufBA :: Handle -> MutableByteArray RealWorld a -> Int -> IO Int
678 hGetBufBA handle (MutableByteArray _ _ ptr) count
679 | count <= 0 = illegalBufferSize handle "hGetBuf" count
681 wantReadableHandle "hGetBuf" handle $
682 \ handle_@Handle__{ haFD=fd, haBuffer=ref } -> do
683 buf@Buffer{ bufBuf=raw, bufWPtr=w, bufRPtr=r } <- readIORef ref
685 then readChunkBA fd ptr 0 count
688 copied <- if (count >= avail)
690 memcpy_ba_baoff ptr raw r (fromIntegral avail)
691 writeIORef ref buf{ bufWPtr=0, bufRPtr=0 }
694 memcpy_ba_baoff ptr raw r (fromIntegral count)
695 writeIORef ref buf{ bufRPtr = r + count }
698 let remaining = count - copied
700 then do rest <- readChunkBA fd ptr copied remaining
701 return (rest + count)
704 readChunkBA :: FD -> RawBuffer -> Int -> Int -> IO Int
705 readChunkBA fd ptr init_off bytes = loop init_off bytes
707 loop :: Int -> Int -> IO Int
708 loop off bytes | bytes <= 0 = return (off - init_off)
710 r <- fromIntegral `liftM`
711 throwErrnoIfMinus1RetryMayBlock "readChunk"
712 (readBA (fromIntegral fd) ptr
713 (fromIntegral off) (fromIntegral bytes))
716 then return (off - init_off)
717 else loop (off + r) (bytes - r)
719 foreign import "read_ba_wrap" unsafe
720 readBA :: FD -> RawBuffer -> Int -> CInt -> IO CInt
722 int read_ba_wrap(int fd, void *ptr, HsInt off, int size) \
723 { return read(fd, ptr + off, size); }
725 -- -----------------------------------------------------------------------------
729 :: Handle -- handle to write to
730 -> MutableByteArray RealWorld a -- buffer
731 -> Int -- number of bytes of data in buffer
734 hPutBufBA handle (MutableByteArray _ _ raw) count
735 | count <= 0 = illegalBufferSize handle "hPutBufBA" count
737 wantWritableHandle "hPutBufBA" handle $
738 \ handle_@Handle__{ haFD=fd, haBuffer=ref } -> do
740 old_buf@Buffer{ bufBuf=old_raw, bufRPtr=r, bufWPtr=w, bufSize=size }
743 -- enough room in handle buffer?
744 if (size - w > count)
745 -- There's enough room in the buffer:
746 -- just copy the data in and update bufWPtr.
747 then do memcpy_baoff_ba old_raw w raw (fromIntegral count)
748 writeIORef ref old_buf{ bufWPtr = w + count }
751 -- else, we have to flush
752 else do flushed_buf <- flushWriteBuffer fd old_buf
753 writeIORef ref flushed_buf
755 Buffer{ bufBuf=raw, bufState=WriteBuffer,
756 bufRPtr=0, bufWPtr=count, bufSize=count }
757 flushWriteBuffer fd this_buf
760 -- ---------------------------------------------------------------------------
763 foreign import "memcpy_wrap_src_off" unsafe
764 memcpy_ba_baoff :: RawBuffer -> RawBuffer -> Int -> CSize -> IO (Ptr ())
765 foreign import "memcpy_wrap_src_off" unsafe
766 memcpy_ptr_baoff :: Ptr a -> RawBuffer -> Int -> CSize -> IO (Ptr ())
767 foreign import "memcpy_wrap_dst_off" unsafe
768 memcpy_baoff_ba :: RawBuffer -> Int -> RawBuffer -> CSize -> IO (Ptr ())
769 foreign import "memcpy_wrap_dst_off" unsafe
770 memcpy_baoff_ptr :: RawBuffer -> Int -> Ptr a -> CSize -> IO (Ptr ())
773 void *memcpy_wrap_dst_off(char *dst, int dst_off, char *src, size_t sz) \
774 { return memcpy(dst+dst_off, src, sz); }
777 void *memcpy_wrap_src_off(char *dst, char *src, int src_off, size_t sz) \
778 { return memcpy(dst, src+src_off, sz); }
780 -----------------------------------------------------------------------------
783 illegalBufferSize :: Handle -> String -> Int -> IO a
784 illegalBufferSize handle fn (sz :: Int) =
785 ioException (IOError (Just handle)
787 ("illegal buffer size " ++ showsPrec 9 sz [])