two different handlers, but calling it twice with the
same event name assigns only one handler.
+Current target is temporarily overridden to the event issuing target
+before handler code starts and switched back after handler is done.
+
@item @code{-work-area-backup} (@option{0}|@option{1}) -- says
whether the work area gets backed up; by default,
@emph{it is not backed up.}
.argc = num_words - 1,
.argv = words + 1,
};
+ /* Black magic of overridden current target:
+ * If the command we are going to handle has a target prefix,
+ * override the current target temporarily for the time
+ * of processing the command.
+ * current_target_override is used also for event handlers
+ * therefore we prevent touching it if command has no prefix.
+ * Previous override is saved and restored back to ensure
+ * correct work when run_command() is re-entered. */
+ struct target *saved_target_override = context->current_target_override;
+ if (c->jim_handler_data)
+ context->current_target_override = c->jim_handler_data;
+
int retval = c->handler(&cmd);
+
+ if (c->jim_handler_data)
+ context->current_target_override = saved_target_override;
+
if (retval == ERROR_COMMAND_SYNTAX_ERROR) {
/* Print help for command */
char *full_name = command_name(c, ' ');
* happen when the Jim Tcl interpreter is provided by eCos for
* instance.
*/
+ context->current_target_override = NULL;
+
Jim_Interp *interp = context->interp;
Jim_DeleteAssocData(interp, "context");
retcode = Jim_SetAssocData(interp, "context", NULL, context);
struct command_context *command_init(const char *startup_tcl, Jim_Interp *interp)
{
- struct command_context *context = malloc(sizeof(struct command_context));
+ struct command_context *context = calloc(1, sizeof(struct command_context));
const char *HostOs;
context->mode = COMMAND_EXEC;
- context->commands = NULL;
- context->current_target = 0;
- context->output_handler = NULL;
- context->output_handler_priv = NULL;
/* Create a jim interpreter if we were not handed one */
if (interp == NULL) {
Jim_Interp *interp;
enum command_mode mode;
struct command *commands;
- int current_target;
+ struct target *current_target;
+ /* The target set by 'targets xx' command or the latest created */
+ struct target *current_target_override;
+ /* If set overrides current_target
+ * It happens during processing of
+ * 1) a target prefixed command
+ * 2) an event handler
+ * Pay attention to reentrancy when setting override.
+ */
command_output_handler_t output_handler;
void *output_handler_priv;
};
command_handler_t handler;
Jim_CmdProc *jim_handler;
void *jim_handler_data;
+ /* Currently used only for target of target-prefixed cmd.
+ * Native OpenOCD commands use jim_handler_data exclusively
+ * as a target override.
+ * Jim handlers outside of target cmd tree can use
+ * jim_handler_data for any handler specific data */
enum command_mode mode;
struct command *next;
};
connection->priv = tclc;
- struct target *target = get_target_by_num(connection->cmd_ctx->current_target);
+ struct target *target = get_current_target(connection->cmd_ctx);
if (target != NULL)
tclc->tc_laststate = target->state;
struct target *get_current_target(struct command_context *cmd_ctx)
{
- struct target *target = get_target_by_num(cmd_ctx->current_target);
+ struct target *target = cmd_ctx->current_target_override
+ ? cmd_ctx->current_target_override
+ : cmd_ctx->current_target;
if (target == NULL) {
LOG_ERROR("BUG: current_target out of bounds");
return ERROR_FAIL;
}
- cmd_ctx->current_target = target->target_number;
+ cmd_ctx->current_target = target;
+ if (cmd_ctx->current_target_override)
+ cmd_ctx->current_target_override = target;
+
return ERROR_OK;
}
else
state = "tap-disabled";
- if (CMD_CTX->current_target == target->target_number)
+ if (CMD_CTX->current_target == target)
marker = '*';
/* keep columns lined up to match the headers above */
for (teap = target->event_action; teap != NULL; teap = teap->next) {
if (teap->event == e) {
- LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
+ LOG_DEBUG("target(%d): %s (%s) event: %d (%s) action: %s",
target->target_number,
target_name(target),
target_type_name(target),
e,
Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
Jim_GetString(teap->body, NULL));
+
+ /* Override current target by the target an event
+ * is issued from (lot of scripts need it).
+ * Return back to previous override as soon
+ * as the handler processing is done */
+ struct command_context *cmd_ctx = current_command_context(teap->interp);
+ struct target *saved_target_override = cmd_ctx->current_target_override;
+ cmd_ctx->current_target_override = target;
+
if (Jim_EvalObj(teap->interp, teap->body) != JIM_OK) {
Jim_MakeErrorMessage(teap->interp);
command_print(NULL, "%s\n", Jim_GetString(Jim_GetResult(teap->interp), NULL));
}
+
+ cmd_ctx->current_target_override = saved_target_override;
}
}
}
target = calloc(1, sizeof(struct target));
/* set target number */
target->target_number = new_target_number();
- cmd_ctx->current_target = target->target_number;
+ cmd_ctx->current_target = target;
/* allocate memory for each unique target type */
target->type = calloc(1, sizeof(struct target_type));