libreadline SIGINT handler doesn't clear buffer

130 views Asked by At

Having the little program below I expect the following:

>> forget_me!^C
>> next
next
>>

But it really is:

>> forget_me!^C
>> next
forget_me!next
>>

Shouldn't the default SIGINT handler clear the buffer as described here http://sunsite.ualberta.ca/Documentation/Gnu/readline-4.1/html_node/readline_40.html#SEC40 (rl_free_line_state)?

How can I achieve the desired result?

#include <cstdlib>
#include <csignal>
#include <iostream>
#include <readline/readline.h>
#include <readline/history.h>

const std::string prompt{">> "};

void new_line_handler(int i) { std::cout << "\n" << prompt; }

int main() {
  struct sigaction action = {};
  action.sa_handler = new_line_handler;
  sigaction(SIGINT, &action, nullptr);

  char *input;
  while (true) {
    input = readline(prompt.c_str());
    if (!input)
      break;
    add_history(input);
    std::cout << input << "\n";
    free(input);
  }
  return 0;
}
0

There are 0 answers