server/telnet: add variables auto-completion
[openocd.git] / src / server / telnet_server.c
index 13fbc3fda61fe0d42acc5b6873cc72085233ac01..2ebcff1633df1ad76df344eecdc7cc6336de7419 100644 (file)
@@ -592,16 +592,30 @@ static void telnet_auto_complete(struct connection *connection)
 
        LIST_HEAD(matches);
 
 
        LIST_HEAD(matches);
 
-       /* user command sequence, either at line beginning
-        * or we start over after these characters ';', '[', '{' */
+       /* - user command sequence, either at line beginning
+        *   or we start over after these characters ';', '[', '{'
+        * - user variable sequence, start after the character '$'
+        *   and do not contain white spaces */
+       bool is_variable_auto_completion = false;
+       bool have_spaces = false;
        size_t seq_start = (t_con->line_cursor == 0) ? 0 : (t_con->line_cursor - 1);
        size_t seq_start = (t_con->line_cursor == 0) ? 0 : (t_con->line_cursor - 1);
-       while (seq_start > 0) {
+       while (1) {
                char c = t_con->line[seq_start];
                char c = t_con->line[seq_start];
+
                if (c == ';' || c == '[' || c == '{') {
                        seq_start++;
                        break;
                if (c == ';' || c == '[' || c == '{') {
                        seq_start++;
                        break;
+               } else if (c == ' ') {
+                       have_spaces = true;
+               } else if (c == '$' && !have_spaces) {
+                       is_variable_auto_completion = true;
+                       seq_start++;
+                       break;
                }
 
                }
 
+               if (seq_start == 0)
+                       break;
+
                seq_start--;
        }
 
                seq_start--;
        }
 
@@ -631,7 +645,12 @@ static void telnet_auto_complete(struct connection *connection)
        query[usr_cmd_len] = '\0';
 
        /* filter commands */
        query[usr_cmd_len] = '\0';
 
        /* filter commands */
-       char *query_cmd = alloc_printf("_telnet_autocomplete_helper {%s*}", query);
+       char *query_cmd;
+
+       if (is_variable_auto_completion)
+               query_cmd = alloc_printf("lsort [info vars {%s*}]", query);
+       else
+               query_cmd = alloc_printf("_telnet_autocomplete_helper {%s*}", query);
 
        if (!query_cmd) {
                LOG_ERROR("Out of memory");
 
        if (!query_cmd) {
                LOG_ERROR("Out of memory");
@@ -659,20 +678,22 @@ static void telnet_auto_complete(struct connection *connection)
 
                /* validate the command */
                bool ignore_cmd = false;
 
                /* validate the command */
                bool ignore_cmd = false;
-               Jim_Cmd *jim_cmd = Jim_GetCommand(command_context->interp, elem, JIM_NONE);
-
-               if (!jim_cmd) {
-                       /* Why we are here? Let's ignore it! */
-                       ignore_cmd = true;
-               } else if (jimcmd_is_oocd_command(jim_cmd)) {
-                       struct command *cmd = jimcmd_privdata(jim_cmd);
+               if (!is_variable_auto_completion) {
+                       Jim_Cmd *jim_cmd = Jim_GetCommand(command_context->interp, elem, JIM_NONE);
 
 
-                       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! */
+                       if (!jim_cmd) {
+                               /* Why we are here? Let's ignore it! */
                                ignore_cmd = true;
                                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;
+                               }
                        }
                }
 
                        }
                }
 

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)