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