#if defined _AIX && !defined REGEX_MALLOC
#pragma alloca
#endif
#undef _GNU_SOURCE
#define _GNU_SOURCE
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#if defined STDC_HEADERS && !defined emacs
# include <stddef.h>
#else
# include <sys/types.h>
#endif
#define WIDE_CHAR_SUPPORT \
(defined _LIBC || HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_BTOWC && !emacs)
#if WIDE_CHAR_SUPPORT
# include <wchar.h>
# include <wctype.h>
#endif
#ifdef _LIBC
# define regfree(preg) __regfree (preg)
# define regexec(pr, st, nm, pm, ef) __regexec (pr, st, nm, pm, ef)
# define regcomp(preg, pattern, cflags) __regcomp (preg, pattern, cflags)
# define regerror(errcode, preg, errbuf, errbuf_size) \
__regerror(errcode, preg, errbuf, errbuf_size)
# define re_set_registers(bu, re, nu, st, en) \
__re_set_registers (bu, re, nu, st, en)
# define re_match_2(bufp, string1, size1, string2, size2, pos, regs, stop) \
__re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
# define re_match(bufp, string, size, pos, regs) \
__re_match (bufp, string, size, pos, regs)
# define re_search(bufp, string, size, startpos, range, regs) \
__re_search (bufp, string, size, startpos, range, regs)
# define re_compile_pattern(pattern, length, bufp) \
__re_compile_pattern (pattern, length, bufp)
# define re_set_syntax(syntax) __re_set_syntax (syntax)
# define re_search_2(bufp, st1, s1, st2, s2, startpos, range, regs, stop) \
__re_search_2 (bufp, st1, s1, st2, s2, startpos, range, regs, stop)
# define re_compile_fastmap(bufp) __re_compile_fastmap (bufp)
# define btowc __btowc
# define iswctype __iswctype
# define wctype __wctype
# define WEAK_ALIAS(a,b) weak_alias (a, b)
# include <locale/localeinfo.h>
# include <locale/elem-hash.h>
# include <langinfo.h>
#else
# define WEAK_ALIAS(a,b)
#endif
#if HAVE_LIBINTL_H || defined _LIBC
# include <libintl.h>
#else
# define gettext(msgid) (msgid)
#endif
#ifndef gettext_noop
# define gettext_noop(String) String
#endif
#ifdef emacs
# include "lisp.h"
# include "buffer.h"
# define SYNTAX_ENTRY_VIA_PROPERTY
# include "syntax.h"
# include "charset.h"
# include "category.h"
# ifdef malloc
# undef malloc
# endif
# define malloc xmalloc
# ifdef realloc
# undef realloc
# endif
# define realloc xrealloc
# ifdef free
# undef free
# endif
# define free xfree
# define PTR_TO_OFFSET(d) POS_AS_IN_BUFFER (POINTER_TO_OFFSET (d))
# define POS_AS_IN_BUFFER(p) ((p) + (NILP (re_match_object) || BUFFERP (re_match_object)))
# define RE_MULTIBYTE_P(bufp) ((bufp)->multibyte)
# define RE_STRING_CHAR(p, s) \
(multibyte ? (STRING_CHAR (p, s)) : (*(p)))
# define RE_STRING_CHAR_AND_LENGTH(p, s, len) \
(multibyte ? (STRING_CHAR_AND_LENGTH (p, s, len)) : ((len) = 1, *(p)))
# define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2) \
do { \
if (multibyte) \
{ \
re_char *dtemp = (p) == (str2) ? (end1) : (p); \
re_char *dlimit = ((p) > (str2) && (p) <= (end2)) ? (str2) : (str1); \
while (dtemp-- > dlimit && !CHAR_HEAD_P (*dtemp)); \
c = STRING_CHAR (dtemp, (p) - dtemp); \
} \
else \
(c = ((p) == (str2) ? (end1) : (p))[-1]); \
} while (0)
#else
# undef REL_ALLOC
# if defined STDC_HEADERS || defined _LIBC
# include <stdlib.h>
# else
char *malloc ();
char *realloc ();
# endif
# ifdef INHIBIT_STRING_HEADER
# if !(defined HAVE_BZERO && defined HAVE_BCOPY)
# if !defined bzero && !defined bcopy
# undef INHIBIT_STRING_HEADER
# endif
# endif
# endif
# ifndef INHIBIT_STRING_HEADER
# if defined HAVE_STRING_H || defined STDC_HEADERS || defined _LIBC
# include <string.h>
# ifndef bzero
# ifndef _LIBC
# define bzero(s, n) (memset (s, '\0', n), (s))
# else
# define bzero(s, n) __bzero (s, n)
# endif
# endif
# else
# include <strings.h>
# ifndef memcmp
# define memcmp(s1, s2, n) bcmp (s1, s2, n)
# endif
# ifndef memcpy
# define memcpy(d, s, n) (bcopy (s, d, n), (d))
# endif
# endif
# endif
enum syntaxcode { Swhitespace = 0, Sword = 1 };
# ifdef SWITCH_ENUM_BUG
# define SWITCH_ENUM_CAST(x) ((int)(x))
# else
# define SWITCH_ENUM_CAST(x) (x)
# endif
# define BASE_LEADING_CODE_P(c) (0)
# define CHAR_CHARSET(c) 0
# define CHARSET_LEADING_CODE_BASE(c) 0
# define MAX_MULTIBYTE_LENGTH 1
# define RE_MULTIBYTE_P(x) 0
# define WORD_BOUNDARY_P(c1, c2) (0)
# define CHAR_HEAD_P(p) (1)
# define SINGLE_BYTE_CHAR_P(c) (1)
# define SAME_CHARSET_P(c1, c2) (1)
# define MULTIBYTE_FORM_LENGTH(p, s) (1)
# define STRING_CHAR(p, s) (*(p))
# define RE_STRING_CHAR STRING_CHAR
# define CHAR_STRING(c, s) (*(s) = (c), 1)
# define STRING_CHAR_AND_LENGTH(p, s, actual_len) ((actual_len) = 1, *(p))
# define RE_STRING_CHAR_AND_LENGTH STRING_CHAR_AND_LENGTH
# define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2) \
(c = ((p) == (str2) ? *((end1) - 1) : *((p) - 1)))
# define MAKE_CHAR(charset, c1, c2) (c1)
#endif
#ifndef RE_TRANSLATE
# define RE_TRANSLATE(TBL, C) ((unsigned char)(TBL)[C])
# define RE_TRANSLATE_P(TBL) (TBL)
#endif
#include "regex.h"
#include <ctype.h>
#ifdef emacs
# define IS_REAL_ASCII(c) ((c) < 0200)
# define ISUNIBYTE(c) (SINGLE_BYTE_CHAR_P ((c)))
# define ISDIGIT(c) ((c) >= '0' && (c) <= '9')
# define ISCNTRL(c) ((c) < ' ')
# define ISXDIGIT(c) (((c) >= '0' && (c) <= '9') \
|| ((c) >= 'a' && (c) <= 'f') \
|| ((c) >= 'A' && (c) <= 'F'))
# define ISBLANK(c) ((c) == ' ' || (c) == '\t')
# define ISGRAPH(c) (SINGLE_BYTE_CHAR_P (c) \
? (c) > ' ' && !((c) >= 0177 && (c) <= 0237) \
: 1)
# define ISPRINT(c) (SINGLE_BYTE_CHAR_P (c) \
? (c) >= ' ' && !((c) >= 0177 && (c) <= 0237) \
: 1)
# define ISALNUM(c) (IS_REAL_ASCII (c) \
? (((c) >= 'a' && (c) <= 'z') \
|| ((c) >= 'A' && (c) <= 'Z') \
|| ((c) >= '0' && (c) <= '9')) \
: SYNTAX (c) == Sword)
# define ISALPHA(c) (IS_REAL_ASCII (c) \
? (((c) >= 'a' && (c) <= 'z') \
|| ((c) >= 'A' && (c) <= 'Z')) \
: SYNTAX (c) == Sword)
# define ISLOWER(c) (LOWERCASEP (c))
# define ISPUNCT(c) (IS_REAL_ASCII (c) \
? ((c) > ' ' && (c) < 0177 \
&& !(((c) >= 'a' && (c) <= 'z') \
|| ((c) >= 'A' && (c) <= 'Z') \
|| ((c) >= '0' && (c) <= '9'))) \
: SYNTAX (c) != Sword)
# define ISSPACE(c) (SYNTAX (c) == Swhitespace)
# define ISUPPER(c) (UPPERCASEP (c))
# define ISWORD(c) (SYNTAX (c) == Sword)
#else
# undef ISASCII
# if defined STDC_HEADERS || (!defined isascii && !defined HAVE_ISASCII)
# define ISASCII(c) 1
# else
# define ISASCII(c) isascii(c)
# endif
# define IS_REAL_ASCII(c) ((c) < 0200)
# define ISUNIBYTE(c) 1
# ifdef isblank
# define ISBLANK(c) (ISASCII (c) && isblank (c))
# else
# define ISBLANK(c) ((c) == ' ' || (c) == '\t')
# endif
# ifdef isgraph
# define ISGRAPH(c) (ISASCII (c) && isgraph (c))
# else
# define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
# endif
# undef ISPRINT
# define ISPRINT(c) (ISASCII (c) && isprint (c))
# define ISDIGIT(c) (ISASCII (c) && isdigit (c))
# define ISALNUM(c) (ISASCII (c) && isalnum (c))
# define ISALPHA(c) (ISASCII (c) && isalpha (c))
# define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
# define ISLOWER(c) (ISASCII (c) && islower (c))
# define ISPUNCT(c) (ISASCII (c) && ispunct (c))
# define ISSPACE(c) (ISASCII (c) && isspace (c))
# define ISUPPER(c) (ISASCII (c) && isupper (c))
# define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
# define ISWORD(c) ISALPHA(c)
# ifdef _tolower
# define TOLOWER(c) _tolower(c)
# else
# define TOLOWER(c) tolower(c)
# endif
# define CHAR_SET_SIZE 256
# ifdef SYNTAX_TABLE
extern char *re_syntax_table;
# else
static char re_syntax_table[CHAR_SET_SIZE];
static void
init_syntax_once ()
{
register int c;
static int done = 0;
if (done)
return;
bzero (re_syntax_table, sizeof re_syntax_table);
for (c = 0; c < CHAR_SET_SIZE; ++c)
if (ISALNUM (c))
re_syntax_table[c] = Sword;
re_syntax_table['_'] = Sword;
done = 1;
}
# endif
# define SYNTAX(c) re_syntax_table[(c)]
#endif
#ifndef NULL
# define NULL (void *)0
#endif
#undef SIGN_EXTEND_CHAR
#if __STDC__
# define SIGN_EXTEND_CHAR(c) ((signed char) (c))
#else
# define SIGN_EXTEND_CHAR(c) ((((unsigned char) (c)) ^ 128) - 128)
#endif
#ifdef REGEX_MALLOC
# define REGEX_ALLOCATE malloc
# define REGEX_REALLOCATE(source, osize, nsize) realloc (source, nsize)
# define REGEX_FREE free
#else
# ifndef alloca
# ifdef __GNUC__
# define alloca __builtin_alloca
# else
# if HAVE_ALLOCA_H
# include <alloca.h>
# endif
# endif
# endif
# define REGEX_ALLOCATE alloca
# define REGEX_REALLOCATE(source, osize, nsize) \
(destination = (char *) alloca (nsize), \
memcpy (destination, source, osize))
# define REGEX_FREE(arg) ((void)0)
#endif
#if defined REL_ALLOC && defined REGEX_MALLOC
# define REGEX_ALLOCATE_STACK(size) \
r_alloc (&failure_stack_ptr, (size))
# define REGEX_REALLOCATE_STACK(source, osize, nsize) \
r_re_alloc (&failure_stack_ptr, (nsize))
# define REGEX_FREE_STACK(ptr) \
r_alloc_free (&failure_stack_ptr)
#else
# ifdef REGEX_MALLOC
# define REGEX_ALLOCATE_STACK malloc
# define REGEX_REALLOCATE_STACK(source, osize, nsize) realloc (source, nsize)
# define REGEX_FREE_STACK free
# else
# define REGEX_ALLOCATE_STACK alloca
# define REGEX_REALLOCATE_STACK(source, osize, nsize) \
REGEX_REALLOCATE (source, osize, nsize)
# define REGEX_FREE_STACK(arg) ((void)0)
# endif
#endif
#define FIRST_STRING_P(ptr) \
(size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
#define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t)))
#define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
#define RETALLOC_IF(addr, n, t) \
if (addr) RETALLOC((addr), (n), t); else (addr) = TALLOC ((n), t)
#define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
#define BYTEWIDTH 8
#define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
#undef MAX
#undef MIN
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
typedef const unsigned char re_char;
typedef char boolean;
#define false 0
#define true 1
static int re_match_2_internal _RE_ARGS ((struct re_pattern_buffer *bufp,
re_char *string1, int size1,
re_char *string2, int size2,
int pos,
struct re_registers *regs,
int stop));
typedef enum
{
no_op = 0,
succeed,
exactn,
anychar,
charset,
charset_not,
start_memory,
stop_memory,
duplicate,
begline,
endline,
begbuf,
endbuf,
jump,
on_failure_jump,
on_failure_keep_string_jump,
on_failure_jump_loop,
on_failure_jump_nastyloop,
on_failure_jump_smart,
succeed_n,
jump_n,
set_number_at,
wordbeg,
wordend,
wordbound,
notwordbound,
syntaxspec,
notsyntaxspec
#ifdef emacs
,before_dot,
at_dot,
after_dot,
categoryspec,
notcategoryspec
#endif
} re_opcode_t;
#define STORE_NUMBER(destination, number) \
do { \
(destination)[0] = (number) & 0377; \
(destination)[1] = (number) >> 8; \
} while (0)
#define STORE_NUMBER_AND_INCR(destination, number) \
do { \
STORE_NUMBER (destination, number); \
(destination) += 2; \
} while (0)
#define EXTRACT_NUMBER(destination, source) \
do { \
(destination) = *(source) & 0377; \
(destination) += SIGN_EXTEND_CHAR (*((source) + 1)) << 8; \
} while (0)
#ifdef DEBUG
static void extract_number _RE_ARGS ((int *dest, re_char *source));
static void
extract_number (dest, source)
int *dest;
re_char *source;
{
int temp = SIGN_EXTEND_CHAR (*(source + 1));
*dest = *source & 0377;
*dest += temp << 8;
}
# ifndef EXTRACT_MACROS
# undef EXTRACT_NUMBER
# define EXTRACT_NUMBER(dest, src) extract_number (&dest, src)
# endif
#endif
#define EXTRACT_NUMBER_AND_INCR(destination, source) \
do { \
EXTRACT_NUMBER (destination, source); \
(source) += 2; \
} while (0)
#ifdef DEBUG
static void extract_number_and_incr _RE_ARGS ((int *destination,
re_char **source));
static void
extract_number_and_incr (destination, source)
int *destination;
re_char **source;
{
extract_number (destination, *source);
*source += 2;
}
# ifndef EXTRACT_MACROS
# undef EXTRACT_NUMBER_AND_INCR
# define EXTRACT_NUMBER_AND_INCR(dest, src) \
extract_number_and_incr (&dest, &src)
# endif
#endif
#define STORE_CHARACTER_AND_INCR(destination, character) \
do { \
(destination)[0] = (character) & 0377; \
(destination)[1] = ((character) >> 8) & 0377; \
(destination)[2] = (character) >> 16; \
(destination) += 3; \
} while (0)
#define EXTRACT_CHARACTER(destination, source) \
do { \
(destination) = ((source)[0] \
| ((source)[1] << 8) \
| ((source)[2] << 16)); \
} while (0)
#define CHARSET_BITMAP_SIZE(p) ((p)[1] & 0x7F)
#define CHARSET_RANGE_TABLE_EXISTS_P(p) ((p)[1] & 0x80)
#define CHARSET_RANGE_TABLE(p) (&(p)[4 + CHARSET_BITMAP_SIZE (p)])
#define CHARSET_RANGE_TABLE_BITS(p) \
((p)[2 + CHARSET_BITMAP_SIZE (p)] \
+ (p)[3 + CHARSET_BITMAP_SIZE (p)] * 0x100)
#define CHARSET_LOOKUP_BITMAP(p, c) \
((c) < CHARSET_BITMAP_SIZE (p) * BYTEWIDTH \
&& (p)[2 + (c) / BYTEWIDTH] & (1 << ((c) % BYTEWIDTH)))
#define CHARSET_RANGE_TABLE_END(range_table, count) \
((range_table) + (count) * 2 * 3)
#define CHARSET_LOOKUP_RANGE_TABLE_RAW(not, c, range_table, count) \
do \
{ \
re_wchar_t range_start, range_end; \
re_char *p; \
re_char *range_table_end \
= CHARSET_RANGE_TABLE_END ((range_table), (count)); \
\
for (p = (range_table); p < range_table_end; p += 2 * 3) \
{ \
EXTRACT_CHARACTER (range_start, p); \
EXTRACT_CHARACTER (range_end, p + 3); \
\
if (range_start <= (c) && (c) <= range_end) \
{ \
(not) = !(not); \
break; \
} \
} \
} \
while (0)
#define CHARSET_LOOKUP_RANGE_TABLE(not, c, charset) \
do \
{ \
\
int count; \
re_char *range_table = CHARSET_RANGE_TABLE (charset); \
\
EXTRACT_NUMBER_AND_INCR (count, range_table); \
CHARSET_LOOKUP_RANGE_TABLE_RAW ((not), (c), range_table, count); \
} \
while (0)
#ifdef DEBUG
# include <stdio.h>
# include <assert.h>
static int debug = -100000;
# define DEBUG_STATEMENT(e) e
# define DEBUG_PRINT1(x) if (debug > 0) printf (x)
# define DEBUG_PRINT2(x1, x2) if (debug > 0) printf (x1, x2)
# define DEBUG_PRINT3(x1, x2, x3) if (debug > 0) printf (x1, x2, x3)
# define DEBUG_PRINT4(x1, x2, x3, x4) if (debug > 0) printf (x1, x2, x3, x4)
# define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) \
if (debug > 0) print_partial_compiled_pattern (s, e)
# define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \
if (debug > 0) print_double_string (w, s1, sz1, s2, sz2)
void
print_fastmap (fastmap)
char *fastmap;
{
unsigned was_a_range = 0;
unsigned i = 0;
while (i < (1 << BYTEWIDTH))
{
if (fastmap[i++])
{
was_a_range = 0;
putchar (i - 1);
while (i < (1 << BYTEWIDTH) && fastmap[i])
{
was_a_range = 1;
i++;
}
if (was_a_range)
{
printf ("-");
putchar (i - 1);
}
}
}
putchar ('\n');
}
void
print_partial_compiled_pattern (start, end)
re_char *start;
re_char *end;
{
int mcnt, mcnt2;
re_char *p = start;
re_char *pend = end;
if (start == NULL)
{
printf ("(null)\n");
return;
}
while (p < pend)
{
printf ("%d:\t", p - start);
switch ((re_opcode_t) *p++)
{
case no_op:
printf ("/no_op");
break;
case succeed:
printf ("/succeed");
break;
case exactn:
mcnt = *p++;
printf ("/exactn/%d", mcnt);
do
{
putchar ('/');
putchar (*p++);
}
while (--mcnt);
break;
case start_memory:
printf ("/start_memory/%d", *p++);
break;
case stop_memory:
printf ("/stop_memory/%d", *p++);
break;
case duplicate:
printf ("/duplicate/%d", *p++);
break;
case anychar:
printf ("/anychar");
break;
case charset:
case charset_not:
{
register int c, last = -100;
register int in_range = 0;
int length = CHARSET_BITMAP_SIZE (p - 1);
int has_range_table = CHARSET_RANGE_TABLE_EXISTS_P (p - 1);
printf ("/charset [%s",
(re_opcode_t) *(p - 1) == charset_not ? "^" : "");
assert (p + *p < pend);
for (c = 0; c < 256; c++)
if (c / 8 < length
&& (p[1 + (c/8)] & (1 << (c % 8))))
{
if (last + 1 == c && ! in_range)
{
putchar ('-');
in_range = 1;
}
else if (last + 1 != c && in_range)
{
putchar (last);
in_range = 0;
}
if (! in_range)
putchar (c);
last = c;
}
if (in_range)
putchar (last);
putchar (']');
p += 1 + length;
if (has_range_table)
{
int count;
printf ("has-range-table");
p += 2;
EXTRACT_NUMBER_AND_INCR (count, p);
p = CHARSET_RANGE_TABLE_END (p, count);
}
}
break;
case begline:
printf ("/begline");
break;
case endline:
printf ("/endline");
break;
case on_failure_jump:
extract_number_and_incr (&mcnt, &p);
printf ("/on_failure_jump to %d", p + mcnt - start);
break;
case on_failure_keep_string_jump:
extract_number_and_incr (&mcnt, &p);
printf ("/on_failure_keep_string_jump to %d", p + mcnt - start);
break;
case on_failure_jump_nastyloop:
extract_number_and_incr (&mcnt, &p);
printf ("/on_failure_jump_nastyloop to %d", p + mcnt - start);
break;
case on_failure_jump_loop:
extract_number_and_incr (&mcnt, &p);
printf ("/on_failure_jump_loop to %d", p + mcnt - start);
break;
case on_failure_jump_smart:
extract_number_and_incr (&mcnt, &p);
printf ("/on_failure_jump_smart to %d", p + mcnt - start);
break;
case jump:
extract_number_and_incr (&mcnt, &p);
printf ("/jump to %d", p + mcnt - start);
break;
case succeed_n:
extract_number_and_incr (&mcnt, &p);
extract_number_and_incr (&mcnt2, &p);
printf ("/succeed_n to %d, %d times", p - 2 + mcnt - start, mcnt2);
break;
case jump_n:
extract_number_and_incr (&mcnt, &p);
extract_number_and_incr (&mcnt2, &p);
printf ("/jump_n to %d, %d times", p - 2 + mcnt - start, mcnt2);
break;
case set_number_at:
extract_number_and_incr (&mcnt, &p);
extract_number_and_incr (&mcnt2, &p);
printf ("/set_number_at location %d to %d", p - 2 + mcnt - start, mcnt2);
break;
case wordbound:
printf ("/wordbound");
break;
case notwordbound:
printf ("/notwordbound");
break;
case wordbeg:
printf ("/wordbeg");
break;
case wordend:
printf ("/wordend");
case syntaxspec:
printf ("/syntaxspec");
mcnt = *p++;
printf ("/%d", mcnt);
break;
case notsyntaxspec:
printf ("/notsyntaxspec");
mcnt = *p++;
printf ("/%d", mcnt);
break;
# ifdef emacs
case before_dot:
printf ("/before_dot");
break;
case at_dot:
printf ("/at_dot");
break;
case after_dot:
printf ("/after_dot");
break;
case categoryspec:
printf ("/categoryspec");
mcnt = *p++;
printf ("/%d", mcnt);
break;
case notcategoryspec:
printf ("/notcategoryspec");
mcnt = *p++;
printf ("/%d", mcnt);
break;
# endif
case begbuf:
printf ("/begbuf");
break;
case endbuf:
printf ("/endbuf");
break;
default:
printf ("?%d", *(p-1));
}
putchar ('\n');
}
printf ("%d:\tend of pattern.\n", p - start);
}
void
print_compiled_pattern (bufp)
struct re_pattern_buffer *bufp;
{
re_char *buffer = bufp->buffer;
print_partial_compiled_pattern (buffer, buffer + bufp->used);
printf ("%ld bytes used/%ld bytes allocated.\n",
bufp->used, bufp->allocated);
if (bufp->fastmap_accurate && bufp->fastmap)
{
printf ("fastmap: ");
print_fastmap (bufp->fastmap);
}
printf ("re_nsub: %d\t", bufp->re_nsub);
printf ("regs_alloc: %d\t", bufp->regs_allocated);
printf ("can_be_null: %d\t", bufp->can_be_null);
printf ("no_sub: %d\t", bufp->no_sub);
printf ("not_bol: %d\t", bufp->not_bol);
printf ("not_eol: %d\t", bufp->not_eol);
printf ("syntax: %lx\n", bufp->syntax);
fflush (stdout);
}
void
print_double_string (where, string1, size1, string2, size2)
re_char *where;
re_char *string1;
re_char *string2;
int size1;
int size2;
{
int this_char;
if (where == NULL)
printf ("(null)");
else
{
if (FIRST_STRING_P (where))
{
for (this_char = where - string1; this_char < size1; this_char++)
putchar (string1[this_char]);
where = string2;
}
for (this_char = where - string2; this_char < size2; this_char++)
putchar (string2[this_char]);
}
}
#else
# undef assert
# define assert(e)
# define DEBUG_STATEMENT(e)
# define DEBUG_PRINT1(x)
# define DEBUG_PRINT2(x1, x2)
# define DEBUG_PRINT3(x1, x2, x3)
# define DEBUG_PRINT4(x1, x2, x3, x4)
# define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
# define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
#endif
reg_syntax_t re_syntax_options;
reg_syntax_t
re_set_syntax (syntax)
reg_syntax_t syntax;
{
reg_syntax_t ret = re_syntax_options;
re_syntax_options = syntax;
return ret;
}
WEAK_ALIAS (__re_set_syntax, re_set_syntax)
static const char *re_error_msgid[] =
{
gettext_noop ("Success"),
gettext_noop ("No match"),
gettext_noop ("Invalid regular expression"),
gettext_noop ("Invalid collation character"),
gettext_noop ("Invalid character class name"),
gettext_noop ("Trailing backslash"),
gettext_noop ("Invalid back reference"),
gettext_noop ("Unmatched [ or [^"),
gettext_noop ("Unmatched ( or \\("),
gettext_noop ("Unmatched \\{"),
gettext_noop ("Invalid content of \\{\\}"),
gettext_noop ("Invalid range end"),
gettext_noop ("Memory exhausted"),
gettext_noop ("Invalid preceding regular expression"),
gettext_noop ("Premature end of regular expression"),
gettext_noop ("Regular expression too big"),
gettext_noop ("Unmatched ) or \\)"),
};
#define MATCH_MAY_ALLOCATE
#ifdef __GNUC__
# undef C_ALLOCA
#endif
#if (defined C_ALLOCA || defined REGEX_MALLOC) && defined emacs
# undef MATCH_MAY_ALLOCATE
#endif
#ifndef INIT_FAILURE_ALLOC
# define INIT_FAILURE_ALLOC 20
#endif
# if defined MATCH_MAY_ALLOCATE
size_t re_max_failures = 40000;
# else
size_t re_max_failures = 4000;
# endif
union fail_stack_elt
{
re_char *pointer;
long integer;
};
typedef union fail_stack_elt fail_stack_elt_t;
typedef struct
{
fail_stack_elt_t *stack;
size_t size;
size_t avail;
size_t frame;
} fail_stack_type;
#define FAIL_STACK_EMPTY() (fail_stack.frame == 0)
#define FAIL_STACK_FULL() (fail_stack.avail == fail_stack.size)
#ifdef MATCH_MAY_ALLOCATE
# define INIT_FAIL_STACK() \
do { \
fail_stack.stack = (fail_stack_elt_t *) \
REGEX_ALLOCATE_STACK (INIT_FAILURE_ALLOC * TYPICAL_FAILURE_SIZE \
* sizeof (fail_stack_elt_t)); \
\
if (fail_stack.stack == NULL) \
return -2; \
\
fail_stack.size = INIT_FAILURE_ALLOC; \
fail_stack.avail = 0; \
fail_stack.frame = 0; \
} while (0)
# define RESET_FAIL_STACK() REGEX_FREE_STACK (fail_stack.stack)
#else
# define INIT_FAIL_STACK() \
do { \
fail_stack.avail = 0; \
fail_stack.frame = 0; \
} while (0)
# define RESET_FAIL_STACK() ((void)0)
#endif
#define FAIL_STACK_GROWTH_FACTOR 4
#define GROW_FAIL_STACK(fail_stack) \
(((fail_stack).size * sizeof (fail_stack_elt_t) \
>= re_max_failures * TYPICAL_FAILURE_SIZE) \
? 0 \
: ((fail_stack).stack \
= (fail_stack_elt_t *) \
REGEX_REALLOCATE_STACK ((fail_stack).stack, \
(fail_stack).size * sizeof (fail_stack_elt_t), \
MIN (re_max_failures * TYPICAL_FAILURE_SIZE, \
((fail_stack).size * sizeof (fail_stack_elt_t) \
* FAIL_STACK_GROWTH_FACTOR))), \
\
(fail_stack).stack == NULL \
? 0 \
: ((fail_stack).size \
= (MIN (re_max_failures * TYPICAL_FAILURE_SIZE, \
((fail_stack).size * sizeof (fail_stack_elt_t) \
* FAIL_STACK_GROWTH_FACTOR)) \
/ sizeof (fail_stack_elt_t)), \
1)))
#define PUSH_FAILURE_POINTER(item) \
fail_stack.stack[fail_stack.avail++].pointer = (item)
#define PUSH_FAILURE_INT(item) \
fail_stack.stack[fail_stack.avail++].integer = (item)
#define PUSH_FAILURE_ELT(item) \
fail_stack.stack[fail_stack.avail++] = (item)
#define POP_FAILURE_POINTER() fail_stack.stack[--fail_stack.avail].pointer
#define POP_FAILURE_INT() fail_stack.stack[--fail_stack.avail].integer
#define POP_FAILURE_ELT() fail_stack.stack[--fail_stack.avail]
#define NUM_NONREG_ITEMS 3
#define FAILURE_PAT(h) fail_stack.stack[(h) - 1].pointer
#define FAILURE_STR(h) (fail_stack.stack[(h) - 2].pointer)
#define NEXT_FAILURE_HANDLE(h) fail_stack.stack[(h) - 3].integer
#define TOP_FAILURE_HANDLE() fail_stack.frame
#define ENSURE_FAIL_STACK(space) \
while (REMAINING_AVAIL_SLOTS <= space) { \
if (!GROW_FAIL_STACK (fail_stack)) \
return -2; \
DEBUG_PRINT2 ("\n Doubled stack; size now: %d\n", (fail_stack).size);\
DEBUG_PRINT2 (" slots available: %d\n", REMAINING_AVAIL_SLOTS);\
}
#define PUSH_FAILURE_REG(num) \
do { \
char *destination; \
ENSURE_FAIL_STACK(3); \
DEBUG_PRINT4 (" Push reg %d (spanning %p -> %p)\n", \
num, regstart[num], regend[num]); \
PUSH_FAILURE_POINTER (regstart[num]); \
PUSH_FAILURE_POINTER (regend[num]); \
PUSH_FAILURE_INT (num); \
} while (0)
#define PUSH_NUMBER(ptr,val) \
do { \
char *destination; \
int c; \
ENSURE_FAIL_STACK(3); \
EXTRACT_NUMBER (c, ptr); \
DEBUG_PRINT4 (" Push number %p = %d -> %d\n", ptr, c, val); \
PUSH_FAILURE_INT (c); \
PUSH_FAILURE_POINTER (ptr); \
PUSH_FAILURE_INT (-1); \
STORE_NUMBER (ptr, val); \
} while (0)
#define POP_FAILURE_REG_OR_COUNT() \
do { \
int reg = POP_FAILURE_INT (); \
if (reg == -1) \
{ \
\
\
unsigned char *ptr = (unsigned char*) POP_FAILURE_POINTER (); \
reg = POP_FAILURE_INT (); \
STORE_NUMBER (ptr, reg); \
DEBUG_PRINT3 (" Pop counter %p = %d\n", ptr, reg); \
} \
else \
{ \
regend[reg] = POP_FAILURE_POINTER (); \
regstart[reg] = POP_FAILURE_POINTER (); \
DEBUG_PRINT4 (" Pop reg %d (spanning %p -> %p)\n", \
reg, regstart[reg], regend[reg]); \
} \
} while (0)
#define CHECK_INFINITE_LOOP(pat_cur, string_place) \
do { \
int failure = TOP_FAILURE_HANDLE(); \
\
while (failure > 0 && \
(FAILURE_STR (failure) == string_place \
|| FAILURE_STR (failure) == NULL)) \
{ \
assert (FAILURE_PAT (failure) >= bufp->buffer \
&& FAILURE_PAT (failure) <= bufp->buffer + bufp->used); \
if (FAILURE_PAT (failure) == pat_cur) \
goto fail; \
DEBUG_PRINT2 (" Other pattern: %p\n", FAILURE_PAT (failure)); \
failure = NEXT_FAILURE_HANDLE(failure); \
} \
DEBUG_PRINT2 (" Other string: %p\n", FAILURE_STR (failure)); \
} while (0)
#define PUSH_FAILURE_POINT(pattern, string_place) \
do { \
char *destination; \
\
\
DEBUG_STATEMENT (nfailure_points_pushed++); \
DEBUG_PRINT1 ("\nPUSH_FAILURE_POINT:\n"); \
DEBUG_PRINT2 (" Before push, next avail: %d\n", (fail_stack).avail); \
DEBUG_PRINT2 (" size: %d\n", (fail_stack).size);\
\
ENSURE_FAIL_STACK (NUM_NONREG_ITEMS); \
\
DEBUG_PRINT1 ("\n"); \
\
DEBUG_PRINT2 (" Push frame index: %d\n", fail_stack.frame); \
PUSH_FAILURE_INT (fail_stack.frame); \
\
DEBUG_PRINT2 (" Push string %p: `", string_place); \
DEBUG_PRINT_DOUBLE_STRING (string_place, string1, size1, string2, size2);\
DEBUG_PRINT1 ("'\n"); \
PUSH_FAILURE_POINTER (string_place); \
\
DEBUG_PRINT2 (" Push pattern %p: ", pattern); \
DEBUG_PRINT_COMPILED_PATTERN (bufp, pattern, pend); \
PUSH_FAILURE_POINTER (pattern); \
\
\
fail_stack.frame = fail_stack.avail; \
} while (0)
#define TYPICAL_FAILURE_SIZE 20
#define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail)
#define POP_FAILURE_POINT(str, pat) \
do { \
assert (!FAIL_STACK_EMPTY ()); \
\
\
DEBUG_PRINT1 ("POP_FAILURE_POINT:\n"); \
DEBUG_PRINT2 (" Before pop, next avail: %d\n", fail_stack.avail); \
DEBUG_PRINT2 (" size: %d\n", fail_stack.size); \
\
\
while (fail_stack.frame < fail_stack.avail) \
POP_FAILURE_REG_OR_COUNT (); \
\
pat = POP_FAILURE_POINTER (); \
DEBUG_PRINT2 (" Popping pattern %p: ", pat); \
DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend); \
\
\
str = POP_FAILURE_POINTER (); \
DEBUG_PRINT2 (" Popping string %p: `", str); \
DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2); \
DEBUG_PRINT1 ("'\n"); \
\
fail_stack.frame = POP_FAILURE_INT (); \
DEBUG_PRINT2 (" Popping frame index: %d\n", fail_stack.frame); \
\
assert (fail_stack.avail >= 0); \
assert (fail_stack.frame <= fail_stack.avail); \
\
DEBUG_STATEMENT (nfailure_points_popped++); \
} while (0)
#define REG_UNSET(e) ((e) == NULL)
static reg_errcode_t regex_compile _RE_ARGS ((re_char *pattern, size_t size,
reg_syntax_t syntax,
struct re_pattern_buffer *bufp));
static void store_op1 _RE_ARGS ((re_opcode_t op, unsigned char *loc, int arg));
static void store_op2 _RE_ARGS ((re_opcode_t op, unsigned char *loc,
int arg1, int arg2));
static void insert_op1 _RE_ARGS ((re_opcode_t op, unsigned char *loc,
int arg, unsigned char *end));
static void insert_op2 _RE_ARGS ((re_opcode_t op, unsigned char *loc,
int arg1, int arg2, unsigned char *end));
static boolean at_begline_loc_p _RE_ARGS ((re_char *pattern,
re_char *p,
reg_syntax_t syntax));
static boolean at_endline_loc_p _RE_ARGS ((re_char *p,
re_char *pend,
reg_syntax_t syntax));
static re_char *skip_one_char _RE_ARGS ((re_char *p));
static int analyse_first _RE_ARGS ((re_char *p, re_char *pend,
char *fastmap, const int multibyte));
#define PATFETCH(c) \
do { \
PATFETCH_RAW (c); \
c = TRANSLATE (c); \
} while (0)
#define PATFETCH_RAW(c) \
do { \
int len; \
if (p == pend) return REG_EEND; \
c = RE_STRING_CHAR_AND_LENGTH (p, pend - p, len); \
p += len; \
} while (0)
#ifndef TRANSLATE
# define TRANSLATE(d) \
(RE_TRANSLATE_P (translate) ? RE_TRANSLATE (translate, (d)) : (d))
#endif
#define INIT_BUF_SIZE 32
#define GET_BUFFER_SPACE(n) \
while ((size_t) (b - bufp->buffer + (n)) > bufp->allocated) \
EXTEND_BUFFER ()
#define BUF_PUSH(c) \
do { \
GET_BUFFER_SPACE (1); \
*b++ = (unsigned char) (c); \
} while (0)
#define BUF_PUSH_2(c1, c2) \
do { \
GET_BUFFER_SPACE (2); \
*b++ = (unsigned char) (c1); \
*b++ = (unsigned char) (c2); \
} while (0)
#define BUF_PUSH_3(c1, c2, c3) \
do { \
GET_BUFFER_SPACE (3); \
*b++ = (unsigned char) (c1); \
*b++ = (unsigned char) (c2); \
*b++ = (unsigned char) (c3); \
} while (0)
#define STORE_JUMP(op, loc, to) \
store_op1 (op, loc, (to) - (loc) - 3)
#define STORE_JUMP2(op, loc, to, arg) \
store_op2 (op, loc, (to) - (loc) - 3, arg)
#define INSERT_JUMP(op, loc, to) \
insert_op1 (op, loc, (to) - (loc) - 3, b)
#define INSERT_JUMP2(op, loc, to, arg) \
insert_op2 (op, loc, (to) - (loc) - 3, arg, b)
#if defined _MSC_VER && !defined WIN32
# define MAX_BUF_SIZE 65500L
#else
# define MAX_BUF_SIZE (1L << 16)
#endif
#if __BOUNDED_POINTERS__
# define SET_HIGH_BOUND(P) (__ptrhigh (P) = __ptrlow (P) + bufp->allocated)
# define MOVE_BUFFER_POINTER(P) \
(__ptrlow (P) += incr, SET_HIGH_BOUND (P), __ptrvalue (P) += incr)
# define ELSE_EXTEND_BUFFER_HIGH_BOUND \
else \
{ \
SET_HIGH_BOUND (b); \
SET_HIGH_BOUND (begalt); \
if (fixup_alt_jump) \
SET_HIGH_BOUND (fixup_alt_jump); \
if (laststart) \
SET_HIGH_BOUND (laststart); \
if (pending_exact) \
SET_HIGH_BOUND (pending_exact); \
}
#else
# define MOVE_BUFFER_POINTER(P) (P) += incr
# define ELSE_EXTEND_BUFFER_HIGH_BOUND
#endif
#define EXTEND_BUFFER() \
do { \
re_char *old_buffer = bufp->buffer; \
if (bufp->allocated == MAX_BUF_SIZE) \
return REG_ESIZE; \
bufp->allocated <<= 1; \
if (bufp->allocated > MAX_BUF_SIZE) \
bufp->allocated = MAX_BUF_SIZE; \
RETALLOC (bufp->buffer, bufp->allocated, unsigned char); \
if (bufp->buffer == NULL) \
return REG_ESPACE; \
\
if (old_buffer != bufp->buffer) \
{ \
int incr = bufp->buffer - old_buffer; \
MOVE_BUFFER_POINTER (b); \
MOVE_BUFFER_POINTER (begalt); \
if (fixup_alt_jump) \
MOVE_BUFFER_POINTER (fixup_alt_jump); \
if (laststart) \
MOVE_BUFFER_POINTER (laststart); \
if (pending_exact) \
MOVE_BUFFER_POINTER (pending_exact); \
} \
ELSE_EXTEND_BUFFER_HIGH_BOUND \
} while (0)
#define MAX_REGNUM 255
typedef unsigned regnum_t;
typedef long pattern_offset_t;
typedef struct
{
pattern_offset_t begalt_offset;
pattern_offset_t fixup_alt_jump;
pattern_offset_t laststart_offset;
regnum_t regnum;
} compile_stack_elt_t;
typedef struct
{
compile_stack_elt_t *stack;
unsigned size;
unsigned avail;
} compile_stack_type;
#define INIT_COMPILE_STACK_SIZE 32
#define COMPILE_STACK_EMPTY (compile_stack.avail == 0)
#define COMPILE_STACK_FULL (compile_stack.avail == compile_stack.size)
#define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
struct range_table_work_area
{
int *table;
int allocated;
int used;
int bits;
};
#define EXTEND_RANGE_TABLE_WORK_AREA(work_area, n) \
do { \
if (((work_area).used + (n)) * sizeof (int) > (work_area).allocated) \
{ \
(work_area).allocated += 16 * sizeof (int); \
if ((work_area).table) \
(work_area).table \
= (int *) realloc ((work_area).table, (work_area).allocated); \
else \
(work_area).table \
= (int *) malloc ((work_area).allocated); \
if ((work_area).table == 0) \
FREE_STACK_RETURN (REG_ESPACE); \
} \
} while (0)
#define SET_RANGE_TABLE_WORK_AREA_BIT(work_area, bit) \
(work_area).bits |= (bit)
#define BIT_WORD 0x1
#define BIT_LOWER 0x2
#define BIT_PUNCT 0x4
#define BIT_SPACE 0x8
#define BIT_UPPER 0x10
#define BIT_MULTIBYTE 0x20
#define SET_RANGE_TABLE_WORK_AREA(work_area, range_start, range_end) \
do { \
EXTEND_RANGE_TABLE_WORK_AREA ((work_area), 2); \
(work_area).table[(work_area).used++] = (range_start); \
(work_area).table[(work_area).used++] = (range_end); \
} while (0)
#define FREE_RANGE_TABLE_WORK_AREA(work_area) \
do { \
if ((work_area).table) \
free ((work_area).table); \
} while (0)
#define CLEAR_RANGE_TABLE_WORK_USED(work_area) ((work_area).used = 0, (work_area).bits = 0)
#define RANGE_TABLE_WORK_USED(work_area) ((work_area).used)
#define RANGE_TABLE_WORK_BITS(work_area) ((work_area).bits)
#define RANGE_TABLE_WORK_ELT(work_area, i) ((work_area).table[i])
#define SET_LIST_BIT(c) (b[((c)) / BYTEWIDTH] |= 1 << ((c) % BYTEWIDTH))
#define GET_UNSIGNED_NUMBER(num) \
do { if (p != pend) \
{ \
PATFETCH (c); \
while ('0' <= c && c <= '9') \
{ \
if (num < 0) \
num = 0; \
num = num * 10 + c - '0'; \
if (p == pend) \
break; \
PATFETCH (c); \
} \
} \
} while (0)
#if WIDE_CHAR_SUPPORT
# ifdef CHARCLASS_NAME_MAX
# define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
# else
# define CHAR_CLASS_MAX_LENGTH 256
# endif
typedef wctype_t re_wctype_t;
typedef wchar_t re_wchar_t;
# define re_wctype wctype
# define re_iswctype iswctype
# define re_wctype_to_bit(cc) 0
#else
# define CHAR_CLASS_MAX_LENGTH 9
# define btowc(c) c
typedef enum { RECC_ERROR = 0,
RECC_ALNUM, RECC_ALPHA, RECC_WORD,
RECC_GRAPH, RECC_PRINT,
RECC_LOWER, RECC_UPPER,
RECC_PUNCT, RECC_CNTRL,
RECC_DIGIT, RECC_XDIGIT,
RECC_BLANK, RECC_SPACE,
RECC_MULTIBYTE, RECC_NONASCII,
RECC_ASCII, RECC_UNIBYTE
} re_wctype_t;
typedef int re_wchar_t;
static re_wctype_t
re_wctype (string)
re_char *string;
{
if (STREQ (string, "alnum")) return RECC_ALNUM;
else if (STREQ (string, "alpha")) return RECC_ALPHA;
else if (STREQ (string, "word")) return RECC_WORD;
else if (STREQ (string, "ascii")) return RECC_ASCII;
else if (STREQ (string, "nonascii")) return RECC_NONASCII;
else if (STREQ (string, "graph")) return RECC_GRAPH;
else if (STREQ (string, "lower")) return RECC_LOWER;
else if (STREQ (string, "print")) return RECC_PRINT;
else if (STREQ (string, "punct")) return RECC_PUNCT;
else if (STREQ (string, "space")) return RECC_SPACE;
else if (STREQ (string, "upper")) return RECC_UPPER;
else if (STREQ (string, "unibyte")) return RECC_UNIBYTE;
else if (STREQ (string, "multibyte")) return RECC_MULTIBYTE;
else if (STREQ (string, "digit")) return RECC_DIGIT;
else if (STREQ (string, "xdigit")) return RECC_XDIGIT;
else if (STREQ (string, "cntrl")) return RECC_CNTRL;
else if (STREQ (string, "blank")) return RECC_BLANK;
else return 0;
}
static boolean
re_iswctype (ch, cc)
int ch;
re_wctype_t cc;
{
switch (cc)
{
case RECC_ALNUM: return ISALNUM (ch);
case RECC_ALPHA: return ISALPHA (ch);
case RECC_BLANK: return ISBLANK (ch);
case RECC_CNTRL: return ISCNTRL (ch);
case RECC_DIGIT: return ISDIGIT (ch);
case RECC_GRAPH: return ISGRAPH (ch);
case RECC_LOWER: return ISLOWER (ch);
case RECC_PRINT: return ISPRINT (ch);
case RECC_PUNCT: return ISPUNCT (ch);
case RECC_SPACE: return ISSPACE (ch);
case RECC_UPPER: return ISUPPER (ch);
case RECC_XDIGIT: return ISXDIGIT (ch);
case RECC_ASCII: return IS_REAL_ASCII (ch);
case RECC_NONASCII: return !IS_REAL_ASCII (ch);
case RECC_UNIBYTE: return ISUNIBYTE (ch);
case RECC_MULTIBYTE: return !ISUNIBYTE (ch);
case RECC_WORD: return ISWORD (ch);
case RECC_ERROR: return false;
default:
abort();
}
}
static int
re_wctype_to_bit (cc)
re_wctype_t cc;
{
switch (cc)
{
case RECC_NONASCII: case RECC_PRINT: case RECC_GRAPH:
case RECC_MULTIBYTE: return BIT_MULTIBYTE;
case RECC_ALPHA: case RECC_ALNUM: case RECC_WORD: return BIT_WORD;
case RECC_LOWER: return BIT_LOWER;
case RECC_UPPER: return BIT_UPPER;
case RECC_PUNCT: return BIT_PUNCT;
case RECC_SPACE: return BIT_SPACE;
case RECC_ASCII: case RECC_DIGIT: case RECC_XDIGIT: case RECC_CNTRL:
case RECC_BLANK: case RECC_UNIBYTE: case RECC_ERROR: return 0;
default:
abort();
}
}
#endif
#if defined WINDOWSNT && defined emacs && defined QUIT
extern int immediate_quit;
# define IMMEDIATE_QUIT_CHECK \
do { \
if (immediate_quit) QUIT; \
} while (0)
#else
# define IMMEDIATE_QUIT_CHECK ((void)0)
#endif
#ifndef MATCH_MAY_ALLOCATE
static fail_stack_type fail_stack;
static int regs_allocated_size;
static re_char ** regstart, ** regend;
static re_char **best_regstart, **best_regend;
static
regex_grow_registers (num_regs)
int num_regs;
{
if (num_regs > regs_allocated_size)
{
RETALLOC_IF (regstart, num_regs, re_char *);
RETALLOC_IF (regend, num_regs, re_char *);
RETALLOC_IF (best_regstart, num_regs, re_char *);
RETALLOC_IF (best_regend, num_regs, re_char *);
regs_allocated_size = num_regs;
}
}
#endif
static boolean group_in_compile_stack _RE_ARGS ((compile_stack_type
compile_stack,
regnum_t regnum));
#define FIXUP_ALT_JUMP() \
do { \
if (fixup_alt_jump) \
STORE_JUMP (jump, fixup_alt_jump, b); \
} while (0)
#define FREE_STACK_RETURN(value) \
do { \
FREE_RANGE_TABLE_WORK_AREA (range_table_work); \
free (compile_stack.stack); \
return value; \
} while (0)
static reg_errcode_t
regex_compile (pattern, size, syntax, bufp)
re_char *pattern;
size_t size;
reg_syntax_t syntax;
struct re_pattern_buffer *bufp;
{
register re_wchar_t c, c1;
re_char *p1;
register unsigned char *b;
compile_stack_type compile_stack;
#ifdef AIX
unsigned char *p = pattern;
#else
re_char *p = pattern;
#endif
re_char *pend = pattern + size;
RE_TRANSLATE_TYPE translate = bufp->translate;
unsigned char *pending_exact = 0;
unsigned char *laststart = 0;
unsigned char *begalt;
re_char *beg_interval;
unsigned char *fixup_alt_jump = 0;
regnum_t regnum = 0;
struct range_table_work_area range_table_work;
const boolean multibyte = RE_MULTIBYTE_P (bufp);
#ifdef DEBUG
debug++;
DEBUG_PRINT1 ("\nCompiling pattern: ");
if (debug > 0)
{
unsigned debug_count;
for (debug_count = 0; debug_count < size; debug_count++)
putchar (pattern[debug_count]);
putchar ('\n');
}
#endif
compile_stack.stack = TALLOC (INIT_COMPILE_STACK_SIZE, compile_stack_elt_t);
if (compile_stack.stack == NULL)
return REG_ESPACE;
compile_stack.size = INIT_COMPILE_STACK_SIZE;
compile_stack.avail = 0;
range_table_work.table = 0;
range_table_work.allocated = 0;
bufp->syntax = syntax;
bufp->fastmap_accurate = 0;
bufp->not_bol = bufp->not_eol = 0;
bufp->used = 0;
bufp->re_nsub = 0;
#if !defined emacs && !defined SYNTAX_TABLE
init_syntax_once ();
#endif
if (bufp->allocated == 0)
{
if (bufp->buffer)
{
RETALLOC (bufp->buffer, INIT_BUF_SIZE, unsigned char);
}
else
{
bufp->buffer = TALLOC (INIT_BUF_SIZE, unsigned char);
}
if (!bufp->buffer) FREE_STACK_RETURN (REG_ESPACE);
bufp->allocated = INIT_BUF_SIZE;
}
begalt = b = bufp->buffer;
while (p != pend)
{
PATFETCH (c);
switch (c)
{
case '^':
{
if (
p == pattern + 1
|| syntax & RE_CONTEXT_INDEP_ANCHORS
|| at_begline_loc_p (pattern, p, syntax))
BUF_PUSH ((syntax & RE_NO_NEWLINE_ANCHOR) ? begbuf : begline);
else
goto normal_char;
}
break;
case '$':
{
if (
p == pend
|| syntax & RE_CONTEXT_INDEP_ANCHORS
|| at_endline_loc_p (p, pend, syntax))
BUF_PUSH ((syntax & RE_NO_NEWLINE_ANCHOR) ? endbuf : endline);
else
goto normal_char;
}
break;
case '+':
case '?':
if ((syntax & RE_BK_PLUS_QM)
|| (syntax & RE_LIMITED_OPS))
goto normal_char;
handle_plus:
case '*':
if (!laststart)
{
if (syntax & RE_CONTEXT_INVALID_OPS)
FREE_STACK_RETURN (REG_BADRPT);
else if (!(syntax & RE_CONTEXT_INDEP_OPS))
goto normal_char;
}
{
boolean zero_times_ok = 0, many_times_ok = 0;
boolean greedy = 1;
for (;;)
{
if ((syntax & RE_FRUGAL)
&& c == '?' && (zero_times_ok || many_times_ok))
greedy = 0;
else
{
zero_times_ok |= c != '+';
many_times_ok |= c != '?';
}
if (p == pend)
break;
else if (*p == '*'
|| (!(syntax & RE_BK_PLUS_QM)
&& (*p == '+' || *p == '?')))
;
else if (syntax & RE_BK_PLUS_QM && *p == '\\')
{
if (p+1 == pend)
FREE_STACK_RETURN (REG_EESCAPE);
if (p[1] == '+' || p[1] == '?')
PATFETCH (c);
else
break;
}
else
break;
PATFETCH (c);
}
if (!laststart || laststart == b)
break;
if (greedy)
{
if (many_times_ok)
{
boolean simple = skip_one_char (laststart) == b;
unsigned int startoffset = 0;
re_opcode_t ofj =
(simple || !analyse_first (laststart, b, NULL, 0)) ?
on_failure_jump : on_failure_jump_loop;
assert (skip_one_char (laststart) <= b);
if (!zero_times_ok && simple)
{
unsigned char *p1, *p2;
startoffset = b - laststart;
GET_BUFFER_SPACE (startoffset);
p1 = b; p2 = laststart;
while (p2 < p1)
*b++ = *p2++;
zero_times_ok = 1;
}
GET_BUFFER_SPACE (6);
if (!zero_times_ok)
STORE_JUMP (ofj, b, b + 6);
else
INSERT_JUMP (simple ? on_failure_jump_smart : ofj,
laststart + startoffset, b + 6);
b += 3;
STORE_JUMP (jump, b, laststart + startoffset);
b += 3;
}
else
{
assert (zero_times_ok);
GET_BUFFER_SPACE (3);
INSERT_JUMP (on_failure_jump, laststart, b + 3);
b += 3;
}
}
else
{
GET_BUFFER_SPACE (7);
if (many_times_ok)
{
boolean emptyp = analyse_first (laststart, b, NULL, 0);
if (emptyp) BUF_PUSH (no_op);
STORE_JUMP (emptyp ? on_failure_jump_nastyloop
: on_failure_jump, b, laststart);
b += 3;
if (zero_times_ok)
{
INSERT_JUMP (jump, laststart, b);
b += 3;
}
}
else
{
INSERT_JUMP (jump, laststart, b + 3);
b += 3;
INSERT_JUMP (on_failure_jump, laststart, laststart + 6);
b += 3;
}
}
}
pending_exact = 0;
break;
case '.':
laststart = b;
BUF_PUSH (anychar);
break;
case '[':
{
CLEAR_RANGE_TABLE_WORK_USED (range_table_work);
if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
GET_BUFFER_SPACE (34);
laststart = b;
BUF_PUSH (*p == '^' ? charset_not : charset);
if (*p == '^')
p++;
p1 = p;
BUF_PUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH);
if ((re_opcode_t) b[-2] == charset_not
&& (syntax & RE_HAT_LISTS_NOT_NEWLINE))
SET_LIST_BIT ('\n');
for (;;)
{
boolean escaped_char = false;
const unsigned char *p2 = p;
if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
PATFETCH (c);
if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) && c == '\\')
{
if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
PATFETCH (c);
escaped_char = true;
}
else
{
if (c == ']' && p2 != p1)
break;
}
if (!escaped_char &&
syntax & RE_CHAR_CLASSES && c == '[' && *p == ':')
{
unsigned char str[CHAR_CLASS_MAX_LENGTH + 1];
const unsigned char *class_beg;
PATFETCH (c);
c1 = 0;
class_beg = p;
if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
for (;;)
{
PATFETCH (c);
if ((c == ':' && *p == ']') || p == pend)
break;
if (c1 < CHAR_CLASS_MAX_LENGTH)
str[c1++] = c;
else
str[0] = '\0';
}
str[c1] = '\0';
if (c == ':' && *p == ']')
{
int ch;
re_wctype_t cc;
cc = re_wctype (str);
if (cc == 0)
FREE_STACK_RETURN (REG_ECTYPE);
PATFETCH (c);
if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
if (multibyte)
SET_RANGE_TABLE_WORK_AREA_BIT (range_table_work,
re_wctype_to_bit (cc));
for (ch = 0; ch < 1 << BYTEWIDTH; ++ch)
{
int translated = TRANSLATE (ch);
if (re_iswctype (btowc (ch), cc))
SET_LIST_BIT (translated);
}
continue;
}
else
{
p = class_beg;
SET_LIST_BIT ('[');
c = ':';
}
}
if (p < pend && p[0] == '-' && p[1] != ']')
{
PATFETCH (c1);
PATFETCH (c1);
if (SINGLE_BYTE_CHAR_P (c))
{
if (! SINGLE_BYTE_CHAR_P (c1))
{
int charset = CHAR_CHARSET (c1);
int c2 = MAKE_CHAR (charset, 0, 0);
SET_RANGE_TABLE_WORK_AREA (range_table_work,
c2, c1);
c1 = 0377;
}
}
else if (!SAME_CHARSET_P (c, c1))
FREE_STACK_RETURN (REG_ERANGE);
}
else
c1 = c;
if (SINGLE_BYTE_CHAR_P (c))
{
re_wchar_t this_char;
int range_start = c, range_end = c1;
if (range_start > range_end)
{
if (syntax & RE_NO_EMPTY_RANGES)
FREE_STACK_RETURN (REG_ERANGE);
}
else
{
for (this_char = range_start; this_char <= range_end;
this_char++)
SET_LIST_BIT (TRANSLATE (this_char));
}
}
else
SET_RANGE_TABLE_WORK_AREA (range_table_work, c, c1);
}
while ((int) b[-1] > 0 && b[b[-1] - 1] == 0)
b[-1]--;
b += b[-1];
if (RANGE_TABLE_WORK_USED (range_table_work)
|| RANGE_TABLE_WORK_BITS (range_table_work))
{
int i;
int used = RANGE_TABLE_WORK_USED (range_table_work);
GET_BUFFER_SPACE (4 + used * 3);
laststart[1] |= 0x80;
*b++ = RANGE_TABLE_WORK_BITS (range_table_work) & 0xff;
*b++ = RANGE_TABLE_WORK_BITS (range_table_work) >> 8;
STORE_NUMBER_AND_INCR (b, used / 2);
for (i = 0; i < used; i++)
STORE_CHARACTER_AND_INCR
(b, RANGE_TABLE_WORK_ELT (range_table_work, i));
}
}
break;
case '(':
if (syntax & RE_NO_BK_PARENS)
goto handle_open;
else
goto normal_char;
case ')':
if (syntax & RE_NO_BK_PARENS)
goto handle_close;
else
goto normal_char;
case '\n':
if (syntax & RE_NEWLINE_ALT)
goto handle_alt;
else
goto normal_char;
case '|':
if (syntax & RE_NO_BK_VBAR)
goto handle_alt;
else
goto normal_char;
case '{':
if (syntax & RE_INTERVALS && syntax & RE_NO_BK_BRACES)
goto handle_interval;
else
goto normal_char;
case '\\':
if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
PATFETCH_RAW (c);
switch (c)
{
case '(':
if (syntax & RE_NO_BK_PARENS)
goto normal_backslash;
handle_open:
{
int shy = 0;
if (p+1 < pend)
{
if ((syntax & RE_SHY_GROUPS) && *p == '?')
{
PATFETCH (c);
PATFETCH (c);
switch (c)
{
case ':': shy = 1; break;
default:
FREE_STACK_RETURN (REG_BADPAT);
}
}
}
if (!shy)
{
bufp->re_nsub++;
regnum++;
}
if (COMPILE_STACK_FULL)
{
RETALLOC (compile_stack.stack, compile_stack.size << 1,
compile_stack_elt_t);
if (compile_stack.stack == NULL) return REG_ESPACE;
compile_stack.size <<= 1;
}
COMPILE_STACK_TOP.begalt_offset = begalt - bufp->buffer;
COMPILE_STACK_TOP.fixup_alt_jump
= fixup_alt_jump ? fixup_alt_jump - bufp->buffer + 1 : 0;
COMPILE_STACK_TOP.laststart_offset = b - bufp->buffer;
COMPILE_STACK_TOP.regnum = shy ? -regnum : regnum;
if (regnum <= MAX_REGNUM && !shy)
BUF_PUSH_2 (start_memory, regnum);
compile_stack.avail++;
fixup_alt_jump = 0;
laststart = 0;
begalt = b;
pending_exact = 0;
break;
}
case ')':
if (syntax & RE_NO_BK_PARENS) goto normal_backslash;
if (COMPILE_STACK_EMPTY)
{
if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
goto normal_backslash;
else
FREE_STACK_RETURN (REG_ERPAREN);
}
handle_close:
FIXUP_ALT_JUMP ();
if (COMPILE_STACK_EMPTY)
{
if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
goto normal_char;
else
FREE_STACK_RETURN (REG_ERPAREN);
}
assert (compile_stack.avail != 0);
{
regnum_t this_group_regnum;
compile_stack.avail--;
begalt = bufp->buffer + COMPILE_STACK_TOP.begalt_offset;
fixup_alt_jump
= COMPILE_STACK_TOP.fixup_alt_jump
? bufp->buffer + COMPILE_STACK_TOP.fixup_alt_jump - 1
: 0;
laststart = bufp->buffer + COMPILE_STACK_TOP.laststart_offset;
this_group_regnum = COMPILE_STACK_TOP.regnum;
pending_exact = 0;
if (this_group_regnum <= MAX_REGNUM && this_group_regnum > 0)
BUF_PUSH_2 (stop_memory, this_group_regnum);
}
break;
case '|':
if (syntax & RE_LIMITED_OPS || syntax & RE_NO_BK_VBAR)
goto normal_backslash;
handle_alt:
if (syntax & RE_LIMITED_OPS)
goto normal_char;
GET_BUFFER_SPACE (3);
INSERT_JUMP (on_failure_jump, begalt, b + 6);
pending_exact = 0;
b += 3;
FIXUP_ALT_JUMP ();
fixup_alt_jump = b;
GET_BUFFER_SPACE (3);
b += 3;
laststart = 0;
begalt = b;
break;
case '{':
if (!(syntax & RE_INTERVALS)
|| (syntax & RE_NO_BK_BRACES))
goto normal_backslash;
handle_interval:
{
int lower_bound = 0, upper_bound = -1;
beg_interval = p;
if (p == pend)
FREE_STACK_RETURN (REG_EBRACE);
GET_UNSIGNED_NUMBER (lower_bound);
if (c == ',')
GET_UNSIGNED_NUMBER (upper_bound);
else
upper_bound = lower_bound;
if (lower_bound < 0 || upper_bound > RE_DUP_MAX
|| (upper_bound >= 0 && lower_bound > upper_bound))
FREE_STACK_RETURN (REG_BADBR);
if (!(syntax & RE_NO_BK_BRACES))
{
if (c != '\\')
FREE_STACK_RETURN (REG_BADBR);
PATFETCH (c);
}
if (c != '}')
FREE_STACK_RETURN (REG_BADBR);
if (!laststart)
{
if (syntax & RE_CONTEXT_INVALID_OPS)
FREE_STACK_RETURN (REG_BADRPT);
else if (syntax & RE_CONTEXT_INDEP_OPS)
laststart = b;
else
goto unfetch_interval;
}
if (upper_bound == 0)
b = laststart;
else if (lower_bound == 1 && upper_bound == 1)
;
else
{
unsigned int nbytes = (upper_bound < 0 ? 3
: upper_bound > 1 ? 5 : 0);
unsigned int startoffset = 0;
GET_BUFFER_SPACE (20);
if (lower_bound == 0)
{
INSERT_JUMP (on_failure_jump_loop, laststart,
b + 3 + nbytes);
b += 3;
}
else
{
INSERT_JUMP2 (succeed_n, laststart,
b + 5 + nbytes,
lower_bound);
b += 5;
insert_op2 (set_number_at, laststart, 5, lower_bound, b);
b += 5;
startoffset += 5;
}
if (upper_bound < 0)
{
STORE_JUMP (jump, b, laststart + startoffset);
b += 3;
}
else if (upper_bound > 1)
{
STORE_JUMP2 (jump_n, b, laststart + startoffset,
upper_bound - 1);
b += 5;
insert_op2 (set_number_at, laststart, b - laststart,
upper_bound - 1, b);
b += 5;
}
}
pending_exact = 0;
beg_interval = NULL;
}
break;
unfetch_interval:
assert (beg_interval);
p = beg_interval;
beg_interval = NULL;
c = '{';
if (!(syntax & RE_NO_BK_BRACES))
{
assert (p > pattern && p[-1] == '\\');
goto normal_backslash;
}
else
goto normal_char;
#ifdef emacs
case '=':
BUF_PUSH (at_dot);
break;
case 's':
laststart = b;
PATFETCH (c);
BUF_PUSH_2 (syntaxspec, syntax_spec_code[c]);
break;
case 'S':
laststart = b;
PATFETCH (c);
BUF_PUSH_2 (notsyntaxspec, syntax_spec_code[c]);
break;
case 'c':
laststart = b;
PATFETCH_RAW (c);
BUF_PUSH_2 (categoryspec, c);
break;
case 'C':
laststart = b;
PATFETCH_RAW (c);
BUF_PUSH_2 (notcategoryspec, c);
break;
#endif
case 'w':
if (syntax & RE_NO_GNU_OPS)
goto normal_char;
laststart = b;
BUF_PUSH_2 (syntaxspec, Sword);
break;
case 'W':
if (syntax & RE_NO_GNU_OPS)
goto normal_char;
laststart = b;
BUF_PUSH_2 (notsyntaxspec, Sword);
break;
case '<':
if (syntax & RE_NO_GNU_OPS)
goto normal_char;
BUF_PUSH (wordbeg);
break;
case '>':
if (syntax & RE_NO_GNU_OPS)
goto normal_char;
BUF_PUSH (wordend);
break;
case 'b':
if (syntax & RE_NO_GNU_OPS)
goto normal_char;
BUF_PUSH (wordbound);
break;
case 'B':
if (syntax & RE_NO_GNU_OPS)
goto normal_char;
BUF_PUSH (notwordbound);
break;
case '`':
if (syntax & RE_NO_GNU_OPS)
goto normal_char;
BUF_PUSH (begbuf);
break;
case '\'':
if (syntax & RE_NO_GNU_OPS)
goto normal_char;
BUF_PUSH (endbuf);
break;
case '1': case '2': case '3': case '4': case '5':
case '6': case '7': case '8': case '9':
{
regnum_t reg;
if (syntax & RE_NO_BK_REFS)
goto normal_backslash;
reg = c - '0';
if (reg > regnum || group_in_compile_stack (compile_stack, reg))
FREE_STACK_RETURN (REG_ESUBREG);
laststart = b;
BUF_PUSH_2 (duplicate, reg);
}
break;
case '+':
case '?':
if (syntax & RE_BK_PLUS_QM)
goto handle_plus;
else
goto normal_backslash;
default:
normal_backslash:
c = TRANSLATE (c);
goto normal_char;
}
break;
default:
normal_char:
if (!pending_exact
|| pending_exact + *pending_exact + 1 != b
|| *pending_exact >= (1 << BYTEWIDTH) - MAX_MULTIBYTE_LENGTH
|| (p != pend && (*p == '*' || *p == '^'))
|| ((syntax & RE_BK_PLUS_QM)
? p + 1 < pend && *p == '\\' && (p[1] == '+' || p[1] == '?')
: p != pend && (*p == '+' || *p == '?'))
|| ((syntax & RE_INTERVALS)
&& ((syntax & RE_NO_BK_BRACES)
? p != pend && *p == '{'
: p + 1 < pend && p[0] == '\\' && p[1] == '{')))
{
laststart = b;
BUF_PUSH_2 (exactn, 0);
pending_exact = b - 1;
}
GET_BUFFER_SPACE (MAX_MULTIBYTE_LENGTH);
{
int len;
if (multibyte)
len = CHAR_STRING (c, b);
else
*b = c, len = 1;
b += len;
(*pending_exact) += len;
}
break;
}
}
FIXUP_ALT_JUMP ();
if (!COMPILE_STACK_EMPTY)
FREE_STACK_RETURN (REG_EPAREN);
if (syntax & RE_NO_POSIX_BACKTRACKING)
BUF_PUSH (succeed);
free (compile_stack.stack);
bufp->used = b - bufp->buffer;
#ifdef DEBUG
if (debug > 0)
{
re_compile_fastmap (bufp);
DEBUG_PRINT1 ("\nCompiled pattern: \n");
print_compiled_pattern (bufp);
}
debug--;
#endif
#ifndef MATCH_MAY_ALLOCATE
{
int num_regs = bufp->re_nsub + 1;
if (fail_stack.size < re_max_failures * TYPICAL_FAILURE_SIZE)
{
fail_stack.size = re_max_failures * TYPICAL_FAILURE_SIZE;
if (! fail_stack.stack)
fail_stack.stack
= (fail_stack_elt_t *) malloc (fail_stack.size
* sizeof (fail_stack_elt_t));
else
fail_stack.stack
= (fail_stack_elt_t *) realloc (fail_stack.stack,
(fail_stack.size
* sizeof (fail_stack_elt_t)));
}
regex_grow_registers (num_regs);
}
#endif
return REG_NOERROR;
}
static void
store_op1 (op, loc, arg)
re_opcode_t op;
unsigned char *loc;
int arg;
{
*loc = (unsigned char) op;
STORE_NUMBER (loc + 1, arg);
}
static void
store_op2 (op, loc, arg1, arg2)
re_opcode_t op;
unsigned char *loc;
int arg1, arg2;
{
*loc = (unsigned char) op;
STORE_NUMBER (loc + 1, arg1);
STORE_NUMBER (loc + 3, arg2);
}
static void
insert_op1 (op, loc, arg, end)
re_opcode_t op;
unsigned char *loc;
int arg;
unsigned char *end;
{
register unsigned char *pfrom = end;
register unsigned char *pto = end + 3;
while (pfrom != loc)
*--pto = *--pfrom;
store_op1 (op, loc, arg);
}
static void
insert_op2 (op, loc, arg1, arg2, end)
re_opcode_t op;
unsigned char *loc;
int arg1, arg2;
unsigned char *end;
{
register unsigned char *pfrom = end;
register unsigned char *pto = end + 5;
while (pfrom != loc)
*--pto = *--pfrom;
store_op2 (op, loc, arg1, arg2);
}
static boolean
at_begline_loc_p (pattern, p, syntax)
re_char *pattern, *p;
reg_syntax_t syntax;
{
re_char *prev = p - 2;
boolean prev_prev_backslash = prev > pattern && prev[-1] == '\\';
return
(*prev == '(' && (syntax & RE_NO_BK_PARENS || prev_prev_backslash))
|| (*prev == '|' && (syntax & RE_NO_BK_VBAR || prev_prev_backslash))
|| ((syntax & RE_SHY_GROUPS) && prev - 2 >= pattern
&& prev[-1] == '?' && prev[-2] == '('
&& (syntax & RE_NO_BK_PARENS
|| (prev - 3 >= pattern && prev[-3] == '\\')));
}
static boolean
at_endline_loc_p (p, pend, syntax)
re_char *p, *pend;
reg_syntax_t syntax;
{
re_char *next = p;
boolean next_backslash = *next == '\\';
re_char *next_next = p + 1 < pend ? p + 1 : 0;
return
(syntax & RE_NO_BK_PARENS ? *next == ')'
: next_backslash && next_next && *next_next == ')')
|| (syntax & RE_NO_BK_VBAR ? *next == '|'
: next_backslash && next_next && *next_next == '|');
}
static boolean
group_in_compile_stack (compile_stack, regnum)
compile_stack_type compile_stack;
regnum_t regnum;
{
int this_element;
for (this_element = compile_stack.avail - 1;
this_element >= 0;
this_element--)
if (compile_stack.stack[this_element].regnum == regnum)
return true;
return false;
}
static int
analyse_first (p, pend, fastmap, multibyte)
re_char *p, *pend;
char *fastmap;
const int multibyte;
{
int j, k;
boolean not;
boolean match_any_multibyte_characters = false;
assert (p);
while (p < pend)
{
re_char *p1 = p;
switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++))
{
case succeed:
return 1;
continue;
case duplicate:
p++;
continue;
case exactn:
if (fastmap)
{
int c = RE_STRING_CHAR (p + 1, pend - p);
if (SINGLE_BYTE_CHAR_P (c))
fastmap[c] = 1;
else
fastmap[p[1]] = 1;
}
break;
case anychar:
if (!fastmap) break;
return -1;
case charset_not:
if (!fastmap) break;
for (j = CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH;
j < (1 << BYTEWIDTH); j++)
fastmap[j] = 1;
case charset:
if (!fastmap) break;
not = (re_opcode_t) *(p - 1) == charset_not;
for (j = CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH - 1, p++;
j >= 0; j--)
if (!!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))) ^ not)
fastmap[j] = 1;
if ((not && multibyte)
|| (CHARSET_RANGE_TABLE_EXISTS_P (&p[-2])
&& CHARSET_RANGE_TABLE_BITS (&p[-2]) != 0))
{
set_fastmap_for_multibyte_characters:
if (match_any_multibyte_characters == false)
{
for (j = 0x80; j < 0xA0; j++)
if (BASE_LEADING_CODE_P (j))
fastmap[j] = 1;
match_any_multibyte_characters = true;
}
}
else if (!not && CHARSET_RANGE_TABLE_EXISTS_P (&p[-2])
&& match_any_multibyte_characters == false)
{
int c, count;
p += CHARSET_BITMAP_SIZE (&p[-2]) + 2;
EXTRACT_NUMBER_AND_INCR (count, p);
for (; count > 0; count--, p += 2 * 3)
{
EXTRACT_CHARACTER (c, p);
j = CHAR_CHARSET (c);
fastmap[CHARSET_LEADING_CODE_BASE (j)] = 1;
}
}
break;
case syntaxspec:
case notsyntaxspec:
if (!fastmap) break;
#ifndef emacs
not = (re_opcode_t)p[-1] == notsyntaxspec;
k = *p++;
for (j = 0; j < (1 << BYTEWIDTH); j++)
if ((SYNTAX (j) == (enum syntaxcode) k) ^ not)
fastmap[j] = 1;
break;
#else
return -1;
case categoryspec:
case notcategoryspec:
if (!fastmap) break;
not = (re_opcode_t)p[-1] == notcategoryspec;
k = *p++;
for (j = 0; j < (1 << BYTEWIDTH); j++)
if ((CHAR_HAS_CATEGORY (j, k)) ^ not)
fastmap[j] = 1;
if (multibyte)
goto set_fastmap_for_multibyte_characters;
break;
case before_dot:
case at_dot:
case after_dot:
#endif
case no_op:
case begline:
case endline:
case begbuf:
case endbuf:
case wordbound:
case notwordbound:
case wordbeg:
case wordend:
continue;
case jump:
EXTRACT_NUMBER_AND_INCR (j, p);
if (j < 0)
break;
p += j;
switch (SWITCH_ENUM_CAST ((re_opcode_t) *p))
{
case on_failure_jump:
case on_failure_keep_string_jump:
case on_failure_jump_loop:
case on_failure_jump_nastyloop:
case on_failure_jump_smart:
p++;
break;
default:
continue;
};
case on_failure_jump:
case on_failure_keep_string_jump:
case on_failure_jump_nastyloop:
case on_failure_jump_loop:
case on_failure_jump_smart:
EXTRACT_NUMBER_AND_INCR (j, p);
if (p + j <= p1)
;
else
{
int r = analyse_first (p, pend, fastmap, multibyte);
if (r) return r;
p += j;
}
continue;
case jump_n:
DEBUG_STATEMENT (EXTRACT_NUMBER (j, p); assert (j < 0));
p += 4;
continue;
case succeed_n:
DEBUG_STATEMENT (EXTRACT_NUMBER (j, p + 2); assert (j > 0));
p += 4;
continue;
case set_number_at:
p += 4;
continue;
case start_memory:
case stop_memory:
p += 1;
continue;
default:
abort ();
}
return 0;
}
return 1;
}
int
re_compile_fastmap (bufp)
struct re_pattern_buffer *bufp;
{
char *fastmap = bufp->fastmap;
int analysis;
assert (fastmap && bufp->buffer);
bzero (fastmap, 1 << BYTEWIDTH);
bufp->fastmap_accurate = 1;
analysis = analyse_first (bufp->buffer, bufp->buffer + bufp->used,
fastmap, RE_MULTIBYTE_P (bufp));
bufp->can_be_null = (analysis != 0);
return 0;
}
void
re_set_registers (bufp, regs, num_regs, starts, ends)
struct re_pattern_buffer *bufp;
struct re_registers *regs;
unsigned num_regs;
regoff_t *starts, *ends;
{
if (num_regs)
{
bufp->regs_allocated = REGS_REALLOCATE;
regs->num_regs = num_regs;
regs->start = starts;
regs->end = ends;
}
else
{
bufp->regs_allocated = REGS_UNALLOCATED;
regs->num_regs = 0;
regs->start = regs->end = (regoff_t *) 0;
}
}
WEAK_ALIAS (__re_set_registers, re_set_registers)
int
re_search (bufp, string, size, startpos, range, regs)
struct re_pattern_buffer *bufp;
const char *string;
int size, startpos, range;
struct re_registers *regs;
{
return re_search_2 (bufp, NULL, 0, string, size, startpos, range,
regs, size);
}
WEAK_ALIAS (__re_search, re_search)
#define STOP_ADDR_VSTRING(P) \
(((P) >= size1 ? string2 + size2 : string1 + size1))
#define POS_ADDR_VSTRING(POS) \
(((POS) >= size1 ? string2 - size1 : string1) + (POS))
int
re_search_2 (bufp, str1, size1, str2, size2, startpos, range, regs, stop)
struct re_pattern_buffer *bufp;
const char *str1, *str2;
int size1, size2;
int startpos;
int range;
struct re_registers *regs;
int stop;
{
int val;
re_char *string1 = (re_char*) str1;
re_char *string2 = (re_char*) str2;
register char *fastmap = bufp->fastmap;
register RE_TRANSLATE_TYPE translate = bufp->translate;
int total_size = size1 + size2;
int endpos = startpos + range;
boolean anchored_start;
const boolean multibyte = RE_MULTIBYTE_P (bufp);
if (startpos < 0 || startpos > total_size)
return -1;
if (endpos < 0)
range = 0 - startpos;
else if (endpos > total_size)
range = total_size - startpos;
if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == begbuf && range > 0)
{
if (startpos > 0)
return -1;
else
range = 0;
}
#ifdef emacs
if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == at_dot && range > 0)
{
range = PT_BYTE - BEGV_BYTE - startpos;
if (range < 0)
return -1;
}
#endif
if (fastmap && !bufp->fastmap_accurate)
re_compile_fastmap (bufp);
anchored_start = (bufp->buffer[0] == begline);
#ifdef emacs
gl_state.object = re_match_object;
{
int charpos = SYNTAX_TABLE_BYTE_TO_CHAR (POS_AS_IN_BUFFER (startpos));
SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object, charpos, 1);
}
#endif
for (;;)
{
if (anchored_start && startpos > 0)
{
if (! ((startpos <= size1 ? string1[startpos - 1]
: string2[startpos - size1 - 1])
== '\n'))
goto advance;
}
if (fastmap && startpos < total_size && !bufp->can_be_null)
{
register re_char *d;
register re_wchar_t buf_ch;
d = POS_ADDR_VSTRING (startpos);
if (range > 0)
{
register int lim = 0;
int irange = range;
if (startpos < size1 && startpos + range >= size1)
lim = range - (size1 - startpos);
if (RE_TRANSLATE_P (translate))
{
if (multibyte)
while (range > lim)
{
int buf_charlen;
buf_ch = STRING_CHAR_AND_LENGTH (d, range - lim,
buf_charlen);
buf_ch = RE_TRANSLATE (translate, buf_ch);
if (buf_ch >= 0400
|| fastmap[buf_ch])
break;
range -= buf_charlen;
d += buf_charlen;
}
else
while (range > lim
&& !fastmap[RE_TRANSLATE (translate, *d)])
{
d++;
range--;
}
}
else
while (range > lim && !fastmap[*d])
{
d++;
range--;
}
startpos += irange - range;
}
else
{
int room = (startpos >= size1
? size2 + size1 - startpos
: size1 - startpos);
buf_ch = RE_STRING_CHAR (d, room);
buf_ch = TRANSLATE (buf_ch);
if (! (buf_ch >= 0400
|| fastmap[buf_ch]))
goto advance;
}
}
if (range >= 0 && startpos == total_size && fastmap
&& !bufp->can_be_null)
return -1;
val = re_match_2_internal (bufp, string1, size1, string2, size2,
startpos, regs, stop);
#ifndef REGEX_MALLOC
# ifdef C_ALLOCA
alloca (0);
# endif
#endif
if (val >= 0)
return startpos;
if (val == -2)
return -2;
advance:
if (!range)
break;
else if (range > 0)
{
if (multibyte)
{
re_char *p = POS_ADDR_VSTRING (startpos);
re_char *pend = STOP_ADDR_VSTRING (startpos);
int len = MULTIBYTE_FORM_LENGTH (p, pend - p);
range -= len;
if (range < 0)
break;
startpos += len;
}
else
{
range--;
startpos++;
}
}
else
{
range++;
startpos--;
if (multibyte)
{
re_char *p = POS_ADDR_VSTRING (startpos);
int len = 0;
while (!CHAR_HEAD_P (*p))
p--, len++;
#if 0
if (MULTIBYTE_FORM_LENGTH (p, len + 1) != (len + 1))
;
else
#endif
{
range += len;
if (range > 0)
break;
startpos -= len;
}
}
}
}
return -1;
}
WEAK_ALIAS (__re_search_2, re_search_2)
static int bcmp_translate _RE_ARGS((re_char *s1, re_char *s2,
register int len,
RE_TRANSLATE_TYPE translate,
const int multibyte));
#define POINTER_TO_OFFSET(ptr) \
(FIRST_STRING_P (ptr) \
? ((regoff_t) ((ptr) - string1)) \
: ((regoff_t) ((ptr) - string2 + size1)))
#define PREFETCH() \
while (d == dend) \
{ \
\
if (dend == end_match_2) \
goto fail; \
\
d = string2; \
dend = end_match_2; \
}
#define PREFETCH_NOLIMIT() \
if (d == end1) \
{ \
d = string2; \
dend = end_match_2; \
} \
#define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2)
#define AT_STRINGS_END(d) ((d) == end2)
#define WORDCHAR_P(d) \
(SYNTAX ((d) == end1 ? *string2 \
: (d) == string2 - 1 ? *(end1 - 1) : *(d)) \
== Sword)
#if 0
#define AT_WORD_BOUNDARY(d) \
(AT_STRINGS_BEG (d) || AT_STRINGS_END (d) \
|| WORDCHAR_P (d - 1) != WORDCHAR_P (d))
#endif
#ifdef MATCH_MAY_ALLOCATE
# define FREE_VAR(var) if (var) { REGEX_FREE (var); var = NULL; } else
# define FREE_VARIABLES() \
do { \
REGEX_FREE_STACK (fail_stack.stack); \
FREE_VAR (regstart); \
FREE_VAR (regend); \
FREE_VAR (best_regstart); \
FREE_VAR (best_regend); \
} while (0)
#else
# define FREE_VARIABLES() ((void)0)
#endif
static re_char *
skip_one_char (p)
re_char *p;
{
switch (SWITCH_ENUM_CAST (*p++))
{
case anychar:
break;
case exactn:
p += *p + 1;
break;
case charset_not:
case charset:
if (CHARSET_RANGE_TABLE_EXISTS_P (p - 1))
{
int mcnt;
p = CHARSET_RANGE_TABLE (p - 1);
EXTRACT_NUMBER_AND_INCR (mcnt, p);
p = CHARSET_RANGE_TABLE_END (p, mcnt);
}
else
p += 1 + CHARSET_BITMAP_SIZE (p - 1);
break;
case syntaxspec:
case notsyntaxspec:
#ifdef emacs
case categoryspec:
case notcategoryspec:
#endif
p++;
break;
default:
p = NULL;
}
return p;
}
static unsigned char *
skip_noops (p, pend)
unsigned char *p, *pend;
{
int mcnt;
while (p < pend)
{
switch (SWITCH_ENUM_CAST ((re_opcode_t) *p))
{
case start_memory:
case stop_memory:
p += 2; break;
case no_op:
p += 1; break;
case jump:
p += 1;
EXTRACT_NUMBER_AND_INCR (mcnt, p);
p += mcnt;
break;
default:
return p;
}
}
assert (p == pend);
return p;
}
static int
mutually_exclusive_p (bufp, p1, p2)
struct re_pattern_buffer *bufp;
unsigned char *p1, *p2;
{
re_opcode_t op2;
const boolean multibyte = RE_MULTIBYTE_P (bufp);
unsigned char *pend = bufp->buffer + bufp->used;
assert (p1 >= bufp->buffer && p1 < pend
&& p2 >= bufp->buffer && p2 <= pend);
p2 = skip_noops (p2, pend);
assert (p1 >= bufp->buffer && p1 < pend
&& p2 >= bufp->buffer && p2 <= pend);
op2 = p2 == pend ? succeed : *p2;
switch (SWITCH_ENUM_CAST (op2))
{
case succeed:
case endbuf:
if (skip_one_char (p1))
{
DEBUG_PRINT1 (" End of pattern: fast loop.\n");
return 1;
}
break;
case endline:
case exactn:
{
register re_wchar_t c
= (re_opcode_t) *p2 == endline ? '\n'
: RE_STRING_CHAR (p2 + 2, pend - p2 - 2);
if ((re_opcode_t) *p1 == exactn)
{
if (c != RE_STRING_CHAR (p1 + 2, pend - p1 - 2))
{
DEBUG_PRINT3 (" '%c' != '%c' => fast loop.\n", c, p1[2]);
return 1;
}
}
else if ((re_opcode_t) *p1 == charset
|| (re_opcode_t) *p1 == charset_not)
{
int not = (re_opcode_t) *p1 == charset_not;
if (SINGLE_BYTE_CHAR_P (c))
{
if (c < CHARSET_BITMAP_SIZE (p1) * BYTEWIDTH
&& p1[2 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
not = !not;
}
else if (CHARSET_RANGE_TABLE_EXISTS_P (p1))
CHARSET_LOOKUP_RANGE_TABLE (not, c, p1);
if (!not)
{
DEBUG_PRINT1 (" No match => fast loop.\n");
return 1;
}
}
else if ((re_opcode_t) *p1 == anychar
&& c == '\n')
{
DEBUG_PRINT1 (" . != \\n => fast loop.\n");
return 1;
}
}
break;
case charset:
{
if ((re_opcode_t) *p1 == exactn)
return mutually_exclusive_p (bufp, p2, p1);
else if (!multibyte || !CHARSET_RANGE_TABLE_EXISTS_P (p2))
{
if ((re_opcode_t) *p1 == charset)
{
int idx;
for (idx = 0;
(idx < (int) p2[1]
&& idx < CHARSET_BITMAP_SIZE (p1));
idx++)
if ((p2[2 + idx] & p1[2 + idx]) != 0)
break;
if (idx == p2[1]
|| idx == CHARSET_BITMAP_SIZE (p1))
{
DEBUG_PRINT1 (" No match => fast loop.\n");
return 1;
}
}
else if ((re_opcode_t) *p1 == charset_not)
{
int idx;
for (idx = 0; idx < (int) p2[1]; idx++)
if (! (p2[2 + idx] == 0
|| (idx < CHARSET_BITMAP_SIZE (p1)
&& ((p2[2 + idx] & ~ p1[2 + idx]) == 0))))
break;
if (idx == p2[1])
{
DEBUG_PRINT1 (" No match => fast loop.\n");
return 1;
}
}
}
}
break;
case charset_not:
switch (SWITCH_ENUM_CAST (*p1))
{
case exactn:
case charset:
return mutually_exclusive_p (bufp, p2, p1);
case charset_not:
;
}
break;
case wordend:
case notsyntaxspec:
return ((re_opcode_t) *p1 == syntaxspec
&& p1[1] == (op2 == wordend ? Sword : p2[1]));
case wordbeg:
case syntaxspec:
return ((re_opcode_t) *p1 == notsyntaxspec
&& p1[1] == (op2 == wordend ? Sword : p2[1]));
case wordbound:
return (((re_opcode_t) *p1 == notsyntaxspec
|| (re_opcode_t) *p1 == syntaxspec)
&& p1[1] == Sword);
#ifdef emacs
case categoryspec:
return ((re_opcode_t) *p1 == notcategoryspec && p1[1] == p2[1]);
case notcategoryspec:
return ((re_opcode_t) *p1 == categoryspec && p1[1] == p2[1]);
#endif
default:
;
}
return 0;
}
#ifndef emacs
int
re_match (bufp, string, size, pos, regs)
struct re_pattern_buffer *bufp;
const char *string;
int size, pos;
struct re_registers *regs;
{
int result = re_match_2_internal (bufp, NULL, 0, (re_char*) string, size,
pos, regs, size);
# if defined C_ALLOCA && !defined REGEX_MALLOC
alloca (0);
# endif
return result;
}
WEAK_ALIAS (__re_match, re_match)
#endif
#ifdef emacs
Lisp_Object re_match_object;
#endif
int
re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
struct re_pattern_buffer *bufp;
const char *string1, *string2;
int size1, size2;
int pos;
struct re_registers *regs;
int stop;
{
int result;
#ifdef emacs
int charpos;
gl_state.object = re_match_object;
charpos = SYNTAX_TABLE_BYTE_TO_CHAR (POS_AS_IN_BUFFER (pos));
SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object, charpos, 1);
#endif
result = re_match_2_internal (bufp, (re_char*) string1, size1,
(re_char*) string2, size2,
pos, regs, stop);
#if defined C_ALLOCA && !defined REGEX_MALLOC
alloca (0);
#endif
return result;
}
WEAK_ALIAS (__re_match_2, re_match_2)
static int
re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
struct re_pattern_buffer *bufp;
re_char *string1, *string2;
int size1, size2;
int pos;
struct re_registers *regs;
int stop;
{
int mcnt;
size_t reg;
boolean not;
re_char *end1, *end2;
re_char *end_match_1, *end_match_2;
re_char *d, *dend;
re_char *dfail;
re_char *p = bufp->buffer;
re_char *pend = p + bufp->used;
RE_TRANSLATE_TYPE translate = bufp->translate;
const boolean multibyte = RE_MULTIBYTE_P (bufp);
#ifdef MATCH_MAY_ALLOCATE
fail_stack_type fail_stack;
#endif
#ifdef DEBUG
unsigned nfailure_points_pushed = 0, nfailure_points_popped = 0;
#endif
#if defined REL_ALLOC && defined REGEX_MALLOC
fail_stack_elt_t *failure_stack_ptr;
#endif
size_t num_regs = bufp->re_nsub + 1;
#ifdef MATCH_MAY_ALLOCATE
re_char **regstart, **regend;
#endif
unsigned best_regs_set = false;
#ifdef MATCH_MAY_ALLOCATE
re_char **best_regstart, **best_regend;
#endif
re_char *match_end = NULL;
#ifdef DEBUG
unsigned num_regs_pushed = 0;
#endif
DEBUG_PRINT1 ("\n\nEntering re_match_2.\n");
INIT_FAIL_STACK ();
#ifdef MATCH_MAY_ALLOCATE
if (bufp->re_nsub)
{
regstart = REGEX_TALLOC (num_regs, re_char *);
regend = REGEX_TALLOC (num_regs, re_char *);
best_regstart = REGEX_TALLOC (num_regs, re_char *);
best_regend = REGEX_TALLOC (num_regs, re_char *);
if (!(regstart && regend && best_regstart && best_regend))
{
FREE_VARIABLES ();
return -2;
}
}
else
{
regstart = regend = best_regstart = best_regend = NULL;
}
#endif
if (pos < 0 || pos > size1 + size2)
{
FREE_VARIABLES ();
return -1;
}
for (reg = 1; reg < num_regs; reg++)
regstart[reg] = regend[reg] = NULL;
if (size2 == 0 && string1 != NULL)
{
string2 = string1;
size2 = size1;
string1 = 0;
size1 = 0;
}
end1 = string1 + size1;
end2 = string2 + size2;
if (pos >= size1)
{
d = string2 + pos - size1;
dend = end_match_2 = string2 + stop - size1;
end_match_1 = end1;
}
else
{
if (stop < size1)
{
end_match_1 = string1 + stop;
end_match_2 = end_match_1;
}
else
{
end_match_1 = end1;
end_match_2 = string2 + stop - size1;
}
d = string1 + pos;
dend = end_match_1;
}
DEBUG_PRINT1 ("The compiled pattern is: ");
DEBUG_PRINT_COMPILED_PATTERN (bufp, p, pend);
DEBUG_PRINT1 ("The string to match is: `");
DEBUG_PRINT_DOUBLE_STRING (d, string1, size1, string2, size2);
DEBUG_PRINT1 ("'\n");
for (;;)
{
DEBUG_PRINT2 ("\n%p: ", p);
if (p == pend)
{
DEBUG_PRINT1 ("end of pattern ... ");
if (d != end_match_2)
{
boolean same_str_p = (FIRST_STRING_P (match_end)
== FIRST_STRING_P (d));
boolean best_match_p;
if (same_str_p)
best_match_p = d > match_end;
else
best_match_p = !FIRST_STRING_P (d);
DEBUG_PRINT1 ("backtracking.\n");
if (!FAIL_STACK_EMPTY ())
{
if (!best_regs_set || best_match_p)
{
best_regs_set = true;
match_end = d;
DEBUG_PRINT1 ("\nSAVING match as best so far.\n");
for (reg = 1; reg < num_regs; reg++)
{
best_regstart[reg] = regstart[reg];
best_regend[reg] = regend[reg];
}
}
goto fail;
}
else if (best_regs_set && !best_match_p)
{
restore_best_regs:
DEBUG_PRINT1 ("Restoring best registers.\n");
d = match_end;
dend = ((d >= string1 && d <= end1)
? end_match_1 : end_match_2);
for (reg = 1; reg < num_regs; reg++)
{
regstart[reg] = best_regstart[reg];
regend[reg] = best_regend[reg];
}
}
}
succeed_label:
DEBUG_PRINT1 ("Accepting match.\n");
if (regs && !bufp->no_sub)
{
if (bufp->regs_allocated == REGS_UNALLOCATED)
{
regs->num_regs = MAX (RE_NREGS, num_regs + 1);
regs->start = TALLOC (regs->num_regs, regoff_t);
regs->end = TALLOC (regs->num_regs, regoff_t);
if (regs->start == NULL || regs->end == NULL)
{
FREE_VARIABLES ();
return -2;
}
bufp->regs_allocated = REGS_REALLOCATE;
}
else if (bufp->regs_allocated == REGS_REALLOCATE)
{
if (regs->num_regs < num_regs + 1)
{
regs->num_regs = num_regs + 1;
RETALLOC (regs->start, regs->num_regs, regoff_t);
RETALLOC (regs->end, regs->num_regs, regoff_t);
if (regs->start == NULL || regs->end == NULL)
{
FREE_VARIABLES ();
return -2;
}
}
}
else
{
assert (bufp->regs_allocated == REGS_FIXED);
}
if (regs->num_regs > 0)
{
regs->start[0] = pos;
regs->end[0] = POINTER_TO_OFFSET (d);
}
for (reg = 1; reg < MIN (num_regs, regs->num_regs); reg++)
{
if (REG_UNSET (regstart[reg]) || REG_UNSET (regend[reg]))
regs->start[reg] = regs->end[reg] = -1;
else
{
regs->start[reg]
= (regoff_t) POINTER_TO_OFFSET (regstart[reg]);
regs->end[reg]
= (regoff_t) POINTER_TO_OFFSET (regend[reg]);
}
}
for (reg = num_regs; reg < regs->num_regs; reg++)
regs->start[reg] = regs->end[reg] = -1;
}
DEBUG_PRINT4 ("%u failure points pushed, %u popped (%u remain).\n",
nfailure_points_pushed, nfailure_points_popped,
nfailure_points_pushed - nfailure_points_popped);
DEBUG_PRINT2 ("%u registers pushed.\n", num_regs_pushed);
mcnt = POINTER_TO_OFFSET (d) - pos;
DEBUG_PRINT2 ("Returning %d from re_match_2.\n", mcnt);
FREE_VARIABLES ();
return mcnt;
}
switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++))
{
case no_op:
DEBUG_PRINT1 ("EXECUTING no_op.\n");
break;
case succeed:
DEBUG_PRINT1 ("EXECUTING succeed.\n");
goto succeed_label;
case exactn:
mcnt = *p++;
DEBUG_PRINT2 ("EXECUTING exactn %d.\n", mcnt);
dfail = d;
if (RE_TRANSLATE_P (translate))
{
if (multibyte)
do
{
int pat_charlen, buf_charlen;
unsigned int pat_ch, buf_ch;
PREFETCH ();
pat_ch = STRING_CHAR_AND_LENGTH (p, pend - p, pat_charlen);
buf_ch = STRING_CHAR_AND_LENGTH (d, dend - d, buf_charlen);
if (RE_TRANSLATE (translate, buf_ch)
!= pat_ch)
{
d = dfail;
goto fail;
}
p += pat_charlen;
d += buf_charlen;
mcnt -= pat_charlen;
}
while (mcnt > 0);
else
do
{
PREFETCH ();
if (RE_TRANSLATE (translate, *d) != *p++)
{
d = dfail;
goto fail;
}
d++;
}
while (--mcnt);
}
else
{
do
{
PREFETCH ();
if (*d++ != *p++)
{
d = dfail;
goto fail;
}
}
while (--mcnt);
}
break;
case anychar:
{
int buf_charlen;
re_wchar_t buf_ch;
DEBUG_PRINT1 ("EXECUTING anychar.\n");
PREFETCH ();
buf_ch = RE_STRING_CHAR_AND_LENGTH (d, dend - d, buf_charlen);
buf_ch = TRANSLATE (buf_ch);
if ((!(bufp->syntax & RE_DOT_NEWLINE)
&& buf_ch == '\n')
|| ((bufp->syntax & RE_DOT_NOT_NULL)
&& buf_ch == '\000'))
goto fail;
DEBUG_PRINT2 (" Matched `%d'.\n", *d);
d += buf_charlen;
}
break;
case charset:
case charset_not:
{
register unsigned int c;
boolean not = (re_opcode_t) *(p - 1) == charset_not;
int len;
re_char *range_table;
int range_table_exists;
int count = 0;
DEBUG_PRINT2 ("EXECUTING charset%s.\n", not ? "_not" : "");
range_table_exists = CHARSET_RANGE_TABLE_EXISTS_P (&p[-1]);
if (range_table_exists)
{
range_table = CHARSET_RANGE_TABLE (&p[-1]);
EXTRACT_NUMBER_AND_INCR (count, range_table);
}
PREFETCH ();
c = RE_STRING_CHAR_AND_LENGTH (d, dend - d, len);
c = TRANSLATE (c);
if (SINGLE_BYTE_CHAR_P (c))
{
if (c < (unsigned) (CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH)
&& p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
not = !not;
}
#ifdef emacs
else if (range_table_exists)
{
int class_bits = CHARSET_RANGE_TABLE_BITS (&p[-1]);
if ( (class_bits & BIT_LOWER && ISLOWER (c))
| (class_bits & BIT_MULTIBYTE)
| (class_bits & BIT_PUNCT && ISPUNCT (c))
| (class_bits & BIT_SPACE && ISSPACE (c))
| (class_bits & BIT_UPPER && ISUPPER (c))
| (class_bits & BIT_WORD && ISWORD (c)))
not = !not;
else
CHARSET_LOOKUP_RANGE_TABLE_RAW (not, c, range_table, count);
}
#endif
if (range_table_exists)
p = CHARSET_RANGE_TABLE_END (range_table, count);
else
p += CHARSET_BITMAP_SIZE (&p[-1]) + 1;
if (!not) goto fail;
d += len;
break;
}
case start_memory:
DEBUG_PRINT2 ("EXECUTING start_memory %d:\n", *p);
PUSH_FAILURE_REG ((unsigned int)*p);
regstart[*p] = d;
regend[*p] = NULL;
DEBUG_PRINT2 (" regstart: %d\n", POINTER_TO_OFFSET (regstart[*p]));
p += 1;
break;
case stop_memory:
DEBUG_PRINT2 ("EXECUTING stop_memory %d:\n", *p);
assert (!REG_UNSET (regstart[*p]));
regend[*p] = d;
DEBUG_PRINT2 (" regend: %d\n", POINTER_TO_OFFSET (regend[*p]));
p += 1;
break;
case duplicate:
{
register re_char *d2, *dend2;
int regno = *p++;
DEBUG_PRINT2 ("EXECUTING duplicate %d.\n", regno);
if (REG_UNSET (regstart[regno]) || REG_UNSET (regend[regno]))
goto fail;
d2 = regstart[regno];
dfail = d;
dend2 = ((FIRST_STRING_P (regstart[regno])
== FIRST_STRING_P (regend[regno]))
? regend[regno] : end_match_1);
for (;;)
{
while (d2 == dend2)
{
if (dend2 == end_match_2) break;
if (dend2 == regend[regno]) break;
d2 = string2;
dend2 = regend[regno];
}
if (d2 == dend2) break;
PREFETCH ();
mcnt = dend - d;
if (mcnt > dend2 - d2)
mcnt = dend2 - d2;
if (RE_TRANSLATE_P (translate)
? bcmp_translate (d, d2, mcnt, translate, multibyte)
: memcmp (d, d2, mcnt))
{
d = dfail;
goto fail;
}
d += mcnt, d2 += mcnt;
}
}
break;
case begline:
DEBUG_PRINT1 ("EXECUTING begline.\n");
if (AT_STRINGS_BEG (d))
{
if (!bufp->not_bol) break;
}
else
{
unsigned char c;
GET_CHAR_BEFORE_2 (c, d, string1, end1, string2, end2);
if (c == '\n')
break;
}
goto fail;
case endline:
DEBUG_PRINT1 ("EXECUTING endline.\n");
if (AT_STRINGS_END (d))
{
if (!bufp->not_eol) break;
}
else
{
PREFETCH_NOLIMIT ();
if (*d == '\n')
break;
}
goto fail;
case begbuf:
DEBUG_PRINT1 ("EXECUTING begbuf.\n");
if (AT_STRINGS_BEG (d))
break;
goto fail;
case endbuf:
DEBUG_PRINT1 ("EXECUTING endbuf.\n");
if (AT_STRINGS_END (d))
break;
goto fail;
case on_failure_keep_string_jump:
EXTRACT_NUMBER_AND_INCR (mcnt, p);
DEBUG_PRINT3 ("EXECUTING on_failure_keep_string_jump %d (to %p):\n",
mcnt, p + mcnt);
PUSH_FAILURE_POINT (p - 3, NULL);
break;
case on_failure_jump_nastyloop:
EXTRACT_NUMBER_AND_INCR (mcnt, p);
DEBUG_PRINT3 ("EXECUTING on_failure_jump_nastyloop %d (to %p):\n",
mcnt, p + mcnt);
assert ((re_opcode_t)p[-4] == no_op);
CHECK_INFINITE_LOOP (p - 4, d);
PUSH_FAILURE_POINT (p - 3, d);
break;
case on_failure_jump_loop:
on_failure:
EXTRACT_NUMBER_AND_INCR (mcnt, p);
DEBUG_PRINT3 ("EXECUTING on_failure_jump_loop %d (to %p):\n",
mcnt, p + mcnt);
CHECK_INFINITE_LOOP (p - 3, d);
PUSH_FAILURE_POINT (p - 3, d);
break;
case on_failure_jump:
IMMEDIATE_QUIT_CHECK;
EXTRACT_NUMBER_AND_INCR (mcnt, p);
DEBUG_PRINT3 ("EXECUTING on_failure_jump %d (to %p):\n",
mcnt, p + mcnt);
PUSH_FAILURE_POINT (p -3, d);
break;
case on_failure_jump_smart:
IMMEDIATE_QUIT_CHECK;
EXTRACT_NUMBER_AND_INCR (mcnt, p);
DEBUG_PRINT3 ("EXECUTING on_failure_jump_smart %d (to %p).\n",
mcnt, p + mcnt);
{
re_char *p1 = p;
unsigned char *p2 = (unsigned char*) p + mcnt;
unsigned char *p3 = (unsigned char*) p - 3;
p -= 3;
EXTRACT_NUMBER (mcnt, p2 - 2);
assert (skip_one_char (p1) == p2 - 3);
assert ((re_opcode_t) p2[-3] == jump && p2 + mcnt == p);
DEBUG_STATEMENT (debug += 2);
if (mutually_exclusive_p (bufp, p1, p2))
{
DEBUG_PRINT1 (" smart exclusive => fast loop.\n");
*p3 = (unsigned char) on_failure_keep_string_jump;
STORE_NUMBER (p2 - 2, mcnt + 3);
}
else
{
DEBUG_PRINT1 (" smart default => slow loop.\n");
*p3 = (unsigned char) on_failure_jump;
}
DEBUG_STATEMENT (debug -= 2);
}
break;
case jump:
unconditional_jump:
IMMEDIATE_QUIT_CHECK;
EXTRACT_NUMBER_AND_INCR (mcnt, p);
DEBUG_PRINT2 ("EXECUTING jump %d ", mcnt);
p += mcnt;
DEBUG_PRINT2 ("(to %p).\n", p);
break;
case succeed_n:
EXTRACT_NUMBER (mcnt, p + 2);
DEBUG_PRINT2 ("EXECUTING succeed_n %d.\n", mcnt);
if (mcnt != 0)
{
unsigned char *p2 = (unsigned char*) p + 2;
mcnt--;
p += 4;
PUSH_NUMBER (p2, mcnt);
}
else
goto on_failure;
break;
case jump_n:
EXTRACT_NUMBER (mcnt, p + 2);
DEBUG_PRINT2 ("EXECUTING jump_n %d.\n", mcnt);
if (mcnt != 0)
{
unsigned char *p2 = (unsigned char*) p + 2;
mcnt--;
PUSH_NUMBER (p2, mcnt);
goto unconditional_jump;
}
else
p += 4;
break;
case set_number_at:
{
unsigned char *p2;
DEBUG_PRINT1 ("EXECUTING set_number_at.\n");
EXTRACT_NUMBER_AND_INCR (mcnt, p);
p2 = (unsigned char*) p + mcnt;
EXTRACT_NUMBER_AND_INCR (mcnt, p);
DEBUG_PRINT3 (" Setting %p to %d.\n", p2, mcnt);
PUSH_NUMBER (p2, mcnt);
break;
}
case wordbound:
case notwordbound:
not = (re_opcode_t) *(p - 1) == notwordbound;
DEBUG_PRINT2 ("EXECUTING %swordbound.\n", not?"not":"");
if (AT_STRINGS_BEG (d) || AT_STRINGS_END (d))
not = !not;
else
{
re_wchar_t c1, c2;
int s1, s2;
#ifdef emacs
int offset = PTR_TO_OFFSET (d - 1);
int charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
UPDATE_SYNTAX_TABLE (charpos);
#endif
GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
s1 = SYNTAX (c1);
#ifdef emacs
UPDATE_SYNTAX_TABLE_FORWARD (charpos + 1);
#endif
PREFETCH_NOLIMIT ();
c2 = RE_STRING_CHAR (d, dend - d);
s2 = SYNTAX (c2);
if (
((s1 == Sword) != (s2 == Sword))
|| ((s1 == Sword) && WORD_BOUNDARY_P (c1, c2)))
not = !not;
}
if (not)
break;
else
goto fail;
case wordbeg:
DEBUG_PRINT1 ("EXECUTING wordbeg.\n");
if (AT_STRINGS_END (d))
goto fail;
else
{
re_wchar_t c1, c2;
int s1, s2;
#ifdef emacs
int offset = PTR_TO_OFFSET (d);
int charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
UPDATE_SYNTAX_TABLE (charpos);
#endif
PREFETCH ();
c2 = RE_STRING_CHAR (d, dend - d);
s2 = SYNTAX (c2);
if (s2 != Sword)
goto fail;
if (!AT_STRINGS_BEG (d))
{
GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
#ifdef emacs
UPDATE_SYNTAX_TABLE_BACKWARD (charpos - 1);
#endif
s1 = SYNTAX (c1);
if ((s1 == Sword) && !WORD_BOUNDARY_P (c1, c2))
goto fail;
}
}
break;
case wordend:
DEBUG_PRINT1 ("EXECUTING wordend.\n");
if (AT_STRINGS_BEG (d))
goto fail;
else
{
re_wchar_t c1, c2;
int s1, s2;
#ifdef emacs
int offset = PTR_TO_OFFSET (d) - 1;
int charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
UPDATE_SYNTAX_TABLE (charpos);
#endif
GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
s1 = SYNTAX (c1);
if (s1 != Sword)
goto fail;
if (!AT_STRINGS_END (d))
{
PREFETCH_NOLIMIT ();
c2 = RE_STRING_CHAR (d, dend - d);
#ifdef emacs
UPDATE_SYNTAX_TABLE_FORWARD (charpos);
#endif
s2 = SYNTAX (c2);
if ((s2 == Sword) && !WORD_BOUNDARY_P (c1, c2))
goto fail;
}
}
break;
case syntaxspec:
case notsyntaxspec:
not = (re_opcode_t) *(p - 1) == notsyntaxspec;
mcnt = *p++;
DEBUG_PRINT3 ("EXECUTING %ssyntaxspec %d.\n", not?"not":"", mcnt);
PREFETCH ();
#ifdef emacs
{
int offset = PTR_TO_OFFSET (d);
int pos1 = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
UPDATE_SYNTAX_TABLE (pos1);
}
#endif
{
int len;
re_wchar_t c;
c = RE_STRING_CHAR_AND_LENGTH (d, dend - d, len);
if ((SYNTAX (c) != (enum syntaxcode) mcnt) ^ not)
goto fail;
d += len;
}
break;
#ifdef emacs
case before_dot:
DEBUG_PRINT1 ("EXECUTING before_dot.\n");
if (PTR_BYTE_POS (d) >= PT_BYTE)
goto fail;
break;
case at_dot:
DEBUG_PRINT1 ("EXECUTING at_dot.\n");
if (PTR_BYTE_POS (d) != PT_BYTE)
goto fail;
break;
case after_dot:
DEBUG_PRINT1 ("EXECUTING after_dot.\n");
if (PTR_BYTE_POS (d) <= PT_BYTE)
goto fail;
break;
case categoryspec:
case notcategoryspec:
not = (re_opcode_t) *(p - 1) == notcategoryspec;
mcnt = *p++;
DEBUG_PRINT3 ("EXECUTING %scategoryspec %d.\n", not?"not":"", mcnt);
PREFETCH ();
{
int len;
re_wchar_t c;
c = RE_STRING_CHAR_AND_LENGTH (d, dend - d, len);
if ((!CHAR_HAS_CATEGORY (c, mcnt)) ^ not)
goto fail;
d += len;
}
break;
#endif
default:
abort ();
}
continue;
fail:
IMMEDIATE_QUIT_CHECK;
if (!FAIL_STACK_EMPTY ())
{
re_char *str, *pat;
DEBUG_PRINT1 ("\nFAIL:\n");
POP_FAILURE_POINT (str, pat);
switch (SWITCH_ENUM_CAST ((re_opcode_t) *pat++))
{
case on_failure_keep_string_jump:
assert (str == NULL);
goto continue_failure_jump;
case on_failure_jump_nastyloop:
assert ((re_opcode_t)pat[-2] == no_op);
PUSH_FAILURE_POINT (pat - 2, str);
case on_failure_jump_loop:
case on_failure_jump:
case succeed_n:
d = str;
continue_failure_jump:
EXTRACT_NUMBER_AND_INCR (mcnt, pat);
p = pat + mcnt;
break;
case no_op:
goto fail;
default:
abort();
}
assert (p >= bufp->buffer && p <= pend);
if (d >= string1 && d <= end1)
dend = end_match_1;
}
else
break;
}
if (best_regs_set)
goto restore_best_regs;
FREE_VARIABLES ();
return -1;
}
static int
bcmp_translate (s1, s2, len, translate, multibyte)
re_char *s1, *s2;
register int len;
RE_TRANSLATE_TYPE translate;
const int multibyte;
{
register re_char *p1 = s1, *p2 = s2;
re_char *p1_end = s1 + len;
re_char *p2_end = s2 + len;
while (p1 < p1_end && p2 < p2_end)
{
int p1_charlen, p2_charlen;
re_wchar_t p1_ch, p2_ch;
p1_ch = RE_STRING_CHAR_AND_LENGTH (p1, p1_end - p1, p1_charlen);
p2_ch = RE_STRING_CHAR_AND_LENGTH (p2, p2_end - p2, p2_charlen);
if (RE_TRANSLATE (translate, p1_ch)
!= RE_TRANSLATE (translate, p2_ch))
return 1;
p1 += p1_charlen, p2 += p2_charlen;
}
if (p1 != p1_end || p2 != p2_end)
return 1;
return 0;
}
const char *
re_compile_pattern (pattern, length, bufp)
const char *pattern;
size_t length;
struct re_pattern_buffer *bufp;
{
reg_errcode_t ret;
bufp->regs_allocated = REGS_UNALLOCATED;
bufp->no_sub = 0;
ret = regex_compile ((re_char*) pattern, length, re_syntax_options, bufp);
if (!ret)
return NULL;
return gettext (re_error_msgid[(int) ret]);
}
WEAK_ALIAS (__re_compile_pattern, re_compile_pattern)
#if defined _REGEX_RE_COMP || defined _LIBC
static struct re_pattern_buffer re_comp_buf;
char *
# ifdef _LIBC
weak_function
# endif
re_comp (s)
const char *s;
{
reg_errcode_t ret;
if (!s)
{
if (!re_comp_buf.buffer)
return (char *) gettext ("No previous regular expression");
return 0;
}
if (!re_comp_buf.buffer)
{
re_comp_buf.buffer = (unsigned char *) malloc (200);
if (re_comp_buf.buffer == NULL)
return (char *) gettext (re_error_msgid[(int) REG_ESPACE]);
re_comp_buf.allocated = 200;
re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH);
if (re_comp_buf.fastmap == NULL)
return (char *) gettext (re_error_msgid[(int) REG_ESPACE]);
}
ret = regex_compile (s, strlen (s), re_syntax_options, &re_comp_buf);
if (!ret)
return NULL;
return (char *) gettext (re_error_msgid[(int) ret]);
}
int
# ifdef _LIBC
weak_function
# endif
re_exec (s)
const char *s;
{
const int len = strlen (s);
return
0 <= re_search (&re_comp_buf, s, len, 0, len, (struct re_registers *) 0);
}
#endif
#ifndef emacs
int
regcomp (preg, pattern, cflags)
regex_t *preg;
const char *pattern;
int cflags;
{
reg_errcode_t ret;
reg_syntax_t syntax
= (cflags & REG_EXTENDED) ?
RE_SYNTAX_POSIX_EXTENDED : RE_SYNTAX_POSIX_BASIC;
preg->buffer = 0;
preg->allocated = 0;
preg->used = 0;
preg->fastmap = (char *) malloc (1 << BYTEWIDTH);
if (cflags & REG_ICASE)
{
unsigned i;
preg->translate
= (RE_TRANSLATE_TYPE) malloc (CHAR_SET_SIZE
* sizeof (*(RE_TRANSLATE_TYPE)0));
if (preg->translate == NULL)
return (int) REG_ESPACE;
for (i = 0; i < CHAR_SET_SIZE; i++)
preg->translate[i] = ISUPPER (i) ? TOLOWER (i) : i;
}
else
preg->translate = NULL;
if (cflags & REG_NEWLINE)
{
syntax &= ~RE_DOT_NEWLINE;
syntax |= RE_HAT_LISTS_NOT_NEWLINE;
}
else
syntax |= RE_NO_NEWLINE_ANCHOR;
preg->no_sub = !!(cflags & REG_NOSUB);
ret = regex_compile ((re_char*) pattern, strlen (pattern), syntax, preg);
if (ret == REG_ERPAREN)
ret = REG_EPAREN;
if (ret == REG_NOERROR && preg->fastmap)
{
re_compile_fastmap (preg);
if (preg->can_be_null)
{
free (preg->fastmap);
preg->fastmap = NULL;
}
}
return (int) ret;
}
WEAK_ALIAS (__regcomp, regcomp)
int
regexec (preg, string, nmatch, pmatch, eflags)
const regex_t *preg;
const char *string;
size_t nmatch;
regmatch_t pmatch[];
int eflags;
{
int ret;
struct re_registers regs;
regex_t private_preg;
int len = strlen (string);
boolean want_reg_info = !preg->no_sub && nmatch > 0 && pmatch;
private_preg = *preg;
private_preg.not_bol = !!(eflags & REG_NOTBOL);
private_preg.not_eol = !!(eflags & REG_NOTEOL);
private_preg.regs_allocated = REGS_FIXED;
if (want_reg_info)
{
regs.num_regs = nmatch;
regs.start = TALLOC (nmatch * 2, regoff_t);
if (regs.start == NULL)
return (int) REG_NOMATCH;
regs.end = regs.start + nmatch;
}
ret = re_search (&private_preg, string, len,
0, len,
want_reg_info ? ®s : (struct re_registers *) 0);
if (want_reg_info)
{
if (ret >= 0)
{
unsigned r;
for (r = 0; r < nmatch; r++)
{
pmatch[r].rm_so = regs.start[r];
pmatch[r].rm_eo = regs.end[r];
}
}
free (regs.start);
}
return ret >= 0 ? (int) REG_NOERROR : (int) REG_NOMATCH;
}
WEAK_ALIAS (__regexec, regexec)
size_t
regerror (errcode, preg, errbuf, errbuf_size)
int errcode;
const regex_t *preg;
char *errbuf;
size_t errbuf_size;
{
const char *msg;
size_t msg_size;
if (errcode < 0
|| errcode >= (sizeof (re_error_msgid) / sizeof (re_error_msgid[0])))
abort ();
msg = gettext (re_error_msgid[errcode]);
msg_size = strlen (msg) + 1;
if (errbuf_size != 0)
{
if (msg_size > errbuf_size)
{
strncpy (errbuf, msg, errbuf_size - 1);
errbuf[errbuf_size - 1] = 0;
}
else
strcpy (errbuf, msg);
}
return msg_size;
}
WEAK_ALIAS (__regerror, regerror)
void
regfree (preg)
regex_t *preg;
{
if (preg->buffer != NULL)
free (preg->buffer);
preg->buffer = NULL;
preg->allocated = 0;
preg->used = 0;
if (preg->fastmap != NULL)
free (preg->fastmap);
preg->fastmap = NULL;
preg->fastmap_accurate = 0;
if (preg->translate != NULL)
free (preg->translate);
preg->translate = NULL;
}
WEAK_ALIAS (__regfree, regfree)
#endif