0

I'm making a project in C++ and was tasked with replacing the std::getline with the more capable readline() function from the readline.h library since it conatins more functionalitites and will be more useful overall. However the program I am executing makes multiple tests with other "in-X" files and generates "out-X" files and checks output with result for testing.

The testing goes basically like this

./a.exe < in-X.txt > out-X.txt

The problem I'm having is the readline() function seems to print to the output file the line it read from the input, so basically all my "out-X" files generated contain all the input plus the expected result and that is very troublesome.

I've already tried many combinations of rl_redisplay() and using rl_delete_text() to no solution. I've also tried using terminal management functions such as rl_prep_terminal(1) to raw mode and rl_deprep_terminal() and nothing seems to work. I also couldn't try rl_tty_set_echoing() because my program doesn't seem to be able to find it in the library.

I've tried to minimize the issue as much as possible and came up with this simplified code

/* Standard include files. stdio.h is required. */
#include <string.h>
#include <iostream>
#include <stdlib.h>

/* Standard readline include files. */
#include <readline/readline.h>
#include <readline/history.h>

int main(int argc, char* argv[]) {
  char *buf;
  while ((buf = readline("Prompt>")) != nullptr) {
    std::string line(buf);
    if (buf)
      add_history(buf);


    std::cout << "Line : " << line << "\n";
  }

  return 0;
}

Which is very straightforward, printing the prompt of readline() is not an issue as it is expected on the output, but printing the "buf" that was read is.

Basically with the in file

a = 2
b = 3
1 + 2 + 3

I would expect the output

Line : a = 2
Line : b = 3
Line : 1 + 2 + 3

But I get

Prompt> a = 2
Line : a = 2
Prompt> b = 3
Line : b = 3
Prompt> 1 + 2 + 3
Line : 1 + 2 + 3
Prompt> 

The last "Prompt>" that contains the EOF is something I can work around or live with, but printing the 1st 3rd and 5th lines I cannot.

  • A [mcve] would be nice... (if not required). – Scheff's Cat May 10 '19 at 11:38
  • At least show the code you wrote :) – Gar May 10 '19 at 11:50
  • I've updated to include the part where I actually use the readline, but the program is an interpreter and very long. I do make calls for the std::cout to print the desired output, not sure if that is a problem though as readline() should function with it. – Túlio Martins May 10 '19 at 12:18
  • There's probably an error in `...` portion of the code. You'll need to minimize it to reproduce the error, and doing that will probably make it obvious. – William Pursell May 10 '19 at 12:31
  • I made a minimal, complete and verifiable example to try to illustrate. I know the problem seems simple that way, but I can't get my head around the fix. – Túlio Martins May 10 '19 at 12:48
  • 1
    Possible duplicate of [GNU readline: avoid prompt string in output if input is not interactive](https://stackoverflow.com/questions/2406826/gnu-readline-avoid-prompt-string-in-output-if-input-is-not-interactive) –  May 10 '19 at 13:09
  • Thank you! Not what I expected, but managed to get me to work around the issue – Túlio Martins May 14 '19 at 20:33

0 Answers0