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