switched to regexp matching for Rules
e21d93b7
6 file(s) · +65 −31
| 52 | 52 | }; |
|
| 53 | 53 | ||
| 54 | 54 | #define RULES \ |
|
| 55 | - | const unsigned int two[] = { 2 }; \ |
|
| 56 | 55 | static Rule rule[] = { \ |
|
| 57 | - | /* class:instance tags isfloat */ \ |
|
| 58 | - | { "Firefox.*", two, False }, \ |
|
| 59 | - | { "Gimp.*", NULL, True}, \ |
|
| 60 | - | { "MPlayer.*", NULL, True}, \ |
|
| 61 | - | { "Acroread.*", NULL, True}, \ |
|
| 56 | + | /* class:instance regex tags regex isfloat */ \ |
|
| 57 | + | { "Firefox.*", "net", False }, \ |
|
| 58 | + | { "Gimp.*", NULL, True}, \ |
|
| 59 | + | { "MPlayer.*", NULL, True}, \ |
|
| 60 | + | { "Acroread.*", NULL, True}, \ |
|
| 62 | 61 | }; |
| 47 | 47 | }; |
|
| 48 | 48 | ||
| 49 | 49 | #define RULES \ |
|
| 50 | - | const unsigned int two[] = { 2 }; \ |
|
| 51 | 50 | static Rule rule[] = { \ |
|
| 52 | - | /* class:instance tags isfloat */ \ |
|
| 53 | - | { "Firefox.*", two, False }, \ |
|
| 54 | - | { "Gimp.*", NULL, True}, \ |
|
| 51 | + | /* class:instance regex tags regex isfloat */ \ |
|
| 52 | + | { "Firefox.*", "2", False }, \ |
|
| 53 | + | { "Gimp.*", NULL, True}, \ |
|
| 55 | 54 | }; |
| 15 | 15 | LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 |
|
| 16 | 16 | ||
| 17 | 17 | # flags |
|
| 18 | - | CFLAGS = -O3 ${INCS} -DVERSION=\"${VERSION}\" |
|
| 19 | - | LDFLAGS = ${LIBS} |
|
| 20 | - | #CFLAGS = -g -Wall -O2 ${INCS} -DVERSION=\"${VERSION}\" |
|
| 21 | - | #LDFLAGS = -g ${LIBS} |
|
| 18 | + | #CFLAGS = -O3 ${INCS} -DVERSION=\"${VERSION}\" |
|
| 19 | + | #LDFLAGS = ${LIBS} |
|
| 20 | + | CFLAGS = -g -Wall -O2 ${INCS} -DVERSION=\"${VERSION}\" |
|
| 21 | + | LDFLAGS = -g ${LIBS} |
|
| 22 | 22 | ||
| 23 | 23 | # compiler |
|
| 24 | 24 | CC = cc |
| 121 | 121 | extern void appendtag(Arg *arg); |
|
| 122 | 122 | extern void dofloat(Arg *arg); |
|
| 123 | 123 | extern void dotile(Arg *arg); |
|
| 124 | + | extern void initrregs(); |
|
| 124 | 125 | extern Client *getnext(Client *c); |
|
| 125 | 126 | extern Client *getprev(Client *c); |
|
| 126 | 127 | extern void replacetag(Arg *arg); |
| 210 | 210 | cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur); |
|
| 211 | 211 | ||
| 212 | 212 | grabkeys(); |
|
| 213 | + | initrregs(); |
|
| 213 | 214 | ||
| 214 | 215 | for(ntags = 0; tags[ntags]; ntags++); |
|
| 215 | 216 |
| 5 | 5 | #include "dwm.h" |
|
| 6 | 6 | #include <regex.h> |
|
| 7 | 7 | #include <stdio.h> |
|
| 8 | + | #include <stdlib.h> |
|
| 8 | 9 | #include <string.h> |
|
| 9 | 10 | #include <sys/types.h> |
|
| 10 | 11 | #include <X11/Xutil.h> |
|
| 11 | 12 | ||
| 12 | - | /* static */ |
|
| 13 | 13 | ||
| 14 | 14 | typedef struct { |
|
| 15 | - | const char *pattern; |
|
| 16 | - | const unsigned int *tags; |
|
| 15 | + | const char *clpattern; |
|
| 16 | + | const char *tpattern; |
|
| 17 | 17 | Bool isfloat; |
|
| 18 | 18 | } Rule; |
|
| 19 | 19 | ||
| 20 | + | typedef struct { |
|
| 21 | + | regex_t *clregex; |
|
| 22 | + | regex_t *tregex; |
|
| 23 | + | } RReg; |
|
| 24 | + | ||
| 25 | + | /* static */ |
|
| 26 | + | ||
| 20 | 27 | TAGS |
|
| 21 | 28 | RULES |
|
| 29 | + | ||
| 30 | + | static RReg *rreg = NULL; |
|
| 31 | + | static unsigned int len = 0; |
|
| 22 | 32 | ||
| 23 | 33 | void (*arrange)(Arg *) = DEFMODE; |
|
| 24 | 34 | ||
| 138 | 148 | } |
|
| 139 | 149 | ||
| 140 | 150 | void |
|
| 151 | + | initrregs() |
|
| 152 | + | { |
|
| 153 | + | unsigned int i; |
|
| 154 | + | regex_t *reg; |
|
| 155 | + | ||
| 156 | + | if(rreg) |
|
| 157 | + | return; |
|
| 158 | + | len = sizeof(rule) / sizeof(rule[0]); |
|
| 159 | + | rreg = emallocz(len * sizeof(RReg)); |
|
| 160 | + | ||
| 161 | + | for(i = 0; i < len; i++) { |
|
| 162 | + | if(rule[i].clpattern) { |
|
| 163 | + | reg = emallocz(sizeof(regex_t)); |
|
| 164 | + | if(regcomp(reg, rule[i].clpattern, 0)) |
|
| 165 | + | free(reg); |
|
| 166 | + | else |
|
| 167 | + | rreg[i].clregex = reg; |
|
| 168 | + | } |
|
| 169 | + | if(rule[i].tpattern) { |
|
| 170 | + | reg = emallocz(sizeof(regex_t)); |
|
| 171 | + | if(regcomp(reg, rule[i].tpattern, 0)) |
|
| 172 | + | free(reg); |
|
| 173 | + | else |
|
| 174 | + | rreg[i].tregex = reg; |
|
| 175 | + | } |
|
| 176 | + | } |
|
| 177 | + | } |
|
| 178 | + | ||
| 179 | + | void |
|
| 141 | 180 | replacetag(Arg *arg) |
|
| 142 | 181 | { |
|
| 143 | 182 | int i; |
|
| 154 | 193 | settags(Client *c) |
|
| 155 | 194 | { |
|
| 156 | 195 | char classinst[256]; |
|
| 157 | - | static unsigned int len = sizeof(rule) / sizeof(rule[0]); |
|
| 158 | - | unsigned int i, j, n; |
|
| 159 | - | regex_t regex; |
|
| 196 | + | unsigned int i, j; |
|
| 160 | 197 | regmatch_t tmp; |
|
| 161 | 198 | Bool matched = False; |
|
| 162 | 199 | XClassHint ch; |
|
| 165 | 202 | snprintf(classinst, sizeof(classinst), "%s:%s", |
|
| 166 | 203 | ch.res_class ? ch.res_class : "", |
|
| 167 | 204 | ch.res_name ? ch.res_name : ""); |
|
| 168 | - | for(i = 0; !matched && i < len; i++) { |
|
| 169 | - | if(!regcomp(®ex, rule[i].pattern, 0)) { |
|
| 170 | - | if(!regexec(®ex, classinst, 1, &tmp, 0)) { |
|
| 171 | - | n = rule[i].tags ? |
|
| 172 | - | sizeof(rule[i].tags) / sizeof(rule[i].tags[0]) : 0; |
|
| 173 | - | matched = n != 0; |
|
| 174 | - | for(j = 0; j < n; j++) |
|
| 175 | - | c->tags[rule[i].tags[j]] = True; |
|
| 176 | - | c->isfloat = rule[i].isfloat; |
|
| 205 | + | for(i = 0; !matched && i < len; i++) |
|
| 206 | + | if(rreg[i].clregex && !regexec(rreg[i].clregex, classinst, 1, &tmp, 0)) { |
|
| 207 | + | c->isfloat = rule[i].isfloat; |
|
| 208 | + | for(j = 0; rreg[i].tregex && j < ntags; j++) { |
|
| 209 | + | if(!regexec(rreg[i].tregex, tags[j], 1, &tmp, 0)) { |
|
| 210 | + | matched = True; |
|
| 211 | + | c->tags[j] = True; |
|
| 212 | + | } |
|
| 177 | 213 | } |
|
| 178 | - | regfree(®ex); |
|
| 179 | 214 | } |
|
| 180 | - | } |
|
| 181 | 215 | if(ch.res_class) |
|
| 182 | 216 | XFree(ch.res_class); |
|
| 183 | 217 | if(ch.res_name) |
|