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. */
46 #include "libguile/_scm.h"
47 #include "libguile/smob.h"
48 #include "libguile/alist.h"
49 #include "libguile/eval.h"
50 #include "libguile/eq.h"
51 #include "libguile/dynwind.h"
52 #include "libguile/backtrace.h"
53 #ifdef DEBUG_EXTENSIONS
54 #include "libguile/debug.h"
56 #include "libguile/continuations.h"
57 #include "libguile/stackchk.h"
58 #include "libguile/stacks.h"
59 #include "libguile/fluids.h"
60 #include "libguile/ports.h"
62 #include "libguile/validate.h"
63 #include "libguile/throw.h"
66 /* the jump buffer data structure */
67 static scm_t_bits tc16_jmpbuffer
;
69 #define SCM_JMPBUFP(OBJ) SCM_TYP16_PREDICATE (tc16_jmpbuffer, OBJ)
71 #define JBACTIVE(OBJ) (SCM_CELL_WORD_0 (OBJ) & (1L << 16L))
72 #define ACTIVATEJB(x) \
73 (SCM_SET_CELL_WORD_0 ((x), (SCM_CELL_WORD_0 (x) | (1L << 16L))))
74 #define DEACTIVATEJB(x) \
75 (SCM_SET_CELL_WORD_0 ((x), (SCM_CELL_WORD_0 (x) & ~(1L << 16L))))
77 #define JBJMPBUF(OBJ) ((jmp_buf *) SCM_CELL_WORD_1 (OBJ))
78 #define SETJBJMPBUF(x,v) (SCM_SET_CELL_WORD_1 ((x), (v)))
79 #ifdef DEBUG_EXTENSIONS
80 #define SCM_JBDFRAME(x) ((scm_t_debug_frame *) SCM_CELL_WORD_2 (x))
81 #define SCM_SETJBDFRAME(x,v) (SCM_SET_CELL_WORD_2 ((x), (v)))
85 jmpbuffer_print (SCM exp
, SCM port
, scm_print_state
*pstate SCM_UNUSED
)
87 scm_puts ("#<jmpbuffer ", port
);
88 scm_puts (JBACTIVE(exp
) ? "(active) " : "(inactive) ", port
);
89 scm_intprint((long) JBJMPBUF (exp
), 16, port
);
100 #ifdef DEBUG_EXTENSIONS
101 SCM_NEWSMOB2 (answer
, tc16_jmpbuffer
, 0, 0);
103 SCM_NEWSMOB (answer
, tc16_jmpbuffer
, 0);
105 SETJBJMPBUF(answer
, (jmp_buf *)0);
106 DEACTIVATEJB(answer
);
113 /* scm_internal_catch (the guts of catch) */
115 struct jmp_buf_and_retval
/* use only on the stack, in scm_catch */
117 jmp_buf buf
; /* must be first */
123 /* scm_internal_catch is the guts of catch. It handles all the
124 mechanics of setting up a catch target, invoking the catch body,
125 and perhaps invoking the handler if the body does a throw.
127 The function is designed to be usable from C code, but is general
128 enough to implement all the semantics Guile Scheme expects from
131 TAG is the catch tag. Typically, this is a symbol, but this
132 function doesn't actually care about that.
134 BODY is a pointer to a C function which runs the body of the catch;
135 this is the code you can throw from. We call it like this:
138 BODY_DATA is just the BODY_DATA argument we received; we pass it
139 through to BODY as its first argument. The caller can make
140 BODY_DATA point to anything useful that BODY might need.
142 HANDLER is a pointer to a C function to deal with a throw to TAG,
143 should one occur. We call it like this:
144 HANDLER (HANDLER_DATA, THROWN_TAG, THROW_ARGS)
146 HANDLER_DATA is the HANDLER_DATA argument we recevied; it's the
147 same idea as BODY_DATA above.
148 THROWN_TAG is the tag that the user threw to; usually this is
149 TAG, but it could be something else if TAG was #t (i.e., a
150 catch-all), or the user threw to a jmpbuf.
151 THROW_ARGS is the list of arguments the user passed to the THROW
152 function, after the tag.
154 BODY_DATA is just a pointer we pass through to BODY. HANDLER_DATA
155 is just a pointer we pass through to HANDLER. We don't actually
156 use either of those pointers otherwise ourselves. The idea is
157 that, if our caller wants to communicate something to BODY or
158 HANDLER, it can pass a pointer to it as MUMBLE_DATA, which BODY and
159 HANDLER can then use. Think of it as a way to make BODY and
160 HANDLER closures, not just functions; MUMBLE_DATA points to the
163 Of course, it's up to the caller to make sure that any data a
164 MUMBLE_DATA needs is protected from GC. A common way to do this is
165 to make MUMBLE_DATA a pointer to data stored in an automatic
166 structure variable; since the collector must scan the stack for
167 references anyway, this assures that any references in MUMBLE_DATA
171 scm_internal_catch (SCM tag
, scm_t_catch_body body
, void *body_data
, scm_t_catch_handler handler
, void *handler_data
)
173 struct jmp_buf_and_retval jbr
;
177 jmpbuf
= make_jmpbuf ();
179 scm_dynwinds
= scm_acons (tag
, jmpbuf
, scm_dynwinds
);
180 SETJBJMPBUF(jmpbuf
, &jbr
.buf
);
181 #ifdef DEBUG_EXTENSIONS
182 SCM_SETJBDFRAME(jmpbuf
, scm_last_debug_frame
);
184 if (setjmp (jbr
.buf
))
189 #ifdef STACK_CHECKING
190 scm_stack_checking_enabled_p
= SCM_STACK_CHECKING_P
;
193 DEACTIVATEJB (jmpbuf
);
194 scm_dynwinds
= SCM_CDR (scm_dynwinds
);
196 throw_args
= jbr
.retval
;
197 throw_tag
= jbr
.throw_tag
;
198 jbr
.throw_tag
= SCM_EOL
;
199 jbr
.retval
= SCM_EOL
;
200 answer
= handler (handler_data
, throw_tag
, throw_args
);
205 answer
= body (body_data
);
207 DEACTIVATEJB (jmpbuf
);
208 scm_dynwinds
= SCM_CDR (scm_dynwinds
);
216 /* scm_internal_lazy_catch (the guts of lazy catching) */
218 /* The smob tag for lazy_catch smobs. */
219 static scm_t_bits tc16_lazy_catch
;
221 /* This is the structure we put on the wind list for a lazy catch. It
222 stores the handler function to call, and the data pointer to pass
223 through to it. It's not a Scheme closure, but it is a function
224 with data, so the term "closure" is appropriate in its broader
227 (We don't need anything like this in the "eager" catch code,
228 because the same C frame runs both the body and the handler.) */
230 scm_t_catch_handler handler
;
234 /* Strictly speaking, we could just pass a zero for our print
235 function, because we don't need to print them. They should never
236 appear in normal data structures, only in the wind list. However,
237 it might be nice for debugging someday... */
239 lazy_catch_print (SCM closure
, SCM port
, scm_print_state
*pstate SCM_UNUSED
)
241 struct lazy_catch
*c
= (struct lazy_catch
*) SCM_CELL_WORD_1 (closure
);
244 sprintf (buf
, "#<lazy-catch 0x%lx 0x%lx>",
245 (long) c
->handler
, (long) c
->handler_data
);
246 scm_puts (buf
, port
);
252 /* Given a pointer to a lazy catch structure, return a smob for it,
253 suitable for inclusion in the wind list. ("Ah yes, a Château
254 Gollombiere '72, non?"). */
256 make_lazy_catch (struct lazy_catch
*c
)
258 SCM_RETURN_NEWSMOB (tc16_lazy_catch
, c
);
261 #define SCM_LAZY_CATCH_P(obj) (SCM_TYP16_PREDICATE (tc16_lazy_catch, obj))
264 /* Exactly like scm_internal_catch, except:
265 - It does not unwind the stack (this is the major difference).
266 - The handler is not allowed to return. */
268 scm_internal_lazy_catch (SCM tag
, scm_t_catch_body body
, void *body_data
, scm_t_catch_handler handler
, void *handler_data
)
270 SCM lazy_catch
, answer
;
274 c
.handler_data
= handler_data
;
275 lazy_catch
= make_lazy_catch (&c
);
278 scm_dynwinds
= scm_acons (tag
, lazy_catch
, scm_dynwinds
);
281 answer
= (*body
) (body_data
);
284 scm_dynwinds
= SCM_CDR (scm_dynwinds
);
291 /* scm_internal_stack_catch
292 Use this one if you want debugging information to be stored in
293 scm_the_last_stack_fluid_var on error. */
296 ss_handler (void *data SCM_UNUSED
, SCM tag
, SCM throw_args
)
299 scm_fluid_set_x (SCM_VARIABLE_REF (scm_the_last_stack_fluid_var
),
300 scm_make_stack (SCM_BOOL_T
, SCM_EOL
));
301 /* Throw the error */
302 return scm_throw (tag
, throw_args
);
308 scm_t_catch_body body
;
313 cwss_body (void *data
)
315 struct cwss_data
*d
= data
;
316 return scm_internal_lazy_catch (d
->tag
, d
->body
, d
->data
, ss_handler
, NULL
);
320 scm_internal_stack_catch (SCM tag
,
321 scm_t_catch_body body
,
323 scm_t_catch_handler handler
,
330 return scm_internal_catch (tag
, cwss_body
, &d
, handler
, handler_data
);
335 /* body and handler functions for use with any of the above catch variants */
337 /* This is a body function you can pass to scm_internal_catch if you
338 want the body to be like Scheme's `catch' --- a thunk.
340 BODY_DATA is a pointer to a scm_body_thunk_data structure, which
341 contains the Scheme procedure to invoke as the body, and the tag
345 scm_body_thunk (void *body_data
)
347 struct scm_body_thunk_data
*c
= (struct scm_body_thunk_data
*) body_data
;
349 return scm_call_0 (c
->body_proc
);
353 /* This is a handler function you can pass to scm_internal_catch if
354 you want the handler to act like Scheme's catch: (throw TAG ARGS ...)
355 applies a handler procedure to (TAG ARGS ...).
357 If the user does a throw to this catch, this function runs a
358 handler procedure written in Scheme. HANDLER_DATA is a pointer to
359 an SCM variable holding the Scheme procedure object to invoke. It
360 ought to be a pointer to an automatic variable (i.e., one living on
361 the stack), or the procedure object should be otherwise protected
364 scm_handle_by_proc (void *handler_data
, SCM tag
, SCM throw_args
)
366 SCM
*handler_proc_p
= (SCM
*) handler_data
;
368 return scm_apply_1 (*handler_proc_p
, tag
, throw_args
);
371 /* SCM_HANDLE_BY_PROC_CATCHING_ALL is like SCM_HANDLE_BY_PROC but
372 catches all throws that the handler might emit itself. The handler
373 used for these `secondary' throws is SCM_HANDLE_BY_MESSAGE_NO_EXIT. */
381 hbpca_body (void *body_data
)
383 struct hbpca_data
*data
= (struct hbpca_data
*)body_data
;
384 return scm_apply_0 (data
->proc
, data
->args
);
388 scm_handle_by_proc_catching_all (void *handler_data
, SCM tag
, SCM throw_args
)
390 SCM
*handler_proc_p
= (SCM
*) handler_data
;
391 struct hbpca_data data
;
392 data
.proc
= *handler_proc_p
;
393 data
.args
= scm_cons (tag
, throw_args
);
395 return scm_internal_catch (SCM_BOOL_T
,
397 scm_handle_by_message_noexit
, NULL
);
400 /* Derive the an exit status from the arguments to (quit ...). */
402 scm_exit_status (SCM args
)
404 if (SCM_NNULLP (args
))
406 SCM cqa
= SCM_CAR (args
);
409 return (SCM_INUM (cqa
));
410 else if (SCM_FALSEP (cqa
))
418 handler_message (void *handler_data
, SCM tag
, SCM args
)
420 char *prog_name
= (char *) handler_data
;
421 SCM p
= scm_cur_errp
;
423 if (scm_ilength (args
) >= 3)
425 SCM stack
= scm_make_stack (SCM_BOOL_T
, SCM_EOL
);
426 SCM subr
= SCM_CAR (args
);
427 SCM message
= SCM_CADR (args
);
428 SCM parts
= SCM_CADDR (args
);
429 SCM rest
= SCM_CDDDR (args
);
431 if (SCM_BACKTRACE_P
&& SCM_NFALSEP (stack
))
433 scm_puts ("Backtrace:\n", p
);
434 scm_display_backtrace (stack
, p
, SCM_UNDEFINED
, SCM_UNDEFINED
);
437 scm_i_display_error (stack
, p
, subr
, message
, parts
, rest
);
444 scm_puts (prog_name
, p
);
447 scm_puts ("uncaught throw to ", p
);
448 scm_prin1 (tag
, p
, 0);
450 scm_prin1 (args
, p
, 1);
456 /* This is a handler function to use if you want scheme to print a
457 message and die. Useful for dealing with throws to uncaught keys
460 At boot time, we establish a catch-all that uses this as its handler.
461 1) If the user wants something different, they can use (catch #t
462 ...) to do what they like.
463 2) Outside the context of a read-eval-print loop, there isn't
464 anything else good to do; libguile should not assume the existence
465 of a read-eval-print loop.
466 3) Given that we shouldn't do anything complex, it's much more
467 robust to do it in C code.
469 HANDLER_DATA, if non-zero, is assumed to be a char * pointing to a
470 message header to print; if zero, we use "guile" instead. That
471 text is followed by a colon, then the message described by ARGS. */
473 /* Dirk:FIXME:: The name of the function should make clear that the
474 * application gets terminated.
478 scm_handle_by_message (void *handler_data
, SCM tag
, SCM args
)
480 if (SCM_NFALSEP (scm_eq_p (tag
, scm_str2symbol ("quit"))))
482 exit (scm_exit_status (args
));
485 handler_message (handler_data
, tag
, args
);
490 /* This is just like scm_handle_by_message, but it doesn't exit; it
491 just returns #f. It's useful in cases where you don't really know
492 enough about the body to handle things in a better way, but don't
493 want to let throws fall off the bottom of the wind list. */
495 scm_handle_by_message_noexit (void *handler_data
, SCM tag
, SCM args
)
497 handler_message (handler_data
, tag
, args
);
504 scm_handle_by_throw (void *handler_data SCM_UNUSED
, SCM tag
, SCM args
)
506 scm_ithrow (tag
, args
, 1);
507 return SCM_UNSPECIFIED
; /* never returns */
512 /* the Scheme-visible CATCH and LAZY-CATCH functions */
514 SCM_DEFINE (scm_catch
, "catch", 3, 0, 0,
515 (SCM key
, SCM thunk
, SCM handler
),
516 "Invoke @var{thunk} in the dynamic context of @var{handler} for\n"
517 "exceptions matching @var{key}. If thunk throws to the symbol\n"
518 "@var{key}, then @var{handler} is invoked this way:\n"
520 "(handler key args ...)\n"
523 "@var{key} is a symbol or @code{#t}.\n"
525 "@var{thunk} takes no arguments. If @var{thunk} returns\n"
526 "normally, that is the return value of @code{catch}.\n"
528 "Handler is invoked outside the scope of its own @code{catch}.\n"
529 "If @var{handler} again throws to the same key, a new handler\n"
530 "from further up the call chain is invoked.\n"
532 "If the key is @code{#t}, then a throw to @emph{any} symbol will\n"
533 "match this call to @code{catch}.")
534 #define FUNC_NAME s_scm_catch
536 struct scm_body_thunk_data c
;
538 SCM_ASSERT (SCM_SYMBOLP (key
) || SCM_EQ_P (key
, SCM_BOOL_T
),
539 key
, SCM_ARG1
, FUNC_NAME
);
544 /* scm_internal_catch takes care of all the mechanics of setting up
545 a catch key; we tell it to call scm_body_thunk to run the body,
546 and scm_handle_by_proc to deal with any throws to this catch.
547 The former receives a pointer to c, telling it how to behave.
548 The latter receives a pointer to HANDLER, so it knows who to call. */
549 return scm_internal_catch (key
,
551 scm_handle_by_proc
, &handler
);
556 SCM_DEFINE (scm_lazy_catch
, "lazy-catch", 3, 0, 0,
557 (SCM key
, SCM thunk
, SCM handler
),
558 "This behaves exactly like @code{catch}, except that it does\n"
559 "not unwind the stack before invoking @var{handler}.\n"
560 "The @var{handler} procedure is not allowed to return:\n"
561 "it must throw to another catch, or otherwise exit non-locally.")
562 #define FUNC_NAME s_scm_lazy_catch
564 struct scm_body_thunk_data c
;
566 SCM_ASSERT (SCM_SYMBOLP (key
) || SCM_EQ_P (key
, SCM_BOOL_T
),
567 key
, SCM_ARG1
, FUNC_NAME
);
572 /* scm_internal_lazy_catch takes care of all the mechanics of
573 setting up a lazy catch key; we tell it to call scm_body_thunk to
574 run the body, and scm_handle_by_proc to deal with any throws to
575 this catch. The former receives a pointer to c, telling it how
576 to behave. The latter receives a pointer to HANDLER, so it knows
578 return scm_internal_lazy_catch (key
,
580 scm_handle_by_proc
, &handler
);
588 SCM_DEFINE (scm_throw
, "throw", 1, 0, 1,
590 "Invoke the catch form matching @var{key}, passing @var{args} to the\n"
591 "@var{handler}. \n\n"
592 "@var{key} is a symbol. It will match catches of the same symbol or of\n"
594 "If there is no handler at all, Guile prints an error and then exits.")
595 #define FUNC_NAME s_scm_throw
597 SCM_VALIDATE_SYMBOL (1,key
);
598 return scm_ithrow (key
, args
, 1);
603 scm_ithrow (SCM key
, SCM args
, int noreturn SCM_UNUSED
)
605 SCM jmpbuf
= SCM_UNDEFINED
;
608 SCM dynpair
= SCM_UNDEFINED
;
611 /* Search the wind list for an appropriate catch.
612 "Waiter, please bring us the wind list." */
613 for (winds
= scm_dynwinds
; SCM_CONSP (winds
); winds
= SCM_CDR (winds
))
615 dynpair
= SCM_CAR (winds
);
616 if (SCM_CONSP (dynpair
))
618 SCM this_key
= SCM_CAR (dynpair
);
620 if (SCM_EQ_P (this_key
, SCM_BOOL_T
) || SCM_EQ_P (this_key
, key
))
626 /* Dirk:FIXME:: This bugfix should be removed some time. */
627 /* GCC 2.95.2 has a bug in its optimizer that makes it generate
628 incorrect code sometimes. This barrier stops it from being too
630 asm volatile ("" : "=g" (winds
));
633 /* If we didn't find anything, print a message and abort the process
634 right here. If you don't want this, establish a catch-all around
635 any code that might throw up. */
636 if (SCM_NULLP (winds
))
638 scm_handle_by_message (NULL
, key
, args
);
642 /* If the wind list is malformed, bail. */
643 if (!SCM_CONSP (winds
))
646 jmpbuf
= SCM_CDR (dynpair
);
648 for (wind_goal
= scm_dynwinds
;
649 !SCM_EQ_P (SCM_CDAR (wind_goal
), jmpbuf
);
650 wind_goal
= SCM_CDR (wind_goal
))
653 /* Is a lazy catch? In wind list entries for lazy catches, the key
654 is bound to a lazy_catch smob, not a jmpbuf. */
655 if (SCM_LAZY_CATCH_P (jmpbuf
))
657 struct lazy_catch
*c
= (struct lazy_catch
*) SCM_CELL_WORD_1 (jmpbuf
);
659 scm_dowinds (wind_goal
, (scm_ilength (scm_dynwinds
)
660 - scm_ilength (wind_goal
)));
662 handle
= scm_dynwinds
;
663 scm_dynwinds
= SCM_CDR (scm_dynwinds
);
665 answer
= (c
->handler
) (c
->handler_data
, key
, args
);
666 scm_misc_error ("throw", "lazy-catch handler did return.", SCM_EOL
);
669 /* Otherwise, it's a normal catch. */
670 else if (SCM_JMPBUFP (jmpbuf
))
672 struct jmp_buf_and_retval
* jbr
;
673 scm_dowinds (wind_goal
, (scm_ilength (scm_dynwinds
)
674 - scm_ilength (wind_goal
)));
675 jbr
= (struct jmp_buf_and_retval
*)JBJMPBUF (jmpbuf
);
676 jbr
->throw_tag
= key
;
678 #ifdef DEBUG_EXTENSIONS
679 scm_last_debug_frame
= SCM_JBDFRAME (jmpbuf
);
681 longjmp (*JBJMPBUF (jmpbuf
), 1);
684 /* Otherwise, it's some random piece of junk. */
693 tc16_jmpbuffer
= scm_make_smob_type ("jmpbuffer", 0);
694 scm_set_smob_print (tc16_jmpbuffer
, jmpbuffer_print
);
696 tc16_lazy_catch
= scm_make_smob_type ("lazy-catch", 0);
697 scm_set_smob_print (tc16_lazy_catch
, lazy_catch_print
);
699 #ifndef SCM_MAGIC_SNARFER
700 #include "libguile/throw.x"