NeoMutt  2021-02-05-666-ge300cd
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;
102  STAILQ_ENTRY(RegexNode) entries;
103 };
104 STAILQ_HEAD(RegexList, RegexNode);
105 
109 struct Replace
110 {
111  struct Regex *regex;
112  size_t nmatch;
113  char *templ;
114  STAILQ_ENTRY(Replace) entries;
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 */
char * templ
Template to match.
Definition: regex3.h:113
int mutt_regexlist_remove(struct RegexList *rl, const char *str)
Remove a Regex from a list.
Definition: regex.c:227
bool mutt_replacelist_match(struct ReplaceList *rl, char *buf, size_t buflen, const char *str)
Does a string match a pattern?
Definition: regex.c:476
regex_t * regex
compiled expression
Definition: regex3.h:92
static size_t mutt_regmatch_len(const regmatch_t *match)
Return the length of a match.
Definition: regex3.h:81
String manipulation buffer.
Definition: buffer.h:33
bool pat_not
do not match
Definition: regex3.h:93
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:263
#define STAILQ_ENTRY(type)
Definition: queue.h:327
bool mutt_regex_match(const struct Regex *regex, const char *str)
Shorthand to mutt_regex_capture()
Definition: regex.c:613
List of regular expressions.
Definition: regex3.h:109
struct RegexNode * mutt_regexlist_new(void)
Create a new RegexList.
Definition: regex.c:213
struct Regex * regex
Regex containing a regular expression.
Definition: regex3.h:111
void mutt_replacelist_free(struct ReplaceList *rl)
Free a ReplaceList object.
Definition: regex.c:448
struct Regex * mutt_regex_compile(const char *str, uint16_t flags)
Create an Regex from a string.
Definition: regex.c:53
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:596
void mutt_regex_free(struct Regex **r)
Free a Regex object.
Definition: regex.c:111
struct Regex * regex
Regex containing a regular expression.
Definition: regex3.h:101
int mutt_replacelist_remove(struct ReplaceList *rl, const char *pat)
Remove a pattern from a list.
Definition: regex.c:565
char * mutt_replacelist_apply(struct ReplaceList *rl, char *buf, size_t buflen, const char *str)
Apply replacements to a buffer.
Definition: regex.c:353
static regoff_t mutt_regmatch_start(const regmatch_t *match)
Return the start of a match.
Definition: regex3.h:61
List of regular expressions.
Definition: regex3.h:99
Cached regular expression.
Definition: regex3.h:89
struct Replace * mutt_replacelist_new(void)
Create a new ReplaceList.
Definition: regex.c:554
void mutt_regexlist_free(struct RegexList *rl)
Free a RegexList object.
Definition: regex.c:171
static regoff_t mutt_regmatch_end(const regmatch_t *match)
Return the end of a match.
Definition: regex3.h:71
char * pattern
printable version
Definition: regex3.h:91
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:132
STAILQ_HEAD(RegexList, RegexNode)
struct Regex * mutt_regex_new(const char *str, uint32_t flags, struct Buffer *err)
Create an Regex from a string.
Definition: regex.c:74
bool mutt_regexlist_match(struct RegexList *rl, const char *str)
Does a string match any Regex in the list?
Definition: regex.c:192
size_t nmatch
Match the &#39;nth&#39; occurrence (0 means the whole expression)
Definition: regex3.h:112