2N/A/* extcmd.c - support extended command */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2009 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/mm.h>
2N/A#include <grub/list.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/extcmd.h>
2N/A#include <grub/script_sh.h>
2N/A#include <grub/dl.h>
2N/A
2N/AGRUB_MOD_LICENSE ("GPLv3+");
2N/A
2N/Agrub_err_t
2N/Agrub_extcmd_dispatcher (struct grub_command *cmd, int argc, char **args,
2N/A struct grub_script *script)
2N/A{
2N/A int new_argc;
2N/A char **new_args;
2N/A struct grub_arg_list *state;
2N/A struct grub_extcmd_context context;
2N/A grub_err_t ret;
2N/A grub_extcmd_t ext = cmd->data;
2N/A
2N/A context.state = 0;
2N/A context.extcmd = ext;
2N/A context.script = script;
2N/A
2N/A if (! ext->options)
2N/A {
2N/A ret = (ext->func) (&context, argc, args);
2N/A return ret;
2N/A }
2N/A
2N/A state = grub_arg_list_alloc (ext, argc, args);
2N/A if (grub_arg_parse (ext, argc, args, state, &new_args, &new_argc))
2N/A {
2N/A context.state = state;
2N/A ret = (ext->func) (&context, new_argc, new_args);
2N/A grub_free (new_args);
2N/A grub_free (state);
2N/A return ret;
2N/A }
2N/A
2N/A grub_free (state);
2N/A return grub_errno;
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_extcmd_dispatch (struct grub_command *cmd, int argc, char **args)
2N/A{
2N/A return grub_extcmd_dispatcher (cmd, argc, args, 0);
2N/A}
2N/A
2N/Agrub_extcmd_t
2N/Agrub_register_extcmd_prio (const char *name, grub_extcmd_func_t func,
2N/A grub_command_flags_t flags, const char *summary,
2N/A const char *description,
2N/A const struct grub_arg_option *parser,
2N/A int prio)
2N/A{
2N/A grub_extcmd_t ext;
2N/A grub_command_t cmd;
2N/A
2N/A ext = (grub_extcmd_t) grub_malloc (sizeof (*ext));
2N/A if (! ext)
2N/A return 0;
2N/A
2N/A cmd = grub_register_command_prio (name, grub_extcmd_dispatch,
2N/A summary, description, prio);
2N/A if (! cmd)
2N/A {
2N/A grub_free (ext);
2N/A return 0;
2N/A }
2N/A
2N/A cmd->flags = (flags | GRUB_COMMAND_FLAG_EXTCMD);
2N/A cmd->data = ext;
2N/A
2N/A ext->cmd = cmd;
2N/A ext->func = func;
2N/A ext->options = parser;
2N/A ext->data = 0;
2N/A
2N/A return ext;
2N/A}
2N/A
2N/Agrub_extcmd_t
2N/Agrub_register_extcmd (const char *name, grub_extcmd_func_t func,
2N/A grub_command_flags_t flags, const char *summary,
2N/A const char *description,
2N/A const struct grub_arg_option *parser)
2N/A{
2N/A return grub_register_extcmd_prio (name, func, flags,
2N/A summary, description, parser, 1);
2N/A}
2N/A
2N/Avoid
2N/Agrub_unregister_extcmd (grub_extcmd_t ext)
2N/A{
2N/A grub_unregister_command (ext->cmd);
2N/A grub_free (ext);
2N/A}