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