From f4392c039f873cfde92f7fc576374ceb0a4d1ac2 Mon Sep 17 00:00:00 2001 From: mcolonna Date: Fri, 17 May 2024 14:44:36 +0200 Subject: [PATCH] fix pipes error + handle no call error + norm --- src/exec_command.c | 115 +++++++++++++++++++++++--------------------- src/main.c | 14 +----- src/parse_command.c | 2 +- 3 files changed, 63 insertions(+), 68 deletions(-) diff --git a/src/exec_command.c b/src/exec_command.c index 4edbf1d..a4adfe9 100644 --- a/src/exec_command.c +++ b/src/exec_command.c @@ -6,30 +6,58 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/24 13:48:00 by jschaft #+# #+# */ -/* Updated: 2024/05/16 18:22:42 by mcolonna ### ########.fr */ +/* Updated: 2024/05/17 14:43:37 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ #include "include.h" +typedef struct s_exec_command_global +{ + t_memclass mc; + int nb_calls; + t_pipes *pipes; +} t_exec_command_global; + +static void close_all_pipes(t_exec_command_global *global) +{ + int i; + + i = 0; + if (global->pipes[i].pipe[0] != 0) + if (close(global->pipes[i].pipe[0]) < 0) + minishell_error("errno"); + while (++i < global->nb_calls) + { + if (close(global->pipes[i].pipe[0]) < 0) + minishell_error("errno"); + if (close(global->pipes[i].pipe[1]) < 0) + minishell_error("errno"); + } + if (global->pipes[i].pipe[1] != 1) + if (close(global->pipes[i].pipe[1]) < 0) + minishell_error("errno"); +} + // Execute a program with specific stdin and stdout: // - inout[0] is stdin // - inout[0] is stdout // If call.program doesn't start with "./" or "/", find the program in $PATH. // (TODO) // If the program wasn't to found in $PATH, or the fork didn't work, -// write the error (TODO) and return the error status. -static int execute_call(t_memclass mc, t_call call, const int inout[2], +// write the error and return the error status. +static int execute_call( + t_exec_command_global *global, t_call call, const int inout[2], char *const envp[]) { - const char **path = get_path(mc, envp); + const char **path = get_path(global->mc, envp); pid_t pid; const char *program_path; - program_path = search_path(mc, path, call.program); + program_path = search_path(global->mc, path, call.program); if (!program_path) - return (minishell_error(str_join(fatal_error, mc, - "command not found: ", str_join(fatal_error, mc, + return (minishell_error(str_join(fatal_error, global->mc, + "command not found: ", str_join(fatal_error, global->mc, call.program, "\n"))), 127); pid = fork(); if (pid < 0) @@ -38,87 +66,66 @@ static int execute_call(t_memclass mc, t_call call, const int inout[2], { if (dup2(inout[0], 0) < 0 || dup2(inout[1], 1) < 0) return (minishell_error("errno"), errno); + close_all_pipes(global); execve(program_path, call.argv, envp); return (minishell_error("errno"), errno); } return (0); } -void close_all_pipes(int nb_calls, t_pipes *pipes) +static int create_pipes(t_exec_command_global *global, t_command command) { int i; - i = 0; - if (pipes[i].pipe[0] != 0) - if (close(pipes[i].pipe[0]) < 0) - minishell_error("errno"); - while (++i < nb_calls) - { - if (close(pipes[i].pipe[0]) < 0) - minishell_error("errno"); - if (close(pipes[i].pipe[1]) < 0) - minishell_error("errno"); - } - if (pipes[i].pipe[1] != 1) - if (close(pipes[i].pipe[1]) < 0) - minishell_error("errno"); -} - -static int create_pipes( - t_memclass mc, t_command command, t_pipes **dest, int nb_calls) -{ - int i; - - *dest = mem_alloc(err_remember, mc, sizeof(t_pipes) * (nb_calls + 1)); + global->pipes = mem_alloc( + err_remember, global->mc, sizeof(t_pipes) * (global->nb_calls + 1)); if (err_get()) return (minishell_error("errno"), errno); i = 0; - (*dest)[i].pipe[0] = command.input_fd; - while (++i < nb_calls) - if (pipe((*dest)[i].pipe) < 0) + global->pipes[i].pipe[0] = command.input_fd; + while (++i < global->nb_calls) + if (pipe(global->pipes[i].pipe) < 0) return (minishell_error("errno"), errno); - (*dest)[i].pipe[1] = command.output_fd; + global->pipes[i].pipe[1] = command.output_fd; return (0); } -static int exec_each_call(t_memclass mc, t_command command, t_pipes *pipes, - char *const envp[]) +static int exec_each_call(t_exec_command_global *global, + t_command command, char *const envp[]) { int i; int r; - int nb_calls; int inout[2]; - nb_calls = 0; - while (command.calls[nb_calls].program != NULL) - nb_calls++; i = -1; - while (++i < nb_calls) + while (++i < global->nb_calls) { - inout[0] = pipes[i].pipe[0]; - inout[1] = pipes[i + 1].pipe[1]; - r = execute_call(mc, command.calls[i], inout, envp); + inout[0] = global->pipes[i].pipe[0]; + inout[1] = global->pipes[i + 1].pipe[1]; + r = execute_call(global, command.calls[i], inout, envp); if (r) return (r); } return (r); } -// TODO fix error blocks when pipe +// TODO line jump if command not found int execute_command(t_memclass mc, t_command command, char *const envp[]) { - t_pipes *pipes; - int nb_calls; - int r; + t_exec_command_global global; + int r; - nb_calls = 0; - while (command.calls[nb_calls].program != NULL) - nb_calls++; - r = create_pipes(mc, command, &pipes, nb_calls); + global.mc = mc; + global.nb_calls = 0; + while (command.calls[global.nb_calls].program != NULL) + global.nb_calls++; + if (global.nb_calls == 0) + return (minishell_error("no program given"), 1); + r = create_pipes(&global, command); if (r) return (r); - exec_each_call(mc, command, pipes, envp); - close_all_pipes(nb_calls, pipes); + exec_each_call(&global, command, envp); + close_all_pipes(&global); while (wait(NULL) != -1) ; if (errno != ECHILD) diff --git a/src/main.c b/src/main.c index 432dd22..8efcceb 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/23 14:33:45 by mcolonna #+# #+# */ -/* Updated: 2024/05/16 18:19:37 by mcolonna ### ########.fr */ +/* Updated: 2024/05/17 14:39:43 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,17 +14,6 @@ t_memclass g_mc; -// Close the input and output fds of the command. -static void close_fds(const t_command *command) -{ - if (command->input_fd != 0) - if (close(command->input_fd) == -1) - minishell_error("errno"); - if (command->output_fd != 1) - if (close(command->output_fd) == -1) - minishell_error("errno"); -} - // Execute a command from a string. static int do_command(char *const envp[], const char *str) { @@ -37,7 +26,6 @@ static int do_command(char *const envp[], const char *str) return (command.error); r = execute_command(mc, command, envp); mem_freeall(mc); - close_fds(&command); return (r); } diff --git a/src/parse_command.c b/src/parse_command.c index 2d6dd3b..d29ca12 100644 --- a/src/parse_command.c +++ b/src/parse_command.c @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/24 13:47:40 by mcolonna #+# #+# */ -/* Updated: 2024/05/07 13:59:30 by mcolonna ### ########.fr */ +/* Updated: 2024/05/17 14:41:50 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */