summaryrefslogtreecommitdiffstats
path: root/collectors/log2journal/log2journal-pattern.c
blob: 4b7e9026b3a4c79c592d4b65816ed01747f5f368 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// SPDX-License-Identifier: GPL-3.0-or-later

#include "log2journal.h"

void search_pattern_cleanup(SEARCH_PATTERN *sp) {
    if(sp->pattern) {
        freez((void *)sp->pattern);
        sp->pattern = NULL;
    }

    if(sp->re) {
        pcre2_code_free(sp->re);
        sp->re = NULL;
    }

    if(sp->match_data) {
        pcre2_match_data_free(sp->match_data);
        sp->match_data = NULL;
    }

    txt_cleanup(&sp->error);
}

static void pcre2_error_message(SEARCH_PATTERN *sp, int rc, int pos) {
    char msg[1024];
    pcre2_get_error_in_buffer(msg, sizeof(msg), rc, pos);
    txt_replace(&sp->error, msg, strlen(msg));
}

static inline bool compile_pcre2(SEARCH_PATTERN *sp) {
    int error_number;
    PCRE2_SIZE error_offset;
    PCRE2_SPTR pattern_ptr = (PCRE2_SPTR)sp->pattern;

    sp->re = pcre2_compile(pattern_ptr, PCRE2_ZERO_TERMINATED, 0, &error_number, &error_offset, NULL);
    if (!sp->re) {
        pcre2_error_message(sp, error_number, (int) error_offset);
        return false;
    }

    return true;
}

bool search_pattern_set(SEARCH_PATTERN *sp, const char *search_pattern, size_t search_pattern_len) {
    search_pattern_cleanup(sp);

    sp->pattern = strndupz(search_pattern, search_pattern_len);
    if (!compile_pcre2(sp))
        return false;

    sp->match_data = pcre2_match_data_create_from_pattern(sp->re, NULL);

    return true;
}