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
;
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
) {
89 /* not found; regex must be compiled */
92 rmb
= mpdm_ref(MPDM_2MBS(r
->data
));
93 regex
= (char *) rmb
->data
;
95 if ((flags
= strrchr(regex
, *regex
)) != NULL
) {
97 if (strchr(flags
, 'i') != NULL
)
99 if (strchr(flags
, 'm') != NULL
)
105 if (!regcomp(&re
, regex
, f
)) {
106 c
= MPDM_C(MPDM_REGEX
, &re
, sizeof(regex_t
));
107 mpdm_hset(regex_cache
, r
, c
);
120 static mpdm_t
regex1(mpdm_t r
, const mpdm_t v
, int offset
)
121 /* test for one regex */
129 /* no matching yet */
130 mpdm_regex_offset
= -1;
132 /* compile the regex */
133 if ((cr
= mpdm_regcomp(r
)) != NULL
) {
140 /* takes pointer to 'last' flag */
141 if ((last
= regex_flags(r
)) != NULL
)
142 last
= wcschr(last
, 'l');
145 ptr
= mpdm_wcstombs((wchar_t *) v
->data
+ offset
, NULL
);
148 while (regexec((regex_t
*) cr
->data
, ptr
+ o
, 1,
149 &rm
, offset
> 0 ? REG_NOTBOL
: 0) == 0) {
152 /* if 'last' is not set, it's done */
162 /* converts to mbs the string from the beginning
163 to the start of the match, just to know
164 the size (and immediately frees it) */
165 free(mpdm_mbstowcs(ptr
, &mpdm_regex_offset
, rm
.rm_so
));
168 mpdm_regex_offset
+= offset
;
170 /* create now the matching string */
171 w
= MPDM_NMBS(ptr
+ rm
.rm_so
, rm
.rm_eo
- rm
.rm_so
);
173 /* and store the size */
174 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
)
226 /* special case: if r is NULL, return previous match */
228 /* if previous regex was successful... */
229 if (mpdm_regex_offset
!= -1) {
233 mpdm_aset(w
, MPDM_I(mpdm_regex_offset
), 0);
234 mpdm_aset(w
, MPDM_I(mpdm_regex_size
), 1);
240 if (r
->flags
& MPDM_MULTIPLE
) {
244 /* multiple value; try sequentially all regexes,
245 moving the offset forward */
250 for (n
= 0; n
< mpdm_size(r
); n
++) {
251 t
= mpdm_regex(mpdm_aget(r
, n
), v
, offset
);
256 /* found; store and move forward */
258 offset
= mpdm_regex_offset
+ mpdm_regex_size
;
266 /* takes pointer to 'global' flag */
267 if ((global
= regex_flags(r
)) != NULL
)
268 global
= wcschr(global
, 'g');
270 if (global
!= NULL
) {
273 /* match sequentially until done */
277 while ((t
= regex1(r
, v
, offset
)) != NULL
) {
280 offset
= mpdm_regex_offset
+ mpdm_regex_size
;
286 w
= regex1(r
, v
, offset
);
297 static mpdm_t
expand_ampersands(const mpdm_t s
, const mpdm_t t
)
298 /* substitutes all unescaped ampersands in s with t */
300 const wchar_t *sptr
= mpdm_string(s
);
302 wchar_t *optr
= NULL
;
310 while ((wptr
= wcschr(sptr
, L
'\\')) != NULL
||
311 (wptr
= wcschr(sptr
, L
'&')) != NULL
) {
314 /* add the leading part */
315 optr
= mpdm_pokewsn(optr
, &osize
, sptr
, n
);
317 if (*wptr
== L
'\\') {
318 if (*(wptr
+ 1) == L
'&' || *(wptr
+ 1) == L
'\\')
321 optr
= mpdm_pokewsn(optr
, &osize
, wptr
, 1);
325 optr
= mpdm_pokev(optr
, &osize
, t
);
330 /* add the rest of the string */
331 optr
= mpdm_pokews(optr
, &osize
, sptr
);
332 optr
= mpdm_pokewsn(optr
, &osize
, L
"", 1);
333 r
= MPDM_ENS(optr
, osize
- 1);
344 * mpdm_sregex - Matches and substitutes a regular expression.
345 * @r: the regular expression
346 * @v: the value to be matched
347 * @s: the substitution string, hash or code
348 * @offset: offset from the start of v->data
350 * Matches a regular expression against a value, and substitutes the
351 * found substring with @s. Valid flags are 'i', for case-insensitive
352 * matching, and 'g', for global replacements (all ocurrences in @v
353 * will be replaced, instead of just the first found one).
355 * If @s is executable, it's executed with the matched part as
356 * the only argument and its return value is used as the
357 * substitution string.
359 * If @s is a hash, the matched string is used as a key to it and
360 * its value used as the substitution. If this value itself is
361 * executable, it's executed with the matched string as its only
362 * argument and its return value used as the substitution.
364 * If @r is NULL, returns the number of substitutions made in the
365 * previous call to mpdm_sregex() (can be zero if none was done).
367 * The global variables @mpdm_regex_offset and @mpdm_regex_size are
368 * set to the offset of the matched string and the size of the
369 * replaced string, respectively.
371 * Always returns a new string (either modified or an exact copy).
372 * [Regular Expressions]
374 mpdm_t
mpdm_sregex(mpdm_t r
, const mpdm_t v
, const mpdm_t s
, int offset
)
377 wchar_t *optr
= NULL
;
386 /* return last count */
387 o
= MPDM_I(mpdm_sregex_count
);
391 /* compile the regex */
392 if ((cr
= mpdm_regcomp(r
)) != NULL
) {
398 /* takes pointer to global flag */
399 if ((global
= regex_flags(r
)) !=NULL
)
400 global
= wcschr(global
, 'g');
402 /* store the first part */
403 optr
= mpdm_pokewsn(optr
, &osize
, v
->data
, offset
);
406 ptr
= mpdm_wcstombs((wchar_t *) v
->data
+ offset
, NULL
);
409 mpdm_sregex_count
= 0;
410 mpdm_regex_offset
= -1;
416 f
= !regexec((regex_t
*) cr
->data
, ptr
+ i
,
417 1, &rm
, offset
> 0 ? REG_NOTBOL
: 0);
420 /* creates a string from the beginning
421 to the start of the match */
422 t
= mpdm_ref(MPDM_NMBS(ptr
+ i
, rm
.rm_so
));
423 optr
= mpdm_pokev(optr
, &osize
, t
);
425 /* store offset of substitution */
426 mpdm_regex_offset
= mpdm_size(t
) + offset
;
430 /* get the matched part */
431 t
= MPDM_NMBS(ptr
+ i
+ rm
.rm_so
, rm
.rm_eo
- rm
.rm_so
);
433 /* is s an executable value? */
434 if (MPDM_IS_EXEC(s
)) {
435 /* execute s, with t as argument */
436 t
= mpdm_exec_1(s
, t
, NULL
);
439 /* is s a hash? use match as key */
440 if (MPDM_IS_HASH(s
)) {
446 /* is the value executable? */
447 if (MPDM_IS_EXEC(v
)) {
448 mpdm_t w
= mpdm_ref(v
);
450 v
= mpdm_exec_1(w
, t
, NULL
);
460 t
= expand_ampersands(s
, t
);
462 /* appends the substitution string */
465 optr
= mpdm_pokev(optr
, &osize
, t
);
467 /* store size of substitution */
468 mpdm_regex_size
= mpdm_size(t
);
474 /* one more substitution */
477 } while (f
&& global
);
479 /* no (more) matches; convert and append the rest */
480 t
= MPDM_MBS(ptr
+ i
);
481 optr
= mpdm_pokev(optr
, &osize
, t
);
487 optr
= mpdm_pokewsn(optr
, &osize
, L
"", 1);
489 o
= MPDM_ENS(optr
, osize
- 1);