regcomp.c revision 7e7bd3dccbfe8f79e25e5c1554b5bc3a9aaca321
/* Extended regular expression matching and search library.
Copyright (C) 2002,2003,2004,2005,2006,2007 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
const re_dfastate_t *init_state,
char *fastmap);
#ifdef RE_ENABLE_I18N
#endif /* RE_ENABLE_I18N */
#ifdef RE_ENABLE_I18N
#endif
void *extra);
void *extra);
bin_tree_t *node);
unsigned int constraint);
reg_errcode_t *err);
bool accept_hyphen);
re_token_t *token);
#ifdef RE_ENABLE_I18N
const unsigned char *name);
const unsigned char *class_name,
#else /* not RE_ENABLE_I18N */
const unsigned char *name);
const unsigned char *class_name,
#endif /* not RE_ENABLE_I18N */
const unsigned char *class_name,
const unsigned char *extra,
const re_token_t *token);
/* 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? */
static const char __re_error_msgid[] =
{
#define REG_NOERROR_IDX 0
"\0"
"\0"
"\0"
"\0"
"\0"
"\0"
"\0"
"\0"
"\0"
"\0"
"\0"
"\0"
"\0"
"\0"
"\0"
"\0"
};
static const size_t __re_error_msgid_idx[] =
{
};
/* Entry points for GNU code. */
/* re_compile_pattern is the GNU regular expression compiler: it
compiles PATTERN (of length LENGTH) and puts the result in BUFP.
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. */
#ifdef _LIBC
const char *
const char *pattern;
struct re_pattern_buffer *bufp;
#else /* size_t might promote */
const char *
struct re_pattern_buffer *bufp)
#endif
{
/* 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, unless RE_NO_SUB is set. */
/* Match anchors at newline. */
if (!ret)
return NULL;
}
#ifdef _LIBC
#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. */
/* 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. */
{
return ret;
}
#ifdef _LIBC
#endif
int
struct re_pattern_buffer *bufp;
{
return 0;
}
#ifdef _LIBC
#endif
static inline void
{
if (icase)
}
/* Helper function for re_compile_fastmap.
Compile fastmap for the initial_state INIT_STATE. */
static void
char *fastmap)
{
{
{
#ifdef RE_ENABLE_I18N
{
unsigned char buf[MB_LEN_MAX];
unsigned char *p;
p = buf;
!= (size_t) -1))
}
#endif
}
else if (type == SIMPLE_BRACKET)
{
int i, ch;
for (i = 0, ch = 0; i < BITSET_WORDS; ++i)
{
int j;
for (j = 0; j < BITSET_WORD_BITS; ++j, ++ch)
if (w & ((bitset_word_t) 1 << j))
}
}
#ifdef RE_ENABLE_I18N
else if (type == COMPLEX_BRACKET)
{
Idx i;
{
# ifdef _LIBC
{
/* 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'. */
for (i = 0; i < SBC_MAX; ++i)
if (table[i] < 0)
}
# else
for (i = 0; i < SBC_MAX; ++i)
# endif /* not _LIBC */
}
{
char buf[256];
{
!= (size_t) -1)
}
}
}
#endif /* RE_ENABLE_I18N */
#ifdef RE_ENABLE_I18N
|| type == OP_UTF8_PERIOD
#endif /* RE_ENABLE_I18N */
{
return;
}
}
}
/* 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
const char *_Restrict_ pattern;
int cflags;
{
/* Try to allocate space for the fastmap. */
return REG_ESPACE;
/* If REG_NEWLINE is set, newlines are treated differently. */
if (cflags & REG_NEWLINE)
{ /* REG_NEWLINE implies neither . nor [^...] match newline. */
syntax &= ~RE_DOT_NEWLINE;
/* It also changes the matching behavior. */
}
else
preg->newline_anchor = 0;
/* 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;
/* We have already checked preg->fastmap != NULL. */
/* Compute the fastmap now, since regexec cannot modify the pattern
buffer. This function never fails in this implementation. */
(void) re_compile_fastmap (preg);
else
{
/* Some error occurred while compiling the expression. */
}
return (int) ret;
}
#ifdef _LIBC
#endif
/* Returns a message corresponding to an error code, ERRCODE, returned
from either regcomp or regexec. We don't use PREG here. */
#ifdef _LIBC
int errcode;
char *_Restrict_ errbuf;
#else /* size_t might promote */
#endif
{
const char *msg;
|| errcode >= (int) (sizeof (__re_error_msgid_idx)
/ sizeof (__re_error_msgid_idx[0])), 0))
/* 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 ();
{
{
}
}
return msg_size;
}
#ifdef _LIBC
#endif
#ifdef RE_ENABLE_I18N
/* This static array is used for the map to single-byte characters when
UTF-8 is used. Otherwise we would allocate memory just to initialize
it the same all the time. UTF-8 is the preferred encoding so this is
a worthwhile optimization. */
static const bitset_t utf8_sb_map =
{
/* Set the first 128 bits. */
# error "bitset_word_t is narrower than 32 bits"
# endif
>> (SBC_MAX % BITSET_WORD_BITS == 0
? 0
};
#endif
static void
{
Idx i, j;
{
}
if (dfa->state_table)
for (i = 0; i <= dfa->state_hash_mask; ++i)
{
{
free_state (state);
}
}
#ifdef RE_ENABLE_I18N
#endif
#ifdef DEBUG
#endif
}
/* Free dynamically allocated space used by PREG. */
void
{
}
#ifdef _LIBC
#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
# endif
re_comp (s)
const char *s;
{
char *fastmap;
if (!s)
{
if (!re_comp_buf.buffer)
return gettext ("No previous regular expression");
return 0;
}
if (re_comp_buf.buffer)
{
__regfree (&re_comp_buf);
}
{
return (char *) gettext (__re_error_msgid
+ __re_error_msgid_idx[(int) REG_ESPACE]);
}
/* 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. */
if (!ret)
return NULL;
/* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
}
#ifdef _LIBC
{
__regfree (&re_comp_buf);
}
#endif
#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
{
/* Initialize the pattern buffer. */
preg->fastmap_accurate = 0;
preg->can_be_null = 0;
/* Initialize the dfa. */
{
/* 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. */
return REG_ESPACE;
}
{
return err;
}
#ifdef DEBUG
/* Note: length+1 will not overflow since it is checked in init_dfa. */
#endif
{
return err;
}
/* Parse the regular expression, and build a structure tree. */
/* Analyze the tree and create the nfa. */
#ifdef RE_ENABLE_I18N
/* If possible, do searching in single byte encoding to speed things up. */
optimize_utf8 (dfa);
#endif
/* Then create the initial state of the dfa. */
/* Release work areas. */
{
}
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
{
#ifdef RE_ENABLE_I18N
#else
#endif
MAX (sizeof (struct re_state_table_entry),
MAX (sizeof (re_token_t),
MAX (sizeof (re_node_set),
MAX (sizeof (regmatch_t),
max_i18n_object_size))));
/* Force allocation of str_tree_storage the first time. */
/* Avoid overflows. The extra "/ 2" is for the table_size doubling
calculation below, and for similar doubling calculations
elsewhere. And it's <= rather than <, because some of the
doubling calculations add 1 afterwards. */
return REG_ESPACE;
/* table_size = 2 ^ ceil(log pat_len) */
if (table_size > pat_len)
break;
#ifdef _LIBC
!= 0);
#else
/* We check exhaustively in the loop below if this charset is a
superset of ASCII. */
dfa->map_notascii = 0;
#endif
#ifdef RE_ENABLE_I18N
{
else
{
int i, j, ch;
return REG_ESPACE;
/* Set the bits corresponding to single byte chars. */
for (i = 0, ch = 0; i < BITSET_WORDS; ++i)
for (j = 0; j < BITSET_WORD_BITS; ++j, ++ch)
{
# ifndef _LIBC
# endif
}
}
}
#endif
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. */
static void
{
int i, j, ch;
for (i = 0, ch = 0; i < BITSET_WORDS; ++i)
for (j = 0; j < BITSET_WORD_BITS; ++j, ++ch)
}
/* Free the work area which are only used while compiling. */
static void
{
{
}
}
/* Create initial states for all contexts. */
static reg_errcode_t
{
/* Initial states have the epsilon closure of the node which is
the first node of the regular expression. */
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. */
for (i = 0; i < init_nodes.nelem; ++i)
{
if (type != OP_BACK_REF)
continue;
{
break;
}
continue;
if (type == OP_BACK_REF)
{
{
i = 0;
}
}
}
/* It must be the first time to invoke acquire_state. */
/* We don't check ERR here, since the initial state must not be NULL. */
return err;
{
| CONTEXT_BEGBUF);
return err;
}
else
return REG_NOERROR;
}
#ifdef RE_ENABLE_I18N
/* If it is possible to do searching in single byte encoding instead of UTF-8
to speed things up, set dfa->mb_cur_max to 1, clear is_utf8 and change
DFA nodes where needed. */
static void
{
int i;
bool mb_chars = false;
bool has_period = false;
{
case CHARACTER:
mb_chars = true;
break;
case ANCHOR:
{
case LINE_FIRST:
case LINE_LAST:
case BUF_FIRST:
case BUF_LAST:
break;
default:
/* Word anchors etc. cannot be handled. */
return;
}
break;
case OP_PERIOD:
has_period = true;
break;
case OP_BACK_REF:
case OP_ALT:
case END_OF_RE:
case OP_DUP_ASTERISK:
case OP_OPEN_SUBEXP:
case OP_CLOSE_SUBEXP:
break;
case COMPLEX_BRACKET:
return;
case SIMPLE_BRACKET:
/* Just double check. */
{
? 0
{
return;
rshift = 0;
}
}
break;
default:
abort ();
}
if (mb_chars || has_period)
{
}
/* The search can be in single byte locale. */
}
#endif
/* Analyze the structure tree, and calculate "first", "next", "edest",
"eclosure", and "inveclosure". */
static reg_errcode_t
{
/* Allocate arrays. */
return REG_ESPACE;
{
Idx i;
dfa->subexp_map[i] = i;
if (dfa->subexp_map[i] != i)
break;
{
}
}
return ret;
return ret;
return ret;
return ret;
/* We only need this during the prune_impossible_nodes pass in regexec.c;
skip it if p_i_n will not run, as calc_inveclosure can be quadratic. */
{
return REG_ESPACE;
}
return ret;
}
/* Our parse trees are very unbalanced, so we cannot use a stack to
implement parse tree visits. Instead, we use parent pointers and
some hairy code in these two functions. */
static reg_errcode_t
void *extra)
{
{
/* Descend down the tree, preferably to the left (or to the right
if that's the only child). */
else
do
{
return err;
return REG_NOERROR;
}
/* Go up while we have a node that is reached from the right. */
}
}
static reg_errcode_t
void *extra)
{
{
return err;
/* Go to the left node, or up and to the right. */
else
{
{
if (!node)
return REG_NOERROR;
}
}
}
}
/* Optimization pass: if a SUBEXP is entirely contained, strip it and tell
re_search_internal to map the inner one's opr.idx to this one's. Adjust
backreferences as well. Requires a preorder visit. */
static reg_errcode_t
{
{
}
{
if (other_idx < BITSET_WORD_BITS)
}
return REG_NOERROR;
}
/* Lowering pass: Turn each SUBEXP node into the appropriate concatenation
of OP_OPEN_SUBEXP, the body of the SUBEXP (if any) and OP_CLOSE_SUBEXP. */
static reg_errcode_t
{
{
}
{
}
return err;
}
static bin_tree_t *
{
/* We do not optimize empty subexpressions, because otherwise we may
have bad CONCAT nodes with NULL children. This is obviously not
very common, so we do not lose much. An example that triggers
this case is the sed "script" /\(\)/x. */
|| !(dfa->used_bkref_map
/* Convert the SUBEXP node to the concatenation of an
OP_OPEN_SUBEXP, the contents, and an OP_CLOSE_SUBEXP. */
{
*err = REG_ESPACE;
return NULL;
}
return tree;
}
/* Pass 1 in building the NFA: compute FIRST and create unlinked automaton
nodes. Requires a postorder visit. */
static reg_errcode_t
{
{
}
else
{
return REG_ESPACE;
}
return REG_NOERROR;
}
/* Pass 2: compute NEXT on the tree. Preorder visit. */
static reg_errcode_t
{
{
case OP_DUP_ASTERISK:
break;
case CONCAT:
break;
default:
break;
}
return REG_NOERROR;
}
/* Pass 3: link all DFA nodes to their NEXT node (any order will do). */
static reg_errcode_t
{
{
case CONCAT:
break;
case END_OF_RE:
break;
case OP_DUP_ASTERISK:
case OP_ALT:
{
else
else
}
break;
case ANCHOR:
case OP_OPEN_SUBEXP:
case OP_CLOSE_SUBEXP:
break;
case OP_BACK_REF:
break;
default:
break;
}
return err;
}
/* 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
{
bool ok;
unsigned int constraint = init_constraint;
{
{
/* 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. */
return REG_ESPACE;
return REG_ESPACE;
}
{
/* 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. */
break;
}
{
/* In case of the node can epsilon-transit, and it has only one
destination. */
{
/* In case of the node has another constraint, append it. */
{
/* ...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. */
return REG_ESPACE;
break;
}
}
return REG_ESPACE;
return REG_ESPACE;
}
else /* dfa->edests[org_node].nelem == 2 */
{
/* In case of the node can epsilon-transit, and it has two
destinations. In the bin_tree_t and DFA, that's '|' and '*'. */
/* Search for a duplicated node which satisfies the constraint. */
if (clone_dest == REG_MISSING)
{
/* There are no such a duplicated node, create a new one. */
return REG_ESPACE;
return REG_ESPACE;
return err;
}
else
{
/* There are a duplicated node which satisfy the constraint,
use it to avoid infinite loop. */
return REG_ESPACE;
}
return REG_ESPACE;
return REG_ESPACE;
}
}
return REG_NOERROR;
}
/* Search for a node which is duplicated from the node ORG_NODE, and
satisfies the constraint CONSTRAINT. */
static Idx
unsigned int constraint)
{
{
return idx; /* Found. */
}
return REG_MISSING; /* Not found. */
}
/* Duplicate the node whose index is ORG_IDX and set the constraint CONSTRAINT.
Return the index of the new node, or REG_MISSING if insufficient storage is
available. */
static Idx
{
{
/* Store the index of the original node. */
}
return dup_idx;
}
static reg_errcode_t
{
bool ok;
{
{
return REG_ESPACE;
}
}
return REG_NOERROR;
}
/* Calculate "eclosure" for all the node in DFA. */
static reg_errcode_t
{
bool incomplete;
#ifdef DEBUG
#endif
incomplete = false;
/* For each nodes, calculate epsilon closure. */
{
{
if (!incomplete)
break;
incomplete = false;
node_idx = 0;
}
#ifdef DEBUG
#endif
/* If we have already calculated, skip it. */
continue;
/* Calculate epsilon closure of `node_idx'. */
return err;
{
incomplete = true;
}
}
return REG_NOERROR;
}
/* Calculate epsilon closure of NODE. */
static reg_errcode_t
{
unsigned int constraint;
Idx i;
bool incomplete;
bool ok;
incomplete = false;
return err;
/* This indicates that we are calculating this node now.
We reference this value to avoid infinite loop. */
/* If the current node has constraints, duplicate all nodes.
Since they must inherit the constraints. */
if (constraint
{
return err;
}
/* Expand each epsilon destination nodes. */
{
/* If calculating the epsilon closure of `edest' is in progress,
return intermediate result. */
{
incomplete = true;
continue;
}
/* If we haven't calculated the epsilon closure of `edest' yet,
calculate now. Otherwise use calculated epsilon closure. */
{
return err;
}
else
/* Merge the epsilon closure of `edest'. */
/* If the epsilon closure of `edest' is incomplete,
the epsilon closure of this node is also incomplete. */
{
incomplete = true;
}
}
/* Epsilon closures include itself. */
return REG_ESPACE;
if (incomplete && !root)
else
return REG_NOERROR;
}
/* Functions for token which are used in the parser. */
/* Fetch a token from INPUT.
We must not use this function inside bracket expressions. */
static void
{
}
/* Peek a token from INPUT, and return the length of the token.
We must not use this function inside bracket expressions. */
static int
{
unsigned char c;
if (re_string_eoi (input))
{
return 0;
}
c = re_string_peek_byte (input, 0);
#ifdef RE_ENABLE_I18N
token->mb_partial = 0;
{
return 1;
}
#endif
if (c == '\\')
{
unsigned char c2;
{
return 1;
}
#ifdef RE_ENABLE_I18N
{
}
else
#endif
switch (c2)
{
case '|':
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))
{
}
break;
case '<':
if (!(syntax & RE_NO_GNU_OPS))
{
}
break;
case '>':
if (!(syntax & RE_NO_GNU_OPS))
{
}
break;
case 'b':
if (!(syntax & RE_NO_GNU_OPS))
{
}
break;
case 'B':
if (!(syntax & RE_NO_GNU_OPS))
{
}
break;
case 'w':
if (!(syntax & RE_NO_GNU_OPS))
break;
case 'W':
if (!(syntax & RE_NO_GNU_OPS))
break;
case 's':
if (!(syntax & RE_NO_GNU_OPS))
break;
case 'S':
if (!(syntax & RE_NO_GNU_OPS))
break;
case '`':
if (!(syntax & RE_NO_GNU_OPS))
{
}
break;
case '\'':
if (!(syntax & RE_NO_GNU_OPS))
{
}
break;
case '(':
if (!(syntax & RE_NO_BK_PARENS))
break;
case ')':
if (!(syntax & RE_NO_BK_PARENS))
break;
case '+':
break;
case '?':
break;
case '{':
break;
case '}':
break;
default:
break;
}
return 2;
}
#ifdef RE_ENABLE_I18N
{
}
else
#endif
switch (c)
{
case '\n':
if (syntax & RE_NEWLINE_ALT)
break;
case '|':
break;
case '*':
break;
case '+':
break;
case '?':
break;
case '{':
break;
case '}':
break;
case '(':
if (syntax & RE_NO_BK_PARENS)
break;
case ')':
if (syntax & RE_NO_BK_PARENS)
break;
case '[':
break;
case '.':
break;
case '^':
re_string_cur_idx (input) != 0)
{
break;
}
break;
case '$':
if (!(syntax & RE_CONTEXT_INDEP_ANCHORS) &&
{
break;
}
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
{
unsigned char c;
if (re_string_eoi (input))
{
return 0;
}
c = re_string_peek_byte (input, 0);
#ifdef RE_ENABLE_I18N
{
return 1;
}
#endif /* RE_ENABLE_I18N */
{
/* In this case, '\' escape a character. */
unsigned char c2;
return 1;
}
if (c == '[') /* '[' is a special char in a bracket exps. */
{
unsigned char c2;
int token_len;
else
c2 = 0;
token_len = 2;
switch (c2)
{
case '.':
break;
case '=':
break;
case ':':
if (syntax & RE_CHAR_CLASSES)
{
break;
}
/* else fall through. */
default:
token_len = 1;
break;
}
return token_len;
}
switch (c)
{
case '-':
break;
case ']':
break;
case '^':
break;
default:
}
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>:
CAT
/ \
/ \
<reg_exp> EOR
CAT means concatenation.
EOR means end of regular expression. */
static bin_tree_t *
{
return NULL;
else
{
*err = REG_ESPACE;
return NULL;
}
return root;
}
/* This function build the following tree, from regular expression
<branch1>|<branch2>:
ALT
/ \
/ \
<branch1> <branch2>
ALT means alternative, which represents the operator `|'. */
static bin_tree_t *
{
return NULL;
{
{
return NULL;
}
else
{
*err = REG_ESPACE;
return NULL;
}
}
return tree;
}
/* This function build the following tree, from regular expression
<exp1><exp2>:
CAT
/ \
/ \
<exp1> <exp2>
CAT means concatenation. */
static bin_tree_t *
{
return NULL;
{
{
return NULL;
}
{
{
*err = REG_ESPACE;
return NULL;
}
}
/* Otherwise expr == NULL, we don't need to create new tree. */
}
return tree;
}
/* This function build the following tree, from regular expression a*:
*
|
a
*/
static bin_tree_t *
{
{
case CHARACTER:
{
*err = REG_ESPACE;
return NULL;
}
#ifdef RE_ENABLE_I18N
{
while (!re_string_eoi (regexp)
{
{
*err = REG_ESPACE;
return NULL;
}
}
}
#endif
break;
case OP_OPEN_SUBEXP:
return NULL;
break;
case OP_OPEN_BRACKET:
return NULL;
break;
case OP_BACK_REF:
{
*err = REG_ESUBREG;
return NULL;
}
{
*err = REG_ESPACE;
return NULL;
}
break;
case OP_OPEN_DUP_NUM:
if (syntax & RE_CONTEXT_INVALID_DUP)
{
*err = REG_BADRPT;
return NULL;
}
/* FALLTHROUGH */
case OP_DUP_ASTERISK:
case OP_DUP_PLUS:
case OP_DUP_QUESTION:
if (syntax & RE_CONTEXT_INVALID_OPS)
{
*err = REG_BADRPT;
return NULL;
}
else if (syntax & RE_CONTEXT_INDEP_OPS)
{
}
/* else fall through */
case OP_CLOSE_SUBEXP:
{
*err = REG_ERPAREN;
return NULL;
}
/* else fall through */
case OP_CLOSE_DUP_NUM:
/* We treat it as a normal character. */
/* Then we can these characters as normal characters. */
/* mb_partial and word_char bits should be initialized already
by peek_token. */
{
*err = REG_ESPACE;
return NULL;
}
break;
case ANCHOR:
&& dfa->word_ops_used == 0)
{
{
}
else
{
}
{
*err = REG_ESPACE;
return NULL;
}
}
else
{
{
*err = REG_ESPACE;
return NULL;
}
}
/* We must return here, since ANCHORs can't be followed
by repetition operators.
eg. RE"^*" is invalid or "<ANCHOR(^)><CHAR(*)>",
it must not be "<ANCHOR(^)><REPEAT(*)>". */
return tree;
case OP_PERIOD:
{
*err = REG_ESPACE;
return NULL;
}
break;
case OP_WORD:
case OP_NOTWORD:
(const unsigned char *) "alnum",
(const unsigned char *) "_",
return NULL;
break;
case OP_SPACE:
case OP_NOTSPACE:
(const unsigned char *) "space",
(const unsigned char *) "",
return NULL;
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;
}
{
return NULL;
/* In BRE consecutive duplications are not allowed. */
if ((syntax & RE_CONTEXT_INVALID_DUP)
{
*err = REG_BADRPT;
return NULL;
}
}
return tree;
}
/* This function build the following tree, from regular expression
(<reg_exp>):
SUBEXP
|
<reg_exp>
*/
static bin_tree_t *
{
/* The subexpression may be a null string. */
else
{
*err = REG_EPAREN;
return NULL;
}
{
*err = REG_ESPACE;
return NULL;
}
return tree;
}
/* This function parse repetition operators like "*", "+", "{1,3}" etc. */
static bin_tree_t *
{
{
end = 0;
if (start == REG_MISSING)
{
start = 0; /* We treat "{,m}" as "{0,m}". */
else
{
return NULL;
}
}
{
/* We treat "{n}" as "{n,n}". */
}
{
/* Invalid sequence. */
{
*err = REG_EBRACE;
else
return NULL;
}
/* If the syntax bit is set, rollback. */
*token = start_token;
/* mb_partial and word_char bits should be already initialized by
peek_token. */
return elem;
}
{
/* First number greater than second. */
return NULL;
}
}
else
{
}
return NULL;
{
return NULL;
}
/* Extract "<re>{n,m}" to "<re><re>...<re><re>{0,<m-n>}". */
{
for (i = 2; i <= start; ++i)
{
goto parse_dup_op_espace;
}
return tree;
/* Duplicate ELEM before it is marked optional. */
}
else
goto parse_dup_op_espace;
/* This loop is actually executed only when end != REG_MISSING,
to rewrite <re>{0,n} as (<re>(<re>...<re>?)?)?... We have
already created the start+1-th copy. */
{
goto parse_dup_op_espace;
goto parse_dup_op_espace;
}
if (old_tree)
return tree;
*err = REG_ESPACE;
return NULL;
}
/* Size of the names for collating symbol/equivalence_class/character_class.
I'm not sure, but maybe enough. */
#define BRACKET_NAME_BUF_SIZE 32
#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
# ifdef RE_ENABLE_I18N
# else /* not RE_ENABLE_I18N */
# endif /* not RE_ENABLE_I18N */
{
0))
return REG_ERANGE;
/* We can handle no multi character collating elements without libc
support. */
return REG_ECOLLATE;
# ifdef RE_ENABLE_I18N
{
: 0));
: 0));
return REG_ECOLLATE;
return REG_ERANGE;
/* Got valid collation sequence values, add them as a new entry.
However, for !_LIBC we have no collation elements: if the
character set is single byte, the single byte character set
that we build below suffices. parse_bracket_exp passes
no MBCSET if dfa->mb_cur_max == 1. */
if (mbcset)
{
/* Check the space of the arrays. */
{
/* There is not enough space, need realloc. */
/* +1 in case of mbcset->nranges is 0. */
/* Use realloc since mbcset->range_starts and mbcset->range_ends
are NULL if *range_alloc == 0. */
return REG_ESPACE;
}
}
/* Build the table for single byte characters. */
{
}
}
# else /* not RE_ENABLE_I18N */
{
unsigned int ch;
: 0));
: 0));
return REG_ERANGE;
/* Build the table for single byte characters. */
}
# 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
# ifdef RE_ENABLE_I18N
# endif
const unsigned char *name)
{
return REG_ECOLLATE;
else
{
return REG_NOERROR;
}
}
#endif /* not _LIBC */
/* This function parse bracket expression like "[abc]", "[a-c]",
"[[.a-a.]]" etc. */
static bin_tree_t *
{
#ifdef _LIBC
const unsigned char *collseqmb;
const char *collseqwc;
const int32_t *symb_table;
const unsigned char *extra;
/* Local function for parse_bracket_exp used in _LIBC environement.
Seek the collating symbol entry correspondings to NAME.
Return the index of the symbol in the SYMB_TABLE. */
auto inline int32_t
const unsigned char *name;
{
{
do
{
/* First compare the hashing value. */
/* Compare the length of the name. */
/* Compare the name. */
name_len) == 0)
{
/* Yep, this is the entry. */
break;
}
/* Next entry. */
}
}
return elem;
}
/* Local function for parse_bracket_exp used in _LIBC environement.
Look up the collation sequence value of BR_ELEM.
Return the value if succeeded, UINT_MAX otherwise. */
auto inline unsigned int
{
{
/*
if (MB_CUR_MAX == 1)
*/
if (nrules == 0)
else
{
}
}
{
}
{
if (nrules != 0)
{
{
/* We found the entry. */
/* Skip the name of collating element name. */
/* Skip the byte sequence of the collating element. */
/* Adjust for the alignment. */
/* Skip the multibyte collation sequence value. */
idx += sizeof (unsigned int);
/* Skip the wide char sequence of the collating element. */
idx += sizeof (unsigned int) *
/* Return the collation sequence value. */
}
{
/* No valid character. Match it as a single byte
character. */
}
}
else if (sym_name_len == 1)
}
return UINT_MAX;
}
/* Local function for parse_bracket_exp used in _LIBC environement.
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. */
auto inline reg_errcode_t
{
unsigned int ch;
/* Equivalence Classes and Character Classes can't be a range
0))
return REG_ERANGE;
return REG_ECOLLATE;
return REG_ERANGE;
/* Got valid collation sequence values, add them as a new entry.
However, if we have no collation elements, and the character set
is single byte, the single byte character set that we
build below suffices. */
{
/* Check the space of the arrays. */
{
/* There is not enough space, need realloc. */
/* +1 in case of mbcset->nranges is 0. */
return REG_ESPACE;
}
}
/* Build the table for single byte characters. */
{
/*
if (MB_CUR_MAX == 1)
*/
if (nrules == 0)
else
}
return REG_NOERROR;
}
/* Local function for parse_bracket_exp used in _LIBC environement.
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. */
auto inline reg_errcode_t
const unsigned char *name;
{
if (nrules != 0)
{
{
/* We found the entry. */
/* Skip the name of collating element name. */
}
{
/* No valid character, treat it as a normal
character. */
return REG_NOERROR;
}
else
return REG_ECOLLATE;
/* Got valid collation sequence, add it as a new entry. */
/* Check the space of the arrays. */
{
/* Not enough, realloc it. */
/* +1 in case of mbcset->ncoll_syms is 0. */
/* Use realloc since mbcset->coll_syms is NULL
if *alloc == 0. */
return REG_ESPACE;
}
return REG_NOERROR;
}
else
{
return REG_ECOLLATE;
else
{
return REG_NOERROR;
}
}
}
#endif
#ifdef RE_ENABLE_I18N
#endif /* not RE_ENABLE_I18N */
bool non_match = false;
int token_len;
bool first_round = true;
#ifdef _LIBC
collseqmb = (const unsigned char *)
if (nrules)
{
/*
if (MB_CUR_MAX > 1)
*/
}
#endif
#ifdef RE_ENABLE_I18N
#endif /* RE_ENABLE_I18N */
#ifdef RE_ENABLE_I18N
#else
#endif /* RE_ENABLE_I18N */
{
*err = REG_ESPACE;
return NULL;
}
{
*err = REG_BADPAT;
}
{
#ifdef RE_ENABLE_I18N
#endif /* not RE_ENABLE_I18N */
non_match = true;
if (syntax & RE_HAT_LISTS_NOT_NEWLINE)
{
*err = REG_BADPAT;
}
}
/* We treat the first ']' as a normal character. */
while (1)
{
unsigned char start_name_buf[BRACKET_NAME_BUF_SIZE];
unsigned char end_name_buf[BRACKET_NAME_BUF_SIZE];
int token_len2 = 0;
bool is_range_exp = false;
{
}
first_round = false;
/* Get information about the next token. We need it in any case. */
/* Do not check for ranges if we know they are not allowed. */
{
{
*err = REG_EBRACK;
}
{
{
*err = REG_EBRACK;
}
{
/* We treat the last '-' as a normal character. */
}
else
is_range_exp = true;
}
}
if (is_range_exp == true)
{
{
}
#ifdef _LIBC
&start_elem, &end_elem);
#else
# ifdef RE_ENABLE_I18N
# else
# endif
#endif /* RE_ENABLE_I18N */
}
else
{
switch (start_elem.type)
{
case SB_CHAR:
break;
#ifdef RE_ENABLE_I18N
case MB_CHAR:
/* Check whether the array has enough space. */
{
/* Not enough, realloc it. */
/* +1 in case of mbcset->nmbchars is 0. */
/* Use realloc since array is NULL if *alloc == 0. */
goto parse_bracket_exp_espace;
}
break;
#endif /* RE_ENABLE_I18N */
case EQUIV_CLASS:
#ifdef RE_ENABLE_I18N
#endif /* RE_ENABLE_I18N */
break;
case COLL_SYM:
#ifdef RE_ENABLE_I18N
#endif /* RE_ENABLE_I18N */
break;
case CHAR_CLASS:
#ifdef RE_ENABLE_I18N
#endif /* RE_ENABLE_I18N */
break;
default:
assert (0);
break;
}
}
{
*err = REG_EBRACK;
}
break;
}
/* If it is non-matching list. */
if (non_match)
bitset_not (sbcset);
#ifdef RE_ENABLE_I18N
/* Ensure only single byte characters are set. */
{
int sbc_idx;
/* Build a tree for complex bracket. */
goto parse_bracket_exp_espace;
break;
/* If there are no bits set in sbcset, there is no point
of having both SIMPLE_BRACKET and COMPLEX_BRACKET. */
if (sbc_idx < BITSET_WORDS)
{
/* Build a tree for simple bracket. */
goto parse_bracket_exp_espace;
/* Then join them by ALT node. */
goto parse_bracket_exp_espace;
}
else
{
}
}
else
#endif /* not RE_ENABLE_I18N */
{
#ifdef RE_ENABLE_I18N
#endif
/* Build a tree for simple bracket. */
goto parse_bracket_exp_espace;
}
return work_tree;
*err = REG_ESPACE;
#ifdef RE_ENABLE_I18N
#endif /* RE_ENABLE_I18N */
return NULL;
}
/* Parse an element in the bracket expression. */
static reg_errcode_t
{
#ifdef RE_ENABLE_I18N
int cur_char_size;
if (cur_char_size > 1)
{
return REG_NOERROR;
}
#endif /* RE_ENABLE_I18N */
{
/* A '-' must only appear as anything but a range indicator before
the closing bracket. Everything else is an error. */
/* The actual error value is not standardized since this whole
case is undefined. But ERANGE makes good sense. */
return REG_ERANGE;
}
return REG_NOERROR;
}
/* Parse a bracket symbol in the bracket expression. Bracket symbols are
such as [:<character_class>:], [.<collating_element>.], and
[=<equivalent_class>=]. */
static reg_errcode_t
{
int i = 0;
if (re_string_eoi(regexp))
return REG_EBRACK;
for (;; ++i)
{
if (i >= BRACKET_NAME_BUF_SIZE)
return REG_EBRACK;
else
if (re_string_eoi(regexp))
return REG_EBRACK;
break;
}
{
case OP_OPEN_COLL_ELEM:
break;
case OP_OPEN_EQUIV_CLASS:
break;
case OP_OPEN_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
#ifdef RE_ENABLE_I18N
#else /* not RE_ENABLE_I18N */
#endif /* not RE_ENABLE_I18N */
{
#ifdef _LIBC
if (nrules != 0)
{
unsigned char char_buf[2];
unsigned int ch;
/* This #include defines a local function! */
/* Calculate the index for equivalence class. */
/* This isn't a valid character. */
return REG_ECOLLATE;
/* Build single byte matcing table for this equivalence class. */
{
/*
idx2 = table[ch];
*/
if (idx2 == 0)
/* This isn't a valid character. */
continue;
{
int cnt = 0;
++cnt;
}
}
/* Check whether the array has enough space. */
{
/* Not enough, realloc it. */
/* +1 in case of mbcset->nequiv_classes is 0. */
/* Use realloc since the array is NULL if *alloc == 0. */
return REG_ESPACE;
}
}
else
#endif /* _LIBC */
{
return REG_ECOLLATE;
}
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
#ifdef RE_ENABLE_I18N
#else /* not RE_ENABLE_I18N */
#endif /* not RE_ENABLE_I18N */
{
int i;
const char *name = (const char *) class_name;
/* In case of REG_ICASE "upper" and "lower" match the both of
upper and lower cases. */
name = "alpha";
#ifdef RE_ENABLE_I18N
/* Check the space of the arrays. */
{
/* Not enough, realloc it. */
/* +1 in case of mbcset->nchar_classes is 0. */
/* Use realloc since array is NULL if *alloc == 0. */
return REG_ESPACE;
}
#endif /* RE_ENABLE_I18N */
#define BUILD_CHARCLASS_LOOP(ctype_func) \
do { \
{ \
for (i = 0; i < SBC_MAX; ++i) \
if (ctype_func (i)) \
} \
else \
{ \
for (i = 0; i < SBC_MAX; ++i) \
if (ctype_func (i)) \
bitset_set (sbcset, i); \
} \
} while (0)
else
return REG_ECTYPE;
return REG_NOERROR;
}
static bin_tree_t *
const unsigned char *class_name,
{
#ifdef RE_ENABLE_I18N
#endif /* not RE_ENABLE_I18N */
#ifdef RE_ENABLE_I18N
#endif /* RE_ENABLE_I18N */
#ifdef RE_ENABLE_I18N
#else /* not RE_ENABLE_I18N */
#endif /* not RE_ENABLE_I18N */
{
*err = REG_ESPACE;
return NULL;
}
if (non_match)
{
#ifdef RE_ENABLE_I18N
#endif /* not RE_ENABLE_I18N */
}
/* We don't care the syntax in this case. */
#ifdef RE_ENABLE_I18N
#endif /* RE_ENABLE_I18N */
class_name, 0);
{
#ifdef RE_ENABLE_I18N
#endif /* RE_ENABLE_I18N */
return NULL;
}
/* \w match '_' also. */
/* If it is non-matching list. */
if (non_match)
bitset_not (sbcset);
#ifdef RE_ENABLE_I18N
/* Ensure only single byte characters are set. */
#endif
/* Build a tree for simple bracket. */
goto build_word_op_espace;
#ifdef RE_ENABLE_I18N
{
/* Build a tree for complex bracket. */
goto build_word_op_espace;
/* Then join them by ALT node. */
return tree;
}
else
{
return tree;
}
#else /* not RE_ENABLE_I18N */
return tree;
#endif /* not RE_ENABLE_I18N */
#ifdef RE_ENABLE_I18N
#endif /* RE_ENABLE_I18N */
*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 REG_MISSING if the number field is empty like "{,1}".
Return REG_ERROR if an error occurred. */
static Idx
{
unsigned char c;
while (1)
{
return REG_ERROR;
break;
}
return num;
}
#ifdef RE_ENABLE_I18N
static void
{
# ifdef _LIBC
# endif
}
#endif /* RE_ENABLE_I18N */
/* Functions for binary tree operation. */
/* Create a tree node. */
static bin_tree_t *
{
re_token_t t;
}
static bin_tree_t *
const re_token_t *token)
{
{
return NULL;
dfa->str_tree_storage_idx = 0;
}
return tree;
}
/* Mark the tree SRC as an optional subexpression.
To be called from preorder or postorder. */
static reg_errcode_t
{
return REG_NOERROR;
}
/* Free the allocated memory inside NODE. */
static void
{
#ifdef RE_ENABLE_I18N
else
#endif /* RE_ENABLE_I18N */
}
/* Worker function for tree walking. Free the allocated memory inside NODE
and its children. */
static reg_errcode_t
{
return REG_NOERROR;
}
/* Duplicate the node SRC, and return new node. This is a preorder
visit similar to the one implemented by the generic visitor, but
we need more infrastructure to maintain two parallel trees --- so,
it's easier to duplicate. */
static bin_tree_t *
{
const bin_tree_t *node;
{
/* Create a new tree and link it back to the current parent. */
return NULL;
/* Go to the left node, or up and to the right. */
{
}
else
{
{
if (!node)
return dup_root;
}
}
}
}