* configure.in: don't append threads.doc to EXTRA_DOT_DOC_FILES,
[bpt/guile.git] / libguile / error.c
CommitLineData
9f40cd87 1/* Copyright (C) 1995,1996,1997,1998,2000,2001 Free Software Foundation, Inc.
0f2d19dd
JB
2 *
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)
6 * any later version.
7 *
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.
12 *
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
82892bed
JB
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
0f2d19dd
JB
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
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.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
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.
37 *
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.
82892bed 40 * If you do not wish that, delete this exception notice. */
1bbd0b84
GB
41
42/* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
44
0f2d19dd
JB
45\f
46
47#include <stdio.h>
e6e2e95a 48#include <errno.h>
bd9e24b3 49
a0599745
MD
50#include "libguile/_scm.h"
51#include "libguile/pairs.h"
52#include "libguile/strings.h"
53#include "libguile/throw.h"
20e6290e 54
a0599745
MD
55#include "libguile/validate.h"
56#include "libguile/error.h"
0f2d19dd 57
bd9e24b3
GH
58#ifdef HAVE_STRING_H
59#include <string.h>
60#endif
95b88819
GH
61#ifdef HAVE_UNISTD_H
62#include <unistd.h>
63#endif
0f2d19dd
JB
64\f
65
66\f
67/* {Errors and Exceptional Conditions}
68 */
69
0f2d19dd 70
c37e0e55 71/* All errors should pass through here. */
7cb1d4d3 72void
1bbd0b84 73scm_error (SCM key, const char *subr, const char *message, SCM args, SCM rest)
7cb1d4d3
GH
74{
75 SCM arg_list;
406c7d90 76 if (scm_gc_running_p)
ef18914a
MD
77 {
78 /* The error occured during GC --- abort */
79 fprintf (stderr, "Error in %s during GC: %s\n",
80 subr ? subr : "unknown function",
81 message ? message : "<empty message>");
82 abort ();
83 }
e0c08f17
KN
84 arg_list = SCM_LIST4 (subr ? scm_makfrom0str (subr) : SCM_BOOL_F,
85 message ? scm_makfrom0str (message) : SCM_BOOL_F,
86 args,
87 rest);
7cb1d4d3
GH
88 scm_ithrow (key, arg_list, 1);
89
90 /* No return, but just in case: */
5c11cc9d
GH
91 {
92 const char msg[] = "guile:scm_error:scm_ithrow returned!\n";
7cb1d4d3 93
5c11cc9d
GH
94 write (2, msg, (sizeof msg) - 1);
95 }
7cb1d4d3
GH
96 exit (1);
97}
0f2d19dd 98
c37e0e55 99/* Scheme interface to scm_error. */
3b3b36dd 100SCM_DEFINE (scm_error_scm, "scm-error", 5, 0, 0,
1bbd0b84 101 (SCM key, SCM subr, SCM message, SCM args, SCM rest),
b380b885
MD
102 "Raise an error with key @var{key}. @var{subr} can be a string naming\n"
103 "the procedure associated with the error, or @code{#f}. @var{message}\n"
104 "is the error message string, possibly containing @code{~S} and @code{~A}\n"
105 "escapes. When an error is reported, these are replaced by formating the\n"
106 "corresponding members of @var{args}: @code{~A} (was @code{%s}) formats using @code{display}\n"
872e0c72 107 "and @code{~S} (was @code{%S}) formats using @code{write}. @var{data} is a\n"
b380b885
MD
108 "list or @code{#f} depending on @var{key}: if @var{key} is\n"
109 "@code{system-error} then it should be a list\n"
110 "containing the Unix @code{errno} value; If @var{key} is @code{signal} then\n"
111 "it should be a list containing the Unix signal number; otherwise it\n"
112 "will usually be @code{#f}.")
1bbd0b84 113#define FUNC_NAME s_scm_error_scm
c37e0e55 114{
1bbd0b84
GB
115 char *szSubr;
116 char *szMessage;
a6d9e5ab
DH
117
118 SCM_VALIDATE_SYMBOL (1, key);
119
120 if (SCM_FALSEP (subr))
121 {
122 szSubr = NULL;
123 }
124 else if (SCM_SYMBOLP (subr))
125 {
126 szSubr = SCM_SYMBOL_CHARS (subr);
127 }
128 else
129 {
130 SCM_VALIDATE_STRING (2, subr);
131 SCM_STRING_COERCE_0TERMINATION_X (subr);
132 szSubr = SCM_STRING_CHARS (subr);
133 }
134
135 if (SCM_FALSEP (message))
136 {
137 szMessage = NULL;
138 }
139 else
140 {
141 SCM_VALIDATE_STRING (2, message);
142 SCM_STRING_COERCE_0TERMINATION_X (message);
143 szMessage = SCM_STRING_CHARS (message);
144 }
89958ad0 145
1bbd0b84 146 scm_error (key, szSubr, szMessage, args, rest);
c37e0e55
GH
147 /* not reached. */
148}
1bbd0b84 149#undef FUNC_NAME
c37e0e55 150
a1ec6916 151SCM_DEFINE (scm_strerror, "strerror", 1, 0, 0,
1bbd0b84 152 (SCM err),
1de052a7 153 "Returns the Unix error message corresponding to @var{err}, an integer.")
1bbd0b84 154#define FUNC_NAME s_scm_strerror
efb997f5 155{
3b3b36dd 156 SCM_VALIDATE_INUM (1,err);
efb997f5
GH
157 return scm_makfrom0str (strerror (SCM_INUM (err)));
158}
1bbd0b84 159#undef FUNC_NAME
efb997f5 160
523f5266 161SCM_SYMBOL (scm_system_error_key, "system-error");
52859adf 162void
1bbd0b84 163scm_syserror (const char *subr)
52859adf 164{
1de052a7
GH
165 int save_errno = errno;
166
01f61221 167 scm_error (scm_system_error_key,
52859adf 168 subr,
70d63753 169 "~A",
1de052a7
GH
170 scm_cons (scm_makfrom0str (strerror (save_errno)), SCM_EOL),
171 scm_cons (SCM_MAKINUM (save_errno), SCM_EOL));
52859adf
GH
172}
173
174void
1bbd0b84 175scm_syserror_msg (const char *subr, const char *message, SCM args, int eno)
52859adf 176{
01f61221 177 scm_error (scm_system_error_key,
52859adf
GH
178 subr,
179 message,
180 args,
5c11cc9d 181 scm_cons (SCM_MAKINUM (eno), SCM_EOL));
52859adf
GH
182}
183
f5f2dcff
DH
184
185#if (SCM_DEBUG_DEPRECATED == 0)
186
f25f761d
GH
187/* scm_sysmissing is no longer used in libguile. it can probably be
188 removed after a release or two. there's a comment in NEWS about it
189 (2000-01-09). */
52859adf 190void
1bbd0b84 191scm_sysmissing (const char *subr)
52859adf
GH
192{
193#ifdef ENOSYS
01f61221 194 scm_error (scm_system_error_key,
52859adf 195 subr,
70d63753 196 "~A",
5c11cc9d
GH
197 scm_cons (scm_makfrom0str (strerror (ENOSYS)), SCM_EOL),
198 scm_cons (SCM_MAKINUM (ENOSYS), SCM_EOL));
52859adf 199#else
01f61221 200 scm_error (scm_system_error_key,
52859adf
GH
201 subr,
202 "Missing function",
203 SCM_BOOL_F,
5c11cc9d 204 scm_cons (SCM_MAKINUM (0), SCM_EOL));
52859adf
GH
205#endif
206}
207
f5f2dcff
DH
208#endif /* SCM_DEBUG_DEPRECATED == 0 */
209
210
523f5266 211SCM_SYMBOL (scm_num_overflow_key, "numerical-overflow");
52859adf 212void
1bbd0b84 213scm_num_overflow (const char *subr)
52859adf 214{
01f61221 215 scm_error (scm_num_overflow_key,
52859adf
GH
216 subr,
217 "Numerical overflow",
218 SCM_BOOL_F,
219 SCM_BOOL_F);
220}
221
523f5266 222SCM_SYMBOL (scm_out_of_range_key, "out-of-range");
52859adf 223void
1bbd0b84 224scm_out_of_range (const char *subr, SCM bad_value)
52859adf 225{
01f61221 226 scm_error (scm_out_of_range_key,
52859adf 227 subr,
70d63753 228 "Argument out of range: ~S",
b6791b2e 229 SCM_LIST1(bad_value),
52859adf
GH
230 SCM_BOOL_F);
231}
f5bf2977 232
1e76143f
GB
233void
234scm_out_of_range_pos (const char *subr, SCM bad_value, SCM pos)
235{
236 scm_error (scm_out_of_range_key,
237 subr,
70d63753 238 "Argument ~S out of range: ~S",
b6791b2e 239 SCM_LIST2(pos,bad_value),
1e76143f
GB
240 SCM_BOOL_F);
241}
242
243
523f5266 244SCM_SYMBOL (scm_args_number_key, "wrong-number-of-args");
0f2d19dd 245void
1bbd0b84 246scm_wrong_num_args (SCM proc)
f5bf2977 247{
01f61221 248 scm_error (scm_args_number_key,
f5bf2977 249 NULL,
70d63753 250 "Wrong number of arguments to ~A",
b6791b2e 251 SCM_LIST1(proc),
f5bf2977
GH
252 SCM_BOOL_F);
253}
254
9f40cd87
DH
255
256void
257scm_error_num_args_subr (const char *subr)
258{
259 scm_error (scm_args_number_key,
260 NULL,
261 "Wrong number of arguments to ~A",
262 SCM_LIST1 (scm_makfrom0str (subr)),
263 SCM_BOOL_F);
264}
265
266
523f5266 267SCM_SYMBOL (scm_arg_type_key, "wrong-type-arg");
f5bf2977 268void
1bbd0b84 269scm_wrong_type_arg (const char *subr, int pos, SCM bad_value)
f5bf2977 270{
01f61221 271 scm_error (scm_arg_type_key,
f5bf2977 272 subr,
70d63753
GB
273 (pos == 0) ? "Wrong type argument: ~S"
274 : "Wrong type argument in position ~A: ~S",
b6791b2e
GB
275 (pos == 0) ? SCM_LIST1(bad_value)
276 : SCM_LIST2(SCM_MAKINUM(pos), bad_value),
f5bf2977
GH
277 SCM_BOOL_F);
278}
279
b6791b2e
GB
280void
281scm_wrong_type_arg_msg (const char *subr, int pos, SCM bad_value, const char *szMessage)
282{
283 SCM msg = scm_makfrom0str(szMessage);
284 if (pos == 0) {
285 scm_error (scm_arg_type_key,
286 subr, "Wrong type argument (expecting ~A): ~S",
287 SCM_LIST2(msg,bad_value),
288 SCM_BOOL_F);
289 } else {
290 scm_error (scm_arg_type_key,
291 subr,
292 "Wrong type argument in position ~A (expecting ~A): ~S",
293 SCM_LIST3(SCM_MAKINUM(pos),msg,bad_value),
294 SCM_BOOL_F);
295 }
296}
297
298
523f5266 299SCM_SYMBOL (scm_memory_alloc_key, "memory-allocation-error");
f5bf2977 300void
1bbd0b84 301scm_memory_error (const char *subr)
f5bf2977 302{
01f61221 303 scm_error (scm_memory_alloc_key,
f5bf2977
GH
304 subr,
305 "Memory allocation error",
306 SCM_BOOL_F,
307 SCM_BOOL_F);
308}
309
523f5266
GH
310SCM_SYMBOL (scm_misc_error_key, "misc-error");
311void
1bbd0b84 312scm_misc_error (const char *subr, const char *message, SCM args)
523f5266
GH
313{
314 scm_error (scm_misc_error_key, subr, message, args, SCM_BOOL_F);
315}
316
b3fcac34
DH
317#if (SCM_DEBUG_DEPRECATED == 0)
318
f5bf2977 319SCM
1bbd0b84 320scm_wta (SCM arg, const char *pos, const char *s_subr)
f5bf2977
GH
321{
322 if (!s_subr || !*s_subr)
323 s_subr = NULL;
324 if ((~0x1fL) & (long) pos)
325 {
326 /* error string supplied. */
26a4f923 327 scm_misc_error (s_subr, pos, SCM_LIST1 (arg));
f5bf2977
GH
328 }
329 else
330 {
331 /* numerical error code. */
332 int error = (long) pos;
333
334 switch (error)
335 {
336 case SCM_ARGn:
337 scm_wrong_type_arg (s_subr, 0, arg);
338 case SCM_ARG1:
339 scm_wrong_type_arg (s_subr, 1, arg);
340 case SCM_ARG2:
341 scm_wrong_type_arg (s_subr, 2, arg);
342 case SCM_ARG3:
343 scm_wrong_type_arg (s_subr, 3, arg);
344 case SCM_ARG4:
345 scm_wrong_type_arg (s_subr, 4, arg);
346 case SCM_ARG5:
347 scm_wrong_type_arg (s_subr, 5, arg);
3d9352fb
MD
348 case SCM_ARG6:
349 scm_wrong_type_arg (s_subr, 6, arg);
350 case SCM_ARG7:
351 scm_wrong_type_arg (s_subr, 7, arg);
f5bf2977
GH
352 case SCM_WNA:
353 scm_wrong_num_args (arg);
b63a956d
DH
354 case SCM_OUTOFRANGE:
355 scm_out_of_range (s_subr, arg);
356 case SCM_NALLOC:
357 scm_memory_error (s_subr);
f5bf2977
GH
358 default:
359 /* this shouldn't happen. */
b1edcd36 360 scm_misc_error (s_subr, "Unknown error", SCM_EOL);
f5bf2977
GH
361 }
362 }
363 return SCM_UNSPECIFIED;
364}
365
b3fcac34
DH
366#endif /* SCM_DEBUG_DEPRECATED == 0 */
367
0f2d19dd
JB
368void
369scm_init_error ()
0f2d19dd 370{
a0599745 371#include "libguile/cpp_err_symbols.c"
8dc9439f 372#ifndef SCM_MAGIC_SNARFER
a0599745 373#include "libguile/error.x"
8dc9439f 374#endif
0f2d19dd
JB
375}
376
89e00824
ML
377
378/*
379 Local Variables:
380 c-file-style: "gnu"
381 End:
382*/