feat(here_doc): handle signals (^C, ^\) correctly

This is a bit of a big commit, because I had to move some stuff to other files,
sorry about that. I can split it up if it's too big to review.
This commit is contained in:
Khaïs COLIN 2025-04-17 09:44:04 +02:00
parent 5e6d7b3b4e
commit f1a0af09f8
15 changed files with 269 additions and 126 deletions

View file

@ -6,15 +6,13 @@
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/20 10:26:05 by jguelen #+# #+# */
/* Updated: 2025/04/16 13:06:40 by khais ### ########.fr */
/* Updated: 2025/04/17 12:05:07 by khais ### ########.fr */
/* */
/* ************************************************************************** */
// stdio must be included before readline
#include <stdio.h>
#include "readline/readline.h"
#include "sig.h"
#include "signal.h"
#include "sig_handlers.h"
/*
** g_signum exists to store the value of a signal if relevant for later
@ -24,37 +22,6 @@
*/
int g_signum = 0;
/*
** redisplay prompt
*/
static void sig_interactive(int signum)
{
if (signum == SIGINT)
{
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();
}
/*
** Stores the value of the caught signal in g_signum for later processing.
*/
static void sig_cmd(int signum, siginfo_t *siginfo, void *context)
{
(void)context;
(void)siginfo;
g_signum = signum;
}
/*
** Catches SIGINT and SIGQUIT.
** Set to ignore SIGQUIT and catch SIGINT to set g_signum for
@ -97,3 +64,26 @@ int set_exec_mode_sig_handling(void)
return (-1);
return (0);
}
/*
** Set to ignore SIGINT and SIGQUIT signals if they are generated through
** a call to kill when the program is currently executing commands.
** Otherwise set g_signum to the number corresponding to the caught signal.
*/
int set_here_doc_mode_sig_handling(void)
{
struct sigaction sig_act;
ft_bzero(&sig_act, sizeof(struct sigaction));
if (sigemptyset(&sig_act.sa_mask) == -1)
return (-1);
sig_act.sa_sigaction = &sig_cmd;
sig_act.sa_flags |= SA_SIGINFO;
if (sigaction(SIGINT, &sig_act, NULL) == -1)
return (-1);
sig_act.sa_handler = SIG_IGN;
sig_act.sa_flags = 0;
if (sigaction(SIGQUIT, &sig_act, NULL) == -1)
return (-1);
return (0);
}