validating redirects WIP

This commit is contained in:
Quinten Mennen 2025-02-26 16:24:30 +01:00
parent 52c46ed40a
commit 46a39c17cb
10 changed files with 259 additions and 196 deletions

View File

@ -17,7 +17,7 @@
t_redirect *redirect_new(t_minishell *minishell, t_token_type type, char *value); t_redirect *redirect_new(t_minishell *minishell, t_token_type type, char *value);
t_list *redirect_get_inputs(t_minishell *minishell, t_list *list); t_list *redirect_get_inputs(t_minishell *minishell, t_list *list);
t_list *redirect_get_outputs(t_minishell *minishell, t_list *list); t_list *redirect_get_outputs(t_minishell *minishell, t_list *list);
int redirect_is_valid(t_list *lst, t_token *token); int redirect_is_valid(t_list *lst, t_token *token, int mode);
int redirect_token_type(t_token *token); int redirect_token_type(t_token *token);
int redirect_is_delimiter(t_token *token); int redirect_is_delimiter(t_token *token);

View File

@ -23,6 +23,8 @@ void simple_builtins(t_minishell *minishell);
void error_msg(char *func, char *msg); void error_msg(char *func, char *msg);
void check_malloc(t_minishell *minishell, void *ptr); void check_malloc(t_minishell *minishell, void *ptr);
char *ft_strdup_safe(t_minishell *minishell, const char *str); char *ft_strdup_safe(t_minishell *minishell, const char *str);
char *ft_substr_safe(t_minishell *minishell, const char *str,
unsigned int start, size_t len);
char *ft_strjoin_safe(t_minishell *minishell, char *ft_strjoin_safe(t_minishell *minishell,
const char *s1, const char *s2); const char *s1, const char *s2);
void *malloc_safe(t_minishell *minishell, size_t size); void *malloc_safe(t_minishell *minishell, size_t size);

View File

