有了下面的小程序,我期待以下内容:
>> forget_me!^C
>> next
next
>>
但它确实是:
>> forget_me!^C
>> next
forget_me!next
>>
不应该默认的SIGINT处理程序清除缓冲区,如http://sunsite.ualberta.ca/Documentation/Gnu/readline-4.1/html_node/readline_40.html#SEC40(rl_free_line_state)所述?
如何达到预期效果?
#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;
}