aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/error.h19
-rw-r--r--src/builtin/cd.c17
-rw-r--r--src/builtin/exit.c12
-rw-r--r--src/error.c40
-rw-r--r--src/eval/cmd.c43
-rw-r--r--src/eval/op.c14
-rw-r--r--src/eval/redir.c20
-rw-r--r--src/main.c13
-rw-r--r--src/parser/parsed.c6
9 files changed, 71 insertions, 113 deletions
diff --git a/include/error.h b/include/error.h
index 2326d1a..3933fe9 100644
--- a/include/error.h
+++ b/include/error.h
@@ -6,7 +6,7 @@
/* By: charles <me@cacharle.xyz> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/08/27 20:34:25 by charles #+# #+# */
-/* Updated: 2020/09/10 14:48:42 by charles ### ########.fr */
+/* Updated: 2020/09/10 20:29:17 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -20,23 +20,10 @@
** error.c
*/
-enum
-{
- ERR_NONE = 1024,
- ERR_FATAL,
- ERR_SYNTAX,
- ERR_OPEN,
- ERR_AMBIGUOUS_REDIR,
- ERR_CMD_NOT_FOUND,
- ERR_IS_DIRECTORY,
- ERR_ERRNO,
-};
-
-typedef int t_err;
+# define EVAL_FATAL 1024
void errorf(const char *format, ...);
void verrorf(const char *format, va_list ap);
-void error_set_status(int status);
-int error_get_status(int status);
+int errorf_ret(int err, const char *format, ...);
#endif
diff --git a/src/builtin/cd.c b/src/builtin/cd.c
index d7115e5..5f7cdd8 100644
--- a/src/builtin/cd.c
+++ b/src/builtin/cd.c
@@ -6,7 +6,7 @@
/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/01 17:10:20 by charles #+# #+# */
-/* Updated: 2020/07/19 19:05:25 by charles ### ########.fr */
+/* Updated: 2020/09/10 19:40:31 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -24,27 +24,18 @@ int builtin_cd(char **argv, t_env env)
(void)env;
if (argv[1] != NULL && argv[2] != NULL)
- {
- errorf("cd: too many arguments\n");
- return (1);
- }
+ return (errorf_ret(1, "cd: too many arguments\n"));
if (argv[1] != NULL && argv[1][0] == '\0')
return (0);
if (argv[1] == NULL)
{
if ((home = env_search(env, "HOME")) == NULL)
- {
- errorf("cd: HOME not set\n");
- return (1);
- }
+ return (errorf_ret(1, "cd: HOME not set\n"));
argv[1] = home;
}
errno = 0;
if (chdir(argv[1]) == -1)
- {
- errorf("cd: %s: %s\n", argv[1], strerror(errno));
- return (1);
- }
+ return (errorf_ret(1, "cd: %s: %s\n", argv[1], strerror(errno)));
if (!(getcwd(buf, PATH_MAX)))
return (1);
if (env_export(env, "PWD", buf) == NULL)
diff --git a/src/builtin/exit.c b/src/builtin/exit.c
index 52a4462..a24efad 100644
--- a/src/builtin/exit.c
+++ b/src/builtin/exit.c
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/01 17:10:16 by charles #+# #+# */
-/* Updated: 2020/08/20 17:31:18 by charles ### ########.fr */
+/* Updated: 2020/09/10 19:41:03 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -32,15 +32,9 @@ int builtin_exit(char **argv, t_env env)
while (ft_isblank(*after))
after++;
if (*after != '\0' || errno == ERANGE)
- {
- errorf("exit: %s: numeric argument required\n", argv[1]);
- return (2);
- }
+ return (errorf_ret(2, "exit: %s: numeric argument required\n", argv[1]));
if (argv[2] != NULL)
- {
- errorf("exit: too many arguments\n");
- return (1);
- }
+ return (errorf_ret(1, "exit: too many arguments\n"));
}
exit(status % 256);
return (0);
diff --git a/src/error.c b/src/error.c
index 6c24635..2e56117 100644
--- a/src/error.c
+++ b/src/error.c
@@ -6,7 +6,7 @@
/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/14 11:02:52 by charles #+# #+# */
-/* Updated: 2020/09/10 14:48:19 by charles ### ########.fr */
+/* Updated: 2020/09/10 20:29:41 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -19,7 +19,7 @@
** \note NULL arguments are ignored
*/
-void errorf(const char *format, ...)
+void errorf(const char *format, ...)
{
va_list ap;
@@ -28,7 +28,11 @@ void errorf(const char *format, ...)
va_end(ap);
}
-void verrorf(const char *format, va_list ap)
+/*
+** \brief errorf with an argument pointer (ap) instead of arguments
+*/
+
+void verrorf(const char *format, va_list ap)
{
char *arg;
@@ -50,28 +54,16 @@ void verrorf(const char *format, va_list ap)
}
}
-static int g_error_to_status[] = {
- [ERR_NONE] = 0,
- [ERR_AMBIGUOUS_REDIR] = 1,
- [ERR_OPEN] = 1,
- [ERR_CMD_NOT_FOUND] = 127,
- [ERR_SYNTAX] = 2,
- [ERR_IS_DIRECTORY] = 126,
- [ERR_ERRNO] = 126,
-};
+/*
+** \brief errorf helper to return an status code and print the error
+*/
-int error_get_status(int status)
+int errorf_ret(int status, const char *format, ...)
{
- return (g_error_to_status[status]);
-}
+ va_list ap;
-void error_set_status(int status)
-{
- if (status == ERR_FATAL)
- exit(1);
- /* printf("%d\n", status); */
- if (status < ERR_NONE)
- g_last_status = status;
- else
- g_last_status = g_error_to_status[status];
+ va_start(ap, format);
+ verrorf(format, ap);
+ va_end(ap);
+ return (status);
}
diff --git a/src/eval/cmd.c b/src/eval/cmd.c
index 107a2a6..36e5a00 100644
--- a/src/eval/cmd.c
+++ b/src/eval/cmd.c
@@ -6,7 +6,7 @@
/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/14 10:41:31 by charles #+# #+# */
-/* Updated: 2020/09/10 15:05:54 by charles ### ########.fr */
+/* Updated: 2020/09/10 20:32:13 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -33,20 +33,19 @@ int fork_wrap(
pid_t child_pid;
if ((child_pid = fork()) == -1)
- return (ERR_FATAL);
+ return (EVAL_FATAL);
if (child_pid == 0)
{
if ((fds[FD_READ] != FD_NONE && dup2(fds[FD_READ], STDIN_FILENO) == -1) ||
(fds[FD_WRITE] != FD_NONE && dup2(fds[FD_WRITE], STDOUT_FILENO) == -1))
exit(EXIT_FAILURE);
- if ((status = wrapped(passed)) == ERR_FATAL)
+ if ((status = wrapped(passed)) == EVAL_FATAL) // FIXME detect fatal in child (pipe ?)
exit(EXIT_FAILURE);
exit(status);
}
g_child_pid = child_pid;
wait(&child_pid);
close(fds[FD_WRITE]);
- // also read end?
return (WEXITSTATUS(child_pid));
}
@@ -61,31 +60,25 @@ int forked_cmd(void *void_param)
if (ft_vecswallow_at(param->env, param->env->size - 1, param->env_local) == NULL)
{
ft_vecdestroy(param->env_local, free);
- return (ERR_FATAL);
+ return (EVAL_FATAL);
}
if (param->builtin != NULL)
return (param->builtin->func(param->argv, param->env));
else
{
if (stat(param->exec_path, &statbuf) == -1)
- {
- errorf("%s: %s\n", param->exec_path, strerror(errno));
- return (error_get_status(ERR_ERRNO));
- }
+ return (errorf_ret(126, "%s: %s\n", param->exec_path, strerror(errno)));
if (S_ISDIR(statbuf.st_mode))
- {
- errorf("%s: Is a directory\n", param->exec_path);
- return (error_get_status(ERR_IS_DIRECTORY));
- }
-
+ return (errorf_ret(126, "%s: Is a directory\n", param->exec_path));
+ if (!(statbuf.st_mode & 0444))
+ return (errorf_ret(126, "%s: %s\n", param->exec_path, strerror(EACCES)));
errno = 0;
status = execve(param->exec_path, param->argv, (char**)param->env->data);
if (status == -1)
{
if (errno == ENOEXEC)
- return (error_get_status(ERR_NONE));
- errorf("%s: %s\n", param->exec_path, strerror(errno));
- return (error_get_status(ERR_ERRNO));
+ return (0);
+ return (errorf_ret(126, "%s: %s\n", param->exec_path, strerror(errno)));
}
return (status);
}
@@ -97,16 +90,12 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast)
char **argv;
int status;
- if ((status = redir_extract(&ast->redirs, env, fds)) != ERR_NONE)
- {
- ast->redirs = NULL;
+ if ((status = redir_extract(&ast->redirs, env, fds)) != 0)
return (status);
- }
- ast->redirs = NULL;
if ((param.env_local = env_from_array((char*[]){NULL})) == NULL)
- return (ERR_FATAL);
+ return (EVAL_FATAL);
if (!variable_extract(&ast->cmd_argv, env, param.env_local))
- return (ERR_FATAL);
+ return (EVAL_FATAL);
/* char **strs = preprocess(&start, env); */
/* */
@@ -127,7 +116,7 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast)
if ((argv = preprocess(&ast->cmd_argv, env)) == NULL)
{
ast->cmd_argv = NULL;
- return (ERR_FATAL);
+ return (EVAL_FATAL);
}
if (argv[0] == NULL)
@@ -141,7 +130,7 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast)
{
errorf("%s: command not found\n", argv[0]);
ft_split_destroy(argv);
- return (ERR_CMD_NOT_FOUND);
+ return (127);
}
}
else if (!param.builtin->forked)
@@ -150,8 +139,8 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast)
param.argv = argv;
param.env = env;
status = fork_wrap(fds, &param, &forked_cmd);
- /* g_last_status = status; */
ft_split_destroy(argv);
ft_vecdestroy(param.env_local, free);
+ g_last_status = status;
return (status);
}
diff --git a/src/eval/op.c b/src/eval/op.c
index e06c959..9a37736 100644
--- a/src/eval/op.c
+++ b/src/eval/op.c
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/17 15:27:22 by charles #+# #+# */
-/* Updated: 2020/09/10 14:15:08 by charles ### ########.fr */
+/* Updated: 2020/09/10 20:28:57 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -29,8 +29,9 @@ int eval_op(int fds[2], t_env env, t_path path, t_ast *ast)
left_fds[FD_WRITE] = p[FD_WRITE];
right_fds[FD_READ] = p[FD_READ];
}
- if ((status = eval(left_fds, env, path, ast->op.left)) == -1)
- return (-1);
+ if ((status = eval(left_fds, env, path, ast->op.left)) == EVAL_FATAL)
+ return (EVAL_FATAL);
+ g_last_status = status;
if ((ast->op.sep == TAG_AND && status != 0) ||
(ast->op.sep == TAG_PIPE && status != 0) ||
(ast->op.sep == TAG_OR && status == 0))
@@ -49,9 +50,10 @@ int wrapped_eval(void *void_param)
int eval_parent(int fds[2], t_env env, t_path path, t_ast *ast)
{
t_fork_param_parent param;
+ int status;
- if (!redir_extract(&ast->redirs, env, fds))
- return (ERR_FATAL);
+ if ((status = redir_extract(&ast->redirs, env, fds)) != 0)
+ return (status);
param.fds[0] = fds[0];
param.fds[1] = fds[1];
param.env = env;
@@ -69,5 +71,5 @@ int eval(int fds[2], t_env env, t_path path, t_ast *ast)
return (eval_op(fds, env, path, ast));
if (ast->tag == AST_CMD)
return (eval_cmd(fds, env, path, ast));
- return (ERR_FATAL);
+ return (EVAL_FATAL);
}
diff --git a/src/eval/redir.c b/src/eval/redir.c
index 9beef17..39e202d 100644
--- a/src/eval/redir.c
+++ b/src/eval/redir.c
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/15 11:05:34 by charles #+# #+# */
-/* Updated: 2020/09/10 14:25:08 by charles ### ########.fr */
+/* Updated: 2020/09/10 20:25:59 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -24,9 +24,9 @@ static int st_open_replace(int *fd, char *filename, int oflag)
{
errorf("%s: %s\n", filename, strerror(errno));
free(filename);
- return (ERR_OPEN);
+ return (1);
}
- return (ERR_NONE);
+ return (0);
}
int redir_extract(
@@ -39,10 +39,10 @@ int redir_extract(
char *filename;
if (*redirs == NULL)
- return (ERR_NONE);
+ return (0);
if (!((*redirs)->tag & TAG_IS_REDIR) || (*redirs)->next == NULL
|| !((*redirs)->next->tag & TAG_IS_STR))
- return (ERR_FATAL);
+ return (EVAL_FATAL);
curr = (*redirs)->next;
after = NULL;
while (curr != NULL && curr->tag & TAG_IS_STR)
@@ -58,18 +58,18 @@ int redir_extract(
{
tok_lst_destroy(redirs, free);
tok_lst_destroy(&after, free);
- return (ERR_FATAL);
+ return (EVAL_FATAL);
}
if (((*redirs)->tag == TAG_REDIR_IN
- && st_open_replace(&fds[FD_READ], filename, O_RDONLY) != ERR_NONE)
+ && st_open_replace(&fds[FD_READ], filename, O_RDONLY) != 0)
|| ((*redirs)->tag == TAG_REDIR_OUT
- && st_open_replace(&fds[FD_WRITE], filename, O_WRONLY | O_CREAT | O_TRUNC) != ERR_NONE)
+ && st_open_replace(&fds[FD_WRITE], filename, O_WRONLY | O_CREAT | O_TRUNC) != 0)
|| ((*redirs)->tag == TAG_REDIR_APPEND
- && st_open_replace(&fds[FD_WRITE], filename, O_WRONLY | O_CREAT | O_APPEND) != ERR_NONE))
+ && st_open_replace(&fds[FD_WRITE], filename, O_WRONLY | O_CREAT | O_APPEND) != 0))
{
tok_lst_destroy(redirs, free);
tok_lst_destroy(&after, free);
- return (ERR_FATAL);
+ return (EVAL_FATAL);
}
tok_lst_destroy(redirs, free);
free(filename);
diff --git a/src/main.c b/src/main.c
index 71d49bb..e8275f5 100644
--- a/src/main.c
+++ b/src/main.c
@@ -6,7 +6,7 @@
/* By: cacharle <cacharle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */
-/* Updated: 2020/09/10 14:33:22 by charles ### ########.fr */
+/* Updated: 2020/09/10 20:26:19 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -30,7 +30,6 @@ void tok_lst_debug(t_tok_lst *tokens);
** TODO
** $?
** concurrent pipeline
-** cmd variable preprocess
** signal on whole line instead of single command
** env local to current minishell process
*/
@@ -106,7 +105,10 @@ int main(int argc, char **argv, char **envp)
/* ft_lstiter(parser_out->ast->redirs, token_debug); */
int fds[2] = {FD_NONE, FD_NONE};
int status = eval(fds, env, path, parser_out->ast);
- error_set_status(status);
+ if (status == EVAL_FATAL)
+ exit(1);
+ g_last_status = status;
+ /* error_set_status(status); */
}
else
{
@@ -136,9 +138,10 @@ int main(int argc, char **argv, char **envp)
int fds[2] = {FD_NONE, FD_NONE};
int status = eval(fds, env, path, parser_out->ast);
- if (status == ERR_FATAL)
+ if (status == EVAL_FATAL)
exit(1);
- error_set_status(status);
+ g_last_status = status;
+ /* error_set_status(status); */
print_prompt();
}
if (ret != FTGL_EOF)
diff --git a/src/parser/parsed.c b/src/parser/parsed.c
index 4e6d6f2..c577b32 100644
--- a/src/parser/parsed.c
+++ b/src/parser/parsed.c
@@ -6,13 +6,13 @@
/* By: charles <me@cacharle.xyz> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/08/27 20:27:42 by charles #+# #+# */
-/* Updated: 2020/08/28 10:44:39 by charles ### ########.fr */
+/* Updated: 2020/09/10 19:31:46 by charles ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser.h"
-t_parsed *parsed_new(t_ast *ast, t_tok_lst *rest)
+t_parsed *parsed_new(t_ast *ast, t_tok_lst *rest)
{
t_parsed *ret;
@@ -24,7 +24,7 @@ t_parsed *parsed_new(t_ast *ast, t_tok_lst *rest)
return ret;
}
-t_parsed *parsed_error(const char *format, ...)
+t_parsed *parsed_error(const char *format, ...)
{
t_parsed *err;
va_list ap;