Commit 6bc99dc5 authored by abenoit's avatar abenoit
Browse files

leaks fix in launch_ext and error ret codes simplified

parent 36ce24b7
......@@ -10,12 +10,6 @@
# define ENTIRE_STR -1
# define NOT_FOUND -1
enum e_exec_errcode
{
HOME_NOT_SET = 9,
NO_SUCH_FILE
};
enum e_cmd_code
{
ECHO,
......
......@@ -48,13 +48,25 @@
enum e_retcode
{
CLEAN_EXIT = 2,
_EXIT_CODE_ = 2,
CLEAN_EXIT,
CHILD_EXIT,
ARG_ERR,
_ERRNO_MSG_,
MALLOC_ERR,
WRITE_ERR,
GNL_ERR,
AMBIG_REDIR
_EXEC_ERROR_,
AMBIG_REDIR,
HOME_NOT_SET,
NO_SUCH_FILE,
_PARSING_ERROR_,
SQUOTE_MISSING,
DQUOTE_MISSING,
ESCAPE_NL,
REDIR_PATH_INVALID,
REDIR_PATH_MISSING,
EMPTY_CMD
};
enum e_redir_op
......
......@@ -29,17 +29,6 @@ enum e_state
NB_STATES
};
enum e_parsing_error
{
SQUOTE_MISSING = 11,
DQUOTE_MISSING,
ESCAPE_NL,
REDIR_PATH_INVALID,
REDIR_PATH_MISSING,
EMPTY_CMD,
NB_PARSING_ERRORS
};
typedef struct s_state_machine
{
char buf[BUF_SIZE];
......
......@@ -88,13 +88,15 @@ static int exec_cmd(char *cmd, char **args, t_xe *xe)
static int create_cmd(char **tmp, char **path, char **args)
{
char *cmd;
char *dir;
int dir_index;
dir_index = search_exec(path, args[0]);
if (dir_index == NOT_FOUND)
return (NO_SUCH_FILE); // other error code
cmd = ft_strjoin(path[dir_index], "/");
cmd = ft_strjoin(cmd, args[0]);
dir = ft_strjoin(path[dir_index], "/");
cmd = ft_strjoin(dir, args[0]);
free(dir);
*tmp = cmd;
return (SUCCESS);
}
......@@ -140,6 +142,7 @@ static int launch_ext(char **args, t_xe *xe)
if (tmp == NULL)
return (M_ERROR);
path = ft_split(tmp, ':');
free(tmp);
if (path == NULL)
return (M_ERROR);
}
......@@ -147,7 +150,10 @@ static int launch_ext(char **args, t_xe *xe)
ret = create_cmd(&tmp, path, args);
free_str_array(path);
if (ret != SUCCESS)
{
free(tmp);
return (ret);
}
}
ret = exec_cmd(tmp, args, xe);
free(tmp);
......
......@@ -14,7 +14,7 @@ void putstr_stderr(const char *str)
int parsing_error(int err_code, t_xe *xe)
{
const char *err_msg[NB_PARSING_ERRORS] = {
const char *err_msg[] = {
"No matching single quote",
"No matching double quote",
"Multiline inputs are currently not supported",
......@@ -24,7 +24,7 @@ int parsing_error(int err_code, t_xe *xe)
(void)err_msg;
ft_putstr_fd("minishell: syntax error: ", STDERR_FILENO);
ft_putstr_fd(err_msg[err_code - 11], STDERR_FILENO);
ft_putstr_fd(err_msg[err_code - _PARSING_ERROR_ - 1], STDERR_FILENO);
ft_putchar_fd('\n', STDERR_FILENO);
xe->stat_loc = 2;
return (SUCCESS);
......@@ -34,12 +34,13 @@ int parsing_error(int err_code, t_xe *xe)
int exec_error(int err_code, t_xe *xe)
{
const char *err_msg[] = {
"Ambiguous redirection",
"HOME not set",
"No such file or directory"};
(void)err_msg;
ft_putstr_fd("minishell: command error: ", STDERR_FILENO);
ft_putstr_fd(err_msg[err_code - 9], STDERR_FILENO);
ft_putstr_fd(err_msg[err_code - _EXEC_ERROR_ - 1], STDERR_FILENO);
ft_putchar_fd('\n', STDERR_FILENO);
// have to set stat_loc as well !!!
if (err_code == HOME_NOT_SET)
......@@ -54,12 +55,10 @@ int exec_error(int err_code, t_xe *xe)
static const char *err_msg(int err_code)
{
const char *msg[5] = {
"Minishell takes no argument",
const char *msg[] = {
"Memory allocation failure",
"Cannot write on standard output",
"Cannot read standard input (GNL error)",
"Ambiguous redirection"};
"Cannot read standard input (GNL error)"};
return (msg[err_code]);
}
......@@ -67,13 +66,7 @@ static const char *err_msg(int err_code)
static int err_output(int err_code)
{
putstr_stderr("Error: ");
if (err_code == ARG_ERR || err_code == AMBIG_REDIR)
{
putstr_stderr(err_msg(err_code - 4));
putstr_stderr("\n");
}
else if (err_code < HOME_NOT_SET)
perror(err_msg(err_code - 4));
perror(err_msg(err_code - _ERRNO_MSG_));
return (SUCCESS);
}
......@@ -82,6 +75,8 @@ int clean_and_exit(int ret, t_xe *xe)
free_str_array(xe->exported); // besoin de free?
free_str_array(xe->env); // besoin de free?
free(xe);
if (ret == ARG_ERR)
putstr_stderr("Minishell takes no argument");
if (ret != CHILD_EXIT)
if (isatty(STDIN_FILENO)) // temp pour le testeur
write(1, "exit\n", 5);
......@@ -90,14 +85,14 @@ int clean_and_exit(int ret, t_xe *xe)
int ft_error(int ret, t_xe *xe)
{
if (ret == CLEAN_EXIT || ret == CHILD_EXIT)
return (clean_and_exit(ret, xe));
else if (ret >= SQUOTE_MISSING)// temp
if (ret > _PARSING_ERROR_)// temp
return (parsing_error(ret, xe));
else if (ret >= HOME_NOT_SET)
else if (ret > _EXEC_ERROR_)
return (exec_error(ret, xe));
else if (ret > CHILD_EXIT)
else if (ret > _ERRNO_MSG_)
err_output(ret);
else if (ret > _EXIT_CODE_)
return (clean_and_exit(ret, xe));
else if (ret != SUCCESS) // temp
putstr_stderr("ERROR CODE ERROR (printed for debug)");// temp
return (SUCCESS);
......
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