]> www.infradead.org Git - users/borneoa/openocd-next.git/commitdiff
command: factorize jim_command_mode()
authorAntonio Borneo <borneo.antonio@gmail.com>
Sat, 2 Dec 2023 15:50:14 +0000 (16:50 +0100)
committerAntonio Borneo <borneo.antonio@gmail.com>
Sat, 29 Mar 2025 18:23:46 +0000 (18:23 +0000)
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 <borneo.antonio@gmail.com>
Reviewed-on: https://review.openocd.org/c/openocd/+/8585
Reviewed-by: Evgeniy Naydanov <evgeniy.naydanov@syntacore.com>
Tested-by: jenkins
src/helper/command.c

index 38fb4f83b7a4cfbde6ca0b68412ddb5d7f35a011..3d4379d06cc6ad2c31cbfd64188239e84c9f10c3 100644 (file)
@@ -41,6 +41,7 @@ static int jim_command_dispatch(Jim_Interp *interp, int argc, Jim_Obj * const *a
 static int help_add_command(struct command_context *cmd_ctx,
        const char *cmd_name, const char *help_text, const char *usage_text);
 static int help_del_command(struct command_context *cmd_ctx, const char *cmd_name);
+static enum command_mode get_command_mode(Jim_Interp *interp, const char *cmd_name);
 
 /* set of functions to wrap jimtcl internal data */
 static inline bool jimcmd_is_proc(Jim_Cmd *cmd)
@@ -779,24 +780,7 @@ static COMMAND_HELPER(command_help_show, struct help_entry *c,
        if (is_match && show_help) {
                char *msg;
 
-               /* TODO: factorize jim_command_mode() to avoid running jim command here */
-               char *request = alloc_printf("command mode %s", c->cmd_name);
-               if (!request) {
-                       LOG_ERROR("Out of memory");
-                       return ERROR_FAIL;
-               }
-               int retval = Jim_Eval(CMD_CTX->interp, request);
-               free(request);
-               enum command_mode mode = COMMAND_UNKNOWN;
-               if (retval != JIM_ERR) {
-                       const char *result = Jim_GetString(Jim_GetResult(CMD_CTX->interp), NULL);
-                       if (!strcmp(result, "any"))
-                               mode = COMMAND_ANY;
-                       else if (!strcmp(result, "config"))
-                               mode = COMMAND_CONFIG;
-                       else if (!strcmp(result, "exec"))
-                               mode = COMMAND_EXEC;
-               }
+               enum command_mode mode = get_command_mode(CMD_CTX->interp, c->cmd_name);
 
                /* Normal commands are runtime-only; highlight exceptions */
                if (mode != COMMAND_EXEC) {
@@ -809,6 +793,7 @@ static COMMAND_HELPER(command_help_show, struct help_entry *c,
                                case COMMAND_ANY:
                                        stage_msg = " (command valid any time)";
                                        break;
+                               case COMMAND_UNKNOWN:
                                default:
                                        stage_msg = " (?mode error?)";
                                        break;
@@ -817,11 +802,13 @@ static COMMAND_HELPER(command_help_show, struct help_entry *c,
                } else
                        msg = alloc_printf("%s", c->help ? c->help : "");
 
-               if (msg) {
-                       command_help_show_wrap(msg, n + 3, n + 3);
-                       free(msg);
-               } else
-                       return -ENOMEM;
+               if (!msg) {
+                       LOG_ERROR("Out of memory");
+                       return ERROR_FAIL;
+               }
+
+               command_help_show_wrap(msg, n + 3, n + 3);
+               free(msg);
        }
 
        return ERROR_OK;
@@ -936,35 +923,41 @@ static int jim_command_dispatch(Jim_Interp *interp, int argc, Jim_Obj * const *a
        return retval;
 }
 
+static enum command_mode get_command_mode(Jim_Interp *interp, const char *cmd_name)
+{
+       if (!cmd_name)
+               return COMMAND_UNKNOWN;
+
+       Jim_Obj *s = Jim_NewStringObj(interp, cmd_name, -1);
+       Jim_IncrRefCount(s);
+       Jim_Cmd *cmd = Jim_GetCommand(interp, s, JIM_NONE);
+       Jim_DecrRefCount(interp, s);
+
+       if (!cmd || !(jimcmd_is_proc(cmd) || jimcmd_is_oocd_command(cmd)))
+               return COMMAND_UNKNOWN;
+
+       /* tcl proc */
+       if (jimcmd_is_proc(cmd))
+               return COMMAND_ANY;
+
+       struct command *c = jimcmd_privdata(cmd);
+       return c->mode;
+}
+
 static int jim_command_mode(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
 {
        struct command_context *cmd_ctx = current_command_context(interp);
-       enum command_mode mode;
+       enum command_mode mode = cmd_ctx->mode;
 
        if (argc > 1) {
                char *full_name = alloc_concatenate_strings(argc - 1, argv + 1);
                if (!full_name)
                        return JIM_ERR;
-               Jim_Obj *s = Jim_NewStringObj(interp, full_name, -1);
-               Jim_IncrRefCount(s);
-               Jim_Cmd *cmd = Jim_GetCommand(interp, s, JIM_NONE);
-               Jim_DecrRefCount(interp, s);
-               free(full_name);
-               if (!cmd || !(jimcmd_is_proc(cmd) || jimcmd_is_oocd_command(cmd))) {
-                       Jim_SetResultString(interp, "unknown", -1);
-                       return JIM_OK;
-               }
 
-               if (jimcmd_is_proc(cmd)) {
-                       /* tcl proc */
-                       mode = COMMAND_ANY;
-               } else {
-                       struct command *c = jimcmd_privdata(cmd);
+               mode = get_command_mode(interp, full_name);
 
-                       mode = c->mode;
-               }
-       } else
-               mode = cmd_ctx->mode;
+               free(full_name);
+       }
 
        const char *mode_str;
        switch (mode) {
@@ -977,6 +970,7 @@ static int jim_command_mode(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
                case COMMAND_EXEC:
                        mode_str = "exec";
                        break;
+               case COMMAND_UNKNOWN:
                default:
                        mode_str = "unknown";
                        break;