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