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)
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
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
#include <grub/types.h>
#include <grub/err.h>
#include <grub/parser.h>
+#include <grub/command.h>
struct grub_script_mem;
char **args;
unsigned int argc;
+ unsigned int shift;
};
/* A single command line. */
/* 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;
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)
{
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
{
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
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));
*/
#include <grub/dl.h>
+#include <grub/i18n.h>
#include <grub/parser.h>
#include <grub/script_sh.h>
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)
--- /dev/null
+#! @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 <http://www.gnu.org/licenses/>.
+
+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
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)))
{