aboutsummaryrefslogtreecommitdiff
path: root/src/error.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-07-14 10:04:44 +0200
committerCharles <sircharlesaze@gmail.com>2020-07-14 10:05:57 +0200
commit29e1af2b65d097e533189db4e7d5e20534c17b35 (patch)
treec3f3033d18cab22e6715e2e57817d72028ae4627 /src/error.c
parent3ea9f3e7e9b40c8e0b17ab394576c82f7b92c0b4 (diff)
downloadminishell-29e1af2b65d097e533189db4e7d5e20534c17b35.tar.gz
minishell-29e1af2b65d097e533189db4e7d5e20534c17b35.tar.bz2
minishell-29e1af2b65d097e533189db4e7d5e20534c17b35.zip
Refactoring error handling during parsing
Diffstat (limited to 'src/error.c')
-rw-r--r--src/error.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/error.c b/src/error.c
new file mode 100644
index 0000000..f5848a6
--- /dev/null
+++ b/src/error.c
@@ -0,0 +1,57 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* error.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/06/14 11:02:52 by charles #+# #+# */
+/* Updated: 2020/07/14 09:23:38 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "eval.h"
+
+static t_error g_errors[] =
+{
+ {ERROR_AMBIGUOUS_REDIR, 1, "ambiguous redirect"},
+ {ERROR_OPEN, 1, NULL},
+ {ERROR_CMD_NOT_FOUND, 127, "command not found"},
+ /* {ERROR_CMD_FOUND_ERROR, 126, NULL}, */
+ {ERROR_SYNTAX, 2, "syntax error near unexpected token "},
+};
+
+t_error *st_error_get(enum e_error id)
+{
+ size_t i;
+ t_error *match;
+
+ match = NULL;
+ i = 0;
+ while (i < sizeof(g_errors) / sizeof(t_error))
+ {
+ if (g_errors[i].id == id)
+ match = &g_errors[i];
+ i++;
+ }
+ return (match);
+}
+
+void error_eval_put(enum e_error id, char *unexpected)
+{
+ t_error *err;
+
+ err = st_error_get(id);
+ ft_putstr_fd("minishell: ", STDERR_FILENO);
+ ft_putstr_fd(unexpected, STDERR_FILENO);
+ ft_putstr_fd(": ", STDERR_FILENO);
+ if (err->msg == NULL)
+ ft_putendl_fd(strerror(errno), STDERR_FILENO);
+ else
+ ft_putendl_fd(err->msg, STDERR_FILENO);
+}
+
+int error_status(enum e_error id)
+{
+ return (st_error_get(id)->status);
+}