Fix excludes pattern matching

This commit is contained in:
Martin Lund 2024-04-27 01:31:35 +02:00
parent 6e779a0520
commit c61d56935b
3 changed files with 18 additions and 50 deletions

View file

@ -33,6 +33,7 @@
#include <errno.h> #include <errno.h>
#include <sys/poll.h> #include <sys/poll.h>
#include <termios.h> #include <termios.h>
#include <fnmatch.h>
#include "error.h" #include "error.h"
#include "print.h" #include "print.h"
#include "options.h" #include "options.h"
@ -179,66 +180,33 @@ double get_current_time(void)
return current_time_ts.tv_sec + current_time_ts.tv_nsec / 1e9; return current_time_ts.tv_sec + current_time_ts.tv_nsec / 1e9;
} }
// Function to match string with comma separated patterns which supports '*' and '?' bool match_patterns(const char *string, const char *patterns)
static bool is_match(const char *str, const char *pattern)
{ {
// If both string and pattern reach end, they match char *pattern;
if (*str == '\0' && *pattern == '\0') char *patterns_copy;
{
return true;
}
// If pattern reaches end but string has characters left, no match if ((string == NULL) || (patterns == NULL))
if (*pattern == '\0')
{ {
return false; return false;
} }
// If current characters match or pattern has '?', move to the next character in both patterns_copy = strdup(patterns);
if (*str == *pattern || *pattern == '?')
{
return is_match(str + 1, pattern + 1);
}
// If current pattern character is '*', check for matches by moving string or pattern // Tokenize the patterns string using strtok
if (*pattern == '*') pattern = strtok(patterns_copy, ",");
while (pattern != NULL)
{ {
// '*' matches zero or more characters, so try all possibilities // Check if the string matches the current pattern
// Move pattern to the next character and check if remaining pattern matches remaining string if (fnmatch(pattern, string, 0) == 0)
// Move string to the next character and check if current pattern matches remaining string
return is_match(str, pattern + 1) || is_match(str + 1, pattern);
}
// No match
return false;
}
bool match_any_pattern(const char *str, const char *patterns)
{
if ((str == NULL) || (patterns == NULL))
{
return false;
}
char *patterns_copy = strdup(patterns);
if (patterns_copy == NULL)
{
tio_error_print("Memory allocation failed");
exit(EXIT_FAILURE);
}
char *token = strtok(patterns_copy, ",");
while (token != NULL)
{
if (is_match(str, token))
{ {
free(patterns_copy); free(patterns_copy);
return true; return true;
} }
token = strtok(NULL, ",");
// Move to the next pattern
pattern = strtok(NULL, ",");
} }
free(patterns_copy); free(patterns_copy);
return false; return false;
} }

View file

@ -36,4 +36,4 @@ unsigned long djb2_hash(const unsigned char *str);
char *base62_encode(unsigned long num); char *base62_encode(unsigned long num);
int read_poll(int fd, void *data, size_t len, int timeout); int read_poll(int fd, void *data, size_t len, int timeout);
double get_current_time(void); double get_current_time(void);
bool match_any_pattern(const char *str, const char *patterns); bool match_patterns(const char *string, const char *patterns);

View file

@ -1717,15 +1717,15 @@ GList *tty_search_for_serial_devices(void)
} }
// Do not add devices excluded by exclude patterns // Do not add devices excluded by exclude patterns
if (match_any_pattern(path, option.exclude_devices)) if (match_patterns(path, option.exclude_devices))
{ {
continue; continue;
} }
if (match_any_pattern(driver, option.exclude_drivers)) if (match_patterns(driver, option.exclude_drivers))
{ {
continue; continue;
} }
if (match_any_pattern(tid, option.exclude_tids)) if (match_patterns(tid, option.exclude_tids))
{ {
continue; continue;
} }