i3
regex.c
Go to the documentation of this file.
1/*
2 * vim:ts=4:sw=4:expandtab
3 *
4 * i3 - an improved 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 %zu: %s\n",
36 (size_t)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;
50 }
53 FREE(regex);
54}
55
56/*
57 * Checks if the given regular expression matches the given input and returns
58 * true if it does. In either case, it logs the outcome using LOG(), so it will
59 * be visible without debug logging.
60 *
61 */
62bool regex_matches(struct regex *regex, const char *input) {
63 pcre2_match_data *match_data;
64 int rc;
65
66 match_data = pcre2_match_data_create_from_pattern(regex->regex, NULL);
67
68 /* We use strlen() because pcre_exec() expects the length of the input
69 * string in bytes */
70 rc = pcre2_match(regex->regex, (PCRE2_SPTR)input, strlen(input), 0, 0, match_data, NULL);
71 pcre2_match_data_free(match_data);
72 if (rc > 0) {
73 LOG("Regular expression \"%s\" matches \"%s\"\n",
74 regex->pattern, input);
75 return true;
76 }
77
78 if (rc == PCRE2_ERROR_NOMATCH) {
79 LOG("Regular expression \"%s\" does not match \"%s\"\n",
80 regex->pattern, input);
81 return false;
82 }
83
84 ELOG("PCRE error %d while trying to use regular expression \"%s\" on input \"%s\", see pcreapi(3)\n",
85 rc, regex->pattern, input);
86 return false;
87}
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:62
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 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...
#define FREE(pointer)
Definition util.h:47
Regular expression wrapper.
Definition data.h:280
char * pattern
Definition data.h:281
pcre2_code * regex
Definition data.h:282