From 79aeeaa6692c1c2c8282df751ff6fda1ba445883 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Wed, 15 Jan 2025 18:35:29 +0100 Subject: [PATCH] Refactor collect_redirs --- Makefile | 1 + include/minishell.h | 9 +++-- src/collect_redirs.c | 94 +++++++++++++++----------------------------- src/read_heredoc.c | 54 +++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 67 deletions(-) create mode 100644 src/read_heredoc.c diff --git a/Makefile b/Makefile index 415d924..650f5e4 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,7 @@ SRC := main.c debug_tools.c init.c signal_handling.c repl.c new_token.c \ parse_cmd.c collect_redirs.c print_ast.c interpreter.c env.c \ get_cmd_path.c env_to_strlst.c execute_cmd.c format_string.c \ builtins_part_one.c builtins_part_two.c env_tools.c error.c \ + read_heredoc.c OBJ_DIR := _obj OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o)) diff --git a/include/minishell.h b/include/minishell.h index 2a426cf..3b97f6a 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/22 17:14:49 by dkaiser #+# #+# */ -/* Updated: 2025/01/15 17:20:56 by dkaiser ### ########.fr */ +/* Updated: 2025/01/15 18:24:09 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -42,8 +42,9 @@ int eval(t_node *node, t_env **env); char *get_cmd_path(char *cmd, t_env *env, int *return_code); int execute_cmd(t_cmd *cmd, t_env **env); char *format_string(char *str, t_env *env); -int set_return_code(int return_code, t_env **env); +int set_return_code(int return_code, t_env **env); int handle_redirections(t_redirection *redirs); -void *error(int err_code, char *err_text, int exit_code, int *ret_code); - +void *error(int err_code, char *err_text, int exit_code, + int *ret_code); +char *read_heredoc(char *delimiter); #endif 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 +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); +} diff --git a/src/read_heredoc.c b/src/read_heredoc.c new file mode 100644 index 0000000..78efbd7 --- /dev/null +++ b/src/read_heredoc.c @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* read_heredoc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser "); + 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); +} -- 2.47.2