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 ...