source: grub-pc/trunk/fuentes/grub-core/kern/err.c @ 22

Last change on this file since 22 was 22, checked in by mabarracus, 4 years ago

updated version and apply net.ifnames=0 into debian/rules

File size: 3.2 KB
Line 
1/* err.c - error handling routines */
2/*
3 *  GRUB  --  GRand Unified Bootloader
4 *  Copyright (C) 2002,2005,2007,2008  Free Software Foundation, Inc.
5 *
6 *  GRUB is free software: you can redistribute it and/or modify
7 *  it under the terms of the GNU General Public License as published by
8 *  the Free Software Foundation, either version 3 of the License, or
9 *  (at your option) any later version.
10 *
11 *  GRUB is distributed in the hope that it will be useful,
12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 *  GNU General Public License for more details.
15 *
16 *  You should have received a copy of the GNU General Public License
17 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <grub/err.h>
21#include <grub/misc.h>
22#include <stdarg.h>
23#include <grub/i18n.h>
24
25#define GRUB_ERROR_STACK_SIZE   10
26
27grub_err_t grub_errno;
28char grub_errmsg[GRUB_MAX_ERRMSG];
29int grub_err_printed_errors;
30
31static struct grub_error_saved grub_error_stack_items[GRUB_ERROR_STACK_SIZE];
32
33static int grub_error_stack_pos;
34static int grub_error_stack_assert;
35
36grub_err_t
37grub_error (grub_err_t n, const char *fmt, ...)
38{
39  va_list ap;
40
41  grub_errno = n;
42
43  va_start (ap, fmt);
44  grub_vsnprintf (grub_errmsg, sizeof (grub_errmsg), _(fmt), ap);
45  va_end (ap);
46
47  return n;
48}
49
50void
51grub_error_push (void)
52{
53  /* Only add items to stack, if there is enough room.  */
54  if (grub_error_stack_pos < GRUB_ERROR_STACK_SIZE)
55    {
56      /* Copy active error message to stack.  */
57      grub_error_stack_items[grub_error_stack_pos].grub_errno = grub_errno;
58      grub_memcpy (grub_error_stack_items[grub_error_stack_pos].errmsg,
59                   grub_errmsg,
60                   sizeof (grub_errmsg));
61
62      /* Advance to next error stack position.  */
63      grub_error_stack_pos++;
64    }
65  else
66    {
67      /* There is no room for new error message. Discard new error message
68         and mark error stack assertion flag.  */
69      grub_error_stack_assert = 1;
70    }
71
72  /* Allow further operation of other components by resetting
73     active errno to GRUB_ERR_NONE.  */
74  grub_errno = GRUB_ERR_NONE;
75}
76
77int
78grub_error_pop (void)
79{
80  if (grub_error_stack_pos > 0)
81    {
82      /* Pop error message from error stack to current active error.  */
83      grub_error_stack_pos--;
84
85      grub_errno = grub_error_stack_items[grub_error_stack_pos].grub_errno;
86      grub_memcpy (grub_errmsg,
87                   grub_error_stack_items[grub_error_stack_pos].errmsg,
88                   sizeof (grub_errmsg));
89
90      return 1;
91    }
92  else
93    {
94      /* There is no more items on error stack, reset to no error state.  */
95      grub_errno = GRUB_ERR_NONE;
96
97      return 0;
98    }
99}
100
101void
102grub_print_error (void)
103{
104  /* Print error messages in reverse order. First print active error message
105     and then empty error stack.  */
106  do
107    {
108      if (grub_errno != GRUB_ERR_NONE)
109        {
110          grub_err_printf (_("error: %s.\n"), grub_errmsg);
111          grub_err_printed_errors++;
112        }
113    }
114  while (grub_error_pop ());
115
116  /* If there was an assert while using error stack, report about it.  */
117  if (grub_error_stack_assert)
118    {
119      grub_err_printf ("assert: error stack overflow detected!\n");
120      grub_error_stack_assert = 0;
121    }
122}
Note: See TracBrowser for help on using the repository browser.