target: use correct target in target-prefixed commands and event handlers
This change contains an alternative to Matthias Welwarsky's #4130 (target-prefixed commands) and to #4293 (event handlers). get_current_target() must retrieve the target associated to the current command. If no target associated, the current target of the command context is used as a fallback. Many Tcl event handlers work with the current target as if it were the target issuing the event. current_target in command_context is a number and has to be converted to a pointer in every get_current_target() call. The solution: - Replace current_target in command_context by a target pointer - Add another target pointer current_target_override - get_current_target() returns current_target_override if set, otherwise current_target - Save, set and restore current_target_override to the current prefix in run_command() - Save, set and restore current_target_override to the event invoking target in target_handle_event() While on it use calloc when allocating a new command_context. Change-Id: I9a82102e94dcac063743834a1d28da861b2e74ea Signed-off-by: Tomas Vanek <vanekt@fbl.cz> Suggested-by: Matthias Welwarsky <matthias.welwarsky@sysgo.com> Reviewed-on: http://openocd.zylin.com/4295 Tested-by: jenkins Reviewed-by: Matthias Welwarsky <matthias@welwarsky.de>
This commit is contained in:
committed by
Matthias Welwarsky
parent
eaeb4191e5
commit
bb9d9c6026
@@ -608,7 +608,23 @@ static int run_command(struct command_context *context,
|
||||
.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, ' ');
|
||||
@@ -643,6 +659,8 @@ int command_run_line(struct command_context *context, char *line)
|
||||
* 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);
|
||||
@@ -1269,14 +1287,10 @@ static const struct command_registration command_builtin_handlers[] = {
|
||||
|
||||
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) {
|
||||
|
||||
@@ -49,7 +49,15 @@ struct command_context {
|
||||
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;
|
||||
};
|
||||
@@ -168,6 +176,11 @@ struct command {
|
||||
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;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user