2006-03-10 13:50:52 +03:00
|
|
|
/*
|
|
|
|
* (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"
|
2006-03-10 13:50:52 +03:00
|
|
|
|
2006-05-30 18:32:28 +04:00
|
|
|
/* 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,
|
2006-05-31 11:51:40 +04:00
|
|
|
VALUE
|
2006-04-06 13:31:54 +04:00
|
|
|
};
|
|
|
|
|
2006-03-26 20:21:12 +04:00
|
|
|
void
|
2006-10-12 18:10:57 +04:00
|
|
|
trim(char *str, const char *chars) {
|
|
|
|
const char *cp;
|
|
|
|
char *sp, *sn;
|
2007-02-24 07:09:58 +03:00
|
|
|
|
2006-10-12 18:10:57 +04:00
|
|
|
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) {
|
2006-03-10 13:50:52 +03:00
|
|
|
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];
|
2006-03-10 13:50:52 +03:00
|
|
|
|
2006-05-30 20:28:05 +04:00
|
|
|
if(!data || !strlen(data))
|
2006-03-26 20:21:12 +04:00
|
|
|
return;
|
2006-06-08 12:54:19 +04:00
|
|
|
while((rul = *rule)) {
|
|
|
|
*rule = rul->next;
|
2006-05-30 20:28:05 +04:00
|
|
|
regfree(&rul->regex);
|
|
|
|
free(rul);
|
2006-03-15 12:35:38 +03:00
|
|
|
}
|
2006-05-30 21:13:33 +04:00
|
|
|
for(p = (char *)data; *p; p++)
|
2006-03-10 13:50:52 +03:00
|
|
|
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 == '>') {
|
2006-05-31 11:51:40 +04:00
|
|
|
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;
|
2006-05-31 11:51:40 +04:00
|
|
|
case VALUE:
|
|
|
|
if(*p == '\n' || *p == 0) {
|
2007-02-15 07:20:47 +03:00
|
|
|
*rule = emallocz(sizeof(Rule));
|
2006-05-30 18:32:28 +04:00
|
|
|
*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;
|
2006-04-13 12:06:07 +04:00
|
|
|
}
|
2006-06-24 13:15:41 +04:00
|
|
|
else free(*rule);
|
2006-03-10 16:17:32 +03:00
|
|
|
mode = IGNORE;
|
|
|
|
}
|
|
|
|
else {
|
2006-05-30 18:32:28 +04:00
|
|
|
*v = *p;
|
|
|
|
v++;
|
2006-03-10 16:17:32 +03:00
|
|
|
}
|
|
|
|
break;
|
2006-03-10 13:50:52 +03:00
|
|
|
}
|
|
|
|
}
|