This commit is contained in:
Etienne Rey-bethbeder 2023-03-13 15:12:36 +01:00
commit 1b17df6170
12 changed files with 174 additions and 317 deletions

Binary file not shown.

View File

@ -21,9 +21,8 @@ SRCS = ${BUILTINS_SRC} \
./syntax/syntax.c \
./format/format.c \
./redirection/heredoc.c \
./redirection/infile.c \
./redirection/outfile.c \
./redirection/file.c \
./redirection/redirection.c \
./parse/parse.c
OBJS = ${SRCS:.c=.o}

View File

@ -51,12 +51,12 @@ static bool ft_executor(t_cmd *cmd, char **env)
return (1);
if (cmd->pid == 0)
{
dup2(cmd->fd_in[0], 0);
dup2(cmd->fd_out[0], 1);
if (cmd->fd_in[1] != -1)
dup2(cmd->fd_in[1], 0);
if (cmd->fd_out[1] != -1)
dup2(cmd->fd_out[1], 1);
dup2(cmd->fd_in[0], 0);
dup2(cmd->fd_out[0], 1);
ft_closer(cmd->fd_in);
ft_closer(cmd->fd_out);
execve(cmd->executable, cmd->args, env);

6
main.c
View File

@ -10,8 +10,6 @@
/* */
/* ************************************************************************** */
#include "cmd/cmd.h"
#include "libftx/libft/list.h"
#include "minishell.h"
static char *ft_get_user_input()
@ -89,7 +87,8 @@ void ft_ctrlc(int num)
void ft_quit(int num)
{
(void) num;
rl_replace_line("", 0);
rl_redisplay();
ft_printf("%c%c %c%c", 0, 0, 0, 0);
}
@ -122,7 +121,6 @@ int main(int ac, char **av, char **env)
if (line == NULL)
break ;
}
free(data.exit_code_str);
ft_lstclear(data.cmds, ft_cmddel);
free(data.cmds);

View File

@ -1,26 +1,5 @@
#include "./parse_private.h"
static int ft_redirection_parse(t_data *data, char *cmd_str, t_cmd *cmd)
{
int fd_in;
int fd_out;
fd_in = ft_infile(data, cmd_str);
if (fd_in == -2)
return (1);
fd_out = ft_outfile(data, cmd_str);
if (fd_out == -2)
{
close(fd_in);
return (1);
}
cmd->fd_in[0] = fd_in;
cmd->fd_in[1] = -1;
cmd->fd_out[0] = fd_out;
cmd->fd_out[1] = -1;
return (0);
}
int ft_args_parse(t_data *data, char *cmd_str, t_cmd *cmd)
{
char **tab;
@ -39,6 +18,7 @@ int ft_args_parse(t_data *data, char *cmd_str, t_cmd *cmd)
ft_freer_tab_ultimate(1, tab);
return (1);
}
ft_quote_remover(str);
free(tab[i]);
tab[i] = str;
i++;
@ -54,7 +34,12 @@ int ft_executable_parse(t_data *data, t_cmd *cmd)
path = cmd->args[0];
own = 0;
if (ft_strcmp(cmd->args[0], "env") == 0)
if (cmd->args[0] == NULL)
{
ft_closer(cmd->fd_in);
ft_closer(cmd->fd_out);
}
else if (ft_strcmp(cmd->args[0], "env") == 0)
own = 1;
else if (ft_strcmp(cmd->args[0], "export") == 0)
own = 1;
@ -68,7 +53,7 @@ int ft_executable_parse(t_data *data, t_cmd *cmd)
own = 1;
else if (ft_strcmp(cmd->args[0], "cd") == 0)
own = 1;
if (own == 0)
else
path = ft_get_executable(data->env, cmd->args[0]);
cmd->own_cmd = own;
cmd->executable = path;
@ -86,8 +71,11 @@ int ft_cmd_parser(t_data *data, char *cmd_str)
ft_eprintf("minishell: malloc failed\n");
return (1);
}
if (ft_redirection_parse(data, cmd_str, cmd))
if (ft_redirection(data, cmd, cmd_str))
{
ft_cmddel(cmd);
return (1);
}
if (ft_args_parse(data, cmd_str, cmd))
{
ft_cmddel(cmd);
@ -118,11 +106,17 @@ int ft_cmds_parser(t_data *data, const char *line)
i = 0;
while (tab[i] != NULL)
{
ft_cmd_parser(data, tab[i]);
if (ft_cmd_parser(data, tab[i]))
{
ft_freer_tab_ultimate(1, tab);
return (1);
}
i++;
}
ft_add_fd(((t_cmd *) (*data->cmds)->content)->fd_in, 0);
ft_add_fd(((t_cmd *) ((ft_lstlast(*data->cmds))->content))->fd_out, 1);
if (((t_cmd *) (*data->cmds)->content)->fd_in[0] == -1)
((t_cmd *) (*data->cmds)->content)->fd_in[0] = 0;
if (((t_cmd *) (ft_lstlast(*data->cmds))->content)->fd_out[0] == -1)
(((t_cmd *) (ft_lstlast(*data->cmds))->content)->fd_out[0] = 1);
ft_freer_tab_ultimate(1, tab);
return (0);
}

View File

