diff --git a/ChangeLog b/ChangeLog index 7ad408602..835596322 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,21 @@ +2010-08-12 BVK Chaitanya + + "break" command support to GRUB script. + + * conf/common.rmk: Rule updates to grub-script-check. + * include/grub/misc.h (grub_min): New function. + * include/grub/script_sh.h (grub_script_init): New prototype. + (grub_script_fini): New prototype. + (grub_script_break): New prototype. + * script/main.c (grub_script_init): New function. + (grub_script_fini): New function. + * script/execute.c (grub_script_break): New function. + * normal/main.c: Calls to grub_script_{init,fini}. + * util/grub-script-check.c (grub_script_break): New function. + + * tests/grub_script_break.in: New testcase. + * conf/tests.rmk: Rules for new test case. + 2010-08-12 BVK Chaitanya Function parameters support to GRUB script. diff --git a/conf/common.rmk b/conf/common.rmk index 4ab829470..908bd74e8 100644 --- a/conf/common.rmk +++ b/conf/common.rmk @@ -94,7 +94,7 @@ grub_script_check_SOURCES = gnulib/progname.c gnulib/getdelim.c gnulib/getline.c util/grub-script-check.c util/misc.c kern/emu/misc.c kern/emu/mm.c \ script/main.c script/script.c script/function.c script/lexer.c \ kern/err.c kern/list.c \ - kern/misc.c kern/env.c grub_script.tab.c \ + kern/command.c kern/misc.c kern/env.c grub_script.tab.c \ grub_script.yy.c grub_script_check_CFLAGS = $(GNULIB_UTIL_CFLAGS) grub_script_check_DEPENDENCIES = grub_script.tab.h diff --git a/conf/tests.rmk b/conf/tests.rmk index 9144e5528..8af4207b7 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_break +grub_script_break_SOURCES = tests/grub_script_break.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_break # dependencies between tests and testing-tools $(SCRIPTED_TESTS): grub-shell grub-shell-tester diff --git a/include/grub/misc.h b/include/grub/misc.h index eab01b0fb..df80cf192 100644 --- a/include/grub/misc.h +++ b/include/grub/misc.h @@ -288,6 +288,15 @@ grub_abs (int x) return (unsigned int) x; } +static inline long +grub_min (long x, long y) +{ + if (x < y) + return x; + else + return y; +} + static inline long grub_max (long x, long y) { diff --git a/include/grub/script_sh.h b/include/grub/script_sh.h index 9bfc7e4bb..512498a59 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; @@ -222,6 +223,9 @@ struct grub_parser_param struct grub_lexer_param *lexerstate; }; +void grub_script_init (void); +void grub_script_fini (void); + void grub_script_argv_free (struct grub_script_argv *argv); int grub_script_argv_next (struct grub_script_argv *argv); int grub_script_argv_append (struct grub_script_argv *argv, const char *s); @@ -311,6 +315,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); +/* Break command for loops. */ +grub_err_t grub_script_break (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/normal/main.c b/normal/main.c index 710b68d14..5df889466 100644 --- a/normal/main.c +++ b/normal/main.c @@ -675,6 +675,7 @@ static void (*grub_xputs_saved) (const char *str); GRUB_MOD_INIT(normal) { grub_context_init (); + grub_script_init (); grub_xputs_saved = grub_xputs; grub_xputs = grub_xputs_normal; @@ -709,6 +710,7 @@ GRUB_MOD_INIT(normal) GRUB_MOD_FINI(normal) { grub_context_fini (); + grub_script_fini (); grub_xputs = grub_xputs_saved; diff --git a/script/execute.c b/script/execute.c index a1e932df2..bcb3d4017 100644 --- a/script/execute.c +++ b/script/execute.c @@ -30,6 +30,9 @@ is sizeof (int) * 3, and one extra for a possible -ve sign. */ #define ERRNO_DIGITS_MAX (sizeof (int) * 3 + 1) +static unsigned long active_loops; +static unsigned long active_breaks; + /* Scope for grub script functions. */ struct grub_script_scope { @@ -37,6 +40,24 @@ struct grub_script_scope }; static struct grub_script_scope *scope = 0; +grub_err_t +grub_script_break (grub_command_t cmd __attribute__((unused)), + int argc, char *argv[]) +{ + char *p = 0; + unsigned long count; + + if (argc == 0) + count = 1; + + else if ((argc > 1) || (count = grub_strtoul (argv[0], &p, 10)) == 0 || + (*p != '\0')) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "bad break"); + + active_breaks = grub_min (active_loops, count); + return GRUB_ERR_NONE; +} + static int grub_env_special (const char *name) { @@ -244,9 +265,11 @@ grub_err_t grub_script_function_call (grub_script_function_t func, int argc, char **args) { grub_err_t ret = 0; + unsigned long loops = active_loops; struct grub_script_scope *old_scope; struct grub_script_scope new_scope; + active_loops = 0; new_scope.argv.argc = argc; new_scope.argv.args = args; @@ -255,6 +278,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; return ret; } @@ -339,7 +363,7 @@ grub_script_execute_cmdlist (struct grub_script_cmd *list) struct grub_script_cmd *cmd; /* Loop over every command and execute it. */ - for (cmd = list->next; cmd; cmd = cmd->next) + for (cmd = list->next; cmd && ! active_breaks; cmd = cmd->next) ret = grub_script_execute_cmd (cmd); return ret; @@ -379,13 +403,21 @@ grub_script_execute_cmdfor (struct grub_script_cmd *cmd) if (grub_script_arglist_to_argv (cmdfor->words, &argv)) return grub_errno; + active_loops++; result = 0; for (i = 0; i < argv.argc; i++) { - grub_script_env_set (cmdfor->name->str, argv.args[i]); - result = grub_script_execute_cmd (cmdfor->list); + if (! active_breaks) + { + grub_script_env_set (cmdfor->name->str, argv.args[i]); + result = grub_script_execute_cmd (cmdfor->list); + } } + if (active_breaks) + active_breaks--; + + active_loops--; grub_script_argv_free (&argv); return result; } @@ -398,6 +430,7 @@ grub_script_execute_cmdwhile (struct grub_script_cmd *cmd) int result; struct grub_script_cmdwhile *cmdwhile = (struct grub_script_cmdwhile *) cmd; + active_loops++; result = 0; do { cond = grub_script_execute_cmd (cmdwhile->cond); @@ -405,8 +438,16 @@ grub_script_execute_cmdwhile (struct grub_script_cmd *cmd) break; result = grub_script_execute_cmd (cmdwhile->list); + + if (active_breaks) + { + active_breaks--; + break; + } + } while (1); /* XXX Put a check for ^C here */ + active_loops--; return result; } diff --git a/script/main.c b/script/main.c index 752a8cd8a..382b35487 100644 --- a/script/main.c +++ b/script/main.c @@ -17,6 +17,7 @@ */ #include +#include #include #include @@ -39,3 +40,20 @@ grub_normal_parse_line (char *line, grub_reader_getline_t getline) return grub_errno; } + +static grub_command_t cmd_break; + +void +grub_script_init (void) +{ + cmd_break = grub_register_command ("break", grub_script_break, + N_("[n]"), N_("Exit from loops")); +} + +void +grub_script_fini (void) +{ + if (cmd_break) + grub_unregister_command (cmd_break); + cmd_break = 0; +} diff --git a/tests/grub_script_break.in b/tests/grub_script_break.in new file mode 100644 index 000000000..bf265e8b3 --- /dev/null +++ b/tests/grub_script_break.in @@ -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 . + +# break without any arguments +for i in 1 2 3 4 5 6 7 8 9 10 +do + echo $i + if test "$i" = 5 + then + break + fi +done + +# break with one +for i in 1 2 3 4 5 6 7 8 9 10 +do + echo $i + if test "$i" = 5 + then + break 1 + fi +done + +# break with loop count +for i in 1 2 3 4 5 +do + for j in a b c d e f + do + echo "$i $j" + if test "$i" = 3 + then + if test "$j" = d + then + break 2 + fi + fi + done +done + +# break into middle loop +for i in 1 2 3 4 5 +do + for j in a b c d e f + do + echo "$i $j" + if test "$i" = 3 + then + if test "$j" = d + then + break 1 + fi + 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" + echo "$a $i $b" + if test "$i" = 3; then echo "break 2"; break 2; fi + done + done +done + diff --git a/util/grub-script-check.c b/util/grub-script-check.c index 0eb0e5101..ac59d5fcc 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_break (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))) {