gnus-start.el (gnus-clean-old-newsrc): Always remove 'unexist' marks if `gnus-newsrc...
[bpt/emacs.git] / src / regex.c
CommitLineData
e318085a 1/* Extended regular expression matching and search library, version
0b32bf0e 2 0.12. (Implements POSIX draft P1003.2/D11.2, except for some of the
bc78d348
KB
3 internationalization features.)
4
ab422c4d 5 Copyright (C) 1993-2013 Free Software Foundation, Inc.
bc78d348 6
fa9a63c5
RM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
e468b87f 9 the Free Software Foundation; either version 3, or (at your option)
fa9a63c5
RM
10 any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
7814e705 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
fa9a63c5
RM
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
fee0bd5f 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
fa9a63c5 19
6df42991 20/* TODO:
505bde11 21 - structure the opcode space into opcode+flag.
dc1e502d 22 - merge with glibc's regex.[ch].
01618498 23 - replace (succeed_n + jump_n + set_number_at) with something that doesn't
6dcf2d0e
SM
24 need to modify the compiled regexp so that re_match can be reentrant.
25 - get rid of on_failure_jump_smart by doing the optimization in re_comp
26 rather than at run-time, so that re_match can be reentrant.
01618498 27*/
505bde11 28
b7432bb2 29/* AIX requires this to be the first thing in the file. */
0b32bf0e 30#if defined _AIX && !defined REGEX_MALLOC
fa9a63c5
RM
31 #pragma alloca
32#endif
33
b8df54ff
PE
34/* Ignore some GCC warnings for now. This section should go away
35 once the Emacs and Gnulib regex code is merged. */
31ff141c 36#if 4 < __GNUC__ + (5 <= __GNUC_MINOR__) || defined __clang__
b8df54ff
PE
37# pragma GCC diagnostic ignored "-Wstrict-overflow"
38# ifndef emacs
b8df54ff
PE
39# pragma GCC diagnostic ignored "-Wunused-function"
40# pragma GCC diagnostic ignored "-Wunused-macros"
41# pragma GCC diagnostic ignored "-Wunused-result"
42# pragma GCC diagnostic ignored "-Wunused-variable"
43# endif
44#endif
45
31ff141c
PE
46#if 4 < __GNUC__ + (5 <= __GNUC_MINOR__) && ! defined __clang__
47# pragma GCC diagnostic ignored "-Wunused-but-set-variable"
48#endif
49
cf38a720 50#include <config.h>
fa9a63c5 51
0e926e56
PE
52#include <stddef.h>
53
54#ifdef emacs
4bb91c68
SM
55/* We need this for `regex.h', and perhaps for the Emacs include files. */
56# include <sys/types.h>
57#endif
fa9a63c5 58
14473664
SM
59/* Whether to use ISO C Amendment 1 wide char functions.
60 Those should not be used for Emacs since it uses its own. */
5e5388f6
GM
61#if defined _LIBC
62#define WIDE_CHAR_SUPPORT 1
63#else
14473664 64#define WIDE_CHAR_SUPPORT \
5e5388f6
GM
65 (HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_BTOWC && !emacs)
66#endif
14473664 67
fa463103 68/* For platform which support the ISO C amendment 1 functionality we
14473664 69 support user defined character classes. */
a0ad02f7 70#if WIDE_CHAR_SUPPORT
14473664
SM
71/* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
72# include <wchar.h>
73# include <wctype.h>
74#endif
75
c0f9ea08
SM
76#ifdef _LIBC
77/* We have to keep the namespace clean. */
78# define regfree(preg) __regfree (preg)
79# define regexec(pr, st, nm, pm, ef) __regexec (pr, st, nm, pm, ef)
80# define regcomp(preg, pattern, cflags) __regcomp (preg, pattern, cflags)
ec869672 81# define regerror(err_code, preg, errbuf, errbuf_size) \
5e617bc2 82 __regerror (err_code, preg, errbuf, errbuf_size)
c0f9ea08
SM
83# define re_set_registers(bu, re, nu, st, en) \
84 __re_set_registers (bu, re, nu, st, en)
85# define re_match_2(bufp, string1, size1, string2, size2, pos, regs, stop) \
86 __re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
87# define re_match(bufp, string, size, pos, regs) \
88 __re_match (bufp, string, size, pos, regs)
89# define re_search(bufp, string, size, startpos, range, regs) \
90 __re_search (bufp, string, size, startpos, range, regs)
91# define re_compile_pattern(pattern, length, bufp) \
92 __re_compile_pattern (pattern, length, bufp)
93# define re_set_syntax(syntax) __re_set_syntax (syntax)
94# define re_search_2(bufp, st1, s1, st2, s2, startpos, range, regs, stop) \
95 __re_search_2 (bufp, st1, s1, st2, s2, startpos, range, regs, stop)
96# define re_compile_fastmap(bufp) __re_compile_fastmap (bufp)
97
14473664
SM
98/* Make sure we call libc's function even if the user overrides them. */
99# define btowc __btowc
100# define iswctype __iswctype
101# define wctype __wctype
102
c0f9ea08
SM
103# define WEAK_ALIAS(a,b) weak_alias (a, b)
104
105/* We are also using some library internals. */
106# include <locale/localeinfo.h>
107# include <locale/elem-hash.h>
108# include <langinfo.h>
109#else
110# define WEAK_ALIAS(a,b)
111#endif
112
4bb91c68 113/* This is for other GNU distributions with internationalized messages. */
0b32bf0e 114#if HAVE_LIBINTL_H || defined _LIBC
fa9a63c5
RM
115# include <libintl.h>
116#else
117# define gettext(msgid) (msgid)
118#endif
119
5e69f11e
RM
120#ifndef gettext_noop
121/* This define is so xgettext can find the internationalizable
122 strings. */
0b32bf0e 123# define gettext_noop(String) String
5e69f11e
RM
124#endif
125
fa9a63c5
RM
126/* The `emacs' switch turns on certain matching commands
127 that make sense only in Emacs. */
128#ifdef emacs
129
0b32bf0e 130# include "lisp.h"
e5560ff7 131# include "character.h"
0b32bf0e 132# include "buffer.h"
b18215fc
RS
133
134/* Make syntax table lookup grant data in gl_state. */
0b32bf0e 135# define SYNTAX_ENTRY_VIA_PROPERTY
b18215fc 136
0b32bf0e 137# include "syntax.h"
0b32bf0e 138# include "category.h"
fa9a63c5 139
7689ef0b
EZ
140# ifdef malloc
141# undef malloc
142# endif
0b32bf0e 143# define malloc xmalloc
7689ef0b
EZ
144# ifdef realloc
145# undef realloc
146# endif
0b32bf0e 147# define realloc xrealloc
7689ef0b
EZ
148# ifdef free
149# undef free
150# endif
0b32bf0e 151# define free xfree
9abbd165 152
7814e705 153/* Converts the pointer to the char to BEG-based offset from the start. */
0b32bf0e
SM
154# define PTR_TO_OFFSET(d) POS_AS_IN_BUFFER (POINTER_TO_OFFSET (d))
155# define POS_AS_IN_BUFFER(p) ((p) + (NILP (re_match_object) || BUFFERP (re_match_object)))
156
157# define RE_MULTIBYTE_P(bufp) ((bufp)->multibyte)
bf216479 158# define RE_TARGET_MULTIBYTE_P(bufp) ((bufp)->target_multibyte)
62a6e103
AS
159# define RE_STRING_CHAR(p, multibyte) \
160 (multibyte ? (STRING_CHAR (p)) : (*(p)))
161# define RE_STRING_CHAR_AND_LENGTH(p, len, multibyte) \
162 (multibyte ? (STRING_CHAR_AND_LENGTH (p, len)) : ((len) = 1, *(p)))
2d1675e4 163
4c0354d7 164# define RE_CHAR_TO_MULTIBYTE(c) UNIBYTE_TO_CHAR (c)
cf9c99bc 165
2afc21f5 166# define RE_CHAR_TO_UNIBYTE(c) CHAR_TO_BYTE_SAFE (c)
cf9c99bc 167
6fdd04b0
KH
168/* Set C a (possibly converted to multibyte) character before P. P
169 points into a string which is the virtual concatenation of STR1
170 (which ends at END1) or STR2 (which ends at END2). */
bf216479
KH
171# define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2) \
172 do { \
02cb78b5 173 if (target_multibyte) \
bf216479
KH
174 { \
175 re_char *dtemp = (p) == (str2) ? (end1) : (p); \
176 re_char *dlimit = ((p) > (str2) && (p) <= (end2)) ? (str2) : (str1); \
177 while (dtemp-- > dlimit && !CHAR_HEAD_P (*dtemp)); \
62a6e103 178 c = STRING_CHAR (dtemp); \
bf216479
KH
179 } \
180 else \
181 { \
182 (c = ((p) == (str2) ? (end1) : (p))[-1]); \
cf9c99bc 183 (c) = RE_CHAR_TO_MULTIBYTE (c); \
bf216479 184 } \
2d1675e4
SM
185 } while (0)
186
6fdd04b0
KH
187/* Set C a (possibly converted to multibyte) character at P, and set
188 LEN to the byte length of that character. */
189# define GET_CHAR_AFTER(c, p, len) \
190 do { \
02cb78b5 191 if (target_multibyte) \
62a6e103 192 (c) = STRING_CHAR_AND_LENGTH (p, len); \
6fdd04b0
KH
193 else \
194 { \
cf9c99bc 195 (c) = *p; \
6fdd04b0 196 len = 1; \
cf9c99bc 197 (c) = RE_CHAR_TO_MULTIBYTE (c); \
6fdd04b0 198 } \
8f924df7 199 } while (0)
4e8a9132 200
fa9a63c5
RM
201#else /* not emacs */
202
203/* If we are not linking with Emacs proper,
204 we can't use the relocating allocator
205 even if config.h says that we can. */
0b32bf0e 206# undef REL_ALLOC
fa9a63c5 207
4004364e 208# include <unistd.h>
fa9a63c5 209
a77f947b
CY
210/* When used in Emacs's lib-src, we need xmalloc and xrealloc. */
211
b8df54ff 212static void *
d2762c86 213xmalloc (size_t size)
a77f947b 214{
38182d90 215 void *val = malloc (size);
a77f947b
CY
216 if (!val && size)
217 {
218 write (2, "virtual memory exhausted\n", 25);
219 exit (1);
220 }
221 return val;
222}
223
b8df54ff 224static void *
d2762c86 225xrealloc (void *block, size_t size)
a77f947b 226{
38182d90 227 void *val;
a77f947b
CY
228 /* We must call malloc explicitly when BLOCK is 0, since some
229 reallocs don't do this. */
230 if (! block)
38182d90 231 val = malloc (size);
a77f947b 232 else
38182d90 233 val = realloc (block, size);
a77f947b
CY
234 if (!val && size)
235 {
236 write (2, "virtual memory exhausted\n", 25);
237 exit (1);
238 }
239 return val;
240}
241
a073faa6
CY
242# ifdef malloc
243# undef malloc
244# endif
245# define malloc xmalloc
246# ifdef realloc
247# undef realloc
248# endif
249# define realloc xrealloc
250
f5d9e83a 251# include <stdbool.h>
9cfdb3ec 252# include <string.h>
fa9a63c5
RM
253
254/* Define the syntax stuff for \<, \>, etc. */
255
990b2375 256/* Sword must be nonzero for the wordchar pattern commands in re_match_2. */
669fa600 257enum syntaxcode { Swhitespace = 0, Sword = 1, Ssymbol = 2 };
fa9a63c5 258
e934739e 259/* Dummy macros for non-Emacs environments. */
0b32bf0e
SM
260# define CHAR_CHARSET(c) 0
261# define CHARSET_LEADING_CODE_BASE(c) 0
262# define MAX_MULTIBYTE_LENGTH 1
263# define RE_MULTIBYTE_P(x) 0
bf216479 264# define RE_TARGET_MULTIBYTE_P(x) 0
0b32bf0e
SM
265# define WORD_BOUNDARY_P(c1, c2) (0)
266# define CHAR_HEAD_P(p) (1)
267# define SINGLE_BYTE_CHAR_P(c) (1)
268# define SAME_CHARSET_P(c1, c2) (1)
aa3830c4 269# define BYTES_BY_CHAR_HEAD(p) (1)
70806df6 270# define PREV_CHAR_BOUNDARY(p, limit) ((p)--)
62a6e103
AS
271# define STRING_CHAR(p) (*(p))
272# define RE_STRING_CHAR(p, multibyte) STRING_CHAR (p)
0b32bf0e 273# define CHAR_STRING(c, s) (*(s) = (c), 1)
62a6e103
AS
274# define STRING_CHAR_AND_LENGTH(p, actual_len) ((actual_len) = 1, *(p))
275# define RE_STRING_CHAR_AND_LENGTH(p, len, multibyte) STRING_CHAR_AND_LENGTH (p, len)
cf9c99bc
KH
276# define RE_CHAR_TO_MULTIBYTE(c) (c)
277# define RE_CHAR_TO_UNIBYTE(c) (c)
0b32bf0e 278# define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2) \
b18215fc 279 (c = ((p) == (str2) ? *((end1) - 1) : *((p) - 1)))
6fdd04b0
KH
280# define GET_CHAR_AFTER(c, p, len) \
281 (c = *p, len = 1)
0b32bf0e 282# define MAKE_CHAR(charset, c1, c2) (c1)
9117d724
KH
283# define BYTE8_TO_CHAR(c) (c)
284# define CHAR_BYTE8_P(c) (0)
bf216479 285# define CHAR_LEADING_CODE(c) (c)
8f924df7 286
fa9a63c5 287#endif /* not emacs */
4e8a9132
SM
288
289#ifndef RE_TRANSLATE
0b32bf0e
SM
290# define RE_TRANSLATE(TBL, C) ((unsigned char)(TBL)[C])
291# define RE_TRANSLATE_P(TBL) (TBL)
4e8a9132 292#endif
fa9a63c5
RM
293\f
294/* Get the interface, including the syntax bits. */
295#include "regex.h"
296
f71b19b6
DL
297/* isalpha etc. are used for the character classes. */
298#include <ctype.h>
fa9a63c5 299
f71b19b6 300#ifdef emacs
fa9a63c5 301
f71b19b6 302/* 1 if C is an ASCII character. */
0b32bf0e 303# define IS_REAL_ASCII(c) ((c) < 0200)
fa9a63c5 304
f71b19b6 305/* 1 if C is a unibyte character. */
0b32bf0e 306# define ISUNIBYTE(c) (SINGLE_BYTE_CHAR_P ((c)))
96cc36cc 307
f71b19b6 308/* The Emacs definitions should not be directly affected by locales. */
96cc36cc 309
f71b19b6 310/* In Emacs, these are only used for single-byte characters. */
0b32bf0e
SM
311# define ISDIGIT(c) ((c) >= '0' && (c) <= '9')
312# define ISCNTRL(c) ((c) < ' ')
313# define ISXDIGIT(c) (((c) >= '0' && (c) <= '9') \
f71b19b6
DL
314 || ((c) >= 'a' && (c) <= 'f') \
315 || ((c) >= 'A' && (c) <= 'F'))
96cc36cc
RS
316
317/* This is only used for single-byte characters. */
0b32bf0e 318# define ISBLANK(c) ((c) == ' ' || (c) == '\t')
96cc36cc
RS
319
320/* The rest must handle multibyte characters. */
321
0b32bf0e 322# define ISGRAPH(c) (SINGLE_BYTE_CHAR_P (c) \
f71b19b6 323 ? (c) > ' ' && !((c) >= 0177 && (c) <= 0237) \
96cc36cc
RS
324 : 1)
325
14473664 326# define ISPRINT(c) (SINGLE_BYTE_CHAR_P (c) \
f71b19b6 327 ? (c) >= ' ' && !((c) >= 0177 && (c) <= 0237) \
96cc36cc
RS
328 : 1)
329
0b32bf0e 330# define ISALNUM(c) (IS_REAL_ASCII (c) \
f71b19b6
DL
331 ? (((c) >= 'a' && (c) <= 'z') \
332 || ((c) >= 'A' && (c) <= 'Z') \
333 || ((c) >= '0' && (c) <= '9')) \
96cc36cc
RS
334 : SYNTAX (c) == Sword)
335
0b32bf0e 336# define ISALPHA(c) (IS_REAL_ASCII (c) \
f71b19b6
DL
337 ? (((c) >= 'a' && (c) <= 'z') \
338 || ((c) >= 'A' && (c) <= 'Z')) \
96cc36cc
RS
339 : SYNTAX (c) == Sword)
340
5da9919f 341# define ISLOWER(c) lowercasep (c)
96cc36cc 342
0b32bf0e 343# define ISPUNCT(c) (IS_REAL_ASCII (c) \
f71b19b6
DL
344 ? ((c) > ' ' && (c) < 0177 \
345 && !(((c) >= 'a' && (c) <= 'z') \
4bb91c68
SM
346 || ((c) >= 'A' && (c) <= 'Z') \
347 || ((c) >= '0' && (c) <= '9'))) \
96cc36cc
RS
348 : SYNTAX (c) != Sword)
349
0b32bf0e 350# define ISSPACE(c) (SYNTAX (c) == Swhitespace)
96cc36cc 351
5da9919f 352# define ISUPPER(c) uppercasep (c)
96cc36cc 353
0b32bf0e 354# define ISWORD(c) (SYNTAX (c) == Sword)
96cc36cc
RS
355
356#else /* not emacs */
357
f71b19b6 358/* 1 if C is an ASCII character. */
0b32bf0e 359# define IS_REAL_ASCII(c) ((c) < 0200)
f71b19b6
DL
360
361/* This distinction is not meaningful, except in Emacs. */
0b32bf0e
SM
362# define ISUNIBYTE(c) 1
363
364# ifdef isblank
0e926e56 365# define ISBLANK(c) isblank (c)
0b32bf0e
SM
366# else
367# define ISBLANK(c) ((c) == ' ' || (c) == '\t')
368# endif
369# ifdef isgraph
0e926e56 370# define ISGRAPH(c) isgraph (c)
0b32bf0e 371# else
0e926e56 372# define ISGRAPH(c) (isprint (c) && !isspace (c))
0b32bf0e
SM
373# endif
374
0e926e56 375/* Solaris defines ISPRINT so we must undefine it first. */
4bb91c68 376# undef ISPRINT
0e926e56
PE
377# define ISPRINT(c) isprint (c)
378# define ISDIGIT(c) isdigit (c)
379# define ISALNUM(c) isalnum (c)
380# define ISALPHA(c) isalpha (c)
381# define ISCNTRL(c) iscntrl (c)
382# define ISLOWER(c) islower (c)
383# define ISPUNCT(c) ispunct (c)
384# define ISSPACE(c) isspace (c)
385# define ISUPPER(c) isupper (c)
386# define ISXDIGIT(c) isxdigit (c)
0b32bf0e 387
5e617bc2 388# define ISWORD(c) ISALPHA (c)
0b32bf0e 389
4bb91c68 390# ifdef _tolower
5e617bc2 391# define TOLOWER(c) _tolower (c)
4bb91c68 392# else
5e617bc2 393# define TOLOWER(c) tolower (c)
4bb91c68
SM
394# endif
395
396/* How many characters in the character set. */
397# define CHAR_SET_SIZE 256
398
0b32bf0e 399# ifdef SYNTAX_TABLE
f71b19b6 400
0b32bf0e 401extern char *re_syntax_table;
f71b19b6 402
0b32bf0e
SM
403# else /* not SYNTAX_TABLE */
404
0b32bf0e
SM
405static char re_syntax_table[CHAR_SET_SIZE];
406
407static void
d2762c86 408init_syntax_once (void)
0b32bf0e
SM
409{
410 register int c;
411 static int done = 0;
412
413 if (done)
414 return;
415
72af86bd 416 memset (re_syntax_table, 0, sizeof re_syntax_table);
0b32bf0e 417
4bb91c68
SM
418 for (c = 0; c < CHAR_SET_SIZE; ++c)
419 if (ISALNUM (c))
420 re_syntax_table[c] = Sword;
fa9a63c5 421
669fa600 422 re_syntax_table['_'] = Ssymbol;
fa9a63c5 423
0b32bf0e
SM
424 done = 1;
425}
426
427# endif /* not SYNTAX_TABLE */
96cc36cc 428
4bb91c68
SM
429# define SYNTAX(c) re_syntax_table[(c)]
430
96cc36cc
RS
431#endif /* not emacs */
432\f
261cb4bb 433#define SIGN_EXTEND_CHAR(c) ((signed char) (c))
fa9a63c5
RM
434\f
435/* Should we use malloc or alloca? If REGEX_MALLOC is not defined, we
436 use `alloca' instead of `malloc'. This is because using malloc in
437 re_search* or re_match* could cause memory leaks when C-g is used in
438 Emacs; also, malloc is slower and causes storage fragmentation. On
5e69f11e
RM
439 the other hand, malloc is more portable, and easier to debug.
440
fa9a63c5
RM
441 Because we sometimes use alloca, some routines have to be macros,
442 not functions -- `alloca'-allocated space disappears at the end of the
443 function it is called in. */
444
445#ifdef REGEX_MALLOC
446
0b32bf0e
SM
447# define REGEX_ALLOCATE malloc
448# define REGEX_REALLOCATE(source, osize, nsize) realloc (source, nsize)
449# define REGEX_FREE free
fa9a63c5
RM
450
451#else /* not REGEX_MALLOC */
452
453/* Emacs already defines alloca, sometimes. */
0b32bf0e 454# ifndef alloca
fa9a63c5
RM
455
456/* Make alloca work the best possible way. */
0b32bf0e
SM
457# ifdef __GNUC__
458# define alloca __builtin_alloca
459# else /* not __GNUC__ */
7f585e7a 460# ifdef HAVE_ALLOCA_H
0b32bf0e
SM
461# include <alloca.h>
462# endif /* HAVE_ALLOCA_H */
463# endif /* not __GNUC__ */
fa9a63c5 464
0b32bf0e 465# endif /* not alloca */
fa9a63c5 466
0b32bf0e 467# define REGEX_ALLOCATE alloca
fa9a63c5
RM
468
469/* Assumes a `char *destination' variable. */
0b32bf0e 470# define REGEX_REALLOCATE(source, osize, nsize) \
fa9a63c5 471 (destination = (char *) alloca (nsize), \
4bb91c68 472 memcpy (destination, source, osize))
fa9a63c5
RM
473
474/* No need to do anything to free, after alloca. */
0b32bf0e 475# define REGEX_FREE(arg) ((void)0) /* Do nothing! But inhibit gcc warning. */
fa9a63c5
RM
476
477#endif /* not REGEX_MALLOC */
478
479/* Define how to allocate the failure stack. */
480
0b32bf0e 481#if defined REL_ALLOC && defined REGEX_MALLOC
4297555e 482
0b32bf0e 483# define REGEX_ALLOCATE_STACK(size) \
fa9a63c5 484 r_alloc (&failure_stack_ptr, (size))
0b32bf0e 485# define REGEX_REALLOCATE_STACK(source, osize, nsize) \
fa9a63c5 486 r_re_alloc (&failure_stack_ptr, (nsize))
0b32bf0e 487# define REGEX_FREE_STACK(ptr) \
fa9a63c5
RM
488 r_alloc_free (&failure_stack_ptr)
489
4297555e 490#else /* not using relocating allocator */
fa9a63c5 491
0b32bf0e 492# ifdef REGEX_MALLOC
fa9a63c5 493
0b32bf0e
SM
494# define REGEX_ALLOCATE_STACK malloc
495# define REGEX_REALLOCATE_STACK(source, osize, nsize) realloc (source, nsize)
496# define REGEX_FREE_STACK free
fa9a63c5 497
0b32bf0e 498# else /* not REGEX_MALLOC */
fa9a63c5 499
0b32bf0e 500# define REGEX_ALLOCATE_STACK alloca
fa9a63c5 501
0b32bf0e 502# define REGEX_REALLOCATE_STACK(source, osize, nsize) \
fa9a63c5 503 REGEX_REALLOCATE (source, osize, nsize)
7814e705 504/* No need to explicitly free anything. */
0b32bf0e 505# define REGEX_FREE_STACK(arg) ((void)0)
fa9a63c5 506
0b32bf0e 507# endif /* not REGEX_MALLOC */
4297555e 508#endif /* not using relocating allocator */
fa9a63c5
RM
509
510
511/* True if `size1' is non-NULL and PTR is pointing anywhere inside
512 `string1' or just past its end. This works if PTR is NULL, which is
513 a good thing. */
25fe55af 514#define FIRST_STRING_P(ptr) \
fa9a63c5
RM
515 (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
516
517/* (Re)Allocate N items of type T using malloc, or fail. */
518#define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t)))
519#define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
fa9a63c5
RM
520#define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
521
4bb91c68 522#define BYTEWIDTH 8 /* In bits. */
fa9a63c5
RM
523
524#define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
525
526#undef MAX
527#undef MIN
528#define MAX(a, b) ((a) > (b) ? (a) : (b))
529#define MIN(a, b) ((a) < (b) ? (a) : (b))
530
66f0296e 531/* Type of source-pattern and string chars. */
a6fc3b5c
EZ
532#ifdef _MSC_VER
533typedef unsigned char re_char;
534#else
66f0296e 535typedef const unsigned char re_char;
a6fc3b5c 536#endif
66f0296e 537
fa9a63c5 538typedef char boolean;
fa9a63c5 539
261cb4bb
PE
540static regoff_t re_match_2_internal (struct re_pattern_buffer *bufp,
541 re_char *string1, size_t size1,
542 re_char *string2, size_t size2,
543 ssize_t pos,
544 struct re_registers *regs,
545 ssize_t stop);
fa9a63c5
RM
546\f
547/* These are the command codes that appear in compiled regular
4bb91c68 548 expressions. Some opcodes are followed by argument bytes. A
fa9a63c5
RM
549 command code can specify any interpretation whatsoever for its
550 arguments. Zero bytes may appear in the compiled regular expression. */
551
552typedef enum
553{
554 no_op = 0,
555
4bb91c68 556 /* Succeed right away--no more backtracking. */
fa9a63c5
RM
557 succeed,
558
25fe55af 559 /* Followed by one byte giving n, then by n literal bytes. */
fa9a63c5
RM
560 exactn,
561
25fe55af 562 /* Matches any (more or less) character. */
fa9a63c5
RM
563 anychar,
564
25fe55af
RS
565 /* Matches any one char belonging to specified set. First
566 following byte is number of bitmap bytes. Then come bytes
567 for a bitmap saying which chars are in. Bits in each byte
568 are ordered low-bit-first. A character is in the set if its
569 bit is 1. A character too large to have a bit in the map is
96cc36cc
RS
570 automatically not in the set.
571
572 If the length byte has the 0x80 bit set, then that stuff
573 is followed by a range table:
574 2 bytes of flags for character sets (low 8 bits, high 8 bits)
0b32bf0e 575 See RANGE_TABLE_WORK_BITS below.
01618498 576 2 bytes, the number of pairs that follow (upto 32767)
96cc36cc 577 pairs, each 2 multibyte characters,
0b32bf0e 578 each multibyte character represented as 3 bytes. */
fa9a63c5
RM
579 charset,
580
25fe55af 581 /* Same parameters as charset, but match any character that is
4bb91c68 582 not one of those specified. */
fa9a63c5
RM
583 charset_not,
584
25fe55af
RS
585 /* Start remembering the text that is matched, for storing in a
586 register. Followed by one byte with the register number, in
587 the range 0 to one less than the pattern buffer's re_nsub
505bde11 588 field. */
fa9a63c5
RM
589 start_memory,
590
25fe55af
RS
591 /* Stop remembering the text that is matched and store it in a
592 memory register. Followed by one byte with the register
593 number, in the range 0 to one less than `re_nsub' in the
505bde11 594 pattern buffer. */
fa9a63c5
RM
595 stop_memory,
596
25fe55af 597 /* Match a duplicate of something remembered. Followed by one
4bb91c68 598 byte containing the register number. */
fa9a63c5
RM
599 duplicate,
600
25fe55af 601 /* Fail unless at beginning of line. */
fa9a63c5
RM
602 begline,
603
4bb91c68 604 /* Fail unless at end of line. */
fa9a63c5
RM
605 endline,
606
25fe55af
RS
607 /* Succeeds if at beginning of buffer (if emacs) or at beginning
608 of string to be matched (if not). */
fa9a63c5
RM
609 begbuf,
610
25fe55af 611 /* Analogously, for end of buffer/string. */
fa9a63c5 612 endbuf,
5e69f11e 613
25fe55af 614 /* Followed by two byte relative address to which to jump. */
5e69f11e 615 jump,
fa9a63c5 616
25fe55af 617 /* Followed by two-byte relative address of place to resume at
7814e705 618 in case of failure. */
fa9a63c5 619 on_failure_jump,
5e69f11e 620
25fe55af
RS
621 /* Like on_failure_jump, but pushes a placeholder instead of the
622 current string position when executed. */
fa9a63c5 623 on_failure_keep_string_jump,
5e69f11e 624
505bde11
SM
625 /* Just like `on_failure_jump', except that it checks that we
626 don't get stuck in an infinite loop (matching an empty string
627 indefinitely). */
628 on_failure_jump_loop,
629
0683b6fa
SM
630 /* Just like `on_failure_jump_loop', except that it checks for
631 a different kind of loop (the kind that shows up with non-greedy
632 operators). This operation has to be immediately preceded
633 by a `no_op'. */
634 on_failure_jump_nastyloop,
635
0b32bf0e 636 /* A smart `on_failure_jump' used for greedy * and + operators.
c7015153 637 It analyzes the loop before which it is put and if the
505bde11 638 loop does not require backtracking, it changes itself to
4e8a9132
SM
639 `on_failure_keep_string_jump' and short-circuits the loop,
640 else it just defaults to changing itself into `on_failure_jump'.
641 It assumes that it is pointing to just past a `jump'. */
505bde11 642 on_failure_jump_smart,
fa9a63c5 643
25fe55af 644 /* Followed by two-byte relative address and two-byte number n.
ed0767d8
SM
645 After matching N times, jump to the address upon failure.
646 Does not work if N starts at 0: use on_failure_jump_loop
647 instead. */
fa9a63c5
RM
648 succeed_n,
649
25fe55af
RS
650 /* Followed by two-byte relative address, and two-byte number n.
651 Jump to the address N times, then fail. */
fa9a63c5
RM
652 jump_n,
653
25fe55af 654 /* Set the following two-byte relative address to the
7814e705 655 subsequent two-byte number. The address *includes* the two
25fe55af 656 bytes of number. */
fa9a63c5
RM
657 set_number_at,
658
fa9a63c5
RM
659 wordbeg, /* Succeeds if at word beginning. */
660 wordend, /* Succeeds if at word end. */
661
662 wordbound, /* Succeeds if at a word boundary. */
7814e705 663 notwordbound, /* Succeeds if not at a word boundary. */
fa9a63c5 664
669fa600
SM
665 symbeg, /* Succeeds if at symbol beginning. */
666 symend, /* Succeeds if at symbol end. */
667
fa9a63c5 668 /* Matches any character whose syntax is specified. Followed by
25fe55af 669 a byte which contains a syntax code, e.g., Sword. */
fa9a63c5
RM
670 syntaxspec,
671
672 /* Matches any character whose syntax is not that specified. */
1fb352e0
SM
673 notsyntaxspec
674
675#ifdef emacs
676 ,before_dot, /* Succeeds if before point. */
677 at_dot, /* Succeeds if at point. */
678 after_dot, /* Succeeds if after point. */
b18215fc
RS
679
680 /* Matches any character whose category-set contains the specified
7814e705
JB
681 category. The operator is followed by a byte which contains a
682 category code (mnemonic ASCII character). */
b18215fc
RS
683 categoryspec,
684
685 /* Matches any character whose category-set does not contain the
686 specified category. The operator is followed by a byte which
687 contains the category code (mnemonic ASCII character). */
688 notcategoryspec
fa9a63c5
RM
689#endif /* emacs */
690} re_opcode_t;
691\f
692/* Common operations on the compiled pattern. */
693
694/* Store NUMBER in two contiguous bytes starting at DESTINATION. */
695
696#define STORE_NUMBER(destination, number) \
697 do { \
698 (destination)[0] = (number) & 0377; \
699 (destination)[1] = (number) >> 8; \
700 } while (0)
701
702/* Same as STORE_NUMBER, except increment DESTINATION to
703 the byte after where the number is stored. Therefore, DESTINATION
704 must be an lvalue. */
705
706#define STORE_NUMBER_AND_INCR(destination, number) \
707 do { \
708 STORE_NUMBER (destination, number); \
709 (destination) += 2; \
710 } while (0)
711
712/* Put into DESTINATION a number stored in two contiguous bytes starting
713 at SOURCE. */
714
715#define EXTRACT_NUMBER(destination, source) \
dc4a2ee0 716 ((destination) = extract_number (source))
fa9a63c5 717
dc4a2ee0
PE
718static int
719extract_number (re_char *source)
fa9a63c5 720{
dc4a2ee0 721 return (SIGN_EXTEND_CHAR (source[1]) << 8) + source[0];
fa9a63c5
RM
722}
723
fa9a63c5
RM
724/* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
725 SOURCE must be an lvalue. */
726
727#define EXTRACT_NUMBER_AND_INCR(destination, source) \
dc4a2ee0 728 ((destination) = extract_number_and_incr (&source))
fa9a63c5 729
dc4a2ee0
PE
730static int
731extract_number_and_incr (re_char **source)
5e69f11e 732{
dc4a2ee0 733 int num = extract_number (*source);
fa9a63c5 734 *source += 2;
dc4a2ee0 735 return num;
fa9a63c5 736}
fa9a63c5 737\f
b18215fc
RS
738/* Store a multibyte character in three contiguous bytes starting
739 DESTINATION, and increment DESTINATION to the byte after where the
7814e705 740 character is stored. Therefore, DESTINATION must be an lvalue. */
b18215fc
RS
741
742#define STORE_CHARACTER_AND_INCR(destination, character) \
743 do { \
744 (destination)[0] = (character) & 0377; \
745 (destination)[1] = ((character) >> 8) & 0377; \
746 (destination)[2] = (character) >> 16; \
747 (destination) += 3; \
748 } while (0)
749
750/* Put into DESTINATION a character stored in three contiguous bytes
7814e705 751 starting at SOURCE. */
b18215fc
RS
752
753#define EXTRACT_CHARACTER(destination, source) \
754 do { \
755 (destination) = ((source)[0] \
756 | ((source)[1] << 8) \
757 | ((source)[2] << 16)); \
758 } while (0)
759
760
761/* Macros for charset. */
762
763/* Size of bitmap of charset P in bytes. P is a start of charset,
764 i.e. *P is (re_opcode_t) charset or (re_opcode_t) charset_not. */
765#define CHARSET_BITMAP_SIZE(p) ((p)[1] & 0x7F)
766
767/* Nonzero if charset P has range table. */
25fe55af 768#define CHARSET_RANGE_TABLE_EXISTS_P(p) ((p)[1] & 0x80)
b18215fc
RS
769
770/* Return the address of range table of charset P. But not the start
771 of table itself, but the before where the number of ranges is
96cc36cc
RS
772 stored. `2 +' means to skip re_opcode_t and size of bitmap,
773 and the 2 bytes of flags at the start of the range table. */
774#define CHARSET_RANGE_TABLE(p) (&(p)[4 + CHARSET_BITMAP_SIZE (p)])
775
776/* Extract the bit flags that start a range table. */
777#define CHARSET_RANGE_TABLE_BITS(p) \
778 ((p)[2 + CHARSET_BITMAP_SIZE (p)] \
779 + (p)[3 + CHARSET_BITMAP_SIZE (p)] * 0x100)
b18215fc 780
b18215fc 781/* Return the address of end of RANGE_TABLE. COUNT is number of
7814e705
JB
782 ranges (which is a pair of (start, end)) in the RANGE_TABLE. `* 2'
783 is start of range and end of range. `* 3' is size of each start
b18215fc
RS
784 and end. */
785#define CHARSET_RANGE_TABLE_END(range_table, count) \
786 ((range_table) + (count) * 2 * 3)
787
7814e705 788/* Test if C is in RANGE_TABLE. A flag NOT is negated if C is in.
b18215fc
RS
789 COUNT is number of ranges in RANGE_TABLE. */
790#define CHARSET_LOOKUP_RANGE_TABLE_RAW(not, c, range_table, count) \
791 do \
792 { \
01618498 793 re_wchar_t range_start, range_end; \
19ed5445 794 re_char *rtp; \
01618498 795 re_char *range_table_end \
b18215fc
RS
796 = CHARSET_RANGE_TABLE_END ((range_table), (count)); \
797 \
19ed5445 798 for (rtp = (range_table); rtp < range_table_end; rtp += 2 * 3) \
b18215fc 799 { \
19ed5445
PE
800 EXTRACT_CHARACTER (range_start, rtp); \
801 EXTRACT_CHARACTER (range_end, rtp + 3); \
b18215fc
RS
802 \
803 if (range_start <= (c) && (c) <= range_end) \
804 { \
805 (not) = !(not); \
806 break; \
807 } \
808 } \
809 } \
810 while (0)
811
812/* Test if C is in range table of CHARSET. The flag NOT is negated if
813 C is listed in it. */
814#define CHARSET_LOOKUP_RANGE_TABLE(not, c, charset) \
815 do \
816 { \
817 /* Number of ranges in range table. */ \
818 int count; \
01618498
SM
819 re_char *range_table = CHARSET_RANGE_TABLE (charset); \
820 \
b18215fc
RS
821 EXTRACT_NUMBER_AND_INCR (count, range_table); \
822 CHARSET_LOOKUP_RANGE_TABLE_RAW ((not), (c), range_table, count); \
823 } \
824 while (0)
825\f
fa9a63c5
RM
826/* If DEBUG is defined, Regex prints many voluminous messages about what
827 it is doing (if the variable `debug' is nonzero). If linked with the
828 main program in `iregex.c', you can enter patterns and strings
829 interactively. And if linked with the main program in `main.c' and
4bb91c68 830 the other test files, you can run the already-written tests. */
fa9a63c5
RM
831
832#ifdef DEBUG
833
834/* We use standard I/O for debugging. */
0b32bf0e 835# include <stdio.h>
fa9a63c5
RM
836
837/* It is useful to test things that ``must'' be true when debugging. */
0b32bf0e 838# include <assert.h>
fa9a63c5 839
99633e97 840static int debug = -100000;
fa9a63c5 841
0b32bf0e 842# define DEBUG_STATEMENT(e) e
dc4a2ee0
PE
843# define DEBUG_PRINT(...) if (debug > 0) printf (__VA_ARGS__)
844# define DEBUG_COMPILES_ARGUMENTS
0b32bf0e 845# define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) \
99633e97 846 if (debug > 0) print_partial_compiled_pattern (s, e)
0b32bf0e 847# define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \
99633e97 848 if (debug > 0) print_double_string (w, s1, sz1, s2, sz2)
fa9a63c5
RM
849
850
851/* Print the fastmap in human-readable form. */
852
dc4a2ee0
PE
853static void
854print_fastmap (char *fastmap)
fa9a63c5
RM
855{
856 unsigned was_a_range = 0;
5e69f11e
RM
857 unsigned i = 0;
858
fa9a63c5
RM
859 while (i < (1 << BYTEWIDTH))
860 {
861 if (fastmap[i++])
862 {
863 was_a_range = 0;
25fe55af
RS
864 putchar (i - 1);
865 while (i < (1 << BYTEWIDTH) && fastmap[i])
866 {
867 was_a_range = 1;
868 i++;
869 }
fa9a63c5 870 if (was_a_range)
25fe55af
RS
871 {
872 printf ("-");
873 putchar (i - 1);
874 }
875 }
fa9a63c5 876 }
5e69f11e 877 putchar ('\n');
fa9a63c5
RM
878}
879
880
881/* Print a compiled pattern string in human-readable form, starting at
882 the START pointer into it and ending just before the pointer END. */
883
dc4a2ee0
PE
884static void
885print_partial_compiled_pattern (re_char *start, re_char *end)
fa9a63c5
RM
886{
887 int mcnt, mcnt2;
01618498
SM
888 re_char *p = start;
889 re_char *pend = end;
fa9a63c5
RM
890
891 if (start == NULL)
892 {
a1a052df 893 fprintf (stderr, "(null)\n");
fa9a63c5
RM
894 return;
895 }
5e69f11e 896
fa9a63c5
RM
897 /* Loop over pattern commands. */
898 while (p < pend)
899 {
dc4a2ee0 900 fprintf (stderr, "%td:\t", p - start);
fa9a63c5
RM
901
902 switch ((re_opcode_t) *p++)
903 {
25fe55af 904 case no_op:
a1a052df 905 fprintf (stderr, "/no_op");
25fe55af 906 break;
fa9a63c5 907
99633e97 908 case succeed:
a1a052df 909 fprintf (stderr, "/succeed");
99633e97
SM
910 break;
911
fa9a63c5
RM
912 case exactn:
913 mcnt = *p++;
a1a052df 914 fprintf (stderr, "/exactn/%d", mcnt);
25fe55af 915 do
fa9a63c5 916 {
a1a052df 917 fprintf (stderr, "/%c", *p++);
25fe55af
RS
918 }
919 while (--mcnt);
920 break;
fa9a63c5
RM
921
922 case start_memory:
a1a052df 923 fprintf (stderr, "/start_memory/%d", *p++);
25fe55af 924 break;
fa9a63c5
RM
925
926 case stop_memory:
a1a052df 927 fprintf (stderr, "/stop_memory/%d", *p++);
25fe55af 928 break;
fa9a63c5
RM
929
930 case duplicate:
a1a052df 931 fprintf (stderr, "/duplicate/%d", *p++);
fa9a63c5
RM
932 break;
933
934 case anychar:
a1a052df 935 fprintf (stderr, "/anychar");
fa9a63c5
RM
936 break;
937
938 case charset:
25fe55af
RS
939 case charset_not:
940 {
941 register int c, last = -100;
fa9a63c5 942 register int in_range = 0;
99633e97
SM
943 int length = CHARSET_BITMAP_SIZE (p - 1);
944 int has_range_table = CHARSET_RANGE_TABLE_EXISTS_P (p - 1);
fa9a63c5 945
a1a052df 946 fprintf (stderr, "/charset [%s",
839966f3 947 (re_opcode_t) *(p - 1) == charset_not ? "^" : "");
5e69f11e 948
839966f3
KH
949 if (p + *p >= pend)
950 fprintf (stderr, " !extends past end of pattern! ");
fa9a63c5 951
25fe55af 952 for (c = 0; c < 256; c++)
96cc36cc 953 if (c / 8 < length
fa9a63c5
RM
954 && (p[1 + (c/8)] & (1 << (c % 8))))
955 {
956 /* Are we starting a range? */
957 if (last + 1 == c && ! in_range)
958 {
a1a052df 959 fprintf (stderr, "-");
fa9a63c5
RM
960 in_range = 1;
961 }
962 /* Have we broken a range? */
963 else if (last + 1 != c && in_range)
96cc36cc 964 {
a1a052df 965 fprintf (stderr, "%c", last);
fa9a63c5
RM
966 in_range = 0;
967 }
5e69f11e 968
fa9a63c5 969 if (! in_range)
a1a052df 970 fprintf (stderr, "%c", c);
fa9a63c5
RM
971
972 last = c;
25fe55af 973 }
fa9a63c5
RM
974
975 if (in_range)
a1a052df 976 fprintf (stderr, "%c", last);
fa9a63c5 977
a1a052df 978 fprintf (stderr, "]");
fa9a63c5 979
99633e97 980 p += 1 + length;
96cc36cc 981
96cc36cc 982 if (has_range_table)
99633e97
SM
983 {
984 int count;
a1a052df 985 fprintf (stderr, "has-range-table");
99633e97
SM
986
987 /* ??? Should print the range table; for now, just skip it. */
988 p += 2; /* skip range table bits */
989 EXTRACT_NUMBER_AND_INCR (count, p);
990 p = CHARSET_RANGE_TABLE_END (p, count);
991 }
fa9a63c5
RM
992 }
993 break;
994
995 case begline:
a1a052df 996 fprintf (stderr, "/begline");
25fe55af 997 break;
fa9a63c5
RM
998
999 case endline:
a1a052df 1000 fprintf (stderr, "/endline");
25fe55af 1001 break;
fa9a63c5
RM
1002
1003 case on_failure_jump:
dc4a2ee0
PE
1004 EXTRACT_NUMBER_AND_INCR (mcnt, p);
1005 fprintf (stderr, "/on_failure_jump to %td", p + mcnt - start);
25fe55af 1006 break;
fa9a63c5
RM
1007
1008 case on_failure_keep_string_jump:
dc4a2ee0
PE
1009 EXTRACT_NUMBER_AND_INCR (mcnt, p);
1010 fprintf (stderr, "/on_failure_keep_string_jump to %td",
1011 p + mcnt - start);
25fe55af 1012 break;
fa9a63c5 1013
0683b6fa 1014 case on_failure_jump_nastyloop:
dc4a2ee0
PE
1015 EXTRACT_NUMBER_AND_INCR (mcnt, p);
1016 fprintf (stderr, "/on_failure_jump_nastyloop to %td",
1017 p + mcnt - start);
0683b6fa
SM
1018 break;
1019
505bde11 1020 case on_failure_jump_loop:
dc4a2ee0
PE
1021 EXTRACT_NUMBER_AND_INCR (mcnt, p);
1022 fprintf (stderr, "/on_failure_jump_loop to %td",
1023 p + mcnt - start);
5e69f11e
RM
1024 break;
1025
505bde11 1026 case on_failure_jump_smart:
dc4a2ee0
PE
1027 EXTRACT_NUMBER_AND_INCR (mcnt, p);
1028 fprintf (stderr, "/on_failure_jump_smart to %td",
1029 p + mcnt - start);
5e69f11e
RM
1030 break;
1031
25fe55af 1032 case jump:
dc4a2ee0
PE
1033 EXTRACT_NUMBER_AND_INCR (mcnt, p);
1034 fprintf (stderr, "/jump to %td", p + mcnt - start);
fa9a63c5
RM
1035 break;
1036
25fe55af 1037 case succeed_n:
dc4a2ee0
PE
1038 EXTRACT_NUMBER_AND_INCR (mcnt, p);
1039 EXTRACT_NUMBER_AND_INCR (mcnt2, p);
1040 fprintf (stderr, "/succeed_n to %td, %d times",
1041 p - 2 + mcnt - start, mcnt2);
25fe55af 1042 break;
5e69f11e 1043
25fe55af 1044 case jump_n:
dc4a2ee0
PE
1045 EXTRACT_NUMBER_AND_INCR (mcnt, p);
1046 EXTRACT_NUMBER_AND_INCR (mcnt2, p);
1047 fprintf (stderr, "/jump_n to %td, %d times",
1048 p - 2 + mcnt - start, mcnt2);
25fe55af 1049 break;
5e69f11e 1050
25fe55af 1051 case set_number_at:
dc4a2ee0
PE
1052 EXTRACT_NUMBER_AND_INCR (mcnt, p);
1053 EXTRACT_NUMBER_AND_INCR (mcnt2, p);
1054 fprintf (stderr, "/set_number_at location %td to %d",
1055 p - 2 + mcnt - start, mcnt2);
25fe55af 1056 break;
5e69f11e 1057
25fe55af 1058 case wordbound:
a1a052df 1059 fprintf (stderr, "/wordbound");
fa9a63c5
RM
1060 break;
1061
1062 case notwordbound:
a1a052df 1063 fprintf (stderr, "/notwordbound");
25fe55af 1064 break;
fa9a63c5
RM
1065
1066 case wordbeg:
a1a052df 1067 fprintf (stderr, "/wordbeg");
fa9a63c5 1068 break;
5e69f11e 1069
fa9a63c5 1070 case wordend:
a1a052df 1071 fprintf (stderr, "/wordend");
e2543b02 1072 break;
5e69f11e 1073
669fa600 1074 case symbeg:
e2543b02 1075 fprintf (stderr, "/symbeg");
669fa600
SM
1076 break;
1077
1078 case symend:
e2543b02 1079 fprintf (stderr, "/symend");
669fa600 1080 break;
5e69f11e 1081
1fb352e0 1082 case syntaxspec:
a1a052df 1083 fprintf (stderr, "/syntaxspec");
1fb352e0 1084 mcnt = *p++;
a1a052df 1085 fprintf (stderr, "/%d", mcnt);
1fb352e0
SM
1086 break;
1087
1088 case notsyntaxspec:
a1a052df 1089 fprintf (stderr, "/notsyntaxspec");
1fb352e0 1090 mcnt = *p++;
a1a052df 1091 fprintf (stderr, "/%d", mcnt);
1fb352e0
SM
1092 break;
1093
0b32bf0e 1094# ifdef emacs
fa9a63c5 1095 case before_dot:
a1a052df 1096 fprintf (stderr, "/before_dot");
25fe55af 1097 break;
fa9a63c5
RM
1098
1099 case at_dot:
a1a052df 1100 fprintf (stderr, "/at_dot");
25fe55af 1101 break;
fa9a63c5
RM
1102
1103 case after_dot:
a1a052df 1104 fprintf (stderr, "/after_dot");
25fe55af 1105 break;
fa9a63c5 1106
1fb352e0 1107 case categoryspec:
a1a052df 1108 fprintf (stderr, "/categoryspec");
fa9a63c5 1109 mcnt = *p++;
a1a052df 1110 fprintf (stderr, "/%d", mcnt);
25fe55af 1111 break;
5e69f11e 1112
1fb352e0 1113 case notcategoryspec:
a1a052df 1114 fprintf (stderr, "/notcategoryspec");
fa9a63c5 1115 mcnt = *p++;
a1a052df 1116 fprintf (stderr, "/%d", mcnt);
fa9a63c5 1117 break;
0b32bf0e 1118# endif /* emacs */
fa9a63c5 1119
fa9a63c5 1120 case begbuf:
a1a052df 1121 fprintf (stderr, "/begbuf");
25fe55af 1122 break;
fa9a63c5
RM
1123
1124 case endbuf:
a1a052df 1125 fprintf (stderr, "/endbuf");
25fe55af 1126 break;
fa9a63c5 1127
25fe55af 1128 default:
a1a052df 1129 fprintf (stderr, "?%d", *(p-1));
fa9a63c5
RM
1130 }
1131
a1a052df 1132 fprintf (stderr, "\n");
fa9a63c5
RM
1133 }
1134
dc4a2ee0 1135 fprintf (stderr, "%td:\tend of pattern.\n", p - start);
fa9a63c5
RM
1136}
1137
1138
dc4a2ee0
PE
1139static void
1140print_compiled_pattern (struct re_pattern_buffer *bufp)
fa9a63c5 1141{
01618498 1142 re_char *buffer = bufp->buffer;
fa9a63c5
RM
1143
1144 print_partial_compiled_pattern (buffer, buffer + bufp->used);
4bb91c68
SM
1145 printf ("%ld bytes used/%ld bytes allocated.\n",
1146 bufp->used, bufp->allocated);
fa9a63c5
RM
1147
1148 if (bufp->fastmap_accurate && bufp->fastmap)
1149 {
1150 printf ("fastmap: ");
1151 print_fastmap (bufp->fastmap);
1152 }
1153
dc4a2ee0 1154 printf ("re_nsub: %zu\t", bufp->re_nsub);
fa9a63c5
RM
1155 printf ("regs_alloc: %d\t", bufp->regs_allocated);
1156 printf ("can_be_null: %d\t", bufp->can_be_null);
fa9a63c5
RM
1157 printf ("no_sub: %d\t", bufp->no_sub);
1158 printf ("not_bol: %d\t", bufp->not_bol);
1159 printf ("not_eol: %d\t", bufp->not_eol);
4bb91c68 1160 printf ("syntax: %lx\n", bufp->syntax);
505bde11 1161 fflush (stdout);
fa9a63c5
RM
1162 /* Perhaps we should print the translate table? */
1163}
1164
1165
dc4a2ee0
PE
1166static void
1167print_double_string (re_char *where, re_char *string1, ssize_t size1,
1168 re_char *string2, ssize_t size2)
fa9a63c5 1169{
d1dfb56c 1170 ssize_t this_char;
5e69f11e 1171
fa9a63c5
RM
1172 if (where == NULL)
1173 printf ("(null)");
1174 else
1175 {
1176 if (FIRST_STRING_P (where))
25fe55af
RS
1177 {
1178 for (this_char = where - string1; this_char < size1; this_char++)
1179 putchar (string1[this_char]);
fa9a63c5 1180
25fe55af
RS
1181 where = string2;
1182 }
fa9a63c5
RM
1183
1184 for (this_char = where - string2; this_char < size2; this_char++)
25fe55af 1185 putchar (string2[this_char]);
fa9a63c5
RM
1186 }
1187}
1188
1189#else /* not DEBUG */
1190
0b32bf0e
SM
1191# undef assert
1192# define assert(e)
fa9a63c5 1193
0b32bf0e 1194# define DEBUG_STATEMENT(e)
dc4a2ee0
PE
1195# if __STDC_VERSION__ < 199901L
1196# define DEBUG_COMPILES_ARGUMENTS
1197# define DEBUG_PRINT /* 'DEBUG_PRINT (x, y)' discards X and Y. */ (void)
1198# else
1199# define DEBUG_PRINT(...)
1200# endif
0b32bf0e
SM
1201# define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
1202# define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
fa9a63c5
RM
1203
1204#endif /* not DEBUG */
1205\f
4da60324
PE
1206/* Use this to suppress gcc's `...may be used before initialized' warnings. */
1207#ifdef lint
1208# define IF_LINT(Code) Code
1209#else
1210# define IF_LINT(Code) /* empty */
1211#endif
1212\f
fa9a63c5
RM
1213/* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
1214 also be assigned to arbitrarily: each pattern buffer stores its own
1215 syntax, so it can be changed between regex compilations. */
1216/* This has no initializer because initialized variables in Emacs
1217 become read-only after dumping. */
1218reg_syntax_t re_syntax_options;
1219
1220
1221/* Specify the precise syntax of regexps for compilation. This provides
1222 for compatibility for various utilities which historically have
1223 different, incompatible syntaxes.
1224
1225 The argument SYNTAX is a bit mask comprised of the various bits
4bb91c68 1226 defined in regex.h. We return the old syntax. */
fa9a63c5
RM
1227
1228reg_syntax_t
971de7fb 1229re_set_syntax (reg_syntax_t syntax)
fa9a63c5
RM
1230{
1231 reg_syntax_t ret = re_syntax_options;
5e69f11e 1232
fa9a63c5
RM
1233 re_syntax_options = syntax;
1234 return ret;
1235}
c0f9ea08 1236WEAK_ALIAS (__re_set_syntax, re_set_syntax)
f9b0fd99
RS
1237
1238/* Regexp to use to replace spaces, or NULL meaning don't. */
1239static re_char *whitespace_regexp;
1240
1241void
971de7fb 1242re_set_whitespace_regexp (const char *regexp)
f9b0fd99 1243{
6470ea05 1244 whitespace_regexp = (re_char *) regexp;
f9b0fd99
RS
1245}
1246WEAK_ALIAS (__re_set_syntax, re_set_syntax)
fa9a63c5
RM
1247\f
1248/* This table gives an error message for each of the error codes listed
4bb91c68 1249 in regex.h. Obviously the order here has to be same as there.
fa9a63c5 1250 POSIX doesn't require that we do anything for REG_NOERROR,
4bb91c68 1251 but why not be nice? */
fa9a63c5
RM
1252
1253static const char *re_error_msgid[] =
5e69f11e
RM
1254 {
1255 gettext_noop ("Success"), /* REG_NOERROR */
1256 gettext_noop ("No match"), /* REG_NOMATCH */
1257 gettext_noop ("Invalid regular expression"), /* REG_BADPAT */
1258 gettext_noop ("Invalid collation character"), /* REG_ECOLLATE */
1259 gettext_noop ("Invalid character class name"), /* REG_ECTYPE */
1260 gettext_noop ("Trailing backslash"), /* REG_EESCAPE */
1261 gettext_noop ("Invalid back reference"), /* REG_ESUBREG */
1262 gettext_noop ("Unmatched [ or [^"), /* REG_EBRACK */
1263 gettext_noop ("Unmatched ( or \\("), /* REG_EPAREN */
1264 gettext_noop ("Unmatched \\{"), /* REG_EBRACE */
1265 gettext_noop ("Invalid content of \\{\\}"), /* REG_BADBR */
1266 gettext_noop ("Invalid range end"), /* REG_ERANGE */
1267 gettext_noop ("Memory exhausted"), /* REG_ESPACE */
1268 gettext_noop ("Invalid preceding regular expression"), /* REG_BADRPT */
1269 gettext_noop ("Premature end of regular expression"), /* REG_EEND */
1270 gettext_noop ("Regular expression too big"), /* REG_ESIZE */
1271 gettext_noop ("Unmatched ) or \\)"), /* REG_ERPAREN */
b3e4c897 1272 gettext_noop ("Range striding over charsets") /* REG_ERANGEX */
fa9a63c5
RM
1273 };
1274\f
4bb91c68 1275/* Avoiding alloca during matching, to placate r_alloc. */
fa9a63c5
RM
1276
1277/* Define MATCH_MAY_ALLOCATE unless we need to make sure that the
1278 searching and matching functions should not call alloca. On some
1279 systems, alloca is implemented in terms of malloc, and if we're
1280 using the relocating allocator routines, then malloc could cause a
1281 relocation, which might (if the strings being searched are in the
1282 ralloc heap) shift the data out from underneath the regexp
1283 routines.
1284
5e69f11e 1285 Here's another reason to avoid allocation: Emacs
fa9a63c5
RM
1286 processes input from X in a signal handler; processing X input may
1287 call malloc; if input arrives while a matching routine is calling
1288 malloc, then we're scrod. But Emacs can't just block input while
1289 calling matching routines; then we don't notice interrupts when
1290 they come in. So, Emacs blocks input around all regexp calls
1291 except the matching calls, which it leaves unprotected, in the
1292 faith that they will not malloc. */
1293
1294/* Normally, this is fine. */
1295#define MATCH_MAY_ALLOCATE
1296
fa9a63c5
RM
1297/* The match routines may not allocate if (1) they would do it with malloc
1298 and (2) it's not safe for them to use malloc.
1299 Note that if REL_ALLOC is defined, matching would not use malloc for the
1300 failure stack, but we would still use it for the register vectors;
4bb91c68 1301 so REL_ALLOC should not affect this. */
b588157e 1302#if defined REGEX_MALLOC && defined emacs
0b32bf0e 1303# undef MATCH_MAY_ALLOCATE
fa9a63c5
RM
1304#endif
1305
1306\f
1307/* Failure stack declarations and macros; both re_compile_fastmap and
1308 re_match_2 use a failure stack. These have to be macros because of
1309 REGEX_ALLOCATE_STACK. */
5e69f11e 1310
fa9a63c5 1311
320a2a73 1312/* Approximate number of failure points for which to initially allocate space
fa9a63c5
RM
1313 when matching. If this number is exceeded, we allocate more
1314 space, so it is not a hard limit. */
1315#ifndef INIT_FAILURE_ALLOC
0b32bf0e 1316# define INIT_FAILURE_ALLOC 20
fa9a63c5
RM
1317#endif
1318
1319/* Roughly the maximum number of failure points on the stack. Would be
320a2a73 1320 exactly that if always used TYPICAL_FAILURE_SIZE items each time we failed.
fa9a63c5 1321 This is a variable only so users of regex can assign to it; we never
ada30c0e
SM
1322 change it ourselves. We always multiply it by TYPICAL_FAILURE_SIZE
1323 before using it, so it should probably be a byte-count instead. */
c0f9ea08
SM
1324# if defined MATCH_MAY_ALLOCATE
1325/* Note that 4400 was enough to cause a crash on Alpha OSF/1,
320a2a73
KH
1326 whose default stack limit is 2mb. In order for a larger
1327 value to work reliably, you have to try to make it accord
1328 with the process stack limit. */
c0f9ea08
SM
1329size_t re_max_failures = 40000;
1330# else
1331size_t re_max_failures = 4000;
1332# endif
fa9a63c5
RM
1333
1334union fail_stack_elt
1335{
01618498 1336 re_char *pointer;
c0f9ea08
SM
1337 /* This should be the biggest `int' that's no bigger than a pointer. */
1338 long integer;
fa9a63c5
RM
1339};
1340
1341typedef union fail_stack_elt fail_stack_elt_t;
1342
1343typedef struct
1344{
1345 fail_stack_elt_t *stack;
c0f9ea08
SM
1346 size_t size;
1347 size_t avail; /* Offset of next open position. */
1348 size_t frame; /* Offset of the cur constructed frame. */
fa9a63c5
RM
1349} fail_stack_type;
1350
505bde11 1351#define FAIL_STACK_EMPTY() (fail_stack.frame == 0)
fa9a63c5
RM
1352
1353
1354/* Define macros to initialize and free the failure stack.
1355 Do `return -2' if the alloc fails. */
1356
1357#ifdef MATCH_MAY_ALLOCATE
0b32bf0e 1358# define INIT_FAIL_STACK() \
fa9a63c5 1359 do { \
38182d90 1360 fail_stack.stack = \
320a2a73
KH
1361 REGEX_ALLOCATE_STACK (INIT_FAILURE_ALLOC * TYPICAL_FAILURE_SIZE \
1362 * sizeof (fail_stack_elt_t)); \
fa9a63c5
RM
1363 \
1364 if (fail_stack.stack == NULL) \
1365 return -2; \
1366 \
1367 fail_stack.size = INIT_FAILURE_ALLOC; \
1368 fail_stack.avail = 0; \
505bde11 1369 fail_stack.frame = 0; \
fa9a63c5 1370 } while (0)
fa9a63c5 1371#else
0b32bf0e 1372# define INIT_FAIL_STACK() \
fa9a63c5
RM
1373 do { \
1374 fail_stack.avail = 0; \
505bde11 1375 fail_stack.frame = 0; \
fa9a63c5
RM
1376 } while (0)
1377
b313f9d8
PE
1378# define RETALLOC_IF(addr, n, t) \
1379 if (addr) RETALLOC((addr), (n), t); else (addr) = TALLOC ((n), t)
fa9a63c5
RM
1380#endif
1381
1382
320a2a73
KH
1383/* Double the size of FAIL_STACK, up to a limit
1384 which allows approximately `re_max_failures' items.
fa9a63c5
RM
1385
1386 Return 1 if succeeds, and 0 if either ran out of memory
5e69f11e
RM
1387 allocating space for it or it was already too large.
1388
4bb91c68 1389 REGEX_REALLOCATE_STACK requires `destination' be declared. */
fa9a63c5 1390
320a2a73
KH
1391/* Factor to increase the failure stack size by
1392 when we increase it.
1393 This used to be 2, but 2 was too wasteful
1394 because the old discarded stacks added up to as much space
1395 were as ultimate, maximum-size stack. */
1396#define FAIL_STACK_GROWTH_FACTOR 4
1397
1398#define GROW_FAIL_STACK(fail_stack) \
eead07d6
KH
1399 (((fail_stack).size * sizeof (fail_stack_elt_t) \
1400 >= re_max_failures * TYPICAL_FAILURE_SIZE) \
fa9a63c5 1401 ? 0 \
320a2a73 1402 : ((fail_stack).stack \
38182d90 1403 = REGEX_REALLOCATE_STACK ((fail_stack).stack, \
25fe55af 1404 (fail_stack).size * sizeof (fail_stack_elt_t), \
320a2a73
KH
1405 MIN (re_max_failures * TYPICAL_FAILURE_SIZE, \
1406 ((fail_stack).size * sizeof (fail_stack_elt_t) \
1407 * FAIL_STACK_GROWTH_FACTOR))), \
fa9a63c5
RM
1408 \
1409 (fail_stack).stack == NULL \
1410 ? 0 \
6453db45
KH
1411 : ((fail_stack).size \
1412 = (MIN (re_max_failures * TYPICAL_FAILURE_SIZE, \
1413 ((fail_stack).size * sizeof (fail_stack_elt_t) \
1414 * FAIL_STACK_GROWTH_FACTOR)) \
1415 / sizeof (fail_stack_elt_t)), \
25fe55af 1416 1)))
fa9a63c5
RM
1417
1418
fa9a63c5
RM
1419/* Push a pointer value onto the failure stack.
1420 Assumes the variable `fail_stack'. Probably should only
4bb91c68 1421 be called from within `PUSH_FAILURE_POINT'. */
fa9a63c5 1422#define PUSH_FAILURE_POINTER(item) \
01618498 1423 fail_stack.stack[fail_stack.avail++].pointer = (item)
fa9a63c5
RM
1424
1425/* This pushes an integer-valued item onto the failure stack.
1426 Assumes the variable `fail_stack'. Probably should only
4bb91c68 1427 be called from within `PUSH_FAILURE_POINT'. */
fa9a63c5
RM
1428#define PUSH_FAILURE_INT(item) \
1429 fail_stack.stack[fail_stack.avail++].integer = (item)
1430
b313f9d8 1431/* These POP... operations complement the PUSH... operations.
fa9a63c5
RM
1432 All assume that `fail_stack' is nonempty. */
1433#define POP_FAILURE_POINTER() fail_stack.stack[--fail_stack.avail].pointer
1434#define POP_FAILURE_INT() fail_stack.stack[--fail_stack.avail].integer
fa9a63c5 1435
505bde11
SM
1436/* Individual items aside from the registers. */
1437#define NUM_NONREG_ITEMS 3
1438
1439/* Used to examine the stack (to detect infinite loops). */
1440#define FAILURE_PAT(h) fail_stack.stack[(h) - 1].pointer
66f0296e 1441#define FAILURE_STR(h) (fail_stack.stack[(h) - 2].pointer)
505bde11
SM
1442#define NEXT_FAILURE_HANDLE(h) fail_stack.stack[(h) - 3].integer
1443#define TOP_FAILURE_HANDLE() fail_stack.frame
fa9a63c5
RM
1444
1445
505bde11
SM
1446#define ENSURE_FAIL_STACK(space) \
1447while (REMAINING_AVAIL_SLOTS <= space) { \
1448 if (!GROW_FAIL_STACK (fail_stack)) \
1449 return -2; \
dc4a2ee0
PE
1450 DEBUG_PRINT ("\n Doubled stack; size now: %zd\n", (fail_stack).size);\
1451 DEBUG_PRINT (" slots available: %zd\n", REMAINING_AVAIL_SLOTS);\
505bde11
SM
1452}
1453
1454/* Push register NUM onto the stack. */
1455#define PUSH_FAILURE_REG(num) \
1456do { \
1457 char *destination; \
dc4a2ee0 1458 long n = num; \
505bde11 1459 ENSURE_FAIL_STACK(3); \
dc4a2ee0
PE
1460 DEBUG_PRINT (" Push reg %ld (spanning %p -> %p)\n", \
1461 n, regstart[n], regend[n]); \
1462 PUSH_FAILURE_POINTER (regstart[n]); \
1463 PUSH_FAILURE_POINTER (regend[n]); \
1464 PUSH_FAILURE_INT (n); \
505bde11
SM
1465} while (0)
1466
01618498
SM
1467/* Change the counter's value to VAL, but make sure that it will
1468 be reset when backtracking. */
1469#define PUSH_NUMBER(ptr,val) \
dc1e502d
SM
1470do { \
1471 char *destination; \
1472 int c; \
1473 ENSURE_FAIL_STACK(3); \
1474 EXTRACT_NUMBER (c, ptr); \
dc4a2ee0 1475 DEBUG_PRINT (" Push number %p = %d -> %d\n", ptr, c, val); \
dc1e502d
SM
1476 PUSH_FAILURE_INT (c); \
1477 PUSH_FAILURE_POINTER (ptr); \
1478 PUSH_FAILURE_INT (-1); \
01618498 1479 STORE_NUMBER (ptr, val); \
dc1e502d
SM
1480} while (0)
1481
505bde11 1482/* Pop a saved register off the stack. */
dc1e502d 1483#define POP_FAILURE_REG_OR_COUNT() \
505bde11 1484do { \
d1dfb56c 1485 long pfreg = POP_FAILURE_INT (); \
19ed5445 1486 if (pfreg == -1) \
dc1e502d
SM
1487 { \
1488 /* It's a counter. */ \
6dcf2d0e
SM
1489 /* Here, we discard `const', making re_match non-reentrant. */ \
1490 unsigned char *ptr = (unsigned char*) POP_FAILURE_POINTER (); \
19ed5445
PE
1491 pfreg = POP_FAILURE_INT (); \
1492 STORE_NUMBER (ptr, pfreg); \
dc4a2ee0 1493 DEBUG_PRINT (" Pop counter %p = %ld\n", ptr, pfreg); \
dc1e502d
SM
1494 } \
1495 else \
1496 { \
19ed5445
PE
1497 regend[pfreg] = POP_FAILURE_POINTER (); \
1498 regstart[pfreg] = POP_FAILURE_POINTER (); \
dc4a2ee0
PE
1499 DEBUG_PRINT (" Pop reg %ld (spanning %p -> %p)\n", \
1500 pfreg, regstart[pfreg], regend[pfreg]); \
dc1e502d 1501 } \
505bde11
SM
1502} while (0)
1503
1504/* Check that we are not stuck in an infinite loop. */
1505#define CHECK_INFINITE_LOOP(pat_cur, string_place) \
1506do { \
d1dfb56c 1507 ssize_t failure = TOP_FAILURE_HANDLE (); \
505bde11 1508 /* Check for infinite matching loops */ \
f6df485f
RS
1509 while (failure > 0 \
1510 && (FAILURE_STR (failure) == string_place \
1511 || FAILURE_STR (failure) == NULL)) \
505bde11
SM
1512 { \
1513 assert (FAILURE_PAT (failure) >= bufp->buffer \
66f0296e 1514 && FAILURE_PAT (failure) <= bufp->buffer + bufp->used); \
505bde11 1515 if (FAILURE_PAT (failure) == pat_cur) \
f6df485f 1516 { \
6df42991
SM
1517 cycle = 1; \
1518 break; \
f6df485f 1519 } \
dc4a2ee0 1520 DEBUG_PRINT (" Other pattern: %p\n", FAILURE_PAT (failure)); \
505bde11
SM
1521 failure = NEXT_FAILURE_HANDLE(failure); \
1522 } \
dc4a2ee0 1523 DEBUG_PRINT (" Other string: %p\n", FAILURE_STR (failure)); \
505bde11 1524} while (0)
6df42991 1525
fa9a63c5 1526/* Push the information about the state we will need
5e69f11e
RM
1527 if we ever fail back to it.
1528
505bde11 1529 Requires variables fail_stack, regstart, regend and
320a2a73 1530 num_regs be declared. GROW_FAIL_STACK requires `destination' be
fa9a63c5 1531 declared.
5e69f11e 1532
fa9a63c5
RM
1533 Does `return FAILURE_CODE' if runs out of memory. */
1534
505bde11
SM
1535#define PUSH_FAILURE_POINT(pattern, string_place) \
1536do { \
1537 char *destination; \
1538 /* Must be int, so when we don't save any registers, the arithmetic \
1539 of 0 + -1 isn't done as unsigned. */ \
1540 \
505bde11 1541 DEBUG_STATEMENT (nfailure_points_pushed++); \
dc4a2ee0
PE
1542 DEBUG_PRINT ("\nPUSH_FAILURE_POINT:\n"); \
1543 DEBUG_PRINT (" Before push, next avail: %zd\n", (fail_stack).avail); \
1544 DEBUG_PRINT (" size: %zd\n", (fail_stack).size);\
505bde11
SM
1545 \
1546 ENSURE_FAIL_STACK (NUM_NONREG_ITEMS); \
1547 \
dc4a2ee0 1548 DEBUG_PRINT ("\n"); \
505bde11 1549 \
dc4a2ee0 1550 DEBUG_PRINT (" Push frame index: %zd\n", fail_stack.frame); \
505bde11
SM
1551 PUSH_FAILURE_INT (fail_stack.frame); \
1552 \
dc4a2ee0 1553 DEBUG_PRINT (" Push string %p: `", string_place); \
505bde11 1554 DEBUG_PRINT_DOUBLE_STRING (string_place, string1, size1, string2, size2);\
dc4a2ee0 1555 DEBUG_PRINT ("'\n"); \
505bde11
SM
1556 PUSH_FAILURE_POINTER (string_place); \
1557 \
dc4a2ee0 1558 DEBUG_PRINT (" Push pattern %p: ", pattern); \
505bde11
SM
1559 DEBUG_PRINT_COMPILED_PATTERN (bufp, pattern, pend); \
1560 PUSH_FAILURE_POINTER (pattern); \
1561 \
1562 /* Close the frame by moving the frame pointer past it. */ \
1563 fail_stack.frame = fail_stack.avail; \
1564} while (0)
fa9a63c5 1565
320a2a73
KH
1566/* Estimate the size of data pushed by a typical failure stack entry.
1567 An estimate is all we need, because all we use this for
1568 is to choose a limit for how big to make the failure stack. */
ada30c0e 1569/* BEWARE, the value `20' is hard-coded in emacs.c:main(). */
320a2a73 1570#define TYPICAL_FAILURE_SIZE 20
fa9a63c5 1571
fa9a63c5
RM
1572/* How many items can still be added to the stack without overflowing it. */
1573#define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail)
1574
1575
1576/* Pops what PUSH_FAIL_STACK pushes.
1577
1578 We restore into the parameters, all of which should be lvalues:
1579 STR -- the saved data position.
1580 PAT -- the saved pattern position.
fa9a63c5 1581 REGSTART, REGEND -- arrays of string positions.
5e69f11e 1582
fa9a63c5 1583 Also assumes the variables `fail_stack' and (if debugging), `bufp',
7814e705 1584 `pend', `string1', `size1', `string2', and `size2'. */
fa9a63c5 1585
505bde11
SM
1586#define POP_FAILURE_POINT(str, pat) \
1587do { \
fa9a63c5
RM
1588 assert (!FAIL_STACK_EMPTY ()); \
1589 \
1590 /* Remove failure points and point to how many regs pushed. */ \
dc4a2ee0
PE
1591 DEBUG_PRINT ("POP_FAILURE_POINT:\n"); \
1592 DEBUG_PRINT (" Before pop, next avail: %zd\n", fail_stack.avail); \
1593 DEBUG_PRINT (" size: %zd\n", fail_stack.size); \
fa9a63c5 1594 \
505bde11
SM
1595 /* Pop the saved registers. */ \
1596 while (fail_stack.frame < fail_stack.avail) \
dc1e502d 1597 POP_FAILURE_REG_OR_COUNT (); \
fa9a63c5 1598 \
dc4a2ee0
PE
1599 pat = POP_FAILURE_POINTER (); \
1600 DEBUG_PRINT (" Popping pattern %p: ", pat); \
505bde11 1601 DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend); \
fa9a63c5
RM
1602 \
1603 /* If the saved string location is NULL, it came from an \
1604 on_failure_keep_string_jump opcode, and we want to throw away the \
1605 saved NULL, thus retaining our current position in the string. */ \
01618498 1606 str = POP_FAILURE_POINTER (); \
dc4a2ee0 1607 DEBUG_PRINT (" Popping string %p: `", str); \
fa9a63c5 1608 DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2); \
dc4a2ee0 1609 DEBUG_PRINT ("'\n"); \
fa9a63c5 1610 \
505bde11 1611 fail_stack.frame = POP_FAILURE_INT (); \
dc4a2ee0 1612 DEBUG_PRINT (" Popping frame index: %zd\n", fail_stack.frame); \
fa9a63c5 1613 \
505bde11
SM
1614 assert (fail_stack.avail >= 0); \
1615 assert (fail_stack.frame <= fail_stack.avail); \
fa9a63c5 1616 \
fa9a63c5 1617 DEBUG_STATEMENT (nfailure_points_popped++); \
505bde11 1618} while (0) /* POP_FAILURE_POINT */
fa9a63c5
RM
1619
1620
1621\f
fa9a63c5 1622/* Registers are set to a sentinel when they haven't yet matched. */
4bb91c68 1623#define REG_UNSET(e) ((e) == NULL)
fa9a63c5
RM
1624\f
1625/* Subroutine declarations and macros for regex_compile. */
1626
261cb4bb
PE
1627static reg_errcode_t regex_compile (re_char *pattern, size_t size,
1628 reg_syntax_t syntax,
1629 struct re_pattern_buffer *bufp);
1630static void store_op1 (re_opcode_t op, unsigned char *loc, int arg);
1631static void store_op2 (re_opcode_t op, unsigned char *loc, int arg1, int arg2);
1632static void insert_op1 (re_opcode_t op, unsigned char *loc,
1633 int arg, unsigned char *end);
1634static void insert_op2 (re_opcode_t op, unsigned char *loc,
1635 int arg1, int arg2, unsigned char *end);
1636static boolean at_begline_loc_p (re_char *pattern, re_char *p,
1637 reg_syntax_t syntax);
1638static boolean at_endline_loc_p (re_char *p, re_char *pend,
1639 reg_syntax_t syntax);
1640static re_char *skip_one_char (re_char *p);
1641static int analyse_first (re_char *p, re_char *pend,
1642 char *fastmap, const int multibyte);
fa9a63c5 1643
fa9a63c5 1644/* Fetch the next character in the uncompiled pattern, with no
4bb91c68 1645 translation. */
36595814 1646#define PATFETCH(c) \
2d1675e4
SM
1647 do { \
1648 int len; \
1649 if (p == pend) return REG_EEND; \
62a6e103 1650 c = RE_STRING_CHAR_AND_LENGTH (p, len, multibyte); \
2d1675e4 1651 p += len; \
fa9a63c5
RM
1652 } while (0)
1653
fa9a63c5
RM
1654
1655/* If `translate' is non-null, return translate[D], else just D. We
1656 cast the subscript to translate because some data is declared as
1657 `char *', to avoid warnings when a string constant is passed. But
1658 when we use a character as a subscript we must make it unsigned. */
6676cb1c 1659#ifndef TRANSLATE
0b32bf0e 1660# define TRANSLATE(d) \
66f0296e 1661 (RE_TRANSLATE_P (translate) ? RE_TRANSLATE (translate, (d)) : (d))
6676cb1c 1662#endif
fa9a63c5
RM
1663
1664
1665/* Macros for outputting the compiled pattern into `buffer'. */
1666
1667/* If the buffer isn't allocated when it comes in, use this. */
1668#define INIT_BUF_SIZE 32
1669
4bb91c68 1670/* Make sure we have at least N more bytes of space in buffer. */
fa9a63c5 1671#define GET_BUFFER_SPACE(n) \
01618498 1672 while ((size_t) (b - bufp->buffer + (n)) > bufp->allocated) \
fa9a63c5
RM
1673 EXTEND_BUFFER ()
1674
1675/* Make sure we have one more byte of buffer space and then add C to it. */
1676#define BUF_PUSH(c) \
1677 do { \
1678 GET_BUFFER_SPACE (1); \
1679 *b++ = (unsigned char) (c); \
1680 } while (0)
1681
1682
1683/* Ensure we have two more bytes of buffer space and then append C1 and C2. */
1684#define BUF_PUSH_2(c1, c2) \
1685 do { \
1686 GET_BUFFER_SPACE (2); \
1687 *b++ = (unsigned char) (c1); \
1688 *b++ = (unsigned char) (c2); \
1689 } while (0)
1690
1691
fa9a63c5 1692/* Store a jump with opcode OP at LOC to location TO. We store a
4bb91c68 1693 relative address offset by the three bytes the jump itself occupies. */
fa9a63c5
RM
1694#define STORE_JUMP(op, loc, to) \
1695 store_op1 (op, loc, (to) - (loc) - 3)
1696
1697/* Likewise, for a two-argument jump. */
1698#define STORE_JUMP2(op, loc, to, arg) \
1699 store_op2 (op, loc, (to) - (loc) - 3, arg)
1700
4bb91c68 1701/* Like `STORE_JUMP', but for inserting. Assume `b' is the buffer end. */
fa9a63c5
RM
1702#define INSERT_JUMP(op, loc, to) \
1703 insert_op1 (op, loc, (to) - (loc) - 3, b)
1704
1705/* Like `STORE_JUMP2', but for inserting. Assume `b' is the buffer end. */
1706#define INSERT_JUMP2(op, loc, to, arg) \
1707 insert_op2 (op, loc, (to) - (loc) - 3, arg, b)
1708
1709
1710/* This is not an arbitrary limit: the arguments which represent offsets
839966f3 1711 into the pattern are two bytes long. So if 2^15 bytes turns out to
fa9a63c5 1712 be too small, many things would have to change. */
839966f3
KH
1713# define MAX_BUF_SIZE (1L << 15)
1714
fa9a63c5
RM
1715/* Extend the buffer by twice its current size via realloc and
1716 reset the pointers that pointed into the old block to point to the
1717 correct places in the new one. If extending the buffer results in it
4bb91c68
SM
1718 being larger than MAX_BUF_SIZE, then flag memory exhausted. */
1719#if __BOUNDED_POINTERS__
1720# define SET_HIGH_BOUND(P) (__ptrhigh (P) = __ptrlow (P) + bufp->allocated)
381880b0
CY
1721# define MOVE_BUFFER_POINTER(P) \
1722 (__ptrlow (P) = new_buffer + (__ptrlow (P) - old_buffer), \
1723 SET_HIGH_BOUND (P), \
1724 __ptrvalue (P) = new_buffer + (__ptrvalue (P) - old_buffer))
4bb91c68
SM
1725# define ELSE_EXTEND_BUFFER_HIGH_BOUND \
1726 else \
1727 { \
1728 SET_HIGH_BOUND (b); \
1729 SET_HIGH_BOUND (begalt); \
1730 if (fixup_alt_jump) \
1731 SET_HIGH_BOUND (fixup_alt_jump); \
1732 if (laststart) \
1733 SET_HIGH_BOUND (laststart); \
1734 if (pending_exact) \
1735 SET_HIGH_BOUND (pending_exact); \
1736 }
1737#else
381880b0 1738# define MOVE_BUFFER_POINTER(P) ((P) = new_buffer + ((P) - old_buffer))
4bb91c68
SM
1739# define ELSE_EXTEND_BUFFER_HIGH_BOUND
1740#endif
fa9a63c5 1741#define EXTEND_BUFFER() \
25fe55af 1742 do { \
381880b0 1743 unsigned char *old_buffer = bufp->buffer; \
25fe55af 1744 if (bufp->allocated == MAX_BUF_SIZE) \
fa9a63c5
RM
1745 return REG_ESIZE; \
1746 bufp->allocated <<= 1; \
1747 if (bufp->allocated > MAX_BUF_SIZE) \
25fe55af 1748 bufp->allocated = MAX_BUF_SIZE; \
01618498 1749 RETALLOC (bufp->buffer, bufp->allocated, unsigned char); \
fa9a63c5
RM
1750 if (bufp->buffer == NULL) \
1751 return REG_ESPACE; \
1752 /* If the buffer moved, move all the pointers into it. */ \
1753 if (old_buffer != bufp->buffer) \
1754 { \
381880b0 1755 unsigned char *new_buffer = bufp->buffer; \
4bb91c68
SM
1756 MOVE_BUFFER_POINTER (b); \
1757 MOVE_BUFFER_POINTER (begalt); \
25fe55af 1758 if (fixup_alt_jump) \
4bb91c68 1759 MOVE_BUFFER_POINTER (fixup_alt_jump); \
25fe55af 1760 if (laststart) \
4bb91c68 1761 MOVE_BUFFER_POINTER (laststart); \
25fe55af 1762 if (pending_exact) \
4bb91c68 1763 MOVE_BUFFER_POINTER (pending_exact); \
fa9a63c5 1764 } \
4bb91c68 1765 ELSE_EXTEND_BUFFER_HIGH_BOUND \
fa9a63c5
RM
1766 } while (0)
1767
1768
1769/* Since we have one byte reserved for the register number argument to
1770 {start,stop}_memory, the maximum number of groups we can report
1771 things about is what fits in that byte. */
1772#define MAX_REGNUM 255
1773
1774/* But patterns can have more than `MAX_REGNUM' registers. We just
1775 ignore the excess. */
098d42af 1776typedef int regnum_t;
fa9a63c5
RM
1777
1778
1779/* Macros for the compile stack. */
1780
1781/* Since offsets can go either forwards or backwards, this type needs to
4bb91c68
SM
1782 be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1. */
1783/* int may be not enough when sizeof(int) == 2. */
1784typedef long pattern_offset_t;
fa9a63c5
RM
1785
1786typedef struct
1787{
1788 pattern_offset_t begalt_offset;
1789 pattern_offset_t fixup_alt_jump;
5e69f11e 1790 pattern_offset_t laststart_offset;
fa9a63c5
RM
1791 regnum_t regnum;
1792} compile_stack_elt_t;
1793
1794
1795typedef struct
1796{
1797 compile_stack_elt_t *stack;
d1dfb56c
EZ
1798 size_t size;
1799 size_t avail; /* Offset of next open position. */
fa9a63c5
RM
1800} compile_stack_type;
1801
1802
1803#define INIT_COMPILE_STACK_SIZE 32
1804
1805#define COMPILE_STACK_EMPTY (compile_stack.avail == 0)
1806#define COMPILE_STACK_FULL (compile_stack.avail == compile_stack.size)
1807
4bb91c68 1808/* The next available element. */
fa9a63c5
RM
1809#define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
1810
0caaedb1
PE
1811/* Explicit quit checking is needed for Emacs, which uses polling to
1812 process input events. */
1813#ifdef emacs
77d11aec
RS
1814# define IMMEDIATE_QUIT_CHECK \
1815 do { \
1816 if (immediate_quit) QUIT; \
1817 } while (0)
1818#else
1819# define IMMEDIATE_QUIT_CHECK ((void)0)
1820#endif
1821\f
b18215fc
RS
1822/* Structure to manage work area for range table. */
1823struct range_table_work_area
1824{
1825 int *table; /* actual work area. */
1826 int allocated; /* allocated size for work area in bytes. */
7814e705 1827 int used; /* actually used size in words. */
96cc36cc 1828 int bits; /* flag to record character classes */
b18215fc
RS
1829};
1830
77d11aec
RS
1831/* Make sure that WORK_AREA can hold more N multibyte characters.
1832 This is used only in set_image_of_range and set_image_of_range_1.
1833 It expects WORK_AREA to be a pointer.
1834 If it can't get the space, it returns from the surrounding function. */
1835
1836#define EXTEND_RANGE_TABLE(work_area, n) \
1837 do { \
8f924df7 1838 if (((work_area).used + (n)) * sizeof (int) > (work_area).allocated) \
77d11aec 1839 { \
8f924df7
KH
1840 extend_range_table_work_area (&work_area); \
1841 if ((work_area).table == 0) \
77d11aec
RS
1842 return (REG_ESPACE); \
1843 } \
b18215fc
RS
1844 } while (0)
1845
96cc36cc
RS
1846#define SET_RANGE_TABLE_WORK_AREA_BIT(work_area, bit) \
1847 (work_area).bits |= (bit)
1848
14473664
SM
1849/* Bits used to implement the multibyte-part of the various character classes
1850 such as [:alnum:] in a charset's range table. */
1851#define BIT_WORD 0x1
1852#define BIT_LOWER 0x2
1853#define BIT_PUNCT 0x4
1854#define BIT_SPACE 0x8
1855#define BIT_UPPER 0x10
1856#define BIT_MULTIBYTE 0x20
96cc36cc 1857
b18215fc
RS
1858/* Set a range (RANGE_START, RANGE_END) to WORK_AREA. */
1859#define SET_RANGE_TABLE_WORK_AREA(work_area, range_start, range_end) \
77d11aec 1860 do { \
8f924df7 1861 EXTEND_RANGE_TABLE ((work_area), 2); \
b18215fc
RS
1862 (work_area).table[(work_area).used++] = (range_start); \
1863 (work_area).table[(work_area).used++] = (range_end); \
1864 } while (0)
1865
7814e705 1866/* Free allocated memory for WORK_AREA. */
b18215fc
RS
1867#define FREE_RANGE_TABLE_WORK_AREA(work_area) \
1868 do { \
1869 if ((work_area).table) \
1870 free ((work_area).table); \
1871 } while (0)
1872
96cc36cc 1873#define CLEAR_RANGE_TABLE_WORK_USED(work_area) ((work_area).used = 0, (work_area).bits = 0)
b18215fc 1874#define RANGE_TABLE_WORK_USED(work_area) ((work_area).used)
96cc36cc 1875#define RANGE_TABLE_WORK_BITS(work_area) ((work_area).bits)
b18215fc 1876#define RANGE_TABLE_WORK_ELT(work_area, i) ((work_area).table[i])
77d11aec 1877\f
b18215fc 1878
fa9a63c5 1879/* Set the bit for character C in a list. */
01618498 1880#define SET_LIST_BIT(c) (b[((c)) / BYTEWIDTH] |= 1 << ((c) % BYTEWIDTH))
fa9a63c5
RM
1881
1882
bf216479
KH
1883#ifdef emacs
1884
cf9c99bc
KH
1885/* Store characters in the range FROM to TO in the bitmap at B (for
1886 ASCII and unibyte characters) and WORK_AREA (for multibyte
1887 characters) while translating them and paying attention to the
1888 continuity of translated characters.
8f924df7 1889
cf9c99bc
KH
1890 Implementation note: It is better to implement these fairly big
1891 macros by a function, but it's not that easy because macros called
8f924df7 1892 in this macro assume various local variables already declared. */
bf216479 1893
cf9c99bc
KH
1894/* Both FROM and TO are ASCII characters. */
1895
1896#define SETUP_ASCII_RANGE(work_area, FROM, TO) \
1897 do { \
1898 int C0, C1; \
1899 \
1900 for (C0 = (FROM); C0 <= (TO); C0++) \
1901 { \
1902 C1 = TRANSLATE (C0); \
1903 if (! ASCII_CHAR_P (C1)) \
1904 { \
1905 SET_RANGE_TABLE_WORK_AREA ((work_area), C1, C1); \
1906 if ((C1 = RE_CHAR_TO_UNIBYTE (C1)) < 0) \
1907 C1 = C0; \
1908 } \
1909 SET_LIST_BIT (C1); \
1910 } \
1911 } while (0)
1912
1913
1914/* Both FROM and TO are unibyte characters (0x80..0xFF). */
1915
1916#define SETUP_UNIBYTE_RANGE(work_area, FROM, TO) \
1917 do { \
1918 int C0, C1, C2, I; \
1919 int USED = RANGE_TABLE_WORK_USED (work_area); \
1920 \
1921 for (C0 = (FROM); C0 <= (TO); C0++) \
1922 { \
1923 C1 = RE_CHAR_TO_MULTIBYTE (C0); \
1924 if (CHAR_BYTE8_P (C1)) \
1925 SET_LIST_BIT (C0); \
1926 else \
1927 { \
1928 C2 = TRANSLATE (C1); \
1929 if (C2 == C1 \
1930 || (C1 = RE_CHAR_TO_UNIBYTE (C2)) < 0) \
1931 C1 = C0; \
1932 SET_LIST_BIT (C1); \
1933 for (I = RANGE_TABLE_WORK_USED (work_area) - 2; I >= USED; I -= 2) \
1934 { \
1935 int from = RANGE_TABLE_WORK_ELT (work_area, I); \
1936 int to = RANGE_TABLE_WORK_ELT (work_area, I + 1); \
1937 \
1938 if (C2 >= from - 1 && C2 <= to + 1) \
1939 { \
1940 if (C2 == from - 1) \
1941 RANGE_TABLE_WORK_ELT (work_area, I)--; \
1942 else if (C2 == to + 1) \
1943 RANGE_TABLE_WORK_ELT (work_area, I + 1)++; \
1944 break; \
1945 } \
1946 } \
1947 if (I < USED) \
1948 SET_RANGE_TABLE_WORK_AREA ((work_area), C2, C2); \
1949 } \
1950 } \
1951 } while (0)
1952
1953
78edd3b7 1954/* Both FROM and TO are multibyte characters. */
cf9c99bc
KH
1955
1956#define SETUP_MULTIBYTE_RANGE(work_area, FROM, TO) \
1957 do { \
1958 int C0, C1, C2, I, USED = RANGE_TABLE_WORK_USED (work_area); \
1959 \
1960 SET_RANGE_TABLE_WORK_AREA ((work_area), (FROM), (TO)); \
1961 for (C0 = (FROM); C0 <= (TO); C0++) \
1962 { \
1963 C1 = TRANSLATE (C0); \
1964 if ((C2 = RE_CHAR_TO_UNIBYTE (C1)) >= 0 \
1965 || (C1 != C0 && (C2 = RE_CHAR_TO_UNIBYTE (C0)) >= 0)) \
1966 SET_LIST_BIT (C2); \
1967 if (C1 >= (FROM) && C1 <= (TO)) \
1968 continue; \
1969 for (I = RANGE_TABLE_WORK_USED (work_area) - 2; I >= USED; I -= 2) \
1970 { \
1971 int from = RANGE_TABLE_WORK_ELT (work_area, I); \
1972 int to = RANGE_TABLE_WORK_ELT (work_area, I + 1); \
1973 \
1974 if (C1 >= from - 1 && C1 <= to + 1) \
1975 { \
1976 if (C1 == from - 1) \
1977 RANGE_TABLE_WORK_ELT (work_area, I)--; \
1978 else if (C1 == to + 1) \
1979 RANGE_TABLE_WORK_ELT (work_area, I + 1)++; \
1980 break; \
1981 } \
1982 } \
1983 if (I < USED) \
1984 SET_RANGE_TABLE_WORK_AREA ((work_area), C1, C1); \
1985 } \
bf216479
KH
1986 } while (0)
1987
1988#endif /* emacs */
1989
fa9a63c5 1990/* Get the next unsigned number in the uncompiled pattern. */
25fe55af 1991#define GET_UNSIGNED_NUMBER(num) \
c72b0edd
SM
1992 do { \
1993 if (p == pend) \
1994 FREE_STACK_RETURN (REG_EBRACE); \
1995 else \
1996 { \
1997 PATFETCH (c); \
1998 while ('0' <= c && c <= '9') \
1999 { \
2000 int prev; \
2001 if (num < 0) \
2002 num = 0; \
2003 prev = num; \
2004 num = num * 10 + c - '0'; \
2005 if (num / 10 != prev) \
2006 FREE_STACK_RETURN (REG_BADBR); \
2007 if (p == pend) \
2008 FREE_STACK_RETURN (REG_EBRACE); \
2009 PATFETCH (c); \
2010 } \
2011 } \
2012 } while (0)
77d11aec 2013\f
1fdab503 2014#if ! WIDE_CHAR_SUPPORT
01618498 2015
14473664 2016/* Map a string to the char class it names (if any). */
1fdab503 2017re_wctype_t
971de7fb 2018re_wctype (const re_char *str)
14473664 2019{
5b0534c8 2020 const char *string = (const char *) str;
14473664
SM
2021 if (STREQ (string, "alnum")) return RECC_ALNUM;
2022 else if (STREQ (string, "alpha")) return RECC_ALPHA;
2023 else if (STREQ (string, "word")) return RECC_WORD;
2024 else if (STREQ (string, "ascii")) return RECC_ASCII;
2025 else if (STREQ (string, "nonascii")) return RECC_NONASCII;
2026 else if (STREQ (string, "graph")) return RECC_GRAPH;
2027 else if (STREQ (string, "lower")) return RECC_LOWER;
2028 else if (STREQ (string, "print")) return RECC_PRINT;
2029 else if (STREQ (string, "punct")) return RECC_PUNCT;
2030 else if (STREQ (string, "space")) return RECC_SPACE;
2031 else if (STREQ (string, "upper")) return RECC_UPPER;
2032 else if (STREQ (string, "unibyte")) return RECC_UNIBYTE;
2033 else if (STREQ (string, "multibyte")) return RECC_MULTIBYTE;
2034 else if (STREQ (string, "digit")) return RECC_DIGIT;
2035 else if (STREQ (string, "xdigit")) return RECC_XDIGIT;
2036 else if (STREQ (string, "cntrl")) return RECC_CNTRL;
2037 else if (STREQ (string, "blank")) return RECC_BLANK;
2038 else return 0;
2039}
2040
e0f24100 2041/* True if CH is in the char class CC. */
1fdab503 2042boolean
971de7fb 2043re_iswctype (int ch, re_wctype_t cc)
14473664
SM
2044{
2045 switch (cc)
2046 {
f3fcc40d
AS
2047 case RECC_ALNUM: return ISALNUM (ch) != 0;
2048 case RECC_ALPHA: return ISALPHA (ch) != 0;
2049 case RECC_BLANK: return ISBLANK (ch) != 0;
2050 case RECC_CNTRL: return ISCNTRL (ch) != 0;
2051 case RECC_DIGIT: return ISDIGIT (ch) != 0;
2052 case RECC_GRAPH: return ISGRAPH (ch) != 0;
2053 case RECC_LOWER: return ISLOWER (ch) != 0;
2054 case RECC_PRINT: return ISPRINT (ch) != 0;
2055 case RECC_PUNCT: return ISPUNCT (ch) != 0;
2056 case RECC_SPACE: return ISSPACE (ch) != 0;
2057 case RECC_UPPER: return ISUPPER (ch) != 0;
2058 case RECC_XDIGIT: return ISXDIGIT (ch) != 0;
2059 case RECC_ASCII: return IS_REAL_ASCII (ch) != 0;
213bd7f2 2060 case RECC_NONASCII: return !IS_REAL_ASCII (ch);
f3fcc40d 2061 case RECC_UNIBYTE: return ISUNIBYTE (ch) != 0;
213bd7f2 2062 case RECC_MULTIBYTE: return !ISUNIBYTE (ch);
f3fcc40d 2063 case RECC_WORD: return ISWORD (ch) != 0;
0cdd06f8
SM
2064 case RECC_ERROR: return false;
2065 default:
5e617bc2 2066 abort ();
14473664
SM
2067 }
2068}
fa9a63c5 2069
14473664
SM
2070/* Return a bit-pattern to use in the range-table bits to match multibyte
2071 chars of class CC. */
2072static int
971de7fb 2073re_wctype_to_bit (re_wctype_t cc)
14473664
SM
2074{
2075 switch (cc)
2076 {
2077 case RECC_NONASCII: case RECC_PRINT: case RECC_GRAPH:
0cdd06f8
SM
2078 case RECC_MULTIBYTE: return BIT_MULTIBYTE;
2079 case RECC_ALPHA: case RECC_ALNUM: case RECC_WORD: return BIT_WORD;
2080 case RECC_LOWER: return BIT_LOWER;
2081 case RECC_UPPER: return BIT_UPPER;
2082 case RECC_PUNCT: return BIT_PUNCT;
2083 case RECC_SPACE: return BIT_SPACE;
14473664 2084 case RECC_ASCII: case RECC_DIGIT: case RECC_XDIGIT: case RECC_CNTRL:
0cdd06f8
SM
2085 case RECC_BLANK: case RECC_UNIBYTE: case RECC_ERROR: return 0;
2086 default:
5e617bc2 2087 abort ();
14473664
SM
2088 }
2089}
2090#endif
77d11aec
RS
2091\f
2092/* Filling in the work area of a range. */
2093
2094/* Actually extend the space in WORK_AREA. */
2095
2096static void
971de7fb 2097extend_range_table_work_area (struct range_table_work_area *work_area)
177c0ea7 2098{
77d11aec 2099 work_area->allocated += 16 * sizeof (int);
38182d90 2100 work_area->table = realloc (work_area->table, work_area->allocated);
77d11aec
RS
2101}
2102
8f924df7 2103#if 0
77d11aec
RS
2104#ifdef emacs
2105
2106/* Carefully find the ranges of codes that are equivalent
2107 under case conversion to the range start..end when passed through
2108 TRANSLATE. Handle the case where non-letters can come in between
2109 two upper-case letters (which happens in Latin-1).
2110 Also handle the case of groups of more than 2 case-equivalent chars.
2111
2112 The basic method is to look at consecutive characters and see
2113 if they can form a run that can be handled as one.
2114
2115 Returns -1 if successful, REG_ESPACE if ran out of space. */
2116
2117static int
1dae0f0a
AS
2118set_image_of_range_1 (struct range_table_work_area *work_area,
2119 re_wchar_t start, re_wchar_t end,
2120 RE_TRANSLATE_TYPE translate)
77d11aec
RS
2121{
2122 /* `one_case' indicates a character, or a run of characters,
2123 each of which is an isolate (no case-equivalents).
2124 This includes all ASCII non-letters.
2125
2126 `two_case' indicates a character, or a run of characters,
2127 each of which has two case-equivalent forms.
2128 This includes all ASCII letters.
2129
2130 `strange' indicates a character that has more than one
2131 case-equivalent. */
177c0ea7 2132
77d11aec
RS
2133 enum case_type {one_case, two_case, strange};
2134
2135 /* Describe the run that is in progress,
2136 which the next character can try to extend.
2137 If run_type is strange, that means there really is no run.
2138 If run_type is one_case, then run_start...run_end is the run.
2139 If run_type is two_case, then the run is run_start...run_end,
2140 and the case-equivalents end at run_eqv_end. */
2141
2142 enum case_type run_type = strange;
2143 int run_start, run_end, run_eqv_end;
2144
2145 Lisp_Object eqv_table;
2146
2147 if (!RE_TRANSLATE_P (translate))
2148 {
b7c12565 2149 EXTEND_RANGE_TABLE (work_area, 2);
77d11aec
RS
2150 work_area->table[work_area->used++] = (start);
2151 work_area->table[work_area->used++] = (end);
b7c12565 2152 return -1;
77d11aec
RS
2153 }
2154
2155 eqv_table = XCHAR_TABLE (translate)->extras[2];
99633e97 2156
77d11aec
RS
2157 for (; start <= end; start++)
2158 {
2159 enum case_type this_type;
2160 int eqv = RE_TRANSLATE (eqv_table, start);
2161 int minchar, maxchar;
2162
2163 /* Classify this character */
2164 if (eqv == start)
2165 this_type = one_case;
2166 else if (RE_TRANSLATE (eqv_table, eqv) == start)
2167 this_type = two_case;
2168 else
2169 this_type = strange;
2170
2171 if (start < eqv)
2172 minchar = start, maxchar = eqv;
2173 else
2174 minchar = eqv, maxchar = start;
2175
2176 /* Can this character extend the run in progress? */
2177 if (this_type == strange || this_type != run_type
2178 || !(minchar == run_end + 1
2179 && (run_type == two_case
2180 ? maxchar == run_eqv_end + 1 : 1)))
2181 {
2182 /* No, end the run.
2183 Record each of its equivalent ranges. */
2184 if (run_type == one_case)
2185 {
2186 EXTEND_RANGE_TABLE (work_area, 2);
2187 work_area->table[work_area->used++] = run_start;
2188 work_area->table[work_area->used++] = run_end;
2189 }
2190 else if (run_type == two_case)
2191 {
2192 EXTEND_RANGE_TABLE (work_area, 4);
2193 work_area->table[work_area->used++] = run_start;
2194 work_area->table[work_area->used++] = run_end;
2195 work_area->table[work_area->used++]
2196 = RE_TRANSLATE (eqv_table, run_start);
2197 work_area->table[work_area->used++]
2198 = RE_TRANSLATE (eqv_table, run_end);
2199 }
2200 run_type = strange;
2201 }
177c0ea7 2202
77d11aec
RS
2203 if (this_type == strange)
2204 {
2205 /* For a strange character, add each of its equivalents, one
2206 by one. Don't start a range. */
2207 do
2208 {
2209 EXTEND_RANGE_TABLE (work_area, 2);
2210 work_area->table[work_area->used++] = eqv;
2211 work_area->table[work_area->used++] = eqv;
2212 eqv = RE_TRANSLATE (eqv_table, eqv);
2213 }
2214 while (eqv != start);
2215 }
2216
2217 /* Add this char to the run, or start a new run. */
2218 else if (run_type == strange)
2219 {
2220 /* Initialize a new range. */
2221 run_type = this_type;
2222 run_start = start;
2223 run_end = start;
2224 run_eqv_end = RE_TRANSLATE (eqv_table, run_end);
2225 }
2226 else
2227 {
2228 /* Extend a running range. */
2229 run_end = minchar;
2230 run_eqv_end = RE_TRANSLATE (eqv_table, run_end);
2231 }
2232 }
2233
2234 /* If a run is still in progress at the end, finish it now
2235 by recording its equivalent ranges. */
2236 if (run_type == one_case)
2237 {
2238 EXTEND_RANGE_TABLE (work_area, 2);
2239 work_area->table[work_area->used++] = run_start;
2240 work_area->table[work_area->used++] = run_end;
2241 }
2242 else if (run_type == two_case)
2243 {
2244 EXTEND_RANGE_TABLE (work_area, 4);
2245 work_area->table[work_area->used++] = run_start;
2246 work_area->table[work_area->used++] = run_end;
2247 work_area->table[work_area->used++]
2248 = RE_TRANSLATE (eqv_table, run_start);
2249 work_area->table[work_area->used++]
2250 = RE_TRANSLATE (eqv_table, run_end);
2251 }
2252
2253 return -1;
2254}
36595814 2255
77d11aec 2256#endif /* emacs */
36595814 2257
2b34df4e 2258/* Record the image of the range start..end when passed through
36595814
SM
2259 TRANSLATE. This is not necessarily TRANSLATE(start)..TRANSLATE(end)
2260 and is not even necessarily contiguous.
b7c12565
RS
2261 Normally we approximate it with the smallest contiguous range that contains
2262 all the chars we need. However, for Latin-1 we go to extra effort
2263 to do a better job.
2264
2265 This function is not called for ASCII ranges.
77d11aec
RS
2266
2267 Returns -1 if successful, REG_ESPACE if ran out of space. */
2268
2269static int
1dae0f0a
AS
2270set_image_of_range (struct range_table_work_area *work_area,
2271 re_wchar_t start, re_wchar_t end,
2272 RE_TRANSLATE_TYPE translate)
36595814 2273{
77d11aec
RS
2274 re_wchar_t cmin, cmax;
2275
2276#ifdef emacs
2277 /* For Latin-1 ranges, use set_image_of_range_1
2278 to get proper handling of ranges that include letters and nonletters.
b7c12565 2279 For a range that includes the whole of Latin-1, this is not necessary.
77d11aec 2280 For other character sets, we don't bother to get this right. */
b7c12565
RS
2281 if (RE_TRANSLATE_P (translate) && start < 04400
2282 && !(start < 04200 && end >= 04377))
77d11aec 2283 {
b7c12565 2284 int newend;
77d11aec 2285 int tem;
b7c12565
RS
2286 newend = end;
2287 if (newend > 04377)
2288 newend = 04377;
2289 tem = set_image_of_range_1 (work_area, start, newend, translate);
77d11aec
RS
2290 if (tem > 0)
2291 return tem;
2292
2293 start = 04400;
2294 if (end < 04400)
2295 return -1;
2296 }
2297#endif
2298
b7c12565
RS
2299 EXTEND_RANGE_TABLE (work_area, 2);
2300 work_area->table[work_area->used++] = (start);
2301 work_area->table[work_area->used++] = (end);
2302
2303 cmin = -1, cmax = -1;
77d11aec 2304
36595814 2305 if (RE_TRANSLATE_P (translate))
b7c12565
RS
2306 {
2307 int ch;
77d11aec 2308
b7c12565
RS
2309 for (ch = start; ch <= end; ch++)
2310 {
2311 re_wchar_t c = TRANSLATE (ch);
2312 if (! (start <= c && c <= end))
2313 {
2314 if (cmin == -1)
2315 cmin = c, cmax = c;
2316 else
2317 {
2318 cmin = MIN (cmin, c);
2319 cmax = MAX (cmax, c);
2320 }
2321 }
2322 }
2323
2324 if (cmin != -1)
2325 {
2326 EXTEND_RANGE_TABLE (work_area, 2);
2327 work_area->table[work_area->used++] = (cmin);
2328 work_area->table[work_area->used++] = (cmax);
2329 }
2330 }
36595814 2331
77d11aec
RS
2332 return -1;
2333}
8f924df7 2334#endif /* 0 */
fa9a63c5
RM
2335\f
2336#ifndef MATCH_MAY_ALLOCATE
2337
2338/* If we cannot allocate large objects within re_match_2_internal,
2339 we make the fail stack and register vectors global.
2340 The fail stack, we grow to the maximum size when a regexp
2341 is compiled.
2342 The register vectors, we adjust in size each time we
2343 compile a regexp, according to the number of registers it needs. */
2344
2345static fail_stack_type fail_stack;
2346
2347/* Size with which the following vectors are currently allocated.
2348 That is so we can make them bigger as needed,
4bb91c68 2349 but never make them smaller. */
fa9a63c5
RM
2350static int regs_allocated_size;
2351
66f0296e
SM
2352static re_char ** regstart, ** regend;
2353static re_char **best_regstart, **best_regend;
fa9a63c5
RM
2354
2355/* Make the register vectors big enough for NUM_REGS registers,
4bb91c68 2356 but don't make them smaller. */
fa9a63c5
RM
2357
2358static
1dae0f0a 2359regex_grow_registers (int num_regs)
fa9a63c5
RM
2360{
2361 if (num_regs > regs_allocated_size)
2362 {
66f0296e
SM
2363 RETALLOC_IF (regstart, num_regs, re_char *);
2364 RETALLOC_IF (regend, num_regs, re_char *);
2365 RETALLOC_IF (best_regstart, num_regs, re_char *);
2366 RETALLOC_IF (best_regend, num_regs, re_char *);
fa9a63c5
RM
2367
2368 regs_allocated_size = num_regs;
2369 }
2370}
2371
2372#endif /* not MATCH_MAY_ALLOCATE */
2373\f
261cb4bb
PE
2374static boolean group_in_compile_stack (compile_stack_type compile_stack,
2375 regnum_t regnum);
99633e97 2376
fa9a63c5
RM
2377/* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
2378 Returns one of error codes defined in `regex.h', or zero for success.
2379
2380 Assumes the `allocated' (and perhaps `buffer') and `translate'
2381 fields are set in BUFP on entry.
2382
2383 If it succeeds, results are put in BUFP (if it returns an error, the
2384 contents of BUFP are undefined):
2385 `buffer' is the compiled pattern;
2386 `syntax' is set to SYNTAX;
2387 `used' is set to the length of the compiled pattern;
2388 `fastmap_accurate' is zero;
2389 `re_nsub' is the number of subexpressions in PATTERN;
2390 `not_bol' and `not_eol' are zero;
5e69f11e 2391
c0f9ea08 2392 The `fastmap' field is neither examined nor set. */
fa9a63c5 2393
505bde11
SM
2394/* Insert the `jump' from the end of last alternative to "here".
2395 The space for the jump has already been allocated. */
2396#define FIXUP_ALT_JUMP() \
2397do { \
2398 if (fixup_alt_jump) \
2399 STORE_JUMP (jump, fixup_alt_jump, b); \
2400} while (0)
2401
2402
fa9a63c5
RM
2403/* Return, freeing storage we allocated. */
2404#define FREE_STACK_RETURN(value) \
b18215fc
RS
2405 do { \
2406 FREE_RANGE_TABLE_WORK_AREA (range_table_work); \
2407 free (compile_stack.stack); \
2408 return value; \
2409 } while (0)
fa9a63c5
RM
2410
2411static reg_errcode_t
971de7fb 2412regex_compile (const re_char *pattern, size_t size, reg_syntax_t syntax, struct re_pattern_buffer *bufp)
fa9a63c5 2413{
01618498
SM
2414 /* We fetch characters from PATTERN here. */
2415 register re_wchar_t c, c1;
5e69f11e 2416
fa9a63c5
RM
2417 /* Points to the end of the buffer, where we should append. */
2418 register unsigned char *b;
5e69f11e 2419
fa9a63c5
RM
2420 /* Keeps track of unclosed groups. */
2421 compile_stack_type compile_stack;
2422
2423 /* Points to the current (ending) position in the pattern. */
22336245
RS
2424#ifdef AIX
2425 /* `const' makes AIX compiler fail. */
66f0296e 2426 unsigned char *p = pattern;
22336245 2427#else
66f0296e 2428 re_char *p = pattern;
22336245 2429#endif
66f0296e 2430 re_char *pend = pattern + size;
5e69f11e 2431
fa9a63c5 2432 /* How to translate the characters in the pattern. */
6676cb1c 2433 RE_TRANSLATE_TYPE translate = bufp->translate;
fa9a63c5
RM
2434
2435 /* Address of the count-byte of the most recently inserted `exactn'
2436 command. This makes it possible to tell if a new exact-match
2437 character can be added to that command or if the character requires
2438 a new `exactn' command. */
2439 unsigned char *pending_exact = 0;
2440
2441 /* Address of start of the most recently finished expression.
2442 This tells, e.g., postfix * where to find the start of its
2443 operand. Reset at the beginning of groups and alternatives. */
2444 unsigned char *laststart = 0;
2445
2446 /* Address of beginning of regexp, or inside of last group. */
2447 unsigned char *begalt;
2448
2449 /* Place in the uncompiled pattern (i.e., the {) to
2450 which to go back if the interval is invalid. */
66f0296e 2451 re_char *beg_interval;
5e69f11e 2452
fa9a63c5 2453 /* Address of the place where a forward jump should go to the end of
7814e705 2454 the containing expression. Each alternative of an `or' -- except the
fa9a63c5
RM
2455 last -- ends with a forward jump of this sort. */
2456 unsigned char *fixup_alt_jump = 0;
2457
b18215fc
RS
2458 /* Work area for range table of charset. */
2459 struct range_table_work_area range_table_work;
2460
2d1675e4
SM
2461 /* If the object matched can contain multibyte characters. */
2462 const boolean multibyte = RE_MULTIBYTE_P (bufp);
2463
f9b0fd99
RS
2464 /* Nonzero if we have pushed down into a subpattern. */
2465 int in_subpattern = 0;
2466
2467 /* These hold the values of p, pattern, and pend from the main
2468 pattern when we have pushed into a subpattern. */
da053e48
PE
2469 re_char *main_p IF_LINT (= NULL);
2470 re_char *main_pattern IF_LINT (= NULL);
2471 re_char *main_pend IF_LINT (= NULL);
f9b0fd99 2472
fa9a63c5 2473#ifdef DEBUG
99633e97 2474 debug++;
dc4a2ee0 2475 DEBUG_PRINT ("\nCompiling pattern: ");
99633e97 2476 if (debug > 0)
fa9a63c5
RM
2477 {
2478 unsigned debug_count;
5e69f11e 2479
fa9a63c5 2480 for (debug_count = 0; debug_count < size; debug_count++)
25fe55af 2481 putchar (pattern[debug_count]);
fa9a63c5
RM
2482 putchar ('\n');
2483 }
2484#endif /* DEBUG */
2485
2486 /* Initialize the compile stack. */
2487 compile_stack.stack = TALLOC (INIT_COMPILE_STACK_SIZE, compile_stack_elt_t);
2488 if (compile_stack.stack == NULL)
2489 return REG_ESPACE;
2490
2491 compile_stack.size = INIT_COMPILE_STACK_SIZE;
2492 compile_stack.avail = 0;
2493
b18215fc
RS
2494 range_table_work.table = 0;
2495 range_table_work.allocated = 0;
2496
fa9a63c5
RM
2497 /* Initialize the pattern buffer. */
2498 bufp->syntax = syntax;
2499 bufp->fastmap_accurate = 0;
2500 bufp->not_bol = bufp->not_eol = 0;
6224b623 2501 bufp->used_syntax = 0;
fa9a63c5
RM
2502
2503 /* Set `used' to zero, so that if we return an error, the pattern
2504 printer (for debugging) will think there's no pattern. We reset it
2505 at the end. */
2506 bufp->used = 0;
5e69f11e 2507
fa9a63c5 2508 /* Always count groups, whether or not bufp->no_sub is set. */
5e69f11e 2509 bufp->re_nsub = 0;
fa9a63c5 2510
0b32bf0e 2511#if !defined emacs && !defined SYNTAX_TABLE
fa9a63c5
RM
2512 /* Initialize the syntax table. */
2513 init_syntax_once ();
2514#endif
2515
2516 if (bufp->allocated == 0)
2517 {
2518 if (bufp->buffer)
2519 { /* If zero allocated, but buffer is non-null, try to realloc
25fe55af 2520 enough space. This loses if buffer's address is bogus, but
7814e705 2521 that is the user's responsibility. */
25fe55af
RS
2522 RETALLOC (bufp->buffer, INIT_BUF_SIZE, unsigned char);
2523 }
fa9a63c5 2524 else
7814e705 2525 { /* Caller did not allocate a buffer. Do it for them. */
25fe55af
RS
2526 bufp->buffer = TALLOC (INIT_BUF_SIZE, unsigned char);
2527 }
fa9a63c5
RM
2528 if (!bufp->buffer) FREE_STACK_RETURN (REG_ESPACE);
2529
2530 bufp->allocated = INIT_BUF_SIZE;
2531 }
2532
2533 begalt = b = bufp->buffer;
2534
2535 /* Loop through the uncompiled pattern until we're at the end. */
f9b0fd99 2536 while (1)
fa9a63c5 2537 {
f9b0fd99
RS
2538 if (p == pend)
2539 {
2540 /* If this is the end of an included regexp,
2541 pop back to the main regexp and try again. */
2542 if (in_subpattern)
2543 {
2544 in_subpattern = 0;
2545 pattern = main_pattern;
2546 p = main_p;
2547 pend = main_pend;
2548 continue;
2549 }
2550 /* If this is the end of the main regexp, we are done. */
2551 break;
2552 }
2553
fa9a63c5
RM
2554 PATFETCH (c);
2555
2556 switch (c)
25fe55af 2557 {
f9b0fd99
RS
2558 case ' ':
2559 {
2560 re_char *p1 = p;
2561
2562 /* If there's no special whitespace regexp, treat
4fb680cd
RS
2563 spaces normally. And don't try to do this recursively. */
2564 if (!whitespace_regexp || in_subpattern)
f9b0fd99
RS
2565 goto normal_char;
2566
2567 /* Peek past following spaces. */
2568 while (p1 != pend)
2569 {
2570 if (*p1 != ' ')
2571 break;
2572 p1++;
2573 }
2574 /* If the spaces are followed by a repetition op,
2575 treat them normally. */
c721eee5
RS
2576 if (p1 != pend
2577 && (*p1 == '*' || *p1 == '+' || *p1 == '?'
f9b0fd99
RS
2578 || (*p1 == '\\' && p1 + 1 != pend && p1[1] == '{')))
2579 goto normal_char;
2580
2581 /* Replace the spaces with the whitespace regexp. */
2582 in_subpattern = 1;
2583 main_p = p1;
2584 main_pend = pend;
2585 main_pattern = pattern;
2586 p = pattern = whitespace_regexp;
5b0534c8 2587 pend = p + strlen ((const char *) p);
f9b0fd99 2588 break;
7814e705 2589 }
f9b0fd99 2590
25fe55af
RS
2591 case '^':
2592 {
7814e705 2593 if ( /* If at start of pattern, it's an operator. */
25fe55af 2594 p == pattern + 1
7814e705 2595 /* If context independent, it's an operator. */
25fe55af 2596 || syntax & RE_CONTEXT_INDEP_ANCHORS
7814e705 2597 /* Otherwise, depends on what's come before. */
25fe55af 2598 || at_begline_loc_p (pattern, p, syntax))
c0f9ea08 2599 BUF_PUSH ((syntax & RE_NO_NEWLINE_ANCHOR) ? begbuf : begline);
25fe55af
RS
2600 else
2601 goto normal_char;
2602 }
2603 break;
2604
2605
2606 case '$':
2607 {
2608 if ( /* If at end of pattern, it's an operator. */
2609 p == pend
7814e705 2610 /* If context independent, it's an operator. */
25fe55af
RS
2611 || syntax & RE_CONTEXT_INDEP_ANCHORS
2612 /* Otherwise, depends on what's next. */
2613 || at_endline_loc_p (p, pend, syntax))
c0f9ea08 2614 BUF_PUSH ((syntax & RE_NO_NEWLINE_ANCHOR) ? endbuf : endline);
25fe55af
RS
2615 else
2616 goto normal_char;
2617 }
2618 break;
fa9a63c5
RM
2619
2620
2621 case '+':
25fe55af
RS
2622 case '?':
2623 if ((syntax & RE_BK_PLUS_QM)
2624 || (syntax & RE_LIMITED_OPS))
2625 goto normal_char;
2626 handle_plus:
2627 case '*':
5ac2eb34 2628 /* If there is no previous pattern... */
25fe55af
RS
2629 if (!laststart)
2630 {
2631 if (syntax & RE_CONTEXT_INVALID_OPS)
2632 FREE_STACK_RETURN (REG_BADRPT);
2633 else if (!(syntax & RE_CONTEXT_INDEP_OPS))
2634 goto normal_char;
2635 }
2636
2637 {
7814e705 2638 /* 1 means zero (many) matches is allowed. */
66f0296e
SM
2639 boolean zero_times_ok = 0, many_times_ok = 0;
2640 boolean greedy = 1;
25fe55af
RS
2641
2642 /* If there is a sequence of repetition chars, collapse it
2643 down to just one (the right one). We can't combine
2644 interval operators with these because of, e.g., `a{2}*',
7814e705 2645 which should only match an even number of `a's. */
25fe55af
RS
2646
2647 for (;;)
2648 {
0b32bf0e 2649 if ((syntax & RE_FRUGAL)
1c8c6d39
DL
2650 && c == '?' && (zero_times_ok || many_times_ok))
2651 greedy = 0;
2652 else
2653 {
2654 zero_times_ok |= c != '+';
2655 many_times_ok |= c != '?';
2656 }
25fe55af
RS
2657
2658 if (p == pend)
2659 break;
ed0767d8
SM
2660 else if (*p == '*'
2661 || (!(syntax & RE_BK_PLUS_QM)
2662 && (*p == '+' || *p == '?')))
25fe55af 2663 ;
ed0767d8 2664 else if (syntax & RE_BK_PLUS_QM && *p == '\\')
25fe55af 2665 {
ed0767d8
SM
2666 if (p+1 == pend)
2667 FREE_STACK_RETURN (REG_EESCAPE);
2668 if (p[1] == '+' || p[1] == '?')
2669 PATFETCH (c); /* Gobble up the backslash. */
2670 else
2671 break;
25fe55af
RS
2672 }
2673 else
ed0767d8 2674 break;
25fe55af 2675 /* If we get here, we found another repeat character. */
ed0767d8
SM
2676 PATFETCH (c);
2677 }
25fe55af
RS
2678
2679 /* Star, etc. applied to an empty pattern is equivalent
2680 to an empty pattern. */
4e8a9132 2681 if (!laststart || laststart == b)
25fe55af
RS
2682 break;
2683
2684 /* Now we know whether or not zero matches is allowed
7814e705 2685 and also whether or not two or more matches is allowed. */
1c8c6d39
DL
2686 if (greedy)
2687 {
99633e97 2688 if (many_times_ok)
4e8a9132
SM
2689 {
2690 boolean simple = skip_one_char (laststart) == b;
d1dfb56c 2691 size_t startoffset = 0;
f6a3f532 2692 re_opcode_t ofj =
01618498 2693 /* Check if the loop can match the empty string. */
6df42991
SM
2694 (simple || !analyse_first (laststart, b, NULL, 0))
2695 ? on_failure_jump : on_failure_jump_loop;
4e8a9132 2696 assert (skip_one_char (laststart) <= b);
177c0ea7 2697
4e8a9132
SM
2698 if (!zero_times_ok && simple)
2699 { /* Since simple * loops can be made faster by using
2700 on_failure_keep_string_jump, we turn simple P+
2701 into PP* if P is simple. */
2702 unsigned char *p1, *p2;
2703 startoffset = b - laststart;
2704 GET_BUFFER_SPACE (startoffset);
2705 p1 = b; p2 = laststart;
2706 while (p2 < p1)
2707 *b++ = *p2++;
2708 zero_times_ok = 1;
99633e97 2709 }
4e8a9132
SM
2710
2711 GET_BUFFER_SPACE (6);
2712 if (!zero_times_ok)
2713 /* A + loop. */
f6a3f532 2714 STORE_JUMP (ofj, b, b + 6);
99633e97 2715 else
4e8a9132
SM
2716 /* Simple * loops can use on_failure_keep_string_jump
2717 depending on what follows. But since we don't know
2718 that yet, we leave the decision up to
2719 on_failure_jump_smart. */
f6a3f532 2720 INSERT_JUMP (simple ? on_failure_jump_smart : ofj,
4e8a9132 2721 laststart + startoffset, b + 6);
99633e97 2722 b += 3;
4e8a9132 2723 STORE_JUMP (jump, b, laststart + startoffset);
99633e97
SM
2724 b += 3;
2725 }
2726 else
2727 {
4e8a9132
SM
2728 /* A simple ? pattern. */
2729 assert (zero_times_ok);
2730 GET_BUFFER_SPACE (3);
2731 INSERT_JUMP (on_failure_jump, laststart, b + 3);
99633e97
SM
2732 b += 3;
2733 }
1c8c6d39
DL
2734 }
2735 else /* not greedy */
5ac2eb34 2736 { /* I wish the greedy and non-greedy cases could be merged. */
1c8c6d39 2737
0683b6fa 2738 GET_BUFFER_SPACE (7); /* We might use less. */
1c8c6d39
DL
2739 if (many_times_ok)
2740 {
f6a3f532
SM
2741 boolean emptyp = analyse_first (laststart, b, NULL, 0);
2742
6df42991
SM
2743 /* The non-greedy multiple match looks like
2744 a repeat..until: we only need a conditional jump
2745 at the end of the loop. */
f6a3f532
SM
2746 if (emptyp) BUF_PUSH (no_op);
2747 STORE_JUMP (emptyp ? on_failure_jump_nastyloop
2748 : on_failure_jump, b, laststart);
1c8c6d39
DL
2749 b += 3;
2750 if (zero_times_ok)
2751 {
2752 /* The repeat...until naturally matches one or more.
2753 To also match zero times, we need to first jump to
6df42991 2754 the end of the loop (its conditional jump). */
1c8c6d39
DL
2755 INSERT_JUMP (jump, laststart, b);
2756 b += 3;
2757 }
2758 }
2759 else
2760 {
2761 /* non-greedy a?? */
1c8c6d39
DL
2762 INSERT_JUMP (jump, laststart, b + 3);
2763 b += 3;
2764 INSERT_JUMP (on_failure_jump, laststart, laststart + 6);
2765 b += 3;
2766 }
2767 }
2768 }
4e8a9132 2769 pending_exact = 0;
fa9a63c5
RM
2770 break;
2771
2772
2773 case '.':
25fe55af
RS
2774 laststart = b;
2775 BUF_PUSH (anychar);
2776 break;
fa9a63c5
RM
2777
2778
25fe55af
RS
2779 case '[':
2780 {
19ed5445
PE
2781 re_char *p1;
2782
b18215fc 2783 CLEAR_RANGE_TABLE_WORK_USED (range_table_work);
fa9a63c5 2784
25fe55af 2785 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
fa9a63c5 2786
25fe55af
RS
2787 /* Ensure that we have enough space to push a charset: the
2788 opcode, the length count, and the bitset; 34 bytes in all. */
fa9a63c5
RM
2789 GET_BUFFER_SPACE (34);
2790
25fe55af 2791 laststart = b;
e318085a 2792
25fe55af 2793 /* We test `*p == '^' twice, instead of using an if
7814e705 2794 statement, so we only need one BUF_PUSH. */
25fe55af
RS
2795 BUF_PUSH (*p == '^' ? charset_not : charset);
2796 if (*p == '^')
2797 p++;
e318085a 2798
25fe55af
RS
2799 /* Remember the first position in the bracket expression. */
2800 p1 = p;
e318085a 2801
7814e705 2802 /* Push the number of bytes in the bitmap. */
25fe55af 2803 BUF_PUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
e318085a 2804
25fe55af 2805 /* Clear the whole map. */
72af86bd 2806 memset (b, 0, (1 << BYTEWIDTH) / BYTEWIDTH);
e318085a 2807
25fe55af
RS
2808 /* charset_not matches newline according to a syntax bit. */
2809 if ((re_opcode_t) b[-2] == charset_not
2810 && (syntax & RE_HAT_LISTS_NOT_NEWLINE))
2811 SET_LIST_BIT ('\n');
fa9a63c5 2812
7814e705 2813 /* Read in characters and ranges, setting map bits. */
25fe55af
RS
2814 for (;;)
2815 {
b18215fc 2816 boolean escaped_char = false;
2d1675e4 2817 const unsigned char *p2 = p;
abbd1bcf 2818 re_wchar_t ch;
e318085a 2819
25fe55af 2820 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
e318085a 2821
36595814
SM
2822 /* Don't translate yet. The range TRANSLATE(X..Y) cannot
2823 always be determined from TRANSLATE(X) and TRANSLATE(Y)
2824 So the translation is done later in a loop. Example:
2825 (let ((case-fold-search t)) (string-match "[A-_]" "A")) */
25fe55af 2826 PATFETCH (c);
e318085a 2827
25fe55af
RS
2828 /* \ might escape characters inside [...] and [^...]. */
2829 if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) && c == '\\')
2830 {
2831 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
e318085a
RS
2832
2833 PATFETCH (c);
b18215fc 2834 escaped_char = true;
25fe55af 2835 }
b18215fc
RS
2836 else
2837 {
7814e705 2838 /* Could be the end of the bracket expression. If it's
657fcfbd
RS
2839 not (i.e., when the bracket expression is `[]' so
2840 far), the ']' character bit gets set way below. */
2d1675e4 2841 if (c == ']' && p2 != p1)
657fcfbd 2842 break;
25fe55af 2843 }
b18215fc 2844
25fe55af
RS
2845 /* See if we're at the beginning of a possible character
2846 class. */
b18215fc 2847
2d1675e4
SM
2848 if (!escaped_char &&
2849 syntax & RE_CHAR_CLASSES && c == '[' && *p == ':')
657fcfbd 2850 {
7814e705 2851 /* Leave room for the null. */
14473664 2852 unsigned char str[CHAR_CLASS_MAX_LENGTH + 1];
ed0767d8 2853 const unsigned char *class_beg;
b18215fc 2854
25fe55af
RS
2855 PATFETCH (c);
2856 c1 = 0;
ed0767d8 2857 class_beg = p;
b18215fc 2858
25fe55af
RS
2859 /* If pattern is `[[:'. */
2860 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
b18215fc 2861
25fe55af
RS
2862 for (;;)
2863 {
14473664
SM
2864 PATFETCH (c);
2865 if ((c == ':' && *p == ']') || p == pend)
2866 break;
2867 if (c1 < CHAR_CLASS_MAX_LENGTH)
2868 str[c1++] = c;
2869 else
2870 /* This is in any case an invalid class name. */
2871 str[0] = '\0';
25fe55af
RS
2872 }
2873 str[c1] = '\0';
b18215fc
RS
2874
2875 /* If isn't a word bracketed by `[:' and `:]':
2876 undo the ending character, the letters, and
2877 leave the leading `:' and `[' (but set bits for
2878 them). */
25fe55af
RS
2879 if (c == ':' && *p == ']')
2880 {
abbd1bcf 2881 re_wctype_t cc = re_wctype (str);
14473664
SM
2882
2883 if (cc == 0)
fa9a63c5
RM
2884 FREE_STACK_RETURN (REG_ECTYPE);
2885
14473664
SM
2886 /* Throw away the ] at the end of the character
2887 class. */
2888 PATFETCH (c);
fa9a63c5 2889
14473664 2890 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
fa9a63c5 2891
cf9c99bc
KH
2892#ifndef emacs
2893 for (ch = 0; ch < (1 << BYTEWIDTH); ++ch)
8f924df7
KH
2894 if (re_iswctype (btowc (ch), cc))
2895 {
2896 c = TRANSLATE (ch);
ed00c2ac
KH
2897 if (c < (1 << BYTEWIDTH))
2898 SET_LIST_BIT (c);
8f924df7 2899 }
cf9c99bc
KH
2900#else /* emacs */
2901 /* Most character classes in a multibyte match
2902 just set a flag. Exceptions are is_blank,
2903 is_digit, is_cntrl, and is_xdigit, since
2904 they can only match ASCII characters. We
2905 don't need to handle them for multibyte.
2906 They are distinguished by a negative wctype. */
96cc36cc 2907
254c06a8
SM
2908 /* Setup the gl_state object to its buffer-defined
2909 value. This hardcodes the buffer-global
2910 syntax-table for ASCII chars, while the other chars
2911 will obey syntax-table properties. It's not ideal,
2912 but it's the way it's been done until now. */
d48cd3f4 2913 SETUP_BUFFER_SYNTAX_TABLE ();
254c06a8 2914
cf9c99bc 2915 for (ch = 0; ch < 256; ++ch)
25fe55af 2916 {
cf9c99bc
KH
2917 c = RE_CHAR_TO_MULTIBYTE (ch);
2918 if (! CHAR_BYTE8_P (c)
2919 && re_iswctype (c, cc))
8f924df7 2920 {
cf9c99bc
KH
2921 SET_LIST_BIT (ch);
2922 c1 = TRANSLATE (c);
2923 if (c1 == c)
2924 continue;
2925 if (ASCII_CHAR_P (c1))
2926 SET_LIST_BIT (c1);
2927 else if ((c1 = RE_CHAR_TO_UNIBYTE (c1)) >= 0)
2928 SET_LIST_BIT (c1);
8f924df7 2929 }
25fe55af 2930 }
cf9c99bc
KH
2931 SET_RANGE_TABLE_WORK_AREA_BIT
2932 (range_table_work, re_wctype_to_bit (cc));
2933#endif /* emacs */
6224b623
SM
2934 /* In most cases the matching rule for char classes
2935 only uses the syntax table for multibyte chars,
2936 so that the content of the syntax-table it is not
2937 hardcoded in the range_table. SPACE and WORD are
2938 the two exceptions. */
2939 if ((1 << cc) & ((1 << RECC_SPACE) | (1 << RECC_WORD)))
2940 bufp->used_syntax = 1;
2941
b18215fc
RS
2942 /* Repeat the loop. */
2943 continue;
25fe55af
RS
2944 }
2945 else
2946 {
ed0767d8
SM
2947 /* Go back to right after the "[:". */
2948 p = class_beg;
25fe55af 2949 SET_LIST_BIT ('[');
b18215fc
RS
2950
2951 /* Because the `:' may starts the range, we
2952 can't simply set bit and repeat the loop.
7814e705 2953 Instead, just set it to C and handle below. */
b18215fc 2954 c = ':';
25fe55af
RS
2955 }
2956 }
b18215fc
RS
2957
2958 if (p < pend && p[0] == '-' && p[1] != ']')
2959 {
2960
2961 /* Discard the `-'. */
2962 PATFETCH (c1);
2963
2964 /* Fetch the character which ends the range. */
2965 PATFETCH (c1);
cf9c99bc
KH
2966#ifdef emacs
2967 if (CHAR_BYTE8_P (c1)
2968 && ! ASCII_CHAR_P (c) && ! CHAR_BYTE8_P (c))
2969 /* Treat the range from a multibyte character to
2970 raw-byte character as empty. */
2971 c = c1 + 1;
2972#endif /* emacs */
e318085a 2973 }
25fe55af 2974 else
b18215fc
RS
2975 /* Range from C to C. */
2976 c1 = c;
2977
cf9c99bc 2978 if (c > c1)
25fe55af 2979 {
cf9c99bc
KH
2980 if (syntax & RE_NO_EMPTY_RANGES)
2981 FREE_STACK_RETURN (REG_ERANGEX);
2982 /* Else, repeat the loop. */
bf216479 2983 }
6fdd04b0 2984 else
25fe55af 2985 {
cf9c99bc
KH
2986#ifndef emacs
2987 /* Set the range into bitmap */
8f924df7 2988 for (; c <= c1; c++)
b18215fc 2989 {
cf9c99bc
KH
2990 ch = TRANSLATE (c);
2991 if (ch < (1 << BYTEWIDTH))
2992 SET_LIST_BIT (ch);
2993 }
2994#else /* emacs */
2995 if (c < 128)
2996 {
2997 ch = MIN (127, c1);
2998 SETUP_ASCII_RANGE (range_table_work, c, ch);
2999 c = ch + 1;
3000 if (CHAR_BYTE8_P (c1))
3001 c = BYTE8_TO_CHAR (128);
3002 }
3003 if (c <= c1)
3004 {
3005 if (CHAR_BYTE8_P (c))
3006 {
3007 c = CHAR_TO_BYTE8 (c);
3008 c1 = CHAR_TO_BYTE8 (c1);
3009 for (; c <= c1; c++)
3010 SET_LIST_BIT (c);
3011 }
3012 else if (multibyte)
3013 {
3014 SETUP_MULTIBYTE_RANGE (range_table_work, c, c1);
3015 }
3016 else
3017 {
3018 SETUP_UNIBYTE_RANGE (range_table_work, c, c1);
3019 }
e934739e 3020 }
cf9c99bc 3021#endif /* emacs */
25fe55af 3022 }
e318085a
RS
3023 }
3024
25fe55af 3025 /* Discard any (non)matching list bytes that are all 0 at the
7814e705 3026 end of the map. Decrease the map-length byte too. */
25fe55af
RS
3027 while ((int) b[-1] > 0 && b[b[-1] - 1] == 0)
3028 b[-1]--;
3029 b += b[-1];
fa9a63c5 3030
96cc36cc
RS
3031 /* Build real range table from work area. */
3032 if (RANGE_TABLE_WORK_USED (range_table_work)
3033 || RANGE_TABLE_WORK_BITS (range_table_work))
b18215fc
RS
3034 {
3035 int i;
3036 int used = RANGE_TABLE_WORK_USED (range_table_work);
fa9a63c5 3037
b18215fc 3038 /* Allocate space for COUNT + RANGE_TABLE. Needs two
96cc36cc 3039 bytes for flags, two for COUNT, and three bytes for
5ac2eb34 3040 each character. */
96cc36cc 3041 GET_BUFFER_SPACE (4 + used * 3);
fa9a63c5 3042
b18215fc
RS
3043 /* Indicate the existence of range table. */
3044 laststart[1] |= 0x80;
fa9a63c5 3045
96cc36cc
RS
3046 /* Store the character class flag bits into the range table.
3047 If not in emacs, these flag bits are always 0. */
3048 *b++ = RANGE_TABLE_WORK_BITS (range_table_work) & 0xff;
3049 *b++ = RANGE_TABLE_WORK_BITS (range_table_work) >> 8;
3050
b18215fc
RS
3051 STORE_NUMBER_AND_INCR (b, used / 2);
3052 for (i = 0; i < used; i++)
3053 STORE_CHARACTER_AND_INCR
3054 (b, RANGE_TABLE_WORK_ELT (range_table_work, i));
3055 }
25fe55af
RS
3056 }
3057 break;
fa9a63c5
RM
3058
3059
b18215fc 3060 case '(':
25fe55af
RS
3061 if (syntax & RE_NO_BK_PARENS)
3062 goto handle_open;
3063 else
3064 goto normal_char;
fa9a63c5
RM
3065
3066
25fe55af
RS
3067 case ')':
3068 if (syntax & RE_NO_BK_PARENS)
3069 goto handle_close;
3070 else
3071 goto normal_char;
e318085a
RS
3072
3073
25fe55af
RS
3074 case '\n':
3075 if (syntax & RE_NEWLINE_ALT)
3076 goto handle_alt;
3077 else
3078 goto normal_char;
e318085a
RS
3079
3080
b18215fc 3081 case '|':
25fe55af
RS
3082 if (syntax & RE_NO_BK_VBAR)
3083 goto handle_alt;
3084 else
3085 goto normal_char;
3086
3087
3088 case '{':
3089 if (syntax & RE_INTERVALS && syntax & RE_NO_BK_BRACES)
3090 goto handle_interval;
3091 else
3092 goto normal_char;
3093
3094
3095 case '\\':
3096 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
3097
3098 /* Do not translate the character after the \, so that we can
3099 distinguish, e.g., \B from \b, even if we normally would
3100 translate, e.g., B to b. */
36595814 3101 PATFETCH (c);
25fe55af
RS
3102
3103 switch (c)
3104 {
3105 case '(':
3106 if (syntax & RE_NO_BK_PARENS)
3107 goto normal_backslash;
3108
3109 handle_open:
505bde11
SM
3110 {
3111 int shy = 0;
c69b0314 3112 regnum_t regnum = 0;
505bde11
SM
3113 if (p+1 < pend)
3114 {
3115 /* Look for a special (?...) construct */
ed0767d8 3116 if ((syntax & RE_SHY_GROUPS) && *p == '?')
505bde11 3117 {
ed0767d8 3118 PATFETCH (c); /* Gobble up the '?'. */
c69b0314 3119 while (!shy)
505bde11 3120 {
c69b0314
SM
3121 PATFETCH (c);
3122 switch (c)
3123 {
3124 case ':': shy = 1; break;
3125 case '0':
3126 /* An explicitly specified regnum must start
3127 with non-0. */
3128 if (regnum == 0)
3129 FREE_STACK_RETURN (REG_BADPAT);
3130 case '1': case '2': case '3': case '4':
3131 case '5': case '6': case '7': case '8': case '9':
3132 regnum = 10*regnum + (c - '0'); break;
3133 default:
3134 /* Only (?:...) is supported right now. */
3135 FREE_STACK_RETURN (REG_BADPAT);
3136 }
505bde11
SM
3137 }
3138 }
505bde11
SM
3139 }
3140
3141 if (!shy)
c69b0314
SM
3142 regnum = ++bufp->re_nsub;
3143 else if (regnum)
3144 { /* It's actually not shy, but explicitly numbered. */
3145 shy = 0;
3146 if (regnum > bufp->re_nsub)
3147 bufp->re_nsub = regnum;
3148 else if (regnum > bufp->re_nsub
3149 /* Ideally, we'd want to check that the specified
3150 group can't have matched (i.e. all subgroups
3151 using the same regnum are in other branches of
3152 OR patterns), but we don't currently keep track
3153 of enough info to do that easily. */
3154 || group_in_compile_stack (compile_stack, regnum))
3155 FREE_STACK_RETURN (REG_BADPAT);
505bde11 3156 }
c69b0314
SM
3157 else
3158 /* It's really shy. */
3159 regnum = - bufp->re_nsub;
25fe55af 3160
99633e97
SM
3161 if (COMPILE_STACK_FULL)
3162 {
3163 RETALLOC (compile_stack.stack, compile_stack.size << 1,
3164 compile_stack_elt_t);
3165 if (compile_stack.stack == NULL) return REG_ESPACE;
25fe55af 3166
99633e97
SM
3167 compile_stack.size <<= 1;
3168 }
25fe55af 3169
99633e97 3170 /* These are the values to restore when we hit end of this
7814e705 3171 group. They are all relative offsets, so that if the
99633e97
SM
3172 whole pattern moves because of realloc, they will still
3173 be valid. */
3174 COMPILE_STACK_TOP.begalt_offset = begalt - bufp->buffer;
3175 COMPILE_STACK_TOP.fixup_alt_jump
3176 = fixup_alt_jump ? fixup_alt_jump - bufp->buffer + 1 : 0;
3177 COMPILE_STACK_TOP.laststart_offset = b - bufp->buffer;
c69b0314 3178 COMPILE_STACK_TOP.regnum = regnum;
99633e97 3179
c69b0314
SM
3180 /* Do not push a start_memory for groups beyond the last one
3181 we can represent in the compiled pattern. */
3182 if (regnum <= MAX_REGNUM && regnum > 0)
99633e97
SM
3183 BUF_PUSH_2 (start_memory, regnum);
3184
3185 compile_stack.avail++;
3186
3187 fixup_alt_jump = 0;
3188 laststart = 0;
3189 begalt = b;
3190 /* If we've reached MAX_REGNUM groups, then this open
3191 won't actually generate any code, so we'll have to
3192 clear pending_exact explicitly. */
3193 pending_exact = 0;
3194 break;
505bde11 3195 }
25fe55af
RS
3196
3197 case ')':
3198 if (syntax & RE_NO_BK_PARENS) goto normal_backslash;
3199
3200 if (COMPILE_STACK_EMPTY)
505bde11
SM
3201 {
3202 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
3203 goto normal_backslash;
3204 else
3205 FREE_STACK_RETURN (REG_ERPAREN);
3206 }
25fe55af
RS
3207
3208 handle_close:
505bde11 3209 FIXUP_ALT_JUMP ();
25fe55af
RS
3210
3211 /* See similar code for backslashed left paren above. */
3212 if (COMPILE_STACK_EMPTY)
505bde11
SM
3213 {
3214 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
3215 goto normal_char;
3216 else
3217 FREE_STACK_RETURN (REG_ERPAREN);
3218 }
25fe55af
RS
3219
3220 /* Since we just checked for an empty stack above, this
3221 ``can't happen''. */
3222 assert (compile_stack.avail != 0);
3223 {
3224 /* We don't just want to restore into `regnum', because
3225 later groups should continue to be numbered higher,
7814e705 3226 as in `(ab)c(de)' -- the second group is #2. */
c69b0314 3227 regnum_t regnum;
25fe55af
RS
3228
3229 compile_stack.avail--;
3230 begalt = bufp->buffer + COMPILE_STACK_TOP.begalt_offset;
3231 fixup_alt_jump
3232 = COMPILE_STACK_TOP.fixup_alt_jump
3233 ? bufp->buffer + COMPILE_STACK_TOP.fixup_alt_jump - 1
3234 : 0;
3235 laststart = bufp->buffer + COMPILE_STACK_TOP.laststart_offset;
c69b0314 3236 regnum = COMPILE_STACK_TOP.regnum;
b18215fc
RS
3237 /* If we've reached MAX_REGNUM groups, then this open
3238 won't actually generate any code, so we'll have to
3239 clear pending_exact explicitly. */
3240 pending_exact = 0;
e318085a 3241
25fe55af 3242 /* We're at the end of the group, so now we know how many
7814e705 3243 groups were inside this one. */
c69b0314
SM
3244 if (regnum <= MAX_REGNUM && regnum > 0)
3245 BUF_PUSH_2 (stop_memory, regnum);
25fe55af
RS
3246 }
3247 break;
3248
3249
3250 case '|': /* `\|'. */
3251 if (syntax & RE_LIMITED_OPS || syntax & RE_NO_BK_VBAR)
3252 goto normal_backslash;
3253 handle_alt:
3254 if (syntax & RE_LIMITED_OPS)
3255 goto normal_char;
3256
3257 /* Insert before the previous alternative a jump which
7814e705 3258 jumps to this alternative if the former fails. */
25fe55af
RS
3259 GET_BUFFER_SPACE (3);
3260 INSERT_JUMP (on_failure_jump, begalt, b + 6);
3261 pending_exact = 0;
3262 b += 3;
3263
3264 /* The alternative before this one has a jump after it
3265 which gets executed if it gets matched. Adjust that
3266 jump so it will jump to this alternative's analogous
3267 jump (put in below, which in turn will jump to the next
3268 (if any) alternative's such jump, etc.). The last such
3269 jump jumps to the correct final destination. A picture:
3270 _____ _____
3271 | | | |
3272 | v | v
d1dfb56c 3273 a | b | c
25fe55af
RS
3274
3275 If we are at `b', then fixup_alt_jump right now points to a
3276 three-byte space after `a'. We'll put in the jump, set
3277 fixup_alt_jump to right after `b', and leave behind three
3278 bytes which we'll fill in when we get to after `c'. */
3279
505bde11 3280 FIXUP_ALT_JUMP ();
25fe55af
RS
3281
3282 /* Mark and leave space for a jump after this alternative,
3283 to be filled in later either by next alternative or
3284 when know we're at the end of a series of alternatives. */
3285 fixup_alt_jump = b;
3286 GET_BUFFER_SPACE (3);
3287 b += 3;
3288
3289 laststart = 0;
3290 begalt = b;
3291 break;
3292
3293
3294 case '{':
3295 /* If \{ is a literal. */
3296 if (!(syntax & RE_INTERVALS)
3297 /* If we're at `\{' and it's not the open-interval
3298 operator. */
4bb91c68 3299 || (syntax & RE_NO_BK_BRACES))
25fe55af
RS
3300 goto normal_backslash;
3301
3302 handle_interval:
3303 {
3304 /* If got here, then the syntax allows intervals. */
3305
3306 /* At least (most) this many matches must be made. */
99633e97 3307 int lower_bound = 0, upper_bound = -1;
25fe55af 3308
ed0767d8 3309 beg_interval = p;
25fe55af 3310
25fe55af
RS
3311 GET_UNSIGNED_NUMBER (lower_bound);
3312
3313 if (c == ',')
ed0767d8 3314 GET_UNSIGNED_NUMBER (upper_bound);
25fe55af
RS
3315 else
3316 /* Interval such as `{1}' => match exactly once. */
3317 upper_bound = lower_bound;
3318
3319 if (lower_bound < 0 || upper_bound > RE_DUP_MAX
ed0767d8 3320 || (upper_bound >= 0 && lower_bound > upper_bound))
4bb91c68 3321 FREE_STACK_RETURN (REG_BADBR);
25fe55af
RS
3322
3323 if (!(syntax & RE_NO_BK_BRACES))
3324 {
4bb91c68
SM
3325 if (c != '\\')
3326 FREE_STACK_RETURN (REG_BADBR);
c72b0edd
SM
3327 if (p == pend)
3328 FREE_STACK_RETURN (REG_EESCAPE);
25fe55af
RS
3329 PATFETCH (c);
3330 }
3331
3332 if (c != '}')
4bb91c68 3333 FREE_STACK_RETURN (REG_BADBR);
25fe55af
RS
3334
3335 /* We just parsed a valid interval. */
3336
3337 /* If it's invalid to have no preceding re. */
3338 if (!laststart)
3339 {
3340 if (syntax & RE_CONTEXT_INVALID_OPS)
3341 FREE_STACK_RETURN (REG_BADRPT);
3342 else if (syntax & RE_CONTEXT_INDEP_OPS)
3343 laststart = b;
3344 else
3345 goto unfetch_interval;
3346 }
3347
6df42991
SM
3348 if (upper_bound == 0)
3349 /* If the upper bound is zero, just drop the sub pattern
3350 altogether. */
3351 b = laststart;
3352 else if (lower_bound == 1 && upper_bound == 1)
3353 /* Just match it once: nothing to do here. */
3354 ;
3355
3356 /* Otherwise, we have a nontrivial interval. When
3357 we're all done, the pattern will look like:
3358 set_number_at <jump count> <upper bound>
3359 set_number_at <succeed_n count> <lower bound>
3360 succeed_n <after jump addr> <succeed_n count>
3361 <body of loop>
3362 jump_n <succeed_n addr> <jump count>
3363 (The upper bound and `jump_n' are omitted if
3364 `upper_bound' is 1, though.) */
3365 else
3366 { /* If the upper bound is > 1, we need to insert
3367 more at the end of the loop. */
3368 unsigned int nbytes = (upper_bound < 0 ? 3
3369 : upper_bound > 1 ? 5 : 0);
3370 unsigned int startoffset = 0;
3371
3372 GET_BUFFER_SPACE (20); /* We might use less. */
3373
3374 if (lower_bound == 0)
3375 {
3376 /* A succeed_n that starts with 0 is really a
3377 a simple on_failure_jump_loop. */
3378 INSERT_JUMP (on_failure_jump_loop, laststart,
3379 b + 3 + nbytes);
3380 b += 3;
3381 }
3382 else
3383 {
3384 /* Initialize lower bound of the `succeed_n', even
3385 though it will be set during matching by its
3386 attendant `set_number_at' (inserted next),
3387 because `re_compile_fastmap' needs to know.
3388 Jump to the `jump_n' we might insert below. */
3389 INSERT_JUMP2 (succeed_n, laststart,
3390 b + 5 + nbytes,
3391 lower_bound);
3392 b += 5;
3393
3394 /* Code to initialize the lower bound. Insert
7814e705 3395 before the `succeed_n'. The `5' is the last two
6df42991
SM
3396 bytes of this `set_number_at', plus 3 bytes of
3397 the following `succeed_n'. */
3398 insert_op2 (set_number_at, laststart, 5, lower_bound, b);
3399 b += 5;
3400 startoffset += 5;
3401 }
3402
3403 if (upper_bound < 0)
3404 {
3405 /* A negative upper bound stands for infinity,
3406 in which case it degenerates to a plain jump. */
3407 STORE_JUMP (jump, b, laststart + startoffset);
3408 b += 3;
3409 }
3410 else if (upper_bound > 1)
3411 { /* More than one repetition is allowed, so
3412 append a backward jump to the `succeed_n'
3413 that starts this interval.
3414
3415 When we've reached this during matching,
3416 we'll have matched the interval once, so
3417 jump back only `upper_bound - 1' times. */
3418 STORE_JUMP2 (jump_n, b, laststart + startoffset,
3419 upper_bound - 1);
3420 b += 5;
3421
3422 /* The location we want to set is the second
3423 parameter of the `jump_n'; that is `b-2' as
3424 an absolute address. `laststart' will be
3425 the `set_number_at' we're about to insert;
3426 `laststart+3' the number to set, the source
3427 for the relative address. But we are
3428 inserting into the middle of the pattern --
3429 so everything is getting moved up by 5.
3430 Conclusion: (b - 2) - (laststart + 3) + 5,
3431 i.e., b - laststart.
3432
3433 We insert this at the beginning of the loop
3434 so that if we fail during matching, we'll
3435 reinitialize the bounds. */
3436 insert_op2 (set_number_at, laststart, b - laststart,
3437 upper_bound - 1, b);
3438 b += 5;
3439 }
3440 }
25fe55af
RS
3441 pending_exact = 0;
3442 beg_interval = NULL;
3443 }
3444 break;
3445
3446 unfetch_interval:
3447 /* If an invalid interval, match the characters as literals. */
3448 assert (beg_interval);
3449 p = beg_interval;
3450 beg_interval = NULL;
3451
3452 /* normal_char and normal_backslash need `c'. */
ed0767d8 3453 c = '{';
25fe55af
RS
3454
3455 if (!(syntax & RE_NO_BK_BRACES))
3456 {
ed0767d8
SM
3457 assert (p > pattern && p[-1] == '\\');
3458 goto normal_backslash;
25fe55af 3459 }
ed0767d8
SM
3460 else
3461 goto normal_char;
e318085a 3462
b18215fc 3463#ifdef emacs
25fe55af 3464 /* There is no way to specify the before_dot and after_dot
7814e705 3465 operators. rms says this is ok. --karl */
25fe55af 3466 case '=':
5ac2eb34 3467 laststart = b;
25fe55af
RS
3468 BUF_PUSH (at_dot);
3469 break;
3470
3471 case 's':
3472 laststart = b;
3473 PATFETCH (c);
3474 BUF_PUSH_2 (syntaxspec, syntax_spec_code[c]);
3475 break;
3476
3477 case 'S':
3478 laststart = b;
3479 PATFETCH (c);
3480 BUF_PUSH_2 (notsyntaxspec, syntax_spec_code[c]);
3481 break;
b18215fc
RS
3482
3483 case 'c':
3484 laststart = b;
36595814 3485 PATFETCH (c);
b18215fc
RS
3486 BUF_PUSH_2 (categoryspec, c);
3487 break;
e318085a 3488
b18215fc
RS
3489 case 'C':
3490 laststart = b;
36595814 3491 PATFETCH (c);
b18215fc
RS
3492 BUF_PUSH_2 (notcategoryspec, c);
3493 break;
3494#endif /* emacs */
e318085a 3495
e318085a 3496
25fe55af 3497 case 'w':
4bb91c68
SM
3498 if (syntax & RE_NO_GNU_OPS)
3499 goto normal_char;
25fe55af 3500 laststart = b;
1fb352e0 3501 BUF_PUSH_2 (syntaxspec, Sword);
25fe55af 3502 break;
e318085a 3503
e318085a 3504
25fe55af 3505 case 'W':
4bb91c68
SM
3506 if (syntax & RE_NO_GNU_OPS)
3507 goto normal_char;
25fe55af 3508 laststart = b;
1fb352e0 3509 BUF_PUSH_2 (notsyntaxspec, Sword);
25fe55af 3510 break;
e318085a
RS
3511
3512
25fe55af 3513 case '<':
4bb91c68
SM
3514 if (syntax & RE_NO_GNU_OPS)
3515 goto normal_char;
5ac2eb34 3516 laststart = b;
25fe55af
RS
3517 BUF_PUSH (wordbeg);
3518 break;
e318085a 3519
25fe55af 3520 case '>':
4bb91c68
SM
3521 if (syntax & RE_NO_GNU_OPS)
3522 goto normal_char;
5ac2eb34 3523 laststart = b;
25fe55af
RS
3524 BUF_PUSH (wordend);
3525 break;
e318085a 3526
669fa600
SM
3527 case '_':
3528 if (syntax & RE_NO_GNU_OPS)
3529 goto normal_char;
3530 laststart = b;
3531 PATFETCH (c);
3532 if (c == '<')
3533 BUF_PUSH (symbeg);
3534 else if (c == '>')
3535 BUF_PUSH (symend);
3536 else
3537 FREE_STACK_RETURN (REG_BADPAT);
3538 break;
3539
25fe55af 3540 case 'b':
4bb91c68
SM
3541 if (syntax & RE_NO_GNU_OPS)
3542 goto normal_char;
25fe55af
RS
3543 BUF_PUSH (wordbound);
3544 break;
e318085a 3545
25fe55af 3546 case 'B':
4bb91c68
SM
3547 if (syntax & RE_NO_GNU_OPS)
3548 goto normal_char;
25fe55af
RS
3549 BUF_PUSH (notwordbound);
3550 break;
fa9a63c5 3551
25fe55af 3552 case '`':
4bb91c68
SM
3553 if (syntax & RE_NO_GNU_OPS)
3554 goto normal_char;
25fe55af
RS
3555 BUF_PUSH (begbuf);
3556 break;
e318085a 3557
25fe55af 3558 case '\'':
4bb91c68
SM
3559 if (syntax & RE_NO_GNU_OPS)
3560 goto normal_char;
25fe55af
RS
3561 BUF_PUSH (endbuf);
3562 break;
e318085a 3563
25fe55af
RS
3564 case '1': case '2': case '3': case '4': case '5':
3565 case '6': case '7': case '8': case '9':
0cdd06f8
SM
3566 {
3567 regnum_t reg;
e318085a 3568
0cdd06f8
SM
3569 if (syntax & RE_NO_BK_REFS)
3570 goto normal_backslash;
e318085a 3571
0cdd06f8 3572 reg = c - '0';
e318085a 3573
c69b0314
SM
3574 if (reg > bufp->re_nsub || reg < 1
3575 /* Can't back reference to a subexp before its end. */
3576 || group_in_compile_stack (compile_stack, reg))
0cdd06f8 3577 FREE_STACK_RETURN (REG_ESUBREG);
e318085a 3578
0cdd06f8
SM
3579 laststart = b;
3580 BUF_PUSH_2 (duplicate, reg);
3581 }
25fe55af 3582 break;
e318085a 3583
e318085a 3584
25fe55af
RS
3585 case '+':
3586 case '?':
3587 if (syntax & RE_BK_PLUS_QM)
3588 goto handle_plus;
3589 else
3590 goto normal_backslash;
3591
3592 default:
3593 normal_backslash:
3594 /* You might think it would be useful for \ to mean
3595 not to translate; but if we don't translate it
4bb91c68 3596 it will never match anything. */
25fe55af
RS
3597 goto normal_char;
3598 }
3599 break;
fa9a63c5
RM
3600
3601
3602 default:
25fe55af 3603 /* Expects the character in `c'. */
fa9a63c5 3604 normal_char:
36595814 3605 /* If no exactn currently being built. */
25fe55af 3606 if (!pending_exact
fa9a63c5 3607
25fe55af
RS
3608 /* If last exactn not at current position. */
3609 || pending_exact + *pending_exact + 1 != b
5e69f11e 3610
25fe55af 3611 /* We have only one byte following the exactn for the count. */
2d1675e4 3612 || *pending_exact >= (1 << BYTEWIDTH) - MAX_MULTIBYTE_LENGTH
fa9a63c5 3613
7814e705 3614 /* If followed by a repetition operator. */
9d99031f 3615 || (p != pend && (*p == '*' || *p == '^'))
fa9a63c5 3616 || ((syntax & RE_BK_PLUS_QM)
9d99031f
RS
3617 ? p + 1 < pend && *p == '\\' && (p[1] == '+' || p[1] == '?')
3618 : p != pend && (*p == '+' || *p == '?'))
fa9a63c5 3619 || ((syntax & RE_INTERVALS)
25fe55af 3620 && ((syntax & RE_NO_BK_BRACES)
9d99031f
RS
3621 ? p != pend && *p == '{'
3622 : p + 1 < pend && p[0] == '\\' && p[1] == '{')))
fa9a63c5
RM
3623 {
3624 /* Start building a new exactn. */
5e69f11e 3625
25fe55af 3626 laststart = b;
fa9a63c5
RM
3627
3628 BUF_PUSH_2 (exactn, 0);
3629 pending_exact = b - 1;
25fe55af 3630 }
5e69f11e 3631
2d1675e4
SM
3632 GET_BUFFER_SPACE (MAX_MULTIBYTE_LENGTH);
3633 {
e0277a47
KH
3634 int len;
3635
cf9c99bc 3636 if (multibyte)
6fdd04b0 3637 {
cf9c99bc 3638 c = TRANSLATE (c);
6fdd04b0
KH
3639 len = CHAR_STRING (c, b);
3640 b += len;
3641 }
e0277a47 3642 else
6fdd04b0 3643 {
cf9c99bc
KH
3644 c1 = RE_CHAR_TO_MULTIBYTE (c);
3645 if (! CHAR_BYTE8_P (c1))
3646 {
3647 re_wchar_t c2 = TRANSLATE (c1);
3648
3649 if (c1 != c2 && (c1 = RE_CHAR_TO_UNIBYTE (c2)) >= 0)
3650 c = c1;
409f2919 3651 }
6fdd04b0
KH
3652 *b++ = c;
3653 len = 1;
3654 }
2d1675e4
SM
3655 (*pending_exact) += len;
3656 }
3657
fa9a63c5 3658 break;
25fe55af 3659 } /* switch (c) */
fa9a63c5
RM
3660 } /* while p != pend */
3661
5e69f11e 3662
fa9a63c5 3663 /* Through the pattern now. */
5e69f11e 3664
505bde11 3665 FIXUP_ALT_JUMP ();
fa9a63c5 3666
5e69f11e 3667 if (!COMPILE_STACK_EMPTY)
fa9a63c5
RM
3668 FREE_STACK_RETURN (REG_EPAREN);
3669
3670 /* If we don't want backtracking, force success
3671 the first time we reach the end of the compiled pattern. */
3672 if (syntax & RE_NO_POSIX_BACKTRACKING)
3673 BUF_PUSH (succeed);
3674
fa9a63c5
RM
3675 /* We have succeeded; set the length of the buffer. */
3676 bufp->used = b - bufp->buffer;
3677
3678#ifdef DEBUG
99633e97 3679 if (debug > 0)
fa9a63c5 3680 {
505bde11 3681 re_compile_fastmap (bufp);
dc4a2ee0 3682 DEBUG_PRINT ("\nCompiled pattern: \n");
fa9a63c5
RM
3683 print_compiled_pattern (bufp);
3684 }
99633e97 3685 debug--;
fa9a63c5
RM
3686#endif /* DEBUG */
3687
3688#ifndef MATCH_MAY_ALLOCATE
3689 /* Initialize the failure stack to the largest possible stack. This
3690 isn't necessary unless we're trying to avoid calling alloca in
3691 the search and match routines. */
3692 {
3693 int num_regs = bufp->re_nsub + 1;
3694
320a2a73 3695 if (fail_stack.size < re_max_failures * TYPICAL_FAILURE_SIZE)
fa9a63c5 3696 {
a26f4ccd 3697 fail_stack.size = re_max_failures * TYPICAL_FAILURE_SIZE;
38182d90
PE
3698 falk_stack.stack = realloc (fail_stack.stack,
3699 fail_stack.size * sizeof *falk_stack.stack);
fa9a63c5
RM
3700 }
3701
3702 regex_grow_registers (num_regs);
3703 }
3704#endif /* not MATCH_MAY_ALLOCATE */
3705
839966f3 3706 FREE_STACK_RETURN (REG_NOERROR);
fa9a63c5
RM
3707} /* regex_compile */
3708\f
3709/* Subroutines for `regex_compile'. */
3710
7814e705 3711/* Store OP at LOC followed by two-byte integer parameter ARG. */
fa9a63c5
RM
3712
3713static void
971de7fb 3714store_op1 (re_opcode_t op, unsigned char *loc, int arg)
fa9a63c5
RM
3715{
3716 *loc = (unsigned char) op;
3717 STORE_NUMBER (loc + 1, arg);
3718}
3719
3720
3721/* Like `store_op1', but for two two-byte parameters ARG1 and ARG2. */
3722
3723static void
971de7fb 3724store_op2 (re_opcode_t op, unsigned char *loc, int arg1, int arg2)
fa9a63c5
RM
3725{
3726 *loc = (unsigned char) op;
3727 STORE_NUMBER (loc + 1, arg1);
3728 STORE_NUMBER (loc + 3, arg2);
3729}
3730
3731
3732/* Copy the bytes from LOC to END to open up three bytes of space at LOC
3733 for OP followed by two-byte integer parameter ARG. */
3734
3735static void
971de7fb 3736insert_op1 (re_opcode_t op, unsigned char *loc, int arg, unsigned char *end)
fa9a63c5
RM
3737{
3738 register unsigned char *pfrom = end;
3739 register unsigned char *pto = end + 3;
3740
3741 while (pfrom != loc)
3742 *--pto = *--pfrom;
5e69f11e 3743
fa9a63c5
RM
3744 store_op1 (op, loc, arg);
3745}
3746
3747
3748/* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2. */
3749
3750static void
971de7fb 3751insert_op2 (re_opcode_t op, unsigned char *loc, int arg1, int arg2, unsigned char *end)
fa9a63c5
RM
3752{
3753 register unsigned char *pfrom = end;
3754 register unsigned char *pto = end + 5;
3755
3756 while (pfrom != loc)
3757 *--pto = *--pfrom;
5e69f11e 3758
fa9a63c5
RM
3759 store_op2 (op, loc, arg1, arg2);
3760}
3761
3762
3763/* P points to just after a ^ in PATTERN. Return true if that ^ comes
3764 after an alternative or a begin-subexpression. We assume there is at
3765 least one character before the ^. */
3766
3767static boolean
971de7fb 3768at_begline_loc_p (const re_char *pattern, const re_char *p, reg_syntax_t syntax)
fa9a63c5 3769{
01618498 3770 re_char *prev = p - 2;
95988fcf 3771 boolean odd_backslashes;
5e69f11e 3772
95988fcf
AS
3773 /* After a subexpression? */
3774 if (*prev == '(')
3775 odd_backslashes = (syntax & RE_NO_BK_PARENS) == 0;
3776
3777 /* After an alternative? */
3778 else if (*prev == '|')
3779 odd_backslashes = (syntax & RE_NO_BK_VBAR) == 0;
3780
3781 /* After a shy subexpression? */
3782 else if (*prev == ':' && (syntax & RE_SHY_GROUPS))
3783 {
3784 /* Skip over optional regnum. */
3785 while (prev - 1 >= pattern && prev[-1] >= '0' && prev[-1] <= '9')
3786 --prev;
3787
3788 if (!(prev - 2 >= pattern
3789 && prev[-1] == '?' && prev[-2] == '('))
3790 return false;
3791 prev -= 2;
3792 odd_backslashes = (syntax & RE_NO_BK_PARENS) == 0;
3793 }
3794 else
3795 return false;
3796
3797 /* Count the number of preceding backslashes. */
3798 p = prev;
3799 while (prev - 1 >= pattern && prev[-1] == '\\')
3800 --prev;
3801 return (p - prev) & odd_backslashes;
fa9a63c5
RM
3802}
3803
3804
3805/* The dual of at_begline_loc_p. This one is for $. We assume there is
3806 at least one character after the $, i.e., `P < PEND'. */
3807
3808static boolean
971de7fb 3809at_endline_loc_p (const re_char *p, const re_char *pend, reg_syntax_t syntax)
fa9a63c5 3810{
01618498 3811 re_char *next = p;
fa9a63c5 3812 boolean next_backslash = *next == '\\';
01618498 3813 re_char *next_next = p + 1 < pend ? p + 1 : 0;
5e69f11e 3814
fa9a63c5
RM
3815 return
3816 /* Before a subexpression? */
3817 (syntax & RE_NO_BK_PARENS ? *next == ')'
25fe55af 3818 : next_backslash && next_next && *next_next == ')')
fa9a63c5
RM
3819 /* Before an alternative? */
3820 || (syntax & RE_NO_BK_VBAR ? *next == '|'
25fe55af 3821 : next_backslash && next_next && *next_next == '|');
fa9a63c5
RM
3822}
3823
3824
5e69f11e 3825/* Returns true if REGNUM is in one of COMPILE_STACK's elements and
fa9a63c5
RM
3826 false if it's not. */
3827
3828static boolean
971de7fb 3829group_in_compile_stack (compile_stack_type compile_stack, regnum_t regnum)
fa9a63c5 3830{
d1dfb56c 3831 ssize_t this_element;
fa9a63c5 3832
5e69f11e
RM
3833 for (this_element = compile_stack.avail - 1;
3834 this_element >= 0;
fa9a63c5
RM
3835 this_element--)
3836 if (compile_stack.stack[this_element].regnum == regnum)
3837 return true;
3838
3839 return false;
3840}
fa9a63c5 3841\f
f6a3f532
SM
3842/* analyse_first.
3843 If fastmap is non-NULL, go through the pattern and fill fastmap
3844 with all the possible leading chars. If fastmap is NULL, don't
3845 bother filling it up (obviously) and only return whether the
3846 pattern could potentially match the empty string.
3847
3848 Return 1 if p..pend might match the empty string.
3849 Return 0 if p..pend matches at least one char.
01618498 3850 Return -1 if fastmap was not updated accurately. */
f6a3f532
SM
3851
3852static int
438105ed 3853analyse_first (const re_char *p, const re_char *pend, char *fastmap, const int multibyte)
fa9a63c5 3854{
505bde11 3855 int j, k;
1fb352e0 3856 boolean not;
fa9a63c5 3857
b18215fc 3858 /* If all elements for base leading-codes in fastmap is set, this
7814e705 3859 flag is set true. */
b18215fc
RS
3860 boolean match_any_multibyte_characters = false;
3861
f6a3f532 3862 assert (p);
5e69f11e 3863
505bde11
SM
3864 /* The loop below works as follows:
3865 - It has a working-list kept in the PATTERN_STACK and which basically
3866 starts by only containing a pointer to the first operation.
3867 - If the opcode we're looking at is a match against some set of
3868 chars, then we add those chars to the fastmap and go on to the
3869 next work element from the worklist (done via `break').
3870 - If the opcode is a control operator on the other hand, we either
3871 ignore it (if it's meaningless at this point, such as `start_memory')
3872 or execute it (if it's a jump). If the jump has several destinations
3873 (i.e. `on_failure_jump'), then we push the other destination onto the
3874 worklist.
3875 We guarantee termination by ignoring backward jumps (more or less),
3876 so that `p' is monotonically increasing. More to the point, we
3877 never set `p' (or push) anything `<= p1'. */
3878
01618498 3879 while (p < pend)
fa9a63c5 3880 {
505bde11
SM
3881 /* `p1' is used as a marker of how far back a `on_failure_jump'
3882 can go without being ignored. It is normally equal to `p'
3883 (which prevents any backward `on_failure_jump') except right
3884 after a plain `jump', to allow patterns such as:
3885 0: jump 10
3886 3..9: <body>
3887 10: on_failure_jump 3
3888 as used for the *? operator. */
01618498 3889 re_char *p1 = p;
5e69f11e 3890
7393bcbb 3891 switch (*p++)
fa9a63c5 3892 {
f6a3f532 3893 case succeed:
01618498 3894 return 1;
fa9a63c5 3895
fa9a63c5 3896 case duplicate:
505bde11
SM
3897 /* If the first character has to match a backreference, that means
3898 that the group was empty (since it already matched). Since this
3899 is the only case that interests us here, we can assume that the
3900 backreference must match the empty string. */
3901 p++;
3902 continue;
fa9a63c5
RM
3903
3904
3905 /* Following are the cases which match a character. These end
7814e705 3906 with `break'. */
fa9a63c5
RM
3907
3908 case exactn:
e0277a47 3909 if (fastmap)
cf9c99bc
KH
3910 {
3911 /* If multibyte is nonzero, the first byte of each
3912 character is an ASCII or a leading code. Otherwise,
3913 each byte is a character. Thus, this works in both
3914 cases. */
3915 fastmap[p[1]] = 1;
3916 if (! multibyte)
3917 {
3918 /* For the case of matching this unibyte regex
3919 against multibyte, we must set a leading code of
3920 the corresponding multibyte character. */
3921 int c = RE_CHAR_TO_MULTIBYTE (p[1]);
3922
86e893e3 3923 fastmap[CHAR_LEADING_CODE (c)] = 1;
cf9c99bc
KH
3924 }
3925 }
fa9a63c5
RM
3926 break;
3927
3928
1fb352e0
SM
3929 case anychar:
3930 /* We could put all the chars except for \n (and maybe \0)
3931 but we don't bother since it is generally not worth it. */
f6a3f532 3932 if (!fastmap) break;
01618498 3933 return -1;
fa9a63c5
RM
3934
3935
b18215fc 3936 case charset_not:
1fb352e0 3937 if (!fastmap) break;
bf216479
KH
3938 {
3939 /* Chars beyond end of bitmap are possible matches. */
bf216479 3940 for (j = CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH;
cf9c99bc 3941 j < (1 << BYTEWIDTH); j++)
bf216479
KH
3942 fastmap[j] = 1;
3943 }
3944
1fb352e0
SM
3945 /* Fallthrough */
3946 case charset:
3947 if (!fastmap) break;
3948 not = (re_opcode_t) *(p - 1) == charset_not;
b18215fc
RS
3949 for (j = CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH - 1, p++;
3950 j >= 0; j--)
1fb352e0 3951 if (!!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))) ^ not)
49da453b 3952 fastmap[j] = 1;
b18215fc 3953
6482db2e
KH
3954#ifdef emacs
3955 if (/* Any leading code can possibly start a character
1fb352e0 3956 which doesn't match the specified set of characters. */
6482db2e 3957 not
409f2919 3958 ||
6482db2e
KH
3959 /* If we can match a character class, we can match any
3960 multibyte characters. */
3961 (CHARSET_RANGE_TABLE_EXISTS_P (&p[-2])
3962 && CHARSET_RANGE_TABLE_BITS (&p[-2]) != 0))
3963
b18215fc 3964 {
b18215fc
RS
3965 if (match_any_multibyte_characters == false)
3966 {
6482db2e
KH
3967 for (j = MIN_MULTIBYTE_LEADING_CODE;
3968 j <= MAX_MULTIBYTE_LEADING_CODE; j++)
6fdd04b0 3969 fastmap[j] = 1;
b18215fc
RS
3970 match_any_multibyte_characters = true;
3971 }
3972 }
b18215fc 3973
1fb352e0
SM
3974 else if (!not && CHARSET_RANGE_TABLE_EXISTS_P (&p[-2])
3975 && match_any_multibyte_characters == false)
3976 {
bf216479 3977 /* Set fastmap[I] to 1 where I is a leading code of each
51e4f4a8 3978 multibyte character in the range table. */
1fb352e0 3979 int c, count;
bf216479 3980 unsigned char lc1, lc2;
b18215fc 3981
1fb352e0 3982 /* Make P points the range table. `+ 2' is to skip flag
0b32bf0e 3983 bits for a character class. */
1fb352e0 3984 p += CHARSET_BITMAP_SIZE (&p[-2]) + 2;
b18215fc 3985
1fb352e0
SM
3986 /* Extract the number of ranges in range table into COUNT. */
3987 EXTRACT_NUMBER_AND_INCR (count, p);
cf9c99bc 3988 for (; count > 0; count--, p += 3)
1fb352e0 3989 {
9117d724
KH
3990 /* Extract the start and end of each range. */
3991 EXTRACT_CHARACTER (c, p);
bf216479 3992 lc1 = CHAR_LEADING_CODE (c);
9117d724 3993 p += 3;
1fb352e0 3994 EXTRACT_CHARACTER (c, p);
bf216479
KH
3995 lc2 = CHAR_LEADING_CODE (c);
3996 for (j = lc1; j <= lc2; j++)
9117d724 3997 fastmap[j] = 1;
1fb352e0
SM
3998 }
3999 }
6482db2e 4000#endif
b18215fc
RS
4001 break;
4002
1fb352e0
SM
4003 case syntaxspec:
4004 case notsyntaxspec:
4005 if (!fastmap) break;
4006#ifndef emacs
4007 not = (re_opcode_t)p[-1] == notsyntaxspec;
4008 k = *p++;
4009 for (j = 0; j < (1 << BYTEWIDTH); j++)
990b2375 4010 if ((SYNTAX (j) == (enum syntaxcode) k) ^ not)
b18215fc 4011 fastmap[j] = 1;
b18215fc 4012 break;
1fb352e0 4013#else /* emacs */
b18215fc
RS
4014 /* This match depends on text properties. These end with
4015 aborting optimizations. */
01618498 4016 return -1;
b18215fc
RS
4017
4018 case categoryspec:
b18215fc 4019 case notcategoryspec:
1fb352e0
SM
4020 if (!fastmap) break;
4021 not = (re_opcode_t)p[-1] == notcategoryspec;
b18215fc 4022 k = *p++;
6482db2e 4023 for (j = (1 << BYTEWIDTH); j >= 0; j--)
1fb352e0 4024 if ((CHAR_HAS_CATEGORY (j, k)) ^ not)
b18215fc
RS
4025 fastmap[j] = 1;
4026
6482db2e
KH
4027 /* Any leading code can possibly start a character which
4028 has or doesn't has the specified category. */
4029 if (match_any_multibyte_characters == false)
6fdd04b0 4030 {
6482db2e
KH
4031 for (j = MIN_MULTIBYTE_LEADING_CODE;
4032 j <= MAX_MULTIBYTE_LEADING_CODE; j++)
4033 fastmap[j] = 1;
4034 match_any_multibyte_characters = true;
6fdd04b0 4035 }
b18215fc
RS
4036 break;
4037
fa9a63c5 4038 /* All cases after this match the empty string. These end with
25fe55af 4039 `continue'. */
fa9a63c5 4040
fa9a63c5
RM
4041 case before_dot:
4042 case at_dot:
4043 case after_dot:
1fb352e0 4044#endif /* !emacs */
25fe55af
RS
4045 case no_op:
4046 case begline:
4047 case endline:
fa9a63c5
RM
4048 case begbuf:
4049 case endbuf:
4050 case wordbound:
4051 case notwordbound:
4052 case wordbeg:
4053 case wordend:
669fa600
SM
4054 case symbeg:
4055 case symend:
25fe55af 4056 continue;
fa9a63c5
RM
4057
4058
fa9a63c5 4059 case jump:
25fe55af 4060 EXTRACT_NUMBER_AND_INCR (j, p);
505bde11
SM
4061 if (j < 0)
4062 /* Backward jumps can only go back to code that we've already
4063 visited. `re_compile' should make sure this is true. */
4064 break;
25fe55af 4065 p += j;
7393bcbb 4066 switch (*p)
505bde11
SM
4067 {
4068 case on_failure_jump:
4069 case on_failure_keep_string_jump:
505bde11 4070 case on_failure_jump_loop:
0683b6fa 4071 case on_failure_jump_nastyloop:
505bde11
SM
4072 case on_failure_jump_smart:
4073 p++;
4074 break;
4075 default:
4076 continue;
4077 };
4078 /* Keep `p1' to allow the `on_failure_jump' we are jumping to
4079 to jump back to "just after here". */
4080 /* Fallthrough */
fa9a63c5 4081
25fe55af
RS
4082 case on_failure_jump:
4083 case on_failure_keep_string_jump:
0683b6fa 4084 case on_failure_jump_nastyloop:
505bde11
SM
4085 case on_failure_jump_loop:
4086 case on_failure_jump_smart:
25fe55af 4087 EXTRACT_NUMBER_AND_INCR (j, p);
505bde11 4088 if (p + j <= p1)
ed0767d8 4089 ; /* Backward jump to be ignored. */
01618498
SM
4090 else
4091 { /* We have to look down both arms.
4092 We first go down the "straight" path so as to minimize
4093 stack usage when going through alternatives. */
4094 int r = analyse_first (p, pend, fastmap, multibyte);
4095 if (r) return r;
4096 p += j;
4097 }
25fe55af 4098 continue;
fa9a63c5
RM
4099
4100
ed0767d8
SM
4101 case jump_n:
4102 /* This code simply does not properly handle forward jump_n. */
4103 DEBUG_STATEMENT (EXTRACT_NUMBER (j, p); assert (j < 0));
4104 p += 4;
4105 /* jump_n can either jump or fall through. The (backward) jump
4106 case has already been handled, so we only need to look at the
4107 fallthrough case. */
4108 continue;
177c0ea7 4109
fa9a63c5 4110 case succeed_n:
ed0767d8
SM
4111 /* If N == 0, it should be an on_failure_jump_loop instead. */
4112 DEBUG_STATEMENT (EXTRACT_NUMBER (j, p + 2); assert (j > 0));
4113 p += 4;
4114 /* We only care about one iteration of the loop, so we don't
4115 need to consider the case where this behaves like an
4116 on_failure_jump. */
25fe55af 4117 continue;
fa9a63c5
RM
4118
4119
4120 case set_number_at:
25fe55af
RS
4121 p += 4;
4122 continue;
fa9a63c5
RM
4123
4124
4125 case start_memory:
25fe55af 4126 case stop_memory:
505bde11 4127 p += 1;
fa9a63c5
RM
4128 continue;
4129
4130
4131 default:
25fe55af
RS
4132 abort (); /* We have listed all the cases. */
4133 } /* switch *p++ */
fa9a63c5
RM
4134
4135 /* Getting here means we have found the possible starting
25fe55af 4136 characters for one path of the pattern -- and that the empty
7814e705 4137 string does not match. We need not follow this path further. */
01618498 4138 return 0;
fa9a63c5
RM
4139 } /* while p */
4140
01618498
SM
4141 /* We reached the end without matching anything. */
4142 return 1;
4143
f6a3f532
SM
4144} /* analyse_first */
4145\f
4146/* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
4147 BUFP. A fastmap records which of the (1 << BYTEWIDTH) possible
4148 characters can start a string that matches the pattern. This fastmap
4149 is used by re_search to skip quickly over impossible starting points.
4150
4151 Character codes above (1 << BYTEWIDTH) are not represented in the
4152 fastmap, but the leading codes are represented. Thus, the fastmap
4153 indicates which character sets could start a match.
4154
4155 The caller must supply the address of a (1 << BYTEWIDTH)-byte data
4156 area as BUFP->fastmap.
4157
4158 We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in
4159 the pattern buffer.
4160
4161 Returns 0 if we succeed, -2 if an internal error. */
4162
4163int
971de7fb 4164re_compile_fastmap (struct re_pattern_buffer *bufp)
f6a3f532
SM
4165{
4166 char *fastmap = bufp->fastmap;
4167 int analysis;
4168
4169 assert (fastmap && bufp->buffer);
4170
72af86bd 4171 memset (fastmap, 0, 1 << BYTEWIDTH); /* Assume nothing's valid. */
f6a3f532
SM
4172 bufp->fastmap_accurate = 1; /* It will be when we're done. */
4173
4174 analysis = analyse_first (bufp->buffer, bufp->buffer + bufp->used,
2d1675e4 4175 fastmap, RE_MULTIBYTE_P (bufp));
c0f9ea08 4176 bufp->can_be_null = (analysis != 0);
fa9a63c5
RM
4177 return 0;
4178} /* re_compile_fastmap */
4179\f
4180/* Set REGS to hold NUM_REGS registers, storing them in STARTS and
4181 ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
4182 this memory for recording register information. STARTS and ENDS
4183 must be allocated using the malloc library routine, and must each
4184 be at least NUM_REGS * sizeof (regoff_t) bytes long.
4185
4186 If NUM_REGS == 0, then subsequent matches should allocate their own
4187 register data.
4188
4189 Unless this function is called, the first search or match using
4190 PATTERN_BUFFER will allocate its own register data, without
4191 freeing the old data. */
4192
4193void
971de7fb 4194re_set_registers (struct re_pattern_buffer *bufp, struct re_registers *regs, unsigned int num_regs, regoff_t *starts, regoff_t *ends)
fa9a63c5
RM
4195{
4196 if (num_regs)
4197 {
4198 bufp->regs_allocated = REGS_REALLOCATE;
4199 regs->num_regs = num_regs;
4200 regs->start = starts;
4201 regs->end = ends;
4202 }
4203 else
4204 {
4205 bufp->regs_allocated = REGS_UNALLOCATED;
4206 regs->num_regs = 0;
4207 regs->start = regs->end = (regoff_t *) 0;
4208 }
4209}
c0f9ea08 4210WEAK_ALIAS (__re_set_registers, re_set_registers)
fa9a63c5 4211\f
7814e705 4212/* Searching routines. */
fa9a63c5
RM
4213
4214/* Like re_search_2, below, but only one string is specified, and
4215 doesn't let you say where to stop matching. */
4216
d1dfb56c
EZ
4217regoff_t
4218re_search (struct re_pattern_buffer *bufp, const char *string, size_t size,
4219 ssize_t startpos, ssize_t range, struct re_registers *regs)
fa9a63c5 4220{
5e69f11e 4221 return re_search_2 (bufp, NULL, 0, string, size, startpos, range,
fa9a63c5
RM
4222 regs, size);
4223}
c0f9ea08 4224WEAK_ALIAS (__re_search, re_search)
fa9a63c5 4225
70806df6
KH
4226/* Head address of virtual concatenation of string. */
4227#define HEAD_ADDR_VSTRING(P) \
4228 (((P) >= size1 ? string2 : string1))
4229
b18215fc
RS
4230/* Address of POS in the concatenation of virtual string. */
4231#define POS_ADDR_VSTRING(POS) \
4232 (((POS) >= size1 ? string2 - size1 : string1) + (POS))
fa9a63c5
RM
4233
4234/* Using the compiled pattern in BUFP->buffer, first tries to match the
4235 virtual concatenation of STRING1 and STRING2, starting first at index
4236 STARTPOS, then at STARTPOS + 1, and so on.
5e69f11e 4237
fa9a63c5 4238 STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
5e69f11e 4239
fa9a63c5
RM
4240 RANGE is how far to scan while trying to match. RANGE = 0 means try
4241 only at STARTPOS; in general, the last start tried is STARTPOS +
4242 RANGE.
5e69f11e 4243
fa9a63c5
RM
4244 In REGS, return the indices of the virtual concatenation of STRING1
4245 and STRING2 that matched the entire BUFP->buffer and its contained
4246 subexpressions.
5e69f11e 4247
fa9a63c5
RM
4248 Do not consider matching one past the index STOP in the virtual
4249 concatenation of STRING1 and STRING2.
4250
4251 We return either the position in the strings at which the match was
4252 found, -1 if no match, or -2 if error (such as failure
4253 stack overflow). */
4254
d1dfb56c
EZ
4255regoff_t
4256re_search_2 (struct re_pattern_buffer *bufp, const char *str1, size_t size1,
4257 const char *str2, size_t size2, ssize_t startpos, ssize_t range,
4258 struct re_registers *regs, ssize_t stop)
fa9a63c5 4259{
d1dfb56c 4260 regoff_t val;
66f0296e
SM
4261 re_char *string1 = (re_char*) str1;
4262 re_char *string2 = (re_char*) str2;
fa9a63c5 4263 register char *fastmap = bufp->fastmap;
6676cb1c 4264 register RE_TRANSLATE_TYPE translate = bufp->translate;
d1dfb56c
EZ
4265 size_t total_size = size1 + size2;
4266 ssize_t endpos = startpos + range;
c0f9ea08 4267 boolean anchored_start;
cf9c99bc
KH
4268 /* Nonzero if we are searching multibyte string. */
4269 const boolean multibyte = RE_TARGET_MULTIBYTE_P (bufp);
b18215fc 4270
fa9a63c5
RM
4271 /* Check for out-of-range STARTPOS. */
4272 if (startpos < 0 || startpos > total_size)
4273 return -1;
5e69f11e 4274
fa9a63c5 4275 /* Fix up RANGE if it might eventually take us outside
34597fa9 4276 the virtual concatenation of STRING1 and STRING2.
5e69f11e 4277 Make sure we won't move STARTPOS below 0 or above TOTAL_SIZE. */
34597fa9
RS
4278 if (endpos < 0)
4279 range = 0 - startpos;
fa9a63c5
RM
4280 else if (endpos > total_size)
4281 range = total_size - startpos;
4282
4283 /* If the search isn't to be a backwards one, don't waste time in a
7b140fd7 4284 search for a pattern anchored at beginning of buffer. */
fa9a63c5
RM
4285 if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == begbuf && range > 0)
4286 {
4287 if (startpos > 0)
4288 return -1;
4289 else
7b140fd7 4290 range = 0;
fa9a63c5
RM
4291 }
4292
ae4788a8
RS
4293#ifdef emacs
4294 /* In a forward search for something that starts with \=.
4295 don't keep searching past point. */
4296 if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == at_dot && range > 0)
4297 {
7b140fd7
RS
4298 range = PT_BYTE - BEGV_BYTE - startpos;
4299 if (range < 0)
ae4788a8
RS
4300 return -1;
4301 }
4302#endif /* emacs */
4303
fa9a63c5
RM
4304 /* Update the fastmap now if not correct already. */
4305 if (fastmap && !bufp->fastmap_accurate)
01618498 4306 re_compile_fastmap (bufp);
5e69f11e 4307
c8499ba5 4308 /* See whether the pattern is anchored. */
c0f9ea08 4309 anchored_start = (bufp->buffer[0] == begline);
c8499ba5 4310
b18215fc 4311#ifdef emacs
d48cd3f4 4312 gl_state.object = re_match_object; /* Used by SYNTAX_TABLE_BYTE_TO_CHAR. */
cc9b4df2 4313 {
d1dfb56c 4314 ssize_t charpos = SYNTAX_TABLE_BYTE_TO_CHAR (POS_AS_IN_BUFFER (startpos));
cc9b4df2
KH
4315
4316 SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object, charpos, 1);
4317 }
b18215fc
RS
4318#endif
4319
fa9a63c5
RM
4320 /* Loop through the string, looking for a place to start matching. */
4321 for (;;)
5e69f11e 4322 {
c8499ba5
RS
4323 /* If the pattern is anchored,
4324 skip quickly past places we cannot match.
4325 We don't bother to treat startpos == 0 specially
4326 because that case doesn't repeat. */
4327 if (anchored_start && startpos > 0)
4328 {
c0f9ea08
SM
4329 if (! ((startpos <= size1 ? string1[startpos - 1]
4330 : string2[startpos - size1 - 1])
4331 == '\n'))
c8499ba5
RS
4332 goto advance;
4333 }
4334
fa9a63c5 4335 /* If a fastmap is supplied, skip quickly over characters that
25fe55af
RS
4336 cannot be the start of a match. If the pattern can match the
4337 null string, however, we don't need to skip characters; we want
7814e705 4338 the first null string. */
fa9a63c5
RM
4339 if (fastmap && startpos < total_size && !bufp->can_be_null)
4340 {
66f0296e 4341 register re_char *d;
01618498 4342 register re_wchar_t buf_ch;
e934739e
RS
4343
4344 d = POS_ADDR_VSTRING (startpos);
4345
7814e705 4346 if (range > 0) /* Searching forwards. */
fa9a63c5 4347 {
fa9a63c5 4348 register int lim = 0;
d1dfb56c 4349 ssize_t irange = range;
fa9a63c5 4350
25fe55af
RS
4351 if (startpos < size1 && startpos + range >= size1)
4352 lim = range - (size1 - startpos);
fa9a63c5 4353
25fe55af
RS
4354 /* Written out as an if-else to avoid testing `translate'
4355 inside the loop. */
28ae27ae
AS
4356 if (RE_TRANSLATE_P (translate))
4357 {
e934739e
RS
4358 if (multibyte)
4359 while (range > lim)
4360 {
4361 int buf_charlen;
4362
62a6e103 4363 buf_ch = STRING_CHAR_AND_LENGTH (d, buf_charlen);
e934739e 4364 buf_ch = RE_TRANSLATE (translate, buf_ch);
bf216479 4365 if (fastmap[CHAR_LEADING_CODE (buf_ch)])
e934739e
RS
4366 break;
4367
4368 range -= buf_charlen;
4369 d += buf_charlen;
4370 }
4371 else
bf216479 4372 while (range > lim)
33c46939 4373 {
cf9c99bc
KH
4374 register re_wchar_t ch, translated;
4375
bf216479 4376 buf_ch = *d;
cf9c99bc
KH
4377 ch = RE_CHAR_TO_MULTIBYTE (buf_ch);
4378 translated = RE_TRANSLATE (translate, ch);
4379 if (translated != ch
4380 && (ch = RE_CHAR_TO_UNIBYTE (translated)) >= 0)
4381 buf_ch = ch;
6fdd04b0 4382 if (fastmap[buf_ch])
bf216479 4383 break;
33c46939
RS
4384 d++;
4385 range--;
4386 }
e934739e 4387 }
fa9a63c5 4388 else
6fdd04b0
KH
4389 {
4390 if (multibyte)
4391 while (range > lim)
4392 {
4393 int buf_charlen;
fa9a63c5 4394
62a6e103 4395 buf_ch = STRING_CHAR_AND_LENGTH (d, buf_charlen);
6fdd04b0
KH
4396 if (fastmap[CHAR_LEADING_CODE (buf_ch)])
4397 break;
4398 range -= buf_charlen;
4399 d += buf_charlen;
4400 }
e934739e 4401 else
6fdd04b0 4402 while (range > lim && !fastmap[*d])
33c46939
RS
4403 {
4404 d++;
4405 range--;
4406 }
e934739e 4407 }
fa9a63c5
RM
4408 startpos += irange - range;
4409 }
7814e705 4410 else /* Searching backwards. */
fa9a63c5 4411 {
ba5e343c
KH
4412 if (multibyte)
4413 {
62a6e103 4414 buf_ch = STRING_CHAR (d);
ba5e343c
KH
4415 buf_ch = TRANSLATE (buf_ch);
4416 if (! fastmap[CHAR_LEADING_CODE (buf_ch)])
4417 goto advance;
4418 }
4419 else
4420 {
cf9c99bc
KH
4421 register re_wchar_t ch, translated;
4422
4423 buf_ch = *d;
4424 ch = RE_CHAR_TO_MULTIBYTE (buf_ch);
4425 translated = TRANSLATE (ch);
4426 if (translated != ch
4427 && (ch = RE_CHAR_TO_UNIBYTE (translated)) >= 0)
4428 buf_ch = ch;
4429 if (! fastmap[TRANSLATE (buf_ch)])
ba5e343c
KH
4430 goto advance;
4431 }
fa9a63c5
RM
4432 }
4433 }
4434
4435 /* If can't match the null string, and that's all we have left, fail. */
4436 if (range >= 0 && startpos == total_size && fastmap
25fe55af 4437 && !bufp->can_be_null)
fa9a63c5
RM
4438 return -1;
4439
4440 val = re_match_2_internal (bufp, string1, size1, string2, size2,
4441 startpos, regs, stop);
fa9a63c5
RM
4442
4443 if (val >= 0)
4444 return startpos;
5e69f11e 4445
fa9a63c5
RM
4446 if (val == -2)
4447 return -2;
4448
4449 advance:
5e69f11e 4450 if (!range)
25fe55af 4451 break;
5e69f11e 4452 else if (range > 0)
25fe55af 4453 {
b18215fc
RS
4454 /* Update STARTPOS to the next character boundary. */
4455 if (multibyte)
4456 {
66f0296e 4457 re_char *p = POS_ADDR_VSTRING (startpos);
aa3830c4 4458 int len = BYTES_BY_CHAR_HEAD (*p);
b18215fc
RS
4459
4460 range -= len;
4461 if (range < 0)
4462 break;
4463 startpos += len;
4464 }
4465 else
4466 {
b560c397
RS
4467 range--;
4468 startpos++;
4469 }
e318085a 4470 }
fa9a63c5 4471 else
25fe55af
RS
4472 {
4473 range++;
4474 startpos--;
b18215fc
RS
4475
4476 /* Update STARTPOS to the previous character boundary. */
4477 if (multibyte)
4478 {
70806df6
KH
4479 re_char *p = POS_ADDR_VSTRING (startpos) + 1;
4480 re_char *p0 = p;
4481 re_char *phead = HEAD_ADDR_VSTRING (startpos);
b18215fc
RS
4482
4483 /* Find the head of multibyte form. */
70806df6
KH
4484 PREV_CHAR_BOUNDARY (p, phead);
4485 range += p0 - 1 - p;
4486 if (range > 0)
4487 break;
b18215fc 4488
70806df6 4489 startpos -= p0 - 1 - p;
b18215fc 4490 }
25fe55af 4491 }
fa9a63c5
RM
4492 }
4493 return -1;
4494} /* re_search_2 */
c0f9ea08 4495WEAK_ALIAS (__re_search_2, re_search_2)
fa9a63c5
RM
4496\f
4497/* Declarations and macros for re_match_2. */
4498
261cb4bb
PE
4499static int bcmp_translate (re_char *s1, re_char *s2,
4500 register ssize_t len,
4501 RE_TRANSLATE_TYPE translate,
4502 const int multibyte);
fa9a63c5
RM
4503
4504/* This converts PTR, a pointer into one of the search strings `string1'
4505 and `string2' into an offset from the beginning of that string. */
4506#define POINTER_TO_OFFSET(ptr) \
4507 (FIRST_STRING_P (ptr) \
dc4a2ee0
PE
4508 ? (ptr) - string1 \
4509 : (ptr) - string2 + (ptrdiff_t) size1)
fa9a63c5 4510
fa9a63c5 4511/* Call before fetching a character with *d. This switches over to
419d1c74
SM
4512 string2 if necessary.
4513 Check re_match_2_internal for a discussion of why end_match_2 might
4514 not be within string2 (but be equal to end_match_1 instead). */
fa9a63c5 4515#define PREFETCH() \
25fe55af 4516 while (d == dend) \
fa9a63c5
RM
4517 { \
4518 /* End of string2 => fail. */ \
25fe55af
RS
4519 if (dend == end_match_2) \
4520 goto fail; \
4bb91c68 4521 /* End of string1 => advance to string2. */ \
25fe55af 4522 d = string2; \
fa9a63c5
RM
4523 dend = end_match_2; \
4524 }
4525
f1ad044f
SM
4526/* Call before fetching a char with *d if you already checked other limits.
4527 This is meant for use in lookahead operations like wordend, etc..
4528 where we might need to look at parts of the string that might be
4529 outside of the LIMITs (i.e past `stop'). */
4530#define PREFETCH_NOLIMIT() \
4531 if (d == end1) \
4532 { \
4533 d = string2; \
4534 dend = end_match_2; \
4535 } \
fa9a63c5
RM
4536
4537/* Test if at very beginning or at very end of the virtual concatenation
7814e705 4538 of `string1' and `string2'. If only one string, it's `string2'. */
fa9a63c5 4539#define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2)
5e69f11e 4540#define AT_STRINGS_END(d) ((d) == end2)
fa9a63c5 4541
9121ca40 4542/* Disabled due to a compiler bug -- see comment at case wordbound */
b18215fc
RS
4543
4544/* The comment at case wordbound is following one, but we don't use
4545 AT_WORD_BOUNDARY anymore to support multibyte form.
4546
4547 The DEC Alpha C compiler 3.x generates incorrect code for the
25fe55af 4548 test WORDCHAR_P (d - 1) != WORDCHAR_P (d) in the expansion of
7814e705 4549 AT_WORD_BOUNDARY, so this code is disabled. Expanding the
b18215fc
RS
4550 macro and introducing temporary variables works around the bug. */
4551
9121ca40 4552#if 0
b313f9d8
PE
4553/* Test if D points to a character which is word-constituent. We have
4554 two special cases to check for: if past the end of string1, look at
4555 the first character in string2; and if before the beginning of
4556 string2, look at the last character in string1. */
4557#define WORDCHAR_P(d) \
4558 (SYNTAX ((d) == end1 ? *string2 \
4559 : (d) == string2 - 1 ? *(end1 - 1) : *(d)) \
4560 == Sword)
4561
fa9a63c5
RM
4562/* Test if the character before D and the one at D differ with respect
4563 to being word-constituent. */
4564#define AT_WORD_BOUNDARY(d) \
4565 (AT_STRINGS_BEG (d) || AT_STRINGS_END (d) \
4566 || WORDCHAR_P (d - 1) != WORDCHAR_P (d))
9121ca40 4567#endif
fa9a63c5
RM
4568
4569/* Free everything we malloc. */
4570#ifdef MATCH_MAY_ALLOCATE
952db0d7
PE
4571# define FREE_VAR(var) \
4572 do { \
4573 if (var) \
4574 { \
4575 REGEX_FREE (var); \
4576 var = NULL; \
4577 } \
4578 } while (0)
0b32bf0e 4579# define FREE_VARIABLES() \
fa9a63c5
RM
4580 do { \
4581 REGEX_FREE_STACK (fail_stack.stack); \
4582 FREE_VAR (regstart); \
4583 FREE_VAR (regend); \
fa9a63c5
RM
4584 FREE_VAR (best_regstart); \
4585 FREE_VAR (best_regend); \
fa9a63c5
RM
4586 } while (0)
4587#else
0b32bf0e 4588# define FREE_VARIABLES() ((void)0) /* Do nothing! But inhibit gcc warning. */
fa9a63c5
RM
4589#endif /* not MATCH_MAY_ALLOCATE */
4590
505bde11
SM
4591\f
4592/* Optimization routines. */
4593
4e8a9132
SM
4594/* If the operation is a match against one or more chars,
4595 return a pointer to the next operation, else return NULL. */
01618498 4596static re_char *
971de7fb 4597skip_one_char (const re_char *p)
4e8a9132 4598{
7393bcbb 4599 switch (*p++)
4e8a9132
SM
4600 {
4601 case anychar:
4602 break;
177c0ea7 4603
4e8a9132
SM
4604 case exactn:
4605 p += *p + 1;
4606 break;
4607
4608 case charset_not:
4609 case charset:
4610 if (CHARSET_RANGE_TABLE_EXISTS_P (p - 1))
4611 {
4612 int mcnt;
4613 p = CHARSET_RANGE_TABLE (p - 1);
4614 EXTRACT_NUMBER_AND_INCR (mcnt, p);
4615 p = CHARSET_RANGE_TABLE_END (p, mcnt);
4616 }
4617 else
4618 p += 1 + CHARSET_BITMAP_SIZE (p - 1);
4619 break;
177c0ea7 4620
4e8a9132
SM
4621 case syntaxspec:
4622 case notsyntaxspec:
1fb352e0 4623#ifdef emacs
4e8a9132
SM
4624 case categoryspec:
4625 case notcategoryspec:
4626#endif /* emacs */
4627 p++;
4628 break;
4629
4630 default:
4631 p = NULL;
4632 }
4633 return p;
4634}
4635
4636
505bde11 4637/* Jump over non-matching operations. */
839966f3 4638static re_char *
971de7fb 4639skip_noops (const re_char *p, const re_char *pend)
505bde11
SM
4640{
4641 int mcnt;
4642 while (p < pend)
4643 {
7393bcbb 4644 switch (*p)
505bde11
SM
4645 {
4646 case start_memory:
505bde11
SM
4647 case stop_memory:
4648 p += 2; break;
4649 case no_op:
4650 p += 1; break;
4651 case jump:
4652 p += 1;
4653 EXTRACT_NUMBER_AND_INCR (mcnt, p);
4654 p += mcnt;
4655 break;
4656 default:
4657 return p;
4658 }
4659 }
4660 assert (p == pend);
4661 return p;
4662}
4663
4664/* Non-zero if "p1 matches something" implies "p2 fails". */
4665static int
971de7fb 4666mutually_exclusive_p (struct re_pattern_buffer *bufp, const re_char *p1, const re_char *p2)
505bde11 4667{
4e8a9132 4668 re_opcode_t op2;
2d1675e4 4669 const boolean multibyte = RE_MULTIBYTE_P (bufp);
505bde11
SM
4670 unsigned char *pend = bufp->buffer + bufp->used;
4671
4e8a9132 4672 assert (p1 >= bufp->buffer && p1 < pend
505bde11
SM
4673 && p2 >= bufp->buffer && p2 <= pend);
4674
4675 /* Skip over open/close-group commands.
4676 If what follows this loop is a ...+ construct,
4677 look at what begins its body, since we will have to
4678 match at least one of that. */
4e8a9132
SM
4679 p2 = skip_noops (p2, pend);
4680 /* The same skip can be done for p1, except that this function
4681 is only used in the case where p1 is a simple match operator. */
4682 /* p1 = skip_noops (p1, pend); */
4683
4684 assert (p1 >= bufp->buffer && p1 < pend
4685 && p2 >= bufp->buffer && p2 <= pend);
4686
4687 op2 = p2 == pend ? succeed : *p2;
4688
7393bcbb 4689 switch (op2)
505bde11 4690 {
4e8a9132
SM
4691 case succeed:
4692 case endbuf:
4693 /* If we're at the end of the pattern, we can change. */
4694 if (skip_one_char (p1))
505bde11 4695 {
dc4a2ee0 4696 DEBUG_PRINT (" End of pattern: fast loop.\n");
505bde11 4697 return 1;
505bde11 4698 }
4e8a9132 4699 break;
177c0ea7 4700
4e8a9132 4701 case endline:
4e8a9132
SM
4702 case exactn:
4703 {
01618498 4704 register re_wchar_t c
4e8a9132 4705 = (re_opcode_t) *p2 == endline ? '\n'
62a6e103 4706 : RE_STRING_CHAR (p2 + 2, multibyte);
505bde11 4707
4e8a9132
SM
4708 if ((re_opcode_t) *p1 == exactn)
4709 {
62a6e103 4710 if (c != RE_STRING_CHAR (p1 + 2, multibyte))
4e8a9132 4711 {
dc4a2ee0 4712 DEBUG_PRINT (" '%c' != '%c' => fast loop.\n", c, p1[2]);
4e8a9132
SM
4713 return 1;
4714 }
4715 }
505bde11 4716
4e8a9132
SM
4717 else if ((re_opcode_t) *p1 == charset
4718 || (re_opcode_t) *p1 == charset_not)
4719 {
4720 int not = (re_opcode_t) *p1 == charset_not;
505bde11 4721
4e8a9132
SM
4722 /* Test if C is listed in charset (or charset_not)
4723 at `p1'. */
6fdd04b0 4724 if (! multibyte || IS_REAL_ASCII (c))
4e8a9132
SM
4725 {
4726 if (c < CHARSET_BITMAP_SIZE (p1) * BYTEWIDTH
4727 && p1[2 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
4728 not = !not;
4729 }
4730 else if (CHARSET_RANGE_TABLE_EXISTS_P (p1))
4731 CHARSET_LOOKUP_RANGE_TABLE (not, c, p1);
505bde11 4732
4e8a9132
SM
4733 /* `not' is equal to 1 if c would match, which means
4734 that we can't change to pop_failure_jump. */
4735 if (!not)
4736 {
dc4a2ee0 4737 DEBUG_PRINT (" No match => fast loop.\n");
4e8a9132
SM
4738 return 1;
4739 }
4740 }
4741 else if ((re_opcode_t) *p1 == anychar
4742 && c == '\n')
4743 {
dc4a2ee0 4744 DEBUG_PRINT (" . != \\n => fast loop.\n");
4e8a9132
SM
4745 return 1;
4746 }
4747 }
4748 break;
505bde11 4749
4e8a9132 4750 case charset:
4e8a9132
SM
4751 {
4752 if ((re_opcode_t) *p1 == exactn)
4753 /* Reuse the code above. */
4754 return mutually_exclusive_p (bufp, p2, p1);
505bde11 4755
505bde11
SM
4756 /* It is hard to list up all the character in charset
4757 P2 if it includes multibyte character. Give up in
4758 such case. */
4759 else if (!multibyte || !CHARSET_RANGE_TABLE_EXISTS_P (p2))
4760 {
4761 /* Now, we are sure that P2 has no range table.
4762 So, for the size of bitmap in P2, `p2[1]' is
7814e705 4763 enough. But P1 may have range table, so the
505bde11
SM
4764 size of bitmap table of P1 is extracted by
4765 using macro `CHARSET_BITMAP_SIZE'.
4766
6fdd04b0
KH
4767 In a multibyte case, we know that all the character
4768 listed in P2 is ASCII. In a unibyte case, P1 has only a
4769 bitmap table. So, in both cases, it is enough to test
4770 only the bitmap table of P1. */
505bde11 4771
411e4203 4772 if ((re_opcode_t) *p1 == charset)
505bde11
SM
4773 {
4774 int idx;
4775 /* We win if the charset inside the loop
4776 has no overlap with the one after the loop. */
4777 for (idx = 0;
4778 (idx < (int) p2[1]
4779 && idx < CHARSET_BITMAP_SIZE (p1));
4780 idx++)
4781 if ((p2[2 + idx] & p1[2 + idx]) != 0)
4782 break;
4783
4784 if (idx == p2[1]
4785 || idx == CHARSET_BITMAP_SIZE (p1))
4786 {
dc4a2ee0 4787 DEBUG_PRINT (" No match => fast loop.\n");
505bde11
SM
4788 return 1;
4789 }
4790 }
411e4203 4791 else if ((re_opcode_t) *p1 == charset_not)
505bde11
SM
4792 {
4793 int idx;
4794 /* We win if the charset_not inside the loop lists
7814e705 4795 every character listed in the charset after. */
505bde11
SM
4796 for (idx = 0; idx < (int) p2[1]; idx++)
4797 if (! (p2[2 + idx] == 0
4798 || (idx < CHARSET_BITMAP_SIZE (p1)
4799 && ((p2[2 + idx] & ~ p1[2 + idx]) == 0))))
4800 break;
4801
d1dfb56c
EZ
4802 if (idx == p2[1])
4803 {
dc4a2ee0 4804 DEBUG_PRINT (" No match => fast loop.\n");
d1dfb56c
EZ
4805 return 1;
4806 }
4e8a9132
SM
4807 }
4808 }
4809 }
609b757a 4810 break;
177c0ea7 4811
411e4203 4812 case charset_not:
7393bcbb 4813 switch (*p1)
411e4203
SM
4814 {
4815 case exactn:
4816 case charset:
4817 /* Reuse the code above. */
4818 return mutually_exclusive_p (bufp, p2, p1);
4819 case charset_not:
4820 /* When we have two charset_not, it's very unlikely that
4821 they don't overlap. The union of the two sets of excluded
4822 chars should cover all possible chars, which, as a matter of
4823 fact, is virtually impossible in multibyte buffers. */
36595814 4824 break;
411e4203
SM
4825 }
4826 break;
4827
4e8a9132 4828 case wordend:
669fa600
SM
4829 return ((re_opcode_t) *p1 == syntaxspec && p1[1] == Sword);
4830 case symend:
4e8a9132 4831 return ((re_opcode_t) *p1 == syntaxspec
669fa600
SM
4832 && (p1[1] == Ssymbol || p1[1] == Sword));
4833 case notsyntaxspec:
4834 return ((re_opcode_t) *p1 == syntaxspec && p1[1] == p2[1]);
4e8a9132
SM
4835
4836 case wordbeg:
669fa600
SM
4837 return ((re_opcode_t) *p1 == notsyntaxspec && p1[1] == Sword);
4838 case symbeg:
4e8a9132 4839 return ((re_opcode_t) *p1 == notsyntaxspec
669fa600
SM
4840 && (p1[1] == Ssymbol || p1[1] == Sword));
4841 case syntaxspec:
4842 return ((re_opcode_t) *p1 == notsyntaxspec && p1[1] == p2[1]);
4e8a9132
SM
4843
4844 case wordbound:
4845 return (((re_opcode_t) *p1 == notsyntaxspec
4846 || (re_opcode_t) *p1 == syntaxspec)
4847 && p1[1] == Sword);
4848
1fb352e0 4849#ifdef emacs
4e8a9132
SM
4850 case categoryspec:
4851 return ((re_opcode_t) *p1 == notcategoryspec && p1[1] == p2[1]);
4852 case notcategoryspec:
4853 return ((re_opcode_t) *p1 == categoryspec && p1[1] == p2[1]);
4854#endif /* emacs */
4855
4856 default:
4857 ;
505bde11
SM
4858 }
4859
4860 /* Safe default. */
4861 return 0;
4862}
4863
fa9a63c5
RM
4864\f
4865/* Matching routines. */
4866
25fe55af 4867#ifndef emacs /* Emacs never uses this. */
fa9a63c5
RM
4868/* re_match is like re_match_2 except it takes only a single string. */
4869
d1dfb56c 4870regoff_t
d2762c86 4871re_match (struct re_pattern_buffer *bufp, const char *string,
d1dfb56c 4872 size_t size, ssize_t pos, struct re_registers *regs)
fa9a63c5 4873{
d1dfb56c
EZ
4874 regoff_t result = re_match_2_internal (bufp, NULL, 0, (re_char*) string,
4875 size, pos, regs, size);
fa9a63c5
RM
4876 return result;
4877}
c0f9ea08 4878WEAK_ALIAS (__re_match, re_match)
fa9a63c5
RM
4879#endif /* not emacs */
4880
b18215fc
RS
4881#ifdef emacs
4882/* In Emacs, this is the string or buffer in which we
7814e705 4883 are matching. It is used for looking up syntax properties. */
b18215fc
RS
4884Lisp_Object re_match_object;
4885#endif
fa9a63c5
RM
4886
4887/* re_match_2 matches the compiled pattern in BUFP against the
4888 the (virtual) concatenation of STRING1 and STRING2 (of length SIZE1
4889 and SIZE2, respectively). We start matching at POS, and stop
4890 matching at STOP.
5e69f11e 4891
fa9a63c5 4892 If REGS is non-null and the `no_sub' field of BUFP is nonzero, we
7814e705 4893 store offsets for the substring each group matched in REGS. See the
fa9a63c5
RM
4894 documentation for exactly how many groups we fill.
4895
4896 We return -1 if no match, -2 if an internal error (such as the
7814e705 4897 failure stack overflowing). Otherwise, we return the length of the
fa9a63c5
RM
4898 matched substring. */
4899
d1dfb56c
EZ
4900regoff_t
4901re_match_2 (struct re_pattern_buffer *bufp, const char *string1,
4902 size_t size1, const char *string2, size_t size2, ssize_t pos,
4903 struct re_registers *regs, ssize_t stop)
fa9a63c5 4904{
d1dfb56c 4905 regoff_t result;
25fe55af 4906
b18215fc 4907#ifdef emacs
d1dfb56c 4908 ssize_t charpos;
d48cd3f4 4909 gl_state.object = re_match_object; /* Used by SYNTAX_TABLE_BYTE_TO_CHAR. */
99633e97 4910 charpos = SYNTAX_TABLE_BYTE_TO_CHAR (POS_AS_IN_BUFFER (pos));
cc9b4df2 4911 SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object, charpos, 1);
b18215fc
RS
4912#endif
4913
4bb91c68
SM
4914 result = re_match_2_internal (bufp, (re_char*) string1, size1,
4915 (re_char*) string2, size2,
cc9b4df2 4916 pos, regs, stop);
fa9a63c5
RM
4917 return result;
4918}
c0f9ea08 4919WEAK_ALIAS (__re_match_2, re_match_2)
fa9a63c5 4920
bf216479 4921
fa9a63c5 4922/* This is a separate function so that we can force an alloca cleanup
7814e705 4923 afterwards. */
d1dfb56c
EZ
4924static regoff_t
4925re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
4926 size_t size1, const re_char *string2, size_t size2,
4927 ssize_t pos, struct re_registers *regs, ssize_t stop)
fa9a63c5
RM
4928{
4929 /* General temporaries. */
dc4a2ee0 4930 int mcnt;
01618498 4931 size_t reg;
fa9a63c5
RM
4932
4933 /* Just past the end of the corresponding string. */
66f0296e 4934 re_char *end1, *end2;
fa9a63c5
RM
4935
4936 /* Pointers into string1 and string2, just past the last characters in
7814e705 4937 each to consider matching. */
66f0296e 4938 re_char *end_match_1, *end_match_2;
fa9a63c5
RM
4939
4940 /* Where we are in the data, and the end of the current string. */
66f0296e 4941 re_char *d, *dend;
5e69f11e 4942
99633e97
SM
4943 /* Used sometimes to remember where we were before starting matching
4944 an operator so that we can go back in case of failure. This "atomic"
4945 behavior of matching opcodes is indispensable to the correctness
4946 of the on_failure_keep_string_jump optimization. */
4947 re_char *dfail;
4948
fa9a63c5 4949 /* Where we are in the pattern, and the end of the pattern. */
01618498
SM
4950 re_char *p = bufp->buffer;
4951 re_char *pend = p + bufp->used;
fa9a63c5 4952
25fe55af 4953 /* We use this to map every character in the string. */
6676cb1c 4954 RE_TRANSLATE_TYPE translate = bufp->translate;
fa9a63c5 4955
cf9c99bc 4956 /* Nonzero if BUFP is setup from a multibyte regex. */
2d1675e4 4957 const boolean multibyte = RE_MULTIBYTE_P (bufp);
b18215fc 4958
cf9c99bc
KH
4959 /* Nonzero if STRING1/STRING2 are multibyte. */
4960 const boolean target_multibyte = RE_TARGET_MULTIBYTE_P (bufp);
4961
fa9a63c5
RM
4962 /* Failure point stack. Each place that can handle a failure further
4963 down the line pushes a failure point on this stack. It consists of
505bde11 4964 regstart, and regend for all registers corresponding to
fa9a63c5
RM
4965 the subexpressions we're currently inside, plus the number of such
4966 registers, and, finally, two char *'s. The first char * is where
4967 to resume scanning the pattern; the second one is where to resume
7814e705
JB
4968 scanning the strings. */
4969#ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
fa9a63c5
RM
4970 fail_stack_type fail_stack;
4971#endif
dc4a2ee0 4972#ifdef DEBUG_COMPILES_ARGUMENTS
fa9a63c5
RM
4973 unsigned nfailure_points_pushed = 0, nfailure_points_popped = 0;
4974#endif
4975
0b32bf0e 4976#if defined REL_ALLOC && defined REGEX_MALLOC
fa9a63c5
RM
4977 /* This holds the pointer to the failure stack, when
4978 it is allocated relocatably. */
4979 fail_stack_elt_t *failure_stack_ptr;
99633e97 4980#endif
fa9a63c5
RM
4981
4982 /* We fill all the registers internally, independent of what we
7814e705 4983 return, for use in backreferences. The number here includes
fa9a63c5 4984 an element for register zero. */
4bb91c68 4985 size_t num_regs = bufp->re_nsub + 1;
5e69f11e 4986
fa9a63c5
RM
4987 /* Information on the contents of registers. These are pointers into
4988 the input strings; they record just what was matched (on this
4989 attempt) by a subexpression part of the pattern, that is, the
4990 regnum-th regstart pointer points to where in the pattern we began
4991 matching and the regnum-th regend points to right after where we
4992 stopped matching the regnum-th subexpression. (The zeroth register
4993 keeps track of what the whole pattern matches.) */
4994#ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
66f0296e 4995 re_char **regstart, **regend;
fa9a63c5
RM
4996#endif
4997
fa9a63c5 4998 /* The following record the register info as found in the above
5e69f11e 4999 variables when we find a match better than any we've seen before.
fa9a63c5
RM
5000 This happens as we backtrack through the failure points, which in
5001 turn happens only if we have not yet matched the entire string. */
5002 unsigned best_regs_set = false;
5003#ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
66f0296e 5004 re_char **best_regstart, **best_regend;
fa9a63c5 5005#endif
5e69f11e 5006
fa9a63c5
RM
5007 /* Logically, this is `best_regend[0]'. But we don't want to have to
5008 allocate space for that if we're not allocating space for anything
7814e705 5009 else (see below). Also, we never need info about register 0 for
fa9a63c5
RM
5010 any of the other register vectors, and it seems rather a kludge to
5011 treat `best_regend' differently than the rest. So we keep track of
5012 the end of the best match so far in a separate variable. We
5013 initialize this to NULL so that when we backtrack the first time
5014 and need to test it, it's not garbage. */
66f0296e 5015 re_char *match_end = NULL;
fa9a63c5 5016
dc4a2ee0 5017#ifdef DEBUG_COMPILES_ARGUMENTS
fa9a63c5 5018 /* Counts the total number of registers pushed. */
5e69f11e 5019 unsigned num_regs_pushed = 0;
fa9a63c5
RM
5020#endif
5021
dc4a2ee0 5022 DEBUG_PRINT ("\n\nEntering re_match_2.\n");
5e69f11e 5023
fa9a63c5 5024 INIT_FAIL_STACK ();
5e69f11e 5025
fa9a63c5
RM
5026#ifdef MATCH_MAY_ALLOCATE
5027 /* Do not bother to initialize all the register variables if there are
5028 no groups in the pattern, as it takes a fair amount of time. If
5029 there are groups, we include space for register 0 (the whole
5030 pattern), even though we never use it, since it simplifies the
5031 array indexing. We should fix this. */
5032 if (bufp->re_nsub)
5033 {
66f0296e
SM
5034 regstart = REGEX_TALLOC (num_regs, re_char *);
5035 regend = REGEX_TALLOC (num_regs, re_char *);
5036 best_regstart = REGEX_TALLOC (num_regs, re_char *);
5037 best_regend = REGEX_TALLOC (num_regs, re_char *);
fa9a63c5 5038
505bde11 5039 if (!(regstart && regend && best_regstart && best_regend))
25fe55af
RS
5040 {
5041 FREE_VARIABLES ();
5042 return -2;
5043 }
fa9a63c5
RM
5044 }
5045 else
5046 {
5047 /* We must initialize all our variables to NULL, so that
25fe55af 5048 `FREE_VARIABLES' doesn't try to free them. */
505bde11 5049 regstart = regend = best_regstart = best_regend = NULL;
fa9a63c5
RM
5050 }
5051#endif /* MATCH_MAY_ALLOCATE */
5052
5053 /* The starting position is bogus. */
5054 if (pos < 0 || pos > size1 + size2)
5055 {
5056 FREE_VARIABLES ();
5057 return -1;
5058 }
5e69f11e 5059
fa9a63c5
RM
5060 /* Initialize subexpression text positions to -1 to mark ones that no
5061 start_memory/stop_memory has been seen for. Also initialize the
5062 register information struct. */
01618498
SM
5063 for (reg = 1; reg < num_regs; reg++)
5064 regstart[reg] = regend[reg] = NULL;
99633e97 5065
fa9a63c5 5066 /* We move `string1' into `string2' if the latter's empty -- but not if
7814e705 5067 `string1' is null. */
fa9a63c5
RM
5068 if (size2 == 0 && string1 != NULL)
5069 {
5070 string2 = string1;
5071 size2 = size1;
5072 string1 = 0;
5073 size1 = 0;
5074 }
5075 end1 = string1 + size1;
5076 end2 = string2 + size2;
5077
5e69f11e 5078 /* `p' scans through the pattern as `d' scans through the data.
fa9a63c5
RM
5079 `dend' is the end of the input string that `d' points within. `d'
5080 is advanced into the following input string whenever necessary, but
5081 this happens before fetching; therefore, at the beginning of the
5082 loop, `d' can be pointing at the end of a string, but it cannot
5083 equal `string2'. */
419d1c74 5084 if (pos >= size1)
fa9a63c5 5085 {
419d1c74
SM
5086 /* Only match within string2. */
5087 d = string2 + pos - size1;
5088 dend = end_match_2 = string2 + stop - size1;
5089 end_match_1 = end1; /* Just to give it a value. */
fa9a63c5
RM
5090 }
5091 else
5092 {
f1ad044f 5093 if (stop < size1)
419d1c74
SM
5094 {
5095 /* Only match within string1. */
5096 end_match_1 = string1 + stop;
5097 /* BEWARE!
5098 When we reach end_match_1, PREFETCH normally switches to string2.
5099 But in the present case, this means that just doing a PREFETCH
5100 makes us jump from `stop' to `gap' within the string.
5101 What we really want here is for the search to stop as
5102 soon as we hit end_match_1. That's why we set end_match_2
5103 to end_match_1 (since PREFETCH fails as soon as we hit
5104 end_match_2). */
5105 end_match_2 = end_match_1;
5106 }
5107 else
f1ad044f
SM
5108 { /* It's important to use this code when stop == size so that
5109 moving `d' from end1 to string2 will not prevent the d == dend
5110 check from catching the end of string. */
419d1c74
SM
5111 end_match_1 = end1;
5112 end_match_2 = string2 + stop - size1;
5113 }
5114 d = string1 + pos;
5115 dend = end_match_1;
fa9a63c5
RM
5116 }
5117
dc4a2ee0 5118 DEBUG_PRINT ("The compiled pattern is: ");
fa9a63c5 5119 DEBUG_PRINT_COMPILED_PATTERN (bufp, p, pend);
dc4a2ee0 5120 DEBUG_PRINT ("The string to match is: `");
fa9a63c5 5121 DEBUG_PRINT_DOUBLE_STRING (d, string1, size1, string2, size2);
dc4a2ee0 5122 DEBUG_PRINT ("'\n");
5e69f11e 5123
7814e705 5124 /* This loops over pattern commands. It exits by returning from the
fa9a63c5
RM
5125 function if the match is complete, or it drops through if the match
5126 fails at this starting point in the input data. */
5127 for (;;)
5128 {
dc4a2ee0 5129 DEBUG_PRINT ("\n%p: ", p);
fa9a63c5
RM
5130
5131 if (p == pend)
dc4a2ee0
PE
5132 {
5133 ptrdiff_t dcnt;
5134
5135 /* End of pattern means we might have succeeded. */
5136 DEBUG_PRINT ("end of pattern ... ");
5e69f11e 5137
fa9a63c5 5138 /* If we haven't matched the entire string, and we want the
25fe55af
RS
5139 longest match, try backtracking. */
5140 if (d != end_match_2)
fa9a63c5
RM
5141 {
5142 /* 1 if this match ends in the same string (string1 or string2)
5143 as the best previous match. */
d42f4f0f
PE
5144 boolean same_str_p = (FIRST_STRING_P (match_end)
5145 == FIRST_STRING_P (d));
fa9a63c5
RM
5146 /* 1 if this match is the best seen so far. */
5147 boolean best_match_p;
5148
5149 /* AIX compiler got confused when this was combined
7814e705 5150 with the previous declaration. */
fa9a63c5
RM
5151 if (same_str_p)
5152 best_match_p = d > match_end;
5153 else
99633e97 5154 best_match_p = !FIRST_STRING_P (d);
fa9a63c5 5155
dc4a2ee0 5156 DEBUG_PRINT ("backtracking.\n");
25fe55af
RS
5157
5158 if (!FAIL_STACK_EMPTY ())
5159 { /* More failure points to try. */
5160
5161 /* If exceeds best match so far, save it. */
5162 if (!best_regs_set || best_match_p)
5163 {
5164 best_regs_set = true;
5165 match_end = d;
5166
dc4a2ee0 5167 DEBUG_PRINT ("\nSAVING match as best so far.\n");
25fe55af 5168
01618498 5169 for (reg = 1; reg < num_regs; reg++)
25fe55af 5170 {
01618498
SM
5171 best_regstart[reg] = regstart[reg];
5172 best_regend[reg] = regend[reg];
25fe55af
RS
5173 }
5174 }
5175 goto fail;
5176 }
5177
5178 /* If no failure points, don't restore garbage. And if
5179 last match is real best match, don't restore second
5180 best one. */
5181 else if (best_regs_set && !best_match_p)
5182 {
5183 restore_best_regs:
5184 /* Restore best match. It may happen that `dend ==
5185 end_match_1' while the restored d is in string2.
5186 For example, the pattern `x.*y.*z' against the
5187 strings `x-' and `y-z-', if the two strings are
7814e705 5188 not consecutive in memory. */
dc4a2ee0 5189 DEBUG_PRINT ("Restoring best registers.\n");
25fe55af
RS
5190
5191 d = match_end;
5192 dend = ((d >= string1 && d <= end1)
5193 ? end_match_1 : end_match_2);
fa9a63c5 5194
01618498 5195 for (reg = 1; reg < num_regs; reg++)
fa9a63c5 5196 {
01618498
SM
5197 regstart[reg] = best_regstart[reg];
5198 regend[reg] = best_regend[reg];
fa9a63c5 5199 }
25fe55af
RS
5200 }
5201 } /* d != end_match_2 */
fa9a63c5
RM
5202
5203 succeed_label:
dc4a2ee0 5204 DEBUG_PRINT ("Accepting match.\n");
fa9a63c5 5205
25fe55af
RS
5206 /* If caller wants register contents data back, do it. */
5207 if (regs && !bufp->no_sub)
fa9a63c5 5208 {
25fe55af
RS
5209 /* Have the register data arrays been allocated? */
5210 if (bufp->regs_allocated == REGS_UNALLOCATED)
7814e705 5211 { /* No. So allocate them with malloc. We need one
25fe55af
RS
5212 extra element beyond `num_regs' for the `-1' marker
5213 GNU code uses. */
5214 regs->num_regs = MAX (RE_NREGS, num_regs + 1);
5215 regs->start = TALLOC (regs->num_regs, regoff_t);
5216 regs->end = TALLOC (regs->num_regs, regoff_t);
5217 if (regs->start == NULL || regs->end == NULL)
fa9a63c5
RM
5218 {
5219 FREE_VARIABLES ();
5220 return -2;
5221 }
25fe55af
RS
5222 bufp->regs_allocated = REGS_REALLOCATE;
5223 }
5224 else if (bufp->regs_allocated == REGS_REALLOCATE)
5225 { /* Yes. If we need more elements than were already
5226 allocated, reallocate them. If we need fewer, just
5227 leave it alone. */
5228 if (regs->num_regs < num_regs + 1)
5229 {
5230 regs->num_regs = num_regs + 1;
5231 RETALLOC (regs->start, regs->num_regs, regoff_t);
5232 RETALLOC (regs->end, regs->num_regs, regoff_t);
5233 if (regs->start == NULL || regs->end == NULL)
fa9a63c5
RM
5234 {
5235 FREE_VARIABLES ();
5236 return -2;
5237 }
25fe55af
RS
5238 }
5239 }
5240 else
fa9a63c5
RM
5241 {
5242 /* These braces fend off a "empty body in an else-statement"
7814e705 5243 warning under GCC when assert expands to nothing. */
fa9a63c5
RM
5244 assert (bufp->regs_allocated == REGS_FIXED);
5245 }
5246
25fe55af
RS
5247 /* Convert the pointer data in `regstart' and `regend' to
5248 indices. Register zero has to be set differently,
5249 since we haven't kept track of any info for it. */
5250 if (regs->num_regs > 0)
5251 {
5252 regs->start[0] = pos;
99633e97 5253 regs->end[0] = POINTER_TO_OFFSET (d);
25fe55af 5254 }
5e69f11e 5255
25fe55af
RS
5256 /* Go through the first `min (num_regs, regs->num_regs)'
5257 registers, since that is all we initialized. */
01618498 5258 for (reg = 1; reg < MIN (num_regs, regs->num_regs); reg++)
fa9a63c5 5259 {
01618498
SM
5260 if (REG_UNSET (regstart[reg]) || REG_UNSET (regend[reg]))
5261 regs->start[reg] = regs->end[reg] = -1;
25fe55af
RS
5262 else
5263 {
dc4a2ee0
PE
5264 regs->start[reg] = POINTER_TO_OFFSET (regstart[reg]);
5265 regs->end[reg] = POINTER_TO_OFFSET (regend[reg]);
25fe55af 5266 }
fa9a63c5 5267 }
5e69f11e 5268
25fe55af
RS
5269 /* If the regs structure we return has more elements than
5270 were in the pattern, set the extra elements to -1. If
5271 we (re)allocated the registers, this is the case,
5272 because we always allocate enough to have at least one
7814e705 5273 -1 at the end. */
01618498
SM
5274 for (reg = num_regs; reg < regs->num_regs; reg++)
5275 regs->start[reg] = regs->end[reg] = -1;
fa9a63c5
RM
5276 } /* regs && !bufp->no_sub */
5277
dc4a2ee0
PE
5278 DEBUG_PRINT ("%u failure points pushed, %u popped (%u remain).\n",
5279 nfailure_points_pushed, nfailure_points_popped,
5280 nfailure_points_pushed - nfailure_points_popped);
5281 DEBUG_PRINT ("%u registers pushed.\n", num_regs_pushed);
fa9a63c5 5282
dc4a2ee0 5283 dcnt = POINTER_TO_OFFSET (d) - pos;
fa9a63c5 5284
dc4a2ee0 5285 DEBUG_PRINT ("Returning %td from re_match_2.\n", dcnt);
fa9a63c5 5286
25fe55af 5287 FREE_VARIABLES ();
dc4a2ee0 5288 return dcnt;
25fe55af 5289 }
fa9a63c5 5290
7814e705 5291 /* Otherwise match next pattern command. */
7393bcbb 5292 switch (*p++)
fa9a63c5 5293 {
25fe55af
RS
5294 /* Ignore these. Used to ignore the n of succeed_n's which
5295 currently have n == 0. */
5296 case no_op:
dc4a2ee0 5297 DEBUG_PRINT ("EXECUTING no_op.\n");
25fe55af 5298 break;
fa9a63c5
RM
5299
5300 case succeed:
dc4a2ee0 5301 DEBUG_PRINT ("EXECUTING succeed.\n");
fa9a63c5
RM
5302 goto succeed_label;
5303
7814e705 5304 /* Match the next n pattern characters exactly. The following
25fe55af 5305 byte in the pattern defines n, and the n bytes after that
7814e705 5306 are the characters to match. */
fa9a63c5
RM
5307 case exactn:
5308 mcnt = *p++;
dc4a2ee0 5309 DEBUG_PRINT ("EXECUTING exactn %d.\n", mcnt);
fa9a63c5 5310
99633e97
SM
5311 /* Remember the start point to rollback upon failure. */
5312 dfail = d;
5313
6fdd04b0 5314#ifndef emacs
25fe55af
RS
5315 /* This is written out as an if-else so we don't waste time
5316 testing `translate' inside the loop. */
28703c16 5317 if (RE_TRANSLATE_P (translate))
6fdd04b0
KH
5318 do
5319 {
5320 PREFETCH ();
5321 if (RE_TRANSLATE (translate, *d) != *p++)
e934739e 5322 {
6fdd04b0
KH
5323 d = dfail;
5324 goto fail;
e934739e 5325 }
6fdd04b0
KH
5326 d++;
5327 }
5328 while (--mcnt);
fa9a63c5 5329 else
6fdd04b0
KH
5330 do
5331 {
5332 PREFETCH ();
5333 if (*d++ != *p++)
bf216479 5334 {
6fdd04b0
KH
5335 d = dfail;
5336 goto fail;
bf216479 5337 }
6fdd04b0
KH
5338 }
5339 while (--mcnt);
5340#else /* emacs */
5341 /* The cost of testing `translate' is comparatively small. */
cf9c99bc 5342 if (target_multibyte)
6fdd04b0
KH
5343 do
5344 {
5345 int pat_charlen, buf_charlen;
cf9c99bc 5346 int pat_ch, buf_ch;
e934739e 5347
6fdd04b0 5348 PREFETCH ();
cf9c99bc 5349 if (multibyte)
62a6e103 5350 pat_ch = STRING_CHAR_AND_LENGTH (p, pat_charlen);
cf9c99bc
KH
5351 else
5352 {
5353 pat_ch = RE_CHAR_TO_MULTIBYTE (*p);
5354 pat_charlen = 1;
5355 }
62a6e103 5356 buf_ch = STRING_CHAR_AND_LENGTH (d, buf_charlen);
e934739e 5357
6fdd04b0 5358 if (TRANSLATE (buf_ch) != pat_ch)
e934739e 5359 {
6fdd04b0
KH
5360 d = dfail;
5361 goto fail;
e934739e 5362 }
bf216479 5363
6fdd04b0
KH
5364 p += pat_charlen;
5365 d += buf_charlen;
5366 mcnt -= pat_charlen;
5367 }
5368 while (mcnt > 0);
fa9a63c5 5369 else
6fdd04b0
KH
5370 do
5371 {
abbd1bcf 5372 int pat_charlen;
cf9c99bc 5373 int pat_ch, buf_ch;
bf216479 5374
6fdd04b0 5375 PREFETCH ();
cf9c99bc
KH
5376 if (multibyte)
5377 {
62a6e103 5378 pat_ch = STRING_CHAR_AND_LENGTH (p, pat_charlen);
2afc21f5 5379 pat_ch = RE_CHAR_TO_UNIBYTE (pat_ch);
cf9c99bc
KH
5380 }
5381 else
5382 {
5383 pat_ch = *p;
5384 pat_charlen = 1;
5385 }
5386 buf_ch = RE_CHAR_TO_MULTIBYTE (*d);
5387 if (! CHAR_BYTE8_P (buf_ch))
5388 {
5389 buf_ch = TRANSLATE (buf_ch);
5390 buf_ch = RE_CHAR_TO_UNIBYTE (buf_ch);
5391 if (buf_ch < 0)
5392 buf_ch = *d;
5393 }
0e2501ed
AS
5394 else
5395 buf_ch = *d;
cf9c99bc 5396 if (buf_ch != pat_ch)
6fdd04b0
KH
5397 {
5398 d = dfail;
5399 goto fail;
bf216479 5400 }
cf9c99bc
KH
5401 p += pat_charlen;
5402 d++;
6fdd04b0
KH
5403 }
5404 while (--mcnt);
5405#endif
25fe55af 5406 break;
fa9a63c5
RM
5407
5408
25fe55af 5409 /* Match any character except possibly a newline or a null. */
fa9a63c5 5410 case anychar:
e934739e
RS
5411 {
5412 int buf_charlen;
01618498 5413 re_wchar_t buf_ch;
fa9a63c5 5414
dc4a2ee0 5415 DEBUG_PRINT ("EXECUTING anychar.\n");
fa9a63c5 5416
e934739e 5417 PREFETCH ();
62a6e103 5418 buf_ch = RE_STRING_CHAR_AND_LENGTH (d, buf_charlen,
cf9c99bc 5419 target_multibyte);
e934739e
RS
5420 buf_ch = TRANSLATE (buf_ch);
5421
5422 if ((!(bufp->syntax & RE_DOT_NEWLINE)
5423 && buf_ch == '\n')
5424 || ((bufp->syntax & RE_DOT_NOT_NULL)
5425 && buf_ch == '\000'))
5426 goto fail;
5427
dc4a2ee0 5428 DEBUG_PRINT (" Matched `%d'.\n", *d);
e934739e
RS
5429 d += buf_charlen;
5430 }
fa9a63c5
RM
5431 break;
5432
5433
5434 case charset:
5435 case charset_not:
5436 {
b18215fc 5437 register unsigned int c;
fa9a63c5 5438 boolean not = (re_opcode_t) *(p - 1) == charset_not;
b18215fc
RS
5439 int len;
5440
5441 /* Start of actual range_table, or end of bitmap if there is no
5442 range table. */
da053e48 5443 re_char *range_table IF_LINT (= NULL);
b18215fc 5444
96cc36cc 5445 /* Nonzero if there is a range table. */
b18215fc
RS
5446 int range_table_exists;
5447
96cc36cc
RS
5448 /* Number of ranges of range table. This is not included
5449 in the initial byte-length of the command. */
5450 int count = 0;
fa9a63c5 5451
f5020181
AS
5452 /* Whether matching against a unibyte character. */
5453 boolean unibyte_char = false;
5454
dc4a2ee0 5455 DEBUG_PRINT ("EXECUTING charset%s.\n", not ? "_not" : "");
fa9a63c5 5456
b18215fc 5457 range_table_exists = CHARSET_RANGE_TABLE_EXISTS_P (&p[-1]);
96cc36cc 5458
b18215fc 5459 if (range_table_exists)
96cc36cc
RS
5460 {
5461 range_table = CHARSET_RANGE_TABLE (&p[-1]); /* Past the bitmap. */
5462 EXTRACT_NUMBER_AND_INCR (count, range_table);
5463 }
b18215fc 5464
2d1675e4 5465 PREFETCH ();
62a6e103 5466 c = RE_STRING_CHAR_AND_LENGTH (d, len, target_multibyte);
cf9c99bc
KH
5467 if (target_multibyte)
5468 {
5469 int c1;
b18215fc 5470
cf9c99bc
KH
5471 c = TRANSLATE (c);
5472 c1 = RE_CHAR_TO_UNIBYTE (c);
5473 if (c1 >= 0)
f5020181
AS
5474 {
5475 unibyte_char = true;
5476 c = c1;
5477 }
cf9c99bc
KH
5478 }
5479 else
5480 {
5481 int c1 = RE_CHAR_TO_MULTIBYTE (c);
5482
5483 if (! CHAR_BYTE8_P (c1))
5484 {
5485 c1 = TRANSLATE (c1);
5486 c1 = RE_CHAR_TO_UNIBYTE (c1);
5487 if (c1 >= 0)
f5020181
AS
5488 {
5489 unibyte_char = true;
5490 c = c1;
5491 }
cf9c99bc 5492 }
0b8be006
AS
5493 else
5494 unibyte_char = true;
cf9c99bc
KH
5495 }
5496
f5020181 5497 if (unibyte_char && c < (1 << BYTEWIDTH))
b18215fc 5498 { /* Lookup bitmap. */
b18215fc
RS
5499 /* Cast to `unsigned' instead of `unsigned char' in
5500 case the bit list is a full 32 bytes long. */
5501 if (c < (unsigned) (CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH)
96cc36cc
RS
5502 && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
5503 not = !not;
b18215fc 5504 }
96cc36cc 5505#ifdef emacs
b18215fc 5506 else if (range_table_exists)
96cc36cc
RS
5507 {
5508 int class_bits = CHARSET_RANGE_TABLE_BITS (&p[-1]);
5509
14473664
SM
5510 if ( (class_bits & BIT_LOWER && ISLOWER (c))
5511 | (class_bits & BIT_MULTIBYTE)
96cc36cc
RS
5512 | (class_bits & BIT_PUNCT && ISPUNCT (c))
5513 | (class_bits & BIT_SPACE && ISSPACE (c))
5514 | (class_bits & BIT_UPPER && ISUPPER (c))
5515 | (class_bits & BIT_WORD && ISWORD (c)))
5516 not = !not;
5517 else
5518 CHARSET_LOOKUP_RANGE_TABLE_RAW (not, c, range_table, count);
5519 }
5520#endif /* emacs */
fa9a63c5 5521
96cc36cc
RS
5522 if (range_table_exists)
5523 p = CHARSET_RANGE_TABLE_END (range_table, count);
5524 else
5525 p += CHARSET_BITMAP_SIZE (&p[-1]) + 1;
fa9a63c5
RM
5526
5527 if (!not) goto fail;
5e69f11e 5528
b18215fc 5529 d += len;
fa9a63c5 5530 }
8fb31792 5531 break;
fa9a63c5
RM
5532
5533
25fe55af 5534 /* The beginning of a group is represented by start_memory.
505bde11 5535 The argument is the register number. The text
25fe55af 5536 matched within the group is recorded (in the internal
7814e705 5537 registers data structure) under the register number. */
25fe55af 5538 case start_memory:
dc4a2ee0 5539 DEBUG_PRINT ("EXECUTING start_memory %d:\n", *p);
505bde11
SM
5540
5541 /* In case we need to undo this operation (via backtracking). */
dc4a2ee0 5542 PUSH_FAILURE_REG (*p);
fa9a63c5 5543
25fe55af 5544 regstart[*p] = d;
4bb91c68 5545 regend[*p] = NULL; /* probably unnecessary. -sm */
dc4a2ee0 5546 DEBUG_PRINT (" regstart: %td\n", POINTER_TO_OFFSET (regstart[*p]));
fa9a63c5 5547
25fe55af 5548 /* Move past the register number and inner group count. */
505bde11 5549 p += 1;
25fe55af 5550 break;
fa9a63c5
RM
5551
5552
25fe55af 5553 /* The stop_memory opcode represents the end of a group. Its
505bde11 5554 argument is the same as start_memory's: the register number. */
fa9a63c5 5555 case stop_memory:
dc4a2ee0 5556 DEBUG_PRINT ("EXECUTING stop_memory %d:\n", *p);
505bde11
SM
5557
5558 assert (!REG_UNSET (regstart[*p]));
5559 /* Strictly speaking, there should be code such as:
177c0ea7 5560
0b32bf0e 5561 assert (REG_UNSET (regend[*p]));
505bde11
SM
5562 PUSH_FAILURE_REGSTOP ((unsigned int)*p);
5563
5564 But the only info to be pushed is regend[*p] and it is known to
5565 be UNSET, so there really isn't anything to push.
5566 Not pushing anything, on the other hand deprives us from the
5567 guarantee that regend[*p] is UNSET since undoing this operation
5568 will not reset its value properly. This is not important since
5569 the value will only be read on the next start_memory or at
5570 the very end and both events can only happen if this stop_memory
5571 is *not* undone. */
fa9a63c5 5572
25fe55af 5573 regend[*p] = d;
dc4a2ee0 5574 DEBUG_PRINT (" regend: %td\n", POINTER_TO_OFFSET (regend[*p]));
fa9a63c5 5575
25fe55af 5576 /* Move past the register number and the inner group count. */
505bde11 5577 p += 1;
25fe55af 5578 break;
fa9a63c5
RM
5579
5580
5581 /* \<digit> has been turned into a `duplicate' command which is
25fe55af
RS
5582 followed by the numeric value of <digit> as the register number. */
5583 case duplicate:
fa9a63c5 5584 {
66f0296e 5585 register re_char *d2, *dend2;
7814e705 5586 int regno = *p++; /* Get which register to match against. */
dc4a2ee0 5587 DEBUG_PRINT ("EXECUTING duplicate %d.\n", regno);
fa9a63c5 5588
7814e705 5589 /* Can't back reference a group which we've never matched. */
25fe55af
RS
5590 if (REG_UNSET (regstart[regno]) || REG_UNSET (regend[regno]))
5591 goto fail;
5e69f11e 5592
7814e705 5593 /* Where in input to try to start matching. */
25fe55af 5594 d2 = regstart[regno];
5e69f11e 5595
99633e97
SM
5596 /* Remember the start point to rollback upon failure. */
5597 dfail = d;
5598
25fe55af
RS
5599 /* Where to stop matching; if both the place to start and
5600 the place to stop matching are in the same string, then
5601 set to the place to stop, otherwise, for now have to use
5602 the end of the first string. */
fa9a63c5 5603
25fe55af 5604 dend2 = ((FIRST_STRING_P (regstart[regno])
fa9a63c5
RM
5605 == FIRST_STRING_P (regend[regno]))
5606 ? regend[regno] : end_match_1);
5607 for (;;)
5608 {
dc4a2ee0
PE
5609 ptrdiff_t dcnt;
5610
fa9a63c5 5611 /* If necessary, advance to next segment in register
25fe55af 5612 contents. */
fa9a63c5
RM
5613 while (d2 == dend2)
5614 {
5615 if (dend2 == end_match_2) break;
5616 if (dend2 == regend[regno]) break;
5617
25fe55af
RS
5618 /* End of string1 => advance to string2. */
5619 d2 = string2;
5620 dend2 = regend[regno];
fa9a63c5
RM
5621 }
5622 /* At end of register contents => success */
5623 if (d2 == dend2) break;
5624
5625 /* If necessary, advance to next segment in data. */
5626 PREFETCH ();
5627
5628 /* How many characters left in this segment to match. */
dc4a2ee0 5629 dcnt = dend - d;
5e69f11e 5630
fa9a63c5 5631 /* Want how many consecutive characters we can match in
25fe55af 5632 one shot, so, if necessary, adjust the count. */
dc4a2ee0
PE
5633 if (dcnt > dend2 - d2)
5634 dcnt = dend2 - d2;
5e69f11e 5635
fa9a63c5 5636 /* Compare that many; failure if mismatch, else move
25fe55af 5637 past them. */
28703c16 5638 if (RE_TRANSLATE_P (translate)
dc4a2ee0
PE
5639 ? bcmp_translate (d, d2, dcnt, translate, target_multibyte)
5640 : memcmp (d, d2, dcnt))
99633e97
SM
5641 {
5642 d = dfail;
5643 goto fail;
5644 }
dc4a2ee0 5645 d += dcnt, d2 += dcnt;
fa9a63c5
RM
5646 }
5647 }
5648 break;
5649
5650
25fe55af 5651 /* begline matches the empty string at the beginning of the string
c0f9ea08 5652 (unless `not_bol' is set in `bufp'), and after newlines. */
fa9a63c5 5653 case begline:
dc4a2ee0 5654 DEBUG_PRINT ("EXECUTING begline.\n");
5e69f11e 5655
25fe55af
RS
5656 if (AT_STRINGS_BEG (d))
5657 {
5658 if (!bufp->not_bol) break;
5659 }
419d1c74 5660 else
25fe55af 5661 {
bf216479 5662 unsigned c;
419d1c74 5663 GET_CHAR_BEFORE_2 (c, d, string1, end1, string2, end2);
c0f9ea08 5664 if (c == '\n')
419d1c74 5665 break;
25fe55af
RS
5666 }
5667 /* In all other cases, we fail. */
5668 goto fail;
fa9a63c5
RM
5669
5670
25fe55af 5671 /* endline is the dual of begline. */
fa9a63c5 5672 case endline:
dc4a2ee0 5673 DEBUG_PRINT ("EXECUTING endline.\n");
fa9a63c5 5674
25fe55af
RS
5675 if (AT_STRINGS_END (d))
5676 {
5677 if (!bufp->not_eol) break;
5678 }
f1ad044f 5679 else
25fe55af 5680 {
f1ad044f 5681 PREFETCH_NOLIMIT ();
c0f9ea08 5682 if (*d == '\n')
f1ad044f 5683 break;
25fe55af
RS
5684 }
5685 goto fail;
fa9a63c5
RM
5686
5687
5688 /* Match at the very beginning of the data. */
25fe55af 5689 case begbuf:
dc4a2ee0 5690 DEBUG_PRINT ("EXECUTING begbuf.\n");
25fe55af
RS
5691 if (AT_STRINGS_BEG (d))
5692 break;
5693 goto fail;
fa9a63c5
RM
5694
5695
5696 /* Match at the very end of the data. */
25fe55af 5697 case endbuf:
dc4a2ee0 5698 DEBUG_PRINT ("EXECUTING endbuf.\n");
fa9a63c5
RM
5699 if (AT_STRINGS_END (d))
5700 break;
25fe55af 5701 goto fail;
5e69f11e 5702
5e69f11e 5703
25fe55af
RS
5704 /* on_failure_keep_string_jump is used to optimize `.*\n'. It
5705 pushes NULL as the value for the string on the stack. Then
505bde11 5706 `POP_FAILURE_POINT' will keep the current value for the
25fe55af 5707 string, instead of restoring it. To see why, consider
7814e705 5708 matching `foo\nbar' against `.*\n'. The .* matches the foo;
25fe55af
RS
5709 then the . fails against the \n. But the next thing we want
5710 to do is match the \n against the \n; if we restored the
5711 string value, we would be back at the foo.
5712
5713 Because this is used only in specific cases, we don't need to
5714 check all the things that `on_failure_jump' does, to make
5715 sure the right things get saved on the stack. Hence we don't
5716 share its code. The only reason to push anything on the
5717 stack at all is that otherwise we would have to change
5718 `anychar's code to do something besides goto fail in this
5719 case; that seems worse than this. */
5720 case on_failure_keep_string_jump:
505bde11 5721 EXTRACT_NUMBER_AND_INCR (mcnt, p);
dc4a2ee0
PE
5722 DEBUG_PRINT ("EXECUTING on_failure_keep_string_jump %d (to %p):\n",
5723 mcnt, p + mcnt);
fa9a63c5 5724
505bde11
SM
5725 PUSH_FAILURE_POINT (p - 3, NULL);
5726 break;
5727
0683b6fa
SM
5728 /* A nasty loop is introduced by the non-greedy *? and +?.
5729 With such loops, the stack only ever contains one failure point
5730 at a time, so that a plain on_failure_jump_loop kind of
5731 cycle detection cannot work. Worse yet, such a detection
5732 can not only fail to detect a cycle, but it can also wrongly
5733 detect a cycle (between different instantiations of the same
6df42991 5734 loop).
0683b6fa
SM
5735 So the method used for those nasty loops is a little different:
5736 We use a special cycle-detection-stack-frame which is pushed
5737 when the on_failure_jump_nastyloop failure-point is *popped*.
5738 This special frame thus marks the beginning of one iteration
5739 through the loop and we can hence easily check right here
5740 whether something matched between the beginning and the end of
5741 the loop. */
5742 case on_failure_jump_nastyloop:
5743 EXTRACT_NUMBER_AND_INCR (mcnt, p);
dc4a2ee0
PE
5744 DEBUG_PRINT ("EXECUTING on_failure_jump_nastyloop %d (to %p):\n",
5745 mcnt, p + mcnt);
0683b6fa
SM
5746
5747 assert ((re_opcode_t)p[-4] == no_op);
6df42991
SM
5748 {
5749 int cycle = 0;
5750 CHECK_INFINITE_LOOP (p - 4, d);
5751 if (!cycle)
5752 /* If there's a cycle, just continue without pushing
5753 this failure point. The failure point is the "try again"
5754 option, which shouldn't be tried.
5755 We want (x?)*?y\1z to match both xxyz and xxyxz. */
5756 PUSH_FAILURE_POINT (p - 3, d);
5757 }
0683b6fa
SM
5758 break;
5759
4e8a9132
SM
5760 /* Simple loop detecting on_failure_jump: just check on the
5761 failure stack if the same spot was already hit earlier. */
505bde11
SM
5762 case on_failure_jump_loop:
5763 on_failure:
5764 EXTRACT_NUMBER_AND_INCR (mcnt, p);
dc4a2ee0
PE
5765 DEBUG_PRINT ("EXECUTING on_failure_jump_loop %d (to %p):\n",
5766 mcnt, p + mcnt);
6df42991
SM
5767 {
5768 int cycle = 0;
5769 CHECK_INFINITE_LOOP (p - 3, d);
5770 if (cycle)
5771 /* If there's a cycle, get out of the loop, as if the matching
5772 had failed. We used to just `goto fail' here, but that was
5773 aborting the search a bit too early: we want to keep the
5774 empty-loop-match and keep matching after the loop.
5775 We want (x?)*y\1z to match both xxyz and xxyxz. */
5776 p += mcnt;
5777 else
5778 PUSH_FAILURE_POINT (p - 3, d);
5779 }
25fe55af 5780 break;
fa9a63c5
RM
5781
5782
5783 /* Uses of on_failure_jump:
5e69f11e 5784
25fe55af
RS
5785 Each alternative starts with an on_failure_jump that points
5786 to the beginning of the next alternative. Each alternative
5787 except the last ends with a jump that in effect jumps past
5788 the rest of the alternatives. (They really jump to the
5789 ending jump of the following alternative, because tensioning
5790 these jumps is a hassle.)
fa9a63c5 5791
25fe55af
RS
5792 Repeats start with an on_failure_jump that points past both
5793 the repetition text and either the following jump or
5794 pop_failure_jump back to this on_failure_jump. */
fa9a63c5 5795 case on_failure_jump:
25fe55af 5796 EXTRACT_NUMBER_AND_INCR (mcnt, p);
dc4a2ee0
PE
5797 DEBUG_PRINT ("EXECUTING on_failure_jump %d (to %p):\n",
5798 mcnt, p + mcnt);
25fe55af 5799
505bde11 5800 PUSH_FAILURE_POINT (p -3, d);
25fe55af
RS
5801 break;
5802
4e8a9132 5803 /* This operation is used for greedy *.
505bde11
SM
5804 Compare the beginning of the repeat with what in the
5805 pattern follows its end. If we can establish that there
5806 is nothing that they would both match, i.e., that we
5807 would have to backtrack because of (as in, e.g., `a*a')
5808 then we can use a non-backtracking loop based on
4e8a9132 5809 on_failure_keep_string_jump instead of on_failure_jump. */
505bde11 5810 case on_failure_jump_smart:
25fe55af 5811 EXTRACT_NUMBER_AND_INCR (mcnt, p);
dc4a2ee0
PE
5812 DEBUG_PRINT ("EXECUTING on_failure_jump_smart %d (to %p).\n",
5813 mcnt, p + mcnt);
25fe55af 5814 {
01618498 5815 re_char *p1 = p; /* Next operation. */
6dcf2d0e
SM
5816 /* Here, we discard `const', making re_match non-reentrant. */
5817 unsigned char *p2 = (unsigned char*) p + mcnt; /* Jump dest. */
5818 unsigned char *p3 = (unsigned char*) p - 3; /* opcode location. */
fa9a63c5 5819
505bde11
SM
5820 p -= 3; /* Reset so that we will re-execute the
5821 instruction once it's been changed. */
fa9a63c5 5822
4e8a9132
SM
5823 EXTRACT_NUMBER (mcnt, p2 - 2);
5824
5825 /* Ensure this is a indeed the trivial kind of loop
5826 we are expecting. */
5827 assert (skip_one_char (p1) == p2 - 3);
5828 assert ((re_opcode_t) p2[-3] == jump && p2 + mcnt == p);
99633e97 5829 DEBUG_STATEMENT (debug += 2);
505bde11 5830 if (mutually_exclusive_p (bufp, p1, p2))
fa9a63c5 5831 {
505bde11 5832 /* Use a fast `on_failure_keep_string_jump' loop. */
dc4a2ee0 5833 DEBUG_PRINT (" smart exclusive => fast loop.\n");
01618498 5834 *p3 = (unsigned char) on_failure_keep_string_jump;
4e8a9132 5835 STORE_NUMBER (p2 - 2, mcnt + 3);
25fe55af 5836 }
505bde11 5837 else
fa9a63c5 5838 {
505bde11 5839 /* Default to a safe `on_failure_jump' loop. */
dc4a2ee0 5840 DEBUG_PRINT (" smart default => slow loop.\n");
01618498 5841 *p3 = (unsigned char) on_failure_jump;
fa9a63c5 5842 }
99633e97 5843 DEBUG_STATEMENT (debug -= 2);
25fe55af 5844 }
505bde11 5845 break;
25fe55af
RS
5846
5847 /* Unconditionally jump (without popping any failure points). */
5848 case jump:
fa9a63c5 5849 unconditional_jump:
5b370c2b 5850 IMMEDIATE_QUIT_CHECK;
fa9a63c5 5851 EXTRACT_NUMBER_AND_INCR (mcnt, p); /* Get the amount to jump. */
dc4a2ee0 5852 DEBUG_PRINT ("EXECUTING jump %d ", mcnt);
7814e705 5853 p += mcnt; /* Do the jump. */
dc4a2ee0 5854 DEBUG_PRINT ("(to %p).\n", p);
25fe55af
RS
5855 break;
5856
5857
25fe55af
RS
5858 /* Have to succeed matching what follows at least n times.
5859 After that, handle like `on_failure_jump'. */
5860 case succeed_n:
01618498 5861 /* Signedness doesn't matter since we only compare MCNT to 0. */
25fe55af 5862 EXTRACT_NUMBER (mcnt, p + 2);
dc4a2ee0 5863 DEBUG_PRINT ("EXECUTING succeed_n %d.\n", mcnt);
5e69f11e 5864
dc1e502d
SM
5865 /* Originally, mcnt is how many times we HAVE to succeed. */
5866 if (mcnt != 0)
25fe55af 5867 {
6dcf2d0e
SM
5868 /* Here, we discard `const', making re_match non-reentrant. */
5869 unsigned char *p2 = (unsigned char*) p + 2; /* counter loc. */
dc1e502d 5870 mcnt--;
01618498
SM
5871 p += 4;
5872 PUSH_NUMBER (p2, mcnt);
25fe55af 5873 }
dc1e502d
SM
5874 else
5875 /* The two bytes encoding mcnt == 0 are two no_op opcodes. */
5876 goto on_failure;
25fe55af
RS
5877 break;
5878
5879 case jump_n:
01618498 5880 /* Signedness doesn't matter since we only compare MCNT to 0. */
25fe55af 5881 EXTRACT_NUMBER (mcnt, p + 2);
dc4a2ee0 5882 DEBUG_PRINT ("EXECUTING jump_n %d.\n", mcnt);
25fe55af
RS
5883
5884 /* Originally, this is how many times we CAN jump. */
dc1e502d 5885 if (mcnt != 0)
25fe55af 5886 {
6dcf2d0e
SM
5887 /* Here, we discard `const', making re_match non-reentrant. */
5888 unsigned char *p2 = (unsigned char*) p + 2; /* counter loc. */
dc1e502d 5889 mcnt--;
01618498 5890 PUSH_NUMBER (p2, mcnt);
dc1e502d 5891 goto unconditional_jump;
25fe55af
RS
5892 }
5893 /* If don't have to jump any more, skip over the rest of command. */
5e69f11e
RM
5894 else
5895 p += 4;
25fe55af 5896 break;
5e69f11e 5897
fa9a63c5
RM
5898 case set_number_at:
5899 {
01618498 5900 unsigned char *p2; /* Location of the counter. */
dc4a2ee0 5901 DEBUG_PRINT ("EXECUTING set_number_at.\n");
fa9a63c5 5902
25fe55af 5903 EXTRACT_NUMBER_AND_INCR (mcnt, p);
6dcf2d0e
SM
5904 /* Here, we discard `const', making re_match non-reentrant. */
5905 p2 = (unsigned char*) p + mcnt;
01618498 5906 /* Signedness doesn't matter since we only copy MCNT's bits . */
25fe55af 5907 EXTRACT_NUMBER_AND_INCR (mcnt, p);
dc4a2ee0 5908 DEBUG_PRINT (" Setting %p to %d.\n", p2, mcnt);
01618498 5909 PUSH_NUMBER (p2, mcnt);
25fe55af
RS
5910 break;
5911 }
9121ca40
KH
5912
5913 case wordbound:
66f0296e 5914 case notwordbound:
19ed5445
PE
5915 {
5916 boolean not = (re_opcode_t) *(p - 1) == notwordbound;
dc4a2ee0 5917 DEBUG_PRINT ("EXECUTING %swordbound.\n", not ? "not" : "");
fa9a63c5 5918
19ed5445 5919 /* We SUCCEED (or FAIL) in one of the following cases: */
9121ca40 5920
19ed5445
PE
5921 /* Case 1: D is at the beginning or the end of string. */
5922 if (AT_STRINGS_BEG (d) || AT_STRINGS_END (d))
5923 not = !not;
5924 else
5925 {
5926 /* C1 is the character before D, S1 is the syntax of C1, C2
5927 is the character at D, and S2 is the syntax of C2. */
5928 re_wchar_t c1, c2;
5929 int s1, s2;
5930 int dummy;
b18215fc 5931#ifdef emacs
d1dfb56c
EZ
5932 ssize_t offset = PTR_TO_OFFSET (d - 1);
5933 ssize_t charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
19ed5445 5934 UPDATE_SYNTAX_TABLE (charpos);
25fe55af 5935#endif
19ed5445
PE
5936 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
5937 s1 = SYNTAX (c1);
b18215fc 5938#ifdef emacs
19ed5445 5939 UPDATE_SYNTAX_TABLE_FORWARD (charpos + 1);
25fe55af 5940#endif
19ed5445
PE
5941 PREFETCH_NOLIMIT ();
5942 GET_CHAR_AFTER (c2, d, dummy);
5943 s2 = SYNTAX (c2);
5944
5945 if (/* Case 2: Only one of S1 and S2 is Sword. */
5946 ((s1 == Sword) != (s2 == Sword))
5947 /* Case 3: Both of S1 and S2 are Sword, and macro
5948 WORD_BOUNDARY_P (C1, C2) returns nonzero. */
5949 || ((s1 == Sword) && WORD_BOUNDARY_P (c1, c2)))
5950 not = !not;
5951 }
5952 if (not)
5953 break;
5954 else
5955 goto fail;
5956 }
fa9a63c5
RM
5957
5958 case wordbeg:
dc4a2ee0 5959 DEBUG_PRINT ("EXECUTING wordbeg.\n");
fa9a63c5 5960
b18215fc
RS
5961 /* We FAIL in one of the following cases: */
5962
7814e705 5963 /* Case 1: D is at the end of string. */
b18215fc 5964 if (AT_STRINGS_END (d))
99633e97 5965 goto fail;
b18215fc
RS
5966 else
5967 {
5968 /* C1 is the character before D, S1 is the syntax of C1, C2
5969 is the character at D, and S2 is the syntax of C2. */
01618498
SM
5970 re_wchar_t c1, c2;
5971 int s1, s2;
bf216479 5972 int dummy;
fa9a63c5 5973#ifdef emacs
d1dfb56c
EZ
5974 ssize_t offset = PTR_TO_OFFSET (d);
5975 ssize_t charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
92432794 5976 UPDATE_SYNTAX_TABLE (charpos);
25fe55af 5977#endif
99633e97 5978 PREFETCH ();
6fdd04b0 5979 GET_CHAR_AFTER (c2, d, dummy);
b18215fc 5980 s2 = SYNTAX (c2);
177c0ea7 5981
b18215fc
RS
5982 /* Case 2: S2 is not Sword. */
5983 if (s2 != Sword)
5984 goto fail;
5985
5986 /* Case 3: D is not at the beginning of string ... */
5987 if (!AT_STRINGS_BEG (d))
5988 {
5989 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
5990#ifdef emacs
5d967c7a 5991 UPDATE_SYNTAX_TABLE_BACKWARD (charpos - 1);
25fe55af 5992#endif
b18215fc
RS
5993 s1 = SYNTAX (c1);
5994
5995 /* ... and S1 is Sword, and WORD_BOUNDARY_P (C1, C2)
7814e705 5996 returns 0. */
b18215fc
RS
5997 if ((s1 == Sword) && !WORD_BOUNDARY_P (c1, c2))
5998 goto fail;
5999 }
6000 }
e318085a
RS
6001 break;
6002
b18215fc 6003 case wordend:
dc4a2ee0 6004 DEBUG_PRINT ("EXECUTING wordend.\n");
b18215fc
RS
6005
6006 /* We FAIL in one of the following cases: */
6007
6008 /* Case 1: D is at the beginning of string. */
6009 if (AT_STRINGS_BEG (d))
e318085a 6010 goto fail;
b18215fc
RS
6011 else
6012 {
6013 /* C1 is the character before D, S1 is the syntax of C1, C2
6014 is the character at D, and S2 is the syntax of C2. */
01618498
SM
6015 re_wchar_t c1, c2;
6016 int s1, s2;
bf216479 6017 int dummy;
5d967c7a 6018#ifdef emacs
d1dfb56c
EZ
6019 ssize_t offset = PTR_TO_OFFSET (d) - 1;
6020 ssize_t charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
92432794 6021 UPDATE_SYNTAX_TABLE (charpos);
5d967c7a 6022#endif
99633e97 6023 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
b18215fc
RS
6024 s1 = SYNTAX (c1);
6025
6026 /* Case 2: S1 is not Sword. */
6027 if (s1 != Sword)
6028 goto fail;
6029
6030 /* Case 3: D is not at the end of string ... */
6031 if (!AT_STRINGS_END (d))
6032 {
f1ad044f 6033 PREFETCH_NOLIMIT ();
6fdd04b0 6034 GET_CHAR_AFTER (c2, d, dummy);
5d967c7a
RS
6035#ifdef emacs
6036 UPDATE_SYNTAX_TABLE_FORWARD (charpos);
6037#endif
b18215fc
RS
6038 s2 = SYNTAX (c2);
6039
6040 /* ... and S2 is Sword, and WORD_BOUNDARY_P (C1, C2)
7814e705 6041 returns 0. */
b18215fc 6042 if ((s2 == Sword) && !WORD_BOUNDARY_P (c1, c2))
25fe55af 6043 goto fail;
b18215fc
RS
6044 }
6045 }
e318085a
RS
6046 break;
6047
669fa600 6048 case symbeg:
dc4a2ee0 6049 DEBUG_PRINT ("EXECUTING symbeg.\n");
669fa600
SM
6050
6051 /* We FAIL in one of the following cases: */
6052
7814e705 6053 /* Case 1: D is at the end of string. */
669fa600
SM
6054 if (AT_STRINGS_END (d))
6055 goto fail;
6056 else
6057 {
6058 /* C1 is the character before D, S1 is the syntax of C1, C2
6059 is the character at D, and S2 is the syntax of C2. */
6060 re_wchar_t c1, c2;
6061 int s1, s2;
6062#ifdef emacs
d1dfb56c
EZ
6063 ssize_t offset = PTR_TO_OFFSET (d);
6064 ssize_t charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
669fa600
SM
6065 UPDATE_SYNTAX_TABLE (charpos);
6066#endif
6067 PREFETCH ();
62a6e103 6068 c2 = RE_STRING_CHAR (d, target_multibyte);
669fa600 6069 s2 = SYNTAX (c2);
7814e705 6070
669fa600
SM
6071 /* Case 2: S2 is neither Sword nor Ssymbol. */
6072 if (s2 != Sword && s2 != Ssymbol)
6073 goto fail;
6074
6075 /* Case 3: D is not at the beginning of string ... */
6076 if (!AT_STRINGS_BEG (d))
6077 {
6078 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
6079#ifdef emacs
6080 UPDATE_SYNTAX_TABLE_BACKWARD (charpos - 1);
6081#endif
6082 s1 = SYNTAX (c1);
6083
6084 /* ... and S1 is Sword or Ssymbol. */
6085 if (s1 == Sword || s1 == Ssymbol)
6086 goto fail;
6087 }
6088 }
6089 break;
6090
6091 case symend:
dc4a2ee0 6092 DEBUG_PRINT ("EXECUTING symend.\n");
669fa600
SM
6093
6094 /* We FAIL in one of the following cases: */
6095
6096 /* Case 1: D is at the beginning of string. */
6097 if (AT_STRINGS_BEG (d))
6098 goto fail;
6099 else
6100 {
6101 /* C1 is the character before D, S1 is the syntax of C1, C2
6102 is the character at D, and S2 is the syntax of C2. */
6103 re_wchar_t c1, c2;
6104 int s1, s2;
6105#ifdef emacs
d1dfb56c
EZ
6106 ssize_t offset = PTR_TO_OFFSET (d) - 1;
6107 ssize_t charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
669fa600
SM
6108 UPDATE_SYNTAX_TABLE (charpos);
6109#endif
6110 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
6111 s1 = SYNTAX (c1);
6112
6113 /* Case 2: S1 is neither Ssymbol nor Sword. */
6114 if (s1 != Sword && s1 != Ssymbol)
6115 goto fail;
6116
6117 /* Case 3: D is not at the end of string ... */
6118 if (!AT_STRINGS_END (d))
6119 {
6120 PREFETCH_NOLIMIT ();
62a6e103 6121 c2 = RE_STRING_CHAR (d, target_multibyte);
669fa600 6122#ifdef emacs
134579f2 6123 UPDATE_SYNTAX_TABLE_FORWARD (charpos + 1);
669fa600
SM
6124#endif
6125 s2 = SYNTAX (c2);
6126
6127 /* ... and S2 is Sword or Ssymbol. */
6128 if (s2 == Sword || s2 == Ssymbol)
6129 goto fail;
b18215fc
RS
6130 }
6131 }
e318085a
RS
6132 break;
6133
fa9a63c5 6134 case syntaxspec:
1fb352e0 6135 case notsyntaxspec:
b18215fc 6136 {
19ed5445
PE
6137 boolean not = (re_opcode_t) *(p - 1) == notsyntaxspec;
6138 mcnt = *p++;
dc4a2ee0
PE
6139 DEBUG_PRINT ("EXECUTING %ssyntaxspec %d.\n", not ? "not" : "",
6140 mcnt);
19ed5445
PE
6141 PREFETCH ();
6142#ifdef emacs
6143 {
d1dfb56c
EZ
6144 ssize_t offset = PTR_TO_OFFSET (d);
6145 ssize_t pos1 = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
19ed5445
PE
6146 UPDATE_SYNTAX_TABLE (pos1);
6147 }
25fe55af 6148#endif
19ed5445
PE
6149 {
6150 int len;
6151 re_wchar_t c;
b18215fc 6152
19ed5445
PE
6153 GET_CHAR_AFTER (c, d, len);
6154 if ((SYNTAX (c) != (enum syntaxcode) mcnt) ^ not)
6155 goto fail;
6156 d += len;
6157 }
b18215fc 6158 }
8fb31792 6159 break;
fa9a63c5 6160
b18215fc 6161#ifdef emacs
1fb352e0 6162 case before_dot:
dc4a2ee0 6163 DEBUG_PRINT ("EXECUTING before_dot.\n");
1fb352e0 6164 if (PTR_BYTE_POS (d) >= PT_BYTE)
fa9a63c5 6165 goto fail;
b18215fc
RS
6166 break;
6167
1fb352e0 6168 case at_dot:
dc4a2ee0 6169 DEBUG_PRINT ("EXECUTING at_dot.\n");
1fb352e0
SM
6170 if (PTR_BYTE_POS (d) != PT_BYTE)
6171 goto fail;
6172 break;
b18215fc 6173
1fb352e0 6174 case after_dot:
dc4a2ee0 6175 DEBUG_PRINT ("EXECUTING after_dot.\n");
1fb352e0
SM
6176 if (PTR_BYTE_POS (d) <= PT_BYTE)
6177 goto fail;
e318085a 6178 break;
fa9a63c5 6179
1fb352e0 6180 case categoryspec:
b18215fc 6181 case notcategoryspec:
b18215fc 6182 {
8fb31792
PE
6183 boolean not = (re_opcode_t) *(p - 1) == notcategoryspec;
6184 mcnt = *p++;
dc4a2ee0
PE
6185 DEBUG_PRINT ("EXECUTING %scategoryspec %d.\n",
6186 not ? "not" : "", mcnt);
8fb31792 6187 PREFETCH ();
01618498 6188
8fb31792
PE
6189 {
6190 int len;
6191 re_wchar_t c;
6192 GET_CHAR_AFTER (c, d, len);
6193 if ((!CHAR_HAS_CATEGORY (c, mcnt)) ^ not)
6194 goto fail;
6195 d += len;
6196 }
b18215fc 6197 }
fa9a63c5 6198 break;
5e69f11e 6199
1fb352e0 6200#endif /* emacs */
5e69f11e 6201
0b32bf0e
SM
6202 default:
6203 abort ();
fa9a63c5 6204 }
b18215fc 6205 continue; /* Successfully executed one pattern command; keep going. */
fa9a63c5
RM
6206
6207
6208 /* We goto here if a matching operation fails. */
6209 fail:
5b370c2b 6210 IMMEDIATE_QUIT_CHECK;
fa9a63c5 6211 if (!FAIL_STACK_EMPTY ())
505bde11 6212 {
01618498 6213 re_char *str, *pat;
505bde11 6214 /* A restart point is known. Restore to that state. */
dc4a2ee0 6215 DEBUG_PRINT ("\nFAIL:\n");
0b32bf0e 6216 POP_FAILURE_POINT (str, pat);
7393bcbb 6217 switch (*pat++)
505bde11
SM
6218 {
6219 case on_failure_keep_string_jump:
6220 assert (str == NULL);
6221 goto continue_failure_jump;
6222
0683b6fa
SM
6223 case on_failure_jump_nastyloop:
6224 assert ((re_opcode_t)pat[-2] == no_op);
6225 PUSH_FAILURE_POINT (pat - 2, str);
6226 /* Fallthrough */
6227
505bde11
SM
6228 case on_failure_jump_loop:
6229 case on_failure_jump:
6230 case succeed_n:
6231 d = str;
6232 continue_failure_jump:
6233 EXTRACT_NUMBER_AND_INCR (mcnt, pat);
6234 p = pat + mcnt;
6235 break;
b18215fc 6236
0683b6fa
SM
6237 case no_op:
6238 /* A special frame used for nastyloops. */
6239 goto fail;
6240
505bde11 6241 default:
5e617bc2 6242 abort ();
505bde11 6243 }
fa9a63c5 6244
505bde11 6245 assert (p >= bufp->buffer && p <= pend);
b18215fc 6246
0b32bf0e 6247 if (d >= string1 && d <= end1)
fa9a63c5 6248 dend = end_match_1;
0b32bf0e 6249 }
fa9a63c5 6250 else
0b32bf0e 6251 break; /* Matching at this starting point really fails. */
fa9a63c5
RM
6252 } /* for (;;) */
6253
6254 if (best_regs_set)
6255 goto restore_best_regs;
6256
6257 FREE_VARIABLES ();
6258
b18215fc 6259 return -1; /* Failure to match. */
dc4a2ee0 6260}
fa9a63c5
RM
6261\f
6262/* Subroutine definitions for re_match_2. */
6263
fa9a63c5
RM
6264/* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
6265 bytes; nonzero otherwise. */
5e69f11e 6266
fa9a63c5 6267static int
d1dfb56c 6268bcmp_translate (const re_char *s1, const re_char *s2, register ssize_t len,
438105ed 6269 RE_TRANSLATE_TYPE translate, const int target_multibyte)
fa9a63c5 6270{
2d1675e4
SM
6271 register re_char *p1 = s1, *p2 = s2;
6272 re_char *p1_end = s1 + len;
6273 re_char *p2_end = s2 + len;
e934739e 6274
4bb91c68
SM
6275 /* FIXME: Checking both p1 and p2 presumes that the two strings might have
6276 different lengths, but relying on a single `len' would break this. -sm */
6277 while (p1 < p1_end && p2 < p2_end)
fa9a63c5 6278 {
e934739e 6279 int p1_charlen, p2_charlen;
01618498 6280 re_wchar_t p1_ch, p2_ch;
e934739e 6281
6fdd04b0
KH
6282 GET_CHAR_AFTER (p1_ch, p1, p1_charlen);
6283 GET_CHAR_AFTER (p2_ch, p2, p2_charlen);
e934739e
RS
6284
6285 if (RE_TRANSLATE (translate, p1_ch)
6286 != RE_TRANSLATE (translate, p2_ch))
bc192b5b 6287 return 1;
e934739e
RS
6288
6289 p1 += p1_charlen, p2 += p2_charlen;
fa9a63c5 6290 }
e934739e
RS
6291
6292 if (p1 != p1_end || p2 != p2_end)
6293 return 1;
6294
fa9a63c5
RM
6295 return 0;
6296}
6297\f
6298/* Entry points for GNU code. */
6299
6300/* re_compile_pattern is the GNU regular expression compiler: it
6301 compiles PATTERN (of length SIZE) and puts the result in BUFP.
6302 Returns 0 if the pattern was valid, otherwise an error string.
5e69f11e 6303
fa9a63c5
RM
6304 Assumes the `allocated' (and perhaps `buffer') and `translate' fields
6305 are set in BUFP on entry.
5e69f11e 6306
b18215fc 6307 We call regex_compile to do the actual compilation. */
fa9a63c5
RM
6308
6309const char *
d1dfb56c
EZ
6310re_compile_pattern (const char *pattern, size_t length,
6311 struct re_pattern_buffer *bufp)
fa9a63c5
RM
6312{
6313 reg_errcode_t ret;
5e69f11e 6314
fa9a63c5
RM
6315 /* GNU code is written to assume at least RE_NREGS registers will be set
6316 (and at least one extra will be -1). */
6317 bufp->regs_allocated = REGS_UNALLOCATED;
5e69f11e 6318
fa9a63c5
RM
6319 /* And GNU code determines whether or not to get register information
6320 by passing null for the REGS argument to re_match, etc., not by
6321 setting no_sub. */
6322 bufp->no_sub = 0;
5e69f11e 6323
4bb91c68 6324 ret = regex_compile ((re_char*) pattern, length, re_syntax_options, bufp);
fa9a63c5
RM
6325
6326 if (!ret)
6327 return NULL;
6328 return gettext (re_error_msgid[(int) ret]);
5e69f11e 6329}
c0f9ea08 6330WEAK_ALIAS (__re_compile_pattern, re_compile_pattern)
fa9a63c5 6331\f
b18215fc
RS
6332/* Entry points compatible with 4.2 BSD regex library. We don't define
6333 them unless specifically requested. */
fa9a63c5 6334
0b32bf0e 6335#if defined _REGEX_RE_COMP || defined _LIBC
fa9a63c5
RM
6336
6337/* BSD has one and only one pattern buffer. */
6338static struct re_pattern_buffer re_comp_buf;
6339
6340char *
0b32bf0e 6341# ifdef _LIBC
48afdd44
RM
6342/* Make these definitions weak in libc, so POSIX programs can redefine
6343 these names if they don't use our functions, and still use
6344 regcomp/regexec below without link errors. */
6345weak_function
0b32bf0e 6346# endif
31011111 6347re_comp (const char *s)
fa9a63c5
RM
6348{
6349 reg_errcode_t ret;
5e69f11e 6350
fa9a63c5
RM
6351 if (!s)
6352 {
6353 if (!re_comp_buf.buffer)
0b32bf0e 6354 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
a60198e5 6355 return (char *) gettext ("No previous regular expression");
fa9a63c5
RM
6356 return 0;
6357 }
6358
6359 if (!re_comp_buf.buffer)
6360 {
38182d90 6361 re_comp_buf.buffer = malloc (200);
fa9a63c5 6362 if (re_comp_buf.buffer == NULL)
0b32bf0e
SM
6363 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6364 return (char *) gettext (re_error_msgid[(int) REG_ESPACE]);
fa9a63c5
RM
6365 re_comp_buf.allocated = 200;
6366
38182d90 6367 re_comp_buf.fastmap = malloc (1 << BYTEWIDTH);
fa9a63c5 6368 if (re_comp_buf.fastmap == NULL)
a60198e5
SM
6369 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6370 return (char *) gettext (re_error_msgid[(int) REG_ESPACE]);
fa9a63c5
RM
6371 }
6372
6373 /* Since `re_exec' always passes NULL for the `regs' argument, we
6374 don't need to initialize the pattern buffer fields which affect it. */
6375
fa9a63c5 6376 ret = regex_compile (s, strlen (s), re_syntax_options, &re_comp_buf);
5e69f11e 6377
fa9a63c5
RM
6378 if (!ret)
6379 return NULL;
6380
6381 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6382 return (char *) gettext (re_error_msgid[(int) ret]);
6383}
6384
6385
31011111 6386int
0b32bf0e 6387# ifdef _LIBC
48afdd44 6388weak_function
0b32bf0e 6389# endif
d1dfb56c 6390re_exec (const char *s)
fa9a63c5 6391{
d1dfb56c 6392 const size_t len = strlen (s);
908589fd
AS
6393 return (re_search (&re_comp_buf, s, len, 0, len, (struct re_registers *) 0)
6394 >= 0);
fa9a63c5
RM
6395}
6396#endif /* _REGEX_RE_COMP */
6397\f
6398/* POSIX.2 functions. Don't define these for Emacs. */
6399
6400#ifndef emacs
6401
6402/* regcomp takes a regular expression as a string and compiles it.
6403
b18215fc 6404 PREG is a regex_t *. We do not expect any fields to be initialized,
fa9a63c5
RM
6405 since POSIX says we shouldn't. Thus, we set
6406
6407 `buffer' to the compiled pattern;
6408 `used' to the length of the compiled pattern;
6409 `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
6410 REG_EXTENDED bit in CFLAGS is set; otherwise, to
6411 RE_SYNTAX_POSIX_BASIC;
c0f9ea08
SM
6412 `fastmap' to an allocated space for the fastmap;
6413 `fastmap_accurate' to zero;
fa9a63c5
RM
6414 `re_nsub' to the number of subexpressions in PATTERN.
6415
6416 PATTERN is the address of the pattern string.
6417
6418 CFLAGS is a series of bits which affect compilation.
6419
6420 If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
6421 use POSIX basic syntax.
6422
6423 If REG_NEWLINE is set, then . and [^...] don't match newline.
6424 Also, regexec will try a match beginning after every newline.
6425
6426 If REG_ICASE is set, then we considers upper- and lowercase
6427 versions of letters to be equivalent when matching.
6428
6429 If REG_NOSUB is set, then when PREG is passed to regexec, that
6430 routine will report only success or failure, and nothing about the
6431 registers.
6432
b18215fc 6433 It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
fa9a63c5
RM
6434 the return codes and their meanings.) */
6435
d1dfb56c 6436reg_errcode_t
d2762c86
DN
6437regcomp (regex_t *__restrict preg, const char *__restrict pattern,
6438 int cflags)
fa9a63c5
RM
6439{
6440 reg_errcode_t ret;
4bb91c68 6441 reg_syntax_t syntax
fa9a63c5
RM
6442 = (cflags & REG_EXTENDED) ?
6443 RE_SYNTAX_POSIX_EXTENDED : RE_SYNTAX_POSIX_BASIC;
6444
6445 /* regex_compile will allocate the space for the compiled pattern. */
6446 preg->buffer = 0;
6447 preg->allocated = 0;
6448 preg->used = 0;
5e69f11e 6449
c0f9ea08 6450 /* Try to allocate space for the fastmap. */
38182d90 6451 preg->fastmap = malloc (1 << BYTEWIDTH);
5e69f11e 6452
fa9a63c5
RM
6453 if (cflags & REG_ICASE)
6454 {
6455 unsigned i;
5e69f11e 6456
38182d90 6457 preg->translate = malloc (CHAR_SET_SIZE * sizeof *preg->translate);
fa9a63c5 6458 if (preg->translate == NULL)
0b32bf0e 6459 return (int) REG_ESPACE;
fa9a63c5
RM
6460
6461 /* Map uppercase characters to corresponding lowercase ones. */
6462 for (i = 0; i < CHAR_SET_SIZE; i++)
4bb91c68 6463 preg->translate[i] = ISUPPER (i) ? TOLOWER (i) : i;
fa9a63c5
RM
6464 }
6465 else
6466 preg->translate = NULL;
6467
6468 /* If REG_NEWLINE is set, newlines are treated differently. */
6469 if (cflags & REG_NEWLINE)
6470 { /* REG_NEWLINE implies neither . nor [^...] match newline. */
6471 syntax &= ~RE_DOT_NEWLINE;
6472 syntax |= RE_HAT_LISTS_NOT_NEWLINE;
fa9a63c5
RM
6473 }
6474 else
c0f9ea08 6475 syntax |= RE_NO_NEWLINE_ANCHOR;
fa9a63c5
RM
6476
6477 preg->no_sub = !!(cflags & REG_NOSUB);
6478
5e69f11e 6479 /* POSIX says a null character in the pattern terminates it, so we
fa9a63c5 6480 can use strlen here in compiling the pattern. */
4bb91c68 6481 ret = regex_compile ((re_char*) pattern, strlen (pattern), syntax, preg);
5e69f11e 6482
fa9a63c5
RM
6483 /* POSIX doesn't distinguish between an unmatched open-group and an
6484 unmatched close-group: both are REG_EPAREN. */
c0f9ea08
SM
6485 if (ret == REG_ERPAREN)
6486 ret = REG_EPAREN;
6487
6488 if (ret == REG_NOERROR && preg->fastmap)
6489 { /* Compute the fastmap now, since regexec cannot modify the pattern
6490 buffer. */
6491 re_compile_fastmap (preg);
6492 if (preg->can_be_null)
6493 { /* The fastmap can't be used anyway. */
6494 free (preg->fastmap);
6495 preg->fastmap = NULL;
6496 }
6497 }
d1dfb56c 6498 return ret;
fa9a63c5 6499}
c0f9ea08 6500WEAK_ALIAS (__regcomp, regcomp)
fa9a63c5
RM
6501
6502
6503/* regexec searches for a given pattern, specified by PREG, in the
6504 string STRING.
5e69f11e 6505
fa9a63c5 6506 If NMATCH is zero or REG_NOSUB was set in the cflags argument to
b18215fc 6507 `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
fa9a63c5
RM
6508 least NMATCH elements, and we set them to the offsets of the
6509 corresponding matched substrings.
5e69f11e 6510
fa9a63c5
RM
6511 EFLAGS specifies `execution flags' which affect matching: if
6512 REG_NOTBOL is set, then ^ does not match at the beginning of the
6513 string; if REG_NOTEOL is set, then $ does not match at the end.
5e69f11e 6514
fa9a63c5
RM
6515 We return 0 if we find a match and REG_NOMATCH if not. */
6516
d1dfb56c 6517reg_errcode_t
d2762c86
DN
6518regexec (const regex_t *__restrict preg, const char *__restrict string,
6519 size_t nmatch, regmatch_t pmatch[__restrict_arr], int eflags)
fa9a63c5 6520{
31011111 6521 regoff_t ret;
fa9a63c5
RM
6522 struct re_registers regs;
6523 regex_t private_preg;
d1dfb56c 6524 size_t len = strlen (string);
c0f9ea08 6525 boolean want_reg_info = !preg->no_sub && nmatch > 0 && pmatch;
fa9a63c5
RM
6526
6527 private_preg = *preg;
5e69f11e 6528
fa9a63c5
RM
6529 private_preg.not_bol = !!(eflags & REG_NOTBOL);
6530 private_preg.not_eol = !!(eflags & REG_NOTEOL);
5e69f11e 6531
fa9a63c5
RM
6532 /* The user has told us exactly how many registers to return
6533 information about, via `nmatch'. We have to pass that on to the
b18215fc 6534 matching routines. */
fa9a63c5 6535 private_preg.regs_allocated = REGS_FIXED;
5e69f11e 6536
fa9a63c5
RM
6537 if (want_reg_info)
6538 {
6539 regs.num_regs = nmatch;
4bb91c68
SM
6540 regs.start = TALLOC (nmatch * 2, regoff_t);
6541 if (regs.start == NULL)
d1dfb56c 6542 return REG_NOMATCH;
4bb91c68 6543 regs.end = regs.start + nmatch;
fa9a63c5
RM
6544 }
6545
c0f9ea08
SM
6546 /* Instead of using not_eol to implement REG_NOTEOL, we could simply
6547 pass (&private_preg, string, len + 1, 0, len, ...) pretending the string
6548 was a little bit longer but still only matching the real part.
6549 This works because the `endline' will check for a '\n' and will find a
6550 '\0', correctly deciding that this is not the end of a line.
6551 But it doesn't work out so nicely for REG_NOTBOL, since we don't have
6552 a convenient '\0' there. For all we know, the string could be preceded
6553 by '\n' which would throw things off. */
6554
fa9a63c5
RM
6555 /* Perform the searching operation. */
6556 ret = re_search (&private_preg, string, len,
0b32bf0e
SM
6557 /* start: */ 0, /* range: */ len,
6558 want_reg_info ? &regs : (struct re_registers *) 0);
5e69f11e 6559
fa9a63c5
RM
6560 /* Copy the register information to the POSIX structure. */
6561 if (want_reg_info)
6562 {
6563 if (ret >= 0)
0b32bf0e
SM
6564 {
6565 unsigned r;
fa9a63c5 6566
0b32bf0e
SM
6567 for (r = 0; r < nmatch; r++)
6568 {
6569 pmatch[r].rm_so = regs.start[r];
6570 pmatch[r].rm_eo = regs.end[r];
6571 }
6572 }
fa9a63c5 6573
b18215fc 6574 /* If we needed the temporary register info, free the space now. */
fa9a63c5 6575 free (regs.start);
fa9a63c5
RM
6576 }
6577
6578 /* We want zero return to mean success, unlike `re_search'. */
d1dfb56c 6579 return ret >= 0 ? REG_NOERROR : REG_NOMATCH;
fa9a63c5 6580}
c0f9ea08 6581WEAK_ALIAS (__regexec, regexec)
fa9a63c5
RM
6582
6583
ec869672
JR
6584/* Returns a message corresponding to an error code, ERR_CODE, returned
6585 from either regcomp or regexec. We don't use PREG here.
6586
6587 ERR_CODE was previously called ERRCODE, but that name causes an
6588 error with msvc8 compiler. */
fa9a63c5
RM
6589
6590size_t
d2762c86 6591regerror (int err_code, const regex_t *preg, char *errbuf, size_t errbuf_size)
fa9a63c5
RM
6592{
6593 const char *msg;
6594 size_t msg_size;
6595
ec869672
JR
6596 if (err_code < 0
6597 || err_code >= (sizeof (re_error_msgid) / sizeof (re_error_msgid[0])))
5e69f11e 6598 /* Only error codes returned by the rest of the code should be passed
b18215fc 6599 to this routine. If we are given anything else, or if other regex
fa9a63c5
RM
6600 code generates an invalid error code, then the program has a bug.
6601 Dump core so we can fix it. */
6602 abort ();
6603
ec869672 6604 msg = gettext (re_error_msgid[err_code]);
fa9a63c5
RM
6605
6606 msg_size = strlen (msg) + 1; /* Includes the null. */
5e69f11e 6607
fa9a63c5
RM
6608 if (errbuf_size != 0)
6609 {
6610 if (msg_size > errbuf_size)
0b32bf0e 6611 {
e99a530f 6612 memcpy (errbuf, msg, errbuf_size - 1);
0b32bf0e
SM
6613 errbuf[errbuf_size - 1] = 0;
6614 }
fa9a63c5 6615 else
0b32bf0e 6616 strcpy (errbuf, msg);
fa9a63c5
RM
6617 }
6618
6619 return msg_size;
6620}
c0f9ea08 6621WEAK_ALIAS (__regerror, regerror)
fa9a63c5
RM
6622
6623
6624/* Free dynamically allocated space used by PREG. */
6625
6626void
d2762c86 6627regfree (regex_t *preg)
fa9a63c5 6628{
c2cd06e6 6629 free (preg->buffer);
fa9a63c5 6630 preg->buffer = NULL;
5e69f11e 6631
fa9a63c5
RM
6632 preg->allocated = 0;
6633 preg->used = 0;
6634
c2cd06e6 6635 free (preg->fastmap);
fa9a63c5
RM
6636 preg->fastmap = NULL;
6637 preg->fastmap_accurate = 0;
6638
c2cd06e6 6639 free (preg->translate);
fa9a63c5
RM
6640 preg->translate = NULL;
6641}
c0f9ea08 6642WEAK_ALIAS (__regfree, regfree)
fa9a63c5
RM
6643
6644#endif /* not emacs */