if (NULL == c->parent) {
/* allocate enough for the name, child names, and '\0' */
name = malloc(len + extra + 1);
+ if (!name) {
+ LOG_ERROR("Out of memory");
+ return NULL;
+ }
strcpy(name, c->name);
} else {
/* parent's extra must include both the space and name */
static bool command_can_run(struct command_context *cmd_ctx, struct command *c)
{
- return c->mode == COMMAND_ANY || c->mode == cmd_ctx->mode;
+ if (c->mode == COMMAND_ANY || c->mode == cmd_ctx->mode)
+ return true;
+
+ /* Many commands may be run only before/after 'init' */
+ const char *when;
+ switch (c->mode) {
+ case COMMAND_CONFIG:
+ when = "before";
+ break;
+ case COMMAND_EXEC:
+ when = "after";
+ break;
+ /* handle the impossible with humor; it guarantees a bug report! */
+ default:
+ when = "if Cthulhu is summoned by";
+ break;
+ }
+ char *full_name = command_name(c, ' ');
+ LOG_ERROR("The '%s' command must be used %s 'init'.",
+ full_name ? full_name : c->name, when);
+ free(full_name);
+ return false;
}
static int run_command(struct command_context *context,
struct command *c, const char *words[], unsigned num_words)
{
- if (!command_can_run(context, c)) {
- /* Many commands may be run only before/after 'init' */
- const char *when;
- switch (c->mode) {
- case COMMAND_CONFIG:
- when = "before";
- break;
- case COMMAND_EXEC:
- when = "after";
- break;
- /* handle the impossible with humor; it guarantees a bug report! */
- default:
- when = "if Cthulhu is summoned by";
- break;
- }
- LOG_ERROR("The '%s' command must be used %s 'init'.",
- c->name, when);
+ if (!command_can_run(context, c))
return ERROR_FAIL;
- }
struct command_invocation cmd = {
.ctx = context,
if (NULL != full_name) {
command_run_linef(context, "usage %s", full_name);
free(full_name);
- } else
- retval = -ENOMEM;
+ }
} else if (retval == ERROR_COMMAND_CLOSE_CONNECTION) {
/* just fall through for a shutdown request */
} else if (retval != ERROR_OK) {
/* we do not print out an error message because the command *should*
* have printed out an error
*/
- LOG_DEBUG("Command failed with error code %d", retval);
+ char *full_name = command_name(c, ' ');
+ LOG_DEBUG("Command '%s' failed with error code %d",
+ full_name ? full_name : c->name, retval);
+ free(full_name);
}
return retval;
{
char *cmd_name = command_name(c, ' ');
if (NULL == cmd_name)
- return -ENOMEM;
+ return ERROR_FAIL;
/* If the match string occurs anywhere, we print out
* stuff for this command. */
}
/* pass the command through to the intended handler */
if (c->jim_handler) {
+ if (!command_can_run(cmd_ctx, c))
+ return JIM_ERR;
+
interp->cmdPrivData = c->jim_handler_data;
return (*c->jim_handler)(interp, count, start);
}
.mode = COMMAND_ANY,
.help = "core command group (introspection)",
.chain = command_subcommand_handlers,
+ .usage = "",
},
COMMAND_REGISTRATION_DONE
};