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