Skip to content

Commit

Permalink
rules: implement regex matching
Browse files Browse the repository at this point in the history
This enables more advanced regular expression syntax. It can be enabled
with `enable_posix_regex`.

Fixes: dunst-project#1013
Fixes: dunst-project#645
Fixes: dunst-project#658
  • Loading branch information
fwsmit committed Jan 16, 2022
1 parent 37c72d5 commit efbfe86
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 14 deletions.
26 changes: 25 additions & 1 deletion docs/dunst.5.pod
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ keyboard focus.

=back

=item B<enable_posix_regex> (default: false)

When set to true (recommended), you can use POSIX regular expressions for
filtering rules. It uses the POSIX Extended Regular Expression syntax:
https://en.m.wikibooks.org/wiki/Regular_Expressions/POSIX-Extended_Regular_Expressions.

If this is set to false (not recommended), dunst will us fnmatch(3) for matching
strings. Dunst doesn't pass any flags to fnmatch, so you cannot make use of
extended patterns.

The POSIX syntax is more powerful and will eventually become the default. The main
differences between POSIX and fnmatch(3) is that POSIX uses ".*" for wildcards
instead of "*" and POSIX allows for partial matches without needing wildcards.
This means that the pattern "abc" will match all strings that contain "abc",
like "abcdef".

=item B<geometry> DEPRECATED

This setting is deprecated and removed. It's split up into B<width>, B<height>, B<origin>,
Expand Down Expand Up @@ -680,7 +696,15 @@ rule, it may affect if it's being matched by a later rule.

=item B<filtering>

Notifications can be matched for any of the following attributes:
With filtering rules you can match notifications to apply rules to only a subset
of notifications.

For filtering rules that filter based on strings you can use regular
expressions. It's recommended to set B<enable_posix_regex> to true. You can then
use the POSIX Extended Regular Expression syntax:
https://en.m.wikibooks.org/wiki/Regular_Expressions/POSIX-Extended_Regular_Expressions.

Notifications can be matched for any of the following attributes.

=over 4

Expand Down
26 changes: 25 additions & 1 deletion src/rules.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <fnmatch.h>
#include <glib.h>
#include <stddef.h>
#include <regex.h>

#include "dunst.h"
#include "utils.h"
Expand Down Expand Up @@ -140,7 +141,30 @@ struct rule *rule_new(const char *name)

static inline bool rule_field_matches_string(const char *value, const char *pattern)
{
return !pattern || (value && !fnmatch(pattern, value, 0));
if (settings.enable_regex) {
if (!pattern) {
return true;
}
if (!value) {
return false;
}
regex_t regex;

// TODO compile each regex only once
if (regcomp(&regex, pattern, REG_NEWLINE | REG_EXTENDED | REG_NOSUB))
return false;

for (int i = 0; ; i++) {
if (regexec(&regex, value, 0, NULL, 0))
break;
regfree(&regex);
return true;
}
regfree(&regex);
return false;
} else {
return !pattern || (value && !fnmatch(pattern, value, 0));
}
}

