NeoMutt  2024-02-01-35-geee02f
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
regex.c File Reference

Manage regular expressions. More...

#include "config.h"
#include <ctype.h>
#include <regex.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "config/types.h"
#include "atoi.h"
#include "buffer.h"
#include "logging2.h"
#include "mbyte.h"
#include "memory.h"
#include "message.h"
#include "queue.h"
#include "regex3.h"
#include "string2.h"
+ Include dependency graph for regex.c:

Go to the source code of this file.

Functions

struct Regexmutt_regex_compile (const char *str, uint16_t flags)
 Create an Regex from a string.
 
struct Regexmutt_regex_new (const char *str, uint32_t flags, struct Buffer *err)
 Create an Regex from a string.
 
void mutt_regex_free (struct Regex **ptr)
 Free a Regex object.
 
int mutt_regexlist_add (struct RegexList *rl, const char *str, uint16_t flags, struct Buffer *err)
 Compile a regex string and add it to a list.
 
void mutt_regexlist_free (struct RegexList *rl)
 Free a RegexList object.
 
bool mutt_regexlist_match (struct RegexList *rl, const char *str)
 Does a string match any Regex in the list?
 
struct RegexNodemutt_regexlist_new (void)
 Create a new RegexList.
 
int mutt_regexlist_remove (struct RegexList *rl, const char *str)
 Remove a Regex from a list.
 
int mutt_replacelist_add (struct ReplaceList *rl, const char *pat, const char *templ, struct Buffer *err)
 Add a pattern and a template to a list.
 
char * mutt_replacelist_apply (struct ReplaceList *rl, char *buf, size_t buflen, const char *str)
 Apply replacements to a buffer.
 
void mutt_replacelist_free (struct ReplaceList *rl)
 Free a ReplaceList object.
 
bool mutt_replacelist_match (struct ReplaceList *rl, char *buf, size_t buflen, const char *str)
 Does a string match a pattern?
 
struct Replacemutt_replacelist_new (void)
 Create a new ReplaceList.
 
int mutt_replacelist_remove (struct ReplaceList *rl, const char *pat)
 Remove a pattern from a list.
 
bool mutt_regex_capture (const struct Regex *regex, const char *str, size_t nmatch, regmatch_t matches[])
 Match a regex against a string, with provided options.
 
bool mutt_regex_match (const struct Regex *regex, const char *str)
 Shorthand to mutt_regex_capture()
 

Detailed Description

Manage regular expressions.

Authors
  • Richard Russon
  • Bo Yu
  • Pietro Cerutti
  • Simon Symeonidis
  • наб

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 regex.c.

Function Documentation

◆ mutt_regex_compile()

struct Regex * mutt_regex_compile ( const char *  str,
uint16_t  flags 
)

Create an Regex from a string.

Parameters
strRegular expression
flagsType flags, e.g. REG_ICASE
Return values
ptrNew Regex object
NULLError

Definition at line 59 of file regex.c.

60{
61 if (!str || (*str == '\0'))
62 return NULL;
63 struct Regex *rx = mutt_mem_calloc(1, sizeof(struct Regex));
64 rx->pattern = mutt_str_dup(str);
65 rx->regex = mutt_mem_calloc(1, sizeof(regex_t));
66 if (REG_COMP(rx->regex, str, flags) != 0)
67 mutt_regex_free(&rx);
68
69 return rx;
70}
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
void mutt_regex_free(struct Regex **ptr)
Free a Regex object.
Definition: regex.c:118
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
#define REG_COMP(preg, regex, cflags)
Compile a regular expression.
Definition: regex3.h:49
Cached regular expression.
Definition: regex3.h:85
char * pattern
printable version
Definition: regex3.h:86
regex_t * regex
compiled expression
Definition: regex3.h:87
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_regex_new()

struct Regex * mutt_regex_new ( const char *  str,
uint32_t  flags,
struct Buffer err 
)

Create an Regex from a string.

Parameters
strRegular expression
flagsType flags, e.g. D_REGEX_MATCH_CASE
errBuffer for error messages
Return values
ptrNew Regex object
NULLError

Definition at line 80 of file regex.c.

