Compare commits

..

3 commits

27 changed files with 139 additions and 133 deletions

2
.gitignore vendored
View file

@ -39,3 +39,5 @@ fuzz
fuzz_hand_tester fuzz_hand_tester
*CORPUS *CORPUS
crash-* crash-*
test.sh
std.supp

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/04 19:50:42 by kcolin #+# #+# */ /* Created: 2025/04/04 19:50:42 by kcolin #+# #+# */
/* Updated: 2025/04/30 14:06:24 by kcolin ### ########.fr */ /* Updated: 2025/05/06 14:26:49 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,14 +21,12 @@
t_subprocess group_cmd_execute(t_group_cmd *cmd, t_minishell *app) t_subprocess group_cmd_execute(t_group_cmd *cmd, t_minishell *app)
{ {
int pid; int pid;
t_std_fds fds;
pid = fork(); pid = fork();
if (pid == 0) if (pid == 0)
{ {
handle_redirections(cmd->redirects, app, &fds); handle_redirections(cmd->redirects, app);
cmd_execute(cmd->cmd, app); cmd_execute(cmd->cmd, app);
restore_std_fds(&fds);
return (SUBPROCESS); return (SUBPROCESS);
} }
if (pid < 0) if (pid < 0)

View file

@ -5,12 +5,13 @@
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/01 16:37:21 by kcolin #+# #+# */ /* Created: 2025/05/06 14:48:26 by kcolin #+# #+# */
/* Updated: 2025/04/29 15:54:21 by kcolin ### ########.fr */ /* Updated: 2025/05/06 14:49:50 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "builtins.h" #include "builtins.h"
#include "handle_redirections.h"
#include "libft.h" #include "libft.h"
#include "simple_cmd_execute.h" #include "simple_cmd_execute.h"
#include "std_fds.h" #include "std_fds.h"
@ -40,8 +41,9 @@ t_builtin_type get_builtin(t_simple_cmd *cmd)
} }
t_subprocess execute_builtin(t_simple_cmd *cmd, t_minishell *app, t_subprocess execute_builtin(t_simple_cmd *cmd, t_minishell *app,
t_std_fds *fds, t_builtin_type type) t_builtin_type type)
{ {
t_std_fds fds;
t_subprocess retvalue; t_subprocess retvalue;
static t_subprocess (*builtins[])(t_simple_cmd *, t_minishell *) = { static t_subprocess (*builtins[])(t_simple_cmd *, t_minishell *) = {
[BUILTIN_INVALID] = builtin_invalid, [BUILTIN_INVALID] = builtin_invalid,
@ -54,8 +56,13 @@ t_subprocess execute_builtin(t_simple_cmd *cmd, t_minishell *app,
[BUILTIN_UNSET] = builtin_unset, [BUILTIN_UNSET] = builtin_unset,
}; };
std_fds_save(&fds);
if (handle_redirections(cmd->redirections, app) == NULL)
{
restore_std_fds(&fds);
return (PARENTPROCESS);
}
retvalue = builtins[type](cmd, app); retvalue = builtins[type](cmd, app);
if (type != BUILTIN_INVALID) restore_std_fds(&fds);
restore_std_fds(fds);
return (retvalue); return (retvalue);
} }

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/31 14:16:13 by kcolin #+# #+# */ /* Created: 2025/03/31 14:16:13 by kcolin #+# #+# */
/* Updated: 2025/04/29 15:46:28 by kcolin ### ########.fr */ /* Updated: 2025/05/06 14:49:59 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,6 +26,6 @@ t_subprocess builtin_unset(t_simple_cmd *cmd, t_minishell *app);
t_builtin_type get_builtin(t_simple_cmd *cmd); t_builtin_type get_builtin(t_simple_cmd *cmd);
t_subprocess execute_builtin(t_simple_cmd *cmd, t_minishell *app, t_subprocess execute_builtin(t_simple_cmd *cmd, t_minishell *app,
t_std_fds *fds, t_builtin_type type); t_builtin_type type);
#endif // BUILTINS_H #endif // BUILTINS_H

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:14:40 by kcolin #+# #+# */ /* Created: 2025/04/17 11:14:40 by kcolin #+# #+# */
/* Updated: 2025/04/29 16:55:24 by kcolin ### ########.fr */ /* Updated: 2025/05/06 17:15:25 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -41,16 +41,19 @@ static t_redirect *do_redirection(t_redirect *redirection)
return (redirection); return (redirection);
} }
t_minishell *handle_redirections(t_redirect *redirections, t_minishell *app, t_minishell *handle_redirections(t_redirect *redirections, t_minishell *app)
t_std_fds *fds)
{ {
std_fds_save(fds);
while (redirections != NULL) while (redirections != NULL)
{ {
if (do_redirection(redirections) == NULL) if (do_redirection(redirections) == NULL)
{ {
restore_std_fds(fds);
app->last_return_value = 1; app->last_return_value = 1;
while (redirections != NULL)
{
if (redirections->type == FT_HEREDOC)
unlink(redirections->redirectee.filename->word);
redirections = redirections->next;
}
return (NULL); return (NULL);
} }
redirections = redirections->next; redirections = redirections->next;

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:14:29 by kcolin #+# #+# */ /* Created: 2025/04/17 11:14:29 by kcolin #+# #+# */
/* Updated: 2025/04/28 12:45:17 by kcolin ### ########.fr */ /* Updated: 2025/05/06 14:23:49 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,6 +16,6 @@
# include "../../minishell.h" # include "../../minishell.h"
t_minishell *handle_redirections(t_redirect *redirections, t_minishell *handle_redirections(t_redirect *redirections,
t_minishell *app, t_std_fds *fds); t_minishell *app);
#endif // HANDLE_REDIRECTIONS_H #endif // HANDLE_REDIRECTIONS_H

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* simple_cmd_execute.c :+: :+: :+: */ /* simple_cmd_execute.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/27 16:21:56 by kcolin #+# #+# */ /* Created: 2025/05/06 14:25:45 by kcolin #+# #+# */
/* Updated: 2025/05/02 12:49:22 by jguelen ### ########.fr */ /* Updated: 2025/05/06 14:59:02 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -76,34 +76,31 @@ static t_subprocess fork_fail(t_minishell *app)
return (PARENTPROCESS); return (PARENTPROCESS);
} }
static t_subprocess exec_external_cmd(t_simple_cmd *cmd, t_minishell *app, static t_subprocess exec_external_cmd(t_simple_cmd *cmd, t_minishell *app)
t_std_fds *fds)
{ {
char *exe; char *exe;
int pid; int pid;
if (cmd->words == NULL)
return (restore_std_fds(fds), PARENTPROCESS);
exe = get_cmdpath(cmd->words->word->word, app);
if (exe != NULL)
{
pid = fork(); pid = fork();
if (pid == 0) if (pid == 0)
return (execute_subprocess(exe, cmd, app, fds)); {
free(exe); if (handle_redirections(cmd->redirections, app) == NULL)
return (SUBPROCESS);
if (cmd->words == NULL)
return (SUBPROCESS);
exe = get_cmdpath(cmd->words->word->word, app);
if (exe == NULL)
return (command_not_found(cmd, app), SUBPROCESS);
return (execute_subprocess(exe, cmd, app));
}
if (pid < 0) if (pid < 0)
return (fork_fail(app)); return (fork_fail(app));
do_waitpid(app, pid); do_waitpid(app, pid);
}
restore_std_fds(fds);
if (exe == NULL)
command_not_found(cmd, app);
return (PARENTPROCESS); return (PARENTPROCESS);
} }
t_subprocess simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app) t_subprocess simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app)
{ {
t_std_fds fds;
t_builtin_type type; t_builtin_type type;
if (cmd == NULL) if (cmd == NULL)
@ -116,10 +113,8 @@ t_subprocess simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app)
return (PARENTPROCESS); return (PARENTPROCESS);
} }
simple_cmd_execute_debug(cmd, app); simple_cmd_execute_debug(cmd, app);
if (handle_redirections(cmd->redirections, app, &fds) == NULL)
return (PARENTPROCESS);
type = get_builtin(cmd); type = get_builtin(cmd);
if (type != BUILTIN_INVALID) if (type != BUILTIN_INVALID)
return (execute_builtin(cmd, app, &fds, type)); return (execute_builtin(cmd, app, type));
return (exec_external_cmd(cmd, app, &fds)); return (exec_external_cmd(cmd, app));
} }

View file

@ -5,8 +5,8 @@
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/02 18:19:23 by kcolin #+# #+# */ /* Created: 2025/05/06 14:50:26 by kcolin #+# #+# */
/* Updated: 2025/04/29 15:14:32 by kcolin ### ########.fr */ /* Updated: 2025/05/06 14:50:26 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -72,9 +72,8 @@ static int ft_execve(char *exe, char **argv, char **envp)
} }
t_subprocess execute_subprocess(char *exe, t_simple_cmd *cmd, t_subprocess execute_subprocess(char *exe, t_simple_cmd *cmd,
t_minishell *app, t_std_fds *fds) t_minishell *app)
{ {
std_fds_close(fds);
app->last_return_value = ft_execve(exe, argv_from_wordlist(cmd->words), app->last_return_value = ft_execve(exe, argv_from_wordlist(cmd->words),
envp_from_env(app->env)); envp_from_env(app->env));
return (SUBPROCESS); return (SUBPROCESS);

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/02 18:17:48 by kcolin #+# #+# */ /* Created: 2025/04/02 18:17:48 by kcolin #+# #+# */
/* Updated: 2025/04/29 15:14:09 by kcolin ### ########.fr */ /* Updated: 2025/05/06 14:50:33 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,6 +16,6 @@
# include "../../minishell.h" # include "../../minishell.h"
t_subprocess execute_subprocess(char *exe, t_simple_cmd *cmd, t_subprocess execute_subprocess(char *exe, t_simple_cmd *cmd,
t_minishell *app, t_std_fds *fds); t_minishell *app);
#endif // SUBPROCESS_H #endif // SUBPROCESS_H

View file

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

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/09 16:53:02 by kcolin #+# #+# */ /* Created: 2025/04/09 16:53:02 by kcolin #+# #+# */
/* Updated: 2025/05/05 11:22:40 by kcolin ### ########.fr */ /* Updated: 2025/05/06 17:10:21 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,7 +15,7 @@
#include "../simple_cmd/simple_cmd.h" #include "../simple_cmd/simple_cmd.h"
#include "libft.h" #include "libft.h"
void redirect_destroy(t_redirect *redirect) void redirect_destroy(t_redirect *redirect, bool delete_file)
{ {
t_redirect *next; t_redirect *next;
@ -25,7 +25,8 @@ void redirect_destroy(t_redirect *redirect)
free(redirect->here_doc_eof); free(redirect->here_doc_eof);
if (redirect->type == FT_HEREDOC if (redirect->type == FT_HEREDOC
&& redirect->redirectee.filename != NULL && redirect->redirectee.filename != NULL
&& redirect->redirectee.filename->word != NULL) && redirect->redirectee.filename->word != NULL
&& delete_file)
unlink(redirect->redirectee.filename->word); unlink(redirect->redirectee.filename->word);
worddesc_destroy(redirect->redirectee.filename); worddesc_destroy(redirect->redirectee.filename);
free(redirect->unexpanded_filename); free(redirect->unexpanded_filename);
@ -34,33 +35,33 @@ void redirect_destroy(t_redirect *redirect)
} }
} }
static void connec_cmd_destroy(t_connec_cmd *cmd) static void connec_cmd_destroy(t_connec_cmd *cmd, bool delete_file)
{ {
if (cmd == NULL) if (cmd == NULL)
return ; return ;
cmd_destroy(cmd->first); cmd_destroy(cmd->first, delete_file);
cmd_destroy(cmd->second); cmd_destroy(cmd->second, delete_file);
free(cmd); 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) if (cmd == NULL)
return ; return ;
cmd_destroy(cmd->cmd); cmd_destroy(cmd->cmd, delete_file);
redirect_destroy(cmd->redirects); redirect_destroy(cmd->redirects, delete_file);
free(cmd); free(cmd);
} }
void cmd_destroy(t_cmd *cmd) void cmd_destroy(t_cmd *cmd, bool delete_file)
{ {
if (cmd == NULL) if (cmd == NULL)
return ; return ;
if (cmd->type == FT_CONNECTION) if (cmd->type == FT_CONNECTION)
connec_cmd_destroy(cmd->value.connection); connec_cmd_destroy(cmd->value.connection, delete_file);
if (cmd->type == FT_GROUP) if (cmd->type == FT_GROUP)
group_cmd_destroy(cmd->value.group); group_cmd_destroy(cmd->value.group, delete_file);
if (cmd->type == FT_SIMPLE) if (cmd->type == FT_SIMPLE)
simple_cmd_destroy(cmd->value.simple); simple_cmd_destroy(cmd->value.simple, delete_file);
free(cmd); free(cmd);
} }

View file

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

View file

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

View file

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

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 11:37:35 by kcolin #+# #+# */ /* 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); *list = cmd_create(FT_CONNECTION);
if (!*list) if (!*list)
{ {
cmd_destroy(*subtree); cmd_destroy(*subtree, true);
cmd_destroy(*opt); cmd_destroy(*opt, true);
return (NULL); return (NULL);
} }
(*list)->value.connection = ft_calloc(1, sizeof(t_connec_cmd)); (*list)->value.connection = ft_calloc(1, sizeof(t_connec_cmd));
if (!(*list)->value.connection) if (!(*list)->value.connection)
{ {
ft_errno(FT_ENOMEM); ft_errno(FT_ENOMEM);
cmd_destroy(*subtree); cmd_destroy(*subtree, true);
cmd_destroy(*opt); cmd_destroy(*opt, true);
return (free(*list), NULL); return (free(*list), NULL);
} }
(*list)->type = FT_CONNECTION; (*list)->type = FT_CONNECTION;

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 10:46:28 by kcolin #+# #+# */ /* 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); subtree = minishell_cmds_parse(app, tokens);
if (!subtree || (*tokens) == NULL if (!subtree || (*tokens) == NULL
|| (*tokens)->word->token_type != CLOSE_PARENTH_TOKEN) || (*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)); worddesc_destroy(wordlist_pop(tokens));
group = cmd_create(FT_GROUP); group = cmd_create(FT_GROUP);
if (!group) if (!group)
return (cmd_destroy(subtree), NULL); return (cmd_destroy(subtree, true), NULL);
group->value.group = ft_calloc(1, sizeof(t_group_cmd)); group->value.group = ft_calloc(1, sizeof(t_group_cmd));
if (!group->value.group) if (!group->value.group)
return (cmd_destroy(subtree), NULL); return (cmd_destroy(subtree, true), NULL);
group->value.group->cmd = subtree; group->value.group->cmd = subtree;
group->value.group->redirects = minishell_redirect_parse(app, tokens); group->value.group->redirects = minishell_redirect_parse(app, tokens);
if (group->value.group->redirects == NULL if (group->value.group->redirects == NULL
&& ft_errno_get() != FT_ESUCCESS) && ft_errno_get() != FT_ESUCCESS)
return (cmd_destroy(group), NULL); return (cmd_destroy(group, true), NULL);
return (group); return (group);
} }

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 11:35:08 by kcolin #+# #+# */ /* 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); return (NULL);
opt = minishell_optional_pipeline_parse(app, tokens); opt = minishell_optional_pipeline_parse(app, tokens);
if (!opt && ft_errno_get() != FT_ESUCCESS) if (!opt && ft_errno_get() != FT_ESUCCESS)
return (cmd_destroy(subtree), NULL); return (cmd_destroy(subtree, true), NULL);
if (!opt) if (!opt)
return (subtree); return (subtree);
while (opt) 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) if (connec_reorient_subtree(&pipeline, &subtree, &opt, FT_PIPE) == NULL)
{ {
app->last_return_value = 1; app->last_return_value = 1;
return (cmd_destroy(subtree), return (cmd_destroy(subtree, true),
ft_perror("minishell_pipeline_parse"), NULL); ft_perror("minishell_pipeline_parse"), NULL);
} }
opt = minishell_optional_pipeline_parse(app, tokens); opt = minishell_optional_pipeline_parse(app, tokens);
} }
if (ft_errno_get() != FT_ESUCCESS) if (ft_errno_get() != FT_ESUCCESS)
return (cmd_destroy(subtree), NULL); return (cmd_destroy(subtree, true), NULL);
return (pipeline); return (pipeline);
} }

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/02 14:40:34 by kcolin #+# #+# */ /* Created: 2025/05/02 14:40:34 by kcolin #+# #+# */
/* Updated: 2025/05/02 14:40:34 by kcolin ### ########.fr */ /* Updated: 2025/05/06 16:50:52 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -83,7 +83,7 @@ t_redirect *redir_from_words(t_worddesc *operator,
here_doc(spec, STDIN_FILENO, app), 0, NULL, WORD_TOKEN); here_doc(spec, STDIN_FILENO, app), 0, NULL, WORD_TOKEN);
worddesc_destroy(spec); worddesc_destroy(spec);
if (redir->redirectee.filename == NULL) if (redir->redirectee.filename == NULL)
return (redirect_destroy(redir), NULL); return (redirect_destroy(redir, true), NULL);
} }
else else
redir->redirectee.filename = specifier; redir->redirectee.filename = specifier;

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 10:13:58 by kcolin #+# #+# */ /* Created: 2025/04/15 10:13:58 by kcolin #+# #+# */
/* Updated: 2025/04/17 11:45:24 by kcolin ### ########.fr */ /* Updated: 2025/05/06 16:57:51 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -34,11 +34,12 @@ t_redirect *minishell_redirect_parse(t_minishell *app, t_wordlist **tokens)
else else
{ {
worddesc_destroy(redir_operator); worddesc_destroy(redir_operator);
return (ft_errno(FT_EERRNO), redirect_destroy(redir_list), NULL); return (ft_errno(FT_EERRNO), redirect_destroy(redir_list, true),
NULL);
} }
new_redir = redir_from_words(redir_operator, redir_specifier, app); new_redir = redir_from_words(redir_operator, redir_specifier, app);
if (new_redir == NULL && ft_errno_get() != FT_ESUCCESS) if (new_redir == NULL && ft_errno_get() != FT_ESUCCESS)
return (redirect_destroy(redir_list), NULL); return (redirect_destroy(redir_list, true), NULL);
t_redirect_add_back(&redir_list, new_redir); t_redirect_add_back(&redir_list, new_redir);
} }
return (redir_list); return (redir_list);

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* cmdgroup_remove_quotes.c :+: :+: :+: */ /* cmdgroup_remove_quotes.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/20 17:36:20 by kcolin #+# #+# */ /* Created: 2025/05/06 16:50:43 by kcolin #+# #+# */
/* Updated: 2025/05/02 12:53:03 by jguelen ### ########.fr */ /* Updated: 2025/05/06 16:50:43 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -33,10 +33,10 @@ static t_redirect *redirection_remove_quotes(t_redirect *in_list)
current->redirectee.filename = result; current->redirectee.filename = result;
if (current->redirectee.filename == NULL) if (current->redirectee.filename == NULL)
{ {
redirect_destroy(in_list); redirect_destroy(in_list, true);
redirect_destroy(out_list); redirect_destroy(out_list, true);
ft_errno(FT_EERRNO); ft_errno(FT_EERRNO);
return (redirect_destroy(current), NULL); return (redirect_destroy(current, true), NULL);
} }
} }
out_list = t_redirect_add_back(&out_list, current); out_list = t_redirect_add_back(&out_list, current);

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* simple_cmd.c :+: :+: :+: */ /* simple_cmd.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/21 12:30:07 by kcolin #+# #+# */ /* Created: 2025/05/06 16:52:05 by kcolin #+# #+# */
/* Updated: 2025/05/02 12:53:31 by jguelen ### ########.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); return (cmd);
} }
void simple_cmd_destroy(t_simple_cmd *cmd) void simple_cmd_destroy(t_simple_cmd *cmd, bool delete_file)
{ {
if (cmd == NULL) if (cmd == NULL)
return ; return ;
wordlist_destroy(cmd->words); wordlist_destroy(cmd->words);
redirect_destroy(cmd->redirections); redirect_destroy(cmd->redirections, delete_file);
free(cmd); free(cmd);
} }

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/21 12:24:57 by kcolin #+# #+# */ /* 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" # include "../../minishell.h"
t_simple_cmd *simple_cmd_from_wordlist(t_wordlist *words); 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, void simple_cmd_debug(t_simple_cmd *cmd, t_buffer *leader,
bool is_last); bool is_last);
void simple_cmd_root_debug(t_simple_cmd *cmd); void simple_cmd_root_debug(t_simple_cmd *cmd);

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 10:38:47 by kcolin #+# #+# */ /* 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); redir = minishell_redirect_parse(app, tokens);
t_redirect_add_back(&simple->value.simple->redirections, redir); t_redirect_add_back(&simple->value.simple->redirections, redir);
if (ft_errno_get() != FT_ESUCCESS) 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) while ((*tokens) && (*tokens)->word->token_type == WORD_TOKEN)
{ {
simple->value.simple->words = wordlist_push(simple->value.simple->words, simple->value.simple->words = wordlist_push(simple->value.simple->words,
wordlist_pop(tokens)); wordlist_pop(tokens));
if (!simple->value.simple->words) 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); redir = minishell_redirect_parse(app, tokens);
if (redir == NULL && ft_errno_get() != FT_ESUCCESS) 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); t_redirect_add_back(&simple->value.simple->redirections, redir);
} }
if (!simple->value.simple->words && !simple->value.simple->redirections) 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); return (simple);
} }

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/01 13:34:51 by kcolin #+# #+# */ /* Created: 2025/04/01 13:34:51 by kcolin #+# #+# */
/* Updated: 2025/04/28 14:38:29 by kcolin ### ########.fr */ /* Updated: 2025/05/06 16:52:25 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -36,9 +36,9 @@ static t_redirect *redirection_var_expansion(t_redirect *in_list,
if (current->redirectee.filename == NULL if (current->redirectee.filename == NULL
&& ft_errno_get() != FT_ESUCCESS) && ft_errno_get() != FT_ESUCCESS)
{ {
redirect_destroy(in_list); redirect_destroy(in_list, true);
redirect_destroy(out_list); redirect_destroy(out_list, true);
return (redirect_destroy(current), NULL); return (redirect_destroy(current, true), NULL);
} }
} }
out_list = t_redirect_add_back(&out_list, current); out_list = t_redirect_add_back(&out_list, current);

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* expand_wildcard.c :+: :+: :+: */ /* expand_wildcard.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/03 19:28:28 by kcolin #+# #+# */ /* Created: 2025/05/06 16:51:47 by kcolin #+# #+# */
/* Updated: 2025/05/02 13:00:43 by jguelen ### ########.fr */ /* Updated: 2025/05/06 16:52:34 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -30,13 +30,13 @@ static t_redirect *redirect_expand_wildcard_one(t_redirect *current)
if (expansion_result != NULL) if (expansion_result != NULL)
{ {
ambiguous_redirect(current->unexpanded_filename); ambiguous_redirect(current->unexpanded_filename);
redirect_destroy(current); redirect_destroy(current, true);
wordlist_destroy(expansion_result); wordlist_destroy(expansion_result);
return (ft_errno(FT_EERRNO), NULL); return (ft_errno(FT_EERRNO), NULL);
} }
} }
else if (expansion_result == NULL && ft_errno_get() != FT_ESUCCESS) else if (expansion_result == NULL && ft_errno_get() != FT_ESUCCESS)
return (redirect_destroy(current), NULL); return (redirect_destroy(current, true), NULL);
return (current); return (current);
} }
@ -55,8 +55,8 @@ static t_redirect *redirect_expand_wildcards(t_redirect *in_list)
if (redirect_expand_wildcard_one(current) == NULL if (redirect_expand_wildcard_one(current) == NULL
&& ft_errno_get() != FT_ESUCCESS) && ft_errno_get() != FT_ESUCCESS)
{ {
redirect_destroy(in_list); redirect_destroy(in_list, true);
redirect_destroy(out_list); redirect_destroy(out_list, true);
return (NULL); return (NULL);
} }
} }

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/21 11:54:16 by kcolin #+# #+# */ /* Created: 2025/04/21 11:54:16 by kcolin #+# #+# */
/* Updated: 2025/04/23 11:19:26 by kcolin ### ########.fr */ /* Updated: 2025/05/06 16:51:36 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,9 +20,9 @@
static void redirect_fieldsplit_cleanup(t_redirect *in_list, static void redirect_fieldsplit_cleanup(t_redirect *in_list,
t_redirect *out_list, t_redirect *current, t_simple_cmd *cmd) t_redirect *out_list, t_redirect *current, t_simple_cmd *cmd)
{ {
redirect_destroy(in_list); redirect_destroy(in_list, true);
redirect_destroy(out_list); redirect_destroy(out_list, true);
redirect_destroy(current); redirect_destroy(current, true);
cmd->redirections = NULL; cmd->redirections = NULL;
} }

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* sig_handlers.c :+: :+: :+: */ /* sig_handlers.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 12:01:39 by kcolin #+# #+# */ /* Created: 2025/05/06 16:57:25 by kcolin #+# #+# */
/* Updated: 2025/05/05 12:03:03 by jguelen ### ########.fr */ /* Updated: 2025/05/06 16:57:27 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */