Subject: (listen ...)

From: Charles Blair <[email-masked]>
Date: Fri, 30 Apr 1999 15:19:54 -0500

i wonder whether someone could explain the following to me.

if i use (listen ...) to test whether a stream i've opened has
characters in it to read, and i do this interactively, i get results i
expect:

> (defvar s (run-program "date" :output :stream))
S
> (listen s)
T
> (read-line s)
"Fri Apr 30 10:39:28 CDT 1999" ;
NIL
> (listen s)
NIL

however, if i execute this file:

#!/[...]/clisp
 
(print (defvar s (run-program "date" :output :stream)))
(print (listen s))
(print (read-line s))
(print (listen s))

i get these results:

S
NIL
"Fri Apr 30 10:44:00 CDT 1999"
NIL

that is, the first call to listen results in nil. /[...]/clisp is this:

#!/bin/sh
exec /[...]/clisp/lib/lisp/lisp.run -M /[...]/clisp/lib/lisp/lispinit.mem -N /[...]/clisp/share/locale "$@"

i get the same results if i try this:

/[...]/clisp/lib/lisp/lisp.run -M /[...]/clisp/lib/lisp/lispinit.mem -N /[...]/clisp/share/locale [filename]

or if i try this:

> (load "[file")
;; Loading file [file] ...
S
NIL
"Fri Apr 30 11:50:42 CDT 1999"
NIL

it seems that there is some side-effect in trying to execute (listen
...) from within a file which i'm not quite grasping.

however, an attempt to look at the stream before the first call to
listen, using peek-char or read-line, for example, will cause listen
to work as expected. so, the following commands:

(print (defvar s (run-program "date" :output :stream)))
(print (listen s))
(print (peek-char nil s))
(print (listen s))
(exit)

when executed from a file, as above, result in this:
 
S
NIL
#\F
T

that is, the second call to listen showed that a character was available. 

n.b. if the first call to listen tests a string input stream, then i
get expected results even if the call is executed from within a
file. so this strikes me as being the result of an interaction with the
environment.

any enlightenment on what is going on here would be greatly
appreciated; thanks.

---

From: Bruno Haible <[email-masked]>
Date: Fri, 30 Apr 1999 23:42:19 +0200 (MET DST)

> > (defvar s (run-program "date" :output :stream))

`listen' returns, according to the HyperSpec, T "if there is a character
immediately available from input-stream". NIL does not mean end-of-stream,
it means "wait, maybe there are still characters coming, maybe not".

When you spawn the "date" program, it needs a few milliseconds until it
produces its first output character. Not enough time for you to type
in "(listen s)" and press the Enter key, but, when loaded from a file,
enough time for clisp to reach and evaluate the next command.

Bruno

---

From: Charles Blair <[email-masked]>
Date: Fri, 30 Apr 1999 23:09:40 -0500

thanks. i tested this with a short `sleep' command before `listen',
and it works now as expected when loaded from a file.