[project @ 1996-01-18 16:33:17 by partain]
[ghc-hetmet.git] / ghc / lib / prelude / PreludeIOError.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1994
3 %
4 \section[PrelIOErr]{I/O Errors}
5
6 Haskell 1.2 does not provide standard error values for I/O errors.
7 This is unacceptable for portable implementations which indulge in
8 non-trivial I/O.  The $IOError$ type has therefore been extended from
9 Haskell 1.2, and possible error values have been identified for all
10 standard operations.
11
12 \begin{code}
13 #include "error.h"
14
15 module PreludeIOError (IOError13(..), _constructError) where
16
17 import Cls
18 import Core
19 import IChar
20 import IInt
21 import IList
22 import List             ( (++) )
23 import PS
24 import Prel             ( (.), not )
25 import PreludeGlaST
26 import Text
27 import TyArray
28 import TyComplex
29
30 data IOError13 = AlreadyExists String
31              | HardwareFault String
32              | IllegalOperation String
33              | InappropriateType String
34              | Interrupted String
35              | InvalidArgument String
36              | NoSuchThing String
37              | OtherError13 String
38              | PermissionDenied String
39              | ProtocolError String
40              | ResourceBusy String
41              | ResourceExhausted String
42              | ResourceVanished String
43              | SystemError String
44              | TimeExpired String
45              | UnsatisfiedConstraints String
46              | UnsupportedOperation String
47              | UserError String
48              | EOF
49              -- not really OK: deriving (Text)
50
51 instance Text IOError13 where -- NB: not interested in reading
52     readsPrec p = error "readsPrec: IOError13"
53
54     showsPrec p (AlreadyExists s)       = show2 "AlreadyExists: "       s
55     showsPrec p (HardwareFault s)       = show2 "HardwareFault: "       s
56     showsPrec p (IllegalOperation s)    = show2 "IllegalOperation: "    s
57     showsPrec p (InappropriateType s)   = show2 "InappropriateType: "   s
58     showsPrec p (Interrupted s)         = show2 "Interrupted: "         s
59     showsPrec p (InvalidArgument s)     = show2 "InvalidArgument: "     s
60     showsPrec p (NoSuchThing s)         = show2 "NoSuchThing: "         s
61     showsPrec p (OtherError13 s)        = show2 "OtherError13: "        s
62     showsPrec p (PermissionDenied s)    = show2 "PermissionDenied: "    s
63     showsPrec p (ProtocolError s)       = show2 "ProtocolError: "       s
64     showsPrec p (ResourceBusy s)        = show2 "ResourceBusy: "        s
65     showsPrec p (ResourceExhausted s)   = show2 "ResourceExhausted: "   s
66     showsPrec p (ResourceVanished s)    = show2 "ResourceVanished: "    s
67     showsPrec p (SystemError s)         = show2 "SystemError: "         s
68     showsPrec p (TimeExpired s)         = show2 "TimeExpired: "         s
69     showsPrec p (UnsatisfiedConstraints s) = show2 "UnsatisfiedConstraints: " s
70     showsPrec p (UnsupportedOperation s)= show2 "UnsupportedOperation: " s
71     showsPrec p (UserError s)           = showString s
72     showsPrec p EOF                     = showString "EOF"
73
74     readList = _readList (readsPrec 0)
75     showList = _showList (showsPrec 0)
76
77 show2 x y = showString x . showString y
78
79 _constructError :: PrimIO IOError13
80 _constructError =
81     _casm_ ``%r = ghc_errtype;''          `thenPrimIO` \ (I# errtype#) ->
82     _casm_ ``%r = ghc_errstr;''           `thenPrimIO` \ str ->
83     let
84       msg = _unpackPS (_packCString str)
85     in
86         returnPrimIO (case errtype# of
87           ERR_ALREADYEXISTS# -> AlreadyExists msg
88           ERR_HARDWAREFAULT# -> HardwareFault msg
89           ERR_ILLEGALOPERATION# -> IllegalOperation msg
90           ERR_INAPPROPRIATETYPE# -> InappropriateType msg
91           ERR_INTERRUPTED# -> Interrupted msg
92           ERR_INVALIDARGUMENT# -> InvalidArgument msg
93           ERR_NOSUCHTHING# -> NoSuchThing msg
94           ERR_OTHERERROR# -> OtherError13 msg
95           ERR_PERMISSIONDENIED# -> PermissionDenied msg
96           ERR_PROTOCOLERROR# -> ProtocolError msg
97           ERR_RESOURCEBUSY# -> ResourceBusy msg
98           ERR_RESOURCEEXHAUSTED# -> ResourceExhausted msg
99           ERR_RESOURCEVANISHED# -> ResourceVanished msg
100           ERR_SYSTEMERROR# -> SystemError msg
101           ERR_TIMEEXPIRED# -> TimeExpired msg
102           ERR_UNSATISFIEDCONSTRAINTS# -> UnsatisfiedConstraints msg
103           ERR_UNSUPPORTEDOPERATION# -> UnsupportedOperation msg
104           ERR_EOF# -> EOF
105           _ -> OtherError13 "bad error construct"
106         )
107 \end{code}
108
109 The $String$ part of an $IOError13$ is platform-dependent.  However, to
110 provide a uniform mechanism for distinguishing among errors within
111 these broad categories, each platform-specific standard shall specify
112 the exact strings to be used for particular errors.  For errors not
113 explicitly mentioned in the standard, any descriptive string may be
114 used.