2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 1999,2000,2001,2002,2003,2004,2008 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/misc.h>
2N/A#include <grub/mm.h>
2N/A#include <grub/normal.h>
2N/A#include <grub/term.h>
2N/A#include <grub/i18n.h>
2N/A
2N/A/* Borrowed from GRUB Legacy */
2N/Astatic const char *color_list[16] =
2N/A{
2N/A "black",
2N/A "blue",
2N/A "green",
2N/A "cyan",
2N/A "red",
2N/A "magenta",
2N/A "brown",
2N/A "light-gray",
2N/A "dark-gray",
2N/A "light-blue",
2N/A "light-green",
2N/A "light-cyan",
2N/A "light-red",
2N/A "light-magenta",
2N/A "yellow",
2N/A "white"
2N/A};
2N/A
2N/Astatic int
2N/Aparse_color_name (grub_uint8_t *ret, char *name)
2N/A{
2N/A grub_uint8_t i;
2N/A for (i = 0; i < sizeof (color_list) / sizeof (*color_list); i++)
2N/A if (! grub_strcmp (name, color_list[i]))
2N/A {
2N/A *ret = i;
2N/A return 0;
2N/A }
2N/A return -1;
2N/A}
2N/A
2N/Aint
2N/Agrub_parse_color_name_pair (grub_uint8_t *color, const char *name)
2N/A{
2N/A int result = 1;
2N/A grub_uint8_t fg, bg;
2N/A char *fg_name, *bg_name;
2N/A
2N/A /* nothing specified by user */
2N/A if (name == NULL)
2N/A return result;
2N/A
2N/A fg_name = grub_strdup (name);
2N/A if (fg_name == NULL)
2N/A {
2N/A /* "out of memory" message was printed by grub_strdup() */
2N/A grub_wait_after_message ();
2N/A return result;
2N/A }
2N/A
2N/A bg_name = grub_strchr (fg_name, '/');
2N/A if (bg_name == NULL)
2N/A {
2N/A grub_printf_ (N_("Warning: syntax error (missing slash) in `%s'\n"), fg_name);
2N/A grub_wait_after_message ();
2N/A goto free_and_return;
2N/A }
2N/A
2N/A *(bg_name++) = '\0';
2N/A
2N/A if (parse_color_name (&fg, fg_name) == -1)
2N/A {
2N/A grub_printf_ (N_("Warning: invalid foreground color `%s'\n"), fg_name);
2N/A grub_wait_after_message ();
2N/A goto free_and_return;
2N/A }
2N/A if (parse_color_name (&bg, bg_name) == -1)
2N/A {
2N/A grub_printf_ (N_("Warning: invalid background color `%s'\n"), bg_name);
2N/A grub_wait_after_message ();
2N/A goto free_and_return;
2N/A }
2N/A
2N/A *color = (bg << 4) | fg;
2N/A result = 0;
2N/A
2N/Afree_and_return:
2N/A grub_free (fg_name);
2N/A return result;
2N/A}
2N/A
2N/Astatic grub_uint8_t color_normal, color_highlight;
2N/A
2N/Astatic void
2N/Aset_colors (void)
2N/A{
2N/A struct grub_term_output *term;
2N/A
2N/A FOR_ACTIVE_TERM_OUTPUTS(term)
2N/A {
2N/A /* Reloads terminal `normal' and `highlight' colors. */
2N/A grub_term_setcolor (term, color_normal, color_highlight);
2N/A
2N/A /* Propagates `normal' color to terminal current color. */
2N/A grub_term_setcolorstate (term, GRUB_TERM_COLOR_NORMAL);
2N/A }
2N/A}
2N/A
2N/A/* Replace default `normal' colors with the ones specified by user (if any). */
2N/Achar *
2N/Agrub_env_write_color_normal (struct grub_env_var *var __attribute__ ((unused)),
2N/A const char *val)
2N/A{
2N/A if (grub_parse_color_name_pair (&color_normal, val))
2N/A return NULL;
2N/A
2N/A set_colors ();
2N/A
2N/A return grub_strdup (val);
2N/A}
2N/A
2N/A/* Replace default `highlight' colors with the ones specified by user (if any). */
2N/Achar *
2N/Agrub_env_write_color_highlight (struct grub_env_var *var __attribute__ ((unused)),
2N/A const char *val)
2N/A{
2N/A if (grub_parse_color_name_pair (&color_highlight, val))
2N/A return NULL;
2N/A
2N/A set_colors ();
2N/A
2N/A return grub_strdup (val);
2N/A}