diff --git a/include/execution.h b/include/execution.h index b49b5f0..5350f31 100644 --- a/include/execution.h +++ b/include/execution.h @@ -6,7 +6,7 @@ /* By: jteissie +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -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 diff --git a/src/execution/execute_commands.c b/src/execution/execute_commands.c index 6c00760..ec8de8c 100644 --- a/src/execution/execute_commands.c +++ b/src/execution/execute_commands.c @@ -6,7 +6,7 @@ /* By: jteissie +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -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; } diff --git a/src/execution/process_command.c b/src/execution/process_command.c index 7b39723..fbd6615 100644 --- a/src/execution/process_command.c +++ b/src/execution/process_command.c @@ -6,7 +6,7 @@ /* By: jteissie +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -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); @@ -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) @@ -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); } diff --git a/src/execution/redirect_child.c b/src/execution/redirect_child.c index e223c73..ae260ba 100644 --- a/src/execution/redirect_child.c +++ b/src/execution/redirect_child.c @@ -6,19 +6,19 @@ /* By: jteissie +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); @@ -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); diff --git a/src/main.c b/src/main.c index 43d0c36..19c9c9a 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: jteissie +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -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) @@ -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)); } diff --git a/src/parser/build_redirect_table.c b/src/parser/build_redirect_table.c index 103c973..f5113c6 100644 --- a/src/parser/build_redirect_table.c +++ b/src/parser/build_redirect_table.c @@ -6,7 +6,7 @@ /* By: jteissie +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -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); }