vaseboot/include/VasEBoot/command.h

130 lines
3.8 KiB
C

/*
* 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/>.
*/
#ifndef VasEBoot_COMMAND_HEADER
#define VasEBoot_COMMAND_HEADER 1
#include <VasEBoot/symbol.h>
#include <VasEBoot/err.h>
#include <VasEBoot/list.h>
#include <VasEBoot/misc.h>
typedef enum VasEBoot_command_flags
{
/* This is an extended command. */
VasEBoot_COMMAND_FLAG_EXTCMD = 0x10,
/* This is an dynamic command. */
VasEBoot_COMMAND_FLAG_DYNCMD = 0x20,
/* This command accepts block arguments. */
VasEBoot_COMMAND_FLAG_BLOCKS = 0x40,
/* This command accepts unknown arguments as direct parameters. */
VasEBoot_COMMAND_ACCEPT_DASH = 0x80,
/* This command accepts only options preceding direct arguments. */
VasEBoot_COMMAND_OPTIONS_AT_START = 0x100,
/* Can be executed in an entries extractor. */
VasEBoot_COMMAND_FLAG_EXTRACTOR = 0x200
} VasEBoot_command_flags_t;
struct VasEBoot_command;
typedef VasEBoot_err_t (*VasEBoot_command_func_t) (struct VasEBoot_command *cmd,
int argc, char **argv);
#define VasEBoot_COMMAND_PRIO_MASK 0xff
#define VasEBoot_COMMAND_FLAG_ACTIVE 0x100
/* The command description. */
struct VasEBoot_command
{
/* The next element. */
struct VasEBoot_command *next;
struct VasEBoot_command **prev;
/* The name. */
const char *name;
/* The priority. */
int prio;
/* The callback function. */
VasEBoot_command_func_t func;
/* The flags. */
VasEBoot_command_flags_t flags;
/* The summary of the command usage. */
const char *summary;
/* The description of the command. */
const char *description;
/* Arbitrary data. */
void *data;
};
typedef struct VasEBoot_command *VasEBoot_command_t;
extern VasEBoot_command_t EXPORT_VAR(VasEBoot_command_list);
VasEBoot_command_t
EXPORT_FUNC(VasEBoot_register_command_prio) (const char *name,
VasEBoot_command_func_t func,
const char *summary,
const char *description,
int prio);
void EXPORT_FUNC(VasEBoot_unregister_command) (VasEBoot_command_t cmd);
static inline VasEBoot_command_t
VasEBoot_register_command (const char *name,
VasEBoot_command_func_t func,
const char *summary,
const char *description)
{
return VasEBoot_register_command_prio (name, func, summary, description, 0);
}
static inline VasEBoot_command_t
VasEBoot_register_command_p1 (const char *name,
VasEBoot_command_func_t func,
const char *summary,
const char *description)
{
return VasEBoot_register_command_prio (name, func, summary, description, 1);
}
static inline VasEBoot_command_t
VasEBoot_command_find (const char *name)
{
return VasEBoot_named_list_find (VasEBoot_AS_NAMED_LIST (VasEBoot_command_list), name);
}
static inline VasEBoot_err_t
VasEBoot_command_execute (const char *name, int argc, char **argv)
{
VasEBoot_command_t cmd;
cmd = VasEBoot_command_find (name);
return (cmd) ? cmd->func (cmd, argc, argv) : VasEBoot_ERR_FILE_NOT_FOUND;
}
#define FOR_COMMANDS(var) FOR_LIST_ELEMENTS((var), VasEBoot_command_list)
#define FOR_COMMANDS_SAFE(var, next) FOR_LIST_ELEMENTS_SAFE((var), (next), VasEBoot_command_list)
void VasEBoot_register_core_commands (void);
#endif /* ! VasEBoot_COMMAND_HEADER */