diff --git a/inc/redirect.h b/inc/redirect.h index 2da3c94..3a1e568 100644 --- a/inc/redirect.h +++ b/inc/redirect.h @@ -1,27 +1,27 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* 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_minishell *msh, t_token_type type, char *value); -void redirect_new_error(t_minishell *msh, t_list **redirects); -void redirect_create(t_minishell *msh, t_list **tokens, t_list **redirects, t_token_type type); -t_list *redirect_get_inputs(t_minishell *msh, t_list *list); -t_list *redirect_get_outputs(t_minishell *msh, t_list *list); -int redirect_is_valid(t_list *lst, t_token *token, int mode); -int redirect_token_type(t_token *token); -int redirect_is_delimiter(t_token *token); -int process_heredoc(t_minishell *msh, t_token *heredoc, t_token *delim); - -#endif +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* redirect.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/23 12:26:29 by Quinten #+# #+# */ +/* Updated: 2025/02/27 18:09:47 by qmennen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef REDIRECT_H +# define REDIRECT_H +# include "minishell.h" + +t_redirect *redirect_new(t_minishell *msh, t_token_type type, char *value); +void redirect_new_error(t_minishell *msh, t_list **redirects, int flag); +int redirect_create(t_minishell *msh, t_list **tokens, t_list **redirects, t_token_type type); +t_list *redirect_get_inputs(t_minishell *msh, t_list *list); +t_list *redirect_get_outputs(t_minishell *msh, t_list *list); +int redirect_is_valid(t_list *lst, t_token *token, int mode); +int redirect_token_type(t_token *token); +int redirect_is_delimiter(t_token *token); +int process_heredoc(t_minishell *msh, t_token *heredoc, t_token *delim); + +#endif diff --git a/src/redirect/redirect_get_inputs.c b/src/redirect/redirect_get_inputs.c index 2eaabd3..75d0a10 100644 --- a/src/redirect/redirect_get_inputs.c +++ b/src/redirect/redirect_get_inputs.c @@ -6,7 +6,7 @@ /* By: qmennen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/23 12:29:05 by Quinten #+# #+# */ -/* Updated: 2025/02/26 17:38:59 by qmennen ### ########.fr */ +/* Updated: 2025/02/27 18:09:15 by qmennen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,10 +39,9 @@ t_list *redirect_get_inputs(t_minishell *msh, t_list *list) } check_heredoc(msh, current, token); flag = redirect_is_valid(current, token, F_OK | R_OK); - redirect_create(msh, ¤t, &redirects, token->type); + flag && (redirect_create(msh, ¤t, &redirects, token->type)); current = current->next; } - if (flag <= 0) - redirect_new_error(msh, &redirects); + redirect_new_error(msh, &redirects, flag); return (redirects); } diff --git a/src/redirect/redirect_get_outputs.c b/src/redirect/redirect_get_outputs.c index 19a4a4e..cc4e4f5 100644 --- a/src/redirect/redirect_get_outputs.c +++ b/src/redirect/redirect_get_outputs.c @@ -1,41 +1,40 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* redirect_get_outputs.c :+: :+: */ -/* +:+ */ -/* By: Quinten +#+ */ -/* +#+ */ -/* Created: 2025/02/23 12:37:24 by Quinten #+# #+# */ -/* Updated: 2025/02/23 12:37:24 by Quinten ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -t_list *redirect_get_outputs(t_minishell *msh, t_list *list) -{ - t_list *current; - t_list *redirects; - t_token *token; - int flag; - - flag = 1; - redirects = NULL; - current = list; - token = (t_token *)current->content; - while (current && flag && !redirect_is_delimiter(token)) - { - token = (t_token *)current->content; - if (token->type != T_REDIRECT_OUT && token->type != T_APPEND_OUT) - { - current = current->next; - continue ; - } - flag = redirect_is_valid(current, token, -1); - redirect_create(msh, ¤t, &redirects, token->type); - current = current->next; - } - if (flag <= 0) - redirect_new_error(msh, &redirects); - return (redirects); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* redirect_get_outputs.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/23 12:37:24 by Quinten #+# #+# */ +/* Updated: 2025/02/27 18:10:57 by qmennen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +t_list *redirect_get_outputs(t_minishell *msh, t_list *list) +{ + t_list *current; + t_list *redirects; + t_token *token; + int flag; + + flag = 1; + redirects = NULL; + current = list; + token = (t_token *)current->content; + while (current && !redirect_is_delimiter(token)) + { + token = (t_token *)current->content; + if (token->type != T_REDIRECT_OUT && token->type != T_APPEND_OUT) + { + current = current->next; + continue; + } + flag = redirect_is_valid(current, token, -1); + flag && (redirect_create(msh, ¤t, &redirects, token->type)); + current = current->next; + } + redirect_new_error(msh, &redirects, flag); + return (redirects); +} diff --git a/src/redirect/redirect_new.c b/src/redirect/redirect_new.c index ee11cf9..79c0951 100644 --- a/src/redirect/redirect_new.c +++ b/src/redirect/redirect_new.c @@ -1,44 +1,45 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* 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_minishell *msh, t_token_type type, char *value) -{ - t_redirect *result; - - result = malloc_safe(msh, sizeof(t_redirect)); - result->type = type; - result->value = NULL; - if (value) - result->value = value; - return (result); -} - -void redirect_create(t_minishell *msh, t_list **tokens, t_list **redirects, -t_token_type type) -{ - t_list *new; - t_redirect *redir; - t_token *file_token; - char *file_name; - - file_token = (t_token *)((*tokens)->next->content); - file_name = NULL; - if (file_token) - file_name = ft_strdup_safe(msh, file_token->value); - redir = redirect_new(msh, type, file_name); - new = ft_lstnew_safe(msh, redir); - ft_lstadd_back(redirects, new); - if (tokens) - *tokens = (*tokens)->next; -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* redirect_new.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/23 12:27:33 by Quinten #+# #+# */ +/* Updated: 2025/02/27 18:11:25 by qmennen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +t_redirect *redirect_new(t_minishell *msh, t_token_type type, char *value) +{ + t_redirect *result; + + result = malloc_safe(msh, sizeof(t_redirect)); + result->type = type; + result->value = NULL; + if (value) + result->value = value; + return (result); +} + +int redirect_create(t_minishell *msh, t_list **tokens, t_list **redirects, +t_token_type type) +{ + t_list *new; + t_redirect *redir; + t_token *file_token; + char *file_name; + + file_token = (t_token *)((*tokens)->next->content); + file_name = NULL; + if (file_token) + file_name = ft_strdup_safe(msh, file_token->value); + redir = redirect_new(msh, type, file_name); + new = ft_lstnew_safe(msh, redir); + ft_lstadd_back(redirects, new); + if (tokens) + *tokens = (*tokens)->next; + return (1); +} diff --git a/src/redirect/redirect_new_error.c b/src/redirect/redirect_new_error.c index 076e062..24cf75c 100644 --- a/src/redirect/redirect_new_error.c +++ b/src/redirect/redirect_new_error.c @@ -6,17 +6,19 @@ /* By: qmennen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/26 17:37:12 by qmennen #+# #+# */ -/* Updated: 2025/02/26 17:44:49 by qmennen ### ########.fr */ +/* Updated: 2025/02/27 18:07:25 by qmennen ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void redirect_new_error(t_minishell *msh, t_list **redirects) +void redirect_new_error(t_minishell *msh, t_list **redirects, int flag) { t_list *new; t_redirect *redir; + if (flag) + return ; redir = redirect_new(msh, T_ERROR, NULL); new = ft_lstnew_safe(msh, redir); ft_lstadd_back(redirects, new);