#lang scheme/base
(require
scheme/control )
(provide repl)
(define (with-timeout sec thunk)
(define retv #t)
(define (watchdog)
(sleep sec)
(set! retv #f))
(let ((ok (thread thunk))
(timeout (thread watchdog)))
(sync ok timeout)
(kill-thread ok)
(kill-thread timeout)
retv))
(define (repl command [OK "OK"])
(define (_read-line)
(with-handlers
((exn:break? (lambda _ "")))
(read-line)))
(define (_command cmd)
(prompt
(with-handlers
((exn:break?
(lambda _
(printf "\nCommand \"~a\" interrupted.\n" cmd)
(command "cold")
(let ((ok (with-timeout 1 (lambda () (command "OK")))))
(unless ok (printf "Timed out.\n"))
ok))))
(command cmd)
(command OK)
#t)))
(define (console)
(let ((cmd (_read-line)))
(unless (eof-object? cmd)
(when (_command cmd)
(console)))))
(with-handlers ((void void))
(file-stream-buffer-mode (current-output-port) 'none))
(printf "Press ctrl-D to quit.\n")
(when (_command "") (console))
(printf "Dada.\n")
)