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