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