Merge branch 'vendor/ELFTOOLCHAIN'
[dragonfly.git] / contrib / tre / lib / regexec.c
1 /*
2   tre_regexec.c - TRE POSIX compatible matching functions (and more).
3
4   This software is released under a BSD-style license.
5   See the file LICENSE for details and copyright.
6
7 */
8
9 #ifdef HAVE_CONFIG_H
10 #include <config.h>
11 #endif /* HAVE_CONFIG_H */
12
13 #ifdef TRE_USE_ALLOCA
14 /* AIX requires this to be the first thing in the file.  */
15 #ifndef __GNUC__
16 # if HAVE_ALLOCA_H
17 #  include <alloca.h>
18 # else
19 #  ifdef _AIX
20  #pragma alloca
21 #  else
22 #   ifndef alloca /* predefined by HP cc +Olibcalls */
23 char *alloca ();
24 #   endif
25 #  endif
26 # endif
27 #endif
28 #endif /* TRE_USE_ALLOCA */
29
30 #include <assert.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #ifdef HAVE_WCHAR_H
34 #include <wchar.h>
35 #endif /* HAVE_WCHAR_H */
36 #ifdef HAVE_WCTYPE_H
37 #include <wctype.h>
38 #endif /* HAVE_WCTYPE_H */
39 #ifndef TRE_WCHAR
40 #include <ctype.h>
41 #endif /* !TRE_WCHAR */
42 #ifdef HAVE_MALLOC_H
43 #include <malloc.h>
44 #endif /* HAVE_MALLOC_H */
45 #include <limits.h>
46
47 #include "tre-internal.h"
48 #include "tre-match-utils.h"
49 #include "tre.h"
50 #include "xmalloc.h"
51
52
53 /* For each tre_last_matched_t in the lm array, find the last matched branch by
54    comparing the touch value of the cmp_tag's.  For all other branches, reset
55    the corresponding tags.  If reset_all is non-zero, reset all tags in all
56    branches.  Recurse into the nested last matched structures, clearing tags as
57    apprpriate. */
58 static void
59 tre_reset_last_matched_branches(tre_tag_t *tags, const tre_last_matched_t *lm,
60                                 int n, int start_tag, int reset_all)
61 {
62   int max, i, reset;
63   tre_last_matched_branch_t *b;
64
65   DPRINT(("tre_reset_last_matched_branches: n=%d start_tag=%d reset_all=%d\n",
66           n, start_tag, reset_all));
67   for (; n-- > 0; lm++)
68     {
69       if (lm->n_branches == 1)
70         {
71           b = lm->branches;
72           if (start_tag > 0)
73             {
74               DPRINT(("  b->cmp_tag=%d %d <? %d\n", b->cmp_tag,
75                       tre_tag_touch_get(tags, b->cmp_tag),
76                       tre_tag_touch_get(tags, start_tag)));
77               reset = (reset_all || tre_tag_touch_get(tags, b->cmp_tag) <
78                        tre_tag_touch_get(tags, start_tag));
79             }
80           else
81             reset = 0;
82
83           if (reset)
84             {
85               int *t;
86
87               for (i = b->n_tags, t = b->tags; i > 0; i--, t++)
88                 {
89                   DPRINT(("  Resetting t%d\n", *t));
90                   tre_tag_reset(tags, *t);
91                 }
92             }
93           if (b->n_last_matched > 0)
94             tre_reset_last_matched_branches(tags, b->last_matched,
95                                                 b->n_last_matched, 
96                                                 lm->start_tag, reset);
97         }
98       else
99         {
100           if (!reset_all)
101             {
102 #ifdef TRE_DEBUG
103               int last;
104 #endif /* TRE_DEBUG */
105               max = 0;
106               for (i = lm->n_branches, b = lm->branches; i > 0; i--, b++)
107                 {
108                   int t = b->cmp_tag;
109                   int touch = tre_tag_touch_get(tags, t);
110                   if (touch > max)
111                     {
112                       max = touch;
113 #ifdef TRE_DEBUG
114                       last = t;
115 #endif /* TRE_DEBUG */
116                     }
117                 }
118               DPRINT(("  Last touched end tag t%d=%d\n", last, max));
119             }
120
121           for (i = lm->n_branches, b = lm->branches; i > 0; i--, b++)
122             {
123               reset = (reset_all || tre_tag_touch_get(tags, b->cmp_tag) < max);
124               if (reset)
125                 {
126                   int j;
127                   int *t;
128
129                   for (j = b->n_tags, t = b->tags; j > 0; j--, t++)
130                     {
131                       DPRINT(("  Resetting t%d\n", *t));
132                       tre_tag_reset(tags, *t);
133                     }
134                 }
135               if (b->n_last_matched > 0)
136                 tre_reset_last_matched_branches(tags, b->last_matched,
137                                                 b->n_last_matched,
138                                                 lm->start_tag, reset);
139             }
140         }
141     }
142 }
143
144
145 /* Fills the POSIX.2 regmatch_t array according to the TNFA tag and match
146    endpoint values. */
147 reg_errcode_t
148 tre_fill_pmatch(size_t nmatch, regmatch_t pmatch[], int cflags,
149                 const tre_tnfa_t *tnfa, const tre_tag_t *intags, int match_eo)
150 {
151   unsigned int i;
152
153   if (cflags & REG_NOSUB) return REG_OK;
154
155   i = 0;
156   if (match_eo >= 0 && intags)
157     {
158       const tre_tag_t *tags = intags;
159       tre_submatch_data_t *submatch_data;
160
161       if (tnfa->last_matched_branch &&
162           tnfa->last_matched_branch->n_last_matched > 0)
163         {
164           tre_tag_t *t;
165 #ifdef TRE_USE_ALLOCA
166           t = alloca(sizeof(*t) * tnfa->num_tags);
167 #else /* !TRE_USE_ALLOCA */
168           t = xmalloc(sizeof(*t) * tnfa->num_tags);
169 #endif /* !TRE_USE_ALLOCA */
170           if (!t) return REG_ESPACE;
171           memcpy(t, intags, tnfa->num_tags * sizeof(tre_tag_t));
172           tre_reset_last_matched_branches(t,
173                                     tnfa->last_matched_branch->last_matched,
174                                     tnfa->last_matched_branch->n_last_matched,
175                                     0, 0);
176           tags = t;
177         }
178       /* Construct submatch offsets from the tags. */
179       DPRINT(("end tag = t%d = %d\n", tnfa->end_tag, match_eo));
180       submatch_data = tnfa->submatch_data;
181       while (i < tnfa->num_submatches && i < nmatch)
182         {
183           if (submatch_data[i].so_tag == tnfa->end_tag)
184             pmatch[i].rm_so = match_eo;
185           else
186             pmatch[i].rm_so = tre_tag_get(tags, submatch_data[i].so_tag);
187
188           if (submatch_data[i].eo_tag == tnfa->end_tag)
189             pmatch[i].rm_eo = match_eo;
190           else
191             pmatch[i].rm_eo = tre_tag_get(tags, submatch_data[i].eo_tag);
192
193           /* If either of the endpoints were not used, this submatch
194              was not part of the match. */
195           if (pmatch[i].rm_so == -1 || pmatch[i].rm_eo == -1)
196             pmatch[i].rm_so = pmatch[i].rm_eo = -1;
197
198           DPRINT(("pmatch[%d] = {t%d = %qd, t%d = %qd}\n", i,
199                   submatch_data[i].so_tag, pmatch[i].rm_so,
200                   submatch_data[i].eo_tag, pmatch[i].rm_eo));
201           i++;
202         }
203 #ifndef TRE_USE_ALLOCA
204 #pragma GCC diagnostic push
205 #pragma GCC diagnostic ignored "-Wdiscarded-qualifiers"
206         if (tags != intags) xfree(tags);
207 #pragma GCC diagnostic pop
208 #endif /* !TRE_USE_ALLOCA */
209     }
210
211   while (i < nmatch)
212     {
213       pmatch[i].rm_so = -1;
214       pmatch[i].rm_eo = -1;
215       i++;
216     }
217
218   return REG_OK;
219 }
220
221
222 /*
223   Wrapper functions for POSIX compatible regexp matching.
224 */
225
226 int
227 tre_have_backrefs(const regex_t *preg)
228 {
229   tre_tnfa_t *tnfa = (void *)preg->TRE_REGEX_T_FIELD;
230   return tnfa->have_backrefs;
231 }
232
233 #ifdef TRE_APPROX
234 int
235 tre_have_approx(const regex_t *preg)
236 {
237   tre_tnfa_t *tnfa = (void *)preg->TRE_REGEX_T_FIELD;
238   return tnfa->have_approx;
239 }
240 #endif /* TRE_APPROX */
241
242 static int
243 tre_match(const tre_tnfa_t *tnfa, const void *string, size_t len,
244           tre_str_type_t type, size_t nmatch, regmatch_t pmatch[],
245           int eflags)
246 {
247   reg_errcode_t status;
248   tre_tag_t *tags = NULL;
249   int eo;
250   size_t offset = 0, count = 0;
251   if (tnfa->num_tags > 0 && nmatch > 0)
252     {
253 #ifdef TRE_USE_ALLOCA
254       tags = alloca(sizeof(*tags) * tnfa->num_tags);
255 #else /* !TRE_USE_ALLOCA */
256       tags = xmalloc(sizeof(*tags) * tnfa->num_tags);
257 #endif /* !TRE_USE_ALLOCA */
258       if (tags == NULL)
259         return REG_ESPACE;
260     }
261
262   if (
263       (eflags & REG_STARTEND) && pmatch)
264     {
265       if (pmatch->rm_so < 0)
266         return REG_INVARG;
267       if (len == (size_t)-1)
268         {
269           if (pmatch->rm_eo < 0 || pmatch->rm_so > pmatch->rm_eo)
270             return REG_INVARG;
271           len = pmatch->rm_eo - pmatch->rm_so;
272         }
273       count = offset = pmatch->rm_so;
274       if (type == STR_WIDE) offset *= sizeof(wchar_t);
275     }
276
277   /* Dispatch to the appropriate matcher. */
278   if (tnfa->have_backrefs || eflags & REG_BACKTRACKING_MATCHER)
279     {
280       /* The regex has back references, use the backtracking matcher. */
281       status = tre_tnfa_run_backtrack(tnfa, string + offset, (int)len, type,
282                                       tags, eflags, &eo);
283     }
284 #ifdef TRE_APPROX
285   else if (tnfa->have_approx || eflags & REG_APPROX_MATCHER)
286     {
287       /* The regex uses approximate matching, use the approximate matcher. */
288       regamatch_t match;
289       regaparams_t params;
290       tre_regaparams_default(&params);
291       params.max_err = 0;
292       params.max_cost = 0;
293       status = tre_tnfa_run_approx(tnfa, string + offset, (int)len, type, tags,
294                                    &match, params, eflags, &eo);
295     }
296 #endif /* TRE_APPROX */
297   else
298     {
299       /* Exact matching, no back references, use the parallel matcher. */
300       status = tre_tnfa_run_parallel(tnfa, string + offset, (int)len, type,
301                                      tags, eflags, &eo);
302     }
303
304   if (status == REG_OK)
305     {
306       /* A match was found, so fill the submatch registers. */
307       status = tre_fill_pmatch(nmatch, pmatch, tnfa->cflags, tnfa, tags, eo);
308       /* If doing REG_STARTEND, adjust the pmatch array (we can't build
309          this into tre_fill_pmatch, because tre_tnfa_run_backtrack calls
310          tre_fill_pmatch itself). */
311       if (status == REG_OK && !(tnfa->cflags & REG_NOSUB) &&
312           (eflags & REG_STARTEND) && pmatch && nmatch > 0)
313         {
314           size_t i;
315           regmatch_t *p;
316           for (i = nmatch, p = pmatch; i > 0; p++, i--)
317             {
318               if (p->rm_so >= 0) p->rm_so += count;
319               if (p->rm_eo >= 0) p->rm_eo += count;
320             }
321         }
322     }
323 #ifndef TRE_USE_ALLOCA
324   if (tags)
325     xfree(tags);
326 #endif /* !TRE_USE_ALLOCA */
327   return status;
328 }
329
330 int
331 tre_regnexec(const regex_t *preg, const char *str, size_t len,
332          size_t nmatch, regmatch_t pmatch[], int eflags)
333 {
334   tre_tnfa_t *tnfa = (void *)preg->TRE_REGEX_T_FIELD;
335   tre_str_type_t type = (TRE_MB_CUR_MAX_L(tnfa->loc) == 1) ? STR_BYTE : STR_MBS;
336
337 #ifdef TRE_USE_SYSTEM_REGEX_H
338   if (preg->re_magic != RE_MAGIC) return REG_BADPAT;
339 #endif /* TRE_USE_SYSTEM_REGEX_H */
340
341   return tre_match(tnfa, str, len, type, nmatch, pmatch, eflags);
342 }
343
344 int
345 tre_regexec(const regex_t *preg, const char *str,
346         size_t nmatch, regmatch_t pmatch[], int eflags)
347 {
348   return tre_regnexec(preg, str, (size_t)-1, nmatch, pmatch, eflags);
349 }
350
351
352 #ifdef TRE_WCHAR
353
354 int
355 tre_regwnexec(const regex_t *preg, const wchar_t *str, size_t len,
356           size_t nmatch, regmatch_t pmatch[], int eflags)
357 {
358   tre_tnfa_t *tnfa = (void *)preg->TRE_REGEX_T_FIELD;
359
360 #ifdef TRE_USE_SYSTEM_REGEX_H
361   if (preg->re_magic != RE_MAGIC) return REG_BADPAT;
362 #endif /* TRE_USE_SYSTEM_REGEX_H */
363
364   return tre_match(tnfa, str, len, STR_WIDE, nmatch, pmatch, eflags);
365 }
366
367 int
368 tre_regwexec(const regex_t *preg, const wchar_t *str,
369          size_t nmatch, regmatch_t pmatch[], int eflags)
370 {
371   return tre_regwnexec(preg, str, (size_t)-1, nmatch, pmatch, eflags);
372 }
373
374 #endif /* TRE_WCHAR */
375
376 #ifdef TRE_APPROX
377
378 /*
379   Wrapper functions for approximate regexp matching.
380 */
381
382 static int
383 tre_match_approx(const tre_tnfa_t *tnfa, const void *string, size_t len,
384                  tre_str_type_t type, regamatch_t *match, regaparams_t params,
385                  int eflags)
386 {
387   reg_errcode_t status;
388   tre_tag_t *tags = NULL;
389   int eo;
390   size_t offset = 0, count = 0;
391
392   /* If the regexp does not use approximate matching features, the
393      maximum cost is zero, and the approximate matcher isn't forced,
394      use the exact matcher instead. */
395   if (params.max_cost == 0 && !tnfa->have_approx
396       && !(eflags & REG_APPROX_MATCHER))
397     return tre_match(tnfa, string, len, type, match->nmatch, match->pmatch,
398                      eflags);
399
400   /* Back references are not supported by the approximate matcher. */
401   if (tnfa->have_backrefs)
402     return REG_BADPAT;
403
404   if (tnfa->num_tags > 0 && match->nmatch > 0)
405     {
406 #if TRE_USE_ALLOCA
407       tags = alloca(sizeof(*tags) * tnfa->num_tags);
408 #else /* !TRE_USE_ALLOCA */
409       tags = xmalloc(sizeof(*tags) * tnfa->num_tags);
410 #endif /* !TRE_USE_ALLOCA */
411       if (tags == NULL)
412         return REG_ESPACE;
413     }
414
415   if (
416       (eflags & REG_STARTEND) && match->pmatch)
417     {
418       if (match->pmatch->rm_so < 0)
419         return REG_INVARG;
420       if (len == (size_t)-1)
421         {
422           if (match->pmatch->rm_eo < 0 || match->pmatch->rm_so >
423               match->pmatch->rm_eo)
424             return REG_INVARG;
425           len = match->pmatch->rm_eo - match->pmatch->rm_so;
426         }
427       count = offset = match->pmatch->rm_so;
428       if (type == STR_WIDE) offset *= sizeof(wchar_t);
429     }
430
431   status = tre_tnfa_run_approx(tnfa, string, (int)len, type, tags,
432                                match, params, eflags, &eo);
433   if (status == REG_OK)
434     {
435       status = tre_fill_pmatch(match->nmatch, match->pmatch, tnfa->cflags,
436                                tnfa, tags, eo);
437       /* If doing REG_STARTEND, adjust the pmatch array (we can't build
438          this into tre_fill_pmatch, because tre_tnfa_run_backtrack call
439          tre_fill_pmatch itself). */
440       if (status == REG_OK && !(tnfa->cflags & REG_NOSUB) &&
441           (eflags & REG_STARTEND) && match->pmatch && match->nmatch > 0)
442         {
443           size_t i;
444           regmatch_t *p;
445           for (i = match->nmatch, p = match->pmatch; i > 0; p++, i--)
446             {
447               if (p->rm_so >= 0) p->rm_so += count;
448               if (p->rm_eo >= 0) p->rm_eo += count;
449             }
450         }
451     }
452 #ifndef TRE_USE_ALLOCA
453   if (tags)
454     xfree(tags);
455 #endif /* !TRE_USE_ALLOCA */
456   return status;
457 }
458
459 int
460 tre_reganexec(const regex_t *preg, const char *str, size_t len,
461           regamatch_t *match, regaparams_t params, int eflags)
462 {
463   tre_tnfa_t *tnfa = (void *)preg->TRE_REGEX_T_FIELD;
464   tre_str_type_t type = (TRE_MB_CUR_MAX_L(tnfa->loc) == 1) ? STR_BYTE : STR_MBS;
465
466 #ifdef TRE_USE_SYSTEM_REGEX_H
467   if (preg->re_magic != RE_MAGIC) return REG_BADPAT;
468 #endif /* TRE_USE_SYSTEM_REGEX_H */
469
470   return tre_match_approx(tnfa, str, len, type, match, params, eflags);
471 }
472
473 int
474 tre_regaexec(const regex_t *preg, const char *str,
475          regamatch_t *match, regaparams_t params, int eflags)
476 {
477   return tre_reganexec(preg, str, (size_t)-1, match, params, eflags);
478 }
479
480 #ifdef TRE_WCHAR
481
482 int
483 tre_regawnexec(const regex_t *preg, const wchar_t *str, size_t len,
484            regamatch_t *match, regaparams_t params, int eflags)
485 {
486   tre_tnfa_t *tnfa = (void *)preg->TRE_REGEX_T_FIELD;
487
488 #ifdef TRE_USE_SYSTEM_REGEX_H
489   if (preg->re_magic != RE_MAGIC) return REG_BADPAT;
490 #endif /* TRE_USE_SYSTEM_REGEX_H */
491
492   return tre_match_approx(tnfa, str, len, STR_WIDE,
493                           match, params, eflags);
494 }
495
496 int
497 tre_regawexec(const regex_t *preg, const wchar_t *str,
498           regamatch_t *match, regaparams_t params, int eflags)
499 {
500   return tre_regawnexec(preg, str, (size_t)-1, match, params, eflags);
501 }
502
503 #endif /* TRE_WCHAR */
504
505 void
506 tre_regaparams_default(regaparams_t *params)
507 {
508   memset(params, 0, sizeof(*params));
509   params->cost_ins = 1;
510   params->cost_del = 1;
511   params->cost_subst = 1;
512   params->max_cost = INT_MAX;
513   params->max_ins = INT_MAX;
514   params->max_del = INT_MAX;
515   params->max_subst = INT_MAX;
516   params->max_err = INT_MAX;
517 }
518
519 #endif /* TRE_APPROX */
520
521 /* EOF */