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-03-10 13:50:52 +03:00
|
|
|
#include "wm.h"
|
|
|
|
|
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,
|
|
|
|
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;
|
|
|
|
}
|
2006-03-10 13:50:52 +03:00
|
|
|
|
2006-03-26 20:21:12 +04:00
|
|
|
void
|
2006-05-30 20:28:05 +04:00
|
|
|
update_rules(RuleVector *rule, const char *data)
|
2006-03-10 13:50:52 +03:00
|
|
|
{
|
|
|
|
int mode = IGNORE;
|
2006-05-30 18:32:28 +04:00
|
|
|
char *p, *r = nil, *v = nil, regex[256], values[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-03-10 13:50:52 +03:00
|
|
|
|
2006-05-30 20:28:05 +04:00
|
|
|
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
|
|
|
}
|
2006-03-10 13:50:52 +03:00
|
|
|
|
2006-03-26 20:21:12 +04:00
|
|
|
for(p = def.rules; *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 == '>') {
|
|
|
|
mode = TAGS;
|
2006-05-30 18:32:28 +04:00
|
|
|
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) {
|
2006-05-30 18:32:28 +04:00
|
|
|
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));
|
2006-05-30 20:28:05 +04:00
|
|
|
cext_vattach(vector_of_rules(rule), rul);
|
2006-04-13 12:06:07 +04:00
|
|
|
}
|
|
|
|
else
|
2006-05-30 18:32:28 +04:00
|
|
|
free(rul);
|
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
|
|
|
}
|
|
|
|
}
|