"setparams" command to update positional parameters.

* tests/grub_script_setparams.in: New test.
	* Makefile.util.def: Rules for new test.

	* grub-core/script/argv.c (grub_script_argv_make): New function.
	* grub-core/script/execute.c (replace_scope): New function.
	(grub_script_setparams): New function.
	* grub-core/script/lexer.c: Remove unused variables.
	* grub-core/script/main.c: Register/unregister setparams command.
	* include/grub/script_sh.h (grub_script_argv_make): New prototype.
	(grub_script_setparams): New prototype.
This commit is contained in:
BVK Chaitanya 2010-09-04 20:27:48 +05:30
commit 52e72f9d46
8 changed files with 165 additions and 7 deletions

View File

@ -1,3 +1,18 @@
2010-09-04 BVK Chaitanya <bvk.groups@gmail.com>
"setparams" command to update positional parameters.
* tests/grub_script_setparams.in: New test.
* Makefile.util.def: Rules for new test.
* grub-core/script/argv.c (grub_script_argv_make): New function.
* grub-core/script/execute.c (replace_scope): New function.
(grub_script_setparams): New function.
* grub-core/script/lexer.c: Remove unused variables.
* grub-core/script/main.c: Register/unregister setparams command.
* include/grub/script_sh.h (grub_script_argv_make): New prototype.
(grub_script_setparams): New prototype.
2010-09-04 BVK Chaitanya <bvk.groups@gmail.com>
* grub-core/normal/completion.c (grub_normal_do_completion): Fix

View File

@ -507,6 +507,12 @@ script = {
common = tests/grub_script_blockarg.in;
};
script = {
testcase;
name = grub_script_setparams;
common = tests/grub_script_setparams.in;
};
program = {
testcase;
name = example_unit_test;

View File

@ -59,6 +59,23 @@ grub_script_argv_free (struct grub_script_argv *argv)
argv->script = 0;
}
/* Make argv from argc, args pair. */
int
grub_script_argv_make (struct grub_script_argv *argv, int argc, char **args)
{
int i;
struct grub_script_argv r = { 0, 0, 0 };
for (i = 0; i < argc; i++)
if (grub_script_argv_next (&r) || grub_script_argv_append (&r, args[i]))
{
grub_script_argv_free (&r);
return 1;
}
*argv = r;
return 0;
}
/* Prepare for next argc. */
int
grub_script_argv_next (struct grub_script_argv *argv)

View File

