| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #ifndef NETDATA_SIMPLE_PATTERN_H |
| 4 | #define NETDATA_SIMPLE_PATTERN_H |
| 5 | |
| 6 | #include "../libnetdata.h" |
| 7 | |
| 8 | typedef enum __attribute__ ((__packed__)) { |
| 9 | SIMPLE_PATTERN_EXACT, |
| 10 | SIMPLE_PATTERN_PREFIX, |
| 11 | SIMPLE_PATTERN_SUFFIX, |
| 12 | SIMPLE_PATTERN_SUBSTRING |
| 13 | } SIMPLE_PREFIX_MODE; |
| 14 | |
| 15 | typedef enum __attribute__ ((__packed__)) { |
| 16 | SP_NOT_MATCHED, |
| 17 | SP_MATCHED_NEGATIVE, |
| 18 | SP_MATCHED_POSITIVE, |
| 19 | } SIMPLE_PATTERN_RESULT; |
| 20 | |
| 21 | struct simple_pattern; |
| 22 | typedef struct simple_pattern SIMPLE_PATTERN; |
| 23 | |
| 24 | #define SIMPLE_PATTERN_NO_SEPARATORS (const char *)(0xFFFFFFFF) |
| 25 | |
| 26 | // create a simple_pattern from the string given |
| 27 | // default_mode is used in cases where EXACT matches, without an asterisk, |
| 28 | // should be considered PREFIX matches. |
| 29 | SIMPLE_PATTERN *simple_pattern_create(const char *list, const char *separators, SIMPLE_PREFIX_MODE default_mode, bool case_sensitive); |
| 30 | |
| 31 | struct netdata_string; |
| 32 | |
| 33 | // test if string str is matched from the pattern and fill 'wildcarded' with the parts matched by '*' |
| 34 | SIMPLE_PATTERN_RESULT simple_pattern_matches_extract(SIMPLE_PATTERN *list, const char *str, char *wildcarded, size_t wildcarded_size); |
| 35 | SIMPLE_PATTERN_RESULT simple_pattern_matches_string_extract(SIMPLE_PATTERN *list, struct netdata_string *str, char *wildcarded, size_t wildcarded_size); |
| 36 | SIMPLE_PATTERN_RESULT simple_pattern_matches_buffer_extract(SIMPLE_PATTERN *list, BUFFER *str, char *wildcarded, size_t wildcarded_size); |
| 37 | SIMPLE_PATTERN_RESULT simple_pattern_matches_length_extract(SIMPLE_PATTERN *list, const char *str, size_t len, char *wildcarded, size_t wildcarded_size); |
| 38 | |
| 39 | // test if string str is matched from the pattern |
| 40 | #define simple_pattern_matches(list, str) (simple_pattern_matches_extract(list, str, NULL, 0) == SP_MATCHED_POSITIVE) |
| 41 | #define simple_pattern_matches_string(list, str) (simple_pattern_matches_string_extract(list, str, NULL, 0) == SP_MATCHED_POSITIVE) |
| 42 | #define simple_pattern_matches_buffer(list, str) (simple_pattern_matches_buffer_extract(list, str, NULL, 0) == SP_MATCHED_POSITIVE) |
| 43 | |
| 44 | // free a simple_pattern that was created with simple_pattern_create() |
| 45 | // list can be NULL, in which case, this does nothing. |
| 46 | void simple_pattern_free(SIMPLE_PATTERN *list); |
| 47 | |
| 48 | void simple_pattern_dump(uint64_t debug_type, SIMPLE_PATTERN *p) ; |
| 49 | int simple_pattern_is_potential_name(SIMPLE_PATTERN *p) ; |
| 50 | char *simple_pattern_iterate(SIMPLE_PATTERN **p); |
| 51 | |
| 52 | // check if string contains pattern wildcards (*, ! prefix, or separators) |
| 53 | bool simple_pattern_contains_wildcards(const char *str, const char *separators); |
| 54 | |
| 55 | #define SIMPLE_PATTERN_DEFAULT_WEB_SEPARATORS ",|\t\r\n\f\v" |
| 56 | |
| 57 | #define is_valid_sp(x) ((x) && *(x) && !((x)[0] == '*' && (x)[1] == '\0')) |
| 58 | |
| 59 | #define string_to_simple_pattern(str) (is_valid_sp(str) ? simple_pattern_create(str, SIMPLE_PATTERN_DEFAULT_WEB_SEPARATORS, SIMPLE_PATTERN_EXACT, true) : NULL) |
| 60 | #define string_to_simple_pattern_nocase(str) (is_valid_sp(str) ? simple_pattern_create(str, SIMPLE_PATTERN_DEFAULT_WEB_SEPARATORS, SIMPLE_PATTERN_EXACT, false) : NULL) |
| 61 | #define string_to_simple_pattern_nocase_substring(str) (is_valid_sp(str) ? simple_pattern_create(str, SIMPLE_PATTERN_DEFAULT_WEB_SEPARATORS, SIMPLE_PATTERN_SUBSTRING, false) : NULL) |
| 62 | |
| 63 | #endif //NETDATA_SIMPLE_PATTERN_H |