diff --git a/conf/tests.rmk b/conf/tests.rmk index 9144e5528..a7fdf4033 100644 --- a/conf/tests.rmk +++ b/conf/tests.rmk @@ -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 diff --git a/include/grub/script_sh.h b/include/grub/script_sh.h index ffedf5c75..6a959e16f 100644 --- a/include/grub/script_sh.h +++ b/include/grub/script_sh.h @@ -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; }; @@ -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; diff --git a/script/execute.c b/script/execute.c index b9538c29b..932be6635 100644 --- a/script/execute.c +++ b/script/execute.c @@ -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: diff --git a/script/main.c b/script/main.c index 19ce88c43..620d9deac 100644 --- a/script/main.c +++ b/script/main.c @@ -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; diff --git a/script/parser.y b/script/parser.y index ce97ab174..3faaf4736 100644 --- a/script/parser.y +++ b/script/parser.y @@ -208,9 +208,9 @@ block: "{" /* restore old scripts; append $$->script to siblings. */ state->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); } ; diff --git a/script/script.c b/script/script.c index 8d856c493..25a34be0d 100644 --- a/script/script.c +++ b/script/script.c @@ -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 index 000000000..783cee8e0 --- /dev/null +++ b/tests/grub_script_blockarg.in @@ -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 . + +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 index 000000000..bb6f3c3f0 --- /dev/null +++ b/tests/test_blockarg.c @@ -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 . + */ + +#include +#include +#include +#include +#include +#include + +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); +}