wmii/cmd/wm/rule.c

167 lines
3.1 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)
*/
2006-04-06 13:31:54 +04:00
enum {
IGNORE,
REGEX,
TAGS
};
typedef struct {
regex_t regex;
2006-04-06 13:31:54 +04:00
char tags[256];
Bool is_valid;
} Rule;
2006-04-06 13:31:54 +04:00
VECTOR(RuleVector, Rule *);
2006-04-06 13:31:54 +04:00
static RuleVector rule;
2006-04-06 13:31:54 +04:00
static Vector *
2006-04-13 17:35:10 +04:00
vector_of_rules(RuleVector *rv)
2006-04-06 13:31:54 +04:00
{
return (Vector *) rv;
}
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));
n = cext_tokenize(toks, 16, buf, '+');
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 16:09:28 +04:00
unsigned int i;
int mode = IGNORE;
2006-04-06 13:31:54 +04:00
char *p, *r = nil, *t = nil, regex[256], tags[256];
if(!def.rules || !strlen(def.rules))
return;
2006-04-06 13:31:54 +04:00
while(rule.size) {
2006-04-06 14:19:20 +04:00
Rule *r = rule.data[0];
2006-04-06 13:31:54 +04:00
if(r->is_valid)
regfree(&r->regex);
2006-04-13 17:35:10 +04:00
cext_vdetach(vector_of_rules(&rule), r);
2006-04-06 13:31:54 +04:00
free(r);
2006-03-15 12:35:38 +03:00
}
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;
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;
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 17:35:10 +04: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 16:17:32 +03:00
mode = IGNORE;
}
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-04-13 16:09:28 +04:00
for(i = 0; i < client.size; i++)
apply_rules(client.data[i]);
update_views();
}
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-04-06 13:31:54 +04:00
unsigned int i;
2006-03-10 16:17:32 +03:00
regmatch_t tmpregm;
2006-04-06 13:31:54 +04:00
for(i = 0; i < rule.size; i++) {
Rule *r = rule.data[i];
if(r->is_valid && !regexec(&r->regex, prop, 1, &tmpregm, 0)) {
2006-04-06 13:31:54 +04:00
if(!strncmp(r->tags, "~", 2))
c->floating = True;
2006-04-13 16:38:29 +04:00
else if(!strlen(c->tags) || !strncmp(c->tags, "nil", 4)) {
2006-04-13 16:31:07 +04:00
if(!strncmp(r->tags, "!", 2)) {
2006-04-13 16:38:29 +04:00
if(view.size)
cext_strlcpy(c->tags, view.data[sel]->name, sizeof(c->tags));
else
2006-04-13 16:09:28 +04:00
cext_strlcpy(c->tags, "nil", sizeof(c->tags));
}
2006-04-13 16:09:28 +04:00
else
cext_strlcpy(c->tags, r->tags, sizeof(c->tags));
}
2006-03-10 16:17:32 +03:00
}
}
}
void
2006-04-12 12:44:07 +04:00
apply_rules(Client *c)
{
2006-03-10 20:39:25 +03:00
if(!def.rules)
goto Fallback;
match(c, c->classinst);
2006-04-13 16:58:22 +04:00
match(c, c->name);
Fallback:
2006-04-12 14:33:28 +04:00
if(!strlen(c->tags) || (!view.size && !strncmp(c->tags, "*", 2)))
cext_strlcpy(c->tags, "nil", sizeof(c->tags));
}