81{
82 if (!str || (*str == '\0'))
83 return NULL;
84
85 uint16_t rflags = 0;
86 struct Regex *reg = mutt_mem_calloc(1, sizeof(struct Regex));
87
88 reg->regex = mutt_mem_calloc(1, sizeof(regex_t));
89 reg->pattern = mutt_str_dup(str);
90
91 /* Should we use smart case matching? */
92 if (((flags & D_REGEX_MATCH_CASE) == 0) && mutt_mb_is_lower(str))
93 rflags |= REG_ICASE;
94
95 /* Is a prefix of '!' allowed? */
96 if (((flags & D_REGEX_ALLOW_NOT) != 0) && (str[0] == '!'))
97 {
98 reg->pat_not = true;
99 str++;
100 }
101
102 int rc = REG_COMP(reg->regex, str, rflags);
103 if (rc != 0)
104 {
105 if (err)
106 regerror(rc, reg->regex, err->data, err->dsize);
107 mutt_regex_free(&reg);
108 return NULL;
109 }
110
111 return reg;
112}
bool mutt_mb_is_lower(const char *s)
Does a multi-byte string contain only lowercase characters?
Definition: mbyte.c:354
size_t dsize
Length of data.
Definition: buffer.h:39
char * data
Pointer to data.
Definition: buffer.h:37
bool pat_not
do not match
Definition: regex3.h:88
#define D_REGEX_ALLOW_NOT
Regex can begin with '!'.
Definition: types.h:104
#define D_REGEX_MATCH_CASE
Case-sensitive matching.
Definition: types.h:103
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_regex_free()

void mutt_regex_free ( struct Regex **  ptr)

Free a Regex object.

Parameters
[out]ptrRegex to free

Definition at line 118 of file regex.c.

119{
120 if (!ptr || !*ptr)
121 return;
122
123 struct Regex *rx = *ptr;
124 FREE(&rx->pattern);
125 if (rx->regex)
126 regfree(rx->regex);
127 FREE(&rx->regex);
128 FREE(ptr);
129}
#define FREE(x)
Definition: memory.h:45
+ Here is the caller graph for this function:

◆ mutt_regexlist_add()

int mutt_regexlist_add ( struct RegexList *  rl,
const char *  str,
uint16_t  flags,
struct Buffer err 
)

Compile a regex string and add it to a list.

Parameters
rlRegexList to add to
strString to compile into a regex
flagsFlags, e.g. REG_ICASE
errBuffer for error messages
Return values
0Success, Regex compiled and added to the list
-1Error, see message in 'err'

Definition at line 140 of file regex.c.

142{
143 if (!rl || !str || (*str == '\0'))
144 return 0;
145
146 struct Regex *rx = mutt_regex_compile(str, flags);
147 if (!rx)
148 {
149 buf_printf(err, "Bad regex: %s\n", str);
150 return -1;
151 }
152
153 /* check to make sure the item is not already on this rl */
154 struct RegexNode *np = NULL;
155 STAILQ_FOREACH(np, rl, entries)
156 {
157 if (mutt_istr_equal(rx->pattern, np->regex->pattern))
158 break; /* already on the rl */
159 }
160
161 if (np)
162 {
163 mutt_regex_free(&rx);
164 }
165 else
166 {
167 np = mutt_regexlist_new();
168 np->regex = rx;
169 STAILQ_INSERT_TAIL(rl, np, entries);
170 }
171
172 return 0;
173}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:178
struct RegexNode * mutt_regexlist_new(void)
Create a new RegexList.
Definition: regex.c:221
struct Regex * mutt_regex_compile(const char *str, uint16_t flags)
Create an Regex from a string.
Definition: regex.c:59
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:721
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:352
#define STAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:389
List of regular expressions.
Definition: regex3.h:95
struct Regex * regex
Regex containing a regular expression.
Definition: regex3.h:96
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_regexlist_free()

void mutt_regexlist_free ( struct RegexList *  rl)

Free a RegexList object.

Parameters
rlRegexList to free

Definition at line 179 of file regex.c.