@ -35,13 +35,35 @@ static unsigned long is_continue;
static unsigned long active_loops;
static unsigned long active_breaks;
#define GRUB_SCRIPT_SCOPE_MALLOCED 1
#define GRUB_SCRIPT_SCOPE_ARGS_MALLOCED 2
/* Scope for grub script functions. */
struct grub_script_scope
{
unsigned flags;
unsigned shifts;
struct grub_script_argv argv;
};
static struct grub_script_scope *scope = 0;
static void
replace_scope (struct grub_script_scope *new_scope)
{
if (scope)
{
scope->argv.argc += scope->shifts;
scope->argv.args -= scope->shifts;
if (scope->flags & GRUB_SCRIPT_SCOPE_ARGS_MALLOCED)
grub_script_argv_free (&scope->argv);
if (scope->flags & GRUB_SCRIPT_SCOPE_MALLOCED)
grub_free (scope);
}
scope = new_scope;
}
grub_err_t
grub_script_break (grub_command_t cmd, int argc, char *argv[])
{
@ -86,11 +108,41 @@ grub_script_shift (grub_command_t cmd __attribute__((unused)),
if (n > scope->argv.argc)
return GRUB_ERR_BAD_ARGUMENT;
scope->shifts += n;
scope->argv.argc -= n;
scope->argv.args += n;
return GRUB_ERR_NONE;
}
grub_err_t
grub_script_setparams (grub_command_t cmd __attribute__((unused)),
int argc, char **args)
{
struct grub_script_scope *new_scope;
struct grub_script_argv argv = { 0, 0, 0 };
if (! scope)
return GRUB_ERR_INVALID_COMMAND;
new_scope = grub_malloc (sizeof (*new_scope));
if (! new_scope)
return grub_errno;
if (grub_script_argv_make (&argv, argc, args))
{
grub_free (new_scope);
return grub_errno;
}
new_scope->shifts = 0;
new_scope->argv = argv;
new_scope->flags = GRUB_SCRIPT_SCOPE_MALLOCED |
GRUB_SCRIPT_SCOPE_ARGS_MALLOCED;
replace_scope (new_scope);
return GRUB_ERR_NONE;
}
static int
grub_env_special (const char *name)
{
@ -105,6 +157,7 @@ grub_env_special (const char *name)
static char **
grub_script_env_get (const char *name, grub_script_arg_type_t type)
{
unsigned i;
struct grub_script_argv result = { 0, 0, 0 };
if (grub_script_argv_next (&result))
@ -139,8 +192,6 @@ grub_script_env_get (const char *name, grub_script_arg_type_t type)
}
else if (grub_strcmp (name, "*") == 0)
{
unsigned i;
for (i = 0; i < scope->argv.argc; i++)
if (type == GRUB_SCRIPT_ARG_TYPE_VAR)
{
@ -161,8 +212,6 @@ grub_script_env_get (const char *name, grub_script_arg_type_t type)
}
else if (grub_strcmp (name, "@") == 0)
{
unsigned i;
for (i = 0; i < scope->argv.argc; i++)
{
if (i != 0 && grub_script_argv_next (&result))
@ -311,6 +360,8 @@ grub_script_function_call (grub_script_function_t func, int argc, char **args)
struct grub_script_scope new_scope;
active_loops = 0;
new_scope.flags = 0;
new_scope.shifts = 0;
new_scope.argv.argc = argc;
new_scope.argv.args = args;
@ -320,7 +371,7 @@ grub_script_function_call (grub_script_function_t func, int argc, char **args)
ret = grub_script_execute (func->func);
active_loops = loops;
scope = old_scope;
replace_scope (old_scope); /* free any scopes by setparams */
return ret;
}

View File

@ -205,8 +205,6 @@ struct grub_lexer_param *
grub_script_lexer_init (struct grub_parser_param *parser, char *script,
grub_reader_getline_t getline)
{
int len;
YY_BUFFER_STATE buffer;
struct grub_lexer_param *lexerstate;
lexerstate = grub_zalloc (sizeof (*lexerstate));

View File

@ -44,6 +44,7 @@ grub_normal_parse_line (char *line, grub_reader_getline_t getline)
static grub_command_t cmd_break;
static grub_command_t cmd_continue;
static grub_command_t cmd_shift;
static grub_command_t cmd_setparams;
void
grub_script_init (void)
@ -54,6 +55,9 @@ grub_script_init (void)
N_("[n]"), N_("Continue loops"));
cmd_shift = grub_register_command ("shift", grub_script_shift,
N_("[n]"), N_("Shift positional parameters."));
cmd_setparams = grub_register_command ("setparams", grub_script_setparams,
N_("[VALUE]..."),
N_("Set positional parameters."));
}
void
@ -70,4 +74,8 @@ grub_script_fini (void)
if (cmd_shift)
grub_unregister_command (cmd_shift);
cmd_shift = 0;
if (cmd_setparams)
grub_unregister_command (cmd_setparams);
cmd_setparams = 0;
}

View File

@ -248,6 +248,7 @@ void grub_script_fini (void);
void grub_script_mem_free (struct grub_script_mem *mem);
void grub_script_argv_free (struct grub_script_argv *argv);
int grub_script_argv_make (struct grub_script_argv *argv, int argc, char **args);
int grub_script_argv_next (struct grub_script_argv *argv);
int grub_script_argv_append (struct grub_script_argv *argv, const char *s);
int grub_script_argv_split_append (struct grub_script_argv *argv, char *s);
@ -342,6 +343,9 @@ grub_err_t grub_script_break (grub_command_t cmd, int argc, char *argv[]);
/* SHIFT command for GRUB script. */
grub_err_t grub_script_shift (grub_command_t cmd, int argc, char *argv[]);
/* SETPARAMS command for GRUB script functions. */
grub_err_t grub_script_setparams (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;

View File

@ -0,0 +1,59 @@
#! @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/>.
if test x$grubshell = xyes; then cmd=setparams; else cmd=set; fi
function f1 {
echo $#
echo "$#"
echo $@
echo "$@"
echo $*
echo "$*"
echo $1 $2
for v in "$@"; do echo $v; done
shift
echo $1 $2
for v in "$@"; do echo $v; done
$cmd 1 2 3 4
echo $#
echo "$#"
echo $@
echo "$@"
echo $*
echo "$*"
echo $1 $2
for v in "$@"; do echo $v; done
shift
echo $1 $2
for v in "$@"; do echo $v; done
}
# f1
# f1 a
f1 a b
f1 a b c
f1 a b c d
f1 a b c d e