vaseboot/VasEBoot-core/kern/command.c

88 lines
2.1 KiB
C

/* command.c - support basic 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 <http://www.gnu.org/licenses/>.
*/
#include <VasEBoot/mm.h>
#include <VasEBoot/command.h>
VasEBoot_command_t VasEBoot_command_list;
VasEBoot_command_t
VasEBoot_register_command_prio (const char *name,
VasEBoot_command_func_t func,
const char *summary,
const char *description,
int prio)
{
VasEBoot_command_t cmd;
int inactive = 0;
VasEBoot_command_t *p, q;
cmd = (VasEBoot_command_t) VasEBoot_zalloc (sizeof (*cmd));
if (! cmd)
return 0;
cmd->name = name;
cmd->func = func;
cmd->summary = (summary) ? summary : "";
cmd->description = description;
cmd->flags = 0;
cmd->prio = prio;
for (p = &VasEBoot_command_list, q = *p; q; p = &(q->next), q = q->next)
{
int r;
r = VasEBoot_strcmp (cmd->name, q->name);
if (r < 0)
break;
if (r > 0)
continue;
if (cmd->prio >= (q->prio & VasEBoot_COMMAND_PRIO_MASK))
{
q->prio &= ~VasEBoot_COMMAND_FLAG_ACTIVE;
break;
}
inactive = 1;
}
*p = cmd;
cmd->next = q;
if (q)
q->prev = &cmd->next;
cmd->prev = p;
if (! inactive)
cmd->prio |= VasEBoot_COMMAND_FLAG_ACTIVE;
return cmd;
}
void
VasEBoot_unregister_command (VasEBoot_command_t cmd)
{
if ((cmd->prio & VasEBoot_COMMAND_FLAG_ACTIVE) && (cmd->next))
cmd->next->prio |= VasEBoot_COMMAND_FLAG_ACTIVE;
VasEBoot_list_remove (VasEBoot_AS_LIST (cmd));
VasEBoot_free (cmd);
}