The Linux Programming Interface

(nextflipdebug5) #1
Pseudoterminals 1399

One problem with the above scenario is that, by default, the stdio package flushes
standard output only when the stdio buffer is filled. This means that the output from
the longrunner program will appear in bursts separated by long intervals of time. One
way to circumvent this problem is to write a program that does the following:
a) Create a pseudoterminal.
b) Exec the program named in its command-line arguments with the standard
file descriptors connected to the pseudoterminal slave.
c) Read output from the pseudoterminal master and write it immediately to
standard output (STDOUT_FILENO, file descriptor 1), and, at the same time,
read input from the terminal and write it to the pseudoterminal master, so
that it can be read by the execed program.
Such a program, which we’ll call unbuffer, would be used as follows:

$ ./unbuffer longrunner | grep str

Write the unbuffer program. (Much of the code for this program will be similar to
that of Listing 64-3.)

64-8. Write a program that implements a scripting language that can be used to drive vi
in a noninteractive mode. Since vi expects to be run from a terminal, the program
will need to employ a pseudoterminal.

Free download pdf