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