新的提示我有類似下面的代碼,使用的ReadLine:的Readline:得到SIGINT
#include <errno.h>
#include <error.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <readline/readline.h>
#include <readline/history.h>
void handle_signals(int signo) {
if (signo == SIGINT) {
printf("You pressed Ctrl+C\n");
}
}
int main (int argc, char **argv)
{
//printf("path is: %s\n", path_string);
char * input;
char * shell_prompt = "i-shell> ";
if (signal(SIGINT, handle_signals) == SIG_ERR) {
printf("failed to register interrupts with kernel\n");
}
//set up custom completer and associated data strucutres
setup_readline();
while (1)
{
input = readline(shell_prompt);
if (!input)
break;
add_history(input);
//do something with the code
execute_command(input);
}
return 0;
}
我知道了設置攔截SIGINT
(即用戶按下Ctrl+C
),所以我可以告訴信號處理器handle_signals()
正在工作。但是,當控制權返回到readline()
時,它將使用輸入前使用的同一行文本。我想要發生的是readline「取消」當前文本行,並給我一個新的行,很像BASH shell。像這樣的東西:
i-shell> bad_command^C
i-shell> _
任何機會得到這個工作?我讀過的郵件列表上的某些內容使用longjmp(2)
提到,但這看起來確實不是一個好主意。
您不能從信號處理程序安全地調用'printf'。 – pat