add struct command_registration
authorZachary T Welch <zw@superlucidity.net>
Fri, 20 Nov 2009 19:23:34 +0000 (11:23 -0800)
committerZachary T Welch <zw@superlucidity.net>
Wed, 25 Nov 2009 05:37:29 +0000 (21:37 -0800)
Add a structure to encapsulate command registration information, rather
than passing them all as parameters.  Enables further API changes that
require additional required or optional parameters.

Updates the register_command API and COMMAND_REGISTER macro to use it,
along with their documentation.

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

index 0561c6c5bcbcb53a3e80bb870da64b6fe91649a1..3df60b65a9c02b6ed0d75d631eee256c4024e72d 100644 (file)
@@ -263,13 +263,12 @@ static void command_free(struct command *c)
 }
 
 struct command* register_command(struct command_context *context,
-               struct command *parent, const char *name,
-               command_handler_t handler, enum command_mode mode,
-               const char *help)
+               struct command *parent, const struct command_registration *cr)
 {
-       if (!context || !name)
+       if (!context || !cr->name)
                return NULL;
 
+       const char *name = cr->name;
        struct command **head = command_list_for_parent(context, parent);
        struct command *c = command_find(*head, name);
        if (NULL != c)
@@ -279,7 +278,7 @@ struct command* register_command(struct command_context *context,
                return c;
        }
 
-       c = command_new(context, parent, name, handler, mode, help);
+       c = command_new(context, parent, name, cr->handler, cr->mode, cr->help);
        /* if allocation failed or it is a placeholder (no handler), we're done */
        if (NULL == c || NULL == c->handler)
                return c;
@@ -762,8 +761,12 @@ int help_add_command(struct command_context *cmd_ctx, struct command *parent,
        if (NULL == nc)
        {
                // add a new command with help text
-               nc = register_command(cmd_ctx, parent, cmd_name,
-                               NULL, COMMAND_ANY, help_text);
+               struct command_registration cr = {
+                               .name = cmd_name,
+                               .mode = COMMAND_ANY,
+                               .help = help_text,
+                       };
+               nc = register_command(cmd_ctx, parent, &cr);
                if (NULL == nc)
                {
                        LOG_ERROR("failed to add '%s' help text", cmd_name);
index 25c05011d649afa7b165206822e573d06d1f96f5..b57ca75d85a3df6ea80cf22d855e2f747b4c19a5 100644 (file)
@@ -177,13 +177,9 @@ struct command
  */
 char *command_name(struct command *c, char delim);
 
-/**
- * Register a command @c handler that can be called from scripts during
- * the execution @c mode specified.
- *
- * If @c parent is non-NULL, the new command will be registered as a
- * sub-command under it; otherwise, it will be available as a top-level
- * command.
+/*
+ * Commands should be registered by filling in one or more of these
+ * structures and passing them to register_command().
  *
  * A conventioal format should be used for help strings, to provide both
  * usage and basic information:
@@ -191,25 +187,48 @@ char *command_name(struct command *c, char delim);
  * "@<options@> ... - some explanation text"
  * @endcode
  *
- * @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
- * register a top-level command.
  * @param name The name of the command to register, which must not have
- * been registered previously.
+ * been registered previously in the intended context.
  * @param handler The callback function that will be called.  If NULL,
  * then the command serves as a placeholder for its children or a script.
  * @param mode The command mode(s) in which this command may be run.
  * @param help The help text that will be displayed to the user.
+ */
+struct command_registration {
+       const char *name;
+       command_handler_t handler;
+       enum command_mode mode;
+       const char *help;
+};
+
+/**
+ * Register a command @c handler that can be called from scripts during
+ * the execution @c mode specified.
+ *
+ * If @c parent is non-NULL, the new command will be registered as a
+ * sub-command under it; otherwise, it will be available as a top-level
+ * 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
+ * register a top-level command.
+ * @param rec A command_registration record that contains the desired
+ * command parameters.
  * @returns The new command, if successful; otherwise, NULL.
  */
 struct command* register_command(struct command_context *cmd_ctx,
-               struct command *parent, const char *name,
-               command_handler_t handler, enum command_mode mode,
-               const char *help);
+               struct command *parent, const struct command_registration *rec);
 
-// provide a simple shim, for now; allows parameters to be migrated
 #define COMMAND_REGISTER(_cmd_ctx, _parent, _name, _handler, _mode, _help) \
-               register_command(_cmd_ctx, _parent, _name, _handler, _mode, _help)
+       ({ \
+               struct command_registration cr = { \
+                               .name = _name, \
+                               .handler = _handler, \
+                               .mode = _mode, \
+                               .help = _help, \
+                       }; \
+               register_command(_cmd_ctx, _parent, &cr); \
+       })
 
 /**
  * Unregisters command @c name from the given context, @c cmd_ctx.

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)