1 /* Copyright (C) 1995, 1996, 1997, 1998 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. */
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
48 #include "libguile/_scm.h"
49 #include "libguile/smob.h"
50 #include "libguile/alist.h"
51 #include "libguile/eval.h"
52 #include "libguile/eq.h"
53 #include "libguile/dynwind.h"
54 #include "libguile/backtrace.h"
55 #ifdef DEBUG_EXTENSIONS
56 #include "libguile/debug.h"
58 #include "libguile/continuations.h"
59 #include "libguile/stackchk.h"
60 #include "libguile/stacks.h"
61 #include "libguile/fluids.h"
62 #include "libguile/ports.h"
64 #include "libguile/validate.h"
65 #include "libguile/throw.h"
68 /* the jump buffer data structure */
69 static int scm_tc16_jmpbuffer
;
71 #define SCM_JMPBUFP(OBJ) (SCM_NIMP(OBJ) && (SCM_TYP16(OBJ) == scm_tc16_jmpbuffer))
73 #define JBACTIVE(OBJ) (SCM_CELL_WORD_0 (OBJ) & (1L << 16L))
74 #define ACTIVATEJB(OBJ) (SCM_SETOR_CAR (OBJ, (1L << 16L)))
75 #define DEACTIVATEJB(OBJ) (SCM_SETAND_CAR (OBJ, ~(1L << 16L)))
77 #ifndef DEBUG_EXTENSIONS
78 #define JBJMPBUF(OBJ) ((jmp_buf *) SCM_CELL_WORD_1 (OBJ))
79 #define SETJBJMPBUF(x,v) (SCM_SET_CELL_WORD_1 ((x), (v)))
81 #define SCM_JBDFRAME(x) ((scm_debug_frame *) SCM_CELL_WORD_0 (SCM_CDR (x)))
82 #define JBJMPBUF(OBJ) ((jmp_buf *) SCM_CELL_WORD_1 (SCM_CDR (OBJ)))
83 #define SCM_SETJBDFRAME(OBJ,X) (SCM_SET_CELL_WORD_0 (SCM_CDR (OBJ), (X)))
84 #define SETJBJMPBUF(OBJ,X) (SCM_SET_CELL_WORD_1 (SCM_CDR (OBJ), (X)))
89 scm_must_free ((char *) SCM_CELL_WORD_1 (jbsmob
));
90 return sizeof (scm_cell
);
95 printjb (SCM exp
, SCM port
, scm_print_state
*pstate
)
97 scm_puts ("#<jmpbuffer ", port
);
98 scm_puts (JBACTIVE(exp
) ? "(active) " : "(inactive) ", port
);
99 scm_intprint((long) JBJMPBUF (exp
), 16, port
);
101 scm_putc ('>', port
);
112 #ifdef DEBUG_EXTENSIONS
113 char *mem
= scm_must_malloc (sizeof (scm_cell
), "jb");
115 #ifdef DEBUG_EXTENSIONS
116 SCM_NEWSMOB (answer
, scm_tc16_jmpbuffer
, mem
);
118 SCM_NEWSMOB (answer
, scm_tc16_jmpbuffer
, 0);
120 SETJBJMPBUF(answer
, (jmp_buf *)0);
121 DEACTIVATEJB(answer
);
128 /* scm_internal_catch (the guts of catch) */
130 struct jmp_buf_and_retval
/* use only on the stack, in scm_catch */
132 jmp_buf buf
; /* must be first */
138 /* scm_internal_catch is the guts of catch. It handles all the
139 mechanics of setting up a catch target, invoking the catch body,
140 and perhaps invoking the handler if the body does a throw.
142 The function is designed to be usable from C code, but is general
143 enough to implement all the semantics Guile Scheme expects from
146 TAG is the catch tag. Typically, this is a symbol, but this
147 function doesn't actually care about that.
149 BODY is a pointer to a C function which runs the body of the catch;
150 this is the code you can throw from. We call it like this:
153 BODY_DATA is just the BODY_DATA argument we received; we pass it
154 through to BODY as its first argument. The caller can make
155 BODY_DATA point to anything useful that BODY might need.
157 HANDLER is a pointer to a C function to deal with a throw to TAG,
158 should one occur. We call it like this:
159 HANDLER (HANDLER_DATA, THROWN_TAG, THROW_ARGS)
161 HANDLER_DATA is the HANDLER_DATA argument we recevied; it's the
162 same idea as BODY_DATA above.
163 THROWN_TAG is the tag that the user threw to; usually this is
164 TAG, but it could be something else if TAG was #t (i.e., a
165 catch-all), or the user threw to a jmpbuf.
166 THROW_ARGS is the list of arguments the user passed to the THROW
167 function, after the tag.
169 BODY_DATA is just a pointer we pass through to BODY. HANDLER_DATA
170 is just a pointer we pass through to HANDLER. We don't actually
171 use either of those pointers otherwise ourselves. The idea is
172 that, if our caller wants to communicate something to BODY or
173 HANDLER, it can pass a pointer to it as MUMBLE_DATA, which BODY and
174 HANDLER can then use. Think of it as a way to make BODY and
175 HANDLER closures, not just functions; MUMBLE_DATA points to the
178 Of course, it's up to the caller to make sure that any data a
179 MUMBLE_DATA needs is protected from GC. A common way to do this is
180 to make MUMBLE_DATA a pointer to data stored in an automatic
181 structure variable; since the collector must scan the stack for
182 references anyway, this assures that any references in MUMBLE_DATA
186 scm_internal_catch (SCM tag
, scm_catch_body_t body
, void *body_data
, scm_catch_handler_t handler
, void *handler_data
)
188 struct jmp_buf_and_retval jbr
;
192 jmpbuf
= make_jmpbuf ();
194 scm_dynwinds
= scm_acons (tag
, jmpbuf
, scm_dynwinds
);
195 SETJBJMPBUF(jmpbuf
, &jbr
.buf
);
196 #ifdef DEBUG_EXTENSIONS
197 SCM_SETJBDFRAME(jmpbuf
, scm_last_debug_frame
);
199 if (setjmp (jbr
.buf
))
204 #ifdef STACK_CHECKING
205 scm_stack_checking_enabled_p
= SCM_STACK_CHECKING_P
;
208 DEACTIVATEJB (jmpbuf
);
209 scm_dynwinds
= SCM_CDR (scm_dynwinds
);
211 throw_args
= jbr
.retval
;
212 throw_tag
= jbr
.throw_tag
;
213 jbr
.throw_tag
= SCM_EOL
;
214 jbr
.retval
= SCM_EOL
;
215 answer
= handler (handler_data
, throw_tag
, throw_args
);
220 answer
= body (body_data
);
222 DEACTIVATEJB (jmpbuf
);
223 scm_dynwinds
= SCM_CDR (scm_dynwinds
);
231 /* scm_internal_lazy_catch (the guts of lazy catching) */
233 /* The smob tag for lazy_catch smobs. */
234 static long tc16_lazy_catch
;
236 /* This is the structure we put on the wind list for a lazy catch. It
237 stores the handler function to call, and the data pointer to pass
238 through to it. It's not a Scheme closure, but it is a function
239 with data, so the term "closure" is appropriate in its broader
242 (We don't need anything like this in the "eager" catch code,
243 because the same C frame runs both the body and the handler.) */
245 scm_catch_handler_t handler
;
249 /* Strictly speaking, we could just pass a zero for our print
250 function, because we don't need to print them. They should never
251 appear in normal data structures, only in the wind list. However,
252 it might be nice for debugging someday... */
254 print_lazy_catch (SCM closure
, SCM port
, scm_print_state
*pstate
)
256 struct lazy_catch
*c
= (struct lazy_catch
*) SCM_CELL_WORD_1 (closure
);
259 sprintf (buf
, "#<lazy-catch 0x%lx 0x%lx>",
260 (long) c
->handler
, (long) c
->handler_data
);
261 scm_puts (buf
, port
);
267 /* Given a pointer to a lazy catch structure, return a smob for it,
268 suitable for inclusion in the wind list. ("Ah yes, a Château
269 Gollombiere '72, non?"). */
271 make_lazy_catch (struct lazy_catch
*c
)
273 SCM_RETURN_NEWSMOB (tc16_lazy_catch
, c
);
276 #define SCM_LAZY_CATCH_P(obj) \
277 (SCM_NIMP (obj) && (SCM_UNPACK_CAR (obj) == tc16_lazy_catch))
280 /* Exactly like scm_internal_catch, except:
281 - It does not unwind the stack (this is the major difference).
282 - If handler returns, its value is returned from the throw. */
284 scm_internal_lazy_catch (SCM tag
, scm_catch_body_t body
, void *body_data
, scm_catch_handler_t handler
, void *handler_data
)
286 SCM lazy_catch
, answer
;
290 c
.handler_data
= handler_data
;
291 lazy_catch
= make_lazy_catch (&c
);
294 scm_dynwinds
= scm_acons (tag
, lazy_catch
, scm_dynwinds
);
297 answer
= (*body
) (body_data
);
300 scm_dynwinds
= SCM_CDR (scm_dynwinds
);
307 /* scm_internal_stack_catch
308 Use this one if you want debugging information to be stored in
309 scm_the_last_stack_fluid on error. */
312 ss_handler (void *data
, SCM tag
, SCM throw_args
)
315 scm_fluid_set_x (SCM_CDR (scm_the_last_stack_fluid
),
316 scm_make_stack (scm_cons (SCM_BOOL_T
, SCM_EOL
)));
317 /* Throw the error */
318 return scm_throw (tag
, throw_args
);
324 scm_catch_body_t body
;
329 cwss_body (void *data
)
331 struct cwss_data
*d
= data
;
332 return scm_internal_lazy_catch (d
->tag
, d
->body
, d
->data
, ss_handler
, NULL
);
336 scm_internal_stack_catch (SCM tag
,
337 scm_catch_body_t body
,
339 scm_catch_handler_t handler
,
346 return scm_internal_catch (tag
, cwss_body
, &d
, handler
, handler_data
);
351 /* body and handler functions for use with any of the above catch variants */
353 /* This is a body function you can pass to scm_internal_catch if you
354 want the body to be like Scheme's `catch' --- a thunk.
356 BODY_DATA is a pointer to a scm_body_thunk_data structure, which
357 contains the Scheme procedure to invoke as the body, and the tag
361 scm_body_thunk (void *body_data
)
363 struct scm_body_thunk_data
*c
= (struct scm_body_thunk_data
*) body_data
;
365 return scm_apply (c
->body_proc
, SCM_EOL
, SCM_EOL
);
369 /* This is a handler function you can pass to scm_internal_catch if
370 you want the handler to act like Scheme's catch: (throw TAG ARGS ...)
371 applies a handler procedure to (TAG ARGS ...).
373 If the user does a throw to this catch, this function runs a
374 handler procedure written in Scheme. HANDLER_DATA is a pointer to
375 an SCM variable holding the Scheme procedure object to invoke. It
376 ought to be a pointer to an automatic variable (i.e., one living on
377 the stack), or the procedure object should be otherwise protected
380 scm_handle_by_proc (void *handler_data
, SCM tag
, SCM throw_args
)
382 SCM
*handler_proc_p
= (SCM
*) handler_data
;
384 return scm_apply (*handler_proc_p
, scm_cons (tag
, throw_args
), SCM_EOL
);
387 /* SCM_HANDLE_BY_PROC_CATCHING_ALL is like SCM_HANDLE_BY_PROC but
388 catches all throws that the handler might emit itself. The handler
389 used for these `secondary' throws is SCM_HANDLE_BY_MESSAGE_NO_EXIT. */
397 hbpca_body (void *body_data
)
399 struct hbpca_data
*data
= (struct hbpca_data
*)body_data
;
400 return scm_apply (data
->proc
, data
->args
, SCM_EOL
);
404 scm_handle_by_proc_catching_all (void *handler_data
, SCM tag
, SCM throw_args
)
406 SCM
*handler_proc_p
= (SCM
*) handler_data
;
407 struct hbpca_data data
;
408 data
.proc
= *handler_proc_p
;
409 data
.args
= scm_cons (tag
, throw_args
);
411 return scm_internal_catch (SCM_BOOL_T
,
413 scm_handle_by_message_noexit
, NULL
);
416 /* Derive the an exit status from the arguments to (quit ...). */
418 scm_exit_status (SCM args
)
420 if (SCM_NNULLP (args
))
422 SCM cqa
= SCM_CAR (args
);
425 return (SCM_INUM (cqa
));
426 else if (SCM_FALSEP (cqa
))
434 handler_message (void *handler_data
, SCM tag
, SCM args
)
436 char *prog_name
= (char *) handler_data
;
437 SCM p
= scm_cur_errp
;
439 if (scm_ilength (args
) >= 3)
441 SCM stack
= scm_make_stack (SCM_LIST1 (SCM_BOOL_T
));
442 SCM subr
= SCM_CAR (args
);
443 SCM message
= SCM_CADR (args
);
444 SCM parts
= SCM_CADDR (args
);
445 SCM rest
= SCM_CDDDR (args
);
447 if (SCM_BACKTRACE_P
&& SCM_NFALSEP (stack
))
449 scm_puts ("Backtrace:\n", p
);
450 scm_display_backtrace (stack
, p
, SCM_UNDEFINED
, SCM_UNDEFINED
);
453 scm_display_error (stack
, p
, subr
, message
, parts
, rest
);
460 scm_puts (prog_name
, p
);
463 scm_puts ("uncaught throw to ", p
);
464 scm_prin1 (tag
, p
, 0);
466 scm_prin1 (args
, p
, 1);
472 /* This is a handler function to use if you want scheme to print a
473 message and die. Useful for dealing with throws to uncaught keys
476 At boot time, we establish a catch-all that uses this as its handler.
477 1) If the user wants something different, they can use (catch #t
478 ...) to do what they like.
479 2) Outside the context of a read-eval-print loop, there isn't
480 anything else good to do; libguile should not assume the existence
481 of a read-eval-print loop.
482 3) Given that we shouldn't do anything complex, it's much more
483 robust to do it in C code.
485 HANDLER_DATA, if non-zero, is assumed to be a char * pointing to a
486 message header to print; if zero, we use "guile" instead. That
487 text is followed by a colon, then the message described by ARGS. */
490 scm_handle_by_message (void *handler_data
, SCM tag
, SCM args
)
492 if (SCM_NFALSEP (scm_eq_p (tag
, SCM_CAR (scm_intern0 ("quit")))))
494 exit (scm_exit_status (args
));
497 handler_message (handler_data
, tag
, args
);
498 /* try to flush the error message first before the rest of the
499 ports: if any throw error, it currently causes a bus
505 /* This is just like scm_handle_by_message, but it doesn't exit; it
506 just returns #f. It's useful in cases where you don't really know
507 enough about the body to handle things in a better way, but don't
508 want to let throws fall off the bottom of the wind list. */
510 scm_handle_by_message_noexit (void *handler_data
, SCM tag
, SCM args
)
512 handler_message (handler_data
, tag
, args
);
519 scm_handle_by_throw (void *handler_data
, SCM tag
, SCM args
)
521 scm_ithrow (tag
, args
, 1);
522 return SCM_UNSPECIFIED
; /* never returns */
527 /* the Scheme-visible CATCH and LAZY-CATCH functions */
529 SCM_DEFINE (scm_catch
, "catch", 3, 0, 0,
530 (SCM tag
, SCM thunk
, SCM handler
),
531 "Invoke @var{thunk} in the dynamic context of @var{handler} for\n"
532 "exceptions matching @var{key}. If thunk throws to the symbol @var{key},\n"
533 "then @var{handler} is invoked this way:\n\n"
535 "(handler key args ...)\n"
537 "@var{key} is a symbol or #t.\n\n"
538 "@var{thunk} takes no arguments. If @var{thunk} returns normally, that\n"
539 "is the return value of @code{catch}.\n\n"
540 "Handler is invoked outside the scope of its own @code{catch}. If\n"
541 "@var{handler} again throws to the same key, a new handler from further\n"
542 "up the call chain is invoked.\n\n"
543 "If the key is @code{#t}, then a throw to @emph{any} symbol will match\n"
544 "this call to @code{catch}.")
545 #define FUNC_NAME s_scm_catch
547 struct scm_body_thunk_data c
;
549 SCM_ASSERT (SCM_SYMBOLP(tag
) || SCM_TRUE_P (tag
),
550 tag
, SCM_ARG1
, FUNC_NAME
);
555 /* scm_internal_catch takes care of all the mechanics of setting up
556 a catch tag; we tell it to call scm_body_thunk to run the body,
557 and scm_handle_by_proc to deal with any throws to this catch.
558 The former receives a pointer to c, telling it how to behave.
559 The latter receives a pointer to HANDLER, so it knows who to call. */
560 return scm_internal_catch (tag
,
562 scm_handle_by_proc
, &handler
);
567 SCM_DEFINE (scm_lazy_catch
, "lazy-catch", 3, 0, 0,
568 (SCM tag
, SCM thunk
, SCM handler
),
570 #define FUNC_NAME s_scm_lazy_catch
572 struct scm_body_thunk_data c
;
574 SCM_ASSERT (SCM_SYMBOLP(tag
) || SCM_TRUE_P (tag
),
575 tag
, SCM_ARG1
, FUNC_NAME
);
580 /* scm_internal_lazy_catch takes care of all the mechanics of
581 setting up a lazy catch tag; we tell it to call scm_body_thunk to
582 run the body, and scm_handle_by_proc to deal with any throws to
583 this catch. The former receives a pointer to c, telling it how
584 to behave. The latter receives a pointer to HANDLER, so it knows
586 return scm_internal_lazy_catch (tag
,
588 scm_handle_by_proc
, &handler
);
596 SCM_DEFINE (scm_throw
, "throw", 1, 0, 1,
598 "Invoke the catch form matching @var{key}, passing @var{args} to the\n"
599 "@var{handler}. \n\n"
600 "@var{key} is a symbol. It will match catches of the same symbol or of\n"
602 "If there is no handler at all, an error is signaled.")
603 #define FUNC_NAME s_scm_throw
605 SCM_VALIDATE_SYMBOL (1,key
);
606 /* May return if handled by lazy catch. */
607 return scm_ithrow (key
, args
, 1);
612 scm_ithrow (SCM key
, SCM args
, int noreturn
)
614 SCM jmpbuf
= SCM_UNDEFINED
;
617 SCM dynpair
= SCM_UNDEFINED
;
620 /* Search the wind list for an appropriate catch.
621 "Waiter, please bring us the wind list." */
622 for (winds
= scm_dynwinds
; SCM_NIMP (winds
); winds
= SCM_CDR (winds
))
624 if (! SCM_CONSP (winds
))
627 dynpair
= SCM_CAR (winds
);
628 if (SCM_CONSP (dynpair
))
630 SCM this_key
= SCM_CAR (dynpair
);
632 if (SCM_TRUE_P (this_key
) || SCM_EQ_P (this_key
, key
))
637 /* If we didn't find anything, abort. scm_boot_guile should
638 have established a catch-all, but obviously things are
639 thoroughly screwed up. */
640 if (SCM_NULLP (winds
))
643 /* If the wind list is malformed, bail. */
644 if (SCM_IMP (winds
) || SCM_NCONSP (winds
))
647 if (!SCM_FALSEP (dynpair
))
648 jmpbuf
= SCM_CDR (dynpair
);
652 return SCM_UNSPECIFIED
;
655 scm_exitval
= scm_cons (key
, args
);
656 scm_dowinds (SCM_EOL
, scm_ilength (scm_dynwinds
));
657 #ifdef DEBUG_EXTENSIONS
658 scm_last_debug_frame
= SCM_DFRAME (scm_rootcont
);
660 longjmp (SCM_JMPBUF (scm_rootcont
), 1);
664 for (wind_goal
= scm_dynwinds
;
665 !SCM_EQ_P (SCM_CDAR (wind_goal
), jmpbuf
);
666 wind_goal
= SCM_CDR (wind_goal
))
669 /* Is a lazy catch? In wind list entries for lazy catches, the key
670 is bound to a lazy_catch smob, not a jmpbuf. */
671 if (SCM_LAZY_CATCH_P (jmpbuf
))
673 struct lazy_catch
*c
= (struct lazy_catch
*) SCM_CELL_WORD_1 (jmpbuf
);
674 SCM oldwinds
= scm_dynwinds
;
676 scm_dowinds (wind_goal
, (scm_ilength (scm_dynwinds
)
677 - scm_ilength (wind_goal
)));
679 handle
= scm_dynwinds
;
680 scm_dynwinds
= SCM_CDR (scm_dynwinds
);
682 answer
= (c
->handler
) (c
->handler_data
, key
, args
);
684 SCM_SETCDR (handle
, scm_dynwinds
);
685 scm_dynwinds
= handle
;
687 scm_dowinds (oldwinds
, (scm_ilength (scm_dynwinds
)
688 - scm_ilength (oldwinds
)));
692 /* Otherwise, it's a normal catch. */
693 else if (SCM_JMPBUFP (jmpbuf
))
695 struct jmp_buf_and_retval
* jbr
;
696 scm_dowinds (wind_goal
, (scm_ilength (scm_dynwinds
)
697 - scm_ilength (wind_goal
)));
698 jbr
= (struct jmp_buf_and_retval
*)JBJMPBUF (jmpbuf
);
699 jbr
->throw_tag
= key
;
703 /* Otherwise, it's some random piece of junk. */
707 #ifdef DEBUG_EXTENSIONS
708 scm_last_debug_frame
= SCM_JBDFRAME (jmpbuf
);
710 longjmp (*JBJMPBUF (jmpbuf
), 1);
717 scm_tc16_jmpbuffer
= scm_make_smob_type_mfpe ("jmpbuffer",
718 #ifdef DEBUG_EXTENSIONS
730 tc16_lazy_catch
= scm_make_smob_type_mfpe ("lazy-catch", 0,
735 #include "libguile/throw.x"