diff --git a/Makefile b/Makefile index 634f3cc..8e3044a 100644 --- a/Makefile +++ b/Makefile @@ -3,10 +3,10 @@ # :::::::: # # Makefile :+: :+: # # +:+ # -# By: qmennen +#+ # +# By: marvin +#+ # # +#+ # # Created: 2024/10/15 11:48:46 by whaffman #+# #+# # -# Updated: 2025/02/20 11:10:42 by whaffman ######## odam.nl # +# Updated: 2025/02/23 12:28:40 by Quinten ######## odam.nl # # # # **************************************************************************** # @@ -24,6 +24,7 @@ OBJ_PATH = obj VPATH = src:src/environment:src/prompt:src/lexer:src/token:src/utils: VPATH += src/executor:src/parser:src/expander:src/debug:src/signal:src/builtin +VPATH += src/redirect SOURCES = $(shell basename -a $(shell find $(SRC_PATH) -type f -name "*.c")) OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o)) diff --git a/inc/minishell.h b/inc/minishell.h index 27bca6a..6b60f96 100644 --- a/inc/minishell.h +++ b/inc/minishell.h @@ -3,10 +3,10 @@ /* :::::::: */ /* minishell.h :+: :+: */ /* +:+ */ -/* By: whaffman +#+ */ +/* By: marvin +#+ */ /* +#+ */ /* Created: 2025/02/04 16:13:13 by whaffman #+# #+# */ -/* Updated: 2025/02/20 11:35:24 by whaffman ######## odam.nl */ +/* Updated: 2025/02/23 12:28:23 by Quinten ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -24,6 +24,7 @@ # include "executor.h" # include "parser.h" # include "expander.h" +# include "redirect.h" # include "debug.h" # include "utils.h" diff --git a/inc/redirect.h b/inc/redirect.h new file mode 100644 index 0000000..4f6f0c4 --- /dev/null +++ b/inc/redirect.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* redirect.h :+: :+: */ +/* +:+ */ +/* By: Quinten +#+ */ +/* +#+ */ +/* Created: 2025/02/23 12:26:29 by Quinten #+# #+# */ +/* Updated: 2025/02/23 12:26:29 by Quinten ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#ifndef REDIRECT_H +# define REDIRECT_H +# include "minishell.h" + +t_redirect *redirect_new(t_token_type type, char *value); +t_list *redirect_get_inputs(t_list *list); +int redirect_is_valid(t_list *lst, t_token *token); +#endif \ No newline at end of file diff --git a/src/parser/parser_get_commands.c b/src/parser/parser_get_commands.c index c6af1fd..e49dfb1 100644 --- a/src/parser/parser_get_commands.c +++ b/src/parser/parser_get_commands.c @@ -28,7 +28,7 @@ t_list *parser_get_commands(t_minishell *minishell) token = (t_token *) current->content; command = parser_command_new(ft_strdup(token->value)); command->args = parser_get_arguments(current, minishell); - command->redirect_in = parser_get_input_redirects(current); + command->redirect_in = redirect_get_inputs(current); ft_lstadd_back(&command_list, ft_lstnew(command)); while (current && (((t_token *)current->content)->type < 3 || ((t_token *)current->content)->type == T_REDIRECT_IN)) current = current->next; diff --git a/src/parser/parser_get_input_redirects.c b/src/parser/parser_get_input_redirects.c deleted file mode 100644 index aee0376..0000000 --- a/src/parser/parser_get_input_redirects.c +++ /dev/null @@ -1,58 +0,0 @@ -# include "minishell.h" - -static int valid_def(t_list *list, t_token *token) -{ - t_token *next; - - if (!list->next) - return (0); - next = (t_token *)list->next->content; - if (!next) - return (0); - return ((token->type == T_REDIRECT_IN || token->type == T_HEREDOC) && next->type < 3); -} - -static t_redirect *redirect_new(t_token_type type, char *value) -{ - t_redirect *result; - - result = ft_malloc_safe(sizeof(t_redirect)); - result->type = type; - result->value = NULL; - if (value) - result->value = value; - return (result); -} - -t_list *parser_get_input_redirects(t_list *list) -{ - t_list *current; - t_list *redirects; - t_token *token; - - redirects = NULL; - current = list; - while (current) - { - token = (t_token *)current->content; - if (token->type != T_REDIRECT_IN && token->type != T_HEREDOC) - { - current = current->next; - continue ; - } - if (valid_def(current, token)) - { - ft_lstadd_front(&redirects, ft_lstnew(redirect_new(token->type, ft_strdup(((t_token *)current->next->content)->value)))); - current = current->next; - continue ; - } - else - { - ft_lstadd_front(&redirects, ft_lstnew(redirect_new(T_ERROR, NULL))); - break ; - } - current = current->next; - } - ft_lstiter(redirects, print_redirects); - return (redirects); -} \ No newline at end of file diff --git a/src/redirect/redirect_get_inputs.c b/src/redirect/redirect_get_inputs.c new file mode 100644 index 0000000..4caf8b8 --- /dev/null +++ b/src/redirect/redirect_get_inputs.c @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* redirect_get_inputs.c :+: :+: */ +/* +:+ */ +/* By: Quinten +#+ */ +/* +#+ */ +/* Created: 2025/02/23 12:29:05 by Quinten #+# #+# */ +/* Updated: 2025/02/23 12:29:05 by Quinten ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +# include "redirect.h" + +t_list *redirect_get_inputs(t_list *list) +{ + t_list *current; + t_list *redirects; + t_token *token; + + redirects = NULL; + current = list; + while (current) + { + token = (t_token *)current->content; + if (token->type != T_REDIRECT_IN && token->type != T_HEREDOC) + { + current = current->next; + continue ; + } + if (redirect_is_valid(current, token)) + { + ft_lstadd_front(&redirects, ft_lstnew(redirect_new(token->type, ft_strdup(((t_token *)current->next->content)->value)))); + current = current->next; + continue ; + } + else + { + ft_lstadd_front(&redirects, ft_lstnew(redirect_new(T_ERROR, NULL))); + break ; + } + current = current->next; + } + ft_lstiter(redirects, print_redirects); + return (redirects); +} \ No newline at end of file diff --git a/src/redirect/redirect_new.c b/src/redirect/redirect_new.c new file mode 100644 index 0000000..4983828 --- /dev/null +++ b/src/redirect/redirect_new.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* redirect_new.c :+: :+: */ +/* +:+ */ +/* By: Quinten +#+ */ +/* +#+ */ +/* Created: 2025/02/23 12:27:33 by Quinten #+# #+# */ +/* Updated: 2025/02/23 12:27:33 by Quinten ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +t_redirect *redirect_new(t_token_type type, char *value) +{ + t_redirect *result; + + result = ft_malloc_safe(sizeof(t_redirect)); + result->type = type; + result->value = NULL; + if (value) + result->value = value; + return (result); +} \ No newline at end of file diff --git a/src/redirect/redirect_valid_type.c b/src/redirect/redirect_valid_type.c new file mode 100644 index 0000000..49d0674 --- /dev/null +++ b/src/redirect/redirect_valid_type.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* redirect_valid_type.c :+: :+: */ +/* +:+ */ +/* By: Quinten +#+ */ +/* +#+ */ +/* Created: 2025/02/23 12:30:18 by Quinten #+# #+# */ +/* Updated: 2025/02/23 12:30:18 by Quinten ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +static int is_redirection(t_token *token) +{ + return (token->type == T_REDIRECT_IN || token->type == T_HEREDOC || + token->type == T_REDIRECT_OUT || token->type == T_APPEND_OUT); +} + +int redirect_is_valid(t_list *lst, t_token *token) +{ + t_token *next; + + if (!lst->next) + return (0); + next = (t_token *)lst->next->content; + if (!next) + return (0); + return (is_redirection(token) && next->type < 3); +} \ No newline at end of file