helper/command: override target only on target prefixed cmds

In current code the current target is overridden whenever
jim_handler_data is not NULL. This happens not only with target
prefixed commands, but also with cti, dap and swo/tpiu prefixed
commands.
While this is not causing any run-time issue, by now, the
behaviour is tricky and makes the code cryptic.

Add a specific field to struct command for the target override so
the content of jim_handler_data can be restricted to command
specific data only (today only cti, dap and swo/tpiu).

Extend the API register_commands() to specify the presence of
either the command data or the override target.

The new API makes obsolete calling command_set_handler_data() to
set jim_handler_data, so remove it.

Change-Id: Icc323faf754b0546a72208f90abd9e68ff2ef52f
Signed-off-by: Antonio Borneo <borneo.antonio@gmail.com>
Reviewed-on: http://openocd.zylin.com/5667
Tested-by: jenkins
Reviewed-by: Oleksij Rempel <linux@rempel-privat.de>
This commit is contained in:
Antonio Borneo
2020-05-12 11:52:56 +02:00
parent 7cd679a2de
commit 4289389937
6 changed files with 69 additions and 52 deletions

View File

@@ -391,8 +391,9 @@ static struct command *register_command(struct command_context *context,
return c;
}
int register_commands(struct command_context *cmd_ctx, struct command *parent,
const struct command_registration *cmds)
int __register_commands(struct command_context *cmd_ctx, struct command *parent,
const struct command_registration *cmds, void *data,
struct target *override_target)
{
int retval = ERROR_OK;
unsigned i;
@@ -406,10 +407,12 @@ int register_commands(struct command_context *cmd_ctx, struct command *parent,
retval = ERROR_FAIL;
break;
}
c->jim_handler_data = data;
c->jim_override_target = override_target;
}
if (NULL != cr->chain) {
struct command *p = c ? : parent;
retval = register_commands(cmd_ctx, p, cr->chain);
retval = __register_commands(cmd_ctx, p, cr->chain, data, override_target);
if (ERROR_OK != retval)
break;
}
@@ -461,13 +464,6 @@ static int unregister_command(struct command_context *context,
return ERROR_OK;
}
void command_set_handler_data(struct command *c, void *p)
{
c->jim_handler_data = p;
for (struct command *cc = c->children; NULL != cc; cc = cc->next)
command_set_handler_data(cc, p);
}
void command_output_text(struct command_context *context, const char *data)
{
if (context && context->output_handler && data)
@@ -1057,12 +1053,12 @@ static int command_unknown(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
* correct work when command_unknown() is re-entered.
*/
struct target *saved_target_override = cmd_ctx->current_target_override;
if (c->jim_handler_data)
cmd_ctx->current_target_override = c->jim_handler_data;
if (c->jim_override_target)
cmd_ctx->current_target_override = c->jim_override_target;
int retval = exec_command(interp, cmd_ctx, c, count, start);
if (c->jim_handler_data)
if (c->jim_override_target)
cmd_ctx->current_target_override = saved_target_override;
return retval;