]> granicus.if.org Git - neomutt/commitdiff
Convert GroupContext to use STAILQ
authorBo YU <yuzibode@126.com>
Fri, 4 May 2018 10:56:06 +0000 (18:56 +0800)
committerRichard Russon <rich@flatcap.org>
Wed, 12 Dec 2018 12:55:54 +0000 (12:55 +0000)
group.c
group.h
init.c
init.h

diff --git a/group.c b/group.c
index 7ed3f0dd255baf8b3f2fe09941ac4ac4704056e1..87ea2cddcc6ac97c6dee7fe99e04f3a3efd11237 100644 (file)
--- a/group.c
+++ b/group.c
@@ -79,18 +79,20 @@ static void group_remove(struct Group *g)
 
 /**
  * mutt_group_context_clear - Empty a Group List
- * @param ctx Group List to modify
+ * @param head Group List to modify
  * @retval 0 Always
  */
-int mutt_group_context_clear(struct GroupContext **ctx)
+int mutt_group_context_clear(struct GroupContextHead *head)
 {
-  struct GroupContext *t = NULL;
-  for (; ctx && *ctx; (*ctx) = t)
+  struct GroupContext *np = STAILQ_FIRST(head), *next = NULL;
+  while (np)
   {
-    group_remove((*ctx)->g);
-    t = (*ctx)->next;
-    FREE(ctx);
+    group_remove(np->g);
+    next = STAILQ_NEXT(np, entries);
+    FREE(&np);
+    np = next;
   }
+  STAILQ_INIT(head);
   return 0;
 }
 
@@ -108,33 +110,36 @@ static bool empty_group(struct Group *g)
 
 /**
  * mutt_group_context_add - Add a Group to a List
- * @param ctx   Group List
+ * @param head  Group List
  * @param group Group to add
  */
-void mutt_group_context_add(struct GroupContext **ctx, struct Group *group)
+void mutt_group_context_add(struct GroupContextHead *head, struct Group *group)
 {
-  for (; *ctx; ctx = &((*ctx)->next))
+  struct GroupContext *np = NULL;
+  STAILQ_FOREACH(np, head, entries)
   {
-    if ((*ctx)->g == group)
+    if (np->g == group)
       return;
   }
-
-  *ctx = mutt_mem_calloc(1, sizeof(struct GroupContext));
-  (*ctx)->g = group;
+  np = mutt_mem_calloc(1, sizeof(struct GroupContext));
+  np->g = group;
+  STAILQ_INSERT_TAIL(head, np, entries);
 }
 
 /**
  * mutt_group_context_destroy - Destroy a Group List
- * @param ctx Group List to destroy
+ * @param head Group List to destroy
  */
-void mutt_group_context_destroy(struct GroupContext **ctx)
+void mutt_group_context_destroy(struct GroupContextHead *head)
 {
-  struct GroupContext *p = NULL;
-  for (; *ctx; *ctx = p)
+  struct GroupContext *np = STAILQ_FIRST(head), *next = NULL;
+  while (np)
   {
-    p = (*ctx)->next;
-    FREE(ctx);
+    next = STAILQ_NEXT(np, entries);
+    FREE(&np);
+    np = next;
   }
+  STAILQ_INIT(head);
 }
 
 /**
@@ -209,74 +214,84 @@ static int group_remove_regex(struct Group *g, const char *s)
 
 /**
  * mutt_group_context_add_addrlist - Add an Address List to a Group List
- * @param ctx Group List to add to
- * @param a   Address List to add
+ * @param head Group List to add to
+ * @param a    Address List to add
  */
-void mutt_group_context_add_addrlist(struct GroupContext *ctx, struct Address *a)
+void mutt_group_context_add_addrlist(struct GroupContextHead *head, struct Address *a)
 {
-  for (; ctx; ctx = ctx->next)
-    group_add_addrlist(ctx->g, a);
+  struct GroupContext *np = NULL;
+  STAILQ_FOREACH(np, head, entries)
+  {
+    group_add_addrlist(np->g, a);
+  }
 }
 
 /**
  * mutt_group_context_remove_addrlist - Remove an Address List from a Group List
- * @param ctx Group List to modify
+ * @param head Group List to modify
  * @param a   Address List to remove
  * @retval  0 Success
  * @retval -1 Error
  */
