NeoMutt  2025-09-05-55-g97fc89
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
prex.c File Reference

Manage precompiled / predefined regular expressions. More...

#include "config.h"
#include <stdbool.h>
#include <stdint.h>
#include "prex.h"
#include "logging2.h"
#include "memory.h"
#include "signal2.h"
+ Include dependency graph for prex.c:

Go to the source code of this file.

Data Structures

struct  PrexStorage
 A predefined / precompiled regex. More...
 

Macros

#define PREX_MONTH   "(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)"
 
#define PREX_MONTH_LAX    "(Jan|January|Feb|February|Mar|March|Apr|April|May|Jun|June|Jul|July|Aug|August|Sep|September|Oct|October|Nov|November|Dec|December)"
 
#define PREX_DOW   "(Mon|Tue|Wed|Thu|Fri|Sat|Sun)"
 
#define PREX_DOW_NOCASE    "([Mm][Oo][Nn]|[Tt][Uu][Ee]|[Ww][Ee][Dd]|[Tt][Hh][Uu]|[Ff][Rr][Ii]|[Ss][Aa][Tt]|[Ss][Uu][Nn])"
 
#define PREX_TIME   "([[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2})"
 
#define PREX_YEAR   "([[:digit:]]{4})"
 
#define UNR_PCTENC_SUBDEL   "][[:alnum:]._~%!$&'()*+,;="
 
#define PATH   ":@/ "
 
#define QUERY_PART   "^&="
 
#define FWS   " *"
 
#define C   "(\\‍(.*\\‍))?"
 
#define CFWS   FWS C FWS
 

Functions

static struct PrexStorageprex (enum Prex which)
 Compile on demand and get data for a predefined regex.
 
regmatch_t * mutt_prex_capture (enum Prex which, const char *str)
 Match a precompiled regex against a string.
 
void mutt_prex_cleanup (void)
 Cleanup heap memory allocated by compiled regexes.
 

Detailed Description

Manage precompiled / predefined regular expressions.

Authors
  • Pietro Cerutti
  • Richard Russon

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file prex.c.

Macro Definition Documentation

◆ PREX_MONTH

#define PREX_MONTH   "(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)"

Definition at line 82 of file prex.c.

◆ PREX_MONTH_LAX

#define PREX_MONTH_LAX    "(Jan|January|Feb|February|Mar|March|Apr|April|May|Jun|June|Jul|July|Aug|August|Sep|September|Oct|October|Nov|November|Dec|December)"

Definition at line 83 of file prex.c.

83#define PREX_MONTH_LAX \
84 "(Jan|January|Feb|February|Mar|March|Apr|April|May|Jun|June|Jul|July|Aug|August|Sep|September|Oct|October|Nov|November|Dec|December)"

◆ PREX_DOW

#define PREX_DOW   "(Mon|Tue|Wed|Thu|Fri|Sat|Sun)"

Definition at line 85 of file prex.c.

◆ PREX_DOW_NOCASE

#define PREX_DOW_NOCASE    "([Mm][Oo][Nn]|[Tt][Uu][Ee]|[Ww][Ee][Dd]|[Tt][Hh][Uu]|[Ff][Rr][Ii]|[Ss][Aa][Tt]|[Ss][Uu][Nn])"

Definition at line 86 of file prex.c.

86#define PREX_DOW_NOCASE \
87 "([Mm][Oo][Nn]|[Tt][Uu][Ee]|[Ww][Ee][Dd]|[Tt][Hh][Uu]|[Ff][Rr][Ii]|[Ss][Aa][Tt]|[Ss][Uu][Nn])"

◆ PREX_TIME

#define PREX_TIME   "([[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2})"

Definition at line 88 of file prex.c.

◆ PREX_YEAR

#define PREX_YEAR   "([[:digit:]]{4})"

Definition at line 89 of file prex.c.

◆ UNR_PCTENC_SUBDEL

#define UNR_PCTENC_SUBDEL   "][[:alnum:]._~%!$&'()*+,;="

◆ PATH

#define PATH   ":@/ "

◆ QUERY_PART

#define QUERY_PART   "^&="

◆ FWS

#define FWS   " *"

◆ C

#define C   "(\\‍(.*\\‍))?"

◆ CFWS

#define CFWS   FWS C FWS

Function Documentation

◆ prex()

static struct PrexStorage * prex ( enum Prex which)
static

Compile on demand and get data for a predefined regex.

Parameters
whichWhich regex to get
Return values
ptrPointer to a PrexStorage struct
Note
Returned pointer is guaranteed not to be NULL. The function asserts on error.

Definition at line 99 of file prex.c.

