3 % (c) The AQUA Project, Glasgow University, 1994-1996
6 This module defines Haskell {\em handles} and the basic operations
7 which are supported for them.
15 import qualified GHCps ( unpackPS, packCString )
18 ---------------------------------
21 -- a useful little number for doing _ccall_s in IO-land:
23 stThen :: PrimIO a -> (a -> IO b) -> IO b
26 stThen (ST m) k = IO $ ST $ \ s ->
27 case (m s) of { (m_res, new_s) ->
28 case (k m_res) of { (IO (ST k_m_res)) ->
31 ---------------------------------
32 -- this one didn't make it into the 1.3 defn
34 -- The construct $try comp$ exposes errors which occur within a
35 -- computation, and which are not fully handled. It always succeeds.
37 tryIO :: IO a -> IO (Either IOError a)
38 tryIO p = catch (p >>= (return . Right)) (return . Left)
40 ---------------------------------
45 | SemiClosedHandle Addr (Addr, Int)
46 | ReadHandle Addr (Maybe BufferMode) Bool
47 | WriteHandle Addr (Maybe BufferMode) Bool
48 | AppendHandle Addr (Maybe BufferMode) Bool
49 | ReadWriteHandle Addr (Maybe BufferMode) Bool
51 instance Eq Handle{-partain:????-}
53 {-# INLINE newHandle #-}
54 {-# INLINE readHandle #-}
55 {-# INLINE writeHandle #-}
57 newHandle :: Handle__ -> IO Handle
58 readHandle :: Handle -> IO Handle__
59 writeHandle :: Handle -> Handle__ -> IO ()
61 #if defined(__CONCURRENT_HASKELL__)
63 type Handle = MVar Handle__
70 type Handle = MutableVar RealWorld Handle__
72 newHandle v = stToIO (newVar v)
73 readHandle h = stToIO (readVar h)
74 writeHandle h v = stToIO (writeVar h v)
76 #endif {- __CONCURRENT_HASKELL__ -}
78 type FilePath = String
80 filePtr :: Handle__ -> Addr
81 filePtr (SemiClosedHandle fp _) = fp
82 filePtr (ReadHandle fp _ _) = fp
83 filePtr (WriteHandle fp _ _) = fp
84 filePtr (AppendHandle fp _ _) = fp
85 filePtr (ReadWriteHandle fp _ _) = fp
87 bufferMode :: Handle__ -> Maybe BufferMode
88 bufferMode (ReadHandle _ m _) = m
89 bufferMode (WriteHandle _ m _) = m
90 bufferMode (AppendHandle _ m _) = m
91 bufferMode (ReadWriteHandle _ m _) = m
93 markHandle :: Handle__ -> Handle__
94 markHandle h@(ReadHandle fp m b)
96 | otherwise = ReadHandle fp m True
97 markHandle h@(WriteHandle fp m b)
99 | otherwise = WriteHandle fp m True
100 markHandle h@(AppendHandle fp m b)
102 | otherwise = AppendHandle fp m True
103 markHandle h@(ReadWriteHandle fp m b)
105 | otherwise = ReadWriteHandle fp m True
107 -------------------------------------------
109 stdin, stdout, stderr :: Handle
111 stdin = unsafePerformPrimIO (
112 _ccall_ getLock (``stdin''::Addr) 0 >>= \ rc ->
114 0 -> new_handle ClosedHandle
115 1 -> new_handle (ReadHandle ``stdin'' Nothing False)
116 _ -> constructError "stdin" >>= \ ioError ->
117 new_handle (ErrorHandle ioError)
122 new_handle x = ioToST (newHandle x)
124 stdout = unsafePerformPrimIO (
125 _ccall_ getLock (``stdout''::Addr) 1 >>= \ rc ->
127 0 -> new_handle ClosedHandle
128 1 -> new_handle (WriteHandle ``stdout'' Nothing False)
129 _ -> constructError "stdout" >>= \ ioError ->
130 new_handle (ErrorHandle ioError)
135 new_handle x = ioToST (newHandle x)
137 stderr = unsafePerformPrimIO (
138 _ccall_ getLock (``stderr''::Addr) 1 >>= \ rc ->
140 0 -> new_handle ClosedHandle
141 1 -> new_handle (WriteHandle ``stderr'' (Just NoBuffering) False)
142 _ -> constructError "stderr" >>= \ ioError ->
143 new_handle (ErrorHandle ioError)
148 new_handle x = ioToST (newHandle x)
152 Three handles are allocated during program initialisation. The first
153 two manage input or output from the Haskell program's standard input
154 or output channel respectively. The third manages output to the
155 standard error channel. These handles are initially open.
157 \subsubsection[OpeningClosing]{Opening and Closing Files}
161 data IOMode = ReadMode | WriteMode | AppendMode | ReadWriteMode
162 deriving (Eq, Ord, Ix, Enum, Read, Show)
164 openFile :: FilePath -> IOMode -> IO Handle
167 stToIO (_ccall_ openFile f m') >>= \ ptr ->
168 if ptr /= ``NULL'' then
169 newHandle (htype ptr Nothing False)
171 stToIO (constructError "openFile") >>= \ ioError ->
173 improved_error -- a HACK, I guess
175 AlreadyExists msg -> AlreadyExists (msg ++ ": " ++ f)
176 NoSuchThing msg -> NoSuchThing (msg ++ ": " ++ f)
177 PermissionDenied msg -> PermissionDenied (msg ++ ": " ++ f)
186 ReadWriteMode -> "r+"
189 ReadMode -> ReadHandle
190 WriteMode -> WriteHandle
191 AppendMode -> AppendHandle
192 ReadWriteMode -> ReadWriteHandle
196 Computation $openFile file mode$ allocates and returns a new, open
197 handle to manage the file {\em file}. It manages input if {\em mode}
198 is $ReadMode$, output if {\em mode} is $WriteMode$ or $AppendMode$,
199 and both input and output if mode is $ReadWriteMode$.
201 If the file does not exist and it is opened for output, it should be
202 created as a new file. If {\em mode} is $WriteMode$ and the file
203 already exists, then it should be truncated to zero length. The
204 handle is positioned at the end of the file if {\em mode} is
205 $AppendMode$, and otherwise at the beginning (in which case its
206 internal position is 0).
208 Implementations should enforce, locally to the Haskell process,
209 multiple-reader single-writer locking on files, which is to say that
210 there may either be many handles on the same file which manage input,
211 or just one handle on the file which manages output. If any open or
212 semi-closed handle is managing a file for output, no new handle can be
213 allocated for that file. If any open or semi-closed handle is
214 managing a file for input, new handles can only be allocated if they
215 do not manage output.
217 Two files are the same if they have the same absolute name. An
218 implementation is free to impose stricter conditions.
222 hClose :: Handle -> IO ()
225 readHandle handle >>= \ htype ->
226 writeHandle handle ClosedHandle >>
228 ErrorHandle ioError ->
231 fail (IllegalOperation "handle is closed")
232 SemiClosedHandle fp (buf,_) ->
233 (if buf /= ``NULL'' then
236 returnPrimIO ()) `stThen` \ () ->
237 if fp /= ``NULL'' then
238 _ccall_ closeFile fp `stThen` \ rc ->
242 constructErrorAndFail "hClose"
246 _ccall_ closeFile (filePtr other) `stThen` \ rc ->
250 constructErrorAndFail "hClose"
254 Computation $hClose hdl$ makes handle {\em hdl} closed. Before the
255 computation finishes, any items buffered for output and not already
256 sent to the operating system are flushed as for $flush$.
258 \subsubsection[EOF]{Detecting the End of Input}
262 hFileSize :: Handle -> IO Integer
264 readHandle handle >>= \ htype ->
266 ErrorHandle ioError ->
267 writeHandle handle htype >>
270 writeHandle handle htype >>
271 fail (IllegalOperation "handle is closed")
272 SemiClosedHandle _ _ ->
273 writeHandle handle htype >>
274 fail (IllegalOperation "handle is closed")
276 -- HACK! We build a unique MP_INT of the right shape to hold
277 -- a single unsigned word, and we let the C routine change the data bits
278 _casm_ ``%r = 1;'' `stThen` \ (I# hack#) ->
279 case int2Integer# hack# of
280 result@(J# _ _ d#) ->
282 bogus_bounds = (error "fileSize"::(Int,Int))
284 _ccall_ fileSize (filePtr other) (ByteArray bogus_bounds d#)
286 writeHandle handle htype >>
290 constructErrorAndFail "hFileSize"
294 For a handle {\em hdl} which attached to a physical file, $hFileSize
295 hdl$ returns the size of {\em hdl} in terms of the number of items
296 which can be read from {\em hdl}.
300 hIsEOF :: Handle -> IO Bool
302 readHandle handle >>= \ htype ->
304 ErrorHandle ioError ->
305 writeHandle handle htype >>
308 writeHandle handle htype >>
309 fail (IllegalOperation "handle is closed")
310 SemiClosedHandle _ _ ->
311 writeHandle handle htype >>
312 fail (IllegalOperation "handle is closed")
314 writeHandle handle htype >>
315 fail (IllegalOperation "handle is not open for reading")
316 AppendHandle _ _ _ ->
317 writeHandle handle htype >>
318 fail (IllegalOperation "handle is not open for reading")
320 _ccall_ fileEOF (filePtr other) `stThen` \ rc ->
321 writeHandle handle (markHandle htype) >>
325 _ -> constructErrorAndFail "hIsEOF"
332 For a readable handle {\em hdl}, computation $hIsEOF hdl$ returns
333 $True$ if no further input can be taken from {\em hdl} or for a
334 physical file, if the current I/O position is equal to the length of
335 the file. Otherwise, it returns $False$.
337 \subsubsection[Buffering]{Buffering Operations}
339 Three kinds of buffering are supported: line-buffering,
340 block-buffering or no-buffering. These modes have the following effects.
341 For output, items are written out from the internal buffer
342 according to the buffer mode:
344 \item[line-buffering] the entire output buffer is written
345 out whenever a newline is output, the output buffer overflows,
346 a flush is issued, or the handle is closed.
348 \item[block-buffering] the entire output buffer is written out whenever
349 it overflows, a flush is issued, or the handle
352 \item[no-buffering] output is written immediately, and never stored
353 in the output buffer.
356 The output buffer is emptied as soon as it has been written out.
358 Similarly, input occurs according to the buffer mode for handle {\em hdl}.
360 \item[line-buffering] when the input buffer for {\em hdl} is not empty,
361 the next item is obtained from the buffer;
362 otherwise, when the input buffer is empty,
363 characters up to and including the next newline
364 character are read into the buffer. No characters
365 are available until the newline character is
367 \item[block-buffering] when the input buffer for {\em hdl} becomes empty,
368 the next block of data is read into this buffer.
369 \item[no-buffering] the next input item is read and returned.
371 For most implementations, physical files will normally be block-buffered
372 and terminals will normally be line-buffered.
376 data BufferMode = NoBuffering | LineBuffering | BlockBuffering (Maybe Int)
377 deriving (Eq, Ord, Read, Show)
379 hSetBuffering :: Handle -> BufferMode -> IO ()
381 hSetBuffering handle mode =
383 (BlockBuffering (Just n))
384 | n <= 0 -> fail (InvalidArgument "illegal buffer size")
386 readHandle handle >>= \ htype ->
387 if isMarked htype then
388 writeHandle handle htype >>
389 fail (UnsupportedOperation "can't set buffering for a dirty handle")
392 ErrorHandle ioError ->
393 writeHandle handle htype >>
396 writeHandle handle htype >>
397 fail (IllegalOperation "handle is closed")
398 SemiClosedHandle _ _ ->
399 writeHandle handle htype >>
400 fail (IllegalOperation "handle is closed")
402 _ccall_ setBuffering (filePtr other) bsize
405 writeHandle handle ((hcon other) (filePtr other) (Just mode) True)
409 writeHandle handle htype >>
410 constructErrorAndFail "hSetBuffering"
413 isMarked :: Handle__ -> Bool
414 isMarked (ReadHandle fp m b) = b
415 isMarked (WriteHandle fp m b) = b
416 isMarked (AppendHandle fp m b) = b
417 isMarked (ReadWriteHandle fp m b) = b
423 BlockBuffering Nothing -> -2
424 BlockBuffering (Just n) -> n
426 hcon :: Handle__ -> (Addr -> (Maybe BufferMode) -> Bool -> Handle__)
427 hcon (ReadHandle _ _ _) = ReadHandle
428 hcon (WriteHandle _ _ _) = WriteHandle
429 hcon (AppendHandle _ _ _) = AppendHandle
430 hcon (ReadWriteHandle _ _ _) = ReadWriteHandle
434 Computation $hSetBuffering hdl mode$ sets the mode of buffering for
435 handle {\em hdl} on subsequent reads and writes.
439 If {\em mode} is $LineBuffering$, line-buffering should be
442 If {\em mode} is $BlockBuffering$ {\em size}, then block-buffering
443 should be enabled if possible. The size of the buffer is {\em n} items
444 if {\em size} is $Just${\em n} and is otherwise implementation-dependent.
446 If {\em mode} is $NoBuffering$, then buffering is disabled if possible.
449 If the buffer mode is changed from $BlockBuffering$ or $LineBuffering$
450 to $NoBuffering$, then any items in the output buffer are written to
451 the device, and any items in the input buffer are discarded. The
452 default buffering mode when a handle is opened is
453 implementation-dependent and may depend on the object which is
454 attached to that handle.
458 hFlush :: Handle -> IO ()
460 readHandle handle >>= \ htype ->
462 ErrorHandle ioError ->
463 writeHandle handle htype >>
466 writeHandle handle htype >>
467 fail (IllegalOperation "handle is closed")
468 SemiClosedHandle _ _ ->
469 writeHandle handle htype >>
470 fail (IllegalOperation "handle is closed")
472 _ccall_ flushFile (filePtr other) `stThen` \ rc ->
473 writeHandle handle (markHandle htype) >>
477 constructErrorAndFail "hFlush"
481 Computation $flush hdl$ causes any items
482 buffered for output in handle {\em hdl} to be sent immediately to
483 the operating system.
485 \subsubsection[Seeking]{Repositioning Handles}
489 data HandlePosn = HandlePosn Handle Int
491 instance Eq HandlePosn{-partain-}
493 hGetPosn :: Handle -> IO HandlePosn
495 readHandle handle >>= \ htype ->
497 ErrorHandle ioError ->
498 writeHandle handle htype >>
501 writeHandle handle htype >>
502 fail (IllegalOperation "handle is closed")
503 SemiClosedHandle _ _ ->
504 writeHandle handle htype >>
505 fail (IllegalOperation "handle is closed")
507 _ccall_ getFilePosn (filePtr other) `stThen` \ posn ->
508 writeHandle handle htype >>
510 return (HandlePosn handle posn)
512 constructErrorAndFail "hGetPosn"
514 hSetPosn :: HandlePosn -> IO ()
515 hSetPosn (HandlePosn handle posn) =
516 readHandle handle >>= \ htype ->
518 ErrorHandle ioError ->
519 writeHandle handle htype >>
522 writeHandle handle htype >>
523 fail (IllegalOperation "handle is closed")
524 SemiClosedHandle _ _ ->
525 writeHandle handle htype >>
526 fail (IllegalOperation "handle is closed")
527 AppendHandle _ _ _ ->
528 writeHandle handle htype >>
529 fail (IllegalOperation "handle is not seekable")
531 _ccall_ setFilePosn (filePtr other) posn `stThen` \ rc ->
532 writeHandle handle (markHandle htype) >>
536 constructErrorAndFail "hSetPosn"
540 Computation $hGetPosn hdl$ returns the current I/O
541 position of {\em hdl} as an abstract position. Computation
542 $hSetPosn p$ sets the position of {\em hdl}
543 to a previously obtained position {\em p}.
547 data SeekMode = AbsoluteSeek | RelativeSeek | SeekFromEnd
548 deriving (Eq, Ord, Ix, Enum, Read, Show)
550 hSeek :: Handle -> SeekMode -> Integer -> IO ()
551 hSeek handle mode offset@(J# _ s# d#) =
552 readHandle handle >>= \ htype ->
554 ErrorHandle ioError ->
555 writeHandle handle htype >>
558 writeHandle handle htype >>
559 fail (IllegalOperation "handle is closed")
560 SemiClosedHandle _ _ ->
561 writeHandle handle htype >>
562 fail (IllegalOperation "handle is closed")
563 AppendHandle _ _ _ ->
564 writeHandle handle htype >>
565 fail (IllegalOperation "handle is not seekable")
567 _ccall_ seekFile (filePtr other) whence (I# s#) (ByteArray (0,0) d#)
569 writeHandle handle (markHandle htype) >>
573 constructErrorAndFail "hSeek"
576 whence = case mode of
577 AbsoluteSeek -> ``SEEK_SET''
578 RelativeSeek -> ``SEEK_CUR''
579 SeekFromEnd -> ``SEEK_END''
583 Computation $hSeek hdl mode i$ sets the position of handle
584 {\em hdl} depending on $mode$. If {\em mode} is
586 \item[{\bf AbsoluteSeek}] The position of {\em hdl} is set to {\em i}.
587 \item[{\bf RelativeSeek}] The position of {\em hdl} is set to offset {\em i} from
588 the current position.
589 \item[{\bf SeekToEnd}] The position of {\em hdl} is set to offset {\em i} from
591 \item[{\bf SeekFromBeginning}] The position of {\em hdl} is set to offset {\em i} from
592 the beginning of the file.
595 Some handles may not be seekable $hIsSeekable$, or only support a
596 subset of the possible positioning operations (e.g. it may only be
597 possible to seek to the end of a tape, or to a positive offset from
598 the beginning or current position).
600 It is not possible to set a negative I/O position, or for a physical
601 file, an I/O position beyond the current end-of-file.
603 \subsubsection[Query]{Handle Properties}
607 hIsOpen :: Handle -> IO Bool
609 readHandle handle >>= \ htype ->
611 ErrorHandle ioError ->
612 writeHandle handle htype >>
615 writeHandle handle htype >>
617 SemiClosedHandle _ _ ->
618 writeHandle handle htype >>
621 writeHandle handle htype >>
624 hIsClosed :: Handle -> IO Bool
626 readHandle handle >>= \ htype ->
628 ErrorHandle ioError ->
629 writeHandle handle htype >>
632 writeHandle handle htype >>
635 writeHandle handle htype >>
638 hIsReadable :: Handle -> IO Bool
640 readHandle handle >>= \ htype ->
642 ErrorHandle ioError ->
643 writeHandle handle htype >>
646 writeHandle handle htype >>
647 fail (IllegalOperation "handle is closed")
648 SemiClosedHandle _ _ ->
649 writeHandle handle htype >>
650 fail (IllegalOperation "handle is closed")
652 writeHandle handle htype >>
653 return (isReadable other)
655 isReadable (ReadHandle _ _ _) = True
656 isReadable (ReadWriteHandle _ _ _) = True
659 hIsWritable :: Handle -> IO Bool
661 readHandle handle >>= \ htype ->
663 ErrorHandle ioError ->
664 writeHandle handle htype >>
667 writeHandle handle htype >>
668 fail (IllegalOperation "handle is closed")
669 SemiClosedHandle _ _ ->
670 writeHandle handle htype >>
671 fail (IllegalOperation "handle is closed")
673 writeHandle handle htype >>
674 return (isWritable other)
676 isWritable (AppendHandle _ _ _) = True
677 isWritable (WriteHandle _ _ _) = True
678 isWritable (ReadWriteHandle _ _ _) = True
681 getBufferMode :: Handle__ -> PrimIO Handle__
682 getBufferMode htype =
683 case bufferMode htype of
684 Just x -> returnPrimIO htype
686 _ccall_ getBufferMode (filePtr htype) `thenPrimIO` \ rc ->
690 0 -> Just NoBuffering
691 -1 -> Just LineBuffering
692 -2 -> Just (BlockBuffering Nothing)
694 n -> Just (BlockBuffering (Just n))
696 returnPrimIO (case htype of
697 ReadHandle fp _ b -> ReadHandle fp mode b
698 WriteHandle fp _ b -> WriteHandle fp mode b
699 AppendHandle fp _ b -> AppendHandle fp mode b
700 ReadWriteHandle fp _ b -> ReadWriteHandle fp mode b)
702 hIsBlockBuffered :: Handle -> IO (Bool,Maybe Int)
703 hIsBlockBuffered handle =
704 readHandle handle >>= \ htype ->
706 ErrorHandle ioError ->
707 writeHandle handle htype >>
710 writeHandle handle htype >>
711 fail (IllegalOperation "handle is closed")
712 SemiClosedHandle _ _ ->
713 writeHandle handle htype >>
714 fail (IllegalOperation "handle is closed")
716 getBufferMode other `stThen` \ other ->
717 case bufferMode other of
718 Just (BlockBuffering size) ->
719 writeHandle handle other >>
722 writeHandle handle other >>
723 return (False, Nothing)
725 constructErrorAndFail "hIsBlockBuffered"
727 hIsLineBuffered :: Handle -> IO Bool
728 hIsLineBuffered handle =
729 readHandle handle >>= \ htype ->
731 ErrorHandle ioError ->
732 writeHandle handle htype >>
735 writeHandle handle htype >>
736 fail (IllegalOperation "handle is closed")
737 SemiClosedHandle _ _ ->
738 writeHandle handle htype >>
739 fail (IllegalOperation "handle is closed")
741 getBufferMode other `stThen` \ other ->
742 case bufferMode other of
743 Just LineBuffering ->
744 writeHandle handle other >>
747 writeHandle handle other >>
750 constructErrorAndFail "hIsLineBuffered"
752 hIsNotBuffered :: Handle -> IO Bool
753 hIsNotBuffered handle =
754 readHandle handle >>= \ htype ->
756 ErrorHandle ioError ->
757 writeHandle handle htype >>
760 writeHandle handle htype >>
761 fail (IllegalOperation "handle is closed")
762 SemiClosedHandle _ _ ->
763 writeHandle handle htype >>
764 fail (IllegalOperation "handle is closed")
766 getBufferMode other `stThen` \ other ->
767 case bufferMode other of
769 writeHandle handle other >>
772 writeHandle handle other >>
775 constructErrorAndFail "hIsNotBuffered"
777 hGetBuffering :: Handle -> IO BufferMode
779 readHandle hndl >>= \ htype ->
781 ErrorHandle ioError ->
782 writeHandle hndl htype >>
785 writeHandle hndl htype >>
786 fail (IllegalOperation "handle is closed")
787 SemiClosedHandle _ _ ->
788 writeHandle hndl htype >>
789 fail (IllegalOperation "handle is closed")
791 getBufferMode other `stThen` \ other ->
792 case bufferMode other of
794 writeHandle hndl other >>
797 constructErrorAndFail "hGetBuffering"
799 hIsSeekable :: Handle -> IO Bool
801 readHandle handle >>= \ htype ->
803 ErrorHandle ioError ->
804 writeHandle handle htype >>
807 writeHandle handle htype >>
808 fail (IllegalOperation "handle is closed")
809 SemiClosedHandle _ _ ->
810 writeHandle handle htype >>
811 fail (IllegalOperation "handle is closed")
812 AppendHandle _ _ _ ->
813 writeHandle handle htype >>
816 _ccall_ seekFileP (filePtr other) `stThen` \ rc ->
817 writeHandle handle htype >>
821 _ -> constructErrorAndFail "hIsSeekable"
825 A number of operations return information about the properties of a
826 handle. Each of these operations returns $True$ if the
827 handle has the specified property, and $False$
830 Computation $hIsBlockBuffered hdl$ returns $( False, Nothing )$ if
831 {\em hdl} is not block-buffered. Otherwise it returns
832 $( True, size )$, where {\em size} is $Nothing$ for default buffering, and
833 $( Just n )$ for block-buffering of {\em n} bytes.
836 -------------------------------------------------------------------
838 = AlreadyExists String
839 | HardwareFault String
840 | IllegalOperation String
841 | InappropriateType String
843 | InvalidArgument String
846 | PermissionDenied String
847 | ProtocolError String
848 | ResourceBusy String
849 | ResourceExhausted String
850 | ResourceVanished String
853 | UnsatisfiedConstraints String
854 | UnsupportedOperation String
858 instance Eq IOError where
859 -- I don't know what the (pointless) idea is here,
860 -- presumably just compare them by their tags (WDP)
861 a == b = tag a == tag b
863 tag (AlreadyExists _) = (1::Int)
864 tag (HardwareFault _) = 2
865 tag (IllegalOperation _) = 3
866 tag (InappropriateType _) = 4
867 tag (Interrupted _) = 5
868 tag (InvalidArgument _) = 6
869 tag (NoSuchThing _) = 7
870 tag (OtherError _) = 8
871 tag (PermissionDenied _) = 9
872 tag (ProtocolError _) = 10
873 tag (ResourceBusy _) = 11
874 tag (ResourceExhausted _) = 12
875 tag (ResourceVanished _) = 13
876 tag (SystemError _) = 14
877 tag (TimeExpired _) = 15
878 tag (UnsatisfiedConstraints _) = 16
879 tag (UnsupportedOperation _) = 17
880 tag (UserError _) = 18
883 instance Show IOError where
884 showsPrec p (AlreadyExists s) = show2 "AlreadyExists: " s
885 showsPrec p (HardwareFault s) = show2 "HardwareFault: " s
886 showsPrec p (IllegalOperation s) = show2 "IllegalOperation: " s
887 showsPrec p (InappropriateType s) = show2 "InappropriateType: " s
888 showsPrec p (Interrupted s) = show2 "Interrupted: " s
889 showsPrec p (InvalidArgument s) = show2 "InvalidArgument: " s
890 showsPrec p (NoSuchThing s) = show2 "NoSuchThing: " s
891 showsPrec p (OtherError s) = show2 "OtherError: " s
892 showsPrec p (PermissionDenied s) = show2 "PermissionDenied: " s
893 showsPrec p (ProtocolError s) = show2 "ProtocolError: " s
894 showsPrec p (ResourceBusy s) = show2 "ResourceBusy: " s
895 showsPrec p (ResourceExhausted s) = show2 "ResourceExhausted: " s
896 showsPrec p (ResourceVanished s) = show2 "ResourceVanished: " s
897 showsPrec p (SystemError s) = show2 "SystemError: " s
898 showsPrec p (TimeExpired s) = show2 "TimeExpired: " s
899 showsPrec p (UnsatisfiedConstraints s) = show2 "UnsatisfiedConstraints: " s
900 showsPrec p (UnsupportedOperation s)= show2 "UnsupportedOperation: " s
901 showsPrec p (UserError s) = showString s
902 showsPrec p EOF = showString "EOF"
904 show2 x y = showString x . showString y
908 The @String@ part of an @IOError@ is platform-dependent. However, to
909 provide a uniform mechanism for distinguishing among errors within
910 these broad categories, each platform-specific standard shall specify
911 the exact strings to be used for particular errors. For errors not
912 explicitly mentioned in the standard, any descriptive string may be
915 SOF 4/96 - added argument to indicate function that flagged error
917 constructErrorAndFail :: String -> IO a
918 constructError :: String -> PrimIO IOError
920 constructErrorAndFail call_site
921 = stToIO (constructError call_site) >>= \ io_error ->
924 constructError call_site
925 = _casm_ ``%r = ghc_errtype;'' >>= \ (I# errtype#) ->
926 _casm_ ``%r = ghc_errstr;'' >>= \ str ->
928 msg = call_site ++ ':' : ' ' : GHCps.unpackPS (GHCps.packCString str)
930 return (case errtype# of
931 ERR_ALREADYEXISTS# -> AlreadyExists msg
932 ERR_HARDWAREFAULT# -> HardwareFault msg
933 ERR_ILLEGALOPERATION# -> IllegalOperation msg
934 ERR_INAPPROPRIATETYPE# -> InappropriateType msg
935 ERR_INTERRUPTED# -> Interrupted msg
936 ERR_INVALIDARGUMENT# -> InvalidArgument msg
937 ERR_NOSUCHTHING# -> NoSuchThing msg
938 ERR_OTHERERROR# -> OtherError msg
939 ERR_PERMISSIONDENIED# -> PermissionDenied msg
940 ERR_PROTOCOLERROR# -> ProtocolError msg
941 ERR_RESOURCEBUSY# -> ResourceBusy msg
942 ERR_RESOURCEEXHAUSTED# -> ResourceExhausted msg
943 ERR_RESOURCEVANISHED# -> ResourceVanished msg
944 ERR_SYSTEMERROR# -> SystemError msg
945 ERR_TIMEEXPIRED# -> TimeExpired msg
946 ERR_UNSATISFIEDCONSTRAINTS# -> UnsatisfiedConstraints msg
947 ERR_UNSUPPORTEDOPERATION# -> UnsupportedOperation msg
949 _ -> OtherError "bad error construct"