Implement support for PCRE regular expressions for all criteria (for_window, commands, assignments)

next
Michael Stapelberg 2011-09-10 23:53:11 +01:00
parent 8e04867e51
commit 2fc54aadf1
8 changed files with 157 additions and 35 deletions

View File

@ -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)

View File

@ -64,5 +64,6 @@
#include "output.h"
#include "ewmh.h"
#include "assignments.h"
#include "regex.h"
#endif

View File

@ -10,6 +10,7 @@
#include <xcb/randr.h>
#include <xcb/xcb_atom.h>
#include <stdbool.h>
#include <pcre.h>
#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,

28
include/regex.h Normal file
View File

@ -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

View File

@ -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:
*

View File

@ -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);
}
;

View File

@ -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 its 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");

63
src/regex.c Normal file
View File

@ -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;
}