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  */
22 struct regex *regex_new(const char *pattern) {
23  const char *error;
24  int errorcode, offset;
25 
26  struct regex *re = scalloc(1, sizeof(struct regex));
27  re->pattern = sstrdup(pattern);
28  int options = PCRE_UTF8;
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 |= PCRE_UCP;
32  while (!(re->regex = pcre_compile2(pattern, options, &errorcode, &error, &offset, NULL))) {
33  /* If the error is that PCRE was not compiled with UTF-8 support we
34  * disable it and try again */
35  if (errorcode == 32) {
36  options &= ~PCRE_UTF8;
37  continue;
38  }
39  ELOG("PCRE regular expression compilation failed at %d: %s\n",
40  offset, error);
41  regex_free(re);
42  return NULL;
43  }
44  re->extra = pcre_study(re->regex, 0, &error);
45  /* If an error happened, we print the error message, but continue.
46  * Studying the regular expression leads to faster matching, but it’s not
47  * absolutely necessary. */
48  if (error) {
49  ELOG("PCRE regular expression studying failed: %s\n", error);
50  }
51  return re;
52 }
53 
54 /*
55  * Frees the given regular expression. It must not be used afterwards!
56  *
57  */
58 void regex_free(struct regex *regex) {
59  if (!regex)
60  return;
61  FREE(regex->pattern);
62  FREE(regex->regex);
63  FREE(regex->extra);
64  FREE(regex);
65 }
66 
67 /*
68  * Checks if the given regular expression matches the given input and returns
69  * true if it does. In either case, it logs the outcome using LOG(), so it will
70  * be visible without debug logging.
71  *
72  */
73 bool regex_matches(struct regex *regex, const char *input) {
74  int rc;
75 
76  /* We use strlen() because pcre_exec() expects the length of the input
77  * string in bytes */
78  if ((rc = pcre_exec(regex->regex, regex->extra, input, strlen(input), 0, 0, NULL, 0)) == 0) {
79  LOG("Regular expression \"%s\" matches \"%s\"\n",
80  regex->pattern, input);
81  return true;
82  }
83 
84  if (rc == PCRE_ERROR_NOMATCH) {
85  LOG("Regular expression \"%s\" does not match \"%s\"\n",
86  regex->pattern, input);
87  return false;
88  }
89 
90  ELOG("PCRE error %d while trying to use regular expression \"%s\" on input \"%s\", see pcreapi(3)\n",
91  rc, regex->pattern, input);
92  return false;
93 }
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
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:73
void regex_free(struct regex *regex)
Frees the given regular expression.
Definition: regex.c:58
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:95
#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:249
pcre_extra * extra
Definition: data.h:252
pcre * regex
Definition: data.h:251
char * pattern
Definition: data.h:250