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