diff options
author | Antonio Borneo <borneo.antonio@gmail.com> | 2020-05-12 00:22:13 +0200 |
---|---|---|
committer | Antonio Borneo <borneo.antonio@gmail.com> | 2021-04-18 15:32:57 +0100 |
commit | a510c8e23c23b7a888623faf0c4a9982489ddf83 (patch) | |
tree | ec3b098397f97524aa7314cbe5e361611e8d7147 /src/helper | |
parent | a9904ba22c6a8c9cd56c85296e80396c9e05ee9e (diff) | |
download | riscv-openocd-a510c8e23c23b7a888623faf0c4a9982489ddf83.zip riscv-openocd-a510c8e23c23b7a888623faf0c4a9982489ddf83.tar.gz riscv-openocd-a510c8e23c23b7a888623faf0c4a9982489ddf83.tar.bz2 |
helper/command: always pass struct command as jim private data
While registering a new command, jim accepts a pointer to command's
private data that will be accessible during the command execution.
Today openocd is not consistent and passes different private data
depending on the command, and then even overwrites it:
- "simple" commands (.handler) are registered with their own
struct command pointer as command private data;
- "native" commands (.jim_handler) at root level are registered
with NULL command private data;
- "native" commands (.jim_handler) not at root level are
registered with the struct command pointer of their root command
as command private data but, when executed, the command private
data is overwritten by the value in field jim_handler_data taken
from their struct command.
Uniform the usage of command private data by always set it to the
struct command pointer while registering the new commands.
Note: for multi-word commands only the root command is registered,
so command private data will be set to the struct command of the
root command. This will change later in this series when the full-
name of the command will be registered.
Don't overwrite the command private data, but let the commands that
needs jim_handler_data to get it directly through struct command.
For sake of uniformity, let function command_set_handler_data() to
set the field jim_handler_data also for "group" commands, even if
such value will not be used.
Now Jim_CmdPrivData() always returns a struct command pointer, so
wrap it in the inline function jim_to_command() to gain compile
time check on the returned type.
While there, uniform the code to use the macro Jim_CmdPrivData()
to access the command's private data pointer.
Change-Id: Idba16242ba1f6769341b4030a49cdf35a5278695
Signed-off-by: Antonio Borneo <borneo.antonio@gmail.com>
Reviewed-on: http://openocd.zylin.com/5664
Tested-by: jenkins
Reviewed-by: Oleksij Rempel <linux@rempel-privat.de>
Diffstat (limited to 'src/helper')
-rw-r--r-- | src/helper/command.c | 8 | ||||
-rw-r--r-- | src/helper/command.h | 9 |
2 files changed, 12 insertions, 5 deletions
diff --git a/src/helper/command.c b/src/helper/command.c index e2726f2..b44e466 100644 --- a/src/helper/command.c +++ b/src/helper/command.c @@ -248,7 +248,7 @@ static int script_command(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { /* the private data is stashed in the interp structure */ - struct command *c = interp->cmdPrivData; + struct command *c = jim_to_command(interp); assert(c); script_debug(interp, argc, argv); return script_command_run(interp, argc, argv, c); @@ -418,7 +418,7 @@ static struct command *register_command(struct command_context *context, int retval = JIM_OK; if (NULL != cr->jim_handler && NULL == parent) { retval = Jim_CreateCommand(context->interp, cr->name, - cr->jim_handler, NULL, NULL); + cr->jim_handler, c, NULL); } else if (NULL != cr->handler || NULL != parent) retval = register_command_handler(context, command_root(c)); @@ -501,8 +501,7 @@ static int unregister_command(struct command_context *context, void command_set_handler_data(struct command *c, void *p) { - if (NULL != c->handler || NULL != c->jim_handler) - c->jim_handler_data = p; + c->jim_handler_data = p; for (struct command *cc = c->children; NULL != cc; cc = cc->next) command_set_handler_data(cc, p); } @@ -1085,7 +1084,6 @@ static int command_unknown(Jim_Interp *interp, int argc, Jim_Obj *const *argv) if (!command_can_run(cmd_ctx, c)) return JIM_ERR; - interp->cmdPrivData = c->jim_handler_data; return (*c->jim_handler)(interp, count, start); } diff --git a/src/helper/command.h b/src/helper/command.h index b0c84bb..cb088d7 100644 --- a/src/helper/command.h +++ b/src/helper/command.h @@ -196,6 +196,15 @@ struct command { }; /* + * Return the struct command pointer kept in private data + * Used to enforce check on data type + */ +static inline struct command *jim_to_command(Jim_Interp *interp) +{ + return Jim_CmdPrivData(interp); +} + +/* * Commands should be registered by filling in one or more of these * structures and passing them to [un]register_commands(). * |