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