NeoMutt  2024-04-25-76-g20fe7b
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 <stdbool.h>
32#include <stddef.h>
33#include <stdint.h>
34#include "prex.h"
35#include "logging2.h"
36#include "memory.h"
37#include "signal2.h"
38
39#ifdef HAVE_PCRE2
40#define PCRE2_CODE_UNIT_WIDTH 8
41#include <pcre2.h>
42#include <string.h>
43
48static bool pcre2_has_unicode(void)
49{
50 static uint32_t checked = -1;
51 if (checked == -1)
52 {
53 pcre2_config(PCRE2_CONFIG_UNICODE, &checked);
54 }
55 return checked;
56}
57#endif
58
70{
71 enum Prex which;
72 size_t nmatches;
73 const char *str;
74#ifdef HAVE_PCRE2
75 pcre2_code *re;
76 pcre2_match_data *mdata;
77#else
78 regex_t *re;
79#endif
80 regmatch_t *matches;
81};
82
83#define PREX_MONTH "(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)"
84#define PREX_DOW "(Mon|Tue|Wed|Thu|Fri|Sat|Sun)"
85#define PREX_DOW_NOCASE \
86 "([Mm][Oo][Nn]|[Tt][Uu][Ee]|[Ww][Ee][Dd]|[Tt][Hh][Uu]|[Ff][Rr][Ii]|[Ss][Aa][Tt]|[Ss][Uu][Nn])"
87#define PREX_TIME "([[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2})"
88#define PREX_YEAR "([[:digit:]]{4})"
89
98static struct PrexStorage *prex(enum Prex which)
99{
100 static struct PrexStorage storage[] = {
101 // clang-format off
102 {
103 PREX_URL,
105 /* Spec: https://tools.ietf.org/html/rfc3986#section-3 */
106#ifdef HAVE_PCRE2
107#define UNR_PCTENC_SUBDEL "][\\p{L}\\p{N}._~%!$&'()*+,;="
108#else
109#define UNR_PCTENC_SUBDEL "][[:alnum:]._~%!$&'()*+,;="
110#endif
111#define PATH ":@/ "
112 "^([[:alpha:]][-+.[:alnum:]]+):" // . scheme
113 "(" // . rest
114 "(" // . . authority + path
115 // . . or path only
116 "(//" // . . . authority + path
117 "(" // . . . . user info
118 "([" UNR_PCTENC_SUBDEL "@-]*)" // . . . . . user name + '@'
119 "(:([" UNR_PCTENC_SUBDEL "-]*))?" // . . . . . password
120 "@)?"
121 "(" // . . . . host
122 "([" UNR_PCTENC_SUBDEL "-]*)" // . . . . . host name
123 "|"
124 "(\\[[[:xdigit:]:.]+\\])" // . . . . . IPv4 or IPv6
125 ")"
126 "(:([[:digit:]]+))?" // . . . . port
127 "(/([" UNR_PCTENC_SUBDEL PATH "-]*))?" // . . . . path
128 ")"
129 "|"
130 "(" // . . . path only
131 "[" UNR_PCTENC_SUBDEL PATH "-]*" // . . . . path
132 ")"
133 ")"
134 // Should be: "(\\?([" UNR_PCTENC_SUBDEL PATH "?-]*))?"
135 "(\\?([^#]*))?" // . . query
136 ")$"
137#undef PATH
138#undef UNR_PCTENC_SUBDEL
139 },
140 {
143#define QUERY_PART "^&=" // Should be: "-[:alnum:]._~%!$'()*+,;:@/"
144 "([" QUERY_PART "]+)=([" QUERY_PART "]+)" // query + ' '
145#undef QUERY_PART
146 },
147 {
150 "=\\?"
151 "([^][()<>@,;:\\\"/?. =]+)" // charset
152 "\\?"
153 "([qQbB])" // encoding
154 "\\?"
155 "([^?]+)" // encoded text - we accept whitespace, see #1189
156 "\\?="
157 },
158 {
161 "^\\#H ([[:alnum:]_\\.-]+) ([[:alnum:]]{4}( [[:alnum:]]{4}){7})[ \t]*$"
162 },
163 {
166 /* Spec: https://tools.ietf.org/html/rfc5322#section-3.3 */
167#define FWS " *"
168#define C "(\\(.*\\))?"
169#define CFWS FWS C FWS
170 "^"
171 CFWS
172 "(([[:alpha:]]+)" CFWS ", *)?" // Day of week (or whatever)
173 CFWS "([[:digit:]]{1,2}) " // Day
174 CFWS PREX_MONTH // Month
175 CFWS "([[:digit:]]{2,4}) " // Year
176 CFWS "([[:digit:]]{1,2})" // Hour
177 ":" CFWS "([[:digit:]]{1,2})" // Minute
178 CFWS
179 "(:" CFWS "([[:digit:]]{1,2}))?" // Second
180 CFWS
181 "("
182 "([+-][[:digit:]]{4})|" // TZ
183 "([[:alpha:]]+)" // Obsolete TZ
184 ")?"
185#undef CFWS
186#undef C
187#undef FWS
188 },
189 {
192 "( ([[:digit:]])|([[:digit:]]{2}))" // Day
193 "-" PREX_MONTH // Month
194 "-" PREX_YEAR // Year
195 " " PREX_TIME // Time
196 " ([+-][[:digit:]]{4})" // TZ
197 },
198 {
201 /* Spec: http://qmail.omnis.ch/man/man5/mbox.html */
202 "^From " // From
203 "([^[:space:]]+) +" // Sender
204 PREX_DOW // Day of week
205 " +"
206 PREX_MONTH // Month
207 " ( ([[:digit:]])|([[:digit:]]{2}))" // Day
208 " +"
209 PREX_TIME // Time
210 " +"
211 PREX_YEAR // Year
212 },
213 {
216 /* Spec: http://qmail.omnis.ch/man/man5/mbox.html */
217 "^From " // From
218 "("
219 "[^[:space:]]+" // Sender
220 "( at [^[:space:]]+)?" // Possibly obfuscated, pipermail-style
221 ")?"
222 " *"
223 PREX_DOW_NOCASE // Day of week
224 " +"
225 PREX_MONTH // Month
226 " +"
227 "( " // Day
228 "([[:digit:]])|"
229 "([[:digit:]]{2})"
230 ")"
231 " +"
232 "("
233 PREX_TIME // Time (HH:MM:SS)
234 "|"
235 "([[:digit:]]{2}" // Time (HH:MM)
236 ":[[:digit:]]{2})"
237 ")"
238 " +"
239 "("
240 "([[:alpha:] ]+)|" // Timezone name (which we skip)
241 "([+][[:digit:]]{4} )" // Timezone offset (which we skip)
242 ")?"
243 "("
244 PREX_YEAR // Year (YYYY)
245 "|"
246 "([[:digit:]]{2})" // Year (YY)
247 ")"
248 },
249 {
252 "^([[:alpha:]]+): (.*)$"
253 },
254 {
257 "^(.*)(tags:)([[:alnum:],]*) ?(.*)$"
258 },
259 // clang-format on
260 };
261
262 ASSERT((which < PREX_MAX) && "Invalid 'which' argument");
263 struct PrexStorage *h = &storage[which];
264 ASSERT((which == h->which) && "Fix 'storage' array");
265 if (!h->re)
266 {
267#ifdef HAVE_PCRE2
268 uint32_t opt = pcre2_has_unicode() ? PCRE2_UTF : 0;
269 int eno = 0;
270 PCRE2_SIZE eoff = 0;
271 h->re = pcre2_compile((PCRE2_SPTR8) h->str, PCRE2_ZERO_TERMINATED, opt,
272 &eno, &eoff, NULL);
273 ASSERT(h->re && "Fix your RE");
274 h->mdata = pcre2_match_data_create_from_pattern(h->re, NULL);
275 uint32_t ccount = 0;
276 pcre2_pattern_info(h->re, PCRE2_INFO_CAPTURECOUNT, &ccount);
277 ASSERT(((ccount + 1) == h->nmatches) && "Number of matches do not match (...)");
278 h->matches = mutt_mem_calloc(h->nmatches, sizeof(*h->matches));
279#else
280 h->re = mutt_mem_calloc(1, sizeof(*h->re));
281 const int rc = regcomp(h->re, h->str, REG_EXTENDED);
282 ASSERT(rc == 0 && "Fix your RE");
283 h->matches = mutt_mem_calloc(h->nmatches, sizeof(*h->matches));
284#endif
285 }
286 return h;
287}
288
296regmatch_t *mutt_prex_capture(enum Prex which, const char *str)
297{
298 if (!str)
299 return NULL;
300
301 struct PrexStorage *h = prex(which);
302#ifdef HAVE_PCRE2
303 size_t len = strlen(str);
304 int rc = pcre2_match(h->re, (PCRE2_SPTR8) str, len, 0, 0, h->mdata, NULL);
305 if (rc < 0)
306 {
307 PCRE2_UCHAR errmsg[1024];
308 pcre2_get_error_message(rc, errmsg, sizeof(errmsg));
309 mutt_debug(LL_DEBUG2, "pcre2_match - <%s> -> <%s> = %s\n", h->str, str, errmsg);
310 return NULL;
311 }
312 PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(h->mdata);
313 int i = 0;
314 for (; i < rc; i++)
315 {
316 h->matches[i].rm_so = ovector[i * 2];
317 h->matches[i].rm_eo = ovector[i * 2 + 1];
318 }
319 for (; i < h->nmatches; i++)
320 {
321 h->matches[i].rm_so = -1;
322 h->matches[i].rm_eo = -1;
323 }
324#else
325 if (regexec(h->re, str, h->nmatches, h->matches, 0))
326 return NULL;
327
328 ASSERT((h->re->re_nsub == (h->nmatches - 1)) &&
329 "Regular expression and matches enum are out of sync");
330#endif
331 return h->matches;
332}
333
338{
339 for (enum Prex which = 0; which < PREX_MAX; which++)
340 {
341 struct PrexStorage *h = prex(which);
342#ifdef HAVE_PCRE2
343 pcre2_match_data_free(h->mdata);
344 pcre2_code_free(h->re);
345#else
346 regfree(h->re);
347 FREE(&h->re);
348#endif
349 FREE(&h->matches);
350 }
351}
#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:51
Memory management wrappers.
#define FREE(x)
Definition: memory.h:45
#define PREX_DOW_NOCASE
Definition: prex.c:85
regmatch_t * mutt_prex_capture(enum Prex which, const char *str)
Match a precompiled regex against a string.
Definition: prex.c:296
#define CFWS
#define PREX_DOW
Definition: prex.c:84
#define PREX_TIME
Definition: prex.c:87
#define QUERY_PART
#define PREX_MONTH
Definition: prex.c:83
static struct PrexStorage * prex(enum Prex which)
Compile on demand and get data for a predefined regex.
Definition: prex.c:98
#define PATH
#define PREX_YEAR
Definition: prex.c:88
void mutt_prex_cleanup(void)
Cleanup heap memory allocated by compiled regexes.
Definition: prex.c:337
#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
Signal handling.
#define ASSERT(COND)
Definition: signal2.h:58
A predefined / precompiled regex.
Definition: prex.c:70
const char * str
Regex string.
Definition: prex.c:73
enum Prex which
Regex type, e.g. PREX_URL.
Definition: prex.c:71
size_t nmatches
Number of regex matches.
Definition: prex.c:72
regex_t * re
Compiled regex.
Definition: prex.c:78
regmatch_t * matches
Resulting matches.
Definition: prex.c:80