0

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?

5
  • Although the names are terrible, it looks like rl_on_new_line displays a prompt, but rl_on_new_line_with_prompt doesn't. tiswww.case.edu/php/chet/readline/… Commented Nov 30, 2023 at 13:36
  • 1
    I did you test case in a new(ish) ksh93 had the same results. Further "testing" showed that each Ctrl-C forced a new line and prompt character in the terminal. Interesting.y, switching to bash the first a<< caused bash: syntax error near unexpected token 'newline'. Each Ctrl-C emitted ^C, newline, and prompt char. Commented Nov 30, 2023 at 14:05
  • @stark When using rl_on_new_line_with_prompt, indeed, there is no prompt displayed, but instead, an issue arises where a blank line is shown. Commented Dec 1, 2023 at 4:12
  • @shellter It appears that the input to test in bash should indeed be <<a, but even with this input, it seems to encounter the same problem of displaying unnecessary blank lines. Commented Dec 1, 2023 at 4:21
  • My point is that this is expected behavior in a shell. Did an instructor point this out as an issue that you should correct? Be sure this is worth the time you are spending on it. (Good learning, but hard to see the practical impact). ALSO please add a bash test 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 do stty -a , you'll see that that code is managing a ton of parameters). .... As I mentioned, I get bash: syntax error near unexpected token 'newline' . Good luck. Commented Dec 1, 2023 at 4:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.