Commit 8fce9081 authored by abenoit's avatar abenoit
Browse files

exit flags added

parent 1096ff8b
......@@ -38,10 +38,13 @@
# define TO_SPLIT 1
# define NOT_TO_SPLIT 0
# define EXEC_PIPE 0x01
# define CMD_PIPE 0x02
# define CHILD_EXIT 0x04
# define CHILD_ERROR 0x08
# 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
/*
** **********************************
......
......@@ -2,6 +2,10 @@
int ft_exit(char **args, t_xe *xe)
{
if (xe->flags & CMD_PIPE)// move dans ft_exit?
xe->flags += EXIT_ABORT;
else
xe->flags += EXIT_FLAG;
if (args != NULL) // necessaire?
{
if (args[1] != NULL)
......@@ -11,16 +15,12 @@ int ft_exit(char **args, t_xe *xe)
if (args[2] != NULL)
{
xe->stat_loc = 1;
if (ft_putstr_fd("exit\n", STDOUT_FILENO) != WRITE_SUCCESS)
return (WRITE_ERR);
return (EXIT_ARG_ERR);
}
xe->stat_loc = (unsigned int)ft_atoi(args[1]) % 256;
}
else
{
if (ft_putstr_fd("exit\n", STDOUT_FILENO) != WRITE_SUCCESS)
return (WRITE_ERR);
xe->stat_loc = 2;
return (EXIT_NAN);
}
......
......@@ -28,6 +28,7 @@ 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);
xe->flags -= RUN;
xe->flags += CHILD_EXIT;
return (ret);
}
......@@ -86,7 +87,7 @@ int parent_pipe_end(t_command *cur_command, t_xe *xe, int fd_in, int proc)
return (ret);
else
{
xe->flags = 0;
xe->flags = RUN;
xe->stat_loc = tmp;
ft_error(ret, xe);
return (handle_execution(xe, STDIN_FILENO, 0));
......@@ -104,7 +105,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->flags = 0;
xe->flags = RUN;
return (handle_execution(xe, STDIN_FILENO, 0));
}
else if (cur_command->pipe_flag == TRUE)
......@@ -134,7 +135,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->flags = 0;
xe->flags = RUN;
return (handle_execution(xe, fd_in, proc));
}
if (cur_command->args == NULL)
......@@ -145,7 +146,7 @@ int handle_execution(t_xe *xe, int fd_in, int proc)
return (FD_ERROR);
}
free_command(cur_command);
xe->flags = 0;
xe->flags = RUN;
return (handle_execution(xe, fd_in, proc));
}
}
......
......@@ -50,11 +50,13 @@ static int exec_cmd(char *cmd, char **args, t_xe *xe)
if (execve(cmd, args, xe->env) == ERROR)
{
xe->flags += CHILD_ERROR;
xe->flags -= RUN;
return (CLEAN_EXIT);
}
else
{
xe->flags += CHILD_EXIT;
xe->flags -= RUN;
return (CLEAN_EXIT);
}
}
......@@ -248,8 +250,6 @@ 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->flags & CMD_PIPE)// move dans ft_exit?
return (PIPE_EXIT);
ret = command[cmd_code](args, xe);
if (cmd_code < 4)
{
......
......@@ -66,11 +66,22 @@ int clean_and_exit(int err_code, t_xe *xe)
int ret;
if (err_code == ARG_ERR)
{
ret = 1;
ft_putstr_fd("minishell takes no argument\n", STDERR_FILENO);
if (err_code < FT_EXIT)
}
else if (err_code == CLEAN_EXIT)
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 (ft_putstr_fd("exit\n", STDOUT_FILENO) != WRITE_SUCCESS)
return (WRITE_ERR); // possible?
}
}
if (xe->flags & CHILD_ERROR)
{
ft_putstr_fd("External function error: ", STDERR_FILENO); // -> strerror
......@@ -80,25 +91,22 @@ int clean_and_exit(int err_code, t_xe *xe)
ft_putstr_fd("exit: bad argument\n", STDERR_FILENO); // -> strerror
ret = (xe->stat_loc);
}
free_str_array(xe->exported); // besoin de free?
free_str_array(xe->env); // besoin de free?
free(xe);
if (isatty(STDIN_FILENO)) // temp pour le testeur
if (!(xe->flags & EXIT_ABORT))
{
if (err_code == CLEAN_EXIT || err_code == FT_EXIT)
{
if (ft_putstr_fd("exit\n", STDOUT_FILENO) != WRITE_SUCCESS)
return (WRITE_ERR); // possible?
}
free_str_array(xe->exported); // besoin de free?
free_str_array(xe->env); // besoin de free?
free(xe);
exit(ret);
}
exit(ret);
else
return (SUCCESS);
}
int ft_error(int ret, t_xe *xe)
{
if (ret == PIPE_EXIT)
{
xe->flags = 0;
xe->flags = RUN;
return (SUCCESS);
}
if (ret > _ERRNO_MSG_ || xe->flags & CHILD_ERROR)
......
......@@ -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);
}
......@@ -48,6 +48,7 @@ int exec_env_init(t_xe *xe, char **env_source)
int tmp;
xe->env = dup_str_array(env_source);
xe->flags = RUN;
if (xe->env == NULL)
return (MALLOC_ERR);
shlvl = get_var_value(xe->env, SHLVL_STR, ft_strlen(SHLVL_STR));
......@@ -85,8 +86,9 @@ 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 != FT_EXIT && !(xe->flags & CHILD_EXIT || xe->flags & CHILD_ERROR)) // why not while (ret == SUCCESS) ?
while (xe->flags & RUN) // why not while (ret == SUCCESS) ?
ret = main_loop(xe);
}
return (error_and_exit(ret, 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