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-2017 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 <https://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"
56 /* We need this for `regex.h', and perhaps for the Emacs include files. */
57 # include <sys/types.h>
60 /* Whether to use ISO C Amendment 1 wide char functions.
61 Those should not be used for Emacs since it uses its own. */
63 #define WIDE_CHAR_SUPPORT 1
65 #define WIDE_CHAR_SUPPORT \
66 (HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_BTOWC && !emacs)
69 /* For platform which support the ISO C amendment 1 functionality we
70 support user defined character classes. */
72 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
78 /* We have to keep the namespace clean. */
79 # define regfree(preg) __regfree (preg)
80 # define regexec(pr, st, nm, pm, ef) __regexec (pr, st, nm, pm, ef)
81 # define regcomp(preg, pattern, cflags) __regcomp (preg, pattern, cflags)
82 # define regerror(err_code, preg, errbuf, errbuf_size) \
83 __regerror (err_code, preg, errbuf, errbuf_size)
84 # define re_set_registers(bu, re, nu, st, en) \
85 __re_set_registers (bu, re, nu, st, en)
86 # define re_match_2(bufp, string1, size1, string2, size2, pos, regs, stop) \
87 __re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
88 # define re_match(bufp, string, size, pos, regs) \
89 __re_match (bufp, string, size, pos, regs)
90 # define re_search(bufp, string, size, startpos, range, regs) \
91 __re_search (bufp, string, size, startpos, range, regs)
92 # define re_compile_pattern(pattern, length, bufp) \
93 __re_compile_pattern (pattern, length, bufp)
94 # define re_set_syntax(syntax) __re_set_syntax (syntax)
95 # define re_search_2(bufp, st1, s1, st2, s2, startpos, range, regs, stop) \
96 __re_search_2 (bufp, st1, s1, st2, s2, startpos, range, regs, stop)
97 # define re_compile_fastmap(bufp) __re_compile_fastmap (bufp)
99 /* Make sure we call libc's function even if the user overrides them. */
100 # define btowc __btowc
101 # define iswctype __iswctype
102 # define wctype __wctype
104 # define WEAK_ALIAS(a,b) weak_alias (a, b)
106 /* We are also using some library internals. */
107 # include <locale/localeinfo.h>
108 # include <locale/elem-hash.h>
109 # include <langinfo.h>
111 # define WEAK_ALIAS(a,b)
114 /* This is for other GNU distributions with internationalized messages. */
115 #if HAVE_LIBINTL_H || defined _LIBC
116 # include <libintl.h>
118 # define gettext(msgid) (msgid)
122 /* This define is so xgettext can find the internationalizable
124 # define gettext_noop(String) String
127 /* The `emacs' switch turns on certain matching commands
128 that make sense only in Emacs. */
132 # include "character.h"
136 # include "category.h"
138 /* Make syntax table lookup grant data in gl_state. */
139 # define SYNTAX(c) syntax_property (c, 1)
144 # define malloc xmalloc
148 # define realloc xrealloc
154 /* Converts the pointer to the char to BEG-based offset from the start. */
155 # define PTR_TO_OFFSET(d) POS_AS_IN_BUFFER (POINTER_TO_OFFSET (d))
156 /* Strings are 0-indexed, buffers are 1-indexed; we pun on the boolean
157 result to get the right base index. */
158 # define POS_AS_IN_BUFFER(p) ((p) + (NILP (re_match_object) || BUFFERP (re_match_object)))
160 # define RE_MULTIBYTE_P(bufp) ((bufp)->multibyte)
161 # define RE_TARGET_MULTIBYTE_P(bufp) ((bufp)->target_multibyte)
162 # define RE_STRING_CHAR(p, multibyte) \
163 (multibyte ? (STRING_CHAR (p)) : (*(p)))
164 # define RE_STRING_CHAR_AND_LENGTH(p, len, multibyte) \
165 (multibyte ? (STRING_CHAR_AND_LENGTH (p, len)) : ((len) = 1, *(p)))
167 # define RE_CHAR_TO_MULTIBYTE(c) UNIBYTE_TO_CHAR (c)
169 # define RE_CHAR_TO_UNIBYTE(c) CHAR_TO_BYTE_SAFE (c)
171 /* Set C a (possibly converted to multibyte) character before P. P
172 points into a string which is the virtual concatenation of STR1
173 (which ends at END1) or STR2 (which ends at END2). */
174 # define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2) \
176 if (target_multibyte) \
178 re_char *dtemp = (p) == (str2) ? (end1) : (p); \
179 re_char *dlimit = ((p) > (str2) && (p) <= (end2)) ? (str2) : (str1); \
180 while (dtemp-- > dlimit && !CHAR_HEAD_P (*dtemp)); \
181 c = STRING_CHAR (dtemp); \
185 (c = ((p) == (str2) ? (end1) : (p))[-1]); \
186 (c) = RE_CHAR_TO_MULTIBYTE (c); \
190 /* Set C a (possibly converted to multibyte) character at P, and set
191 LEN to the byte length of that character. */
192 # define GET_CHAR_AFTER(c, p, len) \
194 if (target_multibyte) \
195 (c) = STRING_CHAR_AND_LENGTH (p, len); \
200 (c) = RE_CHAR_TO_MULTIBYTE (c); \
204 #else /* not emacs */
206 /* If we are not linking with Emacs proper,
207 we can't use the relocating allocator
208 even if config.h says that we can. */
213 /* When used in Emacs's lib-src, we need xmalloc and xrealloc. */
216 xmalloc (size_t size
)
218 void *val
= malloc (size
);
221 write (STDERR_FILENO
, "virtual memory exhausted\n", 25);
228 xrealloc (void *block
, size_t size
)
231 /* We must call malloc explicitly when BLOCK is 0, since some
232 reallocs don't do this. */
236 val
= realloc (block
, size
);
239 write (STDERR_FILENO
, "virtual memory exhausted\n", 25);
248 # define malloc xmalloc
252 # define realloc xrealloc
254 # include <stdbool.h>
257 /* Define the syntax stuff for \<, \>, etc. */
259 /* Sword must be nonzero for the wordchar pattern commands in re_match_2. */
260 enum syntaxcode
{ Swhitespace
= 0, Sword
= 1, Ssymbol
= 2 };
262 /* Dummy macros for non-Emacs environments. */
263 # define MAX_MULTIBYTE_LENGTH 1
264 # define RE_MULTIBYTE_P(x) 0
265 # define RE_TARGET_MULTIBYTE_P(x) 0
266 # define WORD_BOUNDARY_P(c1, c2) (0)
267 # define BYTES_BY_CHAR_HEAD(p) (1)
268 # define PREV_CHAR_BOUNDARY(p, limit) ((p)--)
269 # define STRING_CHAR(p) (*(p))
270 # define RE_STRING_CHAR(p, multibyte) STRING_CHAR (p)
271 # define CHAR_STRING(c, s) (*(s) = (c), 1)
272 # define STRING_CHAR_AND_LENGTH(p, actual_len) ((actual_len) = 1, *(p))
273 # define RE_STRING_CHAR_AND_LENGTH(p, len, multibyte) STRING_CHAR_AND_LENGTH (p, len)
274 # define RE_CHAR_TO_MULTIBYTE(c) (c)
275 # define RE_CHAR_TO_UNIBYTE(c) (c)
276 # define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2) \
277 (c = ((p) == (str2) ? *((end1) - 1) : *((p) - 1)))
278 # define GET_CHAR_AFTER(c, p, len) \
280 # define CHAR_BYTE8_P(c) (0)
281 # define CHAR_LEADING_CODE(c) (c)
283 #endif /* not emacs */
286 # define RE_TRANSLATE(TBL, C) ((unsigned char)(TBL)[C])
287 # define RE_TRANSLATE_P(TBL) (TBL)
290 /* Get the interface, including the syntax bits. */
293 /* isalpha etc. are used for the character classes. */
298 /* 1 if C is an ASCII character. */
299 # define IS_REAL_ASCII(c) ((c) < 0200)
301 /* 1 if C is a unibyte character. */
302 # define ISUNIBYTE(c) (SINGLE_BYTE_CHAR_P ((c)))
304 /* The Emacs definitions should not be directly affected by locales. */
306 /* In Emacs, these are only used for single-byte characters. */
307 # define ISDIGIT(c) ((c) >= '0' && (c) <= '9')
308 # define ISCNTRL(c) ((c) < ' ')
309 # define ISXDIGIT(c) (0 <= char_hexdigit (c))
311 /* The rest must handle multibyte characters. */
313 # define ISBLANK(c) (IS_REAL_ASCII (c) \
314 ? ((c) == ' ' || (c) == '\t') \
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')) \
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
433 in Emacs (note that SAFE_ALLOCA could also call malloc, but does so
434 via `record_xmalloc' which uses `unwind_protect' to ensure the
435 memory is freed even in case of non-local exits); also, malloc is
436 slower and causes storage fragmentation. On the other hand, malloc
437 is more portable, and easier to debug.
439 Because we sometimes use alloca, some routines have to be macros,
440 not functions -- `alloca'-allocated space disappears at the end of the
441 function it is called in. */
445 # define REGEX_ALLOCATE malloc
446 # define REGEX_REALLOCATE(source, osize, nsize) realloc (source, nsize)
447 # define REGEX_FREE free
449 #else /* not REGEX_MALLOC */
452 /* This may be adjusted in main(), if the stack is successfully grown. */
453 ptrdiff_t emacs_re_safe_alloca
= MAX_ALLOCA
;
454 /* Like USE_SAFE_ALLOCA, but use emacs_re_safe_alloca. */
455 # define REGEX_USE_SAFE_ALLOCA \
456 ptrdiff_t sa_avail = emacs_re_safe_alloca; \
457 ptrdiff_t sa_count = SPECPDL_INDEX (); bool sa_must_free = false
459 # define REGEX_SAFE_FREE() SAFE_FREE ()
460 # define REGEX_ALLOCATE SAFE_ALLOCA
463 # define REGEX_ALLOCATE alloca
466 /* Assumes a `char *destination' variable. */
467 # define REGEX_REALLOCATE(source, osize, nsize) \
468 (destination = REGEX_ALLOCATE (nsize), \
469 memcpy (destination, source, osize))
471 /* No need to do anything to free, after alloca. */
472 # define REGEX_FREE(arg) ((void)0) /* Do nothing! But inhibit gcc warning. */
474 #endif /* not REGEX_MALLOC */
476 #ifndef REGEX_USE_SAFE_ALLOCA
477 # define REGEX_USE_SAFE_ALLOCA ((void) 0)
478 # define REGEX_SAFE_FREE() ((void) 0)
481 /* Define how to allocate the failure stack. */
483 #if defined REL_ALLOC && defined REGEX_MALLOC
485 # define REGEX_ALLOCATE_STACK(size) \
486 r_alloc (&failure_stack_ptr, (size))
487 # define REGEX_REALLOCATE_STACK(source, osize, nsize) \
488 r_re_alloc (&failure_stack_ptr, (nsize))
489 # define REGEX_FREE_STACK(ptr) \
490 r_alloc_free (&failure_stack_ptr)
492 #else /* not using relocating allocator */
494 # define REGEX_ALLOCATE_STACK(size) REGEX_ALLOCATE (size)
495 # define REGEX_REALLOCATE_STACK(source, o, n) REGEX_REALLOCATE (source, o, n)
496 # define REGEX_FREE_STACK(ptr) REGEX_FREE (ptr)
498 #endif /* not using relocating allocator */
501 /* True if `size1' is non-NULL and PTR is pointing anywhere inside
502 `string1' or just past its end. This works if PTR is NULL, which is
504 #define FIRST_STRING_P(ptr) \
505 (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
507 /* (Re)Allocate N items of type T using malloc, or fail. */
508 #define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t)))
509 #define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
510 #define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
512 #define BYTEWIDTH 8 /* In bits. */
517 # define max(a, b) ((a) > (b) ? (a) : (b))
518 # define min(a, b) ((a) < (b) ? (a) : (b))
521 /* Type of source-pattern and string chars. */
522 typedef const unsigned char re_char
;
524 typedef char boolean
;
526 static regoff_t
re_match_2_internal (struct re_pattern_buffer
*bufp
,
527 re_char
*string1
, size_t size1
,
528 re_char
*string2
, size_t size2
,
530 struct re_registers
*regs
,
533 /* These are the command codes that appear in compiled regular
534 expressions. Some opcodes are followed by argument bytes. A
535 command code can specify any interpretation whatsoever for its
536 arguments. Zero bytes may appear in the compiled regular expression. */
542 /* Succeed right away--no more backtracking. */
545 /* Followed by one byte giving n, then by n literal bytes. */
548 /* Matches any (more or less) character. */
551 /* Matches any one char belonging to specified set. First
552 following byte is number of bitmap bytes. Then come bytes
553 for a bitmap saying which chars are in. Bits in each byte
554 are ordered low-bit-first. A character is in the set if its
555 bit is 1. A character too large to have a bit in the map is
556 automatically not in the set.
558 If the length byte has the 0x80 bit set, then that stuff
559 is followed by a range table:
560 2 bytes of flags for character sets (low 8 bits, high 8 bits)
561 See RANGE_TABLE_WORK_BITS below.
562 2 bytes, the number of pairs that follow (upto 32767)
563 pairs, each 2 multibyte characters,
564 each multibyte character represented as 3 bytes. */
567 /* Same parameters as charset, but match any character that is
568 not one of those specified. */
571 /* Start remembering the text that is matched, for storing in a
572 register. Followed by one byte with the register number, in
573 the range 0 to one less than the pattern buffer's re_nsub
577 /* Stop remembering the text that is matched and store it in a
578 memory register. Followed by one byte with the register
579 number, in the range 0 to one less than `re_nsub' in the
583 /* Match a duplicate of something remembered. Followed by one
584 byte containing the register number. */
587 /* Fail unless at beginning of line. */
590 /* Fail unless at end of line. */
593 /* Succeeds if at beginning of buffer (if emacs) or at beginning
594 of string to be matched (if not). */
597 /* Analogously, for end of buffer/string. */
600 /* Followed by two byte relative address to which to jump. */
603 /* Followed by two-byte relative address of place to resume at
604 in case of failure. */
607 /* Like on_failure_jump, but pushes a placeholder instead of the
608 current string position when executed. */
609 on_failure_keep_string_jump
,
611 /* Just like `on_failure_jump', except that it checks that we
612 don't get stuck in an infinite loop (matching an empty string
614 on_failure_jump_loop
,
616 /* Just like `on_failure_jump_loop', except that it checks for
617 a different kind of loop (the kind that shows up with non-greedy
618 operators). This operation has to be immediately preceded
620 on_failure_jump_nastyloop
,
622 /* A smart `on_failure_jump' used for greedy * and + operators.
623 It analyzes the loop before which it is put and if the
624 loop does not require backtracking, it changes itself to
625 `on_failure_keep_string_jump' and short-circuits the loop,
626 else it just defaults to changing itself into `on_failure_jump'.
627 It assumes that it is pointing to just past a `jump'. */
628 on_failure_jump_smart
,
630 /* Followed by two-byte relative address and two-byte number n.
631 After matching N times, jump to the address upon failure.
632 Does not work if N starts at 0: use on_failure_jump_loop
636 /* Followed by two-byte relative address, and two-byte number n.
637 Jump to the address N times, then fail. */
640 /* Set the following two-byte relative address to the
641 subsequent two-byte number. The address *includes* the two
645 wordbeg
, /* Succeeds if at word beginning. */
646 wordend
, /* Succeeds if at word end. */
648 wordbound
, /* Succeeds if at a word boundary. */
649 notwordbound
, /* Succeeds if not at a word boundary. */
651 symbeg
, /* Succeeds if at symbol beginning. */
652 symend
, /* Succeeds if at symbol end. */
654 /* Matches any character whose syntax is specified. Followed by
655 a byte which contains a syntax code, e.g., Sword. */
658 /* Matches any character whose syntax is not that specified. */
662 , at_dot
, /* Succeeds if at point. */
664 /* Matches any character whose category-set contains the specified
665 category. The operator is followed by a byte which contains a
666 category code (mnemonic ASCII character). */
669 /* Matches any character whose category-set does not contain the
670 specified category. The operator is followed by a byte which
671 contains the category code (mnemonic ASCII character). */
676 /* Common operations on the compiled pattern. */
678 /* Store NUMBER in two contiguous bytes starting at DESTINATION. */
680 #define STORE_NUMBER(destination, number) \
682 (destination)[0] = (number) & 0377; \
683 (destination)[1] = (number) >> 8; \
686 /* Same as STORE_NUMBER, except increment DESTINATION to
687 the byte after where the number is stored. Therefore, DESTINATION
688 must be an lvalue. */
690 #define STORE_NUMBER_AND_INCR(destination, number) \
692 STORE_NUMBER (destination, number); \
693 (destination) += 2; \
696 /* Put into DESTINATION a number stored in two contiguous bytes starting
699 #define EXTRACT_NUMBER(destination, source) \
700 ((destination) = extract_number (source))
703 extract_number (re_char
*source
)
705 unsigned leading_byte
= SIGN_EXTEND_CHAR (source
[1]);
706 return (leading_byte
<< 8) + source
[0];
709 /* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
710 SOURCE must be an lvalue. */
712 #define EXTRACT_NUMBER_AND_INCR(destination, source) \
713 ((destination) = extract_number_and_incr (&source))
716 extract_number_and_incr (re_char
**source
)
718 int num
= extract_number (*source
);
723 /* Store a multibyte character in three contiguous bytes starting
724 DESTINATION, and increment DESTINATION to the byte after where the
725 character is stored. Therefore, DESTINATION must be an lvalue. */
727 #define STORE_CHARACTER_AND_INCR(destination, character) \
729 (destination)[0] = (character) & 0377; \
730 (destination)[1] = ((character) >> 8) & 0377; \
731 (destination)[2] = (character) >> 16; \
732 (destination) += 3; \
735 /* Put into DESTINATION a character stored in three contiguous bytes
736 starting at SOURCE. */
738 #define EXTRACT_CHARACTER(destination, source) \
740 (destination) = ((source)[0] \
741 | ((source)[1] << 8) \
742 | ((source)[2] << 16)); \
746 /* Macros for charset. */
748 /* Size of bitmap of charset P in bytes. P is a start of charset,
749 i.e. *P is (re_opcode_t) charset or (re_opcode_t) charset_not. */
750 #define CHARSET_BITMAP_SIZE(p) ((p)[1] & 0x7F)
752 /* Nonzero if charset P has range table. */
753 #define CHARSET_RANGE_TABLE_EXISTS_P(p) ((p)[1] & 0x80)
755 /* Return the address of range table of charset P. But not the start
756 of table itself, but the before where the number of ranges is
757 stored. `2 +' means to skip re_opcode_t and size of bitmap,
758 and the 2 bytes of flags at the start of the range table. */
759 #define CHARSET_RANGE_TABLE(p) (&(p)[4 + CHARSET_BITMAP_SIZE (p)])
762 /* Extract the bit flags that start a range table. */
763 #define CHARSET_RANGE_TABLE_BITS(p) \
764 ((p)[2 + CHARSET_BITMAP_SIZE (p)] \
765 + (p)[3 + CHARSET_BITMAP_SIZE (p)] * 0x100)
768 /* Return the address of end of RANGE_TABLE. COUNT is number of
769 ranges (which is a pair of (start, end)) in the RANGE_TABLE. `* 2'
770 is start of range and end of range. `* 3' is size of each start
772 #define CHARSET_RANGE_TABLE_END(range_table, count) \
773 ((range_table) + (count) * 2 * 3)
775 /* If DEBUG is defined, Regex prints many voluminous messages about what
776 it is doing (if the variable `debug' is nonzero). If linked with the
777 main program in `iregex.c', you can enter patterns and strings
778 interactively. And if linked with the main program in `main.c' and
779 the other test files, you can run the already-written tests. */
783 /* We use standard I/O for debugging. */
786 /* It is useful to test things that ``must'' be true when debugging. */
789 static int debug
= -100000;
791 # define DEBUG_STATEMENT(e) e
792 # define DEBUG_PRINT(...) if (debug > 0) printf (__VA_ARGS__)
793 # define DEBUG_COMPILES_ARGUMENTS
794 # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) \
795 if (debug > 0) print_partial_compiled_pattern (s, e)
796 # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \
797 if (debug > 0) print_double_string (w, s1, sz1, s2, sz2)
800 /* Print the fastmap in human-readable form. */
803 print_fastmap (char *fastmap
)
805 unsigned was_a_range
= 0;
808 while (i
< (1 << BYTEWIDTH
))
814 while (i
< (1 << BYTEWIDTH
) && fastmap
[i
])
830 /* Print a compiled pattern string in human-readable form, starting at
831 the START pointer into it and ending just before the pointer END. */
834 print_partial_compiled_pattern (re_char
*start
, re_char
*end
)
842 fprintf (stderr
, "(null)\n");
846 /* Loop over pattern commands. */
849 fprintf (stderr
, "%td:\t", p
- start
);
851 switch ((re_opcode_t
) *p
++)
854 fprintf (stderr
, "/no_op");
858 fprintf (stderr
, "/succeed");
863 fprintf (stderr
, "/exactn/%d", mcnt
);
866 fprintf (stderr
, "/%c", *p
++);
872 fprintf (stderr
, "/start_memory/%d", *p
++);
876 fprintf (stderr
, "/stop_memory/%d", *p
++);
880 fprintf (stderr
, "/duplicate/%d", *p
++);
884 fprintf (stderr
, "/anychar");
890 register int c
, last
= -100;
891 register int in_range
= 0;
892 int length
= CHARSET_BITMAP_SIZE (p
- 1);
893 int has_range_table
= CHARSET_RANGE_TABLE_EXISTS_P (p
- 1);
895 fprintf (stderr
, "/charset [%s",
896 (re_opcode_t
) *(p
- 1) == charset_not
? "^" : "");
899 fprintf (stderr
, " !extends past end of pattern! ");
901 for (c
= 0; c
< 256; c
++)
903 && (p
[1 + (c
/8)] & (1 << (c
% 8))))
905 /* Are we starting a range? */
906 if (last
+ 1 == c
&& ! in_range
)
908 fprintf (stderr
, "-");
911 /* Have we broken a range? */
912 else if (last
+ 1 != c
&& in_range
)
914 fprintf (stderr
, "%c", last
);
919 fprintf (stderr
, "%c", c
);
925 fprintf (stderr
, "%c", last
);
927 fprintf (stderr
, "]");
934 fprintf (stderr
, "has-range-table");
936 /* ??? Should print the range table; for now, just skip it. */
937 p
+= 2; /* skip range table bits */
938 EXTRACT_NUMBER_AND_INCR (count
, p
);
939 p
= CHARSET_RANGE_TABLE_END (p
, count
);
945 fprintf (stderr
, "/begline");
949 fprintf (stderr
, "/endline");
952 case on_failure_jump
:
953 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
954 fprintf (stderr
, "/on_failure_jump to %td", p
+ mcnt
- start
);
957 case on_failure_keep_string_jump
:
958 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
959 fprintf (stderr
, "/on_failure_keep_string_jump to %td",
963 case on_failure_jump_nastyloop
:
964 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
965 fprintf (stderr
, "/on_failure_jump_nastyloop to %td",
969 case on_failure_jump_loop
:
970 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
971 fprintf (stderr
, "/on_failure_jump_loop to %td",
975 case on_failure_jump_smart
:
976 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
977 fprintf (stderr
, "/on_failure_jump_smart to %td",
982 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
983 fprintf (stderr
, "/jump to %td", p
+ mcnt
- start
);
987 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
988 EXTRACT_NUMBER_AND_INCR (mcnt2
, p
);
989 fprintf (stderr
, "/succeed_n to %td, %d times",
990 p
- 2 + mcnt
- start
, mcnt2
);
994 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
995 EXTRACT_NUMBER_AND_INCR (mcnt2
, p
);
996 fprintf (stderr
, "/jump_n to %td, %d times",
997 p
- 2 + mcnt
- start
, mcnt2
);
1001 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
1002 EXTRACT_NUMBER_AND_INCR (mcnt2
, p
);
1003 fprintf (stderr
, "/set_number_at location %td to %d",
1004 p
- 2 + mcnt
- start
, mcnt2
);
1008 fprintf (stderr
, "/wordbound");
1012 fprintf (stderr
, "/notwordbound");
1016 fprintf (stderr
, "/wordbeg");
1020 fprintf (stderr
, "/wordend");
1024 fprintf (stderr
, "/symbeg");
1028 fprintf (stderr
, "/symend");
1032 fprintf (stderr
, "/syntaxspec");
1034 fprintf (stderr
, "/%d", mcnt
);
1038 fprintf (stderr
, "/notsyntaxspec");
1040 fprintf (stderr
, "/%d", mcnt
);
1045 fprintf (stderr
, "/at_dot");
1049 fprintf (stderr
, "/categoryspec");
1051 fprintf (stderr
, "/%d", mcnt
);
1054 case notcategoryspec
:
1055 fprintf (stderr
, "/notcategoryspec");
1057 fprintf (stderr
, "/%d", mcnt
);
1062 fprintf (stderr
, "/begbuf");
1066 fprintf (stderr
, "/endbuf");
1070 fprintf (stderr
, "?%d", *(p
-1));
1073 fprintf (stderr
, "\n");
1076 fprintf (stderr
, "%td:\tend of pattern.\n", p
- start
);
1081 print_compiled_pattern (struct re_pattern_buffer
*bufp
)
1083 re_char
*buffer
= bufp
->buffer
;
1085 print_partial_compiled_pattern (buffer
, buffer
+ bufp
->used
);
1086 printf ("%ld bytes used/%ld bytes allocated.\n",
1087 bufp
->used
, bufp
->allocated
);
1089 if (bufp
->fastmap_accurate
&& bufp
->fastmap
)
1091 printf ("fastmap: ");
1092 print_fastmap (bufp
->fastmap
);
1095 printf ("re_nsub: %zu\t", bufp
->re_nsub
);
1096 printf ("regs_alloc: %d\t", bufp
->regs_allocated
);
1097 printf ("can_be_null: %d\t", bufp
->can_be_null
);
1098 printf ("no_sub: %d\t", bufp
->no_sub
);
1099 printf ("not_bol: %d\t", bufp
->not_bol
);
1100 printf ("not_eol: %d\t", bufp
->not_eol
);
1102 printf ("syntax: %lx\n", bufp
->syntax
);
1105 /* Perhaps we should print the translate table? */
1110 print_double_string (re_char
*where
, re_char
*string1
, ssize_t size1
,
1111 re_char
*string2
, ssize_t size2
)
1119 if (FIRST_STRING_P (where
))
1121 for (this_char
= where
- string1
; this_char
< size1
; this_char
++)
1122 putchar (string1
[this_char
]);
1127 for (this_char
= where
- string2
; this_char
< size2
; this_char
++)
1128 putchar (string2
[this_char
]);
1132 #else /* not DEBUG */
1137 # define DEBUG_STATEMENT(e)
1138 # define DEBUG_PRINT(...)
1139 # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
1140 # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
1142 #endif /* not DEBUG */
1146 /* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
1147 also be assigned to arbitrarily: each pattern buffer stores its own
1148 syntax, so it can be changed between regex compilations. */
1149 /* This has no initializer because initialized variables in Emacs
1150 become read-only after dumping. */
1151 reg_syntax_t re_syntax_options
;
1154 /* Specify the precise syntax of regexps for compilation. This provides
1155 for compatibility for various utilities which historically have
1156 different, incompatible syntaxes.
1158 The argument SYNTAX is a bit mask comprised of the various bits
1159 defined in regex.h. We return the old syntax. */
1162 re_set_syntax (reg_syntax_t syntax
)
1164 reg_syntax_t ret
= re_syntax_options
;
1166 re_syntax_options
= syntax
;
1169 WEAK_ALIAS (__re_set_syntax
, re_set_syntax
)
1173 /* This table gives an error message for each of the error codes listed
1174 in regex.h. Obviously the order here has to be same as there.
1175 POSIX doesn't require that we do anything for REG_NOERROR,
1176 but why not be nice? */
1178 static const char *re_error_msgid
[] =
1180 gettext_noop ("Success"), /* REG_NOERROR */
1181 gettext_noop ("No match"), /* REG_NOMATCH */
1182 gettext_noop ("Invalid regular expression"), /* REG_BADPAT */
1183 gettext_noop ("Invalid collation character"), /* REG_ECOLLATE */
1184 gettext_noop ("Invalid character class name"), /* REG_ECTYPE */
1185 gettext_noop ("Trailing backslash"), /* REG_EESCAPE */
1186 gettext_noop ("Invalid back reference"), /* REG_ESUBREG */
1187 gettext_noop ("Unmatched [ or [^"), /* REG_EBRACK */
1188 gettext_noop ("Unmatched ( or \\("), /* REG_EPAREN */
1189 gettext_noop ("Unmatched \\{"), /* REG_EBRACE */
1190 gettext_noop ("Invalid content of \\{\\}"), /* REG_BADBR */
1191 gettext_noop ("Invalid range end"), /* REG_ERANGE */
1192 gettext_noop ("Memory exhausted"), /* REG_ESPACE */
1193 gettext_noop ("Invalid preceding regular expression"), /* REG_BADRPT */
1194 gettext_noop ("Premature end of regular expression"), /* REG_EEND */
1195 gettext_noop ("Regular expression too big"), /* REG_ESIZE */
1196 gettext_noop ("Unmatched ) or \\)"), /* REG_ERPAREN */
1197 gettext_noop ("Range striding over charsets") /* REG_ERANGEX */
1200 /* Whether to allocate memory during matching. */
1202 /* Define MATCH_MAY_ALLOCATE to allow the searching and matching
1203 functions allocate memory for the failure stack and registers.
1204 Normally should be defined, because otherwise searching and
1205 matching routines will have much smaller memory resources at their
1206 disposal, and therefore might fail to handle complex regexps.
1207 Therefore undefine MATCH_MAY_ALLOCATE only in the following
1208 exceptional situations:
1210 . When running on a system where memory is at premium.
1211 . When alloca cannot be used at all, perhaps due to bugs in
1212 its implementation, or its being unavailable, or due to a
1213 very small stack size. This requires to define REGEX_MALLOC
1214 to use malloc instead, which in turn could lead to memory
1215 leaks if search is interrupted by a signal. (For these
1216 reasons, defining REGEX_MALLOC when building Emacs
1217 automatically undefines MATCH_MAY_ALLOCATE, but outside
1218 Emacs you may not care about memory leaks.) If you want to
1219 prevent the memory leaks, undefine MATCH_MAY_ALLOCATE.
1220 . When code that calls the searching and matching functions
1221 cannot allow memory allocation, for whatever reasons. */
1223 /* Normally, this is fine. */
1224 #define MATCH_MAY_ALLOCATE
1226 /* The match routines may not allocate if (1) they would do it with malloc
1227 and (2) it's not safe for them to use malloc.
1228 Note that if REL_ALLOC is defined, matching would not use malloc for the
1229 failure stack, but we would still use it for the register vectors;
1230 so REL_ALLOC should not affect this. */
1231 #if defined REGEX_MALLOC && defined emacs
1232 # undef MATCH_MAY_ALLOCATE
1236 /* Failure stack declarations and macros; both re_compile_fastmap and
1237 re_match_2 use a failure stack. These have to be macros because of
1238 REGEX_ALLOCATE_STACK. */
1241 /* Approximate number of failure points for which to initially allocate space
1242 when matching. If this number is exceeded, we allocate more
1243 space, so it is not a hard limit. */
1244 #ifndef INIT_FAILURE_ALLOC
1245 # define INIT_FAILURE_ALLOC 20
1248 /* Roughly the maximum number of failure points on the stack. Would be
1249 exactly that if always used TYPICAL_FAILURE_SIZE items each time we failed.
1250 This is a variable only so users of regex can assign to it; we never
1251 change it ourselves. We always multiply it by TYPICAL_FAILURE_SIZE
1252 before using it, so it should probably be a byte-count instead. */
1253 # if defined MATCH_MAY_ALLOCATE
1254 /* Note that 4400 was enough to cause a crash on Alpha OSF/1,
1255 whose default stack limit is 2mb. In order for a larger
1256 value to work reliably, you have to try to make it accord
1257 with the process stack limit. */
1258 size_t emacs_re_max_failures
= 40000;
1260 size_t emacs_re_max_failures
= 4000;
1263 union fail_stack_elt
1266 /* This should be the biggest `int' that's no bigger than a pointer. */
1270 typedef union fail_stack_elt fail_stack_elt_t
;
1274 fail_stack_elt_t
*stack
;
1276 size_t avail
; /* Offset of next open position. */
1277 size_t frame
; /* Offset of the cur constructed frame. */
1280 #define FAIL_STACK_EMPTY() (fail_stack.frame == 0)
1283 /* Define macros to initialize and free the failure stack.
1284 Do `return -2' if the alloc fails. */
1286 #ifdef MATCH_MAY_ALLOCATE
1287 # define INIT_FAIL_STACK() \
1289 fail_stack.stack = \
1290 REGEX_ALLOCATE_STACK (INIT_FAILURE_ALLOC * TYPICAL_FAILURE_SIZE \
1291 * sizeof (fail_stack_elt_t)); \
1293 if (fail_stack.stack == NULL) \
1296 fail_stack.size = INIT_FAILURE_ALLOC; \
1297 fail_stack.avail = 0; \
1298 fail_stack.frame = 0; \
1301 # define INIT_FAIL_STACK() \
1303 fail_stack.avail = 0; \
1304 fail_stack.frame = 0; \
1307 # define RETALLOC_IF(addr, n, t) \
1308 if (addr) RETALLOC((addr), (n), t); else (addr) = TALLOC ((n), t)
1312 /* Double the size of FAIL_STACK, up to a limit
1313 which allows approximately `emacs_re_max_failures' items.
1315 Return 1 if succeeds, and 0 if either ran out of memory
1316 allocating space for it or it was already too large.
1318 REGEX_REALLOCATE_STACK requires `destination' be declared. */
1320 /* Factor to increase the failure stack size by
1321 when we increase it.
1322 This used to be 2, but 2 was too wasteful
1323 because the old discarded stacks added up to as much space
1324 were as ultimate, maximum-size stack. */
1325 #define FAIL_STACK_GROWTH_FACTOR 4
1327 #define GROW_FAIL_STACK(fail_stack) \
1328 (((fail_stack).size >= emacs_re_max_failures * TYPICAL_FAILURE_SIZE) \
1330 : ((fail_stack).stack \
1331 = REGEX_REALLOCATE_STACK ((fail_stack).stack, \
1332 (fail_stack).size * sizeof (fail_stack_elt_t), \
1333 min (emacs_re_max_failures * TYPICAL_FAILURE_SIZE, \
1334 ((fail_stack).size * FAIL_STACK_GROWTH_FACTOR)) \
1335 * sizeof (fail_stack_elt_t)), \
1337 (fail_stack).stack == NULL \
1339 : ((fail_stack).size \
1340 = (min (emacs_re_max_failures * TYPICAL_FAILURE_SIZE, \
1341 ((fail_stack).size * FAIL_STACK_GROWTH_FACTOR))), \
1345 /* Push a pointer value onto the failure stack.
1346 Assumes the variable `fail_stack'. Probably should only
1347 be called from within `PUSH_FAILURE_POINT'. */
1348 #define PUSH_FAILURE_POINTER(item) \
1349 fail_stack.stack[fail_stack.avail++].pointer = (item)
1351 /* This pushes an integer-valued item onto the failure stack.
1352 Assumes the variable `fail_stack'. Probably should only
1353 be called from within `PUSH_FAILURE_POINT'. */
1354 #define PUSH_FAILURE_INT(item) \
1355 fail_stack.stack[fail_stack.avail++].integer = (item)
1357 /* These POP... operations complement the PUSH... operations.
1358 All assume that `fail_stack' is nonempty. */
1359 #define POP_FAILURE_POINTER() fail_stack.stack[--fail_stack.avail].pointer
1360 #define POP_FAILURE_INT() fail_stack.stack[--fail_stack.avail].integer
1362 /* Individual items aside from the registers. */
1363 #define NUM_NONREG_ITEMS 3
1365 /* Used to examine the stack (to detect infinite loops). */
1366 #define FAILURE_PAT(h) fail_stack.stack[(h) - 1].pointer
1367 #define FAILURE_STR(h) (fail_stack.stack[(h) - 2].pointer)
1368 #define NEXT_FAILURE_HANDLE(h) fail_stack.stack[(h) - 3].integer
1369 #define TOP_FAILURE_HANDLE() fail_stack.frame
1372 #define ENSURE_FAIL_STACK(space) \
1373 while (REMAINING_AVAIL_SLOTS <= space) { \
1374 if (!GROW_FAIL_STACK (fail_stack)) \
1376 DEBUG_PRINT ("\n Doubled stack; size now: %zd\n", (fail_stack).size);\
1377 DEBUG_PRINT (" slots available: %zd\n", REMAINING_AVAIL_SLOTS);\
1380 /* Push register NUM onto the stack. */
1381 #define PUSH_FAILURE_REG(num) \
1383 char *destination; \
1385 ENSURE_FAIL_STACK(3); \
1386 DEBUG_PRINT (" Push reg %ld (spanning %p -> %p)\n", \
1387 n, regstart[n], regend[n]); \
1388 PUSH_FAILURE_POINTER (regstart[n]); \
1389 PUSH_FAILURE_POINTER (regend[n]); \
1390 PUSH_FAILURE_INT (n); \
1393 /* Change the counter's value to VAL, but make sure that it will
1394 be reset when backtracking. */
1395 #define PUSH_NUMBER(ptr,val) \
1397 char *destination; \
1399 ENSURE_FAIL_STACK(3); \
1400 EXTRACT_NUMBER (c, ptr); \
1401 DEBUG_PRINT (" Push number %p = %d -> %d\n", ptr, c, val); \
1402 PUSH_FAILURE_INT (c); \
1403 PUSH_FAILURE_POINTER (ptr); \
1404 PUSH_FAILURE_INT (-1); \
1405 STORE_NUMBER (ptr, val); \
1408 /* Pop a saved register off the stack. */
1409 #define POP_FAILURE_REG_OR_COUNT() \
1411 long pfreg = POP_FAILURE_INT (); \
1414 /* It's a counter. */ \
1415 /* Here, we discard `const', making re_match non-reentrant. */ \
1416 unsigned char *ptr = (unsigned char *) POP_FAILURE_POINTER (); \
1417 pfreg = POP_FAILURE_INT (); \
1418 STORE_NUMBER (ptr, pfreg); \
1419 DEBUG_PRINT (" Pop counter %p = %ld\n", ptr, pfreg); \
1423 regend[pfreg] = POP_FAILURE_POINTER (); \
1424 regstart[pfreg] = POP_FAILURE_POINTER (); \
1425 DEBUG_PRINT (" Pop reg %ld (spanning %p -> %p)\n", \
1426 pfreg, regstart[pfreg], regend[pfreg]); \
1430 /* Check that we are not stuck in an infinite loop. */
1431 #define CHECK_INFINITE_LOOP(pat_cur, string_place) \
1433 ssize_t failure = TOP_FAILURE_HANDLE (); \
1434 /* Check for infinite matching loops */ \
1435 while (failure > 0 \
1436 && (FAILURE_STR (failure) == string_place \
1437 || FAILURE_STR (failure) == NULL)) \
1439 assert (FAILURE_PAT (failure) >= bufp->buffer \
1440 && FAILURE_PAT (failure) <= bufp->buffer + bufp->used); \
1441 if (FAILURE_PAT (failure) == pat_cur) \
1446 DEBUG_PRINT (" Other pattern: %p\n", FAILURE_PAT (failure)); \
1447 failure = NEXT_FAILURE_HANDLE(failure); \
1449 DEBUG_PRINT (" Other string: %p\n", FAILURE_STR (failure)); \
1452 /* Push the information about the state we will need
1453 if we ever fail back to it.
1455 Requires variables fail_stack, regstart, regend and
1456 num_regs be declared. GROW_FAIL_STACK requires `destination' be
1459 Does `return FAILURE_CODE' if runs out of memory. */
1461 #define PUSH_FAILURE_POINT(pattern, string_place) \
1463 char *destination; \
1464 /* Must be int, so when we don't save any registers, the arithmetic \
1465 of 0 + -1 isn't done as unsigned. */ \
1467 DEBUG_STATEMENT (nfailure_points_pushed++); \
1468 DEBUG_PRINT ("\nPUSH_FAILURE_POINT:\n"); \
1469 DEBUG_PRINT (" Before push, next avail: %zd\n", (fail_stack).avail); \
1470 DEBUG_PRINT (" size: %zd\n", (fail_stack).size);\
1472 ENSURE_FAIL_STACK (NUM_NONREG_ITEMS); \
1474 DEBUG_PRINT ("\n"); \
1476 DEBUG_PRINT (" Push frame index: %zd\n", fail_stack.frame); \
1477 PUSH_FAILURE_INT (fail_stack.frame); \
1479 DEBUG_PRINT (" Push string %p: \"", string_place); \
1480 DEBUG_PRINT_DOUBLE_STRING (string_place, string1, size1, string2, size2);\
1481 DEBUG_PRINT ("\"\n"); \
1482 PUSH_FAILURE_POINTER (string_place); \
1484 DEBUG_PRINT (" Push pattern %p: ", pattern); \
1485 DEBUG_PRINT_COMPILED_PATTERN (bufp, pattern, pend); \
1486 PUSH_FAILURE_POINTER (pattern); \
1488 /* Close the frame by moving the frame pointer past it. */ \
1489 fail_stack.frame = fail_stack.avail; \
1492 /* Estimate the size of data pushed by a typical failure stack entry.
1493 An estimate is all we need, because all we use this for
1494 is to choose a limit for how big to make the failure stack. */
1495 /* BEWARE, the value `20' is hard-coded in emacs.c:main(). */
1496 #define TYPICAL_FAILURE_SIZE 20
1498 /* How many items can still be added to the stack without overflowing it. */
1499 #define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail)
1502 /* Pops what PUSH_FAIL_STACK pushes.
1504 We restore into the parameters, all of which should be lvalues:
1505 STR -- the saved data position.
1506 PAT -- the saved pattern position.
1507 REGSTART, REGEND -- arrays of string positions.
1509 Also assumes the variables `fail_stack' and (if debugging), `bufp',
1510 `pend', `string1', `size1', `string2', and `size2'. */
1512 #define POP_FAILURE_POINT(str, pat) \
1514 assert (!FAIL_STACK_EMPTY ()); \
1516 /* Remove failure points and point to how many regs pushed. */ \
1517 DEBUG_PRINT ("POP_FAILURE_POINT:\n"); \
1518 DEBUG_PRINT (" Before pop, next avail: %zd\n", fail_stack.avail); \
1519 DEBUG_PRINT (" size: %zd\n", fail_stack.size); \
1521 /* Pop the saved registers. */ \
1522 while (fail_stack.frame < fail_stack.avail) \
1523 POP_FAILURE_REG_OR_COUNT (); \
1525 pat = POP_FAILURE_POINTER (); \
1526 DEBUG_PRINT (" Popping pattern %p: ", pat); \
1527 DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend); \
1529 /* If the saved string location is NULL, it came from an \
1530 on_failure_keep_string_jump opcode, and we want to throw away the \
1531 saved NULL, thus retaining our current position in the string. */ \
1532 str = POP_FAILURE_POINTER (); \
1533 DEBUG_PRINT (" Popping string %p: \"", str); \
1534 DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2); \
1535 DEBUG_PRINT ("\"\n"); \
1537 fail_stack.frame = POP_FAILURE_INT (); \
1538 DEBUG_PRINT (" Popping frame index: %zd\n", fail_stack.frame); \
1540 assert (fail_stack.avail >= 0); \
1541 assert (fail_stack.frame <= fail_stack.avail); \
1543 DEBUG_STATEMENT (nfailure_points_popped++); \
1544 } while (0) /* POP_FAILURE_POINT */
1548 /* Registers are set to a sentinel when they haven't yet matched. */
1549 #define REG_UNSET(e) ((e) == NULL)
1551 /* Subroutine declarations and macros for regex_compile. */
1553 static reg_errcode_t
regex_compile (re_char
*pattern
, size_t size
,
1555 bool posix_backtracking
,
1556 const char *whitespace_regexp
,
1558 reg_syntax_t syntax
,
1560 struct re_pattern_buffer
*bufp
);
1561 static void store_op1 (re_opcode_t op
, unsigned char *loc
, int arg
);
1562 static void store_op2 (re_opcode_t op
, unsigned char *loc
, int arg1
, int arg2
);
1563 static void insert_op1 (re_opcode_t op
, unsigned char *loc
,
1564 int arg
, unsigned char *end
);
1565 static void insert_op2 (re_opcode_t op
, unsigned char *loc
,
1566 int arg1
, int arg2
, unsigned char *end
);
1567 static boolean
at_begline_loc_p (re_char
*pattern
, re_char
*p
,
1568 reg_syntax_t syntax
);
1569 static boolean
at_endline_loc_p (re_char
*p
, re_char
*pend
,
1570 reg_syntax_t syntax
);
1571 static re_char
*skip_one_char (re_char
*p
);
1572 static int analyze_first (re_char
*p
, re_char
*pend
,
1573 char *fastmap
, const int multibyte
);
1575 /* Fetch the next character in the uncompiled pattern, with no
1577 #define PATFETCH(c) \
1580 if (p == pend) return REG_EEND; \
1581 c = RE_STRING_CHAR_AND_LENGTH (p, len, multibyte); \
1586 /* If `translate' is non-null, return translate[D], else just D. We
1587 cast the subscript to translate because some data is declared as
1588 `char *', to avoid warnings when a string constant is passed. But
1589 when we use a character as a subscript we must make it unsigned. */
1591 # define TRANSLATE(d) \
1592 (RE_TRANSLATE_P (translate) ? RE_TRANSLATE (translate, (d)) : (d))
1596 /* Macros for outputting the compiled pattern into `buffer'. */
1598 /* If the buffer isn't allocated when it comes in, use this. */
1599 #define INIT_BUF_SIZE 32
1601 /* Make sure we have at least N more bytes of space in buffer. */
1602 #define GET_BUFFER_SPACE(n) \
1603 while ((size_t) (b - bufp->buffer + (n)) > bufp->allocated) \
1606 /* Make sure we have one more byte of buffer space and then add C to it. */
1607 #define BUF_PUSH(c) \
1609 GET_BUFFER_SPACE (1); \
1610 *b++ = (unsigned char) (c); \
1614 /* Ensure we have two more bytes of buffer space and then append C1 and C2. */
1615 #define BUF_PUSH_2(c1, c2) \
1617 GET_BUFFER_SPACE (2); \
1618 *b++ = (unsigned char) (c1); \
1619 *b++ = (unsigned char) (c2); \
1623 /* Store a jump with opcode OP at LOC to location TO. We store a
1624 relative address offset by the three bytes the jump itself occupies. */
1625 #define STORE_JUMP(op, loc, to) \
1626 store_op1 (op, loc, (to) - (loc) - 3)
1628 /* Likewise, for a two-argument jump. */
1629 #define STORE_JUMP2(op, loc, to, arg) \
1630 store_op2 (op, loc, (to) - (loc) - 3, arg)
1632 /* Like `STORE_JUMP', but for inserting. Assume `b' is the buffer end. */
1633 #define INSERT_JUMP(op, loc, to) \
1634 insert_op1 (op, loc, (to) - (loc) - 3, b)
1636 /* Like `STORE_JUMP2', but for inserting. Assume `b' is the buffer end. */
1637 #define INSERT_JUMP2(op, loc, to, arg) \
1638 insert_op2 (op, loc, (to) - (loc) - 3, arg, b)
1641 /* This is not an arbitrary limit: the arguments which represent offsets
1642 into the pattern are two bytes long. So if 2^15 bytes turns out to
1643 be too small, many things would have to change. */
1644 # define MAX_BUF_SIZE (1L << 15)
1646 /* Extend the buffer by twice its current size via realloc and
1647 reset the pointers that pointed into the old block to point to the
1648 correct places in the new one. If extending the buffer results in it
1649 being larger than MAX_BUF_SIZE, then flag memory exhausted. */
1650 #define EXTEND_BUFFER() \
1652 unsigned char *old_buffer = bufp->buffer; \
1653 if (bufp->allocated == MAX_BUF_SIZE) \
1655 bufp->allocated <<= 1; \
1656 if (bufp->allocated > MAX_BUF_SIZE) \
1657 bufp->allocated = MAX_BUF_SIZE; \
1658 ptrdiff_t b_off = b - old_buffer; \
1659 ptrdiff_t begalt_off = begalt - old_buffer; \
1660 bool fixup_alt_jump_set = !!fixup_alt_jump; \
1661 bool laststart_set = !!laststart; \
1662 bool pending_exact_set = !!pending_exact; \
1663 ptrdiff_t fixup_alt_jump_off, laststart_off, pending_exact_off; \
1664 if (fixup_alt_jump_set) fixup_alt_jump_off = fixup_alt_jump - old_buffer; \
1665 if (laststart_set) laststart_off = laststart - old_buffer; \
1666 if (pending_exact_set) pending_exact_off = pending_exact - old_buffer; \
1667 RETALLOC (bufp->buffer, bufp->allocated, unsigned char); \
1668 if (bufp->buffer == NULL) \
1669 return REG_ESPACE; \
1670 unsigned char *new_buffer = bufp->buffer; \
1671 b = new_buffer + b_off; \
1672 begalt = new_buffer + begalt_off; \
1673 if (fixup_alt_jump_set) fixup_alt_jump = new_buffer + fixup_alt_jump_off; \
1674 if (laststart_set) laststart = new_buffer + laststart_off; \
1675 if (pending_exact_set) pending_exact = new_buffer + pending_exact_off; \
1679 /* Since we have one byte reserved for the register number argument to
1680 {start,stop}_memory, the maximum number of groups we can report
1681 things about is what fits in that byte. */
1682 #define MAX_REGNUM 255
1684 /* But patterns can have more than `MAX_REGNUM' registers. We just
1685 ignore the excess. */
1686 typedef int regnum_t
;
1689 /* Macros for the compile stack. */
1691 /* Since offsets can go either forwards or backwards, this type needs to
1692 be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1. */
1693 /* int may be not enough when sizeof(int) == 2. */
1694 typedef long pattern_offset_t
;
1698 pattern_offset_t begalt_offset
;
1699 pattern_offset_t fixup_alt_jump
;
1700 pattern_offset_t laststart_offset
;
1702 } compile_stack_elt_t
;
1707 compile_stack_elt_t
*stack
;
1709 size_t avail
; /* Offset of next open position. */
1710 } compile_stack_type
;
1713 #define INIT_COMPILE_STACK_SIZE 32
1715 #define COMPILE_STACK_EMPTY (compile_stack.avail == 0)
1716 #define COMPILE_STACK_FULL (compile_stack.avail == compile_stack.size)
1718 /* The next available element. */
1719 #define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
1721 /* Explicit quit checking is needed for Emacs, which uses polling to
1722 process input events. */
1724 static void maybe_quit (void) {}
1727 /* Structure to manage work area for range table. */
1728 struct range_table_work_area
1730 int *table
; /* actual work area. */
1731 int allocated
; /* allocated size for work area in bytes. */
1732 int used
; /* actually used size in words. */
1733 int bits
; /* flag to record character classes */
1738 /* Make sure that WORK_AREA can hold more N multibyte characters.
1739 This is used only in set_image_of_range and set_image_of_range_1.
1740 It expects WORK_AREA to be a pointer.
1741 If it can't get the space, it returns from the surrounding function. */
1743 #define EXTEND_RANGE_TABLE(work_area, n) \
1745 if (((work_area).used + (n)) * sizeof (int) > (work_area).allocated) \
1747 extend_range_table_work_area (&work_area); \
1748 if ((work_area).table == 0) \
1749 return (REG_ESPACE); \
1753 #define SET_RANGE_TABLE_WORK_AREA_BIT(work_area, bit) \
1754 (work_area).bits |= (bit)
1756 /* Set a range (RANGE_START, RANGE_END) to WORK_AREA. */
1757 #define SET_RANGE_TABLE_WORK_AREA(work_area, range_start, range_end) \
1759 EXTEND_RANGE_TABLE ((work_area), 2); \
1760 (work_area).table[(work_area).used++] = (range_start); \
1761 (work_area).table[(work_area).used++] = (range_end); \
1766 /* Free allocated memory for WORK_AREA. */
1767 #define FREE_RANGE_TABLE_WORK_AREA(work_area) \
1769 if ((work_area).table) \
1770 free ((work_area).table); \
1773 #define CLEAR_RANGE_TABLE_WORK_USED(work_area) ((work_area).used = 0, (work_area).bits = 0)
1774 #define RANGE_TABLE_WORK_USED(work_area) ((work_area).used)
1775 #define RANGE_TABLE_WORK_BITS(work_area) ((work_area).bits)
1776 #define RANGE_TABLE_WORK_ELT(work_area, i) ((work_area).table[i])
1778 /* Bits used to implement the multibyte-part of the various character classes
1779 such as [:alnum:] in a charset's range table. The code currently assumes
1780 that only the low 16 bits are used. */
1781 #define BIT_WORD 0x1
1782 #define BIT_LOWER 0x2
1783 #define BIT_PUNCT 0x4
1784 #define BIT_SPACE 0x8
1785 #define BIT_UPPER 0x10
1786 #define BIT_MULTIBYTE 0x20
1787 #define BIT_ALPHA 0x40
1788 #define BIT_ALNUM 0x80
1789 #define BIT_GRAPH 0x100
1790 #define BIT_PRINT 0x200
1791 #define BIT_BLANK 0x400
1794 /* Set the bit for character C in a list. */
1795 #define SET_LIST_BIT(c) (b[((c)) / BYTEWIDTH] |= 1 << ((c) % BYTEWIDTH))
1800 /* Store characters in the range FROM to TO in the bitmap at B (for
1801 ASCII and unibyte characters) and WORK_AREA (for multibyte
1802 characters) while translating them and paying attention to the
1803 continuity of translated characters.
1805 Implementation note: It is better to implement these fairly big
1806 macros by a function, but it's not that easy because macros called
1807 in this macro assume various local variables already declared. */
1809 /* Both FROM and TO are ASCII characters. */
1811 #define SETUP_ASCII_RANGE(work_area, FROM, TO) \
1815 for (C0 = (FROM); C0 <= (TO); C0++) \
1817 C1 = TRANSLATE (C0); \
1818 if (! ASCII_CHAR_P (C1)) \
1820 SET_RANGE_TABLE_WORK_AREA ((work_area), C1, C1); \
1821 if ((C1 = RE_CHAR_TO_UNIBYTE (C1)) < 0) \
1824 SET_LIST_BIT (C1); \
1829 /* Both FROM and TO are unibyte characters (0x80..0xFF). */
1831 #define SETUP_UNIBYTE_RANGE(work_area, FROM, TO) \
1833 int C0, C1, C2, I; \
1834 int USED = RANGE_TABLE_WORK_USED (work_area); \
1836 for (C0 = (FROM); C0 <= (TO); C0++) \
1838 C1 = RE_CHAR_TO_MULTIBYTE (C0); \
1839 if (CHAR_BYTE8_P (C1)) \
1840 SET_LIST_BIT (C0); \
1843 C2 = TRANSLATE (C1); \
1845 || (C1 = RE_CHAR_TO_UNIBYTE (C2)) < 0) \
1847 SET_LIST_BIT (C1); \
1848 for (I = RANGE_TABLE_WORK_USED (work_area) - 2; I >= USED; I -= 2) \
1850 int from = RANGE_TABLE_WORK_ELT (work_area, I); \
1851 int to = RANGE_TABLE_WORK_ELT (work_area, I + 1); \
1853 if (C2 >= from - 1 && C2 <= to + 1) \
1855 if (C2 == from - 1) \
1856 RANGE_TABLE_WORK_ELT (work_area, I)--; \
1857 else if (C2 == to + 1) \
1858 RANGE_TABLE_WORK_ELT (work_area, I + 1)++; \
1863 SET_RANGE_TABLE_WORK_AREA ((work_area), C2, C2); \
1869 /* Both FROM and TO are multibyte characters. */
1871 #define SETUP_MULTIBYTE_RANGE(work_area, FROM, TO) \
1873 int C0, C1, C2, I, USED = RANGE_TABLE_WORK_USED (work_area); \
1875 SET_RANGE_TABLE_WORK_AREA ((work_area), (FROM), (TO)); \
1876 for (C0 = (FROM); C0 <= (TO); C0++) \
1878 C1 = TRANSLATE (C0); \
1879 if ((C2 = RE_CHAR_TO_UNIBYTE (C1)) >= 0 \
1880 || (C1 != C0 && (C2 = RE_CHAR_TO_UNIBYTE (C0)) >= 0)) \
1881 SET_LIST_BIT (C2); \
1882 if (C1 >= (FROM) && C1 <= (TO)) \
1884 for (I = RANGE_TABLE_WORK_USED (work_area) - 2; I >= USED; I -= 2) \
1886 int from = RANGE_TABLE_WORK_ELT (work_area, I); \
1887 int to = RANGE_TABLE_WORK_ELT (work_area, I + 1); \
1889 if (C1 >= from - 1 && C1 <= to + 1) \
1891 if (C1 == from - 1) \
1892 RANGE_TABLE_WORK_ELT (work_area, I)--; \
1893 else if (C1 == to + 1) \
1894 RANGE_TABLE_WORK_ELT (work_area, I + 1)++; \
1899 SET_RANGE_TABLE_WORK_AREA ((work_area), C1, C1); \
1905 /* Get the next unsigned number in the uncompiled pattern. */
1906 #define GET_INTERVAL_COUNT(num) \
1909 FREE_STACK_RETURN (REG_EBRACE); \
1913 while ('0' <= c && c <= '9') \
1917 if (RE_DUP_MAX / 10 - (RE_DUP_MAX % 10 < c - '0') < num) \
1918 FREE_STACK_RETURN (REG_BADBR); \
1919 num = num * 10 + c - '0'; \
1921 FREE_STACK_RETURN (REG_EBRACE); \
1927 #if ! WIDE_CHAR_SUPPORT
1929 /* Parse a character class, i.e. string such as "[:name:]". *strp
1930 points to the string to be parsed and limit is length, in bytes, of
1933 If *strp point to a string that begins with "[:name:]", where name is
1934 a non-empty sequence of lower case letters, *strp will be advanced past the
1935 closing square bracket and RECC_* constant which maps to the name will be
1936 returned. If name is not a valid character class name zero, or RECC_ERROR,
1939 Otherwise, if *strp doesn't begin with "[:name:]", -1 is returned.
1941 The function can be used on ASCII and multibyte (UTF-8-encoded) strings.
1944 re_wctype_parse (const unsigned char **strp
, unsigned limit
)
1946 const char *beg
= (const char *)*strp
, *it
;
1948 if (limit
< 4 || beg
[0] != '[' || beg
[1] != ':')
1951 beg
+= 2; /* skip opening "[:" */
1952 limit
-= 3; /* opening "[:" and half of closing ":]"; --limit handles rest */
1953 for (it
= beg
; it
[0] != ':' || it
[1] != ']'; ++it
)
1957 *strp
= (const unsigned char *)(it
+ 2);
1959 /* Sort tests in the length=five case by frequency the classes to minimize
1960 number of times we fail the comparison. The frequencies of character class
1961 names used in Emacs sources as of 2016-07-27:
1963 $ find \( -name \*.c -o -name \*.el \) -exec grep -h '\[:[a-z]*:]' {} + |
1964 sed 's/]/]\n/g' |grep -o '\[:[a-z]*:]' |sort |uniq -c |sort -nr
1982 If you update this list, consider also updating chain of or'ed conditions
1983 in execute_charset function.
1988 if (!memcmp (beg
, "word", 4)) return RECC_WORD
;
1991 if (!memcmp (beg
, "alnum", 5)) return RECC_ALNUM
;
1992 if (!memcmp (beg
, "alpha", 5)) return RECC_ALPHA
;
1993 if (!memcmp (beg
, "space", 5)) return RECC_SPACE
;
1994 if (!memcmp (beg
, "digit", 5)) return RECC_DIGIT
;
1995 if (!memcmp (beg
, "blank", 5)) return RECC_BLANK
;
1996 if (!memcmp (beg
, "upper", 5)) return RECC_UPPER
;
1997 if (!memcmp (beg
, "lower", 5)) return RECC_LOWER
;
1998 if (!memcmp (beg
, "punct", 5)) return RECC_PUNCT
;
1999 if (!memcmp (beg
, "ascii", 5)) return RECC_ASCII
;
2000 if (!memcmp (beg
, "graph", 5)) return RECC_GRAPH
;
2001 if (!memcmp (beg
, "print", 5)) return RECC_PRINT
;
2002 if (!memcmp (beg
, "cntrl", 5)) return RECC_CNTRL
;
2005 if (!memcmp (beg
, "xdigit", 6)) return RECC_XDIGIT
;
2008 if (!memcmp (beg
, "unibyte", 7)) return RECC_UNIBYTE
;
2011 if (!memcmp (beg
, "nonascii", 8)) return RECC_NONASCII
;
2014 if (!memcmp (beg
, "multibyte", 9)) return RECC_MULTIBYTE
;
2021 /* True if CH is in the char class CC. */
2023 re_iswctype (int ch
, re_wctype_t cc
)
2027 case RECC_ALNUM
: return ISALNUM (ch
) != 0;
2028 case RECC_ALPHA
: return ISALPHA (ch
) != 0;
2029 case RECC_BLANK
: return ISBLANK (ch
) != 0;
2030 case RECC_CNTRL
: return ISCNTRL (ch
) != 0;
2031 case RECC_DIGIT
: return ISDIGIT (ch
) != 0;
2032 case RECC_GRAPH
: return ISGRAPH (ch
) != 0;
2033 case RECC_LOWER
: return ISLOWER (ch
) != 0;
2034 case RECC_PRINT
: return ISPRINT (ch
) != 0;
2035 case RECC_PUNCT
: return ISPUNCT (ch
) != 0;
2036 case RECC_SPACE
: return ISSPACE (ch
) != 0;
2037 case RECC_UPPER
: return ISUPPER (ch
) != 0;
2038 case RECC_XDIGIT
: return ISXDIGIT (ch
) != 0;
2039 case RECC_ASCII
: return IS_REAL_ASCII (ch
) != 0;
2040 case RECC_NONASCII
: return !IS_REAL_ASCII (ch
);
2041 case RECC_UNIBYTE
: return ISUNIBYTE (ch
) != 0;
2042 case RECC_MULTIBYTE
: return !ISUNIBYTE (ch
);
2043 case RECC_WORD
: return ISWORD (ch
) != 0;
2044 case RECC_ERROR
: return false;
2050 /* Return a bit-pattern to use in the range-table bits to match multibyte
2051 chars of class CC. */
2053 re_wctype_to_bit (re_wctype_t cc
)
2058 case RECC_MULTIBYTE
: return BIT_MULTIBYTE
;
2059 case RECC_ALPHA
: return BIT_ALPHA
;
2060 case RECC_ALNUM
: return BIT_ALNUM
;
2061 case RECC_WORD
: return BIT_WORD
;
2062 case RECC_LOWER
: return BIT_LOWER
;
2063 case RECC_UPPER
: return BIT_UPPER
;
2064 case RECC_PUNCT
: return BIT_PUNCT
;
2065 case RECC_SPACE
: return BIT_SPACE
;
2066 case RECC_GRAPH
: return BIT_GRAPH
;
2067 case RECC_PRINT
: return BIT_PRINT
;
2068 case RECC_BLANK
: return BIT_BLANK
;
2069 case RECC_ASCII
: case RECC_DIGIT
: case RECC_XDIGIT
: case RECC_CNTRL
:
2070 case RECC_UNIBYTE
: case RECC_ERROR
: return 0;
2077 /* Filling in the work area of a range. */
2079 /* Actually extend the space in WORK_AREA. */
2082 extend_range_table_work_area (struct range_table_work_area
*work_area
)
2084 work_area
->allocated
+= 16 * sizeof (int);
2085 work_area
->table
= realloc (work_area
->table
, work_area
->allocated
);
2091 /* Carefully find the ranges of codes that are equivalent
2092 under case conversion to the range start..end when passed through
2093 TRANSLATE. Handle the case where non-letters can come in between
2094 two upper-case letters (which happens in Latin-1).
2095 Also handle the case of groups of more than 2 case-equivalent chars.
2097 The basic method is to look at consecutive characters and see
2098 if they can form a run that can be handled as one.
2100 Returns -1 if successful, REG_ESPACE if ran out of space. */
2103 set_image_of_range_1 (struct range_table_work_area
*work_area
,
2104 re_wchar_t start
, re_wchar_t end
,
2105 RE_TRANSLATE_TYPE translate
)
2107 /* `one_case' indicates a character, or a run of characters,
2108 each of which is an isolate (no case-equivalents).
2109 This includes all ASCII non-letters.
2111 `two_case' indicates a character, or a run of characters,
2112 each of which has two case-equivalent forms.
2113 This includes all ASCII letters.
2115 `strange' indicates a character that has more than one
2118 enum case_type
{one_case
, two_case
, strange
};
2120 /* Describe the run that is in progress,
2121 which the next character can try to extend.
2122 If run_type is strange, that means there really is no run.
2123 If run_type is one_case, then run_start...run_end is the run.
2124 If run_type is two_case, then the run is run_start...run_end,
2125 and the case-equivalents end at run_eqv_end. */
2127 enum case_type run_type
= strange
;
2128 int run_start
, run_end
, run_eqv_end
;
2130 Lisp_Object eqv_table
;
2132 if (!RE_TRANSLATE_P (translate
))
2134 EXTEND_RANGE_TABLE (work_area
, 2);
2135 work_area
->table
[work_area
->used
++] = (start
);
2136 work_area
->table
[work_area
->used
++] = (end
);
2140 eqv_table
= XCHAR_TABLE (translate
)->extras
[2];
2142 for (; start
<= end
; start
++)
2144 enum case_type this_type
;
2145 int eqv
= RE_TRANSLATE (eqv_table
, start
);
2146 int minchar
, maxchar
;
2148 /* Classify this character */
2150 this_type
= one_case
;
2151 else if (RE_TRANSLATE (eqv_table
, eqv
) == start
)
2152 this_type
= two_case
;
2154 this_type
= strange
;
2157 minchar
= start
, maxchar
= eqv
;
2159 minchar
= eqv
, maxchar
= start
;
2161 /* Can this character extend the run in progress? */
2162 if (this_type
== strange
|| this_type
!= run_type
2163 || !(minchar
== run_end
+ 1
2164 && (run_type
== two_case
2165 ? maxchar
== run_eqv_end
+ 1 : 1)))
2168 Record each of its equivalent ranges. */
2169 if (run_type
== one_case
)
2171 EXTEND_RANGE_TABLE (work_area
, 2);
2172 work_area
->table
[work_area
->used
++] = run_start
;
2173 work_area
->table
[work_area
->used
++] = run_end
;
2175 else if (run_type
== two_case
)
2177 EXTEND_RANGE_TABLE (work_area
, 4);
2178 work_area
->table
[work_area
->used
++] = run_start
;
2179 work_area
->table
[work_area
->used
++] = run_end
;
2180 work_area
->table
[work_area
->used
++]
2181 = RE_TRANSLATE (eqv_table
, run_start
);
2182 work_area
->table
[work_area
->used
++]
2183 = RE_TRANSLATE (eqv_table
, run_end
);
2188 if (this_type
== strange
)
2190 /* For a strange character, add each of its equivalents, one
2191 by one. Don't start a range. */
2194 EXTEND_RANGE_TABLE (work_area
, 2);
2195 work_area
->table
[work_area
->used
++] = eqv
;
2196 work_area
->table
[work_area
->used
++] = eqv
;
2197 eqv
= RE_TRANSLATE (eqv_table
, eqv
);
2199 while (eqv
!= start
);
2202 /* Add this char to the run, or start a new run. */
2203 else if (run_type
== strange
)
2205 /* Initialize a new range. */
2206 run_type
= this_type
;
2209 run_eqv_end
= RE_TRANSLATE (eqv_table
, run_end
);
2213 /* Extend a running range. */
2215 run_eqv_end
= RE_TRANSLATE (eqv_table
, run_end
);
2219 /* If a run is still in progress at the end, finish it now
2220 by recording its equivalent ranges. */
2221 if (run_type
== one_case
)
2223 EXTEND_RANGE_TABLE (work_area
, 2);
2224 work_area
->table
[work_area
->used
++] = run_start
;
2225 work_area
->table
[work_area
->used
++] = run_end
;
2227 else if (run_type
== two_case
)
2229 EXTEND_RANGE_TABLE (work_area
, 4);
2230 work_area
->table
[work_area
->used
++] = run_start
;
2231 work_area
->table
[work_area
->used
++] = run_end
;
2232 work_area
->table
[work_area
->used
++]
2233 = RE_TRANSLATE (eqv_table
, run_start
);
2234 work_area
->table
[work_area
->used
++]
2235 = RE_TRANSLATE (eqv_table
, run_end
);
2243 /* Record the image of the range start..end when passed through
2244 TRANSLATE. This is not necessarily TRANSLATE(start)..TRANSLATE(end)
2245 and is not even necessarily contiguous.
2246 Normally we approximate it with the smallest contiguous range that contains
2247 all the chars we need. However, for Latin-1 we go to extra effort
2250 This function is not called for ASCII ranges.
2252 Returns -1 if successful, REG_ESPACE if ran out of space. */
2255 set_image_of_range (struct range_table_work_area
*work_area
,
2256 re_wchar_t start
, re_wchar_t end
,
2257 RE_TRANSLATE_TYPE translate
)
2259 re_wchar_t cmin
, cmax
;
2262 /* For Latin-1 ranges, use set_image_of_range_1
2263 to get proper handling of ranges that include letters and nonletters.
2264 For a range that includes the whole of Latin-1, this is not necessary.
2265 For other character sets, we don't bother to get this right. */
2266 if (RE_TRANSLATE_P (translate
) && start
< 04400
2267 && !(start
< 04200 && end
>= 04377))
2274 tem
= set_image_of_range_1 (work_area
, start
, newend
, translate
);
2284 EXTEND_RANGE_TABLE (work_area
, 2);
2285 work_area
->table
[work_area
->used
++] = (start
);
2286 work_area
->table
[work_area
->used
++] = (end
);
2288 cmin
= -1, cmax
= -1;
2290 if (RE_TRANSLATE_P (translate
))
2294 for (ch
= start
; ch
<= end
; ch
++)
2296 re_wchar_t c
= TRANSLATE (ch
);
2297 if (! (start
<= c
&& c
<= end
))
2303 cmin
= min (cmin
, c
);
2304 cmax
= max (cmax
, c
);
2311 EXTEND_RANGE_TABLE (work_area
, 2);
2312 work_area
->table
[work_area
->used
++] = (cmin
);
2313 work_area
->table
[work_area
->used
++] = (cmax
);
2321 #ifndef MATCH_MAY_ALLOCATE
2323 /* If we cannot allocate large objects within re_match_2_internal,
2324 we make the fail stack and register vectors global.
2325 The fail stack, we grow to the maximum size when a regexp
2327 The register vectors, we adjust in size each time we
2328 compile a regexp, according to the number of registers it needs. */
2330 static fail_stack_type fail_stack
;
2332 /* Size with which the following vectors are currently allocated.
2333 That is so we can make them bigger as needed,
2334 but never make them smaller. */
2335 static int regs_allocated_size
;
2337 static re_char
** regstart
, ** regend
;
2338 static re_char
**best_regstart
, **best_regend
;
2340 /* Make the register vectors big enough for NUM_REGS registers,
2341 but don't make them smaller. */
2344 regex_grow_registers (int num_regs
)
2346 if (num_regs
> regs_allocated_size
)
2348 RETALLOC_IF (regstart
, num_regs
, re_char
*);
2349 RETALLOC_IF (regend
, num_regs
, re_char
*);
2350 RETALLOC_IF (best_regstart
, num_regs
, re_char
*);
2351 RETALLOC_IF (best_regend
, num_regs
, re_char
*);
2353 regs_allocated_size
= num_regs
;
2357 #endif /* not MATCH_MAY_ALLOCATE */
2359 static boolean
group_in_compile_stack (compile_stack_type compile_stack
,
2362 /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
2363 Returns one of error codes defined in `regex.h', or zero for success.
2365 If WHITESPACE_REGEXP is given (only #ifdef emacs), it is used instead of
2366 a space character in PATTERN.
2368 Assumes the `allocated' (and perhaps `buffer') and `translate'
2369 fields are set in BUFP on entry.
2371 If it succeeds, results are put in BUFP (if it returns an error, the
2372 contents of BUFP are undefined):
2373 `buffer' is the compiled pattern;
2374 `syntax' is set to SYNTAX;
2375 `used' is set to the length of the compiled pattern;
2376 `fastmap_accurate' is zero;
2377 `re_nsub' is the number of subexpressions in PATTERN;
2378 `not_bol' and `not_eol' are zero;
2380 The `fastmap' field is neither examined nor set. */
2382 /* Insert the `jump' from the end of last alternative to "here".
2383 The space for the jump has already been allocated. */
2384 #define FIXUP_ALT_JUMP() \
2386 if (fixup_alt_jump) \
2387 STORE_JUMP (jump, fixup_alt_jump, b); \
2391 /* Return, freeing storage we allocated. */
2392 #define FREE_STACK_RETURN(value) \
2394 FREE_RANGE_TABLE_WORK_AREA (range_table_work); \
2395 free (compile_stack.stack); \
2399 static reg_errcode_t
2400 regex_compile (re_char
*pattern
, size_t size
,
2402 # define syntax RE_SYNTAX_EMACS
2403 bool posix_backtracking
,
2404 const char *whitespace_regexp
,
2406 reg_syntax_t syntax
,
2407 # define posix_backtracking (!(syntax & RE_NO_POSIX_BACKTRACKING))
2409 struct re_pattern_buffer
*bufp
)
2411 /* We fetch characters from PATTERN here. */
2412 register re_wchar_t c
, c1
;
2414 /* Points to the end of the buffer, where we should append. */
2415 register unsigned char *b
;
2417 /* Keeps track of unclosed groups. */
2418 compile_stack_type compile_stack
;
2420 /* Points to the current (ending) position in the pattern. */
2422 /* `const' makes AIX compiler fail. */
2423 unsigned char *p
= pattern
;
2425 re_char
*p
= pattern
;
2427 re_char
*pend
= pattern
+ size
;
2429 /* How to translate the characters in the pattern. */
2430 RE_TRANSLATE_TYPE translate
= bufp
->translate
;
2432 /* Address of the count-byte of the most recently inserted `exactn'
2433 command. This makes it possible to tell if a new exact-match
2434 character can be added to that command or if the character requires
2435 a new `exactn' command. */
2436 unsigned char *pending_exact
= 0;
2438 /* Address of start of the most recently finished expression.
2439 This tells, e.g., postfix * where to find the start of its
2440 operand. Reset at the beginning of groups and alternatives. */
2441 unsigned char *laststart
= 0;
2443 /* Address of beginning of regexp, or inside of last group. */
2444 unsigned char *begalt
;
2446 /* Place in the uncompiled pattern (i.e., the {) to
2447 which to go back if the interval is invalid. */
2448 re_char
*beg_interval
;
2450 /* Address of the place where a forward jump should go to the end of
2451 the containing expression. Each alternative of an `or' -- except the
2452 last -- ends with a forward jump of this sort. */
2453 unsigned char *fixup_alt_jump
= 0;
2455 /* Work area for range table of charset. */
2456 struct range_table_work_area range_table_work
;
2458 /* If the object matched can contain multibyte characters. */
2459 const boolean multibyte
= RE_MULTIBYTE_P (bufp
);
2462 /* Nonzero if we have pushed down into a subpattern. */
2463 int in_subpattern
= 0;
2465 /* These hold the values of p, pattern, and pend from the main
2466 pattern when we have pushed into a subpattern. */
2468 re_char
*main_pattern
;
2474 DEBUG_PRINT ("\nCompiling pattern: ");
2477 unsigned debug_count
;
2479 for (debug_count
= 0; debug_count
< size
; debug_count
++)
2480 putchar (pattern
[debug_count
]);
2485 /* Initialize the compile stack. */
2486 compile_stack
.stack
= TALLOC (INIT_COMPILE_STACK_SIZE
, compile_stack_elt_t
);
2487 if (compile_stack
.stack
== NULL
)
2490 compile_stack
.size
= INIT_COMPILE_STACK_SIZE
;
2491 compile_stack
.avail
= 0;
2493 range_table_work
.table
= 0;
2494 range_table_work
.allocated
= 0;
2496 /* Initialize the pattern buffer. */
2498 bufp
->syntax
= syntax
;
2500 bufp
->fastmap_accurate
= 0;
2501 bufp
->not_bol
= bufp
->not_eol
= 0;
2502 bufp
->used_syntax
= 0;
2504 /* Set `used' to zero, so that if we return an error, the pattern
2505 printer (for debugging) will think there's no pattern. We reset it
2509 /* Always count groups, whether or not bufp->no_sub is set. */
2512 #if !defined emacs && !defined SYNTAX_TABLE
2513 /* Initialize the syntax table. */
2514 init_syntax_once ();
2517 if (bufp
->allocated
== 0)
2520 { /* If zero allocated, but buffer is non-null, try to realloc
2521 enough space. This loses if buffer's address is bogus, but
2522 that is the user's responsibility. */
2523 RETALLOC (bufp
->buffer
, INIT_BUF_SIZE
, unsigned char);
2526 { /* Caller did not allocate a buffer. Do it for them. */
2527 bufp
->buffer
= TALLOC (INIT_BUF_SIZE
, unsigned char);
2529 if (!bufp
->buffer
) FREE_STACK_RETURN (REG_ESPACE
);
2531 bufp
->allocated
= INIT_BUF_SIZE
;
2534 begalt
= b
= bufp
->buffer
;
2536 /* Loop through the uncompiled pattern until we're at the end. */
2542 /* If this is the end of an included regexp,
2543 pop back to the main regexp and try again. */
2547 pattern
= main_pattern
;
2553 /* If this is the end of the main regexp, we are done. */
2566 /* If there's no special whitespace regexp, treat
2567 spaces normally. And don't try to do this recursively. */
2568 if (!whitespace_regexp
|| in_subpattern
)
2571 /* Peek past following spaces. */
2578 /* If the spaces are followed by a repetition op,
2579 treat them normally. */
2581 && (*p1
== '*' || *p1
== '+' || *p1
== '?'
2582 || (*p1
== '\\' && p1
+ 1 != pend
&& p1
[1] == '{')))
2585 /* Replace the spaces with the whitespace regexp. */
2589 main_pattern
= pattern
;
2590 p
= pattern
= (re_char
*) whitespace_regexp
;
2591 pend
= p
+ strlen (whitespace_regexp
);
2598 if ( /* If at start of pattern, it's an operator. */
2600 /* If context independent, it's an operator. */
2601 || syntax
& RE_CONTEXT_INDEP_ANCHORS
2602 /* Otherwise, depends on what's come before. */
2603 || at_begline_loc_p (pattern
, p
, syntax
))
2604 BUF_PUSH ((syntax
& RE_NO_NEWLINE_ANCHOR
) ? begbuf
: begline
);
2613 if ( /* If at end of pattern, it's an operator. */
2615 /* If context independent, it's an operator. */
2616 || syntax
& RE_CONTEXT_INDEP_ANCHORS
2617 /* Otherwise, depends on what's next. */
2618 || at_endline_loc_p (p
, pend
, syntax
))
2619 BUF_PUSH ((syntax
& RE_NO_NEWLINE_ANCHOR
) ? endbuf
: endline
);
2628 if ((syntax
& RE_BK_PLUS_QM
)
2629 || (syntax
& RE_LIMITED_OPS
))
2634 /* If there is no previous pattern... */
2637 if (syntax
& RE_CONTEXT_INVALID_OPS
)
2638 FREE_STACK_RETURN (REG_BADRPT
);
2639 else if (!(syntax
& RE_CONTEXT_INDEP_OPS
))
2644 /* 1 means zero (many) matches is allowed. */
2645 boolean zero_times_ok
= 0, many_times_ok
= 0;
2648 /* If there is a sequence of repetition chars, collapse it
2649 down to just one (the right one). We can't combine
2650 interval operators with these because of, e.g., `a{2}*',
2651 which should only match an even number of `a's. */
2655 if ((syntax
& RE_FRUGAL
)
2656 && c
== '?' && (zero_times_ok
|| many_times_ok
))
2660 zero_times_ok
|= c
!= '+';
2661 many_times_ok
|= c
!= '?';
2667 || (!(syntax
& RE_BK_PLUS_QM
)
2668 && (*p
== '+' || *p
== '?')))
2670 else if (syntax
& RE_BK_PLUS_QM
&& *p
== '\\')
2673 FREE_STACK_RETURN (REG_EESCAPE
);
2674 if (p
[1] == '+' || p
[1] == '?')
2675 PATFETCH (c
); /* Gobble up the backslash. */
2681 /* If we get here, we found another repeat character. */
2685 /* Star, etc. applied to an empty pattern is equivalent
2686 to an empty pattern. */
2687 if (!laststart
|| laststart
== b
)
2690 /* Now we know whether or not zero matches is allowed
2691 and also whether or not two or more matches is allowed. */
2696 boolean simple
= skip_one_char (laststart
) == b
;
2697 size_t startoffset
= 0;
2699 /* Check if the loop can match the empty string. */
2700 (simple
|| !analyze_first (laststart
, b
, NULL
, 0))
2701 ? on_failure_jump
: on_failure_jump_loop
;
2702 assert (skip_one_char (laststart
) <= b
);
2704 if (!zero_times_ok
&& simple
)
2705 { /* Since simple * loops can be made faster by using
2706 on_failure_keep_string_jump, we turn simple P+
2707 into PP* if P is simple. */
2708 unsigned char *p1
, *p2
;
2709 startoffset
= b
- laststart
;
2710 GET_BUFFER_SPACE (startoffset
);
2711 p1
= b
; p2
= laststart
;
2717 GET_BUFFER_SPACE (6);
2720 STORE_JUMP (ofj
, b
, b
+ 6);
2722 /* Simple * loops can use on_failure_keep_string_jump
2723 depending on what follows. But since we don't know
2724 that yet, we leave the decision up to
2725 on_failure_jump_smart. */
2726 INSERT_JUMP (simple
? on_failure_jump_smart
: ofj
,
2727 laststart
+ startoffset
, b
+ 6);
2729 STORE_JUMP (jump
, b
, laststart
+ startoffset
);
2734 /* A simple ? pattern. */
2735 assert (zero_times_ok
);
2736 GET_BUFFER_SPACE (3);
2737 INSERT_JUMP (on_failure_jump
, laststart
, b
+ 3);
2741 else /* not greedy */
2742 { /* I wish the greedy and non-greedy cases could be merged. */
2744 GET_BUFFER_SPACE (7); /* We might use less. */
2747 boolean emptyp
= analyze_first (laststart
, b
, NULL
, 0);
2749 /* The non-greedy multiple match looks like
2750 a repeat..until: we only need a conditional jump
2751 at the end of the loop. */
2752 if (emptyp
) BUF_PUSH (no_op
);
2753 STORE_JUMP (emptyp
? on_failure_jump_nastyloop
2754 : on_failure_jump
, b
, laststart
);
2758 /* The repeat...until naturally matches one or more.
2759 To also match zero times, we need to first jump to
2760 the end of the loop (its conditional jump). */
2761 INSERT_JUMP (jump
, laststart
, b
);
2767 /* non-greedy a?? */
2768 INSERT_JUMP (jump
, laststart
, b
+ 3);
2770 INSERT_JUMP (on_failure_jump
, laststart
, laststart
+ 6);
2789 CLEAR_RANGE_TABLE_WORK_USED (range_table_work
);
2791 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2793 /* Ensure that we have enough space to push a charset: the
2794 opcode, the length count, and the bitset; 34 bytes in all. */
2795 GET_BUFFER_SPACE (34);
2799 /* We test `*p == '^' twice, instead of using an if
2800 statement, so we only need one BUF_PUSH. */
2801 BUF_PUSH (*p
== '^' ? charset_not
: charset
);
2805 /* Remember the first position in the bracket expression. */
2808 /* Push the number of bytes in the bitmap. */
2809 BUF_PUSH ((1 << BYTEWIDTH
) / BYTEWIDTH
);
2811 /* Clear the whole map. */
2812 memset (b
, 0, (1 << BYTEWIDTH
) / BYTEWIDTH
);
2814 /* charset_not matches newline according to a syntax bit. */
2815 if ((re_opcode_t
) b
[-2] == charset_not
2816 && (syntax
& RE_HAT_LISTS_NOT_NEWLINE
))
2817 SET_LIST_BIT ('\n');
2819 /* Read in characters and ranges, setting map bits. */
2822 boolean escaped_char
= false;
2823 const unsigned char *p2
= p
;
2827 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2829 /* See if we're at the beginning of a possible character
2831 if (syntax
& RE_CHAR_CLASSES
&&
2832 (cc
= re_wctype_parse(&p
, pend
- p
)) != -1)
2835 FREE_STACK_RETURN (REG_ECTYPE
);
2838 FREE_STACK_RETURN (REG_EBRACK
);
2841 for (ch
= 0; ch
< (1 << BYTEWIDTH
); ++ch
)
2842 if (re_iswctype (btowc (ch
), cc
))
2845 if (c
< (1 << BYTEWIDTH
))
2849 /* Most character classes in a multibyte match just set
2850 a flag. Exceptions are is_blank, is_digit, is_cntrl, and
2851 is_xdigit, since they can only match ASCII characters.
2852 We don't need to handle them for multibyte. */
2854 /* Setup the gl_state object to its buffer-defined value.
2855 This hardcodes the buffer-global syntax-table for ASCII
2856 chars, while the other chars will obey syntax-table
2857 properties. It's not ideal, but it's the way it's been
2859 SETUP_BUFFER_SYNTAX_TABLE ();
2861 for (c
= 0; c
< 0x80; ++c
)
2862 if (re_iswctype (c
, cc
))
2868 if (ASCII_CHAR_P (c1
))
2870 else if ((c1
= RE_CHAR_TO_UNIBYTE (c1
)) >= 0)
2873 SET_RANGE_TABLE_WORK_AREA_BIT
2874 (range_table_work
, re_wctype_to_bit (cc
));
2876 /* In most cases the matching rule for char classes only
2877 uses the syntax table for multibyte chars, so that the
2878 content of the syntax-table is not hardcoded in the
2879 range_table. SPACE and WORD are the two exceptions. */
2880 if ((1 << cc
) & ((1 << RECC_SPACE
) | (1 << RECC_WORD
)))
2881 bufp
->used_syntax
= 1;
2883 /* Repeat the loop. */
2887 /* Don't translate yet. The range TRANSLATE(X..Y) cannot
2888 always be determined from TRANSLATE(X) and TRANSLATE(Y)
2889 So the translation is done later in a loop. Example:
2890 (let ((case-fold-search t)) (string-match "[A-_]" "A")) */
2893 /* \ might escape characters inside [...] and [^...]. */
2894 if ((syntax
& RE_BACKSLASH_ESCAPE_IN_LISTS
) && c
== '\\')
2896 if (p
== pend
) FREE_STACK_RETURN (REG_EESCAPE
);
2899 escaped_char
= true;
2903 /* Could be the end of the bracket expression. If it's
2904 not (i.e., when the bracket expression is `[]' so
2905 far), the ']' character bit gets set way below. */
2906 if (c
== ']' && p2
!= p1
)
2910 if (p
< pend
&& p
[0] == '-' && p
[1] != ']')
2913 /* Discard the `-'. */
2916 /* Fetch the character which ends the range. */
2919 if (CHAR_BYTE8_P (c1
)
2920 && ! ASCII_CHAR_P (c
) && ! CHAR_BYTE8_P (c
))
2921 /* Treat the range from a multibyte character to
2922 raw-byte character as empty. */
2927 /* Range from C to C. */
2932 if (syntax
& RE_NO_EMPTY_RANGES
)
2933 FREE_STACK_RETURN (REG_ERANGEX
);
2934 /* Else, repeat the loop. */
2939 /* Set the range into bitmap */
2940 for (; c
<= c1
; c
++)
2943 if (ch
< (1 << BYTEWIDTH
))
2950 SETUP_ASCII_RANGE (range_table_work
, c
, ch
);
2952 if (CHAR_BYTE8_P (c1
))
2953 c
= BYTE8_TO_CHAR (128);
2957 if (CHAR_BYTE8_P (c
))
2959 c
= CHAR_TO_BYTE8 (c
);
2960 c1
= CHAR_TO_BYTE8 (c1
);
2961 for (; c
<= c1
; c
++)
2966 SETUP_MULTIBYTE_RANGE (range_table_work
, c
, c1
);
2970 SETUP_UNIBYTE_RANGE (range_table_work
, c
, c1
);
2977 /* Discard any (non)matching list bytes that are all 0 at the
2978 end of the map. Decrease the map-length byte too. */
2979 while ((int) b
[-1] > 0 && b
[b
[-1] - 1] == 0)
2983 /* Build real range table from work area. */
2984 if (RANGE_TABLE_WORK_USED (range_table_work
)
2985 || RANGE_TABLE_WORK_BITS (range_table_work
))
2988 int used
= RANGE_TABLE_WORK_USED (range_table_work
);
2990 /* Allocate space for COUNT + RANGE_TABLE. Needs two
2991 bytes for flags, two for COUNT, and three bytes for
2993 GET_BUFFER_SPACE (4 + used
* 3);
2995 /* Indicate the existence of range table. */
2996 laststart
[1] |= 0x80;
2998 /* Store the character class flag bits into the range table.
2999 If not in emacs, these flag bits are always 0. */
3000 *b
++ = RANGE_TABLE_WORK_BITS (range_table_work
) & 0xff;
3001 *b
++ = RANGE_TABLE_WORK_BITS (range_table_work
) >> 8;
3003 STORE_NUMBER_AND_INCR (b
, used
/ 2);
3004 for (i
= 0; i
< used
; i
++)
3005 STORE_CHARACTER_AND_INCR
3006 (b
, RANGE_TABLE_WORK_ELT (range_table_work
, i
));
3013 if (syntax
& RE_NO_BK_PARENS
)
3020 if (syntax
& RE_NO_BK_PARENS
)
3027 if (syntax
& RE_NEWLINE_ALT
)
3034 if (syntax
& RE_NO_BK_VBAR
)
3041 if (syntax
& RE_INTERVALS
&& syntax
& RE_NO_BK_BRACES
)
3042 goto handle_interval
;
3048 if (p
== pend
) FREE_STACK_RETURN (REG_EESCAPE
);
3050 /* Do not translate the character after the \, so that we can
3051 distinguish, e.g., \B from \b, even if we normally would
3052 translate, e.g., B to b. */
3058 if (syntax
& RE_NO_BK_PARENS
)
3059 goto normal_backslash
;
3064 regnum_t regnum
= 0;
3067 /* Look for a special (?...) construct */
3068 if ((syntax
& RE_SHY_GROUPS
) && *p
== '?')
3070 PATFETCH (c
); /* Gobble up the '?'. */
3076 case ':': shy
= 1; break;
3078 /* An explicitly specified regnum must start
3081 FREE_STACK_RETURN (REG_BADPAT
);
3083 case '1': case '2': case '3': case '4':
3084 case '5': case '6': case '7': case '8': case '9':
3085 regnum
= 10*regnum
+ (c
- '0'); break;
3087 /* Only (?:...) is supported right now. */
3088 FREE_STACK_RETURN (REG_BADPAT
);
3095 regnum
= ++bufp
->re_nsub
;
3097 { /* It's actually not shy, but explicitly numbered. */
3099 if (regnum
> bufp
->re_nsub
)
3100 bufp
->re_nsub
= regnum
;
3101 else if (regnum
> bufp
->re_nsub
3102 /* Ideally, we'd want to check that the specified
3103 group can't have matched (i.e. all subgroups
3104 using the same regnum are in other branches of
3105 OR patterns), but we don't currently keep track
3106 of enough info to do that easily. */
3107 || group_in_compile_stack (compile_stack
, regnum
))
3108 FREE_STACK_RETURN (REG_BADPAT
);
3111 /* It's really shy. */
3112 regnum
= - bufp
->re_nsub
;
3114 if (COMPILE_STACK_FULL
)
3116 RETALLOC (compile_stack
.stack
, compile_stack
.size
<< 1,
3117 compile_stack_elt_t
);
3118 if (compile_stack
.stack
== NULL
) return REG_ESPACE
;
3120 compile_stack
.size
<<= 1;
3123 /* These are the values to restore when we hit end of this
3124 group. They are all relative offsets, so that if the
3125 whole pattern moves because of realloc, they will still
3127 COMPILE_STACK_TOP
.begalt_offset
= begalt
- bufp
->buffer
;
3128 COMPILE_STACK_TOP
.fixup_alt_jump
3129 = fixup_alt_jump
? fixup_alt_jump
- bufp
->buffer
+ 1 : 0;
3130 COMPILE_STACK_TOP
.laststart_offset
= b
- bufp
->buffer
;
3131 COMPILE_STACK_TOP
.regnum
= regnum
;
3133 /* Do not push a start_memory for groups beyond the last one
3134 we can represent in the compiled pattern. */
3135 if (regnum
<= MAX_REGNUM
&& regnum
> 0)
3136 BUF_PUSH_2 (start_memory
, regnum
);
3138 compile_stack
.avail
++;
3143 /* If we've reached MAX_REGNUM groups, then this open
3144 won't actually generate any code, so we'll have to
3145 clear pending_exact explicitly. */
3151 if (syntax
& RE_NO_BK_PARENS
) goto normal_backslash
;
3153 if (COMPILE_STACK_EMPTY
)
3155 if (syntax
& RE_UNMATCHED_RIGHT_PAREN_ORD
)
3156 goto normal_backslash
;
3158 FREE_STACK_RETURN (REG_ERPAREN
);
3164 /* See similar code for backslashed left paren above. */
3165 if (COMPILE_STACK_EMPTY
)
3167 if (syntax
& RE_UNMATCHED_RIGHT_PAREN_ORD
)
3170 FREE_STACK_RETURN (REG_ERPAREN
);
3173 /* Since we just checked for an empty stack above, this
3174 ``can't happen''. */
3175 assert (compile_stack
.avail
!= 0);
3177 /* We don't just want to restore into `regnum', because
3178 later groups should continue to be numbered higher,
3179 as in `(ab)c(de)' -- the second group is #2. */
3182 compile_stack
.avail
--;
3183 begalt
= bufp
->buffer
+ COMPILE_STACK_TOP
.begalt_offset
;
3185 = COMPILE_STACK_TOP
.fixup_alt_jump
3186 ? bufp
->buffer
+ COMPILE_STACK_TOP
.fixup_alt_jump
- 1
3188 laststart
= bufp
->buffer
+ COMPILE_STACK_TOP
.laststart_offset
;
3189 regnum
= COMPILE_STACK_TOP
.regnum
;
3190 /* If we've reached MAX_REGNUM groups, then this open
3191 won't actually generate any code, so we'll have to
3192 clear pending_exact explicitly. */
3195 /* We're at the end of the group, so now we know how many
3196 groups were inside this one. */
3197 if (regnum
<= MAX_REGNUM
&& regnum
> 0)
3198 BUF_PUSH_2 (stop_memory
, regnum
);
3203 case '|': /* `\|'. */
3204 if (syntax
& RE_LIMITED_OPS
|| syntax
& RE_NO_BK_VBAR
)
3205 goto normal_backslash
;
3207 if (syntax
& RE_LIMITED_OPS
)
3210 /* Insert before the previous alternative a jump which
3211 jumps to this alternative if the former fails. */
3212 GET_BUFFER_SPACE (3);
3213 INSERT_JUMP (on_failure_jump
, begalt
, b
+ 6);
3217 /* The alternative before this one has a jump after it
3218 which gets executed if it gets matched. Adjust that
3219 jump so it will jump to this alternative's analogous
3220 jump (put in below, which in turn will jump to the next
3221 (if any) alternative's such jump, etc.). The last such
3222 jump jumps to the correct final destination. A picture:
3228 If we are at `b', then fixup_alt_jump right now points to a
3229 three-byte space after `a'. We'll put in the jump, set
3230 fixup_alt_jump to right after `b', and leave behind three
3231 bytes which we'll fill in when we get to after `c'. */
3235 /* Mark and leave space for a jump after this alternative,
3236 to be filled in later either by next alternative or
3237 when know we're at the end of a series of alternatives. */
3239 GET_BUFFER_SPACE (3);
3248 /* If \{ is a literal. */
3249 if (!(syntax
& RE_INTERVALS
)
3250 /* If we're at `\{' and it's not the open-interval
3252 || (syntax
& RE_NO_BK_BRACES
))
3253 goto normal_backslash
;
3257 /* If got here, then the syntax allows intervals. */
3259 /* At least (most) this many matches must be made. */
3260 int lower_bound
= 0, upper_bound
= -1;
3264 GET_INTERVAL_COUNT (lower_bound
);
3267 GET_INTERVAL_COUNT (upper_bound
);
3269 /* Interval such as `{1}' => match exactly once. */
3270 upper_bound
= lower_bound
;
3273 || (0 <= upper_bound
&& upper_bound
< lower_bound
))
3274 FREE_STACK_RETURN (REG_BADBR
);
3276 if (!(syntax
& RE_NO_BK_BRACES
))
3279 FREE_STACK_RETURN (REG_BADBR
);
3281 FREE_STACK_RETURN (REG_EESCAPE
);
3286 FREE_STACK_RETURN (REG_BADBR
);
3288 /* We just parsed a valid interval. */
3290 /* If it's invalid to have no preceding re. */
3293 if (syntax
& RE_CONTEXT_INVALID_OPS
)
3294 FREE_STACK_RETURN (REG_BADRPT
);
3295 else if (syntax
& RE_CONTEXT_INDEP_OPS
)
3298 goto unfetch_interval
;
3301 if (upper_bound
== 0)
3302 /* If the upper bound is zero, just drop the sub pattern
3305 else if (lower_bound
== 1 && upper_bound
== 1)
3306 /* Just match it once: nothing to do here. */
3309 /* Otherwise, we have a nontrivial interval. When
3310 we're all done, the pattern will look like:
3311 set_number_at <jump count> <upper bound>
3312 set_number_at <succeed_n count> <lower bound>
3313 succeed_n <after jump addr> <succeed_n count>
3315 jump_n <succeed_n addr> <jump count>
3316 (The upper bound and `jump_n' are omitted if
3317 `upper_bound' is 1, though.) */
3319 { /* If the upper bound is > 1, we need to insert
3320 more at the end of the loop. */
3321 unsigned int nbytes
= (upper_bound
< 0 ? 3
3322 : upper_bound
> 1 ? 5 : 0);
3323 unsigned int startoffset
= 0;
3325 GET_BUFFER_SPACE (20); /* We might use less. */
3327 if (lower_bound
== 0)
3329 /* A succeed_n that starts with 0 is really a
3330 a simple on_failure_jump_loop. */
3331 INSERT_JUMP (on_failure_jump_loop
, laststart
,
3337 /* Initialize lower bound of the `succeed_n', even
3338 though it will be set during matching by its
3339 attendant `set_number_at' (inserted next),
3340 because `re_compile_fastmap' needs to know.
3341 Jump to the `jump_n' we might insert below. */
3342 INSERT_JUMP2 (succeed_n
, laststart
,
3347 /* Code to initialize the lower bound. Insert
3348 before the `succeed_n'. The `5' is the last two
3349 bytes of this `set_number_at', plus 3 bytes of
3350 the following `succeed_n'. */
3351 insert_op2 (set_number_at
, laststart
, 5, lower_bound
, b
);
3356 if (upper_bound
< 0)
3358 /* A negative upper bound stands for infinity,
3359 in which case it degenerates to a plain jump. */
3360 STORE_JUMP (jump
, b
, laststart
+ startoffset
);
3363 else if (upper_bound
> 1)
3364 { /* More than one repetition is allowed, so
3365 append a backward jump to the `succeed_n'
3366 that starts this interval.
3368 When we've reached this during matching,
3369 we'll have matched the interval once, so
3370 jump back only `upper_bound - 1' times. */
3371 STORE_JUMP2 (jump_n
, b
, laststart
+ startoffset
,
3375 /* The location we want to set is the second
3376 parameter of the `jump_n'; that is `b-2' as
3377 an absolute address. `laststart' will be
3378 the `set_number_at' we're about to insert;
3379 `laststart+3' the number to set, the source
3380 for the relative address. But we are
3381 inserting into the middle of the pattern --
3382 so everything is getting moved up by 5.
3383 Conclusion: (b - 2) - (laststart + 3) + 5,
3384 i.e., b - laststart.
3386 We insert this at the beginning of the loop
3387 so that if we fail during matching, we'll
3388 reinitialize the bounds. */
3389 insert_op2 (set_number_at
, laststart
, b
- laststart
,
3390 upper_bound
- 1, b
);
3395 beg_interval
= NULL
;
3400 /* If an invalid interval, match the characters as literals. */
3401 assert (beg_interval
);
3403 beg_interval
= NULL
;
3405 /* normal_char and normal_backslash need `c'. */
3408 if (!(syntax
& RE_NO_BK_BRACES
))
3410 assert (p
> pattern
&& p
[-1] == '\\');
3411 goto normal_backslash
;
3425 BUF_PUSH_2 (syntaxspec
, syntax_spec_code
[c
]);
3431 BUF_PUSH_2 (notsyntaxspec
, syntax_spec_code
[c
]);
3437 BUF_PUSH_2 (categoryspec
, c
);
3443 BUF_PUSH_2 (notcategoryspec
, c
);
3449 if (syntax
& RE_NO_GNU_OPS
)
3452 BUF_PUSH_2 (syntaxspec
, Sword
);
3457 if (syntax
& RE_NO_GNU_OPS
)
3460 BUF_PUSH_2 (notsyntaxspec
, Sword
);
3465 if (syntax
& RE_NO_GNU_OPS
)
3472 if (syntax
& RE_NO_GNU_OPS
)
3479 if (syntax
& RE_NO_GNU_OPS
)
3488 FREE_STACK_RETURN (REG_BADPAT
);
3492 if (syntax
& RE_NO_GNU_OPS
)
3494 BUF_PUSH (wordbound
);
3498 if (syntax
& RE_NO_GNU_OPS
)
3500 BUF_PUSH (notwordbound
);
3504 if (syntax
& RE_NO_GNU_OPS
)
3510 if (syntax
& RE_NO_GNU_OPS
)
3515 case '1': case '2': case '3': case '4': case '5':
3516 case '6': case '7': case '8': case '9':
3520 if (syntax
& RE_NO_BK_REFS
)
3521 goto normal_backslash
;
3525 if (reg
> bufp
->re_nsub
|| reg
< 1
3526 /* Can't back reference to a subexp before its end. */
3527 || group_in_compile_stack (compile_stack
, reg
))
3528 FREE_STACK_RETURN (REG_ESUBREG
);
3531 BUF_PUSH_2 (duplicate
, reg
);
3538 if (syntax
& RE_BK_PLUS_QM
)
3541 goto normal_backslash
;
3545 /* You might think it would be useful for \ to mean
3546 not to translate; but if we don't translate it
3547 it will never match anything. */
3554 /* Expects the character in `c'. */
3556 /* If no exactn currently being built. */
3559 /* If last exactn not at current position. */
3560 || pending_exact
+ *pending_exact
+ 1 != b
3562 /* We have only one byte following the exactn for the count. */
3563 || *pending_exact
>= (1 << BYTEWIDTH
) - MAX_MULTIBYTE_LENGTH
3565 /* If followed by a repetition operator. */
3566 || (p
!= pend
&& (*p
== '*' || *p
== '^'))
3567 || ((syntax
& RE_BK_PLUS_QM
)
3568 ? p
+ 1 < pend
&& *p
== '\\' && (p
[1] == '+' || p
[1] == '?')
3569 : p
!= pend
&& (*p
== '+' || *p
== '?'))
3570 || ((syntax
& RE_INTERVALS
)
3571 && ((syntax
& RE_NO_BK_BRACES
)
3572 ? p
!= pend
&& *p
== '{'
3573 : p
+ 1 < pend
&& p
[0] == '\\' && p
[1] == '{')))
3575 /* Start building a new exactn. */
3579 BUF_PUSH_2 (exactn
, 0);
3580 pending_exact
= b
- 1;
3583 GET_BUFFER_SPACE (MAX_MULTIBYTE_LENGTH
);
3590 len
= CHAR_STRING (c
, b
);
3595 c1
= RE_CHAR_TO_MULTIBYTE (c
);
3596 if (! CHAR_BYTE8_P (c1
))
3598 re_wchar_t c2
= TRANSLATE (c1
);
3600 if (c1
!= c2
&& (c1
= RE_CHAR_TO_UNIBYTE (c2
)) >= 0)
3606 (*pending_exact
) += len
;
3611 } /* while p != pend */
3614 /* Through the pattern now. */
3618 if (!COMPILE_STACK_EMPTY
)
3619 FREE_STACK_RETURN (REG_EPAREN
);
3621 /* If we don't want backtracking, force success
3622 the first time we reach the end of the compiled pattern. */
3623 if (!posix_backtracking
)
3626 /* We have succeeded; set the length of the buffer. */
3627 bufp
->used
= b
- bufp
->buffer
;
3632 re_compile_fastmap (bufp
);
3633 DEBUG_PRINT ("\nCompiled pattern: \n");
3634 print_compiled_pattern (bufp
);
3639 #ifndef MATCH_MAY_ALLOCATE
3640 /* Initialize the failure stack to the largest possible stack. This
3641 isn't necessary unless we're trying to avoid calling alloca in
3642 the search and match routines. */
3644 int num_regs
= bufp
->re_nsub
+ 1;
3646 if (fail_stack
.size
< emacs_re_max_failures
* TYPICAL_FAILURE_SIZE
)
3648 fail_stack
.size
= emacs_re_max_failures
* TYPICAL_FAILURE_SIZE
;
3649 falk_stack
.stack
= realloc (fail_stack
.stack
,
3650 fail_stack
.size
* sizeof *falk_stack
.stack
);
3653 regex_grow_registers (num_regs
);
3655 #endif /* not MATCH_MAY_ALLOCATE */
3657 FREE_STACK_RETURN (REG_NOERROR
);
3662 # undef posix_backtracking
3664 } /* regex_compile */
3666 /* Subroutines for `regex_compile'. */
3668 /* Store OP at LOC followed by two-byte integer parameter ARG. */
3671 store_op1 (re_opcode_t op
, unsigned char *loc
, int arg
)
3673 *loc
= (unsigned char) op
;
3674 STORE_NUMBER (loc
+ 1, arg
);
3678 /* Like `store_op1', but for two two-byte parameters ARG1 and ARG2. */
3681 store_op2 (re_opcode_t op
, unsigned char *loc
, int arg1
, int arg2
)
3683 *loc
= (unsigned char) op
;
3684 STORE_NUMBER (loc
+ 1, arg1
);
3685 STORE_NUMBER (loc
+ 3, arg2
);
3689 /* Copy the bytes from LOC to END to open up three bytes of space at LOC
3690 for OP followed by two-byte integer parameter ARG. */
3693 insert_op1 (re_opcode_t op
, unsigned char *loc
, int arg
, unsigned char *end
)
3695 register unsigned char *pfrom
= end
;
3696 register unsigned char *pto
= end
+ 3;
3698 while (pfrom
!= loc
)
3701 store_op1 (op
, loc
, arg
);
3705 /* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2. */
3708 insert_op2 (re_opcode_t op
, unsigned char *loc
, int arg1
, int arg2
, unsigned char *end
)
3710 register unsigned char *pfrom
= end
;
3711 register unsigned char *pto
= end
+ 5;
3713 while (pfrom
!= loc
)
3716 store_op2 (op
, loc
, arg1
, arg2
);
3720 /* P points to just after a ^ in PATTERN. Return true if that ^ comes
3721 after an alternative or a begin-subexpression. We assume there is at
3722 least one character before the ^. */
3725 at_begline_loc_p (re_char
*pattern
, re_char
*p
, reg_syntax_t syntax
)
3727 re_char
*prev
= p
- 2;
3728 boolean odd_backslashes
;
3730 /* After a subexpression? */
3732 odd_backslashes
= (syntax
& RE_NO_BK_PARENS
) == 0;
3734 /* After an alternative? */
3735 else if (*prev
== '|')
3736 odd_backslashes
= (syntax
& RE_NO_BK_VBAR
) == 0;
3738 /* After a shy subexpression? */
3739 else if (*prev
== ':' && (syntax
& RE_SHY_GROUPS
))
3741 /* Skip over optional regnum. */
3742 while (prev
- 1 >= pattern
&& prev
[-1] >= '0' && prev
[-1] <= '9')
3745 if (!(prev
- 2 >= pattern
3746 && prev
[-1] == '?' && prev
[-2] == '('))
3749 odd_backslashes
= (syntax
& RE_NO_BK_PARENS
) == 0;
3754 /* Count the number of preceding backslashes. */
3756 while (prev
- 1 >= pattern
&& prev
[-1] == '\\')
3758 return (p
- prev
) & odd_backslashes
;
3762 /* The dual of at_begline_loc_p. This one is for $. We assume there is
3763 at least one character after the $, i.e., `P < PEND'. */
3766 at_endline_loc_p (re_char
*p
, re_char
*pend
, reg_syntax_t syntax
)
3769 boolean next_backslash
= *next
== '\\';
3770 re_char
*next_next
= p
+ 1 < pend
? p
+ 1 : 0;
3773 /* Before a subexpression? */
3774 (syntax
& RE_NO_BK_PARENS
? *next
== ')'
3775 : next_backslash
&& next_next
&& *next_next
== ')')
3776 /* Before an alternative? */
3777 || (syntax
& RE_NO_BK_VBAR
? *next
== '|'
3778 : next_backslash
&& next_next
&& *next_next
== '|');
3782 /* Returns true if REGNUM is in one of COMPILE_STACK's elements and
3783 false if it's not. */
3786 group_in_compile_stack (compile_stack_type compile_stack
, regnum_t regnum
)
3788 ssize_t this_element
;
3790 for (this_element
= compile_stack
.avail
- 1;
3793 if (compile_stack
.stack
[this_element
].regnum
== regnum
)
3800 If fastmap is non-NULL, go through the pattern and fill fastmap
3801 with all the possible leading chars. If fastmap is NULL, don't
3802 bother filling it up (obviously) and only return whether the
3803 pattern could potentially match the empty string.
3805 Return 1 if p..pend might match the empty string.
3806 Return 0 if p..pend matches at least one char.
3807 Return -1 if fastmap was not updated accurately. */
3810 analyze_first (re_char
*p
, re_char
*pend
, char *fastmap
,
3811 const int multibyte
)
3816 /* If all elements for base leading-codes in fastmap is set, this
3817 flag is set true. */
3818 boolean match_any_multibyte_characters
= false;
3822 /* The loop below works as follows:
3823 - It has a working-list kept in the PATTERN_STACK and which basically
3824 starts by only containing a pointer to the first operation.
3825 - If the opcode we're looking at is a match against some set of
3826 chars, then we add those chars to the fastmap and go on to the
3827 next work element from the worklist (done via `break').
3828 - If the opcode is a control operator on the other hand, we either
3829 ignore it (if it's meaningless at this point, such as `start_memory')
3830 or execute it (if it's a jump). If the jump has several destinations
3831 (i.e. `on_failure_jump'), then we push the other destination onto the
3833 We guarantee termination by ignoring backward jumps (more or less),
3834 so that `p' is monotonically increasing. More to the point, we
3835 never set `p' (or push) anything `<= p1'. */
3839 /* `p1' is used as a marker of how far back a `on_failure_jump'
3840 can go without being ignored. It is normally equal to `p'
3841 (which prevents any backward `on_failure_jump') except right
3842 after a plain `jump', to allow patterns such as:
3845 10: on_failure_jump 3
3846 as used for the *? operator. */
3855 /* If the first character has to match a backreference, that means
3856 that the group was empty (since it already matched). Since this
3857 is the only case that interests us here, we can assume that the
3858 backreference must match the empty string. */
3863 /* Following are the cases which match a character. These end
3869 /* If multibyte is nonzero, the first byte of each
3870 character is an ASCII or a leading code. Otherwise,
3871 each byte is a character. Thus, this works in both
3876 /* For the case of matching this unibyte regex
3877 against multibyte, we must set a leading code of
3878 the corresponding multibyte character. */
3879 int c
= RE_CHAR_TO_MULTIBYTE (p
[1]);
3881 fastmap
[CHAR_LEADING_CODE (c
)] = 1;
3888 /* We could put all the chars except for \n (and maybe \0)
3889 but we don't bother since it is generally not worth it. */
3890 if (!fastmap
) break;
3895 if (!fastmap
) break;
3897 /* Chars beyond end of bitmap are possible matches. */
3898 for (j
= CHARSET_BITMAP_SIZE (&p
[-1]) * BYTEWIDTH
;
3899 j
< (1 << BYTEWIDTH
); j
++)
3904 if (!fastmap
) break;
3905 not = (re_opcode_t
) *(p
- 1) == charset_not
;
3906 for (j
= CHARSET_BITMAP_SIZE (&p
[-1]) * BYTEWIDTH
- 1, p
++;
3908 if (!!(p
[j
/ BYTEWIDTH
] & (1 << (j
% BYTEWIDTH
))) ^ not)
3912 if (/* Any leading code can possibly start a character
3913 which doesn't match the specified set of characters. */
3916 /* If we can match a character class, we can match any
3917 multibyte characters. */
3918 (CHARSET_RANGE_TABLE_EXISTS_P (&p
[-2])
3919 && CHARSET_RANGE_TABLE_BITS (&p
[-2]) != 0))
3922 if (match_any_multibyte_characters
== false)
3924 for (j
= MIN_MULTIBYTE_LEADING_CODE
;
3925 j
<= MAX_MULTIBYTE_LEADING_CODE
; j
++)
3927 match_any_multibyte_characters
= true;
3931 else if (!not && CHARSET_RANGE_TABLE_EXISTS_P (&p
[-2])
3932 && match_any_multibyte_characters
== false)
3934 /* Set fastmap[I] to 1 where I is a leading code of each
3935 multibyte character in the range table. */
3937 unsigned char lc1
, lc2
;
3939 /* Make P points the range table. `+ 2' is to skip flag
3940 bits for a character class. */
3941 p
+= CHARSET_BITMAP_SIZE (&p
[-2]) + 2;
3943 /* Extract the number of ranges in range table into COUNT. */
3944 EXTRACT_NUMBER_AND_INCR (count
, p
);
3945 for (; count
> 0; count
--, p
+= 3)
3947 /* Extract the start and end of each range. */
3948 EXTRACT_CHARACTER (c
, p
);
3949 lc1
= CHAR_LEADING_CODE (c
);
3951 EXTRACT_CHARACTER (c
, p
);
3952 lc2
= CHAR_LEADING_CODE (c
);
3953 for (j
= lc1
; j
<= lc2
; j
++)
3962 if (!fastmap
) break;
3964 not = (re_opcode_t
)p
[-1] == notsyntaxspec
;
3966 for (j
= 0; j
< (1 << BYTEWIDTH
); j
++)
3967 if ((SYNTAX (j
) == (enum syntaxcode
) k
) ^ not)
3971 /* This match depends on text properties. These end with
3972 aborting optimizations. */
3976 case notcategoryspec
:
3977 if (!fastmap
) break;
3978 not = (re_opcode_t
)p
[-1] == notcategoryspec
;
3980 for (j
= (1 << BYTEWIDTH
); j
>= 0; j
--)
3981 if ((CHAR_HAS_CATEGORY (j
, k
)) ^ not)
3984 /* Any leading code can possibly start a character which
3985 has or doesn't has the specified category. */
3986 if (match_any_multibyte_characters
== false)
3988 for (j
= MIN_MULTIBYTE_LEADING_CODE
;
3989 j
<= MAX_MULTIBYTE_LEADING_CODE
; j
++)
3991 match_any_multibyte_characters
= true;
3995 /* All cases after this match the empty string. These end with
4015 EXTRACT_NUMBER_AND_INCR (j
, p
);
4017 /* Backward jumps can only go back to code that we've already
4018 visited. `re_compile' should make sure this is true. */
4023 case on_failure_jump
:
4024 case on_failure_keep_string_jump
:
4025 case on_failure_jump_loop
:
4026 case on_failure_jump_nastyloop
:
4027 case on_failure_jump_smart
:
4033 /* Keep `p1' to allow the `on_failure_jump' we are jumping to
4034 to jump back to "just after here". */
4037 case on_failure_jump
:
4038 case on_failure_keep_string_jump
:
4039 case on_failure_jump_nastyloop
:
4040 case on_failure_jump_loop
:
4041 case on_failure_jump_smart
:
4042 EXTRACT_NUMBER_AND_INCR (j
, p
);
4044 ; /* Backward jump to be ignored. */
4046 { /* We have to look down both arms.
4047 We first go down the "straight" path so as to minimize
4048 stack usage when going through alternatives. */
4049 int r
= analyze_first (p
, pend
, fastmap
, multibyte
);
4057 /* This code simply does not properly handle forward jump_n. */
4058 DEBUG_STATEMENT (EXTRACT_NUMBER (j
, p
); assert (j
< 0));
4060 /* jump_n can either jump or fall through. The (backward) jump
4061 case has already been handled, so we only need to look at the
4062 fallthrough case. */
4066 /* If N == 0, it should be an on_failure_jump_loop instead. */
4067 DEBUG_STATEMENT (EXTRACT_NUMBER (j
, p
+ 2); assert (j
> 0));
4069 /* We only care about one iteration of the loop, so we don't
4070 need to consider the case where this behaves like an
4087 abort (); /* We have listed all the cases. */
4090 /* Getting here means we have found the possible starting
4091 characters for one path of the pattern -- and that the empty
4092 string does not match. We need not follow this path further. */
4096 /* We reached the end without matching anything. */
4099 } /* analyze_first */
4101 /* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
4102 BUFP. A fastmap records which of the (1 << BYTEWIDTH) possible
4103 characters can start a string that matches the pattern. This fastmap
4104 is used by re_search to skip quickly over impossible starting points.
4106 Character codes above (1 << BYTEWIDTH) are not represented in the
4107 fastmap, but the leading codes are represented. Thus, the fastmap
4108 indicates which character sets could start a match.
4110 The caller must supply the address of a (1 << BYTEWIDTH)-byte data
4111 area as BUFP->fastmap.
4113 We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in
4116 Returns 0 if we succeed, -2 if an internal error. */
4119 re_compile_fastmap (struct re_pattern_buffer
*bufp
)
4121 char *fastmap
= bufp
->fastmap
;
4124 assert (fastmap
&& bufp
->buffer
);
4126 memset (fastmap
, 0, 1 << BYTEWIDTH
); /* Assume nothing's valid. */
4127 bufp
->fastmap_accurate
= 1; /* It will be when we're done. */
4129 analysis
= analyze_first (bufp
->buffer
, bufp
->buffer
+ bufp
->used
,
4130 fastmap
, RE_MULTIBYTE_P (bufp
));
4131 bufp
->can_be_null
= (analysis
!= 0);
4133 } /* re_compile_fastmap */
4135 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
4136 ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
4137 this memory for recording register information. STARTS and ENDS
4138 must be allocated using the malloc library routine, and must each
4139 be at least NUM_REGS * sizeof (regoff_t) bytes long.
4141 If NUM_REGS == 0, then subsequent matches should allocate their own
4144 Unless this function is called, the first search or match using
4145 PATTERN_BUFFER will allocate its own register data, without
4146 freeing the old data. */
4149 re_set_registers (struct re_pattern_buffer
*bufp
, struct re_registers
*regs
, unsigned int num_regs
, regoff_t
*starts
, regoff_t
*ends
)
4153 bufp
->regs_allocated
= REGS_REALLOCATE
;
4154 regs
->num_regs
= num_regs
;
4155 regs
->start
= starts
;
4160 bufp
->regs_allocated
= REGS_UNALLOCATED
;
4162 regs
->start
= regs
->end
= 0;
4165 WEAK_ALIAS (__re_set_registers
, re_set_registers
)
4167 /* Searching routines. */
4169 /* Like re_search_2, below, but only one string is specified, and
4170 doesn't let you say where to stop matching. */
4173 re_search (struct re_pattern_buffer
*bufp
, const char *string
, size_t size
,
4174 ssize_t startpos
, ssize_t range
, struct re_registers
*regs
)
4176 return re_search_2 (bufp
, NULL
, 0, string
, size
, startpos
, range
,
4179 WEAK_ALIAS (__re_search
, re_search
)
4181 /* Head address of virtual concatenation of string. */
4182 #define HEAD_ADDR_VSTRING(P) \
4183 (((P) >= size1 ? string2 : string1))
4185 /* Address of POS in the concatenation of virtual string. */
4186 #define POS_ADDR_VSTRING(POS) \
4187 (((POS) >= size1 ? string2 - size1 : string1) + (POS))
4189 /* Using the compiled pattern in BUFP->buffer, first tries to match the
4190 virtual concatenation of STRING1 and STRING2, starting first at index
4191 STARTPOS, then at STARTPOS + 1, and so on.
4193 STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
4195 RANGE is how far to scan while trying to match. RANGE = 0 means try
4196 only at STARTPOS; in general, the last start tried is STARTPOS +
4199 In REGS, return the indices of the virtual concatenation of STRING1
4200 and STRING2 that matched the entire BUFP->buffer and its contained
4203 Do not consider matching one past the index STOP in the virtual
4204 concatenation of STRING1 and STRING2.
4206 We return either the position in the strings at which the match was
4207 found, -1 if no match, or -2 if error (such as failure
4211 re_search_2 (struct re_pattern_buffer
*bufp
, const char *str1
, size_t size1
,
4212 const char *str2
, size_t size2
, ssize_t startpos
, ssize_t range
,
4213 struct re_registers
*regs
, ssize_t stop
)
4216 re_char
*string1
= (re_char
*) str1
;
4217 re_char
*string2
= (re_char
*) str2
;
4218 register char *fastmap
= bufp
->fastmap
;
4219 register RE_TRANSLATE_TYPE translate
= bufp
->translate
;
4220 size_t total_size
= size1
+ size2
;
4221 ssize_t endpos
= startpos
+ range
;
4222 boolean anchored_start
;
4223 /* Nonzero if we are searching multibyte string. */
4224 const boolean multibyte
= RE_TARGET_MULTIBYTE_P (bufp
);
4226 /* Check for out-of-range STARTPOS. */
4227 if (startpos
< 0 || startpos
> total_size
)
4230 /* Fix up RANGE if it might eventually take us outside
4231 the virtual concatenation of STRING1 and STRING2.
4232 Make sure we won't move STARTPOS below 0 or above TOTAL_SIZE. */
4234 range
= 0 - startpos
;
4235 else if (endpos
> total_size
)
4236 range
= total_size
- startpos
;
4238 /* If the search isn't to be a backwards one, don't waste time in a
4239 search for a pattern anchored at beginning of buffer. */
4240 if (bufp
->used
> 0 && (re_opcode_t
) bufp
->buffer
[0] == begbuf
&& range
> 0)
4249 /* In a forward search for something that starts with \=.
4250 don't keep searching past point. */
4251 if (bufp
->used
> 0 && (re_opcode_t
) bufp
->buffer
[0] == at_dot
&& range
> 0)
4253 range
= PT_BYTE
- BEGV_BYTE
- startpos
;
4259 /* Update the fastmap now if not correct already. */
4260 if (fastmap
&& !bufp
->fastmap_accurate
)
4261 re_compile_fastmap (bufp
);
4263 /* See whether the pattern is anchored. */
4264 anchored_start
= (bufp
->buffer
[0] == begline
);
4267 gl_state
.object
= re_match_object
; /* Used by SYNTAX_TABLE_BYTE_TO_CHAR. */
4269 ssize_t charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (POS_AS_IN_BUFFER (startpos
));
4271 SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object
, charpos
, 1);
4275 /* Loop through the string, looking for a place to start matching. */
4278 /* If the pattern is anchored,
4279 skip quickly past places we cannot match.
4280 We don't bother to treat startpos == 0 specially
4281 because that case doesn't repeat. */
4282 if (anchored_start
&& startpos
> 0)
4284 if (! ((startpos
<= size1
? string1
[startpos
- 1]
4285 : string2
[startpos
- size1
- 1])
4290 /* If a fastmap is supplied, skip quickly over characters that
4291 cannot be the start of a match. If the pattern can match the
4292 null string, however, we don't need to skip characters; we want
4293 the first null string. */
4294 if (fastmap
&& startpos
< total_size
&& !bufp
->can_be_null
)
4296 register re_char
*d
;
4297 register re_wchar_t buf_ch
;
4299 d
= POS_ADDR_VSTRING (startpos
);
4301 if (range
> 0) /* Searching forwards. */
4303 ssize_t irange
= range
, lim
= 0;
4305 if (startpos
< size1
&& startpos
+ range
>= size1
)
4306 lim
= range
- (size1
- startpos
);
4308 /* Written out as an if-else to avoid testing `translate'
4310 if (RE_TRANSLATE_P (translate
))
4317 buf_ch
= STRING_CHAR_AND_LENGTH (d
, buf_charlen
);
4318 buf_ch
= RE_TRANSLATE (translate
, buf_ch
);
4319 if (fastmap
[CHAR_LEADING_CODE (buf_ch
)])
4322 range
-= buf_charlen
;
4328 register re_wchar_t ch
, translated
;
4331 ch
= RE_CHAR_TO_MULTIBYTE (buf_ch
);
4332 translated
= RE_TRANSLATE (translate
, ch
);
4333 if (translated
!= ch
4334 && (ch
= RE_CHAR_TO_UNIBYTE (translated
)) >= 0)
4336 if (fastmap
[buf_ch
])
4349 buf_ch
= STRING_CHAR_AND_LENGTH (d
, buf_charlen
);
4350 if (fastmap
[CHAR_LEADING_CODE (buf_ch
)])
4352 range
-= buf_charlen
;
4356 while (range
> lim
&& !fastmap
[*d
])
4362 startpos
+= irange
- range
;
4364 else /* Searching backwards. */
4368 buf_ch
= STRING_CHAR (d
);
4369 buf_ch
= TRANSLATE (buf_ch
);
4370 if (! fastmap
[CHAR_LEADING_CODE (buf_ch
)])
4375 register re_wchar_t ch
, translated
;
4378 ch
= RE_CHAR_TO_MULTIBYTE (buf_ch
);
4379 translated
= TRANSLATE (ch
);
4380 if (translated
!= ch
4381 && (ch
= RE_CHAR_TO_UNIBYTE (translated
)) >= 0)
4383 if (! fastmap
[TRANSLATE (buf_ch
)])
4389 /* If can't match the null string, and that's all we have left, fail. */
4390 if (range
>= 0 && startpos
== total_size
&& fastmap
4391 && !bufp
->can_be_null
)
4394 val
= re_match_2_internal (bufp
, string1
, size1
, string2
, size2
,
4395 startpos
, regs
, stop
);
4408 /* Update STARTPOS to the next character boundary. */
4411 re_char
*p
= POS_ADDR_VSTRING (startpos
);
4412 int len
= BYTES_BY_CHAR_HEAD (*p
);
4430 /* Update STARTPOS to the previous character boundary. */
4433 re_char
*p
= POS_ADDR_VSTRING (startpos
) + 1;
4435 re_char
*phead
= HEAD_ADDR_VSTRING (startpos
);
4437 /* Find the head of multibyte form. */
4438 PREV_CHAR_BOUNDARY (p
, phead
);
4439 range
+= p0
- 1 - p
;
4443 startpos
-= p0
- 1 - p
;
4449 WEAK_ALIAS (__re_search_2
, re_search_2
)
4451 /* Declarations and macros for re_match_2. */
4453 static int bcmp_translate (re_char
*s1
, re_char
*s2
,
4454 register ssize_t len
,
4455 RE_TRANSLATE_TYPE translate
,
4456 const int multibyte
);
4458 /* This converts PTR, a pointer into one of the search strings `string1'
4459 and `string2' into an offset from the beginning of that string. */
4460 #define POINTER_TO_OFFSET(ptr) \
4461 (FIRST_STRING_P (ptr) \
4463 : (ptr) - string2 + (ptrdiff_t) size1)
4465 /* Call before fetching a character with *d. This switches over to
4466 string2 if necessary.
4467 Check re_match_2_internal for a discussion of why end_match_2 might
4468 not be within string2 (but be equal to end_match_1 instead). */
4469 #define PREFETCH() \
4472 /* End of string2 => fail. */ \
4473 if (dend == end_match_2) \
4475 /* End of string1 => advance to string2. */ \
4477 dend = end_match_2; \
4480 /* Call before fetching a char with *d if you already checked other limits.
4481 This is meant for use in lookahead operations like wordend, etc..
4482 where we might need to look at parts of the string that might be
4483 outside of the LIMITs (i.e past `stop'). */
4484 #define PREFETCH_NOLIMIT() \
4488 dend = end_match_2; \
4491 /* Test if at very beginning or at very end of the virtual concatenation
4492 of `string1' and `string2'. If only one string, it's `string2'. */
4493 #define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2)
4494 #define AT_STRINGS_END(d) ((d) == end2)
4496 /* Disabled due to a compiler bug -- see comment at case wordbound */
4498 /* The comment at case wordbound is following one, but we don't use
4499 AT_WORD_BOUNDARY anymore to support multibyte form.
4501 The DEC Alpha C compiler 3.x generates incorrect code for the
4502 test WORDCHAR_P (d - 1) != WORDCHAR_P (d) in the expansion of
4503 AT_WORD_BOUNDARY, so this code is disabled. Expanding the
4504 macro and introducing temporary variables works around the bug. */
4507 /* Test if D points to a character which is word-constituent. We have
4508 two special cases to check for: if past the end of string1, look at
4509 the first character in string2; and if before the beginning of
4510 string2, look at the last character in string1. */
4511 #define WORDCHAR_P(d) \
4512 (SYNTAX ((d) == end1 ? *string2 \
4513 : (d) == string2 - 1 ? *(end1 - 1) : *(d)) \
4516 /* Test if the character before D and the one at D differ with respect
4517 to being word-constituent. */
4518 #define AT_WORD_BOUNDARY(d) \
4519 (AT_STRINGS_BEG (d) || AT_STRINGS_END (d) \
4520 || WORDCHAR_P (d - 1) != WORDCHAR_P (d))
4523 /* Free everything we malloc. */
4524 #ifdef MATCH_MAY_ALLOCATE
4525 # define FREE_VAR(var) \
4533 # define FREE_VARIABLES() \
4535 REGEX_FREE_STACK (fail_stack.stack); \
4536 FREE_VAR (regstart); \
4537 FREE_VAR (regend); \
4538 FREE_VAR (best_regstart); \
4539 FREE_VAR (best_regend); \
4540 REGEX_SAFE_FREE (); \
4543 # define FREE_VARIABLES() ((void)0) /* Do nothing! But inhibit gcc warning. */
4544 #endif /* not MATCH_MAY_ALLOCATE */
4547 /* Optimization routines. */
4549 /* If the operation is a match against one or more chars,
4550 return a pointer to the next operation, else return NULL. */
4552 skip_one_char (re_char
*p
)
4565 if (CHARSET_RANGE_TABLE_EXISTS_P (p
- 1))
4568 p
= CHARSET_RANGE_TABLE (p
- 1);
4569 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
4570 p
= CHARSET_RANGE_TABLE_END (p
, mcnt
);
4573 p
+= 1 + CHARSET_BITMAP_SIZE (p
- 1);
4580 case notcategoryspec
:
4592 /* Jump over non-matching operations. */
4594 skip_noops (re_char
*p
, re_char
*pend
)
4608 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
4619 /* Test if C matches charset op. *PP points to the charset or charset_not
4620 opcode. When the function finishes, *PP will be advanced past that opcode.
4621 C is character to test (possibly after translations) and CORIG is original
4622 character (i.e. without any translations). UNIBYTE denotes whether c is
4623 unibyte or multibyte character. */
4625 execute_charset (re_char
**pp
, unsigned c
, unsigned corig
, bool unibyte
)
4627 re_char
*p
= *pp
, *rtp
= NULL
;
4628 bool not = (re_opcode_t
) *p
== charset_not
;
4630 if (CHARSET_RANGE_TABLE_EXISTS_P (p
))
4633 rtp
= CHARSET_RANGE_TABLE (p
);
4634 EXTRACT_NUMBER_AND_INCR (count
, rtp
);
4635 *pp
= CHARSET_RANGE_TABLE_END ((rtp
), (count
));
4638 *pp
+= 2 + CHARSET_BITMAP_SIZE (p
);
4640 if (unibyte
&& c
< (1 << BYTEWIDTH
))
4641 { /* Lookup bitmap. */
4642 /* Cast to `unsigned' instead of `unsigned char' in
4643 case the bit list is a full 32 bytes long. */
4644 if (c
< (unsigned) (CHARSET_BITMAP_SIZE (p
) * BYTEWIDTH
)
4645 && p
[2 + c
/ BYTEWIDTH
] & (1 << (c
% BYTEWIDTH
)))
4651 int class_bits
= CHARSET_RANGE_TABLE_BITS (p
);
4652 re_wchar_t range_start
, range_end
;
4654 /* Sort tests by the most commonly used classes with some adjustment to which
4655 tests are easiest to perform. Take a look at comment in re_wctype_parse
4656 for table with frequencies of character class names. */
4658 if ((class_bits
& BIT_MULTIBYTE
) ||
4659 (class_bits
& BIT_ALNUM
&& ISALNUM (c
)) ||
4660 (class_bits
& BIT_ALPHA
&& ISALPHA (c
)) ||
4661 (class_bits
& BIT_SPACE
&& ISSPACE (c
)) ||
4662 (class_bits
& BIT_BLANK
&& ISBLANK (c
)) ||
4663 (class_bits
& BIT_WORD
&& ISWORD (c
)) ||
4664 ((class_bits
& BIT_UPPER
) &&
4665 (ISUPPER (c
) || (corig
!= c
&&
4666 c
== downcase (corig
) && ISLOWER (c
)))) ||
4667 ((class_bits
& BIT_LOWER
) &&
4668 (ISLOWER (c
) || (corig
!= c
&&
4669 c
== upcase (corig
) && ISUPPER(c
)))) ||
4670 (class_bits
& BIT_PUNCT
&& ISPUNCT (c
)) ||
4671 (class_bits
& BIT_GRAPH
&& ISGRAPH (c
)) ||
4672 (class_bits
& BIT_PRINT
&& ISPRINT (c
)))
4675 for (p
= *pp
; rtp
< p
; rtp
+= 2 * 3)
4677 EXTRACT_CHARACTER (range_start
, rtp
);
4678 EXTRACT_CHARACTER (range_end
, rtp
+ 3);
4679 if (range_start
<= c
&& c
<= range_end
)
4687 /* Non-zero if "p1 matches something" implies "p2 fails". */
4689 mutually_exclusive_p (struct re_pattern_buffer
*bufp
, re_char
*p1
,
4693 const boolean multibyte
= RE_MULTIBYTE_P (bufp
);
4694 unsigned char *pend
= bufp
->buffer
+ bufp
->used
;
4696 assert (p1
>= bufp
->buffer
&& p1
< pend
4697 && p2
>= bufp
->buffer
&& p2
<= pend
);
4699 /* Skip over open/close-group commands.
4700 If what follows this loop is a ...+ construct,
4701 look at what begins its body, since we will have to
4702 match at least one of that. */
4703 p2
= skip_noops (p2
, pend
);
4704 /* The same skip can be done for p1, except that this function
4705 is only used in the case where p1 is a simple match operator. */
4706 /* p1 = skip_noops (p1, pend); */
4708 assert (p1
>= bufp
->buffer
&& p1
< pend
4709 && p2
>= bufp
->buffer
&& p2
<= pend
);
4711 op2
= p2
== pend
? succeed
: *p2
;
4717 /* If we're at the end of the pattern, we can change. */
4718 if (skip_one_char (p1
))
4720 DEBUG_PRINT (" End of pattern: fast loop.\n");
4728 register re_wchar_t c
4729 = (re_opcode_t
) *p2
== endline
? '\n'
4730 : RE_STRING_CHAR (p2
+ 2, multibyte
);
4732 if ((re_opcode_t
) *p1
== exactn
)
4734 if (c
!= RE_STRING_CHAR (p1
+ 2, multibyte
))
4736 DEBUG_PRINT (" '%c' != '%c' => fast loop.\n", c
, p1
[2]);
4741 else if ((re_opcode_t
) *p1
== charset
4742 || (re_opcode_t
) *p1
== charset_not
)
4744 if (!execute_charset (&p1
, c
, c
, !multibyte
|| IS_REAL_ASCII (c
)))
4746 DEBUG_PRINT (" No match => fast loop.\n");
4750 else if ((re_opcode_t
) *p1
== anychar
4753 DEBUG_PRINT (" . != \\n => fast loop.\n");
4761 if ((re_opcode_t
) *p1
== exactn
)
4762 /* Reuse the code above. */
4763 return mutually_exclusive_p (bufp
, p2
, p1
);
4765 /* It is hard to list up all the character in charset
4766 P2 if it includes multibyte character. Give up in
4768 else if (!multibyte
|| !CHARSET_RANGE_TABLE_EXISTS_P (p2
))
4770 /* Now, we are sure that P2 has no range table.
4771 So, for the size of bitmap in P2, `p2[1]' is
4772 enough. But P1 may have range table, so the
4773 size of bitmap table of P1 is extracted by
4774 using macro `CHARSET_BITMAP_SIZE'.
4776 In a multibyte case, we know that all the character
4777 listed in P2 is ASCII. In a unibyte case, P1 has only a
4778 bitmap table. So, in both cases, it is enough to test
4779 only the bitmap table of P1. */
4781 if ((re_opcode_t
) *p1
== charset
)
4784 /* We win if the charset inside the loop
4785 has no overlap with the one after the loop. */
4788 && idx
< CHARSET_BITMAP_SIZE (p1
));
4790 if ((p2
[2 + idx
] & p1
[2 + idx
]) != 0)
4794 || idx
== CHARSET_BITMAP_SIZE (p1
))
4796 DEBUG_PRINT (" No match => fast loop.\n");
4800 else if ((re_opcode_t
) *p1
== charset_not
)
4803 /* We win if the charset_not inside the loop lists
4804 every character listed in the charset after. */
4805 for (idx
= 0; idx
< (int) p2
[1]; idx
++)
4806 if (! (p2
[2 + idx
] == 0
4807 || (idx
< CHARSET_BITMAP_SIZE (p1
)
4808 && ((p2
[2 + idx
] & ~ p1
[2 + idx
]) == 0))))
4813 DEBUG_PRINT (" No match => fast loop.\n");
4826 /* Reuse the code above. */
4827 return mutually_exclusive_p (bufp
, p2
, p1
);
4829 /* When we have two charset_not, it's very unlikely that
4830 they don't overlap. The union of the two sets of excluded
4831 chars should cover all possible chars, which, as a matter of
4832 fact, is virtually impossible in multibyte buffers. */
4838 return ((re_opcode_t
) *p1
== syntaxspec
&& p1
[1] == Sword
);
4840 return ((re_opcode_t
) *p1
== syntaxspec
4841 && (p1
[1] == Ssymbol
|| p1
[1] == Sword
));
4843 return ((re_opcode_t
) *p1
== syntaxspec
&& p1
[1] == p2
[1]);
4846 return ((re_opcode_t
) *p1
== notsyntaxspec
&& p1
[1] == Sword
);
4848 return ((re_opcode_t
) *p1
== notsyntaxspec
4849 && (p1
[1] == Ssymbol
|| p1
[1] == Sword
));
4851 return ((re_opcode_t
) *p1
== notsyntaxspec
&& p1
[1] == p2
[1]);
4854 return (((re_opcode_t
) *p1
== notsyntaxspec
4855 || (re_opcode_t
) *p1
== syntaxspec
)
4860 return ((re_opcode_t
) *p1
== notcategoryspec
&& p1
[1] == p2
[1]);
4861 case notcategoryspec
:
4862 return ((re_opcode_t
) *p1
== categoryspec
&& p1
[1] == p2
[1]);
4874 /* Matching routines. */
4876 #ifndef emacs /* Emacs never uses this. */
4877 /* re_match is like re_match_2 except it takes only a single string. */
4880 re_match (struct re_pattern_buffer
*bufp
, const char *string
,
4881 size_t size
, ssize_t pos
, struct re_registers
*regs
)
4883 regoff_t result
= re_match_2_internal (bufp
, NULL
, 0, (re_char
*) string
,
4884 size
, pos
, regs
, size
);
4887 WEAK_ALIAS (__re_match
, re_match
)
4888 #endif /* not emacs */
4890 /* re_match_2 matches the compiled pattern in BUFP against the
4891 the (virtual) concatenation of STRING1 and STRING2 (of length SIZE1
4892 and SIZE2, respectively). We start matching at POS, and stop
4895 If REGS is non-null and the `no_sub' field of BUFP is nonzero, we
4896 store offsets for the substring each group matched in REGS. See the
4897 documentation for exactly how many groups we fill.
4899 We return -1 if no match, -2 if an internal error (such as the
4900 failure stack overflowing). Otherwise, we return the length of the
4901 matched substring. */
4904 re_match_2 (struct re_pattern_buffer
*bufp
, const char *string1
,
4905 size_t size1
, const char *string2
, size_t size2
, ssize_t pos
,
4906 struct re_registers
*regs
, ssize_t stop
)
4912 gl_state
.object
= re_match_object
; /* Used by SYNTAX_TABLE_BYTE_TO_CHAR. */
4913 charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (POS_AS_IN_BUFFER (pos
));
4914 SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object
, charpos
, 1);
4917 result
= re_match_2_internal (bufp
, (re_char
*) string1
, size1
,
4918 (re_char
*) string2
, size2
,
4922 WEAK_ALIAS (__re_match_2
, re_match_2
)
4925 /* This is a separate function so that we can force an alloca cleanup
4928 re_match_2_internal (struct re_pattern_buffer
*bufp
, re_char
*string1
,
4929 size_t size1
, re_char
*string2
, size_t size2
,
4930 ssize_t pos
, struct re_registers
*regs
, ssize_t stop
)
4932 /* General temporaries. */
4936 /* Just past the end of the corresponding string. */
4937 re_char
*end1
, *end2
;
4939 /* Pointers into string1 and string2, just past the last characters in
4940 each to consider matching. */
4941 re_char
*end_match_1
, *end_match_2
;
4943 /* Where we are in the data, and the end of the current string. */
4946 /* Used sometimes to remember where we were before starting matching
4947 an operator so that we can go back in case of failure. This "atomic"
4948 behavior of matching opcodes is indispensable to the correctness
4949 of the on_failure_keep_string_jump optimization. */
4952 /* Where we are in the pattern, and the end of the pattern. */
4953 re_char
*p
= bufp
->buffer
;
4954 re_char
*pend
= p
+ bufp
->used
;
4956 /* We use this to map every character in the string. */
4957 RE_TRANSLATE_TYPE translate
= bufp
->translate
;
4959 /* Nonzero if BUFP is setup from a multibyte regex. */
4960 const boolean multibyte
= RE_MULTIBYTE_P (bufp
);
4962 /* Nonzero if STRING1/STRING2 are multibyte. */
4963 const boolean target_multibyte
= RE_TARGET_MULTIBYTE_P (bufp
);
4965 /* Failure point stack. Each place that can handle a failure further
4966 down the line pushes a failure point on this stack. It consists of
4967 regstart, and regend for all registers corresponding to
4968 the subexpressions we're currently inside, plus the number of such
4969 registers, and, finally, two char *'s. The first char * is where
4970 to resume scanning the pattern; the second one is where to resume
4971 scanning the strings. */
4972 #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
4973 fail_stack_type fail_stack
;
4975 #ifdef DEBUG_COMPILES_ARGUMENTS
4976 unsigned nfailure_points_pushed
= 0, nfailure_points_popped
= 0;
4979 #if defined REL_ALLOC && defined REGEX_MALLOC
4980 /* This holds the pointer to the failure stack, when
4981 it is allocated relocatably. */
4982 fail_stack_elt_t
*failure_stack_ptr
;
4985 /* We fill all the registers internally, independent of what we
4986 return, for use in backreferences. The number here includes
4987 an element for register zero. */
4988 size_t num_regs
= bufp
->re_nsub
+ 1;
4990 /* Information on the contents of registers. These are pointers into
4991 the input strings; they record just what was matched (on this
4992 attempt) by a subexpression part of the pattern, that is, the
4993 regnum-th regstart pointer points to where in the pattern we began
4994 matching and the regnum-th regend points to right after where we
4995 stopped matching the regnum-th subexpression. (The zeroth register
4996 keeps track of what the whole pattern matches.) */
4997 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
4998 re_char
**regstart
, **regend
;
5001 /* The following record the register info as found in the above
5002 variables when we find a match better than any we've seen before.
5003 This happens as we backtrack through the failure points, which in
5004 turn happens only if we have not yet matched the entire string. */
5005 unsigned best_regs_set
= false;
5006 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
5007 re_char
**best_regstart
, **best_regend
;
5010 /* Logically, this is `best_regend[0]'. But we don't want to have to
5011 allocate space for that if we're not allocating space for anything
5012 else (see below). Also, we never need info about register 0 for
5013 any of the other register vectors, and it seems rather a kludge to
5014 treat `best_regend' differently than the rest. So we keep track of
5015 the end of the best match so far in a separate variable. We
5016 initialize this to NULL so that when we backtrack the first time
5017 and need to test it, it's not garbage. */
5018 re_char
*match_end
= NULL
;
5020 #ifdef DEBUG_COMPILES_ARGUMENTS
5021 /* Counts the total number of registers pushed. */
5022 unsigned num_regs_pushed
= 0;
5025 DEBUG_PRINT ("\n\nEntering re_match_2.\n");
5027 REGEX_USE_SAFE_ALLOCA
;
5031 #ifdef MATCH_MAY_ALLOCATE
5032 /* Do not bother to initialize all the register variables if there are
5033 no groups in the pattern, as it takes a fair amount of time. If
5034 there are groups, we include space for register 0 (the whole
5035 pattern), even though we never use it, since it simplifies the
5036 array indexing. We should fix this. */
5039 regstart
= REGEX_TALLOC (num_regs
, re_char
*);
5040 regend
= REGEX_TALLOC (num_regs
, re_char
*);
5041 best_regstart
= REGEX_TALLOC (num_regs
, re_char
*);
5042 best_regend
= REGEX_TALLOC (num_regs
, re_char
*);
5044 if (!(regstart
&& regend
&& best_regstart
&& best_regend
))
5052 /* We must initialize all our variables to NULL, so that
5053 `FREE_VARIABLES' doesn't try to free them. */
5054 regstart
= regend
= best_regstart
= best_regend
= NULL
;
5056 #endif /* MATCH_MAY_ALLOCATE */
5058 /* The starting position is bogus. */
5059 if (pos
< 0 || pos
> size1
+ size2
)
5065 /* Initialize subexpression text positions to -1 to mark ones that no
5066 start_memory/stop_memory has been seen for. Also initialize the
5067 register information struct. */
5068 for (reg
= 1; reg
< num_regs
; reg
++)
5069 regstart
[reg
] = regend
[reg
] = NULL
;
5071 /* We move `string1' into `string2' if the latter's empty -- but not if
5072 `string1' is null. */
5073 if (size2
== 0 && string1
!= NULL
)
5080 end1
= string1
+ size1
;
5081 end2
= string2
+ size2
;
5083 /* `p' scans through the pattern as `d' scans through the data.
5084 `dend' is the end of the input string that `d' points within. `d'
5085 is advanced into the following input string whenever necessary, but
5086 this happens before fetching; therefore, at the beginning of the
5087 loop, `d' can be pointing at the end of a string, but it cannot
5091 /* Only match within string2. */
5092 d
= string2
+ pos
- size1
;
5093 dend
= end_match_2
= string2
+ stop
- size1
;
5094 end_match_1
= end1
; /* Just to give it a value. */
5100 /* Only match within string1. */
5101 end_match_1
= string1
+ stop
;
5103 When we reach end_match_1, PREFETCH normally switches to string2.
5104 But in the present case, this means that just doing a PREFETCH
5105 makes us jump from `stop' to `gap' within the string.
5106 What we really want here is for the search to stop as
5107 soon as we hit end_match_1. That's why we set end_match_2
5108 to end_match_1 (since PREFETCH fails as soon as we hit
5110 end_match_2
= end_match_1
;
5113 { /* It's important to use this code when stop == size so that
5114 moving `d' from end1 to string2 will not prevent the d == dend
5115 check from catching the end of string. */
5117 end_match_2
= string2
+ stop
- size1
;
5123 DEBUG_PRINT ("The compiled pattern is: ");
5124 DEBUG_PRINT_COMPILED_PATTERN (bufp
, p
, pend
);
5125 DEBUG_PRINT ("The string to match is: \"");
5126 DEBUG_PRINT_DOUBLE_STRING (d
, string1
, size1
, string2
, size2
);
5127 DEBUG_PRINT ("\"\n");
5129 /* This loops over pattern commands. It exits by returning from the
5130 function if the match is complete, or it drops through if the match
5131 fails at this starting point in the input data. */
5134 DEBUG_PRINT ("\n%p: ", p
);
5138 /* End of pattern means we might have succeeded. */
5139 DEBUG_PRINT ("end of pattern ... ");
5141 /* If we haven't matched the entire string, and we want the
5142 longest match, try backtracking. */
5143 if (d
!= end_match_2
)
5145 /* True if this match is the best seen so far. */
5149 /* True if this match ends in the same string (string1
5150 or string2) as the best previous match. */
5151 bool same_str_p
= (FIRST_STRING_P (match_end
)
5152 == FIRST_STRING_P (d
));
5154 /* AIX compiler got confused when this was combined
5155 with the previous declaration. */
5157 best_match_p
= d
> match_end
;
5159 best_match_p
= !FIRST_STRING_P (d
);
5162 DEBUG_PRINT ("backtracking.\n");
5164 if (!FAIL_STACK_EMPTY ())
5165 { /* More failure points to try. */
5167 /* If exceeds best match so far, save it. */
5168 if (!best_regs_set
|| best_match_p
)
5170 best_regs_set
= true;
5173 DEBUG_PRINT ("\nSAVING match as best so far.\n");
5175 for (reg
= 1; reg
< num_regs
; reg
++)
5177 best_regstart
[reg
] = regstart
[reg
];
5178 best_regend
[reg
] = regend
[reg
];
5184 /* If no failure points, don't restore garbage. And if
5185 last match is real best match, don't restore second
5187 else if (best_regs_set
&& !best_match_p
)
5190 /* Restore best match. It may happen that `dend ==
5191 end_match_1' while the restored d is in string2.
5192 For example, the pattern `x.*y.*z' against the
5193 strings `x-' and `y-z-', if the two strings are
5194 not consecutive in memory. */
5195 DEBUG_PRINT ("Restoring best registers.\n");
5198 dend
= ((d
>= string1
&& d
<= end1
)
5199 ? end_match_1
: end_match_2
);
5201 for (reg
= 1; reg
< num_regs
; reg
++)
5203 regstart
[reg
] = best_regstart
[reg
];
5204 regend
[reg
] = best_regend
[reg
];
5207 } /* d != end_match_2 */
5210 DEBUG_PRINT ("Accepting match.\n");
5212 /* If caller wants register contents data back, do it. */
5213 if (regs
&& !bufp
->no_sub
)
5215 /* Have the register data arrays been allocated? */
5216 if (bufp
->regs_allocated
== REGS_UNALLOCATED
)
5217 { /* No. So allocate them with malloc. We need one
5218 extra element beyond `num_regs' for the `-1' marker
5220 regs
->num_regs
= max (RE_NREGS
, num_regs
+ 1);
5221 regs
->start
= TALLOC (regs
->num_regs
, regoff_t
);
5222 regs
->end
= TALLOC (regs
->num_regs
, regoff_t
);
5223 if (regs
->start
== NULL
|| regs
->end
== NULL
)
5228 bufp
->regs_allocated
= REGS_REALLOCATE
;
5230 else if (bufp
->regs_allocated
== REGS_REALLOCATE
)
5231 { /* Yes. If we need more elements than were already
5232 allocated, reallocate them. If we need fewer, just
5234 if (regs
->num_regs
< num_regs
+ 1)
5236 regs
->num_regs
= num_regs
+ 1;
5237 RETALLOC (regs
->start
, regs
->num_regs
, regoff_t
);
5238 RETALLOC (regs
->end
, regs
->num_regs
, regoff_t
);
5239 if (regs
->start
== NULL
|| regs
->end
== NULL
)
5248 /* These braces fend off a "empty body in an else-statement"
5249 warning under GCC when assert expands to nothing. */
5250 assert (bufp
->regs_allocated
== REGS_FIXED
);
5253 /* Convert the pointer data in `regstart' and `regend' to
5254 indices. Register zero has to be set differently,
5255 since we haven't kept track of any info for it. */
5256 if (regs
->num_regs
> 0)
5258 regs
->start
[0] = pos
;
5259 regs
->end
[0] = POINTER_TO_OFFSET (d
);
5262 /* Go through the first `min (num_regs, regs->num_regs)'
5263 registers, since that is all we initialized. */
5264 for (reg
= 1; reg
< min (num_regs
, regs
->num_regs
); reg
++)
5266 if (REG_UNSET (regstart
[reg
]) || REG_UNSET (regend
[reg
]))
5267 regs
->start
[reg
] = regs
->end
[reg
] = -1;
5270 regs
->start
[reg
] = POINTER_TO_OFFSET (regstart
[reg
]);
5271 regs
->end
[reg
] = POINTER_TO_OFFSET (regend
[reg
]);
5275 /* If the regs structure we return has more elements than
5276 were in the pattern, set the extra elements to -1. If
5277 we (re)allocated the registers, this is the case,
5278 because we always allocate enough to have at least one
5280 for (reg
= num_regs
; reg
< regs
->num_regs
; reg
++)
5281 regs
->start
[reg
] = regs
->end
[reg
] = -1;
5282 } /* regs && !bufp->no_sub */
5284 DEBUG_PRINT ("%u failure points pushed, %u popped (%u remain).\n",
5285 nfailure_points_pushed
, nfailure_points_popped
,
5286 nfailure_points_pushed
- nfailure_points_popped
);
5287 DEBUG_PRINT ("%u registers pushed.\n", num_regs_pushed
);
5289 ptrdiff_t dcnt
= POINTER_TO_OFFSET (d
) - pos
;
5291 DEBUG_PRINT ("Returning %td from re_match_2.\n", dcnt
);
5297 /* Otherwise match next pattern command. */
5300 /* Ignore these. Used to ignore the n of succeed_n's which
5301 currently have n == 0. */
5303 DEBUG_PRINT ("EXECUTING no_op.\n");
5307 DEBUG_PRINT ("EXECUTING succeed.\n");
5310 /* Match the next n pattern characters exactly. The following
5311 byte in the pattern defines n, and the n bytes after that
5312 are the characters to match. */
5315 DEBUG_PRINT ("EXECUTING exactn %d.\n", mcnt
);
5317 /* Remember the start point to rollback upon failure. */
5321 /* This is written out as an if-else so we don't waste time
5322 testing `translate' inside the loop. */
5323 if (RE_TRANSLATE_P (translate
))
5327 if (RE_TRANSLATE (translate
, *d
) != *p
++)
5347 /* The cost of testing `translate' is comparatively small. */
5348 if (target_multibyte
)
5351 int pat_charlen
, buf_charlen
;
5356 pat_ch
= STRING_CHAR_AND_LENGTH (p
, pat_charlen
);
5359 pat_ch
= RE_CHAR_TO_MULTIBYTE (*p
);
5362 buf_ch
= STRING_CHAR_AND_LENGTH (d
, buf_charlen
);
5364 if (TRANSLATE (buf_ch
) != pat_ch
)
5372 mcnt
-= pat_charlen
;
5384 pat_ch
= STRING_CHAR_AND_LENGTH (p
, pat_charlen
);
5385 pat_ch
= RE_CHAR_TO_UNIBYTE (pat_ch
);
5392 buf_ch
= RE_CHAR_TO_MULTIBYTE (*d
);
5393 if (! CHAR_BYTE8_P (buf_ch
))
5395 buf_ch
= TRANSLATE (buf_ch
);
5396 buf_ch
= RE_CHAR_TO_UNIBYTE (buf_ch
);
5402 if (buf_ch
!= pat_ch
)
5415 /* Match any character except possibly a newline or a null. */
5420 reg_syntax_t syntax
;
5422 DEBUG_PRINT ("EXECUTING anychar.\n");
5425 buf_ch
= RE_STRING_CHAR_AND_LENGTH (d
, buf_charlen
,
5427 buf_ch
= TRANSLATE (buf_ch
);
5430 syntax
= RE_SYNTAX_EMACS
;
5432 syntax
= bufp
->syntax
;
5435 if ((!(syntax
& RE_DOT_NEWLINE
) && buf_ch
== '\n')
5436 || ((syntax
& RE_DOT_NOT_NULL
) && buf_ch
== '\000'))
5439 DEBUG_PRINT (" Matched \"%d\".\n", *d
);
5448 register unsigned int c
, corig
;
5451 /* Whether matching against a unibyte character. */
5452 boolean unibyte_char
= false;
5454 DEBUG_PRINT ("EXECUTING charset%s.\n",
5455 (re_opcode_t
) *(p
- 1) == charset_not
? "_not" : "");
5458 corig
= c
= RE_STRING_CHAR_AND_LENGTH (d
, len
, target_multibyte
);
5459 if (target_multibyte
)
5464 c1
= RE_CHAR_TO_UNIBYTE (c
);
5467 unibyte_char
= true;
5473 int c1
= RE_CHAR_TO_MULTIBYTE (c
);
5475 if (! CHAR_BYTE8_P (c1
))
5477 c1
= TRANSLATE (c1
);
5478 c1
= RE_CHAR_TO_UNIBYTE (c1
);
5481 unibyte_char
= true;
5486 unibyte_char
= true;
5490 if (!execute_charset (&p
, c
, corig
, unibyte_char
))
5498 /* The beginning of a group is represented by start_memory.
5499 The argument is the register number. The text
5500 matched within the group is recorded (in the internal
5501 registers data structure) under the register number. */
5503 DEBUG_PRINT ("EXECUTING start_memory %d:\n", *p
);
5505 /* In case we need to undo this operation (via backtracking). */
5506 PUSH_FAILURE_REG (*p
);
5509 regend
[*p
] = NULL
; /* probably unnecessary. -sm */
5510 DEBUG_PRINT (" regstart: %td\n", POINTER_TO_OFFSET (regstart
[*p
]));
5512 /* Move past the register number and inner group count. */
5517 /* The stop_memory opcode represents the end of a group. Its
5518 argument is the same as start_memory's: the register number. */
5520 DEBUG_PRINT ("EXECUTING stop_memory %d:\n", *p
);
5522 assert (!REG_UNSET (regstart
[*p
]));
5523 /* Strictly speaking, there should be code such as:
5525 assert (REG_UNSET (regend[*p]));
5526 PUSH_FAILURE_REGSTOP ((unsigned int)*p);
5528 But the only info to be pushed is regend[*p] and it is known to
5529 be UNSET, so there really isn't anything to push.
5530 Not pushing anything, on the other hand deprives us from the
5531 guarantee that regend[*p] is UNSET since undoing this operation
5532 will not reset its value properly. This is not important since
5533 the value will only be read on the next start_memory or at
5534 the very end and both events can only happen if this stop_memory
5538 DEBUG_PRINT (" regend: %td\n", POINTER_TO_OFFSET (regend
[*p
]));
5540 /* Move past the register number and the inner group count. */
5545 /* \<digit> has been turned into a `duplicate' command which is
5546 followed by the numeric value of <digit> as the register number. */
5549 register re_char
*d2
, *dend2
;
5550 int regno
= *p
++; /* Get which register to match against. */
5551 DEBUG_PRINT ("EXECUTING duplicate %d.\n", regno
);
5553 /* Can't back reference a group which we've never matched. */
5554 if (REG_UNSET (regstart
[regno
]) || REG_UNSET (regend
[regno
]))
5557 /* Where in input to try to start matching. */
5558 d2
= regstart
[regno
];
5560 /* Remember the start point to rollback upon failure. */
5563 /* Where to stop matching; if both the place to start and
5564 the place to stop matching are in the same string, then
5565 set to the place to stop, otherwise, for now have to use
5566 the end of the first string. */
5568 dend2
= ((FIRST_STRING_P (regstart
[regno
])
5569 == FIRST_STRING_P (regend
[regno
]))
5570 ? regend
[regno
] : end_match_1
);
5575 /* If necessary, advance to next segment in register
5579 if (dend2
== end_match_2
) break;
5580 if (dend2
== regend
[regno
]) break;
5582 /* End of string1 => advance to string2. */
5584 dend2
= regend
[regno
];
5586 /* At end of register contents => success */
5587 if (d2
== dend2
) break;
5589 /* If necessary, advance to next segment in data. */
5592 /* How many characters left in this segment to match. */
5595 /* Want how many consecutive characters we can match in
5596 one shot, so, if necessary, adjust the count. */
5597 if (dcnt
> dend2
- d2
)
5600 /* Compare that many; failure if mismatch, else move
5602 if (RE_TRANSLATE_P (translate
)
5603 ? bcmp_translate (d
, d2
, dcnt
, translate
, target_multibyte
)
5604 : memcmp (d
, d2
, dcnt
))
5609 d
+= dcnt
, d2
+= dcnt
;
5615 /* begline matches the empty string at the beginning of the string
5616 (unless `not_bol' is set in `bufp'), and after newlines. */
5618 DEBUG_PRINT ("EXECUTING begline.\n");
5620 if (AT_STRINGS_BEG (d
))
5622 if (!bufp
->not_bol
) break;
5627 GET_CHAR_BEFORE_2 (c
, d
, string1
, end1
, string2
, end2
);
5631 /* In all other cases, we fail. */
5635 /* endline is the dual of begline. */
5637 DEBUG_PRINT ("EXECUTING endline.\n");
5639 if (AT_STRINGS_END (d
))
5641 if (!bufp
->not_eol
) break;
5645 PREFETCH_NOLIMIT ();
5652 /* Match at the very beginning of the data. */
5654 DEBUG_PRINT ("EXECUTING begbuf.\n");
5655 if (AT_STRINGS_BEG (d
))
5660 /* Match at the very end of the data. */
5662 DEBUG_PRINT ("EXECUTING endbuf.\n");
5663 if (AT_STRINGS_END (d
))
5668 /* on_failure_keep_string_jump is used to optimize `.*\n'. It
5669 pushes NULL as the value for the string on the stack. Then
5670 `POP_FAILURE_POINT' will keep the current value for the
5671 string, instead of restoring it. To see why, consider
5672 matching `foo\nbar' against `.*\n'. The .* matches the foo;
5673 then the . fails against the \n. But the next thing we want
5674 to do is match the \n against the \n; if we restored the
5675 string value, we would be back at the foo.
5677 Because this is used only in specific cases, we don't need to
5678 check all the things that `on_failure_jump' does, to make
5679 sure the right things get saved on the stack. Hence we don't
5680 share its code. The only reason to push anything on the
5681 stack at all is that otherwise we would have to change
5682 `anychar's code to do something besides goto fail in this
5683 case; that seems worse than this. */
5684 case on_failure_keep_string_jump
:
5685 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5686 DEBUG_PRINT ("EXECUTING on_failure_keep_string_jump %d (to %p):\n",
5689 PUSH_FAILURE_POINT (p
- 3, NULL
);
5692 /* A nasty loop is introduced by the non-greedy *? and +?.
5693 With such loops, the stack only ever contains one failure point
5694 at a time, so that a plain on_failure_jump_loop kind of
5695 cycle detection cannot work. Worse yet, such a detection
5696 can not only fail to detect a cycle, but it can also wrongly
5697 detect a cycle (between different instantiations of the same
5699 So the method used for those nasty loops is a little different:
5700 We use a special cycle-detection-stack-frame which is pushed
5701 when the on_failure_jump_nastyloop failure-point is *popped*.
5702 This special frame thus marks the beginning of one iteration
5703 through the loop and we can hence easily check right here
5704 whether something matched between the beginning and the end of
5706 case on_failure_jump_nastyloop
:
5707 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5708 DEBUG_PRINT ("EXECUTING on_failure_jump_nastyloop %d (to %p):\n",
5711 assert ((re_opcode_t
)p
[-4] == no_op
);
5714 CHECK_INFINITE_LOOP (p
- 4, d
);
5716 /* If there's a cycle, just continue without pushing
5717 this failure point. The failure point is the "try again"
5718 option, which shouldn't be tried.
5719 We want (x?)*?y\1z to match both xxyz and xxyxz. */
5720 PUSH_FAILURE_POINT (p
- 3, d
);
5724 /* Simple loop detecting on_failure_jump: just check on the
5725 failure stack if the same spot was already hit earlier. */
5726 case on_failure_jump_loop
:
5728 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5729 DEBUG_PRINT ("EXECUTING on_failure_jump_loop %d (to %p):\n",
5733 CHECK_INFINITE_LOOP (p
- 3, d
);
5735 /* If there's a cycle, get out of the loop, as if the matching
5736 had failed. We used to just `goto fail' here, but that was
5737 aborting the search a bit too early: we want to keep the
5738 empty-loop-match and keep matching after the loop.
5739 We want (x?)*y\1z to match both xxyz and xxyxz. */
5742 PUSH_FAILURE_POINT (p
- 3, d
);
5747 /* Uses of on_failure_jump:
5749 Each alternative starts with an on_failure_jump that points
5750 to the beginning of the next alternative. Each alternative
5751 except the last ends with a jump that in effect jumps past
5752 the rest of the alternatives. (They really jump to the
5753 ending jump of the following alternative, because tensioning
5754 these jumps is a hassle.)
5756 Repeats start with an on_failure_jump that points past both
5757 the repetition text and either the following jump or
5758 pop_failure_jump back to this on_failure_jump. */
5759 case on_failure_jump
:
5760 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5761 DEBUG_PRINT ("EXECUTING on_failure_jump %d (to %p):\n",
5764 PUSH_FAILURE_POINT (p
-3, d
);
5767 /* This operation is used for greedy *.
5768 Compare the beginning of the repeat with what in the
5769 pattern follows its end. If we can establish that there
5770 is nothing that they would both match, i.e., that we
5771 would have to backtrack because of (as in, e.g., `a*a')
5772 then we can use a non-backtracking loop based on
5773 on_failure_keep_string_jump instead of on_failure_jump. */
5774 case on_failure_jump_smart
:
5775 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5776 DEBUG_PRINT ("EXECUTING on_failure_jump_smart %d (to %p).\n",
5779 re_char
*p1
= p
; /* Next operation. */
5780 /* Here, we discard `const', making re_match non-reentrant. */
5781 unsigned char *p2
= (unsigned char *) p
+ mcnt
; /* Jump dest. */
5782 unsigned char *p3
= (unsigned char *) p
- 3; /* opcode location. */
5784 p
-= 3; /* Reset so that we will re-execute the
5785 instruction once it's been changed. */
5787 EXTRACT_NUMBER (mcnt
, p2
- 2);
5789 /* Ensure this is a indeed the trivial kind of loop
5790 we are expecting. */
5791 assert (skip_one_char (p1
) == p2
- 3);
5792 assert ((re_opcode_t
) p2
[-3] == jump
&& p2
+ mcnt
== p
);
5793 DEBUG_STATEMENT (debug
+= 2);
5794 if (mutually_exclusive_p (bufp
, p1
, p2
))
5796 /* Use a fast `on_failure_keep_string_jump' loop. */
5797 DEBUG_PRINT (" smart exclusive => fast loop.\n");
5798 *p3
= (unsigned char) on_failure_keep_string_jump
;
5799 STORE_NUMBER (p2
- 2, mcnt
+ 3);
5803 /* Default to a safe `on_failure_jump' loop. */
5804 DEBUG_PRINT (" smart default => slow loop.\n");
5805 *p3
= (unsigned char) on_failure_jump
;
5807 DEBUG_STATEMENT (debug
-= 2);
5811 /* Unconditionally jump (without popping any failure points). */
5815 EXTRACT_NUMBER_AND_INCR (mcnt
, p
); /* Get the amount to jump. */
5816 DEBUG_PRINT ("EXECUTING jump %d ", mcnt
);
5817 p
+= mcnt
; /* Do the jump. */
5818 DEBUG_PRINT ("(to %p).\n", p
);
5822 /* Have to succeed matching what follows at least n times.
5823 After that, handle like `on_failure_jump'. */
5825 /* Signedness doesn't matter since we only compare MCNT to 0. */
5826 EXTRACT_NUMBER (mcnt
, p
+ 2);
5827 DEBUG_PRINT ("EXECUTING succeed_n %d.\n", mcnt
);
5829 /* Originally, mcnt is how many times we HAVE to succeed. */
5832 /* Here, we discard `const', making re_match non-reentrant. */
5833 unsigned char *p2
= (unsigned char *) p
+ 2; /* counter loc. */
5836 PUSH_NUMBER (p2
, mcnt
);
5839 /* The two bytes encoding mcnt == 0 are two no_op opcodes. */
5844 /* Signedness doesn't matter since we only compare MCNT to 0. */
5845 EXTRACT_NUMBER (mcnt
, p
+ 2);
5846 DEBUG_PRINT ("EXECUTING jump_n %d.\n", mcnt
);
5848 /* Originally, this is how many times we CAN jump. */
5851 /* Here, we discard `const', making re_match non-reentrant. */
5852 unsigned char *p2
= (unsigned char *) p
+ 2; /* counter loc. */
5854 PUSH_NUMBER (p2
, mcnt
);
5855 goto unconditional_jump
;
5857 /* If don't have to jump any more, skip over the rest of command. */
5864 unsigned char *p2
; /* Location of the counter. */
5865 DEBUG_PRINT ("EXECUTING set_number_at.\n");
5867 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5868 /* Here, we discard `const', making re_match non-reentrant. */
5869 p2
= (unsigned char *) p
+ mcnt
;
5870 /* Signedness doesn't matter since we only copy MCNT's bits. */
5871 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5872 DEBUG_PRINT (" Setting %p to %d.\n", p2
, mcnt
);
5873 PUSH_NUMBER (p2
, mcnt
);
5880 boolean
not = (re_opcode_t
) *(p
- 1) == notwordbound
;
5881 DEBUG_PRINT ("EXECUTING %swordbound.\n", not ? "not" : "");
5883 /* We SUCCEED (or FAIL) in one of the following cases: */
5885 /* Case 1: D is at the beginning or the end of string. */
5886 if (AT_STRINGS_BEG (d
) || AT_STRINGS_END (d
))
5890 /* C1 is the character before D, S1 is the syntax of C1, C2
5891 is the character at D, and S2 is the syntax of C2. */
5896 ssize_t offset
= PTR_TO_OFFSET (d
- 1);
5897 ssize_t charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
5898 UPDATE_SYNTAX_TABLE_FAST (charpos
);
5900 GET_CHAR_BEFORE_2 (c1
, d
, string1
, end1
, string2
, end2
);
5903 UPDATE_SYNTAX_TABLE_FORWARD_FAST (charpos
+ 1);
5905 PREFETCH_NOLIMIT ();
5906 GET_CHAR_AFTER (c2
, d
, dummy
);
5909 if (/* Case 2: Only one of S1 and S2 is Sword. */
5910 ((s1
== Sword
) != (s2
== Sword
))
5911 /* Case 3: Both of S1 and S2 are Sword, and macro
5912 WORD_BOUNDARY_P (C1, C2) returns nonzero. */
5913 || ((s1
== Sword
) && WORD_BOUNDARY_P (c1
, c2
)))
5923 DEBUG_PRINT ("EXECUTING wordbeg.\n");
5925 /* We FAIL in one of the following cases: */
5927 /* Case 1: D is at the end of string. */
5928 if (AT_STRINGS_END (d
))
5932 /* C1 is the character before D, S1 is the syntax of C1, C2
5933 is the character at D, and S2 is the syntax of C2. */
5938 ssize_t offset
= PTR_TO_OFFSET (d
);
5939 ssize_t charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
5940 UPDATE_SYNTAX_TABLE_FAST (charpos
);
5943 GET_CHAR_AFTER (c2
, d
, dummy
);
5946 /* Case 2: S2 is not Sword. */
5950 /* Case 3: D is not at the beginning of string ... */
5951 if (!AT_STRINGS_BEG (d
))
5953 GET_CHAR_BEFORE_2 (c1
, d
, string1
, end1
, string2
, end2
);
5955 UPDATE_SYNTAX_TABLE_BACKWARD (charpos
- 1);
5959 /* ... and S1 is Sword, and WORD_BOUNDARY_P (C1, C2)
5961 if ((s1
== Sword
) && !WORD_BOUNDARY_P (c1
, c2
))
5968 DEBUG_PRINT ("EXECUTING wordend.\n");
5970 /* We FAIL in one of the following cases: */
5972 /* Case 1: D is at the beginning of string. */
5973 if (AT_STRINGS_BEG (d
))
5977 /* C1 is the character before D, S1 is the syntax of C1, C2
5978 is the character at D, and S2 is the syntax of C2. */
5983 ssize_t offset
= PTR_TO_OFFSET (d
) - 1;
5984 ssize_t charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
5985 UPDATE_SYNTAX_TABLE_FAST (charpos
);
5987 GET_CHAR_BEFORE_2 (c1
, d
, string1
, end1
, string2
, end2
);
5990 /* Case 2: S1 is not Sword. */
5994 /* Case 3: D is not at the end of string ... */
5995 if (!AT_STRINGS_END (d
))
5997 PREFETCH_NOLIMIT ();
5998 GET_CHAR_AFTER (c2
, d
, dummy
);
6000 UPDATE_SYNTAX_TABLE_FORWARD_FAST (charpos
);
6004 /* ... and S2 is Sword, and WORD_BOUNDARY_P (C1, C2)
6006 if ((s2
== Sword
) && !WORD_BOUNDARY_P (c1
, c2
))
6013 DEBUG_PRINT ("EXECUTING symbeg.\n");
6015 /* We FAIL in one of the following cases: */
6017 /* Case 1: D is at the end of string. */
6018 if (AT_STRINGS_END (d
))
6022 /* C1 is the character before D, S1 is the syntax of C1, C2
6023 is the character at D, and S2 is the syntax of C2. */
6027 ssize_t offset
= PTR_TO_OFFSET (d
);
6028 ssize_t charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
6029 UPDATE_SYNTAX_TABLE_FAST (charpos
);
6032 c2
= RE_STRING_CHAR (d
, target_multibyte
);
6035 /* Case 2: S2 is neither Sword nor Ssymbol. */
6036 if (s2
!= Sword
&& s2
!= Ssymbol
)
6039 /* Case 3: D is not at the beginning of string ... */
6040 if (!AT_STRINGS_BEG (d
))
6042 GET_CHAR_BEFORE_2 (c1
, d
, string1
, end1
, string2
, end2
);
6044 UPDATE_SYNTAX_TABLE_BACKWARD (charpos
- 1);
6048 /* ... and S1 is Sword or Ssymbol. */
6049 if (s1
== Sword
|| s1
== Ssymbol
)
6056 DEBUG_PRINT ("EXECUTING symend.\n");
6058 /* We FAIL in one of the following cases: */
6060 /* Case 1: D is at the beginning of string. */
6061 if (AT_STRINGS_BEG (d
))
6065 /* C1 is the character before D, S1 is the syntax of C1, C2
6066 is the character at D, and S2 is the syntax of C2. */
6070 ssize_t offset
= PTR_TO_OFFSET (d
) - 1;
6071 ssize_t charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
6072 UPDATE_SYNTAX_TABLE_FAST (charpos
);
6074 GET_CHAR_BEFORE_2 (c1
, d
, string1
, end1
, string2
, end2
);
6077 /* Case 2: S1 is neither Ssymbol nor Sword. */
6078 if (s1
!= Sword
&& s1
!= Ssymbol
)
6081 /* Case 3: D is not at the end of string ... */
6082 if (!AT_STRINGS_END (d
))
6084 PREFETCH_NOLIMIT ();
6085 c2
= RE_STRING_CHAR (d
, target_multibyte
);
6087 UPDATE_SYNTAX_TABLE_FORWARD_FAST (charpos
+ 1);
6091 /* ... and S2 is Sword or Ssymbol. */
6092 if (s2
== Sword
|| s2
== Ssymbol
)
6101 boolean
not = (re_opcode_t
) *(p
- 1) == notsyntaxspec
;
6103 DEBUG_PRINT ("EXECUTING %ssyntaxspec %d.\n", not ? "not" : "",
6108 ssize_t offset
= PTR_TO_OFFSET (d
);
6109 ssize_t pos1
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
6110 UPDATE_SYNTAX_TABLE_FAST (pos1
);
6117 GET_CHAR_AFTER (c
, d
, len
);
6118 if ((SYNTAX (c
) != (enum syntaxcode
) mcnt
) ^ not)
6127 DEBUG_PRINT ("EXECUTING at_dot.\n");
6128 if (PTR_BYTE_POS (d
) != PT_BYTE
)
6133 case notcategoryspec
:
6135 boolean
not = (re_opcode_t
) *(p
- 1) == notcategoryspec
;
6137 DEBUG_PRINT ("EXECUTING %scategoryspec %d.\n",
6138 not ? "not" : "", mcnt
);
6144 GET_CHAR_AFTER (c
, d
, len
);
6145 if ((!CHAR_HAS_CATEGORY (c
, mcnt
)) ^ not)
6157 continue; /* Successfully executed one pattern command; keep going. */
6160 /* We goto here if a matching operation fails. */
6163 if (!FAIL_STACK_EMPTY ())
6166 /* A restart point is known. Restore to that state. */
6167 DEBUG_PRINT ("\nFAIL:\n");
6168 POP_FAILURE_POINT (str
, pat
);
6171 case on_failure_keep_string_jump
:
6172 assert (str
== NULL
);
6173 goto continue_failure_jump
;
6175 case on_failure_jump_nastyloop
:
6176 assert ((re_opcode_t
)pat
[-2] == no_op
);
6177 PUSH_FAILURE_POINT (pat
- 2, str
);
6179 case on_failure_jump_loop
:
6180 case on_failure_jump
:
6183 continue_failure_jump
:
6184 EXTRACT_NUMBER_AND_INCR (mcnt
, pat
);
6189 /* A special frame used for nastyloops. */
6196 assert (p
>= bufp
->buffer
&& p
<= pend
);
6198 if (d
>= string1
&& d
<= end1
)
6202 break; /* Matching at this starting point really fails. */
6206 goto restore_best_regs
;
6210 return -1; /* Failure to match. */
6213 /* Subroutine definitions for re_match_2. */
6215 /* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
6216 bytes; nonzero otherwise. */
6219 bcmp_translate (re_char
*s1
, re_char
*s2
, ssize_t len
,
6220 RE_TRANSLATE_TYPE translate
, const int target_multibyte
)
6222 re_char
*p1
= s1
, *p2
= s2
;
6223 re_char
*p1_end
= s1
+ len
;
6224 re_char
*p2_end
= s2
+ len
;
6226 /* FIXME: Checking both p1 and p2 presumes that the two strings might have
6227 different lengths, but relying on a single `len' would break this. -sm */
6228 while (p1
< p1_end
&& p2
< p2_end
)
6230 int p1_charlen
, p2_charlen
;
6231 re_wchar_t p1_ch
, p2_ch
;
6233 GET_CHAR_AFTER (p1_ch
, p1
, p1_charlen
);
6234 GET_CHAR_AFTER (p2_ch
, p2
, p2_charlen
);
6236 if (RE_TRANSLATE (translate
, p1_ch
)
6237 != RE_TRANSLATE (translate
, p2_ch
))
6240 p1
+= p1_charlen
, p2
+= p2_charlen
;
6243 if (p1
!= p1_end
|| p2
!= p2_end
)
6249 /* Entry points for GNU code. */
6251 /* re_compile_pattern is the GNU regular expression compiler: it
6252 compiles PATTERN (of length SIZE) and puts the result in BUFP.
6253 Returns 0 if the pattern was valid, otherwise an error string.
6255 Assumes the `allocated' (and perhaps `buffer') and `translate' fields
6256 are set in BUFP on entry.
6258 We call regex_compile to do the actual compilation. */
6261 re_compile_pattern (const char *pattern
, size_t length
,
6263 bool posix_backtracking
, const char *whitespace_regexp
,
6265 struct re_pattern_buffer
*bufp
)
6269 /* GNU code is written to assume at least RE_NREGS registers will be set
6270 (and at least one extra will be -1). */
6271 bufp
->regs_allocated
= REGS_UNALLOCATED
;
6273 /* And GNU code determines whether or not to get register information
6274 by passing null for the REGS argument to re_match, etc., not by
6278 ret
= regex_compile ((re_char
*) pattern
, length
,
6289 return gettext (re_error_msgid
[(int) ret
]);
6291 WEAK_ALIAS (__re_compile_pattern
, re_compile_pattern
)
6293 /* Entry points compatible with 4.2 BSD regex library. We don't define
6294 them unless specifically requested. */
6296 #if defined _REGEX_RE_COMP || defined _LIBC
6298 /* BSD has one and only one pattern buffer. */
6299 static struct re_pattern_buffer re_comp_buf
;
6303 /* Make these definitions weak in libc, so POSIX programs can redefine
6304 these names if they don't use our functions, and still use
6305 regcomp/regexec below without link errors. */
6308 re_comp (const char *s
)
6314 if (!re_comp_buf
.buffer
)
6315 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6316 return (char *) gettext ("No previous regular expression");
6320 if (!re_comp_buf
.buffer
)
6322 re_comp_buf
.buffer
= malloc (200);
6323 if (re_comp_buf
.buffer
== NULL
)
6324 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6325 return (char *) gettext (re_error_msgid
[(int) REG_ESPACE
]);
6326 re_comp_buf
.allocated
= 200;
6328 re_comp_buf
.fastmap
= malloc (1 << BYTEWIDTH
);
6329 if (re_comp_buf
.fastmap
== NULL
)
6330 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6331 return (char *) gettext (re_error_msgid
[(int) REG_ESPACE
]);
6334 /* Since `re_exec' always passes NULL for the `regs' argument, we
6335 don't need to initialize the pattern buffer fields which affect it. */
6337 ret
= regex_compile (s
, strlen (s
), re_syntax_options
, &re_comp_buf
);
6342 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6343 return (char *) gettext (re_error_msgid
[(int) ret
]);
6351 re_exec (const char *s
)
6353 const size_t len
= strlen (s
);
6354 return re_search (&re_comp_buf
, s
, len
, 0, len
, 0) >= 0;
6356 #endif /* _REGEX_RE_COMP */
6358 /* POSIX.2 functions. Don't define these for Emacs. */
6362 /* regcomp takes a regular expression as a string and compiles it.
6364 PREG is a regex_t *. We do not expect any fields to be initialized,
6365 since POSIX says we shouldn't. Thus, we set
6367 `buffer' to the compiled pattern;
6368 `used' to the length of the compiled pattern;
6369 `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
6370 REG_EXTENDED bit in CFLAGS is set; otherwise, to
6371 RE_SYNTAX_POSIX_BASIC;
6372 `fastmap' to an allocated space for the fastmap;
6373 `fastmap_accurate' to zero;
6374 `re_nsub' to the number of subexpressions in PATTERN.
6376 PATTERN is the address of the pattern string.
6378 CFLAGS is a series of bits which affect compilation.
6380 If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
6381 use POSIX basic syntax.
6383 If REG_NEWLINE is set, then . and [^...] don't match newline.
6384 Also, regexec will try a match beginning after every newline.
6386 If REG_ICASE is set, then we considers upper- and lowercase
6387 versions of letters to be equivalent when matching.
6389 If REG_NOSUB is set, then when PREG is passed to regexec, that
6390 routine will report only success or failure, and nothing about the
6393 It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
6394 the return codes and their meanings.) */
6397 regcomp (regex_t
*_Restrict_ preg
, const char *_Restrict_ pattern
,
6402 = (cflags
& REG_EXTENDED
) ?
6403 RE_SYNTAX_POSIX_EXTENDED
: RE_SYNTAX_POSIX_BASIC
;
6405 /* regex_compile will allocate the space for the compiled pattern. */
6407 preg
->allocated
= 0;
6410 /* Try to allocate space for the fastmap. */
6411 preg
->fastmap
= malloc (1 << BYTEWIDTH
);
6413 if (cflags
& REG_ICASE
)
6417 preg
->translate
= malloc (CHAR_SET_SIZE
* sizeof *preg
->translate
);
6418 if (preg
->translate
== NULL
)
6419 return (int) REG_ESPACE
;
6421 /* Map uppercase characters to corresponding lowercase ones. */
6422 for (i
= 0; i
< CHAR_SET_SIZE
; i
++)
6423 preg
->translate
[i
] = ISUPPER (i
) ? TOLOWER (i
) : i
;
6426 preg
->translate
= NULL
;
6428 /* If REG_NEWLINE is set, newlines are treated differently. */
6429 if (cflags
& REG_NEWLINE
)
6430 { /* REG_NEWLINE implies neither . nor [^...] match newline. */
6431 syntax
&= ~RE_DOT_NEWLINE
;
6432 syntax
|= RE_HAT_LISTS_NOT_NEWLINE
;
6435 syntax
|= RE_NO_NEWLINE_ANCHOR
;
6437 preg
->no_sub
= !!(cflags
& REG_NOSUB
);
6439 /* POSIX says a null character in the pattern terminates it, so we
6440 can use strlen here in compiling the pattern. */
6441 ret
= regex_compile ((re_char
*) pattern
, strlen (pattern
), syntax
, preg
);
6443 /* POSIX doesn't distinguish between an unmatched open-group and an
6444 unmatched close-group: both are REG_EPAREN. */
6445 if (ret
== REG_ERPAREN
)
6448 if (ret
== REG_NOERROR
&& preg
->fastmap
)
6449 { /* Compute the fastmap now, since regexec cannot modify the pattern
6451 re_compile_fastmap (preg
);
6452 if (preg
->can_be_null
)
6453 { /* The fastmap can't be used anyway. */
6454 free (preg
->fastmap
);
6455 preg
->fastmap
= NULL
;
6460 WEAK_ALIAS (__regcomp
, regcomp
)
6463 /* regexec searches for a given pattern, specified by PREG, in the
6466 If NMATCH is zero or REG_NOSUB was set in the cflags argument to
6467 `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
6468 least NMATCH elements, and we set them to the offsets of the
6469 corresponding matched substrings.
6471 EFLAGS specifies `execution flags' which affect matching: if
6472 REG_NOTBOL is set, then ^ does not match at the beginning of the
6473 string; if REG_NOTEOL is set, then $ does not match at the end.
6475 We return 0 if we find a match and REG_NOMATCH if not. */
6478 regexec (const regex_t
*_Restrict_ preg
, const char *_Restrict_ string
,
6479 size_t nmatch
, regmatch_t pmatch
[_Restrict_arr_
], int eflags
)
6482 struct re_registers regs
;
6483 regex_t private_preg
;
6484 size_t len
= strlen (string
);
6485 boolean want_reg_info
= !preg
->no_sub
&& nmatch
> 0 && pmatch
;
6487 private_preg
= *preg
;
6489 private_preg
.not_bol
= !!(eflags
& REG_NOTBOL
);
6490 private_preg
.not_eol
= !!(eflags
& REG_NOTEOL
);
6492 /* The user has told us exactly how many registers to return
6493 information about, via `nmatch'. We have to pass that on to the
6494 matching routines. */
6495 private_preg
.regs_allocated
= REGS_FIXED
;
6499 regs
.num_regs
= nmatch
;
6500 regs
.start
= TALLOC (nmatch
* 2, regoff_t
);
6501 if (regs
.start
== NULL
)
6503 regs
.end
= regs
.start
+ nmatch
;
6506 /* Instead of using not_eol to implement REG_NOTEOL, we could simply
6507 pass (&private_preg, string, len + 1, 0, len, ...) pretending the string
6508 was a little bit longer but still only matching the real part.
6509 This works because the `endline' will check for a '\n' and will find a
6510 '\0', correctly deciding that this is not the end of a line.
6511 But it doesn't work out so nicely for REG_NOTBOL, since we don't have
6512 a convenient '\0' there. For all we know, the string could be preceded
6513 by '\n' which would throw things off. */
6515 /* Perform the searching operation. */
6516 ret
= re_search (&private_preg
, string
, len
,
6517 /* start: */ 0, /* range: */ len
,
6518 want_reg_info
? ®s
: 0);
6520 /* Copy the register information to the POSIX structure. */
6527 for (r
= 0; r
< nmatch
; r
++)
6529 pmatch
[r
].rm_so
= regs
.start
[r
];
6530 pmatch
[r
].rm_eo
= regs
.end
[r
];
6534 /* If we needed the temporary register info, free the space now. */
6538 /* We want zero return to mean success, unlike `re_search'. */
6539 return ret
>= 0 ? REG_NOERROR
: REG_NOMATCH
;
6541 WEAK_ALIAS (__regexec
, regexec
)
6544 /* Returns a message corresponding to an error code, ERR_CODE, returned
6545 from either regcomp or regexec. We don't use PREG here.
6547 ERR_CODE was previously called ERRCODE, but that name causes an
6548 error with msvc8 compiler. */
6551 regerror (int err_code
, const regex_t
*preg
, char *errbuf
, size_t errbuf_size
)
6557 || err_code
>= (sizeof (re_error_msgid
) / sizeof (re_error_msgid
[0])))
6558 /* Only error codes returned by the rest of the code should be passed
6559 to this routine. If we are given anything else, or if other regex
6560 code generates an invalid error code, then the program has a bug.
6561 Dump core so we can fix it. */
6564 msg
= gettext (re_error_msgid
[err_code
]);
6566 msg_size
= strlen (msg
) + 1; /* Includes the null. */
6568 if (errbuf_size
!= 0)
6570 if (msg_size
> errbuf_size
)
6572 memcpy (errbuf
, msg
, errbuf_size
- 1);
6573 errbuf
[errbuf_size
- 1] = 0;
6576 strcpy (errbuf
, msg
);
6581 WEAK_ALIAS (__regerror
, regerror
)
6584 /* Free dynamically allocated space used by PREG. */
6587 regfree (regex_t
*preg
)
6589 free (preg
->buffer
);
6590 preg
->buffer
= NULL
;
6592 preg
->allocated
= 0;
6595 free (preg
->fastmap
);
6596 preg
->fastmap
= NULL
;
6597 preg
->fastmap_accurate
= 0;
6599 free (preg
->translate
);
6600 preg
->translate
= NULL
;
6602 WEAK_ALIAS (__regfree
, regfree
)
6604 #endif /* not emacs */