shift command support

This commit is contained in:
BVK Chaitanya 2010-05-07 10:08:09 +05:30
parent 7897070571
commit e9efa0fe36
7 changed files with 121 additions and 3 deletions

View file

@ -32,6 +32,31 @@
static struct grub_script_scope *scope = 0;
grub_err_t
grub_script_cmd_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;
}
scope->shift = (unsigned int)(n > scope->argc ? 0 : n);
return GRUB_ERR_NONE;
}
static char *
grub_script_env_get (const char *name)
{
@ -49,7 +74,10 @@ grub_script_env_get (const char *name)
if (num == 0)
return 0; /* XXX no file name, for now. */
return (num > scope->argc ? 0 : scope->args[num - 1]);
if (num > scope->argc - scope->shift)
return 0;
else
return scope->args[num - 1 + scope->shift];
}
else
{
@ -60,7 +88,7 @@ grub_script_env_get (const char *name)
else if (grub_strcmp (name, "#") == 0)
{
static char buf[32]; /* Rewritten everytime. */
grub_snprintf (buf, sizeof (buf), "%u", scope->argc);
grub_snprintf (buf, sizeof (buf), "%u", scope->argc - scope->shift);
return buf;
}
else
@ -244,6 +272,7 @@ grub_script_function_call (grub_script_function_t func, int argc, char **args)
grub_err_t ret = 0;
struct grub_script_scope new_scope;
new_scope.shift = 0;
new_scope.argc = argc;
new_scope.args = args;
grub_list_push (GRUB_AS_LIST_P (&scope), GRUB_AS_LIST (&new_scope));

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 ("shift", grub_script_cmd_shift,
N_("[n]"), N_("Shift positional parameters."));
}
GRUB_MOD_FINI(sh)