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