signal: handle redisplaying the prompt correctly

This commit is contained in:
Khaïs COLIN 2025-04-04 16:55:21 +02:00
parent 3a309062d8
commit 10e8738336
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
3 changed files with 28 additions and 13 deletions

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/06 13:44:06 by kcolin #+# #+# */ /* Created: 2025/02/06 13:44:06 by kcolin #+# #+# */
/* Updated: 2025/04/15 11:53:29 by khais ### ########.fr */ /* Updated: 2025/04/15 11:53:49 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -32,8 +32,10 @@
*/ */
static void execute_command(t_simple_cmd *cmd, t_minishell *app) static void execute_command(t_simple_cmd *cmd, t_minishell *app)
{ {
set_exec_mode_sig_handling();
simple_cmd_execute(cmd, app); simple_cmd_execute(cmd, app);
simple_cmd_destroy(cmd); simple_cmd_destroy(cmd);
set_interactive_mode_sig_handling();
} }
/* /*
@ -95,6 +97,7 @@ int main(int argc, char *argv[], char **envp)
/* { */ /* { */
/* ft_printf("\n"); */ /* ft_printf("\n"); */
/* g_signum = -1; */ /* g_signum = -1; */
/* readline_reset(); */
/* } */ /* } */
/* line = get_command(); */ /* line = get_command(); */
/* } */ /* } */

View file

@ -6,10 +6,13 @@
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */ /* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/20 10:26:05 by jguelen #+# #+# */ /* Created: 2025/02/20 10:26:05 by jguelen #+# #+# */
/* Updated: 2025/04/04 16:56:06 by khais ### ########.fr */ /* Updated: 2025/04/04 16:58:58 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
// stdio must be included before readline
#include <stdio.h>
#include "readline/readline.h"
#include "sig.h" #include "sig.h"
#include "signal.h" #include "signal.h"
@ -22,24 +25,31 @@
int g_signum = 0; int g_signum = 0;
/* /*
** Stores the signal number corresponding to the caught signal into g_signum ** redisplay prompt
** for later processing.
*/ */
static void sig_interactive(int signum) static void sig_interactive(int signum)
{ {
g_signum = signum; (void)signum;
rl_replace_line("", 0);
ft_printf("\n");
rl_on_new_line();
rl_redisplay();
}
void readline_reset(void)
{
rl_replace_line("", 0);
ft_printf("\n");
rl_redisplay();
} }
/* /*
** Ignores the signal if it was sent by a kill command. ** Stores the value of the caught signal in g_signum for later processing.
** Otherwise stores the value of the caught signal in g_signum for later
** processing.
*/ */
static void ignoreif_sent_by_kill(int signum, siginfo_t *siginfo, void *context) static void sig_cmd(int signum, siginfo_t *siginfo, void *context)
{ {
(void)context; (void)context;
if (siginfo->si_code == SI_USER) (void)siginfo;
return ;
g_signum = signum; g_signum = signum;
} }
@ -77,8 +87,9 @@ int set_exec_mode_sig_handling(void)
ft_bzero(&sig_act, sizeof(struct sigaction)); ft_bzero(&sig_act, sizeof(struct sigaction));
if (sigemptyset(&sig_act.sa_mask) == -1) if (sigemptyset(&sig_act.sa_mask) == -1)
return (-1); return (-1);
sig_act.sa_sigaction = &ignoreif_sent_by_kill; sig_act.sa_sigaction = &sig_cmd;
sig_act.sa_flags |= SA_SIGINFO; sig_act.sa_flags |= SA_SIGINFO;
sig_act.sa_flags |= SA_RESTART;
if (sigaction(SIGINT, &sig_act, NULL) == -1) if (sigaction(SIGINT, &sig_act, NULL) == -1)
return (-1); return (-1);
if (sigaction(SIGQUIT, &sig_act, NULL) == -1) if (sigaction(SIGQUIT, &sig_act, NULL) == -1)

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/19 18:21:55 by jguelen #+# #+# */ /* Created: 2025/02/19 18:21:55 by jguelen #+# #+# */
/* Updated: 2025/04/04 16:58:12 by khais ### ########.fr */ /* Updated: 2025/04/04 16:58:49 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -31,5 +31,6 @@ extern int g_signum;
int set_interactive_mode_sig_handling(void); int set_interactive_mode_sig_handling(void);
int set_exec_mode_sig_handling(void); int set_exec_mode_sig_handling(void);
void readline_reset(void);
#endif #endif