Implement support for PCRE regular expressions for all criteria (for_window, commands, assignments)
This commit is contained in:
@ -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:
|
||||
*
|
||||
|
@ -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);
|
||||
}
|
||||
;
|
||||
|
||||
|
28
src/match.c
28
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");
|
||||
|
63
src/regex.c
Normal file
63
src/regex.c
Normal 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;
|
||||
}
|
Reference in New Issue
Block a user