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