From c8806e22c6b40d351e99e6af4c5dbe1b67616015 Mon Sep 17 00:00:00 2001 From: Quinten Mennen Date: Wed, 26 Feb 2025 16:54:33 +0100 Subject: [PATCH] process heredoc new structure --- inc/redirect.h | 1 + sources.mk | 3 +- src/redirect/redirect_get_inputs.c | 49 ++++------------------ src/redirect/redirect_process_heredoc.c | 56 +++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 43 deletions(-) create mode 100644 src/redirect/redirect_process_heredoc.c diff --git a/inc/redirect.h b/inc/redirect.h index 40d315b..4a0cdbb 100644 --- a/inc/redirect.h +++ b/inc/redirect.h @@ -20,5 +20,6 @@ t_list *redirect_get_outputs(t_minishell *msh, t_list *list); int redirect_is_valid(t_list *lst, t_token *token, int mode); int redirect_token_type(t_token *token); int redirect_is_delimiter(t_token *token); +int process_heredoc(t_minishell *msh, t_token *heredoc, t_token *delim); #endif diff --git a/sources.mk b/sources.mk index cc0c5a1..b632b98 100644 --- a/sources.mk +++ b/sources.mk @@ -20,4 +20,5 @@ expander_get_variable.c builtin_cd.c builtin_echo.c builtin_env.c \ builtin_exit.c builtin_export.c builtin_pwd.c builtin_router.c \ builtin_unset.c is_builtin.c simple_builtins.c signal.c \ signal_init.c.c redirect_get_outputs.c redirect_new.c \ -redirect_valid_type.c redirect_get_inputs.c main.c \ +redirect_get_inputs.c redirect_valid_type.c \ +redirect_process_heredoc.c main.c \ diff --git a/src/redirect/redirect_get_inputs.c b/src/redirect/redirect_get_inputs.c index e368e2f..5b8654b 100644 --- a/src/redirect/redirect_get_inputs.c +++ b/src/redirect/redirect_get_inputs.c @@ -1,52 +1,17 @@ /* ************************************************************************** */ /* */ -/* :::::::: */ -/* redirect_get_inputs.c :+: :+: */ -/* +:+ */ -/* By: Quinten +#+ */ -/* +#+ */ -/* Created: 2025/02/23 12:29:05 by Quinten #+# #+# */ -/* Updated: 2025/02/23 12:29:05 by Quinten ######## odam.nl */ +/* ::: :::::::: */ +/* redirect_get_inputs.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/23 12:29:05 by Quinten #+# #+# */ +/* Updated: 2025/02/26 16:46:44 by qmennen ### ########.fr */ /* */ /* ************************************************************************** */ #include "redirect.h" -static int process_heredoc(t_minishell *msh, t_token *heredoc, t_token *delim) -{ - char *line; - char *expand; - int fd; - - fd = open(".ms_heredoc", O_WRONLY | O_CREAT | O_TRUNC, 0644); - if (fd < 0) - { - error_msg("heredoc", "unable to write to temp file"); - return (fd); - } - - while (TRUE && fd > 0) - { - line = readline(">"); - if ((*line && ft_strcmp(line, delim->value) == 0) || !line) // TODO: What if not line? - break; - if (!*line) - ft_putendl_fd("", fd); - else - { - expand = expander_parse_string(line, msh); - ft_putendl_fd(expand, fd); - free_safe(msh, (void **)&expand); - } - free(line); - } - close(fd); - free(line); - heredoc->type = T_REDIRECT_IN; - delim->value = ft_strdup_safe(msh, ".ms_heredoc"); - return (1); -} - t_list *redirect_get_inputs(t_minishell *msh, t_list *list) { t_list *current; diff --git a/src/redirect/redirect_process_heredoc.c b/src/redirect/redirect_process_heredoc.c new file mode 100644 index 0000000..d13aaab --- /dev/null +++ b/src/redirect/redirect_process_heredoc.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* redirect_process_heredoc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/26 16:46:32 by qmennen #+# #+# */ +/* Updated: 2025/02/26 16:54:09 by qmennen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +/** + * process_heredoc - Handles the processing of a heredoc in a minishell. + * @msh: Pointer to the minishell structure. + * @heredoc: Pointer to the token representing the heredoc. + * @delim: Pointer to the token representing the delimiter. + * + * This function reads lines from the standard input until a line matching + * the delimiter is encountered or an end-of-file (EOF) is reached. Each + * line is expanded and written to a temporary file named ".ms_heredoc". + * The function sets the type of the heredoc token to T_REDIRECT_IN and + * updates the delimiter token's value to the temporary file's name. + * + * Return: 1 on success, or a negative value on failure. + */ +int process_heredoc(t_minishell *msh, t_token *heredoc, t_token *delim) +{ + char *line; + char *expand; + const int fd = open(".ms_heredoc", O_WRONLY | O_CREAT | O_TRUNC, 0644); + + if (fd < 0) + { + error_msg("heredoc", "unable to write to temp file"); + return (fd); + } + while (TRUE) + { + line = readline(">"); + if ((*line && ft_strcmp(line, delim->value) == 0) || !line) + break ; + if (!*line) + ft_strlcat(line, "\n", 1); + expand = expander_parse_string(line, msh); + ft_putendl_fd(expand, fd); + free_safe(msh, (void **)&expand); + free(line); + } + close(fd); + heredoc->type = T_REDIRECT_IN; + delim->value = ft_strdup_safe(msh, ".ms_heredoc"); + return (free(line), 1); +}