2N/A/* err.c - error handling routines */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2002,2005,2007,2008 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/err.h>
2N/A#include <grub/misc.h>
2N/A#include <stdarg.h>
2N/A#include <grub/i18n.h>
2N/A
2N/A#define GRUB_MAX_ERRMSG 256
2N/A#define GRUB_ERROR_STACK_SIZE 10
2N/A
2N/Agrub_err_t grub_errno;
2N/Achar grub_errmsg[GRUB_MAX_ERRMSG];
2N/Aint grub_err_printed_errors;
2N/A
2N/Astatic struct
2N/A{
2N/A grub_err_t errno;
2N/A char errmsg[GRUB_MAX_ERRMSG];
2N/A} grub_error_stack_items[GRUB_ERROR_STACK_SIZE];
2N/A
2N/Astatic int grub_error_stack_pos;
2N/Astatic int grub_error_stack_assert;
2N/A
2N/Agrub_err_t
2N/Agrub_error (grub_err_t n, const char *fmt, ...)
2N/A{
2N/A va_list ap;
2N/A
2N/A grub_errno = n;
2N/A
2N/A va_start (ap, fmt);
2N/A grub_vsnprintf (grub_errmsg, sizeof (grub_errmsg), _(fmt), ap);
2N/A va_end (ap);
2N/A
2N/A return n;
2N/A}
2N/A
2N/Avoid
2N/Agrub_fatal (const char *fmt, ...)
2N/A{
2N/A va_list ap;
2N/A
2N/A va_start (ap, fmt);
2N/A grub_vprintf (_(fmt), ap);
2N/A va_end (ap);
2N/A
2N/A grub_abort ();
2N/A}
2N/A
2N/Avoid
2N/Agrub_error_push (void)
2N/A{
2N/A /* Only add items to stack, if there is enough room. */
2N/A if (grub_error_stack_pos < GRUB_ERROR_STACK_SIZE)
2N/A {
2N/A /* Copy active error message to stack. */
2N/A grub_error_stack_items[grub_error_stack_pos].errno = grub_errno;
2N/A grub_memcpy (grub_error_stack_items[grub_error_stack_pos].errmsg,
2N/A grub_errmsg,
2N/A sizeof (grub_errmsg));
2N/A
2N/A /* Advance to next error stack position. */
2N/A grub_error_stack_pos++;
2N/A }
2N/A else
2N/A {
2N/A /* There is no room for new error message. Discard new error message
2N/A and mark error stack assertion flag. */
2N/A grub_error_stack_assert = 1;
2N/A }
2N/A
2N/A /* Allow further operation of other components by resetting
2N/A active errno to GRUB_ERR_NONE. */
2N/A grub_errno = GRUB_ERR_NONE;
2N/A}
2N/A
2N/Aint
2N/Agrub_error_pop (void)
2N/A{
2N/A if (grub_error_stack_pos > 0)
2N/A {
2N/A /* Pop error message from error stack to current active error. */
2N/A grub_error_stack_pos--;
2N/A
2N/A grub_errno = grub_error_stack_items[grub_error_stack_pos].errno;
2N/A grub_memcpy (grub_errmsg,
2N/A grub_error_stack_items[grub_error_stack_pos].errmsg,
2N/A sizeof (grub_errmsg));
2N/A
2N/A return 1;
2N/A }
2N/A else
2N/A {
2N/A /* There is no more items on error stack, reset to no error state. */
2N/A grub_errno = GRUB_ERR_NONE;
2N/A
2N/A return 0;
2N/A }
2N/A}
2N/A
2N/Avoid
2N/Agrub_print_error (void)
2N/A{
2N/A /* Print error messages in reverse order. First print active error message
2N/A and then empty error stack. */
2N/A do
2N/A {
2N/A if (grub_errno != GRUB_ERR_NONE)
2N/A {
2N/A grub_err_printf (_("error: %s.\n"), grub_errmsg);
2N/A grub_err_printed_errors++;
2N/A }
2N/A }
2N/A while (grub_error_pop ());
2N/A
2N/A /* If there was an assert while using error stack, report about it. */
2N/A if (grub_error_stack_assert)
2N/A {
2N/A grub_err_printf ("assert: error stack overflow detected!\n");
2N/A grub_error_stack_assert = 0;
2N/A }
2N/A}