fix(here_doc): correctly delete here_doc files in all(?) cases

This commit is contained in:
Khaïs COLIN 2025-05-06 17:20:44 +02:00
parent fb92167494
commit 38dfe3c084
12 changed files with 57 additions and 51 deletions

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:14:40 by kcolin #+# #+# */
/* Updated: 2025/05/06 14:23:45 by kcolin ### ########.fr */
/* Updated: 2025/05/06 17:15:25 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
@ -48,6 +48,12 @@ t_minishell *handle_redirections(t_redirect *redirections, t_minishell *app)
if (do_redirection(redirections) == NULL)
{
app->last_return_value = 1;
while (redirections != NULL)
{
if (redirections->type == FT_HEREDOC)
unlink(redirections->redirectee.filename->word);
redirections = redirections->next;
}
return (NULL);
}
redirections = redirections->next;

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* minishell.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/02 11:53:53 by kcolin #+# #+# */
/* Updated: 2025/05/02 17:38:40 by jguelen ### ########.fr */
/* Created: 2025/05/06 17:12:03 by kcolin #+# #+# */
/* Updated: 2025/05/06 17:12:03 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
@ -74,7 +74,7 @@ int main(int argc, char *argv[], char **envp)
free(line);
debug_command(cmd, &app);
retvalue = execute_command(cmd, &app);
cmd_destroy(cmd);
cmd_destroy(cmd, false);
if (retvalue == SUBPROCESS)
break ;
line = get_command(&app);

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/09 16:53:02 by kcolin #+# #+# */
/* Updated: 2025/05/06 16:51:04 by kcolin ### ########.fr */
/* Updated: 2025/05/06 17:10:21 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
@ -35,33 +35,33 @@ void redirect_destroy(t_redirect *redirect, bool delete_file)
}
}
static void connec_cmd_destroy(t_connec_cmd *cmd)
static void connec_cmd_destroy(t_connec_cmd *cmd, bool delete_file)
{
if (cmd == NULL)
return ;
cmd_destroy(cmd->first);
cmd_destroy(cmd->second);
cmd_destroy(cmd->first, delete_file);
cmd_destroy(cmd->second, delete_file);
free(cmd);
}
static void group_cmd_destroy(t_group_cmd *cmd)
static void group_cmd_destroy(t_group_cmd *cmd, bool delete_file)
{
if (cmd == NULL)
return ;
cmd_destroy(cmd->cmd);
redirect_destroy(cmd->redirects, false);
cmd_destroy(cmd->cmd, delete_file);
redirect_destroy(cmd->redirects, delete_file);
free(cmd);
}
void cmd_destroy(t_cmd *cmd)
void cmd_destroy(t_cmd *cmd, bool delete_file)
{
if (cmd == NULL)
return ;
if (cmd->type == FT_CONNECTION)
connec_cmd_destroy(cmd->value.connection);
connec_cmd_destroy(cmd->value.connection, delete_file);
if (cmd->type == FT_GROUP)
group_cmd_destroy(cmd->value.group);
group_cmd_destroy(cmd->value.group, delete_file);
if (cmd->type == FT_SIMPLE)
simple_cmd_destroy(cmd->value.simple);
simple_cmd_destroy(cmd->value.simple, delete_file);
free(cmd);
}

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/09 16:52:31 by kcolin #+# #+# */
/* Updated: 2025/05/06 16:50:26 by kcolin ### ########.fr */
/* Updated: 2025/05/06 17:08:41 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,7 +15,7 @@
# include "../../minishell.h"
void cmd_destroy(t_cmd *cmd);
void cmd_destroy(t_cmd *cmd, bool delete_file);
void redirect_destroy(t_redirect *redirect, bool delete_file);
#endif // CMD_DESTROY_H

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* cmds_parse.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/16 13:11:33 by kcolin #+# #+# */
/* Updated: 2025/05/02 12:49:54 by jguelen ### ########.fr */
/* Created: 2025/05/06 17:09:29 by kcolin #+# #+# */
/* Updated: 2025/05/06 17:10:00 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
@ -29,7 +29,7 @@ t_cmd *minishell_cmds_parse(t_minishell *app, t_wordlist **tokens)
return (NULL);
opt = minishell_opt_cmds_parse(app, tokens, &connec);
if (!opt && ft_errno_get() != FT_ESUCCESS)
return (cmd_destroy(subtree), NULL);
return (cmd_destroy(subtree, true), NULL);
if (!opt)
return (subtree);
while (opt)
@ -42,6 +42,6 @@ t_cmd *minishell_cmds_parse(t_minishell *app, t_wordlist **tokens)
opt = minishell_opt_cmds_parse(app, tokens, &connec);
}
if (ft_errno_get() != FT_ESUCCESS)
return (cmd_destroy(subtree), NULL);
return (cmd_destroy(subtree, true), NULL);
return (list);
}

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* cmd_parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/31 10:28:28 by jguelen #+# #+# */
/* Updated: 2025/05/02 13:00:27 by jguelen ### ########.fr */
/* Created: 2025/05/06 17:10:37 by kcolin #+# #+# */
/* Updated: 2025/05/06 17:10:43 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
@ -69,7 +69,7 @@ t_cmd *minishell_parse(t_minishell *app, char *command_line)
else
parse_error(app, tokens->word);
wordlist_destroy(tokens);
cmd_destroy(root_cmd);
cmd_destroy(root_cmd, true);
return (NULL);
}
return (root_cmd);

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 11:37:35 by kcolin #+# #+# */
/* Updated: 2025/04/15 11:39:41 by kcolin ### ########.fr */
/* Updated: 2025/05/06 17:10:36 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,16 +21,16 @@ t_cmd *connec_reorient_subtree(t_cmd **list, t_cmd **subtree,
*list = cmd_create(FT_CONNECTION);
if (!*list)
{
cmd_destroy(*subtree);
cmd_destroy(*opt);
cmd_destroy(*subtree, true);
cmd_destroy(*opt, true);
return (NULL);
}
(*list)->value.connection = ft_calloc(1, sizeof(t_connec_cmd));
if (!(*list)->value.connection)
{
ft_errno(FT_ENOMEM);
cmd_destroy(*subtree);
cmd_destroy(*opt);
cmd_destroy(*subtree, true);
cmd_destroy(*opt, true);
return (free(*list), NULL);
}
(*list)->type = FT_CONNECTION;

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 10:46:28 by kcolin #+# #+# */
/* Updated: 2025/04/28 14:26:10 by kcolin ### ########.fr */
/* Updated: 2025/05/06 17:11:19 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
@ -26,19 +26,19 @@ t_cmd *minishell_group_cmd_parse(t_minishell *app, t_wordlist **tokens)
subtree = minishell_cmds_parse(app, tokens);
if (!subtree || (*tokens) == NULL
|| (*tokens)->word->token_type != CLOSE_PARENTH_TOKEN)
return (cmd_destroy(subtree), ft_errno(FT_EERRNO), NULL);
return (cmd_destroy(subtree, true), ft_errno(FT_EERRNO), NULL);
worddesc_destroy(wordlist_pop(tokens));
group = cmd_create(FT_GROUP);
if (!group)
return (cmd_destroy(subtree), NULL);
return (cmd_destroy(subtree, true), NULL);
group->value.group = ft_calloc(1, sizeof(t_group_cmd));
if (!group->value.group)
return (cmd_destroy(subtree), NULL);
return (cmd_destroy(subtree, true), NULL);
group->value.group->cmd = subtree;
group->value.group->redirects = minishell_redirect_parse(app, tokens);
if (group->value.group->redirects == NULL
&& ft_errno_get() != FT_ESUCCESS)
return (cmd_destroy(group), NULL);
return (cmd_destroy(group, true), NULL);
return (group);
}

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 11:35:08 by kcolin #+# #+# */
/* Updated: 2025/04/18 09:19:12 by kcolin ### ########.fr */
/* Updated: 2025/05/06 17:10:11 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
@ -28,7 +28,7 @@ t_cmd *minishell_pipeline_parse(t_minishell *app, t_wordlist **tokens)
return (NULL);
opt = minishell_optional_pipeline_parse(app, tokens);
if (!opt && ft_errno_get() != FT_ESUCCESS)
return (cmd_destroy(subtree), NULL);
return (cmd_destroy(subtree, true), NULL);
if (!opt)
return (subtree);
while (opt)
@ -36,12 +36,12 @@ t_cmd *minishell_pipeline_parse(t_minishell *app, t_wordlist **tokens)
if (connec_reorient_subtree(&pipeline, &subtree, &opt, FT_PIPE) == NULL)
{
app->last_return_value = 1;
return (cmd_destroy(subtree),
return (cmd_destroy(subtree, true),
ft_perror("minishell_pipeline_parse"), NULL);
}
opt = minishell_optional_pipeline_parse(app, tokens);
}
if (ft_errno_get() != FT_ESUCCESS)
return (cmd_destroy(subtree), NULL);
return (cmd_destroy(subtree, true), NULL);
return (pipeline);
}

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/06 16:52:05 by kcolin #+# #+# */
/* Updated: 2025/05/06 16:52:05 by kcolin ### ########.fr */
/* Updated: 2025/05/06 17:07:05 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
@ -27,12 +27,12 @@ t_simple_cmd *simple_cmd_from_wordlist(t_wordlist *words)
return (cmd);
}
void simple_cmd_destroy(t_simple_cmd *cmd)
void simple_cmd_destroy(t_simple_cmd *cmd, bool delete_file)
{
if (cmd == NULL)
return ;
wordlist_destroy(cmd->words);
redirect_destroy(cmd->redirections, false);
redirect_destroy(cmd->redirections, delete_file);
free(cmd);
}

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/21 12:24:57 by kcolin #+# #+# */
/* Updated: 2025/04/10 15:33:10 by kcolin ### ########.fr */
/* Updated: 2025/05/06 17:11:01 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,7 +19,7 @@
# include "../../minishell.h"
t_simple_cmd *simple_cmd_from_wordlist(t_wordlist *words);
void simple_cmd_destroy(t_simple_cmd *cmd);
void simple_cmd_destroy(t_simple_cmd *cmd, bool delete_file);
void simple_cmd_debug(t_simple_cmd *cmd, t_buffer *leader,
bool is_last);
void simple_cmd_root_debug(t_simple_cmd *cmd);

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 10:38:47 by kcolin #+# #+# */
/* Updated: 2025/04/28 14:30:35 by kcolin ### ########.fr */
/* Updated: 2025/05/06 17:11:41 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
@ -41,19 +41,19 @@ t_cmd *minishell_simple_cmd_parse(t_minishell *app, t_wordlist **tokens)
redir = minishell_redirect_parse(app, tokens);
t_redirect_add_back(&simple->value.simple->redirections, redir);
if (ft_errno_get() != FT_ESUCCESS)
return (cmd_destroy(simple), NULL);
return (cmd_destroy(simple, true), NULL);
while ((*tokens) && (*tokens)->word->token_type == WORD_TOKEN)
{
simple->value.simple->words = wordlist_push(simple->value.simple->words,
wordlist_pop(tokens));
if (!simple->value.simple->words)
return (ft_errno(FT_EERRNO), cmd_destroy(simple), NULL);
return (ft_errno(FT_EERRNO), cmd_destroy(simple, true), NULL);
redir = minishell_redirect_parse(app, tokens);
if (redir == NULL && ft_errno_get() != FT_ESUCCESS)
return (cmd_destroy(simple), NULL);
return (cmd_destroy(simple, true), NULL);
t_redirect_add_back(&simple->value.simple->redirections, redir);
}
if (!simple->value.simple->words && !simple->value.simple->redirections)
return (cmd_destroy(simple), ft_errno(FT_EERRNO), NULL);
return (cmd_destroy(simple, true), ft_errno(FT_EERRNO), NULL);
return (simple);
}