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, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
6 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
7 Free Software Foundation, Inc.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
25 - structure the opcode space into opcode+flag.
26 - merge with glibc's regex.[ch].
27 - replace (succeed_n + jump_n + set_number_at) with something that doesn't
28 need to modify the compiled regexp so that re_match can be reentrant.
29 - get rid of on_failure_jump_smart by doing the optimization in re_comp
30 rather than at run-time, so that re_match can be reentrant.
33 /* AIX requires this to be the first thing in the file. */
34 #if defined _AIX && !defined REGEX_MALLOC
42 #if defined STDC_HEADERS && !defined emacs
45 /* We need this for `regex.h', and perhaps for the Emacs include files. */
46 # include <sys/types.h>
49 /* Whether to use ISO C Amendment 1 wide char functions.
50 Those should not be used for Emacs since it uses its own. */
52 #define WIDE_CHAR_SUPPORT 1
54 #define WIDE_CHAR_SUPPORT \
55 (HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_BTOWC && !emacs)
58 /* For platform which support the ISO C amendement 1 functionality we
59 support user defined character classes. */
61 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
67 /* We have to keep the namespace clean. */
68 # define regfree(preg) __regfree (preg)
69 # define regexec(pr, st, nm, pm, ef) __regexec (pr, st, nm, pm, ef)
70 # define regcomp(preg, pattern, cflags) __regcomp (preg, pattern, cflags)
71 # define regerror(err_code, preg, errbuf, errbuf_size) \
72 __regerror(err_code, preg, errbuf, errbuf_size)
73 # define re_set_registers(bu, re, nu, st, en) \
74 __re_set_registers (bu, re, nu, st, en)
75 # define re_match_2(bufp, string1, size1, string2, size2, pos, regs, stop) \
76 __re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
77 # define re_match(bufp, string, size, pos, regs) \
78 __re_match (bufp, string, size, pos, regs)
79 # define re_search(bufp, string, size, startpos, range, regs) \
80 __re_search (bufp, string, size, startpos, range, regs)
81 # define re_compile_pattern(pattern, length, bufp) \
82 __re_compile_pattern (pattern, length, bufp)
83 # define re_set_syntax(syntax) __re_set_syntax (syntax)
84 # define re_search_2(bufp, st1, s1, st2, s2, startpos, range, regs, stop) \
85 __re_search_2 (bufp, st1, s1, st2, s2, startpos, range, regs, stop)
86 # define re_compile_fastmap(bufp) __re_compile_fastmap (bufp)
88 /* Make sure we call libc's function even if the user overrides them. */
89 # define btowc __btowc
90 # define iswctype __iswctype
91 # define wctype __wctype
93 # define WEAK_ALIAS(a,b) weak_alias (a, b)
95 /* We are also using some library internals. */
96 # include <locale/localeinfo.h>
97 # include <locale/elem-hash.h>
98 # include <langinfo.h>
100 # define WEAK_ALIAS(a,b)
103 /* This is for other GNU distributions with internationalized messages. */
104 #if HAVE_LIBINTL_H || defined _LIBC
105 # include <libintl.h>
107 # define gettext(msgid) (msgid)
111 /* This define is so xgettext can find the internationalizable
113 # define gettext_noop(String) String
116 /* The `emacs' switch turns on certain matching commands
117 that make sense only in Emacs. */
124 /* Make syntax table lookup grant data in gl_state. */
125 # define SYNTAX_ENTRY_VIA_PROPERTY
128 # include "character.h"
129 # include "category.h"
134 # define malloc xmalloc
138 # define realloc xrealloc
144 /* Converts the pointer to the char to BEG-based offset from the start. */
145 # define PTR_TO_OFFSET(d) POS_AS_IN_BUFFER (POINTER_TO_OFFSET (d))
146 # define POS_AS_IN_BUFFER(p) ((p) + (NILP (re_match_object) || BUFFERP (re_match_object)))
148 # define RE_MULTIBYTE_P(bufp) ((bufp)->multibyte)
149 # define RE_TARGET_MULTIBYTE_P(bufp) ((bufp)->target_multibyte)
150 # define RE_STRING_CHAR(p, multibyte) \
151 (multibyte ? (STRING_CHAR (p)) : (*(p)))
152 # define RE_STRING_CHAR_AND_LENGTH(p, len, multibyte) \
153 (multibyte ? (STRING_CHAR_AND_LENGTH (p, len)) : ((len) = 1, *(p)))
155 # define RE_CHAR_TO_MULTIBYTE(c) UNIBYTE_TO_CHAR (c)
157 # define RE_CHAR_TO_UNIBYTE(c) CHAR_TO_BYTE_SAFE (c)
159 /* Set C a (possibly converted to multibyte) character before P. P
160 points into a string which is the virtual concatenation of STR1
161 (which ends at END1) or STR2 (which ends at END2). */
162 # define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2) \
164 if (target_multibyte) \
166 re_char *dtemp = (p) == (str2) ? (end1) : (p); \
167 re_char *dlimit = ((p) > (str2) && (p) <= (end2)) ? (str2) : (str1); \
168 while (dtemp-- > dlimit && !CHAR_HEAD_P (*dtemp)); \
169 c = STRING_CHAR (dtemp); \
173 (c = ((p) == (str2) ? (end1) : (p))[-1]); \
174 (c) = RE_CHAR_TO_MULTIBYTE (c); \
178 /* Set C a (possibly converted to multibyte) character at P, and set
179 LEN to the byte length of that character. */
180 # define GET_CHAR_AFTER(c, p, len) \
182 if (target_multibyte) \
183 (c) = STRING_CHAR_AND_LENGTH (p, len); \
188 (c) = RE_CHAR_TO_MULTIBYTE (c); \
192 #else /* not emacs */
194 /* If we are not linking with Emacs proper,
195 we can't use the relocating allocator
196 even if config.h says that we can. */
201 /* When used in Emacs's lib-src, we need xmalloc and xrealloc. */
204 xmalloc (size_t size
)
207 val
= (void *) malloc (size
);
210 write (2, "virtual memory exhausted\n", 25);
217 xrealloc (void *block
, size_t size
)
220 /* We must call malloc explicitly when BLOCK is 0, since some
221 reallocs don't do this. */
223 val
= (void *) malloc (size
);
225 val
= (void *) realloc (block
, size
);
228 write (2, "virtual memory exhausted\n", 25);
237 # define malloc xmalloc
241 # define realloc xrealloc
243 /* This is the normal way of making sure we have memcpy, memcmp and memset. */
244 # if defined HAVE_STRING_H || defined STDC_HEADERS || defined _LIBC
247 # include <strings.h>
249 # define memcmp(s1, s2, n) bcmp (s1, s2, n)
252 # define memcpy(d, s, n) (bcopy (s, d, n), (d))
256 /* Define the syntax stuff for \<, \>, etc. */
258 /* Sword must be nonzero for the wordchar pattern commands in re_match_2. */
259 enum syntaxcode
{ Swhitespace
= 0, Sword
= 1, Ssymbol
= 2 };
261 # define SWITCH_ENUM_CAST(x) (x)
263 /* Dummy macros for non-Emacs environments. */
264 # define CHAR_CHARSET(c) 0
265 # define CHARSET_LEADING_CODE_BASE(c) 0
266 # define MAX_MULTIBYTE_LENGTH 1
267 # define RE_MULTIBYTE_P(x) 0
268 # define RE_TARGET_MULTIBYTE_P(x) 0
269 # define WORD_BOUNDARY_P(c1, c2) (0)
270 # define CHAR_HEAD_P(p) (1)
271 # define SINGLE_BYTE_CHAR_P(c) (1)
272 # define SAME_CHARSET_P(c1, c2) (1)
273 # define BYTES_BY_CHAR_HEAD(p) (1)
274 # define PREV_CHAR_BOUNDARY(p, limit) ((p)--)
275 # define STRING_CHAR(p) (*(p))
276 # define RE_STRING_CHAR(p, multibyte) STRING_CHAR (p)
277 # define CHAR_STRING(c, s) (*(s) = (c), 1)
278 # define STRING_CHAR_AND_LENGTH(p, actual_len) ((actual_len) = 1, *(p))
279 # define RE_STRING_CHAR_AND_LENGTH(p, len, multibyte) STRING_CHAR_AND_LENGTH (p, len)
280 # define RE_CHAR_TO_MULTIBYTE(c) (c)
281 # define RE_CHAR_TO_UNIBYTE(c) (c)
282 # define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2) \
283 (c = ((p) == (str2) ? *((end1) - 1) : *((p) - 1)))
284 # define GET_CHAR_AFTER(c, p, len) \
286 # define MAKE_CHAR(charset, c1, c2) (c1)
287 # define BYTE8_TO_CHAR(c) (c)
288 # define CHAR_BYTE8_P(c) (0)
289 # define CHAR_LEADING_CODE(c) (c)
291 #endif /* not emacs */
294 # define RE_TRANSLATE(TBL, C) ((unsigned char)(TBL)[C])
295 # define RE_TRANSLATE_P(TBL) (TBL)
298 /* Get the interface, including the syntax bits. */
301 /* isalpha etc. are used for the character classes. */
306 /* 1 if C is an ASCII character. */
307 # define IS_REAL_ASCII(c) ((c) < 0200)
309 /* 1 if C is a unibyte character. */
310 # define ISUNIBYTE(c) (SINGLE_BYTE_CHAR_P ((c)))
312 /* The Emacs definitions should not be directly affected by locales. */
314 /* In Emacs, these are only used for single-byte characters. */
315 # define ISDIGIT(c) ((c) >= '0' && (c) <= '9')
316 # define ISCNTRL(c) ((c) < ' ')
317 # define ISXDIGIT(c) (((c) >= '0' && (c) <= '9') \
318 || ((c) >= 'a' && (c) <= 'f') \
319 || ((c) >= 'A' && (c) <= 'F'))
321 /* This is only used for single-byte characters. */
322 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
324 /* The rest must handle multibyte characters. */
326 # define ISGRAPH(c) (SINGLE_BYTE_CHAR_P (c) \
327 ? (c) > ' ' && !((c) >= 0177 && (c) <= 0237) \
330 # define ISPRINT(c) (SINGLE_BYTE_CHAR_P (c) \
331 ? (c) >= ' ' && !((c) >= 0177 && (c) <= 0237) \
334 # define ISALNUM(c) (IS_REAL_ASCII (c) \
335 ? (((c) >= 'a' && (c) <= 'z') \
336 || ((c) >= 'A' && (c) <= 'Z') \
337 || ((c) >= '0' && (c) <= '9')) \
338 : SYNTAX (c) == Sword)
340 # define ISALPHA(c) (IS_REAL_ASCII (c) \
341 ? (((c) >= 'a' && (c) <= 'z') \
342 || ((c) >= 'A' && (c) <= 'Z')) \
343 : SYNTAX (c) == Sword)
345 # define ISLOWER(c) (LOWERCASEP (c))
347 # define ISPUNCT(c) (IS_REAL_ASCII (c) \
348 ? ((c) > ' ' && (c) < 0177 \
349 && !(((c) >= 'a' && (c) <= 'z') \
350 || ((c) >= 'A' && (c) <= 'Z') \
351 || ((c) >= '0' && (c) <= '9'))) \
352 : SYNTAX (c) != Sword)
354 # define ISSPACE(c) (SYNTAX (c) == Swhitespace)
356 # define ISUPPER(c) (UPPERCASEP (c))
358 # define ISWORD(c) (SYNTAX (c) == Sword)
360 #else /* not emacs */
362 /* Jim Meyering writes:
364 "... Some ctype macros are valid only for character codes that
365 isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
366 using /bin/cc or gcc but without giving an ansi option). So, all
367 ctype uses should be through macros like ISPRINT... If
368 STDC_HEADERS is defined, then autoconf has verified that the ctype
369 macros don't need to be guarded with references to isascii. ...
370 Defining isascii to 1 should let any compiler worth its salt
371 eliminate the && through constant folding."
372 Solaris defines some of these symbols so we must undefine them first. */
375 # if defined STDC_HEADERS || (!defined isascii && !defined HAVE_ISASCII)
376 # define ISASCII(c) 1
378 # define ISASCII(c) isascii(c)
381 /* 1 if C is an ASCII character. */
382 # define IS_REAL_ASCII(c) ((c) < 0200)
384 /* This distinction is not meaningful, except in Emacs. */
385 # define ISUNIBYTE(c) 1
388 # define ISBLANK(c) (ISASCII (c) && isblank (c))
390 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
393 # define ISGRAPH(c) (ISASCII (c) && isgraph (c))
395 # define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
399 # define ISPRINT(c) (ISASCII (c) && isprint (c))
400 # define ISDIGIT(c) (ISASCII (c) && isdigit (c))
401 # define ISALNUM(c) (ISASCII (c) && isalnum (c))
402 # define ISALPHA(c) (ISASCII (c) && isalpha (c))
403 # define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
404 # define ISLOWER(c) (ISASCII (c) && islower (c))
405 # define ISPUNCT(c) (ISASCII (c) && ispunct (c))
406 # define ISSPACE(c) (ISASCII (c) && isspace (c))
407 # define ISUPPER(c) (ISASCII (c) && isupper (c))
408 # define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
410 # define ISWORD(c) ISALPHA(c)
413 # define TOLOWER(c) _tolower(c)
415 # define TOLOWER(c) tolower(c)
418 /* How many characters in the character set. */
419 # define CHAR_SET_SIZE 256
423 extern char *re_syntax_table
;
425 # else /* not SYNTAX_TABLE */
427 static char re_syntax_table
[CHAR_SET_SIZE
];
430 init_syntax_once (void)
438 memset (re_syntax_table
, 0, sizeof re_syntax_table
);
440 for (c
= 0; c
< CHAR_SET_SIZE
; ++c
)
442 re_syntax_table
[c
] = Sword
;
444 re_syntax_table
['_'] = Ssymbol
;
449 # endif /* not SYNTAX_TABLE */
451 # define SYNTAX(c) re_syntax_table[(c)]
453 #endif /* not emacs */
456 # define NULL (void *)0
459 /* We remove any previous definition of `SIGN_EXTEND_CHAR',
460 since ours (we hope) works properly with all combinations of
461 machines, compilers, `char' and `unsigned char' argument types.
462 (Per Bothner suggested the basic approach.) */
463 #undef SIGN_EXTEND_CHAR
465 # define SIGN_EXTEND_CHAR(c) ((signed char) (c))
466 #else /* not __STDC__ */
467 /* As in Harbison and Steele. */
468 # define SIGN_EXTEND_CHAR(c) ((((unsigned char) (c)) ^ 128) - 128)
471 /* Should we use malloc or alloca? If REGEX_MALLOC is not defined, we
472 use `alloca' instead of `malloc'. This is because using malloc in
473 re_search* or re_match* could cause memory leaks when C-g is used in
474 Emacs; also, malloc is slower and causes storage fragmentation. On
475 the other hand, malloc is more portable, and easier to debug.
477 Because we sometimes use alloca, some routines have to be macros,
478 not functions -- `alloca'-allocated space disappears at the end of the
479 function it is called in. */
483 # define REGEX_ALLOCATE malloc
484 # define REGEX_REALLOCATE(source, osize, nsize) realloc (source, nsize)
485 # define REGEX_FREE free
487 #else /* not REGEX_MALLOC */
489 /* Emacs already defines alloca, sometimes. */
492 /* Make alloca work the best possible way. */
494 # define alloca __builtin_alloca
495 # else /* not __GNUC__ */
496 # ifdef HAVE_ALLOCA_H
498 # endif /* HAVE_ALLOCA_H */
499 # endif /* not __GNUC__ */
501 # endif /* not alloca */
503 # define REGEX_ALLOCATE alloca
505 /* Assumes a `char *destination' variable. */
506 # define REGEX_REALLOCATE(source, osize, nsize) \
507 (destination = (char *) alloca (nsize), \
508 memcpy (destination, source, osize))
510 /* No need to do anything to free, after alloca. */
511 # define REGEX_FREE(arg) ((void)0) /* Do nothing! But inhibit gcc warning. */
513 #endif /* not REGEX_MALLOC */
515 /* Define how to allocate the failure stack. */
517 #if defined REL_ALLOC && defined REGEX_MALLOC
519 # define REGEX_ALLOCATE_STACK(size) \
520 r_alloc (&failure_stack_ptr, (size))
521 # define REGEX_REALLOCATE_STACK(source, osize, nsize) \
522 r_re_alloc (&failure_stack_ptr, (nsize))
523 # define REGEX_FREE_STACK(ptr) \
524 r_alloc_free (&failure_stack_ptr)
526 #else /* not using relocating allocator */
530 # define REGEX_ALLOCATE_STACK malloc
531 # define REGEX_REALLOCATE_STACK(source, osize, nsize) realloc (source, nsize)
532 # define REGEX_FREE_STACK free
534 # else /* not REGEX_MALLOC */
536 # define REGEX_ALLOCATE_STACK alloca
538 # define REGEX_REALLOCATE_STACK(source, osize, nsize) \
539 REGEX_REALLOCATE (source, osize, nsize)
540 /* No need to explicitly free anything. */
541 # define REGEX_FREE_STACK(arg) ((void)0)
543 # endif /* not REGEX_MALLOC */
544 #endif /* not using relocating allocator */
547 /* True if `size1' is non-NULL and PTR is pointing anywhere inside
548 `string1' or just past its end. This works if PTR is NULL, which is
550 #define FIRST_STRING_P(ptr) \
551 (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
553 /* (Re)Allocate N items of type T using malloc, or fail. */
554 #define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t)))
555 #define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
556 #define RETALLOC_IF(addr, n, t) \
557 if (addr) RETALLOC((addr), (n), t); else (addr) = TALLOC ((n), t)
558 #define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
560 #define BYTEWIDTH 8 /* In bits. */
562 #define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
566 #define MAX(a, b) ((a) > (b) ? (a) : (b))
567 #define MIN(a, b) ((a) < (b) ? (a) : (b))
569 /* Type of source-pattern and string chars. */
570 typedef const unsigned char re_char
;
572 typedef char boolean
;
576 static int re_match_2_internal
_RE_ARGS ((struct re_pattern_buffer
*bufp
,
577 re_char
*string1
, int size1
,
578 re_char
*string2
, int size2
,
580 struct re_registers
*regs
,
583 /* These are the command codes that appear in compiled regular
584 expressions. Some opcodes are followed by argument bytes. A
585 command code can specify any interpretation whatsoever for its
586 arguments. Zero bytes may appear in the compiled regular expression. */
592 /* Succeed right away--no more backtracking. */
595 /* Followed by one byte giving n, then by n literal bytes. */
598 /* Matches any (more or less) character. */
601 /* Matches any one char belonging to specified set. First
602 following byte is number of bitmap bytes. Then come bytes
603 for a bitmap saying which chars are in. Bits in each byte
604 are ordered low-bit-first. A character is in the set if its
605 bit is 1. A character too large to have a bit in the map is
606 automatically not in the set.
608 If the length byte has the 0x80 bit set, then that stuff
609 is followed by a range table:
610 2 bytes of flags for character sets (low 8 bits, high 8 bits)
611 See RANGE_TABLE_WORK_BITS below.
612 2 bytes, the number of pairs that follow (upto 32767)
613 pairs, each 2 multibyte characters,
614 each multibyte character represented as 3 bytes. */
617 /* Same parameters as charset, but match any character that is
618 not one of those specified. */
621 /* Start remembering the text that is matched, for storing in a
622 register. Followed by one byte with the register number, in
623 the range 0 to one less than the pattern buffer's re_nsub
627 /* Stop remembering the text that is matched and store it in a
628 memory register. Followed by one byte with the register
629 number, in the range 0 to one less than `re_nsub' in the
633 /* Match a duplicate of something remembered. Followed by one
634 byte containing the register number. */
637 /* Fail unless at beginning of line. */
640 /* Fail unless at end of line. */
643 /* Succeeds if at beginning of buffer (if emacs) or at beginning
644 of string to be matched (if not). */
647 /* Analogously, for end of buffer/string. */
650 /* Followed by two byte relative address to which to jump. */
653 /* Followed by two-byte relative address of place to resume at
654 in case of failure. */
657 /* Like on_failure_jump, but pushes a placeholder instead of the
658 current string position when executed. */
659 on_failure_keep_string_jump
,
661 /* Just like `on_failure_jump', except that it checks that we
662 don't get stuck in an infinite loop (matching an empty string
664 on_failure_jump_loop
,
666 /* Just like `on_failure_jump_loop', except that it checks for
667 a different kind of loop (the kind that shows up with non-greedy
668 operators). This operation has to be immediately preceded
670 on_failure_jump_nastyloop
,
672 /* A smart `on_failure_jump' used for greedy * and + operators.
673 It analyses the loop before which it is put and if the
674 loop does not require backtracking, it changes itself to
675 `on_failure_keep_string_jump' and short-circuits the loop,
676 else it just defaults to changing itself into `on_failure_jump'.
677 It assumes that it is pointing to just past a `jump'. */
678 on_failure_jump_smart
,
680 /* Followed by two-byte relative address and two-byte number n.
681 After matching N times, jump to the address upon failure.
682 Does not work if N starts at 0: use on_failure_jump_loop
686 /* Followed by two-byte relative address, and two-byte number n.
687 Jump to the address N times, then fail. */
690 /* Set the following two-byte relative address to the
691 subsequent two-byte number. The address *includes* the two
695 wordbeg
, /* Succeeds if at word beginning. */
696 wordend
, /* Succeeds if at word end. */
698 wordbound
, /* Succeeds if at a word boundary. */
699 notwordbound
, /* Succeeds if not at a word boundary. */
701 symbeg
, /* Succeeds if at symbol beginning. */
702 symend
, /* Succeeds if at symbol end. */
704 /* Matches any character whose syntax is specified. Followed by
705 a byte which contains a syntax code, e.g., Sword. */
708 /* Matches any character whose syntax is not that specified. */
712 ,before_dot
, /* Succeeds if before point. */
713 at_dot
, /* Succeeds if at point. */
714 after_dot
, /* Succeeds if after point. */
716 /* Matches any character whose category-set contains the specified
717 category. The operator is followed by a byte which contains a
718 category code (mnemonic ASCII character). */
721 /* Matches any character whose category-set does not contain the
722 specified category. The operator is followed by a byte which
723 contains the category code (mnemonic ASCII character). */
728 /* Common operations on the compiled pattern. */
730 /* Store NUMBER in two contiguous bytes starting at DESTINATION. */
732 #define STORE_NUMBER(destination, number) \
734 (destination)[0] = (number) & 0377; \
735 (destination)[1] = (number) >> 8; \
738 /* Same as STORE_NUMBER, except increment DESTINATION to
739 the byte after where the number is stored. Therefore, DESTINATION
740 must be an lvalue. */
742 #define STORE_NUMBER_AND_INCR(destination, number) \
744 STORE_NUMBER (destination, number); \
745 (destination) += 2; \
748 /* Put into DESTINATION a number stored in two contiguous bytes starting
751 #define EXTRACT_NUMBER(destination, source) \
753 (destination) = *(source) & 0377; \
754 (destination) += SIGN_EXTEND_CHAR (*((source) + 1)) << 8; \
758 static void extract_number
_RE_ARGS ((int *dest
, re_char
*source
));
760 extract_number (dest
, source
)
764 int temp
= SIGN_EXTEND_CHAR (*(source
+ 1));
765 *dest
= *source
& 0377;
769 # ifndef EXTRACT_MACROS /* To debug the macros. */
770 # undef EXTRACT_NUMBER
771 # define EXTRACT_NUMBER(dest, src) extract_number (&dest, src)
772 # endif /* not EXTRACT_MACROS */
776 /* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
777 SOURCE must be an lvalue. */
779 #define EXTRACT_NUMBER_AND_INCR(destination, source) \
781 EXTRACT_NUMBER (destination, source); \
786 static void extract_number_and_incr
_RE_ARGS ((int *destination
,
789 extract_number_and_incr (destination
, source
)
793 extract_number (destination
, *source
);
797 # ifndef EXTRACT_MACROS
798 # undef EXTRACT_NUMBER_AND_INCR
799 # define EXTRACT_NUMBER_AND_INCR(dest, src) \
800 extract_number_and_incr (&dest, &src)
801 # endif /* not EXTRACT_MACROS */
805 /* Store a multibyte character in three contiguous bytes starting
806 DESTINATION, and increment DESTINATION to the byte after where the
807 character is stored. Therefore, DESTINATION must be an lvalue. */
809 #define STORE_CHARACTER_AND_INCR(destination, character) \
811 (destination)[0] = (character) & 0377; \
812 (destination)[1] = ((character) >> 8) & 0377; \
813 (destination)[2] = (character) >> 16; \
814 (destination) += 3; \
817 /* Put into DESTINATION a character stored in three contiguous bytes
818 starting at SOURCE. */
820 #define EXTRACT_CHARACTER(destination, source) \
822 (destination) = ((source)[0] \
823 | ((source)[1] << 8) \
824 | ((source)[2] << 16)); \
828 /* Macros for charset. */
830 /* Size of bitmap of charset P in bytes. P is a start of charset,
831 i.e. *P is (re_opcode_t) charset or (re_opcode_t) charset_not. */
832 #define CHARSET_BITMAP_SIZE(p) ((p)[1] & 0x7F)
834 /* Nonzero if charset P has range table. */
835 #define CHARSET_RANGE_TABLE_EXISTS_P(p) ((p)[1] & 0x80)
837 /* Return the address of range table of charset P. But not the start
838 of table itself, but the before where the number of ranges is
839 stored. `2 +' means to skip re_opcode_t and size of bitmap,
840 and the 2 bytes of flags at the start of the range table. */
841 #define CHARSET_RANGE_TABLE(p) (&(p)[4 + CHARSET_BITMAP_SIZE (p)])
843 /* Extract the bit flags that start a range table. */
844 #define CHARSET_RANGE_TABLE_BITS(p) \
845 ((p)[2 + CHARSET_BITMAP_SIZE (p)] \
846 + (p)[3 + CHARSET_BITMAP_SIZE (p)] * 0x100)
848 /* Test if C is listed in the bitmap of charset P. */
849 #define CHARSET_LOOKUP_BITMAP(p, c) \
850 ((c) < CHARSET_BITMAP_SIZE (p) * BYTEWIDTH \
851 && (p)[2 + (c) / BYTEWIDTH] & (1 << ((c) % BYTEWIDTH)))
853 /* Return the address of end of RANGE_TABLE. COUNT is number of
854 ranges (which is a pair of (start, end)) in the RANGE_TABLE. `* 2'
855 is start of range and end of range. `* 3' is size of each start
857 #define CHARSET_RANGE_TABLE_END(range_table, count) \
858 ((range_table) + (count) * 2 * 3)
860 /* Test if C is in RANGE_TABLE. A flag NOT is negated if C is in.
861 COUNT is number of ranges in RANGE_TABLE. */
862 #define CHARSET_LOOKUP_RANGE_TABLE_RAW(not, c, range_table, count) \
865 re_wchar_t range_start, range_end; \
867 re_char *range_table_end \
868 = CHARSET_RANGE_TABLE_END ((range_table), (count)); \
870 for (p = (range_table); p < range_table_end; p += 2 * 3) \
872 EXTRACT_CHARACTER (range_start, p); \
873 EXTRACT_CHARACTER (range_end, p + 3); \
875 if (range_start <= (c) && (c) <= range_end) \
884 /* Test if C is in range table of CHARSET. The flag NOT is negated if
885 C is listed in it. */
886 #define CHARSET_LOOKUP_RANGE_TABLE(not, c, charset) \
889 /* Number of ranges in range table. */ \
891 re_char *range_table = CHARSET_RANGE_TABLE (charset); \
893 EXTRACT_NUMBER_AND_INCR (count, range_table); \
894 CHARSET_LOOKUP_RANGE_TABLE_RAW ((not), (c), range_table, count); \
898 /* If DEBUG is defined, Regex prints many voluminous messages about what
899 it is doing (if the variable `debug' is nonzero). If linked with the
900 main program in `iregex.c', you can enter patterns and strings
901 interactively. And if linked with the main program in `main.c' and
902 the other test files, you can run the already-written tests. */
906 /* We use standard I/O for debugging. */
909 /* It is useful to test things that ``must'' be true when debugging. */
912 static int debug
= -100000;
914 # define DEBUG_STATEMENT(e) e
915 # define DEBUG_PRINT1(x) if (debug > 0) printf (x)
916 # define DEBUG_PRINT2(x1, x2) if (debug > 0) printf (x1, x2)
917 # define DEBUG_PRINT3(x1, x2, x3) if (debug > 0) printf (x1, x2, x3)
918 # define DEBUG_PRINT4(x1, x2, x3, x4) if (debug > 0) printf (x1, x2, x3, x4)
919 # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) \
920 if (debug > 0) print_partial_compiled_pattern (s, e)
921 # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \
922 if (debug > 0) print_double_string (w, s1, sz1, s2, sz2)
925 /* Print the fastmap in human-readable form. */
928 print_fastmap (fastmap
)
931 unsigned was_a_range
= 0;
934 while (i
< (1 << BYTEWIDTH
))
940 while (i
< (1 << BYTEWIDTH
) && fastmap
[i
])
956 /* Print a compiled pattern string in human-readable form, starting at
957 the START pointer into it and ending just before the pointer END. */
960 print_partial_compiled_pattern (start
, end
)
970 fprintf (stderr
, "(null)\n");
974 /* Loop over pattern commands. */
977 fprintf (stderr
, "%d:\t", p
- start
);
979 switch ((re_opcode_t
) *p
++)
982 fprintf (stderr
, "/no_op");
986 fprintf (stderr
, "/succeed");
991 fprintf (stderr
, "/exactn/%d", mcnt
);
994 fprintf (stderr
, "/%c", *p
++);
1000 fprintf (stderr
, "/start_memory/%d", *p
++);
1004 fprintf (stderr
, "/stop_memory/%d", *p
++);
1008 fprintf (stderr
, "/duplicate/%d", *p
++);
1012 fprintf (stderr
, "/anychar");
1018 register int c
, last
= -100;
1019 register int in_range
= 0;
1020 int length
= CHARSET_BITMAP_SIZE (p
- 1);
1021 int has_range_table
= CHARSET_RANGE_TABLE_EXISTS_P (p
- 1);
1023 fprintf (stderr
, "/charset [%s",
1024 (re_opcode_t
) *(p
- 1) == charset_not
? "^" : "");
1027 fprintf (stderr
, " !extends past end of pattern! ");
1029 for (c
= 0; c
< 256; c
++)
1031 && (p
[1 + (c
/8)] & (1 << (c
% 8))))
1033 /* Are we starting a range? */
1034 if (last
+ 1 == c
&& ! in_range
)
1036 fprintf (stderr
, "-");
1039 /* Have we broken a range? */
1040 else if (last
+ 1 != c
&& in_range
)
1042 fprintf (stderr
, "%c", last
);
1047 fprintf (stderr
, "%c", c
);
1053 fprintf (stderr
, "%c", last
);
1055 fprintf (stderr
, "]");
1059 if (has_range_table
)
1062 fprintf (stderr
, "has-range-table");
1064 /* ??? Should print the range table; for now, just skip it. */
1065 p
+= 2; /* skip range table bits */
1066 EXTRACT_NUMBER_AND_INCR (count
, p
);
1067 p
= CHARSET_RANGE_TABLE_END (p
, count
);
1073 fprintf (stderr
, "/begline");
1077 fprintf (stderr
, "/endline");
1080 case on_failure_jump
:
1081 extract_number_and_incr (&mcnt
, &p
);
1082 fprintf (stderr
, "/on_failure_jump to %d", p
+ mcnt
- start
);
1085 case on_failure_keep_string_jump
:
1086 extract_number_and_incr (&mcnt
, &p
);
1087 fprintf (stderr
, "/on_failure_keep_string_jump to %d", p
+ mcnt
- start
);
1090 case on_failure_jump_nastyloop
:
1091 extract_number_and_incr (&mcnt
, &p
);
1092 fprintf (stderr
, "/on_failure_jump_nastyloop to %d", p
+ mcnt
- start
);
1095 case on_failure_jump_loop
:
1096 extract_number_and_incr (&mcnt
, &p
);
1097 fprintf (stderr
, "/on_failure_jump_loop to %d", p
+ mcnt
- start
);
1100 case on_failure_jump_smart
:
1101 extract_number_and_incr (&mcnt
, &p
);
1102 fprintf (stderr
, "/on_failure_jump_smart to %d", p
+ mcnt
- start
);
1106 extract_number_and_incr (&mcnt
, &p
);
1107 fprintf (stderr
, "/jump to %d", p
+ mcnt
- start
);
1111 extract_number_and_incr (&mcnt
, &p
);
1112 extract_number_and_incr (&mcnt2
, &p
);
1113 fprintf (stderr
, "/succeed_n to %d, %d times", p
- 2 + mcnt
- start
, mcnt2
);
1117 extract_number_and_incr (&mcnt
, &p
);
1118 extract_number_and_incr (&mcnt2
, &p
);
1119 fprintf (stderr
, "/jump_n to %d, %d times", p
- 2 + mcnt
- start
, mcnt2
);
1123 extract_number_and_incr (&mcnt
, &p
);
1124 extract_number_and_incr (&mcnt2
, &p
);
1125 fprintf (stderr
, "/set_number_at location %d to %d", p
- 2 + mcnt
- start
, mcnt2
);
1129 fprintf (stderr
, "/wordbound");
1133 fprintf (stderr
, "/notwordbound");
1137 fprintf (stderr
, "/wordbeg");
1141 fprintf (stderr
, "/wordend");
1145 fprintf (stderr
, "/symbeg");
1149 fprintf (stderr
, "/symend");
1153 fprintf (stderr
, "/syntaxspec");
1155 fprintf (stderr
, "/%d", mcnt
);
1159 fprintf (stderr
, "/notsyntaxspec");
1161 fprintf (stderr
, "/%d", mcnt
);
1166 fprintf (stderr
, "/before_dot");
1170 fprintf (stderr
, "/at_dot");
1174 fprintf (stderr
, "/after_dot");
1178 fprintf (stderr
, "/categoryspec");
1180 fprintf (stderr
, "/%d", mcnt
);
1183 case notcategoryspec
:
1184 fprintf (stderr
, "/notcategoryspec");
1186 fprintf (stderr
, "/%d", mcnt
);
1191 fprintf (stderr
, "/begbuf");
1195 fprintf (stderr
, "/endbuf");
1199 fprintf (stderr
, "?%d", *(p
-1));
1202 fprintf (stderr
, "\n");
1205 fprintf (stderr
, "%d:\tend of pattern.\n", p
- start
);
1210 print_compiled_pattern (bufp
)
1211 struct re_pattern_buffer
*bufp
;
1213 re_char
*buffer
= bufp
->buffer
;
1215 print_partial_compiled_pattern (buffer
, buffer
+ bufp
->used
);
1216 printf ("%ld bytes used/%ld bytes allocated.\n",
1217 bufp
->used
, bufp
->allocated
);
1219 if (bufp
->fastmap_accurate
&& bufp
->fastmap
)
1221 printf ("fastmap: ");
1222 print_fastmap (bufp
->fastmap
);
1225 printf ("re_nsub: %d\t", bufp
->re_nsub
);
1226 printf ("regs_alloc: %d\t", bufp
->regs_allocated
);
1227 printf ("can_be_null: %d\t", bufp
->can_be_null
);
1228 printf ("no_sub: %d\t", bufp
->no_sub
);
1229 printf ("not_bol: %d\t", bufp
->not_bol
);
1230 printf ("not_eol: %d\t", bufp
->not_eol
);
1231 printf ("syntax: %lx\n", bufp
->syntax
);
1233 /* Perhaps we should print the translate table? */
1238 print_double_string (where
, string1
, size1
, string2
, size2
)
1251 if (FIRST_STRING_P (where
))
1253 for (this_char
= where
- string1
; this_char
< size1
; this_char
++)
1254 putchar (string1
[this_char
]);
1259 for (this_char
= where
- string2
; this_char
< size2
; this_char
++)
1260 putchar (string2
[this_char
]);
1264 #else /* not DEBUG */
1269 # define DEBUG_STATEMENT(e)
1270 # define DEBUG_PRINT1(x)
1271 # define DEBUG_PRINT2(x1, x2)
1272 # define DEBUG_PRINT3(x1, x2, x3)
1273 # define DEBUG_PRINT4(x1, x2, x3, x4)
1274 # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
1275 # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
1277 #endif /* not DEBUG */
1279 /* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
1280 also be assigned to arbitrarily: each pattern buffer stores its own
1281 syntax, so it can be changed between regex compilations. */
1282 /* This has no initializer because initialized variables in Emacs
1283 become read-only after dumping. */
1284 reg_syntax_t re_syntax_options
;
1287 /* Specify the precise syntax of regexps for compilation. This provides
1288 for compatibility for various utilities which historically have
1289 different, incompatible syntaxes.
1291 The argument SYNTAX is a bit mask comprised of the various bits
1292 defined in regex.h. We return the old syntax. */
1295 re_set_syntax (reg_syntax_t syntax
)
1297 reg_syntax_t ret
= re_syntax_options
;
1299 re_syntax_options
= syntax
;
1302 WEAK_ALIAS (__re_set_syntax
, re_set_syntax
)
1304 /* Regexp to use to replace spaces, or NULL meaning don't. */
1305 static re_char
*whitespace_regexp
;
1308 re_set_whitespace_regexp (const char *regexp
)
1310 whitespace_regexp
= (re_char
*) regexp
;
1312 WEAK_ALIAS (__re_set_syntax
, re_set_syntax
)
1314 /* This table gives an error message for each of the error codes listed
1315 in regex.h. Obviously the order here has to be same as there.
1316 POSIX doesn't require that we do anything for REG_NOERROR,
1317 but why not be nice? */
1319 static const char *re_error_msgid
[] =
1321 gettext_noop ("Success"), /* REG_NOERROR */
1322 gettext_noop ("No match"), /* REG_NOMATCH */
1323 gettext_noop ("Invalid regular expression"), /* REG_BADPAT */
1324 gettext_noop ("Invalid collation character"), /* REG_ECOLLATE */
1325 gettext_noop ("Invalid character class name"), /* REG_ECTYPE */
1326 gettext_noop ("Trailing backslash"), /* REG_EESCAPE */
1327 gettext_noop ("Invalid back reference"), /* REG_ESUBREG */
1328 gettext_noop ("Unmatched [ or [^"), /* REG_EBRACK */
1329 gettext_noop ("Unmatched ( or \\("), /* REG_EPAREN */
1330 gettext_noop ("Unmatched \\{"), /* REG_EBRACE */
1331 gettext_noop ("Invalid content of \\{\\}"), /* REG_BADBR */
1332 gettext_noop ("Invalid range end"), /* REG_ERANGE */
1333 gettext_noop ("Memory exhausted"), /* REG_ESPACE */
1334 gettext_noop ("Invalid preceding regular expression"), /* REG_BADRPT */
1335 gettext_noop ("Premature end of regular expression"), /* REG_EEND */
1336 gettext_noop ("Regular expression too big"), /* REG_ESIZE */
1337 gettext_noop ("Unmatched ) or \\)"), /* REG_ERPAREN */
1338 gettext_noop ("Range striding over charsets") /* REG_ERANGEX */
1341 /* Avoiding alloca during matching, to placate r_alloc. */
1343 /* Define MATCH_MAY_ALLOCATE unless we need to make sure that the
1344 searching and matching functions should not call alloca. On some
1345 systems, alloca is implemented in terms of malloc, and if we're
1346 using the relocating allocator routines, then malloc could cause a
1347 relocation, which might (if the strings being searched are in the
1348 ralloc heap) shift the data out from underneath the regexp
1351 Here's another reason to avoid allocation: Emacs
1352 processes input from X in a signal handler; processing X input may
1353 call malloc; if input arrives while a matching routine is calling
1354 malloc, then we're scrod. But Emacs can't just block input while
1355 calling matching routines; then we don't notice interrupts when
1356 they come in. So, Emacs blocks input around all regexp calls
1357 except the matching calls, which it leaves unprotected, in the
1358 faith that they will not malloc. */
1360 /* Normally, this is fine. */
1361 #define MATCH_MAY_ALLOCATE
1363 /* The match routines may not allocate if (1) they would do it with malloc
1364 and (2) it's not safe for them to use malloc.
1365 Note that if REL_ALLOC is defined, matching would not use malloc for the
1366 failure stack, but we would still use it for the register vectors;
1367 so REL_ALLOC should not affect this. */
1368 #if defined REGEX_MALLOC && defined emacs
1369 # undef MATCH_MAY_ALLOCATE
1373 /* Failure stack declarations and macros; both re_compile_fastmap and
1374 re_match_2 use a failure stack. These have to be macros because of
1375 REGEX_ALLOCATE_STACK. */
1378 /* Approximate number of failure points for which to initially allocate space
1379 when matching. If this number is exceeded, we allocate more
1380 space, so it is not a hard limit. */
1381 #ifndef INIT_FAILURE_ALLOC
1382 # define INIT_FAILURE_ALLOC 20
1385 /* Roughly the maximum number of failure points on the stack. Would be
1386 exactly that if always used TYPICAL_FAILURE_SIZE items each time we failed.
1387 This is a variable only so users of regex can assign to it; we never
1388 change it ourselves. We always multiply it by TYPICAL_FAILURE_SIZE
1389 before using it, so it should probably be a byte-count instead. */
1390 # if defined MATCH_MAY_ALLOCATE
1391 /* Note that 4400 was enough to cause a crash on Alpha OSF/1,
1392 whose default stack limit is 2mb. In order for a larger
1393 value to work reliably, you have to try to make it accord
1394 with the process stack limit. */
1395 size_t re_max_failures
= 40000;
1397 size_t re_max_failures
= 4000;
1400 union fail_stack_elt
1403 /* This should be the biggest `int' that's no bigger than a pointer. */
1407 typedef union fail_stack_elt fail_stack_elt_t
;
1411 fail_stack_elt_t
*stack
;
1413 size_t avail
; /* Offset of next open position. */
1414 size_t frame
; /* Offset of the cur constructed frame. */
1417 #define FAIL_STACK_EMPTY() (fail_stack.frame == 0)
1418 #define FAIL_STACK_FULL() (fail_stack.avail == fail_stack.size)
1421 /* Define macros to initialize and free the failure stack.
1422 Do `return -2' if the alloc fails. */
1424 #ifdef MATCH_MAY_ALLOCATE
1425 # define INIT_FAIL_STACK() \
1427 fail_stack.stack = (fail_stack_elt_t *) \
1428 REGEX_ALLOCATE_STACK (INIT_FAILURE_ALLOC * TYPICAL_FAILURE_SIZE \
1429 * sizeof (fail_stack_elt_t)); \
1431 if (fail_stack.stack == NULL) \
1434 fail_stack.size = INIT_FAILURE_ALLOC; \
1435 fail_stack.avail = 0; \
1436 fail_stack.frame = 0; \
1439 # define RESET_FAIL_STACK() REGEX_FREE_STACK (fail_stack.stack)
1441 # define INIT_FAIL_STACK() \
1443 fail_stack.avail = 0; \
1444 fail_stack.frame = 0; \
1447 # define RESET_FAIL_STACK() ((void)0)
1451 /* Double the size of FAIL_STACK, up to a limit
1452 which allows approximately `re_max_failures' items.
1454 Return 1 if succeeds, and 0 if either ran out of memory
1455 allocating space for it or it was already too large.
1457 REGEX_REALLOCATE_STACK requires `destination' be declared. */
1459 /* Factor to increase the failure stack size by
1460 when we increase it.
1461 This used to be 2, but 2 was too wasteful
1462 because the old discarded stacks added up to as much space
1463 were as ultimate, maximum-size stack. */
1464 #define FAIL_STACK_GROWTH_FACTOR 4
1466 #define GROW_FAIL_STACK(fail_stack) \
1467 (((fail_stack).size * sizeof (fail_stack_elt_t) \
1468 >= re_max_failures * TYPICAL_FAILURE_SIZE) \
1470 : ((fail_stack).stack \
1471 = (fail_stack_elt_t *) \
1472 REGEX_REALLOCATE_STACK ((fail_stack).stack, \
1473 (fail_stack).size * sizeof (fail_stack_elt_t), \
1474 MIN (re_max_failures * TYPICAL_FAILURE_SIZE, \
1475 ((fail_stack).size * sizeof (fail_stack_elt_t) \
1476 * FAIL_STACK_GROWTH_FACTOR))), \
1478 (fail_stack).stack == NULL \
1480 : ((fail_stack).size \
1481 = (MIN (re_max_failures * TYPICAL_FAILURE_SIZE, \
1482 ((fail_stack).size * sizeof (fail_stack_elt_t) \
1483 * FAIL_STACK_GROWTH_FACTOR)) \
1484 / sizeof (fail_stack_elt_t)), \
1488 /* Push a pointer value onto the failure stack.
1489 Assumes the variable `fail_stack'. Probably should only
1490 be called from within `PUSH_FAILURE_POINT'. */
1491 #define PUSH_FAILURE_POINTER(item) \
1492 fail_stack.stack[fail_stack.avail++].pointer = (item)
1494 /* This pushes an integer-valued item onto the failure stack.
1495 Assumes the variable `fail_stack'. Probably should only
1496 be called from within `PUSH_FAILURE_POINT'. */
1497 #define PUSH_FAILURE_INT(item) \
1498 fail_stack.stack[fail_stack.avail++].integer = (item)
1500 /* Push a fail_stack_elt_t value onto the failure stack.
1501 Assumes the variable `fail_stack'. Probably should only
1502 be called from within `PUSH_FAILURE_POINT'. */
1503 #define PUSH_FAILURE_ELT(item) \
1504 fail_stack.stack[fail_stack.avail++] = (item)
1506 /* These three POP... operations complement the three PUSH... operations.
1507 All assume that `fail_stack' is nonempty. */
1508 #define POP_FAILURE_POINTER() fail_stack.stack[--fail_stack.avail].pointer
1509 #define POP_FAILURE_INT() fail_stack.stack[--fail_stack.avail].integer
1510 #define POP_FAILURE_ELT() fail_stack.stack[--fail_stack.avail]
1512 /* Individual items aside from the registers. */
1513 #define NUM_NONREG_ITEMS 3
1515 /* Used to examine the stack (to detect infinite loops). */
1516 #define FAILURE_PAT(h) fail_stack.stack[(h) - 1].pointer
1517 #define FAILURE_STR(h) (fail_stack.stack[(h) - 2].pointer)
1518 #define NEXT_FAILURE_HANDLE(h) fail_stack.stack[(h) - 3].integer
1519 #define TOP_FAILURE_HANDLE() fail_stack.frame
1522 #define ENSURE_FAIL_STACK(space) \
1523 while (REMAINING_AVAIL_SLOTS <= space) { \
1524 if (!GROW_FAIL_STACK (fail_stack)) \
1526 DEBUG_PRINT2 ("\n Doubled stack; size now: %d\n", (fail_stack).size);\
1527 DEBUG_PRINT2 (" slots available: %d\n", REMAINING_AVAIL_SLOTS);\
1530 /* Push register NUM onto the stack. */
1531 #define PUSH_FAILURE_REG(num) \
1533 char *destination; \
1534 ENSURE_FAIL_STACK(3); \
1535 DEBUG_PRINT4 (" Push reg %d (spanning %p -> %p)\n", \
1536 num, regstart[num], regend[num]); \
1537 PUSH_FAILURE_POINTER (regstart[num]); \
1538 PUSH_FAILURE_POINTER (regend[num]); \
1539 PUSH_FAILURE_INT (num); \
1542 /* Change the counter's value to VAL, but make sure that it will
1543 be reset when backtracking. */
1544 #define PUSH_NUMBER(ptr,val) \
1546 char *destination; \
1548 ENSURE_FAIL_STACK(3); \
1549 EXTRACT_NUMBER (c, ptr); \
1550 DEBUG_PRINT4 (" Push number %p = %d -> %d\n", ptr, c, val); \
1551 PUSH_FAILURE_INT (c); \
1552 PUSH_FAILURE_POINTER (ptr); \
1553 PUSH_FAILURE_INT (-1); \
1554 STORE_NUMBER (ptr, val); \
1557 /* Pop a saved register off the stack. */
1558 #define POP_FAILURE_REG_OR_COUNT() \
1560 int reg = POP_FAILURE_INT (); \
1563 /* It's a counter. */ \
1564 /* Here, we discard `const', making re_match non-reentrant. */ \
1565 unsigned char *ptr = (unsigned char*) POP_FAILURE_POINTER (); \
1566 reg = POP_FAILURE_INT (); \
1567 STORE_NUMBER (ptr, reg); \
1568 DEBUG_PRINT3 (" Pop counter %p = %d\n", ptr, reg); \
1572 regend[reg] = POP_FAILURE_POINTER (); \
1573 regstart[reg] = POP_FAILURE_POINTER (); \
1574 DEBUG_PRINT4 (" Pop reg %d (spanning %p -> %p)\n", \
1575 reg, regstart[reg], regend[reg]); \
1579 /* Check that we are not stuck in an infinite loop. */
1580 #define CHECK_INFINITE_LOOP(pat_cur, string_place) \
1582 int failure = TOP_FAILURE_HANDLE (); \
1583 /* Check for infinite matching loops */ \
1584 while (failure > 0 \
1585 && (FAILURE_STR (failure) == string_place \
1586 || FAILURE_STR (failure) == NULL)) \
1588 assert (FAILURE_PAT (failure) >= bufp->buffer \
1589 && FAILURE_PAT (failure) <= bufp->buffer + bufp->used); \
1590 if (FAILURE_PAT (failure) == pat_cur) \
1595 DEBUG_PRINT2 (" Other pattern: %p\n", FAILURE_PAT (failure)); \
1596 failure = NEXT_FAILURE_HANDLE(failure); \
1598 DEBUG_PRINT2 (" Other string: %p\n", FAILURE_STR (failure)); \
1601 /* Push the information about the state we will need
1602 if we ever fail back to it.
1604 Requires variables fail_stack, regstart, regend and
1605 num_regs be declared. GROW_FAIL_STACK requires `destination' be
1608 Does `return FAILURE_CODE' if runs out of memory. */
1610 #define PUSH_FAILURE_POINT(pattern, string_place) \
1612 char *destination; \
1613 /* Must be int, so when we don't save any registers, the arithmetic \
1614 of 0 + -1 isn't done as unsigned. */ \
1616 DEBUG_STATEMENT (nfailure_points_pushed++); \
1617 DEBUG_PRINT1 ("\nPUSH_FAILURE_POINT:\n"); \
1618 DEBUG_PRINT2 (" Before push, next avail: %d\n", (fail_stack).avail); \
1619 DEBUG_PRINT2 (" size: %d\n", (fail_stack).size);\
1621 ENSURE_FAIL_STACK (NUM_NONREG_ITEMS); \
1623 DEBUG_PRINT1 ("\n"); \
1625 DEBUG_PRINT2 (" Push frame index: %d\n", fail_stack.frame); \
1626 PUSH_FAILURE_INT (fail_stack.frame); \
1628 DEBUG_PRINT2 (" Push string %p: `", string_place); \
1629 DEBUG_PRINT_DOUBLE_STRING (string_place, string1, size1, string2, size2);\
1630 DEBUG_PRINT1 ("'\n"); \
1631 PUSH_FAILURE_POINTER (string_place); \
1633 DEBUG_PRINT2 (" Push pattern %p: ", pattern); \
1634 DEBUG_PRINT_COMPILED_PATTERN (bufp, pattern, pend); \
1635 PUSH_FAILURE_POINTER (pattern); \
1637 /* Close the frame by moving the frame pointer past it. */ \
1638 fail_stack.frame = fail_stack.avail; \
1641 /* Estimate the size of data pushed by a typical failure stack entry.
1642 An estimate is all we need, because all we use this for
1643 is to choose a limit for how big to make the failure stack. */
1644 /* BEWARE, the value `20' is hard-coded in emacs.c:main(). */
1645 #define TYPICAL_FAILURE_SIZE 20
1647 /* How many items can still be added to the stack without overflowing it. */
1648 #define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail)
1651 /* Pops what PUSH_FAIL_STACK pushes.
1653 We restore into the parameters, all of which should be lvalues:
1654 STR -- the saved data position.
1655 PAT -- the saved pattern position.
1656 REGSTART, REGEND -- arrays of string positions.
1658 Also assumes the variables `fail_stack' and (if debugging), `bufp',
1659 `pend', `string1', `size1', `string2', and `size2'. */
1661 #define POP_FAILURE_POINT(str, pat) \
1663 assert (!FAIL_STACK_EMPTY ()); \
1665 /* Remove failure points and point to how many regs pushed. */ \
1666 DEBUG_PRINT1 ("POP_FAILURE_POINT:\n"); \
1667 DEBUG_PRINT2 (" Before pop, next avail: %d\n", fail_stack.avail); \
1668 DEBUG_PRINT2 (" size: %d\n", fail_stack.size); \
1670 /* Pop the saved registers. */ \
1671 while (fail_stack.frame < fail_stack.avail) \
1672 POP_FAILURE_REG_OR_COUNT (); \
1674 pat = POP_FAILURE_POINTER (); \
1675 DEBUG_PRINT2 (" Popping pattern %p: ", pat); \
1676 DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend); \
1678 /* If the saved string location is NULL, it came from an \
1679 on_failure_keep_string_jump opcode, and we want to throw away the \
1680 saved NULL, thus retaining our current position in the string. */ \
1681 str = POP_FAILURE_POINTER (); \
1682 DEBUG_PRINT2 (" Popping string %p: `", str); \
1683 DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2); \
1684 DEBUG_PRINT1 ("'\n"); \
1686 fail_stack.frame = POP_FAILURE_INT (); \
1687 DEBUG_PRINT2 (" Popping frame index: %d\n", fail_stack.frame); \
1689 assert (fail_stack.avail >= 0); \
1690 assert (fail_stack.frame <= fail_stack.avail); \
1692 DEBUG_STATEMENT (nfailure_points_popped++); \
1693 } while (0) /* POP_FAILURE_POINT */
1697 /* Registers are set to a sentinel when they haven't yet matched. */
1698 #define REG_UNSET(e) ((e) == NULL)
1700 /* Subroutine declarations and macros for regex_compile. */
1702 static reg_errcode_t regex_compile
_RE_ARGS ((re_char
*pattern
, size_t size
,
1703 reg_syntax_t syntax
,
1704 struct re_pattern_buffer
*bufp
));
1705 static void store_op1
_RE_ARGS ((re_opcode_t op
, unsigned char *loc
, int arg
));
1706 static void store_op2
_RE_ARGS ((re_opcode_t op
, unsigned char *loc
,
1707 int arg1
, int arg2
));
1708 static void insert_op1
_RE_ARGS ((re_opcode_t op
, unsigned char *loc
,
1709 int arg
, unsigned char *end
));
1710 static void insert_op2
_RE_ARGS ((re_opcode_t op
, unsigned char *loc
,
1711 int arg1
, int arg2
, unsigned char *end
));
1712 static boolean at_begline_loc_p
_RE_ARGS ((re_char
*pattern
,
1714 reg_syntax_t syntax
));
1715 static boolean at_endline_loc_p
_RE_ARGS ((re_char
*p
,
1717 reg_syntax_t syntax
));
1718 static re_char
*skip_one_char
_RE_ARGS ((re_char
*p
));
1719 static int analyse_first
_RE_ARGS ((re_char
*p
, re_char
*pend
,
1720 char *fastmap
, const int multibyte
));
1722 /* Fetch the next character in the uncompiled pattern, with no
1724 #define PATFETCH(c) \
1727 if (p == pend) return REG_EEND; \
1728 c = RE_STRING_CHAR_AND_LENGTH (p, len, multibyte); \
1733 /* If `translate' is non-null, return translate[D], else just D. We
1734 cast the subscript to translate because some data is declared as
1735 `char *', to avoid warnings when a string constant is passed. But
1736 when we use a character as a subscript we must make it unsigned. */
1738 # define TRANSLATE(d) \
1739 (RE_TRANSLATE_P (translate) ? RE_TRANSLATE (translate, (d)) : (d))
1743 /* Macros for outputting the compiled pattern into `buffer'. */
1745 /* If the buffer isn't allocated when it comes in, use this. */
1746 #define INIT_BUF_SIZE 32
1748 /* Make sure we have at least N more bytes of space in buffer. */
1749 #define GET_BUFFER_SPACE(n) \
1750 while ((size_t) (b - bufp->buffer + (n)) > bufp->allocated) \
1753 /* Make sure we have one more byte of buffer space and then add C to it. */
1754 #define BUF_PUSH(c) \
1756 GET_BUFFER_SPACE (1); \
1757 *b++ = (unsigned char) (c); \
1761 /* Ensure we have two more bytes of buffer space and then append C1 and C2. */
1762 #define BUF_PUSH_2(c1, c2) \
1764 GET_BUFFER_SPACE (2); \
1765 *b++ = (unsigned char) (c1); \
1766 *b++ = (unsigned char) (c2); \
1770 /* As with BUF_PUSH_2, except for three bytes. */
1771 #define BUF_PUSH_3(c1, c2, c3) \
1773 GET_BUFFER_SPACE (3); \
1774 *b++ = (unsigned char) (c1); \
1775 *b++ = (unsigned char) (c2); \
1776 *b++ = (unsigned char) (c3); \
1780 /* Store a jump with opcode OP at LOC to location TO. We store a
1781 relative address offset by the three bytes the jump itself occupies. */
1782 #define STORE_JUMP(op, loc, to) \
1783 store_op1 (op, loc, (to) - (loc) - 3)
1785 /* Likewise, for a two-argument jump. */
1786 #define STORE_JUMP2(op, loc, to, arg) \
1787 store_op2 (op, loc, (to) - (loc) - 3, arg)
1789 /* Like `STORE_JUMP', but for inserting. Assume `b' is the buffer end. */
1790 #define INSERT_JUMP(op, loc, to) \
1791 insert_op1 (op, loc, (to) - (loc) - 3, b)
1793 /* Like `STORE_JUMP2', but for inserting. Assume `b' is the buffer end. */
1794 #define INSERT_JUMP2(op, loc, to, arg) \
1795 insert_op2 (op, loc, (to) - (loc) - 3, arg, b)
1798 /* This is not an arbitrary limit: the arguments which represent offsets
1799 into the pattern are two bytes long. So if 2^15 bytes turns out to
1800 be too small, many things would have to change. */
1801 # define MAX_BUF_SIZE (1L << 15)
1803 #if 0 /* This is when we thought it could be 2^16 bytes. */
1804 /* Any other compiler which, like MSC, has allocation limit below 2^16
1805 bytes will have to use approach similar to what was done below for
1806 MSC and drop MAX_BUF_SIZE a bit. Otherwise you may end up
1807 reallocating to 0 bytes. Such thing is not going to work too well.
1808 You have been warned!! */
1809 #if defined _MSC_VER && !defined WIN32
1810 /* Microsoft C 16-bit versions limit malloc to approx 65512 bytes. */
1811 # define MAX_BUF_SIZE 65500L
1813 # define MAX_BUF_SIZE (1L << 16)
1817 /* Extend the buffer by twice its current size via realloc and
1818 reset the pointers that pointed into the old block to point to the
1819 correct places in the new one. If extending the buffer results in it
1820 being larger than MAX_BUF_SIZE, then flag memory exhausted. */
1821 #if __BOUNDED_POINTERS__
1822 # define SET_HIGH_BOUND(P) (__ptrhigh (P) = __ptrlow (P) + bufp->allocated)
1823 # define MOVE_BUFFER_POINTER(P) \
1824 (__ptrlow (P) = new_buffer + (__ptrlow (P) - old_buffer), \
1825 SET_HIGH_BOUND (P), \
1826 __ptrvalue (P) = new_buffer + (__ptrvalue (P) - old_buffer))
1827 # define ELSE_EXTEND_BUFFER_HIGH_BOUND \
1830 SET_HIGH_BOUND (b); \
1831 SET_HIGH_BOUND (begalt); \
1832 if (fixup_alt_jump) \
1833 SET_HIGH_BOUND (fixup_alt_jump); \
1835 SET_HIGH_BOUND (laststart); \
1836 if (pending_exact) \
1837 SET_HIGH_BOUND (pending_exact); \
1840 # define MOVE_BUFFER_POINTER(P) ((P) = new_buffer + ((P) - old_buffer))
1841 # define ELSE_EXTEND_BUFFER_HIGH_BOUND
1843 #define EXTEND_BUFFER() \
1845 unsigned char *old_buffer = bufp->buffer; \
1846 if (bufp->allocated == MAX_BUF_SIZE) \
1848 bufp->allocated <<= 1; \
1849 if (bufp->allocated > MAX_BUF_SIZE) \
1850 bufp->allocated = MAX_BUF_SIZE; \
1851 RETALLOC (bufp->buffer, bufp->allocated, unsigned char); \
1852 if (bufp->buffer == NULL) \
1853 return REG_ESPACE; \
1854 /* If the buffer moved, move all the pointers into it. */ \
1855 if (old_buffer != bufp->buffer) \
1857 unsigned char *new_buffer = bufp->buffer; \
1858 MOVE_BUFFER_POINTER (b); \
1859 MOVE_BUFFER_POINTER (begalt); \
1860 if (fixup_alt_jump) \
1861 MOVE_BUFFER_POINTER (fixup_alt_jump); \
1863 MOVE_BUFFER_POINTER (laststart); \
1864 if (pending_exact) \
1865 MOVE_BUFFER_POINTER (pending_exact); \
1867 ELSE_EXTEND_BUFFER_HIGH_BOUND \
1871 /* Since we have one byte reserved for the register number argument to
1872 {start,stop}_memory, the maximum number of groups we can report
1873 things about is what fits in that byte. */
1874 #define MAX_REGNUM 255
1876 /* But patterns can have more than `MAX_REGNUM' registers. We just
1877 ignore the excess. */
1878 typedef int regnum_t
;
1881 /* Macros for the compile stack. */
1883 /* Since offsets can go either forwards or backwards, this type needs to
1884 be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1. */
1885 /* int may be not enough when sizeof(int) == 2. */
1886 typedef long pattern_offset_t
;
1890 pattern_offset_t begalt_offset
;
1891 pattern_offset_t fixup_alt_jump
;
1892 pattern_offset_t laststart_offset
;
1894 } compile_stack_elt_t
;
1899 compile_stack_elt_t
*stack
;
1901 unsigned avail
; /* Offset of next open position. */
1902 } compile_stack_type
;
1905 #define INIT_COMPILE_STACK_SIZE 32
1907 #define COMPILE_STACK_EMPTY (compile_stack.avail == 0)
1908 #define COMPILE_STACK_FULL (compile_stack.avail == compile_stack.size)
1910 /* The next available element. */
1911 #define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
1913 /* Explicit quit checking is only used on NTemacs and whenever we
1914 use polling to process input events. */
1915 #if defined emacs && (defined WINDOWSNT || defined SYNC_INPUT) && defined QUIT
1916 extern int immediate_quit
;
1917 # define IMMEDIATE_QUIT_CHECK \
1919 if (immediate_quit) QUIT; \
1922 # define IMMEDIATE_QUIT_CHECK ((void)0)
1925 /* Structure to manage work area for range table. */
1926 struct range_table_work_area
1928 int *table
; /* actual work area. */
1929 int allocated
; /* allocated size for work area in bytes. */
1930 int used
; /* actually used size in words. */
1931 int bits
; /* flag to record character classes */
1934 /* Make sure that WORK_AREA can hold more N multibyte characters.
1935 This is used only in set_image_of_range and set_image_of_range_1.
1936 It expects WORK_AREA to be a pointer.
1937 If it can't get the space, it returns from the surrounding function. */
1939 #define EXTEND_RANGE_TABLE(work_area, n) \
1941 if (((work_area).used + (n)) * sizeof (int) > (work_area).allocated) \
1943 extend_range_table_work_area (&work_area); \
1944 if ((work_area).table == 0) \
1945 return (REG_ESPACE); \
1949 #define SET_RANGE_TABLE_WORK_AREA_BIT(work_area, bit) \
1950 (work_area).bits |= (bit)
1952 /* Bits used to implement the multibyte-part of the various character classes
1953 such as [:alnum:] in a charset's range table. */
1954 #define BIT_WORD 0x1
1955 #define BIT_LOWER 0x2
1956 #define BIT_PUNCT 0x4
1957 #define BIT_SPACE 0x8
1958 #define BIT_UPPER 0x10
1959 #define BIT_MULTIBYTE 0x20
1961 /* Set a range (RANGE_START, RANGE_END) to WORK_AREA. */
1962 #define SET_RANGE_TABLE_WORK_AREA(work_area, range_start, range_end) \
1964 EXTEND_RANGE_TABLE ((work_area), 2); \
1965 (work_area).table[(work_area).used++] = (range_start); \
1966 (work_area).table[(work_area).used++] = (range_end); \
1969 /* Free allocated memory for WORK_AREA. */
1970 #define FREE_RANGE_TABLE_WORK_AREA(work_area) \
1972 if ((work_area).table) \
1973 free ((work_area).table); \
1976 #define CLEAR_RANGE_TABLE_WORK_USED(work_area) ((work_area).used = 0, (work_area).bits = 0)
1977 #define RANGE_TABLE_WORK_USED(work_area) ((work_area).used)
1978 #define RANGE_TABLE_WORK_BITS(work_area) ((work_area).bits)
1979 #define RANGE_TABLE_WORK_ELT(work_area, i) ((work_area).table[i])
1982 /* Set the bit for character C in a list. */
1983 #define SET_LIST_BIT(c) (b[((c)) / BYTEWIDTH] |= 1 << ((c) % BYTEWIDTH))
1988 /* Store characters in the range FROM to TO in the bitmap at B (for
1989 ASCII and unibyte characters) and WORK_AREA (for multibyte
1990 characters) while translating them and paying attention to the
1991 continuity of translated characters.
1993 Implementation note: It is better to implement these fairly big
1994 macros by a function, but it's not that easy because macros called
1995 in this macro assume various local variables already declared. */
1997 /* Both FROM and TO are ASCII characters. */
1999 #define SETUP_ASCII_RANGE(work_area, FROM, TO) \
2003 for (C0 = (FROM); C0 <= (TO); C0++) \
2005 C1 = TRANSLATE (C0); \
2006 if (! ASCII_CHAR_P (C1)) \
2008 SET_RANGE_TABLE_WORK_AREA ((work_area), C1, C1); \
2009 if ((C1 = RE_CHAR_TO_UNIBYTE (C1)) < 0) \
2012 SET_LIST_BIT (C1); \
2017 /* Both FROM and TO are unibyte characters (0x80..0xFF). */
2019 #define SETUP_UNIBYTE_RANGE(work_area, FROM, TO) \
2021 int C0, C1, C2, I; \
2022 int USED = RANGE_TABLE_WORK_USED (work_area); \
2024 for (C0 = (FROM); C0 <= (TO); C0++) \
2026 C1 = RE_CHAR_TO_MULTIBYTE (C0); \
2027 if (CHAR_BYTE8_P (C1)) \
2028 SET_LIST_BIT (C0); \
2031 C2 = TRANSLATE (C1); \
2033 || (C1 = RE_CHAR_TO_UNIBYTE (C2)) < 0) \
2035 SET_LIST_BIT (C1); \
2036 for (I = RANGE_TABLE_WORK_USED (work_area) - 2; I >= USED; I -= 2) \
2038 int from = RANGE_TABLE_WORK_ELT (work_area, I); \
2039 int to = RANGE_TABLE_WORK_ELT (work_area, I + 1); \
2041 if (C2 >= from - 1 && C2 <= to + 1) \
2043 if (C2 == from - 1) \
2044 RANGE_TABLE_WORK_ELT (work_area, I)--; \
2045 else if (C2 == to + 1) \
2046 RANGE_TABLE_WORK_ELT (work_area, I + 1)++; \
2051 SET_RANGE_TABLE_WORK_AREA ((work_area), C2, C2); \
2057 /* Both FROM and TO are multibyte characters. */
2059 #define SETUP_MULTIBYTE_RANGE(work_area, FROM, TO) \
2061 int C0, C1, C2, I, USED = RANGE_TABLE_WORK_USED (work_area); \
2063 SET_RANGE_TABLE_WORK_AREA ((work_area), (FROM), (TO)); \
2064 for (C0 = (FROM); C0 <= (TO); C0++) \
2066 C1 = TRANSLATE (C0); \
2067 if ((C2 = RE_CHAR_TO_UNIBYTE (C1)) >= 0 \
2068 || (C1 != C0 && (C2 = RE_CHAR_TO_UNIBYTE (C0)) >= 0)) \
2069 SET_LIST_BIT (C2); \
2070 if (C1 >= (FROM) && C1 <= (TO)) \
2072 for (I = RANGE_TABLE_WORK_USED (work_area) - 2; I >= USED; I -= 2) \
2074 int from = RANGE_TABLE_WORK_ELT (work_area, I); \
2075 int to = RANGE_TABLE_WORK_ELT (work_area, I + 1); \
2077 if (C1 >= from - 1 && C1 <= to + 1) \
2079 if (C1 == from - 1) \
2080 RANGE_TABLE_WORK_ELT (work_area, I)--; \
2081 else if (C1 == to + 1) \
2082 RANGE_TABLE_WORK_ELT (work_area, I + 1)++; \
2087 SET_RANGE_TABLE_WORK_AREA ((work_area), C1, C1); \
2093 /* Get the next unsigned number in the uncompiled pattern. */
2094 #define GET_UNSIGNED_NUMBER(num) \
2097 FREE_STACK_RETURN (REG_EBRACE); \
2101 while ('0' <= c && c <= '9') \
2107 num = num * 10 + c - '0'; \
2108 if (num / 10 != prev) \
2109 FREE_STACK_RETURN (REG_BADBR); \
2111 FREE_STACK_RETURN (REG_EBRACE); \
2117 #if ! WIDE_CHAR_SUPPORT
2119 /* Map a string to the char class it names (if any). */
2121 re_wctype (const re_char
*str
)
2123 const char *string
= str
;
2124 if (STREQ (string
, "alnum")) return RECC_ALNUM
;
2125 else if (STREQ (string
, "alpha")) return RECC_ALPHA
;
2126 else if (STREQ (string
, "word")) return RECC_WORD
;
2127 else if (STREQ (string
, "ascii")) return RECC_ASCII
;
2128 else if (STREQ (string
, "nonascii")) return RECC_NONASCII
;
2129 else if (STREQ (string
, "graph")) return RECC_GRAPH
;
2130 else if (STREQ (string
, "lower")) return RECC_LOWER
;
2131 else if (STREQ (string
, "print")) return RECC_PRINT
;
2132 else if (STREQ (string
, "punct")) return RECC_PUNCT
;
2133 else if (STREQ (string
, "space")) return RECC_SPACE
;
2134 else if (STREQ (string
, "upper")) return RECC_UPPER
;
2135 else if (STREQ (string
, "unibyte")) return RECC_UNIBYTE
;
2136 else if (STREQ (string
, "multibyte")) return RECC_MULTIBYTE
;
2137 else if (STREQ (string
, "digit")) return RECC_DIGIT
;
2138 else if (STREQ (string
, "xdigit")) return RECC_XDIGIT
;
2139 else if (STREQ (string
, "cntrl")) return RECC_CNTRL
;
2140 else if (STREQ (string
, "blank")) return RECC_BLANK
;
2144 /* True if CH is in the char class CC. */
2146 re_iswctype (int ch
, re_wctype_t cc
)
2150 case RECC_ALNUM
: return ISALNUM (ch
);
2151 case RECC_ALPHA
: return ISALPHA (ch
);
2152 case RECC_BLANK
: return ISBLANK (ch
);
2153 case RECC_CNTRL
: return ISCNTRL (ch
);
2154 case RECC_DIGIT
: return ISDIGIT (ch
);
2155 case RECC_GRAPH
: return ISGRAPH (ch
);
2156 case RECC_LOWER
: return ISLOWER (ch
);
2157 case RECC_PRINT
: return ISPRINT (ch
);
2158 case RECC_PUNCT
: return ISPUNCT (ch
);
2159 case RECC_SPACE
: return ISSPACE (ch
);
2160 case RECC_UPPER
: return ISUPPER (ch
);
2161 case RECC_XDIGIT
: return ISXDIGIT (ch
);
2162 case RECC_ASCII
: return IS_REAL_ASCII (ch
);
2163 case RECC_NONASCII
: return !IS_REAL_ASCII (ch
);
2164 case RECC_UNIBYTE
: return ISUNIBYTE (ch
);
2165 case RECC_MULTIBYTE
: return !ISUNIBYTE (ch
);
2166 case RECC_WORD
: return ISWORD (ch
);
2167 case RECC_ERROR
: return false;
2173 /* Return a bit-pattern to use in the range-table bits to match multibyte
2174 chars of class CC. */
2176 re_wctype_to_bit (re_wctype_t cc
)
2180 case RECC_NONASCII
: case RECC_PRINT
: case RECC_GRAPH
:
2181 case RECC_MULTIBYTE
: return BIT_MULTIBYTE
;
2182 case RECC_ALPHA
: case RECC_ALNUM
: case RECC_WORD
: return BIT_WORD
;
2183 case RECC_LOWER
: return BIT_LOWER
;
2184 case RECC_UPPER
: return BIT_UPPER
;
2185 case RECC_PUNCT
: return BIT_PUNCT
;
2186 case RECC_SPACE
: return BIT_SPACE
;
2187 case RECC_ASCII
: case RECC_DIGIT
: case RECC_XDIGIT
: case RECC_CNTRL
:
2188 case RECC_BLANK
: case RECC_UNIBYTE
: case RECC_ERROR
: return 0;
2195 /* Filling in the work area of a range. */
2197 /* Actually extend the space in WORK_AREA. */
2200 extend_range_table_work_area (struct range_table_work_area
*work_area
)
2202 work_area
->allocated
+= 16 * sizeof (int);
2203 if (work_area
->table
)
2205 = (int *) realloc (work_area
->table
, work_area
->allocated
);
2208 = (int *) malloc (work_area
->allocated
);
2214 /* Carefully find the ranges of codes that are equivalent
2215 under case conversion to the range start..end when passed through
2216 TRANSLATE. Handle the case where non-letters can come in between
2217 two upper-case letters (which happens in Latin-1).
2218 Also handle the case of groups of more than 2 case-equivalent chars.
2220 The basic method is to look at consecutive characters and see
2221 if they can form a run that can be handled as one.
2223 Returns -1 if successful, REG_ESPACE if ran out of space. */
2226 set_image_of_range_1 (work_area
, start
, end
, translate
)
2227 RE_TRANSLATE_TYPE translate
;
2228 struct range_table_work_area
*work_area
;
2229 re_wchar_t start
, end
;
2231 /* `one_case' indicates a character, or a run of characters,
2232 each of which is an isolate (no case-equivalents).
2233 This includes all ASCII non-letters.
2235 `two_case' indicates a character, or a run of characters,
2236 each of which has two case-equivalent forms.
2237 This includes all ASCII letters.
2239 `strange' indicates a character that has more than one
2242 enum case_type
{one_case
, two_case
, strange
};
2244 /* Describe the run that is in progress,
2245 which the next character can try to extend.
2246 If run_type is strange, that means there really is no run.
2247 If run_type is one_case, then run_start...run_end is the run.
2248 If run_type is two_case, then the run is run_start...run_end,
2249 and the case-equivalents end at run_eqv_end. */
2251 enum case_type run_type
= strange
;
2252 int run_start
, run_end
, run_eqv_end
;
2254 Lisp_Object eqv_table
;
2256 if (!RE_TRANSLATE_P (translate
))
2258 EXTEND_RANGE_TABLE (work_area
, 2);
2259 work_area
->table
[work_area
->used
++] = (start
);
2260 work_area
->table
[work_area
->used
++] = (end
);
2264 eqv_table
= XCHAR_TABLE (translate
)->extras
[2];
2266 for (; start
<= end
; start
++)
2268 enum case_type this_type
;
2269 int eqv
= RE_TRANSLATE (eqv_table
, start
);
2270 int minchar
, maxchar
;
2272 /* Classify this character */
2274 this_type
= one_case
;
2275 else if (RE_TRANSLATE (eqv_table
, eqv
) == start
)
2276 this_type
= two_case
;
2278 this_type
= strange
;
2281 minchar
= start
, maxchar
= eqv
;
2283 minchar
= eqv
, maxchar
= start
;
2285 /* Can this character extend the run in progress? */
2286 if (this_type
== strange
|| this_type
!= run_type
2287 || !(minchar
== run_end
+ 1
2288 && (run_type
== two_case
2289 ? maxchar
== run_eqv_end
+ 1 : 1)))
2292 Record each of its equivalent ranges. */
2293 if (run_type
== one_case
)
2295 EXTEND_RANGE_TABLE (work_area
, 2);
2296 work_area
->table
[work_area
->used
++] = run_start
;
2297 work_area
->table
[work_area
->used
++] = run_end
;
2299 else if (run_type
== two_case
)
2301 EXTEND_RANGE_TABLE (work_area
, 4);
2302 work_area
->table
[work_area
->used
++] = run_start
;
2303 work_area
->table
[work_area
->used
++] = run_end
;
2304 work_area
->table
[work_area
->used
++]
2305 = RE_TRANSLATE (eqv_table
, run_start
);
2306 work_area
->table
[work_area
->used
++]
2307 = RE_TRANSLATE (eqv_table
, run_end
);
2312 if (this_type
== strange
)
2314 /* For a strange character, add each of its equivalents, one
2315 by one. Don't start a range. */
2318 EXTEND_RANGE_TABLE (work_area
, 2);
2319 work_area
->table
[work_area
->used
++] = eqv
;
2320 work_area
->table
[work_area
->used
++] = eqv
;
2321 eqv
= RE_TRANSLATE (eqv_table
, eqv
);
2323 while (eqv
!= start
);
2326 /* Add this char to the run, or start a new run. */
2327 else if (run_type
== strange
)
2329 /* Initialize a new range. */
2330 run_type
= this_type
;
2333 run_eqv_end
= RE_TRANSLATE (eqv_table
, run_end
);
2337 /* Extend a running range. */
2339 run_eqv_end
= RE_TRANSLATE (eqv_table
, run_end
);
2343 /* If a run is still in progress at the end, finish it now
2344 by recording its equivalent ranges. */
2345 if (run_type
== one_case
)
2347 EXTEND_RANGE_TABLE (work_area
, 2);
2348 work_area
->table
[work_area
->used
++] = run_start
;
2349 work_area
->table
[work_area
->used
++] = run_end
;
2351 else if (run_type
== two_case
)
2353 EXTEND_RANGE_TABLE (work_area
, 4);
2354 work_area
->table
[work_area
->used
++] = run_start
;
2355 work_area
->table
[work_area
->used
++] = run_end
;
2356 work_area
->table
[work_area
->used
++]
2357 = RE_TRANSLATE (eqv_table
, run_start
);
2358 work_area
->table
[work_area
->used
++]
2359 = RE_TRANSLATE (eqv_table
, run_end
);
2367 /* Record the image of the range start..end when passed through
2368 TRANSLATE. This is not necessarily TRANSLATE(start)..TRANSLATE(end)
2369 and is not even necessarily contiguous.
2370 Normally we approximate it with the smallest contiguous range that contains
2371 all the chars we need. However, for Latin-1 we go to extra effort
2374 This function is not called for ASCII ranges.
2376 Returns -1 if successful, REG_ESPACE if ran out of space. */
2379 set_image_of_range (work_area
, start
, end
, translate
)
2380 RE_TRANSLATE_TYPE translate
;
2381 struct range_table_work_area
*work_area
;
2382 re_wchar_t start
, end
;
2384 re_wchar_t cmin
, cmax
;
2387 /* For Latin-1 ranges, use set_image_of_range_1
2388 to get proper handling of ranges that include letters and nonletters.
2389 For a range that includes the whole of Latin-1, this is not necessary.
2390 For other character sets, we don't bother to get this right. */
2391 if (RE_TRANSLATE_P (translate
) && start
< 04400
2392 && !(start
< 04200 && end
>= 04377))
2399 tem
= set_image_of_range_1 (work_area
, start
, newend
, translate
);
2409 EXTEND_RANGE_TABLE (work_area
, 2);
2410 work_area
->table
[work_area
->used
++] = (start
);
2411 work_area
->table
[work_area
->used
++] = (end
);
2413 cmin
= -1, cmax
= -1;
2415 if (RE_TRANSLATE_P (translate
))
2419 for (ch
= start
; ch
<= end
; ch
++)
2421 re_wchar_t c
= TRANSLATE (ch
);
2422 if (! (start
<= c
&& c
<= end
))
2428 cmin
= MIN (cmin
, c
);
2429 cmax
= MAX (cmax
, c
);
2436 EXTEND_RANGE_TABLE (work_area
, 2);
2437 work_area
->table
[work_area
->used
++] = (cmin
);
2438 work_area
->table
[work_area
->used
++] = (cmax
);
2446 #ifndef MATCH_MAY_ALLOCATE
2448 /* If we cannot allocate large objects within re_match_2_internal,
2449 we make the fail stack and register vectors global.
2450 The fail stack, we grow to the maximum size when a regexp
2452 The register vectors, we adjust in size each time we
2453 compile a regexp, according to the number of registers it needs. */
2455 static fail_stack_type fail_stack
;
2457 /* Size with which the following vectors are currently allocated.
2458 That is so we can make them bigger as needed,
2459 but never make them smaller. */
2460 static int regs_allocated_size
;
2462 static re_char
** regstart
, ** regend
;
2463 static re_char
**best_regstart
, **best_regend
;
2465 /* Make the register vectors big enough for NUM_REGS registers,
2466 but don't make them smaller. */
2469 regex_grow_registers (num_regs
)
2472 if (num_regs
> regs_allocated_size
)
2474 RETALLOC_IF (regstart
, num_regs
, re_char
*);
2475 RETALLOC_IF (regend
, num_regs
, re_char
*);
2476 RETALLOC_IF (best_regstart
, num_regs
, re_char
*);
2477 RETALLOC_IF (best_regend
, num_regs
, re_char
*);
2479 regs_allocated_size
= num_regs
;
2483 #endif /* not MATCH_MAY_ALLOCATE */
2485 static boolean group_in_compile_stack
_RE_ARGS ((compile_stack_type
2489 /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
2490 Returns one of error codes defined in `regex.h', or zero for success.
2492 Assumes the `allocated' (and perhaps `buffer') and `translate'
2493 fields are set in BUFP on entry.
2495 If it succeeds, results are put in BUFP (if it returns an error, the
2496 contents of BUFP are undefined):
2497 `buffer' is the compiled pattern;
2498 `syntax' is set to SYNTAX;
2499 `used' is set to the length of the compiled pattern;
2500 `fastmap_accurate' is zero;
2501 `re_nsub' is the number of subexpressions in PATTERN;
2502 `not_bol' and `not_eol' are zero;
2504 The `fastmap' field is neither examined nor set. */
2506 /* Insert the `jump' from the end of last alternative to "here".
2507 The space for the jump has already been allocated. */
2508 #define FIXUP_ALT_JUMP() \
2510 if (fixup_alt_jump) \
2511 STORE_JUMP (jump, fixup_alt_jump, b); \
2515 /* Return, freeing storage we allocated. */
2516 #define FREE_STACK_RETURN(value) \
2518 FREE_RANGE_TABLE_WORK_AREA (range_table_work); \
2519 free (compile_stack.stack); \
2523 static reg_errcode_t
2524 regex_compile (const re_char
*pattern
, size_t size
, reg_syntax_t syntax
, struct re_pattern_buffer
*bufp
)
2526 /* We fetch characters from PATTERN here. */
2527 register re_wchar_t c
, c1
;
2529 /* A random temporary spot in PATTERN. */
2532 /* Points to the end of the buffer, where we should append. */
2533 register unsigned char *b
;
2535 /* Keeps track of unclosed groups. */
2536 compile_stack_type compile_stack
;
2538 /* Points to the current (ending) position in the pattern. */
2540 /* `const' makes AIX compiler fail. */
2541 unsigned char *p
= pattern
;
2543 re_char
*p
= pattern
;
2545 re_char
*pend
= pattern
+ size
;
2547 /* How to translate the characters in the pattern. */
2548 RE_TRANSLATE_TYPE translate
= bufp
->translate
;
2550 /* Address of the count-byte of the most recently inserted `exactn'
2551 command. This makes it possible to tell if a new exact-match
2552 character can be added to that command or if the character requires
2553 a new `exactn' command. */
2554 unsigned char *pending_exact
= 0;
2556 /* Address of start of the most recently finished expression.
2557 This tells, e.g., postfix * where to find the start of its
2558 operand. Reset at the beginning of groups and alternatives. */
2559 unsigned char *laststart
= 0;
2561 /* Address of beginning of regexp, or inside of last group. */
2562 unsigned char *begalt
;
2564 /* Place in the uncompiled pattern (i.e., the {) to
2565 which to go back if the interval is invalid. */
2566 re_char
*beg_interval
;
2568 /* Address of the place where a forward jump should go to the end of
2569 the containing expression. Each alternative of an `or' -- except the
2570 last -- ends with a forward jump of this sort. */
2571 unsigned char *fixup_alt_jump
= 0;
2573 /* Work area for range table of charset. */
2574 struct range_table_work_area range_table_work
;
2576 /* If the object matched can contain multibyte characters. */
2577 const boolean multibyte
= RE_MULTIBYTE_P (bufp
);
2579 /* If a target of matching can contain multibyte characters. */
2580 const boolean target_multibyte
= RE_TARGET_MULTIBYTE_P (bufp
);
2582 /* Nonzero if we have pushed down into a subpattern. */
2583 int in_subpattern
= 0;
2585 /* These hold the values of p, pattern, and pend from the main
2586 pattern when we have pushed into a subpattern. */
2588 re_char
*main_pattern
;
2593 DEBUG_PRINT1 ("\nCompiling pattern: ");
2596 unsigned debug_count
;
2598 for (debug_count
= 0; debug_count
< size
; debug_count
++)
2599 putchar (pattern
[debug_count
]);
2604 /* Initialize the compile stack. */
2605 compile_stack
.stack
= TALLOC (INIT_COMPILE_STACK_SIZE
, compile_stack_elt_t
);
2606 if (compile_stack
.stack
== NULL
)
2609 compile_stack
.size
= INIT_COMPILE_STACK_SIZE
;
2610 compile_stack
.avail
= 0;
2612 range_table_work
.table
= 0;
2613 range_table_work
.allocated
= 0;
2615 /* Initialize the pattern buffer. */
2616 bufp
->syntax
= syntax
;
2617 bufp
->fastmap_accurate
= 0;
2618 bufp
->not_bol
= bufp
->not_eol
= 0;
2619 bufp
->used_syntax
= 0;
2621 /* Set `used' to zero, so that if we return an error, the pattern
2622 printer (for debugging) will think there's no pattern. We reset it
2626 /* Always count groups, whether or not bufp->no_sub is set. */
2629 #if !defined emacs && !defined SYNTAX_TABLE
2630 /* Initialize the syntax table. */
2631 init_syntax_once ();
2634 if (bufp
->allocated
== 0)
2637 { /* If zero allocated, but buffer is non-null, try to realloc
2638 enough space. This loses if buffer's address is bogus, but
2639 that is the user's responsibility. */
2640 RETALLOC (bufp
->buffer
, INIT_BUF_SIZE
, unsigned char);
2643 { /* Caller did not allocate a buffer. Do it for them. */
2644 bufp
->buffer
= TALLOC (INIT_BUF_SIZE
, unsigned char);
2646 if (!bufp
->buffer
) FREE_STACK_RETURN (REG_ESPACE
);
2648 bufp
->allocated
= INIT_BUF_SIZE
;
2651 begalt
= b
= bufp
->buffer
;
2653 /* Loop through the uncompiled pattern until we're at the end. */
2658 /* If this is the end of an included regexp,
2659 pop back to the main regexp and try again. */
2663 pattern
= main_pattern
;
2668 /* If this is the end of the main regexp, we are done. */
2680 /* If there's no special whitespace regexp, treat
2681 spaces normally. And don't try to do this recursively. */
2682 if (!whitespace_regexp
|| in_subpattern
)
2685 /* Peek past following spaces. */
2692 /* If the spaces are followed by a repetition op,
2693 treat them normally. */
2695 && (*p1
== '*' || *p1
== '+' || *p1
== '?'
2696 || (*p1
== '\\' && p1
+ 1 != pend
&& p1
[1] == '{')))
2699 /* Replace the spaces with the whitespace regexp. */
2703 main_pattern
= pattern
;
2704 p
= pattern
= whitespace_regexp
;
2705 pend
= p
+ strlen (p
);
2711 if ( /* If at start of pattern, it's an operator. */
2713 /* If context independent, it's an operator. */
2714 || syntax
& RE_CONTEXT_INDEP_ANCHORS
2715 /* Otherwise, depends on what's come before. */
2716 || at_begline_loc_p (pattern
, p
, syntax
))
2717 BUF_PUSH ((syntax
& RE_NO_NEWLINE_ANCHOR
) ? begbuf
: begline
);
2726 if ( /* If at end of pattern, it's an operator. */
2728 /* If context independent, it's an operator. */
2729 || syntax
& RE_CONTEXT_INDEP_ANCHORS
2730 /* Otherwise, depends on what's next. */
2731 || at_endline_loc_p (p
, pend
, syntax
))
2732 BUF_PUSH ((syntax
& RE_NO_NEWLINE_ANCHOR
) ? endbuf
: endline
);
2741 if ((syntax
& RE_BK_PLUS_QM
)
2742 || (syntax
& RE_LIMITED_OPS
))
2746 /* If there is no previous pattern... */
2749 if (syntax
& RE_CONTEXT_INVALID_OPS
)
2750 FREE_STACK_RETURN (REG_BADRPT
);
2751 else if (!(syntax
& RE_CONTEXT_INDEP_OPS
))
2756 /* 1 means zero (many) matches is allowed. */
2757 boolean zero_times_ok
= 0, many_times_ok
= 0;
2760 /* If there is a sequence of repetition chars, collapse it
2761 down to just one (the right one). We can't combine
2762 interval operators with these because of, e.g., `a{2}*',
2763 which should only match an even number of `a's. */
2767 if ((syntax
& RE_FRUGAL
)
2768 && c
== '?' && (zero_times_ok
|| many_times_ok
))
2772 zero_times_ok
|= c
!= '+';
2773 many_times_ok
|= c
!= '?';
2779 || (!(syntax
& RE_BK_PLUS_QM
)
2780 && (*p
== '+' || *p
== '?')))
2782 else if (syntax
& RE_BK_PLUS_QM
&& *p
== '\\')
2785 FREE_STACK_RETURN (REG_EESCAPE
);
2786 if (p
[1] == '+' || p
[1] == '?')
2787 PATFETCH (c
); /* Gobble up the backslash. */
2793 /* If we get here, we found another repeat character. */
2797 /* Star, etc. applied to an empty pattern is equivalent
2798 to an empty pattern. */
2799 if (!laststart
|| laststart
== b
)
2802 /* Now we know whether or not zero matches is allowed
2803 and also whether or not two or more matches is allowed. */
2808 boolean simple
= skip_one_char (laststart
) == b
;
2809 unsigned int startoffset
= 0;
2811 /* Check if the loop can match the empty string. */
2812 (simple
|| !analyse_first (laststart
, b
, NULL
, 0))
2813 ? on_failure_jump
: on_failure_jump_loop
;
2814 assert (skip_one_char (laststart
) <= b
);
2816 if (!zero_times_ok
&& simple
)
2817 { /* Since simple * loops can be made faster by using
2818 on_failure_keep_string_jump, we turn simple P+
2819 into PP* if P is simple. */
2820 unsigned char *p1
, *p2
;
2821 startoffset
= b
- laststart
;
2822 GET_BUFFER_SPACE (startoffset
);
2823 p1
= b
; p2
= laststart
;
2829 GET_BUFFER_SPACE (6);
2832 STORE_JUMP (ofj
, b
, b
+ 6);
2834 /* Simple * loops can use on_failure_keep_string_jump
2835 depending on what follows. But since we don't know
2836 that yet, we leave the decision up to
2837 on_failure_jump_smart. */
2838 INSERT_JUMP (simple
? on_failure_jump_smart
: ofj
,
2839 laststart
+ startoffset
, b
+ 6);
2841 STORE_JUMP (jump
, b
, laststart
+ startoffset
);
2846 /* A simple ? pattern. */
2847 assert (zero_times_ok
);
2848 GET_BUFFER_SPACE (3);
2849 INSERT_JUMP (on_failure_jump
, laststart
, b
+ 3);
2853 else /* not greedy */
2854 { /* I wish the greedy and non-greedy cases could be merged. */
2856 GET_BUFFER_SPACE (7); /* We might use less. */
2859 boolean emptyp
= analyse_first (laststart
, b
, NULL
, 0);
2861 /* The non-greedy multiple match looks like
2862 a repeat..until: we only need a conditional jump
2863 at the end of the loop. */
2864 if (emptyp
) BUF_PUSH (no_op
);
2865 STORE_JUMP (emptyp
? on_failure_jump_nastyloop
2866 : on_failure_jump
, b
, laststart
);
2870 /* The repeat...until naturally matches one or more.
2871 To also match zero times, we need to first jump to
2872 the end of the loop (its conditional jump). */
2873 INSERT_JUMP (jump
, laststart
, b
);
2879 /* non-greedy a?? */
2880 INSERT_JUMP (jump
, laststart
, b
+ 3);
2882 INSERT_JUMP (on_failure_jump
, laststart
, laststart
+ 6);
2899 CLEAR_RANGE_TABLE_WORK_USED (range_table_work
);
2901 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2903 /* Ensure that we have enough space to push a charset: the
2904 opcode, the length count, and the bitset; 34 bytes in all. */
2905 GET_BUFFER_SPACE (34);
2909 /* We test `*p == '^' twice, instead of using an if
2910 statement, so we only need one BUF_PUSH. */
2911 BUF_PUSH (*p
== '^' ? charset_not
: charset
);
2915 /* Remember the first position in the bracket expression. */
2918 /* Push the number of bytes in the bitmap. */
2919 BUF_PUSH ((1 << BYTEWIDTH
) / BYTEWIDTH
);
2921 /* Clear the whole map. */
2922 memset (b
, 0, (1 << BYTEWIDTH
) / BYTEWIDTH
);
2924 /* charset_not matches newline according to a syntax bit. */
2925 if ((re_opcode_t
) b
[-2] == charset_not
2926 && (syntax
& RE_HAT_LISTS_NOT_NEWLINE
))
2927 SET_LIST_BIT ('\n');
2929 /* Read in characters and ranges, setting map bits. */
2932 boolean escaped_char
= false;
2933 const unsigned char *p2
= p
;
2936 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2938 /* Don't translate yet. The range TRANSLATE(X..Y) cannot
2939 always be determined from TRANSLATE(X) and TRANSLATE(Y)
2940 So the translation is done later in a loop. Example:
2941 (let ((case-fold-search t)) (string-match "[A-_]" "A")) */
2944 /* \ might escape characters inside [...] and [^...]. */
2945 if ((syntax
& RE_BACKSLASH_ESCAPE_IN_LISTS
) && c
== '\\')
2947 if (p
== pend
) FREE_STACK_RETURN (REG_EESCAPE
);
2950 escaped_char
= true;
2954 /* Could be the end of the bracket expression. If it's
2955 not (i.e., when the bracket expression is `[]' so
2956 far), the ']' character bit gets set way below. */
2957 if (c
== ']' && p2
!= p1
)
2961 /* See if we're at the beginning of a possible character
2964 if (!escaped_char
&&
2965 syntax
& RE_CHAR_CLASSES
&& c
== '[' && *p
== ':')
2967 /* Leave room for the null. */
2968 unsigned char str
[CHAR_CLASS_MAX_LENGTH
+ 1];
2969 const unsigned char *class_beg
;
2975 /* If pattern is `[[:'. */
2976 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2981 if ((c
== ':' && *p
== ']') || p
== pend
)
2983 if (c1
< CHAR_CLASS_MAX_LENGTH
)
2986 /* This is in any case an invalid class name. */
2991 /* If isn't a word bracketed by `[:' and `:]':
2992 undo the ending character, the letters, and
2993 leave the leading `:' and `[' (but set bits for
2995 if (c
== ':' && *p
== ']')
3000 cc
= re_wctype (str
);
3003 FREE_STACK_RETURN (REG_ECTYPE
);
3005 /* Throw away the ] at the end of the character
3009 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
3012 for (ch
= 0; ch
< (1 << BYTEWIDTH
); ++ch
)
3013 if (re_iswctype (btowc (ch
), cc
))
3016 if (c
< (1 << BYTEWIDTH
))
3020 /* Most character classes in a multibyte match
3021 just set a flag. Exceptions are is_blank,
3022 is_digit, is_cntrl, and is_xdigit, since
3023 they can only match ASCII characters. We
3024 don't need to handle them for multibyte.
3025 They are distinguished by a negative wctype. */
3027 /* Setup the gl_state object to its buffer-defined
3028 value. This hardcodes the buffer-global
3029 syntax-table for ASCII chars, while the other chars
3030 will obey syntax-table properties. It's not ideal,
3031 but it's the way it's been done until now. */
3032 SETUP_BUFFER_SYNTAX_TABLE ();
3034 for (ch
= 0; ch
< 256; ++ch
)
3036 c
= RE_CHAR_TO_MULTIBYTE (ch
);
3037 if (! CHAR_BYTE8_P (c
)
3038 && re_iswctype (c
, cc
))
3044 if (ASCII_CHAR_P (c1
))
3046 else if ((c1
= RE_CHAR_TO_UNIBYTE (c1
)) >= 0)
3050 SET_RANGE_TABLE_WORK_AREA_BIT
3051 (range_table_work
, re_wctype_to_bit (cc
));
3053 /* In most cases the matching rule for char classes
3054 only uses the syntax table for multibyte chars,
3055 so that the content of the syntax-table it is not
3056 hardcoded in the range_table. SPACE and WORD are
3057 the two exceptions. */
3058 if ((1 << cc
) & ((1 << RECC_SPACE
) | (1 << RECC_WORD
)))
3059 bufp
->used_syntax
= 1;
3061 /* Repeat the loop. */
3066 /* Go back to right after the "[:". */
3070 /* Because the `:' may starts the range, we
3071 can't simply set bit and repeat the loop.
3072 Instead, just set it to C and handle below. */
3077 if (p
< pend
&& p
[0] == '-' && p
[1] != ']')
3080 /* Discard the `-'. */
3083 /* Fetch the character which ends the range. */
3086 if (CHAR_BYTE8_P (c1
)
3087 && ! ASCII_CHAR_P (c
) && ! CHAR_BYTE8_P (c
))
3088 /* Treat the range from a multibyte character to
3089 raw-byte character as empty. */
3094 /* Range from C to C. */
3099 if (syntax
& RE_NO_EMPTY_RANGES
)
3100 FREE_STACK_RETURN (REG_ERANGEX
);
3101 /* Else, repeat the loop. */
3106 /* Set the range into bitmap */
3107 for (; c
<= c1
; c
++)
3110 if (ch
< (1 << BYTEWIDTH
))
3117 SETUP_ASCII_RANGE (range_table_work
, c
, ch
);
3119 if (CHAR_BYTE8_P (c1
))
3120 c
= BYTE8_TO_CHAR (128);
3124 if (CHAR_BYTE8_P (c
))
3126 c
= CHAR_TO_BYTE8 (c
);
3127 c1
= CHAR_TO_BYTE8 (c1
);
3128 for (; c
<= c1
; c
++)
3133 SETUP_MULTIBYTE_RANGE (range_table_work
, c
, c1
);
3137 SETUP_UNIBYTE_RANGE (range_table_work
, c
, c1
);
3144 /* Discard any (non)matching list bytes that are all 0 at the
3145 end of the map. Decrease the map-length byte too. */
3146 while ((int) b
[-1] > 0 && b
[b
[-1] - 1] == 0)
3150 /* Build real range table from work area. */
3151 if (RANGE_TABLE_WORK_USED (range_table_work
)
3152 || RANGE_TABLE_WORK_BITS (range_table_work
))
3155 int used
= RANGE_TABLE_WORK_USED (range_table_work
);
3157 /* Allocate space for COUNT + RANGE_TABLE. Needs two
3158 bytes for flags, two for COUNT, and three bytes for
3160 GET_BUFFER_SPACE (4 + used
* 3);
3162 /* Indicate the existence of range table. */
3163 laststart
[1] |= 0x80;
3165 /* Store the character class flag bits into the range table.
3166 If not in emacs, these flag bits are always 0. */
3167 *b
++ = RANGE_TABLE_WORK_BITS (range_table_work
) & 0xff;
3168 *b
++ = RANGE_TABLE_WORK_BITS (range_table_work
) >> 8;
3170 STORE_NUMBER_AND_INCR (b
, used
/ 2);
3171 for (i
= 0; i
< used
; i
++)
3172 STORE_CHARACTER_AND_INCR
3173 (b
, RANGE_TABLE_WORK_ELT (range_table_work
, i
));
3180 if (syntax
& RE_NO_BK_PARENS
)
3187 if (syntax
& RE_NO_BK_PARENS
)
3194 if (syntax
& RE_NEWLINE_ALT
)
3201 if (syntax
& RE_NO_BK_VBAR
)
3208 if (syntax
& RE_INTERVALS
&& syntax
& RE_NO_BK_BRACES
)
3209 goto handle_interval
;
3215 if (p
== pend
) FREE_STACK_RETURN (REG_EESCAPE
);
3217 /* Do not translate the character after the \, so that we can
3218 distinguish, e.g., \B from \b, even if we normally would
3219 translate, e.g., B to b. */
3225 if (syntax
& RE_NO_BK_PARENS
)
3226 goto normal_backslash
;
3231 regnum_t regnum
= 0;
3234 /* Look for a special (?...) construct */
3235 if ((syntax
& RE_SHY_GROUPS
) && *p
== '?')
3237 PATFETCH (c
); /* Gobble up the '?'. */
3243 case ':': shy
= 1; break;
3245 /* An explicitly specified regnum must start
3248 FREE_STACK_RETURN (REG_BADPAT
);
3249 case '1': case '2': case '3': case '4':
3250 case '5': case '6': case '7': case '8': case '9':
3251 regnum
= 10*regnum
+ (c
- '0'); break;
3253 /* Only (?:...) is supported right now. */
3254 FREE_STACK_RETURN (REG_BADPAT
);
3261 regnum
= ++bufp
->re_nsub
;
3263 { /* It's actually not shy, but explicitly numbered. */
3265 if (regnum
> bufp
->re_nsub
)
3266 bufp
->re_nsub
= regnum
;
3267 else if (regnum
> bufp
->re_nsub
3268 /* Ideally, we'd want to check that the specified
3269 group can't have matched (i.e. all subgroups
3270 using the same regnum are in other branches of
3271 OR patterns), but we don't currently keep track
3272 of enough info to do that easily. */
3273 || group_in_compile_stack (compile_stack
, regnum
))
3274 FREE_STACK_RETURN (REG_BADPAT
);
3277 /* It's really shy. */
3278 regnum
= - bufp
->re_nsub
;
3280 if (COMPILE_STACK_FULL
)
3282 RETALLOC (compile_stack
.stack
, compile_stack
.size
<< 1,
3283 compile_stack_elt_t
);
3284 if (compile_stack
.stack
== NULL
) return REG_ESPACE
;
3286 compile_stack
.size
<<= 1;
3289 /* These are the values to restore when we hit end of this
3290 group. They are all relative offsets, so that if the
3291 whole pattern moves because of realloc, they will still
3293 COMPILE_STACK_TOP
.begalt_offset
= begalt
- bufp
->buffer
;
3294 COMPILE_STACK_TOP
.fixup_alt_jump
3295 = fixup_alt_jump
? fixup_alt_jump
- bufp
->buffer
+ 1 : 0;
3296 COMPILE_STACK_TOP
.laststart_offset
= b
- bufp
->buffer
;
3297 COMPILE_STACK_TOP
.regnum
= regnum
;
3299 /* Do not push a start_memory for groups beyond the last one
3300 we can represent in the compiled pattern. */
3301 if (regnum
<= MAX_REGNUM
&& regnum
> 0)
3302 BUF_PUSH_2 (start_memory
, regnum
);
3304 compile_stack
.avail
++;
3309 /* If we've reached MAX_REGNUM groups, then this open
3310 won't actually generate any code, so we'll have to
3311 clear pending_exact explicitly. */
3317 if (syntax
& RE_NO_BK_PARENS
) goto normal_backslash
;
3319 if (COMPILE_STACK_EMPTY
)
3321 if (syntax
& RE_UNMATCHED_RIGHT_PAREN_ORD
)
3322 goto normal_backslash
;
3324 FREE_STACK_RETURN (REG_ERPAREN
);
3330 /* See similar code for backslashed left paren above. */
3331 if (COMPILE_STACK_EMPTY
)
3333 if (syntax
& RE_UNMATCHED_RIGHT_PAREN_ORD
)
3336 FREE_STACK_RETURN (REG_ERPAREN
);
3339 /* Since we just checked for an empty stack above, this
3340 ``can't happen''. */
3341 assert (compile_stack
.avail
!= 0);
3343 /* We don't just want to restore into `regnum', because
3344 later groups should continue to be numbered higher,
3345 as in `(ab)c(de)' -- the second group is #2. */
3348 compile_stack
.avail
--;
3349 begalt
= bufp
->buffer
+ COMPILE_STACK_TOP
.begalt_offset
;
3351 = COMPILE_STACK_TOP
.fixup_alt_jump
3352 ? bufp
->buffer
+ COMPILE_STACK_TOP
.fixup_alt_jump
- 1
3354 laststart
= bufp
->buffer
+ COMPILE_STACK_TOP
.laststart_offset
;
3355 regnum
= COMPILE_STACK_TOP
.regnum
;
3356 /* If we've reached MAX_REGNUM groups, then this open
3357 won't actually generate any code, so we'll have to
3358 clear pending_exact explicitly. */
3361 /* We're at the end of the group, so now we know how many
3362 groups were inside this one. */
3363 if (regnum
<= MAX_REGNUM
&& regnum
> 0)
3364 BUF_PUSH_2 (stop_memory
, regnum
);
3369 case '|': /* `\|'. */
3370 if (syntax
& RE_LIMITED_OPS
|| syntax
& RE_NO_BK_VBAR
)
3371 goto normal_backslash
;
3373 if (syntax
& RE_LIMITED_OPS
)
3376 /* Insert before the previous alternative a jump which
3377 jumps to this alternative if the former fails. */
3378 GET_BUFFER_SPACE (3);
3379 INSERT_JUMP (on_failure_jump
, begalt
, b
+ 6);
3383 /* The alternative before this one has a jump after it
3384 which gets executed if it gets matched. Adjust that
3385 jump so it will jump to this alternative's analogous
3386 jump (put in below, which in turn will jump to the next
3387 (if any) alternative's such jump, etc.). The last such
3388 jump jumps to the correct final destination. A picture:
3394 If we are at `b', then fixup_alt_jump right now points to a
3395 three-byte space after `a'. We'll put in the jump, set
3396 fixup_alt_jump to right after `b', and leave behind three
3397 bytes which we'll fill in when we get to after `c'. */
3401 /* Mark and leave space for a jump after this alternative,
3402 to be filled in later either by next alternative or
3403 when know we're at the end of a series of alternatives. */
3405 GET_BUFFER_SPACE (3);
3414 /* If \{ is a literal. */
3415 if (!(syntax
& RE_INTERVALS
)
3416 /* If we're at `\{' and it's not the open-interval
3418 || (syntax
& RE_NO_BK_BRACES
))
3419 goto normal_backslash
;
3423 /* If got here, then the syntax allows intervals. */
3425 /* At least (most) this many matches must be made. */
3426 int lower_bound
= 0, upper_bound
= -1;
3430 GET_UNSIGNED_NUMBER (lower_bound
);
3433 GET_UNSIGNED_NUMBER (upper_bound
);
3435 /* Interval such as `{1}' => match exactly once. */
3436 upper_bound
= lower_bound
;
3438 if (lower_bound
< 0 || upper_bound
> RE_DUP_MAX
3439 || (upper_bound
>= 0 && lower_bound
> upper_bound
))
3440 FREE_STACK_RETURN (REG_BADBR
);
3442 if (!(syntax
& RE_NO_BK_BRACES
))
3445 FREE_STACK_RETURN (REG_BADBR
);
3447 FREE_STACK_RETURN (REG_EESCAPE
);
3452 FREE_STACK_RETURN (REG_BADBR
);
3454 /* We just parsed a valid interval. */
3456 /* If it's invalid to have no preceding re. */
3459 if (syntax
& RE_CONTEXT_INVALID_OPS
)
3460 FREE_STACK_RETURN (REG_BADRPT
);
3461 else if (syntax
& RE_CONTEXT_INDEP_OPS
)
3464 goto unfetch_interval
;
3467 if (upper_bound
== 0)
3468 /* If the upper bound is zero, just drop the sub pattern
3471 else if (lower_bound
== 1 && upper_bound
== 1)
3472 /* Just match it once: nothing to do here. */
3475 /* Otherwise, we have a nontrivial interval. When
3476 we're all done, the pattern will look like:
3477 set_number_at <jump count> <upper bound>
3478 set_number_at <succeed_n count> <lower bound>
3479 succeed_n <after jump addr> <succeed_n count>
3481 jump_n <succeed_n addr> <jump count>
3482 (The upper bound and `jump_n' are omitted if
3483 `upper_bound' is 1, though.) */
3485 { /* If the upper bound is > 1, we need to insert
3486 more at the end of the loop. */
3487 unsigned int nbytes
= (upper_bound
< 0 ? 3
3488 : upper_bound
> 1 ? 5 : 0);
3489 unsigned int startoffset
= 0;
3491 GET_BUFFER_SPACE (20); /* We might use less. */
3493 if (lower_bound
== 0)
3495 /* A succeed_n that starts with 0 is really a
3496 a simple on_failure_jump_loop. */
3497 INSERT_JUMP (on_failure_jump_loop
, laststart
,
3503 /* Initialize lower bound of the `succeed_n', even
3504 though it will be set during matching by its
3505 attendant `set_number_at' (inserted next),
3506 because `re_compile_fastmap' needs to know.
3507 Jump to the `jump_n' we might insert below. */
3508 INSERT_JUMP2 (succeed_n
, laststart
,
3513 /* Code to initialize the lower bound. Insert
3514 before the `succeed_n'. The `5' is the last two
3515 bytes of this `set_number_at', plus 3 bytes of
3516 the following `succeed_n'. */
3517 insert_op2 (set_number_at
, laststart
, 5, lower_bound
, b
);
3522 if (upper_bound
< 0)
3524 /* A negative upper bound stands for infinity,
3525 in which case it degenerates to a plain jump. */
3526 STORE_JUMP (jump
, b
, laststart
+ startoffset
);
3529 else if (upper_bound
> 1)
3530 { /* More than one repetition is allowed, so
3531 append a backward jump to the `succeed_n'
3532 that starts this interval.
3534 When we've reached this during matching,
3535 we'll have matched the interval once, so
3536 jump back only `upper_bound - 1' times. */
3537 STORE_JUMP2 (jump_n
, b
, laststart
+ startoffset
,
3541 /* The location we want to set is the second
3542 parameter of the `jump_n'; that is `b-2' as
3543 an absolute address. `laststart' will be
3544 the `set_number_at' we're about to insert;
3545 `laststart+3' the number to set, the source
3546 for the relative address. But we are
3547 inserting into the middle of the pattern --
3548 so everything is getting moved up by 5.
3549 Conclusion: (b - 2) - (laststart + 3) + 5,
3550 i.e., b - laststart.
3552 We insert this at the beginning of the loop
3553 so that if we fail during matching, we'll
3554 reinitialize the bounds. */
3555 insert_op2 (set_number_at
, laststart
, b
- laststart
,
3556 upper_bound
- 1, b
);
3561 beg_interval
= NULL
;
3566 /* If an invalid interval, match the characters as literals. */
3567 assert (beg_interval
);
3569 beg_interval
= NULL
;
3571 /* normal_char and normal_backslash need `c'. */
3574 if (!(syntax
& RE_NO_BK_BRACES
))
3576 assert (p
> pattern
&& p
[-1] == '\\');
3577 goto normal_backslash
;
3583 /* There is no way to specify the before_dot and after_dot
3584 operators. rms says this is ok. --karl */
3592 BUF_PUSH_2 (syntaxspec
, syntax_spec_code
[c
]);
3598 BUF_PUSH_2 (notsyntaxspec
, syntax_spec_code
[c
]);
3604 BUF_PUSH_2 (categoryspec
, c
);
3610 BUF_PUSH_2 (notcategoryspec
, c
);
3616 if (syntax
& RE_NO_GNU_OPS
)
3619 BUF_PUSH_2 (syntaxspec
, Sword
);
3624 if (syntax
& RE_NO_GNU_OPS
)
3627 BUF_PUSH_2 (notsyntaxspec
, Sword
);
3632 if (syntax
& RE_NO_GNU_OPS
)
3638 if (syntax
& RE_NO_GNU_OPS
)
3644 if (syntax
& RE_NO_GNU_OPS
)
3653 FREE_STACK_RETURN (REG_BADPAT
);
3657 if (syntax
& RE_NO_GNU_OPS
)
3659 BUF_PUSH (wordbound
);
3663 if (syntax
& RE_NO_GNU_OPS
)
3665 BUF_PUSH (notwordbound
);
3669 if (syntax
& RE_NO_GNU_OPS
)
3675 if (syntax
& RE_NO_GNU_OPS
)
3680 case '1': case '2': case '3': case '4': case '5':
3681 case '6': case '7': case '8': case '9':
3685 if (syntax
& RE_NO_BK_REFS
)
3686 goto normal_backslash
;
3690 if (reg
> bufp
->re_nsub
|| reg
< 1
3691 /* Can't back reference to a subexp before its end. */
3692 || group_in_compile_stack (compile_stack
, reg
))
3693 FREE_STACK_RETURN (REG_ESUBREG
);
3696 BUF_PUSH_2 (duplicate
, reg
);
3703 if (syntax
& RE_BK_PLUS_QM
)
3706 goto normal_backslash
;
3710 /* You might think it would be useful for \ to mean
3711 not to translate; but if we don't translate it
3712 it will never match anything. */
3719 /* Expects the character in `c'. */
3721 /* If no exactn currently being built. */
3724 /* If last exactn not at current position. */
3725 || pending_exact
+ *pending_exact
+ 1 != b
3727 /* We have only one byte following the exactn for the count. */
3728 || *pending_exact
>= (1 << BYTEWIDTH
) - MAX_MULTIBYTE_LENGTH
3730 /* If followed by a repetition operator. */
3731 || (p
!= pend
&& (*p
== '*' || *p
== '^'))
3732 || ((syntax
& RE_BK_PLUS_QM
)
3733 ? p
+ 1 < pend
&& *p
== '\\' && (p
[1] == '+' || p
[1] == '?')
3734 : p
!= pend
&& (*p
== '+' || *p
== '?'))
3735 || ((syntax
& RE_INTERVALS
)
3736 && ((syntax
& RE_NO_BK_BRACES
)
3737 ? p
!= pend
&& *p
== '{'
3738 : p
+ 1 < pend
&& p
[0] == '\\' && p
[1] == '{')))
3740 /* Start building a new exactn. */
3744 BUF_PUSH_2 (exactn
, 0);
3745 pending_exact
= b
- 1;
3748 GET_BUFFER_SPACE (MAX_MULTIBYTE_LENGTH
);
3755 len
= CHAR_STRING (c
, b
);
3760 c1
= RE_CHAR_TO_MULTIBYTE (c
);
3761 if (! CHAR_BYTE8_P (c1
))
3763 re_wchar_t c2
= TRANSLATE (c1
);
3765 if (c1
!= c2
&& (c1
= RE_CHAR_TO_UNIBYTE (c2
)) >= 0)
3771 (*pending_exact
) += len
;
3776 } /* while p != pend */
3779 /* Through the pattern now. */
3783 if (!COMPILE_STACK_EMPTY
)
3784 FREE_STACK_RETURN (REG_EPAREN
);
3786 /* If we don't want backtracking, force success
3787 the first time we reach the end of the compiled pattern. */
3788 if (syntax
& RE_NO_POSIX_BACKTRACKING
)
3791 /* We have succeeded; set the length of the buffer. */
3792 bufp
->used
= b
- bufp
->buffer
;
3797 re_compile_fastmap (bufp
);
3798 DEBUG_PRINT1 ("\nCompiled pattern: \n");
3799 print_compiled_pattern (bufp
);
3804 #ifndef MATCH_MAY_ALLOCATE
3805 /* Initialize the failure stack to the largest possible stack. This
3806 isn't necessary unless we're trying to avoid calling alloca in
3807 the search and match routines. */
3809 int num_regs
= bufp
->re_nsub
+ 1;
3811 if (fail_stack
.size
< re_max_failures
* TYPICAL_FAILURE_SIZE
)
3813 fail_stack
.size
= re_max_failures
* TYPICAL_FAILURE_SIZE
;
3815 if (! fail_stack
.stack
)
3817 = (fail_stack_elt_t
*) malloc (fail_stack
.size
3818 * sizeof (fail_stack_elt_t
));
3821 = (fail_stack_elt_t
*) realloc (fail_stack
.stack
,
3823 * sizeof (fail_stack_elt_t
)));
3826 regex_grow_registers (num_regs
);
3828 #endif /* not MATCH_MAY_ALLOCATE */
3830 FREE_STACK_RETURN (REG_NOERROR
);
3831 } /* regex_compile */
3833 /* Subroutines for `regex_compile'. */
3835 /* Store OP at LOC followed by two-byte integer parameter ARG. */
3838 store_op1 (re_opcode_t op
, unsigned char *loc
, int arg
)
3840 *loc
= (unsigned char) op
;
3841 STORE_NUMBER (loc
+ 1, arg
);
3845 /* Like `store_op1', but for two two-byte parameters ARG1 and ARG2. */
3848 store_op2 (re_opcode_t op
, unsigned char *loc
, int arg1
, int arg2
)
3850 *loc
= (unsigned char) op
;
3851 STORE_NUMBER (loc
+ 1, arg1
);
3852 STORE_NUMBER (loc
+ 3, arg2
);
3856 /* Copy the bytes from LOC to END to open up three bytes of space at LOC
3857 for OP followed by two-byte integer parameter ARG. */
3860 insert_op1 (re_opcode_t op
, unsigned char *loc
, int arg
, unsigned char *end
)
3862 register unsigned char *pfrom
= end
;
3863 register unsigned char *pto
= end
+ 3;
3865 while (pfrom
!= loc
)
3868 store_op1 (op
, loc
, arg
);
3872 /* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2. */
3875 insert_op2 (re_opcode_t op
, unsigned char *loc
, int arg1
, int arg2
, unsigned char *end
)
3877 register unsigned char *pfrom
= end
;
3878 register unsigned char *pto
= end
+ 5;
3880 while (pfrom
!= loc
)
3883 store_op2 (op
, loc
, arg1
, arg2
);
3887 /* P points to just after a ^ in PATTERN. Return true if that ^ comes
3888 after an alternative or a begin-subexpression. We assume there is at
3889 least one character before the ^. */
3892 at_begline_loc_p (const re_char
*pattern
, const re_char
*p
, reg_syntax_t syntax
)
3894 re_char
*prev
= p
- 2;
3895 boolean prev_prev_backslash
= prev
> pattern
&& prev
[-1] == '\\';
3898 /* After a subexpression? */
3899 (*prev
== '(' && (syntax
& RE_NO_BK_PARENS
|| prev_prev_backslash
))
3900 /* After an alternative? */
3901 || (*prev
== '|' && (syntax
& RE_NO_BK_VBAR
|| prev_prev_backslash
))
3902 /* After a shy subexpression? */
3903 || ((syntax
& RE_SHY_GROUPS
) && prev
- 2 >= pattern
3904 && prev
[-1] == '?' && prev
[-2] == '('
3905 && (syntax
& RE_NO_BK_PARENS
3906 || (prev
- 3 >= pattern
&& prev
[-3] == '\\')));
3910 /* The dual of at_begline_loc_p. This one is for $. We assume there is
3911 at least one character after the $, i.e., `P < PEND'. */
3914 at_endline_loc_p (const re_char
*p
, const re_char
*pend
, reg_syntax_t syntax
)
3917 boolean next_backslash
= *next
== '\\';
3918 re_char
*next_next
= p
+ 1 < pend
? p
+ 1 : 0;
3921 /* Before a subexpression? */
3922 (syntax
& RE_NO_BK_PARENS
? *next
== ')'
3923 : next_backslash
&& next_next
&& *next_next
== ')')
3924 /* Before an alternative? */
3925 || (syntax
& RE_NO_BK_VBAR
? *next
== '|'
3926 : next_backslash
&& next_next
&& *next_next
== '|');
3930 /* Returns true if REGNUM is in one of COMPILE_STACK's elements and
3931 false if it's not. */
3934 group_in_compile_stack (compile_stack_type compile_stack
, regnum_t regnum
)
3938 for (this_element
= compile_stack
.avail
- 1;
3941 if (compile_stack
.stack
[this_element
].regnum
== regnum
)
3948 If fastmap is non-NULL, go through the pattern and fill fastmap
3949 with all the possible leading chars. If fastmap is NULL, don't
3950 bother filling it up (obviously) and only return whether the
3951 pattern could potentially match the empty string.
3953 Return 1 if p..pend might match the empty string.
3954 Return 0 if p..pend matches at least one char.
3955 Return -1 if fastmap was not updated accurately. */
3958 analyse_first (const re_char
*p
, const re_char
*pend
, char *fastmap
, const int multibyte
)
3963 /* If all elements for base leading-codes in fastmap is set, this
3964 flag is set true. */
3965 boolean match_any_multibyte_characters
= false;
3969 /* The loop below works as follows:
3970 - It has a working-list kept in the PATTERN_STACK and which basically
3971 starts by only containing a pointer to the first operation.
3972 - If the opcode we're looking at is a match against some set of
3973 chars, then we add those chars to the fastmap and go on to the
3974 next work element from the worklist (done via `break').
3975 - If the opcode is a control operator on the other hand, we either
3976 ignore it (if it's meaningless at this point, such as `start_memory')
3977 or execute it (if it's a jump). If the jump has several destinations
3978 (i.e. `on_failure_jump'), then we push the other destination onto the
3980 We guarantee termination by ignoring backward jumps (more or less),
3981 so that `p' is monotonically increasing. More to the point, we
3982 never set `p' (or push) anything `<= p1'. */
3986 /* `p1' is used as a marker of how far back a `on_failure_jump'
3987 can go without being ignored. It is normally equal to `p'
3988 (which prevents any backward `on_failure_jump') except right
3989 after a plain `jump', to allow patterns such as:
3992 10: on_failure_jump 3
3993 as used for the *? operator. */
3996 switch (SWITCH_ENUM_CAST ((re_opcode_t
) *p
++))
4003 /* If the first character has to match a backreference, that means
4004 that the group was empty (since it already matched). Since this
4005 is the only case that interests us here, we can assume that the
4006 backreference must match the empty string. */
4011 /* Following are the cases which match a character. These end
4017 /* If multibyte is nonzero, the first byte of each
4018 character is an ASCII or a leading code. Otherwise,
4019 each byte is a character. Thus, this works in both
4024 /* For the case of matching this unibyte regex
4025 against multibyte, we must set a leading code of
4026 the corresponding multibyte character. */
4027 int c
= RE_CHAR_TO_MULTIBYTE (p
[1]);
4029 fastmap
[CHAR_LEADING_CODE (c
)] = 1;
4036 /* We could put all the chars except for \n (and maybe \0)
4037 but we don't bother since it is generally not worth it. */
4038 if (!fastmap
) break;
4043 if (!fastmap
) break;
4045 /* Chars beyond end of bitmap are possible matches. */
4046 for (j
= CHARSET_BITMAP_SIZE (&p
[-1]) * BYTEWIDTH
;
4047 j
< (1 << BYTEWIDTH
); j
++)
4053 if (!fastmap
) break;
4054 not = (re_opcode_t
) *(p
- 1) == charset_not
;
4055 for (j
= CHARSET_BITMAP_SIZE (&p
[-1]) * BYTEWIDTH
- 1, p
++;
4057 if (!!(p
[j
/ BYTEWIDTH
] & (1 << (j
% BYTEWIDTH
))) ^ not)
4061 if (/* Any leading code can possibly start a character
4062 which doesn't match the specified set of characters. */
4065 /* If we can match a character class, we can match any
4066 multibyte characters. */
4067 (CHARSET_RANGE_TABLE_EXISTS_P (&p
[-2])
4068 && CHARSET_RANGE_TABLE_BITS (&p
[-2]) != 0))
4071 if (match_any_multibyte_characters
== false)
4073 for (j
= MIN_MULTIBYTE_LEADING_CODE
;
4074 j
<= MAX_MULTIBYTE_LEADING_CODE
; j
++)
4076 match_any_multibyte_characters
= true;
4080 else if (!not && CHARSET_RANGE_TABLE_EXISTS_P (&p
[-2])
4081 && match_any_multibyte_characters
== false)
4083 /* Set fastmap[I] to 1 where I is a leading code of each
4084 multibyte character in the range table. */
4086 unsigned char lc1
, lc2
;
4088 /* Make P points the range table. `+ 2' is to skip flag
4089 bits for a character class. */
4090 p
+= CHARSET_BITMAP_SIZE (&p
[-2]) + 2;
4092 /* Extract the number of ranges in range table into COUNT. */
4093 EXTRACT_NUMBER_AND_INCR (count
, p
);
4094 for (; count
> 0; count
--, p
+= 3)
4096 /* Extract the start and end of each range. */
4097 EXTRACT_CHARACTER (c
, p
);
4098 lc1
= CHAR_LEADING_CODE (c
);
4100 EXTRACT_CHARACTER (c
, p
);
4101 lc2
= CHAR_LEADING_CODE (c
);
4102 for (j
= lc1
; j
<= lc2
; j
++)
4111 if (!fastmap
) break;
4113 not = (re_opcode_t
)p
[-1] == notsyntaxspec
;
4115 for (j
= 0; j
< (1 << BYTEWIDTH
); j
++)
4116 if ((SYNTAX (j
) == (enum syntaxcode
) k
) ^ not)
4120 /* This match depends on text properties. These end with
4121 aborting optimizations. */
4125 case notcategoryspec
:
4126 if (!fastmap
) break;
4127 not = (re_opcode_t
)p
[-1] == notcategoryspec
;
4129 for (j
= (1 << BYTEWIDTH
); j
>= 0; j
--)
4130 if ((CHAR_HAS_CATEGORY (j
, k
)) ^ not)
4133 /* Any leading code can possibly start a character which
4134 has or doesn't has the specified category. */
4135 if (match_any_multibyte_characters
== false)
4137 for (j
= MIN_MULTIBYTE_LEADING_CODE
;
4138 j
<= MAX_MULTIBYTE_LEADING_CODE
; j
++)
4140 match_any_multibyte_characters
= true;
4144 /* All cases after this match the empty string. These end with
4166 EXTRACT_NUMBER_AND_INCR (j
, p
);
4168 /* Backward jumps can only go back to code that we've already
4169 visited. `re_compile' should make sure this is true. */
4172 switch (SWITCH_ENUM_CAST ((re_opcode_t
) *p
))
4174 case on_failure_jump
:
4175 case on_failure_keep_string_jump
:
4176 case on_failure_jump_loop
:
4177 case on_failure_jump_nastyloop
:
4178 case on_failure_jump_smart
:
4184 /* Keep `p1' to allow the `on_failure_jump' we are jumping to
4185 to jump back to "just after here". */
4188 case on_failure_jump
:
4189 case on_failure_keep_string_jump
:
4190 case on_failure_jump_nastyloop
:
4191 case on_failure_jump_loop
:
4192 case on_failure_jump_smart
:
4193 EXTRACT_NUMBER_AND_INCR (j
, p
);
4195 ; /* Backward jump to be ignored. */
4197 { /* We have to look down both arms.
4198 We first go down the "straight" path so as to minimize
4199 stack usage when going through alternatives. */
4200 int r
= analyse_first (p
, pend
, fastmap
, multibyte
);
4208 /* This code simply does not properly handle forward jump_n. */
4209 DEBUG_STATEMENT (EXTRACT_NUMBER (j
, p
); assert (j
< 0));
4211 /* jump_n can either jump or fall through. The (backward) jump
4212 case has already been handled, so we only need to look at the
4213 fallthrough case. */
4217 /* If N == 0, it should be an on_failure_jump_loop instead. */
4218 DEBUG_STATEMENT (EXTRACT_NUMBER (j
, p
+ 2); assert (j
> 0));
4220 /* We only care about one iteration of the loop, so we don't
4221 need to consider the case where this behaves like an
4238 abort (); /* We have listed all the cases. */
4241 /* Getting here means we have found the possible starting
4242 characters for one path of the pattern -- and that the empty
4243 string does not match. We need not follow this path further. */
4247 /* We reached the end without matching anything. */
4250 } /* analyse_first */
4252 /* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
4253 BUFP. A fastmap records which of the (1 << BYTEWIDTH) possible
4254 characters can start a string that matches the pattern. This fastmap
4255 is used by re_search to skip quickly over impossible starting points.
4257 Character codes above (1 << BYTEWIDTH) are not represented in the
4258 fastmap, but the leading codes are represented. Thus, the fastmap
4259 indicates which character sets could start a match.
4261 The caller must supply the address of a (1 << BYTEWIDTH)-byte data
4262 area as BUFP->fastmap.
4264 We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in
4267 Returns 0 if we succeed, -2 if an internal error. */
4270 re_compile_fastmap (struct re_pattern_buffer
*bufp
)
4272 char *fastmap
= bufp
->fastmap
;
4275 assert (fastmap
&& bufp
->buffer
);
4277 memset (fastmap
, 0, 1 << BYTEWIDTH
); /* Assume nothing's valid. */
4278 bufp
->fastmap_accurate
= 1; /* It will be when we're done. */
4280 analysis
= analyse_first (bufp
->buffer
, bufp
->buffer
+ bufp
->used
,
4281 fastmap
, RE_MULTIBYTE_P (bufp
));
4282 bufp
->can_be_null
= (analysis
!= 0);
4284 } /* re_compile_fastmap */
4286 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
4287 ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
4288 this memory for recording register information. STARTS and ENDS
4289 must be allocated using the malloc library routine, and must each
4290 be at least NUM_REGS * sizeof (regoff_t) bytes long.
4292 If NUM_REGS == 0, then subsequent matches should allocate their own
4295 Unless this function is called, the first search or match using
4296 PATTERN_BUFFER will allocate its own register data, without
4297 freeing the old data. */
4300 re_set_registers (struct re_pattern_buffer
*bufp
, struct re_registers
*regs
, unsigned int num_regs
, regoff_t
*starts
, regoff_t
*ends
)
4304 bufp
->regs_allocated
= REGS_REALLOCATE
;
4305 regs
->num_regs
= num_regs
;
4306 regs
->start
= starts
;
4311 bufp
->regs_allocated
= REGS_UNALLOCATED
;
4313 regs
->start
= regs
->end
= (regoff_t
*) 0;
4316 WEAK_ALIAS (__re_set_registers
, re_set_registers
)
4318 /* Searching routines. */
4320 /* Like re_search_2, below, but only one string is specified, and
4321 doesn't let you say where to stop matching. */
4324 re_search (struct re_pattern_buffer
*bufp
, const char *string
, int size
, int startpos
, int range
, struct re_registers
*regs
)
4326 return re_search_2 (bufp
, NULL
, 0, string
, size
, startpos
, range
,
4329 WEAK_ALIAS (__re_search
, re_search
)
4331 /* Head address of virtual concatenation of string. */
4332 #define HEAD_ADDR_VSTRING(P) \
4333 (((P) >= size1 ? string2 : string1))
4335 /* End address of virtual concatenation of string. */
4336 #define STOP_ADDR_VSTRING(P) \
4337 (((P) >= size1 ? string2 + size2 : string1 + size1))
4339 /* Address of POS in the concatenation of virtual string. */
4340 #define POS_ADDR_VSTRING(POS) \
4341 (((POS) >= size1 ? string2 - size1 : string1) + (POS))
4343 /* Using the compiled pattern in BUFP->buffer, first tries to match the
4344 virtual concatenation of STRING1 and STRING2, starting first at index
4345 STARTPOS, then at STARTPOS + 1, and so on.
4347 STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
4349 RANGE is how far to scan while trying to match. RANGE = 0 means try
4350 only at STARTPOS; in general, the last start tried is STARTPOS +
4353 In REGS, return the indices of the virtual concatenation of STRING1
4354 and STRING2 that matched the entire BUFP->buffer and its contained
4357 Do not consider matching one past the index STOP in the virtual
4358 concatenation of STRING1 and STRING2.
4360 We return either the position in the strings at which the match was
4361 found, -1 if no match, or -2 if error (such as failure
4365 re_search_2 (struct re_pattern_buffer
*bufp
, const char *str1
, int size1
, const char *str2
, int size2
, int startpos
, int range
, struct re_registers
*regs
, int stop
)
4368 re_char
*string1
= (re_char
*) str1
;
4369 re_char
*string2
= (re_char
*) str2
;
4370 register char *fastmap
= bufp
->fastmap
;
4371 register RE_TRANSLATE_TYPE translate
= bufp
->translate
;
4372 int total_size
= size1
+ size2
;
4373 int endpos
= startpos
+ range
;
4374 boolean anchored_start
;
4375 /* Nonzero if we are searching multibyte string. */
4376 const boolean multibyte
= RE_TARGET_MULTIBYTE_P (bufp
);
4378 /* Check for out-of-range STARTPOS. */
4379 if (startpos
< 0 || startpos
> total_size
)
4382 /* Fix up RANGE if it might eventually take us outside
4383 the virtual concatenation of STRING1 and STRING2.
4384 Make sure we won't move STARTPOS below 0 or above TOTAL_SIZE. */
4386 range
= 0 - startpos
;
4387 else if (endpos
> total_size
)
4388 range
= total_size
- startpos
;
4390 /* If the search isn't to be a backwards one, don't waste time in a
4391 search for a pattern anchored at beginning of buffer. */
4392 if (bufp
->used
> 0 && (re_opcode_t
) bufp
->buffer
[0] == begbuf
&& range
> 0)
4401 /* In a forward search for something that starts with \=.
4402 don't keep searching past point. */
4403 if (bufp
->used
> 0 && (re_opcode_t
) bufp
->buffer
[0] == at_dot
&& range
> 0)
4405 range
= PT_BYTE
- BEGV_BYTE
- startpos
;
4411 /* Update the fastmap now if not correct already. */
4412 if (fastmap
&& !bufp
->fastmap_accurate
)
4413 re_compile_fastmap (bufp
);
4415 /* See whether the pattern is anchored. */
4416 anchored_start
= (bufp
->buffer
[0] == begline
);
4419 gl_state
.object
= re_match_object
; /* Used by SYNTAX_TABLE_BYTE_TO_CHAR. */
4421 int charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (POS_AS_IN_BUFFER (startpos
));
4423 SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object
, charpos
, 1);
4427 /* Loop through the string, looking for a place to start matching. */
4430 /* If the pattern is anchored,
4431 skip quickly past places we cannot match.
4432 We don't bother to treat startpos == 0 specially
4433 because that case doesn't repeat. */
4434 if (anchored_start
&& startpos
> 0)
4436 if (! ((startpos
<= size1
? string1
[startpos
- 1]
4437 : string2
[startpos
- size1
- 1])
4442 /* If a fastmap is supplied, skip quickly over characters that
4443 cannot be the start of a match. If the pattern can match the
4444 null string, however, we don't need to skip characters; we want
4445 the first null string. */
4446 if (fastmap
&& startpos
< total_size
&& !bufp
->can_be_null
)
4448 register re_char
*d
;
4449 register re_wchar_t buf_ch
;
4451 d
= POS_ADDR_VSTRING (startpos
);
4453 if (range
> 0) /* Searching forwards. */
4455 register int lim
= 0;
4458 if (startpos
< size1
&& startpos
+ range
>= size1
)
4459 lim
= range
- (size1
- startpos
);
4461 /* Written out as an if-else to avoid testing `translate'
4463 if (RE_TRANSLATE_P (translate
))
4470 buf_ch
= STRING_CHAR_AND_LENGTH (d
, buf_charlen
);
4471 buf_ch
= RE_TRANSLATE (translate
, buf_ch
);
4472 if (fastmap
[CHAR_LEADING_CODE (buf_ch
)])
4475 range
-= buf_charlen
;
4481 register re_wchar_t ch
, translated
;
4484 ch
= RE_CHAR_TO_MULTIBYTE (buf_ch
);
4485 translated
= RE_TRANSLATE (translate
, ch
);
4486 if (translated
!= ch
4487 && (ch
= RE_CHAR_TO_UNIBYTE (translated
)) >= 0)
4489 if (fastmap
[buf_ch
])
4502 buf_ch
= STRING_CHAR_AND_LENGTH (d
, buf_charlen
);
4503 if (fastmap
[CHAR_LEADING_CODE (buf_ch
)])
4505 range
-= buf_charlen
;
4509 while (range
> lim
&& !fastmap
[*d
])
4515 startpos
+= irange
- range
;
4517 else /* Searching backwards. */
4521 buf_ch
= STRING_CHAR (d
);
4522 buf_ch
= TRANSLATE (buf_ch
);
4523 if (! fastmap
[CHAR_LEADING_CODE (buf_ch
)])
4528 register re_wchar_t ch
, translated
;
4531 ch
= RE_CHAR_TO_MULTIBYTE (buf_ch
);
4532 translated
= TRANSLATE (ch
);
4533 if (translated
!= ch
4534 && (ch
= RE_CHAR_TO_UNIBYTE (translated
)) >= 0)
4536 if (! fastmap
[TRANSLATE (buf_ch
)])
4542 /* If can't match the null string, and that's all we have left, fail. */
4543 if (range
>= 0 && startpos
== total_size
&& fastmap
4544 && !bufp
->can_be_null
)
4547 val
= re_match_2_internal (bufp
, string1
, size1
, string2
, size2
,
4548 startpos
, regs
, stop
);
4561 /* Update STARTPOS to the next character boundary. */
4564 re_char
*p
= POS_ADDR_VSTRING (startpos
);
4565 re_char
*pend
= STOP_ADDR_VSTRING (startpos
);
4566 int len
= BYTES_BY_CHAR_HEAD (*p
);
4584 /* Update STARTPOS to the previous character boundary. */
4587 re_char
*p
= POS_ADDR_VSTRING (startpos
) + 1;
4589 re_char
*phead
= HEAD_ADDR_VSTRING (startpos
);
4591 /* Find the head of multibyte form. */
4592 PREV_CHAR_BOUNDARY (p
, phead
);
4593 range
+= p0
- 1 - p
;
4597 startpos
-= p0
- 1 - p
;
4603 WEAK_ALIAS (__re_search_2
, re_search_2
)
4605 /* Declarations and macros for re_match_2. */
4607 static int bcmp_translate
_RE_ARGS((re_char
*s1
, re_char
*s2
,
4609 RE_TRANSLATE_TYPE translate
,
4610 const int multibyte
));
4612 /* This converts PTR, a pointer into one of the search strings `string1'
4613 and `string2' into an offset from the beginning of that string. */
4614 #define POINTER_TO_OFFSET(ptr) \
4615 (FIRST_STRING_P (ptr) \
4616 ? ((regoff_t) ((ptr) - string1)) \
4617 : ((regoff_t) ((ptr) - string2 + size1)))
4619 /* Call before fetching a character with *d. This switches over to
4620 string2 if necessary.
4621 Check re_match_2_internal for a discussion of why end_match_2 might
4622 not be within string2 (but be equal to end_match_1 instead). */
4623 #define PREFETCH() \
4626 /* End of string2 => fail. */ \
4627 if (dend == end_match_2) \
4629 /* End of string1 => advance to string2. */ \
4631 dend = end_match_2; \
4634 /* Call before fetching a char with *d if you already checked other limits.
4635 This is meant for use in lookahead operations like wordend, etc..
4636 where we might need to look at parts of the string that might be
4637 outside of the LIMITs (i.e past `stop'). */
4638 #define PREFETCH_NOLIMIT() \
4642 dend = end_match_2; \
4645 /* Test if at very beginning or at very end of the virtual concatenation
4646 of `string1' and `string2'. If only one string, it's `string2'. */
4647 #define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2)
4648 #define AT_STRINGS_END(d) ((d) == end2)
4651 /* Test if D points to a character which is word-constituent. We have
4652 two special cases to check for: if past the end of string1, look at
4653 the first character in string2; and if before the beginning of
4654 string2, look at the last character in string1. */
4655 #define WORDCHAR_P(d) \
4656 (SYNTAX ((d) == end1 ? *string2 \
4657 : (d) == string2 - 1 ? *(end1 - 1) : *(d)) \
4660 /* Disabled due to a compiler bug -- see comment at case wordbound */
4662 /* The comment at case wordbound is following one, but we don't use
4663 AT_WORD_BOUNDARY anymore to support multibyte form.
4665 The DEC Alpha C compiler 3.x generates incorrect code for the
4666 test WORDCHAR_P (d - 1) != WORDCHAR_P (d) in the expansion of
4667 AT_WORD_BOUNDARY, so this code is disabled. Expanding the
4668 macro and introducing temporary variables works around the bug. */
4671 /* Test if the character before D and the one at D differ with respect
4672 to being word-constituent. */
4673 #define AT_WORD_BOUNDARY(d) \
4674 (AT_STRINGS_BEG (d) || AT_STRINGS_END (d) \
4675 || WORDCHAR_P (d - 1) != WORDCHAR_P (d))
4678 /* Free everything we malloc. */
4679 #ifdef MATCH_MAY_ALLOCATE
4680 # define FREE_VAR(var) if (var) { REGEX_FREE (var); var = NULL; } else
4681 # define FREE_VARIABLES() \
4683 REGEX_FREE_STACK (fail_stack.stack); \
4684 FREE_VAR (regstart); \
4685 FREE_VAR (regend); \
4686 FREE_VAR (best_regstart); \
4687 FREE_VAR (best_regend); \
4690 # define FREE_VARIABLES() ((void)0) /* Do nothing! But inhibit gcc warning. */
4691 #endif /* not MATCH_MAY_ALLOCATE */
4694 /* Optimization routines. */
4696 /* If the operation is a match against one or more chars,
4697 return a pointer to the next operation, else return NULL. */
4699 skip_one_char (const re_char
*p
)
4701 switch (SWITCH_ENUM_CAST (*p
++))
4712 if (CHARSET_RANGE_TABLE_EXISTS_P (p
- 1))
4715 p
= CHARSET_RANGE_TABLE (p
- 1);
4716 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
4717 p
= CHARSET_RANGE_TABLE_END (p
, mcnt
);
4720 p
+= 1 + CHARSET_BITMAP_SIZE (p
- 1);
4727 case notcategoryspec
:
4739 /* Jump over non-matching operations. */
4741 skip_noops (const re_char
*p
, const re_char
*pend
)
4746 switch (SWITCH_ENUM_CAST ((re_opcode_t
) *p
))
4755 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
4766 /* Non-zero if "p1 matches something" implies "p2 fails". */
4768 mutually_exclusive_p (struct re_pattern_buffer
*bufp
, const re_char
*p1
, const re_char
*p2
)
4771 const boolean multibyte
= RE_MULTIBYTE_P (bufp
);
4772 unsigned char *pend
= bufp
->buffer
+ bufp
->used
;
4774 assert (p1
>= bufp
->buffer
&& p1
< pend
4775 && p2
>= bufp
->buffer
&& p2
<= pend
);
4777 /* Skip over open/close-group commands.
4778 If what follows this loop is a ...+ construct,
4779 look at what begins its body, since we will have to
4780 match at least one of that. */
4781 p2
= skip_noops (p2
, pend
);
4782 /* The same skip can be done for p1, except that this function
4783 is only used in the case where p1 is a simple match operator. */
4784 /* p1 = skip_noops (p1, pend); */
4786 assert (p1
>= bufp
->buffer
&& p1
< pend
4787 && p2
>= bufp
->buffer
&& p2
<= pend
);
4789 op2
= p2
== pend
? succeed
: *p2
;
4791 switch (SWITCH_ENUM_CAST (op2
))
4795 /* If we're at the end of the pattern, we can change. */
4796 if (skip_one_char (p1
))
4798 DEBUG_PRINT1 (" End of pattern: fast loop.\n");
4806 register re_wchar_t c
4807 = (re_opcode_t
) *p2
== endline
? '\n'
4808 : RE_STRING_CHAR (p2
+ 2, multibyte
);
4810 if ((re_opcode_t
) *p1
== exactn
)
4812 if (c
!= RE_STRING_CHAR (p1
+ 2, multibyte
))
4814 DEBUG_PRINT3 (" '%c' != '%c' => fast loop.\n", c
, p1
[2]);
4819 else if ((re_opcode_t
) *p1
== charset
4820 || (re_opcode_t
) *p1
== charset_not
)
4822 int not = (re_opcode_t
) *p1
== charset_not
;
4824 /* Test if C is listed in charset (or charset_not)
4826 if (! multibyte
|| IS_REAL_ASCII (c
))
4828 if (c
< CHARSET_BITMAP_SIZE (p1
) * BYTEWIDTH
4829 && p1
[2 + c
/ BYTEWIDTH
] & (1 << (c
% BYTEWIDTH
)))
4832 else if (CHARSET_RANGE_TABLE_EXISTS_P (p1
))
4833 CHARSET_LOOKUP_RANGE_TABLE (not, c
, p1
);
4835 /* `not' is equal to 1 if c would match, which means
4836 that we can't change to pop_failure_jump. */
4839 DEBUG_PRINT1 (" No match => fast loop.\n");
4843 else if ((re_opcode_t
) *p1
== anychar
4846 DEBUG_PRINT1 (" . != \\n => fast loop.\n");
4854 if ((re_opcode_t
) *p1
== exactn
)
4855 /* Reuse the code above. */
4856 return mutually_exclusive_p (bufp
, p2
, p1
);
4858 /* It is hard to list up all the character in charset
4859 P2 if it includes multibyte character. Give up in
4861 else if (!multibyte
|| !CHARSET_RANGE_TABLE_EXISTS_P (p2
))
4863 /* Now, we are sure that P2 has no range table.
4864 So, for the size of bitmap in P2, `p2[1]' is
4865 enough. But P1 may have range table, so the
4866 size of bitmap table of P1 is extracted by
4867 using macro `CHARSET_BITMAP_SIZE'.
4869 In a multibyte case, we know that all the character
4870 listed in P2 is ASCII. In a unibyte case, P1 has only a
4871 bitmap table. So, in both cases, it is enough to test
4872 only the bitmap table of P1. */
4874 if ((re_opcode_t
) *p1
== charset
)
4877 /* We win if the charset inside the loop
4878 has no overlap with the one after the loop. */
4881 && idx
< CHARSET_BITMAP_SIZE (p1
));
4883 if ((p2
[2 + idx
] & p1
[2 + idx
]) != 0)
4887 || idx
== CHARSET_BITMAP_SIZE (p1
))
4889 DEBUG_PRINT1 (" No match => fast loop.\n");
4893 else if ((re_opcode_t
) *p1
== charset_not
)
4896 /* We win if the charset_not inside the loop lists
4897 every character listed in the charset after. */
4898 for (idx
= 0; idx
< (int) p2
[1]; idx
++)
4899 if (! (p2
[2 + idx
] == 0
4900 || (idx
< CHARSET_BITMAP_SIZE (p1
)
4901 && ((p2
[2 + idx
] & ~ p1
[2 + idx
]) == 0))))
4906 DEBUG_PRINT1 (" No match => fast loop.\n");
4915 switch (SWITCH_ENUM_CAST (*p1
))
4919 /* Reuse the code above. */
4920 return mutually_exclusive_p (bufp
, p2
, p1
);
4922 /* When we have two charset_not, it's very unlikely that
4923 they don't overlap. The union of the two sets of excluded
4924 chars should cover all possible chars, which, as a matter of
4925 fact, is virtually impossible in multibyte buffers. */
4931 return ((re_opcode_t
) *p1
== syntaxspec
&& p1
[1] == Sword
);
4933 return ((re_opcode_t
) *p1
== syntaxspec
4934 && (p1
[1] == Ssymbol
|| p1
[1] == Sword
));
4936 return ((re_opcode_t
) *p1
== syntaxspec
&& p1
[1] == p2
[1]);
4939 return ((re_opcode_t
) *p1
== notsyntaxspec
&& p1
[1] == Sword
);
4941 return ((re_opcode_t
) *p1
== notsyntaxspec
4942 && (p1
[1] == Ssymbol
|| p1
[1] == Sword
));
4944 return ((re_opcode_t
) *p1
== notsyntaxspec
&& p1
[1] == p2
[1]);
4947 return (((re_opcode_t
) *p1
== notsyntaxspec
4948 || (re_opcode_t
) *p1
== syntaxspec
)
4953 return ((re_opcode_t
) *p1
== notcategoryspec
&& p1
[1] == p2
[1]);
4954 case notcategoryspec
:
4955 return ((re_opcode_t
) *p1
== categoryspec
&& p1
[1] == p2
[1]);
4967 /* Matching routines. */
4969 #ifndef emacs /* Emacs never uses this. */
4970 /* re_match is like re_match_2 except it takes only a single string. */
4973 re_match (struct re_pattern_buffer
*bufp
, const char *string
,
4974 int size
, int pos
, struct re_registers
*regs
)
4976 int result
= re_match_2_internal (bufp
, NULL
, 0, (re_char
*) string
, size
,
4980 WEAK_ALIAS (__re_match
, re_match
)
4981 #endif /* not emacs */
4984 /* In Emacs, this is the string or buffer in which we
4985 are matching. It is used for looking up syntax properties. */
4986 Lisp_Object re_match_object
;
4989 /* re_match_2 matches the compiled pattern in BUFP against the
4990 the (virtual) concatenation of STRING1 and STRING2 (of length SIZE1
4991 and SIZE2, respectively). We start matching at POS, and stop
4994 If REGS is non-null and the `no_sub' field of BUFP is nonzero, we
4995 store offsets for the substring each group matched in REGS. See the
4996 documentation for exactly how many groups we fill.
4998 We return -1 if no match, -2 if an internal error (such as the
4999 failure stack overflowing). Otherwise, we return the length of the
5000 matched substring. */
5003 re_match_2 (struct re_pattern_buffer
*bufp
, const char *string1
, int size1
, const char *string2
, int size2
, int pos
, struct re_registers
*regs
, int stop
)
5009 gl_state
.object
= re_match_object
; /* Used by SYNTAX_TABLE_BYTE_TO_CHAR. */
5010 charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (POS_AS_IN_BUFFER (pos
));
5011 SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object
, charpos
, 1);
5014 result
= re_match_2_internal (bufp
, (re_char
*) string1
, size1
,
5015 (re_char
*) string2
, size2
,
5019 WEAK_ALIAS (__re_match_2
, re_match_2
)
5022 /* This is a separate function so that we can force an alloca cleanup
5025 re_match_2_internal (struct re_pattern_buffer
*bufp
, const re_char
*string1
, int size1
, const re_char
*string2
, int size2
, int pos
, struct re_registers
*regs
, int stop
)
5027 /* General temporaries. */
5032 /* Just past the end of the corresponding string. */
5033 re_char
*end1
, *end2
;
5035 /* Pointers into string1 and string2, just past the last characters in
5036 each to consider matching. */
5037 re_char
*end_match_1
, *end_match_2
;
5039 /* Where we are in the data, and the end of the current string. */
5042 /* Used sometimes to remember where we were before starting matching
5043 an operator so that we can go back in case of failure. This "atomic"
5044 behavior of matching opcodes is indispensable to the correctness
5045 of the on_failure_keep_string_jump optimization. */
5048 /* Where we are in the pattern, and the end of the pattern. */
5049 re_char
*p
= bufp
->buffer
;
5050 re_char
*pend
= p
+ bufp
->used
;
5052 /* We use this to map every character in the string. */
5053 RE_TRANSLATE_TYPE translate
= bufp
->translate
;
5055 /* Nonzero if BUFP is setup from a multibyte regex. */
5056 const boolean multibyte
= RE_MULTIBYTE_P (bufp
);
5058 /* Nonzero if STRING1/STRING2 are multibyte. */
5059 const boolean target_multibyte
= RE_TARGET_MULTIBYTE_P (bufp
);
5061 /* Failure point stack. Each place that can handle a failure further
5062 down the line pushes a failure point on this stack. It consists of
5063 regstart, and regend for all registers corresponding to
5064 the subexpressions we're currently inside, plus the number of such
5065 registers, and, finally, two char *'s. The first char * is where
5066 to resume scanning the pattern; the second one is where to resume
5067 scanning the strings. */
5068 #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
5069 fail_stack_type fail_stack
;
5072 unsigned nfailure_points_pushed
= 0, nfailure_points_popped
= 0;
5075 #if defined REL_ALLOC && defined REGEX_MALLOC
5076 /* This holds the pointer to the failure stack, when
5077 it is allocated relocatably. */
5078 fail_stack_elt_t
*failure_stack_ptr
;
5081 /* We fill all the registers internally, independent of what we
5082 return, for use in backreferences. The number here includes
5083 an element for register zero. */
5084 size_t num_regs
= bufp
->re_nsub
+ 1;
5086 /* Information on the contents of registers. These are pointers into
5087 the input strings; they record just what was matched (on this
5088 attempt) by a subexpression part of the pattern, that is, the
5089 regnum-th regstart pointer points to where in the pattern we began
5090 matching and the regnum-th regend points to right after where we
5091 stopped matching the regnum-th subexpression. (The zeroth register
5092 keeps track of what the whole pattern matches.) */
5093 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
5094 re_char
**regstart
, **regend
;
5097 /* The following record the register info as found in the above
5098 variables when we find a match better than any we've seen before.
5099 This happens as we backtrack through the failure points, which in
5100 turn happens only if we have not yet matched the entire string. */
5101 unsigned best_regs_set
= false;
5102 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
5103 re_char
**best_regstart
, **best_regend
;
5106 /* Logically, this is `best_regend[0]'. But we don't want to have to
5107 allocate space for that if we're not allocating space for anything
5108 else (see below). Also, we never need info about register 0 for
5109 any of the other register vectors, and it seems rather a kludge to
5110 treat `best_regend' differently than the rest. So we keep track of
5111 the end of the best match so far in a separate variable. We
5112 initialize this to NULL so that when we backtrack the first time
5113 and need to test it, it's not garbage. */
5114 re_char
*match_end
= NULL
;
5117 /* Counts the total number of registers pushed. */
5118 unsigned num_regs_pushed
= 0;
5121 DEBUG_PRINT1 ("\n\nEntering re_match_2.\n");
5125 #ifdef MATCH_MAY_ALLOCATE
5126 /* Do not bother to initialize all the register variables if there are
5127 no groups in the pattern, as it takes a fair amount of time. If
5128 there are groups, we include space for register 0 (the whole
5129 pattern), even though we never use it, since it simplifies the
5130 array indexing. We should fix this. */
5133 regstart
= REGEX_TALLOC (num_regs
, re_char
*);
5134 regend
= REGEX_TALLOC (num_regs
, re_char
*);
5135 best_regstart
= REGEX_TALLOC (num_regs
, re_char
*);
5136 best_regend
= REGEX_TALLOC (num_regs
, re_char
*);
5138 if (!(regstart
&& regend
&& best_regstart
&& best_regend
))
5146 /* We must initialize all our variables to NULL, so that
5147 `FREE_VARIABLES' doesn't try to free them. */
5148 regstart
= regend
= best_regstart
= best_regend
= NULL
;
5150 #endif /* MATCH_MAY_ALLOCATE */
5152 /* The starting position is bogus. */
5153 if (pos
< 0 || pos
> size1
+ size2
)
5159 /* Initialize subexpression text positions to -1 to mark ones that no
5160 start_memory/stop_memory has been seen for. Also initialize the
5161 register information struct. */
5162 for (reg
= 1; reg
< num_regs
; reg
++)
5163 regstart
[reg
] = regend
[reg
] = NULL
;
5165 /* We move `string1' into `string2' if the latter's empty -- but not if
5166 `string1' is null. */
5167 if (size2
== 0 && string1
!= NULL
)
5174 end1
= string1
+ size1
;
5175 end2
= string2
+ size2
;
5177 /* `p' scans through the pattern as `d' scans through the data.
5178 `dend' is the end of the input string that `d' points within. `d'
5179 is advanced into the following input string whenever necessary, but
5180 this happens before fetching; therefore, at the beginning of the
5181 loop, `d' can be pointing at the end of a string, but it cannot
5185 /* Only match within string2. */
5186 d
= string2
+ pos
- size1
;
5187 dend
= end_match_2
= string2
+ stop
- size1
;
5188 end_match_1
= end1
; /* Just to give it a value. */
5194 /* Only match within string1. */
5195 end_match_1
= string1
+ stop
;
5197 When we reach end_match_1, PREFETCH normally switches to string2.
5198 But in the present case, this means that just doing a PREFETCH
5199 makes us jump from `stop' to `gap' within the string.
5200 What we really want here is for the search to stop as
5201 soon as we hit end_match_1. That's why we set end_match_2
5202 to end_match_1 (since PREFETCH fails as soon as we hit
5204 end_match_2
= end_match_1
;
5207 { /* It's important to use this code when stop == size so that
5208 moving `d' from end1 to string2 will not prevent the d == dend
5209 check from catching the end of string. */
5211 end_match_2
= string2
+ stop
- size1
;
5217 DEBUG_PRINT1 ("The compiled pattern is: ");
5218 DEBUG_PRINT_COMPILED_PATTERN (bufp
, p
, pend
);
5219 DEBUG_PRINT1 ("The string to match is: `");
5220 DEBUG_PRINT_DOUBLE_STRING (d
, string1
, size1
, string2
, size2
);
5221 DEBUG_PRINT1 ("'\n");
5223 /* This loops over pattern commands. It exits by returning from the
5224 function if the match is complete, or it drops through if the match
5225 fails at this starting point in the input data. */
5228 DEBUG_PRINT2 ("\n%p: ", p
);
5231 { /* End of pattern means we might have succeeded. */
5232 DEBUG_PRINT1 ("end of pattern ... ");
5234 /* If we haven't matched the entire string, and we want the
5235 longest match, try backtracking. */
5236 if (d
!= end_match_2
)
5238 /* 1 if this match ends in the same string (string1 or string2)
5239 as the best previous match. */
5240 boolean same_str_p
= (FIRST_STRING_P (match_end
)
5241 == FIRST_STRING_P (d
));
5242 /* 1 if this match is the best seen so far. */
5243 boolean best_match_p
;
5245 /* AIX compiler got confused when this was combined
5246 with the previous declaration. */
5248 best_match_p
= d
> match_end
;
5250 best_match_p
= !FIRST_STRING_P (d
);
5252 DEBUG_PRINT1 ("backtracking.\n");
5254 if (!FAIL_STACK_EMPTY ())
5255 { /* More failure points to try. */
5257 /* If exceeds best match so far, save it. */
5258 if (!best_regs_set
|| best_match_p
)
5260 best_regs_set
= true;
5263 DEBUG_PRINT1 ("\nSAVING match as best so far.\n");
5265 for (reg
= 1; reg
< num_regs
; reg
++)
5267 best_regstart
[reg
] = regstart
[reg
];
5268 best_regend
[reg
] = regend
[reg
];
5274 /* If no failure points, don't restore garbage. And if
5275 last match is real best match, don't restore second
5277 else if (best_regs_set
&& !best_match_p
)
5280 /* Restore best match. It may happen that `dend ==
5281 end_match_1' while the restored d is in string2.
5282 For example, the pattern `x.*y.*z' against the
5283 strings `x-' and `y-z-', if the two strings are
5284 not consecutive in memory. */
5285 DEBUG_PRINT1 ("Restoring best registers.\n");
5288 dend
= ((d
>= string1
&& d
<= end1
)
5289 ? end_match_1
: end_match_2
);
5291 for (reg
= 1; reg
< num_regs
; reg
++)
5293 regstart
[reg
] = best_regstart
[reg
];
5294 regend
[reg
] = best_regend
[reg
];
5297 } /* d != end_match_2 */
5300 DEBUG_PRINT1 ("Accepting match.\n");
5302 /* If caller wants register contents data back, do it. */
5303 if (regs
&& !bufp
->no_sub
)
5305 /* Have the register data arrays been allocated? */
5306 if (bufp
->regs_allocated
== REGS_UNALLOCATED
)
5307 { /* No. So allocate them with malloc. We need one
5308 extra element beyond `num_regs' for the `-1' marker
5310 regs
->num_regs
= MAX (RE_NREGS
, num_regs
+ 1);
5311 regs
->start
= TALLOC (regs
->num_regs
, regoff_t
);
5312 regs
->end
= TALLOC (regs
->num_regs
, regoff_t
);
5313 if (regs
->start
== NULL
|| regs
->end
== NULL
)
5318 bufp
->regs_allocated
= REGS_REALLOCATE
;
5320 else if (bufp
->regs_allocated
== REGS_REALLOCATE
)
5321 { /* Yes. If we need more elements than were already
5322 allocated, reallocate them. If we need fewer, just
5324 if (regs
->num_regs
< num_regs
+ 1)
5326 regs
->num_regs
= num_regs
+ 1;
5327 RETALLOC (regs
->start
, regs
->num_regs
, regoff_t
);
5328 RETALLOC (regs
->end
, regs
->num_regs
, regoff_t
);
5329 if (regs
->start
== NULL
|| regs
->end
== NULL
)
5338 /* These braces fend off a "empty body in an else-statement"
5339 warning under GCC when assert expands to nothing. */
5340 assert (bufp
->regs_allocated
== REGS_FIXED
);
5343 /* Convert the pointer data in `regstart' and `regend' to
5344 indices. Register zero has to be set differently,
5345 since we haven't kept track of any info for it. */
5346 if (regs
->num_regs
> 0)
5348 regs
->start
[0] = pos
;
5349 regs
->end
[0] = POINTER_TO_OFFSET (d
);
5352 /* Go through the first `min (num_regs, regs->num_regs)'
5353 registers, since that is all we initialized. */
5354 for (reg
= 1; reg
< MIN (num_regs
, regs
->num_regs
); reg
++)
5356 if (REG_UNSET (regstart
[reg
]) || REG_UNSET (regend
[reg
]))
5357 regs
->start
[reg
] = regs
->end
[reg
] = -1;
5361 = (regoff_t
) POINTER_TO_OFFSET (regstart
[reg
]);
5363 = (regoff_t
) POINTER_TO_OFFSET (regend
[reg
]);
5367 /* If the regs structure we return has more elements than
5368 were in the pattern, set the extra elements to -1. If
5369 we (re)allocated the registers, this is the case,
5370 because we always allocate enough to have at least one
5372 for (reg
= num_regs
; reg
< regs
->num_regs
; reg
++)
5373 regs
->start
[reg
] = regs
->end
[reg
] = -1;
5374 } /* regs && !bufp->no_sub */
5376 DEBUG_PRINT4 ("%u failure points pushed, %u popped (%u remain).\n",
5377 nfailure_points_pushed
, nfailure_points_popped
,
5378 nfailure_points_pushed
- nfailure_points_popped
);
5379 DEBUG_PRINT2 ("%u registers pushed.\n", num_regs_pushed
);
5381 mcnt
= POINTER_TO_OFFSET (d
) - pos
;
5383 DEBUG_PRINT2 ("Returning %d from re_match_2.\n", mcnt
);
5389 /* Otherwise match next pattern command. */
5390 switch (SWITCH_ENUM_CAST ((re_opcode_t
) *p
++))
5392 /* Ignore these. Used to ignore the n of succeed_n's which
5393 currently have n == 0. */
5395 DEBUG_PRINT1 ("EXECUTING no_op.\n");
5399 DEBUG_PRINT1 ("EXECUTING succeed.\n");
5402 /* Match the next n pattern characters exactly. The following
5403 byte in the pattern defines n, and the n bytes after that
5404 are the characters to match. */
5407 DEBUG_PRINT2 ("EXECUTING exactn %d.\n", mcnt
);
5409 /* Remember the start point to rollback upon failure. */
5413 /* This is written out as an if-else so we don't waste time
5414 testing `translate' inside the loop. */
5415 if (RE_TRANSLATE_P (translate
))
5419 if (RE_TRANSLATE (translate
, *d
) != *p
++)
5439 /* The cost of testing `translate' is comparatively small. */
5440 if (target_multibyte
)
5443 int pat_charlen
, buf_charlen
;
5448 pat_ch
= STRING_CHAR_AND_LENGTH (p
, pat_charlen
);
5451 pat_ch
= RE_CHAR_TO_MULTIBYTE (*p
);
5454 buf_ch
= STRING_CHAR_AND_LENGTH (d
, buf_charlen
);
5456 if (TRANSLATE (buf_ch
) != pat_ch
)
5464 mcnt
-= pat_charlen
;
5470 int pat_charlen
, buf_charlen
;
5476 pat_ch
= STRING_CHAR_AND_LENGTH (p
, pat_charlen
);
5477 pat_ch
= RE_CHAR_TO_UNIBYTE (pat_ch
);
5484 buf_ch
= RE_CHAR_TO_MULTIBYTE (*d
);
5485 if (! CHAR_BYTE8_P (buf_ch
))
5487 buf_ch
= TRANSLATE (buf_ch
);
5488 buf_ch
= RE_CHAR_TO_UNIBYTE (buf_ch
);
5494 if (buf_ch
!= pat_ch
)
5507 /* Match any character except possibly a newline or a null. */
5513 DEBUG_PRINT1 ("EXECUTING anychar.\n");
5516 buf_ch
= RE_STRING_CHAR_AND_LENGTH (d
, buf_charlen
,
5518 buf_ch
= TRANSLATE (buf_ch
);
5520 if ((!(bufp
->syntax
& RE_DOT_NEWLINE
)
5522 || ((bufp
->syntax
& RE_DOT_NOT_NULL
)
5523 && buf_ch
== '\000'))
5526 DEBUG_PRINT2 (" Matched `%d'.\n", *d
);
5535 register unsigned int c
;
5536 boolean
not = (re_opcode_t
) *(p
- 1) == charset_not
;
5539 /* Start of actual range_table, or end of bitmap if there is no
5541 re_char
*range_table
;
5543 /* Nonzero if there is a range table. */
5544 int range_table_exists
;
5546 /* Number of ranges of range table. This is not included
5547 in the initial byte-length of the command. */
5550 /* Whether matching against a unibyte character. */
5551 boolean unibyte_char
= false;
5553 DEBUG_PRINT2 ("EXECUTING charset%s.\n", not ? "_not" : "");
5555 range_table_exists
= CHARSET_RANGE_TABLE_EXISTS_P (&p
[-1]);
5557 if (range_table_exists
)
5559 range_table
= CHARSET_RANGE_TABLE (&p
[-1]); /* Past the bitmap. */
5560 EXTRACT_NUMBER_AND_INCR (count
, range_table
);
5564 c
= RE_STRING_CHAR_AND_LENGTH (d
, len
, target_multibyte
);
5565 if (target_multibyte
)
5570 c1
= RE_CHAR_TO_UNIBYTE (c
);
5573 unibyte_char
= true;
5579 int c1
= RE_CHAR_TO_MULTIBYTE (c
);
5581 if (! CHAR_BYTE8_P (c1
))
5583 c1
= TRANSLATE (c1
);
5584 c1
= RE_CHAR_TO_UNIBYTE (c1
);
5587 unibyte_char
= true;
5592 unibyte_char
= true;
5595 if (unibyte_char
&& c
< (1 << BYTEWIDTH
))
5596 { /* Lookup bitmap. */
5597 /* Cast to `unsigned' instead of `unsigned char' in
5598 case the bit list is a full 32 bytes long. */
5599 if (c
< (unsigned) (CHARSET_BITMAP_SIZE (&p
[-1]) * BYTEWIDTH
)
5600 && p
[1 + c
/ BYTEWIDTH
] & (1 << (c
% BYTEWIDTH
)))
5604 else if (range_table_exists
)
5606 int class_bits
= CHARSET_RANGE_TABLE_BITS (&p
[-1]);
5608 if ( (class_bits
& BIT_LOWER
&& ISLOWER (c
))
5609 | (class_bits
& BIT_MULTIBYTE
)
5610 | (class_bits
& BIT_PUNCT
&& ISPUNCT (c
))
5611 | (class_bits
& BIT_SPACE
&& ISSPACE (c
))
5612 | (class_bits
& BIT_UPPER
&& ISUPPER (c
))
5613 | (class_bits
& BIT_WORD
&& ISWORD (c
)))
5616 CHARSET_LOOKUP_RANGE_TABLE_RAW (not, c
, range_table
, count
);
5620 if (range_table_exists
)
5621 p
= CHARSET_RANGE_TABLE_END (range_table
, count
);
5623 p
+= CHARSET_BITMAP_SIZE (&p
[-1]) + 1;
5625 if (!not) goto fail
;
5632 /* The beginning of a group is represented by start_memory.
5633 The argument is the register number. The text
5634 matched within the group is recorded (in the internal
5635 registers data structure) under the register number. */
5637 DEBUG_PRINT2 ("EXECUTING start_memory %d:\n", *p
);
5639 /* In case we need to undo this operation (via backtracking). */
5640 PUSH_FAILURE_REG ((unsigned int)*p
);
5643 regend
[*p
] = NULL
; /* probably unnecessary. -sm */
5644 DEBUG_PRINT2 (" regstart: %d\n", POINTER_TO_OFFSET (regstart
[*p
]));
5646 /* Move past the register number and inner group count. */
5651 /* The stop_memory opcode represents the end of a group. Its
5652 argument is the same as start_memory's: the register number. */
5654 DEBUG_PRINT2 ("EXECUTING stop_memory %d:\n", *p
);
5656 assert (!REG_UNSET (regstart
[*p
]));
5657 /* Strictly speaking, there should be code such as:
5659 assert (REG_UNSET (regend[*p]));
5660 PUSH_FAILURE_REGSTOP ((unsigned int)*p);
5662 But the only info to be pushed is regend[*p] and it is known to
5663 be UNSET, so there really isn't anything to push.
5664 Not pushing anything, on the other hand deprives us from the
5665 guarantee that regend[*p] is UNSET since undoing this operation
5666 will not reset its value properly. This is not important since
5667 the value will only be read on the next start_memory or at
5668 the very end and both events can only happen if this stop_memory
5672 DEBUG_PRINT2 (" regend: %d\n", POINTER_TO_OFFSET (regend
[*p
]));
5674 /* Move past the register number and the inner group count. */
5679 /* \<digit> has been turned into a `duplicate' command which is
5680 followed by the numeric value of <digit> as the register number. */
5683 register re_char
*d2
, *dend2
;
5684 int regno
= *p
++; /* Get which register to match against. */
5685 DEBUG_PRINT2 ("EXECUTING duplicate %d.\n", regno
);
5687 /* Can't back reference a group which we've never matched. */
5688 if (REG_UNSET (regstart
[regno
]) || REG_UNSET (regend
[regno
]))
5691 /* Where in input to try to start matching. */
5692 d2
= regstart
[regno
];
5694 /* Remember the start point to rollback upon failure. */
5697 /* Where to stop matching; if both the place to start and
5698 the place to stop matching are in the same string, then
5699 set to the place to stop, otherwise, for now have to use
5700 the end of the first string. */
5702 dend2
= ((FIRST_STRING_P (regstart
[regno
])
5703 == FIRST_STRING_P (regend
[regno
]))
5704 ? regend
[regno
] : end_match_1
);
5707 /* If necessary, advance to next segment in register
5711 if (dend2
== end_match_2
) break;
5712 if (dend2
== regend
[regno
]) break;
5714 /* End of string1 => advance to string2. */
5716 dend2
= regend
[regno
];
5718 /* At end of register contents => success */
5719 if (d2
== dend2
) break;
5721 /* If necessary, advance to next segment in data. */
5724 /* How many characters left in this segment to match. */
5727 /* Want how many consecutive characters we can match in
5728 one shot, so, if necessary, adjust the count. */
5729 if (mcnt
> dend2
- d2
)
5732 /* Compare that many; failure if mismatch, else move
5734 if (RE_TRANSLATE_P (translate
)
5735 ? bcmp_translate (d
, d2
, mcnt
, translate
, target_multibyte
)
5736 : memcmp (d
, d2
, mcnt
))
5741 d
+= mcnt
, d2
+= mcnt
;
5747 /* begline matches the empty string at the beginning of the string
5748 (unless `not_bol' is set in `bufp'), and after newlines. */
5750 DEBUG_PRINT1 ("EXECUTING begline.\n");
5752 if (AT_STRINGS_BEG (d
))
5754 if (!bufp
->not_bol
) break;
5759 GET_CHAR_BEFORE_2 (c
, d
, string1
, end1
, string2
, end2
);
5763 /* In all other cases, we fail. */
5767 /* endline is the dual of begline. */
5769 DEBUG_PRINT1 ("EXECUTING endline.\n");
5771 if (AT_STRINGS_END (d
))
5773 if (!bufp
->not_eol
) break;
5777 PREFETCH_NOLIMIT ();
5784 /* Match at the very beginning of the data. */
5786 DEBUG_PRINT1 ("EXECUTING begbuf.\n");
5787 if (AT_STRINGS_BEG (d
))
5792 /* Match at the very end of the data. */
5794 DEBUG_PRINT1 ("EXECUTING endbuf.\n");
5795 if (AT_STRINGS_END (d
))
5800 /* on_failure_keep_string_jump is used to optimize `.*\n'. It
5801 pushes NULL as the value for the string on the stack. Then
5802 `POP_FAILURE_POINT' will keep the current value for the
5803 string, instead of restoring it. To see why, consider
5804 matching `foo\nbar' against `.*\n'. The .* matches the foo;
5805 then the . fails against the \n. But the next thing we want
5806 to do is match the \n against the \n; if we restored the
5807 string value, we would be back at the foo.
5809 Because this is used only in specific cases, we don't need to
5810 check all the things that `on_failure_jump' does, to make
5811 sure the right things get saved on the stack. Hence we don't
5812 share its code. The only reason to push anything on the
5813 stack at all is that otherwise we would have to change
5814 `anychar's code to do something besides goto fail in this
5815 case; that seems worse than this. */
5816 case on_failure_keep_string_jump
:
5817 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5818 DEBUG_PRINT3 ("EXECUTING on_failure_keep_string_jump %d (to %p):\n",
5821 PUSH_FAILURE_POINT (p
- 3, NULL
);
5824 /* A nasty loop is introduced by the non-greedy *? and +?.
5825 With such loops, the stack only ever contains one failure point
5826 at a time, so that a plain on_failure_jump_loop kind of
5827 cycle detection cannot work. Worse yet, such a detection
5828 can not only fail to detect a cycle, but it can also wrongly
5829 detect a cycle (between different instantiations of the same
5831 So the method used for those nasty loops is a little different:
5832 We use a special cycle-detection-stack-frame which is pushed
5833 when the on_failure_jump_nastyloop failure-point is *popped*.
5834 This special frame thus marks the beginning of one iteration
5835 through the loop and we can hence easily check right here
5836 whether something matched between the beginning and the end of
5838 case on_failure_jump_nastyloop
:
5839 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5840 DEBUG_PRINT3 ("EXECUTING on_failure_jump_nastyloop %d (to %p):\n",
5843 assert ((re_opcode_t
)p
[-4] == no_op
);
5846 CHECK_INFINITE_LOOP (p
- 4, d
);
5848 /* If there's a cycle, just continue without pushing
5849 this failure point. The failure point is the "try again"
5850 option, which shouldn't be tried.
5851 We want (x?)*?y\1z to match both xxyz and xxyxz. */
5852 PUSH_FAILURE_POINT (p
- 3, d
);
5856 /* Simple loop detecting on_failure_jump: just check on the
5857 failure stack if the same spot was already hit earlier. */
5858 case on_failure_jump_loop
:
5860 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5861 DEBUG_PRINT3 ("EXECUTING on_failure_jump_loop %d (to %p):\n",
5865 CHECK_INFINITE_LOOP (p
- 3, d
);
5867 /* If there's a cycle, get out of the loop, as if the matching
5868 had failed. We used to just `goto fail' here, but that was
5869 aborting the search a bit too early: we want to keep the
5870 empty-loop-match and keep matching after the loop.
5871 We want (x?)*y\1z to match both xxyz and xxyxz. */
5874 PUSH_FAILURE_POINT (p
- 3, d
);
5879 /* Uses of on_failure_jump:
5881 Each alternative starts with an on_failure_jump that points
5882 to the beginning of the next alternative. Each alternative
5883 except the last ends with a jump that in effect jumps past
5884 the rest of the alternatives. (They really jump to the
5885 ending jump of the following alternative, because tensioning
5886 these jumps is a hassle.)
5888 Repeats start with an on_failure_jump that points past both
5889 the repetition text and either the following jump or
5890 pop_failure_jump back to this on_failure_jump. */
5891 case on_failure_jump
:
5892 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5893 DEBUG_PRINT3 ("EXECUTING on_failure_jump %d (to %p):\n",
5896 PUSH_FAILURE_POINT (p
-3, d
);
5899 /* This operation is used for greedy *.
5900 Compare the beginning of the repeat with what in the
5901 pattern follows its end. If we can establish that there
5902 is nothing that they would both match, i.e., that we
5903 would have to backtrack because of (as in, e.g., `a*a')
5904 then we can use a non-backtracking loop based on
5905 on_failure_keep_string_jump instead of on_failure_jump. */
5906 case on_failure_jump_smart
:
5907 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5908 DEBUG_PRINT3 ("EXECUTING on_failure_jump_smart %d (to %p).\n",
5911 re_char
*p1
= p
; /* Next operation. */
5912 /* Here, we discard `const', making re_match non-reentrant. */
5913 unsigned char *p2
= (unsigned char*) p
+ mcnt
; /* Jump dest. */
5914 unsigned char *p3
= (unsigned char*) p
- 3; /* opcode location. */
5916 p
-= 3; /* Reset so that we will re-execute the
5917 instruction once it's been changed. */
5919 EXTRACT_NUMBER (mcnt
, p2
- 2);
5921 /* Ensure this is a indeed the trivial kind of loop
5922 we are expecting. */
5923 assert (skip_one_char (p1
) == p2
- 3);
5924 assert ((re_opcode_t
) p2
[-3] == jump
&& p2
+ mcnt
== p
);
5925 DEBUG_STATEMENT (debug
+= 2);
5926 if (mutually_exclusive_p (bufp
, p1
, p2
))
5928 /* Use a fast `on_failure_keep_string_jump' loop. */
5929 DEBUG_PRINT1 (" smart exclusive => fast loop.\n");
5930 *p3
= (unsigned char) on_failure_keep_string_jump
;
5931 STORE_NUMBER (p2
- 2, mcnt
+ 3);
5935 /* Default to a safe `on_failure_jump' loop. */
5936 DEBUG_PRINT1 (" smart default => slow loop.\n");
5937 *p3
= (unsigned char) on_failure_jump
;
5939 DEBUG_STATEMENT (debug
-= 2);
5943 /* Unconditionally jump (without popping any failure points). */
5946 IMMEDIATE_QUIT_CHECK
;
5947 EXTRACT_NUMBER_AND_INCR (mcnt
, p
); /* Get the amount to jump. */
5948 DEBUG_PRINT2 ("EXECUTING jump %d ", mcnt
);
5949 p
+= mcnt
; /* Do the jump. */
5950 DEBUG_PRINT2 ("(to %p).\n", p
);
5954 /* Have to succeed matching what follows at least n times.
5955 After that, handle like `on_failure_jump'. */
5957 /* Signedness doesn't matter since we only compare MCNT to 0. */
5958 EXTRACT_NUMBER (mcnt
, p
+ 2);
5959 DEBUG_PRINT2 ("EXECUTING succeed_n %d.\n", mcnt
);
5961 /* Originally, mcnt is how many times we HAVE to succeed. */
5964 /* Here, we discard `const', making re_match non-reentrant. */
5965 unsigned char *p2
= (unsigned char*) p
+ 2; /* counter loc. */
5968 PUSH_NUMBER (p2
, mcnt
);
5971 /* The two bytes encoding mcnt == 0 are two no_op opcodes. */
5976 /* Signedness doesn't matter since we only compare MCNT to 0. */
5977 EXTRACT_NUMBER (mcnt
, p
+ 2);
5978 DEBUG_PRINT2 ("EXECUTING jump_n %d.\n", mcnt
);
5980 /* Originally, this is how many times we CAN jump. */
5983 /* Here, we discard `const', making re_match non-reentrant. */
5984 unsigned char *p2
= (unsigned char*) p
+ 2; /* counter loc. */
5986 PUSH_NUMBER (p2
, mcnt
);
5987 goto unconditional_jump
;
5989 /* If don't have to jump any more, skip over the rest of command. */
5996 unsigned char *p2
; /* Location of the counter. */
5997 DEBUG_PRINT1 ("EXECUTING set_number_at.\n");
5999 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
6000 /* Here, we discard `const', making re_match non-reentrant. */
6001 p2
= (unsigned char*) p
+ mcnt
;
6002 /* Signedness doesn't matter since we only copy MCNT's bits . */
6003 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
6004 DEBUG_PRINT3 (" Setting %p to %d.\n", p2
, mcnt
);
6005 PUSH_NUMBER (p2
, mcnt
);
6011 not = (re_opcode_t
) *(p
- 1) == notwordbound
;
6012 DEBUG_PRINT2 ("EXECUTING %swordbound.\n", not?"not":"");
6014 /* We SUCCEED (or FAIL) in one of the following cases: */
6016 /* Case 1: D is at the beginning or the end of string. */
6017 if (AT_STRINGS_BEG (d
) || AT_STRINGS_END (d
))
6021 /* C1 is the character before D, S1 is the syntax of C1, C2
6022 is the character at D, and S2 is the syntax of C2. */
6027 int offset
= PTR_TO_OFFSET (d
- 1);
6028 int charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
6029 UPDATE_SYNTAX_TABLE (charpos
);
6031 GET_CHAR_BEFORE_2 (c1
, d
, string1
, end1
, string2
, end2
);
6034 UPDATE_SYNTAX_TABLE_FORWARD (charpos
+ 1);
6036 PREFETCH_NOLIMIT ();
6037 GET_CHAR_AFTER (c2
, d
, dummy
);
6040 if (/* Case 2: Only one of S1 and S2 is Sword. */
6041 ((s1
== Sword
) != (s2
== Sword
))
6042 /* Case 3: Both of S1 and S2 are Sword, and macro
6043 WORD_BOUNDARY_P (C1, C2) returns nonzero. */
6044 || ((s1
== Sword
) && WORD_BOUNDARY_P (c1
, c2
)))
6053 DEBUG_PRINT1 ("EXECUTING wordbeg.\n");
6055 /* We FAIL in one of the following cases: */
6057 /* Case 1: D is at the end of string. */
6058 if (AT_STRINGS_END (d
))
6062 /* C1 is the character before D, S1 is the syntax of C1, C2
6063 is the character at D, and S2 is the syntax of C2. */
6068 int offset
= PTR_TO_OFFSET (d
);
6069 int charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
6070 UPDATE_SYNTAX_TABLE (charpos
);
6073 GET_CHAR_AFTER (c2
, d
, dummy
);
6076 /* Case 2: S2 is not Sword. */
6080 /* Case 3: D is not at the beginning of string ... */
6081 if (!AT_STRINGS_BEG (d
))
6083 GET_CHAR_BEFORE_2 (c1
, d
, string1
, end1
, string2
, end2
);
6085 UPDATE_SYNTAX_TABLE_BACKWARD (charpos
- 1);
6089 /* ... and S1 is Sword, and WORD_BOUNDARY_P (C1, C2)
6091 if ((s1
== Sword
) && !WORD_BOUNDARY_P (c1
, c2
))
6098 DEBUG_PRINT1 ("EXECUTING wordend.\n");
6100 /* We FAIL in one of the following cases: */
6102 /* Case 1: D is at the beginning of string. */
6103 if (AT_STRINGS_BEG (d
))
6107 /* C1 is the character before D, S1 is the syntax of C1, C2
6108 is the character at D, and S2 is the syntax of C2. */
6113 int offset
= PTR_TO_OFFSET (d
) - 1;
6114 int charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
6115 UPDATE_SYNTAX_TABLE (charpos
);
6117 GET_CHAR_BEFORE_2 (c1
, d
, string1
, end1
, string2
, end2
);
6120 /* Case 2: S1 is not Sword. */
6124 /* Case 3: D is not at the end of string ... */
6125 if (!AT_STRINGS_END (d
))
6127 PREFETCH_NOLIMIT ();
6128 GET_CHAR_AFTER (c2
, d
, dummy
);
6130 UPDATE_SYNTAX_TABLE_FORWARD (charpos
);
6134 /* ... and S2 is Sword, and WORD_BOUNDARY_P (C1, C2)
6136 if ((s2
== Sword
) && !WORD_BOUNDARY_P (c1
, c2
))
6143 DEBUG_PRINT1 ("EXECUTING symbeg.\n");
6145 /* We FAIL in one of the following cases: */
6147 /* Case 1: D is at the end of string. */
6148 if (AT_STRINGS_END (d
))
6152 /* C1 is the character before D, S1 is the syntax of C1, C2
6153 is the character at D, and S2 is the syntax of C2. */
6157 int offset
= PTR_TO_OFFSET (d
);
6158 int charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
6159 UPDATE_SYNTAX_TABLE (charpos
);
6162 c2
= RE_STRING_CHAR (d
, target_multibyte
);
6165 /* Case 2: S2 is neither Sword nor Ssymbol. */
6166 if (s2
!= Sword
&& s2
!= Ssymbol
)
6169 /* Case 3: D is not at the beginning of string ... */
6170 if (!AT_STRINGS_BEG (d
))
6172 GET_CHAR_BEFORE_2 (c1
, d
, string1
, end1
, string2
, end2
);
6174 UPDATE_SYNTAX_TABLE_BACKWARD (charpos
- 1);
6178 /* ... and S1 is Sword or Ssymbol. */
6179 if (s1
== Sword
|| s1
== Ssymbol
)
6186 DEBUG_PRINT1 ("EXECUTING symend.\n");
6188 /* We FAIL in one of the following cases: */
6190 /* Case 1: D is at the beginning of string. */
6191 if (AT_STRINGS_BEG (d
))
6195 /* C1 is the character before D, S1 is the syntax of C1, C2
6196 is the character at D, and S2 is the syntax of C2. */
6200 int offset
= PTR_TO_OFFSET (d
) - 1;
6201 int charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
6202 UPDATE_SYNTAX_TABLE (charpos
);
6204 GET_CHAR_BEFORE_2 (c1
, d
, string1
, end1
, string2
, end2
);
6207 /* Case 2: S1 is neither Ssymbol nor Sword. */
6208 if (s1
!= Sword
&& s1
!= Ssymbol
)
6211 /* Case 3: D is not at the end of string ... */
6212 if (!AT_STRINGS_END (d
))
6214 PREFETCH_NOLIMIT ();
6215 c2
= RE_STRING_CHAR (d
, target_multibyte
);
6217 UPDATE_SYNTAX_TABLE_FORWARD (charpos
+ 1);
6221 /* ... and S2 is Sword or Ssymbol. */
6222 if (s2
== Sword
|| s2
== Ssymbol
)
6230 not = (re_opcode_t
) *(p
- 1) == notsyntaxspec
;
6232 DEBUG_PRINT3 ("EXECUTING %ssyntaxspec %d.\n", not?"not":"", mcnt
);
6236 int offset
= PTR_TO_OFFSET (d
);
6237 int pos1
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
6238 UPDATE_SYNTAX_TABLE (pos1
);
6245 GET_CHAR_AFTER (c
, d
, len
);
6246 if ((SYNTAX (c
) != (enum syntaxcode
) mcnt
) ^ not)
6254 DEBUG_PRINT1 ("EXECUTING before_dot.\n");
6255 if (PTR_BYTE_POS (d
) >= PT_BYTE
)
6260 DEBUG_PRINT1 ("EXECUTING at_dot.\n");
6261 if (PTR_BYTE_POS (d
) != PT_BYTE
)
6266 DEBUG_PRINT1 ("EXECUTING after_dot.\n");
6267 if (PTR_BYTE_POS (d
) <= PT_BYTE
)
6272 case notcategoryspec
:
6273 not = (re_opcode_t
) *(p
- 1) == notcategoryspec
;
6275 DEBUG_PRINT3 ("EXECUTING %scategoryspec %d.\n", not?"not":"", mcnt
);
6281 GET_CHAR_AFTER (c
, d
, len
);
6282 if ((!CHAR_HAS_CATEGORY (c
, mcnt
)) ^ not)
6293 continue; /* Successfully executed one pattern command; keep going. */
6296 /* We goto here if a matching operation fails. */
6298 IMMEDIATE_QUIT_CHECK
;
6299 if (!FAIL_STACK_EMPTY ())
6302 /* A restart point is known. Restore to that state. */
6303 DEBUG_PRINT1 ("\nFAIL:\n");
6304 POP_FAILURE_POINT (str
, pat
);
6305 switch (SWITCH_ENUM_CAST ((re_opcode_t
) *pat
++))
6307 case on_failure_keep_string_jump
:
6308 assert (str
== NULL
);
6309 goto continue_failure_jump
;
6311 case on_failure_jump_nastyloop
:
6312 assert ((re_opcode_t
)pat
[-2] == no_op
);
6313 PUSH_FAILURE_POINT (pat
- 2, str
);
6316 case on_failure_jump_loop
:
6317 case on_failure_jump
:
6320 continue_failure_jump
:
6321 EXTRACT_NUMBER_AND_INCR (mcnt
, pat
);
6326 /* A special frame used for nastyloops. */
6333 assert (p
>= bufp
->buffer
&& p
<= pend
);
6335 if (d
>= string1
&& d
<= end1
)
6339 break; /* Matching at this starting point really fails. */
6343 goto restore_best_regs
;
6347 return -1; /* Failure to match. */
6350 /* Subroutine definitions for re_match_2. */
6352 /* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
6353 bytes; nonzero otherwise. */
6356 bcmp_translate (const re_char
*s1
, const re_char
*s2
, register int len
,
6357 RE_TRANSLATE_TYPE translate
, const int target_multibyte
)
6359 register re_char
*p1
= s1
, *p2
= s2
;
6360 re_char
*p1_end
= s1
+ len
;
6361 re_char
*p2_end
= s2
+ len
;
6363 /* FIXME: Checking both p1 and p2 presumes that the two strings might have
6364 different lengths, but relying on a single `len' would break this. -sm */
6365 while (p1
< p1_end
&& p2
< p2_end
)
6367 int p1_charlen
, p2_charlen
;
6368 re_wchar_t p1_ch
, p2_ch
;
6370 GET_CHAR_AFTER (p1_ch
, p1
, p1_charlen
);
6371 GET_CHAR_AFTER (p2_ch
, p2
, p2_charlen
);
6373 if (RE_TRANSLATE (translate
, p1_ch
)
6374 != RE_TRANSLATE (translate
, p2_ch
))
6377 p1
+= p1_charlen
, p2
+= p2_charlen
;
6380 if (p1
!= p1_end
|| p2
!= p2_end
)
6386 /* Entry points for GNU code. */
6388 /* re_compile_pattern is the GNU regular expression compiler: it
6389 compiles PATTERN (of length SIZE) and puts the result in BUFP.
6390 Returns 0 if the pattern was valid, otherwise an error string.
6392 Assumes the `allocated' (and perhaps `buffer') and `translate' fields
6393 are set in BUFP on entry.
6395 We call regex_compile to do the actual compilation. */
6398 re_compile_pattern (const char *pattern
, size_t length
, struct re_pattern_buffer
*bufp
)
6402 /* GNU code is written to assume at least RE_NREGS registers will be set
6403 (and at least one extra will be -1). */
6404 bufp
->regs_allocated
= REGS_UNALLOCATED
;
6406 /* And GNU code determines whether or not to get register information
6407 by passing null for the REGS argument to re_match, etc., not by
6411 ret
= regex_compile ((re_char
*) pattern
, length
, re_syntax_options
, bufp
);
6415 return gettext (re_error_msgid
[(int) ret
]);
6417 WEAK_ALIAS (__re_compile_pattern
, re_compile_pattern
)
6419 /* Entry points compatible with 4.2 BSD regex library. We don't define
6420 them unless specifically requested. */
6422 #if defined _REGEX_RE_COMP || defined _LIBC
6424 /* BSD has one and only one pattern buffer. */
6425 static struct re_pattern_buffer re_comp_buf
;
6429 /* Make these definitions weak in libc, so POSIX programs can redefine
6430 these names if they don't use our functions, and still use
6431 regcomp/regexec below without link errors. */
6441 if (!re_comp_buf
.buffer
)
6442 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6443 return (char *) gettext ("No previous regular expression");
6447 if (!re_comp_buf
.buffer
)
6449 re_comp_buf
.buffer
= (unsigned char *) malloc (200);
6450 if (re_comp_buf
.buffer
== NULL
)
6451 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6452 return (char *) gettext (re_error_msgid
[(int) REG_ESPACE
]);
6453 re_comp_buf
.allocated
= 200;
6455 re_comp_buf
.fastmap
= (char *) malloc (1 << BYTEWIDTH
);
6456 if (re_comp_buf
.fastmap
== NULL
)
6457 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6458 return (char *) gettext (re_error_msgid
[(int) REG_ESPACE
]);
6461 /* Since `re_exec' always passes NULL for the `regs' argument, we
6462 don't need to initialize the pattern buffer fields which affect it. */
6464 ret
= regex_compile (s
, strlen (s
), re_syntax_options
, &re_comp_buf
);
6469 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6470 return (char *) gettext (re_error_msgid
[(int) ret
]);
6481 const int len
= strlen (s
);
6483 0 <= re_search (&re_comp_buf
, s
, len
, 0, len
, (struct re_registers
*) 0);
6485 #endif /* _REGEX_RE_COMP */
6487 /* POSIX.2 functions. Don't define these for Emacs. */
6491 /* regcomp takes a regular expression as a string and compiles it.
6493 PREG is a regex_t *. We do not expect any fields to be initialized,
6494 since POSIX says we shouldn't. Thus, we set
6496 `buffer' to the compiled pattern;
6497 `used' to the length of the compiled pattern;
6498 `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
6499 REG_EXTENDED bit in CFLAGS is set; otherwise, to
6500 RE_SYNTAX_POSIX_BASIC;
6501 `fastmap' to an allocated space for the fastmap;
6502 `fastmap_accurate' to zero;
6503 `re_nsub' to the number of subexpressions in PATTERN.
6505 PATTERN is the address of the pattern string.
6507 CFLAGS is a series of bits which affect compilation.
6509 If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
6510 use POSIX basic syntax.
6512 If REG_NEWLINE is set, then . and [^...] don't match newline.
6513 Also, regexec will try a match beginning after every newline.
6515 If REG_ICASE is set, then we considers upper- and lowercase
6516 versions of letters to be equivalent when matching.
6518 If REG_NOSUB is set, then when PREG is passed to regexec, that
6519 routine will report only success or failure, and nothing about the
6522 It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
6523 the return codes and their meanings.) */
6526 regcomp (regex_t
*__restrict preg
, const char *__restrict pattern
,
6531 = (cflags
& REG_EXTENDED
) ?
6532 RE_SYNTAX_POSIX_EXTENDED
: RE_SYNTAX_POSIX_BASIC
;
6534 /* regex_compile will allocate the space for the compiled pattern. */
6536 preg
->allocated
= 0;
6539 /* Try to allocate space for the fastmap. */
6540 preg
->fastmap
= (char *) malloc (1 << BYTEWIDTH
);
6542 if (cflags
& REG_ICASE
)
6547 = (RE_TRANSLATE_TYPE
) malloc (CHAR_SET_SIZE
6548 * sizeof (*(RE_TRANSLATE_TYPE
)0));
6549 if (preg
->translate
== NULL
)
6550 return (int) REG_ESPACE
;
6552 /* Map uppercase characters to corresponding lowercase ones. */
6553 for (i
= 0; i
< CHAR_SET_SIZE
; i
++)
6554 preg
->translate
[i
] = ISUPPER (i
) ? TOLOWER (i
) : i
;
6557 preg
->translate
= NULL
;
6559 /* If REG_NEWLINE is set, newlines are treated differently. */
6560 if (cflags
& REG_NEWLINE
)
6561 { /* REG_NEWLINE implies neither . nor [^...] match newline. */
6562 syntax
&= ~RE_DOT_NEWLINE
;
6563 syntax
|= RE_HAT_LISTS_NOT_NEWLINE
;
6566 syntax
|= RE_NO_NEWLINE_ANCHOR
;
6568 preg
->no_sub
= !!(cflags
& REG_NOSUB
);
6570 /* POSIX says a null character in the pattern terminates it, so we
6571 can use strlen here in compiling the pattern. */
6572 ret
= regex_compile ((re_char
*) pattern
, strlen (pattern
), syntax
, preg
);
6574 /* POSIX doesn't distinguish between an unmatched open-group and an
6575 unmatched close-group: both are REG_EPAREN. */
6576 if (ret
== REG_ERPAREN
)
6579 if (ret
== REG_NOERROR
&& preg
->fastmap
)
6580 { /* Compute the fastmap now, since regexec cannot modify the pattern
6582 re_compile_fastmap (preg
);
6583 if (preg
->can_be_null
)
6584 { /* The fastmap can't be used anyway. */
6585 free (preg
->fastmap
);
6586 preg
->fastmap
= NULL
;
6591 WEAK_ALIAS (__regcomp
, regcomp
)
6594 /* regexec searches for a given pattern, specified by PREG, in the
6597 If NMATCH is zero or REG_NOSUB was set in the cflags argument to
6598 `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
6599 least NMATCH elements, and we set them to the offsets of the
6600 corresponding matched substrings.
6602 EFLAGS specifies `execution flags' which affect matching: if
6603 REG_NOTBOL is set, then ^ does not match at the beginning of the
6604 string; if REG_NOTEOL is set, then $ does not match at the end.
6606 We return 0 if we find a match and REG_NOMATCH if not. */
6609 regexec (const regex_t
*__restrict preg
, const char *__restrict string
,
6610 size_t nmatch
, regmatch_t pmatch
[__restrict_arr
], int eflags
)
6613 struct re_registers regs
;
6614 regex_t private_preg
;
6615 int len
= strlen (string
);
6616 boolean want_reg_info
= !preg
->no_sub
&& nmatch
> 0 && pmatch
;
6618 private_preg
= *preg
;
6620 private_preg
.not_bol
= !!(eflags
& REG_NOTBOL
);
6621 private_preg
.not_eol
= !!(eflags
& REG_NOTEOL
);
6623 /* The user has told us exactly how many registers to return
6624 information about, via `nmatch'. We have to pass that on to the
6625 matching routines. */
6626 private_preg
.regs_allocated
= REGS_FIXED
;
6630 regs
.num_regs
= nmatch
;
6631 regs
.start
= TALLOC (nmatch
* 2, regoff_t
);
6632 if (regs
.start
== NULL
)
6633 return (int) REG_NOMATCH
;
6634 regs
.end
= regs
.start
+ nmatch
;
6637 /* Instead of using not_eol to implement REG_NOTEOL, we could simply
6638 pass (&private_preg, string, len + 1, 0, len, ...) pretending the string
6639 was a little bit longer but still only matching the real part.
6640 This works because the `endline' will check for a '\n' and will find a
6641 '\0', correctly deciding that this is not the end of a line.
6642 But it doesn't work out so nicely for REG_NOTBOL, since we don't have
6643 a convenient '\0' there. For all we know, the string could be preceded
6644 by '\n' which would throw things off. */
6646 /* Perform the searching operation. */
6647 ret
= re_search (&private_preg
, string
, len
,
6648 /* start: */ 0, /* range: */ len
,
6649 want_reg_info
? ®s
: (struct re_registers
*) 0);
6651 /* Copy the register information to the POSIX structure. */
6658 for (r
= 0; r
< nmatch
; r
++)
6660 pmatch
[r
].rm_so
= regs
.start
[r
];
6661 pmatch
[r
].rm_eo
= regs
.end
[r
];
6665 /* If we needed the temporary register info, free the space now. */
6669 /* We want zero return to mean success, unlike `re_search'. */
6670 return ret
>= 0 ? (int) REG_NOERROR
: (int) REG_NOMATCH
;
6672 WEAK_ALIAS (__regexec
, regexec
)
6675 /* Returns a message corresponding to an error code, ERR_CODE, returned
6676 from either regcomp or regexec. We don't use PREG here.
6678 ERR_CODE was previously called ERRCODE, but that name causes an
6679 error with msvc8 compiler. */
6682 regerror (int err_code
, const regex_t
*preg
, char *errbuf
, size_t errbuf_size
)
6688 || err_code
>= (sizeof (re_error_msgid
) / sizeof (re_error_msgid
[0])))
6689 /* Only error codes returned by the rest of the code should be passed
6690 to this routine. If we are given anything else, or if other regex
6691 code generates an invalid error code, then the program has a bug.
6692 Dump core so we can fix it. */
6695 msg
= gettext (re_error_msgid
[err_code
]);
6697 msg_size
= strlen (msg
) + 1; /* Includes the null. */
6699 if (errbuf_size
!= 0)
6701 if (msg_size
> errbuf_size
)
6703 strncpy (errbuf
, msg
, errbuf_size
- 1);
6704 errbuf
[errbuf_size
- 1] = 0;
6707 strcpy (errbuf
, msg
);
6712 WEAK_ALIAS (__regerror
, regerror
)
6715 /* Free dynamically allocated space used by PREG. */
6718 regfree (regex_t
*preg
)
6720 free (preg
->buffer
);
6721 preg
->buffer
= NULL
;
6723 preg
->allocated
= 0;
6726 free (preg
->fastmap
);
6727 preg
->fastmap
= NULL
;
6728 preg
->fastmap_accurate
= 0;
6730 free (preg
->translate
);
6731 preg
->translate
= NULL
;
6733 WEAK_ALIAS (__regfree
, regfree
)
6735 #endif /* not emacs */