mappings as standalone module
[apps/madmutt.git] / pattern.c
index 53cce82..b1dba84 100644 (file)
--- a/pattern.c
+++ b/pattern.c
 # include "config.h"
 #endif
 
+#include <lib-lib/mem.h>
+#include <lib-lib/ascii.h>
+#include <lib-lib/str.h>
+#include <lib-lib/macros.h>
+#include <lib-lib/file.h>
+#include <lib-lib/buffer.h>
+#include <lib-lib/mapping.h>
+
 #include "mutt.h"
-#include "buffer.h"
 #include "handler.h"
 #include "enter.h"
-#include "ascii.h"
 #include "mx.h"
-#include "mapping.h"
 #include "keymap.h"
 #include "copy.h"
 #include "mime.h"
 
-#include "lib/mem.h"
-#include "lib/intl.h"
-#include "lib/str.h"
 
 #ifdef USE_IMAP
 #include "mx.h"
@@ -95,12 +97,13 @@ struct pattern_flags {
 #endif
   {
   'x', M_REFERENCE, 0, eat_regexp}, {
+  'X', M_MIMEATTACH, 0, eat_range}, {
   'y', M_XLABEL, 0, eat_regexp}, {
   'z', M_SIZE, 0, eat_range}, {
   '=', M_DUPLICATED, 0, NULL}, {
   '$', M_UNREFERENCED, 0, NULL}, {
   '*', M_REALNAME, 0, NULL}, {
-  0}
+  0, 0, 0, NULL}
 };
 
 static pattern_t *SearchPattern = NULL; /* current search pattern */
@@ -161,7 +164,7 @@ msg_search (CONTEXT *ctx, pattern_t* pat, int msgno)
   if ((msg = mx_open_message (ctx, msgno)) != NULL) {
     if (option (OPTTHOROUGHSRC)) {
       /* decode the header / body */
-      memset (&s, 0, sizeof (s));
+      p_clear(&s, 1);
       s.fpin = msg->fp;
       s.flags = M_CHARCONV;
       mutt_mktemp (tempfile);
@@ -186,13 +189,13 @@ msg_search (CONTEXT *ctx, pattern_t* pat, int msgno)
           return (0);
         }
 
-        fseek (msg->fp, h->offset, 0);
+        fseeko (msg->fp, h->offset, 0);
         mutt_body_handler (h->content, &s);
       }
 
       fp = s.fpout;
       fflush (fp);
-      fseek (fp, 0, 0);
+      fseeko (fp, 0, 0);
       fstat (fileno (fp), &st);
       lng = (long) st.st_size;
     }
@@ -200,34 +203,33 @@ msg_search (CONTEXT *ctx, pattern_t* pat, int msgno)
       /* raw header / body */
       fp = msg->fp;
       if (pat->op != M_BODY) {
-        fseek (fp, h->offset, 0);
+        fseeko (fp, h->offset, 0);
         lng = h->content->offset - h->offset;
       }
       if (pat->op != M_HEADER) {
         if (pat->op == M_BODY)
-          fseek (fp, h->content->offset, 0);
+          fseeko (fp, h->content->offset, 0);
         lng += h->content->length;
       }
     }
 
-    blen = STRING;
-    buf = mem_malloc (blen);
+    buf = p_new(char, blen = STRING);
 
     /* search the file "fp" */
     while (lng > 0) {
       if (pat->op == M_HEADER) {
         if (*(buf = mutt_read_rfc822_line (fp, buf, &blen)) == '\0')
           break;
-      } if (fgets (buf, blen - 1, fp) == NULL)
+      } else if (fgets (buf, blen - 1, fp) == NULL)
         break;                  /* don't loop forever */
       if (patmatch (pat, buf) == 0) {
         match = 1;
         break;
       }
-      lng -= str_len (buf);
+      lng -= m_strlen(buf);
     }
 
-    mem_free (&buf);
+    p_delete(&buf);
 
     mx_close_message (&msg);
 
