Add the current command to the command information
[openocd.git] / src / helper / command.c
index dcad6a198e39ff9de78f6a740263f66af2ece812..e55f9e73e48682fbb331c7fc5514b846416cae86 100644 (file)
@@ -36,7 +36,7 @@
 #endif
 
 // @todo the inclusion of target.h here is a layering violation
-#include "target.h"
+#include <target/target.h>
 #include "command.h"
 #include "configuration.h"
 #include "log.h"
@@ -580,6 +580,7 @@ static int run_command(struct command_context *context,
 
        struct command_invocation cmd = {
                        .ctx = context,
+                       .current = c,
                        .name = c->name,
                        .argc = num_words - 1,
                        .argv = words + 1,
@@ -860,13 +861,13 @@ static COMMAND_HELPER(command_help_find, struct command *head,
 }
 
 static COMMAND_HELPER(command_help_show, struct command *c, unsigned n,
-               bool show_help);
+               bool show_help, const char *match);
 
 static COMMAND_HELPER(command_help_show_list, struct command *head, unsigned n,
-               bool show_help)
+               bool show_help, const char *match)
 {
        for (struct command *c = head; NULL != c; c = c->next)
-               CALL_COMMAND_HANDLER(command_help_show, c, n, show_help);
+               CALL_COMMAND_HANDLER(command_help_show, c, n, show_help, match);
        return ERROR_OK;
 }
 
@@ -899,7 +900,7 @@ static void command_help_show_wrap(const char *str, unsigned n, unsigned n2)
        }
 }
 static COMMAND_HELPER(command_help_show, struct command *c, unsigned n,
-               bool show_help)
+               bool show_help, const char *match)
 {
        if (!command_can_run(CMD_CTX, c))
                return ERROR_OK;
@@ -908,18 +909,30 @@ static COMMAND_HELPER(command_help_show, struct command *c, unsigned n,
        if (NULL == cmd_name)
                return -ENOMEM;
 
-       command_help_show_indent(n);
-       LOG_USER_N("%s", cmd_name);
+       /* If the match string occurs anywhere, we print out
+        * stuff for this command. */
+       bool is_match = (strstr(cmd_name, match) != NULL) ||
+       ((c->usage != NULL) && (strstr(c->usage, match) != NULL)) ||
+       ((c->help != NULL) && (strstr(c->help, match) != NULL));
+       
+       if (is_match)
+       {
+               command_help_show_indent(n);
+               LOG_USER_N("%s", cmd_name);
+       }
        free(cmd_name);
 
-       if (c->usage) {
-               LOG_USER_N(" ");
-               command_help_show_wrap(c->usage, 0, n + 5);
+       if (is_match)
+       {
+               if (c->usage) {
+                       LOG_USER_N(" ");
+                       command_help_show_wrap(c->usage, 0, n + 5);
+               }
+               else
+                       LOG_USER_N("\n");
        }
-       else
-               LOG_USER_N("\n");
 
-       if (show_help)
+       if (is_match && show_help)
        {
                const char *stage_msg;
                switch (c->mode) {
@@ -942,7 +955,7 @@ static COMMAND_HELPER(command_help_show, struct command *c, unsigned n,
                return ERROR_OK;
 
        return CALL_COMMAND_HANDLER(command_help_show_list,
-                       c->children, n, show_help);
+                       c->children, n, show_help, match);
 }
 COMMAND_HANDLER(handle_help_command)
 {
@@ -950,14 +963,15 @@ COMMAND_HANDLER(handle_help_command)
 
        struct command *c = CMD_CTX->commands;
 
-       if (0 == CMD_ARGC)
-               return CALL_COMMAND_HANDLER(command_help_show_list, c, 0, full);
-
-       int retval = CALL_COMMAND_HANDLER(command_help_find, c, &c);
-       if (ERROR_OK != retval)
-               return retval;
-
-       return CALL_COMMAND_HANDLER(command_help_show, c, 0, full);
+       const char *match = "";
+       if (CMD_ARGC == 0)
+               match = "";
+       else if (CMD_ARGC == 1)
+               match = CMD_ARGV[0]; 
+       else
+               return ERROR_COMMAND_SYNTAX_ERROR;
+               
+       return CALL_COMMAND_HANDLER(command_help_show_list, c, 0, full, match);
 }
 
 static int command_unknown_find(unsigned argc, Jim_Obj *const *argv,
@@ -975,6 +989,7 @@ static int command_unknown_find(unsigned argc, Jim_Obj *const *argv,
        return command_unknown_find(--argc, ++argv, (*out)->children, out, false);
 }
 
+
 static int command_unknown(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
 {
        const char *cmd_name = Jim_GetString(argv[0], NULL);
@@ -1272,7 +1287,7 @@ static const struct command_registration command_builtin_handlers[] = {
        COMMAND_REGISTRATION_DONE
 };
 
-struct command_context* command_init(const char *startup_tcl)
+struct command_context* command_init(const char *startup_tcl, Jim_Interp *interp)
 {
        struct command_context* context = malloc(sizeof(struct command_context));
        const char *HostOs;
@@ -1284,14 +1299,18 @@ struct command_context* command_init(const char *startup_tcl)
        context->output_handler_priv = NULL;
 
 #if !BUILD_ECOSBOARD
-       Jim_InitEmbedded();
-       /* Create an interpreter */
-       context->interp = Jim_CreateInterp();
-       /* Add all the Jim core commands */
-       Jim_RegisterCoreCommands(context->interp);
+       /* Create a jim interpreter if we were not handed one */
+       if (interp == NULL)
+       {
+               Jim_InitEmbedded();
+               /* Create an interpreter */
+               interp = Jim_CreateInterp();
+               /* Add all the Jim core commands */
+               Jim_RegisterCoreCommands(interp);
+       }
 #endif
+       context->interp = interp;
 
-       Jim_Interp *interp = context->interp;
 #if defined(_MSC_VER)
        /* WinXX - is generic, the forward
         * looking problem is this:

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)