From 2fc54aadf1ae7cbd4b08442b7a933950a22c66af Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Sat, 10 Sep 2011 23:53:11 +0100 Subject: [PATCH] Implement support for PCRE regular expressions for all criteria (for_window, commands, assignments) --- common.mk | 2 ++ include/all.h | 1 + include/data.h | 27 ++++++++++++++++----- include/regex.h | 28 ++++++++++++++++++++++ src/cfgparse.y | 26 ++++++++++++-------- src/cmdparse.y | 17 +++++++------ src/match.c | 28 ++++++++++++---------- src/regex.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 157 insertions(+), 35 deletions(-) create mode 100644 include/regex.h create mode 100644 src/regex.c diff --git a/common.mk b/common.mk index ce41f287..62eb9958 100644 --- a/common.mk +++ b/common.mk @@ -49,6 +49,7 @@ CFLAGS += $(call cflags_for_lib, xcursor) CFLAGS += $(call cflags_for_lib, x11) CFLAGS += $(call cflags_for_lib, yajl) CFLAGS += $(call cflags_for_lib, libev) +CFLAGS += $(call cflags_for_lib, libpcre) CPPFLAGS += -DI3_VERSION=\"${GIT_VERSION}\" CPPFLAGS += -DSYSCONFDIR=\"${SYSCONFDIR}\" CPPFLAGS += -DTERM_EMU=\"$(TERM_EMU)\" @@ -70,6 +71,7 @@ LIBS += $(call ldflags_for_lib, xcursor, Xcursor) LIBS += $(call ldflags_for_lib, x11, X11) LIBS += $(call ldflags_for_lib, yajl, yajl) LIBS += $(call ldflags_for_lib, libev, ev) +LIBS += $(call ldflags_for_lib, libpcre, pcre) # Please test if -Wl,--as-needed works on your platform and send me a patch. # it is known not to work on Darwin (Mac OS X) diff --git a/include/all.h b/include/all.h index b87be518..9c08ebef 100644 --- a/include/all.h +++ b/include/all.h @@ -64,5 +64,6 @@ #include "output.h" #include "ewmh.h" #include "assignments.h" +#include "regex.h" #endif diff --git a/include/data.h b/include/data.h index 5797b7d8..122833c3 100644 --- a/include/data.h +++ b/include/data.h @@ -10,6 +10,7 @@ #include #include #include +#include #ifndef _DATA_H #define _DATA_H @@ -137,6 +138,21 @@ struct Ignore_Event { SLIST_ENTRY(Ignore_Event) ignore_events; }; +/** + * Regular expression wrapper. It contains the pattern itself as a string (like + * ^foo[0-9]$) as well as a pointer to the compiled PCRE expression and the + * pcre_extra data returned by pcre_study(). + * + * This makes it easier to have a useful logfile, including the matching or + * non-matching pattern. + * + */ +struct regex { + const char *pattern; + pcre *regex; + pcre_extra *extra; +}; + /****************************************************************************** * Major types *****************************************************************************/ @@ -277,12 +293,11 @@ struct Window { }; struct Match { - char *title; - int title_len; - char *application; - char *class; - char *instance; - char *mark; + struct regex *title; + struct regex *application; + struct regex *class; + struct regex *instance; + struct regex *mark; enum { M_DONTCHECK = -1, M_NODOCK = 0, diff --git a/include/regex.h b/include/regex.h new file mode 100644 index 00000000..2a6608a8 --- /dev/null +++ b/include/regex.h @@ -0,0 +1,28 @@ +/* + * vim:ts=4:sw=4:expandtab + * + */ +#ifndef _REGEX_H +#define _REGEX_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); + +/** + * 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); + +#endif diff --git a/src/cfgparse.y b/src/cfgparse.y index 1cbcaf9e..868640e0 100644 --- a/src/cfgparse.y +++ b/src/cfgparse.y @@ -751,12 +751,14 @@ criterion: TOK_CLASS '=' STR { printf("criteria: class = %s\n", $3); - current_match.class = $3; + current_match.class = regex_new($3); + free($3); } | TOK_INSTANCE '=' STR { printf("criteria: instance = %s\n", $3); - current_match.instance = $3; + current_match.instance = regex_new($3); + free($3); } | TOK_CON_ID '=' STR { @@ -791,12 +793,14 @@ criterion: | TOK_MARK '=' STR { printf("criteria: mark = %s\n", $3); - current_match.mark = $3; + current_match.mark = regex_new($3); + free($3); } | TOK_TITLE '=' STR { printf("criteria: title = %s\n", $3); - current_match.title = $3; + current_match.title = regex_new($3); + free($3); } ; @@ -1054,6 +1058,8 @@ workspace_name: assign: TOKASSIGN window_class STR { + /* TODO: the assign command also needs some kind of new syntax where we + * just use criteria. Then deprecate the old form */ printf("assignment of %s to *%s*\n", $2, $3); char *workspace = $3; char *criteria = $2; @@ -1065,15 +1071,15 @@ assign: char *separator = NULL; if ((separator = strchr(criteria, '/')) != NULL) { *(separator++) = '\0'; - match->title = sstrdup(separator); + match->title = regex_new(separator); + printf(" title = %s\n", separator); + } + if (*criteria != '\0') { + match->class = regex_new(criteria); + printf(" class = %s\n", criteria); } - if (*criteria != '\0') - match->class = sstrdup(criteria); free(criteria); - printf(" class = %s\n", match->class); - printf(" title = %s\n", match->title); - /* Compatibility with older versions: If the assignment target starts * with ~, we create the equivalent of: * diff --git a/src/cmdparse.y b/src/cmdparse.y index 72ffae6a..83abbbf2 100644 --- a/src/cmdparse.y +++ b/src/cmdparse.y @@ -267,10 +267,9 @@ matchend: } } else if (current_match.mark != NULL && current->con->mark != NULL && - strcasecmp(current_match.mark, current->con->mark) == 0) { + regex_matches(current_match.mark, current->con->mark)) { printf("match by mark\n"); - TAILQ_INSERT_TAIL(&owindows, current, owindows); - + TAILQ_INSERT_TAIL(&owindows, current, owindows); } else { if (current->con->window == NULL) continue; @@ -300,12 +299,14 @@ criterion: TOK_CLASS '=' STR { printf("criteria: class = %s\n", $3); - current_match.class = $3; + current_match.class = regex_new($3); + free($3); } | TOK_INSTANCE '=' STR { printf("criteria: instance = %s\n", $3); - current_match.instance = $3; + current_match.instance = regex_new($3); + free($3); } | TOK_CON_ID '=' STR { @@ -340,12 +341,14 @@ criterion: | TOK_MARK '=' STR { printf("criteria: mark = %s\n", $3); - current_match.mark = $3; + current_match.mark = regex_new($3); + free($3); } | TOK_TITLE '=' STR { printf("criteria: title = %s\n", $3); - current_match.title = $3; + current_match.title = regex_new($3); + free($3); } ; diff --git a/src/match.c b/src/match.c index 3a346117..85d2eaab 100644 --- a/src/match.c +++ b/src/match.c @@ -52,16 +52,19 @@ bool match_is_empty(Match *match) { void match_copy(Match *dest, Match *src) { memcpy(dest, src, sizeof(Match)); -#define STRDUP(field) do { \ +/* The DUPLICATE_REGEX macro creates a new regular expression from the + * ->pattern of the old one. It therefore does use a little more memory then + * with a refcounting system, but it’s easier this way. */ +#define DUPLICATE_REGEX(field) do { \ if (src->field != NULL) \ - dest->field = sstrdup(src->field); \ + dest->field = regex_new(src->field->pattern); \ } while (0) - STRDUP(title); - STRDUP(mark); - STRDUP(application); - STRDUP(class); - STRDUP(instance); + DUPLICATE_REGEX(title); + DUPLICATE_REGEX(mark); + DUPLICATE_REGEX(application); + DUPLICATE_REGEX(class); + DUPLICATE_REGEX(instance); } /* @@ -71,9 +74,9 @@ void match_copy(Match *dest, Match *src) { bool match_matches_window(Match *match, i3Window *window) { LOG("checking window %d (%s)\n", window->id, window->class_class); - /* TODO: pcre, full matching, … */ if (match->class != NULL) { - if (window->class_class != NULL && strcasecmp(match->class, window->class_class) == 0) { + if (window->class_class != NULL && + regex_matches(match->class, window->class_class)) { LOG("window class matches (%s)\n", window->class_class); } else { LOG("window class does not match\n"); @@ -82,7 +85,8 @@ bool match_matches_window(Match *match, i3Window *window) { } if (match->instance != NULL) { - if (window->class_instance != NULL && strcasecmp(match->instance, window->class_instance) == 0) { + if (window->class_instance != NULL && + regex_matches(match->instance, window->class_instance)) { LOG("window instance matches (%s)\n", window->class_instance); } else { LOG("window instance does not match\n"); @@ -99,9 +103,9 @@ bool match_matches_window(Match *match, i3Window *window) { } } - /* TODO: pcre match */ if (match->title != NULL) { - if (window->name_json != NULL && strcasecmp(match->title, window->name_json) == 0) { + if (window->name_json != NULL && + regex_matches(match->title, window->name_json)) { LOG("title matches (%s)\n", window->name_json); } else { LOG("title does not match\n"); diff --git a/src/regex.c b/src/regex.c new file mode 100644 index 00000000..df01dae4 --- /dev/null +++ b/src/regex.c @@ -0,0 +1,63 @@ +/* + * vim:ts=4:sw=4:expandtab + * + * i3 - an improved dynamic tiling window manager + * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE) + * + * + */ + +#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; + int offset; + + struct regex *re = scalloc(sizeof(struct regex)); + re->pattern = sstrdup(pattern); + if (!(re->regex = pcre_compile(pattern, 0, &error, &offset, NULL))) { + ELOG("PCRE regular expression compilation failed at %d: %s", + offset, error); + return NULL; + } + re->extra = pcre_study(re->regex, 0, &error); + return re; +} + +/* + * 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; + + /* TODO: is strlen(input) correct for UTF-8 matching? */ + /* TODO: enable UTF-8 */ + 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; + } + + /* TODO: handle the other error codes */ + LOG("PCRE error\n"); + return false; +}