Redirection of interactive program's stdin and stdout

Chin Wu chin at sg1.chem.upenn.edu
Thu Jun 27 13:28:28 AEST 1991


Can anyone show me how to redirect interactive program's stdin and
stdout? The purpose is to write a program acting intermediately
between an interactive and the user. I want to capture the output of
the interactive program so that I can parse it. Redirecting the stdin
so that I can send anything to the program when it is expecting a user
input. To do this, I am thinking using a pipe. First, I create a pipe,
and fork a subprocess and then duplicate a file descripter for the
stdin and stdout. The program looks something like:
{
    pipe(fd);
    inchannel = fd[0]; forkout = fd[1];
    pipe(fd);
    outchannel = fd[1]; forkin = fd[0];
    pid = fork();
    if (!pid)  /* child */
	{
	dup2(forkin, 0);
	dup2(forkout, 1);
	close(forkin); close(forkout);
	exec("Some interactive program", argv);
	_exit(0);
	}
/* parent */
    number = read(inchannel, buffer, 100);
/* parse the result */
    write(outchannel, buffer, sizeof(buffer));
}
    This idea doesn't seem to work since the read() in the parent part
will be suspended until the exit of the child. But since this is an
interactive program, that will never happen. Conceptually it must be
possible because the terminal emulation program out there should be
able to intercept the output from the program to parse the results.
So does anyone have done this before can show me how to do it?

--
Chin Wu
chin at sg1.chem.upenn.edu



More information about the Comp.unix.wizards mailing list