Commit 79ab7950 authored by Abenoit's avatar Abenoit
Browse files

avoid parsing and exec error code superposition

parent 994e2aba
......@@ -10,6 +10,7 @@ SRC += parse_input.c
SRC += check_syntax.c
SRC += parse_utils.c
SRC += exec_handler.c
SRC += exec_error.c
#SRC += prepare_args.c
#SRC += test.c #alternative to prepare_args.c
......
......@@ -10,6 +10,12 @@
# define ENTIRE_STR -1
# define NOT_FOUND -1
enum e_exec_errcode
{
HOME_NOT_SET = 7,
FILE_NOT_FOUND
};
enum e_cmd_code
{
ECHO,
......@@ -46,6 +52,12 @@ int ft_env(char **args, t_xe *xe);
** utils
*/
int exec_error(int err_code);
/*
** utils
*/
t_byte shift_options(char ***args, char *op_chars);
int env_replace_var(char *var_name, char *value, t_xe *xe);
int check_var_name(char *var, ssize_t name_len);
......
......@@ -49,7 +49,6 @@
enum e_retcode
{
CLEAN_EXIT = 2,
HOME_NOT_SET,
ARG_ERR,
MALLOC_ERR,
WRITE_ERR,
......
#include "execution.h"
int exec_error(int err_code)
{
const char *err_msg[] = {
"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], STDERR_FILENO);
ft_putchar_fd('\n', STDERR_FILENO);
// have to set stat_loc as well !!!
return (SUCCESS);
}
......@@ -16,7 +16,7 @@ int ft_cd(char **args, t_xe *xe)
else
path = ft_strdup(args[1]);
if (path == NULL)
; // err
return (SUCCESS); // err
errno = 0;
oldpwd = getcwd(NULL, 0);
if (chdir(path) == ERROR)
......
......@@ -56,7 +56,6 @@ int search_exec(char **path, char *name)
static int child_task(char **path, char **args, t_xe *xe)
{
char *tmp;
char *cmd;
int dir_index;
......@@ -67,12 +66,10 @@ static int child_task(char **path, char **args, t_xe *xe)
if (dir_index == NOT_FOUND)
{
free_str_array(path);
return (FAILURE); // other error code
return (FILE_NOT_FOUND); // other error code
}
cmd = ft_strjoin(path[dir_index], "/");
tmp = cmd;
cmd = ft_strjoin(cmd, args[0]);
free(tmp);
if (execve(cmd, args, xe->env) == ERROR)
perror("External function error:");
free(cmd); // does not free if execve succeeds
......@@ -81,8 +78,8 @@ static int child_task(char **path, char **args, t_xe *xe)
static int launch_ext(char **args, t_xe *xe)
{
pid_t pid;
int ret;
pid_t pid;
char *tmp;
char **path;
......@@ -94,9 +91,12 @@ static int launch_ext(char **args, t_xe *xe)
free(tmp);
if (path == NULL)
return (M_ERROR);
// free_str_array(path);
pid = fork();
if (pid == 0)
{
ret = child_task(path, args, xe);
}
else
{
signal(SIGINT, SIG_IGN);
......@@ -104,7 +104,6 @@ static int launch_ext(char **args, t_xe *xe)
waitpid(pid, &xe->stat_loc, 0); // return value? error?
signal_handler();
}
// free_str_array(path);
return (ret);
}
......@@ -187,8 +186,8 @@ int execute_cmd(char **args, char **redir_paths, enum e_redir_op *redir_types,
if (cmd_code == M_ERROR)
return (MALLOC_ERR);
ret = command[cmd_code](args, xe);
if (ret == FAILURE)
ft_putstr_fd("COMMAND ERROR\n", 1);
if (ret > GNL_ERR)
return (exec_error(ret - 7));
free_str_array(args);
return (ret);
}
......@@ -13,7 +13,6 @@ void putstr_stderr(const char *str)
static const char *err_msg(int err_code)
{
const char *msg[] = {
"HOME not set",
"Minishell takes no argument",
"Memory allocation failure",
"Cannot write on standard output",
......
Supports Markdown
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