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