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