Commit 2a5e3a34 authored by abenoit's avatar abenoit
Browse files

piped exit behaviour added

parent 4a7b449b
......@@ -11,6 +11,7 @@ enum e_retcode
CLEAN_EXIT,
ARG_ERR,
FT_EXIT,
PIPE_EXIT,
CHILD_EXIT,
CHILD_ERROR,
_ERRNO_MSG_,
......
......@@ -40,6 +40,8 @@
# define TO_SPLIT 1
# define NOT_TO_SPLIT 0
# define EXEC_PIPE 0x01
# define CMD_PIPE 0x02
/*
** **********************************
......@@ -69,14 +71,14 @@ typedef struct s_command
typedef struct s_xe
{
pid_t gpid;
int child;
int backup_stdin;
int backup_stdout;
int stat_loc;
uint8_t pad[4];
char *line;
char **env;
char **exported;
char pipe;
uint8_t pad[7];
} t_xe;
......
......@@ -32,6 +32,8 @@ int handle_pipe(t_command *cur_command, t_xe *xe, int fd_in, int proc)
}
else
{
if (!(xe->pipe & CMD_PIPE))
xe->pipe += CMD_PIPE;
close(fd[1]);// error
close(fd_in);
free_command(cur_command);
......@@ -43,6 +45,7 @@ int parent_pipe_end(t_command *cur_command, t_xe *xe, int fd_in, int proc)
{
int i;
int ret;
int tmp;
ret = SUCCESS;
if (fd_in != STDIN_FILENO)
......@@ -51,6 +54,7 @@ int parent_pipe_end(t_command *cur_command, t_xe *xe, int fd_in, int proc)
close(fd_in);
}
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);
......@@ -60,17 +64,18 @@ int parent_pipe_end(t_command *cur_command, t_xe *xe, int fd_in, int proc)
wait(&xe->stat_loc);
if (WIFSIGNALED(xe->stat_loc))
{
if (WTERMSIG(xe->stat_loc) == SIGQUIT)
xe->stat_loc = WTERMSIG(xe->stat_loc);
if (xe->stat_loc == SIGQUIT)
{
if (ft_putstr_fd("\b\b^\\Quit (core dumped)\n", STDERR_FILENO) != WRITE_SUCCESS)
return (WRITE_ERR);
}
else
else if (xe->stat_loc == SIGINT)
{
if (ft_putstr_fd("\n", STDERR_FILENO) != WRITE_SUCCESS)
return (WRITE_ERR);
}
xe->stat_loc = (xe->stat_loc % 256) + 128;
xe->stat_loc = (xe->stat_loc % 256) + 128;
}
else if(WIFEXITED(xe->stat_loc))
xe->stat_loc = WEXITSTATUS(xe->stat_loc);
......@@ -79,7 +84,11 @@ int parent_pipe_end(t_command *cur_command, t_xe *xe, int fd_in, int proc)
if (ret != SUCCESS)
return (ret);
else
{
xe->pipe = 0;
xe->stat_loc = tmp;
return (handle_execution(xe, STDIN_FILENO, 0));
}
}
int handle_command(t_command *cur_command, t_xe *xe, int fd_in, int proc)
......@@ -93,6 +102,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;
return (handle_execution(xe, STDIN_FILENO, 0));
}
else if (cur_command->pipe_flag == TRUE)
......@@ -122,6 +132,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;
return (handle_execution(xe, fd_in, proc));
}
if (cur_command->args == NULL)
......@@ -132,6 +143,7 @@ int handle_execution(t_xe *xe, int fd_in, int proc)
return (FD_ERROR);
}
free_command(cur_command);
xe->pipe = 0;
return (handle_execution(xe, fd_in, proc));
}
}
......
......@@ -46,7 +46,7 @@ static int exec_cmd(char *cmd, char **args, t_xe *xe)
pid = fork();
if (pid == 0)
{
xe->child = 1;
xe->pipe += EXEC_PIPE;
if (execve(cmd, args, xe->env) == ERROR)
return (CHILD_ERROR);
else
......@@ -65,7 +65,7 @@ static int exec_cmd(char *cmd, char **args, t_xe *xe)
if (ft_putstr_fd("\b\b^\\Quit (core dumped)\n", STDERR_FILENO) != WRITE_SUCCESS)
return (WRITE_ERR);
}
else
else if (xe->stat_loc == SIGINT)
{
if (ft_putstr_fd("\n", STDERR_FILENO) != WRITE_SUCCESS)
return (WRITE_ERR);
......@@ -246,6 +246,8 @@ int execute_cmd(char **args, char **redir_paths, enum e_redir_op *redir_types,
cmd_code = get_cmd_code(args[0]);
if (cmd_code == M_ERROR)
return (MALLOC_ERR);
else if (cmd_code == EXIT && xe->pipe & CMD_PIPE)
return (PIPE_EXIT);
ret = command[cmd_code](args, xe);
if (ret >= HOME_NOT_SET)// ?
return (ret);// ?
......
......@@ -38,7 +38,7 @@ int exec_error(int err_code, t_xe *xe)
xe->stat_loc = 1;
if (err_code == NO_SUCH_FILE)
xe->stat_loc = 127;
if (xe->child == 1)
if (xe->pipe & EXEC_PIPE)
return (CHILD_EXIT);
else
return (SUCCESS);
......@@ -99,6 +99,11 @@ int clean_and_exit(int err_code, t_xe *xe)
int ft_error(int ret, t_xe *xe)
{
if (ret == PIPE_EXIT)
{
xe->pipe = 0;
return (SUCCESS);
}
if (ret > CHILD_EXIT)
ft_putstr_fd("minishell: ", STDERR_FILENO);
if (ret > _PARSING_ERROR_)// temp
......
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