/*
Expand Down
1 change: 1 addition & 0 deletions src/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ struct settings {
int max_icon_size;
char **icon_theme; // experimental
bool enable_recursive_icon_lookup; // experimental
bool enable_regex; // experimental
char *icon_path;
enum follow_mode f_mode;
bool always_run_script;
Expand Down
10 changes: 10 additions & 0 deletions src/settings_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,16 @@ static const struct setting allowed_settings[] = {
.parser = string_parse_bool,
.parser_data = boolean_enum_data,
},
{
.name = "enable_posix_regex",
.section = "global",
.description = "Enable POSIX regex for filtering rules",
.type = TYPE_CUSTOM,
.default_value = "false",
.value = &settings.enable_regex,
.parser = string_parse_bool,
.parser_data = boolean_enum_data,
},
{
.name = "frame_width",
.section = "global",
Expand Down
70 changes: 58 additions & 12 deletions test/rules.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "../src/rules.c"

#include "greatest.h"
#include <regex.h>

extern const char *base;

Expand All @@ -9,24 +10,15 @@ TEST test_pattern_match(void) {
// NULL should match everything
ASSERT(rule_field_matches_string("anything", NULL));

// Wildcard matching
ASSERT(rule_field_matches_string("anything", "*"));
ASSERT(rule_field_matches_string("*", "*"));
ASSERT(rule_field_matches_string("", "*"));
ASSERT(rule_field_matches_string("ffffasd", "*asd"));
// Literal matches
ASSERT(rule_field_matches_string("asdf", "asdf"));
ASSERT(rule_field_matches_string("test123", "test123"));

// Single character matching
ASSERT(rule_field_matches_string("a", "?"));

// special characters that are not special to fnmatch
ASSERT(rule_field_matches_string("!", "!"));
ASSERT(rule_field_matches_string("!asd", "!asd"));
ASSERT(rule_field_matches_string("/as/d", "/as/d"));
ASSERT(rule_field_matches_string("/as/d", "/as/d"));

// Match substrings
ASSERT(rule_field_matches_string("asd", "asd"));

// ranges
ASSERT(rule_field_matches_string("ac", "[a-z][a-z]"));

Expand All @@ -35,8 +27,62 @@ TEST test_pattern_match(void) {
ASSERT_FALSE(rule_field_matches_string("ffff", "*asd"));
ASSERT_FALSE(rule_field_matches_string("ffff", "?"));
ASSERT_FALSE(rule_field_matches_string("Ac", "[a-z][a-z]"));

// Things that differ between fnmatch(3) and regex(3)

if (settings.enable_regex) {
// Single character matching
ASSERT(rule_field_matches_string("a", "."));

// Wildcard matching
ASSERT(rule_field_matches_string("anything", ".*"));
ASSERT(rule_field_matches_string("*", ".*"));
ASSERT(rule_field_matches_string("", ".*"));
ASSERT(rule_field_matches_string("ffffasd", ".*asd"));

// Substring matching
ASSERT(rule_field_matches_string("asd", ""));
ASSERT(rule_field_matches_string("asd", "sd"));
ASSERT(rule_field_matches_string("asd", "a"));
ASSERT(rule_field_matches_string("asd", "d"));
ASSERT(rule_field_matches_string("asd", "asd"));

// Match multiple strings
ASSERT(rule_field_matches_string("ghj", "asd|dfg|ghj"));
ASSERT(rule_field_matches_string("asd", "asd|dfg|ghj"));
ASSERT(rule_field_matches_string("dfg", "asd|dfg|ghj"));
ASSERT_FALSE(rule_field_matches_string("azd", "asd|dfg|ghj"));

// Special characters
ASSERT_FALSE(rule_field_matches_string("{", "{"));
} else {
// Single character matching
ASSERT(rule_field_matches_string("a", "?"));

// Wildcard matching
ASSERT(rule_field_matches_string("anything", "*"));
ASSERT(rule_field_matches_string("*", "*"));
ASSERT(rule_field_matches_string("", "*"));
ASSERT(rule_field_matches_string("ffffasd", "*asd"));

// Substring matching
ASSERT_FALSE(rule_field_matches_string("asd", ""));
ASSERT_FALSE(rule_field_matches_string("asd", "sd"));
ASSERT_FALSE(rule_field_matches_string("asd", "a"));
ASSERT_FALSE(rule_field_matches_string("asd", "d"));
ASSERT(rule_field_matches_string("asd", "asd"));
}
PASS();
}

SUITE(suite_rules) {
bool store = settings.enable_regex;

settings.enable_regex = false;
RUN_TEST(test_pattern_match);

settings.enable_regex = true;
RUN_TEST(test_pattern_match);

settings.enable_regex = store;
}

0 comments on commit efbfe86

Please sign in to comment.