chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-users] Re: with-open-pipe to handle sigpipe


From: Lui Fungsin
Subject: [Chicken-users] Re: with-open-pipe to handle sigpipe
Date: Tue, 15 Apr 2008 16:02:10 -0700

OK, so the problem is that sigpipe is not handled.

Does the following code look reasonable?

Specifically, I'm not sure if escaping with the continuation k is the
proper way to gracefully handle the signal.

Thanks.

(declare (uses extras regex posix utils))
(require-extension loop)
(require-extension miscmacros)

(define-macro (unwind-protect body . cleanup)
  `(dynamic-wind (let ((ok? #t))
                   (lambda ()
                     (if ok? (set! ok? #f) (error))))
       (lambda () ,body)
       (lambda () ,@cleanup)))

(define-macro (with-signal-handler signum handler . body)
  (let ((old (gensym))
        (signo (gensym)))
    `(let* ((,signo ,signum)
            (,old (signal-handler ,signo)))
       (set-signal-handler! ,signo ,handler)
       (unwind-protect
        (begin ,@body)
        (when ,old (set-signal-handler! ,signo ,old))))))

;;;
;;; need to catch sigpipe
;;;
(define (with-output-to-pipe* cmd thunk . mode)
  (let ((p (apply open-output-pipe cmd mode)))
   (let/cc k
    (with-signal-handler
     signal/pipe (lambda (signo) (k (void)))
     (unwind-protect
      (with-output-to-port p thunk)
      (close-output-pipe p))))))

(define-macro (with-output-to-less . body)
  `(with-output-to-pipe* "/usr/bin/less -X -E -M" (lambda () ,@body)))

(define (command-loop)
  (loop for line = (begin (display "Enter a number or `exit' to quit>
") (read-line))
        while (and line (not (eof-object? line)))
        if (equal? line "exit") do
        (exit)
        else do
        (let ((n (ignore-errors (string->number line))))
          (when n
              (with-output-to-less
               (repeat n (print "Chicken Chicken Chicken Chicken
Chicken Chicken")))))))

(command-loop)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]