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

From: Eli Barzilay (eli at barzilay.org)
Date: Tue Jan 28 22:08:06 EST 2003

On Jan 28, Peter Santoro wrote:
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> I would like to be able use non-blocking keyboard input in my
> customized REPL user interface.  Is there a non-blocking read-char
> available in mzscheme, so that the ENTER key doesn't always have to
> be pressed?

According to your headers your using Linux, I'm assuming that's your
environment.  The first problem in getting that is to make the
terminal give you each character as it is typed, which is not too
related to blocking or not.  For this `stty' is useful to change the
terminal settings (and `tty' to see if you are working with an
interactive terminal).  Below is some code that should get you started
if this is the problem.

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!

(define tty?
      (((p pout pin perr)
         (current-output-port) (current-input-port) (current-error-port)
         (find-executable-path "tty" #f) "-s")))
    (subprocess-wait p)
    (zero? (subprocess-status p))))
(define (stty . args)
      (((p pout pin perr)
        (apply subprocess #f (current-input-port) (current-error-port)
               (find-executable-path "stty" #f) args)))
    (begin0 (read-line pout) (subprocess-wait p))))

(unless tty? (error "Not running with a tty."))
(let ((tty-settings (stty "-g")))
  (stty "-icanon" "-echo" "min" "1")
  (let loop ()
    (printf ">>> ") (flush-output)
    (let ((ch (read-char)))
      (eprintf "~s\n>>> " ch) (flush-output)
      (unless (eq? ch #\q) (loop))))
  (printf "\nBye.\n")
  (stty tty-settings))

Posted on the users mailing list.