1 /* Extended regular expression matching and search library, version
2 0.12. (Implements POSIX draft P1003.2/D11.2, except for some of the
3 internationalization features.)
5 Copyright (C) 1993,94,95,96,97,98,99,2000 Free Software Foundation, Inc.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
23 - structure the opcode space into opcode+flag.
24 - merge with glibc's regex.[ch].
25 - replace (succeed_n + jump_n + set_number_at) with something that doesn't
26 need to modify the compiled regexp so that re_match can be reentrant.
27 - get rid of on_failure_jump_smart by doing the optimization in re_comp
28 rather than at run-time, so that re_match can be reentrant.
31 /* AIX requires this to be the first thing in the file. */
32 #if defined _AIX && !defined REGEX_MALLOC
43 #if defined STDC_HEADERS && !defined emacs
46 /* We need this for `regex.h', and perhaps for the Emacs include files. */
47 # include <sys/types.h>
50 /* Whether to use ISO C Amendment 1 wide char functions.
51 Those should not be used for Emacs since it uses its own. */
53 #define WIDE_CHAR_SUPPORT 1
55 #define WIDE_CHAR_SUPPORT \
56 (HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_BTOWC && !emacs)
59 /* For platform which support the ISO C amendement 1 functionality we
60 support user defined character classes. */
62 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
68 /* We have to keep the namespace clean. */
69 # define regfree(preg) __regfree (preg)
70 # define regexec(pr, st, nm, pm, ef) __regexec (pr, st, nm, pm, ef)
71 # define regcomp(preg, pattern, cflags) __regcomp (preg, pattern, cflags)
72 # define regerror(errcode, preg, errbuf, errbuf_size) \
73 __regerror(errcode, preg, errbuf, errbuf_size)
74 # define re_set_registers(bu, re, nu, st, en) \
75 __re_set_registers (bu, re, nu, st, en)
76 # define re_match_2(bufp, string1, size1, string2, size2, pos, regs, stop) \
77 __re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
78 # define re_match(bufp, string, size, pos, regs) \
79 __re_match (bufp, string, size, pos, regs)
80 # define re_search(bufp, string, size, startpos, range, regs) \
81 __re_search (bufp, string, size, startpos, range, regs)
82 # define re_compile_pattern(pattern, length, bufp) \
83 __re_compile_pattern (pattern, length, bufp)
84 # define re_set_syntax(syntax) __re_set_syntax (syntax)
85 # define re_search_2(bufp, st1, s1, st2, s2, startpos, range, regs, stop) \
86 __re_search_2 (bufp, st1, s1, st2, s2, startpos, range, regs, stop)
87 # define re_compile_fastmap(bufp) __re_compile_fastmap (bufp)
89 /* Make sure we call libc's function even if the user overrides them. */
90 # define btowc __btowc
91 # define iswctype __iswctype
92 # define wctype __wctype
94 # define WEAK_ALIAS(a,b) weak_alias (a, b)
96 /* We are also using some library internals. */
97 # include <locale/localeinfo.h>
98 # include <locale/elem-hash.h>
99 # include <langinfo.h>
101 # define WEAK_ALIAS(a,b)
104 /* This is for other GNU distributions with internationalized messages. */
105 #if HAVE_LIBINTL_H || defined _LIBC
106 # include <libintl.h>
108 # define gettext(msgid) (msgid)
112 /* This define is so xgettext can find the internationalizable
114 # define gettext_noop(String) String
117 /* The `emacs' switch turns on certain matching commands
118 that make sense only in Emacs. */
124 /* Make syntax table lookup grant data in gl_state. */
125 # define SYNTAX_ENTRY_VIA_PROPERTY
128 # include "character.h"
129 # include "category.h"
134 # define malloc xmalloc
138 # define realloc xrealloc
144 /* Converts the pointer to the char to BEG-based offset from the start. */
145 # define PTR_TO_OFFSET(d) POS_AS_IN_BUFFER (POINTER_TO_OFFSET (d))
146 # define POS_AS_IN_BUFFER(p) ((p) + (NILP (re_match_object) || BUFFERP (re_match_object)))
148 # define RE_MULTIBYTE_P(bufp) ((bufp)->multibyte)
149 # define RE_TARGET_MULTIBYTE_P(bufp) ((bufp)->target_multibyte)
150 # define RE_STRING_CHAR(p, s) \
151 (multibyte ? (STRING_CHAR (p, s)) : (*(p)))
152 # define RE_STRING_CHAR_AND_LENGTH(p, s, len) \
153 (multibyte ? (STRING_CHAR_AND_LENGTH (p, s, len)) : ((len) = 1, *(p)))
155 /* Set C a (possibly converted to multibyte) character before P. P
156 points into a string which is the virtual concatenation of STR1
157 (which ends at END1) or STR2 (which ends at END2). */
158 # define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2) \
162 re_char *dtemp = (p) == (str2) ? (end1) : (p); \
163 re_char *dlimit = ((p) > (str2) && (p) <= (end2)) ? (str2) : (str1); \
164 while (dtemp-- > dlimit && !CHAR_HEAD_P (*dtemp)); \
165 c = STRING_CHAR (dtemp, (p) - dtemp); \
169 (c = ((p) == (str2) ? (end1) : (p))[-1]); \
170 MAKE_CHAR_MULTIBYTE (c); \
174 /* Set C a (possibly converted to multibyte) character at P, and set
175 LEN to the byte length of that character. */
176 # define GET_CHAR_AFTER(c, p, len) \
179 c = STRING_CHAR_AND_LENGTH (p, 0, len); \
184 MAKE_CHAR_MULTIBYTE (c); \
189 #else /* not emacs */
191 /* If we are not linking with Emacs proper,
192 we can't use the relocating allocator
193 even if config.h says that we can. */
196 # if defined STDC_HEADERS || defined _LIBC
203 /* When used in Emacs's lib-src, we need to get bzero and bcopy somehow.
204 If nothing else has been done, use the method below. */
205 # ifdef INHIBIT_STRING_HEADER
206 # if !(defined HAVE_BZERO && defined HAVE_BCOPY)
207 # if !defined bzero && !defined bcopy
208 # undef INHIBIT_STRING_HEADER
213 /* This is the normal way of making sure we have memcpy, memcmp and bzero.
214 This is used in most programs--a few other programs avoid this
215 by defining INHIBIT_STRING_HEADER. */
216 # ifndef INHIBIT_STRING_HEADER
217 # if defined HAVE_STRING_H || defined STDC_HEADERS || defined _LIBC
221 # define bzero(s, n) (memset (s, '\0', n), (s))
223 # define bzero(s, n) __bzero (s, n)
227 # include <strings.h>
229 # define memcmp(s1, s2, n) bcmp (s1, s2, n)
232 # define memcpy(d, s, n) (bcopy (s, d, n), (d))
237 /* Define the syntax stuff for \<, \>, etc. */
239 /* Sword must be nonzero for the wordchar pattern commands in re_match_2. */
240 enum syntaxcode
{ Swhitespace
= 0, Sword
= 1 };
242 # ifdef SWITCH_ENUM_BUG
243 # define SWITCH_ENUM_CAST(x) ((int)(x))
245 # define SWITCH_ENUM_CAST(x) (x)
248 /* Dummy macros for non-Emacs environments. */
249 # define BASE_LEADING_CODE_P(c) (0)
250 # define CHAR_CHARSET(c) 0
251 # define CHARSET_LEADING_CODE_BASE(c) 0
252 # define MAX_MULTIBYTE_LENGTH 1
253 # define RE_MULTIBYTE_P(x) 0
254 # define RE_TARGET_MULTIBYTE_P(x) 0
255 # define WORD_BOUNDARY_P(c1, c2) (0)
256 # define CHAR_HEAD_P(p) (1)
257 # define SINGLE_BYTE_CHAR_P(c) (1)
258 # define SAME_CHARSET_P(c1, c2) (1)
259 # define MULTIBYTE_FORM_LENGTH(p, s) (1)
260 # define STRING_CHAR(p, s) (*(p))
261 # define RE_STRING_CHAR STRING_CHAR
262 # define CHAR_STRING(c, s) (*(s) = (c), 1)
263 # define STRING_CHAR_AND_LENGTH(p, s, actual_len) ((actual_len) = 1, *(p))
264 # define RE_STRING_CHAR_AND_LENGTH STRING_CHAR_AND_LENGTH
265 # define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2) \
266 (c = ((p) == (str2) ? *((end1) - 1) : *((p) - 1)))
267 # define GET_CHAR_AFTER(c, p, len) \
269 # define MAKE_CHAR(charset, c1, c2) (c1)
270 # define BYTE8_TO_CHAR(c) (c)
271 # define CHAR_BYTE8_P(c) (0)
272 # define MAKE_CHAR_MULTIBYTE(c) (c)
273 # define MAKE_CHAR_UNIBYTE(c) (c)
274 # define CHAR_LEADING_CODE(c) (c)
275 #endif /* not emacs */
278 # define RE_TRANSLATE(TBL, C) ((unsigned char)(TBL)[C])
279 # define RE_TRANSLATE_P(TBL) (TBL)
282 /* Get the interface, including the syntax bits. */
285 /* isalpha etc. are used for the character classes. */
290 /* 1 if C is an ASCII character. */
291 # define IS_REAL_ASCII(c) ((c) < 0200)
293 /* 1 if C is a unibyte character. */
294 # define ISUNIBYTE(c) (SINGLE_BYTE_CHAR_P ((c)))
296 /* The Emacs definitions should not be directly affected by locales. */
298 /* In Emacs, these are only used for single-byte characters. */
299 # define ISDIGIT(c) ((c) >= '0' && (c) <= '9')
300 # define ISCNTRL(c) ((c) < ' ')
301 # define ISXDIGIT(c) (((c) >= '0' && (c) <= '9') \
302 || ((c) >= 'a' && (c) <= 'f') \
303 || ((c) >= 'A' && (c) <= 'F'))
305 /* This is only used for single-byte characters. */
306 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
308 /* The rest must handle multibyte characters. */
310 # define ISGRAPH(c) (SINGLE_BYTE_CHAR_P (c) \
311 ? (c) > ' ' && !((c) >= 0177 && (c) <= 0237) \
314 # define ISPRINT(c) (SINGLE_BYTE_CHAR_P (c) \
315 ? (c) >= ' ' && !((c) >= 0177 && (c) <= 0237) \
318 # define ISALNUM(c) (IS_REAL_ASCII (c) \
319 ? (((c) >= 'a' && (c) <= 'z') \
320 || ((c) >= 'A' && (c) <= 'Z') \
321 || ((c) >= '0' && (c) <= '9')) \
322 : SYNTAX (c) == Sword)
324 # define ISALPHA(c) (IS_REAL_ASCII (c) \
325 ? (((c) >= 'a' && (c) <= 'z') \
326 || ((c) >= 'A' && (c) <= 'Z')) \
327 : SYNTAX (c) == Sword)
329 # define ISLOWER(c) (LOWERCASEP (c))
331 # define ISPUNCT(c) (IS_REAL_ASCII (c) \
332 ? ((c) > ' ' && (c) < 0177 \
333 && !(((c) >= 'a' && (c) <= 'z') \
334 || ((c) >= 'A' && (c) <= 'Z') \
335 || ((c) >= '0' && (c) <= '9'))) \
336 : SYNTAX (c) != Sword)
338 # define ISSPACE(c) (SYNTAX (c) == Swhitespace)
340 # define ISUPPER(c) (UPPERCASEP (c))
342 # define ISWORD(c) (SYNTAX (c) == Sword)
344 #else /* not emacs */
346 /* Jim Meyering writes:
348 "... Some ctype macros are valid only for character codes that
349 isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
350 using /bin/cc or gcc but without giving an ansi option). So, all
351 ctype uses should be through macros like ISPRINT... If
352 STDC_HEADERS is defined, then autoconf has verified that the ctype
353 macros don't need to be guarded with references to isascii. ...
354 Defining isascii to 1 should let any compiler worth its salt
355 eliminate the && through constant folding."
356 Solaris defines some of these symbols so we must undefine them first. */
359 # if defined STDC_HEADERS || (!defined isascii && !defined HAVE_ISASCII)
360 # define ISASCII(c) 1
362 # define ISASCII(c) isascii(c)
365 /* 1 if C is an ASCII character. */
366 # define IS_REAL_ASCII(c) ((c) < 0200)
368 /* This distinction is not meaningful, except in Emacs. */
369 # define ISUNIBYTE(c) 1
372 # define ISBLANK(c) (ISASCII (c) && isblank (c))
374 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
377 # define ISGRAPH(c) (ISASCII (c) && isgraph (c))
379 # define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
383 # define ISPRINT(c) (ISASCII (c) && isprint (c))
384 # define ISDIGIT(c) (ISASCII (c) && isdigit (c))
385 # define ISALNUM(c) (ISASCII (c) && isalnum (c))
386 # define ISALPHA(c) (ISASCII (c) && isalpha (c))
387 # define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
388 # define ISLOWER(c) (ISASCII (c) && islower (c))
389 # define ISPUNCT(c) (ISASCII (c) && ispunct (c))
390 # define ISSPACE(c) (ISASCII (c) && isspace (c))
391 # define ISUPPER(c) (ISASCII (c) && isupper (c))
392 # define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
394 # define ISWORD(c) ISALPHA(c)
397 # define TOLOWER(c) _tolower(c)
399 # define TOLOWER(c) tolower(c)
402 /* How many characters in the character set. */
403 # define CHAR_SET_SIZE 256
407 extern char *re_syntax_table
;
409 # else /* not SYNTAX_TABLE */
411 static char re_syntax_table
[CHAR_SET_SIZE
];
422 bzero (re_syntax_table
, sizeof re_syntax_table
);
424 for (c
= 0; c
< CHAR_SET_SIZE
; ++c
)
426 re_syntax_table
[c
] = Sword
;
428 re_syntax_table
['_'] = Sword
;
433 # endif /* not SYNTAX_TABLE */
435 # define SYNTAX(c) re_syntax_table[(c)]
437 #endif /* not emacs */
440 # define NULL (void *)0
443 /* We remove any previous definition of `SIGN_EXTEND_CHAR',
444 since ours (we hope) works properly with all combinations of
445 machines, compilers, `char' and `unsigned char' argument types.
446 (Per Bothner suggested the basic approach.) */
447 #undef SIGN_EXTEND_CHAR
449 # define SIGN_EXTEND_CHAR(c) ((signed char) (c))
450 #else /* not __STDC__ */
451 /* As in Harbison and Steele. */
452 # define SIGN_EXTEND_CHAR(c) ((((unsigned char) (c)) ^ 128) - 128)
455 /* Should we use malloc or alloca? If REGEX_MALLOC is not defined, we
456 use `alloca' instead of `malloc'. This is because using malloc in
457 re_search* or re_match* could cause memory leaks when C-g is used in
458 Emacs; also, malloc is slower and causes storage fragmentation. On
459 the other hand, malloc is more portable, and easier to debug.
461 Because we sometimes use alloca, some routines have to be macros,
462 not functions -- `alloca'-allocated space disappears at the end of the
463 function it is called in. */
467 # define REGEX_ALLOCATE malloc
468 # define REGEX_REALLOCATE(source, osize, nsize) realloc (source, nsize)
469 # define REGEX_FREE free
471 #else /* not REGEX_MALLOC */
473 /* Emacs already defines alloca, sometimes. */
476 /* Make alloca work the best possible way. */
478 # define alloca __builtin_alloca
479 # else /* not __GNUC__ */
480 # ifdef HAVE_ALLOCA_H
482 # endif /* HAVE_ALLOCA_H */
483 # endif /* not __GNUC__ */
485 # endif /* not alloca */
487 # define REGEX_ALLOCATE alloca
489 /* Assumes a `char *destination' variable. */
490 # define REGEX_REALLOCATE(source, osize, nsize) \
491 (destination = (char *) alloca (nsize), \
492 memcpy (destination, source, osize))
494 /* No need to do anything to free, after alloca. */
495 # define REGEX_FREE(arg) ((void)0) /* Do nothing! But inhibit gcc warning. */
497 #endif /* not REGEX_MALLOC */
499 /* Define how to allocate the failure stack. */
501 #if defined REL_ALLOC && defined REGEX_MALLOC
503 # define REGEX_ALLOCATE_STACK(size) \
504 r_alloc (&failure_stack_ptr, (size))
505 # define REGEX_REALLOCATE_STACK(source, osize, nsize) \
506 r_re_alloc (&failure_stack_ptr, (nsize))
507 # define REGEX_FREE_STACK(ptr) \
508 r_alloc_free (&failure_stack_ptr)
510 #else /* not using relocating allocator */
514 # define REGEX_ALLOCATE_STACK malloc
515 # define REGEX_REALLOCATE_STACK(source, osize, nsize) realloc (source, nsize)
516 # define REGEX_FREE_STACK free
518 # else /* not REGEX_MALLOC */
520 # define REGEX_ALLOCATE_STACK alloca
522 # define REGEX_REALLOCATE_STACK(source, osize, nsize) \
523 REGEX_REALLOCATE (source, osize, nsize)
524 /* No need to explicitly free anything. */
525 # define REGEX_FREE_STACK(arg) ((void)0)
527 # endif /* not REGEX_MALLOC */
528 #endif /* not using relocating allocator */
531 /* True if `size1' is non-NULL and PTR is pointing anywhere inside
532 `string1' or just past its end. This works if PTR is NULL, which is
534 #define FIRST_STRING_P(ptr) \
535 (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
537 /* (Re)Allocate N items of type T using malloc, or fail. */
538 #define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t)))
539 #define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
540 #define RETALLOC_IF(addr, n, t) \
541 if (addr) RETALLOC((addr), (n), t); else (addr) = TALLOC ((n), t)
542 #define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
544 #define BYTEWIDTH 8 /* In bits. */
546 #define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
550 #define MAX(a, b) ((a) > (b) ? (a) : (b))
551 #define MIN(a, b) ((a) < (b) ? (a) : (b))
553 /* Type of source-pattern and string chars. */
554 typedef const unsigned char re_char
;
556 typedef char boolean
;
560 static int re_match_2_internal
_RE_ARGS ((struct re_pattern_buffer
*bufp
,
561 re_char
*string1
, int size1
,
562 re_char
*string2
, int size2
,
564 struct re_registers
*regs
,
567 /* These are the command codes that appear in compiled regular
568 expressions. Some opcodes are followed by argument bytes. A
569 command code can specify any interpretation whatsoever for its
570 arguments. Zero bytes may appear in the compiled regular expression. */
576 /* Succeed right away--no more backtracking. */
579 /* Followed by one byte giving n, then by n literal bytes. */
582 /* Matches any (more or less) character. */
585 /* Matches any one char belonging to specified set. First
586 following byte is number of bitmap bytes. Then come bytes
587 for a bitmap saying which chars are in. Bits in each byte
588 are ordered low-bit-first. A character is in the set if its
589 bit is 1. A character too large to have a bit in the map is
590 automatically not in the set.
592 If the length byte has the 0x80 bit set, then that stuff
593 is followed by a range table:
594 2 bytes of flags for character sets (low 8 bits, high 8 bits)
595 See RANGE_TABLE_WORK_BITS below.
596 2 bytes, the number of pairs that follow (upto 32767)
597 pairs, each 2 multibyte characters,
598 each multibyte character represented as 3 bytes. */
601 /* Same parameters as charset, but match any character that is
602 not one of those specified. */
605 /* Start remembering the text that is matched, for storing in a
606 register. Followed by one byte with the register number, in
607 the range 0 to one less than the pattern buffer's re_nsub
611 /* Stop remembering the text that is matched and store it in a
612 memory register. Followed by one byte with the register
613 number, in the range 0 to one less than `re_nsub' in the
617 /* Match a duplicate of something remembered. Followed by one
618 byte containing the register number. */
621 /* Fail unless at beginning of line. */
624 /* Fail unless at end of line. */
627 /* Succeeds if at beginning of buffer (if emacs) or at beginning
628 of string to be matched (if not). */
631 /* Analogously, for end of buffer/string. */
634 /* Followed by two byte relative address to which to jump. */
637 /* Followed by two-byte relative address of place to resume at
638 in case of failure. */
641 /* Like on_failure_jump, but pushes a placeholder instead of the
642 current string position when executed. */
643 on_failure_keep_string_jump
,
645 /* Just like `on_failure_jump', except that it checks that we
646 don't get stuck in an infinite loop (matching an empty string
648 on_failure_jump_loop
,
650 /* Just like `on_failure_jump_loop', except that it checks for
651 a different kind of loop (the kind that shows up with non-greedy
652 operators). This operation has to be immediately preceded
654 on_failure_jump_nastyloop
,
656 /* A smart `on_failure_jump' used for greedy * and + operators.
657 It analyses the loop before which it is put and if the
658 loop does not require backtracking, it changes itself to
659 `on_failure_keep_string_jump' and short-circuits the loop,
660 else it just defaults to changing itself into `on_failure_jump'.
661 It assumes that it is pointing to just past a `jump'. */
662 on_failure_jump_smart
,
664 /* Followed by two-byte relative address and two-byte number n.
665 After matching N times, jump to the address upon failure.
666 Does not work if N starts at 0: use on_failure_jump_loop
670 /* Followed by two-byte relative address, and two-byte number n.
671 Jump to the address N times, then fail. */
674 /* Set the following two-byte relative address to the
675 subsequent two-byte number. The address *includes* the two
679 wordbeg
, /* Succeeds if at word beginning. */
680 wordend
, /* Succeeds if at word end. */
682 wordbound
, /* Succeeds if at a word boundary. */
683 notwordbound
, /* Succeeds if not at a word boundary. */
685 /* Matches any character whose syntax is specified. Followed by
686 a byte which contains a syntax code, e.g., Sword. */
689 /* Matches any character whose syntax is not that specified. */
693 ,before_dot
, /* Succeeds if before point. */
694 at_dot
, /* Succeeds if at point. */
695 after_dot
, /* Succeeds if after point. */
697 /* Matches any character whose category-set contains the specified
698 category. The operator is followed by a byte which contains a
699 category code (mnemonic ASCII character). */
702 /* Matches any character whose category-set does not contain the
703 specified category. The operator is followed by a byte which
704 contains the category code (mnemonic ASCII character). */
709 /* Common operations on the compiled pattern. */
711 /* Store NUMBER in two contiguous bytes starting at DESTINATION. */
713 #define STORE_NUMBER(destination, number) \
715 (destination)[0] = (number) & 0377; \
716 (destination)[1] = (number) >> 8; \
719 /* Same as STORE_NUMBER, except increment DESTINATION to
720 the byte after where the number is stored. Therefore, DESTINATION
721 must be an lvalue. */
723 #define STORE_NUMBER_AND_INCR(destination, number) \
725 STORE_NUMBER (destination, number); \
726 (destination) += 2; \
729 /* Put into DESTINATION a number stored in two contiguous bytes starting
732 #define EXTRACT_NUMBER(destination, source) \
734 (destination) = *(source) & 0377; \
735 (destination) += SIGN_EXTEND_CHAR (*((source) + 1)) << 8; \
739 static void extract_number
_RE_ARGS ((int *dest
, re_char
*source
));
741 extract_number (dest
, source
)
745 int temp
= SIGN_EXTEND_CHAR (*(source
+ 1));
746 *dest
= *source
& 0377;
750 # ifndef EXTRACT_MACROS /* To debug the macros. */
751 # undef EXTRACT_NUMBER
752 # define EXTRACT_NUMBER(dest, src) extract_number (&dest, src)
753 # endif /* not EXTRACT_MACROS */
757 /* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
758 SOURCE must be an lvalue. */
760 #define EXTRACT_NUMBER_AND_INCR(destination, source) \
762 EXTRACT_NUMBER (destination, source); \
767 static void extract_number_and_incr
_RE_ARGS ((int *destination
,
770 extract_number_and_incr (destination
, source
)
774 extract_number (destination
, *source
);
778 # ifndef EXTRACT_MACROS
779 # undef EXTRACT_NUMBER_AND_INCR
780 # define EXTRACT_NUMBER_AND_INCR(dest, src) \
781 extract_number_and_incr (&dest, &src)
782 # endif /* not EXTRACT_MACROS */
786 /* Store a multibyte character in three contiguous bytes starting
787 DESTINATION, and increment DESTINATION to the byte after where the
788 character is stored. Therefore, DESTINATION must be an lvalue. */
790 #define STORE_CHARACTER_AND_INCR(destination, character) \
792 (destination)[0] = (character) & 0377; \
793 (destination)[1] = ((character) >> 8) & 0377; \
794 (destination)[2] = (character) >> 16; \
795 (destination) += 3; \
798 /* Put into DESTINATION a character stored in three contiguous bytes
799 starting at SOURCE. */
801 #define EXTRACT_CHARACTER(destination, source) \
803 (destination) = ((source)[0] \
804 | ((source)[1] << 8) \
805 | ((source)[2] << 16)); \
809 /* Macros for charset. */
811 /* Size of bitmap of charset P in bytes. P is a start of charset,
812 i.e. *P is (re_opcode_t) charset or (re_opcode_t) charset_not. */
813 #define CHARSET_BITMAP_SIZE(p) ((p)[1] & 0x7F)
815 /* Nonzero if charset P has range table. */
816 #define CHARSET_RANGE_TABLE_EXISTS_P(p) ((p)[1] & 0x80)
818 /* Return the address of range table of charset P. But not the start
819 of table itself, but the before where the number of ranges is
820 stored. `2 +' means to skip re_opcode_t and size of bitmap,
821 and the 2 bytes of flags at the start of the range table. */
822 #define CHARSET_RANGE_TABLE(p) (&(p)[4 + CHARSET_BITMAP_SIZE (p)])
824 /* Extract the bit flags that start a range table. */
825 #define CHARSET_RANGE_TABLE_BITS(p) \
826 ((p)[2 + CHARSET_BITMAP_SIZE (p)] \
827 + (p)[3 + CHARSET_BITMAP_SIZE (p)] * 0x100)
829 /* Test if C is listed in the bitmap of charset P. */
830 #define CHARSET_LOOKUP_BITMAP(p, c) \
831 ((c) < CHARSET_BITMAP_SIZE (p) * BYTEWIDTH \
832 && (p)[2 + (c) / BYTEWIDTH] & (1 << ((c) % BYTEWIDTH)))
834 /* Return the address of end of RANGE_TABLE. COUNT is number of
835 ranges (which is a pair of (start, end)) in the RANGE_TABLE. `* 2'
836 is start of range and end of range. `* 3' is size of each start
838 #define CHARSET_RANGE_TABLE_END(range_table, count) \
839 ((range_table) + (count) * 2 * 3)
841 /* Test if C is in RANGE_TABLE. A flag NOT is negated if C is in.
842 COUNT is number of ranges in RANGE_TABLE. */
843 #define CHARSET_LOOKUP_RANGE_TABLE_RAW(not, c, range_table, count) \
846 re_wchar_t range_start, range_end; \
848 re_char *range_table_end \
849 = CHARSET_RANGE_TABLE_END ((range_table), (count)); \
851 for (p = (range_table); p < range_table_end; p += 2 * 3) \
853 EXTRACT_CHARACTER (range_start, p); \
854 EXTRACT_CHARACTER (range_end, p + 3); \
856 if (range_start <= (c) && (c) <= range_end) \
865 /* Test if C is in range table of CHARSET. The flag NOT is negated if
866 C is listed in it. */
867 #define CHARSET_LOOKUP_RANGE_TABLE(not, c, charset) \
870 /* Number of ranges in range table. */ \
872 re_char *range_table = CHARSET_RANGE_TABLE (charset); \
874 EXTRACT_NUMBER_AND_INCR (count, range_table); \
875 CHARSET_LOOKUP_RANGE_TABLE_RAW ((not), (c), range_table, count); \
879 /* If DEBUG is defined, Regex prints many voluminous messages about what
880 it is doing (if the variable `debug' is nonzero). If linked with the
881 main program in `iregex.c', you can enter patterns and strings
882 interactively. And if linked with the main program in `main.c' and
883 the other test files, you can run the already-written tests. */
887 /* We use standard I/O for debugging. */
890 /* It is useful to test things that ``must'' be true when debugging. */
893 static int debug
= -100000;
895 # define DEBUG_STATEMENT(e) e
896 # define DEBUG_PRINT1(x) if (debug > 0) printf (x)
897 # define DEBUG_PRINT2(x1, x2) if (debug > 0) printf (x1, x2)
898 # define DEBUG_PRINT3(x1, x2, x3) if (debug > 0) printf (x1, x2, x3)
899 # define DEBUG_PRINT4(x1, x2, x3, x4) if (debug > 0) printf (x1, x2, x3, x4)
900 # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) \
901 if (debug > 0) print_partial_compiled_pattern (s, e)
902 # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \
903 if (debug > 0) print_double_string (w, s1, sz1, s2, sz2)
906 /* Print the fastmap in human-readable form. */
909 print_fastmap (fastmap
)
912 unsigned was_a_range
= 0;
915 while (i
< (1 << BYTEWIDTH
))
921 while (i
< (1 << BYTEWIDTH
) && fastmap
[i
])
937 /* Print a compiled pattern string in human-readable form, starting at
938 the START pointer into it and ending just before the pointer END. */
941 print_partial_compiled_pattern (start
, end
)
955 /* Loop over pattern commands. */
958 printf ("%d:\t", p
- start
);
960 switch ((re_opcode_t
) *p
++)
972 printf ("/exactn/%d", mcnt
);
982 printf ("/start_memory/%d", *p
++);
986 printf ("/stop_memory/%d", *p
++);
990 printf ("/duplicate/%d", *p
++);
1000 register int c
, last
= -100;
1001 register int in_range
= 0;
1002 int length
= CHARSET_BITMAP_SIZE (p
- 1);
1003 int has_range_table
= CHARSET_RANGE_TABLE_EXISTS_P (p
- 1);
1005 printf ("/charset [%s",
1006 (re_opcode_t
) *(p
- 1) == charset_not
? "^" : "");
1008 assert (p
+ *p
< pend
);
1010 for (c
= 0; c
< 256; c
++)
1012 && (p
[1 + (c
/8)] & (1 << (c
% 8))))
1014 /* Are we starting a range? */
1015 if (last
+ 1 == c
&& ! in_range
)
1020 /* Have we broken a range? */
1021 else if (last
+ 1 != c
&& in_range
)
1040 if (has_range_table
)
1043 printf ("has-range-table");
1045 /* ??? Should print the range table; for now, just skip it. */
1046 p
+= 2; /* skip range table bits */
1047 EXTRACT_NUMBER_AND_INCR (count
, p
);
1048 p
= CHARSET_RANGE_TABLE_END (p
, count
);
1054 printf ("/begline");
1058 printf ("/endline");
1061 case on_failure_jump
:
1062 extract_number_and_incr (&mcnt
, &p
);
1063 printf ("/on_failure_jump to %d", p
+ mcnt
- start
);
1066 case on_failure_keep_string_jump
:
1067 extract_number_and_incr (&mcnt
, &p
);
1068 printf ("/on_failure_keep_string_jump to %d", p
+ mcnt
- start
);
1071 case on_failure_jump_nastyloop
:
1072 extract_number_and_incr (&mcnt
, &p
);
1073 printf ("/on_failure_jump_nastyloop to %d", p
+ mcnt
- start
);
1076 case on_failure_jump_loop
:
1077 extract_number_and_incr (&mcnt
, &p
);
1078 printf ("/on_failure_jump_loop to %d", p
+ mcnt
- start
);
1081 case on_failure_jump_smart
:
1082 extract_number_and_incr (&mcnt
, &p
);
1083 printf ("/on_failure_jump_smart to %d", p
+ mcnt
- start
);
1087 extract_number_and_incr (&mcnt
, &p
);
1088 printf ("/jump to %d", p
+ mcnt
- start
);
1092 extract_number_and_incr (&mcnt
, &p
);
1093 extract_number_and_incr (&mcnt2
, &p
);
1094 printf ("/succeed_n to %d, %d times", p
- 2 + mcnt
- start
, mcnt2
);
1098 extract_number_and_incr (&mcnt
, &p
);
1099 extract_number_and_incr (&mcnt2
, &p
);
1100 printf ("/jump_n to %d, %d times", p
- 2 + mcnt
- start
, mcnt2
);
1104 extract_number_and_incr (&mcnt
, &p
);
1105 extract_number_and_incr (&mcnt2
, &p
);
1106 printf ("/set_number_at location %d to %d", p
- 2 + mcnt
- start
, mcnt2
);
1110 printf ("/wordbound");
1114 printf ("/notwordbound");
1118 printf ("/wordbeg");
1122 printf ("/wordend");
1125 printf ("/syntaxspec");
1127 printf ("/%d", mcnt
);
1131 printf ("/notsyntaxspec");
1133 printf ("/%d", mcnt
);
1138 printf ("/before_dot");
1146 printf ("/after_dot");
1150 printf ("/categoryspec");
1152 printf ("/%d", mcnt
);
1155 case notcategoryspec
:
1156 printf ("/notcategoryspec");
1158 printf ("/%d", mcnt
);
1171 printf ("?%d", *(p
-1));
1177 printf ("%d:\tend of pattern.\n", p
- start
);
1182 print_compiled_pattern (bufp
)
1183 struct re_pattern_buffer
*bufp
;
1185 re_char
*buffer
= bufp
->buffer
;
1187 print_partial_compiled_pattern (buffer
, buffer
+ bufp
->used
);
1188 printf ("%ld bytes used/%ld bytes allocated.\n",
1189 bufp
->used
, bufp
->allocated
);
1191 if (bufp
->fastmap_accurate
&& bufp
->fastmap
)
1193 printf ("fastmap: ");
1194 print_fastmap (bufp
->fastmap
);
1197 printf ("re_nsub: %d\t", bufp
->re_nsub
);
1198 printf ("regs_alloc: %d\t", bufp
->regs_allocated
);
1199 printf ("can_be_null: %d\t", bufp
->can_be_null
);
1200 printf ("no_sub: %d\t", bufp
->no_sub
);
1201 printf ("not_bol: %d\t", bufp
->not_bol
);
1202 printf ("not_eol: %d\t", bufp
->not_eol
);
1203 printf ("syntax: %lx\n", bufp
->syntax
);
1205 /* Perhaps we should print the translate table? */
1210 print_double_string (where
, string1
, size1
, string2
, size2
)
1223 if (FIRST_STRING_P (where
))
1225 for (this_char
= where
- string1
; this_char
< size1
; this_char
++)
1226 putchar (string1
[this_char
]);
1231 for (this_char
= where
- string2
; this_char
< size2
; this_char
++)
1232 putchar (string2
[this_char
]);
1236 #else /* not DEBUG */
1241 # define DEBUG_STATEMENT(e)
1242 # define DEBUG_PRINT1(x)
1243 # define DEBUG_PRINT2(x1, x2)
1244 # define DEBUG_PRINT3(x1, x2, x3)
1245 # define DEBUG_PRINT4(x1, x2, x3, x4)
1246 # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
1247 # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
1249 #endif /* not DEBUG */
1251 /* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
1252 also be assigned to arbitrarily: each pattern buffer stores its own
1253 syntax, so it can be changed between regex compilations. */
1254 /* This has no initializer because initialized variables in Emacs
1255 become read-only after dumping. */
1256 reg_syntax_t re_syntax_options
;
1259 /* Specify the precise syntax of regexps for compilation. This provides
1260 for compatibility for various utilities which historically have
1261 different, incompatible syntaxes.
1263 The argument SYNTAX is a bit mask comprised of the various bits
1264 defined in regex.h. We return the old syntax. */
1267 re_set_syntax (syntax
)
1268 reg_syntax_t syntax
;
1270 reg_syntax_t ret
= re_syntax_options
;
1272 re_syntax_options
= syntax
;
1275 WEAK_ALIAS (__re_set_syntax
, re_set_syntax
)
1277 /* This table gives an error message for each of the error codes listed
1278 in regex.h. Obviously the order here has to be same as there.
1279 POSIX doesn't require that we do anything for REG_NOERROR,
1280 but why not be nice? */
1282 static const char *re_error_msgid
[] =
1284 gettext_noop ("Success"), /* REG_NOERROR */
1285 gettext_noop ("No match"), /* REG_NOMATCH */
1286 gettext_noop ("Invalid regular expression"), /* REG_BADPAT */
1287 gettext_noop ("Invalid collation character"), /* REG_ECOLLATE */
1288 gettext_noop ("Invalid character class name"), /* REG_ECTYPE */
1289 gettext_noop ("Trailing backslash"), /* REG_EESCAPE */
1290 gettext_noop ("Invalid back reference"), /* REG_ESUBREG */
1291 gettext_noop ("Unmatched [ or [^"), /* REG_EBRACK */
1292 gettext_noop ("Unmatched ( or \\("), /* REG_EPAREN */
1293 gettext_noop ("Unmatched \\{"), /* REG_EBRACE */
1294 gettext_noop ("Invalid content of \\{\\}"), /* REG_BADBR */
1295 gettext_noop ("Invalid range end"), /* REG_ERANGE */
1296 gettext_noop ("Memory exhausted"), /* REG_ESPACE */
1297 gettext_noop ("Invalid preceding regular expression"), /* REG_BADRPT */
1298 gettext_noop ("Premature end of regular expression"), /* REG_EEND */
1299 gettext_noop ("Regular expression too big"), /* REG_ESIZE */
1300 gettext_noop ("Unmatched ) or \\)"), /* REG_ERPAREN */
1303 /* Avoiding alloca during matching, to placate r_alloc. */
1305 /* Define MATCH_MAY_ALLOCATE unless we need to make sure that the
1306 searching and matching functions should not call alloca. On some
1307 systems, alloca is implemented in terms of malloc, and if we're
1308 using the relocating allocator routines, then malloc could cause a
1309 relocation, which might (if the strings being searched are in the
1310 ralloc heap) shift the data out from underneath the regexp
1313 Here's another reason to avoid allocation: Emacs
1314 processes input from X in a signal handler; processing X input may
1315 call malloc; if input arrives while a matching routine is calling
1316 malloc, then we're scrod. But Emacs can't just block input while
1317 calling matching routines; then we don't notice interrupts when
1318 they come in. So, Emacs blocks input around all regexp calls
1319 except the matching calls, which it leaves unprotected, in the
1320 faith that they will not malloc. */
1322 /* Normally, this is fine. */
1323 #define MATCH_MAY_ALLOCATE
1325 /* When using GNU C, we are not REALLY using the C alloca, no matter
1326 what config.h may say. So don't take precautions for it. */
1331 /* The match routines may not allocate if (1) they would do it with malloc
1332 and (2) it's not safe for them to use malloc.
1333 Note that if REL_ALLOC is defined, matching would not use malloc for the
1334 failure stack, but we would still use it for the register vectors;
1335 so REL_ALLOC should not affect this. */
1336 #if (defined C_ALLOCA || defined REGEX_MALLOC) && defined emacs
1337 # undef MATCH_MAY_ALLOCATE
1341 /* Failure stack declarations and macros; both re_compile_fastmap and
1342 re_match_2 use a failure stack. These have to be macros because of
1343 REGEX_ALLOCATE_STACK. */
1346 /* Approximate number of failure points for which to initially allocate space
1347 when matching. If this number is exceeded, we allocate more
1348 space, so it is not a hard limit. */
1349 #ifndef INIT_FAILURE_ALLOC
1350 # define INIT_FAILURE_ALLOC 20
1353 /* Roughly the maximum number of failure points on the stack. Would be
1354 exactly that if always used TYPICAL_FAILURE_SIZE items each time we failed.
1355 This is a variable only so users of regex can assign to it; we never
1356 change it ourselves. We always multiply it by TYPICAL_FAILURE_SIZE
1357 before using it, so it should probably be a byte-count instead. */
1358 # if defined MATCH_MAY_ALLOCATE
1359 /* Note that 4400 was enough to cause a crash on Alpha OSF/1,
1360 whose default stack limit is 2mb. In order for a larger
1361 value to work reliably, you have to try to make it accord
1362 with the process stack limit. */
1363 size_t re_max_failures
= 40000;
1365 size_t re_max_failures
= 4000;
1368 union fail_stack_elt
1371 /* This should be the biggest `int' that's no bigger than a pointer. */
1375 typedef union fail_stack_elt fail_stack_elt_t
;
1379 fail_stack_elt_t
*stack
;
1381 size_t avail
; /* Offset of next open position. */
1382 size_t frame
; /* Offset of the cur constructed frame. */
1385 #define FAIL_STACK_EMPTY() (fail_stack.frame == 0)
1386 #define FAIL_STACK_FULL() (fail_stack.avail == fail_stack.size)
1389 /* Define macros to initialize and free the failure stack.
1390 Do `return -2' if the alloc fails. */
1392 #ifdef MATCH_MAY_ALLOCATE
1393 # define INIT_FAIL_STACK() \
1395 fail_stack.stack = (fail_stack_elt_t *) \
1396 REGEX_ALLOCATE_STACK (INIT_FAILURE_ALLOC * TYPICAL_FAILURE_SIZE \
1397 * sizeof (fail_stack_elt_t)); \
1399 if (fail_stack.stack == NULL) \
1402 fail_stack.size = INIT_FAILURE_ALLOC; \
1403 fail_stack.avail = 0; \
1404 fail_stack.frame = 0; \
1407 # define RESET_FAIL_STACK() REGEX_FREE_STACK (fail_stack.stack)
1409 # define INIT_FAIL_STACK() \
1411 fail_stack.avail = 0; \
1412 fail_stack.frame = 0; \
1415 # define RESET_FAIL_STACK() ((void)0)
1419 /* Double the size of FAIL_STACK, up to a limit
1420 which allows approximately `re_max_failures' items.
1422 Return 1 if succeeds, and 0 if either ran out of memory
1423 allocating space for it or it was already too large.
1425 REGEX_REALLOCATE_STACK requires `destination' be declared. */
1427 /* Factor to increase the failure stack size by
1428 when we increase it.
1429 This used to be 2, but 2 was too wasteful
1430 because the old discarded stacks added up to as much space
1431 were as ultimate, maximum-size stack. */
1432 #define FAIL_STACK_GROWTH_FACTOR 4
1434 #define GROW_FAIL_STACK(fail_stack) \
1435 (((fail_stack).size * sizeof (fail_stack_elt_t) \
1436 >= re_max_failures * TYPICAL_FAILURE_SIZE) \
1438 : ((fail_stack).stack \
1439 = (fail_stack_elt_t *) \
1440 REGEX_REALLOCATE_STACK ((fail_stack).stack, \
1441 (fail_stack).size * sizeof (fail_stack_elt_t), \
1442 MIN (re_max_failures * TYPICAL_FAILURE_SIZE, \
1443 ((fail_stack).size * sizeof (fail_stack_elt_t) \
1444 * FAIL_STACK_GROWTH_FACTOR))), \
1446 (fail_stack).stack == NULL \
1448 : ((fail_stack).size \
1449 = (MIN (re_max_failures * TYPICAL_FAILURE_SIZE, \
1450 ((fail_stack).size * sizeof (fail_stack_elt_t) \
1451 * FAIL_STACK_GROWTH_FACTOR)) \
1452 / sizeof (fail_stack_elt_t)), \
1456 /* Push a pointer value onto the failure stack.
1457 Assumes the variable `fail_stack'. Probably should only
1458 be called from within `PUSH_FAILURE_POINT'. */
1459 #define PUSH_FAILURE_POINTER(item) \
1460 fail_stack.stack[fail_stack.avail++].pointer = (item)
1462 /* This pushes an integer-valued item onto the failure stack.
1463 Assumes the variable `fail_stack'. Probably should only
1464 be called from within `PUSH_FAILURE_POINT'. */
1465 #define PUSH_FAILURE_INT(item) \
1466 fail_stack.stack[fail_stack.avail++].integer = (item)
1468 /* Push a fail_stack_elt_t value onto the failure stack.
1469 Assumes the variable `fail_stack'. Probably should only
1470 be called from within `PUSH_FAILURE_POINT'. */
1471 #define PUSH_FAILURE_ELT(item) \
1472 fail_stack.stack[fail_stack.avail++] = (item)
1474 /* These three POP... operations complement the three PUSH... operations.
1475 All assume that `fail_stack' is nonempty. */
1476 #define POP_FAILURE_POINTER() fail_stack.stack[--fail_stack.avail].pointer
1477 #define POP_FAILURE_INT() fail_stack.stack[--fail_stack.avail].integer
1478 #define POP_FAILURE_ELT() fail_stack.stack[--fail_stack.avail]
1480 /* Individual items aside from the registers. */
1481 #define NUM_NONREG_ITEMS 3
1483 /* Used to examine the stack (to detect infinite loops). */
1484 #define FAILURE_PAT(h) fail_stack.stack[(h) - 1].pointer
1485 #define FAILURE_STR(h) (fail_stack.stack[(h) - 2].pointer)
1486 #define NEXT_FAILURE_HANDLE(h) fail_stack.stack[(h) - 3].integer
1487 #define TOP_FAILURE_HANDLE() fail_stack.frame
1490 #define ENSURE_FAIL_STACK(space) \
1491 while (REMAINING_AVAIL_SLOTS <= space) { \
1492 if (!GROW_FAIL_STACK (fail_stack)) \
1494 DEBUG_PRINT2 ("\n Doubled stack; size now: %d\n", (fail_stack).size);\
1495 DEBUG_PRINT2 (" slots available: %d\n", REMAINING_AVAIL_SLOTS);\
1498 /* Push register NUM onto the stack. */
1499 #define PUSH_FAILURE_REG(num) \
1501 char *destination; \
1502 ENSURE_FAIL_STACK(3); \
1503 DEBUG_PRINT4 (" Push reg %d (spanning %p -> %p)\n", \
1504 num, regstart[num], regend[num]); \
1505 PUSH_FAILURE_POINTER (regstart[num]); \
1506 PUSH_FAILURE_POINTER (regend[num]); \
1507 PUSH_FAILURE_INT (num); \
1510 /* Change the counter's value to VAL, but make sure that it will
1511 be reset when backtracking. */
1512 #define PUSH_NUMBER(ptr,val) \
1514 char *destination; \
1516 ENSURE_FAIL_STACK(3); \
1517 EXTRACT_NUMBER (c, ptr); \
1518 DEBUG_PRINT4 (" Push number %p = %d -> %d\n", ptr, c, val); \
1519 PUSH_FAILURE_INT (c); \
1520 PUSH_FAILURE_POINTER (ptr); \
1521 PUSH_FAILURE_INT (-1); \
1522 STORE_NUMBER (ptr, val); \
1525 /* Pop a saved register off the stack. */
1526 #define POP_FAILURE_REG_OR_COUNT() \
1528 int reg = POP_FAILURE_INT (); \
1531 /* It's a counter. */ \
1532 /* Here, we discard `const', making re_match non-reentrant. */ \
1533 unsigned char *ptr = (unsigned char*) POP_FAILURE_POINTER (); \
1534 reg = POP_FAILURE_INT (); \
1535 STORE_NUMBER (ptr, reg); \
1536 DEBUG_PRINT3 (" Pop counter %p = %d\n", ptr, reg); \
1540 regend[reg] = POP_FAILURE_POINTER (); \
1541 regstart[reg] = POP_FAILURE_POINTER (); \
1542 DEBUG_PRINT4 (" Pop reg %d (spanning %p -> %p)\n", \
1543 reg, regstart[reg], regend[reg]); \
1547 /* Check that we are not stuck in an infinite loop. */
1548 #define CHECK_INFINITE_LOOP(pat_cur, string_place) \
1550 int failure = TOP_FAILURE_HANDLE(); \
1551 /* Check for infinite matching loops */ \
1552 while (failure > 0 && \
1553 (FAILURE_STR (failure) == string_place \
1554 || FAILURE_STR (failure) == NULL)) \
1556 assert (FAILURE_PAT (failure) >= bufp->buffer \
1557 && FAILURE_PAT (failure) <= bufp->buffer + bufp->used); \
1558 if (FAILURE_PAT (failure) == pat_cur) \
1560 DEBUG_PRINT2 (" Other pattern: %p\n", FAILURE_PAT (failure)); \
1561 failure = NEXT_FAILURE_HANDLE(failure); \
1563 DEBUG_PRINT2 (" Other string: %p\n", FAILURE_STR (failure)); \
1566 /* Push the information about the state we will need
1567 if we ever fail back to it.
1569 Requires variables fail_stack, regstart, regend and
1570 num_regs be declared. GROW_FAIL_STACK requires `destination' be
1573 Does `return FAILURE_CODE' if runs out of memory. */
1575 #define PUSH_FAILURE_POINT(pattern, string_place) \
1577 char *destination; \
1578 /* Must be int, so when we don't save any registers, the arithmetic \
1579 of 0 + -1 isn't done as unsigned. */ \
1581 DEBUG_STATEMENT (nfailure_points_pushed++); \
1582 DEBUG_PRINT1 ("\nPUSH_FAILURE_POINT:\n"); \
1583 DEBUG_PRINT2 (" Before push, next avail: %d\n", (fail_stack).avail); \
1584 DEBUG_PRINT2 (" size: %d\n", (fail_stack).size);\
1586 ENSURE_FAIL_STACK (NUM_NONREG_ITEMS); \
1588 DEBUG_PRINT1 ("\n"); \
1590 DEBUG_PRINT2 (" Push frame index: %d\n", fail_stack.frame); \
1591 PUSH_FAILURE_INT (fail_stack.frame); \
1593 DEBUG_PRINT2 (" Push string %p: `", string_place); \
1594 DEBUG_PRINT_DOUBLE_STRING (string_place, string1, size1, string2, size2);\
1595 DEBUG_PRINT1 ("'\n"); \
1596 PUSH_FAILURE_POINTER (string_place); \
1598 DEBUG_PRINT2 (" Push pattern %p: ", pattern); \
1599 DEBUG_PRINT_COMPILED_PATTERN (bufp, pattern, pend); \
1600 PUSH_FAILURE_POINTER (pattern); \
1602 /* Close the frame by moving the frame pointer past it. */ \
1603 fail_stack.frame = fail_stack.avail; \
1606 /* Estimate the size of data pushed by a typical failure stack entry.
1607 An estimate is all we need, because all we use this for
1608 is to choose a limit for how big to make the failure stack. */
1609 /* BEWARE, the value `20' is hard-coded in emacs.c:main(). */
1610 #define TYPICAL_FAILURE_SIZE 20
1612 /* How many items can still be added to the stack without overflowing it. */
1613 #define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail)
1616 /* Pops what PUSH_FAIL_STACK pushes.
1618 We restore into the parameters, all of which should be lvalues:
1619 STR -- the saved data position.
1620 PAT -- the saved pattern position.
1621 REGSTART, REGEND -- arrays of string positions.
1623 Also assumes the variables `fail_stack' and (if debugging), `bufp',
1624 `pend', `string1', `size1', `string2', and `size2'. */
1626 #define POP_FAILURE_POINT(str, pat) \
1628 assert (!FAIL_STACK_EMPTY ()); \
1630 /* Remove failure points and point to how many regs pushed. */ \
1631 DEBUG_PRINT1 ("POP_FAILURE_POINT:\n"); \
1632 DEBUG_PRINT2 (" Before pop, next avail: %d\n", fail_stack.avail); \
1633 DEBUG_PRINT2 (" size: %d\n", fail_stack.size); \
1635 /* Pop the saved registers. */ \
1636 while (fail_stack.frame < fail_stack.avail) \
1637 POP_FAILURE_REG_OR_COUNT (); \
1639 pat = POP_FAILURE_POINTER (); \
1640 DEBUG_PRINT2 (" Popping pattern %p: ", pat); \
1641 DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend); \
1643 /* If the saved string location is NULL, it came from an \
1644 on_failure_keep_string_jump opcode, and we want to throw away the \
1645 saved NULL, thus retaining our current position in the string. */ \
1646 str = POP_FAILURE_POINTER (); \
1647 DEBUG_PRINT2 (" Popping string %p: `", str); \
1648 DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2); \
1649 DEBUG_PRINT1 ("'\n"); \
1651 fail_stack.frame = POP_FAILURE_INT (); \
1652 DEBUG_PRINT2 (" Popping frame index: %d\n", fail_stack.frame); \
1654 assert (fail_stack.avail >= 0); \
1655 assert (fail_stack.frame <= fail_stack.avail); \
1657 DEBUG_STATEMENT (nfailure_points_popped++); \
1658 } while (0) /* POP_FAILURE_POINT */
1662 /* Registers are set to a sentinel when they haven't yet matched. */
1663 #define REG_UNSET(e) ((e) == NULL)
1665 /* Subroutine declarations and macros for regex_compile. */
1667 static reg_errcode_t regex_compile
_RE_ARGS ((re_char
*pattern
, size_t size
,
1668 reg_syntax_t syntax
,
1669 struct re_pattern_buffer
*bufp
));
1670 static void store_op1
_RE_ARGS ((re_opcode_t op
, unsigned char *loc
, int arg
));
1671 static void store_op2
_RE_ARGS ((re_opcode_t op
, unsigned char *loc
,
1672 int arg1
, int arg2
));
1673 static void insert_op1
_RE_ARGS ((re_opcode_t op
, unsigned char *loc
,
1674 int arg
, unsigned char *end
));
1675 static void insert_op2
_RE_ARGS ((re_opcode_t op
, unsigned char *loc
,
1676 int arg1
, int arg2
, unsigned char *end
));
1677 static boolean at_begline_loc_p
_RE_ARGS ((re_char
*pattern
,
1679 reg_syntax_t syntax
));
1680 static boolean at_endline_loc_p
_RE_ARGS ((re_char
*p
,
1682 reg_syntax_t syntax
));
1683 static re_char
*skip_one_char
_RE_ARGS ((re_char
*p
));
1684 static int analyse_first
_RE_ARGS ((re_char
*p
, re_char
*pend
,
1685 char *fastmap
, const int multibyte
));
1687 /* Fetch the next character in the uncompiled pattern---translating it
1689 #define PATFETCH(c) \
1693 MAKE_CHAR_MULTIBYTE (c); \
1694 c = TRANSLATE (c); \
1695 if (! target_multibyte) \
1696 MAKE_CHAR_UNIBYTE (c); \
1699 /* Fetch the next character in the uncompiled pattern, with no
1701 #define PATFETCH_RAW(c) \
1704 if (p == pend) return REG_EEND; \
1705 c = RE_STRING_CHAR_AND_LENGTH (p, pend - p, len); \
1710 /* If `translate' is non-null, return translate[D], else just D. We
1711 cast the subscript to translate because some data is declared as
1712 `char *', to avoid warnings when a string constant is passed. But
1713 when we use a character as a subscript we must make it unsigned. */
1715 # define TRANSLATE(d) \
1716 (RE_TRANSLATE_P (translate) ? RE_TRANSLATE (translate, (d)) : (d))
1720 /* Macros for outputting the compiled pattern into `buffer'. */
1722 /* If the buffer isn't allocated when it comes in, use this. */
1723 #define INIT_BUF_SIZE 32
1725 /* Make sure we have at least N more bytes of space in buffer. */
1726 #define GET_BUFFER_SPACE(n) \
1727 while ((size_t) (b - bufp->buffer + (n)) > bufp->allocated) \
1730 /* Make sure we have one more byte of buffer space and then add C to it. */
1731 #define BUF_PUSH(c) \
1733 GET_BUFFER_SPACE (1); \
1734 *b++ = (unsigned char) (c); \
1738 /* Ensure we have two more bytes of buffer space and then append C1 and C2. */
1739 #define BUF_PUSH_2(c1, c2) \
1741 GET_BUFFER_SPACE (2); \
1742 *b++ = (unsigned char) (c1); \
1743 *b++ = (unsigned char) (c2); \
1747 /* As with BUF_PUSH_2, except for three bytes. */
1748 #define BUF_PUSH_3(c1, c2, c3) \
1750 GET_BUFFER_SPACE (3); \
1751 *b++ = (unsigned char) (c1); \
1752 *b++ = (unsigned char) (c2); \
1753 *b++ = (unsigned char) (c3); \
1757 /* Store a jump with opcode OP at LOC to location TO. We store a
1758 relative address offset by the three bytes the jump itself occupies. */
1759 #define STORE_JUMP(op, loc, to) \
1760 store_op1 (op, loc, (to) - (loc) - 3)
1762 /* Likewise, for a two-argument jump. */
1763 #define STORE_JUMP2(op, loc, to, arg) \
1764 store_op2 (op, loc, (to) - (loc) - 3, arg)
1766 /* Like `STORE_JUMP', but for inserting. Assume `b' is the buffer end. */
1767 #define INSERT_JUMP(op, loc, to) \
1768 insert_op1 (op, loc, (to) - (loc) - 3, b)
1770 /* Like `STORE_JUMP2', but for inserting. Assume `b' is the buffer end. */
1771 #define INSERT_JUMP2(op, loc, to, arg) \
1772 insert_op2 (op, loc, (to) - (loc) - 3, arg, b)
1775 /* This is not an arbitrary limit: the arguments which represent offsets
1776 into the pattern are two bytes long. So if 2^16 bytes turns out to
1777 be too small, many things would have to change. */
1778 /* Any other compiler which, like MSC, has allocation limit below 2^16
1779 bytes will have to use approach similar to what was done below for
1780 MSC and drop MAX_BUF_SIZE a bit. Otherwise you may end up
1781 reallocating to 0 bytes. Such thing is not going to work too well.
1782 You have been warned!! */
1783 #if defined _MSC_VER && !defined WIN32
1784 /* Microsoft C 16-bit versions limit malloc to approx 65512 bytes. */
1785 # define MAX_BUF_SIZE 65500L
1787 # define MAX_BUF_SIZE (1L << 16)
1790 /* Extend the buffer by twice its current size via realloc and
1791 reset the pointers that pointed into the old block to point to the
1792 correct places in the new one. If extending the buffer results in it
1793 being larger than MAX_BUF_SIZE, then flag memory exhausted. */
1794 #if __BOUNDED_POINTERS__
1795 # define SET_HIGH_BOUND(P) (__ptrhigh (P) = __ptrlow (P) + bufp->allocated)
1796 # define MOVE_BUFFER_POINTER(P) \
1797 (__ptrlow (P) += incr, SET_HIGH_BOUND (P), __ptrvalue (P) += incr)
1798 # define ELSE_EXTEND_BUFFER_HIGH_BOUND \
1801 SET_HIGH_BOUND (b); \
1802 SET_HIGH_BOUND (begalt); \
1803 if (fixup_alt_jump) \
1804 SET_HIGH_BOUND (fixup_alt_jump); \
1806 SET_HIGH_BOUND (laststart); \
1807 if (pending_exact) \
1808 SET_HIGH_BOUND (pending_exact); \
1811 # define MOVE_BUFFER_POINTER(P) (P) += incr
1812 # define ELSE_EXTEND_BUFFER_HIGH_BOUND
1814 #define EXTEND_BUFFER() \
1816 re_char *old_buffer = bufp->buffer; \
1817 if (bufp->allocated == MAX_BUF_SIZE) \
1819 bufp->allocated <<= 1; \
1820 if (bufp->allocated > MAX_BUF_SIZE) \
1821 bufp->allocated = MAX_BUF_SIZE; \
1822 RETALLOC (bufp->buffer, bufp->allocated, unsigned char); \
1823 if (bufp->buffer == NULL) \
1824 return REG_ESPACE; \
1825 /* If the buffer moved, move all the pointers into it. */ \
1826 if (old_buffer != bufp->buffer) \
1828 int incr = bufp->buffer - old_buffer; \
1829 MOVE_BUFFER_POINTER (b); \
1830 MOVE_BUFFER_POINTER (begalt); \
1831 if (fixup_alt_jump) \
1832 MOVE_BUFFER_POINTER (fixup_alt_jump); \
1834 MOVE_BUFFER_POINTER (laststart); \
1835 if (pending_exact) \
1836 MOVE_BUFFER_POINTER (pending_exact); \
1838 ELSE_EXTEND_BUFFER_HIGH_BOUND \
1842 /* Since we have one byte reserved for the register number argument to
1843 {start,stop}_memory, the maximum number of groups we can report
1844 things about is what fits in that byte. */
1845 #define MAX_REGNUM 255
1847 /* But patterns can have more than `MAX_REGNUM' registers. We just
1848 ignore the excess. */
1849 typedef unsigned regnum_t
;
1852 /* Macros for the compile stack. */
1854 /* Since offsets can go either forwards or backwards, this type needs to
1855 be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1. */
1856 /* int may be not enough when sizeof(int) == 2. */
1857 typedef long pattern_offset_t
;
1861 pattern_offset_t begalt_offset
;
1862 pattern_offset_t fixup_alt_jump
;
1863 pattern_offset_t laststart_offset
;
1865 } compile_stack_elt_t
;
1870 compile_stack_elt_t
*stack
;
1872 unsigned avail
; /* Offset of next open position. */
1873 } compile_stack_type
;
1876 #define INIT_COMPILE_STACK_SIZE 32
1878 #define COMPILE_STACK_EMPTY (compile_stack.avail == 0)
1879 #define COMPILE_STACK_FULL (compile_stack.avail == compile_stack.size)
1881 /* The next available element. */
1882 #define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
1885 /* Structure to manage work area for range table. */
1886 struct range_table_work_area
1888 int *table
; /* actual work area. */
1889 int allocated
; /* allocated size for work area in bytes. */
1890 int used
; /* actually used size in words. */
1891 int bits
; /* flag to record character classes */
1894 /* Make sure that WORK_AREA can hold more N multibyte characters. */
1895 #define EXTEND_RANGE_TABLE_WORK_AREA(work_area, n) \
1897 if (((work_area).used + (n)) * sizeof (int) > (work_area).allocated) \
1899 (work_area).allocated += 16 * sizeof (int); \
1900 if ((work_area).table) \
1902 = (int *) realloc ((work_area).table, (work_area).allocated); \
1905 = (int *) malloc ((work_area).allocated); \
1906 if ((work_area).table == 0) \
1907 FREE_STACK_RETURN (REG_ESPACE); \
1911 #define SET_RANGE_TABLE_WORK_AREA_BIT(work_area, bit) \
1912 (work_area).bits |= (bit)
1914 /* Bits used to implement the multibyte-part of the various character classes
1915 such as [:alnum:] in a charset's range table. */
1916 #define BIT_WORD 0x1
1917 #define BIT_LOWER 0x2
1918 #define BIT_PUNCT 0x4
1919 #define BIT_SPACE 0x8
1920 #define BIT_UPPER 0x10
1921 #define BIT_MULTIBYTE 0x20
1923 /* Set a range (RANGE_START, RANGE_END) to WORK_AREA. */
1924 #define SET_RANGE_TABLE_WORK_AREA(work_area, range_start, range_end) \
1926 EXTEND_RANGE_TABLE_WORK_AREA ((work_area), 2); \
1927 (work_area).table[(work_area).used++] = (range_start); \
1928 (work_area).table[(work_area).used++] = (range_end); \
1931 /* Free allocated memory for WORK_AREA. */
1932 #define FREE_RANGE_TABLE_WORK_AREA(work_area) \
1934 if ((work_area).table) \
1935 free ((work_area).table); \
1938 #define CLEAR_RANGE_TABLE_WORK_USED(work_area) ((work_area).used = 0, (work_area).bits = 0)
1939 #define RANGE_TABLE_WORK_USED(work_area) ((work_area).used)
1940 #define RANGE_TABLE_WORK_BITS(work_area) ((work_area).bits)
1941 #define RANGE_TABLE_WORK_ELT(work_area, i) ((work_area).table[i])
1944 /* Set the bit for character C in a list. */
1945 #define SET_LIST_BIT(c) (b[((c)) / BYTEWIDTH] |= 1 << ((c) % BYTEWIDTH))
1950 /* It is better to implement this jumbo macro by a function, but it's
1951 not that easy because macros called within it assumes various
1952 variables being defined. */
1954 #define SETUP_MULTIBYTE_RANGE(work_area, c0, c1) \
1956 re_wchar_t c, t, t_last; \
1960 t_last = multibyte ? TRANSLATE (c) : TRANSLATE (MAKE_CHAR_MULTIBYTE (c)); \
1961 for (c++, n = 1; c <= (c1); c++, n++) \
1963 t = multibyte ? TRANSLATE (c) : TRANSLATE (MAKE_CHAR_MULTIBYTE (c)); \
1964 if (t_last + n == t) \
1966 SET_RANGE_TABLE_WORK_AREA ((work_area), t_last, t_last + n - 1); \
1971 SET_RANGE_TABLE_WORK_AREA ((work_area), t_last, t_last + n - 1); \
1977 /* Get the next unsigned number in the uncompiled pattern. */
1978 #define GET_UNSIGNED_NUMBER(num) \
1979 do { if (p != pend) \
1982 while ('0' <= c && c <= '9') \
1986 num = num * 10 + c - '0'; \
1994 #if WIDE_CHAR_SUPPORT
1995 /* The GNU C library provides support for user-defined character classes
1996 and the functions from ISO C amendement 1. */
1997 # ifdef CHARCLASS_NAME_MAX
1998 # define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
2000 /* This shouldn't happen but some implementation might still have this
2001 problem. Use a reasonable default value. */
2002 # define CHAR_CLASS_MAX_LENGTH 256
2004 typedef wctype_t re_wctype_t
;
2005 typedef wchar_t re_wchar_t
;
2006 # define re_wctype wctype
2007 # define re_iswctype iswctype
2008 # define re_wctype_to_bit(cc) 0
2010 # define CHAR_CLASS_MAX_LENGTH 9 /* Namely, `multibyte'. */
2013 /* Character classes. */
2014 typedef enum { RECC_ERROR
= 0,
2015 RECC_ALNUM
, RECC_ALPHA
, RECC_WORD
,
2016 RECC_GRAPH
, RECC_PRINT
,
2017 RECC_LOWER
, RECC_UPPER
,
2018 RECC_PUNCT
, RECC_CNTRL
,
2019 RECC_DIGIT
, RECC_XDIGIT
,
2020 RECC_BLANK
, RECC_SPACE
,
2021 RECC_MULTIBYTE
, RECC_NONASCII
,
2022 RECC_ASCII
, RECC_UNIBYTE
2025 typedef int re_wchar_t
;
2027 /* Map a string to the char class it names (if any). */
2032 const char *string
= str
;
2033 if (STREQ (string
, "alnum")) return RECC_ALNUM
;
2034 else if (STREQ (string
, "alpha")) return RECC_ALPHA
;
2035 else if (STREQ (string
, "word")) return RECC_WORD
;
2036 else if (STREQ (string
, "ascii")) return RECC_ASCII
;
2037 else if (STREQ (string
, "nonascii")) return RECC_NONASCII
;
2038 else if (STREQ (string
, "graph")) return RECC_GRAPH
;
2039 else if (STREQ (string
, "lower")) return RECC_LOWER
;
2040 else if (STREQ (string
, "print")) return RECC_PRINT
;
2041 else if (STREQ (string
, "punct")) return RECC_PUNCT
;
2042 else if (STREQ (string
, "space")) return RECC_SPACE
;
2043 else if (STREQ (string
, "upper")) return RECC_UPPER
;
2044 else if (STREQ (string
, "unibyte")) return RECC_UNIBYTE
;
2045 else if (STREQ (string
, "multibyte")) return RECC_MULTIBYTE
;
2046 else if (STREQ (string
, "digit")) return RECC_DIGIT
;
2047 else if (STREQ (string
, "xdigit")) return RECC_XDIGIT
;
2048 else if (STREQ (string
, "cntrl")) return RECC_CNTRL
;
2049 else if (STREQ (string
, "blank")) return RECC_BLANK
;
2053 /* True iff CH is in the char class CC. */
2055 re_iswctype (ch
, cc
)
2061 case RECC_ALNUM
: return ISALNUM (ch
);
2062 case RECC_ALPHA
: return ISALPHA (ch
);
2063 case RECC_BLANK
: return ISBLANK (ch
);
2064 case RECC_CNTRL
: return ISCNTRL (ch
);
2065 case RECC_DIGIT
: return ISDIGIT (ch
);
2066 case RECC_GRAPH
: return ISGRAPH (ch
);
2067 case RECC_LOWER
: return ISLOWER (ch
);
2068 case RECC_PRINT
: return ISPRINT (ch
);
2069 case RECC_PUNCT
: return ISPUNCT (ch
);
2070 case RECC_SPACE
: return ISSPACE (ch
);
2071 case RECC_UPPER
: return ISUPPER (ch
);
2072 case RECC_XDIGIT
: return ISXDIGIT (ch
);
2073 case RECC_ASCII
: return IS_REAL_ASCII (ch
);
2074 case RECC_NONASCII
: return !IS_REAL_ASCII (ch
);
2075 case RECC_UNIBYTE
: return ISUNIBYTE (ch
);
2076 case RECC_MULTIBYTE
: return !ISUNIBYTE (ch
);
2077 case RECC_WORD
: return ISWORD (ch
);
2078 case RECC_ERROR
: return false;
2084 /* Return a bit-pattern to use in the range-table bits to match multibyte
2085 chars of class CC. */
2087 re_wctype_to_bit (cc
)
2092 case RECC_NONASCII
: case RECC_PRINT
: case RECC_GRAPH
:
2093 case RECC_MULTIBYTE
: return BIT_MULTIBYTE
;
2094 case RECC_ALPHA
: case RECC_ALNUM
: case RECC_WORD
: return BIT_WORD
;
2095 case RECC_LOWER
: return BIT_LOWER
;
2096 case RECC_UPPER
: return BIT_UPPER
;
2097 case RECC_PUNCT
: return BIT_PUNCT
;
2098 case RECC_SPACE
: return BIT_SPACE
;
2099 case RECC_ASCII
: case RECC_DIGIT
: case RECC_XDIGIT
: case RECC_CNTRL
:
2100 case RECC_BLANK
: case RECC_UNIBYTE
: case RECC_ERROR
: return 0;
2107 /* Explicit quit checking is only used on NTemacs. */
2108 #if defined WINDOWSNT && defined emacs && defined QUIT
2109 extern int immediate_quit
;
2110 # define IMMEDIATE_QUIT_CHECK \
2112 if (immediate_quit) QUIT; \
2115 # define IMMEDIATE_QUIT_CHECK ((void)0)
2118 #ifndef MATCH_MAY_ALLOCATE
2120 /* If we cannot allocate large objects within re_match_2_internal,
2121 we make the fail stack and register vectors global.
2122 The fail stack, we grow to the maximum size when a regexp
2124 The register vectors, we adjust in size each time we
2125 compile a regexp, according to the number of registers it needs. */
2127 static fail_stack_type fail_stack
;
2129 /* Size with which the following vectors are currently allocated.
2130 That is so we can make them bigger as needed,
2131 but never make them smaller. */
2132 static int regs_allocated_size
;
2134 static re_char
** regstart
, ** regend
;
2135 static re_char
**best_regstart
, **best_regend
;
2137 /* Make the register vectors big enough for NUM_REGS registers,
2138 but don't make them smaller. */
2141 regex_grow_registers (num_regs
)
2144 if (num_regs
> regs_allocated_size
)
2146 RETALLOC_IF (regstart
, num_regs
, re_char
*);
2147 RETALLOC_IF (regend
, num_regs
, re_char
*);
2148 RETALLOC_IF (best_regstart
, num_regs
, re_char
*);
2149 RETALLOC_IF (best_regend
, num_regs
, re_char
*);
2151 regs_allocated_size
= num_regs
;
2155 #endif /* not MATCH_MAY_ALLOCATE */
2157 static boolean group_in_compile_stack
_RE_ARGS ((compile_stack_type
2161 /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
2162 Returns one of error codes defined in `regex.h', or zero for success.
2164 Assumes the `allocated' (and perhaps `buffer') and `translate'
2165 fields are set in BUFP on entry.
2167 If it succeeds, results are put in BUFP (if it returns an error, the
2168 contents of BUFP are undefined):
2169 `buffer' is the compiled pattern;
2170 `syntax' is set to SYNTAX;
2171 `used' is set to the length of the compiled pattern;
2172 `fastmap_accurate' is zero;
2173 `re_nsub' is the number of subexpressions in PATTERN;
2174 `not_bol' and `not_eol' are zero;
2176 The `fastmap' field is neither examined nor set. */
2178 /* Insert the `jump' from the end of last alternative to "here".
2179 The space for the jump has already been allocated. */
2180 #define FIXUP_ALT_JUMP() \
2182 if (fixup_alt_jump) \
2183 STORE_JUMP (jump, fixup_alt_jump, b); \
2187 /* Return, freeing storage we allocated. */
2188 #define FREE_STACK_RETURN(value) \
2190 FREE_RANGE_TABLE_WORK_AREA (range_table_work); \
2191 free (compile_stack.stack); \
2195 static reg_errcode_t
2196 regex_compile (pattern
, size
, syntax
, bufp
)
2199 reg_syntax_t syntax
;
2200 struct re_pattern_buffer
*bufp
;
2202 /* We fetch characters from PATTERN here. */
2203 register re_wchar_t c
, c1
;
2205 /* A random temporary spot in PATTERN. */
2208 /* Points to the end of the buffer, where we should append. */
2209 register unsigned char *b
;
2211 /* Keeps track of unclosed groups. */
2212 compile_stack_type compile_stack
;
2214 /* Points to the current (ending) position in the pattern. */
2216 /* `const' makes AIX compiler fail. */
2217 unsigned char *p
= pattern
;
2219 re_char
*p
= pattern
;
2221 re_char
*pend
= pattern
+ size
;
2223 /* How to translate the characters in the pattern. */
2224 RE_TRANSLATE_TYPE translate
= bufp
->translate
;
2226 /* Address of the count-byte of the most recently inserted `exactn'
2227 command. This makes it possible to tell if a new exact-match
2228 character can be added to that command or if the character requires
2229 a new `exactn' command. */
2230 unsigned char *pending_exact
= 0;
2232 /* Address of start of the most recently finished expression.
2233 This tells, e.g., postfix * where to find the start of its
2234 operand. Reset at the beginning of groups and alternatives. */
2235 unsigned char *laststart
= 0;
2237 /* Address of beginning of regexp, or inside of last group. */
2238 unsigned char *begalt
;
2240 /* Place in the uncompiled pattern (i.e., the {) to
2241 which to go back if the interval is invalid. */
2242 re_char
*beg_interval
;
2244 /* Address of the place where a forward jump should go to the end of
2245 the containing expression. Each alternative of an `or' -- except the
2246 last -- ends with a forward jump of this sort. */
2247 unsigned char *fixup_alt_jump
= 0;
2249 /* Counts open-groups as they are encountered. Remembered for the
2250 matching close-group on the compile stack, so the same register
2251 number is put in the stop_memory as the start_memory. */
2252 regnum_t regnum
= 0;
2254 /* Work area for range table of charset. */
2255 struct range_table_work_area range_table_work
;
2257 /* If the object matched can contain multibyte characters. */
2258 const boolean multibyte
= RE_MULTIBYTE_P (bufp
);
2260 /* If a target can contain multibyte characters. */
2261 const boolean target_multibyte
= RE_TARGET_MULTIBYTE_P (bufp
);
2265 DEBUG_PRINT1 ("\nCompiling pattern: ");
2268 unsigned debug_count
;
2270 for (debug_count
= 0; debug_count
< size
; debug_count
++)
2271 putchar (pattern
[debug_count
]);
2276 /* Initialize the compile stack. */
2277 compile_stack
.stack
= TALLOC (INIT_COMPILE_STACK_SIZE
, compile_stack_elt_t
);
2278 if (compile_stack
.stack
== NULL
)
2281 compile_stack
.size
= INIT_COMPILE_STACK_SIZE
;
2282 compile_stack
.avail
= 0;
2284 range_table_work
.table
= 0;
2285 range_table_work
.allocated
= 0;
2287 /* Initialize the pattern buffer. */
2288 bufp
->syntax
= syntax
;
2289 bufp
->fastmap_accurate
= 0;
2290 bufp
->not_bol
= bufp
->not_eol
= 0;
2292 /* Set `used' to zero, so that if we return an error, the pattern
2293 printer (for debugging) will think there's no pattern. We reset it
2297 /* Always count groups, whether or not bufp->no_sub is set. */
2300 #if !defined emacs && !defined SYNTAX_TABLE
2301 /* Initialize the syntax table. */
2302 init_syntax_once ();
2305 if (bufp
->allocated
== 0)
2308 { /* If zero allocated, but buffer is non-null, try to realloc
2309 enough space. This loses if buffer's address is bogus, but
2310 that is the user's responsibility. */
2311 RETALLOC (bufp
->buffer
, INIT_BUF_SIZE
, unsigned char);
2314 { /* Caller did not allocate a buffer. Do it for them. */
2315 bufp
->buffer
= TALLOC (INIT_BUF_SIZE
, unsigned char);
2317 if (!bufp
->buffer
) FREE_STACK_RETURN (REG_ESPACE
);
2319 bufp
->allocated
= INIT_BUF_SIZE
;
2322 begalt
= b
= bufp
->buffer
;
2324 /* Loop through the uncompiled pattern until we're at the end. */
2333 if ( /* If at start of pattern, it's an operator. */
2335 /* If context independent, it's an operator. */
2336 || syntax
& RE_CONTEXT_INDEP_ANCHORS
2337 /* Otherwise, depends on what's come before. */
2338 || at_begline_loc_p (pattern
, p
, syntax
))
2339 BUF_PUSH ((syntax
& RE_NO_NEWLINE_ANCHOR
) ? begbuf
: begline
);
2348 if ( /* If at end of pattern, it's an operator. */
2350 /* If context independent, it's an operator. */
2351 || syntax
& RE_CONTEXT_INDEP_ANCHORS
2352 /* Otherwise, depends on what's next. */
2353 || at_endline_loc_p (p
, pend
, syntax
))
2354 BUF_PUSH ((syntax
& RE_NO_NEWLINE_ANCHOR
) ? endbuf
: endline
);
2363 if ((syntax
& RE_BK_PLUS_QM
)
2364 || (syntax
& RE_LIMITED_OPS
))
2368 /* If there is no previous pattern... */
2371 if (syntax
& RE_CONTEXT_INVALID_OPS
)
2372 FREE_STACK_RETURN (REG_BADRPT
);
2373 else if (!(syntax
& RE_CONTEXT_INDEP_OPS
))
2378 /* 1 means zero (many) matches is allowed. */
2379 boolean zero_times_ok
= 0, many_times_ok
= 0;
2382 /* If there is a sequence of repetition chars, collapse it
2383 down to just one (the right one). We can't combine
2384 interval operators with these because of, e.g., `a{2}*',
2385 which should only match an even number of `a's. */
2389 if ((syntax
& RE_FRUGAL
)
2390 && c
== '?' && (zero_times_ok
|| many_times_ok
))
2394 zero_times_ok
|= c
!= '+';
2395 many_times_ok
|= c
!= '?';
2401 || (!(syntax
& RE_BK_PLUS_QM
)
2402 && (*p
== '+' || *p
== '?')))
2404 else if (syntax
& RE_BK_PLUS_QM
&& *p
== '\\')
2407 FREE_STACK_RETURN (REG_EESCAPE
);
2408 if (p
[1] == '+' || p
[1] == '?')
2409 PATFETCH_RAW (c
); /* Gobble up the backslash. */
2415 /* If we get here, we found another repeat character. */
2419 /* Star, etc. applied to an empty pattern is equivalent
2420 to an empty pattern. */
2421 if (!laststart
|| laststart
== b
)
2424 /* Now we know whether or not zero matches is allowed
2425 and also whether or not two or more matches is allowed. */
2430 boolean simple
= skip_one_char (laststart
) == b
;
2431 unsigned int startoffset
= 0;
2433 /* Check if the loop can match the empty string. */
2434 (simple
|| !analyse_first (laststart
, b
, NULL
, 0)) ?
2435 on_failure_jump
: on_failure_jump_loop
;
2436 assert (skip_one_char (laststart
) <= b
);
2438 if (!zero_times_ok
&& simple
)
2439 { /* Since simple * loops can be made faster by using
2440 on_failure_keep_string_jump, we turn simple P+
2441 into PP* if P is simple. */
2442 unsigned char *p1
, *p2
;
2443 startoffset
= b
- laststart
;
2444 GET_BUFFER_SPACE (startoffset
);
2445 p1
= b
; p2
= laststart
;
2451 GET_BUFFER_SPACE (6);
2454 STORE_JUMP (ofj
, b
, b
+ 6);
2456 /* Simple * loops can use on_failure_keep_string_jump
2457 depending on what follows. But since we don't know
2458 that yet, we leave the decision up to
2459 on_failure_jump_smart. */
2460 INSERT_JUMP (simple
? on_failure_jump_smart
: ofj
,
2461 laststart
+ startoffset
, b
+ 6);
2463 STORE_JUMP (jump
, b
, laststart
+ startoffset
);
2468 /* A simple ? pattern. */
2469 assert (zero_times_ok
);
2470 GET_BUFFER_SPACE (3);
2471 INSERT_JUMP (on_failure_jump
, laststart
, b
+ 3);
2475 else /* not greedy */
2476 { /* I wish the greedy and non-greedy cases could be merged. */
2478 GET_BUFFER_SPACE (7); /* We might use less. */
2481 boolean emptyp
= analyse_first (laststart
, b
, NULL
, 0);
2483 /* The non-greedy multiple match looks like a repeat..until:
2484 we only need a conditional jump at the end of the loop */
2485 if (emptyp
) BUF_PUSH (no_op
);
2486 STORE_JUMP (emptyp
? on_failure_jump_nastyloop
2487 : on_failure_jump
, b
, laststart
);
2491 /* The repeat...until naturally matches one or more.
2492 To also match zero times, we need to first jump to
2493 the end of the loop (its conditional jump). */
2494 INSERT_JUMP (jump
, laststart
, b
);
2500 /* non-greedy a?? */
2501 INSERT_JUMP (jump
, laststart
, b
+ 3);
2503 INSERT_JUMP (on_failure_jump
, laststart
, laststart
+ 6);
2520 CLEAR_RANGE_TABLE_WORK_USED (range_table_work
);
2522 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2524 /* Ensure that we have enough space to push a charset: the
2525 opcode, the length count, and the bitset; 34 bytes in all. */
2526 GET_BUFFER_SPACE (34);
2530 /* We test `*p == '^' twice, instead of using an if
2531 statement, so we only need one BUF_PUSH. */
2532 BUF_PUSH (*p
== '^' ? charset_not
: charset
);
2536 /* Remember the first position in the bracket expression. */
2539 /* Push the number of bytes in the bitmap. */
2540 BUF_PUSH ((1 << BYTEWIDTH
) / BYTEWIDTH
);
2542 /* Clear the whole map. */
2543 bzero (b
, (1 << BYTEWIDTH
) / BYTEWIDTH
);
2545 /* charset_not matches newline according to a syntax bit. */
2546 if ((re_opcode_t
) b
[-2] == charset_not
2547 && (syntax
& RE_HAT_LISTS_NOT_NEWLINE
))
2548 SET_LIST_BIT ('\n');
2550 /* Read in characters and ranges, setting map bits. */
2553 boolean escaped_char
= false;
2554 const unsigned char *p2
= p
;
2556 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2560 /* \ might escape characters inside [...] and [^...]. */
2561 if ((syntax
& RE_BACKSLASH_ESCAPE_IN_LISTS
) && c
== '\\')
2563 if (p
== pend
) FREE_STACK_RETURN (REG_EESCAPE
);
2566 escaped_char
= true;
2570 /* Could be the end of the bracket expression. If it's
2571 not (i.e., when the bracket expression is `[]' so
2572 far), the ']' character bit gets set way below. */
2573 if (c
== ']' && p2
!= p1
)
2577 /* See if we're at the beginning of a possible character
2580 if (!escaped_char
&&
2581 syntax
& RE_CHAR_CLASSES
&& c
== '[' && *p
== ':')
2583 /* Leave room for the null. */
2584 unsigned char str
[CHAR_CLASS_MAX_LENGTH
+ 1];
2585 const unsigned char *class_beg
;
2591 /* If pattern is `[[:'. */
2592 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2597 if ((c
== ':' && *p
== ']') || p
== pend
)
2599 if (c1
< CHAR_CLASS_MAX_LENGTH
)
2602 /* This is in any case an invalid class name. */
2607 /* If isn't a word bracketed by `[:' and `:]':
2608 undo the ending character, the letters, and
2609 leave the leading `:' and `[' (but set bits for
2611 if (c
== ':' && *p
== ']')
2616 cc
= re_wctype (str
);
2619 FREE_STACK_RETURN (REG_ECTYPE
);
2621 /* Throw away the ] at the end of the character
2625 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2627 /* Most character classes in a multibyte match
2628 just set a flag. Exceptions are is_blank,
2629 is_digit, is_cntrl, and is_xdigit, since
2630 they can only match ASCII characters. We
2631 don't need to handle them for multibyte.
2632 They are distinguished by a negative wctype.
2633 We need this only for Emacs. */
2635 SET_RANGE_TABLE_WORK_AREA_BIT (range_table_work
,
2636 re_wctype_to_bit (cc
));
2639 for (ch
= 0; ch
< 1 << BYTEWIDTH
; ++ch
)
2641 MAKE_CHAR_MULTIBYTE (ch
);
2642 ch
= TRANSLATE (ch
);
2643 if (IS_REAL_ASCII (ch
)
2644 & re_iswctype (btowc (ch
), cc
))
2648 /* Repeat the loop. */
2653 /* Go back to right after the "[:". */
2657 /* Because the `:' may starts the range, we
2658 can't simply set bit and repeat the loop.
2659 Instead, just set it to C and handle below. */
2664 if (p
< pend
&& p
[0] == '-' && p
[1] != ']')
2667 /* Discard the `-'. */
2670 /* Fetch the character which ends the range. */
2674 if (syntax
& RE_NO_EMPTY_RANGES
)
2675 FREE_STACK_RETURN (REG_ERANGE
);
2676 /* Else, repeat the loop. */
2683 c1
= TRANSLATE (c1
);
2684 #else /* not emacs */
2685 if (target_multibyte
)
2687 if (! IS_REAL_ASCII (c1
))
2689 re_wchar_t c0
= MAX (c
, 128);
2691 SETUP_MULTIBYTE_RANGE (range_table_work
, c0
, c1
);
2699 MAKE_CHAR_UNIBYTE (c
);
2700 MAKE_CHAR_UNIBYTE (c1
);
2703 #endif /* not emacs */
2704 /* Set the range into bitmap */
2705 for (; c
<= c1
; c
++)
2706 SET_LIST_BIT (TRANSLATE (c
));
2709 /* Discard any (non)matching list bytes that are all 0 at the
2710 end of the map. Decrease the map-length byte too. */
2711 while ((int) b
[-1] > 0 && b
[b
[-1] - 1] == 0)
2715 /* Build real range table from work area. */
2716 if (RANGE_TABLE_WORK_USED (range_table_work
)
2717 || RANGE_TABLE_WORK_BITS (range_table_work
))
2720 int used
= RANGE_TABLE_WORK_USED (range_table_work
);
2722 /* Allocate space for COUNT + RANGE_TABLE. Needs two
2723 bytes for flags, two for COUNT, and three bytes for
2725 GET_BUFFER_SPACE (4 + used
* 3);
2727 /* Indicate the existence of range table. */
2728 laststart
[1] |= 0x80;
2730 /* Store the character class flag bits into the range table.
2731 If not in emacs, these flag bits are always 0. */
2732 *b
++ = RANGE_TABLE_WORK_BITS (range_table_work
) & 0xff;
2733 *b
++ = RANGE_TABLE_WORK_BITS (range_table_work
) >> 8;
2735 STORE_NUMBER_AND_INCR (b
, used
/ 2);
2736 for (i
= 0; i
< used
; i
++)
2737 STORE_CHARACTER_AND_INCR
2738 (b
, RANGE_TABLE_WORK_ELT (range_table_work
, i
));
2745 if (syntax
& RE_NO_BK_PARENS
)
2752 if (syntax
& RE_NO_BK_PARENS
)
2759 if (syntax
& RE_NEWLINE_ALT
)
2766 if (syntax
& RE_NO_BK_VBAR
)
2773 if (syntax
& RE_INTERVALS
&& syntax
& RE_NO_BK_BRACES
)
2774 goto handle_interval
;
2780 if (p
== pend
) FREE_STACK_RETURN (REG_EESCAPE
);
2782 /* Do not translate the character after the \, so that we can
2783 distinguish, e.g., \B from \b, even if we normally would
2784 translate, e.g., B to b. */
2790 if (syntax
& RE_NO_BK_PARENS
)
2791 goto normal_backslash
;
2798 /* Look for a special (?...) construct */
2799 if ((syntax
& RE_SHY_GROUPS
) && *p
== '?')
2801 PATFETCH_RAW (c
); /* Gobble up the '?'. */
2805 case ':': shy
= 1; break;
2807 /* Only (?:...) is supported right now. */
2808 FREE_STACK_RETURN (REG_BADPAT
);
2819 if (COMPILE_STACK_FULL
)
2821 RETALLOC (compile_stack
.stack
, compile_stack
.size
<< 1,
2822 compile_stack_elt_t
);
2823 if (compile_stack
.stack
== NULL
) return REG_ESPACE
;
2825 compile_stack
.size
<<= 1;
2828 /* These are the values to restore when we hit end of this
2829 group. They are all relative offsets, so that if the
2830 whole pattern moves because of realloc, they will still
2832 COMPILE_STACK_TOP
.begalt_offset
= begalt
- bufp
->buffer
;
2833 COMPILE_STACK_TOP
.fixup_alt_jump
2834 = fixup_alt_jump
? fixup_alt_jump
- bufp
->buffer
+ 1 : 0;
2835 COMPILE_STACK_TOP
.laststart_offset
= b
- bufp
->buffer
;
2836 COMPILE_STACK_TOP
.regnum
= shy
? -regnum
: regnum
;
2839 start_memory for groups beyond the last one we can
2840 represent in the compiled pattern. */
2841 if (regnum
<= MAX_REGNUM
&& !shy
)
2842 BUF_PUSH_2 (start_memory
, regnum
);
2844 compile_stack
.avail
++;
2849 /* If we've reached MAX_REGNUM groups, then this open
2850 won't actually generate any code, so we'll have to
2851 clear pending_exact explicitly. */
2857 if (syntax
& RE_NO_BK_PARENS
) goto normal_backslash
;
2859 if (COMPILE_STACK_EMPTY
)
2861 if (syntax
& RE_UNMATCHED_RIGHT_PAREN_ORD
)
2862 goto normal_backslash
;
2864 FREE_STACK_RETURN (REG_ERPAREN
);
2870 /* See similar code for backslashed left paren above. */
2871 if (COMPILE_STACK_EMPTY
)
2873 if (syntax
& RE_UNMATCHED_RIGHT_PAREN_ORD
)
2876 FREE_STACK_RETURN (REG_ERPAREN
);
2879 /* Since we just checked for an empty stack above, this
2880 ``can't happen''. */
2881 assert (compile_stack
.avail
!= 0);
2883 /* We don't just want to restore into `regnum', because
2884 later groups should continue to be numbered higher,
2885 as in `(ab)c(de)' -- the second group is #2. */
2886 regnum_t this_group_regnum
;
2888 compile_stack
.avail
--;
2889 begalt
= bufp
->buffer
+ COMPILE_STACK_TOP
.begalt_offset
;
2891 = COMPILE_STACK_TOP
.fixup_alt_jump
2892 ? bufp
->buffer
+ COMPILE_STACK_TOP
.fixup_alt_jump
- 1
2894 laststart
= bufp
->buffer
+ COMPILE_STACK_TOP
.laststart_offset
;
2895 this_group_regnum
= COMPILE_STACK_TOP
.regnum
;
2896 /* If we've reached MAX_REGNUM groups, then this open
2897 won't actually generate any code, so we'll have to
2898 clear pending_exact explicitly. */
2901 /* We're at the end of the group, so now we know how many
2902 groups were inside this one. */
2903 if (this_group_regnum
<= MAX_REGNUM
&& this_group_regnum
> 0)
2904 BUF_PUSH_2 (stop_memory
, this_group_regnum
);
2909 case '|': /* `\|'. */
2910 if (syntax
& RE_LIMITED_OPS
|| syntax
& RE_NO_BK_VBAR
)
2911 goto normal_backslash
;
2913 if (syntax
& RE_LIMITED_OPS
)
2916 /* Insert before the previous alternative a jump which
2917 jumps to this alternative if the former fails. */
2918 GET_BUFFER_SPACE (3);
2919 INSERT_JUMP (on_failure_jump
, begalt
, b
+ 6);
2923 /* The alternative before this one has a jump after it
2924 which gets executed if it gets matched. Adjust that
2925 jump so it will jump to this alternative's analogous
2926 jump (put in below, which in turn will jump to the next
2927 (if any) alternative's such jump, etc.). The last such
2928 jump jumps to the correct final destination. A picture:
2934 If we are at `b', then fixup_alt_jump right now points to a
2935 three-byte space after `a'. We'll put in the jump, set
2936 fixup_alt_jump to right after `b', and leave behind three
2937 bytes which we'll fill in when we get to after `c'. */
2941 /* Mark and leave space for a jump after this alternative,
2942 to be filled in later either by next alternative or
2943 when know we're at the end of a series of alternatives. */
2945 GET_BUFFER_SPACE (3);
2954 /* If \{ is a literal. */
2955 if (!(syntax
& RE_INTERVALS
)
2956 /* If we're at `\{' and it's not the open-interval
2958 || (syntax
& RE_NO_BK_BRACES
))
2959 goto normal_backslash
;
2963 /* If got here, then the syntax allows intervals. */
2965 /* At least (most) this many matches must be made. */
2966 int lower_bound
= 0, upper_bound
= -1;
2971 FREE_STACK_RETURN (REG_EBRACE
);
2973 GET_UNSIGNED_NUMBER (lower_bound
);
2976 GET_UNSIGNED_NUMBER (upper_bound
);
2978 /* Interval such as `{1}' => match exactly once. */
2979 upper_bound
= lower_bound
;
2981 if (lower_bound
< 0 || upper_bound
> RE_DUP_MAX
2982 || (upper_bound
>= 0 && lower_bound
> upper_bound
))
2983 FREE_STACK_RETURN (REG_BADBR
);
2985 if (!(syntax
& RE_NO_BK_BRACES
))
2988 FREE_STACK_RETURN (REG_BADBR
);
2994 FREE_STACK_RETURN (REG_BADBR
);
2996 /* We just parsed a valid interval. */
2998 /* If it's invalid to have no preceding re. */
3001 if (syntax
& RE_CONTEXT_INVALID_OPS
)
3002 FREE_STACK_RETURN (REG_BADRPT
);
3003 else if (syntax
& RE_CONTEXT_INDEP_OPS
)
3006 goto unfetch_interval
;
3009 if (upper_bound
== 0)
3010 /* If the upper bound is zero, just drop the sub pattern
3013 else if (lower_bound
== 1 && upper_bound
== 1)
3014 /* Just match it once: nothing to do here. */
3017 /* Otherwise, we have a nontrivial interval. When
3018 we're all done, the pattern will look like:
3019 set_number_at <jump count> <upper bound>
3020 set_number_at <succeed_n count> <lower bound>
3021 succeed_n <after jump addr> <succeed_n count>
3023 jump_n <succeed_n addr> <jump count>
3024 (The upper bound and `jump_n' are omitted if
3025 `upper_bound' is 1, though.) */
3027 { /* If the upper bound is > 1, we need to insert
3028 more at the end of the loop. */
3029 unsigned int nbytes
= (upper_bound
< 0 ? 3
3030 : upper_bound
> 1 ? 5 : 0);
3031 unsigned int startoffset
= 0;
3033 GET_BUFFER_SPACE (20); /* We might use less. */
3035 if (lower_bound
== 0)
3037 /* A succeed_n that starts with 0 is really a
3038 a simple on_failure_jump_loop. */
3039 INSERT_JUMP (on_failure_jump_loop
, laststart
,
3045 /* Initialize lower bound of the `succeed_n', even
3046 though it will be set during matching by its
3047 attendant `set_number_at' (inserted next),
3048 because `re_compile_fastmap' needs to know.
3049 Jump to the `jump_n' we might insert below. */
3050 INSERT_JUMP2 (succeed_n
, laststart
,
3055 /* Code to initialize the lower bound. Insert
3056 before the `succeed_n'. The `5' is the last two
3057 bytes of this `set_number_at', plus 3 bytes of
3058 the following `succeed_n'. */
3059 insert_op2 (set_number_at
, laststart
, 5, lower_bound
, b
);
3064 if (upper_bound
< 0)
3066 /* A negative upper bound stands for infinity,
3067 in which case it degenerates to a plain jump. */
3068 STORE_JUMP (jump
, b
, laststart
+ startoffset
);
3071 else if (upper_bound
> 1)
3072 { /* More than one repetition is allowed, so
3073 append a backward jump to the `succeed_n'
3074 that starts this interval.
3076 When we've reached this during matching,
3077 we'll have matched the interval once, so
3078 jump back only `upper_bound - 1' times. */
3079 STORE_JUMP2 (jump_n
, b
, laststart
+ startoffset
,
3083 /* The location we want to set is the second
3084 parameter of the `jump_n'; that is `b-2' as
3085 an absolute address. `laststart' will be
3086 the `set_number_at' we're about to insert;
3087 `laststart+3' the number to set, the source
3088 for the relative address. But we are
3089 inserting into the middle of the pattern --
3090 so everything is getting moved up by 5.
3091 Conclusion: (b - 2) - (laststart + 3) + 5,
3092 i.e., b - laststart.
3094 We insert this at the beginning of the loop
3095 so that if we fail during matching, we'll
3096 reinitialize the bounds. */
3097 insert_op2 (set_number_at
, laststart
, b
- laststart
,
3098 upper_bound
- 1, b
);
3103 beg_interval
= NULL
;
3108 /* If an invalid interval, match the characters as literals. */
3109 assert (beg_interval
);
3111 beg_interval
= NULL
;
3113 /* normal_char and normal_backslash need `c'. */
3116 if (!(syntax
& RE_NO_BK_BRACES
))
3118 assert (p
> pattern
&& p
[-1] == '\\');
3119 goto normal_backslash
;
3125 /* There is no way to specify the before_dot and after_dot
3126 operators. rms says this is ok. --karl */
3134 BUF_PUSH_2 (syntaxspec
, syntax_spec_code
[c
]);
3140 BUF_PUSH_2 (notsyntaxspec
, syntax_spec_code
[c
]);
3146 BUF_PUSH_2 (categoryspec
, c
);
3152 BUF_PUSH_2 (notcategoryspec
, c
);
3158 if (syntax
& RE_NO_GNU_OPS
)
3161 BUF_PUSH_2 (syntaxspec
, Sword
);
3166 if (syntax
& RE_NO_GNU_OPS
)
3169 BUF_PUSH_2 (notsyntaxspec
, Sword
);
3174 if (syntax
& RE_NO_GNU_OPS
)
3180 if (syntax
& RE_NO_GNU_OPS
)
3186 if (syntax
& RE_NO_GNU_OPS
)
3188 BUF_PUSH (wordbound
);
3192 if (syntax
& RE_NO_GNU_OPS
)
3194 BUF_PUSH (notwordbound
);
3198 if (syntax
& RE_NO_GNU_OPS
)
3204 if (syntax
& RE_NO_GNU_OPS
)
3209 case '1': case '2': case '3': case '4': case '5':
3210 case '6': case '7': case '8': case '9':
3214 if (syntax
& RE_NO_BK_REFS
)
3215 goto normal_backslash
;
3219 /* Can't back reference to a subexpression before its end. */
3220 if (reg
> regnum
|| group_in_compile_stack (compile_stack
, reg
))
3221 FREE_STACK_RETURN (REG_ESUBREG
);
3224 BUF_PUSH_2 (duplicate
, reg
);
3231 if (syntax
& RE_BK_PLUS_QM
)
3234 goto normal_backslash
;
3238 /* You might think it would be useful for \ to mean
3239 not to translate; but if we don't translate it
3240 it will never match anything. */
3241 /* Actually we don't have to translate it now, because
3242 it is anyway translated later. */
3252 /* Expects the character in `c'. */
3254 /* If no exactn currently being built. */
3257 /* If last exactn not at current position. */
3258 || pending_exact
+ *pending_exact
+ 1 != b
3260 /* We have only one byte following the exactn for the count. */
3261 || *pending_exact
>= (1 << BYTEWIDTH
) - MAX_MULTIBYTE_LENGTH
3263 /* If followed by a repetition operator. */
3264 || (p
!= pend
&& (*p
== '*' || *p
== '^'))
3265 || ((syntax
& RE_BK_PLUS_QM
)
3266 ? p
+ 1 < pend
&& *p
== '\\' && (p
[1] == '+' || p
[1] == '?')
3267 : p
!= pend
&& (*p
== '+' || *p
== '?'))
3268 || ((syntax
& RE_INTERVALS
)
3269 && ((syntax
& RE_NO_BK_BRACES
)
3270 ? p
!= pend
&& *p
== '{'
3271 : p
+ 1 < pend
&& p
[0] == '\\' && p
[1] == '{')))
3273 /* Start building a new exactn. */
3277 BUF_PUSH_2 (exactn
, 0);
3278 pending_exact
= b
- 1;
3281 GET_BUFFER_SPACE (MAX_MULTIBYTE_LENGTH
);
3286 MAKE_CHAR_MULTIBYTE (c
);
3288 if (target_multibyte
)
3290 len
= CHAR_STRING (c
, b
);
3295 MAKE_CHAR_UNIBYTE (c
);
3299 (*pending_exact
) += len
;
3304 } /* while p != pend */
3307 /* Through the pattern now. */
3311 if (!COMPILE_STACK_EMPTY
)
3312 FREE_STACK_RETURN (REG_EPAREN
);
3314 /* If we don't want backtracking, force success
3315 the first time we reach the end of the compiled pattern. */
3316 if (syntax
& RE_NO_POSIX_BACKTRACKING
)
3319 free (compile_stack
.stack
);
3321 /* We have succeeded; set the length of the buffer. */
3322 bufp
->used
= b
- bufp
->buffer
;
3325 /* Now the buffer is adjusted for the multibyteness of a target. */
3326 bufp
->multibyte
= bufp
->target_multibyte
;
3332 re_compile_fastmap (bufp
);
3333 DEBUG_PRINT1 ("\nCompiled pattern: \n");
3334 print_compiled_pattern (bufp
);
3339 #ifndef MATCH_MAY_ALLOCATE
3340 /* Initialize the failure stack to the largest possible stack. This
3341 isn't necessary unless we're trying to avoid calling alloca in
3342 the search and match routines. */
3344 int num_regs
= bufp
->re_nsub
+ 1;
3346 if (fail_stack
.size
< re_max_failures
* TYPICAL_FAILURE_SIZE
)
3348 fail_stack
.size
= re_max_failures
* TYPICAL_FAILURE_SIZE
;
3350 if (! fail_stack
.stack
)
3352 = (fail_stack_elt_t
*) malloc (fail_stack
.size
3353 * sizeof (fail_stack_elt_t
));
3356 = (fail_stack_elt_t
*) realloc (fail_stack
.stack
,
3358 * sizeof (fail_stack_elt_t
)));
3361 regex_grow_registers (num_regs
);
3363 #endif /* not MATCH_MAY_ALLOCATE */
3366 } /* regex_compile */
3368 /* Subroutines for `regex_compile'. */
3370 /* Store OP at LOC followed by two-byte integer parameter ARG. */
3373 store_op1 (op
, loc
, arg
)
3378 *loc
= (unsigned char) op
;
3379 STORE_NUMBER (loc
+ 1, arg
);
3383 /* Like `store_op1', but for two two-byte parameters ARG1 and ARG2. */
3386 store_op2 (op
, loc
, arg1
, arg2
)
3391 *loc
= (unsigned char) op
;
3392 STORE_NUMBER (loc
+ 1, arg1
);
3393 STORE_NUMBER (loc
+ 3, arg2
);
3397 /* Copy the bytes from LOC to END to open up three bytes of space at LOC
3398 for OP followed by two-byte integer parameter ARG. */
3401 insert_op1 (op
, loc
, arg
, end
)
3407 register unsigned char *pfrom
= end
;
3408 register unsigned char *pto
= end
+ 3;
3410 while (pfrom
!= loc
)
3413 store_op1 (op
, loc
, arg
);
3417 /* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2. */
3420 insert_op2 (op
, loc
, arg1
, arg2
, end
)
3426 register unsigned char *pfrom
= end
;
3427 register unsigned char *pto
= end
+ 5;
3429 while (pfrom
!= loc
)
3432 store_op2 (op
, loc
, arg1
, arg2
);
3436 /* P points to just after a ^ in PATTERN. Return true if that ^ comes
3437 after an alternative or a begin-subexpression. We assume there is at
3438 least one character before the ^. */
3441 at_begline_loc_p (pattern
, p
, syntax
)
3442 re_char
*pattern
, *p
;
3443 reg_syntax_t syntax
;
3445 re_char
*prev
= p
- 2;
3446 boolean prev_prev_backslash
= prev
> pattern
&& prev
[-1] == '\\';
3449 /* After a subexpression? */
3450 (*prev
== '(' && (syntax
& RE_NO_BK_PARENS
|| prev_prev_backslash
))
3451 /* After an alternative? */
3452 || (*prev
== '|' && (syntax
& RE_NO_BK_VBAR
|| prev_prev_backslash
))
3453 /* After a shy subexpression? */
3454 || ((syntax
& RE_SHY_GROUPS
) && prev
- 2 >= pattern
3455 && prev
[-1] == '?' && prev
[-2] == '('
3456 && (syntax
& RE_NO_BK_PARENS
3457 || (prev
- 3 >= pattern
&& prev
[-3] == '\\')));
3461 /* The dual of at_begline_loc_p. This one is for $. We assume there is
3462 at least one character after the $, i.e., `P < PEND'. */
3465 at_endline_loc_p (p
, pend
, syntax
)
3467 reg_syntax_t syntax
;
3470 boolean next_backslash
= *next
== '\\';
3471 re_char
*next_next
= p
+ 1 < pend
? p
+ 1 : 0;
3474 /* Before a subexpression? */
3475 (syntax
& RE_NO_BK_PARENS
? *next
== ')'
3476 : next_backslash
&& next_next
&& *next_next
== ')')
3477 /* Before an alternative? */
3478 || (syntax
& RE_NO_BK_VBAR
? *next
== '|'
3479 : next_backslash
&& next_next
&& *next_next
== '|');
3483 /* Returns true if REGNUM is in one of COMPILE_STACK's elements and
3484 false if it's not. */
3487 group_in_compile_stack (compile_stack
, regnum
)
3488 compile_stack_type compile_stack
;
3493 for (this_element
= compile_stack
.avail
- 1;
3496 if (compile_stack
.stack
[this_element
].regnum
== regnum
)
3503 If fastmap is non-NULL, go through the pattern and fill fastmap
3504 with all the possible leading chars. If fastmap is NULL, don't
3505 bother filling it up (obviously) and only return whether the
3506 pattern could potentially match the empty string.
3508 Return 1 if p..pend might match the empty string.
3509 Return 0 if p..pend matches at least one char.
3510 Return -1 if fastmap was not updated accurately. */
3513 analyse_first (p
, pend
, fastmap
, multibyte
)
3516 const int multibyte
;
3521 /* If all elements for base leading-codes in fastmap is set, this
3522 flag is set true. */
3523 boolean match_any_multibyte_characters
= false;
3527 /* The loop below works as follows:
3528 - It has a working-list kept in the PATTERN_STACK and which basically
3529 starts by only containing a pointer to the first operation.
3530 - If the opcode we're looking at is a match against some set of
3531 chars, then we add those chars to the fastmap and go on to the
3532 next work element from the worklist (done via `break').
3533 - If the opcode is a control operator on the other hand, we either
3534 ignore it (if it's meaningless at this point, such as `start_memory')
3535 or execute it (if it's a jump). If the jump has several destinations
3536 (i.e. `on_failure_jump'), then we push the other destination onto the
3538 We guarantee termination by ignoring backward jumps (more or less),
3539 so that `p' is monotonically increasing. More to the point, we
3540 never set `p' (or push) anything `<= p1'. */
3544 /* `p1' is used as a marker of how far back a `on_failure_jump'
3545 can go without being ignored. It is normally equal to `p'
3546 (which prevents any backward `on_failure_jump') except right
3547 after a plain `jump', to allow patterns such as:
3550 10: on_failure_jump 3
3551 as used for the *? operator. */
3554 switch (SWITCH_ENUM_CAST ((re_opcode_t
) *p
++))
3561 /* If the first character has to match a backreference, that means
3562 that the group was empty (since it already matched). Since this
3563 is the only case that interests us here, we can assume that the
3564 backreference must match the empty string. */
3569 /* Following are the cases which match a character. These end
3574 /* If multibyte is nonzero, the first byte of each
3575 character is an ASCII or a leading code. Otherwise,
3576 each byte is a character. Thus, this works in both
3583 /* We could put all the chars except for \n (and maybe \0)
3584 but we don't bother since it is generally not worth it. */
3585 if (!fastmap
) break;
3590 if (!fastmap
) break;
3592 /* Chars beyond end of bitmap are possible matches. */
3593 /* In a multibyte case, the bitmap is used only for ASCII
3595 int limit
= multibyte
? 128 : (1 << BYTEWIDTH
);
3597 for (j
= CHARSET_BITMAP_SIZE (&p
[-1]) * BYTEWIDTH
;
3604 if (!fastmap
) break;
3605 not = (re_opcode_t
) *(p
- 1) == charset_not
;
3606 for (j
= CHARSET_BITMAP_SIZE (&p
[-1]) * BYTEWIDTH
- 1, p
++;
3608 if (!!(p
[j
/ BYTEWIDTH
] & (1 << (j
% BYTEWIDTH
))) ^ not)
3611 if ((not && multibyte
)
3612 /* Any leading code can possibly start a character
3613 which doesn't match the specified set of characters. */
3614 || (CHARSET_RANGE_TABLE_EXISTS_P (&p
[-2])
3615 && CHARSET_RANGE_TABLE_BITS (&p
[-2]) != 0))
3616 /* If we can match a character class, we can match
3617 any multibyte characters. */
3619 if (match_any_multibyte_characters
== false)
3621 for (j
= 0x80; j
< (1 << BYTEWIDTH
); j
++)
3623 match_any_multibyte_characters
= true;
3627 else if (!not && CHARSET_RANGE_TABLE_EXISTS_P (&p
[-2])
3628 && match_any_multibyte_characters
== false)
3630 /* Set fastmap[I] to 1 where I is a leading code of each
3631 multibyte characer in the range table. */
3633 unsigned char lc1
, lc2
;
3635 /* Make P points the range table. `+ 2' is to skip flag
3636 bits for a character class. */
3637 p
+= CHARSET_BITMAP_SIZE (&p
[-2]) + 2;
3639 /* Extract the number of ranges in range table into COUNT. */
3640 EXTRACT_NUMBER_AND_INCR (count
, p
);
3641 for (; count
> 0; count
--, p
+= 2 * 3) /* XXX */
3643 /* Extract the start and end of each range. */
3644 EXTRACT_CHARACTER (c
, p
);
3645 lc1
= CHAR_LEADING_CODE (c
);
3647 EXTRACT_CHARACTER (c
, p
);
3648 lc2
= CHAR_LEADING_CODE (c
);
3649 for (j
= lc1
; j
<= lc2
; j
++)
3657 if (!fastmap
) break;
3659 not = (re_opcode_t
)p
[-1] == notsyntaxspec
;
3661 for (j
= 0; j
< (1 << BYTEWIDTH
); j
++)
3662 if ((SYNTAX (j
) == (enum syntaxcode
) k
) ^ not)
3666 /* This match depends on text properties. These end with
3667 aborting optimizations. */
3671 case notcategoryspec
:
3672 if (!fastmap
) break;
3673 not = (re_opcode_t
)p
[-1] == notcategoryspec
;
3675 for (j
= (multibyte
? 127 : (1 << BYTEWIDTH
)); j
>= 0; j
--)
3676 if ((CHAR_HAS_CATEGORY (j
, k
)) ^ not)
3681 /* Any character set can possibly contain a character
3682 whose category is K (or not). */
3683 if (match_any_multibyte_characters
== false)
3685 for (j
= 0x80; j
< (1 << BYTEWIDTH
); j
++)
3687 match_any_multibyte_characters
= true;
3692 /* All cases after this match the empty string. These end with
3712 EXTRACT_NUMBER_AND_INCR (j
, p
);
3714 /* Backward jumps can only go back to code that we've already
3715 visited. `re_compile' should make sure this is true. */
3718 switch (SWITCH_ENUM_CAST ((re_opcode_t
) *p
))
3720 case on_failure_jump
:
3721 case on_failure_keep_string_jump
:
3722 case on_failure_jump_loop
:
3723 case on_failure_jump_nastyloop
:
3724 case on_failure_jump_smart
:
3730 /* Keep `p1' to allow the `on_failure_jump' we are jumping to
3731 to jump back to "just after here". */
3734 case on_failure_jump
:
3735 case on_failure_keep_string_jump
:
3736 case on_failure_jump_nastyloop
:
3737 case on_failure_jump_loop
:
3738 case on_failure_jump_smart
:
3739 EXTRACT_NUMBER_AND_INCR (j
, p
);
3741 ; /* Backward jump to be ignored. */
3743 { /* We have to look down both arms.
3744 We first go down the "straight" path so as to minimize
3745 stack usage when going through alternatives. */
3746 int r
= analyse_first (p
, pend
, fastmap
, multibyte
);
3754 /* This code simply does not properly handle forward jump_n. */
3755 DEBUG_STATEMENT (EXTRACT_NUMBER (j
, p
); assert (j
< 0));
3757 /* jump_n can either jump or fall through. The (backward) jump
3758 case has already been handled, so we only need to look at the
3759 fallthrough case. */
3763 /* If N == 0, it should be an on_failure_jump_loop instead. */
3764 DEBUG_STATEMENT (EXTRACT_NUMBER (j
, p
+ 2); assert (j
> 0));
3766 /* We only care about one iteration of the loop, so we don't
3767 need to consider the case where this behaves like an
3784 abort (); /* We have listed all the cases. */
3787 /* Getting here means we have found the possible starting
3788 characters for one path of the pattern -- and that the empty
3789 string does not match. We need not follow this path further. */
3793 /* We reached the end without matching anything. */
3796 } /* analyse_first */
3798 /* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
3799 BUFP. A fastmap records which of the (1 << BYTEWIDTH) possible
3800 characters can start a string that matches the pattern. This fastmap
3801 is used by re_search to skip quickly over impossible starting points.
3803 Character codes above (1 << BYTEWIDTH) are not represented in the
3804 fastmap, but the leading codes are represented. Thus, the fastmap
3805 indicates which character sets could start a match.
3807 The caller must supply the address of a (1 << BYTEWIDTH)-byte data
3808 area as BUFP->fastmap.
3810 We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in
3813 Returns 0 if we succeed, -2 if an internal error. */
3816 re_compile_fastmap (bufp
)
3817 struct re_pattern_buffer
*bufp
;
3819 char *fastmap
= bufp
->fastmap
;
3822 assert (fastmap
&& bufp
->buffer
);
3824 bzero (fastmap
, 1 << BYTEWIDTH
); /* Assume nothing's valid. */
3825 bufp
->fastmap_accurate
= 1; /* It will be when we're done. */
3827 analysis
= analyse_first (bufp
->buffer
, bufp
->buffer
+ bufp
->used
,
3830 /* The compiled pattern buffer is always
3831 setup for multibyte characters. */
3837 bufp
->can_be_null
= (analysis
!= 0);
3839 } /* re_compile_fastmap */
3841 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
3842 ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
3843 this memory for recording register information. STARTS and ENDS
3844 must be allocated using the malloc library routine, and must each
3845 be at least NUM_REGS * sizeof (regoff_t) bytes long.
3847 If NUM_REGS == 0, then subsequent matches should allocate their own
3850 Unless this function is called, the first search or match using
3851 PATTERN_BUFFER will allocate its own register data, without
3852 freeing the old data. */
3855 re_set_registers (bufp
, regs
, num_regs
, starts
, ends
)
3856 struct re_pattern_buffer
*bufp
;
3857 struct re_registers
*regs
;
3859 regoff_t
*starts
, *ends
;
3863 bufp
->regs_allocated
= REGS_REALLOCATE
;
3864 regs
->num_regs
= num_regs
;
3865 regs
->start
= starts
;
3870 bufp
->regs_allocated
= REGS_UNALLOCATED
;
3872 regs
->start
= regs
->end
= (regoff_t
*) 0;
3875 WEAK_ALIAS (__re_set_registers
, re_set_registers
)
3877 /* Searching routines. */
3879 /* Like re_search_2, below, but only one string is specified, and
3880 doesn't let you say where to stop matching. */
3883 re_search (bufp
, string
, size
, startpos
, range
, regs
)
3884 struct re_pattern_buffer
*bufp
;
3886 int size
, startpos
, range
;
3887 struct re_registers
*regs
;
3889 return re_search_2 (bufp
, NULL
, 0, string
, size
, startpos
, range
,
3892 WEAK_ALIAS (__re_search
, re_search
)
3894 /* End address of virtual concatenation of string. */
3895 #define STOP_ADDR_VSTRING(P) \
3896 (((P) >= size1 ? string2 + size2 : string1 + size1))
3898 /* Address of POS in the concatenation of virtual string. */
3899 #define POS_ADDR_VSTRING(POS) \
3900 (((POS) >= size1 ? string2 - size1 : string1) + (POS))
3902 /* Using the compiled pattern in BUFP->buffer, first tries to match the
3903 virtual concatenation of STRING1 and STRING2, starting first at index
3904 STARTPOS, then at STARTPOS + 1, and so on.
3906 STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
3908 RANGE is how far to scan while trying to match. RANGE = 0 means try
3909 only at STARTPOS; in general, the last start tried is STARTPOS +
3912 In REGS, return the indices of the virtual concatenation of STRING1
3913 and STRING2 that matched the entire BUFP->buffer and its contained
3916 Do not consider matching one past the index STOP in the virtual
3917 concatenation of STRING1 and STRING2.
3919 We return either the position in the strings at which the match was
3920 found, -1 if no match, or -2 if error (such as failure
3924 re_search_2 (bufp
, str1
, size1
, str2
, size2
, startpos
, range
, regs
, stop
)
3925 struct re_pattern_buffer
*bufp
;
3926 const char *str1
, *str2
;
3930 struct re_registers
*regs
;
3934 re_char
*string1
= (re_char
*) str1
;
3935 re_char
*string2
= (re_char
*) str2
;
3936 register char *fastmap
= bufp
->fastmap
;
3937 register RE_TRANSLATE_TYPE translate
= bufp
->translate
;
3938 int total_size
= size1
+ size2
;
3939 int endpos
= startpos
+ range
;
3940 boolean anchored_start
;
3941 /* Nonzero if BUFP is setup for multibyte characters. We are sure
3942 that it is the same as RE_TARGET_MULTIBYTE_P (bufp). */
3943 const boolean multibyte
= RE_MULTIBYTE_P (bufp
);
3945 /* Check for out-of-range STARTPOS. */
3946 if (startpos
< 0 || startpos
> total_size
)
3949 /* Fix up RANGE if it might eventually take us outside
3950 the virtual concatenation of STRING1 and STRING2.
3951 Make sure we won't move STARTPOS below 0 or above TOTAL_SIZE. */
3953 range
= 0 - startpos
;
3954 else if (endpos
> total_size
)
3955 range
= total_size
- startpos
;
3957 /* If the search isn't to be a backwards one, don't waste time in a
3958 search for a pattern anchored at beginning of buffer. */
3959 if (bufp
->used
> 0 && (re_opcode_t
) bufp
->buffer
[0] == begbuf
&& range
> 0)
3968 /* In a forward search for something that starts with \=.
3969 don't keep searching past point. */
3970 if (bufp
->used
> 0 && (re_opcode_t
) bufp
->buffer
[0] == at_dot
&& range
> 0)
3972 range
= PT_BYTE
- BEGV_BYTE
- startpos
;
3978 /* Update the fastmap now if not correct already. */
3979 if (fastmap
&& !bufp
->fastmap_accurate
)
3980 re_compile_fastmap (bufp
);
3982 /* See whether the pattern is anchored. */
3983 anchored_start
= (bufp
->buffer
[0] == begline
);
3986 gl_state
.object
= re_match_object
;
3988 int charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (POS_AS_IN_BUFFER (startpos
));
3990 SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object
, charpos
, 1);
3994 /* Loop through the string, looking for a place to start matching. */
3997 /* If the pattern is anchored,
3998 skip quickly past places we cannot match.
3999 We don't bother to treat startpos == 0 specially
4000 because that case doesn't repeat. */
4001 if (anchored_start
&& startpos
> 0)
4003 if (! ((startpos
<= size1
? string1
[startpos
- 1]
4004 : string2
[startpos
- size1
- 1])
4009 /* If a fastmap is supplied, skip quickly over characters that
4010 cannot be the start of a match. If the pattern can match the
4011 null string, however, we don't need to skip characters; we want
4012 the first null string. */
4013 if (fastmap
&& startpos
< total_size
&& !bufp
->can_be_null
)
4015 register re_char
*d
;
4016 register re_wchar_t buf_ch
;
4018 d
= POS_ADDR_VSTRING (startpos
);
4020 if (range
> 0) /* Searching forwards. */
4022 register int lim
= 0;
4025 if (startpos
< size1
&& startpos
+ range
>= size1
)
4026 lim
= range
- (size1
- startpos
);
4028 /* Written out as an if-else to avoid testing `translate'
4030 if (RE_TRANSLATE_P (translate
))
4037 buf_ch
= STRING_CHAR_AND_LENGTH (d
, range
- lim
,
4039 buf_ch
= RE_TRANSLATE (translate
, buf_ch
);
4040 if (fastmap
[CHAR_LEADING_CODE (buf_ch
)])
4042 range
-= buf_charlen
;
4050 MAKE_CHAR_MULTIBYTE (buf_ch
);
4052 buf_ch
= RE_TRANSLATE (translate
, buf_ch
);
4054 MAKE_CHAR_UNIBYTE (buf_ch
);
4056 if (fastmap
[buf_ch
])
4069 buf_ch
= STRING_CHAR_AND_LENGTH (d
, range
- lim
,
4071 if (fastmap
[CHAR_LEADING_CODE (buf_ch
)])
4073 range
-= buf_charlen
;
4077 while (range
> lim
&& !fastmap
[*d
])
4083 startpos
+= irange
- range
;
4085 else /* Searching backwards. */
4087 int room
= (startpos
>= size1
4088 ? size2
+ size1
- startpos
4089 : size1
- startpos
);
4093 buf_ch
= STRING_CHAR (d
, room
);
4094 buf_ch
= TRANSLATE (buf_ch
);
4095 if (! fastmap
[CHAR_LEADING_CODE (buf_ch
)])
4100 if (! fastmap
[TRANSLATE (*d
)])
4106 /* If can't match the null string, and that's all we have left, fail. */
4107 if (range
>= 0 && startpos
== total_size
&& fastmap
4108 && !bufp
->can_be_null
)
4111 val
= re_match_2_internal (bufp
, string1
, size1
, string2
, size2
,
4112 startpos
, regs
, stop
);
4113 #ifndef REGEX_MALLOC
4130 /* Update STARTPOS to the next character boundary. */
4133 re_char
*p
= POS_ADDR_VSTRING (startpos
);
4134 re_char
*pend
= STOP_ADDR_VSTRING (startpos
);
4135 int len
= MULTIBYTE_FORM_LENGTH (p
, pend
- p
);
4153 /* Update STARTPOS to the previous character boundary. */
4156 re_char
*p
= POS_ADDR_VSTRING (startpos
);
4159 /* Find the head of multibyte form. */
4160 while (!CHAR_HEAD_P (*p
))
4171 WEAK_ALIAS (__re_search_2
, re_search_2
)
4173 /* Declarations and macros for re_match_2. */
4175 static int bcmp_translate
_RE_ARGS((re_char
*s1
, re_char
*s2
,
4177 RE_TRANSLATE_TYPE translate
,
4178 const int multibyte
));
4180 /* This converts PTR, a pointer into one of the search strings `string1'
4181 and `string2' into an offset from the beginning of that string. */
4182 #define POINTER_TO_OFFSET(ptr) \
4183 (FIRST_STRING_P (ptr) \
4184 ? ((regoff_t) ((ptr) - string1)) \
4185 : ((regoff_t) ((ptr) - string2 + size1)))
4187 /* Call before fetching a character with *d. This switches over to
4188 string2 if necessary.
4189 Check re_match_2_internal for a discussion of why end_match_2 might
4190 not be within string2 (but be equal to end_match_1 instead). */
4191 #define PREFETCH() \
4194 /* End of string2 => fail. */ \
4195 if (dend == end_match_2) \
4197 /* End of string1 => advance to string2. */ \
4199 dend = end_match_2; \
4202 /* Call before fetching a char with *d if you already checked other limits.
4203 This is meant for use in lookahead operations like wordend, etc..
4204 where we might need to look at parts of the string that might be
4205 outside of the LIMITs (i.e past `stop'). */
4206 #define PREFETCH_NOLIMIT() \
4210 dend = end_match_2; \
4213 /* Test if at very beginning or at very end of the virtual concatenation
4214 of `string1' and `string2'. If only one string, it's `string2'. */
4215 #define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2)
4216 #define AT_STRINGS_END(d) ((d) == end2)
4219 /* Test if D points to a character which is word-constituent. We have
4220 two special cases to check for: if past the end of string1, look at
4221 the first character in string2; and if before the beginning of
4222 string2, look at the last character in string1. */
4223 #define WORDCHAR_P(d) \
4224 (SYNTAX ((d) == end1 ? *string2 \
4225 : (d) == string2 - 1 ? *(end1 - 1) : *(d)) \
4228 /* Disabled due to a compiler bug -- see comment at case wordbound */
4230 /* The comment at case wordbound is following one, but we don't use
4231 AT_WORD_BOUNDARY anymore to support multibyte form.
4233 The DEC Alpha C compiler 3.x generates incorrect code for the
4234 test WORDCHAR_P (d - 1) != WORDCHAR_P (d) in the expansion of
4235 AT_WORD_BOUNDARY, so this code is disabled. Expanding the
4236 macro and introducing temporary variables works around the bug. */
4239 /* Test if the character before D and the one at D differ with respect
4240 to being word-constituent. */
4241 #define AT_WORD_BOUNDARY(d) \
4242 (AT_STRINGS_BEG (d) || AT_STRINGS_END (d) \
4243 || WORDCHAR_P (d - 1) != WORDCHAR_P (d))
4246 /* Free everything we malloc. */
4247 #ifdef MATCH_MAY_ALLOCATE
4248 # define FREE_VAR(var) if (var) { REGEX_FREE (var); var = NULL; } else
4249 # define FREE_VARIABLES() \
4251 REGEX_FREE_STACK (fail_stack.stack); \
4252 FREE_VAR (regstart); \
4253 FREE_VAR (regend); \
4254 FREE_VAR (best_regstart); \
4255 FREE_VAR (best_regend); \
4258 # define FREE_VARIABLES() ((void)0) /* Do nothing! But inhibit gcc warning. */
4259 #endif /* not MATCH_MAY_ALLOCATE */
4262 /* Optimization routines. */
4264 /* If the operation is a match against one or more chars,
4265 return a pointer to the next operation, else return NULL. */
4270 switch (SWITCH_ENUM_CAST (*p
++))
4281 if (CHARSET_RANGE_TABLE_EXISTS_P (p
- 1))
4284 p
= CHARSET_RANGE_TABLE (p
- 1);
4285 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
4286 p
= CHARSET_RANGE_TABLE_END (p
, mcnt
);
4289 p
+= 1 + CHARSET_BITMAP_SIZE (p
- 1);
4296 case notcategoryspec
:
4308 /* Jump over non-matching operations. */
4309 static unsigned char *
4310 skip_noops (p
, pend
)
4311 unsigned char *p
, *pend
;
4316 switch (SWITCH_ENUM_CAST ((re_opcode_t
) *p
))
4325 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
4336 /* Non-zero if "p1 matches something" implies "p2 fails". */
4338 mutually_exclusive_p (bufp
, p1
, p2
)
4339 struct re_pattern_buffer
*bufp
;
4340 unsigned char *p1
, *p2
;
4343 const boolean multibyte
= RE_MULTIBYTE_P (bufp
);
4344 unsigned char *pend
= bufp
->buffer
+ bufp
->used
;
4346 assert (p1
>= bufp
->buffer
&& p1
< pend
4347 && p2
>= bufp
->buffer
&& p2
<= pend
);
4349 /* Skip over open/close-group commands.
4350 If what follows this loop is a ...+ construct,
4351 look at what begins its body, since we will have to
4352 match at least one of that. */
4353 p2
= skip_noops (p2
, pend
);
4354 /* The same skip can be done for p1, except that this function
4355 is only used in the case where p1 is a simple match operator. */
4356 /* p1 = skip_noops (p1, pend); */
4358 assert (p1
>= bufp
->buffer
&& p1
< pend
4359 && p2
>= bufp
->buffer
&& p2
<= pend
);
4361 op2
= p2
== pend
? succeed
: *p2
;
4363 switch (SWITCH_ENUM_CAST (op2
))
4367 /* If we're at the end of the pattern, we can change. */
4368 if (skip_one_char (p1
))
4370 DEBUG_PRINT1 (" End of pattern: fast loop.\n");
4378 register re_wchar_t c
4379 = (re_opcode_t
) *p2
== endline
? '\n'
4380 : RE_STRING_CHAR (p2
+ 2, pend
- p2
- 2);
4382 if ((re_opcode_t
) *p1
== exactn
)
4384 if (c
!= RE_STRING_CHAR (p1
+ 2, pend
- p1
- 2))
4386 DEBUG_PRINT3 (" '%c' != '%c' => fast loop.\n", c
, p1
[2]);
4391 else if ((re_opcode_t
) *p1
== charset
4392 || (re_opcode_t
) *p1
== charset_not
)
4394 int not = (re_opcode_t
) *p1
== charset_not
;
4396 /* Test if C is listed in charset (or charset_not)
4398 if (! multibyte
|| IS_REAL_ASCII (c
))
4400 if (c
< CHARSET_BITMAP_SIZE (p1
) * BYTEWIDTH
4401 && p1
[2 + c
/ BYTEWIDTH
] & (1 << (c
% BYTEWIDTH
)))
4404 else if (CHARSET_RANGE_TABLE_EXISTS_P (p1
))
4405 CHARSET_LOOKUP_RANGE_TABLE (not, c
, p1
);
4407 /* `not' is equal to 1 if c would match, which means
4408 that we can't change to pop_failure_jump. */
4411 DEBUG_PRINT1 (" No match => fast loop.\n");
4415 else if ((re_opcode_t
) *p1
== anychar
4418 DEBUG_PRINT1 (" . != \\n => fast loop.\n");
4426 if ((re_opcode_t
) *p1
== exactn
)
4427 /* Reuse the code above. */
4428 return mutually_exclusive_p (bufp
, p2
, p1
);
4430 /* It is hard to list up all the character in charset
4431 P2 if it includes multibyte character. Give up in
4433 else if (!multibyte
|| !CHARSET_RANGE_TABLE_EXISTS_P (p2
))
4435 /* Now, we are sure that P2 has no range table.
4436 So, for the size of bitmap in P2, `p2[1]' is
4437 enough. But P1 may have range table, so the
4438 size of bitmap table of P1 is extracted by
4439 using macro `CHARSET_BITMAP_SIZE'.
4441 In a multibyte case, we know that all the character
4442 listed in P2 is ASCII. In a unibyte case, P1 has only a
4443 bitmap table. So, in both cases, it is enough to test
4444 only the bitmap table of P1. */
4446 if ((re_opcode_t
) *p1
== charset
)
4449 /* We win if the charset inside the loop
4450 has no overlap with the one after the loop. */
4453 && idx
< CHARSET_BITMAP_SIZE (p1
));
4455 if ((p2
[2 + idx
] & p1
[2 + idx
]) != 0)
4459 || idx
== CHARSET_BITMAP_SIZE (p1
))
4461 DEBUG_PRINT1 (" No match => fast loop.\n");
4465 else if ((re_opcode_t
) *p1
== charset_not
)
4468 /* We win if the charset_not inside the loop lists
4469 every character listed in the charset after. */
4470 for (idx
= 0; idx
< (int) p2
[1]; idx
++)
4471 if (! (p2
[2 + idx
] == 0
4472 || (idx
< CHARSET_BITMAP_SIZE (p1
)
4473 && ((p2
[2 + idx
] & ~ p1
[2 + idx
]) == 0))))
4478 DEBUG_PRINT1 (" No match => fast loop.\n");
4487 switch (SWITCH_ENUM_CAST (*p1
))
4491 /* Reuse the code above. */
4492 return mutually_exclusive_p (bufp
, p2
, p1
);
4494 /* When we have two charset_not, it's very unlikely that
4495 they don't overlap. The union of the two sets of excluded
4496 chars should cover all possible chars, which, as a matter of
4497 fact, is virtually impossible in multibyte buffers. */
4504 return ((re_opcode_t
) *p1
== syntaxspec
4505 && p1
[1] == (op2
== wordend
? Sword
: p2
[1]));
4509 return ((re_opcode_t
) *p1
== notsyntaxspec
4510 && p1
[1] == (op2
== wordend
? Sword
: p2
[1]));
4513 return (((re_opcode_t
) *p1
== notsyntaxspec
4514 || (re_opcode_t
) *p1
== syntaxspec
)
4519 return ((re_opcode_t
) *p1
== notcategoryspec
&& p1
[1] == p2
[1]);
4520 case notcategoryspec
:
4521 return ((re_opcode_t
) *p1
== categoryspec
&& p1
[1] == p2
[1]);
4533 /* Matching routines. */
4535 #ifndef emacs /* Emacs never uses this. */
4536 /* re_match is like re_match_2 except it takes only a single string. */
4539 re_match (bufp
, string
, size
, pos
, regs
)
4540 struct re_pattern_buffer
*bufp
;
4543 struct re_registers
*regs
;
4545 int result
= re_match_2_internal (bufp
, NULL
, 0, (re_char
*) string
, size
,
4547 # if defined C_ALLOCA && !defined REGEX_MALLOC
4552 WEAK_ALIAS (__re_match
, re_match
)
4553 #endif /* not emacs */
4556 /* In Emacs, this is the string or buffer in which we
4557 are matching. It is used for looking up syntax properties. */
4558 Lisp_Object re_match_object
;
4561 /* re_match_2 matches the compiled pattern in BUFP against the
4562 the (virtual) concatenation of STRING1 and STRING2 (of length SIZE1
4563 and SIZE2, respectively). We start matching at POS, and stop
4566 If REGS is non-null and the `no_sub' field of BUFP is nonzero, we
4567 store offsets for the substring each group matched in REGS. See the
4568 documentation for exactly how many groups we fill.
4570 We return -1 if no match, -2 if an internal error (such as the
4571 failure stack overflowing). Otherwise, we return the length of the
4572 matched substring. */
4575 re_match_2 (bufp
, string1
, size1
, string2
, size2
, pos
, regs
, stop
)
4576 struct re_pattern_buffer
*bufp
;
4577 const char *string1
, *string2
;
4580 struct re_registers
*regs
;
4587 gl_state
.object
= re_match_object
;
4588 charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (POS_AS_IN_BUFFER (pos
));
4589 SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object
, charpos
, 1);
4592 result
= re_match_2_internal (bufp
, (re_char
*) string1
, size1
,
4593 (re_char
*) string2
, size2
,
4595 #if defined C_ALLOCA && !defined REGEX_MALLOC
4600 WEAK_ALIAS (__re_match_2
, re_match_2
)
4603 #define TRANSLATE_VIA_MULTIBYTE(c) \
4606 (c) = TRANSLATE (c); \
4609 MAKE_CHAR_MULTIBYTE (c); \
4610 (c) = TRANSLATE (c); \
4611 MAKE_CHAR_UNIBYTE (c); \
4616 #define TRANSLATE_VIA_MULTIBYTE(c) ((c) = TRANSLATE (c))
4620 /* This is a separate function so that we can force an alloca cleanup
4623 re_match_2_internal (bufp
, string1
, size1
, string2
, size2
, pos
, regs
, stop
)
4624 struct re_pattern_buffer
*bufp
;
4625 re_char
*string1
, *string2
;
4628 struct re_registers
*regs
;
4631 /* General temporaries. */
4636 /* Just past the end of the corresponding string. */
4637 re_char
*end1
, *end2
;
4639 /* Pointers into string1 and string2, just past the last characters in
4640 each to consider matching. */
4641 re_char
*end_match_1
, *end_match_2
;
4643 /* Where we are in the data, and the end of the current string. */
4646 /* Used sometimes to remember where we were before starting matching
4647 an operator so that we can go back in case of failure. This "atomic"
4648 behavior of matching opcodes is indispensable to the correctness
4649 of the on_failure_keep_string_jump optimization. */
4652 /* Where we are in the pattern, and the end of the pattern. */
4653 re_char
*p
= bufp
->buffer
;
4654 re_char
*pend
= p
+ bufp
->used
;
4656 /* We use this to map every character in the string. */
4657 RE_TRANSLATE_TYPE translate
= bufp
->translate
;
4659 /* Nonzero if BUFP is setup for multibyte characters. We are sure
4660 that it is the same as RE_TARGET_MULTIBYTE_P (bufp). */
4661 const boolean multibyte
= RE_MULTIBYTE_P (bufp
);
4663 /* Failure point stack. Each place that can handle a failure further
4664 down the line pushes a failure point on this stack. It consists of
4665 regstart, and regend for all registers corresponding to
4666 the subexpressions we're currently inside, plus the number of such
4667 registers, and, finally, two char *'s. The first char * is where
4668 to resume scanning the pattern; the second one is where to resume
4669 scanning the strings. */
4670 #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
4671 fail_stack_type fail_stack
;
4674 unsigned nfailure_points_pushed
= 0, nfailure_points_popped
= 0;
4677 #if defined REL_ALLOC && defined REGEX_MALLOC
4678 /* This holds the pointer to the failure stack, when
4679 it is allocated relocatably. */
4680 fail_stack_elt_t
*failure_stack_ptr
;
4683 /* We fill all the registers internally, independent of what we
4684 return, for use in backreferences. The number here includes
4685 an element for register zero. */
4686 size_t num_regs
= bufp
->re_nsub
+ 1;
4688 /* Information on the contents of registers. These are pointers into
4689 the input strings; they record just what was matched (on this
4690 attempt) by a subexpression part of the pattern, that is, the
4691 regnum-th regstart pointer points to where in the pattern we began
4692 matching and the regnum-th regend points to right after where we
4693 stopped matching the regnum-th subexpression. (The zeroth register
4694 keeps track of what the whole pattern matches.) */
4695 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
4696 re_char
**regstart
, **regend
;
4699 /* The following record the register info as found in the above
4700 variables when we find a match better than any we've seen before.
4701 This happens as we backtrack through the failure points, which in
4702 turn happens only if we have not yet matched the entire string. */
4703 unsigned best_regs_set
= false;
4704 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
4705 re_char
**best_regstart
, **best_regend
;
4708 /* Logically, this is `best_regend[0]'. But we don't want to have to
4709 allocate space for that if we're not allocating space for anything
4710 else (see below). Also, we never need info about register 0 for
4711 any of the other register vectors, and it seems rather a kludge to
4712 treat `best_regend' differently than the rest. So we keep track of
4713 the end of the best match so far in a separate variable. We
4714 initialize this to NULL so that when we backtrack the first time
4715 and need to test it, it's not garbage. */
4716 re_char
*match_end
= NULL
;
4719 /* Counts the total number of registers pushed. */
4720 unsigned num_regs_pushed
= 0;
4723 DEBUG_PRINT1 ("\n\nEntering re_match_2.\n");
4727 #ifdef MATCH_MAY_ALLOCATE
4728 /* Do not bother to initialize all the register variables if there are
4729 no groups in the pattern, as it takes a fair amount of time. If
4730 there are groups, we include space for register 0 (the whole
4731 pattern), even though we never use it, since it simplifies the
4732 array indexing. We should fix this. */
4735 regstart
= REGEX_TALLOC (num_regs
, re_char
*);
4736 regend
= REGEX_TALLOC (num_regs
, re_char
*);
4737 best_regstart
= REGEX_TALLOC (num_regs
, re_char
*);
4738 best_regend
= REGEX_TALLOC (num_regs
, re_char
*);
4740 if (!(regstart
&& regend
&& best_regstart
&& best_regend
))
4748 /* We must initialize all our variables to NULL, so that
4749 `FREE_VARIABLES' doesn't try to free them. */
4750 regstart
= regend
= best_regstart
= best_regend
= NULL
;
4752 #endif /* MATCH_MAY_ALLOCATE */
4754 /* The starting position is bogus. */
4755 if (pos
< 0 || pos
> size1
+ size2
)
4761 /* Initialize subexpression text positions to -1 to mark ones that no
4762 start_memory/stop_memory has been seen for. Also initialize the
4763 register information struct. */
4764 for (reg
= 1; reg
< num_regs
; reg
++)
4765 regstart
[reg
] = regend
[reg
] = NULL
;
4767 /* We move `string1' into `string2' if the latter's empty -- but not if
4768 `string1' is null. */
4769 if (size2
== 0 && string1
!= NULL
)
4776 end1
= string1
+ size1
;
4777 end2
= string2
+ size2
;
4779 /* `p' scans through the pattern as `d' scans through the data.
4780 `dend' is the end of the input string that `d' points within. `d'
4781 is advanced into the following input string whenever necessary, but
4782 this happens before fetching; therefore, at the beginning of the
4783 loop, `d' can be pointing at the end of a string, but it cannot
4787 /* Only match within string2. */
4788 d
= string2
+ pos
- size1
;
4789 dend
= end_match_2
= string2
+ stop
- size1
;
4790 end_match_1
= end1
; /* Just to give it a value. */
4796 /* Only match within string1. */
4797 end_match_1
= string1
+ stop
;
4799 When we reach end_match_1, PREFETCH normally switches to string2.
4800 But in the present case, this means that just doing a PREFETCH
4801 makes us jump from `stop' to `gap' within the string.
4802 What we really want here is for the search to stop as
4803 soon as we hit end_match_1. That's why we set end_match_2
4804 to end_match_1 (since PREFETCH fails as soon as we hit
4806 end_match_2
= end_match_1
;
4809 { /* It's important to use this code when stop == size so that
4810 moving `d' from end1 to string2 will not prevent the d == dend
4811 check from catching the end of string. */
4813 end_match_2
= string2
+ stop
- size1
;
4819 DEBUG_PRINT1 ("The compiled pattern is: ");
4820 DEBUG_PRINT_COMPILED_PATTERN (bufp
, p
, pend
);
4821 DEBUG_PRINT1 ("The string to match is: `");
4822 DEBUG_PRINT_DOUBLE_STRING (d
, string1
, size1
, string2
, size2
);
4823 DEBUG_PRINT1 ("'\n");
4825 /* This loops over pattern commands. It exits by returning from the
4826 function if the match is complete, or it drops through if the match
4827 fails at this starting point in the input data. */
4830 DEBUG_PRINT2 ("\n%p: ", p
);
4833 { /* End of pattern means we might have succeeded. */
4834 DEBUG_PRINT1 ("end of pattern ... ");
4836 /* If we haven't matched the entire string, and we want the
4837 longest match, try backtracking. */
4838 if (d
!= end_match_2
)
4840 /* 1 if this match ends in the same string (string1 or string2)
4841 as the best previous match. */
4842 boolean same_str_p
= (FIRST_STRING_P (match_end
)
4843 == FIRST_STRING_P (d
));
4844 /* 1 if this match is the best seen so far. */
4845 boolean best_match_p
;
4847 /* AIX compiler got confused when this was combined
4848 with the previous declaration. */
4850 best_match_p
= d
> match_end
;
4852 best_match_p
= !FIRST_STRING_P (d
);
4854 DEBUG_PRINT1 ("backtracking.\n");
4856 if (!FAIL_STACK_EMPTY ())
4857 { /* More failure points to try. */
4859 /* If exceeds best match so far, save it. */
4860 if (!best_regs_set
|| best_match_p
)
4862 best_regs_set
= true;
4865 DEBUG_PRINT1 ("\nSAVING match as best so far.\n");
4867 for (reg
= 1; reg
< num_regs
; reg
++)
4869 best_regstart
[reg
] = regstart
[reg
];
4870 best_regend
[reg
] = regend
[reg
];
4876 /* If no failure points, don't restore garbage. And if
4877 last match is real best match, don't restore second
4879 else if (best_regs_set
&& !best_match_p
)
4882 /* Restore best match. It may happen that `dend ==
4883 end_match_1' while the restored d is in string2.
4884 For example, the pattern `x.*y.*z' against the
4885 strings `x-' and `y-z-', if the two strings are
4886 not consecutive in memory. */
4887 DEBUG_PRINT1 ("Restoring best registers.\n");
4890 dend
= ((d
>= string1
&& d
<= end1
)
4891 ? end_match_1
: end_match_2
);
4893 for (reg
= 1; reg
< num_regs
; reg
++)
4895 regstart
[reg
] = best_regstart
[reg
];
4896 regend
[reg
] = best_regend
[reg
];
4899 } /* d != end_match_2 */
4902 DEBUG_PRINT1 ("Accepting match.\n");
4904 /* If caller wants register contents data back, do it. */
4905 if (regs
&& !bufp
->no_sub
)
4907 /* Have the register data arrays been allocated? */
4908 if (bufp
->regs_allocated
== REGS_UNALLOCATED
)
4909 { /* No. So allocate them with malloc. We need one
4910 extra element beyond `num_regs' for the `-1' marker
4912 regs
->num_regs
= MAX (RE_NREGS
, num_regs
+ 1);
4913 regs
->start
= TALLOC (regs
->num_regs
, regoff_t
);
4914 regs
->end
= TALLOC (regs
->num_regs
, regoff_t
);
4915 if (regs
->start
== NULL
|| regs
->end
== NULL
)
4920 bufp
->regs_allocated
= REGS_REALLOCATE
;
4922 else if (bufp
->regs_allocated
== REGS_REALLOCATE
)
4923 { /* Yes. If we need more elements than were already
4924 allocated, reallocate them. If we need fewer, just
4926 if (regs
->num_regs
< num_regs
+ 1)
4928 regs
->num_regs
= num_regs
+ 1;
4929 RETALLOC (regs
->start
, regs
->num_regs
, regoff_t
);
4930 RETALLOC (regs
->end
, regs
->num_regs
, regoff_t
);
4931 if (regs
->start
== NULL
|| regs
->end
== NULL
)
4940 /* These braces fend off a "empty body in an else-statement"
4941 warning under GCC when assert expands to nothing. */
4942 assert (bufp
->regs_allocated
== REGS_FIXED
);
4945 /* Convert the pointer data in `regstart' and `regend' to
4946 indices. Register zero has to be set differently,
4947 since we haven't kept track of any info for it. */
4948 if (regs
->num_regs
> 0)
4950 regs
->start
[0] = pos
;
4951 regs
->end
[0] = POINTER_TO_OFFSET (d
);
4954 /* Go through the first `min (num_regs, regs->num_regs)'
4955 registers, since that is all we initialized. */
4956 for (reg
= 1; reg
< MIN (num_regs
, regs
->num_regs
); reg
++)
4958 if (REG_UNSET (regstart
[reg
]) || REG_UNSET (regend
[reg
]))
4959 regs
->start
[reg
] = regs
->end
[reg
] = -1;
4963 = (regoff_t
) POINTER_TO_OFFSET (regstart
[reg
]);
4965 = (regoff_t
) POINTER_TO_OFFSET (regend
[reg
]);
4969 /* If the regs structure we return has more elements than
4970 were in the pattern, set the extra elements to -1. If
4971 we (re)allocated the registers, this is the case,
4972 because we always allocate enough to have at least one
4974 for (reg
= num_regs
; reg
< regs
->num_regs
; reg
++)
4975 regs
->start
[reg
] = regs
->end
[reg
] = -1;
4976 } /* regs && !bufp->no_sub */
4978 DEBUG_PRINT4 ("%u failure points pushed, %u popped (%u remain).\n",
4979 nfailure_points_pushed
, nfailure_points_popped
,
4980 nfailure_points_pushed
- nfailure_points_popped
);
4981 DEBUG_PRINT2 ("%u registers pushed.\n", num_regs_pushed
);
4983 mcnt
= POINTER_TO_OFFSET (d
) - pos
;
4985 DEBUG_PRINT2 ("Returning %d from re_match_2.\n", mcnt
);
4991 /* Otherwise match next pattern command. */
4992 switch (SWITCH_ENUM_CAST ((re_opcode_t
) *p
++))
4994 /* Ignore these. Used to ignore the n of succeed_n's which
4995 currently have n == 0. */
4997 DEBUG_PRINT1 ("EXECUTING no_op.\n");
5001 DEBUG_PRINT1 ("EXECUTING succeed.\n");
5004 /* Match the next n pattern characters exactly. The following
5005 byte in the pattern defines n, and the n bytes after that
5006 are the characters to match. */
5009 DEBUG_PRINT2 ("EXECUTING exactn %d.\n", mcnt
);
5011 /* Remember the start point to rollback upon failure. */
5015 /* This is written out as an if-else so we don't waste time
5016 testing `translate' inside the loop. */
5017 if (RE_TRANSLATE_P (translate
))
5021 if (RE_TRANSLATE (translate
, *d
) != *p
++)
5041 /* The cost of testing `translate' is comparatively small. */
5045 int pat_charlen
, buf_charlen
;
5046 unsigned int pat_ch
, buf_ch
;
5049 pat_ch
= STRING_CHAR_AND_LENGTH (p
, pend
- p
, pat_charlen
);
5050 buf_ch
= STRING_CHAR_AND_LENGTH (d
, dend
- d
, buf_charlen
);
5052 if (TRANSLATE (buf_ch
) != pat_ch
)
5060 mcnt
-= pat_charlen
;
5066 unsigned int buf_ch
;
5070 TRANSLATE_VIA_MULTIBYTE (buf_ch
);
5081 /* Match any character except possibly a newline or a null. */
5087 DEBUG_PRINT1 ("EXECUTING anychar.\n");
5090 buf_ch
= RE_STRING_CHAR_AND_LENGTH (d
, dend
- d
, buf_charlen
);
5092 if ((!(bufp
->syntax
& RE_DOT_NEWLINE
)
5094 || ((bufp
->syntax
& RE_DOT_NOT_NULL
)
5095 && buf_ch
== '\000'))
5098 DEBUG_PRINT2 (" Matched `%d'.\n", *d
);
5107 register unsigned int c
;
5108 boolean
not = (re_opcode_t
) *(p
- 1) == charset_not
;
5111 /* Start of actual range_table, or end of bitmap if there is no
5113 re_char
*range_table
;
5115 /* Nonzero if there is a range table. */
5116 int range_table_exists
;
5118 /* Number of ranges of range table. This is not included
5119 in the initial byte-length of the command. */
5122 DEBUG_PRINT2 ("EXECUTING charset%s.\n", not ? "_not" : "");
5124 range_table_exists
= CHARSET_RANGE_TABLE_EXISTS_P (&p
[-1]);
5126 if (range_table_exists
)
5128 range_table
= CHARSET_RANGE_TABLE (&p
[-1]); /* Past the bitmap. */
5129 EXTRACT_NUMBER_AND_INCR (count
, range_table
);
5133 c
= RE_STRING_CHAR_AND_LENGTH (d
, dend
- d
, len
);
5134 TRANSLATE_VIA_MULTIBYTE (c
); /* The character to match. */
5136 if (! multibyte
|| IS_REAL_ASCII (c
))
5137 { /* Lookup bitmap. */
5138 /* Cast to `unsigned' instead of `unsigned char' in
5139 case the bit list is a full 32 bytes long. */
5140 if (c
< (unsigned) (CHARSET_BITMAP_SIZE (&p
[-1]) * BYTEWIDTH
)
5141 && p
[1 + c
/ BYTEWIDTH
] & (1 << (c
% BYTEWIDTH
)))
5145 else if (range_table_exists
)
5147 int class_bits
= CHARSET_RANGE_TABLE_BITS (&p
[-1]);
5149 if ( (class_bits
& BIT_LOWER
&& ISLOWER (c
))
5150 | (class_bits
& BIT_MULTIBYTE
)
5151 | (class_bits
& BIT_PUNCT
&& ISPUNCT (c
))
5152 | (class_bits
& BIT_SPACE
&& ISSPACE (c
))
5153 | (class_bits
& BIT_UPPER
&& ISUPPER (c
))
5154 | (class_bits
& BIT_WORD
&& ISWORD (c
)))
5157 CHARSET_LOOKUP_RANGE_TABLE_RAW (not, c
, range_table
, count
);
5161 if (range_table_exists
)
5162 p
= CHARSET_RANGE_TABLE_END (range_table
, count
);
5164 p
+= CHARSET_BITMAP_SIZE (&p
[-1]) + 1;
5166 if (!not) goto fail
;
5173 /* The beginning of a group is represented by start_memory.
5174 The argument is the register number. The text
5175 matched within the group is recorded (in the internal
5176 registers data structure) under the register number. */
5178 DEBUG_PRINT2 ("EXECUTING start_memory %d:\n", *p
);
5180 /* In case we need to undo this operation (via backtracking). */
5181 PUSH_FAILURE_REG ((unsigned int)*p
);
5184 regend
[*p
] = NULL
; /* probably unnecessary. -sm */
5185 DEBUG_PRINT2 (" regstart: %d\n", POINTER_TO_OFFSET (regstart
[*p
]));
5187 /* Move past the register number and inner group count. */
5192 /* The stop_memory opcode represents the end of a group. Its
5193 argument is the same as start_memory's: the register number. */
5195 DEBUG_PRINT2 ("EXECUTING stop_memory %d:\n", *p
);
5197 assert (!REG_UNSET (regstart
[*p
]));
5198 /* Strictly speaking, there should be code such as:
5200 assert (REG_UNSET (regend[*p]));
5201 PUSH_FAILURE_REGSTOP ((unsigned int)*p);
5203 But the only info to be pushed is regend[*p] and it is known to
5204 be UNSET, so there really isn't anything to push.
5205 Not pushing anything, on the other hand deprives us from the
5206 guarantee that regend[*p] is UNSET since undoing this operation
5207 will not reset its value properly. This is not important since
5208 the value will only be read on the next start_memory or at
5209 the very end and both events can only happen if this stop_memory
5213 DEBUG_PRINT2 (" regend: %d\n", POINTER_TO_OFFSET (regend
[*p
]));
5215 /* Move past the register number and the inner group count. */
5220 /* \<digit> has been turned into a `duplicate' command which is
5221 followed by the numeric value of <digit> as the register number. */
5224 register re_char
*d2
, *dend2
;
5225 int regno
= *p
++; /* Get which register to match against. */
5226 DEBUG_PRINT2 ("EXECUTING duplicate %d.\n", regno
);
5228 /* Can't back reference a group which we've never matched. */
5229 if (REG_UNSET (regstart
[regno
]) || REG_UNSET (regend
[regno
]))
5232 /* Where in input to try to start matching. */
5233 d2
= regstart
[regno
];
5235 /* Remember the start point to rollback upon failure. */
5238 /* Where to stop matching; if both the place to start and
5239 the place to stop matching are in the same string, then
5240 set to the place to stop, otherwise, for now have to use
5241 the end of the first string. */
5243 dend2
= ((FIRST_STRING_P (regstart
[regno
])
5244 == FIRST_STRING_P (regend
[regno
]))
5245 ? regend
[regno
] : end_match_1
);
5248 /* If necessary, advance to next segment in register
5252 if (dend2
== end_match_2
) break;
5253 if (dend2
== regend
[regno
]) break;
5255 /* End of string1 => advance to string2. */
5257 dend2
= regend
[regno
];
5259 /* At end of register contents => success */
5260 if (d2
== dend2
) break;
5262 /* If necessary, advance to next segment in data. */
5265 /* How many characters left in this segment to match. */
5268 /* Want how many consecutive characters we can match in
5269 one shot, so, if necessary, adjust the count. */
5270 if (mcnt
> dend2
- d2
)
5273 /* Compare that many; failure if mismatch, else move
5275 if (RE_TRANSLATE_P (translate
)
5276 ? bcmp_translate (d
, d2
, mcnt
, translate
, multibyte
)
5277 : memcmp (d
, d2
, mcnt
))
5282 d
+= mcnt
, d2
+= mcnt
;
5288 /* begline matches the empty string at the beginning of the string
5289 (unless `not_bol' is set in `bufp'), and after newlines. */
5291 DEBUG_PRINT1 ("EXECUTING begline.\n");
5293 if (AT_STRINGS_BEG (d
))
5295 if (!bufp
->not_bol
) break;
5300 GET_CHAR_BEFORE_2 (c
, d
, string1
, end1
, string2
, end2
);
5304 /* In all other cases, we fail. */
5308 /* endline is the dual of begline. */
5310 DEBUG_PRINT1 ("EXECUTING endline.\n");
5312 if (AT_STRINGS_END (d
))
5314 if (!bufp
->not_eol
) break;
5318 PREFETCH_NOLIMIT ();
5325 /* Match at the very beginning of the data. */
5327 DEBUG_PRINT1 ("EXECUTING begbuf.\n");
5328 if (AT_STRINGS_BEG (d
))
5333 /* Match at the very end of the data. */
5335 DEBUG_PRINT1 ("EXECUTING endbuf.\n");
5336 if (AT_STRINGS_END (d
))
5341 /* on_failure_keep_string_jump is used to optimize `.*\n'. It
5342 pushes NULL as the value for the string on the stack. Then
5343 `POP_FAILURE_POINT' will keep the current value for the
5344 string, instead of restoring it. To see why, consider
5345 matching `foo\nbar' against `.*\n'. The .* matches the foo;
5346 then the . fails against the \n. But the next thing we want
5347 to do is match the \n against the \n; if we restored the
5348 string value, we would be back at the foo.
5350 Because this is used only in specific cases, we don't need to
5351 check all the things that `on_failure_jump' does, to make
5352 sure the right things get saved on the stack. Hence we don't
5353 share its code. The only reason to push anything on the
5354 stack at all is that otherwise we would have to change
5355 `anychar's code to do something besides goto fail in this
5356 case; that seems worse than this. */
5357 case on_failure_keep_string_jump
:
5358 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5359 DEBUG_PRINT3 ("EXECUTING on_failure_keep_string_jump %d (to %p):\n",
5362 PUSH_FAILURE_POINT (p
- 3, NULL
);
5365 /* A nasty loop is introduced by the non-greedy *? and +?.
5366 With such loops, the stack only ever contains one failure point
5367 at a time, so that a plain on_failure_jump_loop kind of
5368 cycle detection cannot work. Worse yet, such a detection
5369 can not only fail to detect a cycle, but it can also wrongly
5370 detect a cycle (between different instantiations of the same
5372 So the method used for those nasty loops is a little different:
5373 We use a special cycle-detection-stack-frame which is pushed
5374 when the on_failure_jump_nastyloop failure-point is *popped*.
5375 This special frame thus marks the beginning of one iteration
5376 through the loop and we can hence easily check right here
5377 whether something matched between the beginning and the end of
5379 case on_failure_jump_nastyloop
:
5380 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5381 DEBUG_PRINT3 ("EXECUTING on_failure_jump_nastyloop %d (to %p):\n",
5384 assert ((re_opcode_t
)p
[-4] == no_op
);
5385 CHECK_INFINITE_LOOP (p
- 4, d
);
5386 PUSH_FAILURE_POINT (p
- 3, d
);
5390 /* Simple loop detecting on_failure_jump: just check on the
5391 failure stack if the same spot was already hit earlier. */
5392 case on_failure_jump_loop
:
5394 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5395 DEBUG_PRINT3 ("EXECUTING on_failure_jump_loop %d (to %p):\n",
5398 CHECK_INFINITE_LOOP (p
- 3, d
);
5399 PUSH_FAILURE_POINT (p
- 3, d
);
5403 /* Uses of on_failure_jump:
5405 Each alternative starts with an on_failure_jump that points
5406 to the beginning of the next alternative. Each alternative
5407 except the last ends with a jump that in effect jumps past
5408 the rest of the alternatives. (They really jump to the
5409 ending jump of the following alternative, because tensioning
5410 these jumps is a hassle.)
5412 Repeats start with an on_failure_jump that points past both
5413 the repetition text and either the following jump or
5414 pop_failure_jump back to this on_failure_jump. */
5415 case on_failure_jump
:
5416 IMMEDIATE_QUIT_CHECK
;
5417 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5418 DEBUG_PRINT3 ("EXECUTING on_failure_jump %d (to %p):\n",
5421 PUSH_FAILURE_POINT (p
-3, d
);
5424 /* This operation is used for greedy *.
5425 Compare the beginning of the repeat with what in the
5426 pattern follows its end. If we can establish that there
5427 is nothing that they would both match, i.e., that we
5428 would have to backtrack because of (as in, e.g., `a*a')
5429 then we can use a non-backtracking loop based on
5430 on_failure_keep_string_jump instead of on_failure_jump. */
5431 case on_failure_jump_smart
:
5432 IMMEDIATE_QUIT_CHECK
;
5433 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5434 DEBUG_PRINT3 ("EXECUTING on_failure_jump_smart %d (to %p).\n",
5437 re_char
*p1
= p
; /* Next operation. */
5438 /* Here, we discard `const', making re_match non-reentrant. */
5439 unsigned char *p2
= (unsigned char*) p
+ mcnt
; /* Jump dest. */
5440 unsigned char *p3
= (unsigned char*) p
- 3; /* opcode location. */
5442 p
-= 3; /* Reset so that we will re-execute the
5443 instruction once it's been changed. */
5445 EXTRACT_NUMBER (mcnt
, p2
- 2);
5447 /* Ensure this is a indeed the trivial kind of loop
5448 we are expecting. */
5449 assert (skip_one_char (p1
) == p2
- 3);
5450 assert ((re_opcode_t
) p2
[-3] == jump
&& p2
+ mcnt
== p
);
5451 DEBUG_STATEMENT (debug
+= 2);
5452 if (mutually_exclusive_p (bufp
, p1
, p2
))
5454 /* Use a fast `on_failure_keep_string_jump' loop. */
5455 DEBUG_PRINT1 (" smart exclusive => fast loop.\n");
5456 *p3
= (unsigned char) on_failure_keep_string_jump
;
5457 STORE_NUMBER (p2
- 2, mcnt
+ 3);
5461 /* Default to a safe `on_failure_jump' loop. */
5462 DEBUG_PRINT1 (" smart default => slow loop.\n");
5463 *p3
= (unsigned char) on_failure_jump
;
5465 DEBUG_STATEMENT (debug
-= 2);
5469 /* Unconditionally jump (without popping any failure points). */
5472 IMMEDIATE_QUIT_CHECK
;
5473 EXTRACT_NUMBER_AND_INCR (mcnt
, p
); /* Get the amount to jump. */
5474 DEBUG_PRINT2 ("EXECUTING jump %d ", mcnt
);
5475 p
+= mcnt
; /* Do the jump. */
5476 DEBUG_PRINT2 ("(to %p).\n", p
);
5480 /* Have to succeed matching what follows at least n times.
5481 After that, handle like `on_failure_jump'. */
5483 /* Signedness doesn't matter since we only compare MCNT to 0. */
5484 EXTRACT_NUMBER (mcnt
, p
+ 2);
5485 DEBUG_PRINT2 ("EXECUTING succeed_n %d.\n", mcnt
);
5487 /* Originally, mcnt is how many times we HAVE to succeed. */
5490 /* Here, we discard `const', making re_match non-reentrant. */
5491 unsigned char *p2
= (unsigned char*) p
+ 2; /* counter loc. */
5494 PUSH_NUMBER (p2
, mcnt
);
5497 /* The two bytes encoding mcnt == 0 are two no_op opcodes. */
5502 /* Signedness doesn't matter since we only compare MCNT to 0. */
5503 EXTRACT_NUMBER (mcnt
, p
+ 2);
5504 DEBUG_PRINT2 ("EXECUTING jump_n %d.\n", mcnt
);
5506 /* Originally, this is how many times we CAN jump. */
5509 /* Here, we discard `const', making re_match non-reentrant. */
5510 unsigned char *p2
= (unsigned char*) p
+ 2; /* counter loc. */
5512 PUSH_NUMBER (p2
, mcnt
);
5513 goto unconditional_jump
;
5515 /* If don't have to jump any more, skip over the rest of command. */
5522 unsigned char *p2
; /* Location of the counter. */
5523 DEBUG_PRINT1 ("EXECUTING set_number_at.\n");
5525 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5526 /* Here, we discard `const', making re_match non-reentrant. */
5527 p2
= (unsigned char*) p
+ mcnt
;
5528 /* Signedness doesn't matter since we only copy MCNT's bits . */
5529 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5530 DEBUG_PRINT3 (" Setting %p to %d.\n", p2
, mcnt
);
5531 PUSH_NUMBER (p2
, mcnt
);
5537 not = (re_opcode_t
) *(p
- 1) == notwordbound
;
5538 DEBUG_PRINT2 ("EXECUTING %swordbound.\n", not?"not":"");
5540 /* We SUCCEED (or FAIL) in one of the following cases: */
5542 /* Case 1: D is at the beginning or the end of string. */
5543 if (AT_STRINGS_BEG (d
) || AT_STRINGS_END (d
))
5547 /* C1 is the character before D, S1 is the syntax of C1, C2
5548 is the character at D, and S2 is the syntax of C2. */
5553 int offset
= PTR_TO_OFFSET (d
- 1);
5554 int charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
5555 UPDATE_SYNTAX_TABLE (charpos
);
5557 GET_CHAR_BEFORE_2 (c1
, d
, string1
, end1
, string2
, end2
);
5560 UPDATE_SYNTAX_TABLE_FORWARD (charpos
+ 1);
5562 PREFETCH_NOLIMIT ();
5563 GET_CHAR_AFTER (c2
, d
, dummy
);
5566 if (/* Case 2: Only one of S1 and S2 is Sword. */
5567 ((s1
== Sword
) != (s2
== Sword
))
5568 /* Case 3: Both of S1 and S2 are Sword, and macro
5569 WORD_BOUNDARY_P (C1, C2) returns nonzero. */
5570 || ((s1
== Sword
) && WORD_BOUNDARY_P (c1
, c2
)))
5579 DEBUG_PRINT1 ("EXECUTING wordbeg.\n");
5581 /* We FAIL in one of the following cases: */
5583 /* Case 1: D is at the end of string. */
5584 if (AT_STRINGS_END (d
))
5588 /* C1 is the character before D, S1 is the syntax of C1, C2
5589 is the character at D, and S2 is the syntax of C2. */
5594 int offset
= PTR_TO_OFFSET (d
);
5595 int charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
5596 UPDATE_SYNTAX_TABLE (charpos
);
5599 GET_CHAR_AFTER (c2
, d
, dummy
);
5602 /* Case 2: S2 is not Sword. */
5606 /* Case 3: D is not at the beginning of string ... */
5607 if (!AT_STRINGS_BEG (d
))
5609 GET_CHAR_BEFORE_2 (c1
, d
, string1
, end1
, string2
, end2
);
5611 UPDATE_SYNTAX_TABLE_BACKWARD (charpos
- 1);
5615 /* ... and S1 is Sword, and WORD_BOUNDARY_P (C1, C2)
5617 if ((s1
== Sword
) && !WORD_BOUNDARY_P (c1
, c2
))
5624 DEBUG_PRINT1 ("EXECUTING wordend.\n");
5626 /* We FAIL in one of the following cases: */
5628 /* Case 1: D is at the beginning of string. */
5629 if (AT_STRINGS_BEG (d
))
5633 /* C1 is the character before D, S1 is the syntax of C1, C2
5634 is the character at D, and S2 is the syntax of C2. */
5639 int offset
= PTR_TO_OFFSET (d
) - 1;
5640 int charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
5641 UPDATE_SYNTAX_TABLE (charpos
);
5643 GET_CHAR_BEFORE_2 (c1
, d
, string1
, end1
, string2
, end2
);
5646 /* Case 2: S1 is not Sword. */
5650 /* Case 3: D is not at the end of string ... */
5651 if (!AT_STRINGS_END (d
))
5653 PREFETCH_NOLIMIT ();
5654 GET_CHAR_AFTER (c2
, d
, dummy
);
5656 UPDATE_SYNTAX_TABLE_FORWARD (charpos
);
5660 /* ... and S2 is Sword, and WORD_BOUNDARY_P (C1, C2)
5662 if ((s2
== Sword
) && !WORD_BOUNDARY_P (c1
, c2
))
5670 not = (re_opcode_t
) *(p
- 1) == notsyntaxspec
;
5672 DEBUG_PRINT3 ("EXECUTING %ssyntaxspec %d.\n", not?"not":"", mcnt
);
5676 int offset
= PTR_TO_OFFSET (d
);
5677 int pos1
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
5678 UPDATE_SYNTAX_TABLE (pos1
);
5685 GET_CHAR_AFTER (c
, d
, len
);
5686 if ((SYNTAX (c
) != (enum syntaxcode
) mcnt
) ^ not)
5694 DEBUG_PRINT1 ("EXECUTING before_dot.\n");
5695 if (PTR_BYTE_POS (d
) >= PT_BYTE
)
5700 DEBUG_PRINT1 ("EXECUTING at_dot.\n");
5701 if (PTR_BYTE_POS (d
) != PT_BYTE
)
5706 DEBUG_PRINT1 ("EXECUTING after_dot.\n");
5707 if (PTR_BYTE_POS (d
) <= PT_BYTE
)
5712 case notcategoryspec
:
5713 not = (re_opcode_t
) *(p
- 1) == notcategoryspec
;
5715 DEBUG_PRINT3 ("EXECUTING %scategoryspec %d.\n", not?"not":"", mcnt
);
5721 GET_CHAR_AFTER (c
, d
, len
);
5723 if ((!CHAR_HAS_CATEGORY (c
, mcnt
)) ^ not)
5734 continue; /* Successfully executed one pattern command; keep going. */
5737 /* We goto here if a matching operation fails. */
5739 IMMEDIATE_QUIT_CHECK
;
5740 if (!FAIL_STACK_EMPTY ())
5743 /* A restart point is known. Restore to that state. */
5744 DEBUG_PRINT1 ("\nFAIL:\n");
5745 POP_FAILURE_POINT (str
, pat
);
5746 switch (SWITCH_ENUM_CAST ((re_opcode_t
) *pat
++))
5748 case on_failure_keep_string_jump
:
5749 assert (str
== NULL
);
5750 goto continue_failure_jump
;
5752 case on_failure_jump_nastyloop
:
5753 assert ((re_opcode_t
)pat
[-2] == no_op
);
5754 PUSH_FAILURE_POINT (pat
- 2, str
);
5757 case on_failure_jump_loop
:
5758 case on_failure_jump
:
5761 continue_failure_jump
:
5762 EXTRACT_NUMBER_AND_INCR (mcnt
, pat
);
5767 /* A special frame used for nastyloops. */
5774 assert (p
>= bufp
->buffer
&& p
<= pend
);
5776 if (d
>= string1
&& d
<= end1
)
5780 break; /* Matching at this starting point really fails. */
5784 goto restore_best_regs
;
5788 return -1; /* Failure to match. */
5791 /* Subroutine definitions for re_match_2. */
5793 /* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
5794 bytes; nonzero otherwise. */
5797 bcmp_translate (s1
, s2
, len
, translate
, multibyte
)
5800 RE_TRANSLATE_TYPE translate
;
5801 const int multibyte
;
5803 register re_char
*p1
= s1
, *p2
= s2
;
5804 re_char
*p1_end
= s1
+ len
;
5805 re_char
*p2_end
= s2
+ len
;
5807 /* FIXME: Checking both p1 and p2 presumes that the two strings might have
5808 different lengths, but relying on a single `len' would break this. -sm */
5809 while (p1
< p1_end
&& p2
< p2_end
)
5811 int p1_charlen
, p2_charlen
;
5812 re_wchar_t p1_ch
, p2_ch
;
5814 GET_CHAR_AFTER (p1_ch
, p1
, p1_charlen
);
5815 GET_CHAR_AFTER (p2_ch
, p2
, p2_charlen
);
5817 if (RE_TRANSLATE (translate
, p1_ch
)
5818 != RE_TRANSLATE (translate
, p2_ch
))
5821 p1
+= p1_charlen
, p2
+= p2_charlen
;
5824 if (p1
!= p1_end
|| p2
!= p2_end
)
5830 /* Entry points for GNU code. */
5832 /* re_compile_pattern is the GNU regular expression compiler: it
5833 compiles PATTERN (of length SIZE) and puts the result in BUFP.
5834 Returns 0 if the pattern was valid, otherwise an error string.
5836 Assumes the `allocated' (and perhaps `buffer') and `translate' fields
5837 are set in BUFP on entry.
5839 We call regex_compile to do the actual compilation. */
5842 re_compile_pattern (pattern
, length
, bufp
)
5843 const char *pattern
;
5845 struct re_pattern_buffer
*bufp
;
5849 /* GNU code is written to assume at least RE_NREGS registers will be set
5850 (and at least one extra will be -1). */
5851 bufp
->regs_allocated
= REGS_UNALLOCATED
;
5853 /* And GNU code determines whether or not to get register information
5854 by passing null for the REGS argument to re_match, etc., not by
5858 ret
= regex_compile ((re_char
*) pattern
, length
, re_syntax_options
, bufp
);
5862 return gettext (re_error_msgid
[(int) ret
]);
5864 WEAK_ALIAS (__re_compile_pattern
, re_compile_pattern
)
5866 /* Entry points compatible with 4.2 BSD regex library. We don't define
5867 them unless specifically requested. */
5869 #if defined _REGEX_RE_COMP || defined _LIBC
5871 /* BSD has one and only one pattern buffer. */
5872 static struct re_pattern_buffer re_comp_buf
;
5876 /* Make these definitions weak in libc, so POSIX programs can redefine
5877 these names if they don't use our functions, and still use
5878 regcomp/regexec below without link errors. */
5888 if (!re_comp_buf
.buffer
)
5889 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
5890 return (char *) gettext ("No previous regular expression");
5894 if (!re_comp_buf
.buffer
)
5896 re_comp_buf
.buffer
= (unsigned char *) malloc (200);
5897 if (re_comp_buf
.buffer
== NULL
)
5898 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
5899 return (char *) gettext (re_error_msgid
[(int) REG_ESPACE
]);
5900 re_comp_buf
.allocated
= 200;
5902 re_comp_buf
.fastmap
= (char *) malloc (1 << BYTEWIDTH
);
5903 if (re_comp_buf
.fastmap
== NULL
)
5904 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
5905 return (char *) gettext (re_error_msgid
[(int) REG_ESPACE
]);
5908 /* Since `re_exec' always passes NULL for the `regs' argument, we
5909 don't need to initialize the pattern buffer fields which affect it. */
5911 ret
= regex_compile (s
, strlen (s
), re_syntax_options
, &re_comp_buf
);
5916 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
5917 return (char *) gettext (re_error_msgid
[(int) ret
]);
5928 const int len
= strlen (s
);
5930 0 <= re_search (&re_comp_buf
, s
, len
, 0, len
, (struct re_registers
*) 0);
5932 #endif /* _REGEX_RE_COMP */
5934 /* POSIX.2 functions. Don't define these for Emacs. */
5938 /* regcomp takes a regular expression as a string and compiles it.
5940 PREG is a regex_t *. We do not expect any fields to be initialized,
5941 since POSIX says we shouldn't. Thus, we set
5943 `buffer' to the compiled pattern;
5944 `used' to the length of the compiled pattern;
5945 `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
5946 REG_EXTENDED bit in CFLAGS is set; otherwise, to
5947 RE_SYNTAX_POSIX_BASIC;
5948 `fastmap' to an allocated space for the fastmap;
5949 `fastmap_accurate' to zero;
5950 `re_nsub' to the number of subexpressions in PATTERN.
5952 PATTERN is the address of the pattern string.
5954 CFLAGS is a series of bits which affect compilation.
5956 If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
5957 use POSIX basic syntax.
5959 If REG_NEWLINE is set, then . and [^...] don't match newline.
5960 Also, regexec will try a match beginning after every newline.
5962 If REG_ICASE is set, then we considers upper- and lowercase
5963 versions of letters to be equivalent when matching.
5965 If REG_NOSUB is set, then when PREG is passed to regexec, that
5966 routine will report only success or failure, and nothing about the
5969 It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
5970 the return codes and their meanings.) */
5973 regcomp (preg
, pattern
, cflags
)
5974 regex_t
*__restrict preg
;
5975 const char *__restrict pattern
;
5980 = (cflags
& REG_EXTENDED
) ?
5981 RE_SYNTAX_POSIX_EXTENDED
: RE_SYNTAX_POSIX_BASIC
;
5983 /* regex_compile will allocate the space for the compiled pattern. */
5985 preg
->allocated
= 0;
5988 /* Try to allocate space for the fastmap. */
5989 preg
->fastmap
= (char *) malloc (1 << BYTEWIDTH
);
5991 if (cflags
& REG_ICASE
)
5996 = (RE_TRANSLATE_TYPE
) malloc (CHAR_SET_SIZE
5997 * sizeof (*(RE_TRANSLATE_TYPE
)0));
5998 if (preg
->translate
== NULL
)
5999 return (int) REG_ESPACE
;
6001 /* Map uppercase characters to corresponding lowercase ones. */
6002 for (i
= 0; i
< CHAR_SET_SIZE
; i
++)
6003 preg
->translate
[i
] = ISUPPER (i
) ? TOLOWER (i
) : i
;
6006 preg
->translate
= NULL
;
6008 /* If REG_NEWLINE is set, newlines are treated differently. */
6009 if (cflags
& REG_NEWLINE
)
6010 { /* REG_NEWLINE implies neither . nor [^...] match newline. */
6011 syntax
&= ~RE_DOT_NEWLINE
;
6012 syntax
|= RE_HAT_LISTS_NOT_NEWLINE
;
6015 syntax
|= RE_NO_NEWLINE_ANCHOR
;
6017 preg
->no_sub
= !!(cflags
& REG_NOSUB
);
6019 /* POSIX says a null character in the pattern terminates it, so we
6020 can use strlen here in compiling the pattern. */
6021 ret
= regex_compile ((re_char
*) pattern
, strlen (pattern
), syntax
, preg
);
6023 /* POSIX doesn't distinguish between an unmatched open-group and an
6024 unmatched close-group: both are REG_EPAREN. */
6025 if (ret
== REG_ERPAREN
)
6028 if (ret
== REG_NOERROR
&& preg
->fastmap
)
6029 { /* Compute the fastmap now, since regexec cannot modify the pattern
6031 re_compile_fastmap (preg
);
6032 if (preg
->can_be_null
)
6033 { /* The fastmap can't be used anyway. */
6034 free (preg
->fastmap
);
6035 preg
->fastmap
= NULL
;
6040 WEAK_ALIAS (__regcomp
, regcomp
)
6043 /* regexec searches for a given pattern, specified by PREG, in the
6046 If NMATCH is zero or REG_NOSUB was set in the cflags argument to
6047 `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
6048 least NMATCH elements, and we set them to the offsets of the
6049 corresponding matched substrings.
6051 EFLAGS specifies `execution flags' which affect matching: if
6052 REG_NOTBOL is set, then ^ does not match at the beginning of the
6053 string; if REG_NOTEOL is set, then $ does not match at the end.
6055 We return 0 if we find a match and REG_NOMATCH if not. */
6058 regexec (preg
, string
, nmatch
, pmatch
, eflags
)
6059 const regex_t
*__restrict preg
;
6060 const char *__restrict string
;
6062 regmatch_t pmatch
[];
6066 struct re_registers regs
;
6067 regex_t private_preg
;
6068 int len
= strlen (string
);
6069 boolean want_reg_info
= !preg
->no_sub
&& nmatch
> 0 && pmatch
;
6071 private_preg
= *preg
;
6073 private_preg
.not_bol
= !!(eflags
& REG_NOTBOL
);
6074 private_preg
.not_eol
= !!(eflags
& REG_NOTEOL
);
6076 /* The user has told us exactly how many registers to return
6077 information about, via `nmatch'. We have to pass that on to the
6078 matching routines. */
6079 private_preg
.regs_allocated
= REGS_FIXED
;
6083 regs
.num_regs
= nmatch
;
6084 regs
.start
= TALLOC (nmatch
* 2, regoff_t
);
6085 if (regs
.start
== NULL
)
6086 return (int) REG_NOMATCH
;
6087 regs
.end
= regs
.start
+ nmatch
;
6090 /* Instead of using not_eol to implement REG_NOTEOL, we could simply
6091 pass (&private_preg, string, len + 1, 0, len, ...) pretending the string
6092 was a little bit longer but still only matching the real part.
6093 This works because the `endline' will check for a '\n' and will find a
6094 '\0', correctly deciding that this is not the end of a line.
6095 But it doesn't work out so nicely for REG_NOTBOL, since we don't have
6096 a convenient '\0' there. For all we know, the string could be preceded
6097 by '\n' which would throw things off. */
6099 /* Perform the searching operation. */
6100 ret
= re_search (&private_preg
, string
, len
,
6101 /* start: */ 0, /* range: */ len
,
6102 want_reg_info
? ®s
: (struct re_registers
*) 0);
6104 /* Copy the register information to the POSIX structure. */
6111 for (r
= 0; r
< nmatch
; r
++)
6113 pmatch
[r
].rm_so
= regs
.start
[r
];
6114 pmatch
[r
].rm_eo
= regs
.end
[r
];
6118 /* If we needed the temporary register info, free the space now. */
6122 /* We want zero return to mean success, unlike `re_search'. */
6123 return ret
>= 0 ? (int) REG_NOERROR
: (int) REG_NOMATCH
;
6125 WEAK_ALIAS (__regexec
, regexec
)
6128 /* Returns a message corresponding to an error code, ERRCODE, returned
6129 from either regcomp or regexec. We don't use PREG here. */
6132 regerror (errcode
, preg
, errbuf
, errbuf_size
)
6134 const regex_t
*preg
;
6142 || errcode
>= (sizeof (re_error_msgid
) / sizeof (re_error_msgid
[0])))
6143 /* Only error codes returned by the rest of the code should be passed
6144 to this routine. If we are given anything else, or if other regex
6145 code generates an invalid error code, then the program has a bug.
6146 Dump core so we can fix it. */
6149 msg
= gettext (re_error_msgid
[errcode
]);
6151 msg_size
= strlen (msg
) + 1; /* Includes the null. */
6153 if (errbuf_size
!= 0)
6155 if (msg_size
> errbuf_size
)
6157 strncpy (errbuf
, msg
, errbuf_size
- 1);
6158 errbuf
[errbuf_size
- 1] = 0;
6161 strcpy (errbuf
, msg
);
6166 WEAK_ALIAS (__regerror
, regerror
)
6169 /* Free dynamically allocated space used by PREG. */
6175 if (preg
->buffer
!= NULL
)
6176 free (preg
->buffer
);
6177 preg
->buffer
= NULL
;
6179 preg
->allocated
= 0;
6182 if (preg
->fastmap
!= NULL
)
6183 free (preg
->fastmap
);
6184 preg
->fastmap
= NULL
;
6185 preg
->fastmap_accurate
= 0;
6187 if (preg
->translate
!= NULL
)
6188 free (preg
->translate
);
6189 preg
->translate
= NULL
;
6191 WEAK_ALIAS (__regfree
, regfree
)
6193 #endif /* not emacs */