@ -1,23 +1,23 @@
VPATH = src:src/prompt:src/utils:src/lexer:src/token:src/environment:src/executor:src/parser:src/debug:src/expander:src/builtin:src/signal:src/redirect: VPATH = src:src/prompt:src/utils:src/lexer:src/token:src/environment:src/executor:src/parser:src/debug:src/expander:src/builtin:src/signal:src/redirect:
SOURCES = history_load.c history_write.c prompt.c error_msg.c \ SOURCES = history_load.c history_write.c prompt.c ft_lstclear_safe.c \
ft_lstclear_safe.c ft_lstnew_safe.c ft_strdup_safe.c \ ft_lstnew_safe.c init_minishell.c malloc_safe.c print_banner.c \
ft_strjoin_safe.c init_minishell.c malloc_safe.c print_banner.c \ free_minishell_line.c error_msg.c free_command_list.c \
check_malloc.c free_command_list.c free_freelist.c free_lexer.c \ ft_strdup_safe.c ft_strjoin_safe.c ft_substr_safe.c check_malloc.c \
free_minishell.c free_minishell_line.c free_safe.c free_token.c \ free_freelist.c free_lexer.c free_minishell.c free_safe.c \
free_token_list.c lexer_read_char.c lexer_new.c lexer_parse_input.c \ free_token.c free_token_list.c lexer_parse_input.c lexer_read_word.c \
lexer_read_word.c lexer_token_next.c token_new.c token_parse.c \ lexer_token_next.c lexer_read_char.c lexer_new.c token_parse.c \
environment_add.c environment_del.c environment_free.c \ token_new.c environment_free.c environment_free_list.c \
environment_free_list.c environment_get.c environment_get_arr.c \ environment_print.c environment_add.c environment_del.c \
environment_parse.c environment_print.c executor_close_fds.c \ environment_get.c environment_get_arr.c environment_parse.c \
executor_absolute_path.c executor_child.c executor_count_fds.c \ executor_close_fds.c executor_count_fds.c executor_create_pipes.c \
executor_create_pipes.c executor_create_redirects.c \ executor_create_redirects.c executor_execute_pipeline.c \
executor_execute_pipeline.c executor_fork.c executor_open_fds.c \ executor_fork.c executor_open_fds.c executor_absolute_path.c \
parser_get_arguments.c parser_get_commands.c parser_new_command.c \ executor_child.c parser_get_commands.c parser_get_arguments.c \
print_commands.c print_freelist.c expander_is_character.c \ parser_new_command.c print_commands.c print_freelist.c \
expander_expand_dollar.c expander_allocate_memory.c \ expander_is_character.c expander_allocate_memory.c \
expander_get_variable.c expander_parse_string.c \ expander_expand_dollar.c expander_get_variable.c \
expander_parse_variables.c builtin_echo.c builtin_router.c \ expander_parse_string.c expander_parse_variables.c builtin_echo.c \
simple_builtins.c is_builtin.c builtin_cd.c builtin_env.c \ builtin_router.c simple_builtins.c builtin_cd.c builtin_env.c \
builtin_exit.c builtin_export.c builtin_pwd.c builtin_unset.c \ builtin_exit.c builtin_export.c builtin_pwd.c builtin_unset.c \
signal.c redirect_valid_type.c redirect_get_inputs.c \ is_builtin.c signal.c redirect_valid_type.c redirect_get_inputs.c \
redirect_get_outputs.c redirect_new.c main.c \ redirect_get_outputs.c redirect_new.c main.c \

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* ::: :::::::: */
/* expander_get_variable.c :+: :+: */ /* expander_get_variable.c :+: :+: :+: */
/* +:+ */ /* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/19 13:59:03 by qmennen #+# #+# */ /* Created: 2025/02/19 13:59:03 by qmennen #+# #+# */
/* Updated: 2025/02/25 15:26:36 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 14:10:53 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,7 +21,7 @@ t_environment *expander_get_var(const char *s, int idx, t_minishell *minishell)
i = 0; i = 0;
while (expander_character_valid(s[idx + i])) while (expander_character_valid(s[idx + i]))
i++; i++;
name = ft_substr(s, idx, i); name = ft_substr_safe(minishell, s, idx, i);
if (!name || !*name) if (!name || !*name)
return (NULL); return (NULL);
env = environment_get(minishell, name); env = environment_get(minishell, name);

View File

@ -1,46 +1,63 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* parser_get_commands.c :+: :+: :+: */ /* parser_get_commands.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/11 14:06:02 by qmennen #+# #+# */ /* Created: 2025/02/11 14:06:02 by qmennen #+# #+# */
/* Updated: 2025/02/18 20:36:01 by qmennen ### ########.fr */ /* Updated: 2025/02/18 20:36:01 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
#include "utils.h"
static int is_command_token(t_token *token)
{ static int is_command_token(t_token *token)
return (token->type < 3 || redirect_token_type(token)); {
} return (token->type < 3 || redirect_token_type(token));
}
t_list *parser_get_commands(t_minishell *minishell)
{ /*
t_list *command_list; static int validate_redirects(t_list *lst)
t_list *current; {
t_command *command; t_list *token;
t_token *token;
token = lst;
command_list = NULL; while (token)
if (!minishell->tokens) {
return (NULL); if (((t_token *)token)->type == T_ERROR)
current = minishell->tokens; return (0);
while (current) token = token->next;
{ }
token = (t_token *) current->content; return (1);
command = parser_command_new(minishell, ft_strdup_safe(minishell, token->value)); }
command->args = parser_get_arguments(current, minishell); */
command->redirect_in = redirect_get_inputs(minishell, current);
command->redirect_out = redirect_get_outputs(minishell, current); t_list *parser_get_commands(t_minishell *minishell)
ft_lstadd_back(&command_list, ft_lstnew_safe(minishell, command)); {
while (current && is_command_token((t_token *)current->content)) t_list *command_list;
current = current->next; t_list *current;
if (current && ((t_token *)current->content)->type >= 3) t_command *command;
current = current->next; t_token *token;
}
ft_lstiter(command_list, print_commands); command_list = NULL;
return (command_list); if (!minishell->tokens)
} return (NULL);
current = minishell->tokens;
while (current)
{
token = (t_token *) current->content;
command = parser_command_new(minishell, ft_strdup_safe(minishell, token->value));
command->args = parser_get_arguments(current, minishell);
command->redirect_in = redirect_get_inputs(minishell, current);
command->redirect_out = redirect_get_outputs(minishell, current);
ft_lstadd_back(&command_list, ft_lstnew_safe(minishell, command));
while (current && is_command_token((t_token *)current->content))
current = current->next;
if (current && ((t_token *)current->content)->type >= 3)
current = current->next;
}
ft_lstiter(command_list, print_commands);
return (command_list);
}

