add register_commands for batch registration

The register_commands API takes multiple commands in one call, allowing
modules to declare and pass a much simpler (and more explicit) array of
command_registration records.
This commit is contained in:
Zachary T Welch 2009-11-20 12:46:06 -08:00
parent 69076057dd
commit 2461855494
2 changed files with 36 additions and 0 deletions

View File

@ -302,6 +302,23 @@ struct command* register_command(struct command_context *context,
return c; return c;
} }
int register_commands(struct command_context *cmd_ctx, struct command *parent,
const struct command_registration *cmds)
{
unsigned i;
for (i = 0; cmds[i].name; i++)
{
struct command *c = register_command(cmd_ctx, parent, cmds + i);
if (NULL != c)
continue;
for (unsigned j = 0; j < i; j++)
unregister_command(cmd_ctx, parent, cmds[j].name);
return ERROR_FAIL;
}
return ERROR_OK;
}
int unregister_all_commands(struct command_context *context, int unregister_all_commands(struct command_context *context,
struct command *parent) struct command *parent)
{ {

View File

@ -201,6 +201,9 @@ struct command_registration {
const char *help; const char *help;
}; };
/// Use this as the last entry in an array of command_registration records.
#define COMMAND_REGISTRATION_DONE { .name = NULL }
/** /**
* Register a command @c handler that can be called from scripts during * Register a command @c handler that can be called from scripts during
* the execution @c mode specified. * the execution @c mode specified.
@ -230,6 +233,22 @@ struct command* register_command(struct command_context *cmd_ctx,
register_command(_cmd_ctx, _parent, &cr); \ register_command(_cmd_ctx, _parent, &cr); \
}) })
/**
* Register one or more commands in the specified context, as children
* of @c parent (or top-level commends, if NULL).
*
* @param cmd_ctx The command_context in which to register the command.
* @param parent Register this command as a child of this, or NULL to
* register a top-level command.
* @param cmds Pointer to an array of command_registration records that
* contains the desired command parameters. The last record must have
* NULL for all fields.
* @returns ERROR_OK on success; ERROR_FAIL if any registration fails.
*/
int register_commands(struct command_context *cmd_ctx, struct command *parent,
const struct command_registration *cmds);
/** /**
* Unregisters command @c name from the given context, @c cmd_ctx. * Unregisters command @c name from the given context, @c cmd_ctx.
* @param cmd_ctx The context of the registered command. * @param cmd_ctx The context of the registered command.