diff options
Diffstat (limited to 'src/collect_redirs.c')
| -rw-r--r-- | src/collect_redirs.c | 118 |
1 files changed, 67 insertions, 51 deletions
diff --git a/src/collect_redirs.c b/src/collect_redirs.c index 9ac1605..f274053 100644 --- a/src/collect_redirs.c +++ b/src/collect_redirs.c @@ -3,37 +3,42 @@ /* ::: :::::::: */ /* collect_redirs.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/02 13:49:31 by dkaiser #+# #+# */ -/* Updated: 2024/09/17 19:48:48 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 18:39:24 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" +#include <stdlib.h> -static t_token *collect_redir(t_token **tokens, t_redirection *result, - t_token *cur); -static void collect_and_check_redir(t_token **tokens, t_redirection *result, - t_token **cur); -static void set_redir(t_redirection *redir, int type, char *specifier); -static int is_output_redir(int i); +static void collect_and_check_redir(t_redirection *result, + t_token **cur, t_minidata *data, t_token **tokens); +static t_redirection *set_redir(t_redirection *redir, int type, char *spec, + t_env *env); +static int set_heredoc_data(t_token *cur, t_redirection *result, + t_env *env); -t_redirection *collect_redirs(t_token **tokens) +t_redirection *collect_redirs(t_token **tokens, t_env *env, + t_list **create_files) { t_redirection *result; t_token *cur; + t_minidata data; cur = *tokens; result = malloc(sizeof(t_redirection) * 2); if (result == NULL) return (free_tokens(*tokens), NULL); - set_redir(&result[0], 0, NULL); - set_redir(&result[1], 0, NULL); - while (cur != NULL && cur->next != NULL) + set_redir(&result[0], 0, NULL, env); + set_redir(&result[1], 0, NULL, env); + data.create_files = create_files; + data.env = env; + while (cur != NULL) { if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN) - collect_and_check_redir(tokens, result, &cur); + collect_and_check_redir(result, &cur, &data, tokens); else if (cur->type == REDIR_TOKEN) return (free(result), NULL); else @@ -44,56 +49,67 @@ t_redirection *collect_redirs(t_token **tokens) return (result); } -static void collect_and_check_redir(t_token **tokens, t_redirection *result, - t_token **cur) +static void collect_and_check_redir(t_redirection *result, t_token **cur, + t_minidata *data, t_token **tokens) { - int is_redir_only; + char *str; - is_redir_only = 0; - if ((*cur)->previous == NULL && (*cur)->next->next == NULL) - is_redir_only = 1; - *cur = collect_redir(tokens, result, *cur); - if (is_redir_only) - *tokens = NULL; + if ((*cur)->content.redir_type != INPUT_LIMITER) + str = ft_strdup((*cur)->next->content.string); + if ((*cur)->content.redir_type == INPUT_LIMITER) + { + if (!set_heredoc_data(*cur, result, data->env)) + return ; + } + else if ((*cur)->content.redir_type == INPUT_FILE) + q4fc(data->create_files, set_redir(&result[0], INPUT_FILE, + format_string(str, data->env, 0), data->env)); + else if ((*cur)->content.redir_type == OUTPUT_OVERRIDE) + q4fc(data->create_files, set_redir(&result[1], OUTPUT_OVERRIDE, + format_string(str, data->env, 0), data->env)); + else if ((*cur)->content.redir_type == OUTPUT_APPEND) + q4fc(data->create_files, set_redir(&result[1], OUTPUT_APPEND, + format_string(str, data->env, 0), data->env)); + i_love_the_norme(cur, tokens); } -static t_token *collect_redir(t_token **tokens, t_redirection *result, - t_token *cur) +static t_redirection *set_redir(t_redirection *redir, int type, char *spec, + t_env *env) { - set_redir(&result[is_output_redir(cur->content.redir_type)], - cur->content.redir_type, cur->next->content.string); - cur = cur->next; - free_token_and_connect(cur->previous); - if (cur->next != NULL) - { - if (cur->previous == NULL) - *tokens = cur->next; - cur = cur->next; - free_token_and_connect(cur->previous); - } + t_redirection *result; + + redir->type = type; + if (spec != NULL) + redir->specifier = format_string(spec, env, ft_atoi("0")); else + redir->specifier = spec; + if (redir->type != INPUT_LIMITER) { - free_token(cur); - return (NULL); + result = malloc(sizeof(t_redirection)); + if (!result) + return (NULL); + result->type = type; + result->specifier = spec; + return (result); } - return (cur); + return (NULL); } -static void set_redir(t_redirection *redir, int type, char *specifier) +static int set_heredoc_data(t_token *cur, t_redirection *result, t_env *env) { - redir->type = type; - redir->specifier = specifier; -} + char *heredoc_data; -static int is_output_redir(int i) -{ - if (i & (INPUT_FILE | INPUT_LIMITER)) - return (0); - else if (i & (OUTPUT_APPEND | OUTPUT_OVERRIDE)) - return (1); - else + heredoc_data = NULL; + if (cur->content.redir_type == INPUT_LIMITER) { - panic(UNREACHABLE); - return (-1); + heredoc_data = read_heredoc(cur->next->content.string); + if (!heredoc_data) + { + perror("Heredoc allocation failed"); + return (0); + } + set_redir(&result[0], INPUT_LIMITER, heredoc_data, env); } + set_redir(&result[0], INPUT_LIMITER, heredoc_data, env); + return (1); } |
