Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

read-line-no-hang

43 views
Skip to first unread message

OMouse

unread,
May 20, 2011, 6:12:49 PM5/20/11
to
Does anyone have code for a read-line-no-hang? I'm using CLISP for a
console program and I want some background processing to continue
while I'm waiting for input. I understand it doesn't have threads or
that threads are experimental and I'm also not interested in running a
separate process.

I also don't want to re-invent the wheel if there's a library that has
a non-blocking read-line.

Pascal J. Bourguignon

unread,
May 20, 2011, 6:42:37 PM5/20/11
to
OMouse <omo...@gmail.com> writes:


(defvar *read-line-no-hang-buffer* (make-array 80 :element-type 'character :adjustable t :fill-pointer 0))

(defun read-line-no-hang (&optional (stream *standard-input*) (eof-error-p t) eof-value)
(loop
:for ch = (read-char-no-hang stream nil :eof)
:do (case ch
((nil) (return nil))
((:eof) (if eof-error-p
(error 'end-of-file :stream stream)
(return eof-value)))
((#\newline) (return (prog1 (copy-seq *read-line-no-hang-buffer*)
(setf (fill-pointer *read-line-no-hang-buffer*) 0))))
(otherwise (vector-push-extend ch *read-line-no-hang-buffer*)))))

(defun interactive-test/read-line-no-hang ()
(loop
:named test
:for line = (read-line-no-hang)
:for counter :from 0
:initially (terpri) (princ "Type something while I work (type done when done): ")
:do (when line
(princ "After ") (princ counter) (princ " loops, read: ") (write-line line)
(terpri) (princ "Type something while I work (type done when done): "))
:until (string-equal "done" (string-trim #(#\space #\tab) line))))


--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.

Rob Warnock

unread,
May 20, 2011, 9:37:47 PM5/20/11
to
OMouse <omo...@gmail.com> wrote:
+---------------

| Does anyone have code for a read-line-no-hang? I'm using CLISP for a
| console program and I want some background processing to continue
| while I'm waiting for input. I understand it doesn't have threads or
| that threads are experimental and I'm also not interested in running a
| separate process.
+---------------

If this is under Unix and you haven't messed with the default terminal
settings, then no characters will be "ready" [in the sense of LISTEN or
READ-CHAR-NO-HANG] until you type a newline, so this should be all you need:

(and (listen) (read-line))

That is:

> (loop for line = (and (listen) (read-line))
until line
do
;; Your real "background" application goes here:
(format t "~&Help! Get me out of here! Type something, then <RET>: ")
(force-output)
(sleep 3)
finally
(format t "Thanks! You typed: ~s~%" line)
(return line))

Help! Get me out of here! Type something, then <RET>:
Help! Get me out of here! Type something, then <RET>:
Help! Get me out of here! Type something, then <RET>: O.k., y
Help! Get me out of here! Type something, then <RET>: ou mean
Help! Get me out of here! Type something, then <RET>: like th
Help! Get me out of here! Type something, then <RET>: is?
Thanks! You typed: "O.k., you mean like this?"
"O.k., you mean like this?"
>


-Rob

-----
Rob Warnock <rp...@rpw3.org>
627 26th Avenue <http://rpw3.org/>
San Mateo, CA 94403

OMouse

unread,
May 20, 2011, 11:02:12 PM5/20/11
to
On May 20, 9:37 pm, r...@rpw3.org (Rob Warnock) wrote:
> OMouse  <omo...@gmail.com> wrote:
>
> +---------------
> | Does anyone have code for a read-line-no-hang? I'm using CLISP for a
> | console program and I want some background processing to continue
> | while I'm waiting for input. I understand it doesn't have threads or
> | that threads are experimental and I'm also not interested in running a
> | separate process.
> +---------------
>
> If this is under Unix and you haven't messed with the default terminal
> settings, then no characters will be "ready" [in the sense of LISTEN or
> READ-CHAR-NO-HANG] until you type a newline, so this should be all you need:
>
>     (and (listen) (read-line))
>

Ah cool, I tested it out in Windows with CLISP (I think it uses Cygwin
or the default terminal) and it almost worked well. The only problem
is that a character or two were re-read. I don't care about that
though, I'm only going to be using it in a GNU/Linux terminal anyway ;p

0 new messages