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