wmii/cmd/wm/rule.c

84 lines
1.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 16:17:32 +03:00
#include <sys/types.h>
#include "wm.h"
/* basic rule matching language /regex/ -> value
* regex might contain POSIX regex syntax defined in regex(3) */
2006-04-06 13:31:54 +04:00
enum {
IGNORE,
REGEX,
TAGS
};
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;
}
void
update_rules(RuleVector *rule, const char *data)
{
int mode = IGNORE;
char *p, *r = nil, *v = nil, regex[256], values[256];
if(!data || !strlen(data))
return;
while(rule->size) {
Rule *rul = rule->data[0];
regfree(&rul->regex);
cext_vdetach(vector_of_rules(rule), rul);
free(rul);
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;
values[0] = 0;
v = values;
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) {
Rule *rul = cext_emallocz(sizeof(Rule));
*v = 0;
cext_trim(values, " \t/");
if(!regcomp(&rul->regex, regex, 0)) {
cext_strlcpy(rul->values, values, sizeof(rul->values));
cext_vattach(vector_of_rules(rule), rul);
}
else
free(rul);
2006-03-10 16:17:32 +03:00
mode = IGNORE;
}
else {
*v = *p;
v++;
2006-03-10 16:17:32 +03:00
}
break;
}
}