]> granicus.if.org Git - neomutt/commitdiff
Refactor msg_search
authorFederico Kircheis <federico.kircheis@gmail.com>
Tue, 6 Mar 2018 18:24:31 +0000 (19:24 +0100)
committerRichard Russon <rich@flatcap.org>
Thu, 8 Mar 2018 03:18:02 +0000 (03:18 +0000)
pattern.c

index ca95cb0a4e0bcc77478210e2e451d9fbf3d9b2e9..8285f917750e2a46019f41548aec6eba42ee100c 100644 (file)
--- a/pattern.c
+++ b/pattern.c
@@ -891,14 +891,16 @@ static int patmatch(const struct Pattern *pat, const char *buf)
 
 static int msg_search(struct Context *ctx, struct Pattern *pat, int msgno)
 {
-  struct Message *msg = NULL;
-  struct State s;
+  int match = 0;
+  struct Message *msg = mx_open_message(ctx, msgno);
+  if (!msg)
+  {
+    return match;
+  }
+
   FILE *fp = NULL;
   long lng = 0;
-  int match = 0;
   struct Header *h = ctx->hdrs[msgno];
-  char *buf = NULL;
-  size_t blen;
 #ifdef USE_FMEMOPEN
   char *temp = NULL;
   size_t tempsize;
@@ -907,141 +909,138 @@ static int msg_search(struct Context *ctx, struct Pattern *pat, int msgno)
   struct stat st;
 #endif
 
-  msg = mx_open_message(ctx, msgno);
-  if (msg)
+  if (ThoroughSearch)
   {
-    if (ThoroughSearch)
-    {
-      /* decode the header / body */
-      memset(&s, 0, sizeof(s));
-      s.fpin = msg->fp;
-      s.flags = MUTT_CHARCONV;
+    /* decode the header / body */
+    struct State s;
+    memset(&s, 0, sizeof(s));
+    s.fpin = msg->fp;
+    s.flags = MUTT_CHARCONV;
 #ifdef USE_FMEMOPEN
-      s.fpout = open_memstream(&temp, &tempsize);
-      if (!s.fpout)
-      {
-        mutt_perror(_("Error opening memstream"));
-        return 0;
-      }
+    s.fpout = open_memstream(&temp, &tempsize);
+    if (!s.fpout)
+    {
+      mutt_perror(_("Error opening memstream"));
+      return 0;
+    }
 #else
-      mutt_mktemp(tempfile, sizeof(tempfile));
-      s.fpout = mutt_file_fopen(tempfile, "w+");
-      if (!s.fpout)
-      {
-        mutt_perror(tempfile);
-        return 0;
-      }
+    mutt_mktemp(tempfile, sizeof(tempfile));
+    s.fpout = mutt_file_fopen(tempfile, "w+");
+    if (!s.fpout)
+    {
+      mutt_perror(tempfile);
+      return 0;
+    }
 #endif
 
-      if (pat->op != MUTT_BODY)
-        mutt_copy_header(msg->fp, h, s.fpout, CH_FROM | CH_DECODE, NULL);
+    if (pat->op != MUTT_BODY)
+      mutt_copy_header(msg->fp, h, s.fpout, CH_FROM | CH_DECODE, NULL);
 
-      if (pat->op != MUTT_HEADER)
-      {
-        mutt_parse_mime_message(ctx, h);
+    if (pat->op != MUTT_HEADER)
+    {
+      mutt_parse_mime_message(ctx, h);
 
-        if (WithCrypto && (h->security & ENCRYPT) && !crypt_valid_passphrase(h->security))
+      if (WithCrypto && (h->security & ENCRYPT) && !crypt_valid_passphrase(h->security))
+      {
+        mx_close_message(ctx, &msg);
+        if (s.fpout)
         {
-          mx_close_message(ctx, &msg);
-          if (s.fpout)
-          {
-            mutt_file_fclose(&s.fpout);
+          mutt_file_fclose(&s.fpout);
 #ifdef USE_FMEMOPEN
-            FREE(&temp);
+          FREE(&temp);
 #else
-            unlink(tempfile);
+          unlink(tempfile);
 #endif
-          }
-          return 0;
         }
-
-        fseeko(msg->fp, h->offset, SEEK_SET);
-        mutt_body_handler(h->content, &s);
+        return 0;
       }
 
+      fseeko(msg->fp, h->offset, SEEK_SET);
+      mutt_body_handler(h->content, &s);
+    }
+
 #ifdef USE_FMEMOPEN
-      fclose(s.fpout);
-      lng = tempsize;
+    fclose(s.fpout);
+    lng = tempsize;
 
-      if (tempsize)
+    if (tempsize)
+    {
+      fp = fmemopen(temp, tempsize, "r");
+      if (!fp)
       {
-        fp = fmemopen(temp, tempsize, "r");
-        if (!fp)
-        {
-          mutt_perror(_("Error re-opening memstream"));
-          return 0;
-        }
+        mutt_perror(_("Error re-opening memstream"));
+        return 0;
       }
-      else
-      { /* fmemopen cannot handle empty buffers */
-        fp = mutt_file_fopen("/dev/null", "r");
-        if (!fp)
-        {
-          mutt_perror(_("Error opening /dev/null"));
-          return 0;
-        }
+    }
+    else
+    { /* fmemopen cannot handle empty buffers */
+      fp = mutt_file_fopen("/dev/null", "r");
+      if (!fp)
+      {
+        mutt_perror(_("Error opening /dev/null"));
+        return 0;
       }
+    }
 #else
-      fp = s.fpout;
-      fflush(fp);
-      fseek(fp, 0, SEEK_SET);
-      fstat(fileno(fp), &st);
-      lng = (long) st.st_size;
+    fp = s.fpout;
+    fflush(fp);
+    fseek(fp, 0, SEEK_SET);
+    fstat(fileno(fp), &st);
+    lng = (long) st.st_size;
 #endif
+  }
+  else
+  {
+    /* raw header / body */
+    fp = msg->fp;
+    if (pat->op != MUTT_BODY)
+    {
+      fseeko(fp, h->offset, SEEK_SET);
+      lng = h->content->offset - h->offset;
     }
-    else
+    if (pat->op != MUTT_HEADER)
     {
-      /* raw header / body */
-      fp = msg->fp;
-      if (pat->op != MUTT_BODY)
-      {
-        fseeko(fp, h->offset, SEEK_SET);
-        lng = h->content->offset - h->offset;
-      }
-      if (pat->op != MUTT_HEADER)
-      {
-        if (pat->op == MUTT_BODY)
-          fseeko(fp, h->content->offset, SEEK_SET);
-        lng += h->content->length;
-      }
+      if (pat->op == MUTT_BODY)
+        fseeko(fp, h->content->offset, SEEK_SET);
+      lng += h->content->length;
     }
+  }
 
-    blen = STRING;
-    buf = mutt_mem_malloc(blen);
+  size_t blen = STRING;
+  char *buf = mutt_mem_malloc(blen);
 
-    /* search the file "fp" */
-    while (lng > 0)
+  /* search the file "fp" */
+  while (lng > 0)
+  {
+    if (pat->op == MUTT_HEADER)
     {
-      if (pat->op == MUTT_HEADER)
-      {
-        buf = mutt_read_rfc822_line(fp, buf, &blen);
-        if (*buf == '\0')
-          break;
-      }
-      else if (fgets(buf, blen - 1, fp) == NULL)
-        break; /* don't loop forever */
-      if (patmatch(pat, buf) == 0)
-      {
-        match = 1;
+      buf = mutt_read_rfc822_line(fp, buf, &blen);
+      if (*buf == '\0')
         break;
-      }
-      lng -= mutt_str_strlen(buf);
     }
+    else if (fgets(buf, blen - 1, fp) == NULL)
+      break; /* don't loop forever */
+    if (patmatch(pat, buf) == 0)
+    {
+      match = 1;
+      break;
+    }
+    lng -= mutt_str_strlen(buf);
+  }
 
-    FREE(&buf);
+  FREE(&buf);
 
-    mx_close_message(ctx, &msg);
+  mx_close_message(ctx, &msg);
 
-    if (ThoroughSearch)
-    {
-      mutt_file_fclose(&fp);
+  if (ThoroughSearch)
+  {
+    mutt_file_fclose(&fp);
 #ifdef USE_FMEMOPEN
-      if (tempsize)
-        FREE(&temp);
+    if (tempsize)
+      FREE(&temp);
 #else
-      unlink(tempfile);
+    unlink(tempfile);
 #endif
-    }
   }
 
   return match;