NeoMutt  2024-02-01-23-g345d7b
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
prex.c
Go to the documentation of this file.
1
30#include "config.h"
31#include <assert.h>
32#include <stdbool.h>
33#include <stdint.h>
34#include "prex.h"
35#include "logging2.h"
36#include "memory.h"
37
38#ifdef HAVE_PCRE2
39#define PCRE2_CODE_UNIT_WIDTH 8
40#include <pcre2.h>
41#include <string.h>
42
47static bool pcre2_has_unicode(void)
48{
49 static uint32_t checked = -1;
50 if (checked == -1)
51 {
52 pcre2_config(PCRE2_CONFIG_UNICODE, &checked);
53 }
54 return checked;
55}
56#endif
57
69{
70 enum Prex which;
71 size_t nmatches;
72 const char *str;
73#ifdef HAVE_PCRE2
74 pcre2_code *re;
75 pcre2_match_data *mdata;
76#else
77 regex_t *re;
78#endif
79 regmatch_t *matches;
80};
81
82#define PREX_MONTH "(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)"
83#define PREX_DOW "(Mon|Tue|Wed|Thu|Fri|Sat|Sun)"
84#define PREX_DOW_NOCASE \
85 "([Mm][Oo][Nn]|[Tt][Uu][Ee]|[Ww][Ee][Dd]|[Tt][Hh][Uu]|[Ff][Rr][Ii]|[Ss][Aa][Tt]|[Ss][Uu][Nn])"
86#define PREX_TIME "([[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2})"
87#define PREX_YEAR "([[:digit:]]{4})"
88
97static struct PrexStorage *prex(enum Prex which)
98{
99 static struct PrexStorage storage[] = {
100 // clang-format off
101 {
102 PREX_URL,
104 /* Spec: https://tools.ietf.org/html/rfc3986#section-3 */
105#ifdef HAVE_PCRE2
106#define UNR_PCTENC_SUBDEL "][\\p{L}\\p{N}._~%!$&'()*+,;="
107#else
108#define UNR_PCTENC_SUBDEL "][[:alnum:]._~%!$&'()*+,;="
109#endif
110#define PATH ":@/ "
111 "^([[:alpha:]][-+.[:alnum:]]+):" // . scheme
112 "(" // . rest
113 "(" // . . authority + path
114 // . . or path only
115 "(//" // . . . authority + path
116 "(" // . . . . user info
117 "([" UNR_PCTENC_SUBDEL "@-]*)" // . . . . . user name + '@'
118 "(:([" UNR_PCTENC_SUBDEL "-]*))?" // . . . . . password
119 "@)?"
120 "(" // . . . . host
121 "([" UNR_PCTENC_SUBDEL "-]*)" // . . . . . host name
122 "|"
123 "(\\[[[:xdigit:]:.]+\\])" // . . . . . IPv4 or IPv6
124 ")"
125 "(:([[:digit:]]+))?" // . . . . port
126 "(/([" UNR_PCTENC_SUBDEL PATH "-]*))?" // . . . . path
127 ")"
128 "|"
129 "(" // . . . path only
130 "[" UNR_PCTENC_SUBDEL PATH "-]*" // . . . . path
131 ")"
132 ")"
133 // Should be: "(\\?([" UNR_PCTENC_SUBDEL PATH "?-]*))?"
134 "(\\?([^#]*))?" // . . query
135 ")$"
136#undef PATH
137#undef UNR_PCTENC_SUBDEL
138 },
139 {
142#define QUERY_PART "^&=" // Should be: "-[:alnum:]._~%!$'()*+,;:@/"
143 "([" QUERY_PART "]+)=([" QUERY_PART "]+)" // query + ' '
144#undef QUERY_PART
145 },
146 {
149 "=\\?"
150 "([^][()<>@,;:\\\"/?. =]+)" // charset
151 "\\?"
152 "([qQbB])" // encoding
153 "\\?"
154 "([^?]+)" // encoded text - we accept whitespace, see #1189
155 "\\?="
156 },
157 {
160 "^\\#H ([[:alnum:]_\\.-]+) ([[:alnum:]]{4}( [[:alnum:]]{4}){7})[ \t]*$"
161 },
162 {
165 /* Spec: https://tools.ietf.org/html/rfc5322#section-3.3 */
166#define FWS " *"
167#define C "(\\(.*\\))?"
168#define CFWS FWS C FWS
169 "^"
170 CFWS
171 "(([[:alpha:]]+)" CFWS ", *)?" // Day of week (or whatever)
172 CFWS "([[:digit:]]{1,2}) " // Day
173 CFWS PREX_MONTH // Month
174 CFWS "([[:digit:]]{2,4}) " // Year
175 CFWS "([[:digit:]]{1,2})" // Hour
176 ":" CFWS "([[:digit:]]{1,2})" // Minute
177 CFWS
178 "(:" CFWS "([[:digit:]]{1,2}))?" // Second
179 CFWS
180 "("
181 "([+-][[:digit:]]{4})|" // TZ
182 "([[:alpha:]]+)" // Obsolete TZ
183 ")?"
184#undef CFWS
185#undef C
186#undef FWS
187 },
188 {
191 "( ([[:digit:]])|([[:digit:]]{2}))" // Day
192 "-" PREX_MONTH // Month
193 "-" PREX_YEAR // Year
194 " " PREX_TIME // Time
195 " ([+-][[:digit:]]{4})" // TZ
196 },
197 {
200 /* Spec: http://qmail.omnis.ch/man/man5/mbox.html */
201 "^From " // From
202 "([^[:space:]]+) +" // Sender
203 PREX_DOW // Day of week
204 " +"
205 PREX_MONTH // Month
206 " ( ([[:digit:]])|([[:digit:]]{2}))" // Day
207 " +"
208 PREX_TIME // Time
209 " +"
210 PREX_YEAR // Year
211 },
212 {
215 /* Spec: http://qmail.omnis.ch/man/man5/mbox.html */
216 "^From " // From
217 "("
218 "[^[:space:]]+" // Sender
219 "( at [^[:space:]]+)?" // Possibly obfuscated, pipermail-style
220 ")?"
221 " *"
222 PREX_DOW_NOCASE // Day of week
223 " +"
224 PREX_MONTH // Month
225 " +"
226 "( " // Day
227 "([[:digit:]])|"
228 "([[:digit:]]{2})"
229 ")"
230 " +"
231 "("
232 PREX_TIME // Time (HH:MM:SS)
233 "|"
234 "([[:digit:]]{2}" // Time (HH:MM)
235 ":[[:digit:]]{2})"
236 ")"
237 " +"
238 "("
239 "([[:alpha:] ]+)|" // Timezone name (which we skip)
240 "([+][[:digit:]]{4} )" // Timezone offset (which we skip)
241 ")?"
242 "("
243 PREX_YEAR // Year (YYYY)
244 "|"
245 "([[:digit:]]{2})" // Year (YY)
246 ")"
247 },
248 {
251 "^([[:alpha:]]+): (.*)$"
252 },
253 {
256 "^(.*)(tags:)([[:alnum:],]*) ?(.*)$"
257 },
258 // clang-format on
259 };
260
261 assert((which < PREX_MAX) && "Invalid 'which' argument");
262 struct PrexStorage *h = &storage[which];
263 assert((which == h->which) && "Fix 'storage' array");
264 if (!h->re)
265 {
266#ifdef HAVE_PCRE2
267 uint32_t opt = pcre2_has_unicode() ? PCRE2_UTF : 0;
268 int eno = 0;
269 PCRE2_SIZE eoff = 0;
270 h->re = pcre2_compile((PCRE2_SPTR8) h->str, PCRE2_ZERO_TERMINATED, opt,
271 &eno, &eoff, NULL);
272 assert(h->re && "Fix your RE");
273 h->mdata = pcre2_match_data_create_from_pattern(h->re, NULL);
274 uint32_t ccount = 0;
275 pcre2_pattern_info(h->re, PCRE2_INFO_CAPTURECOUNT, &ccount);
276 assert(((ccount + 1) == h->nmatches) && "Number of matches do not match (...)");
277 h->matches = mutt_mem_calloc(h->nmatches, sizeof(*h->matches));
278#else
279 h->re = mutt_mem_calloc(1, sizeof(*h->re));
280 const int rc = regcomp(h->re, h->str, REG_EXTENDED);
281 assert(rc == 0 && "Fix your RE");
282 h->matches = mutt_mem_calloc(h->nmatches, sizeof(*h->matches));
283#endif
284 }
285 return h;
286}
287
295regmatch_t *mutt_prex_capture(enum Prex which, const char *str)
296{
297 if (!str)
298 return NULL;
299
300 struct PrexStorage *h = prex(which);
301#ifdef HAVE_PCRE2
302 size_t len = strlen(str);
303 int rc = pcre2_match(h->re, (PCRE2_SPTR8) str, len, 0, 0, h->mdata, NULL);
304 if (rc < 0)
305 {
306 PCRE2_UCHAR errmsg[1024];
307 pcre2_get_error_message(rc, errmsg, sizeof(errmsg));
308 mutt_debug(LL_DEBUG2, "pcre2_match - <%s> -> <%s> = %s\n", h->str, str, errmsg);
309 return NULL;
310 }
311 PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(h->mdata);
312 int i = 0;
313 for (; i < rc; i++)
314 {
315 h->matches[i].rm_so = ovector[i * 2];
316 h->matches[i].rm_eo = ovector[i * 2 + 1];
317 }
318 for (; i < h->nmatches; i++)
319 {
320 h->matches[i].rm_so = -1;
321 h->matches[i].rm_eo = -1;
322 }
323#else
324 if (regexec(h->re, str, h->nmatches, h->matches, 0))
325 return NULL;
326
327 assert((h->re->re_nsub == (h->nmatches - 1)) &&
328 "Regular expression and matches enum are out of sync");
329#endif
330 return h->matches;
331}
332
337{
338 for (enum Prex which = 0; which < PREX_MAX; which++)
339 {
340 struct PrexStorage *h = prex(which);
341#ifdef HAVE_PCRE2
342 pcre2_match_data_free(h->mdata);
343 pcre2_code_free(h->re);
344#else
345 regfree(h->re);
346 FREE(&h->re);
347#endif
348 FREE(&h->matches);
349 }
350}
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
Logging Dispatcher.
@ LL_DEBUG2
Log at debug level 2.
Definition: logging2.h:44
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
Memory management wrappers.
#define FREE(x)
Definition: memory.h:45
#define PREX_DOW_NOCASE
Definition: prex.c:84
regmatch_t * mutt_prex_capture(enum Prex which, const char *str)
Match a precompiled regex against a string.
Definition: prex.c:295
#define CFWS
#define PREX_DOW
Definition: prex.c:83
#define PREX_TIME
Definition: prex.c:86
#define QUERY_PART
#define PREX_MONTH
Definition: prex.c:82
static struct PrexStorage * prex(enum Prex which)
Compile on demand and get data for a predefined regex.
Definition: prex.c:97
#define PATH
#define PREX_YEAR
Definition: prex.c:87
void mutt_prex_cleanup(void)
Cleanup heap memory allocated by compiled regexes.
Definition: prex.c:336
#define UNR_PCTENC_SUBDEL
Manage precompiled / predefined regular expressions.
@ PREX_MBOX_FROM_LAX_MATCH_MAX
Definition: prex.h:216
@ PREX_ACCOUNT_CMD_MATCH_MAX
Definition: prex.h:227
@ PREX_IMAP_DATE_MATCH_MAX
Definition: prex.h:170
@ PREX_MBOX_FROM_MATCH_MAX
Definition: prex.h:189
@ PREX_RFC2047_ENCODED_WORD_MATCH_MAX
Definition: prex.h:100
@ PREX_URL_QUERY_KEY_VAL_MATCH_MAX
Definition: prex.h:86
Prex
Predefined list of regular expressions.
Definition: prex.h:33
@ PREX_GNUTLS_CERT_HOST_HASH
[#H foo.com A76D 954B EB79 1F49 5B3A 0A0E 0681 65B1]
Definition: prex.h:37
@ PREX_MBOX_FROM_LAX
[From god@heaven.af.mil Sat Jan 3 01:05:34 1996]
Definition: prex.h:41
@ PREX_URL
[imaps://user:pass@example.com/INBOX?foo=bar]
Definition: prex.h:34
@ PREX_MBOX_FROM
[From god@heaven.af.mil Sat Jan 3 01:05:34 1996]
Definition: prex.h:40
@ PREX_ACCOUNT_CMD
key: value
Definition: prex.h:42
@ PREX_ALIAS_TAGS
tags:a,b,c
Definition: prex.h:43
@ PREX_IMAP_DATE
[16-MAR-2020 15:09:35 -0700]
Definition: prex.h:39
@ PREX_RFC5322_DATE_LAX
[Mon, (Comment) 16 Mar 2020 15:09:35 -0700]
Definition: prex.h:38
@ PREX_URL_QUERY_KEY_VAL
https://example.com/?[q=foo]
Definition: prex.h:35
@ PREX_MAX
Definition: prex.h:44
@ PREX_RFC2047_ENCODED_WORD
[=?utf-8?Q?=E8=81=AA=E6=98=8E=E7=9A=84?=]
Definition: prex.h:36
@ PREX_RFC5322_DATE_LAX_MATCH_MAX
Definition: prex.h:152
@ PREX_URL_MATCH_MAX
Definition: prex.h:73
@ PREX_GNUTLS_CERT_HOST_HASH_MATCH_MAX
Definition: prex.h:114
@ PREX_ALIAS_TAGS_MATCH_MAX
Definition: prex.h:241
A predefined / precompiled regex.
Definition: prex.c:69
const char * str
Regex string.
Definition: prex.c:72
enum Prex which
Regex type, e.g. PREX_URL.
Definition: prex.c:70
size_t nmatches
Number of regex matches.
Definition: prex.c:71
regex_t * re
Compiled regex.
Definition: prex.c:77
regmatch_t * matches
Resulting matches.
Definition: prex.c:79