refactor (heredoc): make norm compliant

This commit is contained in:
Quinten Mennen 2025-03-18 14:41:11 +01:00
parent 1679624f8d
commit c41ea664c0

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}