'return readline to its original state when recieving SIGINT

In bash when a user hits ctrl-c (sending SIGINT) it takes back readline to its original state canceling search/vi-mode/... state.

I tried UNSETSTATE macro but it has no effect, actually even SETSTATE has no effect on the state of readline, however the rl_readline_state variable is changed.

In signal handler I tried:

RL_UNSETSTATE(RL_STATE_ISEARCH|RL_STATE_NSEARCH|RL_STATE_SEARCH|RL_STATE_VIMOTION|RL_STATE_NUMERICARG|RL_STATE_MULTIKEY);

I tried rl_redisplay() but nothing works. Keep in mind that I have:

rl_catch_signals = 0;

I have a handler for rl_getc_function.

Here is my code: https://gitlab.com/abellaismail/minishell/-/blob/dev/src/sig_handler.c



Solution 1:[1]

I knew it had to do with 42 in some way. Here's my repo for the project, if you want to look further into how exactly the solution I found works. Assuming your question applies when the prompt is waiting for a command and you press ctrl + C (when on heredoc or executing childs processes the behaviour is a bit different), I called the following function:

void    ft_signal_ctrl_c(int sig)
{
    (void)sig;
    write(2, "\n", 1);
    rl_replace_line("", 0);
    rl_on_new_line();
    rl_redisplay();
}

This succesfully goes back to the shell while (running) loop, showing a new prompt. errno should be updated separately too (ctrl+ C gives $? = 1).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 carce-bo