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