Commit ae3ed874 authored by abenoit's avatar abenoit
Browse files

changed child exit management from err code to xe flag

parent dcc3d192
......@@ -12,8 +12,6 @@ enum e_retcode
ARG_ERR,
FT_EXIT,
EXIT_NAN,
CHILD_EXIT,
CHILD_ERROR,
_ERRNO_MSG_,
MALLOC_ERR,
WRITE_ERR,
......
......@@ -40,6 +40,8 @@
# define EXEC_PIPE 0x01
# define CMD_PIPE 0x02
# define CHILD_EXIT 0x04
# define CHILD_ERROR 0x08
/*
** **********************************
......@@ -75,7 +77,7 @@ typedef struct s_xe
char *line;
char **env;
char **exported;
char pipe;
char flags;
uint8_t pad[7];
} t_xe;
......
......@@ -28,12 +28,13 @@ int handle_pipe(t_command *cur_command, t_xe *xe, int fd_in, int proc)
child_setup(fd, fd_in);
ret = execute_cmd(cur_command->args, cur_command->redir_paths, cur_command->redir_types, xe);// error
free_command(cur_command);
return (CHILD_EXIT);
xe->flags += CHILD_EXIT;
return (ret);
}
else
{
if (!(xe->pipe & CMD_PIPE))
xe->pipe += CMD_PIPE;
if (!(xe->flags & CMD_PIPE))
xe->flags += CMD_PIPE;
close(fd[1]);// error
close(fd_in);
free_command(cur_command);
......@@ -85,7 +86,7 @@ int parent_pipe_end(t_command *cur_command, t_xe *xe, int fd_in, int proc)
return (ret);
else
{
xe->pipe = 0;
xe->flags = 0;
xe->stat_loc = tmp;
ft_error(ret, xe);
return (handle_execution(xe, STDIN_FILENO, 0));
......@@ -103,7 +104,7 @@ int handle_command(t_command *cur_command, t_xe *xe, int fd_in, int proc)
free_command(cur_command);
dup2(xe->backup_stdout, STDOUT_FILENO);
dup2(xe->backup_stdin, STDIN_FILENO);
xe->pipe = 0;
xe->flags = 0;
return (handle_execution(xe, STDIN_FILENO, 0));
}
else if (cur_command->pipe_flag == TRUE)
......@@ -133,7 +134,7 @@ int handle_execution(t_xe *xe, int fd_in, int proc)
return (FAILURE);
dup2(xe->backup_stdout, STDOUT_FILENO);
dup2(xe->backup_stdin, STDIN_FILENO);
xe->pipe = 0;
xe->flags = 0;
return (handle_execution(xe, fd_in, proc));
}
if (cur_command->args == NULL)
......@@ -144,7 +145,7 @@ int handle_execution(t_xe *xe, int fd_in, int proc)
return (FD_ERROR);
}
free_command(cur_command);
xe->pipe = 0;
xe->flags = 0;
return (handle_execution(xe, fd_in, proc));
}
}
......
......@@ -46,11 +46,17 @@ static int exec_cmd(char *cmd, char **args, t_xe *xe)
pid = fork();
if (pid == 0)
{
xe->pipe += EXEC_PIPE;
xe->flags += EXEC_PIPE;
if (execve(cmd, args, xe->env) == ERROR)
return (CHILD_ERROR);
{
xe->flags += CHILD_ERROR;
return (CLEAN_EXIT);
}
else
return (CHILD_EXIT);
{
xe->flags += CHILD_EXIT;
return (CLEAN_EXIT);
}
}
else
{
......@@ -242,7 +248,7 @@ int execute_cmd(char **args, char **redir_paths, enum e_redir_op *redir_types,
return (ret);
}
cmd_code = get_cmd_code(args[0]);
if (cmd_code == EXIT && xe->pipe & CMD_PIPE)// move dans ft_exit?
if (cmd_code == EXIT && xe->flags & CMD_PIPE)// move dans ft_exit?
return (PIPE_EXIT);
ret = command[cmd_code](args, xe);
if (cmd_code < 4)
......
......@@ -36,7 +36,7 @@ int exec_error(int err_code, t_xe *xe)
// have to set stat_loc as well !!!
if (err_code == NO_SUCH_FILE)
xe->stat_loc = 127;
if (xe->pipe & EXEC_PIPE)
if (xe->flags & EXEC_PIPE)
return (CHILD_EXIT);
else
return (SUCCESS);
......@@ -71,7 +71,7 @@ int clean_and_exit(int err_code, t_xe *xe)
ret = EXIT_SUCCESS;
else
{
if (err_code == CHILD_ERROR)
if (xe->flags & CHILD_ERROR)
{
ft_putstr_fd("External function error: ", STDERR_FILENO); // -> strerror
ft_putendl_fd(strerror(errno), STDERR_FILENO); // strerror error?
......@@ -98,10 +98,10 @@ int ft_error(int ret, t_xe *xe)
{
if (ret == PIPE_EXIT)
{
xe->pipe = 0;
xe->flags = 0;
return (SUCCESS);
}
if (ret > CHILD_EXIT)
if (ret > _ERRNO_MSG_ || xe->flags & CHILD_ERROR)
ft_putstr_fd("minishell: ", STDERR_FILENO);
if (ret > _PARSING_ERROR_)// temp
return (parsing_error(ret, xe));
......
......@@ -85,7 +85,7 @@ int main(int argc, char **argv, char **env_source)
ret = exec_env_init(xe, env_source);
if (ret != SUCCESS) // needed?
return (error_and_exit(ret, xe)); // needed?
while (ret != CLEAN_EXIT && ret != FAILURE && ret != CHILD_EXIT && ret != FT_EXIT) // why not while (ret == SUCCESS) ?
while (ret != CLEAN_EXIT && ret != FAILURE && ret != FT_EXIT && !(xe->flags & CHILD_EXIT || xe->flags & CHILD_ERROR)) // why not while (ret == SUCCESS) ?
ret = main_loop(xe);
}
return (error_and_exit(ret, xe));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment