pulled in menuentry branch for OPTION_REPEATABLE

This commit is contained in:
BVK Chaitanya 2010-08-04 10:24:57 +05:30
commit 72bb1398e3
53 changed files with 1302 additions and 750 deletions

View file

@ -458,10 +458,9 @@ free_tables (void)
}
static grub_err_t
grub_cmd_acpi (struct grub_extcmd *cmd,
int argc, char **args)
grub_cmd_acpi (struct grub_extcmd_context *ctxt, int argc, char **args)
{
struct grub_arg_list *state = cmd->state;
struct grub_arg_list *state = ctxt->state;
struct grub_acpi_rsdp_v10 *rsdp;
struct efiemu_acpi_table *cur, *t;
grub_err_t err;

View file

@ -33,9 +33,9 @@ static const struct grub_arg_option options[] =
};
static grub_err_t
grub_cmd_cat (grub_extcmd_t cmd, int argc, char **args)
grub_cmd_cat (grub_extcmd_context_t ctxt, int argc, char **args)
{
struct grub_arg_list *state = cmd->state;
struct grub_arg_list *state = ctxt->state;
int dos = 0;
grub_file_t file;
char buf[GRUB_DISK_SECTOR_SIZE];

View file

@ -30,9 +30,9 @@ static const struct grub_arg_option options[] =
};
static grub_err_t
grub_cmd_echo (grub_extcmd_t cmd, int argc, char **args)
grub_cmd_echo (grub_extcmd_context_t ctxt, int argc, char **args)
{
struct grub_arg_list *state = cmd->state;
struct grub_arg_list *state = ctxt->state;
int newline = 1;
int i;

View file

@ -21,46 +21,55 @@
#include <grub/list.h>
#include <grub/misc.h>
#include <grub/extcmd.h>
#include <grub/script_sh.h>
static grub_err_t
grub_extcmd_dispatcher (struct grub_command *cmd,
int argc, char **args)
grub_err_t
grub_extcmd_dispatcher (struct grub_command *cmd, int argc, char **args,
struct grub_script *script)
{
int new_argc;
char **new_args;
struct grub_arg_option *parser;
struct grub_arg_list *state;
int maxargs = 0;
struct grub_extcmd_context context;
grub_err_t ret;
grub_extcmd_t ext;
grub_extcmd_t ext = cmd->data;
ext = cmd->data;
parser = (struct grub_arg_option *) ext->options;
while (parser && (parser++)->doc)
maxargs++;
context.state = 0;
context.extcmd = ext;
context.script = script;
/* Set up the option state. */
state = grub_zalloc (sizeof (struct grub_arg_list) * maxargs);
if (! ext->options)
{
ret = (ext->func) (&context, argc, args);
return ret;
}
state = grub_arg_list_alloc (ext, argc, args);
if (grub_arg_parse (ext, argc, args, state, &new_args, &new_argc))
{
ext->state = state;
ret = (ext->func) (ext, new_argc, new_args);
context.state = state;
ret = (ext->func) (&context, new_argc, new_args);
grub_free (new_args);
grub_free (state);
return ret;
}
else
ret = grub_errno;
grub_free (state);
return grub_errno;
}
return ret;
static grub_err_t
grub_extcmd_dispatch (struct grub_command *cmd, int argc, char **args)
{
return grub_extcmd_dispatcher (cmd, argc, args, 0);
}
grub_extcmd_t
grub_register_extcmd (const char *name, grub_extcmd_func_t func,
unsigned flags, const char *summary,
const char *description,
const struct grub_arg_option *parser)
grub_register_extcmd_prio (const char *name, grub_extcmd_func_t func,
unsigned flags, const char *summary,
const char *description,
const struct grub_arg_option *parser,
int prio)
{
grub_extcmd_t ext;
grub_command_t cmd;
@ -69,8 +78,8 @@ grub_register_extcmd (const char *name, grub_extcmd_func_t func,
if (! ext)
return 0;
cmd = grub_register_command_prio (name, grub_extcmd_dispatcher,
summary, description, 1);
cmd = grub_register_command_prio (name, grub_extcmd_dispatch,
summary, description, prio);
if (! cmd)
{
grub_free (ext);
@ -88,6 +97,16 @@ grub_register_extcmd (const char *name, grub_extcmd_func_t func,
return ext;
}
grub_extcmd_t
grub_register_extcmd (const char *name, grub_extcmd_func_t func,
unsigned flags, const char *summary,
const char *description,
const struct grub_arg_option *parser)
{
return grub_register_extcmd_prio (name, func, flags,
summary, description, parser, 1);
}
void
grub_unregister_extcmd (grub_extcmd_t ext)
{

View file

@ -165,10 +165,10 @@ check_list (const gcry_md_spec_t *hash, const char *hashfilename,
}
static grub_err_t
grub_cmd_hashsum (struct grub_extcmd *cmd,
grub_cmd_hashsum (struct grub_extcmd_context *ctxt,
int argc, char **args)
{
struct grub_arg_list *state = cmd->state;
struct grub_arg_list *state = ctxt->state;
const char *hashname = NULL;
const char *prefix = NULL;
const gcry_md_spec_t *hash;
@ -177,7 +177,7 @@ grub_cmd_hashsum (struct grub_extcmd *cmd,
unsigned unread = 0;
for (i = 0; i < ARRAY_SIZE (aliases); i++)
if (grub_strcmp (cmd->cmd->name, aliases[i].name) == 0)
if (grub_strcmp (ctxt->extcmd->cmd->name, aliases[i].name) == 0)
hashname = aliases[i].hashname;
if (state[0].set)
hashname = state[0].arg;

View file

@ -270,9 +270,9 @@ static int get_int_arg (const struct grub_arg_list *state)
}
static grub_err_t
grub_cmd_hdparm (grub_extcmd_t cmd, int argc, char **args) // state????
grub_cmd_hdparm (grub_extcmd_context_t ctxt, int argc, char **args) // state????
{
struct grub_arg_list *state = cmd->state;
struct grub_arg_list *state = ctxt->state;
/* Check command line. */
if (argc != 1)

View file

@ -27,7 +27,7 @@
#include <grub/charset.h>
static grub_err_t
grub_cmd_help (grub_extcmd_t ext __attribute__ ((unused)), int argc,
grub_cmd_help (grub_extcmd_context_t ctxt __attribute__ ((unused)), int argc,
char **args)
{
int cnt = 0;

View file

@ -34,9 +34,9 @@ static const struct grub_arg_option options[] = {
};
static grub_err_t
grub_cmd_hexdump (grub_extcmd_t cmd, int argc, char **args)
grub_cmd_hexdump (grub_extcmd_context_t ctxt, int argc, char **args)
{
struct grub_arg_list *state = cmd->state;
struct grub_arg_list *state = ctxt->state;
char buf[GRUB_DISK_SECTOR_SIZE * 4];
grub_ssize_t size, length;
grub_disk_addr_t skip;

View file

@ -43,7 +43,7 @@ static const struct grub_arg_option options[] =
unsigned char grub_cpuid_has_longmode = 0;
static grub_err_t
grub_cmd_cpuid (grub_extcmd_t cmd __attribute__ ((unused)),
grub_cmd_cpuid (grub_extcmd_context_t ctxt __attribute__ ((unused)),
int argc __attribute__ ((unused)),
char **args __attribute__ ((unused)))
{

View file

@ -196,13 +196,13 @@ list_mappings (void)
}
static grub_err_t
grub_cmd_drivemap (struct grub_extcmd *cmd, int argc, char **args)
grub_cmd_drivemap (struct grub_extcmd_context *ctxt, int argc, char **args)
{
if (cmd->state[OPTIDX_LIST].set)
if (ctxt->state[OPTIDX_LIST].set)
{
return list_mappings ();
}
else if (cmd->state[OPTIDX_RESET].set)
else if (ctxt->state[OPTIDX_RESET].set)
{
/* Reset: just delete all mappings, freeing their memory. */
drivemap_node_t *curnode = map_head;
@ -216,7 +216,7 @@ grub_cmd_drivemap (struct grub_extcmd *cmd, int argc, char **args)
map_head = 0;
return GRUB_ERR_NONE;
}
else if (!cmd->state[OPTIDX_SWAP].set && argc == 0)
else if (!ctxt->state[OPTIDX_SWAP].set && argc == 0)
{
/* No arguments */
return list_mappings ();
@ -248,11 +248,11 @@ grub_cmd_drivemap (struct grub_extcmd *cmd, int argc, char **args)
}
/* Set the mapping for the disk (overwrites any existing mapping). */
grub_dprintf ("drivemap", "%s %s (%02x) = %s (%02x)\n",
cmd->state[OPTIDX_SWAP].set ? "Swapping" : "Mapping",
ctxt->state[OPTIDX_SWAP].set ? "Swapping" : "Mapping",
args[1], mapto, args[0], mapfrom);
err = drivemap_set (mapto, mapfrom);
/* If -s, perform the reverse mapping too (only if the first was OK). */
if (cmd->state[OPTIDX_SWAP].set && err == GRUB_ERR_NONE)
if (ctxt->state[OPTIDX_SWAP].set && err == GRUB_ERR_NONE)
err = drivemap_set (mapfrom, mapto);
return err;
}

View file

@ -29,12 +29,12 @@ static const struct grub_arg_option options[] =
};
static grub_err_t
grub_cmd_halt (grub_extcmd_t cmd,
grub_cmd_halt (grub_extcmd_context_t ctxt,
int argc __attribute__ ((unused)),
char **args __attribute__ ((unused)))
{
struct grub_arg_list *state = cmd->state;
struct grub_arg_list *state = ctxt->state;
int no_apm = 0;
if (state[0].set)
no_apm = 1;

View file

@ -36,7 +36,7 @@ static const struct grub_arg_option options[] =
static grub_err_t
grub_cmd_read (grub_extcmd_t cmd, int argc, char **argv)
grub_cmd_read (grub_extcmd_context_t ctxt, int argc, char **argv)
{
grub_target_addr_t addr;
grub_uint32_t value = 0;
@ -46,7 +46,7 @@ grub_cmd_read (grub_extcmd_t cmd, int argc, char **argv)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid number of arguments");
addr = grub_strtoul (argv[0], 0, 0);
switch (cmd->cmd->name[sizeof ("in") - 1])
switch (ctxt->extcmd->cmd->name[sizeof ("in") - 1])
{
case 'l':
value = grub_inl (addr);
@ -61,10 +61,10 @@ grub_cmd_read (grub_extcmd_t cmd, int argc, char **argv)
break;
}
if (cmd->state[0].set)
if (ctxt->state[0].set)
{
grub_snprintf (buf, sizeof (buf), "%x", value);
grub_env_set (cmd->state[0].arg, buf);
grub_env_set (ctxt->state[0].arg, buf);
}
else
grub_printf ("0x%x\n", value);

View file

@ -34,11 +34,11 @@ static const struct grub_arg_option options[] =
#define grub_cur_term_input grub_term_get_current_input ()
static grub_err_t
grub_cmd_keystatus (grub_extcmd_t cmd,
grub_cmd_keystatus (grub_extcmd_context_t ctxt,
int argc __attribute__ ((unused)),
char **args __attribute__ ((unused)))
{
struct grub_arg_list *state = cmd->state;
struct grub_arg_list *state = ctxt->state;
int expect_mods = 0;
int mods;

View file

@ -111,11 +111,11 @@ read_envblk_file (grub_file_t file)
}
static grub_err_t
grub_cmd_load_env (grub_extcmd_t cmd,
grub_cmd_load_env (grub_extcmd_context_t ctxt,
int argc __attribute__ ((unused)),
char **args __attribute__ ((unused)))
{
struct grub_arg_list *state = cmd->state;
struct grub_arg_list *state = ctxt->state;
grub_file_t file;
grub_envblk_t envblk;
@ -143,11 +143,11 @@ grub_cmd_load_env (grub_extcmd_t cmd,
}
static grub_err_t
grub_cmd_list_env (grub_extcmd_t cmd,
grub_cmd_list_env (grub_extcmd_context_t ctxt,
int argc __attribute__ ((unused)),
char **args __attribute__ ((unused)))
{
struct grub_arg_list *state = cmd->state;
struct grub_arg_list *state = ctxt->state;
grub_file_t file;
grub_envblk_t envblk;
@ -280,9 +280,9 @@ write_blocklists (grub_envblk_t envblk, struct blocklist *blocklists,
}
static grub_err_t
grub_cmd_save_env (grub_extcmd_t cmd, int argc, char **args)
grub_cmd_save_env (grub_extcmd_context_t ctxt, int argc, char **args)
{
struct grub_arg_list *state = cmd->state;
struct grub_arg_list *state = ctxt->state;
grub_file_t file;
grub_envblk_t envblk;
struct blocklist *head = 0;

View file

@ -248,9 +248,9 @@ grub_ls_list_files (char *dirname, int longlist, int all, int human)
}
static grub_err_t
grub_cmd_ls (grub_extcmd_t cmd, int argc, char **args)
grub_cmd_ls (grub_extcmd_context_t ctxt, int argc, char **args)
{
struct grub_arg_list *state = cmd->state;
struct grub_arg_list *state = ctxt->state;
if (argc == 0)
grub_ls_list_devices (state[0].set);

View file

@ -211,11 +211,11 @@ grub_lspci_iter (grub_pci_device_t dev, grub_pci_id_t pciid)
}
static grub_err_t
grub_cmd_lspci (grub_extcmd_t cmd,
grub_cmd_lspci (grub_extcmd_context_t ctxt,
int argc __attribute__ ((unused)),
char **args __attribute__ ((unused)))
{
iospace = cmd->state[0].set;
iospace = ctxt->state[0].set;
grub_pci_iterate (grub_lspci_iter);
return GRUB_ERR_NONE;
}

View file

@ -35,7 +35,7 @@ static const struct grub_arg_option options[] =
static grub_err_t
grub_cmd_read (grub_extcmd_t cmd, int argc, char **argv)
grub_cmd_read (grub_extcmd_context_t ctxt, int argc, char **argv)
{
grub_target_addr_t addr;
grub_uint32_t value = 0;
@ -45,7 +45,7 @@ grub_cmd_read (grub_extcmd_t cmd, int argc, char **argv)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid number of arguments");
addr = grub_strtoul (argv[0], 0, 0);
switch (cmd->cmd->name[sizeof ("read_") - 1])
switch (ctxt->extcmd->cmd->name[sizeof ("read_") - 1])
{
case 'd':
value = *((volatile grub_uint32_t *) addr);
@ -60,10 +60,10 @@ grub_cmd_read (grub_extcmd_t cmd, int argc, char **argv)
break;
}
if (cmd->state[0].set)
if (ctxt->state[0].set)
{
grub_snprintf (buf, sizeof (buf), "%x", value);
grub_env_set (cmd->state[0].arg, buf);
grub_env_set (ctxt->state[0].arg, buf);
}
else
grub_printf ("0x%x\n", value);

213
commands/menuentry.c Normal file
View file

@ -0,0 +1,213 @@
/* menuentry.c - menuentry command */
/*
* GRUB -- GRand Unified Bootloader
* 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 <http://www.gnu.org/licenses/>.
*/
#include <grub/types.h>
#include <grub/misc.h>
#include <grub/err.h>
#include <grub/dl.h>
#include <grub/extcmd.h>
#include <grub/i18n.h>
#include <grub/normal.h>
static const struct grub_arg_option options[] =
{
{"class", 1, GRUB_ARG_OPTION_REPEATABLE,
N_("Menu entry type."), "STRING", ARG_TYPE_STRING},
{"users", 2, 0,
N_("Users allowed to boot this entry."), "USERNAME", ARG_TYPE_STRING},
{"hotkey", 3, 0,
N_("Keyboard key for this entry."), "KEY", ARG_TYPE_STRING},
{0, 0, 0, 0, 0, 0}
};
static struct
{
char *name;
int key;
} hotkey_aliases[] =
{
{"backspace", '\b'},
{"tab", '\t'},
{"delete", GRUB_TERM_DC}
};
/* Add a menu entry to the current menu context (as given by the environment
variable data slot `menu'). As the configuration file is read, the script
parser calls this when a menu entry is to be created. */
static grub_err_t
append_menu_entry (int argc, const char **args, char **classes,
const char *users, const char *hotkey,
const char *sourcecode)
{
unsigned i;
int menu_hotkey = 0;
char **menu_args = NULL;
char *menu_users = NULL;
char *menu_title = NULL;
char *menu_sourcecode = NULL;
struct grub_menu_entry_class *menu_classes = NULL;
grub_menu_t menu;
grub_menu_entry_t *last;
menu = grub_env_get_menu ();
if (! menu)
return grub_error (GRUB_ERR_MENU, "no menu context");
last = &menu->entry_list;
menu_sourcecode = grub_strdup (sourcecode);
if (! menu_sourcecode)
return grub_errno;
if (classes)
{
for (i = 0; classes[i]; i++); /* count # of menuentry classes */
menu_classes = grub_zalloc (sizeof (struct grub_menu_entry_class) * i);
if (! menu_classes)
goto fail;
for (i = 0; classes[i]; i++)
{
menu_classes[i].name = grub_strdup (classes[i]);
if (! menu_classes[i].name)
goto fail;
menu_classes[i].next = classes[i + 1] ? &menu_classes[i + 1] : NULL;
}
}
if (users)
{
menu_users = grub_strdup (users);
if (! menu_users)
goto fail;
}
if (hotkey)
{
for (i = 0; i < ARRAY_SIZE (hotkey_aliases); i++)
if (grub_strcmp (hotkey, hotkey_aliases[i].name) == 0)
{
menu_hotkey = hotkey_aliases[i].key;
break;
}
if (i > ARRAY_SIZE (hotkey_aliases))
goto fail;
}
if (! argc)
{
grub_error (GRUB_ERR_MENU, "menuentry is missing title");
goto fail;
}
menu_title = grub_strdup (args[0]);
if (! menu_title)
goto fail;
/* Save argc, args to pass as parameters to block arg later. */
menu_args = grub_malloc (sizeof (char*) * (argc + 1));
if (! menu_args)
goto fail;
for (i = 0; args[i]; i++)
{
menu_args[i] = grub_strdup (args[i]);
if (! menu_args[i])
goto fail;
}
menu_args[argc] = NULL;
/* Add the menu entry at the end of the list. */
while (*last)
last = &(*last)->next;
*last = grub_zalloc (sizeof (**last));
if (! *last)
goto fail;
(*last)->title = menu_title;
(*last)->hotkey = menu_hotkey;
(*last)->classes = menu_classes;
if (menu_users)
(*last)->restricted = 1;
(*last)->users = menu_users;
(*last)->argc = argc;
(*last)->args = menu_args;
(*last)->sourcecode = menu_sourcecode;
menu->size++;
return GRUB_ERR_NONE;
fail:
grub_free (menu_sourcecode);
for (i = 0; menu_classes && menu_classes[i].name; i++)
grub_free (menu_classes[i].name);
grub_free (menu_classes);
for (i = 0; menu_args && menu_args[i]; i++)
grub_free (menu_args[i]);
grub_free (menu_args);
grub_free (menu_users);
grub_free (menu_title);
return grub_errno;
}
static grub_err_t
grub_cmd_menuentry (grub_extcmd_context_t ctxt, int argc, char **args)
{
char ch;
char *src;
unsigned len;
grub_err_t r;
if (! argc || ! ctxt->script)
return GRUB_ERR_BAD_ARGUMENT;
src = args[argc - 1];
args[argc - 1] = NULL;
len = grub_strlen(src);
ch = src[len - 1];
src[len - 1] = '\0';
r = append_menu_entry (argc - 1, (const char **) args,
ctxt->state[0].args, ctxt->state[1].arg,
ctxt->state[2].arg, src + 1);
src[len - 1] = ch;
args[argc - 1] = src;
return r;
}
static grub_extcmd_t cmd;
GRUB_MOD_INIT(menuentry)
{
cmd = grub_register_extcmd ("menuentry", grub_cmd_menuentry,
GRUB_COMMAND_FLAG_BOTH | GRUB_COMMAND_FLAG_BLOCKS,
N_("BLOCK"), N_("Define a menuentry."), options);
}
GRUB_MOD_FINI(menuentry)
{
grub_unregister_extcmd (cmd);
}

View file

@ -45,9 +45,9 @@ static const struct grub_arg_option options[] =
};
static grub_err_t
grub_cmd_probe (grub_extcmd_t cmd, int argc, char **args)
grub_cmd_probe (grub_extcmd_context_t ctxt, int argc, char **args)
{
struct grub_arg_list *state = cmd->state;
struct grub_arg_list *state = ctxt->state;
grub_device_t dev;
grub_fs_t fs;
char *ptr;

View file

@ -50,9 +50,9 @@ enum options
};
static grub_err_t
grub_cmd_search (grub_extcmd_t cmd, int argc, char **args)
grub_cmd_search (grub_extcmd_context_t ctxt, int argc, char **args)
{
struct grub_arg_list *state = cmd->state;
struct grub_arg_list *state = ctxt->state;
const char *var = 0;
if (argc == 0)

View file

@ -155,7 +155,7 @@ grub_setpci_iter (grub_pci_device_t dev, grub_pci_id_t pciid)
}
static grub_err_t
grub_cmd_setpci (grub_extcmd_t cmd, int argc, char **argv)
grub_cmd_setpci (grub_extcmd_context_t ctxt, int argc, char **argv)
{
const char *ptr;
unsigned i;
@ -163,14 +163,14 @@ grub_cmd_setpci (grub_extcmd_t cmd, int argc, char **argv)
pciid_check_value = 0;
pciid_check_mask = 0;
if (cmd->state[0].set)
if (ctxt->state[0].set)
{
ptr = cmd->state[0].arg;
ptr = ctxt->state[0].arg;
pciid_check_value |= (grub_strtoul (ptr, (char **) &ptr, 16) & 0xffff);
if (grub_errno == GRUB_ERR_BAD_NUMBER)
{
grub_errno = GRUB_ERR_NONE;
ptr = cmd->state[0].arg;
ptr = ctxt->state[0].arg;
}
else
pciid_check_mask |= 0xffff;
@ -191,11 +191,11 @@ grub_cmd_setpci (grub_extcmd_t cmd, int argc, char **argv)
check_bus = check_device = check_function = 0;
if (cmd->state[1].set)
if (ctxt->state[1].set)
{
const char *optr;
ptr = cmd->state[1].arg;
ptr = ctxt->state[1].arg;
optr = ptr;
bus = grub_strtoul (ptr, (char **) &ptr, 16);
if (grub_errno == GRUB_ERR_BAD_NUMBER)
@ -229,8 +229,8 @@ grub_cmd_setpci (grub_extcmd_t cmd, int argc, char **argv)
}
}
if (cmd->state[2].set)
varname = cmd->state[2].arg;
if (ctxt->state[2].set)
varname = ctxt->state[2].arg;
else
varname = NULL;

View file

@ -60,9 +60,9 @@ grub_interruptible_millisleep (grub_uint32_t ms)
}
static grub_err_t
grub_cmd_sleep (grub_extcmd_t cmd, int argc, char **args)
grub_cmd_sleep (grub_extcmd_context_t ctxt, int argc, char **args)
{
struct grub_arg_list *state = cmd->state;
struct grub_arg_list *state = ctxt->state;
int n;
if (argc != 1)

View file

@ -636,7 +636,7 @@ normal_mod_SOURCES = normal/main.c normal/cmdline.c normal/dyncmd.c \
normal/color.c normal/completion.c normal/datetime.c normal/menu.c \
normal/menu_entry.c normal/menu_text.c normal/charset.c \
normal/misc.c normal/crypto.c normal/term.c normal/context.c \
script/main.c script/script.c script/execute.c unidata.c \
script/main.c script/script.c script/execute.c script/argv.c unidata.c \
script/function.c script/lexer.c grub_script.tab.c grub_script.yy.c
normal_mod_CFLAGS = $(COMMON_CFLAGS) $(POSIX_CFLAGS) -Wno-error
normal_mod_LDFLAGS = $(COMMON_LDFLAGS)
@ -830,6 +830,12 @@ boot_mod_SOURCES = commands/boot.c lib/i386/pc/biosnum.c
boot_mod_CFLAGS = $(COMMON_CFLAGS)
boot_mod_LDFLAGS = $(COMMON_LDFLAGS)
# For menuentry.mod.
pkglib_MODULES += menuentry.mod
menuentry_mod_SOURCES = commands/menuentry.c
menuentry_mod_CFLAGS = $(COMMON_CFLAGS)
menuentry_mod_LDFLAGS = $(COMMON_LDFLAGS)
bin_UTILITIES += grub-mkpasswd-pbkdf2
grub_mkpasswd_pbkdf2_SOURCES = gnulib/progname.c gnulib/getdelim.c gnulib/getline.c util/grub-mkpasswd-pbkdf2.c lib/crypto.c lib/libgcrypt-grub/cipher/sha512.c lib/pbkdf2.c util/misc.c kern/emu/misc.c kern/emu/mm.c kern/err.c
grub_mkpasswd_pbkdf2_CFLAGS += -Wno-missing-field-initializers -Wno-error -I$(srcdir)/lib/libgcrypt_wrap -DGRUB_MKPASSWD=1

View file

@ -71,6 +71,9 @@ grub_script_dollar_SOURCES = tests/grub_script_dollar.in
check_SCRIPTS += grub_script_comments
grub_script_comments_SOURCES = tests/grub_script_comments.in
check_SCRIPTS += grub_script_functions
grub_script_functions_SOURCES = tests/grub_script_functions.in
# List of tests to execute on "make check"
# SCRIPTED_TESTS = example_scripted_test
# SCRIPTED_TESTS += example_grub_script_test
@ -87,6 +90,7 @@ SCRIPTED_TESTS += grub_script_blanklines
SCRIPTED_TESTS += grub_script_final_semicolon
SCRIPTED_TESTS += grub_script_dollar
SCRIPTED_TESTS += grub_script_comments
SCRIPTED_TESTS += grub_script_functions
# dependencies between tests and testing-tools
$(SCRIPTED_TESTS): grub-shell grub-shell-tester

View file

@ -71,9 +71,9 @@ delete_loopback (const char *name)
/* The command to add and remove loopback devices. */
static grub_err_t
grub_cmd_loopback (grub_extcmd_t cmd, int argc, char **args)
grub_cmd_loopback (grub_extcmd_context_t ctxt, int argc, char **args)
{
struct grub_arg_list *state = state = cmd->state;
struct grub_arg_list *state = ctxt->state;
grub_file_t file;
struct grub_loopback *newdev;
grub_err_t ret;

View file

@ -27,7 +27,7 @@
#include <grub/i18n.h>
static grub_err_t
grub_cmd_hello (struct grub_extcmd *cmd __attribute__ ((unused)),
grub_cmd_hello (grub_extcmd_context_t ctxt __attribute__ ((unused)),
int argc __attribute__ ((unused)),
char **args __attribute__ ((unused)))
{

View file

@ -37,6 +37,8 @@
#define GRUB_COMMAND_FLAG_EXTCMD 0x10
/* This is an dynamic command. */
#define GRUB_COMMAND_FLAG_DYNCMD 0x20
/* This command accepts block arguments. */
#define GRUB_COMMAND_FLAG_BLOCKS 0x40
struct grub_command;

View file

@ -21,10 +21,12 @@
#include <grub/lib/arg.h>
#include <grub/command.h>
#include <grub/script_sh.h>
struct grub_extcmd;
struct grub_extcmd_context;
typedef grub_err_t (*grub_extcmd_func_t) (struct grub_extcmd *cmd,
typedef grub_err_t (*grub_extcmd_func_t) (struct grub_extcmd_context *ctxt,
int argc, char **args);
/* The argcmd description. */
@ -38,11 +40,21 @@ struct grub_extcmd
const struct grub_arg_option *options;
void *data;
struct grub_arg_list *state;
};
typedef struct grub_extcmd *grub_extcmd_t;
/* Command context for each instance of execution. */
struct grub_extcmd_context
{
struct grub_extcmd *extcmd;
struct grub_arg_list *state;
/* Script parameter, if any. */
struct grub_script *script;
};
typedef struct grub_extcmd_context *grub_extcmd_context_t;
grub_extcmd_t grub_register_extcmd (const char *name,
grub_extcmd_func_t func,
unsigned flags,
@ -50,6 +62,18 @@ grub_extcmd_t grub_register_extcmd (const char *name,
const char *description,
const struct grub_arg_option *parser);
grub_extcmd_t grub_register_extcmd_prio (const char *name,
grub_extcmd_func_t func,
unsigned flags,
const char *summary,
const char *description,
const struct grub_arg_option *parser,
int prio);
void grub_unregister_extcmd (grub_extcmd_t cmd);
grub_err_t
grub_extcmd_dispatcher (struct grub_command *cmd, int argc, char **args,
struct grub_script *script);
#endif /* ! GRUB_EXTCMD_HEADER */

View file

@ -38,6 +38,8 @@ typedef enum grub_arg_type grub_arg_type_t;
/* Flags for the option field op grub_arg_option. */
#define GRUB_ARG_OPTION_OPTIONAL (1 << 1)
/* Flags for an option that can appear multiple times. */
#define GRUB_ARG_OPTION_REPEATABLE (1 << 2)
enum grub_key_type
{
@ -59,7 +61,10 @@ struct grub_arg_option
struct grub_arg_list
{
int set;
char *arg;
union {
char *arg;
char **args;
};
};
struct grub_extcmd;
@ -68,5 +73,7 @@ int grub_arg_parse (struct grub_extcmd *cmd, int argc, char **argv,
struct grub_arg_list *usr, char ***args, int *argnum);
void grub_arg_show_help (struct grub_extcmd *cmd);
struct grub_arg_list* grub_arg_list_alloc (struct grub_extcmd *cmd,
int argc, char *argv[]);
#endif /* ! GRUB_ARG_HEADER */

View file

@ -47,6 +47,10 @@ struct grub_menu_entry
/* The sourcecode of the menu entry, used by the editor. */
const char *sourcecode;
/* Parameters to be passed to menu definition. */
int argc;
char **args;
int hotkey;
/* The next element. */

View file

@ -54,8 +54,6 @@ void grub_normal_execute (const char *config, int nested, int batch);
void grub_menu_init_page (int nested, int edit,
struct grub_term_output *term);
void grub_normal_init_page (struct grub_term_output *term);
grub_err_t grub_normal_add_menu_entry (int argc, const char **args,
const char *sourcecode);
char *grub_file_getline (grub_file_t file);
void grub_cmdline_run (int nested);

View file

@ -39,8 +39,13 @@ struct grub_script_cmd
struct grub_script
{
unsigned refcnt;
struct grub_script_mem *mem;
struct grub_script_cmd *cmd;
/* Other grub_script's from block arguments. */
struct grub_script *siblings;
struct grub_script *children;
};
typedef enum
@ -49,7 +54,8 @@ typedef enum
GRUB_SCRIPT_ARG_TYPE_TEXT,
GRUB_SCRIPT_ARG_TYPE_DQVAR,
GRUB_SCRIPT_ARG_TYPE_DQSTR,
GRUB_SCRIPT_ARG_TYPE_SQSTR
GRUB_SCRIPT_ARG_TYPE_SQSTR,
GRUB_SCRIPT_ARG_TYPE_BLOCK
} grub_script_arg_type_t;
/* A part of an argument. */
@ -59,10 +65,21 @@ struct grub_script_arg
char *str;
/* Parsed block argument. */
struct grub_script *script;
/* Next argument part. */
struct grub_script_arg *next;
};
/* An argument vector. */
struct grub_script_argv
{
unsigned argc;
char **args;
struct grub_script *script;
};
/* A complete argument. It consists of a list of one or more `struct
grub_script_arg's. */
struct grub_script_arglist
@ -82,15 +99,6 @@ struct grub_script_cmdline
struct grub_script_arglist *arglist;
};
/* A block of commands, this can be used to group commands. */
struct grub_script_cmdblock
{
struct grub_script_cmd cmd;
/* A chain of commands. */
struct grub_script_cmd *cmdlist;
};
/* An if statement. */
struct grub_script_cmdif
{
@ -136,21 +144,6 @@ struct grub_script_cmdwhile
int until;
};
/* A menu entry generate statement. */
struct grub_script_cmd_menuentry
{
struct grub_script_cmd cmd;
/* The arguments for this menu entry. */
struct grub_script_arglist *arglist;
/* The sourcecode the entry will be generated from. */
const char *sourcecode;
/* Options. XXX: Not used yet. */
int options;
};
/* State of the lexer as passed to the lexer. */
struct grub_lexer_param
{
@ -218,12 +211,22 @@ struct grub_parser_param
/* The memory that was used while parsing and scanning. */
struct grub_script_mem *memused;
/* The block argument scripts. */
struct grub_script *scripts;
/* The result of the parser. */
struct grub_script_cmd *parsed;
struct grub_lexer_param *lexerstate;
};
void grub_script_mem_free (struct grub_script_mem *mem);
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);
int grub_script_argv_split_append (struct grub_script_argv *argv, char *s);
struct grub_script_arglist *
grub_script_create_arglist (struct grub_parser_param *state);
@ -234,8 +237,6 @@ grub_script_add_arglist (struct grub_parser_param *state,
struct grub_script_cmd *
grub_script_create_cmdline (struct grub_parser_param *state,
struct grub_script_arglist *arglist);
struct grub_script_cmd *
grub_script_create_cmdblock (struct grub_parser_param *state);
struct grub_script_cmd *
grub_script_create_cmdif (struct grub_parser_param *state,
@ -256,15 +257,9 @@ grub_script_create_cmdwhile (struct grub_parser_param *state,
int is_an_until_loop);
struct grub_script_cmd *
grub_script_create_cmdmenu (struct grub_parser_param *state,
struct grub_script_arglist *arglist,
char *sourcecode,
int options);
struct grub_script_cmd *
grub_script_add_cmd (struct grub_parser_param *state,
struct grub_script_cmdblock *cmdblock,
struct grub_script_cmd *cmd);
grub_script_append_cmd (struct grub_parser_param *state,
struct grub_script_cmd *list,
struct grub_script_cmd *last);
struct grub_script_arg *
grub_script_arg_add (struct grub_parser_param *state,
struct grub_script_arg *arg,
@ -282,8 +277,8 @@ struct grub_lexer_param *grub_script_lexer_init (struct grub_parser_param *parse
void grub_script_lexer_fini (struct grub_lexer_param *);
void grub_script_lexer_ref (struct grub_lexer_param *);
void grub_script_lexer_deref (struct grub_lexer_param *);
void grub_script_lexer_record_start (struct grub_parser_param *);
char *grub_script_lexer_record_stop (struct grub_parser_param *);
unsigned grub_script_lexer_record_start (struct grub_parser_param *);
char *grub_script_lexer_record_stop (struct grub_parser_param *, unsigned);
int grub_script_lexer_yywrap (struct grub_parser_param *);
void grub_script_lexer_record (struct grub_parser_param *, char *);
@ -301,14 +296,14 @@ void grub_script_yyerror (struct grub_parser_param *, char const *);
/* Commands to execute, don't use these directly. */
grub_err_t grub_script_execute_cmdline (struct grub_script_cmd *cmd);
grub_err_t grub_script_execute_cmdblock (struct grub_script_cmd *cmd);
grub_err_t grub_script_execute_cmdlist (struct grub_script_cmd *cmd);
grub_err_t grub_script_execute_cmdif (struct grub_script_cmd *cmd);
grub_err_t grub_script_execute_cmdfor (struct grub_script_cmd *cmd);
grub_err_t grub_script_execute_cmdwhile (struct grub_script_cmd *cmd);
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);
grub_err_t grub_script_execute_sourcecode (const char *source, int argc, char **args);
/* This variable points to the parsed command. This is used to
communicate with the bison code. */
@ -344,12 +339,33 @@ grub_script_function_t grub_script_function_create (struct grub_script_arg *func
struct grub_script *cmd);
void grub_script_function_remove (const char *name);
grub_script_function_t grub_script_function_find (char *functionname);
int grub_script_function_call (grub_script_function_t func,
int argc, char **args);
grub_err_t grub_script_function_call (grub_script_function_t func,
int argc, char **args);
char **
grub_script_execute_arglist_to_argv (struct grub_script_arglist *arglist, int *count);
static inline struct grub_script *
grub_script_get (struct grub_script *script)
{
if (script)
script->refcnt++;
return script;
}
static inline void
grub_script_put (struct grub_script *script)
{
if (! script)
return;
if (script->refcnt == 0)
grub_script_free (script);
else
script->refcnt--;
}
grub_err_t
grub_normal_parse_line (char *line, grub_reader_getline_t getline);

View file

@ -209,8 +209,16 @@ parse_option (grub_extcmd_t cmd, int key, char *arg, struct grub_arg_list *usr)
if (found == -1)
return -1;
usr[found].set = 1;
usr[found].arg = arg;
if (opt->flags & GRUB_ARG_OPTION_REPEATABLE)
{
usr[found].args[usr[found].set++] = arg;
usr[found].args[usr[found].set] = NULL;
}
else
{
usr[found].set = 1;
usr[found].arg = arg;
}
}
}
@ -230,10 +238,15 @@ grub_arg_parse (grub_extcmd_t cmd, int argc, char **argv,
grub_err_t add_arg (char *s)
{
argl = grub_realloc (argl, (++num) * sizeof (char *));
char **p = argl;
argl = grub_realloc (argl, (++num + 1) * sizeof (char *));
if (! argl)
return grub_errno;
{
grub_free (p);
return grub_errno;
}
argl[num - 1] = s;
argl[num] = NULL;
return 0;
}
@ -310,8 +323,11 @@ grub_arg_parse (grub_extcmd_t cmd, int argc, char **argv,
if (option) {
arglen = option - arg - 2;
option++;
} else
} else {
arglen = grub_strlen (arg) - 2;
if (argv[curarg + 1])
option = argv[curarg + 1][0] == '-' ? 0 : argv[++curarg];
}
opt = find_long (cmd->options, arg + 2, arglen);
if (! opt)
@ -390,3 +406,43 @@ grub_arg_parse (grub_extcmd_t cmd, int argc, char **argv,
fail:
return complete;
}
struct grub_arg_list*
grub_arg_list_alloc(grub_extcmd_t extcmd, int argc,
char **argv __attribute__((unused)))
{
int i;
char **args;
unsigned argcnt;
struct grub_arg_list *list;
const struct grub_arg_option *options;
options = extcmd->options;
if (! options)
return 0;
argcnt = 0;
for (i = 0; options[i].doc; i++)
{
if (options[i].flags & GRUB_ARG_OPTION_REPEATABLE)
argcnt += (argc + 1) / 2 + 1; /* max possible for any option */
}
list = grub_zalloc (sizeof (*list) * i + sizeof (char*) * argcnt);
if (! list)
return 0;
args = (char**) (list + i);
for (i = 0; options[i].doc; i++)
{
list[i].set = 0;
list[i].arg = 0;
if (options[i].flags & GRUB_ARG_OPTION_REPEATABLE)
{
list[i].args = args;
args += argc / 2 + 1;
}
}
return list;
}

View file

@ -946,10 +946,10 @@ grub_bsd_parse_flags (const struct grub_arg_list *state,
}
static grub_err_t
grub_cmd_freebsd (grub_extcmd_t cmd, int argc, char *argv[])
grub_cmd_freebsd (grub_extcmd_context_t ctxt, int argc, char *argv[])
{
kernel_type = KERNEL_TYPE_FREEBSD;
bootflags = grub_bsd_parse_flags (cmd->state, freebsd_flags);
bootflags = grub_bsd_parse_flags (ctxt->state, freebsd_flags);
if (grub_bsd_load (argc, argv) == GRUB_ERR_NONE)
{
@ -1009,16 +1009,16 @@ grub_cmd_freebsd (grub_extcmd_t cmd, int argc, char *argv[])
}
static grub_err_t
grub_cmd_openbsd (grub_extcmd_t cmd, int argc, char *argv[])
grub_cmd_openbsd (grub_extcmd_context_t ctxt, int argc, char *argv[])
{
grub_uint32_t bootdev;
kernel_type = KERNEL_TYPE_OPENBSD;
bootflags = grub_bsd_parse_flags (cmd->state, openbsd_flags);
bootflags = grub_bsd_parse_flags (ctxt->state, openbsd_flags);
if (cmd->state[OPENBSD_ROOT_ARG].set)
if (ctxt->state[OPENBSD_ROOT_ARG].set)
{
const char *arg = cmd->state[OPENBSD_ROOT_ARG].arg;
const char *arg = ctxt->state[OPENBSD_ROOT_ARG].arg;
int unit, part;
if (*(arg++) != 'w' || *(arg++) != 'd')
return grub_error (GRUB_ERR_BAD_ARGUMENT,
@ -1049,16 +1049,16 @@ grub_cmd_openbsd (grub_extcmd_t cmd, int argc, char *argv[])
}
static grub_err_t
grub_cmd_netbsd (grub_extcmd_t cmd, int argc, char *argv[])
grub_cmd_netbsd (grub_extcmd_context_t ctxt, int argc, char *argv[])
{
kernel_type = KERNEL_TYPE_NETBSD;
bootflags = grub_bsd_parse_flags (cmd->state, netbsd_flags);
bootflags = grub_bsd_parse_flags (ctxt->state, netbsd_flags);
if (grub_bsd_load (argc, argv) == GRUB_ERR_NONE)
{
grub_loader_set (grub_netbsd_boot, grub_bsd_unload, 1);
if (cmd->state[NETBSD_ROOT_ARG].set)
netbsd_root = grub_strdup (cmd->state[NETBSD_ROOT_ARG].arg);
if (ctxt->state[NETBSD_ROOT_ARG].set)
netbsd_root = grub_strdup (ctxt->state[NETBSD_ROOT_ARG].arg);
}
return grub_errno;

View file

@ -1368,15 +1368,15 @@ static const struct grub_arg_option xnu_splash_cmd_options[] =
};
static grub_err_t
grub_cmd_xnu_splash (grub_extcmd_t cmd,
grub_cmd_xnu_splash (grub_extcmd_context_t ctxt,
int argc, char *args[])
{
grub_err_t err;
if (argc != 1)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "file name required");
if (cmd->state[XNU_SPLASH_CMD_ARGINDEX_MODE].set &&
grub_strcmp (cmd->state[XNU_SPLASH_CMD_ARGINDEX_MODE].arg,
if (ctxt->state[XNU_SPLASH_CMD_ARGINDEX_MODE].set &&
grub_strcmp (ctxt->state[XNU_SPLASH_CMD_ARGINDEX_MODE].arg,
"stretch") == 0)
grub_xnu_bitmap_mode = GRUB_XNU_BITMAP_STRETCH;
else

View file

@ -23,16 +23,21 @@
#include <grub/misc.h>
#include <grub/command.h>
#include <grub/normal.h>
#include <grub/extcmd.h>
#include <grub/script_sh.h>
#include <grub/i18n.h>
static grub_err_t
grub_dyncmd_dispatcher (struct grub_command *cmd,
grub_dyncmd_dispatcher (struct grub_extcmd_context *ctxt,
int argc, char **args)
{
char *modname = cmd->data;
char *modname;
grub_dl_t mod;
grub_err_t ret;
grub_extcmd_t extcmd = ctxt->extcmd;
grub_command_t cmd = extcmd->cmd;
modname = extcmd->data;
mod = grub_dl_load (modname);
if (mod)
{
@ -42,11 +47,17 @@ grub_dyncmd_dispatcher (struct grub_command *cmd,
grub_dl_ref (mod);
name = (char *) cmd->name;
grub_unregister_command (cmd);
grub_unregister_extcmd (extcmd);
cmd = grub_command_find (name);
if (cmd)
ret = (cmd->func) (cmd, argc, args);
{
if (cmd->flags & GRUB_COMMAND_FLAG_BLOCKS &&
cmd->flags & GRUB_COMMAND_FLAG_EXTCMD)
ret = grub_extcmd_dispatcher (cmd, argc, args, ctxt->script);
else
ret = (cmd->func) (cmd, argc, args);
}
else
ret = grub_errno;
@ -81,13 +92,14 @@ read_command_list (const char *prefix)
for (ptr = grub_command_list; ptr; ptr = next)
{
next = ptr->next;
if (ptr->func == grub_dyncmd_dispatcher)
if (ptr->flags & GRUB_COMMAND_FLAG_DYNCMD)
{
if (last)
last->next = ptr->next;
else
grub_command_list = ptr->next;
grub_free (ptr);
grub_free (ptr->data); /* extcmd struct */
}
else
last = ptr;
@ -96,7 +108,7 @@ read_command_list (const char *prefix)
for (;; grub_free (buf))
{
char *p, *name, *modname;
grub_command_t cmd;
grub_extcmd_t cmd;
int prio = 0;
buf = grub_file_getline (file);
@ -139,16 +151,19 @@ read_command_list (const char *prefix)
continue;
}
cmd = grub_register_command_prio (name,
grub_dyncmd_dispatcher,
0, N_("not loaded"), prio);
cmd = grub_register_extcmd_prio (name,
grub_dyncmd_dispatcher,
GRUB_COMMAND_FLAG_BLOCKS
| GRUB_COMMAND_FLAG_EXTCMD
| GRUB_COMMAND_FLAG_DYNCMD,
0, N_("not loaded"), 0,
prio);
if (! cmd)
{
grub_free (name);
grub_free (modname);
continue;
}
cmd->flags |= GRUB_COMMAND_FLAG_DYNCMD;
cmd->data = modname;
/* Update the active flag. */

View file

@ -141,209 +141,6 @@ free_menu (grub_menu_t menu)
grub_env_unset_menu ();
}
static void
free_menu_entry_classes (struct grub_menu_entry_class *head)
{
/* Free all the classes. */
while (head)
{
struct grub_menu_entry_class *next;
grub_free (head->name);
next = head->next;
grub_free (head);
head = next;
}
}
static struct
{
char *name;
int key;
} hotkey_aliases[] =
{
{"backspace", '\b'},
{"tab", '\t'},
{"delete", GRUB_TERM_DC}
};
/* Add a menu entry to the current menu context (as given by the environment
variable data slot `menu'). As the configuration file is read, the script
parser calls this when a menu entry is to be created. */
grub_err_t
grub_normal_add_menu_entry (int argc, const char **args,
const char *sourcecode)
{
const char *menutitle = 0;
const char *menusourcecode;
grub_menu_t menu;
grub_menu_entry_t *last;
int failed = 0;
int i;
struct grub_menu_entry_class *classes_head; /* Dummy head node for list. */
struct grub_menu_entry_class *classes_tail;
char *users = NULL;
int hotkey = 0;
/* Allocate dummy head node for class list. */
classes_head = grub_zalloc (sizeof (struct grub_menu_entry_class));
if (! classes_head)
return grub_errno;
classes_tail = classes_head;
menu = grub_env_get_menu ();
if (! menu)
return grub_error (GRUB_ERR_MENU, "no menu context");
last = &menu->entry_list;
menusourcecode = grub_strdup (sourcecode);
if (! menusourcecode)
return grub_errno;
/* Parse menu arguments. */
for (i = 0; i < argc; i++)
{
/* Capture arguments. */
if (grub_strncmp ("--", args[i], 2) == 0 && i + 1 < argc)
{
const char *arg = &args[i][2];
/* Handle menu class. */
if (grub_strcmp(arg, "class") == 0)
{
char *class_name;
struct grub_menu_entry_class *new_class;
i++;
class_name = grub_strdup (args[i]);
if (! class_name)
{
failed = 1;
break;
}
/* Create a new class and add it at the tail of the list. */
new_class = grub_zalloc (sizeof (struct grub_menu_entry_class));
if (! new_class)
{
grub_free (class_name);
failed = 1;
break;
}
/* Fill in the new class node. */
new_class->name = class_name;
/* Link the tail to it, and make it the new tail. */
classes_tail->next = new_class;
classes_tail = new_class;
continue;
}
else if (grub_strcmp(arg, "users") == 0)
{
i++;
users = grub_strdup (args[i]);
if (! users)
{
failed = 1;
break;
}
continue;
}
else if (grub_strcmp(arg, "hotkey") == 0)
{
unsigned j;
i++;
if (args[i][1] == 0)
{
hotkey = args[i][0];
continue;
}
for (j = 0; j < ARRAY_SIZE (hotkey_aliases); j++)
if (grub_strcmp (args[i], hotkey_aliases[j].name) == 0)
{
hotkey = hotkey_aliases[j].key;
break;
}
if (j < ARRAY_SIZE (hotkey_aliases))
continue;
failed = 1;
grub_error (GRUB_ERR_MENU,
"Invalid hotkey: '%s'.", args[i]);
break;
}
else
{
/* Handle invalid argument. */
failed = 1;
grub_error (GRUB_ERR_MENU,
"invalid argument for menuentry: %s", args[i]);
break;
}
}
/* Capture title. */
if (! menutitle)
{
menutitle = grub_strdup (args[i]);
}
else
{
failed = 1;
grub_error (GRUB_ERR_MENU,
"too many titles for menuentry: %s", args[i]);
break;
}
}
/* Validate arguments. */
if ((! failed) && (! menutitle))
{
grub_error (GRUB_ERR_MENU, "menuentry is missing title");
failed = 1;
}
/* If argument parsing failed, free any allocated resources. */
if (failed)
{
free_menu_entry_classes (classes_head);
grub_free ((void *) menutitle);
grub_free ((void *) menusourcecode);
/* Here we assume that grub_error has been used to specify failure details. */
return grub_errno;
}
/* Add the menu entry at the end of the list. */
while (*last)
last = &(*last)->next;
*last = grub_zalloc (sizeof (**last));
if (! *last)
{
free_menu_entry_classes (classes_head);
grub_free ((void *) menutitle);
grub_free ((void *) menusourcecode);
return grub_errno;
}
(*last)->title = menutitle;
(*last)->hotkey = hotkey;
(*last)->classes = classes_head;
if (users)
(*last)->restricted = 1;
(*last)->users = users;
(*last)->sourcecode = menusourcecode;
menu->size++;
return GRUB_ERR_NONE;
}
static grub_menu_t
read_config_file (const char *config)
{

View file

@ -142,44 +142,6 @@ get_and_remove_first_entry_number (const char *name)
return entry;
}
static void
grub_menu_execute_entry_real (grub_menu_entry_t entry)
{
const char *source;
auto grub_err_t getline (char **line, int cont);
grub_err_t getline (char **line, int cont __attribute__ ((unused)))
{
const char *p;
if (!source)
{
*line = 0;
return 0;
}
p = grub_strchr (source, '\n');
if (p)
*line = grub_strndup (source, p - source);
else
*line = grub_strdup (source);
source = p ? p + 1 : 0;
return 0;
}
source = entry->sourcecode;
while (source)
{
char *line;
getline (&line, 0);
grub_normal_parse_line (line, getline);
grub_free (line);
}
}
/* Run a menu entry. */
void
grub_menu_execute_entry(grub_menu_entry_t entry)
@ -197,8 +159,7 @@ grub_menu_execute_entry(grub_menu_entry_t entry)
}
grub_env_set ("chosen", entry->title);
grub_menu_execute_entry_real (entry);
grub_script_execute_sourcecode (entry->sourcecode, entry->argc, entry->args);
if (grub_errno == GRUB_ERR_NONE && grub_loader_is_loaded ())
/* Implicit execution of boot, only if something is loaded. */

136
script/argv.c Normal file
View file

@ -0,0 +1,136 @@
/* argv.c - methods for constructing argument vector */
/*
* GRUB -- GRand Unified Bootloader
* 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 <http://www.gnu.org/licenses/>.
*/
#include <grub/mm.h>
#include <grub/misc.h>
#include <grub/script_sh.h>
static unsigned
round_up_exp (unsigned v)
{
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
if (sizeof (v) > 4)
v |= v >> 32;
v++;
v += (v == 0);
return v;
}
void
grub_script_argv_free (struct grub_script_argv *argv)
{
unsigned i;
if (argv->args)
{
for (i = 0; i < argv->argc; i++)
grub_free (argv->args[i]);
grub_free (argv->args);
}
if (argv->script)
grub_script_put (argv->script);
argv->argc = 0;
argv->args = 0;
argv->script = 0;
}
/* Prepare for next argc. */
int
grub_script_argv_next (struct grub_script_argv *argv)
{
char **p = argv->args;
if (argv->args && argv->argc && argv->args[argv->argc - 1] == 0)
return 0;
p = grub_realloc (p, round_up_exp ((argv->argc + 2) * sizeof (char *)));
if (! p)
return 1;
argv->argc++;
argv->args = p;
if (argv->argc == 1)
argv->args[0] = 0;
argv->args[argv->argc] = 0;
return 0;
}
/* Append `s' to the last argument. */
int
grub_script_argv_append (struct grub_script_argv *argv, const char *s)
{
int a, b;
char *p = argv->args[argv->argc - 1];
if (! s)
return 0;
a = p ? grub_strlen (p) : 0;
b = grub_strlen (s);
p = grub_realloc (p, round_up_exp ((a + b + 1) * sizeof (char)));
if (! p)
return 1;
grub_strcpy (p + a, s);
argv->args[argv->argc - 1] = p;
return 0;
}
/* Split `s' and append words as multiple arguments. */
int
grub_script_argv_split_append (struct grub_script_argv *argv, char *s)
{
char ch;
char *p;
int errors = 0;
if (! s)
return 0;
while (! errors && *s)
{
p = s;
while (*s && ! grub_isspace (*s))
s++;
ch = *s;
*s = '\0';
errors += grub_script_argv_append (argv, p);
*s = ch;
while (*s && grub_isspace (*s))
s++;
if (*s)
errors += grub_script_argv_next (argv);
}
return errors;
}

View file

@ -25,11 +25,213 @@
#include <grub/menu.h>
#include <grub/lib/arg.h>
#include <grub/normal.h>
#include <grub/extcmd.h>
/* 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)
/* Scope for grub script functions. */
struct grub_script_scope
{
struct grub_script_argv argv;
};
static struct grub_script_scope *scope = 0;
static int
grub_env_special (const char *name)
{
if (grub_isdigit (name[0]) ||
grub_strcmp (name, "#") == 0 ||
grub_strcmp (name, "*") == 0 ||
grub_strcmp (name, "@") == 0)
return 1;
return 0;
}
static char **
grub_script_env_get (const char *name, grub_script_arg_type_t type)
{
struct grub_script_argv result = { 0, 0, 0 };
if (grub_script_argv_next (&result))
goto fail;
if (! grub_env_special (name))
{
char *v = grub_env_get (name);
if (v && v[0])
{
if (type == GRUB_SCRIPT_ARG_TYPE_VAR)
{
if (grub_script_argv_split_append (&result, v))
goto fail;
}
else
if (grub_script_argv_append (&result, v))
goto fail;
}
}
else if (! scope)
{
if (grub_script_argv_append (&result, 0))
goto fail;
}
else if (grub_strcmp (name, "#") == 0)
{
char buffer[ERRNO_DIGITS_MAX + 1];
grub_snprintf (buffer, sizeof (buffer), "%u", scope->argv.argc);
if (grub_script_argv_append (&result, buffer))
goto fail;
}
else if (grub_strcmp (name, "*") == 0)
{
unsigned i;
for (i = 0; i < scope->argv.argc; i++)
if (type == GRUB_SCRIPT_ARG_TYPE_VAR)
{
if (i != 0 && grub_script_argv_next (&result))
goto fail;
if (grub_script_argv_split_append (&result, scope->argv.args[i]))
goto fail;
}
else
{
if (i != 0 && grub_script_argv_append (&result, " "))
goto fail;
if (grub_script_argv_append (&result, scope->argv.args[i]))
goto fail;
}
}
else if (grub_strcmp (name, "@") == 0)
{
unsigned i;
for (i = 0; i < scope->argv.argc; i++)
{
if (i != 0 && grub_script_argv_next (&result))
goto fail;
if (type == GRUB_SCRIPT_ARG_TYPE_VAR)
{
if (grub_script_argv_split_append (&result, scope->argv.args[i]))
goto fail;
}
else
if (grub_script_argv_append (&result, scope->argv.args[i]))
goto fail;
}
}
else
{
unsigned long num = grub_strtoul (name, 0, 10);
if (num == 0)
; /* XXX no file name, for now. */
else if (num <= scope->argv.argc)
{
if (type == GRUB_SCRIPT_ARG_TYPE_VAR)
{
if (grub_script_argv_split_append (&result,
scope->argv.args[num - 1]))
goto fail;
}
else
if (grub_script_argv_append (&result, scope->argv.args[num - 1]))
goto fail;
}
}
return result.args;
fail:
grub_script_argv_free (&result);
return 0;
}
static grub_err_t
grub_script_env_set (const char *name, const char *val)
{
if (grub_env_special (name))
return grub_error (GRUB_ERR_BAD_ARGUMENT, "bad variable name");
return grub_env_set (name, val);
}
/* Expand arguments in ARGLIST into multiple arguments. */
static int
grub_script_arglist_to_argv (struct grub_script_arglist *arglist,
struct grub_script_argv *argv)
{
int i;
char **values = 0;
struct grub_script_arg *arg = 0;
struct grub_script_argv result = { 0, 0, 0 };
for (; arglist && arglist->arg; arglist = arglist->next)
{
if (grub_script_argv_next (&result))
goto fail;
arg = arglist->arg;
while (arg)
{
switch (arg->type)
{
case GRUB_SCRIPT_ARG_TYPE_VAR:
case GRUB_SCRIPT_ARG_TYPE_DQVAR:
values = grub_script_env_get (arg->str, arg->type);
for (i = 0; values && values[i]; i++)
{
if (i != 0 && grub_script_argv_next (&result))
goto fail;
if (grub_script_argv_append (&result, values[i]))
goto fail;
}
grub_free (values);
break;
case GRUB_SCRIPT_ARG_TYPE_BLOCK:
if (grub_script_argv_append (&result, "{") ||
grub_script_argv_append (&result, arg->str) ||
grub_script_argv_append (&result, "}"))
goto fail;
result.script = grub_script_get (arg->script);
break;
case GRUB_SCRIPT_ARG_TYPE_TEXT:
if (grub_strlen (arg->str) &&
grub_script_argv_append (&result, arg->str))
goto fail;
break;
case GRUB_SCRIPT_ARG_TYPE_DQSTR:
case GRUB_SCRIPT_ARG_TYPE_SQSTR:
if (grub_script_argv_append (&result, arg->str))
goto fail;
break;
}
arg = arg->next;
}
}
if (! result.args[result.argc - 1])
result.argc--;
*argv = result;
return 0;
fail:
grub_script_argv_free (&result);
return 1;
}
static grub_err_t
grub_script_execute_cmd (struct grub_script_cmd *cmd)
{
@ -46,149 +248,80 @@ grub_script_execute_cmd (struct grub_script_cmd *cmd)
return ret;
}
#define ARG_ALLOCATION_UNIT (32 * sizeof (char))
#define ARGV_ALLOCATION_UNIT (8 * sizeof (void*))
/* Expand arguments in ARGLIST into multiple arguments. */
char **
grub_script_execute_arglist_to_argv (struct grub_script_arglist *arglist, int *count)
/* Execute a function call. */
grub_err_t
grub_script_function_call (grub_script_function_t func, int argc, char **args)
{
int i;
int oom;
int argc;
int empty;
char *ptr;
char **argv;
char *value;
struct grub_script_arg *arg;
grub_err_t ret = 0;
struct grub_script_scope *old_scope;
struct grub_script_scope new_scope;
auto void push (char *str);
void push (char *str)
new_scope.argv.argc = argc;
new_scope.argv.args = args;
old_scope = scope;
scope = &new_scope;
ret = grub_script_execute (func->func);
scope = old_scope;
return ret;
}
/* Execute a source script. */
grub_err_t
grub_script_execute_sourcecode (const char *source, int argc, char **args)
{
grub_err_t ret = 0;
struct grub_script *parsed_script;
struct grub_script_scope new_scope;
struct grub_script_scope *old_scope;
auto grub_err_t getline (char **line, int cont);
grub_err_t getline (char **line, int cont __attribute__ ((unused)))
{
char **p;
const char *p;
if (oom)
return;
p = grub_realloc (argv, ALIGN_UP (sizeof(char*) * (argc + 1), ARGV_ALLOCATION_UNIT));
if (!p)
oom = 1;
else
if (! source)
{
p[argc++] = str;
argv = p;
*line = 0;
return 0;
}
}
auto char* append (const char *str, grub_size_t nchar);
char* append (const char *str, grub_size_t nchar)
{
int len;
int old;
char *p;
if (oom || !str)
return 0;
len = nchar ?: grub_strlen (str);
old = argv[argc - 1] ? grub_strlen (argv[argc - 1]) : 0;
p = grub_realloc (argv[argc - 1], ALIGN_UP(old + len + 1, ARG_ALLOCATION_UNIT));
p = grub_strchr (source, '\n');
if (p)
{
grub_strncpy (p + old, str, len);
p[old + len] = '\0';
}
*line = grub_strndup (source, p - source);
else
{
oom = 1;
grub_free (argv[argc - 1]);
}
argv[argc - 1] = p;
return argv[argc - 1];
*line = grub_strdup (source);
source = p ? p + 1 : 0;
return 0;
}
/* Move *STR to the begining of next word, but return current word. */
auto char* move_to_next (char **str);
char* move_to_next (char **str)
{
char *end;
char *start;
new_scope.argv.argc = argc;
new_scope.argv.args = args;
if (oom || !str || !*str)
return 0;
old_scope = scope;
scope = &new_scope;
start = *str;
while (*start && grub_isspace (*start)) start++;
if (*start == '\0')
return 0;
end = start + 1;
while (*end && !grub_isspace (*end)) end++;
*str = end;
return start;
}
oom = 0;
argv = 0;
argc = 0;
push (0);
for (; arglist; arglist = arglist->next)
while (source)
{
empty = 1;
arg = arglist->arg;
while (arg)
char *line;
getline (&line, 0);
parsed_script = grub_script_parse (line, getline);
if (! parsed_script)
{
switch (arg->type)
{
case GRUB_SCRIPT_ARG_TYPE_VAR:
value = grub_env_get (arg->str);
while (value && *value && (ptr = move_to_next(&value)))
{
empty = 0;
append (ptr, value - ptr);
if (*value) push(0);
}
break;
case GRUB_SCRIPT_ARG_TYPE_TEXT:
if (grub_strlen (arg->str) > 0)
{
empty = 0;
append (arg->str, 0);
}
break;
case GRUB_SCRIPT_ARG_TYPE_DQSTR:
case GRUB_SCRIPT_ARG_TYPE_SQSTR:
empty = 0;
append (arg->str, 0);
break;
case GRUB_SCRIPT_ARG_TYPE_DQVAR:
empty = 0;
append (grub_env_get (arg->str), 0);
break;
}
arg = arg->next;
ret = grub_errno;
break;
}
if (!empty)
push (0);
ret = grub_script_execute (parsed_script);
grub_free (line);
}
if (oom)
{
for (i = 0; i < argc; i++)
grub_free (argv[i]);
grub_free (argv);
argv = 0;
}
if (argv)
*count = argc - 1;
return argv;
scope = old_scope;
return ret;
}
/* Execute a single command line. */
@ -196,21 +329,18 @@ grub_err_t
grub_script_execute_cmdline (struct grub_script_cmd *cmd)
{
struct grub_script_cmdline *cmdline = (struct grub_script_cmdline *) cmd;
char **args = 0;
int i = 0;
grub_command_t grubcmd;
grub_err_t ret = 0;
int argcount = 0;
grub_script_function_t func = 0;
char errnobuf[18];
char *cmdname;
struct grub_script_argv argv = { 0, 0, 0 };
/* Lookup the command. */
args = grub_script_execute_arglist_to_argv (cmdline->arglist, &argcount);
if (! args || ! args[0])
if (grub_script_arglist_to_argv (cmdline->arglist, &argv))
return grub_errno;
cmdname = args[0];
cmdname = argv.args[0];
grubcmd = grub_command_find (cmdname);
if (! grubcmd)
{
@ -232,13 +362,14 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd)
/* Create two strings and set the variable. */
*eq = '\0';
eq++;
grub_env_set (assign, eq);
grub_script_env_set (assign, eq);
}
grub_free (assign);
grub_snprintf (errnobuf, sizeof (errnobuf), "%d", grub_errno);
grub_env_set ("?", errnobuf);
grub_script_argv_free (&argv);
grub_print_error ();
return 0;
@ -247,14 +378,19 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd)
/* Execute the GRUB command or function. */
if (grubcmd)
ret = (grubcmd->func) (grubcmd, argcount - 1, args + 1);
{
if ((grubcmd->flags & GRUB_COMMAND_FLAG_BLOCKS) &&
(grubcmd->flags & GRUB_COMMAND_FLAG_EXTCMD))
ret = grub_extcmd_dispatcher (grubcmd, argv.argc - 1, argv.args + 1,
argv.script);
else
ret = (grubcmd->func) (grubcmd, argv.argc - 1, argv.args + 1);
}
else
ret = grub_script_function_call (func, argcount - 1, args + 1);
ret = grub_script_function_call (func, argv.argc - 1, argv.args + 1);
/* Free arguments. */
for (i = 0; i < argcount; i++)
grub_free (args[i]);
grub_free (args);
grub_script_argv_free (&argv);
if (grub_errno == GRUB_ERR_TEST_FAILURE)
grub_errno = GRUB_ERR_NONE;
@ -269,13 +405,13 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd)
/* Execute a block of one or more commands. */
grub_err_t
grub_script_execute_cmdblock (struct grub_script_cmd *cmd)
grub_script_execute_cmdlist (struct grub_script_cmd *list)
{
int ret = 0;
struct grub_script_cmdblock *cmdblock = (struct grub_script_cmdblock *) cmd;
struct grub_script_cmd *cmd;
/* Loop over every command and execute it. */
for (cmd = cmdblock->cmdlist; cmd; cmd = cmd->next)
for (cmd = list->next; cmd; cmd = cmd->next)
ret = grub_script_execute_cmd (cmd);
return ret;
@ -307,25 +443,22 @@ grub_script_execute_cmdif (struct grub_script_cmd *cmd)
grub_err_t
grub_script_execute_cmdfor (struct grub_script_cmd *cmd)
{
int i;
int result;
char **args;
int argcount;
unsigned i;
grub_err_t result;
struct grub_script_argv argv = { 0, 0, 0 };
struct grub_script_cmdfor *cmdfor = (struct grub_script_cmdfor *) cmd;
args = grub_script_execute_arglist_to_argv (cmdfor->words, &argcount);
if (!args)
if (grub_script_arglist_to_argv (cmdfor->words, &argv))
return grub_errno;
result = 0;
for (i = 0; i < argcount; i++)
for (i = 0; i < argv.argc; i++)
{
grub_env_set (cmdfor->name->str, args[i]);
grub_script_env_set (cmdfor->name->str, argv.args[i]);
result = grub_script_execute_cmd (cmdfor->list);
grub_free (args[i]);
}
grub_free (args);
grub_script_argv_free (&argv);
return result;
}
@ -349,37 +482,6 @@ grub_script_execute_cmdwhile (struct grub_script_cmd *cmd)
return result;
}
/* Execute the menu entry generate statement. */
grub_err_t
grub_script_execute_menuentry (struct grub_script_cmd *cmd)
{
struct grub_script_cmd_menuentry *cmd_menuentry;
char **args = 0;
int argcount = 0;
int i = 0;
cmd_menuentry = (struct grub_script_cmd_menuentry *) cmd;
if (cmd_menuentry->arglist)
{
args = grub_script_execute_arglist_to_argv (cmd_menuentry->arglist, &argcount);
if (!args)
return grub_errno;
}
grub_normal_add_menu_entry (argcount, (const char **) args,
cmd_menuentry->sourcecode);
/* Free arguments. */
for (i = 0; i < argcount; i++)
grub_free (args[i]);
grub_free (args);
return grub_errno;
}
/* Execute any GRUB pre-parsed command or script. */
grub_err_t
grub_script_execute (struct grub_script *script)

View file

@ -103,12 +103,3 @@ grub_script_function_find (char *functionname)
return func;
}
int
grub_script_function_call (grub_script_function_t func,
int argc __attribute__((unused)),
char **args __attribute__((unused)))
{
/* XXX: Arguments are not supported yet. */
return grub_script_execute (func->func);
}

View file

@ -38,68 +38,57 @@ grub_script_lexer_deref (struct grub_lexer_param *state)
}
/* Start recording all characters passing through the lexer. */
void
unsigned
grub_script_lexer_record_start (struct grub_parser_param *parser)
{
struct grub_lexer_param *lexer = parser->lexerstate;
lexer->record = 1;
lexer->recordpos = 0;
if (lexer->recording) /* reuse last record */
return;
lexer->record++;
if (lexer->recording)
return lexer->recordpos;
lexer->recordpos = 0;
lexer->recordlen = GRUB_LEXER_INITIAL_RECORD_SIZE;
lexer->recording = grub_malloc (lexer->recordlen);
if (!lexer->recording)
{
grub_script_yyerror (parser, 0);
lexer->record = 0;
lexer->recordlen = 0;
}
return lexer->recordpos;
}
char *
grub_script_lexer_record_stop (struct grub_parser_param *parser)
grub_script_lexer_record_stop (struct grub_parser_param *parser, unsigned offset)
{
char *ptr;
int count;
char *result;
struct grub_lexer_param *lexer = parser->lexerstate;
auto char *compact (char *start, char *end);
char *compact (char *start, char *end)
{
/* Delete '{' and '}' characters and whitespaces. */
while (*start && grub_isspace (*start)) start++;
if (*start == '{') start++;
while (*start && grub_isspace (*start)) start++;
while (*end && grub_isspace (*end)) end--;
if (*end == '}') end--;
while (*end && grub_isspace (*end)) end--;
end[1] = '\0';
return start;
}
if (!lexer->record || !lexer->recording)
if (!lexer->record)
return 0;
/* XXX This is not necessary in BASH. */
lexer->record--;
if (!lexer->recording)
return 0;
ptr = compact (lexer->recording, lexer->recording + lexer->recordpos - 1);
lexer->record = 0;
lexer->recordpos = 0;
/* This memory would be freed by, grub_script_free. */
result = grub_script_malloc (parser, grub_strlen (ptr) + 1);
if (result)
grub_strcpy (result, ptr);
count = lexer->recordpos - offset;
result = grub_script_malloc (parser, count + 1);
if (result) {
grub_strncpy (result, lexer->recording + offset, count);
result[count] = '\0';
}
if (lexer->record == 0)
{
grub_free (lexer->recording);
lexer->recording = 0;
lexer->recordlen = 0;
lexer->recordpos = 0;
}
return result;
}
#define MAX(a,b) ((a) < (b) ? (b) : (a))
/* Record STR if input recording is enabled. */
void
grub_script_lexer_record (struct grub_parser_param *parser, char *str)
@ -108,21 +97,20 @@ grub_script_lexer_record (struct grub_parser_param *parser, char *str)
char *old;
struct grub_lexer_param *lexer = parser->lexerstate;
if (!lexer->record)
if (!lexer->record || !lexer->recording)
return;
len = grub_strlen (str);
if (lexer->recordpos + len + 1 > lexer->recordlen)
{
old = lexer->recording;
lexer->recordlen = MAX (len, lexer->recordlen) * 2;
lexer->recordlen = grub_max (len, lexer->recordlen) * 2;
lexer->recording = grub_realloc (lexer->recording, lexer->recordlen);
if (!lexer->recording)
{
grub_free (old);
lexer->record = 0;
lexer->recordpos = 0;
lexer->recordlen /= 2;
lexer->recordlen = 0;
grub_script_yyerror (parser, 0);
return;
}

View file

@ -34,7 +34,7 @@ grub_normal_parse_line (char *line, grub_reader_getline_t getline)
grub_script_execute (parsed_script);
/* The parsed script was executed, throw it away. */
grub_script_free (parsed_script);
grub_script_put (parsed_script);
}
return grub_errno;

View file

@ -20,6 +20,7 @@
%{
#include <grub/script_sh.h>
#include <grub/mm.h>
#include <grub/misc.h>
#define YYFREE grub_free
#define YYMALLOC grub_malloc
@ -34,6 +35,11 @@
struct grub_script_arglist *arglist;
struct grub_script_arg *arg;
char *string;
struct {
unsigned offset;
struct grub_script_mem *memory;
struct grub_script *scripts;
};
}
%token GRUB_PARSER_TOKEN_BAD
@ -70,15 +76,15 @@
%token <arg> GRUB_PARSER_TOKEN_WHILE "while"
%token <arg> GRUB_PARSER_TOKEN_TIME "time"
%token <arg> GRUB_PARSER_TOKEN_FUNCTION "function"
%token <arg> GRUB_PARSER_TOKEN_MENUENTRY "menuentry"
%token <arg> GRUB_PARSER_TOKEN_NAME "name"
%token <arg> GRUB_PARSER_TOKEN_WORD "word"
%type <arg> block block0
%type <arglist> word argument arguments0 arguments1
%type <cmd> script_init script
%type <cmd> grubcmd ifclause ifcmd forcmd whilecmd untilcmd
%type <cmd> command commands1 menuentry statement
%type <cmd> command commands1 statement
%pure-parser
%lex-param { struct grub_parser_param *state };
@ -97,9 +103,7 @@ script: newlines0
}
| script statement delimiter newlines0
{
struct grub_script_cmdblock *cmdblock;
cmdblock = (struct grub_script_cmdblock *) $1;
$$ = grub_script_add_cmd (state, cmdblock, $2);
$$ = grub_script_append_cmd (state, $1, $2);
}
| error
{
@ -126,7 +130,6 @@ word: GRUB_PARSER_TOKEN_NAME { $$ = grub_script_add_arglist (state, 0, $1); }
statement: command { $$ = $1; }
| function { $$ = 0; }
| menuentry { $$ = $1; }
;
argument : "case" { $$ = grub_script_add_arglist (state, 0, $1); }
@ -144,10 +147,77 @@ argument : "case" { $$ = grub_script_add_arglist (state, 0, $1); }
| "until" { $$ = grub_script_add_arglist (state, 0, $1); }
| "while" { $$ = grub_script_add_arglist (state, 0, $1); }
| "function" { $$ = grub_script_add_arglist (state, 0, $1); }
| "menuentry" { $$ = grub_script_add_arglist (state, 0, $1); }
| word { $$ = $1; }
;
/*
Block parameter is passed to commands in two forms: as unparsed
string and as pre-parsed grub_script object. Passing as grub_script
object makes memory management difficult, because:
(1) Command may want to keep a reference to grub_script objects for
later use, so script framework may not free the grub_script
object after command completes.
(2) Command may get called multiple times with same grub_script
object under loops, so we should not let command implementation
to free the grub_script object.
To solve above problems, we rely on reference counting for
grub_script objects. Commands that want to keep the grub_script
object must take a reference to it.
Other complexity comes with arbitrary nesting of grub_script
objects: a grub_script object may have commands with several block
parameters, and each block parameter may further contain multiple
block parameters nested. We use temporary variable, state->scripts
to collect nested child scripts (that are linked by siblings and
children members), and will build grub_scripts tree from bottom.
*/
block: "{"
{
grub_script_lexer_ref (state->lexerstate);
$<offset>$ = grub_script_lexer_record_start (state);
$<memory>$ = grub_script_mem_record (state);
/* save currently known scripts. */
$<scripts>$ = state->scripts;
state->scripts = 0;
}
commands1 delimiters0 "}"
{
char *p;
struct grub_script_mem *memory;
struct grub_script *s = $<scripts>2;
memory = grub_script_mem_record_stop (state, $<memory>2);
if ((p = grub_script_lexer_record_stop (state, $<offset>2)))
*grub_strrchr (p, '}') = '\0';
$$ = grub_script_arg_add (state, 0, GRUB_SCRIPT_ARG_TYPE_BLOCK, p);
if (! $$ || ! ($$->script = grub_script_create ($3, memory)))
grub_script_mem_free (memory);
else {
/* attach nested scripts to $$->script as children */
$$->script->children = state->scripts;
/* restore old scripts; append $$->script to siblings. */
state->scripts = $<scripts>2 ?: $$->script;
if (s) {
while (s->siblings)
s = s->siblings;
s->siblings = $$->script;
}
}
grub_script_lexer_deref (state->lexerstate);
}
;
block0: /* Empty */ { $$ = 0; }
| block { $$ = $1; }
;
arguments0: /* Empty */ { $$ = 0; }
| arguments1 { $$ = $1; }
;
@ -163,12 +233,17 @@ arguments1: argument arguments0
}
;
grubcmd: word arguments0
grubcmd: word arguments0 block0
{
if ($1 && $2) {
$1->next = $2;
$1->argcount += $2->argcount;
$2->argcount = 0;
struct grub_script_arglist *x = $2;
if ($3)
x = grub_script_add_arglist (state, $2, $3);
if ($1 && x) {
$1->next = x;
$1->argcount += x->argcount;
x->argcount = 0;
}
$$ = grub_script_create_cmdline (state, $1);
}
@ -185,20 +260,21 @@ command: grubcmd { $$ = $1; }
/* A list of commands. */
commands1: newlines0 command
{
$$ = grub_script_add_cmd (state, 0, $2);
$$ = grub_script_append_cmd (state, 0, $2);
}
| commands1 delimiters1 command
{
struct grub_script_cmdblock *cmdblock;
cmdblock = (struct grub_script_cmdblock *) $1;
$$ = grub_script_add_cmd (state, cmdblock, $3);
$$ = grub_script_append_cmd (state, $1, $3);
}
;
function: "function" "name"
function: "function" "name"
{
grub_script_lexer_ref (state->lexerstate);
state->func_mem = grub_script_mem_record (state);
$<scripts>$ = state->scripts;
state->scripts = 0;
}
delimiters0 "{" commands1 delimiters1 "}"
{
@ -206,30 +282,18 @@ function: "function" "name"
state->func_mem = grub_script_mem_record_stop (state,
state->func_mem);
script = grub_script_create ($6, state->func_mem);
if (script)
grub_script_function_create ($2, script);
if (! script)
grub_script_mem_free (state->func_mem);
else {
script->children = state->scripts;
grub_script_function_create ($2, script);
}
state->scripts = $<scripts>3;
grub_script_lexer_deref (state->lexerstate);
}
;
menuentry: "menuentry"
{
grub_script_lexer_ref (state->lexerstate);
}
arguments1
{
grub_script_lexer_record_start (state);
}
delimiters0 "{" commands1 delimiters1 "}"
{
char *menu_entry;
menu_entry = grub_script_lexer_record_stop (state);
grub_script_lexer_deref (state->lexerstate);
$$ = grub_script_create_cmdmenu (state, $3, menu_entry, 0);
}
;
ifcmd: "if"
{
grub_script_lexer_ref (state->lexerstate);

View file

@ -54,7 +54,7 @@ grub_script_malloc (struct grub_parser_param *state, grub_size_t size)
}
/* Free all memory described by MEM. */
static void
void
grub_script_mem_free (struct grub_script_mem *mem)
{
struct grub_script_mem *memfree;
@ -94,9 +94,21 @@ grub_script_mem_record_stop (struct grub_parser_param *state,
void
grub_script_free (struct grub_script *script)
{
if (!script)
struct grub_script *s;
struct grub_script *t;
if (! script)
return;
grub_script_mem_free (script->mem);
if (script->mem)
grub_script_mem_free (script->mem);
s = script->children;
while (s) {
t = s->siblings;
grub_script_put (s);
s = t;
}
grub_free (script);
}
@ -119,6 +131,8 @@ grub_script_arg_add (struct grub_parser_param *state,
return arg;
argpart->type = type;
argpart->script = 0;
len = grub_strlen (str) + 1;
argpart->str = grub_script_malloc (state, len);
if (!argpart->str)
@ -267,70 +281,40 @@ grub_script_create_cmdwhile (struct grub_parser_param *state,
return (struct grub_script_cmd *) cmd;
}
/* Create a command that adds a menu entry to the menu. Title is an
argument that is parsed to generate a string that can be used as
the title. The sourcecode for this entry is passed in SOURCECODE.
The options for this entry are passed in OPTIONS. */
/* Create a chain of commands. LAST contains the command that should
be added at the end of LIST's list. If LIST is zero, a new list
will be created. */
struct grub_script_cmd *
grub_script_create_cmdmenu (struct grub_parser_param *state,
struct grub_script_arglist *arglist,
char *sourcecode, int options)
{
struct grub_script_cmd_menuentry *cmd;
cmd = grub_script_malloc (state, sizeof (*cmd));
if (!cmd)
return 0;
cmd->cmd.exec = grub_script_execute_menuentry;
cmd->cmd.next = 0;
cmd->sourcecode = sourcecode;
cmd->arglist = arglist;
cmd->options = options;
return (struct grub_script_cmd *) cmd;
}
/* Create a block of commands. CMD contains the command that should
be added at the end of CMDBLOCK's list. If CMDBLOCK is zero, a new
cmdblock will be created. */
struct grub_script_cmd *
grub_script_add_cmd (struct grub_parser_param *state,
struct grub_script_cmdblock *cmdblock,
struct grub_script_cmd *cmd)
grub_script_append_cmd (struct grub_parser_param *state,
struct grub_script_cmd *list,
struct grub_script_cmd *last)
{
struct grub_script_cmd *ptr;
grub_dprintf ("scripting", "cmdblock\n");
grub_dprintf ("scripting", "append command\n");
if (!cmd)
return (struct grub_script_cmd *) cmdblock;
if (! last)
return list;
if (!cmdblock)
if (! list)
{
cmdblock = grub_script_malloc (state, sizeof (*cmdblock));
if (!cmdblock)
list = grub_script_malloc (state, sizeof (*list));
if (! list)
return 0;
cmdblock->cmd.exec = grub_script_execute_cmdblock;
cmdblock->cmd.next = 0;
cmdblock->cmdlist = cmd;
cmd->next = 0;
list->exec = grub_script_execute_cmdlist;
list->next = last;
}
else
{
if (!cmdblock->cmdlist)
cmdblock->cmdlist = cmd;
else
{
ptr = cmdblock->cmdlist;
while (ptr->next)
ptr = ptr->next;
ptr->next = cmd;
}
ptr = list;
while (ptr->next)
ptr = ptr->next;
ptr->next = last;
}
return (struct grub_script_cmd *) cmdblock;
return list;
}
@ -341,16 +325,14 @@ grub_script_create (struct grub_script_cmd *cmd, struct grub_script_mem *mem)
struct grub_script *parsed;
parsed = grub_malloc (sizeof (*parsed));
if (!parsed)
{
grub_script_mem_free (mem);
grub_free (cmd);
return 0;
}
if (! parsed)
return 0;
parsed->mem = mem;
parsed->cmd = cmd;
parsed->refcnt = 0;
parsed->siblings = 0;
parsed->children = 0;
return parsed;
}
@ -365,7 +347,7 @@ grub_script_parse (char *script, grub_reader_getline_t getline)
struct grub_lexer_param *lexstate;
struct grub_parser_param *parsestate;
parsed = grub_malloc (sizeof (*parsed));
parsed = grub_script_create (0, 0);
if (!parsed)
return 0;
@ -399,6 +381,7 @@ grub_script_parse (char *script, grub_reader_getline_t getline)
parsed->mem = grub_script_mem_record_stop (parsestate, membackup);
parsed->cmd = parsestate->parsed;
parsed->children = parsestate->scripts;
grub_script_lexer_fini (lexstate);
grub_free (parsestate);

View file

@ -119,7 +119,8 @@ DIGITS [[:digit:]]+
NAME [[:alpha:]_][[:alnum:]_]*
ESC \\.
VARIABLE ${NAME}|$\{{NAME}\}|${DIGITS}|$\{{DIGITS}\}|$\?|$\{\?\}
SPECIAL \?|\#|\*|\@
VARIABLE ${NAME}|$\{{NAME}\}|${DIGITS}|$\{{DIGITS}\}|${SPECIAL}|$\{{SPECIAL}\}
DQSTR \"([^\\\"]|{ESC})*\"
SQSTR \'[^\']*\'
WORD ({CHAR}|{DQSTR}|{SQSTR}|{ESC}|{VARIABLE})+
@ -168,7 +169,6 @@ WORD ({CHAR}|{DQSTR}|{SQSTR}|{ESC}|{VARIABLE})+
"until" { RECORD; return GRUB_PARSER_TOKEN_UNTIL; }
"while" { RECORD; return GRUB_PARSER_TOKEN_WHILE; }
"function" { RECORD; return GRUB_PARSER_TOKEN_FUNCTION; }
"menuentry" { RECORD; return GRUB_PARSER_TOKEN_MENUENTRY; }
{NAME} { RECORD; return GRUB_PARSER_TOKEN_NAME; }
{WORD} {
@ -221,7 +221,7 @@ WORD ({CHAR}|{DQSTR}|{SQSTR}|{ESC}|{VARIABLE})+
}
<VAR>{
\? |
{SPECIAL} |
{DIGITS} |
{NAME} {
COPY (yytext, yyleng);
@ -231,7 +231,7 @@ WORD ({CHAR}|{DQSTR}|{SQSTR}|{ESC}|{VARIABLE})+
else
ARG (GRUB_SCRIPT_ARG_TYPE_DQVAR);
}
\{\?\} |
\{{SPECIAL}\} |
\{{DIGITS}\} |
\{{NAME}\} {
yytext[yyleng - 1] = '\0';

View file

@ -1111,11 +1111,10 @@ static const struct grub_arg_option background_image_cmd_options[] =
};
static grub_err_t
grub_gfxterm_background_image_cmd (grub_extcmd_t cmd __attribute__ ((unused)),
int argc,
char **args)
grub_gfxterm_background_image_cmd (grub_extcmd_context_t ctxt,
int argc, char **args)
{
struct grub_arg_list *state = cmd->state;
struct grub_arg_list *state = ctxt->state;
/* Check that we have video adapter active. */
if (grub_video_get_info(NULL) != GRUB_ERR_NONE)

View file

@ -236,11 +236,11 @@ static struct grub_term_output grub_serial_term_output =
static grub_err_t
grub_cmd_serial (grub_extcmd_t cmd,
grub_cmd_serial (grub_extcmd_context_t ctxt,
int argc __attribute__ ((unused)),
char **args __attribute__ ((unused)))
{
struct grub_arg_list *state = cmd->state;
struct grub_arg_list *state = ctxt->state;
struct serial_port backup_settings = serial_settings;
grub_err_t hwiniterr;

View file

@ -16,6 +16,33 @@
# You should have received a copy of the GNU General Public License
# along with GRUB. If not, see <http://www.gnu.org/licenses/>.
# simple arguments
echo one two three
echo "one two three"
echo 'one two three'
# empty arguments
echo a "" b
echo a '' b
echo a $foo b
echo a ${foo} b
echo a "$foo" b
echo a "${foo}" b
# multi-part arguments
echo one"two"three
echo one${two}three
echo one"two"$three
echo one'two'three
echo one${two}three
echo one'two'$three
echo one'two'three"four"five${six}seven$eight
foo=bar
echo $foo ${foo}
echo "$foo" "${foo}"

View file

@ -0,0 +1,147 @@
#! @builddir@/grub-shell-tester
# Run GRUB script in a Qemu instance
# 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 <http://www.gnu.org/licenses/>.
echo parameter count
function fcount {
echo fcount "$#"
}
fcount
fcount a
fcount a b
echo parameter count, with nesting
function ffcount {
echo ffcount "$#"
fcount
fcount a
fcount a b
}
ffcount
ffcount 1
ffcount 1 2
echo parameters
function fparam {
echo fparam 1 $1
echo fparam 2 $2
echo fparam 3 $3
}
fparam
fparam a
fparam a b
echo parameters, with nesting
function ffparam {
echo ffparam 1 $1
echo ffparam 2 $2
echo ffparam 3 $3
fparam
fparam a
fparam a b
}
ffparam
ffparam 1
ffparam 1 2
echo parameter expansion with specials
function fstar {
for f in $*
do
echo fstar $f
done
for f in aaa$*bbb
do
echo fstar $f
done
}
fstar
fstar a
fstar a "1 2"
fstar a "1 2" b
function fdqstar {
for f in "$*"
do
echo fdqstar $f
done
for f in aaa"$*"bbb
do
echo fdqstar $f
done
for f in "aaa$*bbb"
do
echo fdqstar $f
done
}
fdqstar
fdqstar a
fdqstar a "1 2"
fdqstar a "1 2" b
function fat {
for f in $@
do
echo fat $f
done
for f in aaa$@bbb
do
echo fat $f
done
}
fat
fat a
fat a "1 2"
fat a "1 2" b
fat a "1 2" b "c d"
fat a "1 2" b "c d" e
function fdqat {
for f in "$@"
do
echo fdqat $f
done
for f in aaa"$@"bbb
do
echo fdqat $f
done
for f in "aaa$@bbb"
do
echo fdqat $f
done
}
# fdqat # this case needs special handling, lets ignore till we really need it.
fdqat a
fdqat a "1 2"
fdqat a "1 2" b
fdqat a "1 2" b "c d"
fdqat a "1 2" b "c d" e

View file

@ -28,7 +28,7 @@ foo=foo
echo "" $foo
echo $bar $foo
bar=""
echo $bar $foo

View file

@ -22,7 +22,7 @@
#include <grub/test.h>
static grub_err_t
grub_functional_test (struct grub_extcmd *cmd __attribute__ ((unused)),
grub_functional_test (grub_extcmd_context_t ctxt __attribute__ ((unused)),
int argc __attribute__ ((unused)),
char **args __attribute__ ((unused)))
{

View file

@ -70,7 +70,7 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd __attribute__ ((unused)
}
grub_err_t
grub_script_execute_cmdblock (struct grub_script_cmd *cmd __attribute__ ((unused)))
grub_script_execute_cmdlist (struct grub_script_cmd *cmd __attribute__ ((unused)))
{
return 0;
}
@ -93,12 +93,6 @@ grub_script_execute_cmdwhile (struct grub_script_cmd *cmd __attribute__ ((unused
return 0;
}
grub_err_t
grub_script_execute_menuentry (struct grub_script_cmd *cmd __attribute__ ((unused)))
{
return 0;
}
grub_err_t
grub_script_execute (struct grub_script *script)
{