wmii/rule.c

88 lines
1.5 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>
2006-10-20 12:07:55 +04:00
#include "wmii.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,
VALUE
2006-04-06 13:31:54 +04:00
};
void
2006-10-12 18:10:57 +04:00
trim(char *str, const char *chars) {
const char *cp;
char *sp, *sn;
for(cp = chars; *cp; cp++) {
for(sp = sn = str; *sn; sn++) {
if(*sn != *cp)
*(sp++) = *sn;
}
*sp = 0;
}
}
void
update_rules(Rule **rule, const char *data) {
int mode = IGNORE;
2006-06-08 12:54:19 +04:00
Rule *rul;
2007-02-05 05:02:05 +03:00
char *p, *r = nil, *v = nil, regex[256], value[256];
if(!data || !strlen(data))
return;
2006-06-08 12:54:19 +04:00
while((rul = *rule)) {
*rule = rul->next;
regfree(&rul->regex);
free(rul);
2006-03-15 12:35:38 +03:00
}
for(p = (char *)data; *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 = VALUE;
value[0] = 0;
v = value;
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 VALUE:
if(*p == '\n' || *p == 0) {
2006-10-12 18:10:57 +04:00
*rule = ixp_emallocz(sizeof(Rule));
*v = 0;
2006-10-12 18:10:57 +04:00
trim(value, " \t/");
2006-06-08 12:54:19 +04:00
if(!regcomp(&(*rule)->regex, regex, 0)) {
2006-10-12 18:10:57 +04:00
strncpy((*rule)->value, value, sizeof(rul->value));
2006-06-08 12:54:19 +04:00
rule = &(*rule)->next;
}
else free(*rule);
2006-03-10 16:17:32 +03:00
mode = IGNORE;
}
else {
*v = *p;
v++;
2006-03-10 16:17:32 +03:00
}
break;
}
}