Commit | Line | Data |
---|---|---|
a2e9d147 MW |
1 | /* |
2 | * This file is part of DisOrder | |
3 | * Copyright (C) 2017 Mark Wooding | |
4 | * | |
5 | * This program is free software: you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation, either version 3 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | /** @file lib/regexp.c | |
19 | * @brief Regular expressions | |
20 | */ | |
21 | #include "common.h" | |
22 | ||
23 | #include "regexp.h" | |
24 | #include "mem.h" | |
25 | ||
16e4a580 MW |
26 | #ifdef HAVE_LIBPCRE2 |
27 | ||
28 | static pcre2_general_context *genctx = 0; | |
29 | static pcre2_compile_context *compctx = 0; | |
30 | ||
31 | static void *rxalloc(size_t sz, void attribute((unused)) *q) | |
32 | { return xmalloc(sz); } | |
33 | static void rxfree(void *p, void attribute((unused)) *q) | |
34 | { xfree(p); } | |
35 | ||
36 | void regexp_setup(void) | |
37 | { | |
38 | if(genctx) { | |
39 | pcre2_compile_context_free(compctx); | |
40 | pcre2_general_context_free(genctx); | |
41 | } | |
42 | genctx = pcre2_general_context_create(rxalloc, rxfree, 0); | |
43 | compctx = pcre2_compile_context_create(genctx); | |
44 | } | |
45 | ||
46 | regexp *regexp_compile(const char *pat, unsigned f, | |
47 | char *errbuf, size_t errlen, size_t *erroff_out) | |
48 | { | |
49 | int errcode; | |
50 | PCRE2_SIZE erroff; | |
51 | regexp *re; | |
52 | ||
53 | re = pcre2_compile((PCRE2_SPTR)pat, strlen(pat), f, | |
54 | &errcode, &erroff, compctx); | |
55 | if(!re) { | |
56 | *erroff_out = erroff; | |
57 | pcre2_get_error_message(errcode, (PCRE2_UCHAR *)errbuf, errlen); | |
58 | } | |
59 | return re; | |
60 | } | |
61 | ||
62 | int regexp_match(const regexp *re, const char *s, size_t n, unsigned f, | |
63 | size_t *ov, size_t on) | |
64 | { | |
65 | int rc; | |
66 | pcre2_match_data *m; | |
67 | PCRE2_SIZE *ovp; | |
68 | size_t i; | |
69 | ||
70 | m = pcre2_match_data_create(on, genctx); | |
71 | rc = pcre2_match(re, (PCRE2_SPTR)s, n, 0, f, m, 0); | |
72 | ovp = pcre2_get_ovector_pointer(m); | |
73 | for(i = 0; i < on; i++) ov[i] = ovp[i]; | |
74 | pcre2_match_data_free(m); | |
75 | return rc; | |
76 | } | |
77 | ||
78 | void regexp_free(regexp *re) | |
79 | { pcre2_code_free(re); } | |
80 | ||
81 | #else | |
82 | ||
a2e9d147 MW |
83 | void regexp_setup(void) |
84 | { | |
85 | pcre_malloc = xmalloc; | |
86 | pcre_free = xfree; | |
87 | } | |
88 | ||
89 | regexp *regexp_compile(const char *pat, unsigned f, | |
90 | char *errbuf, size_t errlen, size_t *erroff_out) | |
91 | { | |
92 | char *p; | |
93 | const char *e; | |
94 | int erroff; | |
95 | regexp *re; | |
96 | size_t i; | |
97 | ||
98 | re = pcre_compile(pat, f, &e, &erroff, 0); | |
99 | if(!re) { | |
100 | *erroff_out = erroff; | |
101 | for(p = errbuf, i = errlen - 1; i && *e; i--) *p++ = *e++; | |
102 | *p = 0; | |
103 | } | |
104 | return re; | |
105 | } | |
106 | ||
107 | int regexp_match(const regexp *re, const char *s, size_t n, unsigned f, | |
108 | size_t *ov, size_t on) | |
109 | { | |
110 | int rc; | |
111 | int *myov; | |
112 | size_t i; | |
113 | ||
114 | myov = xmalloc(on*sizeof(*myov)); | |
115 | rc = pcre_exec(re, 0, s, n, 0, f, myov, on); | |
116 | for(i = 0; i < on; i++) ov[i] = myov[i]; | |
117 | xfree(myov); | |
118 | return rc; | |
119 | } | |
120 | ||
121 | void regexp_free(regexp *re) | |
122 | { pcre_free(re); } | |
123 | ||
16e4a580 MW |
124 | #endif |
125 | ||
a2e9d147 MW |
126 | /* |
127 | Local Variables: | |
128 | c-basic-offset:2 | |
129 | comment-column:40 | |
130 | End: | |
131 | */ |