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