The command_name function returns a malloced string for a given
command and its parents.  This can be used to display a message
to the user, but it is used internally to handle syntax errors.

Signed-off-by: Zachary T Welch <z...@superlucidity.net>
---
 src/helper/command.c |   38 +++++++++++++++++++++++++++-----------
 src/helper/command.h |    8 ++++++++
 2 files changed, 35 insertions(+), 11 deletions(-)

diff --git a/src/helper/command.c b/src/helper/command.c
index 93301b0..1dbd801 100644
--- a/src/helper/command.c
+++ b/src/helper/command.c
@@ -408,6 +408,27 @@ void command_print(command_context_t *context, const char 
*format, ...)
        va_end(ap);
 }
 
+static char *__command_name(struct command_s *c, unsigned extra)
+{
+       char *name;
+       unsigned len = strlen(c->name);
+       if (NULL == c->parent) {
+               // allocate enough for the name, child names, and '\0'
+               name = malloc(len + extra + 1);
+               strcpy(name, c->name);
+       } else {
+               // parent's extra must include both the space and name
+               name = __command_name(c->parent, 1 + len + extra);
+               strcat(name, " ");
+               strcat(name, c->name);
+       }
+       return name;
+}
+char *command_name(struct command_s *c)
+{
+       return __command_name(c, 0);
+}
+
 static int run_command(command_context_t *context,
                command_t *c, const char *words[], unsigned num_words)
 {
@@ -425,17 +446,12 @@ static int run_command(command_context_t *context,
        if (retval == ERROR_COMMAND_SYNTAX_ERROR)
        {
                /* Print help for command */
-               const char *t1="";
-               const char *t2="";
-               const char *t3="";
-               /* maximum of two levels :-) */
-               if (c->parent != NULL)
-               {
-                       t1 = c->parent->name;
-                       t2=" ";
-               }
-               t3 = c->name;
-               command_run_linef(context, "help {%s%s%s}", t1, t2, t3);
+               char *full_name = command_name(c);
+               if (NULL != full_name) {
+                       command_run_linef(context, "help %s", full_name);
+                       free(full_name);
+               } else
+                       retval = -ENOMEM;
        }
        else if (retval == ERROR_COMMAND_CLOSE_CONNECTION)
        {
diff --git a/src/helper/command.h b/src/helper/command.h
index a5fe997..a8efeab 100644
--- a/src/helper/command.h
+++ b/src/helper/command.h
@@ -132,6 +132,14 @@ typedef struct command_s
        struct command_s *next;
 } command_t;
 
+/**
+ * @returns A malloc'd string containing the full command name,
+ * which may include one or more ancestor components.  Multiple names
+ * are separated by single spaces.  The caller must free() the string
+ * when done with it.
+ */
+char *command_name(struct command_s *c);
+
 command_t* register_command(command_context_t *context,
                command_t *parent, char *name, command_handler_t handler,
                enum command_mode mode, char *help);
-- 
1.6.4.4

_______________________________________________
Openocd-development mailing list
Openocd-development@lists.berlios.de
https://lists.berlios.de/mailman/listinfo/openocd-development

Reply via email to