Commit ae0ee94f authored by abenoit's avatar abenoit
Browse files

removed few redundant err-codes

parent ba81bfe6
......@@ -8,9 +8,7 @@
enum e_retcode
{
_EXIT_CODE_ = 2,
CLEAN_EXIT,
ARG_ERR,
FT_EXIT,
EXIT_NAN,
_ERRNO_MSG_,
MALLOC_ERR,
......
......@@ -41,10 +41,9 @@
# define RUN 0x01
# define EXEC_PIPE 0x02
# define CMD_PIPE 0x04
# define CHILD_EXIT 0x08
# define CHILD_ERROR 0x10
# define EXIT_ABORT 0x20
# define EXIT_FLAG 0x40
# define CHILD 0x08
# define EXIT_ABORT 0x10
# define EXIT_FLAG 0x20
/*
** **********************************
......
......@@ -2,10 +2,15 @@
int ft_exit(char **args, t_xe *xe)
{
if (xe->flags & CMD_PIPE)// move dans ft_exit?
if (xe->flags & CMD_PIPE || xe->flags & CHILD)
{
xe->flags += EXIT_ABORT;
}
else
xe->flags += EXIT_FLAG;
{
xe->flags -= RUN;
xe->flags = EXIT_FLAG;
}
if (args != NULL) // necessaire?
{
if (args[1] != NULL)
......@@ -26,5 +31,5 @@ int ft_exit(char **args, t_xe *xe)
}
}
}
return (FT_EXIT);
return (SUCCESS);
}
#include "parsing.h"
#include "execution.h"
void child_setup(const int *fd, int fd_in)
void child_setup(const int *fd, int fd_in, t_xe *xe)
{
xe->flags += CHILD;
signal(SIGINT, SIG_DFL); // error if SIG_ERR? (check errno)
signal(SIGQUIT, SIG_DFL); // error if SIG_ERR? (check errno)
close(fd[0]);// error
......@@ -25,11 +26,10 @@ int handle_pipe(t_command *cur_command, t_xe *xe, int fd_in, int proc)
xe->gpid = fork();// error?
if (xe->gpid == 0)
{
child_setup(fd, fd_in);
child_setup(fd, fd_in, xe);
ret = execute_cmd(cur_command->args, cur_command->redir_paths, cur_command->redir_types, xe);// error
free_command(cur_command);
xe->flags -= RUN;
xe->flags += CHILD_EXIT;
return (ret);
}
else
......@@ -59,6 +59,7 @@ int parent_pipe_end(t_command *cur_command, t_xe *xe, int fd_in, int proc)
ret = execute_cmd(cur_command->args, cur_command->redir_paths, cur_command->redir_types, xe);// error
tmp = xe->stat_loc;
free_command(cur_command);
dup2(xe->backup_stdout, STDOUT_FILENO);
dup2(xe->backup_stdin, STDIN_FILENO);
i = 0;
while (i < proc)
......@@ -83,7 +84,7 @@ int parent_pipe_end(t_command *cur_command, t_xe *xe, int fd_in, int proc)
xe->stat_loc = WEXITSTATUS(xe->stat_loc);
i++;
}
if (ret != SUCCESS && ret < _ERRNO_MSG_)
if (xe->flags & EXIT_FLAG || (ret != SUCCESS && ret < _ERRNO_MSG_))
return (ret);
else
{
......
......@@ -49,15 +49,13 @@ static int exec_cmd(char *cmd, char **args, t_xe *xe)
xe->flags += EXEC_PIPE;
if (execve(cmd, args, xe->env) == ERROR)
{
xe->flags += CHILD_ERROR;
xe->flags -= RUN;
return (EXT_CMD_ERROR);
}
else
{
xe->flags += CHILD_EXIT;
xe->flags -= RUN;
return (CLEAN_EXIT);
return (SUCCESS);
}
}
else
......
......@@ -37,7 +37,7 @@ int exec_error(int err_code, t_xe *xe)
if (err_code == NO_SUCH_FILE)
xe->stat_loc = 127;
if (xe->flags & EXEC_PIPE)
xe->flags += CHILD_EXIT;
xe->flags -= RUN;
return (SUCCESS);
}
......@@ -73,19 +73,19 @@ int clean_and_exit(int err_code, t_xe *xe)
ret = 1;
ft_putstr_fd("minishell takes no argument\n", STDERR_FILENO);
}
else if (err_code == CLEAN_EXIT)
else if (err_code == SUCCESS)
ret = EXIT_SUCCESS;
else
{
if (isatty(STDIN_FILENO) && (xe->flags & EXIT_FLAG)) // temp pour le testeur
{
if (xe->flags & EXIT_FLAG && !(xe->flags & CHILD_EXIT || xe->flags & CHILD_ERROR))
if (xe->flags & EXIT_FLAG && !(xe->flags & CHILD))
{
if (ft_putstr_fd("exit\n", STDOUT_FILENO) != WRITE_SUCCESS)
return (WRITE_ERR); // possible?
}
}
if (xe->flags & CHILD_ERROR)
if (err_code == EXT_CMD_ERROR)
{
ft_putstr_fd("External function error: ", STDERR_FILENO); // -> strerror
ft_putendl_fd(strerror(errno), STDERR_FILENO); // strerror error?
......@@ -102,7 +102,10 @@ int clean_and_exit(int err_code, t_xe *xe)
exit(ret);
}
else
{
xe->flags = RUN;
return (SUCCESS);
}
}
int ft_error(int ret, t_xe *xe)
......@@ -112,7 +115,7 @@ int ft_error(int ret, t_xe *xe)
xe->flags = RUN;
return (SUCCESS);
}
if (ret > _ERRNO_MSG_ || xe->flags & CHILD_ERROR)
if (ret > _ERRNO_MSG_)
ft_putstr_fd("minishell: ", STDERR_FILENO);
if (ret > _PARSING_ERROR_)// temp
return (parsing_error(ret, xe));
......@@ -120,7 +123,7 @@ int ft_error(int ret, t_xe *xe)
return (exec_error(ret, xe));
else if (ret > _ERRNO_MSG_)
err_output(ret, xe);
else if (ret > _EXIT_CODE_)
else if (ret > _EXIT_CODE_ || (xe->flags & EXIT_FLAG))
return (clean_and_exit(ret, xe));
else if (ret != SUCCESS) // temp
ft_putstr_fd("ERROR CODE ERROR (printed for debug)", STDERR_FILENO);// temp
......
......@@ -4,7 +4,7 @@
#include "execution.h"
#include "builtins.h"
static int get_input(char **line)
static int get_input(char **line, t_xe *xe)
{
int ret;
......@@ -19,7 +19,7 @@ static int get_input(char **line)
else if (ret == 0) // EOF in files and heredocs (noeol files not yet supported)
{
free(*line);
return (CLEAN_EXIT);
xe->flags -= RUN;
}
return (SUCCESS);
}
......@@ -29,8 +29,8 @@ static int main_loop(t_xe *xe)
int ret;
char *line;
ret = get_input(&line);
if (ret == SUCCESS)
ret = get_input(&line, xe);
if (ret == SUCCESS && xe->flags == RUN)
{
xe->line = line;
ret = check_syntax(line);
......@@ -89,6 +89,5 @@ int main(int argc, char **argv, char **env_source)
while (xe->flags & RUN) // why not while (ret == SUCCESS) ?
ret = main_loop(xe);
}
write(1, "OK\n", 3);
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