]> git.sur5r.net Git - i3/i3/blob - src/regex.c
Merge branch 'master' into next
[i3/i3] / src / regex.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  *
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  */
22 struct regex *regex_new(const char *pattern) {
23     const char *error;
24     int errorcode, offset;
25
26     struct regex *re = scalloc(sizeof(struct regex));
27     re->pattern = sstrdup(pattern);
28     /* We use PCRE_UCP so that \B, \b, \D, \d, \S, \s, \W, \w and some POSIX
29      * character classes play nicely with Unicode */
30     int options = PCRE_UCP | PCRE_UTF8;
31     while (!(re->regex = pcre_compile2(pattern, options, &errorcode, &error, &offset, NULL))) {
32         /* If the error is that PCRE was not compiled with UTF-8 support we
33          * disable it and try again */
34         if (errorcode == 32) {
35             options &= ~PCRE_UTF8;
36             continue;
37         }
38         ELOG("PCRE regular expression compilation failed at %d: %s\n",
39              offset, error);
40         return NULL;
41     }
42     re->extra = pcre_study(re->regex, 0, &error);
43     /* If an error happened, we print the error message, but continue.
44      * Studying the regular expression leads to faster matching, but it’s not
45      * absolutely necessary. */
46     if (error) {
47         ELOG("PCRE regular expression studying failed: %s\n", error);
48     }
49     return re;
50 }
51
52 /*
53  * Frees the given regular expression. It must not be used afterwards!
54  *
55  */
56 void regex_free(struct regex *regex) {
57     if (!regex)
58         return;
59     FREE(regex->pattern);
60     FREE(regex->regex);
61     FREE(regex->extra);
62 }
63
64 /*
65  * Checks if the given regular expression matches the given input and returns
66  * true if it does. In either case, it logs the outcome using LOG(), so it will
67  * be visible without any debug loglevel.
68  *
69  */
70 bool regex_matches(struct regex *regex, const char *input) {
71     int rc;
72
73     /* We use strlen() because pcre_exec() expects the length of the input
74      * string in bytes */
75     if ((rc = pcre_exec(regex->regex, regex->extra, input, strlen(input), 0, 0, NULL, 0)) == 0) {
76         LOG("Regular expression \"%s\" matches \"%s\"\n",
77             regex->pattern, input);
78         return true;
79     }
80
81     if (rc == PCRE_ERROR_NOMATCH) {
82         LOG("Regular expression \"%s\" does not match \"%s\"\n",
83             regex->pattern, input);
84         return false;
85     }
86
87     ELOG("PCRE error %d while trying to use regular expression \"%s\" on input \"%s\", see pcreapi(3)\n",
88          rc, regex->pattern, input);
89     return false;
90 }