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-2014 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 3, 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, see <http://www.gnu.org/licenses/>. */
21 - structure the opcode space into opcode+flag.
22 - merge with glibc's regex.[ch].
23 - replace (succeed_n + jump_n + set_number_at) with something that doesn't
24 need to modify the compiled regexp so that re_match can be reentrant.
25 - get rid of on_failure_jump_smart by doing the optimization in re_comp
26 rather than at run-time, so that re_match can be reentrant.
29 /* AIX requires this to be the first thing in the file. */
30 #if defined _AIX && !defined REGEX_MALLOC
34 /* Ignore some GCC warnings for now. This section should go away
35 once the Emacs and Gnulib regex code is merged. */
36 #if 4 < __GNUC__ + (5 <= __GNUC_MINOR__) || defined __clang__
37 # pragma GCC diagnostic ignored "-Wstrict-overflow"
39 # pragma GCC diagnostic ignored "-Wunused-function"
40 # pragma GCC diagnostic ignored "-Wunused-macros"
41 # pragma GCC diagnostic ignored "-Wunused-result"
42 # pragma GCC diagnostic ignored "-Wunused-variable"
46 #if 4 < __GNUC__ + (6 <= __GNUC_MINOR__) && ! defined __clang__
47 # pragma GCC diagnostic ignored "-Wunused-but-set-variable"
55 /* We need this for `regex.h', and perhaps for the Emacs include files. */
56 # include <sys/types.h>
59 /* Whether to use ISO C Amendment 1 wide char functions.
60 Those should not be used for Emacs since it uses its own. */
62 #define WIDE_CHAR_SUPPORT 1
64 #define WIDE_CHAR_SUPPORT \
65 (HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_BTOWC && !emacs)
68 /* For platform which support the ISO C amendment 1 functionality we
69 support user defined character classes. */
71 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
77 /* We have to keep the namespace clean. */
78 # define regfree(preg) __regfree (preg)
79 # define regexec(pr, st, nm, pm, ef) __regexec (pr, st, nm, pm, ef)
80 # define regcomp(preg, pattern, cflags) __regcomp (preg, pattern, cflags)
81 # define regerror(err_code, preg, errbuf, errbuf_size) \
82 __regerror (err_code, preg, errbuf, errbuf_size)
83 # define re_set_registers(bu, re, nu, st, en) \
84 __re_set_registers (bu, re, nu, st, en)
85 # define re_match_2(bufp, string1, size1, string2, size2, pos, regs, stop) \
86 __re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
87 # define re_match(bufp, string, size, pos, regs) \
88 __re_match (bufp, string, size, pos, regs)
89 # define re_search(bufp, string, size, startpos, range, regs) \
90 __re_search (bufp, string, size, startpos, range, regs)
91 # define re_compile_pattern(pattern, length, bufp) \
92 __re_compile_pattern (pattern, length, bufp)
93 # define re_set_syntax(syntax) __re_set_syntax (syntax)
94 # define re_search_2(bufp, st1, s1, st2, s2, startpos, range, regs, stop) \
95 __re_search_2 (bufp, st1, s1, st2, s2, startpos, range, regs, stop)
96 # define re_compile_fastmap(bufp) __re_compile_fastmap (bufp)
98 /* Make sure we call libc's function even if the user overrides them. */
99 # define btowc __btowc
100 # define iswctype __iswctype
101 # define wctype __wctype
103 # define WEAK_ALIAS(a,b) weak_alias (a, b)
105 /* We are also using some library internals. */
106 # include <locale/localeinfo.h>
107 # include <locale/elem-hash.h>
108 # include <langinfo.h>
110 # define WEAK_ALIAS(a,b)
113 /* This is for other GNU distributions with internationalized messages. */
114 #if HAVE_LIBINTL_H || defined _LIBC
115 # include <libintl.h>
117 # define gettext(msgid) (msgid)
121 /* This define is so xgettext can find the internationalizable
123 # define gettext_noop(String) String
126 /* The `emacs' switch turns on certain matching commands
127 that make sense only in Emacs. */
131 # include "character.h"
135 # include "category.h"
137 /* Make syntax table lookup grant data in gl_state. */
138 # define SYNTAX(c) syntax_property (c, 1)
143 # define malloc xmalloc
147 # define realloc xrealloc
153 /* Converts the pointer to the char to BEG-based offset from the start. */
154 # define PTR_TO_OFFSET(d) POS_AS_IN_BUFFER (POINTER_TO_OFFSET (d))
155 # define POS_AS_IN_BUFFER(p) ((p) + (NILP (re_match_object) || BUFFERP (re_match_object)))
157 # define RE_MULTIBYTE_P(bufp) ((bufp)->multibyte)
158 # define RE_TARGET_MULTIBYTE_P(bufp) ((bufp)->target_multibyte)
159 # define RE_STRING_CHAR(p, multibyte) \
160 (multibyte ? (STRING_CHAR (p)) : (*(p)))
161 # define RE_STRING_CHAR_AND_LENGTH(p, len, multibyte) \
162 (multibyte ? (STRING_CHAR_AND_LENGTH (p, len)) : ((len) = 1, *(p)))
164 # define RE_CHAR_TO_MULTIBYTE(c) UNIBYTE_TO_CHAR (c)
166 # define RE_CHAR_TO_UNIBYTE(c) CHAR_TO_BYTE_SAFE (c)
168 /* Set C a (possibly converted to multibyte) character before P. P
169 points into a string which is the virtual concatenation of STR1
170 (which ends at END1) or STR2 (which ends at END2). */
171 # define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2) \
173 if (target_multibyte) \
175 re_char *dtemp = (p) == (str2) ? (end1) : (p); \
176 re_char *dlimit = ((p) > (str2) && (p) <= (end2)) ? (str2) : (str1); \
177 while (dtemp-- > dlimit && !CHAR_HEAD_P (*dtemp)); \
178 c = STRING_CHAR (dtemp); \
182 (c = ((p) == (str2) ? (end1) : (p))[-1]); \
183 (c) = RE_CHAR_TO_MULTIBYTE (c); \
187 /* Set C a (possibly converted to multibyte) character at P, and set
188 LEN to the byte length of that character. */
189 # define GET_CHAR_AFTER(c, p, len) \
191 if (target_multibyte) \
192 (c) = STRING_CHAR_AND_LENGTH (p, len); \
197 (c) = RE_CHAR_TO_MULTIBYTE (c); \
201 #else /* not emacs */
203 /* If we are not linking with Emacs proper,
204 we can't use the relocating allocator
205 even if config.h says that we can. */
210 /* When used in Emacs's lib-src, we need xmalloc and xrealloc. */
213 xmalloc (size_t size
)
215 void *val
= malloc (size
);
218 write (2, "virtual memory exhausted\n", 25);
225 xrealloc (void *block
, size_t size
)
228 /* We must call malloc explicitly when BLOCK is 0, since some
229 reallocs don't do this. */
233 val
= realloc (block
, size
);
236 write (2, "virtual memory exhausted\n", 25);
245 # define malloc xmalloc
249 # define realloc xrealloc
251 # include <stdbool.h>
254 /* Define the syntax stuff for \<, \>, etc. */
256 /* Sword must be nonzero for the wordchar pattern commands in re_match_2. */
257 enum syntaxcode
{ Swhitespace
= 0, Sword
= 1, Ssymbol
= 2 };
259 /* Dummy macros for non-Emacs environments. */
260 # define MAX_MULTIBYTE_LENGTH 1
261 # define RE_MULTIBYTE_P(x) 0
262 # define RE_TARGET_MULTIBYTE_P(x) 0
263 # define WORD_BOUNDARY_P(c1, c2) (0)
264 # define BYTES_BY_CHAR_HEAD(p) (1)
265 # define PREV_CHAR_BOUNDARY(p, limit) ((p)--)
266 # define STRING_CHAR(p) (*(p))
267 # define RE_STRING_CHAR(p, multibyte) STRING_CHAR (p)
268 # define CHAR_STRING(c, s) (*(s) = (c), 1)
269 # define STRING_CHAR_AND_LENGTH(p, actual_len) ((actual_len) = 1, *(p))
270 # define RE_STRING_CHAR_AND_LENGTH(p, len, multibyte) STRING_CHAR_AND_LENGTH (p, len)
271 # define RE_CHAR_TO_MULTIBYTE(c) (c)
272 # define RE_CHAR_TO_UNIBYTE(c) (c)
273 # define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2) \
274 (c = ((p) == (str2) ? *((end1) - 1) : *((p) - 1)))
275 # define GET_CHAR_AFTER(c, p, len) \
277 # define CHAR_BYTE8_P(c) (0)
278 # define CHAR_LEADING_CODE(c) (c)
280 #endif /* not emacs */
283 # define RE_TRANSLATE(TBL, C) ((unsigned char)(TBL)[C])
284 # define RE_TRANSLATE_P(TBL) (TBL)
287 /* Get the interface, including the syntax bits. */
290 /* isalpha etc. are used for the character classes. */
295 /* 1 if C is an ASCII character. */
296 # define IS_REAL_ASCII(c) ((c) < 0200)
298 /* 1 if C is a unibyte character. */
299 # define ISUNIBYTE(c) (SINGLE_BYTE_CHAR_P ((c)))
301 /* The Emacs definitions should not be directly affected by locales. */
303 /* In Emacs, these are only used for single-byte characters. */
304 # define ISDIGIT(c) ((c) >= '0' && (c) <= '9')
305 # define ISCNTRL(c) ((c) < ' ')
306 # define ISXDIGIT(c) (((c) >= '0' && (c) <= '9') \
307 || ((c) >= 'a' && (c) <= 'f') \
308 || ((c) >= 'A' && (c) <= 'F'))
310 /* This is only used for single-byte characters. */
311 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
313 /* The rest must handle multibyte characters. */
315 # define ISGRAPH(c) (SINGLE_BYTE_CHAR_P (c) \
316 ? (c) > ' ' && !((c) >= 0177 && (c) <= 0237) \
319 # define ISPRINT(c) (SINGLE_BYTE_CHAR_P (c) \
320 ? (c) >= ' ' && !((c) >= 0177 && (c) <= 0237) \
323 # define ISALNUM(c) (IS_REAL_ASCII (c) \
324 ? (((c) >= 'a' && (c) <= 'z') \
325 || ((c) >= 'A' && (c) <= 'Z') \
326 || ((c) >= '0' && (c) <= '9')) \
327 : SYNTAX (c) == Sword)
329 # define ISALPHA(c) (IS_REAL_ASCII (c) \
330 ? (((c) >= 'a' && (c) <= 'z') \
331 || ((c) >= 'A' && (c) <= 'Z')) \
332 : SYNTAX (c) == Sword)
334 # define ISLOWER(c) lowercasep (c)
336 # define ISPUNCT(c) (IS_REAL_ASCII (c) \
337 ? ((c) > ' ' && (c) < 0177 \
338 && !(((c) >= 'a' && (c) <= 'z') \
339 || ((c) >= 'A' && (c) <= 'Z') \
340 || ((c) >= '0' && (c) <= '9'))) \
341 : SYNTAX (c) != Sword)
343 # define ISSPACE(c) (SYNTAX (c) == Swhitespace)
345 # define ISUPPER(c) uppercasep (c)
347 # define ISWORD(c) (SYNTAX (c) == Sword)
349 #else /* not emacs */
351 /* 1 if C is an ASCII character. */
352 # define IS_REAL_ASCII(c) ((c) < 0200)
354 /* This distinction is not meaningful, except in Emacs. */
355 # define ISUNIBYTE(c) 1
358 # define ISBLANK(c) isblank (c)
360 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
363 # define ISGRAPH(c) isgraph (c)
365 # define ISGRAPH(c) (isprint (c) && !isspace (c))
368 /* Solaris defines ISPRINT so we must undefine it first. */
370 # define ISPRINT(c) isprint (c)
371 # define ISDIGIT(c) isdigit (c)
372 # define ISALNUM(c) isalnum (c)
373 # define ISALPHA(c) isalpha (c)
374 # define ISCNTRL(c) iscntrl (c)
375 # define ISLOWER(c) islower (c)
376 # define ISPUNCT(c) ispunct (c)
377 # define ISSPACE(c) isspace (c)
378 # define ISUPPER(c) isupper (c)
379 # define ISXDIGIT(c) isxdigit (c)
381 # define ISWORD(c) ISALPHA (c)
384 # define TOLOWER(c) _tolower (c)
386 # define TOLOWER(c) tolower (c)
389 /* How many characters in the character set. */
390 # define CHAR_SET_SIZE 256
394 extern char *re_syntax_table
;
396 # else /* not SYNTAX_TABLE */
398 static char re_syntax_table
[CHAR_SET_SIZE
];
401 init_syntax_once (void)
409 memset (re_syntax_table
, 0, sizeof re_syntax_table
);
411 for (c
= 0; c
< CHAR_SET_SIZE
; ++c
)
413 re_syntax_table
[c
] = Sword
;
415 re_syntax_table
['_'] = Ssymbol
;
420 # endif /* not SYNTAX_TABLE */
422 # define SYNTAX(c) re_syntax_table[(c)]
424 #endif /* not emacs */
426 #define SIGN_EXTEND_CHAR(c) ((signed char) (c))
428 /* Should we use malloc or alloca? If REGEX_MALLOC is not defined, we
429 use `alloca' instead of `malloc'. This is because using malloc in
430 re_search* or re_match* could cause memory leaks when C-g is used in
431 Emacs; also, malloc is slower and causes storage fragmentation. On
432 the other hand, malloc is more portable, and easier to debug.
434 Because we sometimes use alloca, some routines have to be macros,
435 not functions -- `alloca'-allocated space disappears at the end of the
436 function it is called in. */
440 # define REGEX_ALLOCATE malloc
441 # define REGEX_REALLOCATE(source, osize, nsize) realloc (source, nsize)
442 # define REGEX_FREE free
444 #else /* not REGEX_MALLOC */
446 /* Emacs already defines alloca, sometimes. */
449 /* Make alloca work the best possible way. */
451 # define alloca __builtin_alloca
452 # else /* not __GNUC__ */
453 # ifdef HAVE_ALLOCA_H
455 # endif /* HAVE_ALLOCA_H */
456 # endif /* not __GNUC__ */
458 # endif /* not alloca */
460 # define REGEX_ALLOCATE alloca
462 /* Assumes a `char *destination' variable. */
463 # define REGEX_REALLOCATE(source, osize, nsize) \
464 (destination = alloca (nsize), \
465 memcpy (destination, source, osize))
467 /* No need to do anything to free, after alloca. */
468 # define REGEX_FREE(arg) ((void)0) /* Do nothing! But inhibit gcc warning. */
470 #endif /* not REGEX_MALLOC */
472 /* Define how to allocate the failure stack. */
474 #if defined REL_ALLOC && defined REGEX_MALLOC
476 # define REGEX_ALLOCATE_STACK(size) \
477 r_alloc (&failure_stack_ptr, (size))
478 # define REGEX_REALLOCATE_STACK(source, osize, nsize) \
479 r_re_alloc (&failure_stack_ptr, (nsize))
480 # define REGEX_FREE_STACK(ptr) \
481 r_alloc_free (&failure_stack_ptr)
483 #else /* not using relocating allocator */
487 # define REGEX_ALLOCATE_STACK malloc
488 # define REGEX_REALLOCATE_STACK(source, osize, nsize) realloc (source, nsize)
489 # define REGEX_FREE_STACK free
491 # else /* not REGEX_MALLOC */
493 # define REGEX_ALLOCATE_STACK alloca
495 # define REGEX_REALLOCATE_STACK(source, osize, nsize) \
496 REGEX_REALLOCATE (source, osize, nsize)
497 /* No need to explicitly free anything. */
498 # define REGEX_FREE_STACK(arg) ((void)0)
500 # endif /* not REGEX_MALLOC */
501 #endif /* not using relocating allocator */
504 /* True if `size1' is non-NULL and PTR is pointing anywhere inside
505 `string1' or just past its end. This works if PTR is NULL, which is
507 #define FIRST_STRING_P(ptr) \
508 (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
510 /* (Re)Allocate N items of type T using malloc, or fail. */
511 #define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t)))
512 #define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
513 #define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
515 #define BYTEWIDTH 8 /* In bits. */
517 #define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
521 #define MAX(a, b) ((a) > (b) ? (a) : (b))
522 #define MIN(a, b) ((a) < (b) ? (a) : (b))
524 /* Type of source-pattern and string chars. */
526 typedef unsigned char re_char
;
527 typedef const re_char const_re_char
;
529 typedef const unsigned char re_char
;
530 typedef re_char const_re_char
;
533 typedef char boolean
;
535 static regoff_t
re_match_2_internal (struct re_pattern_buffer
*bufp
,
536 re_char
*string1
, size_t size1
,
537 re_char
*string2
, size_t size2
,
539 struct re_registers
*regs
,
542 /* These are the command codes that appear in compiled regular
543 expressions. Some opcodes are followed by argument bytes. A
544 command code can specify any interpretation whatsoever for its
545 arguments. Zero bytes may appear in the compiled regular expression. */
551 /* Succeed right away--no more backtracking. */
554 /* Followed by one byte giving n, then by n literal bytes. */
557 /* Matches any (more or less) character. */
560 /* Matches any one char belonging to specified set. First
561 following byte is number of bitmap bytes. Then come bytes
562 for a bitmap saying which chars are in. Bits in each byte
563 are ordered low-bit-first. A character is in the set if its
564 bit is 1. A character too large to have a bit in the map is
565 automatically not in the set.
567 If the length byte has the 0x80 bit set, then that stuff
568 is followed by a range table:
569 2 bytes of flags for character sets (low 8 bits, high 8 bits)
570 See RANGE_TABLE_WORK_BITS below.
571 2 bytes, the number of pairs that follow (upto 32767)
572 pairs, each 2 multibyte characters,
573 each multibyte character represented as 3 bytes. */
576 /* Same parameters as charset, but match any character that is
577 not one of those specified. */
580 /* Start remembering the text that is matched, for storing in a
581 register. Followed by one byte with the register number, in
582 the range 0 to one less than the pattern buffer's re_nsub
586 /* Stop remembering the text that is matched and store it in a
587 memory register. Followed by one byte with the register
588 number, in the range 0 to one less than `re_nsub' in the
592 /* Match a duplicate of something remembered. Followed by one
593 byte containing the register number. */
596 /* Fail unless at beginning of line. */
599 /* Fail unless at end of line. */
602 /* Succeeds if at beginning of buffer (if emacs) or at beginning
603 of string to be matched (if not). */
606 /* Analogously, for end of buffer/string. */
609 /* Followed by two byte relative address to which to jump. */
612 /* Followed by two-byte relative address of place to resume at
613 in case of failure. */
616 /* Like on_failure_jump, but pushes a placeholder instead of the
617 current string position when executed. */
618 on_failure_keep_string_jump
,
620 /* Just like `on_failure_jump', except that it checks that we
621 don't get stuck in an infinite loop (matching an empty string
623 on_failure_jump_loop
,
625 /* Just like `on_failure_jump_loop', except that it checks for
626 a different kind of loop (the kind that shows up with non-greedy
627 operators). This operation has to be immediately preceded
629 on_failure_jump_nastyloop
,
631 /* A smart `on_failure_jump' used for greedy * and + operators.
632 It analyzes the loop before which it is put and if the
633 loop does not require backtracking, it changes itself to
634 `on_failure_keep_string_jump' and short-circuits the loop,
635 else it just defaults to changing itself into `on_failure_jump'.
636 It assumes that it is pointing to just past a `jump'. */
637 on_failure_jump_smart
,
639 /* Followed by two-byte relative address and two-byte number n.
640 After matching N times, jump to the address upon failure.
641 Does not work if N starts at 0: use on_failure_jump_loop
645 /* Followed by two-byte relative address, and two-byte number n.
646 Jump to the address N times, then fail. */
649 /* Set the following two-byte relative address to the
650 subsequent two-byte number. The address *includes* the two
654 wordbeg
, /* Succeeds if at word beginning. */
655 wordend
, /* Succeeds if at word end. */
657 wordbound
, /* Succeeds if at a word boundary. */
658 notwordbound
, /* Succeeds if not at a word boundary. */
660 symbeg
, /* Succeeds if at symbol beginning. */
661 symend
, /* Succeeds if at symbol end. */
663 /* Matches any character whose syntax is specified. Followed by
664 a byte which contains a syntax code, e.g., Sword. */
667 /* Matches any character whose syntax is not that specified. */
671 ,before_dot
, /* Succeeds if before point. */
672 at_dot
, /* Succeeds if at point. */
673 after_dot
, /* Succeeds if after point. */
675 /* Matches any character whose category-set contains the specified
676 category. The operator is followed by a byte which contains a
677 category code (mnemonic ASCII character). */
680 /* Matches any character whose category-set does not contain the
681 specified category. The operator is followed by a byte which
682 contains the category code (mnemonic ASCII character). */
687 /* Common operations on the compiled pattern. */
689 /* Store NUMBER in two contiguous bytes starting at DESTINATION. */
691 #define STORE_NUMBER(destination, number) \
693 (destination)[0] = (number) & 0377; \
694 (destination)[1] = (number) >> 8; \
697 /* Same as STORE_NUMBER, except increment DESTINATION to
698 the byte after where the number is stored. Therefore, DESTINATION
699 must be an lvalue. */
701 #define STORE_NUMBER_AND_INCR(destination, number) \
703 STORE_NUMBER (destination, number); \
704 (destination) += 2; \
707 /* Put into DESTINATION a number stored in two contiguous bytes starting
710 #define EXTRACT_NUMBER(destination, source) \
711 ((destination) = extract_number (source))
714 extract_number (re_char
*source
)
716 unsigned leading_byte
= SIGN_EXTEND_CHAR (source
[1]);
717 return (leading_byte
<< 8) + source
[0];
720 /* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
721 SOURCE must be an lvalue. */
723 #define EXTRACT_NUMBER_AND_INCR(destination, source) \
724 ((destination) = extract_number_and_incr (&source))
727 extract_number_and_incr (re_char
**source
)
729 int num
= extract_number (*source
);
734 /* Store a multibyte character in three contiguous bytes starting
735 DESTINATION, and increment DESTINATION to the byte after where the
736 character is stored. Therefore, DESTINATION must be an lvalue. */
738 #define STORE_CHARACTER_AND_INCR(destination, character) \
740 (destination)[0] = (character) & 0377; \
741 (destination)[1] = ((character) >> 8) & 0377; \
742 (destination)[2] = (character) >> 16; \
743 (destination) += 3; \
746 /* Put into DESTINATION a character stored in three contiguous bytes
747 starting at SOURCE. */
749 #define EXTRACT_CHARACTER(destination, source) \
751 (destination) = ((source)[0] \
752 | ((source)[1] << 8) \
753 | ((source)[2] << 16)); \
757 /* Macros for charset. */
759 /* Size of bitmap of charset P in bytes. P is a start of charset,
760 i.e. *P is (re_opcode_t) charset or (re_opcode_t) charset_not. */
761 #define CHARSET_BITMAP_SIZE(p) ((p)[1] & 0x7F)
763 /* Nonzero if charset P has range table. */
764 #define CHARSET_RANGE_TABLE_EXISTS_P(p) ((p)[1] & 0x80)
766 /* Return the address of range table of charset P. But not the start
767 of table itself, but the before where the number of ranges is
768 stored. `2 +' means to skip re_opcode_t and size of bitmap,
769 and the 2 bytes of flags at the start of the range table. */
770 #define CHARSET_RANGE_TABLE(p) (&(p)[4 + CHARSET_BITMAP_SIZE (p)])
773 /* Extract the bit flags that start a range table. */
774 #define CHARSET_RANGE_TABLE_BITS(p) \
775 ((p)[2 + CHARSET_BITMAP_SIZE (p)] \
776 + (p)[3 + CHARSET_BITMAP_SIZE (p)] * 0x100)
779 /* Return the address of end of RANGE_TABLE. COUNT is number of
780 ranges (which is a pair of (start, end)) in the RANGE_TABLE. `* 2'
781 is start of range and end of range. `* 3' is size of each start
783 #define CHARSET_RANGE_TABLE_END(range_table, count) \
784 ((range_table) + (count) * 2 * 3)
786 /* Test if C is in RANGE_TABLE. A flag NOT is negated if C is in.
787 COUNT is number of ranges in RANGE_TABLE. */
788 #define CHARSET_LOOKUP_RANGE_TABLE_RAW(not, c, range_table, count) \
791 re_wchar_t range_start, range_end; \
793 re_char *range_table_end \
794 = CHARSET_RANGE_TABLE_END ((range_table), (count)); \
796 for (rtp = (range_table); rtp < range_table_end; rtp += 2 * 3) \
798 EXTRACT_CHARACTER (range_start, rtp); \
799 EXTRACT_CHARACTER (range_end, rtp + 3); \
801 if (range_start <= (c) && (c) <= range_end) \
810 /* Test if C is in range table of CHARSET. The flag NOT is negated if
811 C is listed in it. */
812 #define CHARSET_LOOKUP_RANGE_TABLE(not, c, charset) \
815 /* Number of ranges in range table. */ \
817 re_char *range_table = CHARSET_RANGE_TABLE (charset); \
819 EXTRACT_NUMBER_AND_INCR (count, range_table); \
820 CHARSET_LOOKUP_RANGE_TABLE_RAW ((not), (c), range_table, count); \
824 /* If DEBUG is defined, Regex prints many voluminous messages about what
825 it is doing (if the variable `debug' is nonzero). If linked with the
826 main program in `iregex.c', you can enter patterns and strings
827 interactively. And if linked with the main program in `main.c' and
828 the other test files, you can run the already-written tests. */
832 /* We use standard I/O for debugging. */
835 /* It is useful to test things that ``must'' be true when debugging. */
838 static int debug
= -100000;
840 # define DEBUG_STATEMENT(e) e
841 # define DEBUG_PRINT(...) if (debug > 0) printf (__VA_ARGS__)
842 # define DEBUG_COMPILES_ARGUMENTS
843 # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) \
844 if (debug > 0) print_partial_compiled_pattern (s, e)
845 # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \
846 if (debug > 0) print_double_string (w, s1, sz1, s2, sz2)
849 /* Print the fastmap in human-readable form. */
852 print_fastmap (char *fastmap
)
854 unsigned was_a_range
= 0;
857 while (i
< (1 << BYTEWIDTH
))
863 while (i
< (1 << BYTEWIDTH
) && fastmap
[i
])
879 /* Print a compiled pattern string in human-readable form, starting at
880 the START pointer into it and ending just before the pointer END. */
883 print_partial_compiled_pattern (re_char
*start
, re_char
*end
)
891 fprintf (stderr
, "(null)\n");
895 /* Loop over pattern commands. */
898 fprintf (stderr
, "%td:\t", p
- start
);
900 switch ((re_opcode_t
) *p
++)
903 fprintf (stderr
, "/no_op");
907 fprintf (stderr
, "/succeed");
912 fprintf (stderr
, "/exactn/%d", mcnt
);
915 fprintf (stderr
, "/%c", *p
++);
921 fprintf (stderr
, "/start_memory/%d", *p
++);
925 fprintf (stderr
, "/stop_memory/%d", *p
++);
929 fprintf (stderr
, "/duplicate/%d", *p
++);
933 fprintf (stderr
, "/anychar");
939 register int c
, last
= -100;
940 register int in_range
= 0;
941 int length
= CHARSET_BITMAP_SIZE (p
- 1);
942 int has_range_table
= CHARSET_RANGE_TABLE_EXISTS_P (p
- 1);
944 fprintf (stderr
, "/charset [%s",
945 (re_opcode_t
) *(p
- 1) == charset_not
? "^" : "");
948 fprintf (stderr
, " !extends past end of pattern! ");
950 for (c
= 0; c
< 256; c
++)
952 && (p
[1 + (c
/8)] & (1 << (c
% 8))))
954 /* Are we starting a range? */
955 if (last
+ 1 == c
&& ! in_range
)
957 fprintf (stderr
, "-");
960 /* Have we broken a range? */
961 else if (last
+ 1 != c
&& in_range
)
963 fprintf (stderr
, "%c", last
);
968 fprintf (stderr
, "%c", c
);
974 fprintf (stderr
, "%c", last
);
976 fprintf (stderr
, "]");
983 fprintf (stderr
, "has-range-table");
985 /* ??? Should print the range table; for now, just skip it. */
986 p
+= 2; /* skip range table bits */
987 EXTRACT_NUMBER_AND_INCR (count
, p
);
988 p
= CHARSET_RANGE_TABLE_END (p
, count
);
994 fprintf (stderr
, "/begline");
998 fprintf (stderr
, "/endline");
1001 case on_failure_jump
:
1002 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
1003 fprintf (stderr
, "/on_failure_jump to %td", p
+ mcnt
- start
);
1006 case on_failure_keep_string_jump
:
1007 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
1008 fprintf (stderr
, "/on_failure_keep_string_jump to %td",
1012 case on_failure_jump_nastyloop
:
1013 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
1014 fprintf (stderr
, "/on_failure_jump_nastyloop to %td",
1018 case on_failure_jump_loop
:
1019 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
1020 fprintf (stderr
, "/on_failure_jump_loop to %td",
1024 case on_failure_jump_smart
:
1025 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
1026 fprintf (stderr
, "/on_failure_jump_smart to %td",
1031 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
1032 fprintf (stderr
, "/jump to %td", p
+ mcnt
- start
);
1036 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
1037 EXTRACT_NUMBER_AND_INCR (mcnt2
, p
);
1038 fprintf (stderr
, "/succeed_n to %td, %d times",
1039 p
- 2 + mcnt
- start
, mcnt2
);
1043 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
1044 EXTRACT_NUMBER_AND_INCR (mcnt2
, p
);
1045 fprintf (stderr
, "/jump_n to %td, %d times",
1046 p
- 2 + mcnt
- start
, mcnt2
);
1050 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
1051 EXTRACT_NUMBER_AND_INCR (mcnt2
, p
);
1052 fprintf (stderr
, "/set_number_at location %td to %d",
1053 p
- 2 + mcnt
- start
, mcnt2
);
1057 fprintf (stderr
, "/wordbound");
1061 fprintf (stderr
, "/notwordbound");
1065 fprintf (stderr
, "/wordbeg");
1069 fprintf (stderr
, "/wordend");
1073 fprintf (stderr
, "/symbeg");
1077 fprintf (stderr
, "/symend");
1081 fprintf (stderr
, "/syntaxspec");
1083 fprintf (stderr
, "/%d", mcnt
);
1087 fprintf (stderr
, "/notsyntaxspec");
1089 fprintf (stderr
, "/%d", mcnt
);
1094 fprintf (stderr
, "/before_dot");
1098 fprintf (stderr
, "/at_dot");
1102 fprintf (stderr
, "/after_dot");
1106 fprintf (stderr
, "/categoryspec");
1108 fprintf (stderr
, "/%d", mcnt
);
1111 case notcategoryspec
:
1112 fprintf (stderr
, "/notcategoryspec");
1114 fprintf (stderr
, "/%d", mcnt
);
1119 fprintf (stderr
, "/begbuf");
1123 fprintf (stderr
, "/endbuf");
1127 fprintf (stderr
, "?%d", *(p
-1));
1130 fprintf (stderr
, "\n");
1133 fprintf (stderr
, "%td:\tend of pattern.\n", p
- start
);
1138 print_compiled_pattern (struct re_pattern_buffer
*bufp
)
1140 re_char
*buffer
= bufp
->buffer
;
1142 print_partial_compiled_pattern (buffer
, buffer
+ bufp
->used
);
1143 printf ("%ld bytes used/%ld bytes allocated.\n",
1144 bufp
->used
, bufp
->allocated
);
1146 if (bufp
->fastmap_accurate
&& bufp
->fastmap
)
1148 printf ("fastmap: ");
1149 print_fastmap (bufp
->fastmap
);
1152 printf ("re_nsub: %zu\t", bufp
->re_nsub
);
1153 printf ("regs_alloc: %d\t", bufp
->regs_allocated
);
1154 printf ("can_be_null: %d\t", bufp
->can_be_null
);
1155 printf ("no_sub: %d\t", bufp
->no_sub
);
1156 printf ("not_bol: %d\t", bufp
->not_bol
);
1157 printf ("not_eol: %d\t", bufp
->not_eol
);
1158 printf ("syntax: %lx\n", bufp
->syntax
);
1160 /* Perhaps we should print the translate table? */
1165 print_double_string (re_char
*where
, re_char
*string1
, ssize_t size1
,
1166 re_char
*string2
, ssize_t size2
)
1174 if (FIRST_STRING_P (where
))
1176 for (this_char
= where
- string1
; this_char
< size1
; this_char
++)
1177 putchar (string1
[this_char
]);
1182 for (this_char
= where
- string2
; this_char
< size2
; this_char
++)
1183 putchar (string2
[this_char
]);
1187 #else /* not DEBUG */
1192 # define DEBUG_STATEMENT(e)
1193 # define DEBUG_PRINT(...)
1194 # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
1195 # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
1197 #endif /* not DEBUG */
1199 /* Use this to suppress gcc's `...may be used before initialized' warnings. */
1201 # define IF_LINT(Code) Code
1203 # define IF_LINT(Code) /* empty */
1206 /* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
1207 also be assigned to arbitrarily: each pattern buffer stores its own
1208 syntax, so it can be changed between regex compilations. */
1209 /* This has no initializer because initialized variables in Emacs
1210 become read-only after dumping. */
1211 reg_syntax_t re_syntax_options
;
1214 /* Specify the precise syntax of regexps for compilation. This provides
1215 for compatibility for various utilities which historically have
1216 different, incompatible syntaxes.
1218 The argument SYNTAX is a bit mask comprised of the various bits
1219 defined in regex.h. We return the old syntax. */
1222 re_set_syntax (reg_syntax_t syntax
)
1224 reg_syntax_t ret
= re_syntax_options
;
1226 re_syntax_options
= syntax
;
1229 WEAK_ALIAS (__re_set_syntax
, re_set_syntax
)
1231 /* Regexp to use to replace spaces, or NULL meaning don't. */
1232 static const_re_char
*whitespace_regexp
;
1235 re_set_whitespace_regexp (const char *regexp
)
1237 whitespace_regexp
= (const_re_char
*) regexp
;
1239 WEAK_ALIAS (__re_set_syntax
, re_set_syntax
)
1241 /* This table gives an error message for each of the error codes listed
1242 in regex.h. Obviously the order here has to be same as there.
1243 POSIX doesn't require that we do anything for REG_NOERROR,
1244 but why not be nice? */
1246 static const char *re_error_msgid
[] =
1248 gettext_noop ("Success"), /* REG_NOERROR */
1249 gettext_noop ("No match"), /* REG_NOMATCH */
1250 gettext_noop ("Invalid regular expression"), /* REG_BADPAT */
1251 gettext_noop ("Invalid collation character"), /* REG_ECOLLATE */
1252 gettext_noop ("Invalid character class name"), /* REG_ECTYPE */
1253 gettext_noop ("Trailing backslash"), /* REG_EESCAPE */
1254 gettext_noop ("Invalid back reference"), /* REG_ESUBREG */
1255 gettext_noop ("Unmatched [ or [^"), /* REG_EBRACK */
1256 gettext_noop ("Unmatched ( or \\("), /* REG_EPAREN */
1257 gettext_noop ("Unmatched \\{"), /* REG_EBRACE */
1258 gettext_noop ("Invalid content of \\{\\}"), /* REG_BADBR */
1259 gettext_noop ("Invalid range end"), /* REG_ERANGE */
1260 gettext_noop ("Memory exhausted"), /* REG_ESPACE */
1261 gettext_noop ("Invalid preceding regular expression"), /* REG_BADRPT */
1262 gettext_noop ("Premature end of regular expression"), /* REG_EEND */
1263 gettext_noop ("Regular expression too big"), /* REG_ESIZE */
1264 gettext_noop ("Unmatched ) or \\)"), /* REG_ERPAREN */
1265 gettext_noop ("Range striding over charsets") /* REG_ERANGEX */
1268 /* Avoiding alloca during matching, to placate r_alloc. */
1270 /* Define MATCH_MAY_ALLOCATE unless we need to make sure that the
1271 searching and matching functions should not call alloca. On some
1272 systems, alloca is implemented in terms of malloc, and if we're
1273 using the relocating allocator routines, then malloc could cause a
1274 relocation, which might (if the strings being searched are in the
1275 ralloc heap) shift the data out from underneath the regexp
1278 Here's another reason to avoid allocation: Emacs
1279 processes input from X in a signal handler; processing X input may
1280 call malloc; if input arrives while a matching routine is calling
1281 malloc, then we're scrod. But Emacs can't just block input while
1282 calling matching routines; then we don't notice interrupts when
1283 they come in. So, Emacs blocks input around all regexp calls
1284 except the matching calls, which it leaves unprotected, in the
1285 faith that they will not malloc. */
1287 /* Normally, this is fine. */
1288 #define MATCH_MAY_ALLOCATE
1290 /* The match routines may not allocate if (1) they would do it with malloc
1291 and (2) it's not safe for them to use malloc.
1292 Note that if REL_ALLOC is defined, matching would not use malloc for the
1293 failure stack, but we would still use it for the register vectors;
1294 so REL_ALLOC should not affect this. */
1295 #if defined REGEX_MALLOC && defined emacs
1296 # undef MATCH_MAY_ALLOCATE
1300 /* Failure stack declarations and macros; both re_compile_fastmap and
1301 re_match_2 use a failure stack. These have to be macros because of
1302 REGEX_ALLOCATE_STACK. */
1305 /* Approximate number of failure points for which to initially allocate space
1306 when matching. If this number is exceeded, we allocate more
1307 space, so it is not a hard limit. */
1308 #ifndef INIT_FAILURE_ALLOC
1309 # define INIT_FAILURE_ALLOC 20
1312 /* Roughly the maximum number of failure points on the stack. Would be
1313 exactly that if always used TYPICAL_FAILURE_SIZE items each time we failed.
1314 This is a variable only so users of regex can assign to it; we never
1315 change it ourselves. We always multiply it by TYPICAL_FAILURE_SIZE
1316 before using it, so it should probably be a byte-count instead. */
1317 # if defined MATCH_MAY_ALLOCATE
1318 /* Note that 4400 was enough to cause a crash on Alpha OSF/1,
1319 whose default stack limit is 2mb. In order for a larger
1320 value to work reliably, you have to try to make it accord
1321 with the process stack limit. */
1322 size_t re_max_failures
= 40000;
1324 size_t re_max_failures
= 4000;
1327 union fail_stack_elt
1330 /* This should be the biggest `int' that's no bigger than a pointer. */
1334 typedef union fail_stack_elt fail_stack_elt_t
;
1338 fail_stack_elt_t
*stack
;
1340 size_t avail
; /* Offset of next open position. */
1341 size_t frame
; /* Offset of the cur constructed frame. */
1344 #define FAIL_STACK_EMPTY() (fail_stack.frame == 0)
1347 /* Define macros to initialize and free the failure stack.
1348 Do `return -2' if the alloc fails. */
1350 #ifdef MATCH_MAY_ALLOCATE
1351 # define INIT_FAIL_STACK() \
1353 fail_stack.stack = \
1354 REGEX_ALLOCATE_STACK (INIT_FAILURE_ALLOC * TYPICAL_FAILURE_SIZE \
1355 * sizeof (fail_stack_elt_t)); \
1357 if (fail_stack.stack == NULL) \
1360 fail_stack.size = INIT_FAILURE_ALLOC; \
1361 fail_stack.avail = 0; \
1362 fail_stack.frame = 0; \
1365 # define INIT_FAIL_STACK() \
1367 fail_stack.avail = 0; \
1368 fail_stack.frame = 0; \
1371 # define RETALLOC_IF(addr, n, t) \
1372 if (addr) RETALLOC((addr), (n), t); else (addr) = TALLOC ((n), t)
1376 /* Double the size of FAIL_STACK, up to a limit
1377 which allows approximately `re_max_failures' items.
1379 Return 1 if succeeds, and 0 if either ran out of memory
1380 allocating space for it or it was already too large.
1382 REGEX_REALLOCATE_STACK requires `destination' be declared. */
1384 /* Factor to increase the failure stack size by
1385 when we increase it.
1386 This used to be 2, but 2 was too wasteful
1387 because the old discarded stacks added up to as much space
1388 were as ultimate, maximum-size stack. */
1389 #define FAIL_STACK_GROWTH_FACTOR 4
1391 #define GROW_FAIL_STACK(fail_stack) \
1392 (((fail_stack).size * sizeof (fail_stack_elt_t) \
1393 >= re_max_failures * TYPICAL_FAILURE_SIZE) \
1395 : ((fail_stack).stack \
1396 = REGEX_REALLOCATE_STACK ((fail_stack).stack, \
1397 (fail_stack).size * sizeof (fail_stack_elt_t), \
1398 MIN (re_max_failures * TYPICAL_FAILURE_SIZE, \
1399 ((fail_stack).size * sizeof (fail_stack_elt_t) \
1400 * FAIL_STACK_GROWTH_FACTOR))), \
1402 (fail_stack).stack == NULL \
1404 : ((fail_stack).size \
1405 = (MIN (re_max_failures * TYPICAL_FAILURE_SIZE, \
1406 ((fail_stack).size * sizeof (fail_stack_elt_t) \
1407 * FAIL_STACK_GROWTH_FACTOR)) \
1408 / sizeof (fail_stack_elt_t)), \
1412 /* Push a pointer value onto the failure stack.
1413 Assumes the variable `fail_stack'. Probably should only
1414 be called from within `PUSH_FAILURE_POINT'. */
1415 #define PUSH_FAILURE_POINTER(item) \
1416 fail_stack.stack[fail_stack.avail++].pointer = (item)
1418 /* This pushes an integer-valued item onto the failure stack.
1419 Assumes the variable `fail_stack'. Probably should only
1420 be called from within `PUSH_FAILURE_POINT'. */
1421 #define PUSH_FAILURE_INT(item) \
1422 fail_stack.stack[fail_stack.avail++].integer = (item)
1424 /* These POP... operations complement the PUSH... operations.
1425 All assume that `fail_stack' is nonempty. */
1426 #define POP_FAILURE_POINTER() fail_stack.stack[--fail_stack.avail].pointer
1427 #define POP_FAILURE_INT() fail_stack.stack[--fail_stack.avail].integer
1429 /* Individual items aside from the registers. */
1430 #define NUM_NONREG_ITEMS 3
1432 /* Used to examine the stack (to detect infinite loops). */
1433 #define FAILURE_PAT(h) fail_stack.stack[(h) - 1].pointer
1434 #define FAILURE_STR(h) (fail_stack.stack[(h) - 2].pointer)
1435 #define NEXT_FAILURE_HANDLE(h) fail_stack.stack[(h) - 3].integer
1436 #define TOP_FAILURE_HANDLE() fail_stack.frame
1439 #define ENSURE_FAIL_STACK(space) \
1440 while (REMAINING_AVAIL_SLOTS <= space) { \
1441 if (!GROW_FAIL_STACK (fail_stack)) \
1443 DEBUG_PRINT ("\n Doubled stack; size now: %zd\n", (fail_stack).size);\
1444 DEBUG_PRINT (" slots available: %zd\n", REMAINING_AVAIL_SLOTS);\
1447 /* Push register NUM onto the stack. */
1448 #define PUSH_FAILURE_REG(num) \
1450 char *destination; \
1452 ENSURE_FAIL_STACK(3); \
1453 DEBUG_PRINT (" Push reg %ld (spanning %p -> %p)\n", \
1454 n, regstart[n], regend[n]); \
1455 PUSH_FAILURE_POINTER (regstart[n]); \
1456 PUSH_FAILURE_POINTER (regend[n]); \
1457 PUSH_FAILURE_INT (n); \
1460 /* Change the counter's value to VAL, but make sure that it will
1461 be reset when backtracking. */
1462 #define PUSH_NUMBER(ptr,val) \
1464 char *destination; \
1466 ENSURE_FAIL_STACK(3); \
1467 EXTRACT_NUMBER (c, ptr); \
1468 DEBUG_PRINT (" Push number %p = %d -> %d\n", ptr, c, val); \
1469 PUSH_FAILURE_INT (c); \
1470 PUSH_FAILURE_POINTER (ptr); \
1471 PUSH_FAILURE_INT (-1); \
1472 STORE_NUMBER (ptr, val); \
1475 /* Pop a saved register off the stack. */
1476 #define POP_FAILURE_REG_OR_COUNT() \
1478 long pfreg = POP_FAILURE_INT (); \
1481 /* It's a counter. */ \
1482 /* Here, we discard `const', making re_match non-reentrant. */ \
1483 unsigned char *ptr = (unsigned char*) POP_FAILURE_POINTER (); \
1484 pfreg = POP_FAILURE_INT (); \
1485 STORE_NUMBER (ptr, pfreg); \
1486 DEBUG_PRINT (" Pop counter %p = %ld\n", ptr, pfreg); \
1490 regend[pfreg] = POP_FAILURE_POINTER (); \
1491 regstart[pfreg] = POP_FAILURE_POINTER (); \
1492 DEBUG_PRINT (" Pop reg %ld (spanning %p -> %p)\n", \
1493 pfreg, regstart[pfreg], regend[pfreg]); \
1497 /* Check that we are not stuck in an infinite loop. */
1498 #define CHECK_INFINITE_LOOP(pat_cur, string_place) \
1500 ssize_t failure = TOP_FAILURE_HANDLE (); \
1501 /* Check for infinite matching loops */ \
1502 while (failure > 0 \
1503 && (FAILURE_STR (failure) == string_place \
1504 || FAILURE_STR (failure) == NULL)) \
1506 assert (FAILURE_PAT (failure) >= bufp->buffer \
1507 && FAILURE_PAT (failure) <= bufp->buffer + bufp->used); \
1508 if (FAILURE_PAT (failure) == pat_cur) \
1513 DEBUG_PRINT (" Other pattern: %p\n", FAILURE_PAT (failure)); \
1514 failure = NEXT_FAILURE_HANDLE(failure); \
1516 DEBUG_PRINT (" Other string: %p\n", FAILURE_STR (failure)); \
1519 /* Push the information about the state we will need
1520 if we ever fail back to it.
1522 Requires variables fail_stack, regstart, regend and
1523 num_regs be declared. GROW_FAIL_STACK requires `destination' be
1526 Does `return FAILURE_CODE' if runs out of memory. */
1528 #define PUSH_FAILURE_POINT(pattern, string_place) \
1530 char *destination; \
1531 /* Must be int, so when we don't save any registers, the arithmetic \
1532 of 0 + -1 isn't done as unsigned. */ \
1534 DEBUG_STATEMENT (nfailure_points_pushed++); \
1535 DEBUG_PRINT ("\nPUSH_FAILURE_POINT:\n"); \
1536 DEBUG_PRINT (" Before push, next avail: %zd\n", (fail_stack).avail); \
1537 DEBUG_PRINT (" size: %zd\n", (fail_stack).size);\
1539 ENSURE_FAIL_STACK (NUM_NONREG_ITEMS); \
1541 DEBUG_PRINT ("\n"); \
1543 DEBUG_PRINT (" Push frame index: %zd\n", fail_stack.frame); \
1544 PUSH_FAILURE_INT (fail_stack.frame); \
1546 DEBUG_PRINT (" Push string %p: `", string_place); \
1547 DEBUG_PRINT_DOUBLE_STRING (string_place, string1, size1, string2, size2);\
1548 DEBUG_PRINT ("'\n"); \
1549 PUSH_FAILURE_POINTER (string_place); \
1551 DEBUG_PRINT (" Push pattern %p: ", pattern); \
1552 DEBUG_PRINT_COMPILED_PATTERN (bufp, pattern, pend); \
1553 PUSH_FAILURE_POINTER (pattern); \
1555 /* Close the frame by moving the frame pointer past it. */ \
1556 fail_stack.frame = fail_stack.avail; \
1559 /* Estimate the size of data pushed by a typical failure stack entry.
1560 An estimate is all we need, because all we use this for
1561 is to choose a limit for how big to make the failure stack. */
1562 /* BEWARE, the value `20' is hard-coded in emacs.c:main(). */
1563 #define TYPICAL_FAILURE_SIZE 20
1565 /* How many items can still be added to the stack without overflowing it. */
1566 #define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail)
1569 /* Pops what PUSH_FAIL_STACK pushes.
1571 We restore into the parameters, all of which should be lvalues:
1572 STR -- the saved data position.
1573 PAT -- the saved pattern position.
1574 REGSTART, REGEND -- arrays of string positions.
1576 Also assumes the variables `fail_stack' and (if debugging), `bufp',
1577 `pend', `string1', `size1', `string2', and `size2'. */
1579 #define POP_FAILURE_POINT(str, pat) \
1581 assert (!FAIL_STACK_EMPTY ()); \
1583 /* Remove failure points and point to how many regs pushed. */ \
1584 DEBUG_PRINT ("POP_FAILURE_POINT:\n"); \
1585 DEBUG_PRINT (" Before pop, next avail: %zd\n", fail_stack.avail); \
1586 DEBUG_PRINT (" size: %zd\n", fail_stack.size); \
1588 /* Pop the saved registers. */ \
1589 while (fail_stack.frame < fail_stack.avail) \
1590 POP_FAILURE_REG_OR_COUNT (); \
1592 pat = POP_FAILURE_POINTER (); \
1593 DEBUG_PRINT (" Popping pattern %p: ", pat); \
1594 DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend); \
1596 /* If the saved string location is NULL, it came from an \
1597 on_failure_keep_string_jump opcode, and we want to throw away the \
1598 saved NULL, thus retaining our current position in the string. */ \
1599 str = POP_FAILURE_POINTER (); \
1600 DEBUG_PRINT (" Popping string %p: `", str); \
1601 DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2); \
1602 DEBUG_PRINT ("'\n"); \
1604 fail_stack.frame = POP_FAILURE_INT (); \
1605 DEBUG_PRINT (" Popping frame index: %zd\n", fail_stack.frame); \
1607 assert (fail_stack.avail >= 0); \
1608 assert (fail_stack.frame <= fail_stack.avail); \
1610 DEBUG_STATEMENT (nfailure_points_popped++); \
1611 } while (0) /* POP_FAILURE_POINT */
1615 /* Registers are set to a sentinel when they haven't yet matched. */
1616 #define REG_UNSET(e) ((e) == NULL)
1618 /* Subroutine declarations and macros for regex_compile. */
1620 static reg_errcode_t
regex_compile (re_char
*pattern
, size_t size
,
1621 reg_syntax_t syntax
,
1622 struct re_pattern_buffer
*bufp
);
1623 static void store_op1 (re_opcode_t op
, unsigned char *loc
, int arg
);
1624 static void store_op2 (re_opcode_t op
, unsigned char *loc
, int arg1
, int arg2
);
1625 static void insert_op1 (re_opcode_t op
, unsigned char *loc
,
1626 int arg
, unsigned char *end
);
1627 static void insert_op2 (re_opcode_t op
, unsigned char *loc
,
1628 int arg1
, int arg2
, unsigned char *end
);
1629 static boolean
at_begline_loc_p (re_char
*pattern
, re_char
*p
,
1630 reg_syntax_t syntax
);
1631 static boolean
at_endline_loc_p (re_char
*p
, re_char
*pend
,
1632 reg_syntax_t syntax
);
1633 static re_char
*skip_one_char (re_char
*p
);
1634 static int analyse_first (re_char
*p
, re_char
*pend
,
1635 char *fastmap
, const int multibyte
);
1637 /* Fetch the next character in the uncompiled pattern, with no
1639 #define PATFETCH(c) \
1642 if (p == pend) return REG_EEND; \
1643 c = RE_STRING_CHAR_AND_LENGTH (p, len, multibyte); \
1648 /* If `translate' is non-null, return translate[D], else just D. We
1649 cast the subscript to translate because some data is declared as
1650 `char *', to avoid warnings when a string constant is passed. But
1651 when we use a character as a subscript we must make it unsigned. */
1653 # define TRANSLATE(d) \
1654 (RE_TRANSLATE_P (translate) ? RE_TRANSLATE (translate, (d)) : (d))
1658 /* Macros for outputting the compiled pattern into `buffer'. */
1660 /* If the buffer isn't allocated when it comes in, use this. */
1661 #define INIT_BUF_SIZE 32
1663 /* Make sure we have at least N more bytes of space in buffer. */
1664 #define GET_BUFFER_SPACE(n) \
1665 while ((size_t) (b - bufp->buffer + (n)) > bufp->allocated) \
1668 /* Make sure we have one more byte of buffer space and then add C to it. */
1669 #define BUF_PUSH(c) \
1671 GET_BUFFER_SPACE (1); \
1672 *b++ = (unsigned char) (c); \
1676 /* Ensure we have two more bytes of buffer space and then append C1 and C2. */
1677 #define BUF_PUSH_2(c1, c2) \
1679 GET_BUFFER_SPACE (2); \
1680 *b++ = (unsigned char) (c1); \
1681 *b++ = (unsigned char) (c2); \
1685 /* Store a jump with opcode OP at LOC to location TO. We store a
1686 relative address offset by the three bytes the jump itself occupies. */
1687 #define STORE_JUMP(op, loc, to) \
1688 store_op1 (op, loc, (to) - (loc) - 3)
1690 /* Likewise, for a two-argument jump. */
1691 #define STORE_JUMP2(op, loc, to, arg) \
1692 store_op2 (op, loc, (to) - (loc) - 3, arg)
1694 /* Like `STORE_JUMP', but for inserting. Assume `b' is the buffer end. */
1695 #define INSERT_JUMP(op, loc, to) \
1696 insert_op1 (op, loc, (to) - (loc) - 3, b)
1698 /* Like `STORE_JUMP2', but for inserting. Assume `b' is the buffer end. */
1699 #define INSERT_JUMP2(op, loc, to, arg) \
1700 insert_op2 (op, loc, (to) - (loc) - 3, arg, b)
1703 /* This is not an arbitrary limit: the arguments which represent offsets
1704 into the pattern are two bytes long. So if 2^15 bytes turns out to
1705 be too small, many things would have to change. */
1706 # define MAX_BUF_SIZE (1L << 15)
1708 /* Extend the buffer by twice its current size via realloc and
1709 reset the pointers that pointed into the old block to point to the
1710 correct places in the new one. If extending the buffer results in it
1711 being larger than MAX_BUF_SIZE, then flag memory exhausted. */
1712 #if __BOUNDED_POINTERS__
1713 # define SET_HIGH_BOUND(P) (__ptrhigh (P) = __ptrlow (P) + bufp->allocated)
1714 # define MOVE_BUFFER_POINTER(P) \
1715 (__ptrlow (P) = new_buffer + (__ptrlow (P) - old_buffer), \
1716 SET_HIGH_BOUND (P), \
1717 __ptrvalue (P) = new_buffer + (__ptrvalue (P) - old_buffer))
1718 # define ELSE_EXTEND_BUFFER_HIGH_BOUND \
1721 SET_HIGH_BOUND (b); \
1722 SET_HIGH_BOUND (begalt); \
1723 if (fixup_alt_jump) \
1724 SET_HIGH_BOUND (fixup_alt_jump); \
1726 SET_HIGH_BOUND (laststart); \
1727 if (pending_exact) \
1728 SET_HIGH_BOUND (pending_exact); \
1731 # define MOVE_BUFFER_POINTER(P) ((P) = new_buffer + ((P) - old_buffer))
1732 # define ELSE_EXTEND_BUFFER_HIGH_BOUND
1734 #define EXTEND_BUFFER() \
1736 unsigned char *old_buffer = bufp->buffer; \
1737 if (bufp->allocated == MAX_BUF_SIZE) \
1739 bufp->allocated <<= 1; \
1740 if (bufp->allocated > MAX_BUF_SIZE) \
1741 bufp->allocated = MAX_BUF_SIZE; \
1742 RETALLOC (bufp->buffer, bufp->allocated, unsigned char); \
1743 if (bufp->buffer == NULL) \
1744 return REG_ESPACE; \
1745 /* If the buffer moved, move all the pointers into it. */ \
1746 if (old_buffer != bufp->buffer) \
1748 unsigned char *new_buffer = bufp->buffer; \
1749 MOVE_BUFFER_POINTER (b); \
1750 MOVE_BUFFER_POINTER (begalt); \
1751 if (fixup_alt_jump) \
1752 MOVE_BUFFER_POINTER (fixup_alt_jump); \
1754 MOVE_BUFFER_POINTER (laststart); \
1755 if (pending_exact) \
1756 MOVE_BUFFER_POINTER (pending_exact); \
1758 ELSE_EXTEND_BUFFER_HIGH_BOUND \
1762 /* Since we have one byte reserved for the register number argument to
1763 {start,stop}_memory, the maximum number of groups we can report
1764 things about is what fits in that byte. */
1765 #define MAX_REGNUM 255
1767 /* But patterns can have more than `MAX_REGNUM' registers. We just
1768 ignore the excess. */
1769 typedef int regnum_t
;
1772 /* Macros for the compile stack. */
1774 /* Since offsets can go either forwards or backwards, this type needs to
1775 be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1. */
1776 /* int may be not enough when sizeof(int) == 2. */
1777 typedef long pattern_offset_t
;
1781 pattern_offset_t begalt_offset
;
1782 pattern_offset_t fixup_alt_jump
;
1783 pattern_offset_t laststart_offset
;
1785 } compile_stack_elt_t
;
1790 compile_stack_elt_t
*stack
;
1792 size_t avail
; /* Offset of next open position. */
1793 } compile_stack_type
;
1796 #define INIT_COMPILE_STACK_SIZE 32
1798 #define COMPILE_STACK_EMPTY (compile_stack.avail == 0)
1799 #define COMPILE_STACK_FULL (compile_stack.avail == compile_stack.size)
1801 /* The next available element. */
1802 #define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
1804 /* Explicit quit checking is needed for Emacs, which uses polling to
1805 process input events. */
1807 # define IMMEDIATE_QUIT_CHECK \
1809 if (immediate_quit) QUIT; \
1812 # define IMMEDIATE_QUIT_CHECK ((void)0)
1815 /* Structure to manage work area for range table. */
1816 struct range_table_work_area
1818 int *table
; /* actual work area. */
1819 int allocated
; /* allocated size for work area in bytes. */
1820 int used
; /* actually used size in words. */
1821 int bits
; /* flag to record character classes */
1826 /* Make sure that WORK_AREA can hold more N multibyte characters.
1827 This is used only in set_image_of_range and set_image_of_range_1.
1828 It expects WORK_AREA to be a pointer.
1829 If it can't get the space, it returns from the surrounding function. */
1831 #define EXTEND_RANGE_TABLE(work_area, n) \
1833 if (((work_area).used + (n)) * sizeof (int) > (work_area).allocated) \
1835 extend_range_table_work_area (&work_area); \
1836 if ((work_area).table == 0) \
1837 return (REG_ESPACE); \
1841 #define SET_RANGE_TABLE_WORK_AREA_BIT(work_area, bit) \
1842 (work_area).bits |= (bit)
1844 /* Set a range (RANGE_START, RANGE_END) to WORK_AREA. */
1845 #define SET_RANGE_TABLE_WORK_AREA(work_area, range_start, range_end) \
1847 EXTEND_RANGE_TABLE ((work_area), 2); \
1848 (work_area).table[(work_area).used++] = (range_start); \
1849 (work_area).table[(work_area).used++] = (range_end); \
1854 /* Free allocated memory for WORK_AREA. */
1855 #define FREE_RANGE_TABLE_WORK_AREA(work_area) \
1857 if ((work_area).table) \
1858 free ((work_area).table); \
1861 #define CLEAR_RANGE_TABLE_WORK_USED(work_area) ((work_area).used = 0, (work_area).bits = 0)
1862 #define RANGE_TABLE_WORK_USED(work_area) ((work_area).used)
1863 #define RANGE_TABLE_WORK_BITS(work_area) ((work_area).bits)
1864 #define RANGE_TABLE_WORK_ELT(work_area, i) ((work_area).table[i])
1866 /* Bits used to implement the multibyte-part of the various character classes
1867 such as [:alnum:] in a charset's range table. */
1868 #define BIT_WORD 0x1
1869 #define BIT_LOWER 0x2
1870 #define BIT_PUNCT 0x4
1871 #define BIT_SPACE 0x8
1872 #define BIT_UPPER 0x10
1873 #define BIT_MULTIBYTE 0x20
1876 /* Set the bit for character C in a list. */
1877 #define SET_LIST_BIT(c) (b[((c)) / BYTEWIDTH] |= 1 << ((c) % BYTEWIDTH))
1882 /* Store characters in the range FROM to TO in the bitmap at B (for
1883 ASCII and unibyte characters) and WORK_AREA (for multibyte
1884 characters) while translating them and paying attention to the
1885 continuity of translated characters.
1887 Implementation note: It is better to implement these fairly big
1888 macros by a function, but it's not that easy because macros called
1889 in this macro assume various local variables already declared. */
1891 /* Both FROM and TO are ASCII characters. */
1893 #define SETUP_ASCII_RANGE(work_area, FROM, TO) \
1897 for (C0 = (FROM); C0 <= (TO); C0++) \
1899 C1 = TRANSLATE (C0); \
1900 if (! ASCII_CHAR_P (C1)) \
1902 SET_RANGE_TABLE_WORK_AREA ((work_area), C1, C1); \
1903 if ((C1 = RE_CHAR_TO_UNIBYTE (C1)) < 0) \
1906 SET_LIST_BIT (C1); \
1911 /* Both FROM and TO are unibyte characters (0x80..0xFF). */
1913 #define SETUP_UNIBYTE_RANGE(work_area, FROM, TO) \
1915 int C0, C1, C2, I; \
1916 int USED = RANGE_TABLE_WORK_USED (work_area); \
1918 for (C0 = (FROM); C0 <= (TO); C0++) \
1920 C1 = RE_CHAR_TO_MULTIBYTE (C0); \
1921 if (CHAR_BYTE8_P (C1)) \
1922 SET_LIST_BIT (C0); \
1925 C2 = TRANSLATE (C1); \
1927 || (C1 = RE_CHAR_TO_UNIBYTE (C2)) < 0) \
1929 SET_LIST_BIT (C1); \
1930 for (I = RANGE_TABLE_WORK_USED (work_area) - 2; I >= USED; I -= 2) \
1932 int from = RANGE_TABLE_WORK_ELT (work_area, I); \
1933 int to = RANGE_TABLE_WORK_ELT (work_area, I + 1); \
1935 if (C2 >= from - 1 && C2 <= to + 1) \
1937 if (C2 == from - 1) \
1938 RANGE_TABLE_WORK_ELT (work_area, I)--; \
1939 else if (C2 == to + 1) \
1940 RANGE_TABLE_WORK_ELT (work_area, I + 1)++; \
1945 SET_RANGE_TABLE_WORK_AREA ((work_area), C2, C2); \
1951 /* Both FROM and TO are multibyte characters. */
1953 #define SETUP_MULTIBYTE_RANGE(work_area, FROM, TO) \
1955 int C0, C1, C2, I, USED = RANGE_TABLE_WORK_USED (work_area); \
1957 SET_RANGE_TABLE_WORK_AREA ((work_area), (FROM), (TO)); \
1958 for (C0 = (FROM); C0 <= (TO); C0++) \
1960 C1 = TRANSLATE (C0); \
1961 if ((C2 = RE_CHAR_TO_UNIBYTE (C1)) >= 0 \
1962 || (C1 != C0 && (C2 = RE_CHAR_TO_UNIBYTE (C0)) >= 0)) \
1963 SET_LIST_BIT (C2); \
1964 if (C1 >= (FROM) && C1 <= (TO)) \
1966 for (I = RANGE_TABLE_WORK_USED (work_area) - 2; I >= USED; I -= 2) \
1968 int from = RANGE_TABLE_WORK_ELT (work_area, I); \
1969 int to = RANGE_TABLE_WORK_ELT (work_area, I + 1); \
1971 if (C1 >= from - 1 && C1 <= to + 1) \
1973 if (C1 == from - 1) \
1974 RANGE_TABLE_WORK_ELT (work_area, I)--; \
1975 else if (C1 == to + 1) \
1976 RANGE_TABLE_WORK_ELT (work_area, I + 1)++; \
1981 SET_RANGE_TABLE_WORK_AREA ((work_area), C1, C1); \
1987 /* Get the next unsigned number in the uncompiled pattern. */
1988 #define GET_INTERVAL_COUNT(num) \
1991 FREE_STACK_RETURN (REG_EBRACE); \
1995 while ('0' <= c && c <= '9') \
1999 if (RE_DUP_MAX / 10 - (RE_DUP_MAX % 10 < c - '0') < num) \
2000 FREE_STACK_RETURN (REG_BADBR); \
2001 num = num * 10 + c - '0'; \
2003 FREE_STACK_RETURN (REG_EBRACE); \
2009 #if ! WIDE_CHAR_SUPPORT
2011 /* Map a string to the char class it names (if any). */
2013 re_wctype (const_re_char
*str
)
2015 const char *string
= (const char *) str
;
2016 if (STREQ (string
, "alnum")) return RECC_ALNUM
;
2017 else if (STREQ (string
, "alpha")) return RECC_ALPHA
;
2018 else if (STREQ (string
, "word")) return RECC_WORD
;
2019 else if (STREQ (string
, "ascii")) return RECC_ASCII
;
2020 else if (STREQ (string
, "nonascii")) return RECC_NONASCII
;
2021 else if (STREQ (string
, "graph")) return RECC_GRAPH
;
2022 else if (STREQ (string
, "lower")) return RECC_LOWER
;
2023 else if (STREQ (string
, "print")) return RECC_PRINT
;
2024 else if (STREQ (string
, "punct")) return RECC_PUNCT
;
2025 else if (STREQ (string
, "space")) return RECC_SPACE
;
2026 else if (STREQ (string
, "upper")) return RECC_UPPER
;
2027 else if (STREQ (string
, "unibyte")) return RECC_UNIBYTE
;
2028 else if (STREQ (string
, "multibyte")) return RECC_MULTIBYTE
;
2029 else if (STREQ (string
, "digit")) return RECC_DIGIT
;
2030 else if (STREQ (string
, "xdigit")) return RECC_XDIGIT
;
2031 else if (STREQ (string
, "cntrl")) return RECC_CNTRL
;
2032 else if (STREQ (string
, "blank")) return RECC_BLANK
;
2036 /* True if CH is in the char class CC. */
2038 re_iswctype (int ch
, re_wctype_t cc
)
2042 case RECC_ALNUM
: return ISALNUM (ch
) != 0;
2043 case RECC_ALPHA
: return ISALPHA (ch
) != 0;
2044 case RECC_BLANK
: return ISBLANK (ch
) != 0;
2045 case RECC_CNTRL
: return ISCNTRL (ch
) != 0;
2046 case RECC_DIGIT
: return ISDIGIT (ch
) != 0;
2047 case RECC_GRAPH
: return ISGRAPH (ch
) != 0;
2048 case RECC_LOWER
: return ISLOWER (ch
) != 0;
2049 case RECC_PRINT
: return ISPRINT (ch
) != 0;
2050 case RECC_PUNCT
: return ISPUNCT (ch
) != 0;
2051 case RECC_SPACE
: return ISSPACE (ch
) != 0;
2052 case RECC_UPPER
: return ISUPPER (ch
) != 0;
2053 case RECC_XDIGIT
: return ISXDIGIT (ch
) != 0;
2054 case RECC_ASCII
: return IS_REAL_ASCII (ch
) != 0;
2055 case RECC_NONASCII
: return !IS_REAL_ASCII (ch
);
2056 case RECC_UNIBYTE
: return ISUNIBYTE (ch
) != 0;
2057 case RECC_MULTIBYTE
: return !ISUNIBYTE (ch
);
2058 case RECC_WORD
: return ISWORD (ch
) != 0;
2059 case RECC_ERROR
: return false;
2065 /* Return a bit-pattern to use in the range-table bits to match multibyte
2066 chars of class CC. */
2068 re_wctype_to_bit (re_wctype_t cc
)
2072 case RECC_NONASCII
: case RECC_PRINT
: case RECC_GRAPH
:
2073 case RECC_MULTIBYTE
: return BIT_MULTIBYTE
;
2074 case RECC_ALPHA
: case RECC_ALNUM
: case RECC_WORD
: return BIT_WORD
;
2075 case RECC_LOWER
: return BIT_LOWER
;
2076 case RECC_UPPER
: return BIT_UPPER
;
2077 case RECC_PUNCT
: return BIT_PUNCT
;
2078 case RECC_SPACE
: return BIT_SPACE
;
2079 case RECC_ASCII
: case RECC_DIGIT
: case RECC_XDIGIT
: case RECC_CNTRL
:
2080 case RECC_BLANK
: case RECC_UNIBYTE
: case RECC_ERROR
: return 0;
2087 /* Filling in the work area of a range. */
2089 /* Actually extend the space in WORK_AREA. */
2092 extend_range_table_work_area (struct range_table_work_area
*work_area
)
2094 work_area
->allocated
+= 16 * sizeof (int);
2095 work_area
->table
= realloc (work_area
->table
, work_area
->allocated
);
2101 /* Carefully find the ranges of codes that are equivalent
2102 under case conversion to the range start..end when passed through
2103 TRANSLATE. Handle the case where non-letters can come in between
2104 two upper-case letters (which happens in Latin-1).
2105 Also handle the case of groups of more than 2 case-equivalent chars.
2107 The basic method is to look at consecutive characters and see
2108 if they can form a run that can be handled as one.
2110 Returns -1 if successful, REG_ESPACE if ran out of space. */
2113 set_image_of_range_1 (struct range_table_work_area
*work_area
,
2114 re_wchar_t start
, re_wchar_t end
,
2115 RE_TRANSLATE_TYPE translate
)
2117 /* `one_case' indicates a character, or a run of characters,
2118 each of which is an isolate (no case-equivalents).
2119 This includes all ASCII non-letters.
2121 `two_case' indicates a character, or a run of characters,
2122 each of which has two case-equivalent forms.
2123 This includes all ASCII letters.
2125 `strange' indicates a character that has more than one
2128 enum case_type
{one_case
, two_case
, strange
};
2130 /* Describe the run that is in progress,
2131 which the next character can try to extend.
2132 If run_type is strange, that means there really is no run.
2133 If run_type is one_case, then run_start...run_end is the run.
2134 If run_type is two_case, then the run is run_start...run_end,
2135 and the case-equivalents end at run_eqv_end. */
2137 enum case_type run_type
= strange
;
2138 int run_start
, run_end
, run_eqv_end
;
2140 Lisp_Object eqv_table
;
2142 if (!RE_TRANSLATE_P (translate
))
2144 EXTEND_RANGE_TABLE (work_area
, 2);
2145 work_area
->table
[work_area
->used
++] = (start
);
2146 work_area
->table
[work_area
->used
++] = (end
);
2150 eqv_table
= XCHAR_TABLE (translate
)->extras
[2];
2152 for (; start
<= end
; start
++)
2154 enum case_type this_type
;
2155 int eqv
= RE_TRANSLATE (eqv_table
, start
);
2156 int minchar
, maxchar
;
2158 /* Classify this character */
2160 this_type
= one_case
;
2161 else if (RE_TRANSLATE (eqv_table
, eqv
) == start
)
2162 this_type
= two_case
;
2164 this_type
= strange
;
2167 minchar
= start
, maxchar
= eqv
;
2169 minchar
= eqv
, maxchar
= start
;
2171 /* Can this character extend the run in progress? */
2172 if (this_type
== strange
|| this_type
!= run_type
2173 || !(minchar
== run_end
+ 1
2174 && (run_type
== two_case
2175 ? maxchar
== run_eqv_end
+ 1 : 1)))
2178 Record each of its equivalent ranges. */
2179 if (run_type
== one_case
)
2181 EXTEND_RANGE_TABLE (work_area
, 2);
2182 work_area
->table
[work_area
->used
++] = run_start
;
2183 work_area
->table
[work_area
->used
++] = run_end
;
2185 else if (run_type
== two_case
)
2187 EXTEND_RANGE_TABLE (work_area
, 4);
2188 work_area
->table
[work_area
->used
++] = run_start
;
2189 work_area
->table
[work_area
->used
++] = run_end
;
2190 work_area
->table
[work_area
->used
++]
2191 = RE_TRANSLATE (eqv_table
, run_start
);
2192 work_area
->table
[work_area
->used
++]
2193 = RE_TRANSLATE (eqv_table
, run_end
);
2198 if (this_type
== strange
)
2200 /* For a strange character, add each of its equivalents, one
2201 by one. Don't start a range. */
2204 EXTEND_RANGE_TABLE (work_area
, 2);
2205 work_area
->table
[work_area
->used
++] = eqv
;
2206 work_area
->table
[work_area
->used
++] = eqv
;
2207 eqv
= RE_TRANSLATE (eqv_table
, eqv
);
2209 while (eqv
!= start
);
2212 /* Add this char to the run, or start a new run. */
2213 else if (run_type
== strange
)
2215 /* Initialize a new range. */
2216 run_type
= this_type
;
2219 run_eqv_end
= RE_TRANSLATE (eqv_table
, run_end
);
2223 /* Extend a running range. */
2225 run_eqv_end
= RE_TRANSLATE (eqv_table
, run_end
);
2229 /* If a run is still in progress at the end, finish it now
2230 by recording its equivalent ranges. */
2231 if (run_type
== one_case
)
2233 EXTEND_RANGE_TABLE (work_area
, 2);
2234 work_area
->table
[work_area
->used
++] = run_start
;
2235 work_area
->table
[work_area
->used
++] = run_end
;
2237 else if (run_type
== two_case
)
2239 EXTEND_RANGE_TABLE (work_area
, 4);
2240 work_area
->table
[work_area
->used
++] = run_start
;
2241 work_area
->table
[work_area
->used
++] = run_end
;
2242 work_area
->table
[work_area
->used
++]
2243 = RE_TRANSLATE (eqv_table
, run_start
);
2244 work_area
->table
[work_area
->used
++]
2245 = RE_TRANSLATE (eqv_table
, run_end
);
2253 /* Record the image of the range start..end when passed through
2254 TRANSLATE. This is not necessarily TRANSLATE(start)..TRANSLATE(end)
2255 and is not even necessarily contiguous.
2256 Normally we approximate it with the smallest contiguous range that contains
2257 all the chars we need. However, for Latin-1 we go to extra effort
2260 This function is not called for ASCII ranges.
2262 Returns -1 if successful, REG_ESPACE if ran out of space. */
2265 set_image_of_range (struct range_table_work_area
*work_area
,
2266 re_wchar_t start
, re_wchar_t end
,
2267 RE_TRANSLATE_TYPE translate
)
2269 re_wchar_t cmin
, cmax
;
2272 /* For Latin-1 ranges, use set_image_of_range_1
2273 to get proper handling of ranges that include letters and nonletters.
2274 For a range that includes the whole of Latin-1, this is not necessary.
2275 For other character sets, we don't bother to get this right. */
2276 if (RE_TRANSLATE_P (translate
) && start
< 04400
2277 && !(start
< 04200 && end
>= 04377))
2284 tem
= set_image_of_range_1 (work_area
, start
, newend
, translate
);
2294 EXTEND_RANGE_TABLE (work_area
, 2);
2295 work_area
->table
[work_area
->used
++] = (start
);
2296 work_area
->table
[work_area
->used
++] = (end
);
2298 cmin
= -1, cmax
= -1;
2300 if (RE_TRANSLATE_P (translate
))
2304 for (ch
= start
; ch
<= end
; ch
++)
2306 re_wchar_t c
= TRANSLATE (ch
);
2307 if (! (start
<= c
&& c
<= end
))
2313 cmin
= MIN (cmin
, c
);
2314 cmax
= MAX (cmax
, c
);
2321 EXTEND_RANGE_TABLE (work_area
, 2);
2322 work_area
->table
[work_area
->used
++] = (cmin
);
2323 work_area
->table
[work_area
->used
++] = (cmax
);
2331 #ifndef MATCH_MAY_ALLOCATE
2333 /* If we cannot allocate large objects within re_match_2_internal,
2334 we make the fail stack and register vectors global.
2335 The fail stack, we grow to the maximum size when a regexp
2337 The register vectors, we adjust in size each time we
2338 compile a regexp, according to the number of registers it needs. */
2340 static fail_stack_type fail_stack
;
2342 /* Size with which the following vectors are currently allocated.
2343 That is so we can make them bigger as needed,
2344 but never make them smaller. */
2345 static int regs_allocated_size
;
2347 static re_char
** regstart
, ** regend
;
2348 static re_char
**best_regstart
, **best_regend
;
2350 /* Make the register vectors big enough for NUM_REGS registers,
2351 but don't make them smaller. */
2354 regex_grow_registers (int num_regs
)
2356 if (num_regs
> regs_allocated_size
)
2358 RETALLOC_IF (regstart
, num_regs
, re_char
*);
2359 RETALLOC_IF (regend
, num_regs
, re_char
*);
2360 RETALLOC_IF (best_regstart
, num_regs
, re_char
*);
2361 RETALLOC_IF (best_regend
, num_regs
, re_char
*);
2363 regs_allocated_size
= num_regs
;
2367 #endif /* not MATCH_MAY_ALLOCATE */
2369 static boolean
group_in_compile_stack (compile_stack_type compile_stack
,
2372 /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
2373 Returns one of error codes defined in `regex.h', or zero for success.
2375 Assumes the `allocated' (and perhaps `buffer') and `translate'
2376 fields are set in BUFP on entry.
2378 If it succeeds, results are put in BUFP (if it returns an error, the
2379 contents of BUFP are undefined):
2380 `buffer' is the compiled pattern;
2381 `syntax' is set to SYNTAX;
2382 `used' is set to the length of the compiled pattern;
2383 `fastmap_accurate' is zero;
2384 `re_nsub' is the number of subexpressions in PATTERN;
2385 `not_bol' and `not_eol' are zero;
2387 The `fastmap' field is neither examined nor set. */
2389 /* Insert the `jump' from the end of last alternative to "here".
2390 The space for the jump has already been allocated. */
2391 #define FIXUP_ALT_JUMP() \
2393 if (fixup_alt_jump) \
2394 STORE_JUMP (jump, fixup_alt_jump, b); \
2398 /* Return, freeing storage we allocated. */
2399 #define FREE_STACK_RETURN(value) \
2401 FREE_RANGE_TABLE_WORK_AREA (range_table_work); \
2402 free (compile_stack.stack); \
2406 static reg_errcode_t
2407 regex_compile (const_re_char
*pattern
, size_t size
, reg_syntax_t syntax
,
2408 struct re_pattern_buffer
*bufp
)
2410 /* We fetch characters from PATTERN here. */
2411 register re_wchar_t c
, c1
;
2413 /* Points to the end of the buffer, where we should append. */
2414 register unsigned char *b
;
2416 /* Keeps track of unclosed groups. */
2417 compile_stack_type compile_stack
;
2419 /* Points to the current (ending) position in the pattern. */
2421 /* `const' makes AIX compiler fail. */
2422 unsigned char *p
= pattern
;
2424 re_char
*p
= pattern
;
2426 re_char
*pend
= pattern
+ size
;
2428 /* How to translate the characters in the pattern. */
2429 RE_TRANSLATE_TYPE translate
= bufp
->translate
;
2431 /* Address of the count-byte of the most recently inserted `exactn'
2432 command. This makes it possible to tell if a new exact-match
2433 character can be added to that command or if the character requires
2434 a new `exactn' command. */
2435 unsigned char *pending_exact
= 0;
2437 /* Address of start of the most recently finished expression.
2438 This tells, e.g., postfix * where to find the start of its
2439 operand. Reset at the beginning of groups and alternatives. */
2440 unsigned char *laststart
= 0;
2442 /* Address of beginning of regexp, or inside of last group. */
2443 unsigned char *begalt
;
2445 /* Place in the uncompiled pattern (i.e., the {) to
2446 which to go back if the interval is invalid. */
2447 re_char
*beg_interval
;
2449 /* Address of the place where a forward jump should go to the end of
2450 the containing expression. Each alternative of an `or' -- except the
2451 last -- ends with a forward jump of this sort. */
2452 unsigned char *fixup_alt_jump
= 0;
2454 /* Work area for range table of charset. */
2455 struct range_table_work_area range_table_work
;
2457 /* If the object matched can contain multibyte characters. */
2458 const boolean multibyte
= RE_MULTIBYTE_P (bufp
);
2460 /* Nonzero if we have pushed down into a subpattern. */
2461 int in_subpattern
= 0;
2463 /* These hold the values of p, pattern, and pend from the main
2464 pattern when we have pushed into a subpattern. */
2465 re_char
*main_p
IF_LINT (= NULL
);
2466 re_char
*main_pattern
IF_LINT (= NULL
);
2467 re_char
*main_pend
IF_LINT (= NULL
);
2471 DEBUG_PRINT ("\nCompiling pattern: ");
2474 unsigned debug_count
;
2476 for (debug_count
= 0; debug_count
< size
; debug_count
++)
2477 putchar (pattern
[debug_count
]);
2482 /* Initialize the compile stack. */
2483 compile_stack
.stack
= TALLOC (INIT_COMPILE_STACK_SIZE
, compile_stack_elt_t
);
2484 if (compile_stack
.stack
== NULL
)
2487 compile_stack
.size
= INIT_COMPILE_STACK_SIZE
;
2488 compile_stack
.avail
= 0;
2490 range_table_work
.table
= 0;
2491 range_table_work
.allocated
= 0;
2493 /* Initialize the pattern buffer. */
2494 bufp
->syntax
= syntax
;
2495 bufp
->fastmap_accurate
= 0;
2496 bufp
->not_bol
= bufp
->not_eol
= 0;
2497 bufp
->used_syntax
= 0;
2499 /* Set `used' to zero, so that if we return an error, the pattern
2500 printer (for debugging) will think there's no pattern. We reset it
2504 /* Always count groups, whether or not bufp->no_sub is set. */
2507 #if !defined emacs && !defined SYNTAX_TABLE
2508 /* Initialize the syntax table. */
2509 init_syntax_once ();
2512 if (bufp
->allocated
== 0)
2515 { /* If zero allocated, but buffer is non-null, try to realloc
2516 enough space. This loses if buffer's address is bogus, but
2517 that is the user's responsibility. */
2518 RETALLOC (bufp
->buffer
, INIT_BUF_SIZE
, unsigned char);
2521 { /* Caller did not allocate a buffer. Do it for them. */
2522 bufp
->buffer
= TALLOC (INIT_BUF_SIZE
, unsigned char);
2524 if (!bufp
->buffer
) FREE_STACK_RETURN (REG_ESPACE
);
2526 bufp
->allocated
= INIT_BUF_SIZE
;
2529 begalt
= b
= bufp
->buffer
;
2531 /* Loop through the uncompiled pattern until we're at the end. */
2536 /* If this is the end of an included regexp,
2537 pop back to the main regexp and try again. */
2541 pattern
= main_pattern
;
2546 /* If this is the end of the main regexp, we are done. */
2558 /* If there's no special whitespace regexp, treat
2559 spaces normally. And don't try to do this recursively. */
2560 if (!whitespace_regexp
|| in_subpattern
)
2563 /* Peek past following spaces. */
2570 /* If the spaces are followed by a repetition op,
2571 treat them normally. */
2573 && (*p1
== '*' || *p1
== '+' || *p1
== '?'
2574 || (*p1
== '\\' && p1
+ 1 != pend
&& p1
[1] == '{')))
2577 /* Replace the spaces with the whitespace regexp. */
2581 main_pattern
= pattern
;
2582 p
= pattern
= whitespace_regexp
;
2583 pend
= p
+ strlen ((const char *) p
);
2589 if ( /* If at start of pattern, it's an operator. */
2591 /* If context independent, it's an operator. */
2592 || syntax
& RE_CONTEXT_INDEP_ANCHORS
2593 /* Otherwise, depends on what's come before. */
2594 || at_begline_loc_p (pattern
, p
, syntax
))
2595 BUF_PUSH ((syntax
& RE_NO_NEWLINE_ANCHOR
) ? begbuf
: begline
);
2604 if ( /* If at end of pattern, it's an operator. */
2606 /* If context independent, it's an operator. */
2607 || syntax
& RE_CONTEXT_INDEP_ANCHORS
2608 /* Otherwise, depends on what's next. */
2609 || at_endline_loc_p (p
, pend
, syntax
))
2610 BUF_PUSH ((syntax
& RE_NO_NEWLINE_ANCHOR
) ? endbuf
: endline
);
2619 if ((syntax
& RE_BK_PLUS_QM
)
2620 || (syntax
& RE_LIMITED_OPS
))
2624 /* If there is no previous pattern... */
2627 if (syntax
& RE_CONTEXT_INVALID_OPS
)
2628 FREE_STACK_RETURN (REG_BADRPT
);
2629 else if (!(syntax
& RE_CONTEXT_INDEP_OPS
))
2634 /* 1 means zero (many) matches is allowed. */
2635 boolean zero_times_ok
= 0, many_times_ok
= 0;
2638 /* If there is a sequence of repetition chars, collapse it
2639 down to just one (the right one). We can't combine
2640 interval operators with these because of, e.g., `a{2}*',
2641 which should only match an even number of `a's. */
2645 if ((syntax
& RE_FRUGAL
)
2646 && c
== '?' && (zero_times_ok
|| many_times_ok
))
2650 zero_times_ok
|= c
!= '+';
2651 many_times_ok
|= c
!= '?';
2657 || (!(syntax
& RE_BK_PLUS_QM
)
2658 && (*p
== '+' || *p
== '?')))
2660 else if (syntax
& RE_BK_PLUS_QM
&& *p
== '\\')
2663 FREE_STACK_RETURN (REG_EESCAPE
);
2664 if (p
[1] == '+' || p
[1] == '?')
2665 PATFETCH (c
); /* Gobble up the backslash. */
2671 /* If we get here, we found another repeat character. */
2675 /* Star, etc. applied to an empty pattern is equivalent
2676 to an empty pattern. */
2677 if (!laststart
|| laststart
== b
)
2680 /* Now we know whether or not zero matches is allowed
2681 and also whether or not two or more matches is allowed. */
2686 boolean simple
= skip_one_char (laststart
) == b
;
2687 size_t startoffset
= 0;
2689 /* Check if the loop can match the empty string. */
2690 (simple
|| !analyse_first (laststart
, b
, NULL
, 0))
2691 ? on_failure_jump
: on_failure_jump_loop
;
2692 assert (skip_one_char (laststart
) <= b
);
2694 if (!zero_times_ok
&& simple
)
2695 { /* Since simple * loops can be made faster by using
2696 on_failure_keep_string_jump, we turn simple P+
2697 into PP* if P is simple. */
2698 unsigned char *p1
, *p2
;
2699 startoffset
= b
- laststart
;
2700 GET_BUFFER_SPACE (startoffset
);
2701 p1
= b
; p2
= laststart
;
2707 GET_BUFFER_SPACE (6);
2710 STORE_JUMP (ofj
, b
, b
+ 6);
2712 /* Simple * loops can use on_failure_keep_string_jump
2713 depending on what follows. But since we don't know
2714 that yet, we leave the decision up to
2715 on_failure_jump_smart. */
2716 INSERT_JUMP (simple
? on_failure_jump_smart
: ofj
,
2717 laststart
+ startoffset
, b
+ 6);
2719 STORE_JUMP (jump
, b
, laststart
+ startoffset
);
2724 /* A simple ? pattern. */
2725 assert (zero_times_ok
);
2726 GET_BUFFER_SPACE (3);
2727 INSERT_JUMP (on_failure_jump
, laststart
, b
+ 3);
2731 else /* not greedy */
2732 { /* I wish the greedy and non-greedy cases could be merged. */
2734 GET_BUFFER_SPACE (7); /* We might use less. */
2737 boolean emptyp
= analyse_first (laststart
, b
, NULL
, 0);
2739 /* The non-greedy multiple match looks like
2740 a repeat..until: we only need a conditional jump
2741 at the end of the loop. */
2742 if (emptyp
) BUF_PUSH (no_op
);
2743 STORE_JUMP (emptyp
? on_failure_jump_nastyloop
2744 : on_failure_jump
, b
, laststart
);
2748 /* The repeat...until naturally matches one or more.
2749 To also match zero times, we need to first jump to
2750 the end of the loop (its conditional jump). */
2751 INSERT_JUMP (jump
, laststart
, b
);
2757 /* non-greedy a?? */
2758 INSERT_JUMP (jump
, laststart
, b
+ 3);
2760 INSERT_JUMP (on_failure_jump
, laststart
, laststart
+ 6);
2779 CLEAR_RANGE_TABLE_WORK_USED (range_table_work
);
2781 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2783 /* Ensure that we have enough space to push a charset: the
2784 opcode, the length count, and the bitset; 34 bytes in all. */
2785 GET_BUFFER_SPACE (34);
2789 /* We test `*p == '^' twice, instead of using an if
2790 statement, so we only need one BUF_PUSH. */
2791 BUF_PUSH (*p
== '^' ? charset_not
: charset
);
2795 /* Remember the first position in the bracket expression. */
2798 /* Push the number of bytes in the bitmap. */
2799 BUF_PUSH ((1 << BYTEWIDTH
) / BYTEWIDTH
);
2801 /* Clear the whole map. */
2802 memset (b
, 0, (1 << BYTEWIDTH
) / BYTEWIDTH
);
2804 /* charset_not matches newline according to a syntax bit. */
2805 if ((re_opcode_t
) b
[-2] == charset_not
2806 && (syntax
& RE_HAT_LISTS_NOT_NEWLINE
))
2807 SET_LIST_BIT ('\n');
2809 /* Read in characters and ranges, setting map bits. */
2812 boolean escaped_char
= false;
2813 const unsigned char *p2
= p
;
2816 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2818 /* Don't translate yet. The range TRANSLATE(X..Y) cannot
2819 always be determined from TRANSLATE(X) and TRANSLATE(Y)
2820 So the translation is done later in a loop. Example:
2821 (let ((case-fold-search t)) (string-match "[A-_]" "A")) */
2824 /* \ might escape characters inside [...] and [^...]. */
2825 if ((syntax
& RE_BACKSLASH_ESCAPE_IN_LISTS
) && c
== '\\')
2827 if (p
== pend
) FREE_STACK_RETURN (REG_EESCAPE
);
2830 escaped_char
= true;
2834 /* Could be the end of the bracket expression. If it's
2835 not (i.e., when the bracket expression is `[]' so
2836 far), the ']' character bit gets set way below. */
2837 if (c
== ']' && p2
!= p1
)
2841 /* See if we're at the beginning of a possible character
2844 if (!escaped_char
&&
2845 syntax
& RE_CHAR_CLASSES
&& c
== '[' && *p
== ':')
2847 /* Leave room for the null. */
2848 unsigned char str
[CHAR_CLASS_MAX_LENGTH
+ 1];
2849 const unsigned char *class_beg
;
2855 /* If pattern is `[[:'. */
2856 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2861 if ((c
== ':' && *p
== ']') || p
== pend
)
2863 if (c1
< CHAR_CLASS_MAX_LENGTH
)
2866 /* This is in any case an invalid class name. */
2871 /* If isn't a word bracketed by `[:' and `:]':
2872 undo the ending character, the letters, and
2873 leave the leading `:' and `[' (but set bits for
2875 if (c
== ':' && *p
== ']')
2877 re_wctype_t cc
= re_wctype (str
);
2880 FREE_STACK_RETURN (REG_ECTYPE
);
2882 /* Throw away the ] at the end of the character
2886 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2889 for (ch
= 0; ch
< (1 << BYTEWIDTH
); ++ch
)
2890 if (re_iswctype (btowc (ch
), cc
))
2893 if (c
< (1 << BYTEWIDTH
))
2897 /* Most character classes in a multibyte match
2898 just set a flag. Exceptions are is_blank,
2899 is_digit, is_cntrl, and is_xdigit, since
2900 they can only match ASCII characters. We
2901 don't need to handle them for multibyte.
2902 They are distinguished by a negative wctype. */
2904 /* Setup the gl_state object to its buffer-defined
2905 value. This hardcodes the buffer-global
2906 syntax-table for ASCII chars, while the other chars
2907 will obey syntax-table properties. It's not ideal,
2908 but it's the way it's been done until now. */
2909 SETUP_BUFFER_SYNTAX_TABLE ();
2911 for (ch
= 0; ch
< 256; ++ch
)
2913 c
= RE_CHAR_TO_MULTIBYTE (ch
);
2914 if (! CHAR_BYTE8_P (c
)
2915 && re_iswctype (c
, cc
))
2921 if (ASCII_CHAR_P (c1
))
2923 else if ((c1
= RE_CHAR_TO_UNIBYTE (c1
)) >= 0)
2927 SET_RANGE_TABLE_WORK_AREA_BIT
2928 (range_table_work
, re_wctype_to_bit (cc
));
2930 /* In most cases the matching rule for char classes
2931 only uses the syntax table for multibyte chars,
2932 so that the content of the syntax-table it is not
2933 hardcoded in the range_table. SPACE and WORD are
2934 the two exceptions. */
2935 if ((1 << cc
) & ((1 << RECC_SPACE
) | (1 << RECC_WORD
)))
2936 bufp
->used_syntax
= 1;
2938 /* Repeat the loop. */
2943 /* Go back to right after the "[:". */
2947 /* Because the `:' may starts the range, we
2948 can't simply set bit and repeat the loop.
2949 Instead, just set it to C and handle below. */
2954 if (p
< pend
&& p
[0] == '-' && p
[1] != ']')
2957 /* Discard the `-'. */
2960 /* Fetch the character which ends the range. */
2963 if (CHAR_BYTE8_P (c1
)
2964 && ! ASCII_CHAR_P (c
) && ! CHAR_BYTE8_P (c
))
2965 /* Treat the range from a multibyte character to
2966 raw-byte character as empty. */
2971 /* Range from C to C. */
2976 if (syntax
& RE_NO_EMPTY_RANGES
)
2977 FREE_STACK_RETURN (REG_ERANGEX
);
2978 /* Else, repeat the loop. */
2983 /* Set the range into bitmap */
2984 for (; c
<= c1
; c
++)
2987 if (ch
< (1 << BYTEWIDTH
))
2994 SETUP_ASCII_RANGE (range_table_work
, c
, ch
);
2996 if (CHAR_BYTE8_P (c1
))
2997 c
= BYTE8_TO_CHAR (128);
3001 if (CHAR_BYTE8_P (c
))
3003 c
= CHAR_TO_BYTE8 (c
);
3004 c1
= CHAR_TO_BYTE8 (c1
);
3005 for (; c
<= c1
; c
++)
3010 SETUP_MULTIBYTE_RANGE (range_table_work
, c
, c1
);
3014 SETUP_UNIBYTE_RANGE (range_table_work
, c
, c1
);
3021 /* Discard any (non)matching list bytes that are all 0 at the
3022 end of the map. Decrease the map-length byte too. */
3023 while ((int) b
[-1] > 0 && b
[b
[-1] - 1] == 0)
3027 /* Build real range table from work area. */
3028 if (RANGE_TABLE_WORK_USED (range_table_work
)
3029 || RANGE_TABLE_WORK_BITS (range_table_work
))
3032 int used
= RANGE_TABLE_WORK_USED (range_table_work
);
3034 /* Allocate space for COUNT + RANGE_TABLE. Needs two
3035 bytes for flags, two for COUNT, and three bytes for
3037 GET_BUFFER_SPACE (4 + used
* 3);
3039 /* Indicate the existence of range table. */
3040 laststart
[1] |= 0x80;
3042 /* Store the character class flag bits into the range table.
3043 If not in emacs, these flag bits are always 0. */
3044 *b
++ = RANGE_TABLE_WORK_BITS (range_table_work
) & 0xff;
3045 *b
++ = RANGE_TABLE_WORK_BITS (range_table_work
) >> 8;
3047 STORE_NUMBER_AND_INCR (b
, used
/ 2);
3048 for (i
= 0; i
< used
; i
++)
3049 STORE_CHARACTER_AND_INCR
3050 (b
, RANGE_TABLE_WORK_ELT (range_table_work
, i
));
3057 if (syntax
& RE_NO_BK_PARENS
)
3064 if (syntax
& RE_NO_BK_PARENS
)
3071 if (syntax
& RE_NEWLINE_ALT
)
3078 if (syntax
& RE_NO_BK_VBAR
)
3085 if (syntax
& RE_INTERVALS
&& syntax
& RE_NO_BK_BRACES
)
3086 goto handle_interval
;
3092 if (p
== pend
) FREE_STACK_RETURN (REG_EESCAPE
);
3094 /* Do not translate the character after the \, so that we can
3095 distinguish, e.g., \B from \b, even if we normally would
3096 translate, e.g., B to b. */
3102 if (syntax
& RE_NO_BK_PARENS
)
3103 goto normal_backslash
;
3108 regnum_t regnum
= 0;
3111 /* Look for a special (?...) construct */
3112 if ((syntax
& RE_SHY_GROUPS
) && *p
== '?')
3114 PATFETCH (c
); /* Gobble up the '?'. */
3120 case ':': shy
= 1; break;
3122 /* An explicitly specified regnum must start
3125 FREE_STACK_RETURN (REG_BADPAT
);
3126 case '1': case '2': case '3': case '4':
3127 case '5': case '6': case '7': case '8': case '9':
3128 regnum
= 10*regnum
+ (c
- '0'); break;
3130 /* Only (?:...) is supported right now. */
3131 FREE_STACK_RETURN (REG_BADPAT
);
3138 regnum
= ++bufp
->re_nsub
;
3140 { /* It's actually not shy, but explicitly numbered. */
3142 if (regnum
> bufp
->re_nsub
)
3143 bufp
->re_nsub
= regnum
;
3144 else if (regnum
> bufp
->re_nsub
3145 /* Ideally, we'd want to check that the specified
3146 group can't have matched (i.e. all subgroups
3147 using the same regnum are in other branches of
3148 OR patterns), but we don't currently keep track
3149 of enough info to do that easily. */
3150 || group_in_compile_stack (compile_stack
, regnum
))
3151 FREE_STACK_RETURN (REG_BADPAT
);
3154 /* It's really shy. */
3155 regnum
= - bufp
->re_nsub
;
3157 if (COMPILE_STACK_FULL
)
3159 RETALLOC (compile_stack
.stack
, compile_stack
.size
<< 1,
3160 compile_stack_elt_t
);
3161 if (compile_stack
.stack
== NULL
) return REG_ESPACE
;
3163 compile_stack
.size
<<= 1;
3166 /* These are the values to restore when we hit end of this
3167 group. They are all relative offsets, so that if the
3168 whole pattern moves because of realloc, they will still
3170 COMPILE_STACK_TOP
.begalt_offset
= begalt
- bufp
->buffer
;
3171 COMPILE_STACK_TOP
.fixup_alt_jump
3172 = fixup_alt_jump
? fixup_alt_jump
- bufp
->buffer
+ 1 : 0;
3173 COMPILE_STACK_TOP
.laststart_offset
= b
- bufp
->buffer
;
3174 COMPILE_STACK_TOP
.regnum
= regnum
;
3176 /* Do not push a start_memory for groups beyond the last one
3177 we can represent in the compiled pattern. */
3178 if (regnum
<= MAX_REGNUM
&& regnum
> 0)
3179 BUF_PUSH_2 (start_memory
, regnum
);
3181 compile_stack
.avail
++;
3186 /* If we've reached MAX_REGNUM groups, then this open
3187 won't actually generate any code, so we'll have to
3188 clear pending_exact explicitly. */
3194 if (syntax
& RE_NO_BK_PARENS
) goto normal_backslash
;
3196 if (COMPILE_STACK_EMPTY
)
3198 if (syntax
& RE_UNMATCHED_RIGHT_PAREN_ORD
)
3199 goto normal_backslash
;
3201 FREE_STACK_RETURN (REG_ERPAREN
);
3207 /* See similar code for backslashed left paren above. */
3208 if (COMPILE_STACK_EMPTY
)
3210 if (syntax
& RE_UNMATCHED_RIGHT_PAREN_ORD
)
3213 FREE_STACK_RETURN (REG_ERPAREN
);
3216 /* Since we just checked for an empty stack above, this
3217 ``can't happen''. */
3218 assert (compile_stack
.avail
!= 0);
3220 /* We don't just want to restore into `regnum', because
3221 later groups should continue to be numbered higher,
3222 as in `(ab)c(de)' -- the second group is #2. */
3225 compile_stack
.avail
--;
3226 begalt
= bufp
->buffer
+ COMPILE_STACK_TOP
.begalt_offset
;
3228 = COMPILE_STACK_TOP
.fixup_alt_jump
3229 ? bufp
->buffer
+ COMPILE_STACK_TOP
.fixup_alt_jump
- 1
3231 laststart
= bufp
->buffer
+ COMPILE_STACK_TOP
.laststart_offset
;
3232 regnum
= COMPILE_STACK_TOP
.regnum
;
3233 /* If we've reached MAX_REGNUM groups, then this open
3234 won't actually generate any code, so we'll have to
3235 clear pending_exact explicitly. */
3238 /* We're at the end of the group, so now we know how many
3239 groups were inside this one. */
3240 if (regnum
<= MAX_REGNUM
&& regnum
> 0)
3241 BUF_PUSH_2 (stop_memory
, regnum
);
3246 case '|': /* `\|'. */
3247 if (syntax
& RE_LIMITED_OPS
|| syntax
& RE_NO_BK_VBAR
)
3248 goto normal_backslash
;
3250 if (syntax
& RE_LIMITED_OPS
)
3253 /* Insert before the previous alternative a jump which
3254 jumps to this alternative if the former fails. */
3255 GET_BUFFER_SPACE (3);
3256 INSERT_JUMP (on_failure_jump
, begalt
, b
+ 6);
3260 /* The alternative before this one has a jump after it
3261 which gets executed if it gets matched. Adjust that
3262 jump so it will jump to this alternative's analogous
3263 jump (put in below, which in turn will jump to the next
3264 (if any) alternative's such jump, etc.). The last such
3265 jump jumps to the correct final destination. A picture:
3271 If we are at `b', then fixup_alt_jump right now points to a
3272 three-byte space after `a'. We'll put in the jump, set
3273 fixup_alt_jump to right after `b', and leave behind three
3274 bytes which we'll fill in when we get to after `c'. */
3278 /* Mark and leave space for a jump after this alternative,
3279 to be filled in later either by next alternative or
3280 when know we're at the end of a series of alternatives. */
3282 GET_BUFFER_SPACE (3);
3291 /* If \{ is a literal. */
3292 if (!(syntax
& RE_INTERVALS
)
3293 /* If we're at `\{' and it's not the open-interval
3295 || (syntax
& RE_NO_BK_BRACES
))
3296 goto normal_backslash
;
3300 /* If got here, then the syntax allows intervals. */
3302 /* At least (most) this many matches must be made. */
3303 int lower_bound
= 0, upper_bound
= -1;
3307 GET_INTERVAL_COUNT (lower_bound
);
3310 GET_INTERVAL_COUNT (upper_bound
);
3312 /* Interval such as `{1}' => match exactly once. */
3313 upper_bound
= lower_bound
;
3316 || (0 <= upper_bound
&& upper_bound
< lower_bound
))
3317 FREE_STACK_RETURN (REG_BADBR
);
3319 if (!(syntax
& RE_NO_BK_BRACES
))
3322 FREE_STACK_RETURN (REG_BADBR
);
3324 FREE_STACK_RETURN (REG_EESCAPE
);
3329 FREE_STACK_RETURN (REG_BADBR
);
3331 /* We just parsed a valid interval. */
3333 /* If it's invalid to have no preceding re. */
3336 if (syntax
& RE_CONTEXT_INVALID_OPS
)
3337 FREE_STACK_RETURN (REG_BADRPT
);
3338 else if (syntax
& RE_CONTEXT_INDEP_OPS
)
3341 goto unfetch_interval
;
3344 if (upper_bound
== 0)
3345 /* If the upper bound is zero, just drop the sub pattern
3348 else if (lower_bound
== 1 && upper_bound
== 1)
3349 /* Just match it once: nothing to do here. */
3352 /* Otherwise, we have a nontrivial interval. When
3353 we're all done, the pattern will look like:
3354 set_number_at <jump count> <upper bound>
3355 set_number_at <succeed_n count> <lower bound>
3356 succeed_n <after jump addr> <succeed_n count>
3358 jump_n <succeed_n addr> <jump count>
3359 (The upper bound and `jump_n' are omitted if
3360 `upper_bound' is 1, though.) */
3362 { /* If the upper bound is > 1, we need to insert
3363 more at the end of the loop. */
3364 unsigned int nbytes
= (upper_bound
< 0 ? 3
3365 : upper_bound
> 1 ? 5 : 0);
3366 unsigned int startoffset
= 0;
3368 GET_BUFFER_SPACE (20); /* We might use less. */
3370 if (lower_bound
== 0)
3372 /* A succeed_n that starts with 0 is really a
3373 a simple on_failure_jump_loop. */
3374 INSERT_JUMP (on_failure_jump_loop
, laststart
,
3380 /* Initialize lower bound of the `succeed_n', even
3381 though it will be set during matching by its
3382 attendant `set_number_at' (inserted next),
3383 because `re_compile_fastmap' needs to know.
3384 Jump to the `jump_n' we might insert below. */
3385 INSERT_JUMP2 (succeed_n
, laststart
,
3390 /* Code to initialize the lower bound. Insert
3391 before the `succeed_n'. The `5' is the last two
3392 bytes of this `set_number_at', plus 3 bytes of
3393 the following `succeed_n'. */
3394 insert_op2 (set_number_at
, laststart
, 5, lower_bound
, b
);
3399 if (upper_bound
< 0)
3401 /* A negative upper bound stands for infinity,
3402 in which case it degenerates to a plain jump. */
3403 STORE_JUMP (jump
, b
, laststart
+ startoffset
);
3406 else if (upper_bound
> 1)
3407 { /* More than one repetition is allowed, so
3408 append a backward jump to the `succeed_n'
3409 that starts this interval.
3411 When we've reached this during matching,
3412 we'll have matched the interval once, so
3413 jump back only `upper_bound - 1' times. */
3414 STORE_JUMP2 (jump_n
, b
, laststart
+ startoffset
,
3418 /* The location we want to set is the second
3419 parameter of the `jump_n'; that is `b-2' as
3420 an absolute address. `laststart' will be
3421 the `set_number_at' we're about to insert;
3422 `laststart+3' the number to set, the source
3423 for the relative address. But we are
3424 inserting into the middle of the pattern --
3425 so everything is getting moved up by 5.
3426 Conclusion: (b - 2) - (laststart + 3) + 5,
3427 i.e., b - laststart.
3429 We insert this at the beginning of the loop
3430 so that if we fail during matching, we'll
3431 reinitialize the bounds. */
3432 insert_op2 (set_number_at
, laststart
, b
- laststart
,
3433 upper_bound
- 1, b
);
3438 beg_interval
= NULL
;
3443 /* If an invalid interval, match the characters as literals. */
3444 assert (beg_interval
);
3446 beg_interval
= NULL
;
3448 /* normal_char and normal_backslash need `c'. */
3451 if (!(syntax
& RE_NO_BK_BRACES
))
3453 assert (p
> pattern
&& p
[-1] == '\\');
3454 goto normal_backslash
;
3460 /* There is no way to specify the before_dot and after_dot
3461 operators. rms says this is ok. --karl */
3470 BUF_PUSH_2 (syntaxspec
, syntax_spec_code
[c
]);
3476 BUF_PUSH_2 (notsyntaxspec
, syntax_spec_code
[c
]);
3482 BUF_PUSH_2 (categoryspec
, c
);
3488 BUF_PUSH_2 (notcategoryspec
, c
);
3494 if (syntax
& RE_NO_GNU_OPS
)
3497 BUF_PUSH_2 (syntaxspec
, Sword
);
3502 if (syntax
& RE_NO_GNU_OPS
)
3505 BUF_PUSH_2 (notsyntaxspec
, Sword
);
3510 if (syntax
& RE_NO_GNU_OPS
)
3517 if (syntax
& RE_NO_GNU_OPS
)
3524 if (syntax
& RE_NO_GNU_OPS
)
3533 FREE_STACK_RETURN (REG_BADPAT
);
3537 if (syntax
& RE_NO_GNU_OPS
)
3539 BUF_PUSH (wordbound
);
3543 if (syntax
& RE_NO_GNU_OPS
)
3545 BUF_PUSH (notwordbound
);
3549 if (syntax
& RE_NO_GNU_OPS
)
3555 if (syntax
& RE_NO_GNU_OPS
)
3560 case '1': case '2': case '3': case '4': case '5':
3561 case '6': case '7': case '8': case '9':
3565 if (syntax
& RE_NO_BK_REFS
)
3566 goto normal_backslash
;
3570 if (reg
> bufp
->re_nsub
|| reg
< 1
3571 /* Can't back reference to a subexp before its end. */
3572 || group_in_compile_stack (compile_stack
, reg
))
3573 FREE_STACK_RETURN (REG_ESUBREG
);
3576 BUF_PUSH_2 (duplicate
, reg
);
3583 if (syntax
& RE_BK_PLUS_QM
)
3586 goto normal_backslash
;
3590 /* You might think it would be useful for \ to mean
3591 not to translate; but if we don't translate it
3592 it will never match anything. */
3599 /* Expects the character in `c'. */
3601 /* If no exactn currently being built. */
3604 /* If last exactn not at current position. */
3605 || pending_exact
+ *pending_exact
+ 1 != b
3607 /* We have only one byte following the exactn for the count. */
3608 || *pending_exact
>= (1 << BYTEWIDTH
) - MAX_MULTIBYTE_LENGTH
3610 /* If followed by a repetition operator. */
3611 || (p
!= pend
&& (*p
== '*' || *p
== '^'))
3612 || ((syntax
& RE_BK_PLUS_QM
)
3613 ? p
+ 1 < pend
&& *p
== '\\' && (p
[1] == '+' || p
[1] == '?')
3614 : p
!= pend
&& (*p
== '+' || *p
== '?'))
3615 || ((syntax
& RE_INTERVALS
)
3616 && ((syntax
& RE_NO_BK_BRACES
)
3617 ? p
!= pend
&& *p
== '{'
3618 : p
+ 1 < pend
&& p
[0] == '\\' && p
[1] == '{')))
3620 /* Start building a new exactn. */
3624 BUF_PUSH_2 (exactn
, 0);
3625 pending_exact
= b
- 1;
3628 GET_BUFFER_SPACE (MAX_MULTIBYTE_LENGTH
);
3635 len
= CHAR_STRING (c
, b
);
3640 c1
= RE_CHAR_TO_MULTIBYTE (c
);
3641 if (! CHAR_BYTE8_P (c1
))
3643 re_wchar_t c2
= TRANSLATE (c1
);
3645 if (c1
!= c2
&& (c1
= RE_CHAR_TO_UNIBYTE (c2
)) >= 0)
3651 (*pending_exact
) += len
;
3656 } /* while p != pend */
3659 /* Through the pattern now. */
3663 if (!COMPILE_STACK_EMPTY
)
3664 FREE_STACK_RETURN (REG_EPAREN
);
3666 /* If we don't want backtracking, force success
3667 the first time we reach the end of the compiled pattern. */
3668 if (syntax
& RE_NO_POSIX_BACKTRACKING
)
3671 /* We have succeeded; set the length of the buffer. */
3672 bufp
->used
= b
- bufp
->buffer
;
3677 re_compile_fastmap (bufp
);
3678 DEBUG_PRINT ("\nCompiled pattern: \n");
3679 print_compiled_pattern (bufp
);
3684 #ifndef MATCH_MAY_ALLOCATE
3685 /* Initialize the failure stack to the largest possible stack. This
3686 isn't necessary unless we're trying to avoid calling alloca in
3687 the search and match routines. */
3689 int num_regs
= bufp
->re_nsub
+ 1;
3691 if (fail_stack
.size
< re_max_failures
* TYPICAL_FAILURE_SIZE
)
3693 fail_stack
.size
= re_max_failures
* TYPICAL_FAILURE_SIZE
;
3694 falk_stack
.stack
= realloc (fail_stack
.stack
,
3695 fail_stack
.size
* sizeof *falk_stack
.stack
);
3698 regex_grow_registers (num_regs
);
3700 #endif /* not MATCH_MAY_ALLOCATE */
3702 FREE_STACK_RETURN (REG_NOERROR
);
3703 } /* regex_compile */
3705 /* Subroutines for `regex_compile'. */
3707 /* Store OP at LOC followed by two-byte integer parameter ARG. */
3710 store_op1 (re_opcode_t op
, unsigned char *loc
, int arg
)
3712 *loc
= (unsigned char) op
;
3713 STORE_NUMBER (loc
+ 1, arg
);
3717 /* Like `store_op1', but for two two-byte parameters ARG1 and ARG2. */
3720 store_op2 (re_opcode_t op
, unsigned char *loc
, int arg1
, int arg2
)
3722 *loc
= (unsigned char) op
;
3723 STORE_NUMBER (loc
+ 1, arg1
);
3724 STORE_NUMBER (loc
+ 3, arg2
);
3728 /* Copy the bytes from LOC to END to open up three bytes of space at LOC
3729 for OP followed by two-byte integer parameter ARG. */
3732 insert_op1 (re_opcode_t op
, unsigned char *loc
, int arg
, unsigned char *end
)
3734 register unsigned char *pfrom
= end
;
3735 register unsigned char *pto
= end
+ 3;
3737 while (pfrom
!= loc
)
3740 store_op1 (op
, loc
, arg
);
3744 /* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2. */
3747 insert_op2 (re_opcode_t op
, unsigned char *loc
, int arg1
, int arg2
, unsigned char *end
)
3749 register unsigned char *pfrom
= end
;
3750 register unsigned char *pto
= end
+ 5;
3752 while (pfrom
!= loc
)
3755 store_op2 (op
, loc
, arg1
, arg2
);
3759 /* P points to just after a ^ in PATTERN. Return true if that ^ comes
3760 after an alternative or a begin-subexpression. We assume there is at
3761 least one character before the ^. */
3764 at_begline_loc_p (const_re_char
*pattern
, const_re_char
*p
, reg_syntax_t syntax
)
3766 re_char
*prev
= p
- 2;
3767 boolean odd_backslashes
;
3769 /* After a subexpression? */
3771 odd_backslashes
= (syntax
& RE_NO_BK_PARENS
) == 0;
3773 /* After an alternative? */
3774 else if (*prev
== '|')
3775 odd_backslashes
= (syntax
& RE_NO_BK_VBAR
) == 0;
3777 /* After a shy subexpression? */
3778 else if (*prev
== ':' && (syntax
& RE_SHY_GROUPS
))
3780 /* Skip over optional regnum. */
3781 while (prev
- 1 >= pattern
&& prev
[-1] >= '0' && prev
[-1] <= '9')
3784 if (!(prev
- 2 >= pattern
3785 && prev
[-1] == '?' && prev
[-2] == '('))
3788 odd_backslashes
= (syntax
& RE_NO_BK_PARENS
) == 0;
3793 /* Count the number of preceding backslashes. */
3795 while (prev
- 1 >= pattern
&& prev
[-1] == '\\')
3797 return (p
- prev
) & odd_backslashes
;
3801 /* The dual of at_begline_loc_p. This one is for $. We assume there is
3802 at least one character after the $, i.e., `P < PEND'. */
3805 at_endline_loc_p (const_re_char
*p
, const_re_char
*pend
, reg_syntax_t syntax
)
3808 boolean next_backslash
= *next
== '\\';
3809 re_char
*next_next
= p
+ 1 < pend
? p
+ 1 : 0;
3812 /* Before a subexpression? */
3813 (syntax
& RE_NO_BK_PARENS
? *next
== ')'
3814 : next_backslash
&& next_next
&& *next_next
== ')')
3815 /* Before an alternative? */
3816 || (syntax
& RE_NO_BK_VBAR
? *next
== '|'
3817 : next_backslash
&& next_next
&& *next_next
== '|');
3821 /* Returns true if REGNUM is in one of COMPILE_STACK's elements and
3822 false if it's not. */
3825 group_in_compile_stack (compile_stack_type compile_stack
, regnum_t regnum
)
3827 ssize_t this_element
;
3829 for (this_element
= compile_stack
.avail
- 1;
3832 if (compile_stack
.stack
[this_element
].regnum
== regnum
)
3839 If fastmap is non-NULL, go through the pattern and fill fastmap
3840 with all the possible leading chars. If fastmap is NULL, don't
3841 bother filling it up (obviously) and only return whether the
3842 pattern could potentially match the empty string.
3844 Return 1 if p..pend might match the empty string.
3845 Return 0 if p..pend matches at least one char.
3846 Return -1 if fastmap was not updated accurately. */
3849 analyse_first (const_re_char
*p
, const_re_char
*pend
, char *fastmap
,
3850 const int multibyte
)
3855 /* If all elements for base leading-codes in fastmap is set, this
3856 flag is set true. */
3857 boolean match_any_multibyte_characters
= false;
3861 /* The loop below works as follows:
3862 - It has a working-list kept in the PATTERN_STACK and which basically
3863 starts by only containing a pointer to the first operation.
3864 - If the opcode we're looking at is a match against some set of
3865 chars, then we add those chars to the fastmap and go on to the
3866 next work element from the worklist (done via `break').
3867 - If the opcode is a control operator on the other hand, we either
3868 ignore it (if it's meaningless at this point, such as `start_memory')
3869 or execute it (if it's a jump). If the jump has several destinations
3870 (i.e. `on_failure_jump'), then we push the other destination onto the
3872 We guarantee termination by ignoring backward jumps (more or less),
3873 so that `p' is monotonically increasing. More to the point, we
3874 never set `p' (or push) anything `<= p1'. */
3878 /* `p1' is used as a marker of how far back a `on_failure_jump'
3879 can go without being ignored. It is normally equal to `p'
3880 (which prevents any backward `on_failure_jump') except right
3881 after a plain `jump', to allow patterns such as:
3884 10: on_failure_jump 3
3885 as used for the *? operator. */
3894 /* If the first character has to match a backreference, that means
3895 that the group was empty (since it already matched). Since this
3896 is the only case that interests us here, we can assume that the
3897 backreference must match the empty string. */
3902 /* Following are the cases which match a character. These end
3908 /* If multibyte is nonzero, the first byte of each
3909 character is an ASCII or a leading code. Otherwise,
3910 each byte is a character. Thus, this works in both
3915 /* For the case of matching this unibyte regex
3916 against multibyte, we must set a leading code of
3917 the corresponding multibyte character. */
3918 int c
= RE_CHAR_TO_MULTIBYTE (p
[1]);
3920 fastmap
[CHAR_LEADING_CODE (c
)] = 1;
3927 /* We could put all the chars except for \n (and maybe \0)
3928 but we don't bother since it is generally not worth it. */
3929 if (!fastmap
) break;
3934 if (!fastmap
) break;
3936 /* Chars beyond end of bitmap are possible matches. */
3937 for (j
= CHARSET_BITMAP_SIZE (&p
[-1]) * BYTEWIDTH
;
3938 j
< (1 << BYTEWIDTH
); j
++)
3944 if (!fastmap
) break;
3945 not = (re_opcode_t
) *(p
- 1) == charset_not
;
3946 for (j
= CHARSET_BITMAP_SIZE (&p
[-1]) * BYTEWIDTH
- 1, p
++;
3948 if (!!(p
[j
/ BYTEWIDTH
] & (1 << (j
% BYTEWIDTH
))) ^ not)
3952 if (/* Any leading code can possibly start a character
3953 which doesn't match the specified set of characters. */
3956 /* If we can match a character class, we can match any
3957 multibyte characters. */
3958 (CHARSET_RANGE_TABLE_EXISTS_P (&p
[-2])
3959 && CHARSET_RANGE_TABLE_BITS (&p
[-2]) != 0))
3962 if (match_any_multibyte_characters
== false)
3964 for (j
= MIN_MULTIBYTE_LEADING_CODE
;
3965 j
<= MAX_MULTIBYTE_LEADING_CODE
; j
++)
3967 match_any_multibyte_characters
= true;
3971 else if (!not && CHARSET_RANGE_TABLE_EXISTS_P (&p
[-2])
3972 && match_any_multibyte_characters
== false)
3974 /* Set fastmap[I] to 1 where I is a leading code of each
3975 multibyte character in the range table. */
3977 unsigned char lc1
, lc2
;
3979 /* Make P points the range table. `+ 2' is to skip flag
3980 bits for a character class. */
3981 p
+= CHARSET_BITMAP_SIZE (&p
[-2]) + 2;
3983 /* Extract the number of ranges in range table into COUNT. */
3984 EXTRACT_NUMBER_AND_INCR (count
, p
);
3985 for (; count
> 0; count
--, p
+= 3)
3987 /* Extract the start and end of each range. */
3988 EXTRACT_CHARACTER (c
, p
);
3989 lc1
= CHAR_LEADING_CODE (c
);
3991 EXTRACT_CHARACTER (c
, p
);
3992 lc2
= CHAR_LEADING_CODE (c
);
3993 for (j
= lc1
; j
<= lc2
; j
++)
4002 if (!fastmap
) break;
4004 not = (re_opcode_t
)p
[-1] == notsyntaxspec
;
4006 for (j
= 0; j
< (1 << BYTEWIDTH
); j
++)
4007 if ((SYNTAX (j
) == (enum syntaxcode
) k
) ^ not)
4011 /* This match depends on text properties. These end with
4012 aborting optimizations. */
4016 case notcategoryspec
:
4017 if (!fastmap
) break;
4018 not = (re_opcode_t
)p
[-1] == notcategoryspec
;
4020 for (j
= (1 << BYTEWIDTH
); j
>= 0; j
--)
4021 if ((CHAR_HAS_CATEGORY (j
, k
)) ^ not)
4024 /* Any leading code can possibly start a character which
4025 has or doesn't has the specified category. */
4026 if (match_any_multibyte_characters
== false)
4028 for (j
= MIN_MULTIBYTE_LEADING_CODE
;
4029 j
<= MAX_MULTIBYTE_LEADING_CODE
; j
++)
4031 match_any_multibyte_characters
= true;
4035 /* All cases after this match the empty string. These end with
4057 EXTRACT_NUMBER_AND_INCR (j
, p
);
4059 /* Backward jumps can only go back to code that we've already
4060 visited. `re_compile' should make sure this is true. */
4065 case on_failure_jump
:
4066 case on_failure_keep_string_jump
:
4067 case on_failure_jump_loop
:
4068 case on_failure_jump_nastyloop
:
4069 case on_failure_jump_smart
:
4075 /* Keep `p1' to allow the `on_failure_jump' we are jumping to
4076 to jump back to "just after here". */
4079 case on_failure_jump
:
4080 case on_failure_keep_string_jump
:
4081 case on_failure_jump_nastyloop
:
4082 case on_failure_jump_loop
:
4083 case on_failure_jump_smart
:
4084 EXTRACT_NUMBER_AND_INCR (j
, p
);
4086 ; /* Backward jump to be ignored. */
4088 { /* We have to look down both arms.
4089 We first go down the "straight" path so as to minimize
4090 stack usage when going through alternatives. */
4091 int r
= analyse_first (p
, pend
, fastmap
, multibyte
);
4099 /* This code simply does not properly handle forward jump_n. */
4100 DEBUG_STATEMENT (EXTRACT_NUMBER (j
, p
); assert (j
< 0));
4102 /* jump_n can either jump or fall through. The (backward) jump
4103 case has already been handled, so we only need to look at the
4104 fallthrough case. */
4108 /* If N == 0, it should be an on_failure_jump_loop instead. */
4109 DEBUG_STATEMENT (EXTRACT_NUMBER (j
, p
+ 2); assert (j
> 0));
4111 /* We only care about one iteration of the loop, so we don't
4112 need to consider the case where this behaves like an
4129 abort (); /* We have listed all the cases. */
4132 /* Getting here means we have found the possible starting
4133 characters for one path of the pattern -- and that the empty
4134 string does not match. We need not follow this path further. */
4138 /* We reached the end without matching anything. */
4141 } /* analyse_first */
4143 /* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
4144 BUFP. A fastmap records which of the (1 << BYTEWIDTH) possible
4145 characters can start a string that matches the pattern. This fastmap
4146 is used by re_search to skip quickly over impossible starting points.
4148 Character codes above (1 << BYTEWIDTH) are not represented in the
4149 fastmap, but the leading codes are represented. Thus, the fastmap
4150 indicates which character sets could start a match.
4152 The caller must supply the address of a (1 << BYTEWIDTH)-byte data
4153 area as BUFP->fastmap.
4155 We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in
4158 Returns 0 if we succeed, -2 if an internal error. */
4161 re_compile_fastmap (struct re_pattern_buffer
*bufp
)
4163 char *fastmap
= bufp
->fastmap
;
4166 assert (fastmap
&& bufp
->buffer
);
4168 memset (fastmap
, 0, 1 << BYTEWIDTH
); /* Assume nothing's valid. */
4169 bufp
->fastmap_accurate
= 1; /* It will be when we're done. */
4171 analysis
= analyse_first (bufp
->buffer
, bufp
->buffer
+ bufp
->used
,
4172 fastmap
, RE_MULTIBYTE_P (bufp
));
4173 bufp
->can_be_null
= (analysis
!= 0);
4175 } /* re_compile_fastmap */
4177 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
4178 ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
4179 this memory for recording register information. STARTS and ENDS
4180 must be allocated using the malloc library routine, and must each
4181 be at least NUM_REGS * sizeof (regoff_t) bytes long.
4183 If NUM_REGS == 0, then subsequent matches should allocate their own
4186 Unless this function is called, the first search or match using
4187 PATTERN_BUFFER will allocate its own register data, without
4188 freeing the old data. */
4191 re_set_registers (struct re_pattern_buffer
*bufp
, struct re_registers
*regs
, unsigned int num_regs
, regoff_t
*starts
, regoff_t
*ends
)
4195 bufp
->regs_allocated
= REGS_REALLOCATE
;
4196 regs
->num_regs
= num_regs
;
4197 regs
->start
= starts
;
4202 bufp
->regs_allocated
= REGS_UNALLOCATED
;
4204 regs
->start
= regs
->end
= 0;
4207 WEAK_ALIAS (__re_set_registers
, re_set_registers
)
4209 /* Searching routines. */
4211 /* Like re_search_2, below, but only one string is specified, and
4212 doesn't let you say where to stop matching. */
4215 re_search (struct re_pattern_buffer
*bufp
, const char *string
, size_t size
,
4216 ssize_t startpos
, ssize_t range
, struct re_registers
*regs
)
4218 return re_search_2 (bufp
, NULL
, 0, string
, size
, startpos
, range
,
4221 WEAK_ALIAS (__re_search
, re_search
)
4223 /* Head address of virtual concatenation of string. */
4224 #define HEAD_ADDR_VSTRING(P) \
4225 (((P) >= size1 ? string2 : string1))
4227 /* Address of POS in the concatenation of virtual string. */
4228 #define POS_ADDR_VSTRING(POS) \
4229 (((POS) >= size1 ? string2 - size1 : string1) + (POS))
4231 /* Using the compiled pattern in BUFP->buffer, first tries to match the
4232 virtual concatenation of STRING1 and STRING2, starting first at index
4233 STARTPOS, then at STARTPOS + 1, and so on.
4235 STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
4237 RANGE is how far to scan while trying to match. RANGE = 0 means try
4238 only at STARTPOS; in general, the last start tried is STARTPOS +
4241 In REGS, return the indices of the virtual concatenation of STRING1
4242 and STRING2 that matched the entire BUFP->buffer and its contained
4245 Do not consider matching one past the index STOP in the virtual
4246 concatenation of STRING1 and STRING2.
4248 We return either the position in the strings at which the match was
4249 found, -1 if no match, or -2 if error (such as failure
4253 re_search_2 (struct re_pattern_buffer
*bufp
, const char *str1
, size_t size1
,
4254 const char *str2
, size_t size2
, ssize_t startpos
, ssize_t range
,
4255 struct re_registers
*regs
, ssize_t stop
)
4258 re_char
*string1
= (re_char
*) str1
;
4259 re_char
*string2
= (re_char
*) str2
;
4260 register char *fastmap
= bufp
->fastmap
;
4261 register RE_TRANSLATE_TYPE translate
= bufp
->translate
;
4262 size_t total_size
= size1
+ size2
;
4263 ssize_t endpos
= startpos
+ range
;
4264 boolean anchored_start
;
4265 /* Nonzero if we are searching multibyte string. */
4266 const boolean multibyte
= RE_TARGET_MULTIBYTE_P (bufp
);
4268 /* Check for out-of-range STARTPOS. */
4269 if (startpos
< 0 || startpos
> total_size
)
4272 /* Fix up RANGE if it might eventually take us outside
4273 the virtual concatenation of STRING1 and STRING2.
4274 Make sure we won't move STARTPOS below 0 or above TOTAL_SIZE. */
4276 range
= 0 - startpos
;
4277 else if (endpos
> total_size
)
4278 range
= total_size
- startpos
;
4280 /* If the search isn't to be a backwards one, don't waste time in a
4281 search for a pattern anchored at beginning of buffer. */
4282 if (bufp
->used
> 0 && (re_opcode_t
) bufp
->buffer
[0] == begbuf
&& range
> 0)
4291 /* In a forward search for something that starts with \=.
4292 don't keep searching past point. */
4293 if (bufp
->used
> 0 && (re_opcode_t
) bufp
->buffer
[0] == at_dot
&& range
> 0)
4295 range
= PT_BYTE
- BEGV_BYTE
- startpos
;
4301 /* Update the fastmap now if not correct already. */
4302 if (fastmap
&& !bufp
->fastmap_accurate
)
4303 re_compile_fastmap (bufp
);
4305 /* See whether the pattern is anchored. */
4306 anchored_start
= (bufp
->buffer
[0] == begline
);
4309 gl_state
.object
= re_match_object
; /* Used by SYNTAX_TABLE_BYTE_TO_CHAR. */
4311 ssize_t charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (POS_AS_IN_BUFFER (startpos
));
4313 SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object
, charpos
, 1);
4317 /* Loop through the string, looking for a place to start matching. */
4320 /* If the pattern is anchored,
4321 skip quickly past places we cannot match.
4322 We don't bother to treat startpos == 0 specially
4323 because that case doesn't repeat. */
4324 if (anchored_start
&& startpos
> 0)
4326 if (! ((startpos
<= size1
? string1
[startpos
- 1]
4327 : string2
[startpos
- size1
- 1])
4332 /* If a fastmap is supplied, skip quickly over characters that
4333 cannot be the start of a match. If the pattern can match the
4334 null string, however, we don't need to skip characters; we want
4335 the first null string. */
4336 if (fastmap
&& startpos
< total_size
&& !bufp
->can_be_null
)
4338 register re_char
*d
;
4339 register re_wchar_t buf_ch
;
4341 d
= POS_ADDR_VSTRING (startpos
);
4343 if (range
> 0) /* Searching forwards. */
4345 ssize_t irange
= range
, lim
= 0;
4347 if (startpos
< size1
&& startpos
+ range
>= size1
)
4348 lim
= range
- (size1
- startpos
);
4350 /* Written out as an if-else to avoid testing `translate'
4352 if (RE_TRANSLATE_P (translate
))
4359 buf_ch
= STRING_CHAR_AND_LENGTH (d
, buf_charlen
);
4360 buf_ch
= RE_TRANSLATE (translate
, buf_ch
);
4361 if (fastmap
[CHAR_LEADING_CODE (buf_ch
)])
4364 range
-= buf_charlen
;
4370 register re_wchar_t ch
, translated
;
4373 ch
= RE_CHAR_TO_MULTIBYTE (buf_ch
);
4374 translated
= RE_TRANSLATE (translate
, ch
);
4375 if (translated
!= ch
4376 && (ch
= RE_CHAR_TO_UNIBYTE (translated
)) >= 0)
4378 if (fastmap
[buf_ch
])
4391 buf_ch
= STRING_CHAR_AND_LENGTH (d
, buf_charlen
);
4392 if (fastmap
[CHAR_LEADING_CODE (buf_ch
)])
4394 range
-= buf_charlen
;
4398 while (range
> lim
&& !fastmap
[*d
])
4404 startpos
+= irange
- range
;
4406 else /* Searching backwards. */
4410 buf_ch
= STRING_CHAR (d
);
4411 buf_ch
= TRANSLATE (buf_ch
);
4412 if (! fastmap
[CHAR_LEADING_CODE (buf_ch
)])
4417 register re_wchar_t ch
, translated
;
4420 ch
= RE_CHAR_TO_MULTIBYTE (buf_ch
);
4421 translated
= TRANSLATE (ch
);
4422 if (translated
!= ch
4423 && (ch
= RE_CHAR_TO_UNIBYTE (translated
)) >= 0)
4425 if (! fastmap
[TRANSLATE (buf_ch
)])
4431 /* If can't match the null string, and that's all we have left, fail. */
4432 if (range
>= 0 && startpos
== total_size
&& fastmap
4433 && !bufp
->can_be_null
)
4436 val
= re_match_2_internal (bufp
, string1
, size1
, string2
, size2
,
4437 startpos
, regs
, stop
);
4450 /* Update STARTPOS to the next character boundary. */
4453 re_char
*p
= POS_ADDR_VSTRING (startpos
);
4454 int len
= BYTES_BY_CHAR_HEAD (*p
);
4472 /* Update STARTPOS to the previous character boundary. */
4475 re_char
*p
= POS_ADDR_VSTRING (startpos
) + 1;
4477 re_char
*phead
= HEAD_ADDR_VSTRING (startpos
);
4479 /* Find the head of multibyte form. */
4480 PREV_CHAR_BOUNDARY (p
, phead
);
4481 range
+= p0
- 1 - p
;
4485 startpos
-= p0
- 1 - p
;
4491 WEAK_ALIAS (__re_search_2
, re_search_2
)
4493 /* Declarations and macros for re_match_2. */
4495 static int bcmp_translate (re_char
*s1
, re_char
*s2
,
4496 register ssize_t len
,
4497 RE_TRANSLATE_TYPE translate
,
4498 const int multibyte
);
4500 /* This converts PTR, a pointer into one of the search strings `string1'
4501 and `string2' into an offset from the beginning of that string. */
4502 #define POINTER_TO_OFFSET(ptr) \
4503 (FIRST_STRING_P (ptr) \
4505 : (ptr) - string2 + (ptrdiff_t) size1)
4507 /* Call before fetching a character with *d. This switches over to
4508 string2 if necessary.
4509 Check re_match_2_internal for a discussion of why end_match_2 might
4510 not be within string2 (but be equal to end_match_1 instead). */
4511 #define PREFETCH() \
4514 /* End of string2 => fail. */ \
4515 if (dend == end_match_2) \
4517 /* End of string1 => advance to string2. */ \
4519 dend = end_match_2; \
4522 /* Call before fetching a char with *d if you already checked other limits.
4523 This is meant for use in lookahead operations like wordend, etc..
4524 where we might need to look at parts of the string that might be
4525 outside of the LIMITs (i.e past `stop'). */
4526 #define PREFETCH_NOLIMIT() \
4530 dend = end_match_2; \
4533 /* Test if at very beginning or at very end of the virtual concatenation
4534 of `string1' and `string2'. If only one string, it's `string2'. */
4535 #define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2)
4536 #define AT_STRINGS_END(d) ((d) == end2)
4538 /* Disabled due to a compiler bug -- see comment at case wordbound */
4540 /* The comment at case wordbound is following one, but we don't use
4541 AT_WORD_BOUNDARY anymore to support multibyte form.
4543 The DEC Alpha C compiler 3.x generates incorrect code for the
4544 test WORDCHAR_P (d - 1) != WORDCHAR_P (d) in the expansion of
4545 AT_WORD_BOUNDARY, so this code is disabled. Expanding the
4546 macro and introducing temporary variables works around the bug. */
4549 /* Test if D points to a character which is word-constituent. We have
4550 two special cases to check for: if past the end of string1, look at
4551 the first character in string2; and if before the beginning of
4552 string2, look at the last character in string1. */
4553 #define WORDCHAR_P(d) \
4554 (SYNTAX ((d) == end1 ? *string2 \
4555 : (d) == string2 - 1 ? *(end1 - 1) : *(d)) \
4558 /* Test if the character before D and the one at D differ with respect
4559 to being word-constituent. */
4560 #define AT_WORD_BOUNDARY(d) \
4561 (AT_STRINGS_BEG (d) || AT_STRINGS_END (d) \
4562 || WORDCHAR_P (d - 1) != WORDCHAR_P (d))
4565 /* Free everything we malloc. */
4566 #ifdef MATCH_MAY_ALLOCATE
4567 # define FREE_VAR(var) \
4575 # define FREE_VARIABLES() \
4577 REGEX_FREE_STACK (fail_stack.stack); \
4578 FREE_VAR (regstart); \
4579 FREE_VAR (regend); \
4580 FREE_VAR (best_regstart); \
4581 FREE_VAR (best_regend); \
4584 # define FREE_VARIABLES() ((void)0) /* Do nothing! But inhibit gcc warning. */
4585 #endif /* not MATCH_MAY_ALLOCATE */
4588 /* Optimization routines. */
4590 /* If the operation is a match against one or more chars,
4591 return a pointer to the next operation, else return NULL. */
4593 skip_one_char (const_re_char
*p
)
4606 if (CHARSET_RANGE_TABLE_EXISTS_P (p
- 1))
4609 p
= CHARSET_RANGE_TABLE (p
- 1);
4610 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
4611 p
= CHARSET_RANGE_TABLE_END (p
, mcnt
);
4614 p
+= 1 + CHARSET_BITMAP_SIZE (p
- 1);
4621 case notcategoryspec
:
4633 /* Jump over non-matching operations. */
4635 skip_noops (const_re_char
*p
, const_re_char
*pend
)
4649 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
4660 /* Non-zero if "p1 matches something" implies "p2 fails". */
4662 mutually_exclusive_p (struct re_pattern_buffer
*bufp
, const_re_char
*p1
,
4666 const boolean multibyte
= RE_MULTIBYTE_P (bufp
);
4667 unsigned char *pend
= bufp
->buffer
+ bufp
->used
;
4669 assert (p1
>= bufp
->buffer
&& p1
< pend
4670 && p2
>= bufp
->buffer
&& p2
<= pend
);
4672 /* Skip over open/close-group commands.
4673 If what follows this loop is a ...+ construct,
4674 look at what begins its body, since we will have to
4675 match at least one of that. */
4676 p2
= skip_noops (p2
, pend
);
4677 /* The same skip can be done for p1, except that this function
4678 is only used in the case where p1 is a simple match operator. */
4679 /* p1 = skip_noops (p1, pend); */
4681 assert (p1
>= bufp
->buffer
&& p1
< pend
4682 && p2
>= bufp
->buffer
&& p2
<= pend
);
4684 op2
= p2
== pend
? succeed
: *p2
;
4690 /* If we're at the end of the pattern, we can change. */
4691 if (skip_one_char (p1
))
4693 DEBUG_PRINT (" End of pattern: fast loop.\n");
4701 register re_wchar_t c
4702 = (re_opcode_t
) *p2
== endline
? '\n'
4703 : RE_STRING_CHAR (p2
+ 2, multibyte
);
4705 if ((re_opcode_t
) *p1
== exactn
)
4707 if (c
!= RE_STRING_CHAR (p1
+ 2, multibyte
))
4709 DEBUG_PRINT (" '%c' != '%c' => fast loop.\n", c
, p1
[2]);
4714 else if ((re_opcode_t
) *p1
== charset
4715 || (re_opcode_t
) *p1
== charset_not
)
4717 int not = (re_opcode_t
) *p1
== charset_not
;
4719 /* Test if C is listed in charset (or charset_not)
4721 if (! multibyte
|| IS_REAL_ASCII (c
))
4723 if (c
< CHARSET_BITMAP_SIZE (p1
) * BYTEWIDTH
4724 && p1
[2 + c
/ BYTEWIDTH
] & (1 << (c
% BYTEWIDTH
)))
4727 else if (CHARSET_RANGE_TABLE_EXISTS_P (p1
))
4728 CHARSET_LOOKUP_RANGE_TABLE (not, c
, p1
);
4730 /* `not' is equal to 1 if c would match, which means
4731 that we can't change to pop_failure_jump. */
4734 DEBUG_PRINT (" No match => fast loop.\n");
4738 else if ((re_opcode_t
) *p1
== anychar
4741 DEBUG_PRINT (" . != \\n => fast loop.\n");
4749 if ((re_opcode_t
) *p1
== exactn
)
4750 /* Reuse the code above. */
4751 return mutually_exclusive_p (bufp
, p2
, p1
);
4753 /* It is hard to list up all the character in charset
4754 P2 if it includes multibyte character. Give up in
4756 else if (!multibyte
|| !CHARSET_RANGE_TABLE_EXISTS_P (p2
))
4758 /* Now, we are sure that P2 has no range table.
4759 So, for the size of bitmap in P2, `p2[1]' is
4760 enough. But P1 may have range table, so the
4761 size of bitmap table of P1 is extracted by
4762 using macro `CHARSET_BITMAP_SIZE'.
4764 In a multibyte case, we know that all the character
4765 listed in P2 is ASCII. In a unibyte case, P1 has only a
4766 bitmap table. So, in both cases, it is enough to test
4767 only the bitmap table of P1. */
4769 if ((re_opcode_t
) *p1
== charset
)
4772 /* We win if the charset inside the loop
4773 has no overlap with the one after the loop. */
4776 && idx
< CHARSET_BITMAP_SIZE (p1
));
4778 if ((p2
[2 + idx
] & p1
[2 + idx
]) != 0)
4782 || idx
== CHARSET_BITMAP_SIZE (p1
))
4784 DEBUG_PRINT (" No match => fast loop.\n");
4788 else if ((re_opcode_t
) *p1
== charset_not
)
4791 /* We win if the charset_not inside the loop lists
4792 every character listed in the charset after. */
4793 for (idx
= 0; idx
< (int) p2
[1]; idx
++)
4794 if (! (p2
[2 + idx
] == 0
4795 || (idx
< CHARSET_BITMAP_SIZE (p1
)
4796 && ((p2
[2 + idx
] & ~ p1
[2 + idx
]) == 0))))
4801 DEBUG_PRINT (" No match => fast loop.\n");
4814 /* Reuse the code above. */
4815 return mutually_exclusive_p (bufp
, p2
, p1
);
4817 /* When we have two charset_not, it's very unlikely that
4818 they don't overlap. The union of the two sets of excluded
4819 chars should cover all possible chars, which, as a matter of
4820 fact, is virtually impossible in multibyte buffers. */
4826 return ((re_opcode_t
) *p1
== syntaxspec
&& p1
[1] == Sword
);
4828 return ((re_opcode_t
) *p1
== syntaxspec
4829 && (p1
[1] == Ssymbol
|| p1
[1] == Sword
));
4831 return ((re_opcode_t
) *p1
== syntaxspec
&& p1
[1] == p2
[1]);
4834 return ((re_opcode_t
) *p1
== notsyntaxspec
&& p1
[1] == Sword
);
4836 return ((re_opcode_t
) *p1
== notsyntaxspec
4837 && (p1
[1] == Ssymbol
|| p1
[1] == Sword
));
4839 return ((re_opcode_t
) *p1
== notsyntaxspec
&& p1
[1] == p2
[1]);
4842 return (((re_opcode_t
) *p1
== notsyntaxspec
4843 || (re_opcode_t
) *p1
== syntaxspec
)
4848 return ((re_opcode_t
) *p1
== notcategoryspec
&& p1
[1] == p2
[1]);
4849 case notcategoryspec
:
4850 return ((re_opcode_t
) *p1
== categoryspec
&& p1
[1] == p2
[1]);
4862 /* Matching routines. */
4864 #ifndef emacs /* Emacs never uses this. */
4865 /* re_match is like re_match_2 except it takes only a single string. */
4868 re_match (struct re_pattern_buffer
*bufp
, const char *string
,
4869 size_t size
, ssize_t pos
, struct re_registers
*regs
)
4871 regoff_t result
= re_match_2_internal (bufp
, NULL
, 0, (re_char
*) string
,
4872 size
, pos
, regs
, size
);
4875 WEAK_ALIAS (__re_match
, re_match
)
4876 #endif /* not emacs */
4879 /* In Emacs, this is the string or buffer in which we
4880 are matching. It is used for looking up syntax properties. */
4881 Lisp_Object re_match_object
;
4884 /* re_match_2 matches the compiled pattern in BUFP against the
4885 the (virtual) concatenation of STRING1 and STRING2 (of length SIZE1
4886 and SIZE2, respectively). We start matching at POS, and stop
4889 If REGS is non-null and the `no_sub' field of BUFP is nonzero, we
4890 store offsets for the substring each group matched in REGS. See the
4891 documentation for exactly how many groups we fill.
4893 We return -1 if no match, -2 if an internal error (such as the
4894 failure stack overflowing). Otherwise, we return the length of the
4895 matched substring. */
4898 re_match_2 (struct re_pattern_buffer
*bufp
, const char *string1
,
4899 size_t size1
, const char *string2
, size_t size2
, ssize_t pos
,
4900 struct re_registers
*regs
, ssize_t stop
)
4906 gl_state
.object
= re_match_object
; /* Used by SYNTAX_TABLE_BYTE_TO_CHAR. */
4907 charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (POS_AS_IN_BUFFER (pos
));
4908 SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object
, charpos
, 1);
4911 result
= re_match_2_internal (bufp
, (re_char
*) string1
, size1
,
4912 (re_char
*) string2
, size2
,
4916 WEAK_ALIAS (__re_match_2
, re_match_2
)
4919 /* This is a separate function so that we can force an alloca cleanup
4922 re_match_2_internal (struct re_pattern_buffer
*bufp
, const_re_char
*string1
,
4923 size_t size1
, const_re_char
*string2
, size_t size2
,
4924 ssize_t pos
, struct re_registers
*regs
, ssize_t stop
)
4926 /* General temporaries. */
4930 /* Just past the end of the corresponding string. */
4931 re_char
*end1
, *end2
;
4933 /* Pointers into string1 and string2, just past the last characters in
4934 each to consider matching. */
4935 re_char
*end_match_1
, *end_match_2
;
4937 /* Where we are in the data, and the end of the current string. */
4940 /* Used sometimes to remember where we were before starting matching
4941 an operator so that we can go back in case of failure. This "atomic"
4942 behavior of matching opcodes is indispensable to the correctness
4943 of the on_failure_keep_string_jump optimization. */
4946 /* Where we are in the pattern, and the end of the pattern. */
4947 re_char
*p
= bufp
->buffer
;
4948 re_char
*pend
= p
+ bufp
->used
;
4950 /* We use this to map every character in the string. */
4951 RE_TRANSLATE_TYPE translate
= bufp
->translate
;
4953 /* Nonzero if BUFP is setup from a multibyte regex. */
4954 const boolean multibyte
= RE_MULTIBYTE_P (bufp
);
4956 /* Nonzero if STRING1/STRING2 are multibyte. */
4957 const boolean target_multibyte
= RE_TARGET_MULTIBYTE_P (bufp
);
4959 /* Failure point stack. Each place that can handle a failure further
4960 down the line pushes a failure point on this stack. It consists of
4961 regstart, and regend for all registers corresponding to
4962 the subexpressions we're currently inside, plus the number of such
4963 registers, and, finally, two char *'s. The first char * is where
4964 to resume scanning the pattern; the second one is where to resume
4965 scanning the strings. */
4966 #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
4967 fail_stack_type fail_stack
;
4969 #ifdef DEBUG_COMPILES_ARGUMENTS
4970 unsigned nfailure_points_pushed
= 0, nfailure_points_popped
= 0;
4973 #if defined REL_ALLOC && defined REGEX_MALLOC
4974 /* This holds the pointer to the failure stack, when
4975 it is allocated relocatably. */
4976 fail_stack_elt_t
*failure_stack_ptr
;
4979 /* We fill all the registers internally, independent of what we
4980 return, for use in backreferences. The number here includes
4981 an element for register zero. */
4982 size_t num_regs
= bufp
->re_nsub
+ 1;
4984 /* Information on the contents of registers. These are pointers into
4985 the input strings; they record just what was matched (on this
4986 attempt) by a subexpression part of the pattern, that is, the
4987 regnum-th regstart pointer points to where in the pattern we began
4988 matching and the regnum-th regend points to right after where we
4989 stopped matching the regnum-th subexpression. (The zeroth register
4990 keeps track of what the whole pattern matches.) */
4991 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
4992 re_char
**regstart
, **regend
;
4995 /* The following record the register info as found in the above
4996 variables when we find a match better than any we've seen before.
4997 This happens as we backtrack through the failure points, which in
4998 turn happens only if we have not yet matched the entire string. */
4999 unsigned best_regs_set
= false;
5000 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
5001 re_char
**best_regstart
, **best_regend
;
5004 /* Logically, this is `best_regend[0]'. But we don't want to have to
5005 allocate space for that if we're not allocating space for anything
5006 else (see below). Also, we never need info about register 0 for
5007 any of the other register vectors, and it seems rather a kludge to
5008 treat `best_regend' differently than the rest. So we keep track of
5009 the end of the best match so far in a separate variable. We
5010 initialize this to NULL so that when we backtrack the first time
5011 and need to test it, it's not garbage. */
5012 re_char
*match_end
= NULL
;
5014 #ifdef DEBUG_COMPILES_ARGUMENTS
5015 /* Counts the total number of registers pushed. */
5016 unsigned num_regs_pushed
= 0;
5019 DEBUG_PRINT ("\n\nEntering re_match_2.\n");
5023 #ifdef MATCH_MAY_ALLOCATE
5024 /* Do not bother to initialize all the register variables if there are
5025 no groups in the pattern, as it takes a fair amount of time. If
5026 there are groups, we include space for register 0 (the whole
5027 pattern), even though we never use it, since it simplifies the
5028 array indexing. We should fix this. */
5031 regstart
= REGEX_TALLOC (num_regs
, re_char
*);
5032 regend
= REGEX_TALLOC (num_regs
, re_char
*);
5033 best_regstart
= REGEX_TALLOC (num_regs
, re_char
*);
5034 best_regend
= REGEX_TALLOC (num_regs
, re_char
*);
5036 if (!(regstart
&& regend
&& best_regstart
&& best_regend
))
5044 /* We must initialize all our variables to NULL, so that
5045 `FREE_VARIABLES' doesn't try to free them. */
5046 regstart
= regend
= best_regstart
= best_regend
= NULL
;
5048 #endif /* MATCH_MAY_ALLOCATE */
5050 /* The starting position is bogus. */
5051 if (pos
< 0 || pos
> size1
+ size2
)
5057 /* Initialize subexpression text positions to -1 to mark ones that no
5058 start_memory/stop_memory has been seen for. Also initialize the
5059 register information struct. */
5060 for (reg
= 1; reg
< num_regs
; reg
++)
5061 regstart
[reg
] = regend
[reg
] = NULL
;
5063 /* We move `string1' into `string2' if the latter's empty -- but not if
5064 `string1' is null. */
5065 if (size2
== 0 && string1
!= NULL
)
5072 end1
= string1
+ size1
;
5073 end2
= string2
+ size2
;
5075 /* `p' scans through the pattern as `d' scans through the data.
5076 `dend' is the end of the input string that `d' points within. `d'
5077 is advanced into the following input string whenever necessary, but
5078 this happens before fetching; therefore, at the beginning of the
5079 loop, `d' can be pointing at the end of a string, but it cannot
5083 /* Only match within string2. */
5084 d
= string2
+ pos
- size1
;
5085 dend
= end_match_2
= string2
+ stop
- size1
;
5086 end_match_1
= end1
; /* Just to give it a value. */
5092 /* Only match within string1. */
5093 end_match_1
= string1
+ stop
;
5095 When we reach end_match_1, PREFETCH normally switches to string2.
5096 But in the present case, this means that just doing a PREFETCH
5097 makes us jump from `stop' to `gap' within the string.
5098 What we really want here is for the search to stop as
5099 soon as we hit end_match_1. That's why we set end_match_2
5100 to end_match_1 (since PREFETCH fails as soon as we hit
5102 end_match_2
= end_match_1
;
5105 { /* It's important to use this code when stop == size so that
5106 moving `d' from end1 to string2 will not prevent the d == dend
5107 check from catching the end of string. */
5109 end_match_2
= string2
+ stop
- size1
;
5115 DEBUG_PRINT ("The compiled pattern is: ");
5116 DEBUG_PRINT_COMPILED_PATTERN (bufp
, p
, pend
);
5117 DEBUG_PRINT ("The string to match is: `");
5118 DEBUG_PRINT_DOUBLE_STRING (d
, string1
, size1
, string2
, size2
);
5119 DEBUG_PRINT ("'\n");
5121 /* This loops over pattern commands. It exits by returning from the
5122 function if the match is complete, or it drops through if the match
5123 fails at this starting point in the input data. */
5126 DEBUG_PRINT ("\n%p: ", p
);
5132 /* End of pattern means we might have succeeded. */
5133 DEBUG_PRINT ("end of pattern ... ");
5135 /* If we haven't matched the entire string, and we want the
5136 longest match, try backtracking. */
5137 if (d
!= end_match_2
)
5139 /* 1 if this match ends in the same string (string1 or string2)
5140 as the best previous match. */
5141 boolean same_str_p
= (FIRST_STRING_P (match_end
)
5142 == FIRST_STRING_P (d
));
5143 /* 1 if this match is the best seen so far. */
5144 boolean best_match_p
;
5146 /* AIX compiler got confused when this was combined
5147 with the previous declaration. */
5149 best_match_p
= d
> match_end
;
5151 best_match_p
= !FIRST_STRING_P (d
);
5153 DEBUG_PRINT ("backtracking.\n");
5155 if (!FAIL_STACK_EMPTY ())
5156 { /* More failure points to try. */
5158 /* If exceeds best match so far, save it. */
5159 if (!best_regs_set
|| best_match_p
)
5161 best_regs_set
= true;
5164 DEBUG_PRINT ("\nSAVING match as best so far.\n");
5166 for (reg
= 1; reg
< num_regs
; reg
++)
5168 best_regstart
[reg
] = regstart
[reg
];
5169 best_regend
[reg
] = regend
[reg
];
5175 /* If no failure points, don't restore garbage. And if
5176 last match is real best match, don't restore second
5178 else if (best_regs_set
&& !best_match_p
)
5181 /* Restore best match. It may happen that `dend ==
5182 end_match_1' while the restored d is in string2.
5183 For example, the pattern `x.*y.*z' against the
5184 strings `x-' and `y-z-', if the two strings are
5185 not consecutive in memory. */
5186 DEBUG_PRINT ("Restoring best registers.\n");
5189 dend
= ((d
>= string1
&& d
<= end1
)
5190 ? end_match_1
: end_match_2
);
5192 for (reg
= 1; reg
< num_regs
; reg
++)
5194 regstart
[reg
] = best_regstart
[reg
];
5195 regend
[reg
] = best_regend
[reg
];
5198 } /* d != end_match_2 */
5201 DEBUG_PRINT ("Accepting match.\n");
5203 /* If caller wants register contents data back, do it. */
5204 if (regs
&& !bufp
->no_sub
)
5206 /* Have the register data arrays been allocated? */
5207 if (bufp
->regs_allocated
== REGS_UNALLOCATED
)
5208 { /* No. So allocate them with malloc. We need one
5209 extra element beyond `num_regs' for the `-1' marker
5211 regs
->num_regs
= MAX (RE_NREGS
, num_regs
+ 1);
5212 regs
->start
= TALLOC (regs
->num_regs
, regoff_t
);
5213 regs
->end
= TALLOC (regs
->num_regs
, regoff_t
);
5214 if (regs
->start
== NULL
|| regs
->end
== NULL
)
5219 bufp
->regs_allocated
= REGS_REALLOCATE
;
5221 else if (bufp
->regs_allocated
== REGS_REALLOCATE
)
5222 { /* Yes. If we need more elements than were already
5223 allocated, reallocate them. If we need fewer, just
5225 if (regs
->num_regs
< num_regs
+ 1)
5227 regs
->num_regs
= num_regs
+ 1;
5228 RETALLOC (regs
->start
, regs
->num_regs
, regoff_t
);
5229 RETALLOC (regs
->end
, regs
->num_regs
, regoff_t
);
5230 if (regs
->start
== NULL
|| regs
->end
== NULL
)
5239 /* These braces fend off a "empty body in an else-statement"
5240 warning under GCC when assert expands to nothing. */
5241 assert (bufp
->regs_allocated
== REGS_FIXED
);
5244 /* Convert the pointer data in `regstart' and `regend' to
5245 indices. Register zero has to be set differently,
5246 since we haven't kept track of any info for it. */
5247 if (regs
->num_regs
> 0)
5249 regs
->start
[0] = pos
;
5250 regs
->end
[0] = POINTER_TO_OFFSET (d
);
5253 /* Go through the first `min (num_regs, regs->num_regs)'
5254 registers, since that is all we initialized. */
5255 for (reg
= 1; reg
< MIN (num_regs
, regs
->num_regs
); reg
++)
5257 if (REG_UNSET (regstart
[reg
]) || REG_UNSET (regend
[reg
]))
5258 regs
->start
[reg
] = regs
->end
[reg
] = -1;
5261 regs
->start
[reg
] = POINTER_TO_OFFSET (regstart
[reg
]);
5262 regs
->end
[reg
] = POINTER_TO_OFFSET (regend
[reg
]);
5266 /* If the regs structure we return has more elements than
5267 were in the pattern, set the extra elements to -1. If
5268 we (re)allocated the registers, this is the case,
5269 because we always allocate enough to have at least one
5271 for (reg
= num_regs
; reg
< regs
->num_regs
; reg
++)
5272 regs
->start
[reg
] = regs
->end
[reg
] = -1;
5273 } /* regs && !bufp->no_sub */
5275 DEBUG_PRINT ("%u failure points pushed, %u popped (%u remain).\n",
5276 nfailure_points_pushed
, nfailure_points_popped
,
5277 nfailure_points_pushed
- nfailure_points_popped
);
5278 DEBUG_PRINT ("%u registers pushed.\n", num_regs_pushed
);
5280 dcnt
= POINTER_TO_OFFSET (d
) - pos
;
5282 DEBUG_PRINT ("Returning %td from re_match_2.\n", dcnt
);
5288 /* Otherwise match next pattern command. */
5291 /* Ignore these. Used to ignore the n of succeed_n's which
5292 currently have n == 0. */
5294 DEBUG_PRINT ("EXECUTING no_op.\n");
5298 DEBUG_PRINT ("EXECUTING succeed.\n");
5301 /* Match the next n pattern characters exactly. The following
5302 byte in the pattern defines n, and the n bytes after that
5303 are the characters to match. */
5306 DEBUG_PRINT ("EXECUTING exactn %d.\n", mcnt
);
5308 /* Remember the start point to rollback upon failure. */
5312 /* This is written out as an if-else so we don't waste time
5313 testing `translate' inside the loop. */
5314 if (RE_TRANSLATE_P (translate
))
5318 if (RE_TRANSLATE (translate
, *d
) != *p
++)
5338 /* The cost of testing `translate' is comparatively small. */
5339 if (target_multibyte
)
5342 int pat_charlen
, buf_charlen
;
5347 pat_ch
= STRING_CHAR_AND_LENGTH (p
, pat_charlen
);
5350 pat_ch
= RE_CHAR_TO_MULTIBYTE (*p
);
5353 buf_ch
= STRING_CHAR_AND_LENGTH (d
, buf_charlen
);
5355 if (TRANSLATE (buf_ch
) != pat_ch
)
5363 mcnt
-= pat_charlen
;
5375 pat_ch
= STRING_CHAR_AND_LENGTH (p
, pat_charlen
);
5376 pat_ch
= RE_CHAR_TO_UNIBYTE (pat_ch
);
5383 buf_ch
= RE_CHAR_TO_MULTIBYTE (*d
);
5384 if (! CHAR_BYTE8_P (buf_ch
))
5386 buf_ch
= TRANSLATE (buf_ch
);
5387 buf_ch
= RE_CHAR_TO_UNIBYTE (buf_ch
);
5393 if (buf_ch
!= pat_ch
)
5406 /* Match any character except possibly a newline or a null. */
5412 DEBUG_PRINT ("EXECUTING anychar.\n");
5415 buf_ch
= RE_STRING_CHAR_AND_LENGTH (d
, buf_charlen
,
5417 buf_ch
= TRANSLATE (buf_ch
);
5419 if ((!(bufp
->syntax
& RE_DOT_NEWLINE
)
5421 || ((bufp
->syntax
& RE_DOT_NOT_NULL
)
5422 && buf_ch
== '\000'))
5425 DEBUG_PRINT (" Matched `%d'.\n", *d
);
5434 register unsigned int c
;
5435 boolean
not = (re_opcode_t
) *(p
- 1) == charset_not
;
5438 /* Start of actual range_table, or end of bitmap if there is no
5440 re_char
*range_table
IF_LINT (= NULL
);
5442 /* Nonzero if there is a range table. */
5443 int range_table_exists
;
5445 /* Number of ranges of range table. This is not included
5446 in the initial byte-length of the command. */
5449 /* Whether matching against a unibyte character. */
5450 boolean unibyte_char
= false;
5452 DEBUG_PRINT ("EXECUTING charset%s.\n", not ? "_not" : "");
5454 range_table_exists
= CHARSET_RANGE_TABLE_EXISTS_P (&p
[-1]);
5456 if (range_table_exists
)
5458 range_table
= CHARSET_RANGE_TABLE (&p
[-1]); /* Past the bitmap. */
5459 EXTRACT_NUMBER_AND_INCR (count
, range_table
);
5463 c
= RE_STRING_CHAR_AND_LENGTH (d
, len
, target_multibyte
);
5464 if (target_multibyte
)
5469 c1
= RE_CHAR_TO_UNIBYTE (c
);
5472 unibyte_char
= true;
5478 int c1
= RE_CHAR_TO_MULTIBYTE (c
);
5480 if (! CHAR_BYTE8_P (c1
))
5482 c1
= TRANSLATE (c1
);
5483 c1
= RE_CHAR_TO_UNIBYTE (c1
);
5486 unibyte_char
= true;
5491 unibyte_char
= true;
5494 if (unibyte_char
&& c
< (1 << BYTEWIDTH
))
5495 { /* Lookup bitmap. */
5496 /* Cast to `unsigned' instead of `unsigned char' in
5497 case the bit list is a full 32 bytes long. */
5498 if (c
< (unsigned) (CHARSET_BITMAP_SIZE (&p
[-1]) * BYTEWIDTH
)
5499 && p
[1 + c
/ BYTEWIDTH
] & (1 << (c
% BYTEWIDTH
)))
5503 else if (range_table_exists
)
5505 int class_bits
= CHARSET_RANGE_TABLE_BITS (&p
[-1]);
5507 if ( (class_bits
& BIT_LOWER
&& ISLOWER (c
))
5508 | (class_bits
& BIT_MULTIBYTE
)
5509 | (class_bits
& BIT_PUNCT
&& ISPUNCT (c
))
5510 | (class_bits
& BIT_SPACE
&& ISSPACE (c
))
5511 | (class_bits
& BIT_UPPER
&& ISUPPER (c
))
5512 | (class_bits
& BIT_WORD
&& ISWORD (c
)))
5515 CHARSET_LOOKUP_RANGE_TABLE_RAW (not, c
, range_table
, count
);
5519 if (range_table_exists
)
5520 p
= CHARSET_RANGE_TABLE_END (range_table
, count
);
5522 p
+= CHARSET_BITMAP_SIZE (&p
[-1]) + 1;
5524 if (!not) goto fail
;
5531 /* The beginning of a group is represented by start_memory.
5532 The argument is the register number. The text
5533 matched within the group is recorded (in the internal
5534 registers data structure) under the register number. */
5536 DEBUG_PRINT ("EXECUTING start_memory %d:\n", *p
);
5538 /* In case we need to undo this operation (via backtracking). */
5539 PUSH_FAILURE_REG (*p
);
5542 regend
[*p
] = NULL
; /* probably unnecessary. -sm */
5543 DEBUG_PRINT (" regstart: %td\n", POINTER_TO_OFFSET (regstart
[*p
]));
5545 /* Move past the register number and inner group count. */
5550 /* The stop_memory opcode represents the end of a group. Its
5551 argument is the same as start_memory's: the register number. */
5553 DEBUG_PRINT ("EXECUTING stop_memory %d:\n", *p
);
5555 assert (!REG_UNSET (regstart
[*p
]));
5556 /* Strictly speaking, there should be code such as:
5558 assert (REG_UNSET (regend[*p]));
5559 PUSH_FAILURE_REGSTOP ((unsigned int)*p);
5561 But the only info to be pushed is regend[*p] and it is known to
5562 be UNSET, so there really isn't anything to push.
5563 Not pushing anything, on the other hand deprives us from the
5564 guarantee that regend[*p] is UNSET since undoing this operation
5565 will not reset its value properly. This is not important since
5566 the value will only be read on the next start_memory or at
5567 the very end and both events can only happen if this stop_memory
5571 DEBUG_PRINT (" regend: %td\n", POINTER_TO_OFFSET (regend
[*p
]));
5573 /* Move past the register number and the inner group count. */
5578 /* \<digit> has been turned into a `duplicate' command which is
5579 followed by the numeric value of <digit> as the register number. */
5582 register re_char
*d2
, *dend2
;
5583 int regno
= *p
++; /* Get which register to match against. */
5584 DEBUG_PRINT ("EXECUTING duplicate %d.\n", regno
);
5586 /* Can't back reference a group which we've never matched. */
5587 if (REG_UNSET (regstart
[regno
]) || REG_UNSET (regend
[regno
]))
5590 /* Where in input to try to start matching. */
5591 d2
= regstart
[regno
];
5593 /* Remember the start point to rollback upon failure. */
5596 /* Where to stop matching; if both the place to start and
5597 the place to stop matching are in the same string, then
5598 set to the place to stop, otherwise, for now have to use
5599 the end of the first string. */
5601 dend2
= ((FIRST_STRING_P (regstart
[regno
])
5602 == FIRST_STRING_P (regend
[regno
]))
5603 ? regend
[regno
] : end_match_1
);
5608 /* If necessary, advance to next segment in register
5612 if (dend2
== end_match_2
) break;
5613 if (dend2
== regend
[regno
]) break;
5615 /* End of string1 => advance to string2. */
5617 dend2
= regend
[regno
];
5619 /* At end of register contents => success */
5620 if (d2
== dend2
) break;
5622 /* If necessary, advance to next segment in data. */
5625 /* How many characters left in this segment to match. */
5628 /* Want how many consecutive characters we can match in
5629 one shot, so, if necessary, adjust the count. */
5630 if (dcnt
> dend2
- d2
)
5633 /* Compare that many; failure if mismatch, else move
5635 if (RE_TRANSLATE_P (translate
)
5636 ? bcmp_translate (d
, d2
, dcnt
, translate
, target_multibyte
)
5637 : memcmp (d
, d2
, dcnt
))
5642 d
+= dcnt
, d2
+= dcnt
;
5648 /* begline matches the empty string at the beginning of the string
5649 (unless `not_bol' is set in `bufp'), and after newlines. */
5651 DEBUG_PRINT ("EXECUTING begline.\n");
5653 if (AT_STRINGS_BEG (d
))
5655 if (!bufp
->not_bol
) break;
5660 GET_CHAR_BEFORE_2 (c
, d
, string1
, end1
, string2
, end2
);
5664 /* In all other cases, we fail. */
5668 /* endline is the dual of begline. */
5670 DEBUG_PRINT ("EXECUTING endline.\n");
5672 if (AT_STRINGS_END (d
))
5674 if (!bufp
->not_eol
) break;
5678 PREFETCH_NOLIMIT ();
5685 /* Match at the very beginning of the data. */
5687 DEBUG_PRINT ("EXECUTING begbuf.\n");
5688 if (AT_STRINGS_BEG (d
))
5693 /* Match at the very end of the data. */
5695 DEBUG_PRINT ("EXECUTING endbuf.\n");
5696 if (AT_STRINGS_END (d
))
5701 /* on_failure_keep_string_jump is used to optimize `.*\n'. It
5702 pushes NULL as the value for the string on the stack. Then
5703 `POP_FAILURE_POINT' will keep the current value for the
5704 string, instead of restoring it. To see why, consider
5705 matching `foo\nbar' against `.*\n'. The .* matches the foo;
5706 then the . fails against the \n. But the next thing we want
5707 to do is match the \n against the \n; if we restored the
5708 string value, we would be back at the foo.
5710 Because this is used only in specific cases, we don't need to
5711 check all the things that `on_failure_jump' does, to make
5712 sure the right things get saved on the stack. Hence we don't
5713 share its code. The only reason to push anything on the
5714 stack at all is that otherwise we would have to change
5715 `anychar's code to do something besides goto fail in this
5716 case; that seems worse than this. */
5717 case on_failure_keep_string_jump
:
5718 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5719 DEBUG_PRINT ("EXECUTING on_failure_keep_string_jump %d (to %p):\n",
5722 PUSH_FAILURE_POINT (p
- 3, NULL
);
5725 /* A nasty loop is introduced by the non-greedy *? and +?.
5726 With such loops, the stack only ever contains one failure point
5727 at a time, so that a plain on_failure_jump_loop kind of
5728 cycle detection cannot work. Worse yet, such a detection
5729 can not only fail to detect a cycle, but it can also wrongly
5730 detect a cycle (between different instantiations of the same
5732 So the method used for those nasty loops is a little different:
5733 We use a special cycle-detection-stack-frame which is pushed
5734 when the on_failure_jump_nastyloop failure-point is *popped*.
5735 This special frame thus marks the beginning of one iteration
5736 through the loop and we can hence easily check right here
5737 whether something matched between the beginning and the end of
5739 case on_failure_jump_nastyloop
:
5740 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5741 DEBUG_PRINT ("EXECUTING on_failure_jump_nastyloop %d (to %p):\n",
5744 assert ((re_opcode_t
)p
[-4] == no_op
);
5747 CHECK_INFINITE_LOOP (p
- 4, d
);
5749 /* If there's a cycle, just continue without pushing
5750 this failure point. The failure point is the "try again"
5751 option, which shouldn't be tried.
5752 We want (x?)*?y\1z to match both xxyz and xxyxz. */
5753 PUSH_FAILURE_POINT (p
- 3, d
);
5757 /* Simple loop detecting on_failure_jump: just check on the
5758 failure stack if the same spot was already hit earlier. */
5759 case on_failure_jump_loop
:
5761 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5762 DEBUG_PRINT ("EXECUTING on_failure_jump_loop %d (to %p):\n",
5766 CHECK_INFINITE_LOOP (p
- 3, d
);
5768 /* If there's a cycle, get out of the loop, as if the matching
5769 had failed. We used to just `goto fail' here, but that was
5770 aborting the search a bit too early: we want to keep the
5771 empty-loop-match and keep matching after the loop.
5772 We want (x?)*y\1z to match both xxyz and xxyxz. */
5775 PUSH_FAILURE_POINT (p
- 3, d
);
5780 /* Uses of on_failure_jump:
5782 Each alternative starts with an on_failure_jump that points
5783 to the beginning of the next alternative. Each alternative
5784 except the last ends with a jump that in effect jumps past
5785 the rest of the alternatives. (They really jump to the
5786 ending jump of the following alternative, because tensioning
5787 these jumps is a hassle.)
5789 Repeats start with an on_failure_jump that points past both
5790 the repetition text and either the following jump or
5791 pop_failure_jump back to this on_failure_jump. */
5792 case on_failure_jump
:
5793 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5794 DEBUG_PRINT ("EXECUTING on_failure_jump %d (to %p):\n",
5797 PUSH_FAILURE_POINT (p
-3, d
);
5800 /* This operation is used for greedy *.
5801 Compare the beginning of the repeat with what in the
5802 pattern follows its end. If we can establish that there
5803 is nothing that they would both match, i.e., that we
5804 would have to backtrack because of (as in, e.g., `a*a')
5805 then we can use a non-backtracking loop based on
5806 on_failure_keep_string_jump instead of on_failure_jump. */
5807 case on_failure_jump_smart
:
5808 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5809 DEBUG_PRINT ("EXECUTING on_failure_jump_smart %d (to %p).\n",
5812 re_char
*p1
= p
; /* Next operation. */
5813 /* Here, we discard `const', making re_match non-reentrant. */
5814 unsigned char *p2
= (unsigned char*) p
+ mcnt
; /* Jump dest. */
5815 unsigned char *p3
= (unsigned char*) p
- 3; /* opcode location. */
5817 p
-= 3; /* Reset so that we will re-execute the
5818 instruction once it's been changed. */
5820 EXTRACT_NUMBER (mcnt
, p2
- 2);
5822 /* Ensure this is a indeed the trivial kind of loop
5823 we are expecting. */
5824 assert (skip_one_char (p1
) == p2
- 3);
5825 assert ((re_opcode_t
) p2
[-3] == jump
&& p2
+ mcnt
== p
);
5826 DEBUG_STATEMENT (debug
+= 2);
5827 if (mutually_exclusive_p (bufp
, p1
, p2
))
5829 /* Use a fast `on_failure_keep_string_jump' loop. */
5830 DEBUG_PRINT (" smart exclusive => fast loop.\n");
5831 *p3
= (unsigned char) on_failure_keep_string_jump
;
5832 STORE_NUMBER (p2
- 2, mcnt
+ 3);
5836 /* Default to a safe `on_failure_jump' loop. */
5837 DEBUG_PRINT (" smart default => slow loop.\n");
5838 *p3
= (unsigned char) on_failure_jump
;
5840 DEBUG_STATEMENT (debug
-= 2);
5844 /* Unconditionally jump (without popping any failure points). */
5847 IMMEDIATE_QUIT_CHECK
;
5848 EXTRACT_NUMBER_AND_INCR (mcnt
, p
); /* Get the amount to jump. */
5849 DEBUG_PRINT ("EXECUTING jump %d ", mcnt
);
5850 p
+= mcnt
; /* Do the jump. */
5851 DEBUG_PRINT ("(to %p).\n", p
);
5855 /* Have to succeed matching what follows at least n times.
5856 After that, handle like `on_failure_jump'. */
5858 /* Signedness doesn't matter since we only compare MCNT to 0. */
5859 EXTRACT_NUMBER (mcnt
, p
+ 2);
5860 DEBUG_PRINT ("EXECUTING succeed_n %d.\n", mcnt
);
5862 /* Originally, mcnt is how many times we HAVE to succeed. */
5865 /* Here, we discard `const', making re_match non-reentrant. */
5866 unsigned char *p2
= (unsigned char*) p
+ 2; /* counter loc. */
5869 PUSH_NUMBER (p2
, mcnt
);
5872 /* The two bytes encoding mcnt == 0 are two no_op opcodes. */
5877 /* Signedness doesn't matter since we only compare MCNT to 0. */
5878 EXTRACT_NUMBER (mcnt
, p
+ 2);
5879 DEBUG_PRINT ("EXECUTING jump_n %d.\n", mcnt
);
5881 /* Originally, this is how many times we CAN jump. */
5884 /* Here, we discard `const', making re_match non-reentrant. */
5885 unsigned char *p2
= (unsigned char*) p
+ 2; /* counter loc. */
5887 PUSH_NUMBER (p2
, mcnt
);
5888 goto unconditional_jump
;
5890 /* If don't have to jump any more, skip over the rest of command. */
5897 unsigned char *p2
; /* Location of the counter. */
5898 DEBUG_PRINT ("EXECUTING set_number_at.\n");
5900 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5901 /* Here, we discard `const', making re_match non-reentrant. */
5902 p2
= (unsigned char*) p
+ mcnt
;
5903 /* Signedness doesn't matter since we only copy MCNT's bits. */
5904 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5905 DEBUG_PRINT (" Setting %p to %d.\n", p2
, mcnt
);
5906 PUSH_NUMBER (p2
, mcnt
);
5913 boolean
not = (re_opcode_t
) *(p
- 1) == notwordbound
;
5914 DEBUG_PRINT ("EXECUTING %swordbound.\n", not ? "not" : "");
5916 /* We SUCCEED (or FAIL) in one of the following cases: */
5918 /* Case 1: D is at the beginning or the end of string. */
5919 if (AT_STRINGS_BEG (d
) || AT_STRINGS_END (d
))
5923 /* C1 is the character before D, S1 is the syntax of C1, C2
5924 is the character at D, and S2 is the syntax of C2. */
5929 ssize_t offset
= PTR_TO_OFFSET (d
- 1);
5930 ssize_t charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
5931 UPDATE_SYNTAX_TABLE (charpos
);
5933 GET_CHAR_BEFORE_2 (c1
, d
, string1
, end1
, string2
, end2
);
5936 UPDATE_SYNTAX_TABLE_FORWARD (charpos
+ 1);
5938 PREFETCH_NOLIMIT ();
5939 GET_CHAR_AFTER (c2
, d
, dummy
);
5942 if (/* Case 2: Only one of S1 and S2 is Sword. */
5943 ((s1
== Sword
) != (s2
== Sword
))
5944 /* Case 3: Both of S1 and S2 are Sword, and macro
5945 WORD_BOUNDARY_P (C1, C2) returns nonzero. */
5946 || ((s1
== Sword
) && WORD_BOUNDARY_P (c1
, c2
)))
5956 DEBUG_PRINT ("EXECUTING wordbeg.\n");
5958 /* We FAIL in one of the following cases: */
5960 /* Case 1: D is at the end of string. */
5961 if (AT_STRINGS_END (d
))
5965 /* C1 is the character before D, S1 is the syntax of C1, C2
5966 is the character at D, and S2 is the syntax of C2. */
5971 ssize_t offset
= PTR_TO_OFFSET (d
);
5972 ssize_t charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
5973 UPDATE_SYNTAX_TABLE (charpos
);
5976 GET_CHAR_AFTER (c2
, d
, dummy
);
5979 /* Case 2: S2 is not Sword. */
5983 /* Case 3: D is not at the beginning of string ... */
5984 if (!AT_STRINGS_BEG (d
))
5986 GET_CHAR_BEFORE_2 (c1
, d
, string1
, end1
, string2
, end2
);
5988 UPDATE_SYNTAX_TABLE_BACKWARD (charpos
- 1);
5992 /* ... and S1 is Sword, and WORD_BOUNDARY_P (C1, C2)
5994 if ((s1
== Sword
) && !WORD_BOUNDARY_P (c1
, c2
))
6001 DEBUG_PRINT ("EXECUTING wordend.\n");
6003 /* We FAIL in one of the following cases: */
6005 /* Case 1: D is at the beginning of string. */
6006 if (AT_STRINGS_BEG (d
))
6010 /* C1 is the character before D, S1 is the syntax of C1, C2
6011 is the character at D, and S2 is the syntax of C2. */
6016 ssize_t offset
= PTR_TO_OFFSET (d
) - 1;
6017 ssize_t charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
6018 UPDATE_SYNTAX_TABLE (charpos
);
6020 GET_CHAR_BEFORE_2 (c1
, d
, string1
, end1
, string2
, end2
);
6023 /* Case 2: S1 is not Sword. */
6027 /* Case 3: D is not at the end of string ... */
6028 if (!AT_STRINGS_END (d
))
6030 PREFETCH_NOLIMIT ();
6031 GET_CHAR_AFTER (c2
, d
, dummy
);
6033 UPDATE_SYNTAX_TABLE_FORWARD (charpos
);
6037 /* ... and S2 is Sword, and WORD_BOUNDARY_P (C1, C2)
6039 if ((s2
== Sword
) && !WORD_BOUNDARY_P (c1
, c2
))
6046 DEBUG_PRINT ("EXECUTING symbeg.\n");
6048 /* We FAIL in one of the following cases: */
6050 /* Case 1: D is at the end of string. */
6051 if (AT_STRINGS_END (d
))
6055 /* C1 is the character before D, S1 is the syntax of C1, C2
6056 is the character at D, and S2 is the syntax of C2. */
6060 ssize_t offset
= PTR_TO_OFFSET (d
);
6061 ssize_t charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
6062 UPDATE_SYNTAX_TABLE (charpos
);
6065 c2
= RE_STRING_CHAR (d
, target_multibyte
);
6068 /* Case 2: S2 is neither Sword nor Ssymbol. */
6069 if (s2
!= Sword
&& s2
!= Ssymbol
)
6072 /* Case 3: D is not at the beginning of string ... */
6073 if (!AT_STRINGS_BEG (d
))
6075 GET_CHAR_BEFORE_2 (c1
, d
, string1
, end1
, string2
, end2
);
6077 UPDATE_SYNTAX_TABLE_BACKWARD (charpos
- 1);
6081 /* ... and S1 is Sword or Ssymbol. */
6082 if (s1
== Sword
|| s1
== Ssymbol
)
6089 DEBUG_PRINT ("EXECUTING symend.\n");
6091 /* We FAIL in one of the following cases: */
6093 /* Case 1: D is at the beginning of string. */
6094 if (AT_STRINGS_BEG (d
))
6098 /* C1 is the character before D, S1 is the syntax of C1, C2
6099 is the character at D, and S2 is the syntax of C2. */
6103 ssize_t offset
= PTR_TO_OFFSET (d
) - 1;
6104 ssize_t charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
6105 UPDATE_SYNTAX_TABLE (charpos
);
6107 GET_CHAR_BEFORE_2 (c1
, d
, string1
, end1
, string2
, end2
);
6110 /* Case 2: S1 is neither Ssymbol nor Sword. */
6111 if (s1
!= Sword
&& s1
!= Ssymbol
)
6114 /* Case 3: D is not at the end of string ... */
6115 if (!AT_STRINGS_END (d
))
6117 PREFETCH_NOLIMIT ();
6118 c2
= RE_STRING_CHAR (d
, target_multibyte
);
6120 UPDATE_SYNTAX_TABLE_FORWARD (charpos
+ 1);
6124 /* ... and S2 is Sword or Ssymbol. */
6125 if (s2
== Sword
|| s2
== Ssymbol
)
6134 boolean
not = (re_opcode_t
) *(p
- 1) == notsyntaxspec
;
6136 DEBUG_PRINT ("EXECUTING %ssyntaxspec %d.\n", not ? "not" : "",
6141 ssize_t offset
= PTR_TO_OFFSET (d
);
6142 ssize_t pos1
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
6143 UPDATE_SYNTAX_TABLE (pos1
);
6150 GET_CHAR_AFTER (c
, d
, len
);
6151 if ((SYNTAX (c
) != (enum syntaxcode
) mcnt
) ^ not)
6160 DEBUG_PRINT ("EXECUTING before_dot.\n");
6161 if (PTR_BYTE_POS (d
) >= PT_BYTE
)
6166 DEBUG_PRINT ("EXECUTING at_dot.\n");
6167 if (PTR_BYTE_POS (d
) != PT_BYTE
)
6172 DEBUG_PRINT ("EXECUTING after_dot.\n");
6173 if (PTR_BYTE_POS (d
) <= PT_BYTE
)
6178 case notcategoryspec
:
6180 boolean
not = (re_opcode_t
) *(p
- 1) == notcategoryspec
;
6182 DEBUG_PRINT ("EXECUTING %scategoryspec %d.\n",
6183 not ? "not" : "", mcnt
);
6189 GET_CHAR_AFTER (c
, d
, len
);
6190 if ((!CHAR_HAS_CATEGORY (c
, mcnt
)) ^ not)
6202 continue; /* Successfully executed one pattern command; keep going. */
6205 /* We goto here if a matching operation fails. */
6207 IMMEDIATE_QUIT_CHECK
;
6208 if (!FAIL_STACK_EMPTY ())
6211 /* A restart point is known. Restore to that state. */
6212 DEBUG_PRINT ("\nFAIL:\n");
6213 POP_FAILURE_POINT (str
, pat
);
6216 case on_failure_keep_string_jump
:
6217 assert (str
== NULL
);
6218 goto continue_failure_jump
;
6220 case on_failure_jump_nastyloop
:
6221 assert ((re_opcode_t
)pat
[-2] == no_op
);
6222 PUSH_FAILURE_POINT (pat
- 2, str
);
6225 case on_failure_jump_loop
:
6226 case on_failure_jump
:
6229 continue_failure_jump
:
6230 EXTRACT_NUMBER_AND_INCR (mcnt
, pat
);
6235 /* A special frame used for nastyloops. */
6242 assert (p
>= bufp
->buffer
&& p
<= pend
);
6244 if (d
>= string1
&& d
<= end1
)
6248 break; /* Matching at this starting point really fails. */
6252 goto restore_best_regs
;
6256 return -1; /* Failure to match. */
6259 /* Subroutine definitions for re_match_2. */
6261 /* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
6262 bytes; nonzero otherwise. */
6265 bcmp_translate (const_re_char
*s1
, const_re_char
*s2
, register ssize_t len
,
6266 RE_TRANSLATE_TYPE translate
, const int target_multibyte
)
6268 register re_char
*p1
= s1
, *p2
= s2
;
6269 re_char
*p1_end
= s1
+ len
;
6270 re_char
*p2_end
= s2
+ len
;
6272 /* FIXME: Checking both p1 and p2 presumes that the two strings might have
6273 different lengths, but relying on a single `len' would break this. -sm */
6274 while (p1
< p1_end
&& p2
< p2_end
)
6276 int p1_charlen
, p2_charlen
;
6277 re_wchar_t p1_ch
, p2_ch
;
6279 GET_CHAR_AFTER (p1_ch
, p1
, p1_charlen
);
6280 GET_CHAR_AFTER (p2_ch
, p2
, p2_charlen
);
6282 if (RE_TRANSLATE (translate
, p1_ch
)
6283 != RE_TRANSLATE (translate
, p2_ch
))
6286 p1
+= p1_charlen
, p2
+= p2_charlen
;
6289 if (p1
!= p1_end
|| p2
!= p2_end
)
6295 /* Entry points for GNU code. */
6297 /* re_compile_pattern is the GNU regular expression compiler: it
6298 compiles PATTERN (of length SIZE) and puts the result in BUFP.
6299 Returns 0 if the pattern was valid, otherwise an error string.
6301 Assumes the `allocated' (and perhaps `buffer') and `translate' fields
6302 are set in BUFP on entry.
6304 We call regex_compile to do the actual compilation. */
6307 re_compile_pattern (const char *pattern
, size_t length
,
6308 struct re_pattern_buffer
*bufp
)
6312 /* GNU code is written to assume at least RE_NREGS registers will be set
6313 (and at least one extra will be -1). */
6314 bufp
->regs_allocated
= REGS_UNALLOCATED
;
6316 /* And GNU code determines whether or not to get register information
6317 by passing null for the REGS argument to re_match, etc., not by
6321 ret
= regex_compile ((re_char
*) pattern
, length
, re_syntax_options
, bufp
);
6325 return gettext (re_error_msgid
[(int) ret
]);
6327 WEAK_ALIAS (__re_compile_pattern
, re_compile_pattern
)
6329 /* Entry points compatible with 4.2 BSD regex library. We don't define
6330 them unless specifically requested. */
6332 #if defined _REGEX_RE_COMP || defined _LIBC
6334 /* BSD has one and only one pattern buffer. */
6335 static struct re_pattern_buffer re_comp_buf
;
6339 /* Make these definitions weak in libc, so POSIX programs can redefine
6340 these names if they don't use our functions, and still use
6341 regcomp/regexec below without link errors. */
6344 re_comp (const char *s
)
6350 if (!re_comp_buf
.buffer
)
6351 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6352 return (char *) gettext ("No previous regular expression");
6356 if (!re_comp_buf
.buffer
)
6358 re_comp_buf
.buffer
= malloc (200);
6359 if (re_comp_buf
.buffer
== NULL
)
6360 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6361 return (char *) gettext (re_error_msgid
[(int) REG_ESPACE
]);
6362 re_comp_buf
.allocated
= 200;
6364 re_comp_buf
.fastmap
= malloc (1 << BYTEWIDTH
);
6365 if (re_comp_buf
.fastmap
== NULL
)
6366 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6367 return (char *) gettext (re_error_msgid
[(int) REG_ESPACE
]);
6370 /* Since `re_exec' always passes NULL for the `regs' argument, we
6371 don't need to initialize the pattern buffer fields which affect it. */
6373 ret
= regex_compile (s
, strlen (s
), re_syntax_options
, &re_comp_buf
);
6378 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6379 return (char *) gettext (re_error_msgid
[(int) ret
]);
6387 re_exec (const char *s
)
6389 const size_t len
= strlen (s
);
6390 return re_search (&re_comp_buf
, s
, len
, 0, len
, 0) >= 0;
6392 #endif /* _REGEX_RE_COMP */
6394 /* POSIX.2 functions. Don't define these for Emacs. */
6398 /* regcomp takes a regular expression as a string and compiles it.
6400 PREG is a regex_t *. We do not expect any fields to be initialized,
6401 since POSIX says we shouldn't. Thus, we set
6403 `buffer' to the compiled pattern;
6404 `used' to the length of the compiled pattern;
6405 `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
6406 REG_EXTENDED bit in CFLAGS is set; otherwise, to
6407 RE_SYNTAX_POSIX_BASIC;
6408 `fastmap' to an allocated space for the fastmap;
6409 `fastmap_accurate' to zero;
6410 `re_nsub' to the number of subexpressions in PATTERN.
6412 PATTERN is the address of the pattern string.
6414 CFLAGS is a series of bits which affect compilation.
6416 If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
6417 use POSIX basic syntax.
6419 If REG_NEWLINE is set, then . and [^...] don't match newline.
6420 Also, regexec will try a match beginning after every newline.
6422 If REG_ICASE is set, then we considers upper- and lowercase
6423 versions of letters to be equivalent when matching.
6425 If REG_NOSUB is set, then when PREG is passed to regexec, that
6426 routine will report only success or failure, and nothing about the
6429 It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
6430 the return codes and their meanings.) */
6433 regcomp (regex_t
*_Restrict_ preg
, const char *_Restrict_ pattern
,
6438 = (cflags
& REG_EXTENDED
) ?
6439 RE_SYNTAX_POSIX_EXTENDED
: RE_SYNTAX_POSIX_BASIC
;
6441 /* regex_compile will allocate the space for the compiled pattern. */
6443 preg
->allocated
= 0;
6446 /* Try to allocate space for the fastmap. */
6447 preg
->fastmap
= malloc (1 << BYTEWIDTH
);
6449 if (cflags
& REG_ICASE
)
6453 preg
->translate
= malloc (CHAR_SET_SIZE
* sizeof *preg
->translate
);
6454 if (preg
->translate
== NULL
)
6455 return (int) REG_ESPACE
;
6457 /* Map uppercase characters to corresponding lowercase ones. */
6458 for (i
= 0; i
< CHAR_SET_SIZE
; i
++)
6459 preg
->translate
[i
] = ISUPPER (i
) ? TOLOWER (i
) : i
;
6462 preg
->translate
= NULL
;
6464 /* If REG_NEWLINE is set, newlines are treated differently. */
6465 if (cflags
& REG_NEWLINE
)
6466 { /* REG_NEWLINE implies neither . nor [^...] match newline. */
6467 syntax
&= ~RE_DOT_NEWLINE
;
6468 syntax
|= RE_HAT_LISTS_NOT_NEWLINE
;
6471 syntax
|= RE_NO_NEWLINE_ANCHOR
;
6473 preg
->no_sub
= !!(cflags
& REG_NOSUB
);
6475 /* POSIX says a null character in the pattern terminates it, so we
6476 can use strlen here in compiling the pattern. */
6477 ret
= regex_compile ((re_char
*) pattern
, strlen (pattern
), syntax
, preg
);
6479 /* POSIX doesn't distinguish between an unmatched open-group and an
6480 unmatched close-group: both are REG_EPAREN. */
6481 if (ret
== REG_ERPAREN
)
6484 if (ret
== REG_NOERROR
&& preg
->fastmap
)
6485 { /* Compute the fastmap now, since regexec cannot modify the pattern
6487 re_compile_fastmap (preg
);
6488 if (preg
->can_be_null
)
6489 { /* The fastmap can't be used anyway. */
6490 free (preg
->fastmap
);
6491 preg
->fastmap
= NULL
;
6496 WEAK_ALIAS (__regcomp
, regcomp
)
6499 /* regexec searches for a given pattern, specified by PREG, in the
6502 If NMATCH is zero or REG_NOSUB was set in the cflags argument to
6503 `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
6504 least NMATCH elements, and we set them to the offsets of the
6505 corresponding matched substrings.
6507 EFLAGS specifies `execution flags' which affect matching: if
6508 REG_NOTBOL is set, then ^ does not match at the beginning of the
6509 string; if REG_NOTEOL is set, then $ does not match at the end.
6511 We return 0 if we find a match and REG_NOMATCH if not. */
6514 regexec (const regex_t
*_Restrict_ preg
, const char *_Restrict_ string
,
6515 size_t nmatch
, regmatch_t pmatch
[_Restrict_arr_
], int eflags
)
6518 struct re_registers regs
;
6519 regex_t private_preg
;
6520 size_t len
= strlen (string
);
6521 boolean want_reg_info
= !preg
->no_sub
&& nmatch
> 0 && pmatch
;
6523 private_preg
= *preg
;
6525 private_preg
.not_bol
= !!(eflags
& REG_NOTBOL
);
6526 private_preg
.not_eol
= !!(eflags
& REG_NOTEOL
);
6528 /* The user has told us exactly how many registers to return
6529 information about, via `nmatch'. We have to pass that on to the
6530 matching routines. */
6531 private_preg
.regs_allocated
= REGS_FIXED
;
6535 regs
.num_regs
= nmatch
;
6536 regs
.start
= TALLOC (nmatch
* 2, regoff_t
);
6537 if (regs
.start
== NULL
)
6539 regs
.end
= regs
.start
+ nmatch
;
6542 /* Instead of using not_eol to implement REG_NOTEOL, we could simply
6543 pass (&private_preg, string, len + 1, 0, len, ...) pretending the string
6544 was a little bit longer but still only matching the real part.
6545 This works because the `endline' will check for a '\n' and will find a
6546 '\0', correctly deciding that this is not the end of a line.
6547 But it doesn't work out so nicely for REG_NOTBOL, since we don't have
6548 a convenient '\0' there. For all we know, the string could be preceded
6549 by '\n' which would throw things off. */
6551 /* Perform the searching operation. */
6552 ret
= re_search (&private_preg
, string
, len
,
6553 /* start: */ 0, /* range: */ len
,
6554 want_reg_info
? ®s
: 0);
6556 /* Copy the register information to the POSIX structure. */
6563 for (r
= 0; r
< nmatch
; r
++)
6565 pmatch
[r
].rm_so
= regs
.start
[r
];
6566 pmatch
[r
].rm_eo
= regs
.end
[r
];
6570 /* If we needed the temporary register info, free the space now. */
6574 /* We want zero return to mean success, unlike `re_search'. */
6575 return ret
>= 0 ? REG_NOERROR
: REG_NOMATCH
;
6577 WEAK_ALIAS (__regexec
, regexec
)
6580 /* Returns a message corresponding to an error code, ERR_CODE, returned
6581 from either regcomp or regexec. We don't use PREG here.
6583 ERR_CODE was previously called ERRCODE, but that name causes an
6584 error with msvc8 compiler. */
6587 regerror (int err_code
, const regex_t
*preg
, char *errbuf
, size_t errbuf_size
)
6593 || err_code
>= (sizeof (re_error_msgid
) / sizeof (re_error_msgid
[0])))
6594 /* Only error codes returned by the rest of the code should be passed
6595 to this routine. If we are given anything else, or if other regex
6596 code generates an invalid error code, then the program has a bug.
6597 Dump core so we can fix it. */
6600 msg
= gettext (re_error_msgid
[err_code
]);
6602 msg_size
= strlen (msg
) + 1; /* Includes the null. */
6604 if (errbuf_size
!= 0)
6606 if (msg_size
> errbuf_size
)
6608 memcpy (errbuf
, msg
, errbuf_size
- 1);
6609 errbuf
[errbuf_size
- 1] = 0;
6612 strcpy (errbuf
, msg
);
6617 WEAK_ALIAS (__regerror
, regerror
)
6620 /* Free dynamically allocated space used by PREG. */
6623 regfree (regex_t
*preg
)
6625 free (preg
->buffer
);
6626 preg
->buffer
= NULL
;
6628 preg
->allocated
= 0;
6631 free (preg
->fastmap
);
6632 preg
->fastmap
= NULL
;
6633 preg
->fastmap_accurate
= 0;
6635 free (preg
->translate
);
6636 preg
->translate
= NULL
;
6638 WEAK_ALIAS (__regfree
, regfree
)
6640 #endif /* not emacs */