update dynamic cmd dispatch with scripts support

This commit is contained in:
BVK Chaitanya 2010-07-18 20:46:21 +05:30
parent d18b05c4e3
commit f6e59c46e1
3 changed files with 50 additions and 15 deletions

View file

@ -68,10 +68,11 @@ grub_extcmd_dispatch (struct grub_command *cmd, int argc, char **args)
} }
grub_extcmd_t grub_extcmd_t
grub_register_extcmd (const char *name, grub_extcmd_func_t func, grub_register_extcmd_prio (const char *name, grub_extcmd_func_t func,
unsigned flags, const char *summary, unsigned flags, const char *summary,
const char *description, const char *description,
const struct grub_arg_option *parser) const struct grub_arg_option *parser,
int prio)
{ {
grub_extcmd_t ext; grub_extcmd_t ext;
grub_command_t cmd; grub_command_t cmd;
@ -81,7 +82,7 @@ grub_register_extcmd (const char *name, grub_extcmd_func_t func,
return 0; return 0;
cmd = grub_register_command_prio (name, grub_extcmd_dispatch, cmd = grub_register_command_prio (name, grub_extcmd_dispatch,
summary, description, 1); summary, description, prio);
if (! cmd) if (! cmd)
{ {
grub_free (ext); grub_free (ext);
@ -99,6 +100,16 @@ grub_register_extcmd (const char *name, grub_extcmd_func_t func,
return ext; 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 void
grub_unregister_extcmd (grub_extcmd_t ext) grub_unregister_extcmd (grub_extcmd_t ext)
{ {

View file

@ -62,6 +62,14 @@ grub_extcmd_t grub_register_extcmd (const char *name,
const char *description, const char *description,
const struct grub_arg_option *parser); 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); void grub_unregister_extcmd (grub_extcmd_t cmd);
grub_err_t grub_err_t

View file

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