]> git.ipfire.org Git - thirdparty/grub.git/commitdiff
review fixes and a testcase
authorBVK Chaitanya <bvk@dbook>
Mon, 9 Aug 2010 16:12:24 +0000 (21:42 +0530)
committerBVK Chaitanya <bvk@dbook>
Mon, 9 Aug 2010 16:12:24 +0000 (21:42 +0530)
conf/tests.rmk
include/grub/script_sh.h
script/execute.c
script/main.c
script/parser.y
script/script.c
tests/grub_script_blockarg.in [new file with mode: 0644]
tests/test_blockarg.c [new file with mode: 0644]

index 9144e552845b3a517dc7ff959b226bbfcc722e58..a7fdf403307ddf563e9c7674ab5378e1b312a48a 100644 (file)
@@ -19,6 +19,11 @@ functional_test_mod_SOURCES = tests/lib/functional_test.c tests/lib/test.c
 functional_test_mod_CFLAGS  = $(COMMON_CFLAGS)
 functional_test_mod_LDFLAGS = $(COMMON_LDFLAGS)
 
+pkglib_MODULES += test_blockarg.mod
+test_blockarg_mod_SOURCES = tests/test_blockarg.c
+test_blockarg_mod_CFLAGS = $(COMMON_CFLAGS)
+test_blockarg_mod_LDFLAGS = $(COMMON_LDFLAGS)
+
 # Rules for unit tests
 check_UTILITIES += example_unit_test
 example_unit_test_SOURCES = tests/example_unit_test.c kern/list.c kern/misc.c tests/lib/test.c tests/lib/unit_test.c
@@ -74,6 +79,9 @@ grub_script_comments_SOURCES = tests/grub_script_comments.in
 check_SCRIPTS += grub_script_functions
 grub_script_functions_SOURCES = tests/grub_script_functions.in
 
+check_SCRIPTS += grub_script_blockarg
+grub_script_blockarg_SOURCES = tests/grub_script_blockarg.in
+
 # List of tests to execute on "make check"
 # SCRIPTED_TESTS    = example_scripted_test
 # SCRIPTED_TESTS   += example_grub_script_test
@@ -91,6 +99,7 @@ SCRIPTED_TESTS += grub_script_final_semicolon
 SCRIPTED_TESTS += grub_script_dollar
 SCRIPTED_TESTS += grub_script_comments
 SCRIPTED_TESTS += grub_script_functions
+SCRIPTED_TESTS += grub_script_blockarg
 
 # dependencies between tests and testing-tools
 $(SCRIPTED_TESTS): grub-shell grub-shell-tester
index ffedf5c75cce2855a0bcd0eb0149d4f79a5fc642..6a959e16f8aeb0ae2cc759725bdca648b83105fd 100644 (file)
@@ -43,8 +43,8 @@ struct grub_script
   struct grub_script_mem *mem;
   struct grub_script_cmd *cmd;
 
-  /* Other grub_script's from block arguments.  */
-  struct grub_script *siblings;
+  /* grub_scripts from block arguments.  */
+  struct grub_script *next_siblings;
   struct grub_script *children;
 };
 \f
@@ -371,7 +371,7 @@ grub_err_t
 grub_normal_parse_line (char *line, grub_reader_getline_t getline);
 
 static inline struct grub_script *
-grub_script_get (struct grub_script *script)
+grub_script_ref (struct grub_script *script)
 {
   if (script)
     script->refcnt++;
@@ -379,7 +379,7 @@ grub_script_get (struct grub_script *script)
 }
 
 static inline void
-grub_script_put (struct grub_script *script)
+grub_script_unref (struct grub_script *script)
 {
   if (! script)
     return;
index b9538c29ba47242ce8727a5cd983ac7c5f3e00a1..932be6635a2e352db9a014c7bafe19ae2ca7fe29 100644 (file)
@@ -201,7 +201,7 @@ grub_script_arglist_to_argv (struct grub_script_arglist *arglist,
                  grub_script_argv_append (&result, arg->str) ||
                  grub_script_argv_append (&result, "}"))
                goto fail;
-             result.script = grub_script_get (arg->script);
+             result.script = arg->script;
              break;
 
            case GRUB_SCRIPT_ARG_TYPE_TEXT:
index 19ce88c432143bffd3dbd9dccb4d3bf8c4816ba3..620d9deac2003842073f9646e0476acf1f6b963b 100644 (file)
@@ -34,7 +34,7 @@ grub_normal_parse_line (char *line, grub_reader_getline_t getline)
       grub_script_execute (parsed_script);
 
       /* The parsed script was executed, throw it away.  */
-      grub_script_put (parsed_script);
+      grub_script_unref (parsed_script);
     }
 
   return grub_errno;