@ -12,7 +12,7 @@
#include "./redirection_private.h"
int ft_file_is_readable(const char *path)
int ft_file_is_readable(t_data *data, const char *path)
{
int readable;
int fd;
@ -20,12 +20,14 @@ int ft_file_is_readable(const char *path)
fd = open(path, O_RDONLY);
if (fd == -1)
{
data->exit_code = 1;
ft_eprintf("minishell: %s: No such file or directory\n", path);
return (0);
}
readable = read(fd, "", 0);
if (readable == -1)
{
data->exit_code = 1;
ft_eprintf("minishell: %s: Permission denied\n", path);
return (0);
}
@ -33,27 +35,46 @@ int ft_file_is_readable(const char *path)
return (1);
}
int ft_file_is_writable(const char *path)
int ft_file_is_writable(t_data *data, const char *path)
{
int writeable;
int fd;
fd = open(path, O_WRONLY | O_CREAT, 0644);
fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644);
if (fd == -1)
{
data->exit_code = 1;
ft_eprintf("minishell: %s: Permission denied\n", path);
return (0);
}
writeable = write(fd, "", 0);
if (writeable == -1)
{
data->exit_code = 1;
ft_eprintf("minishell: %s: Permission denied\n", path);
return (0);
}
return (fd);
}
int ft_file_is_executable(const char *path)
int ft_file_is_appendable(t_data *data, const char *path)
{
return (access(path, X_OK) == 0);
int writeable;
int fd;
fd = open(path, O_WRONLY | O_APPEND | O_CREAT, 0644);
if (fd == -1)
{
data->exit_code = 1;
ft_eprintf("minishell: %s: Permission denied\n", path);
return (0);
}
writeable = write(fd, "", 0);
if (writeable == -1)
{
data->exit_code = 1;
ft_eprintf("minishell: %s: Permission denied\n", path);
return (0);
}
return (fd);
}

View File

@ -1,138 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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);
}

View File

@ -1,135 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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);
}

114
redirection/redirection.c Normal file
View File

@ -0,0 +1,114 @@
#include "redirection_private.h"
int ft_replace_file(t_data *data, char **tab)
{
size_t i;
char *redirection;
i = 0;
while (tab[i] != NULL)
{
if (ft_is_in("<>", tab[i][0]))
{
i++;
redirection = ft_env_filler(data, tab[i]);
if (redirection == NULL)
return (1);
free(tab[i]);
tab[i] = redirection;
ft_quote_remover(tab[i]);
}
i++;
}
return (0);
}
void ft_remove_redirection(char *cmd_str)
{
size_t i;
ssize_t start;
ssize_t stop;
i = 0;
while (cmd_str[i] != '\0')
{
start = -1;
while ((ft_is_in_quote(cmd_str, i) || !ft_is_in("<>", cmd_str[i]))
&& cmd_str[i] != '\0')
i++;
if (ft_is_in("<>", cmd_str[i]))
start = i;
while (cmd_str[i] == cmd_str[start])
i++;
i++;
while (cmd_str[i] != '\0' && (cmd_str[i] != ' ' || ft_is_in_quote(cmd_str, i)))
i++;
stop = i - start;
if (start != -1)
ft_strshift(cmd_str + start, -1 * stop);
}
}
int ft_set_redirection(t_data *data, t_cmd *cmd, char **tab)
{
size_t i;
i = 0;
while (tab[i + 1] != NULL)
{
ft_quote_remover(tab[i + 1]);
if (ft_strcmp(tab[i], "<<") == 0)
{
cmd->fd_in[0] = ft_heredoc(data, tab[i + 1]);
if (cmd->fd_in[0] == -2)
return (1);
}
else if (ft_strcmp(tab[i], "<") == 0)
{
if (ft_file_is_readable(data, tab[i + 1]))
cmd->fd_in[0] = open(tab[i + 1], O_RDONLY);
else
return (1);
}
else if(ft_strcmp(tab[i], ">") == 0)
{
if (ft_file_is_writable(data, tab[i + 1]))
cmd->fd_out[0] = open(tab[i + 1], O_WRONLY | O_TRUNC | O_CREAT, 0644);
else
return (1);
}
else if(ft_strcmp(tab[i], ">>") == 0)
{
if (ft_file_is_appendable(data, tab[i + 1]))
cmd->fd_out[0] = open(tab[i + 1], O_WRONLY | O_APPEND | O_CREAT, 0644);
else
return (1);
}
i++;
}
return (0);
}
int ft_redirection(t_data *data, t_cmd *cmd, char *cmd_str)
{
char **tab;
cmd->fd_in[0] = -1;
cmd->fd_in[1] = -1;
cmd->fd_out[0] = -1;
cmd->fd_out[1] = -1;
tab = ft_split_quoted(cmd_str, ' ');
if (tab == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (1);
}
ft_remove_redirection(cmd_str);
if (ft_set_redirection(data, cmd, tab))
{
ft_freer_tab_ultimate(1, tab);
return (1);
}
ft_freer_tab_ultimate(1, tab);
return (0);
}

View File

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

View File

@ -8,9 +8,11 @@
# include "../data/data.h"
# include "../env/env.h"
# include "../utils/utils.h"
# include "../cmd/cmd.h"
int ft_file_is_readable(const char *path);
int ft_file_is_writable(const char *path);
int ft_file_is_readable(t_data *data, const char *path);
int ft_file_is_writable(t_data *data, const char *path);
int ft_file_is_appendable(t_data *data, const char *path);
int ft_heredoc(t_data *data, char *stop);
#endif

1
t Executable file
View File

@ -0,0 +1 @@
bozo