core: rebuild of parsing and the execution

This commit is contained in:
Camille Chauvet
2023-03-10 12:32:39 +01:00
parent a18c4cae82
commit d36d9c92f5
54 changed files with 708 additions and 618 deletions

59
redirection/file.c Normal file
View File

@ -0,0 +1,59 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* file.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 17:36:11 by cchauvet #+# #+# */
/* Updated: 2023/02/15 17:41:13 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "./redirection_private.h"
int ft_file_is_readable(const char *path)
{
int readable;
int fd;
fd = open(path, O_RDONLY);
if (fd == -1)
{
ft_eprintf("minishell: %s: No such file or directory\n", path);
return (0);
}
readable = read(fd, "", 0);
if (readable == -1)
{
ft_eprintf("minishell: %s: Permission denied\n", path);
return (0);
}
close(fd);
return (1);
}
int ft_file_is_writable(const char *path)
{
int writeable;
int fd;
fd = open(path, O_WRONLY | O_CREAT, 0644);
if (fd == -1)
{
ft_eprintf("minishell: %s: Permission denied\n", path);
return (0);
}
writeable = write(fd, "", 0);
if (writeable == -1)
{
ft_eprintf("minishell: %s: Permission denied\n", path);
return (0);
}
return (fd);
}
int ft_file_is_executable(const char *path)
{
return (access(path, X_OK) == 0);
}

57
redirection/heredoc.c Normal file
View File

@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* heredoc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/17 15:36:26 by cchauvet #+# #+# */
/* Updated: 2023/02/17 16:21:02 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "./redirection_private.h"
int *ft_get_heredoc()
{
static int heredoc = -1;
return (&heredoc);
}
int ft_heredoc(t_data *data, char *stop)
{
char *line;
char *line_clean;
int fds[2];
if (pipe(fds) == -1)
return (-2);
*ft_get_heredoc() = dup(0);
ft_printf("> ");
line = get_next_line(*ft_get_heredoc());
while (line != NULL)
{
line[ft_strlen(line) - 1] = '\0';
if (ft_strcmp(line, stop) == 0)
{
free(line);
break ;
}
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)
{
close(fds[0]);
close(fds[1]);
return (-2);
}
}
close(fds[1]);
*ft_get_heredoc() = -1;
return (fds[0]);
}

138
redirection/infile.c Normal file
View File

@ -0,0 +1,138 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* infile.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 17:52:10 by cchauvet #+# #+# */
/* Updated: 2023/02/20 13:05:43 by starnakin ### ########.fr */
/* */
/* ************************************************************************** */
#include "./redirection_private.h"
static int ft_infile_is_valid(t_data *data, const char *line)
{
char **tab;
char *path;
ssize_t i;
int return_code;
tab = ft_split_quoted(line, ' ');
if (tab == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (0);
}
if (tab[0] == NULL)
return (1);
return_code = 1;
i = -1;
while (tab[++i] != NULL && return_code == 1)
{
if (tab[i][0] == '<')
{
if (ft_strcmp(tab[i], "<") == 0)
{
path = ft_env_filler(data, tab[i + 1]);
if (path == NULL)
return_code = 0;
ft_quote_remover(path);
free(tab[i + 1]);
tab[i + 1] = path;
if (ft_file_is_readable(tab[i + 1]) == 0)
{
data->exit_code = 1;
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);
return (return_code);
}
static int ft_get_infile(t_data *data, const char *line)
{
size_t i;
int fd;
char **tab;
char *path;
tab = ft_split_quoted(line, ' ');
if (tab == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (-2);
}
fd = -1;
i = 0;
while (tab[i + 1] != NULL)
{
if (tab[i][0] == '<')
if (fd != 0)
close(fd);
if (ft_strcmp("<", tab[i]) == 0)
{
path = ft_env_filler(data, tab[i + 1]);
if (path == NULL)
return (-2);
ft_quote_remover(path);
fd = open(path, O_RDONLY);
free(path);
}
else if (ft_strcmp("<<", tab[i]) == 0)
fd = ft_heredoc(data, ft_quote_remover(tab[i + 1]));
i++;
}
ft_freer_tab_ultimate(1, tab);
return (fd);
}
static int ft_remove_infile(char *line)
{
size_t i;
size_t y;
char **tab;
tab = ft_split_quoted(line, ' ');
if (tab == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (1);
}
i = 0;
y = 0;
while (tab[i] != NULL)
{
if (tab[i][0] == '<')
{
ft_strshift(line + y,
(-1) * (ft_strlen(tab[i]) + ft_strlen(tab[i + 1]) + 1 + (line[ft_strlen(tab[i]) + ft_strlen(tab[i + 1]) + 1] != '\0')));
i++;
}
else
y = y + ft_strlen(tab[i]) + (y != 0);
i++;
}
ft_freer_tab_ultimate(1, tab);
return (0);
}
int ft_infile(t_data *data, char *line)
{
int fd;
if (ft_infile_is_valid(data, line) == 0)
return (-2);
fd = ft_get_infile(data, line);
if (fd == -2)
return (-2);
ft_remove_infile(line);
return (fd);
}

135
redirection/outfile.c Normal file
View File

@ -0,0 +1,135 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* infile.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 17:52:10 by cchauvet #+# #+# */
/* Updated: 2023/02/20 13:05:43 by starnakin ### ########.fr */
/* */
/* ************************************************************************** */
#include "./redirection_private.h"
static int ft_outfile_is_valid(t_data *data, const char *line)
{
char **tab;
char *path;
ssize_t i;
tab = ft_split_quoted(line, ' ');
if (tab == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (0);
}
if (tab[0] == NULL)
return (1);
i = -1;
while (tab[++i] != NULL)
{
if (tab[i][0] == '>')
{
path = ft_env_filler(data, tab[i + 1]);
if (path == NULL)
return (0);
ft_quote_remover(path);
if (ft_file_is_writable(path) == 0)
{
data->exit_code = 1;
free(path);
ft_freer_tab_ultimate(1, tab);
return (0);
}
if (ft_contain_only_str(path, "| >>"))
{
free(path);
ft_eprintf("minishell: %s: must be followed by an outfile\n", path);
ft_freer_tab_ultimate(1, tab);
return (0);
}
free(path);
}
}
ft_freer_tab_ultimate(1, tab);
return (1);
}
static int ft_get_outfile(t_data *data, const char *line)
{
size_t i;
int fd;
char **tab;
char *path;
tab = ft_split_quoted(line, ' ');
if (tab == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (-2);
}
fd = -1;
i = 0;
while (tab[i + 1] != NULL)
{
if (tab[i][0] == '>')
if (fd != 0)
close(fd);
path = ft_env_filler(data, tab[i + 1]);
if (path == NULL)
return (-2);
ft_quote_remover(path);
if (ft_strcmp(">", tab[i]) == 0)
fd = open(path, O_TRUNC | O_CREAT | O_WRONLY, 0644);
else if (ft_strcmp(">>", tab[i]) == 0)
fd = open(path, O_CREAT | O_APPEND, 0644);
free(path);
i++;
}
ft_freer_tab_ultimate(1, tab);
return (fd);
}
static int ft_remove_outfile(char *line)
{
size_t i;
size_t y;
char **tab;
tab = ft_split_quoted(line, ' ');
if (tab == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (1);
}
i = 0;
y = 0;
while (tab[i] != NULL)
{
if (tab[i][0] == '>')
{
ft_strshift(line + y,
(-1) * (ft_strlen(tab[i]) + ft_strlen(tab[i + 1]) + 1 + (line[ft_strlen(tab[i]) + ft_strlen(tab[i + 1]) + 1] != '\0')));
i++;
}
else
y = y + ft_strlen(tab[i]) + (y != 0);
i++;
}
ft_freer_tab_ultimate(1, tab);
return (0);
}
int ft_outfile(t_data *data, char *line)
{
int fd;
if (ft_outfile_is_valid(data, line) == 0)
return (-2);
fd = ft_get_outfile(data, line);
if (fd == -2)
return (-2);
ft_remove_outfile(line);
return (fd);
}

View File

@ -0,0 +1,8 @@
#ifndef REDIRECTION_H
# define REDIRECTION_H
# include "../data/data.h"
int ft_infile(t_data *data, char *line);
int ft_outfile(t_data *data, char *line);
int *ft_get_heredoc();
#endif

View File

@ -0,0 +1,16 @@
#ifndef REDIRECTIONS_PRIVATE
# define REDIRECTIONS_PRIVATE
# include <sys/stat.h>
# include <fcntl.h>
# include <stddef.h>
# include <unistd.h>
# include "../libftx/libftx.h"
# include "../data/data.h"
# include "../env/env.h"
# include "../utils/utils.h"
int ft_file_is_readable(const char *path);
int ft_file_is_writable(const char *path);
int ft_heredoc(t_data *data, char *stop);
#endif