[plt-scheme] Examples of subprocess use, for expect-like dialogs?

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Tue Aug 4 05:29:17 EDT 2009

At Tue, 4 Aug 2009 13:56:44 +1000, Andrew Reilly wrote:
> Does anyone know of code examples or guide-level documentation
> that describes the why-s, wherefores and gotchas associated with
> (subprocess)?  I'm trying to do some expect-style chatting to an
> interactive command-line executable, and I suspect that I'm
> bumping into a buffer flushing or discipline issue and can't
> figure out what I'm doing wrong.  I haven't tried using separate
> threads, because it *should* be OK for the read-line side of the
> equation to block until the subprocess proudces output: it
> should respond immediately.
> [...]
> However if I change the subprocess call to:
>               (subprocess #f #f #f "/bin/cat" "-n")
> then it blocks at the first read-line.

On my machine, `cat -n' doesn't respond immediately unless the output
is a terminal. `cat -n -u' responds immediately (but then the example
gets stuck in the loop, since no result line is ever "EOM").

Beware, also, that if the multi-line string is much larger, then your
example can get stuck due to the buffers between the processes. If you
send a lot of text to `cat' without reading its output, then eventually
`cat' will block writing output until your program reads some of it. If
your program keeps writing, meanwhile, then it will eventually get
stuck for the same reason, since `cat' is stuck and not reading the
data that you're sending it.

Posted on the users mailing list.