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