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)\"
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)
#include "output.h"
#include "ewmh.h"
#include "assignments.h"
+#include "regex.h"
#endif
#include <xcb/randr.h>
#include <xcb/xcb_atom.h>
#include <stdbool.h>
+#include <pcre.h>
#ifndef _DATA_H
#define _DATA_H
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
*****************************************************************************/
};
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,
--- /dev/null
+/*
+ * 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
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
{
| 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);
}
;
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;
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:
*
}
} 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;
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
{
| 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);
}
;
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);
}
/*
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");
}
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");
}
}
- /* 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");
--- /dev/null
+/*
+ * 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;
+}