Commit | Line | Data |
---|---|---|
2b829bbb | 1 | /* Copyright (C) 1995,1996,1997,1998,2000,2001, 2004, 2006 Free Software Foundation, Inc. |
0f2d19dd | 2 | * |
73be1d9e MV |
3 | * This library is free software; you can redistribute it and/or |
4 | * modify it under the terms of the GNU Lesser General Public | |
5 | * License as published by the Free Software Foundation; either | |
6 | * version 2.1 of the License, or (at your option) any later version. | |
0f2d19dd | 7 | * |
73be1d9e MV |
8 | * This library is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * Lesser General Public License for more details. | |
0f2d19dd | 12 | * |
73be1d9e MV |
13 | * You should have received a copy of the GNU Lesser General Public |
14 | * License along with this library; if not, write to the Free Software | |
92205699 | 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
73be1d9e | 16 | */ |
1bbd0b84 | 17 | |
1bbd0b84 | 18 | |
0f2d19dd JB |
19 | \f |
20 | ||
dbb605f5 | 21 | #ifdef HAVE_CONFIG_H |
4600886f RB |
22 | # include <config.h> |
23 | #endif | |
24 | ||
0f2d19dd | 25 | #include <stdio.h> |
e6e2e95a | 26 | #include <errno.h> |
bd9e24b3 | 27 | |
a0599745 | 28 | #include "libguile/_scm.h" |
4af3c6f1 | 29 | #include "libguile/dynwind.h" |
a0599745 MD |
30 | #include "libguile/pairs.h" |
31 | #include "libguile/strings.h" | |
32 | #include "libguile/throw.h" | |
20e6290e | 33 | |
a0599745 MD |
34 | #include "libguile/validate.h" |
35 | #include "libguile/error.h" | |
0f2d19dd | 36 | |
bd9e24b3 GH |
37 | #ifdef HAVE_STRING_H |
38 | #include <string.h> | |
39 | #endif | |
95b88819 GH |
40 | #ifdef HAVE_UNISTD_H |
41 | #include <unistd.h> | |
42 | #endif | |
7beabedb MG |
43 | |
44 | /* For Windows... */ | |
45 | #ifdef HAVE_IO_H | |
46 | #include <io.h> | |
47 | #endif | |
0f2d19dd JB |
48 | \f |
49 | ||
0f2d19dd JB |
50 | /* {Errors and Exceptional Conditions} |
51 | */ | |
52 | ||
0f2d19dd | 53 | |
24d1f171 | 54 | /* Scheme interface to scm_error_scm. */ |
7cb1d4d3 | 55 | void |
1bbd0b84 | 56 | scm_error (SCM key, const char *subr, const char *message, SCM args, SCM rest) |
7cb1d4d3 | 57 | { |
24d1f171 MV |
58 | scm_error_scm |
59 | (key, | |
60 | (subr == NULL) ? SCM_BOOL_F : scm_from_locale_string (subr), | |
61 | (message == NULL) ? SCM_BOOL_F : scm_from_locale_string (message), | |
62 | args, rest); | |
7cb1d4d3 | 63 | } |
0f2d19dd | 64 | |
24d1f171 | 65 | /* All errors should pass through here. */ |
3b3b36dd | 66 | SCM_DEFINE (scm_error_scm, "scm-error", 5, 0, 0, |
1e6808ea MG |
67 | (SCM key, SCM subr, SCM message, SCM args, SCM data), |
68 | "Raise an error with key @var{key}. @var{subr} can be a string\n" | |
69 | "naming the procedure associated with the error, or @code{#f}.\n" | |
70 | "@var{message} is the error message string, possibly containing\n" | |
71 | "@code{~S} and @code{~A} escapes. When an error is reported,\n" | |
72 | "these are replaced by formatting the corresponding members of\n" | |
73 | "@var{args}: @code{~A} (was @code{%s} in older versions of\n" | |
74 | "Guile) formats using @code{display} and @code{~S} (was\n" | |
75 | "@code{%S}) formats using @code{write}. @var{data} is a list or\n" | |
76 | "@code{#f} depending on @var{key}: if @var{key} is\n" | |
77 | "@code{system-error} then it should be a list containing the\n" | |
78 | "Unix @code{errno} value; If @var{key} is @code{signal} then it\n" | |
cdd8c091 MV |
79 | "should be a list containing the Unix signal number; If\n" |
80 | "@var{key} is @code{out-of-range} or @code{wrong-type-arg},\n" | |
81 | "it is a list containing the bad value; otherwise\n" | |
1e6808ea | 82 | "it will usually be @code{#f}.") |
1bbd0b84 | 83 | #define FUNC_NAME s_scm_error_scm |
c37e0e55 | 84 | { |
24d1f171 | 85 | if (scm_gc_running_p) |
a6d9e5ab | 86 | { |
24d1f171 MV |
87 | /* The error occured during GC --- abort */ |
88 | fprintf (stderr, "Guile: error during GC.\n"), | |
89 | abort (); | |
a6d9e5ab | 90 | } |
89958ad0 | 91 | |
24d1f171 MV |
92 | scm_ithrow (key, scm_list_4 (subr, message, args, data), 1); |
93 | ||
94 | /* No return, but just in case: */ | |
95 | fprintf (stderr, "Guile scm_ithrow returned!\n"); | |
96 | exit (1); | |
c37e0e55 | 97 | } |
1bbd0b84 | 98 | #undef FUNC_NAME |
c37e0e55 | 99 | |
b4e15479 SJ |
100 | #ifdef __MINGW32__ |
101 | # include "win32-socket.h" | |
102 | # define SCM_I_STRERROR(err) \ | |
103 | ((err >= WSABASEERR) ? scm_i_socket_strerror (err) : strerror (err)) | |
104 | # define SCM_I_ERRNO() \ | |
105 | (errno ? errno : scm_i_socket_errno ()) | |
106 | #else | |
107 | # define SCM_I_STRERROR(err) strerror (err) | |
108 | # define SCM_I_ERRNO() errno | |
109 | #endif /* __MINGW32__ */ | |
110 | ||
4af3c6f1 KR |
111 | /* strerror may not be thread safe, for instance in glibc (version 2.3.2) an |
112 | error number not among the known values results in a string like "Unknown | |
113 | error 9999" formed in a static buffer, which will be overwritten by a | |
114 | similar call in another thread. A test program running two threads with | |
115 | different unknown error numbers can trip this fairly quickly. | |
116 | ||
117 | Some systems don't do what glibc does, instead just giving a single | |
118 | "Unknown error" for unrecognised numbers. It doesn't seem worth trying | |
119 | to tell if that's the case, a mutex is reasonably fast, and strerror | |
120 | isn't needed very often. | |
121 | ||
122 | strerror_r (when available) could be used, it might be a touch faster | |
123 | than a frame and a mutex, though there's probably not much | |
124 | difference. */ | |
125 | ||
a1ec6916 | 126 | SCM_DEFINE (scm_strerror, "strerror", 1, 0, 0, |
1bbd0b84 | 127 | (SCM err), |
1e6808ea MG |
128 | "Return the Unix error message corresponding to @var{err}, which\n" |
129 | "must be an integer value.") | |
1bbd0b84 | 130 | #define FUNC_NAME s_scm_strerror |
efb997f5 | 131 | { |
4af3c6f1 | 132 | SCM ret; |
661ae7ab MV |
133 | scm_dynwind_begin (0); |
134 | scm_i_dynwind_pthread_mutex_lock (&scm_i_misc_mutex); | |
4af3c6f1 | 135 | |
468e87a7 | 136 | ret = scm_from_locale_string (SCM_I_STRERROR (scm_to_int (err))); |
4af3c6f1 | 137 | |
661ae7ab | 138 | scm_dynwind_end (); |
4af3c6f1 | 139 | return ret; |
efb997f5 | 140 | } |
1bbd0b84 | 141 | #undef FUNC_NAME |
efb997f5 | 142 | |
a7f54aed | 143 | SCM_GLOBAL_SYMBOL (scm_system_error_key, "system-error"); |
52859adf | 144 | void |
1bbd0b84 | 145 | scm_syserror (const char *subr) |
52859adf | 146 | { |
4af3c6f1 | 147 | SCM err = scm_from_int (SCM_I_ERRNO ()); |
01f61221 | 148 | scm_error (scm_system_error_key, |
52859adf | 149 | subr, |
70d63753 | 150 | "~A", |
4af3c6f1 KR |
151 | scm_cons (scm_strerror (err), SCM_EOL), |
152 | scm_cons (err, SCM_EOL)); | |
52859adf GH |
153 | } |
154 | ||
155 | void | |
1bbd0b84 | 156 | scm_syserror_msg (const char *subr, const char *message, SCM args, int eno) |
52859adf | 157 | { |
01f61221 | 158 | scm_error (scm_system_error_key, |
52859adf GH |
159 | subr, |
160 | message, | |
161 | args, | |
7888309b | 162 | scm_cons (scm_from_int (eno), SCM_EOL)); |
52859adf GH |
163 | } |
164 | ||
a7f54aed | 165 | SCM_GLOBAL_SYMBOL (scm_num_overflow_key, "numerical-overflow"); |
52859adf | 166 | void |
1bbd0b84 | 167 | scm_num_overflow (const char *subr) |
52859adf | 168 | { |
01f61221 | 169 | scm_error (scm_num_overflow_key, |
52859adf GH |
170 | subr, |
171 | "Numerical overflow", | |
172 | SCM_BOOL_F, | |
173 | SCM_BOOL_F); | |
174 | } | |
175 | ||
a7f54aed | 176 | SCM_GLOBAL_SYMBOL (scm_out_of_range_key, "out-of-range"); |
52859adf | 177 | void |
1bbd0b84 | 178 | scm_out_of_range (const char *subr, SCM bad_value) |
52859adf | 179 | { |
01f61221 | 180 | scm_error (scm_out_of_range_key, |
52859adf | 181 | subr, |
cdd8c091 | 182 | "Value out of range: ~S", |
1afff620 | 183 | scm_list_1 (bad_value), |
cdd8c091 | 184 | scm_list_1 (bad_value)); |
52859adf | 185 | } |
f5bf2977 | 186 | |
1e76143f GB |
187 | void |
188 | scm_out_of_range_pos (const char *subr, SCM bad_value, SCM pos) | |
189 | { | |
190 | scm_error (scm_out_of_range_key, | |
191 | subr, | |
cdd8c091 | 192 | "Argument ~A out of range: ~S", |
34d19ef6 | 193 | scm_list_2 (pos, bad_value), |
cdd8c091 | 194 | scm_list_1 (bad_value)); |
1e76143f GB |
195 | } |
196 | ||
197 | ||
a7f54aed | 198 | SCM_GLOBAL_SYMBOL (scm_args_number_key, "wrong-number-of-args"); |
0f2d19dd | 199 | void |
1bbd0b84 | 200 | scm_wrong_num_args (SCM proc) |
f5bf2977 | 201 | { |
01f61221 | 202 | scm_error (scm_args_number_key, |
f5bf2977 | 203 | NULL, |
70d63753 | 204 | "Wrong number of arguments to ~A", |
1afff620 | 205 | scm_list_1 (proc), |
f5bf2977 GH |
206 | SCM_BOOL_F); |
207 | } | |
208 | ||
9f40cd87 DH |
209 | |
210 | void | |
211 | scm_error_num_args_subr (const char *subr) | |
212 | { | |
213 | scm_error (scm_args_number_key, | |
214 | NULL, | |
215 | "Wrong number of arguments to ~A", | |
468e87a7 | 216 | scm_list_1 (scm_from_locale_string (subr)), |
9f40cd87 DH |
217 | SCM_BOOL_F); |
218 | } | |
219 | ||
220 | ||
a7f54aed | 221 | SCM_GLOBAL_SYMBOL (scm_arg_type_key, "wrong-type-arg"); |
f5bf2977 | 222 | void |
1bbd0b84 | 223 | scm_wrong_type_arg (const char *subr, int pos, SCM bad_value) |
f5bf2977 | 224 | { |
01f61221 | 225 | scm_error (scm_arg_type_key, |
f5bf2977 | 226 | subr, |
83e1ab6d | 227 | (pos == 0) ? "Wrong type: ~S" |
70d63753 | 228 | : "Wrong type argument in position ~A: ~S", |
1afff620 | 229 | (pos == 0) ? scm_list_1 (bad_value) |
e11e83f3 | 230 | : scm_list_2 (scm_from_int (pos), bad_value), |
cdd8c091 | 231 | scm_list_1 (bad_value)); |
f5bf2977 GH |
232 | } |
233 | ||
b6791b2e GB |
234 | void |
235 | scm_wrong_type_arg_msg (const char *subr, int pos, SCM bad_value, const char *szMessage) | |
236 | { | |
468e87a7 | 237 | SCM msg = scm_from_locale_string (szMessage); |
cdd8c091 MV |
238 | if (pos == 0) |
239 | { | |
240 | scm_error (scm_arg_type_key, | |
241 | subr, "Wrong type (expecting ~A): ~S", | |
242 | scm_list_2 (msg, bad_value), | |
243 | scm_list_1 (bad_value)); | |
244 | } | |
245 | else | |
246 | { | |
247 | scm_error (scm_arg_type_key, | |
248 | subr, | |
249 | "Wrong type argument in position ~A (expecting ~A): ~S", | |
250 | scm_list_3 (scm_from_int (pos), msg, bad_value), | |
251 | scm_list_1 (bad_value)); | |
252 | } | |
b6791b2e GB |
253 | } |
254 | ||
255 | ||
a7f54aed | 256 | SCM_GLOBAL_SYMBOL (scm_memory_alloc_key, "memory-allocation-error"); |
f5bf2977 | 257 | void |
1bbd0b84 | 258 | scm_memory_error (const char *subr) |
f5bf2977 | 259 | { |
468e87a7 MV |
260 | fprintf (stderr, "FATAL: memory error in %s\n", subr); |
261 | abort (); | |
f5bf2977 GH |
262 | } |
263 | ||
a7f54aed | 264 | SCM_GLOBAL_SYMBOL (scm_misc_error_key, "misc-error"); |
523f5266 | 265 | void |
1bbd0b84 | 266 | scm_misc_error (const char *subr, const char *message, SCM args) |
523f5266 GH |
267 | { |
268 | scm_error (scm_misc_error_key, subr, message, args, SCM_BOOL_F); | |
269 | } | |
270 | ||
0f2d19dd JB |
271 | void |
272 | scm_init_error () | |
0f2d19dd | 273 | { |
a0599745 MD |
274 | #include "libguile/cpp_err_symbols.c" |
275 | #include "libguile/error.x" | |
0f2d19dd JB |
276 | } |
277 | ||
89e00824 ML |
278 | |
279 | /* | |
280 | Local Variables: | |
281 | c-file-style: "gnu" | |
282 | End: | |
283 | */ |