3 MPDM - Minimum Profit Data Manager
4 Copyright (C) 2003/2010 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
36 #include <pcreposix.h>
39 #ifdef CONFOPT_SYSTEM_REGEX
43 #ifdef CONFOPT_INCLUDED_REGEX
44 #include "gnu_regex.h"
50 /* matching of the last regex */
52 int mpdm_regex_offset
= -1;
53 int mpdm_regex_size
= 0;
55 /* number of substitutions in last sregex */
57 int mpdm_sregex_count
= 0;
62 static wchar_t *regex_flags(const mpdm_t r
)
64 return wcsrchr((wchar_t *) r
->data
, *(wchar_t *) r
->data
);
68 static mpdm_t
mpdm_regcomp(mpdm_t r
)
71 mpdm_t regex_cache
= NULL
;
73 /* if cache does not exist, create it */
74 if ((regex_cache
= mpdm_hget_s(mpdm_root(), L
"__REGEX_CACHE__")) == NULL
) {
75 regex_cache
= MPDM_H(0);
76 mpdm_hset_s(mpdm_root(), L
"__REGEX_CACHE__", regex_cache
);
79 /* search the regex in the cache */
80 if ((c
= mpdm_hget(regex_cache
, r
)) == NULL
) {
87 /* not found; regex must be compiled */
90 rmb
= mpdm_ref(MPDM_2MBS(r
->data
));
91 regex
= (char *) rmb
->data
;
93 if ((flags
= strrchr(regex
, *regex
)) != NULL
) {
95 if (strchr(flags
, 'i') != NULL
)
97 if (strchr(flags
, 'm') != NULL
)
103 if (!regcomp(&re
, regex
, f
)) {
106 if ((ptr
= malloc(sizeof(regex_t
))) != NULL
) {
108 memcpy(ptr
, &re
, sizeof(regex_t
));
111 c
= mpdm_new(MPDM_FREE
, ptr
, sizeof(regex_t
));
114 mpdm_hset(regex_cache
, r
, c
);
126 static mpdm_t
regex1(mpdm_t r
, const mpdm_t v
, int offset
)
127 /* test for one regex */
132 /* no matching yet */
133 mpdm_regex_offset
= -1;
135 /* compile the regex */
136 if ((cr
= mpdm_regcomp(r
)) != NULL
) {
143 /* takes pointer to 'last' flag */
144 if ((last
= regex_flags(r
)) != NULL
)
145 last
= wcschr(last
, 'l');
148 ptr
= mpdm_wcstombs((wchar_t *) v
->data
+ offset
, NULL
);
151 while (regexec((regex_t
*) cr
->data
, ptr
+ o
, 1,
152 &rm
, offset
> 0 ? REG_NOTBOL
: 0) == 0) {
155 /* if 'last' is not set, it's done */
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
));
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
);
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, 'l', to return the last matching instead of the
198 * first one, or 'g', to match globally; in that last case, an array
199 * containing all matches is returned instead of a string scalar.
201 * If @r is a string, an ordinary regular expression matching is tried
202 * over the @v string. If the matching is possible, the match result
203 * is returned, or NULL otherwise.
205 * If @r is an array (of strings), each element is tried sequentially
206 * as an individual regular expression over the @v string, each one using
207 * the offset returned by the previous match. All regular expressions
208 * must match to be successful. If this is the case, an array (with
209 * the same number of arguments) is returned containing the matched
210 * strings, or NULL otherwise.
212 * If @r is NULL, the result of the previous regex matching
213 * is returned as a two element array. The first element will contain
214 * the character offset of the matching and the second the number of
215 * characters matched. If the previous regex was unsuccessful, NULL
217 * [Regular Expressions]
219 mpdm_t
mpdm_regex(mpdm_t r
, const mpdm_t v
, int offset
)
223 /* special case: if r is NULL, return previous match */
225 /* if previous regex was successful... */
226 if (mpdm_regex_offset
!= -1) {
229 mpdm_aset(w
, MPDM_I(mpdm_regex_offset
), 0);
230 mpdm_aset(w
, MPDM_I(mpdm_regex_size
), 1);
236 /* if the string to be tested is NULL, return NULL */
240 if (r
->flags
& MPDM_MULTIPLE
) {
244 /* multiple value; try sequentially all regexes,
245 moving the offset forward */
249 for (n
= 0; n
< mpdm_size(r
); n
++) {
250 t
= mpdm_regex(mpdm_aget(r
, n
), v
, offset
);
255 /* found; store and move forward */
257 offset
= mpdm_regex_offset
+ mpdm_regex_size
;
263 /* takes pointer to 'global' flag */
264 if ((global
= regex_flags(r
)) != NULL
)
265 global
= wcschr(global
, 'g');
267 if (global
!= NULL
) {
270 /* match sequentially until done */
273 while ((t
= regex1(r
, v
, offset
)) != NULL
) {
276 offset
= mpdm_regex_offset
+ mpdm_regex_size
;
280 w
= regex1(r
, v
, offset
);
287 static mpdm_t
expand_ampersands(const mpdm_t s
, const mpdm_t t
)
288 /* substitutes all unescaped ampersands in s with t */
290 const wchar_t *sptr
= mpdm_string(s
);
292 wchar_t *optr
= NULL
;
299 while ((wptr
= wcschr(sptr
, L
'\\')) != NULL
||
300 (wptr
= wcschr(sptr
, L
'&')) != NULL
) {
303 /* add the leading part */
304 optr
= mpdm_pokewsn(optr
, &osize
, sptr
, n
);
306 if (*wptr
== L
'\\') {
307 if (*(wptr
+ 1) == L
'&' || *(wptr
+ 1) == L
'\\')
310 optr
= mpdm_pokewsn(optr
, &osize
, wptr
, 1);
314 optr
= mpdm_pokev(optr
, &osize
, t
);
319 /* add the rest of the string */
320 optr
= mpdm_pokews(optr
, &osize
, sptr
);
321 optr
= mpdm_pokewsn(optr
, &osize
, L
"", 1);
322 r
= MPDM_ENS(optr
, osize
- 1);
329 * mpdm_sregex - Matches and substitutes a regular expression.
330 * @r: the regular expression
331 * @v: the value to be matched
332 * @s: the substitution string, hash or code
333 * @offset: offset from the start of v->data
335 * Matches a regular expression against a value, and substitutes the
336 * found substring with @s. Valid flags are 'i', for case-insensitive
337 * matching, and 'g', for global replacements (all ocurrences in @v
338 * will be replaced, instead of just the first found one).
340 * If @s is executable, it's executed with the matched part as
341 * the only argument and its return value is used as the
342 * substitution string.
344 * If @s is a hash, the matched string is used as a key to it and
345 * its value used as the substitution. If this value itself is
346 * executable, it's executed with the matched string as its only
347 * argument and its return value used as the substitution.
349 * If @r is NULL, returns the number of substitutions made in the
350 * previous call to mpdm_sregex() (can be zero if none was done).
352 * The global variables @mpdm_regex_offset and @mpdm_regex_size are
353 * set to the offset of the matched string and the size of the
354 * replaced string, respectively.
356 * Always returns a new string (either modified or an exact copy).
357 * [Regular Expressions]
359 mpdm_t
mpdm_sregex(mpdm_t r
, const mpdm_t v
, const mpdm_t s
, int offset
)
362 wchar_t *optr
= NULL
;
366 /* return last count */
367 return MPDM_I(mpdm_sregex_count
);
373 /* compile the regex */
374 if ((cr
= mpdm_regcomp(r
)) != NULL
) {
380 /* takes pointer to global flag */
381 if ((global
= regex_flags(r
)) !=NULL
)
382 global
= wcschr(global
, 'g');
384 /* store the first part */
385 optr
= mpdm_pokewsn(optr
, &osize
, v
->data
, offset
);
388 if ((ptr
= mpdm_wcstombs((wchar_t *) v
->data
+ offset
, NULL
)) == NULL
)
392 mpdm_sregex_count
= 0;
393 mpdm_regex_offset
= -1;
399 f
= !regexec((regex_t
*) cr
->data
, ptr
+ i
,
400 1, &rm
, offset
> 0 ? REG_NOTBOL
: 0);
403 /* creates a string from the beginning
404 to the start of the match */
405 t
= mpdm_ref(MPDM_NMBS(ptr
+ i
, rm
.rm_so
));
406 optr
= mpdm_pokev(optr
, &osize
, t
);
408 /* store offset of substitution */
409 mpdm_regex_offset
= mpdm_size(t
) + offset
;
413 /* get the matched part */
414 t
= MPDM_NMBS(ptr
+ i
+ rm
.rm_so
, rm
.rm_eo
- rm
.rm_so
);
416 /* is s an executable value? */
417 if (MPDM_IS_EXEC(s
)) {
418 mpdm_t v
= mpdm_ref(t
);
420 /* execute s, with t as argument */
421 t
= mpdm_exec_1(s
, v
);
426 /* is s a hash? use match as key */
427 if (MPDM_IS_HASH(s
)) {
428 mpdm_t v
= mpdm_hget(s
, t
);
432 /* is the value executable? */
433 if (MPDM_IS_EXEC(v
)) {
434 mpdm_t w
= mpdm_ref(v
);
436 v
= mpdm_exec_1(w
, t
);
446 mpdm_t v
= mpdm_ref(t
);
447 t
= expand_ampersands(s
, v
);
451 /* appends the substitution string */
454 optr
= mpdm_pokev(optr
, &osize
, t
);
456 /* store size of substitution */
457 mpdm_regex_size
= mpdm_size(t
);
463 /* one more substitution */
467 } while (f
&& global
);
469 /* no (more) matches; convert and append the rest */
470 t
= mpdm_ref(MPDM_MBS(ptr
+ i
));
471 optr
= mpdm_pokev(optr
, &osize
, t
);
478 optr
= mpdm_pokewsn(optr
, &osize
, L
"", 1);
480 return MPDM_ENS(optr
, osize
- 1);