]> git.ipfire.org Git - thirdparty/grub.git/commitdiff
Fix test -a and -o precedence.
authorVladimir 'phcoder' Serbinenko <phcoder@gmail.com>
Sat, 4 May 2013 11:38:59 +0000 (13:38 +0200)
committerVladimir 'phcoder' Serbinenko <phcoder@gmail.com>
Sat, 4 May 2013 11:38:59 +0000 (13:38 +0200)
Reported by: adrian15.

ChangeLog
Makefile.util.def
grub-core/commands/test.c
tests/grub_script_test.in [new file with mode: 0644]

index a721215c6a735c3f23d9336977657a1227b55aea..7a7d11c308edad3ca9f90241536f0e7ad08f9a8b 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+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.
index a357341eb48d411a693ab50a34759d9a494b795c..d0ae67f722927573f777750550abd1bf3c63aa2f 100644 (file)
@@ -581,6 +581,12 @@ script = {
   common = tests/example_grub_script_test.in;
 };
 
+script = {
+  testcase;
+  name = grub_script_test;
+  common = tests/grub_script_test.in;
+};
+
 script = {
   testcase;
   name = grub_script_echo1;
index e3347ee5625c51826e945fd78a9460da66d561e0..c98c13d8c24c1aa906b2c09ba38322fb63a15cc2 100644 (file)
@@ -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 (file)
index 0000000..34a5f14
--- /dev/null
@@ -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