/* extcmd.c - support extended command */ /* * VasEBoot -- GRand Unified Bootloader * Copyright (C) 2009 Free Software Foundation, Inc. * * VasEBoot 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. * * VasEBoot 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 VasEBoot. If not, see . */ #include #include #include #include #include #include VasEBoot_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 (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 | VasEBoot_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); } void VasEBoot_unregister_extcmd (VasEBoot_extcmd_t ext) { VasEBoot_unregister_command (ext->cmd); VasEBoot_free (ext); }