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