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