Commit a6fe249a authored by komask's avatar komask
Browse files

error handling

parent 7632a9f2
......@@ -20,9 +20,10 @@ enum e_retcode
INVALID_PATH_DIR,
FD_ERROR,
EXT_CMD_ERROR,
FORK_ERROR,
_EXEC_ERROR_,
AMBIG_REDIR,
NO_SUCH_FILE,
CMD_NOT_FOUND,
INVALID_EXP_ID,
INVALID_UNS_ID,
HOME_NOT_SET,
......
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_set_index.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mvidal-a <mvidal-a@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/10 16:41:57 by mvidal-a #+# #+# */
/* Updated: 2021/02/10 16:41:58 by mvidal-a ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_set_index(char c, char *set)
......
arthur:
comportement simplifié ^D
main (redundant check):
ret = exec_env_init(xe, env_source);
if (ret != SUCCESS)
return (error_and_exit(ret, xe));
wait and waitpid errors
masaki:
a la fin avec rgrep:
erreurs de write et de ft_put* a terminer
a la fin avec rgrep/vimgrep:
erreurs de ft_put* a terminer
gestion d'erreurs des fonctions autorisees une a une
static functions
......
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_exit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mvidal-a <mvidal-a@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/10 16:41:31 by mvidal-a #+# #+# */
/* Updated: 2021/02/10 16:41:31 by mvidal-a ### ########.fr */
/* */
/* ************************************************************************** */
#include "builtins.h"
int ft_exit(char **args, t_xe *xe)
int ft_exit(char **args, t_xe *xe)
{
if (xe->flags & CMD_PIPE || xe->flags & CHILD)
{
xe->flags += EXIT_ABORT;
}
else
{
xe->flags -= RUN;
xe->flags += EXIT_FLAG;
}
if (args != NULL) // necessaire?
if (args[1] != NULL)
{
if (args[1] != NULL)
if (ft_isnumber(args[1]) == TRUE)
{
if (ft_isnumber(args[1]) == TRUE)
if (args[2] != NULL)
{
if (args[2] != NULL)
{
xe->stat_loc = 1;
return (EXIT_ARG_ERR);
}
xe->stat_loc = (unsigned int)ft_atoi(args[1]) % 256;
}
else
{
xe->stat_loc = 2;
return (EXIT_NAN);
xe->stat_loc = 1;
return (EXIT_ARG_ERR);
}
xe->stat_loc = (unsigned int)ft_atoi(args[1]) % 256;
}
else
{
xe->stat_loc = 2;
return (EXIT_NAN);
}
}
return (SUCCESS);
......
#include "parsing.h"
#include "execution.h"
void child_setup(const int *fd, int fd_in, t_xe *xe)
static int 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
signal(SIGINT, SIG_DFL);
signal(SIGQUIT, SIG_DFL);
if (close(fd[0]) == ERROR)
return (FAILURE);
if (fd_in != STDIN_FILENO)
{
if (dup2(fd_in, STDIN_FILENO) != -1)
close(fd_in);
if (dup2(fd_in, STDIN_FILENO) == ERROR || close(fd_in) == ERROR)
return (FAILURE);
}
if (dup2(fd[1], STDOUT_FILENO) != -1)// error
close(fd[1]);
if (dup2(fd[1], STDOUT_FILENO) == ERROR || close(fd[1]) == ERROR)
return (FAILURE);
return (SUCCESS);
}
int handle_pipe(t_command *cur_command, t_xe *xe, int fd_in, int proc)
......@@ -22,12 +24,16 @@ int handle_pipe(t_command *cur_command, t_xe *xe, int fd_in, int proc)
int ret;
ret = SUCCESS;
pipe(fd);// error?
xe->gpid = fork();// error?
if (xe->gpid == 0)
if (pipe(fd) == ERROR)
return (FD_ERROR);
xe->gpid = fork();
if (xe->gpid == ERROR)
return (FORK_ERROR);
else if (xe->gpid == 0)
{
child_setup(fd, fd_in, xe);
ret = execute_cmd(cur_command->args, cur_command->redir_paths, cur_command->redir_types, xe);// error
if (child_setup(fd, fd_in, xe) != SUCCESS)
return (FD_ERROR);
ret = execute_cmd(cur_command->args, cur_command->redir_paths, cur_command->redir_types, xe);
free_command(cur_command);
xe->flags -= RUN;
return (ret);
......@@ -36,8 +42,8 @@ int handle_pipe(t_command *cur_command, t_xe *xe, int fd_in, int proc)
{
if (!(xe->flags & CMD_PIPE))
xe->flags += CMD_PIPE;
close(fd[1]);// error
close(fd_in);
if (close(fd[1]) == ERROR || close(fd_in) == ERROR)
return (FD_ERROR);
free_command(cur_command);
return (handle_execution(xe, fd[0], proc + 1));
}
......@@ -52,15 +58,18 @@ int parent_pipe_end(t_command *cur_command, t_xe *xe, int fd_in, int proc)
ret = SUCCESS;
if (fd_in != STDIN_FILENO)
{
if (dup2(fd_in, STDIN_FILENO) != -1)
close(fd_in);
if (dup2(fd_in, STDIN_FILENO) == ERROR || close(fd_in) == ERROR)
return (FD_ERROR);
}
dup2(xe->backup_stdout, STDOUT_FILENO);
ret = execute_cmd(cur_command->args, cur_command->redir_paths, cur_command->redir_types, xe);// error
if (dup2(xe->backup_stdout, STDOUT_FILENO) == ERROR)
return (FD_ERROR);
ret = execute_cmd(cur_command->args, cur_command->redir_paths, cur_command->redir_types, xe);
tmp = xe->stat_loc;
free_command(cur_command);
dup2(xe->backup_stdout, STDOUT_FILENO);
dup2(xe->backup_stdin, STDIN_FILENO);
if (dup2(xe->backup_stdout, STDOUT_FILENO) == ERROR)
return (FD_ERROR);
if (dup2(xe->backup_stdin, STDIN_FILENO) == ERROR)
return (FD_ERROR);
i = 0;
while (i < proc)
{
......@@ -101,9 +110,8 @@ int handle_command(t_command *cur_command, t_xe *xe, int fd_in, int proc)
int ret;
ret = SUCCESS;
if ((cur_command->args) == NULL)
if (cur_command->args == NULL)
{
//ne peut être que le dernier argument car le parsing aurait renvoyé une erreur dans les autres cas
free_command(cur_command);
dup2(xe->backup_stdout, STDOUT_FILENO);
dup2(xe->backup_stdin, STDIN_FILENO);
......
......@@ -44,7 +44,9 @@ static int exec_cmd(char *cmd, char **args, t_xe *xe)
pid_t pid;
pid = fork();
if (pid == 0)
if (pid == ERROR)
return (FORK_ERROR);
else if (pid == 0)
{
xe->flags += EXEC_PIPE;
if (execve(cmd, args, xe->env) == ERROR)
......@@ -60,9 +62,9 @@ static int exec_cmd(char *cmd, char **args, t_xe *xe)
}
else
{
signal(SIGINT, SIG_IGN); // error if SIG_ERR? (check errno)
signal(SIGQUIT, SIG_IGN); // error if SIG_ERR? (check errno)
waitpid(pid, &xe->stat_loc, 0); // return value? error?
signal(SIGINT, SIG_IGN);
signal(SIGQUIT, SIG_IGN);
waitpid(pid, &xe->stat_loc, 0);
if (WIFSIGNALED(xe->stat_loc))
{
xe->stat_loc = WTERMSIG(xe->stat_loc);
......@@ -80,7 +82,7 @@ static int exec_cmd(char *cmd, char **args, t_xe *xe)
}
else if(WIFEXITED(xe->stat_loc))
xe->stat_loc = WEXITSTATUS(xe->stat_loc);
signal_handler(); // err
signal_handler();
return (SUCCESS);
}
}
......@@ -93,7 +95,7 @@ static int create_cmd(char **tmp, char **path, char **args)
dir_index = search_exec(path, args[0]);
if (dir_index == NOT_FOUND)
return (NO_SUCH_FILE); // other error code
return (CMD_NOT_FOUND);
dir = ft_strjoin(path[dir_index], "/");
cmd = ft_strjoin(dir, args[0]);
if (cmd == NULL)
......@@ -103,18 +105,17 @@ static int create_cmd(char **tmp, char **path, char **args)
return (SUCCESS);
}
static int add_path_to_localdir(char ***path)
static char *add_path_to_localdir(void)
{
char *tmp;
tmp = malloc(sizeof(char) + 2);
if (tmp == NULL)
return (MALLOC_ERR);
return (NULL);
tmp[0] = '.';
tmp[1] = '/';
tmp[2] = '\0';
*path = push_str_to_array(*path, tmp);
return (SUCCESS);
return (tmp);
}
static int launch_ext(char **args, t_xe *xe)
......@@ -133,15 +134,18 @@ static int launch_ext(char **args, t_xe *xe)
}
else
{
ret = get_var_pos(xe->env, "PATH", 4); // and with PATH unset?
if (ret == -1)
ret = get_var_pos(xe->env, "PATH", 4);
if (ret == NOT_FOUND)
{
path = malloc(sizeof(char*));
*path = NULL;
path = malloc((sizeof(char*) * 2));
path[0] = add_path_to_localdir();
if (path[0] == NULL)
return (MALLOC_ERR);
path[1] = NULL;
}
else
{
tmp = get_var_value(xe->env, "PATH", 4); // and with PATH unset?
tmp = get_var_value(xe->env, "PATH", 4);
if (tmp == NULL)
return (MALLOC_ERR);
path = ft_split(tmp, ':');
......@@ -149,7 +153,6 @@ static int launch_ext(char **args, t_xe *xe)
if (path == NULL)
return (MALLOC_ERR);
}
add_path_to_localdir(&path);
ret = create_cmd(&cmd, path, args);
free_str_array(path);
if (ret != SUCCESS)
......@@ -204,9 +207,10 @@ int apply_redir(char *cur_arg, enum e_redir_op redir, t_xe *xe)
flags |= O_APPEND;
}
redir_fd = open(cur_arg, flags, mode);
if (redir_fd >= 0) // else error
if (redir_fd >= 0)
{
dup2(redir_fd, src_fd); // error
if (dup2(redir_fd, src_fd) == ERROR)
return (FD_ERROR);
}
else
{
......
......@@ -21,7 +21,7 @@ int exec_error(int err_code, t_xe *xe)
{
const char *err_msg[] = {
"Ambiguous redirection",
"No such file or directory", // more specific msg
"Commmand not found",
"export: Variable identifier (name) invalid",
"unset: Variable identifier (name) invalid",
"HOME not set",
......@@ -30,7 +30,7 @@ int exec_error(int err_code, t_xe *xe)
ft_putstr_fd("command error: ", STDERR_FILENO);
ft_putendl_fd(err_msg[err_code - _EXEC_ERROR_ - 1], STDERR_FILENO);
if (err_code == NO_SUCH_FILE)
if (err_code == CMD_NOT_FOUND)
xe->stat_loc = 127;
if (xe->flags & EXEC_PIPE || xe->flags & CHILD)
xe->flags = 0;
......@@ -49,14 +49,15 @@ static int err_output(int err_code, t_xe *xe)
"cd: cannot set variable PWD",
"pwd: cannot get current directory path",
"Error reading a directory in PATH",
"fd",
"external command error"}; // fd?
"fd issue",
"external command error",
"fork failure"};
(void)xe;
ft_putstr_fd("error: ", STDERR_FILENO);
ft_putstr_fd(err_msg[err_code - _ERRNO_MSG_ - 1], STDERR_FILENO);
ft_putstr_fd(": ", STDERR_FILENO); // besoin de faire comme perror?
ft_putendl_fd(strerror(errno), STDERR_FILENO); // strerror error?
ft_putstr_fd(": ", STDERR_FILENO);
ft_putendl_fd(strerror(errno), STDERR_FILENO);
if (err_code == EXT_CMD_ERROR)
{
if (errno == ENOENT)
......@@ -72,8 +73,8 @@ 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_str_array(xe->exported);
free_str_array(xe->env);
free(xe);
exit(ret);
}
......
......@@ -16,10 +16,10 @@ static int get_input(char **line, t_xe *xe)
ret = get_next_line(STDIN_FILENO, line);
if (ret == ERROR)
return (GNL_ERR);
else if (ret == 0) // EOF in files and heredocs (noeol files not yet supported)
else if (ret == 0)
{
free(*line);
xe->flags -= RUN;
free(*line);
xe->flags -= RUN;
}
return (SUCCESS);
}
......@@ -46,9 +46,9 @@ int exec_env_init(t_xe *xe, char **env_source)
{
char *shlvl;
int tmp;
int ret;
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));
......@@ -60,10 +60,13 @@ int exec_env_init(t_xe *xe, char **env_source)
shlvl = ft_itoa(tmp);
if (shlvl == NULL)
return (MALLOC_ERR);
env_replace_var(SHLVL_STR, shlvl, xe); // err
ret = env_replace_var(SHLVL_STR, shlvl, xe);
free(shlvl);
if (ret != SUCCESS)
return (MALLOC_ERR);
xe->backup_stdin = dup(STDIN_FILENO);
xe->backup_stdout = dup(STDOUT_FILENO);
xe->flags = RUN;
return (SUCCESS);
}
......@@ -80,13 +83,13 @@ int main(int argc, char **argv, char **env_source)
ft_bzero(xe, sizeof(t_xe));
if (argc > 1)
ret = ARG_ERR;
else // merge with else if (argc == 2) ?
else
{
signal_handler(); // err?
signal_handler();
ret = exec_env_init(xe, env_source);
if (ret != SUCCESS) // needed?
return (error_and_exit(ret, xe)); // needed?
while (xe->flags & RUN) // why not while (ret == SUCCESS) ?
if (ret != SUCCESS)
return (error_and_exit(ret, xe));
while (xe->flags & RUN)
{
xe->flags = RUN;
ret = main_loop(xe);
......
......@@ -16,6 +16,6 @@ void sig_kill(int signum)
void signal_handler(void)
{
signal(SIGINT, sig_int); // error if SIG_ERR? (check errno)
signal(SIGQUIT, sig_kill); // error if SIG_ERR? (check errno)
signal(SIGINT, sig_int);
signal(SIGQUIT, sig_kill);
}
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