shift command support

This commit is contained in:
BVK Chaitanya 2010-05-07 10:08:09 +05:30
parent 7897070571
commit e9efa0fe36
7 changed files with 121 additions and 3 deletions

View file

@ -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 \ grub_script_check_SOURCES = gnulib/progname.c gnulib/getdelim.c gnulib/getline.c \
util/grub-script-check.c util/misc.c util/mm.c \ util/grub-script-check.c util/misc.c util/mm.c \
script/main.c script/script.c script/function.c script/lexer.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 \ kern/misc.c kern/env.c grub_script_check_init.c grub_script.tab.c \
grub_script.yy.c grub_script.yy.c
grub_script_check_CFLAGS = $(GNULIB_UTIL_CFLAGS) grub_script_check_CFLAGS = $(GNULIB_UTIL_CFLAGS)

View file

@ -74,6 +74,9 @@ grub_script_comments_SOURCES = tests/grub_script_comments.in
check_SCRIPTS += grub_script_functions check_SCRIPTS += grub_script_functions
grub_script_functions_SOURCES = tests/grub_script_functions.in 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" # List of tests to execute on "make check"
# SCRIPTED_TESTS = example_scripted_test # SCRIPTED_TESTS = example_scripted_test
# SCRIPTED_TESTS += example_grub_script_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_dollar
SCRIPTED_TESTS += grub_script_comments SCRIPTED_TESTS += grub_script_comments
SCRIPTED_TESTS += grub_script_functions SCRIPTED_TESTS += grub_script_functions
SCRIPTED_TESTS += grub_script_shift
# dependencies between tests and testing-tools # dependencies between tests and testing-tools
$(SCRIPTED_TESTS): grub-shell grub-shell-tester $(SCRIPTED_TESTS): grub-shell grub-shell-tester

View file

@ -23,6 +23,7 @@
#include <grub/types.h> #include <grub/types.h>
#include <grub/err.h> #include <grub/err.h>
#include <grub/parser.h> #include <grub/parser.h>
#include <grub/command.h>
struct grub_script_mem; struct grub_script_mem;
@ -80,6 +81,7 @@ struct grub_script_scope
char **args; char **args;
unsigned int argc; unsigned int argc;
unsigned int shift;
}; };
/* A single command line. */ /* 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. */ /* Execute any GRUB pre-parsed command or script. */
grub_err_t grub_script_execute (struct grub_script *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 /* This variable points to the parsed command. This is used to
communicate with the bison code. */ communicate with the bison code. */
extern struct grub_script_cmd *grub_script_parsed; extern struct grub_script_cmd *grub_script_parsed;

View file

@ -32,6 +32,31 @@
static struct grub_script_scope *scope = 0; 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 * static char *
grub_script_env_get (const char *name) grub_script_env_get (const char *name)
{ {
@ -49,7 +74,10 @@ grub_script_env_get (const char *name)
if (num == 0) if (num == 0)
return 0; /* XXX no file name, for now. */ 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
{ {
@ -60,7 +88,7 @@ grub_script_env_get (const char *name)
else if (grub_strcmp (name, "#") == 0) else if (grub_strcmp (name, "#") == 0)
{ {
static char buf[32]; /* Rewritten everytime. */ 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; return buf;
} }
else else
@ -244,6 +272,7 @@ grub_script_function_call (grub_script_function_t func, int argc, char **args)
grub_err_t ret = 0; grub_err_t ret = 0;
struct grub_script_scope new_scope; struct grub_script_scope new_scope;
new_scope.shift = 0;
new_scope.argc = argc; new_scope.argc = argc;
new_scope.args = args; new_scope.args = args;
grub_list_push (GRUB_AS_LIST_P (&scope), GRUB_AS_LIST (&new_scope)); grub_list_push (GRUB_AS_LIST_P (&scope), GRUB_AS_LIST (&new_scope));

View file

@ -17,6 +17,7 @@
*/ */
#include <grub/dl.h> #include <grub/dl.h>
#include <grub/i18n.h>
#include <grub/parser.h> #include <grub/parser.h>
#include <grub/script_sh.h> #include <grub/script_sh.h>
@ -49,6 +50,8 @@ static struct grub_parser grub_sh_parser =
GRUB_MOD_INIT(sh) GRUB_MOD_INIT(sh)
{ {
grub_parser_register ("grub", &grub_sh_parser); 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) GRUB_MOD_FINI(sh)

View file

@ -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 <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

View file

@ -57,6 +57,14 @@ grub_refresh (void)
fflush (stdout); 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 * char *
grub_script_execute_argument_to_string (struct grub_script_arg *arg __attribute__ ((unused))) grub_script_execute_argument_to_string (struct grub_script_arg *arg __attribute__ ((unused)))
{ {