wmii/cmd/wm/rule.c

139 lines
2.3 KiB
C
Raw Normal View History

/*
* (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>
#include "wm.h"
/*
* basic rule matching language
*
* /regex/ -> tag [tag ...]
2006-03-10 16:17:32 +03:00
*
* regex might contain POSIX regex syntax defined in regex(3)
*/
typedef struct {
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;
Bool is_valid;
} Rule;
static Rule *rule = nil;
static unsigned int rulesz = 0;
static unsigned int nrule = 0;
enum {
IGNORE,
2006-03-15 12:35:38 +03:00
REGEX,
TAGS
};
void
update_rules()
{
unsigned int i;
int mode = IGNORE;
char *p, *r=nil, *t=nil, regex[256], tags[256];
if(!def.rules || !strlen(def.rules))
return;
for(i = 0; i < nrule; i++)
if(rule[i].is_valid) {
regfree(&rule[i].regex);
rule[i].is_valid = False;
}
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);
rule = cext_emallocz(sizeof(Rule) * nrule);
rulesz = nrule;
2006-03-15 12:35:38 +03:00
}
i = 0;
for(p = def.rules; *p; p++)
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;
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;
rule[i].is_valid = !regcomp(&rule[i].regex, regex, 0);
/* 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 16:17:32 +03:00
2006-03-15 11:02:25 +03:00
static void
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;
for(i = 0; i < nrule; i++) {
Rule *r = &rule[i];
if(r->is_valid && !regexec(&r->regex, prop, 1, &tmpregm, 0)) {
for(j = 0; c->ntag < MAX_TAGS && j < r->ntag; j++) {
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
match_tags(Client *c)
{
2006-03-10 20:39:25 +03:00
if(!def.rules)
return;
match(c, c->name);
match(c, c->classinst);
}