1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-05-04 05:50:26 +02:00
guile/libguile/throw.c
Jim Blandy ee149d0368 * ioext.c (scm_do_read_line): Rewritten to use memchr to find the
newline.  A bit faster, and definitely hairier.
	(scm_read_line): Count newlines here instead.
	* strings.c (scm_take_str): New function.
	(scm_take0str): Reimplement in terms of scm_take_str.  * strings.h
	(scm_take_str): New declaration.  * ioext.c (scm_read_line): Use
	scm_take_str, to avoid copying the string.
	Add some simple-minded support for line buffered ports.
	* ports.h (SCM_BUFLINE): New flag for ports.
	* init.c (scm_init_standard_ports): Request line-buffering on
	the standard output port.
*	* ports.c (scm_mode_bits): Recognize 'l' as a request for line
	buffering.
	(scm_putc, scm_puts, scm_lfwrite): If the port is line-buffered,
	and there's a newline to be written, flush the port.
	* ports.c: (scm_lseek): clear buffers even if just reading current
	position.
	* fports.c (local_fclose): call local_fflush unconditionally.
	(various): don't use the scm_must... memory procs.
	* ports.h (scm_port): make read_pos a pointer to const.
	strports.c: take care of rw_active and rw_randow.
	fports.c: scm_fport_drain_input: removed.  do it all in ports.c.
	strports.c (scm_mkstrport): check that pos is reasonable.
	ioext.c (scm_ftell, scm_fseek): use lseek.
	(SCM_CLEAR_BUFFERS): macro deleted.
	ioext.c (redirect_port: use ptob fflush, read_flush.
	ports.h (scm_ptobfuns): add ftruncate.
	ports.c (scm_newptob): set ftruncate.
	adjust ptob tables.
*	ports.c (scm_ftruncate): new procedure.
	fports.c (local_ftrunate), strports.c (str_ftruncate): new procs.
	strports.c (st_seek, st_grow_port): new procs.
	fports.h (scm_port): change size types from int to off_t.
	ports.c (scm_init_ports): initialise the seek symbols here
	instead of in ioext.c.
	strports.c (scm_call_with_output_string): start with an empty
	string, so seek and ftruncate can be used.
	* ports.h (scm_ptobfuns): add a read_flush procedure which is the
	equivalent to fflush for the read buffer.
	* ports.c (scm_newptob): set read_flush.
	ports.c (void_port_ptob): set read_flush.
	fports.c (local_read_flush): new proc.  add to ptob.
	strport.c (st_read_flush): likewise.
	vport.c (sf_read_flush): likewise.
	fports.h (struct scm_fport): remove random member.  there's nothing
	left but fdes.  leaving it as a struct to allow for future changes.
	fports.c: replace usage of scm_fport::random with scm_port::rw_random.
	ports.c: (scm_putc, scm_puts, scm_lfwrite): call the read_flush
	ptob proc if the read buffer is filled.
	* ports.h (scm_port): add a rw_random member and replace
	reading and writing members with rw_active member.
	SCM_PORT_READ/SCM_PORT_WRITE: new values.
	* ports.h (struct scm_port_table): add writing and reading members
	to replace write_needs_seek: it isn't good enough for non-fports.
	ports.c, ioext.c, fports.c: corresponding changes.
	(struct scm_port_table): give it a typedef and rename to scm_port.
	ports.c, fports.c, strports.c, vports.c, ioext.c, ports.h:
	corresponding changes.
	* ports.c (scm_newptob): bugfix: set seek member.
*	* (scm_lseek): new procedure, using code from ioext.c:scm_fseek
	and generalised to all port types.
	* scmsigs.c (scm_init_scmsigs): set the SA_RESTART flag for all
	signals (it was only being done for handlers installed from Scheme).
	Otherwise (for example) SIGSTOP followed by SIGCONT on an interpreter
	waiting for input caused an EINTR error from read.
	* ports.h (struct scm_port_table): make all the char members
	unsigned, so they convert to int without becoming negative if large.
	* fports.c (scm_fdes_wait_for_input): forgot to check compilation
	with threads enabled.  rename this procedure to
	fport_wait_for_input and take a port instead of a fdes.
	use scm_fport_input_waiting_p instead of scm_fdes_waiting_p.
	* readline.c (scm_readline): Applied a patch from Greg Harvey to
	get readline support working again: use fdopen to get FILE objects.
	* gc.c (scm_init_storage): install an atexit proc to flush the
	ports.
	(cleanup): the new proc.  it sets a global variable which can be
	checked by the ptob flush procs to avoid trying to throw
	exceptions during exit.  not very pleasant but it seems more reliable.
	* fports.c (local_fflush): check terminating variable and if set
	don't throw exception.
	* CHECKME: that the atexit proc is installed if unexec used.
	* throw.c (scm_handle_by_message): don't flush all ports here.
	it still causes bus errors.
	* fports.h (SCM_FPORT_CLEAR_BUFFERS): rename to SCM_CLEAR_BUFFERS
	and move to ioext.c.
	* fports.c (scm_fdes_waiting_p): merged into fport_input_waiting_p.
	* ports.c (scm_char_ready_p): check the port buffer and call the
	ptob entry if needed.
	* ports.h (scm_ptobfuns): input_waiting_p added.  change all the
	ptob initialisers.  use it in char-ready
	* ioext.c (scm_do_read_line): moved from ports.c.  make it static.
	* vports.c (sfflush): modified to write a char (since softports
	currently use shortbuf.)
	* fports.c (scm_standard_stream_to_port): moved to init.c and
	made static.
	* init.c (scm_init_standard_ports): make stdout and stderr
	unbuffered if connected to a terminal.  with stdio they
	were line-buffered by default.
	* ports.h (scm_ptobfuns): change fflush return to void.
	change flush proc definitions.
	* strports.c (scm_call_with_output_string): get size from
	buffer instead of port stream.
	(scm_strprint_obj): likewise.
	(st_flush): new proc.
	* ports.h (struct scm_port_table): added write_end member,
	as an optimisation.  set it where write_buf_size is set.
	* ports.h (struct scm_port_table): change stream from void *
	back to SCM.  SCM presumably must be large enough to hold a
	pointer (and probably vice versa but who knows.)
	(SCM_SSTREAM): deleted.  change users back to SCM_STREAM.
	(scm_puts): rewritten
	* fports.c (local_ffwrite, local_fputs): removed.
	* strports.c (stputc, stputs, stwrite): dyked out (FIXME)
	* vports.c (sfputc, sfputs, sfwrite) likewise.
	* ports.c (write_void_port, puts_void_port): removed.
	(putc_void_port, getc_void_port, fgets_void_port): likewise.
	* ports.c (scm_lfwrite): rewritten using fport.c version.
	* fports.c (local_fputc): deleted.
	* ports.c (scm_add_to_port_table): initialise write_needs_seek.
	* ports.h (scm_ptobfuns): add seek function pointer.
	* fports.c: set it to local_seek, new procedure.
	* fports.h (SCM_MAYBE_DRAIN_INPUT): moved to ports.c.
	use ptob for seek. take ptob instead of fport arg.
	* ports.h (struct scm_port_table): new member write_needs_seek,
	replaces reading member in fport struct.
	* vports.c (sfgetc): store the getted char into the buffer.
	rename to sf_fill_buffer and install it for fill-buffer in ptob.
	the Scheme interface is still a	procedure that gets a char.
	(scm_make_soft_port): set up the port buffer (shortbuf).
	* fports.c (local_fgetc, local_fgets): deleted.
	* strports.c (stgetc): likewise.
	* ports.c: scm_generic_fgets: likewise.
	* ports.h (scm_ptobfuns): add fill_buffer.
	* ports.c (scm_newptob): assign it.
	* strports.c (scm_mkstrport): set up the buffer.
	put just the string into the stream, not cons (pos stream).
	(stfill_buffer): new proc.
	* ports.h: fport buffer moved into port table: to be
	used for all port types.
	* throw.c (scm_handle_by_message): flush ports at exit.
	* socket.c (scm_sock_fd_to_port): use scm_fdes_to_port.
	(scm_getsockopt, scm_setsockopt, scm_shutdown, scm_connect,
	scm_bind, scm_listen, scm_accept, scm_getsockname,
	scm_getpeername, scm_recv, scm_send, scm_recvfrom,
	scm_sendto,
	use SCM_FPORT_FDES.  use SCM_OPFPORTP not SCM_FPORTP.
	* posix.c (scm_getgroups): use SCM_ALLOW/DEFER_INTS.
	(scm_ttyname): use SCM_FPORT_FDES.
	(scm_tcgetpgrp, scm_tcsetpgrp): likewise.
	* ioext.c (scm_isatty_p): use SCM_FPORT_FDES.
	(scm_fdes_to_ports): modified.
	(scm_fdopen): use scm_fdes_to_port.
	* ports.c (scm_init_ports): don't try to flush ports using
	atexit().  it's too late, errors will cause SEGV.
	* fports.c (scm_fport_buffer_add): new procedure.
	* fports.h (SCM_FDES_RANDOM_P): new macro.  use it in
	scm_fdes_to_port and scm_redirect_port.
	* ioext.c (scm_redirect_port): use setvbuf to set buffers in the
	new port.  reset fp->random.
	* fports.c (scm_fdes_to_port), ports.c (scm_void_port),
	filesys.c (scm_opendir):
	restore defer interrupts while the port is constructed.
*	(scm_setvbuf): if mode is _IOFBF and size is not supplied,
	derive buffer size from fdes or use a default.
	(scm_fdes_to_port): use setvbuf instead of creating the buffers
	directly.
	vports.c (various places): use SCM_SSTREAM.
	strports.c: likewise.
	* gdbint.c: likewise.
	* ports.h (SCM_SSTREAM): new macro.
	* fports.c (scm_input_waiting_p): use scm_return_first, since port
	may be removed from the stack by the tail call to scm_fdes_waiting_p.
	* fports.h (SCM_CLEAR_BUFFERS): new macro.
	* ports.c (scm_force_output): call scm_fflush.
	* print.c (scm_newline): don't check errno for EPIPE (it wouldn't
*	reach this point.)  don't flush port (if scm_cur_outp).
	* fports.h (SCM_FPORT_FDES): new macro.
	* vports.c (sfflush): don't need to set errno.
	* ports.c: install scm_flush_all_ports to be run on exit.
	ports.c fports.c ioext.c posix.c socket.c net_db.c filesys.c:
	removed all uses of SCM_DEFER/ALLOW ints for now.  they were mainly
	just protecting errno.  some may need to be put back.
	* scmsigs.c (take_signal): save and restore errno while this
	proc runs.
	*fports.c (print_pipe_port, local_pclose, scm_pipob): deleted.
*	open-pipe, close-pipe are emulated in (ice-9 popen)
	ports.c (scm_ports_prehistory): don't init scm_pipob.
	ports.h (scm_tc16_pipe): deleted.
	posix.c (scm_open_pipe, scm_close_pipe): deleted.
	* ioext.c (scm_primitive_move_to_fdes): use fport.
	* fport.c (scm_fport_fill_buffer): flush write buffer if needed.
	change arg type from scm_fport to SCM port.
	fport.h (SCM_SETFDES): removed.
	(SCM_MAYBE_DRAIN_INPUT): new macro.
	* ioext.c (scm_dup_to_fdes): use SCM_FSTREAM.
	(scm_ftell): always use lseek and account for the buffer.
	(scm_fileno): use fport buffer.
	(scm_fseek): clear fport buffers.  always use lseek.
	* posix.c (scm_pipe): use fport buffer.
	* unif.c: include fports.h instead of genio.h.
	* fports.c (scm_fdes_wait_for_input, scm_fport_fill_buffer): new
	procedures.
	(local_fgetc): use them.
	(local_ffwrite): use buffer.
	(local_fgets): use buffer.
	(scm_setbuf0): deleted.
	(scm_setvbuf): set the buffer.
	(scm_setfileno): deleted.
	(scm_evict_ports): set fdes directly.
*	(scm_freopen): deleted.  doesn't seem useful in Guile.
	(scm_stdio_to_port): deleted.
	fports.h (struct scm_fport): add shortbuf member to avoid separate
	code for unbuffered ports.
	(SCM_FPORTP, SCM_OPFPORTP, SCM_OPINFPORTP, SCM_OPOUTFPORTP): moved
	from ports.h.
	* genio.c, genio.h: move contents into ports.c, ports.h.  The
	division wasn't useful.
	* fports.c, fports.h (scm_fport_drain_input): new procedure.
	* ports.c (scm_drain_input): call scm_fport_drain_input.
	* scm_fdes_waiting_p: new procedure.
	* fports.c (scm_fdes_to_port): allocate read and/or write buffers.
	(scm_input_waiting_p): check the buffer.
	(local_fgetc, local_fflush, local_fputc): likewise.
	* fports.h (scm_fport): read/write_buf,_pos,_buf_end,,_buf_size:
	new members.
	* init.c (scm_init_standard_ports): pass fdes instead of FILE *.
*	* ports.c (scm_drain_input): new procedure.
	ports.h: prototype.
	* fports.c (FPORT_READ_SAFE, FPORT_WRITE_SAFE, FPORT_ALL_OKAY,
	pre_read, pre_write): removed.
	(local_fputc, local_fputs, local_ffwrite): use write, not stdio.
	(scm_standard_stream_to_port): change first arg from FILE * to
	int fdes.
	(local_fflush): flush fdes, not FILE *.
	* fports.h (SCM_NOFTELL): removed.
	* genio.c, ports.c: don't include filesys.h.
	* genio.c (scm_getc): don't use scm_internal_select if FPORT.
	do it in fports.c:local_fgetc.
	* genio.c: don't use SCM_SYSCALL when calling ptob procedures.
	do it where it's needed in the port smobs.
	* filesys.c (scm_input_waiting_p): moved to fports.c, stdio
	  buffer support removed.  take SCM arg, not FILE *.
	* filesys.h: prototype moved too.
	* fports.c (scm_fdes_to_port): new procedure.
	(local_fgetc): use read not fgetc.
	(local_fclose): use close, not fclose.
	(local_fgets): use read, not fgets
	* fports.h: prototype for scm_fdes_to_port.
	* fports.h (scm_fport): new struct.
	* fports.c (scm_open_file): use open, not fopen.
	#include fcntl.h
	* ports.h (struct scm_port_table): change stream from SCM to void *.
	* ports.c (scm_add_to_port_table): check for memory allocation error.
	(scm_prinport): remove MSDOS hair.
	(scm_void_port): set stream to 0 instead of SCM_BOOL_F.
	(scm_close_port): don't throw errors: do it in fports.c.
1999-06-09 12:19:58 +00:00

755 lines
21 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
*
* As a special exception, the Free Software Foundation gives permission
* for additional uses of the text contained in its release of GUILE.
*
* The exception is that, if you link the GUILE library with other files
* to produce an executable, this does not by itself cause the
* resulting executable to be covered by the GNU General Public License.
* Your use of that executable is in no way restricted on account of
* linking the GUILE library code into it.
*
* This exception does not however invalidate any other reasons why
* the executable file might be covered by the GNU General Public License.
*
* This exception applies only to the code released by the
* Free Software Foundation under the name GUILE. If you copy
* code from other Free Software Foundation releases into a copy of
* GUILE, as the General Public License permits, the exception does
* not apply to the code that you add in this way. To avoid misleading
* anyone as to the status of such modified files, you must delete
* this exception notice from them.
*
* If you write modifications of your own for GUILE, it is your choice
* whether to permit this exception to apply to your modifications.
* If you do not wish that, delete this exception notice. */
#include <stdio.h>
#include "_scm.h"
#include "genio.h"
#include "smob.h"
#include "alist.h"
#include "eval.h"
#include "eq.h"
#include "dynwind.h"
#include "backtrace.h"
#ifdef DEBUG_EXTENSIONS
#include "debug.h"
#endif
#include "continuations.h"
#include "stackchk.h"
#include "stacks.h"
#include "fluids.h"
#include "throw.h"
/* the jump buffer data structure */
static int scm_tc16_jmpbuffer;
#define SCM_JMPBUFP(O) (SCM_TYP16(O) == scm_tc16_jmpbuffer)
#define JBACTIVE(O) (SCM_CAR (O) & (1L << 16L))
#define ACTIVATEJB(O) (SCM_SETOR_CAR (O, (1L << 16L)))
#define DEACTIVATEJB(O) (SCM_SETAND_CAR (O, ~(1L << 16L)))
#ifndef DEBUG_EXTENSIONS
#define JBJMPBUF(O) ((jmp_buf*)SCM_CDR (O) )
#define SETJBJMPBUF SCM_SETCDR
#else
#define SCM_JBDFRAME(O) ((scm_debug_frame*)SCM_CAR (SCM_CDR (O)) )
#define JBJMPBUF(O) ((jmp_buf*)SCM_CDR (SCM_CDR (O)) )
#define SCM_SETJBDFRAME(O,X) SCM_SETCAR (SCM_CDR (O), (SCM)(X))
#define SETJBJMPBUF(O,X) SCM_SETCDR(SCM_CDR (O), X)
static scm_sizet freejb SCM_P ((SCM jbsmob));
static scm_sizet
freejb (jbsmob)
SCM jbsmob;
{
scm_must_free ((char *) SCM_CDR (jbsmob));
return sizeof (scm_cell);
}
#endif
static int printjb SCM_P ((SCM exp, SCM port, scm_print_state *pstate));
static int
printjb (exp, port, pstate)
SCM exp;
SCM port;
scm_print_state *pstate;
{
scm_puts ("#<jmpbuffer ", port);
scm_puts (JBACTIVE(exp) ? "(active) " : "(inactive) ", port);
scm_intprint((SCM) JBJMPBUF(exp), 16, port);
scm_putc ('>', port);
return 1 ;
}
static scm_smobfuns jbsmob = {
0,
#ifdef DEBUG_EXTENSIONS
freejb,
#else
scm_free0,
#endif
printjb,
0
};
static SCM make_jmpbuf SCM_P ((void));
static SCM
make_jmpbuf ()
{
SCM answer;
SCM_NEWCELL (answer);
SCM_REDEFER_INTS;
{
#ifdef DEBUG_EXTENSIONS
char *mem = scm_must_malloc (sizeof (scm_cell), "jb");
SCM_SETCDR (answer, (SCM) mem);
#endif
SCM_SETCAR (answer, scm_tc16_jmpbuffer);
SETJBJMPBUF(answer, (jmp_buf *)0);
DEACTIVATEJB(answer);
}
SCM_REALLOW_INTS;
return answer;
}
/* scm_internal_catch (the guts of catch) */
struct jmp_buf_and_retval /* use only on the stack, in scm_catch */
{
jmp_buf buf; /* must be first */
SCM throw_tag;
SCM retval;
};
/* scm_internal_catch is the guts of catch. It handles all the
mechanics of setting up a catch target, invoking the catch body,
and perhaps invoking the handler if the body does a throw.
The function is designed to be usable from C code, but is general
enough to implement all the semantics Guile Scheme expects from
throw.
TAG is the catch tag. Typically, this is a symbol, but this
function doesn't actually care about that.
BODY is a pointer to a C function which runs the body of the catch;
this is the code you can throw from. We call it like this:
BODY (BODY_DATA, JMPBUF)
where:
BODY_DATA is just the BODY_DATA argument we received; we pass it
through to BODY as its first argument. The caller can make
BODY_DATA point to anything useful that BODY might need.
JMPBUF is the Scheme jmpbuf object corresponding to this catch,
which we have just created and initialized.
HANDLER is a pointer to a C function to deal with a throw to TAG,
should one occur. We call it like this:
HANDLER (HANDLER_DATA, THROWN_TAG, THROW_ARGS)
where
HANDLER_DATA is the HANDLER_DATA argument we recevied; it's the
same idea as BODY_DATA above.
THROWN_TAG is the tag that the user threw to; usually this is
TAG, but it could be something else if TAG was #t (i.e., a
catch-all), or the user threw to a jmpbuf.
THROW_ARGS is the list of arguments the user passed to the THROW
function, after the tag.
BODY_DATA is just a pointer we pass through to BODY. HANDLER_DATA
is just a pointer we pass through to HANDLER. We don't actually
use either of those pointers otherwise ourselves. The idea is
that, if our caller wants to communicate something to BODY or
HANDLER, it can pass a pointer to it as MUMBLE_DATA, which BODY and
HANDLER can then use. Think of it as a way to make BODY and
HANDLER closures, not just functions; MUMBLE_DATA points to the
enclosed variables.
Of course, it's up to the caller to make sure that any data a
MUMBLE_DATA needs is protected from GC. A common way to do this is
to make MUMBLE_DATA a pointer to data stored in an automatic
structure variable; since the collector must scan the stack for
references anyway, this assures that any references in MUMBLE_DATA
will be found. */
SCM
scm_internal_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;
{
struct jmp_buf_and_retval jbr;
SCM jmpbuf;
SCM answer;
jmpbuf = make_jmpbuf ();
answer = SCM_EOL;
scm_dynwinds = scm_acons (tag, jmpbuf, scm_dynwinds);
SETJBJMPBUF(jmpbuf, &jbr.buf);
#ifdef DEBUG_EXTENSIONS
SCM_SETJBDFRAME(jmpbuf, scm_last_debug_frame);
#endif
if (setjmp (jbr.buf))
{
SCM throw_tag;
SCM throw_args;
#ifdef STACK_CHECKING
scm_stack_checking_enabled_p = SCM_STACK_CHECKING_P;
#endif
SCM_REDEFER_INTS;
DEACTIVATEJB (jmpbuf);
scm_dynwinds = SCM_CDR (scm_dynwinds);
SCM_REALLOW_INTS;
throw_args = jbr.retval;
throw_tag = jbr.throw_tag;
jbr.throw_tag = SCM_EOL;
jbr.retval = SCM_EOL;
answer = handler (handler_data, throw_tag, throw_args);
}
else
{
ACTIVATEJB (jmpbuf);
answer = body (body_data);
SCM_REDEFER_INTS;
DEACTIVATEJB (jmpbuf);
scm_dynwinds = SCM_CDR (scm_dynwinds);
SCM_REALLOW_INTS;
}
return answer;
}
/* scm_internal_lazy_catch (the guts of lazy catching) */
/* 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_puts (buf, port);
return 1;
}
static scm_smobfuns lazy_catch_funs = {
0, 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<43>teau
Gollombiere '72, non?"). */
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. */
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_REDEFER_INTS;
scm_dynwinds = SCM_CDR (scm_dynwinds);
SCM_REALLOW_INTS;
return answer;
}
/* scm_internal_stack_catch
Use this one if you want debugging information to be stored in
scm_the_last_stack_fluid on error. */
static SCM
ss_handler (void *data, SCM tag, SCM throw_args)
{
/* Save the stack */
scm_fluid_set_x (SCM_CDR (scm_the_last_stack_fluid),
scm_make_stack (scm_cons (SCM_BOOL_T, SCM_EOL)));
/* Throw the error */
return scm_throw (tag, throw_args);
}
struct cwss_data
{
SCM tag;
scm_catch_body_t body;
void *data;
};
static SCM
cwss_body (void *data)
{
struct cwss_data *d = data;
return scm_internal_lazy_catch (d->tag, d->body, d->data, ss_handler, NULL);
}
SCM
scm_internal_stack_catch (SCM tag,
scm_catch_body_t body,
void *body_data,
scm_catch_handler_t handler,
void *handler_data)
{
struct cwss_data d;
d.tag = tag;
d.body = body;
d.data = body_data;
return scm_internal_catch (tag, cwss_body, &d, handler, handler_data);
}
/* body and handler functions for use with any of the above catch variants */
/* This is a body function you can pass to scm_internal_catch if you
want the body to be like Scheme's `catch' --- a thunk.
BODY_DATA is a pointer to a scm_body_thunk_data structure, which
contains the Scheme procedure to invoke as the body, and the tag
we're catching. */
SCM
scm_body_thunk (body_data)
void *body_data;
{
struct scm_body_thunk_data *c = (struct scm_body_thunk_data *) body_data;
return scm_apply (c->body_proc, SCM_EOL, SCM_EOL);
}
/* This is a handler function you can pass to scm_internal_catch if
you want the handler to act like Scheme's catch: (throw TAG ARGS ...)
applies a handler procedure to (TAG ARGS ...).
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 variable (i.e., one living on
the stack), 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_HANDLE_BY_PROC_CATCHING_ALL is like SCM_HANDLE_BY_PROC but
catches all throws that the handler might emit itself. The handler
used for these `secondary' throws is SCM_HANDLE_BY_MESSAGE_NO_EXIT. */
struct hbpca_data {
SCM proc;
SCM args;
};
static SCM
hbpca_body (body_data)
void *body_data;
{
struct hbpca_data *data = (struct hbpca_data *)body_data;
return scm_apply (data->proc, data->args, SCM_EOL);
}
SCM
scm_handle_by_proc_catching_all (handler_data, tag, throw_args)
void *handler_data;
SCM tag;
SCM throw_args;
{
SCM *handler_proc_p = (SCM *) handler_data;
struct hbpca_data data;
data.proc = *handler_proc_p;
data.args = scm_cons (tag, throw_args);
return scm_internal_catch (SCM_BOOL_T,
hbpca_body, &data,
scm_handle_by_message_noexit, NULL);
}
/* Derive the an exit status from the arguments to (quit ...). */
int
scm_exit_status (args)
SCM args;
{
if (SCM_NNULLP (args))
{
SCM cqa = SCM_CAR (args);
if (SCM_INUMP (cqa))
return (SCM_INUM (cqa));
else if (SCM_FALSEP (cqa))
return 1;
}
return 0;
}
static void
handler_message (void *handler_data, SCM tag, SCM args)
{
char *prog_name = (char *) handler_data;
SCM p = scm_cur_errp;
if (scm_ilength (args) >= 3)
{
SCM stack = scm_make_stack (SCM_LIST1 (SCM_BOOL_T));
SCM subr = SCM_CAR (args);
SCM message = SCM_CADR (args);
SCM parts = SCM_CADDR (args);
SCM rest = SCM_CDDDR (args);
scm_display_error (stack, p, subr, message, parts, rest);
}
else
{
if (! prog_name)
prog_name = "guile";
scm_puts (prog_name, p);
scm_puts (": ", p);
scm_puts ("uncaught throw to ", p);
scm_prin1 (tag, p, 0);
scm_puts (": ", p);
scm_prin1 (args, p, 1);
scm_putc ('\n', p);
}
}
/* This is a handler function to use if you want scheme to print a
message and die. Useful for dealing with throws to uncaught keys
at the top level.
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;
{
if (SCM_NFALSEP (scm_eq_p (tag, SCM_CAR (scm_intern0 ("quit")))))
{
exit (scm_exit_status (args));
}
handler_message (handler_data, tag, args);
/* try to flush the error message first before the rest of the
ports: if any throw error, it currently causes a bus
exception. */
exit (2);
}
/* This is just like scm_handle_by_message, but it doesn't exit; it
just returns #f. It's useful in cases where you don't really know
enough about the body to handle things in a better way, but don't
want to let throws fall off the bottom of the wind list. */
SCM
scm_handle_by_message_noexit (handler_data, tag, args)
void *handler_data;
SCM tag;
SCM args;
{
handler_message (handler_data, tag, args);
return SCM_BOOL_F;
}
SCM
scm_handle_by_throw (handler_data, tag, args)
void *handler_data;
SCM tag;
SCM args;
{
scm_ithrow (tag, args, 1);
return SCM_UNSPECIFIED; /* never returns */
}
/* the Scheme-visible CATCH and LAZY-CATCH functions */
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 ((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);
}
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);
}
/* throwing */
SCM_PROC(s_throw, "throw", 1, 0, 1, scm_throw);
SCM
scm_throw (key, args)
SCM key;
SCM args;
{
SCM_ASSERT (SCM_NIMP (key) && SCM_SYMBOLP (key), key, SCM_ARG1, s_throw);
/* May return if handled by lazy catch. */
return scm_ithrow (key, args, 1);
}
SCM
scm_ithrow (key, args, noreturn)
SCM key;
SCM args;
int noreturn;
{
SCM jmpbuf = SCM_UNDEFINED;
SCM wind_goal;
SCM dynpair = SCM_UNDEFINED;
SCM winds;
/* Search the wind list for an appropriate catch.
"Waiter, please bring us the wind list." */
for (winds = scm_dynwinds; SCM_NIMP (winds); winds = SCM_CDR (winds))
{
if (! SCM_CONSP (winds))
abort ();
dynpair = SCM_CAR (winds);
if (SCM_NIMP (dynpair) && SCM_CONSP (dynpair))
{
SCM this_key = SCM_CAR (dynpair);
if (this_key == SCM_BOOL_T || this_key == key)
break;
}
}
/* If we didn't find anything, abort. scm_boot_guile should
have established a catch-all, but obviously things are
thoroughly screwed up. */
if (winds == SCM_EOL)
abort ();
/* If the wind list is malformed, bail. */
if (SCM_IMP (winds) || SCM_NCONSP (winds))
abort ();
if (dynpair != SCM_BOOL_F)
jmpbuf = SCM_CDR (dynpair);
else
{
if (!noreturn)
return SCM_UNSPECIFIED;
else
{
scm_exitval = scm_cons (key, args);
scm_dowinds (SCM_EOL, scm_ilength (scm_dynwinds));
#ifdef DEBUG_EXTENSIONS
scm_last_debug_frame = SCM_DFRAME (scm_rootcont);
#endif
longjmp (SCM_JMPBUF (scm_rootcont), 1);
}
}
for (wind_goal = scm_dynwinds;
SCM_CDAR (wind_goal) != jmpbuf;
wind_goal = SCM_CDR (wind_goal))
;
/* Is a lazy catch? In wind list entries for lazy catches, the key
is bound to a lazy_catch smob, not a jmpbuf. */
if (SCM_LAZY_CATCH_P (jmpbuf))
{
struct lazy_catch *c = (struct lazy_catch *) SCM_CDR (jmpbuf);
SCM oldwinds = scm_dynwinds;
SCM handle, answer;
scm_dowinds (wind_goal, (scm_ilength (scm_dynwinds)
- scm_ilength (wind_goal)));
SCM_REDEFER_INTS;
handle = scm_dynwinds;
scm_dynwinds = SCM_CDR (scm_dynwinds);
SCM_REALLOW_INTS;
answer = (c->handler) (c->handler_data, key, args);
SCM_REDEFER_INTS;
SCM_SETCDR (handle, scm_dynwinds);
scm_dynwinds = handle;
SCM_REALLOW_INTS;
scm_dowinds (oldwinds, (scm_ilength (scm_dynwinds)
- scm_ilength (oldwinds)));
return answer;
}
/* Otherwise, it's a normal catch. */
else if (SCM_JMPBUFP (jmpbuf))
{
struct jmp_buf_and_retval * jbr;
scm_dowinds (wind_goal, (scm_ilength (scm_dynwinds)
- scm_ilength (wind_goal)));
jbr = (struct jmp_buf_and_retval *)JBJMPBUF (jmpbuf);
jbr->throw_tag = key;
jbr->retval = args;
}
/* Otherwise, it's some random piece of junk. */
else
abort ();
#ifdef DEBUG_EXTENSIONS
scm_last_debug_frame = SCM_JBDFRAME (jmpbuf);
#endif
longjmp (*JBJMPBUF (jmpbuf), 1);
}
void
scm_init_throw ()
{
scm_tc16_jmpbuffer = scm_newsmob (&jbsmob);
tc16_lazy_catch = scm_newsmob (&lazy_catch_funs);
#include "throw.x"
}