mirror of
https://github.com/0intro/wmii
synced 2024-11-26 15:49:37 +03:00
128 lines
2.1 KiB
C
128 lines
2.1 KiB
C
/*
|
|
* (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
|
|
* See LICENSE file for license details.
|
|
*/
|
|
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <sys/types.h>
|
|
#include <regex.h>
|
|
#include "wm.h"
|
|
|
|
/*
|
|
* basic rule matching language
|
|
*
|
|
* /regex/ -> tag [tag ...]
|
|
*
|
|
* regex might contain POSIX regex syntax defined in regex(3)
|
|
*/
|
|
|
|
enum {
|
|
IGNORE,
|
|
REGEX,
|
|
TAGS
|
|
};
|
|
|
|
typedef struct {
|
|
regex_t regex;
|
|
char tags[256];
|
|
Bool is_valid;
|
|
} Rule;
|
|
VECTOR(RuleVector, Rule *);
|
|
|
|
static RuleVector rule;
|
|
|
|
static Vector *
|
|
rule2vector(RuleVector *rv)
|
|
{
|
|
return (Vector *) rv;
|
|
}
|
|
|
|
void
|
|
update_rules()
|
|
{
|
|
int mode = IGNORE;
|
|
char *p, *r = nil, *t = nil, regex[256], tags[256];
|
|
|
|
if(!def.rules || !strlen(def.rules))
|
|
return;
|
|
|
|
while(rule.size) {
|
|
Rule *r = rule.data[0];
|
|
if(r->is_valid)
|
|
regfree(&r->regex);
|
|
cext_vdetach(rule2vector(&rule), r);
|
|
free(r);
|
|
}
|
|
|
|
for(p = def.rules; *p; p++)
|
|
switch(mode) {
|
|
case IGNORE:
|
|
if(*p == '/') {
|
|
mode = REGEX;
|
|
r = regex;
|
|
}
|
|
else if(*p == '>') {
|
|
mode = TAGS;
|
|
tags[0] = 0;
|
|
t = tags;
|
|
}
|
|
break;
|
|
case REGEX:
|
|
if(*p == '/') {
|
|
mode = IGNORE;
|
|
*r = 0;
|
|
}
|
|
else {
|
|
*r = *p;
|
|
r++;
|
|
}
|
|
break;
|
|
case TAGS:
|
|
if(*p == '\n' || *(p + 1) == 0) {
|
|
Rule *rul = cext_emallocz(sizeof(Rule));
|
|
*t = 0;
|
|
rul->is_valid = !regcomp(&rul->regex, regex, 0);
|
|
cext_strlcpy(rul->tags, tags, sizeof(rul->tags));
|
|
mode = IGNORE;
|
|
cext_vattach(rule2vector(&rule), rul);
|
|
}
|
|
else {
|
|
if((*p == ' ' || *p == '\t') && (tags[0] == 0))
|
|
continue; /* skip prefixed whitespaces */
|
|
*t = *p;
|
|
t++;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
static void
|
|
match(Client *c, const char *prop)
|
|
{
|
|
unsigned int i;
|
|
regmatch_t tmpregm;
|
|
|
|
for(i = 0; i < rule.size; i++) {
|
|
Rule *r = rule.data[i];
|
|
if(r->is_valid && !regexec(&r->regex, prop, 1, &tmpregm, 0)) {
|
|
if(!strncmp(r->tags, "~", 2))
|
|
c->floating = True;
|
|
else {
|
|
cext_strlcpy(c->tags, r->tags, sizeof(c->tags));
|
|
str2tagvector(&c->tag, c->tags);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void
|
|
match_tags(Client *c)
|
|
{
|
|
if(!def.rules)
|
|
return;
|
|
match(c, c->name);
|
|
match(c, c->classinst);
|
|
}
|