norm
This commit is contained in:
parent
75bbeed855
commit
d165229b59
2 changed files with 67 additions and 46 deletions
|
@ -6,7 +6,7 @@
|
|||
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/05/16 17:30:01 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/05/16 17:34:49 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -77,7 +77,6 @@ void minishell_error(const char *msg);
|
|||
// Call to write the error and exit the program.
|
||||
void fatal_error(const char *msg);
|
||||
|
||||
|
||||
///// PATH /////
|
||||
|
||||
// Get the PATH values.
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/04/24 13:48:00 by jschaft #+# #+# */
|
||||
/* Updated: 2024/05/16 16:11:30 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/05/16 18:10:48 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -14,7 +14,7 @@
|
|||
|
||||
// Execute the call with stdin = in and stdout = out.
|
||||
// Find the program in $PATH
|
||||
static int execute_call(t_memclass mc, t_call call, int in, int out,
|
||||
static int execute_call(t_memclass mc, t_call call, int inout[2],
|
||||
char *const envp[])
|
||||
{
|
||||
char **const path = get_path(envp);
|
||||
|
@ -24,14 +24,14 @@ static int execute_call(t_memclass mc, t_call call, int in, int out,
|
|||
program_path = search_path(mc, (const char **)path, call.program);
|
||||
if (!program_path)
|
||||
return (minishell_error(str_join(fatal_error, mc,
|
||||
"command not found: ", str_join(fatal_error, mc,
|
||||
call.program, "\n"))), 127);
|
||||
"command not found: ", str_join(fatal_error, mc,
|
||||
call.program, "\n"))), 127);
|
||||
pid = fork();
|
||||
if (pid < 0)
|
||||
return (minishell_error("errno"), errno);
|
||||
if (pid == 0)
|
||||
{
|
||||
if (dup2(in, 0) < 0 || dup2(out, 1) < 0)
|
||||
if (dup2(inout[0], 0) < 0 || dup2(inout[1], 1) < 0)
|
||||
return (minishell_error("errno"), errno);
|
||||
execve(program_path, call.argv, envp);
|
||||
return (minishell_error("errno"), errno);
|
||||
|
@ -39,39 +39,10 @@ static int execute_call(t_memclass mc, t_call call, int in, int out,
|
|||
return (0);
|
||||
}
|
||||
|
||||
int execute_command(t_memclass mc, t_command command, char *const envp[])
|
||||
void close_all_pipes(int nb_calls, t_pipes *pipes)
|
||||
{
|
||||
t_pipes *pipes;
|
||||
int i;
|
||||
int nb_calls;
|
||||
int r;
|
||||
int i;
|
||||
|
||||
// get nb calls
|
||||
nb_calls = 0;
|
||||
while (command.calls[nb_calls].program != NULL)
|
||||
nb_calls++;
|
||||
|
||||
// create pipes
|
||||
pipes = mem_alloc(err_remember, mc, sizeof(t_pipes) * (nb_calls + 1));
|
||||
if (err_get())
|
||||
return (minishell_error("errno"), errno);
|
||||
i = 0;
|
||||
pipes[i].pipe[0] = command.input_fd;
|
||||
while (++i < nb_calls)
|
||||
if (pipe(pipes[i].pipe) < 0)
|
||||
return (minishell_error("errno"), errno);
|
||||
pipes[i].pipe[1] = command.output_fd;
|
||||
|
||||
// call all programs
|
||||
i = -1;
|
||||
while (++i < nb_calls)
|
||||
{
|
||||
r = execute_call(mc, command.calls[i], pipes[i].pipe[0], pipes[i + 1].pipe[1], envp);
|
||||
if (r)
|
||||
return (r);
|
||||
}
|
||||
|
||||
// close all
|
||||
i = 0;
|
||||
if (pipes[i].pipe[0] != 0)
|
||||
if (close(pipes[i].pipe[0]) < 0)
|
||||
|
@ -86,15 +57,66 @@ int execute_command(t_memclass mc, t_command command, char *const envp[])
|
|||
if (pipes[i].pipe[1] != 1)
|
||||
if (close(pipes[i].pipe[1]) < 0)
|
||||
minishell_error("errno");
|
||||
}
|
||||
|
||||
// wait the end of all programs
|
||||
i = nb_calls;
|
||||
tf_printf("before\n");
|
||||
while (wait(NULL) != -1)
|
||||
tf_printf("in\n");
|
||||
tf_printf("after\n"); // TODO debug
|
||||
if (errno != ECHILD)
|
||||
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())
|
||||
return (minishell_error("errno"), errno);
|
||||
i = 0;
|
||||
(*dest)[i].pipe[0] = command.input_fd;
|
||||
while (++i < nb_calls)
|
||||
if (pipe((*dest)[i].pipe) < 0)
|
||||
return (minishell_error("errno"), errno);
|
||||
(*dest)[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[])
|
||||
{
|
||||
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)
|
||||
{
|
||||
inout[0] = pipes[i].pipe[0];
|
||||
inout[1] = pipes[i + 1].pipe[1];
|
||||
r = execute_call(mc, command.calls[i], inout, envp);
|
||||
if (r)
|
||||
return (r);
|
||||
}
|
||||
return (r);
|
||||
}
|
||||
|
||||
// TODO fix error blocks when pipe
|
||||
int execute_command(t_memclass mc, t_command command, char *const envp[])
|
||||
{
|
||||
t_pipes *pipes;
|
||||
int nb_calls;
|
||||
int r;
|
||||
|
||||
nb_calls = 0;
|
||||
while (command.calls[nb_calls].program != NULL)
|
||||
nb_calls++;
|
||||
r = create_pipes(mc, command, &pipes, nb_calls);
|
||||
if (r)
|
||||
return (r);
|
||||
exec_each_call(mc, command, pipes, envp);
|
||||
close_all_pipes(nb_calls, pipes);
|
||||
while (wait(NULL) != -1)
|
||||
;
|
||||
if (errno != ECHILD)
|
||||
return (minishell_error("errno"), errno);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue