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

This commit is contained in:
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

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