Does anybody have an example of some scheme code that will catch a
signal?
Yes. jacal/math.scm (which I am appending). I don't claim any
inherent beauty for this way of defining interrupt handlers; I just
needed to be able to:
(1) Catch asychronous interrupts.
(2) Catch errors (underflow, ...)
(3) Be able to dynamically bind interrupt handlers.
(4) Be compatible with other Scheme implementations.
(4) is accomplished by having SCM callback to named interrupt handlers
only if they are defined to procedures. Other Scheme implementations
which don't use this mechanism will ignore my handlers and handle
interrupts in their own way.
Since (4) is not a goal of GUILE, maybe someone will come up with much
nicer interrupt handlers.
I have tried discerning the source code (doing "grep" for
"signal" and "exception" and "catch", rereading libguile/error.c, etc,
etc), but I haven't had much luck.
I ran across the struct errdesc { ... } definition in error.c, and it
looked promising, but repeated tries of:
(catch 'user-interrupt (lambda () (do () (#f))) (lambda args args))
^C
just resulted in:
ERROR: user interrupt
I've tried almost every key I can think of, but I just can't get the
"catch" to catch specific signals.
I need to catch SIGHUP, SIGINT, SIGUSR1, and SIGUSR2. Please, does
somebody know how to do this?
SIGHUP == hang-up
SIGINT == user-interrupt
Doing (catch #t ...) just won't cut it, since I need to do different
things for different signals (and I don't want to catch syntax
errors).
See guile/error.c "struct errdesc scm_errmsgs[]" for where the
interrupt handlers get defined. It should be easy to add more. I am
also appending the "Interrupts" info page which is part of
guile-src/guile-docs/user/scm.info.
================================================================
;;; JACAL: Symbolic Mathematics System. -*-scheme-*-
;;; Copyright 1989, 1990, 1991, 1992, 1993 Aubrey Jaffer.
;;; See the file "COPYING" for terms applying to this program.
;;; See Template.scm in the Scheme Library for how to set up
;;; vicinities and require.
(define jacal-vicinity (program-vicinity))
;; Save our vicinity for later dynamic loads.
(slib:load (in-vicinity (program-vicinity) "scl"))
;; Common Lisp/Scheme compatability definitions.
(slib:load (in-vicinity (program-vicinity) "toploads"))
;; Initialize modes to something reasonable.
(slib:load (in-vicinity (program-vicinity) "modeinit"))
;;;; error and interrupt response for SCM.
;;; Put appropriate handlers for other systems here.
(define (impl:error str args)
(force-output)
(newline-diag)
(perror (tran:translate 'ERROR))
(set-errno 0)
(display-diag str)
(display-diag (tran:translate 'Last-expression-lost))
(newline-diag)
(display-diag args)
(newline-diag)
(force-output)
(math:exit #f)) ;return to math top level.
;;;; These are error handlers for SCM.
;;(define out-of-storage #f)
(define could-not-open #f)
(define arithmetic-error #f)
;;(define user-interrupt #f)
(define end-of-program #f)
;(define hang-up end-of-program) ;automatic
(define (set-handlers!)
;; (set! out-of-storage (lambda args (impl:error "Out of storage" args)))
(set! could-not-open (lambda args (impl:error "File not found" args)))
(set! arithmetic-error (lambda args (impl:error "Arithmetic Error" args)))
;; (set! user-interrupt (lambda args (impl:error "User Interrupt" args)))
(set! end-of-program (lambda args (math:exit #t args))))
(define (cleanup-handlers!)
;; (set! out-of-storage #f)
(set! could-not-open #f)
(set! arithmetic-error #f)
;; (set! user-interrupt #f)
(set! end-of-program #f))
================================================================
File: scm.info, Node: Interrupts, Next: Process Synchronization, Prev: Time, Up: Standard Facilities
Interrupts
==========
-- Function: ticks N
Returns the number of ticks remaining till the next tick interrupt.
Ticks are an arbitrary unit of evaluation. Ticks can vary greatly
in the amount of time they represent.
If N is 0, any ticks request is canceled. Otherwise a
`ticks-interrupt' will be signaled N from the current time.
`ticks' is supported if SCM is compiled with the `ticks' flag
defined.
-- Callback procedure: ticks-interrupt ...
Establishes a response for tick interrupts. Another tick
interrupt will not occur unless `ticks' is called again. Program
execution will resume if the handler returns. This procedure
should (abort) or some other action which does not return if it
does not want processing to continue.
-- Function: alarm SECS
Returns the number of seconds remaining till the next alarm
interrupt. If SECS is 0, any alarm request is canceled. Otherwise
an `alarm-interrupt' will be signaled SECS from the current time.
ALARM is not supported on all systems.
-- Callback procedure: user-interrupt ...
-- Callback procedure: alarm-interrupt ...
Establishes a response for `SIGINT' (control-C interrupt) and
`SIGALRM' interrupts. Program execution will resume if the handler
returns. This procedure should `(abort)' or some other action
which does not return if it does not want processing to continue
after it returns.
Interrupt handlers are disabled during execution `system' and `ed'
procedures.
To unestablish a response for an interrupt set the handler symbol
to `#f'. For instance, `(set! user-interrupt #f)'.
-- Callback procedure: out-of-storage ...
-- Callback procedure: could-not-open ...
-- Callback procedure: end-of-program ...
-- Callback procedure: hang-up ...
-- Callback procedure: arithmetic-error ...
Establishes a response for storage allocation error, file opening
error, end of program, SIGHUP (hang up interrupt) and arithmetic
errors respectively. This procedure should (abort) or some other
action which does not return if it does not want the default error
message to also be displayed. If no procedure is defined for
HANG-UP then END-OF-PROGRAM (if defined) will be called.
To unestablish a response for an error set the handler symbol to
`#f'. For instance, `(set! could-not-open #f)'.