2002-02-27 03:06:03 +08:00
|
|
|
|
/* Extended regular expression matching and search library.
|
2003-01-17 01:27:50 +08:00
|
|
|
|
Copyright (C) 2002, 2003 Free Software Foundation, Inc.
|
2002-02-27 03:06:03 +08:00
|
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
|
|
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
License along with the GNU C Library; if not, write to the Free
|
|
|
|
|
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
|
02111-1307 USA. */
|
|
|
|
|
|
|
|
|
|
static reg_errcode_t re_compile_internal (regex_t *preg, const char * pattern,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
int length, reg_syntax_t syntax);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
static void re_compile_fastmap_iter (regex_t *bufp,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
const re_dfastate_t *init_state,
|
|
|
|
|
char *fastmap);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
static reg_errcode_t init_dfa (re_dfa_t *dfa, int pat_len);
|
2002-02-28 15:43:13 +08:00
|
|
|
|
static reg_errcode_t init_word_char (re_dfa_t *dfa);
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#ifdef RE_ENABLE_I18N
|
2002-02-27 03:06:03 +08:00
|
|
|
|
static void free_charset (re_charset_t *cset);
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#endif /* RE_ENABLE_I18N */
|
2002-02-27 03:06:03 +08:00
|
|
|
|
static void free_workarea_compile (regex_t *preg);
|
|
|
|
|
static reg_errcode_t create_initial_state (re_dfa_t *dfa);
|
|
|
|
|
static reg_errcode_t analyze (re_dfa_t *dfa);
|
|
|
|
|
static reg_errcode_t analyze_tree (re_dfa_t *dfa, bin_tree_t *node);
|
|
|
|
|
static void calc_first (re_dfa_t *dfa, bin_tree_t *node);
|
|
|
|
|
static void calc_next (re_dfa_t *dfa, bin_tree_t *node);
|
|
|
|
|
static void calc_epsdest (re_dfa_t *dfa, bin_tree_t *node);
|
2002-10-12 16:34:26 +08:00
|
|
|
|
static reg_errcode_t duplicate_node_closure (re_dfa_t *dfa, int top_org_node,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
int top_clone_node, int root_node,
|
|
|
|
|
unsigned int constraint);
|
2002-02-28 15:43:13 +08:00
|
|
|
|
static reg_errcode_t duplicate_node (int *new_idx, re_dfa_t *dfa, int org_idx,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
unsigned int constraint);
|
2002-12-17 18:58:04 +08:00
|
|
|
|
static int search_duplicated_node (re_dfa_t *dfa, int org_node,
|
|
|
|
|
unsigned int constraint);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
static reg_errcode_t calc_eclosure (re_dfa_t *dfa);
|
2002-02-28 15:43:13 +08:00
|
|
|
|
static reg_errcode_t calc_eclosure_iter (re_node_set *new_set, re_dfa_t *dfa,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
int node, int root);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
static void calc_inveclosure (re_dfa_t *dfa);
|
|
|
|
|
static int fetch_number (re_string_t *input, re_token_t *token,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
reg_syntax_t syntax);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
static re_token_t fetch_token (re_string_t *input, reg_syntax_t syntax);
|
|
|
|
|
static int peek_token (re_token_t *token, re_string_t *input,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
reg_syntax_t syntax);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
static int peek_token_bracket (re_token_t *token, re_string_t *input,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
reg_syntax_t syntax);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
static bin_tree_t *parse (re_string_t *regexp, regex_t *preg,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
reg_syntax_t syntax, reg_errcode_t *err);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
static bin_tree_t *parse_reg_exp (re_string_t *regexp, regex_t *preg,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
re_token_t *token, reg_syntax_t syntax,
|
|
|
|
|
int nest, reg_errcode_t *err);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
static bin_tree_t *parse_branch (re_string_t *regexp, regex_t *preg,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
re_token_t *token, reg_syntax_t syntax,
|
|
|
|
|
int nest, reg_errcode_t *err);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
static bin_tree_t *parse_expression (re_string_t *regexp, regex_t *preg,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
re_token_t *token, reg_syntax_t syntax,
|
|
|
|
|
int nest, reg_errcode_t *err);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
static bin_tree_t *parse_sub_exp (re_string_t *regexp, regex_t *preg,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
re_token_t *token, reg_syntax_t syntax,
|
|
|
|
|
int nest, reg_errcode_t *err);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
static bin_tree_t *parse_dup_op (bin_tree_t *dup_elem, re_string_t *regexp,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
re_dfa_t *dfa, re_token_t *token,
|
|
|
|
|
reg_syntax_t syntax, reg_errcode_t *err);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
static bin_tree_t *parse_bracket_exp (re_string_t *regexp, re_dfa_t *dfa,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
re_token_t *token, reg_syntax_t syntax,
|
|
|
|
|
reg_errcode_t *err);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
static reg_errcode_t parse_bracket_element (bracket_elem_t *elem,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
re_string_t *regexp,
|
|
|
|
|
re_token_t *token, int token_len,
|
|
|
|
|
re_dfa_t *dfa,
|
|
|
|
|
reg_syntax_t syntax);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
static reg_errcode_t parse_bracket_symbol (bracket_elem_t *elem,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
re_string_t *regexp,
|
|
|
|
|
re_token_t *token);
|
2002-04-27 04:52:02 +08:00
|
|
|
|
#ifndef _LIBC
|
2002-05-24 16:49:00 +08:00
|
|
|
|
# ifdef RE_ENABLE_I18N
|
|
|
|
|
static reg_errcode_t build_range_exp (re_bitset_ptr_t sbcset,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
re_charset_t *mbcset, int *range_alloc,
|
|
|
|
|
bracket_elem_t *start_elem,
|
|
|
|
|
bracket_elem_t *end_elem);
|
2002-05-24 16:49:00 +08:00
|
|
|
|
static reg_errcode_t build_collating_symbol (re_bitset_ptr_t sbcset,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
re_charset_t *mbcset,
|
|
|
|
|
int *coll_sym_alloc,
|
|
|
|
|
const unsigned char *name);
|
2002-05-24 16:49:00 +08:00
|
|
|
|
# else /* not RE_ENABLE_I18N */
|
|
|
|
|
static reg_errcode_t build_range_exp (re_bitset_ptr_t sbcset,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
bracket_elem_t *start_elem,
|
|
|
|
|
bracket_elem_t *end_elem);
|
2002-05-24 16:49:00 +08:00
|
|
|
|
static reg_errcode_t build_collating_symbol (re_bitset_ptr_t sbcset,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
const unsigned char *name);
|
2002-05-24 16:49:00 +08:00
|
|
|
|
# endif /* not RE_ENABLE_I18N */
|
2002-04-27 04:52:02 +08:00
|
|
|
|
#endif /* not _LIBC */
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#ifdef RE_ENABLE_I18N
|
|
|
|
|
static reg_errcode_t build_equiv_class (re_bitset_ptr_t sbcset,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
re_charset_t *mbcset,
|
|
|
|
|
int *equiv_class_alloc,
|
|
|
|
|
const unsigned char *name);
|
2003-06-13 02:29:52 +08:00
|
|
|
|
static reg_errcode_t build_charclass (RE_TRANSLATE_TYPE trans,
|
|
|
|
|
re_bitset_ptr_t sbcset,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
re_charset_t *mbcset,
|
|
|
|
|
int *char_class_alloc,
|
|
|
|
|
const unsigned char *class_name,
|
|
|
|
|
reg_syntax_t syntax);
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#else /* not RE_ENABLE_I18N */
|
|
|
|
|
static reg_errcode_t build_equiv_class (re_bitset_ptr_t sbcset,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
const unsigned char *name);
|
2003-06-13 02:29:52 +08:00
|
|
|
|
static reg_errcode_t build_charclass (RE_TRANSLATE_TYPE trans,
|
|
|
|
|
re_bitset_ptr_t sbcset,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
const unsigned char *class_name,
|
|
|
|
|
reg_syntax_t syntax);
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#endif /* not RE_ENABLE_I18N */
|
2003-06-13 02:29:52 +08:00
|
|
|
|
static bin_tree_t *build_word_op (re_dfa_t *dfa, RE_TRANSLATE_TYPE trans,
|
|
|
|
|
int not, reg_errcode_t *err);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
static void free_bin_tree (bin_tree_t *tree);
|
|
|
|
|
static bin_tree_t *create_tree (bin_tree_t *left, bin_tree_t *right,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
re_token_type_t type, int index);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
static bin_tree_t *duplicate_tree (const bin_tree_t *src, re_dfa_t *dfa);
|
|
|
|
|
|
|
|
|
|
/* This table gives an error message for each of the error codes listed
|
|
|
|
|
in regex.h. Obviously the order here has to be same as there.
|
|
|
|
|
POSIX doesn't require that we do anything for REG_NOERROR,
|
|
|
|
|
but why not be nice? */
|
|
|
|
|
|
2002-03-13 16:59:47 +08:00
|
|
|
|
const char __re_error_msgid[] attribute_hidden =
|
2002-02-27 03:06:03 +08:00
|
|
|
|
{
|
|
|
|
|
#define REG_NOERROR_IDX 0
|
|
|
|
|
gettext_noop ("Success") /* REG_NOERROR */
|
|
|
|
|
"\0"
|
|
|
|
|
#define REG_NOMATCH_IDX (REG_NOERROR_IDX + sizeof "Success")
|
|
|
|
|
gettext_noop ("No match") /* REG_NOMATCH */
|
|
|
|
|
"\0"
|
|
|
|
|
#define REG_BADPAT_IDX (REG_NOMATCH_IDX + sizeof "No match")
|
|
|
|
|
gettext_noop ("Invalid regular expression") /* REG_BADPAT */
|
|
|
|
|
"\0"
|
|
|
|
|
#define REG_ECOLLATE_IDX (REG_BADPAT_IDX + sizeof "Invalid regular expression")
|
|
|
|
|
gettext_noop ("Invalid collation character") /* REG_ECOLLATE */
|
|
|
|
|
"\0"
|
|
|
|
|
#define REG_ECTYPE_IDX (REG_ECOLLATE_IDX + sizeof "Invalid collation character")
|
|
|
|
|
gettext_noop ("Invalid character class name") /* REG_ECTYPE */
|
|
|
|
|
"\0"
|
|
|
|
|
#define REG_EESCAPE_IDX (REG_ECTYPE_IDX + sizeof "Invalid character class name")
|
|
|
|
|
gettext_noop ("Trailing backslash") /* REG_EESCAPE */
|
|
|
|
|
"\0"
|
|
|
|
|
#define REG_ESUBREG_IDX (REG_EESCAPE_IDX + sizeof "Trailing backslash")
|
|
|
|
|
gettext_noop ("Invalid back reference") /* REG_ESUBREG */
|
|
|
|
|
"\0"
|
|
|
|
|
#define REG_EBRACK_IDX (REG_ESUBREG_IDX + sizeof "Invalid back reference")
|
|
|
|
|
gettext_noop ("Unmatched [ or [^") /* REG_EBRACK */
|
|
|
|
|
"\0"
|
|
|
|
|
#define REG_EPAREN_IDX (REG_EBRACK_IDX + sizeof "Unmatched [ or [^")
|
|
|
|
|
gettext_noop ("Unmatched ( or \\(") /* REG_EPAREN */
|
|
|
|
|
"\0"
|
|
|
|
|
#define REG_EBRACE_IDX (REG_EPAREN_IDX + sizeof "Unmatched ( or \\(")
|
|
|
|
|
gettext_noop ("Unmatched \\{") /* REG_EBRACE */
|
|
|
|
|
"\0"
|
|
|
|
|
#define REG_BADBR_IDX (REG_EBRACE_IDX + sizeof "Unmatched \\{")
|
|
|
|
|
gettext_noop ("Invalid content of \\{\\}") /* REG_BADBR */
|
|
|
|
|
"\0"
|
|
|
|
|
#define REG_ERANGE_IDX (REG_BADBR_IDX + sizeof "Invalid content of \\{\\}")
|
|
|
|
|
gettext_noop ("Invalid range end") /* REG_ERANGE */
|
|
|
|
|
"\0"
|
|
|
|
|
#define REG_ESPACE_IDX (REG_ERANGE_IDX + sizeof "Invalid range end")
|
|
|
|
|
gettext_noop ("Memory exhausted") /* REG_ESPACE */
|
|
|
|
|
"\0"
|
|
|
|
|
#define REG_BADRPT_IDX (REG_ESPACE_IDX + sizeof "Memory exhausted")
|
|
|
|
|
gettext_noop ("Invalid preceding regular expression") /* REG_BADRPT */
|
|
|
|
|
"\0"
|
|
|
|
|
#define REG_EEND_IDX (REG_BADRPT_IDX + sizeof "Invalid preceding regular expression")
|
|
|
|
|
gettext_noop ("Premature end of regular expression") /* REG_EEND */
|
|
|
|
|
"\0"
|
|
|
|
|
#define REG_ESIZE_IDX (REG_EEND_IDX + sizeof "Premature end of regular expression")
|
|
|
|
|
gettext_noop ("Regular expression too big") /* REG_ESIZE */
|
|
|
|
|
"\0"
|
|
|
|
|
#define REG_ERPAREN_IDX (REG_ESIZE_IDX + sizeof "Regular expression too big")
|
|
|
|
|
gettext_noop ("Unmatched ) or \\)") /* REG_ERPAREN */
|
|
|
|
|
};
|
|
|
|
|
|
2002-03-13 16:59:47 +08:00
|
|
|
|
const size_t __re_error_msgid_idx[] attribute_hidden =
|
2002-02-27 03:06:03 +08:00
|
|
|
|
{
|
|
|
|
|
REG_NOERROR_IDX,
|
|
|
|
|
REG_NOMATCH_IDX,
|
|
|
|
|
REG_BADPAT_IDX,
|
|
|
|
|
REG_ECOLLATE_IDX,
|
|
|
|
|
REG_ECTYPE_IDX,
|
|
|
|
|
REG_EESCAPE_IDX,
|
|
|
|
|
REG_ESUBREG_IDX,
|
|
|
|
|
REG_EBRACK_IDX,
|
|
|
|
|
REG_EPAREN_IDX,
|
|
|
|
|
REG_EBRACE_IDX,
|
|
|
|
|
REG_BADBR_IDX,
|
|
|
|
|
REG_ERANGE_IDX,
|
|
|
|
|
REG_ESPACE_IDX,
|
|
|
|
|
REG_BADRPT_IDX,
|
|
|
|
|
REG_EEND_IDX,
|
|
|
|
|
REG_ESIZE_IDX,
|
|
|
|
|
REG_ERPAREN_IDX
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* Entry points for GNU code. */
|
|
|
|
|
|
|
|
|
|
/* re_compile_pattern is the GNU regular expression compiler: it
|
2002-07-27 16:20:17 +08:00
|
|
|
|
compiles PATTERN (of length LENGTH) and puts the result in BUFP.
|
2002-02-27 03:06:03 +08:00
|
|
|
|
Returns 0 if the pattern was valid, otherwise an error string.
|
|
|
|
|
|
|
|
|
|
Assumes the `allocated' (and perhaps `buffer') and `translate' fields
|
|
|
|
|
are set in BUFP on entry. */
|
|
|
|
|
|
|
|
|
|
const char *
|
|
|
|
|
re_compile_pattern (pattern, length, bufp)
|
|
|
|
|
const char *pattern;
|
|
|
|
|
size_t length;
|
|
|
|
|
struct re_pattern_buffer *bufp;
|
|
|
|
|
{
|
|
|
|
|
reg_errcode_t ret;
|
|
|
|
|
|
|
|
|
|
/* And GNU code determines whether or not to get register information
|
|
|
|
|
by passing null for the REGS argument to re_match, etc., not by
|
|
|
|
|
setting no_sub. */
|
|
|
|
|
bufp->no_sub = 0;
|
|
|
|
|
|
|
|
|
|
/* Match anchors at newline. */
|
|
|
|
|
bufp->newline_anchor = 1;
|
|
|
|
|
|
2002-09-05 11:33:58 +08:00
|
|
|
|
ret = re_compile_internal (bufp, pattern, length, re_syntax_options);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
|
return NULL;
|
2002-03-13 16:59:47 +08:00
|
|
|
|
return gettext (__re_error_msgid + __re_error_msgid_idx[(int) ret]);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
#ifdef _LIBC
|
|
|
|
|
weak_alias (__re_compile_pattern, re_compile_pattern)
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
|
|
|
|
|
also be assigned to arbitrarily: each pattern buffer stores its own
|
|
|
|
|
syntax, so it can be changed between regex compilations. */
|
|
|
|
|
/* This has no initializer because initialized variables in Emacs
|
|
|
|
|
become read-only after dumping. */
|
|
|
|
|
reg_syntax_t re_syntax_options;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Specify the precise syntax of regexps for compilation. This provides
|
|
|
|
|
for compatibility for various utilities which historically have
|
|
|
|
|
different, incompatible syntaxes.
|
|
|
|
|
|
|
|
|
|
The argument SYNTAX is a bit mask comprised of the various bits
|
|
|
|
|
defined in regex.h. We return the old syntax. */
|
|
|
|
|
|
|
|
|
|
reg_syntax_t
|
|
|
|
|
re_set_syntax (syntax)
|
|
|
|
|
reg_syntax_t syntax;
|
|
|
|
|
{
|
|
|
|
|
reg_syntax_t ret = re_syntax_options;
|
|
|
|
|
|
|
|
|
|
re_syntax_options = syntax;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
#ifdef _LIBC
|
|
|
|
|
weak_alias (__re_set_syntax, re_set_syntax)
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
re_compile_fastmap (bufp)
|
|
|
|
|
struct re_pattern_buffer *bufp;
|
|
|
|
|
{
|
|
|
|
|
re_dfa_t *dfa = (re_dfa_t *) bufp->buffer;
|
|
|
|
|
char *fastmap = bufp->fastmap;
|
|
|
|
|
|
|
|
|
|
memset (fastmap, '\0', sizeof (char) * SBC_MAX);
|
|
|
|
|
re_compile_fastmap_iter (bufp, dfa->init_state, fastmap);
|
|
|
|
|
if (dfa->init_state != dfa->init_state_word)
|
|
|
|
|
re_compile_fastmap_iter (bufp, dfa->init_state_word, fastmap);
|
|
|
|
|
if (dfa->init_state != dfa->init_state_nl)
|
|
|
|
|
re_compile_fastmap_iter (bufp, dfa->init_state_nl, fastmap);
|
|
|
|
|
if (dfa->init_state != dfa->init_state_begbuf)
|
|
|
|
|
re_compile_fastmap_iter (bufp, dfa->init_state_begbuf, fastmap);
|
|
|
|
|
bufp->fastmap_accurate = 1;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
#ifdef _LIBC
|
|
|
|
|
weak_alias (__re_compile_fastmap, re_compile_fastmap)
|
|
|
|
|
#endif
|
|
|
|
|
|
2002-11-07 03:57:50 +08:00
|
|
|
|
static inline void
|
2003-06-14 05:05:42 +08:00
|
|
|
|
__attribute ((always_inline))
|
2002-11-07 03:57:50 +08:00
|
|
|
|
re_set_fastmap (char *fastmap, int icase, int ch)
|
|
|
|
|
{
|
|
|
|
|
fastmap[ch] = 1;
|
|
|
|
|
if (icase)
|
|
|
|
|
fastmap[tolower (ch)] = 1;
|
|
|
|
|
}
|
|
|
|
|
|
2002-02-27 03:06:03 +08:00
|
|
|
|
/* Helper function for re_compile_fastmap.
|
|
|
|
|
Compile fastmap for the initial_state INIT_STATE. */
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
re_compile_fastmap_iter (bufp, init_state, fastmap)
|
|
|
|
|
regex_t *bufp;
|
|
|
|
|
const re_dfastate_t *init_state;
|
|
|
|
|
char *fastmap;
|
|
|
|
|
{
|
|
|
|
|
re_dfa_t *dfa = (re_dfa_t *) bufp->buffer;
|
|
|
|
|
int node_cnt;
|
2002-11-07 03:57:50 +08:00
|
|
|
|
int icase = (MB_CUR_MAX == 1 && (bufp->syntax & RE_ICASE));
|
2002-02-27 03:06:03 +08:00
|
|
|
|
for (node_cnt = 0; node_cnt < init_state->nodes.nelem; ++node_cnt)
|
|
|
|
|
{
|
|
|
|
|
int node = init_state->nodes.elems[node_cnt];
|
|
|
|
|
re_token_type_t type = dfa->nodes[node].type;
|
|
|
|
|
|
|
|
|
|
if (type == CHARACTER)
|
2002-11-07 03:57:50 +08:00
|
|
|
|
re_set_fastmap (fastmap, icase, dfa->nodes[node].opr.c);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
else if (type == SIMPLE_BRACKET)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
int i, j, ch;
|
|
|
|
|
for (i = 0, ch = 0; i < BITSET_UINTS; ++i)
|
|
|
|
|
for (j = 0; j < UINT_BITS; ++j, ++ch)
|
|
|
|
|
if (dfa->nodes[node].opr.sbcset[i] & (1 << j))
|
|
|
|
|
re_set_fastmap (fastmap, icase, ch);
|
|
|
|
|
}
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#ifdef RE_ENABLE_I18N
|
2002-02-27 03:06:03 +08:00
|
|
|
|
else if (type == COMPLEX_BRACKET)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
re_charset_t *cset = dfa->nodes[node].opr.mbcset;
|
|
|
|
|
if (cset->non_match || cset->ncoll_syms || cset->nequiv_classes
|
|
|
|
|
|| cset->nranges || cset->nchar_classes)
|
|
|
|
|
{
|
2002-05-24 16:49:00 +08:00
|
|
|
|
# ifdef _LIBC
|
2002-11-07 04:36:47 +08:00
|
|
|
|
if (_NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES) != 0)
|
|
|
|
|
{
|
|
|
|
|
/* In this case we want to catch the bytes which are
|
|
|
|
|
the first byte of any collation elements.
|
|
|
|
|
e.g. In da_DK, we want to catch 'a' since "aa"
|
|
|
|
|
is a valid collation element, and don't catch
|
|
|
|
|
'b' since 'b' is the only collation element
|
|
|
|
|
which starts from 'b'. */
|
|
|
|
|
int j, ch;
|
|
|
|
|
const int32_t *table = (const int32_t *)
|
|
|
|
|
_NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
|
|
|
|
|
for (i = 0, ch = 0; i < BITSET_UINTS; ++i)
|
|
|
|
|
for (j = 0; j < UINT_BITS; ++j, ++ch)
|
|
|
|
|
if (table[ch] < 0)
|
|
|
|
|
re_set_fastmap (fastmap, icase, ch);
|
|
|
|
|
}
|
2002-05-24 16:49:00 +08:00
|
|
|
|
# else
|
2002-11-07 04:36:47 +08:00
|
|
|
|
if (MB_CUR_MAX > 1)
|
|
|
|
|
for (i = 0; i < SBC_MAX; ++i)
|
|
|
|
|
if (__btowc (i) == WEOF)
|
|
|
|
|
re_set_fastmap (fastmap, icase, i);
|
2002-05-24 16:49:00 +08:00
|
|
|
|
# endif /* not _LIBC */
|
2002-11-07 04:36:47 +08:00
|
|
|
|
}
|
|
|
|
|
for (i = 0; i < cset->nmbchars; ++i)
|
|
|
|
|
{
|
|
|
|
|
char buf[256];
|
|
|
|
|
mbstate_t state;
|
|
|
|
|
memset (&state, '\0', sizeof (state));
|
|
|
|
|
__wcrtomb (buf, cset->mbchars[i], &state);
|
|
|
|
|
re_set_fastmap (fastmap, icase, *(unsigned char *) buf);
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#endif /* RE_ENABLE_I18N */
|
2002-11-07 03:57:50 +08:00
|
|
|
|
else if (type == END_OF_RE || type == OP_PERIOD)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
memset (fastmap, '\1', sizeof (char) * SBC_MAX);
|
|
|
|
|
if (type == END_OF_RE)
|
|
|
|
|
bufp->can_be_null = 1;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Entry point for POSIX code. */
|
|
|
|
|
/* regcomp takes a regular expression as a string and compiles it.
|
|
|
|
|
|
|
|
|
|
PREG is a regex_t *. We do not expect any fields to be initialized,
|
|
|
|
|
since POSIX says we shouldn't. Thus, we set
|
|
|
|
|
|
|
|
|
|
`buffer' to the compiled pattern;
|
|
|
|
|
`used' to the length of the compiled pattern;
|
|
|
|
|
`syntax' to RE_SYNTAX_POSIX_EXTENDED if the
|
|
|
|
|
REG_EXTENDED bit in CFLAGS is set; otherwise, to
|
|
|
|
|
RE_SYNTAX_POSIX_BASIC;
|
|
|
|
|
`newline_anchor' to REG_NEWLINE being set in CFLAGS;
|
|
|
|
|
`fastmap' to an allocated space for the fastmap;
|
|
|
|
|
`fastmap_accurate' to zero;
|
|
|
|
|
`re_nsub' to the number of subexpressions in PATTERN.
|
|
|
|
|
|
|
|
|
|
PATTERN is the address of the pattern string.
|
|
|
|
|
|
|
|
|
|
CFLAGS is a series of bits which affect compilation.
|
|
|
|
|
|
|
|
|
|
If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
|
|
|
|
|
use POSIX basic syntax.
|
|
|
|
|
|
|
|
|
|
If REG_NEWLINE is set, then . and [^...] don't match newline.
|
|
|
|
|
Also, regexec will try a match beginning after every newline.
|
|
|
|
|
|
|
|
|
|
If REG_ICASE is set, then we considers upper- and lowercase
|
|
|
|
|
versions of letters to be equivalent when matching.
|
|
|
|
|
|
|
|
|
|
If REG_NOSUB is set, then when PREG is passed to regexec, that
|
|
|
|
|
routine will report only success or failure, and nothing about the
|
|
|
|
|
registers.
|
|
|
|
|
|
|
|
|
|
It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
|
|
|
|
|
the return codes and their meanings.) */
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
regcomp (preg, pattern, cflags)
|
2002-09-05 11:33:58 +08:00
|
|
|
|
regex_t *__restrict preg;
|
|
|
|
|
const char *__restrict pattern;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
int cflags;
|
|
|
|
|
{
|
|
|
|
|
reg_errcode_t ret;
|
|
|
|
|
reg_syntax_t syntax = ((cflags & REG_EXTENDED) ? RE_SYNTAX_POSIX_EXTENDED
|
2002-11-07 04:36:47 +08:00
|
|
|
|
: RE_SYNTAX_POSIX_BASIC);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
|
|
|
|
|
preg->buffer = NULL;
|
|
|
|
|
preg->allocated = 0;
|
|
|
|
|
preg->used = 0;
|
|
|
|
|
|
|
|
|
|
/* Try to allocate space for the fastmap. */
|
|
|
|
|
preg->fastmap = re_malloc (char, SBC_MAX);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (preg->fastmap == NULL, 0))
|
2002-02-27 03:06:03 +08:00
|
|
|
|
return REG_ESPACE;
|
|
|
|
|
|
|
|
|
|
syntax |= (cflags & REG_ICASE) ? RE_ICASE : 0;
|
|
|
|
|
|
|
|
|
|
/* If REG_NEWLINE is set, newlines are treated differently. */
|
|
|
|
|
if (cflags & REG_NEWLINE)
|
|
|
|
|
{ /* REG_NEWLINE implies neither . nor [^...] match newline. */
|
|
|
|
|
syntax &= ~RE_DOT_NEWLINE;
|
|
|
|
|
syntax |= RE_HAT_LISTS_NOT_NEWLINE;
|
|
|
|
|
/* It also changes the matching behavior. */
|
|
|
|
|
preg->newline_anchor = 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
preg->newline_anchor = 0;
|
|
|
|
|
preg->no_sub = !!(cflags & REG_NOSUB);
|
|
|
|
|
preg->translate = NULL;
|
|
|
|
|
|
|
|
|
|
ret = re_compile_internal (preg, pattern, strlen (pattern), syntax);
|
|
|
|
|
|
|
|
|
|
/* POSIX doesn't distinguish between an unmatched open-group and an
|
|
|
|
|
unmatched close-group: both are REG_EPAREN. */
|
|
|
|
|
if (ret == REG_ERPAREN)
|
|
|
|
|
ret = REG_EPAREN;
|
|
|
|
|
|
2002-02-28 15:43:13 +08:00
|
|
|
|
/* We have already checked preg->fastmap != NULL. */
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (ret == REG_NOERROR, 1))
|
2002-11-06 16:34:35 +08:00
|
|
|
|
/* Compute the fastmap now, since regexec cannot modify the pattern
|
|
|
|
|
buffer. This function nevers fails in this implementation. */
|
2002-12-14 07:45:32 +08:00
|
|
|
|
(void) re_compile_fastmap (preg);
|
2002-11-06 16:34:35 +08:00
|
|
|
|
else
|
2002-02-27 03:06:03 +08:00
|
|
|
|
{
|
2002-11-06 16:34:35 +08:00
|
|
|
|
/* Some error occurred while compiling the expression. */
|
|
|
|
|
re_free (preg->fastmap);
|
|
|
|
|
preg->fastmap = NULL;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (int) ret;
|
|
|
|
|
}
|
|
|
|
|
#ifdef _LIBC
|
|
|
|
|
weak_alias (__regcomp, regcomp)
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* Returns a message corresponding to an error code, ERRCODE, returned
|
|
|
|
|
from either regcomp or regexec. We don't use PREG here. */
|
|
|
|
|
|
|
|
|
|
size_t
|
|
|
|
|
regerror (errcode, preg, errbuf, errbuf_size)
|
|
|
|
|
int errcode;
|
|
|
|
|
const regex_t *preg;
|
|
|
|
|
char *errbuf;
|
|
|
|
|
size_t errbuf_size;
|
|
|
|
|
{
|
|
|
|
|
const char *msg;
|
|
|
|
|
size_t msg_size;
|
|
|
|
|
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (errcode < 0
|
2002-11-07 04:36:47 +08:00
|
|
|
|
|| errcode >= (int) (sizeof (__re_error_msgid_idx)
|
|
|
|
|
/ sizeof (__re_error_msgid_idx[0])), 0))
|
2002-02-27 03:06:03 +08:00
|
|
|
|
/* Only error codes returned by the rest of the code should be passed
|
|
|
|
|
to this routine. If we are given anything else, or if other regex
|
|
|
|
|
code generates an invalid error code, then the program has a bug.
|
|
|
|
|
Dump core so we can fix it. */
|
|
|
|
|
abort ();
|
|
|
|
|
|
2002-03-13 16:59:47 +08:00
|
|
|
|
msg = gettext (__re_error_msgid + __re_error_msgid_idx[errcode]);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
|
|
|
|
|
msg_size = strlen (msg) + 1; /* Includes the null. */
|
|
|
|
|
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (errbuf_size != 0, 1))
|
2002-02-27 03:06:03 +08:00
|
|
|
|
{
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (msg_size > errbuf_size, 0))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
2002-02-27 03:06:03 +08:00
|
|
|
|
#if defined HAVE_MEMPCPY || defined _LIBC
|
|
|
|
|
*((char *) __mempcpy (errbuf, msg, errbuf_size - 1)) = '\0';
|
|
|
|
|
#else
|
2002-11-07 04:36:47 +08:00
|
|
|
|
memcpy (errbuf, msg, errbuf_size - 1);
|
|
|
|
|
errbuf[errbuf_size - 1] = 0;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
#endif
|
2002-11-07 04:36:47 +08:00
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
else
|
2002-11-07 04:36:47 +08:00
|
|
|
|
memcpy (errbuf, msg, msg_size);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return msg_size;
|
|
|
|
|
}
|
|
|
|
|
#ifdef _LIBC
|
|
|
|
|
weak_alias (__regerror, regerror)
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
2002-11-06 16:34:35 +08:00
|
|
|
|
static void
|
|
|
|
|
free_dfa_content (re_dfa_t *dfa)
|
2002-02-27 03:06:03 +08:00
|
|
|
|
{
|
|
|
|
|
int i, j;
|
|
|
|
|
|
2002-11-06 16:34:35 +08:00
|
|
|
|
re_free (dfa->subexps);
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < dfa->nodes_len; ++i)
|
|
|
|
|
{
|
|
|
|
|
re_token_t *node = dfa->nodes + i;
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#ifdef RE_ENABLE_I18N
|
2002-11-06 16:34:35 +08:00
|
|
|
|
if (node->type == COMPLEX_BRACKET && node->duplicated == 0)
|
|
|
|
|
free_charset (node->opr.mbcset);
|
|
|
|
|
else
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#endif /* RE_ENABLE_I18N */
|
2002-11-06 16:34:35 +08:00
|
|
|
|
if (node->type == SIMPLE_BRACKET && node->duplicated == 0)
|
|
|
|
|
re_free (node->opr.sbcset);
|
|
|
|
|
}
|
|
|
|
|
re_free (dfa->nexts);
|
|
|
|
|
for (i = 0; i < dfa->nodes_len; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (dfa->eclosures != NULL)
|
|
|
|
|
re_node_set_free (dfa->eclosures + i);
|
|
|
|
|
if (dfa->inveclosures != NULL)
|
|
|
|
|
re_node_set_free (dfa->inveclosures + i);
|
|
|
|
|
if (dfa->edests != NULL)
|
|
|
|
|
re_node_set_free (dfa->edests + i);
|
|
|
|
|
}
|
|
|
|
|
re_free (dfa->edests);
|
|
|
|
|
re_free (dfa->eclosures);
|
|
|
|
|
re_free (dfa->inveclosures);
|
|
|
|
|
re_free (dfa->nodes);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
|
2002-11-06 16:34:35 +08:00
|
|
|
|
for (i = 0; i <= dfa->state_hash_mask; ++i)
|
|
|
|
|
{
|
|
|
|
|
struct re_state_table_entry *entry = dfa->state_table + i;
|
|
|
|
|
for (j = 0; j < entry->num; ++j)
|
|
|
|
|
{
|
|
|
|
|
re_dfastate_t *state = entry->array[j];
|
2002-11-07 03:23:35 +08:00
|
|
|
|
free_state (state);
|
2002-11-06 16:34:35 +08:00
|
|
|
|
}
|
|
|
|
|
re_free (entry->array);
|
|
|
|
|
}
|
|
|
|
|
re_free (dfa->state_table);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
|
2002-11-06 16:34:35 +08:00
|
|
|
|
if (dfa->word_char != NULL)
|
|
|
|
|
re_free (dfa->word_char);
|
2002-09-28 13:28:44 +08:00
|
|
|
|
#ifdef DEBUG
|
2002-11-06 16:34:35 +08:00
|
|
|
|
re_free (dfa->re_str);
|
2002-09-28 13:28:44 +08:00
|
|
|
|
#endif
|
2002-11-06 16:34:35 +08:00
|
|
|
|
|
|
|
|
|
re_free (dfa);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Free dynamically allocated space used by PREG. */
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
regfree (preg)
|
|
|
|
|
regex_t *preg;
|
|
|
|
|
{
|
|
|
|
|
re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
|
|
|
|
|
if (BE (dfa != NULL, 1))
|
|
|
|
|
free_dfa_content (dfa);
|
|
|
|
|
|
2002-02-27 03:06:03 +08:00
|
|
|
|
re_free (preg->fastmap);
|
|
|
|
|
}
|
|
|
|
|
#ifdef _LIBC
|
|
|
|
|
weak_alias (__regfree, regfree)
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* Entry points compatible with 4.2 BSD regex library. We don't define
|
|
|
|
|
them unless specifically requested. */
|
|
|
|
|
|
|
|
|
|
#if defined _REGEX_RE_COMP || defined _LIBC
|
|
|
|
|
|
|
|
|
|
/* BSD has one and only one pattern buffer. */
|
|
|
|
|
static struct re_pattern_buffer re_comp_buf;
|
|
|
|
|
|
|
|
|
|
char *
|
|
|
|
|
# ifdef _LIBC
|
|
|
|
|
/* Make these definitions weak in libc, so POSIX programs can redefine
|
|
|
|
|
these names if they don't use our functions, and still use
|
|
|
|
|
regcomp/regexec above without link errors. */
|
|
|
|
|
weak_function
|
|
|
|
|
# endif
|
|
|
|
|
re_comp (s)
|
|
|
|
|
const char *s;
|
|
|
|
|
{
|
|
|
|
|
reg_errcode_t ret;
|
2002-10-24 08:22:27 +08:00
|
|
|
|
char *fastmap;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
|
|
|
|
|
if (!s)
|
|
|
|
|
{
|
|
|
|
|
if (!re_comp_buf.buffer)
|
|
|
|
|
return gettext ("No previous regular expression");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2002-10-24 08:22:27 +08:00
|
|
|
|
if (re_comp_buf.buffer)
|
|
|
|
|
{
|
|
|
|
|
fastmap = re_comp_buf.fastmap;
|
|
|
|
|
re_comp_buf.fastmap = NULL;
|
|
|
|
|
__regfree (&re_comp_buf);
|
2002-11-07 03:57:50 +08:00
|
|
|
|
memset (&re_comp_buf, '\0', sizeof (re_comp_buf));
|
2002-10-24 08:22:27 +08:00
|
|
|
|
re_comp_buf.fastmap = fastmap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (re_comp_buf.fastmap == NULL)
|
2002-02-27 03:06:03 +08:00
|
|
|
|
{
|
|
|
|
|
re_comp_buf.fastmap = (char *) malloc (SBC_MAX);
|
|
|
|
|
if (re_comp_buf.fastmap == NULL)
|
2002-03-13 16:59:47 +08:00
|
|
|
|
return (char *) gettext (__re_error_msgid
|
|
|
|
|
+ __re_error_msgid_idx[(int) REG_ESPACE]);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Since `re_exec' always passes NULL for the `regs' argument, we
|
|
|
|
|
don't need to initialize the pattern buffer fields which affect it. */
|
|
|
|
|
|
|
|
|
|
/* Match anchors at newlines. */
|
|
|
|
|
re_comp_buf.newline_anchor = 1;
|
|
|
|
|
|
|
|
|
|
ret = re_compile_internal (&re_comp_buf, s, strlen (s), re_syntax_options);
|
|
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
/* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
|
2002-03-13 16:59:47 +08:00
|
|
|
|
return (char *) gettext (__re_error_msgid + __re_error_msgid_idx[(int) ret]);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
2002-10-24 08:22:27 +08:00
|
|
|
|
|
|
|
|
|
#ifdef _LIBC
|
2002-11-02 04:44:15 +08:00
|
|
|
|
libc_freeres_fn (free_mem)
|
2002-10-24 08:22:27 +08:00
|
|
|
|
{
|
|
|
|
|
__regfree (&re_comp_buf);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2002-02-27 03:06:03 +08:00
|
|
|
|
#endif /* _REGEX_RE_COMP */
|
|
|
|
|
|
|
|
|
|
/* Internal entry point.
|
|
|
|
|
Compile the regular expression PATTERN, whose length is LENGTH.
|
|
|
|
|
SYNTAX indicate regular expression's syntax. */
|
|
|
|
|
|
|
|
|
|
static reg_errcode_t
|
|
|
|
|
re_compile_internal (preg, pattern, length, syntax)
|
|
|
|
|
regex_t *preg;
|
|
|
|
|
const char * pattern;
|
|
|
|
|
int length;
|
|
|
|
|
reg_syntax_t syntax;
|
|
|
|
|
{
|
|
|
|
|
reg_errcode_t err = REG_NOERROR;
|
|
|
|
|
re_dfa_t *dfa;
|
|
|
|
|
re_string_t regexp;
|
|
|
|
|
|
|
|
|
|
/* Initialize the pattern buffer. */
|
|
|
|
|
preg->fastmap_accurate = 0;
|
|
|
|
|
preg->syntax = syntax;
|
|
|
|
|
preg->not_bol = preg->not_eol = 0;
|
|
|
|
|
preg->used = 0;
|
|
|
|
|
preg->re_nsub = 0;
|
2002-11-07 03:57:50 +08:00
|
|
|
|
preg->can_be_null = 0;
|
|
|
|
|
preg->regs_allocated = REGS_UNALLOCATED;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
|
|
|
|
|
/* Initialize the dfa. */
|
|
|
|
|
dfa = (re_dfa_t *) preg->buffer;
|
|
|
|
|
if (preg->allocated < sizeof (re_dfa_t))
|
|
|
|
|
{
|
|
|
|
|
/* If zero allocated, but buffer is non-null, try to realloc
|
|
|
|
|
enough space. This loses if buffer's address is bogus, but
|
|
|
|
|
that is the user's responsibility. If ->buffer is NULL this
|
|
|
|
|
is a simple allocation. */
|
|
|
|
|
dfa = re_realloc (preg->buffer, re_dfa_t, 1);
|
|
|
|
|
if (dfa == NULL)
|
|
|
|
|
return REG_ESPACE;
|
|
|
|
|
preg->allocated = sizeof (re_dfa_t);
|
|
|
|
|
}
|
|
|
|
|
preg->buffer = (unsigned char *) dfa;
|
|
|
|
|
preg->used = sizeof (re_dfa_t);
|
|
|
|
|
|
|
|
|
|
err = init_dfa (dfa, length);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (err != REG_NOERROR, 0))
|
2002-02-27 03:06:03 +08:00
|
|
|
|
{
|
|
|
|
|
re_free (dfa);
|
|
|
|
|
preg->buffer = NULL;
|
2003-01-17 01:27:50 +08:00
|
|
|
|
preg->allocated = 0;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
return err;
|
|
|
|
|
}
|
2002-09-28 13:28:44 +08:00
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
dfa->re_str = re_malloc (char, length + 1);
|
|
|
|
|
strncpy (dfa->re_str, pattern, length + 1);
|
|
|
|
|
#endif
|
2002-02-27 03:06:03 +08:00
|
|
|
|
|
2002-04-25 05:54:53 +08:00
|
|
|
|
err = re_string_construct (®exp, pattern, length, preg->translate,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
syntax & RE_ICASE);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (err != REG_NOERROR, 0))
|
2002-02-27 03:06:03 +08:00
|
|
|
|
{
|
|
|
|
|
re_free (dfa);
|
|
|
|
|
preg->buffer = NULL;
|
2003-01-17 01:27:50 +08:00
|
|
|
|
preg->allocated = 0;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Parse the regular expression, and build a structure tree. */
|
|
|
|
|
preg->re_nsub = 0;
|
|
|
|
|
dfa->str_tree = parse (®exp, preg, syntax, &err);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (dfa->str_tree == NULL, 0))
|
2002-02-27 03:06:03 +08:00
|
|
|
|
goto re_compile_internal_free_return;
|
|
|
|
|
|
|
|
|
|
/* Analyze the tree and collect information which is necessary to
|
|
|
|
|
create the dfa. */
|
|
|
|
|
err = analyze (dfa);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (err != REG_NOERROR, 0))
|
2002-02-27 03:06:03 +08:00
|
|
|
|
goto re_compile_internal_free_return;
|
|
|
|
|
|
|
|
|
|
/* Then create the initial state of the dfa. */
|
|
|
|
|
err = create_initial_state (dfa);
|
|
|
|
|
|
|
|
|
|
/* Release work areas. */
|
|
|
|
|
free_workarea_compile (preg);
|
|
|
|
|
re_string_destruct (®exp);
|
|
|
|
|
|
2002-11-06 16:34:35 +08:00
|
|
|
|
if (BE (err != REG_NOERROR, 0))
|
|
|
|
|
{
|
|
|
|
|
re_compile_internal_free_return:
|
|
|
|
|
free_dfa_content (dfa);
|
|
|
|
|
preg->buffer = NULL;
|
2003-01-17 01:27:50 +08:00
|
|
|
|
preg->allocated = 0;
|
2002-11-06 16:34:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
2002-02-27 03:06:03 +08:00
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Initialize DFA. We use the length of the regular expression PAT_LEN
|
|
|
|
|
as the initial length of some arrays. */
|
|
|
|
|
|
|
|
|
|
static reg_errcode_t
|
|
|
|
|
init_dfa (dfa, pat_len)
|
|
|
|
|
re_dfa_t *dfa;
|
|
|
|
|
int pat_len;
|
|
|
|
|
{
|
|
|
|
|
int table_size;
|
2002-07-11 07:09:16 +08:00
|
|
|
|
|
|
|
|
|
memset (dfa, '\0', sizeof (re_dfa_t));
|
|
|
|
|
|
2002-02-27 03:06:03 +08:00
|
|
|
|
dfa->nodes_alloc = pat_len + 1;
|
|
|
|
|
dfa->nodes = re_malloc (re_token_t, dfa->nodes_alloc);
|
|
|
|
|
|
|
|
|
|
dfa->states_alloc = pat_len + 1;
|
|
|
|
|
|
|
|
|
|
/* table_size = 2 ^ ceil(log pat_len) */
|
|
|
|
|
for (table_size = 1; table_size > 0; table_size <<= 1)
|
|
|
|
|
if (table_size > pat_len)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
dfa->state_table = calloc (sizeof (struct re_state_table_entry), table_size);
|
|
|
|
|
dfa->state_hash_mask = table_size - 1;
|
|
|
|
|
|
|
|
|
|
dfa->subexps_alloc = 1;
|
|
|
|
|
dfa->subexps = re_malloc (re_subexp_t, dfa->subexps_alloc);
|
|
|
|
|
dfa->word_char = NULL;
|
|
|
|
|
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (dfa->nodes == NULL || dfa->state_table == NULL
|
2002-11-07 04:36:47 +08:00
|
|
|
|
|| dfa->subexps == NULL, 0))
|
2002-02-27 03:06:03 +08:00
|
|
|
|
{
|
|
|
|
|
/* We don't bother to free anything which was allocated. Very
|
|
|
|
|
soon the process will go down anyway. */
|
|
|
|
|
dfa->subexps = NULL;
|
|
|
|
|
dfa->state_table = NULL;
|
|
|
|
|
dfa->nodes = NULL;
|
|
|
|
|
return REG_ESPACE;
|
|
|
|
|
}
|
|
|
|
|
return REG_NOERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Initialize WORD_CHAR table, which indicate which character is
|
|
|
|
|
"word". In this case "word" means that it is the word construction
|
|
|
|
|
character used by some operators like "\<", "\>", etc. */
|
|
|
|
|
|
2002-02-28 15:43:13 +08:00
|
|
|
|
static reg_errcode_t
|
2002-02-27 03:06:03 +08:00
|
|
|
|
init_word_char (dfa)
|
|
|
|
|
re_dfa_t *dfa;
|
|
|
|
|
{
|
|
|
|
|
int i, j, ch;
|
|
|
|
|
dfa->word_char = (re_bitset_ptr_t) calloc (sizeof (bitset), 1);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (dfa->word_char == NULL, 0))
|
2002-02-28 15:43:13 +08:00
|
|
|
|
return REG_ESPACE;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
for (i = 0, ch = 0; i < BITSET_UINTS; ++i)
|
|
|
|
|
for (j = 0; j < UINT_BITS; ++j, ++ch)
|
|
|
|
|
if (isalnum (ch) || ch == '_')
|
2002-11-07 04:36:47 +08:00
|
|
|
|
dfa->word_char[i] |= 1 << j;
|
2002-02-28 15:43:13 +08:00
|
|
|
|
return REG_NOERROR;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Free the work area which are only used while compiling. */
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
free_workarea_compile (preg)
|
|
|
|
|
regex_t *preg;
|
|
|
|
|
{
|
|
|
|
|
re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
|
|
|
|
|
free_bin_tree (dfa->str_tree);
|
|
|
|
|
dfa->str_tree = NULL;
|
2002-12-17 18:58:04 +08:00
|
|
|
|
re_free (dfa->org_indices);
|
|
|
|
|
dfa->org_indices = NULL;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Create initial states for all contexts. */
|
|
|
|
|
|
|
|
|
|
static reg_errcode_t
|
|
|
|
|
create_initial_state (dfa)
|
|
|
|
|
re_dfa_t *dfa;
|
|
|
|
|
{
|
|
|
|
|
int first, i;
|
|
|
|
|
reg_errcode_t err;
|
|
|
|
|
re_node_set init_nodes;
|
|
|
|
|
|
|
|
|
|
/* Initial states have the epsilon closure of the node which is
|
|
|
|
|
the first node of the regular expression. */
|
|
|
|
|
first = dfa->str_tree->first;
|
|
|
|
|
dfa->init_node = first;
|
|
|
|
|
err = re_node_set_init_copy (&init_nodes, dfa->eclosures + first);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (err != REG_NOERROR, 0))
|
2002-02-27 03:06:03 +08:00
|
|
|
|
return err;
|
|
|
|
|
|
|
|
|
|
/* The back-references which are in initial states can epsilon transit,
|
|
|
|
|
since in this case all of the subexpressions can be null.
|
|
|
|
|
Then we add epsilon closures of the nodes which are the next nodes of
|
|
|
|
|
the back-references. */
|
|
|
|
|
if (dfa->nbackref > 0)
|
|
|
|
|
for (i = 0; i < init_nodes.nelem; ++i)
|
|
|
|
|
{
|
2002-11-07 04:36:47 +08:00
|
|
|
|
int node_idx = init_nodes.elems[i];
|
|
|
|
|
re_token_type_t type = dfa->nodes[node_idx].type;
|
|
|
|
|
|
|
|
|
|
int clexp_idx;
|
|
|
|
|
if (type != OP_BACK_REF)
|
|
|
|
|
continue;
|
|
|
|
|
for (clexp_idx = 0; clexp_idx < init_nodes.nelem; ++clexp_idx)
|
|
|
|
|
{
|
|
|
|
|
re_token_t *clexp_node;
|
|
|
|
|
clexp_node = dfa->nodes + init_nodes.elems[clexp_idx];
|
|
|
|
|
if (clexp_node->type == OP_CLOSE_SUBEXP
|
|
|
|
|
&& clexp_node->opr.idx + 1 == dfa->nodes[node_idx].opr.idx)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (clexp_idx == init_nodes.nelem)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (type == OP_BACK_REF)
|
|
|
|
|
{
|
|
|
|
|
int dest_idx = dfa->edests[node_idx].elems[0];
|
|
|
|
|
if (!re_node_set_contains (&init_nodes, dest_idx))
|
|
|
|
|
{
|
|
|
|
|
re_node_set_merge (&init_nodes, dfa->eclosures + dest_idx);
|
|
|
|
|
i = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* It must be the first time to invoke acquire_state. */
|
2002-02-28 15:43:13 +08:00
|
|
|
|
dfa->init_state = re_acquire_state_context (&err, dfa, &init_nodes, 0);
|
|
|
|
|
/* We don't check ERR here, since the initial state must not be NULL. */
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (dfa->init_state == NULL, 0))
|
2002-02-28 15:43:13 +08:00
|
|
|
|
return err;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
if (dfa->init_state->has_constraint)
|
|
|
|
|
{
|
2002-02-28 15:43:13 +08:00
|
|
|
|
dfa->init_state_word = re_acquire_state_context (&err, dfa, &init_nodes,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
CONTEXT_WORD);
|
2002-02-28 15:43:13 +08:00
|
|
|
|
dfa->init_state_nl = re_acquire_state_context (&err, dfa, &init_nodes,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
CONTEXT_NEWLINE);
|
2002-02-28 15:43:13 +08:00
|
|
|
|
dfa->init_state_begbuf = re_acquire_state_context (&err, dfa,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
&init_nodes,
|
|
|
|
|
CONTEXT_NEWLINE
|
|
|
|
|
| CONTEXT_BEGBUF);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (dfa->init_state_word == NULL || dfa->init_state_nl == NULL
|
2002-11-07 04:36:47 +08:00
|
|
|
|
|| dfa->init_state_begbuf == NULL, 0))
|
|
|
|
|
return err;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
dfa->init_state_word = dfa->init_state_nl
|
|
|
|
|
= dfa->init_state_begbuf = dfa->init_state;
|
|
|
|
|
|
|
|
|
|
re_node_set_free (&init_nodes);
|
|
|
|
|
return REG_NOERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Analyze the structure tree, and calculate "first", "next", "edest",
|
|
|
|
|
"eclosure", and "inveclosure". */
|
|
|
|
|
|
|
|
|
|
static reg_errcode_t
|
|
|
|
|
analyze (dfa)
|
|
|
|
|
re_dfa_t *dfa;
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
reg_errcode_t ret;
|
|
|
|
|
|
|
|
|
|
/* Allocate arrays. */
|
|
|
|
|
dfa->nexts = re_malloc (int, dfa->nodes_alloc);
|
2002-12-17 18:58:04 +08:00
|
|
|
|
dfa->org_indices = re_malloc (int, dfa->nodes_alloc);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
dfa->edests = re_malloc (re_node_set, dfa->nodes_alloc);
|
|
|
|
|
dfa->eclosures = re_malloc (re_node_set, dfa->nodes_alloc);
|
|
|
|
|
dfa->inveclosures = re_malloc (re_node_set, dfa->nodes_alloc);
|
2002-12-17 18:58:04 +08:00
|
|
|
|
if (BE (dfa->nexts == NULL || dfa->org_indices == NULL || dfa->edests == NULL
|
2002-11-07 04:36:47 +08:00
|
|
|
|
|| dfa->eclosures == NULL || dfa->inveclosures == NULL, 0))
|
2002-02-27 03:06:03 +08:00
|
|
|
|
return REG_ESPACE;
|
|
|
|
|
/* Initialize them. */
|
|
|
|
|
for (i = 0; i < dfa->nodes_len; ++i)
|
|
|
|
|
{
|
|
|
|
|
dfa->nexts[i] = -1;
|
|
|
|
|
re_node_set_init_empty (dfa->edests + i);
|
|
|
|
|
re_node_set_init_empty (dfa->eclosures + i);
|
|
|
|
|
re_node_set_init_empty (dfa->inveclosures + i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = analyze_tree (dfa, dfa->str_tree);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (ret == REG_NOERROR, 1))
|
2002-02-27 03:06:03 +08:00
|
|
|
|
{
|
|
|
|
|
ret = calc_eclosure (dfa);
|
|
|
|
|
if (ret == REG_NOERROR)
|
|
|
|
|
calc_inveclosure (dfa);
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Helper functions for analyze.
|
|
|
|
|
This function calculate "first", "next", and "edest" for the subtree
|
|
|
|
|
whose root is NODE. */
|
|
|
|
|
|
|
|
|
|
static reg_errcode_t
|
|
|
|
|
analyze_tree (dfa, node)
|
|
|
|
|
re_dfa_t *dfa;
|
|
|
|
|
bin_tree_t *node;
|
|
|
|
|
{
|
|
|
|
|
reg_errcode_t ret;
|
|
|
|
|
if (node->first == -1)
|
|
|
|
|
calc_first (dfa, node);
|
|
|
|
|
if (node->next == -1)
|
|
|
|
|
calc_next (dfa, node);
|
|
|
|
|
if (node->eclosure.nelem == 0)
|
|
|
|
|
calc_epsdest (dfa, node);
|
|
|
|
|
/* Calculate "first" etc. for the left child. */
|
|
|
|
|
if (node->left != NULL)
|
|
|
|
|
{
|
|
|
|
|
ret = analyze_tree (dfa, node->left);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (ret != REG_NOERROR, 0))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
return ret;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
/* Calculate "first" etc. for the right child. */
|
|
|
|
|
if (node->right != NULL)
|
|
|
|
|
{
|
|
|
|
|
ret = analyze_tree (dfa, node->right);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (ret != REG_NOERROR, 0))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
return ret;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
return REG_NOERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Calculate "first" for the node NODE. */
|
|
|
|
|
static void
|
|
|
|
|
calc_first (dfa, node)
|
|
|
|
|
re_dfa_t *dfa;
|
|
|
|
|
bin_tree_t *node;
|
|
|
|
|
{
|
|
|
|
|
int idx, type;
|
|
|
|
|
idx = node->node_idx;
|
|
|
|
|
type = (node->type == 0) ? dfa->nodes[idx].type : node->type;
|
|
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
|
{
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
case OP_OPEN_BRACKET:
|
|
|
|
|
case OP_CLOSE_BRACKET:
|
|
|
|
|
case OP_OPEN_DUP_NUM:
|
|
|
|
|
case OP_CLOSE_DUP_NUM:
|
|
|
|
|
case OP_NON_MATCH_LIST:
|
|
|
|
|
case OP_OPEN_COLL_ELEM:
|
|
|
|
|
case OP_CLOSE_COLL_ELEM:
|
|
|
|
|
case OP_OPEN_EQUIV_CLASS:
|
|
|
|
|
case OP_CLOSE_EQUIV_CLASS:
|
|
|
|
|
case OP_OPEN_CHAR_CLASS:
|
|
|
|
|
case OP_CLOSE_CHAR_CLASS:
|
|
|
|
|
/* These must not be appeared here. */
|
|
|
|
|
assert (0);
|
|
|
|
|
#endif
|
|
|
|
|
case END_OF_RE:
|
|
|
|
|
case CHARACTER:
|
|
|
|
|
case OP_PERIOD:
|
|
|
|
|
case OP_DUP_ASTERISK:
|
|
|
|
|
case OP_DUP_QUESTION:
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#ifdef RE_ENABLE_I18N
|
2002-02-27 03:06:03 +08:00
|
|
|
|
case COMPLEX_BRACKET:
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#endif /* RE_ENABLE_I18N */
|
2002-02-27 03:06:03 +08:00
|
|
|
|
case SIMPLE_BRACKET:
|
|
|
|
|
case OP_BACK_REF:
|
|
|
|
|
case ANCHOR:
|
2002-07-11 07:09:16 +08:00
|
|
|
|
case OP_OPEN_SUBEXP:
|
|
|
|
|
case OP_CLOSE_SUBEXP:
|
2002-02-27 03:06:03 +08:00
|
|
|
|
node->first = idx;
|
|
|
|
|
break;
|
|
|
|
|
case OP_DUP_PLUS:
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
assert (node->left != NULL);
|
|
|
|
|
#endif
|
|
|
|
|
if (node->left->first == -1)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
calc_first (dfa, node->left);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
node->first = node->left->first;
|
|
|
|
|
break;
|
|
|
|
|
case OP_ALT:
|
|
|
|
|
node->first = idx;
|
|
|
|
|
break;
|
|
|
|
|
/* else fall through */
|
|
|
|
|
default:
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
assert (node->left != NULL);
|
|
|
|
|
#endif
|
|
|
|
|
if (node->left->first == -1)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
calc_first (dfa, node->left);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
node->first = node->left->first;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Calculate "next" for the node NODE. */
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
calc_next (dfa, node)
|
|
|
|
|
re_dfa_t *dfa;
|
|
|
|
|
bin_tree_t *node;
|
|
|
|
|
{
|
|
|
|
|
int idx, type;
|
|
|
|
|
bin_tree_t *parent = node->parent;
|
|
|
|
|
if (parent == NULL)
|
|
|
|
|
{
|
|
|
|
|
node->next = -1;
|
|
|
|
|
idx = node->node_idx;
|
|
|
|
|
if (node->type == 0)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
dfa->nexts[idx] = node->next;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
idx = parent->node_idx;
|
|
|
|
|
type = (parent->type == 0) ? dfa->nodes[idx].type : parent->type;
|
|
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
|
{
|
|
|
|
|
case OP_DUP_ASTERISK:
|
|
|
|
|
case OP_DUP_PLUS:
|
|
|
|
|
node->next = idx;
|
|
|
|
|
break;
|
|
|
|
|
case CONCAT:
|
|
|
|
|
if (parent->left == node)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
if (parent->right->first == -1)
|
|
|
|
|
calc_first (dfa, parent->right);
|
|
|
|
|
node->next = parent->right->first;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
/* else fall through */
|
|
|
|
|
default:
|
|
|
|
|
if (parent->next == -1)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
calc_next (dfa, parent);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
node->next = parent->next;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
idx = node->node_idx;
|
|
|
|
|
if (node->type == 0)
|
|
|
|
|
dfa->nexts[idx] = node->next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Calculate "edest" for the node NODE. */
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
calc_epsdest (dfa, node)
|
|
|
|
|
re_dfa_t *dfa;
|
|
|
|
|
bin_tree_t *node;
|
|
|
|
|
{
|
|
|
|
|
int idx;
|
|
|
|
|
idx = node->node_idx;
|
|
|
|
|
if (node->type == 0)
|
|
|
|
|
{
|
|
|
|
|
if (dfa->nodes[idx].type == OP_DUP_ASTERISK
|
2002-11-07 04:36:47 +08:00
|
|
|
|
|| dfa->nodes[idx].type == OP_DUP_PLUS
|
|
|
|
|
|| dfa->nodes[idx].type == OP_DUP_QUESTION)
|
|
|
|
|
{
|
|
|
|
|
if (node->left->first == -1)
|
|
|
|
|
calc_first (dfa, node->left);
|
|
|
|
|
if (node->next == -1)
|
|
|
|
|
calc_next (dfa, node);
|
|
|
|
|
re_node_set_init_2 (dfa->edests + idx, node->left->first,
|
|
|
|
|
node->next);
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
else if (dfa->nodes[idx].type == OP_ALT)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
int left, right;
|
|
|
|
|
if (node->left != NULL)
|
|
|
|
|
{
|
|
|
|
|
if (node->left->first == -1)
|
|
|
|
|
calc_first (dfa, node->left);
|
|
|
|
|
left = node->left->first;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (node->next == -1)
|
|
|
|
|
calc_next (dfa, node);
|
|
|
|
|
left = node->next;
|
|
|
|
|
}
|
|
|
|
|
if (node->right != NULL)
|
|
|
|
|
{
|
|
|
|
|
if (node->right->first == -1)
|
|
|
|
|
calc_first (dfa, node->right);
|
|
|
|
|
right = node->right->first;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (node->next == -1)
|
|
|
|
|
calc_next (dfa, node);
|
|
|
|
|
right = node->next;
|
|
|
|
|
}
|
|
|
|
|
re_node_set_init_2 (dfa->edests + idx, left, right);
|
|
|
|
|
}
|
2002-07-11 07:09:16 +08:00
|
|
|
|
else if (dfa->nodes[idx].type == ANCHOR
|
2002-11-07 04:36:47 +08:00
|
|
|
|
|| dfa->nodes[idx].type == OP_OPEN_SUBEXP
|
|
|
|
|
|| dfa->nodes[idx].type == OP_CLOSE_SUBEXP
|
|
|
|
|
|| dfa->nodes[idx].type == OP_BACK_REF)
|
|
|
|
|
re_node_set_init_1 (dfa->edests + idx, node->next);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2002-10-12 16:34:26 +08:00
|
|
|
|
/* Duplicate the epsilon closure of the node ROOT_NODE.
|
|
|
|
|
Note that duplicated nodes have constraint INIT_CONSTRAINT in addition
|
|
|
|
|
to their own constraint. */
|
|
|
|
|
|
|
|
|
|
static reg_errcode_t
|
|
|
|
|
duplicate_node_closure (dfa, top_org_node, top_clone_node, root_node,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
init_constraint)
|
2002-10-12 16:34:26 +08:00
|
|
|
|
re_dfa_t *dfa;
|
|
|
|
|
int top_org_node, top_clone_node, root_node;
|
|
|
|
|
unsigned int init_constraint;
|
|
|
|
|
{
|
|
|
|
|
reg_errcode_t err;
|
|
|
|
|
int org_node, clone_node, ret;
|
|
|
|
|
unsigned int constraint = init_constraint;
|
|
|
|
|
for (org_node = top_org_node, clone_node = top_clone_node;;)
|
|
|
|
|
{
|
|
|
|
|
int org_dest, clone_dest;
|
|
|
|
|
if (dfa->nodes[org_node].type == OP_BACK_REF)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
2002-10-12 16:34:26 +08:00
|
|
|
|
/* If the back reference epsilon-transit, its destination must
|
|
|
|
|
also have the constraint. Then duplicate the epsilon closure
|
|
|
|
|
of the destination of the back reference, and store it in
|
|
|
|
|
edests of the back reference. */
|
2002-11-07 04:36:47 +08:00
|
|
|
|
org_dest = dfa->nexts[org_node];
|
|
|
|
|
re_node_set_empty (dfa->edests + clone_node);
|
|
|
|
|
err = duplicate_node (&clone_dest, dfa, org_dest, constraint);
|
|
|
|
|
if (BE (err != REG_NOERROR, 0))
|
|
|
|
|
return err;
|
|
|
|
|
dfa->nexts[clone_node] = dfa->nexts[org_node];
|
|
|
|
|
ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
|
|
|
|
|
if (BE (ret < 0, 0))
|
|
|
|
|
return REG_ESPACE;
|
|
|
|
|
}
|
2002-10-12 16:34:26 +08:00
|
|
|
|
else if (dfa->edests[org_node].nelem == 0)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
2002-10-12 16:34:26 +08:00
|
|
|
|
/* In case of the node can't epsilon-transit, don't duplicate the
|
|
|
|
|
destination and store the original destination as the
|
|
|
|
|
destination of the node. */
|
2002-11-07 04:36:47 +08:00
|
|
|
|
dfa->nexts[clone_node] = dfa->nexts[org_node];
|
|
|
|
|
break;
|
|
|
|
|
}
|
2002-10-12 16:34:26 +08:00
|
|
|
|
else if (dfa->edests[org_node].nelem == 1)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
2002-10-12 16:34:26 +08:00
|
|
|
|
/* In case of the node can epsilon-transit, and it has only one
|
|
|
|
|
destination. */
|
2002-11-07 04:36:47 +08:00
|
|
|
|
org_dest = dfa->edests[org_node].elems[0];
|
|
|
|
|
re_node_set_empty (dfa->edests + clone_node);
|
|
|
|
|
if (dfa->nodes[org_node].type == ANCHOR)
|
|
|
|
|
{
|
2002-10-12 16:34:26 +08:00
|
|
|
|
/* In case of the node has another constraint, append it. */
|
2002-11-07 04:36:47 +08:00
|
|
|
|
if (org_node == root_node && clone_node != org_node)
|
|
|
|
|
{
|
2002-10-12 16:34:26 +08:00
|
|
|
|
/* ...but if the node is root_node itself, it means the
|
|
|
|
|
epsilon closure have a loop, then tie it to the
|
|
|
|
|
destination of the root_node. */
|
2002-11-07 04:36:47 +08:00
|
|
|
|
ret = re_node_set_insert (dfa->edests + clone_node,
|
|
|
|
|
org_dest);
|
|
|
|
|
if (BE (ret < 0, 0))
|
|
|
|
|
return REG_ESPACE;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
constraint |= dfa->nodes[org_node].opr.ctx_type;
|
|
|
|
|
}
|
|
|
|
|
err = duplicate_node (&clone_dest, dfa, org_dest, constraint);
|
|
|
|
|
if (BE (err != REG_NOERROR, 0))
|
|
|
|
|
return err;
|
|
|
|
|
ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
|
|
|
|
|
if (BE (ret < 0, 0))
|
|
|
|
|
return REG_ESPACE;
|
|
|
|
|
}
|
2002-10-12 16:34:26 +08:00
|
|
|
|
else /* dfa->edests[org_node].nelem == 2 */
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
2002-10-12 16:34:26 +08:00
|
|
|
|
/* In case of the node can epsilon-transit, and it has two
|
2002-12-17 18:58:04 +08:00
|
|
|
|
destinations. E.g. '|', '*', '+', '?'. */
|
2002-11-07 04:36:47 +08:00
|
|
|
|
org_dest = dfa->edests[org_node].elems[0];
|
|
|
|
|
re_node_set_empty (dfa->edests + clone_node);
|
2002-12-17 18:58:04 +08:00
|
|
|
|
/* Search for a duplicated node which satisfies the constraint. */
|
|
|
|
|
clone_dest = search_duplicated_node (dfa, org_dest, constraint);
|
|
|
|
|
if (clone_dest == -1)
|
|
|
|
|
{
|
|
|
|
|
/* There are no such a duplicated node, create a new one. */
|
|
|
|
|
err = duplicate_node (&clone_dest, dfa, org_dest, constraint);
|
|
|
|
|
if (BE (err != REG_NOERROR, 0))
|
|
|
|
|
return err;
|
|
|
|
|
ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
|
|
|
|
|
if (BE (ret < 0, 0))
|
|
|
|
|
return REG_ESPACE;
|
|
|
|
|
err = duplicate_node_closure (dfa, org_dest, clone_dest,
|
|
|
|
|
root_node, constraint);
|
|
|
|
|
if (BE (err != REG_NOERROR, 0))
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* There are a duplicated node which satisfy the constraint,
|
|
|
|
|
use it to avoid infinite loop. */
|
|
|
|
|
ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
|
|
|
|
|
if (BE (ret < 0, 0))
|
|
|
|
|
return REG_ESPACE;
|
|
|
|
|
}
|
2002-11-07 04:36:47 +08:00
|
|
|
|
|
|
|
|
|
org_dest = dfa->edests[org_node].elems[1];
|
|
|
|
|
err = duplicate_node (&clone_dest, dfa, org_dest, constraint);
|
|
|
|
|
if (BE (err != REG_NOERROR, 0))
|
|
|
|
|
return err;
|
|
|
|
|
ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
|
|
|
|
|
if (BE (ret < 0, 0))
|
|
|
|
|
return REG_ESPACE;
|
|
|
|
|
}
|
2002-10-12 16:34:26 +08:00
|
|
|
|
org_node = org_dest;
|
|
|
|
|
clone_node = clone_dest;
|
|
|
|
|
}
|
|
|
|
|
return REG_NOERROR;
|
|
|
|
|
}
|
|
|
|
|
|
2002-12-17 18:58:04 +08:00
|
|
|
|
/* Search for a node which is duplicated from the node ORG_NODE, and
|
|
|
|
|
satisfies the constraint CONSTRAINT. */
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
search_duplicated_node (dfa, org_node, constraint)
|
|
|
|
|
re_dfa_t *dfa;
|
|
|
|
|
int org_node;
|
|
|
|
|
unsigned int constraint;
|
|
|
|
|
{
|
|
|
|
|
int idx;
|
|
|
|
|
for (idx = dfa->nodes_len - 1; dfa->nodes[idx].duplicated && idx > 0; --idx)
|
|
|
|
|
{
|
|
|
|
|
if (org_node == dfa->org_indices[idx]
|
|
|
|
|
&& constraint == dfa->nodes[idx].constraint)
|
|
|
|
|
return idx; /* Found. */
|
|
|
|
|
}
|
|
|
|
|
return -1; /* Not found. */
|
|
|
|
|
}
|
|
|
|
|
|
2002-02-28 15:43:13 +08:00
|
|
|
|
/* Duplicate the node whose index is ORG_IDX and set the constraint CONSTRAINT.
|
|
|
|
|
The new index will be stored in NEW_IDX and return REG_NOERROR if succeeded,
|
|
|
|
|
otherwise return the error code. */
|
|
|
|
|
|
|
|
|
|
static reg_errcode_t
|
|
|
|
|
duplicate_node (new_idx, dfa, org_idx, constraint)
|
2002-02-27 03:06:03 +08:00
|
|
|
|
re_dfa_t *dfa;
|
2002-02-28 15:43:13 +08:00
|
|
|
|
int *new_idx, org_idx;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
unsigned int constraint;
|
|
|
|
|
{
|
|
|
|
|
re_token_t dup;
|
|
|
|
|
int dup_idx;
|
|
|
|
|
|
2002-10-12 16:34:26 +08:00
|
|
|
|
dup = dfa->nodes[org_idx];
|
2002-02-28 15:43:13 +08:00
|
|
|
|
dup_idx = re_dfa_add_node (dfa, dup, 1);
|
2002-10-12 16:34:26 +08:00
|
|
|
|
if (BE (dup_idx == -1, 0))
|
2002-02-28 15:43:13 +08:00
|
|
|
|
return REG_ESPACE;
|
2002-10-12 16:34:26 +08:00
|
|
|
|
dfa->nodes[dup_idx].constraint = constraint;
|
|
|
|
|
if (dfa->nodes[org_idx].type == ANCHOR)
|
|
|
|
|
dfa->nodes[dup_idx].constraint |= dfa->nodes[org_idx].opr.ctx_type;
|
2002-02-28 15:43:13 +08:00
|
|
|
|
dfa->nodes[dup_idx].duplicated = 1;
|
2002-10-12 16:34:26 +08:00
|
|
|
|
re_node_set_init_empty (dfa->edests + dup_idx);
|
|
|
|
|
re_node_set_init_empty (dfa->eclosures + dup_idx);
|
|
|
|
|
re_node_set_init_empty (dfa->inveclosures + dup_idx);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
|
2002-12-17 18:58:04 +08:00
|
|
|
|
/* Store the index of the original node. */
|
|
|
|
|
dfa->org_indices[dup_idx] = org_idx;
|
2002-02-28 15:43:13 +08:00
|
|
|
|
*new_idx = dup_idx;
|
|
|
|
|
return REG_NOERROR;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
calc_inveclosure (dfa)
|
|
|
|
|
re_dfa_t *dfa;
|
|
|
|
|
{
|
2002-10-12 16:34:26 +08:00
|
|
|
|
int src, idx, dest;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
for (src = 0; src < dfa->nodes_len; ++src)
|
|
|
|
|
{
|
|
|
|
|
for (idx = 0; idx < dfa->eclosures[src].nelem; ++idx)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
dest = dfa->eclosures[src].elems[idx];
|
|
|
|
|
re_node_set_insert (dfa->inveclosures + dest, src);
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Calculate "eclosure" for all the node in DFA. */
|
|
|
|
|
|
|
|
|
|
static reg_errcode_t
|
|
|
|
|
calc_eclosure (dfa)
|
|
|
|
|
re_dfa_t *dfa;
|
|
|
|
|
{
|
2002-10-12 16:34:26 +08:00
|
|
|
|
int node_idx, incomplete;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
assert (dfa->nodes_len > 0);
|
|
|
|
|
#endif
|
2002-10-12 16:34:26 +08:00
|
|
|
|
incomplete = 0;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
/* For each nodes, calculate epsilon closure. */
|
2002-10-12 16:34:26 +08:00
|
|
|
|
for (node_idx = 0; ; ++node_idx)
|
2002-02-27 03:06:03 +08:00
|
|
|
|
{
|
2002-02-28 15:43:13 +08:00
|
|
|
|
reg_errcode_t err;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
re_node_set eclosure_elem;
|
2002-10-12 16:34:26 +08:00
|
|
|
|
if (node_idx == dfa->nodes_len)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
if (!incomplete)
|
|
|
|
|
break;
|
|
|
|
|
incomplete = 0;
|
|
|
|
|
node_idx = 0;
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
assert (dfa->eclosures[node_idx].nelem != -1);
|
|
|
|
|
#endif
|
|
|
|
|
/* If we have already calculated, skip it. */
|
|
|
|
|
if (dfa->eclosures[node_idx].nelem != 0)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
continue;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
/* Calculate epsilon closure of `node_idx'. */
|
2002-02-28 15:43:13 +08:00
|
|
|
|
err = calc_eclosure_iter (&eclosure_elem, dfa, node_idx, 1);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (err != REG_NOERROR, 0))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
return err;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
|
|
|
|
|
if (dfa->eclosures[node_idx].nelem == 0)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
incomplete = 1;
|
|
|
|
|
re_node_set_free (&eclosure_elem);
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
return REG_NOERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Calculate epsilon closure of NODE. */
|
|
|
|
|
|
2002-02-28 15:43:13 +08:00
|
|
|
|
static reg_errcode_t
|
|
|
|
|
calc_eclosure_iter (new_set, dfa, node, root)
|
|
|
|
|
re_node_set *new_set;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
re_dfa_t *dfa;
|
|
|
|
|
int node, root;
|
|
|
|
|
{
|
2002-02-28 15:43:13 +08:00
|
|
|
|
reg_errcode_t err;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
unsigned int constraint;
|
2002-10-12 16:34:26 +08:00
|
|
|
|
int i, incomplete;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
re_node_set eclosure;
|
2002-10-12 16:34:26 +08:00
|
|
|
|
incomplete = 0;
|
2002-02-28 15:43:13 +08:00
|
|
|
|
err = re_node_set_alloc (&eclosure, dfa->edests[node].nelem + 1);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (err != REG_NOERROR, 0))
|
2002-02-28 15:43:13 +08:00
|
|
|
|
return err;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
|
|
|
|
|
/* This indicates that we are calculating this node now.
|
|
|
|
|
We reference this value to avoid infinite loop. */
|
|
|
|
|
dfa->eclosures[node].nelem = -1;
|
|
|
|
|
|
|
|
|
|
constraint = ((dfa->nodes[node].type == ANCHOR)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
? dfa->nodes[node].opr.ctx_type : 0);
|
2002-10-12 16:34:26 +08:00
|
|
|
|
/* If the current node has constraints, duplicate all nodes.
|
|
|
|
|
Since they must inherit the constraints. */
|
|
|
|
|
if (constraint && !dfa->nodes[dfa->edests[node].elems[0]].duplicated)
|
|
|
|
|
{
|
|
|
|
|
int org_node, cur_node;
|
|
|
|
|
org_node = cur_node = node;
|
|
|
|
|
err = duplicate_node_closure (dfa, node, node, node, constraint);
|
|
|
|
|
if (BE (err != REG_NOERROR, 0))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
return err;
|
2002-10-12 16:34:26 +08:00
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
|
|
|
|
|
/* Expand each epsilon destination nodes. */
|
2002-10-12 16:34:26 +08:00
|
|
|
|
if (IS_EPSILON_NODE(dfa->nodes[node].type))
|
2002-02-27 03:06:03 +08:00
|
|
|
|
for (i = 0; i < dfa->edests[node].nelem; ++i)
|
|
|
|
|
{
|
2002-11-07 04:36:47 +08:00
|
|
|
|
re_node_set eclosure_elem;
|
|
|
|
|
int edest = dfa->edests[node].elems[i];
|
|
|
|
|
/* If calculating the epsilon closure of `edest' is in progress,
|
|
|
|
|
return intermediate result. */
|
|
|
|
|
if (dfa->eclosures[edest].nelem == -1)
|
|
|
|
|
{
|
|
|
|
|
incomplete = 1;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
/* If we haven't calculated the epsilon closure of `edest' yet,
|
|
|
|
|
calculate now. Otherwise use calculated epsilon closure. */
|
|
|
|
|
if (dfa->eclosures[edest].nelem == 0)
|
|
|
|
|
{
|
|
|
|
|
err = calc_eclosure_iter (&eclosure_elem, dfa, edest, 0);
|
|
|
|
|
if (BE (err != REG_NOERROR, 0))
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
eclosure_elem = dfa->eclosures[edest];
|
|
|
|
|
/* Merge the epsilon closure of `edest'. */
|
|
|
|
|
re_node_set_merge (&eclosure, &eclosure_elem);
|
|
|
|
|
/* If the epsilon closure of `edest' is incomplete,
|
|
|
|
|
the epsilon closure of this node is also incomplete. */
|
|
|
|
|
if (dfa->eclosures[edest].nelem == 0)
|
|
|
|
|
{
|
|
|
|
|
incomplete = 1;
|
|
|
|
|
re_node_set_free (&eclosure_elem);
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Epsilon closures include itself. */
|
|
|
|
|
re_node_set_insert (&eclosure, node);
|
|
|
|
|
if (incomplete && !root)
|
|
|
|
|
dfa->eclosures[node].nelem = 0;
|
|
|
|
|
else
|
|
|
|
|
dfa->eclosures[node] = eclosure;
|
2002-02-28 15:43:13 +08:00
|
|
|
|
*new_set = eclosure;
|
|
|
|
|
return REG_NOERROR;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Functions for token which are used in the parser. */
|
|
|
|
|
|
|
|
|
|
/* Fetch a token from INPUT.
|
|
|
|
|
We must not use this function inside bracket expressions. */
|
|
|
|
|
|
|
|
|
|
static re_token_t
|
|
|
|
|
fetch_token (input, syntax)
|
|
|
|
|
re_string_t *input;
|
|
|
|
|
reg_syntax_t syntax;
|
|
|
|
|
{
|
|
|
|
|
re_token_t token;
|
|
|
|
|
int consumed_byte;
|
|
|
|
|
consumed_byte = peek_token (&token, input, syntax);
|
|
|
|
|
re_string_skip_bytes (input, consumed_byte);
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Peek a token from INPUT, and return the length of the token.
|
|
|
|
|
We must not use this function inside bracket expressions. */
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
peek_token (token, input, syntax)
|
|
|
|
|
re_token_t *token;
|
|
|
|
|
re_string_t *input;
|
|
|
|
|
reg_syntax_t syntax;
|
|
|
|
|
{
|
|
|
|
|
unsigned char c;
|
|
|
|
|
|
|
|
|
|
if (re_string_eoi (input))
|
|
|
|
|
{
|
|
|
|
|
token->type = END_OF_RE;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c = re_string_peek_byte (input, 0);
|
|
|
|
|
token->opr.c = c;
|
|
|
|
|
|
|
|
|
|
#ifdef RE_ENABLE_I18N
|
|
|
|
|
token->mb_partial = 0;
|
|
|
|
|
if (MB_CUR_MAX > 1 &&
|
|
|
|
|
!re_string_first_byte (input, re_string_cur_idx (input)))
|
|
|
|
|
{
|
|
|
|
|
token->type = CHARACTER;
|
|
|
|
|
token->mb_partial = 1;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
if (c == '\\')
|
|
|
|
|
{
|
|
|
|
|
unsigned char c2;
|
|
|
|
|
if (re_string_cur_idx (input) + 1 >= re_string_length (input))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
token->type = BACK_SLASH;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
|
|
|
|
|
c2 = re_string_peek_byte_case (input, 1);
|
|
|
|
|
token->opr.c = c2;
|
|
|
|
|
token->type = CHARACTER;
|
|
|
|
|
switch (c2)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
case '|':
|
|
|
|
|
if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_NO_BK_VBAR))
|
|
|
|
|
token->type = OP_ALT;
|
|
|
|
|
break;
|
|
|
|
|
case '1': case '2': case '3': case '4': case '5':
|
|
|
|
|
case '6': case '7': case '8': case '9':
|
|
|
|
|
if (!(syntax & RE_NO_BK_REFS))
|
|
|
|
|
{
|
|
|
|
|
token->type = OP_BACK_REF;
|
|
|
|
|
token->opr.idx = c2 - '0';
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case '<':
|
|
|
|
|
if (!(syntax & RE_NO_GNU_OPS))
|
|
|
|
|
{
|
|
|
|
|
token->type = ANCHOR;
|
|
|
|
|
token->opr.idx = WORD_FIRST;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case '>':
|
|
|
|
|
if (!(syntax & RE_NO_GNU_OPS))
|
|
|
|
|
{
|
|
|
|
|
token->type = ANCHOR;
|
|
|
|
|
token->opr.idx = WORD_LAST;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'b':
|
|
|
|
|
if (!(syntax & RE_NO_GNU_OPS))
|
|
|
|
|
{
|
|
|
|
|
token->type = ANCHOR;
|
|
|
|
|
token->opr.idx = WORD_DELIM;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'B':
|
|
|
|
|
if (!(syntax & RE_NO_GNU_OPS))
|
|
|
|
|
{
|
|
|
|
|
token->type = ANCHOR;
|
|
|
|
|
token->opr.idx = INSIDE_WORD;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'w':
|
|
|
|
|
if (!(syntax & RE_NO_GNU_OPS))
|
|
|
|
|
token->type = OP_WORD;
|
|
|
|
|
break;
|
|
|
|
|
case 'W':
|
|
|
|
|
if (!(syntax & RE_NO_GNU_OPS))
|
|
|
|
|
token->type = OP_NOTWORD;
|
|
|
|
|
break;
|
|
|
|
|
case '`':
|
|
|
|
|
if (!(syntax & RE_NO_GNU_OPS))
|
|
|
|
|
{
|
|
|
|
|
token->type = ANCHOR;
|
|
|
|
|
token->opr.idx = BUF_FIRST;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case '\'':
|
|
|
|
|
if (!(syntax & RE_NO_GNU_OPS))
|
|
|
|
|
{
|
|
|
|
|
token->type = ANCHOR;
|
|
|
|
|
token->opr.idx = BUF_LAST;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case '(':
|
|
|
|
|
if (!(syntax & RE_NO_BK_PARENS))
|
|
|
|
|
token->type = OP_OPEN_SUBEXP;
|
|
|
|
|
break;
|
|
|
|
|
case ')':
|
|
|
|
|
if (!(syntax & RE_NO_BK_PARENS))
|
|
|
|
|
token->type = OP_CLOSE_SUBEXP;
|
|
|
|
|
break;
|
|
|
|
|
case '+':
|
|
|
|
|
if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_BK_PLUS_QM))
|
|
|
|
|
token->type = OP_DUP_PLUS;
|
|
|
|
|
break;
|
|
|
|
|
case '?':
|
|
|
|
|
if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_BK_PLUS_QM))
|
|
|
|
|
token->type = OP_DUP_QUESTION;
|
|
|
|
|
break;
|
|
|
|
|
case '{':
|
|
|
|
|
if ((syntax & RE_INTERVALS) && (!(syntax & RE_NO_BK_BRACES)))
|
|
|
|
|
token->type = OP_OPEN_DUP_NUM;
|
|
|
|
|
break;
|
|
|
|
|
case '}':
|
|
|
|
|
if ((syntax & RE_INTERVALS) && (!(syntax & RE_NO_BK_BRACES)))
|
|
|
|
|
token->type = OP_CLOSE_DUP_NUM;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
token->type = CHARACTER;
|
|
|
|
|
switch (c)
|
|
|
|
|
{
|
|
|
|
|
case '\n':
|
|
|
|
|
if (syntax & RE_NEWLINE_ALT)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
token->type = OP_ALT;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
break;
|
|
|
|
|
case '|':
|
|
|
|
|
if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_NO_BK_VBAR))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
token->type = OP_ALT;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
break;
|
|
|
|
|
case '*':
|
|
|
|
|
token->type = OP_DUP_ASTERISK;
|
|
|
|
|
break;
|
|
|
|
|
case '+':
|
|
|
|
|
if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_BK_PLUS_QM))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
token->type = OP_DUP_PLUS;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
break;
|
|
|
|
|
case '?':
|
|
|
|
|
if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_BK_PLUS_QM))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
token->type = OP_DUP_QUESTION;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
break;
|
|
|
|
|
case '{':
|
|
|
|
|
if ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
token->type = OP_OPEN_DUP_NUM;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
break;
|
|
|
|
|
case '}':
|
|
|
|
|
if ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
token->type = OP_CLOSE_DUP_NUM;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
break;
|
|
|
|
|
case '(':
|
|
|
|
|
if (syntax & RE_NO_BK_PARENS)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
token->type = OP_OPEN_SUBEXP;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
break;
|
|
|
|
|
case ')':
|
|
|
|
|
if (syntax & RE_NO_BK_PARENS)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
token->type = OP_CLOSE_SUBEXP;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
break;
|
|
|
|
|
case '[':
|
|
|
|
|
token->type = OP_OPEN_BRACKET;
|
|
|
|
|
break;
|
|
|
|
|
case '.':
|
|
|
|
|
token->type = OP_PERIOD;
|
|
|
|
|
break;
|
|
|
|
|
case '^':
|
|
|
|
|
if (!(syntax & RE_CONTEXT_INDEP_ANCHORS) &&
|
2002-11-07 04:36:47 +08:00
|
|
|
|
re_string_cur_idx (input) != 0)
|
|
|
|
|
{
|
|
|
|
|
char prev = re_string_peek_byte (input, -1);
|
|
|
|
|
if (prev != '|' && prev != '(' &&
|
|
|
|
|
(!(syntax & RE_NEWLINE_ALT) || prev != '\n'))
|
|
|
|
|
break;
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
token->type = ANCHOR;
|
|
|
|
|
token->opr.idx = LINE_FIRST;
|
|
|
|
|
break;
|
|
|
|
|
case '$':
|
|
|
|
|
if (!(syntax & RE_CONTEXT_INDEP_ANCHORS) &&
|
2002-11-07 04:36:47 +08:00
|
|
|
|
re_string_cur_idx (input) + 1 != re_string_length (input))
|
|
|
|
|
{
|
|
|
|
|
re_token_t next;
|
|
|
|
|
re_string_skip_bytes (input, 1);
|
|
|
|
|
peek_token (&next, input, syntax);
|
|
|
|
|
re_string_skip_bytes (input, -1);
|
|
|
|
|
if (next.type != OP_ALT && next.type != OP_CLOSE_SUBEXP)
|
|
|
|
|
break;
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
token->type = ANCHOR;
|
|
|
|
|
token->opr.idx = LINE_LAST;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Peek a token from INPUT, and return the length of the token.
|
|
|
|
|
We must not use this function out of bracket expressions. */
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
peek_token_bracket (token, input, syntax)
|
|
|
|
|
re_token_t *token;
|
|
|
|
|
re_string_t *input;
|
|
|
|
|
reg_syntax_t syntax;
|
|
|
|
|
{
|
|
|
|
|
unsigned char c;
|
|
|
|
|
if (re_string_eoi (input))
|
|
|
|
|
{
|
|
|
|
|
token->type = END_OF_RE;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
c = re_string_peek_byte (input, 0);
|
|
|
|
|
token->opr.c = c;
|
|
|
|
|
|
|
|
|
|
#ifdef RE_ENABLE_I18N
|
|
|
|
|
if (MB_CUR_MAX > 1 &&
|
|
|
|
|
!re_string_first_byte (input, re_string_cur_idx (input)))
|
|
|
|
|
{
|
|
|
|
|
token->type = CHARACTER;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
#endif /* RE_ENABLE_I18N */
|
|
|
|
|
|
|
|
|
|
if (c == '\\' && (syntax & RE_BACKSLASH_ESCAPE_IN_LISTS))
|
|
|
|
|
{
|
|
|
|
|
/* In this case, '\' escape a character. */
|
|
|
|
|
unsigned char c2;
|
2002-10-22 02:19:01 +08:00
|
|
|
|
re_string_skip_bytes (input, 1);
|
|
|
|
|
c2 = re_string_peek_byte (input, 0);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
token->opr.c = c2;
|
|
|
|
|
token->type = CHARACTER;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
if (c == '[') /* '[' is a special char in a bracket exps. */
|
|
|
|
|
{
|
|
|
|
|
unsigned char c2;
|
|
|
|
|
int token_len;
|
|
|
|
|
c2 = re_string_peek_byte (input, 1);
|
|
|
|
|
token->opr.c = c2;
|
|
|
|
|
token_len = 2;
|
|
|
|
|
switch (c2)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
case '.':
|
|
|
|
|
token->type = OP_OPEN_COLL_ELEM;
|
|
|
|
|
break;
|
|
|
|
|
case '=':
|
|
|
|
|
token->type = OP_OPEN_EQUIV_CLASS;
|
|
|
|
|
break;
|
|
|
|
|
case ':':
|
|
|
|
|
if (syntax & RE_CHAR_CLASSES)
|
|
|
|
|
{
|
|
|
|
|
token->type = OP_OPEN_CHAR_CLASS;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
/* else fall through. */
|
|
|
|
|
default:
|
|
|
|
|
token->type = CHARACTER;
|
|
|
|
|
token->opr.c = c;
|
|
|
|
|
token_len = 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
return token_len;
|
|
|
|
|
}
|
|
|
|
|
switch (c)
|
|
|
|
|
{
|
|
|
|
|
case '-':
|
|
|
|
|
token->type = OP_CHARSET_RANGE;
|
|
|
|
|
break;
|
|
|
|
|
case ']':
|
|
|
|
|
token->type = OP_CLOSE_BRACKET;
|
|
|
|
|
break;
|
|
|
|
|
case '^':
|
|
|
|
|
token->type = OP_NON_MATCH_LIST;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
token->type = CHARACTER;
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Functions for parser. */
|
|
|
|
|
|
|
|
|
|
/* Entry point of the parser.
|
|
|
|
|
Parse the regular expression REGEXP and return the structure tree.
|
|
|
|
|
If an error is occured, ERR is set by error code, and return NULL.
|
|
|
|
|
This function build the following tree, from regular expression <reg_exp>:
|
2002-11-07 04:36:47 +08:00
|
|
|
|
CAT
|
|
|
|
|
/ \
|
|
|
|
|
/ \
|
2002-02-27 03:06:03 +08:00
|
|
|
|
<reg_exp> EOR
|
|
|
|
|
|
|
|
|
|
CAT means concatenation.
|
|
|
|
|
EOR means end of regular expression. */
|
|
|
|
|
|
|
|
|
|
static bin_tree_t *
|
|
|
|
|
parse (regexp, preg, syntax, err)
|
|
|
|
|
re_string_t *regexp;
|
|
|
|
|
regex_t *preg;
|
|
|
|
|
reg_syntax_t syntax;
|
|
|
|
|
reg_errcode_t *err;
|
|
|
|
|
{
|
|
|
|
|
re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
|
|
|
|
|
bin_tree_t *tree, *eor, *root;
|
|
|
|
|
re_token_t current_token;
|
|
|
|
|
int new_idx;
|
|
|
|
|
current_token = fetch_token (regexp, syntax);
|
|
|
|
|
tree = parse_reg_exp (regexp, preg, ¤t_token, syntax, 0, err);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (*err != REG_NOERROR && tree == NULL, 0))
|
2002-02-27 03:06:03 +08:00
|
|
|
|
return NULL;
|
|
|
|
|
new_idx = re_dfa_add_node (dfa, current_token, 0);
|
|
|
|
|
eor = create_tree (NULL, NULL, 0, new_idx);
|
|
|
|
|
if (tree != NULL)
|
|
|
|
|
root = create_tree (tree, eor, CONCAT, 0);
|
|
|
|
|
else
|
|
|
|
|
root = eor;
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (new_idx == -1 || eor == NULL || root == NULL, 0))
|
2002-10-12 16:34:26 +08:00
|
|
|
|
{
|
|
|
|
|
*err = REG_ESPACE;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* This function build the following tree, from regular expression
|
|
|
|
|
<branch1>|<branch2>:
|
2002-11-07 04:36:47 +08:00
|
|
|
|
ALT
|
|
|
|
|
/ \
|
|
|
|
|
/ \
|
2002-02-27 03:06:03 +08:00
|
|
|
|
<branch1> <branch2>
|
|
|
|
|
|
|
|
|
|
ALT means alternative, which represents the operator `|'. */
|
|
|
|
|
|
|
|
|
|
static bin_tree_t *
|
|
|
|
|
parse_reg_exp (regexp, preg, token, syntax, nest, err)
|
|
|
|
|
re_string_t *regexp;
|
|
|
|
|
regex_t *preg;
|
|
|
|
|
re_token_t *token;
|
|
|
|
|
reg_syntax_t syntax;
|
|
|
|
|
int nest;
|
|
|
|
|
reg_errcode_t *err;
|
|
|
|
|
{
|
|
|
|
|
re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
|
|
|
|
|
bin_tree_t *tree, *branch = NULL;
|
|
|
|
|
int new_idx;
|
|
|
|
|
tree = parse_branch (regexp, preg, token, syntax, nest, err);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (*err != REG_NOERROR && tree == NULL, 0))
|
2002-02-27 03:06:03 +08:00
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
while (token->type == OP_ALT)
|
|
|
|
|
{
|
|
|
|
|
re_token_t alt_token = *token;
|
|
|
|
|
new_idx = re_dfa_add_node (dfa, alt_token, 0);
|
|
|
|
|
*token = fetch_token (regexp, syntax);
|
|
|
|
|
if (token->type != OP_ALT && token->type != END_OF_RE
|
2002-11-07 04:36:47 +08:00
|
|
|
|
&& (nest == 0 || token->type != OP_CLOSE_SUBEXP))
|
|
|
|
|
{
|
|
|
|
|
branch = parse_branch (regexp, preg, token, syntax, nest, err);
|
|
|
|
|
if (BE (*err != REG_NOERROR && branch == NULL, 0))
|
|
|
|
|
{
|
|
|
|
|
free_bin_tree (tree);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-08-13 03:18:45 +08:00
|
|
|
|
else
|
|
|
|
|
branch = NULL;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
tree = create_tree (tree, branch, 0, new_idx);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (new_idx == -1 || tree == NULL, 0))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
*err = REG_ESPACE;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2002-09-28 13:28:44 +08:00
|
|
|
|
dfa->has_plural_match = 1;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
return tree;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* This function build the following tree, from regular expression
|
|
|
|
|
<exp1><exp2>:
|
2002-11-07 04:36:47 +08:00
|
|
|
|
CAT
|
|
|
|
|
/ \
|
2002-02-27 03:06:03 +08:00
|
|
|
|
/ \
|
|
|
|
|
<exp1> <exp2>
|
|
|
|
|
|
|
|
|
|
CAT means concatenation. */
|
|
|
|
|
|
|
|
|
|
static bin_tree_t *
|
|
|
|
|
parse_branch (regexp, preg, token, syntax, nest, err)
|
|
|
|
|
re_string_t *regexp;
|
|
|
|
|
regex_t *preg;
|
|
|
|
|
re_token_t *token;
|
|
|
|
|
reg_syntax_t syntax;
|
|
|
|
|
int nest;
|
|
|
|
|
reg_errcode_t *err;
|
|
|
|
|
{
|
|
|
|
|
bin_tree_t *tree, *exp;
|
|
|
|
|
tree = parse_expression (regexp, preg, token, syntax, nest, err);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (*err != REG_NOERROR && tree == NULL, 0))
|
2002-02-27 03:06:03 +08:00
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
while (token->type != OP_ALT && token->type != END_OF_RE
|
2002-11-07 04:36:47 +08:00
|
|
|
|
&& (nest == 0 || token->type != OP_CLOSE_SUBEXP))
|
2002-02-27 03:06:03 +08:00
|
|
|
|
{
|
|
|
|
|
exp = parse_expression (regexp, preg, token, syntax, nest, err);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (*err != REG_NOERROR && exp == NULL, 0))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
free_bin_tree (tree);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
if (tree != NULL && exp != NULL)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
tree = create_tree (tree, exp, CONCAT, 0);
|
|
|
|
|
if (tree == NULL)
|
|
|
|
|
{
|
|
|
|
|
*err = REG_ESPACE;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
else if (tree == NULL)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
tree = exp;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
/* Otherwise exp == NULL, we don't need to create new tree. */
|
|
|
|
|
}
|
|
|
|
|
return tree;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* This function build the following tree, from regular expression a*:
|
2002-11-07 04:36:47 +08:00
|
|
|
|
*
|
|
|
|
|
|
|
|
|
|
|
a
|
2002-02-27 03:06:03 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static bin_tree_t *
|
|
|
|
|
parse_expression (regexp, preg, token, syntax, nest, err)
|
|
|
|
|
re_string_t *regexp;
|
|
|
|
|
regex_t *preg;
|
|
|
|
|
re_token_t *token;
|
|
|
|
|
reg_syntax_t syntax;
|
|
|
|
|
int nest;
|
|
|
|
|
reg_errcode_t *err;
|
|
|
|
|
{
|
|
|
|
|
re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
|
|
|
|
|
bin_tree_t *tree;
|
|
|
|
|
int new_idx;
|
|
|
|
|
switch (token->type)
|
|
|
|
|
{
|
|
|
|
|
case CHARACTER:
|
|
|
|
|
new_idx = re_dfa_add_node (dfa, *token, 0);
|
|
|
|
|
tree = create_tree (NULL, NULL, 0, new_idx);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (new_idx == -1 || tree == NULL, 0))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
*err = REG_ESPACE;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
#ifdef RE_ENABLE_I18N
|
|
|
|
|
if (MB_CUR_MAX > 1)
|
|
|
|
|
{
|
|
|
|
|
while (!re_string_eoi (regexp)
|
|
|
|
|
&& !re_string_first_byte (regexp, re_string_cur_idx (regexp)))
|
|
|
|
|
{
|
2002-11-07 04:36:47 +08:00
|
|
|
|
bin_tree_t *mbc_remain;
|
|
|
|
|
*token = fetch_token (regexp, syntax);
|
|
|
|
|
new_idx = re_dfa_add_node (dfa, *token, 0);
|
|
|
|
|
mbc_remain = create_tree (NULL, NULL, 0, new_idx);
|
|
|
|
|
tree = create_tree (tree, mbc_remain, CONCAT, 0);
|
|
|
|
|
if (BE (new_idx == -1 || mbc_remain == NULL || tree == NULL, 0))
|
2003-02-21 09:52:32 +08:00
|
|
|
|
{
|
|
|
|
|
*err = REG_ESPACE;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2002-11-07 04:36:47 +08:00
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
break;
|
|
|
|
|
case OP_OPEN_SUBEXP:
|
|
|
|
|
tree = parse_sub_exp (regexp, preg, token, syntax, nest + 1, err);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (*err != REG_NOERROR && tree == NULL, 0))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
return NULL;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
break;
|
|
|
|
|
case OP_OPEN_BRACKET:
|
|
|
|
|
tree = parse_bracket_exp (regexp, dfa, token, syntax, err);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (*err != REG_NOERROR && tree == NULL, 0))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
return NULL;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
break;
|
|
|
|
|
case OP_BACK_REF:
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (preg->re_nsub < token->opr.idx
|
2002-11-07 04:36:47 +08:00
|
|
|
|
|| dfa->subexps[token->opr.idx - 1].end == -1, 0))
|
|
|
|
|
{
|
|
|
|
|
*err = REG_ESUBREG;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2002-11-28 07:00:16 +08:00
|
|
|
|
dfa->used_bkref_map |= 1 << (token->opr.idx - 1);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
new_idx = re_dfa_add_node (dfa, *token, 0);
|
|
|
|
|
tree = create_tree (NULL, NULL, 0, new_idx);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (new_idx == -1 || tree == NULL, 0))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
*err = REG_ESPACE;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
++dfa->nbackref;
|
|
|
|
|
dfa->has_mb_node = 1;
|
|
|
|
|
break;
|
|
|
|
|
case OP_DUP_ASTERISK:
|
|
|
|
|
case OP_DUP_PLUS:
|
|
|
|
|
case OP_DUP_QUESTION:
|
|
|
|
|
case OP_OPEN_DUP_NUM:
|
|
|
|
|
if (syntax & RE_CONTEXT_INVALID_OPS)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
*err = REG_BADRPT;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
else if (syntax & RE_CONTEXT_INDEP_OPS)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
*token = fetch_token (regexp, syntax);
|
|
|
|
|
return parse_expression (regexp, preg, token, syntax, nest, err);
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
/* else fall through */
|
|
|
|
|
case OP_CLOSE_SUBEXP:
|
|
|
|
|
if ((token->type == OP_CLOSE_SUBEXP) &&
|
2002-11-07 04:36:47 +08:00
|
|
|
|
!(syntax & RE_UNMATCHED_RIGHT_PAREN_ORD))
|
|
|
|
|
{
|
|
|
|
|
*err = REG_ERPAREN;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
/* else fall through */
|
|
|
|
|
case OP_CLOSE_DUP_NUM:
|
|
|
|
|
/* We treat it as a normal character. */
|
|
|
|
|
|
|
|
|
|
/* Then we can these characters as normal characters. */
|
|
|
|
|
token->type = CHARACTER;
|
|
|
|
|
new_idx = re_dfa_add_node (dfa, *token, 0);
|
|
|
|
|
tree = create_tree (NULL, NULL, 0, new_idx);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (new_idx == -1 || tree == NULL, 0))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
*err = REG_ESPACE;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
break;
|
|
|
|
|
case ANCHOR:
|
|
|
|
|
if (dfa->word_char == NULL)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
*err = init_word_char (dfa);
|
|
|
|
|
if (BE (*err != REG_NOERROR, 0))
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
if (token->opr.ctx_type == WORD_DELIM)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
bin_tree_t *tree_first, *tree_last;
|
|
|
|
|
int idx_first, idx_last;
|
|
|
|
|
token->opr.ctx_type = WORD_FIRST;
|
|
|
|
|
idx_first = re_dfa_add_node (dfa, *token, 0);
|
|
|
|
|
tree_first = create_tree (NULL, NULL, 0, idx_first);
|
|
|
|
|
token->opr.ctx_type = WORD_LAST;
|
|
|
|
|
idx_last = re_dfa_add_node (dfa, *token, 0);
|
|
|
|
|
tree_last = create_tree (NULL, NULL, 0, idx_last);
|
|
|
|
|
token->type = OP_ALT;
|
|
|
|
|
new_idx = re_dfa_add_node (dfa, *token, 0);
|
|
|
|
|
tree = create_tree (tree_first, tree_last, 0, new_idx);
|
|
|
|
|
if (BE (idx_first == -1 || idx_last == -1 || new_idx == -1
|
|
|
|
|
|| tree_first == NULL || tree_last == NULL
|
|
|
|
|
|| tree == NULL, 0))
|
|
|
|
|
{
|
|
|
|
|
*err = REG_ESPACE;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
else
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
new_idx = re_dfa_add_node (dfa, *token, 0);
|
|
|
|
|
tree = create_tree (NULL, NULL, 0, new_idx);
|
|
|
|
|
if (BE (new_idx == -1 || tree == NULL, 0))
|
2003-02-21 09:52:32 +08:00
|
|
|
|
{
|
|
|
|
|
*err = REG_ESPACE;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2002-11-07 04:36:47 +08:00
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
/* We must return here, since ANCHORs can't be followed
|
2002-11-07 04:36:47 +08:00
|
|
|
|
by repetition operators.
|
|
|
|
|
eg. RE"^*" is invalid or "<ANCHOR(^)><CHAR(*)>",
|
|
|
|
|
it must not be "<ANCHOR(^)><REPEAT(*)>". */
|
2002-02-27 03:06:03 +08:00
|
|
|
|
*token = fetch_token (regexp, syntax);
|
|
|
|
|
return tree;
|
|
|
|
|
case OP_PERIOD:
|
|
|
|
|
new_idx = re_dfa_add_node (dfa, *token, 0);
|
|
|
|
|
tree = create_tree (NULL, NULL, 0, new_idx);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (new_idx == -1 || tree == NULL, 0))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
*err = REG_ESPACE;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
if (MB_CUR_MAX > 1)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
dfa->has_mb_node = 1;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
break;
|
|
|
|
|
case OP_WORD:
|
2003-06-13 02:29:52 +08:00
|
|
|
|
tree = build_word_op (dfa, regexp->trans, 0, err);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (*err != REG_NOERROR && tree == NULL, 0))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
return NULL;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
break;
|
|
|
|
|
case OP_NOTWORD:
|
2003-06-13 02:29:52 +08:00
|
|
|
|
tree = build_word_op (dfa, regexp->trans, 1, err);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (*err != REG_NOERROR && tree == NULL, 0))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
return NULL;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
break;
|
|
|
|
|
case OP_ALT:
|
|
|
|
|
case END_OF_RE:
|
|
|
|
|
return NULL;
|
|
|
|
|
case BACK_SLASH:
|
|
|
|
|
*err = REG_EESCAPE;
|
|
|
|
|
return NULL;
|
|
|
|
|
default:
|
|
|
|
|
/* Must not happen? */
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
assert (0);
|
|
|
|
|
#endif
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
*token = fetch_token (regexp, syntax);
|
|
|
|
|
|
|
|
|
|
while (token->type == OP_DUP_ASTERISK || token->type == OP_DUP_PLUS
|
2002-11-07 04:36:47 +08:00
|
|
|
|
|| token->type == OP_DUP_QUESTION || token->type == OP_OPEN_DUP_NUM)
|
2002-02-27 03:06:03 +08:00
|
|
|
|
{
|
|
|
|
|
tree = parse_dup_op (tree, regexp, dfa, token, syntax, err);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (*err != REG_NOERROR && tree == NULL, 0))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
return NULL;
|
2002-09-28 13:28:44 +08:00
|
|
|
|
dfa->has_plural_match = 1;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tree;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* This function build the following tree, from regular expression
|
|
|
|
|
(<reg_exp>):
|
2002-11-07 04:36:47 +08:00
|
|
|
|
SUBEXP
|
|
|
|
|
|
|
|
|
|
|
<reg_exp>
|
2002-02-27 03:06:03 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static bin_tree_t *
|
|
|
|
|
parse_sub_exp (regexp, preg, token, syntax, nest, err)
|
|
|
|
|
re_string_t *regexp;
|
|
|
|
|
regex_t *preg;
|
|
|
|
|
re_token_t *token;
|
|
|
|
|
reg_syntax_t syntax;
|
|
|
|
|
int nest;
|
|
|
|
|
reg_errcode_t *err;
|
|
|
|
|
{
|
|
|
|
|
re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
|
2002-07-11 07:09:16 +08:00
|
|
|
|
bin_tree_t *tree, *left_par, *right_par;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
size_t cur_nsub;
|
2002-07-11 07:09:16 +08:00
|
|
|
|
int new_idx;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
cur_nsub = preg->re_nsub++;
|
|
|
|
|
if (dfa->subexps_alloc < preg->re_nsub)
|
|
|
|
|
{
|
|
|
|
|
re_subexp_t *new_array;
|
|
|
|
|
dfa->subexps_alloc *= 2;
|
|
|
|
|
new_array = re_realloc (dfa->subexps, re_subexp_t, dfa->subexps_alloc);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (new_array == NULL, 0))
|
2002-02-27 03:06:03 +08:00
|
|
|
|
{
|
|
|
|
|
dfa->subexps_alloc /= 2;
|
|
|
|
|
*err = REG_ESPACE;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
dfa->subexps = new_array;
|
|
|
|
|
}
|
|
|
|
|
dfa->subexps[cur_nsub].start = dfa->nodes_len;
|
|
|
|
|
dfa->subexps[cur_nsub].end = -1;
|
2002-07-11 07:09:16 +08:00
|
|
|
|
|
|
|
|
|
new_idx = re_dfa_add_node (dfa, *token, 0);
|
|
|
|
|
left_par = create_tree (NULL, NULL, 0, new_idx);
|
|
|
|
|
if (BE (new_idx == -1 || left_par == NULL, 0))
|
2002-10-12 16:34:26 +08:00
|
|
|
|
{
|
|
|
|
|
*err = REG_ESPACE;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2002-07-11 07:09:16 +08:00
|
|
|
|
dfa->nodes[new_idx].opr.idx = cur_nsub;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
*token = fetch_token (regexp, syntax);
|
|
|
|
|
|
|
|
|
|
/* The subexpression may be a null string. */
|
|
|
|
|
if (token->type == OP_CLOSE_SUBEXP)
|
2002-07-11 07:09:16 +08:00
|
|
|
|
tree = NULL;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
tree = parse_reg_exp (regexp, preg, token, syntax, nest, err);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (*err != REG_NOERROR && tree == NULL, 0))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
return NULL;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
2002-07-11 07:09:16 +08:00
|
|
|
|
if (BE (token->type != OP_CLOSE_SUBEXP, 0))
|
|
|
|
|
{
|
|
|
|
|
free_bin_tree (tree);
|
|
|
|
|
*err = REG_BADPAT;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
new_idx = re_dfa_add_node (dfa, *token, 0);
|
|
|
|
|
dfa->subexps[cur_nsub].end = dfa->nodes_len;
|
|
|
|
|
right_par = create_tree (NULL, NULL, 0, new_idx);
|
|
|
|
|
tree = ((tree == NULL) ? right_par
|
2002-11-07 04:36:47 +08:00
|
|
|
|
: create_tree (tree, right_par, CONCAT, 0));
|
2002-07-11 07:09:16 +08:00
|
|
|
|
tree = create_tree (left_par, tree, CONCAT, 0);
|
|
|
|
|
if (BE (new_idx == -1 || right_par == NULL || tree == NULL, 0))
|
2002-10-12 16:34:26 +08:00
|
|
|
|
{
|
|
|
|
|
*err = REG_ESPACE;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2002-07-11 07:09:16 +08:00
|
|
|
|
dfa->nodes[new_idx].opr.idx = cur_nsub;
|
|
|
|
|
|
2002-02-27 03:06:03 +08:00
|
|
|
|
return tree;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* This function parse repetition operators like "*", "+", "{1,3}" etc. */
|
|
|
|
|
|
|
|
|
|
static bin_tree_t *
|
|
|
|
|
parse_dup_op (dup_elem, regexp, dfa, token, syntax, err)
|
|
|
|
|
bin_tree_t *dup_elem;
|
|
|
|
|
re_string_t *regexp;
|
|
|
|
|
re_dfa_t *dfa;
|
|
|
|
|
re_token_t *token;
|
|
|
|
|
reg_syntax_t syntax;
|
|
|
|
|
reg_errcode_t *err;
|
|
|
|
|
{
|
|
|
|
|
re_token_t dup_token;
|
|
|
|
|
bin_tree_t *tree = dup_elem, *work_tree;
|
|
|
|
|
int new_idx, start_idx = re_string_cur_idx (regexp);
|
|
|
|
|
re_token_t start_token = *token;
|
|
|
|
|
if (token->type == OP_OPEN_DUP_NUM)
|
|
|
|
|
{
|
2002-04-20 08:57:03 +08:00
|
|
|
|
int i;
|
|
|
|
|
int end = 0;
|
|
|
|
|
int start = fetch_number (regexp, token, syntax);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
bin_tree_t *elem;
|
|
|
|
|
if (start == -1)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
if (token->type == CHARACTER && token->opr.c == ',')
|
|
|
|
|
start = 0; /* We treat "{,m}" as "{0,m}". */
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
*err = REG_BADBR; /* <re>{} is invalid. */
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-04-20 08:57:03 +08:00
|
|
|
|
if (BE (start != -2, 1))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
/* We treat "{n}" as "{n,n}". */
|
|
|
|
|
end = ((token->type == OP_CLOSE_DUP_NUM) ? start
|
|
|
|
|
: ((token->type == CHARACTER && token->opr.c == ',')
|
|
|
|
|
? fetch_number (regexp, token, syntax) : -2));
|
|
|
|
|
}
|
2002-04-20 08:57:03 +08:00
|
|
|
|
if (BE (start == -2 || end == -2, 0))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
/* Invalid sequence. */
|
|
|
|
|
if (token->type == OP_CLOSE_DUP_NUM)
|
|
|
|
|
goto parse_dup_op_invalid_interval;
|
|
|
|
|
else
|
|
|
|
|
goto parse_dup_op_ebrace;
|
|
|
|
|
}
|
2002-04-20 08:57:03 +08:00
|
|
|
|
if (BE (start == 0 && end == 0, 0))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
/* We treat "<re>{0}" and "<re>{0,0}" as null string. */
|
|
|
|
|
*token = fetch_token (regexp, syntax);
|
|
|
|
|
free_bin_tree (dup_elem);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2002-04-20 08:57:03 +08:00
|
|
|
|
|
2002-02-27 03:06:03 +08:00
|
|
|
|
/* Extract "<re>{n,m}" to "<re><re>...<re><re>{0,<m-n>}". */
|
|
|
|
|
elem = tree;
|
|
|
|
|
for (i = 0; i < start; ++i)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
if (i != 0)
|
|
|
|
|
{
|
|
|
|
|
work_tree = duplicate_tree (elem, dfa);
|
|
|
|
|
tree = create_tree (tree, work_tree, CONCAT, 0);
|
|
|
|
|
if (BE (work_tree == NULL || tree == NULL, 0))
|
|
|
|
|
goto parse_dup_op_espace;
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
|
|
|
|
|
if (end == -1)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
/* We treat "<re>{0,}" as "<re>*". */
|
|
|
|
|
dup_token.type = OP_DUP_ASTERISK;
|
|
|
|
|
if (start > 0)
|
|
|
|
|
{
|
|
|
|
|
elem = duplicate_tree (elem, dfa);
|
|
|
|
|
new_idx = re_dfa_add_node (dfa, dup_token, 0);
|
|
|
|
|
work_tree = create_tree (elem, NULL, 0, new_idx);
|
|
|
|
|
tree = create_tree (tree, work_tree, CONCAT, 0);
|
|
|
|
|
if (BE (elem == NULL || new_idx == -1 || work_tree == NULL
|
|
|
|
|
|| tree == NULL, 0))
|
|
|
|
|
goto parse_dup_op_espace;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
new_idx = re_dfa_add_node (dfa, dup_token, 0);
|
|
|
|
|
tree = create_tree (elem, NULL, 0, new_idx);
|
|
|
|
|
if (BE (new_idx == -1 || tree == NULL, 0))
|
|
|
|
|
goto parse_dup_op_espace;
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
else if (end - start > 0)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
/* Then extract "<re>{0,m}" to "<re>?<re>?...<re>?". */
|
|
|
|
|
dup_token.type = OP_DUP_QUESTION;
|
|
|
|
|
if (start > 0)
|
|
|
|
|
{
|
|
|
|
|
elem = duplicate_tree (elem, dfa);
|
|
|
|
|
new_idx = re_dfa_add_node (dfa, dup_token, 0);
|
|
|
|
|
elem = create_tree (elem, NULL, 0, new_idx);
|
|
|
|
|
tree = create_tree (tree, elem, CONCAT, 0);
|
|
|
|
|
if (BE (elem == NULL || new_idx == -1 || tree == NULL, 0))
|
|
|
|
|
goto parse_dup_op_espace;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
new_idx = re_dfa_add_node (dfa, dup_token, 0);
|
|
|
|
|
tree = elem = create_tree (elem, NULL, 0, new_idx);
|
|
|
|
|
if (BE (new_idx == -1 || tree == NULL, 0))
|
|
|
|
|
goto parse_dup_op_espace;
|
|
|
|
|
}
|
|
|
|
|
for (i = 1; i < end - start; ++i)
|
|
|
|
|
{
|
|
|
|
|
work_tree = duplicate_tree (elem, dfa);
|
|
|
|
|
tree = create_tree (tree, work_tree, CONCAT, 0);
|
|
|
|
|
if (BE (work_tree == NULL || tree == NULL, 0))
|
|
|
|
|
{
|
|
|
|
|
*err = REG_ESPACE;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
new_idx = re_dfa_add_node (dfa, *token, 0);
|
|
|
|
|
tree = create_tree (tree, NULL, 0, new_idx);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (new_idx == -1 || tree == NULL, 0))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
*err = REG_ESPACE;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
*token = fetch_token (regexp, syntax);
|
|
|
|
|
return tree;
|
|
|
|
|
|
|
|
|
|
parse_dup_op_espace:
|
|
|
|
|
free_bin_tree (tree);
|
|
|
|
|
*err = REG_ESPACE;
|
|
|
|
|
return NULL;
|
|
|
|
|
|
2002-04-20 08:57:03 +08:00
|
|
|
|
parse_dup_op_ebrace:
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (!(syntax & RE_INVALID_INTERVAL_ORD), 0))
|
2002-02-27 03:06:03 +08:00
|
|
|
|
{
|
|
|
|
|
*err = REG_EBRACE;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2002-04-20 08:57:03 +08:00
|
|
|
|
goto parse_dup_op_rollback;
|
|
|
|
|
parse_dup_op_invalid_interval:
|
|
|
|
|
if (BE (!(syntax & RE_INVALID_INTERVAL_ORD), 0))
|
|
|
|
|
{
|
|
|
|
|
*err = REG_BADBR;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
parse_dup_op_rollback:
|
2002-02-27 03:06:03 +08:00
|
|
|
|
re_string_set_index (regexp, start_idx);
|
|
|
|
|
*token = start_token;
|
|
|
|
|
token->type = CHARACTER;
|
|
|
|
|
return dup_elem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Size of the names for collating symbol/equivalence_class/character_class.
|
|
|
|
|
I'm not sure, but maybe enough. */
|
|
|
|
|
#define BRACKET_NAME_BUF_SIZE 32
|
|
|
|
|
|
2002-04-27 04:52:02 +08:00
|
|
|
|
#ifndef _LIBC
|
|
|
|
|
/* Local function for parse_bracket_exp only used in case of NOT _LIBC.
|
|
|
|
|
Build the range expression which starts from START_ELEM, and ends
|
|
|
|
|
at END_ELEM. The result are written to MBCSET and SBCSET.
|
|
|
|
|
RANGE_ALLOC is the allocated size of mbcset->range_starts, and
|
|
|
|
|
mbcset->range_ends, is a pointer argument sinse we may
|
|
|
|
|
update it. */
|
|
|
|
|
|
|
|
|
|
static reg_errcode_t
|
2002-05-24 16:49:00 +08:00
|
|
|
|
# ifdef RE_ENABLE_I18N
|
|
|
|
|
build_range_exp (sbcset, mbcset, range_alloc, start_elem, end_elem)
|
2002-04-27 04:52:02 +08:00
|
|
|
|
re_charset_t *mbcset;
|
|
|
|
|
int *range_alloc;
|
2002-05-24 16:49:00 +08:00
|
|
|
|
# else /* not RE_ENABLE_I18N */
|
|
|
|
|
build_range_exp (sbcset, start_elem, end_elem)
|
|
|
|
|
# endif /* not RE_ENABLE_I18N */
|
|
|
|
|
re_bitset_ptr_t sbcset;
|
2002-04-27 04:52:02 +08:00
|
|
|
|
bracket_elem_t *start_elem, *end_elem;
|
|
|
|
|
{
|
|
|
|
|
unsigned int start_ch, end_ch;
|
|
|
|
|
/* Equivalence Classes and Character Classes can't be a range start/end. */
|
|
|
|
|
if (BE (start_elem->type == EQUIV_CLASS || start_elem->type == CHAR_CLASS
|
2002-11-07 04:36:47 +08:00
|
|
|
|
|| end_elem->type == EQUIV_CLASS || end_elem->type == CHAR_CLASS,
|
|
|
|
|
0))
|
2002-04-27 04:52:02 +08:00
|
|
|
|
return REG_ERANGE;
|
|
|
|
|
|
|
|
|
|
/* We can handle no multi character collating elements without libc
|
|
|
|
|
support. */
|
2002-09-11 01:39:58 +08:00
|
|
|
|
if (BE ((start_elem->type == COLL_SYM
|
2002-11-07 04:36:47 +08:00
|
|
|
|
&& strlen ((char *) start_elem->opr.name) > 1)
|
|
|
|
|
|| (end_elem->type == COLL_SYM
|
|
|
|
|
&& strlen ((char *) end_elem->opr.name) > 1), 0))
|
2002-04-27 04:52:02 +08:00
|
|
|
|
return REG_ECOLLATE;
|
|
|
|
|
|
|
|
|
|
# ifdef RE_ENABLE_I18N
|
|
|
|
|
{
|
|
|
|
|
wchar_t wc, start_wc, end_wc;
|
|
|
|
|
wchar_t cmp_buf[6] = {L'\0', L'\0', L'\0', L'\0', L'\0', L'\0'};
|
|
|
|
|
|
|
|
|
|
start_ch = ((start_elem->type == SB_CHAR) ? start_elem->opr.ch
|
2002-11-07 04:36:47 +08:00
|
|
|
|
: ((start_elem->type == COLL_SYM) ? start_elem->opr.name[0]
|
|
|
|
|
: 0));
|
2002-04-27 04:52:02 +08:00
|
|
|
|
end_ch = ((end_elem->type == SB_CHAR) ? end_elem->opr.ch
|
2002-11-07 04:36:47 +08:00
|
|
|
|
: ((end_elem->type == COLL_SYM) ? end_elem->opr.name[0]
|
|
|
|
|
: 0));
|
2002-04-27 04:52:02 +08:00
|
|
|
|
start_wc = ((start_elem->type == SB_CHAR || start_elem->type == COLL_SYM)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
? __btowc (start_ch) : start_elem->opr.wch);
|
2002-04-27 04:52:02 +08:00
|
|
|
|
end_wc = ((end_elem->type == SB_CHAR || end_elem->type == COLL_SYM)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
? __btowc (end_ch) : end_elem->opr.wch);
|
2002-04-27 04:52:02 +08:00
|
|
|
|
cmp_buf[0] = start_wc;
|
|
|
|
|
cmp_buf[4] = end_wc;
|
|
|
|
|
if (wcscoll (cmp_buf, cmp_buf + 4) > 0)
|
|
|
|
|
return REG_ERANGE;
|
|
|
|
|
|
|
|
|
|
/* Check the space of the arrays. */
|
|
|
|
|
if (*range_alloc == mbcset->nranges)
|
|
|
|
|
{
|
2002-11-07 04:36:47 +08:00
|
|
|
|
/* There are not enough space, need realloc. */
|
|
|
|
|
wchar_t *new_array_start, *new_array_end;
|
|
|
|
|
int new_nranges;
|
|
|
|
|
|
|
|
|
|
/* +1 in case of mbcset->nranges is 0. */
|
|
|
|
|
new_nranges = 2 * mbcset->nranges + 1;
|
|
|
|
|
/* Use realloc since mbcset->range_starts and mbcset->range_ends
|
|
|
|
|
are NULL if *range_alloc == 0. */
|
|
|
|
|
new_array_start = re_realloc (mbcset->range_starts, wchar_t,
|
|
|
|
|
new_nranges);
|
|
|
|
|
new_array_end = re_realloc (mbcset->range_ends, wchar_t,
|
|
|
|
|
new_nranges);
|
|
|
|
|
|
|
|
|
|
if (BE (new_array_start == NULL || new_array_end == NULL, 0))
|
|
|
|
|
return REG_ESPACE;
|
|
|
|
|
|
|
|
|
|
mbcset->range_starts = new_array_start;
|
|
|
|
|
mbcset->range_ends = new_array_end;
|
|
|
|
|
*range_alloc = new_nranges;
|
2002-04-27 04:52:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mbcset->range_starts[mbcset->nranges] = start_wc;
|
|
|
|
|
mbcset->range_ends[mbcset->nranges++] = end_wc;
|
|
|
|
|
|
|
|
|
|
/* Build the table for single byte characters. */
|
|
|
|
|
for (wc = 0; wc <= SBC_MAX; ++wc)
|
|
|
|
|
{
|
2002-11-07 04:36:47 +08:00
|
|
|
|
cmp_buf[2] = wc;
|
|
|
|
|
if (wcscoll (cmp_buf, cmp_buf + 2) <= 0
|
|
|
|
|
&& wcscoll (cmp_buf + 2, cmp_buf + 4) <= 0)
|
|
|
|
|
bitset_set (sbcset, wc);
|
2002-04-27 04:52:02 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
# else /* not RE_ENABLE_I18N */
|
|
|
|
|
{
|
|
|
|
|
unsigned int ch;
|
|
|
|
|
start_ch = ((start_elem->type == SB_CHAR ) ? start_elem->opr.ch
|
2002-11-07 04:36:47 +08:00
|
|
|
|
: ((start_elem->type == COLL_SYM) ? start_elem->opr.name[0]
|
|
|
|
|
: 0));
|
2002-04-27 04:52:02 +08:00
|
|
|
|
end_ch = ((end_elem->type == SB_CHAR ) ? end_elem->opr.ch
|
2002-11-07 04:36:47 +08:00
|
|
|
|
: ((end_elem->type == COLL_SYM) ? end_elem->opr.name[0]
|
|
|
|
|
: 0));
|
2002-04-27 04:52:02 +08:00
|
|
|
|
if (start_ch > end_ch)
|
|
|
|
|
return REG_ERANGE;
|
|
|
|
|
/* Build the table for single byte characters. */
|
|
|
|
|
for (ch = 0; ch <= SBC_MAX; ++ch)
|
|
|
|
|
if (start_ch <= ch && ch <= end_ch)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
bitset_set (sbcset, ch);
|
2002-04-27 04:52:02 +08:00
|
|
|
|
}
|
|
|
|
|
# endif /* not RE_ENABLE_I18N */
|
|
|
|
|
return REG_NOERROR;
|
|
|
|
|
}
|
|
|
|
|
#endif /* not _LIBC */
|
|
|
|
|
|
|
|
|
|
#ifndef _LIBC
|
|
|
|
|
/* Helper function for parse_bracket_exp only used in case of NOT _LIBC..
|
|
|
|
|
Build the collating element which is represented by NAME.
|
|
|
|
|
The result are written to MBCSET and SBCSET.
|
|
|
|
|
COLL_SYM_ALLOC is the allocated size of mbcset->coll_sym, is a
|
|
|
|
|
pointer argument since we may update it. */
|
|
|
|
|
|
|
|
|
|
static reg_errcode_t
|
2002-05-24 16:49:00 +08:00
|
|
|
|
# ifdef RE_ENABLE_I18N
|
|
|
|
|
build_collating_symbol (sbcset, mbcset, coll_sym_alloc, name)
|
2002-04-27 04:52:02 +08:00
|
|
|
|
re_charset_t *mbcset;
|
|
|
|
|
int *coll_sym_alloc;
|
2002-05-24 16:49:00 +08:00
|
|
|
|
# else /* not RE_ENABLE_I18N */
|
|
|
|
|
build_collating_symbol (sbcset, name)
|
|
|
|
|
# endif /* not RE_ENABLE_I18N */
|
|
|
|
|
re_bitset_ptr_t sbcset;
|
2002-09-11 01:39:58 +08:00
|
|
|
|
const unsigned char *name;
|
2002-04-27 04:52:02 +08:00
|
|
|
|
{
|
2002-09-11 01:39:58 +08:00
|
|
|
|
size_t name_len = strlen ((const char *) name);
|
|
|
|
|
if (BE (name_len != 1, 0))
|
2002-04-27 04:52:02 +08:00
|
|
|
|
return REG_ECOLLATE;
|
|
|
|
|
else
|
|
|
|
|
{
|
2002-09-11 01:39:58 +08:00
|
|
|
|
bitset_set (sbcset, name[0]);
|
2002-04-27 04:52:02 +08:00
|
|
|
|
return REG_NOERROR;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif /* not _LIBC */
|
|
|
|
|
|
2002-02-27 03:06:03 +08:00
|
|
|
|
/* This function parse bracket expression like "[abc]", "[a-c]",
|
|
|
|
|
"[[.a-a.]]" etc. */
|
|
|
|
|
|
|
|
|
|
static bin_tree_t *
|
|
|
|
|
parse_bracket_exp (regexp, dfa, token, syntax, err)
|
|
|
|
|
re_string_t *regexp;
|
|
|
|
|
re_dfa_t *dfa;
|
|
|
|
|
re_token_t *token;
|
|
|
|
|
reg_syntax_t syntax;
|
|
|
|
|
reg_errcode_t *err;
|
|
|
|
|
{
|
|
|
|
|
#ifdef _LIBC
|
2002-09-11 01:39:58 +08:00
|
|
|
|
const unsigned char *collseqmb;
|
|
|
|
|
const char *collseqwc;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
uint32_t nrules;
|
|
|
|
|
int32_t table_size;
|
|
|
|
|
const int32_t *symb_table;
|
|
|
|
|
const unsigned char *extra;
|
|
|
|
|
|
2002-04-27 04:52:02 +08:00
|
|
|
|
/* Local function for parse_bracket_exp used in _LIBC environement.
|
2002-02-27 03:06:03 +08:00
|
|
|
|
Seek the collating symbol entry correspondings to NAME.
|
|
|
|
|
Return the index of the symbol in the SYMB_TABLE. */
|
|
|
|
|
|
|
|
|
|
static inline int32_t
|
2003-06-14 05:05:42 +08:00
|
|
|
|
__attribute ((always_inline))
|
2002-02-27 03:06:03 +08:00
|
|
|
|
seek_collating_symbol_entry (name, name_len)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
const unsigned char *name;
|
|
|
|
|
size_t name_len;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
{
|
2002-09-11 01:39:58 +08:00
|
|
|
|
int32_t hash = elem_hash ((const char *) name, name_len);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
int32_t elem = hash % table_size;
|
|
|
|
|
int32_t second = hash % (table_size - 2);
|
|
|
|
|
while (symb_table[2 * elem] != 0)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
/* First compare the hashing value. */
|
|
|
|
|
if (symb_table[2 * elem] == hash
|
|
|
|
|
/* Compare the length of the name. */
|
|
|
|
|
&& name_len == extra[symb_table[2 * elem + 1]]
|
|
|
|
|
/* Compare the name. */
|
|
|
|
|
&& memcmp (name, &extra[symb_table[2 * elem + 1] + 1],
|
|
|
|
|
name_len) == 0)
|
|
|
|
|
{
|
|
|
|
|
/* Yep, this is the entry. */
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Next entry. */
|
|
|
|
|
elem += second;
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
return elem;
|
|
|
|
|
}
|
|
|
|
|
|
2002-04-27 04:52:02 +08:00
|
|
|
|
/* Local function for parse_bracket_exp used in _LIBC environement.
|
2002-02-27 03:06:03 +08:00
|
|
|
|
Look up the collation sequence value of BR_ELEM.
|
|
|
|
|
Return the value if succeeded, UINT_MAX otherwise. */
|
|
|
|
|
|
|
|
|
|
static inline unsigned int
|
2003-06-14 05:05:42 +08:00
|
|
|
|
__attribute ((always_inline))
|
2002-02-27 03:06:03 +08:00
|
|
|
|
lookup_collation_sequence_value (br_elem)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
bracket_elem_t *br_elem;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
{
|
|
|
|
|
if (br_elem->type == SB_CHAR)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
if (MB_CUR_MAX == 1)
|
|
|
|
|
*/
|
|
|
|
|
if (nrules == 0)
|
|
|
|
|
return collseqmb[br_elem->opr.ch];
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
wint_t wc = __btowc (br_elem->opr.ch);
|
2003-06-14 05:05:42 +08:00
|
|
|
|
return __collseq_table_lookup (collseqwc, wc);
|
2002-11-07 04:36:47 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
else if (br_elem->type == MB_CHAR)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
2003-06-14 05:05:42 +08:00
|
|
|
|
return __collseq_table_lookup (collseqwc, br_elem->opr.wch);
|
2002-11-07 04:36:47 +08:00
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
else if (br_elem->type == COLL_SYM)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
size_t sym_name_len = strlen ((char *) br_elem->opr.name);
|
|
|
|
|
if (nrules != 0)
|
|
|
|
|
{
|
|
|
|
|
int32_t elem, idx;
|
|
|
|
|
elem = seek_collating_symbol_entry (br_elem->opr.name,
|
|
|
|
|
sym_name_len);
|
|
|
|
|
if (symb_table[2 * elem] != 0)
|
|
|
|
|
{
|
|
|
|
|
/* We found the entry. */
|
|
|
|
|
idx = symb_table[2 * elem + 1];
|
|
|
|
|
/* Skip the name of collating element name. */
|
|
|
|
|
idx += 1 + extra[idx];
|
|
|
|
|
/* Skip the byte sequence of the collating element. */
|
|
|
|
|
idx += 1 + extra[idx];
|
|
|
|
|
/* Adjust for the alignment. */
|
|
|
|
|
idx = (idx + 3) & ~3;
|
|
|
|
|
/* Skip the multibyte collation sequence value. */
|
|
|
|
|
idx += sizeof (unsigned int);
|
|
|
|
|
/* Skip the wide char sequence of the collating element. */
|
|
|
|
|
idx += sizeof (unsigned int) *
|
|
|
|
|
(1 + *(unsigned int *) (extra + idx));
|
|
|
|
|
/* Return the collation sequence value. */
|
|
|
|
|
return *(unsigned int *) (extra + idx);
|
|
|
|
|
}
|
|
|
|
|
else if (symb_table[2 * elem] == 0 && sym_name_len == 1)
|
|
|
|
|
{
|
|
|
|
|
/* No valid character. Match it as a single byte
|
|
|
|
|
character. */
|
|
|
|
|
return collseqmb[br_elem->opr.name[0]];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (sym_name_len == 1)
|
|
|
|
|
return collseqmb[br_elem->opr.name[0]];
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
return UINT_MAX;
|
|
|
|
|
}
|
|
|
|
|
|
2002-04-27 04:52:02 +08:00
|
|
|
|
/* Local function for parse_bracket_exp used in _LIBC environement.
|
2002-02-27 03:06:03 +08:00
|
|
|
|
Build the range expression which starts from START_ELEM, and ends
|
|
|
|
|
at END_ELEM. The result are written to MBCSET and SBCSET.
|
|
|
|
|
RANGE_ALLOC is the allocated size of mbcset->range_starts, and
|
|
|
|
|
mbcset->range_ends, is a pointer argument sinse we may
|
|
|
|
|
update it. */
|
|
|
|
|
|
|
|
|
|
static inline reg_errcode_t
|
2003-06-14 05:05:42 +08:00
|
|
|
|
__attribute ((always_inline))
|
2002-05-24 16:49:00 +08:00
|
|
|
|
# ifdef RE_ENABLE_I18N
|
|
|
|
|
build_range_exp (sbcset, mbcset, range_alloc, start_elem, end_elem)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
re_charset_t *mbcset;
|
|
|
|
|
int *range_alloc;
|
2002-05-24 16:49:00 +08:00
|
|
|
|
# else /* not RE_ENABLE_I18N */
|
|
|
|
|
build_range_exp (sbcset, start_elem, end_elem)
|
|
|
|
|
# endif /* not RE_ENABLE_I18N */
|
2002-11-07 04:36:47 +08:00
|
|
|
|
re_bitset_ptr_t sbcset;
|
|
|
|
|
bracket_elem_t *start_elem, *end_elem;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
{
|
|
|
|
|
unsigned int ch;
|
|
|
|
|
uint32_t start_collseq;
|
|
|
|
|
uint32_t end_collseq;
|
|
|
|
|
|
2002-05-24 16:49:00 +08:00
|
|
|
|
# ifdef RE_ENABLE_I18N
|
2002-02-27 03:06:03 +08:00
|
|
|
|
/* Check the space of the arrays. */
|
|
|
|
|
if (*range_alloc == mbcset->nranges)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
/* There are not enough space, need realloc. */
|
|
|
|
|
uint32_t *new_array_start;
|
|
|
|
|
uint32_t *new_array_end;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
int new_nranges;
|
|
|
|
|
|
2002-11-07 04:36:47 +08:00
|
|
|
|
/* +1 in case of mbcset->nranges is 0. */
|
|
|
|
|
new_nranges = 2 * mbcset->nranges + 1;
|
2002-02-28 15:43:13 +08:00
|
|
|
|
/* Use realloc since mbcset->range_starts and mbcset->range_ends
|
2002-11-07 04:36:47 +08:00
|
|
|
|
are NULL if *range_alloc == 0. */
|
|
|
|
|
new_array_start = re_realloc (mbcset->range_starts, uint32_t,
|
|
|
|
|
new_nranges);
|
|
|
|
|
new_array_end = re_realloc (mbcset->range_ends, uint32_t,
|
|
|
|
|
new_nranges);
|
2002-02-28 15:43:13 +08:00
|
|
|
|
|
2002-11-07 04:36:47 +08:00
|
|
|
|
if (BE (new_array_start == NULL || new_array_end == NULL, 0))
|
|
|
|
|
return REG_ESPACE;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
|
2002-11-07 04:36:47 +08:00
|
|
|
|
mbcset->range_starts = new_array_start;
|
|
|
|
|
mbcset->range_ends = new_array_end;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
*range_alloc = new_nranges;
|
2002-11-07 04:36:47 +08:00
|
|
|
|
}
|
2002-05-24 16:49:00 +08:00
|
|
|
|
# endif /* RE_ENABLE_I18N */
|
2002-02-27 03:06:03 +08:00
|
|
|
|
|
2002-04-27 04:52:02 +08:00
|
|
|
|
/* Equivalence Classes and Character Classes can't be a range
|
2002-11-07 04:36:47 +08:00
|
|
|
|
start/end. */
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (start_elem->type == EQUIV_CLASS || start_elem->type == CHAR_CLASS
|
2002-11-07 04:36:47 +08:00
|
|
|
|
|| end_elem->type == EQUIV_CLASS || end_elem->type == CHAR_CLASS,
|
|
|
|
|
0))
|
|
|
|
|
return REG_ERANGE;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
|
|
|
|
|
start_collseq = lookup_collation_sequence_value (start_elem);
|
|
|
|
|
end_collseq = lookup_collation_sequence_value (end_elem);
|
|
|
|
|
/* Check start/end collation sequence values. */
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (start_collseq == UINT_MAX || end_collseq == UINT_MAX, 0))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
return REG_ECOLLATE;
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE ((syntax & RE_NO_EMPTY_RANGES) && start_collseq > end_collseq, 0))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
return REG_ERANGE;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
|
2002-05-24 16:49:00 +08:00
|
|
|
|
# ifdef RE_ENABLE_I18N
|
2002-02-27 03:06:03 +08:00
|
|
|
|
/* Got valid collation sequence values, add them as a new entry. */
|
|
|
|
|
mbcset->range_starts[mbcset->nranges] = start_collseq;
|
|
|
|
|
mbcset->range_ends[mbcset->nranges++] = end_collseq;
|
2002-05-24 16:49:00 +08:00
|
|
|
|
# endif /* RE_ENABLE_I18N */
|
2002-02-27 03:06:03 +08:00
|
|
|
|
|
|
|
|
|
/* Build the table for single byte characters. */
|
|
|
|
|
for (ch = 0; ch <= SBC_MAX; ch++)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
uint32_t ch_collseq;
|
|
|
|
|
/*
|
|
|
|
|
if (MB_CUR_MAX == 1)
|
|
|
|
|
*/
|
|
|
|
|
if (nrules == 0)
|
|
|
|
|
ch_collseq = collseqmb[ch];
|
|
|
|
|
else
|
2003-06-14 05:05:42 +08:00
|
|
|
|
ch_collseq = __collseq_table_lookup (collseqwc, __btowc (ch));
|
2002-11-07 04:36:47 +08:00
|
|
|
|
if (start_collseq <= ch_collseq && ch_collseq <= end_collseq)
|
|
|
|
|
bitset_set (sbcset, ch);
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
return REG_NOERROR;
|
|
|
|
|
}
|
|
|
|
|
|
2002-04-27 04:52:02 +08:00
|
|
|
|
/* Local function for parse_bracket_exp used in _LIBC environement.
|
2002-02-27 03:06:03 +08:00
|
|
|
|
Build the collating element which is represented by NAME.
|
|
|
|
|
The result are written to MBCSET and SBCSET.
|
|
|
|
|
COLL_SYM_ALLOC is the allocated size of mbcset->coll_sym, is a
|
|
|
|
|
pointer argument sinse we may update it. */
|
|
|
|
|
|
|
|
|
|
static inline reg_errcode_t
|
2003-06-14 05:05:42 +08:00
|
|
|
|
__attribute ((always_inline))
|
2002-05-24 16:49:00 +08:00
|
|
|
|
# ifdef RE_ENABLE_I18N
|
|
|
|
|
build_collating_symbol (sbcset, mbcset, coll_sym_alloc, name)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
re_charset_t *mbcset;
|
|
|
|
|
int *coll_sym_alloc;
|
2002-05-24 16:49:00 +08:00
|
|
|
|
# else /* not RE_ENABLE_I18N */
|
|
|
|
|
build_collating_symbol (sbcset, name)
|
|
|
|
|
# endif /* not RE_ENABLE_I18N */
|
2002-11-07 04:36:47 +08:00
|
|
|
|
re_bitset_ptr_t sbcset;
|
|
|
|
|
const unsigned char *name;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
{
|
|
|
|
|
int32_t elem, idx;
|
2002-09-11 01:39:58 +08:00
|
|
|
|
size_t name_len = strlen ((const char *) name);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
if (nrules != 0)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
elem = seek_collating_symbol_entry (name, name_len);
|
|
|
|
|
if (symb_table[2 * elem] != 0)
|
|
|
|
|
{
|
|
|
|
|
/* We found the entry. */
|
|
|
|
|
idx = symb_table[2 * elem + 1];
|
|
|
|
|
/* Skip the name of collating element name. */
|
|
|
|
|
idx += 1 + extra[idx];
|
|
|
|
|
}
|
|
|
|
|
else if (symb_table[2 * elem] == 0 && name_len == 1)
|
|
|
|
|
{
|
|
|
|
|
/* No valid character, treat it as a normal
|
|
|
|
|
character. */
|
|
|
|
|
bitset_set (sbcset, name[0]);
|
|
|
|
|
return REG_NOERROR;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return REG_ECOLLATE;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
|
2002-05-24 16:49:00 +08:00
|
|
|
|
# ifdef RE_ENABLE_I18N
|
2002-11-07 04:36:47 +08:00
|
|
|
|
/* Got valid collation sequence, add it as a new entry. */
|
|
|
|
|
/* Check the space of the arrays. */
|
|
|
|
|
if (*coll_sym_alloc == mbcset->ncoll_syms)
|
|
|
|
|
{
|
|
|
|
|
/* Not enough, realloc it. */
|
|
|
|
|
/* +1 in case of mbcset->ncoll_syms is 0. */
|
|
|
|
|
*coll_sym_alloc = 2 * mbcset->ncoll_syms + 1;
|
|
|
|
|
/* Use realloc since mbcset->coll_syms is NULL
|
|
|
|
|
if *alloc == 0. */
|
|
|
|
|
mbcset->coll_syms = re_realloc (mbcset->coll_syms, int32_t,
|
|
|
|
|
*coll_sym_alloc);
|
|
|
|
|
if (BE (mbcset->coll_syms == NULL, 0))
|
|
|
|
|
return REG_ESPACE;
|
|
|
|
|
}
|
|
|
|
|
mbcset->coll_syms[mbcset->ncoll_syms++] = idx;
|
2002-05-24 16:49:00 +08:00
|
|
|
|
# endif /* RE_ENABLE_I18N */
|
2002-11-07 04:36:47 +08:00
|
|
|
|
return REG_NOERROR;
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
else
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
if (BE (name_len != 1, 0))
|
|
|
|
|
return REG_ECOLLATE;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
bitset_set (sbcset, name[0]);
|
|
|
|
|
return REG_NOERROR;
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
2002-04-27 04:52:02 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
2002-02-27 03:06:03 +08:00
|
|
|
|
re_token_t br_token;
|
|
|
|
|
re_bitset_ptr_t sbcset;
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#ifdef RE_ENABLE_I18N
|
2002-02-27 03:06:03 +08:00
|
|
|
|
re_charset_t *mbcset;
|
|
|
|
|
int coll_sym_alloc = 0, range_alloc = 0, mbchar_alloc = 0;
|
|
|
|
|
int equiv_class_alloc = 0, char_class_alloc = 0;
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#else /* not RE_ENABLE_I18N */
|
|
|
|
|
int non_match = 0;
|
|
|
|
|
#endif /* not RE_ENABLE_I18N */
|
|
|
|
|
bin_tree_t *work_tree;
|
|
|
|
|
int token_len, new_idx;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
#ifdef _LIBC
|
2002-09-11 01:39:58 +08:00
|
|
|
|
collseqmb = (const unsigned char *)
|
|
|
|
|
_NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQMB);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
|
|
|
|
|
if (nrules)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
if (MB_CUR_MAX > 1)
|
|
|
|
|
*/
|
2002-11-07 04:36:47 +08:00
|
|
|
|
collseqwc = _NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQWC);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
table_size = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_SYMB_HASH_SIZEMB);
|
|
|
|
|
symb_table = (const int32_t *) _NL_CURRENT (LC_COLLATE,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
_NL_COLLATE_SYMB_TABLEMB);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
extra = (const unsigned char *) _NL_CURRENT (LC_COLLATE,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
_NL_COLLATE_SYMB_EXTRAMB);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
sbcset = (re_bitset_ptr_t) calloc (sizeof (unsigned int), BITSET_UINTS);
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#ifdef RE_ENABLE_I18N
|
2002-02-27 03:06:03 +08:00
|
|
|
|
mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1);
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#endif /* RE_ENABLE_I18N */
|
|
|
|
|
#ifdef RE_ENABLE_I18N
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (sbcset == NULL || mbcset == NULL, 0))
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#else
|
|
|
|
|
if (BE (sbcset == NULL, 0))
|
|
|
|
|
#endif /* RE_ENABLE_I18N */
|
2002-02-27 03:06:03 +08:00
|
|
|
|
{
|
|
|
|
|
*err = REG_ESPACE;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
token_len = peek_token_bracket (token, regexp, syntax);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (token->type == END_OF_RE, 0))
|
2002-02-27 03:06:03 +08:00
|
|
|
|
{
|
|
|
|
|
*err = REG_BADPAT;
|
2002-04-27 04:52:02 +08:00
|
|
|
|
goto parse_bracket_exp_free_return;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
if (token->type == OP_NON_MATCH_LIST)
|
|
|
|
|
{
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#ifdef RE_ENABLE_I18N
|
2002-02-27 03:06:03 +08:00
|
|
|
|
int i;
|
|
|
|
|
mbcset->non_match = 1;
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#else /* not RE_ENABLE_I18N */
|
|
|
|
|
non_match = 1;
|
|
|
|
|
#endif /* not RE_ENABLE_I18N */
|
2002-02-27 03:06:03 +08:00
|
|
|
|
if (syntax & RE_HAT_LISTS_NOT_NEWLINE)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
bitset_set (sbcset, '\0');
|
2002-02-27 03:06:03 +08:00
|
|
|
|
re_string_skip_bytes (regexp, token_len); /* Skip a token. */
|
|
|
|
|
token_len = peek_token_bracket (token, regexp, syntax);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (token->type == END_OF_RE, 0))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
*err = REG_BADPAT;
|
|
|
|
|
goto parse_bracket_exp_free_return;
|
|
|
|
|
}
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#ifdef RE_ENABLE_I18N
|
2002-02-27 03:06:03 +08:00
|
|
|
|
if (MB_CUR_MAX > 1)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
for (i = 0; i < SBC_MAX; ++i)
|
|
|
|
|
if (__btowc (i) == WEOF)
|
|
|
|
|
bitset_set (sbcset, i);
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#endif /* RE_ENABLE_I18N */
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* We treat the first ']' as a normal character. */
|
|
|
|
|
if (token->type == OP_CLOSE_BRACKET)
|
|
|
|
|
token->type = CHARACTER;
|
|
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
|
{
|
|
|
|
|
bracket_elem_t start_elem, end_elem;
|
2002-09-11 01:39:58 +08:00
|
|
|
|
unsigned char start_name_buf[BRACKET_NAME_BUF_SIZE];
|
|
|
|
|
unsigned char end_name_buf[BRACKET_NAME_BUF_SIZE];
|
2002-02-27 03:06:03 +08:00
|
|
|
|
reg_errcode_t ret;
|
|
|
|
|
int token_len2 = 0, is_range_exp = 0;
|
|
|
|
|
re_token_t token2;
|
|
|
|
|
|
|
|
|
|
start_elem.opr.name = start_name_buf;
|
|
|
|
|
ret = parse_bracket_element (&start_elem, regexp, token, token_len, dfa,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
syntax);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (ret != REG_NOERROR, 0))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
*err = ret;
|
|
|
|
|
goto parse_bracket_exp_free_return;
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
|
|
|
|
|
token_len = peek_token_bracket (token, regexp, syntax);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (token->type == END_OF_RE, 0))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
*err = REG_BADPAT;
|
|
|
|
|
goto parse_bracket_exp_free_return;
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
if (token->type == OP_CHARSET_RANGE)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
re_string_skip_bytes (regexp, token_len); /* Skip '-'. */
|
|
|
|
|
token_len2 = peek_token_bracket (&token2, regexp, syntax);
|
|
|
|
|
if (BE (token->type == END_OF_RE, 0))
|
|
|
|
|
{
|
|
|
|
|
*err = REG_BADPAT;
|
|
|
|
|
goto parse_bracket_exp_free_return;
|
|
|
|
|
}
|
|
|
|
|
if (token2.type == OP_CLOSE_BRACKET)
|
|
|
|
|
{
|
|
|
|
|
/* We treat the last '-' as a normal character. */
|
|
|
|
|
re_string_skip_bytes (regexp, -token_len);
|
|
|
|
|
token->type = CHARACTER;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
is_range_exp = 1;
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
|
|
|
|
|
if (is_range_exp == 1)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
end_elem.opr.name = end_name_buf;
|
|
|
|
|
ret = parse_bracket_element (&end_elem, regexp, &token2, token_len2,
|
|
|
|
|
dfa, syntax);
|
|
|
|
|
if (BE (ret != REG_NOERROR, 0))
|
|
|
|
|
{
|
|
|
|
|
*err = ret;
|
|
|
|
|
goto parse_bracket_exp_free_return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
token_len = peek_token_bracket (token, regexp, syntax);
|
|
|
|
|
if (BE (token->type == END_OF_RE, 0))
|
|
|
|
|
{
|
|
|
|
|
*err = REG_BADPAT;
|
|
|
|
|
goto parse_bracket_exp_free_return;
|
|
|
|
|
}
|
|
|
|
|
*err = build_range_exp (sbcset,
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#ifdef RE_ENABLE_I18N
|
2002-11-07 04:36:47 +08:00
|
|
|
|
mbcset, &range_alloc,
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#endif /* RE_ENABLE_I18N */
|
2002-11-07 04:36:47 +08:00
|
|
|
|
&start_elem, &end_elem);
|
|
|
|
|
if (BE (*err != REG_NOERROR, 0))
|
|
|
|
|
goto parse_bracket_exp_free_return;
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
else
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
switch (start_elem.type)
|
|
|
|
|
{
|
|
|
|
|
case SB_CHAR:
|
|
|
|
|
bitset_set (sbcset, start_elem.opr.ch);
|
|
|
|
|
break;
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#ifdef RE_ENABLE_I18N
|
2002-11-07 04:36:47 +08:00
|
|
|
|
case MB_CHAR:
|
|
|
|
|
/* Check whether the array has enough space. */
|
|
|
|
|
if (mbchar_alloc == mbcset->nmbchars)
|
|
|
|
|
{
|
|
|
|
|
/* Not enough, realloc it. */
|
|
|
|
|
/* +1 in case of mbcset->nmbchars is 0. */
|
|
|
|
|
mbchar_alloc = 2 * mbcset->nmbchars + 1;
|
|
|
|
|
/* Use realloc since array is NULL if *alloc == 0. */
|
|
|
|
|
mbcset->mbchars = re_realloc (mbcset->mbchars, wchar_t,
|
|
|
|
|
mbchar_alloc);
|
|
|
|
|
if (BE (mbcset->mbchars == NULL, 0))
|
|
|
|
|
goto parse_bracket_exp_espace;
|
|
|
|
|
}
|
|
|
|
|
mbcset->mbchars[mbcset->nmbchars++] = start_elem.opr.wch;
|
|
|
|
|
break;
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#endif /* RE_ENABLE_I18N */
|
2002-11-07 04:36:47 +08:00
|
|
|
|
case EQUIV_CLASS:
|
|
|
|
|
*err = build_equiv_class (sbcset,
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#ifdef RE_ENABLE_I18N
|
2002-11-07 04:36:47 +08:00
|
|
|
|
mbcset, &equiv_class_alloc,
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#endif /* RE_ENABLE_I18N */
|
2002-02-27 03:06:03 +08:00
|
|
|
|
start_elem.opr.name);
|
2002-11-07 04:36:47 +08:00
|
|
|
|
if (BE (*err != REG_NOERROR, 0))
|
|
|
|
|
goto parse_bracket_exp_free_return;
|
|
|
|
|
break;
|
|
|
|
|
case COLL_SYM:
|
|
|
|
|
*err = build_collating_symbol (sbcset,
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#ifdef RE_ENABLE_I18N
|
2002-11-07 04:36:47 +08:00
|
|
|
|
mbcset, &coll_sym_alloc,
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#endif /* RE_ENABLE_I18N */
|
2002-02-27 03:06:03 +08:00
|
|
|
|
start_elem.opr.name);
|
2002-11-07 04:36:47 +08:00
|
|
|
|
if (BE (*err != REG_NOERROR, 0))
|
|
|
|
|
goto parse_bracket_exp_free_return;
|
|
|
|
|
break;
|
|
|
|
|
case CHAR_CLASS:
|
2003-06-13 02:29:52 +08:00
|
|
|
|
*err = build_charclass (regexp->trans, sbcset,
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#ifdef RE_ENABLE_I18N
|
2003-01-17 03:20:36 +08:00
|
|
|
|
mbcset, &char_class_alloc,
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#endif /* RE_ENABLE_I18N */
|
2003-01-17 03:20:36 +08:00
|
|
|
|
start_elem.opr.name, syntax);
|
|
|
|
|
if (BE (*err != REG_NOERROR, 0))
|
|
|
|
|
goto parse_bracket_exp_free_return;
|
2002-11-07 04:36:47 +08:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
assert (0);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
if (token->type == OP_CLOSE_BRACKET)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
break;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
re_string_skip_bytes (regexp, token_len); /* Skip a token. */
|
|
|
|
|
|
|
|
|
|
/* If it is non-matching list. */
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#ifdef RE_ENABLE_I18N
|
2002-02-27 03:06:03 +08:00
|
|
|
|
if (mbcset->non_match)
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#else /* not RE_ENABLE_I18N */
|
|
|
|
|
if (non_match)
|
|
|
|
|
#endif /* not RE_ENABLE_I18N */
|
2002-02-27 03:06:03 +08:00
|
|
|
|
bitset_not (sbcset);
|
|
|
|
|
|
|
|
|
|
/* Build a tree for simple bracket. */
|
|
|
|
|
br_token.type = SIMPLE_BRACKET;
|
|
|
|
|
br_token.opr.sbcset = sbcset;
|
|
|
|
|
new_idx = re_dfa_add_node (dfa, br_token, 0);
|
|
|
|
|
work_tree = create_tree (NULL, NULL, 0, new_idx);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (new_idx == -1 || work_tree == NULL, 0))
|
2002-02-27 03:06:03 +08:00
|
|
|
|
goto parse_bracket_exp_espace;
|
|
|
|
|
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#ifdef RE_ENABLE_I18N
|
2002-02-27 03:06:03 +08:00
|
|
|
|
if (mbcset->nmbchars || mbcset->ncoll_syms || mbcset->nequiv_classes
|
2002-04-27 04:52:02 +08:00
|
|
|
|
|| mbcset->nranges || (MB_CUR_MAX > 1 && (mbcset->nchar_classes
|
2002-11-07 04:36:47 +08:00
|
|
|
|
|| mbcset->non_match)))
|
2002-02-27 03:06:03 +08:00
|
|
|
|
{
|
|
|
|
|
re_token_t alt_token;
|
|
|
|
|
bin_tree_t *mbc_tree;
|
|
|
|
|
/* Build a tree for complex bracket. */
|
|
|
|
|
br_token.type = COMPLEX_BRACKET;
|
|
|
|
|
br_token.opr.mbcset = mbcset;
|
|
|
|
|
dfa->has_mb_node = 1;
|
|
|
|
|
new_idx = re_dfa_add_node (dfa, br_token, 0);
|
|
|
|
|
mbc_tree = create_tree (NULL, NULL, 0, new_idx);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (new_idx == -1 || mbc_tree == NULL, 0))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
goto parse_bracket_exp_espace;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
/* Then join them by ALT node. */
|
2002-09-28 13:28:44 +08:00
|
|
|
|
dfa->has_plural_match = 1;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
alt_token.type = OP_ALT;
|
|
|
|
|
new_idx = re_dfa_add_node (dfa, alt_token, 0);
|
|
|
|
|
work_tree = create_tree (work_tree, mbc_tree, 0, new_idx);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (new_idx != -1 && mbc_tree != NULL, 1))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
return work_tree;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
free_charset (mbcset);
|
|
|
|
|
return work_tree;
|
|
|
|
|
}
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#else /* not RE_ENABLE_I18N */
|
|
|
|
|
return work_tree;
|
|
|
|
|
#endif /* not RE_ENABLE_I18N */
|
2002-02-27 03:06:03 +08:00
|
|
|
|
|
|
|
|
|
parse_bracket_exp_espace:
|
|
|
|
|
*err = REG_ESPACE;
|
2002-04-27 04:52:02 +08:00
|
|
|
|
parse_bracket_exp_free_return:
|
|
|
|
|
re_free (sbcset);
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#ifdef RE_ENABLE_I18N
|
2002-04-27 04:52:02 +08:00
|
|
|
|
free_charset (mbcset);
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#endif /* RE_ENABLE_I18N */
|
2002-02-27 03:06:03 +08:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2002-04-27 04:52:02 +08:00
|
|
|
|
/* Parse an element in the bracket expression. */
|
|
|
|
|
|
2002-02-27 03:06:03 +08:00
|
|
|
|
static reg_errcode_t
|
|
|
|
|
parse_bracket_element (elem, regexp, token, token_len, dfa, syntax)
|
|
|
|
|
bracket_elem_t *elem;
|
|
|
|
|
re_string_t *regexp;
|
|
|
|
|
re_token_t *token;
|
|
|
|
|
int token_len;
|
|
|
|
|
re_dfa_t *dfa;
|
|
|
|
|
reg_syntax_t syntax;
|
|
|
|
|
{
|
|
|
|
|
#ifdef RE_ENABLE_I18N
|
|
|
|
|
int cur_char_size;
|
|
|
|
|
cur_char_size = re_string_char_size_at (regexp, re_string_cur_idx (regexp));
|
|
|
|
|
if (cur_char_size > 1)
|
|
|
|
|
{
|
|
|
|
|
elem->type = MB_CHAR;
|
|
|
|
|
elem->opr.wch = re_string_wchar_at (regexp, re_string_cur_idx (regexp));
|
|
|
|
|
re_string_skip_bytes (regexp, cur_char_size);
|
|
|
|
|
return REG_NOERROR;
|
|
|
|
|
}
|
|
|
|
|
#endif /* RE_ENABLE_I18N */
|
|
|
|
|
re_string_skip_bytes (regexp, token_len); /* Skip a token. */
|
|
|
|
|
if (token->type == OP_OPEN_COLL_ELEM || token->type == OP_OPEN_CHAR_CLASS
|
|
|
|
|
|| token->type == OP_OPEN_EQUIV_CLASS)
|
|
|
|
|
return parse_bracket_symbol (elem, regexp, token);
|
|
|
|
|
elem->type = SB_CHAR;
|
|
|
|
|
elem->opr.ch = token->opr.c;
|
|
|
|
|
return REG_NOERROR;
|
|
|
|
|
}
|
|
|
|
|
|
2002-04-27 04:52:02 +08:00
|
|
|
|
/* Parse a bracket symbol in the bracket expression. Bracket symbols are
|
|
|
|
|
such as [:<character_class>:], [.<collating_element>.], and
|
|
|
|
|
[=<equivalent_class>=]. */
|
|
|
|
|
|
2002-02-27 03:06:03 +08:00
|
|
|
|
static reg_errcode_t
|
|
|
|
|
parse_bracket_symbol (elem, regexp, token)
|
|
|
|
|
bracket_elem_t *elem;
|
|
|
|
|
re_string_t *regexp;
|
|
|
|
|
re_token_t *token;
|
|
|
|
|
{
|
|
|
|
|
unsigned char ch, delim = token->opr.c;
|
|
|
|
|
int i = 0;
|
2002-04-20 08:57:03 +08:00
|
|
|
|
for (;; ++i)
|
2002-02-27 03:06:03 +08:00
|
|
|
|
{
|
2002-04-20 08:57:03 +08:00
|
|
|
|
if (re_string_eoi(regexp) || i >= BRACKET_NAME_BUF_SIZE)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
return REG_EBRACK;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
if (token->type == OP_OPEN_CHAR_CLASS)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
ch = re_string_fetch_byte_case (regexp);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
else
|
2002-11-07 04:36:47 +08:00
|
|
|
|
ch = re_string_fetch_byte (regexp);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
if (ch == delim && re_string_peek_byte (regexp, 0) == ']')
|
2002-11-07 04:36:47 +08:00
|
|
|
|
break;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
elem->opr.name[i] = ch;
|
|
|
|
|
}
|
|
|
|
|
re_string_skip_bytes (regexp, 1);
|
|
|
|
|
elem->opr.name[i] = '\0';
|
|
|
|
|
switch (token->type)
|
|
|
|
|
{
|
|
|
|
|
case OP_OPEN_COLL_ELEM:
|
|
|
|
|
elem->type = COLL_SYM;
|
|
|
|
|
break;
|
|
|
|
|
case OP_OPEN_EQUIV_CLASS:
|
|
|
|
|
elem->type = EQUIV_CLASS;
|
|
|
|
|
break;
|
|
|
|
|
case OP_OPEN_CHAR_CLASS:
|
|
|
|
|
elem->type = CHAR_CLASS;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return REG_NOERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Helper function for parse_bracket_exp.
|
|
|
|
|
Build the equivalence class which is represented by NAME.
|
|
|
|
|
The result are written to MBCSET and SBCSET.
|
|
|
|
|
EQUIV_CLASS_ALLOC is the allocated size of mbcset->equiv_classes,
|
|
|
|
|
is a pointer argument sinse we may update it. */
|
|
|
|
|
|
|
|
|
|
static reg_errcode_t
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#ifdef RE_ENABLE_I18N
|
|
|
|
|
build_equiv_class (sbcset, mbcset, equiv_class_alloc, name)
|
2002-02-27 03:06:03 +08:00
|
|
|
|
re_charset_t *mbcset;
|
|
|
|
|
int *equiv_class_alloc;
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#else /* not RE_ENABLE_I18N */
|
|
|
|
|
build_equiv_class (sbcset, name)
|
|
|
|
|
#endif /* not RE_ENABLE_I18N */
|
|
|
|
|
re_bitset_ptr_t sbcset;
|
2002-09-11 01:39:58 +08:00
|
|
|
|
const unsigned char *name;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
{
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#if defined _LIBC && defined RE_ENABLE_I18N
|
2002-02-27 03:06:03 +08:00
|
|
|
|
uint32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
|
|
|
|
|
if (nrules != 0)
|
|
|
|
|
{
|
|
|
|
|
const int32_t *table, *indirect;
|
|
|
|
|
const unsigned char *weights, *extra, *cp;
|
2002-09-11 01:39:58 +08:00
|
|
|
|
unsigned char char_buf[2];
|
2002-02-27 03:06:03 +08:00
|
|
|
|
int32_t idx1, idx2;
|
|
|
|
|
unsigned int ch;
|
|
|
|
|
size_t len;
|
|
|
|
|
/* This #include defines a local function! */
|
|
|
|
|
# include <locale/weight.h>
|
|
|
|
|
/* Calculate the index for equivalence class. */
|
2002-09-11 01:39:58 +08:00
|
|
|
|
cp = name;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
table = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
|
|
|
|
|
weights = (const unsigned char *) _NL_CURRENT (LC_COLLATE,
|
|
|
|
|
_NL_COLLATE_WEIGHTMB);
|
|
|
|
|
extra = (const unsigned char *) _NL_CURRENT (LC_COLLATE,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
_NL_COLLATE_EXTRAMB);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
indirect = (const int32_t *) _NL_CURRENT (LC_COLLATE,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
_NL_COLLATE_INDIRECTMB);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
idx1 = findidx (&cp);
|
2002-09-11 01:39:58 +08:00
|
|
|
|
if (BE (idx1 == 0 || cp < name + strlen ((const char *) name), 0))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
/* This isn't a valid character. */
|
|
|
|
|
return REG_ECOLLATE;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
|
|
|
|
|
/* Build single byte matcing table for this equivalence class. */
|
2002-09-11 01:39:58 +08:00
|
|
|
|
char_buf[1] = (unsigned char) '\0';
|
2002-02-27 03:06:03 +08:00
|
|
|
|
len = weights[idx1];
|
|
|
|
|
for (ch = 0; ch < SBC_MAX; ++ch)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
char_buf[0] = ch;
|
|
|
|
|
cp = char_buf;
|
|
|
|
|
idx2 = findidx (&cp);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
/*
|
2002-11-07 04:36:47 +08:00
|
|
|
|
idx2 = table[ch];
|
2002-02-27 03:06:03 +08:00
|
|
|
|
*/
|
2002-11-07 04:36:47 +08:00
|
|
|
|
if (idx2 == 0)
|
|
|
|
|
/* This isn't a valid character. */
|
|
|
|
|
continue;
|
|
|
|
|
if (len == weights[idx2])
|
|
|
|
|
{
|
|
|
|
|
int cnt = 0;
|
|
|
|
|
while (cnt <= len &&
|
|
|
|
|
weights[idx1 + 1 + cnt] == weights[idx2 + 1 + cnt])
|
|
|
|
|
++cnt;
|
|
|
|
|
|
|
|
|
|
if (cnt > len)
|
|
|
|
|
bitset_set (sbcset, ch);
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-02-28 15:43:13 +08:00
|
|
|
|
/* Check whether the array has enough space. */
|
|
|
|
|
if (*equiv_class_alloc == mbcset->nequiv_classes)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
/* Not enough, realloc it. */
|
|
|
|
|
/* +1 in case of mbcset->nequiv_classes is 0. */
|
|
|
|
|
*equiv_class_alloc = 2 * mbcset->nequiv_classes + 1;
|
|
|
|
|
/* Use realloc since the array is NULL if *alloc == 0. */
|
|
|
|
|
mbcset->equiv_classes = re_realloc (mbcset->equiv_classes, int32_t,
|
|
|
|
|
*equiv_class_alloc);
|
|
|
|
|
if (BE (mbcset->equiv_classes == NULL, 0))
|
|
|
|
|
return REG_ESPACE;
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
mbcset->equiv_classes[mbcset->nequiv_classes++] = idx1;
|
|
|
|
|
}
|
|
|
|
|
else
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#endif /* _LIBC && RE_ENABLE_I18N */
|
2002-02-27 03:06:03 +08:00
|
|
|
|
{
|
2002-09-11 01:39:58 +08:00
|
|
|
|
if (BE (strlen ((const char *) name) != 1, 0))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
return REG_ECOLLATE;
|
2002-09-11 01:39:58 +08:00
|
|
|
|
bitset_set (sbcset, *name);
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
return REG_NOERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Helper function for parse_bracket_exp.
|
|
|
|
|
Build the character class which is represented by NAME.
|
|
|
|
|
The result are written to MBCSET and SBCSET.
|
|
|
|
|
CHAR_CLASS_ALLOC is the allocated size of mbcset->char_classes,
|
|
|
|
|
is a pointer argument sinse we may update it. */
|
|
|
|
|
|
|
|
|
|
static reg_errcode_t
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#ifdef RE_ENABLE_I18N
|
2003-06-13 02:29:52 +08:00
|
|
|
|
build_charclass (trans, sbcset, mbcset, char_class_alloc, class_name, syntax)
|
2002-02-27 03:06:03 +08:00
|
|
|
|
re_charset_t *mbcset;
|
|
|
|
|
int *char_class_alloc;
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#else /* not RE_ENABLE_I18N */
|
2003-06-13 02:29:52 +08:00
|
|
|
|
build_charclass (trans, sbcset, class_name, syntax)
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#endif /* not RE_ENABLE_I18N */
|
2003-06-13 02:29:52 +08:00
|
|
|
|
RE_TRANSLATE_TYPE trans;
|
2002-05-24 16:49:00 +08:00
|
|
|
|
re_bitset_ptr_t sbcset;
|
2002-09-11 01:39:58 +08:00
|
|
|
|
const unsigned char *class_name;
|
2002-04-20 08:57:03 +08:00
|
|
|
|
reg_syntax_t syntax;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
{
|
|
|
|
|
int i;
|
2002-09-11 01:39:58 +08:00
|
|
|
|
const char *name = (const char *) class_name;
|
2002-05-24 16:49:00 +08:00
|
|
|
|
|
|
|
|
|
/* In case of REG_ICASE "upper" and "lower" match the both of
|
|
|
|
|
upper and lower cases. */
|
|
|
|
|
if ((syntax & RE_ICASE)
|
|
|
|
|
&& (strcmp (name, "upper") == 0 || strcmp (name, "lower") == 0))
|
|
|
|
|
name = "alpha";
|
|
|
|
|
|
|
|
|
|
#ifdef RE_ENABLE_I18N
|
2002-02-27 03:06:03 +08:00
|
|
|
|
/* Check the space of the arrays. */
|
2002-02-28 15:43:13 +08:00
|
|
|
|
if (*char_class_alloc == mbcset->nchar_classes)
|
|
|
|
|
{
|
|
|
|
|
/* Not enough, realloc it. */
|
|
|
|
|
/* +1 in case of mbcset->nchar_classes is 0. */
|
|
|
|
|
*char_class_alloc = 2 * mbcset->nchar_classes + 1;
|
|
|
|
|
/* Use realloc since array is NULL if *alloc == 0. */
|
|
|
|
|
mbcset->char_classes = re_realloc (mbcset->char_classes, wctype_t,
|
2002-11-07 04:36:47 +08:00
|
|
|
|
*char_class_alloc);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (mbcset->char_classes == NULL, 0))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
return REG_ESPACE;
|
2002-02-28 15:43:13 +08:00
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
mbcset->char_classes[mbcset->nchar_classes++] = __wctype (name);
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#endif /* RE_ENABLE_I18N */
|
2002-02-27 03:06:03 +08:00
|
|
|
|
|
2003-06-13 02:29:52 +08:00
|
|
|
|
#define BUILD_CHARCLASS_LOOP(ctype_func) \
|
|
|
|
|
for (i = 0; i < SBC_MAX; ++i) \
|
|
|
|
|
{ \
|
|
|
|
|
if (ctype_func (i)) \
|
|
|
|
|
{ \
|
|
|
|
|
int ch = trans ? trans[i] : i; \
|
|
|
|
|
bitset_set (sbcset, ch); \
|
|
|
|
|
} \
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (strcmp (name, "alnum") == 0)
|
|
|
|
|
BUILD_CHARCLASS_LOOP (isalnum)
|
|
|
|
|
else if (strcmp (name, "cntrl") == 0)
|
|
|
|
|
BUILD_CHARCLASS_LOOP (iscntrl)
|
|
|
|
|
else if (strcmp (name, "lower") == 0)
|
|
|
|
|
BUILD_CHARCLASS_LOOP (islower)
|
|
|
|
|
else if (strcmp (name, "space") == 0)
|
|
|
|
|
BUILD_CHARCLASS_LOOP (isspace)
|
|
|
|
|
else if (strcmp (name, "alpha") == 0)
|
|
|
|
|
BUILD_CHARCLASS_LOOP (isalpha)
|
|
|
|
|
else if (strcmp (name, "digit") == 0)
|
|
|
|
|
BUILD_CHARCLASS_LOOP (isdigit)
|
|
|
|
|
else if (strcmp (name, "print") == 0)
|
|
|
|
|
BUILD_CHARCLASS_LOOP (isprint)
|
|
|
|
|
else if (strcmp (name, "upper") == 0)
|
|
|
|
|
BUILD_CHARCLASS_LOOP (isupper)
|
|
|
|
|
else if (strcmp (name, "blank") == 0)
|
|
|
|
|
BUILD_CHARCLASS_LOOP (isblank)
|
|
|
|
|
else if (strcmp (name, "graph") == 0)
|
|
|
|
|
BUILD_CHARCLASS_LOOP (isgraph)
|
|
|
|
|
else if (strcmp (name, "punct") == 0)
|
|
|
|
|
BUILD_CHARCLASS_LOOP (ispunct)
|
|
|
|
|
else if (strcmp (name, "xdigit") == 0)
|
|
|
|
|
BUILD_CHARCLASS_LOOP (isxdigit)
|
|
|
|
|
else
|
|
|
|
|
return REG_ECTYPE;
|
|
|
|
|
|
|
|
|
|
return REG_NOERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bin_tree_t *
|
2003-06-13 02:29:52 +08:00
|
|
|
|
build_word_op (dfa, trans, not, err)
|
2002-02-27 03:06:03 +08:00
|
|
|
|
re_dfa_t *dfa;
|
2003-06-13 02:29:52 +08:00
|
|
|
|
RE_TRANSLATE_TYPE trans;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
int not;
|
|
|
|
|
reg_errcode_t *err;
|
|
|
|
|
{
|
|
|
|
|
re_bitset_ptr_t sbcset;
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#ifdef RE_ENABLE_I18N
|
2002-02-27 03:06:03 +08:00
|
|
|
|
re_charset_t *mbcset;
|
2002-05-24 16:49:00 +08:00
|
|
|
|
int alloc = 0;
|
|
|
|
|
#else /* not RE_ENABLE_I18N */
|
|
|
|
|
int non_match = 0;
|
|
|
|
|
#endif /* not RE_ENABLE_I18N */
|
2002-02-27 03:06:03 +08:00
|
|
|
|
reg_errcode_t ret;
|
|
|
|
|
re_token_t br_token;
|
|
|
|
|
bin_tree_t *tree;
|
2002-05-24 16:49:00 +08:00
|
|
|
|
int new_idx;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
|
|
|
|
|
sbcset = (re_bitset_ptr_t) calloc (sizeof (unsigned int), BITSET_UINTS);
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#ifdef RE_ENABLE_I18N
|
2002-02-27 03:06:03 +08:00
|
|
|
|
mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1);
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#endif /* RE_ENABLE_I18N */
|
|
|
|
|
|
|
|
|
|
#ifdef RE_ENABLE_I18N
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (sbcset == NULL || mbcset == NULL, 0))
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#else /* not RE_ENABLE_I18N */
|
|
|
|
|
if (BE (sbcset == NULL, 0))
|
|
|
|
|
#endif /* not RE_ENABLE_I18N */
|
2002-02-27 03:06:03 +08:00
|
|
|
|
{
|
|
|
|
|
*err = REG_ESPACE;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (not)
|
|
|
|
|
{
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#ifdef RE_ENABLE_I18N
|
2002-02-27 03:06:03 +08:00
|
|
|
|
int i;
|
|
|
|
|
/*
|
|
|
|
|
if (syntax & RE_HAT_LISTS_NOT_NEWLINE)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
bitset_set(cset->sbcset, '\0');
|
2002-02-27 03:06:03 +08:00
|
|
|
|
*/
|
2002-05-24 16:49:00 +08:00
|
|
|
|
mbcset->non_match = 1;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
if (MB_CUR_MAX > 1)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
for (i = 0; i < SBC_MAX; ++i)
|
|
|
|
|
if (__btowc (i) == WEOF)
|
|
|
|
|
bitset_set (sbcset, i);
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#else /* not RE_ENABLE_I18N */
|
|
|
|
|
non_match = 1;
|
|
|
|
|
#endif /* not RE_ENABLE_I18N */
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
2002-04-20 08:57:03 +08:00
|
|
|
|
/* We don't care the syntax in this case. */
|
2003-06-13 02:29:52 +08:00
|
|
|
|
ret = build_charclass (trans, sbcset,
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#ifdef RE_ENABLE_I18N
|
2002-11-07 04:36:47 +08:00
|
|
|
|
mbcset, &alloc,
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#endif /* RE_ENABLE_I18N */
|
2003-06-18 06:40:05 +08:00
|
|
|
|
(const unsigned char *) "alnum", 0);
|
2002-05-24 16:49:00 +08:00
|
|
|
|
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (ret != REG_NOERROR, 0))
|
2002-02-27 03:06:03 +08:00
|
|
|
|
{
|
|
|
|
|
re_free (sbcset);
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#ifdef RE_ENABLE_I18N
|
2002-02-27 03:06:03 +08:00
|
|
|
|
free_charset (mbcset);
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#endif /* RE_ENABLE_I18N */
|
2003-01-17 03:20:36 +08:00
|
|
|
|
*err = ret;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2002-04-27 04:52:02 +08:00
|
|
|
|
/* \w match '_' also. */
|
|
|
|
|
bitset_set (sbcset, '_');
|
2002-02-27 03:06:03 +08:00
|
|
|
|
|
|
|
|
|
/* If it is non-matching list. */
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#ifdef RE_ENABLE_I18N
|
2002-02-27 03:06:03 +08:00
|
|
|
|
if (mbcset->non_match)
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#else /* not RE_ENABLE_I18N */
|
|
|
|
|
if (non_match)
|
|
|
|
|
#endif /* not RE_ENABLE_I18N */
|
2002-02-27 03:06:03 +08:00
|
|
|
|
bitset_not (sbcset);
|
|
|
|
|
|
|
|
|
|
/* Build a tree for simple bracket. */
|
|
|
|
|
br_token.type = SIMPLE_BRACKET;
|
|
|
|
|
br_token.opr.sbcset = sbcset;
|
|
|
|
|
new_idx = re_dfa_add_node (dfa, br_token, 0);
|
|
|
|
|
tree = create_tree (NULL, NULL, 0, new_idx);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (new_idx == -1 || tree == NULL, 0))
|
2002-02-27 03:06:03 +08:00
|
|
|
|
goto build_word_op_espace;
|
|
|
|
|
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#ifdef RE_ENABLE_I18N
|
2002-02-27 03:06:03 +08:00
|
|
|
|
if (MB_CUR_MAX > 1)
|
|
|
|
|
{
|
|
|
|
|
re_token_t alt_token;
|
|
|
|
|
bin_tree_t *mbc_tree;
|
|
|
|
|
/* Build a tree for complex bracket. */
|
|
|
|
|
br_token.type = COMPLEX_BRACKET;
|
|
|
|
|
br_token.opr.mbcset = mbcset;
|
|
|
|
|
dfa->has_mb_node = 1;
|
|
|
|
|
new_idx = re_dfa_add_node (dfa, br_token, 0);
|
|
|
|
|
mbc_tree = create_tree (NULL, NULL, 0, new_idx);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (new_idx == -1 || mbc_tree == NULL, 0))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
goto build_word_op_espace;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
/* Then join them by ALT node. */
|
|
|
|
|
alt_token.type = OP_ALT;
|
|
|
|
|
new_idx = re_dfa_add_node (dfa, alt_token, 0);
|
|
|
|
|
tree = create_tree (tree, mbc_tree, 0, new_idx);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (new_idx != -1 && mbc_tree != NULL, 1))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
return tree;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
free_charset (mbcset);
|
|
|
|
|
return tree;
|
|
|
|
|
}
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#else /* not RE_ENABLE_I18N */
|
|
|
|
|
return tree;
|
|
|
|
|
#endif /* not RE_ENABLE_I18N */
|
|
|
|
|
|
2002-02-27 03:06:03 +08:00
|
|
|
|
build_word_op_espace:
|
|
|
|
|
re_free (sbcset);
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#ifdef RE_ENABLE_I18N
|
2002-02-27 03:06:03 +08:00
|
|
|
|
free_charset (mbcset);
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#endif /* RE_ENABLE_I18N */
|
2002-02-27 03:06:03 +08:00
|
|
|
|
*err = REG_ESPACE;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* This is intended for the expressions like "a{1,3}".
|
|
|
|
|
Fetch a number from `input', and return the number.
|
|
|
|
|
Return -1, if the number field is empty like "{,1}".
|
|
|
|
|
Return -2, If an error is occured. */
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
fetch_number (input, token, syntax)
|
|
|
|
|
re_string_t *input;
|
|
|
|
|
re_token_t *token;
|
|
|
|
|
reg_syntax_t syntax;
|
|
|
|
|
{
|
|
|
|
|
int num = -1;
|
|
|
|
|
unsigned char c;
|
|
|
|
|
while (1)
|
|
|
|
|
{
|
|
|
|
|
*token = fetch_token (input, syntax);
|
|
|
|
|
c = token->opr.c;
|
2002-04-20 08:57:03 +08:00
|
|
|
|
if (BE (token->type == END_OF_RE, 0))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
return -2;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
if (token->type == OP_CLOSE_DUP_NUM || c == ',')
|
2002-11-07 04:36:47 +08:00
|
|
|
|
break;
|
2002-04-20 08:57:03 +08:00
|
|
|
|
num = ((token->type != CHARACTER || c < '0' || '9' < c || num == -2)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
? -2 : ((num == -1) ? c - '0' : num * 10 + c - '0'));
|
2002-04-20 08:57:03 +08:00
|
|
|
|
num = (num > RE_DUP_MAX) ? -2 : num;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
return num;
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#ifdef RE_ENABLE_I18N
|
2002-02-27 03:06:03 +08:00
|
|
|
|
static void
|
|
|
|
|
free_charset (re_charset_t *cset)
|
|
|
|
|
{
|
|
|
|
|
re_free (cset->mbchars);
|
2002-05-24 16:49:00 +08:00
|
|
|
|
# ifdef _LIBC
|
2002-02-27 03:06:03 +08:00
|
|
|
|
re_free (cset->coll_syms);
|
|
|
|
|
re_free (cset->equiv_classes);
|
|
|
|
|
re_free (cset->range_starts);
|
|
|
|
|
re_free (cset->range_ends);
|
2002-05-24 16:49:00 +08:00
|
|
|
|
# endif
|
2002-02-27 03:06:03 +08:00
|
|
|
|
re_free (cset->char_classes);
|
|
|
|
|
re_free (cset);
|
|
|
|
|
}
|
2002-05-24 16:49:00 +08:00
|
|
|
|
#endif /* RE_ENABLE_I18N */
|
2002-02-27 03:06:03 +08:00
|
|
|
|
|
|
|
|
|
/* Functions for binary tree operation. */
|
|
|
|
|
|
|
|
|
|
/* Create a node of tree.
|
|
|
|
|
Note: This function automatically free left and right if malloc fails. */
|
|
|
|
|
|
|
|
|
|
static bin_tree_t *
|
|
|
|
|
create_tree (left, right, type, index)
|
|
|
|
|
bin_tree_t *left;
|
|
|
|
|
bin_tree_t *right;
|
|
|
|
|
re_token_type_t type;
|
|
|
|
|
int index;
|
|
|
|
|
{
|
|
|
|
|
bin_tree_t *tree;
|
|
|
|
|
tree = re_malloc (bin_tree_t, 1);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (tree == NULL, 0))
|
2002-02-27 03:06:03 +08:00
|
|
|
|
{
|
|
|
|
|
free_bin_tree (left);
|
|
|
|
|
free_bin_tree (right);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
tree->parent = NULL;
|
|
|
|
|
tree->left = left;
|
|
|
|
|
tree->right = right;
|
|
|
|
|
tree->type = type;
|
|
|
|
|
tree->node_idx = index;
|
|
|
|
|
tree->first = -1;
|
|
|
|
|
tree->next = -1;
|
|
|
|
|
re_node_set_init_empty (&tree->eclosure);
|
|
|
|
|
|
|
|
|
|
if (left != NULL)
|
|
|
|
|
left->parent = tree;
|
|
|
|
|
if (right != NULL)
|
|
|
|
|
right->parent = tree;
|
|
|
|
|
return tree;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Free the sub tree pointed by TREE. */
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
free_bin_tree (tree)
|
|
|
|
|
bin_tree_t *tree;
|
|
|
|
|
{
|
|
|
|
|
if (tree == NULL)
|
|
|
|
|
return;
|
|
|
|
|
/*re_node_set_free (&tree->eclosure);*/
|
|
|
|
|
free_bin_tree (tree->left);
|
|
|
|
|
free_bin_tree (tree->right);
|
|
|
|
|
re_free (tree);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Duplicate the node SRC, and return new node. */
|
|
|
|
|
|
|
|
|
|
static bin_tree_t *
|
|
|
|
|
duplicate_tree (src, dfa)
|
|
|
|
|
const bin_tree_t *src;
|
|
|
|
|
re_dfa_t *dfa;
|
|
|
|
|
{
|
|
|
|
|
bin_tree_t *left = NULL, *right = NULL, *new_tree;
|
|
|
|
|
int new_node_idx;
|
|
|
|
|
/* Since node indies must be according to Post-order of the tree,
|
|
|
|
|
we must duplicate the left at first. */
|
|
|
|
|
if (src->left != NULL)
|
|
|
|
|
{
|
|
|
|
|
left = duplicate_tree (src->left, dfa);
|
|
|
|
|
if (left == NULL)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
return NULL;
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Secondaly, duplicate the right. */
|
|
|
|
|
if (src->right != NULL)
|
|
|
|
|
{
|
|
|
|
|
right = duplicate_tree (src->right, dfa);
|
|
|
|
|
if (right == NULL)
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
free_bin_tree (left);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* At last, duplicate itself. */
|
|
|
|
|
if (src->type == NON_TYPE)
|
|
|
|
|
{
|
|
|
|
|
new_node_idx = re_dfa_add_node (dfa, dfa->nodes[src->node_idx], 0);
|
|
|
|
|
dfa->nodes[new_node_idx].duplicated = 1;
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (new_node_idx == -1, 0))
|
2002-11-07 04:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
free_bin_tree (left);
|
|
|
|
|
free_bin_tree (right);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2002-02-27 03:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
new_node_idx = src->type;
|
|
|
|
|
|
|
|
|
|
new_tree = create_tree (left, right, src->type, new_node_idx);
|
2002-03-12 10:04:08 +08:00
|
|
|
|
if (BE (new_tree == NULL, 0))
|
2002-02-27 03:06:03 +08:00
|
|
|
|
{
|
|
|
|
|
free_bin_tree (left);
|
|
|
|
|
free_bin_tree (right);
|
|
|
|
|
}
|
|
|
|
|
return new_tree;
|
|
|
|
|
}
|