fix: heredoc is now rightly implemented (no fork, ctrl + c support, ctrl + d support)

This commit is contained in:
Camille Chauvet 2023-03-07 17:48:18 +01:00
parent 8093b250ce
commit 876c75bb92
6 changed files with 56 additions and 63 deletions

BIN
.nfs00000000098c82b80000065f Executable file

Binary file not shown.

Binary file not shown.

View File

@ -13,54 +13,48 @@
#include "libftx/libftx.h" #include "libftx/libftx.h"
#include "minishell.h" #include "minishell.h"
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
int *ft_get_heredoc() int *ft_get_heredoc()
{ {
static int heredoc; static int heredoc = -1;
return (&heredoc); return (&heredoc);
} }
int ft_heredoc_creator(char *stop, int fd) int ft_heredoc(t_data *data, char *stop)
{ {
char *line; char *line;
char *line_clean;
line = readline("> ");
while (line != NULL)
{
if (ft_strcmp(line, stop) == 0)
break ;
ft_putendl_fd(line, fd);
free(line);
line = readline("> ");
if (line == NULL)
return (1);
}
return (0);
}
int ft_heredoc(char *stop)
{
int pid;
int fds[2]; int fds[2];
int exit_code;
if (pipe(fds) == -1) if (pipe(fds) == -1)
return (-1); return (-2);
pid = fork(); *ft_get_heredoc() = dup(0);
if (pid == -1) ft_printf("> ");
return (-1); line = get_next_line(*ft_get_heredoc());
if (pid == 0) while (line != NULL)
{ {
exit_code = ft_heredoc_creator(stop, fds[1]); line[ft_strlen(line) - 1] = '\0';
exit (exit_code + 1); if (ft_strcmp(line, stop) == 0)
{
free(line);
break ;
} }
else line_clean = ft_env_filler(data, line);
free(line);
ft_putendl_fd(line_clean, fds[1]);
free(line_clean);
ft_printf("> ");
line = get_next_line(*ft_get_heredoc());
if (line == NULL && *ft_get_heredoc() == -1)
{ {
*ft_get_heredoc() = pid; close(fds[0]);
waitpid(pid, &exit_code, 0);
*ft_get_heredoc() = 0;
close(fds[1]); close(fds[1]);
return (-2);
}
}
close(fds[1]);
*ft_get_heredoc() = -1;
return (fds[0]); return (fds[0]);
} }
}

View File

@ -10,16 +10,14 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "libftx/libftx.h"
#include "minishell.h" #include "minishell.h"
#include "utils/utils.h"
#include <fcntl.h>
static int ft_infile_is_valid(t_data *data, const char *line) static int ft_infile_is_valid(t_data *data, const char *line)
{ {
char **tab; char **tab;
char *path; char *path;
ssize_t i; ssize_t i;
int return_code;
tab = ft_split_quoted(line, ' '); tab = ft_split_quoted(line, ' ');
if (tab == NULL) if (tab == NULL)
@ -29,34 +27,34 @@ static int ft_infile_is_valid(t_data *data, const char *line)
} }
if (tab[0] == NULL) if (tab[0] == NULL)
return (1); return (1);
return_code = 1;
i = -1; i = -1;
while (tab[++i] != NULL) while (tab[++i] != NULL && return_code == 1)
{ {
if (tab[i][0] == '<') if (tab[i][0] == '<')
{
if (ft_strcmp(tab[i], "<") == 0)
{ {
path = ft_env_filler(data, tab[i + 1]); path = ft_env_filler(data, tab[i + 1]);
if (path == NULL) if (path == NULL)
return (0); return_code = 0;
ft_quote_remover(path); ft_quote_remover(path);
if (ft_file_is_readable(path) == 0) free(tab[i + 1]);
tab[i + 1] = path;
if (ft_file_is_readable(tab[i + 1]) == 0)
{ {
data->exit_code = 1; data->exit_code = 1;
free(path); return_code = -1;
}
}
if (ft_contain_only_str(tab[i + 1], "| <>"))
return_code = 0;
}
}
if (return_code == 0)
ft_eprintf("minishell: %s: must be followed by an infile\n", tab[i]);
ft_freer_tab_ultimate(1, tab); ft_freer_tab_ultimate(1, tab);
return (0); return (return_code);
}
if (ft_contain_only_str(path, "| <>"))
{
free(path);
ft_eprintf("minishell: %s: must be followed by an infile\n", path);
ft_freer_tab_ultimate(1, tab);
return (0);
}
free(path);
}
}
ft_freer_tab_ultimate(1, tab);
return (1);
} }
static int ft_get_infile(t_data *data, const char *line) static int ft_get_infile(t_data *data, const char *line)
@ -89,7 +87,7 @@ static int ft_get_infile(t_data *data, const char *line)
free(path); free(path);
} }
else if (ft_strcmp("<<", tab[i]) == 0) else if (ft_strcmp("<<", tab[i]) == 0)
fd = ft_heredoc(ft_quote_remover(tab[i + 1])); fd = ft_heredoc(data, ft_quote_remover(tab[i + 1]));
i++; i++;
} }
ft_freer_tab_ultimate(1, tab); ft_freer_tab_ultimate(1, tab);

7
main.c
View File

@ -39,7 +39,6 @@ static char *ft_get_user_input()
static int ft_minishell(t_data *data, char *line) static int ft_minishell(t_data *data, char *line)
{ {
t_list **cmds; t_list **cmds;
char *line_clean; char *line_clean;
int infile; int infile;
@ -107,9 +106,11 @@ static int ft_minishell(t_data *data, char *line)
void ft_ctrlc(int num) void ft_ctrlc(int num)
{ {
(void) num; (void) num;
if (*ft_get_heredoc())
if (*ft_get_heredoc() != -1)
{ {
kill(*ft_get_heredoc(), SIGQUIT); close(*ft_get_heredoc());
*ft_get_heredoc() = -1;
} }
else else
{ {

View File

@ -40,7 +40,7 @@ int ft_file_is_appendable(const char *path);
char *ft_get_file_path(const char *infile); char *ft_get_file_path(const char *infile);
int ft_infile(t_data *data, char *line); int ft_infile(t_data *data, char *line);
int ft_outfile(t_data *data, char *line); int ft_outfile(t_data *data, char *line);
int ft_heredoc(char *stop); int ft_heredoc(t_data *data, char *stop);
int *ft_get_heredoc(); int *ft_get_heredoc();
size_t ft_seglen_quoted(const char *str, char c); size_t ft_seglen_quoted(const char *str, char c);
int ft_cmds_executor(t_data *data, t_list **cmds); int ft_cmds_executor(t_data *data, t_list **cmds);