c78952
From 27845fb323de35e9dcaad8c738eb1a2a2bc316f8 Mon Sep 17 00:00:00 2001
f4c76c
From: Peter Jones <pjones@redhat.com>
f4c76c
Date: Fri, 5 Sep 2014 10:07:04 -0400
31cddd
Subject: [PATCH] Allow "fallback" to include entries by title, not
bc092b
 just number.
f4c76c
f4c76c
Resolves: rhbz#1026084
f4c76c
f4c76c
Signed-off-by: Peter Jones <pjones@redhat.com>
f4c76c
---
0ac23e
 grub-core/normal/menu.c | 85 +++++++++++++++++++++++++++++++++----------------
0ac23e
 1 file changed, 58 insertions(+), 27 deletions(-)
f4c76c
f4c76c
diff --git a/grub-core/normal/menu.c b/grub-core/normal/menu.c
ec4acb
index e7a83c2d6e2..d2f64b05e0a 100644
f4c76c
--- a/grub-core/normal/menu.c
f4c76c
+++ b/grub-core/normal/menu.c
0ac23e
@@ -163,16 +163,41 @@ grub_menu_set_timeout (int timeout)
f4c76c
     }
f4c76c
 }
f4c76c
 
f4c76c
+static int
f4c76c
+menuentry_eq (const char *id, const char *spec)
f4c76c
+{
f4c76c
+  const char *ptr1, *ptr2;
f4c76c
+  ptr1 = id;
f4c76c
+  ptr2 = spec;
f4c76c
+  while (1)
f4c76c
+    {
f4c76c
+      if (*ptr2 == '>' && ptr2[1] != '>' && *ptr1 == 0)
0ac23e
+	return ptr2 - spec;
f4c76c
+      if (*ptr2 == '>' && ptr2[1] != '>')
f4c76c
+	return 0;
f4c76c
+      if (*ptr2 == '>')
f4c76c
+	ptr2++;
f4c76c
+      if (*ptr1 != *ptr2)
f4c76c
+	return 0;
f4c76c
+      if (*ptr1 == 0)
0ac23e
+	return ptr1 - id;
f4c76c
+      ptr1++;
f4c76c
+      ptr2++;
f4c76c
+    }
0ac23e
+  return 0;
f4c76c
+}
f4c76c
+
f4c76c
 /* Get the first entry number from the value of the environment variable NAME,
f4c76c
    which is a space-separated list of non-negative integers.  The entry number
f4c76c
    which is returned is stripped from the value of NAME.  If no entry number
f4c76c
    can be found, -1 is returned.  */
f4c76c
 static int
f4c76c
-get_and_remove_first_entry_number (const char *name)
f4c76c
+get_and_remove_first_entry_number (grub_menu_t menu, const char *name)
f4c76c
 {
f4c76c
   const char *val;
f4c76c
   char *tail;
0ac23e
   int entry;
0ac23e
+  int sz = 0;
0ac23e
 
0ac23e
   val = grub_env_get (name);
0ac23e
   if (! val)
0ac23e
@@ -182,9 +207,39 @@ get_and_remove_first_entry_number (const char *name)
f4c76c
 
f4c76c
   entry = (int) grub_strtoul (val, &tail, 0);
f4c76c
 
f4c76c
+  if (grub_errno == GRUB_ERR_BAD_NUMBER)
f4c76c
+    {
f4c76c
+      /* See if the variable matches the title of a menu entry.  */
f4c76c
+      grub_menu_entry_t e = menu->entry_list;
f4c76c
+      int i;
f4c76c
+
f4c76c
+      for (i = 0; e; i++)
f4c76c
+	{
0ac23e
+	  sz = menuentry_eq (e->title, val);
0ac23e
+	  if (sz < 1)
0ac23e
+	    sz = menuentry_eq (e->id, val);
0ac23e
+
0ac23e
+	  if (sz >= 1)
f4c76c
+	    {
f4c76c
+	      entry = i;
f4c76c
+	      break;
f4c76c
+	    }
f4c76c
+	  e = e->next;
f4c76c
+	}
f4c76c
+
0ac23e
+      if (sz > 0)
0ac23e
+	grub_errno = GRUB_ERR_NONE;
0ac23e
+
f4c76c
+      if (! e)
f4c76c
+	entry = -1;
f4c76c
+    }
f4c76c
+
f4c76c
   if (grub_errno == GRUB_ERR_NONE)
f4c76c
     {
f4c76c
-      /* Skip whitespace to find the next digit.  */
0ac23e
+      if (sz > 0)
0ac23e
+	tail += sz;
0ac23e
+
f4c76c
+      /* Skip whitespace to find the next entry.  */
f4c76c
       while (*tail && grub_isspace (*tail))
f4c76c
 	tail++;
f4c76c
       grub_env_set (name, tail);
0ac23e
@@ -347,7 +402,7 @@ grub_menu_execute_with_fallback (grub_menu_t menu,
f4c76c
   grub_menu_execute_entry (entry, 1);
f4c76c
 
f4c76c
   /* Deal with fallback entries.  */
f4c76c
-  while ((fallback_entry = get_and_remove_first_entry_number ("fallback"))
f4c76c
+  while ((fallback_entry = get_and_remove_first_entry_number (menu, "fallback"))
f4c76c
 	 >= 0)
f4c76c
     {
f4c76c
       grub_print_error ();
0ac23e
@@ -465,30 +520,6 @@ grub_menu_register_viewer (struct grub_menu_viewer *viewer)
f4c76c
   viewers = viewer;
f4c76c
 }
f4c76c
 
f4c76c
-static int
f4c76c
-menuentry_eq (const char *id, const char *spec)
f4c76c
-{
f4c76c
-  const char *ptr1, *ptr2;
f4c76c
-  ptr1 = id;
f4c76c
-  ptr2 = spec;
f4c76c
-  while (1)
f4c76c
-    {
f4c76c
-      if (*ptr2 == '>' && ptr2[1] != '>' && *ptr1 == 0)
f4c76c
-	return 1;
f4c76c
-      if (*ptr2 == '>' && ptr2[1] != '>')
f4c76c
-	return 0;
f4c76c
-      if (*ptr2 == '>')
f4c76c
-	ptr2++;
f4c76c
-      if (*ptr1 != *ptr2)
f4c76c
-	return 0;
f4c76c
-      if (*ptr1 == 0)
f4c76c
-	return 1;
f4c76c
-      ptr1++;
f4c76c
-      ptr2++;
f4c76c
-    }
f4c76c
-}
f4c76c
-
f4c76c
-
f4c76c
 /* Get the entry number from the variable NAME.  */
f4c76c
 static int
f4c76c
 get_entry_number (grub_menu_t menu, const char *name)