180{
181 if (!rl)
182 return;
183
184 struct RegexNode *np = NULL, *tmp = NULL;
185 STAILQ_FOREACH_SAFE(np, rl, entries, tmp)
186 {
187 STAILQ_REMOVE(rl, np, RegexNode, entries);
189 FREE(&np);
190 }
191 STAILQ_INIT(rl);
192}
#define STAILQ_REMOVE(head, elm, type, field)
Definition: queue.h:402
#define STAILQ_INIT(head)
Definition: queue.h:372
#define STAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition: queue.h:362
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_regexlist_match()

bool mutt_regexlist_match ( struct RegexList *  rl,
const char *  str 
)

Does a string match any Regex in the list?

Parameters
rlRegexList to match against
strString to compare
Return values
trueString matches one of the Regexes in the list

Definition at line 200 of file regex.c.

201{
202 if (!rl || !str)
203 return false;
204 struct RegexNode *np = NULL;
205 STAILQ_FOREACH(np, rl, entries)
206 {
207 if (mutt_regex_match(np->regex, str))
208 {
209 mutt_debug(LL_DEBUG5, "%s matches %s\n", str, np->regex->pattern);
210 return true;
211 }
212 }
213
214 return false;
215}
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
@ LL_DEBUG5
Log at debug level 5.
Definition: logging2.h:47
bool mutt_regex_match(const struct Regex *regex, const char *str)
Shorthand to mutt_regex_capture()
Definition: regex.c:639
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_regexlist_new()

struct RegexNode * mutt_regexlist_new ( void  )

Create a new RegexList.

Return values
ptrNew RegexList object

Definition at line 221 of file regex.c.

222{
223 return mutt_mem_calloc(1, sizeof(struct RegexNode));
224}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_regexlist_remove()

int mutt_regexlist_remove ( struct RegexList *  rl,
const char *  str 
)

Remove a Regex from a list.

Parameters
rlRegexList to alter
strPattern to remove from the list
Return values
0Success, pattern was found and removed from the list
-1Error, pattern wasn't found

If the pattern is "*", then all the Regexes are removed.

Definition at line 235 of file regex.c.

236{
237 if (!rl || !str)
238 return -1;
239
240 if (mutt_str_equal("*", str))
241 {
242 mutt_regexlist_free(rl); /* "unCMD *" means delete all current entries */
243 return 0;
244 }
245
246 int rc = -1;
247 struct RegexNode *np = NULL, *tmp = NULL;
248 STAILQ_FOREACH_SAFE(np, rl, entries, tmp)
249 {
250 if (mutt_istr_equal(str, np->regex->pattern))
251 {
252 STAILQ_REMOVE(rl, np, RegexNode, entries);
254 FREE(&np);
255 rc = 0;
256 }
257 }
258
259 return rc;
260}
void mutt_regexlist_free(struct RegexList *rl)
Free a RegexList object.
Definition: regex.c:179
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:709
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_replacelist_add()

int mutt_replacelist_add ( struct ReplaceList *  rl,
const char *  pat,
const char *  templ,
struct Buffer err 
)

Add a pattern and a template to a list.

Parameters
rlReplaceList to add to
patPattern to compile into a regex
templTemplate string to associate with the pattern
errBuffer for error messages
Return values
0Success, pattern added to the ReplaceList
-1Error, see message in 'err'

Definition at line 271 of file regex.c.

