Debian Codegraph @

modules/pcre/pcre.c

raw
1 /* This file is part of GNU Dico.
2 Copyright (C) 2008-2024 Sergey Poznyakoff
3
4 GNU Dico is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
7 any later version.
8
9 GNU Dico is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with GNU Dico. If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18 #include <dico.h>
19 #include <stdlib.h>
20 #include <ctype.h>
21 #include <errno.h>
22 #include <appi18n.h>
23 #define PCRE2_CODE_UNIT_WIDTH 8
24 #include <pcre2.h>
25
26 struct dico_pcre_flag
27 {
28 int c;
29 int flag;
30 };
31
32 static struct dico_pcre_flag flagtab[] = {
33 { 'a', PCRE2_ANCHORED }, /* Force pattern anchoring */
34 { 'e', PCRE2_EXTENDED }, /* Ignore whitespace and # comments */
35 { 'i', PCRE2_CASELESS }, /* Do caseless matching */
36 { 'G', PCRE2_UNGREEDY }, /* Invert greediness of quantifiers */
37 { 0 },
38 };
39
40 static int
41 pcre_flag(int c, uint32_t *pflags)
42 {
43 struct dico_pcre_flag *p;
44
45 for (p = flagtab; p->c; p++) {
46 if (p->c == c) {
47 *pflags |= p->flag;
48 return 0;
49 } else if (p->c == tolower(c) || p->c == toupper(c)) {
50 *pflags &= ~p->flag;
51 return 0;
52 }
53 }
54 return 1;
55 }
56
57 static pcre2_code *
58 compile_pattern(const char *pattern)
59 {
60 uint32_t cflags = PCRE2_UTF|PCRE2_NEWLINE_ANY;
61 int error;
62 PCRE2_SIZE length;
63 PCRE2_SIZE error_offset;
64 pcre2_code *pre;
65
66 /*
67 * Pcre2 documentation claims that pattern length is measured in code
68 * points. This, however, doesn't seem to be the case. At least, with
69 * version 10.35, length is measured in characters.
70 */
71 length = strlen(pattern);
72 if (pattern[0] == '/') {
73 char *p;
74
75 pattern++;
76 length--;
77 p = strrchr(pattern, '/');
78 if (!p) {
79 dico_log(L_ERR, 0, _("PCRE missing terminating /: %s"),
80 pattern - 1);
81 return NULL;
82 }
83 length -= strlen(p);
84
85 while (*++p) {
86 if (pcre_flag(*p, &cflags)) {
87 dico_log(L_ERR, 0, _("PCRE error: invalid flag %c"), *p);
88 return NULL;
89 }
90 }
91 }
92 pre = pcre2_compile((PCRE2_SPTR8)pattern, length, cflags, &error, &error_offset, NULL);
93 if (!pre) {
94 char errbuf[120];
95
96 switch (pcre2_get_error_message(error, (PCRE2_UCHAR8*)errbuf, sizeof errbuf)) {
97 case PCRE2_ERROR_NOMEMORY:
98 default:
99 break;
100 case PCRE2_ERROR_BADDATA:
101 strncpy(errbuf, "bad error code", sizeof(errbuf)-1);
102 break;
103 }
104 dico_log(L_ERR, 0,
105 _("pcre_compile(\"%s\") failed at offset %lu: %s"),
106 pattern, error_offset, errbuf);
107 }
108 return pre;
109 }
110
111 struct pcre_call_data
112 {
113 pcre2_code *code;
114 pcre2_match_data *md;
115 };
116
117 static int
118 pcre_sel(int cmd, dico_key_t key, const char *dict_word)
119 {
120 int rc = 0;
121 char const *word = key->word;
122 struct pcre_call_data *cdata = key->call_data;
123
124 switch (cmd) {
125 case DICO_SELECT_BEGIN:
126 if ((cdata = calloc(1, sizeof(cdata[0]))) == NULL) {
127 DICO_LOG_MEMERR();
128 return 1;
129 }
130 if ((cdata->code = compile_pattern(word)) == NULL) {
131 free(cdata);
132 return 1;
133 }
134 cdata->md = pcre2_match_data_create_from_pattern(cdata->code, NULL);
135 if (cdata->md == NULL) {
136 pcre2_code_free(cdata->code);
137 free(cdata);
138 return 1;
139 }
140 key->call_data = cdata;
141 break;
142
143 case DICO_SELECT_RUN:
144 rc = pcre2_match(cdata->code,
145 (PCRE2_SPTR8)dict_word, PCRE2_ZERO_TERMINATED,
146 0, 0, cdata->md, NULL) >= 0;
147 break;
148
149 case DICO_SELECT_END:
150 pcre2_match_data_free(cdata->md);
151 pcre2_code_free(cdata->code);
152 free(cdata);
153 break;
154 }
155 return rc;
156 }
157
158 static struct dico_strategy pcre_strat = {
159 "pcre",
160 "Match using Perl-compatible regular expressions",
161 pcre_sel
162 };
163
164 static int
165 pcre_init(int argc, char **argv)
166 {
167 dico_strategy_add(&pcre_strat);
168 return 0;
169 }
170
171 struct dico_database_module DICO_EXPORT(pcre, module) = {
172 DICO_MODULE_VERSION,
173 DICO_CAPA_NODB,
174 pcre_init,
175 };