Skip to content

Commit 877cec2

Browse files
committed
command: check command mode for native jim commands
The command mode was checked only for simple type of commands. Native commands (handled by jim_handler) was treated as they had mode COMMAND_ANY Change-Id: Iab1d8cbb0b8c6f6b9f3cf942600432dec9a703ff Signed-off-by: Tomas Vanek <[email protected]> Reviewed-on: http://openocd.zylin.com/4841 Tested-by: jenkins Reviewed-by: Antonio Borneo <[email protected]>
1 parent d479020 commit 877cec2

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

src/helper/command.c

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -579,31 +579,35 @@ char *command_name(struct command *c, char delim)
579579

580580
static bool command_can_run(struct command_context *cmd_ctx, struct command *c)
581581
{
582-
return c->mode == COMMAND_ANY || c->mode == cmd_ctx->mode;
582+
if (c->mode == COMMAND_ANY || c->mode == cmd_ctx->mode)
583+
return true;
584+
585+
/* Many commands may be run only before/after 'init' */
586+
const char *when;
587+
switch (c->mode) {
588+
case COMMAND_CONFIG:
589+
when = "before";
590+
break;
591+
case COMMAND_EXEC:
592+
when = "after";
593+
break;
594+
/* handle the impossible with humor; it guarantees a bug report! */
595+
default:
596+
when = "if Cthulhu is summoned by";
597+
break;
598+
}
599+
char *full_name = command_name(c, ' ');
600+
LOG_ERROR("The '%s' command must be used %s 'init'.",
601+
full_name ? full_name : c->name, when);
602+
free(full_name);
603+
return false;
583604
}
584605

585606
static int run_command(struct command_context *context,
586607
struct command *c, const char *words[], unsigned num_words)
587608
{
588-
if (!command_can_run(context, c)) {
589-
/* Many commands may be run only before/after 'init' */
590-
const char *when;
591-
switch (c->mode) {
592-
case COMMAND_CONFIG:
593-
when = "before";
594-
break;
595-
case COMMAND_EXEC:
596-
when = "after";
597-
break;
598-
/* handle the impossible with humor; it guarantees a bug report! */
599-
default:
600-
when = "if Cthulhu is summoned by";
601-
break;
602-
}
603-
LOG_ERROR("The '%s' command must be used %s 'init'.",
604-
c->name, when);
609+
if (!command_can_run(context, c))
605610
return ERROR_FAIL;
606-
}
607611

608612
struct command_invocation cmd = {
609613
.ctx = context,
@@ -1032,6 +1036,9 @@ static int command_unknown(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
10321036
}
10331037
/* pass the command through to the intended handler */
10341038
if (c->jim_handler) {
1039+
if (!command_can_run(cmd_ctx, c))
1040+
return ERROR_FAIL;
1041+
10351042
interp->cmdPrivData = c->jim_handler_data;
10361043
return (*c->jim_handler)(interp, count, start);
10371044
}

0 commit comments

Comments
 (0)