Debian Codegraph @

src/error.c

raw
1 /*
2 * error.c - error handling
3 *
4 * Copyright (c) 2000-2024 Shiro Kawai <[email protected]>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the authors nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #define LIBGAUCHE_BODY
35 #include "gauche.h"
36 #include "gauche/priv/configP.h"
37 #include "gauche/exception.h"
38 #include "gauche/vm.h"
39 #include "gauche/priv/builtin-syms.h"
40
41 #include <errno.h>
42 #include <string.h>
43 #include <ctype.h>
44
45 static ScmObj condition_allocate(ScmClass *klass, ScmObj initargs);
46 static ScmObj message_allocate(ScmClass *klass, ScmObj initargs);
47 static ScmObj syserror_allocate(ScmClass *klass, ScmObj initargs);
48 static ScmObj sigerror_allocate(ScmClass *klass, ScmObj initargs);
49 static ScmObj unberror_allocate(ScmClass *klass, ScmObj initargs);
50 static ScmObj readerror_allocate(ScmClass *klass, ScmObj initargs);
51 static ScmObj porterror_allocate(ScmClass *klass, ScmObj initargs);
52 static ScmObj conterror_allocate(ScmClass *klass, ScmObj initargs);
53 static ScmObj compound_allocate(ScmClass *klass, ScmObj initargs);
54
55 /* Setting up CPL is a bit tricky, since we have multiple
56 inheritance case. */
57
58 #define CONDITION_CPL \
59 SCM_CLASS_STATIC_PTR(Scm_ConditionClass), \
60 SCM_CLASS_STATIC_PTR(Scm_TopClass)
61
62 #define MESSAGE_SERIOUS_CPL \
63 SCM_CLASS_STATIC_PTR(Scm_MessageConditionClass), \
64 SCM_CLASS_STATIC_PTR(Scm_SeriousConditionClass), \
65 CONDITION_CPL
66
67 #define ERROR_CPL \
68 SCM_CLASS_STATIC_PTR(Scm_ErrorClass), \
69 MESSAGE_SERIOUS_CPL
70
71 /*-----------------------------------------------------------
72 * Base conditions
73 */
74 static ScmClass *condition_cpl[] = {
75 CONDITION_CPL,
76 NULL
77 };
78
79 SCM_DEFINE_BASE_CLASS(Scm_ConditionClass, ScmInstance,
80 NULL, NULL, NULL,
81 condition_allocate, SCM_CLASS_DEFAULT_CPL);
82 SCM_DEFINE_BASE_CLASS(Scm_MessageConditionClass, ScmMessageCondition,
83 Scm_MessageConditionPrint, NULL, NULL,
84 message_allocate, condition_cpl);
85 SCM_DEFINE_BASE_CLASS(Scm_SeriousConditionClass, ScmSeriousCondition,
86 NULL, NULL, NULL,
87 condition_allocate, condition_cpl);
88 SCM_DEFINE_BASE_CLASS(Scm_MixinConditionClass, ScmCondition,
89 NULL, NULL, NULL,
90 condition_allocate, condition_cpl);
91
92 static ScmObj condition_allocate(ScmClass *klass, ScmObj initargs SCM_UNUSED)
93 {
94 return SCM_OBJ(SCM_NEW_INSTANCE(ScmCondition, klass));
95 }
96
97 /* We expose this, for other condition subclasses may share this. */
98 void Scm_MessageConditionPrint(ScmObj obj, ScmPort *port,
99 ScmWriteContext *ctx SCM_UNUSED)
100 {
101 ScmClass *k = Scm_ClassOf(obj);
102 Scm_Printf(port, "#<%A \"%150.1A\">",
103 Scm_ShortClassName(k),
104 SCM_ERROR_MESSAGE(obj));
105 }
106
107 static ScmObj message_allocate(ScmClass *klass, ScmObj initargs SCM_UNUSED)
108 {
109 ScmError *e = SCM_NEW_INSTANCE(ScmError, klass);
110 e->message = SCM_FALSE; /* would be set by initialize */
111 return SCM_OBJ(e);
112 }
113
114 /* See comment on gauche/exception.h about hack in 'message' slot.
115 TRANSIENT: Remove this hack on 1.0 release. */
116 static ScmObj message_get(ScmMessageCondition *obj)
117 {
118 ScmObj msglist = obj->message;
119 if (SCM_PAIRP(msglist)) return SCM_CAR(msglist);
120 else return msglist;
121 }
122
123 static void message_set(ScmMessageCondition *obj, ScmObj val)
124 {
125 ScmObj msglist = obj->message;
126 if (SCM_PAIRP(msglist)) SCM_SET_CAR_UNCHECKED(msglist, val);
127 else SCM_MESSAGE_CONDITION(obj)->message = SCM_LIST2(val, val);
128 }
129
130 static ScmObj message_prefix_get(ScmMessageCondition *obj)
131 {
132 ScmObj msglist = obj->message;
133 if (SCM_PAIRP(msglist) && SCM_PAIRP(SCM_CDR(msglist))) {
134 return SCM_CADR(msglist);
135 } else {
136 return msglist;
137 }
138 }
139
140 static void message_prefix_set(ScmMessageCondition *obj, ScmObj val)
141 {
142 ScmObj msglist = obj->message;
143 if (SCM_PAIRP(msglist) && SCM_PAIRP(SCM_CDR(msglist))) {
144 SCM_SET_CAR_UNCHECKED(SCM_CDR(msglist), val);
145 } else {
146 obj->message = SCM_LIST2(msglist, val);
147 }
148 }
149
150 static ScmObj message_args_get(ScmMessageCondition *obj)
151 {
152 ScmObj msglist = obj->message;
153 if (SCM_PAIRP(msglist) && SCM_PAIRP(SCM_CDR(msglist))) {
154 return SCM_CDDR(msglist);
155 } else {
156 return SCM_NIL;
157 }
158 }
159
160 static void message_args_set(ScmMessageCondition *obj, ScmObj val)
161 {
162 ScmObj msglist = obj->message;
163 if (SCM_PAIRP(msglist) && SCM_PAIRP(SCM_CDR(msglist))) {
164 SCM_SET_CDR_UNCHECKED(SCM_CDR(msglist), val);
165 } else {
166 obj->message = Scm_Cons(msglist, Scm_Cons(msglist, val));
167 }
168 }
169
170 static ScmClassStaticSlotSpec message_slots[] = {
171 SCM_CLASS_SLOT_SPEC("message", message_get, message_set),
172 SCM_CLASS_SLOT_SPEC("message-prefix", message_prefix_get, message_prefix_set),
173 SCM_CLASS_SLOT_SPEC("message-args", message_args_get, message_args_set),
174 SCM_CLASS_SLOT_SPEC_END()
175 };
176
177 /*------------------------------------------------------------
178 * Errors
179 */
180
181 static ScmClass *error_cpl[] = {
182 ERROR_CPL,
183 NULL
184 };
185
186 static ScmClass *porterror_cpl[] = {
187 SCM_CLASS_STATIC_PTR(Scm_PortErrorClass),
188 SCM_CLASS_STATIC_PTR(Scm_IOErrorClass),
189 ERROR_CPL,
190 NULL
191 };
192
193 static ScmClass *decoding_error_cpl[] = {
194 SCM_CLASS_STATIC_PTR(Scm_IOReadErrorClass),
195 SCM_CLASS_STATIC_PTR(Scm_PortErrorClass),
196 SCM_CLASS_STATIC_PTR(Scm_IOErrorClass),
197 ERROR_CPL,
198 NULL
199 };
200
201 static ScmClass *encoding_error_cpl[] = {
202 SCM_CLASS_STATIC_PTR(Scm_IOWriteErrorClass),
203 SCM_CLASS_STATIC_PTR(Scm_PortErrorClass),
204 SCM_CLASS_STATIC_PTR(Scm_IOErrorClass),
205 ERROR_CPL,
206 NULL
207 };
208
209 SCM_DEFINE_BASE_CLASS(Scm_ErrorClass, ScmError,
210 Scm_MessageConditionPrint, NULL, NULL,
211 message_allocate, error_cpl+1);
212 SCM_DEFINE_BASE_CLASS(Scm_SystemErrorClass, ScmSystemError,
213 Scm_MessageConditionPrint, NULL, NULL,
214 syserror_allocate, error_cpl);
215 SCM_DEFINE_BASE_CLASS(Scm_UnhandledSignalErrorClass, ScmUnhandledSignalError,
216 Scm_MessageConditionPrint, NULL, NULL,
217 sigerror_allocate, error_cpl);
218 SCM_DEFINE_BASE_CLASS(Scm_UnboundVariableErrorClass, ScmUnboundVariableError,
219 Scm_MessageConditionPrint, NULL, NULL,
220 unberror_allocate, error_cpl);
221 SCM_DEFINE_BASE_CLASS(Scm_ReadErrorClass, ScmReadError,
222 Scm_MessageConditionPrint, NULL, NULL,
223 readerror_allocate, error_cpl);
224 SCM_DEFINE_BASE_CLASS(Scm_IOErrorClass, ScmIOError,
225 Scm_MessageConditionPrint, NULL, NULL,
226 message_allocate, error_cpl);
227 SCM_DEFINE_BASE_CLASS(Scm_PortErrorClass, ScmPortError,
228 Scm_MessageConditionPrint, NULL, NULL,
229 porterror_allocate, porterror_cpl+1);
230 SCM_DEFINE_BASE_CLASS(Scm_IOReadErrorClass, ScmIOReadError,
231 Scm_MessageConditionPrint, NULL, NULL,
232 porterror_allocate, porterror_cpl);
233 SCM_DEFINE_BASE_CLASS(Scm_IOWriteErrorClass, ScmIOWriteError,
234 Scm_MessageConditionPrint, NULL, NULL,
235 porterror_allocate, porterror_cpl);
236 SCM_DEFINE_BASE_CLASS(Scm_IOClosedErrorClass, ScmIOClosedError,
237 Scm_MessageConditionPrint, NULL, NULL,
238 porterror_allocate, porterror_cpl);
239 SCM_DEFINE_BASE_CLASS(Scm_IOUnitErrorClass, ScmIOUnitError,
240 Scm_MessageConditionPrint, NULL, NULL,
241 porterror_allocate, porterror_cpl);
242 SCM_DEFINE_BASE_CLASS(Scm_IODecodingErrorClass, ScmIODecodingError,
243 Scm_MessageConditionPrint, NULL, NULL,
244 porterror_allocate, decoding_error_cpl);
245 SCM_DEFINE_BASE_CLASS(Scm_IOEncodingErrorClass, ScmIOEncodingError,
246 Scm_MessageConditionPrint, NULL, NULL,
247 porterror_allocate, encoding_error_cpl);
248 SCM_DEFINE_BASE_CLASS(Scm_IOInvalidPositionErrorClass, ScmIOInvalidPositionError,
249 Scm_MessageConditionPrint, NULL, NULL,
250 porterror_allocate, porterror_cpl);
251
252 SCM_DEFINE_BASE_CLASS(Scm_ContinuationErrorClass, ScmContinuationError,
253 Scm_MessageConditionPrint, NULL, NULL,
254 conterror_allocate, error_cpl);
255
256 static ScmObj syserror_allocate(ScmClass *klass, ScmObj initargs SCM_UNUSED)
257 {
258 ScmSystemError *e = SCM_NEW_INSTANCE(ScmSystemError, klass);
259 e->common.message = SCM_FALSE; /* set by initialize */
260 e->error_number = 0; /* set by initialize */
261 return SCM_OBJ(e);
262 }
263
264 static ScmObj sigerror_allocate(ScmClass *klass, ScmObj initargs SCM_UNUSED)
265 {
266 ScmUnhandledSignalError *e = SCM_NEW_INSTANCE(ScmUnhandledSignalError,
267 klass);
268 e->common.message = SCM_FALSE; /* set by initialize */
269 e->signal = 0; /* set by initialize */
270 return SCM_OBJ(e);
271 }
272
273 static ScmObj unberror_allocate(ScmClass *klass, ScmObj initargs SCM_UNUSED)
274 {
275 ScmUnboundVariableError *e = SCM_NEW_INSTANCE(ScmUnboundVariableError,
276 klass);
277 e->common.message = SCM_FALSE; /* set by initialize */
278 e->identifier = SCM_FALSE; /* set by initialize */
279 return SCM_OBJ(e);
280 }
281
282 static ScmObj readerror_allocate(ScmClass *klass, ScmObj initargs SCM_UNUSED)
283 {
284 ScmReadError *e = SCM_NEW_INSTANCE(ScmReadError, klass);
285 e->common.message = SCM_FALSE; /* set by initialize */
286 e->port = NULL; /* set by initialize */
287 e->line = -1; /* set by initialize */
288 return SCM_OBJ(e);
289 }
290
291 static ScmObj porterror_allocate(ScmClass *klass, ScmObj initargs SCM_UNUSED)
292 {
293 ScmPortError *e = SCM_NEW_INSTANCE(ScmPortError, klass);
294 e->common.message = SCM_FALSE; /* set by initialize */
295 e->port = NULL; /* set by initialize */
296 e->auxinfo = SCM_NIL; /* set by initialize */
297 return SCM_OBJ(e);
298 }
299
300 static ScmObj conterror_allocate(ScmClass *klass, ScmObj initargs SCM_UNUSED)
301 {
302 ScmContinuationError *e = SCM_NEW_INSTANCE(ScmContinuationError, klass);
303 e->common.message = SCM_FALSE; /* set by initialize */
304 e->promptTag = SCM_FALSE; /* set by initialize */
305 return SCM_OBJ(e);
306 }
307
308 static ScmObj syserror_number_get(ScmSystemError *obj)
309 {
310 return SCM_MAKE_INT(obj->error_number);
311 }
312
313 static void syserror_number_set(ScmSystemError *obj, ScmObj val)
314 {
315 if (!SCM_INTP(val)) {
316 Scm_Error("small integer required, but got %S", val);
317 }
318 obj->error_number = SCM_INT_VALUE(val);
319 }
320
321 static ScmObj sigerror_signal_get(ScmUnhandledSignalError *obj)
322 {
323 return SCM_MAKE_INT(obj->signal);
324 }
325
326 static void sigerror_signal_set(ScmUnhandledSignalError *obj, ScmObj val)
327 {
328 if (!SCM_INTP(val)) {
329 Scm_Error("small integer required, but got %S", val);
330 }
331 obj->signal = SCM_INT_VALUE(val);
332 }
333
334 static ScmObj unberror_identifier_get(ScmUnboundVariableError *obj)
335 {
336 return obj->identifier;
337 }
338
339 static void unberror_identifier_set(ScmUnboundVariableError *obj, ScmObj val)
340 {
341 if (!(SCM_SYMBOLP(val) || !SCM_IDENTIFIERP(val))) {
342 Scm_Error("symbol or identifier required, but got %S", val);
343 }
344 obj->identifier = val;
345 }
346
347 static ScmObj readerror_port_get(ScmReadError *obj)
348 {
349 if (obj->port) return SCM_OBJ(obj->port);
350 else return SCM_FALSE;
351 }
352
353 static void readerror_port_set(ScmReadError *obj, ScmObj val)
354 {
355 if (SCM_IPORTP(val)) {
356 obj->port = SCM_PORT(val);
357 }
358 else if (SCM_FALSEP(val)) {
359 obj->port = NULL;
360 }
361 else {
362 Scm_Error("input port or #f required, but got %S", val);
363 }
364 }
365
366 static ScmObj readerror_line_get(ScmReadError *obj)
367 {
368 return SCM_MAKE_INT(obj->line);
369 }
370
371 static void readerror_line_set(ScmReadError *obj, ScmObj val)
372 {
373 if (!SCM_INTP(val)){
374 Scm_Error("small integer required, but got %S", val);
375 }
376 obj->line = SCM_INT_VALUE(val);
377 }
378
379 static ScmObj readerror_dummy_get(ScmReadError *obj SCM_UNUSED)
380 {
381 return SCM_FALSE;
382 }
383
384 static void readerror_dummy_set(ScmReadError *obj SCM_UNUSED,
385 ScmObj val SCM_UNUSED)
386 {
387 /* nothing */
388 }
389
390 static ScmObj porterror_port_get(ScmPortError *obj)
391 {
392 return obj->port? SCM_OBJ(obj->port) : SCM_FALSE;
393 }
394
395 static void porterror_port_set(ScmPortError *obj, ScmObj val)
396 {
397 if (!SCM_PORTP(val) && !SCM_FALSEP(val)) {
398 Scm_Error("port or #f required, but got %S", val);
399 }
400 obj->port = SCM_FALSEP(val)? NULL : SCM_PORT(val);
401 }
402
403 static ScmObj porterror_auxinfo_getter(ScmPortError *obj, ScmObj key)
404 {
405 ScmObj p = Scm_Assq(key, obj->auxinfo);
406 if (SCM_PAIRP(p)) return SCM_CDR(p);
407 return SCM_FALSE;
408 }
409
410 static void porterror_auxinfo_setter(ScmPortError *obj, ScmObj key, ScmObj val)
411 {
412 ScmObj p = Scm_Assq(key, obj->auxinfo);
413 if (SCM_PAIRP(p)) SCM_SET_CDR(p, val);
414 else obj->auxinfo = Scm_Acons(key, val, obj->auxinfo);
415 }
416
417 static ScmObj sym_offending_char; /* 'offending-char */
418
419 static ScmObj porterror_offending_char_get(ScmPortError *obj)
420 {
421 return porterror_auxinfo_getter(obj, sym_offending_char);
422 }
423
424 static void porterror_offending_char_set(ScmPortError *obj, ScmObj val)
425 {
426 porterror_auxinfo_setter(obj, sym_offending_char, val);
427 }
428
429 static ScmObj sym_position; /* 'position */
430
431 static ScmObj porterror_position_get(ScmPortError *obj)
432 {
433 return porterror_auxinfo_getter(obj, sym_position);
434 }
435
436 static void porterror_position_set(ScmPortError *obj, ScmObj val)
437 {
438 porterror_auxinfo_setter(obj, sym_position, val);
439 }
440
441 static ScmObj conterror_prompt_tag_get(ScmContinuationError *obj)
442 {
443 return obj->promptTag;
444 }
445
446 static void conterror_prompt_tag_set(ScmContinuationError *obj, ScmObj val)
447 {
448 obj->promptTag = val;
449 }
450
451
452 static ScmClassStaticSlotSpec syserror_slots[] = {
453 SCM_CLASS_SLOT_SPEC("errno", syserror_number_get, syserror_number_set),
454 SCM_CLASS_SLOT_SPEC_END()
455 };
456
457 static ScmClassStaticSlotSpec sigerror_slots[] = {
458 SCM_CLASS_SLOT_SPEC("signal", sigerror_signal_get, sigerror_signal_set),
459 SCM_CLASS_SLOT_SPEC_END()
460 };
461
462 static ScmClassStaticSlotSpec unberror_slots[] = {
463 SCM_CLASS_SLOT_SPEC("identifier", unberror_identifier_get, unberror_identifier_set),
464 SCM_CLASS_SLOT_SPEC_END()
465 };
466
467 static ScmClassStaticSlotSpec readerror_slots[] = {
468 SCM_CLASS_SLOT_SPEC("port", readerror_port_get, readerror_port_set),
469 SCM_CLASS_SLOT_SPEC("line", readerror_line_get, readerror_line_set),
470 SCM_CLASS_SLOT_SPEC("column", readerror_dummy_get, readerror_dummy_set),
471 SCM_CLASS_SLOT_SPEC("position", readerror_dummy_get, readerror_dummy_set),
472 SCM_CLASS_SLOT_SPEC("span", readerror_dummy_get, readerror_dummy_set),
473 SCM_CLASS_SLOT_SPEC_END()
474 };
475
476 static ScmClassStaticSlotSpec porterror_slots[] = {
477 SCM_CLASS_SLOT_SPEC("port", porterror_port_get, porterror_port_set),
478 SCM_CLASS_SLOT_SPEC_END()
479 };
480
481 static ScmClassStaticSlotSpec encodingerror_slots[] = {
482 SCM_CLASS_SLOT_SPEC("port", porterror_port_get, porterror_port_set),
483 SCM_CLASS_SLOT_SPEC("offending-char",
484 porterror_offending_char_get,
485 porterror_offending_char_set),
486 SCM_CLASS_SLOT_SPEC_END()
487 };
488
489 static ScmClassStaticSlotSpec invalidpositionerror_slots[] = {
490 SCM_CLASS_SLOT_SPEC("port", porterror_port_get, porterror_port_set),
491 SCM_CLASS_SLOT_SPEC("position",
492 porterror_position_get,
493 porterror_position_set),
494 SCM_CLASS_SLOT_SPEC_END()
495 };
496
497 static ScmClassStaticSlotSpec conterror_slots[] = {
498 SCM_CLASS_SLOT_SPEC("prompt-tag",
499 conterror_prompt_tag_get,
500 conterror_prompt_tag_set),
501 SCM_CLASS_SLOT_SPEC_END()
502 };
503
504
505 /*------------------------------------------------------------
506 * Compound conditions
507 */
508
509 static ScmClass *compound_cpl[] = {
510 SCM_CLASS_STATIC_PTR(Scm_CompoundConditionClass),
511 SCM_CLASS_STATIC_PTR(Scm_SeriousConditionClass),
512 CONDITION_CPL,
513 NULL
514 };
515
516 static void compound_print(ScmObj obj, ScmPort *port,
517 ScmWriteContext *ctx SCM_UNUSED)
518 {
519 ScmClass *k = Scm_ClassOf(obj);
520 Scm_Printf(port, "#<%A", Scm_ShortClassName(k));
521 ScmCompoundCondition *c = SCM_COMPOUND_CONDITION(obj);
522 ScmObj cp;
523 SCM_FOR_EACH(cp, c->conditions) {
524 Scm_Printf(port, " %A", SCM_CAR(cp));
525 }
526 Scm_Printf(port, ">");
527 }
528
529 SCM_DEFINE_BASE_CLASS(Scm_CompoundConditionClass, ScmCompoundCondition,
530 compound_print, NULL, NULL,
531 compound_allocate, compound_cpl+2);
532 SCM_DEFINE_BASE_CLASS(Scm_SeriousCompoundConditionClass, ScmCompoundCondition,
533 compound_print, NULL, NULL,
534 compound_allocate, compound_cpl);
535
536 static ScmObj compound_allocate(ScmClass *klass, ScmObj initargs SCM_UNUSED)
537 {
538 ScmCompoundCondition *e = SCM_NEW_INSTANCE(ScmCompoundCondition, klass);
539 e->conditions = SCM_NIL;
540 return SCM_OBJ(e);
541 }
542
543 ScmObj Scm_MakeCompoundCondition(ScmObj conditions)
544 {
545 ScmObj h = SCM_NIL, t = SCM_NIL;
546 int serious = FALSE;
547 int nconds = Scm_Length(conditions);
548
549 /* some boundary cases */
550 if (nconds < 0) {
551 Scm_Error("Scm_MakeCompoundCondition: list required, but got %S",
552 conditions);
553 }
554 if (nconds == 0) {
555 return compound_allocate(SCM_CLASS_COMPOUND_CONDITION, SCM_NIL);
556 }
557 if (nconds == 1) {
558 if (!SCM_CONDITIONP(SCM_CAR(conditions))) {
559 Scm_Error("make-compound-condition: given non-condition object: %S", SCM_CAR(conditions));
560 }
561 return SCM_CAR(conditions);
562 }
563
564 /* collect conditions and creates compound one */
565 ScmObj cp;
566 SCM_FOR_EACH(cp, conditions) {
567 ScmObj c = SCM_CAR(cp);
568 if (!SCM_CONDITIONP(c)) {
569 Scm_Error("make-compound-condition: given non-condition object: %S", SCM_CAR(cp));
570 }
571 if (SCM_SERIOUS_CONDITION_P(c)) {
572 serious = TRUE;
573 }
574
575 if (SCM_COMPOUND_CONDITION_P(c)) {
576 ScmCompoundCondition *cc = SCM_COMPOUND_CONDITION(c);
577 SCM_APPEND(h, t, cc->conditions);
578 } else {
579 SCM_APPEND1(h, t, c);
580 }
581 }
582 ScmObj cond = compound_allocate((serious?
583 SCM_CLASS_COMPOUND_CONDITION :
584 SCM_CLASS_SERIOUS_COMPOUND_CONDITION),
585 SCM_NIL);
586 SCM_COMPOUND_CONDITION(cond)->conditions = h;
587 return cond;
588 }
589
590 /* Extract condition of type TYPE from a compound condition CONDITION.
591 The returned condition may be of subtype of TYPE.
592 CONDITION may be simple condition; in that case, CONDITION itself
593 is returned iff condition is of a subtype of TYPE.
594 This isn't the same as `extract-condition' of SRFI-35, which always
595 returns a condition of the specified type; it also has different
596 rule to refer to the slots.
597 */
598 ScmObj Scm_ExtractSimpleCondition(ScmObj condition, ScmClass *type)
599 {
600 ScmObj cs = (SCM_COMPOUND_CONDITION_P(condition)
601 ? SCM_COMPOUND_CONDITION(condition)->conditions
602 : SCM_LIST1(condition));
603 ScmObj cp;
604 SCM_FOR_EACH(cp, cs) {
605 if (SCM_ISA(SCM_CAR(cp), type)) return SCM_CAR(cp);
606 }
607 return SCM_FALSE;
608 }
609
610 static ScmObj conditions_get(ScmCompoundCondition *obj)
611 {
612 return obj->conditions;
613 }
614
615 static void conditions_set(ScmCompoundCondition *obj, ScmObj conds)
616 {
617 ScmObj cp;
618 SCM_FOR_EACH(cp, conds) {
619 if (!SCM_CONDITIONP(SCM_CAR(cp))) goto err;
620 }
621 if (!SCM_NULLP(cp)) {
622 err:
623 Scm_Error("conditions slot of a compound condition must be a list of conditions, but got %S", conds);
624 }
625 obj->conditions = conds;
626 }
627
628 static ScmClassStaticSlotSpec compound_slots[] = {
629 SCM_CLASS_SLOT_SPEC("%conditions", conditions_get, conditions_set),
630 SCM_CLASS_SLOT_SPEC_END()
631 };
632
633
634 /*
635 * C-level Constructors & generic API
636 */
637
638 /* actual class structure of thread exceptions are in ext/threads */
639 ScmObj Scm_MakeThreadException(ScmClass *klass, ScmVM *thread)
640 {
641 ScmThreadException *e = SCM_NEW(ScmThreadException);
642 SCM_SET_CLASS(e, klass);
643 e->thread = thread;
644 e->data = SCM_UNDEFINED;
645 return SCM_OBJ(e);
646 }
647
648 ScmObj Scm_MakeError(ScmObj message)
649 {
650 ScmError *e = SCM_ERROR(message_allocate(SCM_CLASS_ERROR, SCM_NIL));
651 e->message = SCM_LIST2(message, message);
652 return SCM_OBJ(e);
653 }
654
655 ScmObj Scm_MakeSystemError(ScmObj message, int en)
656 {
657 ScmSystemError *e =
658 SCM_SYSTEM_ERROR(syserror_allocate(SCM_CLASS_SYSTEM_ERROR, SCM_NIL));
659 e->common.message = SCM_LIST2(message, message);
660 e->error_number = en;
661 return SCM_OBJ(e);
662 }
663
664 ScmObj Scm_MakeUnboundVariableError(ScmObj identifier)
665 {
666 static ScmString *prefix = NULL;
667 if (prefix == NULL) {
668 prefix = SCM_STRING(SCM_MAKE_STR_IMMUTABLE("unbound variable"));
669 }
670
671 ScmUnboundVariableError *e =
672 SCM_UNBOUND_VARIABLE_ERROR(unberror_allocate(SCM_CLASS_UNBOUND_VARIABLE_ERROR, SCM_NIL));
673 ScmObj msg = Scm_Sprintf("%A: %S", SCM_OBJ(prefix), identifier);
674 e->common.message = SCM_LIST3(msg, SCM_OBJ(prefix), identifier);
675 e->identifier = identifier;
676 return SCM_OBJ(e);
677 }
678
679 ScmObj Scm_MakeReadError(ScmObj message, ScmPort *port, int line)
680 {
681 ScmReadError *e =
682 SCM_READ_ERROR(readerror_allocate(SCM_CLASS_READ_ERROR, SCM_NIL));
683 e->common.message = SCM_LIST2(message, message);
684 e->port = port;
685 e->line = line;
686 return SCM_OBJ(e);
687 }
688
689 int Scm_ConditionHasType(ScmObj c, ScmObj k)
690 {
691 if (!SCM_CONDITIONP(c)) return FALSE;
692 if (!SCM_CLASSP(k)) return FALSE;
693 if (!SCM_COMPOUND_CONDITION_P(c)) return SCM_ISA(c, SCM_CLASS(k));
694
695 ScmObj cp;
696 SCM_FOR_EACH(cp, SCM_COMPOUND_CONDITION(c)->conditions) {
697 if (SCM_ISA(SCM_CAR(cp), SCM_CLASS(k))) return TRUE;
698 }
699 return FALSE;
700 }
701
702 ScmObj Scm_ConditionMessage(ScmObj c)
703 {
704 if (SCM_MESSAGE_CONDITION_P(c)) {
705 return message_get(SCM_MESSAGE_CONDITION(c));
706 } else if (SCM_COMPOUND_CONDITION_P(c)) {
707 ScmObj cp;
708 SCM_FOR_EACH(cp, SCM_COMPOUND_CONDITION(c)->conditions) {
709 if (SCM_MESSAGE_CONDITION_P(SCM_CAR(cp))) {
710 return message_get(SCM_MESSAGE_CONDITION(SCM_CAR(cp)));
711 }
712 }
713 }
714 return SCM_FALSE;
715 }
716
717 /* Returns a ScmString representiong the 'type name' of the condition,
718 suitable for the error message. Because of personal preference
719 and backward compatibility, I upcase the class name of the condition
720 sans brackets. If it is a composite condition, the component's typenames
721 are joind with commas, excluding mixin conditions.
722 */
723 ScmObj Scm_ConditionTypeName(ScmObj c)
724 {
725 ScmObj sname;
726 static SCM_DEFINE_STRING_CONST(cond_name_delim, ",", 1, 1);
727
728 /* just a safety net */
729 if (!SCM_CONDITIONP(c)) return SCM_MAKE_STR("(not a condition)");
730
731 if (!SCM_COMPOUND_CONDITION_P(c)) {
732 sname = Scm_ShortClassName(Scm_ClassOf(c));
733 } else {
734 ScmObj h = SCM_NIL, t = SCM_NIL, cp;
735 SCM_FOR_EACH(cp, SCM_COMPOUND_CONDITION(c)->conditions) {
736 ScmObj cc = SCM_CAR(cp);
737 if (SCM_MIXIN_CONDITION_P(cc)) continue;
738 SCM_APPEND1(h, t, Scm_ShortClassName(Scm_ClassOf(cc)));
739 }
740 if (SCM_NULLP(h)) {
741 /* not usual, but tolerate */
742 sname = Scm_ShortClassName(Scm_ClassOf(c));
743 } else {
744 sname = Scm_StringJoin(h, &cond_name_delim, SCM_STRING_JOIN_INFIX);
745 }
746 }
747
748 ScmDString ds;
749 Scm_DStringInit(&ds);
750 ScmObj p = Scm_MakeInputStringPort(SCM_STRING(sname), TRUE);
751 int ch;
752 while ((ch = Scm_Getc(SCM_PORT(p))) != EOF) {
753 Scm_DStringPutc(&ds, Scm_CharUpcase(ch));
754 }
755 return Scm_DStringGet(&ds, 0);
756 }
757
758 /*================================================================
759 * Error handling
760 *
761 * The interaction with dynamic environment of VM is handled by
762 * Scm_VMThrowException() in vm.c. These routines provide
763 * application interface.
764 *
765 * Note on Windows system error: Windows may return error code
766 * in two different ways; GetLastError for Windows API, and errno
767 * for posix-compatibility calls. When Scm_SysError is called
768 * we don't know which error code to look at for sure. As a
769 * convention, we always clear both error code after Scm_SysError
770 * and assumes whichever non-zero code indicates the actual error.
771 * To map to integer error code, we reverse the sign of Windows
772 * error code (Windows error code reserves full 32bit, but they don't
773 * seem to use over 2^31 for the time being).
774 */
775
776 /* Double fault check
777 * In order to aviod infinite loop when error throwing routine
778 * throws an error, we use vm flag SCM_ERROR_BEING_HANDLED to
779 * check that. Ideally a common single API should handle it,
780 * but for the time being, we add the check at the beginning
781 * of Scm_*Error APIs.
782 * The SCM_ERROR_BEING_HANDLED flag is cleared in Scm_VMThrowException().
783 */
784 #define SCM_ERROR_DOUBLE_FAULT_CHECK(vm, msg) \
785 do { \
786 if (SCM_VM_RUNTIME_FLAG_IS_SET(vm, SCM_ERROR_BEING_HANDLED)) { \
787 ScmDString ds; \
788 Scm_DStringInit(&ds); \
789 Scm_DStringPutz(&ds, "Error occurred in error handler (", -1); \
790 Scm_DStringPutz(&ds, msg, -1); \
791 Scm_DStringPutz(&ds, ")", -1); \
792 ScmObj e = Scm_MakeError(Scm_DStringGet(&ds, 0)); \
793 Scm_VMThrowException(vm, e, SCM_RAISE_NON_CONTINUABLE); \
794 } \
795 SCM_VM_RUNTIME_FLAG_SET(vm, SCM_ERROR_BEING_HANDLED); \
796 } while (0)
797
798 /* Common part to format error message passed as varargs.
799 * ostr must be declared as ScmObj, and msg must be the last
800 * arg of function before '...'.
801 */
802 #define SCM_ERROR_MESSAGE_FORMAT(ostr, msg) \
803 do { \
804 ostr = Scm_MakeOutputStringPort(TRUE); \
805 va_list args__; \
806 va_start(args__, msg); \
807 Scm_Vprintf(SCM_PORT(ostr), msg, args__, TRUE); \
808 va_end(args__); \
809 } while (0)
810
811 /* Like SCM_ERROR_MESSAGE_FORMAT, but we also append system error message.
812 * Need to pass errno, which should be fetched by get_errno() *before*
813 * calling Scm_VM().
814 */
815 #define SCM_SYSERROR_MESSAGE_FORMAT(ostr, msg, en) \
816 do { \
817 SCM_ERROR_MESSAGE_FORMAT(ostr, msg); \
818 ScmObj syserr = get_syserrmsg(en); \
819 SCM_PUTZ(": ", -1, ostr); \
820 SCM_PUTS(syserr, ostr); \
821 } while (0)
822
823 /*
824 * C-like interface
825 */
826 void Scm_Error(const char *msg, ...)
827 {
828 ScmVM *vm = Scm_VM();
829 SCM_ERROR_DOUBLE_FAULT_CHECK(vm, msg);
830 ScmObj ostr;
831 SCM_ERROR_MESSAGE_FORMAT(ostr, msg);
832 ScmObj e = Scm_MakeError(Scm_GetOutputString(SCM_PORT(ostr), TRUE));
833 Scm_VMThrowException(vm, e, SCM_RAISE_NON_CONTINUABLE);
834 Scm_Panic("Scm_Error: Scm_VMThrowException returned. something wrong.");
835 }
836
837 /*
838 * Just for convenience to report a system error. Add strerror() message
839 * after the provided message.
840 */
841 static ScmObj get_syserrmsg(int en)
842 {
843 ScmObj syserr;
844 #if !defined(GAUCHE_WINDOWS)
845 syserr = SCM_MAKE_STR_COPYING(strerror(en));
846 #else /*GAUCHE_WINDOWS*/
847 if (en < 0) {
848 ScmObj ostr = Scm_MakeOutputStringPort(TRUE);
849 LPTSTR msgbuf = NULL;
850 const char *xmsgbuf;
851 if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
852 FORMAT_MESSAGE_FROM_SYSTEM |
853 FORMAT_MESSAGE_IGNORE_INSERTS,
854 NULL,
855 -en,
856 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
857 (LPTSTR)&msgbuf,
858 0, NULL)) {
859 xmsgbuf = SCM_WCS2MBS(msgbuf);
860 SCM_PUTZ(xmsgbuf, -1, ostr);
861 }
862 LocalFree(msgbuf);
863 Scm_Printf(SCM_PORT(ostr), "(error code = %d)", -en);
864 syserr = Scm_GetOutputString(SCM_PORT(ostr), 0);
865 } else {
866 syserr = SCM_MAKE_STR_COPYING(strerror(en));
867 }
868 #endif /*GAUCHE_WINDOWS*/
869 return syserr;
870 }
871
872 static int get_errno(void)
873 {
874 #if !defined(GAUCHE_WINDOWS)
875 return errno;
876 #else /*GAUCHE_WINDOWS*/
877 int en;
878
879 if (errno == 0) {
880 en = -(int)GetLastError();
881 } else {
882 en = errno; /* NB: MSDN says we should use _get_errno,
883 but MinGW doesn't seem to have it yet.*/
884 }
885
886 /* Reset the error code, so that we can find which is the actual
887 error code in the next occasion. */
888 errno = 0; /* NB: MSDN says we should use _set_errno,
889 but MinGW doesn't seem to have it yet. */
890 SetLastError(0);
891
892 return en;
893 #endif /*GAUCHE_WINDOWS*/
894 }
895
896 void Scm_SysError(const char *msg, ...)
897 {
898 int en = get_errno(); /* must take this before Scm_VM() */
899 ScmVM *vm = Scm_VM();
900 SCM_ERROR_DOUBLE_FAULT_CHECK(vm, msg);
901
902 ScmObj ostr;
903 SCM_SYSERROR_MESSAGE_FORMAT(ostr, msg, en);
904 ScmObj e = Scm_MakeSystemError(Scm_GetOutputString(SCM_PORT(ostr), TRUE), en);
905 Scm_VMThrowException(vm, e, SCM_RAISE_NON_CONTINUABLE);
906 Scm_Panic("Scm_Error: Scm_VMThrowException returned. something wrong.");
907 }
908
909 /*
910 * A convenience function to raise argument-type-violation errors.
911 * Right now it raises <error>, but it'll be changed once we adopt R6RS
912 * (The draft R6RS defines 'contract violation' in such cases).
913 */
914 void Scm_TypeError(const char *what, const char *expected, ScmObj got)
915 {
916 Scm_Error("%s is supposed to be of type %s, but got %S",
917 what, expected, got);
918 }
919
920
921 /*
922 * A convenience function to raise port-related errors.
923 * It creates either one of port error instance,
924 * depending on the 'reason' argument.
925 * If errno isn't zero, it also creates a <system-error> and throws
926 * a compound condition of both.
927 */
928 void raise_port_error(ScmVM *vm, ScmPort *port, int reason, ScmObj auxinfo,
929 int orig_errno, const char *msg, va_list args)
930 {
931 Scm_SetPortErrorOccurred(port, TRUE);
932
933 ScmObj ostr = Scm_MakeOutputStringPort(TRUE);
934 Scm_Vprintf(SCM_PORT(ostr), msg, args, TRUE);
935 ScmObj syserr = get_syserrmsg(orig_errno);
936 Scm_Putz(": ", -1, SCM_PORT(ostr));
937 Scm_Puts(SCM_STRING(syserr), SCM_PORT(ostr));
938 ScmObj smsg = Scm_GetOutputString(SCM_PORT(ostr), TRUE);
939
940 ScmClass *peclass;
941 switch (reason) {
942 case SCM_PORT_ERROR_INPUT:
943 peclass = SCM_CLASS_IO_READ_ERROR; break;
944 case SCM_PORT_ERROR_OUTPUT:
945 peclass = SCM_CLASS_IO_WRITE_ERROR; break;
946 case SCM_PORT_ERROR_CLOSED:
947 peclass = SCM_CLASS_IO_CLOSED_ERROR; break;
948 case SCM_PORT_ERROR_UNIT:
949 peclass = SCM_CLASS_IO_UNIT_ERROR; break;
950 case SCM_PORT_ERROR_DECODING:
951 peclass = SCM_CLASS_IO_DECODING_ERROR; break;
952 case SCM_PORT_ERROR_ENCODING:
953 peclass = SCM_CLASS_IO_ENCODING_ERROR; break;
954 case SCM_PORT_ERROR_SEEK:
955 peclass = SCM_CLASS_PORT_ERROR; break;
956 case SCM_PORT_ERROR_INVALID_POSITION:
957 peclass = SCM_CLASS_IO_INVALID_POSITION_ERROR; break;
958 default:
959 peclass = SCM_CLASS_PORT_ERROR; break;
960 }
961
962 ScmObj pe = porterror_allocate(peclass, SCM_NIL);
963 SCM_ERROR(pe)->message = SCM_LIST2(smsg, smsg);
964 SCM_PORT_ERROR(pe)->port = port;
965 SCM_PORT_ERROR(pe)->auxinfo = auxinfo;
966
967 ScmObj e = pe;
968 if (orig_errno != 0) {
969 e = Scm_MakeCompoundCondition(SCM_LIST2(Scm_MakeSystemError(smsg, orig_errno),
970 pe));
971 }
972 Scm_VMThrowException(vm, e, SCM_RAISE_NON_CONTINUABLE);
973 }
974
975 void Scm_PortErrorWithAux(ScmPort *port, int reason, ScmObj auxinfo,
976 const char *msg, ...)
977 {
978 int en = get_errno(); /* must take this before Scm_VM() */
979 ScmVM *vm = Scm_VM();
980 SCM_ERROR_DOUBLE_FAULT_CHECK(vm, msg);
981
982 va_list ap;
983 va_start(ap, msg);
984 raise_port_error(vm, port, reason, auxinfo, en, msg, ap);
985 va_end(ap);
986 Scm_Panic("Scm_Error: Scm_VMThrowException returned. something wrong.");
987 }
988
989 void Scm_PortError(ScmPort *port, int reason, const char *msg, ...)
990 {
991 int en = get_errno(); /* must take this before Scm_VM() */
992 ScmVM *vm = Scm_VM();
993 SCM_ERROR_DOUBLE_FAULT_CHECK(vm, msg);
994
995 va_list ap;
996 va_start(ap, msg);
997 raise_port_error(vm, port, reason, SCM_NIL, en, msg, ap);
998 va_end(ap);
999 Scm_Panic("Scm_Error: Scm_VMThrowException returned. something wrong.");
1000 }
1001
1002 /*
1003 * Just print warning
1004 * TODO: customize behavior
1005 */
1006
1007 void Scm_Warn(const char *msg, ...)
1008 {
1009 if (Scm_GetEnv("GAUCHE_SUPPRESS_WARNING") != NULL) return;
1010 va_list args;
1011 va_start(args, msg);
1012 Scm_Printf(SCM_CURERR, "WARNING: %A\n", Scm_Vsprintf(msg, args, TRUE));
1013 Scm_Flush(SCM_CURERR);
1014 va_end(args);
1015 }
1016
1017 /* OBSOLETED: 'warn' is now in Scheme. */
1018 void Scm_FWarn(ScmString *fmt SCM_UNUSED, ScmObj args SCM_UNUSED)
1019 {
1020 Scm_Error("Scm_FWarn is obsoleted");
1021 }
1022
1023 /*
1024 * General exception raising
1025 */
1026
1027 /* An external API to hide Scm_VMThrowException. */
1028 ScmObj Scm_Raise(ScmObj condition, u_long flags)
1029 {
1030 return Scm_VMThrowException(Scm_VM(), condition, flags);
1031 }
1032
1033
1034 /* A convenient API---allows to call user-defined condition easily,
1035 even the condition type is defined in Scheme. For example:
1036
1037 Scm_RaiseCondition(SCM_SYMBOL_VALUE("mymodule", "<my-error>"),
1038 "error-type", SCM_INTERN("fatal"),
1039 "error-code", SCM_MAKE_INT(3),
1040 SCM_RAISE_CONDITION_MESSAGE,
1041 "Fatal error occurred at %S", current_proc);
1042
1043 roughly corresponds to the Scheme code:
1044
1045 (raise (condition
1046 (<my-error> (error-type 'fatal)
1047 (error-code 3)
1048 (message (format "Fatal error occurred at ~s"
1049 current_proc)))))
1050
1051 This function isn't very efficient; but sometimes you want the convenience
1052 more, right?
1053
1054 The argument list format:
1055
1056 <condition-type> {<string-slot-name> <value>}* <terminator>
1057
1058 <terminator> : SCM_RAISE_CONDITION_MESSAGE <fmtstr> <fmtarg> ...
1059 | NULL
1060 */
1061
1062 ScmObj Scm_RaiseCondition(ScmObj condition_type, ...)
1063 {
1064 ScmObj argh = SCM_NIL, argt = SCM_NIL;
1065 va_list ap;
1066
1067 if (!SCM_CLASSP(condition_type)
1068 || !Scm_SubclassP(SCM_CLASS(condition_type), SCM_CLASS_CONDITION)) {
1069 /* If we don't get a condition type, fallback to a normal error. */
1070 condition_type = SCM_OBJ(SCM_CLASS_ERROR);
1071 }
1072 SCM_APPEND1(argh, argt, condition_type);
1073 va_start(ap, condition_type);
1074 for (;;) {
1075 const char *key = va_arg(ap, const char *);
1076 if (key == NULL) {
1077 break;
1078 } else if (key == SCM_RAISE_CONDITION_MESSAGE) {
1079 const char *msg = va_arg(ap, const char*);
1080 ScmObj ostr = Scm_MakeOutputStringPort(TRUE);
1081 Scm_Vprintf(SCM_PORT(ostr), msg, ap, TRUE);
1082 SCM_APPEND1(argh, argt, SCM_MAKE_KEYWORD("message"));
1083 SCM_APPEND1(argh, argt, Scm_GetOutputString(SCM_PORT(ostr), 0));
1084 break;
1085 } else {
1086 ScmObj arg = va_arg(ap, ScmObj);
1087 SCM_APPEND1(argh, argt, SCM_MAKE_KEYWORD(key));
1088 SCM_APPEND1(argh, argt, arg);
1089 }
1090 }
1091 va_end(ap);
1092 return Scm_ApplyRec(SCM_SYMBOL_VALUE("gauche", "error"), argh);
1093 }
1094
1095 /*
1096 * Show stack or call trace.
1097 * stacklite - return value of Scm_VMGetStackLite or Scm_VMGetCallTraceLite
1098 * maxdepth - maximum # of entries to be shown.
1099 * 0 to use the default. -1 for unlimited.
1100 * skip - ignore this number of frames. Useful to call this from
1101 * a Scheme error handling routine, in order to skip the
1102 * frames of the handler itself.
1103 * offset - add this to the frame number. Useful to show a middle part
1104 * of frames only, by combining the skip parameter.
1105 * format - SCM_STACK_TRACE_FORMAT_* enum value. No longer used.
1106 */
1107 void Scm_ShowStackTrace(ScmPort *out, ScmObj stacklite,
1108 int maxdepth, int skip, int offset,
1109 int format SCM_UNUSED)
1110 {
1111 static ScmObj show_stack_trace = SCM_UNDEFINED;
1112 SCM_BIND_PROC(show_stack_trace,
1113 "%show-stack-trace",
1114 Scm_GaucheInternalModule());
1115 Scm_ApplyRec5(show_stack_trace, stacklite, SCM_OBJ(out),
1116 SCM_MAKE_INT(maxdepth), SCM_MAKE_INT(skip),
1117 SCM_MAKE_INT(offset));
1118 }
1119
1120 /* Dump stack trace. Called from the default error reporter.
1121 Also intended to be called from the debugger, so we allow vm to be NULL
1122 to mean the current VM, and port to be NULL for the current error port. */
1123 void Scm_DumpStackTrace(ScmVM *vm, ScmPort *port)
1124 {
1125 if (vm == NULL) vm = Scm_VM();
1126 if (port == NULL) port = SCM_CURERR;
1127 ScmObj stack = Scm_VMGetStackLite(vm);
1128 ScmObj calls = Scm_VMGetCallTraceLite(vm);
1129 SCM_PUTZ("Stack Trace:\n", -1, port);
1130 SCM_PUTZ("_______________________________________\n", -1, port);
1131 Scm_ShowStackTrace(port, stack, 0, 0, 0, 0);
1132 if (SCM_PAIRP(calls)) {
1133 SCM_PUTZ("Call Trace:\n", -1, port);
1134 SCM_PUTZ("_______________________________________\n", -1, port);
1135 Scm_ShowStackTrace(port, calls, 0, 0, 0, 0);
1136 }
1137 SCM_FLUSH(port);
1138 }
1139
1140 /*
1141 * Default error reporter
1142 */
1143
1144 /* The default procedure to display the header of error message.
1145 E is a thrown condition, not necessarily an error object.
1146
1147 The actual operation is written in Scheme (libexc.scm). However,
1148 we can't use that before the infrastructure is fully booted; so
1149 this routine has a fallback which will be used only during initialization.
1150 */
1151 static void Scm_PrintDefaultErrorHeading(ScmObj e, ScmPort *out)
1152 {
1153 if (Scm_RuntimeState() >= SCM_RUNTIME_INITIALIZED) {
1154 static ScmObj print_default_error_heading = SCM_UNDEFINED;
1155 SCM_BIND_PROC(print_default_error_heading,
1156 "print-default-error-heading",
1157 Scm_GaucheModule());
1158 Scm_ApplyRec2(print_default_error_heading, e, SCM_OBJ(out));
1159 static ScmObj print_additional_error_heading = SCM_UNDEFINED;
1160 SCM_BIND_PROC(print_additional_error_heading,
1161 "print-additional-error-heading",
1162 Scm_GaucheModule());
1163 Scm_ApplyRec2(print_additional_error_heading, e, SCM_OBJ(out));
1164 } else {
1165 /* Error during initialization. */
1166 if (SCM_CONDITIONP(e)) {
1167 Scm_Printf(out, "*** %A: %A\n",
1168 Scm_ConditionTypeName(e),
1169 Scm_ConditionMessage(e));
1170 } else {
1171 Scm_Printf(out, "*** ERROR: Unhandled condition: %S\n", e);
1172 }
1173 }
1174 }
1175
1176 /* We treat out == #f or #t just like 'format' - #t for the current output
1177 port, and #f for string port. If it's output port, use it. For any
1178 other objects, we use current error port. This permissive behavior is
1179 intentional - report-error is usually called during error handling,
1180 and raising an error there masks the original error. However, the
1181 caller should explicitly pass SCM_OBJ(SCM_CURERR) if that's what it intends,
1182 instead of relying on the permissive behavior.
1183 */
1184 ScmObj Scm_ReportError(ScmObj e, ScmObj out)
1185 {
1186 ScmVM *vm = Scm_VM();
1187 ScmPort *port = SCM_CURERR;
1188 if (SCM_FALSEP(out)) {
1189 port = SCM_PORT(Scm_MakeOutputStringPort(TRUE));
1190 } else if (SCM_TRUEP(out)) {
1191 port = SCM_CUROUT;
1192 } else if (SCM_OPORTP(out)) {
1193 port = SCM_PORT(out);
1194 }
1195 Scm_PrintDefaultErrorHeading(e, port);
1196 Scm_DumpStackTrace(vm, port);
1197 if (SCM_FALSEP(out)) return Scm_GetOutputString(SCM_PORT(port), 0);
1198 else return SCM_UNDEFINED;
1199 }
1200
1201 /*
1202 * Initialization
1203 */
1204 void Scm__InitExceptions(void)
1205 {
1206 ScmModule *mod = Scm_GaucheModule();
1207
1208 ScmObj mes_ser_supers
1209 = SCM_LIST2(SCM_OBJ(SCM_CLASS_MESSAGE_CONDITION),
1210 SCM_OBJ(SCM_CLASS_SERIOUS_CONDITION));
1211 ScmObj com_ser_supers
1212 = SCM_LIST2(SCM_OBJ(SCM_CLASS_COMPOUND_CONDITION),
1213 SCM_OBJ(SCM_CLASS_SERIOUS_CONDITION));
1214
1215 Scm_InitStaticClassWithMeta(SCM_CLASS_CONDITION,
1216 "<condition>",
1217 mod, NULL, SCM_FALSE, NULL, 0);
1218 ScmClass *cond_meta = Scm_ClassOf(SCM_OBJ(SCM_CLASS_CONDITION));
1219 Scm_InitStaticClassWithMeta(SCM_CLASS_SERIOUS_CONDITION,
1220 "<serious-condition>",
1221 mod, cond_meta, SCM_FALSE, NULL, 0);
1222 Scm_InitStaticClassWithMeta(SCM_CLASS_MESSAGE_CONDITION,
1223 "<message-condition>",
1224 mod, cond_meta, SCM_FALSE, message_slots, 0);
1225 Scm_InitStaticClassWithMeta(SCM_CLASS_MIXIN_CONDITION,
1226 "<mixin-condition>",
1227 mod, cond_meta, SCM_FALSE, NULL, 0);
1228
1229 Scm_InitStaticClassWithMeta(SCM_CLASS_ERROR,
1230 "<error>",
1231 mod, cond_meta, mes_ser_supers,
1232 message_slots, 0);
1233 Scm_InitStaticClassWithMeta(SCM_CLASS_SYSTEM_ERROR,
1234 "<system-error>",
1235 mod, cond_meta, SCM_FALSE,
1236 syserror_slots, 0);
1237 Scm_InitStaticClassWithMeta(SCM_CLASS_UNHANDLED_SIGNAL_ERROR,
1238 "<unhandled-signal-error>",
1239 mod, cond_meta, SCM_FALSE,
1240 sigerror_slots, 0);
1241 Scm_InitStaticClassWithMeta(SCM_CLASS_UNBOUND_VARIABLE_ERROR,
1242 "<unbound-variable-error>",
1243 mod, cond_meta, SCM_FALSE,
1244 unberror_slots, 0);
1245 Scm_InitStaticClassWithMeta(SCM_CLASS_READ_ERROR,
1246 "<read-error>",
1247 mod, cond_meta, SCM_FALSE,
1248 readerror_slots, 0);
1249 Scm_InitStaticClassWithMeta(SCM_CLASS_IO_ERROR,
1250 "<io-error>",
1251 mod, cond_meta, SCM_FALSE,
1252 NULL, 0);
1253 Scm_InitStaticClassWithMeta(SCM_CLASS_PORT_ERROR,
1254 "<port-error>",
1255 mod, cond_meta, SCM_FALSE,
1256 porterror_slots, 0);
1257 Scm_InitStaticClassWithMeta(SCM_CLASS_IO_READ_ERROR,
1258 "<io-read-error>",
1259 mod, cond_meta, SCM_FALSE,
1260 porterror_slots, 0);
1261 Scm_InitStaticClassWithMeta(SCM_CLASS_IO_WRITE_ERROR,
1262 "<io-write-error>",
1263 mod, cond_meta, SCM_FALSE,
1264 porterror_slots, 0);
1265 Scm_InitStaticClassWithMeta(SCM_CLASS_IO_CLOSED_ERROR,
1266 "<io-closed-error>",
1267 mod, cond_meta, SCM_FALSE,
1268 porterror_slots, 0);
1269 Scm_InitStaticClassWithMeta(SCM_CLASS_IO_UNIT_ERROR,
1270 "<io-unit-error>",
1271 mod, cond_meta, SCM_FALSE,
1272 porterror_slots, 0);
1273 Scm_InitStaticClassWithMeta(SCM_CLASS_IO_DECODING_ERROR,
1274 "<io-decoding-error>",
1275 mod, cond_meta, SCM_FALSE,
1276 porterror_slots, 0);
1277 Scm_InitStaticClassWithMeta(SCM_CLASS_IO_ENCODING_ERROR,
1278 "<io-encoding-error>",
1279 mod, cond_meta, SCM_FALSE,
1280 encodingerror_slots, 0);
1281 Scm_InitStaticClassWithMeta(SCM_CLASS_IO_INVALID_POSITION_ERROR,
1282 "<io-invalid-position-error>",
1283 mod, cond_meta, SCM_FALSE,
1284 invalidpositionerror_slots, 0);
1285
1286 Scm_InitStaticClassWithMeta(SCM_CLASS_CONTINUATION_ERROR,
1287 "<continuation-violation>",
1288 mod, cond_meta, SCM_FALSE,
1289 conterror_slots, 0);
1290
1291 Scm_InitStaticClassWithMeta(SCM_CLASS_COMPOUND_CONDITION,
1292 "<compound-condition>",
1293 mod, cond_meta, SCM_FALSE,
1294 compound_slots, 0);
1295 Scm_InitStaticClassWithMeta(SCM_CLASS_SERIOUS_COMPOUND_CONDITION,
1296 "<serious-compound-condition>",
1297 mod, cond_meta, com_ser_supers,
1298 compound_slots, 0);
1299
1300 sym_offending_char = SCM_INTERN("offending-char");
1301 sym_position = SCM_INTERN("position");
1302 }