merge with mainline

This commit is contained in:
BVK Chaitanya 2010-08-15 11:12:53 +05:30
commit 1379af7834
29 changed files with 1468 additions and 797 deletions

View file

@ -21,6 +21,7 @@
#include <grub/misc.h>
#include <grub/script_sh.h>
/* Return nearest power of two that is >= v. */
static unsigned
round_up_exp (unsigned v)
{
@ -52,8 +53,6 @@ grub_script_argv_free (struct grub_script_argv *argv)
grub_free (argv->args);
}
if (argv->script)
grub_script_put (argv->script);
argv->argc = 0;
argv->args = 0;

View file

@ -31,6 +31,10 @@
is sizeof (int) * 3, and one extra for a possible -ve sign. */
#define ERRNO_DIGITS_MAX (sizeof (int) * 3 + 1)
static unsigned long is_continue;
static unsigned long active_loops;
static unsigned long active_breaks;
/* Scope for grub script functions. */
struct grub_script_scope
{
@ -38,6 +42,55 @@ struct grub_script_scope
};
static struct grub_script_scope *scope = 0;
grub_err_t
grub_script_break (grub_command_t cmd, 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");
is_continue = grub_strcmp (cmd->name, "break") ? 1 : 0;
active_breaks = grub_min (active_loops, count);
return GRUB_ERR_NONE;
}
grub_err_t
grub_script_shift (grub_command_t cmd __attribute__((unused)),
int argc, char *argv[])
{
char *p = 0;
unsigned long n = 0;
if (! scope)
return GRUB_ERR_NONE;
if (argc == 0)
n = 1;
else if (argc > 1)
return GRUB_ERR_BAD_ARGUMENT;
else
{
n = grub_strtoul (argv[0], &p, 10);
if (*p != '\0')
return GRUB_ERR_BAD_ARGUMENT;
}
if (n > scope->argv.argc)
return GRUB_ERR_BAD_ARGUMENT;
scope->argv.argc -= n;
scope->argv.args += n;
return GRUB_ERR_NONE;
}
static int
grub_env_special (const char *name)
{
@ -201,7 +254,7 @@ grub_script_arglist_to_argv (struct grub_script_arglist *arglist,
grub_script_argv_append (&result, arg->str) ||
grub_script_argv_append (&result, "}"))
goto fail;
result.script = grub_script_get (arg->script);
result.script = arg->script;
break;
case GRUB_SCRIPT_ARG_TYPE_TEXT:
@ -253,9 +306,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;
@ -264,6 +319,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;
}
@ -337,7 +393,7 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd)
struct grub_script_argv argv = { 0, 0, 0 };
/* Lookup the command. */
if (grub_script_arglist_to_argv (cmdline->arglist, &argv))
if (grub_script_arglist_to_argv (cmdline->arglist, &argv) || ! argv.args[0])
return grub_errno;
cmdname = argv.args[0];
@ -367,7 +423,7 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd)
grub_free (assign);
grub_snprintf (errnobuf, sizeof (errnobuf), "%d", grub_errno);
grub_env_set ("?", errnobuf);
grub_script_env_set ("?", errnobuf);
grub_script_argv_free (&argv);
grub_print_error ();
@ -411,7 +467,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;
@ -451,13 +507,24 @@ 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 (is_continue && active_breaks == 1)
active_breaks = 0;
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;
}
@ -470,6 +537,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);
@ -477,8 +545,19 @@ grub_script_execute_cmdwhile (struct grub_script_cmd *cmd)
break;
result = grub_script_execute_cmd (cmdwhile->list);
if (active_breaks == 1 && is_continue)
active_breaks = 0;
if (active_breaks)
break;
} while (1); /* XXX Put a check for ^C here */
if (active_breaks)
active_breaks--;
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>
@ -39,3 +40,34 @@ grub_normal_parse_line (char *line, grub_reader_getline_t getline)
return grub_errno;
}
static grub_command_t cmd_break;
static grub_command_t cmd_continue;
static grub_command_t cmd_shift;
void
grub_script_init (void)
{
cmd_break = grub_register_command ("break", grub_script_break,
N_("[n]"), N_("Exit from loops"));
cmd_continue = grub_register_command ("continue", grub_script_break,
N_("[n]"), N_("Continue loops"));
cmd_shift = grub_register_command ("shift", grub_script_shift,
N_("[n]"), N_("Shift positional parameters."));
}
void
grub_script_fini (void)
{
if (cmd_break)
grub_unregister_command (cmd_break);
cmd_break = 0;
if (cmd_continue)
grub_unregister_command (cmd_continue);
cmd_continue = 0;
if (cmd_shift)
grub_unregister_command (cmd_shift);
cmd_shift = 0;
}