NeoMutt  2020-11-20
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 "queue.h"
31 
32 struct Buffer;
33 
34 /* ... DT_REGEX */
35 #define DT_REGEX_MATCH_CASE (1 << 6)
36 #define DT_REGEX_ALLOW_NOT (1 << 7)
37 #define DT_REGEX_NOSUB (1 << 8)
38 
39 /* This is a non-standard option supported by Solaris 2.5.x
40  * which allows patterns of the form <...> */
41 #ifndef REG_WORDS
42 #define REG_WORDS 0
43 #endif
44 
53 #define REG_COMP(preg, regex, cflags) regcomp(preg, regex, REG_WORDS | REG_EXTENDED | (cflags))
54 
60 static inline regoff_t mutt_regmatch_start(const regmatch_t *match)
61 {
62  return match->rm_so;
63 }
64 
70 static inline regoff_t mutt_regmatch_end(const regmatch_t *match)
71 {
72  return match->rm_eo;
73 }
74 
80 static inline size_t mutt_regmatch_len(const regmatch_t *match)
81 {
82  return match->rm_eo - match->rm_so;
83 }
84 
88 struct Regex
89 {
90  char *pattern;
91  regex_t *regex;
92  bool pat_not;
93 };
94 
98 struct RegexNode
99 {
100  struct Regex *regex;
101  STAILQ_ENTRY(RegexNode) entries;
102 };
103 STAILQ_HEAD(RegexList, RegexNode);
104 
108 struct Replace
109 {
110  struct Regex *regex;
111  size_t nmatch;
112  char *templ;
113  STAILQ_ENTRY(Replace) entries;
114 };
115 STAILQ_HEAD(ReplaceList, Replace);
116 
117 struct Regex *mutt_regex_compile(const char *str, int flags);
118 struct Regex *mutt_regex_new(const char *str, int flags, struct Buffer *err);
119 void mutt_regex_free(struct Regex **r);
120 
121 int mutt_regexlist_add (struct RegexList *rl, const char *str, int flags, struct Buffer *err);
122 void mutt_regexlist_free (struct RegexList *rl);
123 bool mutt_regexlist_match (struct RegexList *rl, const char *str);
124 struct RegexNode *mutt_regexlist_new (void);
125 int mutt_regexlist_remove(struct RegexList *rl, const char *str);
126 
127 int mutt_replacelist_add (struct ReplaceList *rl, const char *pat, const char *templ, struct Buffer *err);
128 char * mutt_replacelist_apply (struct ReplaceList *rl, char *buf, size_t buflen, const char *str);
129 void mutt_replacelist_free (struct ReplaceList *rl);
130 bool mutt_replacelist_match (struct ReplaceList *rl, char *buf, size_t buflen, const char *str);
131 struct Replace *mutt_replacelist_new (void);
132 int mutt_replacelist_remove(struct ReplaceList *rl, const char *pat);
133 
134 bool mutt_regex_match (const struct Regex *regex, const char *str);
135 bool mutt_regex_capture(const struct Regex *regex, const char *str, size_t num, regmatch_t matches[]);
136 
137 #endif /* MUTT_LIB_REGEX_H */
char * templ
Template to match.
Definition: regex3.h:112
int mutt_regexlist_remove(struct RegexList *rl, const char *str)
Remove a Regex from a list.
Definition: regex.c:225
bool mutt_replacelist_match(struct ReplaceList *rl, char *buf, size_t buflen, const char *str)
Does a string match a pattern?
Definition: regex.c:474
int mutt_regexlist_add(struct RegexList *rl, const char *str, int flags, struct Buffer *err)
Compile a regex string and add it to a list.
Definition: regex.c:131
regex_t * regex
compiled expression
Definition: regex3.h:91
static size_t mutt_regmatch_len(const regmatch_t *match)
Return the length of a match.
Definition: regex3.h:80
String manipulation buffer.
Definition: buffer.h:33
bool pat_not
do not match
Definition: regex3.h:92
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:261
struct Regex * mutt_regex_compile(const char *str, int flags)
Create an Regex from a string.
Definition: regex.c:52
#define STAILQ_ENTRY(type)
Definition: queue.h:324
bool mutt_regex_match(const struct Regex *regex, const char *str)
Shorthand to mutt_regex_capture()
Definition: regex.c:609
List of regular expressions.
Definition: regex3.h:108
struct RegexNode * mutt_regexlist_new(void)
Create a new RegexList.
Definition: regex.c:211
struct Regex * regex
Regex containing a regular expression.
Definition: regex3.h:110
struct Regex * mutt_regex_new(const char *str, int flags, struct Buffer *err)
Create an Regex from a string.
Definition: regex.c:73
void mutt_replacelist_free(struct ReplaceList *rl)
Free a ReplaceList object.
Definition: regex.c:446
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:593
void mutt_regex_free(struct Regex **r)
Free a Regex object.
Definition: regex.c:110
struct Regex * regex
Regex containing a regular expression.
Definition: regex3.h:100
int mutt_replacelist_remove(struct ReplaceList *rl, const char *pat)
Remove a pattern from a list.
Definition: regex.c:563
char * mutt_replacelist_apply(struct ReplaceList *rl, char *buf, size_t buflen, const char *str)
Apply replacements to a buffer.
Definition: regex.c:351
static regoff_t mutt_regmatch_start(const regmatch_t *match)
Return the start of a match.
Definition: regex3.h:60
List of regular expressions.
Definition: regex3.h:98
Cached regular expression.
Definition: regex3.h:88
struct Replace * mutt_replacelist_new(void)
Create a new ReplaceList.
Definition: regex.c:552
void mutt_regexlist_free(struct RegexList *rl)
Free a RegexList object.
Definition: regex.c:169
static regoff_t mutt_regmatch_end(const regmatch_t *match)
Return the end of a match.
Definition: regex3.h:70
char * pattern
printable version
Definition: regex3.h:90
STAILQ_HEAD(RegexList, RegexNode)
bool mutt_regexlist_match(struct RegexList *rl, const char *str)
Does a string match any Regex in the list?
Definition: regex.c:190
size_t nmatch
Match the &#39;nth&#39; occurrence (0 means the whole expression)
Definition: regex3.h:111