fix: heredoc work without global
This commit is contained in:
parent
9ab9c06593
commit
3e7dcd1ba9
@ -6,7 +6,7 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/02/15 14:18:21 by cchauvet #+# #+# */
|
/* 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);
|
*ft_get_exit_code() = WEXITSTATUS(exit_status);
|
||||||
}
|
}
|
||||||
signal(SIGINT, ft_ctrlc);
|
signal(SIGINT, ft_ctrlc);
|
||||||
signal(SIGQUIT, ft_quit);
|
signal(SIGQUIT, SIG_IGN);
|
||||||
}
|
}
|
||||||
|
@ -6,12 +6,14 @@
|
|||||||
/* 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/07 15:06:39 by alouis-j ### ########.fr */
|
/* Updated: 2023/04/17 11:15:38 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "./redirection_private.h"
|
#include "./redirection_private.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include "../signal/signal.h"
|
||||||
|
#include <signal.h>
|
||||||
|
|
||||||
static bool ft_check_heredoc(t_data *data, t_cmd *cmd,
|
static bool ft_check_heredoc(t_data *data, t_cmd *cmd,
|
||||||
char *redirection_identifier, char *redirection)
|
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)
|
if (cmd->fd_in[0] == -2)
|
||||||
return (0);
|
return (0);
|
||||||
|
signal(SIGINT, ft_ctrlc_heredoc);
|
||||||
fd = ft_heredoc(data, redirection);
|
fd = ft_heredoc(data, redirection);
|
||||||
|
signal(SIGINT, ft_ctrlc);
|
||||||
if (fd == -2)
|
if (fd == -2)
|
||||||
return (1);
|
return (1);
|
||||||
else
|
else
|
||||||
|
@ -6,17 +6,23 @@
|
|||||||
/* 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/04/07 15:11:17 by alouis-j ### ########.fr */
|
/* Updated: 2023/04/17 12:14:11 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "./redirection_private.h"
|
#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)
|
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);
|
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);
|
if (ft_fd_is_closed(0))
|
||||||
ft_closer(fds);
|
{
|
||||||
|
close(fds[0]);
|
||||||
|
fds[0] = -2;
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ft_eprintf("bozoshell: warning: here-document at line");
|
ft_eprintf("\nbozoshell: warning: here-document at line 1%s",
|
||||||
ft_eprintf("1 delimited by end-of-file (wanted `%s')\n", stop);
|
"delimited by end-of-file (wanted `adfsd')\n");
|
||||||
return (2);
|
return (1);
|
||||||
}
|
}
|
||||||
return (0);
|
}
|
||||||
}
|
if (ft_strcmp(line, stop) == 0)
|
||||||
|
return (1);
|
||||||
static int ft_heredoc2(t_data *data, char *stop, int fds[2])
|
if (ft_format_and_write(data, line, fds[1]))
|
||||||
{
|
{
|
||||||
char *line;
|
close(fds[0]);
|
||||||
int out;
|
fds[0] = -2;
|
||||||
|
|
||||||
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);
|
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
free(line);
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ft_heredoc(t_data *data, char *stop)
|
int ft_heredoc(t_data *data, char *stop)
|
||||||
{
|
{
|
||||||
int fds[2];
|
int fds[2];
|
||||||
int return_code;
|
int stdin_bak;
|
||||||
|
char *line;
|
||||||
|
|
||||||
if (pipe(fds) == -1)
|
stdin_bak = dup(0);
|
||||||
return (-1);
|
if (stdin_bak == -1)
|
||||||
*ft_get_heredoc() = dup(0);
|
return (1);
|
||||||
while (true)
|
if (pipe(fds))
|
||||||
{
|
{
|
||||||
return_code = ft_heredoc2(data, stop, fds);
|
close(stdin_bak);
|
||||||
if (return_code == 2)
|
return (1);
|
||||||
break ;
|
}
|
||||||
else if (return_code == 1)
|
line = readline("> ");
|
||||||
|
while (ft_heredoc2(data, line, stop, fds) == 0)
|
||||||
{
|
{
|
||||||
if (*ft_get_heredoc() > 2)
|
free(line);
|
||||||
close(*ft_get_heredoc());
|
line = readline("> ");
|
||||||
*ft_get_heredoc() = -1;
|
|
||||||
return (-2);
|
|
||||||
}
|
}
|
||||||
}
|
free(line);
|
||||||
close(*ft_get_heredoc());
|
|
||||||
*ft_get_heredoc() = -1;
|
|
||||||
close(fds[1]);
|
close(fds[1]);
|
||||||
|
dup2(stdin_bak, 0);
|
||||||
|
close(stdin_bak);
|
||||||
return (fds[0]);
|
return (fds[0]);
|
||||||
}
|
}
|
||||||
|
@ -6,26 +6,25 @@
|
|||||||
/* 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/04/17 10:39:36 by cchauvet ### ########.fr */
|
/* Updated: 2023/04/17 11:23:21 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "signal_private.h"
|
#include "signal_private.h"
|
||||||
|
|
||||||
|
void ft_ctrlc_heredoc(int num)
|
||||||
|
{
|
||||||
|
close(0);
|
||||||
|
ft_putchar_fd('\n', 1);
|
||||||
|
(void) num;
|
||||||
|
}
|
||||||
|
|
||||||
void ft_ctrlc(int num)
|
void ft_ctrlc(int num)
|
||||||
{
|
{
|
||||||
if (*ft_get_heredoc() != -1)
|
|
||||||
{
|
|
||||||
close(*ft_get_heredoc());
|
|
||||||
*ft_get_heredoc() = -1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
rl_replace_line("", 0);
|
rl_replace_line("", 0);
|
||||||
rl_on_new_line();
|
rl_on_new_line();
|
||||||
ft_putchar_fd('\n', 1);
|
ft_putchar_fd('\n', 1);
|
||||||
rl_redisplay();
|
rl_redisplay();
|
||||||
}
|
|
||||||
*ft_get_exit_code() = 130;
|
*ft_get_exit_code() = 130;
|
||||||
(void) num;
|
(void) num;
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/03/28 16:02:31 by cchauvet #+# #+# */
|
/* 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_quit(int num);
|
||||||
void ft_ctrlc(int num);
|
void ft_ctrlc(int num);
|
||||||
|
void ft_ctrlc_heredoc(int num);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user