Merge branch 'pcre' into next

This commit is contained in:
Michael Stapelberg 2011-09-11 11:41:29 +01:00
commit 67cf37ce66
12 changed files with 238 additions and 40 deletions

View File

@ -20,6 +20,7 @@
│ docbook-xml │ 4.5 │ 4.5 │ http://www.methods.co.nz/asciidoc/ │
│ libxcursor │ 1.1.11 │ 1.1.11 │ http://ftp.x.org/pub/current/src/lib/ │
│ Xlib │ 1.3.3 │ 1.4.3 │ http://ftp.x.org/pub/current/src/lib/ │
│ PCRE │ 8.12 │ 8.12 │ http://www.pcre.org/ │
└─────────────┴────────┴────────┴────────────────────────────────────────┘
i3-msg, i3-input, i3-nagbar and i3-config-wizard do not introduce any new

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)

2
debian/control vendored
View File

@ -3,7 +3,7 @@ Section: utils
Priority: extra
Maintainer: Michael Stapelberg <michael@stapelberg.de>
DM-Upload-Allowed: yes
Build-Depends: debhelper (>= 6), libx11-dev, libxcb-util0-dev (>= 0.3.8), libxcb-keysyms1-dev, libxcb-xinerama0-dev (>= 1.1), libxcb-randr0-dev, libxcb-icccm4-dev, libxcursor-dev, asciidoc (>= 8.4.4), xmlto, docbook-xml, pkg-config, libev-dev, flex, bison, libyajl-dev, perl, texlive-latex-base, texlive-latex-recommended, texlive-latex-extra
Build-Depends: debhelper (>= 6), libx11-dev, libxcb-util0-dev (>= 0.3.8), libxcb-keysyms1-dev, libxcb-xinerama0-dev (>= 1.1), libxcb-randr0-dev, libxcb-icccm4-dev, libxcursor-dev, asciidoc (>= 8.4.4), xmlto, docbook-xml, pkg-config, libev-dev, flex, bison, libyajl-dev, perl, texlive-latex-base, texlive-latex-recommended, texlive-latex-extra, libpcre3-dev
Standards-Version: 3.9.2
Homepage: http://i3wm.org/

View File

@ -739,8 +739,9 @@ con_id::
Compares the i3-internal container ID, which you can get via the IPC
interface. Handy for scripting.
Note that currently all criteria are compared case-insensitive and do not
support regular expressions. This is planned to change in the future.
The criteria +class+, +instance+, +title+ and +mark+ are actually regular
expressions (PCRE). See +pcresyntax(3)+ or +perldoc perlre+ for information on
how to use them.
=== Splitting containers

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

78
src/regex.c Normal file
View File

@ -0,0 +1,78 @@
/*
* 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 errorcode, offset;
struct regex *re = scalloc(sizeof(struct regex));
re->pattern = sstrdup(pattern);
/* We use PCRE_UCP so that \B, \b, \D, \d, \S, \s, \W, \w and some POSIX
* character classes play nicely with Unicode */
int options = PCRE_UCP | PCRE_UTF8;
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",
offset, error);
return NULL;
}
re->extra = pcre_study(re->regex, 0, &error);
/* If an error happened, we print the error message, but continue.
* Studying the regular expression leads to faster matching, but its not
* absolutely necessary. */
if (error) {
ELOG("PCRE regular expression studying failed: %s\n", 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;
/* We use strlen() because pcre_exec() expects the length of the input
* string in bytes */
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;
}
ELOG("PCRE error %d while trying to use regular expression \"%s\" on input \"%s\", see pcreapi(3)\n",
rc, regex->pattern, input);
return false;
}

View File

@ -34,9 +34,10 @@ my $win = $content->[0];
# first test that matches which should not match this window really do
# not match it
######################################################################
# TODO: use PCRE expressions
# TODO: specify more match types
cmd q|[class="*"] kill|;
# we can match on any (non-empty) class here since that window does not have
# WM_CLASS set
cmd q|[class=".*"] kill|;
cmd q|[con_id="99999"] kill|;
$content = get_ws_content($tmp);
@ -118,4 +119,62 @@ sleep 0.25;
$content = get_ws_content($tmp);
is(@{$content}, 1, 'one window still there');
######################################################################
# check that regular expressions work
######################################################################
$tmp = fresh_workspace;
$left = $x->root->create_child(
class => WINDOW_CLASS_INPUT_OUTPUT,
rect => [ 0, 0, 30, 30 ],
background_color => '#0000ff',
);
$left->_create;
set_wm_class($left->id, 'special7', 'special7');
$left->name('left');
$left->map;
sleep 0.25;
# two windows should be here
$content = get_ws_content($tmp);
ok(@{$content} == 1, 'window opened');
cmd '[class="^special[0-9]$"] kill';
sleep 0.25;
$content = get_ws_content($tmp);
is(@{$content}, 0, 'window killed');
######################################################################
# check that UTF-8 works when matching
######################################################################
$tmp = fresh_workspace;
$left = $x->root->create_child(
class => WINDOW_CLASS_INPUT_OUTPUT,
rect => [ 0, 0, 30, 30 ],
background_color => '#0000ff',
);
$left->_create;
set_wm_class($left->id, 'special7', 'special7');
$left->name('ä 3');
$left->map;
sleep 0.25;
# two windows should be here
$content = get_ws_content($tmp);
ok(@{$content} == 1, 'window opened');
cmd '[title="^\w [3]$"] kill';
sleep 0.25;
$content = get_ws_content($tmp);
is(@{$content}, 0, 'window killed');
done_testing;