From 6e456095fce214a2eadea0cafa3fa7ea1113b4e6 Mon Sep 17 00:00:00 2001 From: Charles Date: Sun, 19 Jul 2020 19:27:52 +0200 Subject: Added errof for cleaner error message, Removed previous error functions --- src/error.c | 73 ++++++++++++++++++++++--------------------------------------- 1 file changed, 26 insertions(+), 47 deletions(-) (limited to 'src/error.c') 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 +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); } -- cgit