1 /* Copyright (C) 1995,1996,1997,1998,2000,2001 Free Software Foundation, Inc.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
8 * This program 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
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
52 #include "libguile/_scm.h"
53 #include "libguile/pairs.h"
54 #include "libguile/strings.h"
55 #include "libguile/throw.h"
57 #include "libguile/validate.h"
58 #include "libguile/error.h"
74 /* {Errors and Exceptional Conditions}
78 /* All errors should pass through here. */
80 scm_error (SCM key
, const char *subr
, const char *message
, SCM args
, SCM rest
)
85 /* The error occured during GC --- abort */
86 fprintf (stderr
, "Error in %s during GC: %s\n",
87 subr
? subr
: "unknown function",
88 message
? message
: "<empty message>");
91 arg_list
= scm_list_4 (subr
? scm_makfrom0str (subr
) : SCM_BOOL_F
,
92 message
? scm_makfrom0str (message
) : SCM_BOOL_F
,
95 scm_ithrow (key
, arg_list
, 1);
97 /* No return, but just in case: */
99 const char msg
[] = "guile:scm_error:scm_ithrow returned!\n";
101 write (2, msg
, (sizeof msg
) - 1);
106 /* Scheme interface to scm_error. */
107 SCM_DEFINE (scm_error_scm
, "scm-error", 5, 0, 0,
108 (SCM key
, SCM subr
, SCM message
, SCM args
, SCM data
),
109 "Raise an error with key @var{key}. @var{subr} can be a string\n"
110 "naming the procedure associated with the error, or @code{#f}.\n"
111 "@var{message} is the error message string, possibly containing\n"
112 "@code{~S} and @code{~A} escapes. When an error is reported,\n"
113 "these are replaced by formatting the corresponding members of\n"
114 "@var{args}: @code{~A} (was @code{%s} in older versions of\n"
115 "Guile) formats using @code{display} and @code{~S} (was\n"
116 "@code{%S}) formats using @code{write}. @var{data} is a list or\n"
117 "@code{#f} depending on @var{key}: if @var{key} is\n"
118 "@code{system-error} then it should be a list containing the\n"
119 "Unix @code{errno} value; If @var{key} is @code{signal} then it\n"
120 "should be a list containing the Unix signal number; otherwise\n"
121 "it will usually be @code{#f}.")
122 #define FUNC_NAME s_scm_error_scm
127 SCM_VALIDATE_SYMBOL (1, key
);
129 if (SCM_FALSEP (subr
))
133 else if (SCM_SYMBOLP (subr
))
135 szSubr
= SCM_SYMBOL_CHARS (subr
);
139 SCM_VALIDATE_STRING (2, subr
);
140 szSubr
= SCM_STRING_CHARS (subr
);
143 if (SCM_FALSEP (message
))
149 SCM_VALIDATE_STRING (2, message
);
150 szMessage
= SCM_STRING_CHARS (message
);
153 scm_error (key
, szSubr
, szMessage
, args
, data
);
159 # include "win32-socket.h"
160 # define SCM_I_STRERROR(err) \
161 ((err >= WSABASEERR) ? scm_i_socket_strerror (err) : strerror (err))
162 # define SCM_I_ERRNO() \
163 (errno ? errno : scm_i_socket_errno ())
165 # define SCM_I_STRERROR(err) strerror (err)
166 # define SCM_I_ERRNO() errno
167 #endif /* __MINGW32__ */
169 SCM_DEFINE (scm_strerror
, "strerror", 1, 0, 0,
171 "Return the Unix error message corresponding to @var{err}, which\n"
172 "must be an integer value.")
173 #define FUNC_NAME s_scm_strerror
175 SCM_VALIDATE_INUM (1, err
);
176 return scm_makfrom0str (SCM_I_STRERROR (SCM_INUM (err
)));
180 SCM_GLOBAL_SYMBOL (scm_system_error_key
, "system-error");
182 scm_syserror (const char *subr
)
184 int save_errno
= SCM_I_ERRNO ();
186 scm_error (scm_system_error_key
,
189 scm_cons (scm_makfrom0str (SCM_I_STRERROR (save_errno
)), SCM_EOL
),
190 scm_cons (SCM_MAKINUM (save_errno
), SCM_EOL
));
194 scm_syserror_msg (const char *subr
, const char *message
, SCM args
, int eno
)
196 scm_error (scm_system_error_key
,
200 scm_cons (SCM_MAKINUM (eno
), SCM_EOL
));
203 SCM_GLOBAL_SYMBOL (scm_num_overflow_key
, "numerical-overflow");
205 scm_num_overflow (const char *subr
)
207 scm_error (scm_num_overflow_key
,
209 "Numerical overflow",
214 SCM_GLOBAL_SYMBOL (scm_out_of_range_key
, "out-of-range");
216 scm_out_of_range (const char *subr
, SCM bad_value
)
218 scm_error (scm_out_of_range_key
,
220 "Argument out of range: ~S",
221 scm_list_1 (bad_value
),
226 scm_out_of_range_pos (const char *subr
, SCM bad_value
, SCM pos
)
228 scm_error (scm_out_of_range_key
,
230 "Argument ~S out of range: ~S",
231 scm_list_2 (pos
, bad_value
),
236 SCM_GLOBAL_SYMBOL (scm_args_number_key
, "wrong-number-of-args");
238 scm_wrong_num_args (SCM proc
)
240 scm_error (scm_args_number_key
,
242 "Wrong number of arguments to ~A",
249 scm_error_num_args_subr (const char *subr
)
251 scm_error (scm_args_number_key
,
253 "Wrong number of arguments to ~A",
254 scm_list_1 (scm_makfrom0str (subr
)),
259 SCM_GLOBAL_SYMBOL (scm_arg_type_key
, "wrong-type-arg");
261 scm_wrong_type_arg (const char *subr
, int pos
, SCM bad_value
)
263 scm_error (scm_arg_type_key
,
265 (pos
== 0) ? "Wrong type argument: ~S"
266 : "Wrong type argument in position ~A: ~S",
267 (pos
== 0) ? scm_list_1 (bad_value
)
268 : scm_list_2 (SCM_MAKINUM (pos
), bad_value
),
273 scm_wrong_type_arg_msg (const char *subr
, int pos
, SCM bad_value
, const char *szMessage
)
275 SCM msg
= scm_makfrom0str(szMessage
);
277 scm_error (scm_arg_type_key
,
278 subr
, "Wrong type argument (expecting ~A): ~S",
279 scm_list_2 (msg
, bad_value
),
282 scm_error (scm_arg_type_key
,
284 "Wrong type argument in position ~A (expecting ~A): ~S",
285 scm_list_3 (SCM_MAKINUM (pos
), msg
, bad_value
),
291 SCM_GLOBAL_SYMBOL (scm_memory_alloc_key
, "memory-allocation-error");
293 scm_memory_error (const char *subr
)
295 scm_error (scm_memory_alloc_key
,
297 "Memory allocation error",
302 SCM_GLOBAL_SYMBOL (scm_misc_error_key
, "misc-error");
304 scm_misc_error (const char *subr
, const char *message
, SCM args
)
306 scm_error (scm_misc_error_key
, subr
, message
, args
, SCM_BOOL_F
);
312 #include "libguile/cpp_err_symbols.c"
313 #include "libguile/error.x"