View File

@ -1,76 +1,93 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* :::::::: */
/* redirect_get_inputs.c :+: :+: */ /* redirect_get_inputs.c :+: :+: */
/* +:+ */ /* +:+ */
/* By: Quinten <qmennen@student.codam.nl> +#+ */ /* By: Quinten <qmennen@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/23 12:29:05 by Quinten #+# #+# */ /* Created: 2025/02/23 12:29:05 by Quinten #+# #+# */
/* Updated: 2025/02/23 12:29:05 by Quinten ######## odam.nl */ /* Updated: 2025/02/23 12:29:05 by Quinten ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "redirect.h" #include "redirect.h"
static void process_heredoc(t_minishell *ms, t_token *heredoc, t_token *delim) static int process_heredoc(t_minishell *msh, t_token *heredoc, t_token *delim)
{ {
char *line; char *line;
int fd; char *expand;
int fd;
fd = open(".ms_heredoc", O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0) //TODO: Will this work? fd = open(".ms_heredoc", O_WRONLY | O_CREAT | O_TRUNC, 0644);
return ; if (fd < 0) // TODO: Will this work?
{
while (TRUE) error_msg("heredoc", "unable to write to temp file");
{ return (fd);
line = readline(">"); }
if ((*line && ft_strcmp(line, delim->value) == 0) || !line) //TODO: What if not line?
break ; while (TRUE && fd > 0)
if (!*line) {
ft_putendl_fd("", fd); line = readline(">");
else if ((*line && ft_strcmp(line, delim->value) == 0) || !line) // TODO: What if not line?
ft_putendl_fd(line, fd); break;
} if (!*line)
close(fd); ft_putendl_fd("", fd);
heredoc->type = T_REDIRECT_IN; else
delim->value = ft_strdup_safe(ms, ".ms_heredoc"); {
} expand = expander_parse_string(line, msh);
ft_putendl_fd(expand, fd);
t_list *redirect_get_inputs(t_minishell *minishell, t_list *list) free_safe(msh, (void **)&expand);
{ }
t_list *current; free(line);
t_list *redirects; }
t_token *token; close(fd);
free(line);
redirects = NULL; heredoc->type = T_REDIRECT_IN;
current = list; delim->value = ft_strdup_safe(msh, ".ms_heredoc");
while (current) return (1);
{ }
token = (t_token *)current->content;
if (redirect_is_delimiter(token)) t_list *redirect_get_inputs(t_minishell *minishell, t_list *list)
break ; {
if (token->type != T_REDIRECT_IN && token->type != T_HEREDOC) t_list *current;
{ t_list *redirects;
current = current->next; t_token *token;
continue ; int result;
}
if (token->type == T_HEREDOC && redirect_is_valid(current, token)) redirects = NULL;
process_heredoc(minishell, token, current->next->content); current = list;
if (redirect_is_valid(current, token)) result = 1;
{ while (current && result)
ft_lstadd_back(&redirects, ft_lstnew_safe(minishell, {
redirect_new(minishell, token->type, token = (t_token *)current->content;
ft_strdup_safe(minishell, if (redirect_is_delimiter(token))
((t_token *)current->next->content)->value)))); break ;
current = current->next; if (token->type != T_REDIRECT_IN && token->type != T_HEREDOC)
continue ; {
} current = current->next;
else continue ;
{ }
ft_lstadd_front(&redirects, ft_lstnew_safe(minishell, redirect_new(minishell, T_ERROR, NULL))); if (token->type == T_HEREDOC && redirect_is_valid(current, token, F_OK | W_OK))
break ; result = process_heredoc(minishell, token, current->next->content);
} if (redirect_is_valid(current, token, F_OK | R_OK))
current = current->next; {
} ft_lstadd_back(&redirects, ft_lstnew_safe(minishell,
return (redirects); redirect_new(minishell, token->type,
} ft_strdup_safe(minishell,
((t_token *)current->next->content)->value))));
current = current->next;
continue;
}
else
{
ft_lstadd_front(&redirects, ft_lstnew_safe(minishell, redirect_new(minishell, T_ERROR, NULL)));
break;
}
current = current->next;
}
if (result < 0)
{
ft_lstadd_front(&redirects, ft_lstnew_safe(minishell, redirect_new(minishell, T_ERROR, NULL)));
}
return (redirects);
}

View File

@ -30,7 +30,7 @@ t_list *redirect_get_outputs(t_minishell *minishell, t_list *list)
current = current->next; current = current->next;
continue ; continue ;
} }
if (redirect_is_valid(current, token)) if (redirect_is_valid(current, token, F_OK | W_OK))
{ {
ft_lstadd_front(&redirects, ft_lstadd_front(&redirects,
ft_lstnew_safe(minishell, redirect_new(minishell, token->type, ft_strdup_safe(minishell, ((t_token *)current->next->content)->value)))); ft_lstnew_safe(minishell, redirect_new(minishell, token->type, ft_strdup_safe(minishell, ((t_token *)current->next->content)->value))));

View File

@ -1,37 +1,41 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* :::::::: */
/* redirect_valid_type.c :+: :+: */ /* redirect_valid_type.c :+: :+: */
/* +:+ */ /* +:+ */
/* By: Quinten <qmennen@student.codam.nl> +#+ */ /* By: Quinten <qmennen@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/23 12:30:18 by Quinten #+# #+# */ /* Created: 2025/02/23 12:30:18 by Quinten #+# #+# */
/* Updated: 2025/02/23 12:30:18 by Quinten ######## odam.nl */ /* Updated: 2025/02/23 12:30:18 by Quinten ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int redirect_token_type(t_token *token) int redirect_token_type(t_token *token)
{ {
return (token->type == T_REDIRECT_IN || token->type == T_HEREDOC || return (token->type == T_REDIRECT_IN || token->type == T_HEREDOC ||
token->type == T_REDIRECT_OUT || token->type == T_APPEND_OUT); token->type == T_REDIRECT_OUT || token->type == T_APPEND_OUT);
} }
int redirect_is_valid(t_list *lst, t_token *token) int redirect_is_valid(t_list *lst, t_token *token, int mode)
{ {
t_token *next; t_token *next;
if (!lst->next) if (!lst->next)
return (0); return (0);
next = (t_token *)lst->next->content; next = (t_token *)lst->next->content;
if (!next) if (!next)
return (0); {
return (redirect_token_type(token) && next->type < 3); return (0);
} }
if (!access(next->value, mode))
int redirect_is_delimiter(t_token *token) return (0);
{ return (redirect_token_type(token) && next->type < 3);
return (token->type == T_PIPE || token->type == T_AND || }
token->type == T_OR || token->type == T_EOF || token->type == T_ERROR);
} int redirect_is_delimiter(t_token *token)
{
return (token->type == T_PIPE || token->type == T_AND ||
token->type == T_OR || token->type == T_EOF || token->type == T_ERROR);
}

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* ::: :::::::: */
/* free_minishell_line.c :+: :+: */ /* free_minishell_line.c :+: :+: :+: */
/* +:+ */ /* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/05 16:01:44 by whaffman #+# #+# */ /* Created: 2025/02/05 16:01:44 by whaffman #+# #+# */
/* Updated: 2025/02/25 17:40:32 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 14:24:20 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_strdup_safe.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/20 18:01:27 by whaffman #+# #+# */
/* Updated: 2025/02/20 18:04:53 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "minishell.h"
char *ft_substr_safe(t_minishell *minishell, const char *str,
unsigned int start, size_t len)
{
char *new_str;
new_str = ft_substr(str, start, len);
check_malloc(minishell, new_str);
return (new_str);
}