throw.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. /* Copyright (C) 1995, 1996, 1997, 1998, 2000, 2002 Free Software Foundation, Inc.
  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
  15. * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. * Boston, MA 02111-1307 USA
  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.
  40. * If you do not wish that, delete this exception notice. */
  41. #include <stdio.h>
  42. #include "libguile/_scm.h"
  43. #include "libguile/smob.h"
  44. #include "libguile/alist.h"
  45. #include "libguile/eval.h"
  46. #include "libguile/eq.h"
  47. #include "libguile/dynwind.h"
  48. #include "libguile/backtrace.h"
  49. #ifdef DEBUG_EXTENSIONS
  50. #include "libguile/debug.h"
  51. #endif
  52. #include "libguile/continuations.h"
  53. #include "libguile/stackchk.h"
  54. #include "libguile/stacks.h"
  55. #include "libguile/fluids.h"
  56. #include "libguile/ports.h"
  57. #include "libguile/validate.h"
  58. #include "libguile/throw.h"
  59. /* the jump buffer data structure */
  60. static int scm_tc16_jmpbuffer;
  61. #define SCM_JMPBUFP(OBJ) (SCM_NIMP(OBJ) && (SCM_TYP16(OBJ) == scm_tc16_jmpbuffer))
  62. #define JBACTIVE(OBJ) (SCM_CELL_WORD_0 (OBJ) & (1L << 16L))
  63. #define ACTIVATEJB(OBJ) (SCM_SETOR_CAR (OBJ, (1L << 16L)))
  64. #define DEACTIVATEJB(OBJ) (SCM_SETAND_CAR (OBJ, ~(1L << 16L)))
  65. #define JBJMPBUF(OBJ) ((jmp_buf *) SCM_CELL_WORD_1 (OBJ))
  66. #define SETJBJMPBUF(x,v) (SCM_SET_CELL_WORD_1 ((x), (v)))
  67. #ifdef DEBUG_EXTENSIONS
  68. #define SCM_JBDFRAME(x) ((scm_debug_frame *) SCM_CELL_WORD_2 (x))
  69. #define SCM_SETJBDFRAME(x,v) (SCM_SET_CELL_WORD_2 ((x), (v)))
  70. #endif
  71. static int
  72. printjb (SCM exp, SCM port, scm_print_state *pstate)
  73. {
  74. scm_puts ("#<jmpbuffer ", port);
  75. scm_puts (JBACTIVE(exp) ? "(active) " : "(inactive) ", port);
  76. scm_intprint((long) JBJMPBUF (exp), 16, port);
  77. scm_putc ('>', port);
  78. return 1 ;
  79. }
  80. static SCM
  81. make_jmpbuf (void)
  82. {
  83. SCM answer;
  84. SCM_REDEFER_INTS;
  85. {
  86. #ifdef DEBUG_EXTENSIONS
  87. SCM_NEWSMOB2 (answer, scm_tc16_jmpbuffer, 0, 0);
  88. #else
  89. SCM_NEWSMOB (answer, scm_tc16_jmpbuffer, 0);
  90. #endif
  91. SETJBJMPBUF(answer, (jmp_buf *)0);
  92. DEACTIVATEJB(answer);
  93. }
  94. SCM_REALLOW_INTS;
  95. return answer;
  96. }
  97. /* scm_internal_catch (the guts of catch) */
  98. struct jmp_buf_and_retval /* use only on the stack, in scm_catch */
  99. {
  100. jmp_buf buf; /* must be first */
  101. SCM throw_tag;
  102. SCM retval;
  103. };
  104. /* scm_internal_catch is the guts of catch. It handles all the
  105. mechanics of setting up a catch target, invoking the catch body,
  106. and perhaps invoking the handler if the body does a throw.
  107. The function is designed to be usable from C code, but is general
  108. enough to implement all the semantics Guile Scheme expects from
  109. throw.
  110. TAG is the catch tag. Typically, this is a symbol, but this
  111. function doesn't actually care about that.
  112. BODY is a pointer to a C function which runs the body of the catch;
  113. this is the code you can throw from. We call it like this:
  114. BODY (BODY_DATA)
  115. where:
  116. BODY_DATA is just the BODY_DATA argument we received; we pass it
  117. through to BODY as its first argument. The caller can make
  118. BODY_DATA point to anything useful that BODY might need.
  119. HANDLER is a pointer to a C function to deal with a throw to TAG,
  120. should one occur. We call it like this:
  121. HANDLER (HANDLER_DATA, THROWN_TAG, THROW_ARGS)
  122. where
  123. HANDLER_DATA is the HANDLER_DATA argument we recevied; it's the
  124. same idea as BODY_DATA above.
  125. THROWN_TAG is the tag that the user threw to; usually this is
  126. TAG, but it could be something else if TAG was #t (i.e., a
  127. catch-all), or the user threw to a jmpbuf.
  128. THROW_ARGS is the list of arguments the user passed to the THROW
  129. function, after the tag.
  130. BODY_DATA is just a pointer we pass through to BODY. HANDLER_DATA
  131. is just a pointer we pass through to HANDLER. We don't actually
  132. use either of those pointers otherwise ourselves. The idea is
  133. that, if our caller wants to communicate something to BODY or
  134. HANDLER, it can pass a pointer to it as MUMBLE_DATA, which BODY and
  135. HANDLER can then use. Think of it as a way to make BODY and
  136. HANDLER closures, not just functions; MUMBLE_DATA points to the
  137. enclosed variables.
  138. Of course, it's up to the caller to make sure that any data a
  139. MUMBLE_DATA needs is protected from GC. A common way to do this is
  140. to make MUMBLE_DATA a pointer to data stored in an automatic
  141. structure variable; since the collector must scan the stack for
  142. references anyway, this assures that any references in MUMBLE_DATA
  143. will be found. */
  144. SCM
  145. scm_internal_catch (SCM tag, scm_catch_body_t body, void *body_data, scm_catch_handler_t handler, void *handler_data)
  146. {
  147. struct jmp_buf_and_retval jbr;
  148. SCM jmpbuf;
  149. SCM answer;
  150. jmpbuf = make_jmpbuf ();
  151. answer = SCM_EOL;
  152. scm_dynwinds = scm_acons (tag, jmpbuf, scm_dynwinds);
  153. SETJBJMPBUF(jmpbuf, &jbr.buf);
  154. #ifdef DEBUG_EXTENSIONS
  155. SCM_SETJBDFRAME(jmpbuf, scm_last_debug_frame);
  156. #endif
  157. if (setjmp (jbr.buf))
  158. {
  159. SCM throw_tag;
  160. SCM throw_args;
  161. #ifdef STACK_CHECKING
  162. scm_stack_checking_enabled_p = SCM_STACK_CHECKING_P;
  163. #endif
  164. SCM_REDEFER_INTS;
  165. DEACTIVATEJB (jmpbuf);
  166. scm_dynwinds = SCM_CDR (scm_dynwinds);
  167. SCM_REALLOW_INTS;
  168. throw_args = jbr.retval;
  169. throw_tag = jbr.throw_tag;
  170. jbr.throw_tag = SCM_EOL;
  171. jbr.retval = SCM_EOL;
  172. answer = handler (handler_data, throw_tag, throw_args);
  173. }
  174. else
  175. {
  176. ACTIVATEJB (jmpbuf);
  177. answer = body (body_data);
  178. SCM_REDEFER_INTS;
  179. DEACTIVATEJB (jmpbuf);
  180. scm_dynwinds = SCM_CDR (scm_dynwinds);
  181. SCM_REALLOW_INTS;
  182. }
  183. return answer;
  184. }
  185. /* scm_internal_lazy_catch (the guts of lazy catching) */
  186. /* The smob tag for lazy_catch smobs. */
  187. static long tc16_lazy_catch;
  188. /* This is the structure we put on the wind list for a lazy catch. It
  189. stores the handler function to call, and the data pointer to pass
  190. through to it. It's not a Scheme closure, but it is a function
  191. with data, so the term "closure" is appropriate in its broader
  192. sense.
  193. (We don't need anything like this in the "eager" catch code,
  194. because the same C frame runs both the body and the handler.) */
  195. struct lazy_catch {
  196. scm_catch_handler_t handler;
  197. void *handler_data;
  198. };
  199. /* Strictly speaking, we could just pass a zero for our print
  200. function, because we don't need to print them. They should never
  201. appear in normal data structures, only in the wind list. However,
  202. it might be nice for debugging someday... */
  203. static int
  204. print_lazy_catch (SCM closure, SCM port, scm_print_state *pstate)
  205. {
  206. struct lazy_catch *c = (struct lazy_catch *) SCM_CELL_WORD_1 (closure);
  207. char buf[200];
  208. sprintf (buf, "#<lazy-catch 0x%lx 0x%lx>",
  209. (long) c->handler, (long) c->handler_data);
  210. scm_puts (buf, port);
  211. return 1;
  212. }
  213. /* Given a pointer to a lazy catch structure, return a smob for it,
  214. suitable for inclusion in the wind list. ("Ah yes, a Château
  215. Gollombiere '72, non?"). */
  216. static SCM
  217. make_lazy_catch (struct lazy_catch *c)
  218. {
  219. SCM_RETURN_NEWSMOB (tc16_lazy_catch, c);
  220. }
  221. #define SCM_LAZY_CATCH_P(obj) (SCM_SMOB_PREDICATE (tc16_lazy_catch, obj))
  222. /* Exactly like scm_internal_catch, except:
  223. - It does not unwind the stack (this is the major difference).
  224. - If handler returns, its value is returned from the throw. */
  225. SCM
  226. scm_internal_lazy_catch (SCM tag, scm_catch_body_t body, void *body_data, scm_catch_handler_t handler, void *handler_data)
  227. {
  228. SCM lazy_catch, answer;
  229. struct lazy_catch c;
  230. c.handler = handler;
  231. c.handler_data = handler_data;
  232. lazy_catch = make_lazy_catch (&c);
  233. SCM_REDEFER_INTS;
  234. scm_dynwinds = scm_acons (tag, lazy_catch, scm_dynwinds);
  235. SCM_REALLOW_INTS;
  236. answer = (*body) (body_data);
  237. SCM_REDEFER_INTS;
  238. scm_dynwinds = SCM_CDR (scm_dynwinds);
  239. SCM_REALLOW_INTS;
  240. return answer;
  241. }
  242. /* scm_internal_stack_catch
  243. Use this one if you want debugging information to be stored in
  244. scm_the_last_stack_fluid on error. */
  245. static SCM
  246. ss_handler (void *data, SCM tag, SCM throw_args)
  247. {
  248. /* Save the stack */
  249. scm_fluid_set_x (SCM_CDR (scm_the_last_stack_fluid),
  250. scm_make_stack (SCM_BOOL_T, SCM_EOL));
  251. /* Throw the error */
  252. return scm_throw (tag, throw_args);
  253. }
  254. struct cwss_data
  255. {
  256. SCM tag;
  257. scm_catch_body_t body;
  258. void *data;
  259. };
  260. static SCM
  261. cwss_body (void *data)
  262. {
  263. struct cwss_data *d = data;
  264. return scm_internal_lazy_catch (d->tag, d->body, d->data, ss_handler, NULL);
  265. }
  266. SCM
  267. scm_internal_stack_catch (SCM tag,
  268. scm_catch_body_t body,
  269. void *body_data,
  270. scm_catch_handler_t handler,
  271. void *handler_data)
  272. {
  273. struct cwss_data d;
  274. d.tag = tag;
  275. d.body = body;
  276. d.data = body_data;
  277. return scm_internal_catch (tag, cwss_body, &d, handler, handler_data);
  278. }
  279. /* body and handler functions for use with any of the above catch variants */
  280. /* This is a body function you can pass to scm_internal_catch if you
  281. want the body to be like Scheme's `catch' --- a thunk.
  282. BODY_DATA is a pointer to a scm_body_thunk_data structure, which
  283. contains the Scheme procedure to invoke as the body, and the tag
  284. we're catching. */
  285. SCM
  286. scm_body_thunk (void *body_data)
  287. {
  288. struct scm_body_thunk_data *c = (struct scm_body_thunk_data *) body_data;
  289. return scm_apply (c->body_proc, SCM_EOL, SCM_EOL);
  290. }
  291. /* This is a handler function you can pass to scm_internal_catch if
  292. you want the handler to act like Scheme's catch: (throw TAG ARGS ...)
  293. applies a handler procedure to (TAG ARGS ...).
  294. If the user does a throw to this catch, this function runs a
  295. handler procedure written in Scheme. HANDLER_DATA is a pointer to
  296. an SCM variable holding the Scheme procedure object to invoke. It
  297. ought to be a pointer to an automatic variable (i.e., one living on
  298. the stack), or the procedure object should be otherwise protected
  299. from GC. */
  300. SCM
  301. scm_handle_by_proc (void *handler_data, SCM tag, SCM throw_args)
  302. {
  303. SCM *handler_proc_p = (SCM *) handler_data;
  304. return scm_apply (*handler_proc_p, scm_cons (tag, throw_args), SCM_EOL);
  305. }
  306. /* SCM_HANDLE_BY_PROC_CATCHING_ALL is like SCM_HANDLE_BY_PROC but
  307. catches all throws that the handler might emit itself. The handler
  308. used for these `secondary' throws is SCM_HANDLE_BY_MESSAGE_NO_EXIT. */
  309. struct hbpca_data {
  310. SCM proc;
  311. SCM args;
  312. };
  313. static SCM
  314. hbpca_body (void *body_data)
  315. {
  316. struct hbpca_data *data = (struct hbpca_data *)body_data;
  317. return scm_apply (data->proc, data->args, SCM_EOL);
  318. }
  319. SCM
  320. scm_handle_by_proc_catching_all (void *handler_data, SCM tag, SCM throw_args)
  321. {
  322. SCM *handler_proc_p = (SCM *) handler_data;
  323. struct hbpca_data data;
  324. data.proc = *handler_proc_p;
  325. data.args = scm_cons (tag, throw_args);
  326. return scm_internal_catch (SCM_BOOL_T,
  327. hbpca_body, &data,
  328. scm_handle_by_message_noexit, NULL);
  329. }
  330. /* Derive the an exit status from the arguments to (quit ...). */
  331. int
  332. scm_exit_status (SCM args)
  333. {
  334. if (SCM_NNULLP (args))
  335. {
  336. SCM cqa = SCM_CAR (args);
  337. if (SCM_INUMP (cqa))
  338. return (SCM_INUM (cqa));
  339. else if (SCM_FALSEP (cqa))
  340. return 1;
  341. }
  342. return 0;
  343. }
  344. static void
  345. handler_message (void *handler_data, SCM tag, SCM args)
  346. {
  347. char *prog_name = (char *) handler_data;
  348. SCM p = scm_cur_errp;
  349. if (scm_ilength (args) >= 3)
  350. {
  351. SCM stack = scm_make_stack (SCM_BOOL_T, SCM_EOL);
  352. SCM subr = SCM_CAR (args);
  353. SCM message = SCM_CADR (args);
  354. SCM parts = SCM_CADDR (args);
  355. SCM rest = SCM_CDDDR (args);
  356. if (SCM_BACKTRACE_P && SCM_NFALSEP (stack))
  357. {
  358. scm_puts ("Backtrace:\n", p);
  359. scm_display_backtrace (stack, p, SCM_UNDEFINED, SCM_UNDEFINED);
  360. scm_newline (p);
  361. }
  362. scm_display_error (stack, p, subr, message, parts, rest);
  363. }
  364. else
  365. {
  366. if (! prog_name)
  367. prog_name = "guile";
  368. scm_puts (prog_name, p);
  369. scm_puts (": ", p);
  370. scm_puts ("uncaught throw to ", p);
  371. scm_prin1 (tag, p, 0);
  372. scm_puts (": ", p);
  373. scm_prin1 (args, p, 1);
  374. scm_putc ('\n', p);
  375. }
  376. }
  377. /* This is a handler function to use if you want scheme to print a
  378. message and die. Useful for dealing with throws to uncaught keys
  379. at the top level.
  380. At boot time, we establish a catch-all that uses this as its handler.
  381. 1) If the user wants something different, they can use (catch #t
  382. ...) to do what they like.
  383. 2) Outside the context of a read-eval-print loop, there isn't
  384. anything else good to do; libguile should not assume the existence
  385. of a read-eval-print loop.
  386. 3) Given that we shouldn't do anything complex, it's much more
  387. robust to do it in C code.
  388. HANDLER_DATA, if non-zero, is assumed to be a char * pointing to a
  389. message header to print; if zero, we use "guile" instead. That
  390. text is followed by a colon, then the message described by ARGS. */
  391. SCM
  392. scm_handle_by_message (void *handler_data, SCM tag, SCM args)
  393. {
  394. if (SCM_NFALSEP (scm_eq_p (tag, SCM_CAR (scm_intern0 ("quit")))))
  395. {
  396. exit (scm_exit_status (args));
  397. }
  398. handler_message (handler_data, tag, args);
  399. /* try to flush the error message first before the rest of the
  400. ports: if any throw error, it currently causes a bus
  401. exception. */
  402. exit (2);
  403. }
  404. /* This is just like scm_handle_by_message, but it doesn't exit; it
  405. just returns #f. It's useful in cases where you don't really know
  406. enough about the body to handle things in a better way, but don't
  407. want to let throws fall off the bottom of the wind list. */
  408. SCM
  409. scm_handle_by_message_noexit (void *handler_data, SCM tag, SCM args)
  410. {
  411. handler_message (handler_data, tag, args);
  412. return SCM_BOOL_F;
  413. }
  414. SCM
  415. scm_handle_by_throw (void *handler_data, SCM tag, SCM args)
  416. {
  417. scm_ithrow (tag, args, 1);
  418. return SCM_UNSPECIFIED; /* never returns */
  419. }
  420. /* the Scheme-visible CATCH and LAZY-CATCH functions */
  421. SCM_DEFINE (scm_catch, "catch", 3, 0, 0,
  422. (SCM tag, SCM thunk, SCM handler),
  423. "Invoke @var{thunk} in the dynamic context of @var{handler} for\n"
  424. "exceptions matching @var{key}. If thunk throws to the symbol @var{key},\n"
  425. "then @var{handler} is invoked this way:\n\n"
  426. "@example\n"
  427. "(handler key args ...)\n"
  428. "@end example\n\n"
  429. "@var{key} is a symbol or #t.\n\n"
  430. "@var{thunk} takes no arguments. If @var{thunk} returns normally, that\n"
  431. "is the return value of @code{catch}.\n\n"
  432. "Handler is invoked outside the scope of its own @code{catch}. If\n"
  433. "@var{handler} again throws to the same key, a new handler from further\n"
  434. "up the call chain is invoked.\n\n"
  435. "If the key is @code{#t}, then a throw to @emph{any} symbol will match\n"
  436. "this call to @code{catch}.")
  437. #define FUNC_NAME s_scm_catch
  438. {
  439. struct scm_body_thunk_data c;
  440. SCM_ASSERT (SCM_SYMBOLP (tag) || SCM_EQ_P (tag, SCM_BOOL_T),
  441. tag, SCM_ARG1, FUNC_NAME);
  442. c.tag = tag;
  443. c.body_proc = thunk;
  444. /* scm_internal_catch takes care of all the mechanics of setting up
  445. a catch tag; we tell it to call scm_body_thunk to run the body,
  446. and scm_handle_by_proc to deal with any throws to this catch.
  447. The former receives a pointer to c, telling it how to behave.
  448. The latter receives a pointer to HANDLER, so it knows who to call. */
  449. return scm_internal_catch (tag,
  450. scm_body_thunk, &c,
  451. scm_handle_by_proc, &handler);
  452. }
  453. #undef FUNC_NAME
  454. SCM_DEFINE (scm_lazy_catch, "lazy-catch", 3, 0, 0,
  455. (SCM tag, SCM thunk, SCM handler),
  456. "")
  457. #define FUNC_NAME s_scm_lazy_catch
  458. {
  459. struct scm_body_thunk_data c;
  460. SCM_ASSERT (SCM_SYMBOLP (tag) || SCM_EQ_P (tag, SCM_BOOL_T),
  461. tag, SCM_ARG1, FUNC_NAME);
  462. c.tag = tag;
  463. c.body_proc = thunk;
  464. /* scm_internal_lazy_catch takes care of all the mechanics of
  465. setting up a lazy catch tag; we tell it to call scm_body_thunk to
  466. run the body, and scm_handle_by_proc to deal with any throws to
  467. this catch. The former receives a pointer to c, telling it how
  468. to behave. The latter receives a pointer to HANDLER, so it knows
  469. who to call. */
  470. return scm_internal_lazy_catch (tag,
  471. scm_body_thunk, &c,
  472. scm_handle_by_proc, &handler);
  473. }
  474. #undef FUNC_NAME
  475. /* throwing */
  476. SCM_DEFINE (scm_throw, "throw", 1, 0, 1,
  477. (SCM key, SCM args),
  478. "Invoke the catch form matching @var{key}, passing @var{args} to the\n"
  479. "@var{handler}. \n\n"
  480. "@var{key} is a symbol. It will match catches of the same symbol or of\n"
  481. "#t.\n\n"
  482. "If there is no handler at all, an error is signaled.")
  483. #define FUNC_NAME s_scm_throw
  484. {
  485. SCM_VALIDATE_SYMBOL (1,key);
  486. /* May return if handled by lazy catch. */
  487. return scm_ithrow (key, args, 1);
  488. }
  489. #undef FUNC_NAME
  490. SCM
  491. scm_ithrow (SCM key, SCM args, int noreturn)
  492. {
  493. SCM jmpbuf = SCM_UNDEFINED;
  494. SCM wind_goal;
  495. SCM dynpair = SCM_UNDEFINED;
  496. SCM winds;
  497. /* Search the wind list for an appropriate catch.
  498. "Waiter, please bring us the wind list." */
  499. for (winds = scm_dynwinds; SCM_NIMP (winds); winds = SCM_CDR (winds))
  500. {
  501. if (! SCM_CONSP (winds))
  502. abort ();
  503. dynpair = SCM_CAR (winds);
  504. if (SCM_CONSP (dynpair))
  505. {
  506. SCM this_key = SCM_CAR (dynpair);
  507. if (SCM_EQ_P (this_key, SCM_BOOL_T) || SCM_EQ_P (this_key, key))
  508. break;
  509. }
  510. }
  511. /* If we didn't find anything, abort. scm_boot_guile should
  512. have established a catch-all, but obviously things are
  513. thoroughly screwed up. */
  514. if (SCM_NULLP (winds))
  515. abort ();
  516. /* If the wind list is malformed, bail. */
  517. if (SCM_IMP (winds) || SCM_NCONSP (winds))
  518. abort ();
  519. if (!SCM_FALSEP (dynpair))
  520. jmpbuf = SCM_CDR (dynpair);
  521. else
  522. {
  523. if (!noreturn)
  524. return SCM_UNSPECIFIED;
  525. else
  526. {
  527. scm_exitval = scm_cons (key, args);
  528. scm_dowinds (SCM_EOL, scm_ilength (scm_dynwinds));
  529. #ifdef DEBUG_EXTENSIONS
  530. scm_last_debug_frame = SCM_DFRAME (scm_rootcont);
  531. #endif
  532. longjmp (SCM_JMPBUF (scm_rootcont), 1);
  533. }
  534. }
  535. for (wind_goal = scm_dynwinds;
  536. !SCM_EQ_P (SCM_CDAR (wind_goal), jmpbuf);
  537. wind_goal = SCM_CDR (wind_goal))
  538. ;
  539. /* Is a lazy catch? In wind list entries for lazy catches, the key
  540. is bound to a lazy_catch smob, not a jmpbuf. */
  541. if (SCM_LAZY_CATCH_P (jmpbuf))
  542. {
  543. struct lazy_catch *c = (struct lazy_catch *) SCM_CELL_WORD_1 (jmpbuf);
  544. SCM oldwinds = scm_dynwinds;
  545. SCM handle, answer;
  546. scm_dowinds (wind_goal, (scm_ilength (scm_dynwinds)
  547. - scm_ilength (wind_goal)));
  548. SCM_REDEFER_INTS;
  549. handle = scm_dynwinds;
  550. scm_dynwinds = SCM_CDR (scm_dynwinds);
  551. SCM_REALLOW_INTS;
  552. answer = (c->handler) (c->handler_data, key, args);
  553. SCM_REDEFER_INTS;
  554. SCM_SETCDR (handle, scm_dynwinds);
  555. scm_dynwinds = handle;
  556. SCM_REALLOW_INTS;
  557. scm_dowinds (oldwinds, (scm_ilength (scm_dynwinds)
  558. - scm_ilength (oldwinds)));
  559. return answer;
  560. }
  561. /* Otherwise, it's a normal catch. */
  562. else if (SCM_JMPBUFP (jmpbuf))
  563. {
  564. struct jmp_buf_and_retval * jbr;
  565. scm_dowinds (wind_goal, (scm_ilength (scm_dynwinds)
  566. - scm_ilength (wind_goal)));
  567. jbr = (struct jmp_buf_and_retval *)JBJMPBUF (jmpbuf);
  568. jbr->throw_tag = key;
  569. jbr->retval = args;
  570. }
  571. /* Otherwise, it's some random piece of junk. */
  572. else
  573. abort ();
  574. #ifdef DEBUG_EXTENSIONS
  575. scm_last_debug_frame = SCM_JBDFRAME (jmpbuf);
  576. #endif
  577. longjmp (*JBJMPBUF (jmpbuf), 1);
  578. }
  579. void
  580. scm_init_throw ()
  581. {
  582. scm_tc16_jmpbuffer = scm_make_smob_type_mfpe ("jmpbuffer",
  583. 0,
  584. NULL, /* mark */
  585. NULL,
  586. printjb,
  587. NULL);
  588. tc16_lazy_catch = scm_make_smob_type_mfpe ("lazy-catch", 0,
  589. NULL,
  590. NULL,
  591. print_lazy_catch,
  592. NULL);
  593. #include "libguile/throw.x"
  594. }
  595. /*
  596. Local Variables:
  597. c-file-style: "gnu"
  598. End:
  599. */