add add_help_text command handler
authorZachary T Welch <zw@superlucidity.net>
Thu, 19 Nov 2009 15:23:25 +0000 (07:23 -0800)
committerZachary T Welch <zw@superlucidity.net>
Fri, 20 Nov 2009 23:03:35 +0000 (15:03 -0800)
Rewrite means for scripts to register help text for commands.  These
cause the new commands to be stored in the command heirarchy, with
built-in commands; however, they will never be invoked there because
they do not receive a command handler.  The same trick is used for
the Jim commands.

Remove the old helpers that were used to register commands.

src/helper/command.c
src/helper/startup.tcl

index 0958147ea116e3183ef13eda30acec6ec2f4b230..d8b78759e28ae0ccf0123ae969d88fb911ec6c98 100644 (file)
@@ -172,32 +172,6 @@ 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"
 
@@ -258,8 +232,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 +743,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,6 +898,11 @@ 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
@@ -922,7 +959,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) \
index ddfef1dc9ae1181d537f58d21b12a1e12e9c2e07..fc84943a5d3891056ba55b4e57b09efa4f30eb19 100644 (file)
@@ -3,21 +3,6 @@
 # Embedded into OpenOCD executable
 #
 
-# Help text list. A list of command + help text pairs.
-#
-# Commands can be more than one word and they are stored
-# as "flash banks" "help text x x x"
-
-proc add_help_text {cmd cmd_help} {
-       global ocd_helptext
-       lappend ocd_helptext [list $cmd $cmd_help]
-}
-
-proc get_help_text {} {
-       global ocd_helptext
-       return $ocd_helptext
-}
-
 
 # We need to explicitly redirect this to the OpenOCD command
 # as Tcl defines the exit proc
@@ -25,6 +10,7 @@ proc exit {} {
        ocd_throw exit
 }
 
+# Help text list. A list of command + help text pairs.
 proc cmd_help {cmdname h indent} {
        set indent [expr $indent * 2]
 

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)