]> git.gag.com Git - fw/openocd/commitdiff
add command registration chaining
authorZachary T Welch <zw@superlucidity.net>
Sat, 21 Nov 2009 21:59:51 +0000 (13:59 -0800)
committerZachary T Welch <zw@superlucidity.net>
Wed, 25 Nov 2009 05:37:30 +0000 (21:37 -0800)
Adds the ability to chain registration structures.  Modules can define a
command with the 'chain' and 'num_chain' fields defined in their
registration table, and the register_commands() function will initialize
these commands.  If the registration record creates a new command, then
the chained commands are created under it; otherwise, they are created
in the same context as the other commands (i.e. the parent argument).

src/helper/command.c
src/helper/command.h

index 9cc996c226fbe563ad79a87b95d771574f53bc3c..b81d2d19591eba1249ab87413a3e8ee7d7571aa4 100644 (file)
@@ -331,18 +331,36 @@ struct command* register_command(struct command_context *context,
 int register_commands(struct command_context *cmd_ctx, struct command *parent,
                const struct command_registration *cmds)
 {
+       int retval = ERROR_OK;
        unsigned i;
-       for (i = 0; cmds[i].name; i++)
+       for (i = 0; cmds[i].name || cmds[i].chain; i++)
        {
-               struct command *c = register_command(cmd_ctx, parent, cmds + i);
-               if (NULL != c)
-                       continue;
+               const struct command_registration *cr = cmds + i;
 
+               struct command *c = NULL;
+               if (NULL != cr->name)
+               {
+                       c = register_command(cmd_ctx, parent, cr);
+                       if (NULL == c)
+                       {
+                               retval = ERROR_FAIL;
+                               break;
+                       }
+               }
+               if (NULL != cr->chain)
+               {
+                       struct command *p = c ? : parent;
+                       retval = register_commands(cmd_ctx, p, cr->chain);
+                       if (ERROR_OK != retval)
+                               break;
+               }
+       }
+       if (ERROR_OK != retval)
+       {
                for (unsigned j = 0; j < i; j++)
                        unregister_command(cmd_ctx, parent, cmds[j].name);
-               return ERROR_FAIL;
        }
-       return ERROR_OK;
+       return retval;
 }
 
 int unregister_all_commands(struct command_context *context,
index 7baa92d506211dfdc590158670614647867f3de1..6e3e93afaec70253a3fffc969b31cbd08443d6bc 100644 (file)
@@ -202,10 +202,18 @@ struct command_registration {
        const char *help;
        /// a string listing the options and arguments, required or optional
        const char *usage;
+
+       /**
+        * If non-NULL, the commands in @c chain will be registered in
+        * the same context and scope of this registration record.
+        * This allows modules to inherit lists commands from other
+        * modules.
+        */
+       const struct command_registration *chain;
 };
 
 /// Use this as the last entry in an array of command_registration records.
-#define COMMAND_REGISTRATION_DONE { .name = NULL }
+#define COMMAND_REGISTRATION_DONE { .name = NULL, .chain = NULL }
 
 /**
  * Register a command @c handler that can be called from scripts during
@@ -238,7 +246,10 @@ struct command* register_command(struct command_context *cmd_ctx,
 
 /**
  * Register one or more commands in the specified context, as children
- * of @c parent (or top-level commends, if NULL).
+ * of @c parent (or top-level commends, if NULL).  In a registration's
+ * record contains a non-NULL @c chain member and name is NULL, the
+ * commands on the chain will be registered in the same context.
+ * Otherwise, the chained commands are added as children of the command.
  *
  * @param cmd_ctx The command_context in which to register the command.
  * @param parent Register this command as a child of this, or NULL to