273{
274 if (!rl || !pat || (*pat == '\0') || !templ)
275 return 0;
276
277 struct Regex *rx = mutt_regex_compile(pat, REG_ICASE);
278 if (!rx)
279 {
280 buf_printf(err, _("Bad regex: %s"), pat);
281 return -1;
282 }
283
284 /* check to make sure the item is not already on this rl */
285 struct Replace *np = NULL;
286 STAILQ_FOREACH(np, rl, entries)
287 {
288 if (mutt_istr_equal(rx->pattern, np->regex->pattern))
289 {
290 /* Already on the rl. Formerly we just skipped this case, but
291 * now we're supporting removals, which means we're supporting
292 * re-adds conceptually. So we probably want this to imply a
293 * removal, then do an add. We can achieve the removal by freeing
294 * the template, and leaving t pointed at the current item. */
295 FREE(&np->templ);
296 break;
297 }
298 }
299
300 /* If np is set, it's pointing into an extant ReplaceList* that we want to
301 * update. Otherwise we want to make a new one to link at the rl's end. */
302 if (np)
303 {
304 mutt_regex_free(&rx);
305 }
306 else
307 {
309 np->regex = rx;
310 rx = NULL;
311 STAILQ_INSERT_TAIL(rl, np, entries);
312 }
313
314 /* Now np is the Replace that we want to modify. It is prepared. */
315 np->templ = mutt_str_dup(templ);
316
317 /* Find highest match number in template string */
318 np->nmatch = 0;
319 for (const char *p = templ; *p;)
320 {
321 if (*p == '%')
322 {
323 int n = 0;
324 const char *end = mutt_str_atoi(++p, &n);
325 if (!end)
326 {
327 // this is not an error, we might have matched %R or %L in subjectrx
328 mutt_debug(LL_DEBUG2, "Invalid match number in replacelist: '%s'\n", p);
329 }
330 if (n > np->nmatch)
331 {
332 np->nmatch = n;
333 }
334 if (end)
335 {
336 p = end;
337 }
338 else
339 {
340 p++;
341 }
342 }
343 else
344 {
345 p++;
346 }
347 }
348
349 if (np->nmatch > np->regex->regex->re_nsub)
350 {
351 if (err)
352 buf_addstr(err, _("Not enough subexpressions for template"));
354 return -1;
355 }
356
357 np->nmatch++; /* match 0 is always the whole expr */
358 return 0;
359}
const char * mutt_str_atoi(const char *str, int *dst)
Convert ASCII string to an integer.
Definition: atoi.c:188
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:243
@ LL_DEBUG2
Log at debug level 2.
Definition: logging2.h:44
#define _(a)
Definition: message.h:28
int mutt_replacelist_remove(struct ReplaceList *rl, const char *pat)
Remove a pattern from a list.
Definition: regex.c:591
struct Replace * mutt_replacelist_new(void)
Create a new ReplaceList.
Definition: regex.c:580
List of regular expressions.
Definition: regex3.h:105
char * templ
Template to match.
Definition: regex3.h:108
size_t nmatch
Match the 'nth' occurrence (0 means the whole expression)
Definition: regex3.h:107
struct Regex * regex
Regex containing a regular expression.
Definition: regex3.h:106
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_replacelist_apply()

char * mutt_replacelist_apply ( struct ReplaceList *  rl,
char *  buf,
size_t  buflen,
const char *  str 
)

Apply replacements to a buffer.

Parameters
rlReplaceList to apply
bufBuffer for the result
buflenLength of the buffer
strString to manipulate
Return values
ptrPointer to 'buf'

If 'buf' is NULL, a new string will be returned. It must be freed by the caller.

Note
This function uses a fixed size buffer of 1024 and so should only be used for visual modifications, such as disp_subj.

Definition at line 374 of file regex.c.

