server/telnet: fix autocomplete for jimtcl commands
authorAntonio Borneo <borneo.antonio@gmail.com>
Thu, 27 May 2021 13:25:43 +0000 (15:25 +0200)
committerAntonio Borneo <borneo.antonio@gmail.com>
Fri, 4 Jun 2021 16:41:09 +0000 (17:41 +0100)
Current autocomplete filters-out some command reported by "info
commands". One of the filter rule concerns the command's private
data.
Every command registered by OpenOCD has its 'struct command' as
private data.

By ignoring commands without private data, we loose several TCL
commands registered by jimtcl, e.g. 'foreach', 'llength'.

By assuming that every command with non-NULL private data has
'struct command' as private data, we risk at best to access
inconsistent data, at worst to trigger a segmentation fault.

Export the already available functions:
- to check if a command has been registered by OpenOCD and
- to get the private data.
While there, rename jimcmd_is_ocd_command() as
jimcmd_is_oocd_command().
Don't filter-out jimtcl commands with no private data.
Check the private data only on OpenOCD commands.

Change-Id: Ib5bf8d2bc5c12440c0cfae438f637c38724a79b7
Signed-off-by: Antonio Borneo <borneo.antonio@gmail.com>
Reviewed-on: http://openocd.zylin.com/6282
Tested-by: jenkins
Reviewed-by: Tarek BOCHKATI <tarek.bouchkati@gmail.com>
src/helper/command.c
src/helper/command.h
src/server/telnet_server.c

index d4f6a0a52dc6d7b8bc47e812c42e12471ea8bfe8..43c5a0ffb7ee7f6452bebd164b47ac5e410bb5c5 100644 (file)
@@ -62,12 +62,12 @@ static inline bool jimcmd_is_proc(Jim_Cmd *cmd)
        return cmd->isproc;
 }
 
-static inline bool jimcmd_is_ocd_command(Jim_Cmd *cmd)
+bool jimcmd_is_oocd_command(Jim_Cmd *cmd)
 {
        return !cmd->isproc && cmd->u.native.cmdProc == jim_command_dispatch;
 }
 
-static inline void *jimcmd_privdata(Jim_Cmd *cmd)
+void *jimcmd_privdata(Jim_Cmd *cmd)
 {
        return cmd->isproc ? NULL : cmd->u.native.privData;
 }
@@ -261,7 +261,7 @@ static struct command *command_find_from_name(Jim_Interp *interp, const char *na
        Jim_IncrRefCount(jim_name);
        Jim_Cmd *cmd = Jim_GetCommand(interp, jim_name, JIM_NONE);
        Jim_DecrRefCount(interp, jim_name);
-       if (!cmd || jimcmd_is_proc(cmd) || !jimcmd_is_ocd_command(cmd))
+       if (!cmd || jimcmd_is_proc(cmd) || !jimcmd_is_oocd_command(cmd))
                return NULL;
 
        return jimcmd_privdata(cmd);
@@ -1020,7 +1020,7 @@ static int jim_command_mode(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
                Jim_Cmd *cmd = Jim_GetCommand(interp, s, JIM_NONE);
                Jim_DecrRefCount(interp, s);
                free(full_name);
-               if (!cmd || !(jimcmd_is_proc(cmd) || jimcmd_is_ocd_command(cmd))) {
+               if (!cmd || !(jimcmd_is_proc(cmd) || jimcmd_is_oocd_command(cmd))) {
                        Jim_SetResultString(interp, "unknown", -1);
                        return JIM_OK;
                }
index 068df9d0e3d4ff2006f18b2a8b82ff9617f899a5..719c94b6bb06f57c6be8d149d2769c5b31436985 100644 (file)
@@ -84,6 +84,17 @@ struct command_invocation {
        Jim_Obj *output;
 };
 
+/**
+ * Return true if the command @c cmd is registered by OpenOCD.
+ */
+bool jimcmd_is_oocd_command(Jim_Cmd *cmd);
+
+/**
+ * Return the pointer to the command's private data specified during the
+ * registration of command @a cmd .
+ */
+void *jimcmd_privdata(Jim_Cmd *cmd);
+
 /**
  * Command handlers may be defined with more parameters than the base
  * set provided by command.c.  This macro uses C99 magic to allow
index acb57efd10b6bf06c083cdf6442182e914f19366..2ad3791d708d0b05c5ba628f8ddca0a456d089bf 100644 (file)
@@ -500,22 +500,18 @@ static void telnet_auto_complete(struct connection *connection)
                bool ignore_cmd = false;
                Jim_Cmd *jim_cmd = Jim_GetCommand(command_context->interp, elem, JIM_NONE);
 
-               if (!jim_cmd)
+               if (!jim_cmd) {
+                       /* Why we are here? Let's ignore it! */
                        ignore_cmd = true;
-               else {
-                       if (!jim_cmd->isproc) {
-                               /* ignore commands without handler
-                                * and those with COMMAND_CONFIG mode */
-                               /* FIXME it's better to use jimcmd_is_ocd_command(jim_cmd)
-                                * or command_find_from_name(command_context->interp, name) */
-                               struct command *cmd = jim_cmd->u.native.privData;
-                               if (!cmd)
-                                       ignore_cmd = true;
-                               /* make Valgrind happy by checking that cmd is not NULL  */
-                               else if (cmd != NULL && !cmd->handler && !cmd->jim_handler)
-                                       ignore_cmd = true;
-                               else if (cmd != NULL && cmd->mode == COMMAND_CONFIG)
-                                       ignore_cmd = true;
+               } else if (jimcmd_is_oocd_command(jim_cmd)) {
+                       struct command *cmd = jimcmd_privdata(jim_cmd);
+
+                       if (cmd && !cmd->handler && !cmd->jim_handler) {
+                               /* Initial part of a multi-word command. Ignore it! */
+                               ignore_cmd = true;
+                       } else if (cmd && cmd->mode == COMMAND_CONFIG) {
+                               /* Not executable after config phase. Ignore it! */
+                               ignore_cmd = true;
                        }
                }