scheme shell
about
download
support
resources
docu
links
 
scsh.net

Controlc Handler Example

Control-C handler example


 #!/usr/local/bin/scsh \
 -o sigevents -o threads -e main -s
 !#

(define (exit-after-some-control-cs control-cs-needed)

;; This is crucial. Alas, its crucialness (cruciality?) is not ;; clearly pointed out in the scsh manual. (set-interrupt-handler interrupt/int #f)

(format #t "Hit control-C ~A times to exit.~%" control-cs-needed)

(spawn (lambda () (let loop ((sigevent (most-recent-sigevent))) (let ((next (next-sigevent sigevent interrupt/int))) (set! control-cs-needed (- control-cs-needed 1))

(if (zero? control-cs-needed) (begin (format #t "OK, you can go now.~%") (exit 0)) (format #t "Hit control-C ~A more times, and I'll let you go.~%" control-cs-needed)) (loop next))))))

(define (main args) (format #t "Greetings. I'm the main program.~%") (exit-after-some-control-cs 3) (let loop () (format #t "~A: main program pretending to do something useful.~%" (format-date "~c ~Z" (date (time))))

;; `sleep' appears *not* to work just like the C library function ;; of the same name: the latter returns immediately if a signal ;; arrives, whereas the former keeps chuggin'. That's exactly ;; what I want; thus I don't (as I'd feared) need to use some form ;; of `select', as I would if this were a C program. Nice! (sleep 2000)

(loop)))


ControlcHandlerExample - raw wiki source | code snippets archive