fix pipes error + handle no call error + norm
This commit is contained in:
parent
2ce36c258e
commit
f4392c039f
3 changed files with 63 additions and 68 deletions
|
@ -6,30 +6,58 @@
|
||||||
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
|
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/04/24 13:48:00 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"
|
#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:
|
// Execute a program with specific stdin and stdout:
|
||||||
// - inout[0] is stdin
|
// - inout[0] is stdin
|
||||||
// - inout[0] is stdout
|
// - inout[0] is stdout
|
||||||
// If call.program doesn't start with "./" or "/", find the program in $PATH.
|
// If call.program doesn't start with "./" or "/", find the program in $PATH.
|
||||||
// (TODO)
|
// (TODO)
|
||||||
// If the program wasn't to found in $PATH, or the fork didn't work,
|
// If the program wasn't to found in $PATH, or the fork didn't work,
|
||||||
// write the error (TODO) and return the error status.
|
// write the error and return the error status.
|
||||||
static int execute_call(t_memclass mc, t_call call, const int inout[2],
|
static int execute_call(
|
||||||
|
t_exec_command_global *global, t_call call, const int inout[2],
|
||||||
char *const envp[])
|
char *const envp[])
|
||||||
{
|
{
|
||||||
const char **path = get_path(mc, envp);
|
const char **path = get_path(global->mc, envp);
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
const char *program_path;
|
const char *program_path;
|
||||||
|
|
||||||
program_path = search_path(mc, path, call.program);
|
program_path = search_path(global->mc, path, call.program);
|
||||||
if (!program_path)
|
if (!program_path)
|
||||||
return (minishell_error(str_join(fatal_error, mc,
|
return (minishell_error(str_join(fatal_error, global->mc,
|
||||||
"command not found: ", str_join(fatal_error, mc,
|
"command not found: ", str_join(fatal_error, global->mc,
|
||||||
call.program, "\n"))), 127);
|
call.program, "\n"))), 127);
|
||||||
pid = fork();
|
pid = fork();
|
||||||
if (pid < 0)
|
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)
|
if (dup2(inout[0], 0) < 0 || dup2(inout[1], 1) < 0)
|
||||||
return (minishell_error("errno"), errno);
|
return (minishell_error("errno"), errno);
|
||||||
|
close_all_pipes(global);
|
||||||
execve(program_path, call.argv, envp);
|
execve(program_path, call.argv, envp);
|
||||||
return (minishell_error("errno"), errno);
|
return (minishell_error("errno"), errno);
|
||||||
}
|
}
|
||||||
return (0);
|
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;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
global->pipes = mem_alloc(
|
||||||
if (pipes[i].pipe[0] != 0)
|
err_remember, global->mc, sizeof(t_pipes) * (global->nb_calls + 1));
|
||||||
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));
|
|
||||||
if (err_get())
|
if (err_get())
|
||||||
return (minishell_error("errno"), errno);
|
return (minishell_error("errno"), errno);
|
||||||
i = 0;
|
i = 0;
|
||||||
(*dest)[i].pipe[0] = command.input_fd;
|
global->pipes[i].pipe[0] = command.input_fd;
|
||||||
while (++i < nb_calls)
|
while (++i < global->nb_calls)
|
||||||
if (pipe((*dest)[i].pipe) < 0)
|
if (pipe(global->pipes[i].pipe) < 0)
|
||||||
return (minishell_error("errno"), errno);
|
return (minishell_error("errno"), errno);
|
||||||
(*dest)[i].pipe[1] = command.output_fd;
|
global->pipes[i].pipe[1] = command.output_fd;
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int exec_each_call(t_memclass mc, t_command command, t_pipes *pipes,
|
static int exec_each_call(t_exec_command_global *global,
|
||||||
char *const envp[])
|
t_command command, char *const envp[])
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int r;
|
int r;
|
||||||
int nb_calls;
|
|
||||||
int inout[2];
|
int inout[2];
|
||||||
|
|
||||||
nb_calls = 0;
|
|
||||||
while (command.calls[nb_calls].program != NULL)
|
|
||||||
nb_calls++;
|
|
||||||
i = -1;
|
i = -1;
|
||||||
while (++i < nb_calls)
|
while (++i < global->nb_calls)
|
||||||
{
|
{
|
||||||
inout[0] = pipes[i].pipe[0];
|
inout[0] = global->pipes[i].pipe[0];
|
||||||
inout[1] = pipes[i + 1].pipe[1];
|
inout[1] = global->pipes[i + 1].pipe[1];
|
||||||
r = execute_call(mc, command.calls[i], inout, envp);
|
r = execute_call(global, command.calls[i], inout, envp);
|
||||||
if (r)
|
if (r)
|
||||||
return (r);
|
return (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[])
|
int execute_command(t_memclass mc, t_command command, char *const envp[])
|
||||||
{
|
{
|
||||||
t_pipes *pipes;
|
t_exec_command_global global;
|
||||||
int nb_calls;
|
int r;
|
||||||
int r;
|
|
||||||
|
|
||||||
nb_calls = 0;
|
global.mc = mc;
|
||||||
while (command.calls[nb_calls].program != NULL)
|
global.nb_calls = 0;
|
||||||
nb_calls++;
|
while (command.calls[global.nb_calls].program != NULL)
|
||||||
r = create_pipes(mc, command, &pipes, nb_calls);
|
global.nb_calls++;
|
||||||
|
if (global.nb_calls == 0)
|
||||||
|
return (minishell_error("no program given"), 1);
|
||||||
|
r = create_pipes(&global, command);
|
||||||
if (r)
|
if (r)
|
||||||
return (r);
|
return (r);
|
||||||
exec_each_call(mc, command, pipes, envp);
|
exec_each_call(&global, command, envp);
|
||||||
close_all_pipes(nb_calls, pipes);
|
close_all_pipes(&global);
|
||||||
while (wait(NULL) != -1)
|
while (wait(NULL) != -1)
|
||||||
;
|
;
|
||||||
if (errno != ECHILD)
|
if (errno != ECHILD)
|
||||||
|
|
14
src/main.c
14
src/main.c
|
@ -6,7 +6,7 @@
|
||||||
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
|
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/04/23 14:33:45 by mcolonna #+# #+# */
|
/* 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;
|
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.
|
// Execute a command from a string.
|
||||||
static int do_command(char *const envp[], const char *str)
|
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);
|
return (command.error);
|
||||||
r = execute_command(mc, command, envp);
|
r = execute_command(mc, command, envp);
|
||||||
mem_freeall(mc);
|
mem_freeall(mc);
|
||||||
close_fds(&command);
|
|
||||||
return (r);
|
return (r);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/04/24 13:47:40 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue