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