"continue" command support to GRUB script.

* script/execute.c (grub_script_execute_cmdwhile): Continue support.
	(grub_script_break): Continue support.
	* script/main.c (grub_script_init): Register continue command.
	(grub_script_fini): Unregister continue command.

	* tests/grub_script_continue.in: New testcase.
	* conf/tests.rmk: Rules for new testcase.
This commit is contained in:
BVK Chaitanya 2010-08-12 11:48:21 +05:30
commit 4d61999e33
5 changed files with 124 additions and 6 deletions

View file

@ -1,3 +1,15 @@
2010-08-12 BVK Chaitanya <bvk.groups@gmail.com>
"continue" command support to GRUB script.
* script/execute.c (grub_script_execute_cmdwhile): Continue support.
(grub_script_break): Continue support.
* script/main.c (grub_script_init): Register continue command.
(grub_script_fini): Unregister continue command.
* tests/grub_script_continue.in: New testcase.
* conf/tests.rmk: Rules for new testcase.
2010-08-12 BVK Chaitanya <bvk@dbook> 2010-08-12 BVK Chaitanya <bvk@dbook>
"break" command support to GRUB script. "break" command support to GRUB script.

View file

@ -77,6 +77,9 @@ grub_script_functions_SOURCES = tests/grub_script_functions.in
check_SCRIPTS += grub_script_break check_SCRIPTS += grub_script_break
grub_script_break_SOURCES = tests/grub_script_break.in grub_script_break_SOURCES = tests/grub_script_break.in
check_SCRIPTS += grub_script_continue
grub_script_continue_SOURCES = tests/grub_script_continue.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
@ -95,6 +98,7 @@ 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_break SCRIPTED_TESTS += grub_script_break
SCRIPTED_TESTS += grub_script_continue
# 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

@ -30,6 +30,7 @@
is sizeof (int) * 3, and one extra for a possible -ve sign. */ is sizeof (int) * 3, and one extra for a possible -ve sign. */
#define ERRNO_DIGITS_MAX (sizeof (int) * 3 + 1) #define ERRNO_DIGITS_MAX (sizeof (int) * 3 + 1)
static unsigned long is_continue;
static unsigned long active_loops; static unsigned long active_loops;
static unsigned long active_breaks; static unsigned long active_breaks;
@ -41,8 +42,7 @@ struct grub_script_scope
static struct grub_script_scope *scope = 0; static struct grub_script_scope *scope = 0;
grub_err_t grub_err_t
grub_script_break (grub_command_t cmd __attribute__((unused)), grub_script_break (grub_command_t cmd, int argc, char *argv[])
int argc, char *argv[])
{ {
char *p = 0; char *p = 0;
unsigned long count; unsigned long count;
@ -54,6 +54,7 @@ grub_script_break (grub_command_t cmd __attribute__((unused)),
(*p != '\0')) (*p != '\0'))
return grub_error (GRUB_ERR_BAD_ARGUMENT, "bad break"); return grub_error (GRUB_ERR_BAD_ARGUMENT, "bad break");
is_continue = grub_strcmp (cmd->name, "break") ? 1 : 0;
active_breaks = grub_min (active_loops, count); active_breaks = grub_min (active_loops, count);
return GRUB_ERR_NONE; return GRUB_ERR_NONE;
} }
@ -398,6 +399,7 @@ grub_script_execute_cmdfor (struct grub_script_cmd *cmd)
unsigned i; unsigned i;
grub_err_t result; grub_err_t result;
struct grub_script_argv argv = { 0, 0 }; struct grub_script_argv argv = { 0, 0 };
struct grub_script_cmdfor *cmdfor = (struct grub_script_cmdfor *) cmd; struct grub_script_cmdfor *cmdfor = (struct grub_script_cmdfor *) cmd;
if (grub_script_arglist_to_argv (cmdfor->words, &argv)) if (grub_script_arglist_to_argv (cmdfor->words, &argv))
@ -407,6 +409,9 @@ grub_script_execute_cmdfor (struct grub_script_cmd *cmd)
result = 0; result = 0;
for (i = 0; i < argv.argc; i++) for (i = 0; i < argv.argc; i++)
{ {
if (is_continue && active_breaks == 1)
active_breaks = 0;
if (! active_breaks) if (! active_breaks)
{ {
grub_script_env_set (cmdfor->name->str, argv.args[i]); grub_script_env_set (cmdfor->name->str, argv.args[i]);
@ -439,14 +444,17 @@ grub_script_execute_cmdwhile (struct grub_script_cmd *cmd)
result = grub_script_execute_cmd (cmdwhile->list); result = grub_script_execute_cmd (cmdwhile->list);
if (active_breaks == 1 && is_continue)
active_breaks = 0;
if (active_breaks) if (active_breaks)
{ break;
active_breaks--;
break;
}
} while (1); /* XXX Put a check for ^C here */ } while (1); /* XXX Put a check for ^C here */
if (active_breaks)
active_breaks--;
active_loops--; active_loops--;
return result; return result;
} }

View file

@ -42,12 +42,15 @@ grub_normal_parse_line (char *line, grub_reader_getline_t getline)
} }
static grub_command_t cmd_break; static grub_command_t cmd_break;
static grub_command_t cmd_continue;
void void
grub_script_init (void) grub_script_init (void)
{ {
cmd_break = grub_register_command ("break", grub_script_break, cmd_break = grub_register_command ("break", grub_script_break,
N_("[n]"), N_("Exit from loops")); N_("[n]"), N_("Exit from loops"));
cmd_continue = grub_register_command ("continue", grub_script_break,
N_("[n]"), N_("Coninue loops"));
} }
void void
@ -56,4 +59,9 @@ grub_script_fini (void)
if (cmd_break) if (cmd_break)
grub_unregister_command (cmd_break); grub_unregister_command (cmd_break);
cmd_break = 0; cmd_break = 0;
if (cmd_continue)
grub_unregister_command (cmd_continue);
cmd_continue = 0;
} }

View file

@ -0,0 +1,86 @@
#! @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 <http://www.gnu.org/licenses/>.
# continue without any arguments
for i in 1 2 3 4 5 6 7 8 9 10
do
if test "$i" = 5
then
continue
fi
echo $i
done
# continue with one
for i in 1 2 3 4 5 6 7 8 9 10
do
if test "$i" = 5
then
continue 1
fi
echo $i
done
# continue with loop count
for i in 1 2 3 4 5
do
for j in a b c d e f
do
if test "$i" = 3
then
if test "$j" = d
then
continue 2
fi
echo "$i $j"
fi
done
done
# continue into middle loop
for i in 1 2 3 4 5
do
for j in a b c d e f
do
if test "$i" = 3
then
if test "$j" = d
then
continue 1
fi
echo "$i $j"
fi
done
done
# while and until loops
a=
while test "$a" != "aaaaaaa"
do
a="a$a"
for i in 1 2 3 4
do
b=
until test "$b" = "bbbbb"
do
b="b$b"
if test "$i" = 3; then echo "continue 2"; continue 2; fi
echo "$a $i $b"
done
done
done