diff --git a/src/redirect/redirect_process_heredoc.c b/src/redirect/redirect_process_heredoc.c index 56c3789..f5799b1 100644 --- a/src/redirect/redirect_process_heredoc.c +++ b/src/redirect/redirect_process_heredoc.c @@ -6,7 +6,7 @@ /* By: qmennen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/26 16:46:32 by qmennen #+# #+# */ -/* Updated: 2025/03/18 14:32:19 by qmennen ### ########.fr */ +/* Updated: 2025/03/18 14:40:51 by qmennen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,10 +52,31 @@ int fork_for_heredoc(t_minishell *msh, t_token *heredoc, t_token *delim) return (0); } +static int process_heredoc_line(t_minishell *msh, int fd, char *delim) +{ + char *line; + char *expand; + + line = readline(">"); + if (!line) + { + error_msg("warning: here-document delimited by end-of-file, wanted", + delim); + return (FAILURE); + } + if ((*line && ft_strcmp(line, delim) == 0)) + return (free(line), FAILURE); + 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); + return (SUCCESS); +} + 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) @@ -65,23 +86,11 @@ int process_heredoc(t_minishell *msh, t_token *heredoc, t_token *delim) } while (TRUE) { - line = readline(">"); - if (!line) - { - error_msg("warning: here-document delimited by end-of-file, wanted", delim->value); - break; - } - if ((*line && ft_strcmp(line, delim->value) == 0)) + if (! process_heredoc_line(msh, fd, delim->value)) 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); + return (SUCCESS); }