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