100{
101 static struct PrexStorage storage[] = {
102 // clang-format off
103 {
104 PREX_URL,
106 /* Spec: https://tools.ietf.org/html/rfc3986#section-3 */
107#ifdef HAVE_PCRE2
108#define UNR_PCTENC_SUBDEL "][\\p{L}\\p{N}._~%!$&'()*+,;="
109#else
110#define UNR_PCTENC_SUBDEL "][[:alnum:]._~%!$&'()*+,;="
111#endif
112#define PATH ":@/ "
113 "^([[:alpha:]][-+.[:alnum:]]+):" // . scheme
114 "(" // . rest
115 "(" // . . authority + path
116 // . . or path only
117 "(//" // . . . authority + path
118 "(" // . . . . user info
119 "([" UNR_PCTENC_SUBDEL "@-]*)" // . . . . . user name + '@'
120 "(:([" UNR_PCTENC_SUBDEL "-]*))?" // . . . . . password
121 "@)?"
122 "(" // . . . . host
123 "([" UNR_PCTENC_SUBDEL "-]*)" // . . . . . host name
124 "|"
125 "(\\[[[:xdigit:]:.]+\\])" // . . . . . IPv4 or IPv6
126 ")"
127 "(:([[:digit:]]+))?" // . . . . port
128 "(/([" UNR_PCTENC_SUBDEL PATH "-]*))?" // . . . . path
129 ")"
130 "|"
131 "(" // . . . path only
132 "[" UNR_PCTENC_SUBDEL PATH "-]*" // . . . . path
133 ")"
134 ")"
135 // Should be: "(\\?([" UNR_PCTENC_SUBDEL PATH "?-]*))?"
136 "(\\?([^#]*))?" // . . query
137 ")$"
138#undef PATH
139#undef UNR_PCTENC_SUBDEL
140 },
141 {
144#define QUERY_PART "^&=" // Should be: "-[:alnum:]._~%!$'()*+,;:@/"
145 "([" QUERY_PART "]+)=([" QUERY_PART "]+)" // query + ' '
146#undef QUERY_PART
147 },
148 {
151 "=\\?"
152 "([^][()<>@,;:\\\"/?. =]+)" // charset
153 "\\?"
154 "([qQbB])" // encoding
155 "\\?"
156 "([^?]+)" // encoded text - we accept whitespace, see #1189
157 "\\?="
158 },
159 {
162 "^\\#H ([[:alnum:]_\\.-]+) ([[:alnum:]]{4}( [[:alnum:]]{4}){7})[ \t]*$"
163 },
164 {
167 /* Spec: https://tools.ietf.org/html/rfc5322#section-3.3 */
168#define FWS " *"
169#define C "(\\(.*\\))?"
170#define CFWS FWS C FWS
171 "^"
172 CFWS
173 "(([[:alpha:]]+)" CFWS ", *)?" // Day of week (or whatever)
174 CFWS "([[:digit:]]{1,2}) " // Day
175 CFWS PREX_MONTH_LAX // Month
176 CFWS "([[:digit:]]{2,4}) " // Year
177 CFWS "([[:digit:]]{1,2})" // Hour
178 ":" CFWS "([[:digit:]]{1,2})" // Minute
179 CFWS
180 "(:" CFWS "([[:digit:]]{1,2}))?" // Second
181 CFWS
182 "("
183 "([+-][[:digit:]]{4})|" // TZ
184 "([[:alpha:]]+)" // Obsolete TZ
185 ")?"
186#undef CFWS
187#undef C
188#undef FWS
189 },
190 {
193 "( ([[:digit:]])|([[:digit:]]{2}))" // Day
194 "-" PREX_MONTH // Month
195 "-" PREX_YEAR // Year
196 " " PREX_TIME // Time
197 " ([+-][[:digit:]]{4})" // TZ
198 },
199 {
202 /* Spec: http://qmail.omnis.ch/man/man5/mbox.html */
203 "^From " // From
204 "([^[:space:]]+) +" // Sender
205 PREX_DOW // Day of week
206 " +"
207 PREX_MONTH // Month
208 " ( ([[:digit:]])|([[:digit:]]{2}))" // Day
209 " +"
210 PREX_TIME // Time
211 " +"
212 PREX_YEAR // Year
213 },
214 {
217 /* Spec: http://qmail.omnis.ch/man/man5/mbox.html */
218 "^From " // From
219 "("
220 "[^[:space:]]+" // Sender
221 "( at [^[:space:]]+)?" // Possibly obfuscated, pipermail-style
222 ")?"
223 " *"
224 PREX_DOW_NOCASE // Day of week
225 " +"
226 PREX_MONTH // Month
227 " +"
228 "( " // Day
229 "([[:digit:]])|"
230 "([[:digit:]]{2})"
231 ")"
232 " +"
233 "("
234 PREX_TIME // Time (HH:MM:SS)
235 "|"
236 "([[:digit:]]{2}" // Time (HH:MM)
237 ":[[:digit:]]{2})"
238 ")"
239 " +"
240 "("
241 "([[:alpha:] ]+)|" // Timezone name (which we skip)
242 "([+][[:digit:]]{4} )" // Timezone offset (which we skip)
243 ")?"
244 "("
245 PREX_YEAR // Year (YYYY)
246 "|"
247 "([[:digit:]]{2})" // Year (YY)
248 ")"
249 },
250 {
253 "^([[:alpha:]]+): (.*)$"
254 },
255 {
258 "^(.*)(tags:)([[:alnum:],]*) ?(.*)$"
259 },
260 // clang-format on
261 };
262
263 ASSERT((which < PREX_MAX) && "Invalid 'which' argument");
264 struct PrexStorage *h = &storage[which];
265 ASSERT((which == h->which) && "Fix 'storage' array");
266 if (!h->re)
267 {
268#ifdef HAVE_PCRE2
269 uint32_t opt = pcre2_has_unicode() ? PCRE2_UTF : 0;
270 int eno = 0;
271 PCRE2_SIZE eoff = 0;
272 h->re = pcre2_compile((PCRE2_SPTR8) h->str, PCRE2_ZERO_TERMINATED, opt,
273 &eno, &eoff, NULL);
274 ASSERT(h->re && "Fix your RE");
275 h->mdata = pcre2_match_data_create_from_pattern(h->re, NULL);
276 uint32_t ccount = 0;
277 pcre2_pattern_info(h->re, PCRE2_INFO_CAPTURECOUNT, &ccount);
278 ASSERT(((ccount + 1) == h->nmatches) && "Number of matches do not match (...)");
279 h->matches = MUTT_MEM_CALLOC(h->nmatches, regmatch_t);
280#else
281 h->re = MUTT_MEM_CALLOC(1, regex_t);
282 const int rc = regcomp(h->re, h->str, REG_EXTENDED);
283 ASSERT(rc == 0 && "Fix your RE");
284 h->matches = MUTT_MEM_CALLOC(h->nmatches, regmatch_t);
285#endif
286 }
287 return h;
288}
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:47
#define PREX_DOW_NOCASE
Definition prex.c:86
#define CFWS
#define PREX_MONTH_LAX
Definition prex.c:83
#define PREX_DOW
Definition prex.c:85
#define PREX_TIME
Definition prex.c:88
#define QUERY_PART
#define PREX_MONTH
Definition prex.c:82
#define PATH
#define PREX_YEAR
Definition prex.c:89
#define UNR_PCTENC_SUBDEL
@ 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_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
#define ASSERT(COND)
Definition signal2.h:60
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
+ Here is the caller graph for this function:

