[plt-scheme] Re: non-blocking keyboard input inside customized repl

From: Peter Santoro (peter at pscomp.com)
Date: Wed Jan 29 10:29:36 EST 2003

Thanks for the help, Eli.  I've modified the code a bit and I'm 
including it below in the event someone else is looking for this on 
Linux.  As I'm not a scheme guru, any suggestions for improvement are 


(define tty-path (delay (find-executable-path "tty" #f) ) )
(define stty-path (delay (find-executable-path "stty" #f) ) )

(define isatty?
   (delay (let*-values
     (((p pout pin perr)
         (current-output-port) (current-input-port) (current-error-port)
           (force tty-path) "-s")))
     (subprocess-wait p)
     (zero? (subprocess-status p)))))

(define (stty . args)
     (((p pout pin perr)
       (apply subprocess #f (current-input-port) (current-error-port)
         (force stty-path) args)))
     (begin0 (read-line pout) (subprocess-wait p))))

(define (read-char-immediate)
   (unless (force isatty?) (error "Not running with a tty."))
   (let ( (ch #\newline) (tty-settings (stty "-g")) )
     (stty "-icanon" "-echo" "min" "1" "time" "0")
     (let ((ch (read-char) ))
       (stty tty-settings)
       ch ) ) )

Posted on the users mailing list.