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