summaryrefslogtreecommitdiffstats
path: root/sway/criteria.c
diff options
context:
space:
mode:
authorLibravatar Scott Anderson <ascent12@hotmail.com>2017-04-07 00:16:06 +1200
committerLibravatar Scott Anderson <ascent12@hotmail.com>2017-04-07 00:16:06 +1200
commitfe54a6725ede65020dc8fb71e8d453c1c51dc3fe (patch)
treeddec77f852459885a514566b1eaaf212ec1e42af /sway/criteria.c
parentMerge pull request #1149 from 4e554c4c/criteria (diff)
downloadsway-fe54a6725ede65020dc8fb71e8d453c1c51dc3fe.tar.gz
sway-fe54a6725ede65020dc8fb71e8d453c1c51dc3fe.tar.zst
sway-fe54a6725ede65020dc8fb71e8d453c1c51dc3fe.zip
Changed regular expressions to use PCRE for i3 compatibility
Diffstat (limited to 'sway/criteria.c')
-rw-r--r--sway/criteria.c40
1 files changed, 17 insertions, 23 deletions
diff --git a/sway/criteria.c b/sway/criteria.c
index bd99461d..706da04f 100644
--- a/sway/criteria.c
+++ b/sway/criteria.c
@@ -2,7 +2,7 @@
2#include <stdlib.h> 2#include <stdlib.h>
3#include <stdio.h> 3#include <stdio.h>
4#include <stdbool.h> 4#include <stdbool.h>
5#include <regex.h> 5#include <pcre.h>
6#include "sway/criteria.h" 6#include "sway/criteria.h"
7#include "sway/container.h" 7#include "sway/container.h"
8#include "sway/config.h" 8#include "sway/config.h"
@@ -42,18 +42,13 @@ static const char * const criteria_strings[] = {
42 */ 42 */
43struct crit_token { 43struct crit_token {
44 enum criteria_type type; 44 enum criteria_type type;
45 regex_t *regex; 45 pcre *regex;
46 char *raw; 46 char *raw;
47}; 47};
48 48
49static void free_crit_token(struct crit_token *crit) { 49static void free_crit_token(struct crit_token *crit) {
50 if (crit->regex) { 50 pcre_free(crit->regex);
51 regfree(crit->regex); 51 free(crit->raw);
52 free(crit->regex);
53 }
54 if (crit->raw) {
55 free(crit->raw);
56 }
57 free(crit); 52 free(crit);
58} 53}
59 54
@@ -190,18 +185,17 @@ static char *parse_criteria_name(enum criteria_type *type, char *name) {
190} 185}
191 186
192// Returns error string on failure or NULL otherwise. 187// Returns error string on failure or NULL otherwise.
193static char *generate_regex(regex_t **regex, char *value) { 188static char *generate_regex(pcre **regex, char *value) {
194 *regex = calloc(1, sizeof(regex_t)); 189 const char *reg_err;
195 int err = regcomp(*regex, value, REG_NOSUB); 190 int offset;
196 if (err != 0) { 191
197 char *reg_err = malloc(64); 192 *regex = pcre_compile(value, PCRE_UTF8 | PCRE_UCP, &reg_err, &offset, NULL);
198 regerror(err, *regex, reg_err, 64);
199 193
194 if (!*regex) {
200 const char *fmt = "Regex compilation (for '%s') failed: %s"; 195 const char *fmt = "Regex compilation (for '%s') failed: %s";
201 int len = strlen(fmt) + strlen(value) + strlen(reg_err) - 3; 196 int len = strlen(fmt) + strlen(value) + strlen(reg_err) - 3;
202 char *error = malloc(len); 197 char *error = malloc(len);
203 snprintf(error, len, fmt, value, reg_err); 198 snprintf(error, len, fmt, value, reg_err);
204 free(reg_err);
205 return error; 199 return error;
206 } 200 }
207 return NULL; 201 return NULL;
@@ -245,8 +239,8 @@ ect_cleanup:
245 return error; 239 return error;
246} 240}
247 241
248static int regex_cmp(const char *item, const regex_t *regex) { 242static int regex_cmp(const char *item, const pcre *regex) {
249 return regexec(regex, item, 0, NULL, 0); 243 return pcre_exec(regex, NULL, item, strlen(item), 0, 0, NULL, 0);
250} 244}
251 245
252// test a single view if it matches list of criteria tokens (all of them). 246// test a single view if it matches list of criteria tokens (all of them).
@@ -266,7 +260,7 @@ static bool criteria_test(swayc_t *cont, list_t *tokens) {
266 if (focused->class && strcmp(cont->class, focused->class) == 0) { 260 if (focused->class && strcmp(cont->class, focused->class) == 0) {
267 matches++; 261 matches++;
268 } 262 }
269 } else if (crit->regex && regexec(crit->regex, cont->class, 0, NULL, 0) == 0) { 263 } else if (crit->regex && regex_cmp(cont->class, crit->regex) == 0) {
270 matches++; 264 matches++;
271 } 265 }
272 break; 266 break;
@@ -281,7 +275,7 @@ static bool criteria_test(swayc_t *cont, list_t *tokens) {
281 case CRIT_ID: 275 case CRIT_ID:
282 if (!cont->app_id) { 276 if (!cont->app_id) {
283 // ignore 277 // ignore
284 } else if (crit->regex && regexec(crit->regex, cont->app_id, 0, NULL, 0) == 0) { 278 } else if (crit->regex && regex_cmp(cont->app_id, crit->regex) == 0) {
285 matches++; 279 matches++;
286 } 280 }
287 break; 281 break;
@@ -293,7 +287,7 @@ static bool criteria_test(swayc_t *cont, list_t *tokens) {
293 if (focused->instance && strcmp(cont->instance, focused->instance) == 0) { 287 if (focused->instance && strcmp(cont->instance, focused->instance) == 0) {
294 matches++; 288 matches++;
295 } 289 }
296 } else if (crit->regex && regexec(crit->regex, cont->instance, 0, NULL, 0) == 0) { 290 } else if (crit->regex && regex_cmp(cont->instance, crit->regex) == 0) {
297 matches++; 291 matches++;
298 } 292 }
299 break; 293 break;
@@ -305,7 +299,7 @@ static bool criteria_test(swayc_t *cont, list_t *tokens) {
305 if (focused->name && strcmp(cont->name, focused->name) == 0) { 299 if (focused->name && strcmp(cont->name, focused->name) == 0) {
306 matches++; 300 matches++;
307 } 301 }
308 } else if (crit->regex && regexec(crit->regex, cont->name, 0, NULL, 0) == 0) { 302 } else if (crit->regex && regex_cmp(cont->name, crit->regex) == 0) {
309 matches++; 303 matches++;
310 } 304 }
311 break; 305 break;
@@ -325,7 +319,7 @@ static bool criteria_test(swayc_t *cont, list_t *tokens) {
325 if (focused_ws->name && strcmp(cont_ws->name, focused_ws->name) == 0) { 319 if (focused_ws->name && strcmp(cont_ws->name, focused_ws->name) == 0) {
326 matches++; 320 matches++;
327 } 321 }
328 } else if (crit->regex && regexec(crit->regex, cont_ws->name, 0, NULL, 0) == 0) { 322 } else if (crit->regex && regex_cmp(cont_ws->name, crit->regex) == 0) {
329 matches++; 323 matches++;
330 } 324 }
331 break; 325 break;