Some fixes to previous indentation.
[mpdm.git] / mpdm_r.c
blob42dbfd7c836f26f9edd6caa382076fad25c91fd8
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 =
77 mpdm_hget_s(mpdm_root(), L"__REGEX_CACHE__")) == NULL) {
78 regex_cache = MPDM_H(0);
79 mpdm_hset_s(mpdm_root(), L"__REGEX_CACHE__", regex_cache);
82 /* search the regex in the cache */
83 if ((c = mpdm_hget(regex_cache, r)) == NULL) {
84 mpdm_t rmb;
85 regex_t re;
86 char *regex;
87 char *flags;
88 int f = REG_EXTENDED;
90 /* not found; regex must be compiled */
92 /* convert to mbs */
93 rmb = mpdm_ref(MPDM_2MBS(r->data));
94 regex = (char *) rmb->data;
96 if ((flags = strrchr(regex, *regex)) != 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 c = MPDM_C(MPDM_REGEX, &re, sizeof(regex_t));
108 mpdm_hset(regex_cache, r, c);
112 mpdm_unref(rmb);
115 mpdm_unref(r);
117 return c;
121 static mpdm_t regex1(mpdm_t r, const mpdm_t v, int offset)
122 /* test for one regex */
124 mpdm_t w = NULL;
125 mpdm_t cr;
127 mpdm_ref(r);
128 mpdm_ref(v);
130 /* no matching yet */
131 mpdm_regex_offset = -1;
133 /* compile the regex */
134 if ((cr = mpdm_regcomp(r)) != NULL) {
135 regmatch_t rm;
136 char *ptr;
137 wchar_t *last;
138 int o = 0;
139 int f = 0;
141 /* takes pointer to 'last' flag */
142 if ((last = regex_flags(r)) != NULL)
143 last = wcschr(last, 'l');
145 /* convert to mbs */
146 ptr = mpdm_wcstombs((wchar_t *) v->data + offset, NULL);
148 /* match? */
149 while (regexec((regex_t *) cr->data, ptr + o, 1,
150 &rm, offset > 0 ? REG_NOTBOL : 0) == 0) {
151 f++;
153 /* if 'last' is not set, it's done */
154 if (last == NULL)
155 break;
157 rm.rm_so += o;
158 rm.rm_eo += o;
159 o = rm.rm_eo;
162 if (f) {
163 /* converts to mbs the string from the beginning
164 to the start of the match, just to know
165 the size (and immediately frees it) */
166 free(mpdm_mbstowcs(ptr, &mpdm_regex_offset, rm.rm_so));
168 /* add the offset */
169 mpdm_regex_offset += offset;
171 /* create now the matching string */
172 w = MPDM_NMBS(ptr + rm.rm_so, rm.rm_eo - rm.rm_so);
174 /* and store the size */
175 mpdm_regex_size = mpdm_size(w);
178 free(ptr);
181 mpdm_unref(v);
182 mpdm_unref(r);
184 return w;
189 * mpdm_regex - Matches a regular expression.
190 * @r: the regular expression
191 * @v: the value to be matched
192 * @offset: offset from the start of v->data
194 * Matches a regular expression against a value. Valid flags are 'i',
195 * for case-insensitive matching, 'm', to treat the string as a
196 * multiline string (i.e., one containing newline characters), so
197 * that ^ and $ match the boundaries of each line instead of the
198 * whole string, 'l', to return the last matching instead of the
199 * first one, or 'g', to match globally; in that last case, an array
200 * containing all matches is returned instead of a string scalar.
202 * If @r is a string, an ordinary regular expression matching is tried
203 * over the @v string. If the matching is possible, the match result
204 * is returned, or NULL otherwise.
206 * If @r is an array (of strings), each element is tried sequentially
207 * as an individual regular expression over the @v string, each one using
208 * the offset returned by the previous match. All regular expressions
209 * must match to be successful. If this is the case, an array (with
210 * the same number of arguments) is returned containing the matched
211 * strings, or NULL otherwise.
213 * If @r is NULL, the result of the previous regex matching
214 * is returned as a two element array. The first element will contain
215 * the character offset of the matching and the second the number of
216 * characters matched. If the previous regex was unsuccessful, NULL
217 * is returned.
218 * [Regular Expressions]
220 mpdm_t mpdm_regex(mpdm_t r, const mpdm_t v, int offset)
222 mpdm_t w = NULL;
224 mpdm_ref(r);
225 mpdm_ref(v);
227 /* special case: if r is NULL, return previous match */
228 if (r == NULL) {
229 /* if previous regex was successful... */
230 if (mpdm_regex_offset != -1) {
231 w = MPDM_A(2);
233 mpdm_ref(w);
234 mpdm_aset(w, MPDM_I(mpdm_regex_offset), 0);
235 mpdm_aset(w, MPDM_I(mpdm_regex_size), 1);
236 mpdm_unrefnd(w);
239 else
240 if (v != NULL) {
241 if (r->flags & MPDM_MULTIPLE) {
242 int n;
243 mpdm_t t;
245 /* multiple value; try sequentially all regexes,
246 moving the offset forward */
248 w = MPDM_A(0);
249 mpdm_ref(w);
251 for (n = 0; n < mpdm_size(r); n++) {
252 t = mpdm_regex(mpdm_aget(r, n), v, offset);
254 if (t == NULL)
255 break;
257 /* found; store and move forward */
258 mpdm_push(w, t);
259 offset = mpdm_regex_offset + mpdm_regex_size;
262 mpdm_unrefnd(w);
264 else {
265 wchar_t *global;
267 /* takes pointer to 'global' flag */
268 if ((global = regex_flags(r)) !=NULL)
269 global = wcschr(global, 'g');
271 if (global !=NULL) {
272 mpdm_t t;
274 /* match sequentially until done */
275 w = MPDM_A(0);
276 mpdm_ref(w);
278 while ((t = regex1(r, v, offset)) != NULL) {
279 mpdm_push(w, t);
281 offset = mpdm_regex_offset + mpdm_regex_size;
284 mpdm_unrefnd(w);
286 else
287 w = regex1(r, v, offset);
291 mpdm_unref(v);
292 mpdm_unref(r);
294 return w;
298 static mpdm_t expand_ampersands(const mpdm_t s, const mpdm_t t)
299 /* substitutes all unescaped ampersands in s with t */
301 const wchar_t *sptr = mpdm_string(s);
302 wchar_t *wptr;
303 wchar_t *optr = NULL;
304 int osize = 0;
305 mpdm_t r = NULL;
307 mpdm_ref(s);
308 mpdm_ref(t);
310 if (s != NULL) {
311 while ((wptr = wcschr(sptr, L'\\')) != NULL ||
312 (wptr = wcschr(sptr, L'&')) != NULL) {
313 int n = wptr - sptr;
315 /* add the leading part */
316 optr = mpdm_pokewsn(optr, &osize, sptr, n);
318 if (*wptr == L'\\') {
319 if (*(wptr + 1) == L'&' || *(wptr + 1) == L'\\')
320 wptr++;
322 optr = mpdm_pokewsn(optr, &osize, wptr, 1);
324 else
325 if (*wptr == '&')
326 optr = mpdm_pokev(optr, &osize, t);
328 sptr = wptr + 1;
331 /* add the rest of the string */
332 optr = mpdm_pokews(optr, &osize, sptr);
333 optr = mpdm_pokewsn(optr, &osize, L"", 1);
334 r = MPDM_ENS(optr, osize - 1);
337 mpdm_unref(t);
338 mpdm_unref(s);
340 return r;
345 * mpdm_sregex - Matches and substitutes a regular expression.
346 * @r: the regular expression
347 * @v: the value to be matched
348 * @s: the substitution string, hash or code
349 * @offset: offset from the start of v->data
351 * Matches a regular expression against a value, and substitutes the
352 * found substring with @s. Valid flags are 'i', for case-insensitive
353 * matching, and 'g', for global replacements (all ocurrences in @v
354 * will be replaced, instead of just the first found one).
356 * If @s is executable, it's executed with the matched part as
357 * the only argument and its return value is used as the
358 * substitution string.
360 * If @s is a hash, the matched string is used as a key to it and
361 * its value used as the substitution. If this value itself is
362 * executable, it's executed with the matched string as its only
363 * argument and its return value used as the substitution.
365 * If @r is NULL, returns the number of substitutions made in the
366 * previous call to mpdm_sregex() (can be zero if none was done).
368 * The global variables @mpdm_regex_offset and @mpdm_regex_size are
369 * set to the offset of the matched string and the size of the
370 * replaced string, respectively.
372 * Always returns a new string (either modified or an exact copy).
373 * [Regular Expressions]
375 mpdm_t mpdm_sregex(mpdm_t r, const mpdm_t v, const mpdm_t s, int offset)
377 mpdm_t cr;
378 wchar_t *optr = NULL;
379 int osize = 0;
380 mpdm_t o = NULL;
382 mpdm_ref(r);
383 mpdm_ref(v);
384 mpdm_ref(s);
386 if (r == NULL) {
387 /* return last count */
388 o = MPDM_I(mpdm_sregex_count);
390 else
391 if (v != NULL) {
392 /* compile the regex */
393 if ((cr = mpdm_regcomp(r)) != NULL) {
394 char *ptr;
395 int f, i = 0;
396 wchar_t *global;
397 mpdm_t t;
399 /* takes pointer to global flag */
400 if ((global = regex_flags(r)) !=NULL)
401 global = wcschr(global, 'g');
403 /* store the first part */
404 optr = mpdm_pokewsn(optr, &osize, v->data, offset);
406 /* convert to mbs */
407 ptr = mpdm_wcstombs((wchar_t *) v->data + offset, NULL);
409 /* reset count */
410 mpdm_sregex_count = 0;
411 mpdm_regex_offset = -1;
413 do {
414 regmatch_t rm;
416 /* try match */
417 f = !regexec((regex_t *) cr->data, ptr + i,
418 1, &rm, offset > 0 ? REG_NOTBOL : 0);
420 if (f) {
421 /* creates a string from the beginning
422 to the start of the match */
423 t = mpdm_ref(MPDM_NMBS(ptr + i, rm.rm_so));
424 optr = mpdm_pokev(optr, &osize, t);
426 /* store offset of substitution */
427 mpdm_regex_offset = mpdm_size(t) + offset;
429 mpdm_unref(t);
431 /* get the matched part */
432 t = MPDM_NMBS(ptr + i + rm.rm_so, rm.rm_eo - rm.rm_so);
434 /* is s an executable value? */
435 if (MPDM_IS_EXEC(s)) {
436 /* execute s, with t as argument */
437 t = mpdm_exec_1(s, t, NULL);
439 else
440 /* is s a hash? use match as key */
441 if (MPDM_IS_HASH(s)) {
442 mpdm_t v;
444 mpdm_ref(t);
445 v = mpdm_hget(s, t);
447 /* is the value executable? */
448 if (MPDM_IS_EXEC(v)) {
449 mpdm_t w = mpdm_ref(v);
451 v = mpdm_exec_1(w, t, NULL);
453 mpdm_unref(w);
456 mpdm_unref(t);
458 t = v;
460 else
461 t = expand_ampersands(s, t);
463 /* appends the substitution string */
464 mpdm_ref(t);
466 optr = mpdm_pokev(optr, &osize, t);
468 /* store size of substitution */
469 mpdm_regex_size = mpdm_size(t);
471 mpdm_unref(t);
473 i += rm.rm_eo;
475 /* one more substitution */
476 mpdm_sregex_count++;
478 } while (f && global);
480 /* no (more) matches; convert and append the rest */
481 t = MPDM_MBS(ptr + i);
482 optr = mpdm_pokev(optr, &osize, t);
484 free(ptr);
487 /* NULL-terminate */
488 optr = mpdm_pokewsn(optr, &osize, L"", 1);
490 o = MPDM_ENS(optr, osize - 1);
493 mpdm_unref(s);
494 mpdm_unref(v);
495 mpdm_unref(r);
497 return o;