added rule.c, a starting point for a rule engine

This commit is contained in:
Anselm R. Garbe 2006-03-10 11:50:52 +01:00
parent 34720c3c0e
commit 6da9bfce1e
3 changed files with 103 additions and 1 deletions

View File

@ -9,7 +9,7 @@ LDFLAGS += -L../../liblitz -llitz -L../../libixp -lixp -L../../libcext -lcext
# Solaris
# LDFLAGS += -lsocket
SRC = area.c bar.c fs.c frame.c wm.c kb.c client.c event.c mouse.c tag.c
SRC = area.c bar.c fs.c frame.c wm.c kb.c client.c event.c mouse.c rule.c tag.c
OBJ = ${SRC:.c=.o}
all: wmiiwm

99
cmd/wm/rule.c Normal file
View File

@ -0,0 +1,99 @@
/*
* (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
* See LICENSE file for license details.
*/
#include <string.h>
#include <stdlib.h>
#include "wm.h"
/*
* basic rule matching language
*
* /pattern/ -> tag [tag ...]
*/
typedef struct {
char pattern[256];
char tags[256];
} Rule;
enum {
IGNORE,
PATTERN,
TAGS
};
/* free the result */
static Rule *
parse(char *data, unsigned int *n)
{
Rule *rules;
unsigned int i;
int mode = IGNORE;
char *last = nil, *p, *pattern, *tags;
if(!data || !strlen(data))
return nil;
*n = 0;
for(last = p = data; *p; p++)
if(*p == '\n')
*n++;
rules = cext_emallocz(sizeof(Rule) * (*n));
i = 0;
for(p = data; *p; p++) {
switch(mode) {
case IGNORE:
if(*p == '/') {
mode = PATTERN;
pattern = rules[i].pattern;
}
else if(*p == '>' && last && *last == '-') {
mode = TAGS;
tags = rules[i].tags;
}
break;
case PATTERN:
if(*p == '/') {
mode = IGNORE;
*pattern = 0;
}
else {
*pattern = *p;
pattern++;
}
break;
case TAGS:
if(*p == ' ' && !strlen(tags))
break;
else if(*p == '\n') {
*tags = 0;
mode = IGNORE;
i++;
}
else {
*tags = *p;
tags++;
}
break;
}
last = p;
}
return rules;
}
char *
match_tags(char *ruledef, Client *c)
{
unsigned int n;
Rule *rules = parse(ruledef, &n);
free(rules);
return nil;
}

View File

@ -285,6 +285,9 @@ void grab_mouse(Window w, unsigned long mod, unsigned int button);
void ungrab_mouse(Window w, unsigned long mod, unsigned int button);
char *warp_mouse(char *arg);
/* rule.c */
char *match_tags(char *ruledef, Client *c);
/* tag.c */
Tag *alloc_tag(char *name);
char *destroy_tag(Tag *t);