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