diff options
Diffstat (limited to 'sway/criteria.c')
-rw-r--r-- | sway/criteria.c | 70 |
1 files changed, 38 insertions, 32 deletions
diff --git a/sway/criteria.c b/sway/criteria.c index 409160c5..97cf667e 100644 --- a/sway/criteria.c +++ b/sway/criteria.c | |||
@@ -3,7 +3,8 @@ | |||
3 | #include <stdio.h> | 3 | #include <stdio.h> |
4 | #include <stdbool.h> | 4 | #include <stdbool.h> |
5 | #include <strings.h> | 5 | #include <strings.h> |
6 | #include <pcre.h> | 6 | #define PCRE2_CODE_UNIT_WIDTH 8 |
7 | #include <pcre2.h> | ||
7 | #include "sway/criteria.h" | 8 | #include "sway/criteria.h" |
8 | #include "sway/tree/container.h" | 9 | #include "sway/tree/container.h" |
9 | #include "sway/config.h" | 10 | #include "sway/config.h" |
@@ -40,17 +41,19 @@ bool criteria_is_empty(struct criteria *criteria) { | |||
40 | char *error = NULL; | 41 | char *error = NULL; |
41 | 42 | ||
42 | // Returns error string on failure or NULL otherwise. | 43 | // Returns error string on failure or NULL otherwise. |
43 | static bool generate_regex(pcre **regex, char *value) { | 44 | static bool generate_regex(pcre2_code **regex, char *value) { |
44 | const char *reg_err; | 45 | int errorcode; |
45 | int offset; | 46 | PCRE2_SIZE offset; |
46 | |||
47 | *regex = pcre_compile(value, PCRE_UTF8 | PCRE_UCP, ®_err, &offset, NULL); | ||
48 | 47 | ||
48 | *regex = pcre2_compile((PCRE2_SPTR)value, PCRE2_ZERO_TERMINATED, PCRE2_UTF | PCRE2_UCP, &errorcode, &offset, NULL); | ||
49 | if (!*regex) { | 49 | if (!*regex) { |
50 | PCRE2_UCHAR buffer[256]; | ||
51 | pcre2_get_error_message(errorcode, buffer, sizeof(buffer)); | ||
52 | |||
50 | const char *fmt = "Regex compilation for '%s' failed: %s"; | 53 | const char *fmt = "Regex compilation for '%s' failed: %s"; |
51 | int len = strlen(fmt) + strlen(value) + strlen(reg_err) - 3; | 54 | int len = strlen(fmt) + strlen(value) + strlen((char*) buffer) - 3; |
52 | error = malloc(len); | 55 | error = malloc(len); |
53 | snprintf(error, len, fmt, value, reg_err); | 56 | snprintf(error, len, fmt, value, buffer); |
54 | return false; | 57 | return false; |
55 | } | 58 | } |
56 | 59 | ||
@@ -66,7 +69,7 @@ static bool pattern_create(struct pattern **pattern, char *value) { | |||
66 | if (strcmp(value, "__focused__") == 0) { | 69 | if (strcmp(value, "__focused__") == 0) { |
67 | (*pattern)->match_type = PATTERN_FOCUSED; | 70 | (*pattern)->match_type = PATTERN_FOCUSED; |
68 | } else { | 71 | } else { |
69 | (*pattern)->match_type = PATTERN_PCRE; | 72 | (*pattern)->match_type = PATTERN_PCRE2; |
70 | if (!generate_regex(&(*pattern)->regex, value)) { | 73 | if (!generate_regex(&(*pattern)->regex, value)) { |
71 | return false; | 74 | return false; |
72 | }; | 75 | }; |
@@ -77,7 +80,7 @@ static bool pattern_create(struct pattern **pattern, char *value) { | |||
77 | static void pattern_destroy(struct pattern *pattern) { | 80 | static void pattern_destroy(struct pattern *pattern) { |
78 | if (pattern) { | 81 | if (pattern) { |
79 | if (pattern->regex) { | 82 | if (pattern->regex) { |
80 | pcre_free(pattern->regex); | 83 | pcre2_code_free(pattern->regex); |
81 | } | 84 | } |
82 | free(pattern); | 85 | free(pattern); |
83 | } | 86 | } |
@@ -99,8 +102,11 @@ void criteria_destroy(struct criteria *criteria) { | |||
99 | free(criteria); | 102 | free(criteria); |
100 | } | 103 | } |
101 | 104 | ||
102 | static int regex_cmp(const char *item, const pcre *regex) { | 105 | static int regex_cmp(const char *item, const pcre2_code *regex) { |
103 | return pcre_exec(regex, NULL, item, strlen(item), 0, 0, NULL, 0); | 106 | pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(regex, NULL); |
107 | int result = pcre2_match(regex, (PCRE2_SPTR)item, strlen(item), 0, 0, match_data, NULL); | ||
108 | pcre2_match_data_free(match_data); | ||
109 | return result; | ||
104 | } | 110 | } |
105 | 111 | ||
106 | #if HAVE_XWAYLAND | 112 | #if HAVE_XWAYLAND |
@@ -155,7 +161,7 @@ static bool criteria_matches_container(struct criteria *criteria, | |||
155 | bool exists = false; | 161 | bool exists = false; |
156 | struct sway_container *con = container; | 162 | struct sway_container *con = container; |
157 | for (int i = 0; i < con->marks->length; ++i) { | 163 | for (int i = 0; i < con->marks->length; ++i) { |
158 | if (regex_cmp(con->marks->items[i], criteria->con_mark->regex) == 0) { | 164 | if (regex_cmp(con->marks->items[i], criteria->con_mark->regex) >= 0) { |
159 | exists = true; | 165 | exists = true; |
160 | break; | 166 | break; |
161 | } | 167 | } |
@@ -192,8 +198,8 @@ static bool criteria_matches_view(struct criteria *criteria, | |||
192 | return false; | 198 | return false; |
193 | } | 199 | } |
194 | break; | 200 | break; |
195 | case PATTERN_PCRE: | 201 | case PATTERN_PCRE2: |
196 | if (regex_cmp(title, criteria->title->regex) != 0) { | 202 | if (regex_cmp(title, criteria->title->regex) < 0) { |
197 | return false; | 203 | return false; |
198 | } | 204 | } |
199 | break; | 205 | break; |
@@ -212,8 +218,8 @@ static bool criteria_matches_view(struct criteria *criteria, | |||
212 | return false; | 218 | return false; |
213 | } | 219 | } |
214 | break; | 220 | break; |
215 | case PATTERN_PCRE: | 221 | case PATTERN_PCRE2: |
216 | if (regex_cmp(shell, criteria->shell->regex) != 0) { | 222 | if (regex_cmp(shell, criteria->shell->regex) < 0) { |
217 | return false; | 223 | return false; |
218 | } | 224 | } |
219 | break; | 225 | break; |
@@ -232,8 +238,8 @@ static bool criteria_matches_view(struct criteria *criteria, | |||
232 | return false; | 238 | return false; |
233 | } | 239 | } |
234 | break; | 240 | break; |
235 | case PATTERN_PCRE: | 241 | case PATTERN_PCRE2: |
236 | if (regex_cmp(app_id, criteria->app_id->regex) != 0) { | 242 | if (regex_cmp(app_id, criteria->app_id->regex) < 0) { |
237 | return false; | 243 | return false; |
238 | } | 244 | } |
239 | break; | 245 | break; |
@@ -264,8 +270,8 @@ static bool criteria_matches_view(struct criteria *criteria, | |||
264 | return false; | 270 | return false; |
265 | } | 271 | } |
266 | break; | 272 | break; |
267 | case PATTERN_PCRE: | 273 | case PATTERN_PCRE2: |
268 | if (regex_cmp(class, criteria->class->regex) != 0) { | 274 | if (regex_cmp(class, criteria->class->regex) < 0) { |
269 | return false; | 275 | return false; |
270 | } | 276 | } |
271 | break; | 277 | break; |
@@ -284,8 +290,8 @@ static bool criteria_matches_view(struct criteria *criteria, | |||
284 | return false; | 290 | return false; |
285 | } | 291 | } |
286 | break; | 292 | break; |
287 | case PATTERN_PCRE: | 293 | case PATTERN_PCRE2: |
288 | if (regex_cmp(instance, criteria->instance->regex) != 0) { | 294 | if (regex_cmp(instance, criteria->instance->regex) < 0) { |
289 | return false; | 295 | return false; |
290 | } | 296 | } |
291 | break; | 297 | break; |
@@ -304,8 +310,8 @@ static bool criteria_matches_view(struct criteria *criteria, | |||
304 | return false; | 310 | return false; |
305 | } | 311 | } |
306 | break; | 312 | break; |
307 | case PATTERN_PCRE: | 313 | case PATTERN_PCRE2: |
308 | if (regex_cmp(window_role, criteria->window_role->regex) != 0) { | 314 | if (regex_cmp(window_role, criteria->window_role->regex) < 0) { |
309 | return false; | 315 | return false; |
310 | } | 316 | } |
311 | break; | 317 | break; |
@@ -351,7 +357,7 @@ static bool criteria_matches_view(struct criteria *criteria, | |||
351 | } | 357 | } |
352 | 358 | ||
353 | if (criteria->workspace) { | 359 | if (criteria->workspace) { |
354 | struct sway_workspace *ws = view->container->workspace; | 360 | struct sway_workspace *ws = view->container->pending.workspace; |
355 | if (!ws) { | 361 | if (!ws) { |
356 | return false; | 362 | return false; |
357 | } | 363 | } |
@@ -359,12 +365,12 @@ static bool criteria_matches_view(struct criteria *criteria, | |||
359 | switch (criteria->workspace->match_type) { | 365 | switch (criteria->workspace->match_type) { |
360 | case PATTERN_FOCUSED: | 366 | case PATTERN_FOCUSED: |
361 | if (focused && | 367 | if (focused && |
362 | strcmp(ws->name, focused->container->workspace->name)) { | 368 | strcmp(ws->name, focused->container->pending.workspace->name)) { |
363 | return false; | 369 | return false; |
364 | } | 370 | } |
365 | break; | 371 | break; |
366 | case PATTERN_PCRE: | 372 | case PATTERN_PCRE2: |
367 | if (regex_cmp(ws->name, criteria->workspace->regex) != 0) { | 373 | if (regex_cmp(ws->name, criteria->workspace->regex) < 0) { |
368 | return false; | 374 | return false; |
369 | } | 375 | } |
370 | break; | 376 | break; |
@@ -676,7 +682,7 @@ struct criteria *criteria_parse(char *raw, char **error_arg) { | |||
676 | } | 682 | } |
677 | name = calloc(head - namestart + 1, 1); | 683 | name = calloc(head - namestart + 1, 1); |
678 | if (head != namestart) { | 684 | if (head != namestart) { |
679 | strncpy(name, namestart, head - namestart); | 685 | memcpy(name, namestart, head - namestart); |
680 | } | 686 | } |
681 | // Parse token value | 687 | // Parse token value |
682 | skip_spaces(&head); | 688 | skip_spaces(&head); |
@@ -703,7 +709,7 @@ struct criteria *criteria_parse(char *raw, char **error_arg) { | |||
703 | } | 709 | } |
704 | } | 710 | } |
705 | value = calloc(head - valuestart + 1, 1); | 711 | value = calloc(head - valuestart + 1, 1); |
706 | strncpy(value, valuestart, head - valuestart); | 712 | memcpy(value, valuestart, head - valuestart); |
707 | if (in_quotes) { | 713 | if (in_quotes) { |
708 | ++head; | 714 | ++head; |
709 | in_quotes = false; | 715 | in_quotes = false; |
@@ -734,7 +740,7 @@ struct criteria *criteria_parse(char *raw, char **error_arg) { | |||
734 | ++head; | 740 | ++head; |
735 | int len = head - raw; | 741 | int len = head - raw; |
736 | criteria->raw = calloc(len + 1, 1); | 742 | criteria->raw = calloc(len + 1, 1); |
737 | strncpy(criteria->raw, raw, len); | 743 | memcpy(criteria->raw, raw, len); |
738 | return criteria; | 744 | return criteria; |
739 | 745 | ||
740 | cleanup: | 746 | cleanup: |