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-2016 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 /* Strings are 0-indexed, buffers are 1-indexed; we pun on the boolean
156 result to get the right base index. */
157 # define POS_AS_IN_BUFFER(p) ((p) + (NILP (re_match_object) || BUFFERP (re_match_object)))
159 # define RE_MULTIBYTE_P(bufp) ((bufp)->multibyte)
160 # define RE_TARGET_MULTIBYTE_P(bufp) ((bufp)->target_multibyte)
161 # define RE_STRING_CHAR(p, multibyte) \
162 (multibyte ? (STRING_CHAR (p)) : (*(p)))
163 # define RE_STRING_CHAR_AND_LENGTH(p, len, multibyte) \
164 (multibyte ? (STRING_CHAR_AND_LENGTH (p, len)) : ((len) = 1, *(p)))
166 # define RE_CHAR_TO_MULTIBYTE(c) UNIBYTE_TO_CHAR (c)
168 # define RE_CHAR_TO_UNIBYTE(c) CHAR_TO_BYTE_SAFE (c)
170 /* Set C a (possibly converted to multibyte) character before P. P
171 points into a string which is the virtual concatenation of STR1
172 (which ends at END1) or STR2 (which ends at END2). */
173 # define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2) \
175 if (target_multibyte) \
177 re_char *dtemp = (p) == (str2) ? (end1) : (p); \
178 re_char *dlimit = ((p) > (str2) && (p) <= (end2)) ? (str2) : (str1); \
179 while (dtemp-- > dlimit && !CHAR_HEAD_P (*dtemp)); \
180 c = STRING_CHAR (dtemp); \
184 (c = ((p) == (str2) ? (end1) : (p))[-1]); \
185 (c) = RE_CHAR_TO_MULTIBYTE (c); \
189 /* Set C a (possibly converted to multibyte) character at P, and set
190 LEN to the byte length of that character. */
191 # define GET_CHAR_AFTER(c, p, len) \
193 if (target_multibyte) \
194 (c) = STRING_CHAR_AND_LENGTH (p, len); \
199 (c) = RE_CHAR_TO_MULTIBYTE (c); \
203 #else /* not emacs */
205 /* If we are not linking with Emacs proper,
206 we can't use the relocating allocator
207 even if config.h says that we can. */
212 /* When used in Emacs's lib-src, we need xmalloc and xrealloc. */
215 xmalloc (size_t size
)
217 void *val
= malloc (size
);
220 write (2, "virtual memory exhausted\n", 25);
227 xrealloc (void *block
, size_t size
)
230 /* We must call malloc explicitly when BLOCK is 0, since some
231 reallocs don't do this. */
235 val
= realloc (block
, size
);
238 write (2, "virtual memory exhausted\n", 25);
247 # define malloc xmalloc
251 # define realloc xrealloc
253 # include <stdbool.h>
256 /* Define the syntax stuff for \<, \>, etc. */
258 /* Sword must be nonzero for the wordchar pattern commands in re_match_2. */
259 enum syntaxcode
{ Swhitespace
= 0, Sword
= 1, Ssymbol
= 2 };
261 /* Dummy macros for non-Emacs environments. */
262 # define MAX_MULTIBYTE_LENGTH 1
263 # define RE_MULTIBYTE_P(x) 0
264 # define RE_TARGET_MULTIBYTE_P(x) 0
265 # define WORD_BOUNDARY_P(c1, c2) (0)
266 # define BYTES_BY_CHAR_HEAD(p) (1)
267 # define PREV_CHAR_BOUNDARY(p, limit) ((p)--)
268 # define STRING_CHAR(p) (*(p))
269 # define RE_STRING_CHAR(p, multibyte) STRING_CHAR (p)
270 # define CHAR_STRING(c, s) (*(s) = (c), 1)
271 # define STRING_CHAR_AND_LENGTH(p, actual_len) ((actual_len) = 1, *(p))
272 # define RE_STRING_CHAR_AND_LENGTH(p, len, multibyte) STRING_CHAR_AND_LENGTH (p, len)
273 # define RE_CHAR_TO_MULTIBYTE(c) (c)
274 # define RE_CHAR_TO_UNIBYTE(c) (c)
275 # define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2) \
276 (c = ((p) == (str2) ? *((end1) - 1) : *((p) - 1)))
277 # define GET_CHAR_AFTER(c, p, len) \
279 # define CHAR_BYTE8_P(c) (0)
280 # define CHAR_LEADING_CODE(c) (c)
282 #endif /* not emacs */
285 # define RE_TRANSLATE(TBL, C) ((unsigned char)(TBL)[C])
286 # define RE_TRANSLATE_P(TBL) (TBL)
289 /* Get the interface, including the syntax bits. */
292 /* isalpha etc. are used for the character classes. */
297 /* 1 if C is an ASCII character. */
298 # define IS_REAL_ASCII(c) ((c) < 0200)
300 /* 1 if C is a unibyte character. */
301 # define ISUNIBYTE(c) (SINGLE_BYTE_CHAR_P ((c)))
303 /* The Emacs definitions should not be directly affected by locales. */
305 /* In Emacs, these are only used for single-byte characters. */
306 # define ISDIGIT(c) ((c) >= '0' && (c) <= '9')
307 # define ISCNTRL(c) ((c) < ' ')
308 # define ISXDIGIT(c) (((c) >= '0' && (c) <= '9') \
309 || ((c) >= 'a' && (c) <= 'f') \
310 || ((c) >= 'A' && (c) <= 'F'))
312 /* This is only used for single-byte characters. */
313 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
315 /* The rest must handle multibyte characters. */
317 # define ISGRAPH(c) (SINGLE_BYTE_CHAR_P (c) \
318 ? (c) > ' ' && !((c) >= 0177 && (c) <= 0240) \
321 # define ISPRINT(c) (SINGLE_BYTE_CHAR_P (c) \
322 ? (c) >= ' ' && !((c) >= 0177 && (c) <= 0237) \
325 # define ISALNUM(c) (IS_REAL_ASCII (c) \
326 ? (((c) >= 'a' && (c) <= 'z') \
327 || ((c) >= 'A' && (c) <= 'Z') \
328 || ((c) >= '0' && (c) <= '9')) \
329 : (alphabeticp (c) || decimalnump (c)))
331 # define ISALPHA(c) (IS_REAL_ASCII (c) \
332 ? (((c) >= 'a' && (c) <= 'z') \
333 || ((c) >= 'A' && (c) <= 'Z')) \
336 # define ISLOWER(c) lowercasep (c)
338 # define ISPUNCT(c) (IS_REAL_ASCII (c) \
339 ? ((c) > ' ' && (c) < 0177 \
340 && !(((c) >= 'a' && (c) <= 'z') \
341 || ((c) >= 'A' && (c) <= 'Z') \
342 || ((c) >= '0' && (c) <= '9'))) \
343 : SYNTAX (c) != Sword)
345 # define ISSPACE(c) (SYNTAX (c) == Swhitespace)
347 # define ISUPPER(c) uppercasep (c)
349 # define ISWORD(c) (SYNTAX (c) == Sword)
351 #else /* not emacs */
353 /* 1 if C is an ASCII character. */
354 # define IS_REAL_ASCII(c) ((c) < 0200)
356 /* This distinction is not meaningful, except in Emacs. */
357 # define ISUNIBYTE(c) 1
360 # define ISBLANK(c) isblank (c)
362 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
365 # define ISGRAPH(c) isgraph (c)
367 # define ISGRAPH(c) (isprint (c) && !isspace (c))
370 /* Solaris defines ISPRINT so we must undefine it first. */
372 # define ISPRINT(c) isprint (c)
373 # define ISDIGIT(c) isdigit (c)
374 # define ISALNUM(c) isalnum (c)
375 # define ISALPHA(c) isalpha (c)
376 # define ISCNTRL(c) iscntrl (c)
377 # define ISLOWER(c) islower (c)
378 # define ISPUNCT(c) ispunct (c)
379 # define ISSPACE(c) isspace (c)
380 # define ISUPPER(c) isupper (c)
381 # define ISXDIGIT(c) isxdigit (c)
383 # define ISWORD(c) ISALPHA (c)
386 # define TOLOWER(c) _tolower (c)
388 # define TOLOWER(c) tolower (c)
391 /* How many characters in the character set. */
392 # define CHAR_SET_SIZE 256
396 extern char *re_syntax_table
;
398 # else /* not SYNTAX_TABLE */
400 static char re_syntax_table
[CHAR_SET_SIZE
];
403 init_syntax_once (void)
411 memset (re_syntax_table
, 0, sizeof re_syntax_table
);
413 for (c
= 0; c
< CHAR_SET_SIZE
; ++c
)
415 re_syntax_table
[c
] = Sword
;
417 re_syntax_table
['_'] = Ssymbol
;
422 # endif /* not SYNTAX_TABLE */
424 # define SYNTAX(c) re_syntax_table[(c)]
426 #endif /* not emacs */
428 #define SIGN_EXTEND_CHAR(c) ((signed char) (c))
430 /* Should we use malloc or alloca? If REGEX_MALLOC is not defined, we
431 use `alloca' instead of `malloc'. This is because using malloc in
432 re_search* or re_match* could cause memory leaks when C-g is used in
433 Emacs; also, malloc is slower and causes storage fragmentation. On
434 the other hand, malloc is more portable, and easier to debug.
436 Because we sometimes use alloca, some routines have to be macros,
437 not functions -- `alloca'-allocated space disappears at the end of the
438 function it is called in. */
442 # define REGEX_ALLOCATE malloc
443 # define REGEX_REALLOCATE(source, osize, nsize) realloc (source, nsize)
444 # define REGEX_FREE free
446 #else /* not REGEX_MALLOC */
448 /* Emacs already defines alloca, sometimes. */
451 /* Make alloca work the best possible way. */
453 # define alloca __builtin_alloca
454 # else /* not __GNUC__ */
455 # ifdef HAVE_ALLOCA_H
457 # endif /* HAVE_ALLOCA_H */
458 # endif /* not __GNUC__ */
460 # endif /* not alloca */
463 # define REGEX_USE_SAFE_ALLOCA USE_SAFE_ALLOCA
464 # define REGEX_SAFE_FREE() SAFE_FREE ()
465 # define REGEX_ALLOCATE SAFE_ALLOCA
467 # define REGEX_ALLOCATE alloca
470 /* Assumes a `char *destination' variable. */
471 # define REGEX_REALLOCATE(source, osize, nsize) \
472 (destination = REGEX_ALLOCATE (nsize), \
473 memcpy (destination, source, osize))
475 /* No need to do anything to free, after alloca. */
476 # define REGEX_FREE(arg) ((void)0) /* Do nothing! But inhibit gcc warning. */
478 #endif /* not REGEX_MALLOC */
480 #ifndef REGEX_USE_SAFE_ALLOCA
481 # define REGEX_USE_SAFE_ALLOCA ((void) 0)
482 # define REGEX_SAFE_FREE() ((void) 0)
485 /* Define how to allocate the failure stack. */
487 #if defined REL_ALLOC && defined REGEX_MALLOC
489 # define REGEX_ALLOCATE_STACK(size) \
490 r_alloc (&failure_stack_ptr, (size))
491 # define REGEX_REALLOCATE_STACK(source, osize, nsize) \
492 r_re_alloc (&failure_stack_ptr, (nsize))
493 # define REGEX_FREE_STACK(ptr) \
494 r_alloc_free (&failure_stack_ptr)
496 #else /* not using relocating allocator */
498 # define REGEX_ALLOCATE_STACK(size) REGEX_ALLOCATE (size)
499 # define REGEX_REALLOCATE_STACK(source, o, n) REGEX_REALLOCATE (source, o, n)
500 # define REGEX_FREE_STACK(ptr) REGEX_FREE (ptr)
502 #endif /* not using relocating allocator */
505 /* True if `size1' is non-NULL and PTR is pointing anywhere inside
506 `string1' or just past its end. This works if PTR is NULL, which is
508 #define FIRST_STRING_P(ptr) \
509 (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
511 /* (Re)Allocate N items of type T using malloc, or fail. */
512 #define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t)))
513 #define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
514 #define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
516 #define BYTEWIDTH 8 /* In bits. */
518 #define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
523 # define max(a, b) ((a) > (b) ? (a) : (b))
524 # define min(a, b) ((a) < (b) ? (a) : (b))
527 /* Type of source-pattern and string chars. */
529 typedef unsigned char re_char
;
530 typedef const re_char const_re_char
;
532 typedef const unsigned char re_char
;
533 typedef re_char const_re_char
;
536 typedef char boolean
;
538 static regoff_t
re_match_2_internal (struct re_pattern_buffer
*bufp
,
539 re_char
*string1
, size_t size1
,
540 re_char
*string2
, size_t size2
,
542 struct re_registers
*regs
,
545 /* These are the command codes that appear in compiled regular
546 expressions. Some opcodes are followed by argument bytes. A
547 command code can specify any interpretation whatsoever for its
548 arguments. Zero bytes may appear in the compiled regular expression. */
554 /* Succeed right away--no more backtracking. */
557 /* Followed by one byte giving n, then by n literal bytes. */
560 /* Matches any (more or less) character. */
563 /* Matches any one char belonging to specified set. First
564 following byte is number of bitmap bytes. Then come bytes
565 for a bitmap saying which chars are in. Bits in each byte
566 are ordered low-bit-first. A character is in the set if its
567 bit is 1. A character too large to have a bit in the map is
568 automatically not in the set.
570 If the length byte has the 0x80 bit set, then that stuff
571 is followed by a range table:
572 2 bytes of flags for character sets (low 8 bits, high 8 bits)
573 See RANGE_TABLE_WORK_BITS below.
574 2 bytes, the number of pairs that follow (upto 32767)
575 pairs, each 2 multibyte characters,
576 each multibyte character represented as 3 bytes. */
579 /* Same parameters as charset, but match any character that is
580 not one of those specified. */
583 /* Start remembering the text that is matched, for storing in a
584 register. Followed by one byte with the register number, in
585 the range 0 to one less than the pattern buffer's re_nsub
589 /* Stop remembering the text that is matched and store it in a
590 memory register. Followed by one byte with the register
591 number, in the range 0 to one less than `re_nsub' in the
595 /* Match a duplicate of something remembered. Followed by one
596 byte containing the register number. */
599 /* Fail unless at beginning of line. */
602 /* Fail unless at end of line. */
605 /* Succeeds if at beginning of buffer (if emacs) or at beginning
606 of string to be matched (if not). */
609 /* Analogously, for end of buffer/string. */
612 /* Followed by two byte relative address to which to jump. */
615 /* Followed by two-byte relative address of place to resume at
616 in case of failure. */
619 /* Like on_failure_jump, but pushes a placeholder instead of the
620 current string position when executed. */
621 on_failure_keep_string_jump
,
623 /* Just like `on_failure_jump', except that it checks that we
624 don't get stuck in an infinite loop (matching an empty string
626 on_failure_jump_loop
,
628 /* Just like `on_failure_jump_loop', except that it checks for
629 a different kind of loop (the kind that shows up with non-greedy
630 operators). This operation has to be immediately preceded
632 on_failure_jump_nastyloop
,
634 /* A smart `on_failure_jump' used for greedy * and + operators.
635 It analyzes the loop before which it is put and if the
636 loop does not require backtracking, it changes itself to
637 `on_failure_keep_string_jump' and short-circuits the loop,
638 else it just defaults to changing itself into `on_failure_jump'.
639 It assumes that it is pointing to just past a `jump'. */
640 on_failure_jump_smart
,
642 /* Followed by two-byte relative address and two-byte number n.
643 After matching N times, jump to the address upon failure.
644 Does not work if N starts at 0: use on_failure_jump_loop
648 /* Followed by two-byte relative address, and two-byte number n.
649 Jump to the address N times, then fail. */
652 /* Set the following two-byte relative address to the
653 subsequent two-byte number. The address *includes* the two
657 wordbeg
, /* Succeeds if at word beginning. */
658 wordend
, /* Succeeds if at word end. */
660 wordbound
, /* Succeeds if at a word boundary. */
661 notwordbound
, /* Succeeds if not at a word boundary. */
663 symbeg
, /* Succeeds if at symbol beginning. */
664 symend
, /* Succeeds if at symbol end. */
666 /* Matches any character whose syntax is specified. Followed by
667 a byte which contains a syntax code, e.g., Sword. */
670 /* Matches any character whose syntax is not that specified. */
674 ,before_dot
, /* Succeeds if before point. */
675 at_dot
, /* Succeeds if at point. */
676 after_dot
, /* Succeeds if after point. */
678 /* Matches any character whose category-set contains the specified
679 category. The operator is followed by a byte which contains a
680 category code (mnemonic ASCII character). */
683 /* Matches any character whose category-set does not contain the
684 specified category. The operator is followed by a byte which
685 contains the category code (mnemonic ASCII character). */
690 /* Common operations on the compiled pattern. */
692 /* Store NUMBER in two contiguous bytes starting at DESTINATION. */
694 #define STORE_NUMBER(destination, number) \
696 (destination)[0] = (number) & 0377; \
697 (destination)[1] = (number) >> 8; \
700 /* Same as STORE_NUMBER, except increment DESTINATION to
701 the byte after where the number is stored. Therefore, DESTINATION
702 must be an lvalue. */
704 #define STORE_NUMBER_AND_INCR(destination, number) \
706 STORE_NUMBER (destination, number); \
707 (destination) += 2; \
710 /* Put into DESTINATION a number stored in two contiguous bytes starting
713 #define EXTRACT_NUMBER(destination, source) \
714 ((destination) = extract_number (source))
717 extract_number (re_char
*source
)
719 unsigned leading_byte
= SIGN_EXTEND_CHAR (source
[1]);
720 return (leading_byte
<< 8) + source
[0];
723 /* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
724 SOURCE must be an lvalue. */
726 #define EXTRACT_NUMBER_AND_INCR(destination, source) \
727 ((destination) = extract_number_and_incr (&source))
730 extract_number_and_incr (re_char
**source
)
732 int num
= extract_number (*source
);
737 /* Store a multibyte character in three contiguous bytes starting
738 DESTINATION, and increment DESTINATION to the byte after where the
739 character is stored. Therefore, DESTINATION must be an lvalue. */
741 #define STORE_CHARACTER_AND_INCR(destination, character) \
743 (destination)[0] = (character) & 0377; \
744 (destination)[1] = ((character) >> 8) & 0377; \
745 (destination)[2] = (character) >> 16; \
746 (destination) += 3; \
749 /* Put into DESTINATION a character stored in three contiguous bytes
750 starting at SOURCE. */
752 #define EXTRACT_CHARACTER(destination, source) \
754 (destination) = ((source)[0] \
755 | ((source)[1] << 8) \
756 | ((source)[2] << 16)); \
760 /* Macros for charset. */
762 /* Size of bitmap of charset P in bytes. P is a start of charset,
763 i.e. *P is (re_opcode_t) charset or (re_opcode_t) charset_not. */
764 #define CHARSET_BITMAP_SIZE(p) ((p)[1] & 0x7F)
766 /* Nonzero if charset P has range table. */
767 #define CHARSET_RANGE_TABLE_EXISTS_P(p) ((p)[1] & 0x80)
769 /* Return the address of range table of charset P. But not the start
770 of table itself, but the before where the number of ranges is
771 stored. `2 +' means to skip re_opcode_t and size of bitmap,
772 and the 2 bytes of flags at the start of the range table. */
773 #define CHARSET_RANGE_TABLE(p) (&(p)[4 + CHARSET_BITMAP_SIZE (p)])
776 /* Extract the bit flags that start a range table. */
777 #define CHARSET_RANGE_TABLE_BITS(p) \
778 ((p)[2 + CHARSET_BITMAP_SIZE (p)] \
779 + (p)[3 + CHARSET_BITMAP_SIZE (p)] * 0x100)
782 /* Return the address of end of RANGE_TABLE. COUNT is number of
783 ranges (which is a pair of (start, end)) in the RANGE_TABLE. `* 2'
784 is start of range and end of range. `* 3' is size of each start
786 #define CHARSET_RANGE_TABLE_END(range_table, count) \
787 ((range_table) + (count) * 2 * 3)
789 /* Test if C is in RANGE_TABLE. A flag NOT is negated if C is in.
790 COUNT is number of ranges in RANGE_TABLE. */
791 #define CHARSET_LOOKUP_RANGE_TABLE_RAW(not, c, range_table, count) \
794 re_wchar_t range_start, range_end; \
796 re_char *range_table_end \
797 = CHARSET_RANGE_TABLE_END ((range_table), (count)); \
799 for (rtp = (range_table); rtp < range_table_end; rtp += 2 * 3) \
801 EXTRACT_CHARACTER (range_start, rtp); \
802 EXTRACT_CHARACTER (range_end, rtp + 3); \
804 if (range_start <= (c) && (c) <= range_end) \
813 /* Test if C is in range table of CHARSET. The flag NOT is negated if
814 C is listed in it. */
815 #define CHARSET_LOOKUP_RANGE_TABLE(not, c, charset) \
818 /* Number of ranges in range table. */ \
820 re_char *range_table = CHARSET_RANGE_TABLE (charset); \
822 EXTRACT_NUMBER_AND_INCR (count, range_table); \
823 CHARSET_LOOKUP_RANGE_TABLE_RAW ((not), (c), range_table, count); \
827 /* If DEBUG is defined, Regex prints many voluminous messages about what
828 it is doing (if the variable `debug' is nonzero). If linked with the
829 main program in `iregex.c', you can enter patterns and strings
830 interactively. And if linked with the main program in `main.c' and
831 the other test files, you can run the already-written tests. */
835 /* We use standard I/O for debugging. */
838 /* It is useful to test things that ``must'' be true when debugging. */
841 static int debug
= -100000;
843 # define DEBUG_STATEMENT(e) e
844 # define DEBUG_PRINT(...) if (debug > 0) printf (__VA_ARGS__)
845 # define DEBUG_COMPILES_ARGUMENTS
846 # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) \
847 if (debug > 0) print_partial_compiled_pattern (s, e)
848 # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \
849 if (debug > 0) print_double_string (w, s1, sz1, s2, sz2)
852 /* Print the fastmap in human-readable form. */
855 print_fastmap (char *fastmap
)
857 unsigned was_a_range
= 0;
860 while (i
< (1 << BYTEWIDTH
))
866 while (i
< (1 << BYTEWIDTH
) && fastmap
[i
])
882 /* Print a compiled pattern string in human-readable form, starting at
883 the START pointer into it and ending just before the pointer END. */
886 print_partial_compiled_pattern (re_char
*start
, re_char
*end
)
894 fprintf (stderr
, "(null)\n");
898 /* Loop over pattern commands. */
901 fprintf (stderr
, "%td:\t", p
- start
);
903 switch ((re_opcode_t
) *p
++)
906 fprintf (stderr
, "/no_op");
910 fprintf (stderr
, "/succeed");
915 fprintf (stderr
, "/exactn/%d", mcnt
);
918 fprintf (stderr
, "/%c", *p
++);
924 fprintf (stderr
, "/start_memory/%d", *p
++);
928 fprintf (stderr
, "/stop_memory/%d", *p
++);
932 fprintf (stderr
, "/duplicate/%d", *p
++);
936 fprintf (stderr
, "/anychar");
942 register int c
, last
= -100;
943 register int in_range
= 0;
944 int length
= CHARSET_BITMAP_SIZE (p
- 1);
945 int has_range_table
= CHARSET_RANGE_TABLE_EXISTS_P (p
- 1);
947 fprintf (stderr
, "/charset [%s",
948 (re_opcode_t
) *(p
- 1) == charset_not
? "^" : "");
951 fprintf (stderr
, " !extends past end of pattern! ");
953 for (c
= 0; c
< 256; c
++)
955 && (p
[1 + (c
/8)] & (1 << (c
% 8))))
957 /* Are we starting a range? */
958 if (last
+ 1 == c
&& ! in_range
)
960 fprintf (stderr
, "-");
963 /* Have we broken a range? */
964 else if (last
+ 1 != c
&& in_range
)
966 fprintf (stderr
, "%c", last
);
971 fprintf (stderr
, "%c", c
);
977 fprintf (stderr
, "%c", last
);
979 fprintf (stderr
, "]");
986 fprintf (stderr
, "has-range-table");
988 /* ??? Should print the range table; for now, just skip it. */
989 p
+= 2; /* skip range table bits */
990 EXTRACT_NUMBER_AND_INCR (count
, p
);
991 p
= CHARSET_RANGE_TABLE_END (p
, count
);
997 fprintf (stderr
, "/begline");
1001 fprintf (stderr
, "/endline");
1004 case on_failure_jump
:
1005 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
1006 fprintf (stderr
, "/on_failure_jump to %td", p
+ mcnt
- start
);
1009 case on_failure_keep_string_jump
:
1010 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
1011 fprintf (stderr
, "/on_failure_keep_string_jump to %td",
1015 case on_failure_jump_nastyloop
:
1016 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
1017 fprintf (stderr
, "/on_failure_jump_nastyloop to %td",
1021 case on_failure_jump_loop
:
1022 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
1023 fprintf (stderr
, "/on_failure_jump_loop to %td",
1027 case on_failure_jump_smart
:
1028 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
1029 fprintf (stderr
, "/on_failure_jump_smart to %td",
1034 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
1035 fprintf (stderr
, "/jump to %td", p
+ mcnt
- start
);
1039 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
1040 EXTRACT_NUMBER_AND_INCR (mcnt2
, p
);
1041 fprintf (stderr
, "/succeed_n to %td, %d times",
1042 p
- 2 + mcnt
- start
, mcnt2
);
1046 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
1047 EXTRACT_NUMBER_AND_INCR (mcnt2
, p
);
1048 fprintf (stderr
, "/jump_n to %td, %d times",
1049 p
- 2 + mcnt
- start
, mcnt2
);
1053 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
1054 EXTRACT_NUMBER_AND_INCR (mcnt2
, p
);
1055 fprintf (stderr
, "/set_number_at location %td to %d",
1056 p
- 2 + mcnt
- start
, mcnt2
);
1060 fprintf (stderr
, "/wordbound");
1064 fprintf (stderr
, "/notwordbound");
1068 fprintf (stderr
, "/wordbeg");
1072 fprintf (stderr
, "/wordend");
1076 fprintf (stderr
, "/symbeg");
1080 fprintf (stderr
, "/symend");
1084 fprintf (stderr
, "/syntaxspec");
1086 fprintf (stderr
, "/%d", mcnt
);
1090 fprintf (stderr
, "/notsyntaxspec");
1092 fprintf (stderr
, "/%d", mcnt
);
1097 fprintf (stderr
, "/before_dot");
1101 fprintf (stderr
, "/at_dot");
1105 fprintf (stderr
, "/after_dot");
1109 fprintf (stderr
, "/categoryspec");
1111 fprintf (stderr
, "/%d", mcnt
);
1114 case notcategoryspec
:
1115 fprintf (stderr
, "/notcategoryspec");
1117 fprintf (stderr
, "/%d", mcnt
);
1122 fprintf (stderr
, "/begbuf");
1126 fprintf (stderr
, "/endbuf");
1130 fprintf (stderr
, "?%d", *(p
-1));
1133 fprintf (stderr
, "\n");
1136 fprintf (stderr
, "%td:\tend of pattern.\n", p
- start
);
1141 print_compiled_pattern (struct re_pattern_buffer
*bufp
)
1143 re_char
*buffer
= bufp
->buffer
;
1145 print_partial_compiled_pattern (buffer
, buffer
+ bufp
->used
);
1146 printf ("%ld bytes used/%ld bytes allocated.\n",
1147 bufp
->used
, bufp
->allocated
);
1149 if (bufp
->fastmap_accurate
&& bufp
->fastmap
)
1151 printf ("fastmap: ");
1152 print_fastmap (bufp
->fastmap
);
1155 printf ("re_nsub: %zu\t", bufp
->re_nsub
);
1156 printf ("regs_alloc: %d\t", bufp
->regs_allocated
);
1157 printf ("can_be_null: %d\t", bufp
->can_be_null
);
1158 printf ("no_sub: %d\t", bufp
->no_sub
);
1159 printf ("not_bol: %d\t", bufp
->not_bol
);
1160 printf ("not_eol: %d\t", bufp
->not_eol
);
1161 printf ("syntax: %lx\n", bufp
->syntax
);
1163 /* Perhaps we should print the translate table? */
1168 print_double_string (re_char
*where
, re_char
*string1
, ssize_t size1
,
1169 re_char
*string2
, ssize_t size2
)
1177 if (FIRST_STRING_P (where
))
1179 for (this_char
= where
- string1
; this_char
< size1
; this_char
++)
1180 putchar (string1
[this_char
]);
1185 for (this_char
= where
- string2
; this_char
< size2
; this_char
++)
1186 putchar (string2
[this_char
]);
1190 #else /* not DEBUG */
1195 # define DEBUG_STATEMENT(e)
1196 # define DEBUG_PRINT(...)
1197 # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
1198 # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
1200 #endif /* not DEBUG */
1202 /* Use this to suppress gcc's `...may be used before initialized' warnings. */
1204 # define IF_LINT(Code) Code
1206 # define IF_LINT(Code) /* empty */
1209 /* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
1210 also be assigned to arbitrarily: each pattern buffer stores its own
1211 syntax, so it can be changed between regex compilations. */
1212 /* This has no initializer because initialized variables in Emacs
1213 become read-only after dumping. */
1214 reg_syntax_t re_syntax_options
;
1217 /* Specify the precise syntax of regexps for compilation. This provides
1218 for compatibility for various utilities which historically have
1219 different, incompatible syntaxes.
1221 The argument SYNTAX is a bit mask comprised of the various bits
1222 defined in regex.h. We return the old syntax. */
1225 re_set_syntax (reg_syntax_t syntax
)
1227 reg_syntax_t ret
= re_syntax_options
;
1229 re_syntax_options
= syntax
;
1232 WEAK_ALIAS (__re_set_syntax
, re_set_syntax
)
1234 /* Regexp to use to replace spaces, or NULL meaning don't. */
1235 static const_re_char
*whitespace_regexp
;
1238 re_set_whitespace_regexp (const char *regexp
)
1240 whitespace_regexp
= (const_re_char
*) regexp
;
1242 WEAK_ALIAS (__re_set_syntax
, re_set_syntax
)
1244 /* This table gives an error message for each of the error codes listed
1245 in regex.h. Obviously the order here has to be same as there.
1246 POSIX doesn't require that we do anything for REG_NOERROR,
1247 but why not be nice? */
1249 static const char *re_error_msgid
[] =
1251 gettext_noop ("Success"), /* REG_NOERROR */
1252 gettext_noop ("No match"), /* REG_NOMATCH */
1253 gettext_noop ("Invalid regular expression"), /* REG_BADPAT */
1254 gettext_noop ("Invalid collation character"), /* REG_ECOLLATE */
1255 gettext_noop ("Invalid character class name"), /* REG_ECTYPE */
1256 gettext_noop ("Trailing backslash"), /* REG_EESCAPE */
1257 gettext_noop ("Invalid back reference"), /* REG_ESUBREG */
1258 gettext_noop ("Unmatched [ or [^"), /* REG_EBRACK */
1259 gettext_noop ("Unmatched ( or \\("), /* REG_EPAREN */
1260 gettext_noop ("Unmatched \\{"), /* REG_EBRACE */
1261 gettext_noop ("Invalid content of \\{\\}"), /* REG_BADBR */
1262 gettext_noop ("Invalid range end"), /* REG_ERANGE */
1263 gettext_noop ("Memory exhausted"), /* REG_ESPACE */
1264 gettext_noop ("Invalid preceding regular expression"), /* REG_BADRPT */
1265 gettext_noop ("Premature end of regular expression"), /* REG_EEND */
1266 gettext_noop ("Regular expression too big"), /* REG_ESIZE */
1267 gettext_noop ("Unmatched ) or \\)"), /* REG_ERPAREN */
1268 gettext_noop ("Range striding over charsets") /* REG_ERANGEX */
1271 /* Avoiding alloca during matching, to placate r_alloc. */
1273 /* Define MATCH_MAY_ALLOCATE unless we need to make sure that the
1274 searching and matching functions should not call alloca. On some
1275 systems, alloca is implemented in terms of malloc, and if we're
1276 using the relocating allocator routines, then malloc could cause a
1277 relocation, which might (if the strings being searched are in the
1278 ralloc heap) shift the data out from underneath the regexp
1281 Here's another reason to avoid allocation: Emacs
1282 processes input from X in a signal handler; processing X input may
1283 call malloc; if input arrives while a matching routine is calling
1284 malloc, then we're scrod. But Emacs can't just block input while
1285 calling matching routines; then we don't notice interrupts when
1286 they come in. So, Emacs blocks input around all regexp calls
1287 except the matching calls, which it leaves unprotected, in the
1288 faith that they will not malloc. */
1290 /* Normally, this is fine. */
1291 #define MATCH_MAY_ALLOCATE
1293 /* The match routines may not allocate if (1) they would do it with malloc
1294 and (2) it's not safe for them to use malloc.
1295 Note that if REL_ALLOC is defined, matching would not use malloc for the
1296 failure stack, but we would still use it for the register vectors;
1297 so REL_ALLOC should not affect this. */
1298 #if defined REGEX_MALLOC && defined emacs
1299 # undef MATCH_MAY_ALLOCATE
1303 /* Failure stack declarations and macros; both re_compile_fastmap and
1304 re_match_2 use a failure stack. These have to be macros because of
1305 REGEX_ALLOCATE_STACK. */
1308 /* Approximate number of failure points for which to initially allocate space
1309 when matching. If this number is exceeded, we allocate more
1310 space, so it is not a hard limit. */
1311 #ifndef INIT_FAILURE_ALLOC
1312 # define INIT_FAILURE_ALLOC 20
1315 /* Roughly the maximum number of failure points on the stack. Would be
1316 exactly that if always used TYPICAL_FAILURE_SIZE items each time we failed.
1317 This is a variable only so users of regex can assign to it; we never
1318 change it ourselves. We always multiply it by TYPICAL_FAILURE_SIZE
1319 before using it, so it should probably be a byte-count instead. */
1320 # if defined MATCH_MAY_ALLOCATE
1321 /* Note that 4400 was enough to cause a crash on Alpha OSF/1,
1322 whose default stack limit is 2mb. In order for a larger
1323 value to work reliably, you have to try to make it accord
1324 with the process stack limit. */
1325 size_t re_max_failures
= 40000;
1327 size_t re_max_failures
= 4000;
1330 union fail_stack_elt
1333 /* This should be the biggest `int' that's no bigger than a pointer. */
1337 typedef union fail_stack_elt fail_stack_elt_t
;
1341 fail_stack_elt_t
*stack
;
1343 size_t avail
; /* Offset of next open position. */
1344 size_t frame
; /* Offset of the cur constructed frame. */
1347 #define FAIL_STACK_EMPTY() (fail_stack.frame == 0)
1350 /* Define macros to initialize and free the failure stack.
1351 Do `return -2' if the alloc fails. */
1353 #ifdef MATCH_MAY_ALLOCATE
1354 # define INIT_FAIL_STACK() \
1356 fail_stack.stack = \
1357 REGEX_ALLOCATE_STACK (INIT_FAILURE_ALLOC * TYPICAL_FAILURE_SIZE \
1358 * sizeof (fail_stack_elt_t)); \
1360 if (fail_stack.stack == NULL) \
1363 fail_stack.size = INIT_FAILURE_ALLOC; \
1364 fail_stack.avail = 0; \
1365 fail_stack.frame = 0; \
1368 # define INIT_FAIL_STACK() \
1370 fail_stack.avail = 0; \
1371 fail_stack.frame = 0; \
1374 # define RETALLOC_IF(addr, n, t) \
1375 if (addr) RETALLOC((addr), (n), t); else (addr) = TALLOC ((n), t)
1379 /* Double the size of FAIL_STACK, up to a limit
1380 which allows approximately `re_max_failures' items.
1382 Return 1 if succeeds, and 0 if either ran out of memory
1383 allocating space for it or it was already too large.
1385 REGEX_REALLOCATE_STACK requires `destination' be declared. */
1387 /* Factor to increase the failure stack size by
1388 when we increase it.
1389 This used to be 2, but 2 was too wasteful
1390 because the old discarded stacks added up to as much space
1391 were as ultimate, maximum-size stack. */
1392 #define FAIL_STACK_GROWTH_FACTOR 4
1394 #define GROW_FAIL_STACK(fail_stack) \
1395 (((fail_stack).size * sizeof (fail_stack_elt_t) \
1396 >= re_max_failures * TYPICAL_FAILURE_SIZE) \
1398 : ((fail_stack).stack \
1399 = REGEX_REALLOCATE_STACK ((fail_stack).stack, \
1400 (fail_stack).size * sizeof (fail_stack_elt_t), \
1401 min (re_max_failures * TYPICAL_FAILURE_SIZE, \
1402 ((fail_stack).size * sizeof (fail_stack_elt_t) \
1403 * FAIL_STACK_GROWTH_FACTOR))), \
1405 (fail_stack).stack == NULL \
1407 : ((fail_stack).size \
1408 = (min (re_max_failures * TYPICAL_FAILURE_SIZE, \
1409 ((fail_stack).size * sizeof (fail_stack_elt_t) \
1410 * FAIL_STACK_GROWTH_FACTOR)) \
1411 / sizeof (fail_stack_elt_t)), \
1415 /* Push a pointer value onto the failure stack.
1416 Assumes the variable `fail_stack'. Probably should only
1417 be called from within `PUSH_FAILURE_POINT'. */
1418 #define PUSH_FAILURE_POINTER(item) \
1419 fail_stack.stack[fail_stack.avail++].pointer = (item)
1421 /* This pushes an integer-valued item onto the failure stack.
1422 Assumes the variable `fail_stack'. Probably should only
1423 be called from within `PUSH_FAILURE_POINT'. */
1424 #define PUSH_FAILURE_INT(item) \
1425 fail_stack.stack[fail_stack.avail++].integer = (item)
1427 /* These POP... operations complement the PUSH... operations.
1428 All assume that `fail_stack' is nonempty. */
1429 #define POP_FAILURE_POINTER() fail_stack.stack[--fail_stack.avail].pointer
1430 #define POP_FAILURE_INT() fail_stack.stack[--fail_stack.avail].integer
1432 /* Individual items aside from the registers. */
1433 #define NUM_NONREG_ITEMS 3
1435 /* Used to examine the stack (to detect infinite loops). */
1436 #define FAILURE_PAT(h) fail_stack.stack[(h) - 1].pointer
1437 #define FAILURE_STR(h) (fail_stack.stack[(h) - 2].pointer)
1438 #define NEXT_FAILURE_HANDLE(h) fail_stack.stack[(h) - 3].integer
1439 #define TOP_FAILURE_HANDLE() fail_stack.frame
1442 #define ENSURE_FAIL_STACK(space) \
1443 while (REMAINING_AVAIL_SLOTS <= space) { \
1444 if (!GROW_FAIL_STACK (fail_stack)) \
1446 DEBUG_PRINT ("\n Doubled stack; size now: %zd\n", (fail_stack).size);\
1447 DEBUG_PRINT (" slots available: %zd\n", REMAINING_AVAIL_SLOTS);\
1450 /* Push register NUM onto the stack. */
1451 #define PUSH_FAILURE_REG(num) \
1453 char *destination; \
1455 ENSURE_FAIL_STACK(3); \
1456 DEBUG_PRINT (" Push reg %ld (spanning %p -> %p)\n", \
1457 n, regstart[n], regend[n]); \
1458 PUSH_FAILURE_POINTER (regstart[n]); \
1459 PUSH_FAILURE_POINTER (regend[n]); \
1460 PUSH_FAILURE_INT (n); \
1463 /* Change the counter's value to VAL, but make sure that it will
1464 be reset when backtracking. */
1465 #define PUSH_NUMBER(ptr,val) \
1467 char *destination; \
1469 ENSURE_FAIL_STACK(3); \
1470 EXTRACT_NUMBER (c, ptr); \
1471 DEBUG_PRINT (" Push number %p = %d -> %d\n", ptr, c, val); \
1472 PUSH_FAILURE_INT (c); \
1473 PUSH_FAILURE_POINTER (ptr); \
1474 PUSH_FAILURE_INT (-1); \
1475 STORE_NUMBER (ptr, val); \
1478 /* Pop a saved register off the stack. */
1479 #define POP_FAILURE_REG_OR_COUNT() \
1481 long pfreg = POP_FAILURE_INT (); \
1484 /* It's a counter. */ \
1485 /* Here, we discard `const', making re_match non-reentrant. */ \
1486 unsigned char *ptr = (unsigned char*) POP_FAILURE_POINTER (); \
1487 pfreg = POP_FAILURE_INT (); \
1488 STORE_NUMBER (ptr, pfreg); \
1489 DEBUG_PRINT (" Pop counter %p = %ld\n", ptr, pfreg); \
1493 regend[pfreg] = POP_FAILURE_POINTER (); \
1494 regstart[pfreg] = POP_FAILURE_POINTER (); \
1495 DEBUG_PRINT (" Pop reg %ld (spanning %p -> %p)\n", \
1496 pfreg, regstart[pfreg], regend[pfreg]); \
1500 /* Check that we are not stuck in an infinite loop. */
1501 #define CHECK_INFINITE_LOOP(pat_cur, string_place) \
1503 ssize_t failure = TOP_FAILURE_HANDLE (); \
1504 /* Check for infinite matching loops */ \
1505 while (failure > 0 \
1506 && (FAILURE_STR (failure) == string_place \
1507 || FAILURE_STR (failure) == NULL)) \
1509 assert (FAILURE_PAT (failure) >= bufp->buffer \
1510 && FAILURE_PAT (failure) <= bufp->buffer + bufp->used); \
1511 if (FAILURE_PAT (failure) == pat_cur) \
1516 DEBUG_PRINT (" Other pattern: %p\n", FAILURE_PAT (failure)); \
1517 failure = NEXT_FAILURE_HANDLE(failure); \
1519 DEBUG_PRINT (" Other string: %p\n", FAILURE_STR (failure)); \
1522 /* Push the information about the state we will need
1523 if we ever fail back to it.
1525 Requires variables fail_stack, regstart, regend and
1526 num_regs be declared. GROW_FAIL_STACK requires `destination' be
1529 Does `return FAILURE_CODE' if runs out of memory. */
1531 #define PUSH_FAILURE_POINT(pattern, string_place) \
1533 char *destination; \
1534 /* Must be int, so when we don't save any registers, the arithmetic \
1535 of 0 + -1 isn't done as unsigned. */ \
1537 DEBUG_STATEMENT (nfailure_points_pushed++); \
1538 DEBUG_PRINT ("\nPUSH_FAILURE_POINT:\n"); \
1539 DEBUG_PRINT (" Before push, next avail: %zd\n", (fail_stack).avail); \
1540 DEBUG_PRINT (" size: %zd\n", (fail_stack).size);\
1542 ENSURE_FAIL_STACK (NUM_NONREG_ITEMS); \
1544 DEBUG_PRINT ("\n"); \
1546 DEBUG_PRINT (" Push frame index: %zd\n", fail_stack.frame); \
1547 PUSH_FAILURE_INT (fail_stack.frame); \
1549 DEBUG_PRINT (" Push string %p: \"", string_place); \
1550 DEBUG_PRINT_DOUBLE_STRING (string_place, string1, size1, string2, size2);\
1551 DEBUG_PRINT ("\"\n"); \
1552 PUSH_FAILURE_POINTER (string_place); \
1554 DEBUG_PRINT (" Push pattern %p: ", pattern); \
1555 DEBUG_PRINT_COMPILED_PATTERN (bufp, pattern, pend); \
1556 PUSH_FAILURE_POINTER (pattern); \
1558 /* Close the frame by moving the frame pointer past it. */ \
1559 fail_stack.frame = fail_stack.avail; \
1562 /* Estimate the size of data pushed by a typical failure stack entry.
1563 An estimate is all we need, because all we use this for
1564 is to choose a limit for how big to make the failure stack. */
1565 /* BEWARE, the value `20' is hard-coded in emacs.c:main(). */
1566 #define TYPICAL_FAILURE_SIZE 20
1568 /* How many items can still be added to the stack without overflowing it. */
1569 #define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail)
1572 /* Pops what PUSH_FAIL_STACK pushes.
1574 We restore into the parameters, all of which should be lvalues:
1575 STR -- the saved data position.
1576 PAT -- the saved pattern position.
1577 REGSTART, REGEND -- arrays of string positions.
1579 Also assumes the variables `fail_stack' and (if debugging), `bufp',
1580 `pend', `string1', `size1', `string2', and `size2'. */
1582 #define POP_FAILURE_POINT(str, pat) \
1584 assert (!FAIL_STACK_EMPTY ()); \
1586 /* Remove failure points and point to how many regs pushed. */ \
1587 DEBUG_PRINT ("POP_FAILURE_POINT:\n"); \
1588 DEBUG_PRINT (" Before pop, next avail: %zd\n", fail_stack.avail); \
1589 DEBUG_PRINT (" size: %zd\n", fail_stack.size); \
1591 /* Pop the saved registers. */ \
1592 while (fail_stack.frame < fail_stack.avail) \
1593 POP_FAILURE_REG_OR_COUNT (); \
1595 pat = POP_FAILURE_POINTER (); \
1596 DEBUG_PRINT (" Popping pattern %p: ", pat); \
1597 DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend); \
1599 /* If the saved string location is NULL, it came from an \
1600 on_failure_keep_string_jump opcode, and we want to throw away the \
1601 saved NULL, thus retaining our current position in the string. */ \
1602 str = POP_FAILURE_POINTER (); \
1603 DEBUG_PRINT (" Popping string %p: \"", str); \
1604 DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2); \
1605 DEBUG_PRINT ("\"\n"); \
1607 fail_stack.frame = POP_FAILURE_INT (); \
1608 DEBUG_PRINT (" Popping frame index: %zd\n", fail_stack.frame); \
1610 assert (fail_stack.avail >= 0); \
1611 assert (fail_stack.frame <= fail_stack.avail); \
1613 DEBUG_STATEMENT (nfailure_points_popped++); \
1614 } while (0) /* POP_FAILURE_POINT */
1618 /* Registers are set to a sentinel when they haven't yet matched. */
1619 #define REG_UNSET(e) ((e) == NULL)
1621 /* Subroutine declarations and macros for regex_compile. */
1623 static reg_errcode_t
regex_compile (re_char
*pattern
, size_t size
,
1624 reg_syntax_t syntax
,
1625 struct re_pattern_buffer
*bufp
);
1626 static void store_op1 (re_opcode_t op
, unsigned char *loc
, int arg
);
1627 static void store_op2 (re_opcode_t op
, unsigned char *loc
, int arg1
, int arg2
);
1628 static void insert_op1 (re_opcode_t op
, unsigned char *loc
,
1629 int arg
, unsigned char *end
);
1630 static void insert_op2 (re_opcode_t op
, unsigned char *loc
,
1631 int arg1
, int arg2
, unsigned char *end
);
1632 static boolean
at_begline_loc_p (re_char
*pattern
, re_char
*p
,
1633 reg_syntax_t syntax
);
1634 static boolean
at_endline_loc_p (re_char
*p
, re_char
*pend
,
1635 reg_syntax_t syntax
);
1636 static re_char
*skip_one_char (re_char
*p
);
1637 static int analyze_first (re_char
*p
, re_char
*pend
,
1638 char *fastmap
, const int multibyte
);
1640 /* Fetch the next character in the uncompiled pattern, with no
1642 #define PATFETCH(c) \
1645 if (p == pend) return REG_EEND; \
1646 c = RE_STRING_CHAR_AND_LENGTH (p, len, multibyte); \
1651 /* If `translate' is non-null, return translate[D], else just D. We
1652 cast the subscript to translate because some data is declared as
1653 `char *', to avoid warnings when a string constant is passed. But
1654 when we use a character as a subscript we must make it unsigned. */
1656 # define TRANSLATE(d) \
1657 (RE_TRANSLATE_P (translate) ? RE_TRANSLATE (translate, (d)) : (d))
1661 /* Macros for outputting the compiled pattern into `buffer'. */
1663 /* If the buffer isn't allocated when it comes in, use this. */
1664 #define INIT_BUF_SIZE 32
1666 /* Make sure we have at least N more bytes of space in buffer. */
1667 #define GET_BUFFER_SPACE(n) \
1668 while ((size_t) (b - bufp->buffer + (n)) > bufp->allocated) \
1671 /* Make sure we have one more byte of buffer space and then add C to it. */
1672 #define BUF_PUSH(c) \
1674 GET_BUFFER_SPACE (1); \
1675 *b++ = (unsigned char) (c); \
1679 /* Ensure we have two more bytes of buffer space and then append C1 and C2. */
1680 #define BUF_PUSH_2(c1, c2) \
1682 GET_BUFFER_SPACE (2); \
1683 *b++ = (unsigned char) (c1); \
1684 *b++ = (unsigned char) (c2); \
1688 /* Store a jump with opcode OP at LOC to location TO. We store a
1689 relative address offset by the three bytes the jump itself occupies. */
1690 #define STORE_JUMP(op, loc, to) \
1691 store_op1 (op, loc, (to) - (loc) - 3)
1693 /* Likewise, for a two-argument jump. */
1694 #define STORE_JUMP2(op, loc, to, arg) \
1695 store_op2 (op, loc, (to) - (loc) - 3, arg)
1697 /* Like `STORE_JUMP', but for inserting. Assume `b' is the buffer end. */
1698 #define INSERT_JUMP(op, loc, to) \
1699 insert_op1 (op, loc, (to) - (loc) - 3, b)
1701 /* Like `STORE_JUMP2', but for inserting. Assume `b' is the buffer end. */
1702 #define INSERT_JUMP2(op, loc, to, arg) \
1703 insert_op2 (op, loc, (to) - (loc) - 3, arg, b)
1706 /* This is not an arbitrary limit: the arguments which represent offsets
1707 into the pattern are two bytes long. So if 2^15 bytes turns out to
1708 be too small, many things would have to change. */
1709 # define MAX_BUF_SIZE (1L << 15)
1711 /* Extend the buffer by twice its current size via realloc and
1712 reset the pointers that pointed into the old block to point to the
1713 correct places in the new one. If extending the buffer results in it
1714 being larger than MAX_BUF_SIZE, then flag memory exhausted. */
1715 #if __BOUNDED_POINTERS__
1716 # define SET_HIGH_BOUND(P) (__ptrhigh (P) = __ptrlow (P) + bufp->allocated)
1717 # define MOVE_BUFFER_POINTER(P) \
1718 (__ptrlow (P) = new_buffer + (__ptrlow (P) - old_buffer), \
1719 SET_HIGH_BOUND (P), \
1720 __ptrvalue (P) = new_buffer + (__ptrvalue (P) - old_buffer))
1721 # define ELSE_EXTEND_BUFFER_HIGH_BOUND \
1724 SET_HIGH_BOUND (b); \
1725 SET_HIGH_BOUND (begalt); \
1726 if (fixup_alt_jump) \
1727 SET_HIGH_BOUND (fixup_alt_jump); \
1729 SET_HIGH_BOUND (laststart); \
1730 if (pending_exact) \
1731 SET_HIGH_BOUND (pending_exact); \
1734 # define MOVE_BUFFER_POINTER(P) ((P) = new_buffer + ((P) - old_buffer))
1735 # define ELSE_EXTEND_BUFFER_HIGH_BOUND
1737 #define EXTEND_BUFFER() \
1739 unsigned char *old_buffer = bufp->buffer; \
1740 if (bufp->allocated == MAX_BUF_SIZE) \
1742 bufp->allocated <<= 1; \
1743 if (bufp->allocated > MAX_BUF_SIZE) \
1744 bufp->allocated = MAX_BUF_SIZE; \
1745 RETALLOC (bufp->buffer, bufp->allocated, unsigned char); \
1746 if (bufp->buffer == NULL) \
1747 return REG_ESPACE; \
1748 /* If the buffer moved, move all the pointers into it. */ \
1749 if (old_buffer != bufp->buffer) \
1751 unsigned char *new_buffer = bufp->buffer; \
1752 MOVE_BUFFER_POINTER (b); \
1753 MOVE_BUFFER_POINTER (begalt); \
1754 if (fixup_alt_jump) \
1755 MOVE_BUFFER_POINTER (fixup_alt_jump); \
1757 MOVE_BUFFER_POINTER (laststart); \
1758 if (pending_exact) \
1759 MOVE_BUFFER_POINTER (pending_exact); \
1761 ELSE_EXTEND_BUFFER_HIGH_BOUND \
1765 /* Since we have one byte reserved for the register number argument to
1766 {start,stop}_memory, the maximum number of groups we can report
1767 things about is what fits in that byte. */
1768 #define MAX_REGNUM 255
1770 /* But patterns can have more than `MAX_REGNUM' registers. We just
1771 ignore the excess. */
1772 typedef int regnum_t
;
1775 /* Macros for the compile stack. */
1777 /* Since offsets can go either forwards or backwards, this type needs to
1778 be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1. */
1779 /* int may be not enough when sizeof(int) == 2. */
1780 typedef long pattern_offset_t
;
1784 pattern_offset_t begalt_offset
;
1785 pattern_offset_t fixup_alt_jump
;
1786 pattern_offset_t laststart_offset
;
1788 } compile_stack_elt_t
;
1793 compile_stack_elt_t
*stack
;
1795 size_t avail
; /* Offset of next open position. */
1796 } compile_stack_type
;
1799 #define INIT_COMPILE_STACK_SIZE 32
1801 #define COMPILE_STACK_EMPTY (compile_stack.avail == 0)
1802 #define COMPILE_STACK_FULL (compile_stack.avail == compile_stack.size)
1804 /* The next available element. */
1805 #define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
1807 /* Explicit quit checking is needed for Emacs, which uses polling to
1808 process input events. */
1810 # define IMMEDIATE_QUIT_CHECK \
1812 if (immediate_quit) QUIT; \
1815 # define IMMEDIATE_QUIT_CHECK ((void)0)
1818 /* Structure to manage work area for range table. */
1819 struct range_table_work_area
1821 int *table
; /* actual work area. */
1822 int allocated
; /* allocated size for work area in bytes. */
1823 int used
; /* actually used size in words. */
1824 int bits
; /* flag to record character classes */
1829 /* Make sure that WORK_AREA can hold more N multibyte characters.
1830 This is used only in set_image_of_range and set_image_of_range_1.
1831 It expects WORK_AREA to be a pointer.
1832 If it can't get the space, it returns from the surrounding function. */
1834 #define EXTEND_RANGE_TABLE(work_area, n) \
1836 if (((work_area).used + (n)) * sizeof (int) > (work_area).allocated) \
1838 extend_range_table_work_area (&work_area); \
1839 if ((work_area).table == 0) \
1840 return (REG_ESPACE); \
1844 #define SET_RANGE_TABLE_WORK_AREA_BIT(work_area, bit) \
1845 (work_area).bits |= (bit)
1847 /* Set a range (RANGE_START, RANGE_END) to WORK_AREA. */
1848 #define SET_RANGE_TABLE_WORK_AREA(work_area, range_start, range_end) \
1850 EXTEND_RANGE_TABLE ((work_area), 2); \
1851 (work_area).table[(work_area).used++] = (range_start); \
1852 (work_area).table[(work_area).used++] = (range_end); \
1857 /* Free allocated memory for WORK_AREA. */
1858 #define FREE_RANGE_TABLE_WORK_AREA(work_area) \
1860 if ((work_area).table) \
1861 free ((work_area).table); \
1864 #define CLEAR_RANGE_TABLE_WORK_USED(work_area) ((work_area).used = 0, (work_area).bits = 0)
1865 #define RANGE_TABLE_WORK_USED(work_area) ((work_area).used)
1866 #define RANGE_TABLE_WORK_BITS(work_area) ((work_area).bits)
1867 #define RANGE_TABLE_WORK_ELT(work_area, i) ((work_area).table[i])
1869 /* Bits used to implement the multibyte-part of the various character classes
1870 such as [:alnum:] in a charset's range table. The code currently assumes
1871 that only the low 16 bits are used. */
1872 #define BIT_WORD 0x1
1873 #define BIT_LOWER 0x2
1874 #define BIT_PUNCT 0x4
1875 #define BIT_SPACE 0x8
1876 #define BIT_UPPER 0x10
1877 #define BIT_MULTIBYTE 0x20
1878 #define BIT_ALPHA 0x40
1879 #define BIT_ALNUM 0x80
1880 #define BIT_GRAPH 0x100
1881 #define BIT_PRINT 0x200
1884 /* Set the bit for character C in a list. */
1885 #define SET_LIST_BIT(c) (b[((c)) / BYTEWIDTH] |= 1 << ((c) % BYTEWIDTH))
1890 /* Store characters in the range FROM to TO in the bitmap at B (for
1891 ASCII and unibyte characters) and WORK_AREA (for multibyte
1892 characters) while translating them and paying attention to the
1893 continuity of translated characters.
1895 Implementation note: It is better to implement these fairly big
1896 macros by a function, but it's not that easy because macros called
1897 in this macro assume various local variables already declared. */
1899 /* Both FROM and TO are ASCII characters. */
1901 #define SETUP_ASCII_RANGE(work_area, FROM, TO) \
1905 for (C0 = (FROM); C0 <= (TO); C0++) \
1907 C1 = TRANSLATE (C0); \
1908 if (! ASCII_CHAR_P (C1)) \
1910 SET_RANGE_TABLE_WORK_AREA ((work_area), C1, C1); \
1911 if ((C1 = RE_CHAR_TO_UNIBYTE (C1)) < 0) \
1914 SET_LIST_BIT (C1); \
1919 /* Both FROM and TO are unibyte characters (0x80..0xFF). */
1921 #define SETUP_UNIBYTE_RANGE(work_area, FROM, TO) \
1923 int C0, C1, C2, I; \
1924 int USED = RANGE_TABLE_WORK_USED (work_area); \
1926 for (C0 = (FROM); C0 <= (TO); C0++) \
1928 C1 = RE_CHAR_TO_MULTIBYTE (C0); \
1929 if (CHAR_BYTE8_P (C1)) \
1930 SET_LIST_BIT (C0); \
1933 C2 = TRANSLATE (C1); \
1935 || (C1 = RE_CHAR_TO_UNIBYTE (C2)) < 0) \
1937 SET_LIST_BIT (C1); \
1938 for (I = RANGE_TABLE_WORK_USED (work_area) - 2; I >= USED; I -= 2) \
1940 int from = RANGE_TABLE_WORK_ELT (work_area, I); \
1941 int to = RANGE_TABLE_WORK_ELT (work_area, I + 1); \
1943 if (C2 >= from - 1 && C2 <= to + 1) \
1945 if (C2 == from - 1) \
1946 RANGE_TABLE_WORK_ELT (work_area, I)--; \
1947 else if (C2 == to + 1) \
1948 RANGE_TABLE_WORK_ELT (work_area, I + 1)++; \
1953 SET_RANGE_TABLE_WORK_AREA ((work_area), C2, C2); \
1959 /* Both FROM and TO are multibyte characters. */
1961 #define SETUP_MULTIBYTE_RANGE(work_area, FROM, TO) \
1963 int C0, C1, C2, I, USED = RANGE_TABLE_WORK_USED (work_area); \
1965 SET_RANGE_TABLE_WORK_AREA ((work_area), (FROM), (TO)); \
1966 for (C0 = (FROM); C0 <= (TO); C0++) \
1968 C1 = TRANSLATE (C0); \
1969 if ((C2 = RE_CHAR_TO_UNIBYTE (C1)) >= 0 \
1970 || (C1 != C0 && (C2 = RE_CHAR_TO_UNIBYTE (C0)) >= 0)) \
1971 SET_LIST_BIT (C2); \
1972 if (C1 >= (FROM) && C1 <= (TO)) \
1974 for (I = RANGE_TABLE_WORK_USED (work_area) - 2; I >= USED; I -= 2) \
1976 int from = RANGE_TABLE_WORK_ELT (work_area, I); \
1977 int to = RANGE_TABLE_WORK_ELT (work_area, I + 1); \
1979 if (C1 >= from - 1 && C1 <= to + 1) \
1981 if (C1 == from - 1) \
1982 RANGE_TABLE_WORK_ELT (work_area, I)--; \
1983 else if (C1 == to + 1) \
1984 RANGE_TABLE_WORK_ELT (work_area, I + 1)++; \
1989 SET_RANGE_TABLE_WORK_AREA ((work_area), C1, C1); \
1995 /* Get the next unsigned number in the uncompiled pattern. */
1996 #define GET_INTERVAL_COUNT(num) \
1999 FREE_STACK_RETURN (REG_EBRACE); \
2003 while ('0' <= c && c <= '9') \
2007 if (RE_DUP_MAX / 10 - (RE_DUP_MAX % 10 < c - '0') < num) \
2008 FREE_STACK_RETURN (REG_BADBR); \
2009 num = num * 10 + c - '0'; \
2011 FREE_STACK_RETURN (REG_EBRACE); \
2017 #if ! WIDE_CHAR_SUPPORT
2019 /* Map a string to the char class it names (if any). */
2021 re_wctype (const_re_char
*str
)
2023 const char *string
= (const char *) str
;
2024 if (STREQ (string
, "alnum")) return RECC_ALNUM
;
2025 else if (STREQ (string
, "alpha")) return RECC_ALPHA
;
2026 else if (STREQ (string
, "word")) return RECC_WORD
;
2027 else if (STREQ (string
, "ascii")) return RECC_ASCII
;
2028 else if (STREQ (string
, "nonascii")) return RECC_NONASCII
;
2029 else if (STREQ (string
, "graph")) return RECC_GRAPH
;
2030 else if (STREQ (string
, "lower")) return RECC_LOWER
;
2031 else if (STREQ (string
, "print")) return RECC_PRINT
;
2032 else if (STREQ (string
, "punct")) return RECC_PUNCT
;
2033 else if (STREQ (string
, "space")) return RECC_SPACE
;
2034 else if (STREQ (string
, "upper")) return RECC_UPPER
;
2035 else if (STREQ (string
, "unibyte")) return RECC_UNIBYTE
;
2036 else if (STREQ (string
, "multibyte")) return RECC_MULTIBYTE
;
2037 else if (STREQ (string
, "digit")) return RECC_DIGIT
;
2038 else if (STREQ (string
, "xdigit")) return RECC_XDIGIT
;
2039 else if (STREQ (string
, "cntrl")) return RECC_CNTRL
;
2040 else if (STREQ (string
, "blank")) return RECC_BLANK
;
2044 /* True if CH is in the char class CC. */
2046 re_iswctype (int ch
, re_wctype_t cc
)
2050 case RECC_ALNUM
: return ISALNUM (ch
) != 0;
2051 case RECC_ALPHA
: return ISALPHA (ch
) != 0;
2052 case RECC_BLANK
: return ISBLANK (ch
) != 0;
2053 case RECC_CNTRL
: return ISCNTRL (ch
) != 0;
2054 case RECC_DIGIT
: return ISDIGIT (ch
) != 0;
2055 case RECC_GRAPH
: return ISGRAPH (ch
) != 0;
2056 case RECC_LOWER
: return ISLOWER (ch
) != 0;
2057 case RECC_PRINT
: return ISPRINT (ch
) != 0;
2058 case RECC_PUNCT
: return ISPUNCT (ch
) != 0;
2059 case RECC_SPACE
: return ISSPACE (ch
) != 0;
2060 case RECC_UPPER
: return ISUPPER (ch
) != 0;
2061 case RECC_XDIGIT
: return ISXDIGIT (ch
) != 0;
2062 case RECC_ASCII
: return IS_REAL_ASCII (ch
) != 0;
2063 case RECC_NONASCII
: return !IS_REAL_ASCII (ch
);
2064 case RECC_UNIBYTE
: return ISUNIBYTE (ch
) != 0;
2065 case RECC_MULTIBYTE
: return !ISUNIBYTE (ch
);
2066 case RECC_WORD
: return ISWORD (ch
) != 0;
2067 case RECC_ERROR
: return false;
2073 /* Return a bit-pattern to use in the range-table bits to match multibyte
2074 chars of class CC. */
2076 re_wctype_to_bit (re_wctype_t cc
)
2081 case RECC_MULTIBYTE
: return BIT_MULTIBYTE
;
2082 case RECC_ALPHA
: return BIT_ALPHA
;
2083 case RECC_ALNUM
: return BIT_ALNUM
;
2084 case RECC_WORD
: return BIT_WORD
;
2085 case RECC_LOWER
: return BIT_LOWER
;
2086 case RECC_UPPER
: return BIT_UPPER
;
2087 case RECC_PUNCT
: return BIT_PUNCT
;
2088 case RECC_SPACE
: return BIT_SPACE
;
2089 case RECC_GRAPH
: return BIT_GRAPH
;
2090 case RECC_PRINT
: return BIT_PRINT
;
2091 case RECC_ASCII
: case RECC_DIGIT
: case RECC_XDIGIT
: case RECC_CNTRL
:
2092 case RECC_BLANK
: case RECC_UNIBYTE
: case RECC_ERROR
: return 0;
2099 /* Filling in the work area of a range. */
2101 /* Actually extend the space in WORK_AREA. */
2104 extend_range_table_work_area (struct range_table_work_area
*work_area
)
2106 work_area
->allocated
+= 16 * sizeof (int);
2107 work_area
->table
= realloc (work_area
->table
, work_area
->allocated
);
2113 /* Carefully find the ranges of codes that are equivalent
2114 under case conversion to the range start..end when passed through
2115 TRANSLATE. Handle the case where non-letters can come in between
2116 two upper-case letters (which happens in Latin-1).
2117 Also handle the case of groups of more than 2 case-equivalent chars.
2119 The basic method is to look at consecutive characters and see
2120 if they can form a run that can be handled as one.
2122 Returns -1 if successful, REG_ESPACE if ran out of space. */
2125 set_image_of_range_1 (struct range_table_work_area
*work_area
,
2126 re_wchar_t start
, re_wchar_t end
,
2127 RE_TRANSLATE_TYPE translate
)
2129 /* `one_case' indicates a character, or a run of characters,
2130 each of which is an isolate (no case-equivalents).
2131 This includes all ASCII non-letters.
2133 `two_case' indicates a character, or a run of characters,
2134 each of which has two case-equivalent forms.
2135 This includes all ASCII letters.
2137 `strange' indicates a character that has more than one
2140 enum case_type
{one_case
, two_case
, strange
};
2142 /* Describe the run that is in progress,
2143 which the next character can try to extend.
2144 If run_type is strange, that means there really is no run.
2145 If run_type is one_case, then run_start...run_end is the run.
2146 If run_type is two_case, then the run is run_start...run_end,
2147 and the case-equivalents end at run_eqv_end. */
2149 enum case_type run_type
= strange
;
2150 int run_start
, run_end
, run_eqv_end
;
2152 Lisp_Object eqv_table
;
2154 if (!RE_TRANSLATE_P (translate
))
2156 EXTEND_RANGE_TABLE (work_area
, 2);
2157 work_area
->table
[work_area
->used
++] = (start
);
2158 work_area
->table
[work_area
->used
++] = (end
);
2162 eqv_table
= XCHAR_TABLE (translate
)->extras
[2];
2164 for (; start
<= end
; start
++)
2166 enum case_type this_type
;
2167 int eqv
= RE_TRANSLATE (eqv_table
, start
);
2168 int minchar
, maxchar
;
2170 /* Classify this character */
2172 this_type
= one_case
;
2173 else if (RE_TRANSLATE (eqv_table
, eqv
) == start
)
2174 this_type
= two_case
;
2176 this_type
= strange
;
2179 minchar
= start
, maxchar
= eqv
;
2181 minchar
= eqv
, maxchar
= start
;
2183 /* Can this character extend the run in progress? */
2184 if (this_type
== strange
|| this_type
!= run_type
2185 || !(minchar
== run_end
+ 1
2186 && (run_type
== two_case
2187 ? maxchar
== run_eqv_end
+ 1 : 1)))
2190 Record each of its equivalent ranges. */
2191 if (run_type
== one_case
)
2193 EXTEND_RANGE_TABLE (work_area
, 2);
2194 work_area
->table
[work_area
->used
++] = run_start
;
2195 work_area
->table
[work_area
->used
++] = run_end
;
2197 else if (run_type
== two_case
)
2199 EXTEND_RANGE_TABLE (work_area
, 4);
2200 work_area
->table
[work_area
->used
++] = run_start
;
2201 work_area
->table
[work_area
->used
++] = run_end
;
2202 work_area
->table
[work_area
->used
++]
2203 = RE_TRANSLATE (eqv_table
, run_start
);
2204 work_area
->table
[work_area
->used
++]
2205 = RE_TRANSLATE (eqv_table
, run_end
);
2210 if (this_type
== strange
)
2212 /* For a strange character, add each of its equivalents, one
2213 by one. Don't start a range. */
2216 EXTEND_RANGE_TABLE (work_area
, 2);
2217 work_area
->table
[work_area
->used
++] = eqv
;
2218 work_area
->table
[work_area
->used
++] = eqv
;
2219 eqv
= RE_TRANSLATE (eqv_table
, eqv
);
2221 while (eqv
!= start
);
2224 /* Add this char to the run, or start a new run. */
2225 else if (run_type
== strange
)
2227 /* Initialize a new range. */
2228 run_type
= this_type
;
2231 run_eqv_end
= RE_TRANSLATE (eqv_table
, run_end
);
2235 /* Extend a running range. */
2237 run_eqv_end
= RE_TRANSLATE (eqv_table
, run_end
);
2241 /* If a run is still in progress at the end, finish it now
2242 by recording its equivalent ranges. */
2243 if (run_type
== one_case
)
2245 EXTEND_RANGE_TABLE (work_area
, 2);
2246 work_area
->table
[work_area
->used
++] = run_start
;
2247 work_area
->table
[work_area
->used
++] = run_end
;
2249 else if (run_type
== two_case
)
2251 EXTEND_RANGE_TABLE (work_area
, 4);
2252 work_area
->table
[work_area
->used
++] = run_start
;
2253 work_area
->table
[work_area
->used
++] = run_end
;
2254 work_area
->table
[work_area
->used
++]
2255 = RE_TRANSLATE (eqv_table
, run_start
);
2256 work_area
->table
[work_area
->used
++]
2257 = RE_TRANSLATE (eqv_table
, run_end
);
2265 /* Record the image of the range start..end when passed through
2266 TRANSLATE. This is not necessarily TRANSLATE(start)..TRANSLATE(end)
2267 and is not even necessarily contiguous.
2268 Normally we approximate it with the smallest contiguous range that contains
2269 all the chars we need. However, for Latin-1 we go to extra effort
2272 This function is not called for ASCII ranges.
2274 Returns -1 if successful, REG_ESPACE if ran out of space. */
2277 set_image_of_range (struct range_table_work_area
*work_area
,
2278 re_wchar_t start
, re_wchar_t end
,
2279 RE_TRANSLATE_TYPE translate
)
2281 re_wchar_t cmin
, cmax
;
2284 /* For Latin-1 ranges, use set_image_of_range_1
2285 to get proper handling of ranges that include letters and nonletters.
2286 For a range that includes the whole of Latin-1, this is not necessary.
2287 For other character sets, we don't bother to get this right. */
2288 if (RE_TRANSLATE_P (translate
) && start
< 04400
2289 && !(start
< 04200 && end
>= 04377))
2296 tem
= set_image_of_range_1 (work_area
, start
, newend
, translate
);
2306 EXTEND_RANGE_TABLE (work_area
, 2);
2307 work_area
->table
[work_area
->used
++] = (start
);
2308 work_area
->table
[work_area
->used
++] = (end
);
2310 cmin
= -1, cmax
= -1;
2312 if (RE_TRANSLATE_P (translate
))
2316 for (ch
= start
; ch
<= end
; ch
++)
2318 re_wchar_t c
= TRANSLATE (ch
);
2319 if (! (start
<= c
&& c
<= end
))
2325 cmin
= min (cmin
, c
);
2326 cmax
= max (cmax
, c
);
2333 EXTEND_RANGE_TABLE (work_area
, 2);
2334 work_area
->table
[work_area
->used
++] = (cmin
);
2335 work_area
->table
[work_area
->used
++] = (cmax
);
2343 #ifndef MATCH_MAY_ALLOCATE
2345 /* If we cannot allocate large objects within re_match_2_internal,
2346 we make the fail stack and register vectors global.
2347 The fail stack, we grow to the maximum size when a regexp
2349 The register vectors, we adjust in size each time we
2350 compile a regexp, according to the number of registers it needs. */
2352 static fail_stack_type fail_stack
;
2354 /* Size with which the following vectors are currently allocated.
2355 That is so we can make them bigger as needed,
2356 but never make them smaller. */
2357 static int regs_allocated_size
;
2359 static re_char
** regstart
, ** regend
;
2360 static re_char
**best_regstart
, **best_regend
;
2362 /* Make the register vectors big enough for NUM_REGS registers,
2363 but don't make them smaller. */
2366 regex_grow_registers (int num_regs
)
2368 if (num_regs
> regs_allocated_size
)
2370 RETALLOC_IF (regstart
, num_regs
, re_char
*);
2371 RETALLOC_IF (regend
, num_regs
, re_char
*);
2372 RETALLOC_IF (best_regstart
, num_regs
, re_char
*);
2373 RETALLOC_IF (best_regend
, num_regs
, re_char
*);
2375 regs_allocated_size
= num_regs
;
2379 #endif /* not MATCH_MAY_ALLOCATE */
2381 static boolean
group_in_compile_stack (compile_stack_type compile_stack
,
2384 /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
2385 Returns one of error codes defined in `regex.h', or zero for success.
2387 Assumes the `allocated' (and perhaps `buffer') and `translate'
2388 fields are set in BUFP on entry.
2390 If it succeeds, results are put in BUFP (if it returns an error, the
2391 contents of BUFP are undefined):
2392 `buffer' is the compiled pattern;
2393 `syntax' is set to SYNTAX;
2394 `used' is set to the length of the compiled pattern;
2395 `fastmap_accurate' is zero;
2396 `re_nsub' is the number of subexpressions in PATTERN;
2397 `not_bol' and `not_eol' are zero;
2399 The `fastmap' field is neither examined nor set. */
2401 /* Insert the `jump' from the end of last alternative to "here".
2402 The space for the jump has already been allocated. */
2403 #define FIXUP_ALT_JUMP() \
2405 if (fixup_alt_jump) \
2406 STORE_JUMP (jump, fixup_alt_jump, b); \
2410 /* Return, freeing storage we allocated. */
2411 #define FREE_STACK_RETURN(value) \
2413 FREE_RANGE_TABLE_WORK_AREA (range_table_work); \
2414 free (compile_stack.stack); \
2418 static reg_errcode_t
2419 regex_compile (const_re_char
*pattern
, size_t size
, reg_syntax_t syntax
,
2420 struct re_pattern_buffer
*bufp
)
2422 /* We fetch characters from PATTERN here. */
2423 register re_wchar_t c
, c1
;
2425 /* Points to the end of the buffer, where we should append. */
2426 register unsigned char *b
;
2428 /* Keeps track of unclosed groups. */
2429 compile_stack_type compile_stack
;
2431 /* Points to the current (ending) position in the pattern. */
2433 /* `const' makes AIX compiler fail. */
2434 unsigned char *p
= pattern
;
2436 re_char
*p
= pattern
;
2438 re_char
*pend
= pattern
+ size
;
2440 /* How to translate the characters in the pattern. */
2441 RE_TRANSLATE_TYPE translate
= bufp
->translate
;
2443 /* Address of the count-byte of the most recently inserted `exactn'
2444 command. This makes it possible to tell if a new exact-match
2445 character can be added to that command or if the character requires
2446 a new `exactn' command. */
2447 unsigned char *pending_exact
= 0;
2449 /* Address of start of the most recently finished expression.
2450 This tells, e.g., postfix * where to find the start of its
2451 operand. Reset at the beginning of groups and alternatives. */
2452 unsigned char *laststart
= 0;
2454 /* Address of beginning of regexp, or inside of last group. */
2455 unsigned char *begalt
;
2457 /* Place in the uncompiled pattern (i.e., the {) to
2458 which to go back if the interval is invalid. */
2459 re_char
*beg_interval
;
2461 /* Address of the place where a forward jump should go to the end of
2462 the containing expression. Each alternative of an `or' -- except the
2463 last -- ends with a forward jump of this sort. */
2464 unsigned char *fixup_alt_jump
= 0;
2466 /* Work area for range table of charset. */
2467 struct range_table_work_area range_table_work
;
2469 /* If the object matched can contain multibyte characters. */
2470 const boolean multibyte
= RE_MULTIBYTE_P (bufp
);
2472 /* Nonzero if we have pushed down into a subpattern. */
2473 int in_subpattern
= 0;
2475 /* These hold the values of p, pattern, and pend from the main
2476 pattern when we have pushed into a subpattern. */
2477 re_char
*main_p
IF_LINT (= NULL
);
2478 re_char
*main_pattern
IF_LINT (= NULL
);
2479 re_char
*main_pend
IF_LINT (= NULL
);
2483 DEBUG_PRINT ("\nCompiling pattern: ");
2486 unsigned debug_count
;
2488 for (debug_count
= 0; debug_count
< size
; debug_count
++)
2489 putchar (pattern
[debug_count
]);
2494 /* Initialize the compile stack. */
2495 compile_stack
.stack
= TALLOC (INIT_COMPILE_STACK_SIZE
, compile_stack_elt_t
);
2496 if (compile_stack
.stack
== NULL
)
2499 compile_stack
.size
= INIT_COMPILE_STACK_SIZE
;
2500 compile_stack
.avail
= 0;
2502 range_table_work
.table
= 0;
2503 range_table_work
.allocated
= 0;
2505 /* Initialize the pattern buffer. */
2506 bufp
->syntax
= syntax
;
2507 bufp
->fastmap_accurate
= 0;
2508 bufp
->not_bol
= bufp
->not_eol
= 0;
2509 bufp
->used_syntax
= 0;
2511 /* Set `used' to zero, so that if we return an error, the pattern
2512 printer (for debugging) will think there's no pattern. We reset it
2516 /* Always count groups, whether or not bufp->no_sub is set. */
2519 #if !defined emacs && !defined SYNTAX_TABLE
2520 /* Initialize the syntax table. */
2521 init_syntax_once ();
2524 if (bufp
->allocated
== 0)
2527 { /* If zero allocated, but buffer is non-null, try to realloc
2528 enough space. This loses if buffer's address is bogus, but
2529 that is the user's responsibility. */
2530 RETALLOC (bufp
->buffer
, INIT_BUF_SIZE
, unsigned char);
2533 { /* Caller did not allocate a buffer. Do it for them. */
2534 bufp
->buffer
= TALLOC (INIT_BUF_SIZE
, unsigned char);
2536 if (!bufp
->buffer
) FREE_STACK_RETURN (REG_ESPACE
);
2538 bufp
->allocated
= INIT_BUF_SIZE
;
2541 begalt
= b
= bufp
->buffer
;
2543 /* Loop through the uncompiled pattern until we're at the end. */
2548 /* If this is the end of an included regexp,
2549 pop back to the main regexp and try again. */
2553 pattern
= main_pattern
;
2558 /* If this is the end of the main regexp, we are done. */
2570 /* If there's no special whitespace regexp, treat
2571 spaces normally. And don't try to do this recursively. */
2572 if (!whitespace_regexp
|| in_subpattern
)
2575 /* Peek past following spaces. */
2582 /* If the spaces are followed by a repetition op,
2583 treat them normally. */
2585 && (*p1
== '*' || *p1
== '+' || *p1
== '?'
2586 || (*p1
== '\\' && p1
+ 1 != pend
&& p1
[1] == '{')))
2589 /* Replace the spaces with the whitespace regexp. */
2593 main_pattern
= pattern
;
2594 p
= pattern
= whitespace_regexp
;
2595 pend
= p
+ strlen ((const char *) p
);
2601 if ( /* If at start of pattern, it's an operator. */
2603 /* If context independent, it's an operator. */
2604 || syntax
& RE_CONTEXT_INDEP_ANCHORS
2605 /* Otherwise, depends on what's come before. */
2606 || at_begline_loc_p (pattern
, p
, syntax
))
2607 BUF_PUSH ((syntax
& RE_NO_NEWLINE_ANCHOR
) ? begbuf
: begline
);
2616 if ( /* If at end of pattern, it's an operator. */
2618 /* If context independent, it's an operator. */
2619 || syntax
& RE_CONTEXT_INDEP_ANCHORS
2620 /* Otherwise, depends on what's next. */
2621 || at_endline_loc_p (p
, pend
, syntax
))
2622 BUF_PUSH ((syntax
& RE_NO_NEWLINE_ANCHOR
) ? endbuf
: endline
);
2631 if ((syntax
& RE_BK_PLUS_QM
)
2632 || (syntax
& RE_LIMITED_OPS
))
2636 /* If there is no previous pattern... */
2639 if (syntax
& RE_CONTEXT_INVALID_OPS
)
2640 FREE_STACK_RETURN (REG_BADRPT
);
2641 else if (!(syntax
& RE_CONTEXT_INDEP_OPS
))
2646 /* 1 means zero (many) matches is allowed. */
2647 boolean zero_times_ok
= 0, many_times_ok
= 0;
2650 /* If there is a sequence of repetition chars, collapse it
2651 down to just one (the right one). We can't combine
2652 interval operators with these because of, e.g., `a{2}*',
2653 which should only match an even number of `a's. */
2657 if ((syntax
& RE_FRUGAL
)
2658 && c
== '?' && (zero_times_ok
|| many_times_ok
))
2662 zero_times_ok
|= c
!= '+';
2663 many_times_ok
|= c
!= '?';
2669 || (!(syntax
& RE_BK_PLUS_QM
)
2670 && (*p
== '+' || *p
== '?')))
2672 else if (syntax
& RE_BK_PLUS_QM
&& *p
== '\\')
2675 FREE_STACK_RETURN (REG_EESCAPE
);
2676 if (p
[1] == '+' || p
[1] == '?')
2677 PATFETCH (c
); /* Gobble up the backslash. */
2683 /* If we get here, we found another repeat character. */
2687 /* Star, etc. applied to an empty pattern is equivalent
2688 to an empty pattern. */
2689 if (!laststart
|| laststart
== b
)
2692 /* Now we know whether or not zero matches is allowed
2693 and also whether or not two or more matches is allowed. */
2698 boolean simple
= skip_one_char (laststart
) == b
;
2699 size_t startoffset
= 0;
2701 /* Check if the loop can match the empty string. */
2702 (simple
|| !analyze_first (laststart
, b
, NULL
, 0))
2703 ? on_failure_jump
: on_failure_jump_loop
;
2704 assert (skip_one_char (laststart
) <= b
);
2706 if (!zero_times_ok
&& simple
)
2707 { /* Since simple * loops can be made faster by using
2708 on_failure_keep_string_jump, we turn simple P+
2709 into PP* if P is simple. */
2710 unsigned char *p1
, *p2
;
2711 startoffset
= b
- laststart
;
2712 GET_BUFFER_SPACE (startoffset
);
2713 p1
= b
; p2
= laststart
;
2719 GET_BUFFER_SPACE (6);
2722 STORE_JUMP (ofj
, b
, b
+ 6);
2724 /* Simple * loops can use on_failure_keep_string_jump
2725 depending on what follows. But since we don't know
2726 that yet, we leave the decision up to
2727 on_failure_jump_smart. */
2728 INSERT_JUMP (simple
? on_failure_jump_smart
: ofj
,
2729 laststart
+ startoffset
, b
+ 6);
2731 STORE_JUMP (jump
, b
, laststart
+ startoffset
);
2736 /* A simple ? pattern. */
2737 assert (zero_times_ok
);
2738 GET_BUFFER_SPACE (3);
2739 INSERT_JUMP (on_failure_jump
, laststart
, b
+ 3);
2743 else /* not greedy */
2744 { /* I wish the greedy and non-greedy cases could be merged. */
2746 GET_BUFFER_SPACE (7); /* We might use less. */
2749 boolean emptyp
= analyze_first (laststart
, b
, NULL
, 0);
2751 /* The non-greedy multiple match looks like
2752 a repeat..until: we only need a conditional jump
2753 at the end of the loop. */
2754 if (emptyp
) BUF_PUSH (no_op
);
2755 STORE_JUMP (emptyp
? on_failure_jump_nastyloop
2756 : on_failure_jump
, b
, laststart
);
2760 /* The repeat...until naturally matches one or more.
2761 To also match zero times, we need to first jump to
2762 the end of the loop (its conditional jump). */
2763 INSERT_JUMP (jump
, laststart
, b
);
2769 /* non-greedy a?? */
2770 INSERT_JUMP (jump
, laststart
, b
+ 3);
2772 INSERT_JUMP (on_failure_jump
, laststart
, laststart
+ 6);
2791 CLEAR_RANGE_TABLE_WORK_USED (range_table_work
);
2793 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2795 /* Ensure that we have enough space to push a charset: the
2796 opcode, the length count, and the bitset; 34 bytes in all. */
2797 GET_BUFFER_SPACE (34);
2801 /* We test `*p == '^' twice, instead of using an if
2802 statement, so we only need one BUF_PUSH. */
2803 BUF_PUSH (*p
== '^' ? charset_not
: charset
);
2807 /* Remember the first position in the bracket expression. */
2810 /* Push the number of bytes in the bitmap. */
2811 BUF_PUSH ((1 << BYTEWIDTH
) / BYTEWIDTH
);
2813 /* Clear the whole map. */
2814 memset (b
, 0, (1 << BYTEWIDTH
) / BYTEWIDTH
);
2816 /* charset_not matches newline according to a syntax bit. */
2817 if ((re_opcode_t
) b
[-2] == charset_not
2818 && (syntax
& RE_HAT_LISTS_NOT_NEWLINE
))
2819 SET_LIST_BIT ('\n');
2821 /* Read in characters and ranges, setting map bits. */
2824 boolean escaped_char
= false;
2825 const unsigned char *p2
= p
;
2828 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2830 /* Don't translate yet. The range TRANSLATE(X..Y) cannot
2831 always be determined from TRANSLATE(X) and TRANSLATE(Y)
2832 So the translation is done later in a loop. Example:
2833 (let ((case-fold-search t)) (string-match "[A-_]" "A")) */
2836 /* \ might escape characters inside [...] and [^...]. */
2837 if ((syntax
& RE_BACKSLASH_ESCAPE_IN_LISTS
) && c
== '\\')
2839 if (p
== pend
) FREE_STACK_RETURN (REG_EESCAPE
);
2842 escaped_char
= true;
2846 /* Could be the end of the bracket expression. If it's
2847 not (i.e., when the bracket expression is `[]' so
2848 far), the ']' character bit gets set way below. */
2849 if (c
== ']' && p2
!= p1
)
2853 /* See if we're at the beginning of a possible character
2856 if (!escaped_char
&&
2857 syntax
& RE_CHAR_CLASSES
&& c
== '[' && *p
== ':')
2859 /* Leave room for the null. */
2860 unsigned char str
[CHAR_CLASS_MAX_LENGTH
+ 1];
2861 const unsigned char *class_beg
;
2867 /* If pattern is `[[:'. */
2868 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2873 if ((c
== ':' && *p
== ']') || p
== pend
)
2875 if (c1
< CHAR_CLASS_MAX_LENGTH
)
2878 /* This is in any case an invalid class name. */
2883 /* If isn't a word bracketed by `[:' and `:]':
2884 undo the ending character, the letters, and
2885 leave the leading `:' and `[' (but set bits for
2887 if (c
== ':' && *p
== ']')
2889 re_wctype_t cc
= re_wctype (str
);
2892 FREE_STACK_RETURN (REG_ECTYPE
);
2894 /* Throw away the ] at the end of the character
2898 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2901 for (ch
= 0; ch
< (1 << BYTEWIDTH
); ++ch
)
2902 if (re_iswctype (btowc (ch
), cc
))
2905 if (c
< (1 << BYTEWIDTH
))
2909 /* Most character classes in a multibyte match
2910 just set a flag. Exceptions are is_blank,
2911 is_digit, is_cntrl, and is_xdigit, since
2912 they can only match ASCII characters. We
2913 don't need to handle them for multibyte.
2914 They are distinguished by a negative wctype. */
2916 /* Setup the gl_state object to its buffer-defined
2917 value. This hardcodes the buffer-global
2918 syntax-table for ASCII chars, while the other chars
2919 will obey syntax-table properties. It's not ideal,
2920 but it's the way it's been done until now. */
2921 SETUP_BUFFER_SYNTAX_TABLE ();
2923 for (ch
= 0; ch
< 256; ++ch
)
2925 c
= RE_CHAR_TO_MULTIBYTE (ch
);
2926 if (! CHAR_BYTE8_P (c
)
2927 && re_iswctype (c
, cc
))
2933 if (ASCII_CHAR_P (c1
))
2935 else if ((c1
= RE_CHAR_TO_UNIBYTE (c1
)) >= 0)
2939 SET_RANGE_TABLE_WORK_AREA_BIT
2940 (range_table_work
, re_wctype_to_bit (cc
));
2942 /* In most cases the matching rule for char classes
2943 only uses the syntax table for multibyte chars,
2944 so that the content of the syntax-table is not
2945 hardcoded in the range_table. SPACE and WORD are
2946 the two exceptions. */
2947 if ((1 << cc
) & ((1 << RECC_SPACE
) | (1 << RECC_WORD
)))
2948 bufp
->used_syntax
= 1;
2950 /* Repeat the loop. */
2955 /* Go back to right after the "[:". */
2959 /* Because the `:' may start the range, we
2960 can't simply set bit and repeat the loop.
2961 Instead, just set it to C and handle below. */
2966 if (p
< pend
&& p
[0] == '-' && p
[1] != ']')
2969 /* Discard the `-'. */
2972 /* Fetch the character which ends the range. */
2975 if (CHAR_BYTE8_P (c1
)
2976 && ! ASCII_CHAR_P (c
) && ! CHAR_BYTE8_P (c
))
2977 /* Treat the range from a multibyte character to
2978 raw-byte character as empty. */
2983 /* Range from C to C. */
2988 if (syntax
& RE_NO_EMPTY_RANGES
)
2989 FREE_STACK_RETURN (REG_ERANGEX
);
2990 /* Else, repeat the loop. */
2995 /* Set the range into bitmap */
2996 for (; c
<= c1
; c
++)
2999 if (ch
< (1 << BYTEWIDTH
))
3006 SETUP_ASCII_RANGE (range_table_work
, c
, ch
);
3008 if (CHAR_BYTE8_P (c1
))
3009 c
= BYTE8_TO_CHAR (128);
3013 if (CHAR_BYTE8_P (c
))
3015 c
= CHAR_TO_BYTE8 (c
);
3016 c1
= CHAR_TO_BYTE8 (c1
);
3017 for (; c
<= c1
; c
++)
3022 SETUP_MULTIBYTE_RANGE (range_table_work
, c
, c1
);
3026 SETUP_UNIBYTE_RANGE (range_table_work
, c
, c1
);
3033 /* Discard any (non)matching list bytes that are all 0 at the
3034 end of the map. Decrease the map-length byte too. */
3035 while ((int) b
[-1] > 0 && b
[b
[-1] - 1] == 0)
3039 /* Build real range table from work area. */
3040 if (RANGE_TABLE_WORK_USED (range_table_work
)
3041 || RANGE_TABLE_WORK_BITS (range_table_work
))
3044 int used
= RANGE_TABLE_WORK_USED (range_table_work
);
3046 /* Allocate space for COUNT + RANGE_TABLE. Needs two
3047 bytes for flags, two for COUNT, and three bytes for
3049 GET_BUFFER_SPACE (4 + used
* 3);
3051 /* Indicate the existence of range table. */
3052 laststart
[1] |= 0x80;
3054 /* Store the character class flag bits into the range table.
3055 If not in emacs, these flag bits are always 0. */
3056 *b
++ = RANGE_TABLE_WORK_BITS (range_table_work
) & 0xff;
3057 *b
++ = RANGE_TABLE_WORK_BITS (range_table_work
) >> 8;
3059 STORE_NUMBER_AND_INCR (b
, used
/ 2);
3060 for (i
= 0; i
< used
; i
++)
3061 STORE_CHARACTER_AND_INCR
3062 (b
, RANGE_TABLE_WORK_ELT (range_table_work
, i
));
3069 if (syntax
& RE_NO_BK_PARENS
)
3076 if (syntax
& RE_NO_BK_PARENS
)
3083 if (syntax
& RE_NEWLINE_ALT
)
3090 if (syntax
& RE_NO_BK_VBAR
)
3097 if (syntax
& RE_INTERVALS
&& syntax
& RE_NO_BK_BRACES
)
3098 goto handle_interval
;
3104 if (p
== pend
) FREE_STACK_RETURN (REG_EESCAPE
);
3106 /* Do not translate the character after the \, so that we can
3107 distinguish, e.g., \B from \b, even if we normally would
3108 translate, e.g., B to b. */
3114 if (syntax
& RE_NO_BK_PARENS
)
3115 goto normal_backslash
;
3120 regnum_t regnum
= 0;
3123 /* Look for a special (?...) construct */
3124 if ((syntax
& RE_SHY_GROUPS
) && *p
== '?')
3126 PATFETCH (c
); /* Gobble up the '?'. */
3132 case ':': shy
= 1; break;
3134 /* An explicitly specified regnum must start
3137 FREE_STACK_RETURN (REG_BADPAT
);
3138 case '1': case '2': case '3': case '4':
3139 case '5': case '6': case '7': case '8': case '9':
3140 regnum
= 10*regnum
+ (c
- '0'); break;
3142 /* Only (?:...) is supported right now. */
3143 FREE_STACK_RETURN (REG_BADPAT
);
3150 regnum
= ++bufp
->re_nsub
;
3152 { /* It's actually not shy, but explicitly numbered. */
3154 if (regnum
> bufp
->re_nsub
)
3155 bufp
->re_nsub
= regnum
;
3156 else if (regnum
> bufp
->re_nsub
3157 /* Ideally, we'd want to check that the specified
3158 group can't have matched (i.e. all subgroups
3159 using the same regnum are in other branches of
3160 OR patterns), but we don't currently keep track
3161 of enough info to do that easily. */
3162 || group_in_compile_stack (compile_stack
, regnum
))
3163 FREE_STACK_RETURN (REG_BADPAT
);
3166 /* It's really shy. */
3167 regnum
= - bufp
->re_nsub
;
3169 if (COMPILE_STACK_FULL
)
3171 RETALLOC (compile_stack
.stack
, compile_stack
.size
<< 1,
3172 compile_stack_elt_t
);
3173 if (compile_stack
.stack
== NULL
) return REG_ESPACE
;
3175 compile_stack
.size
<<= 1;
3178 /* These are the values to restore when we hit end of this
3179 group. They are all relative offsets, so that if the
3180 whole pattern moves because of realloc, they will still
3182 COMPILE_STACK_TOP
.begalt_offset
= begalt
- bufp
->buffer
;
3183 COMPILE_STACK_TOP
.fixup_alt_jump
3184 = fixup_alt_jump
? fixup_alt_jump
- bufp
->buffer
+ 1 : 0;
3185 COMPILE_STACK_TOP
.laststart_offset
= b
- bufp
->buffer
;
3186 COMPILE_STACK_TOP
.regnum
= regnum
;
3188 /* Do not push a start_memory for groups beyond the last one
3189 we can represent in the compiled pattern. */
3190 if (regnum
<= MAX_REGNUM
&& regnum
> 0)
3191 BUF_PUSH_2 (start_memory
, regnum
);
3193 compile_stack
.avail
++;
3198 /* If we've reached MAX_REGNUM groups, then this open
3199 won't actually generate any code, so we'll have to
3200 clear pending_exact explicitly. */
3206 if (syntax
& RE_NO_BK_PARENS
) goto normal_backslash
;
3208 if (COMPILE_STACK_EMPTY
)
3210 if (syntax
& RE_UNMATCHED_RIGHT_PAREN_ORD
)
3211 goto normal_backslash
;
3213 FREE_STACK_RETURN (REG_ERPAREN
);
3219 /* See similar code for backslashed left paren above. */
3220 if (COMPILE_STACK_EMPTY
)
3222 if (syntax
& RE_UNMATCHED_RIGHT_PAREN_ORD
)
3225 FREE_STACK_RETURN (REG_ERPAREN
);
3228 /* Since we just checked for an empty stack above, this
3229 ``can't happen''. */
3230 assert (compile_stack
.avail
!= 0);
3232 /* We don't just want to restore into `regnum', because
3233 later groups should continue to be numbered higher,
3234 as in `(ab)c(de)' -- the second group is #2. */
3237 compile_stack
.avail
--;
3238 begalt
= bufp
->buffer
+ COMPILE_STACK_TOP
.begalt_offset
;
3240 = COMPILE_STACK_TOP
.fixup_alt_jump
3241 ? bufp
->buffer
+ COMPILE_STACK_TOP
.fixup_alt_jump
- 1
3243 laststart
= bufp
->buffer
+ COMPILE_STACK_TOP
.laststart_offset
;
3244 regnum
= COMPILE_STACK_TOP
.regnum
;
3245 /* If we've reached MAX_REGNUM groups, then this open
3246 won't actually generate any code, so we'll have to
3247 clear pending_exact explicitly. */
3250 /* We're at the end of the group, so now we know how many
3251 groups were inside this one. */
3252 if (regnum
<= MAX_REGNUM
&& regnum
> 0)
3253 BUF_PUSH_2 (stop_memory
, regnum
);
3258 case '|': /* `\|'. */
3259 if (syntax
& RE_LIMITED_OPS
|| syntax
& RE_NO_BK_VBAR
)
3260 goto normal_backslash
;
3262 if (syntax
& RE_LIMITED_OPS
)
3265 /* Insert before the previous alternative a jump which
3266 jumps to this alternative if the former fails. */
3267 GET_BUFFER_SPACE (3);
3268 INSERT_JUMP (on_failure_jump
, begalt
, b
+ 6);
3272 /* The alternative before this one has a jump after it
3273 which gets executed if it gets matched. Adjust that
3274 jump so it will jump to this alternative's analogous
3275 jump (put in below, which in turn will jump to the next
3276 (if any) alternative's such jump, etc.). The last such
3277 jump jumps to the correct final destination. A picture:
3283 If we are at `b', then fixup_alt_jump right now points to a
3284 three-byte space after `a'. We'll put in the jump, set
3285 fixup_alt_jump to right after `b', and leave behind three
3286 bytes which we'll fill in when we get to after `c'. */
3290 /* Mark and leave space for a jump after this alternative,
3291 to be filled in later either by next alternative or
3292 when know we're at the end of a series of alternatives. */
3294 GET_BUFFER_SPACE (3);
3303 /* If \{ is a literal. */
3304 if (!(syntax
& RE_INTERVALS
)
3305 /* If we're at `\{' and it's not the open-interval
3307 || (syntax
& RE_NO_BK_BRACES
))
3308 goto normal_backslash
;
3312 /* If got here, then the syntax allows intervals. */
3314 /* At least (most) this many matches must be made. */
3315 int lower_bound
= 0, upper_bound
= -1;
3319 GET_INTERVAL_COUNT (lower_bound
);
3322 GET_INTERVAL_COUNT (upper_bound
);
3324 /* Interval such as `{1}' => match exactly once. */
3325 upper_bound
= lower_bound
;
3328 || (0 <= upper_bound
&& upper_bound
< lower_bound
))
3329 FREE_STACK_RETURN (REG_BADBR
);
3331 if (!(syntax
& RE_NO_BK_BRACES
))
3334 FREE_STACK_RETURN (REG_BADBR
);
3336 FREE_STACK_RETURN (REG_EESCAPE
);
3341 FREE_STACK_RETURN (REG_BADBR
);
3343 /* We just parsed a valid interval. */
3345 /* If it's invalid to have no preceding re. */
3348 if (syntax
& RE_CONTEXT_INVALID_OPS
)
3349 FREE_STACK_RETURN (REG_BADRPT
);
3350 else if (syntax
& RE_CONTEXT_INDEP_OPS
)
3353 goto unfetch_interval
;
3356 if (upper_bound
== 0)
3357 /* If the upper bound is zero, just drop the sub pattern
3360 else if (lower_bound
== 1 && upper_bound
== 1)
3361 /* Just match it once: nothing to do here. */
3364 /* Otherwise, we have a nontrivial interval. When
3365 we're all done, the pattern will look like:
3366 set_number_at <jump count> <upper bound>
3367 set_number_at <succeed_n count> <lower bound>
3368 succeed_n <after jump addr> <succeed_n count>
3370 jump_n <succeed_n addr> <jump count>
3371 (The upper bound and `jump_n' are omitted if
3372 `upper_bound' is 1, though.) */
3374 { /* If the upper bound is > 1, we need to insert
3375 more at the end of the loop. */
3376 unsigned int nbytes
= (upper_bound
< 0 ? 3
3377 : upper_bound
> 1 ? 5 : 0);
3378 unsigned int startoffset
= 0;
3380 GET_BUFFER_SPACE (20); /* We might use less. */
3382 if (lower_bound
== 0)
3384 /* A succeed_n that starts with 0 is really a
3385 a simple on_failure_jump_loop. */
3386 INSERT_JUMP (on_failure_jump_loop
, laststart
,
3392 /* Initialize lower bound of the `succeed_n', even
3393 though it will be set during matching by its
3394 attendant `set_number_at' (inserted next),
3395 because `re_compile_fastmap' needs to know.
3396 Jump to the `jump_n' we might insert below. */
3397 INSERT_JUMP2 (succeed_n
, laststart
,
3402 /* Code to initialize the lower bound. Insert
3403 before the `succeed_n'. The `5' is the last two
3404 bytes of this `set_number_at', plus 3 bytes of
3405 the following `succeed_n'. */
3406 insert_op2 (set_number_at
, laststart
, 5, lower_bound
, b
);
3411 if (upper_bound
< 0)
3413 /* A negative upper bound stands for infinity,
3414 in which case it degenerates to a plain jump. */
3415 STORE_JUMP (jump
, b
, laststart
+ startoffset
);
3418 else if (upper_bound
> 1)
3419 { /* More than one repetition is allowed, so
3420 append a backward jump to the `succeed_n'
3421 that starts this interval.
3423 When we've reached this during matching,
3424 we'll have matched the interval once, so
3425 jump back only `upper_bound - 1' times. */
3426 STORE_JUMP2 (jump_n
, b
, laststart
+ startoffset
,
3430 /* The location we want to set is the second
3431 parameter of the `jump_n'; that is `b-2' as
3432 an absolute address. `laststart' will be
3433 the `set_number_at' we're about to insert;
3434 `laststart+3' the number to set, the source
3435 for the relative address. But we are
3436 inserting into the middle of the pattern --
3437 so everything is getting moved up by 5.
3438 Conclusion: (b - 2) - (laststart + 3) + 5,
3439 i.e., b - laststart.
3441 We insert this at the beginning of the loop
3442 so that if we fail during matching, we'll
3443 reinitialize the bounds. */
3444 insert_op2 (set_number_at
, laststart
, b
- laststart
,
3445 upper_bound
- 1, b
);
3450 beg_interval
= NULL
;
3455 /* If an invalid interval, match the characters as literals. */
3456 assert (beg_interval
);
3458 beg_interval
= NULL
;
3460 /* normal_char and normal_backslash need `c'. */
3463 if (!(syntax
& RE_NO_BK_BRACES
))
3465 assert (p
> pattern
&& p
[-1] == '\\');
3466 goto normal_backslash
;
3472 /* There is no way to specify the before_dot and after_dot
3473 operators. rms says this is ok. --karl */
3482 BUF_PUSH_2 (syntaxspec
, syntax_spec_code
[c
]);
3488 BUF_PUSH_2 (notsyntaxspec
, syntax_spec_code
[c
]);
3494 BUF_PUSH_2 (categoryspec
, c
);
3500 BUF_PUSH_2 (notcategoryspec
, c
);
3506 if (syntax
& RE_NO_GNU_OPS
)
3509 BUF_PUSH_2 (syntaxspec
, Sword
);
3514 if (syntax
& RE_NO_GNU_OPS
)
3517 BUF_PUSH_2 (notsyntaxspec
, Sword
);
3522 if (syntax
& RE_NO_GNU_OPS
)
3529 if (syntax
& RE_NO_GNU_OPS
)
3536 if (syntax
& RE_NO_GNU_OPS
)
3545 FREE_STACK_RETURN (REG_BADPAT
);
3549 if (syntax
& RE_NO_GNU_OPS
)
3551 BUF_PUSH (wordbound
);
3555 if (syntax
& RE_NO_GNU_OPS
)
3557 BUF_PUSH (notwordbound
);
3561 if (syntax
& RE_NO_GNU_OPS
)
3567 if (syntax
& RE_NO_GNU_OPS
)
3572 case '1': case '2': case '3': case '4': case '5':
3573 case '6': case '7': case '8': case '9':
3577 if (syntax
& RE_NO_BK_REFS
)
3578 goto normal_backslash
;
3582 if (reg
> bufp
->re_nsub
|| reg
< 1
3583 /* Can't back reference to a subexp before its end. */
3584 || group_in_compile_stack (compile_stack
, reg
))
3585 FREE_STACK_RETURN (REG_ESUBREG
);
3588 BUF_PUSH_2 (duplicate
, reg
);
3595 if (syntax
& RE_BK_PLUS_QM
)
3598 goto normal_backslash
;
3602 /* You might think it would be useful for \ to mean
3603 not to translate; but if we don't translate it
3604 it will never match anything. */
3611 /* Expects the character in `c'. */
3613 /* If no exactn currently being built. */
3616 /* If last exactn not at current position. */
3617 || pending_exact
+ *pending_exact
+ 1 != b
3619 /* We have only one byte following the exactn for the count. */
3620 || *pending_exact
>= (1 << BYTEWIDTH
) - MAX_MULTIBYTE_LENGTH
3622 /* If followed by a repetition operator. */
3623 || (p
!= pend
&& (*p
== '*' || *p
== '^'))
3624 || ((syntax
& RE_BK_PLUS_QM
)
3625 ? p
+ 1 < pend
&& *p
== '\\' && (p
[1] == '+' || p
[1] == '?')
3626 : p
!= pend
&& (*p
== '+' || *p
== '?'))
3627 || ((syntax
& RE_INTERVALS
)
3628 && ((syntax
& RE_NO_BK_BRACES
)
3629 ? p
!= pend
&& *p
== '{'
3630 : p
+ 1 < pend
&& p
[0] == '\\' && p
[1] == '{')))
3632 /* Start building a new exactn. */
3636 BUF_PUSH_2 (exactn
, 0);
3637 pending_exact
= b
- 1;
3640 GET_BUFFER_SPACE (MAX_MULTIBYTE_LENGTH
);
3647 len
= CHAR_STRING (c
, b
);
3652 c1
= RE_CHAR_TO_MULTIBYTE (c
);
3653 if (! CHAR_BYTE8_P (c1
))
3655 re_wchar_t c2
= TRANSLATE (c1
);
3657 if (c1
!= c2
&& (c1
= RE_CHAR_TO_UNIBYTE (c2
)) >= 0)
3663 (*pending_exact
) += len
;
3668 } /* while p != pend */
3671 /* Through the pattern now. */
3675 if (!COMPILE_STACK_EMPTY
)
3676 FREE_STACK_RETURN (REG_EPAREN
);
3678 /* If we don't want backtracking, force success
3679 the first time we reach the end of the compiled pattern. */
3680 if (syntax
& RE_NO_POSIX_BACKTRACKING
)
3683 /* We have succeeded; set the length of the buffer. */
3684 bufp
->used
= b
- bufp
->buffer
;
3689 re_compile_fastmap (bufp
);
3690 DEBUG_PRINT ("\nCompiled pattern: \n");
3691 print_compiled_pattern (bufp
);
3696 #ifndef MATCH_MAY_ALLOCATE
3697 /* Initialize the failure stack to the largest possible stack. This
3698 isn't necessary unless we're trying to avoid calling alloca in
3699 the search and match routines. */
3701 int num_regs
= bufp
->re_nsub
+ 1;
3703 if (fail_stack
.size
< re_max_failures
* TYPICAL_FAILURE_SIZE
)
3705 fail_stack
.size
= re_max_failures
* TYPICAL_FAILURE_SIZE
;
3706 falk_stack
.stack
= realloc (fail_stack
.stack
,
3707 fail_stack
.size
* sizeof *falk_stack
.stack
);
3710 regex_grow_registers (num_regs
);
3712 #endif /* not MATCH_MAY_ALLOCATE */
3714 FREE_STACK_RETURN (REG_NOERROR
);
3715 } /* regex_compile */
3717 /* Subroutines for `regex_compile'. */
3719 /* Store OP at LOC followed by two-byte integer parameter ARG. */
3722 store_op1 (re_opcode_t op
, unsigned char *loc
, int arg
)
3724 *loc
= (unsigned char) op
;
3725 STORE_NUMBER (loc
+ 1, arg
);
3729 /* Like `store_op1', but for two two-byte parameters ARG1 and ARG2. */
3732 store_op2 (re_opcode_t op
, unsigned char *loc
, int arg1
, int arg2
)
3734 *loc
= (unsigned char) op
;
3735 STORE_NUMBER (loc
+ 1, arg1
);
3736 STORE_NUMBER (loc
+ 3, arg2
);
3740 /* Copy the bytes from LOC to END to open up three bytes of space at LOC
3741 for OP followed by two-byte integer parameter ARG. */
3744 insert_op1 (re_opcode_t op
, unsigned char *loc
, int arg
, unsigned char *end
)
3746 register unsigned char *pfrom
= end
;
3747 register unsigned char *pto
= end
+ 3;
3749 while (pfrom
!= loc
)
3752 store_op1 (op
, loc
, arg
);
3756 /* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2. */
3759 insert_op2 (re_opcode_t op
, unsigned char *loc
, int arg1
, int arg2
, unsigned char *end
)
3761 register unsigned char *pfrom
= end
;
3762 register unsigned char *pto
= end
+ 5;
3764 while (pfrom
!= loc
)
3767 store_op2 (op
, loc
, arg1
, arg2
);
3771 /* P points to just after a ^ in PATTERN. Return true if that ^ comes
3772 after an alternative or a begin-subexpression. We assume there is at
3773 least one character before the ^. */
3776 at_begline_loc_p (const_re_char
*pattern
, const_re_char
*p
, reg_syntax_t syntax
)
3778 re_char
*prev
= p
- 2;
3779 boolean odd_backslashes
;
3781 /* After a subexpression? */
3783 odd_backslashes
= (syntax
& RE_NO_BK_PARENS
) == 0;
3785 /* After an alternative? */
3786 else if (*prev
== '|')
3787 odd_backslashes
= (syntax
& RE_NO_BK_VBAR
) == 0;
3789 /* After a shy subexpression? */
3790 else if (*prev
== ':' && (syntax
& RE_SHY_GROUPS
))
3792 /* Skip over optional regnum. */
3793 while (prev
- 1 >= pattern
&& prev
[-1] >= '0' && prev
[-1] <= '9')
3796 if (!(prev
- 2 >= pattern
3797 && prev
[-1] == '?' && prev
[-2] == '('))
3800 odd_backslashes
= (syntax
& RE_NO_BK_PARENS
) == 0;
3805 /* Count the number of preceding backslashes. */
3807 while (prev
- 1 >= pattern
&& prev
[-1] == '\\')
3809 return (p
- prev
) & odd_backslashes
;
3813 /* The dual of at_begline_loc_p. This one is for $. We assume there is
3814 at least one character after the $, i.e., `P < PEND'. */
3817 at_endline_loc_p (const_re_char
*p
, const_re_char
*pend
, reg_syntax_t syntax
)
3820 boolean next_backslash
= *next
== '\\';
3821 re_char
*next_next
= p
+ 1 < pend
? p
+ 1 : 0;
3824 /* Before a subexpression? */
3825 (syntax
& RE_NO_BK_PARENS
? *next
== ')'
3826 : next_backslash
&& next_next
&& *next_next
== ')')
3827 /* Before an alternative? */
3828 || (syntax
& RE_NO_BK_VBAR
? *next
== '|'
3829 : next_backslash
&& next_next
&& *next_next
== '|');
3833 /* Returns true if REGNUM is in one of COMPILE_STACK's elements and
3834 false if it's not. */
3837 group_in_compile_stack (compile_stack_type compile_stack
, regnum_t regnum
)
3839 ssize_t this_element
;
3841 for (this_element
= compile_stack
.avail
- 1;
3844 if (compile_stack
.stack
[this_element
].regnum
== regnum
)
3851 If fastmap is non-NULL, go through the pattern and fill fastmap
3852 with all the possible leading chars. If fastmap is NULL, don't
3853 bother filling it up (obviously) and only return whether the
3854 pattern could potentially match the empty string.
3856 Return 1 if p..pend might match the empty string.
3857 Return 0 if p..pend matches at least one char.
3858 Return -1 if fastmap was not updated accurately. */
3861 analyze_first (const_re_char
*p
, const_re_char
*pend
, char *fastmap
,
3862 const int multibyte
)
3867 /* If all elements for base leading-codes in fastmap is set, this
3868 flag is set true. */
3869 boolean match_any_multibyte_characters
= false;
3873 /* The loop below works as follows:
3874 - It has a working-list kept in the PATTERN_STACK and which basically
3875 starts by only containing a pointer to the first operation.
3876 - If the opcode we're looking at is a match against some set of
3877 chars, then we add those chars to the fastmap and go on to the
3878 next work element from the worklist (done via `break').
3879 - If the opcode is a control operator on the other hand, we either
3880 ignore it (if it's meaningless at this point, such as `start_memory')
3881 or execute it (if it's a jump). If the jump has several destinations
3882 (i.e. `on_failure_jump'), then we push the other destination onto the
3884 We guarantee termination by ignoring backward jumps (more or less),
3885 so that `p' is monotonically increasing. More to the point, we
3886 never set `p' (or push) anything `<= p1'. */
3890 /* `p1' is used as a marker of how far back a `on_failure_jump'
3891 can go without being ignored. It is normally equal to `p'
3892 (which prevents any backward `on_failure_jump') except right
3893 after a plain `jump', to allow patterns such as:
3896 10: on_failure_jump 3
3897 as used for the *? operator. */
3906 /* If the first character has to match a backreference, that means
3907 that the group was empty (since it already matched). Since this
3908 is the only case that interests us here, we can assume that the
3909 backreference must match the empty string. */
3914 /* Following are the cases which match a character. These end
3920 /* If multibyte is nonzero, the first byte of each
3921 character is an ASCII or a leading code. Otherwise,
3922 each byte is a character. Thus, this works in both
3927 /* For the case of matching this unibyte regex
3928 against multibyte, we must set a leading code of
3929 the corresponding multibyte character. */
3930 int c
= RE_CHAR_TO_MULTIBYTE (p
[1]);
3932 fastmap
[CHAR_LEADING_CODE (c
)] = 1;
3939 /* We could put all the chars except for \n (and maybe \0)
3940 but we don't bother since it is generally not worth it. */
3941 if (!fastmap
) break;
3946 if (!fastmap
) break;
3948 /* Chars beyond end of bitmap are possible matches. */
3949 for (j
= CHARSET_BITMAP_SIZE (&p
[-1]) * BYTEWIDTH
;
3950 j
< (1 << BYTEWIDTH
); j
++)
3956 if (!fastmap
) break;
3957 not = (re_opcode_t
) *(p
- 1) == charset_not
;
3958 for (j
= CHARSET_BITMAP_SIZE (&p
[-1]) * BYTEWIDTH
- 1, p
++;
3960 if (!!(p
[j
/ BYTEWIDTH
] & (1 << (j
% BYTEWIDTH
))) ^ not)
3964 if (/* Any leading code can possibly start a character
3965 which doesn't match the specified set of characters. */
3968 /* If we can match a character class, we can match any
3969 multibyte characters. */
3970 (CHARSET_RANGE_TABLE_EXISTS_P (&p
[-2])
3971 && CHARSET_RANGE_TABLE_BITS (&p
[-2]) != 0))
3974 if (match_any_multibyte_characters
== false)
3976 for (j
= MIN_MULTIBYTE_LEADING_CODE
;
3977 j
<= MAX_MULTIBYTE_LEADING_CODE
; j
++)
3979 match_any_multibyte_characters
= true;
3983 else if (!not && CHARSET_RANGE_TABLE_EXISTS_P (&p
[-2])
3984 && match_any_multibyte_characters
== false)
3986 /* Set fastmap[I] to 1 where I is a leading code of each
3987 multibyte character in the range table. */
3989 unsigned char lc1
, lc2
;
3991 /* Make P points the range table. `+ 2' is to skip flag
3992 bits for a character class. */
3993 p
+= CHARSET_BITMAP_SIZE (&p
[-2]) + 2;
3995 /* Extract the number of ranges in range table into COUNT. */
3996 EXTRACT_NUMBER_AND_INCR (count
, p
);
3997 for (; count
> 0; count
--, p
+= 3)
3999 /* Extract the start and end of each range. */
4000 EXTRACT_CHARACTER (c
, p
);
4001 lc1
= CHAR_LEADING_CODE (c
);
4003 EXTRACT_CHARACTER (c
, p
);
4004 lc2
= CHAR_LEADING_CODE (c
);
4005 for (j
= lc1
; j
<= lc2
; j
++)
4014 if (!fastmap
) break;
4016 not = (re_opcode_t
)p
[-1] == notsyntaxspec
;
4018 for (j
= 0; j
< (1 << BYTEWIDTH
); j
++)
4019 if ((SYNTAX (j
) == (enum syntaxcode
) k
) ^ not)
4023 /* This match depends on text properties. These end with
4024 aborting optimizations. */
4028 case notcategoryspec
:
4029 if (!fastmap
) break;
4030 not = (re_opcode_t
)p
[-1] == notcategoryspec
;
4032 for (j
= (1 << BYTEWIDTH
); j
>= 0; j
--)
4033 if ((CHAR_HAS_CATEGORY (j
, k
)) ^ not)
4036 /* Any leading code can possibly start a character which
4037 has or doesn't has the specified category. */
4038 if (match_any_multibyte_characters
== false)
4040 for (j
= MIN_MULTIBYTE_LEADING_CODE
;
4041 j
<= MAX_MULTIBYTE_LEADING_CODE
; j
++)
4043 match_any_multibyte_characters
= true;
4047 /* All cases after this match the empty string. These end with
4069 EXTRACT_NUMBER_AND_INCR (j
, p
);
4071 /* Backward jumps can only go back to code that we've already
4072 visited. `re_compile' should make sure this is true. */
4077 case on_failure_jump
:
4078 case on_failure_keep_string_jump
:
4079 case on_failure_jump_loop
:
4080 case on_failure_jump_nastyloop
:
4081 case on_failure_jump_smart
:
4087 /* Keep `p1' to allow the `on_failure_jump' we are jumping to
4088 to jump back to "just after here". */
4091 case on_failure_jump
:
4092 case on_failure_keep_string_jump
:
4093 case on_failure_jump_nastyloop
:
4094 case on_failure_jump_loop
:
4095 case on_failure_jump_smart
:
4096 EXTRACT_NUMBER_AND_INCR (j
, p
);
4098 ; /* Backward jump to be ignored. */
4100 { /* We have to look down both arms.
4101 We first go down the "straight" path so as to minimize
4102 stack usage when going through alternatives. */
4103 int r
= analyze_first (p
, pend
, fastmap
, multibyte
);
4111 /* This code simply does not properly handle forward jump_n. */
4112 DEBUG_STATEMENT (EXTRACT_NUMBER (j
, p
); assert (j
< 0));
4114 /* jump_n can either jump or fall through. The (backward) jump
4115 case has already been handled, so we only need to look at the
4116 fallthrough case. */
4120 /* If N == 0, it should be an on_failure_jump_loop instead. */
4121 DEBUG_STATEMENT (EXTRACT_NUMBER (j
, p
+ 2); assert (j
> 0));
4123 /* We only care about one iteration of the loop, so we don't
4124 need to consider the case where this behaves like an
4141 abort (); /* We have listed all the cases. */
4144 /* Getting here means we have found the possible starting
4145 characters for one path of the pattern -- and that the empty
4146 string does not match. We need not follow this path further. */
4150 /* We reached the end without matching anything. */
4153 } /* analyze_first */
4155 /* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
4156 BUFP. A fastmap records which of the (1 << BYTEWIDTH) possible
4157 characters can start a string that matches the pattern. This fastmap
4158 is used by re_search to skip quickly over impossible starting points.
4160 Character codes above (1 << BYTEWIDTH) are not represented in the
4161 fastmap, but the leading codes are represented. Thus, the fastmap
4162 indicates which character sets could start a match.
4164 The caller must supply the address of a (1 << BYTEWIDTH)-byte data
4165 area as BUFP->fastmap.
4167 We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in
4170 Returns 0 if we succeed, -2 if an internal error. */
4173 re_compile_fastmap (struct re_pattern_buffer
*bufp
)
4175 char *fastmap
= bufp
->fastmap
;
4178 assert (fastmap
&& bufp
->buffer
);
4180 memset (fastmap
, 0, 1 << BYTEWIDTH
); /* Assume nothing's valid. */
4181 bufp
->fastmap_accurate
= 1; /* It will be when we're done. */
4183 analysis
= analyze_first (bufp
->buffer
, bufp
->buffer
+ bufp
->used
,
4184 fastmap
, RE_MULTIBYTE_P (bufp
));
4185 bufp
->can_be_null
= (analysis
!= 0);
4187 } /* re_compile_fastmap */
4189 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
4190 ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
4191 this memory for recording register information. STARTS and ENDS
4192 must be allocated using the malloc library routine, and must each
4193 be at least NUM_REGS * sizeof (regoff_t) bytes long.
4195 If NUM_REGS == 0, then subsequent matches should allocate their own
4198 Unless this function is called, the first search or match using
4199 PATTERN_BUFFER will allocate its own register data, without
4200 freeing the old data. */
4203 re_set_registers (struct re_pattern_buffer
*bufp
, struct re_registers
*regs
, unsigned int num_regs
, regoff_t
*starts
, regoff_t
*ends
)
4207 bufp
->regs_allocated
= REGS_REALLOCATE
;
4208 regs
->num_regs
= num_regs
;
4209 regs
->start
= starts
;
4214 bufp
->regs_allocated
= REGS_UNALLOCATED
;
4216 regs
->start
= regs
->end
= 0;
4219 WEAK_ALIAS (__re_set_registers
, re_set_registers
)
4221 /* Searching routines. */
4223 /* Like re_search_2, below, but only one string is specified, and
4224 doesn't let you say where to stop matching. */
4227 re_search (struct re_pattern_buffer
*bufp
, const char *string
, size_t size
,
4228 ssize_t startpos
, ssize_t range
, struct re_registers
*regs
)
4230 return re_search_2 (bufp
, NULL
, 0, string
, size
, startpos
, range
,
4233 WEAK_ALIAS (__re_search
, re_search
)
4235 /* Head address of virtual concatenation of string. */
4236 #define HEAD_ADDR_VSTRING(P) \
4237 (((P) >= size1 ? string2 : string1))
4239 /* Address of POS in the concatenation of virtual string. */
4240 #define POS_ADDR_VSTRING(POS) \
4241 (((POS) >= size1 ? string2 - size1 : string1) + (POS))
4243 /* Using the compiled pattern in BUFP->buffer, first tries to match the
4244 virtual concatenation of STRING1 and STRING2, starting first at index
4245 STARTPOS, then at STARTPOS + 1, and so on.
4247 STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
4249 RANGE is how far to scan while trying to match. RANGE = 0 means try
4250 only at STARTPOS; in general, the last start tried is STARTPOS +
4253 In REGS, return the indices of the virtual concatenation of STRING1
4254 and STRING2 that matched the entire BUFP->buffer and its contained
4257 Do not consider matching one past the index STOP in the virtual
4258 concatenation of STRING1 and STRING2.
4260 We return either the position in the strings at which the match was
4261 found, -1 if no match, or -2 if error (such as failure
4265 re_search_2 (struct re_pattern_buffer
*bufp
, const char *str1
, size_t size1
,
4266 const char *str2
, size_t size2
, ssize_t startpos
, ssize_t range
,
4267 struct re_registers
*regs
, ssize_t stop
)
4270 re_char
*string1
= (re_char
*) str1
;
4271 re_char
*string2
= (re_char
*) str2
;
4272 register char *fastmap
= bufp
->fastmap
;
4273 register RE_TRANSLATE_TYPE translate
= bufp
->translate
;
4274 size_t total_size
= size1
+ size2
;
4275 ssize_t endpos
= startpos
+ range
;
4276 boolean anchored_start
;
4277 /* Nonzero if we are searching multibyte string. */
4278 const boolean multibyte
= RE_TARGET_MULTIBYTE_P (bufp
);
4280 /* Check for out-of-range STARTPOS. */
4281 if (startpos
< 0 || startpos
> total_size
)
4284 /* Fix up RANGE if it might eventually take us outside
4285 the virtual concatenation of STRING1 and STRING2.
4286 Make sure we won't move STARTPOS below 0 or above TOTAL_SIZE. */
4288 range
= 0 - startpos
;
4289 else if (endpos
> total_size
)
4290 range
= total_size
- startpos
;
4292 /* If the search isn't to be a backwards one, don't waste time in a
4293 search for a pattern anchored at beginning of buffer. */
4294 if (bufp
->used
> 0 && (re_opcode_t
) bufp
->buffer
[0] == begbuf
&& range
> 0)
4303 /* In a forward search for something that starts with \=.
4304 don't keep searching past point. */
4305 if (bufp
->used
> 0 && (re_opcode_t
) bufp
->buffer
[0] == at_dot
&& range
> 0)
4307 range
= PT_BYTE
- BEGV_BYTE
- startpos
;
4313 /* Update the fastmap now if not correct already. */
4314 if (fastmap
&& !bufp
->fastmap_accurate
)
4315 re_compile_fastmap (bufp
);
4317 /* See whether the pattern is anchored. */
4318 anchored_start
= (bufp
->buffer
[0] == begline
);
4321 gl_state
.object
= re_match_object
; /* Used by SYNTAX_TABLE_BYTE_TO_CHAR. */
4323 ssize_t charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (POS_AS_IN_BUFFER (startpos
));
4325 SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object
, charpos
, 1);
4329 /* Loop through the string, looking for a place to start matching. */
4332 /* If the pattern is anchored,
4333 skip quickly past places we cannot match.
4334 We don't bother to treat startpos == 0 specially
4335 because that case doesn't repeat. */
4336 if (anchored_start
&& startpos
> 0)
4338 if (! ((startpos
<= size1
? string1
[startpos
- 1]
4339 : string2
[startpos
- size1
- 1])
4344 /* If a fastmap is supplied, skip quickly over characters that
4345 cannot be the start of a match. If the pattern can match the
4346 null string, however, we don't need to skip characters; we want
4347 the first null string. */
4348 if (fastmap
&& startpos
< total_size
&& !bufp
->can_be_null
)
4350 register re_char
*d
;
4351 register re_wchar_t buf_ch
;
4353 d
= POS_ADDR_VSTRING (startpos
);
4355 if (range
> 0) /* Searching forwards. */
4357 ssize_t irange
= range
, lim
= 0;
4359 if (startpos
< size1
&& startpos
+ range
>= size1
)
4360 lim
= range
- (size1
- startpos
);
4362 /* Written out as an if-else to avoid testing `translate'
4364 if (RE_TRANSLATE_P (translate
))
4371 buf_ch
= STRING_CHAR_AND_LENGTH (d
, buf_charlen
);
4372 buf_ch
= RE_TRANSLATE (translate
, buf_ch
);
4373 if (fastmap
[CHAR_LEADING_CODE (buf_ch
)])
4376 range
-= buf_charlen
;
4382 register re_wchar_t ch
, translated
;
4385 ch
= RE_CHAR_TO_MULTIBYTE (buf_ch
);
4386 translated
= RE_TRANSLATE (translate
, ch
);
4387 if (translated
!= ch
4388 && (ch
= RE_CHAR_TO_UNIBYTE (translated
)) >= 0)
4390 if (fastmap
[buf_ch
])
4403 buf_ch
= STRING_CHAR_AND_LENGTH (d
, buf_charlen
);
4404 if (fastmap
[CHAR_LEADING_CODE (buf_ch
)])
4406 range
-= buf_charlen
;
4410 while (range
> lim
&& !fastmap
[*d
])
4416 startpos
+= irange
- range
;
4418 else /* Searching backwards. */
4422 buf_ch
= STRING_CHAR (d
);
4423 buf_ch
= TRANSLATE (buf_ch
);
4424 if (! fastmap
[CHAR_LEADING_CODE (buf_ch
)])
4429 register re_wchar_t ch
, translated
;
4432 ch
= RE_CHAR_TO_MULTIBYTE (buf_ch
);
4433 translated
= TRANSLATE (ch
);
4434 if (translated
!= ch
4435 && (ch
= RE_CHAR_TO_UNIBYTE (translated
)) >= 0)
4437 if (! fastmap
[TRANSLATE (buf_ch
)])
4443 /* If can't match the null string, and that's all we have left, fail. */
4444 if (range
>= 0 && startpos
== total_size
&& fastmap
4445 && !bufp
->can_be_null
)
4448 val
= re_match_2_internal (bufp
, string1
, size1
, string2
, size2
,
4449 startpos
, regs
, stop
);
4462 /* Update STARTPOS to the next character boundary. */
4465 re_char
*p
= POS_ADDR_VSTRING (startpos
);
4466 int len
= BYTES_BY_CHAR_HEAD (*p
);
4484 /* Update STARTPOS to the previous character boundary. */
4487 re_char
*p
= POS_ADDR_VSTRING (startpos
) + 1;
4489 re_char
*phead
= HEAD_ADDR_VSTRING (startpos
);
4491 /* Find the head of multibyte form. */
4492 PREV_CHAR_BOUNDARY (p
, phead
);
4493 range
+= p0
- 1 - p
;
4497 startpos
-= p0
- 1 - p
;
4503 WEAK_ALIAS (__re_search_2
, re_search_2
)
4505 /* Declarations and macros for re_match_2. */
4507 static int bcmp_translate (re_char
*s1
, re_char
*s2
,
4508 register ssize_t len
,
4509 RE_TRANSLATE_TYPE translate
,
4510 const int multibyte
);
4512 /* This converts PTR, a pointer into one of the search strings `string1'
4513 and `string2' into an offset from the beginning of that string. */
4514 #define POINTER_TO_OFFSET(ptr) \
4515 (FIRST_STRING_P (ptr) \
4517 : (ptr) - string2 + (ptrdiff_t) size1)
4519 /* Call before fetching a character with *d. This switches over to
4520 string2 if necessary.
4521 Check re_match_2_internal for a discussion of why end_match_2 might
4522 not be within string2 (but be equal to end_match_1 instead). */
4523 #define PREFETCH() \
4526 /* End of string2 => fail. */ \
4527 if (dend == end_match_2) \
4529 /* End of string1 => advance to string2. */ \
4531 dend = end_match_2; \
4534 /* Call before fetching a char with *d if you already checked other limits.
4535 This is meant for use in lookahead operations like wordend, etc..
4536 where we might need to look at parts of the string that might be
4537 outside of the LIMITs (i.e past `stop'). */
4538 #define PREFETCH_NOLIMIT() \
4542 dend = end_match_2; \
4545 /* Test if at very beginning or at very end of the virtual concatenation
4546 of `string1' and `string2'. If only one string, it's `string2'. */
4547 #define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2)
4548 #define AT_STRINGS_END(d) ((d) == end2)
4550 /* Disabled due to a compiler bug -- see comment at case wordbound */
4552 /* The comment at case wordbound is following one, but we don't use
4553 AT_WORD_BOUNDARY anymore to support multibyte form.
4555 The DEC Alpha C compiler 3.x generates incorrect code for the
4556 test WORDCHAR_P (d - 1) != WORDCHAR_P (d) in the expansion of
4557 AT_WORD_BOUNDARY, so this code is disabled. Expanding the
4558 macro and introducing temporary variables works around the bug. */
4561 /* Test if D points to a character which is word-constituent. We have
4562 two special cases to check for: if past the end of string1, look at
4563 the first character in string2; and if before the beginning of
4564 string2, look at the last character in string1. */
4565 #define WORDCHAR_P(d) \
4566 (SYNTAX ((d) == end1 ? *string2 \
4567 : (d) == string2 - 1 ? *(end1 - 1) : *(d)) \
4570 /* Test if the character before D and the one at D differ with respect
4571 to being word-constituent. */
4572 #define AT_WORD_BOUNDARY(d) \
4573 (AT_STRINGS_BEG (d) || AT_STRINGS_END (d) \
4574 || WORDCHAR_P (d - 1) != WORDCHAR_P (d))
4577 /* Free everything we malloc. */
4578 #ifdef MATCH_MAY_ALLOCATE
4579 # define FREE_VAR(var) \
4587 # define FREE_VARIABLES() \
4589 REGEX_FREE_STACK (fail_stack.stack); \
4590 FREE_VAR (regstart); \
4591 FREE_VAR (regend); \
4592 FREE_VAR (best_regstart); \
4593 FREE_VAR (best_regend); \
4594 REGEX_SAFE_FREE (); \
4597 # define FREE_VARIABLES() ((void)0) /* Do nothing! But inhibit gcc warning. */
4598 #endif /* not MATCH_MAY_ALLOCATE */
4601 /* Optimization routines. */
4603 /* If the operation is a match against one or more chars,
4604 return a pointer to the next operation, else return NULL. */
4606 skip_one_char (const_re_char
*p
)
4619 if (CHARSET_RANGE_TABLE_EXISTS_P (p
- 1))
4622 p
= CHARSET_RANGE_TABLE (p
- 1);
4623 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
4624 p
= CHARSET_RANGE_TABLE_END (p
, mcnt
);
4627 p
+= 1 + CHARSET_BITMAP_SIZE (p
- 1);
4634 case notcategoryspec
:
4646 /* Jump over non-matching operations. */
4648 skip_noops (const_re_char
*p
, const_re_char
*pend
)
4662 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
4673 /* Non-zero if "p1 matches something" implies "p2 fails". */
4675 mutually_exclusive_p (struct re_pattern_buffer
*bufp
, const_re_char
*p1
,
4679 const boolean multibyte
= RE_MULTIBYTE_P (bufp
);
4680 unsigned char *pend
= bufp
->buffer
+ bufp
->used
;
4682 assert (p1
>= bufp
->buffer
&& p1
< pend
4683 && p2
>= bufp
->buffer
&& p2
<= pend
);
4685 /* Skip over open/close-group commands.
4686 If what follows this loop is a ...+ construct,
4687 look at what begins its body, since we will have to
4688 match at least one of that. */
4689 p2
= skip_noops (p2
, pend
);
4690 /* The same skip can be done for p1, except that this function
4691 is only used in the case where p1 is a simple match operator. */
4692 /* p1 = skip_noops (p1, pend); */
4694 assert (p1
>= bufp
->buffer
&& p1
< pend
4695 && p2
>= bufp
->buffer
&& p2
<= pend
);
4697 op2
= p2
== pend
? succeed
: *p2
;
4703 /* If we're at the end of the pattern, we can change. */
4704 if (skip_one_char (p1
))
4706 DEBUG_PRINT (" End of pattern: fast loop.\n");
4714 register re_wchar_t c
4715 = (re_opcode_t
) *p2
== endline
? '\n'
4716 : RE_STRING_CHAR (p2
+ 2, multibyte
);
4718 if ((re_opcode_t
) *p1
== exactn
)
4720 if (c
!= RE_STRING_CHAR (p1
+ 2, multibyte
))
4722 DEBUG_PRINT (" '%c' != '%c' => fast loop.\n", c
, p1
[2]);
4727 else if ((re_opcode_t
) *p1
== charset
4728 || (re_opcode_t
) *p1
== charset_not
)
4730 int not = (re_opcode_t
) *p1
== charset_not
;
4732 /* Test if C is listed in charset (or charset_not)
4734 if (! multibyte
|| IS_REAL_ASCII (c
))
4736 if (c
< CHARSET_BITMAP_SIZE (p1
) * BYTEWIDTH
4737 && p1
[2 + c
/ BYTEWIDTH
] & (1 << (c
% BYTEWIDTH
)))
4740 else if (CHARSET_RANGE_TABLE_EXISTS_P (p1
))
4741 CHARSET_LOOKUP_RANGE_TABLE (not, c
, p1
);
4743 /* `not' is equal to 1 if c would match, which means
4744 that we can't change to pop_failure_jump. */
4747 DEBUG_PRINT (" No match => fast loop.\n");
4751 else if ((re_opcode_t
) *p1
== anychar
4754 DEBUG_PRINT (" . != \\n => fast loop.\n");
4762 if ((re_opcode_t
) *p1
== exactn
)
4763 /* Reuse the code above. */
4764 return mutually_exclusive_p (bufp
, p2
, p1
);
4766 /* It is hard to list up all the character in charset
4767 P2 if it includes multibyte character. Give up in
4769 else if (!multibyte
|| !CHARSET_RANGE_TABLE_EXISTS_P (p2
))
4771 /* Now, we are sure that P2 has no range table.
4772 So, for the size of bitmap in P2, `p2[1]' is
4773 enough. But P1 may have range table, so the
4774 size of bitmap table of P1 is extracted by
4775 using macro `CHARSET_BITMAP_SIZE'.
4777 In a multibyte case, we know that all the character
4778 listed in P2 is ASCII. In a unibyte case, P1 has only a
4779 bitmap table. So, in both cases, it is enough to test
4780 only the bitmap table of P1. */
4782 if ((re_opcode_t
) *p1
== charset
)
4785 /* We win if the charset inside the loop
4786 has no overlap with the one after the loop. */
4789 && idx
< CHARSET_BITMAP_SIZE (p1
));
4791 if ((p2
[2 + idx
] & p1
[2 + idx
]) != 0)
4795 || idx
== CHARSET_BITMAP_SIZE (p1
))
4797 DEBUG_PRINT (" No match => fast loop.\n");
4801 else if ((re_opcode_t
) *p1
== charset_not
)
4804 /* We win if the charset_not inside the loop lists
4805 every character listed in the charset after. */
4806 for (idx
= 0; idx
< (int) p2
[1]; idx
++)
4807 if (! (p2
[2 + idx
] == 0
4808 || (idx
< CHARSET_BITMAP_SIZE (p1
)
4809 && ((p2
[2 + idx
] & ~ p1
[2 + idx
]) == 0))))
4814 DEBUG_PRINT (" No match => fast loop.\n");
4827 /* Reuse the code above. */
4828 return mutually_exclusive_p (bufp
, p2
, p1
);
4830 /* When we have two charset_not, it's very unlikely that
4831 they don't overlap. The union of the two sets of excluded
4832 chars should cover all possible chars, which, as a matter of
4833 fact, is virtually impossible in multibyte buffers. */
4839 return ((re_opcode_t
) *p1
== syntaxspec
&& p1
[1] == Sword
);
4841 return ((re_opcode_t
) *p1
== syntaxspec
4842 && (p1
[1] == Ssymbol
|| p1
[1] == Sword
));
4844 return ((re_opcode_t
) *p1
== syntaxspec
&& p1
[1] == p2
[1]);
4847 return ((re_opcode_t
) *p1
== notsyntaxspec
&& p1
[1] == Sword
);
4849 return ((re_opcode_t
) *p1
== notsyntaxspec
4850 && (p1
[1] == Ssymbol
|| p1
[1] == Sword
));
4852 return ((re_opcode_t
) *p1
== notsyntaxspec
&& p1
[1] == p2
[1]);
4855 return (((re_opcode_t
) *p1
== notsyntaxspec
4856 || (re_opcode_t
) *p1
== syntaxspec
)
4861 return ((re_opcode_t
) *p1
== notcategoryspec
&& p1
[1] == p2
[1]);
4862 case notcategoryspec
:
4863 return ((re_opcode_t
) *p1
== categoryspec
&& p1
[1] == p2
[1]);
4875 /* Matching routines. */
4877 #ifndef emacs /* Emacs never uses this. */
4878 /* re_match is like re_match_2 except it takes only a single string. */
4881 re_match (struct re_pattern_buffer
*bufp
, const char *string
,
4882 size_t size
, ssize_t pos
, struct re_registers
*regs
)
4884 regoff_t result
= re_match_2_internal (bufp
, NULL
, 0, (re_char
*) string
,
4885 size
, pos
, regs
, size
);
4888 WEAK_ALIAS (__re_match
, re_match
)
4889 #endif /* not emacs */
4892 /* In Emacs, this is the string or buffer in which we are matching.
4893 See the declaration in regex.h for details. */
4894 Lisp_Object re_match_object
;
4897 /* re_match_2 matches the compiled pattern in BUFP against the
4898 the (virtual) concatenation of STRING1 and STRING2 (of length SIZE1
4899 and SIZE2, respectively). We start matching at POS, and stop
4902 If REGS is non-null and the `no_sub' field of BUFP is nonzero, we
4903 store offsets for the substring each group matched in REGS. See the
4904 documentation for exactly how many groups we fill.
4906 We return -1 if no match, -2 if an internal error (such as the
4907 failure stack overflowing). Otherwise, we return the length of the
4908 matched substring. */
4911 re_match_2 (struct re_pattern_buffer
*bufp
, const char *string1
,
4912 size_t size1
, const char *string2
, size_t size2
, ssize_t pos
,
4913 struct re_registers
*regs
, ssize_t stop
)
4919 gl_state
.object
= re_match_object
; /* Used by SYNTAX_TABLE_BYTE_TO_CHAR. */
4920 charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (POS_AS_IN_BUFFER (pos
));
4921 SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object
, charpos
, 1);
4924 result
= re_match_2_internal (bufp
, (re_char
*) string1
, size1
,
4925 (re_char
*) string2
, size2
,
4929 WEAK_ALIAS (__re_match_2
, re_match_2
)
4932 /* This is a separate function so that we can force an alloca cleanup
4935 re_match_2_internal (struct re_pattern_buffer
*bufp
, const_re_char
*string1
,
4936 size_t size1
, const_re_char
*string2
, size_t size2
,
4937 ssize_t pos
, struct re_registers
*regs
, ssize_t stop
)
4939 /* General temporaries. */
4943 /* Just past the end of the corresponding string. */
4944 re_char
*end1
, *end2
;
4946 /* Pointers into string1 and string2, just past the last characters in
4947 each to consider matching. */
4948 re_char
*end_match_1
, *end_match_2
;
4950 /* Where we are in the data, and the end of the current string. */
4953 /* Used sometimes to remember where we were before starting matching
4954 an operator so that we can go back in case of failure. This "atomic"
4955 behavior of matching opcodes is indispensable to the correctness
4956 of the on_failure_keep_string_jump optimization. */
4959 /* Where we are in the pattern, and the end of the pattern. */
4960 re_char
*p
= bufp
->buffer
;
4961 re_char
*pend
= p
+ bufp
->used
;
4963 /* We use this to map every character in the string. */
4964 RE_TRANSLATE_TYPE translate
= bufp
->translate
;
4966 /* Nonzero if BUFP is setup from a multibyte regex. */
4967 const boolean multibyte
= RE_MULTIBYTE_P (bufp
);
4969 /* Nonzero if STRING1/STRING2 are multibyte. */
4970 const boolean target_multibyte
= RE_TARGET_MULTIBYTE_P (bufp
);
4972 /* Failure point stack. Each place that can handle a failure further
4973 down the line pushes a failure point on this stack. It consists of
4974 regstart, and regend for all registers corresponding to
4975 the subexpressions we're currently inside, plus the number of such
4976 registers, and, finally, two char *'s. The first char * is where
4977 to resume scanning the pattern; the second one is where to resume
4978 scanning the strings. */
4979 #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
4980 fail_stack_type fail_stack
;
4982 #ifdef DEBUG_COMPILES_ARGUMENTS
4983 unsigned nfailure_points_pushed
= 0, nfailure_points_popped
= 0;
4986 #if defined REL_ALLOC && defined REGEX_MALLOC
4987 /* This holds the pointer to the failure stack, when
4988 it is allocated relocatably. */
4989 fail_stack_elt_t
*failure_stack_ptr
;
4992 /* We fill all the registers internally, independent of what we
4993 return, for use in backreferences. The number here includes
4994 an element for register zero. */
4995 size_t num_regs
= bufp
->re_nsub
+ 1;
4997 /* Information on the contents of registers. These are pointers into
4998 the input strings; they record just what was matched (on this
4999 attempt) by a subexpression part of the pattern, that is, the
5000 regnum-th regstart pointer points to where in the pattern we began
5001 matching and the regnum-th regend points to right after where we
5002 stopped matching the regnum-th subexpression. (The zeroth register
5003 keeps track of what the whole pattern matches.) */
5004 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
5005 re_char
**regstart
, **regend
;
5008 /* The following record the register info as found in the above
5009 variables when we find a match better than any we've seen before.
5010 This happens as we backtrack through the failure points, which in
5011 turn happens only if we have not yet matched the entire string. */
5012 unsigned best_regs_set
= false;
5013 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
5014 re_char
**best_regstart
, **best_regend
;
5017 /* Logically, this is `best_regend[0]'. But we don't want to have to
5018 allocate space for that if we're not allocating space for anything
5019 else (see below). Also, we never need info about register 0 for
5020 any of the other register vectors, and it seems rather a kludge to
5021 treat `best_regend' differently than the rest. So we keep track of
5022 the end of the best match so far in a separate variable. We
5023 initialize this to NULL so that when we backtrack the first time
5024 and need to test it, it's not garbage. */
5025 re_char
*match_end
= NULL
;
5027 #ifdef DEBUG_COMPILES_ARGUMENTS
5028 /* Counts the total number of registers pushed. */
5029 unsigned num_regs_pushed
= 0;
5032 DEBUG_PRINT ("\n\nEntering re_match_2.\n");
5034 REGEX_USE_SAFE_ALLOCA
;
5038 #ifdef MATCH_MAY_ALLOCATE
5039 /* Do not bother to initialize all the register variables if there are
5040 no groups in the pattern, as it takes a fair amount of time. If
5041 there are groups, we include space for register 0 (the whole
5042 pattern), even though we never use it, since it simplifies the
5043 array indexing. We should fix this. */
5046 regstart
= REGEX_TALLOC (num_regs
, re_char
*);
5047 regend
= REGEX_TALLOC (num_regs
, re_char
*);
5048 best_regstart
= REGEX_TALLOC (num_regs
, re_char
*);
5049 best_regend
= REGEX_TALLOC (num_regs
, re_char
*);
5051 if (!(regstart
&& regend
&& best_regstart
&& best_regend
))
5059 /* We must initialize all our variables to NULL, so that
5060 `FREE_VARIABLES' doesn't try to free them. */
5061 regstart
= regend
= best_regstart
= best_regend
= NULL
;
5063 #endif /* MATCH_MAY_ALLOCATE */
5065 /* The starting position is bogus. */
5066 if (pos
< 0 || pos
> size1
+ size2
)
5072 /* Initialize subexpression text positions to -1 to mark ones that no
5073 start_memory/stop_memory has been seen for. Also initialize the
5074 register information struct. */
5075 for (reg
= 1; reg
< num_regs
; reg
++)
5076 regstart
[reg
] = regend
[reg
] = NULL
;
5078 /* We move `string1' into `string2' if the latter's empty -- but not if
5079 `string1' is null. */
5080 if (size2
== 0 && string1
!= NULL
)
5087 end1
= string1
+ size1
;
5088 end2
= string2
+ size2
;
5090 /* `p' scans through the pattern as `d' scans through the data.
5091 `dend' is the end of the input string that `d' points within. `d'
5092 is advanced into the following input string whenever necessary, but
5093 this happens before fetching; therefore, at the beginning of the
5094 loop, `d' can be pointing at the end of a string, but it cannot
5098 /* Only match within string2. */
5099 d
= string2
+ pos
- size1
;
5100 dend
= end_match_2
= string2
+ stop
- size1
;
5101 end_match_1
= end1
; /* Just to give it a value. */
5107 /* Only match within string1. */
5108 end_match_1
= string1
+ stop
;
5110 When we reach end_match_1, PREFETCH normally switches to string2.
5111 But in the present case, this means that just doing a PREFETCH
5112 makes us jump from `stop' to `gap' within the string.
5113 What we really want here is for the search to stop as
5114 soon as we hit end_match_1. That's why we set end_match_2
5115 to end_match_1 (since PREFETCH fails as soon as we hit
5117 end_match_2
= end_match_1
;
5120 { /* It's important to use this code when stop == size so that
5121 moving `d' from end1 to string2 will not prevent the d == dend
5122 check from catching the end of string. */
5124 end_match_2
= string2
+ stop
- size1
;
5130 DEBUG_PRINT ("The compiled pattern is: ");
5131 DEBUG_PRINT_COMPILED_PATTERN (bufp
, p
, pend
);
5132 DEBUG_PRINT ("The string to match is: \"");
5133 DEBUG_PRINT_DOUBLE_STRING (d
, string1
, size1
, string2
, size2
);
5134 DEBUG_PRINT ("\"\n");
5136 /* This loops over pattern commands. It exits by returning from the
5137 function if the match is complete, or it drops through if the match
5138 fails at this starting point in the input data. */
5141 DEBUG_PRINT ("\n%p: ", p
);
5147 /* End of pattern means we might have succeeded. */
5148 DEBUG_PRINT ("end of pattern ... ");
5150 /* If we haven't matched the entire string, and we want the
5151 longest match, try backtracking. */
5152 if (d
!= end_match_2
)
5154 /* 1 if this match ends in the same string (string1 or string2)
5155 as the best previous match. */
5156 boolean same_str_p
= (FIRST_STRING_P (match_end
)
5157 == FIRST_STRING_P (d
));
5158 /* 1 if this match is the best seen so far. */
5159 boolean best_match_p
;
5161 /* AIX compiler got confused when this was combined
5162 with the previous declaration. */
5164 best_match_p
= d
> match_end
;
5166 best_match_p
= !FIRST_STRING_P (d
);
5168 DEBUG_PRINT ("backtracking.\n");
5170 if (!FAIL_STACK_EMPTY ())
5171 { /* More failure points to try. */
5173 /* If exceeds best match so far, save it. */
5174 if (!best_regs_set
|| best_match_p
)
5176 best_regs_set
= true;
5179 DEBUG_PRINT ("\nSAVING match as best so far.\n");
5181 for (reg
= 1; reg
< num_regs
; reg
++)
5183 best_regstart
[reg
] = regstart
[reg
];
5184 best_regend
[reg
] = regend
[reg
];
5190 /* If no failure points, don't restore garbage. And if
5191 last match is real best match, don't restore second
5193 else if (best_regs_set
&& !best_match_p
)
5196 /* Restore best match. It may happen that `dend ==
5197 end_match_1' while the restored d is in string2.
5198 For example, the pattern `x.*y.*z' against the
5199 strings `x-' and `y-z-', if the two strings are
5200 not consecutive in memory. */
5201 DEBUG_PRINT ("Restoring best registers.\n");
5204 dend
= ((d
>= string1
&& d
<= end1
)
5205 ? end_match_1
: end_match_2
);
5207 for (reg
= 1; reg
< num_regs
; reg
++)
5209 regstart
[reg
] = best_regstart
[reg
];
5210 regend
[reg
] = best_regend
[reg
];
5213 } /* d != end_match_2 */
5216 DEBUG_PRINT ("Accepting match.\n");
5218 /* If caller wants register contents data back, do it. */
5219 if (regs
&& !bufp
->no_sub
)
5221 /* Have the register data arrays been allocated? */
5222 if (bufp
->regs_allocated
== REGS_UNALLOCATED
)
5223 { /* No. So allocate them with malloc. We need one
5224 extra element beyond `num_regs' for the `-1' marker
5226 regs
->num_regs
= max (RE_NREGS
, num_regs
+ 1);
5227 regs
->start
= TALLOC (regs
->num_regs
, regoff_t
);
5228 regs
->end
= TALLOC (regs
->num_regs
, regoff_t
);
5229 if (regs
->start
== NULL
|| regs
->end
== NULL
)
5234 bufp
->regs_allocated
= REGS_REALLOCATE
;
5236 else if (bufp
->regs_allocated
== REGS_REALLOCATE
)
5237 { /* Yes. If we need more elements than were already
5238 allocated, reallocate them. If we need fewer, just
5240 if (regs
->num_regs
< num_regs
+ 1)
5242 regs
->num_regs
= num_regs
+ 1;
5243 RETALLOC (regs
->start
, regs
->num_regs
, regoff_t
);
5244 RETALLOC (regs
->end
, regs
->num_regs
, regoff_t
);
5245 if (regs
->start
== NULL
|| regs
->end
== NULL
)
5254 /* These braces fend off a "empty body in an else-statement"
5255 warning under GCC when assert expands to nothing. */
5256 assert (bufp
->regs_allocated
== REGS_FIXED
);
5259 /* Convert the pointer data in `regstart' and `regend' to
5260 indices. Register zero has to be set differently,
5261 since we haven't kept track of any info for it. */
5262 if (regs
->num_regs
> 0)
5264 regs
->start
[0] = pos
;
5265 regs
->end
[0] = POINTER_TO_OFFSET (d
);
5268 /* Go through the first `min (num_regs, regs->num_regs)'
5269 registers, since that is all we initialized. */
5270 for (reg
= 1; reg
< min (num_regs
, regs
->num_regs
); reg
++)
5272 if (REG_UNSET (regstart
[reg
]) || REG_UNSET (regend
[reg
]))
5273 regs
->start
[reg
] = regs
->end
[reg
] = -1;
5276 regs
->start
[reg
] = POINTER_TO_OFFSET (regstart
[reg
]);
5277 regs
->end
[reg
] = POINTER_TO_OFFSET (regend
[reg
]);
5281 /* If the regs structure we return has more elements than
5282 were in the pattern, set the extra elements to -1. If
5283 we (re)allocated the registers, this is the case,
5284 because we always allocate enough to have at least one
5286 for (reg
= num_regs
; reg
< regs
->num_regs
; reg
++)
5287 regs
->start
[reg
] = regs
->end
[reg
] = -1;
5288 } /* regs && !bufp->no_sub */
5290 DEBUG_PRINT ("%u failure points pushed, %u popped (%u remain).\n",
5291 nfailure_points_pushed
, nfailure_points_popped
,
5292 nfailure_points_pushed
- nfailure_points_popped
);
5293 DEBUG_PRINT ("%u registers pushed.\n", num_regs_pushed
);
5295 dcnt
= POINTER_TO_OFFSET (d
) - pos
;
5297 DEBUG_PRINT ("Returning %td from re_match_2.\n", dcnt
);
5303 /* Otherwise match next pattern command. */
5306 /* Ignore these. Used to ignore the n of succeed_n's which
5307 currently have n == 0. */
5309 DEBUG_PRINT ("EXECUTING no_op.\n");
5313 DEBUG_PRINT ("EXECUTING succeed.\n");
5316 /* Match the next n pattern characters exactly. The following
5317 byte in the pattern defines n, and the n bytes after that
5318 are the characters to match. */
5321 DEBUG_PRINT ("EXECUTING exactn %d.\n", mcnt
);
5323 /* Remember the start point to rollback upon failure. */
5327 /* This is written out as an if-else so we don't waste time
5328 testing `translate' inside the loop. */
5329 if (RE_TRANSLATE_P (translate
))
5333 if (RE_TRANSLATE (translate
, *d
) != *p
++)
5353 /* The cost of testing `translate' is comparatively small. */
5354 if (target_multibyte
)
5357 int pat_charlen
, buf_charlen
;
5362 pat_ch
= STRING_CHAR_AND_LENGTH (p
, pat_charlen
);
5365 pat_ch
= RE_CHAR_TO_MULTIBYTE (*p
);
5368 buf_ch
= STRING_CHAR_AND_LENGTH (d
, buf_charlen
);
5370 if (TRANSLATE (buf_ch
) != pat_ch
)
5378 mcnt
-= pat_charlen
;
5390 pat_ch
= STRING_CHAR_AND_LENGTH (p
, pat_charlen
);
5391 pat_ch
= RE_CHAR_TO_UNIBYTE (pat_ch
);
5398 buf_ch
= RE_CHAR_TO_MULTIBYTE (*d
);
5399 if (! CHAR_BYTE8_P (buf_ch
))
5401 buf_ch
= TRANSLATE (buf_ch
);
5402 buf_ch
= RE_CHAR_TO_UNIBYTE (buf_ch
);
5408 if (buf_ch
!= pat_ch
)
5421 /* Match any character except possibly a newline or a null. */
5427 DEBUG_PRINT ("EXECUTING anychar.\n");
5430 buf_ch
= RE_STRING_CHAR_AND_LENGTH (d
, buf_charlen
,
5432 buf_ch
= TRANSLATE (buf_ch
);
5434 if ((!(bufp
->syntax
& RE_DOT_NEWLINE
)
5436 || ((bufp
->syntax
& RE_DOT_NOT_NULL
)
5437 && buf_ch
== '\000'))
5440 DEBUG_PRINT (" Matched \"%d\".\n", *d
);
5449 register unsigned int c
, corig
;
5450 boolean
not = (re_opcode_t
) *(p
- 1) == charset_not
;
5453 /* Start of actual range_table, or end of bitmap if there is no
5455 re_char
*range_table
IF_LINT (= NULL
);
5457 /* Nonzero if there is a range table. */
5458 int range_table_exists
;
5460 /* Number of ranges of range table. This is not included
5461 in the initial byte-length of the command. */
5464 /* Whether matching against a unibyte character. */
5465 boolean unibyte_char
= false;
5467 DEBUG_PRINT ("EXECUTING charset%s.\n", not ? "_not" : "");
5469 range_table_exists
= CHARSET_RANGE_TABLE_EXISTS_P (&p
[-1]);
5471 if (range_table_exists
)
5473 range_table
= CHARSET_RANGE_TABLE (&p
[-1]); /* Past the bitmap. */
5474 EXTRACT_NUMBER_AND_INCR (count
, range_table
);
5478 corig
= c
= RE_STRING_CHAR_AND_LENGTH (d
, len
, target_multibyte
);
5479 if (target_multibyte
)
5484 c1
= RE_CHAR_TO_UNIBYTE (c
);
5487 unibyte_char
= true;
5493 int c1
= RE_CHAR_TO_MULTIBYTE (c
);
5495 if (! CHAR_BYTE8_P (c1
))
5497 c1
= TRANSLATE (c1
);
5498 c1
= RE_CHAR_TO_UNIBYTE (c1
);
5501 unibyte_char
= true;
5506 unibyte_char
= true;
5509 if (unibyte_char
&& c
< (1 << BYTEWIDTH
))
5510 { /* Lookup bitmap. */
5511 /* Cast to `unsigned' instead of `unsigned char' in
5512 case the bit list is a full 32 bytes long. */
5513 if (c
< (unsigned) (CHARSET_BITMAP_SIZE (&p
[-1]) * BYTEWIDTH
)
5514 && p
[1 + c
/ BYTEWIDTH
] & (1 << (c
% BYTEWIDTH
)))
5518 else if (range_table_exists
)
5520 int class_bits
= CHARSET_RANGE_TABLE_BITS (&p
[-1]);
5522 if ( (class_bits
& BIT_LOWER
5525 && c
== upcase (corig
) && ISUPPER(c
))))
5526 | (class_bits
& BIT_MULTIBYTE
)
5527 | (class_bits
& BIT_PUNCT
&& ISPUNCT (c
))
5528 | (class_bits
& BIT_SPACE
&& ISSPACE (c
))
5529 | (class_bits
& BIT_UPPER
5532 && c
== downcase (corig
) && ISLOWER (c
))))
5533 | (class_bits
& BIT_WORD
&& ISWORD (c
))
5534 | (class_bits
& BIT_ALPHA
&& ISALPHA (c
))
5535 | (class_bits
& BIT_ALNUM
&& ISALNUM (c
))
5536 | (class_bits
& BIT_GRAPH
&& ISGRAPH (c
))
5537 | (class_bits
& BIT_PRINT
&& ISPRINT (c
)))
5540 CHARSET_LOOKUP_RANGE_TABLE_RAW (not, c
, range_table
, count
);
5544 if (range_table_exists
)
5545 p
= CHARSET_RANGE_TABLE_END (range_table
, count
);
5547 p
+= CHARSET_BITMAP_SIZE (&p
[-1]) + 1;
5549 if (!not) goto fail
;
5556 /* The beginning of a group is represented by start_memory.
5557 The argument is the register number. The text
5558 matched within the group is recorded (in the internal
5559 registers data structure) under the register number. */
5561 DEBUG_PRINT ("EXECUTING start_memory %d:\n", *p
);
5563 /* In case we need to undo this operation (via backtracking). */
5564 PUSH_FAILURE_REG (*p
);
5567 regend
[*p
] = NULL
; /* probably unnecessary. -sm */
5568 DEBUG_PRINT (" regstart: %td\n", POINTER_TO_OFFSET (regstart
[*p
]));
5570 /* Move past the register number and inner group count. */
5575 /* The stop_memory opcode represents the end of a group. Its
5576 argument is the same as start_memory's: the register number. */
5578 DEBUG_PRINT ("EXECUTING stop_memory %d:\n", *p
);
5580 assert (!REG_UNSET (regstart
[*p
]));
5581 /* Strictly speaking, there should be code such as:
5583 assert (REG_UNSET (regend[*p]));
5584 PUSH_FAILURE_REGSTOP ((unsigned int)*p);
5586 But the only info to be pushed is regend[*p] and it is known to
5587 be UNSET, so there really isn't anything to push.
5588 Not pushing anything, on the other hand deprives us from the
5589 guarantee that regend[*p] is UNSET since undoing this operation
5590 will not reset its value properly. This is not important since
5591 the value will only be read on the next start_memory or at
5592 the very end and both events can only happen if this stop_memory
5596 DEBUG_PRINT (" regend: %td\n", POINTER_TO_OFFSET (regend
[*p
]));
5598 /* Move past the register number and the inner group count. */
5603 /* \<digit> has been turned into a `duplicate' command which is
5604 followed by the numeric value of <digit> as the register number. */
5607 register re_char
*d2
, *dend2
;
5608 int regno
= *p
++; /* Get which register to match against. */
5609 DEBUG_PRINT ("EXECUTING duplicate %d.\n", regno
);
5611 /* Can't back reference a group which we've never matched. */
5612 if (REG_UNSET (regstart
[regno
]) || REG_UNSET (regend
[regno
]))
5615 /* Where in input to try to start matching. */
5616 d2
= regstart
[regno
];
5618 /* Remember the start point to rollback upon failure. */
5621 /* Where to stop matching; if both the place to start and
5622 the place to stop matching are in the same string, then
5623 set to the place to stop, otherwise, for now have to use
5624 the end of the first string. */
5626 dend2
= ((FIRST_STRING_P (regstart
[regno
])
5627 == FIRST_STRING_P (regend
[regno
]))
5628 ? regend
[regno
] : end_match_1
);
5633 /* If necessary, advance to next segment in register
5637 if (dend2
== end_match_2
) break;
5638 if (dend2
== regend
[regno
]) break;
5640 /* End of string1 => advance to string2. */
5642 dend2
= regend
[regno
];
5644 /* At end of register contents => success */
5645 if (d2
== dend2
) break;
5647 /* If necessary, advance to next segment in data. */
5650 /* How many characters left in this segment to match. */
5653 /* Want how many consecutive characters we can match in
5654 one shot, so, if necessary, adjust the count. */
5655 if (dcnt
> dend2
- d2
)
5658 /* Compare that many; failure if mismatch, else move
5660 if (RE_TRANSLATE_P (translate
)
5661 ? bcmp_translate (d
, d2
, dcnt
, translate
, target_multibyte
)
5662 : memcmp (d
, d2
, dcnt
))
5667 d
+= dcnt
, d2
+= dcnt
;
5673 /* begline matches the empty string at the beginning of the string
5674 (unless `not_bol' is set in `bufp'), and after newlines. */
5676 DEBUG_PRINT ("EXECUTING begline.\n");
5678 if (AT_STRINGS_BEG (d
))
5680 if (!bufp
->not_bol
) break;
5685 GET_CHAR_BEFORE_2 (c
, d
, string1
, end1
, string2
, end2
);
5689 /* In all other cases, we fail. */
5693 /* endline is the dual of begline. */
5695 DEBUG_PRINT ("EXECUTING endline.\n");
5697 if (AT_STRINGS_END (d
))
5699 if (!bufp
->not_eol
) break;
5703 PREFETCH_NOLIMIT ();
5710 /* Match at the very beginning of the data. */
5712 DEBUG_PRINT ("EXECUTING begbuf.\n");
5713 if (AT_STRINGS_BEG (d
))
5718 /* Match at the very end of the data. */
5720 DEBUG_PRINT ("EXECUTING endbuf.\n");
5721 if (AT_STRINGS_END (d
))
5726 /* on_failure_keep_string_jump is used to optimize `.*\n'. It
5727 pushes NULL as the value for the string on the stack. Then
5728 `POP_FAILURE_POINT' will keep the current value for the
5729 string, instead of restoring it. To see why, consider
5730 matching `foo\nbar' against `.*\n'. The .* matches the foo;
5731 then the . fails against the \n. But the next thing we want
5732 to do is match the \n against the \n; if we restored the
5733 string value, we would be back at the foo.
5735 Because this is used only in specific cases, we don't need to
5736 check all the things that `on_failure_jump' does, to make
5737 sure the right things get saved on the stack. Hence we don't
5738 share its code. The only reason to push anything on the
5739 stack at all is that otherwise we would have to change
5740 `anychar's code to do something besides goto fail in this
5741 case; that seems worse than this. */
5742 case on_failure_keep_string_jump
:
5743 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5744 DEBUG_PRINT ("EXECUTING on_failure_keep_string_jump %d (to %p):\n",
5747 PUSH_FAILURE_POINT (p
- 3, NULL
);
5750 /* A nasty loop is introduced by the non-greedy *? and +?.
5751 With such loops, the stack only ever contains one failure point
5752 at a time, so that a plain on_failure_jump_loop kind of
5753 cycle detection cannot work. Worse yet, such a detection
5754 can not only fail to detect a cycle, but it can also wrongly
5755 detect a cycle (between different instantiations of the same
5757 So the method used for those nasty loops is a little different:
5758 We use a special cycle-detection-stack-frame which is pushed
5759 when the on_failure_jump_nastyloop failure-point is *popped*.
5760 This special frame thus marks the beginning of one iteration
5761 through the loop and we can hence easily check right here
5762 whether something matched between the beginning and the end of
5764 case on_failure_jump_nastyloop
:
5765 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5766 DEBUG_PRINT ("EXECUTING on_failure_jump_nastyloop %d (to %p):\n",
5769 assert ((re_opcode_t
)p
[-4] == no_op
);
5772 CHECK_INFINITE_LOOP (p
- 4, d
);
5774 /* If there's a cycle, just continue without pushing
5775 this failure point. The failure point is the "try again"
5776 option, which shouldn't be tried.
5777 We want (x?)*?y\1z to match both xxyz and xxyxz. */
5778 PUSH_FAILURE_POINT (p
- 3, d
);
5782 /* Simple loop detecting on_failure_jump: just check on the
5783 failure stack if the same spot was already hit earlier. */
5784 case on_failure_jump_loop
:
5786 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5787 DEBUG_PRINT ("EXECUTING on_failure_jump_loop %d (to %p):\n",
5791 CHECK_INFINITE_LOOP (p
- 3, d
);
5793 /* If there's a cycle, get out of the loop, as if the matching
5794 had failed. We used to just `goto fail' here, but that was
5795 aborting the search a bit too early: we want to keep the
5796 empty-loop-match and keep matching after the loop.
5797 We want (x?)*y\1z to match both xxyz and xxyxz. */
5800 PUSH_FAILURE_POINT (p
- 3, d
);
5805 /* Uses of on_failure_jump:
5807 Each alternative starts with an on_failure_jump that points
5808 to the beginning of the next alternative. Each alternative
5809 except the last ends with a jump that in effect jumps past
5810 the rest of the alternatives. (They really jump to the
5811 ending jump of the following alternative, because tensioning
5812 these jumps is a hassle.)
5814 Repeats start with an on_failure_jump that points past both
5815 the repetition text and either the following jump or
5816 pop_failure_jump back to this on_failure_jump. */
5817 case on_failure_jump
:
5818 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5819 DEBUG_PRINT ("EXECUTING on_failure_jump %d (to %p):\n",
5822 PUSH_FAILURE_POINT (p
-3, d
);
5825 /* This operation is used for greedy *.
5826 Compare the beginning of the repeat with what in the
5827 pattern follows its end. If we can establish that there
5828 is nothing that they would both match, i.e., that we
5829 would have to backtrack because of (as in, e.g., `a*a')
5830 then we can use a non-backtracking loop based on
5831 on_failure_keep_string_jump instead of on_failure_jump. */
5832 case on_failure_jump_smart
:
5833 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5834 DEBUG_PRINT ("EXECUTING on_failure_jump_smart %d (to %p).\n",
5837 re_char
*p1
= p
; /* Next operation. */
5838 /* Here, we discard `const', making re_match non-reentrant. */
5839 unsigned char *p2
= (unsigned char*) p
+ mcnt
; /* Jump dest. */
5840 unsigned char *p3
= (unsigned char*) p
- 3; /* opcode location. */
5842 p
-= 3; /* Reset so that we will re-execute the
5843 instruction once it's been changed. */
5845 EXTRACT_NUMBER (mcnt
, p2
- 2);
5847 /* Ensure this is a indeed the trivial kind of loop
5848 we are expecting. */
5849 assert (skip_one_char (p1
) == p2
- 3);
5850 assert ((re_opcode_t
) p2
[-3] == jump
&& p2
+ mcnt
== p
);
5851 DEBUG_STATEMENT (debug
+= 2);
5852 if (mutually_exclusive_p (bufp
, p1
, p2
))
5854 /* Use a fast `on_failure_keep_string_jump' loop. */
5855 DEBUG_PRINT (" smart exclusive => fast loop.\n");
5856 *p3
= (unsigned char) on_failure_keep_string_jump
;
5857 STORE_NUMBER (p2
- 2, mcnt
+ 3);
5861 /* Default to a safe `on_failure_jump' loop. */
5862 DEBUG_PRINT (" smart default => slow loop.\n");
5863 *p3
= (unsigned char) on_failure_jump
;
5865 DEBUG_STATEMENT (debug
-= 2);
5869 /* Unconditionally jump (without popping any failure points). */
5872 IMMEDIATE_QUIT_CHECK
;
5873 EXTRACT_NUMBER_AND_INCR (mcnt
, p
); /* Get the amount to jump. */
5874 DEBUG_PRINT ("EXECUTING jump %d ", mcnt
);
5875 p
+= mcnt
; /* Do the jump. */
5876 DEBUG_PRINT ("(to %p).\n", p
);
5880 /* Have to succeed matching what follows at least n times.
5881 After that, handle like `on_failure_jump'. */
5883 /* Signedness doesn't matter since we only compare MCNT to 0. */
5884 EXTRACT_NUMBER (mcnt
, p
+ 2);
5885 DEBUG_PRINT ("EXECUTING succeed_n %d.\n", mcnt
);
5887 /* Originally, mcnt is how many times we HAVE to succeed. */
5890 /* Here, we discard `const', making re_match non-reentrant. */
5891 unsigned char *p2
= (unsigned char*) p
+ 2; /* counter loc. */
5894 PUSH_NUMBER (p2
, mcnt
);
5897 /* The two bytes encoding mcnt == 0 are two no_op opcodes. */
5902 /* Signedness doesn't matter since we only compare MCNT to 0. */
5903 EXTRACT_NUMBER (mcnt
, p
+ 2);
5904 DEBUG_PRINT ("EXECUTING jump_n %d.\n", mcnt
);
5906 /* Originally, this is how many times we CAN jump. */
5909 /* Here, we discard `const', making re_match non-reentrant. */
5910 unsigned char *p2
= (unsigned char*) p
+ 2; /* counter loc. */
5912 PUSH_NUMBER (p2
, mcnt
);
5913 goto unconditional_jump
;
5915 /* If don't have to jump any more, skip over the rest of command. */
5922 unsigned char *p2
; /* Location of the counter. */
5923 DEBUG_PRINT ("EXECUTING set_number_at.\n");
5925 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5926 /* Here, we discard `const', making re_match non-reentrant. */
5927 p2
= (unsigned char*) p
+ mcnt
;
5928 /* Signedness doesn't matter since we only copy MCNT's bits. */
5929 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5930 DEBUG_PRINT (" Setting %p to %d.\n", p2
, mcnt
);
5931 PUSH_NUMBER (p2
, mcnt
);
5938 boolean
not = (re_opcode_t
) *(p
- 1) == notwordbound
;
5939 DEBUG_PRINT ("EXECUTING %swordbound.\n", not ? "not" : "");
5941 /* We SUCCEED (or FAIL) in one of the following cases: */
5943 /* Case 1: D is at the beginning or the end of string. */
5944 if (AT_STRINGS_BEG (d
) || AT_STRINGS_END (d
))
5948 /* C1 is the character before D, S1 is the syntax of C1, C2
5949 is the character at D, and S2 is the syntax of C2. */
5954 ssize_t offset
= PTR_TO_OFFSET (d
- 1);
5955 ssize_t charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
5956 UPDATE_SYNTAX_TABLE_FAST (charpos
);
5958 GET_CHAR_BEFORE_2 (c1
, d
, string1
, end1
, string2
, end2
);
5961 UPDATE_SYNTAX_TABLE_FORWARD_FAST (charpos
+ 1);
5963 PREFETCH_NOLIMIT ();
5964 GET_CHAR_AFTER (c2
, d
, dummy
);
5967 if (/* Case 2: Only one of S1 and S2 is Sword. */
5968 ((s1
== Sword
) != (s2
== Sword
))
5969 /* Case 3: Both of S1 and S2 are Sword, and macro
5970 WORD_BOUNDARY_P (C1, C2) returns nonzero. */
5971 || ((s1
== Sword
) && WORD_BOUNDARY_P (c1
, c2
)))
5981 DEBUG_PRINT ("EXECUTING wordbeg.\n");
5983 /* We FAIL in one of the following cases: */
5985 /* Case 1: D is at the end of string. */
5986 if (AT_STRINGS_END (d
))
5990 /* C1 is the character before D, S1 is the syntax of C1, C2
5991 is the character at D, and S2 is the syntax of C2. */
5996 ssize_t offset
= PTR_TO_OFFSET (d
);
5997 ssize_t charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
5998 UPDATE_SYNTAX_TABLE_FAST (charpos
);
6001 GET_CHAR_AFTER (c2
, d
, dummy
);
6004 /* Case 2: S2 is not Sword. */
6008 /* Case 3: D is not at the beginning of string ... */
6009 if (!AT_STRINGS_BEG (d
))
6011 GET_CHAR_BEFORE_2 (c1
, d
, string1
, end1
, string2
, end2
);
6013 UPDATE_SYNTAX_TABLE_BACKWARD (charpos
- 1);
6017 /* ... and S1 is Sword, and WORD_BOUNDARY_P (C1, C2)
6019 if ((s1
== Sword
) && !WORD_BOUNDARY_P (c1
, c2
))
6026 DEBUG_PRINT ("EXECUTING wordend.\n");
6028 /* We FAIL in one of the following cases: */
6030 /* Case 1: D is at the beginning of string. */
6031 if (AT_STRINGS_BEG (d
))
6035 /* C1 is the character before D, S1 is the syntax of C1, C2
6036 is the character at D, and S2 is the syntax of C2. */
6041 ssize_t offset
= PTR_TO_OFFSET (d
) - 1;
6042 ssize_t charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
6043 UPDATE_SYNTAX_TABLE_FAST (charpos
);
6045 GET_CHAR_BEFORE_2 (c1
, d
, string1
, end1
, string2
, end2
);
6048 /* Case 2: S1 is not Sword. */
6052 /* Case 3: D is not at the end of string ... */
6053 if (!AT_STRINGS_END (d
))
6055 PREFETCH_NOLIMIT ();
6056 GET_CHAR_AFTER (c2
, d
, dummy
);
6058 UPDATE_SYNTAX_TABLE_FORWARD_FAST (charpos
);
6062 /* ... and S2 is Sword, and WORD_BOUNDARY_P (C1, C2)
6064 if ((s2
== Sword
) && !WORD_BOUNDARY_P (c1
, c2
))
6071 DEBUG_PRINT ("EXECUTING symbeg.\n");
6073 /* We FAIL in one of the following cases: */
6075 /* Case 1: D is at the end of string. */
6076 if (AT_STRINGS_END (d
))
6080 /* C1 is the character before D, S1 is the syntax of C1, C2
6081 is the character at D, and S2 is the syntax of C2. */
6085 ssize_t offset
= PTR_TO_OFFSET (d
);
6086 ssize_t charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
6087 UPDATE_SYNTAX_TABLE_FAST (charpos
);
6090 c2
= RE_STRING_CHAR (d
, target_multibyte
);
6093 /* Case 2: S2 is neither Sword nor Ssymbol. */
6094 if (s2
!= Sword
&& s2
!= Ssymbol
)
6097 /* Case 3: D is not at the beginning of string ... */
6098 if (!AT_STRINGS_BEG (d
))
6100 GET_CHAR_BEFORE_2 (c1
, d
, string1
, end1
, string2
, end2
);
6102 UPDATE_SYNTAX_TABLE_BACKWARD (charpos
- 1);
6106 /* ... and S1 is Sword or Ssymbol. */
6107 if (s1
== Sword
|| s1
== Ssymbol
)
6114 DEBUG_PRINT ("EXECUTING symend.\n");
6116 /* We FAIL in one of the following cases: */
6118 /* Case 1: D is at the beginning of string. */
6119 if (AT_STRINGS_BEG (d
))
6123 /* C1 is the character before D, S1 is the syntax of C1, C2
6124 is the character at D, and S2 is the syntax of C2. */
6128 ssize_t offset
= PTR_TO_OFFSET (d
) - 1;
6129 ssize_t charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
6130 UPDATE_SYNTAX_TABLE_FAST (charpos
);
6132 GET_CHAR_BEFORE_2 (c1
, d
, string1
, end1
, string2
, end2
);
6135 /* Case 2: S1 is neither Ssymbol nor Sword. */
6136 if (s1
!= Sword
&& s1
!= Ssymbol
)
6139 /* Case 3: D is not at the end of string ... */
6140 if (!AT_STRINGS_END (d
))
6142 PREFETCH_NOLIMIT ();
6143 c2
= RE_STRING_CHAR (d
, target_multibyte
);
6145 UPDATE_SYNTAX_TABLE_FORWARD_FAST (charpos
+ 1);
6149 /* ... and S2 is Sword or Ssymbol. */
6150 if (s2
== Sword
|| s2
== Ssymbol
)
6159 boolean
not = (re_opcode_t
) *(p
- 1) == notsyntaxspec
;
6161 DEBUG_PRINT ("EXECUTING %ssyntaxspec %d.\n", not ? "not" : "",
6166 ssize_t offset
= PTR_TO_OFFSET (d
);
6167 ssize_t pos1
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
6168 UPDATE_SYNTAX_TABLE_FAST (pos1
);
6175 GET_CHAR_AFTER (c
, d
, len
);
6176 if ((SYNTAX (c
) != (enum syntaxcode
) mcnt
) ^ not)
6185 DEBUG_PRINT ("EXECUTING before_dot.\n");
6186 if (PTR_BYTE_POS (d
) >= PT_BYTE
)
6191 DEBUG_PRINT ("EXECUTING at_dot.\n");
6192 if (PTR_BYTE_POS (d
) != PT_BYTE
)
6197 DEBUG_PRINT ("EXECUTING after_dot.\n");
6198 if (PTR_BYTE_POS (d
) <= PT_BYTE
)
6203 case notcategoryspec
:
6205 boolean
not = (re_opcode_t
) *(p
- 1) == notcategoryspec
;
6207 DEBUG_PRINT ("EXECUTING %scategoryspec %d.\n",
6208 not ? "not" : "", mcnt
);
6214 GET_CHAR_AFTER (c
, d
, len
);
6215 if ((!CHAR_HAS_CATEGORY (c
, mcnt
)) ^ not)
6227 continue; /* Successfully executed one pattern command; keep going. */
6230 /* We goto here if a matching operation fails. */
6232 IMMEDIATE_QUIT_CHECK
;
6233 if (!FAIL_STACK_EMPTY ())
6236 /* A restart point is known. Restore to that state. */
6237 DEBUG_PRINT ("\nFAIL:\n");
6238 POP_FAILURE_POINT (str
, pat
);
6241 case on_failure_keep_string_jump
:
6242 assert (str
== NULL
);
6243 goto continue_failure_jump
;
6245 case on_failure_jump_nastyloop
:
6246 assert ((re_opcode_t
)pat
[-2] == no_op
);
6247 PUSH_FAILURE_POINT (pat
- 2, str
);
6250 case on_failure_jump_loop
:
6251 case on_failure_jump
:
6254 continue_failure_jump
:
6255 EXTRACT_NUMBER_AND_INCR (mcnt
, pat
);
6260 /* A special frame used for nastyloops. */
6267 assert (p
>= bufp
->buffer
&& p
<= pend
);
6269 if (d
>= string1
&& d
<= end1
)
6273 break; /* Matching at this starting point really fails. */
6277 goto restore_best_regs
;
6281 return -1; /* Failure to match. */
6284 /* Subroutine definitions for re_match_2. */
6286 /* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
6287 bytes; nonzero otherwise. */
6290 bcmp_translate (const_re_char
*s1
, const_re_char
*s2
, register ssize_t len
,
6291 RE_TRANSLATE_TYPE translate
, const int target_multibyte
)
6293 register re_char
*p1
= s1
, *p2
= s2
;
6294 re_char
*p1_end
= s1
+ len
;
6295 re_char
*p2_end
= s2
+ len
;
6297 /* FIXME: Checking both p1 and p2 presumes that the two strings might have
6298 different lengths, but relying on a single `len' would break this. -sm */
6299 while (p1
< p1_end
&& p2
< p2_end
)
6301 int p1_charlen
, p2_charlen
;
6302 re_wchar_t p1_ch
, p2_ch
;
6304 GET_CHAR_AFTER (p1_ch
, p1
, p1_charlen
);
6305 GET_CHAR_AFTER (p2_ch
, p2
, p2_charlen
);
6307 if (RE_TRANSLATE (translate
, p1_ch
)
6308 != RE_TRANSLATE (translate
, p2_ch
))
6311 p1
+= p1_charlen
, p2
+= p2_charlen
;
6314 if (p1
!= p1_end
|| p2
!= p2_end
)
6320 /* Entry points for GNU code. */
6322 /* re_compile_pattern is the GNU regular expression compiler: it
6323 compiles PATTERN (of length SIZE) and puts the result in BUFP.
6324 Returns 0 if the pattern was valid, otherwise an error string.
6326 Assumes the `allocated' (and perhaps `buffer') and `translate' fields
6327 are set in BUFP on entry.
6329 We call regex_compile to do the actual compilation. */
6332 re_compile_pattern (const char *pattern
, size_t length
,
6333 struct re_pattern_buffer
*bufp
)
6337 /* GNU code is written to assume at least RE_NREGS registers will be set
6338 (and at least one extra will be -1). */
6339 bufp
->regs_allocated
= REGS_UNALLOCATED
;
6341 /* And GNU code determines whether or not to get register information
6342 by passing null for the REGS argument to re_match, etc., not by
6346 ret
= regex_compile ((re_char
*) pattern
, length
, re_syntax_options
, bufp
);
6350 return gettext (re_error_msgid
[(int) ret
]);
6352 WEAK_ALIAS (__re_compile_pattern
, re_compile_pattern
)
6354 /* Entry points compatible with 4.2 BSD regex library. We don't define
6355 them unless specifically requested. */
6357 #if defined _REGEX_RE_COMP || defined _LIBC
6359 /* BSD has one and only one pattern buffer. */
6360 static struct re_pattern_buffer re_comp_buf
;
6364 /* Make these definitions weak in libc, so POSIX programs can redefine
6365 these names if they don't use our functions, and still use
6366 regcomp/regexec below without link errors. */
6369 re_comp (const char *s
)
6375 if (!re_comp_buf
.buffer
)
6376 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6377 return (char *) gettext ("No previous regular expression");
6381 if (!re_comp_buf
.buffer
)
6383 re_comp_buf
.buffer
= malloc (200);
6384 if (re_comp_buf
.buffer
== NULL
)
6385 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6386 return (char *) gettext (re_error_msgid
[(int) REG_ESPACE
]);
6387 re_comp_buf
.allocated
= 200;
6389 re_comp_buf
.fastmap
= malloc (1 << BYTEWIDTH
);
6390 if (re_comp_buf
.fastmap
== NULL
)
6391 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6392 return (char *) gettext (re_error_msgid
[(int) REG_ESPACE
]);
6395 /* Since `re_exec' always passes NULL for the `regs' argument, we
6396 don't need to initialize the pattern buffer fields which affect it. */
6398 ret
= regex_compile (s
, strlen (s
), re_syntax_options
, &re_comp_buf
);
6403 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6404 return (char *) gettext (re_error_msgid
[(int) ret
]);
6412 re_exec (const char *s
)
6414 const size_t len
= strlen (s
);
6415 return re_search (&re_comp_buf
, s
, len
, 0, len
, 0) >= 0;
6417 #endif /* _REGEX_RE_COMP */
6419 /* POSIX.2 functions. Don't define these for Emacs. */
6423 /* regcomp takes a regular expression as a string and compiles it.
6425 PREG is a regex_t *. We do not expect any fields to be initialized,
6426 since POSIX says we shouldn't. Thus, we set
6428 `buffer' to the compiled pattern;
6429 `used' to the length of the compiled pattern;
6430 `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
6431 REG_EXTENDED bit in CFLAGS is set; otherwise, to
6432 RE_SYNTAX_POSIX_BASIC;
6433 `fastmap' to an allocated space for the fastmap;
6434 `fastmap_accurate' to zero;
6435 `re_nsub' to the number of subexpressions in PATTERN.
6437 PATTERN is the address of the pattern string.
6439 CFLAGS is a series of bits which affect compilation.
6441 If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
6442 use POSIX basic syntax.
6444 If REG_NEWLINE is set, then . and [^...] don't match newline.
6445 Also, regexec will try a match beginning after every newline.
6447 If REG_ICASE is set, then we considers upper- and lowercase
6448 versions of letters to be equivalent when matching.
6450 If REG_NOSUB is set, then when PREG is passed to regexec, that
6451 routine will report only success or failure, and nothing about the
6454 It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
6455 the return codes and their meanings.) */
6458 regcomp (regex_t
*_Restrict_ preg
, const char *_Restrict_ pattern
,
6463 = (cflags
& REG_EXTENDED
) ?
6464 RE_SYNTAX_POSIX_EXTENDED
: RE_SYNTAX_POSIX_BASIC
;
6466 /* regex_compile will allocate the space for the compiled pattern. */
6468 preg
->allocated
= 0;
6471 /* Try to allocate space for the fastmap. */
6472 preg
->fastmap
= malloc (1 << BYTEWIDTH
);
6474 if (cflags
& REG_ICASE
)
6478 preg
->translate
= malloc (CHAR_SET_SIZE
* sizeof *preg
->translate
);
6479 if (preg
->translate
== NULL
)
6480 return (int) REG_ESPACE
;
6482 /* Map uppercase characters to corresponding lowercase ones. */
6483 for (i
= 0; i
< CHAR_SET_SIZE
; i
++)
6484 preg
->translate
[i
] = ISUPPER (i
) ? TOLOWER (i
) : i
;
6487 preg
->translate
= NULL
;
6489 /* If REG_NEWLINE is set, newlines are treated differently. */
6490 if (cflags
& REG_NEWLINE
)
6491 { /* REG_NEWLINE implies neither . nor [^...] match newline. */
6492 syntax
&= ~RE_DOT_NEWLINE
;
6493 syntax
|= RE_HAT_LISTS_NOT_NEWLINE
;
6496 syntax
|= RE_NO_NEWLINE_ANCHOR
;
6498 preg
->no_sub
= !!(cflags
& REG_NOSUB
);
6500 /* POSIX says a null character in the pattern terminates it, so we
6501 can use strlen here in compiling the pattern. */
6502 ret
= regex_compile ((re_char
*) pattern
, strlen (pattern
), syntax
, preg
);
6504 /* POSIX doesn't distinguish between an unmatched open-group and an
6505 unmatched close-group: both are REG_EPAREN. */
6506 if (ret
== REG_ERPAREN
)
6509 if (ret
== REG_NOERROR
&& preg
->fastmap
)
6510 { /* Compute the fastmap now, since regexec cannot modify the pattern
6512 re_compile_fastmap (preg
);
6513 if (preg
->can_be_null
)
6514 { /* The fastmap can't be used anyway. */
6515 free (preg
->fastmap
);
6516 preg
->fastmap
= NULL
;
6521 WEAK_ALIAS (__regcomp
, regcomp
)
6524 /* regexec searches for a given pattern, specified by PREG, in the
6527 If NMATCH is zero or REG_NOSUB was set in the cflags argument to
6528 `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
6529 least NMATCH elements, and we set them to the offsets of the
6530 corresponding matched substrings.
6532 EFLAGS specifies `execution flags' which affect matching: if
6533 REG_NOTBOL is set, then ^ does not match at the beginning of the
6534 string; if REG_NOTEOL is set, then $ does not match at the end.
6536 We return 0 if we find a match and REG_NOMATCH if not. */
6539 regexec (const regex_t
*_Restrict_ preg
, const char *_Restrict_ string
,
6540 size_t nmatch
, regmatch_t pmatch
[_Restrict_arr_
], int eflags
)
6543 struct re_registers regs
;
6544 regex_t private_preg
;
6545 size_t len
= strlen (string
);
6546 boolean want_reg_info
= !preg
->no_sub
&& nmatch
> 0 && pmatch
;
6548 private_preg
= *preg
;
6550 private_preg
.not_bol
= !!(eflags
& REG_NOTBOL
);
6551 private_preg
.not_eol
= !!(eflags
& REG_NOTEOL
);
6553 /* The user has told us exactly how many registers to return
6554 information about, via `nmatch'. We have to pass that on to the
6555 matching routines. */
6556 private_preg
.regs_allocated
= REGS_FIXED
;
6560 regs
.num_regs
= nmatch
;
6561 regs
.start
= TALLOC (nmatch
* 2, regoff_t
);
6562 if (regs
.start
== NULL
)
6564 regs
.end
= regs
.start
+ nmatch
;
6567 /* Instead of using not_eol to implement REG_NOTEOL, we could simply
6568 pass (&private_preg, string, len + 1, 0, len, ...) pretending the string
6569 was a little bit longer but still only matching the real part.
6570 This works because the `endline' will check for a '\n' and will find a
6571 '\0', correctly deciding that this is not the end of a line.
6572 But it doesn't work out so nicely for REG_NOTBOL, since we don't have
6573 a convenient '\0' there. For all we know, the string could be preceded
6574 by '\n' which would throw things off. */
6576 /* Perform the searching operation. */
6577 ret
= re_search (&private_preg
, string
, len
,
6578 /* start: */ 0, /* range: */ len
,
6579 want_reg_info
? ®s
: 0);
6581 /* Copy the register information to the POSIX structure. */
6588 for (r
= 0; r
< nmatch
; r
++)
6590 pmatch
[r
].rm_so
= regs
.start
[r
];
6591 pmatch
[r
].rm_eo
= regs
.end
[r
];
6595 /* If we needed the temporary register info, free the space now. */
6599 /* We want zero return to mean success, unlike `re_search'. */
6600 return ret
>= 0 ? REG_NOERROR
: REG_NOMATCH
;
6602 WEAK_ALIAS (__regexec
, regexec
)
6605 /* Returns a message corresponding to an error code, ERR_CODE, returned
6606 from either regcomp or regexec. We don't use PREG here.
6608 ERR_CODE was previously called ERRCODE, but that name causes an
6609 error with msvc8 compiler. */
6612 regerror (int err_code
, const regex_t
*preg
, char *errbuf
, size_t errbuf_size
)
6618 || err_code
>= (sizeof (re_error_msgid
) / sizeof (re_error_msgid
[0])))
6619 /* Only error codes returned by the rest of the code should be passed
6620 to this routine. If we are given anything else, or if other regex
6621 code generates an invalid error code, then the program has a bug.
6622 Dump core so we can fix it. */
6625 msg
= gettext (re_error_msgid
[err_code
]);
6627 msg_size
= strlen (msg
) + 1; /* Includes the null. */
6629 if (errbuf_size
!= 0)
6631 if (msg_size
> errbuf_size
)
6633 memcpy (errbuf
, msg
, errbuf_size
- 1);
6634 errbuf
[errbuf_size
- 1] = 0;
6637 strcpy (errbuf
, msg
);
6642 WEAK_ALIAS (__regerror
, regerror
)
6645 /* Free dynamically allocated space used by PREG. */
6648 regfree (regex_t
*preg
)
6650 free (preg
->buffer
);
6651 preg
->buffer
= NULL
;
6653 preg
->allocated
= 0;
6656 free (preg
->fastmap
);
6657 preg
->fastmap
= NULL
;
6658 preg
->fastmap_accurate
= 0;
6660 free (preg
->translate
);
6661 preg
->translate
= NULL
;
6663 WEAK_ALIAS (__regfree
, regfree
)
6665 #endif /* not emacs */