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