2011-09-11 00:53:11 +02:00
|
|
|
|
/*
|
|
|
|
|
* vim:ts=4:sw=4:expandtab
|
|
|
|
|
*
|
|
|
|
|
* i3 - an improved dynamic tiling window manager
|
|
|
|
|
* © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
|
|
|
|
|
*
|
2011-10-23 00:40:02 +02:00
|
|
|
|
* regex.c: Interface to libPCRE (perl compatible regular expressions).
|
2011-09-11 00:53:11 +02:00
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
#include "all.h"
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Creates a new 'regex' struct containing the given pattern and a PCRE
|
|
|
|
|
* compiled regular expression. Also, calls pcre_study because this regex will
|
|
|
|
|
* most likely be used often (like for every new window and on every relevant
|
|
|
|
|
* property change of existing windows).
|
|
|
|
|
*
|
|
|
|
|
* Returns NULL if the pattern could not be compiled into a regular expression
|
|
|
|
|
* (and ELOGs an appropriate error message).
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
struct regex *regex_new(const char *pattern) {
|
|
|
|
|
const char *error;
|
2011-09-11 12:40:51 +02:00
|
|
|
|
int errorcode, offset;
|
2011-09-11 00:53:11 +02:00
|
|
|
|
|
|
|
|
|
struct regex *re = scalloc(sizeof(struct regex));
|
|
|
|
|
re->pattern = sstrdup(pattern);
|
2011-09-30 21:31:32 +02:00
|
|
|
|
int options = PCRE_UTF8;
|
|
|
|
|
#ifdef PCRE_HAS_UCP
|
2011-09-11 12:40:51 +02:00
|
|
|
|
/* We use PCRE_UCP so that \B, \b, \D, \d, \S, \s, \W, \w and some POSIX
|
|
|
|
|
* character classes play nicely with Unicode */
|
2011-09-30 21:31:32 +02:00
|
|
|
|
options |= PCRE_UCP;
|
|
|
|
|
#endif
|
2011-09-11 12:40:51 +02:00
|
|
|
|
while (!(re->regex = pcre_compile2(pattern, options, &errorcode, &error, &offset, NULL))) {
|
|
|
|
|
/* If the error is that PCRE was not compiled with UTF-8 support we
|
|
|
|
|
* disable it and try again */
|
|
|
|
|
if (errorcode == 32) {
|
|
|
|
|
options &= ~PCRE_UTF8;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
ELOG("PCRE regular expression compilation failed at %d: %s\n",
|
2011-09-11 00:53:11 +02:00
|
|
|
|
offset, error);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
re->extra = pcre_study(re->regex, 0, &error);
|
2011-09-11 12:40:51 +02:00
|
|
|
|
/* If an error happened, we print the error message, but continue.
|
|
|
|
|
* Studying the regular expression leads to faster matching, but it’s not
|
|
|
|
|
* absolutely necessary. */
|
|
|
|
|
if (error) {
|
|
|
|
|
ELOG("PCRE regular expression studying failed: %s\n", error);
|
|
|
|
|
}
|
2011-09-11 00:53:11 +02:00
|
|
|
|
return re;
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-12 00:41:46 +02:00
|
|
|
|
/*
|
|
|
|
|
* Frees the given regular expression. It must not be used afterwards!
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
void regex_free(struct regex *regex) {
|
|
|
|
|
if (!regex)
|
|
|
|
|
return;
|
|
|
|
|
FREE(regex->pattern);
|
|
|
|
|
FREE(regex->regex);
|
|
|
|
|
FREE(regex->extra);
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-11 00:53:11 +02:00
|
|
|
|
/*
|
|
|
|
|
* Checks if the given regular expression matches the given input and returns
|
|
|
|
|
* true if it does. In either case, it logs the outcome using LOG(), so it will
|
|
|
|
|
* be visible without any debug loglevel.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
bool regex_matches(struct regex *regex, const char *input) {
|
|
|
|
|
int rc;
|
|
|
|
|
|
2011-09-11 12:40:51 +02:00
|
|
|
|
/* We use strlen() because pcre_exec() expects the length of the input
|
|
|
|
|
* string in bytes */
|
2011-09-11 00:53:11 +02:00
|
|
|
|
if ((rc = pcre_exec(regex->regex, regex->extra, input, strlen(input), 0, 0, NULL, 0)) == 0) {
|
|
|
|
|
LOG("Regular expression \"%s\" matches \"%s\"\n",
|
|
|
|
|
regex->pattern, input);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rc == PCRE_ERROR_NOMATCH) {
|
|
|
|
|
LOG("Regular expression \"%s\" does not match \"%s\"\n",
|
|
|
|
|
regex->pattern, input);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-11 12:40:51 +02:00
|
|
|
|
ELOG("PCRE error %d while trying to use regular expression \"%s\" on input \"%s\", see pcreapi(3)\n",
|
|
|
|
|
rc, regex->pattern, input);
|
2011-09-11 00:53:11 +02:00
|
|
|
|
return false;
|
|
|
|
|
}
|