rationalize list handling in mutt a bit.
[apps/madmutt.git] / lib-lib / rx.c
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; either version 2 of the License, or (at
5  *  your option) any later version.
6  *
7  *  This program is distributed in the hope that it will be useful, but
8  *  WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  *  General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program; if not, write to the Free Software
14  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
15  *  MA 02110-1301, USA.
16  *
17  *  Copyright © 2006 Pierre Habouzit
18  */
19 /*
20  * This file is part of mutt-ng, see http://www.muttng.org/.
21  * It's licensed under the GNU General Public License,
22  * please see the file GPL in the top level source directory.
23  */
24
25 #if HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <lib-lib/mem.h>
30 #include <lib-lib/str.h>
31 #include <lib-lib/rx.h>
32
33 rx_t *rx_compile(const char *s, int flags)
34 {
35     rx_t *pp = p_new(rx_t, 1);
36
37     pp->pattern = m_strdup(s);
38     pp->rx = p_new(regex_t, 1);
39
40     if (REGCOMP(pp->rx, NONULL(s), flags) != 0) {
41         rx_delete(&pp);
42     }
43
44     return pp;
45 }
46
47 void rx_delete(rx_t **p)
48 {
49     p_delete(&(*p)->pattern);
50     regfree((*p)->rx);
51     p_delete(&(*p)->rx);
52     p_delete(p);
53 }
54
55 int rx_list_match(list2_t *l, const char *pat)
56 {
57     int i;
58
59     if (!pat || !*pat || list_empty(l))
60         return 0;
61
62     for (i = 0; i < l->length; i++) {
63         if (!REGEXEC(((rx_t*)l->data[i])->rx, pat))
64             return 1;
65     }
66
67     return 0;
68 }
69
70 int rx_lookup (list2_t *l, const char *pat)
71 {
72     int i;
73
74     if (!pat || !*pat || list_empty(l))
75         return -1;
76
77     for (i = 0; i < l->length; i++) {
78         if (!strcmp(((rx_t*)l->data[i])->pattern, pat))
79             return i;
80     }
81
82     return -1;
83 }
84
85 int rx_sanitize_string(char *dst, ssize_t n, const char *src)
86 {
87     while (*src) {
88         if (n <= 1)
89             break;
90
91         /* these characters must be escaped in regular expressions */
92         if (strchr("^.[$()|*+?{\\", *src)) {
93             if (n <= 2)
94                 break;
95
96             *dst++ = '\\';
97             n--;
98         }
99
100         *dst++ = *src++;
101         n--;
102     }
103
104     *dst = '\0';
105
106     return *src ? -1 : 0;
107 }