diff --git a/ChangeLog.while-until-loops b/ChangeLog.while-until-loops index 33a3a839d..ac54b8cd9 100644 --- a/ChangeLog.while-until-loops +++ b/ChangeLog.while-until-loops @@ -13,3 +13,8 @@ * util/grub-script-check.c (grub_script_execute_cmdwhile): New function. + * tests/grub_script_while1.in: New testcase. + * conf/tests.rmk: New testcase make rule. + + * commands/true.c (grub_cmd_false): Remove printing error message. + * commands/test.c (grub_cmd_test): Likewise. diff --git a/commands/test.c b/commands/test.c index 6995165cf..64ad69522 100644 --- a/commands/test.c +++ b/commands/test.c @@ -412,8 +412,7 @@ grub_cmd_test (grub_command_t cmd __attribute__ ((unused)), if (argc >= 1 && grub_strcmp (args[argc - 1], "]") == 0) argc--; - return test_parse (args, &argn, argc) ? GRUB_ERR_NONE - : grub_error (GRUB_ERR_TEST_FAILURE, "false"); + return test_parse (args, &argn, argc) ? 0 : 1; } static grub_command_t cmd_1, cmd_2; diff --git a/commands/true.c b/commands/true.c index aa8125853..20403c588 100644 --- a/commands/true.c +++ b/commands/true.c @@ -34,7 +34,7 @@ grub_cmd_false (struct grub_command *cmd __attribute__ ((unused)), int argc __attribute__ ((unused)), char *argv[] __attribute__ ((unused))) { - return grub_error (GRUB_ERR_TEST_FAILURE, "false"); + return 1; } static grub_command_t cmd_true, cmd_false; diff --git a/conf/tests.rmk b/conf/tests.rmk index 92f14797d..442beb0d3 100644 --- a/conf/tests.rmk +++ b/conf/tests.rmk @@ -53,6 +53,9 @@ grub_script_vars1_SOURCES = tests/grub_script_vars1.in check_SCRIPTS += grub_script_for1 grub_script_for1_SOURCES = tests/grub_script_for1.in +check_SCRIPTS += grub_script_while1 +grub_script_while1_SOURCES = tests/grub_script_while1.in + # List of tests to execute on "make check" # SCRIPTED_TESTS = example_scripted_test # SCRIPTED_TESTS += example_grub_script_test @@ -63,6 +66,7 @@ SCRIPTED_TESTS = grub_script_echo1 SCRIPTED_TESTS += grub_script_echo_keywords SCRIPTED_TESTS += grub_script_vars1 SCRIPTED_TESTS += grub_script_for1 +SCRIPTED_TESTS += grub_script_while1 # dependencies between tests and testing-tools $(SCRIPTED_TESTS): grub-shell grub-shell-tester diff --git a/script/execute.c b/script/execute.c index b905de46f..37686627d 100644 --- a/script/execute.c +++ b/script/execute.c @@ -26,13 +26,24 @@ #include #include +/* Max digits for a char is 3 (0xFF is 255), similarly for an int it + is sizeof (int) * 3, and one extra for a possible -ve sign. */ +#define ERRNO_DIGITS_MAX (sizeof (int) * 3 + 1) + static grub_err_t grub_script_execute_cmd (struct grub_script_cmd *cmd) { + int ret; + char errnobuf[ERRNO_DIGITS_MAX + 1]; + if (cmd == 0) return 0; - return cmd->exec (cmd); + ret = cmd->exec (cmd); + + grub_snprintf (errnobuf, sizeof (errnobuf), "%d", ret); + grub_env_set ("?", errnobuf); + return ret; } /* Expand arguments in ARGLIST into multiple arguments. */ @@ -188,7 +199,6 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd) grub_err_t ret = 0; int argcount = 0; grub_script_function_t func = 0; - char errnobuf[18]; char *cmdname; /* Lookup the command. */ @@ -222,11 +232,7 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd) grub_env_set (assign, eq); } grub_free (assign); - - grub_snprintf (errnobuf, sizeof (errnobuf), "%d", grub_errno); - grub_env_set ("?", errnobuf); - - return 0; + return grub_errno; } } @@ -241,9 +247,6 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd) grub_free (args[i]); grub_free (args); - grub_snprintf (errnobuf, sizeof (errnobuf), "%d", ret); - grub_env_set ("?", errnobuf); - return ret; } @@ -251,13 +254,14 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd) grub_err_t grub_script_execute_cmdblock (struct grub_script_cmd *cmd) { + int ret = 0; struct grub_script_cmdblock *cmdblock = (struct grub_script_cmdblock *) cmd; /* Loop over every command and execute it. */ for (cmd = cmdblock->cmdlist; cmd; cmd = cmd->next) - grub_script_execute_cmd (cmd); + ret = grub_script_execute_cmd (cmd); - return 0; + return ret; } /* Execute an if statement. */