i3
regex.c
Go to the documentation of this file.
1/*
2 * vim:ts=4:sw=4:expandtab
3 *
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
6 *
7 * regex.c: Interface to libPCRE (perl compatible regular expressions).
8 *
9 */
10#include "all.h"
11
12/*
13 * Creates a new 'regex' struct containing the given pattern and a PCRE
14 * compiled regular expression. Also, calls pcre_study because this regex will
15 * most likely be used often (like for every new window and on every relevant
16 * property change of existing windows).
17 *
18 * Returns NULL if the pattern could not be compiled into a regular expression
19 * (and ELOGs an appropriate error message).
20 *
21 */
22struct regex *regex_new(const char *pattern) {
23 int errorcode;
24 PCRE2_SIZE offset;
25
26 struct regex *re = scalloc(1, sizeof(struct regex));
27 re->pattern = sstrdup(pattern);
28 uint32_t options = PCRE2_UTF;
29 /* We use PCRE_UCP so that \B, \b, \D, \d, \S, \s, \W, \w and some POSIX
30 * character classes play nicely with Unicode */
31 options |= PCRE2_UCP;
32 if (!(re->regex = pcre2_compile((PCRE2_SPTR)pattern, PCRE2_ZERO_TERMINATED, options, &errorcode, &offset, NULL))) {
33 PCRE2_UCHAR buffer[256];
34 pcre2_get_error_message(errorcode, buffer, sizeof(buffer));
35 ELOG("PCRE regular expression compilation failed at %lu: %s\n",
36 offset, buffer);
37 regex_free(re);
38 return NULL;
39 }
40 return re;
41}
42
43/*
44 * Frees the given regular expression. It must not be used afterwards!
45 *
46 */
47void regex_free(struct regex *regex) {
48 if (!regex)
49 return;
52 FREE(regex);
53}
54
55/*
56 * Checks if the given regular expression matches the given input and returns
57 * true if it does. In either case, it logs the outcome using LOG(), so it will
58 * be visible without debug logging.
59 *
60 */
61bool regex_matches(struct regex *regex, const char *input) {
62 pcre2_match_data *match_data;
63 int rc;
64
65 match_data = pcre2_match_data_create_from_pattern(regex->regex, NULL);
66
67 /* We use strlen() because pcre_exec() expects the length of the input
68 * string in bytes */
69 rc = pcre2_match(regex->regex, (PCRE2_SPTR)input, strlen(input), 0, 0, match_data, NULL);
70 pcre2_match_data_free(match_data);
71 if (rc > 0) {
72 LOG("Regular expression \"%s\" matches \"%s\"\n",
73 regex->pattern, input);
74 return true;
75 }
76
77 if (rc == PCRE2_ERROR_NOMATCH) {
78 LOG("Regular expression \"%s\" does not match \"%s\"\n",
79 regex->pattern, input);
80 return false;
81 }
82
83 ELOG("PCRE error %d while trying to use regular expression \"%s\" on input \"%s\", see pcreapi(3)\n",
84 rc, regex->pattern, input);
85 return false;
86}
bool regex_matches(struct regex *regex, const char *input)
Checks if the given regular expression matches the given input and returns true if it does.
Definition regex.c:61
void regex_free(struct regex *regex)
Frees the given regular expression.
Definition regex.c:47
struct regex * regex_new(const char *pattern)
Creates a new 'regex' struct containing the given pattern and a PCRE compiled regular expression.
Definition regex.c:22
#define FREE(pointer)
Definition util.h:47
#define LOG(fmt,...)
Definition libi3.h:95
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
#define ELOG(fmt,...)
Definition libi3.h:100
void * scalloc(size_t num, size_t size)
Safe-wrapper around calloc which exits if malloc returns NULL (meaning that there is no more memory a...
Regular expression wrapper.
Definition data.h:284
char * pattern
Definition data.h:285
pcre2_code * regex
Definition data.h:286