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