]> granicus.if.org Git - neomutt/commitdiff
Convert Muttrc to STailQ, start to gather common algos in list.h
authorPietro Cerutti <gahr@gahr.ch>
Tue, 25 Jul 2017 12:51:37 +0000 (12:51 +0000)
committerRichard Russon <rich@flatcap.org>
Sat, 5 Aug 2017 17:53:06 +0000 (18:53 +0100)
Issue #374

13 files changed:
globals.h
hcache/hcache.c
headers.c
init.c
list.h
main.c
mutt.h
muttlib.c
parse.c
postpone.c
remailer.c
send.c
thread.c

index b08add8251310a470635c12301ecfe198ec8349c..33f23c3ae7f04e5f5ff7a7cd8c969fdd6c1bbdf9 100644 (file)
--- a/globals.h
+++ b/globals.h
@@ -115,7 +115,7 @@ WHERE char *Mixmaster;
 WHERE char *MixEntryFormat;
 #endif
 
-WHERE struct List *Muttrc;
+WHERE struct STailQHead Muttrc INITVAL(STAILQ_HEAD_INITIALIZER(Muttrc));
 #ifdef USE_NNTP
 WHERE char *GroupFormat;
 WHERE char *Inews;
index 0df5d3f01b59f82d35649d1ca551c75eaa181d2b..8bbb077edaa2d560783b5c75277504cd653d72bc 100644 (file)
@@ -314,9 +314,8 @@ static void restore_stailq(struct STailQHead *l, const unsigned char *d, int *of
   struct STailQNode *np;
   while (counter)
   {
-    np = safe_malloc(sizeof(struct STailQNode));
+    np = mutt_stailq_insert_tail(l, NULL);
     restore_char(&np->data, d, off, convert);
-    STAILQ_INSERT_TAIL(l, np, entries);
     counter--;
   }
 }
index 46841b48260e4b27507444d212a9892aa5c1c5d8..7df42b4824fce30fe65d451575702c0fe06199fc 100644 (file)
--- a/headers.c
+++ b/headers.c
@@ -98,7 +98,7 @@ void mutt_edit_headers(const char *editor, const char *body, struct Header *msg,
   }
 
   mutt_unlink(body);
-  mutt_free_stailq(&msg->env->userhdrs);
+  mutt_stailq_free(&msg->env->userhdrs);
 
   /* Read the temp file back in */
   if ((ifp = fopen(path, "r")) == NULL)
@@ -133,10 +133,10 @@ void mutt_edit_headers(const char *editor, const char *body, struct Header *msg,
         (STAILQ_EMPTY(&n->in_reply_to) ||
          (mutt_strcmp(STAILQ_FIRST(&n->in_reply_to)->data,
                       STAILQ_FIRST(&msg->env->in_reply_to)->data) != 0)))
-      mutt_free_stailq(&msg->env->references);
+      mutt_stailq_free(&msg->env->references);
 
   /* restore old info. */
-  mutt_free_stailq(&n->references);
+  mutt_stailq_free(&n->references);
   STAILQ_SWAP(&n->references, &msg->env->references, STailQNode);
 
   mutt_free_envelope(&msg->env);
diff --git a/init.c b/init.c
index 71c3dded51bd02c5350427cae6f152d7af3164bb..d3410ca7c7f34aa5a416270d5d3335760af79306 100644 (file)
--- a/init.c
+++ b/init.c
@@ -798,9 +798,7 @@ static void add_to_stailq(struct STailQHead *head, const char *str)
       return;
     }
   }
-  np = safe_calloc(1, sizeof(struct STailQNode));
-  np->data = safe_strdup(str);
-  STAILQ_INSERT_TAIL(head, np, entries);
+  mutt_stailq_insert_tail(head, safe_strdup(str));
 }
 
 static struct RxList *new_rx_list(void)
