break command support

This commit is contained in:
BVK Chaitanya 2010-05-05 15:34:26 +05:30
parent 3472009838
commit a7aa28248b
7 changed files with 155 additions and 9 deletions

View file

@ -30,8 +30,29 @@
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;
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)) > active_loops ||
(*p != '\0'))
return grub_error (GRUB_ERR_BAD_ARGUMENT, "bad break");
active_breaks = count;
return GRUB_ERR_NONE;
}
static char *
grub_script_env_get (const char *name)
{
@ -242,8 +263,10 @@ 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 new_scope;
active_loops = 0;
new_scope.argc = argc;
new_scope.args = args;
grub_list_push (GRUB_AS_LIST_P (&scope), GRUB_AS_LIST (&new_scope));
@ -251,6 +274,7 @@ grub_script_function_call (grub_script_function_t func, int argc, char **args)
ret = grub_script_execute (func->func);
grub_list_pop (GRUB_AS_LIST_P (&scope));
active_loops = loops;
return ret;
}
@ -338,7 +362,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;
@ -380,14 +404,22 @@ grub_script_execute_cmdfor (struct grub_script_cmd *cmd)
if (!args)
return grub_errno;
active_loops++;
result = 0;
for (i = 0; i < argcount; i++)
{
grub_script_env_set (cmdfor->name->str, args[i]);
result = grub_script_execute_cmd (cmdfor->list);
if (! active_breaks)
{
grub_script_env_set (cmdfor->name->str, args[i]);
result = grub_script_execute_cmd (cmdfor->list);
}
grub_free (args[i]);
}
if (active_breaks)
active_breaks--;
active_loops--;
grub_free (args);
return result;
}
@ -400,6 +432,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);
@ -407,8 +440,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;
}

View file

@ -17,6 +17,7 @@
*/
#include <grub/dl.h>
#include <grub/i18n.h>
#include <grub/parser.h>
#include <grub/script_sh.h>
@ -49,6 +50,8 @@ static struct grub_parser grub_sh_parser =
GRUB_MOD_INIT(sh)
{
grub_parser_register ("grub", &grub_sh_parser);
grub_register_command ("break", grub_script_break,
N_("[n]"), N_("Exit from loops"));
}
GRUB_MOD_FINI(sh)