]> granicus.if.org Git - neomutt/commitdiff
Convert RegexList to use STAILQ
authorBo YU <yuzibode@126.com>
Sat, 18 Aug 2018 06:58:18 +0000 (14:58 +0800)
committerRichard Russon <rich@flatcap.org>
Sun, 19 Aug 2018 12:21:47 +0000 (13:21 +0100)
alias.c
email/email_globals.c
email/email_globals.h
email/parse.c
globals.h
group.c
hcache/hcache.c
hdrline.c
init.c
mutt/regex.c
mutt/regex3.h

diff --git a/alias.c b/alias.c
index 187ace2554c7555c4cca0425df102433e37f0954..3c02e2a298cd6e7512c6330eb8858f21abd9b1f2 100644 (file)
--- a/alias.c
+++ b/alias.c
@@ -705,10 +705,10 @@ bool mutt_addr_is_user(struct Address *addr)
     return true;
   }
 
-  if (mutt_regexlist_match(Alternates, addr->mailbox))
+  if (mutt_regexlist_match(&Alternates, addr->mailbox))
   {
     mutt_debug(5, "yes, %s matched by alternates.\n", addr->mailbox);
-    if (mutt_regexlist_match(UnAlternates, addr->mailbox))
+    if (mutt_regexlist_match(&UnAlternates, addr->mailbox))
       mutt_debug(5, "but, %s matched by unalternates.\n", addr->mailbox);
     else
       return true;
index bdf50c8086ad616b975eb5865d2c7b39f0114522..8f53c052725a99af4eac774a1e0a470f3cbd20b5 100644 (file)
@@ -38,7 +38,7 @@ char *SpamSeparator = NULL; ///< Config: Separator for multiple spam headers ///
 bool Weed = false; ///< Config: Filter headers when displaying/forwarding/printing/replying
 
 /* Global variables */
-struct RegexList *NoSpamList = NULL;
+struct RegexList NoSpamList = STAILQ_HEAD_INITIALIZER(NoSpamList);
 struct ReplaceList *SpamList = NULL;
 struct ListHead Ignore = STAILQ_HEAD_INITIALIZER(Ignore);
 struct ListHead UnIgnore = STAILQ_HEAD_INITIALIZER(UnIgnore);
index 96cfcd455038b19e2d40b914282d6b0ab1a579cd..63b70e955519016df0d26eefa328ea42e144f159 100644 (file)
@@ -35,7 +35,7 @@ extern bool                Weed;
 
 /* Global variables */
 extern struct ListHead     Ignore;
-extern struct RegexList *  NoSpamList;
+extern struct RegexList    NoSpamList;
 extern struct ReplaceList *SpamList;
 extern struct ListHead     UnIgnore;
 
index 1c8e66d82448486b5bc093a2b3e1c52e58005a64..b71c4a5fbeb3db31d1398b52c76ec1d28e8f5022 100644 (file)
@@ -1013,7 +1013,7 @@ struct Envelope *mutt_rfc822_read_header(FILE *f, struct Header *hdr, bool user_
 
     if (mutt_replacelist_match(SpamList, buf, sizeof(buf), line))
     {
-      if (!mutt_regexlist_match(NoSpamList, line))
+      if (!mutt_regexlist_match(&NoSpamList, line))
       {
         /* if spam tag already exists, figure out how to amend it */
         if (e->spam && *buf)
index 671eced2c14b4c0f691d4118a5b3879af5b0e6bf..dfd4d80f4dcc5c12647c9de7b623b9c6976d8c44 100644 (file)
--- a/globals.h
+++ b/globals.h
@@ -77,12 +77,12 @@ WHERE struct ListHead AttachExclude INITVAL(STAILQ_HEAD_INITIALIZER(AttachExclud
 WHERE struct ListHead InlineAllow INITVAL(STAILQ_HEAD_INITIALIZER(InlineAllow));
 WHERE struct ListHead InlineExclude INITVAL(STAILQ_HEAD_INITIALIZER(InlineExclude));
 
-WHERE struct RegexList *Alternates;
-WHERE struct RegexList *UnAlternates;
-WHERE struct RegexList *MailLists;
-WHERE struct RegexList *UnMailLists;
-WHERE struct RegexList *SubscribedLists;
-WHERE struct RegexList *UnSubscribedLists;
+WHERE struct RegexList Alternates INITVAL(STAILQ_HEAD_INITIALIZER(Alternates));
+WHERE struct RegexList UnAlternates INITVAL(STAILQ_HEAD_INITIALIZER(UnAlternates));
+WHERE struct RegexList MailLists INITVAL(STAILQ_HEAD_INITIALIZER(MailLists));
+WHERE struct RegexList UnMailLists INITVAL(STAILQ_HEAD_INITIALIZER(UnMailLists));
+WHERE struct RegexList SubscribedLists INITVAL(STAILQ_HEAD_INITIALIZER(SubscribedLists));
+WHERE struct RegexList UnSubscribedLists INITVAL(STAILQ_HEAD_INITIALIZER(UnSubscribedLists));
 WHERE struct ReplaceList *SubjectRegexList;
 
 WHERE unsigned short Counter;
diff --git a/group.c b/group.c
index 188e8eb3e42d9783a0b77196fe673960f02067b2..91c93be2f4b1f71f6431102784d0c5d7d9024320 100644 (file)
--- a/group.c
+++ b/group.c
@@ -65,7 +65,7 @@ static void group_remove(struct Group *g)
     return;
   mutt_hash_delete(Groups, g->name, g);
   mutt_addr_free(&g->as);
-  mutt_regexlist_free(&g->rs);
+  mutt_regexlist_free(g->rs);
   FREE(&g->name);
   FREE(&g);
 }
@@ -185,7 +185,7 @@ static int group_remove_addrlist(struct Group *g, struct Address *a)
  */
 static int group_add_regex(struct Group *g, const char *s, int flags, struct Buffer *err)
 {
-  return mutt_regexlist_add(&g->rs, s, flags, err);
+  return mutt_regexlist_add(g->rs, s, flags, err);
 }
 
 /**
@@ -197,7 +197,7 @@ static int group_add_regex(struct Group *g, const char *s, int flags, struct Buf
  */
 static int group_remove_regex(struct Group *g, const char *s)
 {
-  return mutt_regexlist_remove(&g->rs, s);
+  return mutt_regexlist_remove(g->rs, s);
 }
 
 /**
index 8826316a4e6c0980d11f54c474c95896d77b1003..a8d69c149e4b8783b69d2bf0b032b85f4f4fc25b 100644 (file)
@@ -269,7 +269,6 @@ header_cache_t *mutt_hcache_open(const char *path, const char *folder, hcache_na
     } digest;
     struct Md5Ctx ctx;
     struct ReplaceList *spam = NULL;
-    struct RegexList *nospam = NULL;
 
     hcachever = HCACHEVER;
 
@@ -286,9 +285,10 @@ header_cache_t *mutt_hcache_open(const char *path, const char *folder, hcache_na
     }
 
     /* Mix in user's nospam list */
-    for (nospam = NoSpamList; nospam; nospam = nospam->next)
+    struct RegexListNode *np = NULL;
+    STAILQ_FOREACH(np, &NoSpamList, entries)
     {
-      mutt_md5_process(nospam->regex->pattern, &ctx);
+      mutt_md5_process(np->regex->pattern, &ctx);
     }
 
     /* Get a hash and take its bytes as an (unsigned int) hash version */
index 8b2324ca13865e1aee6555744be1ef23e80a0958..ab185aee09707743b0e87abb1f905269d0996dfe 100644 (file)
--- a/hdrline.c
+++ b/hdrline.c
@@ -77,8 +77,8 @@ enum FlagChars
  */
 bool mutt_is_mail_list(struct Address *addr)
 {
-  if (!mutt_regexlist_match(UnMailLists, addr->mailbox))
-    return mutt_regexlist_match(MailLists, addr->mailbox);
+  if (!mutt_regexlist_match(&UnMailLists, addr->mailbox))
+    return mutt_regexlist_match(&MailLists, addr->mailbox);
   return false;
 }
 
@@ -89,10 +89,10 @@ bool mutt_is_mail_list(struct Address *addr)
  */
 bool mutt_is_subscribed_list(struct Address *addr)
 {
-  if (!mutt_regexlist_match(UnMailLists, addr->mailbox) &&
-      !mutt_regexlist_match(UnSubscribedLists, addr->mailbox))
+  if (!mutt_regexlist_match(&UnMailLists, addr->mailbox) &&
+      !mutt_regexlist_match(&UnSubscribedLists, addr->mailbox))
   {
-    return mutt_regexlist_match(SubscribedLists, addr->mailbox);
+    return mutt_regexlist_match(&SubscribedLists, addr->mailbox);
   }
   return false;
 }
diff --git a/init.c b/init.c
index 94d53e1115be162306694e3711d4bb11ff9bad8e..fe3a76d48a6e0f50a52fe412dd3a7b773a0bdc3e 100644 (file)
--- a/init.c
+++ b/init.c
@@ -1924,7 +1924,6 @@ static int parse_spam_list(struct Buffer *buf, struct Buffer *s,
       }
       FREE(&templ.data);
     }
-
     /* If not, try to remove from the nospam list. */
     else
     {
@@ -1933,7 +1932,6 @@ static int parse_spam_list(struct Buffer *buf, struct Buffer *s,
 
     return 0;
   }
-
   /* MUTT_NOSPAM is for nospam commands. */
   else if (data == MUTT_NOSPAM)
   {
index cf40ae2b535ee0c6502e1594a051de6735ed8375..0e8207c4846208d171c0255b72ffc1fda3bb7954 100644 (file)
@@ -125,9 +125,9 @@ void mutt_regex_free(struct Regex **r)
  * @retval 0  Success, Regex compiled and added to the list
  * @retval -1 Error, see message in 'err'
  */
-int mutt_regexlist_add(struct RegexList **rl, const char *str, int flags, struct Buffer *err)
+int mutt_regexlist_add(struct RegexList *rl, const char *str, int flags, struct Buffer *err)
 {
-  struct RegexList *t = NULL, *last = NULL;
+  struct RegexListNode *t = NULL, *last = NULL;
   struct Regex *rx = NULL;
 
   if (!str || !*str)
@@ -141,7 +141,7 @@ int mutt_regexlist_add(struct RegexList **rl, const char *str, int flags, struct
   }
 
   /* check to make sure the item is not already on this rl */
-  for (last = *rl; last; last = last->next)
+  STAILQ_FOREACH_FROM(last, rl, entries)
   {
     if (mutt_str_strcasecmp(rx->pattern, last->regex->pattern) == 0)
     {
@@ -149,18 +149,18 @@ int mutt_regexlist_add(struct RegexList **rl, const char *str, int flags, struct
       last = NULL;
       break;
     }
-    if (!last->next)
+    if (!STAILQ_NEXT(last, entries))
       break;
   }
 
-  if (!*rl || last)
+  if (STAILQ_EMPTY(rl) || last)
   {
     t = mutt_regexlist_new();
     t->regex = rx;
     if (last)
-      last->next = t;
+      STAILQ_INSERT_TAIL(rl, t, entries);
     else
-      *rl = t;
+      STAILQ_INSERT_HEAD(rl, t, entries);
   }
   else /* duplicate */
     mutt_regex_free(&rx);
@@ -172,19 +172,20 @@ int mutt_regexlist_add(struct RegexList **rl, const char *str, int flags, struct
  * mutt_regexlist_free - Free a RegexList object
  * @param rl RegexList to free
  */
-void mutt_regexlist_free(struct RegexList **rl)
+void mutt_regexlist_free(struct RegexList *rl)
 {
-  struct RegexList *p = NULL;
+  struct RegexListNode *np = STAILQ_FIRST(rl), *next = NULL;
 
   if (!rl)
     return;
-  while (*rl)
+  while (np)
   {
-    p = *rl;
-    *rl = (*rl)->next;
-    mutt_regex_free(&p->regex);
-    FREE(&p);
+    mutt_regex_free(&np->regex);
+    next = STAILQ_NEXT(np, entries);
+    FREE(&np);
+    np = next;
   }
+
 }
 
 /**
@@ -195,16 +196,16 @@ void mutt_regexlist_free(struct RegexList **rl)
  */
 bool mutt_regexlist_match(struct RegexList *rl, const char *str)
 {
+  struct RegexListNode *np = NULL;
   if (!str)
     return false;
-
-  for (; rl; rl = rl->next)
+  STAILQ_FOREACH(np, rl, entries)
   {
-    if (!rl->regex || !rl->regex->regex)
+    if (!np->regex || !np->regex->regex)
       continue;
-    if (regexec(rl->regex->regex, str, 0, NULL, 0) == 0)
+    if (regexec(np->regex->regex, str, 0, NULL, 0) == 0)
     {
-      mutt_debug(5, "%s matches %s\n", str, rl->regex->pattern);
+      mutt_debug(5, "%s matches %s\n", str, np->regex->pattern);
       return true;
     }
   }
@@ -216,9 +217,9 @@ bool mutt_regexlist_match(struct RegexList *rl, const char *str)
  * mutt_regexlist_new - Create a new RegexList
  * @retval ptr New RegexList object
  */
-struct RegexList *mutt_regexlist_new(void)
+struct RegexListNode *mutt_regexlist_new(void)
 {
-  return mutt_mem_calloc(1, sizeof(struct RegexList));
+  return mutt_mem_calloc(1, sizeof(struct RegexListNode));
 }
 
 /**
@@ -230,9 +231,9 @@ struct RegexList *mutt_regexlist_new(void)
  *
  * If the pattern is "*", then all the Regexes are removed.
  */
-int mutt_regexlist_remove(struct RegexList **rl, const char *str)
+int mutt_regexlist_remove(struct RegexList *rl, const char *str)
 {
-  struct RegexList *p = NULL, *last = NULL;
+  struct RegexListNode *np = STAILQ_FIRST(rl), *next = NULL;
   int rc = -1;
 
   if (mutt_str_strcmp("*", str) == 0)
@@ -242,26 +243,15 @@ int mutt_regexlist_remove(struct RegexList **rl, const char *str)
   }
   else
   {
-    p = *rl;
-    last = NULL;
-    while (p)
+    while(np)
     {
-      if (mutt_str_strcasecmp(str, p->regex->pattern) == 0)
-      {
-        mutt_regex_free(&p->regex);
-        if (last)
-          last->next = p->next;
-        else
-          (*rl) = p->next;
-        FREE(&p);
-        rc = 0;
-      }
-      else
-      {
-        last = p;
-        p = p->next;
-      }
+      if (mutt_str_strcasecmp(str, np->regex->pattern) == 0)
+        mutt_regex_free(&np->regex);
+      next = STAILQ_NEXT(np, entries);
+      FREE(&np);
+      np = next;
     }
+    rc = 0;
   }
   return rc;
 }
index 8c79257bd0e28fc9cc39cef24d66f4b8eced4c38..7ffdb9581a416559d6360d223171d0e214d6db82 100644 (file)
@@ -26,6 +26,7 @@
 #include <stddef.h>
 #include <regex.h>
 #include <stdbool.h>
+#include "mutt/queue.h"
 
 struct Buffer;
 
@@ -61,14 +62,16 @@ struct Regex
 };
 
 /**
- * struct RegexList - List of regular expressions
+ * struct RegexListNode - List of regular expressions
  */
-struct RegexList
+struct RegexListNode
 {
-  struct Regex *regex;    /**< Regex containing a regular expression */
-  struct RegexList *next; /**< Next item in list */
+  struct Regex *regex; /**< Regex containing a regular expression */
+  STAILQ_ENTRY(RegexListNode) entries; /**< Next item in list */
 };
 
+STAILQ_HEAD(RegexList, RegexListNode);
+
 /**
  * struct ReplaceList - List of regular expressions
  */
@@ -84,11 +87,11 @@ struct Regex *      mutt_regex_compile(const char *str, int flags);
 struct Regex *      mutt_regex_create(const char *str, int flags, struct Buffer *err);
 void                mutt_regex_free(struct Regex **r);
 
-int                 mutt_regexlist_add(struct RegexList **rl, const char *str, int flags, struct Buffer *err);
-void                mutt_regexlist_free(struct RegexList **rl);
+int                 mutt_regexlist_add(struct RegexList *rl, const char *str, int flags, struct Buffer *err);
+void                mutt_regexlist_free(struct RegexList *rl);
 bool                mutt_regexlist_match(struct RegexList *rl, const char *str);
-struct RegexList *  mutt_regexlist_new(void);
-int                 mutt_regexlist_remove(struct RegexList **rl, const char *str);
+struct RegexListNode *  mutt_regexlist_new(void);
+int                 mutt_regexlist_remove(struct RegexList *rl, const char *str);
 
 int                 mutt_replacelist_add(struct ReplaceList **rl, const char *pat, const char *templ, struct Buffer *err);
 char *              mutt_replacelist_apply(struct ReplaceList *rl, char *buf, size_t buflen, const char *str);