375{
376 static regmatch_t *pmatch = NULL;
377 static size_t nmatch = 0;
378 static char twinbuf[2][1024];
379 int switcher = 0;
380 char *p = NULL;
381 size_t cpysize, tlen;
382 char *src = NULL, *dst = NULL;
383
384 if (buf && (buflen != 0))
385 buf[0] = '\0';
386
387 if (!rl || !str || (*str == '\0') || (buf && (buflen == 0)))
388 return buf;
389
390 twinbuf[0][0] = '\0';
391 twinbuf[1][0] = '\0';
392 src = twinbuf[switcher];
393 dst = src;
394
395 mutt_str_copy(src, str, sizeof(*twinbuf));
396
397 struct Replace *np = NULL;
398 STAILQ_FOREACH(np, rl, entries)
399 {
400 /* If this pattern needs more matches, expand pmatch. */
401 if (np->nmatch > nmatch)
402 {
403 mutt_mem_realloc(&pmatch, np->nmatch * sizeof(regmatch_t));
404 nmatch = np->nmatch;
405 }
406
407 if (mutt_regex_capture(np->regex, src, np->nmatch, pmatch))
408 {
409 tlen = 0;
410 switcher ^= 1;
411 dst = twinbuf[switcher];
412
413 mutt_debug(LL_DEBUG5, "%s matches %s\n", src, np->regex->pattern);
414
415 /* Copy into other twinbuf with substitutions */
416 if (np->templ)
417 {
418 for (p = np->templ; *p && (tlen < (sizeof(*twinbuf) - 1));)
419 {
420 if (*p == '%')
421 {
422 p++;
423 if (*p == 'L')
424 {
425 p++;
426 cpysize = MIN(pmatch[0].rm_so, (sizeof(*twinbuf) - 1) - tlen);
427 strncpy(&dst[tlen], src, cpysize);
428 tlen += cpysize;
429 }
430 else if (*p == 'R')
431 {
432 p++;
433 cpysize = MIN(strlen(src) - pmatch[0].rm_eo, (sizeof(*twinbuf) - 1) - tlen);
434 strncpy(&dst[tlen], &src[pmatch[0].rm_eo], cpysize);
435 tlen += cpysize;
436 }
437 else
438 {
439 long n = strtoul(p, &p, 10); /* get subst number */
440 if (n < np->nmatch)
441 {
442 while (isdigit((unsigned char) *p)) /* skip subst token */
443 p++;
444 for (int i = pmatch[n].rm_so;
445 (i < pmatch[n].rm_eo) && (tlen < (sizeof(*twinbuf) - 1)); i++)
446 {
447 dst[tlen++] = src[i];
448 }
449 }
450 }
451 }
452 else
453 {
454 dst[tlen++] = *p++;
455 }
456 }
457 }
458 dst[tlen] = '\0';
459 mutt_debug(LL_DEBUG5, "subst %s\n", dst);
460 }
461 src = dst;
462 }
463
464 if (buf)
465 mutt_str_copy(buf, dst, buflen);
466 else
467 buf = mutt_str_dup(dst);
468 return buf;
469}
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
#define MIN(a, b)
Definition: memory.h:32
bool mutt_regex_capture(const struct Regex *regex, const char *str, size_t nmatch, regmatch_t matches[])
Match a regex against a string, with provided options.
Definition: regex.c:622
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition: string.c:630
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_replacelist_free()

void mutt_replacelist_free ( struct ReplaceList *  rl)

Free a ReplaceList object.

Parameters
rlReplaceList to free

Definition at line 475 of file regex.c.

476{
477 if (!rl)
478 return;
479
480 struct Replace *np = NULL, *tmp = NULL;
481 STAILQ_FOREACH_SAFE(np, rl, entries, tmp)
482 {
483 STAILQ_REMOVE(rl, np, Replace, entries);
485 FREE(&np->templ);
486 FREE(&np);
487 }
488}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_replacelist_match()

bool mutt_replacelist_match ( struct ReplaceList *  rl,
char *  buf,
size_t  buflen,
const char *  str 
)

Does a string match a pattern?

Parameters
rlReplaceList of patterns
bufBuffer to save match
buflenBuffer length
strString to check
Return values
trueString matches a patterh in the ReplaceList

Match a string against the patterns defined by the 'spam' command and output the expanded format into buf when there is a match. If buflen<=0, the match is performed but the format is not expanded and no assumptions are made about the value of buf so it may be NULL.

Definition at line 503 of file regex.c.

