fix(connec_cmd): handle nested connections correctly

This commit is contained in:
Khaïs COLIN 2025-04-28 15:04:47 +02:00
parent 92d647e33c
commit 65c1eb9d84
4 changed files with 20 additions and 20 deletions

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/07 10:38:55 by khais #+# #+# */ /* Created: 2025/04/07 10:38:55 by khais #+# #+# */
/* Updated: 2025/04/25 17:58:15 by khais ### ########.fr */ /* Updated: 2025/04/28 15:04:45 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,19 +16,15 @@
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
static void connec_and_cmd_execute(t_connec_cmd *cmd, t_minishell *app, static void connec_and_cmd_execute(t_connec_cmd *cmd, t_minishell *app)
bool should_exit)
{ {
(void)should_exit;
cmd_execute(cmd->first, app, false); cmd_execute(cmd->first, app, false);
if (app->last_return_value == 0) if (app->last_return_value == 0)
cmd_execute(cmd->second, app, false); cmd_execute(cmd->second, app, false);
} }
static void connec_or_cmd_execute(t_connec_cmd *cmd, t_minishell *app, static void connec_or_cmd_execute(t_connec_cmd *cmd, t_minishell *app)
bool should_exit)
{ {
(void)should_exit;
cmd_execute(cmd->first, app, false); cmd_execute(cmd->first, app, false);
if (app->last_return_value != 0) if (app->last_return_value != 0)
cmd_execute(cmd->second, app, false); cmd_execute(cmd->second, app, false);
@ -40,9 +36,11 @@ void connec_cmd_execute(t_connec_cmd *cmd, t_minishell *app,
if (cmd == NULL) if (cmd == NULL)
return ; return ;
if (cmd->connector == FT_AND) if (cmd->connector == FT_AND)
connec_and_cmd_execute(cmd, app, should_exit); connec_and_cmd_execute(cmd, app);
if (cmd->connector == FT_OR) if (cmd->connector == FT_OR)
connec_or_cmd_execute(cmd, app, should_exit); connec_or_cmd_execute(cmd, app);
if (cmd->connector == FT_PIPE) if (cmd->connector == FT_PIPE)
connec_pipe_cmd_execute(cmd, app, should_exit); connec_pipe_cmd_execute(cmd, app);
if (should_exit)
exit(app->last_return_value);
} }

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/11 12:01:29 by khais #+# #+# */ /* Created: 2025/04/11 12:01:29 by khais #+# #+# */
/* Updated: 2025/04/16 16:56:55 by khais ### ########.fr */ /* Updated: 2025/04/28 15:04:17 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -46,8 +46,7 @@ static void close_and_wait(t_minishell *app, int pid1, int pid2, int pipefd[2])
do_waitpid(app, pid2); do_waitpid(app, pid2);
} }
void connec_pipe_cmd_execute(t_connec_cmd *cmd, t_minishell *app, void connec_pipe_cmd_execute(t_connec_cmd *cmd, t_minishell *app)
bool should_exit)
{ {
int pid1; int pid1;
int pid2; int pid2;
@ -69,9 +68,5 @@ void connec_pipe_cmd_execute(t_connec_cmd *cmd, t_minishell *app,
cmd_execute(cmd->second, app, true); cmd_execute(cmd->second, app, true);
} }
else else
{
close_and_wait(app, pid1, pid2, pipefd); close_and_wait(app, pid1, pid2, pipefd);
if (should_exit)
exit(app->last_return_value);
}
} }

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/11 11:56:45 by khais #+# #+# */ /* Created: 2025/04/11 11:56:45 by khais #+# #+# */
/* Updated: 2025/04/16 16:56:43 by khais ### ########.fr */ /* Updated: 2025/04/28 15:04:23 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,7 +15,6 @@
# include "../../minishell.h" # include "../../minishell.h"
void connec_pipe_cmd_execute(t_connec_cmd *cmd, t_minishell *app, void connec_pipe_cmd_execute(t_connec_cmd *cmd, t_minishell *app);
bool should_exit);
#endif // CONNEC_PIPE_CMD_EXECUTE_H #endif // CONNEC_PIPE_CMD_EXECUTE_H

View file

@ -1441,4 +1441,12 @@ minishell: infile: No such file or directory
1 1
EOF EOF
when_run <<EOF "nested connections"
(echo hi||pwd)&&(echo hello||pwd)
EOF
expecting <<EOF
hi
hello
EOF
finalize finalize