Import latest regex module from gnulib, to fix some 64-bit bugs.

This commit is contained in:
Paul Eggert 2005-09-09 21:07:50 +00:00
parent fd4a5b1b53
commit 3301671de9
10 changed files with 1958 additions and 1542 deletions

View File

@ -2,6 +2,23 @@
* Version 5.3.1-cvs.
Support regular expressions in 64-bit code correctly, by
importing the latest gnulib regexp code, and not assuming
that sizes fit in 32 bits.
* src/csplit.c (process_regexp): Store match length in regoff_t,
not int. Assume that negative return values less than -2
represent regoff_t overflow.
* src/expr.c (docolon): Likewise.
* src/nl.c (proc_text): Likewise.
* src/ptx.c (SKIP_SOMETHING, find_occurs_in_text): Likewise.
* src/tac.c (tac_seekable): Likewise.
* src/expr.c (docolon) Check for size calculation overflow.
* src/nl.c (build_type_arg): Likewise.
* src/ptx.c (matcher_error): New function.
(SKIP_SOMETHING): Use it to report matcher errors.
(alloc_and_compile_regex): No longer any need to worry about
int versus size_t mismatch.
* NEWS: Document "niceness" vs "nice value".
* configure.ac (utils_cv_func_setpriority): Simplify the tests.
Define HAVE_NICE rather than NICE_PRIORITY (since a niceness is

View File

@ -1,3 +1,8 @@
2005-09-09 Paul Eggert <eggert@cs.ucla.edu>
* regcomp.c, regex.c, regex.h, regex_internal.c, regex_internal.h:
* regexec.c: Import from gnulib, to fix some 64-bit bugs.
2005-09-06 Paul Eggert <eggert@cs.ucla.edu>
* verify.h (__builtin_constant_p): Remove, undoing previous change.

File diff suppressed because it is too large Load Diff

View File

@ -21,30 +21,6 @@
#include "config.h"
#endif
#ifdef _AIX
#pragma alloca
#else
# ifndef allocax /* predefined by HP cc +Olibcalls */
# ifdef __GNUC__
# define alloca(size) __builtin_alloca (size)
# else
# if HAVE_ALLOCA_H
# include <alloca.h>
# else
# ifdef __hpux
void *alloca ();
# else
# if !defined __OS2__ && !defined WIN32
char *alloca ();
# else
# include <malloc.h> /* OS/2 defines alloca in here */
# endif
# endif
# endif
# endif
# endif
#endif
#ifdef _LIBC
/* We have to keep the namespace clean. */
# define regfree(preg) __regfree (preg)
@ -70,10 +46,6 @@
# include "../locale/localeinfo.h"
#endif
/* POSIX says that <sys/types.h> must be included (by the caller) before
<regex.h>. */
#include <sys/types.h>
/* On some systems, limits.h sets RE_DUP_MAX to a lower value than
GNU regex allows. Include it before <regex.h>, which correctly
#undefs RE_DUP_MAX and sets it to the right value. */

View File

@ -28,15 +28,61 @@
extern "C" {
#endif
/* POSIX says that <sys/types.h> must be included (by the caller) before
<regex.h>. */
/* Define _REGEX_SOURCE to get definitions that are incompatible with
POSIX. */
#if (!defined _REGEX_SOURCE \
&& (defined _GNU_SOURCE \
|| (!defined _POSIX_C_SOURCE && !defined _POSIX_SOURCE \
&& !defined _XOPEN_SOURCE)))
# define _REGEX_SOURCE 1
#endif
#if !defined _POSIX_C_SOURCE && !defined _POSIX_SOURCE && defined VMS
#if defined _REGEX_SOURCE && defined VMS
/* VMS doesn't have `size_t' in <sys/types.h>, even though POSIX says it
should be there. */
# include <stddef.h>
#endif
#ifdef _REGEX_LARGE_OFFSETS
/* Use types and values that are wide enough to represent signed and
unsigned byte offsets in memory. This currently works only when
the regex code is used outside of the GNU C library; it is not yet
supported within glibc itself, and glibc users should not define
_REGEX_LARGE_OFFSETS. */
/* The type of the offset of a byte within a string.
For historical reasons POSIX 1003.1-2004 requires that regoff_t be
at least as wide as off_t. This is a bit odd (and many common
POSIX platforms set it to the more-sensible ssize_t) but we might
as well conform. We don't know of any hosts where ssize_t is wider
than off_t, so off_t is safe. */
typedef off_t regoff_t;
/* The type of nonnegative object indexes. Traditionally, GNU regex
uses 'int' for these. Code that uses __re_idx_t should work
regardless of whether the type is signed. */
typedef size_t __re_idx_t;
/* The type of object sizes. */
typedef size_t __re_size_t;
/* The type of object sizes, in places where the traditional code
uses unsigned long int. */
typedef size_t __re_long_size_t;
#else
/* Use types that are binary-compatible with the traditional GNU regex
implementation, which mishandles strings longer than INT_MAX. */
typedef int regoff_t;
typedef int __re_idx_t;
typedef unsigned int __re_size_t;
typedef unsigned long int __re_long_size_t;
#endif
/* The following two types have to be signed and unsigned integer type
wide enough to hold a value of a pointer. For most ANSI compilers
ptrdiff_t and size_t should be likely OK. Still size of these two
@ -53,18 +99,18 @@ typedef unsigned long int reg_syntax_t;
/* If this bit is not set, then \ inside a bracket expression is literal.
If set, then such a \ quotes the following character. */
#define RE_BACKSLASH_ESCAPE_IN_LISTS ((unsigned long int) 1)
#define REG_BACKSLASH_ESCAPE_IN_LISTS 1ul
/* If this bit is not set, then + and ? are operators, and \+ and \? are
literals.
If set, then \+ and \? are operators and + and ? are literals. */
#define RE_BK_PLUS_QM (RE_BACKSLASH_ESCAPE_IN_LISTS << 1)
#define REG_BK_PLUS_QM (1ul << 1)
/* If this bit is set, then character classes are supported. They are:
[:alpha:], [:upper:], [:lower:], [:digit:], [:alnum:], [:xdigit:],
[:space:], [:print:], [:punct:], [:graph:], and [:cntrl:].
If not set, then character classes are not supported. */
#define RE_CHAR_CLASSES (RE_BK_PLUS_QM << 1)
#define REG_CHAR_CLASSES (1ul << 2)
/* If this bit is set, then ^ and $ are always anchors (outside bracket
expressions, of course).
@ -74,11 +120,11 @@ typedef unsigned long int reg_syntax_t;
$ is an anchor if it is at the end of a regular expression, or
before a close-group or an alternation operator.
This bit could be (re)combined with RE_CONTEXT_INDEP_OPS, because
This bit could be (re)combined with REG_CONTEXT_INDEP_OPS, because
POSIX draft 11.2 says that * etc. in leading positions is undefined.
We already implemented a previous draft which made those constructs
invalid, though, so we haven't changed the code back. */
#define RE_CONTEXT_INDEP_ANCHORS (RE_CHAR_CLASSES << 1)
#define REG_CONTEXT_INDEP_ANCHORS (1ul << 3)
/* If this bit is set, then special characters are always special
regardless of where they are in the pattern.
@ -86,70 +132,70 @@ typedef unsigned long int reg_syntax_t;
some contexts; otherwise they are ordinary. Specifically,
* + ? and intervals are only special when not after the beginning,
open-group, or alternation operator. */
#define RE_CONTEXT_INDEP_OPS (RE_CONTEXT_INDEP_ANCHORS << 1)
#define REG_CONTEXT_INDEP_OPS (1ul << 4)
/* If this bit is set, then *, +, ?, and { cannot be first in an re or
immediately after an alternation or begin-group operator. */
#define RE_CONTEXT_INVALID_OPS (RE_CONTEXT_INDEP_OPS << 1)
#define REG_CONTEXT_INVALID_OPS (1ul << 5)
/* If this bit is set, then . matches newline.
If not set, then it doesn't. */
#define RE_DOT_NEWLINE (RE_CONTEXT_INVALID_OPS << 1)
#define REG_DOT_NEWLINE (1ul << 6)
/* If this bit is set, then . doesn't match NUL.
If not set, then it does. */
#define RE_DOT_NOT_NULL (RE_DOT_NEWLINE << 1)
#define REG_DOT_NOT_NULL (1ul << 7)
/* If this bit is set, nonmatching lists [^...] do not match newline.
If not set, they do. */
#define RE_HAT_LISTS_NOT_NEWLINE (RE_DOT_NOT_NULL << 1)
#define REG_HAT_LISTS_NOT_NEWLINE (1ul << 8)
/* If this bit is set, either \{...\} or {...} defines an
interval, depending on RE_NO_BK_BRACES.
interval, depending on REG_NO_BK_BRACES.
If not set, \{, \}, {, and } are literals. */
#define RE_INTERVALS (RE_HAT_LISTS_NOT_NEWLINE << 1)
#define REG_INTERVALS (1ul << 9)
/* If this bit is set, +, ? and | aren't recognized as operators.
If not set, they are. */
#define RE_LIMITED_OPS (RE_INTERVALS << 1)
#define REG_LIMITED_OPS (1ul << 10)
/* If this bit is set, newline is an alternation operator.
If not set, newline is literal. */
#define RE_NEWLINE_ALT (RE_LIMITED_OPS << 1)
#define REG_NEWLINE_ALT (1ul << 11)
/* If this bit is set, then `{...}' defines an interval, and \{ and \}
are literals.
If not set, then `\{...\}' defines an interval. */
#define RE_NO_BK_BRACES (RE_NEWLINE_ALT << 1)
#define REG_NO_BK_BRACES (1ul << 12)
/* If this bit is set, (...) defines a group, and \( and \) are literals.
If not set, \(...\) defines a group, and ( and ) are literals. */
#define RE_NO_BK_PARENS (RE_NO_BK_BRACES << 1)
#define REG_NO_BK_PARENS (1ul << 13)
/* If this bit is set, then \<digit> matches <digit>.
If not set, then \<digit> is a back-reference. */
#define RE_NO_BK_REFS (RE_NO_BK_PARENS << 1)
#define REG_NO_BK_REFS (1ul << 14)
/* If this bit is set, then | is an alternation operator, and \| is literal.
If not set, then \| is an alternation operator, and | is literal. */
#define RE_NO_BK_VBAR (RE_NO_BK_REFS << 1)
#define REG_NO_BK_VBAR (1ul << 15)
/* If this bit is set, then an ending range point collating higher
than the starting range point, as in [z-a], is invalid.
If not set, the containing range is empty and does not match any string. */
#define RE_NO_EMPTY_RANGES (RE_NO_BK_VBAR << 1)
#define REG_NO_EMPTY_RANGES (1ul << 16)
/* If this bit is set, then an unmatched ) is ordinary.
If not set, then an unmatched ) is invalid. */
#define RE_UNMATCHED_RIGHT_PAREN_ORD (RE_NO_EMPTY_RANGES << 1)
#define REG_UNMATCHED_RIGHT_PAREN_ORD (1ul << 17)
/* If this bit is set, succeed as soon as we match the whole pattern,
without further backtracking. */
#define RE_NO_POSIX_BACKTRACKING (RE_UNMATCHED_RIGHT_PAREN_ORD << 1)
#define REG_NO_POSIX_BACKTRACKING (1ul << 18)
/* If this bit is set, do not process the GNU regex operators.
If not set, then the GNU regex operators are recognized. */
#define RE_NO_GNU_OPS (RE_NO_POSIX_BACKTRACKING << 1)
#define REG_NO_GNU_OPS (1ul << 19)
/* If this bit is set, turn on internal regex debugging.
If not set, and debugging was on, turn it off.
@ -157,29 +203,29 @@ typedef unsigned long int reg_syntax_t;
We define this bit always, so that all that's needed to turn on
debugging is to recompile regex.c; the calling code can always have
this bit set, and it won't affect anything in the normal case. */
#define RE_DEBUG (RE_NO_GNU_OPS << 1)
#define REG_DEBUG (1ul << 20)
/* If this bit is set, a syntactically invalid interval is treated as
a string of ordinary characters. For example, the ERE 'a{1' is
treated as 'a\{1'. */
#define RE_INVALID_INTERVAL_ORD (RE_DEBUG << 1)
#define REG_INVALID_INTERVAL_ORD (1ul << 21)
/* If this bit is set, then ignore case when matching.
If not set, then case is significant. */
#define RE_ICASE (RE_INVALID_INTERVAL_ORD << 1)
#define REG_IGNORE_CASE (1ul << 22)
/* This bit is used internally like RE_CONTEXT_INDEP_ANCHORS but only
/* This bit is used internally like REG_CONTEXT_INDEP_ANCHORS but only
for ^, because it is difficult to scan the regex backwards to find
whether ^ should be special. */
#define RE_CARET_ANCHORS_HERE (RE_ICASE << 1)
#define REG_CARET_ANCHORS_HERE (1ul << 23)
/* If this bit is set, then \{ cannot be first in an bre or
immediately after an alternation or begin-group operator. */
#define RE_CONTEXT_INVALID_DUP (RE_CARET_ANCHORS_HERE << 1)
#define REG_CONTEXT_INVALID_DUP (1ul << 24)
/* If this bit is set, then no_sub will be set to 1 during
re_compile_pattern. */
#define RE_NO_SUB (RE_CONTEXT_INVALID_DUP << 1)
#define REG_NO_SUB (1ul << 25)
/* This global variable defines the particular regexp syntax to use (for
some interfaces). When a regexp is compiled, the syntax used is
@ -191,81 +237,78 @@ extern reg_syntax_t re_syntax_options;
(The [[[ comments delimit what gets put into the Texinfo file, so
don't delete them!) */
/* [[[begin syntaxes]]] */
#define RE_SYNTAX_EMACS 0
#define REG_SYNTAX_EMACS 0
#define RE_SYNTAX_AWK \
(RE_BACKSLASH_ESCAPE_IN_LISTS | RE_DOT_NOT_NULL \
| RE_NO_BK_PARENS | RE_NO_BK_REFS \
| RE_NO_BK_VBAR | RE_NO_EMPTY_RANGES \
| RE_DOT_NEWLINE | RE_CONTEXT_INDEP_ANCHORS \
| RE_UNMATCHED_RIGHT_PAREN_ORD | RE_NO_GNU_OPS)
#define REG_SYNTAX_AWK \
(REG_BACKSLASH_ESCAPE_IN_LISTS | REG_DOT_NOT_NULL \
| REG_NO_BK_PARENS | REG_NO_BK_REFS \
| REG_NO_BK_VBAR | REG_NO_EMPTY_RANGES \
| REG_DOT_NEWLINE | REG_CONTEXT_INDEP_ANCHORS \
| REG_UNMATCHED_RIGHT_PAREN_ORD | REG_NO_GNU_OPS)
#define RE_SYNTAX_GNU_AWK \
((RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS | RE_DEBUG) \
& ~(RE_DOT_NOT_NULL | RE_INTERVALS | RE_CONTEXT_INDEP_OPS \
| RE_CONTEXT_INVALID_OPS ))
#define REG_SYNTAX_GNU_AWK \
((REG_SYNTAX_POSIX_EXTENDED | REG_BACKSLASH_ESCAPE_IN_LISTS \
| REG_DEBUG) \
& ~(REG_DOT_NOT_NULL | REG_INTERVALS | REG_CONTEXT_INDEP_OPS \
| REG_CONTEXT_INVALID_OPS ))
#define RE_SYNTAX_POSIX_AWK \
(RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS \
| RE_INTERVALS | RE_NO_GNU_OPS)
#define REG_SYNTAX_POSIX_AWK \
(REG_SYNTAX_POSIX_EXTENDED | REG_BACKSLASH_ESCAPE_IN_LISTS \
| REG_INTERVALS | REG_NO_GNU_OPS)
#define RE_SYNTAX_GREP \
(RE_BK_PLUS_QM | RE_CHAR_CLASSES \
| RE_HAT_LISTS_NOT_NEWLINE | RE_INTERVALS \
| RE_NEWLINE_ALT)
#define REG_SYNTAX_GREP \
(REG_BK_PLUS_QM | REG_CHAR_CLASSES \
| REG_HAT_LISTS_NOT_NEWLINE | REG_INTERVALS \
| REG_NEWLINE_ALT)
#define RE_SYNTAX_EGREP \
(RE_CHAR_CLASSES | RE_CONTEXT_INDEP_ANCHORS \
| RE_CONTEXT_INDEP_OPS | RE_HAT_LISTS_NOT_NEWLINE \
| RE_NEWLINE_ALT | RE_NO_BK_PARENS \
| RE_NO_BK_VBAR)
#define REG_SYNTAX_EGREP \
(REG_CHAR_CLASSES | REG_CONTEXT_INDEP_ANCHORS \
| REG_CONTEXT_INDEP_OPS | REG_HAT_LISTS_NOT_NEWLINE \
| REG_NEWLINE_ALT | REG_NO_BK_PARENS \
| REG_NO_BK_VBAR)
#define RE_SYNTAX_POSIX_EGREP \
(RE_SYNTAX_EGREP | RE_INTERVALS | RE_NO_BK_BRACES \
| RE_INVALID_INTERVAL_ORD)
#define REG_SYNTAX_POSIX_EGREP \
(REG_SYNTAX_EGREP | REG_INTERVALS | REG_NO_BK_BRACES \
| REG_INVALID_INTERVAL_ORD)
/* P1003.2/D11.2, section 4.20.7.1, lines 5078ff. */
#define RE_SYNTAX_ED RE_SYNTAX_POSIX_BASIC
#define REG_SYNTAX_ED REG_SYNTAX_POSIX_BASIC
#define RE_SYNTAX_SED RE_SYNTAX_POSIX_BASIC
#define REG_SYNTAX_SED REG_SYNTAX_POSIX_BASIC
/* Syntax bits common to both basic and extended POSIX regex syntax. */
#define _RE_SYNTAX_POSIX_COMMON \
(RE_CHAR_CLASSES | RE_DOT_NEWLINE | RE_DOT_NOT_NULL \
| RE_INTERVALS | RE_NO_EMPTY_RANGES)
#define _REG_SYNTAX_POSIX_COMMON \
(REG_CHAR_CLASSES | REG_DOT_NEWLINE | REG_DOT_NOT_NULL \
| REG_INTERVALS | REG_NO_EMPTY_RANGES)
#define RE_SYNTAX_POSIX_BASIC \
(_RE_SYNTAX_POSIX_COMMON | RE_BK_PLUS_QM | RE_CONTEXT_INVALID_DUP)
#define REG_SYNTAX_POSIX_BASIC \
(_REG_SYNTAX_POSIX_COMMON | REG_BK_PLUS_QM | REG_CONTEXT_INVALID_DUP)
/* Differs from ..._POSIX_BASIC only in that RE_BK_PLUS_QM becomes
RE_LIMITED_OPS, i.e., \? \+ \| are not recognized. Actually, this
/* Differs from ..._POSIX_BASIC only in that REG_BK_PLUS_QM becomes
REG_LIMITED_OPS, i.e., \? \+ \| are not recognized. Actually, this
isn't minimal, since other operators, such as \`, aren't disabled. */
#define RE_SYNTAX_POSIX_MINIMAL_BASIC \
(_RE_SYNTAX_POSIX_COMMON | RE_LIMITED_OPS)
#define REG_SYNTAX_POSIX_MINIMAL_BASIC \
(_REG_SYNTAX_POSIX_COMMON | REG_LIMITED_OPS)
#define RE_SYNTAX_POSIX_EXTENDED \
(_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \
| RE_CONTEXT_INDEP_OPS | RE_NO_BK_BRACES \
| RE_NO_BK_PARENS | RE_NO_BK_VBAR \
| RE_CONTEXT_INVALID_OPS | RE_UNMATCHED_RIGHT_PAREN_ORD)
#define REG_SYNTAX_POSIX_EXTENDED \
(_REG_SYNTAX_POSIX_COMMON | REG_CONTEXT_INDEP_ANCHORS \
| REG_CONTEXT_INDEP_OPS | REG_NO_BK_BRACES \
| REG_NO_BK_PARENS | REG_NO_BK_VBAR \
| REG_CONTEXT_INVALID_OPS | REG_UNMATCHED_RIGHT_PAREN_ORD)
/* Differs from ..._POSIX_EXTENDED in that RE_CONTEXT_INDEP_OPS is
removed and RE_NO_BK_REFS is added. */
#define RE_SYNTAX_POSIX_MINIMAL_EXTENDED \
(_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \
| RE_CONTEXT_INVALID_OPS | RE_NO_BK_BRACES \
| RE_NO_BK_PARENS | RE_NO_BK_REFS \
| RE_NO_BK_VBAR | RE_UNMATCHED_RIGHT_PAREN_ORD)
/* Differs from ..._POSIX_EXTENDED in that REG_CONTEXT_INDEP_OPS is
removed and REG_NO_BK_REFS is added. */
#define REG_SYNTAX_POSIX_MINIMAL_EXTENDED \
(_REG_SYNTAX_POSIX_COMMON | REG_CONTEXT_INDEP_ANCHORS \
| REG_CONTEXT_INVALID_OPS | REG_NO_BK_BRACES \
| REG_NO_BK_PARENS | REG_NO_BK_REFS \
| REG_NO_BK_VBAR | REG_UNMATCHED_RIGHT_PAREN_ORD)
/* [[[end syntaxes]]] */
/* Maximum number of duplicates an interval can allow. Some systems
(erroneously) define this in other header files, but we want our
value, so remove any previous define. */
#ifdef RE_DUP_MAX
# undef RE_DUP_MAX
#endif
/* If sizeof(int) == 2, then ((1 << 15) - 1) overflows. */
#define RE_DUP_MAX (0x7fff)
/* Maximum number of duplicates an interval can allow. This is
distinct from RE_DUP_MAX, to conform to POSIX name space rules and
to avoid collisions with <limits.h>. */
#define REG_DUP_MAX 32767
/* POSIX `cflags' bits (i.e., information for `regcomp'). */
@ -276,16 +319,16 @@ extern reg_syntax_t re_syntax_options;
/* If this bit is set, then ignore case when matching.
If not set, then case is significant. */
#define REG_ICASE (REG_EXTENDED << 1)
#define REG_ICASE (1 << 1)
/* If this bit is set, then anchors do not match at newline
characters in the string.
If not set, then anchors do match at newlines. */
#define REG_NEWLINE (REG_ICASE << 1)
#define REG_NEWLINE (1 << 2)
/* If this bit is set, then report only success or fail in regexec.
If not set, then returns differ between not matching and errors. */
#define REG_NOSUB (REG_NEWLINE << 1)
#define REG_NOSUB (1 << 3)
/* POSIX `eflags' bits (i.e., information for regexec). */
@ -306,74 +349,131 @@ extern reg_syntax_t re_syntax_options;
/* If any error codes are removed, changed, or added, update the
`re_error_msg' table in regex.c. */
`__re_error_msgid' table in regcomp.c. */
typedef enum
{
#ifdef _XOPEN_SOURCE
REG_ENOSYS = -1, /* This will never happen for this implementation. */
#endif
_REG_ENOSYS = -1, /* This will never happen for this implementation. */
#define REG_ENOSYS _REG_ENOSYS
REG_NOERROR = 0, /* Success. */
REG_NOMATCH, /* Didn't find a match (for regexec). */
_REG_NOERROR, /* Success. */
#define REG_NOERROR _REG_NOERROR
_REG_NOMATCH, /* Didn't find a match (for regexec). */
#define REG_NOMATCH _REG_NOMATCH
/* POSIX regcomp return error codes. (In the order listed in the
standard.) */
REG_BADPAT, /* Invalid pattern. */
REG_ECOLLATE, /* Inalid collating element. */
REG_ECTYPE, /* Invalid character class name. */
REG_EESCAPE, /* Trailing backslash. */
REG_ESUBREG, /* Invalid back reference. */
REG_EBRACK, /* Unmatched left bracket. */
REG_EPAREN, /* Parenthesis imbalance. */
REG_EBRACE, /* Unmatched \{. */
REG_BADBR, /* Invalid contents of \{\}. */
REG_ERANGE, /* Invalid range end. */
REG_ESPACE, /* Ran out of memory. */
REG_BADRPT, /* No preceding re for repetition op. */
_REG_BADPAT, /* Invalid pattern. */
#define REG_BADPAT _REG_BADPAT
_REG_ECOLLATE, /* Inalid collating element. */
#define REG_ECOLLATE _REG_ECOLLATE
_REG_ECTYPE, /* Invalid character class name. */
#define REG_ECTYPE _REG_ECTYPE
_REG_EESCAPE, /* Trailing backslash. */
#define REG_EESCAPE _REG_EESCAPE
_REG_ESUBREG, /* Invalid back reference. */
#define REG_ESUBREG _REG_ESUBREG
_REG_EBRACK, /* Unmatched left bracket. */
#define REG_EBRACK _REG_EBRACK
_REG_EPAREN, /* Parenthesis imbalance. */
#define REG_EPAREN _REG_EPAREN
_REG_EBRACE, /* Unmatched \{. */
#define REG_EBRACE _REG_EBRACE
_REG_BADBR, /* Invalid contents of \{\}. */
#define REG_BADBR _REG_BADBR
_REG_ERANGE, /* Invalid range end. */
#define REG_ERANGE _REG_ERANGE
_REG_ESPACE, /* Ran out of memory. */
#define REG_ESPACE _REG_ESPACE
_REG_BADRPT, /* No preceding re for repetition op. */
#define REG_BADRPT _REG_BADRPT
/* Error codes we've added. */
REG_EEND, /* Premature end. */
REG_ESIZE, /* Compiled pattern bigger than 2^16 bytes. */
REG_ERPAREN /* Unmatched ) or \); not returned from regcomp. */
_REG_EEND, /* Premature end. */
#define REG_EEND _REG_EEND
_REG_ESIZE, /* Compiled pattern bigger than 2^16 bytes. */
#define REG_ESIZE _REG_ESIZE
_REG_ERPAREN /* Unmatched ) or \); not returned from regcomp. */
#define REG_ERPAREN _REG_ERPAREN
} reg_errcode_t;
/* In the traditional GNU implementation, regex.h defined member names
like `buffer' that POSIX does not allow. These members now have
names with leading `re_' (e.g., `re_buffer'). Support the old
names only if _REGEX_SOURCE is defined. New programs should use
the new names. */
#ifdef _REGEX_SOURCE
# define _REG_RE_NAME(id) id
# define _REG_RM_NAME(id) id
#else
# define _REG_RE_NAME(id) re_##id
# define _REG_RM_NAME(id) rm_##id
#endif
/* The user can specify the type of the re_translate member by
defining the macro REG_TRANSLATE_TYPE. In the traditional GNU
implementation, this macro was named RE_TRANSLATE_TYPE, but POSIX
does not allow this. Support the old name only if _REGEX_SOURCE
and if the new name is not defined. New programs should use the new
name. */
#ifndef REG_TRANSLATE_TYPE
# if defined _REGEX_SOURCE && defined RE_TRANSLATE_TYPE
# define REG_TRANSLATE_TYPE RE_TRANSLATE_TYPE
# else
# define REG_TRANSLATE_TYPE char *
# endif
#endif
/* This data structure represents a compiled pattern. Before calling
the pattern compiler, the fields `buffer', `allocated', `fastmap',
`translate', and `no_sub' can be set. After the pattern has been
the pattern compiler), the fields `re_buffer', `re_allocated', `re_fastmap',
`re_translate', and `re_no_sub' can be set. After the pattern has been
compiled, the `re_nsub' field is available. All other fields are
private to the regex routines. */
#ifndef RE_TRANSLATE_TYPE
# define RE_TRANSLATE_TYPE char *
#endif
struct re_pattern_buffer
{
/* [[[begin pattern_buffer]]] */
/* Space that holds the compiled pattern. It is declared as
`unsigned char *' because its elements are
sometimes used as array indexes. */
unsigned char *buffer;
unsigned char *_REG_RE_NAME (buffer);
/* Number of bytes to which `buffer' points. */
unsigned long int allocated;
/* Number of bytes to which `re_buffer' points. */
__re_long_size_t _REG_RE_NAME (allocated);
/* Number of bytes actually used in `buffer'. */
unsigned long int used;
/* Number of bytes actually used in `re_buffer'. */
__re_long_size_t _REG_RE_NAME (used);
/* Syntax setting with which the pattern was compiled. */
reg_syntax_t syntax;
reg_syntax_t _REG_RE_NAME (syntax);
/* Pointer to a fastmap, if any, otherwise zero. re_search uses
the fastmap, if there is one, to skip over impossible
starting points for matches. */
char *fastmap;
char *_REG_RE_NAME (fastmap);
/* Either a translate table to apply to all characters before
comparing them, or zero for no translation. The translation
is applied to a pattern when it is compiled and to a string
when it is matched. */
RE_TRANSLATE_TYPE translate;
REG_TRANSLATE_TYPE _REG_RE_NAME (translate);
/* Number of subexpressions found by the compiler. */
size_t re_nsub;
@ -383,59 +483,55 @@ struct re_pattern_buffer
whether or not we should use the fastmap, so we don't set
this absolutely perfectly; see `re_compile_fastmap' (the
`duplicate' case). */
unsigned can_be_null : 1;
unsigned int _REG_RE_NAME (can_be_null) : 1;
/* If REGS_UNALLOCATED, allocate space in the `regs' structure
for `max (RE_NREGS, re_nsub + 1)' groups.
If REGS_REALLOCATE, reallocate space if necessary.
If REGS_FIXED, use what's there. */
#define REGS_UNALLOCATED 0
#define REGS_REALLOCATE 1
#define REGS_FIXED 2
unsigned regs_allocated : 2;
/* If REG_UNALLOCATED, allocate space in the `regs' structure
for `max (REG_NREGS, re_nsub + 1)' groups.
If REG_REALLOCATE, reallocate space if necessary.
If REG_FIXED, use what's there. */
#define REG_UNALLOCATED 0
#define REG_REALLOCATE 1
#define REG_FIXED 2
unsigned int _REG_RE_NAME (regs_allocated) : 2;
/* Set to zero when `regex_compile' compiles a pattern; set to one
by `re_compile_fastmap' if it updates the fastmap. */
unsigned fastmap_accurate : 1;
unsigned int _REG_RE_NAME (fastmap_accurate) : 1;
/* If set, `re_match_2' does not return information about
subexpressions. */
unsigned no_sub : 1;
unsigned int _REG_RE_NAME (no_sub) : 1;
/* If set, a beginning-of-line anchor doesn't match at the
beginning of the string. */
unsigned not_bol : 1;
unsigned int _REG_RE_NAME (not_bol) : 1;
/* Similarly for an end-of-line anchor. */
unsigned not_eol : 1;
unsigned int _REG_RE_NAME (not_eol) : 1;
/* If true, an anchor at a newline matches. */
unsigned newline_anchor : 1;
unsigned int _REG_RE_NAME (newline_anchor) : 1;
/* [[[end pattern_buffer]]] */
};
typedef struct re_pattern_buffer regex_t;
/* Type for byte offsets within the string. POSIX mandates this. */
typedef int regoff_t;
/* This is the structure we store register match data in. See
regex.texinfo for a full description of what registers match. */
struct re_registers
{
unsigned num_regs;
regoff_t *start;
regoff_t *end;
__re_size_t _REG_RM_NAME (num_regs);
regoff_t *_REG_RM_NAME (start);
regoff_t *_REG_RM_NAME (end);
};
/* If `regs_allocated' is REGS_UNALLOCATED in the pattern buffer,
/* If `regs_allocated' is REG_UNALLOCATED in the pattern buffer,
`re_match_2' returns information about at least this many registers
the first time a `regs' structure is passed. */
#ifndef RE_NREGS
# define RE_NREGS 30
#ifndef REG_NREGS
# define REG_NREGS 30
#endif
@ -452,49 +548,55 @@ typedef struct
/* Sets the current default syntax to SYNTAX, and return the old syntax.
You can also simply assign to the `re_syntax_options' variable. */
extern reg_syntax_t re_set_syntax (reg_syntax_t syntax);
extern reg_syntax_t re_set_syntax (reg_syntax_t __syntax);
/* Compile the regular expression PATTERN, with length LENGTH
and syntax given by the global `re_syntax_options', into the buffer
BUFFER. Return NULL if successful, and an error string if not. */
extern const char *re_compile_pattern (const char *pattern, size_t length,
struct re_pattern_buffer *buffer);
extern const char *re_compile_pattern (const char *__pattern, size_t __length,
struct re_pattern_buffer *__buffer);
/* Compile a fastmap for the compiled pattern in BUFFER; used to
accelerate searches. Return 0 if successful and -2 if was an
internal error. */
extern int re_compile_fastmap (struct re_pattern_buffer *buffer);
extern int re_compile_fastmap (struct re_pattern_buffer *__buffer);
/* Search in the string STRING (with length LENGTH) for the pattern
compiled into BUFFER. Start searching at position START, for RANGE
characters. Return the starting position of the match, -1 for no
match, or -2 for an internal error. Also return register
information in REGS (if REGS and BUFFER->no_sub are nonzero). */
extern int re_search (struct re_pattern_buffer *buffer, const char *string,
int length, int start, int range,
struct re_registers *regs);
information in REGS (if REGS and BUFFER->re_no_sub are nonzero). */
extern regoff_t re_search (struct re_pattern_buffer *__buffer,
const char *__string, __re_idx_t __length,
__re_idx_t __start, regoff_t __range,
struct re_registers *__regs);
/* Like `re_search', but search in the concatenation of STRING1 and
STRING2. Also, stop searching at index START + STOP. */
extern int re_search_2 (struct re_pattern_buffer *buffer, const char *string1,
int length1, const char *string2, int length2,
int start, int range, struct re_registers *regs,
int stop);
extern regoff_t re_search_2 (struct re_pattern_buffer *__buffer,
const char *__string1, __re_idx_t __length1,
const char *__string2, __re_idx_t __length2,
__re_idx_t __start, regoff_t __range,
struct re_registers *__regs,
__re_idx_t __stop);
/* Like `re_search', but return how many characters in STRING the regexp
in BUFFER matched, starting at position START. */
extern int re_match (struct re_pattern_buffer *buffer, const char *string,
int length, int start, struct re_registers *regs);
extern regoff_t re_match (struct re_pattern_buffer *__buffer,
const char *__string, __re_idx_t __length,
__re_idx_t __start, struct re_registers *__regs);
/* Relates to `re_match' as `re_search_2' relates to `re_search'. */
extern int re_match_2 (struct re_pattern_buffer *buffer, const char *string1,
int length1, const char *string2, int length2,
int start, struct re_registers *regs, int stop);
extern regoff_t re_match_2 (struct re_pattern_buffer *__buffer,
const char *__string1, __re_idx_t __length1,
const char *__string2, __re_idx_t __length2,
__re_idx_t __start, struct re_registers *__regs,
__re_idx_t __stop);
/* Set REGS to hold NUM_REGS registers, storing them in STARTS and
@ -509,9 +611,10 @@ extern int re_match_2 (struct re_pattern_buffer *buffer, const char *string1,
Unless this function is called, the first search or match using
PATTERN_BUFFER will allocate its own register data, without
freeing the old data. */
extern void re_set_registers (struct re_pattern_buffer *buffer,
struct re_registers *regs, unsigned num_regs,
regoff_t *starts, regoff_t *ends);
extern void re_set_registers (struct re_pattern_buffer *__buffer,
struct re_registers *__regs,
__re_size_t __num_regs,
regoff_t *__starts, regoff_t *__ends);
#if defined _REGEX_RE_COMP || defined _LIBC
# ifndef _CRAY
@ -551,12 +654,106 @@ extern int regexec (const regex_t *__restrict __preg,
regmatch_t __pmatch[__restrict_arr],
int __eflags);
extern size_t regerror (int __errcode, const regex_t *__preg,
char *__errbuf, size_t __errbuf_size);
extern size_t regerror (int __errcode, const regex_t *__restrict __preg,
char *__restrict __errbuf, size_t __errbuf_size);
extern void regfree (regex_t *__preg);
#ifdef _REGEX_SOURCE
/* Define the POSIX-compatible member names in terms of the
incompatible (and deprecated) names established by _REG_RE_NAME.
New programs should use the re_* names. */
# define re_allocated allocated
# define re_buffer buffer
# define re_can_be_null can_be_null
# define re_fastmap fastmap
# define re_fastmap_accurate fastmap_accurate
# define re_newline_anchor newline_anchor
# define re_no_sub no_sub
# define re_not_bol not_bol
# define re_not_eol not_eol
# define re_regs_allocated regs_allocated
# define re_syntax syntax
# define re_translate translate
# define re_used used
/* Similarly for _REG_RM_NAME. */
# define rm_end end
# define rm_num_regs num_regs
# define rm_start start
/* Undef RE_DUP_MAX first, in case the user has already included a
<limits.h> with an incompatible definition.
On GNU systems, the most common spelling for RE_DUP_MAX's value in
<limits.h> is (0x7ffff), so define RE_DUP_MAX to that, not to
REG_DUP_MAX. This avoid some duplicate-macro-definition warnings
with programs that include <limits.h> after this file.
New programs should not assume that regex.h defines RE_DUP_MAX; to
get the value of RE_DUP_MAX, they should instead include <limits.h>
and possibly invoke the sysconf function. */
# undef RE_DUP_MAX
# define RE_DUP_MAX (0x7fff)
/* Define the following symbols for backward source compatibility.
These symbols violate the POSIX name space rules, and new programs
should avoid them. */
# define REGS_FIXED REG_FIXED
# define REGS_REALLOCATE REG_REALLOCATE
# define REGS_UNALLOCATED REG_UNALLOCATED
# define RE_BACKSLASH_ESCAPE_IN_LISTS REG_BACKSLASH_ESCAPE_IN_LISTS
# define RE_BK_PLUS_QM REG_BK_PLUS_QM
# define RE_CARET_ANCHORS_HERE REG_CARET_ANCHORS_HERE
# define RE_CHAR_CLASSES REG_CHAR_CLASSES
# define RE_CONTEXT_INDEP_ANCHORS REG_CONTEXT_INDEP_ANCHORS
# define RE_CONTEXT_INDEP_OPS REG_CONTEXT_INDEP_OPS
# define RE_CONTEXT_INVALID_DUP REG_CONTEXT_INVALID_DUP
# define RE_CONTEXT_INVALID_OPS REG_CONTEXT_INVALID_OPS
# define RE_DEBUG REG_DEBUG
# define RE_DOT_NEWLINE REG_DOT_NEWLINE
# define RE_DOT_NOT_NULL REG_DOT_NOT_NULL
# define RE_HAT_LISTS_NOT_NEWLINE REG_HAT_LISTS_NOT_NEWLINE
# define RE_ICASE REG_IGNORE_CASE /* avoid collision with REG_ICASE */
# define RE_INTERVALS REG_INTERVALS
# define RE_INVALID_INTERVAL_ORD REG_INVALID_INTERVAL_ORD
# define RE_LIMITED_OPS REG_LIMITED_OPS
# define RE_NEWLINE_ALT REG_NEWLINE_ALT
# define RE_NO_BK_BRACES REG_NO_BK_BRACES
# define RE_NO_BK_PARENS REG_NO_BK_PARENS
# define RE_NO_BK_REFS REG_NO_BK_REFS
# define RE_NO_BK_VBAR REG_NO_BK_VBAR
# define RE_NO_EMPTY_RANGES REG_NO_EMPTY_RANGES
# define RE_NO_GNU_OPS REG_NO_GNU_OPS
# define RE_NO_POSIX_BACKTRACKING REG_NO_POSIX_BACKTRACKING
# define RE_NO_SUB REG_NO_SUB
# define RE_NREGS REG_NREGS
# define RE_SYNTAX_AWK REG_SYNTAX_AWK
# define RE_SYNTAX_ED REG_SYNTAX_ED
# define RE_SYNTAX_EGREP REG_SYNTAX_EGREP
# define RE_SYNTAX_EMACS REG_SYNTAX_EMACS
# define RE_SYNTAX_GNU_AWK REG_SYNTAX_GNU_AWK
# define RE_SYNTAX_GREP REG_SYNTAX_GREP
# define RE_SYNTAX_POSIX_AWK REG_SYNTAX_POSIX_AWK
# define RE_SYNTAX_POSIX_BASIC REG_SYNTAX_POSIX_BASIC
# define RE_SYNTAX_POSIX_EGREP REG_SYNTAX_POSIX_EGREP
# define RE_SYNTAX_POSIX_EXTENDED REG_SYNTAX_POSIX_EXTENDED
# define RE_SYNTAX_POSIX_MINIMAL_BASIC REG_SYNTAX_POSIX_MINIMAL_BASIC
# define RE_SYNTAX_POSIX_MINIMAL_EXTENDED REG_SYNTAX_POSIX_MINIMAL_EXTENDED
# define RE_SYNTAX_SED REG_SYNTAX_SED
# define RE_UNMATCHED_RIGHT_PAREN_ORD REG_UNMATCHED_RIGHT_PAREN_ORD
# ifndef RE_TRANSLATE_TYPE
# define RE_TRANSLATE_TYPE REG_TRANSLATE_TYPE
# endif
#endif /* defined _REGEX_SOURCE */
#ifdef __cplusplus
}
#endif /* C++ */