◆ mutt_prex_capture()

regmatch_t * mutt_prex_capture ( enum Prex which,
const char * str )

Match a precompiled regex against a string.

Parameters
whichWhich regex to return
strString to apply regex on
Return values
ptrPointer to an array of matched captures
NULLRegex didn't match

Definition at line 297 of file prex.c.

298{
299 if (!str)
300 return NULL;
301
302 struct PrexStorage *h = prex(which);
303#ifdef HAVE_PCRE2
304 size_t len = strlen(str);
305 int rc = pcre2_match(h->re, (PCRE2_SPTR8) str, len, 0, 0, h->mdata, NULL);
306 if (rc < 0)
307 {
308 PCRE2_UCHAR errmsg[1024];
309 pcre2_get_error_message(rc, errmsg, sizeof(errmsg));
310 mutt_debug(LL_DEBUG2, "pcre2_match - <%s> -> <%s> = %s\n", h->str, str, errmsg);
311 return NULL;
312 }
313 PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(h->mdata);
314 int i = 0;
315 for (; i < rc; i++)
316 {
317 h->matches[i].rm_so = ovector[i * 2];
318 h->matches[i].rm_eo = ovector[i * 2 + 1];
319 }
320 for (; i < h->nmatches; i++)
321 {
322 h->matches[i].rm_so = -1;
323 h->matches[i].rm_eo = -1;
324 }
325#else
326 if (regexec(h->re, str, h->nmatches, h->matches, 0))
327 return NULL;
328
329 ASSERT((h->re->re_nsub == (h->nmatches - 1)) &&
330 "Regular expression and matches enum are out of sync");
331#endif
332 return h->matches;
333}
#define mutt_debug(LEVEL,...)
Definition logging2.h:90
@ LL_DEBUG2
Log at debug level 2.
Definition logging2.h:45
static struct PrexStorage * prex(enum Prex which)
Compile on demand and get data for a predefined regex.
Definition prex.c:99
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_prex_cleanup()

void mutt_prex_cleanup ( void )

Cleanup heap memory allocated by compiled regexes.

Definition at line 338 of file prex.c.

339{
340 for (enum Prex which = 0; which < PREX_MAX; which++)
341 {
342 struct PrexStorage *h = prex(which);
343#ifdef HAVE_PCRE2
344 pcre2_match_data_free(h->mdata);
345 pcre2_code_free(h->re);
346#else
347 regfree(h->re);
348 FREE(&h->re);
349#endif
350 FREE(&h->matches);
351 }
352}
#define FREE(x)
Definition memory.h:62
Prex
Predefined list of regular expressions.
Definition prex.h:33
+ Here is the call graph for this function:
+ Here is the caller graph for this function: