From: Robert Millan <rmh@aybabtu.com>
To: The development of GRUB 2 <grub-devel@gnu.org>
Subject: Re: [PATCH] allow user-configurable menucolor
Date: Tue, 1 Jan 2008 15:45:30 +0100 [thread overview]
Message-ID: <20080101144530.GA11162@thorin> (raw)
In-Reply-To: <477A456B.2010209@nic.fi>
[-- Attachment #1: Type: text/plain, Size: 388 bytes --]
Thanks for the pointers. While fixing them I found other mistakes, then
started "torturing" the code with ill user input, found more mistakes, and
ended up refactoring most of it.
See attached new patch, this time including ChangeLog entry.
--
Robert Millan
<GPLv2> I know my rights; I want my phone call!
<DRM> What use is a phone call, if you are unable to speak?
(as seen on /.)
[-- Attachment #2: menucolor.diff --]
[-- Type: text/x-diff, Size: 5703 bytes --]
2008-01-01 Robert Millan <rmh@aybabtu.com>
* normal/menu.c (grub_color_menu_normal): New variable.
(grub_color_menu_highlight): Likewise.
(grub_menu_run): Split `press any key' prompt from here ...
(wait_after_message): ... to here (new function).
(color_list): New variable (copied from grub/stage2/builtins.c).
(parse_color_name): New function.
(parse_color_name_pair): New function.
(print_entry): Rename `normal_code' to `old_color_normal'.
Rename `highlight_code' to `old_color_highlight'.
(grub_menu_init_page): Use parse_color_name_pair() to update
`grub_color_menu_normal' and `grub_color_menu_highlight' from user
input, if applicable.
diff -urp grub2/normal/menu.c grub2.color/normal/menu.c
--- grub2/normal/menu.c 2007-12-25 12:10:46.000000000 +0100
+++ grub2.color/normal/menu.c 2008-01-01 15:34:15.000000000 +0100
@@ -1,6 +1,6 @@
/*
* GRUB -- GRand Unified Bootloader
- * Copyright (C) 2003,2004,2005,2006,2007 Free Software Foundation, Inc.
+ * Copyright (C) 2003,2004,2005,2006,2007,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
@@ -28,6 +28,99 @@
#define GRUB_COLOR_MENU_NORMAL 0x07
#define GRUB_COLOR_MENU_HIGHLIGHT 0x70
+static grub_uint8_t grub_color_menu_normal = GRUB_COLOR_MENU_NORMAL;
+static grub_uint8_t grub_color_menu_highlight = GRUB_COLOR_MENU_HIGHLIGHT;
+
+static void
+wait_after_message (void)
+{
+ /* Wait until the user pushes any key so that the user
+ can see what happened. */
+ grub_printf ("\nPress any key to continue...");
+ (void) grub_getkey ();
+}
+
+/* Borrowed from GRUB Legacy */
+static char *color_list[16] =
+{
+ "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;
+ for (i = 0; i < sizeof (color_list) / sizeof (*color_list); i++)
+ if (! grub_strcmp (name, color_list[i]))
+ {
+ *ret = i;
+ return 0;
+ }
+ return -1;
+}
+
+static void
+parse_color_name_pair (grub_uint8_t *ret, char *name)
+{
+ grub_uint8_t fg, bg;
+ char *fg_name, *bg_name;
+
+ /* nothing specified by user */
+ if (name == NULL)
+ return;
+
+ fg_name = grub_strdup (name);
+ if (fg_name == NULL)
+ {
+ /* "out of memory" message was printed by grub_strdup() */
+ wait_after_message ();
+ return;
+ }
+
+ bg_name = grub_strchr (fg_name, '/');
+ if (bg_name == NULL)
+ {
+ grub_printf ("Warning: syntax error (missing slash) in `%s'\n", fg_name);
+ wait_after_message ();
+ goto free_and_return;
+ }
+
+ *(bg_name++) = '\0';
+
+ if (parse_color_name (&fg, fg_name) == -1)
+ {
+ grub_printf ("Warning: invalid foreground color `%s'\n", fg_name);
+ wait_after_message ();
+ goto free_and_return;
+ }
+ if (parse_color_name (&bg, bg_name) == -1)
+ {
+ grub_printf ("Warning: invalid background color `%s'\n", bg_name);
+ wait_after_message ();
+ goto free_and_return;
+ }
+
+ *ret = (bg << 4) | fg;
+
+free_and_return:
+ grub_free (fg_name);
+}
+
static void
draw_border (void)
{
@@ -108,7 +201,7 @@ print_entry (int y, int highlight, grub_
grub_ssize_t len;
grub_uint32_t *unicode_title;
grub_ssize_t i;
- grub_uint8_t normal_code, highlight_code;
+ grub_uint8_t old_color_normal, old_color_highlight;
title = entry ? entry->title : "";
unicode_title = grub_malloc (grub_strlen (title) * sizeof (*unicode_title));
@@ -125,8 +218,8 @@ print_entry (int y, int highlight, grub_
return;
}
- grub_getcolor (&normal_code, &highlight_code);
- grub_setcolor (GRUB_COLOR_MENU_NORMAL, GRUB_COLOR_MENU_HIGHLIGHT);
+ grub_getcolor (&old_color_normal, &old_color_highlight);
+ grub_setcolor (grub_color_menu_normal, grub_color_menu_highlight);
grub_setcolorstate (highlight
? GRUB_TERM_COLOR_HIGHLIGHT
: GRUB_TERM_COLOR_NORMAL);
@@ -164,7 +257,7 @@ print_entry (int y, int highlight, grub_
grub_gotoxy (GRUB_TERM_CURSOR_X, y);
- grub_setcolor (normal_code, highlight_code);
+ grub_setcolor (old_color_normal, old_color_highlight);
grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
grub_free (unicode_title);
}
@@ -209,15 +302,19 @@ print_entries (grub_menu_t menu, int fir
void
grub_menu_init_page (int nested, int edit)
{
- grub_uint8_t normal_code, highlight_code;
- grub_getcolor (&normal_code, &highlight_code);
- grub_setcolor (GRUB_COLOR_MENU_NORMAL, GRUB_COLOR_MENU_HIGHLIGHT);
+ grub_uint8_t old_color_normal, old_color_highlight;
+
+ parse_color_name_pair (&grub_color_menu_normal, grub_env_get ("color_normal"));
+ parse_color_name_pair (&grub_color_menu_highlight, grub_env_get ("color_highlight"));
+
+ grub_getcolor (&old_color_normal, &old_color_highlight);
+ grub_setcolor (grub_color_menu_normal, grub_color_menu_highlight);
grub_normal_init_page ();
draw_border ();
print_message (nested, edit);
- grub_setcolor (normal_code, highlight_code);
+ grub_setcolor (old_color_normal, old_color_highlight);
}
/* Return the current timeout. If the variable "timeout" is not set or
@@ -501,10 +598,7 @@ grub_menu_run (grub_menu_t menu, int nes
grub_print_error ();
grub_errno = GRUB_ERR_NONE;
- /* Wait until the user pushes any key so that the user
- can see what happened. */
- grub_printf ("\nPress any key to continue...");
- (void) grub_getkey ();
+ wait_after_message ();
}
}
}
next prev parent reply other threads:[~2008-01-01 14:46 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-01-01 12:47 [PATCH] allow user-configurable menucolor Robert Millan
2008-01-01 13:14 ` Vesa Jääskeläinen
2008-01-01 13:40 ` Robert Millan
2008-01-01 13:51 ` Vesa Jääskeläinen
2008-01-01 14:45 ` Robert Millan [this message]
2008-01-01 17:38 ` Robert Millan
2008-01-02 21:48 ` Robert Millan
2008-01-02 23:55 ` Yoshinori K. Okuji
2008-01-03 0:56 ` Robert Millan
2008-01-02 23:42 ` Yoshinori K. Okuji
2008-01-03 1:04 ` Robert Millan
2008-01-03 15:35 ` Robert Millan
2008-01-03 16:04 ` Vesa Jääskeläinen
2008-01-03 16:38 ` Robert Millan
2008-01-23 8:56 ` Marco Gerards
2008-01-04 8:02 ` opening new context (was: [PATCH] allow user-configurable menucolor) Robert Millan
2008-01-05 1:34 ` Yoshinori K. Okuji
2008-01-05 11:49 ` Robert Millan
2008-01-05 12:03 ` Yoshinori K. Okuji
2008-01-05 12:09 ` Robert Millan
2008-01-05 21:45 ` [PATCH] allow user-configurable menucolor Jeroen Dekkers
2008-01-05 23:42 ` Robert Millan
2008-01-06 11:33 ` Jeroen Dekkers
2008-01-06 12:54 ` Robert Millan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20080101144530.GA11162@thorin \
--to=rmh@aybabtu.com \
--cc=grub-devel@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.