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

From: Andrew Reilly (andrew-scheme at areilly.bpc-users.org)
Date: Mon Aug 3 23:56:44 EDT 2009

Hi all,

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.

What is even more confusing, is that simple examples that I've
tried work or fail (block forever, usually) depending on what
appear to be unrelated details.

For example, this works as expected:

(let-values (((proc in-p outp errp)
              (subprocess #f #f #f "/bin/cat")))
  (display "foo bar baz quux and so on" outp) (newline outp) (flush-output outp)
  (display (read-line in-p)) (newline)
  (display "1 2 3 4" outp) (newline outp) (flush-output outp)
  (display (read-line in-p)) (newline)
  (display "
In other words, people like this treat the tools they use as, well, 
tools: they are good in so far as they are useful to the task at hand, 
and different tools are useful for different tasks.

Now, pause for a minute and consider whether people are actually like 
this. On the whole they are not.  In fact, for most people, most of the 
time, the aim is to put as many obstacles in the way of *getting stuff 
done* as possible.
EOM" outp) (newline outp) (flush-output outp)
  (map (lambda (l) (display l) (newline))
       (let loop ()
         (let ((l (read-line in-p)))
           (cond ((eof-object? l) '())
                 ((string=? l "EOM") '())
                 (else (cons l (loop)))))))
  (close-input-port in-p)
  (close-output-port outp)
  (close-input-port errp)
  (subprocess-wait proc))

However if I change the subprocess call to:
              (subprocess #f #f #f "/bin/cat" "-n")

then it blocks at the first read-line.

It's not just the presence of the extra command line argument,
either, because
              (subprocess #f #f #f "/bin/cat" "-")
does work.

Needless to say, my real task doesn't involve cat, and the
possibility must exist that I've done something wrong there,
too.

Thoughts or suggestions?

Cheers,

-- 
Andrew


Posted on the users mailing list.