504{
505 if (!rl || !buf || !str)
506 return false;
507
508 static regmatch_t *pmatch = NULL;
509 static size_t nmatch = 0;
510 int tlen = 0;
511 char *p = NULL;
512
513 struct Replace *np = NULL;
514 STAILQ_FOREACH(np, rl, entries)
515 {
516 /* If this pattern needs more matches, expand pmatch. */
517 if (np->nmatch > nmatch)
518 {
519 mutt_mem_realloc(&pmatch, np->nmatch * sizeof(regmatch_t));
520 nmatch = np->nmatch;
521 }
522
523 /* Does this pattern match? */
524 if (mutt_regex_capture(np->regex, str, (size_t) np->nmatch, pmatch))
525 {
526 mutt_debug(LL_DEBUG5, "%s matches %s\n", str, np->regex->pattern);
527 mutt_debug(LL_DEBUG5, "%d subs\n", (int) np->regex->regex->re_nsub);
528
529 /* Copy template into buf, with substitutions. */
530 for (p = np->templ; *p && (tlen < (buflen - 1));)
531 {
532 /* backreference to pattern match substring, eg. %1, %2, etc) */
533 if (*p == '%')
534 {
535 char *e = NULL; /* used as pointer to end of integer backreference in strtol() call */
536
537 p++; /* skip over % char */
538 long n = strtol(p, &e, 10);
539 /* Ensure that the integer conversion succeeded (e!=p) and bounds check. The upper bound check
540 * should not strictly be necessary since add_to_spam_list() finds the largest value, and
541 * the static array above is always large enough based on that value. */
542 if ((e != p) && (n >= 0) && (n < np->nmatch) && (pmatch[n].rm_so != -1))
543 {
544 /* copy as much of the substring match as will fit in the output buffer, saving space for
545 * the terminating nul char */
546 for (int idx = pmatch[n].rm_so;
547 (idx < pmatch[n].rm_eo) && (tlen < (buflen - 1)); idx++)
548 {
549 buf[tlen++] = str[idx];
550 }
551 }
552 p = e; /* skip over the parsed integer */
553 }
554 else
555 {
556 buf[tlen++] = *p++;
557 }
558 }
559 /* tlen should always be less than buflen except when buflen<=0
560 * because the bounds checks in the above code leave room for the
561 * terminal nul char. This should avoid returning an unterminated
562 * string to the caller. When buflen<=0 we make no assumption about
563 * the validity of the buf pointer. */
564 if (tlen < buflen)
565 {
566 buf[tlen] = '\0';
567 mutt_debug(LL_DEBUG5, "\"%s\"\n", buf);
568 }
569 return true;
570 }
571 }
572
573 return false;
574}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_replacelist_new()

struct Replace * mutt_replacelist_new ( void  )

Create a new ReplaceList.

Return values
ptrNew ReplaceList

Definition at line 580 of file regex.c.

581{
582 return mutt_mem_calloc(1, sizeof(struct Replace));
583}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_replacelist_remove()

int mutt_replacelist_remove ( struct ReplaceList *  rl,
const char *  pat 
)

Remove a pattern from a list.

Parameters
rlReplaceList to modify
patPattern to remove
Return values
numMatching patterns removed

Definition at line 591 of file regex.c.

592{
593 if (!rl || !pat)
594 return 0;
595
596 int nremoved = 0;
597 struct Replace *np = NULL, *tmp = NULL;
598 STAILQ_FOREACH_SAFE(np, rl, entries, tmp)
599 {
600 if (mutt_str_equal(np->regex->pattern, pat))
601 {
602 STAILQ_REMOVE(rl, np, Replace, entries);
604 FREE(&np->templ);
605 FREE(&np);
606 nremoved++;
607 }
608 }
609
610 return nremoved;
611}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_regex_capture()

bool mutt_regex_capture ( const struct Regex regex,
const char *  str,
size_t  nmatch,
regmatch_t  matches[] 
)

Match a regex against a string, with provided options.

Parameters
regexRegex to execute
strString to apply regex on
nmatchLength of matches
matchesregmatch_t to hold match indices
Return values
truestr matches
falsestr does not match

Definition at line 622 of file regex.c.

624{
625 if (!regex || !str || !regex->regex)
626 return false;
627
628 int rc = regexec(regex->regex, str, nmatch, matches, 0);
629 return ((rc == 0) ^ regex->pat_not);
630}
+ Here is the caller graph for this function:

◆ mutt_regex_match()

bool mutt_regex_match ( const struct Regex regex,
const char *  str 
)

Shorthand to mutt_regex_capture()

Parameters
regexRegex which is desired to match against
strString to search with given regex
Return values
truestr matches
falsestr does not match

Definition at line 639 of file regex.c.

640{
641 return mutt_regex_capture(regex, str, 0, NULL);
642}
+ Here is the call graph for this function:
+ Here is the caller graph for this function: