Doc fix.
[bpt/guile.git] / libguile / throw.c
1 /* Copyright (C) 1995,1996 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, 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 * As a special exception, the Free Software Foundation gives permission
18 * for additional uses of the text contained in its release of GUILE.
19 *
20 * The exception is that, if you link the GUILE library with other files
21 * to produce an executable, this does not by itself cause the
22 * resulting executable to be covered by the GNU General Public License.
23 * Your use of that executable is in no way restricted on account of
24 * linking the GUILE library code into it.
25 *
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
28 *
29 * This exception applies only to the code released by the
30 * Free Software Foundation under the name GUILE. If you copy
31 * code from other Free Software Foundation releases into a copy of
32 * GUILE, as the General Public License permits, the exception does
33 * not apply to the code that you add in this way. To avoid misleading
34 * anyone as to the status of such modified files, you must delete
35 * this exception notice from them.
36 *
37 * If you write modifications of your own for GUILE, it is your choice
38 * whether to permit this exception to apply to your modifications.
39 * If you do not wish that, delete this exception notice.
40 */
41 \f
42
43 #include <stdio.h>
44 #include "_scm.h"
45 #include "genio.h"
46 #include "smob.h"
47 #include "alist.h"
48 #include "eval.h"
49 #include "eq.h"
50 #include "dynwind.h"
51 #include "backtrace.h"
52 #ifdef DEBUG_EXTENSIONS
53 #include "debug.h"
54 #endif
55 #include "continuations.h"
56 #include "stackchk.h"
57
58 #include "throw.h"
59
60 \f
61 /* the jump buffer data structure */
62 static int scm_tc16_jmpbuffer;
63
64 #define SCM_JMPBUFP(O) (SCM_TYP16(O) == scm_tc16_jmpbuffer)
65 #define JBACTIVE(O) (SCM_CAR (O) & (1L << 16L))
66 #define ACTIVATEJB(O) (SCM_SETOR_CAR (O, (1L << 16L)))
67 #define DEACTIVATEJB(O) (SCM_SETAND_CAR (O, ~(1L << 16L)))
68
69 #ifndef DEBUG_EXTENSIONS
70 #define JBJMPBUF(O) ((jmp_buf*)SCM_CDR (O) )
71 #define SETJBJMPBUF SCM_SETCDR
72 #else
73 #define SCM_JBDFRAME(O) ((scm_debug_frame*)SCM_CAR (SCM_CDR (O)) )
74 #define JBJMPBUF(O) ((jmp_buf*)SCM_CDR (SCM_CDR (O)) )
75 #define SCM_SETJBDFRAME(O,X) SCM_SETCAR (SCM_CDR (O), (SCM)(X))
76 #define SETJBJMPBUF(O,X) SCM_SETCDR(SCM_CDR (O), X)
77
78 static scm_sizet freejb SCM_P ((SCM jbsmob));
79
80 static scm_sizet
81 freejb (jbsmob)
82 SCM jbsmob;
83 {
84 scm_must_free ((char *) SCM_CDR (jbsmob));
85 return sizeof (scm_cell);
86 }
87 #endif
88
89 static int printjb SCM_P ((SCM exp, SCM port, scm_print_state *pstate));
90 static int
91 printjb (exp, port, pstate)
92 SCM exp;
93 SCM port;
94 scm_print_state *pstate;
95 {
96 scm_gen_puts (scm_regular_string, "#<jmpbuffer ", port);
97 scm_gen_puts (scm_regular_string, JBACTIVE(exp) ? "(active) " : "(inactive) ", port);
98 scm_intprint((SCM) JBJMPBUF(exp), 16, port);
99 scm_gen_putc ('>', port);
100 return 1 ;
101 }
102
103 static scm_smobfuns jbsmob = {
104 scm_mark0,
105 #ifdef DEBUG_EXTENSIONS
106 freejb,
107 #else
108 scm_free0,
109 #endif
110 printjb,
111 0
112 };
113
114 static SCM make_jmpbuf SCM_P ((void));
115 static SCM
116 make_jmpbuf ()
117 {
118 SCM answer;
119 SCM_NEWCELL (answer);
120 SCM_REDEFER_INTS;
121 {
122 #ifdef DEBUG_EXTENSIONS
123 char *mem = scm_must_malloc (sizeof (scm_cell), "jb");
124 SCM_SETCDR (answer, (SCM) mem);
125 #endif
126 SCM_SETCAR (answer, scm_tc16_jmpbuffer);
127 SETJBJMPBUF(answer, (jmp_buf *)0);
128 DEACTIVATEJB(answer);
129 }
130 SCM_REALLOW_INTS;
131 return answer;
132 }
133
134 \f
135 /* scm_internal_catch (the guts of catch) */
136
137 struct jmp_buf_and_retval /* use only on the stack, in scm_catch */
138 {
139 jmp_buf buf; /* must be first */
140 SCM throw_tag;
141 SCM retval;
142 };
143
144
145 /* scm_internal_catch is the guts of catch. It handles all the
146 mechanics of setting up a catch target, invoking the catch body,
147 and perhaps invoking the handler if the body does a throw.
148
149 The function is designed to be usable from C code, but is general
150 enough to implement all the semantics Guile Scheme expects from
151 throw.
152
153 TAG is the catch tag. Typically, this is a symbol, but this
154 function doesn't actually care about that.
155
156 BODY is a pointer to a C function which runs the body of the catch;
157 this is the code you can throw from. We call it like this:
158 BODY (BODY_DATA, JMPBUF)
159 where:
160 BODY_DATA is just the BODY_DATA argument we received; we pass it
161 through to BODY as its first argument. The caller can make
162 BODY_DATA point to anything useful that BODY might need.
163 JMPBUF is the Scheme jmpbuf object corresponding to this catch,
164 which we have just created and initialized.
165
166 HANDLER is a pointer to a C function to deal with a throw to TAG,
167 should one occur. We call it like this:
168 HANDLER (HANDLER_DATA, THROWN_TAG, THROW_ARGS)
169 where
170 HANDLER_DATA is the HANDLER_DATA argument we recevied; it's the
171 same idea as BODY_DATA above.
172 THROWN_TAG is the tag that the user threw to; usually this is
173 TAG, but it could be something else if TAG was #t (i.e., a
174 catch-all), or the user threw to a jmpbuf.
175 THROW_ARGS is the list of arguments the user passed to the THROW
176 function.
177
178 BODY_DATA is just a pointer we pass through to BODY. HANDLER_DATA
179 is just a pointer we pass through to HANDLER. We don't actually
180 use either of those pointers otherwise ourselves. The idea is
181 that, if our caller wants to communicate something to BODY or
182 HANDLER, it can pass a pointer to it as MUMBLE_DATA, which BODY and
183 HANDLER can then use. Think of it as a way to make BODY and
184 HANDLER closures, not just functions; MUMBLE_DATA points to the
185 enclosed variables.
186
187 Of course, it's up to the caller to make sure that any data a
188 MUMBLE_DATA needs is protected from GC. A common way to do this is
189 to make MUMBLE_DATA a pointer to data stored in an automatic
190 structure variable; since the collector must scan the stack for
191 references anyway, this assures that any references in MUMBLE_DATA
192 will be found. */
193
194 SCM
195 scm_internal_catch (tag, body, body_data, handler, handler_data)
196 SCM tag;
197 scm_catch_body_t body;
198 void *body_data;
199 scm_catch_handler_t handler;
200 void *handler_data;
201 {
202 struct jmp_buf_and_retval jbr;
203 SCM jmpbuf;
204 SCM answer;
205
206 jmpbuf = make_jmpbuf ();
207 answer = SCM_EOL;
208 scm_dynwinds = scm_acons (tag, jmpbuf, scm_dynwinds);
209 SETJBJMPBUF(jmpbuf, &jbr.buf);
210 #ifdef DEBUG_EXTENSIONS
211 SCM_SETJBDFRAME(jmpbuf, scm_last_debug_frame);
212 #endif
213 if (setjmp (jbr.buf))
214 {
215 SCM throw_tag;
216 SCM throw_args;
217
218 #ifdef STACK_CHECKING
219 scm_stack_checking_enabled_p = SCM_STACK_CHECKING_P;
220 #endif
221 SCM_REDEFER_INTS;
222 DEACTIVATEJB (jmpbuf);
223 scm_dynwinds = SCM_CDR (scm_dynwinds);
224 SCM_REALLOW_INTS;
225 throw_args = jbr.retval;
226 throw_tag = jbr.throw_tag;
227 jbr.throw_tag = SCM_EOL;
228 jbr.retval = SCM_EOL;
229 answer = handler (handler_data, throw_tag, throw_args);
230 }
231 else
232 {
233 ACTIVATEJB (jmpbuf);
234 answer = body (body_data, jmpbuf);
235 SCM_REDEFER_INTS;
236 DEACTIVATEJB (jmpbuf);
237 scm_dynwinds = SCM_CDR (scm_dynwinds);
238 SCM_REALLOW_INTS;
239 }
240 return answer;
241 }
242
243
244 \f
245 /* scm_internal_lazy_catch (the guts of lazy catching) */
246
247 /* The smob tag for lazy_catch smobs. */
248 static long tc16_lazy_catch;
249
250 /* This is the structure we put on the wind list for a lazy catch. It
251 stores the handler function to call, and the data pointer to pass
252 through to it. It's not a Scheme closure, but it is a function
253 with data, so the term "closure" is appropriate in its broader
254 sense.
255
256 (We don't need anything like this in the "eager" catch code,
257 because the same C frame runs both the body and the handler.) */
258 struct lazy_catch {
259 scm_catch_handler_t handler;
260 void *handler_data;
261 };
262
263 /* Strictly speaking, we could just pass a zero for our print
264 function, because we don't need to print them. They should never
265 appear in normal data structures, only in the wind list. However,
266 it might be nice for debugging someday... */
267 static int
268 print_lazy_catch (SCM closure, SCM port, scm_print_state *pstate)
269 {
270 struct lazy_catch *c = (struct lazy_catch *) SCM_CDR (closure);
271 char buf[200];
272
273 sprintf (buf, "#<lazy-catch 0x%lx 0x%lx>",
274 (long) c->handler, (long) c->handler_data);
275 scm_gen_puts (scm_regular_string, buf, port);
276
277 return 1;
278 }
279
280 static scm_smobfuns lazy_catch_funs = {
281 scm_mark0, scm_free0, print_lazy_catch, 0
282 };
283
284
285 /* Given a pointer to a lazy catch structure, return a smob for it,
286 suitable for inclusion in the wind list. ("Ah yes, a Château
287 Gollombiere '72, non?"). */
288 static SCM
289 make_lazy_catch (struct lazy_catch *c)
290 {
291 SCM smob;
292
293 SCM_NEWCELL (smob);
294 SCM_SETCDR (smob, c);
295 SCM_SETCAR (smob, tc16_lazy_catch);
296
297 return smob;
298 }
299
300 #define SCM_LAZY_CATCH_P(obj) \
301 (SCM_NIMP (obj) && (SCM_CAR (obj) == tc16_lazy_catch))
302
303
304 /* Exactly like scm_internal_catch, except:
305 - It does not unwind the stack (this is the major difference).
306 - If handler returns, its value is returned from the throw.
307 - BODY always receives #f as its JMPBUF argument (since there's no
308 jmpbuf associated with a lazy catch, because we don't unwind the
309 stack.) */
310 SCM
311 scm_internal_lazy_catch (tag, body, body_data, handler, handler_data)
312 SCM tag;
313 scm_catch_body_t body;
314 void *body_data;
315 scm_catch_handler_t handler;
316 void *handler_data;
317 {
318 SCM lazy_catch, answer;
319 struct lazy_catch c;
320
321 c.handler = handler;
322 c.handler_data = handler_data;
323 lazy_catch = make_lazy_catch (&c);
324
325 SCM_REDEFER_INTS;
326 scm_dynwinds = scm_acons (tag, lazy_catch, scm_dynwinds);
327 SCM_REALLOW_INTS;
328
329 answer = (*body) (body_data, SCM_BOOL_F);
330
331 SCM_REDEFER_INTS;
332 scm_dynwinds = SCM_CDR (scm_dynwinds);
333 SCM_REALLOW_INTS;
334
335 return answer;
336 }
337
338
339 \f
340 /* body and handler functions for use with either of the above */
341
342 /* This is a body function you can pass to scm_internal_catch if you
343 want the body to be like Scheme's `catch' --- a thunk, or a
344 function of one argument if the tag is #f.
345
346 BODY_DATA is a pointer to a scm_body_thunk_data structure, which
347 contains the Scheme procedure to invoke as the body, and the tag
348 we're catching. If the tag is #f, then we pass JMPBUF (created by
349 scm_internal_catch) to the body procedure; otherwise, the body gets
350 no arguments. */
351
352 SCM
353 scm_body_thunk (body_data, jmpbuf)
354 void *body_data;
355 SCM jmpbuf;
356 {
357 struct scm_body_thunk_data *c = (struct scm_body_thunk_data *) body_data;
358
359 if (c->tag == SCM_BOOL_F)
360 return scm_apply (c->body_proc, scm_cons (jmpbuf, SCM_EOL), SCM_EOL);
361 else
362 return scm_apply (c->body_proc, SCM_EOL, SCM_EOL);
363 }
364
365
366 /* This is a handler function you can pass to scm_internal_catch if
367 you want the handler to act like Scheme's catch --- call a
368 procedure with the tag and the throw arguments.
369
370 If the user does a throw to this catch, this function runs a
371 handler procedure written in Scheme. HANDLER_DATA is a pointer to
372 an SCM variable holding the Scheme procedure object to invoke. It
373 ought to be a pointer to an automatic variable (i.e., one living on
374 the stack), or the procedure object should be otherwise protected
375 from GC. */
376 SCM
377 scm_handle_by_proc (handler_data, tag, throw_args)
378 void *handler_data;
379 SCM tag;
380 SCM throw_args;
381 {
382 SCM *handler_proc_p = (SCM *) handler_data;
383
384 return scm_apply (*handler_proc_p, scm_cons (tag, throw_args), SCM_EOL);
385 }
386
387
388 /* This is a handler function to use if you want scheme to print a
389 message and die. Useful for dealing with throws to uncaught keys
390 at the top level.
391
392 At boot time, we establish a catch-all that uses this as its handler.
393 1) If the user wants something different, they can use (catch #t
394 ...) to do what they like.
395 2) Outside the context of a read-eval-print loop, there isn't
396 anything else good to do; libguile should not assume the existence
397 of a read-eval-print loop.
398 3) Given that we shouldn't do anything complex, it's much more
399 robust to do it in C code.
400
401 HANDLER_DATA, if non-zero, is assumed to be a char * pointing to a
402 message header to print; if zero, we use "guile" instead. That
403 text is followed by a colon, then the message described by ARGS. */
404
405 SCM
406 scm_handle_by_message (handler_data, tag, args)
407 void *handler_data;
408 SCM tag;
409 SCM args;
410 {
411 char *prog_name = (char *) handler_data;
412 SCM p = scm_def_errp;
413
414 if (SCM_NFALSEP (scm_eq_p (tag, SCM_CAR (scm_intern0 ("quit")))))
415 exit (scm_exit_status (args));
416
417 if (! prog_name)
418 prog_name = "guile";
419
420 scm_gen_puts (scm_regular_string, prog_name, p);
421 scm_gen_puts (scm_regular_string, ": ", p);
422
423 if (scm_ilength (args) >= 3)
424 {
425 SCM message = SCM_CADR (args);
426 SCM parts = SCM_CADDR (args);
427
428 scm_display_error_message (message, parts, p);
429 }
430 else
431 {
432 scm_gen_puts (scm_regular_string, "uncaught throw to ", p);
433 scm_prin1 (tag, p, 0);
434 scm_gen_puts (scm_regular_string, ": ", p);
435 scm_prin1 (args, p, 1);
436 scm_gen_putc ('\n', p);
437 }
438
439 exit (2);
440 }
441
442
443 /* Derive the an exit status from the arguments to (quit ...). */
444 int
445 scm_exit_status (args)
446 SCM args;
447 {
448 if (SCM_NNULLP (args))
449 {
450 SCM cqa = SCM_CAR (args);
451
452 if (SCM_INUMP (cqa))
453 return (SCM_INUM (cqa));
454 else if (SCM_FALSEP (cqa))
455 return 1;
456 }
457 return 0;
458 }
459
460
461
462 \f
463 /* the Scheme-visible CATCH and LAZY-CATCH functions */
464
465 SCM_PROC(s_catch, "catch", 3, 0, 0, scm_catch);
466 SCM
467 scm_catch (tag, thunk, handler)
468 SCM tag;
469 SCM thunk;
470 SCM handler;
471 {
472 struct scm_body_thunk_data c;
473
474 SCM_ASSERT ((tag == SCM_BOOL_F)
475 || (SCM_NIMP(tag) && SCM_SYMBOLP(tag))
476 || (tag == SCM_BOOL_T),
477 tag, SCM_ARG1, s_catch);
478
479 c.tag = tag;
480 c.body_proc = thunk;
481
482 /* scm_internal_catch takes care of all the mechanics of setting up
483 a catch tag; we tell it to call scm_body_thunk to run the body,
484 and scm_handle_by_proc to deal with any throws to this catch.
485 The former receives a pointer to c, telling it how to behave.
486 The latter receives a pointer to HANDLER, so it knows who to call. */
487 return scm_internal_catch (tag,
488 scm_body_thunk, &c,
489 scm_handle_by_proc, &handler);
490 }
491
492
493 SCM_PROC(s_lazy_catch, "lazy-catch", 3, 0, 0, scm_lazy_catch);
494 SCM
495 scm_lazy_catch (tag, thunk, handler)
496 SCM tag;
497 SCM thunk;
498 SCM handler;
499 {
500 struct scm_body_thunk_data c;
501
502 SCM_ASSERT ((SCM_NIMP(tag) && SCM_SYMBOLP(tag))
503 || (tag == SCM_BOOL_T),
504 tag, SCM_ARG1, s_lazy_catch);
505
506 c.tag = tag;
507 c.body_proc = thunk;
508
509 /* scm_internal_lazy_catch takes care of all the mechanics of
510 setting up a lazy catch tag; we tell it to call scm_body_thunk to
511 run the body, and scm_handle_by_proc to deal with any throws to
512 this catch. The former receives a pointer to c, telling it how
513 to behave. The latter receives a pointer to HANDLER, so it knows
514 who to call. */
515 return scm_internal_lazy_catch (tag,
516 scm_body_thunk, &c,
517 scm_handle_by_proc, &handler);
518 }
519
520
521 \f
522 /* throwing */
523
524 SCM_PROC(s_throw, "throw", 1, 0, 1, scm_throw);
525 SCM
526 scm_throw (key, args)
527 SCM key;
528 SCM args;
529 {
530 /* May return if handled by lazy catch. */
531 return scm_ithrow (key, args, 1);
532 }
533
534
535 SCM
536 scm_ithrow (key, args, noreturn)
537 SCM key;
538 SCM args;
539 int noreturn;
540 {
541 SCM jmpbuf;
542 SCM wind_goal;
543
544 if (SCM_NIMP (key) && SCM_JMPBUFP (key))
545 {
546 jmpbuf = key;
547 if (noreturn)
548 {
549 SCM_ASSERT (JBACTIVE (jmpbuf), jmpbuf,
550 "throw to dynamically inactive catch",
551 s_throw);
552 }
553 else if (!JBACTIVE (jmpbuf))
554 return SCM_UNSPECIFIED;
555 }
556 else
557 {
558 SCM dynpair = SCM_UNDEFINED;
559 SCM winds;
560
561 if (noreturn)
562 {
563 SCM_ASSERT (SCM_NIMP (key) && SCM_SYMBOLP (key), key, SCM_ARG1,
564 s_throw);
565 }
566 else if (!(SCM_NIMP (key) && SCM_SYMBOLP (key)))
567 return SCM_UNSPECIFIED;
568
569 /* Search the wind list for an appropriate catch.
570 "Waiter, please bring us the wind list." */
571 for (winds = scm_dynwinds; SCM_NIMP (winds); winds = SCM_CDR (winds))
572 {
573 if (! SCM_CONSP (winds))
574 abort ();
575
576 dynpair = SCM_CAR (winds);
577 if (SCM_NIMP (dynpair) && SCM_CONSP (dynpair))
578 {
579 SCM this_key = SCM_CAR (dynpair);
580
581 if (this_key == SCM_BOOL_T || this_key == key)
582 break;
583 }
584 }
585
586 /* If we didn't find anything, abort. scm_boot_guile should
587 have established a catch-all, but obviously things are
588 thoroughly screwed up. */
589 if (winds == SCM_EOL)
590 abort ();
591
592 /* If the wind list is malformed, bail. */
593 if (SCM_IMP (winds) || SCM_NCONSP (winds))
594 abort ();
595
596 if (dynpair != SCM_BOOL_F)
597 jmpbuf = SCM_CDR (dynpair);
598 else
599 {
600 if (!noreturn)
601 return SCM_UNSPECIFIED;
602 else
603 {
604 scm_exitval = scm_cons (key, args);
605 scm_dowinds (SCM_EOL, scm_ilength (scm_dynwinds));
606 #ifdef DEBUG_EXTENSIONS
607 scm_last_debug_frame = SCM_DFRAME (scm_rootcont);
608 #endif
609 longjmp (SCM_JMPBUF (scm_rootcont), 1);
610 }
611 }
612 }
613 for (wind_goal = scm_dynwinds;
614 SCM_CDAR (wind_goal) != jmpbuf;
615 wind_goal = SCM_CDR (wind_goal))
616 ;
617
618 /* Is a lazy catch? In wind list entries for lazy catches, the key
619 is bound to a lazy_catch smob, not a jmpbuf. */
620 if (SCM_LAZY_CATCH_P (jmpbuf))
621 {
622 struct lazy_catch *c = (struct lazy_catch *) SCM_CDR (jmpbuf);
623 SCM oldwinds = scm_dynwinds;
624 SCM handle, answer;
625 scm_dowinds (wind_goal, (scm_ilength (scm_dynwinds)
626 - scm_ilength (wind_goal)));
627 SCM_REDEFER_INTS;
628 handle = scm_dynwinds;
629 scm_dynwinds = SCM_CDR (scm_dynwinds);
630 SCM_REALLOW_INTS;
631 answer = (c->handler) (c->handler_data, key, args);
632 SCM_REDEFER_INTS;
633 SCM_SETCDR (handle, scm_dynwinds);
634 scm_dynwinds = handle;
635 SCM_REALLOW_INTS;
636 scm_dowinds (oldwinds, (scm_ilength (scm_dynwinds)
637 - scm_ilength (oldwinds)));
638 return answer;
639 }
640
641 /* Otherwise, it's a normal catch. */
642 else if (SCM_JMPBUFP (jmpbuf))
643 {
644 struct jmp_buf_and_retval * jbr;
645 scm_dowinds (wind_goal, (scm_ilength (scm_dynwinds)
646 - scm_ilength (wind_goal)));
647 jbr = (struct jmp_buf_and_retval *)JBJMPBUF (jmpbuf);
648 jbr->throw_tag = key;
649 jbr->retval = args;
650 }
651
652 /* Otherwise, it's some random piece of junk. */
653 else
654 abort ();
655
656 #ifdef DEBUG_EXTENSIONS
657 scm_last_debug_frame = SCM_JBDFRAME (jmpbuf);
658 #endif
659 longjmp (*JBJMPBUF (jmpbuf), 1);
660 }
661
662
663 void
664 scm_init_throw ()
665 {
666 scm_tc16_jmpbuffer = scm_newsmob (&jbsmob);
667 tc16_lazy_catch = scm_newsmob (&lazy_catch_funs);
668 #include "throw.x"
669 }