aboutsummaryrefslogtreecommitdiff
path: root/src/error.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-07-19 19:27:52 +0200
committerCharles <sircharlesaze@gmail.com>2020-07-19 19:27:52 +0200
commit6e456095fce214a2eadea0cafa3fa7ea1113b4e6 (patch)
tree42d255dc5042f1df8079567e7ca3426d22cba24d /src/error.c
parenta478d95bed0f24f9783714600af48a6c1cde2aae (diff)
downloadminishell-6e456095fce214a2eadea0cafa3fa7ea1113b4e6.tar.gz
minishell-6e456095fce214a2eadea0cafa3fa7ea1113b4e6.tar.bz2
minishell-6e456095fce214a2eadea0cafa3fa7ea1113b4e6.zip
Added errof for cleaner error message, Removed previous error functions
Diffstat (limited to 'src/error.c')
-rw-r--r--src/error.c73
1 files changed, 26 insertions, 47 deletions
diff --git a/src/error.c b/src/error.c
index fe5b4b1..0d2095e 100644
--- a/src/error.c
+++ b/src/error.c
@@ -6,61 +6,40 @@
/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/14 11:02:52 by charles #+# #+# */
-/* Updated: 2020/07/19 15:34:20 by charles ### ########.fr */
+/* Updated: 2020/07/19 19:20: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"},
-};
-
-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;
+/*
+** \brief printf like function that only works with `%s`,
+** prefix the message with the program name
+** and output on STDERR
+** \note NULL arguments are ignored
+*/
- err = st_error_get(id);
- ft_putstr_fd(g_basename, STDERR_FILENO);
- ft_putstr_fd(": ", 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)
+void errorf(const char *format, ...)
{
- return (st_error_get(id)->status);
-}
+ va_list ap;
+ char *arg;
-void error_put_invalid_identifier(char *prefix, char *identifier)
-{
+ va_start(ap, format);
ft_putstr_fd(g_basename, STDERR_FILENO);
ft_putstr_fd(": ", STDERR_FILENO);
- ft_putstr_fd(prefix, STDERR_FILENO);
- ft_putstr_fd(": `", STDERR_FILENO);
- ft_putstr_fd(identifier, STDERR_FILENO);
- ft_putstr_fd("': not a valid identifier\n", STDERR_FILENO);
+ while (*format != '\0')
+ {
+ if (format[0] == '%' && format[1] == 's')
+ {
+ arg = va_arg(ap, char*);
+ ft_putstr_fd(arg, STDERR_FILENO);
+ format += 2;
+ }
+ else
+ {
+ ft_putchar_fd(*format, STDERR_FILENO);
+ format++;
+ }
+ }
+ va_end(ap);
}