2009-11-02 Samuel Thibault <samuel.thibault@ens-lyon.org>
[grub2/jjazz.git] / normal / color.c
blob340e43a0281590a05215380514e959a484647aa4
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>
24 /* Borrowed from GRUB Legacy */
25 static char *color_list[16] =
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"
45 static int
46 parse_color_name (grub_uint8_t *ret, char *name)
48 grub_uint8_t i;
49 for (i = 0; i < sizeof (color_list) / sizeof (*color_list); i++)
50 if (! grub_strcmp (name, color_list[i]))
52 *ret = i;
53 return 0;
55 return -1;
58 void
59 grub_parse_color_name_pair (grub_uint8_t *ret, const char *name)
61 grub_uint8_t fg, bg;
62 char *fg_name, *bg_name;
64 /* nothing specified by user */
65 if (name == NULL)
66 return;
68 fg_name = grub_strdup (name);
69 if (fg_name == NULL)
71 /* "out of memory" message was printed by grub_strdup() */
72 grub_wait_after_message ();
73 return;
76 bg_name = grub_strchr (fg_name, '/');
77 if (bg_name == NULL)
79 grub_printf ("Warning: syntax error (missing slash) in `%s'\n", fg_name);
80 grub_wait_after_message ();
81 goto free_and_return;
84 *(bg_name++) = '\0';
86 if (parse_color_name (&fg, fg_name) == -1)
88 grub_printf ("Warning: invalid foreground color `%s'\n", fg_name);
89 grub_wait_after_message ();
90 goto free_and_return;
92 if (parse_color_name (&bg, bg_name) == -1)
94 grub_printf ("Warning: invalid background color `%s'\n", bg_name);
95 grub_wait_after_message ();
96 goto free_and_return;
99 *ret = (bg << 4) | fg;
101 free_and_return:
102 grub_free (fg_name);
105 /* Replace default `normal' colors with the ones specified by user (if any). */
106 char *
107 grub_env_write_color_normal (struct grub_env_var *var __attribute__ ((unused)),
108 const char *val)
110 grub_uint8_t color_normal, color_highlight;
112 /* Use old settings in case grub_parse_color_name_pair() has no effect. */
113 grub_getcolor (&color_normal, &color_highlight);
115 grub_parse_color_name_pair (&color_normal, val);
117 /* Reloads terminal `normal' and `highlight' colors. */
118 grub_setcolor (color_normal, color_highlight);
120 /* Propagates `normal' color to terminal current color. */
121 grub_setcolorstate (GRUB_TERM_COLOR_NORMAL);
123 return grub_strdup (val);
126 /* Replace default `highlight' colors with the ones specified by user (if any). */
127 char *
128 grub_env_write_color_highlight (struct grub_env_var *var __attribute__ ((unused)),
129 const char *val)
131 grub_uint8_t color_normal, color_highlight;
133 /* Use old settings in case grub_parse_color_name_pair() has no effect. */
134 grub_getcolor (&color_normal, &color_highlight);
136 grub_parse_color_name_pair (&color_highlight, val);
138 /* Reloads terminal `normal' and `highlight' colors. */
139 grub_setcolor (color_normal, color_highlight);
141 /* Propagates `normal' color to terminal current color.
142 Note: Using GRUB_TERM_COLOR_NORMAL here rather than
143 GRUB_TERM_COLOR_HIGHLIGHT is intentional. We don't want to switch
144 to highlight state just because color was reloaded. */
145 grub_setcolorstate (GRUB_TERM_COLOR_NORMAL);
147 return grub_strdup (val);