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 /.)
2008-01-01  Robert Millan  <[EMAIL PROTECTED]>

	* 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 ();
 	}
     }
 }
_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/grub-devel

Reply via email to