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

Allocating Your Own Pty

By default, a pty is automatically allocated each time a process is spawned. It is possible to allocate a pty through some other mechanism (of your own). Conceivably, you could also use a pair of fifos or something similar even though it may not completely emulate tty functionality.

Two variables control pty allocation. They are:

extern int exp_autoallocpty;
extern int exp_pty[2];

The variable exp_autoallocpty is set to one by default. If you set it to zero, a pty is not automatically allocated by the spawn functions. Instead, the value of exp_pty[0] is used as the master pty file descriptor, and the value of exp_pty[1] is used as the slave pty file descriptor.

The following illustrates pty allocation with the pipe system call. (On traditional UNIX systems, a pipe is a one-way device, so this example is not suitable for most Expect applications. Nonetheless, it serves to demonstrate the calling protocol.) The first statement turns off the automatic pty allocation. The second statement uses the pipe system call which conveniently produces two connected file descriptors in the exp_pty array. The exp_popen creates a cat process and uses the two file descriptors in the exp_pty array.

exp_autoallocpty = 0;
pipe(exp_pty);
exp_popen("cat");

When you allocate your own pty, you must also initialize it. The spawn functions do none of the usual pty initializations (e.g., exp_stty_init is not used).

After the new process is created, the slave pty file descriptor is closed ...

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