I'm writing a shell-like program in C using GNU readline and have encountered a bug related to prompt handling.
Brief Description of the Issue
In my program, after exiting a heredoc with ^D and then pressing ^C at the main prompt, an extra prompt appears unexpectedly.
$ <<a # Start of heredoc
> # User inputs ^D to exit heredoc
$ ^C # User inputs ^C at the main prompt
$ # An extra prompt appears unexpectedly
$
Reproducing the Issue
The code snippet below is a simplified version and doesn't exactly replicate my entire shell program, but it demonstrates a similar issue with prompt handling.
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <readline/readline.h>
volatile sig_atomic_t g_sig;
void handle_sigint(int sig) {
g_sig = sig;
}
int readline_hook(void) {
if (g_sig == SIGINT) {
rl_replace_line("", 0);
rl_on_new_line();
rl_redisplay();
rl_done = 1;
}
return 0;
}
int main(void) {
char *line;
signal(SIGINT, handle_sigint);
rl_event_hook = readline_hook;
while (1) {
g_sig = 0;
line = readline("$ ");
free(line);
}
}
Question
How can I resolve the issue where an extra prompt is displayed in readline? What do I need to do to fix it?
rl_on_new_linedisplays a prompt, butrl_on_new_line_with_promptdoesn't. tiswww.case.edu/php/chet/readline/…bashthe firsta<<causedbash: syntax error near unexpected token 'newline'. Each Ctrl-C emitted^C, newline, and prompt char.<<a, but even with this input, it seems to encounter the same problem of displaying unnecessary blank lines.bashtest sample in your Q to show the results you're getting there. .... This while problem could be from an interaction with stty (and if you dostty -a, you'll see that that code is managing a ton of parameters). .... As I mentioned, I getbash: syntax error near unexpected token 'newline'. Good luck.