@@ -245,13 +247,19 @@ int eat_regexp (pattern_t * pat, BUFFER * s, BUFFER * err)
   BUFFER buf;
   int r;
 
-  memset (&buf, 0, sizeof (buf));
+  p_clear(&buf, 1);
+
   if (mutt_extract_token (&buf, s, M_TOKEN_PATTERN | M_TOKEN_COMMENT) != 0 ||
       !buf.data) {
     snprintf (err->data, err->dsize, _("Error in expression: %s"), s->dptr);
     return (-1);
   }
 
+  if (!*buf.data) {
+    snprintf (err->data, err->dsize, _("Empty expression"));
+    return (-1);
+  }
+
 #if 0
  /* If there are no RE metacharacters, use simple search anyway */
   if (!pat->stringmatch && !strpbrk (buf.data, "|[{.*+?^$"))
@@ -259,16 +267,16 @@ int eat_regexp (pattern_t * pat, BUFFER * s, BUFFER * err)
 #endif
 
   if (pat->stringmatch) {
-    pat->str = str_dup (buf.data);
-    mem_free (&buf.data);
+    pat->str = m_strdup(buf.data);
+    p_delete(&buf.data);
   } else {
-    pat->rx = mem_malloc (sizeof (regex_t));
+    pat->rx = p_new(regex_t, 1);
     r = REGCOMP (pat->rx, buf.data, REG_NEWLINE | REG_NOSUB | mutt_which_case (buf.data));
-    mem_free (&buf.data);
+    p_delete(&buf.data);
     if (r) {
       regerror (r, pat->rx, err->data, err->dsize);
       regfree (pat->rx);
-      mem_free (&pat->rx);
+      p_delete(&pat->rx);
       return (-1);
     }
   }
@@ -351,8 +359,7 @@ int eat_range (pattern_t * pat, BUFFER * s, BUFFER * err)
   if (skip_quote && *tmp == '"')
     tmp++;
 
-  SKIPWS (tmp);
-  s->dptr = tmp;
+  s->dptr = vskipspaces(tmp);
   return 0;
 }
 
@@ -460,7 +467,7 @@ static const char *parse_date_range (const char *pc, struct tm *min,
     const char *pt;
     char ch = *pc++;
 
-    SKIPWS (pc);
+    pc = vskipspaces(pc);
     switch (ch) {
     case '-':
       {
@@ -516,7 +523,7 @@ static const char *parse_date_range (const char *pc, struct tm *min,
     default:
       flag |= M_PDR_ERRORDONE;
     }
-    SKIPWS (pc);
+    pc = vskipspaces(pc);
   }
   if ((flag & M_PDR_ERROR) && !(flag & M_PDR_ABSOLUTE)) {       /* getDate has its own error message, don't overwrite it here */
     snprintf (err->data, err->dsize, _("Invalid relative date: %s"), pc - 1);
@@ -529,21 +536,21 @@ static int eat_date (pattern_t * pat, BUFFER * s, BUFFER * err)
   BUFFER buffer;
   struct tm min, max;
 
-  memset (&buffer, 0, sizeof (buffer));
+  p_clear(&buffer, 1);
   if (mutt_extract_token (&buffer, s, M_TOKEN_COMMENT | M_TOKEN_PATTERN) != 0
       || !buffer.data) {
-    strfcpy (err->data, _("error in expression"), err->dsize);
+    m_strcpy(err->data, err->dsize, _("error in expression"));
     return (-1);
   }
 
-  memset (&min, 0, sizeof (min));
+  p_clear(&min, 1);
   /* the `0' time is Jan 1, 1970 UTC, so in order to prevent a negative time
      when doing timezone conversion, we use Jan 2, 1970 UTC as the base
      here */
   min.tm_mday = 2;
   min.tm_year = 70;
 
-  memset (&max, 0, sizeof (max));
+  p_clear(&max, 1);
 
   /* Arbitrary year in the future.  Don't set this too high
      or mutt_mktime() returns something larger than will
@@ -596,15 +603,15 @@ static int eat_date (pattern_t * pat, BUFFER * s, BUFFER * err)
     if (isdigit ((unsigned char) *pc)) {
       /* mininum date specified */
       if ((pc = getDate (pc, &min, err)) == NULL) {
-        mem_free (&buffer.data);
+        p_delete(&buffer.data);
         return (-1);
       }
       haveMin = TRUE;
-      SKIPWS (pc);
+      pc = vskipspaces(pc);
       if (*pc == '-') {
-        const char *pt = pc + 1;
+        const char *pt;
 
-        SKIPWS (pt);
+        pt = skipspaces(pc + 1);
         untilNow = (*pt == '\0');
       }
     }
@@ -629,7 +636,7 @@ static int eat_date (pattern_t * pat, BUFFER * s, BUFFER * err)
       max.tm_mday = min.tm_mday;
 
       if (!parse_date_range (pc, &min, &max, haveMin, &baseMin, err)) { /* bail out on any parsing error */
-        mem_free (&buffer.data);
+        p_delete(&buffer.data);
         return (-1);
       }
     }
@@ -641,7 +648,7 @@ static int eat_date (pattern_t * pat, BUFFER * s, BUFFER * err)
   pat->min = mutt_mktime (&min, 1);
   pat->max = mutt_mktime (&max, 1);
 
-  mem_free (&buffer.data);
+  p_delete(&buffer.data);
 
   return 0;
 }
