diff options
| author | Dominik Kaiser | 2025-01-15 18:35:29 +0100 |
|---|---|---|
| committer | Dominik Kaiser | 2025-01-15 18:35:29 +0100 |
| commit | 79aeeaa6692c1c2c8282df751ff6fda1ba445883 (patch) | |
| tree | e579137f50465efc1a554ff39b45912c2331069b /src/collect_redirs.c | |
| parent | 29932bf9401f511cfa4b2fbb183bb174ecf13c24 (diff) | |
| download | minishell-79aeeaa6692c1c2c8282df751ff6fda1ba445883.tar.gz minishell-79aeeaa6692c1c2c8282df751ff6fda1ba445883.zip | |
Refactor collect_redirs
Diffstat (limited to 'src/collect_redirs.c')
| -rw-r--r-- | src/collect_redirs.c | 94 |
1 files changed, 31 insertions, 63 deletions
diff --git a/src/collect_redirs.c b/src/collect_redirs.c index 02c866f..2947c52 100644 --- a/src/collect_redirs.c +++ b/src/collect_redirs.c @@ -6,7 +6,7 @@ /* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/02 13:49:31 by dkaiser #+# #+# */ -/* Updated: 2025/01/15 18:10:46 by dkaiser ### ########.fr */ +/* Updated: 2025/01/15 18:33:36 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,49 +14,9 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur, t_env *env); -static void set_redir(t_redirection *redir, int type, char *specifier, +static void set_redir(t_redirection *redir, int type, char *spec, 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"); - return (free(result), 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); -} +static char *get_heredoc_data(t_token *cur); t_redirection *collect_redirs(t_token **tokens, t_env *env) { @@ -83,42 +43,33 @@ t_redirection *collect_redirs(t_token **tokens, t_env *env) return (result); } -static void set_redir(t_redirection *redir, int type, char *specifier, - t_env *env) +static void set_redir(t_redirection *redir, int type, char *spec, t_env *env) { redir->type = type; - if (specifier != NULL) - redir->specifier = format_string(specifier, env); + if (spec != NULL) + redir->specifier = format_string(spec, env); else - redir->specifier = specifier; + redir->specifier = spec; } static void collect_and_check_redir(t_redirection *result, t_token **cur, t_env *env) { - char *heredoc_data; t_token *next_token; + char *str; - heredoc_data = NULL; + if ((*cur)->content.redir_type != INPUT_LIMITER) + str = ft_strdup((*cur)->next->content.string); if ((*cur)->content.redir_type == INPUT_LIMITER) { - heredoc_data = read_heredoc((*cur)->next->content.string); - if (!heredoc_data) - { - perror("Heredoc allocation failed"); - return ; - } - set_redir(&result[0], INPUT_LIMITER, heredoc_data, env); + set_redir(&result[0], INPUT_LIMITER, get_heredoc_data(*cur), env); } else if ((*cur)->content.redir_type == INPUT_FILE) - set_redir(&result[0], INPUT_FILE, - ft_strdup((*cur)->next->content.string), env); + set_redir(&result[0], INPUT_FILE, str, env); else if ((*cur)->content.redir_type == OUTPUT_OVERRIDE) - set_redir(&result[1], OUTPUT_OVERRIDE, - ft_strdup((*cur)->next->content.string), env); + set_redir(&result[1], OUTPUT_OVERRIDE, str, env); else if ((*cur)->content.redir_type == OUTPUT_APPEND) - set_redir(&result[1], OUTPUT_APPEND, - ft_strdup((*cur)->next->content.string), env); + set_redir(&result[1], OUTPUT_APPEND, str, env); next_token = (*cur)->next; free_token_and_connect(*cur); if (next_token) @@ -129,3 +80,20 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur, else *cur = NULL; } + +static char *get_heredoc_data(t_token *cur) +{ + char *heredoc_data; + + if (cur->content.redir_type == INPUT_LIMITER) + { + heredoc_data = read_heredoc(cur->next->content.string); + if (!heredoc_data) + { + perror("Heredoc allocation failed"); + return ; + } + set_redir(&result[0], INPUT_LIMITER, heredoc_data, env); + } + return (heredoc_data); +} |
