847f385eec430e507e56ac4badbd2915fdd1c69e
[pspp] / lib / regcomp.c
1 /* Extended regular expression matching and search library.
2    Copyright (C) 2002, 2003, 2004, 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    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    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 along
17    with this program; if not, write to the Free Software Foundation,
18    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19
20 #ifndef __GNUC_PREREQ
21 # if defined __GNUC__ && defined __GNUC_MINOR__
22 #  define __GNUC_PREREQ(maj, min) \
23         ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
24 # else
25 #  define __GNUC_PREREQ(maj, min) 0
26 # endif
27 #endif
28
29 #if !__GNUC_PREREQ (3, 1)
30 # define always_inline
31 #endif
32
33 static reg_errcode_t re_compile_internal (regex_t *preg, const char * pattern,
34                                           Idx length, reg_syntax_t syntax);
35 static void re_compile_fastmap_iter (regex_t *bufp,
36                                      const re_dfastate_t *init_state,
37                                      char *fastmap);
38 static reg_errcode_t init_dfa (re_dfa_t *dfa, Idx pat_len);
39 #ifdef RE_ENABLE_I18N
40 static void free_charset (re_charset_t *cset);
41 #endif /* RE_ENABLE_I18N */
42 static void free_workarea_compile (regex_t *preg);
43 static reg_errcode_t create_initial_state (re_dfa_t *dfa);
44 #ifdef RE_ENABLE_I18N
45 static void optimize_utf8 (re_dfa_t *dfa);
46 #endif
47 static reg_errcode_t analyze (regex_t *preg);
48 static reg_errcode_t preorder (bin_tree_t *root,
49                                reg_errcode_t (fn (void *, bin_tree_t *)),
50                                void *extra);
51 static reg_errcode_t postorder (bin_tree_t *root,
52                                 reg_errcode_t (fn (void *, bin_tree_t *)),
53                                 void *extra);
54 static reg_errcode_t optimize_subexps (void *extra, bin_tree_t *node);
55 static reg_errcode_t lower_subexps (void *extra, bin_tree_t *node);
56 static bin_tree_t *lower_subexp (reg_errcode_t *err, regex_t *preg,
57                                  bin_tree_t *node);
58 static reg_errcode_t calc_first (void *extra, bin_tree_t *node);
59 static reg_errcode_t calc_next (void *extra, bin_tree_t *node);
60 static reg_errcode_t link_nfa_nodes (void *extra, bin_tree_t *node);
61 static Idx duplicate_node (re_dfa_t *dfa, Idx org_idx, unsigned int constraint);
62 static Idx search_duplicated_node (const re_dfa_t *dfa, Idx org_node,
63                                    unsigned int constraint);
64 static reg_errcode_t calc_eclosure (re_dfa_t *dfa);
65 static reg_errcode_t calc_eclosure_iter (re_node_set *new_set, re_dfa_t *dfa,
66                                          Idx node, bool root);
67 static reg_errcode_t calc_inveclosure (re_dfa_t *dfa);
68 static Idx fetch_number (re_string_t *input, re_token_t *token,
69                          reg_syntax_t syntax);
70 static int peek_token (re_token_t *token, re_string_t *input,
71                         reg_syntax_t syntax);
72 static bin_tree_t *parse (re_string_t *regexp, regex_t *preg,
73                           reg_syntax_t syntax, reg_errcode_t *err);
74 static bin_tree_t *parse_reg_exp (re_string_t *regexp, regex_t *preg,
75                                   re_token_t *token, reg_syntax_t syntax,
76                                   Idx nest, reg_errcode_t *err);
77 static bin_tree_t *parse_branch (re_string_t *regexp, regex_t *preg,
78                                  re_token_t *token, reg_syntax_t syntax,
79                                  Idx nest, reg_errcode_t *err);
80 static bin_tree_t *parse_expression (re_string_t *regexp, regex_t *preg,
81                                      re_token_t *token, reg_syntax_t syntax,
82                                      Idx nest, reg_errcode_t *err);
83 static bin_tree_t *parse_sub_exp (re_string_t *regexp, regex_t *preg,
84                                   re_token_t *token, reg_syntax_t syntax,
85                                   Idx nest, reg_errcode_t *err);
86 static bin_tree_t *parse_dup_op (bin_tree_t *dup_elem, re_string_t *regexp,
87                                  re_dfa_t *dfa, re_token_t *token,
88                                  reg_syntax_t syntax, reg_errcode_t *err);
89 static bin_tree_t *parse_bracket_exp (re_string_t *regexp, re_dfa_t *dfa,
90                                       re_token_t *token, reg_syntax_t syntax,
91                                       reg_errcode_t *err);
92 static reg_errcode_t parse_bracket_element (bracket_elem_t *elem,
93                                             re_string_t *regexp,
94                                             re_token_t *token, int token_len,
95                                             re_dfa_t *dfa,
96                                             reg_syntax_t syntax,
97                                             bool accept_hyphen);
98 static reg_errcode_t parse_bracket_symbol (bracket_elem_t *elem,
99                                           re_string_t *regexp,
100                                           re_token_t *token);
101 #ifdef RE_ENABLE_I18N
102 static reg_errcode_t build_equiv_class (bitset sbcset,
103                                         re_charset_t *mbcset,
104                                         Idx *equiv_class_alloc,
105                                         const unsigned char *name);
106 static reg_errcode_t build_charclass (unsigned REG_TRANSLATE_TYPE trans,
107                                       bitset sbcset,
108                                       re_charset_t *mbcset,
109                                       Idx *char_class_alloc,
110                                       const unsigned char *class_name,
111                                       reg_syntax_t syntax);
112 #else  /* not RE_ENABLE_I18N */
113 static reg_errcode_t build_equiv_class (bitset sbcset,
114                                         const unsigned char *name);
115 static reg_errcode_t build_charclass (unsigned REG_TRANSLATE_TYPE trans,
116                                       bitset sbcset,
117                                       const unsigned char *class_name,
118                                       reg_syntax_t syntax);
119 #endif /* not RE_ENABLE_I18N */
120 static bin_tree_t *build_charclass_op (re_dfa_t *dfa,
121                                        unsigned REG_TRANSLATE_TYPE trans,
122                                        const unsigned char *class_name,
123                                        const unsigned char *extra,
124                                        bool non_match, reg_errcode_t *err);
125 static bin_tree_t *create_tree (re_dfa_t *dfa,
126                                 bin_tree_t *left, bin_tree_t *right,
127                                 re_token_type_t type);
128 static bin_tree_t *create_token_tree (re_dfa_t *dfa,
129                                       bin_tree_t *left, bin_tree_t *right,
130                                       const re_token_t *token);
131 static bin_tree_t *duplicate_tree (const bin_tree_t *src, re_dfa_t *dfa);
132 static void free_token (re_token_t *node);
133 static reg_errcode_t free_tree (void *extra, bin_tree_t *node);
134 static reg_errcode_t mark_opt_subexp (void *extra, bin_tree_t *node);
135 \f
136 /* This table gives an error message for each of the error codes listed
137    in regex.h.  Obviously the order here has to be same as there.
138    POSIX doesn't require that we do anything for REG_NOERROR,
139    but why not be nice?  */
140
141 const char __re_error_msgid[] attribute_hidden =
142   {
143 #define REG_NOERROR_IDX 0
144     gettext_noop ("Success")    /* REG_NOERROR */
145     "\0"
146 #define REG_NOMATCH_IDX (REG_NOERROR_IDX + sizeof "Success")
147     gettext_noop ("No match")   /* REG_NOMATCH */
148     "\0"
149 #define REG_BADPAT_IDX  (REG_NOMATCH_IDX + sizeof "No match")
150     gettext_noop ("Invalid regular expression") /* REG_BADPAT */
151     "\0"
152 #define REG_ECOLLATE_IDX (REG_BADPAT_IDX + sizeof "Invalid regular expression")
153     gettext_noop ("Invalid collation character") /* REG_ECOLLATE */
154     "\0"
155 #define REG_ECTYPE_IDX  (REG_ECOLLATE_IDX + sizeof "Invalid collation character")
156     gettext_noop ("Invalid character class name") /* REG_ECTYPE */
157     "\0"
158 #define REG_EESCAPE_IDX (REG_ECTYPE_IDX + sizeof "Invalid character class name")
159     gettext_noop ("Trailing backslash") /* REG_EESCAPE */
160     "\0"
161 #define REG_ESUBREG_IDX (REG_EESCAPE_IDX + sizeof "Trailing backslash")
162     gettext_noop ("Invalid back reference") /* REG_ESUBREG */
163     "\0"
164 #define REG_EBRACK_IDX  (REG_ESUBREG_IDX + sizeof "Invalid back reference")
165     gettext_noop ("Unmatched [ or [^")  /* REG_EBRACK */
166     "\0"
167 #define REG_EPAREN_IDX  (REG_EBRACK_IDX + sizeof "Unmatched [ or [^")
168     gettext_noop ("Unmatched ( or \\(") /* REG_EPAREN */
169     "\0"
170 #define REG_EBRACE_IDX  (REG_EPAREN_IDX + sizeof "Unmatched ( or \\(")
171     gettext_noop ("Unmatched \\{") /* REG_EBRACE */
172     "\0"
173 #define REG_BADBR_IDX   (REG_EBRACE_IDX + sizeof "Unmatched \\{")
174     gettext_noop ("Invalid content of \\{\\}") /* REG_BADBR */
175     "\0"
176 #define REG_ERANGE_IDX  (REG_BADBR_IDX + sizeof "Invalid content of \\{\\}")
177     gettext_noop ("Invalid range end")  /* REG_ERANGE */
178     "\0"
179 #define REG_ESPACE_IDX  (REG_ERANGE_IDX + sizeof "Invalid range end")
180     gettext_noop ("Memory exhausted") /* REG_ESPACE */
181     "\0"
182 #define REG_BADRPT_IDX  (REG_ESPACE_IDX + sizeof "Memory exhausted")
183     gettext_noop ("Invalid preceding regular expression") /* REG_BADRPT */
184     "\0"
185 #define REG_EEND_IDX    (REG_BADRPT_IDX + sizeof "Invalid preceding regular expression")
186     gettext_noop ("Premature end of regular expression") /* REG_EEND */
187     "\0"
188 #define REG_ESIZE_IDX   (REG_EEND_IDX + sizeof "Premature end of regular expression")
189     gettext_noop ("Regular expression too big") /* REG_ESIZE */
190     "\0"
191 #define REG_ERPAREN_IDX (REG_ESIZE_IDX + sizeof "Regular expression too big")
192     gettext_noop ("Unmatched ) or \\)") /* REG_ERPAREN */
193   };
194
195 const size_t __re_error_msgid_idx[] attribute_hidden =
196   {
197     REG_NOERROR_IDX,
198     REG_NOMATCH_IDX,
199     REG_BADPAT_IDX,
200     REG_ECOLLATE_IDX,
201     REG_ECTYPE_IDX,
202     REG_EESCAPE_IDX,
203     REG_ESUBREG_IDX,
204     REG_EBRACK_IDX,
205     REG_EPAREN_IDX,
206     REG_EBRACE_IDX,
207     REG_BADBR_IDX,
208     REG_ERANGE_IDX,
209     REG_ESPACE_IDX,
210     REG_BADRPT_IDX,
211     REG_EEND_IDX,
212     REG_ESIZE_IDX,
213     REG_ERPAREN_IDX
214   };
215 \f
216 /* Entry points for GNU code.  */
217
218 /* re_compile_pattern is the GNU regular expression compiler: it
219    compiles PATTERN (of length LENGTH) and puts the result in BUFP.
220    Returns 0 if the pattern was valid, otherwise an error string.
221
222    Assumes the `re_allocated' (and perhaps `re_buffer') and `translate' fields
223    are set in BUFP on entry.  */
224
225 const char *
226 re_compile_pattern (const char *pattern, size_t length,
227                     struct re_pattern_buffer *bufp)
228 {
229   reg_errcode_t ret;
230
231   /* And GNU code determines whether or not to get register information
232      by passing null for the REGS argument to re_match, etc., not by
233      setting re_no_sub, unless REG_NO_SUB is set.  */
234   bufp->re_no_sub = !!(re_syntax_options & REG_NO_SUB);
235
236   /* Match anchors at newline.  */
237   bufp->re_newline_anchor = 1;
238
239   ret = re_compile_internal (bufp, pattern, length, re_syntax_options);
240
241   if (!ret)
242     return NULL;
243   return gettext (__re_error_msgid + __re_error_msgid_idx[(int) ret]);
244 }
245 #ifdef _LIBC
246 weak_alias (__re_compile_pattern, re_compile_pattern)
247 #endif
248
249 /* Set by `re_set_syntax' to the current regexp syntax to recognize.  Can
250    also be assigned to arbitrarily: each pattern buffer stores its own
251    syntax, so it can be changed between regex compilations.  */
252 /* This has no initializer because initialized variables in Emacs
253    become read-only after dumping.  */
254 reg_syntax_t re_syntax_options;
255
256
257 /* Specify the precise syntax of regexps for compilation.  This provides
258    for compatibility for various utilities which historically have
259    different, incompatible syntaxes.
260
261    The argument SYNTAX is a bit mask comprised of the various bits
262    defined in regex.h.  We return the old syntax.  */
263
264 reg_syntax_t
265 re_set_syntax (reg_syntax_t syntax)
266 {
267   reg_syntax_t ret = re_syntax_options;
268
269   re_syntax_options = syntax;
270   return ret;
271 }
272 #ifdef _LIBC
273 weak_alias (__re_set_syntax, re_set_syntax)
274 #endif
275
276 int
277 re_compile_fastmap (struct re_pattern_buffer *bufp)
278 {
279   re_dfa_t *dfa = (re_dfa_t *) bufp->re_buffer;
280   char *fastmap = bufp->re_fastmap;
281
282   memset (fastmap, '\0', sizeof (char) * SBC_MAX);
283   re_compile_fastmap_iter (bufp, dfa->init_state, fastmap);
284   if (dfa->init_state != dfa->init_state_word)
285     re_compile_fastmap_iter (bufp, dfa->init_state_word, fastmap);
286   if (dfa->init_state != dfa->init_state_nl)
287     re_compile_fastmap_iter (bufp, dfa->init_state_nl, fastmap);
288   if (dfa->init_state != dfa->init_state_begbuf)
289     re_compile_fastmap_iter (bufp, dfa->init_state_begbuf, fastmap);
290   bufp->re_fastmap_accurate = 1;
291   return 0;
292 }
293 #ifdef _LIBC
294 weak_alias (__re_compile_fastmap, re_compile_fastmap)
295 #endif
296
297 static inline void
298 __attribute ((always_inline))
299 re_set_fastmap (char *fastmap, bool icase, int ch)
300 {
301   fastmap[ch] = 1;
302   if (icase)
303     fastmap[tolower (ch)] = 1;
304 }
305
306 /* Helper function for re_compile_fastmap.
307    Compile fastmap for the initial_state INIT_STATE.  */
308
309 static void
310 re_compile_fastmap_iter (regex_t *bufp, const re_dfastate_t *init_state,
311                          char *fastmap)
312 {
313   re_dfa_t *dfa = (re_dfa_t *) bufp->re_buffer;
314   Idx node_cnt;
315   bool icase = (dfa->mb_cur_max == 1 && (bufp->re_syntax & REG_IGNORE_CASE));
316   for (node_cnt = 0; node_cnt < init_state->nodes.nelem; ++node_cnt)
317     {
318       Idx node = init_state->nodes.elems[node_cnt];
319       re_token_type_t type = dfa->nodes[node].type;
320
321       if (type == CHARACTER)
322         {
323           re_set_fastmap (fastmap, icase, dfa->nodes[node].opr.c);
324 #ifdef RE_ENABLE_I18N
325           if ((bufp->re_syntax & REG_IGNORE_CASE) && dfa->mb_cur_max > 1)
326             {
327               unsigned char buf[MB_LEN_MAX];
328               unsigned char *p;
329               wchar_t wc;
330               mbstate_t state;
331
332               p = buf;
333               *p++ = dfa->nodes[node].opr.c;
334               while (++node < dfa->nodes_len
335                      && dfa->nodes[node].type == CHARACTER
336                      && dfa->nodes[node].mb_partial)
337                 *p++ = dfa->nodes[node].opr.c;
338               memset (&state, 0, sizeof (state));
339               if (mbrtowc (&wc, (const char *) buf, p - buf,
340                            &state) == p - buf
341                   && (__wcrtomb ((char *) buf, towlower (wc), &state)
342                       != (size_t) -1))
343                 re_set_fastmap (fastmap, false, buf[0]);
344             }
345 #endif
346         }
347       else if (type == SIMPLE_BRACKET)
348         {
349           int i, j, ch;
350           for (i = 0, ch = 0; i < BITSET_WORDS; ++i)
351             for (j = 0; j < BITSET_WORD_BITS; ++j, ++ch)
352               if (dfa->nodes[node].opr.sbcset[i] & ((bitset_word) 1 << j))
353                 re_set_fastmap (fastmap, icase, ch);
354         }
355 #ifdef RE_ENABLE_I18N
356       else if (type == COMPLEX_BRACKET)
357         {
358           Idx i;
359           re_charset_t *cset = dfa->nodes[node].opr.mbcset;
360           if (cset->non_match || cset->ncoll_syms || cset->nequiv_classes
361               || cset->nranges || cset->nchar_classes)
362             {
363 # ifdef _LIBC
364               if (_NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES) != 0)
365                 {
366                   /* In this case we want to catch the bytes which are
367                      the first byte of any collation elements.
368                      e.g. In da_DK, we want to catch 'a' since "aa"
369                           is a valid collation element, and don't catch
370                           'b' since 'b' is the only collation element
371                           which starts from 'b'.  */
372                   const int32_t *table = (const int32_t *)
373                     _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
374                   for (i = 0; i < SBC_MAX; ++i)
375                     if (table[i] < 0)
376                       re_set_fastmap (fastmap, icase, i);
377                 }
378 # else
379               if (dfa->mb_cur_max > 1)
380                 for (i = 0; i < SBC_MAX; ++i)
381                   if (__btowc (i) == WEOF)
382                     re_set_fastmap (fastmap, icase, i);
383 # endif /* not _LIBC */
384             }
385           for (i = 0; i < cset->nmbchars; ++i)
386             {
387               char buf[256];
388               mbstate_t state;
389               memset (&state, '\0', sizeof (state));
390               if (__wcrtomb (buf, cset->mbchars[i], &state) != (size_t) -1)
391                 re_set_fastmap (fastmap, icase, *(unsigned char *) buf);
392               if ((bufp->re_syntax & REG_IGNORE_CASE) && dfa->mb_cur_max > 1)
393                 {
394                   if (__wcrtomb (buf, towlower (cset->mbchars[i]), &state)
395                       != (size_t) -1)
396                     re_set_fastmap (fastmap, false, *(unsigned char *) buf);
397                 }
398             }
399         }
400 #endif /* RE_ENABLE_I18N */
401       else if (type == OP_PERIOD
402 #ifdef RE_ENABLE_I18N
403                || type == OP_UTF8_PERIOD
404 #endif /* RE_ENABLE_I18N */
405                || type == END_OF_RE)
406         {
407           memset (fastmap, '\1', sizeof (char) * SBC_MAX);
408           if (type == END_OF_RE)
409             bufp->re_can_be_null = 1;
410           return;
411         }
412     }
413 }
414 \f
415 /* Entry point for POSIX code.  */
416 /* regcomp takes a regular expression as a string and compiles it.
417
418    PREG is a regex_t *.  We do not expect any fields to be initialized,
419    since POSIX says we shouldn't.  Thus, we set
420
421      `re_buffer' to the compiled pattern;
422      `re_used' to the length of the compiled pattern;
423      `re_syntax' to REG_SYNTAX_POSIX_EXTENDED if the
424        REG_EXTENDED bit in CFLAGS is set; otherwise, to
425        REG_SYNTAX_POSIX_BASIC;
426      `re_newline_anchor' to REG_NEWLINE being set in CFLAGS;
427      `re_fastmap' to an allocated space for the fastmap;
428      `re_fastmap_accurate' to zero;
429      `re_nsub' to the number of subexpressions in PATTERN.
430
431    PATTERN is the address of the pattern string.
432
433    CFLAGS is a series of bits which affect compilation.
434
435      If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
436      use POSIX basic syntax.
437
438      If REG_NEWLINE is set, then . and [^...] don't match newline.
439      Also, regexec will try a match beginning after every newline.
440
441      If REG_ICASE is set, then we considers upper- and lowercase
442      versions of letters to be equivalent when matching.
443
444      If REG_NOSUB is set, then when PREG is passed to regexec, that
445      routine will report only success or failure, and nothing about the
446      registers.
447
448    It returns 0 if it succeeds, nonzero if it doesn't.  (See regex.h for
449    the return codes and their meanings.)  */
450
451 int
452 regcomp (regex_t *__restrict preg, const char *__restrict pattern, int cflags)
453 {
454   reg_errcode_t ret;
455   reg_syntax_t syntax = ((cflags & REG_EXTENDED) ? REG_SYNTAX_POSIX_EXTENDED
456                         : REG_SYNTAX_POSIX_BASIC);
457
458   preg->re_buffer = NULL;
459   preg->re_allocated = 0;
460   preg->re_used = 0;
461
462   /* Try to allocate space for the fastmap.  */
463   preg->re_fastmap = re_malloc (char, SBC_MAX);
464   if (BE (preg->re_fastmap == NULL, 0))
465     return REG_ESPACE;
466
467   syntax |= (cflags & REG_ICASE) ? REG_IGNORE_CASE : 0;
468
469   /* If REG_NEWLINE is set, newlines are treated differently.  */
470   if (cflags & REG_NEWLINE)
471     { /* REG_NEWLINE implies neither . nor [^...] match newline.  */
472       syntax &= ~REG_DOT_NEWLINE;
473       syntax |= REG_HAT_LISTS_NOT_NEWLINE;
474       /* It also changes the matching behavior.  */
475       preg->re_newline_anchor = 1;
476     }
477   else
478     preg->re_newline_anchor = 0;
479   preg->re_no_sub = !!(cflags & REG_NOSUB);
480   preg->re_translate = NULL;
481
482   ret = re_compile_internal (preg, pattern, strlen (pattern), syntax);
483
484   /* POSIX doesn't distinguish between an unmatched open-group and an
485      unmatched close-group: both are REG_EPAREN.  */
486   if (ret == REG_ERPAREN)
487     ret = REG_EPAREN;
488
489   /* We have already checked preg->re_fastmap != NULL.  */
490   if (BE (ret == REG_NOERROR, 1))
491     /* Compute the fastmap now, since regexec cannot modify the pattern
492        buffer.  This function never fails in this implementation.  */
493     (void) re_compile_fastmap (preg);
494   else
495     {
496       /* Some error occurred while compiling the expression.  */
497       re_free (preg->re_fastmap);
498       preg->re_fastmap = NULL;
499     }
500
501   return (int) ret;
502 }
503 #ifdef _LIBC
504 weak_alias (__regcomp, regcomp)
505 #endif
506
507 /* Returns a message corresponding to an error code, ERRCODE, returned
508    from either regcomp or regexec.   We don't use PREG here.  */
509
510 size_t
511 regerror (int errcode, const regex_t *__restrict preg,
512           char *__restrict errbuf, size_t errbuf_size)
513 {
514   const char *msg;
515   size_t msg_size;
516
517   if (BE (errcode < 0
518           || errcode >= (int) (sizeof (__re_error_msgid_idx)
519                                / sizeof (__re_error_msgid_idx[0])), 0))
520     /* Only error codes returned by the rest of the code should be passed
521        to this routine.  If we are given anything else, or if other regex
522        code generates an invalid error code, then the program has a bug.
523        Dump core so we can fix it.  */
524     abort ();
525
526   msg = gettext (__re_error_msgid + __re_error_msgid_idx[errcode]);
527
528   msg_size = strlen (msg) + 1; /* Includes the null.  */
529
530   if (BE (errbuf_size != 0, 1))
531     {
532       if (BE (msg_size > errbuf_size, 0))
533         {
534 #if defined HAVE_MEMPCPY || defined _LIBC
535           *((char *) __mempcpy (errbuf, msg, errbuf_size - 1)) = '\0';
536 #else
537           memcpy (errbuf, msg, errbuf_size - 1);
538           errbuf[errbuf_size - 1] = 0;
539 #endif
540         }
541       else
542         memcpy (errbuf, msg, msg_size);
543     }
544
545   return msg_size;
546 }
547 #ifdef _LIBC
548 weak_alias (__regerror, regerror)
549 #endif
550
551
552 #ifdef RE_ENABLE_I18N
553 /* This static array is used for the map to single-byte characters when
554    UTF-8 is used.  Otherwise we would allocate memory just to initialize
555    it the same all the time.  UTF-8 is the preferred encoding so this is
556    a worthwhile optimization.  */
557 static const bitset utf8_sb_map =
558 {
559   /* Set the first 128 bits.  */
560 # if 2 < BITSET_WORDS
561   BITSET_WORD_MAX,
562 # endif
563 # if 4 < BITSET_WORDS
564   BITSET_WORD_MAX,
565 # endif
566 # if 6 < BITSET_WORDS
567   BITSET_WORD_MAX,
568 # endif
569 # if 8 < BITSET_WORDS
570 #  error "Invalid BITSET_WORDS"
571 # endif
572   (BITSET_WORD_MAX
573    >> (SBC_MAX % BITSET_WORD_BITS == 0
574        ? 0
575        : BITSET_WORD_BITS - SBC_MAX % BITSET_WORD_BITS))
576 };
577 #endif
578
579
580 static void
581 free_dfa_content (re_dfa_t *dfa)
582 {
583   Idx i, j;
584
585   if (dfa->nodes)
586     for (i = 0; i < dfa->nodes_len; ++i)
587       free_token (dfa->nodes + i);
588   re_free (dfa->nexts);
589   for (i = 0; i < dfa->nodes_len; ++i)
590     {
591       if (dfa->eclosures != NULL)
592         re_node_set_free (dfa->eclosures + i);
593       if (dfa->inveclosures != NULL)
594         re_node_set_free (dfa->inveclosures + i);
595       if (dfa->edests != NULL)
596         re_node_set_free (dfa->edests + i);
597     }
598   re_free (dfa->edests);
599   re_free (dfa->eclosures);
600   re_free (dfa->inveclosures);
601   re_free (dfa->nodes);
602
603   if (dfa->state_table)
604     for (i = 0; i <= dfa->state_hash_mask; ++i)
605       {
606         struct re_state_table_entry *entry = dfa->state_table + i;
607         for (j = 0; j < entry->num; ++j)
608           {
609             re_dfastate_t *state = entry->array[j];
610             free_state (state);
611           }
612         re_free (entry->array);
613       }
614   re_free (dfa->state_table);
615 #ifdef RE_ENABLE_I18N
616   if (dfa->sb_char != utf8_sb_map)
617     re_free (dfa->sb_char);
618 #endif
619   re_free (dfa->subexp_map);
620 #ifdef DEBUG
621   re_free (dfa->re_str);
622 #endif
623
624   re_free (dfa);
625 }
626
627
628 /* Free dynamically allocated space used by PREG.  */
629
630 void
631 regfree (regex_t *preg)
632 {
633   re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
634   if (BE (dfa != NULL, 1))
635     free_dfa_content (dfa);
636   preg->re_buffer = NULL;
637   preg->re_allocated = 0;
638
639   re_free (preg->re_fastmap);
640   preg->re_fastmap = NULL;
641
642   re_free (preg->re_translate);
643   preg->re_translate = NULL;
644 }
645 #ifdef _LIBC
646 weak_alias (__regfree, regfree)
647 #endif
648 \f
649 /* Entry points compatible with 4.2 BSD regex library.  We don't define
650    them unless specifically requested.  */
651
652 #if defined _REGEX_RE_COMP || defined _LIBC
653
654 /* BSD has one and only one pattern buffer.  */
655 static struct re_pattern_buffer re_comp_buf;
656
657 char *
658 # ifdef _LIBC
659 /* Make these definitions weak in libc, so POSIX programs can redefine
660    these names if they don't use our functions, and still use
661    regcomp/regexec above without link errors.  */
662 weak_function
663 # endif
664 re_comp (const char *s)
665 {
666   reg_errcode_t ret;
667   char *fastmap;
668
669   if (!s)
670     {
671       if (!re_comp_buf.re_buffer)
672         return gettext ("No previous regular expression");
673       return 0;
674     }
675
676   if (re_comp_buf.re_buffer)
677     {
678       fastmap = re_comp_buf.re_fastmap;
679       re_comp_buf.re_fastmap = NULL;
680       __regfree (&re_comp_buf);
681       memset (&re_comp_buf, '\0', sizeof (re_comp_buf));
682       re_comp_buf.re_fastmap = fastmap;
683     }
684
685   if (re_comp_buf.re_fastmap == NULL)
686     {
687       re_comp_buf.re_fastmap = (char *) malloc (SBC_MAX);
688       if (re_comp_buf.re_fastmap == NULL)
689         return (char *) gettext (__re_error_msgid
690                                  + __re_error_msgid_idx[(int) REG_ESPACE]);
691     }
692
693   /* Since `re_exec' always passes NULL for the `regs' argument, we
694      don't need to initialize the pattern buffer fields which affect it.  */
695
696   /* Match anchors at newlines.  */
697   re_comp_buf.re_newline_anchor = 1;
698
699   ret = re_compile_internal (&re_comp_buf, s, strlen (s), re_syntax_options);
700
701   if (!ret)
702     return NULL;
703
704   /* Yes, we're discarding `const' here if !HAVE_LIBINTL.  */
705   return (char *) gettext (__re_error_msgid + __re_error_msgid_idx[(int) ret]);
706 }
707
708 #ifdef _LIBC
709 libc_freeres_fn (free_mem)
710 {
711   __regfree (&re_comp_buf);
712 }
713 #endif
714
715 #endif /* _REGEX_RE_COMP */
716 \f
717 /* Internal entry point.
718    Compile the regular expression PATTERN, whose length is LENGTH.
719    SYNTAX indicate regular expression's syntax.  */
720
721 static reg_errcode_t
722 re_compile_internal (regex_t *preg, const char *pattern, Idx length,
723                      reg_syntax_t syntax)
724 {
725   reg_errcode_t err = REG_NOERROR;
726   re_dfa_t *dfa;
727   re_string_t regexp;
728
729   /* Initialize the pattern buffer.  */
730   preg->re_fastmap_accurate = 0;
731   preg->re_syntax = syntax;
732   preg->re_not_bol = preg->re_not_eol = 0;
733   preg->re_used = 0;
734   preg->re_nsub = 0;
735   preg->re_can_be_null = 0;
736   preg->re_regs_allocated = REG_UNALLOCATED;
737
738   /* Initialize the dfa.  */
739   dfa = (re_dfa_t *) preg->re_buffer;
740   if (BE (preg->re_allocated < sizeof (re_dfa_t), 0))
741     {
742       /* If zero allocated, but buffer is non-null, try to realloc
743          enough space.  This loses if buffer's address is bogus, but
744          that is the user's responsibility.  If buffer is null this
745          is a simple allocation.  */
746       dfa = re_realloc (preg->re_buffer, re_dfa_t, 1);
747       if (dfa == NULL)
748         return REG_ESPACE;
749       preg->re_allocated = sizeof (re_dfa_t);
750       preg->re_buffer = (unsigned char *) dfa;
751     }
752   preg->re_used = sizeof (re_dfa_t);
753
754   __libc_lock_init (dfa->lock);
755
756   err = init_dfa (dfa, length);
757   if (BE (err != REG_NOERROR, 0))
758     {
759       free_dfa_content (dfa);
760       preg->re_buffer = NULL;
761       preg->re_allocated = 0;
762       return err;
763     }
764 #ifdef DEBUG
765   dfa->re_str = re_malloc (char, length + 1);
766   strncpy (dfa->re_str, pattern, length + 1);
767 #endif
768
769   err = re_string_construct (&regexp, pattern, length, preg->re_translate,
770                              syntax & REG_IGNORE_CASE, dfa);
771   if (BE (err != REG_NOERROR, 0))
772     {
773     re_compile_internal_free_return:
774       free_workarea_compile (preg);
775       re_string_destruct (&regexp);
776       free_dfa_content (dfa);
777       preg->re_buffer = NULL;
778       preg->re_allocated = 0;
779       return err;
780     }
781
782   /* Parse the regular expression, and build a structure tree.  */
783   preg->re_nsub = 0;
784   dfa->str_tree = parse (&regexp, preg, syntax, &err);
785   if (BE (dfa->str_tree == NULL, 0))
786     goto re_compile_internal_free_return;
787
788   /* Analyze the tree and create the nfa.  */
789   err = analyze (preg);
790   if (BE (err != REG_NOERROR, 0))
791     goto re_compile_internal_free_return;
792
793 #ifdef RE_ENABLE_I18N
794   /* If possible, do searching in single byte encoding to speed things up.  */
795   if (dfa->is_utf8 && !(syntax & REG_IGNORE_CASE) && preg->re_translate == NULL)
796     optimize_utf8 (dfa);
797 #endif
798
799   /* Then create the initial state of the dfa.  */
800   err = create_initial_state (dfa);
801
802   /* Release work areas.  */
803   free_workarea_compile (preg);
804   re_string_destruct (&regexp);
805
806   if (BE (err != REG_NOERROR, 0))
807     {
808       free_dfa_content (dfa);
809       preg->re_buffer = NULL;
810       preg->re_allocated = 0;
811     }
812
813   return err;
814 }
815
816 /* Initialize DFA.  We use the length of the regular expression PAT_LEN
817    as the initial length of some arrays.  */
818
819 static reg_errcode_t
820 init_dfa (re_dfa_t *dfa, Idx pat_len)
821 {
822   __re_size_t table_size;
823 #ifndef _LIBC
824   char *codeset_name;
825 #endif
826
827   memset (dfa, '\0', sizeof (re_dfa_t));
828
829   /* Force allocation of str_tree_storage the first time.  */
830   dfa->str_tree_storage_idx = BIN_TREE_STORAGE_SIZE;
831
832   dfa->nodes_alloc = pat_len + 1;
833   dfa->nodes = re_xmalloc (re_token_t, dfa->nodes_alloc);
834
835   /*  table_size = 2 ^ ceil(log pat_len) */
836   for (table_size = 1; table_size <= pat_len; table_size <<= 1)
837     if (0 < (Idx) -1 && table_size == 0)
838       return REG_ESPACE;
839
840   dfa->state_table = re_calloc (struct re_state_table_entry, table_size);
841   dfa->state_hash_mask = table_size - 1;
842
843   dfa->mb_cur_max = MB_CUR_MAX;
844 #ifdef _LIBC
845   if (dfa->mb_cur_max == 6
846       && strcmp (_NL_CURRENT (LC_CTYPE, _NL_CTYPE_CODESET_NAME), "UTF-8") == 0)
847     dfa->is_utf8 = 1;
848   dfa->map_notascii = (_NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_MAP_TO_NONASCII)
849                        != 0);
850 #else
851 # ifdef HAVE_LANGINFO_CODESET
852   codeset_name = nl_langinfo (CODESET);
853 # else
854   codeset_name = getenv ("LC_ALL");
855   if (codeset_name == NULL || codeset_name[0] == '\0')
856     codeset_name = getenv ("LC_CTYPE");
857   if (codeset_name == NULL || codeset_name[0] == '\0')
858     codeset_name = getenv ("LANG");
859   if (codeset_name == NULL)
860     codeset_name = "";
861   else if (strchr (codeset_name, '.') !=  NULL)
862     codeset_name = strchr (codeset_name, '.') + 1;
863 # endif
864
865   if (strcasecmp (codeset_name, "UTF-8") == 0
866       || strcasecmp (codeset_name, "UTF8") == 0)
867     dfa->is_utf8 = 1;
868
869   /* We check exhaustively in the loop below if this charset is a
870      superset of ASCII.  */
871   dfa->map_notascii = 0;
872 #endif
873
874 #ifdef RE_ENABLE_I18N
875   if (dfa->mb_cur_max > 1)
876     {
877       if (dfa->is_utf8)
878         dfa->sb_char = (re_bitset_ptr_t) utf8_sb_map;
879       else
880         {
881           int i, j, ch;
882
883           dfa->sb_char = re_calloc (bitset_word, BITSET_WORDS);
884           if (BE (dfa->sb_char == NULL, 0))
885             return REG_ESPACE;
886
887           /* Set the bits corresponding to single byte chars.  */
888           for (i = 0, ch = 0; i < BITSET_WORDS; ++i)
889             for (j = 0; j < BITSET_WORD_BITS; ++j, ++ch)
890               {
891                 wint_t wch = __btowc (ch);
892                 if (wch != WEOF)
893                   dfa->sb_char[i] |= (bitset_word) 1 << j;
894 # ifndef _LIBC
895                 if (isascii (ch) && wch != ch)
896                   dfa->map_notascii = 1;
897 # endif
898               }
899         }
900     }
901 #endif
902
903   if (BE (dfa->nodes == NULL || dfa->state_table == NULL, 0))
904     return REG_ESPACE;
905   return REG_NOERROR;
906 }
907
908 /* Initialize WORD_CHAR table, which indicate which character is
909    "word".  In this case "word" means that it is the word construction
910    character used by some operators like "\<", "\>", etc.  */
911
912 static void
913 init_word_char (re_dfa_t *dfa)
914 {
915   int i, j, ch;
916   dfa->word_ops_used = 1;
917   for (i = 0, ch = 0; i < BITSET_WORDS; ++i)
918     for (j = 0; j < BITSET_WORD_BITS; ++j, ++ch)
919       if (isalnum (ch) || ch == '_')
920         dfa->word_char[i] |= (bitset_word) 1 << j;
921 }
922
923 /* Free the work area which are only used while compiling.  */
924
925 static void
926 free_workarea_compile (regex_t *preg)
927 {
928   re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
929   bin_tree_storage_t *storage, *next;
930   for (storage = dfa->str_tree_storage; storage; storage = next)
931     {
932       next = storage->next;
933       re_free (storage);
934     }
935   dfa->str_tree_storage = NULL;
936   dfa->str_tree_storage_idx = BIN_TREE_STORAGE_SIZE;
937   dfa->str_tree = NULL;
938   re_free (dfa->org_indices);
939   dfa->org_indices = NULL;
940 }
941
942 /* Create initial states for all contexts.  */
943
944 static reg_errcode_t
945 create_initial_state (re_dfa_t *dfa)
946 {
947   Idx first, i;
948   reg_errcode_t err;
949   re_node_set init_nodes;
950
951   /* Initial states have the epsilon closure of the node which is
952      the first node of the regular expression.  */
953   first = dfa->str_tree->first->node_idx;
954   dfa->init_node = first;
955   err = re_node_set_init_copy (&init_nodes, dfa->eclosures + first);
956   if (BE (err != REG_NOERROR, 0))
957     return err;
958
959   /* The back-references which are in initial states can epsilon transit,
960      since in this case all of the subexpressions can be null.
961      Then we add epsilon closures of the nodes which are the next nodes of
962      the back-references.  */
963   if (dfa->nbackref > 0)
964     for (i = 0; i < init_nodes.nelem; ++i)
965       {
966         Idx node_idx = init_nodes.elems[i];
967         re_token_type_t type = dfa->nodes[node_idx].type;
968
969         Idx clexp_idx;
970         if (type != OP_BACK_REF)
971           continue;
972         for (clexp_idx = 0; clexp_idx < init_nodes.nelem; ++clexp_idx)
973           {
974             re_token_t *clexp_node;
975             clexp_node = dfa->nodes + init_nodes.elems[clexp_idx];
976             if (clexp_node->type == OP_CLOSE_SUBEXP
977                 && clexp_node->opr.idx == dfa->nodes[node_idx].opr.idx)
978               break;
979           }
980         if (clexp_idx == init_nodes.nelem)
981           continue;
982
983         if (type == OP_BACK_REF)
984           {
985             Idx dest_idx = dfa->edests[node_idx].elems[0];
986             if (!re_node_set_contains (&init_nodes, dest_idx))
987               {
988                 re_node_set_merge (&init_nodes, dfa->eclosures + dest_idx);
989                 i = 0;
990               }
991           }
992       }
993
994   /* It must be the first time to invoke acquire_state.  */
995   dfa->init_state = re_acquire_state_context (&err, dfa, &init_nodes, 0);
996   /* We don't check ERR here, since the initial state must not be NULL.  */
997   if (BE (dfa->init_state == NULL, 0))
998     return err;
999   if (dfa->init_state->has_constraint)
1000     {
1001       dfa->init_state_word = re_acquire_state_context (&err, dfa, &init_nodes,
1002                                                        CONTEXT_WORD);
1003       dfa->init_state_nl = re_acquire_state_context (&err, dfa, &init_nodes,
1004                                                      CONTEXT_NEWLINE);
1005       dfa->init_state_begbuf = re_acquire_state_context (&err, dfa,
1006                                                          &init_nodes,
1007                                                          CONTEXT_NEWLINE
1008                                                          | CONTEXT_BEGBUF);
1009       if (BE (dfa->init_state_word == NULL || dfa->init_state_nl == NULL
1010               || dfa->init_state_begbuf == NULL, 0))
1011         return err;
1012     }
1013   else
1014     dfa->init_state_word = dfa->init_state_nl
1015       = dfa->init_state_begbuf = dfa->init_state;
1016
1017   re_node_set_free (&init_nodes);
1018   return REG_NOERROR;
1019 }
1020 \f
1021 #ifdef RE_ENABLE_I18N
1022 /* If it is possible to do searching in single byte encoding instead of UTF-8
1023    to speed things up, set dfa->mb_cur_max to 1, clear is_utf8 and change
1024    DFA nodes where needed.  */
1025
1026 static void
1027 optimize_utf8 (re_dfa_t *dfa)
1028 {
1029   Idx node;
1030   int i;
1031   bool mb_chars = false;
1032   bool has_period = false;
1033
1034   for (node = 0; node < dfa->nodes_len; ++node)
1035     switch (dfa->nodes[node].type)
1036       {
1037       case CHARACTER:
1038         if (dfa->nodes[node].opr.c >= 0x80)
1039           mb_chars = true;
1040         break;
1041       case ANCHOR:
1042         switch (dfa->nodes[node].opr.idx)
1043           {
1044           case LINE_FIRST:
1045           case LINE_LAST:
1046           case BUF_FIRST:
1047           case BUF_LAST:
1048             break;
1049           default:
1050             /* Word anchors etc. cannot be handled.  */
1051             return;
1052           }
1053         break;
1054       case OP_PERIOD:
1055         has_period = true;
1056         break;
1057       case OP_BACK_REF:
1058       case OP_ALT:
1059       case END_OF_RE:
1060       case OP_DUP_ASTERISK:
1061       case OP_OPEN_SUBEXP:
1062       case OP_CLOSE_SUBEXP:
1063         break;
1064       case COMPLEX_BRACKET:
1065         return;
1066       case SIMPLE_BRACKET:
1067         /* Just double check.  */
1068         {
1069           int rshift =
1070             (SBC_MAX / 2 % BITSET_WORD_BITS == 0
1071              ? 0
1072              : BITSET_WORD_BITS - SBC_MAX / 2 % BITSET_WORD_BITS);
1073           for (i = SBC_MAX / 2 / BITSET_WORD_BITS; i < BITSET_WORDS; ++i)
1074             {
1075               if (dfa->nodes[node].opr.sbcset[i] >> rshift != 0)
1076                 return;
1077               rshift = 0;
1078             }
1079         }
1080         break;
1081       default:
1082         abort ();
1083       }
1084
1085   if (mb_chars || has_period)
1086     for (node = 0; node < dfa->nodes_len; ++node)
1087       {
1088         if (dfa->nodes[node].type == CHARACTER
1089             && dfa->nodes[node].opr.c >= 0x80)
1090           dfa->nodes[node].mb_partial = 0;
1091         else if (dfa->nodes[node].type == OP_PERIOD)
1092           dfa->nodes[node].type = OP_UTF8_PERIOD;
1093       }
1094
1095   /* The search can be in single byte locale.  */
1096   dfa->mb_cur_max = 1;
1097   dfa->is_utf8 = 0;
1098   dfa->has_mb_node = dfa->nbackref > 0 || has_period;
1099 }
1100 #endif
1101 \f
1102 /* Analyze the structure tree, and calculate "first", "next", "edest",
1103    "eclosure", and "inveclosure".  */
1104
1105 static reg_errcode_t
1106 analyze (regex_t *preg)
1107 {
1108   re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
1109   reg_errcode_t ret;
1110
1111   /* Allocate arrays.  */
1112   dfa->nexts = re_malloc (Idx, dfa->nodes_alloc);
1113   dfa->org_indices = re_malloc (Idx, dfa->nodes_alloc);
1114   dfa->edests = re_xmalloc (re_node_set, dfa->nodes_alloc);
1115   dfa->eclosures = re_malloc (re_node_set, dfa->nodes_alloc);
1116   if (BE (dfa->nexts == NULL || dfa->org_indices == NULL || dfa->edests == NULL
1117           || dfa->eclosures == NULL, 0))
1118     return REG_ESPACE;
1119
1120   dfa->subexp_map = re_xmalloc (Idx, preg->re_nsub);
1121   if (dfa->subexp_map != NULL)
1122     {
1123       Idx i;
1124       for (i = 0; i < preg->re_nsub; i++)
1125         dfa->subexp_map[i] = i;
1126       preorder (dfa->str_tree, optimize_subexps, dfa);
1127       for (i = 0; i < preg->re_nsub; i++)
1128         if (dfa->subexp_map[i] != i)
1129           break;
1130       if (i == preg->re_nsub)
1131         {
1132           free (dfa->subexp_map);
1133           dfa->subexp_map = NULL;
1134         }
1135     }
1136
1137   ret = postorder (dfa->str_tree, lower_subexps, preg);
1138   if (BE (ret != REG_NOERROR, 0))
1139     return ret;
1140   ret = postorder (dfa->str_tree, calc_first, dfa);
1141   if (BE (ret != REG_NOERROR, 0))
1142     return ret;
1143   preorder (dfa->str_tree, calc_next, dfa);
1144   ret = preorder (dfa->str_tree, link_nfa_nodes, dfa);
1145   if (BE (ret != REG_NOERROR, 0))
1146     return ret;
1147   ret = calc_eclosure (dfa);
1148   if (BE (ret != REG_NOERROR, 0))
1149     return ret;
1150
1151   /* We only need this during the prune_impossible_nodes pass in regexec.c;
1152      skip it if p_i_n will not run, as calc_inveclosure can be quadratic.  */
1153   if ((!preg->re_no_sub && preg->re_nsub > 0 && dfa->has_plural_match)
1154       || dfa->nbackref)
1155     {
1156       dfa->inveclosures = re_xmalloc (re_node_set, dfa->nodes_len);
1157       if (BE (dfa->inveclosures == NULL, 0))
1158         return REG_ESPACE;
1159       ret = calc_inveclosure (dfa);
1160     }
1161
1162   return ret;
1163 }
1164
1165 /* Our parse trees are very unbalanced, so we cannot use a stack to
1166    implement parse tree visits.  Instead, we use parent pointers and
1167    some hairy code in these two functions.  */
1168 static reg_errcode_t
1169 postorder (bin_tree_t *root, reg_errcode_t (fn (void *, bin_tree_t *)),
1170            void *extra)
1171 {
1172   bin_tree_t *node, *prev;
1173
1174   for (node = root; ; )
1175     {
1176       /* Descend down the tree, preferably to the left (or to the right
1177          if that's the only child).  */
1178       while (node->left || node->right)
1179         if (node->left)
1180           node = node->left;
1181         else
1182           node = node->right;
1183
1184       do
1185         {
1186           reg_errcode_t err = fn (extra, node);
1187           if (BE (err != REG_NOERROR, 0))
1188             return err;
1189           if (node->parent == NULL)
1190             return REG_NOERROR;
1191           prev = node;
1192           node = node->parent;
1193         }
1194       /* Go up while we have a node that is reached from the right.  */
1195       while (node->right == prev || node->right == NULL);
1196       node = node->right;
1197     }
1198 }
1199
1200 static reg_errcode_t
1201 preorder (bin_tree_t *root, reg_errcode_t (fn (void *, bin_tree_t *)),
1202           void *extra)
1203 {
1204   bin_tree_t *node;
1205
1206   for (node = root; ; )
1207     {
1208       reg_errcode_t err = fn (extra, node);
1209       if (BE (err != REG_NOERROR, 0))
1210         return err;
1211
1212       /* Go to the left node, or up and to the right.  */
1213       if (node->left)
1214         node = node->left;
1215       else
1216         {
1217           bin_tree_t *prev = NULL;
1218           while (node->right == prev || node->right == NULL)
1219             {
1220               prev = node;
1221               node = node->parent;
1222               if (!node)
1223                 return REG_NOERROR;
1224             }
1225           node = node->right;
1226         }
1227     }
1228 }
1229
1230 /* Optimization pass: if a SUBEXP is entirely contained, strip it and tell
1231    re_search_internal to map the inner one's opr.idx to this one's.  Adjust
1232    backreferences as well.  Requires a preorder visit.  */
1233 static reg_errcode_t
1234 optimize_subexps (void *extra, bin_tree_t *node)
1235 {
1236   re_dfa_t *dfa = (re_dfa_t *) extra;
1237
1238   if (node->token.type == OP_BACK_REF && dfa->subexp_map)
1239     {
1240       int idx = node->token.opr.idx;
1241       node->token.opr.idx = dfa->subexp_map[idx];
1242       dfa->used_bkref_map |= 1 << node->token.opr.idx;
1243     }
1244
1245   else if (node->token.type == SUBEXP
1246            && node->left && node->left->token.type == SUBEXP)
1247     {
1248       Idx other_idx = node->left->token.opr.idx;
1249
1250       node->left = node->left->left;
1251       if (node->left)
1252         node->left->parent = node;
1253
1254       dfa->subexp_map[other_idx] = dfa->subexp_map[node->token.opr.idx];
1255       if (other_idx < BITSET_WORD_BITS)
1256         dfa->used_bkref_map &= ~ ((bitset_word) 1 << other_idx);
1257     }
1258
1259   return REG_NOERROR;
1260 }
1261
1262 /* Lowering pass: Turn each SUBEXP node into the appropriate concatenation
1263    of OP_OPEN_SUBEXP, the body of the SUBEXP (if any) and OP_CLOSE_SUBEXP.  */
1264 static reg_errcode_t
1265 lower_subexps (void *extra, bin_tree_t *node)
1266 {
1267   regex_t *preg = (regex_t *) extra;
1268   reg_errcode_t err = REG_NOERROR;
1269
1270   if (node->left && node->left->token.type == SUBEXP)
1271     {
1272       node->left = lower_subexp (&err, preg, node->left);
1273       if (node->left)
1274         node->left->parent = node;
1275     }
1276   if (node->right && node->right->token.type == SUBEXP)
1277     {
1278       node->right = lower_subexp (&err, preg, node->right);
1279       if (node->right)
1280         node->right->parent = node;
1281     }
1282
1283   return err;
1284 }
1285
1286 static bin_tree_t *
1287 lower_subexp (reg_errcode_t *err, regex_t *preg, bin_tree_t *node)
1288 {
1289   re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
1290   bin_tree_t *body = node->left;
1291   bin_tree_t *op, *cls, *tree1, *tree;
1292
1293   if (preg->re_no_sub
1294       /* We do not optimize empty subexpressions, because otherwise we may
1295          have bad CONCAT nodes with NULL children.  This is obviously not
1296          very common, so we do not lose much.  An example that triggers
1297          this case is the sed "script" /\(\)/x.  */
1298       && node->left != NULL
1299       && ! (node->token.opr.idx < BITSET_WORD_BITS
1300             && dfa->used_bkref_map & ((bitset_word) 1 << node->token.opr.idx)))
1301     return node->left;
1302
1303   /* Convert the SUBEXP node to the concatenation of an
1304      OP_OPEN_SUBEXP, the contents, and an OP_CLOSE_SUBEXP.  */
1305   op = create_tree (dfa, NULL, NULL, OP_OPEN_SUBEXP);
1306   cls = create_tree (dfa, NULL, NULL, OP_CLOSE_SUBEXP);
1307   tree1 = body ? create_tree (dfa, body, cls, CONCAT) : cls;
1308   tree = create_tree (dfa, op, tree1, CONCAT);
1309   if (BE (tree == NULL || tree1 == NULL || op == NULL || cls == NULL, 0))
1310     {
1311       *err = REG_ESPACE;
1312       return NULL;
1313     }
1314
1315   op->token.opr.idx = cls->token.opr.idx = node->token.opr.idx;
1316   op->token.opt_subexp = cls->token.opt_subexp = node->token.opt_subexp;
1317   return tree;
1318 }
1319
1320 /* Pass 1 in building the NFA: compute FIRST and create unlinked automaton
1321    nodes.  Requires a postorder visit.  */
1322 static reg_errcode_t
1323 calc_first (void *extra, bin_tree_t *node)
1324 {
1325   re_dfa_t *dfa = (re_dfa_t *) extra;
1326   if (node->token.type == CONCAT)
1327     {
1328       node->first = node->left->first;
1329       node->node_idx = node->left->node_idx;
1330     }
1331   else
1332     {
1333       node->first = node;
1334       node->node_idx = re_dfa_add_node (dfa, node->token);
1335       if (BE (node->node_idx == REG_MISSING, 0))
1336         return REG_ESPACE;
1337     }
1338   return REG_NOERROR;
1339 }
1340
1341 /* Pass 2: compute NEXT on the tree.  Preorder visit.  */
1342 static reg_errcode_t
1343 calc_next (void *extra, bin_tree_t *node)
1344 {
1345   switch (node->token.type)
1346     {
1347     case OP_DUP_ASTERISK:
1348       node->left->next = node;
1349       break;
1350     case CONCAT:
1351       node->left->next = node->right->first;
1352       node->right->next = node->next;
1353       break;
1354     default:
1355       if (node->left)
1356         node->left->next = node->next;
1357       if (node->right)
1358         node->right->next = node->next;
1359       break;
1360     }
1361   return REG_NOERROR;
1362 }
1363
1364 /* Pass 3: link all DFA nodes to their NEXT node (any order will do).  */
1365 static reg_errcode_t
1366 link_nfa_nodes (void *extra, bin_tree_t *node)
1367 {
1368   re_dfa_t *dfa = (re_dfa_t *) extra;
1369   Idx idx = node->node_idx;
1370   reg_errcode_t err = REG_NOERROR;
1371
1372   switch (node->token.type)
1373     {
1374     case CONCAT:
1375       break;
1376
1377     case END_OF_RE:
1378       assert (node->next == NULL);
1379       break;
1380
1381     case OP_DUP_ASTERISK:
1382     case OP_ALT:
1383       {
1384         Idx left, right;
1385         dfa->has_plural_match = 1;
1386         if (node->left != NULL)
1387           left = node->left->first->node_idx;
1388         else
1389           left = node->next->node_idx;
1390         if (node->right != NULL)
1391           right = node->right->first->node_idx;
1392         else
1393           right = node->next->node_idx;
1394         assert (REG_VALID_INDEX (left));
1395         assert (REG_VALID_INDEX (right));
1396         err = re_node_set_init_2 (dfa->edests + idx, left, right);
1397       }
1398       break;
1399
1400     case ANCHOR:
1401     case OP_OPEN_SUBEXP:
1402     case OP_CLOSE_SUBEXP:
1403       err = re_node_set_init_1 (dfa->edests + idx, node->next->node_idx);
1404       break;
1405
1406     case OP_BACK_REF:
1407       dfa->nexts[idx] = node->next->node_idx;
1408       if (node->token.type == OP_BACK_REF)
1409         re_node_set_init_1 (dfa->edests + idx, dfa->nexts[idx]);
1410       break;
1411
1412     default:
1413       assert (!IS_EPSILON_NODE (node->token.type));
1414       dfa->nexts[idx] = node->next->node_idx;
1415       break;
1416     }
1417
1418   return err;
1419 }
1420
1421 /* Duplicate the epsilon closure of the node ROOT_NODE.
1422    Note that duplicated nodes have constraint INIT_CONSTRAINT in addition
1423    to their own constraint.  */
1424
1425 static reg_errcode_t
1426 duplicate_node_closure (re_dfa_t *dfa, Idx top_org_node,
1427                         Idx top_clone_node, Idx root_node,
1428                         unsigned int init_constraint)
1429 {
1430   Idx org_node, clone_node;
1431   bool ok;
1432   unsigned int constraint = init_constraint;
1433   for (org_node = top_org_node, clone_node = top_clone_node;;)
1434     {
1435       Idx org_dest, clone_dest;
1436       if (dfa->nodes[org_node].type == OP_BACK_REF)
1437         {
1438           /* If the back reference epsilon-transit, its destination must
1439              also have the constraint.  Then duplicate the epsilon closure
1440              of the destination of the back reference, and store it in
1441              edests of the back reference.  */
1442           org_dest = dfa->nexts[org_node];
1443           re_node_set_empty (dfa->edests + clone_node);
1444           clone_dest = duplicate_node (dfa, org_dest, constraint);
1445           if (BE (clone_dest == REG_MISSING, 0))
1446             return REG_ESPACE;
1447           dfa->nexts[clone_node] = dfa->nexts[org_node];
1448           ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
1449           if (BE (! ok, 0))
1450             return REG_ESPACE;
1451         }
1452       else if (dfa->edests[org_node].nelem == 0)
1453         {
1454           /* In case of the node can't epsilon-transit, don't duplicate the
1455              destination and store the original destination as the
1456              destination of the node.  */
1457           dfa->nexts[clone_node] = dfa->nexts[org_node];
1458           break;
1459         }
1460       else if (dfa->edests[org_node].nelem == 1)
1461         {
1462           /* In case of the node can epsilon-transit, and it has only one
1463              destination.  */
1464           org_dest = dfa->edests[org_node].elems[0];
1465           re_node_set_empty (dfa->edests + clone_node);
1466           if (dfa->nodes[org_node].type == ANCHOR)
1467             {
1468               /* In case of the node has another constraint, append it.  */
1469               if (org_node == root_node && clone_node != org_node)
1470                 {
1471                   /* ...but if the node is root_node itself, it means the
1472                      epsilon closure have a loop, then tie it to the
1473                      destination of the root_node.  */
1474                   ok = re_node_set_insert (dfa->edests + clone_node,
1475                                             org_dest);
1476                   if (BE (! ok, 0))
1477                     return REG_ESPACE;
1478                   break;
1479                 }
1480               constraint |= dfa->nodes[org_node].opr.ctx_type;
1481             }
1482           clone_dest = duplicate_node (dfa, org_dest, constraint);
1483           if (BE (clone_dest == REG_MISSING, 0))
1484             return REG_ESPACE;
1485           ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
1486           if (BE (! ok, 0))
1487             return REG_ESPACE;
1488         }
1489       else /* dfa->edests[org_node].nelem == 2 */
1490         {
1491           /* In case of the node can epsilon-transit, and it has two
1492              destinations. In the bin_tree_t and DFA, that's '|' and '*'.   */
1493           org_dest = dfa->edests[org_node].elems[0];
1494           re_node_set_empty (dfa->edests + clone_node);
1495           /* Search for a duplicated node which satisfies the constraint.  */
1496           clone_dest = search_duplicated_node (dfa, org_dest, constraint);
1497           if (clone_dest == REG_MISSING)
1498             {
1499               /* There are no such a duplicated node, create a new one.  */
1500               reg_errcode_t err;
1501               clone_dest = duplicate_node (dfa, org_dest, constraint);
1502               if (BE (clone_dest == REG_MISSING, 0))
1503                 return REG_ESPACE;
1504               ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
1505               if (BE (! ok, 0))
1506                 return REG_ESPACE;
1507               err = duplicate_node_closure (dfa, org_dest, clone_dest,
1508                                             root_node, constraint);
1509               if (BE (err != REG_NOERROR, 0))
1510                 return err;
1511             }
1512           else
1513             {
1514               /* There are a duplicated node which satisfy the constraint,
1515                  use it to avoid infinite loop.  */
1516               ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
1517               if (BE (! ok, 0))
1518                 return REG_ESPACE;
1519             }
1520
1521           org_dest = dfa->edests[org_node].elems[1];
1522           clone_dest = duplicate_node (dfa, org_dest, constraint);
1523           if (BE (clone_dest == REG_MISSING, 0))
1524             return REG_ESPACE;
1525           ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
1526           if (BE (! ok, 0))
1527             return REG_ESPACE;
1528         }
1529       org_node = org_dest;
1530       clone_node = clone_dest;
1531     }
1532   return REG_NOERROR;
1533 }
1534
1535 /* Search for a node which is duplicated from the node ORG_NODE, and
1536    satisfies the constraint CONSTRAINT.  */
1537
1538 static Idx
1539 search_duplicated_node (const re_dfa_t *dfa, Idx org_node,
1540                         unsigned int constraint)
1541 {
1542   Idx idx;
1543   for (idx = dfa->nodes_len - 1; dfa->nodes[idx].duplicated && idx > 0; --idx)
1544     {
1545       if (org_node == dfa->org_indices[idx]
1546           && constraint == dfa->nodes[idx].constraint)
1547         return idx; /* Found.  */
1548     }
1549   return REG_MISSING; /* Not found.  */
1550 }
1551
1552 /* Duplicate the node whose index is ORG_IDX and set the constraint CONSTRAINT.
1553    Return the index of the new node, or REG_MISSING if insufficient storage is
1554    available.  */
1555
1556 static Idx
1557 duplicate_node (re_dfa_t *dfa, Idx org_idx, unsigned int constraint)
1558 {
1559   Idx dup_idx = re_dfa_add_node (dfa, dfa->nodes[org_idx]);
1560   if (BE (dup_idx != REG_MISSING, 1))
1561     {
1562       dfa->nodes[dup_idx].constraint = constraint;
1563       if (dfa->nodes[org_idx].type == ANCHOR)
1564         dfa->nodes[dup_idx].constraint |= dfa->nodes[org_idx].opr.ctx_type;
1565       dfa->nodes[dup_idx].duplicated = 1;
1566
1567       /* Store the index of the original node.  */
1568       dfa->org_indices[dup_idx] = org_idx;
1569     }
1570   return dup_idx;
1571 }
1572
1573 static reg_errcode_t
1574 calc_inveclosure (re_dfa_t *dfa)
1575 {
1576   Idx src, idx;
1577   bool ok;
1578   for (idx = 0; idx < dfa->nodes_len; ++idx)
1579     re_node_set_init_empty (dfa->inveclosures + idx);
1580
1581   for (src = 0; src < dfa->nodes_len; ++src)
1582     {
1583       Idx *elems = dfa->eclosures[src].elems;
1584       for (idx = 0; idx < dfa->eclosures[src].nelem; ++idx)
1585         {
1586           ok = re_node_set_insert_last (dfa->inveclosures + elems[idx], src);
1587           if (BE (! ok, 0))
1588             return REG_ESPACE;
1589         }
1590     }
1591
1592   return REG_NOERROR;
1593 }
1594
1595 /* Calculate "eclosure" for all the node in DFA.  */
1596
1597 static reg_errcode_t
1598 calc_eclosure (re_dfa_t *dfa)
1599 {
1600   Idx node_idx;
1601   bool incomplete;
1602 #ifdef DEBUG
1603   assert (dfa->nodes_len > 0);
1604 #endif
1605   incomplete = false;
1606   /* For each nodes, calculate epsilon closure.  */
1607   for (node_idx = 0; ; ++node_idx)
1608     {
1609       reg_errcode_t err;
1610       re_node_set eclosure_elem;
1611       if (node_idx == dfa->nodes_len)
1612         {
1613           if (!incomplete)
1614             break;
1615           incomplete = false;
1616           node_idx = 0;
1617         }
1618
1619 #ifdef DEBUG
1620       assert (dfa->eclosures[node_idx].nelem != REG_MISSING);
1621 #endif
1622
1623       /* If we have already calculated, skip it.  */
1624       if (dfa->eclosures[node_idx].nelem != 0)
1625         continue;
1626       /* Calculate epsilon closure of `node_idx'.  */
1627       err = calc_eclosure_iter (&eclosure_elem, dfa, node_idx, true);
1628       if (BE (err != REG_NOERROR, 0))
1629         return err;
1630
1631       if (dfa->eclosures[node_idx].nelem == 0)
1632         {
1633           incomplete = true;
1634           re_node_set_free (&eclosure_elem);
1635         }
1636     }
1637   return REG_NOERROR;
1638 }
1639
1640 /* Calculate epsilon closure of NODE.  */
1641
1642 static reg_errcode_t
1643 calc_eclosure_iter (re_node_set *new_set, re_dfa_t *dfa, Idx node, bool root)
1644 {
1645   reg_errcode_t err;
1646   unsigned int constraint;
1647   Idx i;
1648   bool incomplete;
1649   bool ok;
1650   re_node_set eclosure;
1651   incomplete = false;
1652   err = re_node_set_alloc (&eclosure, dfa->edests[node].nelem + 1);
1653   if (BE (err != REG_NOERROR, 0))
1654     return err;
1655
1656   /* This indicates that we are calculating this node now.
1657      We reference this value to avoid infinite loop.  */
1658   dfa->eclosures[node].nelem = REG_MISSING;
1659
1660   constraint = ((dfa->nodes[node].type == ANCHOR)
1661                 ? dfa->nodes[node].opr.ctx_type : 0);
1662   /* If the current node has constraints, duplicate all nodes.
1663      Since they must inherit the constraints.  */
1664   if (constraint
1665       && dfa->edests[node].nelem
1666       && !dfa->nodes[dfa->edests[node].elems[0]].duplicated)
1667     {
1668       Idx org_node, cur_node;
1669       org_node = cur_node = node;
1670       err = duplicate_node_closure (dfa, node, node, node, constraint);
1671       if (BE (err != REG_NOERROR, 0))
1672         return err;
1673     }
1674
1675   /* Expand each epsilon destination nodes.  */
1676   if (IS_EPSILON_NODE(dfa->nodes[node].type))
1677     for (i = 0; i < dfa->edests[node].nelem; ++i)
1678       {
1679         re_node_set eclosure_elem;
1680         Idx edest = dfa->edests[node].elems[i];
1681         /* If calculating the epsilon closure of `edest' is in progress,
1682            return intermediate result.  */
1683         if (dfa->eclosures[edest].nelem == REG_MISSING)
1684           {
1685             incomplete = true;
1686             continue;
1687           }
1688         /* If we haven't calculated the epsilon closure of `edest' yet,
1689            calculate now. Otherwise use calculated epsilon closure.  */
1690         if (dfa->eclosures[edest].nelem == 0)
1691           {
1692             err = calc_eclosure_iter (&eclosure_elem, dfa, edest, false);
1693             if (BE (err != REG_NOERROR, 0))
1694               return err;
1695           }
1696         else
1697           eclosure_elem = dfa->eclosures[edest];
1698         /* Merge the epsilon closure of `edest'.  */
1699         re_node_set_merge (&eclosure, &eclosure_elem);
1700         /* If the epsilon closure of `edest' is incomplete,
1701            the epsilon closure of this node is also incomplete.  */
1702         if (dfa->eclosures[edest].nelem == 0)
1703           {
1704             incomplete = true;
1705             re_node_set_free (&eclosure_elem);
1706           }
1707       }
1708
1709   /* Epsilon closures include itself.  */
1710   ok = re_node_set_insert (&eclosure, node);
1711   if (BE (! ok, 0))
1712     return REG_ESPACE;
1713   if (incomplete && !root)
1714     dfa->eclosures[node].nelem = 0;
1715   else
1716     dfa->eclosures[node] = eclosure;
1717   *new_set = eclosure;
1718   return REG_NOERROR;
1719 }
1720 \f
1721 /* Functions for token which are used in the parser.  */
1722
1723 /* Fetch a token from INPUT.
1724    We must not use this function inside bracket expressions.  */
1725
1726 static void
1727 fetch_token (re_token_t *result, re_string_t *input, reg_syntax_t syntax)
1728 {
1729   re_string_skip_bytes (input, peek_token (result, input, syntax));
1730 }
1731
1732 /* Peek a token from INPUT, and return the length of the token.
1733    We must not use this function inside bracket expressions.  */
1734
1735 static int
1736 peek_token (re_token_t *token, re_string_t *input, reg_syntax_t syntax)
1737 {
1738   unsigned char c;
1739
1740   if (re_string_eoi (input))
1741     {
1742       token->type = END_OF_RE;
1743       return 0;
1744     }
1745
1746   c = re_string_peek_byte (input, 0);
1747   token->opr.c = c;
1748
1749   token->word_char = 0;
1750 #ifdef RE_ENABLE_I18N
1751   token->mb_partial = 0;
1752   if (input->mb_cur_max > 1 &&
1753       !re_string_first_byte (input, re_string_cur_idx (input)))
1754     {
1755       token->type = CHARACTER;
1756       token->mb_partial = 1;
1757       return 1;
1758     }
1759 #endif
1760   if (c == '\\')
1761     {
1762       unsigned char c2;
1763       if (re_string_cur_idx (input) + 1 >= re_string_length (input))
1764         {
1765           token->type = BACK_SLASH;
1766           return 1;
1767         }
1768
1769       c2 = re_string_peek_byte_case (input, 1);
1770       token->opr.c = c2;
1771       token->type = CHARACTER;
1772 #ifdef RE_ENABLE_I18N
1773       if (input->mb_cur_max > 1)
1774         {
1775           wint_t wc = re_string_wchar_at (input,
1776                                           re_string_cur_idx (input) + 1);
1777           token->word_char = IS_WIDE_WORD_CHAR (wc) != 0;
1778         }
1779       else
1780 #endif
1781         token->word_char = IS_WORD_CHAR (c2) != 0;
1782
1783       switch (c2)
1784         {
1785         case '|':
1786           if (!(syntax & REG_LIMITED_OPS) && !(syntax & REG_NO_BK_VBAR))
1787             token->type = OP_ALT;
1788           break;
1789         case '1': case '2': case '3': case '4': case '5':
1790         case '6': case '7': case '8': case '9':
1791           if (!(syntax & REG_NO_BK_REFS))
1792             {
1793               token->type = OP_BACK_REF;
1794               token->opr.idx = c2 - '1';
1795             }
1796           break;
1797         case '<':
1798           if (!(syntax & REG_NO_GNU_OPS))
1799             {
1800               token->type = ANCHOR;
1801               token->opr.ctx_type = WORD_FIRST;
1802             }
1803           break;
1804         case '>':
1805           if (!(syntax & REG_NO_GNU_OPS))
1806             {
1807               token->type = ANCHOR;
1808               token->opr.ctx_type = WORD_LAST;
1809             }
1810           break;
1811         case 'b':
1812           if (!(syntax & REG_NO_GNU_OPS))
1813             {
1814               token->type = ANCHOR;
1815               token->opr.ctx_type = WORD_DELIM;
1816             }
1817           break;
1818         case 'B':
1819           if (!(syntax & REG_NO_GNU_OPS))
1820             {
1821               token->type = ANCHOR;
1822               token->opr.ctx_type = NOT_WORD_DELIM;
1823             }
1824           break;
1825         case 'w':
1826           if (!(syntax & REG_NO_GNU_OPS))
1827             token->type = OP_WORD;
1828           break;
1829         case 'W':
1830           if (!(syntax & REG_NO_GNU_OPS))
1831             token->type = OP_NOTWORD;
1832           break;
1833         case 's':
1834           if (!(syntax & REG_NO_GNU_OPS))
1835             token->type = OP_SPACE;
1836           break;
1837         case 'S':
1838           if (!(syntax & REG_NO_GNU_OPS))
1839             token->type = OP_NOTSPACE;
1840           break;
1841         case '`':
1842           if (!(syntax & REG_NO_GNU_OPS))
1843             {
1844               token->type = ANCHOR;
1845               token->opr.ctx_type = BUF_FIRST;
1846             }
1847           break;
1848         case '\'':
1849           if (!(syntax & REG_NO_GNU_OPS))
1850             {
1851               token->type = ANCHOR;
1852               token->opr.ctx_type = BUF_LAST;
1853             }
1854           break;
1855         case '(':
1856           if (!(syntax & REG_NO_BK_PARENS))
1857             token->type = OP_OPEN_SUBEXP;
1858           break;
1859         case ')':
1860           if (!(syntax & REG_NO_BK_PARENS))
1861             token->type = OP_CLOSE_SUBEXP;
1862           break;
1863         case '+':
1864           if (!(syntax & REG_LIMITED_OPS) && (syntax & REG_BK_PLUS_QM))
1865             token->type = OP_DUP_PLUS;
1866           break;
1867         case '?':
1868           if (!(syntax & REG_LIMITED_OPS) && (syntax & REG_BK_PLUS_QM))
1869             token->type = OP_DUP_QUESTION;
1870           break;
1871         case '{':
1872           if ((syntax & REG_INTERVALS) && (!(syntax & REG_NO_BK_BRACES)))
1873             token->type = OP_OPEN_DUP_NUM;
1874           break;
1875         case '}':
1876           if ((syntax & REG_INTERVALS) && (!(syntax & REG_NO_BK_BRACES)))
1877             token->type = OP_CLOSE_DUP_NUM;
1878           break;
1879         default:
1880           break;
1881         }
1882       return 2;
1883     }
1884
1885   token->type = CHARACTER;
1886 #ifdef RE_ENABLE_I18N
1887   if (input->mb_cur_max > 1)
1888     {
1889       wint_t wc = re_string_wchar_at (input, re_string_cur_idx (input));
1890       token->word_char = IS_WIDE_WORD_CHAR (wc) != 0;
1891     }
1892   else
1893 #endif
1894     token->word_char = IS_WORD_CHAR (token->opr.c);
1895
1896   switch (c)
1897     {
1898     case '\n':
1899       if (syntax & REG_NEWLINE_ALT)
1900         token->type = OP_ALT;
1901       break;
1902     case '|':
1903       if (!(syntax & REG_LIMITED_OPS) && (syntax & REG_NO_BK_VBAR))
1904         token->type = OP_ALT;
1905       break;
1906     case '*':
1907       token->type = OP_DUP_ASTERISK;
1908       break;
1909     case '+':
1910       if (!(syntax & REG_LIMITED_OPS) && !(syntax & REG_BK_PLUS_QM))
1911         token->type = OP_DUP_PLUS;
1912       break;
1913     case '?':
1914       if (!(syntax & REG_LIMITED_OPS) && !(syntax & REG_BK_PLUS_QM))
1915         token->type = OP_DUP_QUESTION;
1916       break;
1917     case '{':
1918       if ((syntax & REG_INTERVALS) && (syntax & REG_NO_BK_BRACES))
1919         token->type = OP_OPEN_DUP_NUM;
1920       break;
1921     case '}':
1922       if ((syntax & REG_INTERVALS) && (syntax & REG_NO_BK_BRACES))
1923         token->type = OP_CLOSE_DUP_NUM;
1924       break;
1925     case '(':
1926       if (syntax & REG_NO_BK_PARENS)
1927         token->type = OP_OPEN_SUBEXP;
1928       break;
1929     case ')':
1930       if (syntax & REG_NO_BK_PARENS)
1931         token->type = OP_CLOSE_SUBEXP;
1932       break;
1933     case '[':
1934       token->type = OP_OPEN_BRACKET;
1935       break;
1936     case '.':
1937       token->type = OP_PERIOD;
1938       break;
1939     case '^':
1940       if (!(syntax & (REG_CONTEXT_INDEP_ANCHORS | REG_CARET_ANCHORS_HERE)) &&
1941           re_string_cur_idx (input) != 0)
1942         {
1943           char prev = re_string_peek_byte (input, -1);
1944           if (!(syntax & REG_NEWLINE_ALT) || prev != '\n')
1945             break;
1946         }
1947       token->type = ANCHOR;
1948       token->opr.ctx_type = LINE_FIRST;
1949       break;
1950     case '$':
1951       if (!(syntax & REG_CONTEXT_INDEP_ANCHORS) &&
1952           re_string_cur_idx (input) + 1 != re_string_length (input))
1953         {
1954           re_token_t next;
1955           re_string_skip_bytes (input, 1);
1956           peek_token (&next, input, syntax);
1957           re_string_skip_bytes (input, -1);
1958           if (next.type != OP_ALT && next.type != OP_CLOSE_SUBEXP)
1959             break;
1960         }
1961       token->type = ANCHOR;
1962       token->opr.ctx_type = LINE_LAST;
1963       break;
1964     default:
1965       break;
1966     }
1967   return 1;
1968 }
1969
1970 /* Peek a token from INPUT, and return the length of the token.
1971    We must not use this function out of bracket expressions.  */
1972
1973 static int
1974 peek_token_bracket (re_token_t *token, re_string_t *input, reg_syntax_t syntax)
1975 {
1976   unsigned char c;
1977   if (re_string_eoi (input))
1978     {
1979       token->type = END_OF_RE;
1980       return 0;
1981     }
1982   c = re_string_peek_byte (input, 0);
1983   token->opr.c = c;
1984
1985 #ifdef RE_ENABLE_I18N
1986   if (input->mb_cur_max > 1 &&
1987       !re_string_first_byte (input, re_string_cur_idx (input)))
1988     {
1989       token->type = CHARACTER;
1990       return 1;
1991     }
1992 #endif /* RE_ENABLE_I18N */
1993
1994   if (c == '\\' && (syntax & REG_BACKSLASH_ESCAPE_IN_LISTS)
1995       && re_string_cur_idx (input) + 1 < re_string_length (input))
1996     {
1997       /* In this case, '\' escape a character.  */
1998       unsigned char c2;
1999       re_string_skip_bytes (input, 1);
2000       c2 = re_string_peek_byte (input, 0);
2001       token->opr.c = c2;
2002       token->type = CHARACTER;
2003       return 1;
2004     }
2005   if (c == '[') /* '[' is a special char in a bracket exps.  */
2006     {
2007       unsigned char c2;
2008       int token_len;
2009       if (re_string_cur_idx (input) + 1 < re_string_length (input))
2010         c2 = re_string_peek_byte (input, 1);
2011       else
2012         c2 = 0;
2013       token->opr.c = c2;
2014       token_len = 2;
2015       switch (c2)
2016         {
2017         case '.':
2018           token->type = OP_OPEN_COLL_ELEM;
2019           break;
2020         case '=':
2021           token->type = OP_OPEN_EQUIV_CLASS;
2022           break;
2023         case ':':
2024           if (syntax & REG_CHAR_CLASSES)
2025             {
2026               token->type = OP_OPEN_CHAR_CLASS;
2027               break;
2028             }
2029           /* else fall through.  */
2030         default:
2031           token->type = CHARACTER;
2032           token->opr.c = c;
2033           token_len = 1;
2034           break;
2035         }
2036       return token_len;
2037     }
2038   switch (c)
2039     {
2040     case '-':
2041       token->type = OP_CHARSET_RANGE;
2042       break;
2043     case ']':
2044       token->type = OP_CLOSE_BRACKET;
2045       break;
2046     case '^':
2047       token->type = OP_NON_MATCH_LIST;
2048       break;
2049     default:
2050       token->type = CHARACTER;
2051     }
2052   return 1;
2053 }
2054 \f
2055 /* Functions for parser.  */
2056
2057 /* Entry point of the parser.
2058    Parse the regular expression REGEXP and return the structure tree.
2059    If an error is occured, ERR is set by error code, and return NULL.
2060    This function build the following tree, from regular expression <reg_exp>:
2061            CAT
2062            / \
2063           /   \
2064    <reg_exp>  EOR
2065
2066    CAT means concatenation.
2067    EOR means end of regular expression.  */
2068
2069 static bin_tree_t *
2070 parse (re_string_t *regexp, regex_t *preg, reg_syntax_t syntax,
2071        reg_errcode_t *err)
2072 {
2073   re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
2074   bin_tree_t *tree, *eor, *root;
2075   re_token_t current_token;
2076   dfa->syntax = syntax;
2077   fetch_token (&current_token, regexp, syntax | REG_CARET_ANCHORS_HERE);
2078   tree = parse_reg_exp (regexp, preg, &current_token, syntax, 0, err);
2079   if (BE (*err != REG_NOERROR && tree == NULL, 0))
2080     return NULL;
2081   eor = create_tree (dfa, NULL, NULL, END_OF_RE);
2082   if (tree != NULL)
2083     root = create_tree (dfa, tree, eor, CONCAT);
2084   else
2085     root = eor;
2086   if (BE (eor == NULL || root == NULL, 0))
2087     {
2088       *err = REG_ESPACE;
2089       return NULL;
2090     }
2091   return root;
2092 }
2093
2094 /* This function build the following tree, from regular expression
2095    <branch1>|<branch2>:
2096            ALT
2097            / \
2098           /   \
2099    <branch1> <branch2>
2100
2101    ALT means alternative, which represents the operator `|'.  */
2102
2103 static bin_tree_t *
2104 parse_reg_exp (re_string_t *regexp, regex_t *preg, re_token_t *token,
2105                reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
2106 {
2107   re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
2108   bin_tree_t *tree, *branch = NULL;
2109   tree = parse_branch (regexp, preg, token, syntax, nest, err);
2110   if (BE (*err != REG_NOERROR && tree == NULL, 0))
2111     return NULL;
2112
2113   while (token->type == OP_ALT)
2114     {
2115       fetch_token (token, regexp, syntax | REG_CARET_ANCHORS_HERE);
2116       if (token->type != OP_ALT && token->type != END_OF_RE
2117           && (nest == 0 || token->type != OP_CLOSE_SUBEXP))
2118         {
2119           branch = parse_branch (regexp, preg, token, syntax, nest, err);
2120           if (BE (*err != REG_NOERROR && branch == NULL, 0))
2121             return NULL;
2122         }
2123       else
2124         branch = NULL;
2125       tree = create_tree (dfa, tree, branch, OP_ALT);
2126       if (BE (tree == NULL, 0))
2127         {
2128           *err = REG_ESPACE;
2129           return NULL;
2130         }
2131     }
2132   return tree;
2133 }
2134
2135 /* This function build the following tree, from regular expression
2136    <exp1><exp2>:
2137         CAT
2138         / \
2139        /   \
2140    <exp1> <exp2>
2141
2142    CAT means concatenation.  */
2143
2144 static bin_tree_t *
2145 parse_branch (re_string_t *regexp, regex_t *preg, re_token_t *token,
2146               reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
2147 {
2148   bin_tree_t *tree, *exp;
2149   re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
2150   tree = parse_expression (regexp, preg, token, syntax, nest, err);
2151   if (BE (*err != REG_NOERROR && tree == NULL, 0))
2152     return NULL;
2153
2154   while (token->type != OP_ALT && token->type != END_OF_RE
2155          && (nest == 0 || token->type != OP_CLOSE_SUBEXP))
2156     {
2157       exp = parse_expression (regexp, preg, token, syntax, nest, err);
2158       if (BE (*err != REG_NOERROR && exp == NULL, 0))
2159         {
2160           return NULL;
2161         }
2162       if (tree != NULL && exp != NULL)
2163         {
2164           tree = create_tree (dfa, tree, exp, CONCAT);
2165           if (tree == NULL)
2166             {
2167               *err = REG_ESPACE;
2168               return NULL;
2169             }
2170         }
2171       else if (tree == NULL)
2172         tree = exp;
2173       /* Otherwise exp == NULL, we don't need to create new tree.  */
2174     }
2175   return tree;
2176 }
2177
2178 /* This function build the following tree, from regular expression a*:
2179          *
2180          |
2181          a
2182 */
2183
2184 static bin_tree_t *
2185 parse_expression (re_string_t *regexp, regex_t *preg, re_token_t *token,
2186                   reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
2187 {
2188   re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
2189   bin_tree_t *tree;
2190   switch (token->type)
2191     {
2192     case CHARACTER:
2193       tree = create_token_tree (dfa, NULL, NULL, token);
2194       if (BE (tree == NULL, 0))
2195         {
2196           *err = REG_ESPACE;
2197           return NULL;
2198         }
2199 #ifdef RE_ENABLE_I18N
2200       if (dfa->mb_cur_max > 1)
2201         {
2202           while (!re_string_eoi (regexp)
2203                  && !re_string_first_byte (regexp, re_string_cur_idx (regexp)))
2204             {
2205               bin_tree_t *mbc_remain;
2206               fetch_token (token, regexp, syntax);
2207               mbc_remain = create_token_tree (dfa, NULL, NULL, token);
2208               tree = create_tree (dfa, tree, mbc_remain, CONCAT);
2209               if (BE (mbc_remain == NULL || tree == NULL, 0))
2210                 {
2211                   *err = REG_ESPACE;
2212                   return NULL;
2213                 }
2214             }
2215         }
2216 #endif
2217       break;
2218     case OP_OPEN_SUBEXP:
2219       tree = parse_sub_exp (regexp, preg, token, syntax, nest + 1, err);
2220       if (BE (*err != REG_NOERROR && tree == NULL, 0))
2221         return NULL;
2222       break;
2223     case OP_OPEN_BRACKET:
2224       tree = parse_bracket_exp (regexp, dfa, token, syntax, err);
2225       if (BE (*err != REG_NOERROR && tree == NULL, 0))
2226         return NULL;
2227       break;
2228     case OP_BACK_REF:
2229       if (!BE (dfa->completed_bkref_map & (1 << token->opr.idx), 1))
2230         {
2231           *err = REG_ESUBREG;
2232           return NULL;
2233         }
2234       dfa->used_bkref_map |= 1 << token->opr.idx;
2235       tree = create_token_tree (dfa, NULL, NULL, token);
2236       if (BE (tree == NULL, 0))
2237         {
2238           *err = REG_ESPACE;
2239           return NULL;
2240         }
2241       ++dfa->nbackref;
2242       dfa->has_mb_node = 1;
2243       break;
2244     case OP_OPEN_DUP_NUM:
2245       if (syntax & REG_CONTEXT_INVALID_DUP)
2246         {
2247           *err = REG_BADRPT;
2248           return NULL;
2249         }
2250       /* FALLTHROUGH */
2251     case OP_DUP_ASTERISK:
2252     case OP_DUP_PLUS:
2253     case OP_DUP_QUESTION:
2254       if (syntax & REG_CONTEXT_INVALID_OPS)
2255         {
2256           *err = REG_BADRPT;
2257           return NULL;
2258         }
2259       else if (syntax & REG_CONTEXT_INDEP_OPS)
2260         {
2261           fetch_token (token, regexp, syntax);
2262           return parse_expression (regexp, preg, token, syntax, nest, err);
2263         }
2264       /* else fall through  */
2265     case OP_CLOSE_SUBEXP:
2266       if ((token->type == OP_CLOSE_SUBEXP) &&
2267           !(syntax & REG_UNMATCHED_RIGHT_PAREN_ORD))
2268         {
2269           *err = REG_ERPAREN;
2270           return NULL;
2271         }
2272       /* else fall through  */
2273     case OP_CLOSE_DUP_NUM:
2274       /* We treat it as a normal character.  */
2275
2276       /* Then we can these characters as normal characters.  */
2277       token->type = CHARACTER;
2278       /* mb_partial and word_char bits should be initialized already
2279          by peek_token.  */
2280       tree = create_token_tree (dfa, NULL, NULL, token);
2281       if (BE (tree == NULL, 0))
2282         {
2283           *err = REG_ESPACE;
2284           return NULL;
2285         }
2286       break;
2287     case ANCHOR:
2288       if ((token->opr.ctx_type
2289            & (WORD_DELIM | NOT_WORD_DELIM | WORD_FIRST | WORD_LAST))
2290           && dfa->word_ops_used == 0)
2291         init_word_char (dfa);
2292       if (token->opr.ctx_type == WORD_DELIM
2293           || token->opr.ctx_type == NOT_WORD_DELIM)
2294         {
2295           bin_tree_t *tree_first, *tree_last;
2296           if (token->opr.ctx_type == WORD_DELIM)
2297             {
2298               token->opr.ctx_type = WORD_FIRST;
2299               tree_first = create_token_tree (dfa, NULL, NULL, token);
2300               token->opr.ctx_type = WORD_LAST;
2301             }
2302           else
2303             {
2304               token->opr.ctx_type = INSIDE_WORD;
2305               tree_first = create_token_tree (dfa, NULL, NULL, token);
2306               token->opr.ctx_type = INSIDE_NOTWORD;
2307             }
2308           tree_last = create_token_tree (dfa, NULL, NULL, token);
2309           tree = create_tree (dfa, tree_first, tree_last, OP_ALT);
2310           if (BE (tree_first == NULL || tree_last == NULL || tree == NULL, 0))
2311             {
2312               *err = REG_ESPACE;
2313               return NULL;
2314             }
2315         }
2316       else
2317         {
2318           tree = create_token_tree (dfa, NULL, NULL, token);
2319           if (BE (tree == NULL, 0))
2320             {
2321               *err = REG_ESPACE;
2322               return NULL;
2323             }
2324         }
2325       /* We must return here, since ANCHORs can't be followed
2326          by repetition operators.
2327          eg. RE"^*" is invalid or "<ANCHOR(^)><CHAR(*)>",
2328              it must not be "<ANCHOR(^)><REPEAT(*)>".  */
2329       fetch_token (token, regexp, syntax);
2330       return tree;
2331     case OP_PERIOD:
2332       tree = create_token_tree (dfa, NULL, NULL, token);
2333       if (BE (tree == NULL, 0))
2334         {
2335           *err = REG_ESPACE;
2336           return NULL;
2337         }
2338       if (dfa->mb_cur_max > 1)
2339         dfa->has_mb_node = 1;
2340       break;
2341     case OP_WORD:
2342     case OP_NOTWORD:
2343       tree = build_charclass_op (dfa, regexp->trans,
2344                                  (const unsigned char *) "alnum",
2345                                  (const unsigned char *) "_",
2346                                  token->type == OP_NOTWORD, err);
2347       if (BE (*err != REG_NOERROR && tree == NULL, 0))
2348         return NULL;
2349       break;
2350     case OP_SPACE:
2351     case OP_NOTSPACE:
2352       tree = build_charclass_op (dfa, regexp->trans,
2353                                  (const unsigned char *) "space",
2354                                  (const unsigned char *) "",
2355                                  token->type == OP_NOTSPACE, err);
2356       if (BE (*err != REG_NOERROR && tree == NULL, 0))
2357         return NULL;
2358       break;
2359     case OP_ALT:
2360     case END_OF_RE:
2361       return NULL;
2362     case BACK_SLASH:
2363       *err = REG_EESCAPE;
2364       return NULL;
2365     default:
2366       /* Must not happen?  */
2367 #ifdef DEBUG
2368       assert (0);
2369 #endif
2370       return NULL;
2371     }
2372   fetch_token (token, regexp, syntax);
2373
2374   while (token->type == OP_DUP_ASTERISK || token->type == OP_DUP_PLUS
2375          || token->type == OP_DUP_QUESTION || token->type == OP_OPEN_DUP_NUM)
2376     {
2377       tree = parse_dup_op (tree, regexp, dfa, token, syntax, err);
2378       if (BE (*err != REG_NOERROR && tree == NULL, 0))
2379         return NULL;
2380       /* In BRE consecutive duplications are not allowed.  */
2381       if ((syntax & REG_CONTEXT_INVALID_DUP)
2382           && (token->type == OP_DUP_ASTERISK
2383               || token->type == OP_OPEN_DUP_NUM))
2384         {
2385           *err = REG_BADRPT;
2386           return NULL;
2387         }
2388     }
2389
2390   return tree;
2391 }
2392
2393 /* This function build the following tree, from regular expression
2394    (<reg_exp>):
2395          SUBEXP
2396             |
2397         <reg_exp>
2398 */
2399
2400 static bin_tree_t *
2401 parse_sub_exp (re_string_t *regexp, regex_t *preg, re_token_t *token,
2402                reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
2403 {
2404   re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
2405   bin_tree_t *tree;
2406   size_t cur_nsub;
2407   cur_nsub = preg->re_nsub++;
2408
2409   fetch_token (token, regexp, syntax | REG_CARET_ANCHORS_HERE);
2410
2411   /* The subexpression may be a null string.  */
2412   if (token->type == OP_CLOSE_SUBEXP)
2413     tree = NULL;
2414   else
2415     {
2416       tree = parse_reg_exp (regexp, preg, token, syntax, nest, err);
2417       if (BE (*err == REG_NOERROR && token->type != OP_CLOSE_SUBEXP, 0))
2418         *err = REG_EPAREN;
2419       if (BE (*err != REG_NOERROR, 0))
2420         return NULL;
2421     }
2422
2423   if (cur_nsub <= '9' - '1')
2424     dfa->completed_bkref_map |= 1 << cur_nsub;
2425
2426   tree = create_tree (dfa, tree, NULL, SUBEXP);
2427   if (BE (tree == NULL, 0))
2428     {
2429       *err = REG_ESPACE;
2430       return NULL;
2431     }
2432   tree->token.opr.idx = cur_nsub;
2433   return tree;
2434 }
2435
2436 /* This function parse repetition operators like "*", "+", "{1,3}" etc.  */
2437
2438 static bin_tree_t *
2439 parse_dup_op (bin_tree_t *elem, re_string_t *regexp, re_dfa_t *dfa,
2440               re_token_t *token, reg_syntax_t syntax, reg_errcode_t *err)
2441 {
2442   bin_tree_t *tree = NULL, *old_tree = NULL;
2443   Idx i, start, end, start_idx = re_string_cur_idx (regexp);
2444   re_token_t start_token = *token;
2445
2446   if (token->type == OP_OPEN_DUP_NUM)
2447     {
2448       end = 0;
2449       start = fetch_number (regexp, token, syntax);
2450       if (start == REG_MISSING)
2451         {
2452           if (token->type == CHARACTER && token->opr.c == ',')
2453             start = 0; /* We treat "{,m}" as "{0,m}".  */
2454           else
2455             {
2456               *err = REG_BADBR; /* <re>{} is invalid.  */
2457               return NULL;
2458             }
2459         }
2460       if (BE (start != REG_ERROR, 1))
2461         {
2462           /* We treat "{n}" as "{n,n}".  */
2463           end = ((token->type == OP_CLOSE_DUP_NUM) ? start
2464                  : ((token->type == CHARACTER && token->opr.c == ',')
2465                     ? fetch_number (regexp, token, syntax) : REG_ERROR));
2466         }
2467       if (BE (start == REG_ERROR || end == REG_ERROR, 0))
2468         {
2469           /* Invalid sequence.  */
2470           if (BE (!(syntax & REG_INVALID_INTERVAL_ORD), 0))
2471             {
2472               if (token->type == END_OF_RE)
2473                 *err = REG_EBRACE;
2474               else
2475                 *err = REG_BADBR;
2476
2477               return NULL;
2478             }
2479
2480           /* If the syntax bit is set, rollback.  */
2481           re_string_set_index (regexp, start_idx);
2482           *token = start_token;
2483           token->type = CHARACTER;
2484           /* mb_partial and word_char bits should be already initialized by
2485              peek_token.  */
2486           return elem;
2487         }
2488
2489       if (BE (end != REG_MISSING && start > end, 0))
2490         {
2491           /* First number greater than second.  */
2492           *err = REG_BADBR;
2493           return NULL;
2494         }
2495     }
2496   else
2497     {
2498       start = (token->type == OP_DUP_PLUS) ? 1 : 0;
2499       end = (token->type == OP_DUP_QUESTION) ? 1 : REG_MISSING;
2500     }
2501
2502   fetch_token (token, regexp, syntax);
2503
2504   if (BE (elem == NULL, 0))
2505     return NULL;
2506   if (BE (start == 0 && end == 0, 0))
2507     {
2508       postorder (elem, free_tree, NULL);
2509       return NULL;
2510     }
2511
2512   /* Extract "<re>{n,m}" to "<re><re>...<re><re>{0,<m-n>}".  */
2513   if (BE (start > 0, 0))
2514     {
2515       tree = elem;
2516       for (i = 2; i <= start; ++i)
2517         {
2518           elem = duplicate_tree (elem, dfa);
2519           tree = create_tree (dfa, tree, elem, CONCAT);
2520           if (BE (elem == NULL || tree == NULL, 0))
2521             goto parse_dup_op_espace;
2522         }
2523
2524       if (start == end)
2525         return tree;
2526
2527       /* Duplicate ELEM before it is marked optional.  */
2528       elem = duplicate_tree (elem, dfa);
2529       old_tree = tree;
2530     }
2531   else
2532     old_tree = NULL;
2533
2534   if (elem->token.type == SUBEXP)
2535     postorder (elem, mark_opt_subexp, (void *) (long) elem->token.opr.idx);
2536
2537   tree = create_tree (dfa, elem, NULL,
2538                       (end == REG_MISSING ? OP_DUP_ASTERISK : OP_ALT));
2539   if (BE (tree == NULL, 0))
2540     goto parse_dup_op_espace;
2541
2542   /* This loop is actually executed only when end != REG_MISSING,
2543      to rewrite <re>{0,n} as (<re>(<re>...<re>?)?)?...  We have
2544      already created the start+1-th copy.  */
2545   if ((Idx) -1 < 0 || end != REG_MISSING)
2546     for (i = start + 2; i <= end; ++i)
2547       {
2548         elem = duplicate_tree (elem, dfa);
2549         tree = create_tree (dfa, tree, elem, CONCAT);
2550         if (BE (elem == NULL || tree == NULL, 0))
2551           goto parse_dup_op_espace;
2552
2553         tree = create_tree (dfa, tree, NULL, OP_ALT);
2554         if (BE (tree == NULL, 0))
2555           goto parse_dup_op_espace;
2556       }
2557
2558   if (old_tree)
2559     tree = create_tree (dfa, old_tree, tree, CONCAT);
2560
2561   return tree;
2562
2563  parse_dup_op_espace:
2564   *err = REG_ESPACE;
2565   return NULL;
2566 }
2567
2568 /* Size of the names for collating symbol/equivalence_class/character_class.
2569    I'm not sure, but maybe enough.  */
2570 #define BRACKET_NAME_BUF_SIZE 32
2571
2572 #ifndef _LIBC
2573   /* Local function for parse_bracket_exp only used in case of NOT _LIBC.
2574      Build the range expression which starts from START_ELEM, and ends
2575      at END_ELEM.  The result are written to MBCSET and SBCSET.
2576      RANGE_ALLOC is the allocated size of mbcset->range_starts, and
2577      mbcset->range_ends, is a pointer argument sinse we may
2578      update it.  */
2579
2580 static reg_errcode_t
2581 build_range_exp (bitset sbcset,
2582 # ifdef RE_ENABLE_I18N
2583                  re_charset_t *mbcset, Idx *range_alloc,
2584 # endif
2585                  bracket_elem_t *start_elem, bracket_elem_t *end_elem)
2586 {
2587   unsigned int start_ch, end_ch;
2588   /* Equivalence Classes and Character Classes can't be a range start/end.  */
2589   if (BE (start_elem->type == EQUIV_CLASS || start_elem->type == CHAR_CLASS
2590           || end_elem->type == EQUIV_CLASS || end_elem->type == CHAR_CLASS,
2591           0))
2592     return REG_ERANGE;
2593
2594   /* We can handle no multi character collating elements without libc
2595      support.  */
2596   if (BE ((start_elem->type == COLL_SYM
2597            && strlen ((char *) start_elem->opr.name) > 1)
2598           || (end_elem->type == COLL_SYM
2599               && strlen ((char *) end_elem->opr.name) > 1), 0))
2600     return REG_ECOLLATE;
2601
2602 # ifdef RE_ENABLE_I18N
2603   {
2604     wchar_t wc;
2605     wint_t start_wc, end_wc;
2606     wchar_t cmp_buf[6] = {L'\0', L'\0', L'\0', L'\0', L'\0', L'\0'};
2607
2608     start_ch = ((start_elem->type == SB_CHAR) ? start_elem->opr.ch
2609                 : ((start_elem->type == COLL_SYM) ? start_elem->opr.name[0]
2610                    : 0));
2611     end_ch = ((end_elem->type == SB_CHAR) ? end_elem->opr.ch
2612               : ((end_elem->type == COLL_SYM) ? end_elem->opr.name[0]
2613                  : 0));
2614     start_wc = ((start_elem->type == SB_CHAR || start_elem->type == COLL_SYM)
2615                 ? __btowc (start_ch) : start_elem->opr.wch);
2616     end_wc = ((end_elem->type == SB_CHAR || end_elem->type == COLL_SYM)
2617               ? __btowc (end_ch) : end_elem->opr.wch);
2618     if (start_wc == WEOF || end_wc == WEOF)
2619       return REG_ECOLLATE;
2620     cmp_buf[0] = start_wc;
2621     cmp_buf[4] = end_wc;
2622     if (wcscoll (cmp_buf, cmp_buf + 4) > 0)
2623       return REG_ERANGE;
2624
2625     /* Got valid collation sequence values, add them as a new entry.
2626        However, for !_LIBC we have no collation elements: if the
2627        character set is single byte, the single byte character set
2628        that we build below suffices.  parse_bracket_exp passes
2629        no MBCSET if dfa->mb_cur_max == 1.  */
2630     if (mbcset)
2631       {
2632         /* Check the space of the arrays.  */
2633         if (BE (*range_alloc == mbcset->nranges, 0))
2634           {
2635             /* There is not enough space, need realloc.  */
2636             wchar_t *new_array_start, *new_array_end;
2637             Idx new_nranges;
2638
2639             new_nranges = mbcset->nranges;
2640             /* Use realloc since mbcset->range_starts and mbcset->range_ends
2641                are NULL if *range_alloc == 0.  */
2642             new_array_start = re_x2realloc (mbcset->range_starts, wchar_t,
2643                                             &new_nranges);
2644             new_array_end = re_realloc (mbcset->range_ends, wchar_t,
2645                                         new_nranges);
2646
2647             if (BE (new_array_start == NULL || new_array_end == NULL, 0))
2648               return REG_ESPACE;
2649
2650             mbcset->range_starts = new_array_start;
2651             mbcset->range_ends = new_array_end;
2652             *range_alloc = new_nranges;
2653           }
2654
2655         mbcset->range_starts[mbcset->nranges] = start_wc;
2656         mbcset->range_ends[mbcset->nranges++] = end_wc;
2657       }
2658
2659     /* Build the table for single byte characters.  */
2660     for (wc = 0; wc < SBC_MAX; ++wc)
2661       {
2662         cmp_buf[2] = wc;
2663         if (wcscoll (cmp_buf, cmp_buf + 2) <= 0
2664             && wcscoll (cmp_buf + 2, cmp_buf + 4) <= 0)
2665           bitset_set (sbcset, wc);
2666       }
2667   }
2668 # else /* not RE_ENABLE_I18N */
2669   {
2670     unsigned int ch;
2671     start_ch = ((start_elem->type == SB_CHAR ) ? start_elem->opr.ch
2672                 : ((start_elem->type == COLL_SYM) ? start_elem->opr.name[0]
2673                    : 0));
2674     end_ch = ((end_elem->type == SB_CHAR ) ? end_elem->opr.ch
2675               : ((end_elem->type == COLL_SYM) ? end_elem->opr.name[0]
2676                  : 0));
2677     if (start_ch > end_ch)
2678       return REG_ERANGE;
2679     /* Build the table for single byte characters.  */
2680     for (ch = 0; ch < SBC_MAX; ++ch)
2681       if (start_ch <= ch  && ch <= end_ch)
2682         bitset_set (sbcset, ch);
2683   }
2684 # endif /* not RE_ENABLE_I18N */
2685   return REG_NOERROR;
2686 }
2687 #endif /* not _LIBC */
2688
2689 #ifndef _LIBC
2690 /* Helper function for parse_bracket_exp only used in case of NOT _LIBC..
2691    Build the collating element which is represented by NAME.
2692    The result are written to MBCSET and SBCSET.
2693    COLL_SYM_ALLOC is the allocated size of mbcset->coll_sym, is a
2694    pointer argument since we may update it.  */
2695
2696 static reg_errcode_t
2697 build_collating_symbol (bitset sbcset,
2698 # ifdef RE_ENABLE_I18N
2699                         re_charset_t *mbcset, Idx *coll_sym_alloc,
2700 # endif
2701                         const unsigned char *name)
2702 {
2703   size_t name_len = strlen ((const char *) name);
2704   if (BE (name_len != 1, 0))
2705     return REG_ECOLLATE;
2706   else
2707     {
2708       bitset_set (sbcset, name[0]);
2709       return REG_NOERROR;
2710     }
2711 }
2712 #endif /* not _LIBC */
2713
2714 /* This function parse bracket expression like "[abc]", "[a-c]",
2715    "[[.a-a.]]" etc.  */
2716
2717 static bin_tree_t *
2718 parse_bracket_exp (re_string_t *regexp, re_dfa_t *dfa, re_token_t *token,
2719                    reg_syntax_t syntax, reg_errcode_t *err)
2720 {
2721 #ifdef _LIBC
2722   const unsigned char *collseqmb;
2723   const char *collseqwc;
2724   uint32_t nrules;
2725   int32_t table_size;
2726   const int32_t *symb_table;
2727   const unsigned char *extra;
2728
2729   /* Local function for parse_bracket_exp used in _LIBC environement.
2730      Seek the collating symbol entry correspondings to NAME.
2731      Return the index of the symbol in the SYMB_TABLE.  */
2732
2733   auto inline int32_t
2734   __attribute ((always_inline))
2735   seek_collating_symbol_entry (const unsigned char *name, size_t name_len)
2736     {
2737       int32_t hash = elem_hash ((const char *) name, name_len);
2738       int32_t elem = hash % table_size;
2739       int32_t second = hash % (table_size - 2);
2740       while (symb_table[2 * elem] != 0)
2741         {
2742           /* First compare the hashing value.  */
2743           if (symb_table[2 * elem] == hash
2744               /* Compare the length of the name.  */
2745               && name_len == extra[symb_table[2 * elem + 1]]
2746               /* Compare the name.  */
2747               && memcmp (name, &extra[symb_table[2 * elem + 1] + 1],
2748                          name_len) == 0)
2749             {
2750               /* Yep, this is the entry.  */
2751               break;
2752             }
2753
2754           /* Next entry.  */
2755           elem += second;
2756         }
2757       return elem;
2758     }
2759
2760   /* Local function for parse_bracket_exp used in _LIBC environement.
2761      Look up the collation sequence value of BR_ELEM.
2762      Return the value if succeeded, UINT_MAX otherwise.  */
2763
2764   auto inline unsigned int
2765   __attribute ((always_inline))
2766   lookup_collation_sequence_value (bracket_elem_t *br_elem)
2767     {
2768       if (br_elem->type == SB_CHAR)
2769         {
2770           /*
2771           if (MB_CUR_MAX == 1)
2772           */
2773           if (nrules == 0)
2774             return collseqmb[br_elem->opr.ch];
2775           else
2776             {
2777               wint_t wc = __btowc (br_elem->opr.ch);
2778               return __collseq_table_lookup (collseqwc, wc);
2779             }
2780         }
2781       else if (br_elem->type == MB_CHAR)
2782         {
2783           return __collseq_table_lookup (collseqwc, br_elem->opr.wch);
2784         }
2785       else if (br_elem->type == COLL_SYM)
2786         {
2787           size_t sym_name_len = strlen ((char *) br_elem->opr.name);
2788           if (nrules != 0)
2789             {
2790               int32_t elem, idx;
2791               elem = seek_collating_symbol_entry (br_elem->opr.name,
2792                                                   sym_name_len);
2793               if (symb_table[2 * elem] != 0)
2794                 {
2795                   /* We found the entry.  */
2796                   idx = symb_table[2 * elem + 1];
2797                   /* Skip the name of collating element name.  */
2798                   idx += 1 + extra[idx];
2799                   /* Skip the byte sequence of the collating element.  */
2800                   idx += 1 + extra[idx];
2801                   /* Adjust for the alignment.  */
2802                   idx = (idx + 3) & ~3;
2803                   /* Skip the multibyte collation sequence value.  */
2804                   idx += sizeof (unsigned int);
2805                   /* Skip the wide char sequence of the collating element.  */
2806                   idx += sizeof (unsigned int) *
2807                     (1 + *(unsigned int *) (extra + idx));
2808                   /* Return the collation sequence value.  */
2809                   return *(unsigned int *) (extra + idx);
2810                 }
2811               else if (symb_table[2 * elem] == 0 && sym_name_len == 1)
2812                 {
2813                   /* No valid character.  Match it as a single byte
2814                      character.  */
2815                   return collseqmb[br_elem->opr.name[0]];
2816                 }
2817             }
2818           else if (sym_name_len == 1)
2819             return collseqmb[br_elem->opr.name[0]];
2820         }
2821       return UINT_MAX;
2822     }
2823
2824   /* Local function for parse_bracket_exp used in _LIBC environement.
2825      Build the range expression which starts from START_ELEM, and ends
2826      at END_ELEM.  The result are written to MBCSET and SBCSET.
2827      RANGE_ALLOC is the allocated size of mbcset->range_starts, and
2828      mbcset->range_ends, is a pointer argument sinse we may
2829      update it.  */
2830
2831   auto inline reg_errcode_t
2832   __attribute ((always_inline))
2833   build_range_exp (bitset sbcset, re_charset_t *mbcset,
2834                    Idx *range_alloc,
2835                    bracket_elem_t *start_elem, bracket_elem_t *end_elem)
2836     {
2837       unsigned int ch;
2838       uint32_t start_collseq;
2839       uint32_t end_collseq;
2840
2841       /* Equivalence Classes and Character Classes can't be a range
2842          start/end.  */
2843       if (BE (start_elem->type == EQUIV_CLASS || start_elem->type == CHAR_CLASS
2844               || end_elem->type == EQUIV_CLASS || end_elem->type == CHAR_CLASS,
2845               0))
2846         return REG_ERANGE;
2847
2848       start_collseq = lookup_collation_sequence_value (start_elem);
2849       end_collseq = lookup_collation_sequence_value (end_elem);
2850       /* Check start/end collation sequence values.  */
2851       if (BE (start_collseq == UINT_MAX || end_collseq == UINT_MAX, 0))
2852         return REG_ECOLLATE;
2853       if (BE ((syntax & REG_NO_EMPTY_RANGES) && start_collseq > end_collseq, 0))
2854         return REG_ERANGE;
2855
2856       /* Got valid collation sequence values, add them as a new entry.
2857          However, if we have no collation elements, and the character set
2858          is single byte, the single byte character set that we
2859          build below suffices. */
2860       if (nrules > 0 || dfa->mb_cur_max > 1)
2861         {
2862           /* Check the space of the arrays.  */
2863           if (BE (*range_alloc == mbcset->nranges, 0))
2864             {
2865               /* There is not enough space, need realloc.  */
2866               uint32_t *new_array_start;
2867               uint32_t *new_array_end;
2868               Idx new_nranges;
2869
2870               new_nranges = mbcset->nranges;
2871               new_array_start = re_x2realloc (mbcset->range_starts, uint32_t,
2872                                               &new_nranges);
2873               new_array_end = re_realloc (mbcset->range_ends, uint32_t,
2874                                           new_nranges);
2875
2876               if (BE (new_array_start == NULL || new_array_end == NULL, 0))
2877                 return REG_ESPACE;
2878
2879               mbcset->range_starts = new_array_start;
2880               mbcset->range_ends = new_array_end;
2881               *range_alloc = new_nranges;
2882             }
2883
2884           mbcset->range_starts[mbcset->nranges] = start_collseq;
2885           mbcset->range_ends[mbcset->nranges++] = end_collseq;
2886         }
2887
2888       /* Build the table for single byte characters.  */
2889       for (ch = 0; ch < SBC_MAX; ch++)
2890         {
2891           uint32_t ch_collseq;
2892           /*
2893           if (MB_CUR_MAX == 1)
2894           */
2895           if (nrules == 0)
2896             ch_collseq = collseqmb[ch];
2897           else
2898             ch_collseq = __collseq_table_lookup (collseqwc, __btowc (ch));
2899           if (start_collseq <= ch_collseq && ch_collseq <= end_collseq)
2900             bitset_set (sbcset, ch);
2901         }
2902       return REG_NOERROR;
2903     }
2904
2905   /* Local function for parse_bracket_exp used in _LIBC environement.
2906      Build the collating element which is represented by NAME.
2907      The result are written to MBCSET and SBCSET.
2908      COLL_SYM_ALLOC is the allocated size of mbcset->coll_sym, is a
2909      pointer argument sinse we may update it.  */
2910
2911   auto inline reg_errcode_t
2912   __attribute ((always_inline))
2913   build_collating_symbol (bitset sbcset, re_charset_t *mbcset,
2914                           Idx *coll_sym_alloc, const unsigned char *name)
2915     {
2916       int32_t elem, idx;
2917       size_t name_len = strlen ((const char *) name);
2918       if (nrules != 0)
2919         {
2920           elem = seek_collating_symbol_entry (name, name_len);
2921           if (symb_table[2 * elem] != 0)
2922             {
2923               /* We found the entry.  */
2924               idx = symb_table[2 * elem + 1];
2925               /* Skip the name of collating element name.  */
2926               idx += 1 + extra[idx];
2927             }
2928           else if (symb_table[2 * elem] == 0 && name_len == 1)
2929             {
2930               /* No valid character, treat it as a normal
2931                  character.  */
2932               bitset_set (sbcset, name[0]);
2933               return REG_NOERROR;
2934             }
2935           else
2936             return REG_ECOLLATE;
2937
2938           /* Got valid collation sequence, add it as a new entry.  */
2939           /* Check the space of the arrays.  */
2940           if (BE (*coll_sym_alloc == mbcset->ncoll_syms, 0))
2941             {
2942               /* Not enough, realloc it.  */
2943               Idx new_coll_sym_alloc = mbcset->ncoll_syms;
2944               /* Use realloc since mbcset->coll_syms is NULL
2945                  if *alloc == 0.  */
2946               int32_t *new_coll_syms = re_x2realloc (mbcset->coll_syms, int32_t,
2947                                                      &new_coll_sym_alloc);
2948               if (BE (new_coll_syms == NULL, 0))
2949                 return REG_ESPACE;
2950               mbcset->coll_syms = new_coll_syms;
2951               *coll_sym_alloc = new_coll_sym_alloc;
2952             }
2953           mbcset->coll_syms[mbcset->ncoll_syms++] = idx;
2954           return REG_NOERROR;
2955         }
2956       else
2957         {
2958           if (BE (name_len != 1, 0))
2959             return REG_ECOLLATE;
2960           else
2961             {
2962               bitset_set (sbcset, name[0]);
2963               return REG_NOERROR;
2964             }
2965         }
2966     }
2967 #endif
2968
2969   re_token_t br_token;
2970   re_bitset_ptr_t sbcset;
2971 #ifdef RE_ENABLE_I18N
2972   re_charset_t *mbcset;
2973   Idx coll_sym_alloc = 0, range_alloc = 0, mbchar_alloc = 0;
2974   Idx equiv_class_alloc = 0, char_class_alloc = 0;
2975 #endif /* not RE_ENABLE_I18N */
2976   bool non_match = false;
2977   bin_tree_t *work_tree;
2978   int token_len;
2979   bool first_round = true;
2980 #ifdef _LIBC
2981   collseqmb = (const unsigned char *)
2982     _NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQMB);
2983   nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
2984   if (nrules)
2985     {
2986       /*
2987       if (MB_CUR_MAX > 1)
2988       */
2989         collseqwc = _NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQWC);
2990       table_size = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_SYMB_HASH_SIZEMB);
2991       symb_table = (const int32_t *) _NL_CURRENT (LC_COLLATE,
2992                                                   _NL_COLLATE_SYMB_TABLEMB);
2993       extra = (const unsigned char *) _NL_CURRENT (LC_COLLATE,
2994                                                    _NL_COLLATE_SYMB_EXTRAMB);
2995     }
2996 #endif
2997   sbcset = re_calloc (bitset_word, BITSET_WORDS);
2998 #ifdef RE_ENABLE_I18N
2999   mbcset = re_calloc (re_charset_t, 1);
3000 #endif /* RE_ENABLE_I18N */
3001 #ifdef RE_ENABLE_I18N
3002   if (BE (sbcset == NULL || mbcset == NULL, 0))
3003 #else
3004   if (BE (sbcset == NULL, 0))
3005 #endif /* RE_ENABLE_I18N */
3006     {
3007       *err = REG_ESPACE;
3008       return NULL;
3009     }
3010
3011   token_len = peek_token_bracket (token, regexp, syntax);
3012   if (BE (token->type == END_OF_RE, 0))
3013     {
3014       *err = REG_BADPAT;
3015       goto parse_bracket_exp_free_return;
3016     }
3017   if (token->type == OP_NON_MATCH_LIST)
3018     {
3019 #ifdef RE_ENABLE_I18N
3020       mbcset->non_match = 1;
3021 #endif /* not RE_ENABLE_I18N */
3022       non_match = true;
3023       if (syntax & REG_HAT_LISTS_NOT_NEWLINE)
3024         bitset_set (sbcset, '\0');
3025       re_string_skip_bytes (regexp, token_len); /* Skip a token.  */
3026       token_len = peek_token_bracket (token, regexp, syntax);
3027       if (BE (token->type == END_OF_RE, 0))
3028         {
3029           *err = REG_BADPAT;
3030           goto parse_bracket_exp_free_return;
3031         }
3032     }
3033
3034   /* We treat the first ']' as a normal character.  */
3035   if (token->type == OP_CLOSE_BRACKET)
3036     token->type = CHARACTER;
3037
3038   while (1)
3039     {
3040       bracket_elem_t start_elem, end_elem;
3041       unsigned char start_name_buf[BRACKET_NAME_BUF_SIZE];
3042       unsigned char end_name_buf[BRACKET_NAME_BUF_SIZE];
3043       reg_errcode_t ret;
3044       int token_len2 = 0;
3045       bool is_range_exp = false;
3046       re_token_t token2;
3047
3048       start_elem.opr.name = start_name_buf;
3049       ret = parse_bracket_element (&start_elem, regexp, token, token_len, dfa,
3050                                    syntax, first_round);
3051       if (BE (ret != REG_NOERROR, 0))
3052         {
3053           *err = ret;
3054           goto parse_bracket_exp_free_return;
3055         }
3056       first_round = false;
3057
3058       /* Get information about the next token.  We need it in any case.  */
3059       token_len = peek_token_bracket (token, regexp, syntax);
3060
3061       /* Do not check for ranges if we know they are not allowed.  */
3062       if (start_elem.type != CHAR_CLASS && start_elem.type != EQUIV_CLASS)
3063         {
3064           if (BE (token->type == END_OF_RE, 0))
3065             {
3066               *err = REG_EBRACK;
3067               goto parse_bracket_exp_free_return;
3068             }
3069           if (token->type == OP_CHARSET_RANGE)
3070             {
3071               re_string_skip_bytes (regexp, token_len); /* Skip '-'.  */
3072               token_len2 = peek_token_bracket (&token2, regexp, syntax);
3073               if (BE (token2.type == END_OF_RE, 0))
3074                 {
3075                   *err = REG_EBRACK;
3076                   goto parse_bracket_exp_free_return;
3077                 }
3078               if (token2.type == OP_CLOSE_BRACKET)
3079                 {
3080                   /* We treat the last '-' as a normal character.  */
3081                   re_string_skip_bytes (regexp, -token_len);
3082                   token->type = CHARACTER;
3083                 }
3084               else
3085                 is_range_exp = true;
3086             }
3087         }
3088
3089       if (is_range_exp == true)
3090         {
3091           end_elem.opr.name = end_name_buf;
3092           ret = parse_bracket_element (&end_elem, regexp, &token2, token_len2,
3093                                        dfa, syntax, true);
3094           if (BE (ret != REG_NOERROR, 0))
3095             {
3096               *err = ret;
3097               goto parse_bracket_exp_free_return;
3098             }
3099
3100           token_len = peek_token_bracket (token, regexp, syntax);
3101
3102 #ifdef _LIBC
3103           *err = build_range_exp (sbcset, mbcset, &range_alloc,
3104                                   &start_elem, &end_elem);
3105 #else
3106 # ifdef RE_ENABLE_I18N
3107           *err = build_range_exp (sbcset,
3108                                   dfa->mb_cur_max > 1 ? mbcset : NULL,
3109                                   &range_alloc, &start_elem, &end_elem);
3110 # else
3111           *err = build_range_exp (sbcset, &start_elem, &end_elem);
3112 # endif
3113 #endif /* RE_ENABLE_I18N */
3114           if (BE (*err != REG_NOERROR, 0))
3115             goto parse_bracket_exp_free_return;
3116         }
3117       else
3118         {
3119           switch (start_elem.type)
3120             {
3121             case SB_CHAR:
3122               bitset_set (sbcset, start_elem.opr.ch);
3123               break;
3124 #ifdef RE_ENABLE_I18N
3125             case MB_CHAR:
3126               /* Check whether the array has enough space.  */
3127               if (BE (mbchar_alloc == mbcset->nmbchars, 0))
3128                 {
3129                   wchar_t *new_mbchars;
3130                   /* Not enough, realloc it.  */
3131                   mbchar_alloc = mbcset->nmbchars;
3132                   /* Use realloc since array is NULL if *alloc == 0.  */
3133                   new_mbchars = re_x2realloc (mbcset->mbchars, wchar_t,
3134                                               &mbchar_alloc);
3135                   if (BE (new_mbchars == NULL, 0))
3136                     goto parse_bracket_exp_espace;
3137                   mbcset->mbchars = new_mbchars;
3138                 }
3139               mbcset->mbchars[mbcset->nmbchars++] = start_elem.opr.wch;
3140               break;
3141 #endif /* RE_ENABLE_I18N */
3142             case EQUIV_CLASS:
3143               *err = build_equiv_class (sbcset,
3144 #ifdef RE_ENABLE_I18N
3145                                         mbcset, &equiv_class_alloc,
3146 #endif /* RE_ENABLE_I18N */
3147                                         start_elem.opr.name);
3148               if (BE (*err != REG_NOERROR, 0))
3149                 goto parse_bracket_exp_free_return;
3150               break;
3151             case COLL_SYM:
3152               *err = build_collating_symbol (sbcset,
3153 #ifdef RE_ENABLE_I18N
3154                                              mbcset, &coll_sym_alloc,
3155 #endif /* RE_ENABLE_I18N */
3156                                              start_elem.opr.name);
3157               if (BE (*err != REG_NOERROR, 0))
3158                 goto parse_bracket_exp_free_return;
3159               break;
3160             case CHAR_CLASS:
3161               *err = build_charclass (regexp->trans, sbcset,
3162 #ifdef RE_ENABLE_I18N
3163                                       mbcset, &char_class_alloc,
3164 #endif /* RE_ENABLE_I18N */
3165                                       start_elem.opr.name, syntax);
3166               if (BE (*err != REG_NOERROR, 0))
3167                goto parse_bracket_exp_free_return;
3168               break;
3169             default:
3170               assert (0);
3171               break;
3172             }
3173         }
3174       if (BE (token->type == END_OF_RE, 0))
3175         {
3176           *err = REG_EBRACK;
3177           goto parse_bracket_exp_free_return;
3178         }
3179       if (token->type == OP_CLOSE_BRACKET)
3180         break;
3181     }
3182
3183   re_string_skip_bytes (regexp, token_len); /* Skip a token.  */
3184
3185   /* If it is non-matching list.  */
3186   if (non_match)
3187     bitset_not (sbcset);
3188
3189 #ifdef RE_ENABLE_I18N
3190   /* Ensure only single byte characters are set.  */
3191   if (dfa->mb_cur_max > 1)
3192     bitset_mask (sbcset, dfa->sb_char);
3193
3194   if (mbcset->nmbchars || mbcset->ncoll_syms || mbcset->nequiv_classes
3195       || mbcset->nranges || (dfa->mb_cur_max > 1 && (mbcset->nchar_classes
3196                                                      || mbcset->non_match)))
3197     {
3198       bin_tree_t *mbc_tree;
3199       int sbc_idx;
3200       /* Build a tree for complex bracket.  */
3201       dfa->has_mb_node = 1;
3202       br_token.type = COMPLEX_BRACKET;
3203       br_token.opr.mbcset = mbcset;
3204       mbc_tree = create_token_tree (dfa, NULL, NULL, &br_token);
3205       if (BE (mbc_tree == NULL, 0))
3206         goto parse_bracket_exp_espace;
3207       for (sbc_idx = 0; sbc_idx < BITSET_WORDS; ++sbc_idx)
3208         if (sbcset[sbc_idx])
3209           break;
3210       /* If there are no bits set in sbcset, there is no point
3211          of having both SIMPLE_BRACKET and COMPLEX_BRACKET.  */
3212       if (sbc_idx < BITSET_WORDS)
3213         {
3214           /* Build a tree for simple bracket.  */
3215           br_token.type = SIMPLE_BRACKET;
3216           br_token.opr.sbcset = sbcset;
3217           work_tree = create_token_tree (dfa, NULL, NULL, &br_token);
3218           if (BE (work_tree == NULL, 0))
3219             goto parse_bracket_exp_espace;
3220
3221           /* Then join them by ALT node.  */
3222           work_tree = create_tree (dfa, work_tree, mbc_tree, OP_ALT);
3223           if (BE (work_tree == NULL, 0))
3224             goto parse_bracket_exp_espace;
3225         }
3226       else
3227         {
3228           re_free (sbcset);
3229           work_tree = mbc_tree;
3230         }
3231     }
3232   else
3233 #endif /* not RE_ENABLE_I18N */
3234     {
3235 #ifdef RE_ENABLE_I18N
3236       free_charset (mbcset);
3237 #endif
3238       /* Build a tree for simple bracket.  */
3239       br_token.type = SIMPLE_BRACKET;
3240       br_token.opr.sbcset = sbcset;
3241       work_tree = create_token_tree (dfa, NULL, NULL, &br_token);
3242       if (BE (work_tree == NULL, 0))
3243         goto parse_bracket_exp_espace;
3244     }
3245   return work_tree;
3246
3247  parse_bracket_exp_espace:
3248   *err = REG_ESPACE;
3249  parse_bracket_exp_free_return:
3250   re_free (sbcset);
3251 #ifdef RE_ENABLE_I18N
3252   free_charset (mbcset);
3253 #endif /* RE_ENABLE_I18N */
3254   return NULL;
3255 }
3256
3257 /* Parse an element in the bracket expression.  */
3258
3259 static reg_errcode_t
3260 parse_bracket_element (bracket_elem_t *elem, re_string_t *regexp,
3261                        re_token_t *token, int token_len, re_dfa_t *dfa,
3262                        reg_syntax_t syntax, bool accept_hyphen)
3263 {
3264 #ifdef RE_ENABLE_I18N
3265   int cur_char_size;
3266   cur_char_size = re_string_char_size_at (regexp, re_string_cur_idx (regexp));
3267   if (cur_char_size > 1)
3268     {
3269       elem->type = MB_CHAR;
3270       elem->opr.wch = re_string_wchar_at (regexp, re_string_cur_idx (regexp));
3271       re_string_skip_bytes (regexp, cur_char_size);
3272       return REG_NOERROR;
3273     }
3274 #endif /* RE_ENABLE_I18N */
3275   re_string_skip_bytes (regexp, token_len); /* Skip a token.  */
3276   if (token->type == OP_OPEN_COLL_ELEM || token->type == OP_OPEN_CHAR_CLASS
3277       || token->type == OP_OPEN_EQUIV_CLASS)
3278     return parse_bracket_symbol (elem, regexp, token);
3279   if (BE (token->type == OP_CHARSET_RANGE, 0) && !accept_hyphen)
3280     {
3281       /* A '-' must only appear as anything but a range indicator before
3282          the closing bracket.  Everything else is an error.  */
3283       re_token_t token2;
3284       (void) peek_token_bracket (&token2, regexp, syntax);
3285       if (token2.type != OP_CLOSE_BRACKET)
3286         /* The actual error value is not standardized since this whole
3287            case is undefined.  But ERANGE makes good sense.  */
3288         return REG_ERANGE;
3289     }
3290   elem->type = SB_CHAR;
3291   elem->opr.ch = token->opr.c;
3292   return REG_NOERROR;
3293 }
3294
3295 /* Parse a bracket symbol in the bracket expression.  Bracket symbols are
3296    such as [:<character_class>:], [.<collating_element>.], and
3297    [=<equivalent_class>=].  */
3298
3299 static reg_errcode_t
3300 parse_bracket_symbol (bracket_elem_t *elem, re_string_t *regexp,
3301                       re_token_t *token)
3302 {
3303   unsigned char ch, delim = token->opr.c;
3304   int i = 0;
3305   if (re_string_eoi(regexp))
3306     return REG_EBRACK;
3307   for (;; ++i)
3308     {
3309       if (i >= BRACKET_NAME_BUF_SIZE)
3310         return REG_EBRACK;
3311       if (token->type == OP_OPEN_CHAR_CLASS)
3312         ch = re_string_fetch_byte_case (regexp);
3313       else
3314         ch = re_string_fetch_byte (regexp);
3315       if (re_string_eoi(regexp))
3316         return REG_EBRACK;
3317       if (ch == delim && re_string_peek_byte (regexp, 0) == ']')
3318         break;
3319       elem->opr.name[i] = ch;
3320     }
3321   re_string_skip_bytes (regexp, 1);
3322   elem->opr.name[i] = '\0';
3323   switch (token->type)
3324     {
3325     case OP_OPEN_COLL_ELEM:
3326       elem->type = COLL_SYM;
3327       break;
3328     case OP_OPEN_EQUIV_CLASS:
3329       elem->type = EQUIV_CLASS;
3330       break;
3331     case OP_OPEN_CHAR_CLASS:
3332       elem->type = CHAR_CLASS;
3333       break;
3334     default:
3335       break;
3336     }
3337   return REG_NOERROR;
3338 }
3339
3340   /* Helper function for parse_bracket_exp.
3341      Build the equivalence class which is represented by NAME.
3342      The result are written to MBCSET and SBCSET.
3343      EQUIV_CLASS_ALLOC is the allocated size of mbcset->equiv_classes,
3344      is a pointer argument sinse we may update it.  */
3345
3346 static reg_errcode_t
3347 build_equiv_class (bitset sbcset,
3348 #ifdef RE_ENABLE_I18N
3349                    re_charset_t *mbcset, Idx *equiv_class_alloc,
3350 #endif
3351                    const unsigned char *name)
3352 {
3353 #if defined _LIBC
3354   uint32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
3355   if (nrules != 0)
3356     {
3357       const int32_t *table, *indirect;
3358       const unsigned char *weights, *extra, *cp;
3359       unsigned char char_buf[2];
3360       int32_t idx1, idx2;
3361       unsigned int ch;
3362       size_t len;
3363       /* This #include defines a local function!  */
3364 # include <locale/weight.h>
3365       /* Calculate the index for equivalence class.  */
3366       cp = name;
3367       table = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
3368       weights = (const unsigned char *) _NL_CURRENT (LC_COLLATE,
3369                                                _NL_COLLATE_WEIGHTMB);
3370       extra = (const unsigned char *) _NL_CURRENT (LC_COLLATE,
3371                                                    _NL_COLLATE_EXTRAMB);
3372       indirect = (const int32_t *) _NL_CURRENT (LC_COLLATE,
3373                                                 _NL_COLLATE_INDIRECTMB);
3374       idx1 = findidx (&cp);
3375       if (BE (idx1 == 0 || cp < name + strlen ((const char *) name), 0))
3376         /* This isn't a valid character.  */
3377         return REG_ECOLLATE;
3378
3379       /* Build single byte matcing table for this equivalence class.  */
3380       char_buf[1] = (unsigned char) '\0';
3381       len = weights[idx1];
3382       for (ch = 0; ch < SBC_MAX; ++ch)
3383         {
3384           char_buf[0] = ch;
3385           cp = char_buf;
3386           idx2 = findidx (&cp);
3387 /*
3388           idx2 = table[ch];
3389 */
3390           if (idx2 == 0)
3391             /* This isn't a valid character.  */
3392             continue;
3393           if (len == weights[idx2])
3394             {
3395               int cnt = 0;
3396               while (cnt <= len &&
3397                      weights[idx1 + 1 + cnt] == weights[idx2 + 1 + cnt])
3398                 ++cnt;
3399
3400               if (cnt > len)
3401                 bitset_set (sbcset, ch);
3402             }
3403         }
3404       /* Check whether the array has enough space.  */
3405       if (BE (*equiv_class_alloc == mbcset->nequiv_classes, 0))
3406         {
3407           /* Not enough, realloc it.  */
3408           Idx new_equiv_class_alloc = mbcset->nequiv_classes;
3409           /* Use realloc since the array is NULL if *alloc == 0.  */
3410           int32_t *new_equiv_classes = re_x2realloc (mbcset->equiv_classes,
3411                                                      int32_t,
3412                                                      &new_equiv_class_alloc);
3413           if (BE (new_equiv_classes == NULL, 0))
3414             return REG_ESPACE;
3415           mbcset->equiv_classes = new_equiv_classes;
3416           *equiv_class_alloc = new_equiv_class_alloc;
3417         }
3418       mbcset->equiv_classes[mbcset->nequiv_classes++] = idx1;
3419     }
3420   else
3421 #endif /* _LIBC */
3422     {
3423       if (BE (strlen ((const char *) name) != 1, 0))
3424         return REG_ECOLLATE;
3425       bitset_set (sbcset, *name);
3426     }
3427   return REG_NOERROR;
3428 }
3429
3430   /* Helper function for parse_bracket_exp.
3431      Build the character class which is represented by NAME.
3432      The result are written to MBCSET and SBCSET.
3433      CHAR_CLASS_ALLOC is the allocated size of mbcset->char_classes,
3434      is a pointer argument sinse we may update it.  */
3435
3436 static reg_errcode_t
3437 build_charclass (unsigned REG_TRANSLATE_TYPE trans, bitset sbcset,
3438 #ifdef RE_ENABLE_I18N
3439                  re_charset_t *mbcset, Idx *char_class_alloc,
3440 #endif
3441                  const unsigned char *class_name, reg_syntax_t syntax)
3442 {
3443   int i;
3444   const char *name = (const char *) class_name;
3445
3446   /* In case of REG_ICASE "upper" and "lower" match the both of
3447      upper and lower cases.  */
3448   if ((syntax & REG_IGNORE_CASE)
3449       && (strcmp (name, "upper") == 0 || strcmp (name, "lower") == 0))
3450     name = "alpha";
3451
3452 #ifdef RE_ENABLE_I18N
3453   /* Check the space of the arrays.  */
3454   if (BE (*char_class_alloc == mbcset->nchar_classes, 0))
3455     {
3456       /* Not enough, realloc it.  */
3457       Idx new_char_class_alloc = mbcset->nchar_classes;
3458       /* Use realloc since array is NULL if *alloc == 0.  */
3459       wctype_t *new_char_classes = re_x2realloc (mbcset->char_classes, wctype_t,
3460                                                  &new_char_class_alloc);
3461       if (BE (new_char_classes == NULL, 0))
3462         return REG_ESPACE;
3463       mbcset->char_classes = new_char_classes;
3464       *char_class_alloc = new_char_class_alloc;
3465     }
3466   mbcset->char_classes[mbcset->nchar_classes++] = __wctype (name);
3467 #endif /* RE_ENABLE_I18N */
3468
3469 #define BUILD_CHARCLASS_LOOP(ctype_func)        \
3470     for (i = 0; i < SBC_MAX; ++i)               \
3471       {                                         \
3472         if (ctype_func (i))                     \
3473           {                                     \
3474             int ch = trans ? trans[i] : i;      \
3475             bitset_set (sbcset, ch);            \
3476           }                                     \
3477       }
3478
3479   if (strcmp (name, "alnum") == 0)
3480     BUILD_CHARCLASS_LOOP (isalnum)
3481   else if (strcmp (name, "cntrl") == 0)
3482     BUILD_CHARCLASS_LOOP (iscntrl)
3483   else if (strcmp (name, "lower") == 0)
3484     BUILD_CHARCLASS_LOOP (islower)
3485   else if (strcmp (name, "space") == 0)
3486     BUILD_CHARCLASS_LOOP (isspace)
3487   else if (strcmp (name, "alpha") == 0)
3488     BUILD_CHARCLASS_LOOP (isalpha)
3489   else if (strcmp (name, "digit") == 0)
3490     BUILD_CHARCLASS_LOOP (isdigit)
3491   else if (strcmp (name, "print") == 0)
3492     BUILD_CHARCLASS_LOOP (isprint)
3493   else if (strcmp (name, "upper") == 0)
3494     BUILD_CHARCLASS_LOOP (isupper)
3495   else if (strcmp (name, "blank") == 0)
3496     BUILD_CHARCLASS_LOOP (isblank)
3497   else if (strcmp (name, "graph") == 0)
3498     BUILD_CHARCLASS_LOOP (isgraph)
3499   else if (strcmp (name, "punct") == 0)
3500     BUILD_CHARCLASS_LOOP (ispunct)
3501   else if (strcmp (name, "xdigit") == 0)
3502     BUILD_CHARCLASS_LOOP (isxdigit)
3503   else
3504     return REG_ECTYPE;
3505
3506   return REG_NOERROR;
3507 }
3508
3509 static bin_tree_t *
3510 build_charclass_op (re_dfa_t *dfa, unsigned REG_TRANSLATE_TYPE trans,
3511                     const unsigned char *class_name,
3512                     const unsigned char *extra,
3513                     bool non_match, reg_errcode_t *err)
3514 {
3515   re_bitset_ptr_t sbcset;
3516 #ifdef RE_ENABLE_I18N
3517   re_charset_t *mbcset;
3518   Idx alloc = 0;
3519 #endif /* not RE_ENABLE_I18N */
3520   reg_errcode_t ret;
3521   re_token_t br_token;
3522   bin_tree_t *tree;
3523
3524   sbcset = re_calloc (bitset_word, BITSET_WORDS);
3525 #ifdef RE_ENABLE_I18N
3526   mbcset = re_calloc (re_charset_t, 1);
3527 #endif /* RE_ENABLE_I18N */
3528
3529 #ifdef RE_ENABLE_I18N
3530   if (BE (sbcset == NULL || mbcset == NULL, 0))
3531 #else /* not RE_ENABLE_I18N */
3532   if (BE (sbcset == NULL, 0))
3533 #endif /* not RE_ENABLE_I18N */
3534     {
3535       *err = REG_ESPACE;
3536       return NULL;
3537     }
3538
3539   if (non_match)
3540     {
3541 #ifdef RE_ENABLE_I18N
3542       /*
3543       if (syntax & REG_HAT_LISTS_NOT_NEWLINE)
3544         bitset_set(cset->sbcset, '\0');
3545       */
3546       mbcset->non_match = 1;
3547 #endif /* not RE_ENABLE_I18N */
3548     }
3549
3550   /* We don't care the syntax in this case.  */
3551   ret = build_charclass (trans, sbcset,
3552 #ifdef RE_ENABLE_I18N
3553                          mbcset, &alloc,
3554 #endif /* RE_ENABLE_I18N */
3555                          class_name, 0);
3556
3557   if (BE (ret != REG_NOERROR, 0))
3558     {
3559       re_free (sbcset);
3560 #ifdef RE_ENABLE_I18N
3561       free_charset (mbcset);
3562 #endif /* RE_ENABLE_I18N */
3563       *err = ret;
3564       return NULL;
3565     }
3566   /* \w match '_' also.  */
3567   for (; *extra; extra++)
3568     bitset_set (sbcset, *extra);
3569
3570   /* If it is non-matching list.  */
3571   if (non_match)
3572     bitset_not (sbcset);
3573
3574 #ifdef RE_ENABLE_I18N
3575   /* Ensure only single byte characters are set.  */
3576   if (dfa->mb_cur_max > 1)
3577     bitset_mask (sbcset, dfa->sb_char);
3578 #endif
3579
3580   /* Build a tree for simple bracket.  */
3581   br_token.type = SIMPLE_BRACKET;
3582   br_token.opr.sbcset = sbcset;
3583   tree = create_token_tree (dfa, NULL, NULL, &br_token);
3584   if (BE (tree == NULL, 0))
3585     goto build_word_op_espace;
3586
3587 #ifdef RE_ENABLE_I18N
3588   if (dfa->mb_cur_max > 1)
3589     {
3590       bin_tree_t *mbc_tree;
3591       /* Build a tree for complex bracket.  */
3592       br_token.type = COMPLEX_BRACKET;
3593       br_token.opr.mbcset = mbcset;
3594       dfa->has_mb_node = 1;
3595       mbc_tree = create_token_tree (dfa, NULL, NULL, &br_token);
3596       if (BE (mbc_tree == NULL, 0))
3597         goto build_word_op_espace;
3598       /* Then join them by ALT node.  */
3599       tree = create_tree (dfa, tree, mbc_tree, OP_ALT);
3600       if (BE (mbc_tree != NULL, 1))
3601         return tree;
3602     }
3603   else
3604     {
3605       free_charset (mbcset);
3606       return tree;
3607     }
3608 #else /* not RE_ENABLE_I18N */
3609   return tree;
3610 #endif /* not RE_ENABLE_I18N */
3611
3612  build_word_op_espace:
3613   re_free (sbcset);
3614 #ifdef RE_ENABLE_I18N
3615   free_charset (mbcset);
3616 #endif /* RE_ENABLE_I18N */
3617   *err = REG_ESPACE;
3618   return NULL;
3619 }
3620
3621 /* This is intended for the expressions like "a{1,3}".
3622    Fetch a number from `input', and return the number.
3623    Return REG_MISSING if the number field is empty like "{,1}".
3624    Return REG_ERROR if an error occurred.  */
3625
3626 static Idx
3627 fetch_number (re_string_t *input, re_token_t *token, reg_syntax_t syntax)
3628 {
3629   Idx num = REG_MISSING;
3630   unsigned char c;
3631   while (1)
3632     {
3633       fetch_token (token, input, syntax);
3634       c = token->opr.c;
3635       if (BE (token->type == END_OF_RE, 0))
3636         return REG_ERROR;
3637       if (token->type == OP_CLOSE_DUP_NUM || c == ',')
3638         break;
3639       num = ((token->type != CHARACTER || c < '0' || '9' < c
3640               || num == REG_ERROR)
3641              ? REG_ERROR
3642              : ((num == REG_MISSING) ? c - '0' : num * 10 + c - '0'));
3643       num = (num > REG_DUP_MAX) ? REG_ERROR : num;
3644     }
3645   return num;
3646 }
3647 \f
3648 #ifdef RE_ENABLE_I18N
3649 static void
3650 free_charset (re_charset_t *cset)
3651 {
3652   re_free (cset->mbchars);
3653 # ifdef _LIBC
3654   re_free (cset->coll_syms);
3655   re_free (cset->equiv_classes);
3656   re_free (cset->range_starts);
3657   re_free (cset->range_ends);
3658 # endif
3659   re_free (cset->char_classes);
3660   re_free (cset);
3661 }
3662 #endif /* RE_ENABLE_I18N */
3663 \f
3664 /* Functions for binary tree operation.  */
3665
3666 /* Create a tree node.  */
3667
3668 static bin_tree_t *
3669 create_tree (re_dfa_t *dfa, bin_tree_t *left, bin_tree_t *right,
3670              re_token_type_t type)
3671 {
3672   re_token_t t;
3673   t.type = type;
3674   return create_token_tree (dfa, left, right, &t);
3675 }
3676
3677 static bin_tree_t *
3678 create_token_tree (re_dfa_t *dfa, bin_tree_t *left, bin_tree_t *right,
3679                    const re_token_t *token)
3680 {
3681   bin_tree_t *tree;
3682   if (BE (dfa->str_tree_storage_idx == BIN_TREE_STORAGE_SIZE, 0))
3683     {
3684       bin_tree_storage_t *storage = re_malloc (bin_tree_storage_t, 1);
3685
3686       if (storage == NULL)
3687         return NULL;
3688       storage->next = dfa->str_tree_storage;
3689       dfa->str_tree_storage = storage;
3690       dfa->str_tree_storage_idx = 0;
3691     }
3692   tree = &dfa->str_tree_storage->data[dfa->str_tree_storage_idx++];
3693
3694   tree->parent = NULL;
3695   tree->left = left;
3696   tree->right = right;
3697   tree->token = *token;
3698   tree->token.duplicated = 0;
3699   tree->token.opt_subexp = 0;
3700   tree->first = NULL;
3701   tree->next = NULL;
3702   tree->node_idx = REG_MISSING;
3703
3704   if (left != NULL)
3705     left->parent = tree;
3706   if (right != NULL)
3707     right->parent = tree;
3708   return tree;
3709 }
3710
3711 /* Mark the tree SRC as an optional subexpression.
3712    To be called from preorder or postorder.  */
3713
3714 static reg_errcode_t
3715 mark_opt_subexp (void *extra, bin_tree_t *node)
3716 {
3717   Idx idx = (Idx) (long) extra;
3718   if (node->token.type == SUBEXP && node->token.opr.idx == idx)
3719     node->token.opt_subexp = 1;
3720
3721   return REG_NOERROR;
3722 }
3723
3724 /* Free the allocated memory inside NODE. */
3725
3726 static void
3727 free_token (re_token_t *node)
3728 {
3729 #ifdef RE_ENABLE_I18N
3730   if (node->type == COMPLEX_BRACKET && node->duplicated == 0)
3731     free_charset (node->opr.mbcset);
3732   else
3733 #endif /* RE_ENABLE_I18N */
3734     if (node->type == SIMPLE_BRACKET && node->duplicated == 0)
3735       re_free (node->opr.sbcset);
3736 }
3737
3738 /* Worker function for tree walking.  Free the allocated memory inside NODE
3739    and its children. */
3740
3741 static reg_errcode_t
3742 free_tree (void *extra, bin_tree_t *node)
3743 {
3744   free_token (&node->token);
3745   return REG_NOERROR;
3746 }
3747
3748
3749 /* Duplicate the node SRC, and return new node.  This is a preorder
3750    visit similar to the one implemented by the generic visitor, but
3751    we need more infrastructure to maintain two parallel trees --- so,
3752    it's easier to duplicate.  */
3753
3754 static bin_tree_t *
3755 duplicate_tree (const bin_tree_t *root, re_dfa_t *dfa)
3756 {
3757   const bin_tree_t *node;
3758   bin_tree_t *dup_root;
3759   bin_tree_t **p_new = &dup_root, *dup_node = root->parent;
3760
3761   for (node = root; ; )
3762     {
3763       /* Create a new tree and link it back to the current parent.  */
3764       *p_new = create_token_tree (dfa, NULL, NULL, &node->token);
3765       if (*p_new == NULL)
3766         return NULL;
3767       (*p_new)->parent = dup_node;
3768       (*p_new)->token.duplicated = 1;
3769       dup_node = *p_new;
3770
3771       /* Go to the left node, or up and to the right.  */
3772       if (node->left)
3773         {
3774           node = node->left;
3775           p_new = &dup_node->left;
3776         }
3777       else
3778         {
3779           const bin_tree_t *prev = NULL;
3780           while (node->right == prev || node->right == NULL)
3781             {
3782               prev = node;
3783               node = node->parent;
3784               dup_node = dup_node->parent;
3785               if (!node)
3786                 return dup_root;
3787             }
3788           node = node->right;
3789           p_new = &dup_node->right;
3790         }
3791     }
3792 }