[project @ 1996-01-08 20:28:12 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
28 data IOError13 = AlreadyExists String
29              | HardwareFault String
30              | IllegalOperation String
31              | InappropriateType String
32              | Interrupted String
33              | InvalidArgument String
34              | NoSuchThing String
35              | OtherError13 String
36              | PermissionDenied String
37              | ProtocolError String
38              | ResourceBusy String
39              | ResourceExhausted String
40              | ResourceVanished String
41              | SystemError String
42              | TimeExpired String
43              | UnsatisfiedConstraints String
44              | UnsupportedOperation String
45              | UserError String
46              | EOF
47              -- not really OK: deriving (Text)
48
49 instance Text IOError13 where -- NB: not interested in reading
50     showsPrec p (AlreadyExists s)       = show2 "AlreadyExists: "       s
51     showsPrec p (HardwareFault s)       = show2 "HardwareFault: "       s
52     showsPrec p (IllegalOperation s)    = show2 "IllegalOperation: "    s
53     showsPrec p (InappropriateType s)   = show2 "InappropriateType: "   s
54     showsPrec p (Interrupted s)         = show2 "Interrupted: "         s
55     showsPrec p (InvalidArgument s)     = show2 "InvalidArgument: "     s
56     showsPrec p (NoSuchThing s)         = show2 "NoSuchThing: "         s
57     showsPrec p (OtherError13 s)        = show2 "OtherError13: "        s
58     showsPrec p (PermissionDenied s)    = show2 "PermissionDenied: "    s
59     showsPrec p (ProtocolError s)       = show2 "ProtocolError: "       s
60     showsPrec p (ResourceBusy s)        = show2 "ResourceBusy: "        s
61     showsPrec p (ResourceExhausted s)   = show2 "ResourceExhausted: "   s
62     showsPrec p (ResourceVanished s)    = show2 "ResourceVanished: "    s
63     showsPrec p (SystemError s)         = show2 "SystemError: "         s
64     showsPrec p (TimeExpired s)         = show2 "TimeExpired: "         s
65     showsPrec p (UnsatisfiedConstraints s) = show2 "UnsatisfiedConstraints: " s
66     showsPrec p (UnsupportedOperation s)= show2 "UnsupportedOperation: " s
67     showsPrec p (UserError s)           = showString s
68     showsPrec p EOF                     = showString "EOF"
69
70 show2 x y = showString x . showString y
71
72 _constructError :: PrimIO IOError13
73 _constructError =
74     _casm_ ``%r = ghc_errtype;''          `thenPrimIO` \ (I# errtype#) ->
75     _casm_ ``%r = ghc_errstr;''           `thenPrimIO` \ str ->
76     let
77       msg = _unpackPS (_packCString str)
78     in
79         returnPrimIO (case errtype# of
80           ERR_ALREADYEXISTS# -> AlreadyExists msg
81           ERR_HARDWAREFAULT# -> HardwareFault msg
82           ERR_ILLEGALOPERATION# -> IllegalOperation msg
83           ERR_INAPPROPRIATETYPE# -> InappropriateType msg
84           ERR_INTERRUPTED# -> Interrupted msg
85           ERR_INVALIDARGUMENT# -> InvalidArgument msg
86           ERR_NOSUCHTHING# -> NoSuchThing msg
87           ERR_OTHERERROR# -> OtherError13 msg
88           ERR_PERMISSIONDENIED# -> PermissionDenied msg
89           ERR_PROTOCOLERROR# -> ProtocolError msg
90           ERR_RESOURCEBUSY# -> ResourceBusy msg
91           ERR_RESOURCEEXHAUSTED# -> ResourceExhausted msg
92           ERR_RESOURCEVANISHED# -> ResourceVanished msg
93           ERR_SYSTEMERROR# -> SystemError msg
94           ERR_TIMEEXPIRED# -> TimeExpired msg
95           ERR_UNSATISFIEDCONSTRAINTS# -> UnsatisfiedConstraints msg
96           ERR_UNSUPPORTEDOPERATION# -> UnsupportedOperation msg
97           ERR_EOF# -> EOF
98           _ -> OtherError13 "bad error construct"
99         )
100 \end{code}
101
102 The $String$ part of an $IOError13$ is platform-dependent.  However, to
103 provide a uniform mechanism for distinguishing among errors within
104 these broad categories, each platform-specific standard shall specify
105 the exact strings to be used for particular errors.  For errors not
106 explicitly mentioned in the standard, any descriptive string may be
107 used.