Commit db0c076e authored by abenoit's avatar abenoit
Browse files

ft_error and exit fix with few norme fixes

parent cfc60114
......@@ -88,7 +88,6 @@ int parent_pipe_end(t_command *cur_command, t_xe *xe, int fd_in, int proc)
return (ret);
else
{
xe->flags = RUN;
xe->stat_loc = tmp;
ft_error(ret, xe);
if (ret == EXIT_ARG_ERR)
......@@ -108,7 +107,6 @@ 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->flags = RUN;
return (handle_execution(xe, STDIN_FILENO, 0));
}
else if (cur_command->pipe_flag == TRUE)
......@@ -139,7 +137,6 @@ int handle_execution(t_xe *xe, int fd_in, int proc)
dup2(xe->backup_stdout, STDOUT_FILENO);
dup2(xe->backup_stdin, STDIN_FILENO);
xe->stat_loc = 1;
xe->flags = RUN;
return (handle_execution(xe, fd_in, proc));
}
if (cur_command->args == NULL)
......@@ -150,7 +147,6 @@ int handle_execution(t_xe *xe, int fd_in, int proc)
return (FD_ERROR);
}
free_command(cur_command);
xe->flags = RUN;
return (handle_execution(xe, fd_in, proc));
}
}
......
......@@ -68,6 +68,22 @@ static int err_output(int err_code, t_xe *xe)
return (SUCCESS);
}
int exit_trigger(int ret, t_xe *xe)
{
if (!(xe->flags & EXIT_ABORT))
{
free_str_array(xe->exported); // besoin de free?
free_str_array(xe->env); // besoin de free?
free(xe);
exit(ret);
}
else
{
xe->flags = RUN;
return (SUCCESS);
}
}
int clean_and_exit(int err_code, t_xe *xe)
{
int ret;
......@@ -83,6 +99,7 @@ int clean_and_exit(int err_code, t_xe *xe)
{
if (err_code == EXT_CMD_ERROR)
{
xe->flags += EXIT_ABORT;
ft_putstr_fd("External function error: ", STDERR_FILENO);
ft_putendl_fd(strerror(errno), STDERR_FILENO);
}
......@@ -90,18 +107,7 @@ int clean_and_exit(int err_code, t_xe *xe)
ft_putstr_fd("exit: bad argument\n", STDERR_FILENO);
ret = (xe->stat_loc);
}
if (!(xe->flags & EXIT_ABORT))
{
free_str_array(xe->exported); // besoin de free?
free_str_array(xe->env); // besoin de free?
free(xe);
exit(ret);
}
else
{
xe->flags = RUN;
return (SUCCESS);
}
return (exit_trigger(ret, xe));
}
int ft_error(int ret, t_xe *xe)
......@@ -115,9 +121,9 @@ int ft_error(int ret, t_xe *xe)
else if (ret > _EXEC_ERROR_)
return (exec_error(ret, xe));
else if (ret > _ERRNO_MSG_)
err_output(ret, xe);
else if (ret > _EXIT_CODE_ || !(xe->flags & RUN) || (xe->flags & EXIT_FLAG))
return (clean_and_exit(ret, xe));
return (err_output(ret, xe));
else if (ret > _EXIT_CODE_ || !(xe->flags & RUN))
ret = clean_and_exit(ret, xe);
else if (ret != SUCCESS)
ft_putstr_fd("Fatal error", STDERR_FILENO);
return (SUCCESS);
......@@ -125,6 +131,6 @@ int ft_error(int ret, t_xe *xe)
int error_and_exit(enum e_retcode ret, t_xe *xe)
{
ft_error(ret, xe);
return (clean_and_exit(ret, xe));
ret = ft_error(ret, xe);
return (exit_trigger(ret, xe));
}
......@@ -37,8 +37,8 @@ static int main_loop(t_xe *xe)
if (ret == SUCCESS)
ret = handle_execution(xe, STDIN_FILENO, 0);
free(line);
ret = ft_error(ret, xe);
}
ret = ft_error(ret, xe);
return (ret);
}
......@@ -87,7 +87,10 @@ int main(int argc, char **argv, char **env_source)
if (ret != SUCCESS) // needed?
return (error_and_exit(ret, xe)); // needed?
while (xe->flags & RUN) // why not while (ret == SUCCESS) ?
{
xe->flags = RUN;
ret = main_loop(xe);
}
}
return (clean_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