/* extcmd.c - support extended command */ /* * VAS_EBOOT -- GRand Unified Bootloader * Copyright (C) 2009 Free Software Foundation, Inc. * * VAS_EBOOT 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. * * VAS_EBOOT 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 VAS_EBOOT. If not, see . */ #include #include #include #include #include #include #include VAS_EBOOT_MOD_LICENSE ("GPLv3+"); VasEBoot_err_t VasEBoot_extcmd_dispatcher (struct VasEBoot_command *cmd, int argc, char **args, struct VasEBoot_script *script) { int new_argc; char **new_args; struct VasEBoot_arg_list *state; struct VasEBoot_extcmd_context context; VasEBoot_err_t ret; VasEBoot_extcmd_t ext = cmd->data; context.state = 0; context.extcmd = ext; context.script = script; if (! ext->options) { ret = (ext->func) (&context, argc, args); return ret; } state = VasEBoot_arg_list_alloc (ext, argc, args); if (state == NULL) return VasEBoot_errno; if (VasEBoot_arg_parse (ext, argc, args, state, &new_args, &new_argc)) { context.state = state; ret = (ext->func) (&context, new_argc, new_args); VasEBoot_free (new_args); VasEBoot_free (state); return ret; } VasEBoot_free (state); return VasEBoot_errno; } static VasEBoot_err_t VasEBoot_extcmd_dispatch (struct VasEBoot_command *cmd, int argc, char **args) { return VasEBoot_extcmd_dispatcher (cmd, argc, args, 0); } VasEBoot_extcmd_t VasEBoot_register_extcmd_prio (const char *name, VasEBoot_extcmd_func_t func, VasEBoot_command_flags_t flags, const char *summary, const char *description, const struct VasEBoot_arg_option *parser, int prio) { VasEBoot_extcmd_t ext; VasEBoot_command_t cmd; ext = (VasEBoot_extcmd_t) VasEBoot_malloc (sizeof (*ext)); if (! ext) return 0; cmd = VasEBoot_register_command_prio (name, VasEBoot_extcmd_dispatch, summary, description, prio); if (! cmd) { VasEBoot_free (ext); return 0; } cmd->flags = (flags | VAS_EBOOT_COMMAND_FLAG_EXTCMD); cmd->data = ext; ext->cmd = cmd; ext->func = func; ext->options = parser; ext->data = 0; return ext; } VasEBoot_extcmd_t VasEBoot_register_extcmd (const char *name, VasEBoot_extcmd_func_t func, VasEBoot_command_flags_t flags, const char *summary, const char *description, const struct VasEBoot_arg_option *parser) { return VasEBoot_register_extcmd_prio (name, func, flags, summary, description, parser, 1); } static VasEBoot_err_t VasEBoot_extcmd_lockdown (VasEBoot_extcmd_context_t ctxt __attribute__ ((unused)), int argc __attribute__ ((unused)), char **argv __attribute__ ((unused))) { return VasEBoot_error (VAS_EBOOT_ERR_ACCESS_DENIED, N_("%s: the command is not allowed when lockdown is enforced"), ctxt->extcmd->cmd->name); } VasEBoot_extcmd_t VasEBoot_register_extcmd_lockdown (const char *name, VasEBoot_extcmd_func_t func, VasEBoot_command_flags_t flags, const char *summary, const char *description, const struct VasEBoot_arg_option *parser) { if (VasEBoot_is_lockdown () == VAS_EBOOT_LOCKDOWN_ENABLED) func = VasEBoot_extcmd_lockdown; return VasEBoot_register_extcmd (name, func, flags, summary, description, parser); } void VasEBoot_unregister_extcmd (VasEBoot_extcmd_t ext) { if (ext == NULL) return; VasEBoot_unregister_command (ext->cmd); VasEBoot_free (ext); }