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