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