X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=blobdiff_plain;f=pattern.c;h=ea72544f65a08ea375d726133511560be1e82603;hp=868d7abbcb2b409cb4834672d2fddc0a6b537275;hb=f3cbb9f51357972f6e74244494236a41dc4d84cd;hpb=d4321859bb9ce5b43fbd8303394cf98ae7720a38 diff --git a/pattern.c b/pattern.c index 868d7ab..ea72544 100644 --- a/pattern.c +++ b/pattern.c @@ -7,101 +7,80 @@ * please see the file GPL in the top level source directory. */ -#if HAVE_CONFIG_H -# include "config.h" -#endif - -#include -#include -#include -#include -#include -#include -#include +#include #include - +#include #include +#include +#include +#include -#include "mutt.h" +#include "pattern.h" +#include "alias.h" #include "handler.h" -#include "mx.h" #include "keymap.h" #include "copy.h" -#include - -#include -#include -#include -#include -#include -#include - -#include - static int eat_regexp (pattern_t * pat, BUFFER *, BUFFER *); static int eat_date (pattern_t * pat, BUFFER *, BUFFER *); static int eat_range (pattern_t * pat, BUFFER *, BUFFER *); static int patmatch (const pattern_t* pat, const char* buf); -struct pattern_flags { +static struct pattern_flags { int tag; /* character used to represent this op */ int op; /* operation to perform */ int class; - int (*eat_arg) (pattern_t *, BUFFER *, BUFFER *); -} Flags[] = { - { - 'A', M_ALL, 0, NULL}, { - 'b', M_BODY, M_FULL_MSG, eat_regexp}, { - 'B', M_WHOLE_MSG, M_FULL_MSG, eat_regexp}, { - 'c', M_CC, 0, eat_regexp}, { - 'C', M_RECIPIENT, 0, eat_regexp}, { - 'd', M_DATE, 0, eat_date}, { - 'D', M_DELETED, 0, NULL}, { - 'e', M_SENDER, 0, eat_regexp}, { - 'E', M_EXPIRED, 0, NULL}, { - 'f', M_FROM, 0, eat_regexp}, { - 'F', M_FLAG, 0, NULL}, { - 'g', M_CRYPT_SIGN, 0, NULL}, { - 'G', M_CRYPT_ENCRYPT, 0, NULL}, { - 'h', M_HEADER, M_FULL_MSG, eat_regexp}, { - 'H', M_HORMEL, 0, eat_regexp}, { - 'i', M_ID, 0, eat_regexp}, { - 'k', M_PGP_KEY, 0, NULL}, { - 'L', M_ADDRESS, 0, eat_regexp}, { - 'l', M_LIST, 0, NULL}, { - 'm', M_MESSAGE, 0, eat_range}, { - 'M', M_MULTIPART, 0, NULL}, { - 'n', M_SCORE, 0, eat_range}, { - 'N', M_NEW, 0, NULL}, { - 'O', M_OLD, 0, NULL}, { - 'p', M_PERSONAL_RECIP, 0, NULL}, { - 'P', M_PERSONAL_FROM, 0, NULL}, { - 'Q', M_REPLIED, 0, NULL}, { - 'R', M_READ, 0, NULL}, { - 'r', M_DATE_RECEIVED, 0, eat_date}, { - 's', M_SUBJECT, 0, eat_regexp}, { - 'S', M_SUPERSEDED, 0, NULL}, { - 'T', M_TAG, 0, NULL}, { - 't', M_TO, 0, eat_regexp}, { - 'U', M_UNREAD, 0, NULL}, { - 'u', M_SUBSCRIBED_LIST, 0, NULL}, { - 'v', M_COLLAPSED, 0, NULL}, { - 'V', M_CRYPT_VERIFIED, 0, NULL}, + int (*eat_arg)(pattern_t *, BUFFER *, BUFFER *); +} const Flags[] = { + {'A', M_ALL, 0, NULL}, + {'b', M_BODY, M_FULL_MSG, eat_regexp}, + {'B', M_WHOLE_MSG, M_FULL_MSG, eat_regexp}, + {'c', M_CC, 0, eat_regexp}, + {'C', M_RECIPIENT, 0, eat_regexp}, + {'d', M_DATE, 0, eat_date}, + {'D', M_DELETED, 0, NULL}, + {'e', M_SENDER, 0, eat_regexp}, + {'E', M_EXPIRED, 0, NULL}, + {'f', M_FROM, 0, eat_regexp}, + {'F', M_FLAG, 0, NULL}, + {'g', M_CRYPT_SIGN, 0, NULL}, + {'G', M_CRYPT_ENCRYPT, 0, NULL}, + {'h', M_HEADER, M_FULL_MSG, eat_regexp}, + {'H', M_HORMEL, 0, eat_regexp}, + {'i', M_ID, 0, eat_regexp}, + {'k', M_PGP_KEY, 0, NULL}, + {'L', M_ADDRESS, 0, eat_regexp}, + {'l', M_LIST, 0, NULL}, + {'m', M_MESSAGE, 0, eat_range}, + {'M', M_MULTIPART, 0, NULL}, + {'n', M_SCORE, 0, eat_range}, + {'N', M_NEW, 0, NULL}, + {'O', M_OLD, 0, NULL}, + {'p', M_PERSONAL_RECIP, 0, NULL}, + {'P', M_PERSONAL_FROM, 0, NULL}, + {'Q', M_REPLIED, 0, NULL}, + {'R', M_READ, 0, NULL}, + {'r', M_DATE_RECEIVED, 0, eat_date}, + {'s', M_SUBJECT, 0, eat_regexp}, + {'S', M_SUPERSEDED, 0, NULL}, + {'T', M_TAG, 0, NULL}, + {'t', M_TO, 0, eat_regexp}, + {'U', M_UNREAD, 0, NULL}, + {'u', M_SUBSCRIBED_LIST, 0, NULL}, + {'v', M_COLLAPSED, 0, NULL}, + {'V', M_CRYPT_VERIFIED, 0, NULL}, #ifdef USE_NNTP - { - 'w', M_NEWSGROUPS, 0, eat_regexp}, + {'w', M_NEWSGROUPS, 0, eat_regexp}, #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, NULL} + {'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, NULL} }; static pattern_t *SearchPattern = NULL; /* current search pattern */ @@ -121,17 +100,14 @@ static char LastSearchExpn[LONG_STRING] = { 0 }; /* expanded version of #define M_PDR_ERROR 0x0100 #define M_PDR_ERRORDONE (M_PDR_ERROR | M_PDR_DONE) - -int mutt_getvaluebychar (char ch, struct mapping_t *table) +void pattern_wipe(pattern_t *pat) { - int i; - - for (i = 0; table[i].name; i++) { - if (ch == table[i].name[0]) - return table[i].value; - } - - return (-1); + if (pat->rx) { + regfree (pat->rx); + p_delete(&pat->rx); + } + p_delete(&pat->str); + pattern_list_wipe(&pat->child); } /* if no uppercase letters are given, do a case-insensitive search */ @@ -165,9 +141,9 @@ msg_search (CONTEXT *ctx, pattern_t* pat, int msgno) p_clear(&s, 1); s.fpin = msg->fp; s.flags = M_CHARCONV; - mutt_mktemp (tempfile); - if ((s.fpout = safe_fopen (tempfile, "w+")) == NULL) { - mutt_perror (tempfile); + s.fpout = m_tempfile(tempfile, sizeof(tempfile), NONULL(Tempdir), NULL); + if (!s.fpout) { + mutt_error(_("Could not create temporary file")); return (0); } @@ -181,7 +157,7 @@ msg_search (CONTEXT *ctx, pattern_t* pat, int msgno) { mx_close_message (&msg); if (fp) { - fclose (fp); + m_fclose(&fp); unlink (tempfile); } return (0); @@ -232,7 +208,7 @@ msg_search (CONTEXT *ctx, pattern_t* pat, int msgno) mx_close_message (&msg); if (option (OPTTHOROUGHSRC)) { - fclose (fp); + m_fclose(&fp); unlink (tempfile); } } @@ -258,12 +234,6 @@ int eat_regexp (pattern_t * pat, BUFFER * s, BUFFER * err) return (-1); } -#if 0 - /* If there are no RE metacharacters, use simple search anyway */ - if (!pat->stringmatch && !strpbrk (buf.data, "|[{.*+?^$")) - pat->stringmatch = 1; -#endif - if (pat->stringmatch) { pat->str = m_strdup(buf.data); p_delete(&buf.data); @@ -651,7 +621,7 @@ static int eat_date (pattern_t * pat, BUFFER * s, BUFFER * err) return 0; } -static struct pattern_flags *lookup_tag (char tag) +static const struct pattern_flags *lookup_tag (char tag) { int i; @@ -677,26 +647,7 @@ static /* const */ char *find_matching_paren ( /* const */ char *s) return s; } -void mutt_pattern_free (pattern_t ** pat) -{ - pattern_t *tmp; - - while (*pat) { - tmp = *pat; - *pat = (*pat)->next; - - if (tmp->rx) { - regfree (tmp->rx); - p_delete(&tmp->rx); - } - p_delete(&tmp->str); - if (tmp->child) - mutt_pattern_free (&tmp->child); - p_delete(&tmp); - } -} - -pattern_t *mutt_pattern_comp ( /* const */ char *s, int flags, BUFFER * err) +pattern_t *mutt_pattern_comp(const char *s, int flags, BUFFER *err) { pattern_t *curlist = NULL; pattern_t *tmp; @@ -705,13 +656,13 @@ pattern_t *mutt_pattern_comp ( /* const */ char *s, int flags, BUFFER * err) int alladdr = 0; int or = 0; int implicit = 1; /* used to detect logical AND operator */ - struct pattern_flags *entry; + const struct pattern_flags *entry; char *p; char *buf; BUFFER ps; p_clear(&ps, 1); - ps.dptr = s; + ps.dptr = s; ps.dsize = m_strlen(s); while (*ps.dptr) { @@ -734,7 +685,7 @@ pattern_t *mutt_pattern_comp ( /* const */ char *s, int flags, BUFFER * err) } if (curlist->next) { /* A & B | C == (A & B) | C */ - tmp = new_pattern (); + tmp = pattern_new(); tmp->op = M_AND; tmp->child = curlist; @@ -754,7 +705,7 @@ pattern_t *mutt_pattern_comp ( /* const */ char *s, int flags, BUFFER * err) case '~': if (implicit && or) { /* A | B & C == (A | B) & C */ - tmp = new_pattern (); + tmp = pattern_new(); tmp->op = M_OR; tmp->child = curlist; curlist = tmp; @@ -762,7 +713,7 @@ pattern_t *mutt_pattern_comp ( /* const */ char *s, int flags, BUFFER * err) or = 0; } - tmp = new_pattern (); + tmp = pattern_new(); tmp->not = not; tmp->alladdr = alladdr; tmp->stringmatch = (*ps.dptr == '=') ? 1 : 0; @@ -778,13 +729,13 @@ pattern_t *mutt_pattern_comp ( /* const */ char *s, int flags, BUFFER * err) ps.dptr++; /* move past the ~ */ if ((entry = lookup_tag (*ps.dptr)) == NULL) { snprintf (err->data, err->dsize, _("%c: invalid command"), *ps.dptr); - mutt_pattern_free (&curlist); + pattern_list_wipe(&curlist); return NULL; } if (entry->class && (flags & entry->class) == 0) { snprintf (err->data, err->dsize, _("%c: not supported in this mode"), *ps.dptr); - mutt_pattern_free (&curlist); + pattern_list_wipe(&curlist); return NULL; } tmp->op = entry->op; @@ -794,11 +745,11 @@ pattern_t *mutt_pattern_comp ( /* const */ char *s, int flags, BUFFER * err) if (entry->eat_arg) { if (!*ps.dptr) { snprintf (err->data, err->dsize, _("missing parameter")); - mutt_pattern_free (&curlist); + pattern_list_wipe(&curlist); return NULL; } if (entry->eat_arg (tmp, &ps, err) == -1) { - mutt_pattern_free (&curlist); + pattern_list_wipe(&curlist); return NULL; } } @@ -809,14 +760,14 @@ pattern_t *mutt_pattern_comp ( /* const */ char *s, int flags, BUFFER * err) if (*p != ')') { snprintf (err->data, err->dsize, _("mismatched parenthesis: %s"), ps.dptr); - mutt_pattern_free (&curlist); + pattern_list_wipe(&curlist); return NULL; } /* compile the sub-expression */ buf = p_dupstr(ps.dptr + 1, p - ps.dptr - 1); if ((tmp = mutt_pattern_comp (buf, flags, err)) == NULL) { p_delete(&buf); - mutt_pattern_free (&curlist); + pattern_list_wipe(&curlist); return NULL; } p_delete(&buf); @@ -833,7 +784,7 @@ pattern_t *mutt_pattern_comp ( /* const */ char *s, int flags, BUFFER * err) break; default: snprintf (err->data, err->dsize, _("error in pattern at: %s"), ps.dptr); - mutt_pattern_free (&curlist); + pattern_list_wipe(&curlist); return NULL; } } @@ -842,7 +793,7 @@ pattern_t *mutt_pattern_comp ( /* const */ char *s, int flags, BUFFER * err) return NULL; } if (curlist->next) { - tmp = new_pattern (); + tmp = pattern_new(); tmp->op = or ? M_OR : M_AND; tmp->child = curlist; curlist = tmp; @@ -1092,7 +1043,7 @@ mutt_pattern_exec (struct pattern_t *pat, pattern_exec_flag flags, else { mutt_parse_mime_message(ctx, h); count = mutt_count_body_parts(h, 0); - mutt_free_body(&h->content->parts); + body_list_wipe(&h->content->parts); } return (pat->not ^ (count >= pat->min && (pat->max == M_MAXRANGE || @@ -1112,7 +1063,7 @@ mutt_pattern_exec (struct pattern_t *pat, pattern_exec_flag flags, */ return (h->env && h->env->from && (mutt_addr_is_user(h->env->from) - || alias_reverse_lookup(Aliases, h->env->from) + || alias_reverse_lookup(h->env->from) || (h->env->from->personal && valid_realname(h->env-> from->personal) && h->env->from->mailbox) @@ -1143,7 +1094,7 @@ static void quote_simple (char *tmp, ssize_t len, const char *p) } /* convert a simple search into a real request */ -void mutt_check_simple (char *s, size_t len, const char *simple) +void mutt_check_simple (char *s, ssize_t len, const char *simple) { char tmp[LONG_STRING]; @@ -1173,7 +1124,7 @@ void mutt_check_simple (char *s, size_t len, const char *simple) m_strcpy(s, len, "~U"); else { quote_simple (tmp, sizeof (tmp), s); - mutt_expand_fmt (s, len, simple, tmp); + m_file_fmt(s, len, simple, tmp); } } } @@ -1263,7 +1214,7 @@ int mutt_pattern_func (int op, char *prompt) /* drop previous limit pattern */ p_delete(&Context->pattern); if (Context->limit_pattern) - mutt_pattern_free (&Context->limit_pattern); + pattern_list_wipe(&Context->limit_pattern); if (Context->msgcount && !Context->vcount) { mutt_error _("No messages matched criteria."); } @@ -1276,7 +1227,7 @@ int mutt_pattern_func (int op, char *prompt) } } p_delete(&simple); - mutt_pattern_free (&pat); + pattern_list_wipe(&pat); return 0; } @@ -1312,7 +1263,7 @@ int mutt_search_command (int cur, int op) m_strcpy(LastSearch, sizeof(LastSearch), buf); mutt_message _("Compiling search pattern..."); - mutt_pattern_free (&SearchPattern); + pattern_list_wipe(&SearchPattern); err.data = error; err.dsize = sizeof (error); if ((SearchPattern =