Skip to content

Commit 50c1a15

Browse files
committed
command: factorize jim_command_mode()
During 'help' dump, to determine the mode (e.g. COMMAND_CONFIG) of a command, the current code executes the OpenOCD TCL command "command mode", while it could directly call the implementation of the TCL command above. Factorize jim_command_mode() and call the inner implementation instead of executing "command mode". Change-Id: Id8c33d0ed1373b5744dcc3ac354c3e0a88576f75 Signed-off-by: Antonio Borneo <[email protected]> Reviewed-on: https://review.openocd.org/c/openocd/+/8585 Reviewed-by: Evgeniy Naydanov <[email protected]> Tested-by: jenkins
1 parent f55ec6d commit 50c1a15

File tree

1 file changed

+36
-42
lines changed

1 file changed

+36
-42
lines changed

src/helper/command.c

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ static int jim_command_dispatch(Jim_Interp *interp, int argc, Jim_Obj * const *a
4141
static int help_add_command(struct command_context *cmd_ctx,
4242
const char *cmd_name, const char *help_text, const char *usage_text);
4343
static int help_del_command(struct command_context *cmd_ctx, const char *cmd_name);
44+
static enum command_mode get_command_mode(Jim_Interp *interp, const char *cmd_name);
4445

4546
/* set of functions to wrap jimtcl internal data */
4647
static inline bool jimcmd_is_proc(Jim_Cmd *cmd)
@@ -779,24 +780,7 @@ static COMMAND_HELPER(command_help_show, struct help_entry *c,
779780
if (is_match && show_help) {
780781
char *msg;
781782

782-
/* TODO: factorize jim_command_mode() to avoid running jim command here */
783-
char *request = alloc_printf("command mode %s", c->cmd_name);
784-
if (!request) {
785-
LOG_ERROR("Out of memory");
786-
return ERROR_FAIL;
787-
}
788-
int retval = Jim_Eval(CMD_CTX->interp, request);
789-
free(request);
790-
enum command_mode mode = COMMAND_UNKNOWN;
791-
if (retval != JIM_ERR) {
792-
const char *result = Jim_GetString(Jim_GetResult(CMD_CTX->interp), NULL);
793-
if (!strcmp(result, "any"))
794-
mode = COMMAND_ANY;
795-
else if (!strcmp(result, "config"))
796-
mode = COMMAND_CONFIG;
797-
else if (!strcmp(result, "exec"))
798-
mode = COMMAND_EXEC;
799-
}
783+
enum command_mode mode = get_command_mode(CMD_CTX->interp, c->cmd_name);
800784

801785
/* Normal commands are runtime-only; highlight exceptions */
802786
if (mode != COMMAND_EXEC) {
@@ -809,6 +793,7 @@ static COMMAND_HELPER(command_help_show, struct help_entry *c,
809793
case COMMAND_ANY:
810794
stage_msg = " (command valid any time)";
811795
break;
796+
case COMMAND_UNKNOWN:
812797
default:
813798
stage_msg = " (?mode error?)";
814799
break;
@@ -817,11 +802,13 @@ static COMMAND_HELPER(command_help_show, struct help_entry *c,
817802
} else
818803
msg = alloc_printf("%s", c->help ? c->help : "");
819804

820-
if (msg) {
821-
command_help_show_wrap(msg, n + 3, n + 3);
822-
free(msg);
823-
} else
824-
return -ENOMEM;
805+
if (!msg) {
806+
LOG_ERROR("Out of memory");
807+
return ERROR_FAIL;
808+
}
809+
810+
command_help_show_wrap(msg, n + 3, n + 3);
811+
free(msg);
825812
}
826813

827814
return ERROR_OK;
@@ -936,35 +923,41 @@ static int jim_command_dispatch(Jim_Interp *interp, int argc, Jim_Obj * const *a
936923
return retval;
937924
}
938925

926+
static enum command_mode get_command_mode(Jim_Interp *interp, const char *cmd_name)
927+
{
928+
if (!cmd_name)
929+
return COMMAND_UNKNOWN;
930+
931+
Jim_Obj *s = Jim_NewStringObj(interp, cmd_name, -1);
932+
Jim_IncrRefCount(s);
933+
Jim_Cmd *cmd = Jim_GetCommand(interp, s, JIM_NONE);
934+
Jim_DecrRefCount(interp, s);
935+
936+
if (!cmd || !(jimcmd_is_proc(cmd) || jimcmd_is_oocd_command(cmd)))
937+
return COMMAND_UNKNOWN;
938+
939+
/* tcl proc */
940+
if (jimcmd_is_proc(cmd))
941+
return COMMAND_ANY;
942+
943+
struct command *c = jimcmd_privdata(cmd);
944+
return c->mode;
945+
}
946+
939947
static int jim_command_mode(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
940948
{
941949
struct command_context *cmd_ctx = current_command_context(interp);
942-
enum command_mode mode;
950+
enum command_mode mode = cmd_ctx->mode;
943951

944952
if (argc > 1) {
945953
char *full_name = alloc_concatenate_strings(argc - 1, argv + 1);
946954
if (!full_name)
947955
return JIM_ERR;
948-
Jim_Obj *s = Jim_NewStringObj(interp, full_name, -1);
949-
Jim_IncrRefCount(s);
950-
Jim_Cmd *cmd = Jim_GetCommand(interp, s, JIM_NONE);
951-
Jim_DecrRefCount(interp, s);
952-
free(full_name);
953-
if (!cmd || !(jimcmd_is_proc(cmd) || jimcmd_is_oocd_command(cmd))) {
954-
Jim_SetResultString(interp, "unknown", -1);
955-
return JIM_OK;
956-
}
957956

958-
if (jimcmd_is_proc(cmd)) {
959-
/* tcl proc */
960-
mode = COMMAND_ANY;
961-
} else {
962-
struct command *c = jimcmd_privdata(cmd);
957+
mode = get_command_mode(interp, full_name);
963958

964-
mode = c->mode;
965-
}
966-
} else
967-
mode = cmd_ctx->mode;
959+
free(full_name);
960+
}
968961

969962
const char *mode_str;
970963
switch (mode) {
@@ -977,6 +970,7 @@ static int jim_command_mode(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
977970
case COMMAND_EXEC:
978971
mode_str = "exec";
979972
break;
973+
case COMMAND_UNKNOWN:
980974
default:
981975
mode_str = "unknown";
982976
break;

0 commit comments

Comments
 (0)