master
c 54 lines 1.35 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #include "log2journal.h"
4
5 void search_pattern_cleanup(SEARCH_PATTERN *sp) {
6 if(sp->pattern) {
7 freez((void *)sp->pattern);
8 sp->pattern = NULL;
9 }
10
11 if(sp->re) {
12 pcre2_code_free(sp->re);
13 sp->re = NULL;
14 }
15
16 if(sp->match_data) {
17 pcre2_match_data_free(sp->match_data);
18 sp->match_data = NULL;
19 }
20
21 txt_l2j_cleanup(&sp->error);
22 }
23
24 static void pcre2_error_message(SEARCH_PATTERN *sp, int rc, int pos) {
25 char msg[1024];
26 pcre2_get_error_in_buffer(msg, sizeof(msg), rc, pos);
27 txt_l2j_set(&sp->error, msg, strlen(msg));
28 }
29
30 static inline bool compile_pcre2(SEARCH_PATTERN *sp) {
31 int error_number;
32 PCRE2_SIZE error_offset;
33 PCRE2_SPTR pattern_ptr = (PCRE2_SPTR)sp->pattern;
34
35 sp->re = pcre2_compile(pattern_ptr, PCRE2_ZERO_TERMINATED, 0, &error_number, &error_offset, NULL);
36 if (!sp->re) {
37 pcre2_error_message(sp, error_number, (int) error_offset);
38 return false;
39 }
40
41 return true;
42 }
43
44 bool search_pattern_set(SEARCH_PATTERN *sp, const char *search_pattern, size_t search_pattern_len) {
45 search_pattern_cleanup(sp);
46
47 sp->pattern = strndupz(search_pattern, search_pattern_len);
48 if (!compile_pcre2(sp))
49 return false;
50
51 sp->match_data = pcre2_match_data_create_from_pattern(sp->re, NULL);
52
53 return true;
54 }