@@ -1185,7 +1183,7 @@ static int parse_stailq(struct Buffer *buf, struct Buffer *s, unsigned long data
 static void remove_from_stailq(struct STailQHead *head, const char *str)
 {
   if (mutt_strcmp("*", str) == 0)
-    mutt_free_stailq(head); /* ``unCMD *'' means delete all current entries */
+    mutt_stailq_free(head); /* ``unCMD *'' means delete all current entries */
   else
   {
     struct STailQNode *np, *tmp;
@@ -1213,7 +1211,7 @@ static int parse_unstailq(struct Buffer *buf, struct Buffer *s,
      */
     if (mutt_strcmp(buf->data, "*") == 0)
     {
-      mutt_free_stailq((struct STailQHead *)data);
+      mutt_stailq_free((struct STailQHead *)data);
       break;
     }
     remove_from_stailq((struct STailQHead *)data, buf->data);
@@ -2116,7 +2114,7 @@ static int parse_unmy_hdr(struct Buffer *buf, struct Buffer *s,
     mutt_extract_token(buf, s, 0);
     if (mutt_strcmp("*", buf->data) == 0)
     {
-      mutt_free_stailq(&UserHeader);
+      mutt_stailq_free(&UserHeader);
       continue;
     }
 
@@ -2164,8 +2162,7 @@ static int parse_my_hdr(struct Buffer *buf, struct Buffer *s,
   if (!n)
   {
     /* not found, allocate memory for a new node and add it to the list */
-    n = safe_calloc(1, sizeof(struct STailQNode));
-    STAILQ_INSERT_TAIL(&UserHeader, n, entries);
+    n = mutt_stailq_insert_tail(&UserHeader, NULL);
   }
   else
   {
@@ -3333,9 +3330,7 @@ static int source_rc(const char *rcfile_path, struct Buffer *err)
     }
     if (!np)
     {
-      np = safe_calloc(1, sizeof(struct STailQNode));
-      np->data = safe_strdup(rcfile);
-      STAILQ_INSERT_HEAD(&MuttrcStack, np, entries);
+      mutt_stailq_insert_head(&MuttrcStack, safe_strdup(rcfile));
     }
     else
     {
@@ -4481,7 +4476,8 @@ void mutt_init(int skip_sys_rc, struct List *commands)
   add_to_list(&MailToAllow, "in-reply-to");
   add_to_list(&MailToAllow, "references");
 
-  if (!Muttrc)
+  struct STailQNode *np = NULL;
+  if (STAILQ_EMPTY(&Muttrc))
   {
     char *xdg_cfg_home = getenv("XDG_CONFIG_HOME");
 
@@ -4494,31 +4490,30 @@ void mutt_init(int skip_sys_rc, struct List *commands)
     char *config = find_cfg(HomeDir, xdg_cfg_home);
     if (config)
     {
-      Muttrc = mutt_add_list(Muttrc, config);
-      FREE(&config);
+      mutt_stailq_insert_tail(&Muttrc, config);
     }
   }
   else
   {
-    for (struct List *config = Muttrc; config != NULL; config = config->next)
+    STAILQ_FOREACH(np, &Muttrc, entries)
     {
-      strfcpy(buffer, config->data, sizeof(buffer));
-      FREE(&config->data);
+      strfcpy(buffer, np->data, sizeof(buffer));
+      FREE(&np->data);
       mutt_expand_path(buffer, sizeof(buffer));
-      config->data = safe_strdup(buffer);
-      if (access(config->data, F_OK))
+      np->data = safe_strdup(buffer);
+      if (access(np->data, F_OK))
       {
-        snprintf(buffer, sizeof(buffer), "%s: %s", config->data, strerror(errno));
+        snprintf(buffer, sizeof(buffer), "%s: %s", np->data, strerror(errno));
         mutt_endwin(buffer);
         exit(1);
       }
     }
   }
 
-  if (Muttrc && Muttrc->data)
+  if (!STAILQ_EMPTY(&Muttrc))
   {
     FREE(&AliasFile);
-    AliasFile = safe_strdup(Muttrc->data);
+    AliasFile = safe_strdup(STAILQ_FIRST(&Muttrc)->data);
   }
 
   /* Process the global rc file if it exists and the user hasn't explicitly
@@ -4572,13 +4567,13 @@ void mutt_init(int skip_sys_rc, struct List *commands)
   }
 
   /* Read the user's initialization file.  */
-  for (struct List *config = Muttrc; config != NULL; config = config->next)
+  STAILQ_FOREACH(np, &Muttrc, entries)
   {
-    if (config->data)
+    if (np->data)
     {
       if (!option(OPT_NO_CURSES))
         endwin();
-      if (source_rc(config->data, &err) != 0)
+      if (source_rc(np->data, &err) != 0)
       {
         fputs(err.data, stderr);
         fputc('\n', stderr);
diff --git a/list.h b/list.h
index 1275b30279eb2076cee834fafe95d5b8fe877066..2da29e2056834eb6ed3cbd3b02fec15f8e13c0e2 100644 (file)
--- a/list.h
+++ b/list.h
@@ -50,4 +50,46 @@ struct STailQNode
     STAILQ_ENTRY(STailQNode) entries;
 };
 
+static inline struct STailQNode* mutt_stailq_insert_head(struct STailQHead *h, char *s)
+{
+  struct STailQNode *np = safe_calloc(1, sizeof(struct STailQNode));
+  np->data = s;
+  STAILQ_INSERT_HEAD(h, np, entries);
+  return np;
+}
+
+static inline struct STailQNode* mutt_stailq_insert_tail(struct STailQHead *h, char * s)
+{
+  struct STailQNode *np = safe_calloc(1, sizeof(struct STailQNode));
+  np->data = s;
+  STAILQ_INSERT_TAIL(h, np, entries);
+  return np;
+}
+
+static inline struct STailQNode *mutt_stailq_find(struct STailQHead *h, const char *data)
+{
+  struct STailQNode *np = NULL;
+  STAILQ_FOREACH(np, h, entries)
+  {
+    if (np->data == data || mutt_strcmp(np->data, data) == 0)
+    {
+      return np;
+    }
+  }
+  return NULL;
+}
+
+static inline void mutt_stailq_free(struct STailQHead *h)
+{
+  struct STailQNode *np = STAILQ_FIRST(h), *next = NULL;
+  while (np)
+  {
+      next = STAILQ_NEXT(np, entries);
+      FREE(&np->data);
+      FREE(&np);
+      np = next;
+  }
+  STAILQ_INIT(h);
+}
+
 #endif /* _MUTT_LIST_H */
diff --git a/main.c b/main.c
index 27f1deb6dfa9f50c92bbf5f211cc3656d3642f77..642b91901442c7b3210bbaba0a24547a01d32d9f 100644 (file)
--- a/main.c
+++ b/main.c
@@ -290,7 +290,7 @@ int main(int argc, char **argv, char **env)
 
         case 'F':
           /* mutt_str_replace (&Muttrc, optarg); */
-          Muttrc = mutt_add_list(Muttrc, optarg);
+          mutt_stailq_insert_tail(&Muttrc, safe_strdup(optarg));
           break;
 
         case 'f':
diff --git a/mutt.h b/mutt.h
index 1bf4291a0d8309ed6714b4250fb1f1820d44de1b..f6cd9c160641c1ccd05381b1dc6d1f175280ea50 100644 (file)
--- a/mutt.h
+++ b/mutt.h
@@ -314,7 +314,6 @@ enum QuadOptionVars
 #define MUTT_KEYWORDS       (1 << 3) /**< rfc2822 */
 
 void mutt_free_list(struct List **list);
-void mutt_free_stailq(struct STailQHead *list);
 void mutt_free_rx_list(struct RxList **list);
 void mutt_free_replace_list(struct ReplaceList **list);
 int mutt_matches_ignore(const char *s);
@@ -324,7 +323,6 @@ bool mutt_matches_list(const char *s, struct List *t);
 struct List *mutt_add_list(struct List *head, const char *data);
 struct List *mutt_add_list_n(struct List *head, const void *data, size_t len);
 struct List *mutt_find_list(struct List *l, const char *data);
-struct STailQNode *mutt_find_stailq(struct STailQHead *h, const char *data);
 int mutt_remove_from_rx_list(struct RxList **l, const char *str);
 
 void mutt_init(int skip_sys_rc, struct List *commands);
index e901a8157ed4ad09c4e65f446d24988630c5b25c..f86d07b0daa0a395abe3be94147b388d56c99e05 100644 (file)
--- a/muttlib.c
+++ b/muttlib.c
@@ -287,19 +287,6 @@ struct List *mutt_find_list(struct List *l, const char *data)
   return NULL;
 }
 
-struct STailQNode *mutt_find_stailq(struct STailQHead *h, const char *data)
-{
-  struct STailQNode *np = NULL;
-  STAILQ_FOREACH(np, h, entries)
-  {
-    if (np->data == data)
-      return np;
-    if (data && np->data && (mutt_strcmp(np->data, data) == 0))
-      return np;
-  }
-  return NULL;
-}
-
 int mutt_remove_from_rx_list(struct RxList **l, const char *str)
 {
   struct RxList *p = NULL, *last = NULL;
@@ -351,18 +338,6 @@ void mutt_free_list(struct List **list)
   }
 }
 
-void mutt_free_stailq(struct STailQHead *h)
-{
-    struct STailQNode *np = NULL;
-    while (!STAILQ_EMPTY(h))
-    {
-        np = STAILQ_FIRST(h);
-        STAILQ_REMOVE_HEAD(h, entries);
-        FREE(&np->data);
-        FREE(&np);
-    }
-}
-
 void mutt_free_header(struct Header **h)
 {
   if (!h || !*h)
@@ -373,7 +348,7 @@ void mutt_free_header(struct Header **h)
   FREE(&(*h)->tree);
   FREE(&(*h)->path);
 #ifdef MIXMASTER
-  mutt_free_stailq(&(*h)->chain);
+  mutt_stailq_free(&(*h)->chain);
 #endif
 #if defined(USE_POP) || defined(USE_IMAP) || defined(USE_NNTP) || defined(USE_NOTMUCH)
   if ((*h)->free_cb)
@@ -716,9 +691,9 @@ void mutt_free_envelope(struct Envelope **p)
 
   mutt_buffer_free(&(*p)->spam);
 
-  mutt_free_stailq(&(*p)->references);
-  mutt_free_stailq(&(*p)->in_reply_to);
-  mutt_free_stailq(&(*p)->userhdrs);
+  mutt_stailq_free(&(*p)->references);
+  mutt_stailq_free(&(*p)->in_reply_to);
+  mutt_stailq_free(&(*p)->userhdrs);
   FREE(p);
 }
 
@@ -781,7 +756,7 @@ void mutt_merge_envelopes(struct Envelope *base, struct Envelope **extra)
   /* spam and user headers should never be hashed, and the new envelope may
     * have better values. Use new versions regardless. */
   mutt_buffer_free(&base->spam);
-  mutt_free_stailq(&base->userhdrs);
+  mutt_stailq_free(&base->userhdrs);
   MOVE_ELEM(spam);
   MOVE_STAILQ(userhdrs);
 #undef MOVE_ELEM
diff --git a/parse.c b/parse.c
index 281b133645d9be32f7a52e910f98ebec12d70181..bf1a726d9e9245a0c8b46edc474c5c4bc65734f0 100644 (file)
--- a/parse.c
+++ b/parse.c
@@ -116,17 +116,13 @@ char *mutt_read_rfc822_line(FILE *f, char *line, size_t *linelen)
 
 static void parse_references(struct STailQHead *head, char *s)
 {
-  struct STailQNode *np = NULL;
   char *m = NULL;
   const char *sp = NULL;
 
-  m = mutt_extract_message_id(s, &sp);
-  while (m)
+  while ((m = mutt_extract_message_id(s, &sp)))
   {
-    np  = safe_malloc(sizeof(struct STailQNode));
-    np->data = m;
-    STAILQ_INSERT_HEAD(head, np, entries);
-    m = mutt_extract_message_id(NULL, &sp);
+    mutt_stailq_insert_head(head, m);
+    s = NULL;
   }
 }
 
@@ -1099,7 +1095,7 @@ int mutt_parse_rfc822_line(struct Envelope *e, struct Header *hdr, char *line,
     case 'i':
       if (mutt_strcasecmp(line + 1, "n-reply-to") == 0)
       {
-        mutt_free_stailq(&e->in_reply_to);
+        mutt_stailq_free(&e->in_reply_to);
         parse_references(&e->in_reply_to, p);
         matched = 1;
       }
@@ -1200,7 +1196,7 @@ int mutt_parse_rfc822_line(struct Envelope *e, struct Header *hdr, char *line,
     case 'r':
       if (mutt_strcasecmp(line + 1, "eferences") == 0)
       {
-        mutt_free_stailq(&e->references);
+        mutt_stailq_free(&e->references);
         parse_references(&e->references, p);
         matched = 1;
       }
@@ -1342,9 +1338,7 @@ int mutt_parse_rfc822_line(struct Envelope *e, struct Header *hdr, char *line,
 
     if (!(weed && option(OPT_WEED) && mutt_matches_ignore(line)))
     {
-      struct STailQNode *np = calloc(1, sizeof(struct STailQNode));
-      np->data = safe_strdup(line);
-      STAILQ_INSERT_TAIL(&e->userhdrs, np, entries);
+      struct STailQNode *np = mutt_stailq_insert_tail(&e->userhdrs, safe_strdup(line));
       if (do_2047)
         rfc2047_decode(&np->data);
     }
index fc8248cf1067a4e3b320a0e1eb4ddc5669e730b4..9043dd91ed346e3a791dd3a60a350cb2b942c19a 100644 (file)
@@ -369,15 +369,12 @@ int mutt_get_postponed(struct Context *ctx, struct Header *hdr,
     else if (mutt_strncmp("X-Mutt-Mix:", np->data, 11) == 0)
     {
       char *t = NULL;
-      mutt_free_stailq(&hdr->chain);
+      mutt_stailq_free(&hdr->chain);
 
       t = strtok(np->data + 11, " \t\n");
-      struct STailQNode *n;
       while (t)
       {
-        n = safe_calloc(1, sizeof(struct STailQNode));
-        n->data = safe_strdup(t);
-        STAILQ_INSERT_TAIL(&hdr->chain, n, entries);
+        mutt_stailq_insert_tail(&hdr->chain, safe_strdup(t));
         t = strtok(NULL, " \t\n");
       }
     }
index 8000dcd1993bc72181966f856967aaaf49d93f99..31085875e2b8b576bb588f9938e9d79a2c1f3586 100644 (file)
@@ -484,7 +484,7 @@ void mix_make_chain(struct STailQHead *chainhead)
   {
     mix_chain_add(chain, p->data, type2_list);
   }
-  mutt_free_stailq(chainhead);
+  mutt_stailq_free(chainhead);
 
   /* safety check */
   for (i = 0; i < chain->cl; i++)
@@ -646,7 +646,6 @@ void mix_make_chain(struct STailQHead *chainhead)
 
   if (chain->cl)
   {
-    struct STailQNode *n = NULL;
     for (i = 0; i < chain->cl; i++)
     {
       if ((j = chain->ch[i]))
@@ -654,9 +653,7 @@ void mix_make_chain(struct STailQHead *chainhead)
       else
         t = "*";
 
-      n = safe_calloc(1, sizeof(struct STailQNode));
-      n->data = safe_strdup(t);
-      STAILQ_INSERT_TAIL(chainhead, n, entries);
+      mutt_stailq_insert_tail(chainhead, safe_strdup(t));
     }
   }
 
diff --git a/send.c b/send.c
index 2169611c7db1d1e845ec599598b84b12d4da70a5..eb0c2434fed46cc6f261c886623c9b11cba76995 100644 (file)
--- a/send.c
+++ b/send.c
@@ -399,9 +399,7 @@ static void process_user_header(struct Envelope *env)
              (mutt_strncasecmp("subject:", uh->data, 8) != 0) &&
              (mutt_strncasecmp("return-path:", uh->data, 12) != 0))
     {
-      struct STailQNode *np = safe_calloc(1, sizeof(struct STailQNode));
-      np->data = safe_strdup(uh->data);
-      STAILQ_INSERT_TAIL(&env->userhdrs, np, entries);
+      mutt_stailq_insert_tail(&env->userhdrs, safe_strdup(uh->data));
     }
   }
 }
@@ -646,14 +644,12 @@ int mutt_fetch_recips(struct Envelope *out, struct Envelope *in, int flags)
 static void add_references(struct STailQHead *head, struct Envelope *e)
 {
   struct STailQHead *src;
-  struct STailQNode *np, *new;
+  struct STailQNode *np;
 
   src = !STAILQ_EMPTY(&e->references) ? &e->references : &e->in_reply_to;
   STAILQ_FOREACH(np, src, entries)
   {
-    new = safe_calloc(1, sizeof(struct STailQNode));
-    new->data = safe_strdup(np->data);
-    STAILQ_INSERT_TAIL(head, new, entries);
+    mutt_stailq_insert_tail(head, safe_strdup(np->data));
   }
 }
 
@@ -661,9 +657,7 @@ static void add_message_id(struct STailQHead *head, struct Envelope *e)
 {
   if (e->message_id)
   {
-    struct STailQNode *new = safe_calloc(1, sizeof(struct STailQNode));
-    new->data = safe_strdup(e->message_id);
-    STAILQ_INSERT_HEAD(head, new, entries);
+    mutt_stailq_insert_head(head, safe_strdup(e->message_id));
   }
 }
 
@@ -757,7 +751,7 @@ static void make_reference_headers(struct Envelope *curenv,
      discouraged by RfC2822, sect. 3.6.4 */
   if (ctx->tagged > 0 && !STAILQ_EMPTY(&env->in_reply_to) &&
       STAILQ_NEXT(STAILQ_FIRST(&env->in_reply_to), entries))
-    mutt_free_stailq(&env->references);
+    mutt_stailq_free(&env->references);
 }
 
 static int envelope_defaults(struct Envelope *env, struct Context *ctx,
@@ -1255,8 +1249,8 @@ static int is_reply(struct Header *reply, struct Header *orig)
 {
   if (!reply || !reply->env || !orig || !orig->env)
     return 0;
-  return mutt_find_stailq(&orig->env->references, reply->env->message_id) ||
-         mutt_find_stailq(&orig->env->in_reply_to, reply->env->message_id);
+  return mutt_stailq_find(&orig->env->references, reply->env->message_id) ||
+         mutt_stailq_find(&orig->env->in_reply_to, reply->env->message_id);
 }
 
 static int has_recips(struct Address *a)
index ba6bf59b98a556804502f1321fc76259b6326685..47c01ac8748643f4d6cfa868625dcf081fed5470 100644 (file)
--- a/thread.c
+++ b/thread.c
@@ -1457,8 +1457,8 @@ static void clean_references(struct MuttThread *brk, struct MuttThread *cur)
 
 void mutt_break_thread(struct Header *hdr)
 {
-  mutt_free_stailq(&hdr->env->in_reply_to);
-  mutt_free_stailq(&hdr->env->references);
+  mutt_stailq_free(&hdr->env->in_reply_to);
+  mutt_stailq_free(&hdr->env->references);
   hdr->env->irt_changed = hdr->env->refs_changed = hdr->changed = true;
 
   clean_references(hdr->thread, hdr->thread->child);
@@ -1470,12 +1470,7 @@ static bool link_threads(struct Header *parent, struct Header *child, struct Con
     return false;
 
   mutt_break_thread(child);
-
-  STAILQ_INIT(&child->env->in_reply_to);
-  struct STailQNode *new = safe_calloc(1, sizeof(struct STailQNode));
-  new->data = safe_strdup(parent->env->message_id);
-  STAILQ_INSERT_HEAD(&child->env->in_reply_to, new, entries);
-
+  mutt_stailq_insert_head(&child->env->in_reply_to, safe_strdup(parent->env->message_id));
   mutt_set_flag(ctx, child, MUTT_TAG, 0);
 
   child->env->irt_changed = child->changed = true;