NeoMutt  2024-04-25-102-g19653a
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 <stddef.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 83 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 84 of file prex.c.

◆ PREX_DOW

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

Definition at line 86 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 87 of file prex.c.

◆ PREX_TIME

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

Definition at line 89 of file prex.c.

◆ PREX_YEAR

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

Definition at line 90 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 100 of file prex.c.

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

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

340{
341 for (enum Prex which = 0; which < PREX_MAX; which++)
342 {
343 struct PrexStorage *h = prex(which);
344#ifdef HAVE_PCRE2
345 pcre2_match_data_free(h->mdata);
346 pcre2_code_free(h->re);
347#else
348 regfree(h->re);
349 FREE(&h->re);
350#endif
351 FREE(&h->matches);
352 }
353}
#define FREE(x)
Definition: memory.h:45
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: