diff --git a/Makefile.util.def b/Makefile.util.def index dd38774fd..e0900c73f 100644 --- a/Makefile.util.def +++ b/Makefile.util.def @@ -531,6 +531,12 @@ script = { common = tests/grub_script_expansion.in; }; +script = { + testcase; + name = grub_script_not; + common = tests/grub_script_not.in; +}; + program = { testcase; name = example_unit_test; diff --git a/grub-core/script/execute.c b/grub-core/script/execute.c index a7baee96c..b43ec85e1 100644 --- a/grub-core/script/execute.c +++ b/grub-core/script/execute.c @@ -547,13 +547,32 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd) grub_script_function_t func = 0; char errnobuf[18]; char *cmdname; + int argc; + char **args; + int invert; struct grub_script_argv argv = { 0, 0, 0 }; /* Lookup the command. */ if (grub_script_arglist_to_argv (cmdline->arglist, &argv) || ! argv.args[0]) return grub_errno; + invert = 0; + argc = argv.argc - 1; + args = argv.args + 1; cmdname = argv.args[0]; + if (grub_strcmp (cmdname, "!") == 0) + { + if (argv.argc < 2 || ! argv.args[1]) + { + grub_script_argv_free (&argv); + return grub_error (GRUB_ERR_BAD_ARGUMENT, "missing arguments"); + } + + invert = 1; + argc = argv.argc - 2; + args = argv.args + 2; + cmdname = argv.args[1]; + } grubcmd = grub_command_find (cmdname); if (! grubcmd) { @@ -594,13 +613,15 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd) { if ((grubcmd->flags & GRUB_COMMAND_FLAG_BLOCKS) && (grubcmd->flags & GRUB_COMMAND_FLAG_EXTCMD)) - ret = grub_extcmd_dispatcher (grubcmd, argv.argc - 1, argv.args + 1, - argv.script); + ret = grub_extcmd_dispatcher (grubcmd, argc, args, argv.script); else - ret = (grubcmd->func) (grubcmd, argv.argc - 1, argv.args + 1); + ret = (grubcmd->func) (grubcmd, argc, args); } else - ret = grub_script_function_call (func, argv.argc - 1, argv.args + 1); + ret = grub_script_function_call (func, argc, args); + + if (invert) + ret = ! ret; /* Free arguments. */ grub_script_argv_free (&argv); diff --git a/grub-core/script/yylex.l b/grub-core/script/yylex.l index d4cad8097..55620b6bd 100644 --- a/grub-core/script/yylex.l +++ b/grub-core/script/yylex.l @@ -155,7 +155,6 @@ MULTILINE {WORD}?((\"{DQCHR}*)|(\'{SQCHR}*)|(\\\n)) ">" { RECORD; return GRUB_PARSER_TOKEN_GT; } /* Reserved words */ -"!" { RECORD; return GRUB_PARSER_TOKEN_NOT; } "{" { RECORD; return GRUB_PARSER_TOKEN_LBR; } "}" { RECORD; return GRUB_PARSER_TOKEN_RBR; } "[[" { RECORD; return GRUB_PARSER_TOKEN_RSQBR2; } diff --git a/tests/grub_script_not.in b/tests/grub_script_not.in new file mode 100644 index 000000000..ff71039a6 --- /dev/null +++ b/tests/grub_script_not.in @@ -0,0 +1,62 @@ +#! @builddir@/grub-shell-tester +# +# 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 . + +true +echo $? + +! true +echo $? + +false +echo $? + +! false +echo $? + +# +# Negated forms (copied from grub_script_if.in) +# + +#basic if, execute +if ! true; then echo yes; fi + +#basic if, no execution +if ! false; then echo no; fi + +#if else, execute if path +if ! true; then echo yes; else echo no; fi + +#if else, execute else path +if ! false; then echo no; else echo yes; fi + +#if elif, execute elif +if ! false; then echo no; elif ! true; then echo yes; fi + +#if elif else, execute else +if ! false; then echo no; elif ! false; then echo no; else echo yes; fi + +#if elif(1) elif(2), execute elif(2) +if false; then echo no; elif ! false; then echo no; elif ! true; then echo yes; fi + +#if elif(1) elif(2) else, execute else +if false; then echo no; elif false; then echo no; elif ! false; then echo no; else echo yes; fi + +#if {if elif else}, execute elif +if true; then if false; then echo no; elif ! true; then echo yes; else echo no; fi; fi + +#if {if elif} else, execute elif. ofcourse no dangling-else problem due to "fi" +if true; then if ! false; then echo no; elif true; then echo yes; fi; else echo no; fi