diff options
author | Antonio Borneo <borneo.antonio@gmail.com> | 2023-08-06 12:26:25 +0200 |
---|---|---|
committer | Evgeniy Naydanov <109669442+en-sc@users.noreply.github.com> | 2024-01-29 13:36:27 +0300 |
commit | 350f5c3547a8efea7015816e610f19ea64f1f6f8 (patch) | |
tree | e200e7c9915d07cdc9a694f5ad268e274b233997 | |
parent | d645030394615d7eadd9d21302193c53520e625b (diff) | |
download | riscv-openocd-350f5c3547a8efea7015816e610f19ea64f1f6f8.zip riscv-openocd-350f5c3547a8efea7015816e610f19ea64f1f6f8.tar.gz riscv-openocd-350f5c3547a8efea7015816e610f19ea64f1f6f8.tar.bz2 |
helper/command: inline run_command() in exec_command()
Simplify the command execution by inlining run_command() inside
exec_command().
Change-Id: Id932b006846720cfd867d22d142cd35831dbd1a2
Signed-off-by: Antonio Borneo <borneo.antonio@gmail.com>
Reviewed-on: https://review.openocd.org/c/openocd/+/8056
Tested-by: jenkins
-rw-r--r-- | src/helper/command.c | 43 |
1 files changed, 18 insertions, 25 deletions
diff --git a/src/helper/command.c b/src/helper/command.c index 8860cf8..57db2ad 100644 --- a/src/helper/command.c +++ b/src/helper/command.c @@ -489,14 +489,27 @@ static bool command_can_run(struct command_context *cmd_ctx, struct command *c, return false; } -static int run_command(struct command_context *context, - struct command *c, const char **words, unsigned num_words) +static int exec_command(Jim_Interp *interp, struct command_context *context, + struct command *c, int argc, Jim_Obj * const *argv) { + if (c->jim_handler) + return c->jim_handler(interp, argc, argv); + + /* use c->handler */ + const char **words = malloc(argc * sizeof(char *)); + if (!words) { + LOG_ERROR("Out of memory"); + return JIM_ERR; + } + + for (int i = 0; i < argc; i++) + words[i] = Jim_GetString(argv[i], NULL); + struct command_invocation cmd = { .ctx = context, .current = c, .name = c->name, - .argc = num_words - 1, + .argc = argc - 1, .argv = words + 1, }; @@ -526,7 +539,8 @@ static int run_command(struct command_context *context, } Jim_DecrRefCount(context->interp, cmd.output); - return retval; + free(words); + return command_retval_set(interp, retval); } int command_run_line(struct command_context *context, char *line) @@ -867,27 +881,6 @@ static char *alloc_concatenate_strings(int argc, Jim_Obj * const *argv) return all; } -static int exec_command(Jim_Interp *interp, struct command_context *cmd_ctx, - struct command *c, int argc, Jim_Obj * const *argv) -{ - if (c->jim_handler) - return c->jim_handler(interp, argc, argv); - - /* use c->handler */ - const char **words = malloc(argc * sizeof(char *)); - if (!words) { - LOG_ERROR("Out of memory"); - return JIM_ERR; - } - - for (int i = 0; i < argc; i++) - words[i] = Jim_GetString(argv[i], NULL); - - int retval = run_command(cmd_ctx, c, words, argc); - free(words); - return command_retval_set(interp, retval); -} - static int jim_command_dispatch(Jim_Interp *interp, int argc, Jim_Obj * const *argv) { /* check subcommands */ |