Regex matching for one string moved to the static function regex1().
[mpdm.git] / mpdm_r.c
blob10ce7746b75dd8730a8a87f698f56e2dac41aa10
1 /*
3 MPDM - Minimum Profit Data Manager
4 Copyright (C) 2003/2007 Angel Ortega <angel@triptico.com>
6 mpdm_r.c - Regular expressions
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 http://www.triptico.com
26 #include "config.h"
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <wchar.h>
33 #include "mpdm.h"
35 #ifdef CONFOPT_PCRE
36 #include <pcreposix.h>
37 #endif
39 #ifdef CONFOPT_SYSTEM_REGEX
40 #include <regex.h>
41 #endif
43 #ifdef CONFOPT_INCLUDED_REGEX
44 #include "gnu_regex.h"
45 #endif
47 /*******************
48 Data
49 ********************/
51 /* matching of the last regex */
53 int mpdm_regex_offset = -1;
54 int mpdm_regex_size = 0;
56 /* number of substitutions in last sregex */
58 int mpdm_sregex_count = 0;
60 /*******************
61 Code
62 ********************/
64 static wchar_t *regex_flags(const mpdm_t r)
66 return wcsrchr((wchar_t *) r->data, *(wchar_t *) r->data);
70 static mpdm_t mpdm_regcomp(mpdm_t r)
72 mpdm_t c = NULL;
73 mpdm_t regex_cache = NULL;
75 /* if cache does not exist, create it */
76 if ((regex_cache = mpdm_hget_s(mpdm_root(), L"__REGEX_CACHE__")) == NULL) {
77 regex_cache = MPDM_H(0);
78 mpdm_hset_s(mpdm_root(), L"__REGEX_CACHE__", regex_cache);
81 /* search the regex in the cache */
82 if ((c = mpdm_hget(regex_cache, r)) == NULL) {
83 mpdm_t rmb;
84 regex_t re;
85 char *regex;
86 char *flags;
87 int f = REG_EXTENDED;
89 /* not found; regex must be compiled */
91 /* convert to mbs */
92 rmb = MPDM_2MBS(r->data);
94 regex = (char *) rmb->data;
95 if ((flags = strrchr(regex, *regex)) == NULL)
96 return NULL;
98 if (strchr(flags, 'i') != NULL)
99 f |= REG_ICASE;
100 if (strchr(flags, 'm') != NULL)
101 f |= REG_NEWLINE;
103 regex++;
104 *flags = '\0';
106 if (!regcomp(&re, regex, f)) {
107 void *ptr;
109 if ((ptr = malloc(sizeof(regex_t))) != NULL) {
110 /* copies */
111 memcpy(ptr, &re, sizeof(regex_t));
113 /* create value */
114 c = mpdm_new(MPDM_FREE, ptr, sizeof(regex_t));
116 /* stores */
117 mpdm_hset(regex_cache, r, c);
122 return c;
126 static mpdm_t regex1(mpdm_t r, const mpdm_t v, int offset)
127 /* test for one regex */
129 mpdm_t w = NULL;
130 mpdm_t cr;
132 /* no matching yet */
133 mpdm_regex_offset = -1;
135 /* compile the regex */
136 if ((cr = mpdm_regcomp(r)) != NULL) {
137 regmatch_t rm;
138 char *ptr;
139 wchar_t *last;
140 int o = 0;
141 int f = 0;
143 /* takes pointer to 'last' flag */
144 if ((last = regex_flags(r)) != NULL)
145 last = wcschr(last, 'l');
147 /* convert to mbs */
148 ptr = mpdm_wcstombs((wchar_t *) v->data + offset, NULL);
150 /* match? */
151 while (regexec((regex_t *) cr->data, ptr + o, 1,
152 &rm, offset > 0 ? REG_NOTBOL : 0) == 0) {
153 f++;
155 /* if 'last' is not set, it's done */
156 if (last == NULL)
157 break;
159 rm.rm_so += o;
160 rm.rm_eo += o;
161 o = rm.rm_eo;
164 if (f) {
165 /* converts to mbs the string from the beginning
166 to the start of the match, just to know
167 the size (and immediately frees it) */
168 free(mpdm_mbstowcs(ptr, &mpdm_regex_offset, rm.rm_so));
170 /* add the offset */
171 mpdm_regex_offset += offset;
173 /* create now the matching string */
174 w = MPDM_NMBS(ptr + rm.rm_so, rm.rm_eo - rm.rm_so);
176 /* and store the size */
177 mpdm_regex_size = mpdm_size(w);
180 free(ptr);
183 return w;
188 * mpdm_regex - Matches a regular expression.
189 * @r: the regular expression
190 * @v: the value to be matched
191 * @offset: offset from the start of v->data
193 * Matches a regular expression against a value. Valid flags are 'i',
194 * for case-insensitive matching, 'm', to treat the string as a
195 * multiline string (i.e., one containing newline characters), so
196 * that ^ and $ match the boundaries of each line instead of the
197 * whole string, or 'l', to return the last matching instead of
198 * the first one.
200 * If @r is a string, an ordinary regular expression matching is tried
201 * over the @v string. If the matching is possible, the matched string
202 * is returned, or NULL otherwise.
204 * If @r is an array (of strings), each element is tried sequentially
205 * as an individual regular expression over the @v string, each one using
206 * the offset returned by the previous match. All regular expressions
207 * must match to be successful. If this is the case, an array (with
208 * the same number of arguments) is returned containing the matched
209 * strings, or NULL otherwise.
211 * If @r is NULL, the result of the previous regex matching
212 * is returned as a two element array. The first element will contain
213 * the character offset of the matching and the second the number of
214 * characters matched. If the previous regex was unsuccessful, NULL
215 * is returned.
216 * [Regular Expressions]
218 mpdm_t mpdm_regex(mpdm_t r, const mpdm_t v, int offset)
220 mpdm_t w = NULL;
222 /* special case: if r is NULL, return previous match */
223 if (r == NULL) {
224 /* if previous regex was successful... */
225 if (mpdm_regex_offset != -1) {
226 w = MPDM_A(2);
228 mpdm_aset(w, MPDM_I(mpdm_regex_offset), 0);
229 mpdm_aset(w, MPDM_I(mpdm_regex_size), 1);
232 return w;
235 /* if the string to be tested is NULL, return NULL */
236 if (v == NULL)
237 return NULL;
239 if (r->flags & MPDM_MULTIPLE) {
240 int n;
241 mpdm_t t;
243 /* multiple value; try sequentially all regexes,
244 moving the offset forward */
246 w = MPDM_A(mpdm_size(r));
248 for (n = 0; n < mpdm_size(r); n++) {
249 t = mpdm_regex(mpdm_aget(r, n), v, offset);
251 /* if not found, invalid all search and exit */
252 if (t == NULL) {
253 w = NULL;
254 break;
257 /* found; store and move forward */
258 mpdm_aset(w, t, n);
259 offset = mpdm_regex_offset + mpdm_regex_size;
262 else
263 w = regex1(r, v, offset);
265 return w;
269 static mpdm_t expand_ampersands(const mpdm_t s, const mpdm_t t)
270 /* substitutes all unescaped ampersands in s with t */
272 const wchar_t *sptr = mpdm_string(s);
273 wchar_t *wptr;
274 mpdm_t r = s;
276 if ((wptr = wcschr(sptr, L'&')) != NULL) {
278 r = NULL;
280 while (wptr != NULL) {
281 int n = wptr - sptr;
282 mpdm_t t2 = t;
284 if (n && *(wptr - 1) == '\\') {
285 /* is it escaped? avoid the \ */
286 n--;
288 /* and set the substitution string to & */
289 t2 = MPDM_LS(L"&");
292 /* add the leading part */
293 r = mpdm_strcat(r, MPDM_NS(sptr, n));
295 /* now add the substitution string */
296 r = mpdm_strcat(r, t2);
298 sptr = wptr + 1;
299 wptr = wcschr(sptr, L'&');
302 /* add the rest of the string */
303 r = mpdm_strcat(r, MPDM_S(sptr));
306 return r;
311 * mpdm_sregex - Matches and substitutes a regular expression.
312 * @r: the regular expression
313 * @v: the value to be matched
314 * @s: the substitution string, hash or code
315 * @offset: offset from the start of v->data
317 * Matches a regular expression against a value, and substitutes the
318 * found substring with @s. Valid flags are 'i', for case-insensitive
319 * matching, and 'g', for global replacements (all ocurrences in @v
320 * will be replaced, instead of just the first found one).
322 * If @s is executable, it's executed with the matched part as
323 * the only argument and its return value is used as the
324 * substitution string.
326 * If @s is a hash, the matched string is used as a key to it and
327 * its value used as the substitution.
329 * If @r is NULL, returns the number of substitutions made in the
330 * previous call to mpdm_sregex() (can be zero if none was done).
332 * The global variables @mpdm_regex_offset and @mpdm_regex_size are
333 * set to the offset of the matched string and the size of the
334 * replaced string, respectively.
336 * Returns the modified string, or the original one if no substitutions
337 * were done.
338 * [Regular Expressions]
340 mpdm_t mpdm_sregex(mpdm_t r, const mpdm_t v, const mpdm_t s, int offset)
342 mpdm_t cr;
343 mpdm_t o = v;
345 if (r == NULL) {
346 /* return last count */
347 return MPDM_I(mpdm_sregex_count);
350 if (v == NULL)
351 return NULL;
353 /* compile the regex */
354 if ((cr = mpdm_regcomp(r)) != NULL) {
355 char *ptr;
356 int f, i = 0;
357 wchar_t *global;
358 mpdm_t t;
360 /* takes pointer to global flag */
361 if ((global = regex_flags(r)) !=NULL)
362 global = wcschr(global, 'g');
364 /* store the first part */
365 o = MPDM_NS(v->data, offset);
367 /* convert to mbs */
368 if ((ptr = mpdm_wcstombs((wchar_t *) v->data + offset, NULL)) == NULL)
369 return NULL;
371 /* reset count */
372 mpdm_sregex_count = 0;
373 mpdm_regex_offset = -1;
375 do {
376 regmatch_t rm;
378 /* try match */
379 f = !regexec((regex_t *) cr->data, ptr + i,
380 1, &rm, offset > 0 ? REG_NOTBOL : 0);
382 if (f) {
383 /* creates a string from the beginning
384 to the start of the match */
385 t = MPDM_NMBS(ptr + i, rm.rm_so);
386 o = mpdm_strcat(o, t);
388 /* store offset of substitution */
389 mpdm_regex_offset = mpdm_size(t) + offset;
391 /* get the matched part */
392 t = MPDM_NMBS(ptr + i + rm.rm_so, rm.rm_eo - rm.rm_so);
394 /* is s an executable value? */
395 if (MPDM_IS_EXEC(s)) {
396 /* protect o from sweeping */
397 mpdm_ref(o);
399 /* execute s, with t as argument */
400 t = mpdm_exec_1(s, t);
402 mpdm_unref(o);
404 else
405 /* is s a hash? use match as key */
406 if (MPDM_IS_HASH(s))
407 t = mpdm_hget(s, t);
408 else
409 t = expand_ampersands(s, t);
411 /* appends the substitution string */
412 o = mpdm_strcat(o, t);
414 /* store size of substitution */
415 mpdm_regex_size = mpdm_size(t);
417 i += rm.rm_eo;
419 /* one more substitution */
420 mpdm_sregex_count++;
423 } while (f && global);
425 /* no (more) matches; convert and append the rest */
426 t = MPDM_MBS(ptr + i);
427 o = mpdm_strcat(o, t);
429 free(ptr);
432 return o;