err.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #include <grub/err.h>
  20. #include <grub/misc.h>
  21. #include <stdarg.h>
  22. #include <grub/i18n.h>
  23. #define GRUB_ERROR_STACK_SIZE 10
  24. grub_err_t grub_errno;
  25. char grub_errmsg[GRUB_MAX_ERRMSG];
  26. int grub_err_printed_errors;
  27. static struct grub_error_saved grub_error_stack_items[GRUB_ERROR_STACK_SIZE];
  28. static int grub_error_stack_pos;
  29. static int grub_error_stack_assert;
  30. grub_err_t
  31. grub_error (grub_err_t n, const char *fmt, ...)
  32. {
  33. va_list ap;
  34. grub_errno = n;
  35. va_start (ap, fmt);
  36. grub_vsnprintf (grub_errmsg, sizeof (grub_errmsg), _(fmt), ap);
  37. va_end (ap);
  38. return n;
  39. }
  40. void
  41. grub_error_push (void)
  42. {
  43. /* Only add items to stack, if there is enough room. */
  44. if (grub_error_stack_pos < GRUB_ERROR_STACK_SIZE)
  45. {
  46. /* Copy active error message to stack. */
  47. grub_error_stack_items[grub_error_stack_pos].grub_errno = grub_errno;
  48. grub_memcpy (grub_error_stack_items[grub_error_stack_pos].errmsg,
  49. grub_errmsg,
  50. sizeof (grub_errmsg));
  51. /* Advance to next error stack position. */
  52. grub_error_stack_pos++;
  53. }
  54. else
  55. {
  56. /* There is no room for new error message. Discard new error message
  57. and mark error stack assertion flag. */
  58. grub_error_stack_assert = 1;
  59. }
  60. /* Allow further operation of other components by resetting
  61. active errno to GRUB_ERR_NONE. */
  62. grub_errno = GRUB_ERR_NONE;
  63. }
  64. int
  65. grub_error_pop (void)
  66. {
  67. if (grub_error_stack_pos > 0)
  68. {
  69. /* Pop error message from error stack to current active error. */
  70. grub_error_stack_pos--;
  71. grub_errno = grub_error_stack_items[grub_error_stack_pos].grub_errno;
  72. grub_memcpy (grub_errmsg,
  73. grub_error_stack_items[grub_error_stack_pos].errmsg,
  74. sizeof (grub_errmsg));
  75. return 1;
  76. }
  77. else
  78. {
  79. /* There is no more items on error stack, reset to no error state. */
  80. grub_errno = GRUB_ERR_NONE;
  81. return 0;
  82. }
  83. }
  84. void
  85. grub_print_error (void)
  86. {
  87. /* Print error messages in reverse order. First print active error message
  88. and then empty error stack. */
  89. do
  90. {
  91. if (grub_errno != GRUB_ERR_NONE)
  92. {
  93. grub_err_printf (_("error: %s.\n"), grub_errmsg);
  94. grub_err_printed_errors++;
  95. }
  96. }
  97. while (grub_error_pop ());
  98. /* If there was an assert while using error stack, report about it. */
  99. if (grub_error_stack_assert)
  100. {
  101. grub_err_printf ("assert: error stack overflow detected!\n");
  102. grub_error_stack_assert = 0;
  103. }
  104. }