Blob Blame History Raw
From 568a5bef7e6b7309a652b328d65a9964b6d415e5 Mon Sep 17 00:00:00 2001
From: Vladimir 'phcoder' Serbinenko <phcoder@gmail.com>
Date: Sat, 4 May 2013 13:38:59 +0200
Subject: [PATCH 415/482] 	Fix test -a and -o precedence. 	Reported by:
 adrian15.

---
 ChangeLog                 |  5 +++++
 Makefile.util.def         |  6 ++++++
 grub-core/commands/test.c | 30 ++++++++++++++----------------
 tests/grub_script_test.in | 15 +++++++++++++++
 4 files changed, 40 insertions(+), 16 deletions(-)
 create mode 100644 tests/grub_script_test.in

diff --git a/ChangeLog b/ChangeLog
index a721215..7a7d11c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2013-05-04  Vladimir Serbinenko  <phcoder@gmail.com>
 
+	Fix test -a and -o precedence.
+	Reported by: adrian15.
+
+2013-05-04  Vladimir Serbinenko  <phcoder@gmail.com>
+
 	* grub-core/font/font.c (grub_font_construct_glyph): Fix memory leak.
 
 2013-05-03  Andrey Borzenkov <arvidjaar@gmail.com>
diff --git a/Makefile.util.def b/Makefile.util.def
index a357341..d0ae67f 100644
--- a/Makefile.util.def
+++ b/Makefile.util.def
@@ -583,6 +583,12 @@ script = {
 
 script = {
   testcase;
+  name = grub_script_test;
+  common = tests/grub_script_test.in;
+};
+
+script = {
+  testcase;
   name = grub_script_echo1;
   common = tests/grub_script_echo1.in;
 };
diff --git a/grub-core/commands/test.c b/grub-core/commands/test.c
index e3347ee..c98c13d 100644
--- a/grub-core/commands/test.c
+++ b/grub-core/commands/test.c
@@ -41,7 +41,8 @@ grub_strtosl (char *arg, char **end, int base)
 /* Context for test_parse.  */
 struct test_parse_ctx
 {
-  int ret, discard, invert;
+  int invert;
+  int or, and;
   int file_exists;
   struct grub_dirhook_info file_info;
   char *filename;
@@ -51,9 +52,8 @@ struct test_parse_ctx
 static void
 update_val (int val, struct test_parse_ctx *ctx)
 {
-  if (! ctx->discard)
-    ctx->ret = ctx->invert ? ! val : val;
-  ctx->invert = ctx->discard = 0;
+  ctx->and = ctx->and && (ctx->invert ? ! val : val);
+  ctx->invert = 0;
 }
 
 /* A hook for iterating directories. */
@@ -153,8 +153,8 @@ static int
 test_parse (char **args, int *argn, int argc)
 {
   struct test_parse_ctx ctx = {
-    .ret = 0,
-    .discard = 0,
+    .and = 1,
+    .or = 0,
     .invert = 0
   };
 
@@ -332,7 +332,7 @@ test_parse (char **args, int *argn, int argc)
 	      get_fileinfo (args[*argn + 1], &ctx);
 	      update_val (ctx.file_exists && ctx.file_info.dir, &ctx);
 	      (*argn) += 2;
-	      return ctx.ret;
+	      return ctx.or || ctx.and;
 	    }
 
 	  if (grub_strcmp (args[*argn], "-e") == 0)
@@ -340,7 +340,7 @@ test_parse (char **args, int *argn, int argc)
 	      get_fileinfo (args[*argn + 1], &ctx);
 	      update_val (ctx.file_exists, &ctx);
 	      (*argn) += 2;
-	      return ctx.ret;
+	      return ctx.or || ctx.and;
 	    }
 
 	  if (grub_strcmp (args[*argn], "-f") == 0)
@@ -349,7 +349,7 @@ test_parse (char **args, int *argn, int argc)
 	      /* FIXME: check for other types. */
 	      update_val (ctx.file_exists && ! ctx.file_info.dir, &ctx);
 	      (*argn) += 2;
-	      return ctx.ret;
+	      return ctx.or || ctx.and;
 	    }
 
 	  if (grub_strcmp (args[*argn], "-s") == 0)
@@ -362,7 +362,7 @@ test_parse (char **args, int *argn, int argc)
 		grub_file_close (file);
 	      grub_errno = GRUB_ERR_NONE;
 	      (*argn) += 2;
-	      return ctx.ret;
+	      return ctx.or || ctx.and;
 	    }
 
 	  /* String tests. */
@@ -387,7 +387,7 @@ test_parse (char **args, int *argn, int argc)
       if (grub_strcmp (args[*argn], ")") == 0)
 	{
 	  (*argn)++;
-	  return ctx.ret;
+	  return ctx.or || ctx.and;
 	}
       /* Recursively invoke if parenthesis. */
       if (grub_strcmp (args[*argn], "(") == 0)
@@ -405,15 +405,13 @@ test_parse (char **args, int *argn, int argc)
 	}
       if (grub_strcmp (args[*argn], "-a") == 0)
 	{
-	  /* If current value is 0 second value is to be discarded. */
-	  ctx.discard = ! ctx.ret;
 	  (*argn)++;
 	  continue;
 	}
       if (grub_strcmp (args[*argn], "-o") == 0)
 	{
-	  /* If current value is 1 second value is to be discarded. */
-	  ctx.discard = ctx.ret;
+	  ctx.or = ctx.or || ctx.and;
+	  ctx.and = 1;
 	  (*argn)++;
 	  continue;
 	}
@@ -422,7 +420,7 @@ test_parse (char **args, int *argn, int argc)
       update_val (args[*argn][0], &ctx);
       (*argn)++;
     }
-  return ctx.ret;
+  return ctx.or || ctx.and;
 }
 
 static grub_err_t
diff --git a/tests/grub_script_test.in b/tests/grub_script_test.in
new file mode 100644
index 0000000..34a5f14
--- /dev/null
+++ b/tests/grub_script_test.in
@@ -0,0 +1,15 @@
+#! @builddir@/grub-shell-tester
+
+for device in 'hd0' 'fd0'; do
+    # But search them if their search has been inforced
+    set fd0search="no"
+    if [ "$device" != "fd0" -a "$device" != "cd" \
+    -o \
+    "$device" = "fd0" -a "$fd0search" = "yes" ]\
+    ; then
+      echo "Yes"
+    else
+      echo "No"
+    fi
+
+done
\ No newline at end of file
-- 
1.8.2.1