@matheusbsbs / matheus-bagliano / commits / d18f76dccf
Fork 0

compat/regex: use the regex engine from gawk for compat

Change the regex engine in compat to use the gawk engine from the gawk-devel module in gawk CVS. This engine supports the REG_STARTEND flag, which was optionally available in Git since v1.7.2-rc0~77^2~1. The source was grabbed from cvs.savannah.gnu.org:/sources/gawk, and these are the upstream versions of the files being included: regcomp.c 1.4 regex.h 1.3 regex.h 1.3 regex_internal.c 1.3 regex_internal.h 1.3 regexec.c 1.3 Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Acked-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Aug 17, 2010 at 09:24 UTC d18f76dccff1fc56221846926edb4542d88f60e0
6 files changed +11180 -5105
compat/regex/regcomp.c new
+3892
@@ -0,0 +1,3892 @@
1 +/* Extended regular expression matching and search library.
2 + Copyright (C) 2002-2007,2009,2010 Free Software Foundation, Inc.
3 + This file is part of the GNU C Library.
4 + Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
5 +
6 + The GNU C Library is free software; you can redistribute it and/or
7 + modify it under the terms of the GNU Lesser General Public
8 + License as published by the Free Software Foundation; either
9 + version 2.1 of the License, or (at your option) any later version.
10 +
11 + The GNU C Library is distributed in the hope that it will be useful,
12 + but WITHOUT ANY WARRANTY; without even the implied warranty of
13 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 + Lesser General Public License for more details.
15 +
16 + You should have received a copy of the GNU Lesser General Public
17 + License along with the GNU C Library; if not, write to the Free
18 + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 + 02110-1301 USA. */
20 +
21 +static reg_errcode_t re_compile_internal (regex_t *preg, const char * pattern,
22 + size_t length, reg_syntax_t syntax);
23 +static void re_compile_fastmap_iter (regex_t *bufp,
24 + const re_dfastate_t *init_state,
25 + char *fastmap);
26 +static reg_errcode_t init_dfa (re_dfa_t *dfa, size_t pat_len);
27 +#ifdef RE_ENABLE_I18N
28 +static void free_charset (re_charset_t *cset);
29 +#endif /* RE_ENABLE_I18N */
30 +static void free_workarea_compile (regex_t *preg);
31 +static reg_errcode_t create_initial_state (re_dfa_t *dfa);
32 +#ifdef RE_ENABLE_I18N
33 +static void optimize_utf8 (re_dfa_t *dfa);
34 +#endif
35 +static reg_errcode_t analyze (regex_t *preg);
36 +static reg_errcode_t preorder (bin_tree_t *root,
37 + reg_errcode_t (fn (void *, bin_tree_t *)),
38 + void *extra);
39 +static reg_errcode_t postorder (bin_tree_t *root,
40 + reg_errcode_t (fn (void *, bin_tree_t *)),
41 + void *extra);
42 +static reg_errcode_t optimize_subexps (void *extra, bin_tree_t *node);
43 +static reg_errcode_t lower_subexps (void *extra, bin_tree_t *node);
44 +static bin_tree_t *lower_subexp (reg_errcode_t *err, regex_t *preg,
45 + bin_tree_t *node);
46 +static reg_errcode_t calc_first (void *extra, bin_tree_t *node);
47 +static reg_errcode_t calc_next (void *extra, bin_tree_t *node);
48 +static reg_errcode_t link_nfa_nodes (void *extra, bin_tree_t *node);
49 +static int duplicate_node (re_dfa_t *dfa, int org_idx, unsigned int constraint);
50 +static int search_duplicated_node (const re_dfa_t *dfa, int org_node,
51 + unsigned int constraint);
52 +static reg_errcode_t calc_eclosure (re_dfa_t *dfa);
53 +static reg_errcode_t calc_eclosure_iter (re_node_set *new_set, re_dfa_t *dfa,
54 + int node, int root);
55 +static reg_errcode_t calc_inveclosure (re_dfa_t *dfa);
56 +static int fetch_number (re_string_t *input, re_token_t *token,
57 + reg_syntax_t syntax);
58 +static int peek_token (re_token_t *token, re_string_t *input,
59 + reg_syntax_t syntax) internal_function;
60 +static bin_tree_t *parse (re_string_t *regexp, regex_t *preg,
61 + reg_syntax_t syntax, reg_errcode_t *err);
62 +static bin_tree_t *parse_reg_exp (re_string_t *regexp, regex_t *preg,
63 + re_token_t *token, reg_syntax_t syntax,
64 + int nest, reg_errcode_t *err);
65 +static bin_tree_t *parse_branch (re_string_t *regexp, regex_t *preg,
66 + re_token_t *token, reg_syntax_t syntax,
67 + int nest, reg_errcode_t *err);
68 +static bin_tree_t *parse_expression (re_string_t *regexp, regex_t *preg,
69 + re_token_t *token, reg_syntax_t syntax,
70 + int nest, reg_errcode_t *err);
71 +static bin_tree_t *parse_sub_exp (re_string_t *regexp, regex_t *preg,
72 + re_token_t *token, reg_syntax_t syntax,
73 + int nest, reg_errcode_t *err);
74 +static bin_tree_t *parse_dup_op (bin_tree_t *dup_elem, re_string_t *regexp,
75 + re_dfa_t *dfa, re_token_t *token,
76 + reg_syntax_t syntax, reg_errcode_t *err);
77 +static bin_tree_t *parse_bracket_exp (re_string_t *regexp, re_dfa_t *dfa,
78 + re_token_t *token, reg_syntax_t syntax,
79 + reg_errcode_t *err);
80 +static reg_errcode_t parse_bracket_element (bracket_elem_t *elem,
81 + re_string_t *regexp,
82 + re_token_t *token, int token_len,
83 + re_dfa_t *dfa,
84 + reg_syntax_t syntax,
85 + int accept_hyphen);
86 +static reg_errcode_t parse_bracket_symbol (bracket_elem_t *elem,
87 + re_string_t *regexp,
88 + re_token_t *token);
89 +#ifdef RE_ENABLE_I18N
90 +static reg_errcode_t build_equiv_class (bitset_t sbcset,
91 + re_charset_t *mbcset,
92 + int *equiv_class_alloc,
93 + const unsigned char *name);
94 +static reg_errcode_t build_charclass (RE_TRANSLATE_TYPE trans,
95 + bitset_t sbcset,
96 + re_charset_t *mbcset,
97 + int *char_class_alloc,
98 + const char *class_name,
99 + reg_syntax_t syntax);
100 +#else /* not RE_ENABLE_I18N */
101 +static reg_errcode_t build_equiv_class (bitset_t sbcset,
102 + const unsigned char *name);
103 +static reg_errcode_t build_charclass (RE_TRANSLATE_TYPE trans,
104 + bitset_t sbcset,
105 + const char *class_name,
106 + reg_syntax_t syntax);
107 +#endif /* not RE_ENABLE_I18N */
108 +static bin_tree_t *build_charclass_op (re_dfa_t *dfa,
109 + RE_TRANSLATE_TYPE trans,
110 + const char *class_name,
111 + const char *extra,
112 + int non_match, reg_errcode_t *err);
113 +static bin_tree_t *create_tree (re_dfa_t *dfa,
114 + bin_tree_t *left, bin_tree_t *right,
115 + re_token_type_t type);
116 +static bin_tree_t *create_token_tree (re_dfa_t *dfa,
117 + bin_tree_t *left, bin_tree_t *right,
118 + const re_token_t *token);
119 +static bin_tree_t *duplicate_tree (const bin_tree_t *src, re_dfa_t *dfa);
120 +static void free_token (re_token_t *node);
121 +static reg_errcode_t free_tree (void *extra, bin_tree_t *node);
122 +static reg_errcode_t mark_opt_subexp (void *extra, bin_tree_t *node);
123 +
124 +/* This table gives an error message for each of the error codes listed
125 + in regex.h. Obviously the order here has to be same as there.
126 + POSIX doesn't require that we do anything for REG_NOERROR,
127 + but why not be nice? */
128 +
129 +const char __re_error_msgid[] attribute_hidden =
130 + {
131 +#define REG_NOERROR_IDX 0
132 + gettext_noop ("Success") /* REG_NOERROR */
133 + "\0"
134 +#define REG_NOMATCH_IDX (REG_NOERROR_IDX + sizeof "Success")
135 + gettext_noop ("No match") /* REG_NOMATCH */
136 + "\0"
137 +#define REG_BADPAT_IDX (REG_NOMATCH_IDX + sizeof "No match")
138 + gettext_noop ("Invalid regular expression") /* REG_BADPAT */
139 + "\0"
140 +#define REG_ECOLLATE_IDX (REG_BADPAT_IDX + sizeof "Invalid regular expression")
141 + gettext_noop ("Invalid collation character") /* REG_ECOLLATE */
142 + "\0"
143 +#define REG_ECTYPE_IDX (REG_ECOLLATE_IDX + sizeof "Invalid collation character")
144 + gettext_noop ("Invalid character class name") /* REG_ECTYPE */
145 + "\0"
146 +#define REG_EESCAPE_IDX (REG_ECTYPE_IDX + sizeof "Invalid character class name")
147 + gettext_noop ("Trailing backslash") /* REG_EESCAPE */
148 + "\0"
149 +#define REG_ESUBREG_IDX (REG_EESCAPE_IDX + sizeof "Trailing backslash")
150 + gettext_noop ("Invalid back reference") /* REG_ESUBREG */
151 + "\0"
152 +#define REG_EBRACK_IDX (REG_ESUBREG_IDX + sizeof "Invalid back reference")
153 + gettext_noop ("Unmatched [ or [^") /* REG_EBRACK */
154 + "\0"
155 +#define REG_EPAREN_IDX (REG_EBRACK_IDX + sizeof "Unmatched [ or [^")
156 + gettext_noop ("Unmatched ( or \\(") /* REG_EPAREN */
157 + "\0"
158 +#define REG_EBRACE_IDX (REG_EPAREN_IDX + sizeof "Unmatched ( or \\(")
159 + gettext_noop ("Unmatched \\{") /* REG_EBRACE */
160 + "\0"
161 +#define REG_BADBR_IDX (REG_EBRACE_IDX + sizeof "Unmatched \\{")
162 + gettext_noop ("Invalid content of \\{\\}") /* REG_BADBR */
163 + "\0"
164 +#define REG_ERANGE_IDX (REG_BADBR_IDX + sizeof "Invalid content of \\{\\}")
165 + gettext_noop ("Invalid range end") /* REG_ERANGE */
166 + "\0"
167 +#define REG_ESPACE_IDX (REG_ERANGE_IDX + sizeof "Invalid range end")
168 + gettext_noop ("Memory exhausted") /* REG_ESPACE */
169 + "\0"
170 +#define REG_BADRPT_IDX (REG_ESPACE_IDX + sizeof "Memory exhausted")
171 + gettext_noop ("Invalid preceding regular expression") /* REG_BADRPT */
172 + "\0"
173 +#define REG_EEND_IDX (REG_BADRPT_IDX + sizeof "Invalid preceding regular expression")
174 + gettext_noop ("Premature end of regular expression") /* REG_EEND */
175 + "\0"
176 +#define REG_ESIZE_IDX (REG_EEND_IDX + sizeof "Premature end of regular expression")
177 + gettext_noop ("Regular expression too big") /* REG_ESIZE */
178 + "\0"
179 +#define REG_ERPAREN_IDX (REG_ESIZE_IDX + sizeof "Regular expression too big")
180 + gettext_noop ("Unmatched ) or \\)") /* REG_ERPAREN */
181 + };
182 +
183 +const size_t __re_error_msgid_idx[] attribute_hidden =
184 + {
185 + REG_NOERROR_IDX,
186 + REG_NOMATCH_IDX,
187 + REG_BADPAT_IDX,
188 + REG_ECOLLATE_IDX,
189 + REG_ECTYPE_IDX,
190 + REG_EESCAPE_IDX,
191 + REG_ESUBREG_IDX,
192 + REG_EBRACK_IDX,
193 + REG_EPAREN_IDX,
194 + REG_EBRACE_IDX,
195 + REG_BADBR_IDX,
196 + REG_ERANGE_IDX,
197 + REG_ESPACE_IDX,
198 + REG_BADRPT_IDX,
199 + REG_EEND_IDX,
200 + REG_ESIZE_IDX,
201 + REG_ERPAREN_IDX
202 + };
203 +
204 +/* Entry points for GNU code. */
205 +
206 +
207 +#ifdef ZOS_USS
208 +
209 +/* For ZOS USS we must define btowc */
210 +
211 +wchar_t
212 +btowc (int c)
213 +{
214 + wchar_t wtmp[2];
215 + char tmp[2];
216 +
217 + tmp[0] = c;
218 + tmp[1] = 0;
219 +
220 + mbtowc (wtmp, tmp, 1);
221 + return wtmp[0];
222 +}
223 +#endif
224 +
225 +/* re_compile_pattern is the GNU regular expression compiler: it
226 + compiles PATTERN (of length LENGTH) and puts the result in BUFP.
227 + Returns 0 if the pattern was valid, otherwise an error string.
228 +
229 + Assumes the `allocated' (and perhaps `buffer') and `translate' fields
230 + are set in BUFP on entry. */
231 +
232 +const char *
233 +re_compile_pattern (pattern, length, bufp)
234 + const char *pattern;
235 + size_t length;
236 + struct re_pattern_buffer *bufp;
237 +{
238 + reg_errcode_t ret;
239 +
240 + /* And GNU code determines whether or not to get register information
241 + by passing null for the REGS argument to re_match, etc., not by
242 + setting no_sub, unless RE_NO_SUB is set. */
243 + bufp->no_sub = !!(re_syntax_options & RE_NO_SUB);
244 +
245 + /* Match anchors at newline. */
246 + bufp->newline_anchor = 1;
247 +
248 + ret = re_compile_internal (bufp, pattern, length, re_syntax_options);
249 +
250 + if (!ret)
251 + return NULL;
252 + return gettext (__re_error_msgid + __re_error_msgid_idx[(int) ret]);
253 +}
254 +#ifdef _LIBC
255 +weak_alias (__re_compile_pattern, re_compile_pattern)
256 +#endif
257 +
258 +/* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
259 + also be assigned to arbitrarily: each pattern buffer stores its own
260 + syntax, so it can be changed between regex compilations. */
261 +/* This has no initializer because initialized variables in Emacs
262 + become read-only after dumping. */
263 +reg_syntax_t re_syntax_options;
264 +
265 +
266 +/* Specify the precise syntax of regexps for compilation. This provides
267 + for compatibility for various utilities which historically have
268 + different, incompatible syntaxes.
269 +
270 + The argument SYNTAX is a bit mask comprised of the various bits
271 + defined in regex.h. We return the old syntax. */
272 +
273 +reg_syntax_t
274 +re_set_syntax (syntax)
275 + reg_syntax_t syntax;
276 +{
277 + reg_syntax_t ret = re_syntax_options;
278 +
279 + re_syntax_options = syntax;
280 + return ret;
281 +}
282 +#ifdef _LIBC
283 +weak_alias (__re_set_syntax, re_set_syntax)
284 +#endif
285 +
286 +int
287 +re_compile_fastmap (bufp)
288 + struct re_pattern_buffer *bufp;
289 +{
290 + re_dfa_t *dfa = (re_dfa_t *) bufp->buffer;
291 + char *fastmap = bufp->fastmap;
292 +
293 + memset (fastmap, '\0', sizeof (char) * SBC_MAX);
294 + re_compile_fastmap_iter (bufp, dfa->init_state, fastmap);
295 + if (dfa->init_state != dfa->init_state_word)
296 + re_compile_fastmap_iter (bufp, dfa->init_state_word, fastmap);
297 + if (dfa->init_state != dfa->init_state_nl)
298 + re_compile_fastmap_iter (bufp, dfa->init_state_nl, fastmap);
299 + if (dfa->init_state != dfa->init_state_begbuf)
300 + re_compile_fastmap_iter (bufp, dfa->init_state_begbuf, fastmap);
301 + bufp->fastmap_accurate = 1;
302 + return 0;
303 +}
304 +#ifdef _LIBC
305 +weak_alias (__re_compile_fastmap, re_compile_fastmap)
306 +#endif
307 +
308 +static inline void
309 +__attribute ((always_inline))
310 +re_set_fastmap (char *fastmap, int icase, int ch)
311 +{
312 + fastmap[ch] = 1;
313 + if (icase)
314 + fastmap[tolower (ch)] = 1;
315 +}
316 +
317 +/* Helper function for re_compile_fastmap.
318 + Compile fastmap for the initial_state INIT_STATE. */
319 +
320 +static void
321 +re_compile_fastmap_iter (regex_t *bufp, const re_dfastate_t *init_state,
322 + char *fastmap)
323 +{
324 + volatile re_dfa_t *dfa = (re_dfa_t *) bufp->buffer;
325 + int node_cnt;
326 + int icase = (dfa->mb_cur_max == 1 && (bufp->syntax & RE_ICASE));
327 + for (node_cnt = 0; node_cnt < init_state->nodes.nelem; ++node_cnt)
328 + {
329 + int node = init_state->nodes.elems[node_cnt];
330 + re_token_type_t type = dfa->nodes[node].type;
331 +
332 + if (type == CHARACTER)
333 + {
334 + re_set_fastmap (fastmap, icase, dfa->nodes[node].opr.c);
335 +#ifdef RE_ENABLE_I18N
336 + if ((bufp->syntax & RE_ICASE) && dfa->mb_cur_max > 1)
337 + {
338 + unsigned char *buf = re_malloc (unsigned char, dfa->mb_cur_max), *p;
339 + wchar_t wc;
340 + mbstate_t state;
341 +
342 + p = buf;
343 + *p++ = dfa->nodes[node].opr.c;
344 + while (++node < dfa->nodes_len
345 + && dfa->nodes[node].type == CHARACTER
346 + && dfa->nodes[node].mb_partial)
347 + *p++ = dfa->nodes[node].opr.c;
348 + memset (&state, '\0', sizeof (state));
349 + if (__mbrtowc (&wc, (const char *) buf, p - buf,
350 + &state) == p - buf
351 + && (__wcrtomb ((char *) buf, towlower (wc), &state)
352 + != (size_t) -1))
353 + re_set_fastmap (fastmap, 0, buf[0]);
354 + re_free (buf);
355 + }
356 +#endif
357 + }
358 + else if (type == SIMPLE_BRACKET)
359 + {
360 + int i, ch;
361 + for (i = 0, ch = 0; i < BITSET_WORDS; ++i)
362 + {
363 + int j;
364 + bitset_word_t w = dfa->nodes[node].opr.sbcset[i];
365 + for (j = 0; j < BITSET_WORD_BITS; ++j, ++ch)
366 + if (w & ((bitset_word_t) 1 << j))
367 + re_set_fastmap (fastmap, icase, ch);
368 + }
369 + }
370 +#ifdef RE_ENABLE_I18N
371 + else if (type == COMPLEX_BRACKET)
372 + {
373 + re_charset_t *cset = dfa->nodes[node].opr.mbcset;
374 + int i;
375 +
376 +# ifdef _LIBC
377 + /* See if we have to try all bytes which start multiple collation
378 + elements.
379 + e.g. In da_DK, we want to catch 'a' since "aa" is a valid
380 + collation element, and don't catch 'b' since 'b' is
381 + the only collation element which starts from 'b' (and
382 + it is caught by SIMPLE_BRACKET). */
383 + if (_NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES) != 0
384 + && (cset->ncoll_syms || cset->nranges))
385 + {
386 + const int32_t *table = (const int32_t *)
387 + _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
388 + for (i = 0; i < SBC_MAX; ++i)
389 + if (table[i] < 0)
390 + re_set_fastmap (fastmap, icase, i);
391 + }
392 +# endif /* _LIBC */
393 +
394 + /* See if we have to start the match at all multibyte characters,
395 + i.e. where we would not find an invalid sequence. This only
396 + applies to multibyte character sets; for single byte character
397 + sets, the SIMPLE_BRACKET again suffices. */
398 + if (dfa->mb_cur_max > 1
399 + && (cset->nchar_classes || cset->non_match || cset->nranges
400 +# ifdef _LIBC
401 + || cset->nequiv_classes
402 +# endif /* _LIBC */
403 + ))
404 + {
405 + unsigned char c = 0;
406 + do
407 + {
408 + mbstate_t mbs;
409 + memset (&mbs, 0, sizeof (mbs));
410 + if (__mbrtowc (NULL, (char *) &c, 1, &mbs) == (size_t) -2)
411 + re_set_fastmap (fastmap, false, (int) c);
412 + }
413 + while (++c != 0);
414 + }
415 +
416 + else
417 + {
418 + /* ... Else catch all bytes which can start the mbchars. */
419 + for (i = 0; i < cset->nmbchars; ++i)
420 + {
421 + char buf[256];
422 + mbstate_t state;
423 + memset (&state, '\0', sizeof (state));
424 + if (__wcrtomb (buf, cset->mbchars[i], &state) != (size_t) -1)
425 + re_set_fastmap (fastmap, icase, *(unsigned char *) buf);
426 + if ((bufp->syntax & RE_ICASE) && dfa->mb_cur_max > 1)
427 + {
428 + if (__wcrtomb (buf, towlower (cset->mbchars[i]), &state)
429 + != (size_t) -1)
430 + re_set_fastmap (fastmap, false, *(unsigned char *) buf);
431 + }
432 + }
433 + }
434 + }
435 +#endif /* RE_ENABLE_I18N */
436 + else if (type == OP_PERIOD
437 +#ifdef RE_ENABLE_I18N
438 + || type == OP_UTF8_PERIOD
439 +#endif /* RE_ENABLE_I18N */
440 + || type == END_OF_RE)
441 + {
442 + memset (fastmap, '\1', sizeof (char) * SBC_MAX);
443 + if (type == END_OF_RE)
444 + bufp->can_be_null = 1;
445 + return;
446 + }
447 + }
448 +}
449 +
450 +/* Entry point for POSIX code. */
451 +/* regcomp takes a regular expression as a string and compiles it.
452 +
453 + PREG is a regex_t *. We do not expect any fields to be initialized,
454 + since POSIX says we shouldn't. Thus, we set
455 +
456 + `buffer' to the compiled pattern;
457 + `used' to the length of the compiled pattern;
458 + `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
459 + REG_EXTENDED bit in CFLAGS is set; otherwise, to
460 + RE_SYNTAX_POSIX_BASIC;
461 + `newline_anchor' to REG_NEWLINE being set in CFLAGS;
462 + `fastmap' to an allocated space for the fastmap;
463 + `fastmap_accurate' to zero;
464 + `re_nsub' to the number of subexpressions in PATTERN.
465 +
466 + PATTERN is the address of the pattern string.
467 +
468 + CFLAGS is a series of bits which affect compilation.
469 +
470 + If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
471 + use POSIX basic syntax.
472 +
473 + If REG_NEWLINE is set, then . and [^...] don't match newline.
474 + Also, regexec will try a match beginning after every newline.
475 +
476 + If REG_ICASE is set, then we considers upper- and lowercase
477 + versions of letters to be equivalent when matching.
478 +
479 + If REG_NOSUB is set, then when PREG is passed to regexec, that
480 + routine will report only success or failure, and nothing about the
481 + registers.
482 +
483 + It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
484 + the return codes and their meanings.) */
485 +
486 +int
487 +regcomp (preg, pattern, cflags)
488 + regex_t *__restrict preg;
489 + const char *__restrict pattern;
490 + int cflags;
491 +{
492 + reg_errcode_t ret;
493 + reg_syntax_t syntax = ((cflags & REG_EXTENDED) ? RE_SYNTAX_POSIX_EXTENDED
494 + : RE_SYNTAX_POSIX_BASIC);
495 +
496 + preg->buffer = NULL;
497 + preg->allocated = 0;
498 + preg->used = 0;
499 +
500 + /* Try to allocate space for the fastmap. */
501 + preg->fastmap = re_malloc (char, SBC_MAX);
502 + if (BE (preg->fastmap == NULL, 0))
503 + return REG_ESPACE;
504 +
505 + syntax |= (cflags & REG_ICASE) ? RE_ICASE : 0;
506 +
507 + /* If REG_NEWLINE is set, newlines are treated differently. */
508 + if (cflags & REG_NEWLINE)
509 + { /* REG_NEWLINE implies neither . nor [^...] match newline. */
510 + syntax &= ~RE_DOT_NEWLINE;
511 + syntax |= RE_HAT_LISTS_NOT_NEWLINE;
512 + /* It also changes the matching behavior. */
513 + preg->newline_anchor = 1;
514 + }
515 + else
516 + preg->newline_anchor = 0;
517 + preg->no_sub = !!(cflags & REG_NOSUB);
518 + preg->translate = NULL;
519 +
520 + ret = re_compile_internal (preg, pattern, strlen (pattern), syntax);
521 +
522 + /* POSIX doesn't distinguish between an unmatched open-group and an
523 + unmatched close-group: both are REG_EPAREN. */
524 + if (ret == REG_ERPAREN)
525 + ret = REG_EPAREN;
526 +
527 + /* We have already checked preg->fastmap != NULL. */
528 + if (BE (ret == REG_NOERROR, 1))
529 + /* Compute the fastmap now, since regexec cannot modify the pattern
530 + buffer. This function never fails in this implementation. */
531 + (void) re_compile_fastmap (preg);
532 + else
533 + {
534 + /* Some error occurred while compiling the expression. */
535 + re_free (preg->fastmap);
536 + preg->fastmap = NULL;
537 + }
538 +
539 + return (int) ret;
540 +}
541 +#ifdef _LIBC
542 +weak_alias (__regcomp, regcomp)
543 +#endif
544 +
545 +/* Returns a message corresponding to an error code, ERRCODE, returned
546 + from either regcomp or regexec. We don't use PREG here. */
547 +
548 +size_t
549 +regerror (errcode, preg, errbuf, errbuf_size)
550 + int errcode;
551 + const regex_t *__restrict preg;
552 + char *__restrict errbuf;
553 + size_t errbuf_size;
554 +{
555 + const char *msg;
556 + size_t msg_size;
557 +
558 + if (BE (errcode < 0
559 + || errcode >= (int) (sizeof (__re_error_msgid_idx)
560 + / sizeof (__re_error_msgid_idx[0])), 0))
561 + /* Only error codes returned by the rest of the code should be passed
562 + to this routine. If we are given anything else, or if other regex
563 + code generates an invalid error code, then the program has a bug.
564 + Dump core so we can fix it. */
565 + abort ();
566 +
567 + msg = gettext (__re_error_msgid + __re_error_msgid_idx[errcode]);
568 +
569 + msg_size = strlen (msg) + 1; /* Includes the null. */
570 +
571 + if (BE (errbuf_size != 0, 1))
572 + {
573 + if (BE (msg_size > errbuf_size, 0))
574 + {
575 + memcpy (errbuf, msg, errbuf_size - 1);
576 + errbuf[errbuf_size - 1] = 0;
577 + }
578 + else
579 + memcpy (errbuf, msg, msg_size);
580 + }
581 +
582 + return msg_size;
583 +}
584 +#ifdef _LIBC
585 +weak_alias (__regerror, regerror)
586 +#endif
587 +
588 +
589 +#ifdef RE_ENABLE_I18N
590 +/* This static array is used for the map to single-byte characters when
591 + UTF-8 is used. Otherwise we would allocate memory just to initialize
592 + it the same all the time. UTF-8 is the preferred encoding so this is
593 + a worthwhile optimization. */
594 +#if __GNUC__ >= 3
595 +static const bitset_t utf8_sb_map = {
596 + /* Set the first 128 bits. */
597 + [0 ... 0x80 / BITSET_WORD_BITS - 1] = BITSET_WORD_MAX
598 +};
599 +#else /* ! (__GNUC__ >= 3) */
600 +static bitset_t utf8_sb_map;
601 +#endif /* __GNUC__ >= 3 */
602 +#endif /* RE_ENABLE_I18N */
603 +
604 +
605 +static void
606 +free_dfa_content (re_dfa_t *dfa)
607 +{
608 + int i, j;
609 +
610 + if (dfa->nodes)
611 + for (i = 0; i < dfa->nodes_len; ++i)
612 + free_token (dfa->nodes + i);
613 + re_free (dfa->nexts);
614 + for (i = 0; i < dfa->nodes_len; ++i)
615 + {
616 + if (dfa->eclosures != NULL)
617 + re_node_set_free (dfa->eclosures + i);
618 + if (dfa->inveclosures != NULL)
619 + re_node_set_free (dfa->inveclosures + i);
620 + if (dfa->edests != NULL)
621 + re_node_set_free (dfa->edests + i);
622 + }
623 + re_free (dfa->edests);
624 + re_free (dfa->eclosures);
625 + re_free (dfa->inveclosures);
626 + re_free (dfa->nodes);
627 +
628 + if (dfa->state_table)
629 + for (i = 0; i <= dfa->state_hash_mask; ++i)
630 + {
631 + struct re_state_table_entry *entry = dfa->state_table + i;
632 + for (j = 0; j < entry->num; ++j)
633 + {
634 + re_dfastate_t *state = entry->array[j];
635 + free_state (state);
636 + }
637 + re_free (entry->array);
638 + }
639 + re_free (dfa->state_table);
640 +#ifdef RE_ENABLE_I18N
641 + if (dfa->sb_char != utf8_sb_map)
642 + re_free (dfa->sb_char);
643 +#endif
644 + re_free (dfa->subexp_map);
645 +#ifdef DEBUG
646 + re_free (dfa->re_str);
647 +#endif
648 +
649 + re_free (dfa);
650 +}
651 +
652 +
653 +/* Free dynamically allocated space used by PREG. */
654 +
655 +void
656 +regfree (preg)
657 + regex_t *preg;
658 +{
659 + re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
660 + if (BE (dfa != NULL, 1))
661 + free_dfa_content (dfa);
662 + preg->buffer = NULL;
663 + preg->allocated = 0;
664 +
665 + re_free (preg->fastmap);
666 + preg->fastmap = NULL;
667 +
668 + re_free (preg->translate);
669 + preg->translate = NULL;
670 +}
671 +#ifdef _LIBC
672 +weak_alias (__regfree, regfree)
673 +#endif
674 +
675 +/* Entry points compatible with 4.2 BSD regex library. We don't define
676 + them unless specifically requested. */
677 +
678 +#if defined _REGEX_RE_COMP || defined _LIBC
679 +
680 +/* BSD has one and only one pattern buffer. */
681 +static struct re_pattern_buffer re_comp_buf;
682 +
683 +char *
684 +# ifdef _LIBC
685 +/* Make these definitions weak in libc, so POSIX programs can redefine
686 + these names if they don't use our functions, and still use
687 + regcomp/regexec above without link errors. */
688 +weak_function
689 +# endif
690 +re_comp (s)
691 + const char *s;
692 +{
693 + reg_errcode_t ret;
694 + char *fastmap;
695 +
696 + if (!s)
697 + {
698 + if (!re_comp_buf.buffer)
699 + return gettext ("No previous regular expression");
700 + return 0;
701 + }
702 +
703 + if (re_comp_buf.buffer)
704 + {
705 + fastmap = re_comp_buf.fastmap;
706 + re_comp_buf.fastmap = NULL;
707 + __regfree (&re_comp_buf);
708 + memset (&re_comp_buf, '\0', sizeof (re_comp_buf));
709 + re_comp_buf.fastmap = fastmap;
710 + }
711 +
712 + if (re_comp_buf.fastmap == NULL)
713 + {
714 + re_comp_buf.fastmap = (char *) malloc (SBC_MAX);
715 + if (re_comp_buf.fastmap == NULL)
716 + return (char *) gettext (__re_error_msgid
717 + + __re_error_msgid_idx[(int) REG_ESPACE]);
718 + }
719 +
720 + /* Since `re_exec' always passes NULL for the `regs' argument, we
721 + don't need to initialize the pattern buffer fields which affect it. */
722 +
723 + /* Match anchors at newlines. */
724 + re_comp_buf.newline_anchor = 1;
725 +
726 + ret = re_compile_internal (&re_comp_buf, s, strlen (s), re_syntax_options);
727 +
728 + if (!ret)
729 + return NULL;
730 +
731 + /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
732 + return (char *) gettext (__re_error_msgid + __re_error_msgid_idx[(int) ret]);
733 +}
734 +
735 +#ifdef _LIBC
736 +libc_freeres_fn (free_mem)
737 +{
738 + __regfree (&re_comp_buf);
739 +}
740 +#endif
741 +
742 +#endif /* _REGEX_RE_COMP */
743 +
744 +/* Internal entry point.
745 + Compile the regular expression PATTERN, whose length is LENGTH.
746 + SYNTAX indicate regular expression's syntax. */
747 +
748 +static reg_errcode_t
749 +re_compile_internal (regex_t *preg, const char * pattern, size_t length,
750 + reg_syntax_t syntax)
751 +{
752 + reg_errcode_t err = REG_NOERROR;
753 + re_dfa_t *dfa;
754 + re_string_t regexp;
755 +
756 + /* Initialize the pattern buffer. */
757 + preg->fastmap_accurate = 0;
758 + preg->syntax = syntax;
759 + preg->not_bol = preg->not_eol = 0;
760 + preg->used = 0;
761 + preg->re_nsub = 0;
762 + preg->can_be_null = 0;
763 + preg->regs_allocated = REGS_UNALLOCATED;
764 +
765 + /* Initialize the dfa. */
766 + dfa = (re_dfa_t *) preg->buffer;
767 + if (BE (preg->allocated < sizeof (re_dfa_t), 0))
768 + {
769 + /* If zero allocated, but buffer is non-null, try to realloc
770 + enough space. This loses if buffer's address is bogus, but
771 + that is the user's responsibility. If ->buffer is NULL this
772 + is a simple allocation. */
773 + dfa = re_realloc (preg->buffer, re_dfa_t, 1);
774 + if (dfa == NULL)
775 + return REG_ESPACE;
776 + preg->allocated = sizeof (re_dfa_t);
777 + preg->buffer = (unsigned char *) dfa;
778 + }
779 + preg->used = sizeof (re_dfa_t);
780 +
781 + err = init_dfa (dfa, length);
782 + if (BE (err != REG_NOERROR, 0))
783 + {
784 + free_dfa_content (dfa);
785 + preg->buffer = NULL;
786 + preg->allocated = 0;
787 + return err;
788 + }
789 +#ifdef DEBUG
790 + /* Note: length+1 will not overflow since it is checked in init_dfa. */
791 + dfa->re_str = re_malloc (char, length + 1);
792 + strncpy (dfa->re_str, pattern, length + 1);
793 +#endif
794 +
795 + __libc_lock_init (dfa->lock);
796 +
797 + err = re_string_construct (&regexp, pattern, length, preg->translate,
798 + syntax & RE_ICASE, dfa);
799 + if (BE (err != REG_NOERROR, 0))
800 + {
801 + re_compile_internal_free_return:
802 + free_workarea_compile (preg);
803 + re_string_destruct (&regexp);
804 + free_dfa_content (dfa);
805 + preg->buffer = NULL;
806 + preg->allocated = 0;
807 + return err;
808 + }
809 +
810 + /* Parse the regular expression, and build a structure tree. */
811 + preg->re_nsub = 0;
812 + dfa->str_tree = parse (&regexp, preg, syntax, &err);
813 + if (BE (dfa->str_tree == NULL, 0))
814 + goto re_compile_internal_free_return;
815 +
816 + /* Analyze the tree and create the nfa. */
817 + err = analyze (preg);
818 + if (BE (err != REG_NOERROR, 0))
819 + goto re_compile_internal_free_return;
820 +
821 +#ifdef RE_ENABLE_I18N
822 + /* If possible, do searching in single byte encoding to speed things up. */
823 + if (dfa->is_utf8 && !(syntax & RE_ICASE) && preg->translate == NULL)
824 + optimize_utf8 (dfa);
825 +#endif
826 +
827 + /* Then create the initial state of the dfa. */
828 + err = create_initial_state (dfa);
829 +
830 + /* Release work areas. */
831 + free_workarea_compile (preg);
832 + re_string_destruct (&regexp);
833 +
834 + if (BE (err != REG_NOERROR, 0))
835 + {
836 + free_dfa_content (dfa);
837 + preg->buffer = NULL;
838 + preg->allocated = 0;
839 + }
840 +
841 + return err;
842 +}
843 +
844 +/* Initialize DFA. We use the length of the regular expression PAT_LEN
845 + as the initial length of some arrays. */
846 +
847 +static reg_errcode_t
848 +init_dfa (re_dfa_t *dfa, size_t pat_len)
849 +{
850 + unsigned int table_size;
851 +#ifndef _LIBC
852 + char *codeset_name;
853 +#endif
854 +
855 + memset (dfa, '\0', sizeof (re_dfa_t));
856 +
857 + /* Force allocation of str_tree_storage the first time. */
858 + dfa->str_tree_storage_idx = BIN_TREE_STORAGE_SIZE;
859 +
860 + /* Avoid overflows. */
861 + if (pat_len == SIZE_MAX)
862 + return REG_ESPACE;
863 +
864 + dfa->nodes_alloc = pat_len + 1;
865 + dfa->nodes = re_malloc (re_token_t, dfa->nodes_alloc);
866 +
867 + /* table_size = 2 ^ ceil(log pat_len) */
868 + for (table_size = 1; ; table_size <<= 1)
869 + if (table_size > pat_len)
870 + break;
871 +
872 + dfa->state_table = calloc (sizeof (struct re_state_table_entry), table_size);
873 + dfa->state_hash_mask = table_size - 1;
874 +
875 + dfa->mb_cur_max = MB_CUR_MAX;
876 +#ifdef _LIBC
877 + if (dfa->mb_cur_max == 6
878 + && strcmp (_NL_CURRENT (LC_CTYPE, _NL_CTYPE_CODESET_NAME), "UTF-8") == 0)
879 + dfa->is_utf8 = 1;
880 + dfa->map_notascii = (_NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_MAP_TO_NONASCII)
881 + != 0);
882 +#else
883 +# ifdef HAVE_LANGINFO_CODESET
884 + codeset_name = nl_langinfo (CODESET);
885 +# else
886 + codeset_name = getenv ("LC_ALL");
887 + if (codeset_name == NULL || codeset_name[0] == '\0')
888 + codeset_name = getenv ("LC_CTYPE");
889 + if (codeset_name == NULL || codeset_name[0] == '\0')
890 + codeset_name = getenv ("LANG");
891 + if (codeset_name == NULL)
892 + codeset_name = "";
893 + else if (strchr (codeset_name, '.') != NULL)
894 + codeset_name = strchr (codeset_name, '.') + 1;
895 +# endif
896 +
897 + /* strcasecmp isn't a standard interface. brute force check */
898 +#if 0
899 + if (strcasecmp (codeset_name, "UTF-8") == 0
900 + || strcasecmp (codeset_name, "UTF8") == 0)
901 + dfa->is_utf8 = 1;
902 +#else
903 + if ( (codeset_name[0] == 'U' || codeset_name[0] == 'u')
904 + && (codeset_name[1] == 'T' || codeset_name[1] == 't')
905 + && (codeset_name[2] == 'F' || codeset_name[2] == 'f')
906 + && (codeset_name[3] == '-'
907 + ? codeset_name[4] == '8' && codeset_name[5] == '\0'
908 + : codeset_name[3] == '8' && codeset_name[4] == '\0'))
909 + dfa->is_utf8 = 1;
910 +#endif
911 +
912 + /* We check exhaustively in the loop below if this charset is a
913 + superset of ASCII. */
914 + dfa->map_notascii = 0;
915 +#endif
916 +
917 +#ifdef RE_ENABLE_I18N
918 + if (dfa->mb_cur_max > 1)
919 + {
920 + if (dfa->is_utf8)
921 + {
922 +#if !defined(__GNUC__) || __GNUC__ < 3
923 + static short utf8_sb_map_inited = 0;
924 +
925 + if (! utf8_sb_map_inited)
926 + {
927 + int i;
928 +
929 + utf8_sb_map_inited = 0;
930 + for (i = 0; i <= 0x80 / BITSET_WORD_BITS - 1; i++)
931 + utf8_sb_map[i] = BITSET_WORD_MAX;
932 + }
933 +#endif
934 + dfa->sb_char = (re_bitset_ptr_t) utf8_sb_map;
935 + }
936 + else
937 + {
938 + int i, j, ch;
939 +
940 + dfa->sb_char = (re_bitset_ptr_t) calloc (sizeof (bitset_t), 1);
941 + if (BE (dfa->sb_char == NULL, 0))
942 + return REG_ESPACE;
943 +
944 + /* Set the bits corresponding to single byte chars. */
945 + for (i = 0, ch = 0; i < BITSET_WORDS; ++i)
946 + for (j = 0; j < BITSET_WORD_BITS; ++j, ++ch)
947 + {
948 + wint_t wch = __btowc (ch);
949 + if (wch != WEOF)
950 + dfa->sb_char[i] |= (bitset_word_t) 1 << j;
951 +# ifndef _LIBC
952 + if (isascii (ch) && wch != ch)
953 + dfa->map_notascii = 1;
954 +# endif
955 + }
956 + }
957 + }
958 +#endif
959 +
960 + if (BE (dfa->nodes == NULL || dfa->state_table == NULL, 0))
961 + return REG_ESPACE;
962 + return REG_NOERROR;
963 +}
964 +
965 +/* Initialize WORD_CHAR table, which indicate which character is
966 + "word". In this case "word" means that it is the word construction
967 + character used by some operators like "\<", "\>", etc. */
968 +
969 +static void
970 +internal_function
971 +init_word_char (re_dfa_t *dfa)
972 +{
973 + int i, j, ch;
974 + dfa->word_ops_used = 1;
975 + for (i = 0, ch = 0; i < BITSET_WORDS; ++i)
976 + for (j = 0; j < BITSET_WORD_BITS; ++j, ++ch)
977 + if (isalnum (ch) || ch == '_')
978 + dfa->word_char[i] |= (bitset_word_t) 1 << j;
979 +}
980 +
981 +/* Free the work area which are only used while compiling. */
982 +
983 +static void
984 +free_workarea_compile (regex_t *preg)
985 +{
986 + re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
987 + bin_tree_storage_t *storage, *next;
988 + for (storage = dfa->str_tree_storage; storage; storage = next)
989 + {
990 + next = storage->next;
991 + re_free (storage);
992 + }
993 + dfa->str_tree_storage = NULL;
994 + dfa->str_tree_storage_idx = BIN_TREE_STORAGE_SIZE;
995 + dfa->str_tree = NULL;
996 + re_free (dfa->org_indices);
997 + dfa->org_indices = NULL;
998 +}
999 +
1000 +/* Create initial states for all contexts. */
1001 +
1002 +static reg_errcode_t
1003 +create_initial_state (re_dfa_t *dfa)
1004 +{
1005 + int first, i;
1006 + reg_errcode_t err;
1007 + re_node_set init_nodes;
1008 +
1009 + /* Initial states have the epsilon closure of the node which is
1010 + the first node of the regular expression. */
1011 + first = dfa->str_tree->first->node_idx;
1012 + dfa->init_node = first;
1013 + err = re_node_set_init_copy (&init_nodes, dfa->eclosures + first);
1014 + if (BE (err != REG_NOERROR, 0))
1015 + return err;
1016 +
1017 + /* The back-references which are in initial states can epsilon transit,
1018 + since in this case all of the subexpressions can be null.
1019 + Then we add epsilon closures of the nodes which are the next nodes of
1020 + the back-references. */
1021 + if (dfa->nbackref > 0)
1022 + for (i = 0; i < init_nodes.nelem; ++i)
1023 + {
1024 + int node_idx = init_nodes.elems[i];
1025 + re_token_type_t type = dfa->nodes[node_idx].type;
1026 +
1027 + int clexp_idx;
1028 + if (type != OP_BACK_REF)
1029 + continue;
1030 + for (clexp_idx = 0; clexp_idx < init_nodes.nelem; ++clexp_idx)
1031 + {
1032 + re_token_t *clexp_node;
1033 + clexp_node = dfa->nodes + init_nodes.elems[clexp_idx];
1034 + if (clexp_node->type == OP_CLOSE_SUBEXP
1035 + && clexp_node->opr.idx == dfa->nodes[node_idx].opr.idx)
1036 + break;
1037 + }
1038 + if (clexp_idx == init_nodes.nelem)
1039 + continue;
1040 +
1041 + if (type == OP_BACK_REF)
1042 + {
1043 + int dest_idx = dfa->edests[node_idx].elems[0];
1044 + if (!re_node_set_contains (&init_nodes, dest_idx))
1045 + {
1046 + reg_errcode_t err = re_node_set_merge (&init_nodes,
1047 + dfa->eclosures
1048 + + dest_idx);
1049 + if (err != REG_NOERROR)
1050 + return err;
1051 + i = 0;
1052 + }
1053 + }
1054 + }
1055 +
1056 + /* It must be the first time to invoke acquire_state. */
1057 + dfa->init_state = re_acquire_state_context (&err, dfa, &init_nodes, 0);
1058 + /* We don't check ERR here, since the initial state must not be NULL. */
1059 + if (BE (dfa->init_state == NULL, 0))
1060 + return err;
1061 + if (dfa->init_state->has_constraint)
1062 + {
1063 + dfa->init_state_word = re_acquire_state_context (&err, dfa, &init_nodes,
1064 + CONTEXT_WORD);
1065 + dfa->init_state_nl = re_acquire_state_context (&err, dfa, &init_nodes,
1066 + CONTEXT_NEWLINE);
1067 + dfa->init_state_begbuf = re_acquire_state_context (&err, dfa,
1068 + &init_nodes,
1069 + CONTEXT_NEWLINE
1070 + | CONTEXT_BEGBUF);
1071 + if (BE (dfa->init_state_word == NULL || dfa->init_state_nl == NULL
1072 + || dfa->init_state_begbuf == NULL, 0))
1073 + return err;
1074 + }
1075 + else
1076 + dfa->init_state_word = dfa->init_state_nl
1077 + = dfa->init_state_begbuf = dfa->init_state;
1078 +
1079 + re_node_set_free (&init_nodes);
1080 + return REG_NOERROR;
1081 +}
1082 +
1083 +#ifdef RE_ENABLE_I18N
1084 +/* If it is possible to do searching in single byte encoding instead of UTF-8
1085 + to speed things up, set dfa->mb_cur_max to 1, clear is_utf8 and change
1086 + DFA nodes where needed. */
1087 +
1088 +static void
1089 +optimize_utf8 (re_dfa_t *dfa)
1090 +{
1091 + int node, i, mb_chars = 0, has_period = 0;
1092 +
1093 + for (node = 0; node < dfa->nodes_len; ++node)
1094 + switch (dfa->nodes[node].type)
1095 + {
1096 + case CHARACTER:
1097 + if (dfa->nodes[node].opr.c >= 0x80)
1098 + mb_chars = 1;
1099 + break;
1100 + case ANCHOR:
1101 + switch (dfa->nodes[node].opr.ctx_type)
1102 + {
1103 + case LINE_FIRST:
1104 + case LINE_LAST:
1105 + case BUF_FIRST:
1106 + case BUF_LAST:
1107 + break;
1108 + default:
1109 + /* Word anchors etc. cannot be handled. It's okay to test
1110 + opr.ctx_type since constraints (for all DFA nodes) are
1111 + created by ORing one or more opr.ctx_type values. */
1112 + return;
1113 + }
1114 + break;
1115 + case OP_PERIOD:
1116 + has_period = 1;
1117 + break;
1118 + case OP_BACK_REF:
1119 + case OP_ALT:
1120 + case END_OF_RE:
1121 + case OP_DUP_ASTERISK:
1122 + case OP_OPEN_SUBEXP:
1123 + case OP_CLOSE_SUBEXP:
1124 + break;
1125 + case COMPLEX_BRACKET:
1126 + return;
1127 + case SIMPLE_BRACKET:
1128 + /* Just double check. The non-ASCII range starts at 0x80. */
1129 + assert (0x80 % BITSET_WORD_BITS == 0);
1130 + for (i = 0x80 / BITSET_WORD_BITS; i < BITSET_WORDS; ++i)
1131 + if (dfa->nodes[node].opr.sbcset[i])
1132 + return;
1133 + break;
1134 + default:
1135 + abort ();
1136 + }
1137 +
1138 + if (mb_chars || has_period)
1139 + for (node = 0; node < dfa->nodes_len; ++node)
1140 + {
1141 + if (dfa->nodes[node].type == CHARACTER
1142 + && dfa->nodes[node].opr.c >= 0x80)
1143 + dfa->nodes[node].mb_partial = 0;
1144 + else if (dfa->nodes[node].type == OP_PERIOD)
1145 + dfa->nodes[node].type = OP_UTF8_PERIOD;
1146 + }
1147 +
1148 + /* The search can be in single byte locale. */
1149 + dfa->mb_cur_max = 1;
1150 + dfa->is_utf8 = 0;
1151 + dfa->has_mb_node = dfa->nbackref > 0 || has_period;
1152 +}
1153 +#endif
1154 +
1155 +/* Analyze the structure tree, and calculate "first", "next", "edest",
1156 + "eclosure", and "inveclosure". */
1157 +
1158 +static reg_errcode_t
1159 +analyze (regex_t *preg)
1160 +{
1161 + re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
1162 + reg_errcode_t ret;
1163 +
1164 + /* Allocate arrays. */
1165 + dfa->nexts = re_malloc (int, dfa->nodes_alloc);
1166 + dfa->org_indices = re_malloc (int, dfa->nodes_alloc);
1167 + dfa->edests = re_malloc (re_node_set, dfa->nodes_alloc);
1168 + dfa->eclosures = re_malloc (re_node_set, dfa->nodes_alloc);
1169 + if (BE (dfa->nexts == NULL || dfa->org_indices == NULL || dfa->edests == NULL
1170 + || dfa->eclosures == NULL, 0))
1171 + return REG_ESPACE;
1172 +
1173 + dfa->subexp_map = re_malloc (int, preg->re_nsub);
1174 + if (dfa->subexp_map != NULL)
1175 + {
1176 + int i;
1177 + for (i = 0; i < preg->re_nsub; i++)
1178 + dfa->subexp_map[i] = i;
1179 + preorder (dfa->str_tree, optimize_subexps, dfa);
1180 + for (i = 0; i < preg->re_nsub; i++)
1181 + if (dfa->subexp_map[i] != i)
1182 + break;
1183 + if (i == preg->re_nsub)
1184 + {
1185 + free (dfa->subexp_map);
1186 + dfa->subexp_map = NULL;
1187 + }
1188 + }
1189 +
1190 + ret = postorder (dfa->str_tree, lower_subexps, preg);
1191 + if (BE (ret != REG_NOERROR, 0))
1192 + return ret;
1193 + ret = postorder (dfa->str_tree, calc_first, dfa);
1194 + if (BE (ret != REG_NOERROR, 0))
1195 + return ret;
1196 + preorder (dfa->str_tree, calc_next, dfa);
1197 + ret = preorder (dfa->str_tree, link_nfa_nodes, dfa);
1198 + if (BE (ret != REG_NOERROR, 0))
1199 + return ret;
1200 + ret = calc_eclosure (dfa);
1201 + if (BE (ret != REG_NOERROR, 0))
1202 + return ret;
1203 +
1204 + /* We only need this during the prune_impossible_nodes pass in regexec.c;
1205 + skip it if p_i_n will not run, as calc_inveclosure can be quadratic. */
1206 + if ((!preg->no_sub && preg->re_nsub > 0 && dfa->has_plural_match)
1207 + || dfa->nbackref)
1208 + {
1209 + dfa->inveclosures = re_malloc (re_node_set, dfa->nodes_len);
1210 + if (BE (dfa->inveclosures == NULL, 0))
1211 + return REG_ESPACE;
1212 + ret = calc_inveclosure (dfa);
1213 + }
1214 +
1215 + return ret;
1216 +}
1217 +
1218 +/* Our parse trees are very unbalanced, so we cannot use a stack to
1219 + implement parse tree visits. Instead, we use parent pointers and
1220 + some hairy code in these two functions. */
1221 +static reg_errcode_t
1222 +postorder (bin_tree_t *root, reg_errcode_t (fn (void *, bin_tree_t *)),
1223 + void *extra)
1224 +{
1225 + bin_tree_t *node, *prev;
1226 +
1227 + for (node = root; ; )
1228 + {
1229 + /* Descend down the tree, preferably to the left (or to the right
1230 + if that's the only child). */
1231 + while (node->left || node->right)
1232 + if (node->left)
1233 + node = node->left;
1234 + else
1235 + node = node->right;
1236 +
1237 + do
1238 + {
1239 + reg_errcode_t err = fn (extra, node);
1240 + if (BE (err != REG_NOERROR, 0))
1241 + return err;
1242 + if (node->parent == NULL)
1243 + return REG_NOERROR;
1244 + prev = node;
1245 + node = node->parent;
1246 + }
1247 + /* Go up while we have a node that is reached from the right. */
1248 + while (node->right == prev || node->right == NULL);
1249 + node = node->right;
1250 + }
1251 +}
1252 +
1253 +static reg_errcode_t
1254 +preorder (bin_tree_t *root, reg_errcode_t (fn (void *, bin_tree_t *)),
1255 + void *extra)
1256 +{
1257 + bin_tree_t *node;
1258 +
1259 + for (node = root; ; )
1260 + {
1261 + reg_errcode_t err = fn (extra, node);
1262 + if (BE (err != REG_NOERROR, 0))
1263 + return err;
1264 +
1265 + /* Go to the left node, or up and to the right. */
1266 + if (node->left)
1267 + node = node->left;
1268 + else
1269 + {
1270 + bin_tree_t *prev = NULL;
1271 + while (node->right == prev || node->right == NULL)
1272 + {
1273 + prev = node;
1274 + node = node->parent;
1275 + if (!node)
1276 + return REG_NOERROR;
1277 + }
1278 + node = node->right;
1279 + }
1280 + }
1281 +}
1282 +
1283 +/* Optimization pass: if a SUBEXP is entirely contained, strip it and tell
1284 + re_search_internal to map the inner one's opr.idx to this one's. Adjust
1285 + backreferences as well. Requires a preorder visit. */
1286 +static reg_errcode_t
1287 +optimize_subexps (void *extra, bin_tree_t *node)
1288 +{
1289 + re_dfa_t *dfa = (re_dfa_t *) extra;
1290 +
1291 + if (node->token.type == OP_BACK_REF && dfa->subexp_map)
1292 + {
1293 + int idx = node->token.opr.idx;
1294 + node->token.opr.idx = dfa->subexp_map[idx];
1295 + dfa->used_bkref_map |= 1 << node->token.opr.idx;
1296 + }
1297 +
1298 + else if (node->token.type == SUBEXP
1299 + && node->left && node->left->token.type == SUBEXP)
1300 + {
1301 + int other_idx = node->left->token.opr.idx;
1302 +
1303 + node->left = node->left->left;
1304 + if (node->left)
1305 + node->left->parent = node;
1306 +
1307 + dfa->subexp_map[other_idx] = dfa->subexp_map[node->token.opr.idx];
1308 + if (other_idx < BITSET_WORD_BITS)
1309 + dfa->used_bkref_map &= ~((bitset_word_t) 1 << other_idx);
1310 + }
1311 +
1312 + return REG_NOERROR;
1313 +}
1314 +
1315 +/* Lowering pass: Turn each SUBEXP node into the appropriate concatenation
1316 + of OP_OPEN_SUBEXP, the body of the SUBEXP (if any) and OP_CLOSE_SUBEXP. */
1317 +static reg_errcode_t
1318 +lower_subexps (void *extra, bin_tree_t *node)
1319 +{
1320 + regex_t *preg = (regex_t *) extra;
1321 + reg_errcode_t err = REG_NOERROR;
1322 +
1323 + if (node->left && node->left->token.type == SUBEXP)
1324 + {
1325 + node->left = lower_subexp (&err, preg, node->left);
1326 + if (node->left)
1327 + node->left->parent = node;
1328 + }
1329 + if (node->right && node->right->token.type == SUBEXP)
1330 + {
1331 + node->right = lower_subexp (&err, preg, node->right);
1332 + if (node->right)
1333 + node->right->parent = node;
1334 + }
1335 +
1336 + return err;
1337 +}
1338 +
1339 +static bin_tree_t *
1340 +lower_subexp (reg_errcode_t *err, regex_t *preg, bin_tree_t *node)
1341 +{
1342 + re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
1343 + bin_tree_t *body = node->left;
1344 + bin_tree_t *op, *cls, *tree1, *tree;
1345 +
1346 + if (preg->no_sub
1347 + /* We do not optimize empty subexpressions, because otherwise we may
1348 + have bad CONCAT nodes with NULL children. This is obviously not
1349 + very common, so we do not lose much. An example that triggers
1350 + this case is the sed "script" /\(\)/x. */
1351 + && node->left != NULL
1352 + && (node->token.opr.idx >= BITSET_WORD_BITS
1353 + || !(dfa->used_bkref_map
1354 + & ((bitset_word_t) 1 << node->token.opr.idx))))
1355 + return node->left;
1356 +
1357 + /* Convert the SUBEXP node to the concatenation of an
1358 + OP_OPEN_SUBEXP, the contents, and an OP_CLOSE_SUBEXP. */
1359 + op = create_tree (dfa, NULL, NULL, OP_OPEN_SUBEXP);
1360 + cls = create_tree (dfa, NULL, NULL, OP_CLOSE_SUBEXP);
1361 + tree1 = body ? create_tree (dfa, body, cls, CONCAT) : cls;
1362 + tree = create_tree (dfa, op, tree1, CONCAT);
1363 + if (BE (tree == NULL || tree1 == NULL || op == NULL || cls == NULL, 0))
1364 + {
1365 + *err = REG_ESPACE;
1366 + return NULL;
1367 + }
1368 +
1369 + op->token.opr.idx = cls->token.opr.idx = node->token.opr.idx;
1370 + op->token.opt_subexp = cls->token.opt_subexp = node->token.opt_subexp;
1371 + return tree;
1372 +}
1373 +
1374 +/* Pass 1 in building the NFA: compute FIRST and create unlinked automaton
1375 + nodes. Requires a postorder visit. */
1376 +static reg_errcode_t
1377 +calc_first (void *extra, bin_tree_t *node)
1378 +{
1379 + re_dfa_t *dfa = (re_dfa_t *) extra;
1380 + if (node->token.type == CONCAT)
1381 + {
1382 + node->first = node->left->first;
1383 + node->node_idx = node->left->node_idx;
1384 + }
1385 + else
1386 + {
1387 + node->first = node;
1388 + node->node_idx = re_dfa_add_node (dfa, node->token);
1389 + if (BE (node->node_idx == -1, 0))
1390 + return REG_ESPACE;
1391 + if (node->token.type == ANCHOR)
1392 + dfa->nodes[node->node_idx].constraint = node->token.opr.ctx_type;
1393 + }
1394 + return REG_NOERROR;
1395 +}
1396 +
1397 +/* Pass 2: compute NEXT on the tree. Preorder visit. */
1398 +static reg_errcode_t
1399 +calc_next (void *extra, bin_tree_t *node)
1400 +{
1401 + switch (node->token.type)
1402 + {
1403 + case OP_DUP_ASTERISK:
1404 + node->left->next = node;
1405 + break;
1406 + case CONCAT:
1407 + node->left->next = node->right->first;
1408 + node->right->next = node->next;
1409 + break;
1410 + default:
1411 + if (node->left)
1412 + node->left->next = node->next;
1413 + if (node->right)
1414 + node->right->next = node->next;
1415 + break;
1416 + }
1417 + return REG_NOERROR;
1418 +}
1419 +
1420 +/* Pass 3: link all DFA nodes to their NEXT node (any order will do). */
1421 +static reg_errcode_t
1422 +link_nfa_nodes (void *extra, bin_tree_t *node)
1423 +{
1424 + re_dfa_t *dfa = (re_dfa_t *) extra;
1425 + int idx = node->node_idx;
1426 + reg_errcode_t err = REG_NOERROR;
1427 +
1428 + switch (node->token.type)
1429 + {
1430 + case CONCAT:
1431 + break;
1432 +
1433 + case END_OF_RE:
1434 + assert (node->next == NULL);
1435 + break;
1436 +
1437 + case OP_DUP_ASTERISK:
1438 + case OP_ALT:
1439 + {
1440 + int left, right;
1441 + dfa->has_plural_match = 1;
1442 + if (node->left != NULL)
1443 + left = node->left->first->node_idx;
1444 + else
1445 + left = node->next->node_idx;
1446 + if (node->right != NULL)
1447 + right = node->right->first->node_idx;
1448 + else
1449 + right = node->next->node_idx;
1450 + assert (left > -1);
1451 + assert (right > -1);
1452 + err = re_node_set_init_2 (dfa->edests + idx, left, right);
1453 + }
1454 + break;
1455 +
1456 + case ANCHOR:
1457 + case OP_OPEN_SUBEXP:
1458 + case OP_CLOSE_SUBEXP:
1459 + err = re_node_set_init_1 (dfa->edests + idx, node->next->node_idx);
1460 + break;
1461 +
1462 + case OP_BACK_REF:
1463 + dfa->nexts[idx] = node->next->node_idx;
1464 + if (node->token.type == OP_BACK_REF)
1465 + err = re_node_set_init_1 (dfa->edests + idx, dfa->nexts[idx]);
1466 + break;
1467 +
1468 + default:
1469 + assert (!IS_EPSILON_NODE (node->token.type));
1470 + dfa->nexts[idx] = node->next->node_idx;
1471 + break;
1472 + }
1473 +
1474 + return err;
1475 +}
1476 +
1477 +/* Duplicate the epsilon closure of the node ROOT_NODE.
1478 + Note that duplicated nodes have constraint INIT_CONSTRAINT in addition
1479 + to their own constraint. */
1480 +
1481 +static reg_errcode_t
1482 +internal_function
1483 +duplicate_node_closure (re_dfa_t *dfa, int top_org_node, int top_clone_node,
1484 + int root_node, unsigned int init_constraint)
1485 +{
1486 + int org_node, clone_node, ret;
1487 + unsigned int constraint = init_constraint;
1488 + for (org_node = top_org_node, clone_node = top_clone_node;;)
1489 + {
1490 + int org_dest, clone_dest;
1491 + if (dfa->nodes[org_node].type == OP_BACK_REF)
1492 + {
1493 + /* If the back reference epsilon-transit, its destination must
1494 + also have the constraint. Then duplicate the epsilon closure
1495 + of the destination of the back reference, and store it in
1496 + edests of the back reference. */
1497 + org_dest = dfa->nexts[org_node];
1498 + re_node_set_empty (dfa->edests + clone_node);
1499 + clone_dest = duplicate_node (dfa, org_dest, constraint);
1500 + if (BE (clone_dest == -1, 0))
1501 + return REG_ESPACE;
1502 + dfa->nexts[clone_node] = dfa->nexts[org_node];
1503 + ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
1504 + if (BE (ret < 0, 0))
1505 + return REG_ESPACE;
1506 + }
1507 + else if (dfa->edests[org_node].nelem == 0)
1508 + {
1509 + /* In case of the node can't epsilon-transit, don't duplicate the
1510 + destination and store the original destination as the
1511 + destination of the node. */
1512 + dfa->nexts[clone_node] = dfa->nexts[org_node];
1513 + break;
1514 + }
1515 + else if (dfa->edests[org_node].nelem == 1)
1516 + {
1517 + /* In case of the node can epsilon-transit, and it has only one
1518 + destination. */
1519 + org_dest = dfa->edests[org_node].elems[0];
1520 + re_node_set_empty (dfa->edests + clone_node);
1521 + /* If the node is root_node itself, it means the epsilon clsoure
1522 + has a loop. Then tie it to the destination of the root_node. */
1523 + if (org_node == root_node && clone_node != org_node)
1524 + {
1525 + ret = re_node_set_insert (dfa->edests + clone_node, org_dest);
1526 + if (BE (ret < 0, 0))
1527 + return REG_ESPACE;
1528 + break;
1529 + }
1530 + /* In case of the node has another constraint, add it. */
1531 + constraint |= dfa->nodes[org_node].constraint;
1532 + clone_dest = duplicate_node (dfa, org_dest, constraint);
1533 + if (BE (clone_dest == -1, 0))
1534 + return REG_ESPACE;
1535 + ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
1536 + if (BE (ret < 0, 0))
1537 + return REG_ESPACE;
1538 + }
1539 + else /* dfa->edests[org_node].nelem == 2 */
1540 + {
1541 + /* In case of the node can epsilon-transit, and it has two
1542 + destinations. In the bin_tree_t and DFA, that's '|' and '*'. */
1543 + org_dest = dfa->edests[org_node].elems[0];
1544 + re_node_set_empty (dfa->edests + clone_node);
1545 + /* Search for a duplicated node which satisfies the constraint. */
1546 + clone_dest = search_duplicated_node (dfa, org_dest, constraint);
1547 + if (clone_dest == -1)
1548 + {
1549 + /* There is no such duplicated node, create a new one. */
1550 + reg_errcode_t err;
1551 + clone_dest = duplicate_node (dfa, org_dest, constraint);
1552 + if (BE (clone_dest == -1, 0))
1553 + return REG_ESPACE;
1554 + ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
1555 + if (BE (ret < 0, 0))
1556 + return REG_ESPACE;
1557 + err = duplicate_node_closure (dfa, org_dest, clone_dest,
1558 + root_node, constraint);
1559 + if (BE (err != REG_NOERROR, 0))
1560 + return err;
1561 + }
1562 + else
1563 + {
1564 + /* There is a duplicated node which satisfies the constraint,
1565 + use it to avoid infinite loop. */
1566 + ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
1567 + if (BE (ret < 0, 0))
1568 + return REG_ESPACE;
1569 + }
1570 +
1571 + org_dest = dfa->edests[org_node].elems[1];
1572 + clone_dest = duplicate_node (dfa, org_dest, constraint);
1573 + if (BE (clone_dest == -1, 0))
1574 + return REG_ESPACE;
1575 + ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
1576 + if (BE (ret < 0, 0))
1577 + return REG_ESPACE;
1578 + }
1579 + org_node = org_dest;
1580 + clone_node = clone_dest;
1581 + }
1582 + return REG_NOERROR;
1583 +}
1584 +
1585 +/* Search for a node which is duplicated from the node ORG_NODE, and
1586 + satisfies the constraint CONSTRAINT. */
1587 +
1588 +static int
1589 +search_duplicated_node (const re_dfa_t *dfa, int org_node,
1590 + unsigned int constraint)
1591 +{
1592 + int idx;
1593 + for (idx = dfa->nodes_len - 1; dfa->nodes[idx].duplicated && idx > 0; --idx)
1594 + {
1595 + if (org_node == dfa->org_indices[idx]
1596 + && constraint == dfa->nodes[idx].constraint)
1597 + return idx; /* Found. */
1598 + }
1599 + return -1; /* Not found. */
1600 +}
1601 +
1602 +/* Duplicate the node whose index is ORG_IDX and set the constraint CONSTRAINT.
1603 + Return the index of the new node, or -1 if insufficient storage is
1604 + available. */
1605 +
1606 +static int
1607 +duplicate_node (re_dfa_t *dfa, int org_idx, unsigned int constraint)
1608 +{
1609 + int dup_idx = re_dfa_add_node (dfa, dfa->nodes[org_idx]);
1610 + if (BE (dup_idx != -1, 1))
1611 + {
1612 + dfa->nodes[dup_idx].constraint = constraint;
1613 + dfa->nodes[dup_idx].constraint |= dfa->nodes[org_idx].constraint;
1614 + dfa->nodes[dup_idx].duplicated = 1;
1615 +
1616 + /* Store the index of the original node. */
1617 + dfa->org_indices[dup_idx] = org_idx;
1618 + }
1619 + return dup_idx;
1620 +}
1621 +
1622 +static reg_errcode_t
1623 +calc_inveclosure (re_dfa_t *dfa)
1624 +{
1625 + int src, idx, ret;
1626 + for (idx = 0; idx < dfa->nodes_len; ++idx)
1627 + re_node_set_init_empty (dfa->inveclosures + idx);
1628 +
1629 + for (src = 0; src < dfa->nodes_len; ++src)
1630 + {
1631 + int *elems = dfa->eclosures[src].elems;
1632 + for (idx = 0; idx < dfa->eclosures[src].nelem; ++idx)
1633 + {
1634 + ret = re_node_set_insert_last (dfa->inveclosures + elems[idx], src);
1635 + if (BE (ret == -1, 0))
1636 + return REG_ESPACE;
1637 + }
1638 + }
1639 +
1640 + return REG_NOERROR;
1641 +}
1642 +
1643 +/* Calculate "eclosure" for all the node in DFA. */
1644 +
1645 +static reg_errcode_t
1646 +calc_eclosure (re_dfa_t *dfa)
1647 +{
1648 + int node_idx, incomplete;
1649 +#ifdef DEBUG
1650 + assert (dfa->nodes_len > 0);
1651 +#endif
1652 + incomplete = 0;
1653 + /* For each nodes, calculate epsilon closure. */
1654 + for (node_idx = 0; ; ++node_idx)
1655 + {
1656 + reg_errcode_t err;
1657 + re_node_set eclosure_elem;
1658 + if (node_idx == dfa->nodes_len)
1659 + {
1660 + if (!incomplete)
1661 + break;
1662 + incomplete = 0;
1663 + node_idx = 0;
1664 + }
1665 +
1666 +#ifdef DEBUG
1667 + assert (dfa->eclosures[node_idx].nelem != -1);
1668 +#endif
1669 +
1670 + /* If we have already calculated, skip it. */
1671 + if (dfa->eclosures[node_idx].nelem != 0)
1672 + continue;
1673 + /* Calculate epsilon closure of `node_idx'. */
1674 + err = calc_eclosure_iter (&eclosure_elem, dfa, node_idx, 1);
1675 + if (BE (err != REG_NOERROR, 0))
1676 + return err;
1677 +
1678 + if (dfa->eclosures[node_idx].nelem == 0)
1679 + {
1680 + incomplete = 1;
1681 + re_node_set_free (&eclosure_elem);
1682 + }
1683 + }
1684 + return REG_NOERROR;
1685 +}
1686 +
1687 +/* Calculate epsilon closure of NODE. */
1688 +
1689 +static reg_errcode_t
1690 +calc_eclosure_iter (re_node_set *new_set, re_dfa_t *dfa, int node, int root)
1691 +{
1692 + reg_errcode_t err;
1693 + int i;
1694 + re_node_set eclosure;
1695 + int ret;
1696 + int incomplete = 0;
1697 + err = re_node_set_alloc (&eclosure, dfa->edests[node].nelem + 1);
1698 + if (BE (err != REG_NOERROR, 0))
1699 + return err;
1700 +
1701 + /* This indicates that we are calculating this node now.
1702 + We reference this value to avoid infinite loop. */
1703 + dfa->eclosures[node].nelem = -1;
1704 +
1705 + /* If the current node has constraints, duplicate all nodes
1706 + since they must inherit the constraints. */
1707 + if (dfa->nodes[node].constraint
1708 + && dfa->edests[node].nelem
1709 + && !dfa->nodes[dfa->edests[node].elems[0]].duplicated)
1710 + {
1711 + err = duplicate_node_closure (dfa, node, node, node,
1712 + dfa->nodes[node].constraint);
1713 + if (BE (err != REG_NOERROR, 0))
1714 + return err;
1715 + }
1716 +
1717 + /* Expand each epsilon destination nodes. */
1718 + if (IS_EPSILON_NODE(dfa->nodes[node].type))
1719 + for (i = 0; i < dfa->edests[node].nelem; ++i)
1720 + {
1721 + re_node_set eclosure_elem;
1722 + int edest = dfa->edests[node].elems[i];
1723 + /* If calculating the epsilon closure of `edest' is in progress,
1724 + return intermediate result. */
1725 + if (dfa->eclosures[edest].nelem == -1)
1726 + {
1727 + incomplete = 1;
1728 + continue;
1729 + }
1730 + /* If we haven't calculated the epsilon closure of `edest' yet,
1731 + calculate now. Otherwise use calculated epsilon closure. */
1732 + if (dfa->eclosures[edest].nelem == 0)
1733 + {
1734 + err = calc_eclosure_iter (&eclosure_elem, dfa, edest, 0);
1735 + if (BE (err != REG_NOERROR, 0))
1736 + return err;
1737 + }
1738 + else
1739 + eclosure_elem = dfa->eclosures[edest];
1740 + /* Merge the epsilon closure of `edest'. */
1741 + err = re_node_set_merge (&eclosure, &eclosure_elem);
1742 + if (BE (err != REG_NOERROR, 0))
1743 + return err;
1744 + /* If the epsilon closure of `edest' is incomplete,
1745 + the epsilon closure of this node is also incomplete. */
1746 + if (dfa->eclosures[edest].nelem == 0)
1747 + {
1748 + incomplete = 1;
1749 + re_node_set_free (&eclosure_elem);
1750 + }
1751 + }
1752 +
1753 + /* An epsilon closure includes itself. */
1754 + ret = re_node_set_insert (&eclosure, node);
1755 + if (BE (ret < 0, 0))
1756 + return REG_ESPACE;
1757 + if (incomplete && !root)
1758 + dfa->eclosures[node].nelem = 0;
1759 + else
1760 + dfa->eclosures[node] = eclosure;
1761 + *new_set = eclosure;
1762 + return REG_NOERROR;
1763 +}
1764 +
1765 +/* Functions for token which are used in the parser. */
1766 +
1767 +/* Fetch a token from INPUT.
1768 + We must not use this function inside bracket expressions. */
1769 +
1770 +static void
1771 +internal_function
1772 +fetch_token (re_token_t *result, re_string_t *input, reg_syntax_t syntax)
1773 +{
1774 + re_string_skip_bytes (input, peek_token (result, input, syntax));
1775 +}
1776 +
1777 +/* Peek a token from INPUT, and return the length of the token.
1778 + We must not use this function inside bracket expressions. */
1779 +
1780 +static int
1781 +internal_function
1782 +peek_token (re_token_t *token, re_string_t *input, reg_syntax_t syntax)
1783 +{
1784 + unsigned char c;
1785 +
1786 + if (re_string_eoi (input))
1787 + {
1788 + token->type = END_OF_RE;
1789 + return 0;
1790 + }
1791 +
1792 + c = re_string_peek_byte (input, 0);
1793 + token->opr.c = c;
1794 +
1795 + token->word_char = 0;
1796 +#ifdef RE_ENABLE_I18N
1797 + token->mb_partial = 0;
1798 + if (input->mb_cur_max > 1 &&
1799 + !re_string_first_byte (input, re_string_cur_idx (input)))
1800 + {
1801 + token->type = CHARACTER;
1802 + token->mb_partial = 1;
1803 + return 1;
1804 + }
1805 +#endif
1806 + if (c == '\\')
1807 + {
1808 + unsigned char c2;
1809 + if (re_string_cur_idx (input) + 1 >= re_string_length (input))
1810 + {
1811 + token->type = BACK_SLASH;
1812 + return 1;
1813 + }
1814 +
1815 + c2 = re_string_peek_byte_case (input, 1);
1816 + token->opr.c = c2;
1817 + token->type = CHARACTER;
1818 +#ifdef RE_ENABLE_I18N
1819 + if (input->mb_cur_max > 1)
1820 + {
1821 + wint_t wc = re_string_wchar_at (input,
1822 + re_string_cur_idx (input) + 1);
1823 + token->word_char = IS_WIDE_WORD_CHAR (wc) != 0;
1824 + }
1825 + else
1826 +#endif
1827 + token->word_char = IS_WORD_CHAR (c2) != 0;
1828 +
1829 + switch (c2)
1830 + {
1831 + case '|':
1832 + if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_NO_BK_VBAR))
1833 + token->type = OP_ALT;
1834 + break;
1835 + case '1': case '2': case '3': case '4': case '5':
1836 + case '6': case '7': case '8': case '9':
1837 + if (!(syntax & RE_NO_BK_REFS))
1838 + {
1839 + token->type = OP_BACK_REF;
1840 + token->opr.idx = c2 - '1';
1841 + }
1842 + break;
1843 + case '<':
1844 + if (!(syntax & RE_NO_GNU_OPS))
1845 + {
1846 + token->type = ANCHOR;
1847 + token->opr.ctx_type = WORD_FIRST;
1848 + }
1849 + break;
1850 + case '>':
1851 + if (!(syntax & RE_NO_GNU_OPS))
1852 + {
1853 + token->type = ANCHOR;
1854 + token->opr.ctx_type = WORD_LAST;
1855 + }
1856 + break;
1857 + case 'b':
1858 + if (!(syntax & RE_NO_GNU_OPS))
1859 + {
1860 + token->type = ANCHOR;
1861 + token->opr.ctx_type = WORD_DELIM;
1862 + }
1863 + break;
1864 + case 'B':
1865 + if (!(syntax & RE_NO_GNU_OPS))
1866 + {
1867 + token->type = ANCHOR;
1868 + token->opr.ctx_type = NOT_WORD_DELIM;
1869 + }
1870 + break;
1871 + case 'w':
1872 + if (!(syntax & RE_NO_GNU_OPS))
1873 + token->type = OP_WORD;
1874 + break;
1875 + case 'W':
1876 + if (!(syntax & RE_NO_GNU_OPS))
1877 + token->type = OP_NOTWORD;
1878 + break;
1879 + case 's':
1880 + if (!(syntax & RE_NO_GNU_OPS))
1881 + token->type = OP_SPACE;
1882 + break;
1883 + case 'S':
1884 + if (!(syntax & RE_NO_GNU_OPS))
1885 + token->type = OP_NOTSPACE;
1886 + break;
1887 + case '`':
1888 + if (!(syntax & RE_NO_GNU_OPS))
1889 + {
1890 + token->type = ANCHOR;
1891 + token->opr.ctx_type = BUF_FIRST;
1892 + }
1893 + break;
1894 + case '\'':
1895 + if (!(syntax & RE_NO_GNU_OPS))
1896 + {
1897 + token->type = ANCHOR;
1898 + token->opr.ctx_type = BUF_LAST;
1899 + }
1900 + break;
1901 + case '(':
1902 + if (!(syntax & RE_NO_BK_PARENS))
1903 + token->type = OP_OPEN_SUBEXP;
1904 + break;
1905 + case ')':
1906 + if (!(syntax & RE_NO_BK_PARENS))
1907 + token->type = OP_CLOSE_SUBEXP;
1908 + break;
1909 + case '+':
1910 + if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_BK_PLUS_QM))
1911 + token->type = OP_DUP_PLUS;
1912 + break;
1913 + case '?':
1914 + if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_BK_PLUS_QM))
1915 + token->type = OP_DUP_QUESTION;
1916 + break;
1917 + case '{':
1918 + if ((syntax & RE_INTERVALS) && (!(syntax & RE_NO_BK_BRACES)))
1919 + token->type = OP_OPEN_DUP_NUM;
1920 + break;
1921 + case '}':
1922 + if ((syntax & RE_INTERVALS) && (!(syntax & RE_NO_BK_BRACES)))
1923 + token->type = OP_CLOSE_DUP_NUM;
1924 + break;
1925 + default:
1926 + break;
1927 + }
1928 + return 2;
1929 + }
1930 +
1931 + token->type = CHARACTER;
1932 +#ifdef RE_ENABLE_I18N
1933 + if (input->mb_cur_max > 1)
1934 + {
1935 + wint_t wc = re_string_wchar_at (input, re_string_cur_idx (input));
1936 + token->word_char = IS_WIDE_WORD_CHAR (wc) != 0;
1937 + }
1938 + else
1939 +#endif
1940 + token->word_char = IS_WORD_CHAR (token->opr.c);
1941 +
1942 + switch (c)
1943 + {
1944 + case '\n':
1945 + if (syntax & RE_NEWLINE_ALT)
1946 + token->type = OP_ALT;
1947 + break;
1948 + case '|':
1949 + if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_NO_BK_VBAR))
1950 + token->type = OP_ALT;
1951 + break;
1952 + case '*':
1953 + token->type = OP_DUP_ASTERISK;
1954 + break;
1955 + case '+':
1956 + if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_BK_PLUS_QM))
1957 + token->type = OP_DUP_PLUS;
1958 + break;
1959 + case '?':
1960 + if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_BK_PLUS_QM))
1961 + token->type = OP_DUP_QUESTION;
1962 + break;
1963 + case '{':
1964 + if ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
1965 + token->type = OP_OPEN_DUP_NUM;
1966 + break;
1967 + case '}':
1968 + if ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
1969 + token->type = OP_CLOSE_DUP_NUM;
1970 + break;
1971 + case '(':
1972 + if (syntax & RE_NO_BK_PARENS)
1973 + token->type = OP_OPEN_SUBEXP;
1974 + break;
1975 + case ')':
1976 + if (syntax & RE_NO_BK_PARENS)
1977 + token->type = OP_CLOSE_SUBEXP;
1978 + break;
1979 + case '[':
1980 + token->type = OP_OPEN_BRACKET;
1981 + break;
1982 + case '.':
1983 + token->type = OP_PERIOD;
1984 + break;
1985 + case '^':
1986 + if (!(syntax & (RE_CONTEXT_INDEP_ANCHORS | RE_CARET_ANCHORS_HERE)) &&
1987 + re_string_cur_idx (input) != 0)
1988 + {
1989 + char prev = re_string_peek_byte (input, -1);
1990 + if (!(syntax & RE_NEWLINE_ALT) || prev != '\n')
1991 + break;
1992 + }
1993 + token->type = ANCHOR;
1994 + token->opr.ctx_type = LINE_FIRST;
1995 + break;
1996 + case '$':
1997 + if (!(syntax & RE_CONTEXT_INDEP_ANCHORS) &&
1998 + re_string_cur_idx (input) + 1 != re_string_length (input))
1999 + {
2000 + re_token_t next;
2001 + re_string_skip_bytes (input, 1);
2002 + peek_token (&next, input, syntax);
2003 + re_string_skip_bytes (input, -1);
2004 + if (next.type != OP_ALT && next.type != OP_CLOSE_SUBEXP)
2005 + break;
2006 + }
2007 + token->type = ANCHOR;
2008 + token->opr.ctx_type = LINE_LAST;
2009 + break;
2010 + default:
2011 + break;
2012 + }
2013 + return 1;
2014 +}
2015 +
2016 +/* Peek a token from INPUT, and return the length of the token.
2017 + We must not use this function out of bracket expressions. */
2018 +
2019 +static int
2020 +internal_function
2021 +peek_token_bracket (re_token_t *token, re_string_t *input, reg_syntax_t syntax)
2022 +{
2023 + unsigned char c;
2024 + if (re_string_eoi (input))
2025 + {
2026 + token->type = END_OF_RE;
2027 + return 0;
2028 + }
2029 + c = re_string_peek_byte (input, 0);
2030 + token->opr.c = c;
2031 +
2032 +#ifdef RE_ENABLE_I18N
2033 + if (input->mb_cur_max > 1 &&
2034 + !re_string_first_byte (input, re_string_cur_idx (input)))
2035 + {
2036 + token->type = CHARACTER;
2037 + return 1;
2038 + }
2039 +#endif /* RE_ENABLE_I18N */
2040 +
2041 + if (c == '\\' && (syntax & RE_BACKSLASH_ESCAPE_IN_LISTS)
2042 + && re_string_cur_idx (input) + 1 < re_string_length (input))
2043 + {
2044 + /* In this case, '\' escape a character. */
2045 + unsigned char c2;
2046 + re_string_skip_bytes (input, 1);
2047 + c2 = re_string_peek_byte (input, 0);
2048 + token->opr.c = c2;
2049 + token->type = CHARACTER;
2050 + return 1;
2051 + }
2052 + if (c == '[') /* '[' is a special char in a bracket exps. */
2053 + {
2054 + unsigned char c2;
2055 + int token_len;
2056 + if (re_string_cur_idx (input) + 1 < re_string_length (input))
2057 + c2 = re_string_peek_byte (input, 1);
2058 + else
2059 + c2 = 0;
2060 + token->opr.c = c2;
2061 + token_len = 2;
2062 + switch (c2)
2063 + {
2064 + case '.':
2065 + token->type = OP_OPEN_COLL_ELEM;
2066 + break;
2067 + case '=':
2068 + token->type = OP_OPEN_EQUIV_CLASS;
2069 + break;
2070 + case ':':
2071 + if (syntax & RE_CHAR_CLASSES)
2072 + {
2073 + token->type = OP_OPEN_CHAR_CLASS;
2074 + break;
2075 + }
2076 + /* else fall through. */
2077 + default:
2078 + token->type = CHARACTER;
2079 + token->opr.c = c;
2080 + token_len = 1;
2081 + break;
2082 + }
2083 + return token_len;
2084 + }
2085 + switch (c)
2086 + {
2087 + case '-':
2088 + token->type = OP_CHARSET_RANGE;
2089 + break;
2090 + case ']':
2091 + token->type = OP_CLOSE_BRACKET;
2092 + break;
2093 + case '^':
2094 + token->type = OP_NON_MATCH_LIST;
2095 + break;
2096 + default:
2097 + token->type = CHARACTER;
2098 + }
2099 + return 1;
2100 +}
2101 +
2102 +/* Functions for parser. */
2103 +
2104 +/* Entry point of the parser.
2105 + Parse the regular expression REGEXP and return the structure tree.
2106 + If an error is occured, ERR is set by error code, and return NULL.
2107 + This function build the following tree, from regular expression <reg_exp>:
2108 + CAT
2109 + / \
2110 + / \
2111 + <reg_exp> EOR
2112 +
2113 + CAT means concatenation.
2114 + EOR means end of regular expression. */
2115 +
2116 +static bin_tree_t *
2117 +parse (re_string_t *regexp, regex_t *preg, reg_syntax_t syntax,
2118 + reg_errcode_t *err)
2119 +{
2120 + re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
2121 + bin_tree_t *tree, *eor, *root;
2122 + re_token_t current_token;
2123 + dfa->syntax = syntax;
2124 + fetch_token (&current_token, regexp, syntax | RE_CARET_ANCHORS_HERE);
2125 + tree = parse_reg_exp (regexp, preg, &current_token, syntax, 0, err);
2126 + if (BE (*err != REG_NOERROR && tree == NULL, 0))
2127 + return NULL;
2128 + eor = create_tree (dfa, NULL, NULL, END_OF_RE);
2129 + if (tree != NULL)
2130 + root = create_tree (dfa, tree, eor, CONCAT);
2131 + else
2132 + root = eor;
2133 + if (BE (eor == NULL || root == NULL, 0))
2134 + {
2135 + *err = REG_ESPACE;
2136 + return NULL;
2137 + }
2138 + return root;
2139 +}
2140 +
2141 +/* This function build the following tree, from regular expression
2142 + <branch1>|<branch2>:
2143 + ALT
2144 + / \
2145 + / \
2146 + <branch1> <branch2>
2147 +
2148 + ALT means alternative, which represents the operator `|'. */
2149 +
2150 +static bin_tree_t *
2151 +parse_reg_exp (re_string_t *regexp, regex_t *preg, re_token_t *token,
2152 + reg_syntax_t syntax, int nest, reg_errcode_t *err)
2153 +{
2154 + re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
2155 + bin_tree_t *tree, *branch = NULL;
2156 + tree = parse_branch (regexp, preg, token, syntax, nest, err);
2157 + if (BE (*err != REG_NOERROR && tree == NULL, 0))
2158 + return NULL;
2159 +
2160 + while (token->type == OP_ALT)
2161 + {
2162 + fetch_token (token, regexp, syntax | RE_CARET_ANCHORS_HERE);
2163 + if (token->type != OP_ALT && token->type != END_OF_RE
2164 + && (nest == 0 || token->type != OP_CLOSE_SUBEXP))
2165 + {
2166 + branch = parse_branch (regexp, preg, token, syntax, nest, err);
2167 + if (BE (*err != REG_NOERROR && branch == NULL, 0))
2168 + return NULL;
2169 + }
2170 + else
2171 + branch = NULL;
2172 + tree = create_tree (dfa, tree, branch, OP_ALT);
2173 + if (BE (tree == NULL, 0))
2174 + {
2175 + *err = REG_ESPACE;
2176 + return NULL;
2177 + }
2178 + }
2179 + return tree;
2180 +}
2181 +
2182 +/* This function build the following tree, from regular expression
2183 + <exp1><exp2>:
2184 + CAT
2185 + / \
2186 + / \
2187 + <exp1> <exp2>
2188 +
2189 + CAT means concatenation. */
2190 +
2191 +static bin_tree_t *
2192 +parse_branch (re_string_t *regexp, regex_t *preg, re_token_t *token,
2193 + reg_syntax_t syntax, int nest, reg_errcode_t *err)
2194 +{
2195 + bin_tree_t *tree, *exp;
2196 + re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
2197 + tree = parse_expression (regexp, preg, token, syntax, nest, err);
2198 + if (BE (*err != REG_NOERROR && tree == NULL, 0))
2199 + return NULL;
2200 +
2201 + while (token->type != OP_ALT && token->type != END_OF_RE
2202 + && (nest == 0 || token->type != OP_CLOSE_SUBEXP))
2203 + {
2204 + exp = parse_expression (regexp, preg, token, syntax, nest, err);
2205 + if (BE (*err != REG_NOERROR && exp == NULL, 0))
2206 + {
2207 + return NULL;
2208 + }
2209 + if (tree != NULL && exp != NULL)
2210 + {
2211 + tree = create_tree (dfa, tree, exp, CONCAT);
2212 + if (tree == NULL)
2213 + {
2214 + *err = REG_ESPACE;
2215 + return NULL;
2216 + }
2217 + }
2218 + else if (tree == NULL)
2219 + tree = exp;
2220 + /* Otherwise exp == NULL, we don't need to create new tree. */
2221 + }
2222 + return tree;
2223 +}
2224 +
2225 +/* This function build the following tree, from regular expression a*:
2226 + *
2227 + |
2228 + a
2229 +*/
2230 +
2231 +static bin_tree_t *
2232 +parse_expression (re_string_t *regexp, regex_t *preg, re_token_t *token,
2233 + reg_syntax_t syntax, int nest, reg_errcode_t *err)
2234 +{
2235 + re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
2236 + bin_tree_t *tree;
2237 + switch (token->type)
2238 + {
2239 + case CHARACTER:
2240 + tree = create_token_tree (dfa, NULL, NULL, token);
2241 + if (BE (tree == NULL, 0))
2242 + {
2243 + *err = REG_ESPACE;
2244 + return NULL;
2245 + }
2246 +#ifdef RE_ENABLE_I18N
2247 + if (dfa->mb_cur_max > 1)
2248 + {
2249 + while (!re_string_eoi (regexp)
2250 + && !re_string_first_byte (regexp, re_string_cur_idx (regexp)))
2251 + {
2252 + bin_tree_t *mbc_remain;
2253 + fetch_token (token, regexp, syntax);
2254 + mbc_remain = create_token_tree (dfa, NULL, NULL, token);
2255 + tree = create_tree (dfa, tree, mbc_remain, CONCAT);
2256 + if (BE (mbc_remain == NULL || tree == NULL, 0))
2257 + {
2258 + *err = REG_ESPACE;
2259 + return NULL;
2260 + }
2261 + }
2262 + }
2263 +#endif
2264 + break;
2265 + case OP_OPEN_SUBEXP:
2266 + tree = parse_sub_exp (regexp, preg, token, syntax, nest + 1, err);
2267 + if (BE (*err != REG_NOERROR && tree == NULL, 0))
2268 + return NULL;
2269 + break;
2270 + case OP_OPEN_BRACKET:
2271 + tree = parse_bracket_exp (regexp, dfa, token, syntax, err);
2272 + if (BE (*err != REG_NOERROR && tree == NULL, 0))
2273 + return NULL;
2274 + break;
2275 + case OP_BACK_REF:
2276 + if (!BE (dfa->completed_bkref_map & (1 << token->opr.idx), 1))
2277 + {
2278 + *err = REG_ESUBREG;
2279 + return NULL;
2280 + }
2281 + dfa->used_bkref_map |= 1 << token->opr.idx;
2282 + tree = create_token_tree (dfa, NULL, NULL, token);
2283 + if (BE (tree == NULL, 0))
2284 + {
2285 + *err = REG_ESPACE;
2286 + return NULL;
2287 + }
2288 + ++dfa->nbackref;
2289 + dfa->has_mb_node = 1;
2290 + break;
2291 + case OP_OPEN_DUP_NUM:
2292 + if (syntax & RE_CONTEXT_INVALID_DUP)
2293 + {
2294 + *err = REG_BADRPT;
2295 + return NULL;
2296 + }
2297 + /* FALLTHROUGH */
2298 + case OP_DUP_ASTERISK:
2299 + case OP_DUP_PLUS:
2300 + case OP_DUP_QUESTION:
2301 + if (syntax & RE_CONTEXT_INVALID_OPS)
2302 + {
2303 + *err = REG_BADRPT;
2304 + return NULL;
2305 + }
2306 + else if (syntax & RE_CONTEXT_INDEP_OPS)
2307 + {
2308 + fetch_token (token, regexp, syntax);
2309 + return parse_expression (regexp, preg, token, syntax, nest, err);
2310 + }
2311 + /* else fall through */
2312 + case OP_CLOSE_SUBEXP:
2313 + if ((token->type == OP_CLOSE_SUBEXP) &&
2314 + !(syntax & RE_UNMATCHED_RIGHT_PAREN_ORD))
2315 + {
2316 + *err = REG_ERPAREN;
2317 + return NULL;
2318 + }
2319 + /* else fall through */
2320 + case OP_CLOSE_DUP_NUM:
2321 + /* We treat it as a normal character. */
2322 +
2323 + /* Then we can these characters as normal characters. */
2324 + token->type = CHARACTER;
2325 + /* mb_partial and word_char bits should be initialized already
2326 + by peek_token. */
2327 + tree = create_token_tree (dfa, NULL, NULL, token);
2328 + if (BE (tree == NULL, 0))
2329 + {
2330 + *err = REG_ESPACE;
2331 + return NULL;
2332 + }
2333 + break;
2334 + case ANCHOR:
2335 + if ((token->opr.ctx_type
2336 + & (WORD_DELIM | NOT_WORD_DELIM | WORD_FIRST | WORD_LAST))
2337 + && dfa->word_ops_used == 0)
2338 + init_word_char (dfa);
2339 + if (token->opr.ctx_type == WORD_DELIM
2340 + || token->opr.ctx_type == NOT_WORD_DELIM)
2341 + {
2342 + bin_tree_t *tree_first, *tree_last;
2343 + if (token->opr.ctx_type == WORD_DELIM)
2344 + {
2345 + token->opr.ctx_type = WORD_FIRST;
2346 + tree_first = create_token_tree (dfa, NULL, NULL, token);
2347 + token->opr.ctx_type = WORD_LAST;
2348 + }
2349 + else
2350 + {
2351 + token->opr.ctx_type = INSIDE_WORD;
2352 + tree_first = create_token_tree (dfa, NULL, NULL, token);
2353 + token->opr.ctx_type = INSIDE_NOTWORD;
2354 + }
2355 + tree_last = create_token_tree (dfa, NULL, NULL, token);
2356 + tree = create_tree (dfa, tree_first, tree_last, OP_ALT);
2357 + if (BE (tree_first == NULL || tree_last == NULL || tree == NULL, 0))
2358 + {
2359 + *err = REG_ESPACE;
2360 + return NULL;
2361 + }
2362 + }
2363 + else
2364 + {
2365 + tree = create_token_tree (dfa, NULL, NULL, token);
2366 + if (BE (tree == NULL, 0))
2367 + {
2368 + *err = REG_ESPACE;
2369 + return NULL;
2370 + }
2371 + }
2372 + /* We must return here, since ANCHORs can't be followed
2373 + by repetition operators.
2374 + eg. RE"^*" is invalid or "<ANCHOR(^)><CHAR(*)>",
2375 + it must not be "<ANCHOR(^)><REPEAT(*)>". */
2376 + fetch_token (token, regexp, syntax);
2377 + return tree;
2378 + case OP_PERIOD:
2379 + tree = create_token_tree (dfa, NULL, NULL, token);
2380 + if (BE (tree == NULL, 0))
2381 + {
2382 + *err = REG_ESPACE;
2383 + return NULL;
2384 + }
2385 + if (dfa->mb_cur_max > 1)
2386 + dfa->has_mb_node = 1;
2387 + break;
2388 + case OP_WORD:
2389 + case OP_NOTWORD:
2390 + tree = build_charclass_op (dfa, regexp->trans,
2391 + "alnum",
2392 + "_",
2393 + token->type == OP_NOTWORD, err);
2394 + if (BE (*err != REG_NOERROR && tree == NULL, 0))
2395 + return NULL;
2396 + break;
2397 + case OP_SPACE:
2398 + case OP_NOTSPACE:
2399 + tree = build_charclass_op (dfa, regexp->trans,
2400 + "space",
2401 + "",
2402 + token->type == OP_NOTSPACE, err);
2403 + if (BE (*err != REG_NOERROR && tree == NULL, 0))
2404 + return NULL;
2405 + break;
2406 + case OP_ALT:
2407 + case END_OF_RE:
2408 + return NULL;
2409 + case BACK_SLASH:
2410 + *err = REG_EESCAPE;
2411 + return NULL;
2412 + default:
2413 + /* Must not happen? */
2414 +#ifdef DEBUG
2415 + assert (0);
2416 +#endif
2417 + return NULL;
2418 + }
2419 + fetch_token (token, regexp, syntax);
2420 +
2421 + while (token->type == OP_DUP_ASTERISK || token->type == OP_DUP_PLUS
2422 + || token->type == OP_DUP_QUESTION || token->type == OP_OPEN_DUP_NUM)
2423 + {
2424 + tree = parse_dup_op (tree, regexp, dfa, token, syntax, err);
2425 + if (BE (*err != REG_NOERROR && tree == NULL, 0))
2426 + return NULL;
2427 + /* In BRE consecutive duplications are not allowed. */
2428 + if ((syntax & RE_CONTEXT_INVALID_DUP)
2429 + && (token->type == OP_DUP_ASTERISK
2430 + || token->type == OP_OPEN_DUP_NUM))
2431 + {
2432 + *err = REG_BADRPT;
2433 + return NULL;
2434 + }
2435 + }
2436 +
2437 + return tree;
2438 +}
2439 +
2440 +/* This function build the following tree, from regular expression
2441 + (<reg_exp>):
2442 + SUBEXP
2443 + |
2444 + <reg_exp>
2445 +*/
2446 +
2447 +static bin_tree_t *
2448 +parse_sub_exp (re_string_t *regexp, regex_t *preg, re_token_t *token,
2449 + reg_syntax_t syntax, int nest, reg_errcode_t *err)
2450 +{
2451 + re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
2452 + bin_tree_t *tree;
2453 + size_t cur_nsub;
2454 + cur_nsub = preg->re_nsub++;
2455 +
2456 + fetch_token (token, regexp, syntax | RE_CARET_ANCHORS_HERE);
2457 +
2458 + /* The subexpression may be a null string. */
2459 + if (token->type == OP_CLOSE_SUBEXP)
2460 + tree = NULL;
2461 + else
2462 + {
2463 + tree = parse_reg_exp (regexp, preg, token, syntax, nest, err);
2464 + if (BE (*err == REG_NOERROR && token->type != OP_CLOSE_SUBEXP, 0))
2465 + *err = REG_EPAREN;
2466 + if (BE (*err != REG_NOERROR, 0))
2467 + return NULL;
2468 + }
2469 +
2470 + if (cur_nsub <= '9' - '1')
2471 + dfa->completed_bkref_map |= 1 << cur_nsub;
2472 +
2473 + tree = create_tree (dfa, tree, NULL, SUBEXP);
2474 + if (BE (tree == NULL, 0))
2475 + {
2476 + *err = REG_ESPACE;
2477 + return NULL;
2478 + }
2479 + tree->token.opr.idx = cur_nsub;
2480 + return tree;
2481 +}
2482 +
2483 +/* This function parse repetition operators like "*", "+", "{1,3}" etc. */
2484 +
2485 +static bin_tree_t *
2486 +parse_dup_op (bin_tree_t *elem, re_string_t *regexp, re_dfa_t *dfa,
2487 + re_token_t *token, reg_syntax_t syntax, reg_errcode_t *err)
2488 +{
2489 + bin_tree_t *tree = NULL, *old_tree = NULL;
2490 + int i, start, end, start_idx = re_string_cur_idx (regexp);
2491 +#ifndef RE_TOKEN_INIT_BUG
2492 + re_token_t start_token = *token;
2493 +#else
2494 + re_token_t start_token;
2495 +
2496 + memcpy ((void *) &start_token, (void *) token, sizeof start_token);
2497 +#endif
2498 +
2499 + if (token->type == OP_OPEN_DUP_NUM)
2500 + {
2501 + end = 0;
2502 + start = fetch_number (regexp, token, syntax);
2503 + if (start == -1)
2504 + {
2505 + if (token->type == CHARACTER && token->opr.c == ',')
2506 + start = 0; /* We treat "{,m}" as "{0,m}". */
2507 + else
2508 + {
2509 + *err = REG_BADBR; /* <re>{} is invalid. */
2510 + return NULL;
2511 + }
2512 + }
2513 + if (BE (start != -2, 1))
2514 + {
2515 + /* We treat "{n}" as "{n,n}". */
2516 + end = ((token->type == OP_CLOSE_DUP_NUM) ? start
2517 + : ((token->type == CHARACTER && token->opr.c == ',')
2518 + ? fetch_number (regexp, token, syntax) : -2));
2519 + }
2520 + if (BE (start == -2 || end == -2, 0))
2521 + {
2522 + /* Invalid sequence. */
2523 + if (BE (!(syntax & RE_INVALID_INTERVAL_ORD), 0))
2524 + {
2525 + if (token->type == END_OF_RE)
2526 + *err = REG_EBRACE;
2527 + else
2528 + *err = REG_BADBR;
2529 +
2530 + return NULL;
2531 + }
2532 +
2533 + /* If the syntax bit is set, rollback. */
2534 + re_string_set_index (regexp, start_idx);
2535 + *token = start_token;
2536 + token->type = CHARACTER;
2537 + /* mb_partial and word_char bits should be already initialized by
2538 + peek_token. */
2539 + return elem;
2540 + }
2541 +
2542 + if (BE ((end != -1 && start > end) || token->type != OP_CLOSE_DUP_NUM, 0))
2543 + {
2544 + /* First number greater than second. */
2545 + *err = REG_BADBR;
2546 + return NULL;
2547 + }
2548 + }
2549 + else
2550 + {
2551 + start = (token->type == OP_DUP_PLUS) ? 1 : 0;
2552 + end = (token->type == OP_DUP_QUESTION) ? 1 : -1;
2553 + }
2554 +
2555 + fetch_token (token, regexp, syntax);
2556 +
2557 + if (BE (elem == NULL, 0))
2558 + return NULL;
2559 + if (BE (start == 0 && end == 0, 0))
2560 + {
2561 + postorder (elem, free_tree, NULL);
2562 + return NULL;
2563 + }
2564 +
2565 + /* Extract "<re>{n,m}" to "<re><re>...<re><re>{0,<m-n>}". */
2566 + if (BE (start > 0, 0))
2567 + {
2568 + tree = elem;
2569 + for (i = 2; i <= start; ++i)
2570 + {
2571 + elem = duplicate_tree (elem, dfa);
2572 + tree = create_tree (dfa, tree, elem, CONCAT);
2573 + if (BE (elem == NULL || tree == NULL, 0))
2574 + goto parse_dup_op_espace;
2575 + }
2576 +
2577 + if (start == end)
2578 + return tree;
2579 +
2580 + /* Duplicate ELEM before it is marked optional. */
2581 + elem = duplicate_tree (elem, dfa);
2582 + old_tree = tree;
2583 + }
2584 + else
2585 + old_tree = NULL;
2586 +
2587 + if (elem->token.type == SUBEXP)
2588 + postorder (elem, mark_opt_subexp, (void *) (long) elem->token.opr.idx);
2589 +
2590 + tree = create_tree (dfa, elem, NULL, (end == -1 ? OP_DUP_ASTERISK : OP_ALT));
2591 + if (BE (tree == NULL, 0))
2592 + goto parse_dup_op_espace;
2593 +
2594 + /* This loop is actually executed only when end != -1,
2595 + to rewrite <re>{0,n} as (<re>(<re>...<re>?)?)?... We have
2596 + already created the start+1-th copy. */
2597 + for (i = start + 2; i <= end; ++i)
2598 + {
2599 + elem = duplicate_tree (elem, dfa);
2600 + tree = create_tree (dfa, tree, elem, CONCAT);
2601 + if (BE (elem == NULL || tree == NULL, 0))
2602 + goto parse_dup_op_espace;
2603 +
2604 + tree = create_tree (dfa, tree, NULL, OP_ALT);
2605 + if (BE (tree == NULL, 0))
2606 + goto parse_dup_op_espace;
2607 + }
2608 +
2609 + if (old_tree)
2610 + tree = create_tree (dfa, old_tree, tree, CONCAT);
2611 +
2612 + return tree;
2613 +
2614 + parse_dup_op_espace:
2615 + *err = REG_ESPACE;
2616 + return NULL;
2617 +}
2618 +
2619 +/* Size of the names for collating symbol/equivalence_class/character_class.
2620 + I'm not sure, but maybe enough. */
2621 +#define BRACKET_NAME_BUF_SIZE 32
2622 +
2623 +#ifndef _LIBC
2624 + /* Local function for parse_bracket_exp only used in case of NOT _LIBC.
2625 + Build the range expression which starts from START_ELEM, and ends
2626 + at END_ELEM. The result are written to MBCSET and SBCSET.
2627 + RANGE_ALLOC is the allocated size of mbcset->range_starts, and
2628 + mbcset->range_ends, is a pointer argument sinse we may
2629 + update it. */
2630 +
2631 +static reg_errcode_t
2632 +internal_function
2633 +# ifdef RE_ENABLE_I18N
2634 +build_range_exp (bitset_t sbcset, re_charset_t *mbcset, int *range_alloc,
2635 + bracket_elem_t *start_elem, bracket_elem_t *end_elem)
2636 +# else /* not RE_ENABLE_I18N */
2637 +build_range_exp (bitset_t sbcset, bracket_elem_t *start_elem,
2638 + bracket_elem_t *end_elem)
2639 +# endif /* not RE_ENABLE_I18N */
2640 +{
2641 + unsigned int start_ch, end_ch;
2642 + /* Equivalence Classes and Character Classes can't be a range start/end. */
2643 + if (BE (start_elem->type == EQUIV_CLASS || start_elem->type == CHAR_CLASS
2644 + || end_elem->type == EQUIV_CLASS || end_elem->type == CHAR_CLASS,
2645 + 0))
2646 + return REG_ERANGE;
2647 +
2648 + /* We can handle no multi character collating elements without libc
2649 + support. */
2650 + if (BE ((start_elem->type == COLL_SYM
2651 + && strlen ((char *) start_elem->opr.name) > 1)
2652 + || (end_elem->type == COLL_SYM
2653 + && strlen ((char *) end_elem->opr.name) > 1), 0))
2654 + return REG_ECOLLATE;
2655 +
2656 +# ifdef RE_ENABLE_I18N
2657 + {
2658 + wchar_t wc;
2659 + wint_t start_wc;
2660 + wint_t end_wc;
2661 + wchar_t cmp_buf[6] = {L'\0', L'\0', L'\0', L'\0', L'\0', L'\0'};
2662 +
2663 + start_ch = ((start_elem->type == SB_CHAR) ? start_elem->opr.ch
2664 + : ((start_elem->type == COLL_SYM) ? start_elem->opr.name[0]
2665 + : 0));
2666 + end_ch = ((end_elem->type == SB_CHAR) ? end_elem->opr.ch
2667 + : ((end_elem->type == COLL_SYM) ? end_elem->opr.name[0]
2668 + : 0));
2669 +#ifdef GAWK
2670 + /*
2671 + * Fedora Core 2, maybe others, have broken `btowc' that returns -1
2672 + * for any value > 127. Sigh. Note that `start_ch' and `end_ch' are
2673 + * unsigned, so we don't have sign extension problems.
2674 + */
2675 + start_wc = ((start_elem->type == SB_CHAR || start_elem->type == COLL_SYM)
2676 + ? start_ch : start_elem->opr.wch);
2677 + end_wc = ((end_elem->type == SB_CHAR || end_elem->type == COLL_SYM)
2678 + ? end_ch : end_elem->opr.wch);
2679 +#else
2680 + start_wc = ((start_elem->type == SB_CHAR || start_elem->type == COLL_SYM)
2681 + ? __btowc (start_ch) : start_elem->opr.wch);
2682 + end_wc = ((end_elem->type == SB_CHAR || end_elem->type == COLL_SYM)
2683 + ? __btowc (end_ch) : end_elem->opr.wch);
2684 +#endif
2685 + if (start_wc == WEOF || end_wc == WEOF)
2686 + return REG_ECOLLATE;
2687 + cmp_buf[0] = start_wc;
2688 + cmp_buf[4] = end_wc;
2689 + if (wcscoll (cmp_buf, cmp_buf + 4) > 0)
2690 + return REG_ERANGE;
2691 +
2692 + /* Got valid collation sequence values, add them as a new entry.
2693 + However, for !_LIBC we have no collation elements: if the
2694 + character set is single byte, the single byte character set
2695 + that we build below suffices. parse_bracket_exp passes
2696 + no MBCSET if dfa->mb_cur_max == 1. */
2697 + if (mbcset)
2698 + {
2699 + /* Check the space of the arrays. */
2700 + if (BE (*range_alloc == mbcset->nranges, 0))
2701 + {
2702 + /* There is not enough space, need realloc. */
2703 + wchar_t *new_array_start, *new_array_end;
2704 + int new_nranges;
2705 +
2706 + /* +1 in case of mbcset->nranges is 0. */
2707 + new_nranges = 2 * mbcset->nranges + 1;
2708 + /* Use realloc since mbcset->range_starts and mbcset->range_ends
2709 + are NULL if *range_alloc == 0. */
2710 + new_array_start = re_realloc (mbcset->range_starts, wchar_t,
2711 + new_nranges);
2712 + new_array_end = re_realloc (mbcset->range_ends, wchar_t,
2713 + new_nranges);
2714 +
2715 + if (BE (new_array_start == NULL || new_array_end == NULL, 0))
2716 + return REG_ESPACE;
2717 +
2718 + mbcset->range_starts = new_array_start;
2719 + mbcset->range_ends = new_array_end;
2720 + *range_alloc = new_nranges;
2721 + }
2722 +
2723 + mbcset->range_starts[mbcset->nranges] = start_wc;
2724 + mbcset->range_ends[mbcset->nranges++] = end_wc;
2725 + }
2726 +
2727 + /* Build the table for single byte characters. */
2728 + for (wc = 0; wc < SBC_MAX; ++wc)
2729 + {
2730 + cmp_buf[2] = wc;
2731 + if (wcscoll (cmp_buf, cmp_buf + 2) <= 0
2732 + && wcscoll (cmp_buf + 2, cmp_buf + 4) <= 0)
2733 + bitset_set (sbcset, wc);
2734 + }
2735 + }
2736 +# else /* not RE_ENABLE_I18N */
2737 + {
2738 + unsigned int ch;
2739 + start_ch = ((start_elem->type == SB_CHAR ) ? start_elem->opr.ch
2740 + : ((start_elem->type == COLL_SYM) ? start_elem->opr.name[0]
2741 + : 0));
2742 + end_ch = ((end_elem->type == SB_CHAR ) ? end_elem->opr.ch
2743 + : ((end_elem->type == COLL_SYM) ? end_elem->opr.name[0]
2744 + : 0));
2745 + if (start_ch > end_ch)
2746 + return REG_ERANGE;
2747 + /* Build the table for single byte characters. */
2748 + for (ch = 0; ch < SBC_MAX; ++ch)
2749 + if (start_ch <= ch && ch <= end_ch)
2750 + bitset_set (sbcset, ch);
2751 + }
2752 +# endif /* not RE_ENABLE_I18N */
2753 + return REG_NOERROR;
2754 +}
2755 +#endif /* not _LIBC */
2756 +
2757 +#ifndef _LIBC
2758 +/* Helper function for parse_bracket_exp only used in case of NOT _LIBC..
2759 + Build the collating element which is represented by NAME.
2760 + The result are written to MBCSET and SBCSET.
2761 + COLL_SYM_ALLOC is the allocated size of mbcset->coll_sym, is a
2762 + pointer argument since we may update it. */
2763 +
2764 +static reg_errcode_t
2765 +internal_function
2766 +# ifdef RE_ENABLE_I18N
2767 +build_collating_symbol (bitset_t sbcset, re_charset_t *mbcset,
2768 + int *coll_sym_alloc, const unsigned char *name)
2769 +# else /* not RE_ENABLE_I18N */
2770 +build_collating_symbol (bitset_t sbcset, const unsigned char *name)
2771 +# endif /* not RE_ENABLE_I18N */
2772 +{
2773 + size_t name_len = strlen ((const char *) name);
2774 + if (BE (name_len != 1, 0))
2775 + return REG_ECOLLATE;
2776 + else
2777 + {
2778 + bitset_set (sbcset, name[0]);
2779 + return REG_NOERROR;
2780 + }
2781 +}
2782 +#endif /* not _LIBC */
2783 +
2784 +/* This function parse bracket expression like "[abc]", "[a-c]",
2785 + "[[.a-a.]]" etc. */
2786 +
2787 +static bin_tree_t *
2788 +parse_bracket_exp (re_string_t *regexp, re_dfa_t *dfa, re_token_t *token,
2789 + reg_syntax_t syntax, reg_errcode_t *err)
2790 +{
2791 +#ifdef _LIBC
2792 + const unsigned char *collseqmb;
2793 + const char *collseqwc;
2794 + uint32_t nrules;
2795 + int32_t table_size;
2796 + const int32_t *symb_table;
2797 + const unsigned char *extra;
2798 +
2799 + /* Local function for parse_bracket_exp used in _LIBC environement.
2800 + Seek the collating symbol entry correspondings to NAME.
2801 + Return the index of the symbol in the SYMB_TABLE. */
2802 +
2803 + auto inline int32_t
2804 + __attribute ((always_inline))
2805 + seek_collating_symbol_entry (name, name_len)
2806 + const unsigned char *name;
2807 + size_t name_len;
2808 + {
2809 + int32_t hash = elem_hash ((const char *) name, name_len);
2810 + int32_t elem = hash % table_size;
2811 + if (symb_table[2 * elem] != 0)
2812 + {
2813 + int32_t second = hash % (table_size - 2) + 1;
2814 +
2815 + do
2816 + {
2817 + /* First compare the hashing value. */
2818 + if (symb_table[2 * elem] == hash
2819 + /* Compare the length of the name. */
2820 + && name_len == extra[symb_table[2 * elem + 1]]
2821 + /* Compare the name. */
2822 + && memcmp (name, &extra[symb_table[2 * elem + 1] + 1],
2823 + name_len) == 0)
2824 + {
2825 + /* Yep, this is the entry. */
2826 + break;
2827 + }
2828 +
2829 + /* Next entry. */
2830 + elem += second;
2831 + }
2832 + while (symb_table[2 * elem] != 0);
2833 + }
2834 + return elem;
2835 + }
2836 +
2837 + /* Local function for parse_bracket_exp used in _LIBC environment.
2838 + Look up the collation sequence value of BR_ELEM.
2839 + Return the value if succeeded, UINT_MAX otherwise. */
2840 +
2841 + auto inline unsigned int
2842 + __attribute ((always_inline))
2843 + lookup_collation_sequence_value (br_elem)
2844 + bracket_elem_t *br_elem;
2845 + {
2846 + if (br_elem->type == SB_CHAR)
2847 + {
2848 + /*
2849 + if (MB_CUR_MAX == 1)
2850 + */
2851 + if (nrules == 0)
2852 + return collseqmb[br_elem->opr.ch];
2853 + else
2854 + {
2855 + wint_t wc = __btowc (br_elem->opr.ch);
2856 + return __collseq_table_lookup (collseqwc, wc);
2857 + }
2858 + }
2859 + else if (br_elem->type == MB_CHAR)
2860 + {
2861 + if (nrules != 0)
2862 + return __collseq_table_lookup (collseqwc, br_elem->opr.wch);
2863 + }
2864 + else if (br_elem->type == COLL_SYM)
2865 + {
2866 + size_t sym_name_len = strlen ((char *) br_elem->opr.name);
2867 + if (nrules != 0)
2868 + {
2869 + int32_t elem, idx;
2870 + elem = seek_collating_symbol_entry (br_elem->opr.name,
2871 + sym_name_len);
2872 + if (symb_table[2 * elem] != 0)
2873 + {
2874 + /* We found the entry. */
2875 + idx = symb_table[2 * elem + 1];
2876 + /* Skip the name of collating element name. */
2877 + idx += 1 + extra[idx];
2878 + /* Skip the byte sequence of the collating element. */
2879 + idx += 1 + extra[idx];
2880 + /* Adjust for the alignment. */
2881 + idx = (idx + 3) & ~3;
2882 + /* Skip the multibyte collation sequence value. */
2883 + idx += sizeof (unsigned int);
2884 + /* Skip the wide char sequence of the collating element. */
2885 + idx += sizeof (unsigned int) *
2886 + (1 + *(unsigned int *) (extra + idx));
2887 + /* Return the collation sequence value. */
2888 + return *(unsigned int *) (extra + idx);
2889 + }
2890 + else if (symb_table[2 * elem] == 0 && sym_name_len == 1)
2891 + {
2892 + /* No valid character. Match it as a single byte
2893 + character. */
2894 + return collseqmb[br_elem->opr.name[0]];
2895 + }
2896 + }
2897 + else if (sym_name_len == 1)
2898 + return collseqmb[br_elem->opr.name[0]];
2899 + }
2900 + return UINT_MAX;
2901 + }
2902 +
2903 + /* Local function for parse_bracket_exp used in _LIBC environement.
2904 + Build the range expression which starts from START_ELEM, and ends
2905 + at END_ELEM. The result are written to MBCSET and SBCSET.
2906 + RANGE_ALLOC is the allocated size of mbcset->range_starts, and
2907 + mbcset->range_ends, is a pointer argument sinse we may
2908 + update it. */
2909 +
2910 + auto inline reg_errcode_t
2911 + __attribute ((always_inline))
2912 + build_range_exp (sbcset, mbcset, range_alloc, start_elem, end_elem)
2913 + re_charset_t *mbcset;
2914 + int *range_alloc;
2915 + bitset_t sbcset;
2916 + bracket_elem_t *start_elem, *end_elem;
2917 + {
2918 + unsigned int ch;
2919 + uint32_t start_collseq;
2920 + uint32_t end_collseq;
2921 +
2922 + /* Equivalence Classes and Character Classes can't be a range
2923 + start/end. */
2924 + if (BE (start_elem->type == EQUIV_CLASS || start_elem->type == CHAR_CLASS
2925 + || end_elem->type == EQUIV_CLASS || end_elem->type == CHAR_CLASS,
2926 + 0))
2927 + return REG_ERANGE;
2928 +
2929 + start_collseq = lookup_collation_sequence_value (start_elem);
2930 + end_collseq = lookup_collation_sequence_value (end_elem);
2931 + /* Check start/end collation sequence values. */
2932 + if (BE (start_collseq == UINT_MAX || end_collseq == UINT_MAX, 0))
2933 + return REG_ECOLLATE;
2934 + if (BE ((syntax & RE_NO_EMPTY_RANGES) && start_collseq > end_collseq, 0))
2935 + return REG_ERANGE;
2936 +
2937 + /* Got valid collation sequence values, add them as a new entry.
2938 + However, if we have no collation elements, and the character set
2939 + is single byte, the single byte character set that we
2940 + build below suffices. */
2941 + if (nrules > 0 || dfa->mb_cur_max > 1)
2942 + {
2943 + /* Check the space of the arrays. */
2944 + if (BE (*range_alloc == mbcset->nranges, 0))
2945 + {
2946 + /* There is not enough space, need realloc. */
2947 + uint32_t *new_array_start;
2948 + uint32_t *new_array_end;
2949 + int new_nranges;
2950 +
2951 + /* +1 in case of mbcset->nranges is 0. */
2952 + new_nranges = 2 * mbcset->nranges + 1;
2953 + new_array_start = re_realloc (mbcset->range_starts, uint32_t,
2954 + new_nranges);
2955 + new_array_end = re_realloc (mbcset->range_ends, uint32_t,
2956 + new_nranges);
2957 +
2958 + if (BE (new_array_start == NULL || new_array_end == NULL, 0))
2959 + return REG_ESPACE;
2960 +
2961 + mbcset->range_starts = new_array_start;
2962 + mbcset->range_ends = new_array_end;
2963 + *range_alloc = new_nranges;
2964 + }
2965 +
2966 + mbcset->range_starts[mbcset->nranges] = start_collseq;
2967 + mbcset->range_ends[mbcset->nranges++] = end_collseq;
2968 + }
2969 +
2970 + /* Build the table for single byte characters. */
2971 + for (ch = 0; ch < SBC_MAX; ch++)
2972 + {
2973 + uint32_t ch_collseq;
2974 + /*
2975 + if (MB_CUR_MAX == 1)
2976 + */
2977 + if (nrules == 0)
2978 + ch_collseq = collseqmb[ch];
2979 + else
2980 + ch_collseq = __collseq_table_lookup (collseqwc, __btowc (ch));
2981 + if (start_collseq <= ch_collseq && ch_collseq <= end_collseq)
2982 + bitset_set (sbcset, ch);
2983 + }
2984 + return REG_NOERROR;
2985 + }
2986 +
2987 + /* Local function for parse_bracket_exp used in _LIBC environement.
2988 + Build the collating element which is represented by NAME.
2989 + The result are written to MBCSET and SBCSET.
2990 + COLL_SYM_ALLOC is the allocated size of mbcset->coll_sym, is a
2991 + pointer argument sinse we may update it. */
2992 +
2993 + auto inline reg_errcode_t
2994 + __attribute ((always_inline))
2995 + build_collating_symbol (sbcset, mbcset, coll_sym_alloc, name)
2996 + re_charset_t *mbcset;
2997 + int *coll_sym_alloc;
2998 + bitset_t sbcset;
2999 + const unsigned char *name;
3000 + {
3001 + int32_t elem, idx;
3002 + size_t name_len = strlen ((const char *) name);
3003 + if (nrules != 0)
3004 + {
3005 + elem = seek_collating_symbol_entry (name, name_len);
3006 + if (symb_table[2 * elem] != 0)
3007 + {
3008 + /* We found the entry. */
3009 + idx = symb_table[2 * elem + 1];
3010 + /* Skip the name of collating element name. */
3011 + idx += 1 + extra[idx];
3012 + }
3013 + else if (symb_table[2 * elem] == 0 && name_len == 1)
3014 + {
3015 + /* No valid character, treat it as a normal
3016 + character. */
3017 + bitset_set (sbcset, name[0]);
3018 + return REG_NOERROR;
3019 + }
3020 + else
3021 + return REG_ECOLLATE;
3022 +
3023 + /* Got valid collation sequence, add it as a new entry. */
3024 + /* Check the space of the arrays. */
3025 + if (BE (*coll_sym_alloc == mbcset->ncoll_syms, 0))
3026 + {
3027 + /* Not enough, realloc it. */
3028 + /* +1 in case of mbcset->ncoll_syms is 0. */
3029 + int new_coll_sym_alloc = 2 * mbcset->ncoll_syms + 1;
3030 + /* Use realloc since mbcset->coll_syms is NULL
3031 + if *alloc == 0. */
3032 + int32_t *new_coll_syms = re_realloc (mbcset->coll_syms, int32_t,
3033 + new_coll_sym_alloc);
3034 + if (BE (new_coll_syms == NULL, 0))
3035 + return REG_ESPACE;
3036 + mbcset->coll_syms = new_coll_syms;
3037 + *coll_sym_alloc = new_coll_sym_alloc;
3038 + }
3039 + mbcset->coll_syms[mbcset->ncoll_syms++] = idx;
3040 + return REG_NOERROR;
3041 + }
3042 + else
3043 + {
3044 + if (BE (name_len != 1, 0))
3045 + return REG_ECOLLATE;
3046 + else
3047 + {
3048 + bitset_set (sbcset, name[0]);
3049 + return REG_NOERROR;
3050 + }
3051 + }
3052 + }
3053 +#endif
3054 +
3055 + re_token_t br_token;
3056 + re_bitset_ptr_t sbcset;
3057 +#ifdef RE_ENABLE_I18N
3058 + re_charset_t *mbcset;
3059 + int coll_sym_alloc = 0, range_alloc = 0, mbchar_alloc = 0;
3060 + int equiv_class_alloc = 0, char_class_alloc = 0;
3061 +#endif /* not RE_ENABLE_I18N */
3062 + int non_match = 0;
3063 + bin_tree_t *work_tree;
3064 + int token_len;
3065 + int first_round = 1;
3066 +#ifdef _LIBC
3067 + collseqmb = (const unsigned char *)
3068 + _NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQMB);
3069 + nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
3070 + if (nrules)
3071 + {
3072 + /*
3073 + if (MB_CUR_MAX > 1)
3074 + */
3075 + collseqwc = _NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQWC);
3076 + table_size = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_SYMB_HASH_SIZEMB);
3077 + symb_table = (const int32_t *) _NL_CURRENT (LC_COLLATE,
3078 + _NL_COLLATE_SYMB_TABLEMB);
3079 + extra = (const unsigned char *) _NL_CURRENT (LC_COLLATE,
3080 + _NL_COLLATE_SYMB_EXTRAMB);
3081 + }
3082 +#endif
3083 + sbcset = (re_bitset_ptr_t) calloc (sizeof (bitset_t), 1);
3084 +#ifdef RE_ENABLE_I18N
3085 + mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1);
3086 +#endif /* RE_ENABLE_I18N */
3087 +#ifdef RE_ENABLE_I18N
3088 + if (BE (sbcset == NULL || mbcset == NULL, 0))
3089 +#else
3090 + if (BE (sbcset == NULL, 0))
3091 +#endif /* RE_ENABLE_I18N */
3092 + {
3093 + *err = REG_ESPACE;
3094 + return NULL;
3095 + }
3096 +
3097 + token_len = peek_token_bracket (token, regexp, syntax);
3098 + if (BE (token->type == END_OF_RE, 0))
3099 + {
3100 + *err = REG_BADPAT;
3101 + goto parse_bracket_exp_free_return;
3102 + }
3103 + if (token->type == OP_NON_MATCH_LIST)
3104 + {
3105 +#ifdef RE_ENABLE_I18N
3106 + mbcset->non_match = 1;
3107 +#endif /* not RE_ENABLE_I18N */
3108 + non_match = 1;
3109 + if (syntax & RE_HAT_LISTS_NOT_NEWLINE)
3110 + bitset_set (sbcset, '\n');
3111 + re_string_skip_bytes (regexp, token_len); /* Skip a token. */
3112 + token_len = peek_token_bracket (token, regexp, syntax);
3113 + if (BE (token->type == END_OF_RE, 0))
3114 + {
3115 + *err = REG_BADPAT;
3116 + goto parse_bracket_exp_free_return;
3117 + }
3118 + }
3119 +
3120 + /* We treat the first ']' as a normal character. */
3121 + if (token->type == OP_CLOSE_BRACKET)
3122 + token->type = CHARACTER;
3123 +
3124 + while (1)
3125 + {
3126 + bracket_elem_t start_elem, end_elem;
3127 + unsigned char start_name_buf[BRACKET_NAME_BUF_SIZE];
3128 + unsigned char end_name_buf[BRACKET_NAME_BUF_SIZE];
3129 + reg_errcode_t ret;
3130 + int token_len2 = 0, is_range_exp = 0;
3131 + re_token_t token2;
3132 +
3133 + start_elem.opr.name = start_name_buf;
3134 + ret = parse_bracket_element (&start_elem, regexp, token, token_len, dfa,
3135 + syntax, first_round);
3136 + if (BE (ret != REG_NOERROR, 0))
3137 + {
3138 + *err = ret;
3139 + goto parse_bracket_exp_free_return;
3140 + }
3141 + first_round = 0;
3142 +
3143 + /* Get information about the next token. We need it in any case. */
3144 + token_len = peek_token_bracket (token, regexp, syntax);
3145 +
3146 + /* Do not check for ranges if we know they are not allowed. */
3147 + if (start_elem.type != CHAR_CLASS && start_elem.type != EQUIV_CLASS)
3148 + {
3149 + if (BE (token->type == END_OF_RE, 0))
3150 + {
3151 + *err = REG_EBRACK;
3152 + goto parse_bracket_exp_free_return;
3153 + }
3154 + if (token->type == OP_CHARSET_RANGE)
3155 + {
3156 + re_string_skip_bytes (regexp, token_len); /* Skip '-'. */
3157 + token_len2 = peek_token_bracket (&token2, regexp, syntax);
3158 + if (BE (token2.type == END_OF_RE, 0))
3159 + {
3160 + *err = REG_EBRACK;
3161 + goto parse_bracket_exp_free_return;
3162 + }
3163 + if (token2.type == OP_CLOSE_BRACKET)
3164 + {
3165 + /* We treat the last '-' as a normal character. */
3166 + re_string_skip_bytes (regexp, -token_len);
3167 + token->type = CHARACTER;
3168 + }
3169 + else
3170 + is_range_exp = 1;
3171 + }
3172 + }
3173 +
3174 + if (is_range_exp == 1)
3175 + {
3176 + end_elem.opr.name = end_name_buf;
3177 + ret = parse_bracket_element (&end_elem, regexp, &token2, token_len2,
3178 + dfa, syntax, 1);
3179 + if (BE (ret != REG_NOERROR, 0))
3180 + {
3181 + *err = ret;
3182 + goto parse_bracket_exp_free_return;
3183 + }
3184 +
3185 + token_len = peek_token_bracket (token, regexp, syntax);
3186 +
3187 +#ifdef _LIBC
3188 + *err = build_range_exp (sbcset, mbcset, &range_alloc,
3189 + &start_elem, &end_elem);
3190 +#else
3191 +# ifdef RE_ENABLE_I18N
3192 + *err = build_range_exp (sbcset,
3193 + dfa->mb_cur_max > 1 ? mbcset : NULL,
3194 + &range_alloc, &start_elem, &end_elem);
3195 +# else
3196 + *err = build_range_exp (sbcset, &start_elem, &end_elem);
3197 +# endif
3198 +#endif /* RE_ENABLE_I18N */
3199 + if (BE (*err != REG_NOERROR, 0))
3200 + goto parse_bracket_exp_free_return;
3201 + }
3202 + else
3203 + {
3204 + switch (start_elem.type)
3205 + {
3206 + case SB_CHAR:
3207 + bitset_set (sbcset, start_elem.opr.ch);
3208 + break;
3209 +#ifdef RE_ENABLE_I18N
3210 + case MB_CHAR:
3211 + /* Check whether the array has enough space. */
3212 + if (BE (mbchar_alloc == mbcset->nmbchars, 0))
3213 + {
3214 + wchar_t *new_mbchars;
3215 + /* Not enough, realloc it. */
3216 + /* +1 in case of mbcset->nmbchars is 0. */
3217 + mbchar_alloc = 2 * mbcset->nmbchars + 1;
3218 + /* Use realloc since array is NULL if *alloc == 0. */
3219 + new_mbchars = re_realloc (mbcset->mbchars, wchar_t,
3220 + mbchar_alloc);
3221 + if (BE (new_mbchars == NULL, 0))
3222 + goto parse_bracket_exp_espace;
3223 + mbcset->mbchars = new_mbchars;
3224 + }
3225 + mbcset->mbchars[mbcset->nmbchars++] = start_elem.opr.wch;
3226 + break;
3227 +#endif /* RE_ENABLE_I18N */
3228 + case EQUIV_CLASS:
3229 + *err = build_equiv_class (sbcset,
3230 +#ifdef RE_ENABLE_I18N
3231 + mbcset, &equiv_class_alloc,
3232 +#endif /* RE_ENABLE_I18N */
3233 + start_elem.opr.name);
3234 + if (BE (*err != REG_NOERROR, 0))
3235 + goto parse_bracket_exp_free_return;
3236 + break;
3237 + case COLL_SYM:
3238 + *err = build_collating_symbol (sbcset,
3239 +#ifdef RE_ENABLE_I18N
3240 + mbcset, &coll_sym_alloc,
3241 +#endif /* RE_ENABLE_I18N */
3242 + start_elem.opr.name);
3243 + if (BE (*err != REG_NOERROR, 0))
3244 + goto parse_bracket_exp_free_return;
3245 + break;
3246 + case CHAR_CLASS:
3247 + *err = build_charclass (regexp->trans, sbcset,
3248 +#ifdef RE_ENABLE_I18N
3249 + mbcset, &char_class_alloc,
3250 +#endif /* RE_ENABLE_I18N */
3251 + (const char *) start_elem.opr.name, syntax);
3252 + if (BE (*err != REG_NOERROR, 0))
3253 + goto parse_bracket_exp_free_return;
3254 + break;
3255 + default:
3256 + assert (0);
3257 + break;
3258 + }
3259 + }
3260 + if (BE (token->type == END_OF_RE, 0))
3261 + {
3262 + *err = REG_EBRACK;
3263 + goto parse_bracket_exp_free_return;
3264 + }
3265 + if (token->type == OP_CLOSE_BRACKET)
3266 + break;
3267 + }
3268 +
3269 + re_string_skip_bytes (regexp, token_len); /* Skip a token. */
3270 +
3271 + /* If it is non-matching list. */
3272 + if (non_match)
3273 + bitset_not (sbcset);
3274 +
3275 +#ifdef RE_ENABLE_I18N
3276 + /* Ensure only single byte characters are set. */
3277 + if (dfa->mb_cur_max > 1)
3278 + bitset_mask (sbcset, dfa->sb_char);
3279 +
3280 + if (mbcset->nmbchars || mbcset->ncoll_syms || mbcset->nequiv_classes
3281 + || mbcset->nranges || (dfa->mb_cur_max > 1 && (mbcset->nchar_classes
3282 + || mbcset->non_match)))
3283 + {
3284 + bin_tree_t *mbc_tree;
3285 + int sbc_idx;
3286 + /* Build a tree for complex bracket. */
3287 + dfa->has_mb_node = 1;
3288 + br_token.type = COMPLEX_BRACKET;
3289 + br_token.opr.mbcset = mbcset;
3290 + mbc_tree = create_token_tree (dfa, NULL, NULL, &br_token);
3291 + if (BE (mbc_tree == NULL, 0))
3292 + goto parse_bracket_exp_espace;
3293 + for (sbc_idx = 0; sbc_idx < BITSET_WORDS; ++sbc_idx)
3294 + if (sbcset[sbc_idx])
3295 + break;
3296 + /* If there are no bits set in sbcset, there is no point
3297 + of having both SIMPLE_BRACKET and COMPLEX_BRACKET. */
3298 + if (sbc_idx < BITSET_WORDS)
3299 + {
3300 + /* Build a tree for simple bracket. */
3301 + br_token.type = SIMPLE_BRACKET;
3302 + br_token.opr.sbcset = sbcset;
3303 + work_tree = create_token_tree (dfa, NULL, NULL, &br_token);
3304 + if (BE (work_tree == NULL, 0))
3305 + goto parse_bracket_exp_espace;
3306 +
3307 + /* Then join them by ALT node. */
3308 + work_tree = create_tree (dfa, work_tree, mbc_tree, OP_ALT);
3309 + if (BE (work_tree == NULL, 0))
3310 + goto parse_bracket_exp_espace;
3311 + }
3312 + else
3313 + {
3314 + re_free (sbcset);
3315 + work_tree = mbc_tree;
3316 + }
3317 + }
3318 + else
3319 +#endif /* not RE_ENABLE_I18N */
3320 + {
3321 +#ifdef RE_ENABLE_I18N
3322 + free_charset (mbcset);
3323 +#endif
3324 + /* Build a tree for simple bracket. */
3325 + br_token.type = SIMPLE_BRACKET;
3326 + br_token.opr.sbcset = sbcset;
3327 + work_tree = create_token_tree (dfa, NULL, NULL, &br_token);
3328 + if (BE (work_tree == NULL, 0))
3329 + goto parse_bracket_exp_espace;
3330 + }
3331 + return work_tree;
3332 +
3333 + parse_bracket_exp_espace:
3334 + *err = REG_ESPACE;
3335 + parse_bracket_exp_free_return:
3336 + re_free (sbcset);
3337 +#ifdef RE_ENABLE_I18N
3338 + free_charset (mbcset);
3339 +#endif /* RE_ENABLE_I18N */
3340 + return NULL;
3341 +}
3342 +
3343 +/* Parse an element in the bracket expression. */
3344 +
3345 +static reg_errcode_t
3346 +parse_bracket_element (bracket_elem_t *elem, re_string_t *regexp,
3347 + re_token_t *token, int token_len, re_dfa_t *dfa,
3348 + reg_syntax_t syntax, int accept_hyphen)
3349 +{
3350 +#ifdef RE_ENABLE_I18N
3351 + int cur_char_size;
3352 + cur_char_size = re_string_char_size_at (regexp, re_string_cur_idx (regexp));
3353 + if (cur_char_size > 1)
3354 + {
3355 + elem->type = MB_CHAR;
3356 + elem->opr.wch = re_string_wchar_at (regexp, re_string_cur_idx (regexp));
3357 + re_string_skip_bytes (regexp, cur_char_size);
3358 + return REG_NOERROR;
3359 + }
3360 +#endif /* RE_ENABLE_I18N */
3361 + re_string_skip_bytes (regexp, token_len); /* Skip a token. */
3362 + if (token->type == OP_OPEN_COLL_ELEM || token->type == OP_OPEN_CHAR_CLASS
3363 + || token->type == OP_OPEN_EQUIV_CLASS)
3364 + return parse_bracket_symbol (elem, regexp, token);
3365 + if (BE (token->type == OP_CHARSET_RANGE, 0) && !accept_hyphen)
3366 + {
3367 + /* A '-' must only appear as anything but a range indicator before
3368 + the closing bracket. Everything else is an error. */
3369 + re_token_t token2;
3370 + (void) peek_token_bracket (&token2, regexp, syntax);
3371 + if (token2.type != OP_CLOSE_BRACKET)
3372 + /* The actual error value is not standardized since this whole
3373 + case is undefined. But ERANGE makes good sense. */
3374 + return REG_ERANGE;
3375 + }
3376 + elem->type = SB_CHAR;
3377 + elem->opr.ch = token->opr.c;
3378 + return REG_NOERROR;
3379 +}
3380 +
3381 +/* Parse a bracket symbol in the bracket expression. Bracket symbols are
3382 + such as [:<character_class>:], [.<collating_element>.], and
3383 + [=<equivalent_class>=]. */
3384 +
3385 +static reg_errcode_t
3386 +parse_bracket_symbol (bracket_elem_t *elem, re_string_t *regexp,
3387 + re_token_t *token)
3388 +{
3389 + unsigned char ch, delim = token->opr.c;
3390 + int i = 0;
3391 + if (re_string_eoi(regexp))
3392 + return REG_EBRACK;
3393 + for (;; ++i)
3394 + {
3395 + if (i >= BRACKET_NAME_BUF_SIZE)
3396 + return REG_EBRACK;
3397 + if (token->type == OP_OPEN_CHAR_CLASS)
3398 + ch = re_string_fetch_byte_case (regexp);
3399 + else
3400 + ch = re_string_fetch_byte (regexp);
3401 + if (re_string_eoi(regexp))
3402 + return REG_EBRACK;
3403 + if (ch == delim && re_string_peek_byte (regexp, 0) == ']')
3404 + break;
3405 + elem->opr.name[i] = ch;
3406 + }
3407 + re_string_skip_bytes (regexp, 1);
3408 + elem->opr.name[i] = '\0';
3409 + switch (token->type)
3410 + {
3411 + case OP_OPEN_COLL_ELEM:
3412 + elem->type = COLL_SYM;
3413 + break;
3414 + case OP_OPEN_EQUIV_CLASS:
3415 + elem->type = EQUIV_CLASS;
3416 + break;
3417 + case OP_OPEN_CHAR_CLASS:
3418 + elem->type = CHAR_CLASS;
3419 + break;
3420 + default:
3421 + break;
3422 + }
3423 + return REG_NOERROR;
3424 +}
3425 +
3426 + /* Helper function for parse_bracket_exp.
3427 + Build the equivalence class which is represented by NAME.
3428 + The result are written to MBCSET and SBCSET.
3429 + EQUIV_CLASS_ALLOC is the allocated size of mbcset->equiv_classes,
3430 + is a pointer argument sinse we may update it. */
3431 +
3432 +static reg_errcode_t
3433 +#ifdef RE_ENABLE_I18N
3434 +build_equiv_class (bitset_t sbcset, re_charset_t *mbcset,
3435 + int *equiv_class_alloc, const unsigned char *name)
3436 +#else /* not RE_ENABLE_I18N */
3437 +build_equiv_class (bitset_t sbcset, const unsigned char *name)
3438 +#endif /* not RE_ENABLE_I18N */
3439 +{
3440 +#ifdef _LIBC
3441 + uint32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
3442 + if (nrules != 0)
3443 + {
3444 + const int32_t *table, *indirect;
3445 + const unsigned char *weights, *extra, *cp;
3446 + unsigned char char_buf[2];
3447 + int32_t idx1, idx2;
3448 + unsigned int ch;
3449 + size_t len;
3450 + /* This #include defines a local function! */
3451 +# include <locale/weight.h>
3452 + /* Calculate the index for equivalence class. */
3453 + cp = name;
3454 + table = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
3455 + weights = (const unsigned char *) _NL_CURRENT (LC_COLLATE,
3456 + _NL_COLLATE_WEIGHTMB);
3457 + extra = (const unsigned char *) _NL_CURRENT (LC_COLLATE,
3458 + _NL_COLLATE_EXTRAMB);
3459 + indirect = (const int32_t *) _NL_CURRENT (LC_COLLATE,
3460 + _NL_COLLATE_INDIRECTMB);
3461 + idx1 = findidx (&cp);
3462 + if (BE (idx1 == 0 || cp < name + strlen ((const char *) name), 0))
3463 + /* This isn't a valid character. */
3464 + return REG_ECOLLATE;
3465 +
3466 + /* Build single byte matcing table for this equivalence class. */
3467 + char_buf[1] = (unsigned char) '\0';
3468 + len = weights[idx1 & 0xffffff];
3469 + for (ch = 0; ch < SBC_MAX; ++ch)
3470 + {
3471 + char_buf[0] = ch;
3472 + cp = char_buf;
3473 + idx2 = findidx (&cp);
3474 +/*
3475 + idx2 = table[ch];
3476 +*/
3477 + if (idx2 == 0)
3478 + /* This isn't a valid character. */
3479 + continue;
3480 + /* Compare only if the length matches and the collation rule
3481 + index is the same. */
3482 + if (len == weights[idx2 & 0xffffff] && (idx1 >> 24) == (idx2 >> 24))
3483 + {
3484 + int cnt = 0;
3485 +
3486 + while (cnt <= len &&
3487 + weights[(idx1 & 0xffffff) + 1 + cnt]
3488 + == weights[(idx2 & 0xffffff) + 1 + cnt])
3489 + ++cnt;
3490 +
3491 + if (cnt > len)
3492 + bitset_set (sbcset, ch);
3493 + }
3494 + }
3495 + /* Check whether the array has enough space. */
3496 + if (BE (*equiv_class_alloc == mbcset->nequiv_classes, 0))
3497 + {
3498 + /* Not enough, realloc it. */
3499 + /* +1 in case of mbcset->nequiv_classes is 0. */
3500 + int new_equiv_class_alloc = 2 * mbcset->nequiv_classes + 1;
3501 + /* Use realloc since the array is NULL if *alloc == 0. */
3502 + int32_t *new_equiv_classes = re_realloc (mbcset->equiv_classes,
3503 + int32_t,
3504 + new_equiv_class_alloc);
3505 + if (BE (new_equiv_classes == NULL, 0))
3506 + return REG_ESPACE;
3507 + mbcset->equiv_classes = new_equiv_classes;
3508 + *equiv_class_alloc = new_equiv_class_alloc;
3509 + }
3510 + mbcset->equiv_classes[mbcset->nequiv_classes++] = idx1;
3511 + }
3512 + else
3513 +#endif /* _LIBC */
3514 + {
3515 + if (BE (strlen ((const char *) name) != 1, 0))
3516 + return REG_ECOLLATE;
3517 + bitset_set (sbcset, *name);
3518 + }
3519 + return REG_NOERROR;
3520 +}
3521 +
3522 + /* Helper function for parse_bracket_exp.
3523 + Build the character class which is represented by NAME.
3524 + The result are written to MBCSET and SBCSET.
3525 + CHAR_CLASS_ALLOC is the allocated size of mbcset->char_classes,
3526 + is a pointer argument sinse we may update it. */
3527 +
3528 +static reg_errcode_t
3529 +#ifdef RE_ENABLE_I18N
3530 +build_charclass (RE_TRANSLATE_TYPE trans, bitset_t sbcset,
3531 + re_charset_t *mbcset, int *char_class_alloc,
3532 + const char *class_name, reg_syntax_t syntax)
3533 +#else /* not RE_ENABLE_I18N */
3534 +build_charclass (RE_TRANSLATE_TYPE trans, bitset_t sbcset,
3535 + const char *class_name, reg_syntax_t syntax)
3536 +#endif /* not RE_ENABLE_I18N */
3537 +{
3538 + int i;
3539 +
3540 + /* In case of REG_ICASE "upper" and "lower" match the both of
3541 + upper and lower cases. */
3542 + if ((syntax & RE_ICASE)
3543 + && (strcmp (class_name, "upper") == 0 || strcmp (class_name, "lower") == 0))
3544 + class_name = "alpha";
3545 +
3546 +#ifdef RE_ENABLE_I18N
3547 + /* Check the space of the arrays. */
3548 + if (BE (*char_class_alloc == mbcset->nchar_classes, 0))
3549 + {
3550 + /* Not enough, realloc it. */
3551 + /* +1 in case of mbcset->nchar_classes is 0. */
3552 + int new_char_class_alloc = 2 * mbcset->nchar_classes + 1;
3553 + /* Use realloc since array is NULL if *alloc == 0. */
3554 + wctype_t *new_char_classes = re_realloc (mbcset->char_classes, wctype_t,
3555 + new_char_class_alloc);
3556 + if (BE (new_char_classes == NULL, 0))
3557 + return REG_ESPACE;
3558 + mbcset->char_classes = new_char_classes;
3559 + *char_class_alloc = new_char_class_alloc;
3560 + }
3561 + mbcset->char_classes[mbcset->nchar_classes++] = __wctype (class_name);
3562 +#endif /* RE_ENABLE_I18N */
3563 +
3564 +#define BUILD_CHARCLASS_LOOP(ctype_func) \
3565 + do { \
3566 + if (BE (trans != NULL, 0)) \
3567 + { \
3568 + for (i = 0; i < SBC_MAX; ++i) \
3569 + if (ctype_func (i)) \
3570 + bitset_set (sbcset, trans[i]); \
3571 + } \
3572 + else \
3573 + { \
3574 + for (i = 0; i < SBC_MAX; ++i) \
3575 + if (ctype_func (i)) \
3576 + bitset_set (sbcset, i); \
3577 + } \
3578 + } while (0)
3579 +
3580 + if (strcmp (class_name, "alnum") == 0)
3581 + BUILD_CHARCLASS_LOOP (isalnum);
3582 + else if (strcmp (class_name, "cntrl") == 0)
3583 + BUILD_CHARCLASS_LOOP (iscntrl);
3584 + else if (strcmp (class_name, "lower") == 0)
3585 + BUILD_CHARCLASS_LOOP (islower);
3586 + else if (strcmp (class_name, "space") == 0)
3587 + BUILD_CHARCLASS_LOOP (isspace);
3588 + else if (strcmp (class_name, "alpha") == 0)
3589 + BUILD_CHARCLASS_LOOP (isalpha);
3590 + else if (strcmp (class_name, "digit") == 0)
3591 + BUILD_CHARCLASS_LOOP (isdigit);
3592 + else if (strcmp (class_name, "print") == 0)
3593 + BUILD_CHARCLASS_LOOP (isprint);
3594 + else if (strcmp (class_name, "upper") == 0)
3595 + BUILD_CHARCLASS_LOOP (isupper);
3596 + else if (strcmp (class_name, "blank") == 0)
3597 +#ifndef GAWK
3598 + BUILD_CHARCLASS_LOOP (isblank);
3599 +#else
3600 + /* see comments above */
3601 + BUILD_CHARCLASS_LOOP (is_blank);
3602 +#endif
3603 + else if (strcmp (class_name, "graph") == 0)
3604 + BUILD_CHARCLASS_LOOP (isgraph);
3605 + else if (strcmp (class_name, "punct") == 0)
3606 + BUILD_CHARCLASS_LOOP (ispunct);
3607 + else if (strcmp (class_name, "xdigit") == 0)
3608 + BUILD_CHARCLASS_LOOP (isxdigit);
3609 + else
3610 + return REG_ECTYPE;
3611 +
3612 + return REG_NOERROR;
3613 +}
3614 +
3615 +static bin_tree_t *
3616 +build_charclass_op (re_dfa_t *dfa, RE_TRANSLATE_TYPE trans,
3617 + const char *class_name,
3618 + const char *extra, int non_match,
3619 + reg_errcode_t *err)
3620 +{
3621 + re_bitset_ptr_t sbcset;
3622 +#ifdef RE_ENABLE_I18N
3623 + re_charset_t *mbcset;
3624 + int alloc = 0;
3625 +#endif /* not RE_ENABLE_I18N */
3626 + reg_errcode_t ret;
3627 + re_token_t br_token;
3628 + bin_tree_t *tree;
3629 +
3630 + sbcset = (re_bitset_ptr_t) calloc (sizeof (bitset_t), 1);
3631 +#ifdef RE_ENABLE_I18N
3632 + mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1);
3633 +#endif /* RE_ENABLE_I18N */
3634 +
3635 +#ifdef RE_ENABLE_I18N
3636 + if (BE (sbcset == NULL || mbcset == NULL, 0))
3637 +#else /* not RE_ENABLE_I18N */
3638 + if (BE (sbcset == NULL, 0))
3639 +#endif /* not RE_ENABLE_I18N */
3640 + {
3641 + *err = REG_ESPACE;
3642 + return NULL;
3643 + }
3644 +
3645 + if (non_match)
3646 + {
3647 +#ifdef RE_ENABLE_I18N
3648 + mbcset->non_match = 1;
3649 +#endif /* not RE_ENABLE_I18N */
3650 + }
3651 +
3652 + /* We don't care the syntax in this case. */
3653 + ret = build_charclass (trans, sbcset,
3654 +#ifdef RE_ENABLE_I18N
3655 + mbcset, &alloc,
3656 +#endif /* RE_ENABLE_I18N */
3657 + class_name, 0);
3658 +
3659 + if (BE (ret != REG_NOERROR, 0))
3660 + {
3661 + re_free (sbcset);
3662 +#ifdef RE_ENABLE_I18N
3663 + free_charset (mbcset);
3664 +#endif /* RE_ENABLE_I18N */
3665 + *err = ret;
3666 + return NULL;
3667 + }
3668 + /* \w match '_' also. */
3669 + for (; *extra; extra++)
3670 + bitset_set (sbcset, *extra);
3671 +
3672 + /* If it is non-matching list. */
3673 + if (non_match)
3674 + bitset_not (sbcset);
3675 +
3676 +#ifdef RE_ENABLE_I18N
3677 + /* Ensure only single byte characters are set. */
3678 + if (dfa->mb_cur_max > 1)
3679 + bitset_mask (sbcset, dfa->sb_char);
3680 +#endif
3681 +
3682 + /* Build a tree for simple bracket. */
3683 + br_token.type = SIMPLE_BRACKET;
3684 + br_token.opr.sbcset = sbcset;
3685 + tree = create_token_tree (dfa, NULL, NULL, &br_token);
3686 + if (BE (tree == NULL, 0))
3687 + goto build_word_op_espace;
3688 +
3689 +#ifdef RE_ENABLE_I18N
3690 + if (dfa->mb_cur_max > 1)
3691 + {
3692 + bin_tree_t *mbc_tree;
3693 + /* Build a tree for complex bracket. */
3694 + br_token.type = COMPLEX_BRACKET;
3695 + br_token.opr.mbcset = mbcset;
3696 + dfa->has_mb_node = 1;
3697 + mbc_tree = create_token_tree (dfa, NULL, NULL, &br_token);
3698 + if (BE (mbc_tree == NULL, 0))
3699 + goto build_word_op_espace;
3700 + /* Then join them by ALT node. */
3701 + tree = create_tree (dfa, tree, mbc_tree, OP_ALT);
3702 + if (BE (mbc_tree != NULL, 1))
3703 + return tree;
3704 + }
3705 + else
3706 + {
3707 + free_charset (mbcset);
3708 + return tree;
3709 + }
3710 +#else /* not RE_ENABLE_I18N */
3711 + return tree;
3712 +#endif /* not RE_ENABLE_I18N */
3713 +
3714 + build_word_op_espace:
3715 + re_free (sbcset);
3716 +#ifdef RE_ENABLE_I18N
3717 + free_charset (mbcset);
3718 +#endif /* RE_ENABLE_I18N */
3719 + *err = REG_ESPACE;
3720 + return NULL;
3721 +}
3722 +
3723 +/* This is intended for the expressions like "a{1,3}".
3724 + Fetch a number from `input', and return the number.
3725 + Return -1, if the number field is empty like "{,1}".
3726 + Return -2, If an error is occured. */
3727 +
3728 +static int
3729 +fetch_number (re_string_t *input, re_token_t *token, reg_syntax_t syntax)
3730 +{
3731 + int num = -1;
3732 + unsigned char c;
3733 + while (1)
3734 + {
3735 + fetch_token (token, input, syntax);
3736 + c = token->opr.c;
3737 + if (BE (token->type == END_OF_RE, 0))
3738 + return -2;
3739 + if (token->type == OP_CLOSE_DUP_NUM || c == ',')
3740 + break;
3741 + num = ((token->type != CHARACTER || c < '0' || '9' < c || num == -2)
3742 + ? -2 : ((num == -1) ? c - '0' : num * 10 + c - '0'));
3743 + num = (num > RE_DUP_MAX) ? -2 : num;
3744 + }
3745 + return num;
3746 +}
3747 +
3748 +#ifdef RE_ENABLE_I18N
3749 +static void
3750 +free_charset (re_charset_t *cset)
3751 +{
3752 + re_free (cset->mbchars);
3753 +# ifdef _LIBC
3754 + re_free (cset->coll_syms);
3755 + re_free (cset->equiv_classes);
3756 + re_free (cset->range_starts);
3757 + re_free (cset->range_ends);
3758 +# endif
3759 + re_free (cset->char_classes);
3760 + re_free (cset);
3761 +}
3762 +#endif /* RE_ENABLE_I18N */
3763 +
3764 +/* Functions for binary tree operation. */
3765 +
3766 +/* Create a tree node. */
3767 +
3768 +static bin_tree_t *
3769 +create_tree (re_dfa_t *dfa, bin_tree_t *left, bin_tree_t *right,
3770 + re_token_type_t type)
3771 +{
3772 + re_token_t t;
3773 + t.type = type;
3774 + return create_token_tree (dfa, left, right, &t);
3775 +}
3776 +
3777 +static bin_tree_t *
3778 +create_token_tree (re_dfa_t *dfa, bin_tree_t *left, bin_tree_t *right,
3779 + const re_token_t *token)
3780 +{
3781 + bin_tree_t *tree;
3782 + if (BE (dfa->str_tree_storage_idx == BIN_TREE_STORAGE_SIZE, 0))
3783 + {
3784 + bin_tree_storage_t *storage = re_malloc (bin_tree_storage_t, 1);
3785 +
3786 + if (storage == NULL)
3787 + return NULL;
3788 + storage->next = dfa->str_tree_storage;
3789 + dfa->str_tree_storage = storage;
3790 + dfa->str_tree_storage_idx = 0;
3791 + }
3792 + tree = &dfa->str_tree_storage->data[dfa->str_tree_storage_idx++];
3793 +
3794 + tree->parent = NULL;
3795 + tree->left = left;
3796 + tree->right = right;
3797 + tree->token = *token;
3798 + tree->token.duplicated = 0;
3799 + tree->token.opt_subexp = 0;
3800 + tree->first = NULL;
3801 + tree->next = NULL;
3802 + tree->node_idx = -1;
3803 +
3804 + if (left != NULL)
3805 + left->parent = tree;
3806 + if (right != NULL)
3807 + right->parent = tree;
3808 + return tree;
3809 +}
3810 +
3811 +/* Mark the tree SRC as an optional subexpression.
3812 + To be called from preorder or postorder. */
3813 +
3814 +static reg_errcode_t
3815 +mark_opt_subexp (void *extra, bin_tree_t *node)
3816 +{
3817 + int idx = (int) (long) extra;
3818 + if (node->token.type == SUBEXP && node->token.opr.idx == idx)
3819 + node->token.opt_subexp = 1;
3820 +
3821 + return REG_NOERROR;
3822 +}
3823 +
3824 +/* Free the allocated memory inside NODE. */
3825 +
3826 +static void
3827 +free_token (re_token_t *node)
3828 +{
3829 +#ifdef RE_ENABLE_I18N
3830 + if (node->type == COMPLEX_BRACKET && node->duplicated == 0)
3831 + free_charset (node->opr.mbcset);
3832 + else
3833 +#endif /* RE_ENABLE_I18N */
3834 + if (node->type == SIMPLE_BRACKET && node->duplicated == 0)
3835 + re_free (node->opr.sbcset);
3836 +}
3837 +
3838 +/* Worker function for tree walking. Free the allocated memory inside NODE
3839 + and its children. */
3840 +
3841 +static reg_errcode_t
3842 +free_tree (void *extra, bin_tree_t *node)
3843 +{
3844 + free_token (&node->token);
3845 + return REG_NOERROR;
3846 +}
3847 +
3848 +
3849 +/* Duplicate the node SRC, and return new node. This is a preorder
3850 + visit similar to the one implemented by the generic visitor, but
3851 + we need more infrastructure to maintain two parallel trees --- so,
3852 + it's easier to duplicate. */
3853 +
3854 +static bin_tree_t *
3855 +duplicate_tree (const bin_tree_t *root, re_dfa_t *dfa)
3856 +{
3857 + const bin_tree_t *node;
3858 + bin_tree_t *dup_root;
3859 + bin_tree_t **p_new = &dup_root, *dup_node = root->parent;
3860 +
3861 + for (node = root; ; )
3862 + {
3863 + /* Create a new tree and link it back to the current parent. */
3864 + *p_new = create_token_tree (dfa, NULL, NULL, &node->token);
3865 + if (*p_new == NULL)
3866 + return NULL;
3867 + (*p_new)->parent = dup_node;
3868 + (*p_new)->token.duplicated = 1;
3869 + dup_node = *p_new;
3870 +
3871 + /* Go to the left node, or up and to the right. */
3872 + if (node->left)
3873 + {
3874 + node = node->left;
3875 + p_new = &dup_node->left;
3876 + }
3877 + else
3878 + {
3879 + const bin_tree_t *prev = NULL;
3880 + while (node->right == prev || node->right == NULL)
3881 + {
3882 + prev = node;
3883 + node = node->parent;
3884 + dup_node = dup_node->parent;
3885 + if (!node)
3886 + return dup_root;
3887 + }
3888 + node = node->right;
3889 + p_new = &dup_node->right;
3890 + }
3891 + }
3892 +}
compat/regex/regex.c
+83 -4920
@@ -1,4924 +1,87 @@
1 -/* Extended regular expression matching and search library,
2 - version 0.12.
3 - (Implements POSIX draft P10003.2/D11.2, except for
4 - internationalization features.)
1 +/* Extended regular expression matching and search library.
2 + Copyright (C) 2002, 2003, 2005 Free Software Foundation, Inc.
3 + This file is part of the GNU C Library.
4 + Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
5
6 - Copyright (C) 1993 Free Software Foundation, Inc.
6 + The GNU C Library is free software; you can redistribute it and/or
7 + modify it under the terms of the GNU Lesser General Public
8 + License as published by the Free Software Foundation; either
9 + version 2.1 of the License, or (at your option) any later version.
10
8 - This program is free software; you can redistribute it and/or modify
9 - it under the terms of the GNU General Public License as published by
10 - the Free Software Foundation; either version 2, or (at your option)
11 - any later version.
12 -
13 - This program is distributed in the hope that it will be useful,
11 + The GNU C Library is distributed in the hope that it will be useful,
12 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.
17 -
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 -
22 -/* AIX requires this to be the first thing in the file. */
23 -#if defined (_AIX) && !defined (REGEX_MALLOC)
24 - #pragma alloca
25 -#endif
26 -
27 -#define _GNU_SOURCE
28 -
29 -/* We need this for `regex.h', and perhaps for the Emacs include files. */
30 -#include <sys/types.h>
31 -
32 -/* We used to test for `BSTRING' here, but only GCC and Emacs define
33 - `BSTRING', as far as I know, and neither of them use this code. */
34 -#include <string.h>
35 -#ifndef bcmp
36 -#define bcmp(s1, s2, n) memcmp ((s1), (s2), (n))
37 -#endif
38 -#ifndef bcopy
39 -#define bcopy(s, d, n) memcpy ((d), (s), (n))
40 -#endif
41 -#ifndef bzero
42 -#define bzero(s, n) memset ((s), 0, (n))
43 -#endif
44 -
45 -#include <stdlib.h>
46 -
47 -
48 -/* Define the syntax stuff for \<, \>, etc. */
49 -
50 -/* This must be nonzero for the wordchar and notwordchar pattern
51 - commands in re_match_2. */
52 -#ifndef Sword
53 -#define Sword 1
54 -#endif
55 -
56 -#ifdef SYNTAX_TABLE
57 -
58 -extern char *re_syntax_table;
59 -
60 -#else /* not SYNTAX_TABLE */
61 -
62 -/* How many characters in the character set. */
63 -#define CHAR_SET_SIZE 256
64 -
65 -static char re_syntax_table[CHAR_SET_SIZE];
66 -
67 -static void
68 -init_syntax_once ()
69 -{
70 - register int c;
71 - static int done = 0;
72 -
73 - if (done)
74 - return;
75 -
76 - bzero (re_syntax_table, sizeof re_syntax_table);
77 -
78 - for (c = 'a'; c <= 'z'; c++)
79 - re_syntax_table[c] = Sword;
80 -
81 - for (c = 'A'; c <= 'Z'; c++)
82 - re_syntax_table[c] = Sword;
83 -
84 - for (c = '0'; c <= '9'; c++)
85 - re_syntax_table[c] = Sword;
86 -
87 - re_syntax_table['_'] = Sword;
88 -
89 - done = 1;
90 -}
91 -
92 -#endif /* not SYNTAX_TABLE */
93 -
94 -#define SYNTAX(c) re_syntax_table[c]
95 -
96 -
97 -/* Get the interface, including the syntax bits. */
98 -#include "regex.h"
99 -
100 -/* isalpha etc. are used for the character classes. */
101 -#include <ctype.h>
102 -
103 -#ifndef isascii
104 -#define isascii(c) 1
105 -#endif
106 -
107 -#ifdef isblank
108 -#define ISBLANK(c) (isascii (c) && isblank (c))
109 -#else
110 -#define ISBLANK(c) ((c) == ' ' || (c) == '\t')
111 -#endif
112 -#ifdef isgraph
113 -#define ISGRAPH(c) (isascii (c) && isgraph (c))
114 -#else
115 -#define ISGRAPH(c) (isascii (c) && isprint (c) && !isspace (c))
116 -#endif
117 -
118 -#define ISPRINT(c) (isascii (c) && isprint (c))
119 -#define ISDIGIT(c) (isascii (c) && isdigit (c))
120 -#define ISALNUM(c) (isascii (c) && isalnum (c))
121 -#define ISALPHA(c) (isascii (c) && isalpha (c))
122 -#define ISCNTRL(c) (isascii (c) && iscntrl (c))
123 -#define ISLOWER(c) (isascii (c) && islower (c))
124 -#define ISPUNCT(c) (isascii (c) && ispunct (c))
125 -#define ISSPACE(c) (isascii (c) && isspace (c))
126 -#define ISUPPER(c) (isascii (c) && isupper (c))
127 -#define ISXDIGIT(c) (isascii (c) && isxdigit (c))
128 -
129 -#ifndef NULL
130 -#define NULL 0
131 -#endif
132 -
133 -/* We remove any previous definition of `SIGN_EXTEND_CHAR',
134 - since ours (we hope) works properly with all combinations of
135 - machines, compilers, `char' and `unsigned char' argument types.
136 - (Per Bothner suggested the basic approach.) */
137 -#undef SIGN_EXTEND_CHAR
138 -#if __STDC__
139 -#define SIGN_EXTEND_CHAR(c) ((signed char) (c))
140 -#else /* not __STDC__ */
141 -/* As in Harbison and Steele. */
142 -#define SIGN_EXTEND_CHAR(c) ((((unsigned char) (c)) ^ 128) - 128)
143 -#endif
144 -
145 -/* Should we use malloc or alloca? If REGEX_MALLOC is not defined, we
146 - use `alloca' instead of `malloc'. This is because using malloc in
147 - re_search* or re_match* could cause memory leaks when C-g is used in
148 - Emacs; also, malloc is slower and causes storage fragmentation. On
149 - the other hand, malloc is more portable, and easier to debug.
150 -
151 - Because we sometimes use alloca, some routines have to be macros,
152 - not functions -- `alloca'-allocated space disappears at the end of the
153 - function it is called in. */
154 -
155 -#ifdef REGEX_MALLOC
156 -
157 -#define REGEX_ALLOCATE malloc
158 -#define REGEX_REALLOCATE(source, osize, nsize) realloc (source, nsize)
159 -
160 -#else /* not REGEX_MALLOC */
161 -
162 -/* Emacs already defines alloca, sometimes. */
163 -#ifndef alloca
164 -
165 -/* Make alloca work the best possible way. */
166 -#ifdef __GNUC__
167 -#define alloca __builtin_alloca
168 -#else /* not __GNUC__ */
169 -#if HAVE_ALLOCA_H
170 -#include <alloca.h>
171 -#else /* not __GNUC__ or HAVE_ALLOCA_H */
172 -#ifndef _AIX /* Already did AIX, up at the top. */
173 -char *alloca ();
174 -#endif /* not _AIX */
175 -#endif /* not HAVE_ALLOCA_H */
176 -#endif /* not __GNUC__ */
177 -
178 -#endif /* not alloca */
179 -
180 -#define REGEX_ALLOCATE alloca
181 -
182 -/* Assumes a `char *destination' variable. */
183 -#define REGEX_REALLOCATE(source, osize, nsize) \
184 - (destination = (char *) alloca (nsize), \
185 - bcopy (source, destination, osize), \
186 - destination)
187 -
188 -#endif /* not REGEX_MALLOC */
189 -
190 -
191 -/* True if `size1' is non-NULL and PTR is pointing anywhere inside
192 - `string1' or just past its end. This works if PTR is NULL, which is
193 - a good thing. */
194 -#define FIRST_STRING_P(ptr) \
195 - (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
196 -
197 -/* (Re)Allocate N items of type T using malloc, or fail. */
198 -#define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t)))
199 -#define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
200 -#define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
201 -
202 -#define BYTEWIDTH 8 /* In bits. */
203 -
204 -#define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
205 -
206 -#define MAX(a, b) ((a) > (b) ? (a) : (b))
207 -#define MIN(a, b) ((a) < (b) ? (a) : (b))
208 -
209 -typedef char boolean;
210 -#define false 0
211 -#define true 1
212 -
213 -/* These are the command codes that appear in compiled regular
214 - expressions. Some opcodes are followed by argument bytes. A
215 - command code can specify any interpretation whatsoever for its
216 - arguments. Zero bytes may appear in the compiled regular expression.
217 -
218 - The value of `exactn' is needed in search.c (search_buffer) in Emacs.
219 - So regex.h defines a symbol `RE_EXACTN_VALUE' to be 1; the value of
220 - `exactn' we use here must also be 1. */
221 -
222 -typedef enum
223 -{
224 - no_op = 0,
225 -
226 - /* Followed by one byte giving n, then by n literal bytes. */
227 - exactn = 1,
228 -
229 - /* Matches any (more or less) character. */
230 - anychar,
231 -
232 - /* Matches any one char belonging to specified set. First
233 - following byte is number of bitmap bytes. Then come bytes
234 - for a bitmap saying which chars are in. Bits in each byte
235 - are ordered low-bit-first. A character is in the set if its
236 - bit is 1. A character too large to have a bit in the map is
237 - automatically not in the set. */
238 - charset,
239 -
240 - /* Same parameters as charset, but match any character that is
241 - not one of those specified. */
242 - charset_not,
243 -
244 - /* Start remembering the text that is matched, for storing in a
245 - register. Followed by one byte with the register number, in
246 - the range 0 to one less than the pattern buffer's re_nsub
247 - field. Then followed by one byte with the number of groups
248 - inner to this one. (This last has to be part of the
249 - start_memory only because we need it in the on_failure_jump
250 - of re_match_2.) */
251 - start_memory,
252 -
253 - /* Stop remembering the text that is matched and store it in a
254 - memory register. Followed by one byte with the register
255 - number, in the range 0 to one less than `re_nsub' in the
256 - pattern buffer, and one byte with the number of inner groups,
257 - just like `start_memory'. (We need the number of inner
258 - groups here because we don't have any easy way of finding the
259 - corresponding start_memory when we're at a stop_memory.) */
260 - stop_memory,
261 -
262 - /* Match a duplicate of something remembered. Followed by one
263 - byte containing the register number. */
264 - duplicate,
265 -
266 - /* Fail unless at beginning of line. */
267 - begline,
268 -
269 - /* Fail unless at end of line. */
270 - endline,
271 -
272 - /* Succeeds if at beginning of buffer (if emacs) or at beginning
273 - of string to be matched (if not). */
274 - begbuf,
275 -
276 - /* Analogously, for end of buffer/string. */
277 - endbuf,
278 -
279 - /* Followed by two byte relative address to which to jump. */
280 - jump,
281 -
282 - /* Same as jump, but marks the end of an alternative. */
283 - jump_past_alt,
284 -
285 - /* Followed by two-byte relative address of place to resume at
286 - in case of failure. */
287 - on_failure_jump,
288 -
289 - /* Like on_failure_jump, but pushes a placeholder instead of the
290 - current string position when executed. */
291 - on_failure_keep_string_jump,
292 -
293 - /* Throw away latest failure point and then jump to following
294 - two-byte relative address. */
295 - pop_failure_jump,
296 -
297 - /* Change to pop_failure_jump if know won't have to backtrack to
298 - match; otherwise change to jump. This is used to jump
299 - back to the beginning of a repeat. If what follows this jump
300 - clearly won't match what the repeat does, such that we can be
301 - sure that there is no use backtracking out of repetitions
302 - already matched, then we change it to a pop_failure_jump.
303 - Followed by two-byte address. */
304 - maybe_pop_jump,
305 -
306 - /* Jump to following two-byte address, and push a dummy failure
307 - point. This failure point will be thrown away if an attempt
308 - is made to use it for a failure. A `+' construct makes this
309 - before the first repeat. Also used as an intermediary kind
310 - of jump when compiling an alternative. */
311 - dummy_failure_jump,
312 -
313 - /* Push a dummy failure point and continue. Used at the end of
314 - alternatives. */
315 - push_dummy_failure,
316 -
317 - /* Followed by two-byte relative address and two-byte number n.
318 - After matching N times, jump to the address upon failure. */
319 - succeed_n,
320 -
321 - /* Followed by two-byte relative address, and two-byte number n.
322 - Jump to the address N times, then fail. */
323 - jump_n,
324 -
325 - /* Set the following two-byte relative address to the
326 - subsequent two-byte number. The address *includes* the two
327 - bytes of number. */
328 - set_number_at,
329 -
330 - wordchar, /* Matches any word-constituent character. */
331 - notwordchar, /* Matches any char that is not a word-constituent. */
332 -
333 - wordbeg, /* Succeeds if at word beginning. */
334 - wordend, /* Succeeds if at word end. */
335 -
336 - wordbound, /* Succeeds if at a word boundary. */
337 - notwordbound /* Succeeds if not at a word boundary. */
338 -
339 -#ifdef emacs
340 - ,before_dot, /* Succeeds if before point. */
341 - at_dot, /* Succeeds if at point. */
342 - after_dot, /* Succeeds if after point. */
343 -
344 - /* Matches any character whose syntax is specified. Followed by
345 - a byte which contains a syntax code, e.g., Sword. */
346 - syntaxspec,
347 -
348 - /* Matches any character whose syntax is not that specified. */
349 - notsyntaxspec
350 -#endif /* emacs */
351 -} re_opcode_t;
352 -
353 -/* Common operations on the compiled pattern. */
354 -
355 -/* Store NUMBER in two contiguous bytes starting at DESTINATION. */
356 -
357 -#define STORE_NUMBER(destination, number) \
358 - do { \
359 - (destination)[0] = (number) & 0377; \
360 - (destination)[1] = (number) >> 8; \
361 - } while (0)
362 -
363 -/* Same as STORE_NUMBER, except increment DESTINATION to
364 - the byte after where the number is stored. Therefore, DESTINATION
365 - must be an lvalue. */
366 -
367 -#define STORE_NUMBER_AND_INCR(destination, number) \
368 - do { \
369 - STORE_NUMBER (destination, number); \
370 - (destination) += 2; \
371 - } while (0)
372 -
373 -/* Put into DESTINATION a number stored in two contiguous bytes starting
374 - at SOURCE. */
375 -
376 -#define EXTRACT_NUMBER(destination, source) \
377 - do { \
378 - (destination) = *(source) & 0377; \
379 - (destination) += SIGN_EXTEND_CHAR (*((source) + 1)) << 8; \
380 - } while (0)
381 -
382 -#ifdef DEBUG
383 -static void
384 -extract_number (dest, source)
385 - int *dest;
386 - unsigned char *source;
387 -{
388 - int temp = SIGN_EXTEND_CHAR (*(source + 1));
389 - *dest = *source & 0377;
390 - *dest += temp << 8;
391 -}
392 -
393 -#ifndef EXTRACT_MACROS /* To debug the macros. */
394 -#undef EXTRACT_NUMBER
395 -#define EXTRACT_NUMBER(dest, src) extract_number (&dest, src)
396 -#endif /* not EXTRACT_MACROS */
397 -
398 -#endif /* DEBUG */
399 -
400 -/* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
401 - SOURCE must be an lvalue. */
402 -
403 -#define EXTRACT_NUMBER_AND_INCR(destination, source) \
404 - do { \
405 - EXTRACT_NUMBER (destination, source); \
406 - (source) += 2; \
407 - } while (0)
408 -
409 -#ifdef DEBUG
410 -static void
411 -extract_number_and_incr (destination, source)
412 - int *destination;
413 - unsigned char **source;
414 -{
415 - extract_number (destination, *source);
416 - *source += 2;
417 -}
418 -
419 -#ifndef EXTRACT_MACROS
420 -#undef EXTRACT_NUMBER_AND_INCR
421 -#define EXTRACT_NUMBER_AND_INCR(dest, src) \
422 - extract_number_and_incr (&dest, &src)
423 -#endif /* not EXTRACT_MACROS */
424 -
425 -#endif /* DEBUG */
426 -
427 -/* If DEBUG is defined, Regex prints many voluminous messages about what
428 - it is doing (if the variable `debug' is nonzero). If linked with the
429 - main program in `iregex.c', you can enter patterns and strings
430 - interactively. And if linked with the main program in `main.c' and
431 - the other test files, you can run the already-written tests. */
432 -
433 -#ifdef DEBUG
434 -
435 -/* We use standard I/O for debugging. */
436 -#include <stdio.h>
437 -
438 -/* It is useful to test things that ``must'' be true when debugging. */
439 -#include <assert.h>
440 -
441 -static int debug = 0;
442 -
443 -#define DEBUG_STATEMENT(e) e
444 -#define DEBUG_PRINT1(x) if (debug) printf (x)
445 -#define DEBUG_PRINT2(x1, x2) if (debug) printf (x1, x2)
446 -#define DEBUG_PRINT3(x1, x2, x3) if (debug) printf (x1, x2, x3)
447 -#define DEBUG_PRINT4(x1, x2, x3, x4) if (debug) printf (x1, x2, x3, x4)
448 -#define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) \
449 - if (debug) print_partial_compiled_pattern (s, e)
450 -#define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \
451 - if (debug) print_double_string (w, s1, sz1, s2, sz2)
452 -
453 -
454 -extern void printchar ();
455 -
456 -/* Print the fastmap in human-readable form. */
457 -
458 -void
459 -print_fastmap (fastmap)
460 - char *fastmap;
461 -{
462 - unsigned was_a_range = 0;
463 - unsigned i = 0;
464 -
465 - while (i < (1 << BYTEWIDTH))
466 - {
467 - if (fastmap[i++])
468 - {
469 - was_a_range = 0;
470 - printchar (i - 1);
471 - while (i < (1 << BYTEWIDTH) && fastmap[i])
472 - {
473 - was_a_range = 1;
474 - i++;
475 - }
476 - if (was_a_range)
477 - {
478 - printf ("-");
479 - printchar (i - 1);
480 - }
481 - }
482 - }
483 - putchar ('\n');
484 -}
485 -
486 -
487 -/* Print a compiled pattern string in human-readable form, starting at
488 - the START pointer into it and ending just before the pointer END. */
489 -
490 -void
491 -print_partial_compiled_pattern (start, end)
492 - unsigned char *start;
493 - unsigned char *end;
494 -{
495 - int mcnt, mcnt2;
496 - unsigned char *p = start;
497 - unsigned char *pend = end;
498 -
499 - if (start == NULL)
500 - {
501 - printf ("(null)\n");
502 - return;
503 - }
504 -
505 - /* Loop over pattern commands. */
506 - while (p < pend)
507 - {
508 - switch ((re_opcode_t) *p++)
509 - {
510 - case no_op:
511 - printf ("/no_op");
512 - break;
513 -
514 - case exactn:
515 - mcnt = *p++;
516 - printf ("/exactn/%d", mcnt);
517 - do
518 - {
519 - putchar ('/');
520 - printchar (*p++);
521 - }
522 - while (--mcnt);
523 - break;
524 -
525 - case start_memory:
526 - mcnt = *p++;
527 - printf ("/start_memory/%d/%d", mcnt, *p++);
528 - break;
529 -
530 - case stop_memory:
531 - mcnt = *p++;
532 - printf ("/stop_memory/%d/%d", mcnt, *p++);
533 - break;
534 -
535 - case duplicate:
536 - printf ("/duplicate/%d", *p++);
537 - break;
538 -
539 - case anychar:
540 - printf ("/anychar");
541 - break;
542 -
543 - case charset:
544 - case charset_not:
545 - {
546 - register int c;
547 -
548 - printf ("/charset%s",
549 - (re_opcode_t) *(p - 1) == charset_not ? "_not" : "");
550 -
551 - assert (p + *p < pend);
552 -
553 - for (c = 0; c < *p; c++)
554 - {
555 - unsigned bit;
556 - unsigned char map_byte = p[1 + c];
557 -
558 - putchar ('/');
559 -
560 - for (bit = 0; bit < BYTEWIDTH; bit++)
561 - if (map_byte & (1 << bit))
562 - printchar (c * BYTEWIDTH + bit);
563 - }
564 - p += 1 + *p;
565 - break;
566 - }
567 -
568 - case begline:
569 - printf ("/begline");
570 - break;
571 -
572 - case endline:
573 - printf ("/endline");
574 - break;
575 -
576 - case on_failure_jump:
577 - extract_number_and_incr (&mcnt, &p);
578 - printf ("/on_failure_jump/0/%d", mcnt);
579 - break;
580 -
581 - case on_failure_keep_string_jump:
582 - extract_number_and_incr (&mcnt, &p);
583 - printf ("/on_failure_keep_string_jump/0/%d", mcnt);
584 - break;
585 -
586 - case dummy_failure_jump:
587 - extract_number_and_incr (&mcnt, &p);
588 - printf ("/dummy_failure_jump/0/%d", mcnt);
589 - break;
590 -
591 - case push_dummy_failure:
592 - printf ("/push_dummy_failure");
593 - break;
594 -
595 - case maybe_pop_jump:
596 - extract_number_and_incr (&mcnt, &p);
597 - printf ("/maybe_pop_jump/0/%d", mcnt);
598 - break;
599 -
600 - case pop_failure_jump:
601 - extract_number_and_incr (&mcnt, &p);
602 - printf ("/pop_failure_jump/0/%d", mcnt);
603 - break;
604 -
605 - case jump_past_alt:
606 - extract_number_and_incr (&mcnt, &p);
607 - printf ("/jump_past_alt/0/%d", mcnt);
608 - break;
609 -
610 - case jump:
611 - extract_number_and_incr (&mcnt, &p);
612 - printf ("/jump/0/%d", mcnt);
613 - break;
614 -
615 - case succeed_n:
616 - extract_number_and_incr (&mcnt, &p);
617 - extract_number_and_incr (&mcnt2, &p);
618 - printf ("/succeed_n/0/%d/0/%d", mcnt, mcnt2);
619 - break;
620 -
621 - case jump_n:
622 - extract_number_and_incr (&mcnt, &p);
623 - extract_number_and_incr (&mcnt2, &p);
624 - printf ("/jump_n/0/%d/0/%d", mcnt, mcnt2);
625 - break;
626 -
627 - case set_number_at:
628 - extract_number_and_incr (&mcnt, &p);
629 - extract_number_and_incr (&mcnt2, &p);
630 - printf ("/set_number_at/0/%d/0/%d", mcnt, mcnt2);
631 - break;
632 -
633 - case wordbound:
634 - printf ("/wordbound");
635 - break;
636 -
637 - case notwordbound:
638 - printf ("/notwordbound");
639 - break;
640 -
641 - case wordbeg:
642 - printf ("/wordbeg");
643 - break;
644 -
645 - case wordend:
646 - printf ("/wordend");
647 -
648 -#ifdef emacs
649 - case before_dot:
650 - printf ("/before_dot");
651 - break;
652 -
653 - case at_dot:
654 - printf ("/at_dot");
655 - break;
656 -
657 - case after_dot:
658 - printf ("/after_dot");
659 - break;
660 -
661 - case syntaxspec:
662 - printf ("/syntaxspec");
663 - mcnt = *p++;
664 - printf ("/%d", mcnt);
665 - break;
666 -
667 - case notsyntaxspec:
668 - printf ("/notsyntaxspec");
669 - mcnt = *p++;
670 - printf ("/%d", mcnt);
671 - break;
672 -#endif /* emacs */
673 -
674 - case wordchar:
675 - printf ("/wordchar");
676 - break;
677 -
678 - case notwordchar:
679 - printf ("/notwordchar");
680 - break;
681 -
682 - case begbuf:
683 - printf ("/begbuf");
684 - break;
685 -
686 - case endbuf:
687 - printf ("/endbuf");
688 - break;
689 -
690 - default:
691 - printf ("?%d", *(p-1));
692 - }
693 - }
694 - printf ("/\n");
695 -}
696 -
697 -
698 -void
699 -print_compiled_pattern (bufp)
700 - struct re_pattern_buffer *bufp;
701 -{
702 - unsigned char *buffer = bufp->buffer;
703 -
704 - print_partial_compiled_pattern (buffer, buffer + bufp->used);
705 - printf ("%d bytes used/%d bytes allocated.\n", bufp->used, bufp->allocated);
706 -
707 - if (bufp->fastmap_accurate && bufp->fastmap)
708 - {
709 - printf ("fastmap: ");
710 - print_fastmap (bufp->fastmap);
711 - }
712 -
713 - printf ("re_nsub: %d\t", bufp->re_nsub);
714 - printf ("regs_alloc: %d\t", bufp->regs_allocated);
715 - printf ("can_be_null: %d\t", bufp->can_be_null);
716 - printf ("newline_anchor: %d\n", bufp->newline_anchor);
717 - printf ("no_sub: %d\t", bufp->no_sub);
718 - printf ("not_bol: %d\t", bufp->not_bol);
719 - printf ("not_eol: %d\t", bufp->not_eol);
720 - printf ("syntax: %d\n", bufp->syntax);
721 - /* Perhaps we should print the translate table? */
722 -}
723 -
724 -
725 -void
726 -print_double_string (where, string1, size1, string2, size2)
727 - const char *where;
728 - const char *string1;
729 - const char *string2;
730 - int size1;
731 - int size2;
732 -{
733 - unsigned this_char;
734 -
735 - if (where == NULL)
736 - printf ("(null)");
737 - else
738 - {
739 - if (FIRST_STRING_P (where))
740 - {
741 - for (this_char = where - string1; this_char < size1; this_char++)
742 - printchar (string1[this_char]);
743 -
744 - where = string2;
745 - }
746 -
747 - for (this_char = where - string2; this_char < size2; this_char++)
748 - printchar (string2[this_char]);
749 - }
750 -}
751 -
752 -#else /* not DEBUG */
753 -
754 -#undef assert
755 -#define assert(e)
756 -
757 -#define DEBUG_STATEMENT(e)
758 -#define DEBUG_PRINT1(x)
759 -#define DEBUG_PRINT2(x1, x2)
760 -#define DEBUG_PRINT3(x1, x2, x3)
761 -#define DEBUG_PRINT4(x1, x2, x3, x4)
762 -#define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
763 -#define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
764 -
765 -#endif /* not DEBUG */
766 -
767 -/* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
768 - also be assigned to arbitrarily: each pattern buffer stores its own
769 - syntax, so it can be changed between regex compilations. */
770 -reg_syntax_t re_syntax_options = RE_SYNTAX_EMACS;
771 -
772 -
773 -/* Specify the precise syntax of regexps for compilation. This provides
774 - for compatibility for various utilities which historically have
775 - different, incompatible syntaxes.
776 -
777 - The argument SYNTAX is a bit mask comprised of the various bits
778 - defined in regex.h. We return the old syntax. */
779 -
780 -reg_syntax_t
781 -re_set_syntax (syntax)
782 - reg_syntax_t syntax;
783 -{
784 - reg_syntax_t ret = re_syntax_options;
785 -
786 - re_syntax_options = syntax;
787 - return ret;
788 -}
789 -
790 -/* This table gives an error message for each of the error codes listed
791 - in regex.h. Obviously the order here has to be same as there. */
792 -
793 -static const char *re_error_msg[] =
794 - { NULL, /* REG_NOERROR */
795 - "No match", /* REG_NOMATCH */
796 - "Invalid regular expression", /* REG_BADPAT */
797 - "Invalid collation character", /* REG_ECOLLATE */
798 - "Invalid character class name", /* REG_ECTYPE */
799 - "Trailing backslash", /* REG_EESCAPE */
800 - "Invalid back reference", /* REG_ESUBREG */
801 - "Unmatched [ or [^", /* REG_EBRACK */
802 - "Unmatched ( or \\(", /* REG_EPAREN */
803 - "Unmatched \\{", /* REG_EBRACE */
804 - "Invalid content of \\{\\}", /* REG_BADBR */
805 - "Invalid range end", /* REG_ERANGE */
806 - "Memory exhausted", /* REG_ESPACE */
807 - "Invalid preceding regular expression", /* REG_BADRPT */
808 - "Premature end of regular expression", /* REG_EEND */
809 - "Regular expression too big", /* REG_ESIZE */
810 - "Unmatched ) or \\)", /* REG_ERPAREN */
811 - };
812 -
813 -/* Subroutine declarations and macros for regex_compile. */
814 -
815 -static void store_op1 (), store_op2 ();
816 -static void insert_op1 (), insert_op2 ();
817 -static boolean at_begline_loc_p (), at_endline_loc_p ();
818 -static boolean group_in_compile_stack ();
819 -static reg_errcode_t compile_range ();
820 -
821 -/* Fetch the next character in the uncompiled pattern---translating it
822 - if necessary. Also cast from a signed character in the constant
823 - string passed to us by the user to an unsigned char that we can use
824 - as an array index (in, e.g., `translate'). */
825 -#define PATFETCH(c) \
826 - do {if (p == pend) return REG_EEND; \
827 - c = (unsigned char) *p++; \
828 - if (translate) c = translate[c]; \
829 - } while (0)
830 -
831 -/* Fetch the next character in the uncompiled pattern, with no
832 - translation. */
833 -#define PATFETCH_RAW(c) \
834 - do {if (p == pend) return REG_EEND; \
835 - c = (unsigned char) *p++; \
836 - } while (0)
837 -
838 -/* Go backwards one character in the pattern. */
839 -#define PATUNFETCH p--
840 -
841 -
842 -/* If `translate' is non-null, return translate[D], else just D. We
843 - cast the subscript to translate because some data is declared as
844 - `char *', to avoid warnings when a string constant is passed. But
845 - when we use a character as a subscript we must make it unsigned. */
846 -#define TRANSLATE(d) (translate ? translate[(unsigned char) (d)] : (d))
847 -
848 -
849 -/* Macros for outputting the compiled pattern into `buffer'. */
850 -
851 -/* If the buffer isn't allocated when it comes in, use this. */
852 -#define INIT_BUF_SIZE 32
853 -
854 -/* Make sure we have at least N more bytes of space in buffer. */
855 -#define GET_BUFFER_SPACE(n) \
856 - while (b - bufp->buffer + (n) > bufp->allocated) \
857 - EXTEND_BUFFER ()
858 -
859 -/* Make sure we have one more byte of buffer space and then add C to it. */
860 -#define BUF_PUSH(c) \
861 - do { \
862 - GET_BUFFER_SPACE (1); \
863 - *b++ = (unsigned char) (c); \
864 - } while (0)
865 -
866 -
867 -/* Ensure we have two more bytes of buffer space and then append C1 and C2. */
868 -#define BUF_PUSH_2(c1, c2) \
869 - do { \
870 - GET_BUFFER_SPACE (2); \
871 - *b++ = (unsigned char) (c1); \
872 - *b++ = (unsigned char) (c2); \
873 - } while (0)
874 -
875 -
876 -/* As with BUF_PUSH_2, except for three bytes. */
877 -#define BUF_PUSH_3(c1, c2, c3) \
878 - do { \
879 - GET_BUFFER_SPACE (3); \
880 - *b++ = (unsigned char) (c1); \
881 - *b++ = (unsigned char) (c2); \
882 - *b++ = (unsigned char) (c3); \
883 - } while (0)
884 -
885 -
886 -/* Store a jump with opcode OP at LOC to location TO. We store a
887 - relative address offset by the three bytes the jump itself occupies. */
888 -#define STORE_JUMP(op, loc, to) \
889 - store_op1 (op, loc, (to) - (loc) - 3)
890 -
891 -/* Likewise, for a two-argument jump. */
892 -#define STORE_JUMP2(op, loc, to, arg) \
893 - store_op2 (op, loc, (to) - (loc) - 3, arg)
894 -
895 -/* Like `STORE_JUMP', but for inserting. Assume `b' is the buffer end. */
896 -#define INSERT_JUMP(op, loc, to) \
897 - insert_op1 (op, loc, (to) - (loc) - 3, b)
898 -
899 -/* Like `STORE_JUMP2', but for inserting. Assume `b' is the buffer end. */
900 -#define INSERT_JUMP2(op, loc, to, arg) \
901 - insert_op2 (op, loc, (to) - (loc) - 3, arg, b)
902 -
903 -
904 -/* This is not an arbitrary limit: the arguments which represent offsets
905 - into the pattern are two bytes long. So if 2^16 bytes turns out to
906 - be too small, many things would have to change. */
907 -#define MAX_BUF_SIZE (1L << 16)
908 -
909 -
910 -/* Extend the buffer by twice its current size via realloc and
911 - reset the pointers that pointed into the old block to point to the
912 - correct places in the new one. If extending the buffer results in it
913 - being larger than MAX_BUF_SIZE, then flag memory exhausted. */
914 -#define EXTEND_BUFFER() \
915 - do { \
916 - unsigned char *old_buffer = bufp->buffer; \
917 - if (bufp->allocated == MAX_BUF_SIZE) \
918 - return REG_ESIZE; \
919 - bufp->allocated <<= 1; \
920 - if (bufp->allocated > MAX_BUF_SIZE) \
921 - bufp->allocated = MAX_BUF_SIZE; \
922 - bufp->buffer = (unsigned char *) realloc (bufp->buffer, bufp->allocated);\
923 - if (bufp->buffer == NULL) \
924 - return REG_ESPACE; \
925 - /* If the buffer moved, move all the pointers into it. */ \
926 - if (old_buffer != bufp->buffer) \
927 - { \
928 - b = (b - old_buffer) + bufp->buffer; \
929 - begalt = (begalt - old_buffer) + bufp->buffer; \
930 - if (fixup_alt_jump) \
931 - fixup_alt_jump = (fixup_alt_jump - old_buffer) + bufp->buffer;\
932 - if (laststart) \
933 - laststart = (laststart - old_buffer) + bufp->buffer; \
934 - if (pending_exact) \
935 - pending_exact = (pending_exact - old_buffer) + bufp->buffer; \
936 - } \
937 - } while (0)
938 -
939 -
940 -/* Since we have one byte reserved for the register number argument to
941 - {start,stop}_memory, the maximum number of groups we can report
942 - things about is what fits in that byte. */
943 -#define MAX_REGNUM 255
944 -
945 -/* But patterns can have more than `MAX_REGNUM' registers. We just
946 - ignore the excess. */
947 -typedef unsigned regnum_t;
948 -
949 -
950 -/* Macros for the compile stack. */
951 -
952 -/* Since offsets can go either forwards or backwards, this type needs to
953 - be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1. */
954 -typedef int pattern_offset_t;
955 -
956 -typedef struct
957 -{
958 - pattern_offset_t begalt_offset;
959 - pattern_offset_t fixup_alt_jump;
960 - pattern_offset_t inner_group_offset;
961 - pattern_offset_t laststart_offset;
962 - regnum_t regnum;
963 -} compile_stack_elt_t;
964 -
965 -
966 -typedef struct
967 -{
968 - compile_stack_elt_t *stack;
969 - unsigned size;
970 - unsigned avail; /* Offset of next open position. */
971 -} compile_stack_type;
972 -
973 -
974 -#define INIT_COMPILE_STACK_SIZE 32
975 -
976 -#define COMPILE_STACK_EMPTY (compile_stack.avail == 0)
977 -#define COMPILE_STACK_FULL (compile_stack.avail == compile_stack.size)
978 -
979 -/* The next available element. */
980 -#define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
981 -
982 -
983 -/* Set the bit for character C in a list. */
984 -#define SET_LIST_BIT(c) \
985 - (b[((unsigned char) (c)) / BYTEWIDTH] \
986 - |= 1 << (((unsigned char) c) % BYTEWIDTH))
987 -
988 -
989 -/* Get the next unsigned number in the uncompiled pattern. */
990 -#define GET_UNSIGNED_NUMBER(num) \
991 - { if (p != pend) \
992 - { \
993 - PATFETCH (c); \
994 - while (ISDIGIT (c)) \
995 - { \
996 - if (num < 0) \
997 - num = 0; \
998 - num = num * 10 + c - '0'; \
999 - if (p == pend) \
1000 - break; \
1001 - PATFETCH (c); \
1002 - } \
1003 - } \
1004 - }
1005 -
1006 -#define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
1007 -
1008 -#define IS_CHAR_CLASS(string) \
1009 - (STREQ (string, "alpha") || STREQ (string, "upper") \
1010 - || STREQ (string, "lower") || STREQ (string, "digit") \
1011 - || STREQ (string, "alnum") || STREQ (string, "xdigit") \
1012 - || STREQ (string, "space") || STREQ (string, "print") \
1013 - || STREQ (string, "punct") || STREQ (string, "graph") \
1014 - || STREQ (string, "cntrl") || STREQ (string, "blank"))
1015 -
1016 -/* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
1017 - Returns one of error codes defined in `regex.h', or zero for success.
1018 -
1019 - Assumes the `allocated' (and perhaps `buffer') and `translate'
1020 - fields are set in BUFP on entry.
1021 -
1022 - If it succeeds, results are put in BUFP (if it returns an error, the
1023 - contents of BUFP are undefined):
1024 - `buffer' is the compiled pattern;
1025 - `syntax' is set to SYNTAX;
1026 - `used' is set to the length of the compiled pattern;
1027 - `fastmap_accurate' is zero;
1028 - `re_nsub' is the number of subexpressions in PATTERN;
1029 - `not_bol' and `not_eol' are zero;
1030 -
1031 - The `fastmap' and `newline_anchor' fields are neither
1032 - examined nor set. */
1033 -
1034 -static reg_errcode_t
1035 -regex_compile (pattern, size, syntax, bufp)
1036 - const char *pattern;
1037 - int size;
1038 - reg_syntax_t syntax;
1039 - struct re_pattern_buffer *bufp;
1040 -{
1041 - /* We fetch characters from PATTERN here. Even though PATTERN is
1042 - `char *' (i.e., signed), we declare these variables as unsigned, so
1043 - they can be reliably used as array indices. */
1044 - register unsigned char c, c1;
1045 -
1046 - /* A random temporary spot in PATTERN. */
1047 - const char *p1;
1048 -
1049 - /* Points to the end of the buffer, where we should append. */
1050 - register unsigned char *b;
1051 -
1052 - /* Keeps track of unclosed groups. */
1053 - compile_stack_type compile_stack;
1054 -
1055 - /* Points to the current (ending) position in the pattern. */
1056 - const char *p = pattern;
1057 - const char *pend = pattern + size;
1058 -
1059 - /* How to translate the characters in the pattern. */
1060 - char *translate = bufp->translate;
1061 -
1062 - /* Address of the count-byte of the most recently inserted `exactn'
1063 - command. This makes it possible to tell if a new exact-match
1064 - character can be added to that command or if the character requires
1065 - a new `exactn' command. */
1066 - unsigned char *pending_exact = 0;
1067 -
1068 - /* Address of start of the most recently finished expression.
1069 - This tells, e.g., postfix * where to find the start of its
1070 - operand. Reset at the beginning of groups and alternatives. */
1071 - unsigned char *laststart = 0;
1072 -
1073 - /* Address of beginning of regexp, or inside of last group. */
1074 - unsigned char *begalt;
1075 -
1076 - /* Place in the uncompiled pattern (i.e., the {) to
1077 - which to go back if the interval is invalid. */
1078 - const char *beg_interval;
1079 -
1080 - /* Address of the place where a forward jump should go to the end of
1081 - the containing expression. Each alternative of an `or' -- except the
1082 - last -- ends with a forward jump of this sort. */
1083 - unsigned char *fixup_alt_jump = 0;
1084 -
1085 - /* Counts open-groups as they are encountered. Remembered for the
1086 - matching close-group on the compile stack, so the same register
1087 - number is put in the stop_memory as the start_memory. */
1088 - regnum_t regnum = 0;
1089 -
1090 -#ifdef DEBUG
1091 - DEBUG_PRINT1 ("\nCompiling pattern: ");
1092 - if (debug)
1093 - {
1094 - unsigned debug_count;
1095 -
1096 - for (debug_count = 0; debug_count < size; debug_count++)
1097 - printchar (pattern[debug_count]);
1098 - putchar ('\n');
1099 - }
1100 -#endif /* DEBUG */
1101 -
1102 - /* Initialize the compile stack. */
1103 - compile_stack.stack = TALLOC (INIT_COMPILE_STACK_SIZE, compile_stack_elt_t);
1104 - if (compile_stack.stack == NULL)
1105 - return REG_ESPACE;
1106 -
1107 - compile_stack.size = INIT_COMPILE_STACK_SIZE;
1108 - compile_stack.avail = 0;
1109 -
1110 - /* Initialize the pattern buffer. */
1111 - bufp->syntax = syntax;
1112 - bufp->fastmap_accurate = 0;
1113 - bufp->not_bol = bufp->not_eol = 0;
1114 -
1115 - /* Set `used' to zero, so that if we return an error, the pattern
1116 - printer (for debugging) will think there's no pattern. We reset it
1117 - at the end. */
1118 - bufp->used = 0;
1119 -
1120 - /* Always count groups, whether or not bufp->no_sub is set. */
1121 - bufp->re_nsub = 0;
1122 -
1123 -#if !defined (emacs) && !defined (SYNTAX_TABLE)
1124 - /* Initialize the syntax table. */
1125 - init_syntax_once ();
13 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 + Lesser General Public License for more details.
15 +
16 + You should have received a copy of the GNU Lesser General Public
17 + License along with the GNU C Library; if not, write to the Free
18 + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 + 02110-1301 USA. */
20 +
21 +#ifdef HAVE_CONFIG_H
22 +#include "config.h"
23 +#endif
24 +
25 +/* Make sure noone compiles this code with a C++ compiler. */
26 +#ifdef __cplusplus
27 +# error "This is C code, use a C compiler"
28 +#endif
29 +
30 +#ifdef _LIBC
31 +/* We have to keep the namespace clean. */
32 +# define regfree(preg) __regfree (preg)
33 +# define regexec(pr, st, nm, pm, ef) __regexec (pr, st, nm, pm, ef)
34 +# define regcomp(preg, pattern, cflags) __regcomp (preg, pattern, cflags)
35 +# define regerror(errcode, preg, errbuf, errbuf_size) \
36 + __regerror(errcode, preg, errbuf, errbuf_size)
37 +# define re_set_registers(bu, re, nu, st, en) \
38 + __re_set_registers (bu, re, nu, st, en)
39 +# define re_match_2(bufp, string1, size1, string2, size2, pos, regs, stop) \
40 + __re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
41 +# define re_match(bufp, string, size, pos, regs) \
42 + __re_match (bufp, string, size, pos, regs)
43 +# define re_search(bufp, string, size, startpos, range, regs) \
44 + __re_search (bufp, string, size, startpos, range, regs)
45 +# define re_compile_pattern(pattern, length, bufp) \
46 + __re_compile_pattern (pattern, length, bufp)
47 +# define re_set_syntax(syntax) __re_set_syntax (syntax)
48 +# define re_search_2(bufp, st1, s1, st2, s2, startpos, range, regs, stop) \
49 + __re_search_2 (bufp, st1, s1, st2, s2, startpos, range, regs, stop)
50 +# define re_compile_fastmap(bufp) __re_compile_fastmap (bufp)
51 +
52 +# include "../locale/localeinfo.h"
53 +#endif
54 +
55 +#if defined (_MSC_VER)
56 +#include <stdio.h> /* for size_t */
57 +#endif
58 +
59 +/* On some systems, limits.h sets RE_DUP_MAX to a lower value than
60 + GNU regex allows. Include it before <regex.h>, which correctly
61 + #undefs RE_DUP_MAX and sets it to the right value. */
62 +#include <limits.h>
63 +
64 +#ifdef GAWK
65 +#undef alloca
66 +#define alloca alloca_is_bad_you_should_never_use_it
67 +#endif
68 +#include <regex.h>
69 +#include "regex_internal.h"
70 +
71 +#include "regex_internal.c"
72 +#ifdef GAWK
73 +#define bool int
74 +#define true (1)
75 +#define false (0)
76 +#endif
77 +#include "regcomp.c"
78 +#include "regexec.c"
79 +
80 +/* Binary backward compatibility. */
81 +#if _LIBC
82 +# include <shlib-compat.h>
83 +# if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_3)
84 +link_warning (re_max_failures, "the 're_max_failures' variable is obsolete and will go away.")
85 +int re_max_failures = 2000;
86 +# endif
87 #endif
1127 -
1128 - if (bufp->allocated == 0)
1129 - {
1130 - if (bufp->buffer)
1131 - { /* If zero allocated, but buffer is non-null, try to realloc
1132 - enough space. This loses if buffer's address is bogus, but
1133 - that is the user's responsibility. */
1134 - RETALLOC (bufp->buffer, INIT_BUF_SIZE, unsigned char);
1135 - }
1136 - else
1137 - { /* Caller did not allocate a buffer. Do it for them. */
1138 - bufp->buffer = TALLOC (INIT_BUF_SIZE, unsigned char);
1139 - }
1140 - if (!bufp->buffer) return REG_ESPACE;
1141 -
1142 - bufp->allocated = INIT_BUF_SIZE;
1143 - }
1144 -
1145 - begalt = b = bufp->buffer;
1146 -
1147 - /* Loop through the uncompiled pattern until we're at the end. */
1148 - while (p != pend)
1149 - {
1150 - PATFETCH (c);
1151 -
1152 - switch (c)
1153 - {
1154 - case '^':
1155 - {
1156 - if ( /* If at start of pattern, it's an operator. */
1157 - p == pattern + 1
1158 - /* If context independent, it's an operator. */
1159 - || syntax & RE_CONTEXT_INDEP_ANCHORS
1160 - /* Otherwise, depends on what's come before. */
1161 - || at_begline_loc_p (pattern, p, syntax))
1162 - BUF_PUSH (begline);
1163 - else
1164 - goto normal_char;
1165 - }
1166 - break;
1167 -
1168 -
1169 - case '$':
1170 - {
1171 - if ( /* If at end of pattern, it's an operator. */
1172 - p == pend
1173 - /* If context independent, it's an operator. */
1174 - || syntax & RE_CONTEXT_INDEP_ANCHORS
1175 - /* Otherwise, depends on what's next. */
1176 - || at_endline_loc_p (p, pend, syntax))
1177 - BUF_PUSH (endline);
1178 - else
1179 - goto normal_char;
1180 - }
1181 - break;
1182 -
1183 -
1184 - case '+':
1185 - case '?':
1186 - if ((syntax & RE_BK_PLUS_QM)
1187 - || (syntax & RE_LIMITED_OPS))
1188 - goto normal_char;
1189 - handle_plus:
1190 - case '*':
1191 - /* If there is no previous pattern... */
1192 - if (!laststart)
1193 - {
1194 - if (syntax & RE_CONTEXT_INVALID_OPS)
1195 - return REG_BADRPT;
1196 - else if (!(syntax & RE_CONTEXT_INDEP_OPS))
1197 - goto normal_char;
1198 - }
1199 -
1200 - {
1201 - /* Are we optimizing this jump? */
1202 - boolean keep_string_p = false;
1203 -
1204 - /* 1 means zero (many) matches is allowed. */
1205 - char zero_times_ok = 0, many_times_ok = 0;
1206 -
1207 - /* If there is a sequence of repetition chars, collapse it
1208 - down to just one (the right one). We can't combine
1209 - interval operators with these because of, e.g., `a{2}*',
1210 - which should only match an even number of `a's. */
1211 -
1212 - for (;;)
1213 - {
1214 - zero_times_ok |= c != '+';
1215 - many_times_ok |= c != '?';
1216 -
1217 - if (p == pend)
1218 - break;
1219 -
1220 - PATFETCH (c);
1221 -
1222 - if (c == '*'
1223 - || (!(syntax & RE_BK_PLUS_QM) && (c == '+' || c == '?')))
1224 - ;
1225 -
1226 - else if (syntax & RE_BK_PLUS_QM && c == '\\')
1227 - {
1228 - if (p == pend) return REG_EESCAPE;
1229 -
1230 - PATFETCH (c1);
1231 - if (!(c1 == '+' || c1 == '?'))
1232 - {
1233 - PATUNFETCH;
1234 - PATUNFETCH;
1235 - break;
1236 - }
1237 -
1238 - c = c1;
1239 - }
1240 - else
1241 - {
1242 - PATUNFETCH;
1243 - break;
1244 - }
1245 -
1246 - /* If we get here, we found another repeat character. */
1247 - }
1248 -
1249 - /* Star, etc. applied to an empty pattern is equivalent
1250 - to an empty pattern. */
1251 - if (!laststart)
1252 - break;
1253 -
1254 - /* Now we know whether or not zero matches is allowed
1255 - and also whether or not two or more matches is allowed. */
1256 - if (many_times_ok)
1257 - { /* More than one repetition is allowed, so put in at the
1258 - end a backward relative jump from `b' to before the next
1259 - jump we're going to put in below (which jumps from
1260 - laststart to after this jump).
1261 -
1262 - But if we are at the `*' in the exact sequence `.*\n',
1263 - insert an unconditional jump backwards to the .,
1264 - instead of the beginning of the loop. This way we only
1265 - push a failure point once, instead of every time
1266 - through the loop. */
1267 - assert (p - 1 > pattern);
1268 -
1269 - /* Allocate the space for the jump. */
1270 - GET_BUFFER_SPACE (3);
1271 -
1272 - /* We know we are not at the first character of the pattern,
1273 - because laststart was nonzero. And we've already
1274 - incremented `p', by the way, to be the character after
1275 - the `*'. Do we have to do something analogous here
1276 - for null bytes, because of RE_DOT_NOT_NULL? */
1277 - if (TRANSLATE (*(p - 2)) == TRANSLATE ('.')
1278 - && zero_times_ok
1279 - && p < pend && TRANSLATE (*p) == TRANSLATE ('\n')
1280 - && !(syntax & RE_DOT_NEWLINE))
1281 - { /* We have .*\n. */
1282 - STORE_JUMP (jump, b, laststart);
1283 - keep_string_p = true;
1284 - }
1285 - else
1286 - /* Anything else. */
1287 - STORE_JUMP (maybe_pop_jump, b, laststart - 3);
1288 -
1289 - /* We've added more stuff to the buffer. */
1290 - b += 3;
1291 - }
1292 -
1293 - /* On failure, jump from laststart to b + 3, which will be the
1294 - end of the buffer after this jump is inserted. */
1295 - GET_BUFFER_SPACE (3);
1296 - INSERT_JUMP (keep_string_p ? on_failure_keep_string_jump
1297 - : on_failure_jump,
1298 - laststart, b + 3);
1299 - pending_exact = 0;
1300 - b += 3;
1301 -
1302 - if (!zero_times_ok)
1303 - {
1304 - /* At least one repetition is required, so insert a
1305 - `dummy_failure_jump' before the initial
1306 - `on_failure_jump' instruction of the loop. This
1307 - effects a skip over that instruction the first time
1308 - we hit that loop. */
1309 - GET_BUFFER_SPACE (3);
1310 - INSERT_JUMP (dummy_failure_jump, laststart, laststart + 6);
1311 - b += 3;
1312 - }
1313 - }
1314 - break;
1315 -
1316 -
1317 - case '.':
1318 - laststart = b;
1319 - BUF_PUSH (anychar);
1320 - break;
1321 -
1322 -
1323 - case '[':
1324 - {
1325 - boolean had_char_class = false;
1326 -
1327 - if (p == pend) return REG_EBRACK;
1328 -
1329 - /* Ensure that we have enough space to push a charset: the
1330 - opcode, the length count, and the bitset; 34 bytes in all. */
1331 - GET_BUFFER_SPACE (34);
1332 -
1333 - laststart = b;
1334 -
1335 - /* We test `*p == '^' twice, instead of using an if
1336 - statement, so we only need one BUF_PUSH. */
1337 - BUF_PUSH (*p == '^' ? charset_not : charset);
1338 - if (*p == '^')
1339 - p++;
1340 -
1341 - /* Remember the first position in the bracket expression. */
1342 - p1 = p;
1343 -
1344 - /* Push the number of bytes in the bitmap. */
1345 - BUF_PUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
1346 -
1347 - /* Clear the whole map. */
1348 - bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH);
1349 -
1350 - /* charset_not matches newline according to a syntax bit. */
1351 - if ((re_opcode_t) b[-2] == charset_not
1352 - && (syntax & RE_HAT_LISTS_NOT_NEWLINE))
1353 - SET_LIST_BIT ('\n');
1354 -
1355 - /* Read in characters and ranges, setting map bits. */
1356 - for (;;)
1357 - {
1358 - if (p == pend) return REG_EBRACK;
1359 -
1360 - PATFETCH (c);
1361 -
1362 - /* \ might escape characters inside [...] and [^...]. */
1363 - if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) && c == '\\')
1364 - {
1365 - if (p == pend) return REG_EESCAPE;
1366 -
1367 - PATFETCH (c1);
1368 - SET_LIST_BIT (c1);
1369 - continue;
1370 - }
1371 -
1372 - /* Could be the end of the bracket expression. If it's
1373 - not (i.e., when the bracket expression is `[]' so
1374 - far), the ']' character bit gets set way below. */
1375 - if (c == ']' && p != p1 + 1)
1376 - break;
1377 -
1378 - /* Look ahead to see if it's a range when the last thing
1379 - was a character class. */
1380 - if (had_char_class && c == '-' && *p != ']')
1381 - return REG_ERANGE;
1382 -
1383 - /* Look ahead to see if it's a range when the last thing
1384 - was a character: if this is a hyphen not at the
1385 - beginning or the end of a list, then it's the range
1386 - operator. */
1387 - if (c == '-'
1388 - && !(p - 2 >= pattern && p[-2] == '[')
1389 - && !(p - 3 >= pattern && p[-3] == '[' && p[-2] == '^')
1390 - && *p != ']')
1391 - {
1392 - reg_errcode_t ret
1393 - = compile_range (&p, pend, translate, syntax, b);
1394 - if (ret != REG_NOERROR) return ret;
1395 - }
1396 -
1397 - else if (p[0] == '-' && p[1] != ']')
1398 - { /* This handles ranges made up of characters only. */
1399 - reg_errcode_t ret;
1400 -
1401 - /* Move past the `-'. */
1402 - PATFETCH (c1);
1403 -
1404 - ret = compile_range (&p, pend, translate, syntax, b);
1405 - if (ret != REG_NOERROR) return ret;
1406 - }
1407 -
1408 - /* See if we're at the beginning of a possible character
1409 - class. */
1410 -
1411 - else if (syntax & RE_CHAR_CLASSES && c == '[' && *p == ':')
1412 - { /* Leave room for the null. */
1413 - char str[CHAR_CLASS_MAX_LENGTH + 1];
1414 -
1415 - PATFETCH (c);
1416 - c1 = 0;
1417 -
1418 - /* If pattern is `[[:'. */
1419 - if (p == pend) return REG_EBRACK;
1420 -
1421 - for (;;)
1422 - {
1423 - PATFETCH (c);
1424 - if (c == ':' || c == ']' || p == pend
1425 - || c1 == CHAR_CLASS_MAX_LENGTH)
1426 - break;
1427 - str[c1++] = c;
1428 - }
1429 - str[c1] = '\0';
1430 -
1431 - /* If isn't a word bracketed by `[:' and:`]':
1432 - undo the ending character, the letters, and leave
1433 - the leading `:' and `[' (but set bits for them). */
1434 - if (c == ':' && *p == ']')
1435 - {
1436 - int ch;
1437 - boolean is_alnum = STREQ (str, "alnum");
1438 - boolean is_alpha = STREQ (str, "alpha");
1439 - boolean is_blank = STREQ (str, "blank");
1440 - boolean is_cntrl = STREQ (str, "cntrl");
1441 - boolean is_digit = STREQ (str, "digit");
1442 - boolean is_graph = STREQ (str, "graph");
1443 - boolean is_lower = STREQ (str, "lower");
1444 - boolean is_print = STREQ (str, "print");
1445 - boolean is_punct = STREQ (str, "punct");
1446 - boolean is_space = STREQ (str, "space");
1447 - boolean is_upper = STREQ (str, "upper");
1448 - boolean is_xdigit = STREQ (str, "xdigit");
1449 -
1450 - if (!IS_CHAR_CLASS (str)) return REG_ECTYPE;
1451 -
1452 - /* Throw away the ] at the end of the character
1453 - class. */
1454 - PATFETCH (c);
1455 -
1456 - if (p == pend) return REG_EBRACK;
1457 -
1458 - for (ch = 0; ch < 1 << BYTEWIDTH; ch++)
1459 - {
1460 - if ( (is_alnum && ISALNUM (ch))
1461 - || (is_alpha && ISALPHA (ch))
1462 - || (is_blank && ISBLANK (ch))
1463 - || (is_cntrl && ISCNTRL (ch))
1464 - || (is_digit && ISDIGIT (ch))
1465 - || (is_graph && ISGRAPH (ch))
1466 - || (is_lower && ISLOWER (ch))
1467 - || (is_print && ISPRINT (ch))
1468 - || (is_punct && ISPUNCT (ch))
1469 - || (is_space && ISSPACE (ch))
1470 - || (is_upper && ISUPPER (ch))
1471 - || (is_xdigit && ISXDIGIT (ch)))
1472 - SET_LIST_BIT (ch);
1473 - }
1474 - had_char_class = true;
1475 - }
1476 - else
1477 - {
1478 - c1++;
1479 - while (c1--)
1480 - PATUNFETCH;
1481 - SET_LIST_BIT ('[');
1482 - SET_LIST_BIT (':');
1483 - had_char_class = false;
1484 - }
1485 - }
1486 - else
1487 - {
1488 - had_char_class = false;
1489 - SET_LIST_BIT (c);
1490 - }
1491 - }
1492 -
1493 - /* Discard any (non)matching list bytes that are all 0 at the
1494 - end of the map. Decrease the map-length byte too. */
1495 - while ((int) b[-1] > 0 && b[b[-1] - 1] == 0)
1496 - b[-1]--;
1497 - b += b[-1];
1498 - }
1499 - break;
1500 -
1501 -
1502 - case '(':
1503 - if (syntax & RE_NO_BK_PARENS)
1504 - goto handle_open;
1505 - else
1506 - goto normal_char;
1507 -
1508 -
1509 - case ')':
1510 - if (syntax & RE_NO_BK_PARENS)
1511 - goto handle_close;
1512 - else
1513 - goto normal_char;
1514 -
1515 -
1516 - case '\n':
1517 - if (syntax & RE_NEWLINE_ALT)
1518 - goto handle_alt;
1519 - else
1520 - goto normal_char;
1521 -
1522 -
1523 - case '|':
1524 - if (syntax & RE_NO_BK_VBAR)
1525 - goto handle_alt;
1526 - else
1527 - goto normal_char;
1528 -
1529 -
1530 - case '{':
1531 - if (syntax & RE_INTERVALS && syntax & RE_NO_BK_BRACES)
1532 - goto handle_interval;
1533 - else
1534 - goto normal_char;
1535 -
1536 -
1537 - case '\\':
1538 - if (p == pend) return REG_EESCAPE;
1539 -
1540 - /* Do not translate the character after the \, so that we can
1541 - distinguish, e.g., \B from \b, even if we normally would
1542 - translate, e.g., B to b. */
1543 - PATFETCH_RAW (c);
1544 -
1545 - switch (c)
1546 - {
1547 - case '(':
1548 - if (syntax & RE_NO_BK_PARENS)
1549 - goto normal_backslash;
1550 -
1551 - handle_open:
1552 - bufp->re_nsub++;
1553 - regnum++;
1554 -
1555 - if (COMPILE_STACK_FULL)
1556 - {
1557 - RETALLOC (compile_stack.stack, compile_stack.size << 1,
1558 - compile_stack_elt_t);
1559 - if (compile_stack.stack == NULL) return REG_ESPACE;
1560 -
1561 - compile_stack.size <<= 1;
1562 - }
1563 -
1564 - /* These are the values to restore when we hit end of this
1565 - group. They are all relative offsets, so that if the
1566 - whole pattern moves because of realloc, they will still
1567 - be valid. */
1568 - COMPILE_STACK_TOP.begalt_offset = begalt - bufp->buffer;
1569 - COMPILE_STACK_TOP.fixup_alt_jump
1570 - = fixup_alt_jump ? fixup_alt_jump - bufp->buffer + 1 : 0;
1571 - COMPILE_STACK_TOP.laststart_offset = b - bufp->buffer;
1572 - COMPILE_STACK_TOP.regnum = regnum;
1573 -
1574 - /* We will eventually replace the 0 with the number of
1575 - groups inner to this one. But do not push a
1576 - start_memory for groups beyond the last one we can
1577 - represent in the compiled pattern. */
1578 - if (regnum <= MAX_REGNUM)
1579 - {
1580 - COMPILE_STACK_TOP.inner_group_offset = b - bufp->buffer + 2;
1581 - BUF_PUSH_3 (start_memory, regnum, 0);
1582 - }
1583 -
1584 - compile_stack.avail++;
1585 -
1586 - fixup_alt_jump = 0;
1587 - laststart = 0;
1588 - begalt = b;
1589 - /* If we've reached MAX_REGNUM groups, then this open
1590 - won't actually generate any code, so we'll have to
1591 - clear pending_exact explicitly. */
1592 - pending_exact = 0;
1593 - break;
1594 -
1595 -
1596 - case ')':
1597 - if (syntax & RE_NO_BK_PARENS) goto normal_backslash;
1598 -
1599 - if (COMPILE_STACK_EMPTY)
1600 - {
1601 - if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
1602 - goto normal_backslash;
1603 - else
1604 - return REG_ERPAREN;
1605 - }
1606 -
1607 - handle_close:
1608 - if (fixup_alt_jump)
1609 - { /* Push a dummy failure point at the end of the
1610 - alternative for a possible future
1611 - `pop_failure_jump' to pop. See comments at
1612 - `push_dummy_failure' in `re_match_2'. */
1613 - BUF_PUSH (push_dummy_failure);
1614 -
1615 - /* We allocated space for this jump when we assigned
1616 - to `fixup_alt_jump', in the `handle_alt' case below. */
1617 - STORE_JUMP (jump_past_alt, fixup_alt_jump, b - 1);
1618 - }
1619 -
1620 - /* See similar code for backslashed left paren above. */
1621 - if (COMPILE_STACK_EMPTY)
1622 - {
1623 - if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
1624 - goto normal_char;
1625 - else
1626 - return REG_ERPAREN;
1627 - }
1628 -
1629 - /* Since we just checked for an empty stack above, this
1630 - ``can't happen''. */
1631 - assert (compile_stack.avail != 0);
1632 - {
1633 - /* We don't just want to restore into `regnum', because
1634 - later groups should continue to be numbered higher,
1635 - as in `(ab)c(de)' -- the second group is #2. */
1636 - regnum_t this_group_regnum;
1637 -
1638 - compile_stack.avail--;
1639 - begalt = bufp->buffer + COMPILE_STACK_TOP.begalt_offset;
1640 - fixup_alt_jump
1641 - = COMPILE_STACK_TOP.fixup_alt_jump
1642 - ? bufp->buffer + COMPILE_STACK_TOP.fixup_alt_jump - 1
1643 - : 0;
1644 - laststart = bufp->buffer + COMPILE_STACK_TOP.laststart_offset;
1645 - this_group_regnum = COMPILE_STACK_TOP.regnum;
1646 - /* If we've reached MAX_REGNUM groups, then this open
1647 - won't actually generate any code, so we'll have to
1648 - clear pending_exact explicitly. */
1649 - pending_exact = 0;
1650 -
1651 - /* We're at the end of the group, so now we know how many
1652 - groups were inside this one. */
1653 - if (this_group_regnum <= MAX_REGNUM)
1654 - {
1655 - unsigned char *inner_group_loc
1656 - = bufp->buffer + COMPILE_STACK_TOP.inner_group_offset;
1657 -
1658 - *inner_group_loc = regnum - this_group_regnum;
1659 - BUF_PUSH_3 (stop_memory, this_group_regnum,
1660 - regnum - this_group_regnum);
1661 - }
1662 - }
1663 - break;
1664 -
1665 -
1666 - case '|': /* `\|'. */
1667 - if (syntax & RE_LIMITED_OPS || syntax & RE_NO_BK_VBAR)
1668 - goto normal_backslash;
1669 - handle_alt:
1670 - if (syntax & RE_LIMITED_OPS)
1671 - goto normal_char;
1672 -
1673 - /* Insert before the previous alternative a jump which
1674 - jumps to this alternative if the former fails. */
1675 - GET_BUFFER_SPACE (3);
1676 - INSERT_JUMP (on_failure_jump, begalt, b + 6);
1677 - pending_exact = 0;
1678 - b += 3;
1679 -
1680 - /* The alternative before this one has a jump after it
1681 - which gets executed if it gets matched. Adjust that
1682 - jump so it will jump to this alternative's analogous
1683 - jump (put in below, which in turn will jump to the next
1684 - (if any) alternative's such jump, etc.). The last such
1685 - jump jumps to the correct final destination. A picture:
1686 - _____ _____
1687 - | | | |
1688 - | v | v
1689 - a | b | c
1690 -
1691 - If we are at `b', then fixup_alt_jump right now points to a
1692 - three-byte space after `a'. We'll put in the jump, set
1693 - fixup_alt_jump to right after `b', and leave behind three
1694 - bytes which we'll fill in when we get to after `c'. */
1695 -
1696 - if (fixup_alt_jump)
1697 - STORE_JUMP (jump_past_alt, fixup_alt_jump, b);
1698 -
1699 - /* Mark and leave space for a jump after this alternative,
1700 - to be filled in later either by next alternative or
1701 - when know we're at the end of a series of alternatives. */
1702 - fixup_alt_jump = b;
1703 - GET_BUFFER_SPACE (3);
1704 - b += 3;
1705 -
1706 - laststart = 0;
1707 - begalt = b;
1708 - break;
1709 -
1710 -
1711 - case '{':
1712 - /* If \{ is a literal. */
1713 - if (!(syntax & RE_INTERVALS)
1714 - /* If we're at `\{' and it's not the open-interval
1715 - operator. */
1716 - || ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
1717 - || (p - 2 == pattern && p == pend))
1718 - goto normal_backslash;
1719 -
1720 - handle_interval:
1721 - {
1722 - /* If got here, then the syntax allows intervals. */
1723 -
1724 - /* At least (most) this many matches must be made. */
1725 - int lower_bound = -1, upper_bound = -1;
1726 -
1727 - beg_interval = p - 1;
1728 -
1729 - if (p == pend)
1730 - {
1731 - if (syntax & RE_NO_BK_BRACES)
1732 - goto unfetch_interval;
1733 - else
1734 - return REG_EBRACE;
1735 - }
1736 -
1737 - GET_UNSIGNED_NUMBER (lower_bound);
1738 -
1739 - if (c == ',')
1740 - {
1741 - GET_UNSIGNED_NUMBER (upper_bound);
1742 - if (upper_bound < 0) upper_bound = RE_DUP_MAX;
1743 - }
1744 - else
1745 - /* Interval such as `{1}' => match exactly once. */
1746 - upper_bound = lower_bound;
1747 -
1748 - if (lower_bound < 0 || upper_bound > RE_DUP_MAX
1749 - || lower_bound > upper_bound)
1750 - {
1751 - if (syntax & RE_NO_BK_BRACES)
1752 - goto unfetch_interval;
1753 - else
1754 - return REG_BADBR;
1755 - }
1756 -
1757 - if (!(syntax & RE_NO_BK_BRACES))
1758 - {
1759 - if (c != '\\') return REG_EBRACE;
1760 -
1761 - PATFETCH (c);
1762 - }
1763 -
1764 - if (c != '}')
1765 - {
1766 - if (syntax & RE_NO_BK_BRACES)
1767 - goto unfetch_interval;
1768 - else
1769 - return REG_BADBR;
1770 - }
1771 -
1772 - /* We just parsed a valid interval. */
1773 -
1774 - /* If it's invalid to have no preceding re. */
1775 - if (!laststart)
1776 - {
1777 - if (syntax & RE_CONTEXT_INVALID_OPS)
1778 - return REG_BADRPT;
1779 - else if (syntax & RE_CONTEXT_INDEP_OPS)
1780 - laststart = b;
1781 - else
1782 - goto unfetch_interval;
1783 - }
1784 -
1785 - /* If the upper bound is zero, don't want to succeed at
1786 - all; jump from `laststart' to `b + 3', which will be
1787 - the end of the buffer after we insert the jump. */
1788 - if (upper_bound == 0)
1789 - {
1790 - GET_BUFFER_SPACE (3);
1791 - INSERT_JUMP (jump, laststart, b + 3);
1792 - b += 3;
1793 - }
1794 -
1795 - /* Otherwise, we have a nontrivial interval. When
1796 - we're all done, the pattern will look like:
1797 - set_number_at <jump count> <upper bound>
1798 - set_number_at <succeed_n count> <lower bound>
1799 - succeed_n <after jump addr> <succeed_n count>
1800 - <body of loop>
1801 - jump_n <succeed_n addr> <jump count>
1802 - (The upper bound and `jump_n' are omitted if
1803 - `upper_bound' is 1, though.) */
1804 - else
1805 - { /* If the upper bound is > 1, we need to insert
1806 - more at the end of the loop. */
1807 - unsigned nbytes = 10 + (upper_bound > 1) * 10;
1808 -
1809 - GET_BUFFER_SPACE (nbytes);
1810 -
1811 - /* Initialize lower bound of the `succeed_n', even
1812 - though it will be set during matching by its
1813 - attendant `set_number_at' (inserted next),
1814 - because `re_compile_fastmap' needs to know.
1815 - Jump to the `jump_n' we might insert below. */
1816 - INSERT_JUMP2 (succeed_n, laststart,
1817 - b + 5 + (upper_bound > 1) * 5,
1818 - lower_bound);
1819 - b += 5;
1820 -
1821 - /* Code to initialize the lower bound. Insert
1822 - before the `succeed_n'. The `5' is the last two
1823 - bytes of this `set_number_at', plus 3 bytes of
1824 - the following `succeed_n'. */
1825 - insert_op2 (set_number_at, laststart, 5, lower_bound, b);
1826 - b += 5;
1827 -
1828 - if (upper_bound > 1)
1829 - { /* More than one repetition is allowed, so
1830 - append a backward jump to the `succeed_n'
1831 - that starts this interval.
1832 -
1833 - When we've reached this during matching,
1834 - we'll have matched the interval once, so
1835 - jump back only `upper_bound - 1' times. */
1836 - STORE_JUMP2 (jump_n, b, laststart + 5,
1837 - upper_bound - 1);
1838 - b += 5;
1839 -
1840 - /* The location we want to set is the second
1841 - parameter of the `jump_n'; that is `b-2' as
1842 - an absolute address. `laststart' will be
1843 - the `set_number_at' we're about to insert;
1844 - `laststart+3' the number to set, the source
1845 - for the relative address. But we are
1846 - inserting into the middle of the pattern --
1847 - so everything is getting moved up by 5.
1848 - Conclusion: (b - 2) - (laststart + 3) + 5,
1849 - i.e., b - laststart.
1850 -
1851 - We insert this at the beginning of the loop
1852 - so that if we fail during matching, we'll
1853 - reinitialize the bounds. */
1854 - insert_op2 (set_number_at, laststart, b - laststart,
1855 - upper_bound - 1, b);
1856 - b += 5;
1857 - }
1858 - }
1859 - pending_exact = 0;
1860 - beg_interval = NULL;
1861 - }
1862 - break;
1863 -
1864 - unfetch_interval:
1865 - /* If an invalid interval, match the characters as literals. */
1866 - assert (beg_interval);
1867 - p = beg_interval;
1868 - beg_interval = NULL;
1869 -
1870 - /* normal_char and normal_backslash need `c'. */
1871 - PATFETCH (c);
1872 -
1873 - if (!(syntax & RE_NO_BK_BRACES))
1874 - {
1875 - if (p > pattern && p[-1] == '\\')
1876 - goto normal_backslash;
1877 - }
1878 - goto normal_char;
1879 -
1880 -#ifdef emacs
1881 - /* There is no way to specify the before_dot and after_dot
1882 - operators. rms says this is ok. --karl */
1883 - case '=':
1884 - BUF_PUSH (at_dot);
1885 - break;
1886 -
1887 - case 's':
1888 - laststart = b;
1889 - PATFETCH (c);
1890 - BUF_PUSH_2 (syntaxspec, syntax_spec_code[c]);
1891 - break;
1892 -
1893 - case 'S':
1894 - laststart = b;
1895 - PATFETCH (c);
1896 - BUF_PUSH_2 (notsyntaxspec, syntax_spec_code[c]);
1897 - break;
1898 -#endif /* emacs */
1899 -
1900 -
1901 - case 'w':
1902 - laststart = b;
1903 - BUF_PUSH (wordchar);
1904 - break;
1905 -
1906 -
1907 - case 'W':
1908 - laststart = b;
1909 - BUF_PUSH (notwordchar);
1910 - break;
1911 -
1912 -
1913 - case '<':
1914 - BUF_PUSH (wordbeg);
1915 - break;
1916 -
1917 - case '>':
1918 - BUF_PUSH (wordend);
1919 - break;
1920 -
1921 - case 'b':
1922 - BUF_PUSH (wordbound);
1923 - break;
1924 -
1925 - case 'B':
1926 - BUF_PUSH (notwordbound);
1927 - break;
1928 -
1929 - case '`':
1930 - BUF_PUSH (begbuf);
1931 - break;
1932 -
1933 - case '\'':
1934 - BUF_PUSH (endbuf);
1935 - break;
1936 -
1937 - case '1': case '2': case '3': case '4': case '5':
1938 - case '6': case '7': case '8': case '9':
1939 - if (syntax & RE_NO_BK_REFS)
1940 - goto normal_char;
1941 -
1942 - c1 = c - '0';
1943 -
1944 - if (c1 > regnum)
1945 - return REG_ESUBREG;
1946 -
1947 - /* Can't back reference to a subexpression if inside of it. */
1948 - if (group_in_compile_stack (compile_stack, c1))
1949 - goto normal_char;
1950 -
1951 - laststart = b;
1952 - BUF_PUSH_2 (duplicate, c1);
1953 - break;
1954 -
1955 -
1956 - case '+':
1957 - case '?':
1958 - if (syntax & RE_BK_PLUS_QM)
1959 - goto handle_plus;
1960 - else
1961 - goto normal_backslash;
1962 -
1963 - default:
1964 - normal_backslash:
1965 - /* You might think it would be useful for \ to mean
1966 - not to translate; but if we don't translate it
1967 - it will never match anything. */
1968 - c = TRANSLATE (c);
1969 - goto normal_char;
1970 - }
1971 - break;
1972 -
1973 -
1974 - default:
1975 - /* Expects the character in `c'. */
1976 - normal_char:
1977 - /* If no exactn currently being built. */
1978 - if (!pending_exact
1979 -
1980 - /* If last exactn not at current position. */
1981 - || pending_exact + *pending_exact + 1 != b
1982 -
1983 - /* We have only one byte following the exactn for the count. */
1984 - || *pending_exact == (1 << BYTEWIDTH) - 1
1985 -
1986 - /* If followed by a repetition operator. */
1987 - || *p == '*' || *p == '^'
1988 - || ((syntax & RE_BK_PLUS_QM)
1989 - ? *p == '\\' && (p[1] == '+' || p[1] == '?')
1990 - : (*p == '+' || *p == '?'))
1991 - || ((syntax & RE_INTERVALS)
1992 - && ((syntax & RE_NO_BK_BRACES)
1993 - ? *p == '{'
1994 - : (p[0] == '\\' && p[1] == '{'))))
1995 - {
1996 - /* Start building a new exactn. */
1997 -
1998 - laststart = b;
1999 -
2000 - BUF_PUSH_2 (exactn, 0);
2001 - pending_exact = b - 1;
2002 - }
2003 -
2004 - BUF_PUSH (c);
2005 - (*pending_exact)++;
2006 - break;
2007 - } /* switch (c) */
2008 - } /* while p != pend */
2009 -
2010 -
2011 - /* Through the pattern now. */
2012 -
2013 - if (fixup_alt_jump)
2014 - STORE_JUMP (jump_past_alt, fixup_alt_jump, b);
2015 -
2016 - if (!COMPILE_STACK_EMPTY)
2017 - return REG_EPAREN;
2018 -
2019 - free (compile_stack.stack);
2020 -
2021 - /* We have succeeded; set the length of the buffer. */
2022 - bufp->used = b - bufp->buffer;
2023 -
2024 -#ifdef DEBUG
2025 - if (debug)
2026 - {
2027 - DEBUG_PRINT1 ("\nCompiled pattern: ");
2028 - print_compiled_pattern (bufp);
2029 - }
2030 -#endif /* DEBUG */
2031 -
2032 - return REG_NOERROR;
2033 -} /* regex_compile */
2034 -
2035 -/* Subroutines for `regex_compile'. */
2036 -
2037 -/* Store OP at LOC followed by two-byte integer parameter ARG. */
2038 -
2039 -static void
2040 -store_op1 (op, loc, arg)
2041 - re_opcode_t op;
2042 - unsigned char *loc;
2043 - int arg;
2044 -{
2045 - *loc = (unsigned char) op;
2046 - STORE_NUMBER (loc + 1, arg);
2047 -}
2048 -
2049 -
2050 -/* Like `store_op1', but for two two-byte parameters ARG1 and ARG2. */
2051 -
2052 -static void
2053 -store_op2 (op, loc, arg1, arg2)
2054 - re_opcode_t op;
2055 - unsigned char *loc;
2056 - int arg1, arg2;
2057 -{
2058 - *loc = (unsigned char) op;
2059 - STORE_NUMBER (loc + 1, arg1);
2060 - STORE_NUMBER (loc + 3, arg2);
2061 -}
2062 -
2063 -
2064 -/* Copy the bytes from LOC to END to open up three bytes of space at LOC
2065 - for OP followed by two-byte integer parameter ARG. */
2066 -
2067 -static void
2068 -insert_op1 (op, loc, arg, end)
2069 - re_opcode_t op;
2070 - unsigned char *loc;
2071 - int arg;
2072 - unsigned char *end;
2073 -{
2074 - register unsigned char *pfrom = end;
2075 - register unsigned char *pto = end + 3;
2076 -
2077 - while (pfrom != loc)
2078 - *--pto = *--pfrom;
2079 -
2080 - store_op1 (op, loc, arg);
2081 -}
2082 -
2083 -
2084 -/* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2. */
2085 -
2086 -static void
2087 -insert_op2 (op, loc, arg1, arg2, end)
2088 - re_opcode_t op;
2089 - unsigned char *loc;
2090 - int arg1, arg2;
2091 - unsigned char *end;
2092 -{
2093 - register unsigned char *pfrom = end;
2094 - register unsigned char *pto = end + 5;
2095 -
2096 - while (pfrom != loc)
2097 - *--pto = *--pfrom;
2098 -
2099 - store_op2 (op, loc, arg1, arg2);
2100 -}
2101 -
2102 -
2103 -/* P points to just after a ^ in PATTERN. Return true if that ^ comes
2104 - after an alternative or a begin-subexpression. We assume there is at
2105 - least one character before the ^. */
2106 -
2107 -static boolean
2108 -at_begline_loc_p (pattern, p, syntax)
2109 - const char *pattern, *p;
2110 - reg_syntax_t syntax;
2111 -{
2112 - const char *prev = p - 2;
2113 - boolean prev_prev_backslash = prev > pattern && prev[-1] == '\\';
2114 -
2115 - return
2116 - /* After a subexpression? */
2117 - (*prev == '(' && (syntax & RE_NO_BK_PARENS || prev_prev_backslash))
2118 - /* After an alternative? */
2119 - || (*prev == '|' && (syntax & RE_NO_BK_VBAR || prev_prev_backslash));
2120 -}
2121 -
2122 -
2123 -/* The dual of at_begline_loc_p. This one is for $. We assume there is
2124 - at least one character after the $, i.e., `P < PEND'. */
2125 -
2126 -static boolean
2127 -at_endline_loc_p (p, pend, syntax)
2128 - const char *p, *pend;
2129 - int syntax;
2130 -{
2131 - const char *next = p;
2132 - boolean next_backslash = *next == '\\';
2133 - const char *next_next = p + 1 < pend ? p + 1 : NULL;
2134 -
2135 - return
2136 - /* Before a subexpression? */
2137 - (syntax & RE_NO_BK_PARENS ? *next == ')'
2138 - : next_backslash && next_next && *next_next == ')')
2139 - /* Before an alternative? */
2140 - || (syntax & RE_NO_BK_VBAR ? *next == '|'
2141 - : next_backslash && next_next && *next_next == '|');
2142 -}
2143 -
2144 -
2145 -/* Returns true if REGNUM is in one of COMPILE_STACK's elements and
2146 - false if it's not. */
2147 -
2148 -static boolean
2149 -group_in_compile_stack (compile_stack, regnum)
2150 - compile_stack_type compile_stack;
2151 - regnum_t regnum;
2152 -{
2153 - int this_element;
2154 -
2155 - for (this_element = compile_stack.avail - 1;
2156 - this_element >= 0;
2157 - this_element--)
2158 - if (compile_stack.stack[this_element].regnum == regnum)
2159 - return true;
2160 -
2161 - return false;
2162 -}
2163 -
2164 -
2165 -/* Read the ending character of a range (in a bracket expression) from the
2166 - uncompiled pattern *P_PTR (which ends at PEND). We assume the
2167 - starting character is in `P[-2]'. (`P[-1]' is the character `-'.)
2168 - Then we set the translation of all bits between the starting and
2169 - ending characters (inclusive) in the compiled pattern B.
2170 -
2171 - Return an error code.
2172 -
2173 - We use these short variable names so we can use the same macros as
2174 - `regex_compile' itself. */
2175 -
2176 -static reg_errcode_t
2177 -compile_range (p_ptr, pend, translate, syntax, b)
2178 - const char **p_ptr, *pend;
2179 - char *translate;
2180 - reg_syntax_t syntax;
2181 - unsigned char *b;
2182 -{
2183 - unsigned this_char;
2184 -
2185 - const char *p = *p_ptr;
2186 - int range_start, range_end;
2187 -
2188 - if (p == pend)
2189 - return REG_ERANGE;
2190 -
2191 - /* Even though the pattern is a signed `char *', we need to fetch
2192 - with unsigned char *'s; if the high bit of the pattern character
2193 - is set, the range endpoints will be negative if we fetch using a
2194 - signed char *.
2195 -
2196 - We also want to fetch the endpoints without translating them; the
2197 - appropriate translation is done in the bit-setting loop below. */
2198 - range_start = ((unsigned char *) p)[-2];
2199 - range_end = ((unsigned char *) p)[0];
2200 -
2201 - /* Have to increment the pointer into the pattern string, so the
2202 - caller isn't still at the ending character. */
2203 - (*p_ptr)++;
2204 -
2205 - /* If the start is after the end, the range is empty. */
2206 - if (range_start > range_end)
2207 - return syntax & RE_NO_EMPTY_RANGES ? REG_ERANGE : REG_NOERROR;
2208 -
2209 - /* Here we see why `this_char' has to be larger than an `unsigned
2210 - char' -- the range is inclusive, so if `range_end' == 0xff
2211 - (assuming 8-bit characters), we would otherwise go into an infinite
2212 - loop, since all characters <= 0xff. */
2213 - for (this_char = range_start; this_char <= range_end; this_char++)
2214 - {
2215 - SET_LIST_BIT (TRANSLATE (this_char));
2216 - }
2217 -
2218 - return REG_NOERROR;
2219 -}
2220 -
2221 -/* Failure stack declarations and macros; both re_compile_fastmap and
2222 - re_match_2 use a failure stack. These have to be macros because of
2223 - REGEX_ALLOCATE. */
2224 -
2225 -
2226 -/* Number of failure points for which to initially allocate space
2227 - when matching. If this number is exceeded, we allocate more
2228 - space, so it is not a hard limit. */
2229 -#ifndef INIT_FAILURE_ALLOC
2230 -#define INIT_FAILURE_ALLOC 5
2231 -#endif
2232 -
2233 -/* Roughly the maximum number of failure points on the stack. Would be
2234 - exactly that if always used MAX_FAILURE_SPACE each time we failed.
2235 - This is a variable only so users of regex can assign to it; we never
2236 - change it ourselves. */
2237 -int re_max_failures = 2000;
2238 -
2239 -typedef const unsigned char *fail_stack_elt_t;
2240 -
2241 -typedef struct
2242 -{
2243 - fail_stack_elt_t *stack;
2244 - unsigned size;
2245 - unsigned avail; /* Offset of next open position. */
2246 -} fail_stack_type;
2247 -
2248 -#define FAIL_STACK_EMPTY() (fail_stack.avail == 0)
2249 -#define FAIL_STACK_PTR_EMPTY() (fail_stack_ptr->avail == 0)
2250 -#define FAIL_STACK_FULL() (fail_stack.avail == fail_stack.size)
2251 -#define FAIL_STACK_TOP() (fail_stack.stack[fail_stack.avail])
2252 -
2253 -
2254 -/* Initialize `fail_stack'. Do `return -2' if the alloc fails. */
2255 -
2256 -#define INIT_FAIL_STACK() \
2257 - do { \
2258 - fail_stack.stack = (fail_stack_elt_t *) \
2259 - REGEX_ALLOCATE (INIT_FAILURE_ALLOC * sizeof (fail_stack_elt_t)); \
2260 - \
2261 - if (fail_stack.stack == NULL) \
2262 - return -2; \
2263 - \
2264 - fail_stack.size = INIT_FAILURE_ALLOC; \
2265 - fail_stack.avail = 0; \
2266 - } while (0)
2267 -
2268 -
2269 -/* Double the size of FAIL_STACK, up to approximately `re_max_failures' items.
2270 -
2271 - Return 1 if succeeds, and 0 if either ran out of memory
2272 - allocating space for it or it was already too large.
2273 -
2274 - REGEX_REALLOCATE requires `destination' be declared. */
2275 -
2276 -#define DOUBLE_FAIL_STACK(fail_stack) \
2277 - ((fail_stack).size > re_max_failures * MAX_FAILURE_ITEMS \
2278 - ? 0 \
2279 - : ((fail_stack).stack = (fail_stack_elt_t *) \
2280 - REGEX_REALLOCATE ((fail_stack).stack, \
2281 - (fail_stack).size * sizeof (fail_stack_elt_t), \
2282 - ((fail_stack).size << 1) * sizeof (fail_stack_elt_t)), \
2283 - \
2284 - (fail_stack).stack == NULL \
2285 - ? 0 \
2286 - : ((fail_stack).size <<= 1, \
2287 - 1)))
2288 -
2289 -
2290 -/* Push PATTERN_OP on FAIL_STACK.
2291 -
2292 - Return 1 if was able to do so and 0 if ran out of memory allocating
2293 - space to do so. */
2294 -#define PUSH_PATTERN_OP(pattern_op, fail_stack) \
2295 - ((FAIL_STACK_FULL () \
2296 - && !DOUBLE_FAIL_STACK (fail_stack)) \
2297 - ? 0 \
2298 - : ((fail_stack).stack[(fail_stack).avail++] = pattern_op, \
2299 - 1))
2300 -
2301 -/* This pushes an item onto the failure stack. Must be a four-byte
2302 - value. Assumes the variable `fail_stack'. Probably should only
2303 - be called from within `PUSH_FAILURE_POINT'. */
2304 -#define PUSH_FAILURE_ITEM(item) \
2305 - fail_stack.stack[fail_stack.avail++] = (fail_stack_elt_t) item
2306 -
2307 -/* The complement operation. Assumes `fail_stack' is nonempty. */
2308 -#define POP_FAILURE_ITEM() fail_stack.stack[--fail_stack.avail]
2309 -
2310 -/* Used to omit pushing failure point id's when we're not debugging. */
2311 -#ifdef DEBUG
2312 -#define DEBUG_PUSH PUSH_FAILURE_ITEM
2313 -#define DEBUG_POP(item_addr) *(item_addr) = POP_FAILURE_ITEM ()
2314 -#else
2315 -#define DEBUG_PUSH(item)
2316 -#define DEBUG_POP(item_addr)
2317 -#endif
2318 -
2319 -
2320 -/* Push the information about the state we will need
2321 - if we ever fail back to it.
2322 -
2323 - Requires variables fail_stack, regstart, regend, reg_info, and
2324 - num_regs be declared. DOUBLE_FAIL_STACK requires `destination' be
2325 - declared.
2326 -
2327 - Does `return FAILURE_CODE' if runs out of memory. */
2328 -
2329 -#define PUSH_FAILURE_POINT(pattern_place, string_place, failure_code) \
2330 - do { \
2331 - char *destination; \
2332 - /* Must be int, so when we don't save any registers, the arithmetic \
2333 - of 0 + -1 isn't done as unsigned. */ \
2334 - int this_reg; \
2335 - \
2336 - DEBUG_STATEMENT (failure_id++); \
2337 - DEBUG_STATEMENT (nfailure_points_pushed++); \
2338 - DEBUG_PRINT2 ("\nPUSH_FAILURE_POINT #%u:\n", failure_id); \
2339 - DEBUG_PRINT2 (" Before push, next avail: %d\n", (fail_stack).avail);\
2340 - DEBUG_PRINT2 (" size: %d\n", (fail_stack).size);\
2341 - \
2342 - DEBUG_PRINT2 (" slots needed: %d\n", NUM_FAILURE_ITEMS); \
2343 - DEBUG_PRINT2 (" available: %d\n", REMAINING_AVAIL_SLOTS); \
2344 - \
2345 - /* Ensure we have enough space allocated for what we will push. */ \
2346 - while (REMAINING_AVAIL_SLOTS < NUM_FAILURE_ITEMS) \
2347 - { \
2348 - if (!DOUBLE_FAIL_STACK (fail_stack)) \
2349 - return failure_code; \
2350 - \
2351 - DEBUG_PRINT2 ("\n Doubled stack; size now: %d\n", \
2352 - (fail_stack).size); \
2353 - DEBUG_PRINT2 (" slots available: %d\n", REMAINING_AVAIL_SLOTS);\
2354 - } \
2355 - \
2356 - /* Push the info, starting with the registers. */ \
2357 - DEBUG_PRINT1 ("\n"); \
2358 - \
2359 - for (this_reg = lowest_active_reg; this_reg <= highest_active_reg; \
2360 - this_reg++) \
2361 - { \
2362 - DEBUG_PRINT2 (" Pushing reg: %d\n", this_reg); \
2363 - DEBUG_STATEMENT (num_regs_pushed++); \
2364 - \
2365 - DEBUG_PRINT2 (" start: 0x%x\n", regstart[this_reg]); \
2366 - PUSH_FAILURE_ITEM (regstart[this_reg]); \
2367 - \
2368 - DEBUG_PRINT2 (" end: 0x%x\n", regend[this_reg]); \
2369 - PUSH_FAILURE_ITEM (regend[this_reg]); \
2370 - \
2371 - DEBUG_PRINT2 (" info: 0x%x\n ", reg_info[this_reg]); \
2372 - DEBUG_PRINT2 (" match_null=%d", \
2373 - REG_MATCH_NULL_STRING_P (reg_info[this_reg])); \
2374 - DEBUG_PRINT2 (" active=%d", IS_ACTIVE (reg_info[this_reg])); \
2375 - DEBUG_PRINT2 (" matched_something=%d", \
2376 - MATCHED_SOMETHING (reg_info[this_reg])); \
2377 - DEBUG_PRINT2 (" ever_matched=%d", \
2378 - EVER_MATCHED_SOMETHING (reg_info[this_reg])); \
2379 - DEBUG_PRINT1 ("\n"); \
2380 - PUSH_FAILURE_ITEM (reg_info[this_reg].word); \
2381 - } \
2382 - \
2383 - DEBUG_PRINT2 (" Pushing low active reg: %d\n", lowest_active_reg);\
2384 - PUSH_FAILURE_ITEM (lowest_active_reg); \
2385 - \
2386 - DEBUG_PRINT2 (" Pushing high active reg: %d\n", highest_active_reg);\
2387 - PUSH_FAILURE_ITEM (highest_active_reg); \
2388 - \
2389 - DEBUG_PRINT2 (" Pushing pattern 0x%x: ", pattern_place); \
2390 - DEBUG_PRINT_COMPILED_PATTERN (bufp, pattern_place, pend); \
2391 - PUSH_FAILURE_ITEM (pattern_place); \
2392 - \
2393 - DEBUG_PRINT2 (" Pushing string 0x%x: `", string_place); \
2394 - DEBUG_PRINT_DOUBLE_STRING (string_place, string1, size1, string2, \
2395 - size2); \
2396 - DEBUG_PRINT1 ("'\n"); \
2397 - PUSH_FAILURE_ITEM (string_place); \
2398 - \
2399 - DEBUG_PRINT2 (" Pushing failure id: %u\n", failure_id); \
2400 - DEBUG_PUSH (failure_id); \
2401 - } while (0)
2402 -
2403 -/* This is the number of items that are pushed and popped on the stack
2404 - for each register. */
2405 -#define NUM_REG_ITEMS 3
2406 -
2407 -/* Individual items aside from the registers. */
2408 -#ifdef DEBUG
2409 -#define NUM_NONREG_ITEMS 5 /* Includes failure point id. */
2410 -#else
2411 -#define NUM_NONREG_ITEMS 4
2412 -#endif
2413 -
2414 -/* We push at most this many items on the stack. */
2415 -#define MAX_FAILURE_ITEMS ((num_regs - 1) * NUM_REG_ITEMS + NUM_NONREG_ITEMS)
2416 -
2417 -/* We actually push this many items. */
2418 -#define NUM_FAILURE_ITEMS \
2419 - ((highest_active_reg - lowest_active_reg + 1) * NUM_REG_ITEMS \
2420 - + NUM_NONREG_ITEMS)
2421 -
2422 -/* How many items can still be added to the stack without overflowing it. */
2423 -#define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail)
2424 -
2425 -
2426 -/* Pops what PUSH_FAIL_STACK pushes.
2427 -
2428 - We restore into the parameters, all of which should be lvalues:
2429 - STR -- the saved data position.
2430 - PAT -- the saved pattern position.
2431 - LOW_REG, HIGH_REG -- the highest and lowest active registers.
2432 - REGSTART, REGEND -- arrays of string positions.
2433 - REG_INFO -- array of information about each subexpression.
2434 -
2435 - Also assumes the variables `fail_stack' and (if debugging), `bufp',
2436 - `pend', `string1', `size1', `string2', and `size2'. */
2437 -
2438 -#define POP_FAILURE_POINT(str, pat, low_reg, high_reg, regstart, regend, reg_info)\
2439 -{ \
2440 - DEBUG_STATEMENT (fail_stack_elt_t failure_id;) \
2441 - int this_reg; \
2442 - const unsigned char *string_temp; \
2443 - \
2444 - assert (!FAIL_STACK_EMPTY ()); \
2445 - \
2446 - /* Remove failure points and point to how many regs pushed. */ \
2447 - DEBUG_PRINT1 ("POP_FAILURE_POINT:\n"); \
2448 - DEBUG_PRINT2 (" Before pop, next avail: %d\n", fail_stack.avail); \
2449 - DEBUG_PRINT2 (" size: %d\n", fail_stack.size); \
2450 - \
2451 - assert (fail_stack.avail >= NUM_NONREG_ITEMS); \
2452 - \
2453 - DEBUG_POP (&failure_id); \
2454 - DEBUG_PRINT2 (" Popping failure id: %u\n", failure_id); \
2455 - \
2456 - /* If the saved string location is NULL, it came from an \
2457 - on_failure_keep_string_jump opcode, and we want to throw away the \
2458 - saved NULL, thus retaining our current position in the string. */ \
2459 - string_temp = POP_FAILURE_ITEM (); \
2460 - if (string_temp != NULL) \
2461 - str = (const char *) string_temp; \
2462 - \
2463 - DEBUG_PRINT2 (" Popping string 0x%x: `", str); \
2464 - DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2); \
2465 - DEBUG_PRINT1 ("'\n"); \
2466 - \
2467 - pat = (unsigned char *) POP_FAILURE_ITEM (); \
2468 - DEBUG_PRINT2 (" Popping pattern 0x%x: ", pat); \
2469 - DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend); \
2470 - \
2471 - /* Restore register info. */ \
2472 - high_reg = (unsigned) POP_FAILURE_ITEM (); \
2473 - DEBUG_PRINT2 (" Popping high active reg: %d\n", high_reg); \
2474 - \
2475 - low_reg = (unsigned) POP_FAILURE_ITEM (); \
2476 - DEBUG_PRINT2 (" Popping low active reg: %d\n", low_reg); \
2477 - \
2478 - for (this_reg = high_reg; this_reg >= low_reg; this_reg--) \
2479 - { \
2480 - DEBUG_PRINT2 (" Popping reg: %d\n", this_reg); \
2481 - \
2482 - reg_info[this_reg].word = POP_FAILURE_ITEM (); \
2483 - DEBUG_PRINT2 (" info: 0x%x\n", reg_info[this_reg]); \
2484 - \
2485 - regend[this_reg] = (const char *) POP_FAILURE_ITEM (); \
2486 - DEBUG_PRINT2 (" end: 0x%x\n", regend[this_reg]); \
2487 - \
2488 - regstart[this_reg] = (const char *) POP_FAILURE_ITEM (); \
2489 - DEBUG_PRINT2 (" start: 0x%x\n", regstart[this_reg]); \
2490 - } \
2491 - \
2492 - DEBUG_STATEMENT (nfailure_points_popped++); \
2493 -} /* POP_FAILURE_POINT */
2494 -
2495 -/* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
2496 - BUFP. A fastmap records which of the (1 << BYTEWIDTH) possible
2497 - characters can start a string that matches the pattern. This fastmap
2498 - is used by re_search to skip quickly over impossible starting points.
2499 -
2500 - The caller must supply the address of a (1 << BYTEWIDTH)-byte data
2501 - area as BUFP->fastmap.
2502 -
2503 - We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in
2504 - the pattern buffer.
2505 -
2506 - Returns 0 if we succeed, -2 if an internal error. */
2507 -
2508 -int
2509 -re_compile_fastmap (bufp)
2510 - struct re_pattern_buffer *bufp;
2511 -{
2512 - int j, k;
2513 - fail_stack_type fail_stack;
2514 -#ifndef REGEX_MALLOC
2515 - char *destination;
2516 -#endif
2517 - /* We don't push any register information onto the failure stack. */
2518 - unsigned num_regs = 0;
2519 -
2520 - register char *fastmap = bufp->fastmap;
2521 - unsigned char *pattern = bufp->buffer;
2522 - unsigned long size = bufp->used;
2523 - const unsigned char *p = pattern;
2524 - register unsigned char *pend = pattern + size;
2525 -
2526 - /* Assume that each path through the pattern can be null until
2527 - proven otherwise. We set this false at the bottom of switch
2528 - statement, to which we get only if a particular path doesn't
2529 - match the empty string. */
2530 - boolean path_can_be_null = true;
2531 -
2532 - /* We aren't doing a `succeed_n' to begin with. */
2533 - boolean succeed_n_p = false;
2534 -
2535 - assert (fastmap != NULL && p != NULL);
2536 -
2537 - INIT_FAIL_STACK ();
2538 - bzero (fastmap, 1 << BYTEWIDTH); /* Assume nothing's valid. */
2539 - bufp->fastmap_accurate = 1; /* It will be when we're done. */
2540 - bufp->can_be_null = 0;
2541 -
2542 - while (p != pend || !FAIL_STACK_EMPTY ())
2543 - {
2544 - if (p == pend)
2545 - {
2546 - bufp->can_be_null |= path_can_be_null;
2547 -
2548 - /* Reset for next path. */
2549 - path_can_be_null = true;
2550 -
2551 - p = fail_stack.stack[--fail_stack.avail];
2552 - }
2553 -
2554 - /* We should never be about to go beyond the end of the pattern. */
2555 - assert (p < pend);
2556 -
2557 -#ifdef SWITCH_ENUM_BUG
2558 - switch ((int) ((re_opcode_t) *p++))
2559 -#else
2560 - switch ((re_opcode_t) *p++)
2561 -#endif
2562 - {
2563 -
2564 - /* I guess the idea here is to simply not bother with a fastmap
2565 - if a backreference is used, since it's too hard to figure out
2566 - the fastmap for the corresponding group. Setting
2567 - `can_be_null' stops `re_search_2' from using the fastmap, so
2568 - that is all we do. */
2569 - case duplicate:
2570 - bufp->can_be_null = 1;
2571 - return 0;
2572 -
2573 -
2574 - /* Following are the cases which match a character. These end
2575 - with `break'. */
2576 -
2577 - case exactn:
2578 - fastmap[p[1]] = 1;
2579 - break;
2580 -
2581 -
2582 - case charset:
2583 - for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
2584 - if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
2585 - fastmap[j] = 1;
2586 - break;
2587 -
2588 -
2589 - case charset_not:
2590 - /* Chars beyond end of map must be allowed. */
2591 - for (j = *p * BYTEWIDTH; j < (1 << BYTEWIDTH); j++)
2592 - fastmap[j] = 1;
2593 -
2594 - for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
2595 - if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
2596 - fastmap[j] = 1;
2597 - break;
2598 -
2599 -
2600 - case wordchar:
2601 - for (j = 0; j < (1 << BYTEWIDTH); j++)
2602 - if (SYNTAX (j) == Sword)
2603 - fastmap[j] = 1;
2604 - break;
2605 -
2606 -
2607 - case notwordchar:
2608 - for (j = 0; j < (1 << BYTEWIDTH); j++)
2609 - if (SYNTAX (j) != Sword)
2610 - fastmap[j] = 1;
2611 - break;
2612 -
2613 -
2614 - case anychar:
2615 - /* `.' matches anything ... */
2616 - for (j = 0; j < (1 << BYTEWIDTH); j++)
2617 - fastmap[j] = 1;
2618 -
2619 - /* ... except perhaps newline. */
2620 - if (!(bufp->syntax & RE_DOT_NEWLINE))
2621 - fastmap['\n'] = 0;
2622 -
2623 - /* Return if we have already set `can_be_null'; if we have,
2624 - then the fastmap is irrelevant. Something's wrong here. */
2625 - else if (bufp->can_be_null)
2626 - return 0;
2627 -
2628 - /* Otherwise, have to check alternative paths. */
2629 - break;
2630 -
2631 -
2632 -#ifdef emacs
2633 - case syntaxspec:
2634 - k = *p++;
2635 - for (j = 0; j < (1 << BYTEWIDTH); j++)
2636 - if (SYNTAX (j) == (enum syntaxcode) k)
2637 - fastmap[j] = 1;
2638 - break;
2639 -
2640 -
2641 - case notsyntaxspec:
2642 - k = *p++;
2643 - for (j = 0; j < (1 << BYTEWIDTH); j++)
2644 - if (SYNTAX (j) != (enum syntaxcode) k)
2645 - fastmap[j] = 1;
2646 - break;
2647 -
2648 -
2649 - /* All cases after this match the empty string. These end with
2650 - `continue'. */
2651 -
2652 -
2653 - case before_dot:
2654 - case at_dot:
2655 - case after_dot:
2656 - continue;
2657 -#endif /* not emacs */
2658 -
2659 -
2660 - case no_op:
2661 - case begline:
2662 - case endline:
2663 - case begbuf:
2664 - case endbuf:
2665 - case wordbound:
2666 - case notwordbound:
2667 - case wordbeg:
2668 - case wordend:
2669 - case push_dummy_failure:
2670 - continue;
2671 -
2672 -
2673 - case jump_n:
2674 - case pop_failure_jump:
2675 - case maybe_pop_jump:
2676 - case jump:
2677 - case jump_past_alt:
2678 - case dummy_failure_jump:
2679 - EXTRACT_NUMBER_AND_INCR (j, p);
2680 - p += j;
2681 - if (j > 0)
2682 - continue;
2683 -
2684 - /* Jump backward implies we just went through the body of a
2685 - loop and matched nothing. Opcode jumped to should be
2686 - `on_failure_jump' or `succeed_n'. Just treat it like an
2687 - ordinary jump. For a * loop, it has pushed its failure
2688 - point already; if so, discard that as redundant. */
2689 - if ((re_opcode_t) *p != on_failure_jump
2690 - && (re_opcode_t) *p != succeed_n)
2691 - continue;
2692 -
2693 - p++;
2694 - EXTRACT_NUMBER_AND_INCR (j, p);
2695 - p += j;
2696 -
2697 - /* If what's on the stack is where we are now, pop it. */
2698 - if (!FAIL_STACK_EMPTY ()
2699 - && fail_stack.stack[fail_stack.avail - 1] == p)
2700 - fail_stack.avail--;
2701 -
2702 - continue;
2703 -
2704 -
2705 - case on_failure_jump:
2706 - case on_failure_keep_string_jump:
2707 - handle_on_failure_jump:
2708 - EXTRACT_NUMBER_AND_INCR (j, p);
2709 -
2710 - /* For some patterns, e.g., `(a?)?', `p+j' here points to the
2711 - end of the pattern. We don't want to push such a point,
2712 - since when we restore it above, entering the switch will
2713 - increment `p' past the end of the pattern. We don't need
2714 - to push such a point since we obviously won't find any more
2715 - fastmap entries beyond `pend'. Such a pattern can match
2716 - the null string, though. */
2717 - if (p + j < pend)
2718 - {
2719 - if (!PUSH_PATTERN_OP (p + j, fail_stack))
2720 - return -2;
2721 - }
2722 - else
2723 - bufp->can_be_null = 1;
2724 -
2725 - if (succeed_n_p)
2726 - {
2727 - EXTRACT_NUMBER_AND_INCR (k, p); /* Skip the n. */
2728 - succeed_n_p = false;
2729 - }
2730 -
2731 - continue;
2732 -
2733 -
2734 - case succeed_n:
2735 - /* Get to the number of times to succeed. */
2736 - p += 2;
2737 -
2738 - /* Increment p past the n for when k != 0. */
2739 - EXTRACT_NUMBER_AND_INCR (k, p);
2740 - if (k == 0)
2741 - {
2742 - p -= 4;
2743 - succeed_n_p = true; /* Spaghetti code alert. */
2744 - goto handle_on_failure_jump;
2745 - }
2746 - continue;
2747 -
2748 -
2749 - case set_number_at:
2750 - p += 4;
2751 - continue;
2752 -
2753 -
2754 - case start_memory:
2755 - case stop_memory:
2756 - p += 2;
2757 - continue;
2758 -
2759 -
2760 - default:
2761 - abort (); /* We have listed all the cases. */
2762 - } /* switch *p++ */
2763 -
2764 - /* Getting here means we have found the possible starting
2765 - characters for one path of the pattern -- and that the empty
2766 - string does not match. We need not follow this path further.
2767 - Instead, look at the next alternative (remembered on the
2768 - stack), or quit if no more. The test at the top of the loop
2769 - does these things. */
2770 - path_can_be_null = false;
2771 - p = pend;
2772 - } /* while p */
2773 -
2774 - /* Set `can_be_null' for the last path (also the first path, if the
2775 - pattern is empty). */
2776 - bufp->can_be_null |= path_can_be_null;
2777 - return 0;
2778 -} /* re_compile_fastmap */
2779 -
2780 -/* Set REGS to hold NUM_REGS registers, storing them in STARTS and
2781 - ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
2782 - this memory for recording register information. STARTS and ENDS
2783 - must be allocated using the malloc library routine, and must each
2784 - be at least NUM_REGS * sizeof (regoff_t) bytes long.
2785 -
2786 - If NUM_REGS == 0, then subsequent matches should allocate their own
2787 - register data.
2788 -
2789 - Unless this function is called, the first search or match using
2790 - PATTERN_BUFFER will allocate its own register data, without
2791 - freeing the old data. */
2792 -
2793 -void
2794 -re_set_registers (bufp, regs, num_regs, starts, ends)
2795 - struct re_pattern_buffer *bufp;
2796 - struct re_registers *regs;
2797 - unsigned num_regs;
2798 - regoff_t *starts, *ends;
2799 -{
2800 - if (num_regs)
2801 - {
2802 - bufp->regs_allocated = REGS_REALLOCATE;
2803 - regs->num_regs = num_regs;
2804 - regs->start = starts;
2805 - regs->end = ends;
2806 - }
2807 - else
2808 - {
2809 - bufp->regs_allocated = REGS_UNALLOCATED;
2810 - regs->num_regs = 0;
2811 - regs->start = regs->end = (regoff_t *) 0;
2812 - }
2813 -}
2814 -
2815 -/* Searching routines. */
2816 -
2817 -/* Like re_search_2, below, but only one string is specified, and
2818 - doesn't let you say where to stop matching. */
2819 -
2820 -int
2821 -re_search (bufp, string, size, startpos, range, regs)
2822 - struct re_pattern_buffer *bufp;
2823 - const char *string;
2824 - int size, startpos, range;
2825 - struct re_registers *regs;
2826 -{
2827 - return re_search_2 (bufp, NULL, 0, string, size, startpos, range,
2828 - regs, size);
2829 -}
2830 -
2831 -
2832 -/* Using the compiled pattern in BUFP->buffer, first tries to match the
2833 - virtual concatenation of STRING1 and STRING2, starting first at index
2834 - STARTPOS, then at STARTPOS + 1, and so on.
2835 -
2836 - STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
2837 -
2838 - RANGE is how far to scan while trying to match. RANGE = 0 means try
2839 - only at STARTPOS; in general, the last start tried is STARTPOS +
2840 - RANGE.
2841 -
2842 - In REGS, return the indices of the virtual concatenation of STRING1
2843 - and STRING2 that matched the entire BUFP->buffer and its contained
2844 - subexpressions.
2845 -
2846 - Do not consider matching one past the index STOP in the virtual
2847 - concatenation of STRING1 and STRING2.
2848 -
2849 - We return either the position in the strings at which the match was
2850 - found, -1 if no match, or -2 if error (such as failure
2851 - stack overflow). */
2852 -
2853 -int
2854 -re_search_2 (bufp, string1, size1, string2, size2, startpos, range, regs, stop)
2855 - struct re_pattern_buffer *bufp;
2856 - const char *string1, *string2;
2857 - int size1, size2;
2858 - int startpos;
2859 - int range;
2860 - struct re_registers *regs;
2861 - int stop;
2862 -{
2863 - int val;
2864 - register char *fastmap = bufp->fastmap;
2865 - register char *translate = bufp->translate;
2866 - int total_size = size1 + size2;
2867 - int endpos = startpos + range;
2868 -
2869 - /* Check for out-of-range STARTPOS. */
2870 - if (startpos < 0 || startpos > total_size)
2871 - return -1;
2872 -
2873 - /* Fix up RANGE if it might eventually take us outside
2874 - the virtual concatenation of STRING1 and STRING2. */
2875 - if (endpos < -1)
2876 - range = -1 - startpos;
2877 - else if (endpos > total_size)
2878 - range = total_size - startpos;
2879 -
2880 - /* If the search isn't to be a backwards one, don't waste time in a
2881 - search for a pattern that must be anchored. */
2882 - if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == begbuf && range > 0)
2883 - {
2884 - if (startpos > 0)
2885 - return -1;
2886 - else
2887 - range = 1;
2888 - }
2889 -
2890 - /* Update the fastmap now if not correct already. */
2891 - if (fastmap && !bufp->fastmap_accurate)
2892 - if (re_compile_fastmap (bufp) == -2)
2893 - return -2;
2894 -
2895 - /* Loop through the string, looking for a place to start matching. */
2896 - for (;;)
2897 - {
2898 - /* If a fastmap is supplied, skip quickly over characters that
2899 - cannot be the start of a match. If the pattern can match the
2900 - null string, however, we don't need to skip characters; we want
2901 - the first null string. */
2902 - if (fastmap && startpos < total_size && !bufp->can_be_null)
2903 - {
2904 - if (range > 0) /* Searching forwards. */
2905 - {
2906 - register const char *d;
2907 - register int lim = 0;
2908 - int irange = range;
2909 -
2910 - if (startpos < size1 && startpos + range >= size1)
2911 - lim = range - (size1 - startpos);
2912 -
2913 - d = (startpos >= size1 ? string2 - size1 : string1) + startpos;
2914 -
2915 - /* Written out as an if-else to avoid testing `translate'
2916 - inside the loop. */
2917 - if (translate)
2918 - while (range > lim
2919 - && !fastmap[(unsigned char)
2920 - translate[(unsigned char) *d++]])
2921 - range--;
2922 - else
2923 - while (range > lim && !fastmap[(unsigned char) *d++])
2924 - range--;
2925 -
2926 - startpos += irange - range;
2927 - }
2928 - else /* Searching backwards. */
2929 - {
2930 - register char c = (size1 == 0 || startpos >= size1
2931 - ? string2[startpos - size1]
2932 - : string1[startpos]);
2933 -
2934 - if (!fastmap[(unsigned char) TRANSLATE (c)])
2935 - goto advance;
2936 - }
2937 - }
2938 -
2939 - /* If can't match the null string, and that's all we have left, fail. */
2940 - if (range >= 0 && startpos == total_size && fastmap
2941 - && !bufp->can_be_null)
2942 - return -1;
2943 -
2944 - val = re_match_2 (bufp, string1, size1, string2, size2,
2945 - startpos, regs, stop);
2946 - if (val >= 0)
2947 - return startpos;
2948 -
2949 - if (val == -2)
2950 - return -2;
2951 -
2952 - advance:
2953 - if (!range)
2954 - break;
2955 - else if (range > 0)
2956 - {
2957 - range--;
2958 - startpos++;
2959 - }
2960 - else
2961 - {
2962 - range++;
2963 - startpos--;
2964 - }
2965 - }
2966 - return -1;
2967 -} /* re_search_2 */
2968 -
2969 -/* Declarations and macros for re_match_2. */
2970 -
2971 -static int bcmp_translate ();
2972 -static boolean alt_match_null_string_p (),
2973 - common_op_match_null_string_p (),
2974 - group_match_null_string_p ();
2975 -
2976 -/* Structure for per-register (a.k.a. per-group) information.
2977 - This must not be longer than one word, because we push this value
2978 - onto the failure stack. Other register information, such as the
2979 - starting and ending positions (which are addresses), and the list of
2980 - inner groups (which is a bits list) are maintained in separate
2981 - variables.
2982 -
2983 - We are making a (strictly speaking) nonportable assumption here: that
2984 - the compiler will pack our bit fields into something that fits into
2985 - the type of `word', i.e., is something that fits into one item on the
2986 - failure stack. */
2987 -typedef union
2988 -{
2989 - fail_stack_elt_t word;
2990 - struct
2991 - {
2992 - /* This field is one if this group can match the empty string,
2993 - zero if not. If not yet determined, `MATCH_NULL_UNSET_VALUE'. */
2994 -#define MATCH_NULL_UNSET_VALUE 3
2995 - unsigned match_null_string_p : 2;
2996 - unsigned is_active : 1;
2997 - unsigned matched_something : 1;
2998 - unsigned ever_matched_something : 1;
2999 - } bits;
3000 -} register_info_type;
3001 -
3002 -#define REG_MATCH_NULL_STRING_P(R) ((R).bits.match_null_string_p)
3003 -#define IS_ACTIVE(R) ((R).bits.is_active)
3004 -#define MATCHED_SOMETHING(R) ((R).bits.matched_something)
3005 -#define EVER_MATCHED_SOMETHING(R) ((R).bits.ever_matched_something)
3006 -
3007 -
3008 -/* Call this when have matched a real character; it sets `matched' flags
3009 - for the subexpressions which we are currently inside. Also records
3010 - that those subexprs have matched. */
3011 -#define SET_REGS_MATCHED() \
3012 - do \
3013 - { \
3014 - unsigned r; \
3015 - for (r = lowest_active_reg; r <= highest_active_reg; r++) \
3016 - { \
3017 - MATCHED_SOMETHING (reg_info[r]) \
3018 - = EVER_MATCHED_SOMETHING (reg_info[r]) \
3019 - = 1; \
3020 - } \
3021 - } \
3022 - while (0)
3023 -
3024 -
3025 -/* This converts PTR, a pointer into one of the search strings `string1'
3026 - and `string2' into an offset from the beginning of that string. */
3027 -#define POINTER_TO_OFFSET(ptr) \
3028 - (FIRST_STRING_P (ptr) ? (ptr) - string1 : (ptr) - string2 + size1)
3029 -
3030 -/* Registers are set to a sentinel when they haven't yet matched. */
3031 -#define REG_UNSET_VALUE ((char *) -1)
3032 -#define REG_UNSET(e) ((e) == REG_UNSET_VALUE)
3033 -
3034 -
3035 -/* Macros for dealing with the split strings in re_match_2. */
3036 -
3037 -#define MATCHING_IN_FIRST_STRING (dend == end_match_1)
3038 -
3039 -/* Call before fetching a character with *d. This switches over to
3040 - string2 if necessary. */
3041 -#define PREFETCH() \
3042 - while (d == dend) \
3043 - { \
3044 - /* End of string2 => fail. */ \
3045 - if (dend == end_match_2) \
3046 - goto fail; \
3047 - /* End of string1 => advance to string2. */ \
3048 - d = string2; \
3049 - dend = end_match_2; \
3050 - }
3051 -
3052 -
3053 -/* Test if at very beginning or at very end of the virtual concatenation
3054 - of `string1' and `string2'. If only one string, it's `string2'. */
3055 -#define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2)
3056 -#define AT_STRINGS_END(d) ((d) == end2)
3057 -
3058 -
3059 -/* Test if D points to a character which is word-constituent. We have
3060 - two special cases to check for: if past the end of string1, look at
3061 - the first character in string2; and if before the beginning of
3062 - string2, look at the last character in string1. */
3063 -#define WORDCHAR_P(d) \
3064 - (SYNTAX ((d) == end1 ? *string2 \
3065 - : (d) == string2 - 1 ? *(end1 - 1) : *(d)) \
3066 - == Sword)
3067 -
3068 -/* Test if the character before D and the one at D differ with respect
3069 - to being word-constituent. */
3070 -#define AT_WORD_BOUNDARY(d) \
3071 - (AT_STRINGS_BEG (d) || AT_STRINGS_END (d) \
3072 - || WORDCHAR_P (d - 1) != WORDCHAR_P (d))
3073 -
3074 -
3075 -/* Free everything we malloc. */
3076 -#ifdef REGEX_MALLOC
3077 -#define FREE_VAR(var) if (var) free (var); var = NULL
3078 -#define FREE_VARIABLES() \
3079 - do { \
3080 - FREE_VAR (fail_stack.stack); \
3081 - FREE_VAR (regstart); \
3082 - FREE_VAR (regend); \
3083 - FREE_VAR (old_regstart); \
3084 - FREE_VAR (old_regend); \
3085 - FREE_VAR (best_regstart); \
3086 - FREE_VAR (best_regend); \
3087 - FREE_VAR (reg_info); \
3088 - FREE_VAR (reg_dummy); \
3089 - FREE_VAR (reg_info_dummy); \
3090 - } while (0)
3091 -#else /* not REGEX_MALLOC */
3092 -/* Some MIPS systems (at least) want this to free alloca'd storage. */
3093 -#define FREE_VARIABLES() alloca (0)
3094 -#endif /* not REGEX_MALLOC */
3095 -
3096 -
3097 -/* These values must meet several constraints. They must not be valid
3098 - register values; since we have a limit of 255 registers (because
3099 - we use only one byte in the pattern for the register number), we can
3100 - use numbers larger than 255. They must differ by 1, because of
3101 - NUM_FAILURE_ITEMS above. And the value for the lowest register must
3102 - be larger than the value for the highest register, so we do not try
3103 - to actually save any registers when none are active. */
3104 -#define NO_HIGHEST_ACTIVE_REG (1 << BYTEWIDTH)
3105 -#define NO_LOWEST_ACTIVE_REG (NO_HIGHEST_ACTIVE_REG + 1)
3106 -
3107 -/* Matching routines. */
3108 -
3109 -#ifndef emacs /* Emacs never uses this. */
3110 -/* re_match is like re_match_2 except it takes only a single string. */
3111 -
3112 -int
3113 -re_match (bufp, string, size, pos, regs)
3114 - struct re_pattern_buffer *bufp;
3115 - const char *string;
3116 - int size, pos;
3117 - struct re_registers *regs;
3118 - {
3119 - return re_match_2 (bufp, NULL, 0, string, size, pos, regs, size);
3120 -}
3121 -#endif /* not emacs */
3122 -
3123 -
3124 -/* re_match_2 matches the compiled pattern in BUFP against the
3125 - the (virtual) concatenation of STRING1 and STRING2 (of length SIZE1
3126 - and SIZE2, respectively). We start matching at POS, and stop
3127 - matching at STOP.
3128 -
3129 - If REGS is non-null and the `no_sub' field of BUFP is nonzero, we
3130 - store offsets for the substring each group matched in REGS. See the
3131 - documentation for exactly how many groups we fill.
3132 -
3133 - We return -1 if no match, -2 if an internal error (such as the
3134 - failure stack overflowing). Otherwise, we return the length of the
3135 - matched substring. */
3136 -
3137 -int
3138 -re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
3139 - struct re_pattern_buffer *bufp;
3140 - const char *string1, *string2;
3141 - int size1, size2;
3142 - int pos;
3143 - struct re_registers *regs;
3144 - int stop;
3145 -{
3146 - /* General temporaries. */
3147 - int mcnt;
3148 - unsigned char *p1;
3149 -
3150 - /* Just past the end of the corresponding string. */
3151 - const char *end1, *end2;
3152 -
3153 - /* Pointers into string1 and string2, just past the last characters in
3154 - each to consider matching. */
3155 - const char *end_match_1, *end_match_2;
3156 -
3157 - /* Where we are in the data, and the end of the current string. */
3158 - const char *d, *dend;
3159 -
3160 - /* Where we are in the pattern, and the end of the pattern. */
3161 - unsigned char *p = bufp->buffer;
3162 - register unsigned char *pend = p + bufp->used;
3163 -
3164 - /* We use this to map every character in the string. */
3165 - char *translate = bufp->translate;
3166 -
3167 - /* Failure point stack. Each place that can handle a failure further
3168 - down the line pushes a failure point on this stack. It consists of
3169 - restart, regend, and reg_info for all registers corresponding to
3170 - the subexpressions we're currently inside, plus the number of such
3171 - registers, and, finally, two char *'s. The first char * is where
3172 - to resume scanning the pattern; the second one is where to resume
3173 - scanning the strings. If the latter is zero, the failure point is
3174 - a ``dummy''; if a failure happens and the failure point is a dummy,
3175 - it gets discarded and the next next one is tried. */
3176 - fail_stack_type fail_stack;
3177 -#ifdef DEBUG
3178 - static unsigned failure_id = 0;
3179 - unsigned nfailure_points_pushed = 0, nfailure_points_popped = 0;
3180 -#endif
3181 -
3182 - /* We fill all the registers internally, independent of what we
3183 - return, for use in backreferences. The number here includes
3184 - an element for register zero. */
3185 - unsigned num_regs = bufp->re_nsub + 1;
3186 -
3187 - /* The currently active registers. */
3188 - unsigned lowest_active_reg = NO_LOWEST_ACTIVE_REG;
3189 - unsigned highest_active_reg = NO_HIGHEST_ACTIVE_REG;
3190 -
3191 - /* Information on the contents of registers. These are pointers into
3192 - the input strings; they record just what was matched (on this
3193 - attempt) by a subexpression part of the pattern, that is, the
3194 - regnum-th regstart pointer points to where in the pattern we began
3195 - matching and the regnum-th regend points to right after where we
3196 - stopped matching the regnum-th subexpression. (The zeroth register
3197 - keeps track of what the whole pattern matches.) */
3198 - const char **regstart = NULL, **regend = NULL;
3199 -
3200 - /* If a group that's operated upon by a repetition operator fails to
3201 - match anything, then the register for its start will need to be
3202 - restored because it will have been set to wherever in the string we
3203 - are when we last see its open-group operator. Similarly for a
3204 - register's end. */
3205 - const char **old_regstart = NULL, **old_regend = NULL;
3206 -
3207 - /* The is_active field of reg_info helps us keep track of which (possibly
3208 - nested) subexpressions we are currently in. The matched_something
3209 - field of reg_info[reg_num] helps us tell whether or not we have
3210 - matched any of the pattern so far this time through the reg_num-th
3211 - subexpression. These two fields get reset each time through any
3212 - loop their register is in. */
3213 - register_info_type *reg_info = NULL;
3214 -
3215 - /* The following record the register info as found in the above
3216 - variables when we find a match better than any we've seen before.
3217 - This happens as we backtrack through the failure points, which in
3218 - turn happens only if we have not yet matched the entire string. */
3219 - unsigned best_regs_set = false;
3220 - const char **best_regstart = NULL, **best_regend = NULL;
3221 -
3222 - /* Logically, this is `best_regend[0]'. But we don't want to have to
3223 - allocate space for that if we're not allocating space for anything
3224 - else (see below). Also, we never need info about register 0 for
3225 - any of the other register vectors, and it seems rather a kludge to
3226 - treat `best_regend' differently than the rest. So we keep track of
3227 - the end of the best match so far in a separate variable. We
3228 - initialize this to NULL so that when we backtrack the first time
3229 - and need to test it, it's not garbage. */
3230 - const char *match_end = NULL;
3231 -
3232 - /* Used when we pop values we don't care about. */
3233 - const char **reg_dummy = NULL;
3234 - register_info_type *reg_info_dummy = NULL;
3235 -
3236 -#ifdef DEBUG
3237 - /* Counts the total number of registers pushed. */
3238 - unsigned num_regs_pushed = 0;
3239 -#endif
3240 -
3241 - DEBUG_PRINT1 ("\n\nEntering re_match_2.\n");
3242 -
3243 - INIT_FAIL_STACK ();
3244 -
3245 - /* Do not bother to initialize all the register variables if there are
3246 - no groups in the pattern, as it takes a fair amount of time. If
3247 - there are groups, we include space for register 0 (the whole
3248 - pattern), even though we never use it, since it simplifies the
3249 - array indexing. We should fix this. */
3250 - if (bufp->re_nsub)
3251 - {
3252 - regstart = REGEX_TALLOC (num_regs, const char *);
3253 - regend = REGEX_TALLOC (num_regs, const char *);
3254 - old_regstart = REGEX_TALLOC (num_regs, const char *);
3255 - old_regend = REGEX_TALLOC (num_regs, const char *);
3256 - best_regstart = REGEX_TALLOC (num_regs, const char *);
3257 - best_regend = REGEX_TALLOC (num_regs, const char *);
3258 - reg_info = REGEX_TALLOC (num_regs, register_info_type);
3259 - reg_dummy = REGEX_TALLOC (num_regs, const char *);
3260 - reg_info_dummy = REGEX_TALLOC (num_regs, register_info_type);
3261 -
3262 - if (!(regstart && regend && old_regstart && old_regend && reg_info
3263 - && best_regstart && best_regend && reg_dummy && reg_info_dummy))
3264 - {
3265 - FREE_VARIABLES ();
3266 - return -2;
3267 - }
3268 - }
3269 -#ifdef REGEX_MALLOC
3270 - else
3271 - {
3272 - /* We must initialize all our variables to NULL, so that
3273 - `FREE_VARIABLES' doesn't try to free them. */
3274 - regstart = regend = old_regstart = old_regend = best_regstart
3275 - = best_regend = reg_dummy = NULL;
3276 - reg_info = reg_info_dummy = (register_info_type *) NULL;
3277 - }
3278 -#endif /* REGEX_MALLOC */
3279 -
3280 - /* The starting position is bogus. */
3281 - if (pos < 0 || pos > size1 + size2)
3282 - {
3283 - FREE_VARIABLES ();
3284 - return -1;
3285 - }
3286 -
3287 - /* Initialize subexpression text positions to -1 to mark ones that no
3288 - start_memory/stop_memory has been seen for. Also initialize the
3289 - register information struct. */
3290 - for (mcnt = 1; mcnt < num_regs; mcnt++)
3291 - {
3292 - regstart[mcnt] = regend[mcnt]
3293 - = old_regstart[mcnt] = old_regend[mcnt] = REG_UNSET_VALUE;
3294 -
3295 - REG_MATCH_NULL_STRING_P (reg_info[mcnt]) = MATCH_NULL_UNSET_VALUE;
3296 - IS_ACTIVE (reg_info[mcnt]) = 0;
3297 - MATCHED_SOMETHING (reg_info[mcnt]) = 0;
3298 - EVER_MATCHED_SOMETHING (reg_info[mcnt]) = 0;
3299 - }
3300 -
3301 - /* We move `string1' into `string2' if the latter's empty -- but not if
3302 - `string1' is null. */
3303 - if (size2 == 0 && string1 != NULL)
3304 - {
3305 - string2 = string1;
3306 - size2 = size1;
3307 - string1 = 0;
3308 - size1 = 0;
3309 - }
3310 - end1 = string1 + size1;
3311 - end2 = string2 + size2;
3312 -
3313 - /* Compute where to stop matching, within the two strings. */
3314 - if (stop <= size1)
3315 - {
3316 - end_match_1 = string1 + stop;
3317 - end_match_2 = string2;
3318 - }
3319 - else
3320 - {
3321 - end_match_1 = end1;
3322 - end_match_2 = string2 + stop - size1;
3323 - }
3324 -
3325 - /* `p' scans through the pattern as `d' scans through the data.
3326 - `dend' is the end of the input string that `d' points within. `d'
3327 - is advanced into the following input string whenever necessary, but
3328 - this happens before fetching; therefore, at the beginning of the
3329 - loop, `d' can be pointing at the end of a string, but it cannot
3330 - equal `string2'. */
3331 - if (size1 > 0 && pos <= size1)
3332 - {
3333 - d = string1 + pos;
3334 - dend = end_match_1;
3335 - }
3336 - else
3337 - {
3338 - d = string2 + pos - size1;
3339 - dend = end_match_2;
3340 - }
3341 -
3342 - DEBUG_PRINT1 ("The compiled pattern is: ");
3343 - DEBUG_PRINT_COMPILED_PATTERN (bufp, p, pend);
3344 - DEBUG_PRINT1 ("The string to match is: `");
3345 - DEBUG_PRINT_DOUBLE_STRING (d, string1, size1, string2, size2);
3346 - DEBUG_PRINT1 ("'\n");
3347 -
3348 - /* This loops over pattern commands. It exits by returning from the
3349 - function if the match is complete, or it drops through if the match
3350 - fails at this starting point in the input data. */
3351 - for (;;)
3352 - {
3353 - DEBUG_PRINT2 ("\n0x%x: ", p);
3354 -
3355 - if (p == pend)
3356 - { /* End of pattern means we might have succeeded. */
3357 - DEBUG_PRINT1 ("end of pattern ... ");
3358 -
3359 - /* If we haven't matched the entire string, and we want the
3360 - longest match, try backtracking. */
3361 - if (d != end_match_2)
3362 - {
3363 - DEBUG_PRINT1 ("backtracking.\n");
3364 -
3365 - if (!FAIL_STACK_EMPTY ())
3366 - { /* More failure points to try. */
3367 - boolean same_str_p = (FIRST_STRING_P (match_end)
3368 - == MATCHING_IN_FIRST_STRING);
3369 -
3370 - /* If exceeds best match so far, save it. */
3371 - if (!best_regs_set
3372 - || (same_str_p && d > match_end)
3373 - || (!same_str_p && !MATCHING_IN_FIRST_STRING))
3374 - {
3375 - best_regs_set = true;
3376 - match_end = d;
3377 -
3378 - DEBUG_PRINT1 ("\nSAVING match as best so far.\n");
3379 -
3380 - for (mcnt = 1; mcnt < num_regs; mcnt++)
3381 - {
3382 - best_regstart[mcnt] = regstart[mcnt];
3383 - best_regend[mcnt] = regend[mcnt];
3384 - }
3385 - }
3386 - goto fail;
3387 - }
3388 -
3389 - /* If no failure points, don't restore garbage. */
3390 - else if (best_regs_set)
3391 - {
3392 - restore_best_regs:
3393 - /* Restore best match. It may happen that `dend ==
3394 - end_match_1' while the restored d is in string2.
3395 - For example, the pattern `x.*y.*z' against the
3396 - strings `x-' and `y-z-', if the two strings are
3397 - not consecutive in memory. */
3398 - DEBUG_PRINT1 ("Restoring best registers.\n");
3399 -
3400 - d = match_end;
3401 - dend = ((d >= string1 && d <= end1)
3402 - ? end_match_1 : end_match_2);
3403 -
3404 - for (mcnt = 1; mcnt < num_regs; mcnt++)
3405 - {
3406 - regstart[mcnt] = best_regstart[mcnt];
3407 - regend[mcnt] = best_regend[mcnt];
3408 - }
3409 - }
3410 - } /* d != end_match_2 */
3411 -
3412 - DEBUG_PRINT1 ("Accepting match.\n");
3413 -
3414 - /* If caller wants register contents data back, do it. */
3415 - if (regs && !bufp->no_sub)
3416 - {
3417 - /* Have the register data arrays been allocated? */
3418 - if (bufp->regs_allocated == REGS_UNALLOCATED)
3419 - { /* No. So allocate them with malloc. We need one
3420 - extra element beyond `num_regs' for the `-1' marker
3421 - GNU code uses. */
3422 - regs->num_regs = MAX (RE_NREGS, num_regs + 1);
3423 - regs->start = TALLOC (regs->num_regs, regoff_t);
3424 - regs->end = TALLOC (regs->num_regs, regoff_t);
3425 - if (regs->start == NULL || regs->end == NULL)
3426 - return -2;
3427 - bufp->regs_allocated = REGS_REALLOCATE;
3428 - }
3429 - else if (bufp->regs_allocated == REGS_REALLOCATE)
3430 - { /* Yes. If we need more elements than were already
3431 - allocated, reallocate them. If we need fewer, just
3432 - leave it alone. */
3433 - if (regs->num_regs < num_regs + 1)
3434 - {
3435 - regs->num_regs = num_regs + 1;
3436 - RETALLOC (regs->start, regs->num_regs, regoff_t);
3437 - RETALLOC (regs->end, regs->num_regs, regoff_t);
3438 - if (regs->start == NULL || regs->end == NULL)
3439 - return -2;
3440 - }
3441 - }
3442 - else
3443 - assert (bufp->regs_allocated == REGS_FIXED);
3444 -
3445 - /* Convert the pointer data in `regstart' and `regend' to
3446 - indices. Register zero has to be set differently,
3447 - since we haven't kept track of any info for it. */
3448 - if (regs->num_regs > 0)
3449 - {
3450 - regs->start[0] = pos;
3451 - regs->end[0] = (MATCHING_IN_FIRST_STRING ? d - string1
3452 - : d - string2 + size1);
3453 - }
3454 -
3455 - /* Go through the first `min (num_regs, regs->num_regs)'
3456 - registers, since that is all we initialized. */
3457 - for (mcnt = 1; mcnt < MIN (num_regs, regs->num_regs); mcnt++)
3458 - {
3459 - if (REG_UNSET (regstart[mcnt]) || REG_UNSET (regend[mcnt]))
3460 - regs->start[mcnt] = regs->end[mcnt] = -1;
3461 - else
3462 - {
3463 - regs->start[mcnt] = POINTER_TO_OFFSET (regstart[mcnt]);
3464 - regs->end[mcnt] = POINTER_TO_OFFSET (regend[mcnt]);
3465 - }
3466 - }
3467 -
3468 - /* If the regs structure we return has more elements than
3469 - were in the pattern, set the extra elements to -1. If
3470 - we (re)allocated the registers, this is the case,
3471 - because we always allocate enough to have at least one
3472 - -1 at the end. */
3473 - for (mcnt = num_regs; mcnt < regs->num_regs; mcnt++)
3474 - regs->start[mcnt] = regs->end[mcnt] = -1;
3475 - } /* regs && !bufp->no_sub */
3476 -
3477 - FREE_VARIABLES ();
3478 - DEBUG_PRINT4 ("%u failure points pushed, %u popped (%u remain).\n",
3479 - nfailure_points_pushed, nfailure_points_popped,
3480 - nfailure_points_pushed - nfailure_points_popped);
3481 - DEBUG_PRINT2 ("%u registers pushed.\n", num_regs_pushed);
3482 -
3483 - mcnt = d - pos - (MATCHING_IN_FIRST_STRING
3484 - ? string1
3485 - : string2 - size1);
3486 -
3487 - DEBUG_PRINT2 ("Returning %d from re_match_2.\n", mcnt);
3488 -
3489 - return mcnt;
3490 - }
3491 -
3492 - /* Otherwise match next pattern command. */
3493 -#ifdef SWITCH_ENUM_BUG
3494 - switch ((int) ((re_opcode_t) *p++))
3495 -#else
3496 - switch ((re_opcode_t) *p++)
3497 -#endif
3498 - {
3499 - /* Ignore these. Used to ignore the n of succeed_n's which
3500 - currently have n == 0. */
3501 - case no_op:
3502 - DEBUG_PRINT1 ("EXECUTING no_op.\n");
3503 - break;
3504 -
3505 -
3506 - /* Match the next n pattern characters exactly. The following
3507 - byte in the pattern defines n, and the n bytes after that
3508 - are the characters to match. */
3509 - case exactn:
3510 - mcnt = *p++;
3511 - DEBUG_PRINT2 ("EXECUTING exactn %d.\n", mcnt);
3512 -
3513 - /* This is written out as an if-else so we don't waste time
3514 - testing `translate' inside the loop. */
3515 - if (translate)
3516 - {
3517 - do
3518 - {
3519 - PREFETCH ();
3520 - if (translate[(unsigned char) *d++] != (char) *p++)
3521 - goto fail;
3522 - }
3523 - while (--mcnt);
3524 - }
3525 - else
3526 - {
3527 - do
3528 - {
3529 - PREFETCH ();
3530 - if (*d++ != (char) *p++) goto fail;
3531 - }
3532 - while (--mcnt);
3533 - }
3534 - SET_REGS_MATCHED ();
3535 - break;
3536 -
3537 -
3538 - /* Match any character except possibly a newline or a null. */
3539 - case anychar:
3540 - DEBUG_PRINT1 ("EXECUTING anychar.\n");
3541 -
3542 - PREFETCH ();
3543 -
3544 - if ((!(bufp->syntax & RE_DOT_NEWLINE) && TRANSLATE (*d) == '\n')
3545 - || (bufp->syntax & RE_DOT_NOT_NULL && TRANSLATE (*d) == '\000'))
3546 - goto fail;
3547 -
3548 - SET_REGS_MATCHED ();
3549 - DEBUG_PRINT2 (" Matched `%d'.\n", *d);
3550 - d++;
3551 - break;
3552 -
3553 -
3554 - case charset:
3555 - case charset_not:
3556 - {
3557 - register unsigned char c;
3558 - boolean not = (re_opcode_t) *(p - 1) == charset_not;
3559 -
3560 - DEBUG_PRINT2 ("EXECUTING charset%s.\n", not ? "_not" : "");
3561 -
3562 - PREFETCH ();
3563 - c = TRANSLATE (*d); /* The character to match. */
3564 -
3565 - /* Cast to `unsigned' instead of `unsigned char' in case the
3566 - bit list is a full 32 bytes long. */
3567 - if (c < (unsigned) (*p * BYTEWIDTH)
3568 - && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
3569 - not = !not;
3570 -
3571 - p += 1 + *p;
3572 -
3573 - if (!not) goto fail;
3574 -
3575 - SET_REGS_MATCHED ();
3576 - d++;
3577 - break;
3578 - }
3579 -
3580 -
3581 - /* The beginning of a group is represented by start_memory.
3582 - The arguments are the register number in the next byte, and the
3583 - number of groups inner to this one in the next. The text
3584 - matched within the group is recorded (in the internal
3585 - registers data structure) under the register number. */
3586 - case start_memory:
3587 - DEBUG_PRINT3 ("EXECUTING start_memory %d (%d):\n", *p, p[1]);
3588 -
3589 - /* Find out if this group can match the empty string. */
3590 - p1 = p; /* To send to group_match_null_string_p. */
3591 -
3592 - if (REG_MATCH_NULL_STRING_P (reg_info[*p]) == MATCH_NULL_UNSET_VALUE)
3593 - REG_MATCH_NULL_STRING_P (reg_info[*p])
3594 - = group_match_null_string_p (&p1, pend, reg_info);
3595 -
3596 - /* Save the position in the string where we were the last time
3597 - we were at this open-group operator in case the group is
3598 - operated upon by a repetition operator, e.g., with `(a*)*b'
3599 - against `ab'; then we want to ignore where we are now in
3600 - the string in case this attempt to match fails. */
3601 - old_regstart[*p] = REG_MATCH_NULL_STRING_P (reg_info[*p])
3602 - ? REG_UNSET (regstart[*p]) ? d : regstart[*p]
3603 - : regstart[*p];
3604 - DEBUG_PRINT2 (" old_regstart: %d\n",
3605 - POINTER_TO_OFFSET (old_regstart[*p]));
3606 -
3607 - regstart[*p] = d;
3608 - DEBUG_PRINT2 (" regstart: %d\n", POINTER_TO_OFFSET (regstart[*p]));
3609 -
3610 - IS_ACTIVE (reg_info[*p]) = 1;
3611 - MATCHED_SOMETHING (reg_info[*p]) = 0;
3612 -
3613 - /* This is the new highest active register. */
3614 - highest_active_reg = *p;
3615 -
3616 - /* If nothing was active before, this is the new lowest active
3617 - register. */
3618 - if (lowest_active_reg == NO_LOWEST_ACTIVE_REG)
3619 - lowest_active_reg = *p;
3620 -
3621 - /* Move past the register number and inner group count. */
3622 - p += 2;
3623 - break;
3624 -
3625 -
3626 - /* The stop_memory opcode represents the end of a group. Its
3627 - arguments are the same as start_memory's: the register
3628 - number, and the number of inner groups. */
3629 - case stop_memory:
3630 - DEBUG_PRINT3 ("EXECUTING stop_memory %d (%d):\n", *p, p[1]);
3631 -
3632 - /* We need to save the string position the last time we were at
3633 - this close-group operator in case the group is operated
3634 - upon by a repetition operator, e.g., with `((a*)*(b*)*)*'
3635 - against `aba'; then we want to ignore where we are now in
3636 - the string in case this attempt to match fails. */
3637 - old_regend[*p] = REG_MATCH_NULL_STRING_P (reg_info[*p])
3638 - ? REG_UNSET (regend[*p]) ? d : regend[*p]
3639 - : regend[*p];
3640 - DEBUG_PRINT2 (" old_regend: %d\n",
3641 - POINTER_TO_OFFSET (old_regend[*p]));
3642 -
3643 - regend[*p] = d;
3644 - DEBUG_PRINT2 (" regend: %d\n", POINTER_TO_OFFSET (regend[*p]));
3645 -
3646 - /* This register isn't active anymore. */
3647 - IS_ACTIVE (reg_info[*p]) = 0;
3648 -
3649 - /* If this was the only register active, nothing is active
3650 - anymore. */
3651 - if (lowest_active_reg == highest_active_reg)
3652 - {
3653 - lowest_active_reg = NO_LOWEST_ACTIVE_REG;
3654 - highest_active_reg = NO_HIGHEST_ACTIVE_REG;
3655 - }
3656 - else
3657 - { /* We must scan for the new highest active register, since
3658 - it isn't necessarily one less than now: consider
3659 - (a(b)c(d(e)f)g). When group 3 ends, after the f), the
3660 - new highest active register is 1. */
3661 - unsigned char r = *p - 1;
3662 - while (r > 0 && !IS_ACTIVE (reg_info[r]))
3663 - r--;
3664 -
3665 - /* If we end up at register zero, that means that we saved
3666 - the registers as the result of an `on_failure_jump', not
3667 - a `start_memory', and we jumped to past the innermost
3668 - `stop_memory'. For example, in ((.)*) we save
3669 - registers 1 and 2 as a result of the *, but when we pop
3670 - back to the second ), we are at the stop_memory 1.
3671 - Thus, nothing is active. */
3672 - if (r == 0)
3673 - {
3674 - lowest_active_reg = NO_LOWEST_ACTIVE_REG;
3675 - highest_active_reg = NO_HIGHEST_ACTIVE_REG;
3676 - }
3677 - else
3678 - highest_active_reg = r;
3679 - }
3680 -
3681 - /* If just failed to match something this time around with a
3682 - group that's operated on by a repetition operator, try to
3683 - force exit from the ``loop'', and restore the register
3684 - information for this group that we had before trying this
3685 - last match. */
3686 - if ((!MATCHED_SOMETHING (reg_info[*p])
3687 - || (re_opcode_t) p[-3] == start_memory)
3688 - && (p + 2) < pend)
3689 - {
3690 - boolean is_a_jump_n = false;
3691 -
3692 - p1 = p + 2;
3693 - mcnt = 0;
3694 - switch ((re_opcode_t) *p1++)
3695 - {
3696 - case jump_n:
3697 - is_a_jump_n = true;
3698 - case pop_failure_jump:
3699 - case maybe_pop_jump:
3700 - case jump:
3701 - case dummy_failure_jump:
3702 - EXTRACT_NUMBER_AND_INCR (mcnt, p1);
3703 - if (is_a_jump_n)
3704 - p1 += 2;
3705 - break;
3706 -
3707 - default:
3708 - /* do nothing */ ;
3709 - }
3710 - p1 += mcnt;
3711 -
3712 - /* If the next operation is a jump backwards in the pattern
3713 - to an on_failure_jump right before the start_memory
3714 - corresponding to this stop_memory, exit from the loop
3715 - by forcing a failure after pushing on the stack the
3716 - on_failure_jump's jump in the pattern, and d. */
3717 - if (mcnt < 0 && (re_opcode_t) *p1 == on_failure_jump
3718 - && (re_opcode_t) p1[3] == start_memory && p1[4] == *p)
3719 - {
3720 - /* If this group ever matched anything, then restore
3721 - what its registers were before trying this last
3722 - failed match, e.g., with `(a*)*b' against `ab' for
3723 - regstart[1], and, e.g., with `((a*)*(b*)*)*'
3724 - against `aba' for regend[3].
3725 -
3726 - Also restore the registers for inner groups for,
3727 - e.g., `((a*)(b*))*' against `aba' (register 3 would
3728 - otherwise get trashed). */
3729 -
3730 - if (EVER_MATCHED_SOMETHING (reg_info[*p]))
3731 - {
3732 - unsigned r;
3733 -
3734 - EVER_MATCHED_SOMETHING (reg_info[*p]) = 0;
3735 -
3736 - /* Restore this and inner groups' (if any) registers. */
3737 - for (r = *p; r < *p + *(p + 1); r++)
3738 - {
3739 - regstart[r] = old_regstart[r];
3740 -
3741 - /* xx why this test? */
3742 - if ((int) old_regend[r] >= (int) regstart[r])
3743 - regend[r] = old_regend[r];
3744 - }
3745 - }
3746 - p1++;
3747 - EXTRACT_NUMBER_AND_INCR (mcnt, p1);
3748 - PUSH_FAILURE_POINT (p1 + mcnt, d, -2);
3749 -
3750 - goto fail;
3751 - }
3752 - }
3753 -
3754 - /* Move past the register number and the inner group count. */
3755 - p += 2;
3756 - break;
3757 -
3758 -
3759 - /* \<digit> has been turned into a `duplicate' command which is
3760 - followed by the numeric value of <digit> as the register number. */
3761 - case duplicate:
3762 - {
3763 - register const char *d2, *dend2;
3764 - int regno = *p++; /* Get which register to match against. */
3765 - DEBUG_PRINT2 ("EXECUTING duplicate %d.\n", regno);
3766 -
3767 - /* Can't back reference a group which we've never matched. */
3768 - if (REG_UNSET (regstart[regno]) || REG_UNSET (regend[regno]))
3769 - goto fail;
3770 -
3771 - /* Where in input to try to start matching. */
3772 - d2 = regstart[regno];
3773 -
3774 - /* Where to stop matching; if both the place to start and
3775 - the place to stop matching are in the same string, then
3776 - set to the place to stop, otherwise, for now have to use
3777 - the end of the first string. */
3778 -
3779 - dend2 = ((FIRST_STRING_P (regstart[regno])
3780 - == FIRST_STRING_P (regend[regno]))
3781 - ? regend[regno] : end_match_1);
3782 - for (;;)
3783 - {
3784 - /* If necessary, advance to next segment in register
3785 - contents. */
3786 - while (d2 == dend2)
3787 - {
3788 - if (dend2 == end_match_2) break;
3789 - if (dend2 == regend[regno]) break;
3790 -
3791 - /* End of string1 => advance to string2. */
3792 - d2 = string2;
3793 - dend2 = regend[regno];
3794 - }
3795 - /* At end of register contents => success */
3796 - if (d2 == dend2) break;
3797 -
3798 - /* If necessary, advance to next segment in data. */
3799 - PREFETCH ();
3800 -
3801 - /* How many characters left in this segment to match. */
3802 - mcnt = dend - d;
3803 -
3804 - /* Want how many consecutive characters we can match in
3805 - one shot, so, if necessary, adjust the count. */
3806 - if (mcnt > dend2 - d2)
3807 - mcnt = dend2 - d2;
3808 -
3809 - /* Compare that many; failure if mismatch, else move
3810 - past them. */
3811 - if (translate
3812 - ? bcmp_translate (d, d2, mcnt, translate)
3813 - : bcmp (d, d2, mcnt))
3814 - goto fail;
3815 - d += mcnt, d2 += mcnt;
3816 - }
3817 - }
3818 - break;
3819 -
3820 -
3821 - /* begline matches the empty string at the beginning of the string
3822 - (unless `not_bol' is set in `bufp'), and, if
3823 - `newline_anchor' is set, after newlines. */
3824 - case begline:
3825 - DEBUG_PRINT1 ("EXECUTING begline.\n");
3826 -
3827 - if (AT_STRINGS_BEG (d))
3828 - {
3829 - if (!bufp->not_bol) break;
3830 - }
3831 - else if (d[-1] == '\n' && bufp->newline_anchor)
3832 - {
3833 - break;
3834 - }
3835 - /* In all other cases, we fail. */
3836 - goto fail;
3837 -
3838 -
3839 - /* endline is the dual of begline. */
3840 - case endline:
3841 - DEBUG_PRINT1 ("EXECUTING endline.\n");
3842 -
3843 - if (AT_STRINGS_END (d))
3844 - {
3845 - if (!bufp->not_eol) break;
3846 - }
3847 -
3848 - /* We have to ``prefetch'' the next character. */
3849 - else if ((d == end1 ? *string2 : *d) == '\n'
3850 - && bufp->newline_anchor)
3851 - {
3852 - break;
3853 - }
3854 - goto fail;
3855 -
3856 -
3857 - /* Match at the very beginning of the data. */
3858 - case begbuf:
3859 - DEBUG_PRINT1 ("EXECUTING begbuf.\n");
3860 - if (AT_STRINGS_BEG (d))
3861 - break;
3862 - goto fail;
3863 -
3864 -
3865 - /* Match at the very end of the data. */
3866 - case endbuf:
3867 - DEBUG_PRINT1 ("EXECUTING endbuf.\n");
3868 - if (AT_STRINGS_END (d))
3869 - break;
3870 - goto fail;
3871 -
3872 -
3873 - /* on_failure_keep_string_jump is used to optimize `.*\n'. It
3874 - pushes NULL as the value for the string on the stack. Then
3875 - `pop_failure_point' will keep the current value for the
3876 - string, instead of restoring it. To see why, consider
3877 - matching `foo\nbar' against `.*\n'. The .* matches the foo;
3878 - then the . fails against the \n. But the next thing we want
3879 - to do is match the \n against the \n; if we restored the
3880 - string value, we would be back at the foo.
3881 -
3882 - Because this is used only in specific cases, we don't need to
3883 - check all the things that `on_failure_jump' does, to make
3884 - sure the right things get saved on the stack. Hence we don't
3885 - share its code. The only reason to push anything on the
3886 - stack at all is that otherwise we would have to change
3887 - `anychar's code to do something besides goto fail in this
3888 - case; that seems worse than this. */
3889 - case on_failure_keep_string_jump:
3890 - DEBUG_PRINT1 ("EXECUTING on_failure_keep_string_jump");
3891 -
3892 - EXTRACT_NUMBER_AND_INCR (mcnt, p);
3893 - DEBUG_PRINT3 (" %d (to 0x%x):\n", mcnt, p + mcnt);
3894 -
3895 - PUSH_FAILURE_POINT (p + mcnt, NULL, -2);
3896 - break;
3897 -
3898 -
3899 - /* Uses of on_failure_jump:
3900 -
3901 - Each alternative starts with an on_failure_jump that points
3902 - to the beginning of the next alternative. Each alternative
3903 - except the last ends with a jump that in effect jumps past
3904 - the rest of the alternatives. (They really jump to the
3905 - ending jump of the following alternative, because tensioning
3906 - these jumps is a hassle.)
3907 -
3908 - Repeats start with an on_failure_jump that points past both
3909 - the repetition text and either the following jump or
3910 - pop_failure_jump back to this on_failure_jump. */
3911 - case on_failure_jump:
3912 - on_failure:
3913 - DEBUG_PRINT1 ("EXECUTING on_failure_jump");
3914 -
3915 - EXTRACT_NUMBER_AND_INCR (mcnt, p);
3916 - DEBUG_PRINT3 (" %d (to 0x%x)", mcnt, p + mcnt);
3917 -
3918 - /* If this on_failure_jump comes right before a group (i.e.,
3919 - the original * applied to a group), save the information
3920 - for that group and all inner ones, so that if we fail back
3921 - to this point, the group's information will be correct.
3922 - For example, in \(a*\)*\1, we need the preceding group,
3923 - and in \(\(a*\)b*\)\2, we need the inner group. */
3924 -
3925 - /* We can't use `p' to check ahead because we push
3926 - a failure point to `p + mcnt' after we do this. */
3927 - p1 = p;
3928 -
3929 - /* We need to skip no_op's before we look for the
3930 - start_memory in case this on_failure_jump is happening as
3931 - the result of a completed succeed_n, as in \(a\)\{1,3\}b\1
3932 - against aba. */
3933 - while (p1 < pend && (re_opcode_t) *p1 == no_op)
3934 - p1++;
3935 -
3936 - if (p1 < pend && (re_opcode_t) *p1 == start_memory)
3937 - {
3938 - /* We have a new highest active register now. This will
3939 - get reset at the start_memory we are about to get to,
3940 - but we will have saved all the registers relevant to
3941 - this repetition op, as described above. */
3942 - highest_active_reg = *(p1 + 1) + *(p1 + 2);
3943 - if (lowest_active_reg == NO_LOWEST_ACTIVE_REG)
3944 - lowest_active_reg = *(p1 + 1);
3945 - }
3946 -
3947 - DEBUG_PRINT1 (":\n");
3948 - PUSH_FAILURE_POINT (p + mcnt, d, -2);
3949 - break;
3950 -
3951 -
3952 - /* A smart repeat ends with `maybe_pop_jump'.
3953 - We change it to either `pop_failure_jump' or `jump'. */
3954 - case maybe_pop_jump:
3955 - EXTRACT_NUMBER_AND_INCR (mcnt, p);
3956 - DEBUG_PRINT2 ("EXECUTING maybe_pop_jump %d.\n", mcnt);
3957 - {
3958 - register unsigned char *p2 = p;
3959 -
3960 - /* Compare the beginning of the repeat with what in the
3961 - pattern follows its end. If we can establish that there
3962 - is nothing that they would both match, i.e., that we
3963 - would have to backtrack because of (as in, e.g., `a*a')
3964 - then we can change to pop_failure_jump, because we'll
3965 - never have to backtrack.
3966 -
3967 - This is not true in the case of alternatives: in
3968 - `(a|ab)*' we do need to backtrack to the `ab' alternative
3969 - (e.g., if the string was `ab'). But instead of trying to
3970 - detect that here, the alternative has put on a dummy
3971 - failure point which is what we will end up popping. */
3972 -
3973 - /* Skip over open/close-group commands. */
3974 - while (p2 + 2 < pend
3975 - && ((re_opcode_t) *p2 == stop_memory
3976 - || (re_opcode_t) *p2 == start_memory))
3977 - p2 += 3; /* Skip over args, too. */
3978 -
3979 - /* If we're at the end of the pattern, we can change. */
3980 - if (p2 == pend)
3981 - {
3982 - /* Consider what happens when matching ":\(.*\)"
3983 - against ":/". I don't really understand this code
3984 - yet. */
3985 - p[-3] = (unsigned char) pop_failure_jump;
3986 - DEBUG_PRINT1
3987 - (" End of pattern: change to `pop_failure_jump'.\n");
3988 - }
3989 -
3990 - else if ((re_opcode_t) *p2 == exactn
3991 - || (bufp->newline_anchor && (re_opcode_t) *p2 == endline))
3992 - {
3993 - register unsigned char c
3994 - = *p2 == (unsigned char) endline ? '\n' : p2[2];
3995 - p1 = p + mcnt;
3996 -
3997 - /* p1[0] ... p1[2] are the `on_failure_jump' corresponding
3998 - to the `maybe_finalize_jump' of this case. Examine what
3999 - follows. */
4000 - if ((re_opcode_t) p1[3] == exactn && p1[5] != c)
4001 - {
4002 - p[-3] = (unsigned char) pop_failure_jump;
4003 - DEBUG_PRINT3 (" %c != %c => pop_failure_jump.\n",
4004 - c, p1[5]);
4005 - }
4006 -
4007 - else if ((re_opcode_t) p1[3] == charset
4008 - || (re_opcode_t) p1[3] == charset_not)
4009 - {
4010 - int not = (re_opcode_t) p1[3] == charset_not;
4011 -
4012 - if (c < (unsigned char) (p1[4] * BYTEWIDTH)
4013 - && p1[5 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
4014 - not = !not;
4015 -
4016 - /* `not' is equal to 1 if c would match, which means
4017 - that we can't change to pop_failure_jump. */
4018 - if (!not)
4019 - {
4020 - p[-3] = (unsigned char) pop_failure_jump;
4021 - DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
4022 - }
4023 - }
4024 - }
4025 - }
4026 - p -= 2; /* Point at relative address again. */
4027 - if ((re_opcode_t) p[-1] != pop_failure_jump)
4028 - {
4029 - p[-1] = (unsigned char) jump;
4030 - DEBUG_PRINT1 (" Match => jump.\n");
4031 - goto unconditional_jump;
4032 - }
4033 - /* Note fall through. */
4034 -
4035 -
4036 - /* The end of a simple repeat has a pop_failure_jump back to
4037 - its matching on_failure_jump, where the latter will push a
4038 - failure point. The pop_failure_jump takes off failure
4039 - points put on by this pop_failure_jump's matching
4040 - on_failure_jump; we got through the pattern to here from the
4041 - matching on_failure_jump, so didn't fail. */
4042 - case pop_failure_jump:
4043 - {
4044 - /* We need to pass separate storage for the lowest and
4045 - highest registers, even though we don't care about the
4046 - actual values. Otherwise, we will restore only one
4047 - register from the stack, since lowest will == highest in
4048 - `pop_failure_point'. */
4049 - unsigned dummy_low_reg, dummy_high_reg;
4050 - unsigned char *pdummy;
4051 - const char *sdummy;
4052 -
4053 - DEBUG_PRINT1 ("EXECUTING pop_failure_jump.\n");
4054 - POP_FAILURE_POINT (sdummy, pdummy,
4055 - dummy_low_reg, dummy_high_reg,
4056 - reg_dummy, reg_dummy, reg_info_dummy);
4057 - }
4058 - /* Note fall through. */
4059 -
4060 -
4061 - /* Unconditionally jump (without popping any failure points). */
4062 - case jump:
4063 - unconditional_jump:
4064 - EXTRACT_NUMBER_AND_INCR (mcnt, p); /* Get the amount to jump. */
4065 - DEBUG_PRINT2 ("EXECUTING jump %d ", mcnt);
4066 - p += mcnt; /* Do the jump. */
4067 - DEBUG_PRINT2 ("(to 0x%x).\n", p);
4068 - break;
4069 -
4070 -
4071 - /* We need this opcode so we can detect where alternatives end
4072 - in `group_match_null_string_p' et al. */
4073 - case jump_past_alt:
4074 - DEBUG_PRINT1 ("EXECUTING jump_past_alt.\n");
4075 - goto unconditional_jump;
4076 -
4077 -
4078 - /* Normally, the on_failure_jump pushes a failure point, which
4079 - then gets popped at pop_failure_jump. We will end up at
4080 - pop_failure_jump, also, and with a pattern of, say, `a+', we
4081 - are skipping over the on_failure_jump, so we have to push
4082 - something meaningless for pop_failure_jump to pop. */
4083 - case dummy_failure_jump:
4084 - DEBUG_PRINT1 ("EXECUTING dummy_failure_jump.\n");
4085 - /* It doesn't matter what we push for the string here. What
4086 - the code at `fail' tests is the value for the pattern. */
4087 - PUSH_FAILURE_POINT (0, 0, -2);
4088 - goto unconditional_jump;
4089 -
4090 -
4091 - /* At the end of an alternative, we need to push a dummy failure
4092 - point in case we are followed by a `pop_failure_jump', because
4093 - we don't want the failure point for the alternative to be
4094 - popped. For example, matching `(a|ab)*' against `aab'
4095 - requires that we match the `ab' alternative. */
4096 - case push_dummy_failure:
4097 - DEBUG_PRINT1 ("EXECUTING push_dummy_failure.\n");
4098 - /* See comments just above at `dummy_failure_jump' about the
4099 - two zeroes. */
4100 - PUSH_FAILURE_POINT (0, 0, -2);
4101 - break;
4102 -
4103 - /* Have to succeed matching what follows at least n times.
4104 - After that, handle like `on_failure_jump'. */
4105 - case succeed_n:
4106 - EXTRACT_NUMBER (mcnt, p + 2);
4107 - DEBUG_PRINT2 ("EXECUTING succeed_n %d.\n", mcnt);
4108 -
4109 - assert (mcnt >= 0);
4110 - /* Originally, this is how many times we HAVE to succeed. */
4111 - if (mcnt > 0)
4112 - {
4113 - mcnt--;
4114 - p += 2;
4115 - STORE_NUMBER_AND_INCR (p, mcnt);
4116 - DEBUG_PRINT3 (" Setting 0x%x to %d.\n", p, mcnt);
4117 - }
4118 - else if (mcnt == 0)
4119 - {
4120 - DEBUG_PRINT2 (" Setting two bytes from 0x%x to no_op.\n", p+2);
4121 - p[2] = (unsigned char) no_op;
4122 - p[3] = (unsigned char) no_op;
4123 - goto on_failure;
4124 - }
4125 - break;
4126 -
4127 - case jump_n:
4128 - EXTRACT_NUMBER (mcnt, p + 2);
4129 - DEBUG_PRINT2 ("EXECUTING jump_n %d.\n", mcnt);
4130 -
4131 - /* Originally, this is how many times we CAN jump. */
4132 - if (mcnt)
4133 - {
4134 - mcnt--;
4135 - STORE_NUMBER (p + 2, mcnt);
4136 - goto unconditional_jump;
4137 - }
4138 - /* If don't have to jump any more, skip over the rest of command. */
4139 - else
4140 - p += 4;
4141 - break;
4142 -
4143 - case set_number_at:
4144 - {
4145 - DEBUG_PRINT1 ("EXECUTING set_number_at.\n");
4146 -
4147 - EXTRACT_NUMBER_AND_INCR (mcnt, p);
4148 - p1 = p + mcnt;
4149 - EXTRACT_NUMBER_AND_INCR (mcnt, p);
4150 - DEBUG_PRINT3 (" Setting 0x%x to %d.\n", p1, mcnt);
4151 - STORE_NUMBER (p1, mcnt);
4152 - break;
4153 - }
4154 -
4155 - case wordbound:
4156 - DEBUG_PRINT1 ("EXECUTING wordbound.\n");
4157 - if (AT_WORD_BOUNDARY (d))
4158 - break;
4159 - goto fail;
4160 -
4161 - case notwordbound:
4162 - DEBUG_PRINT1 ("EXECUTING notwordbound.\n");
4163 - if (AT_WORD_BOUNDARY (d))
4164 - goto fail;
4165 - break;
4166 -
4167 - case wordbeg:
4168 - DEBUG_PRINT1 ("EXECUTING wordbeg.\n");
4169 - if (WORDCHAR_P (d) && (AT_STRINGS_BEG (d) || !WORDCHAR_P (d - 1)))
4170 - break;
4171 - goto fail;
4172 -
4173 - case wordend:
4174 - DEBUG_PRINT1 ("EXECUTING wordend.\n");
4175 - if (!AT_STRINGS_BEG (d) && WORDCHAR_P (d - 1)
4176 - && (!WORDCHAR_P (d) || AT_STRINGS_END (d)))
4177 - break;
4178 - goto fail;
4179 -
4180 -#ifdef emacs
4181 -#ifdef emacs19
4182 - case before_dot:
4183 - DEBUG_PRINT1 ("EXECUTING before_dot.\n");
4184 - if (PTR_CHAR_POS ((unsigned char *) d) >= point)
4185 - goto fail;
4186 - break;
4187 -
4188 - case at_dot:
4189 - DEBUG_PRINT1 ("EXECUTING at_dot.\n");
4190 - if (PTR_CHAR_POS ((unsigned char *) d) != point)
4191 - goto fail;
4192 - break;
4193 -
4194 - case after_dot:
4195 - DEBUG_PRINT1 ("EXECUTING after_dot.\n");
4196 - if (PTR_CHAR_POS ((unsigned char *) d) <= point)
4197 - goto fail;
4198 - break;
4199 -#else /* not emacs19 */
4200 - case at_dot:
4201 - DEBUG_PRINT1 ("EXECUTING at_dot.\n");
4202 - if (PTR_CHAR_POS ((unsigned char *) d) + 1 != point)
4203 - goto fail;
4204 - break;
4205 -#endif /* not emacs19 */
4206 -
4207 - case syntaxspec:
4208 - DEBUG_PRINT2 ("EXECUTING syntaxspec %d.\n", mcnt);
4209 - mcnt = *p++;
4210 - goto matchsyntax;
4211 -
4212 - case wordchar:
4213 - DEBUG_PRINT1 ("EXECUTING Emacs wordchar.\n");
4214 - mcnt = (int) Sword;
4215 - matchsyntax:
4216 - PREFETCH ();
4217 - if (SYNTAX (*d++) != (enum syntaxcode) mcnt)
4218 - goto fail;
4219 - SET_REGS_MATCHED ();
4220 - break;
4221 -
4222 - case notsyntaxspec:
4223 - DEBUG_PRINT2 ("EXECUTING notsyntaxspec %d.\n", mcnt);
4224 - mcnt = *p++;
4225 - goto matchnotsyntax;
4226 -
4227 - case notwordchar:
4228 - DEBUG_PRINT1 ("EXECUTING Emacs notwordchar.\n");
4229 - mcnt = (int) Sword;
4230 - matchnotsyntax:
4231 - PREFETCH ();
4232 - if (SYNTAX (*d++) == (enum syntaxcode) mcnt)
4233 - goto fail;
4234 - SET_REGS_MATCHED ();
4235 - break;
4236 -
4237 -#else /* not emacs */
4238 - case wordchar:
4239 - DEBUG_PRINT1 ("EXECUTING non-Emacs wordchar.\n");
4240 - PREFETCH ();
4241 - if (!WORDCHAR_P (d))
4242 - goto fail;
4243 - SET_REGS_MATCHED ();
4244 - d++;
4245 - break;
4246 -
4247 - case notwordchar:
4248 - DEBUG_PRINT1 ("EXECUTING non-Emacs notwordchar.\n");
4249 - PREFETCH ();
4250 - if (WORDCHAR_P (d))
4251 - goto fail;
4252 - SET_REGS_MATCHED ();
4253 - d++;
4254 - break;
4255 -#endif /* not emacs */
4256 -
4257 - default:
4258 - abort ();
4259 - }
4260 - continue; /* Successfully executed one pattern command; keep going. */
4261 -
4262 -
4263 - /* We goto here if a matching operation fails. */
4264 - fail:
4265 - if (!FAIL_STACK_EMPTY ())
4266 - { /* A restart point is known. Restore to that state. */
4267 - DEBUG_PRINT1 ("\nFAIL:\n");
4268 - POP_FAILURE_POINT (d, p,
4269 - lowest_active_reg, highest_active_reg,
4270 - regstart, regend, reg_info);
4271 -
4272 - /* If this failure point is a dummy, try the next one. */
4273 - if (!p)
4274 - goto fail;
4275 -
4276 - /* If we failed to the end of the pattern, don't examine *p. */
4277 - assert (p <= pend);
4278 - if (p < pend)
4279 - {
4280 - boolean is_a_jump_n = false;
4281 -
4282 - /* If failed to a backwards jump that's part of a repetition
4283 - loop, need to pop this failure point and use the next one. */
4284 - switch ((re_opcode_t) *p)
4285 - {
4286 - case jump_n:
4287 - is_a_jump_n = true;
4288 - case maybe_pop_jump:
4289 - case pop_failure_jump:
4290 - case jump:
4291 - p1 = p + 1;
4292 - EXTRACT_NUMBER_AND_INCR (mcnt, p1);
4293 - p1 += mcnt;
4294 -
4295 - if ((is_a_jump_n && (re_opcode_t) *p1 == succeed_n)
4296 - || (!is_a_jump_n
4297 - && (re_opcode_t) *p1 == on_failure_jump))
4298 - goto fail;
4299 - break;
4300 - default:
4301 - /* do nothing */ ;
4302 - }
4303 - }
4304 -
4305 - if (d >= string1 && d <= end1)
4306 - dend = end_match_1;
4307 - }
4308 - else
4309 - break; /* Matching at this starting point really fails. */
4310 - } /* for (;;) */
4311 -
4312 - if (best_regs_set)
4313 - goto restore_best_regs;
4314 -
4315 - FREE_VARIABLES ();
4316 -
4317 - return -1; /* Failure to match. */
4318 -} /* re_match_2 */
4319 -
4320 -/* Subroutine definitions for re_match_2. */
4321 -
4322 -
4323 -/* We are passed P pointing to a register number after a start_memory.
4324 -
4325 - Return true if the pattern up to the corresponding stop_memory can
4326 - match the empty string, and false otherwise.
4327 -
4328 - If we find the matching stop_memory, sets P to point to one past its number.
4329 - Otherwise, sets P to an undefined byte less than or equal to END.
4330 -
4331 - We don't handle duplicates properly (yet). */
4332 -
4333 -static boolean
4334 -group_match_null_string_p (p, end, reg_info)
4335 - unsigned char **p, *end;
4336 - register_info_type *reg_info;
4337 -{
4338 - int mcnt;
4339 - /* Point to after the args to the start_memory. */
4340 - unsigned char *p1 = *p + 2;
4341 -
4342 - while (p1 < end)
4343 - {
4344 - /* Skip over opcodes that can match nothing, and return true or
4345 - false, as appropriate, when we get to one that can't, or to the
4346 - matching stop_memory. */
4347 -
4348 - switch ((re_opcode_t) *p1)
4349 - {
4350 - /* Could be either a loop or a series of alternatives. */
4351 - case on_failure_jump:
4352 - p1++;
4353 - EXTRACT_NUMBER_AND_INCR (mcnt, p1);
4354 -
4355 - /* If the next operation is not a jump backwards in the
4356 - pattern. */
4357 -
4358 - if (mcnt >= 0)
4359 - {
4360 - /* Go through the on_failure_jumps of the alternatives,
4361 - seeing if any of the alternatives cannot match nothing.
4362 - The last alternative starts with only a jump,
4363 - whereas the rest start with on_failure_jump and end
4364 - with a jump, e.g., here is the pattern for `a|b|c':
4365 -
4366 - /on_failure_jump/0/6/exactn/1/a/jump_past_alt/0/6
4367 - /on_failure_jump/0/6/exactn/1/b/jump_past_alt/0/3
4368 - /exactn/1/c
4369 -
4370 - So, we have to first go through the first (n-1)
4371 - alternatives and then deal with the last one separately. */
4372 -
4373 -
4374 - /* Deal with the first (n-1) alternatives, which start
4375 - with an on_failure_jump (see above) that jumps to right
4376 - past a jump_past_alt. */
4377 -
4378 - while ((re_opcode_t) p1[mcnt-3] == jump_past_alt)
4379 - {
4380 - /* `mcnt' holds how many bytes long the alternative
4381 - is, including the ending `jump_past_alt' and
4382 - its number. */
4383 -
4384 - if (!alt_match_null_string_p (p1, p1 + mcnt - 3,
4385 - reg_info))
4386 - return false;
4387 -
4388 - /* Move to right after this alternative, including the
4389 - jump_past_alt. */
4390 - p1 += mcnt;
4391 -
4392 - /* Break if it's the beginning of an n-th alternative
4393 - that doesn't begin with an on_failure_jump. */
4394 - if ((re_opcode_t) *p1 != on_failure_jump)
4395 - break;
4396 -
4397 - /* Still have to check that it's not an n-th
4398 - alternative that starts with an on_failure_jump. */
4399 - p1++;
4400 - EXTRACT_NUMBER_AND_INCR (mcnt, p1);
4401 - if ((re_opcode_t) p1[mcnt-3] != jump_past_alt)
4402 - {
4403 - /* Get to the beginning of the n-th alternative. */
4404 - p1 -= 3;
4405 - break;
4406 - }
4407 - }
4408 -
4409 - /* Deal with the last alternative: go back and get number
4410 - of the `jump_past_alt' just before it. `mcnt' contains
4411 - the length of the alternative. */
4412 - EXTRACT_NUMBER (mcnt, p1 - 2);
4413 -
4414 - if (!alt_match_null_string_p (p1, p1 + mcnt, reg_info))
4415 - return false;
4416 -
4417 - p1 += mcnt; /* Get past the n-th alternative. */
4418 - } /* if mcnt > 0 */
4419 - break;
4420 -
4421 -
4422 - case stop_memory:
4423 - assert (p1[1] == **p);
4424 - *p = p1 + 2;
4425 - return true;
4426 -
4427 -
4428 - default:
4429 - if (!common_op_match_null_string_p (&p1, end, reg_info))
4430 - return false;
4431 - }
4432 - } /* while p1 < end */
4433 -
4434 - return false;
4435 -} /* group_match_null_string_p */
4436 -
4437 -
4438 -/* Similar to group_match_null_string_p, but doesn't deal with alternatives:
4439 - It expects P to be the first byte of a single alternative and END one
4440 - byte past the last. The alternative can contain groups. */
4441 -
4442 -static boolean
4443 -alt_match_null_string_p (p, end, reg_info)
4444 - unsigned char *p, *end;
4445 - register_info_type *reg_info;
4446 -{
4447 - int mcnt;
4448 - unsigned char *p1 = p;
4449 -
4450 - while (p1 < end)
4451 - {
4452 - /* Skip over opcodes that can match nothing, and break when we get
4453 - to one that can't. */
4454 -
4455 - switch ((re_opcode_t) *p1)
4456 - {
4457 - /* It's a loop. */
4458 - case on_failure_jump:
4459 - p1++;
4460 - EXTRACT_NUMBER_AND_INCR (mcnt, p1);
4461 - p1 += mcnt;
4462 - break;
4463 -
4464 - default:
4465 - if (!common_op_match_null_string_p (&p1, end, reg_info))
4466 - return false;
4467 - }
4468 - } /* while p1 < end */
4469 -
4470 - return true;
4471 -} /* alt_match_null_string_p */
4472 -
4473 -
4474 -/* Deals with the ops common to group_match_null_string_p and
4475 - alt_match_null_string_p.
4476 -
4477 - Sets P to one after the op and its arguments, if any. */
4478 -
4479 -static boolean
4480 -common_op_match_null_string_p (p, end, reg_info)
4481 - unsigned char **p, *end;
4482 - register_info_type *reg_info;
4483 -{
4484 - int mcnt;
4485 - boolean ret;
4486 - int reg_no;
4487 - unsigned char *p1 = *p;
4488 -
4489 - switch ((re_opcode_t) *p1++)
4490 - {
4491 - case no_op:
4492 - case begline:
4493 - case endline:
4494 - case begbuf:
4495 - case endbuf:
4496 - case wordbeg:
4497 - case wordend:
4498 - case wordbound:
4499 - case notwordbound:
4500 -#ifdef emacs
4501 - case before_dot:
4502 - case at_dot:
4503 - case after_dot:
4504 -#endif
4505 - break;
4506 -
4507 - case start_memory:
4508 - reg_no = *p1;
4509 - assert (reg_no > 0 && reg_no <= MAX_REGNUM);
4510 - ret = group_match_null_string_p (&p1, end, reg_info);
4511 -
4512 - /* Have to set this here in case we're checking a group which
4513 - contains a group and a back reference to it. */
4514 -
4515 - if (REG_MATCH_NULL_STRING_P (reg_info[reg_no]) == MATCH_NULL_UNSET_VALUE)
4516 - REG_MATCH_NULL_STRING_P (reg_info[reg_no]) = ret;
4517 -
4518 - if (!ret)
4519 - return false;
4520 - break;
4521 -
4522 - /* If this is an optimized succeed_n for zero times, make the jump. */
4523 - case jump:
4524 - EXTRACT_NUMBER_AND_INCR (mcnt, p1);
4525 - if (mcnt >= 0)
4526 - p1 += mcnt;
4527 - else
4528 - return false;
4529 - break;
4530 -
4531 - case succeed_n:
4532 - /* Get to the number of times to succeed. */
4533 - p1 += 2;
4534 - EXTRACT_NUMBER_AND_INCR (mcnt, p1);
4535 -
4536 - if (mcnt == 0)
4537 - {
4538 - p1 -= 4;
4539 - EXTRACT_NUMBER_AND_INCR (mcnt, p1);
4540 - p1 += mcnt;
4541 - }
4542 - else
4543 - return false;
4544 - break;
4545 -
4546 - case duplicate:
4547 - if (!REG_MATCH_NULL_STRING_P (reg_info[*p1]))
4548 - return false;
4549 - break;
4550 -
4551 - case set_number_at:
4552 - p1 += 4;
4553 -
4554 - default:
4555 - /* All other opcodes mean we cannot match the empty string. */
4556 - return false;
4557 - }
4558 -
4559 - *p = p1;
4560 - return true;
4561 -} /* common_op_match_null_string_p */
4562 -
4563 -
4564 -/* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
4565 - bytes; nonzero otherwise. */
4566 -
4567 -static int
4568 -bcmp_translate(
4569 - unsigned char *s1,
4570 - unsigned char *s2,
4571 - int len,
4572 - char *translate
4573 -)
4574 -{
4575 - register unsigned char *p1 = s1, *p2 = s2;
4576 - while (len)
4577 - {
4578 - if (translate[*p1++] != translate[*p2++]) return 1;
4579 - len--;
4580 - }
4581 - return 0;
4582 -}
4583 -
4584 -/* Entry points for GNU code. */
4585 -
4586 -/* re_compile_pattern is the GNU regular expression compiler: it
4587 - compiles PATTERN (of length SIZE) and puts the result in BUFP.
4588 - Returns 0 if the pattern was valid, otherwise an error string.
4589 -
4590 - Assumes the `allocated' (and perhaps `buffer') and `translate' fields
4591 - are set in BUFP on entry.
4592 -
4593 - We call regex_compile to do the actual compilation. */
4594 -
4595 -const char *
4596 -re_compile_pattern (pattern, length, bufp)
4597 - const char *pattern;
4598 - int length;
4599 - struct re_pattern_buffer *bufp;
4600 -{
4601 - reg_errcode_t ret;
4602 -
4603 - /* GNU code is written to assume at least RE_NREGS registers will be set
4604 - (and at least one extra will be -1). */
4605 - bufp->regs_allocated = REGS_UNALLOCATED;
4606 -
4607 - /* And GNU code determines whether or not to get register information
4608 - by passing null for the REGS argument to re_match, etc., not by
4609 - setting no_sub. */
4610 - bufp->no_sub = 0;
4611 -
4612 - /* Match anchors at newline. */
4613 - bufp->newline_anchor = 1;
4614 -
4615 - ret = regex_compile (pattern, length, re_syntax_options, bufp);
4616 -
4617 - return re_error_msg[(int) ret];
4618 -}
4619 -
4620 -/* Entry points compatible with 4.2 BSD regex library. We don't define
4621 - them if this is an Emacs or POSIX compilation. */
4622 -
4623 -#if !defined (emacs) && !defined (_POSIX_SOURCE)
4624 -
4625 -/* BSD has one and only one pattern buffer. */
4626 -static struct re_pattern_buffer re_comp_buf;
4627 -
4628 -char *
4629 -re_comp (s)
4630 - const char *s;
4631 -{
4632 - reg_errcode_t ret;
4633 -
4634 - if (!s)
4635 - {
4636 - if (!re_comp_buf.buffer)
4637 - return "No previous regular expression";
4638 - return 0;
4639 - }
4640 -
4641 - if (!re_comp_buf.buffer)
4642 - {
4643 - re_comp_buf.buffer = (unsigned char *) malloc (200);
4644 - if (re_comp_buf.buffer == NULL)
4645 - return "Memory exhausted";
4646 - re_comp_buf.allocated = 200;
4647 -
4648 - re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH);
4649 - if (re_comp_buf.fastmap == NULL)
4650 - return "Memory exhausted";
4651 - }
4652 -
4653 - /* Since `re_exec' always passes NULL for the `regs' argument, we
4654 - don't need to initialize the pattern buffer fields which affect it. */
4655 -
4656 - /* Match anchors at newlines. */
4657 - re_comp_buf.newline_anchor = 1;
4658 -
4659 - ret = regex_compile (s, strlen (s), re_syntax_options, &re_comp_buf);
4660 -
4661 - /* Yes, we're discarding `const' here. */
4662 - return (char *) re_error_msg[(int) ret];
4663 -}
4664 -
4665 -
4666 -int
4667 -re_exec (s)
4668 - const char *s;
4669 -{
4670 - const int len = strlen (s);
4671 - return
4672 - 0 <= re_search (&re_comp_buf, s, len, 0, len, (struct re_registers *) 0);
4673 -}
4674 -#endif /* not emacs and not _POSIX_SOURCE */
4675 -
4676 -/* POSIX.2 functions. Don't define these for Emacs. */
4677 -
4678 -#ifndef emacs
4679 -
4680 -/* regcomp takes a regular expression as a string and compiles it.
4681 -
4682 - PREG is a regex_t *. We do not expect any fields to be initialized,
4683 - since POSIX says we shouldn't. Thus, we set
4684 -
4685 - `buffer' to the compiled pattern;
4686 - `used' to the length of the compiled pattern;
4687 - `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
4688 - REG_EXTENDED bit in CFLAGS is set; otherwise, to
4689 - RE_SYNTAX_POSIX_BASIC;
4690 - `newline_anchor' to REG_NEWLINE being set in CFLAGS;
4691 - `fastmap' and `fastmap_accurate' to zero;
4692 - `re_nsub' to the number of subexpressions in PATTERN.
4693 -
4694 - PATTERN is the address of the pattern string.
4695 -
4696 - CFLAGS is a series of bits which affect compilation.
4697 -
4698 - If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
4699 - use POSIX basic syntax.
4700 -
4701 - If REG_NEWLINE is set, then . and [^...] don't match newline.
4702 - Also, regexec will try a match beginning after every newline.
4703 -
4704 - If REG_ICASE is set, then we considers upper- and lowercase
4705 - versions of letters to be equivalent when matching.
4706 -
4707 - If REG_NOSUB is set, then when PREG is passed to regexec, that
4708 - routine will report only success or failure, and nothing about the
4709 - registers.
4710 -
4711 - It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
4712 - the return codes and their meanings.) */
4713 -
4714 -int
4715 -regcomp (preg, pattern, cflags)
4716 - regex_t *preg;
4717 - const char *pattern;
4718 - int cflags;
4719 -{
4720 - reg_errcode_t ret;
4721 - unsigned syntax
4722 - = (cflags & REG_EXTENDED) ?
4723 - RE_SYNTAX_POSIX_EXTENDED : RE_SYNTAX_POSIX_BASIC;
4724 -
4725 - /* regex_compile will allocate the space for the compiled pattern. */
4726 - preg->buffer = 0;
4727 - preg->allocated = 0;
4728 -
4729 - /* Don't bother to use a fastmap when searching. This simplifies the
4730 - REG_NEWLINE case: if we used a fastmap, we'd have to put all the
4731 - characters after newlines into the fastmap. This way, we just try
4732 - every character. */
4733 - preg->fastmap = 0;
4734 -
4735 - if (cflags & REG_ICASE)
4736 - {
4737 - unsigned i;
4738 -
4739 - preg->translate = (char *) malloc (CHAR_SET_SIZE);
4740 - if (preg->translate == NULL)
4741 - return (int) REG_ESPACE;
4742 -
4743 - /* Map uppercase characters to corresponding lowercase ones. */
4744 - for (i = 0; i < CHAR_SET_SIZE; i++)
4745 - preg->translate[i] = ISUPPER (i) ? tolower (i) : i;
4746 - }
4747 - else
4748 - preg->translate = NULL;
4749 -
4750 - /* If REG_NEWLINE is set, newlines are treated differently. */
4751 - if (cflags & REG_NEWLINE)
4752 - { /* REG_NEWLINE implies neither . nor [^...] match newline. */
4753 - syntax &= ~RE_DOT_NEWLINE;
4754 - syntax |= RE_HAT_LISTS_NOT_NEWLINE;
4755 - /* It also changes the matching behavior. */
4756 - preg->newline_anchor = 1;
4757 - }
4758 - else
4759 - preg->newline_anchor = 0;
4760 -
4761 - preg->no_sub = !!(cflags & REG_NOSUB);
4762 -
4763 - /* POSIX says a null character in the pattern terminates it, so we
4764 - can use strlen here in compiling the pattern. */
4765 - ret = regex_compile (pattern, strlen (pattern), syntax, preg);
4766 -
4767 - /* POSIX doesn't distinguish between an unmatched open-group and an
4768 - unmatched close-group: both are REG_EPAREN. */
4769 - if (ret == REG_ERPAREN) ret = REG_EPAREN;
4770 -
4771 - return (int) ret;
4772 -}
4773 -
4774 -
4775 -/* regexec searches for a given pattern, specified by PREG, in the
4776 - string STRING.
4777 -
4778 - If NMATCH is zero or REG_NOSUB was set in the cflags argument to
4779 - `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
4780 - least NMATCH elements, and we set them to the offsets of the
4781 - corresponding matched substrings.
4782 -
4783 - EFLAGS specifies `execution flags' which affect matching: if
4784 - REG_NOTBOL is set, then ^ does not match at the beginning of the
4785 - string; if REG_NOTEOL is set, then $ does not match at the end.
4786 -
4787 - We return 0 if we find a match and REG_NOMATCH if not. */
4788 -
4789 -int
4790 -regexec (preg, string, nmatch, pmatch, eflags)
4791 - const regex_t *preg;
4792 - const char *string;
4793 - size_t nmatch;
4794 - regmatch_t pmatch[];
4795 - int eflags;
4796 -{
4797 - int ret;
4798 - struct re_registers regs;
4799 - regex_t private_preg;
4800 - int len = strlen (string);
4801 - boolean want_reg_info = !preg->no_sub && nmatch > 0;
4802 -
4803 - private_preg = *preg;
4804 -
4805 - private_preg.not_bol = !!(eflags & REG_NOTBOL);
4806 - private_preg.not_eol = !!(eflags & REG_NOTEOL);
4807 -
4808 - /* The user has told us exactly how many registers to return
4809 - information about, via `nmatch'. We have to pass that on to the
4810 - matching routines. */
4811 - private_preg.regs_allocated = REGS_FIXED;
4812 -
4813 - if (want_reg_info)
4814 - {
4815 - regs.num_regs = nmatch;
4816 - regs.start = TALLOC (nmatch, regoff_t);
4817 - regs.end = TALLOC (nmatch, regoff_t);
4818 - if (regs.start == NULL || regs.end == NULL)
4819 - return (int) REG_NOMATCH;
4820 - }
4821 -
4822 - /* Perform the searching operation. */
4823 - ret = re_search (&private_preg, string, len,
4824 - /* start: */ 0, /* range: */ len,
4825 - want_reg_info ? &regs : (struct re_registers *) 0);
4826 -
4827 - /* Copy the register information to the POSIX structure. */
4828 - if (want_reg_info)
4829 - {
4830 - if (ret >= 0)
4831 - {
4832 - unsigned r;
4833 -
4834 - for (r = 0; r < nmatch; r++)
4835 - {
4836 - pmatch[r].rm_so = regs.start[r];
4837 - pmatch[r].rm_eo = regs.end[r];
4838 - }
4839 - }
4840 -
4841 - /* If we needed the temporary register info, free the space now. */
4842 - free (regs.start);
4843 - free (regs.end);
4844 - }
4845 -
4846 - /* We want zero return to mean success, unlike `re_search'. */
4847 - return ret >= 0 ? (int) REG_NOERROR : (int) REG_NOMATCH;
4848 -}
4849 -
4850 -
4851 -/* Returns a message corresponding to an error code, ERRCODE, returned
4852 - from either regcomp or regexec. We don't use PREG here. */
4853 -
4854 -size_t
4855 -regerror(int errcode, const regex_t *preg,
4856 - char *errbuf, size_t errbuf_size)
4857 -{
4858 - const char *msg;
4859 - size_t msg_size;
4860 -
4861 - if (errcode < 0
4862 - || errcode >= (sizeof (re_error_msg) / sizeof (re_error_msg[0])))
4863 - /* Only error codes returned by the rest of the code should be passed
4864 - to this routine. If we are given anything else, or if other regex
4865 - code generates an invalid error code, then the program has a bug.
4866 - Dump core so we can fix it. */
4867 - abort ();
4868 -
4869 - msg = re_error_msg[errcode];
4870 -
4871 - /* POSIX doesn't require that we do anything in this case, but why
4872 - not be nice. */
4873 - if (! msg)
4874 - msg = "Success";
4875 -
4876 - msg_size = strlen (msg) + 1; /* Includes the null. */
4877 -
4878 - if (errbuf_size != 0)
4879 - {
4880 - if (msg_size > errbuf_size)
4881 - {
4882 - strncpy (errbuf, msg, errbuf_size - 1);
4883 - errbuf[errbuf_size - 1] = 0;
4884 - }
4885 - else
4886 - strcpy (errbuf, msg);
4887 - }
4888 -
4889 - return msg_size;
4890 -}
4891 -
4892 -
4893 -/* Free dynamically allocated space used by PREG. */
4894 -
4895 -void
4896 -regfree (preg)
4897 - regex_t *preg;
4898 -{
4899 - if (preg->buffer != NULL)
4900 - free (preg->buffer);
4901 - preg->buffer = NULL;
4902 -
4903 - preg->allocated = 0;
4904 - preg->used = 0;
4905 -
4906 - if (preg->fastmap != NULL)
4907 - free (preg->fastmap);
4908 - preg->fastmap = NULL;
4909 - preg->fastmap_accurate = 0;
4910 -
4911 - if (preg->translate != NULL)
4912 - free (preg->translate);
4913 - preg->translate = NULL;
4914 -}
4915 -
4916 -#endif /* not emacs */

This file is too large to show in full.

compat/regex/regex.h
+274 -185
@@ -1,70 +1,87 @@
1 /* Definitions for data structures and routines for the regular
2 - expression library, version 0.12.
2 + expression library.
3 + Copyright (C) 1985,1989-93,1995-98,2000,2001,2002,2003,2005,2006,2008
4 + Free Software Foundation, Inc.
5 + This file is part of the GNU C Library.
6
4 - Copyright (C) 1985, 1989, 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
7 + The GNU C Library is free software; you can redistribute it and/or
8 + modify it under the terms of the GNU Lesser General Public
9 + License as published by the Free Software Foundation; either
10 + version 2.1 of the License, or (at your option) any later version.
11
6 - This program is free software; you can redistribute it and/or modify
7 - it under the terms of the GNU General Public License as published by
8 - the Free Software Foundation; either version 2, or (at your option)
9 - any later version.
10 -
11 - This program is distributed in the hope that it will be useful,
12 + The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 - GNU General Public License for more details.
15 -
16 - You should have received a copy of the GNU General Public License
17 - along with this program; if not, write to the Free Software
18 - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
14 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 + Lesser General Public License for more details.
16
20 -#ifndef __REGEXP_LIBRARY_H__
21 -#define __REGEXP_LIBRARY_H__
17 + You should have received a copy of the GNU Lesser General Public
18 + License along with the GNU C Library; if not, write to the Free
19 + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 + 02110-1301 USA. */
21
23 -/* POSIX says that <sys/types.h> must be included (by the caller) before
24 - <regex.h>. */
22 +#ifndef _REGEX_H
23 +#define _REGEX_H 1
24
26 -#ifdef VMS
27 -/* VMS doesn't have `size_t' in <sys/types.h>, even though POSIX says it
28 - should be there. */
25 +#ifdef HAVE_STDDEF_H
26 #include <stddef.h>
27 #endif
28
29 +#ifdef HAVE_SYS_TYPES_H
30 +#include <sys/types.h>
31 +#endif
32 +
33 +#ifndef _LIBC
34 +#define __USE_GNU 1
35 +#endif
36 +
37 +/* Allow the use in C++ code. */
38 +#ifdef __cplusplus
39 +extern "C" {
40 +#endif
41 +
42 +/* The following two types have to be signed and unsigned integer type
43 + wide enough to hold a value of a pointer. For most ANSI compilers
44 + ptrdiff_t and size_t should be likely OK. Still size of these two
45 + types is 2 for Microsoft C. Ugh... */
46 +typedef long int s_reg_t;
47 +typedef unsigned long int active_reg_t;
48
49 /* The following bits are used to determine the regexp syntax we
50 recognize. The set/not-set meanings are chosen so that Emacs syntax
51 remains the value 0. The bits are given in alphabetical order, and
52 the definitions shifted by one from the previous bit; thus, when we
53 add or remove a bit, only one other definition need change. */
38 -typedef unsigned reg_syntax_t;
54 +typedef unsigned long int reg_syntax_t;
55
56 +#ifdef __USE_GNU
57 /* If this bit is not set, then \ inside a bracket expression is literal.
58 If set, then such a \ quotes the following character. */
42 -#define RE_BACKSLASH_ESCAPE_IN_LISTS (1)
59 +# define RE_BACKSLASH_ESCAPE_IN_LISTS ((unsigned long int) 1)
60
61 /* If this bit is not set, then + and ? are operators, and \+ and \? are
62 literals.
63 If set, then \+ and \? are operators and + and ? are literals. */
47 -#define RE_BK_PLUS_QM (RE_BACKSLASH_ESCAPE_IN_LISTS << 1)
64 +# define RE_BK_PLUS_QM (RE_BACKSLASH_ESCAPE_IN_LISTS << 1)
65
66 /* If this bit is set, then character classes are supported. They are:
67 [:alpha:], [:upper:], [:lower:], [:digit:], [:alnum:], [:xdigit:],
68 [:space:], [:print:], [:punct:], [:graph:], and [:cntrl:].
69 If not set, then character classes are not supported. */
53 -#define RE_CHAR_CLASSES (RE_BK_PLUS_QM << 1)
70 +# define RE_CHAR_CLASSES (RE_BK_PLUS_QM << 1)
71
72 /* If this bit is set, then ^ and $ are always anchors (outside bracket
73 expressions, of course).
74 If this bit is not set, then it depends:
58 - ^ is an anchor if it is at the beginning of a regular
59 - expression or after an open-group or an alternation operator;
60 - $ is an anchor if it is at the end of a regular expression, or
61 - before a close-group or an alternation operator.
75 + ^ is an anchor if it is at the beginning of a regular
76 + expression or after an open-group or an alternation operator;
77 + $ is an anchor if it is at the end of a regular expression, or
78 + before a close-group or an alternation operator.
79
80 This bit could be (re)combined with RE_CONTEXT_INDEP_OPS, because
81 POSIX draft 11.2 says that * etc. in leading positions is undefined.
82 We already implemented a previous draft which made those constructs
83 invalid, though, so we haven't changed the code back. */
67 -#define RE_CONTEXT_INDEP_ANCHORS (RE_CHAR_CLASSES << 1)
84 +# define RE_CONTEXT_INDEP_ANCHORS (RE_CHAR_CLASSES << 1)
85
86 /* If this bit is set, then special characters are always special
87 regardless of where they are in the pattern.
@@ -72,63 +89,94 @@ typedef unsigned reg_syntax_t;
89 some contexts; otherwise they are ordinary. Specifically,
90 * + ? and intervals are only special when not after the beginning,
91 open-group, or alternation operator. */
75 -#define RE_CONTEXT_INDEP_OPS (RE_CONTEXT_INDEP_ANCHORS << 1)
92 +# define RE_CONTEXT_INDEP_OPS (RE_CONTEXT_INDEP_ANCHORS << 1)
93
94 /* If this bit is set, then *, +, ?, and { cannot be first in an re or
95 immediately after an alternation or begin-group operator. */
79 -#define RE_CONTEXT_INVALID_OPS (RE_CONTEXT_INDEP_OPS << 1)
96 +# define RE_CONTEXT_INVALID_OPS (RE_CONTEXT_INDEP_OPS << 1)
97
98 /* If this bit is set, then . matches newline.
99 If not set, then it doesn't. */
83 -#define RE_DOT_NEWLINE (RE_CONTEXT_INVALID_OPS << 1)
100 +# define RE_DOT_NEWLINE (RE_CONTEXT_INVALID_OPS << 1)
101
102 /* If this bit is set, then . doesn't match NUL.
103 If not set, then it does. */
87 -#define RE_DOT_NOT_NULL (RE_DOT_NEWLINE << 1)
104 +# define RE_DOT_NOT_NULL (RE_DOT_NEWLINE << 1)
105
106 /* If this bit is set, nonmatching lists [^...] do not match newline.
107 If not set, they do. */
91 -#define RE_HAT_LISTS_NOT_NEWLINE (RE_DOT_NOT_NULL << 1)
108 +# define RE_HAT_LISTS_NOT_NEWLINE (RE_DOT_NOT_NULL << 1)
109
110 /* If this bit is set, either \{...\} or {...} defines an
111 interval, depending on RE_NO_BK_BRACES.
112 If not set, \{, \}, {, and } are literals. */
96 -#define RE_INTERVALS (RE_HAT_LISTS_NOT_NEWLINE << 1)
113 +# define RE_INTERVALS (RE_HAT_LISTS_NOT_NEWLINE << 1)
114
115 /* If this bit is set, +, ? and | aren't recognized as operators.
116 If not set, they are. */
100 -#define RE_LIMITED_OPS (RE_INTERVALS << 1)
117 +# define RE_LIMITED_OPS (RE_INTERVALS << 1)
118
119 /* If this bit is set, newline is an alternation operator.
120 If not set, newline is literal. */
104 -#define RE_NEWLINE_ALT (RE_LIMITED_OPS << 1)
121 +# define RE_NEWLINE_ALT (RE_LIMITED_OPS << 1)
122
123 /* If this bit is set, then `{...}' defines an interval, and \{ and \}
124 are literals.
125 If not set, then `\{...\}' defines an interval. */
109 -#define RE_NO_BK_BRACES (RE_NEWLINE_ALT << 1)
126 +# define RE_NO_BK_BRACES (RE_NEWLINE_ALT << 1)
127
128 /* If this bit is set, (...) defines a group, and \( and \) are literals.
129 If not set, \(...\) defines a group, and ( and ) are literals. */
113 -#define RE_NO_BK_PARENS (RE_NO_BK_BRACES << 1)
130 +# define RE_NO_BK_PARENS (RE_NO_BK_BRACES << 1)
131
132 /* If this bit is set, then \<digit> matches <digit>.
133 If not set, then \<digit> is a back-reference. */
117 -#define RE_NO_BK_REFS (RE_NO_BK_PARENS << 1)
134 +# define RE_NO_BK_REFS (RE_NO_BK_PARENS << 1)
135
136 /* If this bit is set, then | is an alternation operator, and \| is literal.
137 If not set, then \| is an alternation operator, and | is literal. */
121 -#define RE_NO_BK_VBAR (RE_NO_BK_REFS << 1)
138 +# define RE_NO_BK_VBAR (RE_NO_BK_REFS << 1)
139
140 /* If this bit is set, then an ending range point collating higher
141 than the starting range point, as in [z-a], is invalid.
142 If not set, then when ending range point collates higher than the
143 starting range point, the range is ignored. */
127 -#define RE_NO_EMPTY_RANGES (RE_NO_BK_VBAR << 1)
144 +# define RE_NO_EMPTY_RANGES (RE_NO_BK_VBAR << 1)
145
146 /* If this bit is set, then an unmatched ) is ordinary.
147 If not set, then an unmatched ) is invalid. */
131 -#define RE_UNMATCHED_RIGHT_PAREN_ORD (RE_NO_EMPTY_RANGES << 1)
148 +# define RE_UNMATCHED_RIGHT_PAREN_ORD (RE_NO_EMPTY_RANGES << 1)
149 +
150 +/* If this bit is set, succeed as soon as we match the whole pattern,
151 + without further backtracking. */
152 +# define RE_NO_POSIX_BACKTRACKING (RE_UNMATCHED_RIGHT_PAREN_ORD << 1)
153 +
154 +/* If this bit is set, do not process the GNU regex operators.
155 + If not set, then the GNU regex operators are recognized. */
156 +# define RE_NO_GNU_OPS (RE_NO_POSIX_BACKTRACKING << 1)
157 +
158 +/* If this bit is set, a syntactically invalid interval is treated as
159 + a string of ordinary characters. For example, the ERE 'a{1' is
160 + treated as 'a\{1'. */
161 +# define RE_INVALID_INTERVAL_ORD (RE_NO_GNU_OPS << 1)
162 +
163 +/* If this bit is set, then ignore case when matching.
164 + If not set, then case is significant. */
165 +# define RE_ICASE (RE_INVALID_INTERVAL_ORD << 1)
166 +
167 +/* This bit is used internally like RE_CONTEXT_INDEP_ANCHORS but only
168 + for ^, because it is difficult to scan the regex backwards to find
169 + whether ^ should be special. */
170 +# define RE_CARET_ANCHORS_HERE (RE_ICASE << 1)
171 +
172 +/* If this bit is set, then \{ cannot be first in an bre or
173 + immediately after an alternation or begin-group operator. */
174 +# define RE_CONTEXT_INVALID_DUP (RE_CARET_ANCHORS_HERE << 1)
175 +
176 +/* If this bit is set, then no_sub will be set to 1 during
177 + re_compile_pattern. */
178 +#define RE_NO_SUB (RE_CONTEXT_INVALID_DUP << 1)
179 +#endif
180
181 /* This global variable defines the particular regexp syntax to use (for
182 some interfaces). When a regexp is compiled, the syntax used is
@@ -136,6 +184,7 @@ typedef unsigned reg_syntax_t;
184 already-compiled regexps. */
185 extern reg_syntax_t re_syntax_options;
186
187 +#ifdef __USE_GNU
188 /* Define combinations of the above bits for the standard possibilities.
189 (The [[[ comments delimit what gets put into the Texinfo file, so
190 don't delete them!) */
@@ -143,13 +192,22 @@ extern reg_syntax_t re_syntax_options;
192 #define RE_SYNTAX_EMACS 0
193
194 #define RE_SYNTAX_AWK \
146 - (RE_BACKSLASH_ESCAPE_IN_LISTS | RE_DOT_NOT_NULL \
147 - | RE_NO_BK_PARENS | RE_NO_BK_REFS \
148 - | RE_NO_BK_VBAR | RE_NO_EMPTY_RANGES \
149 - | RE_UNMATCHED_RIGHT_PAREN_ORD)
150 -
151 -#define RE_SYNTAX_POSIX_AWK \
152 - (RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS)
195 + (RE_BACKSLASH_ESCAPE_IN_LISTS | RE_DOT_NOT_NULL \
196 + | RE_NO_BK_PARENS | RE_NO_BK_REFS \
197 + | RE_NO_BK_VBAR | RE_NO_EMPTY_RANGES \
198 + | RE_DOT_NEWLINE | RE_CONTEXT_INDEP_ANCHORS \
199 + | RE_UNMATCHED_RIGHT_PAREN_ORD | RE_NO_GNU_OPS)
200 +
201 +#define RE_SYNTAX_GNU_AWK \
202 + ((RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS \
203 + | RE_INVALID_INTERVAL_ORD) \
204 + & ~(RE_DOT_NOT_NULL | RE_CONTEXT_INDEP_OPS \
205 + | RE_CONTEXT_INVALID_OPS ))
206 +
207 +#define RE_SYNTAX_POSIX_AWK \
208 + (RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS \
209 + | RE_INTERVALS | RE_NO_GNU_OPS \
210 + | RE_INVALID_INTERVAL_ORD)
211
212 #define RE_SYNTAX_GREP \
213 (RE_BK_PLUS_QM | RE_CHAR_CLASSES \
@@ -163,7 +221,8 @@ extern reg_syntax_t re_syntax_options;
221 | RE_NO_BK_VBAR)
222
223 #define RE_SYNTAX_POSIX_EGREP \
166 - (RE_SYNTAX_EGREP | RE_INTERVALS | RE_NO_BK_BRACES)
224 + (RE_SYNTAX_EGREP | RE_INTERVALS | RE_NO_BK_BRACES \
225 + | RE_INVALID_INTERVAL_ORD)
226
227 /* P1003.2/D11.2, section 4.20.7.1, lines 5078ff. */
228 #define RE_SYNTAX_ED RE_SYNTAX_POSIX_BASIC
@@ -176,7 +235,7 @@ extern reg_syntax_t re_syntax_options;
235 | RE_INTERVALS | RE_NO_EMPTY_RANGES)
236
237 #define RE_SYNTAX_POSIX_BASIC \
179 - (_RE_SYNTAX_POSIX_COMMON | RE_BK_PLUS_QM)
238 + (_RE_SYNTAX_POSIX_COMMON | RE_BK_PLUS_QM | RE_CONTEXT_INVALID_DUP)
239
240 /* Differs from ..._POSIX_BASIC only in that RE_BK_PLUS_QM becomes
241 RE_LIMITED_OPS, i.e., \? \+ \| are not recognized. Actually, this
@@ -185,13 +244,13 @@ extern reg_syntax_t re_syntax_options;
244 (_RE_SYNTAX_POSIX_COMMON | RE_LIMITED_OPS)
245
246 #define RE_SYNTAX_POSIX_EXTENDED \
188 - (_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \
189 - | RE_CONTEXT_INDEP_OPS | RE_NO_BK_BRACES \
190 - | RE_NO_BK_PARENS | RE_NO_BK_VBAR \
191 - | RE_UNMATCHED_RIGHT_PAREN_ORD)
247 + (_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \
248 + | RE_CONTEXT_INDEP_OPS | RE_NO_BK_BRACES \
249 + | RE_NO_BK_PARENS | RE_NO_BK_VBAR \
250 + | RE_CONTEXT_INVALID_OPS | RE_UNMATCHED_RIGHT_PAREN_ORD)
251
193 -/* Differs from ..._POSIX_EXTENDED in that RE_CONTEXT_INVALID_OPS
194 - replaces RE_CONTEXT_INDEP_OPS and RE_NO_BK_REFS is added. */
252 +/* Differs from ..._POSIX_EXTENDED in that RE_CONTEXT_INDEP_OPS is
253 + removed and RE_NO_BK_REFS is added. */
254 #define RE_SYNTAX_POSIX_MINIMAL_EXTENDED \
255 (_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \
256 | RE_CONTEXT_INVALID_OPS | RE_NO_BK_BRACES \
@@ -202,10 +261,12 @@ extern reg_syntax_t re_syntax_options;
261 /* Maximum number of duplicates an interval can allow. Some systems
262 (erroneously) define this in other header files, but we want our
263 value, so remove any previous define. */
205 -#ifdef RE_DUP_MAX
206 -#undef RE_DUP_MAX
264 +# ifdef RE_DUP_MAX
265 +# undef RE_DUP_MAX
266 +# endif
267 +/* If sizeof(int) == 2, then ((1 << 15) - 1) overflows. */
268 +# define RE_DUP_MAX (0x7fff)
269 #endif
208 -#define RE_DUP_MAX ((1 << 15) - 1)
270
271
272 /* POSIX `cflags' bits (i.e., information for `regcomp'). */
@@ -240,18 +301,26 @@ extern reg_syntax_t re_syntax_options;
301 /* Like REG_NOTBOL, except for the end-of-line. */
302 #define REG_NOTEOL (1 << 1)
303
304 +/* Use PMATCH[0] to delimit the start and end of the search in the
305 + buffer. */
306 +#define REG_STARTEND (1 << 2)
307 +
308
309 /* If any error codes are removed, changed, or added, update the
310 `re_error_msg' table in regex.c. */
311 typedef enum
312 {
313 +#if defined _XOPEN_SOURCE || defined __USE_XOPEN2K
314 + REG_ENOSYS = -1, /* This will never happen for this implementation. */
315 +#endif
316 +
317 REG_NOERROR = 0, /* Success. */
318 REG_NOMATCH, /* Didn't find a match (for regexec). */
319
320 /* POSIX regcomp return error codes. (In the order listed in the
321 standard.) */
322 REG_BADPAT, /* Invalid pattern. */
254 - REG_ECOLLATE, /* Not implemented. */
323 + REG_ECOLLATE, /* Inalid collating element. */
324 REG_ECTYPE, /* Invalid character class name. */
325 REG_EESCAPE, /* Trailing backslash. */
326 REG_ESUBREG, /* Invalid back reference. */
@@ -275,85 +344,92 @@ typedef enum
344 compiled, the `re_nsub' field is available. All other fields are
345 private to the regex routines. */
346
347 +#ifndef RE_TRANSLATE_TYPE
348 +# define __RE_TRANSLATE_TYPE unsigned char *
349 +# ifdef __USE_GNU
350 +# define RE_TRANSLATE_TYPE __RE_TRANSLATE_TYPE
351 +# endif
352 +#endif
353 +
354 +#ifdef __USE_GNU
355 +# define __REPB_PREFIX(name) name
356 +#else
357 +# define __REPB_PREFIX(name) __##name
358 +#endif
359 +
360 struct re_pattern_buffer
361 {
280 -/* [[[begin pattern_buffer]]] */
281 - /* Space that holds the compiled pattern. It is declared as
282 - `unsigned char *' because its elements are
283 - sometimes used as array indexes. */
284 - unsigned char *buffer;
362 + /* Space that holds the compiled pattern. It is declared as
363 + `unsigned char *' because its elements are sometimes used as
364 + array indexes. */
365 + unsigned char *__REPB_PREFIX(buffer);
366
286 - /* Number of bytes to which `buffer' points. */
287 - unsigned long allocated;
367 + /* Number of bytes to which `buffer' points. */
368 + unsigned long int __REPB_PREFIX(allocated);
369
289 - /* Number of bytes actually used in `buffer'. */
290 - unsigned long used;
370 + /* Number of bytes actually used in `buffer'. */
371 + unsigned long int __REPB_PREFIX(used);
372
292 - /* Syntax setting with which the pattern was compiled. */
293 - reg_syntax_t syntax;
373 + /* Syntax setting with which the pattern was compiled. */
374 + reg_syntax_t __REPB_PREFIX(syntax);
375
295 - /* Pointer to a fastmap, if any, otherwise zero. re_search uses
296 - the fastmap, if there is one, to skip over impossible
297 - starting points for matches. */
298 - char *fastmap;
376 + /* Pointer to a fastmap, if any, otherwise zero. re_search uses the
377 + fastmap, if there is one, to skip over impossible starting points
378 + for matches. */
379 + char *__REPB_PREFIX(fastmap);
380
300 - /* Either a translate table to apply to all characters before
301 - comparing them, or zero for no translation. The translation
302 - is applied to a pattern when it is compiled and to a string
303 - when it is matched. */
304 - char *translate;
381 + /* Either a translate table to apply to all characters before
382 + comparing them, or zero for no translation. The translation is
383 + applied to a pattern when it is compiled and to a string when it
384 + is matched. */
385 + __RE_TRANSLATE_TYPE __REPB_PREFIX(translate);
386
306 - /* Number of subexpressions found by the compiler. */
387 + /* Number of subexpressions found by the compiler. */
388 size_t re_nsub;
389
309 - /* Zero if this pattern cannot match the empty string, one else.
310 - Well, in truth it's used only in `re_search_2', to see
311 - whether or not we should use the fastmap, so we don't set
312 - this absolutely perfectly; see `re_compile_fastmap' (the
313 - `duplicate' case). */
314 - unsigned can_be_null : 1;
315 -
316 - /* If REGS_UNALLOCATED, allocate space in the `regs' structure
317 - for `max (RE_NREGS, re_nsub + 1)' groups.
318 - If REGS_REALLOCATE, reallocate space if necessary.
319 - If REGS_FIXED, use what's there. */
320 -#define REGS_UNALLOCATED 0
321 -#define REGS_REALLOCATE 1
322 -#define REGS_FIXED 2
323 - unsigned regs_allocated : 2;
324 -
325 - /* Set to zero when `regex_compile' compiles a pattern; set to one
326 - by `re_compile_fastmap' if it updates the fastmap. */
327 - unsigned fastmap_accurate : 1;
328 -
329 - /* If set, `re_match_2' does not return information about
330 - subexpressions. */
331 - unsigned no_sub : 1;
332 -
333 - /* If set, a beginning-of-line anchor doesn't match at the
334 - beginning of the string. */
335 - unsigned not_bol : 1;
336 -
337 - /* Similarly for an end-of-line anchor. */
338 - unsigned not_eol : 1;
339 -
340 - /* If true, an anchor at a newline matches. */
341 - unsigned newline_anchor : 1;
342 -
343 -/* [[[end pattern_buffer]]] */
344 -};
390 + /* Zero if this pattern cannot match the empty string, one else.
391 + Well, in truth it's used only in `re_search_2', to see whether or
392 + not we should use the fastmap, so we don't set this absolutely
393 + perfectly; see `re_compile_fastmap' (the `duplicate' case). */
394 + unsigned __REPB_PREFIX(can_be_null) : 1;
395 +
396 + /* If REGS_UNALLOCATED, allocate space in the `regs' structure
397 + for `max (RE_NREGS, re_nsub + 1)' groups.
398 + If REGS_REALLOCATE, reallocate space if necessary.
399 + If REGS_FIXED, use what's there. */
400 +#ifdef __USE_GNU
401 +# define REGS_UNALLOCATED 0
402 +# define REGS_REALLOCATE 1
403 +# define REGS_FIXED 2
404 +#endif
405 + unsigned __REPB_PREFIX(regs_allocated) : 2;
406
346 -typedef struct re_pattern_buffer regex_t;
407 + /* Set to zero when `regex_compile' compiles a pattern; set to one
408 + by `re_compile_fastmap' if it updates the fastmap. */
409 + unsigned __REPB_PREFIX(fastmap_accurate) : 1;
410 +
411 + /* If set, `re_match_2' does not return information about
412 + subexpressions. */
413 + unsigned __REPB_PREFIX(no_sub) : 1;
414
415 + /* If set, a beginning-of-line anchor doesn't match at the beginning
416 + of the string. */
417 + unsigned __REPB_PREFIX(not_bol) : 1;
418 +
419 + /* Similarly for an end-of-line anchor. */
420 + unsigned __REPB_PREFIX(not_eol) : 1;
421 +
422 + /* If true, an anchor at a newline matches. */
423 + unsigned __REPB_PREFIX(newline_anchor) : 1;
424 +};
425
349 -/* search.c (search_buffer) in Emacs needs this one opcode value. It is
350 - defined both in `regex.c' and here. */
351 -#define RE_EXACTN_VALUE 1
426 +typedef struct re_pattern_buffer regex_t;
427
428 /* Type for byte offsets within the string. POSIX mandates this. */
429 typedef int regoff_t;
430
431
432 +#ifdef __USE_GNU
433 /* This is the structure we store register match data in. See
434 regex.texinfo for a full description of what registers match. */
435 struct re_registers
@@ -367,8 +443,9 @@ struct re_registers
443 /* If `regs_allocated' is REGS_UNALLOCATED in the pattern buffer,
444 `re_match_2' returns information about at least this many registers
445 the first time a `regs' structure is passed. */
370 -#ifndef RE_NREGS
371 -#define RE_NREGS 30
446 +# ifndef RE_NREGS
447 +# define RE_NREGS 30
448 +# endif
449 #endif
450
451
@@ -383,38 +460,22 @@ typedef struct
460
461 /* Declarations for routines. */
462
386 -/* To avoid duplicating every routine declaration -- once with a
387 - prototype (if we are ANSI), and once without (if we aren't) -- we
388 - use the following macro to declare argument types. This
389 - unfortunately clutters up the declarations a bit, but I think it's
390 - worth it. */
391 -
392 -#if __STDC__
393 -
394 -#define _RE_ARGS(args) args
395 -
396 -#else /* not __STDC__ */
397 -
398 -#define _RE_ARGS(args) ()
399 -
400 -#endif /* not __STDC__ */
401 -
463 +#ifdef __USE_GNU
464 /* Sets the current default syntax to SYNTAX, and return the old syntax.
465 You can also simply assign to the `re_syntax_options' variable. */
404 -extern reg_syntax_t re_set_syntax _RE_ARGS ((reg_syntax_t syntax));
466 +extern reg_syntax_t re_set_syntax (reg_syntax_t __syntax);
467
468 /* Compile the regular expression PATTERN, with length LENGTH
469 and syntax given by the global `re_syntax_options', into the buffer
470 BUFFER. Return NULL if successful, and an error string if not. */
409 -extern const char *re_compile_pattern
410 - _RE_ARGS ((const char *pattern, int length,
411 - struct re_pattern_buffer *buffer));
471 +extern const char *re_compile_pattern (const char *__pattern, size_t __length,
472 + struct re_pattern_buffer *__buffer);
473
474
475 /* Compile a fastmap for the compiled pattern in BUFFER; used to
476 accelerate searches. Return 0 if successful and -2 if was an
477 internal error. */
417 -extern int re_compile_fastmap _RE_ARGS ((struct re_pattern_buffer *buffer));
478 +extern int re_compile_fastmap (struct re_pattern_buffer *__buffer);
479
480
481 /* Search in the string STRING (with length LENGTH) for the pattern
@@ -422,31 +483,30 @@ extern int re_compile_fastmap _RE_ARGS ((struct re_pattern_buffer *buffer));
483 characters. Return the starting position of the match, -1 for no
484 match, or -2 for an internal error. Also return register
485 information in REGS (if REGS and BUFFER->no_sub are nonzero). */
425 -extern int re_search
426 - _RE_ARGS ((struct re_pattern_buffer *buffer, const char *string,
427 - int length, int start, int range, struct re_registers *regs));
486 +extern int re_search (struct re_pattern_buffer *__buffer, const char *__cstring,
487 + int __length, int __start, int __range,
488 + struct re_registers *__regs);
489
490
491 /* Like `re_search', but search in the concatenation of STRING1 and
492 STRING2. Also, stop searching at index START + STOP. */
432 -extern int re_search_2
433 - _RE_ARGS ((struct re_pattern_buffer *buffer, const char *string1,
434 - int length1, const char *string2, int length2,
435 - int start, int range, struct re_registers *regs, int stop));
493 +extern int re_search_2 (struct re_pattern_buffer *__buffer,
494 + const char *__string1, int __length1,
495 + const char *__string2, int __length2, int __start,
496 + int __range, struct re_registers *__regs, int __stop);
497
498
499 /* Like `re_search', but return how many characters in STRING the regexp
500 in BUFFER matched, starting at position START. */
440 -extern int re_match
441 - _RE_ARGS ((struct re_pattern_buffer *buffer, const char *string,
442 - int length, int start, struct re_registers *regs));
501 +extern int re_match (struct re_pattern_buffer *__buffer, const char *__cstring,
502 + int __length, int __start, struct re_registers *__regs);
503
504
505 /* Relates to `re_match' as `re_search_2' relates to `re_search'. */
446 -extern int re_match_2
447 - _RE_ARGS ((struct re_pattern_buffer *buffer, const char *string1,
448 - int length1, const char *string2, int length2,
449 - int start, struct re_registers *regs, int stop));
506 +extern int re_match_2 (struct re_pattern_buffer *__buffer,
507 + const char *__string1, int __length1,
508 + const char *__string2, int __length2, int __start,
509 + struct re_registers *__regs, int __stop);
510
511
512 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
@@ -461,30 +521,59 @@ extern int re_match_2
521 Unless this function is called, the first search or match using
522 PATTERN_BUFFER will allocate its own register data, without
523 freeing the old data. */
464 -extern void re_set_registers
465 - _RE_ARGS ((struct re_pattern_buffer *buffer, struct re_registers *regs,
466 - unsigned num_regs, regoff_t *starts, regoff_t *ends));
467 -
524 +extern void re_set_registers (struct re_pattern_buffer *__buffer,
525 + struct re_registers *__regs,
526 + unsigned int __num_regs,
527 + regoff_t *__starts, regoff_t *__ends);
528 +#endif /* Use GNU */
529 +
530 +#if defined _REGEX_RE_COMP || (defined _LIBC && defined __USE_BSD)
531 +# ifndef _CRAY
532 /* 4.2 bsd compatibility. */
469 -extern char *re_comp _RE_ARGS ((const char *));
470 -extern int re_exec _RE_ARGS ((const char *));
533 +extern char *re_comp (const char *);
534 +extern int re_exec (const char *);
535 +# endif
536 +#endif
537 +
538 +/* GCC 2.95 and later have "__restrict"; C99 compilers have
539 + "restrict", and "configure" may have defined "restrict". */
540 +#ifndef __restrict
541 +# if ! (2 < __GNUC__ || (2 == __GNUC__ && 95 <= __GNUC_MINOR__))
542 +# if defined restrict || 199901L <= __STDC_VERSION__
543 +# define __restrict restrict
544 +# else
545 +# define __restrict
546 +# endif
547 +# endif
548 +#endif
549 +/* gcc 3.1 and up support the [restrict] syntax. */
550 +#ifndef __restrict_arr
551 +# if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)) \
552 + && !defined __GNUG__
553 +# define __restrict_arr __restrict
554 +# else
555 +# define __restrict_arr
556 +# endif
557 +#endif
558
559 /* POSIX compatibility. */
473 -extern int regcomp _RE_ARGS ((regex_t *preg, const char *pattern, int cflags));
474 -extern int regexec
475 - _RE_ARGS ((const regex_t *preg, const char *string, size_t nmatch,
476 - regmatch_t pmatch[], int eflags));
477 -extern size_t regerror
478 - _RE_ARGS ((int errcode, const regex_t *preg, char *errbuf,
479 - size_t errbuf_size));
480 -extern void regfree _RE_ARGS ((regex_t *preg));
481 -
482 -#endif /* not __REGEXP_LIBRARY_H__ */
483 -
484 -/*
485 -Local variables:
486 -make-backup-files: t
487 -version-control: t
488 -trim-versions-without-asking: nil
489 -End:
490 -*/
560 +extern int regcomp (regex_t *__restrict __preg,
561 + const char *__restrict __pattern,
562 + int __cflags);
563 +
564 +extern int regexec (const regex_t *__restrict __preg,
565 + const char *__restrict __cstring, size_t __nmatch,
566 + regmatch_t __pmatch[__restrict_arr],
567 + int __eflags);
568 +
569 +extern size_t regerror (int __errcode, const regex_t *__restrict __preg,
570 + char *__restrict __errbuf, size_t __errbuf_size);
571 +
572 +extern void regfree (regex_t *__preg);
573 +
574 +
575 +#ifdef __cplusplus
576 +}
577 +#endif /* C++ */
578 +
579 +#endif /* regex.h */
compat/regex/regex_internal.c new
+1744
@@ -0,0 +1,1744 @@
1 +/* Extended regular expression matching and search library.
2 + Copyright (C) 2002-2006, 2010 Free Software Foundation, Inc.
3 + This file is part of the GNU C Library.
4 + Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
5 +
6 + The GNU C Library is free software; you can redistribute it and/or
7 + modify it under the terms of the GNU Lesser General Public
8 + License as published by the Free Software Foundation; either
9 + version 2.1 of the License, or (at your option) any later version.
10 +
11 + The GNU C Library is distributed in the hope that it will be useful,
12 + but WITHOUT ANY WARRANTY; without even the implied warranty of
13 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 + Lesser General Public License for more details.
15 +
16 + You should have received a copy of the GNU Lesser General Public
17 + License along with the GNU C Library; if not, write to the Free
18 + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 + 02110-1301 USA. */
20 +
21 +static void re_string_construct_common (const char *str, int len,
22 + re_string_t *pstr,
23 + RE_TRANSLATE_TYPE trans, int icase,
24 + const re_dfa_t *dfa) internal_function;
25 +static re_dfastate_t *create_ci_newstate (const re_dfa_t *dfa,
26 + const re_node_set *nodes,
27 + unsigned int hash) internal_function;
28 +static re_dfastate_t *create_cd_newstate (const re_dfa_t *dfa,
29 + const re_node_set *nodes,
30 + unsigned int context,
31 + unsigned int hash) internal_function;
32 +
33 +#ifdef GAWK
34 +#undef MAX /* safety */
35 +static int
36 +MAX(size_t a, size_t b)
37 +{
38 + return (a > b ? a : b);
39 +}
40 +#endif
41 +
42 +/* Functions for string operation. */
43 +
44 +/* This function allocate the buffers. It is necessary to call
45 + re_string_reconstruct before using the object. */
46 +
47 +static reg_errcode_t
48 +internal_function
49 +re_string_allocate (re_string_t *pstr, const char *str, int len, int init_len,
50 + RE_TRANSLATE_TYPE trans, int icase, const re_dfa_t *dfa)
51 +{
52 + reg_errcode_t ret;
53 + int init_buf_len;
54 +
55 + /* Ensure at least one character fits into the buffers. */
56 + if (init_len < dfa->mb_cur_max)
57 + init_len = dfa->mb_cur_max;
58 + init_buf_len = (len + 1 < init_len) ? len + 1: init_len;
59 + re_string_construct_common (str, len, pstr, trans, icase, dfa);
60 +
61 + ret = re_string_realloc_buffers (pstr, init_buf_len);
62 + if (BE (ret != REG_NOERROR, 0))
63 + return ret;
64 +
65 + pstr->word_char = dfa->word_char;
66 + pstr->word_ops_used = dfa->word_ops_used;
67 + pstr->mbs = pstr->mbs_allocated ? pstr->mbs : (unsigned char *) str;
68 + pstr->valid_len = (pstr->mbs_allocated || dfa->mb_cur_max > 1) ? 0 : len;
69 + pstr->valid_raw_len = pstr->valid_len;
70 + return REG_NOERROR;
71 +}
72 +
73 +/* This function allocate the buffers, and initialize them. */
74 +
75 +static reg_errcode_t
76 +internal_function
77 +re_string_construct (re_string_t *pstr, const char *str, int len,
78 + RE_TRANSLATE_TYPE trans, int icase, const re_dfa_t *dfa)
79 +{
80 + reg_errcode_t ret;
81 + memset (pstr, '\0', sizeof (re_string_t));
82 + re_string_construct_common (str, len, pstr, trans, icase, dfa);
83 +
84 + if (len > 0)
85 + {
86 + ret = re_string_realloc_buffers (pstr, len + 1);
87 + if (BE (ret != REG_NOERROR, 0))
88 + return ret;
89 + }
90 + pstr->mbs = pstr->mbs_allocated ? pstr->mbs : (unsigned char *) str;
91 +
92 + if (icase)
93 + {
94 +#ifdef RE_ENABLE_I18N
95 + if (dfa->mb_cur_max > 1)
96 + {
97 + while (1)
98 + {
99 + ret = build_wcs_upper_buffer (pstr);
100 + if (BE (ret != REG_NOERROR, 0))
101 + return ret;
102 + if (pstr->valid_raw_len >= len)
103 + break;
104 + if (pstr->bufs_len > pstr->valid_len + dfa->mb_cur_max)
105 + break;
106 + ret = re_string_realloc_buffers (pstr, pstr->bufs_len * 2);
107 + if (BE (ret != REG_NOERROR, 0))
108 + return ret;
109 + }
110 + }
111 + else
112 +#endif /* RE_ENABLE_I18N */
113 + build_upper_buffer (pstr);
114 + }
115 + else
116 + {
117 +#ifdef RE_ENABLE_I18N
118 + if (dfa->mb_cur_max > 1)
119 + build_wcs_buffer (pstr);
120 + else
121 +#endif /* RE_ENABLE_I18N */
122 + {
123 + if (trans != NULL)
124 + re_string_translate_buffer (pstr);
125 + else
126 + {
127 + pstr->valid_len = pstr->bufs_len;
128 + pstr->valid_raw_len = pstr->bufs_len;
129 + }
130 + }
131 + }
132 +
133 + return REG_NOERROR;
134 +}
135 +
136 +/* Helper functions for re_string_allocate, and re_string_construct. */
137 +
138 +static reg_errcode_t
139 +internal_function
140 +re_string_realloc_buffers (re_string_t *pstr, int new_buf_len)
141 +{
142 +#ifdef RE_ENABLE_I18N
143 + if (pstr->mb_cur_max > 1)
144 + {
145 + wint_t *new_wcs;
146 +
147 + /* Avoid overflow in realloc. */
148 + const size_t max_object_size = MAX (sizeof (wint_t), sizeof (int));
149 + if (BE (SIZE_MAX / max_object_size < new_buf_len, 0))
150 + return REG_ESPACE;
151 +
152 + new_wcs = re_realloc (pstr->wcs, wint_t, new_buf_len);
153 + if (BE (new_wcs == NULL, 0))
154 + return REG_ESPACE;
155 + pstr->wcs = new_wcs;
156 + if (pstr->offsets != NULL)
157 + {
158 + int *new_offsets = re_realloc (pstr->offsets, int, new_buf_len);
159 + if (BE (new_offsets == NULL, 0))
160 + return REG_ESPACE;
161 + pstr->offsets = new_offsets;
162 + }
163 + }
164 +#endif /* RE_ENABLE_I18N */
165 + if (pstr->mbs_allocated)
166 + {
167 + unsigned char *new_mbs = re_realloc (pstr->mbs, unsigned char,
168 + new_buf_len);
169 + if (BE (new_mbs == NULL, 0))
170 + return REG_ESPACE;
171 + pstr->mbs = new_mbs;
172 + }
173 + pstr->bufs_len = new_buf_len;
174 + return REG_NOERROR;
175 +}
176 +
177 +
178 +static void
179 +internal_function
180 +re_string_construct_common (const char *str, int len, re_string_t *pstr,
181 + RE_TRANSLATE_TYPE trans, int icase,
182 + const re_dfa_t *dfa)
183 +{
184 + pstr->raw_mbs = (const unsigned char *) str;
185 + pstr->len = len;
186 + pstr->raw_len = len;
187 + pstr->trans = trans;
188 + pstr->icase = icase ? 1 : 0;
189 + pstr->mbs_allocated = (trans != NULL || icase);
190 + pstr->mb_cur_max = dfa->mb_cur_max;
191 + pstr->is_utf8 = dfa->is_utf8;
192 + pstr->map_notascii = dfa->map_notascii;
193 + pstr->stop = pstr->len;
194 + pstr->raw_stop = pstr->stop;
195 +}
196 +
197 +#ifdef RE_ENABLE_I18N
198 +
199 +/* Build wide character buffer PSTR->WCS.
200 + If the byte sequence of the string are:
201 + <mb1>(0), <mb1>(1), <mb2>(0), <mb2>(1), <sb3>
202 + Then wide character buffer will be:
203 + <wc1> , WEOF , <wc2> , WEOF , <wc3>
204 + We use WEOF for padding, they indicate that the position isn't
205 + a first byte of a multibyte character.
206 +
207 + Note that this function assumes PSTR->VALID_LEN elements are already
208 + built and starts from PSTR->VALID_LEN. */
209 +
210 +static void
211 +internal_function
212 +build_wcs_buffer (re_string_t *pstr)
213 +{
214 +#ifdef _LIBC
215 + unsigned char buf[MB_LEN_MAX];
216 + assert (MB_LEN_MAX >= pstr->mb_cur_max);
217 +#else
218 + unsigned char buf[64];
219 +#endif
220 + mbstate_t prev_st;
221 + int byte_idx, end_idx, remain_len;
222 + size_t mbclen;
223 +
224 + /* Build the buffers from pstr->valid_len to either pstr->len or
225 + pstr->bufs_len. */
226 + end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len;
227 + for (byte_idx = pstr->valid_len; byte_idx < end_idx;)
228 + {
229 + wchar_t wc;
230 + const char *p;
231 +
232 + remain_len = end_idx - byte_idx;
233 + prev_st = pstr->cur_state;
234 + /* Apply the translation if we need. */
235 + if (BE (pstr->trans != NULL, 0))
236 + {
237 + int i, ch;
238 +
239 + for (i = 0; i < pstr->mb_cur_max && i < remain_len; ++i)
240 + {
241 + ch = pstr->raw_mbs [pstr->raw_mbs_idx + byte_idx + i];
242 + buf[i] = pstr->mbs[byte_idx + i] = pstr->trans[ch];
243 + }
244 + p = (const char *) buf;
245 + }
246 + else
247 + p = (const char *) pstr->raw_mbs + pstr->raw_mbs_idx + byte_idx;
248 + mbclen = __mbrtowc (&wc, p, remain_len, &pstr->cur_state);
249 + if (BE (mbclen == (size_t) -2, 0))
250 + {
251 + /* The buffer doesn't have enough space, finish to build. */
252 + pstr->cur_state = prev_st;
253 + break;
254 + }
255 + else if (BE (mbclen == (size_t) -1 || mbclen == 0, 0))
256 + {
257 + /* We treat these cases as a singlebyte character. */
258 + mbclen = 1;
259 + wc = (wchar_t) pstr->raw_mbs[pstr->raw_mbs_idx + byte_idx];
260 + if (BE (pstr->trans != NULL, 0))
261 + wc = pstr->trans[wc];
262 + pstr->cur_state = prev_st;
263 + }
264 +
265 + /* Write wide character and padding. */
266 + pstr->wcs[byte_idx++] = wc;
267 + /* Write paddings. */
268 + for (remain_len = byte_idx + mbclen - 1; byte_idx < remain_len ;)
269 + pstr->wcs[byte_idx++] = WEOF;
270 + }
271 + pstr->valid_len = byte_idx;
272 + pstr->valid_raw_len = byte_idx;
273 +}
274 +
275 +/* Build wide character buffer PSTR->WCS like build_wcs_buffer,
276 + but for REG_ICASE. */
277 +
278 +static reg_errcode_t
279 +internal_function
280 +build_wcs_upper_buffer (re_string_t *pstr)
281 +{
282 + mbstate_t prev_st;
283 + int src_idx, byte_idx, end_idx, remain_len;
284 + size_t mbclen;
285 +#ifdef _LIBC
286 + char buf[MB_LEN_MAX];
287 + assert (MB_LEN_MAX >= pstr->mb_cur_max);
288 +#else
289 + char buf[64];
290 +#endif
291 +
292 + byte_idx = pstr->valid_len;
293 + end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len;
294 +
295 + /* The following optimization assumes that ASCII characters can be
296 + mapped to wide characters with a simple cast. */
297 + if (! pstr->map_notascii && pstr->trans == NULL && !pstr->offsets_needed)
298 + {
299 + while (byte_idx < end_idx)
300 + {
301 + wchar_t wc;
302 +
303 + if (isascii (pstr->raw_mbs[pstr->raw_mbs_idx + byte_idx])
304 + && mbsinit (&pstr->cur_state))
305 + {
306 + /* In case of a singlebyte character. */
307 + pstr->mbs[byte_idx]
308 + = toupper (pstr->raw_mbs[pstr->raw_mbs_idx + byte_idx]);
309 + /* The next step uses the assumption that wchar_t is encoded
310 + ASCII-safe: all ASCII values can be converted like this. */
311 + pstr->wcs[byte_idx] = (wchar_t) pstr->mbs[byte_idx];
312 + ++byte_idx;
313 + continue;
314 + }
315 +
316 + remain_len = end_idx - byte_idx;
317 + prev_st = pstr->cur_state;
318 + mbclen = __mbrtowc (&wc,
319 + ((const char *) pstr->raw_mbs + pstr->raw_mbs_idx
320 + + byte_idx), remain_len, &pstr->cur_state);
321 + if (BE (mbclen + 2 > 2, 1))
322 + {
323 + wchar_t wcu = wc;
324 + if (iswlower (wc))
325 + {
326 + size_t mbcdlen;
327 +
328 + wcu = towupper (wc);
329 + mbcdlen = wcrtomb (buf, wcu, &prev_st);
330 + if (BE (mbclen == mbcdlen, 1))
331 + memcpy (pstr->mbs + byte_idx, buf, mbclen);
332 + else
333 + {
334 + src_idx = byte_idx;
335 + goto offsets_needed;
336 + }
337 + }
338 + else
339 + memcpy (pstr->mbs + byte_idx,
340 + pstr->raw_mbs + pstr->raw_mbs_idx + byte_idx, mbclen);
341 + pstr->wcs[byte_idx++] = wcu;
342 + /* Write paddings. */
343 + for (remain_len = byte_idx + mbclen - 1; byte_idx < remain_len ;)
344 + pstr->wcs[byte_idx++] = WEOF;
345 + }
346 + else if (mbclen == (size_t) -1 || mbclen == 0)
347 + {
348 + /* It is an invalid character or '\0'. Just use the byte. */
349 + int ch = pstr->raw_mbs[pstr->raw_mbs_idx + byte_idx];
350 + pstr->mbs[byte_idx] = ch;
351 + /* And also cast it to wide char. */
352 + pstr->wcs[byte_idx++] = (wchar_t) ch;
353 + if (BE (mbclen == (size_t) -1, 0))
354 + pstr->cur_state = prev_st;
355 + }
356 + else
357 + {
358 + /* The buffer doesn't have enough space, finish to build. */
359 + pstr->cur_state = prev_st;
360 + break;
361 + }
362 + }
363 + pstr->valid_len = byte_idx;
364 + pstr->valid_raw_len = byte_idx;
365 + return REG_NOERROR;
366 + }
367 + else
368 + for (src_idx = pstr->valid_raw_len; byte_idx < end_idx;)
369 + {
370 + wchar_t wc;
371 + const char *p;
372 + offsets_needed:
373 + remain_len = end_idx - byte_idx;
374 + prev_st = pstr->cur_state;
375 + if (BE (pstr->trans != NULL, 0))
376 + {
377 + int i, ch;
378 +
379 + for (i = 0; i < pstr->mb_cur_max && i < remain_len; ++i)
380 + {
381 + ch = pstr->raw_mbs [pstr->raw_mbs_idx + src_idx + i];
382 + buf[i] = pstr->trans[ch];
383 + }
384 + p = (const char *) buf;
385 + }
386 + else
387 + p = (const char *) pstr->raw_mbs + pstr->raw_mbs_idx + src_idx;
388 + mbclen = __mbrtowc (&wc, p, remain_len, &pstr->cur_state);
389 + if (BE (mbclen + 2 > 2, 1))
390 + {
391 + wchar_t wcu = wc;
392 + if (iswlower (wc))
393 + {
394 + size_t mbcdlen;
395 +
396 + wcu = towupper (wc);
397 + mbcdlen = wcrtomb ((char *) buf, wcu, &prev_st);
398 + if (BE (mbclen == mbcdlen, 1))
399 + memcpy (pstr->mbs + byte_idx, buf, mbclen);
400 + else if (mbcdlen != (size_t) -1)
401 + {
402 + size_t i;
403 +
404 + if (byte_idx + mbcdlen > pstr->bufs_len)
405 + {
406 + pstr->cur_state = prev_st;
407 + break;
408 + }
409 +
410 + if (pstr->offsets == NULL)
411 + {
412 + pstr->offsets = re_malloc (int, pstr->bufs_len);
413 +
414 + if (pstr->offsets == NULL)
415 + return REG_ESPACE;
416 + }
417 + if (!pstr->offsets_needed)
418 + {
419 + for (i = 0; i < (size_t) byte_idx; ++i)
420 + pstr->offsets[i] = i;
421 + pstr->offsets_needed = 1;
422 + }
423 +
424 + memcpy (pstr->mbs + byte_idx, buf, mbcdlen);
425 + pstr->wcs[byte_idx] = wcu;
426 + pstr->offsets[byte_idx] = src_idx;
427 + for (i = 1; i < mbcdlen; ++i)
428 + {
429 + pstr->offsets[byte_idx + i]
430 + = src_idx + (i < mbclen ? i : mbclen - 1);
431 + pstr->wcs[byte_idx + i] = WEOF;
432 + }
433 + pstr->len += mbcdlen - mbclen;
434 + if (pstr->raw_stop > src_idx)
435 + pstr->stop += mbcdlen - mbclen;
436 + end_idx = (pstr->bufs_len > pstr->len)
437 + ? pstr->len : pstr->bufs_len;
438 + byte_idx += mbcdlen;
439 + src_idx += mbclen;
440 + continue;
441 + }
442 + else
443 + memcpy (pstr->mbs + byte_idx, p, mbclen);
444 + }
445 + else
446 + memcpy (pstr->mbs + byte_idx, p, mbclen);
447 +
448 + if (BE (pstr->offsets_needed != 0, 0))
449 + {
450 + size_t i;
451 + for (i = 0; i < mbclen; ++i)
452 + pstr->offsets[byte_idx + i] = src_idx + i;
453 + }
454 + src_idx += mbclen;
455 +
456 + pstr->wcs[byte_idx++] = wcu;
457 + /* Write paddings. */
458 + for (remain_len = byte_idx + mbclen - 1; byte_idx < remain_len ;)
459 + pstr->wcs[byte_idx++] = WEOF;
460 + }
461 + else if (mbclen == (size_t) -1 || mbclen == 0)
462 + {
463 + /* It is an invalid character or '\0'. Just use the byte. */
464 + int ch = pstr->raw_mbs[pstr->raw_mbs_idx + src_idx];
465 +
466 + if (BE (pstr->trans != NULL, 0))
467 + ch = pstr->trans [ch];
468 + pstr->mbs[byte_idx] = ch;
469 +
470 + if (BE (pstr->offsets_needed != 0, 0))
471 + pstr->offsets[byte_idx] = src_idx;
472 + ++src_idx;
473 +
474 + /* And also cast it to wide char. */
475 + pstr->wcs[byte_idx++] = (wchar_t) ch;
476 + if (BE (mbclen == (size_t) -1, 0))
477 + pstr->cur_state = prev_st;
478 + }
479 + else
480 + {
481 + /* The buffer doesn't have enough space, finish to build. */
482 + pstr->cur_state = prev_st;
483 + break;
484 + }
485 + }
486 + pstr->valid_len = byte_idx;
487 + pstr->valid_raw_len = src_idx;
488 + return REG_NOERROR;
489 +}
490 +
491 +/* Skip characters until the index becomes greater than NEW_RAW_IDX.
492 + Return the index. */
493 +
494 +static int
495 +internal_function
496 +re_string_skip_chars (re_string_t *pstr, int new_raw_idx, wint_t *last_wc)
497 +{
498 + mbstate_t prev_st;
499 + int rawbuf_idx;
500 + size_t mbclen;
501 + wint_t wc = WEOF;
502 +
503 + /* Skip the characters which are not necessary to check. */
504 + for (rawbuf_idx = pstr->raw_mbs_idx + pstr->valid_raw_len;
505 + rawbuf_idx < new_raw_idx;)
506 + {
507 + wchar_t wc2;
508 + int remain_len = pstr->len - rawbuf_idx;
509 + prev_st = pstr->cur_state;
510 + mbclen = __mbrtowc (&wc2, (const char *) pstr->raw_mbs + rawbuf_idx,
511 + remain_len, &pstr->cur_state);
512 + if (BE (mbclen == (size_t) -2 || mbclen == (size_t) -1 || mbclen == 0, 0))
513 + {
514 + /* We treat these cases as a single byte character. */
515 + if (mbclen == 0 || remain_len == 0)
516 + wc = L'\0';
517 + else
518 + wc = *(unsigned char *) (pstr->raw_mbs + rawbuf_idx);
519 + mbclen = 1;
520 + pstr->cur_state = prev_st;
521 + }
522 + else
523 + wc = (wint_t) wc2;
524 + /* Then proceed the next character. */
525 + rawbuf_idx += mbclen;
526 + }
527 + *last_wc = (wint_t) wc;
528 + return rawbuf_idx;
529 +}
530 +#endif /* RE_ENABLE_I18N */
531 +
532 +/* Build the buffer PSTR->MBS, and apply the translation if we need.
533 + This function is used in case of REG_ICASE. */
534 +
535 +static void
536 +internal_function
537 +build_upper_buffer (re_string_t *pstr)
538 +{
539 + int char_idx, end_idx;
540 + end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len;
541 +
542 + for (char_idx = pstr->valid_len; char_idx < end_idx; ++char_idx)
543 + {
544 + int ch = pstr->raw_mbs[pstr->raw_mbs_idx + char_idx];
545 + if (BE (pstr->trans != NULL, 0))
546 + ch = pstr->trans[ch];
547 + if (islower (ch))
548 + pstr->mbs[char_idx] = toupper (ch);
549 + else
550 + pstr->mbs[char_idx] = ch;
551 + }
552 + pstr->valid_len = char_idx;
553 + pstr->valid_raw_len = char_idx;
554 +}
555 +
556 +/* Apply TRANS to the buffer in PSTR. */
557 +
558 +static void
559 +internal_function
560 +re_string_translate_buffer (re_string_t *pstr)
561 +{
562 + int buf_idx, end_idx;
563 + end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len;
564 +
565 + for (buf_idx = pstr->valid_len; buf_idx < end_idx; ++buf_idx)
566 + {
567 + int ch = pstr->raw_mbs[pstr->raw_mbs_idx + buf_idx];
568 + pstr->mbs[buf_idx] = pstr->trans[ch];
569 + }
570 +
571 + pstr->valid_len = buf_idx;
572 + pstr->valid_raw_len = buf_idx;
573 +}
574 +
575 +/* This function re-construct the buffers.
576 + Concretely, convert to wide character in case of pstr->mb_cur_max > 1,
577 + convert to upper case in case of REG_ICASE, apply translation. */
578 +
579 +static reg_errcode_t
580 +internal_function
581 +re_string_reconstruct (re_string_t *pstr, int idx, int eflags)
582 +{
583 + int offset = idx - pstr->raw_mbs_idx;
584 + if (BE (offset < 0, 0))
585 + {
586 + /* Reset buffer. */
587 +#ifdef RE_ENABLE_I18N
588 + if (pstr->mb_cur_max > 1)
589 + memset (&pstr->cur_state, '\0', sizeof (mbstate_t));
590 +#endif /* RE_ENABLE_I18N */
591 + pstr->len = pstr->raw_len;
592 + pstr->stop = pstr->raw_stop;
593 + pstr->valid_len = 0;
594 + pstr->raw_mbs_idx = 0;
595 + pstr->valid_raw_len = 0;
596 + pstr->offsets_needed = 0;
597 + pstr->tip_context = ((eflags & REG_NOTBOL) ? CONTEXT_BEGBUF
598 + : CONTEXT_NEWLINE | CONTEXT_BEGBUF);
599 + if (!pstr->mbs_allocated)
600 + pstr->mbs = (unsigned char *) pstr->raw_mbs;
601 + offset = idx;
602 + }
603 +
604 + if (BE (offset != 0, 1))
605 + {
606 + /* Should the already checked characters be kept? */
607 + if (BE (offset < pstr->valid_raw_len, 1))
608 + {
609 + /* Yes, move them to the front of the buffer. */
610 +#ifdef RE_ENABLE_I18N
611 + if (BE (pstr->offsets_needed, 0))
612 + {
613 + int low = 0, high = pstr->valid_len, mid;
614 + do
615 + {
616 + mid = (high + low) / 2;
617 + if (pstr->offsets[mid] > offset)
618 + high = mid;
619 + else if (pstr->offsets[mid] < offset)
620 + low = mid + 1;
621 + else
622 + break;
623 + }
624 + while (low < high);
625 + if (pstr->offsets[mid] < offset)
626 + ++mid;
627 + pstr->tip_context = re_string_context_at (pstr, mid - 1,
628 + eflags);
629 + /* This can be quite complicated, so handle specially
630 + only the common and easy case where the character with
631 + different length representation of lower and upper
632 + case is present at or after offset. */
633 + if (pstr->valid_len > offset
634 + && mid == offset && pstr->offsets[mid] == offset)
635 + {
636 + memmove (pstr->wcs, pstr->wcs + offset,
637 + (pstr->valid_len - offset) * sizeof (wint_t));
638 + memmove (pstr->mbs, pstr->mbs + offset, pstr->valid_len - offset);
639 + pstr->valid_len -= offset;
640 + pstr->valid_raw_len -= offset;
641 + for (low = 0; low < pstr->valid_len; low++)
642 + pstr->offsets[low] = pstr->offsets[low + offset] - offset;
643 + }
644 + else
645 + {
646 + /* Otherwise, just find out how long the partial multibyte
647 + character at offset is and fill it with WEOF/255. */
648 + pstr->len = pstr->raw_len - idx + offset;
649 + pstr->stop = pstr->raw_stop - idx + offset;
650 + pstr->offsets_needed = 0;
651 + while (mid > 0 && pstr->offsets[mid - 1] == offset)
652 + --mid;
653 + while (mid < pstr->valid_len)
654 + if (pstr->wcs[mid] != WEOF)
655 + break;
656 + else
657 + ++mid;
658 + if (mid == pstr->valid_len)
659 + pstr->valid_len = 0;
660 + else
661 + {
662 + pstr->valid_len = pstr->offsets[mid] - offset;
663 + if (pstr->valid_len)
664 + {
665 + for (low = 0; low < pstr->valid_len; ++low)
666 + pstr->wcs[low] = WEOF;
667 + memset (pstr->mbs, 255, pstr->valid_len);
668 + }
669 + }
670 + pstr->valid_raw_len = pstr->valid_len;
671 + }
672 + }
673 + else
674 +#endif
675 + {
676 + pstr->tip_context = re_string_context_at (pstr, offset - 1,
677 + eflags);
678 +#ifdef RE_ENABLE_I18N
679 + if (pstr->mb_cur_max > 1)
680 + memmove (pstr->wcs, pstr->wcs + offset,
681 + (pstr->valid_len - offset) * sizeof (wint_t));
682 +#endif /* RE_ENABLE_I18N */
683 + if (BE (pstr->mbs_allocated, 0))
684 + memmove (pstr->mbs, pstr->mbs + offset,
685 + pstr->valid_len - offset);
686 + pstr->valid_len -= offset;
687 + pstr->valid_raw_len -= offset;
688 +#if DEBUG
689 + assert (pstr->valid_len > 0);
690 +#endif
691 + }
692 + }
693 + else
694 + {
695 + /* No, skip all characters until IDX. */
696 + int prev_valid_len = pstr->valid_len;
697 +
698 +#ifdef RE_ENABLE_I18N
699 + if (BE (pstr->offsets_needed, 0))
700 + {
701 + pstr->len = pstr->raw_len - idx + offset;
702 + pstr->stop = pstr->raw_stop - idx + offset;
703 + pstr->offsets_needed = 0;
704 + }
705 +#endif
706 + pstr->valid_len = 0;
707 +#ifdef RE_ENABLE_I18N
708 + if (pstr->mb_cur_max > 1)
709 + {
710 + int wcs_idx;
711 + wint_t wc = WEOF;
712 +
713 + if (pstr->is_utf8)
714 + {
715 + const unsigned char *raw, *p, *end;
716 +
717 + /* Special case UTF-8. Multi-byte chars start with any
718 + byte other than 0x80 - 0xbf. */
719 + raw = pstr->raw_mbs + pstr->raw_mbs_idx;
720 + end = raw + (offset - pstr->mb_cur_max);
721 + if (end < pstr->raw_mbs)
722 + end = pstr->raw_mbs;
723 + p = raw + offset - 1;
724 +#ifdef _LIBC
725 + /* We know the wchar_t encoding is UCS4, so for the simple
726 + case, ASCII characters, skip the conversion step. */
727 + if (isascii (*p) && BE (pstr->trans == NULL, 1))
728 + {
729 + memset (&pstr->cur_state, '\0', sizeof (mbstate_t));
730 + /* pstr->valid_len = 0; */
731 + wc = (wchar_t) *p;
732 + }
733 + else
734 +#endif
735 + for (; p >= end; --p)
736 + if ((*p & 0xc0) != 0x80)
737 + {
738 + mbstate_t cur_state;
739 + wchar_t wc2;
740 + int mlen = raw + pstr->len - p;
741 + unsigned char buf[6];
742 + size_t mbclen;
743 +
744 + if (BE (pstr->trans != NULL, 0))
745 + {
746 + int i = mlen < 6 ? mlen : 6;
747 + while (--i >= 0)
748 + buf[i] = pstr->trans[p[i]];
749 + }
750 + /* XXX Don't use mbrtowc, we know which conversion
751 + to use (UTF-8 -> UCS4). */
752 + memset (&cur_state, 0, sizeof (cur_state));
753 + mbclen = __mbrtowc (&wc2, (const char *) p, mlen,
754 + &cur_state);
755 + if (raw + offset - p <= mbclen
756 + && mbclen < (size_t) -2)
757 + {
758 + memset (&pstr->cur_state, '\0',
759 + sizeof (mbstate_t));
760 + pstr->valid_len = mbclen - (raw + offset - p);
761 + wc = wc2;
762 + }
763 + break;
764 + }
765 + }
766 +
767 + if (wc == WEOF)
768 + pstr->valid_len = re_string_skip_chars (pstr, idx, &wc) - idx;
769 + if (wc == WEOF)
770 + pstr->tip_context
771 + = re_string_context_at (pstr, prev_valid_len - 1, eflags);
772 + else
773 + pstr->tip_context = ((BE (pstr->word_ops_used != 0, 0)
774 + && IS_WIDE_WORD_CHAR (wc))
775 + ? CONTEXT_WORD
776 + : ((IS_WIDE_NEWLINE (wc)
777 + && pstr->newline_anchor)
778 + ? CONTEXT_NEWLINE : 0));
779 + if (BE (pstr->valid_len, 0))
780 + {
781 + for (wcs_idx = 0; wcs_idx < pstr->valid_len; ++wcs_idx)
782 + pstr->wcs[wcs_idx] = WEOF;
783 + if (pstr->mbs_allocated)
784 + memset (pstr->mbs, 255, pstr->valid_len);
785 + }
786 + pstr->valid_raw_len = pstr->valid_len;
787 + }
788 + else
789 +#endif /* RE_ENABLE_I18N */
790 + {
791 + int c = pstr->raw_mbs[pstr->raw_mbs_idx + offset - 1];
792 + pstr->valid_raw_len = 0;
793 + if (pstr->trans)
794 + c = pstr->trans[c];
795 + pstr->tip_context = (bitset_contain (pstr->word_char, c)
796 + ? CONTEXT_WORD
797 + : ((IS_NEWLINE (c) && pstr->newline_anchor)
798 + ? CONTEXT_NEWLINE : 0));
799 + }
800 + }
801 + if (!BE (pstr->mbs_allocated, 0))
802 + pstr->mbs += offset;
803 + }
804 + pstr->raw_mbs_idx = idx;
805 + pstr->len -= offset;
806 + pstr->stop -= offset;
807 +
808 + /* Then build the buffers. */
809 +#ifdef RE_ENABLE_I18N
810 + if (pstr->mb_cur_max > 1)
811 + {
812 + if (pstr->icase)
813 + {
814 + reg_errcode_t ret = build_wcs_upper_buffer (pstr);
815 + if (BE (ret != REG_NOERROR, 0))
816 + return ret;
817 + }
818 + else
819 + build_wcs_buffer (pstr);
820 + }
821 + else
822 +#endif /* RE_ENABLE_I18N */
823 + if (BE (pstr->mbs_allocated, 0))
824 + {
825 + if (pstr->icase)
826 + build_upper_buffer (pstr);
827 + else if (pstr->trans != NULL)
828 + re_string_translate_buffer (pstr);
829 + }
830 + else
831 + pstr->valid_len = pstr->len;
832 +
833 + pstr->cur_idx = 0;
834 + return REG_NOERROR;
835 +}
836 +
837 +static unsigned char
838 +internal_function __attribute ((pure))
839 +re_string_peek_byte_case (const re_string_t *pstr, int idx)
840 +{
841 + int ch, off;
842 +
843 + /* Handle the common (easiest) cases first. */
844 + if (BE (!pstr->mbs_allocated, 1))
845 + return re_string_peek_byte (pstr, idx);
846 +
847 +#ifdef RE_ENABLE_I18N
848 + if (pstr->mb_cur_max > 1
849 + && ! re_string_is_single_byte_char (pstr, pstr->cur_idx + idx))
850 + return re_string_peek_byte (pstr, idx);
851 +#endif
852 +
853 + off = pstr->cur_idx + idx;
854 +#ifdef RE_ENABLE_I18N
855 + if (pstr->offsets_needed)
856 + off = pstr->offsets[off];
857 +#endif
858 +
859 + ch = pstr->raw_mbs[pstr->raw_mbs_idx + off];
860 +
861 +#ifdef RE_ENABLE_I18N
862 + /* Ensure that e.g. for tr_TR.UTF-8 BACKSLASH DOTLESS SMALL LETTER I
863 + this function returns CAPITAL LETTER I instead of first byte of
864 + DOTLESS SMALL LETTER I. The latter would confuse the parser,
865 + since peek_byte_case doesn't advance cur_idx in any way. */
866 + if (pstr->offsets_needed && !isascii (ch))
867 + return re_string_peek_byte (pstr, idx);
868 +#endif
869 +
870 + return ch;
871 +}
872 +
873 +static unsigned char
874 +internal_function __attribute ((pure))
875 +re_string_fetch_byte_case (re_string_t *pstr)
876 +{
877 + if (BE (!pstr->mbs_allocated, 1))
878 + return re_string_fetch_byte (pstr);
879 +
880 +#ifdef RE_ENABLE_I18N
881 + if (pstr->offsets_needed)
882 + {
883 + int off, ch;
884 +
885 + /* For tr_TR.UTF-8 [[:islower:]] there is
886 + [[: CAPITAL LETTER I WITH DOT lower:]] in mbs. Skip
887 + in that case the whole multi-byte character and return
888 + the original letter. On the other side, with
889 + [[: DOTLESS SMALL LETTER I return [[:I, as doing
890 + anything else would complicate things too much. */
891 +
892 + if (!re_string_first_byte (pstr, pstr->cur_idx))
893 + return re_string_fetch_byte (pstr);
894 +
895 + off = pstr->offsets[pstr->cur_idx];
896 + ch = pstr->raw_mbs[pstr->raw_mbs_idx + off];
897 +
898 + if (! isascii (ch))
899 + return re_string_fetch_byte (pstr);
900 +
901 + re_string_skip_bytes (pstr,
902 + re_string_char_size_at (pstr, pstr->cur_idx));
903 + return ch;
904 + }
905 +#endif
906 +
907 + return pstr->raw_mbs[pstr->raw_mbs_idx + pstr->cur_idx++];
908 +}
909 +
910 +static void
911 +internal_function
912 +re_string_destruct (re_string_t *pstr)
913 +{
914 +#ifdef RE_ENABLE_I18N
915 + re_free (pstr->wcs);
916 + re_free (pstr->offsets);
917 +#endif /* RE_ENABLE_I18N */
918 + if (pstr->mbs_allocated)
919 + re_free (pstr->mbs);
920 +}
921 +
922 +/* Return the context at IDX in INPUT. */
923 +
924 +static unsigned int
925 +internal_function
926 +re_string_context_at (const re_string_t *input, int idx, int eflags)
927 +{
928 + int c;
929 + if (BE (idx < 0, 0))
930 + /* In this case, we use the value stored in input->tip_context,
931 + since we can't know the character in input->mbs[-1] here. */
932 + return input->tip_context;
933 + if (BE (idx == input->len, 0))
934 + return ((eflags & REG_NOTEOL) ? CONTEXT_ENDBUF
935 + : CONTEXT_NEWLINE | CONTEXT_ENDBUF);
936 +#ifdef RE_ENABLE_I18N
937 + if (input->mb_cur_max > 1)
938 + {
939 + wint_t wc;
940 + int wc_idx = idx;
941 + while(input->wcs[wc_idx] == WEOF)
942 + {
943 +#ifdef DEBUG
944 + /* It must not happen. */
945 + assert (wc_idx >= 0);
946 +#endif
947 + --wc_idx;
948 + if (wc_idx < 0)
949 + return input->tip_context;
950 + }
951 + wc = input->wcs[wc_idx];
952 + if (BE (input->word_ops_used != 0, 0) && IS_WIDE_WORD_CHAR (wc))
953 + return CONTEXT_WORD;
954 + return (IS_WIDE_NEWLINE (wc) && input->newline_anchor
955 + ? CONTEXT_NEWLINE : 0);
956 + }
957 + else
958 +#endif
959 + {
960 + c = re_string_byte_at (input, idx);
961 + if (bitset_contain (input->word_char, c))
962 + return CONTEXT_WORD;
963 + return IS_NEWLINE (c) && input->newline_anchor ? CONTEXT_NEWLINE : 0;
964 + }
965 +}
966 +
967 +/* Functions for set operation. */
968 +
969 +static reg_errcode_t
970 +internal_function
971 +re_node_set_alloc (re_node_set *set, int size)
972 +{
973 + /*
974 + * ADR: valgrind says size can be 0, which then doesn't
975 + * free the block of size 0. Harumph. This seems
976 + * to work ok, though.
977 + */
978 + if (size == 0)
979 + {
980 + memset(set, 0, sizeof(*set));
981 + return REG_NOERROR;
982 + }
983 + set->alloc = size;
984 + set->nelem = 0;
985 + set->elems = re_malloc (int, size);
986 + if (BE (set->elems == NULL, 0))
987 + return REG_ESPACE;
988 + return REG_NOERROR;
989 +}
990 +
991 +static reg_errcode_t
992 +internal_function
993 +re_node_set_init_1 (re_node_set *set, int elem)
994 +{
995 + set->alloc = 1;
996 + set->nelem = 1;
997 + set->elems = re_malloc (int, 1);
998 + if (BE (set->elems == NULL, 0))
999 + {
1000 + set->alloc = set->nelem = 0;
1001 + return REG_ESPACE;
1002 + }
1003 + set->elems[0] = elem;
1004 + return REG_NOERROR;
1005 +}
1006 +
1007 +static reg_errcode_t
1008 +internal_function
1009 +re_node_set_init_2 (re_node_set *set, int elem1, int elem2)
1010 +{
1011 + set->alloc = 2;
1012 + set->elems = re_malloc (int, 2);
1013 + if (BE (set->elems == NULL, 0))
1014 + return REG_ESPACE;
1015 + if (elem1 == elem2)
1016 + {
1017 + set->nelem = 1;
1018 + set->elems[0] = elem1;
1019 + }
1020 + else
1021 + {
1022 + set->nelem = 2;
1023 + if (elem1 < elem2)
1024 + {
1025 + set->elems[0] = elem1;
1026 + set->elems[1] = elem2;
1027 + }
1028 + else
1029 + {
1030 + set->elems[0] = elem2;
1031 + set->elems[1] = elem1;
1032 + }
1033 + }
1034 + return REG_NOERROR;
1035 +}
1036 +
1037 +static reg_errcode_t
1038 +internal_function
1039 +re_node_set_init_copy (re_node_set *dest, const re_node_set *src)
1040 +{
1041 + dest->nelem = src->nelem;
1042 + if (src->nelem > 0)
1043 + {
1044 + dest->alloc = dest->nelem;
1045 + dest->elems = re_malloc (int, dest->alloc);
1046 + if (BE (dest->elems == NULL, 0))
1047 + {
1048 + dest->alloc = dest->nelem = 0;
1049 + return REG_ESPACE;
1050 + }
1051 + memcpy (dest->elems, src->elems, src->nelem * sizeof (int));
1052 + }
1053 + else
1054 + re_node_set_init_empty (dest);
1055 + return REG_NOERROR;
1056 +}
1057 +
1058 +/* Calculate the intersection of the sets SRC1 and SRC2. And merge it to
1059 + DEST. Return value indicate the error code or REG_NOERROR if succeeded.
1060 + Note: We assume dest->elems is NULL, when dest->alloc is 0. */
1061 +
1062 +static reg_errcode_t
1063 +internal_function
1064 +re_node_set_add_intersect (re_node_set *dest, const re_node_set *src1,
1065 + const re_node_set *src2)
1066 +{
1067 + int i1, i2, is, id, delta, sbase;
1068 + if (src1->nelem == 0 || src2->nelem == 0)
1069 + return REG_NOERROR;
1070 +
1071 + /* We need dest->nelem + 2 * elems_in_intersection; this is a
1072 + conservative estimate. */
1073 + if (src1->nelem + src2->nelem + dest->nelem > dest->alloc)
1074 + {
1075 + int new_alloc = src1->nelem + src2->nelem + dest->alloc;
1076 + int *new_elems = re_realloc (dest->elems, int, new_alloc);
1077 + if (BE (new_elems == NULL, 0))
1078 + return REG_ESPACE;
1079 + dest->elems = new_elems;
1080 + dest->alloc = new_alloc;
1081 + }
1082 +
1083 + /* Find the items in the intersection of SRC1 and SRC2, and copy
1084 + into the top of DEST those that are not already in DEST itself. */
1085 + sbase = dest->nelem + src1->nelem + src2->nelem;
1086 + i1 = src1->nelem - 1;
1087 + i2 = src2->nelem - 1;
1088 + id = dest->nelem - 1;
1089 + for (;;)
1090 + {
1091 + if (src1->elems[i1] == src2->elems[i2])
1092 + {
1093 + /* Try to find the item in DEST. Maybe we could binary search? */
1094 + while (id >= 0 && dest->elems[id] > src1->elems[i1])
1095 + --id;
1096 +
1097 + if (id < 0 || dest->elems[id] != src1->elems[i1])
1098 + dest->elems[--sbase] = src1->elems[i1];
1099 +
1100 + if (--i1 < 0 || --i2 < 0)
1101 + break;
1102 + }
1103 +
1104 + /* Lower the highest of the two items. */
1105 + else if (src1->elems[i1] < src2->elems[i2])
1106 + {
1107 + if (--i2 < 0)
1108 + break;
1109 + }
1110 + else
1111 + {
1112 + if (--i1 < 0)
1113 + break;
1114 + }
1115 + }
1116 +
1117 + id = dest->nelem - 1;
1118 + is = dest->nelem + src1->nelem + src2->nelem - 1;
1119 + delta = is - sbase + 1;
1120 +
1121 + /* Now copy. When DELTA becomes zero, the remaining
1122 + DEST elements are already in place; this is more or
1123 + less the same loop that is in re_node_set_merge. */
1124 + dest->nelem += delta;
1125 + if (delta > 0 && id >= 0)
1126 + for (;;)
1127 + {
1128 + if (dest->elems[is] > dest->elems[id])
1129 + {
1130 + /* Copy from the top. */
1131 + dest->elems[id + delta--] = dest->elems[is--];
1132 + if (delta == 0)
1133 + break;
1134 + }
1135 + else
1136 + {
1137 + /* Slide from the bottom. */
1138 + dest->elems[id + delta] = dest->elems[id];
1139 + if (--id < 0)
1140 + break;
1141 + }
1142 + }
1143 +
1144 + /* Copy remaining SRC elements. */
1145 + memcpy (dest->elems, dest->elems + sbase, delta * sizeof (int));
1146 +
1147 + return REG_NOERROR;
1148 +}
1149 +
1150 +/* Calculate the union set of the sets SRC1 and SRC2. And store it to
1151 + DEST. Return value indicate the error code or REG_NOERROR if succeeded. */
1152 +
1153 +static reg_errcode_t
1154 +internal_function
1155 +re_node_set_init_union (re_node_set *dest, const re_node_set *src1,
1156 + const re_node_set *src2)
1157 +{
1158 + int i1, i2, id;
1159 + if (src1 != NULL && src1->nelem > 0 && src2 != NULL && src2->nelem > 0)
1160 + {
1161 + dest->alloc = src1->nelem + src2->nelem;
1162 + dest->elems = re_malloc (int, dest->alloc);
1163 + if (BE (dest->elems == NULL, 0))
1164 + return REG_ESPACE;
1165 + }
1166 + else
1167 + {
1168 + if (src1 != NULL && src1->nelem > 0)
1169 + return re_node_set_init_copy (dest, src1);
1170 + else if (src2 != NULL && src2->nelem > 0)
1171 + return re_node_set_init_copy (dest, src2);
1172 + else
1173 + re_node_set_init_empty (dest);
1174 + return REG_NOERROR;
1175 + }
1176 + for (i1 = i2 = id = 0 ; i1 < src1->nelem && i2 < src2->nelem ;)
1177 + {
1178 + if (src1->elems[i1] > src2->elems[i2])
1179 + {
1180 + dest->elems[id++] = src2->elems[i2++];
1181 + continue;
1182 + }
1183 + if (src1->elems[i1] == src2->elems[i2])
1184 + ++i2;
1185 + dest->elems[id++] = src1->elems[i1++];
1186 + }
1187 + if (i1 < src1->nelem)
1188 + {
1189 + memcpy (dest->elems + id, src1->elems + i1,
1190 + (src1->nelem - i1) * sizeof (int));
1191 + id += src1->nelem - i1;
1192 + }
1193 + else if (i2 < src2->nelem)
1194 + {
1195 + memcpy (dest->elems + id, src2->elems + i2,
1196 + (src2->nelem - i2) * sizeof (int));
1197 + id += src2->nelem - i2;
1198 + }
1199 + dest->nelem = id;
1200 + return REG_NOERROR;
1201 +}
1202 +
1203 +/* Calculate the union set of the sets DEST and SRC. And store it to
1204 + DEST. Return value indicate the error code or REG_NOERROR if succeeded. */
1205 +
1206 +static reg_errcode_t
1207 +internal_function
1208 +re_node_set_merge (re_node_set *dest, const re_node_set *src)
1209 +{
1210 + int is, id, sbase, delta;
1211 + if (src == NULL || src->nelem == 0)
1212 + return REG_NOERROR;
1213 + if (dest->alloc < 2 * src->nelem + dest->nelem)
1214 + {
1215 + int new_alloc = 2 * (src->nelem + dest->alloc);
1216 + int *new_buffer = re_realloc (dest->elems, int, new_alloc);
1217 + if (BE (new_buffer == NULL, 0))
1218 + return REG_ESPACE;
1219 + dest->elems = new_buffer;
1220 + dest->alloc = new_alloc;
1221 + }
1222 +
1223 + if (BE (dest->nelem == 0, 0))
1224 + {
1225 + dest->nelem = src->nelem;
1226 + memcpy (dest->elems, src->elems, src->nelem * sizeof (int));
1227 + return REG_NOERROR;
1228 + }
1229 +
1230 + /* Copy into the top of DEST the items of SRC that are not
1231 + found in DEST. Maybe we could binary search in DEST? */
1232 + for (sbase = dest->nelem + 2 * src->nelem,
1233 + is = src->nelem - 1, id = dest->nelem - 1; is >= 0 && id >= 0; )
1234 + {
1235 + if (dest->elems[id] == src->elems[is])
1236 + is--, id--;
1237 + else if (dest->elems[id] < src->elems[is])
1238 + dest->elems[--sbase] = src->elems[is--];
1239 + else /* if (dest->elems[id] > src->elems[is]) */
1240 + --id;
1241 + }
1242 +
1243 + if (is >= 0)
1244 + {
1245 + /* If DEST is exhausted, the remaining items of SRC must be unique. */
1246 + sbase -= is + 1;
1247 + memcpy (dest->elems + sbase, src->elems, (is + 1) * sizeof (int));
1248 + }
1249 +
1250 + id = dest->nelem - 1;
1251 + is = dest->nelem + 2 * src->nelem - 1;
1252 + delta = is - sbase + 1;
1253 + if (delta == 0)
1254 + return REG_NOERROR;
1255 +
1256 + /* Now copy. When DELTA becomes zero, the remaining
1257 + DEST elements are already in place. */
1258 + dest->nelem += delta;
1259 + for (;;)
1260 + {
1261 + if (dest->elems[is] > dest->elems[id])
1262 + {
1263 + /* Copy from the top. */
1264 + dest->elems[id + delta--] = dest->elems[is--];
1265 + if (delta == 0)
1266 + break;
1267 + }
1268 + else
1269 + {
1270 + /* Slide from the bottom. */
1271 + dest->elems[id + delta] = dest->elems[id];
1272 + if (--id < 0)
1273 + {
1274 + /* Copy remaining SRC elements. */
1275 + memcpy (dest->elems, dest->elems + sbase,
1276 + delta * sizeof (int));
1277 + break;
1278 + }
1279 + }
1280 + }
1281 +
1282 + return REG_NOERROR;
1283 +}
1284 +
1285 +/* Insert the new element ELEM to the re_node_set* SET.
1286 + SET should not already have ELEM.
1287 + return -1 if an error is occured, return 1 otherwise. */
1288 +
1289 +static int
1290 +internal_function
1291 +re_node_set_insert (re_node_set *set, int elem)
1292 +{
1293 + int idx;
1294 + /* In case the set is empty. */
1295 + if (set->alloc == 0)
1296 + {
1297 + if (BE (re_node_set_init_1 (set, elem) == REG_NOERROR, 1))
1298 + return 1;
1299 + else
1300 + return -1;
1301 + }
1302 +
1303 + if (BE (set->nelem, 0) == 0)
1304 + {
1305 + /* We already guaranteed above that set->alloc != 0. */
1306 + set->elems[0] = elem;
1307 + ++set->nelem;
1308 + return 1;
1309 + }
1310 +
1311 + /* Realloc if we need. */
1312 + if (set->alloc == set->nelem)
1313 + {
1314 + int *new_elems;
1315 + set->alloc = set->alloc * 2;
1316 + new_elems = re_realloc (set->elems, int, set->alloc);
1317 + if (BE (new_elems == NULL, 0))
1318 + return -1;
1319 + set->elems = new_elems;
1320 + }
1321 +
1322 + /* Move the elements which follows the new element. Test the
1323 + first element separately to skip a check in the inner loop. */
1324 + if (elem < set->elems[0])
1325 + {
1326 + idx = 0;
1327 + for (idx = set->nelem; idx > 0; idx--)
1328 + set->elems[idx] = set->elems[idx - 1];
1329 + }
1330 + else
1331 + {
1332 + for (idx = set->nelem; set->elems[idx - 1] > elem; idx--)
1333 + set->elems[idx] = set->elems[idx - 1];
1334 + }
1335 +
1336 + /* Insert the new element. */
1337 + set->elems[idx] = elem;
1338 + ++set->nelem;
1339 + return 1;
1340 +}
1341 +
1342 +/* Insert the new element ELEM to the re_node_set* SET.
1343 + SET should not already have any element greater than or equal to ELEM.
1344 + Return -1 if an error is occured, return 1 otherwise. */
1345 +
1346 +static int
1347 +internal_function
1348 +re_node_set_insert_last (re_node_set *set, int elem)
1349 +{
1350 + /* Realloc if we need. */
1351 + if (set->alloc == set->nelem)
1352 + {
1353 + int *new_elems;
1354 + set->alloc = (set->alloc + 1) * 2;
1355 + new_elems = re_realloc (set->elems, int, set->alloc);
1356 + if (BE (new_elems == NULL, 0))
1357 + return -1;
1358 + set->elems = new_elems;
1359 + }
1360 +
1361 + /* Insert the new element. */
1362 + set->elems[set->nelem++] = elem;
1363 + return 1;
1364 +}
1365 +
1366 +/* Compare two node sets SET1 and SET2.
1367 + return 1 if SET1 and SET2 are equivalent, return 0 otherwise. */
1368 +
1369 +static int
1370 +internal_function __attribute ((pure))
1371 +re_node_set_compare (const re_node_set *set1, const re_node_set *set2)
1372 +{
1373 + int i;
1374 + if (set1 == NULL || set2 == NULL || set1->nelem != set2->nelem)
1375 + return 0;
1376 + for (i = set1->nelem ; --i >= 0 ; )
1377 + if (set1->elems[i] != set2->elems[i])
1378 + return 0;
1379 + return 1;
1380 +}
1381 +
1382 +/* Return (idx + 1) if SET contains the element ELEM, return 0 otherwise. */
1383 +
1384 +static int
1385 +internal_function __attribute ((pure))
1386 +re_node_set_contains (const re_node_set *set, int elem)
1387 +{
1388 + unsigned int idx, right, mid;
1389 + if (set->nelem <= 0)
1390 + return 0;
1391 +
1392 + /* Binary search the element. */
1393 + idx = 0;
1394 + right = set->nelem - 1;
1395 + while (idx < right)
1396 + {
1397 + mid = (idx + right) / 2;
1398 + if (set->elems[mid] < elem)
1399 + idx = mid + 1;
1400 + else
1401 + right = mid;
1402 + }
1403 + return set->elems[idx] == elem ? idx + 1 : 0;
1404 +}
1405 +
1406 +static void
1407 +internal_function
1408 +re_node_set_remove_at (re_node_set *set, int idx)
1409 +{
1410 + if (idx < 0 || idx >= set->nelem)
1411 + return;
1412 + --set->nelem;
1413 + for (; idx < set->nelem; idx++)
1414 + set->elems[idx] = set->elems[idx + 1];
1415 +}
1416 +
1417 +
1418 +/* Add the token TOKEN to dfa->nodes, and return the index of the token.
1419 + Or return -1, if an error will be occured. */
1420 +
1421 +static int
1422 +internal_function
1423 +re_dfa_add_node (re_dfa_t *dfa, re_token_t token)
1424 +{
1425 + if (BE (dfa->nodes_len >= dfa->nodes_alloc, 0))
1426 + {
1427 + size_t new_nodes_alloc = dfa->nodes_alloc * 2;
1428 + int *new_nexts, *new_indices;
1429 + re_node_set *new_edests, *new_eclosures;
1430 + re_token_t *new_nodes;
1431 +
1432 + /* Avoid overflows in realloc. */
1433 + const size_t max_object_size = MAX (sizeof (re_token_t),
1434 + MAX (sizeof (re_node_set),
1435 + sizeof (int)));
1436 + if (BE (SIZE_MAX / max_object_size < new_nodes_alloc, 0))
1437 + return -1;
1438 +
1439 + new_nodes = re_realloc (dfa->nodes, re_token_t, new_nodes_alloc);
1440 + if (BE (new_nodes == NULL, 0))
1441 + return -1;
1442 + dfa->nodes = new_nodes;
1443 + new_nexts = re_realloc (dfa->nexts, int, new_nodes_alloc);
1444 + new_indices = re_realloc (dfa->org_indices, int, new_nodes_alloc);
1445 + new_edests = re_realloc (dfa->edests, re_node_set, new_nodes_alloc);
1446 + new_eclosures = re_realloc (dfa->eclosures, re_node_set, new_nodes_alloc);
1447 + if (BE (new_nexts == NULL || new_indices == NULL
1448 + || new_edests == NULL || new_eclosures == NULL, 0))
1449 + return -1;
1450 + dfa->nexts = new_nexts;
1451 + dfa->org_indices = new_indices;
1452 + dfa->edests = new_edests;
1453 + dfa->eclosures = new_eclosures;
1454 + dfa->nodes_alloc = new_nodes_alloc;
1455 + }
1456 + dfa->nodes[dfa->nodes_len] = token;
1457 + dfa->nodes[dfa->nodes_len].constraint = 0;
1458 +#ifdef RE_ENABLE_I18N
1459 + dfa->nodes[dfa->nodes_len].accept_mb =
1460 + (token.type == OP_PERIOD && dfa->mb_cur_max > 1) || token.type == COMPLEX_BRACKET;
1461 +#endif
1462 + dfa->nexts[dfa->nodes_len] = -1;
1463 + re_node_set_init_empty (dfa->edests + dfa->nodes_len);
1464 + re_node_set_init_empty (dfa->eclosures + dfa->nodes_len);
1465 + return dfa->nodes_len++;
1466 +}
1467 +
1468 +static inline unsigned int
1469 +internal_function
1470 +calc_state_hash (const re_node_set *nodes, unsigned int context)
1471 +{
1472 + unsigned int hash = nodes->nelem + context;
1473 + int i;
1474 + for (i = 0 ; i < nodes->nelem ; i++)
1475 + hash += nodes->elems[i];
1476 + return hash;
1477 +}
1478 +
1479 +/* Search for the state whose node_set is equivalent to NODES.
1480 + Return the pointer to the state, if we found it in the DFA.
1481 + Otherwise create the new one and return it. In case of an error
1482 + return NULL and set the error code in ERR.
1483 + Note: - We assume NULL as the invalid state, then it is possible that
1484 + return value is NULL and ERR is REG_NOERROR.
1485 + - We never return non-NULL value in case of any errors, it is for
1486 + optimization. */
1487 +
1488 +static re_dfastate_t *
1489 +internal_function
1490 +re_acquire_state (reg_errcode_t *err, const re_dfa_t *dfa,
1491 + const re_node_set *nodes)
1492 +{
1493 + unsigned int hash;
1494 + re_dfastate_t *new_state;
1495 + struct re_state_table_entry *spot;
1496 + int i;
1497 + if (BE (nodes->nelem == 0, 0))
1498 + {
1499 + *err = REG_NOERROR;
1500 + return NULL;
1501 + }
1502 + hash = calc_state_hash (nodes, 0);
1503 + spot = dfa->state_table + (hash & dfa->state_hash_mask);
1504 +
1505 + for (i = 0 ; i < spot->num ; i++)
1506 + {
1507 + re_dfastate_t *state = spot->array[i];
1508 + if (hash != state->hash)
1509 + continue;
1510 + if (re_node_set_compare (&state->nodes, nodes))
1511 + return state;
1512 + }
1513 +
1514 + /* There are no appropriate state in the dfa, create the new one. */
1515 + new_state = create_ci_newstate (dfa, nodes, hash);
1516 + if (BE (new_state == NULL, 0))
1517 + *err = REG_ESPACE;
1518 +
1519 + return new_state;
1520 +}
1521 +
1522 +/* Search for the state whose node_set is equivalent to NODES and
1523 + whose context is equivalent to CONTEXT.
1524 + Return the pointer to the state, if we found it in the DFA.
1525 + Otherwise create the new one and return it. In case of an error
1526 + return NULL and set the error code in ERR.
1527 + Note: - We assume NULL as the invalid state, then it is possible that
1528 + return value is NULL and ERR is REG_NOERROR.
1529 + - We never return non-NULL value in case of any errors, it is for
1530 + optimization. */
1531 +
1532 +static re_dfastate_t *
1533 +internal_function
1534 +re_acquire_state_context (reg_errcode_t *err, const re_dfa_t *dfa,
1535 + const re_node_set *nodes, unsigned int context)
1536 +{
1537 + unsigned int hash;
1538 + re_dfastate_t *new_state;
1539 + struct re_state_table_entry *spot;
1540 + int i;
1541 + if (nodes->nelem == 0)
1542 + {
1543 + *err = REG_NOERROR;
1544 + return NULL;
1545 + }
1546 + hash = calc_state_hash (nodes, context);
1547 + spot = dfa->state_table + (hash & dfa->state_hash_mask);
1548 +
1549 + for (i = 0 ; i < spot->num ; i++)
1550 + {
1551 + re_dfastate_t *state = spot->array[i];
1552 + if (state->hash == hash
1553 + && state->context == context
1554 + && re_node_set_compare (state->entrance_nodes, nodes))
1555 + return state;
1556 + }
1557 + /* There are no appropriate state in `dfa', create the new one. */
1558 + new_state = create_cd_newstate (dfa, nodes, context, hash);
1559 + if (BE (new_state == NULL, 0))
1560 + *err = REG_ESPACE;
1561 +
1562 + return new_state;
1563 +}
1564 +
1565 +/* Finish initialization of the new state NEWSTATE, and using its hash value
1566 + HASH put in the appropriate bucket of DFA's state table. Return value
1567 + indicates the error code if failed. */
1568 +
1569 +static reg_errcode_t
1570 +register_state (const re_dfa_t *dfa, re_dfastate_t *newstate,
1571 + unsigned int hash)
1572 +{
1573 + struct re_state_table_entry *spot;
1574 + reg_errcode_t err;
1575 + int i;
1576 +
1577 + newstate->hash = hash;
1578 + err = re_node_set_alloc (&newstate->non_eps_nodes, newstate->nodes.nelem);
1579 + if (BE (err != REG_NOERROR, 0))
1580 + return REG_ESPACE;
1581 + for (i = 0; i < newstate->nodes.nelem; i++)
1582 + {
1583 + int elem = newstate->nodes.elems[i];
1584 + if (!IS_EPSILON_NODE (dfa->nodes[elem].type))
1585 + if (re_node_set_insert_last (&newstate->non_eps_nodes, elem) < 0)
1586 + return REG_ESPACE;
1587 + }
1588 +
1589 + spot = dfa->state_table + (hash & dfa->state_hash_mask);
1590 + if (BE (spot->alloc <= spot->num, 0))
1591 + {
1592 + int new_alloc = 2 * spot->num + 2;
1593 + re_dfastate_t **new_array = re_realloc (spot->array, re_dfastate_t *,
1594 + new_alloc);
1595 + if (BE (new_array == NULL, 0))
1596 + return REG_ESPACE;
1597 + spot->array = new_array;
1598 + spot->alloc = new_alloc;
1599 + }
1600 + spot->array[spot->num++] = newstate;
1601 + return REG_NOERROR;
1602 +}
1603 +
1604 +static void
1605 +free_state (re_dfastate_t *state)
1606 +{
1607 + re_node_set_free (&state->non_eps_nodes);
1608 + re_node_set_free (&state->inveclosure);
1609 + if (state->entrance_nodes != &state->nodes)
1610 + {
1611 + re_node_set_free (state->entrance_nodes);
1612 + re_free (state->entrance_nodes);
1613 + }
1614 + re_node_set_free (&state->nodes);
1615 + re_free (state->word_trtable);
1616 + re_free (state->trtable);
1617 + re_free (state);
1618 +}
1619 +
1620 +/* Create the new state which is independ of contexts.
1621 + Return the new state if succeeded, otherwise return NULL. */
1622 +
1623 +static re_dfastate_t *
1624 +internal_function
1625 +create_ci_newstate (const re_dfa_t *dfa, const re_node_set *nodes,
1626 + unsigned int hash)
1627 +{
1628 + int i;
1629 + reg_errcode_t err;
1630 + re_dfastate_t *newstate;
1631 +
1632 + newstate = (re_dfastate_t *) calloc (sizeof (re_dfastate_t), 1);
1633 + if (BE (newstate == NULL, 0))
1634 + return NULL;
1635 + err = re_node_set_init_copy (&newstate->nodes, nodes);
1636 + if (BE (err != REG_NOERROR, 0))
1637 + {
1638 + re_free (newstate);
1639 + return NULL;
1640 + }
1641 +
1642 + newstate->entrance_nodes = &newstate->nodes;
1643 + for (i = 0 ; i < nodes->nelem ; i++)
1644 + {
1645 + re_token_t *node = dfa->nodes + nodes->elems[i];
1646 + re_token_type_t type = node->type;
1647 + if (type == CHARACTER && !node->constraint)
1648 + continue;
1649 +#ifdef RE_ENABLE_I18N
1650 + newstate->accept_mb |= node->accept_mb;
1651 +#endif /* RE_ENABLE_I18N */
1652 +
1653 + /* If the state has the halt node, the state is a halt state. */
1654 + if (type == END_OF_RE)
1655 + newstate->halt = 1;
1656 + else if (type == OP_BACK_REF)
1657 + newstate->has_backref = 1;
1658 + else if (type == ANCHOR || node->constraint)
1659 + newstate->has_constraint = 1;
1660 + }
1661 + err = register_state (dfa, newstate, hash);
1662 + if (BE (err != REG_NOERROR, 0))
1663 + {
1664 + free_state (newstate);
1665 + newstate = NULL;
1666 + }
1667 + return newstate;
1668 +}
1669 +
1670 +/* Create the new state which is depend on the context CONTEXT.
1671 + Return the new state if succeeded, otherwise return NULL. */
1672 +
1673 +static re_dfastate_t *
1674 +internal_function
1675 +create_cd_newstate (const re_dfa_t *dfa, const re_node_set *nodes,
1676 + unsigned int context, unsigned int hash)
1677 +{
1678 + int i, nctx_nodes = 0;
1679 + reg_errcode_t err;
1680 + re_dfastate_t *newstate;
1681 +
1682 + newstate = (re_dfastate_t *) calloc (sizeof (re_dfastate_t), 1);
1683 + if (BE (newstate == NULL, 0))
1684 + return NULL;
1685 + err = re_node_set_init_copy (&newstate->nodes, nodes);
1686 + if (BE (err != REG_NOERROR, 0))
1687 + {
1688 + re_free (newstate);
1689 + return NULL;
1690 + }
1691 +
1692 + newstate->context = context;
1693 + newstate->entrance_nodes = &newstate->nodes;
1694 +
1695 + for (i = 0 ; i < nodes->nelem ; i++)
1696 + {
1697 + re_token_t *node = dfa->nodes + nodes->elems[i];
1698 + re_token_type_t type = node->type;
1699 + unsigned int constraint = node->constraint;
1700 +
1701 + if (type == CHARACTER && !constraint)
1702 + continue;
1703 +#ifdef RE_ENABLE_I18N
1704 + newstate->accept_mb |= node->accept_mb;
1705 +#endif /* RE_ENABLE_I18N */
1706 +
1707 + /* If the state has the halt node, the state is a halt state. */
1708 + if (type == END_OF_RE)
1709 + newstate->halt = 1;
1710 + else if (type == OP_BACK_REF)
1711 + newstate->has_backref = 1;
1712 +
1713 + if (constraint)
1714 + {
1715 + if (newstate->entrance_nodes == &newstate->nodes)
1716 + {
1717 + newstate->entrance_nodes = re_malloc (re_node_set, 1);
1718 + if (BE (newstate->entrance_nodes == NULL, 0))
1719 + {
1720 + free_state (newstate);
1721 + return NULL;
1722 + }
1723 + if (re_node_set_init_copy (newstate->entrance_nodes, nodes)
1724 + != REG_NOERROR)
1725 + return NULL;
1726 + nctx_nodes = 0;
1727 + newstate->has_constraint = 1;
1728 + }
1729 +
1730 + if (NOT_SATISFY_PREV_CONSTRAINT (constraint,context))
1731 + {
1732 + re_node_set_remove_at (&newstate->nodes, i - nctx_nodes);
1733 + ++nctx_nodes;
1734 + }
1735 + }
1736 + }
1737 + err = register_state (dfa, newstate, hash);
1738 + if (BE (err != REG_NOERROR, 0))
1739 + {
1740 + free_state (newstate);
1741 + newstate = NULL;
1742 + }
1743 + return newstate;
1744 +}
compat/regex/regex_internal.h new
+810
@@ -0,0 +1,810 @@
1 +/* Extended regular expression matching and search library.
2 + Copyright (C) 2002-2005, 2007, 2008, 2010 Free Software Foundation, Inc.
3 + This file is part of the GNU C Library.
4 + Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
5 +
6 + The GNU C Library is free software; you can redistribute it and/or
7 + modify it under the terms of the GNU Lesser General Public
8 + License as published by the Free Software Foundation; either
9 + version 2.1 of the License, or (at your option) any later version.
10 +
11 + The GNU C Library is distributed in the hope that it will be useful,
12 + but WITHOUT ANY WARRANTY; without even the implied warranty of
13 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 + Lesser General Public License for more details.
15 +
16 + You should have received a copy of the GNU Lesser General Public
17 + License along with the GNU C Library; if not, write to the Free
18 + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 + 02111-1307 USA. */
20 +
21 +#ifndef _REGEX_INTERNAL_H
22 +#define _REGEX_INTERNAL_H 1
23 +
24 +#include <assert.h>
25 +#include <ctype.h>
26 +#include <stdio.h>
27 +#include <stdlib.h>
28 +#include <string.h>
29 +
30 +#if defined HAVE_LANGINFO_H || defined HAVE_LANGINFO_CODESET || defined _LIBC
31 +# include <langinfo.h>
32 +#endif
33 +#if defined HAVE_LOCALE_H || defined _LIBC
34 +# include <locale.h>
35 +#endif
36 +#if defined HAVE_WCHAR_H || defined _LIBC
37 +# include <wchar.h>
38 +#endif /* HAVE_WCHAR_H || _LIBC */
39 +#if defined HAVE_WCTYPE_H || defined _LIBC
40 +# include <wctype.h>
41 +#endif /* HAVE_WCTYPE_H || _LIBC */
42 +#if defined HAVE_STDBOOL_H || defined _LIBC
43 +# include <stdbool.h>
44 +#endif /* HAVE_STDBOOL_H || _LIBC */
45 +#if !defined(ZOS_USS)
46 +#if defined HAVE_STDINT_H || defined _LIBC
47 +# include <stdint.h>
48 +#endif /* HAVE_STDINT_H || _LIBC */
49 +#endif /* !ZOS_USS */
50 +#if defined _LIBC
51 +# include <bits/libc-lock.h>
52 +#else
53 +# define __libc_lock_define(CLASS,NAME)
54 +# define __libc_lock_init(NAME) do { } while (0)
55 +# define __libc_lock_lock(NAME) do { } while (0)
56 +# define __libc_lock_unlock(NAME) do { } while (0)
57 +#endif
58 +
59 +#ifndef GAWK
60 +/* In case that the system doesn't have isblank(). */
61 +#if !defined _LIBC && !defined HAVE_ISBLANK && !defined isblank
62 +# define isblank(ch) ((ch) == ' ' || (ch) == '\t')
63 +#endif
64 +#else /* GAWK */
65 +/*
66 + * This is a freaking mess. On glibc systems you have to define
67 + * a magic constant to get isblank() out of <ctype.h>, since it's
68 + * a C99 function. To heck with all that and borrow a page from
69 + * dfa.c's book.
70 + */
71 +
72 +static int
73 +is_blank (int c)
74 +{
75 + return (c == ' ' || c == '\t');
76 +}
77 +#endif /* GAWK */
78 +
79 +#ifdef _LIBC
80 +# ifndef _RE_DEFINE_LOCALE_FUNCTIONS
81 +# define _RE_DEFINE_LOCALE_FUNCTIONS 1
82 +# include <locale/localeinfo.h>
83 +# include <locale/elem-hash.h>
84 +# include <locale/coll-lookup.h>
85 +# endif
86 +#endif
87 +
88 +/* This is for other GNU distributions with internationalized messages. */
89 +#if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
90 +# include <libintl.h>
91 +# ifdef _LIBC
92 +# undef gettext
93 +# define gettext(msgid) \
94 + INTUSE(__dcgettext) (_libc_intl_domainname, msgid, LC_MESSAGES)
95 +# endif
96 +#else
97 +# define gettext(msgid) (msgid)
98 +#endif
99 +
100 +#ifndef gettext_noop
101 +/* This define is so xgettext can find the internationalizable
102 + strings. */
103 +# define gettext_noop(String) String
104 +#endif
105 +
106 +/* For loser systems without the definition. */
107 +#ifndef SIZE_MAX
108 +# define SIZE_MAX ((size_t) -1)
109 +#endif
110 +
111 +#ifndef NO_MBSUPPORT
112 +#include "mbsupport.h" /* gawk */
113 +#endif
114 +#ifndef MB_CUR_MAX
115 +#define MB_CUR_MAX 1
116 +#endif
117 +
118 +#if (defined MBS_SUPPORT) || _LIBC
119 +# define RE_ENABLE_I18N
120 +#endif
121 +
122 +#if __GNUC__ >= 3
123 +# define BE(expr, val) __builtin_expect (expr, val)
124 +#else
125 +# define BE(expr, val) (expr)
126 +# ifdef inline
127 +# undef inline
128 +# endif
129 +# define inline
130 +#endif
131 +
132 +/* Number of single byte character. */
133 +#define SBC_MAX 256
134 +
135 +#define COLL_ELEM_LEN_MAX 8
136 +
137 +/* The character which represents newline. */
138 +#define NEWLINE_CHAR '\n'
139 +#define WIDE_NEWLINE_CHAR L'\n'
140 +
141 +/* Rename to standard API for using out of glibc. */
142 +#ifndef _LIBC
143 +# ifdef __wctype
144 +# undef __wctype
145 +# endif
146 +# define __wctype wctype
147 +# ifdef __iswctype
148 +# undef __iswctype
149 +# endif
150 +# define __iswctype iswctype
151 +# define __btowc btowc
152 +# define __mbrtowc mbrtowc
153 +#undef __mempcpy /* GAWK */
154 +# define __mempcpy mempcpy
155 +# define __wcrtomb wcrtomb
156 +# define __regfree regfree
157 +# define attribute_hidden
158 +#endif /* not _LIBC */
159 +
160 +#ifdef __GNUC__
161 +# define __attribute(arg) __attribute__ (arg)
162 +#else
163 +# define __attribute(arg)
164 +#endif
165 +
166 +extern const char __re_error_msgid[] attribute_hidden;
167 +extern const size_t __re_error_msgid_idx[] attribute_hidden;
168 +
169 +/* An integer used to represent a set of bits. It must be unsigned,
170 + and must be at least as wide as unsigned int. */
171 +typedef unsigned long int bitset_word_t;
172 +/* All bits set in a bitset_word_t. */
173 +#define BITSET_WORD_MAX ULONG_MAX
174 +/* Number of bits in a bitset_word_t. */
175 +#define BITSET_WORD_BITS (sizeof (bitset_word_t) * CHAR_BIT)
176 +/* Number of bitset_word_t in a bit_set. */
177 +#define BITSET_WORDS (SBC_MAX / BITSET_WORD_BITS)
178 +typedef bitset_word_t bitset_t[BITSET_WORDS];
179 +typedef bitset_word_t *re_bitset_ptr_t;
180 +typedef const bitset_word_t *re_const_bitset_ptr_t;
181 +
182 +#define bitset_set(set,i) \
183 + (set[i / BITSET_WORD_BITS] |= (bitset_word_t) 1 << i % BITSET_WORD_BITS)
184 +#define bitset_clear(set,i) \
185 + (set[i / BITSET_WORD_BITS] &= ~((bitset_word_t) 1 << i % BITSET_WORD_BITS))
186 +#define bitset_contain(set,i) \
187 + (set[i / BITSET_WORD_BITS] & ((bitset_word_t) 1 << i % BITSET_WORD_BITS))
188 +#define bitset_empty(set) memset (set, '\0', sizeof (bitset_t))
189 +#define bitset_set_all(set) memset (set, '\xff', sizeof (bitset_t))
190 +#define bitset_copy(dest,src) memcpy (dest, src, sizeof (bitset_t))
191 +
192 +#define PREV_WORD_CONSTRAINT 0x0001
193 +#define PREV_NOTWORD_CONSTRAINT 0x0002
194 +#define NEXT_WORD_CONSTRAINT 0x0004
195 +#define NEXT_NOTWORD_CONSTRAINT 0x0008
196 +#define PREV_NEWLINE_CONSTRAINT 0x0010
197 +#define NEXT_NEWLINE_CONSTRAINT 0x0020
198 +#define PREV_BEGBUF_CONSTRAINT 0x0040
199 +#define NEXT_ENDBUF_CONSTRAINT 0x0080
200 +#define WORD_DELIM_CONSTRAINT 0x0100
201 +#define NOT_WORD_DELIM_CONSTRAINT 0x0200
202 +
203 +typedef enum
204 +{
205 + INSIDE_WORD = PREV_WORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
206 + WORD_FIRST = PREV_NOTWORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
207 + WORD_LAST = PREV_WORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
208 + INSIDE_NOTWORD = PREV_NOTWORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
209 + LINE_FIRST = PREV_NEWLINE_CONSTRAINT,
210 + LINE_LAST = NEXT_NEWLINE_CONSTRAINT,
211 + BUF_FIRST = PREV_BEGBUF_CONSTRAINT,
212 + BUF_LAST = NEXT_ENDBUF_CONSTRAINT,
213 + WORD_DELIM = WORD_DELIM_CONSTRAINT,
214 + NOT_WORD_DELIM = NOT_WORD_DELIM_CONSTRAINT
215 +} re_context_type;
216 +
217 +typedef struct
218 +{
219 + int alloc;
220 + int nelem;
221 + int *elems;
222 +} re_node_set;
223 +
224 +typedef enum
225 +{
226 + NON_TYPE = 0,
227 +
228 + /* Node type, These are used by token, node, tree. */
229 + CHARACTER = 1,
230 + END_OF_RE = 2,
231 + SIMPLE_BRACKET = 3,
232 + OP_BACK_REF = 4,
233 + OP_PERIOD = 5,
234 +#ifdef RE_ENABLE_I18N
235 + COMPLEX_BRACKET = 6,
236 + OP_UTF8_PERIOD = 7,
237 +#endif /* RE_ENABLE_I18N */
238 +
239 + /* We define EPSILON_BIT as a macro so that OP_OPEN_SUBEXP is used
240 + when the debugger shows values of this enum type. */
241 +#define EPSILON_BIT 8
242 + OP_OPEN_SUBEXP = EPSILON_BIT | 0,
243 + OP_CLOSE_SUBEXP = EPSILON_BIT | 1,
244 + OP_ALT = EPSILON_BIT | 2,
245 + OP_DUP_ASTERISK = EPSILON_BIT | 3,
246 + ANCHOR = EPSILON_BIT | 4,
247 +
248 + /* Tree type, these are used only by tree. */
249 + CONCAT = 16,
250 + SUBEXP = 17,
251 +
252 + /* Token type, these are used only by token. */
253 + OP_DUP_PLUS = 18,
254 + OP_DUP_QUESTION,
255 + OP_OPEN_BRACKET,
256 + OP_CLOSE_BRACKET,
257 + OP_CHARSET_RANGE,
258 + OP_OPEN_DUP_NUM,
259 + OP_CLOSE_DUP_NUM,
260 + OP_NON_MATCH_LIST,
261 + OP_OPEN_COLL_ELEM,
262 + OP_CLOSE_COLL_ELEM,
263 + OP_OPEN_EQUIV_CLASS,
264 + OP_CLOSE_EQUIV_CLASS,
265 + OP_OPEN_CHAR_CLASS,
266 + OP_CLOSE_CHAR_CLASS,
267 + OP_WORD,
268 + OP_NOTWORD,
269 + OP_SPACE,
270 + OP_NOTSPACE,
271 + BACK_SLASH
272 +
273 +} re_token_type_t;
274 +
275 +#ifdef RE_ENABLE_I18N
276 +typedef struct
277 +{
278 + /* Multibyte characters. */
279 + wchar_t *mbchars;
280 +
281 + /* Collating symbols. */
282 +# ifdef _LIBC
283 + int32_t *coll_syms;
284 +# endif
285 +
286 + /* Equivalence classes. */
287 +# ifdef _LIBC
288 + int32_t *equiv_classes;
289 +# endif
290 +
291 + /* Range expressions. */
292 +# ifdef _LIBC
293 + uint32_t *range_starts;
294 + uint32_t *range_ends;
295 +# else /* not _LIBC */
296 + wchar_t *range_starts;
297 + wchar_t *range_ends;
298 +# endif /* not _LIBC */
299 +
300 + /* Character classes. */
301 + wctype_t *char_classes;
302 +
303 + /* If this character set is the non-matching list. */
304 + unsigned int non_match : 1;
305 +
306 + /* # of multibyte characters. */
307 + int nmbchars;
308 +
309 + /* # of collating symbols. */
310 + int ncoll_syms;
311 +
312 + /* # of equivalence classes. */
313 + int nequiv_classes;
314 +
315 + /* # of range expressions. */
316 + int nranges;
317 +
318 + /* # of character classes. */
319 + int nchar_classes;
320 +} re_charset_t;
321 +#endif /* RE_ENABLE_I18N */
322 +
323 +typedef struct
324 +{
325 + union
326 + {
327 + unsigned char c; /* for CHARACTER */
328 + re_bitset_ptr_t sbcset; /* for SIMPLE_BRACKET */
329 +#ifdef RE_ENABLE_I18N
330 + re_charset_t *mbcset; /* for COMPLEX_BRACKET */
331 +#endif /* RE_ENABLE_I18N */
332 + int idx; /* for BACK_REF */
333 + re_context_type ctx_type; /* for ANCHOR */
334 + } opr;
335 +#if __GNUC__ >= 2
336 + re_token_type_t type : 8;
337 +#else
338 + re_token_type_t type;
339 +#endif
340 + unsigned int constraint : 10; /* context constraint */
341 + unsigned int duplicated : 1;
342 + unsigned int opt_subexp : 1;
343 +#ifdef RE_ENABLE_I18N
344 + unsigned int accept_mb : 1;
345 + /* These 2 bits can be moved into the union if needed (e.g. if running out
346 + of bits; move opr.c to opr.c.c and move the flags to opr.c.flags). */
347 + unsigned int mb_partial : 1;
348 +#endif
349 + unsigned int word_char : 1;
350 +} re_token_t;
351 +
352 +#define IS_EPSILON_NODE(type) ((type) & EPSILON_BIT)
353 +
354 +struct re_string_t
355 +{
356 + /* Indicate the raw buffer which is the original string passed as an
357 + argument of regexec(), re_search(), etc.. */
358 + const unsigned char *raw_mbs;
359 + /* Store the multibyte string. In case of "case insensitive mode" like
360 + REG_ICASE, upper cases of the string are stored, otherwise MBS points
361 + the same address that RAW_MBS points. */
362 + unsigned char *mbs;
363 +#ifdef RE_ENABLE_I18N
364 + /* Store the wide character string which is corresponding to MBS. */
365 + wint_t *wcs;
366 + int *offsets;
367 + mbstate_t cur_state;
368 +#endif
369 + /* Index in RAW_MBS. Each character mbs[i] corresponds to
370 + raw_mbs[raw_mbs_idx + i]. */
371 + int raw_mbs_idx;
372 + /* The length of the valid characters in the buffers. */
373 + int valid_len;
374 + /* The corresponding number of bytes in raw_mbs array. */
375 + int valid_raw_len;
376 + /* The length of the buffers MBS and WCS. */
377 + int bufs_len;
378 + /* The index in MBS, which is updated by re_string_fetch_byte. */
379 + int cur_idx;
380 + /* length of RAW_MBS array. */
381 + int raw_len;
382 + /* This is RAW_LEN - RAW_MBS_IDX + VALID_LEN - VALID_RAW_LEN. */
383 + int len;
384 + /* End of the buffer may be shorter than its length in the cases such
385 + as re_match_2, re_search_2. Then, we use STOP for end of the buffer
386 + instead of LEN. */
387 + int raw_stop;
388 + /* This is RAW_STOP - RAW_MBS_IDX adjusted through OFFSETS. */
389 + int stop;
390 +
391 + /* The context of mbs[0]. We store the context independently, since
392 + the context of mbs[0] may be different from raw_mbs[0], which is
393 + the beginning of the input string. */
394 + unsigned int tip_context;
395 + /* The translation passed as a part of an argument of re_compile_pattern. */
396 + RE_TRANSLATE_TYPE trans;
397 + /* Copy of re_dfa_t's word_char. */
398 + re_const_bitset_ptr_t word_char;
399 + /* 1 if REG_ICASE. */
400 + unsigned char icase;
401 + unsigned char is_utf8;
402 + unsigned char map_notascii;
403 + unsigned char mbs_allocated;
404 + unsigned char offsets_needed;
405 + unsigned char newline_anchor;
406 + unsigned char word_ops_used;
407 + int mb_cur_max;
408 +};
409 +typedef struct re_string_t re_string_t;
410 +
411 +
412 +struct re_dfa_t;
413 +typedef struct re_dfa_t re_dfa_t;
414 +
415 +#ifndef _LIBC
416 +# ifdef __i386__
417 +# define internal_function __attribute ((regparm (3), stdcall))
418 +# else
419 +# define internal_function
420 +# endif
421 +#endif
422 +
423 +#ifndef NOT_IN_libc
424 +static reg_errcode_t re_string_realloc_buffers (re_string_t *pstr,
425 + int new_buf_len)
426 + internal_function;
427 +# ifdef RE_ENABLE_I18N
428 +static void build_wcs_buffer (re_string_t *pstr) internal_function;
429 +static reg_errcode_t build_wcs_upper_buffer (re_string_t *pstr)
430 + internal_function;
431 +# endif /* RE_ENABLE_I18N */
432 +static void build_upper_buffer (re_string_t *pstr) internal_function;
433 +static void re_string_translate_buffer (re_string_t *pstr) internal_function;
434 +static unsigned int re_string_context_at (const re_string_t *input, int idx,
435 + int eflags)
436 + internal_function __attribute ((pure));
437 +#endif
438 +#define re_string_peek_byte(pstr, offset) \
439 + ((pstr)->mbs[(pstr)->cur_idx + offset])
440 +#define re_string_fetch_byte(pstr) \
441 + ((pstr)->mbs[(pstr)->cur_idx++])
442 +#define re_string_first_byte(pstr, idx) \
443 + ((idx) == (pstr)->valid_len || (pstr)->wcs[idx] != WEOF)
444 +#define re_string_is_single_byte_char(pstr, idx) \
445 + ((pstr)->wcs[idx] != WEOF && ((pstr)->valid_len == (idx) + 1 \
446 + || (pstr)->wcs[(idx) + 1] != WEOF))
447 +#define re_string_eoi(pstr) ((pstr)->stop <= (pstr)->cur_idx)
448 +#define re_string_cur_idx(pstr) ((pstr)->cur_idx)
449 +#define re_string_get_buffer(pstr) ((pstr)->mbs)
450 +#define re_string_length(pstr) ((pstr)->len)
451 +#define re_string_byte_at(pstr,idx) ((pstr)->mbs[idx])
452 +#define re_string_skip_bytes(pstr,idx) ((pstr)->cur_idx += (idx))
453 +#define re_string_set_index(pstr,idx) ((pstr)->cur_idx = (idx))
454 +
455 +#ifndef _LIBC
456 +# if HAVE_ALLOCA
457 +# if (_MSC_VER)
458 +# include <malloc.h>
459 +# define __libc_use_alloca(n) 0
460 +# else
461 +# include <alloca.h>
462 +/* The OS usually guarantees only one guard page at the bottom of the stack,
463 + and a page size can be as small as 4096 bytes. So we cannot safely
464 + allocate anything larger than 4096 bytes. Also care for the possibility
465 + of a few compiler-allocated temporary stack slots. */
466 +# define __libc_use_alloca(n) ((n) < 4032)
467 +# endif
468 +# else
469 +/* alloca is implemented with malloc, so just use malloc. */
470 +# define __libc_use_alloca(n) 0
471 +# endif
472 +#endif
473 +
474 +#define re_malloc(t,n) ((t *) malloc ((n) * sizeof (t)))
475 +/* SunOS 4.1.x realloc doesn't accept null pointers: pre-Standard C. Sigh. */
476 +#define re_realloc(p,t,n) ((p != NULL) ? (t *) realloc (p,(n)*sizeof(t)) : (t *) calloc(n,sizeof(t)))
477 +#define re_free(p) free (p)
478 +
479 +struct bin_tree_t
480 +{
481 + struct bin_tree_t *parent;
482 + struct bin_tree_t *left;
483 + struct bin_tree_t *right;
484 + struct bin_tree_t *first;
485 + struct bin_tree_t *next;
486 +
487 + re_token_t token;
488 +
489 + /* `node_idx' is the index in dfa->nodes, if `type' == 0.
490 + Otherwise `type' indicate the type of this node. */
491 + int node_idx;
492 +};
493 +typedef struct bin_tree_t bin_tree_t;
494 +
495 +#define BIN_TREE_STORAGE_SIZE \
496 + ((1024 - sizeof (void *)) / sizeof (bin_tree_t))
497 +
498 +struct bin_tree_storage_t
499 +{
500 + struct bin_tree_storage_t *next;
501 + bin_tree_t data[BIN_TREE_STORAGE_SIZE];
502 +};
503 +typedef struct bin_tree_storage_t bin_tree_storage_t;
504 +
505 +#define CONTEXT_WORD 1
506 +#define CONTEXT_NEWLINE (CONTEXT_WORD << 1)
507 +#define CONTEXT_BEGBUF (CONTEXT_NEWLINE << 1)
508 +#define CONTEXT_ENDBUF (CONTEXT_BEGBUF << 1)
509 +
510 +#define IS_WORD_CONTEXT(c) ((c) & CONTEXT_WORD)
511 +#define IS_NEWLINE_CONTEXT(c) ((c) & CONTEXT_NEWLINE)
512 +#define IS_BEGBUF_CONTEXT(c) ((c) & CONTEXT_BEGBUF)
513 +#define IS_ENDBUF_CONTEXT(c) ((c) & CONTEXT_ENDBUF)
514 +#define IS_ORDINARY_CONTEXT(c) ((c) == 0)
515 +
516 +#define IS_WORD_CHAR(ch) (isalnum (ch) || (ch) == '_')
517 +#define IS_NEWLINE(ch) ((ch) == NEWLINE_CHAR)
518 +#define IS_WIDE_WORD_CHAR(ch) (iswalnum (ch) || (ch) == L'_')
519 +#define IS_WIDE_NEWLINE(ch) ((ch) == WIDE_NEWLINE_CHAR)
520 +
521 +#define NOT_SATISFY_PREV_CONSTRAINT(constraint,context) \
522 + ((((constraint) & PREV_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
523 + || ((constraint & PREV_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
524 + || ((constraint & PREV_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context))\
525 + || ((constraint & PREV_BEGBUF_CONSTRAINT) && !IS_BEGBUF_CONTEXT (context)))
526 +
527 +#define NOT_SATISFY_NEXT_CONSTRAINT(constraint,context) \
528 + ((((constraint) & NEXT_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
529 + || (((constraint) & NEXT_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
530 + || (((constraint) & NEXT_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context)) \
531 + || (((constraint) & NEXT_ENDBUF_CONSTRAINT) && !IS_ENDBUF_CONTEXT (context)))
532 +
533 +struct re_dfastate_t
534 +{
535 + unsigned int hash;
536 + re_node_set nodes;
537 + re_node_set non_eps_nodes;
538 + re_node_set inveclosure;
539 + re_node_set *entrance_nodes;
540 + struct re_dfastate_t **trtable, **word_trtable;
541 + unsigned int context : 4;
542 + unsigned int halt : 1;
543 + /* If this state can accept `multi byte'.
544 + Note that we refer to multibyte characters, and multi character
545 + collating elements as `multi byte'. */
546 + unsigned int accept_mb : 1;
547 + /* If this state has backreference node(s). */
548 + unsigned int has_backref : 1;
549 + unsigned int has_constraint : 1;
550 +};
551 +typedef struct re_dfastate_t re_dfastate_t;
552 +
553 +struct re_state_table_entry
554 +{
555 + int num;
556 + int alloc;
557 + re_dfastate_t **array;
558 +};
559 +
560 +/* Array type used in re_sub_match_last_t and re_sub_match_top_t. */
561 +
562 +typedef struct
563 +{
564 + int next_idx;
565 + int alloc;
566 + re_dfastate_t **array;
567 +} state_array_t;
568 +
569 +/* Store information about the node NODE whose type is OP_CLOSE_SUBEXP. */
570 +
571 +typedef struct
572 +{
573 + int node;
574 + int str_idx; /* The position NODE match at. */
575 + state_array_t path;
576 +} re_sub_match_last_t;
577 +
578 +/* Store information about the node NODE whose type is OP_OPEN_SUBEXP.
579 + And information about the node, whose type is OP_CLOSE_SUBEXP,
580 + corresponding to NODE is stored in LASTS. */
581 +
582 +typedef struct
583 +{
584 + int str_idx;
585 + int node;
586 + state_array_t *path;
587 + int alasts; /* Allocation size of LASTS. */
588 + int nlasts; /* The number of LASTS. */
589 + re_sub_match_last_t **lasts;
590 +} re_sub_match_top_t;
591 +
592 +struct re_backref_cache_entry
593 +{
594 + int node;
595 + int str_idx;
596 + int subexp_from;
597 + int subexp_to;
598 + char more;
599 + char unused;
600 + unsigned short int eps_reachable_subexps_map;
601 +};
602 +
603 +typedef struct
604 +{
605 + /* The string object corresponding to the input string. */
606 + re_string_t input;
607 +#if defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L)
608 + const re_dfa_t *const dfa;
609 +#else
610 + const re_dfa_t *dfa;
611 +#endif
612 + /* EFLAGS of the argument of regexec. */
613 + int eflags;
614 + /* Where the matching ends. */
615 + int match_last;
616 + int last_node;
617 + /* The state log used by the matcher. */
618 + re_dfastate_t **state_log;
619 + int state_log_top;
620 + /* Back reference cache. */
621 + int nbkref_ents;
622 + int abkref_ents;
623 + struct re_backref_cache_entry *bkref_ents;
624 + int max_mb_elem_len;
625 + int nsub_tops;
626 + int asub_tops;
627 + re_sub_match_top_t **sub_tops;
628 +} re_match_context_t;
629 +
630 +typedef struct
631 +{
632 + re_dfastate_t **sifted_states;
633 + re_dfastate_t **limited_states;
634 + int last_node;
635 + int last_str_idx;
636 + re_node_set limits;
637 +} re_sift_context_t;
638 +
639 +struct re_fail_stack_ent_t
640 +{
641 + int idx;
642 + int node;
643 + regmatch_t *regs;
644 + re_node_set eps_via_nodes;
645 +};
646 +
647 +struct re_fail_stack_t
648 +{
649 + int num;
650 + int alloc;
651 + struct re_fail_stack_ent_t *stack;
652 +};
653 +
654 +struct re_dfa_t
655 +{
656 + re_token_t *nodes;
657 + size_t nodes_alloc;
658 + size_t nodes_len;
659 + int *nexts;
660 + int *org_indices;
661 + re_node_set *edests;
662 + re_node_set *eclosures;
663 + re_node_set *inveclosures;
664 + struct re_state_table_entry *state_table;
665 + re_dfastate_t *init_state;
666 + re_dfastate_t *init_state_word;
667 + re_dfastate_t *init_state_nl;
668 + re_dfastate_t *init_state_begbuf;
669 + bin_tree_t *str_tree;
670 + bin_tree_storage_t *str_tree_storage;
671 + re_bitset_ptr_t sb_char;
672 + int str_tree_storage_idx;
673 +
674 + /* number of subexpressions `re_nsub' is in regex_t. */
675 + unsigned int state_hash_mask;
676 + int init_node;
677 + int nbackref; /* The number of backreference in this dfa. */
678 +
679 + /* Bitmap expressing which backreference is used. */
680 + bitset_word_t used_bkref_map;
681 + bitset_word_t completed_bkref_map;
682 +
683 + unsigned int has_plural_match : 1;
684 + /* If this dfa has "multibyte node", which is a backreference or
685 + a node which can accept multibyte character or multi character
686 + collating element. */
687 + unsigned int has_mb_node : 1;
688 + unsigned int is_utf8 : 1;
689 + unsigned int map_notascii : 1;
690 + unsigned int word_ops_used : 1;
691 + int mb_cur_max;
692 + bitset_t word_char;
693 + reg_syntax_t syntax;
694 + int *subexp_map;
695 +#ifdef DEBUG
696 + char* re_str;
697 +#endif
698 +#if defined _LIBC
699 + __libc_lock_define (, lock)
700 +#endif
701 +};
702 +
703 +#define re_node_set_init_empty(set) memset (set, '\0', sizeof (re_node_set))
704 +#define re_node_set_remove(set,id) \
705 + (re_node_set_remove_at (set, re_node_set_contains (set, id) - 1))
706 +#define re_node_set_empty(p) ((p)->nelem = 0)
707 +#define re_node_set_free(set) re_free ((set)->elems)
708 +
709 +
710 +typedef enum
711 +{
712 + SB_CHAR,
713 + MB_CHAR,
714 + EQUIV_CLASS,
715 + COLL_SYM,
716 + CHAR_CLASS
717 +} bracket_elem_type;
718 +
719 +typedef struct
720 +{
721 + bracket_elem_type type;
722 + union
723 + {
724 + unsigned char ch;
725 + unsigned char *name;
726 + wchar_t wch;
727 + } opr;
728 +} bracket_elem_t;
729 +
730 +
731 +/* Inline functions for bitset operation. */
732 +static inline void
733 +bitset_not (bitset_t set)
734 +{
735 + int bitset_i;
736 + for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
737 + set[bitset_i] = ~set[bitset_i];
738 +}
739 +
740 +static inline void
741 +bitset_merge (bitset_t dest, const bitset_t src)
742 +{
743 + int bitset_i;
744 + for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
745 + dest[bitset_i] |= src[bitset_i];
746 +}
747 +
748 +static inline void
749 +bitset_mask (bitset_t dest, const bitset_t src)
750 +{
751 + int bitset_i;
752 + for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
753 + dest[bitset_i] &= src[bitset_i];
754 +}
755 +
756 +#ifdef RE_ENABLE_I18N
757 +/* Inline functions for re_string. */
758 +static inline int
759 +internal_function __attribute ((pure))
760 +re_string_char_size_at (const re_string_t *pstr, int idx)
761 +{
762 + int byte_idx;
763 + if (pstr->mb_cur_max == 1)
764 + return 1;
765 + for (byte_idx = 1; idx + byte_idx < pstr->valid_len; ++byte_idx)
766 + if (pstr->wcs[idx + byte_idx] != WEOF)
767 + break;
768 + return byte_idx;
769 +}
770 +
771 +static inline wint_t
772 +internal_function __attribute ((pure))
773 +re_string_wchar_at (const re_string_t *pstr, int idx)
774 +{
775 + if (pstr->mb_cur_max == 1)
776 + return (wint_t) pstr->mbs[idx];
777 + return (wint_t) pstr->wcs[idx];
778 +}
779 +
780 +# ifndef NOT_IN_libc
781 +static int
782 +internal_function __attribute ((pure))
783 +re_string_elem_size_at (const re_string_t *pstr, int idx)
784 +{
785 +# ifdef _LIBC
786 + const unsigned char *p, *extra;
787 + const int32_t *table, *indirect;
788 + int32_t tmp;
789 +# include <locale/weight.h>
790 + uint_fast32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
791 +
792 + if (nrules != 0)
793 + {
794 + table = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
795 + extra = (const unsigned char *)
796 + _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB);
797 + indirect = (const int32_t *) _NL_CURRENT (LC_COLLATE,
798 + _NL_COLLATE_INDIRECTMB);
799 + p = pstr->mbs + idx;
800 + tmp = findidx (&p);
801 + return p - pstr->mbs - idx;
802 + }
803 + else
804 +# endif /* _LIBC */
805 + return 1;
806 +}
807 +# endif
808 +#endif /* RE_ENABLE_I18N */
809 +
810 +#endif /* _REGEX_INTERNAL_H */
compat/regex/regexec.c new
+4377
@@ -0,0 +1,4377 @@
1 +/* Extended regular expression matching and search library.
2 + Copyright (C) 2002-2005, 2007, 2009, 2010 Free Software Foundation, Inc.
3 + This file is part of the GNU C Library.
4 + Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
5 +
6 + The GNU C Library is free software; you can redistribute it and/or
7 + modify it under the terms of the GNU Lesser General Public
8 + License as published by the Free Software Foundation; either
9 + version 2.1 of the License, or (at your option) any later version.
10 +
11 + The GNU C Library is distributed in the hope that it will be useful,
12 + but WITHOUT ANY WARRANTY; without even the implied warranty of
13 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 + Lesser General Public License for more details.
15 +
16 + You should have received a copy of the GNU Lesser General Public
17 + License along with the GNU C Library; if not, write to the Free
18 + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 + 02110-1301 USA. */
20 +
21 +static reg_errcode_t match_ctx_init (re_match_context_t *cache, int eflags,
22 + int n) internal_function;
23 +static void match_ctx_clean (re_match_context_t *mctx) internal_function;
24 +static void match_ctx_free (re_match_context_t *cache) internal_function;
25 +static reg_errcode_t match_ctx_add_entry (re_match_context_t *cache, int node,
26 + int str_idx, int from, int to)
27 + internal_function;
28 +static int search_cur_bkref_entry (const re_match_context_t *mctx, int str_idx)
29 + internal_function;
30 +static reg_errcode_t match_ctx_add_subtop (re_match_context_t *mctx, int node,
31 + int str_idx) internal_function;
32 +static re_sub_match_last_t * match_ctx_add_sublast (re_sub_match_top_t *subtop,
33 + int node, int str_idx)
34 + internal_function;
35 +static void sift_ctx_init (re_sift_context_t *sctx, re_dfastate_t **sifted_sts,
36 + re_dfastate_t **limited_sts, int last_node,
37 + int last_str_idx)
38 + internal_function;
39 +static reg_errcode_t re_search_internal (const regex_t *preg,
40 + const char *string, int length,
41 + int start, int range, int stop,
42 + size_t nmatch, regmatch_t pmatch[],
43 + int eflags) internal_function;
44 +static int re_search_2_stub (struct re_pattern_buffer *bufp,
45 + const char *string1, int length1,
46 + const char *string2, int length2,
47 + int start, int range, struct re_registers *regs,
48 + int stop, int ret_len) internal_function;
49 +static int re_search_stub (struct re_pattern_buffer *bufp,
50 + const char *string, int length, int start,
51 + int range, int stop, struct re_registers *regs,
52 + int ret_len) internal_function;
53 +static unsigned re_copy_regs (struct re_registers *regs, regmatch_t *pmatch,
54 + int nregs, int regs_allocated) internal_function;
55 +static reg_errcode_t prune_impossible_nodes (re_match_context_t *mctx)
56 + internal_function;
57 +static int check_matching (re_match_context_t *mctx, int fl_longest_match,
58 + int *p_match_first) internal_function;
59 +static int check_halt_state_context (const re_match_context_t *mctx,
60 + const re_dfastate_t *state, int idx)
61 + internal_function;
62 +static void update_regs (const re_dfa_t *dfa, regmatch_t *pmatch,
63 + regmatch_t *prev_idx_match, int cur_node,
64 + int cur_idx, int nmatch) internal_function;
65 +static reg_errcode_t push_fail_stack (struct re_fail_stack_t *fs,
66 + int str_idx, int dest_node, int nregs,
67 + regmatch_t *regs,
68 + re_node_set *eps_via_nodes)
69 + internal_function;
70 +static reg_errcode_t set_regs (const regex_t *preg,
71 + const re_match_context_t *mctx,
72 + size_t nmatch, regmatch_t *pmatch,
73 + int fl_backtrack) internal_function;
74 +static reg_errcode_t free_fail_stack_return (struct re_fail_stack_t *fs)
75 + internal_function;
76 +
77 +#ifdef RE_ENABLE_I18N
78 +static int sift_states_iter_mb (const re_match_context_t *mctx,
79 + re_sift_context_t *sctx,
80 + int node_idx, int str_idx, int max_str_idx)
81 + internal_function;
82 +#endif /* RE_ENABLE_I18N */
83 +static reg_errcode_t sift_states_backward (const re_match_context_t *mctx,
84 + re_sift_context_t *sctx)
85 + internal_function;
86 +static reg_errcode_t build_sifted_states (const re_match_context_t *mctx,
87 + re_sift_context_t *sctx, int str_idx,
88 + re_node_set *cur_dest)
89 + internal_function;
90 +static reg_errcode_t update_cur_sifted_state (const re_match_context_t *mctx,
91 + re_sift_context_t *sctx,
92 + int str_idx,
93 + re_node_set *dest_nodes)
94 + internal_function;
95 +static reg_errcode_t add_epsilon_src_nodes (const re_dfa_t *dfa,
96 + re_node_set *dest_nodes,
97 + const re_node_set *candidates)
98 + internal_function;
99 +static int check_dst_limits (const re_match_context_t *mctx,
100 + re_node_set *limits,
101 + int dst_node, int dst_idx, int src_node,
102 + int src_idx) internal_function;
103 +static int check_dst_limits_calc_pos_1 (const re_match_context_t *mctx,
104 + int boundaries, int subexp_idx,
105 + int from_node, int bkref_idx)
106 + internal_function;
107 +static int check_dst_limits_calc_pos (const re_match_context_t *mctx,
108 + int limit, int subexp_idx,
109 + int node, int str_idx,
110 + int bkref_idx) internal_function;
111 +static reg_errcode_t check_subexp_limits (const re_dfa_t *dfa,
112 + re_node_set *dest_nodes,
113 + const re_node_set *candidates,
114 + re_node_set *limits,
115 + struct re_backref_cache_entry *bkref_ents,
116 + int str_idx) internal_function;
117 +static reg_errcode_t sift_states_bkref (const re_match_context_t *mctx,
118 + re_sift_context_t *sctx,
119 + int str_idx, const re_node_set *candidates)
120 + internal_function;
121 +static reg_errcode_t merge_state_array (const re_dfa_t *dfa,
122 + re_dfastate_t **dst,
123 + re_dfastate_t **src, int num)
124 + internal_function;
125 +static re_dfastate_t *find_recover_state (reg_errcode_t *err,
126 + re_match_context_t *mctx) internal_function;
127 +static re_dfastate_t *transit_state (reg_errcode_t *err,
128 + re_match_context_t *mctx,
129 + re_dfastate_t *state) internal_function;
130 +static re_dfastate_t *merge_state_with_log (reg_errcode_t *err,
131 + re_match_context_t *mctx,
132 + re_dfastate_t *next_state)
133 + internal_function;
134 +static reg_errcode_t check_subexp_matching_top (re_match_context_t *mctx,
135 + re_node_set *cur_nodes,
136 + int str_idx) internal_function;
137 +#if 0
138 +static re_dfastate_t *transit_state_sb (reg_errcode_t *err,
139 + re_match_context_t *mctx,
140 + re_dfastate_t *pstate)
141 + internal_function;
142 +#endif
143 +#ifdef RE_ENABLE_I18N
144 +static reg_errcode_t transit_state_mb (re_match_context_t *mctx,
145 + re_dfastate_t *pstate)
146 + internal_function;
147 +#endif /* RE_ENABLE_I18N */
148 +static reg_errcode_t transit_state_bkref (re_match_context_t *mctx,
149 + const re_node_set *nodes)
150 + internal_function;
151 +static reg_errcode_t get_subexp (re_match_context_t *mctx,
152 + int bkref_node, int bkref_str_idx)
153 + internal_function;
154 +static reg_errcode_t get_subexp_sub (re_match_context_t *mctx,
155 + const re_sub_match_top_t *sub_top,
156 + re_sub_match_last_t *sub_last,
157 + int bkref_node, int bkref_str)
158 + internal_function;
159 +static int find_subexp_node (const re_dfa_t *dfa, const re_node_set *nodes,
160 + int subexp_idx, int type) internal_function;
161 +static reg_errcode_t check_arrival (re_match_context_t *mctx,
162 + state_array_t *path, int top_node,
163 + int top_str, int last_node, int last_str,
164 + int type) internal_function;
165 +static reg_errcode_t check_arrival_add_next_nodes (re_match_context_t *mctx,
166 + int str_idx,
167 + re_node_set *cur_nodes,
168 + re_node_set *next_nodes)
169 + internal_function;
170 +static reg_errcode_t check_arrival_expand_ecl (const re_dfa_t *dfa,
171 + re_node_set *cur_nodes,
172 + int ex_subexp, int type)
173 + internal_function;
174 +static reg_errcode_t check_arrival_expand_ecl_sub (const re_dfa_t *dfa,
175 + re_node_set *dst_nodes,
176 + int target, int ex_subexp,
177 + int type) internal_function;
178 +static reg_errcode_t expand_bkref_cache (re_match_context_t *mctx,
179 + re_node_set *cur_nodes, int cur_str,
180 + int subexp_num, int type)
181 + internal_function;
182 +static int build_trtable (const re_dfa_t *dfa,
183 + re_dfastate_t *state) internal_function;
184 +#ifdef RE_ENABLE_I18N
185 +static int check_node_accept_bytes (const re_dfa_t *dfa, int node_idx,
186 + const re_string_t *input, int idx)
187 + internal_function;
188 +# ifdef _LIBC
189 +static unsigned int find_collation_sequence_value (const unsigned char *mbs,
190 + size_t name_len)
191 + internal_function;
192 +# endif /* _LIBC */
193 +#endif /* RE_ENABLE_I18N */
194 +static int group_nodes_into_DFAstates (const re_dfa_t *dfa,
195 + const re_dfastate_t *state,
196 + re_node_set *states_node,
197 + bitset_t *states_ch) internal_function;
198 +static int check_node_accept (const re_match_context_t *mctx,
199 + const re_token_t *node, int idx)
200 + internal_function;
201 +static reg_errcode_t extend_buffers (re_match_context_t *mctx)
202 + internal_function;
203 +
204 +/* Entry point for POSIX code. */
205 +
206 +/* regexec searches for a given pattern, specified by PREG, in the
207 + string STRING.
208 +
209 + If NMATCH is zero or REG_NOSUB was set in the cflags argument to
210 + `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
211 + least NMATCH elements, and we set them to the offsets of the
212 + corresponding matched substrings.
213 +
214 + EFLAGS specifies `execution flags' which affect matching: if
215 + REG_NOTBOL is set, then ^ does not match at the beginning of the
216 + string; if REG_NOTEOL is set, then $ does not match at the end.
217 +
218 + We return 0 if we find a match and REG_NOMATCH if not. */
219 +
220 +int
221 +regexec (preg, string, nmatch, pmatch, eflags)
222 + const regex_t *__restrict preg;
223 + const char *__restrict string;
224 + size_t nmatch;
225 + regmatch_t pmatch[];
226 + int eflags;
227 +{
228 + reg_errcode_t err;
229 + int start, length;
230 +
231 + if (eflags & ~(REG_NOTBOL | REG_NOTEOL | REG_STARTEND))
232 + return REG_BADPAT;
233 +
234 + if (eflags & REG_STARTEND)
235 + {
236 + start = pmatch[0].rm_so;
237 + length = pmatch[0].rm_eo;
238 + }
239 + else
240 + {
241 + start = 0;
242 + length = strlen (string);
243 + }
244 +
245 + __libc_lock_lock (dfa->lock);
246 + if (preg->no_sub)
247 + err = re_search_internal (preg, string, length, start, length - start,
248 + length, 0, NULL, eflags);
249 + else
250 + err = re_search_internal (preg, string, length, start, length - start,
251 + length, nmatch, pmatch, eflags);
252 + __libc_lock_unlock (dfa->lock);
253 + return err != REG_NOERROR;
254 +}
255 +
256 +#ifdef _LIBC
257 +# include <shlib-compat.h>
258 +versioned_symbol (libc, __regexec, regexec, GLIBC_2_3_4);
259 +
260 +# if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_3_4)
261 +__typeof__ (__regexec) __compat_regexec;
262 +
263 +int
264 +attribute_compat_text_section
265 +__compat_regexec (const regex_t *__restrict preg,
266 + const char *__restrict string, size_t nmatch,
267 + regmatch_t pmatch[], int eflags)
268 +{
269 + return regexec (preg, string, nmatch, pmatch,
270 + eflags & (REG_NOTBOL | REG_NOTEOL));
271 +}
272 +compat_symbol (libc, __compat_regexec, regexec, GLIBC_2_0);
273 +# endif
274 +#endif
275 +
276 +/* Entry points for GNU code. */
277 +
278 +/* re_match, re_search, re_match_2, re_search_2
279 +
280 + The former two functions operate on STRING with length LENGTH,
281 + while the later two operate on concatenation of STRING1 and STRING2
282 + with lengths LENGTH1 and LENGTH2, respectively.
283 +
284 + re_match() matches the compiled pattern in BUFP against the string,
285 + starting at index START.
286 +
287 + re_search() first tries matching at index START, then it tries to match
288 + starting from index START + 1, and so on. The last start position tried
289 + is START + RANGE. (Thus RANGE = 0 forces re_search to operate the same
290 + way as re_match().)
291 +
292 + The parameter STOP of re_{match,search}_2 specifies that no match exceeding
293 + the first STOP characters of the concatenation of the strings should be
294 + concerned.
295 +
296 + If REGS is not NULL, and BUFP->no_sub is not set, the offsets of the match
297 + and all groups is stroed in REGS. (For the "_2" variants, the offsets are
298 + computed relative to the concatenation, not relative to the individual
299 + strings.)
300 +
301 + On success, re_match* functions return the length of the match, re_search*
302 + return the position of the start of the match. Return value -1 means no
303 + match was found and -2 indicates an internal error. */
304 +
305 +int
306 +re_match (bufp, string, length, start, regs)
307 + struct re_pattern_buffer *bufp;
308 + const char *string;
309 + int length, start;
310 + struct re_registers *regs;
311 +{
312 + return re_search_stub (bufp, string, length, start, 0, length, regs, 1);
313 +}
314 +#ifdef _LIBC
315 +weak_alias (__re_match, re_match)
316 +#endif
317 +
318 +int
319 +re_search (bufp, string, length, start, range, regs)
320 + struct re_pattern_buffer *bufp;
321 + const char *string;
322 + int length, start, range;
323 + struct re_registers *regs;
324 +{
325 + return re_search_stub (bufp, string, length, start, range, length, regs, 0);
326 +}
327 +#ifdef _LIBC
328 +weak_alias (__re_search, re_search)
329 +#endif
330 +
331 +int
332 +re_match_2 (bufp, string1, length1, string2, length2, start, regs, stop)
333 + struct re_pattern_buffer *bufp;
334 + const char *string1, *string2;
335 + int length1, length2, start, stop;
336 + struct re_registers *regs;
337 +{
338 + return re_search_2_stub (bufp, string1, length1, string2, length2,
339 + start, 0, regs, stop, 1);
340 +}
341 +#ifdef _LIBC
342 +weak_alias (__re_match_2, re_match_2)
343 +#endif
344 +
345 +int
346 +re_search_2 (bufp, string1, length1, string2, length2, start, range, regs, stop)
347 + struct re_pattern_buffer *bufp;
348 + const char *string1, *string2;
349 + int length1, length2, start, range, stop;
350 + struct re_registers *regs;
351 +{
352 + return re_search_2_stub (bufp, string1, length1, string2, length2,
353 + start, range, regs, stop, 0);
354 +}
355 +#ifdef _LIBC
356 +weak_alias (__re_search_2, re_search_2)
357 +#endif
358 +
359 +static int
360 +re_search_2_stub (bufp, string1, length1, string2, length2, start, range, regs,
361 + stop, ret_len)
362 + struct re_pattern_buffer *bufp;
363 + const char *string1, *string2;
364 + int length1, length2, start, range, stop, ret_len;
365 + struct re_registers *regs;
366 +{
367 + const char *str;
368 + int rval;
369 + int len = length1 + length2;
370 + int free_str = 0;
371 +
372 + if (BE (length1 < 0 || length2 < 0 || stop < 0, 0))
373 + return -2;
374 +
375 + /* Concatenate the strings. */
376 + if (length2 > 0)
377 + if (length1 > 0)
378 + {
379 + char *s = re_malloc (char, len);
380 +
381 + if (BE (s == NULL, 0))
382 + return -2;
383 + memcpy (s, string1, length1);
384 + memcpy (s + length1, string2, length2);
385 + str = s;
386 + free_str = 1;
387 + }
388 + else
389 + str = string2;
390 + else
391 + str = string1;
392 +
393 + rval = re_search_stub (bufp, str, len, start, range, stop, regs, ret_len);
394 + if (free_str)
395 + re_free ((char *) str);
396 + return rval;
397 +}
398 +
399 +/* The parameters have the same meaning as those of re_search.
400 + Additional parameters:
401 + If RET_LEN is nonzero the length of the match is returned (re_match style);
402 + otherwise the position of the match is returned. */
403 +
404 +static int
405 +re_search_stub (bufp, string, length, start, range, stop, regs, ret_len)
406 + struct re_pattern_buffer *bufp;
407 + const char *string;
408 + int length, start, range, stop, ret_len;
409 + struct re_registers *regs;
410 +{
411 + reg_errcode_t result;
412 + regmatch_t *pmatch;
413 + int nregs, rval;
414 + int eflags = 0;
415 +
416 + /* Check for out-of-range. */
417 + if (BE (start < 0 || start > length, 0))
418 + return -1;
419 + if (BE (start + range > length, 0))
420 + range = length - start;
421 + else if (BE (start + range < 0, 0))
422 + range = -start;
423 +
424 + __libc_lock_lock (dfa->lock);
425 +
426 + eflags |= (bufp->not_bol) ? REG_NOTBOL : 0;
427 + eflags |= (bufp->not_eol) ? REG_NOTEOL : 0;
428 +
429 + /* Compile fastmap if we haven't yet. */
430 + if (range > 0 && bufp->fastmap != NULL && !bufp->fastmap_accurate)
431 + re_compile_fastmap (bufp);
432 +
433 + if (BE (bufp->no_sub, 0))
434 + regs = NULL;
435 +
436 + /* We need at least 1 register. */
437 + if (regs == NULL)
438 + nregs = 1;
439 + else if (BE (bufp->regs_allocated == REGS_FIXED &&
440 + regs->num_regs < bufp->re_nsub + 1, 0))
441 + {
442 + nregs = regs->num_regs;
443 + if (BE (nregs < 1, 0))
444 + {
445 + /* Nothing can be copied to regs. */
446 + regs = NULL;
447 + nregs = 1;
448 + }
449 + }
450 + else
451 + nregs = bufp->re_nsub + 1;
452 + pmatch = re_malloc (regmatch_t, nregs);
453 + if (BE (pmatch == NULL, 0))
454 + {
455 + rval = -2;
456 + goto out;
457 + }
458 +
459 + result = re_search_internal (bufp, string, length, start, range, stop,
460 + nregs, pmatch, eflags);
461 +
462 + rval = 0;
463 +
464 + /* I hope we needn't fill ther regs with -1's when no match was found. */
465 + if (result != REG_NOERROR)
466 + rval = -1;
467 + else if (regs != NULL)
468 + {
469 + /* If caller wants register contents data back, copy them. */
470 + bufp->regs_allocated = re_copy_regs (regs, pmatch, nregs,
471 + bufp->regs_allocated);
472 + if (BE (bufp->regs_allocated == REGS_UNALLOCATED, 0))
473 + rval = -2;
474 + }
475 +
476 + if (BE (rval == 0, 1))
477 + {
478 + if (ret_len)
479 + {
480 + assert (pmatch[0].rm_so == start);
481 + rval = pmatch[0].rm_eo - start;
482 + }
483 + else
484 + rval = pmatch[0].rm_so;
485 + }
486 + re_free (pmatch);
487 + out:
488 + __libc_lock_unlock (dfa->lock);
489 + return rval;
490 +}
491 +
492 +static unsigned
493 +re_copy_regs (regs, pmatch, nregs, regs_allocated)
494 + struct re_registers *regs;
495 + regmatch_t *pmatch;
496 + int nregs, regs_allocated;
497 +{
498 + int rval = REGS_REALLOCATE;
499 + int i;
500 + int need_regs = nregs + 1;
501 + /* We need one extra element beyond `num_regs' for the `-1' marker GNU code
502 + uses. */
503 +
504 + /* Have the register data arrays been allocated? */
505 + if (regs_allocated == REGS_UNALLOCATED)
506 + { /* No. So allocate them with malloc. */
507 + regs->start = re_malloc (regoff_t, need_regs);
508 + if (BE (regs->start == NULL, 0))
509 + return REGS_UNALLOCATED;
510 + regs->end = re_malloc (regoff_t, need_regs);
511 + if (BE (regs->end == NULL, 0))
512 + {
513 + re_free (regs->start);
514 + return REGS_UNALLOCATED;
515 + }
516 + regs->num_regs = need_regs;
517 + }
518 + else if (regs_allocated == REGS_REALLOCATE)
519 + { /* Yes. If we need more elements than were already
520 + allocated, reallocate them. If we need fewer, just
521 + leave it alone. */
522 + if (BE (need_regs > regs->num_regs, 0))
523 + {
524 + regoff_t *new_start = re_realloc (regs->start, regoff_t, need_regs);
525 + regoff_t *new_end;
526 + if (BE (new_start == NULL, 0))
527 + return REGS_UNALLOCATED;
528 + new_end = re_realloc (regs->end, regoff_t, need_regs);
529 + if (BE (new_end == NULL, 0))
530 + {
531 + re_free (new_start);
532 + return REGS_UNALLOCATED;
533 + }
534 + regs->start = new_start;
535 + regs->end = new_end;
536 + regs->num_regs = need_regs;
537 + }
538 + }
539 + else
540 + {
541 + assert (regs_allocated == REGS_FIXED);
542 + /* This function may not be called with REGS_FIXED and nregs too big. */
543 + assert (regs->num_regs >= nregs);
544 + rval = REGS_FIXED;
545 + }
546 +
547 + /* Copy the regs. */
548 + for (i = 0; i < nregs; ++i)
549 + {
550 + regs->start[i] = pmatch[i].rm_so;
551 + regs->end[i] = pmatch[i].rm_eo;
552 + }
553 + for ( ; i < regs->num_regs; ++i)
554 + regs->start[i] = regs->end[i] = -1;
555 +
556 + return rval;
557 +}
558 +
559 +/* Set REGS to hold NUM_REGS registers, storing them in STARTS and
560 + ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
561 + this memory for recording register information. STARTS and ENDS
562 + must be allocated using the malloc library routine, and must each
563 + be at least NUM_REGS * sizeof (regoff_t) bytes long.
564 +
565 + If NUM_REGS == 0, then subsequent matches should allocate their own
566 + register data.
567 +
568 + Unless this function is called, the first search or match using
569 + PATTERN_BUFFER will allocate its own register data, without
570 + freeing the old data. */
571 +
572 +void
573 +re_set_registers (bufp, regs, num_regs, starts, ends)
574 + struct re_pattern_buffer *bufp;
575 + struct re_registers *regs;
576 + unsigned num_regs;
577 + regoff_t *starts, *ends;
578 +{
579 + if (num_regs)
580 + {
581 + bufp->regs_allocated = REGS_REALLOCATE;
582 + regs->num_regs = num_regs;
583 + regs->start = starts;
584 + regs->end = ends;
585 + }
586 + else
587 + {
588 + bufp->regs_allocated = REGS_UNALLOCATED;
589 + regs->num_regs = 0;
590 + regs->start = regs->end = (regoff_t *) 0;
591 + }
592 +}
593 +#ifdef _LIBC
594 +weak_alias (__re_set_registers, re_set_registers)
595 +#endif
596 +
597 +/* Entry points compatible with 4.2 BSD regex library. We don't define
598 + them unless specifically requested. */
599 +
600 +#if defined _REGEX_RE_COMP || defined _LIBC
601 +int
602 +# ifdef _LIBC
603 +weak_function
604 +# endif
605 +re_exec (s)
606 + const char *s;
607 +{
608 + return 0 == regexec (&re_comp_buf, s, 0, NULL, 0);
609 +}
610 +#endif /* _REGEX_RE_COMP */
611 +
612 +/* Internal entry point. */
613 +
614 +/* Searches for a compiled pattern PREG in the string STRING, whose
615 + length is LENGTH. NMATCH, PMATCH, and EFLAGS have the same
616 + mingings with regexec. START, and RANGE have the same meanings
617 + with re_search.
618 + Return REG_NOERROR if we find a match, and REG_NOMATCH if not,
619 + otherwise return the error code.
620 + Note: We assume front end functions already check ranges.
621 + (START + RANGE >= 0 && START + RANGE <= LENGTH) */
622 +
623 +static reg_errcode_t
624 +re_search_internal (preg, string, length, start, range, stop, nmatch, pmatch,
625 + eflags)
626 + const regex_t *preg;
627 + const char *string;
628 + int length, start, range, stop, eflags;
629 + size_t nmatch;
630 + regmatch_t pmatch[];
631 +{
632 + reg_errcode_t err;
633 + const re_dfa_t *dfa = (const re_dfa_t *) preg->buffer;
634 + int left_lim, right_lim, incr;
635 + int fl_longest_match, match_first, match_kind, match_last = -1;
636 + int extra_nmatch;
637 + int sb, ch;
638 +#if defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L)
639 + re_match_context_t mctx = { .dfa = dfa };
640 +#else
641 + re_match_context_t mctx;
642 +#endif
643 + char *fastmap = (preg->fastmap != NULL && preg->fastmap_accurate
644 + && range && !preg->can_be_null) ? preg->fastmap : NULL;
645 + RE_TRANSLATE_TYPE t = preg->translate;
646 +
647 +#if !(defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L))
648 + memset (&mctx, '\0', sizeof (re_match_context_t));
649 + mctx.dfa = dfa;
650 +#endif
651 +
652 + extra_nmatch = (nmatch > preg->re_nsub) ? nmatch - (preg->re_nsub + 1) : 0;
653 + nmatch -= extra_nmatch;
654 +
655 + /* Check if the DFA haven't been compiled. */
656 + if (BE (preg->used == 0 || dfa->init_state == NULL
657 + || dfa->init_state_word == NULL || dfa->init_state_nl == NULL
658 + || dfa->init_state_begbuf == NULL, 0))
659 + return REG_NOMATCH;
660 +
661 +#ifdef DEBUG
662 + /* We assume front-end functions already check them. */
663 + assert (start + range >= 0 && start + range <= length);
664 +#endif
665 +
666 + /* If initial states with non-begbuf contexts have no elements,
667 + the regex must be anchored. If preg->newline_anchor is set,
668 + we'll never use init_state_nl, so do not check it. */
669 + if (dfa->init_state->nodes.nelem == 0
670 + && dfa->init_state_word->nodes.nelem == 0
671 + && (dfa->init_state_nl->nodes.nelem == 0
672 + || !preg->newline_anchor))
673 + {
674 + if (start != 0 && start + range != 0)
675 + return REG_NOMATCH;
676 + start = range = 0;
677 + }
678 +
679 + /* We must check the longest matching, if nmatch > 0. */
680 + fl_longest_match = (nmatch != 0 || dfa->nbackref);
681 +
682 + err = re_string_allocate (&mctx.input, string, length, dfa->nodes_len + 1,
683 + preg->translate, preg->syntax & RE_ICASE, dfa);
684 + if (BE (err != REG_NOERROR, 0))
685 + goto free_return;
686 + mctx.input.stop = stop;
687 + mctx.input.raw_stop = stop;
688 + mctx.input.newline_anchor = preg->newline_anchor;
689 +
690 + err = match_ctx_init (&mctx, eflags, dfa->nbackref * 2);
691 + if (BE (err != REG_NOERROR, 0))
692 + goto free_return;
693 +
694 + /* We will log all the DFA states through which the dfa pass,
695 + if nmatch > 1, or this dfa has "multibyte node", which is a
696 + back-reference or a node which can accept multibyte character or
697 + multi character collating element. */
698 + if (nmatch > 1 || dfa->has_mb_node)
699 + {
700 + /* Avoid overflow. */
701 + if (BE (SIZE_MAX / sizeof (re_dfastate_t *) <= mctx.input.bufs_len, 0))
702 + {
703 + err = REG_ESPACE;
704 + goto free_return;
705 + }
706 +
707 + mctx.state_log = re_malloc (re_dfastate_t *, mctx.input.bufs_len + 1);
708 + if (BE (mctx.state_log == NULL, 0))
709 + {
710 + err = REG_ESPACE;
711 + goto free_return;
712 + }
713 + }
714 + else
715 + mctx.state_log = NULL;
716 +
717 + match_first = start;
718 + mctx.input.tip_context = (eflags & REG_NOTBOL) ? CONTEXT_BEGBUF
719 + : CONTEXT_NEWLINE | CONTEXT_BEGBUF;
720 +
721 + /* Check incrementally whether of not the input string match. */
722 + incr = (range < 0) ? -1 : 1;
723 + left_lim = (range < 0) ? start + range : start;
724 + right_lim = (range < 0) ? start : start + range;
725 + sb = dfa->mb_cur_max == 1;
726 + match_kind =
727 + (fastmap
728 + ? ((sb || !(preg->syntax & RE_ICASE || t) ? 4 : 0)
729 + | (range >= 0 ? 2 : 0)
730 + | (t != NULL ? 1 : 0))
731 + : 8);
732 +
733 + for (;; match_first += incr)
734 + {
735 + err = REG_NOMATCH;
736 + if (match_first < left_lim || right_lim < match_first)
737 + goto free_return;
738 +
739 + /* Advance as rapidly as possible through the string, until we
740 + find a plausible place to start matching. This may be done
741 + with varying efficiency, so there are various possibilities:
742 + only the most common of them are specialized, in order to
743 + save on code size. We use a switch statement for speed. */
744 + switch (match_kind)
745 + {
746 + case 8:
747 + /* No fastmap. */
748 + break;
749 +
750 + case 7:
751 + /* Fastmap with single-byte translation, match forward. */
752 + while (BE (match_first < right_lim, 1)
753 + && !fastmap[t[(unsigned char) string[match_first]]])
754 + ++match_first;
755 + goto forward_match_found_start_or_reached_end;
756 +
757 + case 6:
758 + /* Fastmap without translation, match forward. */
759 + while (BE (match_first < right_lim, 1)
760 + && !fastmap[(unsigned char) string[match_first]])
761 + ++match_first;
762 +
763 + forward_match_found_start_or_reached_end:
764 + if (BE (match_first == right_lim, 0))
765 + {
766 + ch = match_first >= length
767 + ? 0 : (unsigned char) string[match_first];
768 + if (!fastmap[t ? t[ch] : ch])
769 + goto free_return;
770 + }
771 + break;
772 +
773 + case 4:
774 + case 5:
775 + /* Fastmap without multi-byte translation, match backwards. */
776 + while (match_first >= left_lim)
777 + {
778 + ch = match_first >= length
779 + ? 0 : (unsigned char) string[match_first];
780 + if (fastmap[t ? t[ch] : ch])
781 + break;
782 + --match_first;
783 + }
784 + if (match_first < left_lim)
785 + goto free_return;
786 + break;
787 +
788 + default:
789 + /* In this case, we can't determine easily the current byte,
790 + since it might be a component byte of a multibyte
791 + character. Then we use the constructed buffer instead. */
792 + for (;;)
793 + {
794 + /* If MATCH_FIRST is out of the valid range, reconstruct the
795 + buffers. */
796 + unsigned int offset = match_first - mctx.input.raw_mbs_idx;
797 + if (BE (offset >= (unsigned int) mctx.input.valid_raw_len, 0))
798 + {
799 + err = re_string_reconstruct (&mctx.input, match_first,
800 + eflags);
801 + if (BE (err != REG_NOERROR, 0))
802 + goto free_return;
803 +
804 + offset = match_first - mctx.input.raw_mbs_idx;
805 + }
806 + /* If MATCH_FIRST is out of the buffer, leave it as '\0'.
807 + Note that MATCH_FIRST must not be smaller than 0. */
808 + ch = (match_first >= length
809 + ? 0 : re_string_byte_at (&mctx.input, offset));
810 + if (fastmap[ch])
811 + break;
812 + match_first += incr;
813 + if (match_first < left_lim || match_first > right_lim)
814 + {
815 + err = REG_NOMATCH;
816 + goto free_return;
817 + }
818 + }
819 + break;
820 + }
821 +
822 + /* Reconstruct the buffers so that the matcher can assume that
823 + the matching starts from the beginning of the buffer. */
824 + err = re_string_reconstruct (&mctx.input, match_first, eflags);
825 + if (BE (err != REG_NOERROR, 0))
826 + goto free_return;
827 +
828 +#ifdef RE_ENABLE_I18N
829 + /* Don't consider this char as a possible match start if it part,
830 + yet isn't the head, of a multibyte character. */
831 + if (!sb && !re_string_first_byte (&mctx.input, 0))
832 + continue;
833 +#endif
834 +
835 + /* It seems to be appropriate one, then use the matcher. */
836 + /* We assume that the matching starts from 0. */
837 + mctx.state_log_top = mctx.nbkref_ents = mctx.max_mb_elem_len = 0;
838 + match_last = check_matching (&mctx, fl_longest_match,
839 + range >= 0 ? &match_first : NULL);
840 + if (match_last != -1)
841 + {
842 + if (BE (match_last == -2, 0))
843 + {
844 + err = REG_ESPACE;
845 + goto free_return;
846 + }
847 + else
848 + {
849 + mctx.match_last = match_last;
850 + if ((!preg->no_sub && nmatch > 1) || dfa->nbackref)
851 + {
852 + re_dfastate_t *pstate = mctx.state_log[match_last];
853 + mctx.last_node = check_halt_state_context (&mctx, pstate,
854 + match_last);
855 + }
856 + if ((!preg->no_sub && nmatch > 1 && dfa->has_plural_match)
857 + || dfa->nbackref)
858 + {
859 + err = prune_impossible_nodes (&mctx);
860 + if (err == REG_NOERROR)
861 + break;
862 + if (BE (err != REG_NOMATCH, 0))
863 + goto free_return;
864 + match_last = -1;
865 + }
866 + else
867 + break; /* We found a match. */
868 + }
869 + }
870 +
871 + match_ctx_clean (&mctx);
872 + }
873 +
874 +#ifdef DEBUG
875 + assert (match_last != -1);
876 + assert (err == REG_NOERROR);
877 +#endif
878 +
879 + /* Set pmatch[] if we need. */
880 + if (nmatch > 0)
881 + {
882 + int reg_idx;
883 +
884 + /* Initialize registers. */
885 + for (reg_idx = 1; reg_idx < nmatch; ++reg_idx)
886 + pmatch[reg_idx].rm_so = pmatch[reg_idx].rm_eo = -1;
887 +
888 + /* Set the points where matching start/end. */
889 + pmatch[0].rm_so = 0;
890 + pmatch[0].rm_eo = mctx.match_last;
891 +
892 + if (!preg->no_sub && nmatch > 1)
893 + {
894 + err = set_regs (preg, &mctx, nmatch, pmatch,
895 + dfa->has_plural_match && dfa->nbackref > 0);
896 + if (BE (err != REG_NOERROR, 0))
897 + goto free_return;
898 + }
899 +
900 + /* At last, add the offset to the each registers, since we slided
901 + the buffers so that we could assume that the matching starts
902 + from 0. */
903 + for (reg_idx = 0; reg_idx < nmatch; ++reg_idx)
904 + if (pmatch[reg_idx].rm_so != -1)
905 + {
906 +#ifdef RE_ENABLE_I18N
907 + if (BE (mctx.input.offsets_needed != 0, 0))
908 + {
909 + pmatch[reg_idx].rm_so =
910 + (pmatch[reg_idx].rm_so == mctx.input.valid_len
911 + ? mctx.input.valid_raw_len
912 + : mctx.input.offsets[pmatch[reg_idx].rm_so]);
913 + pmatch[reg_idx].rm_eo =
914 + (pmatch[reg_idx].rm_eo == mctx.input.valid_len
915 + ? mctx.input.valid_raw_len
916 + : mctx.input.offsets[pmatch[reg_idx].rm_eo]);
917 + }
918 +#else
919 + assert (mctx.input.offsets_needed == 0);
920 +#endif
921 + pmatch[reg_idx].rm_so += match_first;
922 + pmatch[reg_idx].rm_eo += match_first;
923 + }
924 + for (reg_idx = 0; reg_idx < extra_nmatch; ++reg_idx)
925 + {
926 + pmatch[nmatch + reg_idx].rm_so = -1;
927 + pmatch[nmatch + reg_idx].rm_eo = -1;
928 + }
929 +
930 + if (dfa->subexp_map)
931 + for (reg_idx = 0; reg_idx + 1 < nmatch; reg_idx++)
932 + if (dfa->subexp_map[reg_idx] != reg_idx)
933 + {
934 + pmatch[reg_idx + 1].rm_so
935 + = pmatch[dfa->subexp_map[reg_idx] + 1].rm_so;
936 + pmatch[reg_idx + 1].rm_eo
937 + = pmatch[dfa->subexp_map[reg_idx] + 1].rm_eo;
938 + }
939 + }
940 +
941 + free_return:
942 + re_free (mctx.state_log);
943 + if (dfa->nbackref)
944 + match_ctx_free (&mctx);
945 + re_string_destruct (&mctx.input);
946 + return err;
947 +}
948 +
949 +static reg_errcode_t
950 +prune_impossible_nodes (mctx)
951 + re_match_context_t *mctx;
952 +{
953 + const re_dfa_t *const dfa = mctx->dfa;
954 + int halt_node, match_last;
955 + reg_errcode_t ret;
956 + re_dfastate_t **sifted_states;
957 + re_dfastate_t **lim_states = NULL;
958 + re_sift_context_t sctx;
959 +#ifdef DEBUG
960 + assert (mctx->state_log != NULL);
961 +#endif
962 + match_last = mctx->match_last;
963 + halt_node = mctx->last_node;
964 +
965 + /* Avoid overflow. */
966 + if (BE (SIZE_MAX / sizeof (re_dfastate_t *) <= match_last, 0))
967 + return REG_ESPACE;
968 +
969 + sifted_states = re_malloc (re_dfastate_t *, match_last + 1);
970 + if (BE (sifted_states == NULL, 0))
971 + {
972 + ret = REG_ESPACE;
973 + goto free_return;
974 + }
975 + if (dfa->nbackref)
976 + {
977 + lim_states = re_malloc (re_dfastate_t *, match_last + 1);
978 + if (BE (lim_states == NULL, 0))
979 + {
980 + ret = REG_ESPACE;
981 + goto free_return;
982 + }
983 + while (1)
984 + {
985 + memset (lim_states, '\0',
986 + sizeof (re_dfastate_t *) * (match_last + 1));
987 + sift_ctx_init (&sctx, sifted_states, lim_states, halt_node,
988 + match_last);
989 + ret = sift_states_backward (mctx, &sctx);
990 + re_node_set_free (&sctx.limits);
991 + if (BE (ret != REG_NOERROR, 0))
992 + goto free_return;
993 + if (sifted_states[0] != NULL || lim_states[0] != NULL)
994 + break;
995 + do
996 + {
997 + --match_last;
998 + if (match_last < 0)
999 + {
1000 + ret = REG_NOMATCH;
1001 + goto free_return;
1002 + }
1003 + } while (mctx->state_log[match_last] == NULL
1004 + || !mctx->state_log[match_last]->halt);
1005 + halt_node = check_halt_state_context (mctx,
1006 + mctx->state_log[match_last],
1007 + match_last);
1008 + }
1009 + ret = merge_state_array (dfa, sifted_states, lim_states,
1010 + match_last + 1);
1011 + re_free (lim_states);
1012 + lim_states = NULL;
1013 + if (BE (ret != REG_NOERROR, 0))
1014 + goto free_return;
1015 + }
1016 + else
1017 + {
1018 + sift_ctx_init (&sctx, sifted_states, lim_states, halt_node, match_last);
1019 + ret = sift_states_backward (mctx, &sctx);
1020 + re_node_set_free (&sctx.limits);
1021 + if (BE (ret != REG_NOERROR, 0))
1022 + goto free_return;
1023 + if (sifted_states[0] == NULL)
1024 + {
1025 + ret = REG_NOMATCH;
1026 + goto free_return;
1027 + }
1028 + }
1029 + re_free (mctx->state_log);
1030 + mctx->state_log = sifted_states;
1031 + sifted_states = NULL;
1032 + mctx->last_node = halt_node;
1033 + mctx->match_last = match_last;
1034 + ret = REG_NOERROR;
1035 + free_return:
1036 + re_free (sifted_states);
1037 + re_free (lim_states);
1038 + return ret;
1039 +}
1040 +
1041 +/* Acquire an initial state and return it.
1042 + We must select appropriate initial state depending on the context,
1043 + since initial states may have constraints like "\<", "^", etc.. */
1044 +
1045 +static inline re_dfastate_t *
1046 +__attribute ((always_inline)) internal_function
1047 +acquire_init_state_context (reg_errcode_t *err, const re_match_context_t *mctx,
1048 + int idx)
1049 +{
1050 + const re_dfa_t *const dfa = mctx->dfa;
1051 + if (dfa->init_state->has_constraint)
1052 + {
1053 + unsigned int context;
1054 + context = re_string_context_at (&mctx->input, idx - 1, mctx->eflags);
1055 + if (IS_WORD_CONTEXT (context))
1056 + return dfa->init_state_word;
1057 + else if (IS_ORDINARY_CONTEXT (context))
1058 + return dfa->init_state;
1059 + else if (IS_BEGBUF_CONTEXT (context) && IS_NEWLINE_CONTEXT (context))
1060 + return dfa->init_state_begbuf;
1061 + else if (IS_NEWLINE_CONTEXT (context))
1062 + return dfa->init_state_nl;
1063 + else if (IS_BEGBUF_CONTEXT (context))
1064 + {
1065 + /* It is relatively rare case, then calculate on demand. */
1066 + return re_acquire_state_context (err, dfa,
1067 + dfa->init_state->entrance_nodes,
1068 + context);
1069 + }
1070 + else
1071 + /* Must not happen? */
1072 + return dfa->init_state;
1073 + }
1074 + else
1075 + return dfa->init_state;
1076 +}
1077 +
1078 +/* Check whether the regular expression match input string INPUT or not,
1079 + and return the index where the matching end, return -1 if not match,
1080 + or return -2 in case of an error.
1081 + FL_LONGEST_MATCH means we want the POSIX longest matching.
1082 + If P_MATCH_FIRST is not NULL, and the match fails, it is set to the
1083 + next place where we may want to try matching.
1084 + Note that the matcher assume that the maching starts from the current
1085 + index of the buffer. */
1086 +
1087 +static int
1088 +internal_function
1089 +check_matching (re_match_context_t *mctx, int fl_longest_match,
1090 + int *p_match_first)
1091 +{
1092 + const re_dfa_t *const dfa = mctx->dfa;
1093 + reg_errcode_t err;
1094 + int match = 0;
1095 + int match_last = -1;
1096 + int cur_str_idx = re_string_cur_idx (&mctx->input);
1097 + re_dfastate_t *cur_state;
1098 + int at_init_state = p_match_first != NULL;
1099 + int next_start_idx = cur_str_idx;
1100 +
1101 + err = REG_NOERROR;
1102 + cur_state = acquire_init_state_context (&err, mctx, cur_str_idx);
1103 + /* An initial state must not be NULL (invalid). */
1104 + if (BE (cur_state == NULL, 0))
1105 + {
1106 + assert (err == REG_ESPACE);
1107 + return -2;
1108 + }
1109 +
1110 + if (mctx->state_log != NULL)
1111 + {
1112 + mctx->state_log[cur_str_idx] = cur_state;
1113 +
1114 + /* Check OP_OPEN_SUBEXP in the initial state in case that we use them
1115 + later. E.g. Processing back references. */
1116 + if (BE (dfa->nbackref, 0))
1117 + {
1118 + at_init_state = 0;
1119 + err = check_subexp_matching_top (mctx, &cur_state->nodes, 0);
1120 + if (BE (err != REG_NOERROR, 0))
1121 + return err;
1122 +
1123 + if (cur_state->has_backref)
1124 + {
1125 + err = transit_state_bkref (mctx, &cur_state->nodes);
1126 + if (BE (err != REG_NOERROR, 0))
1127 + return err;
1128 + }
1129 + }
1130 + }
1131 +
1132 + /* If the RE accepts NULL string. */
1133 + if (BE (cur_state->halt, 0))
1134 + {
1135 + if (!cur_state->has_constraint
1136 + || check_halt_state_context (mctx, cur_state, cur_str_idx))
1137 + {
1138 + if (!fl_longest_match)
1139 + return cur_str_idx;
1140 + else
1141 + {
1142 + match_last = cur_str_idx;
1143 + match = 1;
1144 + }
1145 + }
1146 + }
1147 +
1148 + while (!re_string_eoi (&mctx->input))
1149 + {
1150 + re_dfastate_t *old_state = cur_state;
1151 + int next_char_idx = re_string_cur_idx (&mctx->input) + 1;
1152 +
1153 + if (BE (next_char_idx >= mctx->input.bufs_len, 0)
1154 + || (BE (next_char_idx >= mctx->input.valid_len, 0)
1155 + && mctx->input.valid_len < mctx->input.len))
1156 + {
1157 + err = extend_buffers (mctx);
1158 + if (BE (err != REG_NOERROR, 0))
1159 + {
1160 + assert (err == REG_ESPACE);
1161 + return -2;
1162 + }
1163 + }
1164 +
1165 + cur_state = transit_state (&err, mctx, cur_state);
1166 + if (mctx->state_log != NULL)
1167 + cur_state = merge_state_with_log (&err, mctx, cur_state);
1168 +
1169 + if (cur_state == NULL)
1170 + {
1171 + /* Reached the invalid state or an error. Try to recover a valid
1172 + state using the state log, if available and if we have not
1173 + already found a valid (even if not the longest) match. */
1174 + if (BE (err != REG_NOERROR, 0))
1175 + return -2;
1176 +
1177 + if (mctx->state_log == NULL
1178 + || (match && !fl_longest_match)
1179 + || (cur_state = find_recover_state (&err, mctx)) == NULL)
1180 + break;
1181 + }
1182 +
1183 + if (BE (at_init_state, 0))
1184 + {
1185 + if (old_state == cur_state)
1186 + next_start_idx = next_char_idx;
1187 + else
1188 + at_init_state = 0;
1189 + }
1190 +
1191 + if (cur_state->halt)
1192 + {
1193 + /* Reached a halt state.
1194 + Check the halt state can satisfy the current context. */
1195 + if (!cur_state->has_constraint
1196 + || check_halt_state_context (mctx, cur_state,
1197 + re_string_cur_idx (&mctx->input)))
1198 + {
1199 + /* We found an appropriate halt state. */
1200 + match_last = re_string_cur_idx (&mctx->input);
1201 + match = 1;
1202 +
1203 + /* We found a match, do not modify match_first below. */
1204 + p_match_first = NULL;
1205 + if (!fl_longest_match)
1206 + break;
1207 + }
1208 + }
1209 + }
1210 +
1211 + if (p_match_first)
1212 + *p_match_first += next_start_idx;
1213 +
1214 + return match_last;
1215 +}
1216 +
1217 +/* Check NODE match the current context. */
1218 +
1219 +static int
1220 +internal_function
1221 +check_halt_node_context (const re_dfa_t *dfa, int node, unsigned int context)
1222 +{
1223 + re_token_type_t type = dfa->nodes[node].type;
1224 + unsigned int constraint = dfa->nodes[node].constraint;
1225 + if (type != END_OF_RE)
1226 + return 0;
1227 + if (!constraint)
1228 + return 1;
1229 + if (NOT_SATISFY_NEXT_CONSTRAINT (constraint, context))
1230 + return 0;
1231 + return 1;
1232 +}
1233 +
1234 +/* Check the halt state STATE match the current context.
1235 + Return 0 if not match, if the node, STATE has, is a halt node and
1236 + match the context, return the node. */
1237 +
1238 +static int
1239 +internal_function
1240 +check_halt_state_context (const re_match_context_t *mctx,
1241 + const re_dfastate_t *state, int idx)
1242 +{
1243 + int i;
1244 + unsigned int context;
1245 +#ifdef DEBUG
1246 + assert (state->halt);
1247 +#endif
1248 + context = re_string_context_at (&mctx->input, idx, mctx->eflags);
1249 + for (i = 0; i < state->nodes.nelem; ++i)
1250 + if (check_halt_node_context (mctx->dfa, state->nodes.elems[i], context))
1251 + return state->nodes.elems[i];
1252 + return 0;
1253 +}
1254 +
1255 +/* Compute the next node to which "NFA" transit from NODE("NFA" is a NFA
1256 + corresponding to the DFA).
1257 + Return the destination node, and update EPS_VIA_NODES, return -1 in case
1258 + of errors. */
1259 +
1260 +static int
1261 +internal_function
1262 +proceed_next_node (const re_match_context_t *mctx, int nregs, regmatch_t *regs,
1263 + int *pidx, int node, re_node_set *eps_via_nodes,
1264 + struct re_fail_stack_t *fs)
1265 +{
1266 + const re_dfa_t *const dfa = mctx->dfa;
1267 + int i, err;
1268 + if (IS_EPSILON_NODE (dfa->nodes[node].type))
1269 + {
1270 + re_node_set *cur_nodes = &mctx->state_log[*pidx]->nodes;
1271 + re_node_set *edests = &dfa->edests[node];
1272 + int dest_node;
1273 + err = re_node_set_insert (eps_via_nodes, node);
1274 + if (BE (err < 0, 0))
1275 + return -2;
1276 + /* Pick up a valid destination, or return -1 if none is found. */
1277 + for (dest_node = -1, i = 0; i < edests->nelem; ++i)
1278 + {
1279 + int candidate = edests->elems[i];
1280 + if (!re_node_set_contains (cur_nodes, candidate))
1281 + continue;
1282 + if (dest_node == -1)
1283 + dest_node = candidate;
1284 +
1285 + else
1286 + {
1287 + /* In order to avoid infinite loop like "(a*)*", return the second
1288 + epsilon-transition if the first was already considered. */
1289 + if (re_node_set_contains (eps_via_nodes, dest_node))
1290 + return candidate;
1291 +
1292 + /* Otherwise, push the second epsilon-transition on the fail stack. */
1293 + else if (fs != NULL
1294 + && push_fail_stack (fs, *pidx, candidate, nregs, regs,
1295 + eps_via_nodes))
1296 + return -2;
1297 +
1298 + /* We know we are going to exit. */
1299 + break;
1300 + }
1301 + }
1302 + return dest_node;
1303 + }
1304 + else
1305 + {
1306 + int naccepted = 0;
1307 + re_token_type_t type = dfa->nodes[node].type;
1308 +
1309 +#ifdef RE_ENABLE_I18N
1310 + if (dfa->nodes[node].accept_mb)
1311 + naccepted = check_node_accept_bytes (dfa, node, &mctx->input, *pidx);
1312 + else
1313 +#endif /* RE_ENABLE_I18N */
1314 + if (type == OP_BACK_REF)
1315 + {
1316 + int subexp_idx = dfa->nodes[node].opr.idx + 1;
1317 + naccepted = regs[subexp_idx].rm_eo - regs[subexp_idx].rm_so;
1318 + if (fs != NULL)
1319 + {
1320 + if (regs[subexp_idx].rm_so == -1 || regs[subexp_idx].rm_eo == -1)
1321 + return -1;
1322 + else if (naccepted)
1323 + {
1324 + char *buf = (char *) re_string_get_buffer (&mctx->input);
1325 + if (memcmp (buf + regs[subexp_idx].rm_so, buf + *pidx,
1326 + naccepted) != 0)
1327 + return -1;
1328 + }
1329 + }
1330 +
1331 + if (naccepted == 0)
1332 + {
1333 + int dest_node;
1334 + err = re_node_set_insert (eps_via_nodes, node);
1335 + if (BE (err < 0, 0))
1336 + return -2;
1337 + dest_node = dfa->edests[node].elems[0];
1338 + if (re_node_set_contains (&mctx->state_log[*pidx]->nodes,
1339 + dest_node))
1340 + return dest_node;
1341 + }
1342 + }
1343 +
1344 + if (naccepted != 0
1345 + || check_node_accept (mctx, dfa->nodes + node, *pidx))
1346 + {
1347 + int dest_node = dfa->nexts[node];
1348 + *pidx = (naccepted == 0) ? *pidx + 1 : *pidx + naccepted;
1349 + if (fs && (*pidx > mctx->match_last || mctx->state_log[*pidx] == NULL
1350 + || !re_node_set_contains (&mctx->state_log[*pidx]->nodes,
1351 + dest_node)))
1352 + return -1;
1353 + re_node_set_empty (eps_via_nodes);
1354 + return dest_node;
1355 + }
1356 + }
1357 + return -1;
1358 +}
1359 +
1360 +static reg_errcode_t
1361 +internal_function
1362 +push_fail_stack (struct re_fail_stack_t *fs, int str_idx, int dest_node,
1363 + int nregs, regmatch_t *regs, re_node_set *eps_via_nodes)
1364 +{
1365 + reg_errcode_t err;
1366 + int num = fs->num++;
1367 + if (fs->num == fs->alloc)
1368 + {
1369 + struct re_fail_stack_ent_t *new_array;
1370 + new_array = realloc (fs->stack, (sizeof (struct re_fail_stack_ent_t)
1371 + * fs->alloc * 2));
1372 + if (new_array == NULL)
1373 + return REG_ESPACE;
1374 + fs->alloc *= 2;
1375 + fs->stack = new_array;
1376 + }
1377 + fs->stack[num].idx = str_idx;
1378 + fs->stack[num].node = dest_node;
1379 + fs->stack[num].regs = re_malloc (regmatch_t, nregs);
1380 + if (fs->stack[num].regs == NULL)
1381 + return REG_ESPACE;
1382 + memcpy (fs->stack[num].regs, regs, sizeof (regmatch_t) * nregs);
1383 + err = re_node_set_init_copy (&fs->stack[num].eps_via_nodes, eps_via_nodes);
1384 + return err;
1385 +}
1386 +
1387 +static int
1388 +internal_function
1389 +pop_fail_stack (struct re_fail_stack_t *fs, int *pidx, int nregs,
1390 + regmatch_t *regs, re_node_set *eps_via_nodes)
1391 +{
1392 + int num = --fs->num;
1393 + assert (num >= 0);
1394 + *pidx = fs->stack[num].idx;
1395 + memcpy (regs, fs->stack[num].regs, sizeof (regmatch_t) * nregs);
1396 + re_node_set_free (eps_via_nodes);
1397 + re_free (fs->stack[num].regs);
1398 + *eps_via_nodes = fs->stack[num].eps_via_nodes;
1399 + return fs->stack[num].node;
1400 +}
1401 +
1402 +/* Set the positions where the subexpressions are starts/ends to registers
1403 + PMATCH.
1404 + Note: We assume that pmatch[0] is already set, and
1405 + pmatch[i].rm_so == pmatch[i].rm_eo == -1 for 0 < i < nmatch. */
1406 +
1407 +static reg_errcode_t
1408 +internal_function
1409 +set_regs (const regex_t *preg, const re_match_context_t *mctx, size_t nmatch,
1410 + regmatch_t *pmatch, int fl_backtrack)
1411 +{
1412 + const re_dfa_t *dfa = (const re_dfa_t *) preg->buffer;
1413 + int idx, cur_node;
1414 + re_node_set eps_via_nodes;
1415 + struct re_fail_stack_t *fs;
1416 + struct re_fail_stack_t fs_body = { 0, 2, NULL };
1417 + regmatch_t *prev_idx_match;
1418 + int prev_idx_match_malloced = 0;
1419 +
1420 +#ifdef DEBUG
1421 + assert (nmatch > 1);
1422 + assert (mctx->state_log != NULL);
1423 +#endif
1424 + if (fl_backtrack)
1425 + {
1426 + fs = &fs_body;
1427 + fs->stack = re_malloc (struct re_fail_stack_ent_t, fs->alloc);
1428 + if (fs->stack == NULL)
1429 + return REG_ESPACE;
1430 + }
1431 + else
1432 + fs = NULL;
1433 +
1434 + cur_node = dfa->init_node;
1435 + re_node_set_init_empty (&eps_via_nodes);
1436 +
1437 +#ifdef HAVE_ALLOCA
1438 + if (__libc_use_alloca (nmatch * sizeof (regmatch_t)))
1439 + prev_idx_match = (regmatch_t *) alloca (nmatch * sizeof (regmatch_t));
1440 + else
1441 +#endif
1442 + {
1443 + prev_idx_match = re_malloc (regmatch_t, nmatch);
1444 + if (prev_idx_match == NULL)
1445 + {
1446 + free_fail_stack_return (fs);
1447 + return REG_ESPACE;
1448 + }
1449 + prev_idx_match_malloced = 1;
1450 + }
1451 + memcpy (prev_idx_match, pmatch, sizeof (regmatch_t) * nmatch);
1452 +
1453 + for (idx = pmatch[0].rm_so; idx <= pmatch[0].rm_eo ;)
1454 + {
1455 + update_regs (dfa, pmatch, prev_idx_match, cur_node, idx, nmatch);
1456 +
1457 + if (idx == pmatch[0].rm_eo && cur_node == mctx->last_node)
1458 + {
1459 + int reg_idx;
1460 + if (fs)
1461 + {
1462 + for (reg_idx = 0; reg_idx < nmatch; ++reg_idx)
1463 + if (pmatch[reg_idx].rm_so > -1 && pmatch[reg_idx].rm_eo == -1)
1464 + break;
1465 + if (reg_idx == nmatch)
1466 + {
1467 + re_node_set_free (&eps_via_nodes);
1468 + if (prev_idx_match_malloced)
1469 + re_free (prev_idx_match);
1470 + return free_fail_stack_return (fs);
1471 + }
1472 + cur_node = pop_fail_stack (fs, &idx, nmatch, pmatch,
1473 + &eps_via_nodes);
1474 + }
1475 + else
1476 + {
1477 + re_node_set_free (&eps_via_nodes);
1478 + if (prev_idx_match_malloced)
1479 + re_free (prev_idx_match);
1480 + return REG_NOERROR;
1481 + }
1482 + }
1483 +
1484 + /* Proceed to next node. */
1485 + cur_node = proceed_next_node (mctx, nmatch, pmatch, &idx, cur_node,
1486 + &eps_via_nodes, fs);
1487 +
1488 + if (BE (cur_node < 0, 0))
1489 + {
1490 + if (BE (cur_node == -2, 0))
1491 + {
1492 + re_node_set_free (&eps_via_nodes);
1493 + if (prev_idx_match_malloced)
1494 + re_free (prev_idx_match);
1495 + free_fail_stack_return (fs);
1496 + return REG_ESPACE;
1497 + }
1498 + if (fs)
1499 + cur_node = pop_fail_stack (fs, &idx, nmatch, pmatch,
1500 + &eps_via_nodes);
1501 + else
1502 + {
1503 + re_node_set_free (&eps_via_nodes);
1504 + if (prev_idx_match_malloced)
1505 + re_free (prev_idx_match);
1506 + return REG_NOMATCH;
1507 + }
1508 + }
1509 + }
1510 + re_node_set_free (&eps_via_nodes);
1511 + if (prev_idx_match_malloced)
1512 + re_free (prev_idx_match);
1513 + return free_fail_stack_return (fs);
1514 +}
1515 +
1516 +static reg_errcode_t
1517 +internal_function
1518 +free_fail_stack_return (struct re_fail_stack_t *fs)
1519 +{
1520 + if (fs)
1521 + {
1522 + int fs_idx;
1523 + for (fs_idx = 0; fs_idx < fs->num; ++fs_idx)
1524 + {
1525 + re_node_set_free (&fs->stack[fs_idx].eps_via_nodes);
1526 + re_free (fs->stack[fs_idx].regs);
1527 + }
1528 + re_free (fs->stack);
1529 + }
1530 + return REG_NOERROR;
1531 +}
1532 +
1533 +static void
1534 +internal_function
1535 +update_regs (const re_dfa_t *dfa, regmatch_t *pmatch,
1536 + regmatch_t *prev_idx_match, int cur_node, int cur_idx, int nmatch)
1537 +{
1538 + int type = dfa->nodes[cur_node].type;
1539 + if (type == OP_OPEN_SUBEXP)
1540 + {
1541 + int reg_num = dfa->nodes[cur_node].opr.idx + 1;
1542 +
1543 + /* We are at the first node of this sub expression. */
1544 + if (reg_num < nmatch)
1545 + {
1546 + pmatch[reg_num].rm_so = cur_idx;
1547 + pmatch[reg_num].rm_eo = -1;
1548 + }
1549 + }
1550 + else if (type == OP_CLOSE_SUBEXP)
1551 + {
1552 + int reg_num = dfa->nodes[cur_node].opr.idx + 1;
1553 + if (reg_num < nmatch)
1554 + {
1555 + /* We are at the last node of this sub expression. */
1556 + if (pmatch[reg_num].rm_so < cur_idx)
1557 + {
1558 + pmatch[reg_num].rm_eo = cur_idx;
1559 + /* This is a non-empty match or we are not inside an optional
1560 + subexpression. Accept this right away. */
1561 + memcpy (prev_idx_match, pmatch, sizeof (regmatch_t) * nmatch);
1562 + }
1563 + else
1564 + {
1565 + if (dfa->nodes[cur_node].opt_subexp
1566 + && prev_idx_match[reg_num].rm_so != -1)
1567 + /* We transited through an empty match for an optional
1568 + subexpression, like (a?)*, and this is not the subexp's
1569 + first match. Copy back the old content of the registers
1570 + so that matches of an inner subexpression are undone as
1571 + well, like in ((a?))*. */
1572 + memcpy (pmatch, prev_idx_match, sizeof (regmatch_t) * nmatch);
1573 + else
1574 + /* We completed a subexpression, but it may be part of
1575 + an optional one, so do not update PREV_IDX_MATCH. */
1576 + pmatch[reg_num].rm_eo = cur_idx;
1577 + }
1578 + }
1579 + }
1580 +}
1581 +
1582 +/* This function checks the STATE_LOG from the SCTX->last_str_idx to 0
1583 + and sift the nodes in each states according to the following rules.
1584 + Updated state_log will be wrote to STATE_LOG.
1585 +
1586 + Rules: We throw away the Node `a' in the STATE_LOG[STR_IDX] if...
1587 + 1. When STR_IDX == MATCH_LAST(the last index in the state_log):
1588 + If `a' isn't the LAST_NODE and `a' can't epsilon transit to
1589 + the LAST_NODE, we throw away the node `a'.
1590 + 2. When 0 <= STR_IDX < MATCH_LAST and `a' accepts
1591 + string `s' and transit to `b':
1592 + i. If 'b' isn't in the STATE_LOG[STR_IDX+strlen('s')], we throw
1593 + away the node `a'.
1594 + ii. If 'b' is in the STATE_LOG[STR_IDX+strlen('s')] but 'b' is
1595 + thrown away, we throw away the node `a'.
1596 + 3. When 0 <= STR_IDX < MATCH_LAST and 'a' epsilon transit to 'b':
1597 + i. If 'b' isn't in the STATE_LOG[STR_IDX], we throw away the
1598 + node `a'.
1599 + ii. If 'b' is in the STATE_LOG[STR_IDX] but 'b' is thrown away,
1600 + we throw away the node `a'. */
1601 +
1602 +#define STATE_NODE_CONTAINS(state,node) \
1603 + ((state) != NULL && re_node_set_contains (&(state)->nodes, node))
1604 +
1605 +static reg_errcode_t
1606 +internal_function
1607 +sift_states_backward (const re_match_context_t *mctx, re_sift_context_t *sctx)
1608 +{
1609 + reg_errcode_t err;
1610 + int null_cnt = 0;
1611 + int str_idx = sctx->last_str_idx;
1612 + re_node_set cur_dest;
1613 +
1614 +#ifdef DEBUG
1615 + assert (mctx->state_log != NULL && mctx->state_log[str_idx] != NULL);
1616 +#endif
1617 +
1618 + /* Build sifted state_log[str_idx]. It has the nodes which can epsilon
1619 + transit to the last_node and the last_node itself. */
1620 + err = re_node_set_init_1 (&cur_dest, sctx->last_node);
1621 + if (BE (err != REG_NOERROR, 0))
1622 + return err;
1623 + err = update_cur_sifted_state (mctx, sctx, str_idx, &cur_dest);
1624 + if (BE (err != REG_NOERROR, 0))
1625 + goto free_return;
1626 +
1627 + /* Then check each states in the state_log. */
1628 + while (str_idx > 0)
1629 + {
1630 + /* Update counters. */
1631 + null_cnt = (sctx->sifted_states[str_idx] == NULL) ? null_cnt + 1 : 0;
1632 + if (null_cnt > mctx->max_mb_elem_len)
1633 + {
1634 + memset (sctx->sifted_states, '\0',
1635 + sizeof (re_dfastate_t *) * str_idx);
1636 + re_node_set_free (&cur_dest);
1637 + return REG_NOERROR;
1638 + }
1639 + re_node_set_empty (&cur_dest);
1640 + --str_idx;
1641 +
1642 + if (mctx->state_log[str_idx])
1643 + {
1644 + err = build_sifted_states (mctx, sctx, str_idx, &cur_dest);
1645 + if (BE (err != REG_NOERROR, 0))
1646 + goto free_return;
1647 + }
1648 +
1649 + /* Add all the nodes which satisfy the following conditions:
1650 + - It can epsilon transit to a node in CUR_DEST.
1651 + - It is in CUR_SRC.
1652 + And update state_log. */
1653 + err = update_cur_sifted_state (mctx, sctx, str_idx, &cur_dest);
1654 + if (BE (err != REG_NOERROR, 0))
1655 + goto free_return;
1656 + }
1657 + err = REG_NOERROR;
1658 + free_return:
1659 + re_node_set_free (&cur_dest);
1660 + return err;
1661 +}
1662 +
1663 +static reg_errcode_t
1664 +internal_function
1665 +build_sifted_states (const re_match_context_t *mctx, re_sift_context_t *sctx,
1666 + int str_idx, re_node_set *cur_dest)
1667 +{
1668 + const re_dfa_t *const dfa = mctx->dfa;
1669 + const re_node_set *cur_src = &mctx->state_log[str_idx]->non_eps_nodes;
1670 + int i;
1671 +
1672 + /* Then build the next sifted state.
1673 + We build the next sifted state on `cur_dest', and update
1674 + `sifted_states[str_idx]' with `cur_dest'.
1675 + Note:
1676 + `cur_dest' is the sifted state from `state_log[str_idx + 1]'.
1677 + `cur_src' points the node_set of the old `state_log[str_idx]'
1678 + (with the epsilon nodes pre-filtered out). */
1679 + for (i = 0; i < cur_src->nelem; i++)
1680 + {
1681 + int prev_node = cur_src->elems[i];
1682 + int naccepted = 0;
1683 + int ret;
1684 +
1685 +#ifdef DEBUG
1686 + re_token_type_t type = dfa->nodes[prev_node].type;
1687 + assert (!IS_EPSILON_NODE (type));
1688 +#endif
1689 +#ifdef RE_ENABLE_I18N
1690 + /* If the node may accept `multi byte'. */
1691 + if (dfa->nodes[prev_node].accept_mb)
1692 + naccepted = sift_states_iter_mb (mctx, sctx, prev_node,
1693 + str_idx, sctx->last_str_idx);
1694 +#endif /* RE_ENABLE_I18N */
1695 +
1696 + /* We don't check backreferences here.
1697 + See update_cur_sifted_state(). */
1698 + if (!naccepted
1699 + && check_node_accept (mctx, dfa->nodes + prev_node, str_idx)
1700 + && STATE_NODE_CONTAINS (sctx->sifted_states[str_idx + 1],
1701 + dfa->nexts[prev_node]))
1702 + naccepted = 1;
1703 +
1704 + if (naccepted == 0)
1705 + continue;
1706 +
1707 + if (sctx->limits.nelem)
1708 + {
1709 + int to_idx = str_idx + naccepted;
1710 + if (check_dst_limits (mctx, &sctx->limits,
1711 + dfa->nexts[prev_node], to_idx,
1712 + prev_node, str_idx))
1713 + continue;
1714 + }
1715 + ret = re_node_set_insert (cur_dest, prev_node);
1716 + if (BE (ret == -1, 0))
1717 + return REG_ESPACE;
1718 + }
1719 +
1720 + return REG_NOERROR;
1721 +}
1722 +
1723 +/* Helper functions. */
1724 +
1725 +static reg_errcode_t
1726 +internal_function
1727 +clean_state_log_if_needed (re_match_context_t *mctx, int next_state_log_idx)
1728 +{
1729 + int top = mctx->state_log_top;
1730 +
1731 + if (next_state_log_idx >= mctx->input.bufs_len
1732 + || (next_state_log_idx >= mctx->input.valid_len
1733 + && mctx->input.valid_len < mctx->input.len))
1734 + {
1735 + reg_errcode_t err;
1736 + err = extend_buffers (mctx);
1737 + if (BE (err != REG_NOERROR, 0))
1738 + return err;
1739 + }
1740 +
1741 + if (top < next_state_log_idx)
1742 + {
1743 + memset (mctx->state_log + top + 1, '\0',
1744 + sizeof (re_dfastate_t *) * (next_state_log_idx - top));
1745 + mctx->state_log_top = next_state_log_idx;
1746 + }
1747 + return REG_NOERROR;
1748 +}
1749 +
1750 +static reg_errcode_t
1751 +internal_function
1752 +merge_state_array (const re_dfa_t *dfa, re_dfastate_t **dst,
1753 + re_dfastate_t **src, int num)
1754 +{
1755 + int st_idx;
1756 + reg_errcode_t err;
1757 + for (st_idx = 0; st_idx < num; ++st_idx)
1758 + {
1759 + if (dst[st_idx] == NULL)
1760 + dst[st_idx] = src[st_idx];
1761 + else if (src[st_idx] != NULL)
1762 + {
1763 + re_node_set merged_set;
1764 + err = re_node_set_init_union (&merged_set, &dst[st_idx]->nodes,
1765 + &src[st_idx]->nodes);
1766 + if (BE (err != REG_NOERROR, 0))
1767 + return err;
1768 + dst[st_idx] = re_acquire_state (&err, dfa, &merged_set);
1769 + re_node_set_free (&merged_set);
1770 + if (BE (err != REG_NOERROR, 0))
1771 + return err;
1772 + }
1773 + }
1774 + return REG_NOERROR;
1775 +}
1776 +
1777 +static reg_errcode_t
1778 +internal_function
1779 +update_cur_sifted_state (const re_match_context_t *mctx,
1780 + re_sift_context_t *sctx, int str_idx,
1781 + re_node_set *dest_nodes)
1782 +{
1783 + const re_dfa_t *const dfa = mctx->dfa;
1784 + reg_errcode_t err = REG_NOERROR;
1785 + const re_node_set *candidates;
1786 + candidates = ((mctx->state_log[str_idx] == NULL) ? NULL
1787 + : &mctx->state_log[str_idx]->nodes);
1788 +
1789 + if (dest_nodes->nelem == 0)
1790 + sctx->sifted_states[str_idx] = NULL;
1791 + else
1792 + {
1793 + if (candidates)
1794 + {
1795 + /* At first, add the nodes which can epsilon transit to a node in
1796 + DEST_NODE. */
1797 + err = add_epsilon_src_nodes (dfa, dest_nodes, candidates);
1798 + if (BE (err != REG_NOERROR, 0))
1799 + return err;
1800 +
1801 + /* Then, check the limitations in the current sift_context. */
1802 + if (sctx->limits.nelem)
1803 + {
1804 + err = check_subexp_limits (dfa, dest_nodes, candidates, &sctx->limits,
1805 + mctx->bkref_ents, str_idx);
1806 + if (BE (err != REG_NOERROR, 0))
1807 + return err;
1808 + }
1809 + }
1810 +
1811 + sctx->sifted_states[str_idx] = re_acquire_state (&err, dfa, dest_nodes);
1812 + if (BE (err != REG_NOERROR, 0))
1813 + return err;
1814 + }
1815 +
1816 + if (candidates && mctx->state_log[str_idx]->has_backref)
1817 + {
1818 + err = sift_states_bkref (mctx, sctx, str_idx, candidates);
1819 + if (BE (err != REG_NOERROR, 0))
1820 + return err;
1821 + }
1822 + return REG_NOERROR;
1823 +}
1824 +
1825 +static reg_errcode_t
1826 +internal_function
1827 +add_epsilon_src_nodes (const re_dfa_t *dfa, re_node_set *dest_nodes,
1828 + const re_node_set *candidates)
1829 +{
1830 + reg_errcode_t err = REG_NOERROR;
1831 + int i;
1832 +
1833 + re_dfastate_t *state = re_acquire_state (&err, dfa, dest_nodes);
1834 + if (BE (err != REG_NOERROR, 0))
1835 + return err;
1836 +
1837 + if (!state->inveclosure.alloc)
1838 + {
1839 + err = re_node_set_alloc (&state->inveclosure, dest_nodes->nelem);
1840 + if (BE (err != REG_NOERROR, 0))
1841 + return REG_ESPACE;
1842 + for (i = 0; i < dest_nodes->nelem; i++)
1843 + {
1844 + err = re_node_set_merge (&state->inveclosure,
1845 + dfa->inveclosures + dest_nodes->elems[i]);
1846 + if (BE (err != REG_NOERROR, 0))
1847 + return REG_ESPACE;
1848 + }
1849 + }
1850 + return re_node_set_add_intersect (dest_nodes, candidates,
1851 + &state->inveclosure);
1852 +}
1853 +
1854 +static reg_errcode_t
1855 +internal_function
1856 +sub_epsilon_src_nodes (const re_dfa_t *dfa, int node, re_node_set *dest_nodes,
1857 + const re_node_set *candidates)
1858 +{
1859 + int ecl_idx;
1860 + reg_errcode_t err;
1861 + re_node_set *inv_eclosure = dfa->inveclosures + node;
1862 + re_node_set except_nodes;
1863 + re_node_set_init_empty (&except_nodes);
1864 + for (ecl_idx = 0; ecl_idx < inv_eclosure->nelem; ++ecl_idx)
1865 + {
1866 + int cur_node = inv_eclosure->elems[ecl_idx];
1867 + if (cur_node == node)
1868 + continue;
1869 + if (IS_EPSILON_NODE (dfa->nodes[cur_node].type))
1870 + {
1871 + int edst1 = dfa->edests[cur_node].elems[0];
1872 + int edst2 = ((dfa->edests[cur_node].nelem > 1)
1873 + ? dfa->edests[cur_node].elems[1] : -1);
1874 + if ((!re_node_set_contains (inv_eclosure, edst1)
1875 + && re_node_set_contains (dest_nodes, edst1))
1876 + || (edst2 > 0
1877 + && !re_node_set_contains (inv_eclosure, edst2)
1878 + && re_node_set_contains (dest_nodes, edst2)))
1879 + {
1880 + err = re_node_set_add_intersect (&except_nodes, candidates,
1881 + dfa->inveclosures + cur_node);
1882 + if (BE (err != REG_NOERROR, 0))
1883 + {
1884 + re_node_set_free (&except_nodes);
1885 + return err;
1886 + }
1887 + }
1888 + }
1889 + }
1890 + for (ecl_idx = 0; ecl_idx < inv_eclosure->nelem; ++ecl_idx)
1891 + {
1892 + int cur_node = inv_eclosure->elems[ecl_idx];
1893 + if (!re_node_set_contains (&except_nodes, cur_node))
1894 + {
1895 + int idx = re_node_set_contains (dest_nodes, cur_node) - 1;
1896 + re_node_set_remove_at (dest_nodes, idx);
1897 + }
1898 + }
1899 + re_node_set_free (&except_nodes);
1900 + return REG_NOERROR;
1901 +}
1902 +
1903 +static int
1904 +internal_function
1905 +check_dst_limits (const re_match_context_t *mctx, re_node_set *limits,
1906 + int dst_node, int dst_idx, int src_node, int src_idx)
1907 +{
1908 + const re_dfa_t *const dfa = mctx->dfa;
1909 + int lim_idx, src_pos, dst_pos;
1910 +
1911 + int dst_bkref_idx = search_cur_bkref_entry (mctx, dst_idx);
1912 + int src_bkref_idx = search_cur_bkref_entry (mctx, src_idx);
1913 + for (lim_idx = 0; lim_idx < limits->nelem; ++lim_idx)
1914 + {
1915 + int subexp_idx;
1916 + struct re_backref_cache_entry *ent;
1917 + ent = mctx->bkref_ents + limits->elems[lim_idx];
1918 + subexp_idx = dfa->nodes[ent->node].opr.idx;
1919 +
1920 + dst_pos = check_dst_limits_calc_pos (mctx, limits->elems[lim_idx],
1921 + subexp_idx, dst_node, dst_idx,
1922 + dst_bkref_idx);
1923 + src_pos = check_dst_limits_calc_pos (mctx, limits->elems[lim_idx],
1924 + subexp_idx, src_node, src_idx,
1925 + src_bkref_idx);
1926 +
1927 + /* In case of:
1928 + <src> <dst> ( <subexp> )
1929 + ( <subexp> ) <src> <dst>
1930 + ( <subexp1> <src> <subexp2> <dst> <subexp3> ) */
1931 + if (src_pos == dst_pos)
1932 + continue; /* This is unrelated limitation. */
1933 + else
1934 + return 1;
1935 + }
1936 + return 0;
1937 +}
1938 +
1939 +static int
1940 +internal_function
1941 +check_dst_limits_calc_pos_1 (const re_match_context_t *mctx, int boundaries,
1942 + int subexp_idx, int from_node, int bkref_idx)
1943 +{
1944 + const re_dfa_t *const dfa = mctx->dfa;
1945 + const re_node_set *eclosures = dfa->eclosures + from_node;
1946 + int node_idx;
1947 +
1948 + /* Else, we are on the boundary: examine the nodes on the epsilon
1949 + closure. */
1950 + for (node_idx = 0; node_idx < eclosures->nelem; ++node_idx)
1951 + {
1952 + int node = eclosures->elems[node_idx];
1953 + switch (dfa->nodes[node].type)
1954 + {
1955 + case OP_BACK_REF:
1956 + if (bkref_idx != -1)
1957 + {
1958 + struct re_backref_cache_entry *ent = mctx->bkref_ents + bkref_idx;
1959 + do
1960 + {
1961 + int dst, cpos;
1962 +
1963 + if (ent->node != node)
1964 + continue;
1965 +
1966 + if (subexp_idx < BITSET_WORD_BITS
1967 + && !(ent->eps_reachable_subexps_map
1968 + & ((bitset_word_t) 1 << subexp_idx)))
1969 + continue;
1970 +
1971 + /* Recurse trying to reach the OP_OPEN_SUBEXP and
1972 + OP_CLOSE_SUBEXP cases below. But, if the
1973 + destination node is the same node as the source
1974 + node, don't recurse because it would cause an
1975 + infinite loop: a regex that exhibits this behavior
1976 + is ()\1*\1* */
1977 + dst = dfa->edests[node].elems[0];
1978 + if (dst == from_node)
1979 + {
1980 + if (boundaries & 1)
1981 + return -1;
1982 + else /* if (boundaries & 2) */
1983 + return 0;
1984 + }
1985 +
1986 + cpos =
1987 + check_dst_limits_calc_pos_1 (mctx, boundaries, subexp_idx,
1988 + dst, bkref_idx);
1989 + if (cpos == -1 /* && (boundaries & 1) */)
1990 + return -1;
1991 + if (cpos == 0 && (boundaries & 2))
1992 + return 0;
1993 +
1994 + if (subexp_idx < BITSET_WORD_BITS)
1995 + ent->eps_reachable_subexps_map
1996 + &= ~((bitset_word_t) 1 << subexp_idx);
1997 + }
1998 + while (ent++->more);
1999 + }
2000 + break;
2001 +
2002 + case OP_OPEN_SUBEXP:
2003 + if ((boundaries & 1) && subexp_idx == dfa->nodes[node].opr.idx)
2004 + return -1;
2005 + break;
2006 +
2007 + case OP_CLOSE_SUBEXP:
2008 + if ((boundaries & 2) && subexp_idx == dfa->nodes[node].opr.idx)
2009 + return 0;
2010 + break;
2011 +
2012 + default:
2013 + break;
2014 + }
2015 + }
2016 +
2017 + return (boundaries & 2) ? 1 : 0;
2018 +}
2019 +
2020 +static int
2021 +internal_function
2022 +check_dst_limits_calc_pos (const re_match_context_t *mctx, int limit,
2023 + int subexp_idx, int from_node, int str_idx,
2024 + int bkref_idx)
2025 +{
2026 + struct re_backref_cache_entry *lim = mctx->bkref_ents + limit;
2027 + int boundaries;
2028 +
2029 + /* If we are outside the range of the subexpression, return -1 or 1. */
2030 + if (str_idx < lim->subexp_from)
2031 + return -1;
2032 +
2033 + if (lim->subexp_to < str_idx)
2034 + return 1;
2035 +
2036 + /* If we are within the subexpression, return 0. */
2037 + boundaries = (str_idx == lim->subexp_from);
2038 + boundaries |= (str_idx == lim->subexp_to) << 1;
2039 + if (boundaries == 0)
2040 + return 0;
2041 +
2042 + /* Else, examine epsilon closure. */
2043 + return check_dst_limits_calc_pos_1 (mctx, boundaries, subexp_idx,
2044 + from_node, bkref_idx);
2045 +}
2046 +
2047 +/* Check the limitations of sub expressions LIMITS, and remove the nodes
2048 + which are against limitations from DEST_NODES. */
2049 +
2050 +static reg_errcode_t
2051 +internal_function
2052 +check_subexp_limits (const re_dfa_t *dfa, re_node_set *dest_nodes,
2053 + const re_node_set *candidates, re_node_set *limits,
2054 + struct re_backref_cache_entry *bkref_ents, int str_idx)
2055 +{
2056 + reg_errcode_t err;
2057 + int node_idx, lim_idx;
2058 +
2059 + for (lim_idx = 0; lim_idx < limits->nelem; ++lim_idx)
2060 + {
2061 + int subexp_idx;
2062 + struct re_backref_cache_entry *ent;
2063 + ent = bkref_ents + limits->elems[lim_idx];
2064 +
2065 + if (str_idx <= ent->subexp_from || ent->str_idx < str_idx)
2066 + continue; /* This is unrelated limitation. */
2067 +
2068 + subexp_idx = dfa->nodes[ent->node].opr.idx;
2069 + if (ent->subexp_to == str_idx)
2070 + {
2071 + int ops_node = -1;
2072 + int cls_node = -1;
2073 + for (node_idx = 0; node_idx < dest_nodes->nelem; ++node_idx)
2074 + {
2075 + int node = dest_nodes->elems[node_idx];
2076 + re_token_type_t type = dfa->nodes[node].type;
2077 + if (type == OP_OPEN_SUBEXP
2078 + && subexp_idx == dfa->nodes[node].opr.idx)
2079 + ops_node = node;
2080 + else if (type == OP_CLOSE_SUBEXP
2081 + && subexp_idx == dfa->nodes[node].opr.idx)
2082 + cls_node = node;
2083 + }
2084 +
2085 + /* Check the limitation of the open subexpression. */
2086 + /* Note that (ent->subexp_to = str_idx != ent->subexp_from). */
2087 + if (ops_node >= 0)
2088 + {
2089 + err = sub_epsilon_src_nodes (dfa, ops_node, dest_nodes,
2090 + candidates);
2091 + if (BE (err != REG_NOERROR, 0))
2092 + return err;
2093 + }
2094 +
2095 + /* Check the limitation of the close subexpression. */
2096 + if (cls_node >= 0)
2097 + for (node_idx = 0; node_idx < dest_nodes->nelem; ++node_idx)
2098 + {
2099 + int node = dest_nodes->elems[node_idx];
2100 + if (!re_node_set_contains (dfa->inveclosures + node,
2101 + cls_node)
2102 + && !re_node_set_contains (dfa->eclosures + node,
2103 + cls_node))
2104 + {
2105 + /* It is against this limitation.
2106 + Remove it form the current sifted state. */
2107 + err = sub_epsilon_src_nodes (dfa, node, dest_nodes,
2108 + candidates);
2109 + if (BE (err != REG_NOERROR, 0))
2110 + return err;
2111 + --node_idx;
2112 + }
2113 + }
2114 + }
2115 + else /* (ent->subexp_to != str_idx) */
2116 + {
2117 + for (node_idx = 0; node_idx < dest_nodes->nelem; ++node_idx)
2118 + {
2119 + int node = dest_nodes->elems[node_idx];
2120 + re_token_type_t type = dfa->nodes[node].type;
2121 + if (type == OP_CLOSE_SUBEXP || type == OP_OPEN_SUBEXP)
2122 + {
2123 + if (subexp_idx != dfa->nodes[node].opr.idx)
2124 + continue;
2125 + /* It is against this limitation.
2126 + Remove it form the current sifted state. */
2127 + err = sub_epsilon_src_nodes (dfa, node, dest_nodes,
2128 + candidates);
2129 + if (BE (err != REG_NOERROR, 0))
2130 + return err;
2131 + }
2132 + }
2133 + }
2134 + }
2135 + return REG_NOERROR;
2136 +}
2137 +
2138 +static reg_errcode_t
2139 +internal_function
2140 +sift_states_bkref (const re_match_context_t *mctx, re_sift_context_t *sctx,
2141 + int str_idx, const re_node_set *candidates)
2142 +{
2143 + const re_dfa_t *const dfa = mctx->dfa;
2144 + reg_errcode_t err;
2145 + int node_idx, node;
2146 + re_sift_context_t local_sctx;
2147 + int first_idx = search_cur_bkref_entry (mctx, str_idx);
2148 +
2149 + if (first_idx == -1)
2150 + return REG_NOERROR;
2151 +
2152 + local_sctx.sifted_states = NULL; /* Mark that it hasn't been initialized. */
2153 +
2154 + for (node_idx = 0; node_idx < candidates->nelem; ++node_idx)
2155 + {
2156 + int enabled_idx;
2157 + re_token_type_t type;
2158 + struct re_backref_cache_entry *entry;
2159 + node = candidates->elems[node_idx];
2160 + type = dfa->nodes[node].type;
2161 + /* Avoid infinite loop for the REs like "()\1+". */
2162 + if (node == sctx->last_node && str_idx == sctx->last_str_idx)
2163 + continue;
2164 + if (type != OP_BACK_REF)
2165 + continue;
2166 +
2167 + entry = mctx->bkref_ents + first_idx;
2168 + enabled_idx = first_idx;
2169 + do
2170 + {
2171 + int subexp_len;
2172 + int to_idx;
2173 + int dst_node;
2174 + int ret;
2175 + re_dfastate_t *cur_state;
2176 +
2177 + if (entry->node != node)
2178 + continue;
2179 + subexp_len = entry->subexp_to - entry->subexp_from;
2180 + to_idx = str_idx + subexp_len;
2181 + dst_node = (subexp_len ? dfa->nexts[node]
2182 + : dfa->edests[node].elems[0]);
2183 +
2184 + if (to_idx > sctx->last_str_idx
2185 + || sctx->sifted_states[to_idx] == NULL
2186 + || !STATE_NODE_CONTAINS (sctx->sifted_states[to_idx], dst_node)
2187 + || check_dst_limits (mctx, &sctx->limits, node,
2188 + str_idx, dst_node, to_idx))
2189 + continue;
2190 +
2191 + if (local_sctx.sifted_states == NULL)
2192 + {
2193 + local_sctx = *sctx;
2194 + err = re_node_set_init_copy (&local_sctx.limits, &sctx->limits);
2195 + if (BE (err != REG_NOERROR, 0))
2196 + goto free_return;
2197 + }
2198 + local_sctx.last_node = node;
2199 + local_sctx.last_str_idx = str_idx;
2200 + ret = re_node_set_insert (&local_sctx.limits, enabled_idx);
2201 + if (BE (ret < 0, 0))
2202 + {
2203 + err = REG_ESPACE;
2204 + goto free_return;
2205 + }
2206 + cur_state = local_sctx.sifted_states[str_idx];
2207 + err = sift_states_backward (mctx, &local_sctx);
2208 + if (BE (err != REG_NOERROR, 0))
2209 + goto free_return;
2210 + if (sctx->limited_states != NULL)
2211 + {
2212 + err = merge_state_array (dfa, sctx->limited_states,
2213 + local_sctx.sifted_states,
2214 + str_idx + 1);
2215 + if (BE (err != REG_NOERROR, 0))
2216 + goto free_return;
2217 + }
2218 + local_sctx.sifted_states[str_idx] = cur_state;
2219 + re_node_set_remove (&local_sctx.limits, enabled_idx);
2220 +
2221 + /* mctx->bkref_ents may have changed, reload the pointer. */
2222 + entry = mctx->bkref_ents + enabled_idx;
2223 + }
2224 + while (enabled_idx++, entry++->more);
2225 + }
2226 + err = REG_NOERROR;
2227 + free_return:
2228 + if (local_sctx.sifted_states != NULL)
2229 + {
2230 + re_node_set_free (&local_sctx.limits);
2231 + }
2232 +
2233 + return err;
2234 +}
2235 +
2236 +
2237 +#ifdef RE_ENABLE_I18N
2238 +static int
2239 +internal_function
2240 +sift_states_iter_mb (const re_match_context_t *mctx, re_sift_context_t *sctx,
2241 + int node_idx, int str_idx, int max_str_idx)
2242 +{
2243 + const re_dfa_t *const dfa = mctx->dfa;
2244 + int naccepted;
2245 + /* Check the node can accept `multi byte'. */
2246 + naccepted = check_node_accept_bytes (dfa, node_idx, &mctx->input, str_idx);
2247 + if (naccepted > 0 && str_idx + naccepted <= max_str_idx &&
2248 + !STATE_NODE_CONTAINS (sctx->sifted_states[str_idx + naccepted],
2249 + dfa->nexts[node_idx]))
2250 + /* The node can't accept the `multi byte', or the
2251 + destination was already thrown away, then the node
2252 + could't accept the current input `multi byte'. */
2253 + naccepted = 0;
2254 + /* Otherwise, it is sure that the node could accept
2255 + `naccepted' bytes input. */
2256 + return naccepted;
2257 +}
2258 +#endif /* RE_ENABLE_I18N */
2259 +
2260 +
2261 +/* Functions for state transition. */
2262 +
2263 +/* Return the next state to which the current state STATE will transit by
2264 + accepting the current input byte, and update STATE_LOG if necessary.
2265 + If STATE can accept a multibyte char/collating element/back reference
2266 + update the destination of STATE_LOG. */
2267 +
2268 +static re_dfastate_t *
2269 +internal_function
2270 +transit_state (reg_errcode_t *err, re_match_context_t *mctx,
2271 + re_dfastate_t *state)
2272 +{
2273 + re_dfastate_t **trtable;
2274 + unsigned char ch;
2275 +
2276 +#ifdef RE_ENABLE_I18N
2277 + /* If the current state can accept multibyte. */
2278 + if (BE (state->accept_mb, 0))
2279 + {
2280 + *err = transit_state_mb (mctx, state);
2281 + if (BE (*err != REG_NOERROR, 0))
2282 + return NULL;
2283 + }
2284 +#endif /* RE_ENABLE_I18N */
2285 +
2286 + /* Then decide the next state with the single byte. */
2287 +#if 0
2288 + if (0)
2289 + /* don't use transition table */
2290 + return transit_state_sb (err, mctx, state);
2291 +#endif
2292 +
2293 + /* Use transition table */
2294 + ch = re_string_fetch_byte (&mctx->input);
2295 + for (;;)
2296 + {
2297 + trtable = state->trtable;
2298 + if (BE (trtable != NULL, 1))
2299 + return trtable[ch];
2300 +
2301 + trtable = state->word_trtable;
2302 + if (BE (trtable != NULL, 1))
2303 + {
2304 + unsigned int context;
2305 + context
2306 + = re_string_context_at (&mctx->input,
2307 + re_string_cur_idx (&mctx->input) - 1,
2308 + mctx->eflags);
2309 + if (IS_WORD_CONTEXT (context))
2310 + return trtable[ch + SBC_MAX];
2311 + else
2312 + return trtable[ch];
2313 + }
2314 +
2315 + if (!build_trtable (mctx->dfa, state))
2316 + {
2317 + *err = REG_ESPACE;
2318 + return NULL;
2319 + }
2320 +
2321 + /* Retry, we now have a transition table. */
2322 + }
2323 +}
2324 +
2325 +/* Update the state_log if we need */
2326 +re_dfastate_t *
2327 +internal_function
2328 +merge_state_with_log (reg_errcode_t *err, re_match_context_t *mctx,
2329 + re_dfastate_t *next_state)
2330 +{
2331 + const re_dfa_t *const dfa = mctx->dfa;
2332 + int cur_idx = re_string_cur_idx (&mctx->input);
2333 +
2334 + if (cur_idx > mctx->state_log_top)
2335 + {
2336 + mctx->state_log[cur_idx] = next_state;
2337 + mctx->state_log_top = cur_idx;
2338 + }
2339 + else if (mctx->state_log[cur_idx] == 0)
2340 + {
2341 + mctx->state_log[cur_idx] = next_state;
2342 + }
2343 + else
2344 + {
2345 + re_dfastate_t *pstate;
2346 + unsigned int context;
2347 + re_node_set next_nodes, *log_nodes, *table_nodes = NULL;
2348 + /* If (state_log[cur_idx] != 0), it implies that cur_idx is
2349 + the destination of a multibyte char/collating element/
2350 + back reference. Then the next state is the union set of
2351 + these destinations and the results of the transition table. */
2352 + pstate = mctx->state_log[cur_idx];
2353 + log_nodes = pstate->entrance_nodes;
2354 + if (next_state != NULL)
2355 + {
2356 + table_nodes = next_state->entrance_nodes;
2357 + *err = re_node_set_init_union (&next_nodes, table_nodes,
2358 + log_nodes);
2359 + if (BE (*err != REG_NOERROR, 0))
2360 + return NULL;
2361 + }
2362 + else
2363 + next_nodes = *log_nodes;
2364 + /* Note: We already add the nodes of the initial state,
2365 + then we don't need to add them here. */
2366 +
2367 + context = re_string_context_at (&mctx->input,
2368 + re_string_cur_idx (&mctx->input) - 1,
2369 + mctx->eflags);
2370 + next_state = mctx->state_log[cur_idx]
2371 + = re_acquire_state_context (err, dfa, &next_nodes, context);
2372 + /* We don't need to check errors here, since the return value of
2373 + this function is next_state and ERR is already set. */
2374 +
2375 + if (table_nodes != NULL)
2376 + re_node_set_free (&next_nodes);
2377 + }
2378 +
2379 + if (BE (dfa->nbackref, 0) && next_state != NULL)
2380 + {
2381 + /* Check OP_OPEN_SUBEXP in the current state in case that we use them
2382 + later. We must check them here, since the back references in the
2383 + next state might use them. */
2384 + *err = check_subexp_matching_top (mctx, &next_state->nodes,
2385 + cur_idx);
2386 + if (BE (*err != REG_NOERROR, 0))
2387 + return NULL;
2388 +
2389 + /* If the next state has back references. */
2390 + if (next_state->has_backref)
2391 + {
2392 + *err = transit_state_bkref (mctx, &next_state->nodes);
2393 + if (BE (*err != REG_NOERROR, 0))
2394 + return NULL;
2395 + next_state = mctx->state_log[cur_idx];
2396 + }
2397 + }
2398 +
2399 + return next_state;
2400 +}
2401 +
2402 +/* Skip bytes in the input that correspond to part of a
2403 + multi-byte match, then look in the log for a state
2404 + from which to restart matching. */
2405 +re_dfastate_t *
2406 +internal_function
2407 +find_recover_state (reg_errcode_t *err, re_match_context_t *mctx)
2408 +{
2409 + re_dfastate_t *cur_state;
2410 + do
2411 + {
2412 + int max = mctx->state_log_top;
2413 + int cur_str_idx = re_string_cur_idx (&mctx->input);
2414 +
2415 + do
2416 + {
2417 + if (++cur_str_idx > max)
2418 + return NULL;
2419 + re_string_skip_bytes (&mctx->input, 1);
2420 + }
2421 + while (mctx->state_log[cur_str_idx] == NULL);
2422 +
2423 + cur_state = merge_state_with_log (err, mctx, NULL);
2424 + }
2425 + while (*err == REG_NOERROR && cur_state == NULL);
2426 + return cur_state;
2427 +}
2428 +
2429 +/* Helper functions for transit_state. */
2430 +
2431 +/* From the node set CUR_NODES, pick up the nodes whose types are
2432 + OP_OPEN_SUBEXP and which have corresponding back references in the regular
2433 + expression. And register them to use them later for evaluating the
2434 + correspoding back references. */
2435 +
2436 +static reg_errcode_t
2437 +internal_function
2438 +check_subexp_matching_top (re_match_context_t *mctx, re_node_set *cur_nodes,
2439 + int str_idx)
2440 +{
2441 + const re_dfa_t *const dfa = mctx->dfa;
2442 + int node_idx;
2443 + reg_errcode_t err;
2444 +
2445 + /* TODO: This isn't efficient.
2446 + Because there might be more than one nodes whose types are
2447 + OP_OPEN_SUBEXP and whose index is SUBEXP_IDX, we must check all
2448 + nodes.
2449 + E.g. RE: (a){2} */
2450 + for (node_idx = 0; node_idx < cur_nodes->nelem; ++node_idx)
2451 + {
2452 + int node = cur_nodes->elems[node_idx];
2453 + if (dfa->nodes[node].type == OP_OPEN_SUBEXP
2454 + && dfa->nodes[node].opr.idx < BITSET_WORD_BITS
2455 + && (dfa->used_bkref_map
2456 + & ((bitset_word_t) 1 << dfa->nodes[node].opr.idx)))
2457 + {
2458 + err = match_ctx_add_subtop (mctx, node, str_idx);
2459 + if (BE (err != REG_NOERROR, 0))
2460 + return err;
2461 + }
2462 + }
2463 + return REG_NOERROR;
2464 +}
2465 +
2466 +#if 0
2467 +/* Return the next state to which the current state STATE will transit by
2468 + accepting the current input byte. */
2469 +
2470 +static re_dfastate_t *
2471 +transit_state_sb (reg_errcode_t *err, re_match_context_t *mctx,
2472 + re_dfastate_t *state)
2473 +{
2474 + const re_dfa_t *const dfa = mctx->dfa;
2475 + re_node_set next_nodes;
2476 + re_dfastate_t *next_state;
2477 + int node_cnt, cur_str_idx = re_string_cur_idx (&mctx->input);
2478 + unsigned int context;
2479 +
2480 + *err = re_node_set_alloc (&next_nodes, state->nodes.nelem + 1);
2481 + if (BE (*err != REG_NOERROR, 0))
2482 + return NULL;
2483 + for (node_cnt = 0; node_cnt < state->nodes.nelem; ++node_cnt)
2484 + {
2485 + int cur_node = state->nodes.elems[node_cnt];
2486 + if (check_node_accept (mctx, dfa->nodes + cur_node, cur_str_idx))
2487 + {
2488 + *err = re_node_set_merge (&next_nodes,
2489 + dfa->eclosures + dfa->nexts[cur_node]);
2490 + if (BE (*err != REG_NOERROR, 0))
2491 + {
2492 + re_node_set_free (&next_nodes);
2493 + return NULL;
2494 + }
2495 + }
2496 + }
2497 + context = re_string_context_at (&mctx->input, cur_str_idx, mctx->eflags);
2498 + next_state = re_acquire_state_context (err, dfa, &next_nodes, context);
2499 + /* We don't need to check errors here, since the return value of
2500 + this function is next_state and ERR is already set. */
2501 +
2502 + re_node_set_free (&next_nodes);
2503 + re_string_skip_bytes (&mctx->input, 1);
2504 + return next_state;
2505 +}
2506 +#endif
2507 +
2508 +#ifdef RE_ENABLE_I18N
2509 +static reg_errcode_t
2510 +internal_function
2511 +transit_state_mb (re_match_context_t *mctx, re_dfastate_t *pstate)
2512 +{
2513 + const re_dfa_t *const dfa = mctx->dfa;
2514 + reg_errcode_t err;
2515 + int i;
2516 +
2517 + for (i = 0; i < pstate->nodes.nelem; ++i)
2518 + {
2519 + re_node_set dest_nodes, *new_nodes;
2520 + int cur_node_idx = pstate->nodes.elems[i];
2521 + int naccepted, dest_idx;
2522 + unsigned int context;
2523 + re_dfastate_t *dest_state;
2524 +
2525 + if (!dfa->nodes[cur_node_idx].accept_mb)
2526 + continue;
2527 +
2528 + if (dfa->nodes[cur_node_idx].constraint)
2529 + {
2530 + context = re_string_context_at (&mctx->input,
2531 + re_string_cur_idx (&mctx->input),
2532 + mctx->eflags);
2533 + if (NOT_SATISFY_NEXT_CONSTRAINT (dfa->nodes[cur_node_idx].constraint,
2534 + context))
2535 + continue;
2536 + }
2537 +
2538 + /* How many bytes the node can accept? */
2539 + naccepted = check_node_accept_bytes (dfa, cur_node_idx, &mctx->input,
2540 + re_string_cur_idx (&mctx->input));
2541 + if (naccepted == 0)
2542 + continue;
2543 +
2544 + /* The node can accepts `naccepted' bytes. */
2545 + dest_idx = re_string_cur_idx (&mctx->input) + naccepted;
2546 + mctx->max_mb_elem_len = ((mctx->max_mb_elem_len < naccepted) ? naccepted
2547 + : mctx->max_mb_elem_len);
2548 + err = clean_state_log_if_needed (mctx, dest_idx);
2549 + if (BE (err != REG_NOERROR, 0))
2550 + return err;
2551 +#ifdef DEBUG
2552 + assert (dfa->nexts[cur_node_idx] != -1);
2553 +#endif
2554 + new_nodes = dfa->eclosures + dfa->nexts[cur_node_idx];
2555 +
2556 + dest_state = mctx->state_log[dest_idx];
2557 + if (dest_state == NULL)
2558 + dest_nodes = *new_nodes;
2559 + else
2560 + {
2561 + err = re_node_set_init_union (&dest_nodes,
2562 + dest_state->entrance_nodes, new_nodes);
2563 + if (BE (err != REG_NOERROR, 0))
2564 + return err;
2565 + }
2566 + context = re_string_context_at (&mctx->input, dest_idx - 1,
2567 + mctx->eflags);
2568 + mctx->state_log[dest_idx]
2569 + = re_acquire_state_context (&err, dfa, &dest_nodes, context);
2570 + if (dest_state != NULL)
2571 + re_node_set_free (&dest_nodes);
2572 + if (BE (mctx->state_log[dest_idx] == NULL && err != REG_NOERROR, 0))
2573 + return err;
2574 + }
2575 + return REG_NOERROR;
2576 +}
2577 +#endif /* RE_ENABLE_I18N */
2578 +
2579 +static reg_errcode_t
2580 +internal_function
2581 +transit_state_bkref (re_match_context_t *mctx, const re_node_set *nodes)
2582 +{
2583 + const re_dfa_t *const dfa = mctx->dfa;
2584 + reg_errcode_t err;
2585 + int i;
2586 + int cur_str_idx = re_string_cur_idx (&mctx->input);
2587 +
2588 + for (i = 0; i < nodes->nelem; ++i)
2589 + {
2590 + int dest_str_idx, prev_nelem, bkc_idx;
2591 + int node_idx = nodes->elems[i];
2592 + unsigned int context;
2593 + const re_token_t *node = dfa->nodes + node_idx;
2594 + re_node_set *new_dest_nodes;
2595 +
2596 + /* Check whether `node' is a backreference or not. */
2597 + if (node->type != OP_BACK_REF)
2598 + continue;
2599 +
2600 + if (node->constraint)
2601 + {
2602 + context = re_string_context_at (&mctx->input, cur_str_idx,
2603 + mctx->eflags);
2604 + if (NOT_SATISFY_NEXT_CONSTRAINT (node->constraint, context))
2605 + continue;
2606 + }
2607 +
2608 + /* `node' is a backreference.
2609 + Check the substring which the substring matched. */
2610 + bkc_idx = mctx->nbkref_ents;
2611 + err = get_subexp (mctx, node_idx, cur_str_idx);
2612 + if (BE (err != REG_NOERROR, 0))
2613 + goto free_return;
2614 +
2615 + /* And add the epsilon closures (which is `new_dest_nodes') of
2616 + the backreference to appropriate state_log. */
2617 +#ifdef DEBUG
2618 + assert (dfa->nexts[node_idx] != -1);
2619 +#endif
2620 + for (; bkc_idx < mctx->nbkref_ents; ++bkc_idx)
2621 + {
2622 + int subexp_len;
2623 + re_dfastate_t *dest_state;
2624 + struct re_backref_cache_entry *bkref_ent;
2625 + bkref_ent = mctx->bkref_ents + bkc_idx;
2626 + if (bkref_ent->node != node_idx || bkref_ent->str_idx != cur_str_idx)
2627 + continue;
2628 + subexp_len = bkref_ent->subexp_to - bkref_ent->subexp_from;
2629 + new_dest_nodes = (subexp_len == 0
2630 + ? dfa->eclosures + dfa->edests[node_idx].elems[0]
2631 + : dfa->eclosures + dfa->nexts[node_idx]);
2632 + dest_str_idx = (cur_str_idx + bkref_ent->subexp_to
2633 + - bkref_ent->subexp_from);
2634 + context = re_string_context_at (&mctx->input, dest_str_idx - 1,
2635 + mctx->eflags);
2636 + dest_state = mctx->state_log[dest_str_idx];
2637 + prev_nelem = ((mctx->state_log[cur_str_idx] == NULL) ? 0
2638 + : mctx->state_log[cur_str_idx]->nodes.nelem);
2639 + /* Add `new_dest_node' to state_log. */
2640 + if (dest_state == NULL)
2641 + {
2642 + mctx->state_log[dest_str_idx]
2643 + = re_acquire_state_context (&err, dfa, new_dest_nodes,
2644 + context);
2645 + if (BE (mctx->state_log[dest_str_idx] == NULL
2646 + && err != REG_NOERROR, 0))
2647 + goto free_return;
2648 + }
2649 + else
2650 + {
2651 + re_node_set dest_nodes;
2652 + err = re_node_set_init_union (&dest_nodes,
2653 + dest_state->entrance_nodes,
2654 + new_dest_nodes);
2655 + if (BE (err != REG_NOERROR, 0))
2656 + {
2657 + re_node_set_free (&dest_nodes);
2658 + goto free_return;
2659 + }
2660 + mctx->state_log[dest_str_idx]
2661 + = re_acquire_state_context (&err, dfa, &dest_nodes, context);
2662 + re_node_set_free (&dest_nodes);
2663 + if (BE (mctx->state_log[dest_str_idx] == NULL
2664 + && err != REG_NOERROR, 0))
2665 + goto free_return;
2666 + }
2667 + /* We need to check recursively if the backreference can epsilon
2668 + transit. */
2669 + if (subexp_len == 0
2670 + && mctx->state_log[cur_str_idx]->nodes.nelem > prev_nelem)
2671 + {
2672 + err = check_subexp_matching_top (mctx, new_dest_nodes,
2673 + cur_str_idx);
2674 + if (BE (err != REG_NOERROR, 0))
2675 + goto free_return;
2676 + err = transit_state_bkref (mctx, new_dest_nodes);
2677 + if (BE (err != REG_NOERROR, 0))
2678 + goto free_return;
2679 + }
2680 + }
2681 + }
2682 + err = REG_NOERROR;
2683 + free_return:
2684 + return err;
2685 +}
2686 +
2687 +/* Enumerate all the candidates which the backreference BKREF_NODE can match
2688 + at BKREF_STR_IDX, and register them by match_ctx_add_entry().
2689 + Note that we might collect inappropriate candidates here.
2690 + However, the cost of checking them strictly here is too high, then we
2691 + delay these checking for prune_impossible_nodes(). */
2692 +
2693 +static reg_errcode_t
2694 +internal_function
2695 +get_subexp (re_match_context_t *mctx, int bkref_node, int bkref_str_idx)
2696 +{
2697 + const re_dfa_t *const dfa = mctx->dfa;
2698 + int subexp_num, sub_top_idx;
2699 + const char *buf = (const char *) re_string_get_buffer (&mctx->input);
2700 + /* Return if we have already checked BKREF_NODE at BKREF_STR_IDX. */
2701 + int cache_idx = search_cur_bkref_entry (mctx, bkref_str_idx);
2702 + if (cache_idx != -1)
2703 + {
2704 + const struct re_backref_cache_entry *entry
2705 + = mctx->bkref_ents + cache_idx;
2706 + do
2707 + if (entry->node == bkref_node)
2708 + return REG_NOERROR; /* We already checked it. */
2709 + while (entry++->more);
2710 + }
2711 +
2712 + subexp_num = dfa->nodes[bkref_node].opr.idx;
2713 +
2714 + /* For each sub expression */
2715 + for (sub_top_idx = 0; sub_top_idx < mctx->nsub_tops; ++sub_top_idx)
2716 + {
2717 + reg_errcode_t err;
2718 + re_sub_match_top_t *sub_top = mctx->sub_tops[sub_top_idx];
2719 + re_sub_match_last_t *sub_last;
2720 + int sub_last_idx, sl_str, bkref_str_off;
2721 +
2722 + if (dfa->nodes[sub_top->node].opr.idx != subexp_num)
2723 + continue; /* It isn't related. */
2724 +
2725 + sl_str = sub_top->str_idx;
2726 + bkref_str_off = bkref_str_idx;
2727 + /* At first, check the last node of sub expressions we already
2728 + evaluated. */
2729 + for (sub_last_idx = 0; sub_last_idx < sub_top->nlasts; ++sub_last_idx)
2730 + {
2731 + int sl_str_diff;
2732 + sub_last = sub_top->lasts[sub_last_idx];
2733 + sl_str_diff = sub_last->str_idx - sl_str;
2734 + /* The matched string by the sub expression match with the substring
2735 + at the back reference? */
2736 + if (sl_str_diff > 0)
2737 + {
2738 + if (BE (bkref_str_off + sl_str_diff > mctx->input.valid_len, 0))
2739 + {
2740 + /* Not enough chars for a successful match. */
2741 + if (bkref_str_off + sl_str_diff > mctx->input.len)
2742 + break;
2743 +
2744 + err = clean_state_log_if_needed (mctx,
2745 + bkref_str_off
2746 + + sl_str_diff);
2747 + if (BE (err != REG_NOERROR, 0))
2748 + return err;
2749 + buf = (const char *) re_string_get_buffer (&mctx->input);
2750 + }
2751 + if (memcmp (buf + bkref_str_off, buf + sl_str, sl_str_diff) != 0)
2752 + /* We don't need to search this sub expression any more. */
2753 + break;
2754 + }
2755 + bkref_str_off += sl_str_diff;
2756 + sl_str += sl_str_diff;
2757 + err = get_subexp_sub (mctx, sub_top, sub_last, bkref_node,
2758 + bkref_str_idx);
2759 +
2760 + /* Reload buf, since the preceding call might have reallocated
2761 + the buffer. */
2762 + buf = (const char *) re_string_get_buffer (&mctx->input);
2763 +
2764 + if (err == REG_NOMATCH)
2765 + continue;
2766 + if (BE (err != REG_NOERROR, 0))
2767 + return err;
2768 + }
2769 +
2770 + if (sub_last_idx < sub_top->nlasts)
2771 + continue;
2772 + if (sub_last_idx > 0)
2773 + ++sl_str;
2774 + /* Then, search for the other last nodes of the sub expression. */
2775 + for (; sl_str <= bkref_str_idx; ++sl_str)
2776 + {
2777 + int cls_node, sl_str_off;
2778 + const re_node_set *nodes;
2779 + sl_str_off = sl_str - sub_top->str_idx;
2780 + /* The matched string by the sub expression match with the substring
2781 + at the back reference? */
2782 + if (sl_str_off > 0)
2783 + {
2784 + if (BE (bkref_str_off >= mctx->input.valid_len, 0))
2785 + {
2786 + /* If we are at the end of the input, we cannot match. */
2787 + if (bkref_str_off >= mctx->input.len)
2788 + break;
2789 +
2790 + err = extend_buffers (mctx);
2791 + if (BE (err != REG_NOERROR, 0))
2792 + return err;
2793 +
2794 + buf = (const char *) re_string_get_buffer (&mctx->input);
2795 + }
2796 + if (buf [bkref_str_off++] != buf[sl_str - 1])
2797 + break; /* We don't need to search this sub expression
2798 + any more. */
2799 + }
2800 + if (mctx->state_log[sl_str] == NULL)
2801 + continue;
2802 + /* Does this state have a ')' of the sub expression? */
2803 + nodes = &mctx->state_log[sl_str]->nodes;
2804 + cls_node = find_subexp_node (dfa, nodes, subexp_num,
2805 + OP_CLOSE_SUBEXP);
2806 + if (cls_node == -1)
2807 + continue; /* No. */
2808 + if (sub_top->path == NULL)
2809 + {
2810 + sub_top->path = calloc (sizeof (state_array_t),
2811 + sl_str - sub_top->str_idx + 1);
2812 + if (sub_top->path == NULL)
2813 + return REG_ESPACE;
2814 + }
2815 + /* Can the OP_OPEN_SUBEXP node arrive the OP_CLOSE_SUBEXP node
2816 + in the current context? */
2817 + err = check_arrival (mctx, sub_top->path, sub_top->node,
2818 + sub_top->str_idx, cls_node, sl_str,
2819 + OP_CLOSE_SUBEXP);
2820 + if (err == REG_NOMATCH)
2821 + continue;
2822 + if (BE (err != REG_NOERROR, 0))
2823 + return err;
2824 + sub_last = match_ctx_add_sublast (sub_top, cls_node, sl_str);
2825 + if (BE (sub_last == NULL, 0))
2826 + return REG_ESPACE;
2827 + err = get_subexp_sub (mctx, sub_top, sub_last, bkref_node,
2828 + bkref_str_idx);
2829 + if (err == REG_NOMATCH)
2830 + continue;
2831 + }
2832 + }
2833 + return REG_NOERROR;
2834 +}
2835 +
2836 +/* Helper functions for get_subexp(). */
2837 +
2838 +/* Check SUB_LAST can arrive to the back reference BKREF_NODE at BKREF_STR.
2839 + If it can arrive, register the sub expression expressed with SUB_TOP
2840 + and SUB_LAST. */
2841 +
2842 +static reg_errcode_t
2843 +internal_function
2844 +get_subexp_sub (re_match_context_t *mctx, const re_sub_match_top_t *sub_top,
2845 + re_sub_match_last_t *sub_last, int bkref_node, int bkref_str)
2846 +{
2847 + reg_errcode_t err;
2848 + int to_idx;
2849 + /* Can the subexpression arrive the back reference? */
2850 + err = check_arrival (mctx, &sub_last->path, sub_last->node,
2851 + sub_last->str_idx, bkref_node, bkref_str,
2852 + OP_OPEN_SUBEXP);
2853 + if (err != REG_NOERROR)
2854 + return err;
2855 + err = match_ctx_add_entry (mctx, bkref_node, bkref_str, sub_top->str_idx,
2856 + sub_last->str_idx);
2857 + if (BE (err != REG_NOERROR, 0))
2858 + return err;
2859 + to_idx = bkref_str + sub_last->str_idx - sub_top->str_idx;
2860 + return clean_state_log_if_needed (mctx, to_idx);
2861 +}
2862 +
2863 +/* Find the first node which is '(' or ')' and whose index is SUBEXP_IDX.
2864 + Search '(' if FL_OPEN, or search ')' otherwise.
2865 + TODO: This function isn't efficient...
2866 + Because there might be more than one nodes whose types are
2867 + OP_OPEN_SUBEXP and whose index is SUBEXP_IDX, we must check all
2868 + nodes.
2869 + E.g. RE: (a){2} */
2870 +
2871 +static int
2872 +internal_function
2873 +find_subexp_node (const re_dfa_t *dfa, const re_node_set *nodes,
2874 + int subexp_idx, int type)
2875 +{
2876 + int cls_idx;
2877 + for (cls_idx = 0; cls_idx < nodes->nelem; ++cls_idx)
2878 + {
2879 + int cls_node = nodes->elems[cls_idx];
2880 + const re_token_t *node = dfa->nodes + cls_node;
2881 + if (node->type == type
2882 + && node->opr.idx == subexp_idx)
2883 + return cls_node;
2884 + }
2885 + return -1;
2886 +}
2887 +
2888 +/* Check whether the node TOP_NODE at TOP_STR can arrive to the node
2889 + LAST_NODE at LAST_STR. We record the path onto PATH since it will be
2890 + heavily reused.
2891 + Return REG_NOERROR if it can arrive, or REG_NOMATCH otherwise. */
2892 +
2893 +static reg_errcode_t
2894 +internal_function
2895 +check_arrival (re_match_context_t *mctx, state_array_t *path, int top_node,
2896 + int top_str, int last_node, int last_str, int type)
2897 +{
2898 + const re_dfa_t *const dfa = mctx->dfa;
2899 + reg_errcode_t err = REG_NOERROR;
2900 + int subexp_num, backup_cur_idx, str_idx, null_cnt;
2901 + re_dfastate_t *cur_state = NULL;
2902 + re_node_set *cur_nodes, next_nodes;
2903 + re_dfastate_t **backup_state_log;
2904 + unsigned int context;
2905 +
2906 + subexp_num = dfa->nodes[top_node].opr.idx;
2907 + /* Extend the buffer if we need. */
2908 + if (BE (path->alloc < last_str + mctx->max_mb_elem_len + 1, 0))
2909 + {
2910 + re_dfastate_t **new_array;
2911 + int old_alloc = path->alloc;
2912 + path->alloc += last_str + mctx->max_mb_elem_len + 1;
2913 + new_array = re_realloc (path->array, re_dfastate_t *, path->alloc);
2914 + if (BE (new_array == NULL, 0))
2915 + {
2916 + path->alloc = old_alloc;
2917 + return REG_ESPACE;
2918 + }
2919 + path->array = new_array;
2920 + memset (new_array + old_alloc, '\0',
2921 + sizeof (re_dfastate_t *) * (path->alloc - old_alloc));
2922 + }
2923 +
2924 + str_idx = path->next_idx ? path->next_idx : top_str;
2925 +
2926 + /* Temporary modify MCTX. */
2927 + backup_state_log = mctx->state_log;
2928 + backup_cur_idx = mctx->input.cur_idx;
2929 + mctx->state_log = path->array;
2930 + mctx->input.cur_idx = str_idx;
2931 +
2932 + /* Setup initial node set. */
2933 + context = re_string_context_at (&mctx->input, str_idx - 1, mctx->eflags);
2934 + if (str_idx == top_str)
2935 + {
2936 + err = re_node_set_init_1 (&next_nodes, top_node);
2937 + if (BE (err != REG_NOERROR, 0))
2938 + return err;
2939 + err = check_arrival_expand_ecl (dfa, &next_nodes, subexp_num, type);
2940 + if (BE (err != REG_NOERROR, 0))
2941 + {
2942 + re_node_set_free (&next_nodes);
2943 + return err;
2944 + }
2945 + }
2946 + else
2947 + {
2948 + cur_state = mctx->state_log[str_idx];
2949 + if (cur_state && cur_state->has_backref)
2950 + {
2951 + err = re_node_set_init_copy (&next_nodes, &cur_state->nodes);
2952 + if (BE (err != REG_NOERROR, 0))
2953 + return err;
2954 + }
2955 + else
2956 + re_node_set_init_empty (&next_nodes);
2957 + }
2958 + if (str_idx == top_str || (cur_state && cur_state->has_backref))
2959 + {
2960 + if (next_nodes.nelem)
2961 + {
2962 + err = expand_bkref_cache (mctx, &next_nodes, str_idx,
2963 + subexp_num, type);
2964 + if (BE (err != REG_NOERROR, 0))
2965 + {
2966 + re_node_set_free (&next_nodes);
2967 + return err;
2968 + }
2969 + }
2970 + cur_state = re_acquire_state_context (&err, dfa, &next_nodes, context);
2971 + if (BE (cur_state == NULL && err != REG_NOERROR, 0))
2972 + {
2973 + re_node_set_free (&next_nodes);
2974 + return err;
2975 + }
2976 + mctx->state_log[str_idx] = cur_state;
2977 + }
2978 +
2979 + for (null_cnt = 0; str_idx < last_str && null_cnt <= mctx->max_mb_elem_len;)
2980 + {
2981 + re_node_set_empty (&next_nodes);
2982 + if (mctx->state_log[str_idx + 1])
2983 + {
2984 + err = re_node_set_merge (&next_nodes,
2985 + &mctx->state_log[str_idx + 1]->nodes);
2986 + if (BE (err != REG_NOERROR, 0))
2987 + {
2988 + re_node_set_free (&next_nodes);
2989 + return err;
2990 + }
2991 + }
2992 + if (cur_state)
2993 + {
2994 + err = check_arrival_add_next_nodes (mctx, str_idx,
2995 + &cur_state->non_eps_nodes,
2996 + &next_nodes);
2997 + if (BE (err != REG_NOERROR, 0))
2998 + {
2999 + re_node_set_free (&next_nodes);
3000 + return err;
3001 + }
3002 + }
3003 + ++str_idx;
3004 + if (next_nodes.nelem)
3005 + {
3006 + err = check_arrival_expand_ecl (dfa, &next_nodes, subexp_num, type);
3007 + if (BE (err != REG_NOERROR, 0))
3008 + {
3009 + re_node_set_free (&next_nodes);
3010 + return err;
3011 + }
3012 + err = expand_bkref_cache (mctx, &next_nodes, str_idx,
3013 + subexp_num, type);
3014 + if (BE (err != REG_NOERROR, 0))
3015 + {
3016 + re_node_set_free (&next_nodes);
3017 + return err;
3018 + }
3019 + }
3020 + context = re_string_context_at (&mctx->input, str_idx - 1, mctx->eflags);
3021 + cur_state = re_acquire_state_context (&err, dfa, &next_nodes, context);
3022 + if (BE (cur_state == NULL && err != REG_NOERROR, 0))
3023 + {
3024 + re_node_set_free (&next_nodes);
3025 + return err;
3026 + }
3027 + mctx->state_log[str_idx] = cur_state;
3028 + null_cnt = cur_state == NULL ? null_cnt + 1 : 0;
3029 + }
3030 + re_node_set_free (&next_nodes);
3031 + cur_nodes = (mctx->state_log[last_str] == NULL ? NULL
3032 + : &mctx->state_log[last_str]->nodes);
3033 + path->next_idx = str_idx;
3034 +
3035 + /* Fix MCTX. */
3036 + mctx->state_log = backup_state_log;
3037 + mctx->input.cur_idx = backup_cur_idx;
3038 +
3039 + /* Then check the current node set has the node LAST_NODE. */
3040 + if (cur_nodes != NULL && re_node_set_contains (cur_nodes, last_node))
3041 + return REG_NOERROR;
3042 +
3043 + return REG_NOMATCH;
3044 +}
3045 +
3046 +/* Helper functions for check_arrival. */
3047 +
3048 +/* Calculate the destination nodes of CUR_NODES at STR_IDX, and append them
3049 + to NEXT_NODES.
3050 + TODO: This function is similar to the functions transit_state*(),
3051 + however this function has many additional works.
3052 + Can't we unify them? */
3053 +
3054 +static reg_errcode_t
3055 +internal_function
3056 +check_arrival_add_next_nodes (re_match_context_t *mctx, int str_idx,
3057 + re_node_set *cur_nodes, re_node_set *next_nodes)
3058 +{
3059 + const re_dfa_t *const dfa = mctx->dfa;
3060 + int result;
3061 + int cur_idx;
3062 + reg_errcode_t err = REG_NOERROR;
3063 + re_node_set union_set;
3064 + re_node_set_init_empty (&union_set);
3065 + for (cur_idx = 0; cur_idx < cur_nodes->nelem; ++cur_idx)
3066 + {
3067 + int naccepted = 0;
3068 + int cur_node = cur_nodes->elems[cur_idx];
3069 +#ifdef DEBUG
3070 + re_token_type_t type = dfa->nodes[cur_node].type;
3071 + assert (!IS_EPSILON_NODE (type));
3072 +#endif
3073 +#ifdef RE_ENABLE_I18N
3074 + /* If the node may accept `multi byte'. */
3075 + if (dfa->nodes[cur_node].accept_mb)
3076 + {
3077 + naccepted = check_node_accept_bytes (dfa, cur_node, &mctx->input,
3078 + str_idx);
3079 + if (naccepted > 1)
3080 + {
3081 + re_dfastate_t *dest_state;
3082 + int next_node = dfa->nexts[cur_node];
3083 + int next_idx = str_idx + naccepted;
3084 + dest_state = mctx->state_log[next_idx];
3085 + re_node_set_empty (&union_set);
3086 + if (dest_state)
3087 + {
3088 + err = re_node_set_merge (&union_set, &dest_state->nodes);
3089 + if (BE (err != REG_NOERROR, 0))
3090 + {
3091 + re_node_set_free (&union_set);
3092 + return err;
3093 + }
3094 + }
3095 + result = re_node_set_insert (&union_set, next_node);
3096 + if (BE (result < 0, 0))
3097 + {
3098 + re_node_set_free (&union_set);
3099 + return REG_ESPACE;
3100 + }
3101 + mctx->state_log[next_idx] = re_acquire_state (&err, dfa,
3102 + &union_set);
3103 + if (BE (mctx->state_log[next_idx] == NULL
3104 + && err != REG_NOERROR, 0))
3105 + {
3106 + re_node_set_free (&union_set);
3107 + return err;
3108 + }
3109 + }
3110 + }
3111 +#endif /* RE_ENABLE_I18N */
3112 + if (naccepted
3113 + || check_node_accept (mctx, dfa->nodes + cur_node, str_idx))
3114 + {
3115 + result = re_node_set_insert (next_nodes, dfa->nexts[cur_node]);
3116 + if (BE (result < 0, 0))
3117 + {
3118 + re_node_set_free (&union_set);
3119 + return REG_ESPACE;
3120 + }
3121 + }
3122 + }
3123 + re_node_set_free (&union_set);
3124 + return REG_NOERROR;
3125 +}
3126 +
3127 +/* For all the nodes in CUR_NODES, add the epsilon closures of them to
3128 + CUR_NODES, however exclude the nodes which are:
3129 + - inside the sub expression whose number is EX_SUBEXP, if FL_OPEN.
3130 + - out of the sub expression whose number is EX_SUBEXP, if !FL_OPEN.
3131 +*/
3132 +
3133 +static reg_errcode_t
3134 +internal_function
3135 +check_arrival_expand_ecl (const re_dfa_t *dfa, re_node_set *cur_nodes,
3136 + int ex_subexp, int type)
3137 +{
3138 + reg_errcode_t err;
3139 + int idx, outside_node;
3140 + re_node_set new_nodes;
3141 +#ifdef DEBUG
3142 + assert (cur_nodes->nelem);
3143 +#endif
3144 + err = re_node_set_alloc (&new_nodes, cur_nodes->nelem);
3145 + if (BE (err != REG_NOERROR, 0))
3146 + return err;
3147 + /* Create a new node set NEW_NODES with the nodes which are epsilon
3148 + closures of the node in CUR_NODES. */
3149 +
3150 + for (idx = 0; idx < cur_nodes->nelem; ++idx)
3151 + {
3152 + int cur_node = cur_nodes->elems[idx];
3153 + const re_node_set *eclosure = dfa->eclosures + cur_node;
3154 + outside_node = find_subexp_node (dfa, eclosure, ex_subexp, type);
3155 + if (outside_node == -1)
3156 + {
3157 + /* There are no problematic nodes, just merge them. */
3158 + err = re_node_set_merge (&new_nodes, eclosure);
3159 + if (BE (err != REG_NOERROR, 0))
3160 + {
3161 + re_node_set_free (&new_nodes);
3162 + return err;
3163 + }
3164 + }
3165 + else
3166 + {
3167 + /* There are problematic nodes, re-calculate incrementally. */
3168 + err = check_arrival_expand_ecl_sub (dfa, &new_nodes, cur_node,
3169 + ex_subexp, type);
3170 + if (BE (err != REG_NOERROR, 0))
3171 + {
3172 + re_node_set_free (&new_nodes);
3173 + return err;
3174 + }
3175 + }
3176 + }
3177 + re_node_set_free (cur_nodes);
3178 + *cur_nodes = new_nodes;
3179 + return REG_NOERROR;
3180 +}
3181 +
3182 +/* Helper function for check_arrival_expand_ecl.
3183 + Check incrementally the epsilon closure of TARGET, and if it isn't
3184 + problematic append it to DST_NODES. */
3185 +
3186 +static reg_errcode_t
3187 +internal_function
3188 +check_arrival_expand_ecl_sub (const re_dfa_t *dfa, re_node_set *dst_nodes,
3189 + int target, int ex_subexp, int type)
3190 +{
3191 + int cur_node;
3192 + for (cur_node = target; !re_node_set_contains (dst_nodes, cur_node);)
3193 + {
3194 + int err;
3195 +
3196 + if (dfa->nodes[cur_node].type == type
3197 + && dfa->nodes[cur_node].opr.idx == ex_subexp)
3198 + {
3199 + if (type == OP_CLOSE_SUBEXP)
3200 + {
3201 + err = re_node_set_insert (dst_nodes, cur_node);
3202 + if (BE (err == -1, 0))
3203 + return REG_ESPACE;
3204 + }
3205 + break;
3206 + }
3207 + err = re_node_set_insert (dst_nodes, cur_node);
3208 + if (BE (err == -1, 0))
3209 + return REG_ESPACE;
3210 + if (dfa->edests[cur_node].nelem == 0)
3211 + break;
3212 + if (dfa->edests[cur_node].nelem == 2)
3213 + {
3214 + err = check_arrival_expand_ecl_sub (dfa, dst_nodes,
3215 + dfa->edests[cur_node].elems[1],
3216 + ex_subexp, type);
3217 + if (BE (err != REG_NOERROR, 0))
3218 + return err;
3219 + }
3220 + cur_node = dfa->edests[cur_node].elems[0];
3221 + }
3222 + return REG_NOERROR;
3223 +}
3224 +
3225 +
3226 +/* For all the back references in the current state, calculate the
3227 + destination of the back references by the appropriate entry
3228 + in MCTX->BKREF_ENTS. */
3229 +
3230 +static reg_errcode_t
3231 +internal_function
3232 +expand_bkref_cache (re_match_context_t *mctx, re_node_set *cur_nodes,
3233 + int cur_str, int subexp_num, int type)
3234 +{
3235 + const re_dfa_t *const dfa = mctx->dfa;
3236 + reg_errcode_t err;
3237 + int cache_idx_start = search_cur_bkref_entry (mctx, cur_str);
3238 + struct re_backref_cache_entry *ent;
3239 +
3240 + if (cache_idx_start == -1)
3241 + return REG_NOERROR;
3242 +
3243 + restart:
3244 + ent = mctx->bkref_ents + cache_idx_start;
3245 + do
3246 + {
3247 + int to_idx, next_node;
3248 +
3249 + /* Is this entry ENT is appropriate? */
3250 + if (!re_node_set_contains (cur_nodes, ent->node))
3251 + continue; /* No. */
3252 +
3253 + to_idx = cur_str + ent->subexp_to - ent->subexp_from;
3254 + /* Calculate the destination of the back reference, and append it
3255 + to MCTX->STATE_LOG. */
3256 + if (to_idx == cur_str)
3257 + {
3258 + /* The backreference did epsilon transit, we must re-check all the
3259 + node in the current state. */
3260 + re_node_set new_dests;
3261 + reg_errcode_t err2, err3;
3262 + next_node = dfa->edests[ent->node].elems[0];
3263 + if (re_node_set_contains (cur_nodes, next_node))
3264 + continue;
3265 + err = re_node_set_init_1 (&new_dests, next_node);
3266 + err2 = check_arrival_expand_ecl (dfa, &new_dests, subexp_num, type);
3267 + err3 = re_node_set_merge (cur_nodes, &new_dests);
3268 + re_node_set_free (&new_dests);
3269 + if (BE (err != REG_NOERROR || err2 != REG_NOERROR
3270 + || err3 != REG_NOERROR, 0))
3271 + {
3272 + err = (err != REG_NOERROR ? err
3273 + : (err2 != REG_NOERROR ? err2 : err3));
3274 + return err;
3275 + }
3276 + /* TODO: It is still inefficient... */
3277 + goto restart;
3278 + }
3279 + else
3280 + {
3281 + re_node_set union_set;
3282 + next_node = dfa->nexts[ent->node];
3283 + if (mctx->state_log[to_idx])
3284 + {
3285 + int ret;
3286 + if (re_node_set_contains (&mctx->state_log[to_idx]->nodes,
3287 + next_node))
3288 + continue;
3289 + err = re_node_set_init_copy (&union_set,
3290 + &mctx->state_log[to_idx]->nodes);
3291 + ret = re_node_set_insert (&union_set, next_node);
3292 + if (BE (err != REG_NOERROR || ret < 0, 0))
3293 + {
3294 + re_node_set_free (&union_set);
3295 + err = err != REG_NOERROR ? err : REG_ESPACE;
3296 + return err;
3297 + }
3298 + }
3299 + else
3300 + {
3301 + err = re_node_set_init_1 (&union_set, next_node);
3302 + if (BE (err != REG_NOERROR, 0))
3303 + return err;
3304 + }
3305 + mctx->state_log[to_idx] = re_acquire_state (&err, dfa, &union_set);
3306 + re_node_set_free (&union_set);
3307 + if (BE (mctx->state_log[to_idx] == NULL
3308 + && err != REG_NOERROR, 0))
3309 + return err;
3310 + }
3311 + }
3312 + while (ent++->more);
3313 + return REG_NOERROR;
3314 +}
3315 +
3316 +/* Build transition table for the state.
3317 + Return 1 if succeeded, otherwise return NULL. */
3318 +
3319 +static int
3320 +internal_function
3321 +build_trtable (const re_dfa_t *dfa, re_dfastate_t *state)
3322 +{
3323 + reg_errcode_t err;
3324 + int i, j, ch, need_word_trtable = 0;
3325 + bitset_word_t elem, mask;
3326 + bool dests_node_malloced = false;
3327 + bool dest_states_malloced = false;
3328 + int ndests; /* Number of the destination states from `state'. */
3329 + re_dfastate_t **trtable;
3330 + re_dfastate_t **dest_states = NULL, **dest_states_word, **dest_states_nl;
3331 + re_node_set follows, *dests_node;
3332 + bitset_t *dests_ch;
3333 + bitset_t acceptable;
3334 +
3335 + struct dests_alloc
3336 + {
3337 + re_node_set dests_node[SBC_MAX];
3338 + bitset_t dests_ch[SBC_MAX];
3339 + } *dests_alloc;
3340 +
3341 + /* We build DFA states which corresponds to the destination nodes
3342 + from `state'. `dests_node[i]' represents the nodes which i-th
3343 + destination state contains, and `dests_ch[i]' represents the
3344 + characters which i-th destination state accepts. */
3345 +#ifdef HAVE_ALLOCA
3346 + if (__libc_use_alloca (sizeof (struct dests_alloc)))
3347 + dests_alloc = (struct dests_alloc *) alloca (sizeof (struct dests_alloc));
3348 + else
3349 +#endif
3350 + {
3351 + dests_alloc = re_malloc (struct dests_alloc, 1);
3352 + if (BE (dests_alloc == NULL, 0))
3353 + return 0;
3354 + dests_node_malloced = true;
3355 + }
3356 + dests_node = dests_alloc->dests_node;
3357 + dests_ch = dests_alloc->dests_ch;
3358 +
3359 + /* Initialize transiton table. */
3360 + state->word_trtable = state->trtable = NULL;
3361 +
3362 + /* At first, group all nodes belonging to `state' into several
3363 + destinations. */
3364 + ndests = group_nodes_into_DFAstates (dfa, state, dests_node, dests_ch);
3365 + if (BE (ndests <= 0, 0))
3366 + {
3367 + if (dests_node_malloced)
3368 + free (dests_alloc);
3369 + /* Return 0 in case of an error, 1 otherwise. */
3370 + if (ndests == 0)
3371 + {
3372 + state->trtable = (re_dfastate_t **)
3373 + calloc (sizeof (re_dfastate_t *), SBC_MAX);
3374 + return 1;
3375 + }
3376 + return 0;
3377 + }
3378 +
3379 + err = re_node_set_alloc (&follows, ndests + 1);
3380 + if (BE (err != REG_NOERROR, 0))
3381 + goto out_free;
3382 +
3383 + /* Avoid arithmetic overflow in size calculation. */
3384 + if (BE ((((SIZE_MAX - (sizeof (re_node_set) + sizeof (bitset_t)) * SBC_MAX)
3385 + / (3 * sizeof (re_dfastate_t *)))
3386 + < ndests),
3387 + 0))
3388 + goto out_free;
3389 +
3390 +#ifdef HAVE_ALLOCA
3391 + if (__libc_use_alloca ((sizeof (re_node_set) + sizeof (bitset_t)) * SBC_MAX
3392 + + ndests * 3 * sizeof (re_dfastate_t *)))
3393 + dest_states = (re_dfastate_t **)
3394 + alloca (ndests * 3 * sizeof (re_dfastate_t *));
3395 + else
3396 +#endif
3397 + {
3398 + dest_states = (re_dfastate_t **)
3399 + malloc (ndests * 3 * sizeof (re_dfastate_t *));
3400 + if (BE (dest_states == NULL, 0))
3401 + {
3402 +out_free:
3403 + if (dest_states_malloced)
3404 + free (dest_states);
3405 + re_node_set_free (&follows);
3406 + for (i = 0; i < ndests; ++i)
3407 + re_node_set_free (dests_node + i);
3408 + if (dests_node_malloced)
3409 + free (dests_alloc);
3410 + return 0;
3411 + }
3412 + dest_states_malloced = true;
3413 + }
3414 + dest_states_word = dest_states + ndests;
3415 + dest_states_nl = dest_states_word + ndests;
3416 + bitset_empty (acceptable);
3417 +
3418 + /* Then build the states for all destinations. */
3419 + for (i = 0; i < ndests; ++i)
3420 + {
3421 + int next_node;
3422 + re_node_set_empty (&follows);
3423 + /* Merge the follows of this destination states. */
3424 + for (j = 0; j < dests_node[i].nelem; ++j)
3425 + {
3426 + next_node = dfa->nexts[dests_node[i].elems[j]];
3427 + if (next_node != -1)
3428 + {
3429 + err = re_node_set_merge (&follows, dfa->eclosures + next_node);
3430 + if (BE (err != REG_NOERROR, 0))
3431 + goto out_free;
3432 + }
3433 + }
3434 + dest_states[i] = re_acquire_state_context (&err, dfa, &follows, 0);
3435 + if (BE (dest_states[i] == NULL && err != REG_NOERROR, 0))
3436 + goto out_free;
3437 + /* If the new state has context constraint,
3438 + build appropriate states for these contexts. */
3439 + if (dest_states[i]->has_constraint)
3440 + {
3441 + dest_states_word[i] = re_acquire_state_context (&err, dfa, &follows,
3442 + CONTEXT_WORD);
3443 + if (BE (dest_states_word[i] == NULL && err != REG_NOERROR, 0))
3444 + goto out_free;
3445 +
3446 + if (dest_states[i] != dest_states_word[i] && dfa->mb_cur_max > 1)
3447 + need_word_trtable = 1;
3448 +
3449 + dest_states_nl[i] = re_acquire_state_context (&err, dfa, &follows,
3450 + CONTEXT_NEWLINE);
3451 + if (BE (dest_states_nl[i] == NULL && err != REG_NOERROR, 0))
3452 + goto out_free;
3453 + }
3454 + else
3455 + {
3456 + dest_states_word[i] = dest_states[i];
3457 + dest_states_nl[i] = dest_states[i];
3458 + }
3459 + bitset_merge (acceptable, dests_ch[i]);
3460 + }
3461 +
3462 + if (!BE (need_word_trtable, 0))
3463 + {
3464 + /* We don't care about whether the following character is a word
3465 + character, or we are in a single-byte character set so we can
3466 + discern by looking at the character code: allocate a
3467 + 256-entry transition table. */
3468 + trtable = state->trtable =
3469 + (re_dfastate_t **) calloc (sizeof (re_dfastate_t *), SBC_MAX);
3470 + if (BE (trtable == NULL, 0))
3471 + goto out_free;
3472 +
3473 + /* For all characters ch...: */
3474 + for (i = 0; i < BITSET_WORDS; ++i)
3475 + for (ch = i * BITSET_WORD_BITS, elem = acceptable[i], mask = 1;
3476 + elem;
3477 + mask <<= 1, elem >>= 1, ++ch)
3478 + if (BE (elem & 1, 0))
3479 + {
3480 + /* There must be exactly one destination which accepts
3481 + character ch. See group_nodes_into_DFAstates. */
3482 + for (j = 0; (dests_ch[j][i] & mask) == 0; ++j)
3483 + ;
3484 +
3485 + /* j-th destination accepts the word character ch. */
3486 + if (dfa->word_char[i] & mask)
3487 + trtable[ch] = dest_states_word[j];
3488 + else
3489 + trtable[ch] = dest_states[j];
3490 + }
3491 + }
3492 + else
3493 + {
3494 + /* We care about whether the following character is a word
3495 + character, and we are in a multi-byte character set: discern
3496 + by looking at the character code: build two 256-entry
3497 + transition tables, one starting at trtable[0] and one
3498 + starting at trtable[SBC_MAX]. */
3499 + trtable = state->word_trtable =
3500 + (re_dfastate_t **) calloc (sizeof (re_dfastate_t *), 2 * SBC_MAX);
3501 + if (BE (trtable == NULL, 0))
3502 + goto out_free;
3503 +
3504 + /* For all characters ch...: */
3505 + for (i = 0; i < BITSET_WORDS; ++i)
3506 + for (ch = i * BITSET_WORD_BITS, elem = acceptable[i], mask = 1;
3507 + elem;
3508 + mask <<= 1, elem >>= 1, ++ch)
3509 + if (BE (elem & 1, 0))
3510 + {
3511 + /* There must be exactly one destination which accepts
3512 + character ch. See group_nodes_into_DFAstates. */
3513 + for (j = 0; (dests_ch[j][i] & mask) == 0; ++j)
3514 + ;
3515 +
3516 + /* j-th destination accepts the word character ch. */
3517 + trtable[ch] = dest_states[j];
3518 + trtable[ch + SBC_MAX] = dest_states_word[j];
3519 + }
3520 + }
3521 +
3522 + /* new line */
3523 + if (bitset_contain (acceptable, NEWLINE_CHAR))
3524 + {
3525 + /* The current state accepts newline character. */
3526 + for (j = 0; j < ndests; ++j)
3527 + if (bitset_contain (dests_ch[j], NEWLINE_CHAR))
3528 + {
3529 + /* k-th destination accepts newline character. */
3530 + trtable[NEWLINE_CHAR] = dest_states_nl[j];
3531 + if (need_word_trtable)
3532 + trtable[NEWLINE_CHAR + SBC_MAX] = dest_states_nl[j];
3533 + /* There must be only one destination which accepts
3534 + newline. See group_nodes_into_DFAstates. */
3535 + break;
3536 + }
3537 + }
3538 +
3539 + if (dest_states_malloced)
3540 + free (dest_states);
3541 +
3542 + re_node_set_free (&follows);
3543 + for (i = 0; i < ndests; ++i)
3544 + re_node_set_free (dests_node + i);
3545 +
3546 + if (dests_node_malloced)
3547 + free (dests_alloc);
3548 +
3549 + return 1;
3550 +}
3551 +
3552 +/* Group all nodes belonging to STATE into several destinations.
3553 + Then for all destinations, set the nodes belonging to the destination
3554 + to DESTS_NODE[i] and set the characters accepted by the destination
3555 + to DEST_CH[i]. This function return the number of destinations. */
3556 +
3557 +static int
3558 +internal_function
3559 +group_nodes_into_DFAstates (const re_dfa_t *dfa, const re_dfastate_t *state,
3560 + re_node_set *dests_node, bitset_t *dests_ch)
3561 +{
3562 + reg_errcode_t err;
3563 + int result;
3564 + int i, j, k;
3565 + int ndests; /* Number of the destinations from `state'. */
3566 + bitset_t accepts; /* Characters a node can accept. */
3567 + const re_node_set *cur_nodes = &state->nodes;
3568 + bitset_empty (accepts);
3569 + ndests = 0;
3570 +
3571 + /* For all the nodes belonging to `state', */
3572 + for (i = 0; i < cur_nodes->nelem; ++i)
3573 + {
3574 + re_token_t *node = &dfa->nodes[cur_nodes->elems[i]];
3575 + re_token_type_t type = node->type;
3576 + unsigned int constraint = node->constraint;
3577 +
3578 + /* Enumerate all single byte character this node can accept. */
3579 + if (type == CHARACTER)
3580 + bitset_set (accepts, node->opr.c);
3581 + else if (type == SIMPLE_BRACKET)
3582 + {
3583 + bitset_merge (accepts, node->opr.sbcset);
3584 + }
3585 + else if (type == OP_PERIOD)
3586 + {
3587 +#ifdef RE_ENABLE_I18N
3588 + if (dfa->mb_cur_max > 1)
3589 + bitset_merge (accepts, dfa->sb_char);
3590 + else
3591 +#endif
3592 + bitset_set_all (accepts);
3593 + if (!(dfa->syntax & RE_DOT_NEWLINE))
3594 + bitset_clear (accepts, '\n');
3595 + if (dfa->syntax & RE_DOT_NOT_NULL)
3596 + bitset_clear (accepts, '\0');
3597 + }
3598 +#ifdef RE_ENABLE_I18N
3599 + else if (type == OP_UTF8_PERIOD)
3600 + {
3601 + memset (accepts, '\xff', sizeof (bitset_t) / 2);
3602 + if (!(dfa->syntax & RE_DOT_NEWLINE))
3603 + bitset_clear (accepts, '\n');
3604 + if (dfa->syntax & RE_DOT_NOT_NULL)
3605 + bitset_clear (accepts, '\0');
3606 + }
3607 +#endif
3608 + else
3609 + continue;
3610 +
3611 + /* Check the `accepts' and sift the characters which are not
3612 + match it the context. */
3613 + if (constraint)
3614 + {
3615 + if (constraint & NEXT_NEWLINE_CONSTRAINT)
3616 + {
3617 + bool accepts_newline = bitset_contain (accepts, NEWLINE_CHAR);
3618 + bitset_empty (accepts);
3619 + if (accepts_newline)
3620 + bitset_set (accepts, NEWLINE_CHAR);
3621 + else
3622 + continue;
3623 + }
3624 + if (constraint & NEXT_ENDBUF_CONSTRAINT)
3625 + {
3626 + bitset_empty (accepts);
3627 + continue;
3628 + }
3629 +
3630 + if (constraint & NEXT_WORD_CONSTRAINT)
3631 + {
3632 + bitset_word_t any_set = 0;
3633 + if (type == CHARACTER && !node->word_char)
3634 + {
3635 + bitset_empty (accepts);
3636 + continue;
3637 + }
3638 +#ifdef RE_ENABLE_I18N
3639 + if (dfa->mb_cur_max > 1)
3640 + for (j = 0; j < BITSET_WORDS; ++j)
3641 + any_set |= (accepts[j] &= (dfa->word_char[j] | ~dfa->sb_char[j]));
3642 + else
3643 +#endif
3644 + for (j = 0; j < BITSET_WORDS; ++j)
3645 + any_set |= (accepts[j] &= dfa->word_char[j]);
3646 + if (!any_set)
3647 + continue;
3648 + }
3649 + if (constraint & NEXT_NOTWORD_CONSTRAINT)
3650 + {
3651 + bitset_word_t any_set = 0;
3652 + if (type == CHARACTER && node->word_char)
3653 + {
3654 + bitset_empty (accepts);
3655 + continue;
3656 + }
3657 +#ifdef RE_ENABLE_I18N
3658 + if (dfa->mb_cur_max > 1)
3659 + for (j = 0; j < BITSET_WORDS; ++j)
3660 + any_set |= (accepts[j] &= ~(dfa->word_char[j] & dfa->sb_char[j]));
3661 + else
3662 +#endif
3663 + for (j = 0; j < BITSET_WORDS; ++j)
3664 + any_set |= (accepts[j] &= ~dfa->word_char[j]);
3665 + if (!any_set)
3666 + continue;
3667 + }
3668 + }
3669 +
3670 + /* Then divide `accepts' into DFA states, or create a new
3671 + state. Above, we make sure that accepts is not empty. */
3672 + for (j = 0; j < ndests; ++j)
3673 + {
3674 + bitset_t intersec; /* Intersection sets, see below. */
3675 + bitset_t remains;
3676 + /* Flags, see below. */
3677 + bitset_word_t has_intersec, not_subset, not_consumed;
3678 +
3679 + /* Optimization, skip if this state doesn't accept the character. */
3680 + if (type == CHARACTER && !bitset_contain (dests_ch[j], node->opr.c))
3681 + continue;
3682 +
3683 + /* Enumerate the intersection set of this state and `accepts'. */
3684 + has_intersec = 0;
3685 + for (k = 0; k < BITSET_WORDS; ++k)
3686 + has_intersec |= intersec[k] = accepts[k] & dests_ch[j][k];
3687 + /* And skip if the intersection set is empty. */
3688 + if (!has_intersec)
3689 + continue;
3690 +
3691 + /* Then check if this state is a subset of `accepts'. */
3692 + not_subset = not_consumed = 0;
3693 + for (k = 0; k < BITSET_WORDS; ++k)
3694 + {
3695 + not_subset |= remains[k] = ~accepts[k] & dests_ch[j][k];
3696 + not_consumed |= accepts[k] = accepts[k] & ~dests_ch[j][k];
3697 + }
3698 +
3699 + /* If this state isn't a subset of `accepts', create a
3700 + new group state, which has the `remains'. */
3701 + if (not_subset)
3702 + {
3703 + bitset_copy (dests_ch[ndests], remains);
3704 + bitset_copy (dests_ch[j], intersec);
3705 + err = re_node_set_init_copy (dests_node + ndests, &dests_node[j]);
3706 + if (BE (err != REG_NOERROR, 0))
3707 + goto error_return;
3708 + ++ndests;
3709 + }
3710 +
3711 + /* Put the position in the current group. */
3712 + result = re_node_set_insert (&dests_node[j], cur_nodes->elems[i]);
3713 + if (BE (result < 0, 0))
3714 + goto error_return;
3715 +
3716 + /* If all characters are consumed, go to next node. */
3717 + if (!not_consumed)
3718 + break;
3719 + }
3720 + /* Some characters remain, create a new group. */
3721 + if (j == ndests)
3722 + {
3723 + bitset_copy (dests_ch[ndests], accepts);
3724 + err = re_node_set_init_1 (dests_node + ndests, cur_nodes->elems[i]);
3725 + if (BE (err != REG_NOERROR, 0))
3726 + goto error_return;
3727 + ++ndests;
3728 + bitset_empty (accepts);
3729 + }
3730 + }
3731 + return ndests;
3732 + error_return:
3733 + for (j = 0; j < ndests; ++j)
3734 + re_node_set_free (dests_node + j);
3735 + return -1;
3736 +}
3737 +
3738 +#ifdef RE_ENABLE_I18N
3739 +/* Check how many bytes the node `dfa->nodes[node_idx]' accepts.
3740 + Return the number of the bytes the node accepts.
3741 + STR_IDX is the current index of the input string.
3742 +
3743 + This function handles the nodes which can accept one character, or
3744 + one collating element like '.', '[a-z]', opposite to the other nodes
3745 + can only accept one byte. */
3746 +
3747 +static int
3748 +internal_function
3749 +check_node_accept_bytes (const re_dfa_t *dfa, int node_idx,
3750 + const re_string_t *input, int str_idx)
3751 +{
3752 + const re_token_t *node = dfa->nodes + node_idx;
3753 + int char_len, elem_len;
3754 + int i;
3755 + wint_t wc;
3756 +
3757 + if (BE (node->type == OP_UTF8_PERIOD, 0))
3758 + {
3759 + unsigned char c = re_string_byte_at (input, str_idx), d;
3760 + if (BE (c < 0xc2, 1))
3761 + return 0;
3762 +
3763 + if (str_idx + 2 > input->len)
3764 + return 0;
3765 +
3766 + d = re_string_byte_at (input, str_idx + 1);
3767 + if (c < 0xe0)
3768 + return (d < 0x80 || d > 0xbf) ? 0 : 2;
3769 + else if (c < 0xf0)
3770 + {
3771 + char_len = 3;
3772 + if (c == 0xe0 && d < 0xa0)
3773 + return 0;
3774 + }
3775 + else if (c < 0xf8)
3776 + {
3777 + char_len = 4;
3778 + if (c == 0xf0 && d < 0x90)
3779 + return 0;
3780 + }
3781 + else if (c < 0xfc)
3782 + {
3783 + char_len = 5;
3784 + if (c == 0xf8 && d < 0x88)
3785 + return 0;
3786 + }
3787 + else if (c < 0xfe)
3788 + {
3789 + char_len = 6;
3790 + if (c == 0xfc && d < 0x84)
3791 + return 0;
3792 + }
3793 + else
3794 + return 0;
3795 +
3796 + if (str_idx + char_len > input->len)
3797 + return 0;
3798 +
3799 + for (i = 1; i < char_len; ++i)
3800 + {
3801 + d = re_string_byte_at (input, str_idx + i);
3802 + if (d < 0x80 || d > 0xbf)
3803 + return 0;
3804 + }
3805 + return char_len;
3806 + }
3807 +
3808 + char_len = re_string_char_size_at (input, str_idx);
3809 + if (node->type == OP_PERIOD)
3810 + {
3811 + if (char_len <= 1)
3812 + return 0;
3813 + /* FIXME: I don't think this if is needed, as both '\n'
3814 + and '\0' are char_len == 1. */
3815 + /* '.' accepts any one character except the following two cases. */
3816 + if ((!(dfa->syntax & RE_DOT_NEWLINE) &&
3817 + re_string_byte_at (input, str_idx) == '\n') ||
3818 + ((dfa->syntax & RE_DOT_NOT_NULL) &&
3819 + re_string_byte_at (input, str_idx) == '\0'))
3820 + return 0;
3821 + return char_len;
3822 + }
3823 +
3824 + elem_len = re_string_elem_size_at (input, str_idx);
3825 + wc = __btowc(*(input->mbs+str_idx));
3826 + if (((elem_len <= 1 && char_len <= 1) || char_len == 0) && (wc != WEOF && wc < SBC_MAX))
3827 + return 0;
3828 +
3829 + if (node->type == COMPLEX_BRACKET)
3830 + {
3831 + const re_charset_t *cset = node->opr.mbcset;
3832 +# ifdef _LIBC
3833 + const unsigned char *pin
3834 + = ((const unsigned char *) re_string_get_buffer (input) + str_idx);
3835 + int j;
3836 + uint32_t nrules;
3837 +# endif /* _LIBC */
3838 + int match_len = 0;
3839 + wchar_t wc = ((cset->nranges || cset->nchar_classes || cset->nmbchars)
3840 + ? re_string_wchar_at (input, str_idx) : 0);
3841 +
3842 + /* match with multibyte character? */
3843 + for (i = 0; i < cset->nmbchars; ++i)
3844 + if (wc == cset->mbchars[i])
3845 + {
3846 + match_len = char_len;
3847 + goto check_node_accept_bytes_match;
3848 + }
3849 + /* match with character_class? */
3850 + for (i = 0; i < cset->nchar_classes; ++i)
3851 + {
3852 + wctype_t wt = cset->char_classes[i];
3853 + if (__iswctype (wc, wt))
3854 + {
3855 + match_len = char_len;
3856 + goto check_node_accept_bytes_match;
3857 + }
3858 + }
3859 +
3860 +# ifdef _LIBC
3861 + nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
3862 + if (nrules != 0)
3863 + {
3864 + unsigned int in_collseq = 0;
3865 + const int32_t *table, *indirect;
3866 + const unsigned char *weights, *extra;
3867 + const char *collseqwc;
3868 + /* This #include defines a local function! */
3869 +# include <locale/weight.h>
3870 +
3871 + /* match with collating_symbol? */
3872 + if (cset->ncoll_syms)
3873 + extra = (const unsigned char *)
3874 + _NL_CURRENT (LC_COLLATE, _NL_COLLATE_SYMB_EXTRAMB);
3875 + for (i = 0; i < cset->ncoll_syms; ++i)
3876 + {
3877 + const unsigned char *coll_sym = extra + cset->coll_syms[i];
3878 + /* Compare the length of input collating element and
3879 + the length of current collating element. */
3880 + if (*coll_sym != elem_len)
3881 + continue;
3882 + /* Compare each bytes. */
3883 + for (j = 0; j < *coll_sym; j++)
3884 + if (pin[j] != coll_sym[1 + j])
3885 + break;
3886 + if (j == *coll_sym)
3887 + {
3888 + /* Match if every bytes is equal. */
3889 + match_len = j;
3890 + goto check_node_accept_bytes_match;
3891 + }
3892 + }
3893 +
3894 + if (cset->nranges)
3895 + {
3896 + if (elem_len <= char_len)
3897 + {
3898 + collseqwc = _NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQWC);
3899 + in_collseq = __collseq_table_lookup (collseqwc, wc);
3900 + }
3901 + else
3902 + in_collseq = find_collation_sequence_value (pin, elem_len);
3903 + }
3904 + /* match with range expression? */
3905 + for (i = 0; i < cset->nranges; ++i)
3906 + if (cset->range_starts[i] <= in_collseq
3907 + && in_collseq <= cset->range_ends[i])
3908 + {
3909 + match_len = elem_len;
3910 + goto check_node_accept_bytes_match;
3911 + }
3912 +
3913 + /* match with equivalence_class? */
3914 + if (cset->nequiv_classes)
3915 + {
3916 + const unsigned char *cp = pin;
3917 + table = (const int32_t *)
3918 + _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
3919 + weights = (const unsigned char *)
3920 + _NL_CURRENT (LC_COLLATE, _NL_COLLATE_WEIGHTMB);
3921 + extra = (const unsigned char *)
3922 + _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB);
3923 + indirect = (const int32_t *)
3924 + _NL_CURRENT (LC_COLLATE, _NL_COLLATE_INDIRECTMB);
3925 + int32_t idx = findidx (&cp);
3926 + if (idx > 0)
3927 + for (i = 0; i < cset->nequiv_classes; ++i)
3928 + {
3929 + int32_t equiv_class_idx = cset->equiv_classes[i];
3930 + size_t weight_len = weights[idx & 0xffffff];
3931 + if (weight_len == weights[equiv_class_idx & 0xffffff]
3932 + && (idx >> 24) == (equiv_class_idx >> 24))
3933 + {
3934 + int cnt = 0;
3935 +
3936 + idx &= 0xffffff;
3937 + equiv_class_idx &= 0xffffff;
3938 +
3939 + while (cnt <= weight_len
3940 + && (weights[equiv_class_idx + 1 + cnt]
3941 + == weights[idx + 1 + cnt]))
3942 + ++cnt;
3943 + if (cnt > weight_len)
3944 + {
3945 + match_len = elem_len;
3946 + goto check_node_accept_bytes_match;
3947 + }
3948 + }
3949 + }
3950 + }
3951 + }
3952 + else
3953 +# endif /* _LIBC */
3954 + {
3955 + /* match with range expression? */
3956 +#if __GNUC__ >= 2
3957 + wchar_t cmp_buf[] = {L'\0', L'\0', wc, L'\0', L'\0', L'\0'};
3958 +#else
3959 + wchar_t cmp_buf[] = {L'\0', L'\0', L'\0', L'\0', L'\0', L'\0'};
3960 + cmp_buf[2] = wc;
3961 +#endif
3962 + for (i = 0; i < cset->nranges; ++i)
3963 + {
3964 + cmp_buf[0] = cset->range_starts[i];
3965 + cmp_buf[4] = cset->range_ends[i];
3966 + if (wcscoll (cmp_buf, cmp_buf + 2) <= 0
3967 + && wcscoll (cmp_buf + 2, cmp_buf + 4) <= 0)
3968 + {
3969 + match_len = char_len;
3970 + goto check_node_accept_bytes_match;
3971 + }
3972 + }
3973 + }
3974 + check_node_accept_bytes_match:
3975 + if (!cset->non_match)
3976 + return match_len;
3977 + else
3978 + {
3979 + if (match_len > 0)
3980 + return 0;
3981 + else
3982 + return (elem_len > char_len) ? elem_len : char_len;
3983 + }
3984 + }
3985 + return 0;
3986 +}
3987 +
3988 +# ifdef _LIBC
3989 +static unsigned int
3990 +internal_function
3991 +find_collation_sequence_value (const unsigned char *mbs, size_t mbs_len)
3992 +{
3993 + uint32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
3994 + if (nrules == 0)
3995 + {
3996 + if (mbs_len == 1)
3997 + {
3998 + /* No valid character. Match it as a single byte character. */
3999 + const unsigned char *collseq = (const unsigned char *)
4000 + _NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQMB);
4001 + return collseq[mbs[0]];
4002 + }
4003 + return UINT_MAX;
4004 + }
4005 + else
4006 + {
4007 + int32_t idx;
4008 + const unsigned char *extra = (const unsigned char *)
4009 + _NL_CURRENT (LC_COLLATE, _NL_COLLATE_SYMB_EXTRAMB);
4010 + int32_t extrasize = (const unsigned char *)
4011 + _NL_CURRENT (LC_COLLATE, _NL_COLLATE_SYMB_EXTRAMB + 1) - extra;
4012 +
4013 + for (idx = 0; idx < extrasize;)
4014 + {
4015 + int mbs_cnt, found = 0;
4016 + int32_t elem_mbs_len;
4017 + /* Skip the name of collating element name. */
4018 + idx = idx + extra[idx] + 1;
4019 + elem_mbs_len = extra[idx++];
4020 + if (mbs_len == elem_mbs_len)
4021 + {
4022 + for (mbs_cnt = 0; mbs_cnt < elem_mbs_len; ++mbs_cnt)
4023 + if (extra[idx + mbs_cnt] != mbs[mbs_cnt])
4024 + break;
4025 + if (mbs_cnt == elem_mbs_len)
4026 + /* Found the entry. */
4027 + found = 1;
4028 + }
4029 + /* Skip the byte sequence of the collating element. */
4030 + idx += elem_mbs_len;
4031 + /* Adjust for the alignment. */
4032 + idx = (idx + 3) & ~3;
4033 + /* Skip the collation sequence value. */
4034 + idx += sizeof (uint32_t);
4035 + /* Skip the wide char sequence of the collating element. */
4036 + idx = idx + sizeof (uint32_t) * (extra[idx] + 1);
4037 + /* If we found the entry, return the sequence value. */
4038 + if (found)
4039 + return *(uint32_t *) (extra + idx);
4040 + /* Skip the collation sequence value. */
4041 + idx += sizeof (uint32_t);
4042 + }
4043 + return UINT_MAX;
4044 + }
4045 +}
4046 +# endif /* _LIBC */
4047 +#endif /* RE_ENABLE_I18N */
4048 +
4049 +/* Check whether the node accepts the byte which is IDX-th
4050 + byte of the INPUT. */
4051 +
4052 +static int
4053 +internal_function
4054 +check_node_accept (const re_match_context_t *mctx, const re_token_t *node,
4055 + int idx)
4056 +{
4057 + unsigned char ch;
4058 + ch = re_string_byte_at (&mctx->input, idx);
4059 + switch (node->type)
4060 + {
4061 + case CHARACTER:
4062 + if (node->opr.c != ch)
4063 + return 0;
4064 + break;
4065 +
4066 + case SIMPLE_BRACKET:
4067 + if (!bitset_contain (node->opr.sbcset, ch))
4068 + return 0;
4069 + break;
4070 +
4071 +#ifdef RE_ENABLE_I18N
4072 + case OP_UTF8_PERIOD:
4073 + if (ch >= 0x80)
4074 + return 0;
4075 + /* FALLTHROUGH */
4076 +#endif
4077 + case OP_PERIOD:
4078 + if ((ch == '\n' && !(mctx->dfa->syntax & RE_DOT_NEWLINE))
4079 + || (ch == '\0' && (mctx->dfa->syntax & RE_DOT_NOT_NULL)))
4080 + return 0;
4081 + break;
4082 +
4083 + default:
4084 + return 0;
4085 + }
4086 +
4087 + if (node->constraint)
4088 + {
4089 + /* The node has constraints. Check whether the current context
4090 + satisfies the constraints. */
4091 + unsigned int context = re_string_context_at (&mctx->input, idx,
4092 + mctx->eflags);
4093 + if (NOT_SATISFY_NEXT_CONSTRAINT (node->constraint, context))
4094 + return 0;
4095 + }
4096 +
4097 + return 1;
4098 +}
4099 +
4100 +/* Extend the buffers, if the buffers have run out. */
4101 +
4102 +static reg_errcode_t
4103 +internal_function
4104 +extend_buffers (re_match_context_t *mctx)
4105 +{
4106 + reg_errcode_t ret;
4107 + re_string_t *pstr = &mctx->input;
4108 +
4109 + /* Avoid overflow. */
4110 + if (BE (INT_MAX / 2 / sizeof (re_dfastate_t *) <= pstr->bufs_len, 0))
4111 + return REG_ESPACE;
4112 +
4113 + /* Double the lengthes of the buffers. */
4114 + ret = re_string_realloc_buffers (pstr, pstr->bufs_len * 2);
4115 + if (BE (ret != REG_NOERROR, 0))
4116 + return ret;
4117 +
4118 + if (mctx->state_log != NULL)
4119 + {
4120 + /* And double the length of state_log. */
4121 + /* XXX We have no indication of the size of this buffer. If this
4122 + allocation fail we have no indication that the state_log array
4123 + does not have the right size. */
4124 + re_dfastate_t **new_array = re_realloc (mctx->state_log, re_dfastate_t *,
4125 + pstr->bufs_len + 1);
4126 + if (BE (new_array == NULL, 0))
4127 + return REG_ESPACE;
4128 + mctx->state_log = new_array;
4129 + }
4130 +
4131 + /* Then reconstruct the buffers. */
4132 + if (pstr->icase)
4133 + {
4134 +#ifdef RE_ENABLE_I18N
4135 + if (pstr->mb_cur_max > 1)
4136 + {
4137 + ret = build_wcs_upper_buffer (pstr);
4138 + if (BE (ret != REG_NOERROR, 0))
4139 + return ret;
4140 + }
4141 + else
4142 +#endif /* RE_ENABLE_I18N */
4143 + build_upper_buffer (pstr);
4144 + }
4145 + else
4146 + {
4147 +#ifdef RE_ENABLE_I18N
4148 + if (pstr->mb_cur_max > 1)
4149 + build_wcs_buffer (pstr);
4150 + else
4151 +#endif /* RE_ENABLE_I18N */
4152 + {
4153 + if (pstr->trans != NULL)
4154 + re_string_translate_buffer (pstr);
4155 + }
4156 + }
4157 + return REG_NOERROR;
4158 +}
4159 +
4160 +
4161 +/* Functions for matching context. */
4162 +
4163 +/* Initialize MCTX. */
4164 +
4165 +static reg_errcode_t
4166 +internal_function
4167 +match_ctx_init (re_match_context_t *mctx, int eflags, int n)
4168 +{
4169 + mctx->eflags = eflags;
4170 + mctx->match_last = -1;
4171 + if (n > 0)
4172 + {
4173 + mctx->bkref_ents = re_malloc (struct re_backref_cache_entry, n);
4174 + mctx->sub_tops = re_malloc (re_sub_match_top_t *, n);
4175 + if (BE (mctx->bkref_ents == NULL || mctx->sub_tops == NULL, 0))
4176 + return REG_ESPACE;
4177 + }
4178 + /* Already zero-ed by the caller.
4179 + else
4180 + mctx->bkref_ents = NULL;
4181 + mctx->nbkref_ents = 0;
4182 + mctx->nsub_tops = 0; */
4183 + mctx->abkref_ents = n;
4184 + mctx->max_mb_elem_len = 1;
4185 + mctx->asub_tops = n;
4186 + return REG_NOERROR;
4187 +}
4188 +
4189 +/* Clean the entries which depend on the current input in MCTX.
4190 + This function must be invoked when the matcher changes the start index
4191 + of the input, or changes the input string. */
4192 +
4193 +static void
4194 +internal_function
4195 +match_ctx_clean (re_match_context_t *mctx)
4196 +{
4197 + int st_idx;
4198 + for (st_idx = 0; st_idx < mctx->nsub_tops; ++st_idx)
4199 + {
4200 + int sl_idx;
4201 + re_sub_match_top_t *top = mctx->sub_tops[st_idx];
4202 + for (sl_idx = 0; sl_idx < top->nlasts; ++sl_idx)
4203 + {
4204 + re_sub_match_last_t *last = top->lasts[sl_idx];
4205 + re_free (last->path.array);
4206 + re_free (last);
4207 + }
4208 + re_free (top->lasts);
4209 + if (top->path)
4210 + {
4211 + re_free (top->path->array);
4212 + re_free (top->path);
4213 + }
4214 + free (top);
4215 + }
4216 +
4217 + mctx->nsub_tops = 0;
4218 + mctx->nbkref_ents = 0;
4219 +}
4220 +
4221 +/* Free all the memory associated with MCTX. */
4222 +
4223 +static void
4224 +internal_function
4225 +match_ctx_free (re_match_context_t *mctx)
4226 +{
4227 + /* First, free all the memory associated with MCTX->SUB_TOPS. */
4228 + match_ctx_clean (mctx);
4229 + re_free (mctx->sub_tops);
4230 + re_free (mctx->bkref_ents);
4231 +}
4232 +
4233 +/* Add a new backreference entry to MCTX.
4234 + Note that we assume that caller never call this function with duplicate
4235 + entry, and call with STR_IDX which isn't smaller than any existing entry.
4236 +*/
4237 +
4238 +static reg_errcode_t
4239 +internal_function
4240 +match_ctx_add_entry (re_match_context_t *mctx, int node, int str_idx, int from,
4241 + int to)
4242 +{
4243 + if (mctx->nbkref_ents >= mctx->abkref_ents)
4244 + {
4245 + struct re_backref_cache_entry* new_entry;
4246 + new_entry = re_realloc (mctx->bkref_ents, struct re_backref_cache_entry,
4247 + mctx->abkref_ents * 2);
4248 + if (BE (new_entry == NULL, 0))
4249 + {
4250 + re_free (mctx->bkref_ents);
4251 + return REG_ESPACE;
4252 + }
4253 + mctx->bkref_ents = new_entry;
4254 + memset (mctx->bkref_ents + mctx->nbkref_ents, '\0',
4255 + sizeof (struct re_backref_cache_entry) * mctx->abkref_ents);
4256 + mctx->abkref_ents *= 2;
4257 + }
4258 + if (mctx->nbkref_ents > 0
4259 + && mctx->bkref_ents[mctx->nbkref_ents - 1].str_idx == str_idx)
4260 + mctx->bkref_ents[mctx->nbkref_ents - 1].more = 1;
4261 +
4262 + mctx->bkref_ents[mctx->nbkref_ents].node = node;
4263 + mctx->bkref_ents[mctx->nbkref_ents].str_idx = str_idx;
4264 + mctx->bkref_ents[mctx->nbkref_ents].subexp_from = from;
4265 + mctx->bkref_ents[mctx->nbkref_ents].subexp_to = to;
4266 +
4267 + /* This is a cache that saves negative results of check_dst_limits_calc_pos.
4268 + If bit N is clear, means that this entry won't epsilon-transition to
4269 + an OP_OPEN_SUBEXP or OP_CLOSE_SUBEXP for the N+1-th subexpression. If
4270 + it is set, check_dst_limits_calc_pos_1 will recurse and try to find one
4271 + such node.
4272 +
4273 + A backreference does not epsilon-transition unless it is empty, so set
4274 + to all zeros if FROM != TO. */
4275 + mctx->bkref_ents[mctx->nbkref_ents].eps_reachable_subexps_map
4276 + = (from == to ? ~0 : 0);
4277 +
4278 + mctx->bkref_ents[mctx->nbkref_ents++].more = 0;
4279 + if (mctx->max_mb_elem_len < to - from)
4280 + mctx->max_mb_elem_len = to - from;
4281 + return REG_NOERROR;
4282 +}
4283 +
4284 +/* Search for the first entry which has the same str_idx, or -1 if none is
4285 + found. Note that MCTX->BKREF_ENTS is already sorted by MCTX->STR_IDX. */
4286 +
4287 +static int
4288 +internal_function
4289 +search_cur_bkref_entry (const re_match_context_t *mctx, int str_idx)
4290 +{
4291 + int left, right, mid, last;
4292 + last = right = mctx->nbkref_ents;
4293 + for (left = 0; left < right;)
4294 + {
4295 + mid = (left + right) / 2;
4296 + if (mctx->bkref_ents[mid].str_idx < str_idx)
4297 + left = mid + 1;
4298 + else
4299 + right = mid;
4300 + }
4301 + if (left < last && mctx->bkref_ents[left].str_idx == str_idx)
4302 + return left;
4303 + else
4304 + return -1;
4305 +}
4306 +
4307 +/* Register the node NODE, whose type is OP_OPEN_SUBEXP, and which matches
4308 + at STR_IDX. */
4309 +
4310 +static reg_errcode_t
4311 +internal_function
4312 +match_ctx_add_subtop (re_match_context_t *mctx, int node, int str_idx)
4313 +{
4314 +#ifdef DEBUG
4315 + assert (mctx->sub_tops != NULL);
4316 + assert (mctx->asub_tops > 0);
4317 +#endif
4318 + if (BE (mctx->nsub_tops == mctx->asub_tops, 0))
4319 + {
4320 + int new_asub_tops = mctx->asub_tops * 2;
4321 + re_sub_match_top_t **new_array = re_realloc (mctx->sub_tops,
4322 + re_sub_match_top_t *,
4323 + new_asub_tops);
4324 + if (BE (new_array == NULL, 0))
4325 + return REG_ESPACE;
4326 + mctx->sub_tops = new_array;
4327 + mctx->asub_tops = new_asub_tops;
4328 + }
4329 + mctx->sub_tops[mctx->nsub_tops] = calloc (1, sizeof (re_sub_match_top_t));
4330 + if (BE (mctx->sub_tops[mctx->nsub_tops] == NULL, 0))
4331 + return REG_ESPACE;
4332 + mctx->sub_tops[mctx->nsub_tops]->node = node;
4333 + mctx->sub_tops[mctx->nsub_tops++]->str_idx = str_idx;
4334 + return REG_NOERROR;
4335 +}
4336 +
4337 +/* Register the node NODE, whose type is OP_CLOSE_SUBEXP, and which matches
4338 + at STR_IDX, whose corresponding OP_OPEN_SUBEXP is SUB_TOP. */
4339 +
4340 +static re_sub_match_last_t *
4341 +internal_function
4342 +match_ctx_add_sublast (re_sub_match_top_t *subtop, int node, int str_idx)
4343 +{
4344 + re_sub_match_last_t *new_entry;
4345 + if (BE (subtop->nlasts == subtop->alasts, 0))
4346 + {
4347 + int new_alasts = 2 * subtop->alasts + 1;
4348 + re_sub_match_last_t **new_array = re_realloc (subtop->lasts,
4349 + re_sub_match_last_t *,
4350 + new_alasts);
4351 + if (BE (new_array == NULL, 0))
4352 + return NULL;
4353 + subtop->lasts = new_array;
4354 + subtop->alasts = new_alasts;
4355 + }
4356 + new_entry = calloc (1, sizeof (re_sub_match_last_t));
4357 + if (BE (new_entry != NULL, 1))
4358 + {
4359 + subtop->lasts[subtop->nlasts] = new_entry;
4360 + new_entry->node = node;
4361 + new_entry->str_idx = str_idx;
4362 + ++subtop->nlasts;
4363 + }
4364 + return new_entry;
4365 +}
4366 +
4367 +static void
4368 +internal_function
4369 +sift_ctx_init (re_sift_context_t *sctx, re_dfastate_t **sifted_sts,
4370 + re_dfastate_t **limited_sts, int last_node, int last_str_idx)
4371 +{
4372 + sctx->sifted_states = sifted_sts;
4373 + sctx->limited_states = limited_sts;
4374 + sctx->last_node = last_node;
4375 + sctx->last_str_idx = last_str_idx;
4376 + re_node_set_init_empty (&sctx->limits);
4377 +}