-int mutt_group_context_remove_addrlist(struct GroupContext *ctx, struct Address *a)
+int mutt_group_context_remove_addrlist(struct GroupContextHead *head, struct Address *a)
 {
   int rc = 0;
+  struct GroupContext *np = NULL;
 
-  for (; (!rc) && ctx; ctx = ctx->next)
+  STAILQ_FOREACH(np, head, entries)
   {
-    rc = group_remove_addrlist(ctx->g, a);
-    if (empty_group(ctx->g))
-      group_remove(ctx->g);
+    rc = group_remove_addrlist(np->g, a);
+    if (empty_group(np->g))
+      group_remove(np->g);
+    if (rc)
+      return rc;
   }
-
   return rc;
 }
 
 /**
  * mutt_group_context_add_regex - Add a Regex to a Group List
- * @param ctx   Group List to add to
+ * @param head  Group List to add to
  * @param s     Regex string to add
  * @param flags Flags, e.g. REG_ICASE
  * @param err   Buffer for error message
  * @retval  0 Success
  * @retval -1 Error
  */
-int mutt_group_context_add_regex(struct GroupContext *ctx, const char *s,
+int mutt_group_context_add_regex(struct GroupContextHead *head, const char *s,
                                  int flags, struct Buffer *err)
 {
   int rc = 0;
 
-  for (; (!rc) && ctx; ctx = ctx->next)
-    rc = group_add_regex(ctx->g, s, flags, err);
-
+  struct GroupContext *np = NULL;
+  STAILQ_FOREACH(np, head, entries)
+  {
+    rc = group_add_regex(np->g, s, flags, err);
+    if (rc)
+      return rc;
+  }
   return rc;
 }
 
 /**
  * mutt_group_context_remove_regex - Remove a Regex from a Group List
- * @param ctx Group List to modify
- * @param s   Regex string to remove
+ * @param head Group List to modify
+ * @param s    Regex string to remove
  * @retval  0 Success
  * @retval -1 Error
  */
-int mutt_group_context_remove_regex(struct GroupContext *ctx, const char *s)
+int mutt_group_context_remove_regex(struct GroupContextHead *head, const char *s)
 {
   int rc = 0;
-
-  for (; (!rc) && ctx; ctx = ctx->next)
+  struct GroupContext *np = NULL;
+  STAILQ_FOREACH(np, head, entries)
   {
-    rc = group_remove_regex(ctx->g, s);
-    if (empty_group(ctx->g))
-      group_remove(ctx->g);
+    rc = group_remove_regex(np->g, s);
+    if (empty_group(np->g))
+      group_remove(np->g);
+    if (rc)
+      return rc;
   }
-
   return rc;
 }
 
diff --git a/group.h b/group.h
index 6cb404e326df46ccac4c81777b744d31f463b177..b2027b7c97be12fec9c62a409f8ccc42ae689c2a 100644 (file)
--- a/group.h
+++ b/group.h
@@ -48,19 +48,22 @@ struct Group
 struct GroupContext
 {
   struct Group *g;
-  struct GroupContext *next;
+  STAILQ_ENTRY(GroupContext) entries;
 };
 
-void mutt_group_context_add(struct GroupContext **ctx, struct Group *group);
-void mutt_group_context_destroy(struct GroupContext **ctx);
-void mutt_group_context_add_addrlist(struct GroupContext *ctx, struct Address *a);
-int mutt_group_context_add_regex(struct GroupContext *ctx, const char *s, int flags, struct Buffer *err);
+STAILQ_HEAD(GroupContextHead, GroupContext);
+
+void mutt_group_context_add(struct GroupContextHead *head, struct Group *group);
+void mutt_group_context_destroy(struct GroupContextHead *head);
+void mutt_group_context_add_addrlist(struct GroupContextHead *head, struct Address *a);
+int mutt_group_context_add_regex(struct GroupContextHead *head, const char *s,
+                                 int flags, struct Buffer *err);
 
 bool mutt_group_match(struct Group *g, const char *s);
 
-int mutt_group_context_clear(struct GroupContext **ctx);
-int mutt_group_context_remove_regex(struct GroupContext *ctx, const char *s);
-int mutt_group_context_remove_addrlist(struct GroupContext *ctx, struct Address *a);
+int mutt_group_context_clear(struct GroupContextHead *head);
+int mutt_group_context_remove_regex(struct GroupContextHead *head, const char *s);
+int mutt_group_context_remove_addrlist(struct GroupContextHead *head, struct Address *a);
 
 struct Group *mutt_pattern_group(const char *k);
 
diff --git a/init.c b/init.c
index 83e1d4aa883f6e81577d13c3e6b7a060ad1b2610..4aa269de06c8ae347f89d445153eccf22af211e7 100644 (file)
--- a/init.c
+++ b/init.c
@@ -540,7 +540,7 @@ static int parse_attach_list(struct Buffer *buf, struct Buffer *s,
  * @retval  0 Success
  * @retval -1 Error
  */
-static int parse_group_context(struct GroupContext **ctx, struct Buffer *buf,
+static int parse_group_context(struct GroupContextHead *ctx, struct Buffer *buf,
                                struct Buffer *s, unsigned long data, struct Buffer *err)
 {
   while (mutt_str_strcasecmp(buf->data, "-group") == 0)
@@ -567,7 +567,6 @@ static int parse_group_context(struct GroupContext **ctx, struct Buffer *buf,
   return 0;
 
 bail:
-  mutt_group_context_destroy(ctx);
   return -1;
 }
 
@@ -894,7 +893,7 @@ static int parse_alias(struct Buffer *buf, struct Buffer *s, unsigned long data,
 {
   struct Alias *tmp = NULL;
   char *estr = NULL;
-  struct GroupContext *gc = NULL;
+  struct GroupContextHead gc = STAILQ_HEAD_INITIALIZER(gc);
 
   if (!MoreArgs(s))
   {
@@ -945,7 +944,7 @@ static int parse_alias(struct Buffer *buf, struct Buffer *s, unsigned long data,
     goto bail;
   }
 
-  mutt_group_context_add_addrlist(gc, tmp->addr);
+  mutt_group_context_add_addrlist(&gc, tmp->addr);
   mutt_alias_add_reverse(tmp);
 
   if (DebugLevel > 2)
@@ -973,7 +972,7 @@ bail:
 static int parse_alternates(struct Buffer *buf, struct Buffer *s,
                             unsigned long data, struct Buffer *err)
 {
-  struct GroupContext *gc = NULL;
+  struct GroupContextHead gc = STAILQ_HEAD_INITIALIZER(gc);
 
   alternates_clean();
 
@@ -989,7 +988,7 @@ static int parse_alternates(struct Buffer *buf, struct Buffer *s,
     if (mutt_regexlist_add(&Alternates, buf->data, REG_ICASE, err) != 0)
       goto bail;
 
-    if (mutt_group_context_add_regex(gc, buf->data, REG_ICASE, err) != 0)
+    if (mutt_group_context_add_regex(&gc, buf->data, REG_ICASE, err) != 0)
       goto bail;
   } while (MoreArgs(s));
 
@@ -1106,7 +1105,7 @@ static int parse_finish(struct Buffer *buf, struct Buffer *s,
 static int parse_group(struct Buffer *buf, struct Buffer *s, unsigned long data,
                        struct Buffer *err)
 {
-  struct GroupContext *gc = NULL;
+  struct GroupContextHead gc = STAILQ_HEAD_INITIALIZER(gc);
   enum GroupState state = GS_NONE;
   struct Address *addr = NULL;
   char *estr = NULL;
@@ -1139,12 +1138,12 @@ static int parse_group(struct Buffer *buf, struct Buffer *s, unsigned long data,
 
         case GS_RX:
           if (data == MUTT_GROUP &&
-              mutt_group_context_add_regex(gc, buf->data, REG_ICASE, err) != 0)
+              mutt_group_context_add_regex(&gc, buf->data, REG_ICASE, err) != 0)
           {
             goto bail;
           }
           else if (data == MUTT_UNGROUP &&
-                   mutt_group_context_remove_regex(gc, buf->data) < 0)
+                   mutt_group_context_remove_regex(&gc, buf->data) < 0)
           {
             goto bail;
           }
@@ -1163,9 +1162,9 @@ static int parse_group(struct Buffer *buf, struct Buffer *s, unsigned long data,
             goto bail;
           }
           if (data == MUTT_GROUP)
-            mutt_group_context_add_addrlist(gc, addr);
+            mutt_group_context_add_addrlist(&gc, addr);
           else if (data == MUTT_UNGROUP)
-            mutt_group_context_remove_addrlist(gc, addr);
+            mutt_group_context_remove_addrlist(&gc, addr);
           mutt_addr_free(&addr);
           break;
       }
@@ -1295,7 +1294,7 @@ static int parse_ignore(struct Buffer *buf, struct Buffer *s,
 static int parse_lists(struct Buffer *buf, struct Buffer *s, unsigned long data,
                        struct Buffer *err)
 {
-  struct GroupContext *gc = NULL;
+  struct GroupContextHead gc = STAILQ_HEAD_INITIALIZER(gc);
 
   do
   {
@@ -1309,7 +1308,7 @@ static int parse_lists(struct Buffer *buf, struct Buffer *s, unsigned long data,
     if (mutt_regexlist_add(&MailLists, buf->data, REG_ICASE, err) != 0)
       goto bail;
 
-    if (mutt_group_context_add_regex(gc, buf->data, REG_ICASE, err) != 0)
+    if (mutt_group_context_add_regex(&gc, buf->data, REG_ICASE, err) != 0)
       goto bail;
   } while (MoreArgs(s));
 
@@ -1970,7 +1969,7 @@ static int parse_subjectrx_list(struct Buffer *buf, struct Buffer *s,
 static int parse_subscribe(struct Buffer *buf, struct Buffer *s,
                            unsigned long data, struct Buffer *err)
 {
-  struct GroupContext *gc = NULL;
+  struct GroupContextHead gc = STAILQ_HEAD_INITIALIZER(gc);
 
   do
   {
@@ -1986,7 +1985,7 @@ static int parse_subscribe(struct Buffer *buf, struct Buffer *s,
       goto bail;
     if (mutt_regexlist_add(&SubscribedLists, buf->data, REG_ICASE, err) != 0)
       goto bail;
-    if (mutt_group_context_add_regex(gc, buf->data, REG_ICASE, err) != 0)
+    if (mutt_group_context_add_regex(&gc, buf->data, REG_ICASE, err) != 0)
       goto bail;
   } while (MoreArgs(s));
 
diff --git a/init.h b/init.h
index b766ba391725deeb6a6e7611437d8a61c306dbd9..2c3f06a614ddf4f0d1f127670fb0932576b28553 100644 (file)
--- a/init.h
+++ b/init.h
@@ -4709,7 +4709,7 @@ static int parse_unsubscribe     (struct Buffer *buf, struct Buffer *s, unsigned
 static int parse_unsubscribe_from(struct Buffer *buf, struct Buffer *s, unsigned long data, struct Buffer *err);
 #endif
 /* Parse -group arguments */
-static int parse_group_context   (struct GroupContext **ctx,
+static int parse_group_context   (struct GroupContextHead *ctx,
                                   struct Buffer *buf, struct Buffer *s, unsigned long data, struct Buffer *err);
 
 const struct Command Commands[] = {