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