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