+scm_body_thunk (body_data, jmpbuf)
+ void *body_data;
+ SCM jmpbuf;
+{
+ struct scm_body_thunk_data *c = (struct scm_body_thunk_data *) body_data;
+
+ if (c->tag == SCM_BOOL_F)
+ return scm_apply (c->body_proc, scm_cons (jmpbuf, SCM_EOL), SCM_EOL);
+ else
+ return scm_apply (c->body_proc, SCM_EOL, SCM_EOL);
+}
+
+
+/* If the user does a throw to this catch, this function runs a
+ handler procedure written in Scheme. HANDLER_DATA is a pointer to
+ an SCM variable holding the Scheme procedure object to invoke. It
+ ought to be a pointer to an automatic, or the procedure object
+ should be otherwise protected from GC. */
+SCM
+scm_handle_by_proc (handler_data, tag, throw_args)
+ void *handler_data;
+ SCM tag;
+ SCM throw_args;
+{
+ SCM *handler_proc_p = (SCM *) handler_data;
+
+ return scm_apply (*handler_proc_p, scm_cons (tag, throw_args), SCM_EOL);
+}
+
+
+SCM_PROC(s_catch, "catch", 3, 0, 0, scm_catch);
+SCM
+scm_catch (tag, thunk, handler)
+ SCM tag;
+ SCM thunk;
+ SCM handler;
+{
+ struct scm_body_thunk_data c;
+
+ SCM_ASSERT ((tag == SCM_BOOL_F)
+ || (SCM_NIMP(tag) && SCM_SYMBOLP(tag))
+ || (tag == SCM_BOOL_T),
+ tag, SCM_ARG1, s_catch);
+
+ c.tag = tag;
+ c.body_proc = thunk;
+
+ /* scm_internal_catch takes care of all the mechanics of setting up
+ a catch tag; we tell it to call scm_body_thunk to run the body,
+ and scm_handle_by_proc to deal with any throws to this catch.
+ The former receives a pointer to c, telling it how to behave.
+ The latter receives a pointer to HANDLER, so it knows who to call. */
+ return scm_internal_catch (tag,
+ scm_body_thunk, &c,
+ scm_handle_by_proc, &handler);
+}
+
+
+/* The smob tag for lazy_catch smobs. */
+static long tc16_lazy_catch;
+
+/* This is the structure we put on the wind list for a lazy catch. It
+ stores the handler function to call, and the data pointer to pass
+ through to it. It's not a Scheme closure, but it is a function
+ with data, so the term "closure" is appropriate in its broader
+ sense.
+
+ (We don't need anything like this in the "eager" catch code,
+ because the same C frame runs both the body and the handler.) */
+struct lazy_catch {
+ scm_catch_handler_t handler;
+ void *handler_data;
+};
+
+/* Strictly speaking, we could just pass a zero for our print
+ function, because we don't need to print them. They should never
+ appear in normal data structures, only in the wind list. However,
+ it might be nice for debugging someday... */
+static int
+print_lazy_catch (SCM closure, SCM port, scm_print_state *pstate)
+{
+ struct lazy_catch *c = (struct lazy_catch *) SCM_CDR (closure);
+ char buf[200];
+
+ sprintf (buf, "#<lazy-catch 0x%lx 0x%lx>",
+ (long) c->handler, (long) c->handler_data);
+ scm_gen_puts (scm_regular_string, buf, port);
+
+ return 1;
+}
+
+static scm_smobfuns lazy_catch_funs = {
+ scm_mark0, scm_free0, print_lazy_catch, 0
+};
+
+
+/* Given a pointer to a lazy catch structure, return a smob for it,
+ suitable for inclusion in the wind list. ("Ah yes, a Château
+ Gollombiere '72, no?"). */
+static SCM
+make_lazy_catch (struct lazy_catch *c)
+{
+ SCM smob;
+
+ SCM_NEWCELL (smob);
+ SCM_SETCDR (smob, c);
+ SCM_SETCAR (smob, tc16_lazy_catch);
+
+ return smob;
+}
+
+#define SCM_LAZY_CATCH_P(obj) \
+ (SCM_NIMP (obj) && (SCM_CAR (obj) == tc16_lazy_catch))
+
+
+/* Exactly like scm_internal_catch, except:
+ - It does not unwind the stack (this is the major difference).
+ - If handler returns, its value is returned from the throw.
+ - BODY always receives #f as its JMPBUF argument (since there's no
+ jmpbuf associated with a lazy catch, because we don't unwind the
+ stack.) */
+SCM
+scm_internal_lazy_catch (tag, body, body_data, handler, handler_data)
+ SCM tag;
+ scm_catch_body_t body;
+ void *body_data;
+ scm_catch_handler_t handler;
+ void *handler_data;
+{
+ SCM lazy_catch, answer;
+ struct lazy_catch c;
+
+ c.handler = handler;
+ c.handler_data = handler_data;
+ lazy_catch = make_lazy_catch (&c);
+
+ SCM_REDEFER_INTS;
+ scm_dynwinds = scm_acons (tag, lazy_catch, scm_dynwinds);
+ SCM_REALLOW_INTS;
+
+ answer = (*body) (body_data, SCM_BOOL_F);
+
+ SCM_REDEFER_INTS;
+ scm_dynwinds = SCM_CDR (scm_dynwinds);
+ SCM_REALLOW_INTS;
+
+ return answer;
+}
+
+
+SCM_PROC(s_lazy_catch, "lazy-catch", 3, 0, 0, scm_lazy_catch);
+SCM
+scm_lazy_catch (tag, thunk, handler)
+ SCM tag;
+ SCM thunk;
+ SCM handler;
+{
+ struct scm_body_thunk_data c;
+
+ SCM_ASSERT ((SCM_NIMP(tag) && SCM_SYMBOLP(tag))
+ || (tag == SCM_BOOL_T),
+ tag, SCM_ARG1, s_lazy_catch);
+
+ c.tag = tag;
+ c.body_proc = thunk;
+
+ /* scm_internal_lazy_catch takes care of all the mechanics of
+ setting up a lazy catch tag; we tell it to call scm_body_thunk to
+ run the body, and scm_handle_by_proc to deal with any throws to
+ this catch. The former receives a pointer to c, telling it how
+ to behave. The latter receives a pointer to HANDLER, so it knows
+ who to call. */
+ return scm_internal_lazy_catch (tag,
+ scm_body_thunk, &c,
+ scm_handle_by_proc, &handler);
+}
+
+
+/* The user has thrown to an uncaught key --- print a message and die.
+ At boot time, we establish a catch-all that uses this as its handler.
+ 1) If the user wants something different, they can use (catch #t
+ ...) to do what they like.
+ 2) Outside the context of a read-eval-print loop, there isn't
+ anything else good to do; libguile should not assume the existence
+ of a read-eval-print loop.
+ 3) Given that we shouldn't do anything complex, it's much more
+ robust to do it in C code.
+
+ HANDLER_DATA, if non-zero, is assumed to be a char * pointing to a
+ message header to print; if zero, we use "guile" instead. That
+ text is followed by a colon, then the message described by ARGS. */
+
+SCM
+scm_handle_by_message (handler_data, tag, args)
+ void *handler_data;
+ SCM tag;
+ SCM args;
+{
+ char *prog_name = (char *) handler_data;
+ SCM p = scm_def_errp;
+
+ if (! prog_name)
+ prog_name = "guile";
+
+ scm_gen_puts (scm_regular_string, prog_name, p);
+ scm_gen_puts (scm_regular_string, ": ", p);
+
+ if (scm_ilength (args) >= 3)
+ {
+ SCM message = SCM_CADR (args);
+ SCM parts = SCM_CADDR (args);
+
+ scm_display_error_message (message, parts, p);
+ }
+ else
+ {
+ scm_gen_puts (scm_regular_string, "uncaught throw to ", p);
+ scm_prin1 (tag, p, 0);
+ scm_gen_puts (scm_regular_string, ": ", p);
+ scm_prin1 (args, p, 1);
+ scm_gen_putc ('\n', p);
+ }
+
+ exit (2);
+}
+
+
+SCM_PROC(s_throw, "throw", 1, 0, 1, scm_throw);
+SCM
+scm_throw (key, args)
+ SCM key;
+ SCM args;
+{
+ /* May return if handled by lazy catch. */
+ return scm_ithrow (key, args, 1);
+}
+
+