i3
regex.c
Go to the documentation of this file.
1 #undef I3__FILE__
2 #define I3__FILE__ "regex.c"
3 /*
4  * vim:ts=4:sw=4:expandtab
5  *
6  * i3 - an improved dynamic tiling window manager
7  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
8  *
9  * regex.c: Interface to libPCRE (perl compatible regular expressions).
10  *
11  */
12 #include "all.h"
13 
14 /*
15  * Creates a new 'regex' struct containing the given pattern and a PCRE
16  * compiled regular expression. Also, calls pcre_study because this regex will
17  * most likely be used often (like for every new window and on every relevant
18  * property change of existing windows).
19  *
20  * Returns NULL if the pattern could not be compiled into a regular expression
21  * (and ELOGs an appropriate error message).
22  *
23  */
24 struct regex *regex_new(const char *pattern) {
25  const char *error;
26  int errorcode, offset;
27 
28  struct regex *re = scalloc(sizeof(struct regex));
29  re->pattern = sstrdup(pattern);
30  int options = PCRE_UTF8;
31 #ifdef PCRE_HAS_UCP
32  /* We use PCRE_UCP so that \B, \b, \D, \d, \S, \s, \W, \w and some POSIX
33  * character classes play nicely with Unicode */
34  options |= PCRE_UCP;
35 #endif
36  while (!(re->regex = pcre_compile2(pattern, options, &errorcode, &error, &offset, NULL))) {
37  /* If the error is that PCRE was not compiled with UTF-8 support we
38  * disable it and try again */
39  if (errorcode == 32) {
40  options &= ~PCRE_UTF8;
41  continue;
42  }
43  ELOG("PCRE regular expression compilation failed at %d: %s\n",
44  offset, error);
45  return NULL;
46  }
47  re->extra = pcre_study(re->regex, 0, &error);
48  /* If an error happened, we print the error message, but continue.
49  * Studying the regular expression leads to faster matching, but it’s not
50  * absolutely necessary. */
51  if (error) {
52  ELOG("PCRE regular expression studying failed: %s\n", error);
53  }
54  return re;
55 }
56 
57 /*
58  * Frees the given regular expression. It must not be used afterwards!
59  *
60  */
61 void regex_free(struct regex *regex) {
62  if (!regex)
63  return;
64  FREE(regex->pattern);
65  FREE(regex->regex);
66  FREE(regex->extra);
67 }
68 
69 /*
70  * Checks if the given regular expression matches the given input and returns
71  * true if it does. In either case, it logs the outcome using LOG(), so it will
72  * be visible without debug logging.
73  *
74  */
75 bool regex_matches(struct regex *regex, const char *input) {
76  int rc;
77 
78  /* We use strlen() because pcre_exec() expects the length of the input
79  * string in bytes */
80  if ((rc = pcre_exec(regex->regex, regex->extra, input, strlen(input), 0, 0, NULL, 0)) == 0) {
81  LOG("Regular expression \"%s\" matches \"%s\"\n",
82  regex->pattern, input);
83  return true;
84  }
85 
86  if (rc == PCRE_ERROR_NOMATCH) {
87  LOG("Regular expression \"%s\" does not match \"%s\"\n",
88  regex->pattern, input);
89  return false;
90  }
91 
92  ELOG("PCRE error %d while trying to use regular expression \"%s\" on input \"%s\", see pcreapi(3)\n",
93  rc, regex->pattern, input);
94  return false;
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 LOG(fmt,...)
Definition: libi3.h:76
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:24
void regex_free(struct regex *regex)
Frees the given regular expression.
Definition: regex.c:61
pcre * regex
Definition: data.h:217
#define ELOG(fmt,...)
Definition: libi3.h:81
void * scalloc(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:215
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:75
char * pattern
Definition: data.h:216
#define FREE(pointer)
Definition: util.h:46
pcre_extra * extra
Definition: data.h:218