diff --git a/sources.mk b/sources.mk index 5ee83b6..cc0c5a1 100644 --- a/sources.mk +++ b/sources.mk @@ -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: -SOURCES = history_load.c history_write.c prompt.c ft_lstclear_safe.c \ -ft_lstnew_safe.c init_minishell.c malloc_safe.c print_banner.c \ -free_minishell_line.c error_msg.c free_command_list.c \ -ft_strdup_safe.c ft_strjoin_safe.c ft_substr_safe.c check_malloc.c \ -free_freelist.c free_lexer.c free_minishell.c free_safe.c \ -free_token.c free_token_list.c lexer_parse_input.c lexer_read_word.c \ -lexer_token_next.c lexer_read_char.c lexer_new.c token_parse.c \ -token_new.c environment_free.c environment_free_list.c \ -environment_print.c environment_add.c environment_del.c \ -environment_get.c environment_get_arr.c environment_parse.c \ -executor_close_fds.c executor_count_fds.c executor_create_pipes.c \ -executor_create_redirects.c executor_execute_pipeline.c \ -executor_fork.c executor_open_fds.c executor_absolute_path.c \ -executor_child.c parser_get_commands.c parser_get_arguments.c \ -parser_new_command.c print_commands.c print_freelist.c \ -expander_is_character.c expander_allocate_memory.c \ -expander_expand_dollar.c expander_get_variable.c \ -expander_parse_string.c expander_parse_variables.c builtin_echo.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 \ -is_builtin.c signal.c redirect_valid_type.c redirect_get_inputs.c \ -redirect_get_outputs.c redirect_new.c main.c \ +SOURCES = history_load.c history_write.c prompt.c print_banner.c \ +init_minishell.c ft_substr_safe.c check_malloc.c error_msg.c \ +free_command_list.c free_freelist.c free_lexer.c free_minishell.c \ +free_minishell_line.c free_safe.c free_token.c free_token_list.c \ +ft_lstclear_safe.c ft_lstnew_safe.c ft_strdup_safe.c \ +ft_strjoin_safe.c malloc_safe.c lexer_read_char.c lexer_new.c \ +lexer_parse_input.c lexer_read_word.c lexer_token_next.c token_new.c \ +token_parse.c environment_add.c environment_del.c environment_free.c \ +environment_free_list.c environment_get.c environment_get_arr.c \ +environment_parse.c environment_print.c executor_close_fds.c \ +executor_absolute_path.c executor_child.c executor_count_fds.c \ +executor_create_pipes.c executor_create_redirects.c \ +executor_execute_pipeline.c executor_fork.c executor_open_fds.c \ +parser_get_arguments.c parser_new_command.c parser_get_commands.c \ +print_commands.c print_freelist.c expander_is_character.c \ +expander_expand_dollar.c expander_allocate_memory.c \ +expander_parse_string.c expander_parse_variables.c \ +expander_get_variable.c builtin_cd.c builtin_echo.c builtin_env.c \ +builtin_exit.c builtin_export.c builtin_pwd.c builtin_router.c \ +builtin_unset.c is_builtin.c simple_builtins.c signal.c \ +signal_init.c.c redirect_get_outputs.c redirect_new.c \ +redirect_valid_type.c redirect_get_inputs.c main.c \ diff --git a/src/expander/expander_get_variable.c b/src/expander/expander_get_variable.c index 980189d..79ff55c 100644 --- a/src/expander/expander_get_variable.c +++ b/src/expander/expander_get_variable.c @@ -21,7 +21,7 @@ t_environment *expander_get_var(const char *s, int idx, t_minishell *msh) i = 0; while (expander_character_valid(s[idx + i])) i++; - name = ft_substr_safe(minishell, s, idx, i); + name = ft_substr_safe(msh, s, idx, i); if (!name || !*name) return (NULL); env = environment_get(msh, name); diff --git a/src/parser/parser_get_commands.c b/src/parser/parser_get_commands.c index 6503c23..44e182f 100644 --- a/src/parser/parser_get_commands.c +++ b/src/parser/parser_get_commands.c @@ -18,7 +18,6 @@ static int is_command_token(t_token *token) return (token->type < 3 || redirect_token_type(token)); } -/* static int validate_redirects(t_list *lst) { t_list *token; @@ -26,13 +25,12 @@ static int validate_redirects(t_list *lst) token = lst; while (token) { - if (((t_token *)token)->type == T_ERROR) + if (((t_redirect *)token->content)->type == T_ERROR) return (0); token = token->next; } return (1); } -*/ t_list *parser_get_commands(t_minishell *msh) { @@ -42,9 +40,9 @@ t_list *parser_get_commands(t_minishell *msh) t_token *token; command_list = NULL; - if (!minishell->tokens) + if (!msh->tokens) return (NULL); - current = minishell->tokens; + current = msh->tokens; while (current) { token = (t_token *) current->content; @@ -52,6 +50,10 @@ t_list *parser_get_commands(t_minishell *msh) command->args = parser_get_arguments(current, msh); command->redirect_in = redirect_get_inputs(msh, current); command->redirect_out = redirect_get_outputs(msh, current); + if (!validate_redirects(command->redirect_in)) + { + break ; + } ft_lstadd_back(&command_list, ft_lstnew_safe(msh, command)); while (current && is_command_token((t_token *)current->content)) current = current->next; diff --git a/src/redirect/redirect_get_inputs.c b/src/redirect/redirect_get_inputs.c index ab494b6..e368e2f 100644 --- a/src/redirect/redirect_get_inputs.c +++ b/src/redirect/redirect_get_inputs.c @@ -19,7 +19,7 @@ static int process_heredoc(t_minishell *msh, t_token *heredoc, t_token *delim) int fd; fd = open(".ms_heredoc", O_WRONLY | O_CREAT | O_TRUNC, 0644); - if (fd < 0) // TODO: Will this work? + if (fd < 0) { error_msg("heredoc", "unable to write to temp file"); return (fd); @@ -67,11 +67,11 @@ t_list *redirect_get_inputs(t_minishell *msh, t_list *list) current = current->next; continue ; } - if (token->type == T_HEREDOC && redirect_is_valid(current, token, F_OK | W_OK)) + if (token->type == T_HEREDOC && redirect_is_valid(current, token, -1)) result = process_heredoc(msh, token, current->next->content); if (redirect_is_valid(current, token, F_OK | R_OK)) { - ft_lstadd_back(&redirects, ft_lstnew_safe(minishell, + ft_lstadd_back(&redirects, ft_lstnew_safe(msh, redirect_new(msh, token->type, ft_strdup_safe(msh, ((t_token *)current->next->content)->value)))); diff --git a/src/redirect/redirect_valid_type.c b/src/redirect/redirect_valid_type.c index 064ba53..dc0e50e 100644 --- a/src/redirect/redirect_valid_type.c +++ b/src/redirect/redirect_valid_type.c @@ -9,17 +9,17 @@ /* Updated: 2025/02/23 12:30:18 by Quinten ######## odam.nl */ /* */ /* ************************************************************************** */ - + #include "minishell.h" - + int redirect_token_type(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, int mode) { t_token *next; @@ -31,8 +31,11 @@ int redirect_is_valid(t_list *lst, t_token *token, int mode) { return (0); } - if (!access(next->value, mode)) + if (mode >= 0 && access(next->value, mode) != 0) + { + error_msg("minishell", "unable to write to temp file"); return (0); + } return (redirect_token_type(token) && next->type < 3); } @@ -43,4 +46,4 @@ int redirect_is_delimiter(t_token *token) || token->type == T_OR || token->type == T_EOF || token->type == T_ERROR); -} +}