wmii/cmd/wm/rule.c

183 lines
3.4 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 14:17:32 +01:00
#include <sys/types.h>
#include <regex.h>
#include "wm.h"
/*
* basic rule matching language
*
* /regex/ -> tag [tag ...]
2006-03-10 14:17:32 +01:00
*
* regex might contain POSIX regex syntax defined in regex(3)
*/
2006-04-06 11:31:54 +02:00
enum {
IGNORE,
REGEX,
TAGS
};
typedef struct {
regex_t regex;
2006-04-06 11:31:54 +02:00
char tags[256];
Bool is_valid;
} Rule;
2006-04-06 11:31:54 +02:00
VECTOR(RuleVector, Rule *);
VECTOR(PropVector, char *);
2006-04-06 11:31:54 +02:00
static RuleVector rule;
2006-04-06 11:31:54 +02:00
static Vector *
2006-04-13 15:35:10 +02:00
vector_of_rules(RuleVector *rv)
2006-04-06 11:31:54 +02:00
{
return (Vector *) rv;
}
static Vector *
vector_of_props(PropVector *pv)
{
return (Vector *) pv;
}
Bool
permit_tags(const char *tags)
{
static char *exclude[]
= { "bar", "client", "ctl", "def", "event", "view" };
char buf[256];
char *toks[16];
unsigned int i, j, n;
cext_strlcpy(buf, tags, sizeof(buf));
if(!(n = cext_tokenize(toks, 16, buf, '+')))
return False;
for(i = 0; i < (sizeof(exclude)/sizeof(exclude[0])); i++)
for(j = 0; j < n; j++) {
if(!strncmp(exclude[i], toks[j], strlen(toks[j])) &&
!strncmp(exclude[i], toks[j], strlen(exclude[i])))
return False;
}
return True;
}
void
update_rules()
{
2006-04-13 14:09:28 +02:00
unsigned int i;
int mode = IGNORE;
2006-04-06 11:31:54 +02:00
char *p, *r = nil, *t = nil, regex[256], tags[256];
if(!def.rules || !strlen(def.rules))
return;
2006-04-06 11:31:54 +02:00
while(rule.size) {
2006-04-06 12:19:20 +02:00
Rule *r = rule.data[0];
2006-04-06 11:31:54 +02:00
if(r->is_valid)
regfree(&r->regex);
2006-04-13 15:35:10 +02:00
cext_vdetach(vector_of_rules(&rule), r);
2006-04-06 11:31:54 +02:00
free(r);
2006-03-15 10:35:38 +01:00
}
for(p = def.rules; *p; p++)
switch(mode) {
2006-03-10 14:17:32 +01:00
case IGNORE:
if(*p == '/') {
2006-03-15 10:35:38 +01:00
mode = REGEX;
r = regex;
2006-03-10 14:17:32 +01:00
}
else if(*p == '>') {
mode = TAGS;
tags[0] = 0;
2006-03-15 10:35:38 +01:00
t = tags;
2006-03-10 14:17:32 +01:00
}
break;
2006-03-15 10:35:38 +01:00
case REGEX:
2006-03-10 14:17:32 +01:00
if(*p == '/') {
mode = IGNORE;
2006-03-15 10:35:38 +01:00
*r = 0;
2006-03-10 14:17:32 +01:00
}
else {
2006-03-15 10:35:38 +01:00
*r = *p;
r++;
2006-03-10 14:17:32 +01:00
}
break;
case TAGS:
if(*p == '\n' || *(p + 1) == 0) {
2006-03-15 10:35:38 +01:00
*t = 0;
cext_trim(tags, " \t");
if(permit_tags(tags)) {
Rule *rul = cext_emallocz(sizeof(Rule));
rul->is_valid = !regcomp(&rul->regex, regex, 0);
cext_strlcpy(rul->tags, tags, sizeof(rul->tags));
2006-04-13 15:35:10 +02:00
cext_vattach(vector_of_rules(&rule), rul);
}
else
fprintf(stderr, "wmiiwm: ignoring rule with tags '%s', restricted tag name\n",
tags);
2006-03-10 14:17:32 +01:00
mode = IGNORE;
}
else {
2006-03-15 10:44:54 +01:00
*t = *p;
2006-03-15 10:35:38 +01:00
t++;
2006-03-10 14:17:32 +01:00
}
break;
}
2006-04-13 14:09:28 +02:00
for(i = 0; i < client.size; i++)
apply_rules(client.data[i]);
update_views();
}
2006-03-15 09:02:25 +01:00
static void
match(Client *c, PropVector prop)
2006-03-10 14:17:32 +01:00
{
unsigned int i,j;
2006-03-10 14:17:32 +01:00
regmatch_t tmpregm;
2006-04-06 11:31:54 +02:00
for(i = 0; i < rule.size; i++) {
Rule *r = rule.data[i];
for(j=0; j < prop.size; j++)
if(r->is_valid && !regexec(&r->regex, prop.data[j], 1, &tmpregm, 0)) {
if(!strncmp(r->tags, "~", 2))
c->floating = True;
else if(!strlen(c->tags) || !strncmp(c->tags, "nil", 4)) {
if(!strncmp(r->tags, "!", 2)) {
if(view.size)
cext_strlcpy(c->tags, view.data[sel]->name, sizeof(c->tags));
else
cext_strlcpy(c->tags, "nil", sizeof(c->tags));
}
else
cext_strlcpy(c->tags, r->tags, sizeof(c->tags));
}
}
2006-03-10 14:17:32 +01:00
}
}
void
2006-04-12 10:44:07 +02:00
apply_rules(Client *c)
{
PropVector prop = {0};
2006-03-10 18:39:25 +01:00
if(!def.rules)
goto Fallback;
cext_vattach(vector_of_props(&prop), c->classinst);
cext_vattach(vector_of_props(&prop), c->name);
match(c, prop);
while(prop.size)
cext_vdetach(vector_of_props(&prop), prop.data[0]);
Fallback:
if(!strlen(c->tags))
cext_strlcpy(c->tags, "nil", sizeof(c->tags));
}