fix pipes error + handle no call error + norm

This commit is contained in:
mcolonna 2024-05-17 14:44:36 +02:00
parent 2ce36c258e
commit f4392c039f
3 changed files with 63 additions and 68 deletions

View file

@ -6,30 +6,58 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)

View file

@ -6,7 +6,7 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */