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. */
|
82
|
1128 int re_max_failures = 20000;
|
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. */
|
82
|
1351 /* We used to use (num_regs - 1), which is the number of registers
|
|
1352 this regexp will save; but that was changed to 5
|
|
1353 to avoid stack overflow for a regexp with lots of parens. */
|
|
1354 #define MAX_FAILURE_ITEMS (5 * NUM_REG_ITEMS + NUM_NONREG_ITEMS)
|
0
|
1355
|
|
1356 /* We actually push this many items. */
|
|
1357 #define NUM_FAILURE_ITEMS \
|
|
1358 ((highest_active_reg - lowest_active_reg + 1) * NUM_REG_ITEMS \
|
|
1359 + NUM_NONREG_ITEMS)
|
|
1360
|
|
1361 /* How many items can still be added to the stack without overflowing it. */
|
|
1362 #define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail)
|
|
1363
|
|
1364
|
|
1365 /* Pops what PUSH_FAIL_STACK pushes.
|
|
1366
|
|
1367 We restore into the parameters, all of which should be lvalues:
|
|
1368 STR -- the saved data position.
|
|
1369 PAT -- the saved pattern position.
|
|
1370 LOW_REG, HIGH_REG -- the highest and lowest active registers.
|
|
1371 REGSTART, REGEND -- arrays of string positions.
|
|
1372 REG_INFO -- array of information about each subexpression.
|
|
1373
|
|
1374 Also assumes the variables `fail_stack' and (if debugging), `bufp',
|
|
1375 `pend', `string1', `size1', `string2', and `size2'. */
|
|
1376
|
|
1377 #define POP_FAILURE_POINT(str, pat, low_reg, high_reg, regstart, regend, reg_info)\
|
|
1378 { \
|
|
1379 DEBUG_STATEMENT (fail_stack_elt_t ffailure_id;) \
|
|
1380 int this_reg; \
|
|
1381 CONST unsigned char *string_temp; \
|
|
1382 \
|
|
1383 assert (!FAIL_STACK_EMPTY ()); \
|
|
1384 \
|
|
1385 /* Remove failure points and point to how many regs pushed. */ \
|
|
1386 DEBUG_PRINT1 ("POP_FAILURE_POINT:\n"); \
|
|
1387 DEBUG_PRINT2 (" Before pop, next avail: %d\n", fail_stack.avail); \
|
|
1388 DEBUG_PRINT2 (" size: %d\n", fail_stack.size); \
|
|
1389 \
|
|
1390 assert (fail_stack.avail >= NUM_NONREG_ITEMS); \
|
|
1391 \
|
|
1392 DEBUG_POP (&ffailure_id.integer); \
|
|
1393 DEBUG_PRINT2 (" Popping failure id: %u\n", \
|
|
1394 * (unsigned int *) &ffailure_id); \
|
|
1395 \
|
|
1396 /* If the saved string location is NULL, it came from an \
|
|
1397 on_failure_keep_string_jump opcode, and we want to throw away the \
|
|
1398 saved NULL, thus retaining our current position in the string. */ \
|
|
1399 string_temp = POP_FAILURE_POINTER (); \
|
|
1400 if (string_temp != NULL) \
|
|
1401 str = (CONST char *) string_temp; \
|
|
1402 \
|
|
1403 DEBUG_PRINT2 (" Popping string 0x%lx: `", (unsigned long) str); \
|
|
1404 DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2); \
|
|
1405 DEBUG_PRINT1 ("'\n"); \
|
|
1406 \
|
|
1407 pat = (unsigned char *) POP_FAILURE_POINTER (); \
|
|
1408 DEBUG_PRINT2 (" Popping pattern 0x%lx: ", (unsigned long) pat); \
|
|
1409 DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend); \
|
|
1410 \
|
|
1411 /* Restore register info. */ \
|
|
1412 high_reg = (unsigned) POP_FAILURE_INT (); \
|
|
1413 DEBUG_PRINT2 (" Popping high active reg: %d\n", high_reg); \
|
|
1414 \
|
|
1415 low_reg = (unsigned) POP_FAILURE_INT (); \
|
|
1416 DEBUG_PRINT2 (" Popping low active reg: %d\n", low_reg); \
|
|
1417 \
|
|
1418 for (this_reg = high_reg; this_reg >= low_reg; this_reg--) \
|
|
1419 { \
|
|
1420 DEBUG_PRINT2 (" Popping reg: %d\n", this_reg); \
|
|
1421 \
|
|
1422 reg_info[this_reg].word = POP_FAILURE_ELT (); \
|
|
1423 DEBUG_PRINT2 (" info: 0x%lx\n", \
|
|
1424 * (unsigned long *) ®_info[this_reg]); \
|
|
1425 \
|
|
1426 regend[this_reg] = (CONST char *) POP_FAILURE_POINTER (); \
|
|
1427 DEBUG_PRINT2 (" end: 0x%lx\n", \
|
|
1428 (unsigned long) regend[this_reg]); \
|
|
1429 \
|
|
1430 regstart[this_reg] = (CONST char *) POP_FAILURE_POINTER (); \
|
|
1431 DEBUG_PRINT2 (" start: 0x%lx\n", \
|
|
1432 (unsigned long) regstart[this_reg]); \
|
|
1433 } \
|
|
1434 \
|
|
1435 set_regs_matched_done = 0; \
|
|
1436 DEBUG_STATEMENT (nfailure_points_popped++); \
|
|
1437 } /* POP_FAILURE_POINT */
|
|
1438
|
|
1439
|
|
1440
|
|
1441 /* Structure for per-register (a.k.a. per-group) information.
|
|
1442 Other register information, such as the
|
|
1443 starting and ending positions (which are addresses), and the list of
|
|
1444 inner groups (which is a bits list) are maintained in separate
|
|
1445 variables.
|
|
1446
|
|
1447 We are making a (strictly speaking) nonportable assumption here: that
|
|
1448 the compiler will pack our bit fields into something that fits into
|
|
1449 the type of `word', i.e., is something that fits into one item on the
|
|
1450 failure stack. */
|
|
1451
|
|
1452 typedef union
|
|
1453 {
|
|
1454 fail_stack_elt_t word;
|
|
1455 struct
|
|
1456 {
|
|
1457 /* This field is one if this group can match the empty string,
|
|
1458 zero if not. If not yet determined, `MATCH_NULL_UNSET_VALUE'. */
|
|
1459 #define MATCH_NULL_UNSET_VALUE 3
|
|
1460 unsigned match_null_string_p : 2;
|
|
1461 unsigned is_active : 1;
|
|
1462 unsigned matched_something : 1;
|
|
1463 unsigned ever_matched_something : 1;
|
|
1464 } bits;
|
|
1465 } register_info_type;
|
|
1466
|
|
1467 #define REG_MATCH_NULL_STRING_P(R) ((R).bits.match_null_string_p)
|
|
1468 #define IS_ACTIVE(R) ((R).bits.is_active)
|
|
1469 #define MATCHED_SOMETHING(R) ((R).bits.matched_something)
|
|
1470 #define EVER_MATCHED_SOMETHING(R) ((R).bits.ever_matched_something)
|
|
1471
|
|
1472
|
|
1473 /* Call this when have matched a real character; it sets `matched' flags
|
|
1474 for the subexpressions which we are currently inside. Also records
|
|
1475 that those subexprs have matched. */
|
|
1476 #define SET_REGS_MATCHED() \
|
|
1477 do \
|
|
1478 { \
|
|
1479 if (!set_regs_matched_done) \
|
|
1480 { \
|
|
1481 unsigned r; \
|
|
1482 set_regs_matched_done = 1; \
|
|
1483 for (r = lowest_active_reg; r <= highest_active_reg; r++) \
|
|
1484 { \
|
|
1485 MATCHED_SOMETHING (reg_info[r]) \
|
|
1486 = EVER_MATCHED_SOMETHING (reg_info[r]) \
|
|
1487 = 1; \
|
|
1488 } \
|
|
1489 } \
|
|
1490 } \
|
|
1491 while (0)
|
|
1492
|
|
1493 /* Registers are set to a sentinel when they haven't yet matched. */
|
|
1494 static char reg_unset_dummy;
|
|
1495 #define REG_UNSET_VALUE (®_unset_dummy)
|
|
1496 #define REG_UNSET(e) ((e) == REG_UNSET_VALUE)
|
|
1497
|
|
1498 /* Subroutine declarations and macros for regex_compile. */
|
|
1499
|
|
1500 /* Fetch the next character in the uncompiled pattern---translating it
|
|
1501 if necessary. Also cast from a signed character in the constant
|
|
1502 string passed to us by the user to an unsigned char that we can use
|
|
1503 as an array index (in, e.g., `translate'). */
|
|
1504 #define PATFETCH(c) \
|
|
1505 do {if (p == pend) return REG_EEND; \
|
|
1506 assert (p < pend); \
|
|
1507 c = (unsigned char) *p++; \
|
|
1508 if (translate) c = (unsigned char) translate[c]; \
|
|
1509 } while (0)
|
|
1510
|
|
1511 /* Fetch the next character in the uncompiled pattern, with no
|
|
1512 translation. */
|
|
1513 #define PATFETCH_RAW(c) \
|
|
1514 do {if (p == pend) return REG_EEND; \
|
|
1515 assert (p < pend); \
|
|
1516 c = (unsigned char) *p++; \
|
|
1517 } while (0)
|
|
1518
|
|
1519 /* Go backwards one character in the pattern. */
|
|
1520 #define PATUNFETCH p--
|
|
1521
|
70
|
1522 #ifdef MULE
|
|
1523
|
|
1524 #define PATFETCH_EXTENDED(emch) \
|
|
1525 do {if (p == pend) return REG_EEND; \
|
|
1526 assert (p < pend); \
|
|
1527 emch = charptr_emchar ((CONST Bufbyte *) p); \
|
|
1528 INC_CHARPTR (p); \
|
|
1529 if (translate && emch < 0x80) \
|
|
1530 emch = (Emchar) (unsigned char) translate[emch]; \
|
|
1531 } while (0)
|
|
1532
|
|
1533 #define PATFETCH_RAW_EXTENDED(emch) \
|
|
1534 do {if (p == pend) return REG_EEND; \
|
|
1535 assert (p < pend); \
|
|
1536 emch = charptr_emchar ((CONST Bufbyte *) p); \
|
|
1537 INC_CHARPTR (p); \
|
|
1538 } while (0)
|
|
1539
|
|
1540 #define PATUNFETCH_EXTENDED DEC_CHARPTR (p)
|
|
1541
|
|
1542 #define PATFETCH_EITHER(emch) \
|
|
1543 do { \
|
|
1544 if (has_extended_chars) \
|
|
1545 PATFETCH_EXTENDED (emch); \
|
|
1546 else \
|
|
1547 PATFETCH (emch); \
|
|
1548 } while (0)
|
|
1549
|
|
1550 #define PATFETCH_RAW_EITHER(emch) \
|
|
1551 do { \
|
|
1552 if (has_extended_chars) \
|
|
1553 PATFETCH_RAW_EXTENDED (emch); \
|
|
1554 else \
|
|
1555 PATFETCH_RAW (emch); \
|
|
1556 } while (0)
|
|
1557
|
|
1558 #define PATUNFETCH_EITHER \
|
|
1559 do { \
|
|
1560 if (has_extended_chars) \
|
|
1561 PATUNFETCH_EXTENDED (emch); \
|
|
1562 else \
|
|
1563 PATUNFETCH (emch); \
|
|
1564 } while (0)
|
|
1565
|
|
1566 #else /* not MULE */
|
|
1567
|
0
|
1568 #define PATFETCH_EITHER(emch) PATFETCH (emch)
|
|
1569 #define PATFETCH_RAW_EITHER(emch) PATFETCH_RAW (emch)
|
|
1570 #define PATUNFETCH_EITHER PATUNFETCH
|
|
1571
|
70
|
1572 #endif /* not MULE */
|
0
|
1573
|
|
1574 /* If `translate' is non-null, return translate[D], else just D. We
|
|
1575 cast the subscript to translate because some data is declared as
|
|
1576 `char *', to avoid warnings when a string constant is passed. But
|
|
1577 when we use a character as a subscript we must make it unsigned. */
|
|
1578 #define TRANSLATE(d) (translate ? translate[(unsigned char) (d)] : (d))
|
|
1579
|
70
|
1580 #ifdef MULE
|
|
1581
|
|
1582 #define TRANSLATE_EXTENDED_UNSAFE(emch) \
|
|
1583 (translate && emch < 0x80 ? translate[emch] : (emch))
|
|
1584
|
|
1585 #endif
|
0
|
1586
|
|
1587 /* Macros for outputting the compiled pattern into `buffer'. */
|
|
1588
|
|
1589 /* If the buffer isn't allocated when it comes in, use this. */
|
|
1590 #define INIT_BUF_SIZE 32
|
|
1591
|
|
1592 /* Make sure we have at least N more bytes of space in buffer. */
|
|
1593 #define GET_BUFFER_SPACE(n) \
|
|
1594 while (b - bufp->buffer + (n) > bufp->allocated) \
|
|
1595 EXTEND_BUFFER ()
|
|
1596
|
|
1597 /* Make sure we have one more byte of buffer space and then add C to it. */
|
|
1598 #define BUF_PUSH(c) \
|
|
1599 do { \
|
|
1600 GET_BUFFER_SPACE (1); \
|
|
1601 *b++ = (unsigned char) (c); \
|
|
1602 } while (0)
|
|
1603
|
|
1604
|
|
1605 /* Ensure we have two more bytes of buffer space and then append C1 and C2. */
|
|
1606 #define BUF_PUSH_2(c1, c2) \
|
|
1607 do { \
|
|
1608 GET_BUFFER_SPACE (2); \
|
|
1609 *b++ = (unsigned char) (c1); \
|
|
1610 *b++ = (unsigned char) (c2); \
|
|
1611 } while (0)
|
|
1612
|
|
1613
|
|
1614 /* As with BUF_PUSH_2, except for three bytes. */
|
|
1615 #define BUF_PUSH_3(c1, c2, c3) \
|
|
1616 do { \
|
|
1617 GET_BUFFER_SPACE (3); \
|
|
1618 *b++ = (unsigned char) (c1); \
|
|
1619 *b++ = (unsigned char) (c2); \
|
|
1620 *b++ = (unsigned char) (c3); \
|
|
1621 } while (0)
|
|
1622
|
|
1623
|
|
1624 /* Store a jump with opcode OP at LOC to location TO. We store a
|
|
1625 relative address offset by the three bytes the jump itself occupies. */
|
|
1626 #define STORE_JUMP(op, loc, to) \
|
|
1627 store_op1 (op, loc, (to) - (loc) - 3)
|
|
1628
|
|
1629 /* Likewise, for a two-argument jump. */
|
|
1630 #define STORE_JUMP2(op, loc, to, arg) \
|
|
1631 store_op2 (op, loc, (to) - (loc) - 3, arg)
|
|
1632
|
|
1633 /* Like `STORE_JUMP', but for inserting. Assume `b' is the buffer end. */
|
|
1634 #define INSERT_JUMP(op, loc, to) \
|
|
1635 insert_op1 (op, loc, (to) - (loc) - 3, b)
|
|
1636
|
|
1637 /* Like `STORE_JUMP2', but for inserting. Assume `b' is the buffer end. */
|
|
1638 #define INSERT_JUMP2(op, loc, to, arg) \
|
|
1639 insert_op2 (op, loc, (to) - (loc) - 3, arg, b)
|
|
1640
|
|
1641
|
|
1642 /* This is not an arbitrary limit: the arguments which represent offsets
|
|
1643 into the pattern are two bytes long. So if 2^16 bytes turns out to
|
|
1644 be too small, many things would have to change. */
|
|
1645 #define MAX_BUF_SIZE (1L << 16)
|
|
1646
|
|
1647
|
|
1648 /* Extend the buffer by twice its current size via realloc and
|
|
1649 reset the pointers that pointed into the old block to point to the
|
|
1650 correct places in the new one. If extending the buffer results in it
|
|
1651 being larger than MAX_BUF_SIZE, then flag memory exhausted. */
|
|
1652 #define EXTEND_BUFFER() \
|
|
1653 do { \
|
|
1654 unsigned char *old_buffer = bufp->buffer; \
|
|
1655 if (bufp->allocated == MAX_BUF_SIZE) \
|
|
1656 return REG_ESIZE; \
|
|
1657 bufp->allocated <<= 1; \
|
|
1658 if (bufp->allocated > MAX_BUF_SIZE) \
|
|
1659 bufp->allocated = MAX_BUF_SIZE; \
|
|
1660 bufp->buffer = (unsigned char *) realloc (bufp->buffer, bufp->allocated);\
|
|
1661 if (bufp->buffer == NULL) \
|
|
1662 return REG_ESPACE; \
|
|
1663 /* If the buffer moved, move all the pointers into it. */ \
|
|
1664 if (old_buffer != bufp->buffer) \
|
|
1665 { \
|
|
1666 b = (b - old_buffer) + bufp->buffer; \
|
|
1667 begalt = (begalt - old_buffer) + bufp->buffer; \
|
|
1668 if (fixup_alt_jump) \
|
|
1669 fixup_alt_jump = (fixup_alt_jump - old_buffer) + bufp->buffer;\
|
|
1670 if (laststart) \
|
|
1671 laststart = (laststart - old_buffer) + bufp->buffer; \
|
|
1672 if (pending_exact) \
|
|
1673 pending_exact = (pending_exact - old_buffer) + bufp->buffer; \
|
|
1674 } \
|
|
1675 } while (0)
|
|
1676
|
|
1677
|
|
1678 /* Since we have one byte reserved for the register number argument to
|
|
1679 {start,stop}_memory, the maximum number of groups we can report
|
|
1680 things about is what fits in that byte. */
|
|
1681 #define MAX_REGNUM 255
|
|
1682
|
|
1683 /* But patterns can have more than `MAX_REGNUM' registers. We just
|
|
1684 ignore the excess. */
|
|
1685 typedef unsigned regnum_t;
|
|
1686
|
|
1687
|
|
1688 /* Macros for the compile stack. */
|
|
1689
|
|
1690 /* Since offsets can go either forwards or backwards, this type needs to
|
|
1691 be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1. */
|
|
1692 typedef int pattern_offset_t;
|
|
1693
|
|
1694 typedef struct
|
|
1695 {
|
|
1696 pattern_offset_t begalt_offset;
|
|
1697 pattern_offset_t fixup_alt_jump;
|
|
1698 pattern_offset_t inner_group_offset;
|
|
1699 pattern_offset_t laststart_offset;
|
|
1700 regnum_t regnum;
|
|
1701 } compile_stack_elt_t;
|
|
1702
|
|
1703
|
|
1704 typedef struct
|
|
1705 {
|
|
1706 compile_stack_elt_t *stack;
|
|
1707 unsigned size;
|
|
1708 unsigned avail; /* Offset of next open position. */
|
|
1709 } compile_stack_type;
|
|
1710
|
|
1711
|
|
1712 #define INIT_COMPILE_STACK_SIZE 32
|
|
1713
|
|
1714 #define COMPILE_STACK_EMPTY (compile_stack.avail == 0)
|
|
1715 #define COMPILE_STACK_FULL (compile_stack.avail == compile_stack.size)
|
|
1716
|
|
1717 /* The next available element. */
|
|
1718 #define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
|
|
1719
|
|
1720
|
|
1721 /* Set the bit for character C in a bit vector. */
|
|
1722 #define SET_LIST_BIT(c) \
|
|
1723 (b[((unsigned char) (c)) / BYTEWIDTH] \
|
|
1724 |= 1 << (((unsigned char) c) % BYTEWIDTH))
|
|
1725
|
70
|
1726 #ifdef MULE
|
|
1727
|
|
1728 /* Set the "bit" for character C in a range table. */
|
|
1729 #define SET_RANGETAB_BIT(c) put_range_table (rtab, c, c, Qt)
|
|
1730
|
|
1731 /* Set the "bit" for character c in the appropriate table. */
|
|
1732 #define SET_EITHER_BIT(c) \
|
|
1733 do { \
|
|
1734 if (has_extended_chars) \
|
|
1735 SET_RANGETAB_BIT (c); \
|
|
1736 else \
|
|
1737 SET_LIST_BIT (c); \
|
|
1738 } while (0)
|
|
1739
|
|
1740 #else /* not MULE */
|
|
1741
|
0
|
1742 #define SET_EITHER_BIT(c) SET_LIST_BIT (c)
|
|
1743
|
70
|
1744 #endif
|
0
|
1745
|
|
1746
|
|
1747 /* Get the next unsigned number in the uncompiled pattern. */
|
|
1748 #define GET_UNSIGNED_NUMBER(num) \
|
|
1749 { if (p != pend) \
|
|
1750 { \
|
|
1751 PATFETCH (c); \
|
|
1752 while (ISDIGIT (c)) \
|
|
1753 { \
|
|
1754 if (num < 0) \
|
|
1755 num = 0; \
|
|
1756 num = num * 10 + c - '0'; \
|
|
1757 if (p == pend) \
|
|
1758 break; \
|
|
1759 PATFETCH (c); \
|
|
1760 } \
|
|
1761 } \
|
|
1762 }
|
|
1763
|
|
1764 #define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
|
|
1765
|
|
1766 #define IS_CHAR_CLASS(string) \
|
|
1767 (STREQ (string, "alpha") || STREQ (string, "upper") \
|
|
1768 || STREQ (string, "lower") || STREQ (string, "digit") \
|
|
1769 || STREQ (string, "alnum") || STREQ (string, "xdigit") \
|
|
1770 || STREQ (string, "space") || STREQ (string, "print") \
|
|
1771 || STREQ (string, "punct") || STREQ (string, "graph") \
|
|
1772 || STREQ (string, "cntrl") || STREQ (string, "blank"))
|
|
1773
|
|
1774 static void store_op1 (re_opcode_t op, unsigned char *loc, int arg);
|
|
1775 static void store_op2 (re_opcode_t op, unsigned char *loc, int arg1, int arg2);
|
|
1776 static void insert_op1 (re_opcode_t op, unsigned char *loc, int arg,
|
|
1777 unsigned char *end);
|
|
1778 static void insert_op2 (re_opcode_t op, unsigned char *loc, int arg1, int arg2,
|
|
1779 unsigned char *end);
|
|
1780 static boolean at_begline_loc_p (CONST char *pattern, CONST char *p,
|
|
1781 reg_syntax_t syntax);
|
|
1782 static boolean at_endline_loc_p (CONST char *p, CONST char *pend, int syntax);
|
|
1783 static boolean group_in_compile_stack (compile_stack_type compile_stack,
|
|
1784 regnum_t regnum);
|
|
1785 static reg_errcode_t compile_range (CONST char **p_ptr, CONST char *pend,
|
|
1786 char *translate, reg_syntax_t syntax,
|
|
1787 unsigned char *b);
|
70
|
1788 #ifdef MULE
|
|
1789 static reg_errcode_t compile_extended_range (CONST char **p_ptr,
|
|
1790 CONST char *pend,
|
|
1791 char *translate,
|
|
1792 reg_syntax_t syntax,
|
|
1793 Lisp_Object rtab);
|
|
1794 #endif
|
0
|
1795 static boolean group_match_null_string_p (unsigned char **p,
|
|
1796 unsigned char *end,
|
|
1797 register_info_type *reg_info);
|
|
1798 static boolean alt_match_null_string_p (unsigned char *p, unsigned char *end,
|
|
1799 register_info_type *reg_info);
|
|
1800 static boolean common_op_match_null_string_p (unsigned char **p,
|
|
1801 unsigned char *end,
|
|
1802 register_info_type *reg_info);
|
|
1803 static int bcmp_translate (CONST unsigned char *s1, CONST unsigned char *s2,
|
|
1804 register int len, char *translate);
|
|
1805 static int re_match_2_internal (struct re_pattern_buffer *bufp,
|
|
1806 CONST char *string1, int size1,
|
|
1807 CONST char *string2, int size2, int pos,
|
|
1808 struct re_registers *regs, int stop);
|
|
1809
|
|
1810 #ifndef MATCH_MAY_ALLOCATE
|
|
1811
|
|
1812 /* If we cannot allocate large objects within re_match_2_internal,
|
|
1813 we make the fail stack and register vectors global.
|
|
1814 The fail stack, we grow to the maximum size when a regexp
|
|
1815 is compiled.
|
|
1816 The register vectors, we adjust in size each time we
|
|
1817 compile a regexp, according to the number of registers it needs. */
|
|
1818
|
|
1819 static fail_stack_type fail_stack;
|
|
1820
|
|
1821 /* Size with which the following vectors are currently allocated.
|
|
1822 That is so we can make them bigger as needed,
|
|
1823 but never make them smaller. */
|
|
1824 static int regs_allocated_size;
|
|
1825
|
|
1826 static CONST char ** regstart, ** regend;
|
|
1827 static CONST char ** old_regstart, ** old_regend;
|
|
1828 static CONST char **best_regstart, **best_regend;
|
|
1829 static register_info_type *reg_info;
|
|
1830 static CONST char **reg_dummy;
|
|
1831 static register_info_type *reg_info_dummy;
|
|
1832
|
|
1833 /* Make the register vectors big enough for NUM_REGS registers,
|
|
1834 but don't make them smaller. */
|
|
1835
|
|
1836 static
|
|
1837 regex_grow_registers (int num_regs)
|
|
1838 {
|
|
1839 if (num_regs > regs_allocated_size)
|
|
1840 {
|
|
1841 RETALLOC_IF (regstart, num_regs, CONST char *);
|
|
1842 RETALLOC_IF (regend, num_regs, CONST char *);
|
|
1843 RETALLOC_IF (old_regstart, num_regs, CONST char *);
|
|
1844 RETALLOC_IF (old_regend, num_regs, CONST char *);
|
|
1845 RETALLOC_IF (best_regstart, num_regs, CONST char *);
|
|
1846 RETALLOC_IF (best_regend, num_regs, CONST char *);
|
|
1847 RETALLOC_IF (reg_info, num_regs, register_info_type);
|
|
1848 RETALLOC_IF (reg_dummy, num_regs, CONST char *);
|
|
1849 RETALLOC_IF (reg_info_dummy, num_regs, register_info_type);
|
|
1850
|
|
1851 regs_allocated_size = num_regs;
|
|
1852 }
|
|
1853 }
|
|
1854
|
|
1855 #endif /* not MATCH_MAY_ALLOCATE */
|
|
1856
|
|
1857 /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
|
|
1858 Returns one of error codes defined in `regex.h', or zero for success.
|
|
1859
|
|
1860 Assumes the `allocated' (and perhaps `buffer') and `translate'
|
|
1861 fields are set in BUFP on entry.
|
|
1862
|
|
1863 If it succeeds, results are put in BUFP (if it returns an error, the
|
|
1864 contents of BUFP are undefined):
|
|
1865 `buffer' is the compiled pattern;
|
|
1866 `syntax' is set to SYNTAX;
|
|
1867 `used' is set to the length of the compiled pattern;
|
|
1868 `fastmap_accurate' is zero;
|
|
1869 `re_nsub' is the number of subexpressions in PATTERN;
|
|
1870 `not_bol' and `not_eol' are zero;
|
|
1871
|
|
1872 The `fastmap' and `newline_anchor' fields are neither
|
|
1873 examined nor set. */
|
|
1874
|
|
1875 /* Return, freeing storage we allocated. */
|
|
1876 #define FREE_STACK_RETURN(value) \
|
|
1877 return (free (compile_stack.stack), value)
|
|
1878
|
|
1879 static reg_errcode_t
|
|
1880 regex_compile (CONST char *pattern, int size, reg_syntax_t syntax,
|
|
1881 struct re_pattern_buffer *bufp)
|
|
1882 {
|
|
1883 /* We fetch characters from PATTERN here. We declare these as int
|
|
1884 (or possibly long) so that chars above 127 can be used as
|
|
1885 array indices. The macros that fetch a character from the pattern
|
|
1886 make sure to coerce to unsigned char before assigning, so we won't
|
|
1887 get bitten by negative numbers here. */
|
|
1888 /* XEmacs change: used to be unsigned char. */
|
|
1889 register EMACS_INT c, c1;
|
|
1890
|
|
1891 /* A random temporary spot in PATTERN. */
|
|
1892 CONST char *p1;
|
|
1893
|
|
1894 /* Points to the end of the buffer, where we should append. */
|
|
1895 register unsigned char *b;
|
|
1896
|
|
1897 /* Keeps track of unclosed groups. */
|
|
1898 compile_stack_type compile_stack;
|
|
1899
|
|
1900 /* Points to the current (ending) position in the pattern. */
|
|
1901 CONST char *p = pattern;
|
|
1902 CONST char *pend = pattern + size;
|
|
1903
|
|
1904 /* How to translate the characters in the pattern. */
|
|
1905 char *translate = bufp->translate;
|
|
1906
|
|
1907 /* Address of the count-byte of the most recently inserted `exactn'
|
|
1908 command. This makes it possible to tell if a new exact-match
|
|
1909 character can be added to that command or if the character requires
|
|
1910 a new `exactn' command. */
|
|
1911 unsigned char *pending_exact = 0;
|
|
1912
|
|
1913 /* Address of start of the most recently finished expression.
|
|
1914 This tells, e.g., postfix * where to find the start of its
|
|
1915 operand. Reset at the beginning of groups and alternatives. */
|
|
1916 unsigned char *laststart = 0;
|
|
1917
|
|
1918 /* Address of beginning of regexp, or inside of last group. */
|
|
1919 unsigned char *begalt;
|
|
1920
|
|
1921 /* Place in the uncompiled pattern (i.e., the {) to
|
|
1922 which to go back if the interval is invalid. */
|
|
1923 CONST char *beg_interval;
|
|
1924
|
|
1925 /* Address of the place where a forward jump should go to the end of
|
|
1926 the containing expression. Each alternative of an `or' -- except the
|
|
1927 last -- ends with a forward jump of this sort. */
|
|
1928 unsigned char *fixup_alt_jump = 0;
|
|
1929
|
|
1930 /* Counts open-groups as they are encountered. Remembered for the
|
|
1931 matching close-group on the compile stack, so the same register
|
|
1932 number is put in the stop_memory as the start_memory. */
|
|
1933 regnum_t regnum = 0;
|
|
1934
|
|
1935 #ifdef DEBUG
|
|
1936 DEBUG_PRINT1 ("\nCompiling pattern: ");
|
|
1937 if (debug)
|
|
1938 {
|
|
1939 unsigned debug_count;
|
|
1940
|
|
1941 for (debug_count = 0; debug_count < size; debug_count++)
|
|
1942 putchar (pattern[debug_count]);
|
|
1943 putchar ('\n');
|
|
1944 }
|
|
1945 #endif /* DEBUG */
|
|
1946
|
|
1947 /* Initialize the compile stack. */
|
|
1948 compile_stack.stack = TALLOC (INIT_COMPILE_STACK_SIZE, compile_stack_elt_t);
|
|
1949 if (compile_stack.stack == NULL)
|
|
1950 return REG_ESPACE;
|
|
1951
|
|
1952 compile_stack.size = INIT_COMPILE_STACK_SIZE;
|
|
1953 compile_stack.avail = 0;
|
|
1954
|
|
1955 /* Initialize the pattern buffer. */
|
|
1956 bufp->syntax = syntax;
|
|
1957 bufp->fastmap_accurate = 0;
|
|
1958 bufp->not_bol = bufp->not_eol = 0;
|
|
1959
|
|
1960 /* Set `used' to zero, so that if we return an error, the pattern
|
|
1961 printer (for debugging) will think there's no pattern. We reset it
|
|
1962 at the end. */
|
|
1963 bufp->used = 0;
|
|
1964
|
|
1965 /* Always count groups, whether or not bufp->no_sub is set. */
|
|
1966 bufp->re_nsub = 0;
|
|
1967
|
|
1968 #if !defined (emacs) && !defined (SYNTAX_TABLE)
|
|
1969 /* Initialize the syntax table. */
|
|
1970 init_syntax_once ();
|
|
1971 #endif
|
|
1972
|
|
1973 if (bufp->allocated == 0)
|
|
1974 {
|
|
1975 if (bufp->buffer)
|
|
1976 { /* If zero allocated, but buffer is non-null, try to realloc
|
|
1977 enough space. This loses if buffer's address is bogus, but
|
|
1978 that is the user's responsibility. */
|
|
1979 RETALLOC (bufp->buffer, INIT_BUF_SIZE, unsigned char);
|
|
1980 }
|
|
1981 else
|
|
1982 { /* Caller did not allocate a buffer. Do it for them. */
|
|
1983 bufp->buffer = TALLOC (INIT_BUF_SIZE, unsigned char);
|
|
1984 }
|
|
1985 if (!bufp->buffer) FREE_STACK_RETURN (REG_ESPACE);
|
|
1986
|
|
1987 bufp->allocated = INIT_BUF_SIZE;
|
|
1988 }
|
|
1989
|
|
1990 begalt = b = bufp->buffer;
|
|
1991
|
|
1992 /* Loop through the uncompiled pattern until we're at the end. */
|
|
1993 while (p != pend)
|
|
1994 {
|
|
1995 PATFETCH (c);
|
|
1996
|
|
1997 switch (c)
|
|
1998 {
|
|
1999 case '^':
|
|
2000 {
|
|
2001 if ( /* If at start of pattern, it's an operator. */
|
|
2002 p == pattern + 1
|
|
2003 /* If context independent, it's an operator. */
|
|
2004 || syntax & RE_CONTEXT_INDEP_ANCHORS
|
|
2005 /* Otherwise, depends on what's come before. */
|
|
2006 || at_begline_loc_p (pattern, p, syntax))
|
|
2007 BUF_PUSH (begline);
|
|
2008 else
|
|
2009 goto normal_char;
|
|
2010 }
|
|
2011 break;
|
|
2012
|
|
2013
|
|
2014 case '$':
|
|
2015 {
|
|
2016 if ( /* If at end of pattern, it's an operator. */
|
|
2017 p == pend
|
|
2018 /* If context independent, it's an operator. */
|
|
2019 || syntax & RE_CONTEXT_INDEP_ANCHORS
|
|
2020 /* Otherwise, depends on what's next. */
|
|
2021 || at_endline_loc_p (p, pend, syntax))
|
|
2022 BUF_PUSH (endline);
|
|
2023 else
|
|
2024 goto normal_char;
|
|
2025 }
|
|
2026 break;
|
|
2027
|
|
2028
|
|
2029 case '+':
|
|
2030 case '?':
|
|
2031 if ((syntax & RE_BK_PLUS_QM)
|
|
2032 || (syntax & RE_LIMITED_OPS))
|
|
2033 goto normal_char;
|
|
2034 handle_plus:
|
|
2035 case '*':
|
|
2036 /* If there is no previous pattern... */
|
|
2037 if (!laststart)
|
|
2038 {
|
|
2039 if (syntax & RE_CONTEXT_INVALID_OPS)
|
|
2040 FREE_STACK_RETURN (REG_BADRPT);
|
|
2041 else if (!(syntax & RE_CONTEXT_INDEP_OPS))
|
|
2042 goto normal_char;
|
|
2043 }
|
|
2044
|
|
2045 {
|
|
2046 /* Are we optimizing this jump? */
|
|
2047 boolean keep_string_p = false;
|
|
2048
|
|
2049 /* 1 means zero (many) matches is allowed. */
|
|
2050 char zero_times_ok = 0, many_times_ok = 0;
|
|
2051
|
|
2052 /* If there is a sequence of repetition chars, collapse it
|
|
2053 down to just one (the right one). We can't combine
|
|
2054 interval operators with these because of, e.g., `a{2}*',
|
|
2055 which should only match an even number of `a's. */
|
|
2056
|
|
2057 for (;;)
|
|
2058 {
|
|
2059 zero_times_ok |= c != '+';
|
|
2060 many_times_ok |= c != '?';
|
|
2061
|
|
2062 if (p == pend)
|
|
2063 break;
|
|
2064
|
|
2065 PATFETCH (c);
|
|
2066
|
|
2067 if (c == '*'
|
|
2068 || (!(syntax & RE_BK_PLUS_QM) && (c == '+' || c == '?')))
|
|
2069 ;
|
|
2070
|
|
2071 else if (syntax & RE_BK_PLUS_QM && c == '\\')
|
|
2072 {
|
|
2073 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
|
|
2074
|
|
2075 PATFETCH (c1);
|
|
2076 if (!(c1 == '+' || c1 == '?'))
|
|
2077 {
|
|
2078 PATUNFETCH;
|
|
2079 PATUNFETCH;
|
|
2080 break;
|
|
2081 }
|
|
2082
|
|
2083 c = c1;
|
|
2084 }
|
|
2085 else
|
|
2086 {
|
|
2087 PATUNFETCH;
|
|
2088 break;
|
|
2089 }
|
|
2090
|
|
2091 /* If we get here, we found another repeat character. */
|
|
2092 }
|
|
2093
|
|
2094 /* Star, etc. applied to an empty pattern is equivalent
|
|
2095 to an empty pattern. */
|
|
2096 if (!laststart)
|
|
2097 break;
|
|
2098
|
|
2099 /* Now we know whether or not zero matches is allowed
|
|
2100 and also whether or not two or more matches is allowed. */
|
|
2101 if (many_times_ok)
|
|
2102 { /* More than one repetition is allowed, so put in at the
|
|
2103 end a backward relative jump from `b' to before the next
|
|
2104 jump we're going to put in below (which jumps from
|
|
2105 laststart to after this jump).
|
|
2106
|
|
2107 But if we are at the `*' in the exact sequence `.*\n',
|
|
2108 insert an unconditional jump backwards to the .,
|
|
2109 instead of the beginning of the loop. This way we only
|
|
2110 push a failure point once, instead of every time
|
|
2111 through the loop. */
|
|
2112 assert (p - 1 > pattern);
|
|
2113
|
|
2114 /* Allocate the space for the jump. */
|
|
2115 GET_BUFFER_SPACE (3);
|
|
2116
|
|
2117 /* We know we are not at the first character of the pattern,
|
|
2118 because laststart was nonzero. And we've already
|
|
2119 incremented `p', by the way, to be the character after
|
|
2120 the `*'. Do we have to do something analogous here
|
|
2121 for null bytes, because of RE_DOT_NOT_NULL? */
|
|
2122 if (TRANSLATE (*(p - 2)) == TRANSLATE ('.')
|
|
2123 && zero_times_ok
|
|
2124 && p < pend && TRANSLATE (*p) == TRANSLATE ('\n')
|
|
2125 && !(syntax & RE_DOT_NEWLINE))
|
|
2126 { /* We have .*\n. */
|
|
2127 STORE_JUMP (jump, b, laststart);
|
|
2128 keep_string_p = true;
|
|
2129 }
|
|
2130 else
|
|
2131 /* Anything else. */
|
|
2132 STORE_JUMP (maybe_pop_jump, b, laststart - 3);
|
|
2133
|
|
2134 /* We've added more stuff to the buffer. */
|
|
2135 b += 3;
|
|
2136 }
|
|
2137
|
|
2138 /* On failure, jump from laststart to b + 3, which will be the
|
|
2139 end of the buffer after this jump is inserted. */
|
|
2140 GET_BUFFER_SPACE (3);
|
|
2141 INSERT_JUMP (keep_string_p ? on_failure_keep_string_jump
|
|
2142 : on_failure_jump,
|
|
2143 laststart, b + 3);
|
|
2144 pending_exact = 0;
|
|
2145 b += 3;
|
|
2146
|
|
2147 if (!zero_times_ok)
|
|
2148 {
|
|
2149 /* At least one repetition is required, so insert a
|
|
2150 `dummy_failure_jump' before the initial
|
|
2151 `on_failure_jump' instruction of the loop. This
|
|
2152 effects a skip over that instruction the first time
|
|
2153 we hit that loop. */
|
|
2154 GET_BUFFER_SPACE (3);
|
|
2155 INSERT_JUMP (dummy_failure_jump, laststart, laststart + 6);
|
|
2156 b += 3;
|
|
2157 }
|
|
2158 }
|
|
2159 break;
|
|
2160
|
|
2161
|
|
2162 case '.':
|
|
2163 laststart = b;
|
|
2164 BUF_PUSH (anychar);
|
|
2165 break;
|
|
2166
|
|
2167
|
|
2168 case '[':
|
|
2169 {
|
|
2170 /* XEmacs change: this whole section */
|
|
2171 boolean had_char_class = false;
|
70
|
2172 #ifdef MULE
|
|
2173 boolean has_extended_chars = false;
|
|
2174 REGISTER Lisp_Object rtab = Qnil;
|
|
2175 #endif
|
0
|
2176
|
|
2177 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
|
|
2178
|
|
2179 /* Ensure that we have enough space to push a charset: the
|
|
2180 opcode, the length count, and the bitset; 34 bytes in all. */
|
|
2181 GET_BUFFER_SPACE (34);
|
|
2182
|
|
2183 laststart = b;
|
|
2184
|
|
2185 /* We test `*p == '^' twice, instead of using an if
|
|
2186 statement, so we only need one BUF_PUSH. */
|
|
2187 BUF_PUSH (*p == '^' ? charset_not : charset);
|
|
2188 if (*p == '^')
|
|
2189 p++;
|
|
2190
|
|
2191 /* Remember the first position in the bracket expression. */
|
|
2192 p1 = p;
|
|
2193
|
|
2194 /* Push the number of bytes in the bitmap. */
|
|
2195 BUF_PUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
|
|
2196
|
|
2197 /* Clear the whole map. */
|
|
2198 bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH);
|
|
2199
|
|
2200 /* charset_not matches newline according to a syntax bit. */
|
|
2201 if ((re_opcode_t) b[-2] == charset_not
|
|
2202 && (syntax & RE_HAT_LISTS_NOT_NEWLINE))
|
|
2203 SET_LIST_BIT ('\n');
|
|
2204
|
70
|
2205 #ifdef MULE
|
|
2206 start_over_with_extended:
|
|
2207 if (has_extended_chars)
|
|
2208 {
|
|
2209 /* There are extended chars here, which means we need to start
|
|
2210 over and shift to unified range-table format. */
|
|
2211 if (b[-2] == charset)
|
|
2212 b[-2] = charset_mule;
|
|
2213 else
|
|
2214 b[-2] = charset_mule_not;
|
|
2215 b--;
|
|
2216 p = p1; /* go back to the beginning of the charset, after
|
|
2217 a possible ^. */
|
|
2218 rtab = Vthe_lisp_rangetab;
|
|
2219 Fclear_range_table (rtab);
|
|
2220
|
|
2221 /* charset_not matches newline according to a syntax bit. */
|
|
2222 if ((re_opcode_t) b[-1] == charset_mule_not
|
|
2223 && (syntax & RE_HAT_LISTS_NOT_NEWLINE))
|
|
2224 SET_EITHER_BIT ('\n');
|
|
2225 }
|
|
2226 #endif /* MULE */
|
|
2227
|
0
|
2228 /* Read in characters and ranges, setting map bits. */
|
|
2229 for (;;)
|
|
2230 {
|
|
2231 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
|
|
2232
|
|
2233 PATFETCH_EITHER (c);
|
|
2234
|
70
|
2235 #ifdef MULE
|
|
2236 if (c >= 0x80 && !has_extended_chars)
|
|
2237 {
|
|
2238 has_extended_chars = 1;
|
|
2239 /* Frumble-bumble, we've found some extended chars.
|
|
2240 Need to start over, process everything using
|
|
2241 the general extended-char mechanism, and need
|
|
2242 to use charset_mule and charset_mule_not instead
|
|
2243 of charset and charset_not. */
|
|
2244 goto start_over_with_extended;
|
|
2245 }
|
|
2246 #endif /* MULE */
|
0
|
2247 /* \ might escape characters inside [...] and [^...]. */
|
|
2248 if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) && c == '\\')
|
|
2249 {
|
|
2250 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
|
|
2251
|
|
2252 PATFETCH_EITHER (c1);
|
70
|
2253 #ifdef MULE
|
|
2254 if (c1 >= 0x80 && !has_extended_chars)
|
|
2255 {
|
|
2256 has_extended_chars = 1;
|
|
2257 goto start_over_with_extended;
|
|
2258 }
|
|
2259 #endif /* MULE */
|
0
|
2260 SET_EITHER_BIT (c1);
|
|
2261 continue;
|
|
2262 }
|
|
2263
|
|
2264 /* Could be the end of the bracket expression. If it's
|
|
2265 not (i.e., when the bracket expression is `[]' so
|
|
2266 far), the ']' character bit gets set way below. */
|
|
2267 if (c == ']' && p != p1 + 1)
|
|
2268 break;
|
|
2269
|
|
2270 /* Look ahead to see if it's a range when the last thing
|
|
2271 was a character class. */
|
|
2272 if (had_char_class && c == '-' && *p != ']')
|
|
2273 FREE_STACK_RETURN (REG_ERANGE);
|
|
2274
|
|
2275 /* Look ahead to see if it's a range when the last thing
|
|
2276 was a character: if this is a hyphen not at the
|
|
2277 beginning or the end of a list, then it's the range
|
|
2278 operator. */
|
|
2279 if (c == '-'
|
|
2280 && !(p - 2 >= pattern && p[-2] == '[')
|
|
2281 && !(p - 3 >= pattern && p[-3] == '[' && p[-2] == '^')
|
|
2282 && *p != ']')
|
|
2283 {
|
|
2284 reg_errcode_t ret;
|
|
2285
|
70
|
2286 #ifdef MULE
|
|
2287 if (* (unsigned char *) p >= 0x80 && !has_extended_chars)
|
|
2288 {
|
|
2289 has_extended_chars = 1;
|
|
2290 goto start_over_with_extended;
|
|
2291 }
|
|
2292 if (has_extended_chars)
|
|
2293 ret = compile_extended_range (&p, pend, translate,
|
|
2294 syntax, rtab);
|
|
2295 else
|
|
2296 #endif /* MULE */
|
|
2297 ret = compile_range (&p, pend, translate, syntax, b);
|
0
|
2298 if (ret != REG_NOERROR) FREE_STACK_RETURN (ret);
|
|
2299 }
|
|
2300
|
|
2301 else if (p[0] == '-' && p[1] != ']')
|
|
2302 { /* This handles ranges made up of characters only. */
|
|
2303 reg_errcode_t ret;
|
|
2304
|
|
2305 /* Move past the `-'. */
|
|
2306 PATFETCH (c1);
|
|
2307
|
70
|
2308 #ifdef MULE
|
|
2309 if (* (unsigned char *) p >= 0x80 && !has_extended_chars)
|
|
2310 {
|
|
2311 has_extended_chars = 1;
|
|
2312 goto start_over_with_extended;
|
|
2313 }
|
|
2314 if (has_extended_chars)
|
|
2315 ret = compile_extended_range (&p, pend, translate,
|
|
2316 syntax, rtab);
|
|
2317 else
|
|
2318 #endif /* MULE */
|
|
2319 ret = compile_range (&p, pend, translate, syntax, b);
|
0
|
2320 if (ret != REG_NOERROR) FREE_STACK_RETURN (ret);
|
|
2321 }
|
|
2322
|
|
2323 /* See if we're at the beginning of a possible character
|
|
2324 class. */
|
|
2325
|
|
2326 else if (syntax & RE_CHAR_CLASSES && c == '[' && *p == ':')
|
|
2327 { /* Leave room for the null. */
|
|
2328 char str[CHAR_CLASS_MAX_LENGTH + 1];
|
|
2329
|
|
2330 PATFETCH (c);
|
|
2331 c1 = 0;
|
|
2332
|
|
2333 /* If pattern is `[[:'. */
|
|
2334 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
|
|
2335
|
|
2336 for (;;)
|
|
2337 {
|
|
2338 /* Do not do PATFETCH_EITHER() here. We want
|
|
2339 to just see if the bytes match particular
|
|
2340 strings, and we put them all back if not.
|
|
2341
|
|
2342 #### May need to be changed once trt tables
|
|
2343 are working. */
|
|
2344 PATFETCH (c);
|
|
2345 if (c == ':' || c == ']' || p == pend
|
|
2346 || c1 == CHAR_CLASS_MAX_LENGTH)
|
|
2347 break;
|
|
2348 str[c1++] = c;
|
|
2349 }
|
|
2350 str[c1] = '\0';
|
|
2351
|
|
2352 /* If isn't a word bracketed by `[:' and:`]':
|
|
2353 undo the ending character, the letters, and leave
|
|
2354 the leading `:' and `[' (but set bits for them). */
|
|
2355 if (c == ':' && *p == ']')
|
|
2356 {
|
|
2357 int ch;
|
|
2358 boolean is_alnum = STREQ (str, "alnum");
|
|
2359 boolean is_alpha = STREQ (str, "alpha");
|
|
2360 boolean is_blank = STREQ (str, "blank");
|
|
2361 boolean is_cntrl = STREQ (str, "cntrl");
|
|
2362 boolean is_digit = STREQ (str, "digit");
|
|
2363 boolean is_graph = STREQ (str, "graph");
|
|
2364 boolean is_lower = STREQ (str, "lower");
|
|
2365 boolean is_print = STREQ (str, "print");
|
|
2366 boolean is_punct = STREQ (str, "punct");
|
|
2367 boolean is_space = STREQ (str, "space");
|
|
2368 boolean is_upper = STREQ (str, "upper");
|
|
2369 boolean is_xdigit = STREQ (str, "xdigit");
|
|
2370
|
|
2371 if (!IS_CHAR_CLASS (str))
|
|
2372 FREE_STACK_RETURN (REG_ECTYPE);
|
|
2373
|
|
2374 /* Throw away the ] at the end of the character
|
|
2375 class. */
|
|
2376 PATFETCH (c);
|
|
2377
|
|
2378 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
|
|
2379
|
|
2380 for (ch = 0; ch < 1 << BYTEWIDTH; ch++)
|
|
2381 {
|
|
2382 /* This was split into 3 if's to
|
|
2383 avoid an arbitrary limit in some compiler. */
|
|
2384 if ( (is_alnum && ISALNUM (ch))
|
|
2385 || (is_alpha && ISALPHA (ch))
|
|
2386 || (is_blank && ISBLANK (ch))
|
|
2387 || (is_cntrl && ISCNTRL (ch)))
|
|
2388 SET_EITHER_BIT (ch);
|
|
2389 if ( (is_digit && ISDIGIT (ch))
|
|
2390 || (is_graph && ISGRAPH (ch))
|
|
2391 || (is_lower && ISLOWER (ch))
|
|
2392 || (is_print && ISPRINT (ch)))
|
|
2393 SET_EITHER_BIT (ch);
|
|
2394 if ( (is_punct && ISPUNCT (ch))
|
|
2395 || (is_space && ISSPACE (ch))
|
|
2396 || (is_upper && ISUPPER (ch))
|
|
2397 || (is_xdigit && ISXDIGIT (ch)))
|
|
2398 SET_EITHER_BIT (ch);
|
|
2399 }
|
|
2400 had_char_class = true;
|
|
2401 }
|
|
2402 else
|
|
2403 {
|
|
2404 c1++;
|
|
2405 while (c1--)
|
|
2406 PATUNFETCH;
|
|
2407 SET_EITHER_BIT ('[');
|
|
2408 SET_EITHER_BIT (':');
|
|
2409 had_char_class = false;
|
|
2410 }
|
|
2411 }
|
|
2412 else
|
|
2413 {
|
|
2414 had_char_class = false;
|
|
2415 SET_EITHER_BIT (c);
|
|
2416 }
|
|
2417 }
|
|
2418
|
70
|
2419 #ifdef MULE
|
|
2420 if (has_extended_chars)
|
|
2421 {
|
|
2422 /* We have a range table, not a bit vector. */
|
|
2423 int bytes_needed =
|
|
2424 unified_range_table_bytes_needed (rtab);
|
|
2425 GET_BUFFER_SPACE (bytes_needed);
|
|
2426 unified_range_table_copy_data (rtab, b);
|
|
2427 b += unified_range_table_bytes_used (b);
|
|
2428 break;
|
|
2429 }
|
|
2430 #endif /* MULE */
|
0
|
2431 /* Discard any (non)matching list bytes that are all 0 at the
|
|
2432 end of the map. Decrease the map-length byte too. */
|
|
2433 while ((int) b[-1] > 0 && b[b[-1] - 1] == 0)
|
|
2434 b[-1]--;
|
|
2435 b += b[-1];
|
|
2436 }
|
|
2437 break;
|
|
2438
|
|
2439
|
|
2440 case '(':
|
|
2441 if (syntax & RE_NO_BK_PARENS)
|
|
2442 goto handle_open;
|
|
2443 else
|
|
2444 goto normal_char;
|
|
2445
|
|
2446
|
|
2447 case ')':
|
|
2448 if (syntax & RE_NO_BK_PARENS)
|
|
2449 goto handle_close;
|
|
2450 else
|
|
2451 goto normal_char;
|
|
2452
|
|
2453
|
|
2454 case '\n':
|
|
2455 if (syntax & RE_NEWLINE_ALT)
|
|
2456 goto handle_alt;
|
|
2457 else
|
|
2458 goto normal_char;
|
|
2459
|
|
2460
|
|
2461 case '|':
|
|
2462 if (syntax & RE_NO_BK_VBAR)
|
|
2463 goto handle_alt;
|
|
2464 else
|
|
2465 goto normal_char;
|
|
2466
|
|
2467
|
|
2468 case '{':
|
|
2469 if (syntax & RE_INTERVALS && syntax & RE_NO_BK_BRACES)
|
|
2470 goto handle_interval;
|
|
2471 else
|
|
2472 goto normal_char;
|
|
2473
|
|
2474
|
|
2475 case '\\':
|
|
2476 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
|
|
2477
|
|
2478 /* Do not translate the character after the \, so that we can
|
|
2479 distinguish, e.g., \B from \b, even if we normally would
|
|
2480 translate, e.g., B to b. */
|
|
2481 PATFETCH_RAW (c);
|
|
2482
|
|
2483 switch (c)
|
|
2484 {
|
|
2485 case '(':
|
|
2486 if (syntax & RE_NO_BK_PARENS)
|
|
2487 goto normal_backslash;
|
|
2488
|
|
2489 handle_open:
|
|
2490 bufp->re_nsub++;
|
|
2491 regnum++;
|
|
2492
|
|
2493 if (COMPILE_STACK_FULL)
|
|
2494 {
|
|
2495 RETALLOC (compile_stack.stack, compile_stack.size << 1,
|
|
2496 compile_stack_elt_t);
|
|
2497 if (compile_stack.stack == NULL) return REG_ESPACE;
|
|
2498
|
|
2499 compile_stack.size <<= 1;
|
|
2500 }
|
|
2501
|
|
2502 /* These are the values to restore when we hit end of this
|
|
2503 group. They are all relative offsets, so that if the
|
|
2504 whole pattern moves because of realloc, they will still
|
|
2505 be valid. */
|
|
2506 COMPILE_STACK_TOP.begalt_offset = begalt - bufp->buffer;
|
|
2507 COMPILE_STACK_TOP.fixup_alt_jump
|
|
2508 = fixup_alt_jump ? fixup_alt_jump - bufp->buffer + 1 : 0;
|
|
2509 COMPILE_STACK_TOP.laststart_offset = b - bufp->buffer;
|
|
2510 COMPILE_STACK_TOP.regnum = regnum;
|
|
2511
|
|
2512 /* We will eventually replace the 0 with the number of
|
|
2513 groups inner to this one. But do not push a
|
|
2514 start_memory for groups beyond the last one we can
|
|
2515 represent in the compiled pattern. */
|
|
2516 if (regnum <= MAX_REGNUM)
|
|
2517 {
|
|
2518 COMPILE_STACK_TOP.inner_group_offset = b - bufp->buffer + 2;
|
|
2519 BUF_PUSH_3 (start_memory, regnum, 0);
|
|
2520 }
|
|
2521
|
|
2522 compile_stack.avail++;
|
|
2523
|
|
2524 fixup_alt_jump = 0;
|
|
2525 laststart = 0;
|
|
2526 begalt = b;
|
|
2527 /* If we've reached MAX_REGNUM groups, then this open
|
|
2528 won't actually generate any code, so we'll have to
|
|
2529 clear pending_exact explicitly. */
|
|
2530 pending_exact = 0;
|
|
2531 break;
|
|
2532
|
|
2533
|
|
2534 case ')':
|
|
2535 if (syntax & RE_NO_BK_PARENS) goto normal_backslash;
|
|
2536
|
|
2537 if (COMPILE_STACK_EMPTY)
|
|
2538 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
|
|
2539 goto normal_backslash;
|
|
2540 else
|
|
2541 FREE_STACK_RETURN (REG_ERPAREN);
|
|
2542
|
|
2543 handle_close:
|
|
2544 if (fixup_alt_jump)
|
|
2545 { /* Push a dummy failure point at the end of the
|
|
2546 alternative for a possible future
|
|
2547 `pop_failure_jump' to pop. See comments at
|
|
2548 `push_dummy_failure' in `re_match_2'. */
|
|
2549 BUF_PUSH (push_dummy_failure);
|
|
2550
|
|
2551 /* We allocated space for this jump when we assigned
|
|
2552 to `fixup_alt_jump', in the `handle_alt' case below. */
|
|
2553 STORE_JUMP (jump_past_alt, fixup_alt_jump, b - 1);
|
|
2554 }
|
|
2555
|
|
2556 /* See similar code for backslashed left paren above. */
|
|
2557 if (COMPILE_STACK_EMPTY)
|
|
2558 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
|
|
2559 goto normal_char;
|
|
2560 else
|
|
2561 FREE_STACK_RETURN (REG_ERPAREN);
|
|
2562
|
|
2563 /* Since we just checked for an empty stack above, this
|
|
2564 ``can't happen''. */
|
|
2565 assert (compile_stack.avail != 0);
|
|
2566 {
|
|
2567 /* We don't just want to restore into `regnum', because
|
|
2568 later groups should continue to be numbered higher,
|
|
2569 as in `(ab)c(de)' -- the second group is #2. */
|
|
2570 regnum_t this_group_regnum;
|
|
2571
|
|
2572 compile_stack.avail--;
|
|
2573 begalt = bufp->buffer + COMPILE_STACK_TOP.begalt_offset;
|
|
2574 fixup_alt_jump
|
|
2575 = COMPILE_STACK_TOP.fixup_alt_jump
|
|
2576 ? bufp->buffer + COMPILE_STACK_TOP.fixup_alt_jump - 1
|
|
2577 : 0;
|
|
2578 laststart = bufp->buffer + COMPILE_STACK_TOP.laststart_offset;
|
|
2579 this_group_regnum = COMPILE_STACK_TOP.regnum;
|
|
2580 /* If we've reached MAX_REGNUM groups, then this open
|
|
2581 won't actually generate any code, so we'll have to
|
|
2582 clear pending_exact explicitly. */
|
|
2583 pending_exact = 0;
|
|
2584
|
|
2585 /* We're at the end of the group, so now we know how many
|
|
2586 groups were inside this one. */
|
|
2587 if (this_group_regnum <= MAX_REGNUM)
|
|
2588 {
|
|
2589 unsigned char *inner_group_loc
|
|
2590 = bufp->buffer + COMPILE_STACK_TOP.inner_group_offset;
|
|
2591
|
|
2592 *inner_group_loc = regnum - this_group_regnum;
|
|
2593 BUF_PUSH_3 (stop_memory, this_group_regnum,
|
|
2594 regnum - this_group_regnum);
|
|
2595 }
|
|
2596 }
|
|
2597 break;
|
|
2598
|
|
2599
|
|
2600 case '|': /* `\|'. */
|
|
2601 if (syntax & RE_LIMITED_OPS || syntax & RE_NO_BK_VBAR)
|
|
2602 goto normal_backslash;
|
|
2603 handle_alt:
|
|
2604 if (syntax & RE_LIMITED_OPS)
|
|
2605 goto normal_char;
|
|
2606
|
|
2607 /* Insert before the previous alternative a jump which
|
|
2608 jumps to this alternative if the former fails. */
|
|
2609 GET_BUFFER_SPACE (3);
|
|
2610 INSERT_JUMP (on_failure_jump, begalt, b + 6);
|
|
2611 pending_exact = 0;
|
|
2612 b += 3;
|
|
2613
|
|
2614 /* The alternative before this one has a jump after it
|
|
2615 which gets executed if it gets matched. Adjust that
|
|
2616 jump so it will jump to this alternative's analogous
|
|
2617 jump (put in below, which in turn will jump to the next
|
|
2618 (if any) alternative's such jump, etc.). The last such
|
|
2619 jump jumps to the correct final destination. A picture:
|
|
2620 _____ _____
|
|
2621 | | | |
|
|
2622 | v | v
|
|
2623 a | b | c
|
|
2624
|
|
2625 If we are at `b', then fixup_alt_jump right now points to a
|
|
2626 three-byte space after `a'. We'll put in the jump, set
|
|
2627 fixup_alt_jump to right after `b', and leave behind three
|
|
2628 bytes which we'll fill in when we get to after `c'. */
|
|
2629
|
|
2630 if (fixup_alt_jump)
|
|
2631 STORE_JUMP (jump_past_alt, fixup_alt_jump, b);
|
|
2632
|
|
2633 /* Mark and leave space for a jump after this alternative,
|
|
2634 to be filled in later either by next alternative or
|
|
2635 when know we're at the end of a series of alternatives. */
|
|
2636 fixup_alt_jump = b;
|
|
2637 GET_BUFFER_SPACE (3);
|
|
2638 b += 3;
|
|
2639
|
|
2640 laststart = 0;
|
|
2641 begalt = b;
|
|
2642 break;
|
|
2643
|
|
2644
|
|
2645 case '{':
|
|
2646 /* If \{ is a literal. */
|
|
2647 if (!(syntax & RE_INTERVALS)
|
|
2648 /* If we're at `\{' and it's not the open-interval
|
|
2649 operator. */
|
|
2650 || ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
|
|
2651 || (p - 2 == pattern && p == pend))
|
|
2652 goto normal_backslash;
|
|
2653
|
|
2654 handle_interval:
|
|
2655 {
|
|
2656 /* If got here, then the syntax allows intervals. */
|
|
2657
|
|
2658 /* At least (most) this many matches must be made. */
|
|
2659 int lower_bound = -1, upper_bound = -1;
|
|
2660
|
|
2661 beg_interval = p - 1;
|
|
2662
|
|
2663 if (p == pend)
|
|
2664 {
|
|
2665 if (syntax & RE_NO_BK_BRACES)
|
|
2666 goto unfetch_interval;
|
|
2667 else
|
|
2668 FREE_STACK_RETURN (REG_EBRACE);
|
|
2669 }
|
|
2670
|
|
2671 GET_UNSIGNED_NUMBER (lower_bound);
|
|
2672
|
|
2673 if (c == ',')
|
|
2674 {
|
|
2675 GET_UNSIGNED_NUMBER (upper_bound);
|
|
2676 if (upper_bound < 0) upper_bound = RE_DUP_MAX;
|
|
2677 }
|
|
2678 else
|
|
2679 /* Interval such as `{1}' => match exactly once. */
|
|
2680 upper_bound = lower_bound;
|
|
2681
|
|
2682 if (lower_bound < 0 || upper_bound > RE_DUP_MAX
|
|
2683 || lower_bound > upper_bound)
|
|
2684 {
|
|
2685 if (syntax & RE_NO_BK_BRACES)
|
|
2686 goto unfetch_interval;
|
|
2687 else
|
|
2688 FREE_STACK_RETURN (REG_BADBR);
|
|
2689 }
|
|
2690
|
|
2691 if (!(syntax & RE_NO_BK_BRACES))
|
|
2692 {
|
|
2693 if (c != '\\') FREE_STACK_RETURN (REG_EBRACE);
|
|
2694
|
|
2695 PATFETCH (c);
|
|
2696 }
|
|
2697
|
|
2698 if (c != '}')
|
|
2699 {
|
|
2700 if (syntax & RE_NO_BK_BRACES)
|
|
2701 goto unfetch_interval;
|
|
2702 else
|
|
2703 FREE_STACK_RETURN (REG_BADBR);
|
|
2704 }
|
|
2705
|
|
2706 /* We just parsed a valid interval. */
|
|
2707
|
|
2708 /* If it's invalid to have no preceding re. */
|
|
2709 if (!laststart)
|
|
2710 {
|
|
2711 if (syntax & RE_CONTEXT_INVALID_OPS)
|
|
2712 FREE_STACK_RETURN (REG_BADRPT);
|
|
2713 else if (syntax & RE_CONTEXT_INDEP_OPS)
|
|
2714 laststart = b;
|
|
2715 else
|
|
2716 goto unfetch_interval;
|
|
2717 }
|
|
2718
|
|
2719 /* If the upper bound is zero, don't want to succeed at
|
|
2720 all; jump from `laststart' to `b + 3', which will be
|
|
2721 the end of the buffer after we insert the jump. */
|
|
2722 if (upper_bound == 0)
|
|
2723 {
|
|
2724 GET_BUFFER_SPACE (3);
|
|
2725 INSERT_JUMP (jump, laststart, b + 3);
|
|
2726 b += 3;
|
|
2727 }
|
|
2728
|
|
2729 /* Otherwise, we have a nontrivial interval. When
|
|
2730 we're all done, the pattern will look like:
|
|
2731 set_number_at <jump count> <upper bound>
|
|
2732 set_number_at <succeed_n count> <lower bound>
|
|
2733 succeed_n <after jump addr> <succeed_n count>
|
|
2734 <body of loop>
|
|
2735 jump_n <succeed_n addr> <jump count>
|
|
2736 (The upper bound and `jump_n' are omitted if
|
|
2737 `upper_bound' is 1, though.) */
|
|
2738 else
|
|
2739 { /* If the upper bound is > 1, we need to insert
|
|
2740 more at the end of the loop. */
|
|
2741 unsigned nbytes = 10 + (upper_bound > 1) * 10;
|
|
2742
|
|
2743 GET_BUFFER_SPACE (nbytes);
|
|
2744
|
|
2745 /* Initialize lower bound of the `succeed_n', even
|
|
2746 though it will be set during matching by its
|
|
2747 attendant `set_number_at' (inserted next),
|
|
2748 because `re_compile_fastmap' needs to know.
|
|
2749 Jump to the `jump_n' we might insert below. */
|
|
2750 INSERT_JUMP2 (succeed_n, laststart,
|
|
2751 b + 5 + (upper_bound > 1) * 5,
|
|
2752 lower_bound);
|
|
2753 b += 5;
|
|
2754
|
|
2755 /* Code to initialize the lower bound. Insert
|
|
2756 before the `succeed_n'. The `5' is the last two
|
|
2757 bytes of this `set_number_at', plus 3 bytes of
|
|
2758 the following `succeed_n'. */
|
|
2759 insert_op2 (set_number_at, laststart, 5, lower_bound, b);
|
|
2760 b += 5;
|
|
2761
|
|
2762 if (upper_bound > 1)
|
|
2763 { /* More than one repetition is allowed, so
|
|
2764 append a backward jump to the `succeed_n'
|
|
2765 that starts this interval.
|
|
2766
|
|
2767 When we've reached this during matching,
|
|
2768 we'll have matched the interval once, so
|
|
2769 jump back only `upper_bound - 1' times. */
|
|
2770 STORE_JUMP2 (jump_n, b, laststart + 5,
|
|
2771 upper_bound - 1);
|
|
2772 b += 5;
|
|
2773
|
|
2774 /* The location we want to set is the second
|
|
2775 parameter of the `jump_n'; that is `b-2' as
|
|
2776 an absolute address. `laststart' will be
|
|
2777 the `set_number_at' we're about to insert;
|
|
2778 `laststart+3' the number to set, the source
|
|
2779 for the relative address. But we are
|
|
2780 inserting into the middle of the pattern --
|
|
2781 so everything is getting moved up by 5.
|
|
2782 Conclusion: (b - 2) - (laststart + 3) + 5,
|
|
2783 i.e., b - laststart.
|
|
2784
|
|
2785 We insert this at the beginning of the loop
|
|
2786 so that if we fail during matching, we'll
|
|
2787 reinitialize the bounds. */
|
|
2788 insert_op2 (set_number_at, laststart, b - laststart,
|
|
2789 upper_bound - 1, b);
|
|
2790 b += 5;
|
|
2791 }
|
|
2792 }
|
|
2793 pending_exact = 0;
|
|
2794 beg_interval = NULL;
|
|
2795 }
|
|
2796 break;
|
|
2797
|
|
2798 unfetch_interval:
|
|
2799 /* If an invalid interval, match the characters as literals. */
|
|
2800 assert (beg_interval);
|
|
2801 p = beg_interval;
|
|
2802 beg_interval = NULL;
|
|
2803
|
|
2804 /* normal_char and normal_backslash need `c'. */
|
|
2805 PATFETCH (c);
|
|
2806
|
|
2807 if (!(syntax & RE_NO_BK_BRACES))
|
|
2808 {
|
|
2809 if (p > pattern && p[-1] == '\\')
|
|
2810 goto normal_backslash;
|
|
2811 }
|
|
2812 goto normal_char;
|
|
2813
|
|
2814 #ifdef emacs
|
|
2815 /* There is no way to specify the before_dot and after_dot
|
|
2816 operators. rms says this is ok. --karl */
|
|
2817 case '=':
|
|
2818 BUF_PUSH (at_dot);
|
|
2819 break;
|
|
2820
|
|
2821 case 's':
|
|
2822 laststart = b;
|
|
2823 PATFETCH (c);
|
|
2824 /* XEmacs addition */
|
|
2825 if (c >= 0x80 || syntax_spec_code[c] == 0377)
|
|
2826 FREE_STACK_RETURN (REG_ESYNTAX);
|
|
2827 BUF_PUSH_2 (syntaxspec, syntax_spec_code[c]);
|
|
2828 break;
|
|
2829
|
|
2830 case 'S':
|
|
2831 laststart = b;
|
|
2832 PATFETCH (c);
|
|
2833 /* XEmacs addition */
|
|
2834 if (c >= 0x80 || syntax_spec_code[c] == 0377)
|
|
2835 FREE_STACK_RETURN (REG_ESYNTAX);
|
|
2836 BUF_PUSH_2 (notsyntaxspec, syntax_spec_code[c]);
|
|
2837 break;
|
|
2838 #endif /* emacs */
|
|
2839
|
|
2840
|
|
2841 case 'w':
|
|
2842 laststart = b;
|
|
2843 BUF_PUSH (wordchar);
|
|
2844 break;
|
|
2845
|
|
2846
|
|
2847 case 'W':
|
|
2848 laststart = b;
|
|
2849 BUF_PUSH (notwordchar);
|
|
2850 break;
|
|
2851
|
|
2852
|
|
2853 case '<':
|
|
2854 BUF_PUSH (wordbeg);
|
|
2855 break;
|
|
2856
|
|
2857 case '>':
|
|
2858 BUF_PUSH (wordend);
|
|
2859 break;
|
|
2860
|
|
2861 case 'b':
|
|
2862 BUF_PUSH (wordbound);
|
|
2863 break;
|
|
2864
|
|
2865 case 'B':
|
|
2866 BUF_PUSH (notwordbound);
|
|
2867 break;
|
|
2868
|
|
2869 case '`':
|
|
2870 BUF_PUSH (begbuf);
|
|
2871 break;
|
|
2872
|
|
2873 case '\'':
|
|
2874 BUF_PUSH (endbuf);
|
|
2875 break;
|
|
2876
|
|
2877 case '1': case '2': case '3': case '4': case '5':
|
|
2878 case '6': case '7': case '8': case '9':
|
|
2879 if (syntax & RE_NO_BK_REFS)
|
|
2880 goto normal_char;
|
|
2881
|
|
2882 c1 = c - '0';
|
|
2883
|
|
2884 if (c1 > regnum)
|
|
2885 FREE_STACK_RETURN (REG_ESUBREG);
|
|
2886
|
|
2887 /* Can't back reference to a subexpression if inside of it. */
|
|
2888 if (group_in_compile_stack (compile_stack, c1))
|
|
2889 goto normal_char;
|
|
2890
|
|
2891 laststart = b;
|
|
2892 BUF_PUSH_2 (duplicate, c1);
|
|
2893 break;
|
|
2894
|
|
2895
|
|
2896 case '+':
|
|
2897 case '?':
|
|
2898 if (syntax & RE_BK_PLUS_QM)
|
|
2899 goto handle_plus;
|
|
2900 else
|
|
2901 goto normal_backslash;
|
|
2902
|
|
2903 default:
|
|
2904 normal_backslash:
|
|
2905 /* You might think it would be useful for \ to mean
|
2
|
2906 not to translate; but if we don't translate it,
|
0
|
2907 it will never match anything. */
|
|
2908 c = TRANSLATE (c);
|
|
2909 goto normal_char;
|
|
2910 }
|
|
2911 break;
|
|
2912
|
|
2913
|
|
2914 default:
|
|
2915 /* Expects the character in `c'. */
|
|
2916 /* `p' points to the location after where `c' came from. */
|
|
2917 normal_char:
|
|
2918 {
|
|
2919 /* XEmacs: modifications here for Mule. */
|
|
2920 /* `q' points to the beginning of the next char. */
|
|
2921 CONST char *q = p - 1;
|
|
2922 INC_CHARPTR (q);
|
|
2923
|
|
2924 /* If no exactn currently being built. */
|
|
2925 if (!pending_exact
|
|
2926
|
|
2927 /* If last exactn not at current position. */
|
|
2928 || pending_exact + *pending_exact + 1 != b
|
|
2929
|
|
2930 /* We have only one byte following the exactn for the count. */
|
|
2931 || ((unsigned int) (*pending_exact + (q - p)) >=
|
|
2932 ((unsigned int) (1 << BYTEWIDTH) - 1))
|
|
2933
|
|
2934 /* If followed by a repetition operator. */
|
|
2935 || *q == '*' || *q == '^'
|
|
2936 || ((syntax & RE_BK_PLUS_QM)
|
|
2937 ? *q == '\\' && (q[1] == '+' || q[1] == '?')
|
|
2938 : (*q == '+' || *q == '?'))
|
|
2939 || ((syntax & RE_INTERVALS)
|
|
2940 && ((syntax & RE_NO_BK_BRACES)
|
|
2941 ? *q == '{'
|
|
2942 : (q[0] == '\\' && q[1] == '{'))))
|
|
2943 {
|
|
2944 /* Start building a new exactn. */
|
|
2945
|
|
2946 laststart = b;
|
|
2947
|
|
2948 BUF_PUSH_2 (exactn, 0);
|
|
2949 pending_exact = b - 1;
|
|
2950 }
|
|
2951
|
|
2952 BUF_PUSH (c);
|
|
2953 (*pending_exact)++;
|
|
2954
|
|
2955 while (p < q)
|
|
2956 {
|
|
2957 PATFETCH (c);
|
|
2958 BUF_PUSH (c);
|
|
2959 (*pending_exact)++;
|
|
2960 }
|
|
2961 break;
|
|
2962 }
|
|
2963 } /* switch (c) */
|
|
2964 } /* while p != pend */
|
|
2965
|
|
2966
|
|
2967 /* Through the pattern now. */
|
|
2968
|
|
2969 if (fixup_alt_jump)
|
|
2970 STORE_JUMP (jump_past_alt, fixup_alt_jump, b);
|
|
2971
|
|
2972 if (!COMPILE_STACK_EMPTY)
|
|
2973 FREE_STACK_RETURN (REG_EPAREN);
|
|
2974
|
|
2975 /* If we don't want backtracking, force success
|
|
2976 the first time we reach the end of the compiled pattern. */
|
|
2977 if (syntax & RE_NO_POSIX_BACKTRACKING)
|
|
2978 BUF_PUSH (succeed);
|
|
2979
|
|
2980 free (compile_stack.stack);
|
|
2981
|
|
2982 /* We have succeeded; set the length of the buffer. */
|
|
2983 bufp->used = b - bufp->buffer;
|
|
2984
|
|
2985 #ifdef DEBUG
|
|
2986 if (debug)
|
|
2987 {
|
|
2988 DEBUG_PRINT1 ("\nCompiled pattern: \n");
|
|
2989 print_compiled_pattern (bufp);
|
|
2990 }
|
|
2991 #endif /* DEBUG */
|
|
2992
|
|
2993 #ifndef MATCH_MAY_ALLOCATE
|
|
2994 /* Initialize the failure stack to the largest possible stack. This
|
|
2995 isn't necessary unless we're trying to avoid calling alloca in
|
|
2996 the search and match routines. */
|
|
2997 {
|
|
2998 int num_regs = bufp->re_nsub + 1;
|
|
2999
|
|
3000 /* Since DOUBLE_FAIL_STACK refuses to double only if the current size
|
|
3001 is strictly greater than re_max_failures, the largest possible stack
|
|
3002 is 2 * re_max_failures failure points. */
|
|
3003 if (fail_stack.size < (2 * re_max_failures * MAX_FAILURE_ITEMS))
|
|
3004 {
|
|
3005 fail_stack.size = (2 * re_max_failures * MAX_FAILURE_ITEMS);
|
|
3006
|
|
3007 #ifdef emacs
|
|
3008 if (! fail_stack.stack)
|
|
3009 fail_stack.stack
|
|
3010 = (fail_stack_elt_t *) xmalloc (fail_stack.size
|
|
3011 * sizeof (fail_stack_elt_t));
|
|
3012 else
|
|
3013 fail_stack.stack
|
|
3014 = (fail_stack_elt_t *) xrealloc (fail_stack.stack,
|
|
3015 (fail_stack.size
|
|
3016 * sizeof (fail_stack_elt_t)));
|
|
3017 #else /* not emacs */
|
|
3018 if (! fail_stack.stack)
|
|
3019 fail_stack.stack
|
|
3020 = (fail_stack_elt_t *) malloc (fail_stack.size
|
|
3021 * sizeof (fail_stack_elt_t));
|
|
3022 else
|
|
3023 fail_stack.stack
|
|
3024 = (fail_stack_elt_t *) realloc (fail_stack.stack,
|
|
3025 (fail_stack.size
|
|
3026 * sizeof (fail_stack_elt_t)));
|
|
3027 #endif /* not emacs */
|
|
3028 }
|
|
3029
|
|
3030 regex_grow_registers (num_regs);
|
|
3031 }
|
|
3032 #endif /* not MATCH_MAY_ALLOCATE */
|
|
3033
|
|
3034 return REG_NOERROR;
|
|
3035 } /* regex_compile */
|
|
3036
|
|
3037 /* Subroutines for `regex_compile'. */
|
|
3038
|
|
3039 /* Store OP at LOC followed by two-byte integer parameter ARG. */
|
|
3040
|
|
3041 static void
|
|
3042 store_op1 (re_opcode_t op, unsigned char *loc, int arg)
|
|
3043 {
|
|
3044 *loc = (unsigned char) op;
|
|
3045 STORE_NUMBER (loc + 1, arg);
|
|
3046 }
|
|
3047
|
|
3048
|
|
3049 /* Like `store_op1', but for two two-byte parameters ARG1 and ARG2. */
|
|
3050
|
|
3051 static void
|
|
3052 store_op2 (re_opcode_t op, unsigned char *loc, int arg1, int arg2)
|
|
3053 {
|
|
3054 *loc = (unsigned char) op;
|
|
3055 STORE_NUMBER (loc + 1, arg1);
|
|
3056 STORE_NUMBER (loc + 3, arg2);
|
|
3057 }
|
|
3058
|
|
3059
|
|
3060 /* Copy the bytes from LOC to END to open up three bytes of space at LOC
|
|
3061 for OP followed by two-byte integer parameter ARG. */
|
|
3062
|
|
3063 static void
|
|
3064 insert_op1 (re_opcode_t op, unsigned char *loc, int arg, unsigned char *end)
|
|
3065 {
|
|
3066 register unsigned char *pfrom = end;
|
|
3067 register unsigned char *pto = end + 3;
|
|
3068
|
|
3069 while (pfrom != loc)
|
|
3070 *--pto = *--pfrom;
|
|
3071
|
|
3072 store_op1 (op, loc, arg);
|
|
3073 }
|
|
3074
|
|
3075
|
|
3076 /* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2. */
|
|
3077
|
|
3078 static void
|
|
3079 insert_op2 (re_opcode_t op, unsigned char *loc, int arg1, int arg2,
|
|
3080 unsigned char *end)
|
|
3081 {
|
|
3082 register unsigned char *pfrom = end;
|
|
3083 register unsigned char *pto = end + 5;
|
|
3084
|
|
3085 while (pfrom != loc)
|
|
3086 *--pto = *--pfrom;
|
|
3087
|
|
3088 store_op2 (op, loc, arg1, arg2);
|
|
3089 }
|
|
3090
|
|
3091
|
|
3092 /* P points to just after a ^ in PATTERN. Return true if that ^ comes
|
|
3093 after an alternative or a begin-subexpression. We assume there is at
|
|
3094 least one character before the ^. */
|
|
3095
|
|
3096 static boolean
|
|
3097 at_begline_loc_p (CONST char *pattern, CONST char *p, reg_syntax_t syntax)
|
|
3098 {
|
|
3099 CONST char *prev = p - 2;
|
|
3100 boolean prev_prev_backslash = prev > pattern && prev[-1] == '\\';
|
|
3101
|
|
3102 return
|
|
3103 /* After a subexpression? */
|
|
3104 (*prev == '(' && (syntax & RE_NO_BK_PARENS || prev_prev_backslash))
|
|
3105 /* After an alternative? */
|
|
3106 || (*prev == '|' && (syntax & RE_NO_BK_VBAR || prev_prev_backslash));
|
|
3107 }
|
|
3108
|
|
3109
|
|
3110 /* The dual of at_begline_loc_p. This one is for $. We assume there is
|
|
3111 at least one character after the $, i.e., `P < PEND'. */
|
|
3112
|
|
3113 static boolean
|
|
3114 at_endline_loc_p (CONST char *p, CONST char *pend, int syntax)
|
|
3115 {
|
|
3116 CONST char *next = p;
|
|
3117 boolean next_backslash = *next == '\\';
|
|
3118 CONST char *next_next = p + 1 < pend ? p + 1 : 0;
|
|
3119
|
|
3120 return
|
|
3121 /* Before a subexpression? */
|
|
3122 (syntax & RE_NO_BK_PARENS ? *next == ')'
|
|
3123 : next_backslash && next_next && *next_next == ')')
|
|
3124 /* Before an alternative? */
|
|
3125 || (syntax & RE_NO_BK_VBAR ? *next == '|'
|
|
3126 : next_backslash && next_next && *next_next == '|');
|
|
3127 }
|
|
3128
|
|
3129
|
|
3130 /* Returns true if REGNUM is in one of COMPILE_STACK's elements and
|
|
3131 false if it's not. */
|
|
3132
|
|
3133 static boolean
|
|
3134 group_in_compile_stack (compile_stack_type compile_stack, regnum_t regnum)
|
|
3135 {
|
|
3136 int this_element;
|
|
3137
|
|
3138 for (this_element = compile_stack.avail - 1;
|
|
3139 this_element >= 0;
|
|
3140 this_element--)
|
|
3141 if (compile_stack.stack[this_element].regnum == regnum)
|
|
3142 return true;
|
|
3143
|
|
3144 return false;
|
|
3145 }
|
|
3146
|
|
3147
|
|
3148 /* Read the ending character of a range (in a bracket expression) from the
|
|
3149 uncompiled pattern *P_PTR (which ends at PEND). We assume the
|
|
3150 starting character is in `P[-2]'. (`P[-1]' is the character `-'.)
|
|
3151 Then we set the translation of all bits between the starting and
|
|
3152 ending characters (inclusive) in the compiled pattern B.
|
|
3153
|
|
3154 Return an error code.
|
|
3155
|
|
3156 We use these short variable names so we can use the same macros as
|
|
3157 `regex_compile' itself. */
|
|
3158
|
|
3159 static reg_errcode_t
|
|
3160 compile_range (CONST char **p_ptr, CONST char *pend, char *translate,
|
|
3161 reg_syntax_t syntax, unsigned char *b)
|
|
3162 {
|
|
3163 unsigned this_char;
|
|
3164
|
|
3165 CONST char *p = *p_ptr;
|
|
3166 int range_start, range_end;
|
|
3167
|
|
3168 if (p == pend)
|
|
3169 return REG_ERANGE;
|
|
3170
|
|
3171 /* Even though the pattern is a signed `char *', we need to fetch
|
|
3172 with unsigned char *'s; if the high bit of the pattern character
|
|
3173 is set, the range endpoints will be negative if we fetch using a
|
|
3174 signed char *.
|
|
3175
|
|
3176 We also want to fetch the endpoints without translating them; the
|
|
3177 appropriate translation is done in the bit-setting loop below. */
|
|
3178 /* The SVR4 compiler on the 3B2 had trouble with unsigned CONST char *. */
|
|
3179 range_start = ((CONST unsigned char *) p)[-2];
|
|
3180 range_end = ((CONST unsigned char *) p)[0];
|
|
3181
|
|
3182 /* Have to increment the pointer into the pattern string, so the
|
|
3183 caller isn't still at the ending character. */
|
|
3184 (*p_ptr)++;
|
|
3185
|
|
3186 /* If the start is after the end, the range is empty. */
|
|
3187 if (range_start > range_end)
|
|
3188 return syntax & RE_NO_EMPTY_RANGES ? REG_ERANGE : REG_NOERROR;
|
|
3189
|
|
3190 /* Here we see why `this_char' has to be larger than an `unsigned
|
|
3191 char' -- the range is inclusive, so if `range_end' == 0xff
|
|
3192 (assuming 8-bit characters), we would otherwise go into an infinite
|
|
3193 loop, since all characters <= 0xff. */
|
|
3194 for (this_char = range_start; this_char <= range_end; this_char++)
|
|
3195 {
|
|
3196 SET_LIST_BIT (TRANSLATE (this_char));
|
|
3197 }
|
|
3198
|
|
3199 return REG_NOERROR;
|
|
3200 }
|
70
|
3201
|
|
3202 #ifdef MULE
|
|
3203
|
|
3204 static reg_errcode_t
|
|
3205 compile_extended_range (CONST char **p_ptr, CONST char *pend, char *translate,
|
|
3206 reg_syntax_t syntax, Lisp_Object rtab)
|
|
3207 {
|
|
3208 Emchar this_char;
|
|
3209
|
|
3210 CONST char *p = *p_ptr;
|
|
3211 EMACS_INT range_start, range_end;
|
|
3212
|
|
3213 if (p == pend)
|
|
3214 return REG_ERANGE;
|
|
3215
|
|
3216 p--; /* back to '-' */
|
|
3217 DEC_CHARPTR (p); /* back to start of range */
|
|
3218 /* We also want to fetch the endpoints without translating them; the
|
|
3219 appropriate translation is done in the bit-setting loop below. */
|
|
3220 range_start = charptr_emchar ((CONST Bufbyte *) p);
|
|
3221 range_end = charptr_emchar ((CONST Bufbyte *) (*p_ptr));
|
|
3222 INC_CHARPTR (*p_ptr);
|
|
3223
|
|
3224 /* If the start is after the end, the range is empty. */
|
|
3225 if (range_start > range_end)
|
|
3226 return syntax & RE_NO_EMPTY_RANGES ? REG_ERANGE : REG_NOERROR;
|
|
3227
|
|
3228 /* Can't have ranges spanning different charsets, except maybe for
|
|
3229 ranges entirely witin the first 256 chars. */
|
|
3230
|
|
3231 if ((range_start >= 0x100 || range_end >= 0x100)
|
|
3232 && CHAR_LEADING_BYTE (range_start) !=
|
|
3233 CHAR_LEADING_BYTE (range_end))
|
|
3234 return REG_ERANGESPAN;
|
|
3235
|
|
3236 /* As advertised, translations only work over the 0 - 0x7F range.
|
|
3237 Making this kind of stuff work generally is much harder.
|
|
3238 Iterating over the whole range like this would be way efficient
|
|
3239 if the range encompasses 10,000 chars or something. You'd have
|
|
3240 to do something like this:
|
|
3241
|
|
3242 range_table a;
|
|
3243 range_table b;
|
|
3244 map over translation table in [range_start, range_end] of
|
|
3245 (put the mapped range in a;
|
|
3246 put the translation in b)
|
|
3247 invert the range in a and truncate to [range_start, range_end]
|
|
3248 compute the union of a, b
|
|
3249 union the result into rtab
|
|
3250 */
|
|
3251 for (this_char = range_start;
|
|
3252 this_char <= range_end && this_char < 0x80; this_char++)
|
|
3253 {
|
|
3254 SET_RANGETAB_BIT (TRANSLATE (this_char));
|
|
3255 }
|
|
3256
|
|
3257 if (this_char <= range_end)
|
|
3258 put_range_table (rtab, this_char, range_end, Qt);
|
|
3259
|
|
3260 return REG_NOERROR;
|
|
3261 }
|
|
3262
|
|
3263 #endif /* MULE */
|
0
|
3264
|
|
3265 /* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
|
|
3266 BUFP. A fastmap records which of the (1 << BYTEWIDTH) possible
|
|
3267 characters can start a string that matches the pattern. This fastmap
|
|
3268 is used by re_search to skip quickly over impossible starting points.
|
|
3269
|
|
3270 The caller must supply the address of a (1 << BYTEWIDTH)-byte data
|
|
3271 area as BUFP->fastmap.
|
|
3272
|
|
3273 We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in
|
|
3274 the pattern buffer.
|
|
3275
|
|
3276 Returns 0 if we succeed, -2 if an internal error. */
|
|
3277
|
|
3278 int
|
|
3279 re_compile_fastmap (struct re_pattern_buffer *bufp)
|
|
3280 {
|
|
3281 int j, k;
|
|
3282 #ifdef MATCH_MAY_ALLOCATE
|
|
3283 fail_stack_type fail_stack;
|
|
3284 #endif
|
|
3285 DECLARE_DESTINATION
|
|
3286 /* We don't push any register information onto the failure stack. */
|
|
3287 unsigned num_regs = 0;
|
|
3288
|
|
3289 register char *fastmap = bufp->fastmap;
|
|
3290 unsigned char *pattern = bufp->buffer;
|
|
3291 unsigned long size = bufp->used;
|
|
3292 unsigned char *p = pattern;
|
|
3293 register unsigned char *pend = pattern + size;
|
|
3294
|
|
3295 #ifdef REL_ALLOC
|
|
3296 /* This holds the pointer to the failure stack, when
|
|
3297 it is allocated relocatably. */
|
|
3298 fail_stack_elt_t *failure_stack_ptr;
|
|
3299 #endif
|
|
3300
|
|
3301 /* Assume that each path through the pattern can be null until
|
|
3302 proven otherwise. We set this false at the bottom of switch
|
|
3303 statement, to which we get only if a particular path doesn't
|
|
3304 match the empty string. */
|
|
3305 boolean path_can_be_null = true;
|
|
3306
|
|
3307 /* We aren't doing a `succeed_n' to begin with. */
|
|
3308 boolean succeed_n_p = false;
|
|
3309
|
|
3310 assert (fastmap != NULL && p != NULL);
|
|
3311
|
|
3312 INIT_FAIL_STACK ();
|
|
3313 bzero (fastmap, 1 << BYTEWIDTH); /* Assume nothing's valid. */
|
|
3314 bufp->fastmap_accurate = 1; /* It will be when we're done. */
|
|
3315 bufp->can_be_null = 0;
|
|
3316
|
|
3317 while (1)
|
|
3318 {
|
|
3319 if (p == pend || *p == succeed)
|
|
3320 {
|
|
3321 /* We have reached the (effective) end of pattern. */
|
|
3322 if (!FAIL_STACK_EMPTY ())
|
|
3323 {
|
|
3324 bufp->can_be_null |= path_can_be_null;
|
|
3325
|
|
3326 /* Reset for next path. */
|
|
3327 path_can_be_null = true;
|
|
3328
|
|
3329 p = fail_stack.stack[--fail_stack.avail].pointer;
|
|
3330
|
|
3331 continue;
|
|
3332 }
|
|
3333 else
|
|
3334 break;
|
|
3335 }
|
|
3336
|
|
3337 /* We should never be about to go beyond the end of the pattern. */
|
|
3338 assert (p < pend);
|
|
3339
|
|
3340 switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++))
|
|
3341 {
|
|
3342
|
|
3343 /* I guess the idea here is to simply not bother with a fastmap
|
|
3344 if a backreference is used, since it's too hard to figure out
|
|
3345 the fastmap for the corresponding group. Setting
|
|
3346 `can_be_null' stops `re_search_2' from using the fastmap, so
|
|
3347 that is all we do. */
|
|
3348 case duplicate:
|
|
3349 bufp->can_be_null = 1;
|
|
3350 goto done;
|
|
3351
|
|
3352
|
|
3353 /* Following are the cases which match a character. These end
|
|
3354 with `break'. */
|
|
3355
|
|
3356 case exactn:
|
|
3357 fastmap[p[1]] = 1;
|
|
3358 break;
|
|
3359
|
|
3360
|
|
3361 case charset:
|
70
|
3362 /* XEmacs: Under Mule, these bit vectors will
|
|
3363 only contain values for characters below 0x80. */
|
0
|
3364 for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
|
|
3365 if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
|
|
3366 fastmap[j] = 1;
|
|
3367 break;
|
|
3368
|
|
3369
|
|
3370 case charset_not:
|
|
3371 /* Chars beyond end of map must be allowed. */
|
70
|
3372 #ifdef MULE
|
|
3373 for (j = *p * BYTEWIDTH; j < 0x80; j++)
|
|
3374 fastmap[j] = 1;
|
|
3375 /* And all extended characters must be allowed, too. */
|
|
3376 for (j = 0x80; j < 0xA0; j++)
|
|
3377 fastmap[j] = 1;
|
|
3378 #else
|
0
|
3379 for (j = *p * BYTEWIDTH; j < (1 << BYTEWIDTH); j++)
|
|
3380 fastmap[j] = 1;
|
70
|
3381 #endif
|
0
|
3382
|
|
3383 for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
|
|
3384 if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
|
|
3385 fastmap[j] = 1;
|
|
3386 break;
|
|
3387
|
70
|
3388 #ifdef MULE
|
|
3389 case charset_mule:
|
|
3390 {
|
|
3391 int nentries;
|
|
3392 int i;
|
|
3393
|
|
3394 nentries = unified_range_table_nentries (p);
|
|
3395 for (i = 0; i < nentries; i++)
|
|
3396 {
|
|
3397 EMACS_INT first, last;
|
|
3398 Lisp_Object dummy_val;
|
|
3399 int jj;
|
|
3400 Bufbyte strr[MAX_EMCHAR_LEN];
|
|
3401
|
|
3402 unified_range_table_get_range (p, i, &first, &last,
|
|
3403 &dummy_val);
|
|
3404 for (jj = first; jj <= last && jj < 0x80; jj++)
|
|
3405 fastmap[jj] = 1;
|
|
3406 /* Ranges below 0x100 can span charsets, but there
|
|
3407 are only two (Control-1 and Latin-1), and
|
|
3408 either first or last has to be in them. */
|
|
3409 set_charptr_emchar (strr, first);
|
|
3410 fastmap[*strr] = 1;
|
|
3411 if (last < 0x100)
|
|
3412 {
|
|
3413 set_charptr_emchar (strr, last);
|
|
3414 fastmap[*strr] = 1;
|
|
3415 }
|
|
3416 }
|
|
3417 }
|
|
3418 break;
|
|
3419
|
|
3420 case charset_mule_not:
|
|
3421 {
|
|
3422 int nentries;
|
|
3423 int i;
|
|
3424
|
|
3425 nentries = unified_range_table_nentries (p);
|
|
3426 for (i = 0; i < nentries; i++)
|
|
3427 {
|
|
3428 EMACS_INT first, last;
|
|
3429 Lisp_Object dummy_val;
|
|
3430 int jj;
|
|
3431 int smallest_prev = 0;
|
|
3432
|
|
3433 unified_range_table_get_range (p, i, &first, &last,
|
|
3434 &dummy_val);
|
|
3435 for (jj = smallest_prev; jj < first && jj < 0x80; jj++)
|
|
3436 fastmap[jj] = 1;
|
|
3437 smallest_prev = last + 1;
|
|
3438 if (smallest_prev >= 0x80)
|
|
3439 break;
|
|
3440 }
|
|
3441 /* Calculating which leading bytes are actually allowed
|
|
3442 here is rather difficult, so we just punt and allow
|
|
3443 all of them. */
|
|
3444 for (i = 0x80; i < 0xA0; i++)
|
|
3445 fastmap[i] = 1;
|
|
3446 }
|
|
3447 break;
|
|
3448 #endif /* MULE */
|
|
3449
|
0
|
3450
|
|
3451 case wordchar:
|
|
3452 #ifdef emacs
|
|
3453 k = (int) Sword;
|
|
3454 goto matchsyntax;
|
|
3455 #else
|
|
3456 for (j = 0; j < (1 << BYTEWIDTH); j++)
|
70
|
3457 if (SYNTAX_UNSAFE
|
|
3458 (XCHAR_TABLE
|
|
3459 (regex_emacs_buffer->mirror_syntax_table), j) == Sword)
|
0
|
3460 fastmap[j] = 1;
|
|
3461 break;
|
|
3462 #endif
|
|
3463
|
|
3464
|
|
3465 case notwordchar:
|
|
3466 #ifdef emacs
|
|
3467 k = (int) Sword;
|
|
3468 goto matchnotsyntax;
|
|
3469 #else
|
|
3470 for (j = 0; j < (1 << BYTEWIDTH); j++)
|
70
|
3471 if (SYNTAX_UNSAFE
|
|
3472 (XCHAR_TABLE
|
|
3473 (regex_emacs_buffer->mirror_syntax_table), j) != Sword)
|
0
|
3474 fastmap[j] = 1;
|
|
3475 break;
|
|
3476 #endif
|
|
3477
|
|
3478
|
|
3479 case anychar:
|
|
3480 {
|
|
3481 int fastmap_newline = fastmap['\n'];
|
|
3482
|
|
3483 /* `.' matches anything ... */
|
70
|
3484 #ifdef MULE
|
|
3485 /* "anything" only includes bytes that can be the
|
|
3486 first byte of a character. */
|
|
3487 for (j = 0; j < 0xA0; j++)
|
|
3488 fastmap[j] = 1;
|
|
3489 #else
|
0
|
3490 for (j = 0; j < (1 << BYTEWIDTH); j++)
|
|
3491 fastmap[j] = 1;
|
70
|
3492 #endif
|
0
|
3493
|
|
3494 /* ... except perhaps newline. */
|
|
3495 if (!(bufp->syntax & RE_DOT_NEWLINE))
|
|
3496 fastmap['\n'] = fastmap_newline;
|
|
3497
|
|
3498 /* Return if we have already set `can_be_null'; if we have,
|
|
3499 then the fastmap is irrelevant. Something's wrong here. */
|
|
3500 else if (bufp->can_be_null)
|
|
3501 goto done;
|
|
3502
|
|
3503 /* Otherwise, have to check alternative paths. */
|
|
3504 break;
|
|
3505 }
|
|
3506
|
|
3507 #ifdef emacs
|
|
3508 case syntaxspec:
|
|
3509 k = *p++;
|
|
3510 matchsyntax:
|
70
|
3511 #ifdef MULE
|
|
3512 for (j = 0; j < 0x80; j++)
|
|
3513 if (SYNTAX_UNSAFE
|
|
3514 (XCHAR_TABLE
|
|
3515 (regex_emacs_buffer->mirror_syntax_table), j) ==
|
0
|
3516 (enum syntaxcode) k)
|
|
3517 fastmap[j] = 1;
|
70
|
3518 for (j = 0x80; j < 0xA0; j++)
|
|
3519 {
|
|
3520 if (j == PRE_LEADING_BYTE_PRIVATE_1
|
|
3521 || j == PRE_LEADING_BYTE_PRIVATE_2)
|
|
3522 /* too complicated to calculate this right */
|
|
3523 fastmap[j] = 1;
|
|
3524 else
|
|
3525 {
|
|
3526 int multi_p;
|
|
3527 Lisp_Object cset;
|
|
3528
|
|
3529 cset = CHARSET_BY_LEADING_BYTE (j);
|
|
3530 if (CHARSETP (cset))
|
|
3531 {
|
|
3532 if (charset_syntax (regex_emacs_buffer, cset,
|
|
3533 &multi_p)
|
|
3534 == Sword || multi_p)
|
|
3535 fastmap[j] = 1;
|
|
3536 }
|
|
3537 }
|
|
3538 }
|
|
3539 #else /* ! MULE */
|
|
3540 for (j = 0; j < (1 << BYTEWIDTH); j++)
|
|
3541 if (SYNTAX_UNSAFE
|
|
3542 (XCHAR_TABLE
|
|
3543 (regex_emacs_buffer->mirror_syntax_table), j) ==
|
|
3544 (enum syntaxcode) k)
|
|
3545 fastmap[j] = 1;
|
|
3546 #endif /* ! MULE */
|
0
|
3547 break;
|
|
3548
|
|
3549
|
|
3550 case notsyntaxspec:
|
|
3551 k = *p++;
|
|
3552 matchnotsyntax:
|
70
|
3553 #ifdef MULE
|
|
3554 for (j = 0; j < 0x80; j++)
|
|
3555 if (SYNTAX_UNSAFE
|
|
3556 (XCHAR_TABLE
|
|
3557 (regex_emacs_buffer->mirror_syntax_table), j) !=
|
0
|
3558 (enum syntaxcode) k)
|
|
3559 fastmap[j] = 1;
|
70
|
3560 for (j = 0x80; j < 0xA0; j++)
|
|
3561 {
|
|
3562 if (j == PRE_LEADING_BYTE_PRIVATE_1
|
|
3563 || j == PRE_LEADING_BYTE_PRIVATE_2)
|
|
3564 /* too complicated to calculate this right */
|
|
3565 fastmap[j] = 1;
|
|
3566 else
|
|
3567 {
|
|
3568 int multi_p;
|
|
3569 Lisp_Object cset;
|
|
3570
|
|
3571 cset = CHARSET_BY_LEADING_BYTE (j);
|
|
3572 if (CHARSETP (cset))
|
|
3573 {
|
|
3574 if (charset_syntax (regex_emacs_buffer, cset,
|
|
3575 &multi_p)
|
|
3576 != Sword || multi_p)
|
|
3577 fastmap[j] = 1;
|
|
3578 }
|
|
3579 }
|
|
3580 }
|
|
3581 #else /* ! MULE */
|
|
3582 for (j = 0; j < (1 << BYTEWIDTH); j++)
|
|
3583 if (SYNTAX_UNSAFE
|
|
3584 (XCHAR_TABLE
|
|
3585 (regex_emacs_buffer->mirror_syntax_table), j) !=
|
|
3586 (enum syntaxcode) k)
|
|
3587 fastmap[j] = 1;
|
|
3588 #endif /* ! MULE */
|
0
|
3589 break;
|
|
3590
|
|
3591
|
|
3592 /* All cases after this match the empty string. These end with
|
|
3593 `continue'. */
|
|
3594
|
|
3595
|
|
3596 case before_dot:
|
|
3597 case at_dot:
|
|
3598 case after_dot:
|
|
3599 continue;
|
|
3600 #endif /* not emacs */
|
|
3601
|
|
3602
|
|
3603 case no_op:
|
|
3604 case begline:
|
|
3605 case endline:
|
|
3606 case begbuf:
|
|
3607 case endbuf:
|
|
3608 case wordbound:
|
|
3609 case notwordbound:
|
|
3610 case wordbeg:
|
|
3611 case wordend:
|
|
3612 case push_dummy_failure:
|
|
3613 continue;
|
|
3614
|
|
3615
|
|
3616 case jump_n:
|
|
3617 case pop_failure_jump:
|
|
3618 case maybe_pop_jump:
|
|
3619 case jump:
|
|
3620 case jump_past_alt:
|
|
3621 case dummy_failure_jump:
|
|
3622 EXTRACT_NUMBER_AND_INCR (j, p);
|
|
3623 p += j;
|
|
3624 if (j > 0)
|
|
3625 continue;
|
|
3626
|
|
3627 /* Jump backward implies we just went through the body of a
|
|
3628 loop and matched nothing. Opcode jumped to should be
|
|
3629 `on_failure_jump' or `succeed_n'. Just treat it like an
|
|
3630 ordinary jump. For a * loop, it has pushed its failure
|
|
3631 point already; if so, discard that as redundant. */
|
|
3632 if ((re_opcode_t) *p != on_failure_jump
|
|
3633 && (re_opcode_t) *p != succeed_n)
|
|
3634 continue;
|
|
3635
|
|
3636 p++;
|
|
3637 EXTRACT_NUMBER_AND_INCR (j, p);
|
|
3638 p += j;
|
|
3639
|
|
3640 /* If what's on the stack is where we are now, pop it. */
|
|
3641 if (!FAIL_STACK_EMPTY ()
|
|
3642 && fail_stack.stack[fail_stack.avail - 1].pointer == p)
|
|
3643 fail_stack.avail--;
|
|
3644
|
|
3645 continue;
|
|
3646
|
|
3647
|
|
3648 case on_failure_jump:
|
|
3649 case on_failure_keep_string_jump:
|
|
3650 handle_on_failure_jump:
|
|
3651 EXTRACT_NUMBER_AND_INCR (j, p);
|
|
3652
|
|
3653 /* For some patterns, e.g., `(a?)?', `p+j' here points to the
|
|
3654 end of the pattern. We don't want to push such a point,
|
|
3655 since when we restore it above, entering the switch will
|
|
3656 increment `p' past the end of the pattern. We don't need
|
|
3657 to push such a point since we obviously won't find any more
|
|
3658 fastmap entries beyond `pend'. Such a pattern can match
|
|
3659 the null string, though. */
|
|
3660 if (p + j < pend)
|
|
3661 {
|
|
3662 if (!PUSH_PATTERN_OP (p + j, fail_stack))
|
|
3663 {
|
|
3664 RESET_FAIL_STACK ();
|
|
3665 return -2;
|
|
3666 }
|
|
3667 }
|
|
3668 else
|
|
3669 bufp->can_be_null = 1;
|
|
3670
|
|
3671 if (succeed_n_p)
|
|
3672 {
|
|
3673 EXTRACT_NUMBER_AND_INCR (k, p); /* Skip the n. */
|
|
3674 succeed_n_p = false;
|
|
3675 }
|
|
3676
|
|
3677 continue;
|
|
3678
|
|
3679
|
|
3680 case succeed_n:
|
|
3681 /* Get to the number of times to succeed. */
|
|
3682 p += 2;
|
|
3683
|
|
3684 /* Increment p past the n for when k != 0. */
|
|
3685 EXTRACT_NUMBER_AND_INCR (k, p);
|
|
3686 if (k == 0)
|
|
3687 {
|
|
3688 p -= 4;
|
|
3689 succeed_n_p = true; /* Spaghetti code alert. */
|
|
3690 goto handle_on_failure_jump;
|
|
3691 }
|
|
3692 continue;
|
|
3693
|
|
3694
|
|
3695 case set_number_at:
|
|
3696 p += 4;
|
|
3697 continue;
|
|
3698
|
|
3699
|
|
3700 case start_memory:
|
|
3701 case stop_memory:
|
|
3702 p += 2;
|
|
3703 continue;
|
|
3704
|
|
3705
|
|
3706 default:
|
|
3707 abort (); /* We have listed all the cases. */
|
|
3708 } /* switch *p++ */
|
|
3709
|
|
3710 /* Getting here means we have found the possible starting
|
|
3711 characters for one path of the pattern -- and that the empty
|
|
3712 string does not match. We need not follow this path further.
|
|
3713 Instead, look at the next alternative (remembered on the
|
|
3714 stack), or quit if no more. The test at the top of the loop
|
|
3715 does these things. */
|
|
3716 path_can_be_null = false;
|
|
3717 p = pend;
|
|
3718 } /* while p */
|
|
3719
|
|
3720 /* Set `can_be_null' for the last path (also the first path, if the
|
|
3721 pattern is empty). */
|
|
3722 bufp->can_be_null |= path_can_be_null;
|
|
3723
|
|
3724 done:
|
|
3725 RESET_FAIL_STACK ();
|
|
3726 return 0;
|
|
3727 } /* re_compile_fastmap */
|
|
3728
|
|
3729 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
|
|
3730 ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
|
|
3731 this memory for recording register information. STARTS and ENDS
|
|
3732 must be allocated using the malloc library routine, and must each
|
|
3733 be at least NUM_REGS * sizeof (regoff_t) bytes long.
|
|
3734
|
|
3735 If NUM_REGS == 0, then subsequent matches should allocate their own
|
|
3736 register data.
|
|
3737
|
|
3738 Unless this function is called, the first search or match using
|
|
3739 PATTERN_BUFFER will allocate its own register data, without
|
|
3740 freeing the old data. */
|
|
3741
|
|
3742 void
|
|
3743 re_set_registers (struct re_pattern_buffer *bufp, struct re_registers *regs,
|
|
3744 unsigned num_regs, regoff_t *starts, regoff_t *ends)
|
|
3745 {
|
|
3746 if (num_regs)
|
|
3747 {
|
|
3748 bufp->regs_allocated = REGS_REALLOCATE;
|
|
3749 regs->num_regs = num_regs;
|
|
3750 regs->start = starts;
|
|
3751 regs->end = ends;
|
|
3752 }
|
|
3753 else
|
|
3754 {
|
|
3755 bufp->regs_allocated = REGS_UNALLOCATED;
|
|
3756 regs->num_regs = 0;
|
|
3757 regs->start = regs->end = (regoff_t *) 0;
|
|
3758 }
|
|
3759 }
|
|
3760
|
|
3761 /* Searching routines. */
|
|
3762
|
|
3763 /* Like re_search_2, below, but only one string is specified, and
|
|
3764 doesn't let you say where to stop matching. */
|
|
3765
|
|
3766 int
|
|
3767 re_search (struct re_pattern_buffer *bufp, CONST char *string, int size,
|
|
3768 int startpos, int range, struct re_registers *regs)
|
|
3769 {
|
|
3770 return re_search_2 (bufp, NULL, 0, string, size, startpos, range,
|
|
3771 regs, size);
|
|
3772 }
|
|
3773
|
|
3774
|
|
3775 /* Using the compiled pattern in BUFP->buffer, first tries to match the
|
|
3776 virtual concatenation of STRING1 and STRING2, starting first at index
|
|
3777 STARTPOS, then at STARTPOS + 1, and so on.
|
|
3778
|
|
3779 STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
|
|
3780
|
|
3781 RANGE is how far to scan while trying to match. RANGE = 0 means try
|
|
3782 only at STARTPOS; in general, the last start tried is STARTPOS +
|
|
3783 RANGE.
|
|
3784
|
|
3785 In REGS, return the indices of the virtual concatenation of STRING1
|
|
3786 and STRING2 that matched the entire BUFP->buffer and its contained
|
|
3787 subexpressions.
|
|
3788
|
|
3789 Do not consider matching one past the index STOP in the virtual
|
|
3790 concatenation of STRING1 and STRING2.
|
|
3791
|
|
3792 We return either the position in the strings at which the match was
|
|
3793 found, -1 if no match, or -2 if error (such as failure
|
|
3794 stack overflow). */
|
|
3795
|
|
3796 int
|
|
3797 re_search_2 (struct re_pattern_buffer *bufp, CONST char *string1,
|
|
3798 int size1, CONST char *string2, int size2, int startpos,
|
|
3799 int range, struct re_registers *regs, int stop)
|
|
3800 {
|
|
3801 int val;
|
|
3802 register char *fastmap = bufp->fastmap;
|
|
3803 register char *translate = bufp->translate;
|
|
3804 int total_size = size1 + size2;
|
|
3805 int endpos = startpos + range;
|
|
3806 #ifdef REGEX_BEGLINE_CHECK
|
|
3807 int anchored_at_begline = 0;
|
|
3808 #endif
|
|
3809
|
|
3810 /* Check for out-of-range STARTPOS. */
|
|
3811 if (startpos < 0 || startpos > total_size)
|
|
3812 return -1;
|
|
3813
|
|
3814 /* Fix up RANGE if it might eventually take us outside
|
|
3815 the virtual concatenation of STRING1 and STRING2. */
|
|
3816 if (endpos < -1)
|
|
3817 range = -1 - startpos;
|
|
3818 else if (endpos > total_size)
|
|
3819 range = total_size - startpos;
|
|
3820
|
|
3821 /* If the search isn't to be a backwards one, don't waste time in a
|
|
3822 search for a pattern that must be anchored. */
|
|
3823 if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == begbuf && range > 0)
|
|
3824 {
|
|
3825 if (startpos > 0)
|
|
3826 return -1;
|
|
3827 else
|
|
3828 range = 1;
|
|
3829 }
|
|
3830
|
|
3831 /* Update the fastmap now if not correct already. */
|
|
3832 if (fastmap && !bufp->fastmap_accurate)
|
|
3833 if (re_compile_fastmap (bufp) == -2)
|
|
3834 return -2;
|
|
3835
|
|
3836 #ifdef REGEX_BEGLINE_CHECK
|
|
3837 {
|
|
3838 int i = 0;
|
|
3839
|
|
3840 while (i < bufp->used)
|
|
3841 {
|
|
3842 if (bufp->buffer[i] == start_memory ||
|
|
3843 bufp->buffer[i] == stop_memory)
|
|
3844 i += 2;
|
|
3845 else
|
|
3846 break;
|
|
3847 }
|
|
3848 anchored_at_begline = i < bufp->used && bufp->buffer[i] == begline;
|
|
3849 }
|
|
3850 #endif
|
|
3851
|
|
3852 /* Loop through the string, looking for a place to start matching. */
|
|
3853 for (;;)
|
|
3854 {
|
|
3855 #ifdef REGEX_BEGLINE_CHECK
|
|
3856 /* If the regex is anchored at the beginning of a line (i.e. with a ^),
|
|
3857 then we can speed things up by skipping to the next beginning-of-
|
|
3858 line. */
|
|
3859 if (anchored_at_begline && startpos > 0 && startpos != size1 &&
|
|
3860 range > 0)
|
|
3861 {
|
|
3862 /* whose stupid idea was it anyway to make this
|
|
3863 function take two strings to match?? */
|
|
3864 int lim = 0;
|
|
3865 unsigned char *p;
|
|
3866 int irange = range;
|
|
3867 if (startpos < size1 && startpos + range >= size1)
|
|
3868 lim = range - (size1 - startpos);
|
|
3869
|
|
3870 p = ((unsigned char *)
|
|
3871 &(startpos >= size1 ? string2 - size1 : string1)[startpos]);
|
|
3872 p--;
|
|
3873
|
|
3874 if (translate)
|
|
3875 {
|
|
3876 while (range > lim && translate[*p++] != '\n')
|
|
3877 range--;
|
|
3878 }
|
|
3879 else
|
|
3880 {
|
|
3881 while (range > lim && *p++ != '\n')
|
|
3882 range--;
|
|
3883 }
|
|
3884 startpos += irange - range;
|
|
3885 }
|
|
3886 #endif /* REGEX_BEGLINE_CHECK */
|
|
3887
|
|
3888 /* If a fastmap is supplied, skip quickly over characters that
|
|
3889 cannot be the start of a match. If the pattern can match the
|
|
3890 null string, however, we don't need to skip characters; we want
|
|
3891 the first null string. */
|
|
3892 if (fastmap && startpos < total_size && !bufp->can_be_null)
|
|
3893 {
|
|
3894 if (range > 0) /* Searching forwards. */
|
|
3895 {
|
|
3896 register CONST char *d;
|
|
3897 register int lim = 0;
|
|
3898 int irange = range;
|
|
3899
|
|
3900 if (startpos < size1 && startpos + range >= size1)
|
|
3901 lim = range - (size1 - startpos);
|
|
3902
|
|
3903 d = (startpos >= size1 ? string2 - size1 : string1) + startpos;
|
|
3904
|
|
3905 /* Written out as an if-else to avoid testing `translate'
|
|
3906 inside the loop. */
|
|
3907 if (translate)
|
|
3908 while (range > lim
|
|
3909 && !fastmap[(unsigned char)
|
|
3910 translate[(unsigned char) *d++]])
|
|
3911 range--;
|
|
3912 else
|
|
3913 while (range > lim && !fastmap[(unsigned char) *d++])
|
|
3914 range--;
|
|
3915
|
|
3916 startpos += irange - range;
|
|
3917 }
|
|
3918 else /* Searching backwards. */
|
|
3919 {
|
|
3920 register char c = (size1 == 0 || startpos >= size1
|
|
3921 ? string2[startpos - size1]
|
|
3922 : string1[startpos]);
|
|
3923
|
|
3924 if (!fastmap[(unsigned char) TRANSLATE (c)])
|
|
3925 goto advance;
|
|
3926 }
|
|
3927 }
|
|
3928
|
|
3929 /* If can't match the null string, and that's all we have left, fail. */
|
|
3930 if (range >= 0 && startpos == total_size && fastmap
|
|
3931 && !bufp->can_be_null)
|
|
3932 return -1;
|
|
3933
|
|
3934 #ifdef emacs /* XEmacs added, w/removal of immediate_quit */
|
|
3935 if (!no_quit_in_re_search)
|
|
3936 QUIT;
|
|
3937 #endif
|
|
3938 val = re_match_2_internal (bufp, string1, size1, string2, size2,
|
|
3939 startpos, regs, stop);
|
|
3940 #ifndef REGEX_MALLOC
|
|
3941 #ifdef C_ALLOCA
|
|
3942 alloca (0);
|
|
3943 #endif
|
|
3944 #endif
|
|
3945
|
|
3946 if (val >= 0)
|
|
3947 return startpos;
|
|
3948
|
|
3949 if (val == -2)
|
|
3950 return -2;
|
|
3951
|
|
3952 advance:
|
|
3953 if (!range)
|
|
3954 break;
|
|
3955 else if (range > 0)
|
|
3956 {
|
|
3957 range--;
|
|
3958 startpos++;
|
|
3959 }
|
|
3960 else
|
|
3961 {
|
|
3962 range++;
|
|
3963 startpos--;
|
|
3964 }
|
|
3965 }
|
|
3966 return -1;
|
|
3967 } /* re_search_2 */
|
|
3968
|
|
3969 /* Declarations and macros for re_match_2. */
|
|
3970
|
|
3971 /* This converts PTR, a pointer into one of the search strings `string1'
|
|
3972 and `string2' into an offset from the beginning of that string. */
|
|
3973 #define POINTER_TO_OFFSET(ptr) \
|
|
3974 (FIRST_STRING_P (ptr) \
|
|
3975 ? ((regoff_t) ((ptr) - string1)) \
|
|
3976 : ((regoff_t) ((ptr) - string2 + size1)))
|
|
3977
|
|
3978 /* Macros for dealing with the split strings in re_match_2. */
|
|
3979
|
|
3980 #define MATCHING_IN_FIRST_STRING (dend == end_match_1)
|
|
3981
|
|
3982 /* Call before fetching a character with *d. This switches over to
|
|
3983 string2 if necessary. */
|
|
3984 #define PREFETCH() \
|
|
3985 while (d == dend) \
|
|
3986 { \
|
|
3987 /* End of string2 => fail. */ \
|
|
3988 if (dend == end_match_2) \
|
|
3989 goto fail; \
|
|
3990 /* End of string1 => advance to string2. */ \
|
|
3991 d = string2; \
|
|
3992 dend = end_match_2; \
|
|
3993 }
|
|
3994
|
|
3995
|
|
3996 /* Test if at very beginning or at very end of the virtual concatenation
|
|
3997 of `string1' and `string2'. If only one string, it's `string2'. */
|
|
3998 #define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2)
|
|
3999 #define AT_STRINGS_END(d) ((d) == end2)
|
|
4000
|
|
4001 /* XEmacs change:
|
|
4002 If the given position straddles the string gap, return the equivalent
|
|
4003 position that is before or after the gap, respectively; otherwise,
|
|
4004 return the same position. */
|
|
4005 #define POS_BEFORE_GAP_UNSAFE(d) ((d) == string2 ? end1 : (d))
|
|
4006 #define POS_AFTER_GAP_UNSAFE(d) ((d) == end1 ? string2 : (d))
|
|
4007
|
|
4008 /* Test if CH is a word-constituent character. (XEmacs change) */
|
|
4009 #define WORDCHAR_P_UNSAFE(ch) \
|
70
|
4010 (SYNTAX_UNSAFE (XCHAR_TABLE (regex_emacs_buffer->mirror_syntax_table), \
|
|
4011 ch) == Sword)
|
0
|
4012
|
|
4013 /* Free everything we malloc. */
|
|
4014 #ifdef MATCH_MAY_ALLOCATE
|
|
4015 #define FREE_VAR(var) if (var) REGEX_FREE (var); var = NULL
|
|
4016 #define FREE_VARIABLES() \
|
|
4017 do { \
|
|
4018 REGEX_FREE_STACK (fail_stack.stack); \
|
|
4019 FREE_VAR (regstart); \
|
|
4020 FREE_VAR (regend); \
|
|
4021 FREE_VAR (old_regstart); \
|
|
4022 FREE_VAR (old_regend); \
|
|
4023 FREE_VAR (best_regstart); \
|
|
4024 FREE_VAR (best_regend); \
|
|
4025 FREE_VAR (reg_info); \
|
|
4026 FREE_VAR (reg_dummy); \
|
|
4027 FREE_VAR (reg_info_dummy); \
|
|
4028 } while (0)
|
|
4029 #else
|
|
4030 #define FREE_VARIABLES() ((void)0) /* Do nothing! But inhibit gcc warning. */
|
|
4031 #endif /* not MATCH_MAY_ALLOCATE */
|
|
4032
|
|
4033 /* These values must meet several constraints. They must not be valid
|
|
4034 register values; since we have a limit of 255 registers (because
|
|
4035 we use only one byte in the pattern for the register number), we can
|
|
4036 use numbers larger than 255. They must differ by 1, because of
|
|
4037 NUM_FAILURE_ITEMS above. And the value for the lowest register must
|
|
4038 be larger than the value for the highest register, so we do not try
|
|
4039 to actually save any registers when none are active. */
|
|
4040 #define NO_HIGHEST_ACTIVE_REG (1 << BYTEWIDTH)
|
|
4041 #define NO_LOWEST_ACTIVE_REG (NO_HIGHEST_ACTIVE_REG + 1)
|
|
4042
|
|
4043 /* Matching routines. */
|
|
4044
|
|
4045 #ifndef emacs /* Emacs never uses this. */
|
|
4046 /* re_match is like re_match_2 except it takes only a single string. */
|
|
4047
|
|
4048 int
|
|
4049 re_match (struct re_pattern_buffer *bufp, CONST char *string, int size,
|
|
4050 int pos, struct re_registers *regs)
|
|
4051 {
|
|
4052 int result = re_match_2_internal (bufp, NULL, 0, string, size,
|
|
4053 pos, regs, size);
|
|
4054 alloca (0);
|
|
4055 return result;
|
|
4056 }
|
|
4057 #endif /* not emacs */
|
|
4058
|
|
4059
|
|
4060 /* re_match_2 matches the compiled pattern in BUFP against the
|
2
|
4061 (virtual) concatenation of STRING1 and STRING2 (of length SIZE1 and
|
|
4062 SIZE2, respectively). We start matching at POS, and stop matching
|
|
4063 at STOP.
|
0
|
4064
|
|
4065 If REGS is non-null and the `no_sub' field of BUFP is nonzero, we
|
|
4066 store offsets for the substring each group matched in REGS. See the
|
|
4067 documentation for exactly how many groups we fill.
|
|
4068
|
|
4069 We return -1 if no match, -2 if an internal error (such as the
|
|
4070 failure stack overflowing). Otherwise, we return the length of the
|
|
4071 matched substring. */
|
|
4072
|
|
4073 int
|
|
4074 re_match_2 (struct re_pattern_buffer *bufp, CONST char *string1,
|
|
4075 int size1, CONST char *string2, int size2, int pos,
|
|
4076 struct re_registers *regs, int stop)
|
|
4077 {
|
|
4078 int result = re_match_2_internal (bufp, string1, size1, string2, size2,
|
|
4079 pos, regs, stop);
|
|
4080 alloca (0);
|
|
4081 return result;
|
|
4082 }
|
|
4083
|
|
4084 /* This is a separate function so that we can force an alloca cleanup
|
|
4085 afterwards. */
|
|
4086 static int
|
|
4087 re_match_2_internal (struct re_pattern_buffer *bufp, CONST char *string1,
|
|
4088 int size1, CONST char *string2, int size2, int pos,
|
|
4089 struct re_registers *regs, int stop)
|
|
4090 {
|
|
4091 /* General temporaries. */
|
|
4092 int mcnt;
|
|
4093 unsigned char *p1;
|
|
4094 int should_succeed; /* XEmacs change */
|
|
4095
|
|
4096 /* Just past the end of the corresponding string. */
|
|
4097 CONST char *end1, *end2;
|
|
4098
|
|
4099 /* Pointers into string1 and string2, just past the last characters in
|
|
4100 each to consider matching. */
|
|
4101 CONST char *end_match_1, *end_match_2;
|
|
4102
|
|
4103 /* Where we are in the data, and the end of the current string. */
|
|
4104 CONST char *d, *dend;
|
|
4105
|
|
4106 /* Where we are in the pattern, and the end of the pattern. */
|
|
4107 unsigned char *p = bufp->buffer;
|
|
4108 register unsigned char *pend = p + bufp->used;
|
|
4109
|
|
4110 /* Mark the opcode just after a start_memory, so we can test for an
|
|
4111 empty subpattern when we get to the stop_memory. */
|
|
4112 unsigned char *just_past_start_mem = 0;
|
|
4113
|
|
4114 /* We use this to map every character in the string. */
|
|
4115 char *translate = bufp->translate;
|
|
4116
|
|
4117 /* Failure point stack. Each place that can handle a failure further
|
|
4118 down the line pushes a failure point on this stack. It consists of
|
|
4119 restart, regend, and reg_info for all registers corresponding to
|
|
4120 the subexpressions we're currently inside, plus the number of such
|
|
4121 registers, and, finally, two char *'s. The first char * is where
|
|
4122 to resume scanning the pattern; the second one is where to resume
|
|
4123 scanning the strings. If the latter is zero, the failure point is
|
|
4124 a ``dummy''; if a failure happens and the failure point is a dummy,
|
2
|
4125 it gets discarded and the next one is tried. */
|
0
|
4126 #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
|
|
4127 fail_stack_type fail_stack;
|
|
4128 #endif
|
|
4129 #ifdef DEBUG
|
|
4130 static unsigned failure_id;
|
|
4131 unsigned nfailure_points_pushed = 0, nfailure_points_popped = 0;
|
|
4132 #endif
|
|
4133
|
|
4134 #ifdef REL_ALLOC
|
|
4135 /* This holds the pointer to the failure stack, when
|
|
4136 it is allocated relocatably. */
|
|
4137 fail_stack_elt_t *failure_stack_ptr;
|
|
4138 #endif
|
|
4139
|
|
4140 /* We fill all the registers internally, independent of what we
|
|
4141 return, for use in backreferences. The number here includes
|
|
4142 an element for register zero. */
|
|
4143 unsigned num_regs = bufp->re_nsub + 1;
|
|
4144
|
|
4145 /* The currently active registers. */
|
|
4146 unsigned lowest_active_reg = NO_LOWEST_ACTIVE_REG;
|
|
4147 unsigned highest_active_reg = NO_HIGHEST_ACTIVE_REG;
|
|
4148
|
|
4149 /* Information on the contents of registers. These are pointers into
|
|
4150 the input strings; they record just what was matched (on this
|
|
4151 attempt) by a subexpression part of the pattern, that is, the
|
|
4152 regnum-th regstart pointer points to where in the pattern we began
|
|
4153 matching and the regnum-th regend points to right after where we
|
|
4154 stopped matching the regnum-th subexpression. (The zeroth register
|
|
4155 keeps track of what the whole pattern matches.) */
|
|
4156 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
|
|
4157 CONST char **regstart, **regend;
|
|
4158 #endif
|
|
4159
|
|
4160 /* If a group that's operated upon by a repetition operator fails to
|
|
4161 match anything, then the register for its start will need to be
|
|
4162 restored because it will have been set to wherever in the string we
|
|
4163 are when we last see its open-group operator. Similarly for a
|
|
4164 register's end. */
|
|
4165 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
|
|
4166 CONST char **old_regstart, **old_regend;
|
|
4167 #endif
|
|
4168
|
|
4169 /* The is_active field of reg_info helps us keep track of which (possibly
|
|
4170 nested) subexpressions we are currently in. The matched_something
|
|
4171 field of reg_info[reg_num] helps us tell whether or not we have
|
|
4172 matched any of the pattern so far this time through the reg_num-th
|
|
4173 subexpression. These two fields get reset each time through any
|
|
4174 loop their register is in. */
|
|
4175 #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
|
|
4176 register_info_type *reg_info;
|
|
4177 #endif
|
|
4178
|
|
4179 /* The following record the register info as found in the above
|
|
4180 variables when we find a match better than any we've seen before.
|
|
4181 This happens as we backtrack through the failure points, which in
|
|
4182 turn happens only if we have not yet matched the entire string. */
|
|
4183 unsigned best_regs_set = false;
|
|
4184 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
|
|
4185 CONST char **best_regstart, **best_regend;
|
|
4186 #endif
|
|
4187
|
|
4188 /* Logically, this is `best_regend[0]'. But we don't want to have to
|
|
4189 allocate space for that if we're not allocating space for anything
|
|
4190 else (see below). Also, we never need info about register 0 for
|
|
4191 any of the other register vectors, and it seems rather a kludge to
|
|
4192 treat `best_regend' differently than the rest. So we keep track of
|
|
4193 the end of the best match so far in a separate variable. We
|
|
4194 initialize this to NULL so that when we backtrack the first time
|
|
4195 and need to test it, it's not garbage. */
|
|
4196 CONST char *match_end = NULL;
|
|
4197
|
|
4198 /* This helps SET_REGS_MATCHED avoid doing redundant work. */
|
|
4199 int set_regs_matched_done = 0;
|
|
4200
|
|
4201 /* Used when we pop values we don't care about. */
|
|
4202 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
|
|
4203 CONST char **reg_dummy;
|
|
4204 register_info_type *reg_info_dummy;
|
|
4205 #endif
|
|
4206
|
|
4207 #ifdef DEBUG
|
|
4208 /* Counts the total number of registers pushed. */
|
|
4209 unsigned num_regs_pushed = 0;
|
|
4210 #endif
|
|
4211
|
|
4212 DEBUG_PRINT1 ("\n\nEntering re_match_2.\n");
|
|
4213
|
|
4214 INIT_FAIL_STACK ();
|
|
4215
|
|
4216 #ifdef MATCH_MAY_ALLOCATE
|
|
4217 /* Do not bother to initialize all the register variables if there are
|
|
4218 no groups in the pattern, as it takes a fair amount of time. If
|
|
4219 there are groups, we include space for register 0 (the whole
|
|
4220 pattern), even though we never use it, since it simplifies the
|
|
4221 array indexing. We should fix this. */
|
|
4222 if (bufp->re_nsub)
|
|
4223 {
|
|
4224 regstart = REGEX_TALLOC (num_regs, CONST char *);
|
|
4225 regend = REGEX_TALLOC (num_regs, CONST char *);
|
|
4226 old_regstart = REGEX_TALLOC (num_regs, CONST char *);
|
|
4227 old_regend = REGEX_TALLOC (num_regs, CONST char *);
|
|
4228 best_regstart = REGEX_TALLOC (num_regs, CONST char *);
|
|
4229 best_regend = REGEX_TALLOC (num_regs, CONST char *);
|
|
4230 reg_info = REGEX_TALLOC (num_regs, register_info_type);
|
|
4231 reg_dummy = REGEX_TALLOC (num_regs, CONST char *);
|
|
4232 reg_info_dummy = REGEX_TALLOC (num_regs, register_info_type);
|
|
4233
|
|
4234 if (!(regstart && regend && old_regstart && old_regend && reg_info
|
|
4235 && best_regstart && best_regend && reg_dummy && reg_info_dummy))
|
|
4236 {
|
|
4237 FREE_VARIABLES ();
|
|
4238 return -2;
|
|
4239 }
|
|
4240 }
|
|
4241 else
|
|
4242 {
|
|
4243 /* We must initialize all our variables to NULL, so that
|
|
4244 `FREE_VARIABLES' doesn't try to free them. */
|
|
4245 regstart = regend = old_regstart = old_regend = best_regstart
|
|
4246 = best_regend = reg_dummy = NULL;
|
|
4247 reg_info = reg_info_dummy = (register_info_type *) NULL;
|
|
4248 }
|
|
4249 #endif /* MATCH_MAY_ALLOCATE */
|
|
4250
|
|
4251 /* The starting position is bogus. */
|
|
4252 if (pos < 0 || pos > size1 + size2)
|
|
4253 {
|
|
4254 FREE_VARIABLES ();
|
|
4255 return -1;
|
|
4256 }
|
|
4257
|
|
4258 /* Initialize subexpression text positions to -1 to mark ones that no
|
|
4259 start_memory/stop_memory has been seen for. Also initialize the
|
|
4260 register information struct. */
|
|
4261 for (mcnt = 1; mcnt < num_regs; mcnt++)
|
|
4262 {
|
|
4263 regstart[mcnt] = regend[mcnt]
|
|
4264 = old_regstart[mcnt] = old_regend[mcnt] = REG_UNSET_VALUE;
|
|
4265
|
|
4266 REG_MATCH_NULL_STRING_P (reg_info[mcnt]) = MATCH_NULL_UNSET_VALUE;
|
|
4267 IS_ACTIVE (reg_info[mcnt]) = 0;
|
|
4268 MATCHED_SOMETHING (reg_info[mcnt]) = 0;
|
|
4269 EVER_MATCHED_SOMETHING (reg_info[mcnt]) = 0;
|
|
4270 }
|
|
4271
|
|
4272 /* We move `string1' into `string2' if the latter's empty -- but not if
|
|
4273 `string1' is null. */
|
|
4274 if (size2 == 0 && string1 != NULL)
|
|
4275 {
|
|
4276 string2 = string1;
|
|
4277 size2 = size1;
|
|
4278 string1 = 0;
|
|
4279 size1 = 0;
|
|
4280 }
|
|
4281 end1 = string1 + size1;
|
|
4282 end2 = string2 + size2;
|
|
4283
|
|
4284 /* Compute where to stop matching, within the two strings. */
|
|
4285 if (stop <= size1)
|
|
4286 {
|
|
4287 end_match_1 = string1 + stop;
|
|
4288 end_match_2 = string2;
|
|
4289 }
|
|
4290 else
|
|
4291 {
|
|
4292 end_match_1 = end1;
|
|
4293 end_match_2 = string2 + stop - size1;
|
|
4294 }
|
|
4295
|
|
4296 /* `p' scans through the pattern as `d' scans through the data.
|
|
4297 `dend' is the end of the input string that `d' points within. `d'
|
|
4298 is advanced into the following input string whenever necessary, but
|
|
4299 this happens before fetching; therefore, at the beginning of the
|
|
4300 loop, `d' can be pointing at the end of a string, but it cannot
|
|
4301 equal `string2'. */
|
|
4302 if (size1 > 0 && pos <= size1)
|
|
4303 {
|
|
4304 d = string1 + pos;
|
|
4305 dend = end_match_1;
|
|
4306 }
|
|
4307 else
|
|
4308 {
|
|
4309 d = string2 + pos - size1;
|
|
4310 dend = end_match_2;
|
|
4311 }
|
|
4312
|
|
4313 DEBUG_PRINT1 ("The compiled pattern is: ");
|
|
4314 DEBUG_PRINT_COMPILED_PATTERN (bufp, p, pend);
|
|
4315 DEBUG_PRINT1 ("The string to match is: `");
|
|
4316 DEBUG_PRINT_DOUBLE_STRING (d, string1, size1, string2, size2);
|
|
4317 DEBUG_PRINT1 ("'\n");
|
|
4318
|
|
4319 /* This loops over pattern commands. It exits by returning from the
|
|
4320 function if the match is complete, or it drops through if the match
|
|
4321 fails at this starting point in the input data. */
|
|
4322 for (;;)
|
|
4323 {
|
|
4324 DEBUG_PRINT2 ("\n0x%lx: ", (unsigned long) p);
|
|
4325
|
|
4326 if (p == pend)
|
|
4327 { /* End of pattern means we might have succeeded. */
|
|
4328 DEBUG_PRINT1 ("end of pattern ... ");
|
|
4329
|
|
4330 /* If we haven't matched the entire string, and we want the
|
|
4331 longest match, try backtracking. */
|
|
4332 if (d != end_match_2)
|
|
4333 {
|
|
4334 /* 1 if this match ends in the same string (string1 or string2)
|
|
4335 as the best previous match. */
|
|
4336 boolean same_str_p = (FIRST_STRING_P (match_end)
|
|
4337 == MATCHING_IN_FIRST_STRING);
|
|
4338 /* 1 if this match is the best seen so far. */
|
|
4339 boolean best_match_p;
|
|
4340
|
|
4341 /* AIX compiler got confused when this was combined
|
|
4342 with the previous declaration. */
|
|
4343 if (same_str_p)
|
|
4344 best_match_p = d > match_end;
|
|
4345 else
|
|
4346 best_match_p = !MATCHING_IN_FIRST_STRING;
|
|
4347
|
|
4348 DEBUG_PRINT1 ("backtracking.\n");
|
|
4349
|
|
4350 if (!FAIL_STACK_EMPTY ())
|
|
4351 { /* More failure points to try. */
|
|
4352
|
|
4353 /* If exceeds best match so far, save it. */
|
|
4354 if (!best_regs_set || best_match_p)
|
|
4355 {
|
|
4356 best_regs_set = true;
|
|
4357 match_end = d;
|
|
4358
|
|
4359 DEBUG_PRINT1 ("\nSAVING match as best so far.\n");
|
|
4360
|
|
4361 for (mcnt = 1; mcnt < num_regs; mcnt++)
|
|
4362 {
|
|
4363 best_regstart[mcnt] = regstart[mcnt];
|
|
4364 best_regend[mcnt] = regend[mcnt];
|
|
4365 }
|
|
4366 }
|
|
4367 goto fail;
|
|
4368 }
|
|
4369
|
|
4370 /* If no failure points, don't restore garbage. And if
|
|
4371 last match is real best match, don't restore second
|
|
4372 best one. */
|
|
4373 else if (best_regs_set && !best_match_p)
|
|
4374 {
|
|
4375 restore_best_regs:
|
|
4376 /* Restore best match. It may happen that `dend ==
|
|
4377 end_match_1' while the restored d is in string2.
|
|
4378 For example, the pattern `x.*y.*z' against the
|
|
4379 strings `x-' and `y-z-', if the two strings are
|
|
4380 not consecutive in memory. */
|
|
4381 DEBUG_PRINT1 ("Restoring best registers.\n");
|
|
4382
|
|
4383 d = match_end;
|
|
4384 dend = ((d >= string1 && d <= end1)
|
|
4385 ? end_match_1 : end_match_2);
|
|
4386
|
|
4387 for (mcnt = 1; mcnt < num_regs; mcnt++)
|
|
4388 {
|
|
4389 regstart[mcnt] = best_regstart[mcnt];
|
|
4390 regend[mcnt] = best_regend[mcnt];
|
|
4391 }
|
|
4392 }
|
|
4393 } /* d != end_match_2 */
|
|
4394
|
|
4395 succeed_label:
|
|
4396 DEBUG_PRINT1 ("Accepting match.\n");
|
|
4397
|
|
4398 /* If caller wants register contents data back, do it. */
|
|
4399 if (regs && !bufp->no_sub)
|
|
4400 {
|
|
4401 /* Have the register data arrays been allocated? */
|
|
4402 if (bufp->regs_allocated == REGS_UNALLOCATED)
|
|
4403 { /* No. So allocate them with malloc. We need one
|
|
4404 extra element beyond `num_regs' for the `-1' marker
|
|
4405 GNU code uses. */
|
|
4406 regs->num_regs = MAX (RE_NREGS, num_regs + 1);
|
|
4407 regs->start = TALLOC (regs->num_regs, regoff_t);
|
|
4408 regs->end = TALLOC (regs->num_regs, regoff_t);
|
|
4409 if (regs->start == NULL || regs->end == NULL)
|
|
4410 {
|
|
4411 FREE_VARIABLES ();
|
|
4412 return -2;
|
|
4413 }
|
|
4414 bufp->regs_allocated = REGS_REALLOCATE;
|
|
4415 }
|
|
4416 else if (bufp->regs_allocated == REGS_REALLOCATE)
|
|
4417 { /* Yes. If we need more elements than were already
|
|
4418 allocated, reallocate them. If we need fewer, just
|
|
4419 leave it alone. */
|
|
4420 if (regs->num_regs < num_regs + 1)
|
|
4421 {
|
|
4422 regs->num_regs = num_regs + 1;
|
|
4423 RETALLOC (regs->start, regs->num_regs, regoff_t);
|
|
4424 RETALLOC (regs->end, regs->num_regs, regoff_t);
|
|
4425 if (regs->start == NULL || regs->end == NULL)
|
|
4426 {
|
|
4427 FREE_VARIABLES ();
|
|
4428 return -2;
|
|
4429 }
|
|
4430 }
|
|
4431 }
|
|
4432 else
|
|
4433 {
|
|
4434 /* These braces fend off a "empty body in an else-statement"
|
|
4435 warning under GCC when assert expands to nothing. */
|
|
4436 assert (bufp->regs_allocated == REGS_FIXED);
|
|
4437 }
|
|
4438
|
|
4439 /* Convert the pointer data in `regstart' and `regend' to
|
|
4440 indices. Register zero has to be set differently,
|
|
4441 since we haven't kept track of any info for it. */
|
|
4442 if (regs->num_regs > 0)
|
|
4443 {
|
|
4444 regs->start[0] = pos;
|
|
4445 regs->end[0] = (MATCHING_IN_FIRST_STRING
|
|
4446 ? ((regoff_t) (d - string1))
|
|
4447 : ((regoff_t) (d - string2 + size1)));
|
|
4448 }
|
|
4449
|
|
4450 /* Go through the first `min (num_regs, regs->num_regs)'
|
|
4451 registers, since that is all we initialized. */
|
|
4452 for (mcnt = 1; mcnt < MIN (num_regs, regs->num_regs); mcnt++)
|
|
4453 {
|
|
4454 if (REG_UNSET (regstart[mcnt]) || REG_UNSET (regend[mcnt]))
|
|
4455 regs->start[mcnt] = regs->end[mcnt] = -1;
|
|
4456 else
|
|
4457 {
|
|
4458 regs->start[mcnt]
|
|
4459 = (regoff_t) POINTER_TO_OFFSET (regstart[mcnt]);
|
|
4460 regs->end[mcnt]
|
|
4461 = (regoff_t) POINTER_TO_OFFSET (regend[mcnt]);
|
|
4462 }
|
|
4463 }
|
|
4464
|
|
4465 /* If the regs structure we return has more elements than
|
|
4466 were in the pattern, set the extra elements to -1. If
|
|
4467 we (re)allocated the registers, this is the case,
|
|
4468 because we always allocate enough to have at least one
|
|
4469 -1 at the end. */
|
|
4470 for (mcnt = num_regs; mcnt < regs->num_regs; mcnt++)
|
|
4471 regs->start[mcnt] = regs->end[mcnt] = -1;
|
|
4472 } /* regs && !bufp->no_sub */
|
|
4473
|
|
4474 DEBUG_PRINT4 ("%u failure points pushed, %u popped (%u remain).\n",
|
|
4475 nfailure_points_pushed, nfailure_points_popped,
|
|
4476 nfailure_points_pushed - nfailure_points_popped);
|
|
4477 DEBUG_PRINT2 ("%u registers pushed.\n", num_regs_pushed);
|
|
4478
|
|
4479 mcnt = d - pos - (MATCHING_IN_FIRST_STRING
|
|
4480 ? string1
|
|
4481 : string2 - size1);
|
|
4482
|
|
4483 DEBUG_PRINT2 ("Returning %d from re_match_2.\n", mcnt);
|
|
4484
|
|
4485 FREE_VARIABLES ();
|
|
4486 return mcnt;
|
|
4487 }
|
|
4488
|
|
4489 /* Otherwise match next pattern command. */
|
|
4490 switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++))
|
|
4491 {
|
|
4492 /* Ignore these. Used to ignore the n of succeed_n's which
|
|
4493 currently have n == 0. */
|
|
4494 case no_op:
|
|
4495 DEBUG_PRINT1 ("EXECUTING no_op.\n");
|
|
4496 break;
|
|
4497
|
|
4498 case succeed:
|
|
4499 DEBUG_PRINT1 ("EXECUTING succeed.\n");
|
|
4500 goto succeed_label;
|
|
4501
|
|
4502 /* Match the next n pattern characters exactly. The following
|
|
4503 byte in the pattern defines n, and the n bytes after that
|
|
4504 are the characters to match. */
|
|
4505 case exactn:
|
|
4506 mcnt = *p++;
|
|
4507 DEBUG_PRINT2 ("EXECUTING exactn %d.\n", mcnt);
|
|
4508
|
|
4509 /* This is written out as an if-else so we don't waste time
|
|
4510 testing `translate' inside the loop. */
|
|
4511 if (translate)
|
|
4512 {
|
|
4513 do
|
|
4514 {
|
|
4515 PREFETCH ();
|
|
4516 if (translate[(unsigned char) *d++] != (char) *p++)
|
|
4517 goto fail;
|
|
4518 }
|
|
4519 while (--mcnt);
|
|
4520 }
|
|
4521 else
|
|
4522 {
|
|
4523 do
|
|
4524 {
|
|
4525 PREFETCH ();
|
|
4526 if (*d++ != (char) *p++) goto fail;
|
|
4527 }
|
|
4528 while (--mcnt);
|
|
4529 }
|
|
4530 SET_REGS_MATCHED ();
|
|
4531 break;
|
|
4532
|
|
4533
|
|
4534 /* Match any character except possibly a newline or a null. */
|
|
4535 case anychar:
|
|
4536 DEBUG_PRINT1 ("EXECUTING anychar.\n");
|
|
4537
|
|
4538 PREFETCH ();
|
|
4539
|
|
4540 if ((!(bufp->syntax & RE_DOT_NEWLINE) && TRANSLATE (*d) == '\n')
|
|
4541 || (bufp->syntax & RE_DOT_NOT_NULL && TRANSLATE (*d) == '\000'))
|
|
4542 goto fail;
|
|
4543
|
|
4544 SET_REGS_MATCHED ();
|
|
4545 DEBUG_PRINT2 (" Matched `%d'.\n", *d);
|
|
4546 INC_CHARPTR (d); /* XEmacs change */
|
|
4547 break;
|
|
4548
|
|
4549
|
|
4550 case charset:
|
|
4551 case charset_not:
|
|
4552 {
|
|
4553 register unsigned char c;
|
|
4554 boolean not = (re_opcode_t) *(p - 1) == charset_not;
|
|
4555
|
|
4556 DEBUG_PRINT2 ("EXECUTING charset%s.\n", not ? "_not" : "");
|
|
4557
|
|
4558 PREFETCH ();
|
|
4559 c = TRANSLATE (*d); /* The character to match. */
|
|
4560
|
|
4561 /* Cast to `unsigned' instead of `unsigned char' in case the
|
|
4562 bit list is a full 32 bytes long. */
|
|
4563 if (c < (unsigned) (*p * BYTEWIDTH)
|
|
4564 && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
|
|
4565 not = !not;
|
|
4566
|
|
4567 p += 1 + *p;
|
|
4568
|
|
4569 if (!not) goto fail;
|
|
4570
|
|
4571 SET_REGS_MATCHED ();
|
70
|
4572 INC_CHARPTR (d); /* XEmacs change */
|
0
|
4573 break;
|
|
4574 }
|
|
4575
|
70
|
4576 #ifdef MULE
|
|
4577 case charset_mule:
|
|
4578 case charset_mule_not:
|
|
4579 {
|
|
4580 register Emchar c;
|
|
4581 boolean not = (re_opcode_t) *(p - 1) == charset_mule_not;
|
|
4582
|
|
4583 DEBUG_PRINT2 ("EXECUTING charset_mule%s.\n", not ? "_not" : "");
|
|
4584
|
|
4585 PREFETCH ();
|
|
4586 c = charptr_emchar ((CONST Bufbyte *) d);
|
|
4587 c = TRANSLATE_EXTENDED_UNSAFE (c); /* The character to match. */
|
|
4588
|
|
4589 if (EQ (Qt, unified_range_table_lookup (p, c, Qnil)))
|
|
4590 not = !not;
|
|
4591
|
|
4592 p += unified_range_table_bytes_used (p);
|
|
4593
|
|
4594 if (!not) goto fail;
|
|
4595
|
|
4596 SET_REGS_MATCHED ();
|
|
4597 INC_CHARPTR (d);
|
|
4598 break;
|
|
4599 }
|
|
4600 #endif
|
|
4601
|
0
|
4602
|
|
4603 /* The beginning of a group is represented by start_memory.
|
|
4604 The arguments are the register number in the next byte, and the
|
|
4605 number of groups inner to this one in the next. The text
|
|
4606 matched within the group is recorded (in the internal
|
|
4607 registers data structure) under the register number. */
|
|
4608 case start_memory:
|
|
4609 DEBUG_PRINT3 ("EXECUTING start_memory %d (%d):\n", *p, p[1]);
|
|
4610
|
|
4611 /* Find out if this group can match the empty string. */
|
|
4612 p1 = p; /* To send to group_match_null_string_p. */
|
|
4613
|
|
4614 if (REG_MATCH_NULL_STRING_P (reg_info[*p]) == MATCH_NULL_UNSET_VALUE)
|
|
4615 REG_MATCH_NULL_STRING_P (reg_info[*p])
|
|
4616 = group_match_null_string_p (&p1, pend, reg_info);
|
|
4617
|
|
4618 /* Save the position in the string where we were the last time
|
|
4619 we were at this open-group operator in case the group is
|
|
4620 operated upon by a repetition operator, e.g., with `(a*)*b'
|
|
4621 against `ab'; then we want to ignore where we are now in
|
|
4622 the string in case this attempt to match fails. */
|
|
4623 old_regstart[*p] = REG_MATCH_NULL_STRING_P (reg_info[*p])
|
|
4624 ? REG_UNSET (regstart[*p]) ? d : regstart[*p]
|
|
4625 : regstart[*p];
|
|
4626 DEBUG_PRINT2 (" old_regstart: %d\n",
|
|
4627 POINTER_TO_OFFSET (old_regstart[*p]));
|
|
4628
|
|
4629 regstart[*p] = d;
|
|
4630 DEBUG_PRINT2 (" regstart: %d\n", POINTER_TO_OFFSET (regstart[*p]));
|
|
4631
|
|
4632 IS_ACTIVE (reg_info[*p]) = 1;
|
|
4633 MATCHED_SOMETHING (reg_info[*p]) = 0;
|
|
4634
|
|
4635 /* Clear this whenever we change the register activity status. */
|
|
4636 set_regs_matched_done = 0;
|
|
4637
|
|
4638 /* This is the new highest active register. */
|
|
4639 highest_active_reg = *p;
|
|
4640
|
|
4641 /* If nothing was active before, this is the new lowest active
|
|
4642 register. */
|
|
4643 if (lowest_active_reg == NO_LOWEST_ACTIVE_REG)
|
|
4644 lowest_active_reg = *p;
|
|
4645
|
|
4646 /* Move past the register number and inner group count. */
|
|
4647 p += 2;
|
|
4648 just_past_start_mem = p;
|
|
4649
|
|
4650 break;
|
|
4651
|
|
4652
|
|
4653 /* The stop_memory opcode represents the end of a group. Its
|
|
4654 arguments are the same as start_memory's: the register
|
|
4655 number, and the number of inner groups. */
|
|
4656 case stop_memory:
|
|
4657 DEBUG_PRINT3 ("EXECUTING stop_memory %d (%d):\n", *p, p[1]);
|
|
4658
|
|
4659 /* We need to save the string position the last time we were at
|
|
4660 this close-group operator in case the group is operated
|
|
4661 upon by a repetition operator, e.g., with `((a*)*(b*)*)*'
|
|
4662 against `aba'; then we want to ignore where we are now in
|
|
4663 the string in case this attempt to match fails. */
|
|
4664 old_regend[*p] = REG_MATCH_NULL_STRING_P (reg_info[*p])
|
|
4665 ? REG_UNSET (regend[*p]) ? d : regend[*p]
|
|
4666 : regend[*p];
|
|
4667 DEBUG_PRINT2 (" old_regend: %d\n",
|
|
4668 POINTER_TO_OFFSET (old_regend[*p]));
|
|
4669
|
|
4670 regend[*p] = d;
|
|
4671 DEBUG_PRINT2 (" regend: %d\n", POINTER_TO_OFFSET (regend[*p]));
|
|
4672
|
|
4673 /* This register isn't active anymore. */
|
|
4674 IS_ACTIVE (reg_info[*p]) = 0;
|
|
4675
|
|
4676 /* Clear this whenever we change the register activity status. */
|
|
4677 set_regs_matched_done = 0;
|
|
4678
|
|
4679 /* If this was the only register active, nothing is active
|
|
4680 anymore. */
|
|
4681 if (lowest_active_reg == highest_active_reg)
|
|
4682 {
|
|
4683 lowest_active_reg = NO_LOWEST_ACTIVE_REG;
|
|
4684 highest_active_reg = NO_HIGHEST_ACTIVE_REG;
|
|
4685 }
|
|
4686 else
|
|
4687 { /* We must scan for the new highest active register, since
|
|
4688 it isn't necessarily one less than now: consider
|
|
4689 (a(b)c(d(e)f)g). When group 3 ends, after the f), the
|
|
4690 new highest active register is 1. */
|
|
4691 unsigned char r = *p - 1;
|
|
4692 while (r > 0 && !IS_ACTIVE (reg_info[r]))
|
|
4693 r--;
|
|
4694
|
|
4695 /* If we end up at register zero, that means that we saved
|
|
4696 the registers as the result of an `on_failure_jump', not
|
|
4697 a `start_memory', and we jumped to past the innermost
|
|
4698 `stop_memory'. For example, in ((.)*) we save
|
|
4699 registers 1 and 2 as a result of the *, but when we pop
|
|
4700 back to the second ), we are at the stop_memory 1.
|
|
4701 Thus, nothing is active. */
|
|
4702 if (r == 0)
|
|
4703 {
|
|
4704 lowest_active_reg = NO_LOWEST_ACTIVE_REG;
|
|
4705 highest_active_reg = NO_HIGHEST_ACTIVE_REG;
|
|
4706 }
|
|
4707 else
|
|
4708 highest_active_reg = r;
|
|
4709 }
|
|
4710
|
|
4711 /* If just failed to match something this time around with a
|
|
4712 group that's operated on by a repetition operator, try to
|
|
4713 force exit from the ``loop'', and restore the register
|
|
4714 information for this group that we had before trying this
|
|
4715 last match. */
|
|
4716 if ((!MATCHED_SOMETHING (reg_info[*p])
|
|
4717 || just_past_start_mem == p - 1)
|
|
4718 && (p + 2) < pend)
|
|
4719 {
|
|
4720 boolean is_a_jump_n = false;
|
|
4721
|
|
4722 p1 = p + 2;
|
|
4723 mcnt = 0;
|
|
4724 switch ((re_opcode_t) *p1++)
|
|
4725 {
|
|
4726 case jump_n:
|
|
4727 is_a_jump_n = true;
|
|
4728 case pop_failure_jump:
|
|
4729 case maybe_pop_jump:
|
|
4730 case jump:
|
|
4731 case dummy_failure_jump:
|
|
4732 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
|
|
4733 if (is_a_jump_n)
|
|
4734 p1 += 2;
|
|
4735 break;
|
|
4736
|
|
4737 default:
|
|
4738 /* do nothing */ ;
|
|
4739 }
|
|
4740 p1 += mcnt;
|
|
4741
|
|
4742 /* If the next operation is a jump backwards in the pattern
|
|
4743 to an on_failure_jump right before the start_memory
|
|
4744 corresponding to this stop_memory, exit from the loop
|
|
4745 by forcing a failure after pushing on the stack the
|
|
4746 on_failure_jump's jump in the pattern, and d. */
|
|
4747 if (mcnt < 0 && (re_opcode_t) *p1 == on_failure_jump
|
|
4748 && (re_opcode_t) p1[3] == start_memory && p1[4] == *p)
|
|
4749 {
|
|
4750 /* If this group ever matched anything, then restore
|
|
4751 what its registers were before trying this last
|
|
4752 failed match, e.g., with `(a*)*b' against `ab' for
|
|
4753 regstart[1], and, e.g., with `((a*)*(b*)*)*'
|
|
4754 against `aba' for regend[3].
|
|
4755
|
|
4756 Also restore the registers for inner groups for,
|
|
4757 e.g., `((a*)(b*))*' against `aba' (register 3 would
|
|
4758 otherwise get trashed). */
|
|
4759
|
|
4760 if (EVER_MATCHED_SOMETHING (reg_info[*p]))
|
|
4761 {
|
|
4762 unsigned r;
|
|
4763
|
|
4764 EVER_MATCHED_SOMETHING (reg_info[*p]) = 0;
|
|
4765
|
|
4766 /* Restore this and inner groups' (if any) registers. */
|
|
4767 for (r = *p; r < *p + *(p + 1); r++)
|
|
4768 {
|
|
4769 regstart[r] = old_regstart[r];
|
|
4770
|
|
4771 /* xx why this test? */
|
|
4772 if (old_regend[r] >= regstart[r])
|
|
4773 regend[r] = old_regend[r];
|
|
4774 }
|
|
4775 }
|
|
4776 p1++;
|
|
4777 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
|
|
4778 PUSH_FAILURE_POINT (p1 + mcnt, d, -2);
|
|
4779
|
|
4780 goto fail;
|
|
4781 }
|
|
4782 }
|
|
4783
|
|
4784 /* Move past the register number and the inner group count. */
|
|
4785 p += 2;
|
|
4786 break;
|
|
4787
|
|
4788
|
|
4789 /* \<digit> has been turned into a `duplicate' command which is
|
|
4790 followed by the numeric value of <digit> as the register number. */
|
|
4791 case duplicate:
|
|
4792 {
|
|
4793 register CONST char *d2, *dend2;
|
|
4794 int regno = *p++; /* Get which register to match against. */
|
|
4795 DEBUG_PRINT2 ("EXECUTING duplicate %d.\n", regno);
|
|
4796
|
|
4797 /* Can't back reference a group which we've never matched. */
|
|
4798 if (REG_UNSET (regstart[regno]) || REG_UNSET (regend[regno]))
|
|
4799 goto fail;
|
|
4800
|
|
4801 /* Where in input to try to start matching. */
|
|
4802 d2 = regstart[regno];
|
|
4803
|
|
4804 /* Where to stop matching; if both the place to start and
|
|
4805 the place to stop matching are in the same string, then
|
|
4806 set to the place to stop, otherwise, for now have to use
|
|
4807 the end of the first string. */
|
|
4808
|
|
4809 dend2 = ((FIRST_STRING_P (regstart[regno])
|
|
4810 == FIRST_STRING_P (regend[regno]))
|
|
4811 ? regend[regno] : end_match_1);
|
|
4812 for (;;)
|
|
4813 {
|
|
4814 /* If necessary, advance to next segment in register
|
|
4815 contents. */
|
|
4816 while (d2 == dend2)
|
|
4817 {
|
|
4818 if (dend2 == end_match_2) break;
|
|
4819 if (dend2 == regend[regno]) break;
|
|
4820
|
|
4821 /* End of string1 => advance to string2. */
|
|
4822 d2 = string2;
|
|
4823 dend2 = regend[regno];
|
|
4824 }
|
|
4825 /* At end of register contents => success */
|
|
4826 if (d2 == dend2) break;
|
|
4827
|
|
4828 /* If necessary, advance to next segment in data. */
|
|
4829 PREFETCH ();
|
|
4830
|
|
4831 /* How many characters left in this segment to match. */
|
|
4832 mcnt = dend - d;
|
|
4833
|
|
4834 /* Want how many consecutive characters we can match in
|
|
4835 one shot, so, if necessary, adjust the count. */
|
|
4836 if (mcnt > dend2 - d2)
|
|
4837 mcnt = dend2 - d2;
|
|
4838
|
|
4839 /* Compare that many; failure if mismatch, else move
|
|
4840 past them. */
|
|
4841 if (translate
|
|
4842 ? bcmp_translate ((unsigned char *) d,
|
|
4843 (unsigned char *) d2, mcnt, translate)
|
|
4844 : memcmp (d, d2, mcnt))
|
|
4845 goto fail;
|
|
4846 d += mcnt, d2 += mcnt;
|
|
4847
|
|
4848 /* Do this because we've match some characters. */
|
|
4849 SET_REGS_MATCHED ();
|
|
4850 }
|
|
4851 }
|
|
4852 break;
|
|
4853
|
|
4854
|
|
4855 /* begline matches the empty string at the beginning of the string
|
|
4856 (unless `not_bol' is set in `bufp'), and, if
|
|
4857 `newline_anchor' is set, after newlines. */
|
|
4858 case begline:
|
|
4859 DEBUG_PRINT1 ("EXECUTING begline.\n");
|
|
4860
|
|
4861 if (AT_STRINGS_BEG (d))
|
|
4862 {
|
|
4863 if (!bufp->not_bol) break;
|
|
4864 }
|
|
4865 else if (d[-1] == '\n' && bufp->newline_anchor)
|
|
4866 {
|
|
4867 break;
|
|
4868 }
|
|
4869 /* In all other cases, we fail. */
|
|
4870 goto fail;
|
|
4871
|
|
4872
|
|
4873 /* endline is the dual of begline. */
|
|
4874 case endline:
|
|
4875 DEBUG_PRINT1 ("EXECUTING endline.\n");
|
|
4876
|
|
4877 if (AT_STRINGS_END (d))
|
|
4878 {
|
|
4879 if (!bufp->not_eol) break;
|
|
4880 }
|
|
4881
|
|
4882 /* We have to ``prefetch'' the next character. */
|
|
4883 else if ((d == end1 ? *string2 : *d) == '\n'
|
|
4884 && bufp->newline_anchor)
|
|
4885 {
|
|
4886 break;
|
|
4887 }
|
|
4888 goto fail;
|
|
4889
|
|
4890
|
|
4891 /* Match at the very beginning of the data. */
|
|
4892 case begbuf:
|
|
4893 DEBUG_PRINT1 ("EXECUTING begbuf.\n");
|
|
4894 if (AT_STRINGS_BEG (d))
|
|
4895 break;
|
|
4896 goto fail;
|
|
4897
|
|
4898
|
|
4899 /* Match at the very end of the data. */
|
|
4900 case endbuf:
|
|
4901 DEBUG_PRINT1 ("EXECUTING endbuf.\n");
|
|
4902 if (AT_STRINGS_END (d))
|
|
4903 break;
|
|
4904 goto fail;
|
|
4905
|
|
4906
|
|
4907 /* on_failure_keep_string_jump is used to optimize `.*\n'. It
|
|
4908 pushes NULL as the value for the string on the stack. Then
|
|
4909 `pop_failure_point' will keep the current value for the
|
|
4910 string, instead of restoring it. To see why, consider
|
|
4911 matching `foo\nbar' against `.*\n'. The .* matches the foo;
|
|
4912 then the . fails against the \n. But the next thing we want
|
|
4913 to do is match the \n against the \n; if we restored the
|
|
4914 string value, we would be back at the foo.
|
|
4915
|
|
4916 Because this is used only in specific cases, we don't need to
|
|
4917 check all the things that `on_failure_jump' does, to make
|
|
4918 sure the right things get saved on the stack. Hence we don't
|
|
4919 share its code. The only reason to push anything on the
|
|
4920 stack at all is that otherwise we would have to change
|
|
4921 `anychar's code to do something besides goto fail in this
|
|
4922 case; that seems worse than this. */
|
|
4923 case on_failure_keep_string_jump:
|
|
4924 DEBUG_PRINT1 ("EXECUTING on_failure_keep_string_jump");
|
|
4925
|
|
4926 EXTRACT_NUMBER_AND_INCR (mcnt, p);
|
|
4927 DEBUG_PRINT3 (" %d (to 0x%lx):\n", mcnt, (unsigned long) (p + mcnt));
|
|
4928
|
|
4929 PUSH_FAILURE_POINT (p + mcnt, NULL, -2);
|
|
4930 break;
|
|
4931
|
|
4932
|
|
4933 /* Uses of on_failure_jump:
|
|
4934
|
|
4935 Each alternative starts with an on_failure_jump that points
|
|
4936 to the beginning of the next alternative. Each alternative
|
|
4937 except the last ends with a jump that in effect jumps past
|
|
4938 the rest of the alternatives. (They really jump to the
|
|
4939 ending jump of the following alternative, because tensioning
|
|
4940 these jumps is a hassle.)
|
|
4941
|
|
4942 Repeats start with an on_failure_jump that points past both
|
|
4943 the repetition text and either the following jump or
|
|
4944 pop_failure_jump back to this on_failure_jump. */
|
|
4945 case on_failure_jump:
|
|
4946 on_failure:
|
|
4947 DEBUG_PRINT1 ("EXECUTING on_failure_jump");
|
|
4948
|
|
4949 EXTRACT_NUMBER_AND_INCR (mcnt, p);
|
|
4950 DEBUG_PRINT3 (" %d (to 0x%lx)", mcnt, (unsigned long) (p + mcnt));
|
|
4951
|
|
4952 /* If this on_failure_jump comes right before a group (i.e.,
|
|
4953 the original * applied to a group), save the information
|
|
4954 for that group and all inner ones, so that if we fail back
|
|
4955 to this point, the group's information will be correct.
|
|
4956 For example, in \(a*\)*\1, we need the preceding group,
|
|
4957 and in \(\(a*\)b*\)\2, we need the inner group. */
|
|
4958
|
|
4959 /* We can't use `p' to check ahead because we push
|
|
4960 a failure point to `p + mcnt' after we do this. */
|
|
4961 p1 = p;
|
|
4962
|
|
4963 /* We need to skip no_op's before we look for the
|
|
4964 start_memory in case this on_failure_jump is happening as
|
|
4965 the result of a completed succeed_n, as in \(a\)\{1,3\}b\1
|
|
4966 against aba. */
|
|
4967 while (p1 < pend && (re_opcode_t) *p1 == no_op)
|
|
4968 p1++;
|
|
4969
|
|
4970 if (p1 < pend && (re_opcode_t) *p1 == start_memory)
|
|
4971 {
|
|
4972 /* We have a new highest active register now. This will
|
|
4973 get reset at the start_memory we are about to get to,
|
|
4974 but we will have saved all the registers relevant to
|
|
4975 this repetition op, as described above. */
|
|
4976 highest_active_reg = *(p1 + 1) + *(p1 + 2);
|
|
4977 if (lowest_active_reg == NO_LOWEST_ACTIVE_REG)
|
|
4978 lowest_active_reg = *(p1 + 1);
|
|
4979 }
|
|
4980
|
|
4981 DEBUG_PRINT1 (":\n");
|
|
4982 PUSH_FAILURE_POINT (p + mcnt, d, -2);
|
|
4983 break;
|
|
4984
|
|
4985
|
|
4986 /* A smart repeat ends with `maybe_pop_jump'.
|
|
4987 We change it to either `pop_failure_jump' or `jump'. */
|
|
4988 case maybe_pop_jump:
|
|
4989 EXTRACT_NUMBER_AND_INCR (mcnt, p);
|
|
4990 DEBUG_PRINT2 ("EXECUTING maybe_pop_jump %d.\n", mcnt);
|
|
4991 {
|
|
4992 register unsigned char *p2 = p;
|
|
4993
|
|
4994 /* Compare the beginning of the repeat with what in the
|
|
4995 pattern follows its end. If we can establish that there
|
|
4996 is nothing that they would both match, i.e., that we
|
|
4997 would have to backtrack because of (as in, e.g., `a*a')
|
|
4998 then we can change to pop_failure_jump, because we'll
|
|
4999 never have to backtrack.
|
|
5000
|
|
5001 This is not true in the case of alternatives: in
|
|
5002 `(a|ab)*' we do need to backtrack to the `ab' alternative
|
|
5003 (e.g., if the string was `ab'). But instead of trying to
|
|
5004 detect that here, the alternative has put on a dummy
|
|
5005 failure point which is what we will end up popping. */
|
|
5006
|
|
5007 /* Skip over open/close-group commands.
|
|
5008 If what follows this loop is a ...+ construct,
|
|
5009 look at what begins its body, since we will have to
|
|
5010 match at least one of that. */
|
|
5011 while (1)
|
|
5012 {
|
|
5013 if (p2 + 2 < pend
|
|
5014 && ((re_opcode_t) *p2 == stop_memory
|
|
5015 || (re_opcode_t) *p2 == start_memory))
|
|
5016 p2 += 3;
|
|
5017 else if (p2 + 6 < pend
|
|
5018 && (re_opcode_t) *p2 == dummy_failure_jump)
|
|
5019 p2 += 6;
|
|
5020 else
|
|
5021 break;
|
|
5022 }
|
|
5023
|
|
5024 p1 = p + mcnt;
|
|
5025 /* p1[0] ... p1[2] are the `on_failure_jump' corresponding
|
|
5026 to the `maybe_finalize_jump' of this case. Examine what
|
|
5027 follows. */
|
|
5028
|
|
5029 /* If we're at the end of the pattern, we can change. */
|
|
5030 if (p2 == pend)
|
|
5031 {
|
|
5032 /* Consider what happens when matching ":\(.*\)"
|
|
5033 against ":/". I don't really understand this code
|
|
5034 yet. */
|
|
5035 p[-3] = (unsigned char) pop_failure_jump;
|
|
5036 DEBUG_PRINT1
|
|
5037 (" End of pattern: change to `pop_failure_jump'.\n");
|
|
5038 }
|
|
5039
|
|
5040 else if ((re_opcode_t) *p2 == exactn
|
|
5041 || (bufp->newline_anchor && (re_opcode_t) *p2 == endline))
|
|
5042 {
|
|
5043 register unsigned char c
|
|
5044 = *p2 == (unsigned char) endline ? '\n' : p2[2];
|
|
5045
|
|
5046 if ((re_opcode_t) p1[3] == exactn && p1[5] != c)
|
|
5047 {
|
|
5048 p[-3] = (unsigned char) pop_failure_jump;
|
|
5049 DEBUG_PRINT3 (" %c != %c => pop_failure_jump.\n",
|
|
5050 c, p1[5]);
|
|
5051 }
|
|
5052
|
|
5053 else if ((re_opcode_t) p1[3] == charset
|
|
5054 || (re_opcode_t) p1[3] == charset_not)
|
|
5055 {
|
|
5056 int not = (re_opcode_t) p1[3] == charset_not;
|
|
5057
|
|
5058 if (c < (unsigned char) (p1[4] * BYTEWIDTH)
|
|
5059 && p1[5 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
|
|
5060 not = !not;
|
|
5061
|
|
5062 /* `not' is equal to 1 if c would match, which means
|
|
5063 that we can't change to pop_failure_jump. */
|
|
5064 if (!not)
|
|
5065 {
|
|
5066 p[-3] = (unsigned char) pop_failure_jump;
|
|
5067 DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
|
|
5068 }
|
|
5069 }
|
|
5070 }
|
|
5071 else if ((re_opcode_t) *p2 == charset)
|
|
5072 {
|
|
5073 #ifdef DEBUG
|
|
5074 register unsigned char c
|
|
5075 = *p2 == (unsigned char) endline ? '\n' : p2[2];
|
|
5076 #endif
|
|
5077
|
|
5078 if ((re_opcode_t) p1[3] == exactn
|
70
|
5079 && ! ((int) p2[1] * BYTEWIDTH > (int) p1[4]
|
|
5080 && (p2[1 + p1[4] / BYTEWIDTH]
|
|
5081 & (1 << (p1[4] % BYTEWIDTH)))))
|
0
|
5082 {
|
|
5083 p[-3] = (unsigned char) pop_failure_jump;
|
|
5084 DEBUG_PRINT3 (" %c != %c => pop_failure_jump.\n",
|
|
5085 c, p1[5]);
|
|
5086 }
|
|
5087
|
|
5088 else if ((re_opcode_t) p1[3] == charset_not)
|
|
5089 {
|
|
5090 int idx;
|
|
5091 /* We win if the charset_not inside the loop
|
|
5092 lists every character listed in the charset after. */
|
|
5093 for (idx = 0; idx < (int) p2[1]; idx++)
|
|
5094 if (! (p2[2 + idx] == 0
|
|
5095 || (idx < (int) p1[4]
|
|
5096 && ((p2[2 + idx] & ~ p1[5 + idx]) == 0))))
|
|
5097 break;
|
|
5098
|
|
5099 if (idx == p2[1])
|
|
5100 {
|
|
5101 p[-3] = (unsigned char) pop_failure_jump;
|
|
5102 DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
|
|
5103 }
|
|
5104 }
|
|
5105 else if ((re_opcode_t) p1[3] == charset)
|
|
5106 {
|
|
5107 int idx;
|
|
5108 /* We win if the charset inside the loop
|
|
5109 has no overlap with the one after the loop. */
|
|
5110 for (idx = 0;
|
|
5111 idx < (int) p2[1] && idx < (int) p1[4];
|
|
5112 idx++)
|
|
5113 if ((p2[2 + idx] & p1[5 + idx]) != 0)
|
|
5114 break;
|
|
5115
|
|
5116 if (idx == p2[1] || idx == p1[4])
|
|
5117 {
|
|
5118 p[-3] = (unsigned char) pop_failure_jump;
|
|
5119 DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
|
|
5120 }
|
|
5121 }
|
|
5122 }
|
|
5123 }
|
|
5124 p -= 2; /* Point at relative address again. */
|
|
5125 if ((re_opcode_t) p[-1] != pop_failure_jump)
|
|
5126 {
|
|
5127 p[-1] = (unsigned char) jump;
|
|
5128 DEBUG_PRINT1 (" Match => jump.\n");
|
|
5129 goto unconditional_jump;
|
|
5130 }
|
|
5131 /* Note fall through. */
|
|
5132
|
|
5133
|
|
5134 /* The end of a simple repeat has a pop_failure_jump back to
|
|
5135 its matching on_failure_jump, where the latter will push a
|
|
5136 failure point. The pop_failure_jump takes off failure
|
|
5137 points put on by this pop_failure_jump's matching
|
|
5138 on_failure_jump; we got through the pattern to here from the
|
|
5139 matching on_failure_jump, so didn't fail. */
|
|
5140 case pop_failure_jump:
|
|
5141 {
|
|
5142 /* We need to pass separate storage for the lowest and
|
|
5143 highest registers, even though we don't care about the
|
|
5144 actual values. Otherwise, we will restore only one
|
|
5145 register from the stack, since lowest will == highest in
|
|
5146 `pop_failure_point'. */
|
|
5147 unsigned dummy_low_reg, dummy_high_reg;
|
|
5148 unsigned char *pdummy;
|
|
5149 CONST char *sdummy;
|
|
5150
|
|
5151 DEBUG_PRINT1 ("EXECUTING pop_failure_jump.\n");
|
|
5152 POP_FAILURE_POINT (sdummy, pdummy,
|
|
5153 dummy_low_reg, dummy_high_reg,
|
|
5154 reg_dummy, reg_dummy, reg_info_dummy);
|
|
5155 }
|
|
5156 /* Note fall through. */
|
|
5157
|
|
5158
|
|
5159 /* Unconditionally jump (without popping any failure points). */
|
|
5160 case jump:
|
|
5161 unconditional_jump:
|
|
5162 EXTRACT_NUMBER_AND_INCR (mcnt, p); /* Get the amount to jump. */
|
|
5163 DEBUG_PRINT2 ("EXECUTING jump %d ", mcnt);
|
|
5164 p += mcnt; /* Do the jump. */
|
|
5165 DEBUG_PRINT2 ("(to 0x%lx).\n", (unsigned long) p);
|
|
5166 break;
|
|
5167
|
|
5168
|
|
5169 /* We need this opcode so we can detect where alternatives end
|
|
5170 in `group_match_null_string_p' et al. */
|
|
5171 case jump_past_alt:
|
|
5172 DEBUG_PRINT1 ("EXECUTING jump_past_alt.\n");
|
|
5173 goto unconditional_jump;
|
|
5174
|
|
5175
|
|
5176 /* Normally, the on_failure_jump pushes a failure point, which
|
|
5177 then gets popped at pop_failure_jump. We will end up at
|
|
5178 pop_failure_jump, also, and with a pattern of, say, `a+', we
|
|
5179 are skipping over the on_failure_jump, so we have to push
|
|
5180 something meaningless for pop_failure_jump to pop. */
|
|
5181 case dummy_failure_jump:
|
|
5182 DEBUG_PRINT1 ("EXECUTING dummy_failure_jump.\n");
|
|
5183 /* It doesn't matter what we push for the string here. What
|
|
5184 the code at `fail' tests is the value for the pattern. */
|
|
5185 PUSH_FAILURE_POINT (0, 0, -2);
|
|
5186 goto unconditional_jump;
|
|
5187
|
|
5188
|
|
5189 /* At the end of an alternative, we need to push a dummy failure
|
|
5190 point in case we are followed by a `pop_failure_jump', because
|
|
5191 we don't want the failure point for the alternative to be
|
|
5192 popped. For example, matching `(a|ab)*' against `aab'
|
|
5193 requires that we match the `ab' alternative. */
|
|
5194 case push_dummy_failure:
|
|
5195 DEBUG_PRINT1 ("EXECUTING push_dummy_failure.\n");
|
|
5196 /* See comments just above at `dummy_failure_jump' about the
|
|
5197 two zeroes. */
|
|
5198 PUSH_FAILURE_POINT (0, 0, -2);
|
|
5199 break;
|
|
5200
|
|
5201 /* Have to succeed matching what follows at least n times.
|
|
5202 After that, handle like `on_failure_jump'. */
|
|
5203 case succeed_n:
|
|
5204 EXTRACT_NUMBER (mcnt, p + 2);
|
|
5205 DEBUG_PRINT2 ("EXECUTING succeed_n %d.\n", mcnt);
|
|
5206
|
|
5207 assert (mcnt >= 0);
|
|
5208 /* Originally, this is how many times we HAVE to succeed. */
|
|
5209 if (mcnt > 0)
|
|
5210 {
|
|
5211 mcnt--;
|
|
5212 p += 2;
|
|
5213 STORE_NUMBER_AND_INCR (p, mcnt);
|
|
5214 DEBUG_PRINT3 (" Setting 0x%lx to %d.\n", (unsigned long) p,
|
|
5215 mcnt);
|
|
5216 }
|
|
5217 else if (mcnt == 0)
|
|
5218 {
|
|
5219 DEBUG_PRINT2 (" Setting two bytes from 0x%lx to no_op.\n",
|
|
5220 (unsigned long) p+2);
|
|
5221 p[2] = (unsigned char) no_op;
|
|
5222 p[3] = (unsigned char) no_op;
|
|
5223 goto on_failure;
|
|
5224 }
|
|
5225 break;
|
|
5226
|
|
5227 case jump_n:
|
|
5228 EXTRACT_NUMBER (mcnt, p + 2);
|
|
5229 DEBUG_PRINT2 ("EXECUTING jump_n %d.\n", mcnt);
|
|
5230
|
|
5231 /* Originally, this is how many times we CAN jump. */
|
|
5232 if (mcnt)
|
|
5233 {
|
|
5234 mcnt--;
|
|
5235 STORE_NUMBER (p + 2, mcnt);
|
|
5236 goto unconditional_jump;
|
|
5237 }
|
|
5238 /* If don't have to jump any more, skip over the rest of command. */
|
|
5239 else
|
|
5240 p += 4;
|
|
5241 break;
|
|
5242
|
|
5243 case set_number_at:
|
|
5244 {
|
|
5245 DEBUG_PRINT1 ("EXECUTING set_number_at.\n");
|
|
5246
|
|
5247 EXTRACT_NUMBER_AND_INCR (mcnt, p);
|
|
5248 p1 = p + mcnt;
|
|
5249 EXTRACT_NUMBER_AND_INCR (mcnt, p);
|
|
5250 DEBUG_PRINT3 (" Setting 0x%lx to %d.\n", (unsigned long) p1,
|
|
5251 mcnt);
|
|
5252 STORE_NUMBER (p1, mcnt);
|
|
5253 break;
|
|
5254 }
|
|
5255
|
|
5256 case wordbound:
|
|
5257 DEBUG_PRINT1 ("EXECUTING wordbound.\n");
|
|
5258 should_succeed = 1;
|
|
5259 matchwordbound:
|
|
5260 {
|
|
5261 /* XEmacs change */
|
|
5262 int result;
|
|
5263 if (AT_STRINGS_BEG (d) || AT_STRINGS_END (d))
|
|
5264 result = 1;
|
|
5265 else
|
|
5266 {
|
|
5267 CONST unsigned char *d_before =
|
|
5268 (CONST unsigned char *) POS_BEFORE_GAP_UNSAFE (d);
|
|
5269 CONST unsigned char *d_after =
|
|
5270 (CONST unsigned char *) POS_AFTER_GAP_UNSAFE (d);
|
|
5271 Emchar emch1, emch2;
|
|
5272
|
|
5273 DEC_CHARPTR (d_before);
|
|
5274 emch1 = charptr_emchar (d_before);
|
|
5275 emch2 = charptr_emchar (d_after);
|
|
5276 result = (WORDCHAR_P_UNSAFE (emch1) !=
|
|
5277 WORDCHAR_P_UNSAFE (emch2));
|
|
5278 }
|
|
5279 if (result == should_succeed)
|
|
5280 break;
|
|
5281 goto fail;
|
|
5282 }
|
|
5283
|
|
5284 case notwordbound:
|
|
5285 DEBUG_PRINT1 ("EXECUTING notwordbound.\n");
|
|
5286 should_succeed = 0;
|
|
5287 goto matchwordbound;
|
|
5288
|
|
5289 case wordbeg:
|
|
5290 DEBUG_PRINT1 ("EXECUTING wordbeg.\n");
|
|
5291 {
|
|
5292 /* XEmacs: this originally read:
|
|
5293
|
|
5294 if (WORDCHAR_P (d) && (AT_STRINGS_BEG (d) || !WORDCHAR_P (d - 1)))
|
|
5295 break;
|
|
5296
|
|
5297 */
|
|
5298 CONST unsigned char *dtmp =
|
|
5299 (CONST unsigned char *) POS_AFTER_GAP_UNSAFE (d);
|
|
5300 Emchar emch = charptr_emchar (dtmp);
|
|
5301 if (!WORDCHAR_P_UNSAFE (emch))
|
|
5302 goto fail;
|
|
5303 if (AT_STRINGS_BEG (d))
|
|
5304 break;
|
|
5305 dtmp = (CONST unsigned char *) POS_BEFORE_GAP_UNSAFE (d);
|
|
5306 DEC_CHARPTR (dtmp);
|
|
5307 emch = charptr_emchar (dtmp);
|
|
5308 if (!WORDCHAR_P_UNSAFE (emch))
|
|
5309 break;
|
|
5310 goto fail;
|
|
5311 }
|
|
5312
|
|
5313 case wordend:
|
|
5314 DEBUG_PRINT1 ("EXECUTING wordend.\n");
|
|
5315 {
|
|
5316 /* XEmacs: this originally read:
|
|
5317
|
|
5318 if (!AT_STRINGS_BEG (d) && WORDCHAR_P (d - 1)
|
|
5319 && (!WORDCHAR_P (d) || AT_STRINGS_END (d)))
|
|
5320 break;
|
|
5321
|
|
5322 The or condition is incorrect (reversed).
|
|
5323 */
|
|
5324 CONST unsigned char *dtmp;
|
|
5325 Emchar emch;
|
|
5326 if (AT_STRINGS_BEG (d))
|
|
5327 goto fail;
|
|
5328 dtmp = (CONST unsigned char *) POS_BEFORE_GAP_UNSAFE (d);
|
|
5329 DEC_CHARPTR (dtmp);
|
|
5330 emch = charptr_emchar (dtmp);
|
|
5331 if (!WORDCHAR_P_UNSAFE (emch))
|
|
5332 goto fail;
|
|
5333 if (AT_STRINGS_END (d))
|
|
5334 break;
|
|
5335 dtmp = (CONST unsigned char *) POS_AFTER_GAP_UNSAFE (d);
|
|
5336 emch = charptr_emchar (dtmp);
|
|
5337 if (!WORDCHAR_P_UNSAFE (emch))
|
|
5338 break;
|
|
5339 goto fail;
|
|
5340 }
|
|
5341
|
|
5342 #ifdef emacs
|
|
5343 case before_dot:
|
|
5344 DEBUG_PRINT1 ("EXECUTING before_dot.\n");
|
|
5345 if (BUF_PTR_BYTE_POS (regex_emacs_buffer, (unsigned char *) d) >=
|
|
5346 BUF_PT (regex_emacs_buffer))
|
|
5347 goto fail;
|
|
5348 break;
|
|
5349
|
|
5350 case at_dot:
|
|
5351 DEBUG_PRINT1 ("EXECUTING at_dot.\n");
|
|
5352 if (BUF_PTR_BYTE_POS (regex_emacs_buffer, (unsigned char *) d)
|
|
5353 != BUF_PT (regex_emacs_buffer))
|
|
5354 goto fail;
|
|
5355 break;
|
|
5356
|
|
5357 case after_dot:
|
|
5358 DEBUG_PRINT1 ("EXECUTING after_dot.\n");
|
|
5359 if (BUF_PTR_BYTE_POS (regex_emacs_buffer, (unsigned char *) d)
|
|
5360 <= BUF_PT (regex_emacs_buffer))
|
|
5361 goto fail;
|
|
5362 break;
|
|
5363 #if 0 /* not emacs19 */
|
|
5364 case at_dot:
|
|
5365 DEBUG_PRINT1 ("EXECUTING at_dot.\n");
|
|
5366 if (BUF_PTR_BYTE_POS (regex_emacs_buffer, (unsigned char *) d) + 1
|
|
5367 != BUF_PT (regex_emacs_buffer))
|
|
5368 goto fail;
|
|
5369 break;
|
|
5370 #endif /* not emacs19 */
|
|
5371
|
|
5372 case syntaxspec:
|
|
5373 DEBUG_PRINT2 ("EXECUTING syntaxspec %d.\n", mcnt);
|
|
5374 mcnt = *p++;
|
|
5375 goto matchsyntax;
|
|
5376
|
|
5377 case wordchar:
|
|
5378 DEBUG_PRINT1 ("EXECUTING Emacs wordchar.\n");
|
|
5379 mcnt = (int) Sword;
|
|
5380 matchsyntax:
|
|
5381 should_succeed = 1;
|
|
5382 matchornotsyntax:
|
|
5383 {
|
|
5384 int matches;
|
|
5385 Emchar emch;
|
|
5386
|
|
5387 PREFETCH ();
|
|
5388 emch = charptr_emchar ((CONST Bufbyte *) d);
|
70
|
5389 matches = (SYNTAX_UNSAFE
|
|
5390 (XCHAR_TABLE (regex_emacs_buffer->mirror_syntax_table),
|
|
5391 emch) == (enum syntaxcode) mcnt);
|
0
|
5392 INC_CHARPTR (d);
|
|
5393 if (matches != should_succeed)
|
|
5394 goto fail;
|
|
5395 SET_REGS_MATCHED ();
|
|
5396 }
|
|
5397 break;
|
|
5398
|
|
5399 case notsyntaxspec:
|
|
5400 DEBUG_PRINT2 ("EXECUTING notsyntaxspec %d.\n", mcnt);
|
|
5401 mcnt = *p++;
|
|
5402 goto matchnotsyntax;
|
|
5403
|
|
5404 case notwordchar:
|
|
5405 DEBUG_PRINT1 ("EXECUTING Emacs notwordchar.\n");
|
|
5406 mcnt = (int) Sword;
|
|
5407 matchnotsyntax:
|
|
5408 should_succeed = 0;
|
|
5409 goto matchornotsyntax;
|
|
5410
|
|
5411 #else /* not emacs */
|
|
5412 case wordchar:
|
|
5413 DEBUG_PRINT1 ("EXECUTING non-Emacs wordchar.\n");
|
|
5414 PREFETCH ();
|
|
5415 if (!WORDCHAR_P_UNSAFE ((int) (*d)))
|
|
5416 goto fail;
|
|
5417 SET_REGS_MATCHED ();
|
|
5418 d++;
|
|
5419 break;
|
|
5420
|
|
5421 case notwordchar:
|
|
5422 DEBUG_PRINT1 ("EXECUTING non-Emacs notwordchar.\n");
|
|
5423 PREFETCH ();
|
|
5424 if (!WORDCHAR_P_UNSAFE ((int) (*d)))
|
|
5425 goto fail;
|
|
5426 SET_REGS_MATCHED ();
|
|
5427 d++;
|
|
5428 break;
|
|
5429 #endif /* not emacs */
|
|
5430
|
|
5431 default:
|
|
5432 abort ();
|
|
5433 }
|
|
5434 continue; /* Successfully executed one pattern command; keep going. */
|
|
5435
|
|
5436
|
|
5437 /* We goto here if a matching operation fails. */
|
|
5438 fail:
|
|
5439 if (!FAIL_STACK_EMPTY ())
|
|
5440 { /* A restart point is known. Restore to that state. */
|
|
5441 DEBUG_PRINT1 ("\nFAIL:\n");
|
|
5442 POP_FAILURE_POINT (d, p,
|
|
5443 lowest_active_reg, highest_active_reg,
|
|
5444 regstart, regend, reg_info);
|
|
5445
|
|
5446 /* If this failure point is a dummy, try the next one. */
|
|
5447 if (!p)
|
|
5448 goto fail;
|
|
5449
|
|
5450 /* If we failed to the end of the pattern, don't examine *p. */
|
|
5451 assert (p <= pend);
|
|
5452 if (p < pend)
|
|
5453 {
|
|
5454 boolean is_a_jump_n = false;
|
|
5455
|
|
5456 /* If failed to a backwards jump that's part of a repetition
|
|
5457 loop, need to pop this failure point and use the next one. */
|
|
5458 switch ((re_opcode_t) *p)
|
|
5459 {
|
|
5460 case jump_n:
|
|
5461 is_a_jump_n = true;
|
|
5462 case maybe_pop_jump:
|
|
5463 case pop_failure_jump:
|
|
5464 case jump:
|
|
5465 p1 = p + 1;
|
|
5466 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
|
|
5467 p1 += mcnt;
|
|
5468
|
|
5469 if ((is_a_jump_n && (re_opcode_t) *p1 == succeed_n)
|
|
5470 || (!is_a_jump_n
|
|
5471 && (re_opcode_t) *p1 == on_failure_jump))
|
|
5472 goto fail;
|
|
5473 break;
|
|
5474 default:
|
|
5475 /* do nothing */ ;
|
|
5476 }
|
|
5477 }
|
|
5478
|
|
5479 if (d >= string1 && d <= end1)
|
|
5480 dend = end_match_1;
|
|
5481 }
|
|
5482 else
|
|
5483 break; /* Matching at this starting point really fails. */
|
|
5484 } /* for (;;) */
|
|
5485
|
|
5486 if (best_regs_set)
|
|
5487 goto restore_best_regs;
|
|
5488
|
|
5489 FREE_VARIABLES ();
|
|
5490
|
|
5491 return -1; /* Failure to match. */
|
|
5492 } /* re_match_2 */
|
|
5493
|
|
5494 /* Subroutine definitions for re_match_2. */
|
|
5495
|
|
5496
|
|
5497 /* We are passed P pointing to a register number after a start_memory.
|
|
5498
|
|
5499 Return true if the pattern up to the corresponding stop_memory can
|
|
5500 match the empty string, and false otherwise.
|
|
5501
|
|
5502 If we find the matching stop_memory, sets P to point to one past its number.
|
|
5503 Otherwise, sets P to an undefined byte less than or equal to END.
|
|
5504
|
|
5505 We don't handle duplicates properly (yet). */
|
|
5506
|
|
5507 static boolean
|
|
5508 group_match_null_string_p (unsigned char **p, unsigned char *end,
|
|
5509 register_info_type *reg_info)
|
|
5510 {
|
|
5511 int mcnt;
|
|
5512 /* Point to after the args to the start_memory. */
|
|
5513 unsigned char *p1 = *p + 2;
|
|
5514
|
|
5515 while (p1 < end)
|
|
5516 {
|
|
5517 /* Skip over opcodes that can match nothing, and return true or
|
|
5518 false, as appropriate, when we get to one that can't, or to the
|
|
5519 matching stop_memory. */
|
|
5520
|
|
5521 switch ((re_opcode_t) *p1)
|
|
5522 {
|
|
5523 /* Could be either a loop or a series of alternatives. */
|
|
5524 case on_failure_jump:
|
|
5525 p1++;
|
|
5526 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
|
|
5527
|
|
5528 /* If the next operation is not a jump backwards in the
|
|
5529 pattern. */
|
|
5530
|
|
5531 if (mcnt >= 0)
|
|
5532 {
|
|
5533 /* Go through the on_failure_jumps of the alternatives,
|
|
5534 seeing if any of the alternatives cannot match nothing.
|
|
5535 The last alternative starts with only a jump,
|
|
5536 whereas the rest start with on_failure_jump and end
|
|
5537 with a jump, e.g., here is the pattern for `a|b|c':
|
|
5538
|
|
5539 /on_failure_jump/0/6/exactn/1/a/jump_past_alt/0/6
|
|
5540 /on_failure_jump/0/6/exactn/1/b/jump_past_alt/0/3
|
|
5541 /exactn/1/c
|
|
5542
|
|
5543 So, we have to first go through the first (n-1)
|
|
5544 alternatives and then deal with the last one separately. */
|
|
5545
|
|
5546
|
|
5547 /* Deal with the first (n-1) alternatives, which start
|
|
5548 with an on_failure_jump (see above) that jumps to right
|
|
5549 past a jump_past_alt. */
|
|
5550
|
|
5551 while ((re_opcode_t) p1[mcnt-3] == jump_past_alt)
|
|
5552 {
|
|
5553 /* `mcnt' holds how many bytes long the alternative
|
|
5554 is, including the ending `jump_past_alt' and
|
|
5555 its number. */
|
|
5556
|
|
5557 if (!alt_match_null_string_p (p1, p1 + mcnt - 3,
|
|
5558 reg_info))
|
|
5559 return false;
|
|
5560
|
|
5561 /* Move to right after this alternative, including the
|
|
5562 jump_past_alt. */
|
|
5563 p1 += mcnt;
|
|
5564
|
|
5565 /* Break if it's the beginning of an n-th alternative
|
|
5566 that doesn't begin with an on_failure_jump. */
|
|
5567 if ((re_opcode_t) *p1 != on_failure_jump)
|
|
5568 break;
|
|
5569
|
|
5570 /* Still have to check that it's not an n-th
|
|
5571 alternative that starts with an on_failure_jump. */
|
|
5572 p1++;
|
|
5573 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
|
|
5574 if ((re_opcode_t) p1[mcnt-3] != jump_past_alt)
|
|
5575 {
|
|
5576 /* Get to the beginning of the n-th alternative. */
|
|
5577 p1 -= 3;
|
|
5578 break;
|
|
5579 }
|
|
5580 }
|
|
5581
|
|
5582 /* Deal with the last alternative: go back and get number
|
|
5583 of the `jump_past_alt' just before it. `mcnt' contains
|
|
5584 the length of the alternative. */
|
|
5585 EXTRACT_NUMBER (mcnt, p1 - 2);
|
|
5586
|
|
5587 if (!alt_match_null_string_p (p1, p1 + mcnt, reg_info))
|
|
5588 return false;
|
|
5589
|
|
5590 p1 += mcnt; /* Get past the n-th alternative. */
|
|
5591 } /* if mcnt > 0 */
|
|
5592 break;
|
|
5593
|
|
5594
|
|
5595 case stop_memory:
|
|
5596 assert (p1[1] == **p);
|
|
5597 *p = p1 + 2;
|
|
5598 return true;
|
|
5599
|
|
5600
|
|
5601 default:
|
|
5602 if (!common_op_match_null_string_p (&p1, end, reg_info))
|
|
5603 return false;
|
|
5604 }
|
|
5605 } /* while p1 < end */
|
|
5606
|
|
5607 return false;
|
|
5608 } /* group_match_null_string_p */
|
|
5609
|
|
5610
|
|
5611 /* Similar to group_match_null_string_p, but doesn't deal with alternatives:
|
|
5612 It expects P to be the first byte of a single alternative and END one
|
|
5613 byte past the last. The alternative can contain groups. */
|
|
5614
|
|
5615 static boolean
|
|
5616 alt_match_null_string_p (unsigned char *p, unsigned char *end,
|
|
5617 register_info_type *reg_info)
|
|
5618 {
|
|
5619 int mcnt;
|
|
5620 unsigned char *p1 = p;
|
|
5621
|
|
5622 while (p1 < end)
|
|
5623 {
|
|
5624 /* Skip over opcodes that can match nothing, and break when we get
|
|
5625 to one that can't. */
|
|
5626
|
|
5627 switch ((re_opcode_t) *p1)
|
|
5628 {
|
|
5629 /* It's a loop. */
|
|
5630 case on_failure_jump:
|
|
5631 p1++;
|
|
5632 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
|
|
5633 p1 += mcnt;
|
|
5634 break;
|
|
5635
|
|
5636 default:
|
|
5637 if (!common_op_match_null_string_p (&p1, end, reg_info))
|
|
5638 return false;
|
|
5639 }
|
|
5640 } /* while p1 < end */
|
|
5641
|
|
5642 return true;
|
|
5643 } /* alt_match_null_string_p */
|
|
5644
|
|
5645
|
|
5646 /* Deals with the ops common to group_match_null_string_p and
|
|
5647 alt_match_null_string_p.
|
|
5648
|
|
5649 Sets P to one after the op and its arguments, if any. */
|
|
5650
|
|
5651 static boolean
|
|
5652 common_op_match_null_string_p (unsigned char **p, unsigned char *end,
|
|
5653 register_info_type *reg_info)
|
|
5654 {
|
|
5655 int mcnt;
|
|
5656 boolean ret;
|
|
5657 int reg_no;
|
|
5658 unsigned char *p1 = *p;
|
|
5659
|
|
5660 switch ((re_opcode_t) *p1++)
|
|
5661 {
|
|
5662 case no_op:
|
|
5663 case begline:
|
|
5664 case endline:
|
|
5665 case begbuf:
|
|
5666 case endbuf:
|
|
5667 case wordbeg:
|
|
5668 case wordend:
|
|
5669 case wordbound:
|
|
5670 case notwordbound:
|
|
5671 #ifdef emacs
|
|
5672 case before_dot:
|
|
5673 case at_dot:
|
|
5674 case after_dot:
|
|
5675 #endif
|
|
5676 break;
|
|
5677
|
|
5678 case start_memory:
|
|
5679 reg_no = *p1;
|
|
5680 assert (reg_no > 0 && reg_no <= MAX_REGNUM);
|
|
5681 ret = group_match_null_string_p (&p1, end, reg_info);
|
|
5682
|
|
5683 /* Have to set this here in case we're checking a group which
|
|
5684 contains a group and a back reference to it. */
|
|
5685
|
|
5686 if (REG_MATCH_NULL_STRING_P (reg_info[reg_no]) == MATCH_NULL_UNSET_VALUE)
|
|
5687 REG_MATCH_NULL_STRING_P (reg_info[reg_no]) = ret;
|
|
5688
|
|
5689 if (!ret)
|
|
5690 return false;
|
|
5691 break;
|
|
5692
|
|
5693 /* If this is an optimized succeed_n for zero times, make the jump. */
|
|
5694 case jump:
|
|
5695 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
|
|
5696 if (mcnt >= 0)
|
|
5697 p1 += mcnt;
|
|
5698 else
|
|
5699 return false;
|
|
5700 break;
|
|
5701
|
|
5702 case succeed_n:
|
|
5703 /* Get to the number of times to succeed. */
|
|
5704 p1 += 2;
|
|
5705 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
|
|
5706
|
|
5707 if (mcnt == 0)
|
|
5708 {
|
|
5709 p1 -= 4;
|
|
5710 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
|
|
5711 p1 += mcnt;
|
|
5712 }
|
|
5713 else
|
|
5714 return false;
|
|
5715 break;
|
|
5716
|
|
5717 case duplicate:
|
|
5718 if (!REG_MATCH_NULL_STRING_P (reg_info[*p1]))
|
|
5719 return false;
|
|
5720 break;
|
|
5721
|
|
5722 case set_number_at:
|
|
5723 p1 += 4;
|
|
5724
|
|
5725 default:
|
|
5726 /* All other opcodes mean we cannot match the empty string. */
|
|
5727 return false;
|
|
5728 }
|
|
5729
|
|
5730 *p = p1;
|
|
5731 return true;
|
|
5732 } /* common_op_match_null_string_p */
|
|
5733
|
|
5734
|
|
5735 /* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
|
|
5736 bytes; nonzero otherwise. */
|
|
5737
|
|
5738 static int
|
|
5739 bcmp_translate (CONST unsigned char *s1, CONST unsigned char *s2,
|
|
5740 register int len, char *translate)
|
|
5741 {
|
|
5742 register CONST unsigned char *p1 = s1, *p2 = s2;
|
|
5743 while (len)
|
|
5744 {
|
|
5745 if (translate[*p1++] != translate[*p2++]) return 1;
|
|
5746 len--;
|
|
5747 }
|
|
5748 return 0;
|
|
5749 }
|
|
5750
|
|
5751 /* Entry points for GNU code. */
|
|
5752
|
|
5753 /* re_compile_pattern is the GNU regular expression compiler: it
|
|
5754 compiles PATTERN (of length SIZE) and puts the result in BUFP.
|
|
5755 Returns 0 if the pattern was valid, otherwise an error string.
|
|
5756
|
|
5757 Assumes the `allocated' (and perhaps `buffer') and `translate' fields
|
|
5758 are set in BUFP on entry.
|
|
5759
|
|
5760 We call regex_compile to do the actual compilation. */
|
|
5761
|
|
5762 CONST char *
|
|
5763 re_compile_pattern (CONST char *pattern, int length,
|
|
5764 struct re_pattern_buffer *bufp)
|
|
5765 {
|
|
5766 reg_errcode_t ret;
|
|
5767
|
|
5768 /* GNU code is written to assume at least RE_NREGS registers will be set
|
|
5769 (and at least one extra will be -1). */
|
|
5770 bufp->regs_allocated = REGS_UNALLOCATED;
|
|
5771
|
|
5772 /* And GNU code determines whether or not to get register information
|
|
5773 by passing null for the REGS argument to re_match, etc., not by
|
|
5774 setting no_sub. */
|
|
5775 bufp->no_sub = 0;
|
|
5776
|
|
5777 /* Match anchors at newline. */
|
|
5778 bufp->newline_anchor = 1;
|
|
5779
|
|
5780 ret = regex_compile (pattern, length, re_syntax_options, bufp);
|
|
5781
|
|
5782 if (!ret)
|
|
5783 return NULL;
|
|
5784 return gettext (re_error_msgid[(int) ret]);
|
|
5785 }
|
|
5786
|
|
5787 /* Entry points compatible with 4.2 BSD regex library. We don't define
|
|
5788 them unless specifically requested. */
|
|
5789
|
|
5790 #ifdef _REGEX_RE_COMP
|
|
5791
|
|
5792 /* BSD has one and only one pattern buffer. */
|
|
5793 static struct re_pattern_buffer re_comp_buf;
|
|
5794
|
|
5795 char *
|
|
5796 re_comp (CONST char *s)
|
|
5797 {
|
|
5798 reg_errcode_t ret;
|
|
5799
|
|
5800 if (!s)
|
|
5801 {
|
|
5802 if (!re_comp_buf.buffer)
|
|
5803 return gettext ("No previous regular expression");
|
|
5804 return 0;
|
|
5805 }
|
|
5806
|
|
5807 if (!re_comp_buf.buffer)
|
|
5808 {
|
|
5809 re_comp_buf.buffer = (unsigned char *) malloc (200);
|
|
5810 if (re_comp_buf.buffer == NULL)
|
|
5811 return gettext (re_error_msgid[(int) REG_ESPACE]);
|
|
5812 re_comp_buf.allocated = 200;
|
|
5813
|
|
5814 re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH);
|
|
5815 if (re_comp_buf.fastmap == NULL)
|
|
5816 return gettext (re_error_msgid[(int) REG_ESPACE]);
|
|
5817 }
|
|
5818
|
|
5819 /* Since `re_exec' always passes NULL for the `regs' argument, we
|
|
5820 don't need to initialize the pattern buffer fields which affect it. */
|
|
5821
|
|
5822 /* Match anchors at newlines. */
|
|
5823 re_comp_buf.newline_anchor = 1;
|
|
5824
|
|
5825 ret = regex_compile (s, strlen (s), re_syntax_options, &re_comp_buf);
|
|
5826
|
|
5827 if (!ret)
|
|
5828 return NULL;
|
|
5829
|
|
5830 /* Yes, we're discarding `CONST' here if !HAVE_LIBINTL. */
|
|
5831 return (char *) gettext (re_error_msgid[(int) ret]);
|
|
5832 }
|
|
5833
|
|
5834
|
|
5835 int
|
|
5836 re_exec (CONST char *s)
|
|
5837 {
|
|
5838 CONST int len = strlen (s);
|
|
5839 return
|
|
5840 0 <= re_search (&re_comp_buf, s, len, 0, len, (struct re_registers *) 0);
|
|
5841 }
|
|
5842 #endif /* _REGEX_RE_COMP */
|
|
5843
|
|
5844 /* POSIX.2 functions. Don't define these for Emacs. */
|
|
5845
|
|
5846 #ifndef emacs
|
|
5847
|
|
5848 /* regcomp takes a regular expression as a string and compiles it.
|
|
5849
|
|
5850 PREG is a regex_t *. We do not expect any fields to be initialized,
|
|
5851 since POSIX says we shouldn't. Thus, we set
|
|
5852
|
|
5853 `buffer' to the compiled pattern;
|
|
5854 `used' to the length of the compiled pattern;
|
|
5855 `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
|
|
5856 REG_EXTENDED bit in CFLAGS is set; otherwise, to
|
|
5857 RE_SYNTAX_POSIX_BASIC;
|
|
5858 `newline_anchor' to REG_NEWLINE being set in CFLAGS;
|
|
5859 `fastmap' and `fastmap_accurate' to zero;
|
|
5860 `re_nsub' to the number of subexpressions in PATTERN.
|
|
5861
|
|
5862 PATTERN is the address of the pattern string.
|
|
5863
|
|
5864 CFLAGS is a series of bits which affect compilation.
|
|
5865
|
|
5866 If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
|
|
5867 use POSIX basic syntax.
|
|
5868
|
|
5869 If REG_NEWLINE is set, then . and [^...] don't match newline.
|
|
5870 Also, regexec will try a match beginning after every newline.
|
|
5871
|
|
5872 If REG_ICASE is set, then we considers upper- and lowercase
|
|
5873 versions of letters to be equivalent when matching.
|
|
5874
|
|
5875 If REG_NOSUB is set, then when PREG is passed to regexec, that
|
|
5876 routine will report only success or failure, and nothing about the
|
|
5877 registers.
|
|
5878
|
|
5879 It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
|
|
5880 the return codes and their meanings.) */
|
|
5881
|
|
5882 int
|
|
5883 regcomp (regex_t *preg, CONST char *pattern, int cflags)
|
|
5884 {
|
|
5885 reg_errcode_t ret;
|
|
5886 unsigned syntax
|
|
5887 = (cflags & REG_EXTENDED) ?
|
|
5888 RE_SYNTAX_POSIX_EXTENDED : RE_SYNTAX_POSIX_BASIC;
|
|
5889
|
|
5890 /* regex_compile will allocate the space for the compiled pattern. */
|
|
5891 preg->buffer = 0;
|
|
5892 preg->allocated = 0;
|
|
5893 preg->used = 0;
|
|
5894
|
|
5895 /* Don't bother to use a fastmap when searching. This simplifies the
|
|
5896 REG_NEWLINE case: if we used a fastmap, we'd have to put all the
|
|
5897 characters after newlines into the fastmap. This way, we just try
|
|
5898 every character. */
|
|
5899 preg->fastmap = 0;
|
|
5900
|
|
5901 if (cflags & REG_ICASE)
|
|
5902 {
|
|
5903 unsigned i;
|
|
5904
|
|
5905 preg->translate = (char *) malloc (CHAR_SET_SIZE);
|
|
5906 if (preg->translate == NULL)
|
|
5907 return (int) REG_ESPACE;
|
|
5908
|
|
5909 /* Map uppercase characters to corresponding lowercase ones. */
|
|
5910 for (i = 0; i < CHAR_SET_SIZE; i++)
|
|
5911 preg->translate[i] = ISUPPER (i) ? tolower (i) : i;
|
|
5912 }
|
|
5913 else
|
|
5914 preg->translate = NULL;
|
|
5915
|
|
5916 /* If REG_NEWLINE is set, newlines are treated differently. */
|
|
5917 if (cflags & REG_NEWLINE)
|
|
5918 { /* REG_NEWLINE implies neither . nor [^...] match newline. */
|
|
5919 syntax &= ~RE_DOT_NEWLINE;
|
|
5920 syntax |= RE_HAT_LISTS_NOT_NEWLINE;
|
|
5921 /* It also changes the matching behavior. */
|
|
5922 preg->newline_anchor = 1;
|
|
5923 }
|
|
5924 else
|
|
5925 preg->newline_anchor = 0;
|
|
5926
|
|
5927 preg->no_sub = !!(cflags & REG_NOSUB);
|
|
5928
|
|
5929 /* POSIX says a null character in the pattern terminates it, so we
|
|
5930 can use strlen here in compiling the pattern. */
|
|
5931 ret = regex_compile (pattern, strlen (pattern), syntax, preg);
|
|
5932
|
|
5933 /* POSIX doesn't distinguish between an unmatched open-group and an
|
|
5934 unmatched close-group: both are REG_EPAREN. */
|
|
5935 if (ret == REG_ERPAREN) ret = REG_EPAREN;
|
|
5936
|
|
5937 return (int) ret;
|
|
5938 }
|
|
5939
|
|
5940
|
|
5941 /* regexec searches for a given pattern, specified by PREG, in the
|
|
5942 string STRING.
|
|
5943
|
|
5944 If NMATCH is zero or REG_NOSUB was set in the cflags argument to
|
|
5945 `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
|
|
5946 least NMATCH elements, and we set them to the offsets of the
|
|
5947 corresponding matched substrings.
|
|
5948
|
|
5949 EFLAGS specifies `execution flags' which affect matching: if
|
|
5950 REG_NOTBOL is set, then ^ does not match at the beginning of the
|
|
5951 string; if REG_NOTEOL is set, then $ does not match at the end.
|
|
5952
|
|
5953 We return 0 if we find a match and REG_NOMATCH if not. */
|
|
5954
|
|
5955 int
|
|
5956 regexec (CONST regex_t *preg, CONST char *string, size_t nmatch,
|
|
5957 regmatch_t pmatch[], int eflags)
|
|
5958 {
|
|
5959 int ret;
|
|
5960 struct re_registers regs;
|
|
5961 regex_t private_preg;
|
|
5962 int len = strlen (string);
|
|
5963 boolean want_reg_info = !preg->no_sub && nmatch > 0;
|
|
5964
|
|
5965 private_preg = *preg;
|
|
5966
|
|
5967 private_preg.not_bol = !!(eflags & REG_NOTBOL);
|
|
5968 private_preg.not_eol = !!(eflags & REG_NOTEOL);
|
|
5969
|
|
5970 /* The user has told us exactly how many registers to return
|
|
5971 information about, via `nmatch'. We have to pass that on to the
|
|
5972 matching routines. */
|
|
5973 private_preg.regs_allocated = REGS_FIXED;
|
|
5974
|
|
5975 if (want_reg_info)
|
|
5976 {
|
|
5977 regs.num_regs = nmatch;
|
|
5978 regs.start = TALLOC (nmatch, regoff_t);
|
|
5979 regs.end = TALLOC (nmatch, regoff_t);
|
|
5980 if (regs.start == NULL || regs.end == NULL)
|
|
5981 return (int) REG_NOMATCH;
|
|
5982 }
|
|
5983
|
|
5984 /* Perform the searching operation. */
|
|
5985 ret = re_search (&private_preg, string, len,
|
|
5986 /* start: */ 0, /* range: */ len,
|
|
5987 want_reg_info ? ®s : (struct re_registers *) 0);
|
|
5988
|
|
5989 /* Copy the register information to the POSIX structure. */
|
|
5990 if (want_reg_info)
|
|
5991 {
|
|
5992 if (ret >= 0)
|
|
5993 {
|
|
5994 unsigned r;
|
|
5995
|
|
5996 for (r = 0; r < nmatch; r++)
|
|
5997 {
|
|
5998 pmatch[r].rm_so = regs.start[r];
|
|
5999 pmatch[r].rm_eo = regs.end[r];
|
|
6000 }
|
|
6001 }
|
|
6002
|
|
6003 /* If we needed the temporary register info, free the space now. */
|
|
6004 free (regs.start);
|
|
6005 free (regs.end);
|
|
6006 }
|
|
6007
|
|
6008 /* We want zero return to mean success, unlike `re_search'. */
|
|
6009 return ret >= 0 ? (int) REG_NOERROR : (int) REG_NOMATCH;
|
|
6010 }
|
|
6011
|
|
6012
|
|
6013 /* Returns a message corresponding to an error code, ERRCODE, returned
|
|
6014 from either regcomp or regexec. We don't use PREG here. */
|
|
6015
|
|
6016 size_t
|
|
6017 regerror (int errcode, CONST regex_t *preg, char *errbuf, size_t errbuf_size)
|
|
6018 {
|
|
6019 CONST char *msg;
|
|
6020 size_t msg_size;
|
|
6021
|
|
6022 if (errcode < 0
|
|
6023 || errcode >= (sizeof (re_error_msgid) / sizeof (re_error_msgid[0])))
|
|
6024 /* Only error codes returned by the rest of the code should be passed
|
|
6025 to this routine. If we are given anything else, or if other regex
|
|
6026 code generates an invalid error code, then the program has a bug.
|
|
6027 Dump core so we can fix it. */
|
|
6028 abort ();
|
|
6029
|
|
6030 msg = gettext (re_error_msgid[errcode]);
|
|
6031
|
|
6032 msg_size = strlen (msg) + 1; /* Includes the null. */
|
|
6033
|
|
6034 if (errbuf_size != 0)
|
|
6035 {
|
|
6036 if (msg_size > errbuf_size)
|
|
6037 {
|
|
6038 strncpy (errbuf, msg, errbuf_size - 1);
|
|
6039 errbuf[errbuf_size - 1] = 0;
|
|
6040 }
|
|
6041 else
|
|
6042 strcpy (errbuf, msg);
|
|
6043 }
|
|
6044
|
|
6045 return msg_size;
|
|
6046 }
|
|
6047
|
|
6048
|
|
6049 /* Free dynamically allocated space used by PREG. */
|
|
6050
|
|
6051 void
|
|
6052 regfree (regex_t *preg)
|
|
6053 {
|
|
6054 if (preg->buffer != NULL)
|
|
6055 free (preg->buffer);
|
|
6056 preg->buffer = NULL;
|
|
6057
|
|
6058 preg->allocated = 0;
|
|
6059 preg->used = 0;
|
|
6060
|
|
6061 if (preg->fastmap != NULL)
|
|
6062 free (preg->fastmap);
|
|
6063 preg->fastmap = NULL;
|
|
6064 preg->fastmap_accurate = 0;
|
|
6065
|
|
6066 if (preg->translate != NULL)
|
|
6067 free (preg->translate);
|
|
6068 preg->translate = NULL;
|
|
6069 }
|
|
6070
|
|
6071 #endif /* not emacs */
|
|
6072
|
|
6073 /*
|
|
6074 Local variables:
|
|
6075 make-backup-files: t
|
|
6076 version-control: t
|
|
6077 trim-versions-without-asking: nil
|
|
6078 End:
|
|
6079 */
|