diff --git a/conf/common.rmk b/conf/common.rmk index 4b39e9b71..01f15dc59 100644 --- a/conf/common.rmk +++ b/conf/common.rmk @@ -106,7 +106,7 @@ util/grub-script-check.c_DEPENDENCIES = grub_script_check_init.h grub_script_check_SOURCES = gnulib/progname.c gnulib/getdelim.c gnulib/getline.c \ util/grub-script-check.c util/misc.c util/mm.c \ script/main.c script/script.c script/function.c script/lexer.c \ - kern/handler.c kern/err.c kern/parser.c kern/list.c \ + kern/handler.c kern/err.c kern/parser.c kern/list.c kern/command.c \ kern/misc.c kern/env.c grub_script_check_init.c grub_script.tab.c \ grub_script.yy.c grub_script_check_CFLAGS = $(GNULIB_UTIL_CFLAGS) diff --git a/conf/tests.rmk b/conf/tests.rmk index 9144e5528..9cda007db 100644 --- a/conf/tests.rmk +++ b/conf/tests.rmk @@ -74,6 +74,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_shift +grub_script_shift_SOURCES = tests/grub_script_shift.in + # List of tests to execute on "make check" # SCRIPTED_TESTS = example_scripted_test # SCRIPTED_TESTS += example_grub_script_test @@ -91,6 +94,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_shift # 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 730aa3005..8610348f6 100644 --- a/include/grub/script_sh.h +++ b/include/grub/script_sh.h @@ -23,6 +23,7 @@ #include #include #include +#include struct grub_script_mem; @@ -80,6 +81,7 @@ struct grub_script_scope char **args; unsigned int argc; + unsigned int shift; }; /* A single command line. */ @@ -308,6 +310,9 @@ grub_err_t grub_script_execute_menuentry (struct grub_script_cmd *cmd); /* Execute any GRUB pre-parsed command or script. */ grub_err_t grub_script_execute (struct grub_script *script); +/* SHIFT command for GRUB script. */ +grub_err_t grub_script_cmd_shift (grub_command_t cmd, int argc, char *argv[]); + /* This variable points to the parsed command. This is used to communicate with the bison code. */ extern struct grub_script_cmd *grub_script_parsed; diff --git a/script/execute.c b/script/execute.c index 571b6785b..d3f8e9b25 100644 --- a/script/execute.c +++ b/script/execute.c @@ -32,6 +32,31 @@ static struct grub_script_scope *scope = 0; +grub_err_t +grub_script_cmd_shift (grub_command_t cmd __attribute__((unused)), + int argc, char *argv[]) +{ + char *p = 0; + unsigned long n = 0; + + if (! scope) + return GRUB_ERR_NONE; + + if (argc == 0) + n = 1; + else if (argc > 1) + return GRUB_ERR_BAD_ARGUMENT; + else + { + n = grub_strtoul (argv[0], &p, 10); + if (*p != '\0') + return GRUB_ERR_BAD_ARGUMENT; + } + + scope->shift = (unsigned int)(n > scope->argc ? 0 : n); + return GRUB_ERR_NONE; +} + static char * grub_script_env_get (const char *name) { @@ -49,7 +74,10 @@ grub_script_env_get (const char *name) if (num == 0) return 0; /* XXX no file name, for now. */ - return (num > scope->argc ? 0 : scope->args[num - 1]); + if (num > scope->argc - scope->shift) + return 0; + else + return scope->args[num - 1 + scope->shift]; } else { @@ -60,7 +88,7 @@ grub_script_env_get (const char *name) else if (grub_strcmp (name, "#") == 0) { static char buf[32]; /* Rewritten everytime. */ - grub_snprintf (buf, sizeof (buf), "%u", scope->argc); + grub_snprintf (buf, sizeof (buf), "%u", scope->argc - scope->shift); return buf; } else @@ -244,6 +272,7 @@ grub_script_function_call (grub_script_function_t func, int argc, char **args) grub_err_t ret = 0; struct grub_script_scope new_scope; + new_scope.shift = 0; new_scope.argc = argc; new_scope.args = args; grub_list_push (GRUB_AS_LIST_P (&scope), GRUB_AS_LIST (&new_scope)); diff --git a/script/main.c b/script/main.c index b5159dc7d..ef810611c 100644 --- a/script/main.c +++ b/script/main.c @@ -17,6 +17,7 @@ */ #include +#include #include #include @@ -49,6 +50,8 @@ static struct grub_parser grub_sh_parser = GRUB_MOD_INIT(sh) { grub_parser_register ("grub", &grub_sh_parser); + grub_register_command ("shift", grub_script_cmd_shift, + N_("[n]"), N_("Shift positional parameters.")); } GRUB_MOD_FINI(sh) diff --git a/tests/grub_script_shift.in b/tests/grub_script_shift.in new file mode 100644 index 000000000..542e72974 --- /dev/null +++ b/tests/grub_script_shift.in @@ -0,0 +1,69 @@ +#! @builddir@/grub-shell-tester + +# 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 . + +function f1 { + echo f1 $# $1 $2 $3 + shift + echo f1 $# $1 $2 $3 +} + +function f2 { + echo f2 $# $1 $2 $3 + shift 1 + echo f2 $# $1 $2 $3 +} + +function f3 { + echo f3 $# $1 $2 $3 + shift 3 + echo f3 $# $1 $2 $3 +} + +function f4 { + echo f4 $# $1 $2 $3 + shift 100 + echo f4 $# $1 $2 $3 +} + +f1 +f1 a +f1 a b +f1 a b c +f1 a b c d +f1 a b c d e + +f2 +f2 a +f2 a b +f2 a b c +f2 a b c d +f2 a b c d e + +f3 +f3 a +f3 a b +f3 a b c +f3 a b c d +f3 a b c d e + +f4 +f4 a +f4 a b +f4 a b c +f4 a b c d +f4 a b c d e diff --git a/util/grub-script-check.c b/util/grub-script-check.c index 3b7ab295d..1d9ebd57f 100644 --- a/util/grub-script-check.c +++ b/util/grub-script-check.c @@ -57,6 +57,14 @@ grub_refresh (void) fflush (stdout); } +grub_err_t +grub_script_cmd_shift (grub_command_t cmd __attribute__((unused)), + int argc __attribute__((unused)), + char *argv[] __attribute__((unused))) +{ + return 0; +} + char * grub_script_execute_argument_to_string (struct grub_script_arg *arg __attribute__ ((unused))) {