From 79aeeaa6692c1c2c8282df751ff6fda1ba445883 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Wed, 15 Jan 2025 18:35:29 +0100 Subject: Refactor collect_redirs --- src/read_heredoc.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/read_heredoc.c (limited to 'src/read_heredoc.c') 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); +} -- cgit v1.2.3 From 9f2424c0dca6073d1e97f290ad890a2ad7143ef1 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Wed, 15 Jan 2025 18:55:47 +0100 Subject: Fix collect_redirs and start refactoring heredoc --- src/collect_redirs.c | 16 ++++++++++------ src/read_heredoc.c | 17 ++++++++++++----- 2 files changed, 22 insertions(+), 11 deletions(-) (limited to 'src/read_heredoc.c') diff --git a/src/collect_redirs.c b/src/collect_redirs.c index 2947c52..4decda7 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:33:36 by dkaiser ### ########.fr */ +/* Updated: 2025/01/15 18:54:42 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,8 @@ 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 *spec, t_env *env); -static char *get_heredoc_data(t_token *cur); +static int set_heredoc_data(t_token *cur, t_redirection *result, + t_env *env); t_redirection *collect_redirs(t_token **tokens, t_env *env) { @@ -62,7 +63,8 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur, str = ft_strdup((*cur)->next->content.string); if ((*cur)->content.redir_type == INPUT_LIMITER) { - set_redir(&result[0], INPUT_LIMITER, get_heredoc_data(*cur), env); + if (!set_heredoc_data(*cur, result, env)) + return ; } else if ((*cur)->content.redir_type == INPUT_FILE) set_redir(&result[0], INPUT_FILE, str, env); @@ -81,19 +83,21 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur, *cur = NULL; } -static char *get_heredoc_data(t_token *cur) +static int set_heredoc_data(t_token *cur, t_redirection *result, t_env *env) { char *heredoc_data; + heredoc_data = NULL; if (cur->content.redir_type == INPUT_LIMITER) { 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, env); } - return (heredoc_data); + set_redir(&result[0], INPUT_LIMITER, heredoc_data, env); + return (1); } diff --git a/src/read_heredoc.c b/src/read_heredoc.c index 78efbd7..4b45b14 100644 --- a/src/read_heredoc.c +++ b/src/read_heredoc.c @@ -6,12 +6,14 @@ /* By: dkaiser +#include +#include static char *concat_str(char *temp, char *line); +static char *get_result(char *temp, char *result, char *line); +static void *print_error_and_free(char *result); char *read_heredoc(char *delimiter) { @@ -35,18 +40,8 @@ char *read_heredoc(char *delimiter) 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'; - result = concat_str(temp, line); + return (print_error_and_free(result)); + result = get_result(temp, result, line); total_length += line_length; } return (result); @@ -59,3 +54,23 @@ static char *concat_str(char *temp, char *line) free(line); return (temp); } + +static char *get_result(char *temp, char *result, char *line) +{ + if (result) + { + ft_strcpy(temp, result); + free(result); + } + else + temp[0] = '\0'; + return (concat_str(temp, line)); +} + +static void *print_error_and_free(char *result) +{ + errno = ENOMEM; + perror("heredoc"); + free(result); + return (NULL); +} -- cgit v1.2.3