[comp.unix.wizards] Talking to programs

jonas@Neon.Stanford.EDU (jonas karlsson) (08/04/90)

How do  i call a program from a function, have the function respond to 
the various prompts, give the correct replies, and catch the output?
Someone suggested using socketpairs, fork off a child that dup2's standard in 
and out into one half of the socketpairs, and then execv'ing the program
(basically).  I tried following his suggestions, with no success. 
could someone please give me a detailed description (or code) on
how to do this?

tnx
-j

brnstnd@kramden.acf.nyu.edu (Dan Bernstein) (08/19/90)

In article <1990Aug3.213540.14476@Neon.Stanford.EDU> jonas@Neon.Stanford.EDU (jonas karlsson) writes:
> How do  i call a program from a function, have the function respond to 
> the various prompts, give the correct replies, and catch the output?

You have two problems. One is that most programs want to talk directly
to a terminal, not to a pipe or file or socket. To solve this problem,
you could pick up my pty program (ftp site down for maintenance; ask me
for a copy of the c.s.unix submission). ``pty foo'' handles redirection
where ``foo'' cannot.

The other is that pipes are one-way, while you want two-way
communication. If you have named pipes, here's one easy solution:

  (umask 077;mknod input p;mknod output p)   # on this Sun, anyway
  pty program args < input | tee record > output
  # now read prompts from output and write replies into input
  # record contains a transcript

Otherwise you'll have to write some code to stick normal pipes in front
of and in back of the program. You can skip this coding by picking up
``expect'' from Don Libes; it also does the necessary pseudo-terminal
work, though pty is more flexible. ``expect'' is available via anonymous
ftp to 129.6.32.4, if I remember right.

---Dan