[Haskell-cafe] Expect module?

Donn Cave donn at avvanta.com
Mon Jan 19 12:23:40 EST 2009

Quoth Neil Mitchell <ndmitchell at gmail.com>:

>> Is there a Haskell-Expect module? Something that would allow me to
>> control an external Unix program via its stdin/stdout/stderr?
> System.Process does what you want, I think:

It might not.  Expect uses pseudottys (cf. openpty()), and select().

System.Process supports what you might think of as the "naive" model,
using pipes for the I/O device and making some simplifying assumptions
about the behavior of the external program.  It's more portable and
works for many common cases, at least the popen() and system() usages
that probably account for 98 percent of process invocations.

But once you need output from a process _before_ it exits, you will
encounter the problem with pipes:  output to a pipe is normally block
buffered, so it doesn't reliably get to the pipe on time.  The C I/O
library treats a pty device like a tty and line buffers output.
(But unfortunately, a pty device is not just a pipe with that single
property - I wouldn't replace pipes with ptys just as a matter of course,
because depending on the OS they may do things like discard date on
overflow, or they may be a severely limited system resource.)

select() also helps with another potential problem, when the same
process is writing to two or more pipes, which are fixed size devices.
In GHC, I suppose the potential deadlock might similarly be avoided
using threads.  I don't know if GHC supports openpty() - having a
little trouble getting data from its web site this morning.  But of
course there's more to Expect than just the raw system calls.


More information about the Haskell-Cafe mailing list