aewl

diff tag.c @ 191:56fee1dc9d53

switched to regexp matching for Rules
author arg@10ksloc.org
date Fri, 04 Aug 2006 14:40:32 +0200
parents e848966a1ac6
children 655e38416bb8
line diff
     1.1 --- a/tag.c	Fri Aug 04 13:42:40 2006 +0200
     1.2 +++ b/tag.c	Fri Aug 04 14:40:32 2006 +0200
     1.3 @@ -5,21 +5,31 @@
     1.4  #include "dwm.h"
     1.5  #include <regex.h>
     1.6  #include <stdio.h>
     1.7 +#include <stdlib.h>
     1.8  #include <string.h>
     1.9  #include <sys/types.h>
    1.10  #include <X11/Xutil.h>
    1.11  
    1.12 -/* static */
    1.13  
    1.14  typedef struct {
    1.15 -	const char *pattern;
    1.16 -	const unsigned int *tags;
    1.17 +	const char *clpattern;
    1.18 +	const char *tpattern;
    1.19  	Bool isfloat;
    1.20  } Rule;
    1.21  
    1.22 +typedef struct {
    1.23 +	regex_t *clregex;
    1.24 +	regex_t *tregex;
    1.25 +} RReg;
    1.26 +
    1.27 +/* static */
    1.28 +
    1.29  TAGS
    1.30  RULES
    1.31  
    1.32 +static RReg *rreg = NULL;
    1.33 +static unsigned int len = 0;
    1.34 +
    1.35  void (*arrange)(Arg *) = DEFMODE;
    1.36  
    1.37  /* extern */
    1.38 @@ -138,6 +148,35 @@
    1.39  }
    1.40  
    1.41  void
    1.42 +initrregs()
    1.43 +{
    1.44 +	unsigned int i;
    1.45 +	regex_t *reg;
    1.46 +
    1.47 +	if(rreg)
    1.48 +		return;
    1.49 +	len = sizeof(rule) / sizeof(rule[0]);
    1.50 +	rreg = emallocz(len * sizeof(RReg));
    1.51 +
    1.52 +	for(i = 0; i < len; i++) {
    1.53 +		if(rule[i].clpattern) {
    1.54 +			reg = emallocz(sizeof(regex_t));
    1.55 +			if(regcomp(reg, rule[i].clpattern, 0))
    1.56 +				free(reg);
    1.57 +			else
    1.58 +				rreg[i].clregex = reg;
    1.59 +		}
    1.60 +		if(rule[i].tpattern) {
    1.61 +			reg = emallocz(sizeof(regex_t));
    1.62 +			if(regcomp(reg, rule[i].tpattern, 0))
    1.63 +				free(reg);
    1.64 +			else
    1.65 +				rreg[i].tregex = reg;
    1.66 +		}
    1.67 +	}
    1.68 +}
    1.69 +
    1.70 +void
    1.71  replacetag(Arg *arg)
    1.72  {
    1.73  	int i;
    1.74 @@ -154,9 +193,7 @@
    1.75  settags(Client *c)
    1.76  {
    1.77  	char classinst[256];
    1.78 -	static unsigned int len = sizeof(rule) / sizeof(rule[0]);
    1.79 -	unsigned int i, j, n;
    1.80 -	regex_t regex;
    1.81 +	unsigned int i, j;
    1.82  	regmatch_t tmp;
    1.83  	Bool matched = False;
    1.84  	XClassHint ch;
    1.85 @@ -165,19 +202,16 @@
    1.86  		snprintf(classinst, sizeof(classinst), "%s:%s",
    1.87  				ch.res_class ? ch.res_class : "",
    1.88  				ch.res_name ? ch.res_name : "");
    1.89 -		for(i = 0; !matched && i < len; i++) {
    1.90 -			if(!regcomp(&regex, rule[i].pattern, 0)) {
    1.91 -				if(!regexec(&regex, classinst, 1, &tmp, 0)) {
    1.92 -					n = rule[i].tags ?
    1.93 -						sizeof(rule[i].tags) / sizeof(rule[i].tags[0]) : 0;
    1.94 -					matched = n != 0;
    1.95 -					for(j = 0; j < n; j++)
    1.96 -						c->tags[rule[i].tags[j]] = True;
    1.97 -					c->isfloat = rule[i].isfloat;
    1.98 +		for(i = 0; !matched && i < len; i++)
    1.99 +			if(rreg[i].clregex && !regexec(rreg[i].clregex, classinst, 1, &tmp, 0)) {
   1.100 +				c->isfloat = rule[i].isfloat;
   1.101 +				for(j = 0; rreg[i].tregex && j < ntags; j++) {
   1.102 +					if(!regexec(rreg[i].tregex, tags[j], 1, &tmp, 0)) {
   1.103 +						matched = True;
   1.104 +						c->tags[j] = True;
   1.105 +					}
   1.106  				}
   1.107 -				regfree(&regex);
   1.108  			}
   1.109 -		}
   1.110  		if(ch.res_class)
   1.111  			XFree(ch.res_class);
   1.112  		if(ch.res_name)