Skip to content

Commit

Permalink
Pipe remains open at the end of the exec, pipes do not work properly,…
Browse files Browse the repository at this point in the history
… need to refactor to open ALL redirection files, but only keep the last one.
  • Loading branch information
Jean Teissier authored and Jean Teissier committed Jul 14, 2024
1 parent 4aa406e commit 8047ef7
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 32 deletions.
6 changes: 3 additions & 3 deletions include/execution.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jteissie <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/07 16:53:36 by jteissie #+# #+# */
/* Updated: 2024/07/13 19:38:13 by jteissie ### ########.fr */
/* Updated: 2024/07/14 13:50:32 by jteissie ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -32,7 +32,7 @@ int redirect_file(int file_fd[], int mode);

int execute_data(t_parser *parsed_data, char **env);
int execute_commands(t_parser *data, char **envp);
int process_command(t_lex_parser *p, char **envp, int index, t_parser *d);
int redirect_child(int file_fd[], int p_fd[], int index);
int process_command(t_lex_parser *p, char **envp, t_parser *d);
int redirect_child(int file_fd[], int p_fd[], int has_pipe[]);

#endif
10 changes: 4 additions & 6 deletions src/execution/execute_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jteissie <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/07 14:19:59 by jteissie #+# #+# */
/* Updated: 2024/07/14 11:22:32 by jteissie ### ########.fr */
/* Updated: 2024/07/14 13:51:29 by jteissie ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -59,18 +59,16 @@ int execute_commands(t_parser *data, char **envp)
{
int cmd_count;
t_lex_parser *roaming;
int i;

cmd_count = count_commands(data);
i = cmd_count;
roaming = data->node;
while (roaming && i)
while (roaming && cmd_count)
{
if (roaming->type == TK_PARS_CMD)
{
if (process_command(roaming, envp, cmd_count - i, data) == PANIC)
if (process_command(roaming, envp, data) == PANIC)
return (PANIC);
i--;
cmd_count--;
}
roaming = roaming->next;
}
Expand Down
17 changes: 8 additions & 9 deletions src/execution/process_command.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jteissie <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/10 13:07:11 by jteissie #+# #+# */
/* Updated: 2024/07/13 19:38:45 by jteissie ### ########.fr */
/* Updated: 2024/07/14 16:13:47 by jteissie ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -49,12 +49,8 @@ void check_pipes(t_lex_parser *table, int pipe_status[])
pipe_status[1] = TRUE;
}

int open_pipes(t_lex_parser *parsed, int p_fd[])
int open_pipes(t_lex_parser *parsed, int p_fd[], int has_pipe[])
{
int has_pipe[2];

has_pipe[0] = FALSE;
has_pipe[1] = FALSE;
check_pipes(parsed, has_pipe);
if (pipe(p_fd) < 0)
return (-1);
Expand All @@ -76,15 +72,18 @@ int redirect_parent(int p_fd[], int file_fd[])
return (dup_status);
}

int process_command(t_lex_parser *p, char **envp, int index, t_parser *d)
int process_command(t_lex_parser *p, char **envp, t_parser *d)
{
int pipe_fd[2];
int file_fd[2];
int has_pipe[2];
t_cmd_table *cmd_table;
pid_t pid_child;

cmd_table = p->table;
if (open_files(file_fd, p) == -1 || open_pipes(p, pipe_fd) == -1)
has_pipe[0] = FALSE;
has_pipe[1] = FALSE;
if (open_files(file_fd, p) == -1 || open_pipes(p, pipe_fd, has_pipe) == -1)
return (PANIC);
pid_child = fork();
if (redirect_file(file_fd, 0) < 0)
Expand All @@ -93,7 +92,7 @@ int process_command(t_lex_parser *p, char **envp, int index, t_parser *d)
return (PANIC);
if (pid_child == 0)
{
if (redirect_child(file_fd, pipe_fd, index) == PANIC)
if (redirect_child(file_fd, pipe_fd, has_pipe) == PANIC)
handle_error("syscall error in exec child.\n", errno);
execute_cmd(cmd_table->cmd, envp, d);
}
Expand Down
10 changes: 5 additions & 5 deletions src/execution/redirect_child.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
/* By: jteissie <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/12 14:00:07 by jteissie #+# #+# */
/* Updated: 2024/07/12 15:35:03 by jteissie ### ########.fr */
/* Updated: 2024/07/14 13:50:16 by jteissie ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

int redirect_pipe(int p_fd[], int index)
int redirect_pipe(int p_fd[], int has_pipe[])
{
int dup_status;

dup_status = 0;
close(p_fd[0]);
if (index > 0)
if (has_pipe[1] == TRUE)
dup_status += dup2(p_fd[1], STDOUT_FILENO);
close(p_fd[1]);
return (dup_status);
Expand All @@ -44,9 +44,9 @@ int redirect_file(int file_fd[], int mode)
return (dup_status);
}

int redirect_child(int file_fd[], int p_fd[], int index)
int redirect_child(int file_fd[], int p_fd[], int has_pipe[])
{
if (redirect_pipe(p_fd, index) < 0)
if (redirect_pipe(p_fd, has_pipe) < 0)
return (PANIC);
if (redirect_file(file_fd, 1) < 0)
return (PANIC);
Expand Down
14 changes: 7 additions & 7 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jteissie <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/25 13:43:42 by bthomas #+# #+# */
/* Updated: 2024/07/14 13:13:30 by jteissie ### ########.fr */
/* Updated: 2024/07/14 16:09:16 by jteissie ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -77,17 +77,17 @@ int main(int argc, char **argv, char **env)
t_data data;
t_parser parsed_data;
int std_fd[2];
char *prompt;
// char *prompt;

(void)argv;
(void)argc;
init(&data, env);
prompt = get_prompt(NULL);
// prompt = get_prompt(NULL);
parsed_data.node = NULL;
while (1)
{
prompt = get_prompt(prompt);
data.input = readline(prompt);
// prompt = get_prompt(prompt);
data.input = readline("minishell>");
std_fd[0] = dup(STDIN_FILENO);
std_fd[1] = dup(STDOUT_FILENO);
if (std_fd[0] < 0 || std_fd[1] < 0)
Expand All @@ -107,7 +107,7 @@ int main(int argc, char **argv, char **env)
if (std_fd[0] < 0 || std_fd[1] < 0)
return (PANIC);
}
if (prompt)
free(prompt);
// if (prompt)
// free(prompt);
return (lex_clean_exit(&data, 0));
}
4 changes: 2 additions & 2 deletions src/parser/build_redirect_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jteissie <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/08 18:21:54 by jteissie #+# #+# */
/* Updated: 2024/07/12 18:53:50 by jteissie ### ########.fr */
/* Updated: 2024/07/14 13:46:35 by jteissie ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -18,7 +18,7 @@ int get_redir_type(t_token *lexer)
return (TK_PARS_IN);
else if (ft_strncmp(lexer->lexstr, ">", 1) == 0)
return (TK_PARS_OUT);
else if (ft_strncmp(lexer->lexstr, ">>", 1) == 0)
else if (ft_strncmp(lexer->lexstr, ">>", 2) == 0)
return (TK_PARS_OUT_APPEND);
return (TK_INVALID);
}
Expand Down

0 comments on commit 8047ef7

Please sign in to comment.