Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / normal / color.c
blob06f1a877c2bca6c574797fac1fb16401af503fc4
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 1999,2000,2001,2002,2003,2004,2008 Free Software Foundation, Inc.
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.
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.
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/>.
19 #include <grub/misc.h>
20 #include <grub/mm.h>
21 #include <grub/normal.h>
22 #include <grub/term.h>
23 #include <grub/i18n.h>
25 /* Borrowed from GRUB Legacy */
26 static const char *color_list[16] =
28 "black",
29 "blue",
30 "green",
31 "cyan",
32 "red",
33 "magenta",
34 "brown",
35 "light-gray",
36 "dark-gray",
37 "light-blue",
38 "light-green",
39 "light-cyan",
40 "light-red",
41 "light-magenta",
42 "yellow",
43 "white"
46 static int
47 parse_color_name (grub_uint8_t *ret, char *name)
49 grub_uint8_t i;
50 for (i = 0; i < sizeof (color_list) / sizeof (*color_list); i++)
51 if (! grub_strcmp (name, color_list[i]))
53 *ret = i;
54 return 0;
56 return -1;
59 int
60 grub_parse_color_name_pair (grub_uint8_t *color, const char *name)
62 int result = 1;
63 grub_uint8_t fg, bg;
64 char *fg_name, *bg_name;
66 /* nothing specified by user */
67 if (name == NULL)
68 return result;
70 fg_name = grub_strdup (name);
71 if (fg_name == NULL)
73 /* "out of memory" message was printed by grub_strdup() */
74 grub_wait_after_message ();
75 return result;
78 bg_name = grub_strchr (fg_name, '/');
79 if (bg_name == NULL)
81 grub_printf_ (N_("Warning: syntax error (missing slash) in `%s'\n"), fg_name);
82 grub_wait_after_message ();
83 goto free_and_return;
86 *(bg_name++) = '\0';
88 if (parse_color_name (&fg, fg_name) == -1)
90 grub_printf_ (N_("Warning: invalid foreground color `%s'\n"), fg_name);
91 grub_wait_after_message ();
92 goto free_and_return;
94 if (parse_color_name (&bg, bg_name) == -1)
96 grub_printf_ (N_("Warning: invalid background color `%s'\n"), bg_name);
97 grub_wait_after_message ();
98 goto free_and_return;
101 *color = (bg << 4) | fg;
102 result = 0;
104 free_and_return:
105 grub_free (fg_name);
106 return result;
109 static grub_uint8_t color_normal, color_highlight;
111 static void
112 set_colors (void)
114 struct grub_term_output *term;
116 FOR_ACTIVE_TERM_OUTPUTS(term)
118 /* Reloads terminal `normal' and `highlight' colors. */
119 grub_term_setcolor (term, color_normal, color_highlight);
121 /* Propagates `normal' color to terminal current color. */
122 grub_term_setcolorstate (term, GRUB_TERM_COLOR_NORMAL);
126 /* Replace default `normal' colors with the ones specified by user (if any). */
127 char *
128 grub_env_write_color_normal (struct grub_env_var *var __attribute__ ((unused)),
129 const char *val)
131 if (grub_parse_color_name_pair (&color_normal, val))
132 return NULL;
134 set_colors ();
136 return grub_strdup (val);
139 /* Replace default `highlight' colors with the ones specified by user (if any). */
140 char *
141 grub_env_write_color_highlight (struct grub_env_var *var __attribute__ ((unused)),
142 const char *val)
144 if (grub_parse_color_name_pair (&color_highlight, val))
145 return NULL;
147 set_colors ();
149 return grub_strdup (val);