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