Skip to Content
Exploring Expect
book

Exploring Expect

by Don Libes
December 1994
Intermediate to advanced
606 pages
16h 7m
English
O'Reilly Media, Inc.
Content preview from Exploring Expect

Expecting From Streams

Both exp_expectl and exp_expectv have analogous functions that work on streams instead of file descriptors. The stream functions have the same names as their file counterparts except that the stream functions have an "f" in the name. This is similar to the distinction between write and fwrite. Both of the stream versions are identical to their file counterparts except that the first argument is a stream instead of a file descriptor.

exp_fexpectl is the stream version of exp_expectl. A simple example looks like this:

FILE *fp = exp_popen("telnet");
exp_fexpectl(fp, exp_glob, "prompt*", 1, exp_end);

On some systems, the stream versions of the expect functions are much slower than the file descriptor versions because there is no way to portably read an unknown number of bytes without the potential of timing out. Thus, characters are read one at a time. While automated versions of interactive programs do not usually demand high speed, the file descriptor functions are likely to be more efficient on all systems.

You can get the best of both worlds, writing with the usual stream functions (i.e., fprintf) and reading with the file descriptor versions of expect, as long as you do not attempt to intermix other stream input functions (e.g., fgetc). To do this, pass "fileno(stream)" as the file descriptor to exp_expectl or exp_expectv. Fortunately, there is little reason to use anything but the expect functions when reading from interactive programs.

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

AI Agents in Action

AI Agents in Action

Micheal Lanham
Learning Go

Learning Go

Jon Bodner

Publisher Resources

ISBN: 9781565920903Supplemental ContentErrata Page