2006-03-10 13:50:52 +03:00
|
|
|
/*
|
|
|
|
* (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
|
|
|
|
* See LICENSE file for license details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2006-03-10 16:17:32 +03:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <regex.h>
|
2006-03-10 13:50:52 +03:00
|
|
|
#include "wm.h"
|
|
|
|
|
2006-03-23 18:12:06 +03:00
|
|
|
/*
|
2006-03-10 13:50:52 +03:00
|
|
|
* basic rule matching language
|
|
|
|
*
|
2006-03-23 18:12:06 +03:00
|
|
|
* /regex/ -> tag [tag ...]
|
2006-03-10 16:17:32 +03:00
|
|
|
*
|
|
|
|
* regex might contain POSIX regex syntax defined in regex(3)
|
2006-03-10 13:50:52 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct {
|
2006-03-26 19:49:31 +04:00
|
|
|
regex_t regex;
|
2006-03-15 12:35:38 +03:00
|
|
|
char tag[MAX_TAGS][MAX_TAGLEN];
|
2006-03-15 11:02:25 +03:00
|
|
|
unsigned int ntag;
|
2006-03-26 19:49:31 +04:00
|
|
|
Bool is_valid;
|
2006-03-10 13:50:52 +03:00
|
|
|
} Rule;
|
|
|
|
|
2006-03-26 20:21:12 +04:00
|
|
|
static Rule *rule = nil;
|
|
|
|
static unsigned int rulesz = 0;
|
|
|
|
static unsigned int nrule = 0;
|
|
|
|
|
2006-03-10 13:50:52 +03:00
|
|
|
enum {
|
|
|
|
IGNORE,
|
2006-03-15 12:35:38 +03:00
|
|
|
REGEX,
|
2006-03-10 13:50:52 +03:00
|
|
|
TAGS
|
|
|
|
};
|
|
|
|
|
2006-03-26 20:21:12 +04:00
|
|
|
void
|
|
|
|
update_rules()
|
2006-03-10 13:50:52 +03:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
int mode = IGNORE;
|
2006-04-03 00:53:56 +04:00
|
|
|
char *p, *r=nil, *t=nil, regex[256], tags[256];
|
2006-03-10 13:50:52 +03:00
|
|
|
|
2006-03-26 20:21:12 +04:00
|
|
|
if(!def.rules || !strlen(def.rules))
|
|
|
|
return;
|
2006-03-10 13:50:52 +03:00
|
|
|
|
2006-03-26 20:21:12 +04:00
|
|
|
for(i = 0; i < nrule; i++)
|
2006-03-26 19:49:31 +04:00
|
|
|
if(rule[i].is_valid) {
|
|
|
|
regfree(&rule[i].regex);
|
|
|
|
rule[i].is_valid = False;
|
|
|
|
}
|
|
|
|
|
2006-03-26 20:21:12 +04:00
|
|
|
nrule = 0;
|
|
|
|
for(p = def.rules; *p; p++)
|
|
|
|
if(*p == '\n')
|
|
|
|
nrule++;
|
|
|
|
|
|
|
|
if(nrule > rulesz) {
|
2006-03-15 12:35:38 +03:00
|
|
|
if(rule)
|
|
|
|
free(rule);
|
2006-03-26 20:21:12 +04:00
|
|
|
rule = cext_emallocz(sizeof(Rule) * nrule);
|
|
|
|
rulesz = nrule;
|
2006-03-15 12:35:38 +03:00
|
|
|
}
|
2006-03-10 13:50:52 +03:00
|
|
|
|
|
|
|
i = 0;
|
2006-03-26 20:21:12 +04:00
|
|
|
for(p = def.rules; *p; p++)
|
2006-03-10 13:50:52 +03:00
|
|
|
switch(mode) {
|
2006-03-10 16:17:32 +03:00
|
|
|
case IGNORE:
|
|
|
|
if(*p == '/') {
|
2006-03-15 12:35:38 +03:00
|
|
|
mode = REGEX;
|
|
|
|
r = regex;
|
2006-03-10 16:17:32 +03:00
|
|
|
}
|
|
|
|
else if(*p == '>') {
|
|
|
|
mode = TAGS;
|
2006-03-23 10:43:26 +03:00
|
|
|
tags[0] = 0;
|
2006-03-15 12:35:38 +03:00
|
|
|
t = tags;
|
2006-03-10 16:17:32 +03:00
|
|
|
}
|
|
|
|
break;
|
2006-03-15 12:35:38 +03:00
|
|
|
case REGEX:
|
2006-03-10 16:17:32 +03:00
|
|
|
if(*p == '/') {
|
|
|
|
mode = IGNORE;
|
2006-03-15 12:35:38 +03:00
|
|
|
*r = 0;
|
2006-03-26 20:21:12 +04:00
|
|
|
rule[i].is_valid = !regcomp(&rule[i].regex, regex, 0);
|
2006-04-03 00:53:56 +04:00
|
|
|
/* Is there a memory leak here if the rule is invalid? */
|
2006-03-10 16:17:32 +03:00
|
|
|
}
|
|
|
|
else {
|
2006-03-15 12:35:38 +03:00
|
|
|
*r = *p;
|
|
|
|
r++;
|
2006-03-10 16:17:32 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TAGS:
|
|
|
|
if(*p == '\n' || *(p + 1) == 0) {
|
2006-03-15 12:35:38 +03:00
|
|
|
*t = 0;
|
|
|
|
rule[i].ntag = str2tags(rule[i].tag, tags);
|
2006-03-10 16:17:32 +03:00
|
|
|
mode = IGNORE;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else {
|
2006-03-15 12:44:54 +03:00
|
|
|
if((*p == ' ' || *p == '\t') && (tags[0] == 0))
|
|
|
|
continue; /* skip prefixed whitespaces */
|
|
|
|
*t = *p;
|
2006-03-15 12:35:38 +03:00
|
|
|
t++;
|
2006-03-10 16:17:32 +03:00
|
|
|
}
|
|
|
|
break;
|
2006-03-10 13:50:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-10 16:17:32 +03:00
|
|
|
|
2006-03-15 11:02:25 +03:00
|
|
|
static void
|
2006-03-26 20:21:12 +04:00
|
|
|
match(Client *c, const char *prop)
|
2006-03-10 16:17:32 +03:00
|
|
|
{
|
2006-03-15 11:02:25 +03:00
|
|
|
unsigned int i, j;
|
2006-03-10 16:17:32 +03:00
|
|
|
regmatch_t tmpregm;
|
|
|
|
|
2006-03-15 11:02:25 +03:00
|
|
|
c->ntag = 0;
|
2006-03-26 20:21:12 +04:00
|
|
|
for(i = 0; i < nrule; i++) {
|
2006-03-26 19:49:31 +04:00
|
|
|
Rule *r = &rule[i];
|
2006-03-26 20:21:12 +04:00
|
|
|
if(r->is_valid && !regexec(&r->regex, prop, 1, &tmpregm, 0)) {
|
|
|
|
for(j = 0; c->ntag < MAX_TAGS && j < r->ntag; j++) {
|
2006-03-31 09:57:33 +04:00
|
|
|
if(!strncmp(r->tag[j], "~", 2))
|
|
|
|
c->floating = True;
|
|
|
|
else {
|
|
|
|
cext_strlcpy(c->tag[c->ntag], r->tag[j], sizeof(c->tag[c->ntag]));
|
|
|
|
c->ntag++;
|
|
|
|
}
|
2006-03-15 11:02:25 +03:00
|
|
|
}
|
2006-03-10 16:17:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-03-10 19:17:59 +03:00
|
|
|
match_tags(Client *c)
|
2006-03-10 13:50:52 +03:00
|
|
|
{
|
2006-03-10 20:39:25 +03:00
|
|
|
if(!def.rules)
|
|
|
|
return;
|
2006-03-26 20:21:12 +04:00
|
|
|
match(c, c->name);
|
|
|
|
match(c, c->classinst);
|
2006-03-10 13:50:52 +03:00
|
|
|
}
|