index ce97ab17439fc56f7abfe08265984fcafa357328..3faaf47366a51265c9b1d3158ec6a2fad4288265 100644 (file)
@@ -208,9 +208,9 @@ block: "{"
           /* restore old scripts; append $$->script to siblings. */
           state->scripts = $<scripts>2 ?: $$->script;
           if (s) {
-            while (s->siblings)
-              s = s->siblings;
-            s->siblings = $$->script;
+            while (s->next_siblings)
+              s = s->next_siblings;
+            s->next_siblings = $$->script;
           }
         }
 
@@ -243,11 +243,12 @@ grubcmd: word arguments0 block0
           if ($3)
             x = grub_script_add_arglist (state, $2, $3);
 
-           if ($1 && x) {
-             $1->next = x;
-             $1->argcount += x->argcount;
-             x->argcount = 0;
-           }
+           if ($1 && x)
+            {
+              $1->next = x;
+              $1->argcount += x->argcount;
+              x->argcount = 0;
+            }
            $$ = grub_script_create_cmdline (state, $1);
          }
 ;
index 8d856c493e20f2f8e9ada05a905be7cf2e31f7df..25a34be0da01220c7d298e5f520607445ebc41c6 100644 (file)
@@ -105,8 +105,8 @@ grub_script_free (struct grub_script *script)
 
   s = script->children;
   while (s) {
-    t = s->siblings;
-    grub_script_put (s);
+    t = s->next_siblings;
+    grub_script_unref (s);
     s = t;
   }
   grub_free (script);
@@ -355,8 +355,8 @@ grub_script_create (struct grub_script_cmd *cmd, struct grub_script_mem *mem)
   parsed->mem = mem;
   parsed->cmd = cmd;
   parsed->refcnt = 0;
-  parsed->siblings = 0;
   parsed->children = 0;
+  parsed->next_siblings = 0;
 
   return parsed;
 }
diff --git a/tests/grub_script_blockarg.in b/tests/grub_script_blockarg.in
new file mode 100644 (file)
index 0000000..783cee8
--- /dev/null
@@ -0,0 +1,41 @@
+#! /bin/bash
+
+# Run GRUB script in a Qemu instance
+# Copyright (C) 2010  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
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# GRUB is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+
+error_if_not () {
+    if test "$1" != "$2"; then
+       echo "[$1]" != "[$2]"
+       exit 1
+    fi
+}
+
+cmd='test_blockarg { true }'
+v=`echo "$cmd" | @builddir@/grub-shell`
+error_if_not "$v" '{ true }'
+
+tmp=`mktemp`
+cmd='test_blockarg { test_blockarg { true } }'
+echo "$cmd" | @builddir@/grub-shell >$tmp
+error_if_not "`head -n1 $tmp|tail -n1`" '{ test_blockarg { true } }'
+error_if_not "`head -n2 $tmp|tail -n1`" '{ true }'
+
+cmd='test_blockarg { test_blockarg { test_blockarg { true } }; test_blockarg { true } }'
+echo "$cmd" | @builddir@/grub-shell >$tmp
+error_if_not "`head -n1 $tmp|tail -n1`" '{ test_blockarg { test_blockarg { true } }; test_blockarg { true } }'
+error_if_not "`head -n2 $tmp|tail -n1`" '{ test_blockarg { true } }'
+error_if_not "`head -n3 $tmp|tail -n1`" '{ true }'
+error_if_not "`head -n4 $tmp|tail -n1`" '{ true }'
diff --git a/tests/test_blockarg.c b/tests/test_blockarg.c
new file mode 100644 (file)
index 0000000..bb6f3c3
--- /dev/null
@@ -0,0 +1,51 @@
+/* test_blockarg.c - print and execute block argument  */
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2010  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
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/dl.h>
+#include <grub/err.h>
+#include <grub/misc.h>
+#include <grub/i18n.h>
+#include <grub/extcmd.h>
+#include <grub/script_sh.h>
+
+static grub_err_t
+test_blockarg (grub_extcmd_context_t ctxt, int argc, char **args)
+{
+  if (! ctxt->script)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "no block parameter");
+
+  grub_printf ("%s\n", args[argc - 1]);
+  grub_script_execute (ctxt->script);
+  return GRUB_ERR_NONE;
+}
+
+static grub_extcmd_t cmd;
+
+GRUB_MOD_INIT(test_blockarg)
+{
+  cmd = grub_register_extcmd ("test_blockarg", test_blockarg,
+                             GRUB_COMMAND_FLAG_BOTH | GRUB_COMMAND_FLAG_BLOCKS,
+                             N_("BLOCK"),
+                             N_("Print and execute block argument."), 0);
+}
+
+GRUB_MOD_FINI(test_blockarg)
+{
+  grub_unregister_extcmd (cmd);
+}