2N/A/* Extended regular expression matching and search library.
2N/A Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free
2N/A Software Foundation, Inc.
2N/A Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
2N/A This file is part of the GNU C Library.
2N/A Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
2N/A
2N/A This program is free software; you can redistribute it and/or modify
2N/A it under the terms of the GNU General Public License as published by
2N/A the Free Software Foundation; either version 3, or (at your option)
2N/A any later version.
2N/A
2N/A This program is distributed in the hope that it will be useful,
2N/A but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A GNU General Public License for more details.
2N/A
2N/A You should have received a copy of the GNU General Public License along
2N/A with this program; if not, write to the Free Software Foundation,
2N/A Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
2N/A
2N/Astatic reg_errcode_t re_compile_internal (regex_t *preg, const char * pattern,
2N/A size_t length, reg_syntax_t syntax);
2N/Astatic void re_compile_fastmap_iter (regex_t *bufp,
2N/A const re_dfastate_t *init_state,
2N/A char *fastmap);
2N/Astatic reg_errcode_t init_dfa (re_dfa_t *dfa, size_t pat_len);
2N/A#ifdef RE_ENABLE_I18N
2N/Astatic void free_charset (re_charset_t *cset);
2N/A#endif /* RE_ENABLE_I18N */
2N/Astatic void free_workarea_compile (regex_t *preg);
2N/Astatic reg_errcode_t create_initial_state (re_dfa_t *dfa);
2N/A#ifdef RE_ENABLE_I18N
2N/Astatic void optimize_utf8 (re_dfa_t *dfa);
2N/A#endif
2N/Astatic reg_errcode_t analyze (regex_t *preg);
2N/Astatic reg_errcode_t preorder (bin_tree_t *root,
2N/A reg_errcode_t (fn (void *, bin_tree_t *)),
2N/A void *extra);
2N/Astatic reg_errcode_t postorder (bin_tree_t *root,
2N/A reg_errcode_t (fn (void *, bin_tree_t *)),
2N/A void *extra);
2N/Astatic reg_errcode_t optimize_subexps (void *extra, bin_tree_t *node);
2N/Astatic reg_errcode_t lower_subexps (void *extra, bin_tree_t *node);
2N/Astatic bin_tree_t *lower_subexp (reg_errcode_t *err, regex_t *preg,
2N/A bin_tree_t *node);
2N/Astatic reg_errcode_t calc_first (void *extra, bin_tree_t *node);
2N/Astatic reg_errcode_t calc_next (void *extra, bin_tree_t *node);
2N/Astatic reg_errcode_t link_nfa_nodes (void *extra, bin_tree_t *node);
2N/Astatic Idx duplicate_node (re_dfa_t *dfa, Idx org_idx, unsigned int constraint);
2N/Astatic Idx search_duplicated_node (const re_dfa_t *dfa, Idx org_node,
2N/A unsigned int constraint);
2N/Astatic reg_errcode_t calc_eclosure (re_dfa_t *dfa);
2N/Astatic reg_errcode_t calc_eclosure_iter (re_node_set *new_set, re_dfa_t *dfa,
2N/A Idx node, bool root);
2N/Astatic reg_errcode_t calc_inveclosure (re_dfa_t *dfa);
2N/Astatic Idx fetch_number (re_string_t *input, re_token_t *token,
2N/A reg_syntax_t syntax);
2N/Astatic int peek_token (re_token_t *token, re_string_t *input,
2N/A reg_syntax_t syntax) internal_function;
2N/Astatic bin_tree_t *parse (re_string_t *regexp, regex_t *preg,
2N/A reg_syntax_t syntax, reg_errcode_t *err);
2N/Astatic bin_tree_t *parse_reg_exp (re_string_t *regexp, regex_t *preg,
2N/A re_token_t *token, reg_syntax_t syntax,
2N/A Idx nest, reg_errcode_t *err);
2N/Astatic bin_tree_t *parse_branch (re_string_t *regexp, regex_t *preg,
2N/A re_token_t *token, reg_syntax_t syntax,
2N/A Idx nest, reg_errcode_t *err);
2N/Astatic bin_tree_t *parse_expression (re_string_t *regexp, regex_t *preg,
2N/A re_token_t *token, reg_syntax_t syntax,
2N/A Idx nest, reg_errcode_t *err);
2N/Astatic bin_tree_t *parse_sub_exp (re_string_t *regexp, regex_t *preg,
2N/A re_token_t *token, reg_syntax_t syntax,
2N/A Idx nest, reg_errcode_t *err);
2N/Astatic bin_tree_t *parse_dup_op (bin_tree_t *dup_elem, re_string_t *regexp,
2N/A re_dfa_t *dfa, re_token_t *token,
2N/A reg_syntax_t syntax, reg_errcode_t *err);
2N/Astatic bin_tree_t *parse_bracket_exp (re_string_t *regexp, re_dfa_t *dfa,
2N/A re_token_t *token, reg_syntax_t syntax,
2N/A reg_errcode_t *err);
2N/Astatic reg_errcode_t parse_bracket_element (bracket_elem_t *elem,
2N/A re_string_t *regexp,
2N/A re_token_t *token, int token_len,
2N/A re_dfa_t *dfa,
2N/A reg_syntax_t syntax,
2N/A bool accept_hyphen);
2N/Astatic reg_errcode_t parse_bracket_symbol (bracket_elem_t *elem,
2N/A re_string_t *regexp,
2N/A re_token_t *token);
2N/A#ifdef RE_ENABLE_I18N
2N/Astatic reg_errcode_t build_equiv_class (bitset_t sbcset,
2N/A re_charset_t *mbcset,
2N/A Idx *equiv_class_alloc,
2N/A const unsigned char *name);
2N/Astatic reg_errcode_t build_charclass (RE_TRANSLATE_TYPE trans,
2N/A bitset_t sbcset,
2N/A re_charset_t *mbcset,
2N/A Idx *char_class_alloc,
2N/A const unsigned char *class_name,
2N/A reg_syntax_t syntax);
2N/A#else /* not RE_ENABLE_I18N */
2N/Astatic reg_errcode_t build_equiv_class (bitset_t sbcset,
2N/A const unsigned char *name);
2N/Astatic reg_errcode_t build_charclass (RE_TRANSLATE_TYPE trans,
2N/A bitset_t sbcset,
2N/A const unsigned char *class_name,
2N/A reg_syntax_t syntax);
2N/A#endif /* not RE_ENABLE_I18N */
2N/Astatic bin_tree_t *build_charclass_op (re_dfa_t *dfa,
2N/A RE_TRANSLATE_TYPE trans,
2N/A const unsigned char *class_name,
2N/A const unsigned char *extra,
2N/A bool non_match, reg_errcode_t *err);
2N/Astatic bin_tree_t *create_tree (re_dfa_t *dfa,
2N/A bin_tree_t *left, bin_tree_t *right,
2N/A re_token_type_t type);
2N/Astatic bin_tree_t *create_token_tree (re_dfa_t *dfa,
2N/A bin_tree_t *left, bin_tree_t *right,
2N/A const re_token_t *token);
2N/Astatic bin_tree_t *duplicate_tree (const bin_tree_t *src, re_dfa_t *dfa);
2N/Astatic void free_token (re_token_t *node);
2N/Astatic reg_errcode_t free_tree (void *extra, bin_tree_t *node);
2N/Astatic reg_errcode_t mark_opt_subexp (void *extra, bin_tree_t *node);
2N/A
2N/A/* This table gives an error message for each of the error codes listed
2N/A in regex.h. Obviously the order here has to be same as there.
2N/A POSIX doesn't require that we do anything for REG_NOERROR,
2N/A but why not be nice? */
2N/A
2N/Astatic const char __re_error_msgid[] =
2N/A {
2N/A#define REG_NOERROR_IDX 0
2N/A gettext_noop ("Success") /* REG_NOERROR */
2N/A "\0"
2N/A#define REG_NOMATCH_IDX (REG_NOERROR_IDX + sizeof "Success")
2N/A gettext_noop ("No match") /* REG_NOMATCH */
2N/A "\0"
2N/A#define REG_BADPAT_IDX (REG_NOMATCH_IDX + sizeof "No match")
2N/A gettext_noop ("Invalid regular expression") /* REG_BADPAT */
2N/A "\0"
2N/A#define REG_ECOLLATE_IDX (REG_BADPAT_IDX + sizeof "Invalid regular expression")
2N/A gettext_noop ("Invalid collation character") /* REG_ECOLLATE */
2N/A "\0"
2N/A#define REG_ECTYPE_IDX (REG_ECOLLATE_IDX + sizeof "Invalid collation character")
2N/A gettext_noop ("Invalid character class name") /* REG_ECTYPE */
2N/A "\0"
2N/A#define REG_EESCAPE_IDX (REG_ECTYPE_IDX + sizeof "Invalid character class name")
2N/A gettext_noop ("Trailing backslash") /* REG_EESCAPE */
2N/A "\0"
2N/A#define REG_ESUBREG_IDX (REG_EESCAPE_IDX + sizeof "Trailing backslash")
2N/A gettext_noop ("Invalid back reference") /* REG_ESUBREG */
2N/A "\0"
2N/A#define REG_EBRACK_IDX (REG_ESUBREG_IDX + sizeof "Invalid back reference")
2N/A gettext_noop ("Unmatched [ or [^") /* REG_EBRACK */
2N/A "\0"
2N/A#define REG_EPAREN_IDX (REG_EBRACK_IDX + sizeof "Unmatched [ or [^")
2N/A gettext_noop ("Unmatched ( or \\(") /* REG_EPAREN */
2N/A "\0"
2N/A#define REG_EBRACE_IDX (REG_EPAREN_IDX + sizeof "Unmatched ( or \\(")
2N/A gettext_noop ("Unmatched \\{") /* REG_EBRACE */
2N/A "\0"
2N/A#define REG_BADBR_IDX (REG_EBRACE_IDX + sizeof "Unmatched \\{")
2N/A gettext_noop ("Invalid content of \\{\\}") /* REG_BADBR */
2N/A "\0"
2N/A#define REG_ERANGE_IDX (REG_BADBR_IDX + sizeof "Invalid content of \\{\\}")
2N/A gettext_noop ("Invalid range end") /* REG_ERANGE */
2N/A "\0"
2N/A#define REG_ESPACE_IDX (REG_ERANGE_IDX + sizeof "Invalid range end")
2N/A gettext_noop ("Memory exhausted") /* REG_ESPACE */
2N/A "\0"
2N/A#define REG_BADRPT_IDX (REG_ESPACE_IDX + sizeof "Memory exhausted")
2N/A gettext_noop ("Invalid preceding regular expression") /* REG_BADRPT */
2N/A "\0"
2N/A#define REG_EEND_IDX (REG_BADRPT_IDX + sizeof "Invalid preceding regular expression")
2N/A gettext_noop ("Premature end of regular expression") /* REG_EEND */
2N/A "\0"
2N/A#define REG_ESIZE_IDX (REG_EEND_IDX + sizeof "Premature end of regular expression")
2N/A gettext_noop ("Regular expression too big") /* REG_ESIZE */
2N/A "\0"
2N/A#define REG_ERPAREN_IDX (REG_ESIZE_IDX + sizeof "Regular expression too big")
2N/A gettext_noop ("Unmatched ) or \\)") /* REG_ERPAREN */
2N/A };
2N/A
2N/Astatic const size_t __re_error_msgid_idx[] =
2N/A {
2N/A REG_NOERROR_IDX,
2N/A REG_NOMATCH_IDX,
2N/A REG_BADPAT_IDX,
2N/A REG_ECOLLATE_IDX,
2N/A REG_ECTYPE_IDX,
2N/A REG_EESCAPE_IDX,
2N/A REG_ESUBREG_IDX,
2N/A REG_EBRACK_IDX,
2N/A REG_EPAREN_IDX,
2N/A REG_EBRACE_IDX,
2N/A REG_BADBR_IDX,
2N/A REG_ERANGE_IDX,
2N/A REG_ESPACE_IDX,
2N/A REG_BADRPT_IDX,
2N/A REG_EEND_IDX,
2N/A REG_ESIZE_IDX,
2N/A REG_ERPAREN_IDX
2N/A };
2N/A
2N/A/* Entry points for GNU code. */
2N/A
2N/A/* re_compile_pattern is the GNU regular expression compiler: it
2N/A compiles PATTERN (of length LENGTH) and puts the result in BUFP.
2N/A Returns 0 if the pattern was valid, otherwise an error string.
2N/A
2N/A Assumes the `allocated' (and perhaps `buffer') and `translate' fields
2N/A are set in BUFP on entry. */
2N/A
2N/A#ifdef _LIBC
2N/Aconst char *
2N/Are_compile_pattern (pattern, length, bufp)
2N/A const char *pattern;
2N/A size_t length;
2N/A struct re_pattern_buffer *bufp;
2N/A#else /* size_t might promote */
2N/Aconst char *
2N/Are_compile_pattern (const char *pattern, size_t length,
2N/A struct re_pattern_buffer *bufp)
2N/A#endif
2N/A{
2N/A reg_errcode_t ret;
2N/A
2N/A /* And GNU code determines whether or not to get register information
2N/A by passing null for the REGS argument to re_match, etc., not by
2N/A setting no_sub, unless RE_NO_SUB is set. */
2N/A bufp->no_sub = !!(re_syntax_options & RE_NO_SUB);
2N/A
2N/A /* Match anchors at newline. */
2N/A bufp->newline_anchor = 1;
2N/A
2N/A ret = re_compile_internal (bufp, pattern, length, re_syntax_options);
2N/A
2N/A if (!ret)
2N/A return NULL;
2N/A return gettext (__re_error_msgid + __re_error_msgid_idx[(int) ret]);
2N/A}
2N/A#ifdef _LIBC
2N/Aweak_alias (__re_compile_pattern, re_compile_pattern)
2N/A#endif
2N/A
2N/A/* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
2N/A also be assigned to arbitrarily: each pattern buffer stores its own
2N/A syntax, so it can be changed between regex compilations. */
2N/A/* This has no initializer because initialized variables in Emacs
2N/A become read-only after dumping. */
2N/Areg_syntax_t re_syntax_options;
2N/A
2N/A
2N/A/* Specify the precise syntax of regexps for compilation. This provides
2N/A for compatibility for various utilities which historically have
2N/A different, incompatible syntaxes.
2N/A
2N/A The argument SYNTAX is a bit mask comprised of the various bits
2N/A defined in regex.h. We return the old syntax. */
2N/A
2N/Areg_syntax_t
2N/Are_set_syntax (syntax)
2N/A reg_syntax_t syntax;
2N/A{
2N/A reg_syntax_t ret = re_syntax_options;
2N/A
2N/A re_syntax_options = syntax;
2N/A return ret;
2N/A}
2N/A#ifdef _LIBC
2N/Aweak_alias (__re_set_syntax, re_set_syntax)
2N/A#endif
2N/A
2N/Aint
2N/Are_compile_fastmap (bufp)
2N/A struct re_pattern_buffer *bufp;
2N/A{
2N/A re_dfa_t *dfa = (re_dfa_t *) bufp->buffer;
2N/A char *fastmap = bufp->fastmap;
2N/A
2N/A memset (fastmap, '\0', sizeof (char) * SBC_MAX);
2N/A re_compile_fastmap_iter (bufp, dfa->init_state, fastmap);
2N/A if (dfa->init_state != dfa->init_state_word)
2N/A re_compile_fastmap_iter (bufp, dfa->init_state_word, fastmap);
2N/A if (dfa->init_state != dfa->init_state_nl)
2N/A re_compile_fastmap_iter (bufp, dfa->init_state_nl, fastmap);
2N/A if (dfa->init_state != dfa->init_state_begbuf)
2N/A re_compile_fastmap_iter (bufp, dfa->init_state_begbuf, fastmap);
2N/A bufp->fastmap_accurate = 1;
2N/A return 0;
2N/A}
2N/A#ifdef _LIBC
2N/Aweak_alias (__re_compile_fastmap, re_compile_fastmap)
2N/A#endif
2N/A
2N/Astatic inline void
2N/A__attribute ((always_inline))
2N/Are_set_fastmap (char *fastmap, bool icase, int ch)
2N/A{
2N/A fastmap[ch] = 1;
2N/A if (icase)
2N/A fastmap[tolower (ch)] = 1;
2N/A}
2N/A
2N/A/* Helper function for re_compile_fastmap.
2N/A Compile fastmap for the initial_state INIT_STATE. */
2N/A
2N/Astatic void
2N/Are_compile_fastmap_iter (regex_t *bufp, const re_dfastate_t *init_state,
2N/A char *fastmap)
2N/A{
2N/A re_dfa_t *dfa = (re_dfa_t *) bufp->buffer;
2N/A Idx node_cnt;
2N/A bool icase = (dfa->mb_cur_max == 1 && (bufp->syntax & RE_ICASE));
2N/A for (node_cnt = 0; node_cnt < init_state->nodes.nelem; ++node_cnt)
2N/A {
2N/A Idx node = init_state->nodes.elems[node_cnt];
2N/A re_token_type_t type = dfa->nodes[node].type;
2N/A
2N/A if (type == CHARACTER)
2N/A {
2N/A re_set_fastmap (fastmap, icase, dfa->nodes[node].opr.c);
2N/A#ifdef RE_ENABLE_I18N
2N/A if ((bufp->syntax & RE_ICASE) && dfa->mb_cur_max > 1)
2N/A {
2N/A unsigned char buf[MB_LEN_MAX];
2N/A unsigned char *p;
2N/A wchar_t wc;
2N/A mbstate_t state;
2N/A
2N/A p = buf;
2N/A *p++ = dfa->nodes[node].opr.c;
2N/A while (++node < dfa->nodes_len
2N/A && dfa->nodes[node].type == CHARACTER
2N/A && dfa->nodes[node].mb_partial)
2N/A *p++ = dfa->nodes[node].opr.c;
2N/A memset (&state, '\0', sizeof (state));
2N/A if (__mbrtowc (&wc, (const char *) buf, p - buf,
2N/A &state) == p - buf
2N/A && (__wcrtomb ((char *) buf, towlower (wc), &state)
2N/A != (size_t) -1))
2N/A re_set_fastmap (fastmap, false, buf[0]);
2N/A }
2N/A#endif
2N/A }
2N/A else if (type == SIMPLE_BRACKET)
2N/A {
2N/A int i, ch;
2N/A for (i = 0, ch = 0; i < BITSET_WORDS; ++i)
2N/A {
2N/A int j;
2N/A bitset_word_t w = dfa->nodes[node].opr.sbcset[i];
2N/A for (j = 0; j < BITSET_WORD_BITS; ++j, ++ch)
2N/A if (w & ((bitset_word_t) 1 << j))
2N/A re_set_fastmap (fastmap, icase, ch);
2N/A }
2N/A }
2N/A#ifdef RE_ENABLE_I18N
2N/A else if (type == COMPLEX_BRACKET)
2N/A {
2N/A re_charset_t *cset = dfa->nodes[node].opr.mbcset;
2N/A Idx i;
2N/A
2N/A# ifdef _LIBC
2N/A /* See if we have to try all bytes which start multiple collation
2N/A elements.
2N/A e.g. In da_DK, we want to catch 'a' since "aa" is a valid
2N/A collation element, and don't catch 'b' since 'b' is
2N/A the only collation element which starts from 'b' (and
2N/A it is caught by SIMPLE_BRACKET). */
2N/A if (_NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES) != 0
2N/A && (cset->ncoll_syms || cset->nranges))
2N/A {
2N/A const int32_t *table = (const int32_t *)
2N/A _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
2N/A for (i = 0; i < SBC_MAX; ++i)
2N/A if (table[i] < 0)
2N/A re_set_fastmap (fastmap, icase, i);
2N/A }
2N/A# endif /* _LIBC */
2N/A
2N/A /* See if we have to start the match at all multibyte characters,
2N/A i.e. where we would not find an invalid sequence. This only
2N/A applies to multibyte character sets; for single byte character
2N/A sets, the SIMPLE_BRACKET again suffices. */
2N/A if (dfa->mb_cur_max > 1
2N/A && (cset->nchar_classes || cset->non_match || cset->nranges
2N/A# ifdef _LIBC
2N/A || cset->nequiv_classes
2N/A# endif /* _LIBC */
2N/A ))
2N/A {
2N/A unsigned char c = 0;
2N/A do
2N/A {
2N/A mbstate_t mbs;
2N/A memset (&mbs, 0, sizeof (mbs));
2N/A if (__mbrtowc (NULL, (char *) &c, 1, &mbs) == (size_t) -2)
2N/A re_set_fastmap (fastmap, false, (int) c);
2N/A }
2N/A while (++c != 0);
2N/A }
2N/A
2N/A else
2N/A {
2N/A /* ... Else catch all bytes which can start the mbchars. */
2N/A for (i = 0; i < cset->nmbchars; ++i)
2N/A {
2N/A char buf[256];
2N/A mbstate_t state;
2N/A memset (&state, '\0', sizeof (state));
2N/A if (__wcrtomb (buf, cset->mbchars[i], &state) != (size_t) -1)
2N/A re_set_fastmap (fastmap, icase, *(unsigned char *) buf);
2N/A if ((bufp->syntax & RE_ICASE) && dfa->mb_cur_max > 1)
2N/A {
2N/A if (__wcrtomb (buf, towlower (cset->mbchars[i]), &state)
2N/A != (size_t) -1)
2N/A re_set_fastmap (fastmap, false, *(unsigned char *) buf);
2N/A }
2N/A }
2N/A }
2N/A }
2N/A#endif /* RE_ENABLE_I18N */
2N/A else if (type == OP_PERIOD
2N/A#ifdef RE_ENABLE_I18N
2N/A || type == OP_UTF8_PERIOD
2N/A#endif /* RE_ENABLE_I18N */
2N/A || type == END_OF_RE)
2N/A {
2N/A memset (fastmap, '\1', sizeof (char) * SBC_MAX);
2N/A if (type == END_OF_RE)
2N/A bufp->can_be_null = 1;
2N/A return;
2N/A }
2N/A }
2N/A}
2N/A
2N/A/* Entry point for POSIX code. */
2N/A/* regcomp takes a regular expression as a string and compiles it.
2N/A
2N/A PREG is a regex_t *. We do not expect any fields to be initialized,
2N/A since POSIX says we shouldn't. Thus, we set
2N/A
2N/A `buffer' to the compiled pattern;
2N/A `used' to the length of the compiled pattern;
2N/A `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
2N/A REG_EXTENDED bit in CFLAGS is set; otherwise, to
2N/A RE_SYNTAX_POSIX_BASIC;
2N/A `newline_anchor' to REG_NEWLINE being set in CFLAGS;
2N/A `fastmap' to an allocated space for the fastmap;
2N/A `fastmap_accurate' to zero;
2N/A `re_nsub' to the number of subexpressions in PATTERN.
2N/A
2N/A PATTERN is the address of the pattern string.
2N/A
2N/A CFLAGS is a series of bits which affect compilation.
2N/A
2N/A If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
2N/A use POSIX basic syntax.
2N/A
2N/A If REG_NEWLINE is set, then . and [^...] don't match newline.
2N/A Also, regexec will try a match beginning after every newline.
2N/A
2N/A If REG_ICASE is set, then we considers upper- and lowercase
2N/A versions of letters to be equivalent when matching.
2N/A
2N/A If REG_NOSUB is set, then when PREG is passed to regexec, that
2N/A routine will report only success or failure, and nothing about the
2N/A registers.
2N/A
2N/A It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
2N/A the return codes and their meanings.) */
2N/A
2N/Aint
2N/Aregcomp (preg, pattern, cflags)
2N/A regex_t *_Restrict_ preg;
2N/A const char *_Restrict_ pattern;
2N/A int cflags;
2N/A{
2N/A reg_errcode_t ret;
2N/A reg_syntax_t syntax = ((cflags & REG_EXTENDED) ? RE_SYNTAX_POSIX_EXTENDED
2N/A : RE_SYNTAX_POSIX_BASIC);
2N/A
2N/A preg->buffer = NULL;
2N/A preg->allocated = 0;
2N/A preg->used = 0;
2N/A
2N/A /* Try to allocate space for the fastmap. */
2N/A preg->fastmap = re_malloc (char, SBC_MAX);
2N/A if (BE (preg->fastmap == NULL, 0))
2N/A return REG_ESPACE;
2N/A
2N/A syntax |= (cflags & REG_ICASE) ? RE_ICASE : 0;
2N/A
2N/A /* If REG_NEWLINE is set, newlines are treated differently. */
2N/A if (cflags & REG_NEWLINE)
2N/A { /* REG_NEWLINE implies neither . nor [^...] match newline. */
2N/A syntax &= ~RE_DOT_NEWLINE;
2N/A syntax |= RE_HAT_LISTS_NOT_NEWLINE;
2N/A /* It also changes the matching behavior. */
2N/A preg->newline_anchor = 1;
2N/A }
2N/A else
2N/A preg->newline_anchor = 0;
2N/A preg->no_sub = !!(cflags & REG_NOSUB);
2N/A preg->translate = NULL;
2N/A
2N/A ret = re_compile_internal (preg, pattern, strlen (pattern), syntax);
2N/A
2N/A /* POSIX doesn't distinguish between an unmatched open-group and an
2N/A unmatched close-group: both are REG_EPAREN. */
2N/A if (ret == REG_ERPAREN)
2N/A ret = REG_EPAREN;
2N/A
2N/A /* We have already checked preg->fastmap != NULL. */
2N/A if (BE (ret == REG_NOERROR, 1))
2N/A /* Compute the fastmap now, since regexec cannot modify the pattern
2N/A buffer. This function never fails in this implementation. */
2N/A (void) re_compile_fastmap (preg);
2N/A else
2N/A {
2N/A /* Some error occurred while compiling the expression. */
2N/A re_free (preg->fastmap);
2N/A preg->fastmap = NULL;
2N/A }
2N/A
2N/A return (int) ret;
2N/A}
2N/A#ifdef _LIBC
2N/Aweak_alias (__regcomp, regcomp)
2N/A#endif
2N/A
2N/A/* Returns a message corresponding to an error code, ERRCODE, returned
2N/A from either regcomp or regexec. We don't use PREG here. */
2N/A
2N/A#ifdef _LIBC
2N/Asize_t
2N/Aregerror (errcode, preg, errbuf, errbuf_size)
2N/A int errcode;
2N/A const regex_t *_Restrict_ preg;
2N/A char *_Restrict_ errbuf;
2N/A size_t errbuf_size;
2N/A#else /* size_t might promote */
2N/Asize_t
2N/Aregerror (int errcode, const regex_t *_Restrict_ preg,
2N/A char *_Restrict_ errbuf, size_t errbuf_size)
2N/A#endif
2N/A{
2N/A const char *msg;
2N/A size_t msg_size;
2N/A
2N/A if (BE (errcode < 0
2N/A || errcode >= (int) (sizeof (__re_error_msgid_idx)
2N/A / sizeof (__re_error_msgid_idx[0])), 0))
2N/A /* Only error codes returned by the rest of the code should be passed
2N/A to this routine. If we are given anything else, or if other regex
2N/A code generates an invalid error code, then the program has a bug.
2N/A Dump core so we can fix it. */
2N/A abort ();
2N/A
2N/A msg = gettext (__re_error_msgid + __re_error_msgid_idx[errcode]);
2N/A
2N/A msg_size = strlen (msg) + 1; /* Includes the null. */
2N/A
2N/A if (BE (errbuf_size != 0, 1))
2N/A {
2N/A size_t cpy_size = msg_size;
2N/A if (BE (msg_size > errbuf_size, 0))
2N/A {
2N/A cpy_size = errbuf_size - 1;
2N/A errbuf[cpy_size] = '\0';
2N/A }
2N/A memcpy (errbuf, msg, cpy_size);
2N/A }
2N/A
2N/A return msg_size;
2N/A}
2N/A#ifdef _LIBC
2N/Aweak_alias (__regerror, regerror)
2N/A#endif
2N/A
2N/A
2N/A#ifdef RE_ENABLE_I18N
2N/A/* This static array is used for the map to single-byte characters when
2N/A UTF-8 is used. Otherwise we would allocate memory just to initialize
2N/A it the same all the time. UTF-8 is the preferred encoding so this is
2N/A a worthwhile optimization. */
2N/Astatic const bitset_t utf8_sb_map =
2N/A{
2N/A /* Set the first 128 bits. */
2N/A# if 4 * BITSET_WORD_BITS < ASCII_CHARS
2N/A# error "bitset_word_t is narrower than 32 bits"
2N/A# elif 3 * BITSET_WORD_BITS < ASCII_CHARS
2N/A BITSET_WORD_MAX, BITSET_WORD_MAX, BITSET_WORD_MAX,
2N/A# elif 2 * BITSET_WORD_BITS < ASCII_CHARS
2N/A BITSET_WORD_MAX, BITSET_WORD_MAX,
2N/A# elif 1 * BITSET_WORD_BITS < ASCII_CHARS
2N/A BITSET_WORD_MAX,
2N/A# endif
2N/A (BITSET_WORD_MAX
2N/A >> (SBC_MAX % BITSET_WORD_BITS == 0
2N/A ? 0
2N/A : BITSET_WORD_BITS - SBC_MAX % BITSET_WORD_BITS))
2N/A};
2N/A#endif
2N/A
2N/A
2N/Astatic void
2N/Afree_dfa_content (re_dfa_t *dfa)
2N/A{
2N/A Idx i, j;
2N/A
2N/A if (dfa->nodes)
2N/A for (i = 0; i < dfa->nodes_len; ++i)
2N/A free_token (dfa->nodes + i);
2N/A re_free (dfa->nexts);
2N/A for (i = 0; i < dfa->nodes_len; ++i)
2N/A {
2N/A if (dfa->eclosures != NULL)
2N/A re_node_set_free (dfa->eclosures + i);
2N/A if (dfa->inveclosures != NULL)
2N/A re_node_set_free (dfa->inveclosures + i);
2N/A if (dfa->edests != NULL)
2N/A re_node_set_free (dfa->edests + i);
2N/A }
2N/A re_free (dfa->edests);
2N/A re_free (dfa->eclosures);
2N/A re_free (dfa->inveclosures);
2N/A re_free (dfa->nodes);
2N/A
2N/A if (dfa->state_table)
2N/A for (i = 0; i <= dfa->state_hash_mask; ++i)
2N/A {
2N/A struct re_state_table_entry *entry = dfa->state_table + i;
2N/A for (j = 0; j < entry->num; ++j)
2N/A {
2N/A re_dfastate_t *state = entry->array[j];
2N/A free_state (state);
2N/A }
2N/A re_free (entry->array);
2N/A }
2N/A re_free (dfa->state_table);
2N/A#ifdef RE_ENABLE_I18N
2N/A if (dfa->sb_char != utf8_sb_map)
2N/A re_free (dfa->sb_char);
2N/A#endif
2N/A re_free (dfa->subexp_map);
2N/A#ifdef DEBUG
2N/A re_free (dfa->re_str);
2N/A#endif
2N/A
2N/A re_free (dfa);
2N/A}
2N/A
2N/A
2N/A/* Free dynamically allocated space used by PREG. */
2N/A
2N/Avoid
2N/Aregfree (preg)
2N/A regex_t *preg;
2N/A{
2N/A re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
2N/A if (BE (dfa != NULL, 1))
2N/A free_dfa_content (dfa);
2N/A preg->buffer = NULL;
2N/A preg->allocated = 0;
2N/A
2N/A re_free (preg->fastmap);
2N/A preg->fastmap = NULL;
2N/A
2N/A re_free (preg->translate);
2N/A preg->translate = NULL;
2N/A}
2N/A#ifdef _LIBC
2N/Aweak_alias (__regfree, regfree)
2N/A#endif
2N/A
2N/A/* Entry points compatible with 4.2 BSD regex library. We don't define
2N/A them unless specifically requested. */
2N/A
2N/A#if defined _REGEX_RE_COMP || defined _LIBC
2N/A
2N/A/* BSD has one and only one pattern buffer. */
2N/Astatic struct re_pattern_buffer re_comp_buf;
2N/A
2N/Achar *
2N/A# ifdef _LIBC
2N/A/* Make these definitions weak in libc, so POSIX programs can redefine
2N/A these names if they don't use our functions, and still use
2N/A regcomp/regexec above without link errors. */
2N/Aweak_function
2N/A# endif
2N/Are_comp (s)
2N/A const char *s;
2N/A{
2N/A reg_errcode_t ret;
2N/A char *fastmap;
2N/A
2N/A if (!s)
2N/A {
2N/A if (!re_comp_buf.buffer)
2N/A return gettext ("No previous regular expression");
2N/A return 0;
2N/A }
2N/A
2N/A if (re_comp_buf.buffer)
2N/A {
2N/A fastmap = re_comp_buf.fastmap;
2N/A re_comp_buf.fastmap = NULL;
2N/A __regfree (&re_comp_buf);
2N/A memset (&re_comp_buf, '\0', sizeof (re_comp_buf));
2N/A re_comp_buf.fastmap = fastmap;
2N/A }
2N/A
2N/A if (re_comp_buf.fastmap == NULL)
2N/A {
2N/A re_comp_buf.fastmap = (char *) malloc (SBC_MAX);
2N/A if (re_comp_buf.fastmap == NULL)
2N/A return (char *) gettext (__re_error_msgid
2N/A + __re_error_msgid_idx[(int) REG_ESPACE]);
2N/A }
2N/A
2N/A /* Since `re_exec' always passes NULL for the `regs' argument, we
2N/A don't need to initialize the pattern buffer fields which affect it. */
2N/A
2N/A /* Match anchors at newlines. */
2N/A re_comp_buf.newline_anchor = 1;
2N/A
2N/A ret = re_compile_internal (&re_comp_buf, s, strlen (s), re_syntax_options);
2N/A
2N/A if (!ret)
2N/A return NULL;
2N/A
2N/A /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
2N/A return (char *) gettext (__re_error_msgid + __re_error_msgid_idx[(int) ret]);
2N/A}
2N/A
2N/A#ifdef _LIBC
2N/Alibc_freeres_fn (free_mem)
2N/A{
2N/A __regfree (&re_comp_buf);
2N/A}
2N/A#endif
2N/A
2N/A#endif /* _REGEX_RE_COMP */
2N/A
2N/A/* Internal entry point.
2N/A Compile the regular expression PATTERN, whose length is LENGTH.
2N/A SYNTAX indicate regular expression's syntax. */
2N/A
2N/Astatic reg_errcode_t
2N/Are_compile_internal (regex_t *preg, const char * pattern, size_t length,
2N/A reg_syntax_t syntax)
2N/A{
2N/A reg_errcode_t err = REG_NOERROR;
2N/A re_dfa_t *dfa;
2N/A re_string_t regexp;
2N/A
2N/A /* Initialize the pattern buffer. */
2N/A preg->fastmap_accurate = 0;
2N/A preg->syntax = syntax;
2N/A preg->not_bol = preg->not_eol = 0;
2N/A preg->used = 0;
2N/A preg->re_nsub = 0;
2N/A preg->can_be_null = 0;
2N/A preg->regs_allocated = REGS_UNALLOCATED;
2N/A
2N/A /* Initialize the dfa. */
2N/A dfa = (re_dfa_t *) preg->buffer;
2N/A if (BE (preg->allocated < sizeof (re_dfa_t), 0))
2N/A {
2N/A /* If zero allocated, but buffer is non-null, try to realloc
2N/A enough space. This loses if buffer's address is bogus, but
2N/A that is the user's responsibility. If ->buffer is NULL this
2N/A is a simple allocation. */
2N/A dfa = re_realloc (preg->buffer, re_dfa_t, 1);
2N/A if (dfa == NULL)
2N/A return REG_ESPACE;
2N/A preg->allocated = sizeof (re_dfa_t);
2N/A preg->buffer = (unsigned char *) dfa;
2N/A }
2N/A preg->used = sizeof (re_dfa_t);
2N/A
2N/A err = init_dfa (dfa, length);
2N/A if (BE (err != REG_NOERROR, 0))
2N/A {
2N/A free_dfa_content (dfa);
2N/A preg->buffer = NULL;
2N/A preg->allocated = 0;
2N/A return err;
2N/A }
2N/A#ifdef DEBUG
2N/A /* Note: length+1 will not overflow since it is checked in init_dfa. */
2N/A dfa->re_str = re_malloc (char, length + 1);
2N/A strncpy (dfa->re_str, pattern, length + 1);
2N/A#endif
2N/A
2N/A __libc_lock_init (dfa->lock);
2N/A
2N/A err = re_string_construct (&regexp, pattern, length, preg->translate,
2N/A (syntax & RE_ICASE) != 0, dfa);
2N/A if (BE (err != REG_NOERROR, 0))
2N/A {
2N/A re_compile_internal_free_return:
2N/A free_workarea_compile (preg);
2N/A re_string_destruct (&regexp);
2N/A free_dfa_content (dfa);
2N/A preg->buffer = NULL;
2N/A preg->allocated = 0;
2N/A return err;
2N/A }
2N/A
2N/A /* Parse the regular expression, and build a structure tree. */
2N/A preg->re_nsub = 0;
2N/A dfa->str_tree = parse (&regexp, preg, syntax, &err);
2N/A if (BE (dfa->str_tree == NULL, 0))
2N/A goto re_compile_internal_free_return;
2N/A
2N/A /* Analyze the tree and create the nfa. */
2N/A err = analyze (preg);
2N/A if (BE (err != REG_NOERROR, 0))
2N/A goto re_compile_internal_free_return;
2N/A
2N/A#ifdef RE_ENABLE_I18N
2N/A /* If possible, do searching in single byte encoding to speed things up. */
2N/A if (dfa->is_utf8 && !(syntax & RE_ICASE) && preg->translate == NULL)
2N/A optimize_utf8 (dfa);
2N/A#endif
2N/A
2N/A /* Then create the initial state of the dfa. */
2N/A err = create_initial_state (dfa);
2N/A
2N/A /* Release work areas. */
2N/A free_workarea_compile (preg);
2N/A re_string_destruct (&regexp);
2N/A
2N/A if (BE (err != REG_NOERROR, 0))
2N/A {
2N/A free_dfa_content (dfa);
2N/A preg->buffer = NULL;
2N/A preg->allocated = 0;
2N/A }
2N/A
2N/A return err;
2N/A}
2N/A
2N/A/* Initialize DFA. We use the length of the regular expression PAT_LEN
2N/A as the initial length of some arrays. */
2N/A
2N/Astatic reg_errcode_t
2N/Ainit_dfa (re_dfa_t *dfa, size_t pat_len)
2N/A{
2N/A __re_size_t table_size;
2N/A#ifndef _LIBC
2N/A const char *codeset_name;
2N/A#endif
2N/A#ifdef RE_ENABLE_I18N
2N/A size_t max_i18n_object_size = MAX (sizeof (wchar_t), sizeof (wctype_t));
2N/A#else
2N/A size_t max_i18n_object_size = 0;
2N/A#endif
2N/A size_t max_object_size =
2N/A MAX (sizeof (struct re_state_table_entry),
2N/A MAX (sizeof (re_token_t),
2N/A MAX (sizeof (re_node_set),
2N/A MAX (sizeof (regmatch_t),
2N/A max_i18n_object_size))));
2N/A
2N/A memset (dfa, '\0', sizeof (re_dfa_t));
2N/A
2N/A /* Force allocation of str_tree_storage the first time. */
2N/A dfa->str_tree_storage_idx = BIN_TREE_STORAGE_SIZE;
2N/A
2N/A /* Avoid overflows. The extra "/ 2" is for the table_size doubling
2N/A calculation below, and for similar doubling calculations
2N/A elsewhere. And it's <= rather than <, because some of the
2N/A doubling calculations add 1 afterwards. */
2N/A if (BE (SIZE_MAX / max_object_size / 2 <= pat_len, 0))
2N/A return REG_ESPACE;
2N/A
2N/A dfa->nodes_alloc = pat_len + 1;
2N/A dfa->nodes = re_malloc (re_token_t, dfa->nodes_alloc);
2N/A
2N/A /* table_size = 2 ^ ceil(log pat_len) */
2N/A for (table_size = 1; ; table_size <<= 1)
2N/A if (table_size > pat_len)
2N/A break;
2N/A
2N/A dfa->state_table = calloc (sizeof (struct re_state_table_entry), table_size);
2N/A dfa->state_hash_mask = table_size - 1;
2N/A
2N/A dfa->mb_cur_max = MB_CUR_MAX;
2N/A#ifdef _LIBC
2N/A if (dfa->mb_cur_max == 6
2N/A && strcmp (_NL_CURRENT (LC_CTYPE, _NL_CTYPE_CODESET_NAME), "UTF-8") == 0)
2N/A dfa->is_utf8 = 1;
2N/A dfa->map_notascii = (_NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_MAP_TO_NONASCII)
2N/A != 0);
2N/A#else
2N/A codeset_name = nl_langinfo (CODESET);
2N/A if (strcasecmp (codeset_name, "UTF-8") == 0
2N/A || strcasecmp (codeset_name, "UTF8") == 0)
2N/A dfa->is_utf8 = 1;
2N/A
2N/A /* We check exhaustively in the loop below if this charset is a
2N/A superset of ASCII. */
2N/A dfa->map_notascii = 0;
2N/A#endif
2N/A
2N/A#ifdef RE_ENABLE_I18N
2N/A if (dfa->mb_cur_max > 1)
2N/A {
2N/A if (dfa->is_utf8)
2N/A dfa->sb_char = (re_bitset_ptr_t) utf8_sb_map;
2N/A else
2N/A {
2N/A int i, j, ch;
2N/A
2N/A dfa->sb_char = (re_bitset_ptr_t) calloc (sizeof (bitset_t), 1);
2N/A if (BE (dfa->sb_char == NULL, 0))
2N/A return REG_ESPACE;
2N/A
2N/A /* Set the bits corresponding to single byte chars. */
2N/A for (i = 0, ch = 0; i < BITSET_WORDS; ++i)
2N/A for (j = 0; j < BITSET_WORD_BITS; ++j, ++ch)
2N/A {
2N/A wint_t wch = __btowc (ch);
2N/A if (wch != WEOF)
2N/A dfa->sb_char[i] |= (bitset_word_t) 1 << j;
2N/A# ifndef _LIBC
2N/A if (isascii (ch) && wch != ch)
2N/A dfa->map_notascii = 1;
2N/A# endif
2N/A }
2N/A }
2N/A }
2N/A#endif
2N/A
2N/A if (BE (dfa->nodes == NULL || dfa->state_table == NULL, 0))
2N/A return REG_ESPACE;
2N/A return REG_NOERROR;
2N/A}
2N/A
2N/A/* Initialize WORD_CHAR table, which indicate which character is
2N/A "word". In this case "word" means that it is the word construction
2N/A character used by some operators like "\<", "\>", etc. */
2N/A
2N/Astatic void
2N/Ainternal_function
2N/Ainit_word_char (re_dfa_t *dfa)
2N/A{
2N/A int i, j, ch;
2N/A dfa->word_ops_used = 1;
2N/A for (i = 0, ch = 0; i < BITSET_WORDS; ++i)
2N/A for (j = 0; j < BITSET_WORD_BITS; ++j, ++ch)
2N/A if (isalnum (ch) || ch == '_')
2N/A dfa->word_char[i] |= (bitset_word_t) 1 << j;
2N/A}
2N/A
2N/A/* Free the work area which are only used while compiling. */
2N/A
2N/Astatic void
2N/Afree_workarea_compile (regex_t *preg)
2N/A{
2N/A re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
2N/A bin_tree_storage_t *storage, *next;
2N/A for (storage = dfa->str_tree_storage; storage; storage = next)
2N/A {
2N/A next = storage->next;
2N/A re_free (storage);
2N/A }
2N/A dfa->str_tree_storage = NULL;
2N/A dfa->str_tree_storage_idx = BIN_TREE_STORAGE_SIZE;
2N/A dfa->str_tree = NULL;
2N/A re_free (dfa->org_indices);
2N/A dfa->org_indices = NULL;
2N/A}
2N/A
2N/A/* Create initial states for all contexts. */
2N/A
2N/Astatic reg_errcode_t
2N/Acreate_initial_state (re_dfa_t *dfa)
2N/A{
2N/A Idx first, i;
2N/A reg_errcode_t err;
2N/A re_node_set init_nodes;
2N/A
2N/A /* Initial states have the epsilon closure of the node which is
2N/A the first node of the regular expression. */
2N/A first = dfa->str_tree->first->node_idx;
2N/A dfa->init_node = first;
2N/A err = re_node_set_init_copy (&init_nodes, dfa->eclosures + first);
2N/A if (BE (err != REG_NOERROR, 0))
2N/A return err;
2N/A
2N/A /* The back-references which are in initial states can epsilon transit,
2N/A since in this case all of the subexpressions can be null.
2N/A Then we add epsilon closures of the nodes which are the next nodes of
2N/A the back-references. */
2N/A if (dfa->nbackref > 0)
2N/A for (i = 0; i < init_nodes.nelem; ++i)
2N/A {
2N/A Idx node_idx = init_nodes.elems[i];
2N/A re_token_type_t type = dfa->nodes[node_idx].type;
2N/A
2N/A Idx clexp_idx;
2N/A if (type != OP_BACK_REF)
2N/A continue;
2N/A for (clexp_idx = 0; clexp_idx < init_nodes.nelem; ++clexp_idx)
2N/A {
2N/A re_token_t *clexp_node;
2N/A clexp_node = dfa->nodes + init_nodes.elems[clexp_idx];
2N/A if (clexp_node->type == OP_CLOSE_SUBEXP
2N/A && clexp_node->opr.idx == dfa->nodes[node_idx].opr.idx)
2N/A break;
2N/A }
2N/A if (clexp_idx == init_nodes.nelem)
2N/A continue;
2N/A
2N/A if (type == OP_BACK_REF)
2N/A {
2N/A Idx dest_idx = dfa->edests[node_idx].elems[0];
2N/A if (!re_node_set_contains (&init_nodes, dest_idx))
2N/A {
2N/A reg_errcode_t merge_err
2N/A = re_node_set_merge (&init_nodes, dfa->eclosures + dest_idx);
2N/A if (merge_err != REG_NOERROR)
2N/A return merge_err;
2N/A i = 0;
2N/A }
2N/A }
2N/A }
2N/A
2N/A /* It must be the first time to invoke acquire_state. */
2N/A dfa->init_state = re_acquire_state_context (&err, dfa, &init_nodes, 0);
2N/A /* We don't check ERR here, since the initial state must not be NULL. */
2N/A if (BE (dfa->init_state == NULL, 0))
2N/A return err;
2N/A if (dfa->init_state->has_constraint)
2N/A {
2N/A dfa->init_state_word = re_acquire_state_context (&err, dfa, &init_nodes,
2N/A CONTEXT_WORD);
2N/A dfa->init_state_nl = re_acquire_state_context (&err, dfa, &init_nodes,
2N/A CONTEXT_NEWLINE);
2N/A dfa->init_state_begbuf = re_acquire_state_context (&err, dfa,
2N/A &init_nodes,
2N/A CONTEXT_NEWLINE
2N/A | CONTEXT_BEGBUF);
2N/A if (BE (dfa->init_state_word == NULL || dfa->init_state_nl == NULL
2N/A || dfa->init_state_begbuf == NULL, 0))
2N/A return err;
2N/A }
2N/A else
2N/A dfa->init_state_word = dfa->init_state_nl
2N/A = dfa->init_state_begbuf = dfa->init_state;
2N/A
2N/A re_node_set_free (&init_nodes);
2N/A return REG_NOERROR;
2N/A}
2N/A
2N/A#ifdef RE_ENABLE_I18N
2N/A/* If it is possible to do searching in single byte encoding instead of UTF-8
2N/A to speed things up, set dfa->mb_cur_max to 1, clear is_utf8 and change
2N/A DFA nodes where needed. */
2N/A
2N/Astatic void
2N/Aoptimize_utf8 (re_dfa_t *dfa)
2N/A{
2N/A Idx node;
2N/A int i;
2N/A bool mb_chars = false;
2N/A bool has_period = false;
2N/A
2N/A for (node = 0; node < dfa->nodes_len; ++node)
2N/A switch (dfa->nodes[node].type)
2N/A {
2N/A case CHARACTER:
2N/A if (dfa->nodes[node].opr.c >= ASCII_CHARS)
2N/A mb_chars = true;
2N/A break;
2N/A case ANCHOR:
2N/A switch (dfa->nodes[node].opr.ctx_type)
2N/A {
2N/A case LINE_FIRST:
2N/A case LINE_LAST:
2N/A case BUF_FIRST:
2N/A case BUF_LAST:
2N/A break;
2N/A default:
2N/A /* Word anchors etc. cannot be handled. It's okay to test
2N/A opr.ctx_type since constraints (for all DFA nodes) are
2N/A created by ORing one or more opr.ctx_type values. */
2N/A return;
2N/A }
2N/A break;
2N/A case OP_PERIOD:
2N/A has_period = true;
2N/A break;
2N/A case OP_BACK_REF:
2N/A case OP_ALT:
2N/A case END_OF_RE:
2N/A case OP_DUP_ASTERISK:
2N/A case OP_OPEN_SUBEXP:
2N/A case OP_CLOSE_SUBEXP:
2N/A break;
2N/A case COMPLEX_BRACKET:
2N/A return;
2N/A case SIMPLE_BRACKET:
2N/A /* Just double check. */
2N/A {
2N/A int rshift = (ASCII_CHARS % BITSET_WORD_BITS == 0
2N/A ? 0
2N/A : BITSET_WORD_BITS - ASCII_CHARS % BITSET_WORD_BITS);
2N/A for (i = ASCII_CHARS / BITSET_WORD_BITS; i < BITSET_WORDS; ++i)
2N/A {
2N/A if (dfa->nodes[node].opr.sbcset[i] >> rshift != 0)
2N/A return;
2N/A rshift = 0;
2N/A }
2N/A }
2N/A break;
2N/A default:
2N/A abort ();
2N/A }
2N/A
2N/A if (mb_chars || has_period)
2N/A for (node = 0; node < dfa->nodes_len; ++node)
2N/A {
2N/A if (dfa->nodes[node].type == CHARACTER
2N/A && dfa->nodes[node].opr.c >= ASCII_CHARS)
2N/A dfa->nodes[node].mb_partial = 0;
2N/A else if (dfa->nodes[node].type == OP_PERIOD)
2N/A dfa->nodes[node].type = OP_UTF8_PERIOD;
2N/A }
2N/A
2N/A /* The search can be in single byte locale. */
2N/A dfa->mb_cur_max = 1;
2N/A dfa->is_utf8 = 0;
2N/A dfa->has_mb_node = dfa->nbackref > 0 || has_period;
2N/A}
2N/A#endif
2N/A
2N/A/* Analyze the structure tree, and calculate "first", "next", "edest",
2N/A "eclosure", and "inveclosure". */
2N/A
2N/Astatic reg_errcode_t
2N/Aanalyze (regex_t *preg)
2N/A{
2N/A re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
2N/A reg_errcode_t ret;
2N/A
2N/A /* Allocate arrays. */
2N/A dfa->nexts = re_malloc (Idx, dfa->nodes_alloc);
2N/A dfa->org_indices = re_malloc (Idx, dfa->nodes_alloc);
2N/A dfa->edests = re_malloc (re_node_set, dfa->nodes_alloc);
2N/A dfa->eclosures = re_malloc (re_node_set, dfa->nodes_alloc);
2N/A if (BE (dfa->nexts == NULL || dfa->org_indices == NULL || dfa->edests == NULL
2N/A || dfa->eclosures == NULL, 0))
2N/A return REG_ESPACE;
2N/A
2N/A dfa->subexp_map = re_malloc (Idx, preg->re_nsub);
2N/A if (dfa->subexp_map != NULL)
2N/A {
2N/A Idx i;
2N/A for (i = 0; i < preg->re_nsub; i++)
2N/A dfa->subexp_map[i] = i;
2N/A preorder (dfa->str_tree, optimize_subexps, dfa);
2N/A for (i = 0; i < preg->re_nsub; i++)
2N/A if (dfa->subexp_map[i] != i)
2N/A break;
2N/A if (i == preg->re_nsub)
2N/A {
2N/A free (dfa->subexp_map);
2N/A dfa->subexp_map = NULL;
2N/A }
2N/A }
2N/A
2N/A ret = postorder (dfa->str_tree, lower_subexps, preg);
2N/A if (BE (ret != REG_NOERROR, 0))
2N/A return ret;
2N/A ret = postorder (dfa->str_tree, calc_first, dfa);
2N/A if (BE (ret != REG_NOERROR, 0))
2N/A return ret;
2N/A preorder (dfa->str_tree, calc_next, dfa);
2N/A ret = preorder (dfa->str_tree, link_nfa_nodes, dfa);
2N/A if (BE (ret != REG_NOERROR, 0))
2N/A return ret;
2N/A ret = calc_eclosure (dfa);
2N/A if (BE (ret != REG_NOERROR, 0))
2N/A return ret;
2N/A
2N/A /* We only need this during the prune_impossible_nodes pass in regexec.c;
2N/A skip it if p_i_n will not run, as calc_inveclosure can be quadratic. */
2N/A if ((!preg->no_sub && preg->re_nsub > 0 && dfa->has_plural_match)
2N/A || dfa->nbackref)
2N/A {
2N/A dfa->inveclosures = re_malloc (re_node_set, dfa->nodes_len);
2N/A if (BE (dfa->inveclosures == NULL, 0))
2N/A return REG_ESPACE;
2N/A ret = calc_inveclosure (dfa);
2N/A }
2N/A
2N/A return ret;
2N/A}
2N/A
2N/A/* Our parse trees are very unbalanced, so we cannot use a stack to
2N/A implement parse tree visits. Instead, we use parent pointers and
2N/A some hairy code in these two functions. */
2N/Astatic reg_errcode_t
2N/Apostorder (bin_tree_t *root, reg_errcode_t (fn (void *, bin_tree_t *)),
2N/A void *extra)
2N/A{
2N/A bin_tree_t *node, *prev;
2N/A
2N/A for (node = root; ; )
2N/A {
2N/A /* Descend down the tree, preferably to the left (or to the right
2N/A if that's the only child). */
2N/A while (node->left || node->right)
2N/A if (node->left)
2N/A node = node->left;
2N/A else
2N/A node = node->right;
2N/A
2N/A do
2N/A {
2N/A reg_errcode_t err = fn (extra, node);
2N/A if (BE (err != REG_NOERROR, 0))
2N/A return err;
2N/A if (node->parent == NULL)
2N/A return REG_NOERROR;
2N/A prev = node;
2N/A node = node->parent;
2N/A }
2N/A /* Go up while we have a node that is reached from the right. */
2N/A while (node->right == prev || node->right == NULL);
2N/A node = node->right;
2N/A }
2N/A}
2N/A
2N/Astatic reg_errcode_t
2N/Apreorder (bin_tree_t *root, reg_errcode_t (fn (void *, bin_tree_t *)),
2N/A void *extra)
2N/A{
2N/A bin_tree_t *node;
2N/A
2N/A for (node = root; ; )
2N/A {
2N/A reg_errcode_t err = fn (extra, node);
2N/A if (BE (err != REG_NOERROR, 0))
2N/A return err;
2N/A
2N/A /* Go to the left node, or up and to the right. */
2N/A if (node->left)
2N/A node = node->left;
2N/A else
2N/A {
2N/A bin_tree_t *prev = NULL;
2N/A while (node->right == prev || node->right == NULL)
2N/A {
2N/A prev = node;
2N/A node = node->parent;
2N/A if (!node)
2N/A return REG_NOERROR;
2N/A }
2N/A node = node->right;
2N/A }
2N/A }
2N/A}
2N/A
2N/A/* Optimization pass: if a SUBEXP is entirely contained, strip it and tell
2N/A re_search_internal to map the inner one's opr.idx to this one's. Adjust
2N/A backreferences as well. Requires a preorder visit. */
2N/Astatic reg_errcode_t
2N/Aoptimize_subexps (void *extra, bin_tree_t *node)
2N/A{
2N/A re_dfa_t *dfa = (re_dfa_t *) extra;
2N/A
2N/A if (node->token.type == OP_BACK_REF && dfa->subexp_map)
2N/A {
2N/A int idx = node->token.opr.idx;
2N/A node->token.opr.idx = dfa->subexp_map[idx];
2N/A dfa->used_bkref_map |= 1 << node->token.opr.idx;
2N/A }
2N/A
2N/A else if (node->token.type == SUBEXP
2N/A && node->left && node->left->token.type == SUBEXP)
2N/A {
2N/A Idx other_idx = node->left->token.opr.idx;
2N/A
2N/A node->left = node->left->left;
2N/A if (node->left)
2N/A node->left->parent = node;
2N/A
2N/A dfa->subexp_map[other_idx] = dfa->subexp_map[node->token.opr.idx];
2N/A if (other_idx < BITSET_WORD_BITS)
2N/A dfa->used_bkref_map &= ~((bitset_word_t) 1 << other_idx);
2N/A }
2N/A
2N/A return REG_NOERROR;
2N/A}
2N/A
2N/A/* Lowering pass: Turn each SUBEXP node into the appropriate concatenation
2N/A of OP_OPEN_SUBEXP, the body of the SUBEXP (if any) and OP_CLOSE_SUBEXP. */
2N/Astatic reg_errcode_t
2N/Alower_subexps (void *extra, bin_tree_t *node)
2N/A{
2N/A regex_t *preg = (regex_t *) extra;
2N/A reg_errcode_t err = REG_NOERROR;
2N/A
2N/A if (node->left && node->left->token.type == SUBEXP)
2N/A {
2N/A node->left = lower_subexp (&err, preg, node->left);
2N/A if (node->left)
2N/A node->left->parent = node;
2N/A }
2N/A if (node->right && node->right->token.type == SUBEXP)
2N/A {
2N/A node->right = lower_subexp (&err, preg, node->right);
2N/A if (node->right)
2N/A node->right->parent = node;
2N/A }
2N/A
2N/A return err;
2N/A}
2N/A
2N/Astatic bin_tree_t *
2N/Alower_subexp (reg_errcode_t *err, regex_t *preg, bin_tree_t *node)
2N/A{
2N/A re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
2N/A bin_tree_t *body = node->left;
2N/A bin_tree_t *op, *cls, *tree1, *tree;
2N/A
2N/A if (preg->no_sub
2N/A /* We do not optimize empty subexpressions, because otherwise we may
2N/A have bad CONCAT nodes with NULL children. This is obviously not
2N/A very common, so we do not lose much. An example that triggers
2N/A this case is the sed "script" /\(\)/x. */
2N/A && node->left != NULL
2N/A && (node->token.opr.idx >= BITSET_WORD_BITS
2N/A || !(dfa->used_bkref_map
2N/A & ((bitset_word_t) 1 << node->token.opr.idx))))
2N/A return node->left;
2N/A
2N/A /* Convert the SUBEXP node to the concatenation of an
2N/A OP_OPEN_SUBEXP, the contents, and an OP_CLOSE_SUBEXP. */
2N/A op = create_tree (dfa, NULL, NULL, OP_OPEN_SUBEXP);
2N/A cls = create_tree (dfa, NULL, NULL, OP_CLOSE_SUBEXP);
2N/A tree1 = body ? create_tree (dfa, body, cls, CONCAT) : cls;
2N/A tree = create_tree (dfa, op, tree1, CONCAT);
2N/A if (BE (tree == NULL || tree1 == NULL || op == NULL || cls == NULL, 0))
2N/A {
2N/A *err = REG_ESPACE;
2N/A return NULL;
2N/A }
2N/A
2N/A op->token.opr.idx = cls->token.opr.idx = node->token.opr.idx;
2N/A op->token.opt_subexp = cls->token.opt_subexp = node->token.opt_subexp;
2N/A return tree;
2N/A}
2N/A
2N/A/* Pass 1 in building the NFA: compute FIRST and create unlinked automaton
2N/A nodes. Requires a postorder visit. */
2N/Astatic reg_errcode_t
2N/Acalc_first (void *extra, bin_tree_t *node)
2N/A{
2N/A re_dfa_t *dfa = (re_dfa_t *) extra;
2N/A if (node->token.type == CONCAT)
2N/A {
2N/A node->first = node->left->first;
2N/A node->node_idx = node->left->node_idx;
2N/A }
2N/A else
2N/A {
2N/A node->first = node;
2N/A node->node_idx = re_dfa_add_node (dfa, node->token);
2N/A if (BE (node->node_idx == REG_MISSING, 0))
2N/A return REG_ESPACE;
2N/A if (node->token.type == ANCHOR)
2N/A dfa->nodes[node->node_idx].constraint = node->token.opr.ctx_type;
2N/A }
2N/A return REG_NOERROR;
2N/A}
2N/A
2N/A/* Pass 2: compute NEXT on the tree. Preorder visit. */
2N/Astatic reg_errcode_t
2N/Acalc_next (void *extra, bin_tree_t *node)
2N/A{
2N/A switch (node->token.type)
2N/A {
2N/A case OP_DUP_ASTERISK:
2N/A node->left->next = node;
2N/A break;
2N/A case CONCAT:
2N/A node->left->next = node->right->first;
2N/A node->right->next = node->next;
2N/A break;
2N/A default:
2N/A if (node->left)
2N/A node->left->next = node->next;
2N/A if (node->right)
2N/A node->right->next = node->next;
2N/A break;
2N/A }
2N/A return REG_NOERROR;
2N/A}
2N/A
2N/A/* Pass 3: link all DFA nodes to their NEXT node (any order will do). */
2N/Astatic reg_errcode_t
2N/Alink_nfa_nodes (void *extra, bin_tree_t *node)
2N/A{
2N/A re_dfa_t *dfa = (re_dfa_t *) extra;
2N/A Idx idx = node->node_idx;
2N/A reg_errcode_t err = REG_NOERROR;
2N/A
2N/A switch (node->token.type)
2N/A {
2N/A case CONCAT:
2N/A break;
2N/A
2N/A case END_OF_RE:
2N/A assert (node->next == NULL);
2N/A break;
2N/A
2N/A case OP_DUP_ASTERISK:
2N/A case OP_ALT:
2N/A {
2N/A Idx left, right;
2N/A dfa->has_plural_match = 1;
2N/A if (node->left != NULL)
2N/A left = node->left->first->node_idx;
2N/A else
2N/A left = node->next->node_idx;
2N/A if (node->right != NULL)
2N/A right = node->right->first->node_idx;
2N/A else
2N/A right = node->next->node_idx;
2N/A assert (REG_VALID_INDEX (left));
2N/A assert (REG_VALID_INDEX (right));
2N/A err = re_node_set_init_2 (dfa->edests + idx, left, right);
2N/A }
2N/A break;
2N/A
2N/A case ANCHOR:
2N/A case OP_OPEN_SUBEXP:
2N/A case OP_CLOSE_SUBEXP:
2N/A err = re_node_set_init_1 (dfa->edests + idx, node->next->node_idx);
2N/A break;
2N/A
2N/A case OP_BACK_REF:
2N/A dfa->nexts[idx] = node->next->node_idx;
2N/A if (node->token.type == OP_BACK_REF)
2N/A err = re_node_set_init_1 (dfa->edests + idx, dfa->nexts[idx]);
2N/A break;
2N/A
2N/A default:
2N/A assert (!IS_EPSILON_NODE (node->token.type));
2N/A dfa->nexts[idx] = node->next->node_idx;
2N/A break;
2N/A }
2N/A
2N/A return err;
2N/A}
2N/A
2N/A/* Duplicate the epsilon closure of the node ROOT_NODE.
2N/A Note that duplicated nodes have constraint INIT_CONSTRAINT in addition
2N/A to their own constraint. */
2N/A
2N/Astatic reg_errcode_t
2N/Ainternal_function
2N/Aduplicate_node_closure (re_dfa_t *dfa, Idx top_org_node, Idx top_clone_node,
2N/A Idx root_node, unsigned int init_constraint)
2N/A{
2N/A Idx org_node, clone_node;
2N/A bool ok;
2N/A unsigned int constraint = init_constraint;
2N/A for (org_node = top_org_node, clone_node = top_clone_node;;)
2N/A {
2N/A Idx org_dest, clone_dest;
2N/A if (dfa->nodes[org_node].type == OP_BACK_REF)
2N/A {
2N/A /* If the back reference epsilon-transit, its destination must
2N/A also have the constraint. Then duplicate the epsilon closure
2N/A of the destination of the back reference, and store it in
2N/A edests of the back reference. */
2N/A org_dest = dfa->nexts[org_node];
2N/A re_node_set_empty (dfa->edests + clone_node);
2N/A clone_dest = duplicate_node (dfa, org_dest, constraint);
2N/A if (BE (clone_dest == REG_MISSING, 0))
2N/A return REG_ESPACE;
2N/A dfa->nexts[clone_node] = dfa->nexts[org_node];
2N/A ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
2N/A if (BE (! ok, 0))
2N/A return REG_ESPACE;
2N/A }
2N/A else if (dfa->edests[org_node].nelem == 0)
2N/A {
2N/A /* In case of the node can't epsilon-transit, don't duplicate the
2N/A destination and store the original destination as the
2N/A destination of the node. */
2N/A dfa->nexts[clone_node] = dfa->nexts[org_node];
2N/A break;
2N/A }
2N/A else if (dfa->edests[org_node].nelem == 1)
2N/A {
2N/A /* In case of the node can epsilon-transit, and it has only one
2N/A destination. */
2N/A org_dest = dfa->edests[org_node].elems[0];
2N/A re_node_set_empty (dfa->edests + clone_node);
2N/A /* If the node is root_node itself, it means the epsilon closure
2N/A has a loop. Then tie it to the destination of the root_node. */
2N/A if (org_node == root_node && clone_node != org_node)
2N/A {
2N/A ok = re_node_set_insert (dfa->edests + clone_node, org_dest);
2N/A if (BE (! ok, 0))
2N/A return REG_ESPACE;
2N/A break;
2N/A }
2N/A /* In case the node has another constraint, append it. */
2N/A constraint |= dfa->nodes[org_node].constraint;
2N/A clone_dest = duplicate_node (dfa, org_dest, constraint);
2N/A if (BE (clone_dest == REG_MISSING, 0))
2N/A return REG_ESPACE;
2N/A ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
2N/A if (BE (! ok, 0))
2N/A return REG_ESPACE;
2N/A }
2N/A else /* dfa->edests[org_node].nelem == 2 */
2N/A {
2N/A /* In case of the node can epsilon-transit, and it has two
2N/A destinations. In the bin_tree_t and DFA, that's '|' and '*'. */
2N/A org_dest = dfa->edests[org_node].elems[0];
2N/A re_node_set_empty (dfa->edests + clone_node);
2N/A /* Search for a duplicated node which satisfies the constraint. */
2N/A clone_dest = search_duplicated_node (dfa, org_dest, constraint);
2N/A if (clone_dest == REG_MISSING)
2N/A {
2N/A /* There is no such duplicated node, create a new one. */
2N/A reg_errcode_t err;
2N/A clone_dest = duplicate_node (dfa, org_dest, constraint);
2N/A if (BE (clone_dest == REG_MISSING, 0))
2N/A return REG_ESPACE;
2N/A ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
2N/A if (BE (! ok, 0))
2N/A return REG_ESPACE;
2N/A err = duplicate_node_closure (dfa, org_dest, clone_dest,
2N/A root_node, constraint);
2N/A if (BE (err != REG_NOERROR, 0))
2N/A return err;
2N/A }
2N/A else
2N/A {
2N/A /* There is a duplicated node which satisfies the constraint,
2N/A use it to avoid infinite loop. */
2N/A ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
2N/A if (BE (! ok, 0))
2N/A return REG_ESPACE;
2N/A }
2N/A
2N/A org_dest = dfa->edests[org_node].elems[1];
2N/A clone_dest = duplicate_node (dfa, org_dest, constraint);
2N/A if (BE (clone_dest == REG_MISSING, 0))
2N/A return REG_ESPACE;
2N/A ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
2N/A if (BE (! ok, 0))
2N/A return REG_ESPACE;
2N/A }
2N/A org_node = org_dest;
2N/A clone_node = clone_dest;
2N/A }
2N/A return REG_NOERROR;
2N/A}
2N/A
2N/A/* Search for a node which is duplicated from the node ORG_NODE, and
2N/A satisfies the constraint CONSTRAINT. */
2N/A
2N/Astatic Idx
2N/Asearch_duplicated_node (const re_dfa_t *dfa, Idx org_node,
2N/A unsigned int constraint)
2N/A{
2N/A Idx idx;
2N/A for (idx = dfa->nodes_len - 1; dfa->nodes[idx].duplicated && idx > 0; --idx)
2N/A {
2N/A if (org_node == dfa->org_indices[idx]
2N/A && constraint == dfa->nodes[idx].constraint)
2N/A return idx; /* Found. */
2N/A }
2N/A return REG_MISSING; /* Not found. */
2N/A}
2N/A
2N/A/* Duplicate the node whose index is ORG_IDX and set the constraint CONSTRAINT.
2N/A Return the index of the new node, or REG_MISSING if insufficient storage is
2N/A available. */
2N/A
2N/Astatic Idx
2N/Aduplicate_node (re_dfa_t *dfa, Idx org_idx, unsigned int constraint)
2N/A{
2N/A Idx dup_idx = re_dfa_add_node (dfa, dfa->nodes[org_idx]);
2N/A if (BE (dup_idx != REG_MISSING, 1))
2N/A {
2N/A dfa->nodes[dup_idx].constraint = constraint;
2N/A dfa->nodes[dup_idx].constraint |= dfa->nodes[org_idx].constraint;
2N/A dfa->nodes[dup_idx].duplicated = 1;
2N/A
2N/A /* Store the index of the original node. */
2N/A dfa->org_indices[dup_idx] = org_idx;
2N/A }
2N/A return dup_idx;
2N/A}
2N/A
2N/Astatic reg_errcode_t
2N/Acalc_inveclosure (re_dfa_t *dfa)
2N/A{
2N/A Idx src, idx;
2N/A bool ok;
2N/A for (idx = 0; idx < dfa->nodes_len; ++idx)
2N/A re_node_set_init_empty (dfa->inveclosures + idx);
2N/A
2N/A for (src = 0; src < dfa->nodes_len; ++src)
2N/A {
2N/A Idx *elems = dfa->eclosures[src].elems;
2N/A for (idx = 0; idx < dfa->eclosures[src].nelem; ++idx)
2N/A {
2N/A ok = re_node_set_insert_last (dfa->inveclosures + elems[idx], src);
2N/A if (BE (! ok, 0))
2N/A return REG_ESPACE;
2N/A }
2N/A }
2N/A
2N/A return REG_NOERROR;
2N/A}
2N/A
2N/A/* Calculate "eclosure" for all the node in DFA. */
2N/A
2N/Astatic reg_errcode_t
2N/Acalc_eclosure (re_dfa_t *dfa)
2N/A{
2N/A Idx node_idx;
2N/A bool incomplete;
2N/A#ifdef DEBUG
2N/A assert (dfa->nodes_len > 0);
2N/A#endif
2N/A incomplete = false;
2N/A /* For each nodes, calculate epsilon closure. */
2N/A for (node_idx = 0; ; ++node_idx)
2N/A {
2N/A reg_errcode_t err;
2N/A re_node_set eclosure_elem;
2N/A if (node_idx == dfa->nodes_len)
2N/A {
2N/A if (!incomplete)
2N/A break;
2N/A incomplete = false;
2N/A node_idx = 0;
2N/A }
2N/A
2N/A#ifdef DEBUG
2N/A assert (dfa->eclosures[node_idx].nelem != REG_MISSING);
2N/A#endif
2N/A
2N/A /* If we have already calculated, skip it. */
2N/A if (dfa->eclosures[node_idx].nelem != 0)
2N/A continue;
2N/A /* Calculate epsilon closure of `node_idx'. */
2N/A err = calc_eclosure_iter (&eclosure_elem, dfa, node_idx, true);
2N/A if (BE (err != REG_NOERROR, 0))
2N/A return err;
2N/A
2N/A if (dfa->eclosures[node_idx].nelem == 0)
2N/A {
2N/A incomplete = true;
2N/A re_node_set_free (&eclosure_elem);
2N/A }
2N/A }
2N/A return REG_NOERROR;
2N/A}
2N/A
2N/A/* Calculate epsilon closure of NODE. */
2N/A
2N/Astatic reg_errcode_t
2N/Acalc_eclosure_iter (re_node_set *new_set, re_dfa_t *dfa, Idx node, bool root)
2N/A{
2N/A reg_errcode_t err;
2N/A Idx i;
2N/A re_node_set eclosure;
2N/A bool ok;
2N/A bool incomplete = false;
2N/A err = re_node_set_alloc (&eclosure, dfa->edests[node].nelem + 1);
2N/A if (BE (err != REG_NOERROR, 0))
2N/A return err;
2N/A
2N/A /* This indicates that we are calculating this node now.
2N/A We reference this value to avoid infinite loop. */
2N/A dfa->eclosures[node].nelem = REG_MISSING;
2N/A
2N/A /* If the current node has constraints, duplicate all nodes
2N/A since they must inherit the constraints. */
2N/A if (dfa->nodes[node].constraint
2N/A && dfa->edests[node].nelem
2N/A && !dfa->nodes[dfa->edests[node].elems[0]].duplicated)
2N/A {
2N/A err = duplicate_node_closure (dfa, node, node, node,
2N/A dfa->nodes[node].constraint);
2N/A if (BE (err != REG_NOERROR, 0))
2N/A return err;
2N/A }
2N/A
2N/A /* Expand each epsilon destination nodes. */
2N/A if (IS_EPSILON_NODE(dfa->nodes[node].type))
2N/A for (i = 0; i < dfa->edests[node].nelem; ++i)
2N/A {
2N/A re_node_set eclosure_elem;
2N/A Idx edest = dfa->edests[node].elems[i];
2N/A /* If calculating the epsilon closure of `edest' is in progress,
2N/A return intermediate result. */
2N/A if (dfa->eclosures[edest].nelem == REG_MISSING)
2N/A {
2N/A incomplete = true;
2N/A continue;
2N/A }
2N/A /* If we haven't calculated the epsilon closure of `edest' yet,
2N/A calculate now. Otherwise use calculated epsilon closure. */
2N/A if (dfa->eclosures[edest].nelem == 0)
2N/A {
2N/A err = calc_eclosure_iter (&eclosure_elem, dfa, edest, false);
2N/A if (BE (err != REG_NOERROR, 0))
2N/A return err;
2N/A }
2N/A else
2N/A eclosure_elem = dfa->eclosures[edest];
2N/A /* Merge the epsilon closure of `edest'. */
2N/A err = re_node_set_merge (&eclosure, &eclosure_elem);
2N/A if (BE (err != REG_NOERROR, 0))
2N/A return err;
2N/A /* If the epsilon closure of `edest' is incomplete,
2N/A the epsilon closure of this node is also incomplete. */
2N/A if (dfa->eclosures[edest].nelem == 0)
2N/A {
2N/A incomplete = true;
2N/A re_node_set_free (&eclosure_elem);
2N/A }
2N/A }
2N/A
2N/A /* An epsilon closure includes itself. */
2N/A ok = re_node_set_insert (&eclosure, node);
2N/A if (BE (! ok, 0))
2N/A return REG_ESPACE;
2N/A if (incomplete && !root)
2N/A dfa->eclosures[node].nelem = 0;
2N/A else
2N/A dfa->eclosures[node] = eclosure;
2N/A *new_set = eclosure;
2N/A return REG_NOERROR;
2N/A}
2N/A
2N/A/* Functions for token which are used in the parser. */
2N/A
2N/A/* Fetch a token from INPUT.
2N/A We must not use this function inside bracket expressions. */
2N/A
2N/Astatic void
2N/Ainternal_function
2N/Afetch_token (re_token_t *result, re_string_t *input, reg_syntax_t syntax)
2N/A{
2N/A re_string_skip_bytes (input, peek_token (result, input, syntax));
2N/A}
2N/A
2N/A/* Peek a token from INPUT, and return the length of the token.
2N/A We must not use this function inside bracket expressions. */
2N/A
2N/Astatic int
2N/Ainternal_function
2N/Apeek_token (re_token_t *token, re_string_t *input, reg_syntax_t syntax)
2N/A{
2N/A unsigned char c;
2N/A
2N/A if (re_string_eoi (input))
2N/A {
2N/A token->type = END_OF_RE;
2N/A return 0;
2N/A }
2N/A
2N/A c = re_string_peek_byte (input, 0);
2N/A token->opr.c = c;
2N/A
2N/A token->word_char = 0;
2N/A#ifdef RE_ENABLE_I18N
2N/A token->mb_partial = 0;
2N/A if (input->mb_cur_max > 1 &&
2N/A !re_string_first_byte (input, re_string_cur_idx (input)))
2N/A {
2N/A token->type = CHARACTER;
2N/A token->mb_partial = 1;
2N/A return 1;
2N/A }
2N/A#endif
2N/A if (c == '\\')
2N/A {
2N/A unsigned char c2;
2N/A if (re_string_cur_idx (input) + 1 >= re_string_length (input))
2N/A {
2N/A token->type = BACK_SLASH;
2N/A return 1;
2N/A }
2N/A
2N/A c2 = re_string_peek_byte_case (input, 1);
2N/A token->opr.c = c2;
2N/A token->type = CHARACTER;
2N/A#ifdef RE_ENABLE_I18N
2N/A if (input->mb_cur_max > 1)
2N/A {
2N/A wint_t wc = re_string_wchar_at (input,
2N/A re_string_cur_idx (input) + 1);
2N/A token->word_char = IS_WIDE_WORD_CHAR (wc) != 0;
2N/A }
2N/A else
2N/A#endif
2N/A token->word_char = IS_WORD_CHAR (c2) != 0;
2N/A
2N/A switch (c2)
2N/A {
2N/A case '|':
2N/A if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_NO_BK_VBAR))
2N/A token->type = OP_ALT;
2N/A break;
2N/A case '1': case '2': case '3': case '4': case '5':
2N/A case '6': case '7': case '8': case '9':
2N/A if (!(syntax & RE_NO_BK_REFS))
2N/A {
2N/A token->type = OP_BACK_REF;
2N/A token->opr.idx = c2 - '1';
2N/A }
2N/A break;
2N/A case '<':
2N/A if (!(syntax & RE_NO_GNU_OPS))
2N/A {
2N/A token->type = ANCHOR;
2N/A token->opr.ctx_type = WORD_FIRST;
2N/A }
2N/A break;
2N/A case '>':
2N/A if (!(syntax & RE_NO_GNU_OPS))
2N/A {
2N/A token->type = ANCHOR;
2N/A token->opr.ctx_type = WORD_LAST;
2N/A }
2N/A break;
2N/A case 'b':
2N/A if (!(syntax & RE_NO_GNU_OPS))
2N/A {
2N/A token->type = ANCHOR;
2N/A token->opr.ctx_type = WORD_DELIM;
2N/A }
2N/A break;
2N/A case 'B':
2N/A if (!(syntax & RE_NO_GNU_OPS))
2N/A {
2N/A token->type = ANCHOR;
2N/A token->opr.ctx_type = NOT_WORD_DELIM;
2N/A }
2N/A break;
2N/A case 'w':
2N/A if (!(syntax & RE_NO_GNU_OPS))
2N/A token->type = OP_WORD;
2N/A break;
2N/A case 'W':
2N/A if (!(syntax & RE_NO_GNU_OPS))
2N/A token->type = OP_NOTWORD;
2N/A break;
2N/A case 's':
2N/A if (!(syntax & RE_NO_GNU_OPS))
2N/A token->type = OP_SPACE;
2N/A break;
2N/A case 'S':
2N/A if (!(syntax & RE_NO_GNU_OPS))
2N/A token->type = OP_NOTSPACE;
2N/A break;
2N/A case '`':
2N/A if (!(syntax & RE_NO_GNU_OPS))
2N/A {
2N/A token->type = ANCHOR;
2N/A token->opr.ctx_type = BUF_FIRST;
2N/A }
2N/A break;
2N/A case '\'':
2N/A if (!(syntax & RE_NO_GNU_OPS))
2N/A {
2N/A token->type = ANCHOR;
2N/A token->opr.ctx_type = BUF_LAST;
2N/A }
2N/A break;
2N/A case '(':
2N/A if (!(syntax & RE_NO_BK_PARENS))
2N/A token->type = OP_OPEN_SUBEXP;
2N/A break;
2N/A case ')':
2N/A if (!(syntax & RE_NO_BK_PARENS))
2N/A token->type = OP_CLOSE_SUBEXP;
2N/A break;
2N/A case '+':
2N/A if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_BK_PLUS_QM))
2N/A token->type = OP_DUP_PLUS;
2N/A break;
2N/A case '?':
2N/A if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_BK_PLUS_QM))
2N/A token->type = OP_DUP_QUESTION;
2N/A break;
2N/A case '{':
2N/A if ((syntax & RE_INTERVALS) && (!(syntax & RE_NO_BK_BRACES)))
2N/A token->type = OP_OPEN_DUP_NUM;
2N/A break;
2N/A case '}':
2N/A if ((syntax & RE_INTERVALS) && (!(syntax & RE_NO_BK_BRACES)))
2N/A token->type = OP_CLOSE_DUP_NUM;
2N/A break;
2N/A default:
2N/A break;
2N/A }
2N/A return 2;
2N/A }
2N/A
2N/A token->type = CHARACTER;
2N/A#ifdef RE_ENABLE_I18N
2N/A if (input->mb_cur_max > 1)
2N/A {
2N/A wint_t wc = re_string_wchar_at (input, re_string_cur_idx (input));
2N/A token->word_char = IS_WIDE_WORD_CHAR (wc) != 0;
2N/A }
2N/A else
2N/A#endif
2N/A token->word_char = IS_WORD_CHAR (token->opr.c);
2N/A
2N/A switch (c)
2N/A {
2N/A case '\n':
2N/A if (syntax & RE_NEWLINE_ALT)
2N/A token->type = OP_ALT;
2N/A break;
2N/A case '|':
2N/A if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_NO_BK_VBAR))
2N/A token->type = OP_ALT;
2N/A break;
2N/A case '*':
2N/A token->type = OP_DUP_ASTERISK;
2N/A break;
2N/A case '+':
2N/A if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_BK_PLUS_QM))
2N/A token->type = OP_DUP_PLUS;
2N/A break;
2N/A case '?':
2N/A if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_BK_PLUS_QM))
2N/A token->type = OP_DUP_QUESTION;
2N/A break;
2N/A case '{':
2N/A if ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
2N/A token->type = OP_OPEN_DUP_NUM;
2N/A break;
2N/A case '}':
2N/A if ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
2N/A token->type = OP_CLOSE_DUP_NUM;
2N/A break;
2N/A case '(':
2N/A if (syntax & RE_NO_BK_PARENS)
2N/A token->type = OP_OPEN_SUBEXP;
2N/A break;
2N/A case ')':
2N/A if (syntax & RE_NO_BK_PARENS)
2N/A token->type = OP_CLOSE_SUBEXP;
2N/A break;
2N/A case '[':
2N/A token->type = OP_OPEN_BRACKET;
2N/A break;
2N/A case '.':
2N/A token->type = OP_PERIOD;
2N/A break;
2N/A case '^':
2N/A if (!(syntax & (RE_CONTEXT_INDEP_ANCHORS | RE_CARET_ANCHORS_HERE)) &&
2N/A re_string_cur_idx (input) != 0)
2N/A {
2N/A char prev = re_string_peek_byte (input, -1);
2N/A if (!(syntax & RE_NEWLINE_ALT) || prev != '\n')
2N/A break;
2N/A }
2N/A token->type = ANCHOR;
2N/A token->opr.ctx_type = LINE_FIRST;
2N/A break;
2N/A case '$':
2N/A if (!(syntax & RE_CONTEXT_INDEP_ANCHORS) &&
2N/A re_string_cur_idx (input) + 1 != re_string_length (input))
2N/A {
2N/A re_token_t next;
2N/A re_string_skip_bytes (input, 1);
2N/A peek_token (&next, input, syntax);
2N/A re_string_skip_bytes (input, -1);
2N/A if (next.type != OP_ALT && next.type != OP_CLOSE_SUBEXP)
2N/A break;
2N/A }
2N/A token->type = ANCHOR;
2N/A token->opr.ctx_type = LINE_LAST;
2N/A break;
2N/A default:
2N/A break;
2N/A }
2N/A return 1;
2N/A}
2N/A
2N/A/* Peek a token from INPUT, and return the length of the token.
2N/A We must not use this function out of bracket expressions. */
2N/A
2N/Astatic int
2N/Ainternal_function
2N/Apeek_token_bracket (re_token_t *token, re_string_t *input, reg_syntax_t syntax)
2N/A{
2N/A unsigned char c;
2N/A if (re_string_eoi (input))
2N/A {
2N/A token->type = END_OF_RE;
2N/A return 0;
2N/A }
2N/A c = re_string_peek_byte (input, 0);
2N/A token->opr.c = c;
2N/A
2N/A#ifdef RE_ENABLE_I18N
2N/A if (input->mb_cur_max > 1 &&
2N/A !re_string_first_byte (input, re_string_cur_idx (input)))
2N/A {
2N/A token->type = CHARACTER;
2N/A return 1;
2N/A }
2N/A#endif /* RE_ENABLE_I18N */
2N/A
2N/A if (c == '\\' && (syntax & RE_BACKSLASH_ESCAPE_IN_LISTS)
2N/A && re_string_cur_idx (input) + 1 < re_string_length (input))
2N/A {
2N/A /* In this case, '\' escape a character. */
2N/A unsigned char c2;
2N/A re_string_skip_bytes (input, 1);
2N/A c2 = re_string_peek_byte (input, 0);
2N/A token->opr.c = c2;
2N/A token->type = CHARACTER;
2N/A return 1;
2N/A }
2N/A if (c == '[') /* '[' is a special char in a bracket exps. */
2N/A {
2N/A unsigned char c2;
2N/A int token_len;
2N/A if (re_string_cur_idx (input) + 1 < re_string_length (input))
2N/A c2 = re_string_peek_byte (input, 1);
2N/A else
2N/A c2 = 0;
2N/A token->opr.c = c2;
2N/A token_len = 2;
2N/A switch (c2)
2N/A {
2N/A case '.':
2N/A token->type = OP_OPEN_COLL_ELEM;
2N/A break;
2N/A case '=':
2N/A token->type = OP_OPEN_EQUIV_CLASS;
2N/A break;
2N/A case ':':
2N/A if (syntax & RE_CHAR_CLASSES)
2N/A {
2N/A token->type = OP_OPEN_CHAR_CLASS;
2N/A break;
2N/A }
2N/A /* else fall through. */
2N/A default:
2N/A token->type = CHARACTER;
2N/A token->opr.c = c;
2N/A token_len = 1;
2N/A break;
2N/A }
2N/A return token_len;
2N/A }
2N/A switch (c)
2N/A {
2N/A case '-':
2N/A token->type = OP_CHARSET_RANGE;
2N/A break;
2N/A case ']':
2N/A token->type = OP_CLOSE_BRACKET;
2N/A break;
2N/A case '^':
2N/A token->type = OP_NON_MATCH_LIST;
2N/A break;
2N/A default:
2N/A token->type = CHARACTER;
2N/A }
2N/A return 1;
2N/A}
2N/A
2N/A/* Functions for parser. */
2N/A
2N/A/* Entry point of the parser.
2N/A Parse the regular expression REGEXP and return the structure tree.
2N/A If an error is occured, ERR is set by error code, and return NULL.
2N/A This function build the following tree, from regular expression <reg_exp>:
2N/A CAT
2N/A / \
2N/A / \
2N/A <reg_exp> EOR
2N/A
2N/A CAT means concatenation.
2N/A EOR means end of regular expression. */
2N/A
2N/Astatic bin_tree_t *
2N/Aparse (re_string_t *regexp, regex_t *preg, reg_syntax_t syntax,
2N/A reg_errcode_t *err)
2N/A{
2N/A re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
2N/A bin_tree_t *tree, *eor, *root;
2N/A re_token_t current_token;
2N/A dfa->syntax = syntax;
2N/A fetch_token (&current_token, regexp, syntax | RE_CARET_ANCHORS_HERE);
2N/A tree = parse_reg_exp (regexp, preg, &current_token, syntax, 0, err);
2N/A if (BE (*err != REG_NOERROR && tree == NULL, 0))
2N/A return NULL;
2N/A eor = create_tree (dfa, NULL, NULL, END_OF_RE);
2N/A if (tree != NULL)
2N/A root = create_tree (dfa, tree, eor, CONCAT);
2N/A else
2N/A root = eor;
2N/A if (BE (eor == NULL || root == NULL, 0))
2N/A {
2N/A *err = REG_ESPACE;
2N/A return NULL;
2N/A }
2N/A return root;
2N/A}
2N/A
2N/A/* This function build the following tree, from regular expression
2N/A <branch1>|<branch2>:
2N/A ALT
2N/A / \
2N/A / \
2N/A <branch1> <branch2>
2N/A
2N/A ALT means alternative, which represents the operator `|'. */
2N/A
2N/Astatic bin_tree_t *
2N/Aparse_reg_exp (re_string_t *regexp, regex_t *preg, re_token_t *token,
2N/A reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
2N/A{
2N/A re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
2N/A bin_tree_t *tree, *branch = NULL;
2N/A tree = parse_branch (regexp, preg, token, syntax, nest, err);
2N/A if (BE (*err != REG_NOERROR && tree == NULL, 0))
2N/A return NULL;
2N/A
2N/A while (token->type == OP_ALT)
2N/A {
2N/A fetch_token (token, regexp, syntax | RE_CARET_ANCHORS_HERE);
2N/A if (token->type != OP_ALT && token->type != END_OF_RE
2N/A && (nest == 0 || token->type != OP_CLOSE_SUBEXP))
2N/A {
2N/A branch = parse_branch (regexp, preg, token, syntax, nest, err);
2N/A if (BE (*err != REG_NOERROR && branch == NULL, 0))
2N/A return NULL;
2N/A }
2N/A else
2N/A branch = NULL;
2N/A tree = create_tree (dfa, tree, branch, OP_ALT);
2N/A if (BE (tree == NULL, 0))
2N/A {
2N/A *err = REG_ESPACE;
2N/A return NULL;
2N/A }
2N/A }
2N/A return tree;
2N/A}
2N/A
2N/A/* This function build the following tree, from regular expression
2N/A <exp1><exp2>:
2N/A CAT
2N/A / \
2N/A / \
2N/A <exp1> <exp2>
2N/A
2N/A CAT means concatenation. */
2N/A
2N/Astatic bin_tree_t *
2N/Aparse_branch (re_string_t *regexp, regex_t *preg, re_token_t *token,
2N/A reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
2N/A{
2N/A bin_tree_t *tree, *expr;
2N/A re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
2N/A tree = parse_expression (regexp, preg, token, syntax, nest, err);
2N/A if (BE (*err != REG_NOERROR && tree == NULL, 0))
2N/A return NULL;
2N/A
2N/A while (token->type != OP_ALT && token->type != END_OF_RE
2N/A && (nest == 0 || token->type != OP_CLOSE_SUBEXP))
2N/A {
2N/A expr = parse_expression (regexp, preg, token, syntax, nest, err);
2N/A if (BE (*err != REG_NOERROR && expr == NULL, 0))
2N/A {
2N/A return NULL;
2N/A }
2N/A if (tree != NULL && expr != NULL)
2N/A {
2N/A tree = create_tree (dfa, tree, expr, CONCAT);
2N/A if (tree == NULL)
2N/A {
2N/A *err = REG_ESPACE;
2N/A return NULL;
2N/A }
2N/A }
2N/A else if (tree == NULL)
2N/A tree = expr;
2N/A /* Otherwise expr == NULL, we don't need to create new tree. */
2N/A }
2N/A return tree;
2N/A}
2N/A
2N/A/* This function build the following tree, from regular expression a*:
2N/A *
2N/A |
2N/A a
2N/A*/
2N/A
2N/Astatic bin_tree_t *
2N/Aparse_expression (re_string_t *regexp, regex_t *preg, re_token_t *token,
2N/A reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
2N/A{
2N/A re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
2N/A bin_tree_t *tree;
2N/A switch (token->type)
2N/A {
2N/A case CHARACTER:
2N/A tree = create_token_tree (dfa, NULL, NULL, token);
2N/A if (BE (tree == NULL, 0))
2N/A {
2N/A *err = REG_ESPACE;
2N/A return NULL;
2N/A }
2N/A#ifdef RE_ENABLE_I18N
2N/A if (dfa->mb_cur_max > 1)
2N/A {
2N/A while (!re_string_eoi (regexp)
2N/A && !re_string_first_byte (regexp, re_string_cur_idx (regexp)))
2N/A {
2N/A bin_tree_t *mbc_remain;
2N/A fetch_token (token, regexp, syntax);
2N/A mbc_remain = create_token_tree (dfa, NULL, NULL, token);
2N/A tree = create_tree (dfa, tree, mbc_remain, CONCAT);
2N/A if (BE (mbc_remain == NULL || tree == NULL, 0))
2N/A {
2N/A *err = REG_ESPACE;
2N/A return NULL;
2N/A }
2N/A }
2N/A }
2N/A#endif
2N/A break;
2N/A case OP_OPEN_SUBEXP:
2N/A tree = parse_sub_exp (regexp, preg, token, syntax, nest + 1, err);
2N/A if (BE (*err != REG_NOERROR && tree == NULL, 0))
2N/A return NULL;
2N/A break;
2N/A case OP_OPEN_BRACKET:
2N/A tree = parse_bracket_exp (regexp, dfa, token, syntax, err);
2N/A if (BE (*err != REG_NOERROR && tree == NULL, 0))
2N/A return NULL;
2N/A break;
2N/A case OP_BACK_REF:
2N/A if (!BE (dfa->completed_bkref_map & (1 << token->opr.idx), 1))
2N/A {
2N/A *err = REG_ESUBREG;
2N/A return NULL;
2N/A }
2N/A dfa->used_bkref_map |= 1 << token->opr.idx;
2N/A tree = create_token_tree (dfa, NULL, NULL, token);
2N/A if (BE (tree == NULL, 0))
2N/A {
2N/A *err = REG_ESPACE;
2N/A return NULL;
2N/A }
2N/A ++dfa->nbackref;
2N/A dfa->has_mb_node = 1;
2N/A break;
2N/A case OP_OPEN_DUP_NUM:
2N/A if (syntax & RE_CONTEXT_INVALID_DUP)
2N/A {
2N/A *err = REG_BADRPT;
2N/A return NULL;
2N/A }
2N/A /* FALLTHROUGH */
2N/A case OP_DUP_ASTERISK:
2N/A case OP_DUP_PLUS:
2N/A case OP_DUP_QUESTION:
2N/A if (syntax & RE_CONTEXT_INVALID_OPS)
2N/A {
2N/A *err = REG_BADRPT;
2N/A return NULL;
2N/A }
2N/A else if (syntax & RE_CONTEXT_INDEP_OPS)
2N/A {
2N/A fetch_token (token, regexp, syntax);
2N/A return parse_expression (regexp, preg, token, syntax, nest, err);
2N/A }
2N/A /* else fall through */
2N/A case OP_CLOSE_SUBEXP:
2N/A if ((token->type == OP_CLOSE_SUBEXP) &&
2N/A !(syntax & RE_UNMATCHED_RIGHT_PAREN_ORD))
2N/A {
2N/A *err = REG_ERPAREN;
2N/A return NULL;
2N/A }
2N/A /* else fall through */
2N/A case OP_CLOSE_DUP_NUM:
2N/A /* We treat it as a normal character. */
2N/A
2N/A /* Then we can these characters as normal characters. */
2N/A token->type = CHARACTER;
2N/A /* mb_partial and word_char bits should be initialized already
2N/A by peek_token. */
2N/A tree = create_token_tree (dfa, NULL, NULL, token);
2N/A if (BE (tree == NULL, 0))
2N/A {
2N/A *err = REG_ESPACE;
2N/A return NULL;
2N/A }
2N/A break;
2N/A case ANCHOR:
2N/A if ((token->opr.ctx_type
2N/A & (WORD_DELIM | NOT_WORD_DELIM | WORD_FIRST | WORD_LAST))
2N/A && dfa->word_ops_used == 0)
2N/A init_word_char (dfa);
2N/A if (token->opr.ctx_type == WORD_DELIM
2N/A || token->opr.ctx_type == NOT_WORD_DELIM)
2N/A {
2N/A bin_tree_t *tree_first, *tree_last;
2N/A if (token->opr.ctx_type == WORD_DELIM)
2N/A {
2N/A token->opr.ctx_type = WORD_FIRST;
2N/A tree_first = create_token_tree (dfa, NULL, NULL, token);
2N/A token->opr.ctx_type = WORD_LAST;
2N/A }
2N/A else
2N/A {
2N/A token->opr.ctx_type = INSIDE_WORD;
2N/A tree_first = create_token_tree (dfa, NULL, NULL, token);
2N/A token->opr.ctx_type = INSIDE_NOTWORD;
2N/A }
2N/A tree_last = create_token_tree (dfa, NULL, NULL, token);
2N/A tree = create_tree (dfa, tree_first, tree_last, OP_ALT);
2N/A if (BE (tree_first == NULL || tree_last == NULL || tree == NULL, 0))
2N/A {
2N/A *err = REG_ESPACE;
2N/A return NULL;
2N/A }
2N/A }
2N/A else
2N/A {
2N/A tree = create_token_tree (dfa, NULL, NULL, token);
2N/A if (BE (tree == NULL, 0))
2N/A {
2N/A *err = REG_ESPACE;
2N/A return NULL;
2N/A }
2N/A }
2N/A /* We must return here, since ANCHORs can't be followed
2N/A by repetition operators.
2N/A eg. RE"^*" is invalid or "<ANCHOR(^)><CHAR(*)>",
2N/A it must not be "<ANCHOR(^)><REPEAT(*)>". */
2N/A fetch_token (token, regexp, syntax);
2N/A return tree;
2N/A case OP_PERIOD:
2N/A tree = create_token_tree (dfa, NULL, NULL, token);
2N/A if (BE (tree == NULL, 0))
2N/A {
2N/A *err = REG_ESPACE;
2N/A return NULL;
2N/A }
2N/A if (dfa->mb_cur_max > 1)
2N/A dfa->has_mb_node = 1;
2N/A break;
2N/A case OP_WORD:
2N/A case OP_NOTWORD:
2N/A tree = build_charclass_op (dfa, regexp->trans,
2N/A (const unsigned char *) "alnum",
2N/A (const unsigned char *) "_",
2N/A token->type == OP_NOTWORD, err);
2N/A if (BE (*err != REG_NOERROR && tree == NULL, 0))
2N/A return NULL;
2N/A break;
2N/A case OP_SPACE:
2N/A case OP_NOTSPACE:
2N/A tree = build_charclass_op (dfa, regexp->trans,
2N/A (const unsigned char *) "space",
2N/A (const unsigned char *) "",
2N/A token->type == OP_NOTSPACE, err);
2N/A if (BE (*err != REG_NOERROR && tree == NULL, 0))
2N/A return NULL;
2N/A break;
2N/A case OP_ALT:
2N/A case END_OF_RE:
2N/A return NULL;
2N/A case BACK_SLASH:
2N/A *err = REG_EESCAPE;
2N/A return NULL;
2N/A default:
2N/A /* Must not happen? */
2N/A#ifdef DEBUG
2N/A assert (0);
2N/A#endif
2N/A return NULL;
2N/A }
2N/A fetch_token (token, regexp, syntax);
2N/A
2N/A while (token->type == OP_DUP_ASTERISK || token->type == OP_DUP_PLUS
2N/A || token->type == OP_DUP_QUESTION || token->type == OP_OPEN_DUP_NUM)
2N/A {
2N/A tree = parse_dup_op (tree, regexp, dfa, token, syntax, err);
2N/A if (BE (*err != REG_NOERROR && tree == NULL, 0))
2N/A return NULL;
2N/A /* In BRE consecutive duplications are not allowed. */
2N/A if ((syntax & RE_CONTEXT_INVALID_DUP)
2N/A && (token->type == OP_DUP_ASTERISK
2N/A || token->type == OP_OPEN_DUP_NUM))
2N/A {
2N/A *err = REG_BADRPT;
2N/A return NULL;
2N/A }
2N/A }
2N/A
2N/A return tree;
2N/A}
2N/A
2N/A/* This function build the following tree, from regular expression
2N/A (<reg_exp>):
2N/A SUBEXP
2N/A |
2N/A <reg_exp>
2N/A*/
2N/A
2N/Astatic bin_tree_t *
2N/Aparse_sub_exp (re_string_t *regexp, regex_t *preg, re_token_t *token,
2N/A reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
2N/A{
2N/A re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
2N/A bin_tree_t *tree;
2N/A size_t cur_nsub;
2N/A cur_nsub = preg->re_nsub++;
2N/A
2N/A fetch_token (token, regexp, syntax | RE_CARET_ANCHORS_HERE);
2N/A
2N/A /* The subexpression may be a null string. */
2N/A if (token->type == OP_CLOSE_SUBEXP)
2N/A tree = NULL;
2N/A else
2N/A {
2N/A tree = parse_reg_exp (regexp, preg, token, syntax, nest, err);
2N/A if (BE (*err == REG_NOERROR && token->type != OP_CLOSE_SUBEXP, 0))
2N/A *err = REG_EPAREN;
2N/A if (BE (*err != REG_NOERROR, 0))
2N/A return NULL;
2N/A }
2N/A
2N/A if (cur_nsub <= '9' - '1')
2N/A dfa->completed_bkref_map |= 1 << cur_nsub;
2N/A
2N/A tree = create_tree (dfa, tree, NULL, SUBEXP);
2N/A if (BE (tree == NULL, 0))
2N/A {
2N/A *err = REG_ESPACE;
2N/A return NULL;
2N/A }
2N/A tree->token.opr.idx = cur_nsub;
2N/A return tree;
2N/A}
2N/A
2N/A/* This function parse repetition operators like "*", "+", "{1,3}" etc. */
2N/A
2N/Astatic bin_tree_t *
2N/Aparse_dup_op (bin_tree_t *elem, re_string_t *regexp, re_dfa_t *dfa,
2N/A re_token_t *token, reg_syntax_t syntax, reg_errcode_t *err)
2N/A{
2N/A bin_tree_t *tree = NULL, *old_tree = NULL;
2N/A Idx i, start, end, start_idx = re_string_cur_idx (regexp);
2N/A re_token_t start_token = *token;
2N/A
2N/A if (token->type == OP_OPEN_DUP_NUM)
2N/A {
2N/A end = 0;
2N/A start = fetch_number (regexp, token, syntax);
2N/A if (start == REG_MISSING)
2N/A {
2N/A if (token->type == CHARACTER && token->opr.c == ',')
2N/A start = 0; /* We treat "{,m}" as "{0,m}". */
2N/A else
2N/A {
2N/A *err = REG_BADBR; /* <re>{} is invalid. */
2N/A return NULL;
2N/A }
2N/A }
2N/A if (BE (start != REG_ERROR, 1))
2N/A {
2N/A /* We treat "{n}" as "{n,n}". */
2N/A end = ((token->type == OP_CLOSE_DUP_NUM) ? start
2N/A : ((token->type == CHARACTER && token->opr.c == ',')
2N/A ? fetch_number (regexp, token, syntax) : REG_ERROR));
2N/A }
2N/A if (BE (start == REG_ERROR || end == REG_ERROR, 0))
2N/A {
2N/A /* Invalid sequence. */
2N/A if (BE (!(syntax & RE_INVALID_INTERVAL_ORD), 0))
2N/A {
2N/A if (token->type == END_OF_RE)
2N/A *err = REG_EBRACE;
2N/A else
2N/A *err = REG_BADBR;
2N/A
2N/A return NULL;
2N/A }
2N/A
2N/A /* If the syntax bit is set, rollback. */
2N/A re_string_set_index (regexp, start_idx);
2N/A *token = start_token;
2N/A token->type = CHARACTER;
2N/A /* mb_partial and word_char bits should be already initialized by
2N/A peek_token. */
2N/A return elem;
2N/A }
2N/A
2N/A if (BE ((end != REG_MISSING && start > end)
2N/A || token->type != OP_CLOSE_DUP_NUM, 0))
2N/A {
2N/A /* First number greater than second. */
2N/A *err = REG_BADBR;
2N/A return NULL;
2N/A }
2N/A }
2N/A else
2N/A {
2N/A start = (token->type == OP_DUP_PLUS) ? 1 : 0;
2N/A end = (token->type == OP_DUP_QUESTION) ? 1 : REG_MISSING;
2N/A }
2N/A
2N/A fetch_token (token, regexp, syntax);
2N/A
2N/A if (BE (elem == NULL, 0))
2N/A return NULL;
2N/A if (BE (start == 0 && end == 0, 0))
2N/A {
2N/A postorder (elem, free_tree, NULL);
2N/A return NULL;
2N/A }
2N/A
2N/A /* Extract "<re>{n,m}" to "<re><re>...<re><re>{0,<m-n>}". */
2N/A if (BE (start > 0, 0))
2N/A {
2N/A tree = elem;
2N/A for (i = 2; i <= start; ++i)
2N/A {
2N/A elem = duplicate_tree (elem, dfa);
2N/A tree = create_tree (dfa, tree, elem, CONCAT);
2N/A if (BE (elem == NULL || tree == NULL, 0))
2N/A goto parse_dup_op_espace;
2N/A }
2N/A
2N/A if (start == end)
2N/A return tree;
2N/A
2N/A /* Duplicate ELEM before it is marked optional. */
2N/A elem = duplicate_tree (elem, dfa);
2N/A old_tree = tree;
2N/A }
2N/A else
2N/A old_tree = NULL;
2N/A
2N/A if (elem->token.type == SUBEXP)
2N/A postorder (elem, mark_opt_subexp, (void *) (long) elem->token.opr.idx);
2N/A
2N/A tree = create_tree (dfa, elem, NULL,
2N/A (end == REG_MISSING ? OP_DUP_ASTERISK : OP_ALT));
2N/A if (BE (tree == NULL, 0))
2N/A goto parse_dup_op_espace;
2N/A
2N/A/* From gnulib's "intprops.h":
2N/A True if the arithmetic type T is signed. */
2N/A#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
2N/A
2N/A /* This loop is actually executed only when end != REG_MISSING,
2N/A to rewrite <re>{0,n} as (<re>(<re>...<re>?)?)?... We have
2N/A already created the start+1-th copy. */
2N/A if (TYPE_SIGNED (Idx) || end != REG_MISSING)
2N/A for (i = start + 2; i <= end; ++i)
2N/A {
2N/A elem = duplicate_tree (elem, dfa);
2N/A tree = create_tree (dfa, tree, elem, CONCAT);
2N/A if (BE (elem == NULL || tree == NULL, 0))
2N/A goto parse_dup_op_espace;
2N/A
2N/A tree = create_tree (dfa, tree, NULL, OP_ALT);
2N/A if (BE (tree == NULL, 0))
2N/A goto parse_dup_op_espace;
2N/A }
2N/A
2N/A if (old_tree)
2N/A tree = create_tree (dfa, old_tree, tree, CONCAT);
2N/A
2N/A return tree;
2N/A
2N/A parse_dup_op_espace:
2N/A *err = REG_ESPACE;
2N/A return NULL;
2N/A}
2N/A
2N/A/* Size of the names for collating symbol/equivalence_class/character_class.
2N/A I'm not sure, but maybe enough. */
2N/A#define BRACKET_NAME_BUF_SIZE 32
2N/A
2N/A#ifndef _LIBC
2N/A /* Local function for parse_bracket_exp only used in case of NOT _LIBC.
2N/A Build the range expression which starts from START_ELEM, and ends
2N/A at END_ELEM. The result are written to MBCSET and SBCSET.
2N/A RANGE_ALLOC is the allocated size of mbcset->range_starts, and
2N/A mbcset->range_ends, is a pointer argument sinse we may
2N/A update it. */
2N/A
2N/Astatic reg_errcode_t
2N/Ainternal_function
2N/A# ifdef RE_ENABLE_I18N
2N/Abuild_range_exp (const reg_syntax_t syntax,
2N/A bitset_t sbcset,
2N/A re_charset_t *mbcset,
2N/A Idx *range_alloc,
2N/A const bracket_elem_t *start_elem,
2N/A const bracket_elem_t *end_elem)
2N/A# else /* not RE_ENABLE_I18N */
2N/Abuild_range_exp (const reg_syntax_t syntax,
2N/A bitset_t sbcset,
2N/A const bracket_elem_t *start_elem,
2N/A const bracket_elem_t *end_elem)
2N/A# endif /* not RE_ENABLE_I18N */
2N/A{
2N/A unsigned int start_ch, end_ch;
2N/A /* Equivalence Classes and Character Classes can't be a range start/end. */
2N/A if (BE (start_elem->type == EQUIV_CLASS || start_elem->type == CHAR_CLASS
2N/A || end_elem->type == EQUIV_CLASS || end_elem->type == CHAR_CLASS,
2N/A 0))
2N/A return REG_ERANGE;
2N/A
2N/A /* We can handle no multi character collating elements without libc
2N/A support. */
2N/A if (BE ((start_elem->type == COLL_SYM
2N/A && strlen ((char *) start_elem->opr.name) > 1)
2N/A || (end_elem->type == COLL_SYM
2N/A && strlen ((char *) end_elem->opr.name) > 1), 0))
2N/A return REG_ECOLLATE;
2N/A
2N/A# ifdef RE_ENABLE_I18N
2N/A {
2N/A wchar_t wc;
2N/A wint_t start_wc;
2N/A wint_t end_wc;
2N/A wchar_t cmp_buf[6] = {L'\0', L'\0', L'\0', L'\0', L'\0', L'\0'};
2N/A
2N/A start_ch = ((start_elem->type == SB_CHAR) ? start_elem->opr.ch
2N/A : ((start_elem->type == COLL_SYM) ? start_elem->opr.name[0]
2N/A : 0));
2N/A end_ch = ((end_elem->type == SB_CHAR) ? end_elem->opr.ch
2N/A : ((end_elem->type == COLL_SYM) ? end_elem->opr.name[0]
2N/A : 0));
2N/A start_wc = ((start_elem->type == SB_CHAR || start_elem->type == COLL_SYM)
2N/A ? __btowc (start_ch) : start_elem->opr.wch);
2N/A end_wc = ((end_elem->type == SB_CHAR || end_elem->type == COLL_SYM)
2N/A ? __btowc (end_ch) : end_elem->opr.wch);
2N/A if (start_wc == WEOF || end_wc == WEOF)
2N/A return REG_ECOLLATE;
2N/A cmp_buf[0] = start_wc;
2N/A cmp_buf[4] = end_wc;
2N/A
2N/A if (BE ((syntax & RE_NO_EMPTY_RANGES)
2N/A && wcscoll (cmp_buf, cmp_buf + 4) > 0, 0))
2N/A return REG_ERANGE;
2N/A
2N/A /* Got valid collation sequence values, add them as a new entry.
2N/A However, for !_LIBC we have no collation elements: if the
2N/A character set is single byte, the single byte character set
2N/A that we build below suffices. parse_bracket_exp passes
2N/A no MBCSET if dfa->mb_cur_max == 1. */
2N/A if (mbcset)
2N/A {
2N/A /* Check the space of the arrays. */
2N/A if (BE (*range_alloc == mbcset->nranges, 0))
2N/A {
2N/A /* There is not enough space, need realloc. */
2N/A wchar_t *new_array_start, *new_array_end;
2N/A Idx new_nranges;
2N/A
2N/A /* +1 in case of mbcset->nranges is 0. */
2N/A new_nranges = 2 * mbcset->nranges + 1;
2N/A /* Use realloc since mbcset->range_starts and mbcset->range_ends
2N/A are NULL if *range_alloc == 0. */
2N/A new_array_start = re_realloc (mbcset->range_starts, wchar_t,
2N/A new_nranges);
2N/A new_array_end = re_realloc (mbcset->range_ends, wchar_t,
2N/A new_nranges);
2N/A
2N/A if (BE (new_array_start == NULL || new_array_end == NULL, 0))
2N/A return REG_ESPACE;
2N/A
2N/A mbcset->range_starts = new_array_start;
2N/A mbcset->range_ends = new_array_end;
2N/A *range_alloc = new_nranges;
2N/A }
2N/A
2N/A mbcset->range_starts[mbcset->nranges] = start_wc;
2N/A mbcset->range_ends[mbcset->nranges++] = end_wc;
2N/A }
2N/A
2N/A /* Build the table for single byte characters. */
2N/A for (wc = 0; wc < SBC_MAX; ++wc)
2N/A {
2N/A cmp_buf[2] = wc;
2N/A if (wcscoll (cmp_buf, cmp_buf + 2) <= 0
2N/A && wcscoll (cmp_buf + 2, cmp_buf + 4) <= 0)
2N/A bitset_set (sbcset, wc);
2N/A }
2N/A }
2N/A# else /* not RE_ENABLE_I18N */
2N/A {
2N/A unsigned int ch;
2N/A start_ch = ((start_elem->type == SB_CHAR ) ? start_elem->opr.ch
2N/A : ((start_elem->type == COLL_SYM) ? start_elem->opr.name[0]
2N/A : 0));
2N/A end_ch = ((end_elem->type == SB_CHAR ) ? end_elem->opr.ch
2N/A : ((end_elem->type == COLL_SYM) ? end_elem->opr.name[0]
2N/A : 0));
2N/A if (start_ch > end_ch)
2N/A return REG_ERANGE;
2N/A /* Build the table for single byte characters. */
2N/A for (ch = 0; ch < SBC_MAX; ++ch)
2N/A if (start_ch <= ch && ch <= end_ch)
2N/A bitset_set (sbcset, ch);
2N/A }
2N/A# endif /* not RE_ENABLE_I18N */
2N/A return REG_NOERROR;
2N/A}
2N/A#endif /* not _LIBC */
2N/A
2N/A#ifndef _LIBC
2N/A/* Helper function for parse_bracket_exp only used in case of NOT _LIBC..
2N/A Build the collating element which is represented by NAME.
2N/A The result are written to MBCSET and SBCSET.
2N/A COLL_SYM_ALLOC is the allocated size of mbcset->coll_sym, is a
2N/A pointer argument since we may update it. */
2N/A
2N/Astatic reg_errcode_t
2N/Ainternal_function
2N/Abuild_collating_symbol (bitset_t sbcset,
2N/A# ifdef RE_ENABLE_I18N
2N/A re_charset_t *mbcset, Idx *coll_sym_alloc,
2N/A# endif
2N/A const unsigned char *name)
2N/A{
2N/A size_t name_len = strlen ((const char *) name);
2N/A if (BE (name_len != 1, 0))
2N/A return REG_ECOLLATE;
2N/A else
2N/A {
2N/A bitset_set (sbcset, name[0]);
2N/A return REG_NOERROR;
2N/A }
2N/A}
2N/A#endif /* not _LIBC */
2N/A
2N/A/* This function parse bracket expression like "[abc]", "[a-c]",
2N/A "[[.a-a.]]" etc. */
2N/A
2N/Astatic bin_tree_t *
2N/Aparse_bracket_exp (re_string_t *regexp, re_dfa_t *dfa, re_token_t *token,
2N/A reg_syntax_t syntax, reg_errcode_t *err)
2N/A{
2N/A#ifdef _LIBC
2N/A const unsigned char *collseqmb;
2N/A const char *collseqwc;
2N/A uint32_t nrules;
2N/A int32_t table_size;
2N/A const int32_t *symb_table;
2N/A const unsigned char *extra;
2N/A
2N/A /* Local function for parse_bracket_exp used in _LIBC environement.
2N/A Seek the collating symbol entry correspondings to NAME.
2N/A Return the index of the symbol in the SYMB_TABLE. */
2N/A
2N/A auto inline int32_t
2N/A __attribute ((always_inline))
2N/A seek_collating_symbol_entry (name, name_len)
2N/A const unsigned char *name;
2N/A size_t name_len;
2N/A {
2N/A int32_t hash = elem_hash ((const char *) name, name_len);
2N/A int32_t elem = hash % table_size;
2N/A if (symb_table[2 * elem] != 0)
2N/A {
2N/A int32_t second = hash % (table_size - 2) + 1;
2N/A
2N/A do
2N/A {
2N/A /* First compare the hashing value. */
2N/A if (symb_table[2 * elem] == hash
2N/A /* Compare the length of the name. */
2N/A && name_len == extra[symb_table[2 * elem + 1]]
2N/A /* Compare the name. */
2N/A && memcmp (name, &extra[symb_table[2 * elem + 1] + 1],
2N/A name_len) == 0)
2N/A {
2N/A /* Yep, this is the entry. */
2N/A break;
2N/A }
2N/A
2N/A /* Next entry. */
2N/A elem += second;
2N/A }
2N/A while (symb_table[2 * elem] != 0);
2N/A }
2N/A return elem;
2N/A }
2N/A
2N/A /* Local function for parse_bracket_exp used in _LIBC environment.
2N/A Look up the collation sequence value of BR_ELEM.
2N/A Return the value if succeeded, UINT_MAX otherwise. */
2N/A
2N/A auto inline unsigned int
2N/A __attribute ((always_inline))
2N/A lookup_collation_sequence_value (br_elem)
2N/A bracket_elem_t *br_elem;
2N/A {
2N/A if (br_elem->type == SB_CHAR)
2N/A {
2N/A /*
2N/A if (MB_CUR_MAX == 1)
2N/A */
2N/A if (nrules == 0)
2N/A return collseqmb[br_elem->opr.ch];
2N/A else
2N/A {
2N/A wint_t wc = __btowc (br_elem->opr.ch);
2N/A return __collseq_table_lookup (collseqwc, wc);
2N/A }
2N/A }
2N/A else if (br_elem->type == MB_CHAR)
2N/A {
2N/A if (nrules != 0)
2N/A return __collseq_table_lookup (collseqwc, br_elem->opr.wch);
2N/A }
2N/A else if (br_elem->type == COLL_SYM)
2N/A {
2N/A size_t sym_name_len = strlen ((char *) br_elem->opr.name);
2N/A if (nrules != 0)
2N/A {
2N/A int32_t elem, idx;
2N/A elem = seek_collating_symbol_entry (br_elem->opr.name,
2N/A sym_name_len);
2N/A if (symb_table[2 * elem] != 0)
2N/A {
2N/A /* We found the entry. */
2N/A idx = symb_table[2 * elem + 1];
2N/A /* Skip the name of collating element name. */
2N/A idx += 1 + extra[idx];
2N/A /* Skip the byte sequence of the collating element. */
2N/A idx += 1 + extra[idx];
2N/A /* Adjust for the alignment. */
2N/A idx = (idx + 3) & ~3;
2N/A /* Skip the multibyte collation sequence value. */
2N/A idx += sizeof (unsigned int);
2N/A /* Skip the wide char sequence of the collating element. */
2N/A idx += sizeof (unsigned int) *
2N/A (1 + *(unsigned int *) (extra + idx));
2N/A /* Return the collation sequence value. */
2N/A return *(unsigned int *) (extra + idx);
2N/A }
2N/A else if (symb_table[2 * elem] == 0 && sym_name_len == 1)
2N/A {
2N/A /* No valid character. Match it as a single byte
2N/A character. */
2N/A return collseqmb[br_elem->opr.name[0]];
2N/A }
2N/A }
2N/A else if (sym_name_len == 1)
2N/A return collseqmb[br_elem->opr.name[0]];
2N/A }
2N/A return UINT_MAX;
2N/A }
2N/A
2N/A /* Local function for parse_bracket_exp used in _LIBC environement.
2N/A Build the range expression which starts from START_ELEM, and ends
2N/A at END_ELEM. The result are written to MBCSET and SBCSET.
2N/A RANGE_ALLOC is the allocated size of mbcset->range_starts, and
2N/A mbcset->range_ends, is a pointer argument sinse we may
2N/A update it. */
2N/A
2N/A auto inline reg_errcode_t
2N/A __attribute ((always_inline))
2N/A build_range_exp (sbcset, mbcset, range_alloc, start_elem, end_elem)
2N/A re_charset_t *mbcset;
2N/A Idx *range_alloc;
2N/A bitset_t sbcset;
2N/A bracket_elem_t *start_elem, *end_elem;
2N/A {
2N/A unsigned int ch;
2N/A uint32_t start_collseq;
2N/A uint32_t end_collseq;
2N/A
2N/A /* Equivalence Classes and Character Classes can't be a range
2N/A start/end. */
2N/A if (BE (start_elem->type == EQUIV_CLASS || start_elem->type == CHAR_CLASS
2N/A || end_elem->type == EQUIV_CLASS || end_elem->type == CHAR_CLASS,
2N/A 0))
2N/A return REG_ERANGE;
2N/A
2N/A start_collseq = lookup_collation_sequence_value (start_elem);
2N/A end_collseq = lookup_collation_sequence_value (end_elem);
2N/A /* Check start/end collation sequence values. */
2N/A if (BE (start_collseq == UINT_MAX || end_collseq == UINT_MAX, 0))
2N/A return REG_ECOLLATE;
2N/A if (BE ((syntax & RE_NO_EMPTY_RANGES) && start_collseq > end_collseq, 0))
2N/A return REG_ERANGE;
2N/A
2N/A /* Got valid collation sequence values, add them as a new entry.
2N/A However, if we have no collation elements, and the character set
2N/A is single byte, the single byte character set that we
2N/A build below suffices. */
2N/A if (nrules > 0 || dfa->mb_cur_max > 1)
2N/A {
2N/A /* Check the space of the arrays. */
2N/A if (BE (*range_alloc == mbcset->nranges, 0))
2N/A {
2N/A /* There is not enough space, need realloc. */
2N/A uint32_t *new_array_start;
2N/A uint32_t *new_array_end;
2N/A Idx new_nranges;
2N/A
2N/A /* +1 in case of mbcset->nranges is 0. */
2N/A new_nranges = 2 * mbcset->nranges + 1;
2N/A new_array_start = re_realloc (mbcset->range_starts, uint32_t,
2N/A new_nranges);
2N/A new_array_end = re_realloc (mbcset->range_ends, uint32_t,
2N/A new_nranges);
2N/A
2N/A if (BE (new_array_start == NULL || new_array_end == NULL, 0))
2N/A return REG_ESPACE;
2N/A
2N/A mbcset->range_starts = new_array_start;
2N/A mbcset->range_ends = new_array_end;
2N/A *range_alloc = new_nranges;
2N/A }
2N/A
2N/A mbcset->range_starts[mbcset->nranges] = start_collseq;
2N/A mbcset->range_ends[mbcset->nranges++] = end_collseq;
2N/A }
2N/A
2N/A /* Build the table for single byte characters. */
2N/A for (ch = 0; ch < SBC_MAX; ch++)
2N/A {
2N/A uint32_t ch_collseq;
2N/A /*
2N/A if (MB_CUR_MAX == 1)
2N/A */
2N/A if (nrules == 0)
2N/A ch_collseq = collseqmb[ch];
2N/A else
2N/A ch_collseq = __collseq_table_lookup (collseqwc, __btowc (ch));
2N/A if (start_collseq <= ch_collseq && ch_collseq <= end_collseq)
2N/A bitset_set (sbcset, ch);
2N/A }
2N/A return REG_NOERROR;
2N/A }
2N/A
2N/A /* Local function for parse_bracket_exp used in _LIBC environement.
2N/A Build the collating element which is represented by NAME.
2N/A The result are written to MBCSET and SBCSET.
2N/A COLL_SYM_ALLOC is the allocated size of mbcset->coll_sym, is a
2N/A pointer argument sinse we may update it. */
2N/A
2N/A auto inline reg_errcode_t
2N/A __attribute ((always_inline))
2N/A build_collating_symbol (sbcset, mbcset, coll_sym_alloc, name)
2N/A re_charset_t *mbcset;
2N/A Idx *coll_sym_alloc;
2N/A bitset_t sbcset;
2N/A const unsigned char *name;
2N/A {
2N/A int32_t elem, idx;
2N/A size_t name_len = strlen ((const char *) name);
2N/A if (nrules != 0)
2N/A {
2N/A elem = seek_collating_symbol_entry (name, name_len);
2N/A if (symb_table[2 * elem] != 0)
2N/A {
2N/A /* We found the entry. */
2N/A idx = symb_table[2 * elem + 1];
2N/A /* Skip the name of collating element name. */
2N/A idx += 1 + extra[idx];
2N/A }
2N/A else if (symb_table[2 * elem] == 0 && name_len == 1)
2N/A {
2N/A /* No valid character, treat it as a normal
2N/A character. */
2N/A bitset_set (sbcset, name[0]);
2N/A return REG_NOERROR;
2N/A }
2N/A else
2N/A return REG_ECOLLATE;
2N/A
2N/A /* Got valid collation sequence, add it as a new entry. */
2N/A /* Check the space of the arrays. */
2N/A if (BE (*coll_sym_alloc == mbcset->ncoll_syms, 0))
2N/A {
2N/A /* Not enough, realloc it. */
2N/A /* +1 in case of mbcset->ncoll_syms is 0. */
2N/A Idx new_coll_sym_alloc = 2 * mbcset->ncoll_syms + 1;
2N/A /* Use realloc since mbcset->coll_syms is NULL
2N/A if *alloc == 0. */
2N/A int32_t *new_coll_syms = re_realloc (mbcset->coll_syms, int32_t,
2N/A new_coll_sym_alloc);
2N/A if (BE (new_coll_syms == NULL, 0))
2N/A return REG_ESPACE;
2N/A mbcset->coll_syms = new_coll_syms;
2N/A *coll_sym_alloc = new_coll_sym_alloc;
2N/A }
2N/A mbcset->coll_syms[mbcset->ncoll_syms++] = idx;
2N/A return REG_NOERROR;
2N/A }
2N/A else
2N/A {
2N/A if (BE (name_len != 1, 0))
2N/A return REG_ECOLLATE;
2N/A else
2N/A {
2N/A bitset_set (sbcset, name[0]);
2N/A return REG_NOERROR;
2N/A }
2N/A }
2N/A }
2N/A#endif
2N/A
2N/A re_token_t br_token;
2N/A re_bitset_ptr_t sbcset;
2N/A#ifdef RE_ENABLE_I18N
2N/A re_charset_t *mbcset;
2N/A Idx coll_sym_alloc = 0, range_alloc = 0, mbchar_alloc = 0;
2N/A Idx equiv_class_alloc = 0, char_class_alloc = 0;
2N/A#endif /* not RE_ENABLE_I18N */
2N/A bool non_match = false;
2N/A bin_tree_t *work_tree;
2N/A int token_len;
2N/A bool first_round = true;
2N/A#ifdef _LIBC
2N/A collseqmb = (const unsigned char *)
2N/A _NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQMB);
2N/A nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
2N/A if (nrules)
2N/A {
2N/A /*
2N/A if (MB_CUR_MAX > 1)
2N/A */
2N/A collseqwc = _NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQWC);
2N/A table_size = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_SYMB_HASH_SIZEMB);
2N/A symb_table = (const int32_t *) _NL_CURRENT (LC_COLLATE,
2N/A _NL_COLLATE_SYMB_TABLEMB);
2N/A extra = (const unsigned char *) _NL_CURRENT (LC_COLLATE,
2N/A _NL_COLLATE_SYMB_EXTRAMB);
2N/A }
2N/A#endif
2N/A sbcset = (re_bitset_ptr_t) calloc (sizeof (bitset_t), 1);
2N/A#ifdef RE_ENABLE_I18N
2N/A mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1);
2N/A#endif /* RE_ENABLE_I18N */
2N/A#ifdef RE_ENABLE_I18N
2N/A if (BE (sbcset == NULL || mbcset == NULL, 0))
2N/A#else
2N/A if (BE (sbcset == NULL, 0))
2N/A#endif /* RE_ENABLE_I18N */
2N/A {
2N/A if (sbcset != NULL)
2N/A re_free(sbcset);
2N/A#ifdef RE_ENABLE_I18N
2N/A if (mbcset != NULL)
2N/A free_charset(mbcset);
2N/A#endif
2N/A *err = REG_ESPACE;
2N/A return NULL;
2N/A }
2N/A
2N/A token_len = peek_token_bracket (token, regexp, syntax);
2N/A if (BE (token->type == END_OF_RE, 0))
2N/A {
2N/A *err = REG_BADPAT;
2N/A goto parse_bracket_exp_free_return;
2N/A }
2N/A if (token->type == OP_NON_MATCH_LIST)
2N/A {
2N/A#ifdef RE_ENABLE_I18N
2N/A mbcset->non_match = 1;
2N/A#endif /* not RE_ENABLE_I18N */
2N/A non_match = true;
2N/A if (syntax & RE_HAT_LISTS_NOT_NEWLINE)
2N/A bitset_set (sbcset, '\n');
2N/A re_string_skip_bytes (regexp, token_len); /* Skip a token. */
2N/A token_len = peek_token_bracket (token, regexp, syntax);
2N/A if (BE (token->type == END_OF_RE, 0))
2N/A {
2N/A *err = REG_BADPAT;
2N/A goto parse_bracket_exp_free_return;
2N/A }
2N/A }
2N/A
2N/A /* We treat the first ']' as a normal character. */
2N/A if (token->type == OP_CLOSE_BRACKET)
2N/A token->type = CHARACTER;
2N/A
2N/A while (1)
2N/A {
2N/A bracket_elem_t start_elem, end_elem;
2N/A unsigned char start_name_buf[BRACKET_NAME_BUF_SIZE];
2N/A unsigned char end_name_buf[BRACKET_NAME_BUF_SIZE];
2N/A reg_errcode_t ret;
2N/A int token_len2 = 0;
2N/A bool is_range_exp = false;
2N/A re_token_t token2;
2N/A
2N/A start_elem.opr.name = start_name_buf;
2N/A ret = parse_bracket_element (&start_elem, regexp, token, token_len, dfa,
2N/A syntax, first_round);
2N/A if (BE (ret != REG_NOERROR, 0))
2N/A {
2N/A *err = ret;
2N/A goto parse_bracket_exp_free_return;
2N/A }
2N/A first_round = false;
2N/A
2N/A /* Get information about the next token. We need it in any case. */
2N/A token_len = peek_token_bracket (token, regexp, syntax);
2N/A
2N/A /* Do not check for ranges if we know they are not allowed. */
2N/A if (start_elem.type != CHAR_CLASS && start_elem.type != EQUIV_CLASS)
2N/A {
2N/A if (BE (token->type == END_OF_RE, 0))
2N/A {
2N/A *err = REG_EBRACK;
2N/A goto parse_bracket_exp_free_return;
2N/A }
2N/A if (token->type == OP_CHARSET_RANGE)
2N/A {
2N/A re_string_skip_bytes (regexp, token_len); /* Skip '-'. */
2N/A token_len2 = peek_token_bracket (&token2, regexp, syntax);
2N/A if (BE (token2.type == END_OF_RE, 0))
2N/A {
2N/A *err = REG_EBRACK;
2N/A goto parse_bracket_exp_free_return;
2N/A }
2N/A if (token2.type == OP_CLOSE_BRACKET)
2N/A {
2N/A /* We treat the last '-' as a normal character. */
2N/A re_string_skip_bytes (regexp, -token_len);
2N/A token->type = CHARACTER;
2N/A }
2N/A else
2N/A is_range_exp = true;
2N/A }
2N/A }
2N/A
2N/A if (is_range_exp == true)
2N/A {
2N/A end_elem.opr.name = end_name_buf;
2N/A ret = parse_bracket_element (&end_elem, regexp, &token2, token_len2,
2N/A dfa, syntax, true);
2N/A if (BE (ret != REG_NOERROR, 0))
2N/A {
2N/A *err = ret;
2N/A goto parse_bracket_exp_free_return;
2N/A }
2N/A
2N/A token_len = peek_token_bracket (token, regexp, syntax);
2N/A
2N/A#ifdef _LIBC
2N/A *err = build_range_exp (sbcset, mbcset, &range_alloc,
2N/A &start_elem, &end_elem);
2N/A#else
2N/A# ifdef RE_ENABLE_I18N
2N/A *err = build_range_exp (syntax, sbcset,
2N/A dfa->mb_cur_max > 1 ? mbcset : NULL,
2N/A &range_alloc, &start_elem, &end_elem);
2N/A# else
2N/A *err = build_range_exp (syntax, sbcset, &start_elem, &end_elem);
2N/A# endif
2N/A#endif /* RE_ENABLE_I18N */
2N/A if (BE (*err != REG_NOERROR, 0))
2N/A goto parse_bracket_exp_free_return;
2N/A }
2N/A else
2N/A {
2N/A switch (start_elem.type)
2N/A {
2N/A case SB_CHAR:
2N/A bitset_set (sbcset, start_elem.opr.ch);
2N/A break;
2N/A#ifdef RE_ENABLE_I18N
2N/A case MB_CHAR:
2N/A /* Check whether the array has enough space. */
2N/A if (BE (mbchar_alloc == mbcset->nmbchars, 0))
2N/A {
2N/A wchar_t *new_mbchars;
2N/A /* Not enough, realloc it. */
2N/A /* +1 in case of mbcset->nmbchars is 0. */
2N/A mbchar_alloc = 2 * mbcset->nmbchars + 1;
2N/A /* Use realloc since array is NULL if *alloc == 0. */
2N/A new_mbchars = re_realloc (mbcset->mbchars, wchar_t,
2N/A mbchar_alloc);
2N/A if (BE (new_mbchars == NULL, 0))
2N/A goto parse_bracket_exp_espace;
2N/A mbcset->mbchars = new_mbchars;
2N/A }
2N/A mbcset->mbchars[mbcset->nmbchars++] = start_elem.opr.wch;
2N/A break;
2N/A#endif /* RE_ENABLE_I18N */
2N/A case EQUIV_CLASS:
2N/A *err = build_equiv_class (sbcset,
2N/A#ifdef RE_ENABLE_I18N
2N/A mbcset, &equiv_class_alloc,
2N/A#endif /* RE_ENABLE_I18N */
2N/A start_elem.opr.name);
2N/A if (BE (*err != REG_NOERROR, 0))
2N/A goto parse_bracket_exp_free_return;
2N/A break;
2N/A case COLL_SYM:
2N/A *err = build_collating_symbol (sbcset,
2N/A#ifdef RE_ENABLE_I18N
2N/A mbcset, &coll_sym_alloc,
2N/A#endif /* RE_ENABLE_I18N */
2N/A start_elem.opr.name);
2N/A if (BE (*err != REG_NOERROR, 0))
2N/A goto parse_bracket_exp_free_return;
2N/A break;
2N/A case CHAR_CLASS:
2N/A *err = build_charclass (regexp->trans, sbcset,
2N/A#ifdef RE_ENABLE_I18N
2N/A mbcset, &char_class_alloc,
2N/A#endif /* RE_ENABLE_I18N */
2N/A start_elem.opr.name, syntax);
2N/A if (BE (*err != REG_NOERROR, 0))
2N/A goto parse_bracket_exp_free_return;
2N/A break;
2N/A default:
2N/A assert (0);
2N/A break;
2N/A }
2N/A }
2N/A if (BE (token->type == END_OF_RE, 0))
2N/A {
2N/A *err = REG_EBRACK;
2N/A goto parse_bracket_exp_free_return;
2N/A }
2N/A if (token->type == OP_CLOSE_BRACKET)
2N/A break;
2N/A }
2N/A
2N/A re_string_skip_bytes (regexp, token_len); /* Skip a token. */
2N/A
2N/A /* If it is non-matching list. */
2N/A if (non_match)
2N/A bitset_not (sbcset);
2N/A
2N/A#ifdef RE_ENABLE_I18N
2N/A /* Ensure only single byte characters are set. */
2N/A if (dfa->mb_cur_max > 1)
2N/A bitset_mask (sbcset, dfa->sb_char);
2N/A
2N/A if (mbcset->nmbchars || mbcset->ncoll_syms || mbcset->nequiv_classes
2N/A || mbcset->nranges || (dfa->mb_cur_max > 1 && (mbcset->nchar_classes
2N/A || mbcset->non_match)))
2N/A {
2N/A bin_tree_t *mbc_tree;
2N/A int sbc_idx;
2N/A /* Build a tree for complex bracket. */
2N/A dfa->has_mb_node = 1;
2N/A br_token.type = COMPLEX_BRACKET;
2N/A br_token.opr.mbcset = mbcset;
2N/A mbc_tree = create_token_tree (dfa, NULL, NULL, &br_token);
2N/A if (BE (mbc_tree == NULL, 0))
2N/A goto parse_bracket_exp_espace;
2N/A for (sbc_idx = 0; sbc_idx < BITSET_WORDS; ++sbc_idx)
2N/A if (sbcset[sbc_idx])
2N/A break;
2N/A /* If there are no bits set in sbcset, there is no point
2N/A of having both SIMPLE_BRACKET and COMPLEX_BRACKET. */
2N/A if (sbc_idx < BITSET_WORDS)
2N/A {
2N/A /* Build a tree for simple bracket. */
2N/A br_token.type = SIMPLE_BRACKET;
2N/A br_token.opr.sbcset = sbcset;
2N/A work_tree = create_token_tree (dfa, NULL, NULL, &br_token);
2N/A if (BE (work_tree == NULL, 0))
2N/A goto parse_bracket_exp_espace;
2N/A
2N/A /* Then join them by ALT node. */
2N/A work_tree = create_tree (dfa, work_tree, mbc_tree, OP_ALT);
2N/A if (BE (work_tree == NULL, 0))
2N/A goto parse_bracket_exp_espace;
2N/A }
2N/A else
2N/A {
2N/A re_free (sbcset);
2N/A work_tree = mbc_tree;
2N/A }
2N/A }
2N/A else
2N/A#endif /* not RE_ENABLE_I18N */
2N/A {
2N/A#ifdef RE_ENABLE_I18N
2N/A free_charset (mbcset);
2N/A mbcset = NULL;
2N/A#endif
2N/A /* Build a tree for simple bracket. */
2N/A br_token.type = SIMPLE_BRACKET;
2N/A br_token.opr.sbcset = sbcset;
2N/A work_tree = create_token_tree (dfa, NULL, NULL, &br_token);
2N/A if (BE (work_tree == NULL, 0))
2N/A goto parse_bracket_exp_espace;
2N/A }
2N/A return work_tree;
2N/A
2N/A parse_bracket_exp_espace:
2N/A *err = REG_ESPACE;
2N/A parse_bracket_exp_free_return:
2N/A re_free (sbcset);
2N/A#ifdef RE_ENABLE_I18N
2N/A if (mbcset != NULL)
2N/A free_charset (mbcset);
2N/A#endif /* RE_ENABLE_I18N */
2N/A return NULL;
2N/A}
2N/A
2N/A/* Parse an element in the bracket expression. */
2N/A
2N/Astatic reg_errcode_t
2N/Aparse_bracket_element (bracket_elem_t *elem, re_string_t *regexp,
2N/A re_token_t *token, int token_len, re_dfa_t *dfa,
2N/A reg_syntax_t syntax, bool accept_hyphen)
2N/A{
2N/A#ifdef RE_ENABLE_I18N
2N/A int cur_char_size;
2N/A cur_char_size = re_string_char_size_at (regexp, re_string_cur_idx (regexp));
2N/A if (cur_char_size > 1)
2N/A {
2N/A elem->type = MB_CHAR;
2N/A elem->opr.wch = re_string_wchar_at (regexp, re_string_cur_idx (regexp));
2N/A re_string_skip_bytes (regexp, cur_char_size);
2N/A return REG_NOERROR;
2N/A }
2N/A#endif /* RE_ENABLE_I18N */
2N/A re_string_skip_bytes (regexp, token_len); /* Skip a token. */
2N/A if (token->type == OP_OPEN_COLL_ELEM || token->type == OP_OPEN_CHAR_CLASS
2N/A || token->type == OP_OPEN_EQUIV_CLASS)
2N/A return parse_bracket_symbol (elem, regexp, token);
2N/A if (BE (token->type == OP_CHARSET_RANGE, 0) && !accept_hyphen)
2N/A {
2N/A /* A '-' must only appear as anything but a range indicator before
2N/A the closing bracket. Everything else is an error. */
2N/A re_token_t token2;
2N/A (void) peek_token_bracket (&token2, regexp, syntax);
2N/A if (token2.type != OP_CLOSE_BRACKET)
2N/A /* The actual error value is not standardized since this whole
2N/A case is undefined. But ERANGE makes good sense. */
2N/A return REG_ERANGE;
2N/A }
2N/A elem->type = SB_CHAR;
2N/A elem->opr.ch = token->opr.c;
2N/A return REG_NOERROR;
2N/A}
2N/A
2N/A/* Parse a bracket symbol in the bracket expression. Bracket symbols are
2N/A such as [:<character_class>:], [.<collating_element>.], and
2N/A [=<equivalent_class>=]. */
2N/A
2N/Astatic reg_errcode_t
2N/Aparse_bracket_symbol (bracket_elem_t *elem, re_string_t *regexp,
2N/A re_token_t *token)
2N/A{
2N/A unsigned char ch, delim = token->opr.c;
2N/A int i = 0;
2N/A if (re_string_eoi(regexp))
2N/A return REG_EBRACK;
2N/A for (;; ++i)
2N/A {
2N/A if (i >= BRACKET_NAME_BUF_SIZE)
2N/A return REG_EBRACK;
2N/A if (token->type == OP_OPEN_CHAR_CLASS)
2N/A ch = re_string_fetch_byte_case (regexp);
2N/A else
2N/A ch = re_string_fetch_byte (regexp);
2N/A if (re_string_eoi(regexp))
2N/A return REG_EBRACK;
2N/A if (ch == delim && re_string_peek_byte (regexp, 0) == ']')
2N/A break;
2N/A elem->opr.name[i] = ch;
2N/A }
2N/A re_string_skip_bytes (regexp, 1);
2N/A elem->opr.name[i] = '\0';
2N/A switch (token->type)
2N/A {
2N/A case OP_OPEN_COLL_ELEM:
2N/A elem->type = COLL_SYM;
2N/A break;
2N/A case OP_OPEN_EQUIV_CLASS:
2N/A elem->type = EQUIV_CLASS;
2N/A break;
2N/A case OP_OPEN_CHAR_CLASS:
2N/A elem->type = CHAR_CLASS;
2N/A break;
2N/A default:
2N/A break;
2N/A }
2N/A return REG_NOERROR;
2N/A}
2N/A
2N/A /* Helper function for parse_bracket_exp.
2N/A Build the equivalence class which is represented by NAME.
2N/A The result are written to MBCSET and SBCSET.
2N/A EQUIV_CLASS_ALLOC is the allocated size of mbcset->equiv_classes,
2N/A is a pointer argument sinse we may update it. */
2N/A
2N/Astatic reg_errcode_t
2N/A#ifdef RE_ENABLE_I18N
2N/Abuild_equiv_class (bitset_t sbcset, re_charset_t *mbcset,
2N/A Idx *equiv_class_alloc, const unsigned char *name)
2N/A#else /* not RE_ENABLE_I18N */
2N/Abuild_equiv_class (bitset_t sbcset, const unsigned char *name)
2N/A#endif /* not RE_ENABLE_I18N */
2N/A{
2N/A#ifdef _LIBC
2N/A uint32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
2N/A if (nrules != 0)
2N/A {
2N/A const int32_t *table, *indirect;
2N/A const unsigned char *weights, *extra, *cp;
2N/A unsigned char char_buf[2];
2N/A int32_t idx1, idx2;
2N/A unsigned int ch;
2N/A size_t len;
2N/A /* This #include defines a local function! */
2N/A# include <locale/weight.h>
2N/A /* Calculate the index for equivalence class. */
2N/A cp = name;
2N/A table = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
2N/A weights = (const unsigned char *) _NL_CURRENT (LC_COLLATE,
2N/A _NL_COLLATE_WEIGHTMB);
2N/A extra = (const unsigned char *) _NL_CURRENT (LC_COLLATE,
2N/A _NL_COLLATE_EXTRAMB);
2N/A indirect = (const int32_t *) _NL_CURRENT (LC_COLLATE,
2N/A _NL_COLLATE_INDIRECTMB);
2N/A idx1 = findidx (&cp);
2N/A if (BE (idx1 == 0 || cp < name + strlen ((const char *) name), 0))
2N/A /* This isn't a valid character. */
2N/A return REG_ECOLLATE;
2N/A
2N/A /* Build single byte matcing table for this equivalence class. */
2N/A char_buf[1] = (unsigned char) '\0';
2N/A len = weights[idx1 & 0xffffff];
2N/A for (ch = 0; ch < SBC_MAX; ++ch)
2N/A {
2N/A char_buf[0] = ch;
2N/A cp = char_buf;
2N/A idx2 = findidx (&cp);
2N/A/*
2N/A idx2 = table[ch];
2N/A*/
2N/A if (idx2 == 0)
2N/A /* This isn't a valid character. */
2N/A continue;
2N/A /* Compare only if the length matches and the collation rule
2N/A index is the same. */
2N/A if (len == weights[idx2 & 0xffffff] && (idx1 >> 24) == (idx2 >> 24))
2N/A {
2N/A int cnt = 0;
2N/A
2N/A while (cnt <= len &&
2N/A weights[(idx1 & 0xffffff) + 1 + cnt]
2N/A == weights[(idx2 & 0xffffff) + 1 + cnt])
2N/A ++cnt;
2N/A
2N/A if (cnt > len)
2N/A bitset_set (sbcset, ch);
2N/A }
2N/A }
2N/A /* Check whether the array has enough space. */
2N/A if (BE (*equiv_class_alloc == mbcset->nequiv_classes, 0))
2N/A {
2N/A /* Not enough, realloc it. */
2N/A /* +1 in case of mbcset->nequiv_classes is 0. */
2N/A Idx new_equiv_class_alloc = 2 * mbcset->nequiv_classes + 1;
2N/A /* Use realloc since the array is NULL if *alloc == 0. */
2N/A int32_t *new_equiv_classes = re_realloc (mbcset->equiv_classes,
2N/A int32_t,
2N/A new_equiv_class_alloc);
2N/A if (BE (new_equiv_classes == NULL, 0))
2N/A return REG_ESPACE;
2N/A mbcset->equiv_classes = new_equiv_classes;
2N/A *equiv_class_alloc = new_equiv_class_alloc;
2N/A }
2N/A mbcset->equiv_classes[mbcset->nequiv_classes++] = idx1;
2N/A }
2N/A else
2N/A#endif /* _LIBC */
2N/A {
2N/A if (BE (strlen ((const char *) name) != 1, 0))
2N/A return REG_ECOLLATE;
2N/A bitset_set (sbcset, *name);
2N/A }
2N/A return REG_NOERROR;
2N/A}
2N/A
2N/A /* Helper function for parse_bracket_exp.
2N/A Build the character class which is represented by NAME.
2N/A The result are written to MBCSET and SBCSET.
2N/A CHAR_CLASS_ALLOC is the allocated size of mbcset->char_classes,
2N/A is a pointer argument sinse we may update it. */
2N/A
2N/Astatic reg_errcode_t
2N/A#ifdef RE_ENABLE_I18N
2N/Abuild_charclass (RE_TRANSLATE_TYPE trans, bitset_t sbcset,
2N/A re_charset_t *mbcset, Idx *char_class_alloc,
2N/A const unsigned char *class_name, reg_syntax_t syntax)
2N/A#else /* not RE_ENABLE_I18N */
2N/Abuild_charclass (RE_TRANSLATE_TYPE trans, bitset_t sbcset,
2N/A const unsigned char *class_name, reg_syntax_t syntax)
2N/A#endif /* not RE_ENABLE_I18N */
2N/A{
2N/A int i;
2N/A const char *name = (const char *) class_name;
2N/A
2N/A /* In case of REG_ICASE "upper" and "lower" match the both of
2N/A upper and lower cases. */
2N/A if ((syntax & RE_ICASE)
2N/A && (strcmp (name, "upper") == 0 || strcmp (name, "lower") == 0))
2N/A name = "alpha";
2N/A
2N/A#ifdef RE_ENABLE_I18N
2N/A /* Check the space of the arrays. */
2N/A if (BE (*char_class_alloc == mbcset->nchar_classes, 0))
2N/A {
2N/A /* Not enough, realloc it. */
2N/A /* +1 in case of mbcset->nchar_classes is 0. */
2N/A Idx new_char_class_alloc = 2 * mbcset->nchar_classes + 1;
2N/A /* Use realloc since array is NULL if *alloc == 0. */
2N/A wctype_t *new_char_classes = re_realloc (mbcset->char_classes, wctype_t,
2N/A new_char_class_alloc);
2N/A if (BE (new_char_classes == NULL, 0))
2N/A return REG_ESPACE;
2N/A mbcset->char_classes = new_char_classes;
2N/A *char_class_alloc = new_char_class_alloc;
2N/A }
2N/A mbcset->char_classes[mbcset->nchar_classes++] = __wctype (name);
2N/A#endif /* RE_ENABLE_I18N */
2N/A
2N/A#define BUILD_CHARCLASS_LOOP(ctype_func) \
2N/A do { \
2N/A if (BE (trans != NULL, 0)) \
2N/A { \
2N/A for (i = 0; i < SBC_MAX; ++i) \
2N/A if (ctype_func (i)) \
2N/A bitset_set (sbcset, trans[i]); \
2N/A } \
2N/A else \
2N/A { \
2N/A for (i = 0; i < SBC_MAX; ++i) \
2N/A if (ctype_func (i)) \
2N/A bitset_set (sbcset, i); \
2N/A } \
2N/A } while (0)
2N/A
2N/A if (strcmp (name, "alnum") == 0)
2N/A BUILD_CHARCLASS_LOOP (isalnum);
2N/A else if (strcmp (name, "cntrl") == 0)
2N/A BUILD_CHARCLASS_LOOP (iscntrl);
2N/A else if (strcmp (name, "lower") == 0)
2N/A BUILD_CHARCLASS_LOOP (islower);
2N/A else if (strcmp (name, "space") == 0)
2N/A BUILD_CHARCLASS_LOOP (isspace);
2N/A else if (strcmp (name, "alpha") == 0)
2N/A BUILD_CHARCLASS_LOOP (isalpha);
2N/A else if (strcmp (name, "digit") == 0)
2N/A BUILD_CHARCLASS_LOOP (isdigit);
2N/A else if (strcmp (name, "print") == 0)
2N/A BUILD_CHARCLASS_LOOP (isprint);
2N/A else if (strcmp (name, "upper") == 0)
2N/A BUILD_CHARCLASS_LOOP (isupper);
2N/A else if (strcmp (name, "blank") == 0)
2N/A BUILD_CHARCLASS_LOOP (isblank);
2N/A else if (strcmp (name, "graph") == 0)
2N/A BUILD_CHARCLASS_LOOP (isgraph);
2N/A else if (strcmp (name, "punct") == 0)
2N/A BUILD_CHARCLASS_LOOP (ispunct);
2N/A else if (strcmp (name, "xdigit") == 0)
2N/A BUILD_CHARCLASS_LOOP (isxdigit);
2N/A else
2N/A return REG_ECTYPE;
2N/A
2N/A return REG_NOERROR;
2N/A}
2N/A
2N/Astatic bin_tree_t *
2N/Abuild_charclass_op (re_dfa_t *dfa, RE_TRANSLATE_TYPE trans,
2N/A const unsigned char *class_name,
2N/A const unsigned char *extra, bool non_match,
2N/A reg_errcode_t *err)
2N/A{
2N/A re_bitset_ptr_t sbcset;
2N/A#ifdef RE_ENABLE_I18N
2N/A re_charset_t *mbcset;
2N/A Idx alloc = 0;
2N/A#endif /* not RE_ENABLE_I18N */
2N/A reg_errcode_t ret;
2N/A re_token_t br_token;
2N/A bin_tree_t *tree;
2N/A
2N/A sbcset = (re_bitset_ptr_t) calloc (sizeof (bitset_t), 1);
2N/A#ifdef RE_ENABLE_I18N
2N/A mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1);
2N/A#endif /* RE_ENABLE_I18N */
2N/A
2N/A#ifdef RE_ENABLE_I18N
2N/A if (BE (sbcset == NULL || mbcset == NULL, 0))
2N/A#else /* not RE_ENABLE_I18N */
2N/A if (BE (sbcset == NULL, 0))
2N/A#endif /* not RE_ENABLE_I18N */
2N/A {
2N/A if (sbcset != NULL)
2N/A re_free(sbcset);
2N/A#ifdef RE_ENABLE_I18N
2N/A if (mbcset != NULL)
2N/A free_charset(mbcset);
2N/A#endif
2N/A *err = REG_ESPACE;
2N/A return NULL;
2N/A }
2N/A
2N/A if (non_match)
2N/A {
2N/A#ifdef RE_ENABLE_I18N
2N/A mbcset->non_match = 1;
2N/A#endif /* not RE_ENABLE_I18N */
2N/A }
2N/A
2N/A /* We don't care the syntax in this case. */
2N/A ret = build_charclass (trans, sbcset,
2N/A#ifdef RE_ENABLE_I18N
2N/A mbcset, &alloc,
2N/A#endif /* RE_ENABLE_I18N */
2N/A class_name, 0);
2N/A
2N/A if (BE (ret != REG_NOERROR, 0))
2N/A {
2N/A re_free (sbcset);
2N/A#ifdef RE_ENABLE_I18N
2N/A free_charset (mbcset);
2N/A#endif /* RE_ENABLE_I18N */
2N/A *err = ret;
2N/A return NULL;
2N/A }
2N/A /* \w match '_' also. */
2N/A for (; *extra; extra++)
2N/A bitset_set (sbcset, *extra);
2N/A
2N/A /* If it is non-matching list. */
2N/A if (non_match)
2N/A bitset_not (sbcset);
2N/A
2N/A#ifdef RE_ENABLE_I18N
2N/A /* Ensure only single byte characters are set. */
2N/A if (dfa->mb_cur_max > 1)
2N/A bitset_mask (sbcset, dfa->sb_char);
2N/A#endif
2N/A
2N/A /* Build a tree for simple bracket. */
2N/A br_token.type = SIMPLE_BRACKET;
2N/A br_token.opr.sbcset = sbcset;
2N/A tree = create_token_tree (dfa, NULL, NULL, &br_token);
2N/A if (BE (tree == NULL, 0))
2N/A goto build_word_op_espace;
2N/A
2N/A#ifdef RE_ENABLE_I18N
2N/A if (dfa->mb_cur_max > 1)
2N/A {
2N/A bin_tree_t *mbc_tree;
2N/A /* Build a tree for complex bracket. */
2N/A br_token.type = COMPLEX_BRACKET;
2N/A br_token.opr.mbcset = mbcset;
2N/A dfa->has_mb_node = 1;
2N/A mbc_tree = create_token_tree (dfa, NULL, NULL, &br_token);
2N/A if (BE (mbc_tree == NULL, 0))
2N/A goto build_word_op_espace;
2N/A /* Then join them by ALT node. */
2N/A tree = create_tree (dfa, tree, mbc_tree, OP_ALT);
2N/A if (BE (mbc_tree != NULL, 1))
2N/A return tree;
2N/A }
2N/A else
2N/A {
2N/A free_charset (mbcset);
2N/A return tree;
2N/A }
2N/A#else /* not RE_ENABLE_I18N */
2N/A return tree;
2N/A#endif /* not RE_ENABLE_I18N */
2N/A
2N/A build_word_op_espace:
2N/A re_free (sbcset);
2N/A#ifdef RE_ENABLE_I18N
2N/A free_charset (mbcset);
2N/A#endif /* RE_ENABLE_I18N */
2N/A *err = REG_ESPACE;
2N/A return NULL;
2N/A}
2N/A
2N/A/* This is intended for the expressions like "a{1,3}".
2N/A Fetch a number from `input', and return the number.
2N/A Return REG_MISSING if the number field is empty like "{,1}".
2N/A Return REG_ERROR if an error occurred. */
2N/A
2N/Astatic Idx
2N/Afetch_number (re_string_t *input, re_token_t *token, reg_syntax_t syntax)
2N/A{
2N/A Idx num = REG_MISSING;
2N/A unsigned char c;
2N/A while (1)
2N/A {
2N/A fetch_token (token, input, syntax);
2N/A c = token->opr.c;
2N/A if (BE (token->type == END_OF_RE, 0))
2N/A return REG_ERROR;
2N/A if (token->type == OP_CLOSE_DUP_NUM || c == ',')
2N/A break;
2N/A num = ((token->type != CHARACTER || c < '0' || '9' < c
2N/A || num == REG_ERROR)
2N/A ? REG_ERROR
2N/A : ((num == REG_MISSING) ? c - '0' : num * 10 + c - '0'));
2N/A num = (num > RE_DUP_MAX) ? REG_ERROR : num;
2N/A }
2N/A return num;
2N/A}
2N/A
2N/A#ifdef RE_ENABLE_I18N
2N/Astatic void
2N/Afree_charset (re_charset_t *cset)
2N/A{
2N/A re_free (cset->mbchars);
2N/A# ifdef _LIBC
2N/A re_free (cset->coll_syms);
2N/A re_free (cset->equiv_classes);
2N/A re_free (cset->range_starts);
2N/A re_free (cset->range_ends);
2N/A# endif
2N/A re_free (cset->char_classes);
2N/A re_free (cset);
2N/A}
2N/A#endif /* RE_ENABLE_I18N */
2N/A
2N/A/* Functions for binary tree operation. */
2N/A
2N/A/* Create a tree node. */
2N/A
2N/Astatic bin_tree_t *
2N/Acreate_tree (re_dfa_t *dfa, bin_tree_t *left, bin_tree_t *right,
2N/A re_token_type_t type)
2N/A{
2N/A re_token_t t;
2N/A t.type = type;
2N/A return create_token_tree (dfa, left, right, &t);
2N/A}
2N/A
2N/Astatic bin_tree_t *
2N/Acreate_token_tree (re_dfa_t *dfa, bin_tree_t *left, bin_tree_t *right,
2N/A const re_token_t *token)
2N/A{
2N/A bin_tree_t *tree;
2N/A if (BE (dfa->str_tree_storage_idx == BIN_TREE_STORAGE_SIZE, 0))
2N/A {
2N/A bin_tree_storage_t *storage = re_malloc (bin_tree_storage_t, 1);
2N/A
2N/A if (storage == NULL)
2N/A return NULL;
2N/A storage->next = dfa->str_tree_storage;
2N/A dfa->str_tree_storage = storage;
2N/A dfa->str_tree_storage_idx = 0;
2N/A }
2N/A tree = &dfa->str_tree_storage->data[dfa->str_tree_storage_idx++];
2N/A
2N/A tree->parent = NULL;
2N/A tree->left = left;
2N/A tree->right = right;
2N/A tree->token = *token;
2N/A tree->token.duplicated = 0;
2N/A tree->token.opt_subexp = 0;
2N/A tree->first = NULL;
2N/A tree->next = NULL;
2N/A tree->node_idx = REG_MISSING;
2N/A
2N/A if (left != NULL)
2N/A left->parent = tree;
2N/A if (right != NULL)
2N/A right->parent = tree;
2N/A return tree;
2N/A}
2N/A
2N/A/* Mark the tree SRC as an optional subexpression.
2N/A To be called from preorder or postorder. */
2N/A
2N/Astatic reg_errcode_t
2N/Amark_opt_subexp (void *extra, bin_tree_t *node)
2N/A{
2N/A Idx idx = (Idx) (long) extra;
2N/A if (node->token.type == SUBEXP && node->token.opr.idx == idx)
2N/A node->token.opt_subexp = 1;
2N/A
2N/A return REG_NOERROR;
2N/A}
2N/A
2N/A/* Free the allocated memory inside NODE. */
2N/A
2N/Astatic void
2N/Afree_token (re_token_t *node)
2N/A{
2N/A#ifdef RE_ENABLE_I18N
2N/A if (node->type == COMPLEX_BRACKET && node->duplicated == 0)
2N/A free_charset (node->opr.mbcset);
2N/A else
2N/A#endif /* RE_ENABLE_I18N */
2N/A if (node->type == SIMPLE_BRACKET && node->duplicated == 0)
2N/A re_free (node->opr.sbcset);
2N/A}
2N/A
2N/A/* Worker function for tree walking. Free the allocated memory inside NODE
2N/A and its children. */
2N/A
2N/Astatic reg_errcode_t
2N/Afree_tree (void *extra, bin_tree_t *node)
2N/A{
2N/A free_token (&node->token);
2N/A return REG_NOERROR;
2N/A}
2N/A
2N/A
2N/A/* Duplicate the node SRC, and return new node. This is a preorder
2N/A visit similar to the one implemented by the generic visitor, but
2N/A we need more infrastructure to maintain two parallel trees --- so,
2N/A it's easier to duplicate. */
2N/A
2N/Astatic bin_tree_t *
2N/Aduplicate_tree (const bin_tree_t *root, re_dfa_t *dfa)
2N/A{
2N/A const bin_tree_t *node;
2N/A bin_tree_t *dup_root;
2N/A bin_tree_t **p_new = &dup_root, *dup_node = root->parent;
2N/A
2N/A for (node = root; ; )
2N/A {
2N/A /* Create a new tree and link it back to the current parent. */
2N/A *p_new = create_token_tree (dfa, NULL, NULL, &node->token);
2N/A if (*p_new == NULL)
2N/A return NULL;
2N/A (*p_new)->parent = dup_node;
2N/A (*p_new)->token.duplicated = 1;
2N/A dup_node = *p_new;
2N/A
2N/A /* Go to the left node, or up and to the right. */
2N/A if (node->left)
2N/A {
2N/A node = node->left;
2N/A p_new = &dup_node->left;
2N/A }
2N/A else
2N/A {
2N/A const bin_tree_t *prev = NULL;
2N/A while (node->right == prev || node->right == NULL)
2N/A {
2N/A prev = node;
2N/A node = node->parent;
2N/A dup_node = dup_node->parent;
2N/A if (!node)
2N/A return dup_root;
2N/A }
2N/A node = node->right;
2N/A p_new = &dup_node->right;
2N/A }
2N/A }
2N/A}