diff options
Diffstat (limited to 'src/collect_redirs.c')
| -rw-r--r-- | src/collect_redirs.c | 147 |
1 files changed, 68 insertions, 79 deletions
diff --git a/src/collect_redirs.c b/src/collect_redirs.c index be0e00f..67ab8f8 100644 --- a/src/collect_redirs.c +++ b/src/collect_redirs.c @@ -6,121 +6,110 @@ /* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/02 13:49:31 by dkaiser #+# #+# */ -/* Updated: 2025/01/13 09:52:00 by chuhlig ### ########.fr */ +/* Updated: 2025/01/21 20:19:48 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" +#include <stdlib.h> -static void collect_and_check_redir(t_redirection *result, t_token **cur); -static void set_redir(t_redirection *redir, int type, char *specifier); +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); -static char *read_heredoc(char *delimiter) -{ - char *line; - char *result; - char *temp; - size_t total_length; - size_t line_length; - - total_length = 0; - result = NULL; - while (1) - { - line = readline("> "); - if (!line || ft_strcmp(line, delimiter) == 0) - { - free(line); - break ; - } - line_length = ft_strlen(line) + 1; - temp = malloc(total_length + line_length + 1); - if (!temp) - { - perror("malloc"); - free(result); - return (NULL); - } - if (result) - { - ft_strcpy(temp, result); - free(result); - } - else - { - temp[0] = '\0'; - } - ft_strcat(temp, line); - ft_strcat(temp, "\n"); - result = temp; - total_length += line_length; - free(line); - } - return (result); -} - -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) + free(set_redir(&result[0], 0, NULL, env)); + free(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(result, &cur); + collect_and_check_redir(result, &cur, &data, tokens); else if (cur->type == REDIR_TOKEN) return (free(result), NULL); else cur = cur->next; } - if (cur && cur->type == REDIR_TOKEN) - return (free(result), NULL); return (result); } -static void set_redir(t_redirection *redir, int type, char *specifier) +static void collect_and_check_redir(t_redirection *result, t_token **cur, + t_minidata *data, t_token **tokens) { + char *str; + + 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_redirection *set_redir(t_redirection *redir, int type, char *spec, + t_env *env) +{ + t_redirection *result; + redir->type = type; - redir->specifier = specifier; + // if (redir->specifier != NULL) + // free(redir->specifier); + if (spec != NULL) + redir->specifier = format_string(spec, env, ft_atoi("0")); + else + redir->specifier = spec; + if (redir->type != INPUT_LIMITER) + { + result = malloc(sizeof(t_redirection)); + if (!result) + return (NULL); + result->type = type; + result->specifier = spec; + return (result); + } + return (NULL); } -static void collect_and_check_redir(t_redirection *result, t_token **cur) +static int set_heredoc_data(t_token *cur, t_redirection *result, t_env *env) { char *heredoc_data; - t_token *next_token; heredoc_data = NULL; - if ((*cur)->content.redir_type == INPUT_LIMITER) + if (cur->content.redir_type == INPUT_LIMITER) { - heredoc_data = read_heredoc((*cur)->next->content.string); + heredoc_data = read_heredoc(cur->next->content.string); if (!heredoc_data) { perror("Heredoc allocation failed"); - return ; + return (0); } - set_redir(&result[0], INPUT_LIMITER, heredoc_data); - } - else if ((*cur)->content.redir_type == INPUT_FILE) - set_redir(&result[0], INPUT_FILE, ft_strdup((*cur)->next->content.string)); - else if ((*cur)->content.redir_type == OUTPUT_OVERRIDE) - set_redir(&result[1], OUTPUT_OVERRIDE, ft_strdup((*cur)->next->content.string)); - else if ((*cur)->content.redir_type == OUTPUT_APPEND) - set_redir(&result[1], OUTPUT_APPEND, ft_strdup((*cur)->next->content.string)); - else - printf("Unknown redirection type encountered\n"); - next_token = (*cur)->next; - free_token_and_connect(*cur); - if (next_token) - { - *cur = next_token->next; - free_token_and_connect(next_token); + set_redir(&result[0], INPUT_LIMITER, heredoc_data, env); } - else - *cur = NULL; + set_redir(&result[0], INPUT_LIMITER, heredoc_data, env); + return (1); } |
