fix: heredoc signal

This commit is contained in:
Camille Chauvet 2023-04-05 14:13:56 +02:00
parent fe535a21f0
commit 404fcaa15f
4 changed files with 34 additions and 12 deletions

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/29 17:32:06 by cchauvet #+# #+# */ /* Created: 2023/03/29 17:32:06 by cchauvet #+# #+# */
/* Updated: 2023/04/04 14:58:29 by alouis-j ### ########.fr */ /* Updated: 2023/04/05 14:12:02 by alouis-j ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,6 +19,8 @@ static bool ft_check_heredoc(t_data *data, t_cmd *cmd,
if (ft_strcmp(redirection_identifier, "<<") == 0) if (ft_strcmp(redirection_identifier, "<<") == 0)
{ {
if (cmd->fd_in[0] == -2)
return (0);
fd = ft_heredoc(data, redirection); fd = ft_heredoc(data, redirection);
if (fd == -2) if (fd == -2)
return (1); return (1);
@ -39,6 +41,8 @@ static bool ft_check_infile(t_data *data, t_cmd *cmd,
if (ft_strcmp(redirection_identifier, "<") == 0) if (ft_strcmp(redirection_identifier, "<") == 0)
{ {
if (cmd->fd_in[0] == -2)
return (0);
if (ft_file_is_readable(data, redirection)) if (ft_file_is_readable(data, redirection))
{ {
fd = open(redirection, O_RDONLY); fd = open(redirection, O_RDONLY);
@ -51,7 +55,7 @@ static bool ft_check_infile(t_data *data, t_cmd *cmd,
if (cmd->fd_in[0] > 2) if (cmd->fd_in[0] > 2)
close(cmd->fd_in[0]); close(cmd->fd_in[0]);
cmd->fd_in[0] = -2; cmd->fd_in[0] = -2;
return (1); return (0);
} }
} }
return (0); return (0);
@ -64,6 +68,8 @@ static bool ft_check_outfile(t_data *data, t_cmd *cmd,
if (ft_strcmp(redirection_identifier, ">") == 0) if (ft_strcmp(redirection_identifier, ">") == 0)
{ {
if (cmd->fd_out[0] == -2)
return (0);
if (ft_file_is_writable(data, redirection)) if (ft_file_is_writable(data, redirection))
{ {
fd = open(redirection, fd = open(redirection,
@ -77,7 +83,7 @@ static bool ft_check_outfile(t_data *data, t_cmd *cmd,
if (cmd->fd_out[0] > 2) if (cmd->fd_out[0] > 2)
close(cmd->fd_out[0]); close(cmd->fd_out[0]);
cmd->fd_out[0] = -2; cmd->fd_out[0] = -2;
return (1); return (0);
} }
} }
return (0); return (0);
@ -90,6 +96,8 @@ static bool ft_check_outfile_append(t_data *data, t_cmd *cmd,
if (ft_strcmp(redirection_identifier, ">>") == 0) if (ft_strcmp(redirection_identifier, ">>") == 0)
{ {
if (cmd->fd_out[0] == -2)
return (0);
if (ft_file_is_writable(data, redirection)) if (ft_file_is_writable(data, redirection))
{ {
fd = open(redirection, fd = open(redirection,
@ -103,7 +111,6 @@ static bool ft_check_outfile_append(t_data *data, t_cmd *cmd,
if (cmd->fd_out[0] > 2) if (cmd->fd_out[0] > 2)
close(cmd->fd_out[0]); close(cmd->fd_out[0]);
cmd->fd_out[0] = -2; cmd->fd_out[0] = -2;
return (1);
} }
} }
return (0); return (0);
@ -131,6 +138,10 @@ bool ft_check_redirection(t_data *data, t_cmd *cmd,
|| ft_check_outfile(data, cmd, redirection_identifier, str) || ft_check_outfile(data, cmd, redirection_identifier, str)
|| ft_check_outfile_append(data, cmd, redirection_identifier, || ft_check_outfile_append(data, cmd, redirection_identifier,
str)) str))
{
free(str);
return (1);
}
; ;
free(str); free(str);
return (0); return (0);

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/17 15:36:26 by cchauvet #+# #+# */ /* Created: 2023/02/17 15:36:26 by cchauvet #+# #+# */
/* Updated: 2023/03/31 18:49:06 by alouis-j ### ########.fr */ /* Updated: 2023/04/05 14:07:10 by alouis-j ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -39,9 +39,18 @@ static int ft_heredoc2(t_data *data, char *stop, int fds[2])
line = get_next_line(*ft_get_heredoc()); line = get_next_line(*ft_get_heredoc());
if (line == NULL) if (line == NULL)
{ {
if (*ft_get_heredoc() == -1)
{
ft_putchar_fd('\n', 1);
ft_closer(fds); ft_closer(fds);
return (1); return (1);
} }
else
{
ft_eprintf("minishell: warning: here-document at line 1 delimited by end-of-file (wanted `%s')\n", stop);
return (2);
}
}
line[ft_strlen(line) - 1] = '\0'; line[ft_strlen(line) - 1] = '\0';
if (ft_strcmp(line, stop) == 0) if (ft_strcmp(line, stop) == 0)
{ {
@ -71,7 +80,7 @@ int ft_heredoc(t_data *data, char *stop)
return_code = ft_heredoc2(data, stop, fds); return_code = ft_heredoc2(data, stop, fds);
if (return_code == 2) if (return_code == 2)
break ; break ;
else if (return_code) else if (return_code == 1)
{ {
close(*ft_get_heredoc()); close(*ft_get_heredoc());
*ft_get_heredoc() = -1; *ft_get_heredoc() = -1;

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/27 13:44:22 by cchauvet #+# #+# */ /* Created: 2023/03/27 13:44:22 by cchauvet #+# #+# */
/* Updated: 2023/04/04 13:56:47 by alouis-j ### ########.fr */ /* Updated: 2023/04/05 14:00:52 by alouis-j ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -78,7 +78,8 @@ int ft_set_redirection(t_data *data, t_cmd *cmd, char **tab)
i = 0; i = 0;
while (tab[i + 1] != NULL) while (tab[i + 1] != NULL)
{ {
ft_check_redirection(data, cmd, tab[i], tab[i + 1]); if (ft_check_redirection(data, cmd, tab[i], tab[i + 1]))
return (1);
i++; i++;
} }
if (ft_is_in("<>", tab[i][0])) if (ft_is_in("<>", tab[i][0]))

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/28 15:59:01 by cchauvet #+# #+# */ /* Created: 2023/03/28 15:59:01 by cchauvet #+# #+# */
/* Updated: 2023/03/28 16:09:37 by cchauvet ### ########.fr */ /* Updated: 2023/04/05 14:09:39 by alouis-j ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -30,6 +30,7 @@ void ft_ctrlc(int num)
data->exit_code = 130; data->exit_code = 130;
if (*ft_get_heredoc() != -1) if (*ft_get_heredoc() != -1)
{ {
if (*ft_get_heredoc() > 2)
close(*ft_get_heredoc()); close(*ft_get_heredoc());
*ft_get_heredoc() = -1; *ft_get_heredoc() = -1;
} }