NeoMutt  2021-10-29-225-gb9986f
Teaching an old dog new tricks
DOXYGEN
regex3.h
Go to the documentation of this file.
1 
23 #ifndef MUTT_LIB_REGEX_H
24 #define MUTT_LIB_REGEX_H
25 
26 #include "config.h"
27 #include <stddef.h>
28 #include <regex.h>
29 #include <stdbool.h>
30 #include <stdint.h>
31 #include "queue.h"
32 
33 struct Buffer;
34 
35 /* ... DT_REGEX */
36 #define DT_REGEX_MATCH_CASE (1 << 6)
37 #define DT_REGEX_ALLOW_NOT (1 << 7)
38 #define DT_REGEX_NOSUB (1 << 8)
39 
40 /* This is a non-standard option supported by Solaris 2.5.x
41  * which allows patterns of the form <...> */
42 #ifndef REG_WORDS
43 #define REG_WORDS 0
44 #endif
45 
54 #define REG_COMP(preg, regex, cflags) regcomp(preg, regex, REG_WORDS | REG_EXTENDED | (cflags))
55 
61 static inline regoff_t mutt_regmatch_start(const regmatch_t *match)
62 {
63  return match->rm_so;
64 }
65 
71 static inline regoff_t mutt_regmatch_end(const regmatch_t *match)
72 {
73  return match->rm_eo;
74 }
75 
81 static inline size_t mutt_regmatch_len(const regmatch_t *match)
82 {
83  return match->rm_eo - match->rm_so;
84 }
85 
89 struct Regex
90 {
91  char *pattern;
92  regex_t *regex;
93  bool pat_not;
94 };
95 
99 struct RegexNode
100 {
101  struct Regex *regex;
103 };
104 STAILQ_HEAD(RegexList, RegexNode);
105 
109 struct Replace
110 {
111  struct Regex *regex;
112  size_t nmatch;
113  char *templ;
115 };
116 STAILQ_HEAD(ReplaceList, Replace);
117 
118 struct Regex *mutt_regex_compile(const char *str, uint16_t flags);
119 struct Regex *mutt_regex_new(const char *str, uint32_t flags, struct Buffer *err);
120 void mutt_regex_free(struct Regex **r);
121 
122 int mutt_regexlist_add (struct RegexList *rl, const char *str, uint16_t flags, struct Buffer *err);
123 void mutt_regexlist_free (struct RegexList *rl);
124 bool mutt_regexlist_match (struct RegexList *rl, const char *str);
125 struct RegexNode *mutt_regexlist_new (void);
126 int mutt_regexlist_remove(struct RegexList *rl, const char *str);
127 
128 int mutt_replacelist_add (struct ReplaceList *rl, const char *pat, const char *templ, struct Buffer *err);
129 char * mutt_replacelist_apply (struct ReplaceList *rl, char *buf, size_t buflen, const char *str);
130 void mutt_replacelist_free (struct ReplaceList *rl);
131 bool mutt_replacelist_match (struct ReplaceList *rl, char *buf, size_t buflen, const char *str);
132 struct Replace *mutt_replacelist_new (void);
133 int mutt_replacelist_remove(struct ReplaceList *rl, const char *pat);
134 
135 bool mutt_regex_match (const struct Regex *regex, const char *str);
136 bool mutt_regex_capture(const struct Regex *regex, const char *str, size_t num, regmatch_t matches[]);
137 
138 #endif /* MUTT_LIB_REGEX_H */
int mutt_replacelist_remove(struct ReplaceList *rl, const char *pat)
Remove a pattern from a list.
Definition: regex.c:581
void mutt_regexlist_free(struct RegexList *rl)
Free a RegexList object.
Definition: regex.c:173
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.
Definition: regex.c:134
void mutt_replacelist_free(struct ReplaceList *rl)
Free a ReplaceList object.
Definition: regex.c:465
struct Regex * mutt_regex_new(const char *str, uint32_t flags, struct Buffer *err)
Create an Regex from a string.
Definition: regex.c:76
int mutt_regexlist_remove(struct RegexList *rl, const char *str)
Remove a Regex from a list.
Definition: regex.c:229
bool mutt_replacelist_match(struct ReplaceList *rl, char *buf, size_t buflen, const char *str)
Does a string match a pattern?
Definition: regex.c:493
static size_t mutt_regmatch_len(const regmatch_t *match)
Return the length of a match.
Definition: regex3.h:81
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.
Definition: regex.c:265
STAILQ_HEAD(RegexList, RegexNode)
static regoff_t mutt_regmatch_end(const regmatch_t *match)
Return the end of a match.
Definition: regex3.h:71
void mutt_regex_free(struct Regex **r)
Free a Regex object.
Definition: regex.c:113
bool mutt_regexlist_match(struct RegexList *rl, const char *str)
Does a string match any Regex in the list?
Definition: regex.c:194
struct Replace * mutt_replacelist_new(void)
Create a new ReplaceList.
Definition: regex.c:570
bool mutt_regex_capture(const struct Regex *regex, const char *str, size_t num, regmatch_t matches[])
Match a regex against a string, with provided options.
Definition: regex.c:612
struct RegexNode * mutt_regexlist_new(void)
Create a new RegexList.
Definition: regex.c:215
static regoff_t mutt_regmatch_start(const regmatch_t *match)
Return the start of a match.
Definition: regex3.h:61
bool mutt_regex_match(const struct Regex *regex, const char *str)
Shorthand to mutt_regex_capture()
Definition: regex.c:629
char * mutt_replacelist_apply(struct ReplaceList *rl, char *buf, size_t buflen, const char *str)
Apply replacements to a buffer.
Definition: regex.c:367
struct Regex * mutt_regex_compile(const char *str, uint16_t flags)
Create an Regex from a string.
Definition: regex.c:55
String manipulation buffer.
Definition: buffer.h:34
List of regular expressions.
Definition: regex3.h:100
STAILQ_ENTRY(RegexNode) entries
Linked list.
struct Regex * regex
Regex containing a regular expression.
Definition: regex3.h:101
Cached regular expression.
Definition: regex3.h:90
char * pattern
printable version
Definition: regex3.h:91
bool pat_not
do not match
Definition: regex3.h:93
regex_t * regex
compiled expression
Definition: regex3.h:92
List of regular expressions.
Definition: regex3.h:110
char * templ
Template to match.
Definition: regex3.h:113
size_t nmatch
Match the 'nth' occurrence (0 means the whole expression)
Definition: regex3.h:112
STAILQ_ENTRY(Replace) entries
Linked list.
struct Regex * regex
Regex containing a regular expression.
Definition: regex3.h:111