拥有下面的小程序,我期望以下内容:
>> 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;
}