i3
src/regex.c
Go to the documentation of this file.
00001 /*
00002  * vim:ts=4:sw=4:expandtab
00003  *
00004  * i3 - an improved dynamic tiling window manager
00005  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
00006  *
00007  * regex.c: Interface to libPCRE (perl compatible regular expressions).
00008  *
00009  */
00010 #include "all.h"
00011 
00012 /*
00013  * Creates a new 'regex' struct containing the given pattern and a PCRE
00014  * compiled regular expression. Also, calls pcre_study because this regex will
00015  * most likely be used often (like for every new window and on every relevant
00016  * property change of existing windows).
00017  *
00018  * Returns NULL if the pattern could not be compiled into a regular expression
00019  * (and ELOGs an appropriate error message).
00020  *
00021  */
00022 struct regex *regex_new(const char *pattern) {
00023     const char *error;
00024     int errorcode, offset;
00025 
00026     struct regex *re = scalloc(sizeof(struct regex));
00027     re->pattern = sstrdup(pattern);
00028     int options = PCRE_UTF8;
00029 #ifdef PCRE_HAS_UCP
00030     /* We use PCRE_UCP so that \B, \b, \D, \d, \S, \s, \W, \w and some POSIX
00031      * character classes play nicely with Unicode */
00032     options |= PCRE_UCP;
00033 #endif
00034     while (!(re->regex = pcre_compile2(pattern, options, &errorcode, &error, &offset, NULL))) {
00035         /* If the error is that PCRE was not compiled with UTF-8 support we
00036          * disable it and try again */
00037         if (errorcode == 32) {
00038             options &= ~PCRE_UTF8;
00039             continue;
00040         }
00041         ELOG("PCRE regular expression compilation failed at %d: %s\n",
00042              offset, error);
00043         return NULL;
00044     }
00045     re->extra = pcre_study(re->regex, 0, &error);
00046     /* If an error happened, we print the error message, but continue.
00047      * Studying the regular expression leads to faster matching, but it’s not
00048      * absolutely necessary. */
00049     if (error) {
00050         ELOG("PCRE regular expression studying failed: %s\n", error);
00051     }
00052     return re;
00053 }
00054 
00055 /*
00056  * Frees the given regular expression. It must not be used afterwards!
00057  *
00058  */
00059 void regex_free(struct regex *regex) {
00060     if (!regex)
00061         return;
00062     FREE(regex->pattern);
00063     FREE(regex->regex);
00064     FREE(regex->extra);
00065 }
00066 
00067 /*
00068  * Checks if the given regular expression matches the given input and returns
00069  * true if it does. In either case, it logs the outcome using LOG(), so it will
00070  * be visible without any debug loglevel.
00071  *
00072  */
00073 bool regex_matches(struct regex *regex, const char *input) {
00074     int rc;
00075 
00076     /* We use strlen() because pcre_exec() expects the length of the input
00077      * string in bytes */
00078     if ((rc = pcre_exec(regex->regex, regex->extra, input, strlen(input), 0, 0, NULL, 0)) == 0) {
00079         LOG("Regular expression \"%s\" matches \"%s\"\n",
00080             regex->pattern, input);
00081         return true;
00082     }
00083 
00084     if (rc == PCRE_ERROR_NOMATCH) {
00085         LOG("Regular expression \"%s\" does not match \"%s\"\n",
00086             regex->pattern, input);
00087         return false;
00088     }
00089 
00090     ELOG("PCRE error %d while trying to use regular expression \"%s\" on input \"%s\", see pcreapi(3)\n",
00091          rc, regex->pattern, input);
00092     return false;
00093 }