View File

@ -17,17 +17,17 @@
with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
static void re_string_construct_common (const char *str, int len,
static void re_string_construct_common (const char *str, Idx len,
re_string_t *pstr,
RE_TRANSLATE_TYPE trans, int icase,
REG_TRANSLATE_TYPE trans, bool icase,
const re_dfa_t *dfa) internal_function;
static re_dfastate_t *create_ci_newstate (re_dfa_t *dfa,
static re_dfastate_t *create_ci_newstate (const re_dfa_t *dfa,
const re_node_set *nodes,
unsigned int hash) internal_function;
static re_dfastate_t *create_cd_newstate (re_dfa_t *dfa,
re_hashval_t hash) internal_function;
static re_dfastate_t *create_cd_newstate (const re_dfa_t *dfa,
const re_node_set *nodes,
unsigned int context,
unsigned int hash) internal_function;
re_hashval_t hash) internal_function;
/* Functions for string operation. */
@ -36,11 +36,11 @@ static re_dfastate_t *create_cd_newstate (re_dfa_t *dfa,
static reg_errcode_t
internal_function
re_string_allocate (re_string_t *pstr, const char *str, int len, int init_len,
RE_TRANSLATE_TYPE trans, int icase, const re_dfa_t *dfa)
re_string_allocate (re_string_t *pstr, const char *str, Idx len, Idx init_len,
REG_TRANSLATE_TYPE trans, bool icase, const re_dfa_t *dfa)
{
reg_errcode_t ret;
int init_buf_len;
Idx init_buf_len;
/* Ensure at least one character fits into the buffers. */
if (init_len < dfa->mb_cur_max)
@ -64,8 +64,8 @@ re_string_allocate (re_string_t *pstr, const char *str, int len, int init_len,
static reg_errcode_t
internal_function
re_string_construct (re_string_t *pstr, const char *str, int len,
RE_TRANSLATE_TYPE trans, int icase, const re_dfa_t *dfa)
re_string_construct (re_string_t *pstr, const char *str, Idx len,
REG_TRANSLATE_TYPE trans, bool icase, const re_dfa_t *dfa)
{
reg_errcode_t ret;
memset (pstr, '\0', sizeof (re_string_t));
@ -127,18 +127,18 @@ re_string_construct (re_string_t *pstr, const char *str, int len,
static reg_errcode_t
internal_function
re_string_realloc_buffers (re_string_t *pstr, int new_buf_len)
re_string_realloc_buffers (re_string_t *pstr, Idx new_buf_len)
{
#ifdef RE_ENABLE_I18N
if (pstr->mb_cur_max > 1)
{
wint_t *new_wcs = re_realloc (pstr->wcs, wint_t, new_buf_len);
wint_t *new_wcs = re_xrealloc (pstr->wcs, wint_t, new_buf_len);
if (BE (new_wcs == NULL, 0))
return REG_ESPACE;
pstr->wcs = new_wcs;
if (pstr->offsets != NULL)
{
int *new_offsets = re_realloc (pstr->offsets, int, new_buf_len);
Idx *new_offsets = re_xrealloc (pstr->offsets, Idx, new_buf_len);
if (BE (new_offsets == NULL, 0))
return REG_ESPACE;
pstr->offsets = new_offsets;
@ -160,15 +160,15 @@ re_string_realloc_buffers (re_string_t *pstr, int new_buf_len)
static void
internal_function
re_string_construct_common (const char *str, int len, re_string_t *pstr,
RE_TRANSLATE_TYPE trans, int icase,
re_string_construct_common (const char *str, Idx len, re_string_t *pstr,
REG_TRANSLATE_TYPE trans, bool icase,
const re_dfa_t *dfa)
{
pstr->raw_mbs = (const unsigned char *) str;
pstr->len = len;
pstr->raw_len = len;
pstr->trans = (unsigned RE_TRANSLATE_TYPE) trans;
pstr->icase = icase ? 1 : 0;
pstr->trans = (unsigned REG_TRANSLATE_TYPE) trans;
pstr->icase = icase;
pstr->mbs_allocated = (trans != NULL || icase);
pstr->mb_cur_max = dfa->mb_cur_max;
pstr->is_utf8 = dfa->is_utf8;
@ -201,7 +201,7 @@ build_wcs_buffer (re_string_t *pstr)
unsigned char buf[64];
#endif
mbstate_t prev_st;
int byte_idx, end_idx, remain_len;
Idx byte_idx, end_idx, remain_len;
size_t mbclen;
/* Build the buffers from pstr->valid_len to either pstr->len or
@ -258,12 +258,12 @@ build_wcs_buffer (re_string_t *pstr)
/* Build wide character buffer PSTR->WCS like build_wcs_buffer,
but for REG_ICASE. */
static int
static reg_errcode_t
internal_function
build_wcs_upper_buffer (re_string_t *pstr)
{
mbstate_t prev_st;
int src_idx, byte_idx, end_idx, remain_len;
Idx src_idx, byte_idx, end_idx, remain_len;
size_t mbclen;
#ifdef _LIBC
char buf[MB_LEN_MAX];
@ -301,7 +301,7 @@ build_wcs_upper_buffer (re_string_t *pstr)
mbclen = mbrtowc (&wc,
((const char *) pstr->raw_mbs + pstr->raw_mbs_idx
+ byte_idx), remain_len, &pstr->cur_state);
if (BE (mbclen + 2 > 2, 1))
if (BE ((size_t) (mbclen + 2) > 2, 1))
{
wchar_t wcu = wc;
if (iswlower (wc))
@ -369,7 +369,7 @@ build_wcs_upper_buffer (re_string_t *pstr)
else
p = (const char *) pstr->raw_mbs + pstr->raw_mbs_idx + src_idx;
mbclen = mbrtowc (&wc, p, remain_len, &pstr->cur_state);
if (BE (mbclen + 2 > 2, 1))
if (BE ((size_t) (mbclen + 2) > 2, 1))
{
wchar_t wcu = wc;
if (iswlower (wc))
@ -392,7 +392,7 @@ build_wcs_upper_buffer (re_string_t *pstr)
if (pstr->offsets == NULL)
{
pstr->offsets = re_malloc (int, pstr->bufs_len);
pstr->offsets = re_xmalloc (Idx, pstr->bufs_len);
if (pstr->offsets == NULL)
return REG_ESPACE;
@ -474,12 +474,12 @@ build_wcs_upper_buffer (re_string_t *pstr)
/* Skip characters until the index becomes greater than NEW_RAW_IDX.
Return the index. */
static int
static Idx
internal_function
re_string_skip_chars (re_string_t *pstr, int new_raw_idx, wint_t *last_wc)
re_string_skip_chars (re_string_t *pstr, Idx new_raw_idx, wint_t *last_wc)
{
mbstate_t prev_st;
int rawbuf_idx;
Idx rawbuf_idx;
size_t mbclen;
wchar_t wc = 0;
@ -487,7 +487,7 @@ re_string_skip_chars (re_string_t *pstr, int new_raw_idx, wint_t *last_wc)
for (rawbuf_idx = pstr->raw_mbs_idx + pstr->valid_raw_len;
rawbuf_idx < new_raw_idx;)
{
int remain_len;
Idx remain_len;
remain_len = pstr->len - rawbuf_idx;
prev_st = pstr->cur_state;
mbclen = mbrtowc (&wc, (const char *) pstr->raw_mbs + rawbuf_idx,
@ -513,7 +513,7 @@ static void
internal_function
build_upper_buffer (re_string_t *pstr)
{
int char_idx, end_idx;
Idx char_idx, end_idx;
end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len;
for (char_idx = pstr->valid_len; char_idx < end_idx; ++char_idx)
@ -536,7 +536,7 @@ static void
internal_function
re_string_translate_buffer (re_string_t *pstr)
{
int buf_idx, end_idx;
Idx buf_idx, end_idx;
end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len;
for (buf_idx = pstr->valid_len; buf_idx < end_idx; ++buf_idx)
@ -555,10 +555,13 @@ re_string_translate_buffer (re_string_t *pstr)
static reg_errcode_t
internal_function
re_string_reconstruct (re_string_t *pstr, int idx, int eflags)
re_string_reconstruct (re_string_t *pstr, Idx idx, int eflags)
{
int offset = idx - pstr->raw_mbs_idx;
if (BE (offset < 0, 0))
Idx offset;
if (BE (pstr->raw_mbs_idx <= idx, 0))
offset = idx - pstr->raw_mbs_idx;
else
{
/* Reset buffer. */
#ifdef RE_ENABLE_I18N
@ -621,7 +624,7 @@ re_string_reconstruct (re_string_t *pstr, int idx, int eflags)
#ifdef RE_ENABLE_I18N
if (pstr->mb_cur_max > 1)
{
int wcs_idx;
Idx wcs_idx;
wint_t wc = WEOF;
if (pstr->is_utf8)
@ -637,8 +640,9 @@ re_string_reconstruct (re_string_t *pstr, int idx, int eflags)
{
mbstate_t cur_state;
wchar_t wc2;
int mlen = raw + pstr->len - p;
Idx mlen = raw + pstr->len - p;
unsigned char buf[6];
size_t mbclen;
q = p;
if (BE (pstr->trans != NULL, 0))
@ -651,14 +655,13 @@ re_string_reconstruct (re_string_t *pstr, int idx, int eflags)
/* XXX Don't use mbrtowc, we know which conversion
to use (UTF-8 -> UCS4). */
memset (&cur_state, 0, sizeof (cur_state));
mlen = (mbrtowc (&wc2, (const char *) p, mlen,
&cur_state)
- (raw + offset - p));
if (mlen >= 0)
mbclen = mbrtowc (&wc2, (const char *) p, mlen,
&cur_state);
if (raw + offset - p <= mbclen && mbclen < (size_t) -2)
{
memset (&pstr->cur_state, '\0',
sizeof (mbstate_t));
pstr->valid_len = mlen;
pstr->valid_len = mbclen - (raw + offset - p);
wc = wc2;
}
break;
@ -672,7 +675,7 @@ re_string_reconstruct (re_string_t *pstr, int idx, int eflags)
for (wcs_idx = 0; wcs_idx < pstr->valid_len; ++wcs_idx)
pstr->wcs[wcs_idx] = WEOF;
if (pstr->mbs_allocated)
memset (pstr->mbs, 255, pstr->valid_len);
memset (pstr->mbs, -1, pstr->valid_len);
}
pstr->valid_raw_len = pstr->valid_len;
pstr->tip_context = ((BE (pstr->word_ops_used != 0, 0)
@ -707,7 +710,7 @@ re_string_reconstruct (re_string_t *pstr, int idx, int eflags)
{
if (pstr->icase)
{
int ret = build_wcs_upper_buffer (pstr);
reg_errcode_t ret = build_wcs_upper_buffer (pstr);
if (BE (ret != REG_NOERROR, 0))
return ret;
}
@ -731,10 +734,11 @@ re_string_reconstruct (re_string_t *pstr, int idx, int eflags)
}
static unsigned char
internal_function
re_string_peek_byte_case (const re_string_t *pstr, int idx)
internal_function __attribute ((pure))
re_string_peek_byte_case (const re_string_t *pstr, Idx idx)
{
int ch, off;
int ch;
Idx off;
/* Handle the common (easiest) cases first. */
if (BE (!pstr->mbs_allocated, 1))
@ -767,7 +771,7 @@ re_string_peek_byte_case (const re_string_t *pstr, int idx)
}
static unsigned char
internal_function
internal_function __attribute ((pure))
re_string_fetch_byte_case (re_string_t *pstr)
{
if (BE (!pstr->mbs_allocated, 1))
@ -776,7 +780,8 @@ re_string_fetch_byte_case (re_string_t *pstr)
#ifdef RE_ENABLE_I18N
if (pstr->offsets_needed)
{
int off, ch;
Idx off;
int ch;
/* For tr_TR.UTF-8 [[:islower:]] there is
[[: CAPITAL LETTER I WITH DOT lower:]] in mbs. Skip
@ -819,10 +824,10 @@ re_string_destruct (re_string_t *pstr)
static unsigned int
internal_function
re_string_context_at (const re_string_t *input, int idx, int eflags)
re_string_context_at (const re_string_t *input, Idx idx, int eflags)
{
int c;
if (BE (idx < 0, 0))
if (BE (! REG_VALID_INDEX (idx), 0))
/* In this case, we use the value stored in input->tip_context,
since we can't know the character in input->mbs[-1] here. */
return input->tip_context;
@ -833,15 +838,15 @@ re_string_context_at (const re_string_t *input, int idx, int eflags)
if (input->mb_cur_max > 1)
{
wint_t wc;
int wc_idx = idx;
Idx wc_idx = idx;
while(input->wcs[wc_idx] == WEOF)
{
#ifdef DEBUG
/* It must not happen. */
assert (wc_idx >= 0);
assert (REG_VALID_INDEX (wc_idx));
#endif
--wc_idx;
if (wc_idx < 0)
if (! REG_VALID_INDEX (wc_idx))
return input->tip_context;
}
wc = input->wcs[wc_idx];
@ -864,11 +869,11 @@ re_string_context_at (const re_string_t *input, int idx, int eflags)
static reg_errcode_t
internal_function
re_node_set_alloc (re_node_set *set, int size)
re_node_set_alloc (re_node_set *set, Idx size)
{
set->alloc = size;
set->nelem = 0;
set->elems = re_malloc (int, size);
set->elems = re_xmalloc (Idx, size);
if (BE (set->elems == NULL, 0))
return REG_ESPACE;
return REG_NOERROR;
@ -876,11 +881,11 @@ re_node_set_alloc (re_node_set *set, int size)
static reg_errcode_t
internal_function
re_node_set_init_1 (re_node_set *set, int elem)
re_node_set_init_1 (re_node_set *set, Idx elem)
{
set->alloc = 1;
set->nelem = 1;
set->elems = re_malloc (int, 1);
set->elems = re_malloc (Idx, 1);
if (BE (set->elems == NULL, 0))
{
set->alloc = set->nelem = 0;
@ -892,10 +897,10 @@ re_node_set_init_1 (re_node_set *set, int elem)
static reg_errcode_t
internal_function
re_node_set_init_2 (re_node_set *set, int elem1, int elem2)
re_node_set_init_2 (re_node_set *set, Idx elem1, Idx elem2)
{
set->alloc = 2;
set->elems = re_malloc (int, 2);
set->elems = re_malloc (Idx, 2);
if (BE (set->elems == NULL, 0))
return REG_ESPACE;
if (elem1 == elem2)
@ -928,13 +933,13 @@ re_node_set_init_copy (re_node_set *dest, const re_node_set *src)
if (src->nelem > 0)
{
dest->alloc = dest->nelem;
dest->elems = re_malloc (int, dest->alloc);
dest->elems = re_malloc (Idx, dest->alloc);
if (BE (dest->elems == NULL, 0))
{
dest->alloc = dest->nelem = 0;
return REG_ESPACE;
}
memcpy (dest->elems, src->elems, src->nelem * sizeof (int));
memcpy (dest->elems, src->elems, src->nelem * sizeof dest->elems[0]);
}
else
re_node_set_init_empty (dest);
@ -950,7 +955,7 @@ internal_function
re_node_set_add_intersect (re_node_set *dest, const re_node_set *src1,
const re_node_set *src2)
{
int i1, i2, is, id, delta, sbase;
Idx i1, i2, is, id, delta, sbase;
if (src1->nelem == 0 || src2->nelem == 0)
return REG_NOERROR;
@ -958,8 +963,13 @@ re_node_set_add_intersect (re_node_set *dest, const re_node_set *src1,
conservative estimate. */
if (src1->nelem + src2->nelem + dest->nelem > dest->alloc)
{
int new_alloc = src1->nelem + src2->nelem + dest->alloc;
int *new_elems = re_realloc (dest->elems, int, new_alloc);
Idx new_alloc = src1->nelem + src2->nelem + dest->alloc;
Idx *new_elems;
if (sizeof (Idx) < 3
&& (new_alloc < dest->alloc
|| ((Idx) (src1->nelem + src2->nelem) < src1->nelem)))
return REG_ESPACE;
new_elems = re_xrealloc (dest->elems, Idx, new_alloc);
if (BE (new_elems == NULL, 0))
return REG_ESPACE;
dest->elems = new_elems;
@ -977,25 +987,25 @@ re_node_set_add_intersect (re_node_set *dest, const re_node_set *src1,
if (src1->elems[i1] == src2->elems[i2])
{
/* Try to find the item in DEST. Maybe we could binary search? */
while (id >= 0 && dest->elems[id] > src1->elems[i1])
while (REG_VALID_INDEX (id) && dest->elems[id] > src1->elems[i1])
--id;
if (id < 0 || dest->elems[id] != src1->elems[i1])
if (! REG_VALID_INDEX (id) || dest->elems[id] != src1->elems[i1])
dest->elems[--sbase] = src1->elems[i1];
if (--i1 < 0 || --i2 < 0)
if (! REG_VALID_INDEX (--i1) || ! REG_VALID_INDEX (--i2))
break;
}
/* Lower the highest of the two items. */
else if (src1->elems[i1] < src2->elems[i2])
{
if (--i2 < 0)
if (! REG_VALID_INDEX (--i2))
break;
}
else
{
if (--i1 < 0)
if (! REG_VALID_INDEX (--i1))
break;
}
}
@ -1008,7 +1018,7 @@ re_node_set_add_intersect (re_node_set *dest, const re_node_set *src1,
DEST elements are already in place; this is more or
less the same loop that is in re_node_set_merge. */
dest->nelem += delta;
if (delta > 0 && id >= 0)
if (delta > 0 && REG_VALID_INDEX (id))
for (;;)
{
if (dest->elems[is] > dest->elems[id])
@ -1022,13 +1032,13 @@ re_node_set_add_intersect (re_node_set *dest, const re_node_set *src1,
{
/* Slide from the bottom. */
dest->elems[id + delta] = dest->elems[id];
if (--id < 0)
if (! REG_VALID_INDEX (--id))
break;
}
}
/* Copy remaining SRC elements. */
memcpy (dest->elems, dest->elems + sbase, delta * sizeof (int));
memcpy (dest->elems, dest->elems + sbase, delta * sizeof dest->elems[0]);
return REG_NOERROR;
}
@ -1041,11 +1051,13 @@ internal_function
re_node_set_init_union (re_node_set *dest, const re_node_set *src1,
const re_node_set *src2)
{
int i1, i2, id;
Idx i1, i2, id;
if (src1 != NULL && src1->nelem > 0 && src2 != NULL && src2->nelem > 0)
{
dest->alloc = src1->nelem + src2->nelem;
dest->elems = re_malloc (int, dest->alloc);
if (sizeof (Idx) < 2 && dest->alloc < src1->nelem)
return REG_ESPACE;
dest->elems = re_xmalloc (Idx, dest->alloc);
if (BE (dest->elems == NULL, 0))
return REG_ESPACE;
}
@ -1073,13 +1085,13 @@ re_node_set_init_union (re_node_set *dest, const re_node_set *src1,
if (i1 < src1->nelem)
{
memcpy (dest->elems + id, src1->elems + i1,
(src1->nelem - i1) * sizeof (int));
(src1->nelem - i1) * sizeof dest->elems[0]);
id += src1->nelem - i1;
}
else if (i2 < src2->nelem)
{
memcpy (dest->elems + id, src2->elems + i2,
(src2->nelem - i2) * sizeof (int));
(src2->nelem - i2) * sizeof dest->elems[0]);
id += src2->nelem - i2;
}
dest->nelem = id;
@ -1093,13 +1105,20 @@ static reg_errcode_t
internal_function
re_node_set_merge (re_node_set *dest, const re_node_set *src)
{
int is, id, sbase, delta;
Idx is, id, sbase, delta;
if (src == NULL || src->nelem == 0)
return REG_NOERROR;
if (sizeof (Idx) < 3
&& ((Idx) (2 * src->nelem) < src->nelem
|| (Idx) (2 * src->nelem + dest->nelem) < dest->nelem))
return REG_ESPACE;
if (dest->alloc < 2 * src->nelem + dest->nelem)
{
int new_alloc = 2 * (src->nelem + dest->alloc);
int *new_buffer = re_realloc (dest->elems, int, new_alloc);
Idx new_alloc = src->nelem + dest->alloc;
Idx *new_buffer;
if (sizeof (Idx) < 4 && new_alloc < dest->alloc)
return REG_ESPACE;
new_buffer = re_x2realloc (dest->elems, Idx, &new_alloc);
if (BE (new_buffer == NULL, 0))
return REG_ESPACE;
dest->elems = new_buffer;
@ -1109,14 +1128,15 @@ re_node_set_merge (re_node_set *dest, const re_node_set *src)
if (BE (dest->nelem == 0, 0))
{
dest->nelem = src->nelem;
memcpy (dest->elems, src->elems, src->nelem * sizeof (int));
memcpy (dest->elems, src->elems, src->nelem * sizeof dest->elems[0]);
return REG_NOERROR;
}
/* Copy into the top of DEST the items of SRC that are not
found in DEST. Maybe we could binary search in DEST? */
for (sbase = dest->nelem + 2 * src->nelem,
is = src->nelem - 1, id = dest->nelem - 1; is >= 0 && id >= 0; )
is = src->nelem - 1, id = dest->nelem - 1;
REG_VALID_INDEX (is) && REG_VALID_INDEX (id); )
{
if (dest->elems[id] == src->elems[is])
is--, id--;
@ -1126,11 +1146,12 @@ re_node_set_merge (re_node_set *dest, const re_node_set *src)
--id;
}
if (is >= 0)
if (REG_VALID_INDEX (is))
{
/* If DEST is exhausted, the remaining items of SRC must be unique. */
sbase -= is + 1;
memcpy (dest->elems + sbase, src->elems, (is + 1) * sizeof (int));
memcpy (dest->elems + sbase, src->elems,
(is + 1) * sizeof dest->elems[0]);
}
id = dest->nelem - 1;
@ -1155,11 +1176,11 @@ re_node_set_merge (re_node_set *dest, const re_node_set *src)
{
/* Slide from the bottom. */
dest->elems[id + delta] = dest->elems[id];
if (--id < 0)
if (! REG_VALID_INDEX (--id))
{
/* Copy remaining SRC elements. */
memcpy (dest->elems, dest->elems + sbase,
delta * sizeof (int));
delta * sizeof dest->elems[0]);
break;
}
}
@ -1170,38 +1191,31 @@ re_node_set_merge (re_node_set *dest, const re_node_set *src)
/* Insert the new element ELEM to the re_node_set* SET.
SET should not already have ELEM.
return -1 if an error is occured, return 1 otherwise. */
Return true if successful. */
static int
static bool
internal_function
re_node_set_insert (re_node_set *set, int elem)
re_node_set_insert (re_node_set *set, Idx elem)
{
int idx;
Idx idx;
/* In case the set is empty. */
if (set->alloc == 0)
{
if (BE (re_node_set_init_1 (set, elem) == REG_NOERROR, 1))
return 1;
else
return -1;
}
return re_node_set_init_1 (set, elem) == REG_NOERROR;
if (BE (set->nelem, 0) == 0)
{
/* We already guaranteed above that set->alloc != 0. */
set->elems[0] = elem;
++set->nelem;
return 1;
return true;
}
/* Realloc if we need. */
if (set->alloc == set->nelem)
{
int *new_elems;
set->alloc = set->alloc * 2;
new_elems = re_realloc (set->elems, int, set->alloc);
Idx *new_elems = re_x2realloc (set->elems, Idx, &set->alloc);
if (BE (new_elems == NULL, 0))
return -1;
return false;
set->elems = new_elems;
}
@ -1222,57 +1236,56 @@ re_node_set_insert (re_node_set *set, int elem)
/* Insert the new element. */
set->elems[idx] = elem;
++set->nelem;
return 1;
return true;
}
/* Insert the new element ELEM to the re_node_set* SET.
SET should not already have any element greater than or equal to ELEM.
Return -1 if an error is occured, return 1 otherwise. */
Return true if successful. */
static int
static bool
internal_function
re_node_set_insert_last (re_node_set *set, int elem)
re_node_set_insert_last (re_node_set *set, Idx elem)
{
/* Realloc if we need. */
if (set->alloc == set->nelem)
{
int *new_elems;
set->alloc = (set->alloc + 1) * 2;
new_elems = re_realloc (set->elems, int, set->alloc);
Idx *new_elems;
new_elems = re_x2realloc (set->elems, Idx, &set->alloc);
if (BE (new_elems == NULL, 0))
return -1;
return false;
set->elems = new_elems;
}
/* Insert the new element. */
set->elems[set->nelem++] = elem;
return 1;
return true;
}
/* Compare two node sets SET1 and SET2.
return 1 if SET1 and SET2 are equivalent, return 0 otherwise. */
Return true if SET1 and SET2 are equivalent. */
static int
internal_function
static bool
internal_function __attribute ((pure))
re_node_set_compare (const re_node_set *set1, const re_node_set *set2)
{
int i;
Idx i;
if (set1 == NULL || set2 == NULL || set1->nelem != set2->nelem)
return 0;
for (i = set1->nelem ; --i >= 0 ; )
return false;
for (i = set1->nelem ; REG_VALID_INDEX (--i) ; )
if (set1->elems[i] != set2->elems[i])
return 0;
return 1;
return false;
return true;
}
/* Return (idx + 1) if SET contains the element ELEM, return 0 otherwise. */
static int
internal_function
re_node_set_contains (const re_node_set *set, int elem)
static Idx
internal_function __attribute ((pure))
re_node_set_contains (const re_node_set *set, Idx elem)
{
unsigned int idx, right, mid;
if (set->nelem <= 0)
__re_size_t idx, right, mid;
if (! REG_VALID_NONZERO_INDEX (set->nelem))
return 0;
/* Binary search the element. */
@ -1291,7 +1304,7 @@ re_node_set_contains (const re_node_set *set, int elem)
static void
internal_function
re_node_set_remove_at (re_node_set *set, int idx)
re_node_set_remove_at (re_node_set *set, Idx idx)
{
if (idx < 0 || idx >= set->nelem)
return;
@ -1302,31 +1315,31 @@ re_node_set_remove_at (re_node_set *set, int idx)
/* Add the token TOKEN to dfa->nodes, and return the index of the token.
Or return -1, if an error will be occured. */
Or return REG_MISSING if an error occurred. */
static int
static Idx
internal_function
re_dfa_add_node (re_dfa_t *dfa, re_token_t token)
{
int type = token.type;
if (BE (dfa->nodes_len >= dfa->nodes_alloc, 0))
{
int new_nodes_alloc = dfa->nodes_alloc * 2;
int *new_nexts, *new_indices;
Idx new_nodes_alloc = dfa->nodes_alloc;
Idx *new_nexts, *new_indices;
re_node_set *new_edests, *new_eclosures;
re_token_t *new_nodes = re_realloc (dfa->nodes, re_token_t,
new_nodes_alloc);
re_token_t *new_nodes = re_x2realloc (dfa->nodes, re_token_t,
&new_nodes_alloc);
if (BE (new_nodes == NULL, 0))
return -1;
return REG_MISSING;
dfa->nodes = new_nodes;
new_nexts = re_realloc (dfa->nexts, int, new_nodes_alloc);
new_indices = re_realloc (dfa->org_indices, int, new_nodes_alloc);
new_edests = re_realloc (dfa->edests, re_node_set, new_nodes_alloc);
new_nexts = re_realloc (dfa->nexts, Idx, new_nodes_alloc);
new_indices = re_realloc (dfa->org_indices, Idx, new_nodes_alloc);
new_edests = re_xrealloc (dfa->edests, re_node_set, new_nodes_alloc);
new_eclosures = re_realloc (dfa->eclosures, re_node_set, new_nodes_alloc);
if (BE (new_nexts == NULL || new_indices == NULL
|| new_edests == NULL || new_eclosures == NULL, 0))
return -1;
return REG_MISSING;
dfa->nexts = new_nexts;
dfa->org_indices = new_indices;
dfa->edests = new_edests;
@ -1339,18 +1352,18 @@ re_dfa_add_node (re_dfa_t *dfa, re_token_t token)
dfa->nodes[dfa->nodes_len].accept_mb =
(type == OP_PERIOD && dfa->mb_cur_max > 1) || type == COMPLEX_BRACKET;
#endif
dfa->nexts[dfa->nodes_len] = -1;
dfa->nexts[dfa->nodes_len] = REG_MISSING;
re_node_set_init_empty (dfa->edests + dfa->nodes_len);
re_node_set_init_empty (dfa->eclosures + dfa->nodes_len);
return dfa->nodes_len++;
}
static inline unsigned int
static inline re_hashval_t
internal_function
calc_state_hash (const re_node_set *nodes, unsigned int context)
{
unsigned int hash = nodes->nelem + context;
int i;
re_hashval_t hash = nodes->nelem + context;
Idx i;
for (i = 0 ; i < nodes->nelem ; i++)
hash += nodes->elems[i];
return hash;
@ -1369,10 +1382,10 @@ static re_dfastate_t*
internal_function
re_acquire_state (reg_errcode_t *err, re_dfa_t *dfa, const re_node_set *nodes)
{
unsigned int hash;
re_hashval_t hash;
re_dfastate_t *new_state;
struct re_state_table_entry *spot;
int i;
Idx i;
#ifdef lint
/* Suppress bogus uninitialized-variable warnings. */
*err = REG_NOERROR;
@ -1420,10 +1433,10 @@ internal_function
re_acquire_state_context (reg_errcode_t *err, re_dfa_t *dfa,
const re_node_set *nodes, unsigned int context)
{
unsigned int hash;
re_hashval_t hash;
re_dfastate_t *new_state;
struct re_state_table_entry *spot;
int i;
Idx i;
#ifdef lint
/* Suppress bogus uninitialized-variable warnings. */
*err = REG_NOERROR;
@ -1461,11 +1474,11 @@ re_acquire_state_context (reg_errcode_t *err, re_dfa_t *dfa,
static reg_errcode_t
internal_function
register_state (re_dfa_t *dfa, re_dfastate_t *newstate, unsigned int hash)
register_state (const re_dfa_t *dfa, re_dfastate_t *newstate, re_hashval_t hash)
{
struct re_state_table_entry *spot;
reg_errcode_t err;
int i;
Idx i;
newstate->hash = hash;
err = re_node_set_alloc (&newstate->non_eps_nodes, newstate->nodes.nelem);
@ -1473,17 +1486,21 @@ register_state (re_dfa_t *dfa, re_dfastate_t *newstate, unsigned int hash)
return REG_ESPACE;
for (i = 0; i < newstate->nodes.nelem; i++)
{
int elem = newstate->nodes.elems[i];
Idx elem = newstate->nodes.elems[i];
if (!IS_EPSILON_NODE (dfa->nodes[elem].type))
re_node_set_insert_last (&newstate->non_eps_nodes, elem);
{
bool ok = re_node_set_insert_last (&newstate->non_eps_nodes, elem);
if (BE (! ok, 0))
return REG_ESPACE;
}
}
spot = dfa->state_table + (hash & dfa->state_hash_mask);
if (BE (spot->alloc <= spot->num, 0))
{
int new_alloc = 2 * spot->num + 2;
re_dfastate_t **new_array = re_realloc (spot->array, re_dfastate_t *,
new_alloc);
Idx new_alloc = spot->num;
re_dfastate_t **new_array = re_x2realloc (spot->array, re_dfastate_t *,
&new_alloc);
if (BE (new_array == NULL, 0))
return REG_ESPACE;
spot->array = new_array;
@ -1498,13 +1515,14 @@ register_state (re_dfa_t *dfa, re_dfastate_t *newstate, unsigned int hash)
static re_dfastate_t *
internal_function
create_ci_newstate (re_dfa_t *dfa, const re_node_set *nodes, unsigned int hash)
create_ci_newstate (const re_dfa_t *dfa, const re_node_set *nodes,
re_hashval_t hash)
{
int i;
Idx i;
reg_errcode_t err;
re_dfastate_t *newstate;
newstate = (re_dfastate_t *) calloc (sizeof (re_dfastate_t), 1);
newstate = re_calloc (re_dfastate_t, 1);
if (BE (newstate == NULL, 0))
return NULL;
err = re_node_set_init_copy (&newstate->nodes, nodes);
@ -1547,14 +1565,14 @@ create_ci_newstate (re_dfa_t *dfa, const re_node_set *nodes, unsigned int hash)
static re_dfastate_t *
internal_function
create_cd_newstate (re_dfa_t *dfa, const re_node_set *nodes,
unsigned int context, unsigned int hash)
create_cd_newstate (const re_dfa_t *dfa, const re_node_set *nodes,
unsigned int context, re_hashval_t hash)
{
int i, nctx_nodes = 0;
Idx i, nctx_nodes = 0;
reg_errcode_t err;
re_dfastate_t *newstate;
newstate = (re_dfastate_t *) calloc (sizeof (re_dfastate_t), 1);
newstate = re_calloc (re_dfastate_t, 1);
if (BE (newstate == NULL, 0))
return NULL;
err = re_node_set_init_copy (&newstate->nodes, nodes);

View File

@ -22,6 +22,7 @@
#include <assert.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -90,8 +91,6 @@
# define inline
#endif
/* Number of bits in a byte. */
#define BYTE_BITS 8
/* Number of single byte character. */
#define SBC_MAX 256
@ -123,26 +122,73 @@
extern const char __re_error_msgid[] attribute_hidden;
extern const size_t __re_error_msgid_idx[] attribute_hidden;
/* Number of bits in an unsinged int. */
#define UINT_BITS (sizeof (unsigned int) * BYTE_BITS)
/* Number of unsigned int in an bit_set. */
#define BITSET_UINTS ((SBC_MAX + UINT_BITS - 1) / UINT_BITS)
typedef unsigned int bitset[BITSET_UINTS];
typedef unsigned int *re_bitset_ptr_t;
typedef const unsigned int *re_const_bitset_ptr_t;
typedef __re_idx_t Idx;
#define bitset_set(set,i) (set[i / UINT_BITS] |= 1 << i % UINT_BITS)
#define bitset_clear(set,i) (set[i / UINT_BITS] &= ~(1 << i % UINT_BITS))
#define bitset_contain(set,i) (set[i / UINT_BITS] & (1 << i % UINT_BITS))
#define bitset_empty(set) memset (set, 0, sizeof (unsigned int) * BITSET_UINTS)
#define bitset_set_all(set) \
memset (set, 255, sizeof (unsigned int) * BITSET_UINTS)
#define bitset_copy(dest,src) \
memcpy (dest, src, sizeof (unsigned int) * BITSET_UINTS)
static inline void bitset_not (bitset set);
static inline void bitset_merge (bitset dest, const bitset src);
static inline void bitset_not_merge (bitset dest, const bitset src);
static inline void bitset_mask (bitset dest, const bitset src);
/* Special return value for failure to match. */
#define REG_MISSING ((Idx) -1)
/* Special return value for internal error. */
#define REG_ERROR ((Idx) -2)
/* Test whether N is a valid index, and is not one of the above. */
#ifdef _REGEX_LARGE_OFFSETS
# define REG_VALID_INDEX(n) ((Idx) (n) < REG_ERROR)
#else
# define REG_VALID_INDEX(n) (0 <= (n))
#endif
/* Test whether N is a valid nonzero index. */
#ifdef _REGEX_LARGE_OFFSETS
# define REG_VALID_NONZERO_INDEX(n) ((Idx) ((n) - 1) < (Idx) (REG_ERROR - 1))
#else
# define REG_VALID_NONZERO_INDEX(n) (0 < (n))
#endif
/* A hash value, suitable for computing hash tables. */
typedef __re_size_t re_hashval_t;
/* An integer used to represent a set of bits. It must be unsigned,
and must be at least as wide as unsigned int. */
typedef unsigned long int bitset_word;
/* Maximum value of a bitset word. It must be useful in preprocessor
contexts, and must be consistent with bitset_word. */
#define BITSET_WORD_MAX ULONG_MAX
/* Number of bits in a bitset word. Avoid greater-than-32-bit
integers and unconditional shifts by more than 31 bits, as they're
not portable. */
#if BITSET_WORD_MAX == 0xffffffff
# define BITSET_WORD_BITS 32
#elif BITSET_WORD_MAX >> 31 >> 5 == 1
# define BITSET_WORD_BITS 36
#elif BITSET_WORD_MAX >> 31 >> 16 == 1
# define BITSET_WORD_BITS 48
#elif BITSET_WORD_MAX >> 31 >> 28 == 1
# define BITSET_WORD_BITS 60
#elif BITSET_WORD_MAX >> 31 >> 31 >> 1 == 1
# define BITSET_WORD_BITS 64
#elif BITSET_WORD_MAX >> 31 >> 31 >> 9 == 1
# define BITSET_WORD_BITS 72
#elif BITSET_WORD_MAX >> 31 >> 31 >> 31 >> 31 >> 3 == 1
# define BITSET_WORD_BITS 128
#elif BITSET_WORD_MAX >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 7 == 1
# define BITSET_WORD_BITS 256
#elif BITSET_WORD_MAX >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 7 > 1
# define BITSET_WORD_BITS 257 /* any value > SBC_MAX will do here */
# if BITSET_WORD_BITS <= SBC_MAX
# error "Invalid SBC_MAX"
# endif
#else
# error "Add case for new bitset_word size"
#endif
/* Number of bitset words in a bitset. */
#define BITSET_WORDS ((SBC_MAX + BITSET_WORD_BITS - 1) / BITSET_WORD_BITS)
typedef bitset_word bitset[BITSET_WORDS];
typedef bitset_word *re_bitset_ptr_t;
typedef const bitset_word *re_const_bitset_ptr_t;
#define PREV_WORD_CONSTRAINT 0x0001
#define PREV_NOTWORD_CONSTRAINT 0x0002
@ -171,9 +217,9 @@ typedef enum
typedef struct
{
int alloc;
int nelem;
int *elems;
Idx alloc;
Idx nelem;
Idx *elems;
} re_node_set;
typedef enum
@ -259,19 +305,19 @@ typedef struct
unsigned int non_match : 1;
/* # of multibyte characters. */
int nmbchars;
Idx nmbchars;
/* # of collating symbols. */
int ncoll_syms;
Idx ncoll_syms;
/* # of equivalence classes. */
int nequiv_classes;
Idx nequiv_classes;
/* # of range expressions. */
int nranges;
Idx nranges;
/* # of character classes. */
int nchar_classes;
Idx nchar_classes;
} re_charset_t;
#endif /* RE_ENABLE_I18N */
@ -284,7 +330,7 @@ typedef struct
#ifdef RE_ENABLE_I18N
re_charset_t *mbcset; /* for COMPLEX_BRACKET */
#endif /* RE_ENABLE_I18N */
int idx; /* for BACK_REF */
Idx idx; /* for BACK_REF */
re_context_type ctx_type; /* for ANCHOR */
} opr;
#if __GNUC__ >= 2
@ -318,40 +364,40 @@ struct re_string_t
#ifdef RE_ENABLE_I18N
/* Store the wide character string which is corresponding to MBS. */
wint_t *wcs;
int *offsets;
Idx *offsets;
mbstate_t cur_state;
#endif
/* Index in RAW_MBS. Each character mbs[i] corresponds to
raw_mbs[raw_mbs_idx + i]. */
int raw_mbs_idx;
Idx raw_mbs_idx;
/* The length of the valid characters in the buffers. */
int valid_len;
Idx valid_len;
/* The corresponding number of bytes in raw_mbs array. */
int valid_raw_len;
Idx valid_raw_len;
/* The length of the buffers MBS and WCS. */
int bufs_len;
Idx bufs_len;
/* The index in MBS, which is updated by re_string_fetch_byte. */
int cur_idx;
Idx cur_idx;
/* length of RAW_MBS array. */
int raw_len;
Idx raw_len;
/* This is RAW_LEN - RAW_MBS_IDX + VALID_LEN - VALID_RAW_LEN. */
int len;
Idx len;
/* End of the buffer may be shorter than its length in the cases such
as re_match_2, re_search_2. Then, we use STOP for end of the buffer
instead of LEN. */
int raw_stop;
Idx raw_stop;
/* This is RAW_STOP - RAW_MBS_IDX adjusted through OFFSETS. */
int stop;
Idx stop;
/* The context of mbs[0]. We store the context independently, since
the context of mbs[0] may be different from raw_mbs[0], which is
the beginning of the input string. */
unsigned int tip_context;
/* The translation passed as a part of an argument of re_compile_pattern. */
unsigned RE_TRANSLATE_TYPE trans;
unsigned REG_TRANSLATE_TYPE trans;
/* Copy of re_dfa_t's word_char. */
re_const_bitset_ptr_t word_char;
/* 1 if REG_ICASE. */
/* true if REG_ICASE. */
unsigned char icase;
unsigned char is_utf8;
unsigned char map_notascii;
@ -375,42 +421,18 @@ typedef struct re_dfa_t re_dfa_t;
# endif
#endif
static reg_errcode_t re_string_allocate (re_string_t *pstr, const char *str,
int len, int init_len,
RE_TRANSLATE_TYPE trans, int icase,
const re_dfa_t *dfa)
internal_function;
static reg_errcode_t re_string_construct (re_string_t *pstr, const char *str,
int len, RE_TRANSLATE_TYPE trans,
int icase, const re_dfa_t *dfa)
internal_function;
static reg_errcode_t re_string_reconstruct (re_string_t *pstr, int idx,
int eflags) internal_function;
static reg_errcode_t re_string_realloc_buffers (re_string_t *pstr,
int new_buf_len)
Idx new_buf_len)
internal_function;
#ifdef RE_ENABLE_I18N
static void build_wcs_buffer (re_string_t *pstr) internal_function;
static int build_wcs_upper_buffer (re_string_t *pstr) internal_function;
static reg_errcode_t build_wcs_upper_buffer (re_string_t *pstr)
internal_function;
#endif /* RE_ENABLE_I18N */
static void build_upper_buffer (re_string_t *pstr) internal_function;
static void re_string_translate_buffer (re_string_t *pstr) internal_function;
static void re_string_destruct (re_string_t *pstr) internal_function;
#ifdef RE_ENABLE_I18N
static int re_string_elem_size_at (const re_string_t *pstr, int idx)
internal_function __attribute ((pure));
static inline int re_string_char_size_at (const re_string_t *pstr, int idx)
internal_function __attribute ((pure));
static inline wint_t re_string_wchar_at (const re_string_t *pstr, int idx)
internal_function __attribute ((pure));
#endif /* RE_ENABLE_I18N */
static unsigned int re_string_context_at (const re_string_t *input, int idx,
int eflags)
internal_function __attribute ((pure));
static unsigned char re_string_peek_byte_case (const re_string_t *pstr,
int idx)
internal_function __attribute ((pure));
static unsigned char re_string_fetch_byte_case (re_string_t *pstr)
static unsigned int re_string_context_at (const re_string_t *input,
Idx idx, int eflags)
internal_function __attribute ((pure));
#define re_string_peek_byte(pstr, offset) \
@ -430,10 +452,86 @@ static unsigned char re_string_fetch_byte_case (re_string_t *pstr)
#define re_string_skip_bytes(pstr,idx) ((pstr)->cur_idx += (idx))
#define re_string_set_index(pstr,idx) ((pstr)->cur_idx = (idx))
#include <alloca.h>
#ifndef _LIBC
# if HAVE_ALLOCA
/* The OS usually guarantees only one guard page at the bottom of the stack,
and a page size can be as small as 4096 bytes. So we cannot safely
allocate anything larger than 4096 bytes. Also care for the possibility
of a few compiler-allocated temporary stack slots. */
# define __libc_use_alloca(n) ((n) < 4032)
# else
/* alloca is implemented with malloc, so just use malloc. */
# define __libc_use_alloca(n) 0
# endif
#endif
#define re_malloc(t,n) ((t *) malloc ((n) * sizeof (t)))
#define re_xmalloc(t,n) ((t *) re_xnmalloc (n, sizeof (t)))
#define re_calloc(t,n) ((t *) calloc (n, sizeof (t)))
#define re_realloc(p,t,n) ((t *) realloc (p, (n) * sizeof (t)))
#define re_xrealloc(p,t,n) ((t *) re_xnrealloc (p, n, sizeof (t)))
#define re_x2realloc(p,t,pn) ((t *) re_x2nrealloc (p, pn, sizeof (t)))
#define re_free(p) free (p)
#ifndef SIZE_MAX
# define SIZE_MAX ((size_t) -1)
#endif
/* Return true if an array of N objects, each of size S, cannot exist
due to size arithmetic overflow. S must be nonzero. */
static inline bool
re_alloc_oversized (size_t n, size_t s)
{
return BE (SIZE_MAX / s < n, 0);
}
/* Return true if an array of (2 * N + 1) objects, each of size S,
cannot exist due to size arithmetic overflow. S must be nonzero. */
static inline bool
re_x2alloc_oversized (size_t n, size_t s)
{
return BE ((SIZE_MAX / s - 1) / 2 < n, 0);
}
/* Allocate an array of N objects, each with S bytes of memory,
dynamically, with error checking. S must be nonzero. */
static inline void *
re_xnmalloc (size_t n, size_t s)
{
return re_alloc_oversized (n, s) ? NULL : malloc (n * s);
}
/* Change the size of an allocated block of memory P to an array of N
objects each of S bytes, with error checking. S must be nonzero. */
static inline void *
re_xnrealloc (void *p, size_t n, size_t s)
{
return re_alloc_oversized (n, s) ? NULL : realloc (p, n * s);
}
/* Reallocate a block of memory P to an array of (2 * (*PN) + 1)
objects each of S bytes, with error checking. S must be nonzero.
If the allocation is successful, set *PN to the new allocation
count and return the resulting pointer. Otherwise, return
NULL. */
static inline void *
re_x2nrealloc (void *p, size_t *pn, size_t s)
{
if (re_x2alloc_oversized (*pn, s))
return NULL;
else
{
/* Add 1 in case *PN is zero. */
size_t n1 = 2 * *pn + 1;
p = realloc (p, n1 * s);
if (BE (p != NULL, 1))
*pn = n1;
return p;
}
}
struct bin_tree_t
{
struct bin_tree_t *parent;
@ -446,7 +544,7 @@ struct bin_tree_t
/* `node_idx' is the index in dfa->nodes, if `type' == 0.
Otherwise `type' indicate the type of this node. */
int node_idx;
Idx node_idx;
};
typedef struct bin_tree_t bin_tree_t;
@ -490,7 +588,7 @@ typedef struct bin_tree_storage_t bin_tree_storage_t;
struct re_dfastate_t
{
unsigned int hash;
re_hashval_t hash;
re_node_set nodes;
re_node_set non_eps_nodes;
re_node_set inveclosure;
@ -510,8 +608,8 @@ typedef struct re_dfastate_t re_dfastate_t;
struct re_state_table_entry
{
int num;
int alloc;
Idx num;
Idx alloc;
re_dfastate_t **array;
};
@ -519,8 +617,8 @@ struct re_state_table_entry
typedef struct
{
int next_idx;
int alloc;
Idx next_idx;
Idx alloc;
re_dfastate_t **array;
} state_array_t;
@ -528,8 +626,8 @@ typedef struct
typedef struct
{
int node;
int str_idx; /* The position NODE match at. */
Idx node;
Idx str_idx; /* The position NODE match at. */
state_array_t path;
} re_sub_match_last_t;
@ -539,21 +637,20 @@ typedef struct
typedef struct
{
int str_idx;
int node;
int next_last_offset;
Idx str_idx;
Idx node;
state_array_t *path;
int alasts; /* Allocation size of LASTS. */
int nlasts; /* The number of LASTS. */
Idx alasts; /* Allocation size of LASTS. */
Idx nlasts; /* The number of LASTS. */
re_sub_match_last_t **lasts;
} re_sub_match_top_t;
struct re_backref_cache_entry
{
int node;
int str_idx;
int subexp_from;
int subexp_to;
Idx node;
Idx str_idx;
Idx subexp_from;
Idx subexp_to;
char more;
char unused;
unsigned short int eps_reachable_subexps_map;
@ -571,18 +668,18 @@ typedef struct
/* EFLAGS of the argument of regexec. */
int eflags;
/* Where the matching ends. */
int match_last;
int last_node;
Idx match_last;
Idx last_node;
/* The state log used by the matcher. */
re_dfastate_t **state_log;
int state_log_top;
Idx state_log_top;
/* Back reference cache. */
int nbkref_ents;
int abkref_ents;
Idx nbkref_ents;
Idx abkref_ents;
struct re_backref_cache_entry *bkref_ents;
int max_mb_elem_len;
int nsub_tops;
int asub_tops;
Idx nsub_tops;
Idx asub_tops;
re_sub_match_top_t **sub_tops;
} re_match_context_t;
@ -590,33 +687,33 @@ typedef struct
{
re_dfastate_t **sifted_states;
re_dfastate_t **limited_states;
int last_node;
int last_str_idx;
Idx last_node;
Idx last_str_idx;
re_node_set limits;
} re_sift_context_t;
struct re_fail_stack_ent_t
{
int idx;
int node;
Idx idx;
Idx node;
regmatch_t *regs;
re_node_set eps_via_nodes;
};
struct re_fail_stack_t
{
int num;
int alloc;
Idx num;
Idx alloc;
struct re_fail_stack_ent_t *stack;
};
struct re_dfa_t
{
re_token_t *nodes;
int nodes_alloc;
int nodes_len;
int *nexts;
int *org_indices;
Idx nodes_alloc;
Idx nodes_len;
Idx *nexts;
Idx *org_indices;
re_node_set *edests;
re_node_set *eclosures;
re_node_set *inveclosures;
@ -631,14 +728,13 @@ struct re_dfa_t
int str_tree_storage_idx;
/* number of subexpressions `re_nsub' is in regex_t. */
unsigned int state_hash_mask;
int states_alloc;
int init_node;
int nbackref; /* The number of backreference in this dfa. */
re_hashval_t state_hash_mask;
Idx init_node;
Idx nbackref; /* The number of backreference in this dfa. */
/* Bitmap expressing which backreference is used. */
unsigned int used_bkref_map;
unsigned int completed_bkref_map;
bitset_word used_bkref_map;
bitset_word completed_bkref_map;
unsigned int has_plural_match : 1;
/* If this dfa has "multibyte node", which is a backreference or
@ -651,51 +747,20 @@ struct re_dfa_t
int mb_cur_max;
bitset word_char;
reg_syntax_t syntax;
int *subexp_map;
Idx *subexp_map;
#ifdef DEBUG
char* re_str;
#endif
__libc_lock_define (, lock)
};
#ifndef RE_NO_INTERNAL_PROTOTYPES
static reg_errcode_t re_node_set_alloc (re_node_set *set, int size) internal_function;
static reg_errcode_t re_node_set_init_1 (re_node_set *set, int elem) internal_function;
static reg_errcode_t re_node_set_init_2 (re_node_set *set, int elem1,
int elem2) internal_function;
#define re_node_set_init_empty(set) memset (set, '\0', sizeof (re_node_set))
static reg_errcode_t re_node_set_init_copy (re_node_set *dest,
const re_node_set *src) internal_function;
static reg_errcode_t re_node_set_add_intersect (re_node_set *dest,
const re_node_set *src1,
const re_node_set *src2) internal_function;
static reg_errcode_t re_node_set_init_union (re_node_set *dest,
const re_node_set *src1,
const re_node_set *src2) internal_function;
static reg_errcode_t re_node_set_merge (re_node_set *dest,
const re_node_set *src) internal_function;
static int re_node_set_insert (re_node_set *set, int elem) internal_function;
static int re_node_set_insert_last (re_node_set *set,
int elem) internal_function;
static int re_node_set_compare (const re_node_set *set1,
const re_node_set *set2)
internal_function __attribute ((pure));
static int re_node_set_contains (const re_node_set *set, int elem)
internal_function __attribute ((pure));
static void re_node_set_remove_at (re_node_set *set, int idx) internal_function;
#define re_node_set_remove(set,id) \
(re_node_set_remove_at (set, re_node_set_contains (set, id) - 1))
#define re_node_set_empty(p) ((p)->nelem = 0)
#define re_node_set_free(set) re_free ((set)->elems)
static int re_dfa_add_node (re_dfa_t *dfa, re_token_t token) internal_function;
static re_dfastate_t *re_acquire_state (reg_errcode_t *err, re_dfa_t *dfa,
const re_node_set *nodes) internal_function;
static re_dfastate_t *re_acquire_state_context (reg_errcode_t *err,
re_dfa_t *dfa,
const re_node_set *nodes,
unsigned int context) internal_function;
static void free_state (re_dfastate_t *state) internal_function;
#endif
typedef enum
@ -720,43 +785,79 @@ typedef struct
/* Inline functions for bitset operation. */
static inline void
bitset_set (bitset set, Idx i)
{
set[i / BITSET_WORD_BITS] |= (bitset_word) 1 << i % BITSET_WORD_BITS;
}
static inline void
bitset_clear (bitset set, Idx i)
{
set[i / BITSET_WORD_BITS] &= ~ ((bitset_word) 1 << i % BITSET_WORD_BITS);
}
static inline bool
bitset_contain (const bitset set, Idx i)
{
return (set[i / BITSET_WORD_BITS] >> i % BITSET_WORD_BITS) & 1;
}
static inline void
bitset_empty (bitset set)
{
memset (set, 0, sizeof (bitset));
}
static inline void
bitset_set_all (bitset set)
{
memset (set, -1, sizeof (bitset_word) * (SBC_MAX / BITSET_WORD_BITS));
if (SBC_MAX % BITSET_WORD_BITS != 0)
set[BITSET_WORDS - 1] =
((bitset_word) 1 << SBC_MAX % BITSET_WORD_BITS) - 1;
}
static inline void
bitset_copy (bitset dest, const bitset src)
{
memcpy (dest, src, sizeof (bitset));
}
static inline void
bitset_not (bitset set)
{
int bitset_i;
for (bitset_i = 0; bitset_i < BITSET_UINTS; ++bitset_i)
set[bitset_i] = ~set[bitset_i];
int i;
for (i = 0; i < SBC_MAX / BITSET_WORD_BITS; ++i)
set[i] = ~set[i];
if (SBC_MAX % BITSET_WORD_BITS != 0)
set[BITSET_WORDS - 1] =
((((bitset_word) 1 << SBC_MAX % BITSET_WORD_BITS) - 1)
& ~set[BITSET_WORDS - 1]);
}
static inline void
bitset_merge (bitset dest, const bitset src)
{
int bitset_i;
for (bitset_i = 0; bitset_i < BITSET_UINTS; ++bitset_i)
dest[bitset_i] |= src[bitset_i];
}
static inline void
bitset_not_merge (bitset dest, const bitset src)
{
int i;
for (i = 0; i < BITSET_UINTS; ++i)
dest[i] |= ~src[i];
for (i = 0; i < BITSET_WORDS; ++i)
dest[i] |= src[i];
}
static inline void
bitset_mask (bitset dest, const bitset src)
{
int bitset_i;
for (bitset_i = 0; bitset_i < BITSET_UINTS; ++bitset_i)
dest[bitset_i] &= src[bitset_i];
int i;
for (i = 0; i < BITSET_WORDS; ++i)
dest[i] &= src[i];
}
#if defined RE_ENABLE_I18N && !defined RE_NO_INTERNAL_PROTOTYPES
#if defined RE_ENABLE_I18N
/* Inline functions for re_string. */
static inline int
internal_function
re_string_char_size_at (const re_string_t *pstr, int idx)
internal_function __attribute ((pure))
re_string_char_size_at (const re_string_t *pstr, Idx idx)
{
int byte_idx;
if (pstr->mb_cur_max == 1)
@ -768,8 +869,8 @@ re_string_char_size_at (const re_string_t *pstr, int idx)
}
static inline wint_t
internal_function
re_string_wchar_at (const re_string_t *pstr, int idx)
internal_function __attribute ((pure))
re_string_wchar_at (const re_string_t *pstr, Idx idx)
{
if (pstr->mb_cur_max == 1)
return (wint_t) pstr->mbs[idx];
@ -777,8 +878,8 @@ re_string_wchar_at (const re_string_t *pstr, int idx)
}
static int
internal_function
re_string_elem_size_at (const re_string_t *pstr, int idx)
internal_function __attribute ((pure))
re_string_elem_size_at (const re_string_t *pstr, Idx idx)
{
#ifdef _LIBC
const unsigned char *p, *extra;

File diff suppressed because it is too large Load Diff

View File

@ -1,3 +1,7 @@
2005-09-09 Paul Eggert <eggert@cs.ucla.edu>
* regex.m4: Import from gnulib, to fix some 64-bit bugs.
2005-09-01 Jim Meyering <jim@meyering.net>
* lchown.m4: Require gl_FUNC_CHOWN, for the definition of

View File

@ -1,4 +1,4 @@
#serial 24
#serial 29
# Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005 Free
# Software Foundation, Inc.
@ -10,121 +10,125 @@
dnl Initially derived from code in GNU grep.
dnl Mostly written by Jim Meyering.
AC_PREREQ([2.50])
AC_DEFUN([gl_REGEX],
[
gl_INCLUDED_REGEX([lib/regex.c])
])
AC_REQUIRE([AC_SYS_LARGEFILE]) dnl for a sufficently-wide off_t
AC_DEFINE([_REGEX_LARGE_OFFSETS], 1,
[Define if you want regoff_t to be at least as wide POSIX requires.])
dnl Usage: gl_INCLUDED_REGEX([lib/regex.c])
dnl
AC_DEFUN([gl_INCLUDED_REGEX],
[
AC_LIBSOURCES(
[regcomp.c, regex.c, regex.h,
regex_internal.c, regex_internal.h, regexec.c])
AC_LIBSOURCES(
[regcomp.c, regex.c, regex.h,
regex_internal.c, regex_internal.h, regexec.c])
dnl Even packages that don't use regex.c can use this macro.
dnl Of course, for them it doesn't do anything.
AC_ARG_WITH([included-regex],
[AC_HELP_STRING([--without-included-regex],
[don't compile regex; this is the default on
systems with recent-enough versions of the GNU C
Library (use with caution on other systems)])])
# Assume we'll default to using the included regex.c.
ac_use_included_regex=yes
# However, if the system regex support is good enough that it passes the
# the following run test, then default to *not* using the included regex.c.
case $with_included_regex in
yes|no) ac_use_included_regex=$with_included_regex
;;
'')
# If the system regex support is good enough that it passes the the
# following run test, then default to *not* using the included regex.c.
# If cross compiling, assume the test would fail and use the included
# regex.c. The first failing regular expression is from `Spencer ere
# test #75' in grep-2.3.
AC_CACHE_CHECK([for working re_compile_pattern],
[gl_cv_func_working_re_compile_pattern],
[gl_cv_func_re_compile_pattern_broken],
[AC_RUN_IFELSE(
[AC_LANG_PROGRAM(
[AC_INCLUDES_DEFAULT
#include <regex.h>],
[[static struct re_pattern_buffer regex;
const char *s;
struct re_registers regs;
re_set_syntax (RE_SYNTAX_POSIX_EGREP);
memset (&regex, 0, sizeof (regex));
s = re_compile_pattern ("a[:@:>@:]b\n", 9, &regex);
/* This should fail with _Invalid character class name_ error. */
if (!s)
exit (1);
[AC_LANG_PROGRAM(
[AC_INCLUDES_DEFAULT
#include <regex.h>],
[[static struct re_pattern_buffer regex;
const char *s;
struct re_registers regs;
/* Use the POSIX-compliant spelling with leading REG_,
rather than the traditional GNU spelling with leading RE_,
so that we reject older libc implementations. */
re_set_syntax (REG_SYNTAX_POSIX_EGREP);
memset (&regex, 0, sizeof (regex));
s = re_compile_pattern ("a[:@:>@:]b\n", 9, &regex);
/* This should fail with _Invalid character class name_ error. */
if (!s)
exit (1);
/* This should succeed, but does not for e.g. glibc-2.1.3. */
memset (&regex, 0, sizeof (regex));
s = re_compile_pattern ("{1", 2, &regex);
/* This should succeed, but does not for e.g. glibc-2.1.3. */
memset (&regex, 0, sizeof (regex));
s = re_compile_pattern ("{1", 2, &regex);
if (s)
exit (1);
if (s)
exit (1);
/* The following example is derived from a problem report
against gawk from Jorge Stolfi <stolfi@ic.unicamp.br>. */
memset (&regex, 0, sizeof (regex));
s = re_compile_pattern ("[an\371]*n", 7, &regex);
if (s)
exit (1);
/* The following example is derived from a problem report
against gawk from Jorge Stolfi <stolfi@ic.unicamp.br>. */
memset (&regex, 0, sizeof (regex));
s = re_compile_pattern ("[an\371]*n", 7, &regex);
if (s)
exit (1);
/* This should match, but does not for e.g. glibc-2.2.1. */
if (re_match (&regex, "an", 2, 0, &regs) != 2)
exit (1);
/* This should match, but does not for e.g. glibc-2.2.1. */
if (re_match (&regex, "an", 2, 0, &regs) != 2)
exit (1);
memset (&regex, 0, sizeof (regex));
s = re_compile_pattern ("x", 1, &regex);
if (s)
exit (1);
memset (&regex, 0, sizeof (regex));
s = re_compile_pattern ("x", 1, &regex);
if (s)
exit (1);
/* The version of regex.c in e.g. GNU libc-2.2.93 did not
work with a negative RANGE argument. */
if (re_search (&regex, "wxy", 3, 2, -2, &regs) != 1)
exit (1);
/* The version of regex.c in e.g. GNU libc-2.2.93 did not
work with a negative RANGE argument. */
if (re_search (&regex, "wxy", 3, 2, -2, &regs) != 1)
exit (1);
/* The version of regex.c in older versions of gnulib
* ignored RE_ICASE. Detect that problem too. */
memset (&regex, 0, sizeof (regex));
re_set_syntax(RE_SYNTAX_EMACS|RE_ICASE);
s = re_compile_pattern ("x", 1, &regex);
if (s)
exit (1);
/* The version of regex.c in older versions of gnulib
ignored REG_IGNORE_CASE (which was then called RE_ICASE).
Detect that problem too. */
memset (&regex, 0, sizeof (regex));
re_set_syntax (REG_SYNTAX_EMACS | REG_IGNORE_CASE);
s = re_compile_pattern ("x", 1, &regex);
if (s)
exit (1);
if (re_search (&regex, "WXY", 3, 0, 3, &regs) < 0)
exit (1);
if (re_search (&regex, "WXY", 3, 0, 3, &regs) < 0)
exit (1);
/* REG_STARTEND was added to glibc on 2004-01-15.
Reject older versions. */
if (! REG_STARTEND)
exit (1);
/* REG_STARTEND was added to glibc on 2004-01-15.
Reject older versions. */
if (! REG_STARTEND)
exit (1);
exit (0);]])],
[gl_cv_func_working_re_compile_pattern=yes],
[gl_cv_func_working_re_compile_pattern=no],
dnl When crosscompiling, assume it is broken.
[gl_cv_func_working_re_compile_pattern=no])])
if test $gl_cv_func_working_re_compile_pattern = yes; then
ac_use_included_regex=no
fi
/* Reject hosts whose regoff_t values are too narrow.
These include glibc 2.3.5 on hosts with 64-bit off_t
and 32-bit int, and Solaris 10 on hosts with 32-bit int
and _FILE_OFFSET_BITS=64. */
if (sizeof (regoff_t) < sizeof (off_t))
exit (1);
test -n "$1" || AC_MSG_ERROR([missing argument])
m4_syscmd([test -f '$1'])
ifelse(m4_sysval, 0,
[
AC_ARG_WITH([included-regex],
[ --without-included-regex don't compile regex; this is the default on
systems with recent-enough versions of the GNU C
Library (use with caution on other systems)],
[gl_with_regex=$withval],
[gl_with_regex=$ac_use_included_regex])
if test "X$gl_with_regex" = Xyes; then
AC_LIBOBJ([regex])
gl_PREREQ_REGEX
fi
],
)
]
)
exit (0);]])],
[gl_cv_func_re_compile_pattern_broken=no],
[gl_cv_func_re_compile_pattern_broken=yes],
dnl When crosscompiling, assume it is broken.
[gl_cv_func_re_compile_pattern_broken=yes])])
ac_use_included_regex=$gl_cv_func_re_compile_pattern_broken
;;
*) AC_MSG_ERROR([Invalid value for --with-included-regex: $with_included_regex])
;;
esac
if test $ac_use_included_regex = yes; then
AC_LIBOBJ([regex])
gl_PREREQ_REGEX
fi
])
# Prerequisites of lib/regex.c and lib/regex_internal.c.
AC_DEFUN([gl_PREREQ_REGEX],
[
AC_REQUIRE([AC_GNU_SOURCE])
AC_REQUIRE([gl_C_RESTRICT])
AC_REQUIRE([AM_LANGINFO_CODESET])
AC_CHECK_HEADERS_ONCE([locale.h wchar.h wctype.h])