Fixed mpdm_sregex().
[mpdm.git] / mpdm_r.c
blob8a4758c10f5db0eb96bb9e3c9c3ab853a5e33cb0
1 /*
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
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
48 /** data **/
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;
60 /** code **/
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)
70 mpdm_t c = NULL;
71 mpdm_t regex_cache = NULL;
73 mpdm_ref(r);
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_ref(MPDM_2MBS(r->data));
93 regex = (char *) rmb->data;
95 if ((flags = strrchr(regex, *regex)) != NULL) {
97 if (strchr(flags, 'i') != NULL)
98 f |= REG_ICASE;
99 if (strchr(flags, 'm') != NULL)
100 f |= REG_NEWLINE;
102 regex++;
103 *flags = '\0';
105 if (!regcomp(&re, regex, f)) {
106 c = MPDM_C(MPDM_REGEX, &re, sizeof(regex_t));
107 mpdm_hset(regex_cache, r, c);
111 mpdm_unref(rmb);
114 mpdm_unref(r);
116 return c;
120 static mpdm_t regex1(mpdm_t r, const mpdm_t v, int offset)
121 /* test for one regex */
123 mpdm_t w = NULL;
124 mpdm_t cr;
126 mpdm_ref(r);
127 mpdm_ref(v);
129 /* no matching yet */
130 mpdm_regex_offset = -1;
132 /* compile the regex */
133 if ((cr = mpdm_regcomp(r)) != NULL) {
134 regmatch_t rm;
135 char *ptr;
136 wchar_t *last;
137 int o = 0;
138 int f = 0;
140 /* takes pointer to 'last' flag */
141 if ((last = regex_flags(r)) != NULL)
142 last = wcschr(last, 'l');
144 /* convert to mbs */
145 ptr = mpdm_wcstombs((wchar_t *) v->data + offset, NULL);
147 /* match? */
148 while (regexec((regex_t *) cr->data, ptr + o, 1,
149 &rm, offset > 0 ? REG_NOTBOL : 0) == 0) {
150 f++;
152 /* if 'last' is not set, it's done */
153 if (last == NULL)
154 break;
156 rm.rm_so += o;
157 rm.rm_eo += o;
158 o = rm.rm_eo;
161 if (f) {
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));
167 /* add the offset */
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);
177 free(ptr);
180 mpdm_unref(v);
181 mpdm_unref(r);
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, '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
216 * is returned.
217 * [Regular Expressions]
219 mpdm_t mpdm_regex(mpdm_t r, const mpdm_t v, int offset)
221 mpdm_t w = NULL;
223 mpdm_ref(r);
224 mpdm_ref(v);
226 /* special case: if r is NULL, return previous match */
227 if (r == NULL) {
228 /* if previous regex was successful... */
229 if (mpdm_regex_offset != -1) {
230 w = MPDM_A(2);
232 mpdm_ref(w);
233 mpdm_aset(w, MPDM_I(mpdm_regex_offset), 0);
234 mpdm_aset(w, MPDM_I(mpdm_regex_size), 1);
235 mpdm_unrefnd(w);
238 else
239 if (v != NULL) {
240 if (r->flags & MPDM_MULTIPLE) {
241 int n;
242 mpdm_t t;
244 /* multiple value; try sequentially all regexes,
245 moving the offset forward */
247 w = MPDM_A(0);
248 mpdm_ref(w);
250 for (n = 0; n < mpdm_size(r); n++) {
251 t = mpdm_regex(mpdm_aget(r, n), v, offset);
253 if (t == NULL)
254 break;
256 /* found; store and move forward */
257 mpdm_push(w, t);
258 offset = mpdm_regex_offset + mpdm_regex_size;
261 mpdm_unrefnd(w);
263 else {
264 wchar_t *global;
266 /* takes pointer to 'global' flag */
267 if ((global = regex_flags(r)) != NULL)
268 global = wcschr(global, 'g');
270 if (global != NULL) {
271 mpdm_t t;
273 /* match sequentially until done */
274 w = MPDM_A(0);
275 mpdm_ref(w);
277 while ((t = regex1(r, v, offset)) != NULL) {
278 mpdm_push(w, t);
280 offset = mpdm_regex_offset + mpdm_regex_size;
283 mpdm_unrefnd(w);
285 else
286 w = regex1(r, v, offset);
290 mpdm_unref(v);
291 mpdm_unref(r);
293 return w;
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);
301 wchar_t *wptr;
302 wchar_t *optr = NULL;
303 int osize = 0;
304 mpdm_t r = NULL;
306 mpdm_ref(s);
307 mpdm_ref(t);
309 if (s != NULL) {
310 while ((wptr = wcschr(sptr, L'\\')) != NULL ||
311 (wptr = wcschr(sptr, L'&')) != NULL) {
312 int n = wptr - sptr;
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'\\')
319 wptr++;
321 optr = mpdm_pokewsn(optr, &osize, wptr, 1);
323 else
324 if (*wptr == '&')
325 optr = mpdm_pokev(optr, &osize, t);
327 sptr = wptr + 1;
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);
336 mpdm_unref(t);
337 mpdm_unref(s);
339 return r;
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)
376 mpdm_t cr;
377 wchar_t *optr = NULL;
378 int osize = 0;
379 mpdm_t o = NULL;
381 mpdm_ref(r);
382 mpdm_ref(v);
383 mpdm_ref(s);
385 if (r == NULL) {
386 /* return last count */
387 o = MPDM_I(mpdm_sregex_count);
389 else
390 if (v != NULL) {
391 /* compile the regex */
392 if ((cr = mpdm_regcomp(r)) != NULL) {
393 char *ptr;
394 int f, i = 0;
395 wchar_t *global;
396 mpdm_t t;
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);
405 /* convert to mbs */
406 ptr = mpdm_wcstombs((wchar_t *) v->data + offset, NULL);
408 /* reset count */
409 mpdm_sregex_count = 0;
410 mpdm_regex_offset = -1;
412 do {
413 regmatch_t rm;
415 /* try match */
416 f = !regexec((regex_t *) cr->data, ptr + i,
417 1, &rm, offset > 0 ? REG_NOTBOL : 0);
419 if (f) {
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;
428 mpdm_unref(t);
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);
438 else
439 /* is s a hash? use match as key */
440 if (MPDM_IS_HASH(s)) {
441 mpdm_t v;
443 mpdm_ref(t);
444 v = mpdm_hget(s, t);
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);
452 mpdm_unref(w);
455 mpdm_unref(t);
457 t = v;
459 else
460 t = expand_ampersands(s, t);
462 /* appends the substitution string */
463 mpdm_ref(t);
465 optr = mpdm_pokev(optr, &osize, t);
467 /* store size of substitution */
468 mpdm_regex_size = mpdm_size(t);
470 mpdm_unref(t);
472 i += rm.rm_eo;
474 /* one more substitution */
475 mpdm_sregex_count++;
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);
483 free(ptr);
486 /* NULL-terminate */
487 optr = mpdm_pokewsn(optr, &osize, L"", 1);
489 o = MPDM_ENS(optr, osize - 1);
492 mpdm_unref(s);
493 mpdm_unref(v);
494 mpdm_unref(r);
496 return o;