Skip to content

Commit

Permalink
* src/augtool.c (main_loop): re-open rl_outstream/stdout only when st…
Browse files Browse the repository at this point in the history
…dout isn't

  a tty (fixes -e -i); use /dev/tty instead of /dev/stdout when re-opening to
  prevent permission errors

Fixes ticket hercules-team#241
  • Loading branch information
Dominic Cleal committed Jan 2, 2012
1 parent d3c8609 commit 4e000dc
Showing 1 changed file with 28 additions and 19 deletions.
47 changes: 28 additions & 19 deletions src/augtool.c
Expand Up @@ -423,8 +423,6 @@ static int main_loop(void) {
bool end_reached = false;
bool get_line = true;
bool in_interactive = false;
// make readline silent by default
rl_outstream = fopen("/dev/null", "w");

if (inputfile) {
if (freopen(inputfile, "r", stdin) == NULL) {
Expand All @@ -437,10 +435,12 @@ static int main_loop(void) {
}
}

// make readline silent by default
echo = echo || isatty(fileno(stdin));

if (echo)
rl_outstream = NULL;
else
rl_outstream = fopen("/dev/null", "w");

while(1) {
if (get_line) {
Expand All @@ -451,22 +451,31 @@ static int main_loop(void) {

if (line == NULL) {
if (!isatty(fileno(stdin)) && interactive && !in_interactive) {
in_interactive = true;
echo = true;
// reopen in and out streams
if ((rl_instream = fopen("/dev/tty", "r")) == NULL) {
perror("Failed to open terminal for reading");
return -1;
}
if (rl_outstream != NULL) {
fclose(rl_outstream);
rl_outstream = NULL;
}
if ((rl_outstream = fopen("/dev/stdout", "w")) == NULL) {
perror("Failed to reopen stdout");
return -1;
}
continue;
in_interactive = true;
if (echo)
printf("\n");
echo = true;

// reopen in stream
if ((rl_instream = fopen("/dev/tty", "r")) == NULL) {
perror("Failed to open terminal for reading");
return -1;
}

// reopen stdout and stream to a tty if originally silenced or
// not connected to a tty, for full interactive mode
if (rl_outstream == NULL || !isatty(fileno(rl_outstream))) {
if (rl_outstream != NULL) {
fclose(rl_outstream);
rl_outstream = NULL;
}
if (freopen("/dev/tty", "w", stdout) == NULL) {
perror("Failed to reopen stdout");
return -1;
}
rl_outstream = stdout;
}
continue;
}

if (auto_save) {
Expand Down

0 comments on commit 4e000dc

Please sign in to comment.