improve command registration
authorZachary T Welch <zw@superlucidity.net>
Wed, 11 Nov 2009 18:20:55 +0000 (10:20 -0800)
committerZachary T Welch <zw@superlucidity.net>
Wed, 11 Nov 2009 21:50:28 +0000 (13:50 -0800)
Eliminate duplicate code for linking commands into a list.

Adds a check to ensure the command does not already exist;
if it does, return that one instead of creating a duplicate.

src/helper/command.c

index 5ac5c3015d43c6b1b41a398b7c650baad9ddad47..f960127d8b9e20cdabecb999048fb95323d16dc4 100644 (file)
@@ -191,13 +191,53 @@ static void command_helptext_add(Jim_Obj *cmd_list, const char *help)
 /* nice short description of source file */
 #define __THIS__FILE__ "command.c"
 
 /* nice short description of source file */
 #define __THIS__FILE__ "command.c"
 
-command_t* register_command(command_context_t *context, command_t *parent, char *name, int (*handler)(struct command_context_s *context, char* name, char** args, int argc), enum command_mode mode, char *help)
+/**
+ * Find a command by name from a list of commands.
+ * @returns The named command if found, or NULL.
+ */
+static struct command_s *command_find(struct command_s **head, const char *name)
+{
+       assert(head);
+       for (struct command_s *cc = *head; cc; cc = cc->next)
+       {
+               if (strcmp(cc->name, name) == 0)
+                       return cc;
+       }
+       return NULL;
+}
+
+/**
+ * Add the command to the end of linked list.
+ * @returns Returns false if the named command already exists in the list.
+ * Returns true otherwise.
+ */
+static void command_add_child(struct command_s **head, struct command_s *c)
 {
 {
-       command_t *c, *p;
+       assert(head);
+       if (NULL == *head)
+       {
+               *head = c;
+               return;
+       }
+       struct command_s *cc = *head;
+       while (cc->next) cc = cc->next;
+       cc->next = c;
+}
 
 
+command_t* register_command(command_context_t *context,
+               command_t *parent, char *name,
+               int (*handler)(struct command_context_s *context,
+                               char* name, char** args, int argc),
+               enum command_mode mode, char *help)
+{
        if (!context || !name)
                return NULL;
 
        if (!context || !name)
                return NULL;
 
+       struct command_s **head = parent ? &parent->children : &context->commands;
+       struct command_s *c = command_find(head, name);
+       if (NULL != c)
+               return c;
+
        c = malloc(sizeof(command_t));
 
        c->name = strdup(name);
        c = malloc(sizeof(command_t));
 
        c->name = strdup(name);
@@ -207,35 +247,7 @@ command_t* register_command(command_context_t *context, command_t *parent, char
        c->mode = mode;
        c->next = NULL;
 
        c->mode = mode;
        c->next = NULL;
 
-       /* place command in tree */
-       if (parent)
-       {
-               if (parent->children)
-               {
-                       /* find last child */
-                       for (p = parent->children; p && p->next; p = p->next);
-                       if (p)
-                               p->next = c;
-               }
-               else
-               {
-                       parent->children = c;
-               }
-       }
-       else
-       {
-               if (context->commands)
-               {
-                       /* find last command */
-                       for (p = context->commands; p && p->next; p = p->next);
-                       if (p)
-                               p->next = c;
-               }
-               else
-               {
-                       context->commands = c;
-               }
-       }
+       command_add_child(head, c);
 
        command_helptext_add(command_name_list(c), help);
 
 
        command_helptext_add(command_name_list(c), help);
 

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)