NeoMutt  2024-10-02-34-g9dd811
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
regex.c File Reference

Type representing a regular expression. More...

#include "config.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "mutt/lib.h"
#include "regex2.h"
#include "set.h"
#include "types.h"
+ Include dependency graph for regex.c:

Go to the source code of this file.

Functions

bool regex_equal (const struct Regex *a, const struct Regex *b)
 Compare two regexes.
 
void regex_free (struct Regex **ptr)
 Free a Regex object.
 
static void regex_destroy (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
 Destroy a Regex object - Implements ConfigSetType::destroy() -.
 
struct Regexregex_new (const char *str, uint32_t flags, struct Buffer *err)
 Create an Regex from a string.
 
static int regex_string_set (const struct ConfigSet *cs, void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
 Set a Regex by string - Implements ConfigSetType::string_set() -.
 
static int regex_string_get (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *result)
 Get a Regex as a string - Implements ConfigSetType::string_get() -.
 
static int regex_native_set (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
 Set a Regex config item by Regex object - Implements ConfigSetType::native_set() -.
 
static intptr_t regex_native_get (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
 Get a Regex object from a Regex config item - Implements ConfigSetType::native_get() -.
 
static int regex_reset (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
 Reset a Regex to its initial value - Implements ConfigSetType::reset() -.
 

Variables

const struct ConfigSetType CstRegex
 Config type representing a regular expression.
 

Detailed Description

Type representing a regular expression.

Authors
  • Richard Russon
  • Pietro Cerutti
  • наб

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

◆ regex_equal()

bool regex_equal ( const struct Regex a,
const struct Regex b 
)

Compare two regexes.

Parameters
aFirst regex
bSecond regex
Return values
trueThey are identical

Definition at line 52 of file regex.c.

53{
54 if (!a && !b) /* both empty */
55 return true;
56 if (!a ^ !b) /* one is empty, but not the other */
57 return false;
58 if (a->pat_not != b->pat_not)
59 return false;
60
61 return mutt_str_equal(a->pattern, b->pattern);
62}
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:660
char * pattern
printable version
Definition: regex3.h:87
bool pat_not
do not match
Definition: regex3.h:89
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ regex_free()

void regex_free ( struct Regex **  ptr)

Free a Regex object.

Parameters
[out]ptrRegex to free

Definition at line 68 of file regex.c.

69{
70 if (!ptr || !*ptr)
71 return;
72
73 struct Regex *rx = *ptr;
74 FREE(&rx->pattern);
75 if (rx->regex)
76 regfree(rx->regex);
77 FREE(&rx->regex);
78
79 FREE(ptr);
80}
#define FREE(x)
Definition: memory.h:45
Cached regular expression.
Definition: regex3.h:86
regex_t * regex
compiled expression
Definition: regex3.h:88
+ Here is the caller graph for this function:

◆ regex_new()

struct Regex * 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 102 of file regex.c.

103{
104 if (!str)
105 return NULL;
106
107 int rflags = 0;
108 struct Regex *reg = mutt_mem_calloc(1, sizeof(struct Regex));
109
110 reg->regex = mutt_mem_calloc(1, sizeof(regex_t));
111 reg->pattern = mutt_str_dup(str);
112
113 /* Should we use smart case matching? */
114 if (((flags & D_REGEX_MATCH_CASE) == 0) && mutt_mb_is_lower(str))
115 rflags |= REG_ICASE;
116
117 if ((flags & D_REGEX_NOSUB))
118 rflags |= REG_NOSUB;
119
120 /* Is a prefix of '!' allowed? */
121 if (((flags & D_REGEX_ALLOW_NOT) != 0) && (str[0] == '!'))
122 {
123 reg->pat_not = true;
124 str++;
125 }
126
127 int rc = REG_COMP(reg->regex, str, rflags);
128 if (rc != 0)
129 {
130 if (err)
131 regerror(rc, reg->regex, err->data, err->dsize);
132 regex_free(&reg);
133 return NULL;
134 }
135
136 return reg;
137}
void regex_free(struct Regex **ptr)
Free a Regex object.
Definition: regex.c:68
bool mutt_mb_is_lower(const char *s)
Does a multi-byte string contain only lowercase characters?
Definition: mbyte.c:354
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:51
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:50
size_t dsize
Length of data.
Definition: buffer.h:39
char * data
Pointer to data.
Definition: buffer.h:37
#define D_REGEX_ALLOW_NOT
Regex can begin with '!'.
Definition: types.h:107
#define D_REGEX_MATCH_CASE
Case-sensitive matching.
Definition: types.h:106
#define D_REGEX_NOSUB
Do not report what was matched (REG_NOSUB)
Definition: types.h:108
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ CstRegex

const struct ConfigSetType CstRegex
Initial value:
= {
"regex",
NULL,
NULL,
}
static void regex_destroy(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
Destroy a Regex object - Implements ConfigSetType::destroy() -.
Definition: regex.c:85
static intptr_t regex_native_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Get a Regex object from a Regex config item - Implements ConfigSetType::native_get() -.
Definition: regex.c:275
static int regex_native_set(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Set a Regex config item by Regex object - Implements ConfigSetType::native_set() -.
Definition: regex.c:228
static int regex_reset(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Reset a Regex to its initial value - Implements ConfigSetType::reset() -.
Definition: regex.c:286
static int regex_string_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *result)
Get a Regex as a string - Implements ConfigSetType::string_get() -.
Definition: regex.c:202
static int regex_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
Set a Regex by string - Implements ConfigSetType::string_set() -.
Definition: regex.c:142
@ DT_REGEX
regular expressions
Definition: types.h:42

Config type representing a regular expression.

Definition at line 335 of file regex.c.