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