Skip to content

Commit

Permalink
helper/command: inline run_command() in exec_command()
Browse files Browse the repository at this point in the history
Simplify the command execution by inlining run_command() inside
exec_command().

Change-Id: Id932b006846720cfd867d22d142cd35831dbd1a2
Signed-off-by: Antonio Borneo <[email protected]>
Reviewed-on: https://review.openocd.org/c/openocd/+/8056
Tested-by: jenkins
  • Loading branch information
borneoa committed Jan 13, 2024
1 parent f9ea9ce commit f857db9
Showing 1 changed file with 18 additions and 25 deletions.
43 changes: 18 additions & 25 deletions src/helper/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 */
Expand Down

0 comments on commit f857db9

Please sign in to comment.