From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with archive (Exim 4.43) id 1J9iNe-0000pP-EH for mharc-grub-devel@gnu.org; Tue, 01 Jan 2008 09:46:10 -0500 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1J9iNc-0000p9-W0 for grub-devel@gnu.org; Tue, 01 Jan 2008 09:46:09 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1J9iNb-0000ow-0S for grub-devel@gnu.org; Tue, 01 Jan 2008 09:46:08 -0500 Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1J9iNa-0000ot-QP for grub-devel@gnu.org; Tue, 01 Jan 2008 09:46:06 -0500 Received: from aybabtu.com ([69.60.117.155]) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1J9iNa-0000Q4-ER for grub-devel@gnu.org; Tue, 01 Jan 2008 09:46:06 -0500 Received: from [192.168.10.6] (helo=thorin) by aybabtu.com with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.63) (envelope-from ) id 1J9iNQ-0002fG-DU for grub-devel@gnu.org; Tue, 01 Jan 2008 15:45:59 +0100 Received: from rmh by thorin with local (Exim 4.63) (envelope-from ) id 1J9iN0-0002v0-UM for grub-devel@gnu.org; Tue, 01 Jan 2008 15:45:30 +0100 Date: Tue, 1 Jan 2008 15:45:30 +0100 From: Robert Millan To: The development of GRUB 2 Message-ID: <20080101144530.GA11162@thorin> References: <20080101124728.GA30417@thorin> <477A3CCA.4090908@nic.fi> <20080101134026.GA32057@thorin> <477A456B.2010209@nic.fi> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="k+w/mQv8wyuph6w0" Content-Disposition: inline In-Reply-To: <477A456B.2010209@nic.fi> Organization: free as in freedom X-Message-Flag: Worried about Outlook viruses? Switch to Thunderbird! www.mozilla.com/thunderbird X-Debbugs-No-Ack: true User-Agent: Mutt/1.5.13 (2006-08-11) X-detected-kernel: by monty-python.gnu.org: Genre and OS details not recognized. Subject: Re: [PATCH] allow user-configurable menucolor X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: The development of GRUB 2 List-Id: The development of GRUB 2 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 01 Jan 2008 14:46:09 -0000 --k+w/mQv8wyuph6w0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 I know my rights; I want my phone call! What use is a phone call, if you are unable to speak? (as seen on /.) --k+w/mQv8wyuph6w0 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="menucolor.diff" 2008-01-01 Robert Millan * 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 (); } } } --k+w/mQv8wyuph6w0--