fix: heredoc work without global

This commit is contained in:
Camille Chauvet 2023-04-17 12:16:41 +00:00
parent 9ab9c06593
commit 3e7dcd1ba9
5 changed files with 64 additions and 72 deletions

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 14:18:21 by cchauvet #+# #+# */
/* Updated: 2023/04/17 10:41:23 by cchauvet ### ########.fr */
/* Updated: 2023/04/17 11:57:07 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -69,5 +69,5 @@ void ft_cmdwaiter(void *ptr)
*ft_get_exit_code() = WEXITSTATUS(exit_status);
}
signal(SIGINT, ft_ctrlc);
signal(SIGQUIT, ft_quit);
signal(SIGQUIT, SIG_IGN);
}

View File

@ -6,12 +6,14 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/29 17:32:06 by cchauvet #+# #+# */
/* Updated: 2023/04/07 15:06:39 by alouis-j ### ########.fr */
/* Updated: 2023/04/17 11:15:38 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "./redirection_private.h"
#include <stdbool.h>
#include "../signal/signal.h"
#include <signal.h>
static bool ft_check_heredoc(t_data *data, t_cmd *cmd,
char *redirection_identifier, char *redirection)
@ -22,7 +24,9 @@ static bool ft_check_heredoc(t_data *data, t_cmd *cmd,
{
if (cmd->fd_in[0] == -2)
return (0);
signal(SIGINT, ft_ctrlc_heredoc);
fd = ft_heredoc(data, redirection);
signal(SIGINT, ft_ctrlc);
if (fd == -2)
return (1);
else

View File

@ -6,17 +6,23 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/17 15:36:26 by cchauvet #+# #+# */
/* Updated: 2023/04/07 15:11:17 by alouis-j ### ########.fr */
/* Updated: 2023/04/17 12:14:11 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "./redirection_private.h"
#include <readline/readline.h>
#include <unistd.h>
int *ft_get_heredoc(void)
static bool ft_fd_is_closed(int fd)
{
static int heredoc = -1;
int fd2;
return (&heredoc);
fd2 = dup(fd);
if (fd2 == -1)
return (1);
close(fd2);
return (0);
}
static int ft_format_and_write(t_data *data, const char *str, int fd)
@ -31,75 +37,57 @@ static int ft_format_and_write(t_data *data, const char *str, int fd)
return (0);
}
static int ft_heredoc3(char *stop, int fds[2])
int ft_heredoc2(t_data *data, char *line, char *stop, int fds[2])
{
if (*ft_get_heredoc() == -1)
if (line == NULL)
{
ft_putchar_fd('\n', 1);
ft_closer(fds);
if (ft_fd_is_closed(0))
{
close(fds[0]);
fds[0] = -2;
return (1);
}
else
{
ft_eprintf("bozoshell: warning: here-document at line");
ft_eprintf("1 delimited by end-of-file (wanted `%s')\n", stop);
return (2);
}
return (0);
}
static int ft_heredoc2(t_data *data, char *stop, int fds[2])
{
char *line;
int out;
ft_printf("> ");
line = get_next_line(*ft_get_heredoc());
if (line == NULL)
{
out = ft_heredoc3(stop, fds);
if (out)
return (out);
}
line[ft_strlen(line) - 1] = '\0';
if (ft_strcmp(line, stop) == 0)
{
free(line);
return (2);
}
if (ft_format_and_write(data, line, fds[1]))
{
ft_closer(fds);
free(line);
ft_eprintf("\nbozoshell: warning: here-document at line 1%s",
"delimited by end-of-file (wanted `adfsd')\n");
return (1);
}
}
if (ft_strcmp(line, stop) == 0)
return (1);
if (ft_format_and_write(data, line, fds[1]))
{
close(fds[0]);
fds[0] = -2;
return (1);
}
free(line);
return (0);
}
int ft_heredoc(t_data *data, char *stop)
{
int fds[2];
int return_code;
int stdin_bak;
char *line;
if (pipe(fds) == -1)
return (-1);
*ft_get_heredoc() = dup(0);
while (true)
stdin_bak = dup(0);
if (stdin_bak == -1)
return (1);
if (pipe(fds))
{
return_code = ft_heredoc2(data, stop, fds);
if (return_code == 2)
break ;
else if (return_code == 1)
close(stdin_bak);
return (1);
}
line = readline("> ");
while (ft_heredoc2(data, line, stop, fds) == 0)
{
if (*ft_get_heredoc() > 2)
close(*ft_get_heredoc());
*ft_get_heredoc() = -1;
return (-2);
free(line);
line = readline("> ");
}
}
close(*ft_get_heredoc());
*ft_get_heredoc() = -1;
free(line);
close(fds[1]);
dup2(stdin_bak, 0);
close(stdin_bak);
return (fds[0]);
}

View File

@ -6,26 +6,25 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/28 15:59:01 by cchauvet #+# #+# */
/* Updated: 2023/04/17 10:39:36 by cchauvet ### ########.fr */
/* Updated: 2023/04/17 11:23:21 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "signal_private.h"
void ft_ctrlc(int num)
void ft_ctrlc_heredoc(int num)
{
if (*ft_get_heredoc() != -1)
{
close(*ft_get_heredoc());
*ft_get_heredoc() = -1;
close(0);
ft_putchar_fd('\n', 1);
(void) num;
}
else
void ft_ctrlc(int num)
{
rl_replace_line("", 0);
rl_on_new_line();
ft_putchar_fd('\n', 1);
rl_redisplay();
}
*ft_get_exit_code() = 130;
(void) num;
}

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/28 16:02:31 by cchauvet #+# #+# */
/* Updated: 2023/03/28 16:03:11 by cchauvet ### ########.fr */
/* Updated: 2023/04/17 11:14:28 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,5 +15,6 @@
void ft_quit(int num);
void ft_ctrlc(int num);
void ft_ctrlc_heredoc(int num);
#endif