maintain command lists in sorted order
[openocd.git] / src / helper / command.c
index f135bb03cb2f8dbed1e7d75a2d2ed042e3e424ef..f6c6b2d7af8332ee7ebda6dcdcf3d070d2ae636a 100644 (file)
@@ -172,38 +172,13 @@ static int script_command(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
        return (retval == ERROR_OK)?JIM_OK:JIM_ERR;
 }
 
-static Jim_Obj *command_name_list(struct command *c)
-{
-       Jim_Obj *cmd_list = c->parent ?
-                       command_name_list(c->parent) :
-                       Jim_NewListObj(interp, NULL, 0);
-       Jim_ListAppendElement(interp, cmd_list,
-                       Jim_NewStringObj(interp, c->name, -1));
-
-       return cmd_list;
-}
-
-static void command_helptext_add(Jim_Obj *cmd_list, const char *help)
-{
-       Jim_Obj *cmd_entry = Jim_NewListObj(interp, NULL, 0);
-       Jim_ListAppendElement(interp, cmd_entry, cmd_list);
-       Jim_ListAppendElement(interp, cmd_entry,
-                       Jim_NewStringObj(interp, help ? : "", -1));
-
-       /* accumulate help text in Tcl helptext list.  */
-       Jim_Obj *helptext = Jim_GetGlobalVariableStr(interp,
-                       "ocd_helptext", JIM_ERRMSG);
-       if (Jim_IsShared(helptext))
-               helptext = Jim_DuplicateObj(interp, helptext);
-       Jim_ListAppendElement(interp, helptext, cmd_entry);
-}
-
 /* nice short description of source file */
 #define __THIS__FILE__ "command.c"
 
 /**
  * Find a command by name from a list of commands.
- * @returns The named command if found, or NULL.
+ * @returns Returns the named command if it exists in the list.
+ * Returns NULL otherwise.
  */
 static struct command *command_find(struct command *head, const char *name)
 {
@@ -216,9 +191,10 @@ static struct command *command_find(struct command *head, const char *name)
 }
 
 /**
- * Add the command to the end of linked list.
- * @returns Returns false if the named command already exists in the list.
- * Returns true otherwise.
+ * Add the command into the linked list, sorted by name.
+ * @param head Address to head of command list pointer, which may be
+ * updated if @c c gets inserted at the beginning of the list.
+ * @param c The command to add to the list pointed to by @c head.
  */
 static void command_add_child(struct command **head, struct command *c)
 {
@@ -228,9 +204,17 @@ static void command_add_child(struct command **head, struct command *c)
                *head = c;
                return;
        }
-       struct command *cc = *head;
-       while (cc->next) cc = cc->next;
-       cc->next = c;
+
+       while ((*head)->next && (strcmp(c->name, (*head)->name) > 0))
+               head = &(*head)->next;
+
+       if (strcmp(c->name, (*head)->name) > 0) {
+               c->next = (*head)->next;
+               (*head)->next = c;
+       } else {
+               c->next = *head;
+               *head = c;
+       }
 }
 
 static struct command **command_list_for_parent(
@@ -258,8 +242,6 @@ static struct command *command_new(struct command_context *cmd_ctx,
 
        command_add_child(command_list_for_parent(cmd_ctx, parent), c);
 
-       command_helptext_add(command_name_list(c), help);
-
        return c;
 }
 static void command_free(struct command *c)
@@ -771,6 +753,66 @@ COMMAND_HANDLER(handle_help_command)
        return CALL_COMMAND_HANDLER(command_help_show, c, 0);
 }
 
+
+int help_add_command(struct command_context *cmd_ctx, struct command *parent,
+               const char *cmd_name, const char *help_text)
+{
+       struct command **head = command_list_for_parent(cmd_ctx, parent);
+       struct command *nc = command_find(*head, cmd_name);
+       if (NULL == nc)
+       {
+               // add a new command with help text
+               nc = register_command(cmd_ctx, parent, cmd_name,
+                               NULL, COMMAND_ANY, help_text);
+               if (NULL == nc)
+               {
+                       LOG_ERROR("failed to add '%s' help text", cmd_name);
+                       return ERROR_FAIL;
+               }
+               LOG_DEBUG("added '%s' help text", cmd_name);
+       }
+       else
+       {
+               bool replaced = false;
+               if (nc->help)
+               {
+                       free((void *)nc->help);
+                       replaced = true;
+               }
+               nc->help = strdup(help_text);
+
+               if (replaced)
+                       LOG_INFO("replaced existing '%s' help", cmd_name);
+               else
+                       LOG_DEBUG("added '%s' help text", cmd_name);
+       }
+       return ERROR_OK;
+}
+
+COMMAND_HANDLER(handle_help_add_command)
+{
+       if (CMD_ARGC < 2)
+       {
+               LOG_ERROR("%s: insufficient arguments", CMD_NAME);
+               return ERROR_INVALID_ARGUMENTS;
+       }
+
+       // save help text and remove it from argument list
+       const char *help_text = CMD_ARGV[--CMD_ARGC];
+       // likewise for the leaf command name
+       const char *cmd_name = CMD_ARGV[--CMD_ARGC];
+
+       struct command *c = NULL;
+       if (CMD_ARGC > 0)
+       {
+               c = CMD_CTX->commands;
+               int retval = CALL_COMMAND_HANDLER(command_help_find, c, &c);
+               if (ERROR_OK != retval)
+                       return retval;
+       }
+       return help_add_command(CMD_CTX, c, cmd_name, help_text);
+}
+
 /* sleep command sleeps for <n> miliseconds
  * this is useful in target startup scripts
  */
@@ -866,15 +908,22 @@ struct command_context* command_init(const char *startup_tcl)
        interp->cb_fflush = openocd_jim_fflush;
        interp->cb_fgets = openocd_jim_fgets;
 
+       register_command(context, NULL, "add_help_text",
+                       handle_help_add_command, COMMAND_ANY,
+                       "<command> [...] <help_text>] - "
+                       "add new command help text");
+
 #if !BUILD_ECOSBOARD
        Jim_EventLoopOnLoad(interp);
 #endif
+       Jim_SetAssocData(interp, "context", NULL, context);
        if (Jim_Eval_Named(interp, startup_tcl, "embedded:startup.tcl",1) == JIM_ERR)
        {
                LOG_ERROR("Failed to run startup.tcl (embedded into OpenOCD)");
                Jim_PrintErrorMessage(interp);
                exit(-1);
        }
+       Jim_DeleteAssocData(interp, "context");
 
        register_command(context, NULL, "sleep",
                        handle_sleep_command, COMMAND_ANY,
@@ -920,7 +969,7 @@ void register_jim(struct command_context *cmd_ctx, const char *name,
        Jim_ListAppendElement(interp, cmd_list,
                        Jim_NewStringObj(interp, name, -1));
 
-       command_helptext_add(cmd_list, help);
+       help_add_command(cmd_ctx, NULL, name, help);
 }
 
 #define DEFINE_PARSE_NUM_TYPE(name, type, func, min, max) \

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)