@@ -682,12 +689,12 @@ void mutt_pattern_free (pattern_t ** pat)
 
     if (tmp->rx) {
       regfree (tmp->rx);
-      mem_free (&tmp->rx);
+      p_delete(&tmp->rx);
     }
-    mem_free (&tmp->str);
+    p_delete(&tmp->str);
     if (tmp->child)
       mutt_pattern_free (&tmp->child);
-    mem_free (&tmp);
+    p_delete(&tmp);
   }
 }
 
@@ -698,7 +705,6 @@ pattern_t *mutt_pattern_comp ( /* const */ char *s, int flags, BUFFER * err)
   pattern_t *last = NULL;
   int not = 0;
   int alladdr = 0;
-  int stringmatch = 0;
   int or = 0;
   int implicit = 1;             /* used to detect logical AND operator */
   struct pattern_flags *entry;
@@ -706,12 +712,12 @@ pattern_t *mutt_pattern_comp ( /* const */ char *s, int flags, BUFFER * err)
   char *buf;
   BUFFER ps;
 
-  memset (&ps, 0, sizeof (ps));
+  p_clear(&ps, 1);
   ps.dptr = s;
-  ps.dsize = str_len (s);
+  ps.dsize = m_strlen(s);
 
   while (*ps.dptr) {
-    SKIPWS (ps.dptr);
+    ps.dptr = vskipspaces(ps.dptr);
     switch (*ps.dptr) {
     case '^':
       ps.dptr++;
@@ -744,10 +750,8 @@ pattern_t *mutt_pattern_comp ( /* const */ char *s, int flags, BUFFER * err)
       implicit = 0;
       not = 0;
       alladdr = 0;
-      stringmatch = 0;
       break;
     case '=':
-      stringmatch = 1;
       /* fallthrough */
     case '~':
       if (implicit && or) {
@@ -763,10 +767,9 @@ pattern_t *mutt_pattern_comp ( /* const */ char *s, int flags, BUFFER * err)
       tmp = new_pattern ();
       tmp->not = not;
       tmp->alladdr = alladdr;
-      tmp->stringmatch = stringmatch;
+      tmp->stringmatch = (*ps.dptr == '=') ? 1 : 0;
       not = 0;
       alladdr = 0;
-      stringmatch = 0;
 
       if (last)
         last->next = tmp;
@@ -788,8 +791,7 @@ pattern_t *mutt_pattern_comp ( /* const */ char *s, int flags, BUFFER * err)
       }
       tmp->op = entry->op;
 
-      ps.dptr++;                /* eat the operator and any optional whitespace */
-      SKIPWS (ps.dptr);
+      ps.dptr = vskipspaces(ps.dptr + 1);
 
       if (entry->eat_arg) {
         if (!*ps.dptr) {
@@ -815,11 +817,11 @@ pattern_t *mutt_pattern_comp ( /* const */ char *s, int flags, BUFFER * err)
       /* compile the sub-expression */
       buf = str_substrdup (ps.dptr + 1, p);
       if ((tmp = mutt_pattern_comp (buf, flags, err)) == NULL) {
-        mem_free (&buf);
+        p_delete(&buf);
         mutt_pattern_free (&curlist);
         return NULL;
       }
-      mem_free (&buf);
+      p_delete(&buf);
       if (last)
         last->next = tmp;
       else
@@ -838,7 +840,7 @@ pattern_t *mutt_pattern_comp ( /* const */ char *s, int flags, BUFFER * err)
     }
   }
   if (!curlist) {
-    strfcpy (err->data, _("empty pattern"), err->dsize);
+    m_strcpy(err->data, err->dsize, _("empty pattern"));
     return NULL;
   }
   if (curlist->next) {
@@ -999,7 +1001,7 @@ mutt_pattern_exec (struct pattern_t *pat, pattern_exec_flag flags,
   case M_WHOLE_MSG:
 #ifdef USE_IMAP
     /* IMAP search sets h->matched at search compile time */
-    if (Context->magic == M_IMAP && pat->stringmatch)
+    if (ctx->magic == M_IMAP && pat->stringmatch)
       return (h->matched);
 #endif
     return (pat->not ^ msg_search (ctx, pat, h->msgno));
@@ -1092,6 +1094,23 @@ mutt_pattern_exec (struct pattern_t *pat, pattern_exec_flag flags,
                    && patmatch (pat, h->env->spam->data) == 0));
   case M_DUPLICATED:
     return (pat->not ^ (h->thread && h->thread->duplicate_thread));
+
+  case M_MIMEATTACH:
+    {
+      int count;
+      
+      if (h->content->parts)
+        count = mutt_count_body_parts(h, 0);
+      else {
+        mutt_parse_mime_message(ctx, h);
+        count = mutt_count_body_parts(h, 0);
+        mutt_free_body(&h->content->parts);
+      }
+      
+      return (pat->not ^ (count >= pat->min && (pat->max == M_MAXRANGE ||
+                                                count <= pat->max)));
+    }
+
   case M_UNREFERENCED:
     return (pat->not ^ (h->thread && !h->thread->child));
   case M_MULTIPART:
@@ -1150,24 +1169,24 @@ void mutt_check_simple (char *s, size_t len, const char *simple)
 
   if (!strchr (s, '~') && !strchr (s, '=')) {       /* yup, so spoof a real request */
     /* convert old tokens into the new format */
-    if (ascii_strcasecmp ("all", s) == 0 || !str_cmp ("^", s) || !str_cmp (".", s))     /* ~A is more efficient */
-      strfcpy (s, "~A", len);
+    if (ascii_strcasecmp ("all", s) == 0 || !m_strcmp("^", s) || !m_strcmp(".", s))     /* ~A is more efficient */
+      m_strcpy(s, len, "~A");
     else if (ascii_strcasecmp ("del", s) == 0)
-      strfcpy (s, "~D", len);
+      m_strcpy(s, len, "~D");
     else if (ascii_strcasecmp ("flag", s) == 0)
-      strfcpy (s, "~F", len);
+      m_strcpy(s, len, "~F");
     else if (ascii_strcasecmp ("new", s) == 0)
-      strfcpy (s, "~N", len);
+      m_strcpy(s, len, "~N");
     else if (ascii_strcasecmp ("old", s) == 0)
-      strfcpy (s, "~O", len);
+      m_strcpy(s, len, "~O");
     else if (ascii_strcasecmp ("repl", s) == 0)
-      strfcpy (s, "~Q", len);
+      m_strcpy(s, len, "~Q");
     else if (ascii_strcasecmp ("read", s) == 0)
-      strfcpy (s, "~R", len);
+      m_strcpy(s, len, "~R");
     else if (ascii_strcasecmp ("tag", s) == 0)
-      strfcpy (s, "~T", len);
+      m_strcpy(s, len, "~T");
     else if (ascii_strcasecmp ("unread", s) == 0)
-      strfcpy (s, "~U", len);
+      m_strcpy(s, len, "~U");
     else {
       quote_simple (tmp, sizeof (tmp), s);
       mutt_expand_fmt (s, len, simple, tmp);
@@ -1182,26 +1201,20 @@ int mutt_pattern_func (int op, char *prompt)
   BUFFER err;
   int i;
 
-  strfcpy (buf, NONULL (Context->pattern), sizeof (buf));
+  m_strcpy(buf, sizeof(buf), NONULL(Context->pattern));
   if (prompt || op != M_LIMIT)
-    if (mutt_get_field (prompt, buf, sizeof (buf), M_PATTERN | M_CLEAR) != 0)
+    if (mutt_get_field (prompt, buf, sizeof (buf), M_PATTERN | M_CLEAR) != 0 || !buf[0])
       return (-1);
-  if (!buf[0]) {
-    if (op == M_LIMIT)
-      strncpy (buf, "~A", sizeof (buf));
-    else
-      return (-1);
-  }
 
   mutt_message _("Compiling search pattern...");
 
-  simple = str_dup (buf);
+  simple = m_strdup(buf);
   mutt_check_simple (buf, sizeof (buf), NONULL (SimpleSearch));
 
   err.data = error;
   err.dsize = sizeof (error);
   if ((pat = mutt_pattern_comp (buf, M_FULL_MSG, &err)) == NULL) {
-    mem_free (&simple);
+    p_delete(&simple);
     mutt_error ("%s", err.data);
     return (-1);
   }
@@ -1266,7 +1279,7 @@ int mutt_pattern_func (int op, char *prompt)
 
   if (op == M_LIMIT) {
     /* drop previous limit pattern */
-    mem_free (&Context->pattern);
+    p_delete(&Context->pattern);
     if (Context->limit_pattern)
       mutt_pattern_free (&Context->limit_pattern);
     if (Context->msgcount && !Context->vcount) {
@@ -1274,13 +1287,13 @@ int mutt_pattern_func (int op, char *prompt)
     }
 
     /* record new limit pattern, unless match all */
-    if (str_ncmp (buf, "~A", 2) != 0) {
+    if (m_strncmp(buf, "~A", 2) != 0) {
       Context->pattern = simple;
       simple = NULL;            /* don't clobber it */
       Context->limit_pattern = mutt_pattern_comp (buf, M_FULL_MSG, &err);
     }
   }
-  mem_free (&simple);
+  p_delete(&simple);
   mutt_pattern_free (&pat);
   return 0;
 }
@@ -1296,7 +1309,7 @@ int mutt_search_command (int cur, int op)
   HEADER *h;
 
   if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE) {
-    strfcpy (buf, LastSearch, sizeof (buf));
+    m_strcpy(buf, sizeof(buf), LastSearch);
     if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") :
                         _("Reverse search for: "), buf, sizeof (buf),
                         M_CLEAR | M_PATTERN) != 0 || !buf[0])
@@ -1309,12 +1322,12 @@ int mutt_search_command (int cur, int op)
 
     /* compare the *expanded* version of the search pattern in case 
        $simple_search has changed while we were searching */
-    strfcpy (temp, buf, sizeof (temp));
+    m_strcpy(temp, sizeof(temp), buf);
     mutt_check_simple (temp, sizeof (temp), NONULL (SimpleSearch));
 
-    if (!SearchPattern || str_cmp (temp, LastSearchExpn)) {
+    if (!SearchPattern || m_strcmp(temp, LastSearchExpn)) {
       set_option (OPTSEARCHINVALID);
-      strfcpy (LastSearch, buf, sizeof (LastSearch));
+      m_strcpy(LastSearch, sizeof(LastSearch), buf);
       mutt_message _("Compiling search pattern...");
 
       mutt_pattern_free (&SearchPattern);