I am searching for a C interface for bash shells. I.e. I would like to have a set of functions which allow me to open a session, execute commands, return the output (STDOUT,STDERR) and finally to close the shell. It could be a library or C source code based on standard libraries.
The general root problem seems to be how to programmatically run interactive terminal program.
Now this would in my part require actual testing, but you would roughly need to
stdin
, stdout
, and stderr
(the parent process writing to stdin_pipe
and reading stdout_pipe
and stderr_pipe
) using pipe(2)
system call;dup2(2)
;execve(2)
/ execv(3)
) your interactive shell;stdin_pipe
and reading the errors and responses from the other two pipes.(If you do not need to make the distinction between stdout
and stderr
you could just simplify your life by using popen(3)
- you could probably redirect stderr
to stdout
by proper choice of command string).
For properly working solution, however, I believe you probably would need to use pseudo ttys (pty(7)
) by calling forkpty(3)
instead of just fork.
As it starts to get more and more complicated to take into account all the nyances of dealing with pseudo terminals, why not search for C expect library which should be able to do all this for you. Or emulate how expect or some other language equivalent like pexpect
is implemented. Actually expect
seems to provide a C library called libexpect(3)
for you so that you do not need to write tcl/tk for programming the interaction. I am not personally familiar with the library, and there could be other better ones.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With