color.c 3.37 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 1999,2000,2001,2002,2003,2004,2008  Free Software Foundation, Inc.
 *
 *  GRUB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <grub/misc.h>
#include <grub/mm.h>
#include <grub/normal.h>
#include <grub/term.h>
23
#include <grub/i18n.h>
24 25

/* Borrowed from GRUB Legacy */
26
static const char *color_list[16] =
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
{
  "black",
  "blue",
  "green",
  "cyan",
  "red",
  "magenta",
  "brown",
  "light-gray",
  "dark-gray",
  "light-blue",
  "light-green",
  "light-cyan",
  "light-red",
  "light-magenta",
  "yellow",
  "white"
};

static int
parse_color_name (grub_uint8_t *ret, char *name)
{
  grub_uint8_t i;
50
  for (i = 0; i < ARRAY_SIZE(color_list); i++)
51 52 53 54 55 56 57 58
    if (! grub_strcmp (name, color_list[i]))
      {
        *ret = i;
        return 0;
      }
  return -1;
}

59 60
int
grub_parse_color_name_pair (grub_uint8_t *color, const char *name)
61
{
62
  int result = 1;
63 64 65 66 67
  grub_uint8_t fg, bg;
  char *fg_name, *bg_name;

  /* nothing specified by user */
  if (name == NULL)
68
    return result;
69 70 71 72 73 74

  fg_name = grub_strdup (name);
  if (fg_name == NULL)
    {
      /* "out of memory" message was printed by grub_strdup() */
      grub_wait_after_message ();
75
      return result;
76 77 78 79 80
    }

  bg_name = grub_strchr (fg_name, '/');
  if (bg_name == NULL)
    {
81
      grub_printf_ (N_("Warning: syntax error (missing slash) in `%s'\n"), fg_name);
82 83 84 85 86 87 88 89
      grub_wait_after_message ();
      goto free_and_return;
    }

  *(bg_name++) = '\0';

  if (parse_color_name (&fg, fg_name) == -1)
    {
90
      grub_printf_ (N_("Warning: invalid foreground color `%s'\n"), fg_name);
91 92 93 94 95
      grub_wait_after_message ();
      goto free_and_return;
    }
  if (parse_color_name (&bg, bg_name) == -1)
    {
96
      grub_printf_ (N_("Warning: invalid background color `%s'\n"), bg_name);
97 98 99 100
      grub_wait_after_message ();
      goto free_and_return;
    }

101 102
  *color = (bg << 4) | fg;
  result = 0;
103 104 105

free_and_return:
  grub_free (fg_name);
106
  return result;
107 108
}

109 110 111 112 113
static void
set_colors (void)
{
  struct grub_term_output *term;

114 115 116 117 118
  FOR_ACTIVE_TERM_OUTPUTS(term)
  {
    /* Propagates `normal' color to terminal current color.  */
    grub_term_setcolorstate (term, GRUB_TERM_COLOR_NORMAL);
  }
119 120
}

121 122
/* Replace default `normal' colors with the ones specified by user (if any).  */
char *
123 124
grub_env_write_color_normal (struct grub_env_var *var __attribute__ ((unused)),
			     const char *val)
125
{
126
  if (grub_parse_color_name_pair (&grub_term_normal_color, val))
127
    return NULL;
128

129
  set_colors ();
130 131 132 133 134 135

  return grub_strdup (val);
}

/* Replace default `highlight' colors with the ones specified by user (if any).  */
char *
136 137
grub_env_write_color_highlight (struct grub_env_var *var __attribute__ ((unused)),
				const char *val)
138
{
139
  if (grub_parse_color_name_pair (&grub_term_highlight_color, val))
140
    return NULL;
141

142
  set_colors ();
143 144 145

  return grub_strdup (val);
}