Reputation: 9893
I've written a small C program that uses readline("")
to read input from stdin. Rather than interacting with it from the terminal, I've written a test harness which executes the C program and gives it input:
test harness --stdout--> | --stdin--> C program
^------stdin--- | <--stdout-----/
This test harness is receiving an echo of its output, even though the program itself. Why is readline() echoing its input, and can I disable this? Or should I not be using readline() in a non-interactive environment?
Upvotes: 3
Views: 1968
Reputation: 273
By default Readline echos user input to stdout, however it is configurable.
From readline documentation:
Variable: FILE * rl_outstream
The stdio stream to which Readline performs output.
Just set rl_outstream
to stderr
in beginning of your program, so it will echoing to stderr instead; For example:
#include <readline/readline.h>
#include <stdio.h>
...
int main() {
rl_outstream = stderr;
...
In fact bash(1) does this too.
And I also agree that if your program isn't designed to be used interactively, your should use another simpler way to get lines from stdin, such as getline(3), fgets(3) or getchar(3).
Upvotes: 2
Reputation:
The readline library is solely intended for interactive use. Your program should probably fall back to using fgets()
if isatty(STDIN_FILENO)
is false.
Upvotes: 5