color.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 1999,2000,2001,2002,2003,2004,2008 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/misc.h>
  19. #include <grub/mm.h>
  20. #include <grub/normal_menu.h>
  21. #include <grub/term.h>
  22. #include <grub/lib.h>
  23. #include <grub/i18n.h>
  24. /* Borrowed from GRUB Legacy */
  25. static char *color_list[16] =
  26. {
  27. "black",
  28. "blue",
  29. "green",
  30. "cyan",
  31. "red",
  32. "magenta",
  33. "brown",
  34. "light-gray",
  35. "dark-gray",
  36. "light-blue",
  37. "light-green",
  38. "light-cyan",
  39. "light-red",
  40. "light-magenta",
  41. "yellow",
  42. "white"
  43. };
  44. static int
  45. parse_color_name (grub_uint8_t *ret, char *name)
  46. {
  47. grub_uint8_t i;
  48. for (i = 0; i < sizeof (color_list) / sizeof (*color_list); i++)
  49. if (! grub_strcmp (name, color_list[i]))
  50. {
  51. *ret = i;
  52. return 0;
  53. }
  54. return -1;
  55. }
  56. void
  57. grub_parse_color_name_pair (grub_uint8_t *ret, const char *name)
  58. {
  59. grub_uint8_t fg, bg;
  60. char *fg_name, *bg_name;
  61. /* nothing specified by user */
  62. if (name == NULL)
  63. return;
  64. fg_name = grub_strdup (name);
  65. if (fg_name == NULL)
  66. {
  67. /* "out of memory" message was printed by grub_strdup() */
  68. grub_wait_after_message ();
  69. return;
  70. }
  71. bg_name = grub_strchr (fg_name, '/');
  72. if (bg_name == NULL)
  73. {
  74. grub_printf (N_("Warning: syntax error (missing slash) in `%s'\n"), fg_name);
  75. grub_wait_after_message ();
  76. goto free_and_return;
  77. }
  78. *(bg_name++) = '\0';
  79. if (parse_color_name (&fg, fg_name) == -1)
  80. {
  81. grub_printf (N_("Warning: invalid foreground color `%s'\n"), fg_name);
  82. grub_wait_after_message ();
  83. goto free_and_return;
  84. }
  85. if (parse_color_name (&bg, bg_name) == -1)
  86. {
  87. grub_printf (N_("Warning: invalid background color `%s'\n"), bg_name);
  88. grub_wait_after_message ();
  89. goto free_and_return;
  90. }
  91. *ret = (bg << 4) | fg;
  92. free_and_return:
  93. grub_free (fg_name);
  94. }
  95. static grub_uint8_t color_normal, color_highlight;
  96. static void
  97. set_colors (void)
  98. {
  99. struct grub_term_output *term;
  100. FOR_ACTIVE_TERM_OUTPUTS(term)
  101. {
  102. /* Reloads terminal `normal' and `highlight' colors. */
  103. grub_term_setcolor (term, color_normal, color_highlight);
  104. /* Propagates `normal' color to terminal current color. */
  105. grub_term_setcolorstate (term, GRUB_TERM_COLOR_NORMAL);
  106. }
  107. }
  108. /* Replace default `normal' colors with the ones specified by user (if any). */
  109. char *
  110. grub_env_write_color_normal (struct grub_env_var *var __attribute__ ((unused)),
  111. const char *val)
  112. {
  113. grub_parse_color_name_pair (&color_normal, val);
  114. set_colors ();
  115. return grub_strdup (val);
  116. }
  117. /* Replace default `highlight' colors with the ones specified by user (if any). */
  118. char *
  119. grub_env_write_color_highlight (struct grub_env_var *var __attribute__ ((unused)),
  120. const char *val)
  121. {
  122. grub_parse_color_name_pair (&color_highlight, val);
  123. set_colors ();
  124. return grub_strdup (val);
  125. }