1 /* Extended regular expression matching and search library, version
2 0.12. (Implements POSIX draft P1003.2/D11.2, except for some of the
3 internationalization features.)
5 Copyright (C) 1993,94,95,96,97,98,99,2000,04 Free Software Foundation, Inc.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
23 - structure the opcode space into opcode+flag.
24 - merge with glibc's regex.[ch].
25 - replace (succeed_n + jump_n + set_number_at) with something that doesn't
26 need to modify the compiled regexp so that re_match can be reentrant.
27 - get rid of on_failure_jump_smart by doing the optimization in re_comp
28 rather than at run-time, so that re_match can be reentrant.
31 /* AIX requires this to be the first thing in the file. */
32 #if defined _AIX && !defined REGEX_MALLOC
40 #if defined STDC_HEADERS && !defined emacs
43 /* We need this for `regex.h', and perhaps for the Emacs include files. */
44 # include <sys/types.h>
47 /* Whether to use ISO C Amendment 1 wide char functions.
48 Those should not be used for Emacs since it uses its own. */
50 #define WIDE_CHAR_SUPPORT 1
52 #define WIDE_CHAR_SUPPORT \
53 (HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_BTOWC && !emacs)
56 /* For platform which support the ISO C amendement 1 functionality we
57 support user defined character classes. */
59 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
65 /* We have to keep the namespace clean. */
66 # define regfree(preg) __regfree (preg)
67 # define regexec(pr, st, nm, pm, ef) __regexec (pr, st, nm, pm, ef)
68 # define regcomp(preg, pattern, cflags) __regcomp (preg, pattern, cflags)
69 # define regerror(errcode, preg, errbuf, errbuf_size) \
70 __regerror(errcode, preg, errbuf, errbuf_size)
71 # define re_set_registers(bu, re, nu, st, en) \
72 __re_set_registers (bu, re, nu, st, en)
73 # define re_match_2(bufp, string1, size1, string2, size2, pos, regs, stop) \
74 __re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
75 # define re_match(bufp, string, size, pos, regs) \
76 __re_match (bufp, string, size, pos, regs)
77 # define re_search(bufp, string, size, startpos, range, regs) \
78 __re_search (bufp, string, size, startpos, range, regs)
79 # define re_compile_pattern(pattern, length, bufp) \
80 __re_compile_pattern (pattern, length, bufp)
81 # define re_set_syntax(syntax) __re_set_syntax (syntax)
82 # define re_search_2(bufp, st1, s1, st2, s2, startpos, range, regs, stop) \
83 __re_search_2 (bufp, st1, s1, st2, s2, startpos, range, regs, stop)
84 # define re_compile_fastmap(bufp) __re_compile_fastmap (bufp)
86 /* Make sure we call libc's function even if the user overrides them. */
87 # define btowc __btowc
88 # define iswctype __iswctype
89 # define wctype __wctype
91 # define WEAK_ALIAS(a,b) weak_alias (a, b)
93 /* We are also using some library internals. */
94 # include <locale/localeinfo.h>
95 # include <locale/elem-hash.h>
96 # include <langinfo.h>
98 # define WEAK_ALIAS(a,b)
101 /* This is for other GNU distributions with internationalized messages. */
102 #if HAVE_LIBINTL_H || defined _LIBC
103 # include <libintl.h>
105 # define gettext(msgid) (msgid)
109 /* This define is so xgettext can find the internationalizable
111 # define gettext_noop(String) String
114 /* The `emacs' switch turns on certain matching commands
115 that make sense only in Emacs. */
121 /* Make syntax table lookup grant data in gl_state. */
122 # define SYNTAX_ENTRY_VIA_PROPERTY
125 # include "character.h"
126 # include "category.h"
131 # define malloc xmalloc
135 # define realloc xrealloc
141 /* Converts the pointer to the char to BEG-based offset from the start. */
142 # define PTR_TO_OFFSET(d) POS_AS_IN_BUFFER (POINTER_TO_OFFSET (d))
143 # define POS_AS_IN_BUFFER(p) ((p) + (NILP (re_match_object) || BUFFERP (re_match_object)))
145 # define RE_MULTIBYTE_P(bufp) ((bufp)->multibyte)
146 # define RE_TARGET_MULTIBYTE_P(bufp) ((bufp)->target_multibyte)
147 # define RE_STRING_CHAR(p, s) \
148 (multibyte ? (STRING_CHAR (p, s)) : (*(p)))
149 # define RE_STRING_CHAR_AND_LENGTH(p, s, len) \
150 (multibyte ? (STRING_CHAR_AND_LENGTH (p, s, len)) : ((len) = 1, *(p)))
152 /* Set C a (possibly converted to multibyte) character before P. P
153 points into a string which is the virtual concatenation of STR1
154 (which ends at END1) or STR2 (which ends at END2). */
155 # define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2) \
159 re_char *dtemp = (p) == (str2) ? (end1) : (p); \
160 re_char *dlimit = ((p) > (str2) && (p) <= (end2)) ? (str2) : (str1); \
161 while (dtemp-- > dlimit && !CHAR_HEAD_P (*dtemp)); \
162 c = STRING_CHAR (dtemp, (p) - dtemp); \
166 (c = ((p) == (str2) ? (end1) : (p))[-1]); \
167 MAKE_CHAR_MULTIBYTE (c); \
171 /* Set C a (possibly converted to multibyte) character at P, and set
172 LEN to the byte length of that character. */
173 # define GET_CHAR_AFTER(c, p, len) \
176 c = STRING_CHAR_AND_LENGTH (p, 0, len); \
181 MAKE_CHAR_MULTIBYTE (c); \
185 #else /* not emacs */
187 /* If we are not linking with Emacs proper,
188 we can't use the relocating allocator
189 even if config.h says that we can. */
192 # if defined STDC_HEADERS || defined _LIBC
199 /* When used in Emacs's lib-src, we need to get bzero and bcopy somehow.
200 If nothing else has been done, use the method below. */
201 # ifdef INHIBIT_STRING_HEADER
202 # if !(defined HAVE_BZERO && defined HAVE_BCOPY)
203 # if !defined bzero && !defined bcopy
204 # undef INHIBIT_STRING_HEADER
209 /* This is the normal way of making sure we have memcpy, memcmp and bzero.
210 This is used in most programs--a few other programs avoid this
211 by defining INHIBIT_STRING_HEADER. */
212 # ifndef INHIBIT_STRING_HEADER
213 # if defined HAVE_STRING_H || defined STDC_HEADERS || defined _LIBC
217 # define bzero(s, n) (memset (s, '\0', n), (s))
219 # define bzero(s, n) __bzero (s, n)
223 # include <strings.h>
225 # define memcmp(s1, s2, n) bcmp (s1, s2, n)
228 # define memcpy(d, s, n) (bcopy (s, d, n), (d))
233 /* Define the syntax stuff for \<, \>, etc. */
235 /* Sword must be nonzero for the wordchar pattern commands in re_match_2. */
236 enum syntaxcode
{ Swhitespace
= 0, Sword
= 1, Ssymbol
= 2 };
238 # ifdef SWITCH_ENUM_BUG
239 # define SWITCH_ENUM_CAST(x) ((int)(x))
241 # define SWITCH_ENUM_CAST(x) (x)
244 /* Dummy macros for non-Emacs environments. */
245 # define BASE_LEADING_CODE_P(c) (0)
246 # define CHAR_CHARSET(c) 0
247 # define CHARSET_LEADING_CODE_BASE(c) 0
248 # define MAX_MULTIBYTE_LENGTH 1
249 # define RE_MULTIBYTE_P(x) 0
250 # define RE_TARGET_MULTIBYTE_P(x) 0
251 # define WORD_BOUNDARY_P(c1, c2) (0)
252 # define CHAR_HEAD_P(p) (1)
253 # define SINGLE_BYTE_CHAR_P(c) (1)
254 # define SAME_CHARSET_P(c1, c2) (1)
255 # define MULTIBYTE_FORM_LENGTH(p, s) (1)
256 # define PREV_CHAR_BOUNDARY(p, limit) ((p)--)
257 # define STRING_CHAR(p, s) (*(p))
258 # define RE_STRING_CHAR STRING_CHAR
259 # define CHAR_STRING(c, s) (*(s) = (c), 1)
260 # define STRING_CHAR_AND_LENGTH(p, s, actual_len) ((actual_len) = 1, *(p))
261 # define RE_STRING_CHAR_AND_LENGTH STRING_CHAR_AND_LENGTH
262 # define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2) \
263 (c = ((p) == (str2) ? *((end1) - 1) : *((p) - 1)))
264 # define GET_CHAR_AFTER(c, p, len) \
266 # define MAKE_CHAR(charset, c1, c2) (c1)
267 # define BYTE8_TO_CHAR(c) (c)
268 # define CHAR_BYTE8_P(c) (0)
269 # define MAKE_CHAR_MULTIBYTE(c) (c)
270 # define MAKE_CHAR_UNIBYTE(c) (c)
271 # define CHAR_LEADING_CODE(c) (c)
273 #endif /* not emacs */
276 # define RE_TRANSLATE(TBL, C) ((unsigned char)(TBL)[C])
277 # define RE_TRANSLATE_P(TBL) (TBL)
280 /* Get the interface, including the syntax bits. */
283 /* isalpha etc. are used for the character classes. */
288 /* 1 if C is an ASCII character. */
289 # define IS_REAL_ASCII(c) ((c) < 0200)
291 /* 1 if C is a unibyte character. */
292 # define ISUNIBYTE(c) (SINGLE_BYTE_CHAR_P ((c)))
294 /* The Emacs definitions should not be directly affected by locales. */
296 /* In Emacs, these are only used for single-byte characters. */
297 # define ISDIGIT(c) ((c) >= '0' && (c) <= '9')
298 # define ISCNTRL(c) ((c) < ' ')
299 # define ISXDIGIT(c) (((c) >= '0' && (c) <= '9') \
300 || ((c) >= 'a' && (c) <= 'f') \
301 || ((c) >= 'A' && (c) <= 'F'))
303 /* This is only used for single-byte characters. */
304 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
306 /* The rest must handle multibyte characters. */
308 # define ISGRAPH(c) (SINGLE_BYTE_CHAR_P (c) \
309 ? (c) > ' ' && !((c) >= 0177 && (c) <= 0237) \
312 # define ISPRINT(c) (SINGLE_BYTE_CHAR_P (c) \
313 ? (c) >= ' ' && !((c) >= 0177 && (c) <= 0237) \
316 # define ISALNUM(c) (IS_REAL_ASCII (c) \
317 ? (((c) >= 'a' && (c) <= 'z') \
318 || ((c) >= 'A' && (c) <= 'Z') \
319 || ((c) >= '0' && (c) <= '9')) \
320 : SYNTAX (c) == Sword)
322 # define ISALPHA(c) (IS_REAL_ASCII (c) \
323 ? (((c) >= 'a' && (c) <= 'z') \
324 || ((c) >= 'A' && (c) <= 'Z')) \
325 : SYNTAX (c) == Sword)
327 # define ISLOWER(c) (LOWERCASEP (c))
329 # define ISPUNCT(c) (IS_REAL_ASCII (c) \
330 ? ((c) > ' ' && (c) < 0177 \
331 && !(((c) >= 'a' && (c) <= 'z') \
332 || ((c) >= 'A' && (c) <= 'Z') \
333 || ((c) >= '0' && (c) <= '9'))) \
334 : SYNTAX (c) != Sword)
336 # define ISSPACE(c) (SYNTAX (c) == Swhitespace)
338 # define ISUPPER(c) (UPPERCASEP (c))
340 # define ISWORD(c) (SYNTAX (c) == Sword)
342 #else /* not emacs */
344 /* Jim Meyering writes:
346 "... Some ctype macros are valid only for character codes that
347 isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
348 using /bin/cc or gcc but without giving an ansi option). So, all
349 ctype uses should be through macros like ISPRINT... If
350 STDC_HEADERS is defined, then autoconf has verified that the ctype
351 macros don't need to be guarded with references to isascii. ...
352 Defining isascii to 1 should let any compiler worth its salt
353 eliminate the && through constant folding."
354 Solaris defines some of these symbols so we must undefine them first. */
357 # if defined STDC_HEADERS || (!defined isascii && !defined HAVE_ISASCII)
358 # define ISASCII(c) 1
360 # define ISASCII(c) isascii(c)
363 /* 1 if C is an ASCII character. */
364 # define IS_REAL_ASCII(c) ((c) < 0200)
366 /* This distinction is not meaningful, except in Emacs. */
367 # define ISUNIBYTE(c) 1
370 # define ISBLANK(c) (ISASCII (c) && isblank (c))
372 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
375 # define ISGRAPH(c) (ISASCII (c) && isgraph (c))
377 # define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
381 # define ISPRINT(c) (ISASCII (c) && isprint (c))
382 # define ISDIGIT(c) (ISASCII (c) && isdigit (c))
383 # define ISALNUM(c) (ISASCII (c) && isalnum (c))
384 # define ISALPHA(c) (ISASCII (c) && isalpha (c))
385 # define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
386 # define ISLOWER(c) (ISASCII (c) && islower (c))
387 # define ISPUNCT(c) (ISASCII (c) && ispunct (c))
388 # define ISSPACE(c) (ISASCII (c) && isspace (c))
389 # define ISUPPER(c) (ISASCII (c) && isupper (c))
390 # define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
392 # define ISWORD(c) ISALPHA(c)
395 # define TOLOWER(c) _tolower(c)
397 # define TOLOWER(c) tolower(c)
400 /* How many characters in the character set. */
401 # define CHAR_SET_SIZE 256
405 extern char *re_syntax_table
;
407 # else /* not SYNTAX_TABLE */
409 static char re_syntax_table
[CHAR_SET_SIZE
];
420 bzero (re_syntax_table
, sizeof re_syntax_table
);
422 for (c
= 0; c
< CHAR_SET_SIZE
; ++c
)
424 re_syntax_table
[c
] = Sword
;
426 re_syntax_table
['_'] = Ssymbol
;
431 # endif /* not SYNTAX_TABLE */
433 # define SYNTAX(c) re_syntax_table[(c)]
435 #endif /* not emacs */
438 # define NULL (void *)0
441 /* We remove any previous definition of `SIGN_EXTEND_CHAR',
442 since ours (we hope) works properly with all combinations of
443 machines, compilers, `char' and `unsigned char' argument types.
444 (Per Bothner suggested the basic approach.) */
445 #undef SIGN_EXTEND_CHAR
447 # define SIGN_EXTEND_CHAR(c) ((signed char) (c))
448 #else /* not __STDC__ */
449 /* As in Harbison and Steele. */
450 # define SIGN_EXTEND_CHAR(c) ((((unsigned char) (c)) ^ 128) - 128)
453 /* Should we use malloc or alloca? If REGEX_MALLOC is not defined, we
454 use `alloca' instead of `malloc'. This is because using malloc in
455 re_search* or re_match* could cause memory leaks when C-g is used in
456 Emacs; also, malloc is slower and causes storage fragmentation. On
457 the other hand, malloc is more portable, and easier to debug.
459 Because we sometimes use alloca, some routines have to be macros,
460 not functions -- `alloca'-allocated space disappears at the end of the
461 function it is called in. */
465 # define REGEX_ALLOCATE malloc
466 # define REGEX_REALLOCATE(source, osize, nsize) realloc (source, nsize)
467 # define REGEX_FREE free
469 #else /* not REGEX_MALLOC */
471 /* Emacs already defines alloca, sometimes. */
474 /* Make alloca work the best possible way. */
476 # define alloca __builtin_alloca
477 # else /* not __GNUC__ */
478 # ifdef HAVE_ALLOCA_H
480 # endif /* HAVE_ALLOCA_H */
481 # endif /* not __GNUC__ */
483 # endif /* not alloca */
485 # define REGEX_ALLOCATE alloca
487 /* Assumes a `char *destination' variable. */
488 # define REGEX_REALLOCATE(source, osize, nsize) \
489 (destination = (char *) alloca (nsize), \
490 memcpy (destination, source, osize))
492 /* No need to do anything to free, after alloca. */
493 # define REGEX_FREE(arg) ((void)0) /* Do nothing! But inhibit gcc warning. */
495 #endif /* not REGEX_MALLOC */
497 /* Define how to allocate the failure stack. */
499 #if defined REL_ALLOC && defined REGEX_MALLOC
501 # define REGEX_ALLOCATE_STACK(size) \
502 r_alloc (&failure_stack_ptr, (size))
503 # define REGEX_REALLOCATE_STACK(source, osize, nsize) \
504 r_re_alloc (&failure_stack_ptr, (nsize))
505 # define REGEX_FREE_STACK(ptr) \
506 r_alloc_free (&failure_stack_ptr)
508 #else /* not using relocating allocator */
512 # define REGEX_ALLOCATE_STACK malloc
513 # define REGEX_REALLOCATE_STACK(source, osize, nsize) realloc (source, nsize)
514 # define REGEX_FREE_STACK free
516 # else /* not REGEX_MALLOC */
518 # define REGEX_ALLOCATE_STACK alloca
520 # define REGEX_REALLOCATE_STACK(source, osize, nsize) \
521 REGEX_REALLOCATE (source, osize, nsize)
522 /* No need to explicitly free anything. */
523 # define REGEX_FREE_STACK(arg) ((void)0)
525 # endif /* not REGEX_MALLOC */
526 #endif /* not using relocating allocator */
529 /* True if `size1' is non-NULL and PTR is pointing anywhere inside
530 `string1' or just past its end. This works if PTR is NULL, which is
532 #define FIRST_STRING_P(ptr) \
533 (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
535 /* (Re)Allocate N items of type T using malloc, or fail. */
536 #define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t)))
537 #define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
538 #define RETALLOC_IF(addr, n, t) \
539 if (addr) RETALLOC((addr), (n), t); else (addr) = TALLOC ((n), t)
540 #define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
542 #define BYTEWIDTH 8 /* In bits. */
544 #define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
548 #define MAX(a, b) ((a) > (b) ? (a) : (b))
549 #define MIN(a, b) ((a) < (b) ? (a) : (b))
551 /* Type of source-pattern and string chars. */
552 typedef const unsigned char re_char
;
554 typedef char boolean
;
558 static int re_match_2_internal
_RE_ARGS ((struct re_pattern_buffer
*bufp
,
559 re_char
*string1
, int size1
,
560 re_char
*string2
, int size2
,
562 struct re_registers
*regs
,
565 /* These are the command codes that appear in compiled regular
566 expressions. Some opcodes are followed by argument bytes. A
567 command code can specify any interpretation whatsoever for its
568 arguments. Zero bytes may appear in the compiled regular expression. */
574 /* Succeed right away--no more backtracking. */
577 /* Followed by one byte giving n, then by n literal bytes. */
580 /* Matches any (more or less) character. */
583 /* Matches any one char belonging to specified set. First
584 following byte is number of bitmap bytes. Then come bytes
585 for a bitmap saying which chars are in. Bits in each byte
586 are ordered low-bit-first. A character is in the set if its
587 bit is 1. A character too large to have a bit in the map is
588 automatically not in the set.
590 If the length byte has the 0x80 bit set, then that stuff
591 is followed by a range table:
592 2 bytes of flags for character sets (low 8 bits, high 8 bits)
593 See RANGE_TABLE_WORK_BITS below.
594 2 bytes, the number of pairs that follow (upto 32767)
595 pairs, each 2 multibyte characters,
596 each multibyte character represented as 3 bytes. */
599 /* Same parameters as charset, but match any character that is
600 not one of those specified. */
603 /* Start remembering the text that is matched, for storing in a
604 register. Followed by one byte with the register number, in
605 the range 0 to one less than the pattern buffer's re_nsub
609 /* Stop remembering the text that is matched and store it in a
610 memory register. Followed by one byte with the register
611 number, in the range 0 to one less than `re_nsub' in the
615 /* Match a duplicate of something remembered. Followed by one
616 byte containing the register number. */
619 /* Fail unless at beginning of line. */
622 /* Fail unless at end of line. */
625 /* Succeeds if at beginning of buffer (if emacs) or at beginning
626 of string to be matched (if not). */
629 /* Analogously, for end of buffer/string. */
632 /* Followed by two byte relative address to which to jump. */
635 /* Followed by two-byte relative address of place to resume at
636 in case of failure. */
639 /* Like on_failure_jump, but pushes a placeholder instead of the
640 current string position when executed. */
641 on_failure_keep_string_jump
,
643 /* Just like `on_failure_jump', except that it checks that we
644 don't get stuck in an infinite loop (matching an empty string
646 on_failure_jump_loop
,
648 /* Just like `on_failure_jump_loop', except that it checks for
649 a different kind of loop (the kind that shows up with non-greedy
650 operators). This operation has to be immediately preceded
652 on_failure_jump_nastyloop
,
654 /* A smart `on_failure_jump' used for greedy * and + operators.
655 It analyses the loop before which it is put and if the
656 loop does not require backtracking, it changes itself to
657 `on_failure_keep_string_jump' and short-circuits the loop,
658 else it just defaults to changing itself into `on_failure_jump'.
659 It assumes that it is pointing to just past a `jump'. */
660 on_failure_jump_smart
,
662 /* Followed by two-byte relative address and two-byte number n.
663 After matching N times, jump to the address upon failure.
664 Does not work if N starts at 0: use on_failure_jump_loop
668 /* Followed by two-byte relative address, and two-byte number n.
669 Jump to the address N times, then fail. */
672 /* Set the following two-byte relative address to the
673 subsequent two-byte number. The address *includes* the two
677 wordbeg
, /* Succeeds if at word beginning. */
678 wordend
, /* Succeeds if at word end. */
680 wordbound
, /* Succeeds if at a word boundary. */
681 notwordbound
, /* Succeeds if not at a word boundary. */
683 symbeg
, /* Succeeds if at symbol beginning. */
684 symend
, /* Succeeds if at symbol end. */
686 /* Matches any character whose syntax is specified. Followed by
687 a byte which contains a syntax code, e.g., Sword. */
690 /* Matches any character whose syntax is not that specified. */
694 ,before_dot
, /* Succeeds if before point. */
695 at_dot
, /* Succeeds if at point. */
696 after_dot
, /* Succeeds if after point. */
698 /* Matches any character whose category-set contains the specified
699 category. The operator is followed by a byte which contains a
700 category code (mnemonic ASCII character). */
703 /* Matches any character whose category-set does not contain the
704 specified category. The operator is followed by a byte which
705 contains the category code (mnemonic ASCII character). */
710 /* Common operations on the compiled pattern. */
712 /* Store NUMBER in two contiguous bytes starting at DESTINATION. */
714 #define STORE_NUMBER(destination, number) \
716 (destination)[0] = (number) & 0377; \
717 (destination)[1] = (number) >> 8; \
720 /* Same as STORE_NUMBER, except increment DESTINATION to
721 the byte after where the number is stored. Therefore, DESTINATION
722 must be an lvalue. */
724 #define STORE_NUMBER_AND_INCR(destination, number) \
726 STORE_NUMBER (destination, number); \
727 (destination) += 2; \
730 /* Put into DESTINATION a number stored in two contiguous bytes starting
733 #define EXTRACT_NUMBER(destination, source) \
735 (destination) = *(source) & 0377; \
736 (destination) += SIGN_EXTEND_CHAR (*((source) + 1)) << 8; \
740 static void extract_number
_RE_ARGS ((int *dest
, re_char
*source
));
742 extract_number (dest
, source
)
746 int temp
= SIGN_EXTEND_CHAR (*(source
+ 1));
747 *dest
= *source
& 0377;
751 # ifndef EXTRACT_MACROS /* To debug the macros. */
752 # undef EXTRACT_NUMBER
753 # define EXTRACT_NUMBER(dest, src) extract_number (&dest, src)
754 # endif /* not EXTRACT_MACROS */
758 /* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
759 SOURCE must be an lvalue. */
761 #define EXTRACT_NUMBER_AND_INCR(destination, source) \
763 EXTRACT_NUMBER (destination, source); \
768 static void extract_number_and_incr
_RE_ARGS ((int *destination
,
771 extract_number_and_incr (destination
, source
)
775 extract_number (destination
, *source
);
779 # ifndef EXTRACT_MACROS
780 # undef EXTRACT_NUMBER_AND_INCR
781 # define EXTRACT_NUMBER_AND_INCR(dest, src) \
782 extract_number_and_incr (&dest, &src)
783 # endif /* not EXTRACT_MACROS */
787 /* Store a multibyte character in three contiguous bytes starting
788 DESTINATION, and increment DESTINATION to the byte after where the
789 character is stored. Therefore, DESTINATION must be an lvalue. */
791 #define STORE_CHARACTER_AND_INCR(destination, character) \
793 (destination)[0] = (character) & 0377; \
794 (destination)[1] = ((character) >> 8) & 0377; \
795 (destination)[2] = (character) >> 16; \
796 (destination) += 3; \
799 /* Put into DESTINATION a character stored in three contiguous bytes
800 starting at SOURCE. */
802 #define EXTRACT_CHARACTER(destination, source) \
804 (destination) = ((source)[0] \
805 | ((source)[1] << 8) \
806 | ((source)[2] << 16)); \
810 /* Macros for charset. */
812 /* Size of bitmap of charset P in bytes. P is a start of charset,
813 i.e. *P is (re_opcode_t) charset or (re_opcode_t) charset_not. */
814 #define CHARSET_BITMAP_SIZE(p) ((p)[1] & 0x7F)
816 /* Nonzero if charset P has range table. */
817 #define CHARSET_RANGE_TABLE_EXISTS_P(p) ((p)[1] & 0x80)
819 /* Return the address of range table of charset P. But not the start
820 of table itself, but the before where the number of ranges is
821 stored. `2 +' means to skip re_opcode_t and size of bitmap,
822 and the 2 bytes of flags at the start of the range table. */
823 #define CHARSET_RANGE_TABLE(p) (&(p)[4 + CHARSET_BITMAP_SIZE (p)])
825 /* Extract the bit flags that start a range table. */
826 #define CHARSET_RANGE_TABLE_BITS(p) \
827 ((p)[2 + CHARSET_BITMAP_SIZE (p)] \
828 + (p)[3 + CHARSET_BITMAP_SIZE (p)] * 0x100)
830 /* Test if C is listed in the bitmap of charset P. */
831 #define CHARSET_LOOKUP_BITMAP(p, c) \
832 ((c) < CHARSET_BITMAP_SIZE (p) * BYTEWIDTH \
833 && (p)[2 + (c) / BYTEWIDTH] & (1 << ((c) % BYTEWIDTH)))
835 /* Return the address of end of RANGE_TABLE. COUNT is number of
836 ranges (which is a pair of (start, end)) in the RANGE_TABLE. `* 2'
837 is start of range and end of range. `* 3' is size of each start
839 #define CHARSET_RANGE_TABLE_END(range_table, count) \
840 ((range_table) + (count) * 2 * 3)
842 /* Test if C is in RANGE_TABLE. A flag NOT is negated if C is in.
843 COUNT is number of ranges in RANGE_TABLE. */
844 #define CHARSET_LOOKUP_RANGE_TABLE_RAW(not, c, range_table, count) \
847 re_wchar_t range_start, range_end; \
849 re_char *range_table_end \
850 = CHARSET_RANGE_TABLE_END ((range_table), (count)); \
852 for (p = (range_table); p < range_table_end; p += 2 * 3) \
854 EXTRACT_CHARACTER (range_start, p); \
855 EXTRACT_CHARACTER (range_end, p + 3); \
857 if (range_start <= (c) && (c) <= range_end) \
866 /* Test if C is in range table of CHARSET. The flag NOT is negated if
867 C is listed in it. */
868 #define CHARSET_LOOKUP_RANGE_TABLE(not, c, charset) \
871 /* Number of ranges in range table. */ \
873 re_char *range_table = CHARSET_RANGE_TABLE (charset); \
875 EXTRACT_NUMBER_AND_INCR (count, range_table); \
876 CHARSET_LOOKUP_RANGE_TABLE_RAW ((not), (c), range_table, count); \
880 /* If DEBUG is defined, Regex prints many voluminous messages about what
881 it is doing (if the variable `debug' is nonzero). If linked with the
882 main program in `iregex.c', you can enter patterns and strings
883 interactively. And if linked with the main program in `main.c' and
884 the other test files, you can run the already-written tests. */
888 /* We use standard I/O for debugging. */
891 /* It is useful to test things that ``must'' be true when debugging. */
894 static int debug
= -100000;
896 # define DEBUG_STATEMENT(e) e
897 # define DEBUG_PRINT1(x) if (debug > 0) printf (x)
898 # define DEBUG_PRINT2(x1, x2) if (debug > 0) printf (x1, x2)
899 # define DEBUG_PRINT3(x1, x2, x3) if (debug > 0) printf (x1, x2, x3)
900 # define DEBUG_PRINT4(x1, x2, x3, x4) if (debug > 0) printf (x1, x2, x3, x4)
901 # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) \
902 if (debug > 0) print_partial_compiled_pattern (s, e)
903 # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \
904 if (debug > 0) print_double_string (w, s1, sz1, s2, sz2)
907 /* Print the fastmap in human-readable form. */
910 print_fastmap (fastmap
)
913 unsigned was_a_range
= 0;
916 while (i
< (1 << BYTEWIDTH
))
922 while (i
< (1 << BYTEWIDTH
) && fastmap
[i
])
938 /* Print a compiled pattern string in human-readable form, starting at
939 the START pointer into it and ending just before the pointer END. */
942 print_partial_compiled_pattern (start
, end
)
952 fprintf (stderr
, "(null)\n");
956 /* Loop over pattern commands. */
959 fprintf (stderr
, "%d:\t", p
- start
);
961 switch ((re_opcode_t
) *p
++)
964 fprintf (stderr
, "/no_op");
968 fprintf (stderr
, "/succeed");
973 fprintf (stderr
, "/exactn/%d", mcnt
);
976 fprintf (stderr
, "/%c", *p
++);
982 fprintf (stderr
, "/start_memory/%d", *p
++);
986 fprintf (stderr
, "/stop_memory/%d", *p
++);
990 fprintf (stderr
, "/duplicate/%d", *p
++);
994 fprintf (stderr
, "/anychar");
1000 register int c
, last
= -100;
1001 register int in_range
= 0;
1002 int length
= CHARSET_BITMAP_SIZE (p
- 1);
1003 int has_range_table
= CHARSET_RANGE_TABLE_EXISTS_P (p
- 1);
1005 fprintf (stderr
, "/charset [%s",
1006 (re_opcode_t
) *(p
- 1) == charset_not
? "^" : "");
1009 fprintf (stderr
, " !extends past end of pattern! ");
1011 for (c
= 0; c
< 256; c
++)
1013 && (p
[1 + (c
/8)] & (1 << (c
% 8))))
1015 /* Are we starting a range? */
1016 if (last
+ 1 == c
&& ! in_range
)
1018 fprintf (stderr
, "-");
1021 /* Have we broken a range? */
1022 else if (last
+ 1 != c
&& in_range
)
1024 fprintf (stderr
, "%c", last
);
1029 fprintf (stderr
, "%c", c
);
1035 fprintf (stderr
, "%c", last
);
1037 fprintf (stderr
, "]");
1041 if (has_range_table
)
1044 fprintf (stderr
, "has-range-table");
1046 /* ??? Should print the range table; for now, just skip it. */
1047 p
+= 2; /* skip range table bits */
1048 EXTRACT_NUMBER_AND_INCR (count
, p
);
1049 p
= CHARSET_RANGE_TABLE_END (p
, count
);
1055 fprintf (stderr
, "/begline");
1059 fprintf (stderr
, "/endline");
1062 case on_failure_jump
:
1063 extract_number_and_incr (&mcnt
, &p
);
1064 fprintf (stderr
, "/on_failure_jump to %d", p
+ mcnt
- start
);
1067 case on_failure_keep_string_jump
:
1068 extract_number_and_incr (&mcnt
, &p
);
1069 fprintf (stderr
, "/on_failure_keep_string_jump to %d", p
+ mcnt
- start
);
1072 case on_failure_jump_nastyloop
:
1073 extract_number_and_incr (&mcnt
, &p
);
1074 fprintf (stderr
, "/on_failure_jump_nastyloop to %d", p
+ mcnt
- start
);
1077 case on_failure_jump_loop
:
1078 extract_number_and_incr (&mcnt
, &p
);
1079 fprintf (stderr
, "/on_failure_jump_loop to %d", p
+ mcnt
- start
);
1082 case on_failure_jump_smart
:
1083 extract_number_and_incr (&mcnt
, &p
);
1084 fprintf (stderr
, "/on_failure_jump_smart to %d", p
+ mcnt
- start
);
1088 extract_number_and_incr (&mcnt
, &p
);
1089 fprintf (stderr
, "/jump to %d", p
+ mcnt
- start
);
1093 extract_number_and_incr (&mcnt
, &p
);
1094 extract_number_and_incr (&mcnt2
, &p
);
1095 fprintf (stderr
, "/succeed_n to %d, %d times", p
- 2 + mcnt
- start
, mcnt2
);
1099 extract_number_and_incr (&mcnt
, &p
);
1100 extract_number_and_incr (&mcnt2
, &p
);
1101 fprintf (stderr
, "/jump_n to %d, %d times", p
- 2 + mcnt
- start
, mcnt2
);
1105 extract_number_and_incr (&mcnt
, &p
);
1106 extract_number_and_incr (&mcnt2
, &p
);
1107 fprintf (stderr
, "/set_number_at location %d to %d", p
- 2 + mcnt
- start
, mcnt2
);
1111 fprintf (stderr
, "/wordbound");
1115 fprintf (stderr
, "/notwordbound");
1119 fprintf (stderr
, "/wordbeg");
1123 fprintf (stderr
, "/wordend");
1127 fprintf (stderr
, "/symbeg");
1131 fprintf (stderr
, "/symend");
1135 fprintf (stderr
, "/syntaxspec");
1137 fprintf (stderr
, "/%d", mcnt
);
1141 fprintf (stderr
, "/notsyntaxspec");
1143 fprintf (stderr
, "/%d", mcnt
);
1148 fprintf (stderr
, "/before_dot");
1152 fprintf (stderr
, "/at_dot");
1156 fprintf (stderr
, "/after_dot");
1160 fprintf (stderr
, "/categoryspec");
1162 fprintf (stderr
, "/%d", mcnt
);
1165 case notcategoryspec
:
1166 fprintf (stderr
, "/notcategoryspec");
1168 fprintf (stderr
, "/%d", mcnt
);
1173 fprintf (stderr
, "/begbuf");
1177 fprintf (stderr
, "/endbuf");
1181 fprintf (stderr
, "?%d", *(p
-1));
1184 fprintf (stderr
, "\n");
1187 fprintf (stderr
, "%d:\tend of pattern.\n", p
- start
);
1192 print_compiled_pattern (bufp
)
1193 struct re_pattern_buffer
*bufp
;
1195 re_char
*buffer
= bufp
->buffer
;
1197 print_partial_compiled_pattern (buffer
, buffer
+ bufp
->used
);
1198 printf ("%ld bytes used/%ld bytes allocated.\n",
1199 bufp
->used
, bufp
->allocated
);
1201 if (bufp
->fastmap_accurate
&& bufp
->fastmap
)
1203 printf ("fastmap: ");
1204 print_fastmap (bufp
->fastmap
);
1207 printf ("re_nsub: %d\t", bufp
->re_nsub
);
1208 printf ("regs_alloc: %d\t", bufp
->regs_allocated
);
1209 printf ("can_be_null: %d\t", bufp
->can_be_null
);
1210 printf ("no_sub: %d\t", bufp
->no_sub
);
1211 printf ("not_bol: %d\t", bufp
->not_bol
);
1212 printf ("not_eol: %d\t", bufp
->not_eol
);
1213 printf ("syntax: %lx\n", bufp
->syntax
);
1215 /* Perhaps we should print the translate table? */
1220 print_double_string (where
, string1
, size1
, string2
, size2
)
1233 if (FIRST_STRING_P (where
))
1235 for (this_char
= where
- string1
; this_char
< size1
; this_char
++)
1236 putchar (string1
[this_char
]);
1241 for (this_char
= where
- string2
; this_char
< size2
; this_char
++)
1242 putchar (string2
[this_char
]);
1246 #else /* not DEBUG */
1251 # define DEBUG_STATEMENT(e)
1252 # define DEBUG_PRINT1(x)
1253 # define DEBUG_PRINT2(x1, x2)
1254 # define DEBUG_PRINT3(x1, x2, x3)
1255 # define DEBUG_PRINT4(x1, x2, x3, x4)
1256 # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
1257 # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
1259 #endif /* not DEBUG */
1261 /* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
1262 also be assigned to arbitrarily: each pattern buffer stores its own
1263 syntax, so it can be changed between regex compilations. */
1264 /* This has no initializer because initialized variables in Emacs
1265 become read-only after dumping. */
1266 reg_syntax_t re_syntax_options
;
1269 /* Specify the precise syntax of regexps for compilation. This provides
1270 for compatibility for various utilities which historically have
1271 different, incompatible syntaxes.
1273 The argument SYNTAX is a bit mask comprised of the various bits
1274 defined in regex.h. We return the old syntax. */
1277 re_set_syntax (syntax
)
1278 reg_syntax_t syntax
;
1280 reg_syntax_t ret
= re_syntax_options
;
1282 re_syntax_options
= syntax
;
1285 WEAK_ALIAS (__re_set_syntax
, re_set_syntax
)
1287 /* This table gives an error message for each of the error codes listed
1288 in regex.h. Obviously the order here has to be same as there.
1289 POSIX doesn't require that we do anything for REG_NOERROR,
1290 but why not be nice? */
1292 static const char *re_error_msgid
[] =
1294 gettext_noop ("Success"), /* REG_NOERROR */
1295 gettext_noop ("No match"), /* REG_NOMATCH */
1296 gettext_noop ("Invalid regular expression"), /* REG_BADPAT */
1297 gettext_noop ("Invalid collation character"), /* REG_ECOLLATE */
1298 gettext_noop ("Invalid character class name"), /* REG_ECTYPE */
1299 gettext_noop ("Trailing backslash"), /* REG_EESCAPE */
1300 gettext_noop ("Invalid back reference"), /* REG_ESUBREG */
1301 gettext_noop ("Unmatched [ or [^"), /* REG_EBRACK */
1302 gettext_noop ("Unmatched ( or \\("), /* REG_EPAREN */
1303 gettext_noop ("Unmatched \\{"), /* REG_EBRACE */
1304 gettext_noop ("Invalid content of \\{\\}"), /* REG_BADBR */
1305 gettext_noop ("Invalid range end"), /* REG_ERANGE */
1306 gettext_noop ("Memory exhausted"), /* REG_ESPACE */
1307 gettext_noop ("Invalid preceding regular expression"), /* REG_BADRPT */
1308 gettext_noop ("Premature end of regular expression"), /* REG_EEND */
1309 gettext_noop ("Regular expression too big"), /* REG_ESIZE */
1310 gettext_noop ("Unmatched ) or \\)"), /* REG_ERPAREN */
1313 /* Avoiding alloca during matching, to placate r_alloc. */
1315 /* Define MATCH_MAY_ALLOCATE unless we need to make sure that the
1316 searching and matching functions should not call alloca. On some
1317 systems, alloca is implemented in terms of malloc, and if we're
1318 using the relocating allocator routines, then malloc could cause a
1319 relocation, which might (if the strings being searched are in the
1320 ralloc heap) shift the data out from underneath the regexp
1323 Here's another reason to avoid allocation: Emacs
1324 processes input from X in a signal handler; processing X input may
1325 call malloc; if input arrives while a matching routine is calling
1326 malloc, then we're scrod. But Emacs can't just block input while
1327 calling matching routines; then we don't notice interrupts when
1328 they come in. So, Emacs blocks input around all regexp calls
1329 except the matching calls, which it leaves unprotected, in the
1330 faith that they will not malloc. */
1332 /* Normally, this is fine. */
1333 #define MATCH_MAY_ALLOCATE
1335 /* When using GNU C, we are not REALLY using the C alloca, no matter
1336 what config.h may say. So don't take precautions for it. */
1341 /* The match routines may not allocate if (1) they would do it with malloc
1342 and (2) it's not safe for them to use malloc.
1343 Note that if REL_ALLOC is defined, matching would not use malloc for the
1344 failure stack, but we would still use it for the register vectors;
1345 so REL_ALLOC should not affect this. */
1346 #if (defined C_ALLOCA || defined REGEX_MALLOC) && defined emacs
1347 # undef MATCH_MAY_ALLOCATE
1351 /* Failure stack declarations and macros; both re_compile_fastmap and
1352 re_match_2 use a failure stack. These have to be macros because of
1353 REGEX_ALLOCATE_STACK. */
1356 /* Approximate number of failure points for which to initially allocate space
1357 when matching. If this number is exceeded, we allocate more
1358 space, so it is not a hard limit. */
1359 #ifndef INIT_FAILURE_ALLOC
1360 # define INIT_FAILURE_ALLOC 20
1363 /* Roughly the maximum number of failure points on the stack. Would be
1364 exactly that if always used TYPICAL_FAILURE_SIZE items each time we failed.
1365 This is a variable only so users of regex can assign to it; we never
1366 change it ourselves. We always multiply it by TYPICAL_FAILURE_SIZE
1367 before using it, so it should probably be a byte-count instead. */
1368 # if defined MATCH_MAY_ALLOCATE
1369 /* Note that 4400 was enough to cause a crash on Alpha OSF/1,
1370 whose default stack limit is 2mb. In order for a larger
1371 value to work reliably, you have to try to make it accord
1372 with the process stack limit. */
1373 size_t re_max_failures
= 40000;
1375 size_t re_max_failures
= 4000;
1378 union fail_stack_elt
1381 /* This should be the biggest `int' that's no bigger than a pointer. */
1385 typedef union fail_stack_elt fail_stack_elt_t
;
1389 fail_stack_elt_t
*stack
;
1391 size_t avail
; /* Offset of next open position. */
1392 size_t frame
; /* Offset of the cur constructed frame. */
1395 #define FAIL_STACK_EMPTY() (fail_stack.frame == 0)
1396 #define FAIL_STACK_FULL() (fail_stack.avail == fail_stack.size)
1399 /* Define macros to initialize and free the failure stack.
1400 Do `return -2' if the alloc fails. */
1402 #ifdef MATCH_MAY_ALLOCATE
1403 # define INIT_FAIL_STACK() \
1405 fail_stack.stack = (fail_stack_elt_t *) \
1406 REGEX_ALLOCATE_STACK (INIT_FAILURE_ALLOC * TYPICAL_FAILURE_SIZE \
1407 * sizeof (fail_stack_elt_t)); \
1409 if (fail_stack.stack == NULL) \
1412 fail_stack.size = INIT_FAILURE_ALLOC; \
1413 fail_stack.avail = 0; \
1414 fail_stack.frame = 0; \
1417 # define RESET_FAIL_STACK() REGEX_FREE_STACK (fail_stack.stack)
1419 # define INIT_FAIL_STACK() \
1421 fail_stack.avail = 0; \
1422 fail_stack.frame = 0; \
1425 # define RESET_FAIL_STACK() ((void)0)
1429 /* Double the size of FAIL_STACK, up to a limit
1430 which allows approximately `re_max_failures' items.
1432 Return 1 if succeeds, and 0 if either ran out of memory
1433 allocating space for it or it was already too large.
1435 REGEX_REALLOCATE_STACK requires `destination' be declared. */
1437 /* Factor to increase the failure stack size by
1438 when we increase it.
1439 This used to be 2, but 2 was too wasteful
1440 because the old discarded stacks added up to as much space
1441 were as ultimate, maximum-size stack. */
1442 #define FAIL_STACK_GROWTH_FACTOR 4
1444 #define GROW_FAIL_STACK(fail_stack) \
1445 (((fail_stack).size * sizeof (fail_stack_elt_t) \
1446 >= re_max_failures * TYPICAL_FAILURE_SIZE) \
1448 : ((fail_stack).stack \
1449 = (fail_stack_elt_t *) \
1450 REGEX_REALLOCATE_STACK ((fail_stack).stack, \
1451 (fail_stack).size * sizeof (fail_stack_elt_t), \
1452 MIN (re_max_failures * TYPICAL_FAILURE_SIZE, \
1453 ((fail_stack).size * sizeof (fail_stack_elt_t) \
1454 * FAIL_STACK_GROWTH_FACTOR))), \
1456 (fail_stack).stack == NULL \
1458 : ((fail_stack).size \
1459 = (MIN (re_max_failures * TYPICAL_FAILURE_SIZE, \
1460 ((fail_stack).size * sizeof (fail_stack_elt_t) \
1461 * FAIL_STACK_GROWTH_FACTOR)) \
1462 / sizeof (fail_stack_elt_t)), \
1466 /* Push a pointer value onto the failure stack.
1467 Assumes the variable `fail_stack'. Probably should only
1468 be called from within `PUSH_FAILURE_POINT'. */
1469 #define PUSH_FAILURE_POINTER(item) \
1470 fail_stack.stack[fail_stack.avail++].pointer = (item)
1472 /* This pushes an integer-valued item onto the failure stack.
1473 Assumes the variable `fail_stack'. Probably should only
1474 be called from within `PUSH_FAILURE_POINT'. */
1475 #define PUSH_FAILURE_INT(item) \
1476 fail_stack.stack[fail_stack.avail++].integer = (item)
1478 /* Push a fail_stack_elt_t value onto the failure stack.
1479 Assumes the variable `fail_stack'. Probably should only
1480 be called from within `PUSH_FAILURE_POINT'. */
1481 #define PUSH_FAILURE_ELT(item) \
1482 fail_stack.stack[fail_stack.avail++] = (item)
1484 /* These three POP... operations complement the three PUSH... operations.
1485 All assume that `fail_stack' is nonempty. */
1486 #define POP_FAILURE_POINTER() fail_stack.stack[--fail_stack.avail].pointer
1487 #define POP_FAILURE_INT() fail_stack.stack[--fail_stack.avail].integer
1488 #define POP_FAILURE_ELT() fail_stack.stack[--fail_stack.avail]
1490 /* Individual items aside from the registers. */
1491 #define NUM_NONREG_ITEMS 3
1493 /* Used to examine the stack (to detect infinite loops). */
1494 #define FAILURE_PAT(h) fail_stack.stack[(h) - 1].pointer
1495 #define FAILURE_STR(h) (fail_stack.stack[(h) - 2].pointer)
1496 #define NEXT_FAILURE_HANDLE(h) fail_stack.stack[(h) - 3].integer
1497 #define TOP_FAILURE_HANDLE() fail_stack.frame
1500 #define ENSURE_FAIL_STACK(space) \
1501 while (REMAINING_AVAIL_SLOTS <= space) { \
1502 if (!GROW_FAIL_STACK (fail_stack)) \
1504 DEBUG_PRINT2 ("\n Doubled stack; size now: %d\n", (fail_stack).size);\
1505 DEBUG_PRINT2 (" slots available: %d\n", REMAINING_AVAIL_SLOTS);\
1508 /* Push register NUM onto the stack. */
1509 #define PUSH_FAILURE_REG(num) \
1511 char *destination; \
1512 ENSURE_FAIL_STACK(3); \
1513 DEBUG_PRINT4 (" Push reg %d (spanning %p -> %p)\n", \
1514 num, regstart[num], regend[num]); \
1515 PUSH_FAILURE_POINTER (regstart[num]); \
1516 PUSH_FAILURE_POINTER (regend[num]); \
1517 PUSH_FAILURE_INT (num); \
1520 /* Change the counter's value to VAL, but make sure that it will
1521 be reset when backtracking. */
1522 #define PUSH_NUMBER(ptr,val) \
1524 char *destination; \
1526 ENSURE_FAIL_STACK(3); \
1527 EXTRACT_NUMBER (c, ptr); \
1528 DEBUG_PRINT4 (" Push number %p = %d -> %d\n", ptr, c, val); \
1529 PUSH_FAILURE_INT (c); \
1530 PUSH_FAILURE_POINTER (ptr); \
1531 PUSH_FAILURE_INT (-1); \
1532 STORE_NUMBER (ptr, val); \
1535 /* Pop a saved register off the stack. */
1536 #define POP_FAILURE_REG_OR_COUNT() \
1538 int reg = POP_FAILURE_INT (); \
1541 /* It's a counter. */ \
1542 /* Here, we discard `const', making re_match non-reentrant. */ \
1543 unsigned char *ptr = (unsigned char*) POP_FAILURE_POINTER (); \
1544 reg = POP_FAILURE_INT (); \
1545 STORE_NUMBER (ptr, reg); \
1546 DEBUG_PRINT3 (" Pop counter %p = %d\n", ptr, reg); \
1550 regend[reg] = POP_FAILURE_POINTER (); \
1551 regstart[reg] = POP_FAILURE_POINTER (); \
1552 DEBUG_PRINT4 (" Pop reg %d (spanning %p -> %p)\n", \
1553 reg, regstart[reg], regend[reg]); \
1557 /* Check that we are not stuck in an infinite loop. */
1558 #define CHECK_INFINITE_LOOP(pat_cur, string_place) \
1560 int failure = TOP_FAILURE_HANDLE (); \
1561 /* Check for infinite matching loops */ \
1562 while (failure > 0 \
1563 && (FAILURE_STR (failure) == string_place \
1564 || FAILURE_STR (failure) == NULL)) \
1566 assert (FAILURE_PAT (failure) >= bufp->buffer \
1567 && FAILURE_PAT (failure) <= bufp->buffer + bufp->used); \
1568 if (FAILURE_PAT (failure) == pat_cur) \
1573 DEBUG_PRINT2 (" Other pattern: %p\n", FAILURE_PAT (failure)); \
1574 failure = NEXT_FAILURE_HANDLE(failure); \
1576 DEBUG_PRINT2 (" Other string: %p\n", FAILURE_STR (failure)); \
1579 /* Push the information about the state we will need
1580 if we ever fail back to it.
1582 Requires variables fail_stack, regstart, regend and
1583 num_regs be declared. GROW_FAIL_STACK requires `destination' be
1586 Does `return FAILURE_CODE' if runs out of memory. */
1588 #define PUSH_FAILURE_POINT(pattern, string_place) \
1590 char *destination; \
1591 /* Must be int, so when we don't save any registers, the arithmetic \
1592 of 0 + -1 isn't done as unsigned. */ \
1594 DEBUG_STATEMENT (nfailure_points_pushed++); \
1595 DEBUG_PRINT1 ("\nPUSH_FAILURE_POINT:\n"); \
1596 DEBUG_PRINT2 (" Before push, next avail: %d\n", (fail_stack).avail); \
1597 DEBUG_PRINT2 (" size: %d\n", (fail_stack).size);\
1599 ENSURE_FAIL_STACK (NUM_NONREG_ITEMS); \
1601 DEBUG_PRINT1 ("\n"); \
1603 DEBUG_PRINT2 (" Push frame index: %d\n", fail_stack.frame); \
1604 PUSH_FAILURE_INT (fail_stack.frame); \
1606 DEBUG_PRINT2 (" Push string %p: `", string_place); \
1607 DEBUG_PRINT_DOUBLE_STRING (string_place, string1, size1, string2, size2);\
1608 DEBUG_PRINT1 ("'\n"); \
1609 PUSH_FAILURE_POINTER (string_place); \
1611 DEBUG_PRINT2 (" Push pattern %p: ", pattern); \
1612 DEBUG_PRINT_COMPILED_PATTERN (bufp, pattern, pend); \
1613 PUSH_FAILURE_POINTER (pattern); \
1615 /* Close the frame by moving the frame pointer past it. */ \
1616 fail_stack.frame = fail_stack.avail; \
1619 /* Estimate the size of data pushed by a typical failure stack entry.
1620 An estimate is all we need, because all we use this for
1621 is to choose a limit for how big to make the failure stack. */
1622 /* BEWARE, the value `20' is hard-coded in emacs.c:main(). */
1623 #define TYPICAL_FAILURE_SIZE 20
1625 /* How many items can still be added to the stack without overflowing it. */
1626 #define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail)
1629 /* Pops what PUSH_FAIL_STACK pushes.
1631 We restore into the parameters, all of which should be lvalues:
1632 STR -- the saved data position.
1633 PAT -- the saved pattern position.
1634 REGSTART, REGEND -- arrays of string positions.
1636 Also assumes the variables `fail_stack' and (if debugging), `bufp',
1637 `pend', `string1', `size1', `string2', and `size2'. */
1639 #define POP_FAILURE_POINT(str, pat) \
1641 assert (!FAIL_STACK_EMPTY ()); \
1643 /* Remove failure points and point to how many regs pushed. */ \
1644 DEBUG_PRINT1 ("POP_FAILURE_POINT:\n"); \
1645 DEBUG_PRINT2 (" Before pop, next avail: %d\n", fail_stack.avail); \
1646 DEBUG_PRINT2 (" size: %d\n", fail_stack.size); \
1648 /* Pop the saved registers. */ \
1649 while (fail_stack.frame < fail_stack.avail) \
1650 POP_FAILURE_REG_OR_COUNT (); \
1652 pat = POP_FAILURE_POINTER (); \
1653 DEBUG_PRINT2 (" Popping pattern %p: ", pat); \
1654 DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend); \
1656 /* If the saved string location is NULL, it came from an \
1657 on_failure_keep_string_jump opcode, and we want to throw away the \
1658 saved NULL, thus retaining our current position in the string. */ \
1659 str = POP_FAILURE_POINTER (); \
1660 DEBUG_PRINT2 (" Popping string %p: `", str); \
1661 DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2); \
1662 DEBUG_PRINT1 ("'\n"); \
1664 fail_stack.frame = POP_FAILURE_INT (); \
1665 DEBUG_PRINT2 (" Popping frame index: %d\n", fail_stack.frame); \
1667 assert (fail_stack.avail >= 0); \
1668 assert (fail_stack.frame <= fail_stack.avail); \
1670 DEBUG_STATEMENT (nfailure_points_popped++); \
1671 } while (0) /* POP_FAILURE_POINT */
1675 /* Registers are set to a sentinel when they haven't yet matched. */
1676 #define REG_UNSET(e) ((e) == NULL)
1678 /* Subroutine declarations and macros for regex_compile. */
1680 static reg_errcode_t regex_compile
_RE_ARGS ((re_char
*pattern
, size_t size
,
1681 reg_syntax_t syntax
,
1682 struct re_pattern_buffer
*bufp
));
1683 static void store_op1
_RE_ARGS ((re_opcode_t op
, unsigned char *loc
, int arg
));
1684 static void store_op2
_RE_ARGS ((re_opcode_t op
, unsigned char *loc
,
1685 int arg1
, int arg2
));
1686 static void insert_op1
_RE_ARGS ((re_opcode_t op
, unsigned char *loc
,
1687 int arg
, unsigned char *end
));
1688 static void insert_op2
_RE_ARGS ((re_opcode_t op
, unsigned char *loc
,
1689 int arg1
, int arg2
, unsigned char *end
));
1690 static boolean at_begline_loc_p
_RE_ARGS ((re_char
*pattern
,
1692 reg_syntax_t syntax
));
1693 static boolean at_endline_loc_p
_RE_ARGS ((re_char
*p
,
1695 reg_syntax_t syntax
));
1696 static re_char
*skip_one_char
_RE_ARGS ((re_char
*p
));
1697 static int analyse_first
_RE_ARGS ((re_char
*p
, re_char
*pend
,
1698 char *fastmap
, const int multibyte
));
1700 /* Fetch the next character in the uncompiled pattern, with no
1702 #define PATFETCH(c) \
1705 if (p == pend) return REG_EEND; \
1706 c = RE_STRING_CHAR_AND_LENGTH (p, pend - p, len); \
1711 /* If `translate' is non-null, return translate[D], else just D. We
1712 cast the subscript to translate because some data is declared as
1713 `char *', to avoid warnings when a string constant is passed. But
1714 when we use a character as a subscript we must make it unsigned. */
1716 # define TRANSLATE(d) \
1717 (RE_TRANSLATE_P (translate) ? RE_TRANSLATE (translate, (d)) : (d))
1721 /* Macros for outputting the compiled pattern into `buffer'. */
1723 /* If the buffer isn't allocated when it comes in, use this. */
1724 #define INIT_BUF_SIZE 32
1726 /* Make sure we have at least N more bytes of space in buffer. */
1727 #define GET_BUFFER_SPACE(n) \
1728 while ((size_t) (b - bufp->buffer + (n)) > bufp->allocated) \
1731 /* Make sure we have one more byte of buffer space and then add C to it. */
1732 #define BUF_PUSH(c) \
1734 GET_BUFFER_SPACE (1); \
1735 *b++ = (unsigned char) (c); \
1739 /* Ensure we have two more bytes of buffer space and then append C1 and C2. */
1740 #define BUF_PUSH_2(c1, c2) \
1742 GET_BUFFER_SPACE (2); \
1743 *b++ = (unsigned char) (c1); \
1744 *b++ = (unsigned char) (c2); \
1748 /* As with BUF_PUSH_2, except for three bytes. */
1749 #define BUF_PUSH_3(c1, c2, c3) \
1751 GET_BUFFER_SPACE (3); \
1752 *b++ = (unsigned char) (c1); \
1753 *b++ = (unsigned char) (c2); \
1754 *b++ = (unsigned char) (c3); \
1758 /* Store a jump with opcode OP at LOC to location TO. We store a
1759 relative address offset by the three bytes the jump itself occupies. */
1760 #define STORE_JUMP(op, loc, to) \
1761 store_op1 (op, loc, (to) - (loc) - 3)
1763 /* Likewise, for a two-argument jump. */
1764 #define STORE_JUMP2(op, loc, to, arg) \
1765 store_op2 (op, loc, (to) - (loc) - 3, arg)
1767 /* Like `STORE_JUMP', but for inserting. Assume `b' is the buffer end. */
1768 #define INSERT_JUMP(op, loc, to) \
1769 insert_op1 (op, loc, (to) - (loc) - 3, b)
1771 /* Like `STORE_JUMP2', but for inserting. Assume `b' is the buffer end. */
1772 #define INSERT_JUMP2(op, loc, to, arg) \
1773 insert_op2 (op, loc, (to) - (loc) - 3, arg, b)
1776 /* This is not an arbitrary limit: the arguments which represent offsets
1777 into the pattern are two bytes long. So if 2^15 bytes turns out to
1778 be too small, many things would have to change. */
1779 # define MAX_BUF_SIZE (1L << 15)
1781 #if 0 /* This is when we thought it could be 2^16 bytes. */
1782 /* Any other compiler which, like MSC, has allocation limit below 2^16
1783 bytes will have to use approach similar to what was done below for
1784 MSC and drop MAX_BUF_SIZE a bit. Otherwise you may end up
1785 reallocating to 0 bytes. Such thing is not going to work too well.
1786 You have been warned!! */
1787 #if defined _MSC_VER && !defined WIN32
1788 /* Microsoft C 16-bit versions limit malloc to approx 65512 bytes. */
1789 # define MAX_BUF_SIZE 65500L
1791 # define MAX_BUF_SIZE (1L << 16)
1795 /* Extend the buffer by twice its current size via realloc and
1796 reset the pointers that pointed into the old block to point to the
1797 correct places in the new one. If extending the buffer results in it
1798 being larger than MAX_BUF_SIZE, then flag memory exhausted. */
1799 #if __BOUNDED_POINTERS__
1800 # define SET_HIGH_BOUND(P) (__ptrhigh (P) = __ptrlow (P) + bufp->allocated)
1801 # define MOVE_BUFFER_POINTER(P) \
1802 (__ptrlow (P) += incr, SET_HIGH_BOUND (P), __ptrvalue (P) += incr)
1803 # define ELSE_EXTEND_BUFFER_HIGH_BOUND \
1806 SET_HIGH_BOUND (b); \
1807 SET_HIGH_BOUND (begalt); \
1808 if (fixup_alt_jump) \
1809 SET_HIGH_BOUND (fixup_alt_jump); \
1811 SET_HIGH_BOUND (laststart); \
1812 if (pending_exact) \
1813 SET_HIGH_BOUND (pending_exact); \
1816 # define MOVE_BUFFER_POINTER(P) (P) += incr
1817 # define ELSE_EXTEND_BUFFER_HIGH_BOUND
1819 #define EXTEND_BUFFER() \
1821 re_char *old_buffer = bufp->buffer; \
1822 if (bufp->allocated == MAX_BUF_SIZE) \
1824 bufp->allocated <<= 1; \
1825 if (bufp->allocated > MAX_BUF_SIZE) \
1826 bufp->allocated = MAX_BUF_SIZE; \
1827 RETALLOC (bufp->buffer, bufp->allocated, unsigned char); \
1828 if (bufp->buffer == NULL) \
1829 return REG_ESPACE; \
1830 /* If the buffer moved, move all the pointers into it. */ \
1831 if (old_buffer != bufp->buffer) \
1833 int incr = bufp->buffer - old_buffer; \
1834 MOVE_BUFFER_POINTER (b); \
1835 MOVE_BUFFER_POINTER (begalt); \
1836 if (fixup_alt_jump) \
1837 MOVE_BUFFER_POINTER (fixup_alt_jump); \
1839 MOVE_BUFFER_POINTER (laststart); \
1840 if (pending_exact) \
1841 MOVE_BUFFER_POINTER (pending_exact); \
1843 ELSE_EXTEND_BUFFER_HIGH_BOUND \
1847 /* Since we have one byte reserved for the register number argument to
1848 {start,stop}_memory, the maximum number of groups we can report
1849 things about is what fits in that byte. */
1850 #define MAX_REGNUM 255
1852 /* But patterns can have more than `MAX_REGNUM' registers. We just
1853 ignore the excess. */
1854 typedef int regnum_t
;
1857 /* Macros for the compile stack. */
1859 /* Since offsets can go either forwards or backwards, this type needs to
1860 be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1. */
1861 /* int may be not enough when sizeof(int) == 2. */
1862 typedef long pattern_offset_t
;
1866 pattern_offset_t begalt_offset
;
1867 pattern_offset_t fixup_alt_jump
;
1868 pattern_offset_t laststart_offset
;
1870 } compile_stack_elt_t
;
1875 compile_stack_elt_t
*stack
;
1877 unsigned avail
; /* Offset of next open position. */
1878 } compile_stack_type
;
1881 #define INIT_COMPILE_STACK_SIZE 32
1883 #define COMPILE_STACK_EMPTY (compile_stack.avail == 0)
1884 #define COMPILE_STACK_FULL (compile_stack.avail == compile_stack.size)
1886 /* The next available element. */
1887 #define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
1889 /* Explicit quit checking is only used on NTemacs. */
1890 #if defined WINDOWSNT && defined emacs && defined QUIT
1891 extern int immediate_quit
;
1892 # define IMMEDIATE_QUIT_CHECK \
1894 if (immediate_quit) QUIT; \
1897 # define IMMEDIATE_QUIT_CHECK ((void)0)
1900 /* Structure to manage work area for range table. */
1901 struct range_table_work_area
1903 int *table
; /* actual work area. */
1904 int allocated
; /* allocated size for work area in bytes. */
1905 int used
; /* actually used size in words. */
1906 int bits
; /* flag to record character classes */
1909 /* Make sure that WORK_AREA can hold more N multibyte characters.
1910 This is used only in set_image_of_range and set_image_of_range_1.
1911 It expects WORK_AREA to be a pointer.
1912 If it can't get the space, it returns from the surrounding function. */
1914 #define EXTEND_RANGE_TABLE(work_area, n) \
1916 if (((work_area).used + (n)) * sizeof (int) > (work_area).allocated) \
1918 extend_range_table_work_area (&work_area); \
1919 if ((work_area).table == 0) \
1920 return (REG_ESPACE); \
1924 #define SET_RANGE_TABLE_WORK_AREA_BIT(work_area, bit) \
1925 (work_area).bits |= (bit)
1927 /* Bits used to implement the multibyte-part of the various character classes
1928 such as [:alnum:] in a charset's range table. */
1929 #define BIT_WORD 0x1
1930 #define BIT_LOWER 0x2
1931 #define BIT_PUNCT 0x4
1932 #define BIT_SPACE 0x8
1933 #define BIT_UPPER 0x10
1934 #define BIT_MULTIBYTE 0x20
1936 /* Set a range (RANGE_START, RANGE_END) to WORK_AREA. */
1937 #define SET_RANGE_TABLE_WORK_AREA(work_area, range_start, range_end) \
1939 EXTEND_RANGE_TABLE ((work_area), 2); \
1940 (work_area).table[(work_area).used++] = (range_start); \
1941 (work_area).table[(work_area).used++] = (range_end); \
1944 /* Free allocated memory for WORK_AREA. */
1945 #define FREE_RANGE_TABLE_WORK_AREA(work_area) \
1947 if ((work_area).table) \
1948 free ((work_area).table); \
1951 #define CLEAR_RANGE_TABLE_WORK_USED(work_area) ((work_area).used = 0, (work_area).bits = 0)
1952 #define RANGE_TABLE_WORK_USED(work_area) ((work_area).used)
1953 #define RANGE_TABLE_WORK_BITS(work_area) ((work_area).bits)
1954 #define RANGE_TABLE_WORK_ELT(work_area, i) ((work_area).table[i])
1957 /* Set the bit for character C in a list. */
1958 #define SET_LIST_BIT(c) (b[((c)) / BYTEWIDTH] |= 1 << ((c) % BYTEWIDTH))
1963 /* Store characters in the rage range C0 to C1 in WORK_AREA while
1964 translating them and paying attention to the continuity of
1965 translated characters.
1967 Implementation note: It is better to implement this fairly big
1968 macro by a function, but it's not that easy because macros called
1969 in this macro assume various local variables already declared. */
1971 #define SETUP_MULTIBYTE_RANGE(work_area, c0, c1) \
1973 re_wchar_t c, t, t_last; \
1977 t_last = multibyte ? TRANSLATE (c) : TRANSLATE (MAKE_CHAR_MULTIBYTE (c)); \
1978 for (c++, n = 1; c <= (c1); c++, n++) \
1980 t = multibyte ? TRANSLATE (c) : TRANSLATE (MAKE_CHAR_MULTIBYTE (c)); \
1981 if (t_last + n == t) \
1983 SET_RANGE_TABLE_WORK_AREA ((work_area), t_last, t_last + n - 1); \
1988 SET_RANGE_TABLE_WORK_AREA ((work_area), t_last, t_last + n - 1); \
1993 /* Get the next unsigned number in the uncompiled pattern. */
1994 #define GET_UNSIGNED_NUMBER(num) \
1995 do { if (p != pend) \
1999 FREE_STACK_RETURN (REG_BADBR); \
2000 while ('0' <= c && c <= '9') \
2006 num = num * 10 + c - '0'; \
2007 if (num / 10 != prev) \
2008 FREE_STACK_RETURN (REG_BADBR); \
2014 FREE_STACK_RETURN (REG_BADBR); \
2018 #if ! WIDE_CHAR_SUPPORT
2020 /* Map a string to the char class it names (if any). */
2025 const char *string
= str
;
2026 if (STREQ (string
, "alnum")) return RECC_ALNUM
;
2027 else if (STREQ (string
, "alpha")) return RECC_ALPHA
;
2028 else if (STREQ (string
, "word")) return RECC_WORD
;
2029 else if (STREQ (string
, "ascii")) return RECC_ASCII
;
2030 else if (STREQ (string
, "nonascii")) return RECC_NONASCII
;
2031 else if (STREQ (string
, "graph")) return RECC_GRAPH
;
2032 else if (STREQ (string
, "lower")) return RECC_LOWER
;
2033 else if (STREQ (string
, "print")) return RECC_PRINT
;
2034 else if (STREQ (string
, "punct")) return RECC_PUNCT
;
2035 else if (STREQ (string
, "space")) return RECC_SPACE
;
2036 else if (STREQ (string
, "upper")) return RECC_UPPER
;
2037 else if (STREQ (string
, "unibyte")) return RECC_UNIBYTE
;
2038 else if (STREQ (string
, "multibyte")) return RECC_MULTIBYTE
;
2039 else if (STREQ (string
, "digit")) return RECC_DIGIT
;
2040 else if (STREQ (string
, "xdigit")) return RECC_XDIGIT
;
2041 else if (STREQ (string
, "cntrl")) return RECC_CNTRL
;
2042 else if (STREQ (string
, "blank")) return RECC_BLANK
;
2046 /* True iff CH is in the char class CC. */
2048 re_iswctype (ch
, cc
)
2054 case RECC_ALNUM
: return ISALNUM (ch
);
2055 case RECC_ALPHA
: return ISALPHA (ch
);
2056 case RECC_BLANK
: return ISBLANK (ch
);
2057 case RECC_CNTRL
: return ISCNTRL (ch
);
2058 case RECC_DIGIT
: return ISDIGIT (ch
);
2059 case RECC_GRAPH
: return ISGRAPH (ch
);
2060 case RECC_LOWER
: return ISLOWER (ch
);
2061 case RECC_PRINT
: return ISPRINT (ch
);
2062 case RECC_PUNCT
: return ISPUNCT (ch
);
2063 case RECC_SPACE
: return ISSPACE (ch
);
2064 case RECC_UPPER
: return ISUPPER (ch
);
2065 case RECC_XDIGIT
: return ISXDIGIT (ch
);
2066 case RECC_ASCII
: return IS_REAL_ASCII (ch
);
2067 case RECC_NONASCII
: return !IS_REAL_ASCII (ch
);
2068 case RECC_UNIBYTE
: return ISUNIBYTE (ch
);
2069 case RECC_MULTIBYTE
: return !ISUNIBYTE (ch
);
2070 case RECC_WORD
: return ISWORD (ch
);
2071 case RECC_ERROR
: return false;
2077 /* Return a bit-pattern to use in the range-table bits to match multibyte
2078 chars of class CC. */
2080 re_wctype_to_bit (cc
)
2085 case RECC_NONASCII
: case RECC_PRINT
: case RECC_GRAPH
:
2086 case RECC_MULTIBYTE
: return BIT_MULTIBYTE
;
2087 case RECC_ALPHA
: case RECC_ALNUM
: case RECC_WORD
: return BIT_WORD
;
2088 case RECC_LOWER
: return BIT_LOWER
;
2089 case RECC_UPPER
: return BIT_UPPER
;
2090 case RECC_PUNCT
: return BIT_PUNCT
;
2091 case RECC_SPACE
: return BIT_SPACE
;
2092 case RECC_ASCII
: case RECC_DIGIT
: case RECC_XDIGIT
: case RECC_CNTRL
:
2093 case RECC_BLANK
: case RECC_UNIBYTE
: case RECC_ERROR
: return 0;
2100 /* Filling in the work area of a range. */
2102 /* Actually extend the space in WORK_AREA. */
2105 extend_range_table_work_area (work_area
)
2106 struct range_table_work_area
*work_area
;
2108 work_area
->allocated
+= 16 * sizeof (int);
2109 if (work_area
->table
)
2111 = (int *) realloc (work_area
->table
, work_area
->allocated
);
2114 = (int *) malloc (work_area
->allocated
);
2120 /* Carefully find the ranges of codes that are equivalent
2121 under case conversion to the range start..end when passed through
2122 TRANSLATE. Handle the case where non-letters can come in between
2123 two upper-case letters (which happens in Latin-1).
2124 Also handle the case of groups of more than 2 case-equivalent chars.
2126 The basic method is to look at consecutive characters and see
2127 if they can form a run that can be handled as one.
2129 Returns -1 if successful, REG_ESPACE if ran out of space. */
2132 set_image_of_range_1 (work_area
, start
, end
, translate
)
2133 RE_TRANSLATE_TYPE translate
;
2134 struct range_table_work_area
*work_area
;
2135 re_wchar_t start
, end
;
2137 /* `one_case' indicates a character, or a run of characters,
2138 each of which is an isolate (no case-equivalents).
2139 This includes all ASCII non-letters.
2141 `two_case' indicates a character, or a run of characters,
2142 each of which has two case-equivalent forms.
2143 This includes all ASCII letters.
2145 `strange' indicates a character that has more than one
2148 enum case_type
{one_case
, two_case
, strange
};
2150 /* Describe the run that is in progress,
2151 which the next character can try to extend.
2152 If run_type is strange, that means there really is no run.
2153 If run_type is one_case, then run_start...run_end is the run.
2154 If run_type is two_case, then the run is run_start...run_end,
2155 and the case-equivalents end at run_eqv_end. */
2157 enum case_type run_type
= strange
;
2158 int run_start
, run_end
, run_eqv_end
;
2160 Lisp_Object eqv_table
;
2162 if (!RE_TRANSLATE_P (translate
))
2164 EXTEND_RANGE_TABLE (work_area
, 2);
2165 work_area
->table
[work_area
->used
++] = (start
);
2166 work_area
->table
[work_area
->used
++] = (end
);
2170 eqv_table
= XCHAR_TABLE (translate
)->extras
[2];
2172 for (; start
<= end
; start
++)
2174 enum case_type this_type
;
2175 int eqv
= RE_TRANSLATE (eqv_table
, start
);
2176 int minchar
, maxchar
;
2178 /* Classify this character */
2180 this_type
= one_case
;
2181 else if (RE_TRANSLATE (eqv_table
, eqv
) == start
)
2182 this_type
= two_case
;
2184 this_type
= strange
;
2187 minchar
= start
, maxchar
= eqv
;
2189 minchar
= eqv
, maxchar
= start
;
2191 /* Can this character extend the run in progress? */
2192 if (this_type
== strange
|| this_type
!= run_type
2193 || !(minchar
== run_end
+ 1
2194 && (run_type
== two_case
2195 ? maxchar
== run_eqv_end
+ 1 : 1)))
2198 Record each of its equivalent ranges. */
2199 if (run_type
== one_case
)
2201 EXTEND_RANGE_TABLE (work_area
, 2);
2202 work_area
->table
[work_area
->used
++] = run_start
;
2203 work_area
->table
[work_area
->used
++] = run_end
;
2205 else if (run_type
== two_case
)
2207 EXTEND_RANGE_TABLE (work_area
, 4);
2208 work_area
->table
[work_area
->used
++] = run_start
;
2209 work_area
->table
[work_area
->used
++] = run_end
;
2210 work_area
->table
[work_area
->used
++]
2211 = RE_TRANSLATE (eqv_table
, run_start
);
2212 work_area
->table
[work_area
->used
++]
2213 = RE_TRANSLATE (eqv_table
, run_end
);
2218 if (this_type
== strange
)
2220 /* For a strange character, add each of its equivalents, one
2221 by one. Don't start a range. */
2224 EXTEND_RANGE_TABLE (work_area
, 2);
2225 work_area
->table
[work_area
->used
++] = eqv
;
2226 work_area
->table
[work_area
->used
++] = eqv
;
2227 eqv
= RE_TRANSLATE (eqv_table
, eqv
);
2229 while (eqv
!= start
);
2232 /* Add this char to the run, or start a new run. */
2233 else if (run_type
== strange
)
2235 /* Initialize a new range. */
2236 run_type
= this_type
;
2239 run_eqv_end
= RE_TRANSLATE (eqv_table
, run_end
);
2243 /* Extend a running range. */
2245 run_eqv_end
= RE_TRANSLATE (eqv_table
, run_end
);
2249 /* If a run is still in progress at the end, finish it now
2250 by recording its equivalent ranges. */
2251 if (run_type
== one_case
)
2253 EXTEND_RANGE_TABLE (work_area
, 2);
2254 work_area
->table
[work_area
->used
++] = run_start
;
2255 work_area
->table
[work_area
->used
++] = run_end
;
2257 else if (run_type
== two_case
)
2259 EXTEND_RANGE_TABLE (work_area
, 4);
2260 work_area
->table
[work_area
->used
++] = run_start
;
2261 work_area
->table
[work_area
->used
++] = run_end
;
2262 work_area
->table
[work_area
->used
++]
2263 = RE_TRANSLATE (eqv_table
, run_start
);
2264 work_area
->table
[work_area
->used
++]
2265 = RE_TRANSLATE (eqv_table
, run_end
);
2273 /* Record the the image of the range start..end when passed through
2274 TRANSLATE. This is not necessarily TRANSLATE(start)..TRANSLATE(end)
2275 and is not even necessarily contiguous.
2276 Normally we approximate it with the smallest contiguous range that contains
2277 all the chars we need. However, for Latin-1 we go to extra effort
2280 This function is not called for ASCII ranges.
2282 Returns -1 if successful, REG_ESPACE if ran out of space. */
2285 set_image_of_range (work_area
, start
, end
, translate
)
2286 RE_TRANSLATE_TYPE translate
;
2287 struct range_table_work_area
*work_area
;
2288 re_wchar_t start
, end
;
2290 re_wchar_t cmin
, cmax
;
2293 /* For Latin-1 ranges, use set_image_of_range_1
2294 to get proper handling of ranges that include letters and nonletters.
2295 For a range that includes the whole of Latin-1, this is not necessary.
2296 For other character sets, we don't bother to get this right. */
2297 if (RE_TRANSLATE_P (translate
) && start
< 04400
2298 && !(start
< 04200 && end
>= 04377))
2305 tem
= set_image_of_range_1 (work_area
, start
, newend
, translate
);
2315 EXTEND_RANGE_TABLE (work_area
, 2);
2316 work_area
->table
[work_area
->used
++] = (start
);
2317 work_area
->table
[work_area
->used
++] = (end
);
2319 cmin
= -1, cmax
= -1;
2321 if (RE_TRANSLATE_P (translate
))
2325 for (ch
= start
; ch
<= end
; ch
++)
2327 re_wchar_t c
= TRANSLATE (ch
);
2328 if (! (start
<= c
&& c
<= end
))
2334 cmin
= MIN (cmin
, c
);
2335 cmax
= MAX (cmax
, c
);
2342 EXTEND_RANGE_TABLE (work_area
, 2);
2343 work_area
->table
[work_area
->used
++] = (cmin
);
2344 work_area
->table
[work_area
->used
++] = (cmax
);
2352 #ifndef MATCH_MAY_ALLOCATE
2354 /* If we cannot allocate large objects within re_match_2_internal,
2355 we make the fail stack and register vectors global.
2356 The fail stack, we grow to the maximum size when a regexp
2358 The register vectors, we adjust in size each time we
2359 compile a regexp, according to the number of registers it needs. */
2361 static fail_stack_type fail_stack
;
2363 /* Size with which the following vectors are currently allocated.
2364 That is so we can make them bigger as needed,
2365 but never make them smaller. */
2366 static int regs_allocated_size
;
2368 static re_char
** regstart
, ** regend
;
2369 static re_char
**best_regstart
, **best_regend
;
2371 /* Make the register vectors big enough for NUM_REGS registers,
2372 but don't make them smaller. */
2375 regex_grow_registers (num_regs
)
2378 if (num_regs
> regs_allocated_size
)
2380 RETALLOC_IF (regstart
, num_regs
, re_char
*);
2381 RETALLOC_IF (regend
, num_regs
, re_char
*);
2382 RETALLOC_IF (best_regstart
, num_regs
, re_char
*);
2383 RETALLOC_IF (best_regend
, num_regs
, re_char
*);
2385 regs_allocated_size
= num_regs
;
2389 #endif /* not MATCH_MAY_ALLOCATE */
2391 static boolean group_in_compile_stack
_RE_ARGS ((compile_stack_type
2395 /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
2396 Returns one of error codes defined in `regex.h', or zero for success.
2398 Assumes the `allocated' (and perhaps `buffer') and `translate'
2399 fields are set in BUFP on entry.
2401 If it succeeds, results are put in BUFP (if it returns an error, the
2402 contents of BUFP are undefined):
2403 `buffer' is the compiled pattern;
2404 `syntax' is set to SYNTAX;
2405 `used' is set to the length of the compiled pattern;
2406 `fastmap_accurate' is zero;
2407 `re_nsub' is the number of subexpressions in PATTERN;
2408 `not_bol' and `not_eol' are zero;
2410 The `fastmap' field is neither examined nor set. */
2412 /* Insert the `jump' from the end of last alternative to "here".
2413 The space for the jump has already been allocated. */
2414 #define FIXUP_ALT_JUMP() \
2416 if (fixup_alt_jump) \
2417 STORE_JUMP (jump, fixup_alt_jump, b); \
2421 /* Return, freeing storage we allocated. */
2422 #define FREE_STACK_RETURN(value) \
2424 FREE_RANGE_TABLE_WORK_AREA (range_table_work); \
2425 free (compile_stack.stack); \
2429 static reg_errcode_t
2430 regex_compile (pattern
, size
, syntax
, bufp
)
2433 reg_syntax_t syntax
;
2434 struct re_pattern_buffer
*bufp
;
2436 /* We fetch characters from PATTERN here. */
2437 register re_wchar_t c
, c1
;
2439 /* A random temporary spot in PATTERN. */
2442 /* Points to the end of the buffer, where we should append. */
2443 register unsigned char *b
;
2445 /* Keeps track of unclosed groups. */
2446 compile_stack_type compile_stack
;
2448 /* Points to the current (ending) position in the pattern. */
2450 /* `const' makes AIX compiler fail. */
2451 unsigned char *p
= pattern
;
2453 re_char
*p
= pattern
;
2455 re_char
*pend
= pattern
+ size
;
2457 /* How to translate the characters in the pattern. */
2458 RE_TRANSLATE_TYPE translate
= bufp
->translate
;
2460 /* Address of the count-byte of the most recently inserted `exactn'
2461 command. This makes it possible to tell if a new exact-match
2462 character can be added to that command or if the character requires
2463 a new `exactn' command. */
2464 unsigned char *pending_exact
= 0;
2466 /* Address of start of the most recently finished expression.
2467 This tells, e.g., postfix * where to find the start of its
2468 operand. Reset at the beginning of groups and alternatives. */
2469 unsigned char *laststart
= 0;
2471 /* Address of beginning of regexp, or inside of last group. */
2472 unsigned char *begalt
;
2474 /* Place in the uncompiled pattern (i.e., the {) to
2475 which to go back if the interval is invalid. */
2476 re_char
*beg_interval
;
2478 /* Address of the place where a forward jump should go to the end of
2479 the containing expression. Each alternative of an `or' -- except the
2480 last -- ends with a forward jump of this sort. */
2481 unsigned char *fixup_alt_jump
= 0;
2483 /* Counts open-groups as they are encountered. Remembered for the
2484 matching close-group on the compile stack, so the same register
2485 number is put in the stop_memory as the start_memory. */
2486 regnum_t regnum
= 0;
2488 /* Work area for range table of charset. */
2489 struct range_table_work_area range_table_work
;
2491 /* If the object matched can contain multibyte characters. */
2492 const boolean multibyte
= RE_MULTIBYTE_P (bufp
);
2494 /* If a target of matching can contain multibyte characters. */
2495 const boolean target_multibyte
= RE_TARGET_MULTIBYTE_P (bufp
);
2499 DEBUG_PRINT1 ("\nCompiling pattern: ");
2502 unsigned debug_count
;
2504 for (debug_count
= 0; debug_count
< size
; debug_count
++)
2505 putchar (pattern
[debug_count
]);
2510 /* Initialize the compile stack. */
2511 compile_stack
.stack
= TALLOC (INIT_COMPILE_STACK_SIZE
, compile_stack_elt_t
);
2512 if (compile_stack
.stack
== NULL
)
2515 compile_stack
.size
= INIT_COMPILE_STACK_SIZE
;
2516 compile_stack
.avail
= 0;
2518 range_table_work
.table
= 0;
2519 range_table_work
.allocated
= 0;
2521 /* Initialize the pattern buffer. */
2522 bufp
->syntax
= syntax
;
2523 bufp
->fastmap_accurate
= 0;
2524 bufp
->not_bol
= bufp
->not_eol
= 0;
2526 /* Set `used' to zero, so that if we return an error, the pattern
2527 printer (for debugging) will think there's no pattern. We reset it
2531 /* Always count groups, whether or not bufp->no_sub is set. */
2534 #if !defined emacs && !defined SYNTAX_TABLE
2535 /* Initialize the syntax table. */
2536 init_syntax_once ();
2539 if (bufp
->allocated
== 0)
2542 { /* If zero allocated, but buffer is non-null, try to realloc
2543 enough space. This loses if buffer's address is bogus, but
2544 that is the user's responsibility. */
2545 RETALLOC (bufp
->buffer
, INIT_BUF_SIZE
, unsigned char);
2548 { /* Caller did not allocate a buffer. Do it for them. */
2549 bufp
->buffer
= TALLOC (INIT_BUF_SIZE
, unsigned char);
2551 if (!bufp
->buffer
) FREE_STACK_RETURN (REG_ESPACE
);
2553 bufp
->allocated
= INIT_BUF_SIZE
;
2556 begalt
= b
= bufp
->buffer
;
2558 /* Loop through the uncompiled pattern until we're at the end. */
2567 if ( /* If at start of pattern, it's an operator. */
2569 /* If context independent, it's an operator. */
2570 || syntax
& RE_CONTEXT_INDEP_ANCHORS
2571 /* Otherwise, depends on what's come before. */
2572 || at_begline_loc_p (pattern
, p
, syntax
))
2573 BUF_PUSH ((syntax
& RE_NO_NEWLINE_ANCHOR
) ? begbuf
: begline
);
2582 if ( /* If at end of pattern, it's an operator. */
2584 /* If context independent, it's an operator. */
2585 || syntax
& RE_CONTEXT_INDEP_ANCHORS
2586 /* Otherwise, depends on what's next. */
2587 || at_endline_loc_p (p
, pend
, syntax
))
2588 BUF_PUSH ((syntax
& RE_NO_NEWLINE_ANCHOR
) ? endbuf
: endline
);
2597 if ((syntax
& RE_BK_PLUS_QM
)
2598 || (syntax
& RE_LIMITED_OPS
))
2602 /* If there is no previous pattern... */
2605 if (syntax
& RE_CONTEXT_INVALID_OPS
)
2606 FREE_STACK_RETURN (REG_BADRPT
);
2607 else if (!(syntax
& RE_CONTEXT_INDEP_OPS
))
2612 /* 1 means zero (many) matches is allowed. */
2613 boolean zero_times_ok
= 0, many_times_ok
= 0;
2616 /* If there is a sequence of repetition chars, collapse it
2617 down to just one (the right one). We can't combine
2618 interval operators with these because of, e.g., `a{2}*',
2619 which should only match an even number of `a's. */
2623 if ((syntax
& RE_FRUGAL
)
2624 && c
== '?' && (zero_times_ok
|| many_times_ok
))
2628 zero_times_ok
|= c
!= '+';
2629 many_times_ok
|= c
!= '?';
2635 || (!(syntax
& RE_BK_PLUS_QM
)
2636 && (*p
== '+' || *p
== '?')))
2638 else if (syntax
& RE_BK_PLUS_QM
&& *p
== '\\')
2641 FREE_STACK_RETURN (REG_EESCAPE
);
2642 if (p
[1] == '+' || p
[1] == '?')
2643 PATFETCH (c
); /* Gobble up the backslash. */
2649 /* If we get here, we found another repeat character. */
2653 /* Star, etc. applied to an empty pattern is equivalent
2654 to an empty pattern. */
2655 if (!laststart
|| laststart
== b
)
2658 /* Now we know whether or not zero matches is allowed
2659 and also whether or not two or more matches is allowed. */
2664 boolean simple
= skip_one_char (laststart
) == b
;
2665 unsigned int startoffset
= 0;
2667 /* Check if the loop can match the empty string. */
2668 (simple
|| !analyse_first (laststart
, b
, NULL
, 0))
2669 ? on_failure_jump
: on_failure_jump_loop
;
2670 assert (skip_one_char (laststart
) <= b
);
2672 if (!zero_times_ok
&& simple
)
2673 { /* Since simple * loops can be made faster by using
2674 on_failure_keep_string_jump, we turn simple P+
2675 into PP* if P is simple. */
2676 unsigned char *p1
, *p2
;
2677 startoffset
= b
- laststart
;
2678 GET_BUFFER_SPACE (startoffset
);
2679 p1
= b
; p2
= laststart
;
2685 GET_BUFFER_SPACE (6);
2688 STORE_JUMP (ofj
, b
, b
+ 6);
2690 /* Simple * loops can use on_failure_keep_string_jump
2691 depending on what follows. But since we don't know
2692 that yet, we leave the decision up to
2693 on_failure_jump_smart. */
2694 INSERT_JUMP (simple
? on_failure_jump_smart
: ofj
,
2695 laststart
+ startoffset
, b
+ 6);
2697 STORE_JUMP (jump
, b
, laststart
+ startoffset
);
2702 /* A simple ? pattern. */
2703 assert (zero_times_ok
);
2704 GET_BUFFER_SPACE (3);
2705 INSERT_JUMP (on_failure_jump
, laststart
, b
+ 3);
2709 else /* not greedy */
2710 { /* I wish the greedy and non-greedy cases could be merged. */
2712 GET_BUFFER_SPACE (7); /* We might use less. */
2715 boolean emptyp
= analyse_first (laststart
, b
, NULL
, 0);
2717 /* The non-greedy multiple match looks like
2718 a repeat..until: we only need a conditional jump
2719 at the end of the loop. */
2720 if (emptyp
) BUF_PUSH (no_op
);
2721 STORE_JUMP (emptyp
? on_failure_jump_nastyloop
2722 : on_failure_jump
, b
, laststart
);
2726 /* The repeat...until naturally matches one or more.
2727 To also match zero times, we need to first jump to
2728 the end of the loop (its conditional jump). */
2729 INSERT_JUMP (jump
, laststart
, b
);
2735 /* non-greedy a?? */
2736 INSERT_JUMP (jump
, laststart
, b
+ 3);
2738 INSERT_JUMP (on_failure_jump
, laststart
, laststart
+ 6);
2755 CLEAR_RANGE_TABLE_WORK_USED (range_table_work
);
2757 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2759 /* Ensure that we have enough space to push a charset: the
2760 opcode, the length count, and the bitset; 34 bytes in all. */
2761 GET_BUFFER_SPACE (34);
2765 /* We test `*p == '^' twice, instead of using an if
2766 statement, so we only need one BUF_PUSH. */
2767 BUF_PUSH (*p
== '^' ? charset_not
: charset
);
2771 /* Remember the first position in the bracket expression. */
2774 /* Push the number of bytes in the bitmap. */
2775 BUF_PUSH ((1 << BYTEWIDTH
) / BYTEWIDTH
);
2777 /* Clear the whole map. */
2778 bzero (b
, (1 << BYTEWIDTH
) / BYTEWIDTH
);
2780 /* charset_not matches newline according to a syntax bit. */
2781 if ((re_opcode_t
) b
[-2] == charset_not
2782 && (syntax
& RE_HAT_LISTS_NOT_NEWLINE
))
2783 SET_LIST_BIT ('\n');
2785 /* Read in characters and ranges, setting map bits. */
2788 boolean escaped_char
= false;
2789 const unsigned char *p2
= p
;
2791 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2793 /* Don't translate yet. The range TRANSLATE(X..Y) cannot
2794 always be determined from TRANSLATE(X) and TRANSLATE(Y)
2795 So the translation is done later in a loop. Example:
2796 (let ((case-fold-search t)) (string-match "[A-_]" "A")) */
2799 /* \ might escape characters inside [...] and [^...]. */
2800 if ((syntax
& RE_BACKSLASH_ESCAPE_IN_LISTS
) && c
== '\\')
2802 if (p
== pend
) FREE_STACK_RETURN (REG_EESCAPE
);
2805 escaped_char
= true;
2809 /* Could be the end of the bracket expression. If it's
2810 not (i.e., when the bracket expression is `[]' so
2811 far), the ']' character bit gets set way below. */
2812 if (c
== ']' && p2
!= p1
)
2816 /* See if we're at the beginning of a possible character
2819 if (!escaped_char
&&
2820 syntax
& RE_CHAR_CLASSES
&& c
== '[' && *p
== ':')
2822 /* Leave room for the null. */
2823 unsigned char str
[CHAR_CLASS_MAX_LENGTH
+ 1];
2824 const unsigned char *class_beg
;
2830 /* If pattern is `[[:'. */
2831 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2836 if ((c
== ':' && *p
== ']') || p
== pend
)
2838 if (c1
< CHAR_CLASS_MAX_LENGTH
)
2841 /* This is in any case an invalid class name. */
2846 /* If isn't a word bracketed by `[:' and `:]':
2847 undo the ending character, the letters, and
2848 leave the leading `:' and `[' (but set bits for
2850 if (c
== ':' && *p
== ']')
2856 cc
= re_wctype (str
);
2859 FREE_STACK_RETURN (REG_ECTYPE
);
2861 /* Throw away the ] at the end of the character
2865 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2867 /* Most character classes in a multibyte match
2868 just set a flag. Exceptions are is_blank,
2869 is_digit, is_cntrl, and is_xdigit, since
2870 they can only match ASCII characters. We
2871 don't need to handle them for multibyte.
2872 They are distinguished by a negative wctype. */
2874 for (ch
= 0; ch
< 128; ++ch
)
2875 if (re_iswctype (btowc (ch
), cc
))
2881 if (target_multibyte
)
2883 SET_RANGE_TABLE_WORK_AREA_BIT
2884 (range_table_work
, re_wctype_to_bit (cc
));
2888 for (ch
= 0; ch
< (1 << BYTEWIDTH
); ++ch
)
2891 MAKE_CHAR_MULTIBYTE (c
);
2892 if (re_iswctype (btowc (c
), cc
))
2895 MAKE_CHAR_UNIBYTE (c
);
2901 /* Repeat the loop. */
2906 /* Go back to right after the "[:". */
2910 /* Because the `:' may starts the range, we
2911 can't simply set bit and repeat the loop.
2912 Instead, just set it to C and handle below. */
2917 if (p
< pend
&& p
[0] == '-' && p
[1] != ']')
2920 /* Discard the `-'. */
2923 /* Fetch the character which ends the range. */
2927 if (syntax
& RE_NO_EMPTY_RANGES
)
2928 FREE_STACK_RETURN (REG_ERANGE
);
2929 /* Else, repeat the loop. */
2933 /* Range from C to C. */
2938 c1
= TRANSLATE (c1
);
2939 /* Set the range into bitmap */
2940 for (; c
<= c1
; c
++)
2941 SET_LIST_BIT (TRANSLATE (c
));
2942 #else /* not emacs */
2943 if (target_multibyte
)
2947 re_wchar_t c0
= MAX (c
, 128);
2949 SETUP_MULTIBYTE_RANGE (range_table_work
, c0
, c1
);
2952 for (; c
<= c1
; c
++)
2953 SET_LIST_BIT (TRANSLATE (c
));
2959 for (; c
<= c1
; c
++)
2963 MAKE_CHAR_MULTIBYTE (c0
);
2964 c0
= TRANSLATE (c0
);
2965 MAKE_CHAR_UNIBYTE (c0
);
2969 #endif /* not emacs */
2972 /* Discard any (non)matching list bytes that are all 0 at the
2973 end of the map. Decrease the map-length byte too. */
2974 while ((int) b
[-1] > 0 && b
[b
[-1] - 1] == 0)
2978 /* Build real range table from work area. */
2979 if (RANGE_TABLE_WORK_USED (range_table_work
)
2980 || RANGE_TABLE_WORK_BITS (range_table_work
))
2983 int used
= RANGE_TABLE_WORK_USED (range_table_work
);
2985 /* Allocate space for COUNT + RANGE_TABLE. Needs two
2986 bytes for flags, two for COUNT, and three bytes for
2988 GET_BUFFER_SPACE (4 + used
* 3);
2990 /* Indicate the existence of range table. */
2991 laststart
[1] |= 0x80;
2993 /* Store the character class flag bits into the range table.
2994 If not in emacs, these flag bits are always 0. */
2995 *b
++ = RANGE_TABLE_WORK_BITS (range_table_work
) & 0xff;
2996 *b
++ = RANGE_TABLE_WORK_BITS (range_table_work
) >> 8;
2998 STORE_NUMBER_AND_INCR (b
, used
/ 2);
2999 for (i
= 0; i
< used
; i
++)
3000 STORE_CHARACTER_AND_INCR
3001 (b
, RANGE_TABLE_WORK_ELT (range_table_work
, i
));
3008 if (syntax
& RE_NO_BK_PARENS
)
3015 if (syntax
& RE_NO_BK_PARENS
)
3022 if (syntax
& RE_NEWLINE_ALT
)
3029 if (syntax
& RE_NO_BK_VBAR
)
3036 if (syntax
& RE_INTERVALS
&& syntax
& RE_NO_BK_BRACES
)
3037 goto handle_interval
;
3043 if (p
== pend
) FREE_STACK_RETURN (REG_EESCAPE
);
3045 /* Do not translate the character after the \, so that we can
3046 distinguish, e.g., \B from \b, even if we normally would
3047 translate, e.g., B to b. */
3053 if (syntax
& RE_NO_BK_PARENS
)
3054 goto normal_backslash
;
3061 /* Look for a special (?...) construct */
3062 if ((syntax
& RE_SHY_GROUPS
) && *p
== '?')
3064 PATFETCH (c
); /* Gobble up the '?'. */
3068 case ':': shy
= 1; break;
3070 /* Only (?:...) is supported right now. */
3071 FREE_STACK_RETURN (REG_BADPAT
);
3082 if (COMPILE_STACK_FULL
)
3084 RETALLOC (compile_stack
.stack
, compile_stack
.size
<< 1,
3085 compile_stack_elt_t
);
3086 if (compile_stack
.stack
== NULL
) return REG_ESPACE
;
3088 compile_stack
.size
<<= 1;
3091 /* These are the values to restore when we hit end of this
3092 group. They are all relative offsets, so that if the
3093 whole pattern moves because of realloc, they will still
3095 COMPILE_STACK_TOP
.begalt_offset
= begalt
- bufp
->buffer
;
3096 COMPILE_STACK_TOP
.fixup_alt_jump
3097 = fixup_alt_jump
? fixup_alt_jump
- bufp
->buffer
+ 1 : 0;
3098 COMPILE_STACK_TOP
.laststart_offset
= b
- bufp
->buffer
;
3099 COMPILE_STACK_TOP
.regnum
= shy
? -regnum
: regnum
;
3102 start_memory for groups beyond the last one we can
3103 represent in the compiled pattern. */
3104 if (regnum
<= MAX_REGNUM
&& !shy
)
3105 BUF_PUSH_2 (start_memory
, regnum
);
3107 compile_stack
.avail
++;
3112 /* If we've reached MAX_REGNUM groups, then this open
3113 won't actually generate any code, so we'll have to
3114 clear pending_exact explicitly. */
3120 if (syntax
& RE_NO_BK_PARENS
) goto normal_backslash
;
3122 if (COMPILE_STACK_EMPTY
)
3124 if (syntax
& RE_UNMATCHED_RIGHT_PAREN_ORD
)
3125 goto normal_backslash
;
3127 FREE_STACK_RETURN (REG_ERPAREN
);
3133 /* See similar code for backslashed left paren above. */
3134 if (COMPILE_STACK_EMPTY
)
3136 if (syntax
& RE_UNMATCHED_RIGHT_PAREN_ORD
)
3139 FREE_STACK_RETURN (REG_ERPAREN
);
3142 /* Since we just checked for an empty stack above, this
3143 ``can't happen''. */
3144 assert (compile_stack
.avail
!= 0);
3146 /* We don't just want to restore into `regnum', because
3147 later groups should continue to be numbered higher,
3148 as in `(ab)c(de)' -- the second group is #2. */
3149 regnum_t this_group_regnum
;
3151 compile_stack
.avail
--;
3152 begalt
= bufp
->buffer
+ COMPILE_STACK_TOP
.begalt_offset
;
3154 = COMPILE_STACK_TOP
.fixup_alt_jump
3155 ? bufp
->buffer
+ COMPILE_STACK_TOP
.fixup_alt_jump
- 1
3157 laststart
= bufp
->buffer
+ COMPILE_STACK_TOP
.laststart_offset
;
3158 this_group_regnum
= COMPILE_STACK_TOP
.regnum
;
3159 /* If we've reached MAX_REGNUM groups, then this open
3160 won't actually generate any code, so we'll have to
3161 clear pending_exact explicitly. */
3164 /* We're at the end of the group, so now we know how many
3165 groups were inside this one. */
3166 if (this_group_regnum
<= MAX_REGNUM
&& this_group_regnum
> 0)
3167 BUF_PUSH_2 (stop_memory
, this_group_regnum
);
3172 case '|': /* `\|'. */
3173 if (syntax
& RE_LIMITED_OPS
|| syntax
& RE_NO_BK_VBAR
)
3174 goto normal_backslash
;
3176 if (syntax
& RE_LIMITED_OPS
)
3179 /* Insert before the previous alternative a jump which
3180 jumps to this alternative if the former fails. */
3181 GET_BUFFER_SPACE (3);
3182 INSERT_JUMP (on_failure_jump
, begalt
, b
+ 6);
3186 /* The alternative before this one has a jump after it
3187 which gets executed if it gets matched. Adjust that
3188 jump so it will jump to this alternative's analogous
3189 jump (put in below, which in turn will jump to the next
3190 (if any) alternative's such jump, etc.). The last such
3191 jump jumps to the correct final destination. A picture:
3197 If we are at `b', then fixup_alt_jump right now points to a
3198 three-byte space after `a'. We'll put in the jump, set
3199 fixup_alt_jump to right after `b', and leave behind three
3200 bytes which we'll fill in when we get to after `c'. */
3204 /* Mark and leave space for a jump after this alternative,
3205 to be filled in later either by next alternative or
3206 when know we're at the end of a series of alternatives. */
3208 GET_BUFFER_SPACE (3);
3217 /* If \{ is a literal. */
3218 if (!(syntax
& RE_INTERVALS
)
3219 /* If we're at `\{' and it's not the open-interval
3221 || (syntax
& RE_NO_BK_BRACES
))
3222 goto normal_backslash
;
3226 /* If got here, then the syntax allows intervals. */
3228 /* At least (most) this many matches must be made. */
3229 int lower_bound
= 0, upper_bound
= -1;
3234 FREE_STACK_RETURN (REG_EBRACE
);
3236 GET_UNSIGNED_NUMBER (lower_bound
);
3239 GET_UNSIGNED_NUMBER (upper_bound
);
3241 /* Interval such as `{1}' => match exactly once. */
3242 upper_bound
= lower_bound
;
3244 if (lower_bound
< 0 || upper_bound
> RE_DUP_MAX
3245 || (upper_bound
>= 0 && lower_bound
> upper_bound
))
3246 FREE_STACK_RETURN (REG_BADBR
);
3248 if (!(syntax
& RE_NO_BK_BRACES
))
3251 FREE_STACK_RETURN (REG_BADBR
);
3257 FREE_STACK_RETURN (REG_BADBR
);
3259 /* We just parsed a valid interval. */
3261 /* If it's invalid to have no preceding re. */
3264 if (syntax
& RE_CONTEXT_INVALID_OPS
)
3265 FREE_STACK_RETURN (REG_BADRPT
);
3266 else if (syntax
& RE_CONTEXT_INDEP_OPS
)
3269 goto unfetch_interval
;
3272 if (upper_bound
== 0)
3273 /* If the upper bound is zero, just drop the sub pattern
3276 else if (lower_bound
== 1 && upper_bound
== 1)
3277 /* Just match it once: nothing to do here. */
3280 /* Otherwise, we have a nontrivial interval. When
3281 we're all done, the pattern will look like:
3282 set_number_at <jump count> <upper bound>
3283 set_number_at <succeed_n count> <lower bound>
3284 succeed_n <after jump addr> <succeed_n count>
3286 jump_n <succeed_n addr> <jump count>
3287 (The upper bound and `jump_n' are omitted if
3288 `upper_bound' is 1, though.) */
3290 { /* If the upper bound is > 1, we need to insert
3291 more at the end of the loop. */
3292 unsigned int nbytes
= (upper_bound
< 0 ? 3
3293 : upper_bound
> 1 ? 5 : 0);
3294 unsigned int startoffset
= 0;
3296 GET_BUFFER_SPACE (20); /* We might use less. */
3298 if (lower_bound
== 0)
3300 /* A succeed_n that starts with 0 is really a
3301 a simple on_failure_jump_loop. */
3302 INSERT_JUMP (on_failure_jump_loop
, laststart
,
3308 /* Initialize lower bound of the `succeed_n', even
3309 though it will be set during matching by its
3310 attendant `set_number_at' (inserted next),
3311 because `re_compile_fastmap' needs to know.
3312 Jump to the `jump_n' we might insert below. */
3313 INSERT_JUMP2 (succeed_n
, laststart
,
3318 /* Code to initialize the lower bound. Insert
3319 before the `succeed_n'. The `5' is the last two
3320 bytes of this `set_number_at', plus 3 bytes of
3321 the following `succeed_n'. */
3322 insert_op2 (set_number_at
, laststart
, 5, lower_bound
, b
);
3327 if (upper_bound
< 0)
3329 /* A negative upper bound stands for infinity,
3330 in which case it degenerates to a plain jump. */
3331 STORE_JUMP (jump
, b
, laststart
+ startoffset
);
3334 else if (upper_bound
> 1)
3335 { /* More than one repetition is allowed, so
3336 append a backward jump to the `succeed_n'
3337 that starts this interval.
3339 When we've reached this during matching,
3340 we'll have matched the interval once, so
3341 jump back only `upper_bound - 1' times. */
3342 STORE_JUMP2 (jump_n
, b
, laststart
+ startoffset
,
3346 /* The location we want to set is the second
3347 parameter of the `jump_n'; that is `b-2' as
3348 an absolute address. `laststart' will be
3349 the `set_number_at' we're about to insert;
3350 `laststart+3' the number to set, the source
3351 for the relative address. But we are
3352 inserting into the middle of the pattern --
3353 so everything is getting moved up by 5.
3354 Conclusion: (b - 2) - (laststart + 3) + 5,
3355 i.e., b - laststart.
3357 We insert this at the beginning of the loop
3358 so that if we fail during matching, we'll
3359 reinitialize the bounds. */
3360 insert_op2 (set_number_at
, laststart
, b
- laststart
,
3361 upper_bound
- 1, b
);
3366 beg_interval
= NULL
;
3371 /* If an invalid interval, match the characters as literals. */
3372 assert (beg_interval
);
3374 beg_interval
= NULL
;
3376 /* normal_char and normal_backslash need `c'. */
3379 if (!(syntax
& RE_NO_BK_BRACES
))
3381 assert (p
> pattern
&& p
[-1] == '\\');
3382 goto normal_backslash
;
3388 /* There is no way to specify the before_dot and after_dot
3389 operators. rms says this is ok. --karl */
3397 BUF_PUSH_2 (syntaxspec
, syntax_spec_code
[c
]);
3403 BUF_PUSH_2 (notsyntaxspec
, syntax_spec_code
[c
]);
3409 BUF_PUSH_2 (categoryspec
, c
);
3415 BUF_PUSH_2 (notcategoryspec
, c
);
3421 if (syntax
& RE_NO_GNU_OPS
)
3424 BUF_PUSH_2 (syntaxspec
, Sword
);
3429 if (syntax
& RE_NO_GNU_OPS
)
3432 BUF_PUSH_2 (notsyntaxspec
, Sword
);
3437 if (syntax
& RE_NO_GNU_OPS
)
3443 if (syntax
& RE_NO_GNU_OPS
)
3449 if (syntax
& RE_NO_GNU_OPS
)
3458 FREE_STACK_RETURN (REG_BADPAT
);
3462 if (syntax
& RE_NO_GNU_OPS
)
3464 BUF_PUSH (wordbound
);
3468 if (syntax
& RE_NO_GNU_OPS
)
3470 BUF_PUSH (notwordbound
);
3474 if (syntax
& RE_NO_GNU_OPS
)
3480 if (syntax
& RE_NO_GNU_OPS
)
3485 case '1': case '2': case '3': case '4': case '5':
3486 case '6': case '7': case '8': case '9':
3490 if (syntax
& RE_NO_BK_REFS
)
3491 goto normal_backslash
;
3495 /* Can't back reference to a subexpression before its end. */
3496 if (reg
> regnum
|| group_in_compile_stack (compile_stack
, reg
))
3497 FREE_STACK_RETURN (REG_ESUBREG
);
3500 BUF_PUSH_2 (duplicate
, reg
);
3507 if (syntax
& RE_BK_PLUS_QM
)
3510 goto normal_backslash
;
3514 /* You might think it would be useful for \ to mean
3515 not to translate; but if we don't translate it
3516 it will never match anything. */
3523 /* Expects the character in `c'. */
3525 /* If no exactn currently being built. */
3528 /* If last exactn not at current position. */
3529 || pending_exact
+ *pending_exact
+ 1 != b
3531 /* We have only one byte following the exactn for the count. */
3532 || *pending_exact
>= (1 << BYTEWIDTH
) - MAX_MULTIBYTE_LENGTH
3534 /* If followed by a repetition operator. */
3535 || (p
!= pend
&& (*p
== '*' || *p
== '^'))
3536 || ((syntax
& RE_BK_PLUS_QM
)
3537 ? p
+ 1 < pend
&& *p
== '\\' && (p
[1] == '+' || p
[1] == '?')
3538 : p
!= pend
&& (*p
== '+' || *p
== '?'))
3539 || ((syntax
& RE_INTERVALS
)
3540 && ((syntax
& RE_NO_BK_BRACES
)
3541 ? p
!= pend
&& *p
== '{'
3542 : p
+ 1 < pend
&& p
[0] == '\\' && p
[1] == '{')))
3544 /* Start building a new exactn. */
3548 BUF_PUSH_2 (exactn
, 0);
3549 pending_exact
= b
- 1;
3552 GET_BUFFER_SPACE (MAX_MULTIBYTE_LENGTH
);
3557 MAKE_CHAR_MULTIBYTE (c
);
3559 if (target_multibyte
)
3561 len
= CHAR_STRING (c
, b
);
3566 MAKE_CHAR_UNIBYTE (c
);
3570 (*pending_exact
) += len
;
3575 } /* while p != pend */
3578 /* Through the pattern now. */
3582 if (!COMPILE_STACK_EMPTY
)
3583 FREE_STACK_RETURN (REG_EPAREN
);
3585 /* If we don't want backtracking, force success
3586 the first time we reach the end of the compiled pattern. */
3587 if (syntax
& RE_NO_POSIX_BACKTRACKING
)
3590 /* We have succeeded; set the length of the buffer. */
3591 bufp
->used
= b
- bufp
->buffer
;
3594 /* Now the buffer is adjusted for the multibyteness of a target. */
3595 bufp
->multibyte
= bufp
->target_multibyte
;
3601 re_compile_fastmap (bufp
);
3602 DEBUG_PRINT1 ("\nCompiled pattern: \n");
3603 print_compiled_pattern (bufp
);
3608 #ifndef MATCH_MAY_ALLOCATE
3609 /* Initialize the failure stack to the largest possible stack. This
3610 isn't necessary unless we're trying to avoid calling alloca in
3611 the search and match routines. */
3613 int num_regs
= bufp
->re_nsub
+ 1;
3615 if (fail_stack
.size
< re_max_failures
* TYPICAL_FAILURE_SIZE
)
3617 fail_stack
.size
= re_max_failures
* TYPICAL_FAILURE_SIZE
;
3619 if (! fail_stack
.stack
)
3621 = (fail_stack_elt_t
*) malloc (fail_stack
.size
3622 * sizeof (fail_stack_elt_t
));
3625 = (fail_stack_elt_t
*) realloc (fail_stack
.stack
,
3627 * sizeof (fail_stack_elt_t
)));
3630 regex_grow_registers (num_regs
);
3632 #endif /* not MATCH_MAY_ALLOCATE */
3634 FREE_STACK_RETURN (REG_NOERROR
);
3635 } /* regex_compile */
3637 /* Subroutines for `regex_compile'. */
3639 /* Store OP at LOC followed by two-byte integer parameter ARG. */
3642 store_op1 (op
, loc
, arg
)
3647 *loc
= (unsigned char) op
;
3648 STORE_NUMBER (loc
+ 1, arg
);
3652 /* Like `store_op1', but for two two-byte parameters ARG1 and ARG2. */
3655 store_op2 (op
, loc
, arg1
, arg2
)
3660 *loc
= (unsigned char) op
;
3661 STORE_NUMBER (loc
+ 1, arg1
);
3662 STORE_NUMBER (loc
+ 3, arg2
);
3666 /* Copy the bytes from LOC to END to open up three bytes of space at LOC
3667 for OP followed by two-byte integer parameter ARG. */
3670 insert_op1 (op
, loc
, arg
, end
)
3676 register unsigned char *pfrom
= end
;
3677 register unsigned char *pto
= end
+ 3;
3679 while (pfrom
!= loc
)
3682 store_op1 (op
, loc
, arg
);
3686 /* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2. */
3689 insert_op2 (op
, loc
, arg1
, arg2
, end
)
3695 register unsigned char *pfrom
= end
;
3696 register unsigned char *pto
= end
+ 5;
3698 while (pfrom
!= loc
)
3701 store_op2 (op
, loc
, arg1
, arg2
);
3705 /* P points to just after a ^ in PATTERN. Return true if that ^ comes
3706 after an alternative or a begin-subexpression. We assume there is at
3707 least one character before the ^. */
3710 at_begline_loc_p (pattern
, p
, syntax
)
3711 re_char
*pattern
, *p
;
3712 reg_syntax_t syntax
;
3714 re_char
*prev
= p
- 2;
3715 boolean prev_prev_backslash
= prev
> pattern
&& prev
[-1] == '\\';
3718 /* After a subexpression? */
3719 (*prev
== '(' && (syntax
& RE_NO_BK_PARENS
|| prev_prev_backslash
))
3720 /* After an alternative? */
3721 || (*prev
== '|' && (syntax
& RE_NO_BK_VBAR
|| prev_prev_backslash
))
3722 /* After a shy subexpression? */
3723 || ((syntax
& RE_SHY_GROUPS
) && prev
- 2 >= pattern
3724 && prev
[-1] == '?' && prev
[-2] == '('
3725 && (syntax
& RE_NO_BK_PARENS
3726 || (prev
- 3 >= pattern
&& prev
[-3] == '\\')));
3730 /* The dual of at_begline_loc_p. This one is for $. We assume there is
3731 at least one character after the $, i.e., `P < PEND'. */
3734 at_endline_loc_p (p
, pend
, syntax
)
3736 reg_syntax_t syntax
;
3739 boolean next_backslash
= *next
== '\\';
3740 re_char
*next_next
= p
+ 1 < pend
? p
+ 1 : 0;
3743 /* Before a subexpression? */
3744 (syntax
& RE_NO_BK_PARENS
? *next
== ')'
3745 : next_backslash
&& next_next
&& *next_next
== ')')
3746 /* Before an alternative? */
3747 || (syntax
& RE_NO_BK_VBAR
? *next
== '|'
3748 : next_backslash
&& next_next
&& *next_next
== '|');
3752 /* Returns true if REGNUM is in one of COMPILE_STACK's elements and
3753 false if it's not. */
3756 group_in_compile_stack (compile_stack
, regnum
)
3757 compile_stack_type compile_stack
;
3762 for (this_element
= compile_stack
.avail
- 1;
3765 if (compile_stack
.stack
[this_element
].regnum
== regnum
)
3772 If fastmap is non-NULL, go through the pattern and fill fastmap
3773 with all the possible leading chars. If fastmap is NULL, don't
3774 bother filling it up (obviously) and only return whether the
3775 pattern could potentially match the empty string.
3777 Return 1 if p..pend might match the empty string.
3778 Return 0 if p..pend matches at least one char.
3779 Return -1 if fastmap was not updated accurately. */
3782 analyse_first (p
, pend
, fastmap
, multibyte
)
3785 const int multibyte
;
3790 /* If all elements for base leading-codes in fastmap is set, this
3791 flag is set true. */
3792 boolean match_any_multibyte_characters
= false;
3796 /* The loop below works as follows:
3797 - It has a working-list kept in the PATTERN_STACK and which basically
3798 starts by only containing a pointer to the first operation.
3799 - If the opcode we're looking at is a match against some set of
3800 chars, then we add those chars to the fastmap and go on to the
3801 next work element from the worklist (done via `break').
3802 - If the opcode is a control operator on the other hand, we either
3803 ignore it (if it's meaningless at this point, such as `start_memory')
3804 or execute it (if it's a jump). If the jump has several destinations
3805 (i.e. `on_failure_jump'), then we push the other destination onto the
3807 We guarantee termination by ignoring backward jumps (more or less),
3808 so that `p' is monotonically increasing. More to the point, we
3809 never set `p' (or push) anything `<= p1'. */
3813 /* `p1' is used as a marker of how far back a `on_failure_jump'
3814 can go without being ignored. It is normally equal to `p'
3815 (which prevents any backward `on_failure_jump') except right
3816 after a plain `jump', to allow patterns such as:
3819 10: on_failure_jump 3
3820 as used for the *? operator. */
3823 switch (SWITCH_ENUM_CAST ((re_opcode_t
) *p
++))
3830 /* If the first character has to match a backreference, that means
3831 that the group was empty (since it already matched). Since this
3832 is the only case that interests us here, we can assume that the
3833 backreference must match the empty string. */
3838 /* Following are the cases which match a character. These end
3843 /* If multibyte is nonzero, the first byte of each
3844 character is an ASCII or a leading code. Otherwise,
3845 each byte is a character. Thus, this works in both
3852 /* We could put all the chars except for \n (and maybe \0)
3853 but we don't bother since it is generally not worth it. */
3854 if (!fastmap
) break;
3859 if (!fastmap
) break;
3861 /* Chars beyond end of bitmap are possible matches. */
3862 /* In a multibyte case, the bitmap is used only for ASCII
3864 int limit
= multibyte
? 128 : (1 << BYTEWIDTH
);
3866 for (j
= CHARSET_BITMAP_SIZE (&p
[-1]) * BYTEWIDTH
;
3873 if (!fastmap
) break;
3874 not = (re_opcode_t
) *(p
- 1) == charset_not
;
3875 for (j
= CHARSET_BITMAP_SIZE (&p
[-1]) * BYTEWIDTH
- 1, p
++;
3877 if (!!(p
[j
/ BYTEWIDTH
] & (1 << (j
% BYTEWIDTH
))) ^ not)
3880 if ((not && multibyte
)
3881 /* Any leading code can possibly start a character
3882 which doesn't match the specified set of characters. */
3883 || (CHARSET_RANGE_TABLE_EXISTS_P (&p
[-2])
3884 && CHARSET_RANGE_TABLE_BITS (&p
[-2]) != 0))
3885 /* If we can match a character class, we can match
3886 any multibyte characters. */
3888 if (match_any_multibyte_characters
== false)
3890 for (j
= 0x80; j
< (1 << BYTEWIDTH
); j
++)
3892 match_any_multibyte_characters
= true;
3896 else if (!not && CHARSET_RANGE_TABLE_EXISTS_P (&p
[-2])
3897 && match_any_multibyte_characters
== false)
3899 /* Set fastmap[I] to 1 where I is a leading code of each
3900 multibyte characer in the range table. */
3902 unsigned char lc1
, lc2
;
3904 /* Make P points the range table. `+ 2' is to skip flag
3905 bits for a character class. */
3906 p
+= CHARSET_BITMAP_SIZE (&p
[-2]) + 2;
3908 /* Extract the number of ranges in range table into COUNT. */
3909 EXTRACT_NUMBER_AND_INCR (count
, p
);
3910 for (; count
> 0; count
--, p
+= 2 * 3) /* XXX */
3912 /* Extract the start and end of each range. */
3913 EXTRACT_CHARACTER (c
, p
);
3914 lc1
= CHAR_LEADING_CODE (c
);
3916 EXTRACT_CHARACTER (c
, p
);
3917 lc2
= CHAR_LEADING_CODE (c
);
3918 for (j
= lc1
; j
<= lc2
; j
++)
3926 if (!fastmap
) break;
3928 not = (re_opcode_t
)p
[-1] == notsyntaxspec
;
3930 for (j
= 0; j
< (1 << BYTEWIDTH
); j
++)
3931 if ((SYNTAX (j
) == (enum syntaxcode
) k
) ^ not)
3935 /* This match depends on text properties. These end with
3936 aborting optimizations. */
3940 case notcategoryspec
:
3941 if (!fastmap
) break;
3942 not = (re_opcode_t
)p
[-1] == notcategoryspec
;
3944 for (j
= (multibyte
? 127 : (1 << BYTEWIDTH
)); j
>= 0; j
--)
3945 if ((CHAR_HAS_CATEGORY (j
, k
)) ^ not)
3950 /* Any character set can possibly contain a character
3951 whose category is K (or not). */
3952 if (match_any_multibyte_characters
== false)
3954 for (j
= 0x80; j
< (1 << BYTEWIDTH
); j
++)
3956 match_any_multibyte_characters
= true;
3961 /* All cases after this match the empty string. These end with
3983 EXTRACT_NUMBER_AND_INCR (j
, p
);
3985 /* Backward jumps can only go back to code that we've already
3986 visited. `re_compile' should make sure this is true. */
3989 switch (SWITCH_ENUM_CAST ((re_opcode_t
) *p
))
3991 case on_failure_jump
:
3992 case on_failure_keep_string_jump
:
3993 case on_failure_jump_loop
:
3994 case on_failure_jump_nastyloop
:
3995 case on_failure_jump_smart
:
4001 /* Keep `p1' to allow the `on_failure_jump' we are jumping to
4002 to jump back to "just after here". */
4005 case on_failure_jump
:
4006 case on_failure_keep_string_jump
:
4007 case on_failure_jump_nastyloop
:
4008 case on_failure_jump_loop
:
4009 case on_failure_jump_smart
:
4010 EXTRACT_NUMBER_AND_INCR (j
, p
);
4012 ; /* Backward jump to be ignored. */
4014 { /* We have to look down both arms.
4015 We first go down the "straight" path so as to minimize
4016 stack usage when going through alternatives. */
4017 int r
= analyse_first (p
, pend
, fastmap
, multibyte
);
4025 /* This code simply does not properly handle forward jump_n. */
4026 DEBUG_STATEMENT (EXTRACT_NUMBER (j
, p
); assert (j
< 0));
4028 /* jump_n can either jump or fall through. The (backward) jump
4029 case has already been handled, so we only need to look at the
4030 fallthrough case. */
4034 /* If N == 0, it should be an on_failure_jump_loop instead. */
4035 DEBUG_STATEMENT (EXTRACT_NUMBER (j
, p
+ 2); assert (j
> 0));
4037 /* We only care about one iteration of the loop, so we don't
4038 need to consider the case where this behaves like an
4055 abort (); /* We have listed all the cases. */
4058 /* Getting here means we have found the possible starting
4059 characters for one path of the pattern -- and that the empty
4060 string does not match. We need not follow this path further. */
4064 /* We reached the end without matching anything. */
4067 } /* analyse_first */
4069 /* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
4070 BUFP. A fastmap records which of the (1 << BYTEWIDTH) possible
4071 characters can start a string that matches the pattern. This fastmap
4072 is used by re_search to skip quickly over impossible starting points.
4074 Character codes above (1 << BYTEWIDTH) are not represented in the
4075 fastmap, but the leading codes are represented. Thus, the fastmap
4076 indicates which character sets could start a match.
4078 The caller must supply the address of a (1 << BYTEWIDTH)-byte data
4079 area as BUFP->fastmap.
4081 We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in
4084 Returns 0 if we succeed, -2 if an internal error. */
4087 re_compile_fastmap (bufp
)
4088 struct re_pattern_buffer
*bufp
;
4090 char *fastmap
= bufp
->fastmap
;
4093 assert (fastmap
&& bufp
->buffer
);
4095 bzero (fastmap
, 1 << BYTEWIDTH
); /* Assume nothing's valid. */
4096 bufp
->fastmap_accurate
= 1; /* It will be when we're done. */
4098 analysis
= analyse_first (bufp
->buffer
, bufp
->buffer
+ bufp
->used
,
4099 fastmap
, RE_MULTIBYTE_P (bufp
));
4100 bufp
->can_be_null
= (analysis
!= 0);
4102 } /* re_compile_fastmap */
4104 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
4105 ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
4106 this memory for recording register information. STARTS and ENDS
4107 must be allocated using the malloc library routine, and must each
4108 be at least NUM_REGS * sizeof (regoff_t) bytes long.
4110 If NUM_REGS == 0, then subsequent matches should allocate their own
4113 Unless this function is called, the first search or match using
4114 PATTERN_BUFFER will allocate its own register data, without
4115 freeing the old data. */
4118 re_set_registers (bufp
, regs
, num_regs
, starts
, ends
)
4119 struct re_pattern_buffer
*bufp
;
4120 struct re_registers
*regs
;
4122 regoff_t
*starts
, *ends
;
4126 bufp
->regs_allocated
= REGS_REALLOCATE
;
4127 regs
->num_regs
= num_regs
;
4128 regs
->start
= starts
;
4133 bufp
->regs_allocated
= REGS_UNALLOCATED
;
4135 regs
->start
= regs
->end
= (regoff_t
*) 0;
4138 WEAK_ALIAS (__re_set_registers
, re_set_registers
)
4140 /* Searching routines. */
4142 /* Like re_search_2, below, but only one string is specified, and
4143 doesn't let you say where to stop matching. */
4146 re_search (bufp
, string
, size
, startpos
, range
, regs
)
4147 struct re_pattern_buffer
*bufp
;
4149 int size
, startpos
, range
;
4150 struct re_registers
*regs
;
4152 return re_search_2 (bufp
, NULL
, 0, string
, size
, startpos
, range
,
4155 WEAK_ALIAS (__re_search
, re_search
)
4157 /* Head address of virtual concatenation of string. */
4158 #define HEAD_ADDR_VSTRING(P) \
4159 (((P) >= size1 ? string2 : string1))
4161 /* End address of virtual concatenation of string. */
4162 #define STOP_ADDR_VSTRING(P) \
4163 (((P) >= size1 ? string2 + size2 : string1 + size1))
4165 /* Address of POS in the concatenation of virtual string. */
4166 #define POS_ADDR_VSTRING(POS) \
4167 (((POS) >= size1 ? string2 - size1 : string1) + (POS))
4169 /* Using the compiled pattern in BUFP->buffer, first tries to match the
4170 virtual concatenation of STRING1 and STRING2, starting first at index
4171 STARTPOS, then at STARTPOS + 1, and so on.
4173 STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
4175 RANGE is how far to scan while trying to match. RANGE = 0 means try
4176 only at STARTPOS; in general, the last start tried is STARTPOS +
4179 In REGS, return the indices of the virtual concatenation of STRING1
4180 and STRING2 that matched the entire BUFP->buffer and its contained
4183 Do not consider matching one past the index STOP in the virtual
4184 concatenation of STRING1 and STRING2.
4186 We return either the position in the strings at which the match was
4187 found, -1 if no match, or -2 if error (such as failure
4191 re_search_2 (bufp
, str1
, size1
, str2
, size2
, startpos
, range
, regs
, stop
)
4192 struct re_pattern_buffer
*bufp
;
4193 const char *str1
, *str2
;
4197 struct re_registers
*regs
;
4201 re_char
*string1
= (re_char
*) str1
;
4202 re_char
*string2
= (re_char
*) str2
;
4203 register char *fastmap
= bufp
->fastmap
;
4204 register RE_TRANSLATE_TYPE translate
= bufp
->translate
;
4205 int total_size
= size1
+ size2
;
4206 int endpos
= startpos
+ range
;
4207 boolean anchored_start
;
4208 /* Nonzero if BUFP is setup for multibyte characters. We are sure
4209 that it is the same as RE_TARGET_MULTIBYTE_P (bufp). */
4210 const boolean multibyte
= RE_MULTIBYTE_P (bufp
);
4212 /* Check for out-of-range STARTPOS. */
4213 if (startpos
< 0 || startpos
> total_size
)
4216 /* Fix up RANGE if it might eventually take us outside
4217 the virtual concatenation of STRING1 and STRING2.
4218 Make sure we won't move STARTPOS below 0 or above TOTAL_SIZE. */
4220 range
= 0 - startpos
;
4221 else if (endpos
> total_size
)
4222 range
= total_size
- startpos
;
4224 /* If the search isn't to be a backwards one, don't waste time in a
4225 search for a pattern anchored at beginning of buffer. */
4226 if (bufp
->used
> 0 && (re_opcode_t
) bufp
->buffer
[0] == begbuf
&& range
> 0)
4235 /* In a forward search for something that starts with \=.
4236 don't keep searching past point. */
4237 if (bufp
->used
> 0 && (re_opcode_t
) bufp
->buffer
[0] == at_dot
&& range
> 0)
4239 range
= PT_BYTE
- BEGV_BYTE
- startpos
;
4245 /* Update the fastmap now if not correct already. */
4246 if (fastmap
&& !bufp
->fastmap_accurate
)
4247 re_compile_fastmap (bufp
);
4249 /* See whether the pattern is anchored. */
4250 anchored_start
= (bufp
->buffer
[0] == begline
);
4253 gl_state
.object
= re_match_object
;
4255 int charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (POS_AS_IN_BUFFER (startpos
));
4257 SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object
, charpos
, 1);
4261 /* Loop through the string, looking for a place to start matching. */
4264 /* If the pattern is anchored,
4265 skip quickly past places we cannot match.
4266 We don't bother to treat startpos == 0 specially
4267 because that case doesn't repeat. */
4268 if (anchored_start
&& startpos
> 0)
4270 if (! ((startpos
<= size1
? string1
[startpos
- 1]
4271 : string2
[startpos
- size1
- 1])
4276 /* If a fastmap is supplied, skip quickly over characters that
4277 cannot be the start of a match. If the pattern can match the
4278 null string, however, we don't need to skip characters; we want
4279 the first null string. */
4280 if (fastmap
&& startpos
< total_size
&& !bufp
->can_be_null
)
4282 register re_char
*d
;
4283 register re_wchar_t buf_ch
;
4285 d
= POS_ADDR_VSTRING (startpos
);
4287 if (range
> 0) /* Searching forwards. */
4289 register int lim
= 0;
4292 if (startpos
< size1
&& startpos
+ range
>= size1
)
4293 lim
= range
- (size1
- startpos
);
4295 /* Written out as an if-else to avoid testing `translate'
4297 if (RE_TRANSLATE_P (translate
))
4304 buf_ch
= STRING_CHAR_AND_LENGTH (d
, range
- lim
,
4306 buf_ch
= RE_TRANSLATE (translate
, buf_ch
);
4307 if (fastmap
[CHAR_LEADING_CODE (buf_ch
)])
4310 range
-= buf_charlen
;
4317 MAKE_CHAR_MULTIBYTE (buf_ch
);
4318 buf_ch
= RE_TRANSLATE (translate
, buf_ch
);
4319 MAKE_CHAR_UNIBYTE (buf_ch
);
4320 if (fastmap
[buf_ch
])
4333 buf_ch
= STRING_CHAR_AND_LENGTH (d
, range
- lim
,
4335 if (fastmap
[CHAR_LEADING_CODE (buf_ch
)])
4337 range
-= buf_charlen
;
4341 while (range
> lim
&& !fastmap
[*d
])
4347 startpos
+= irange
- range
;
4349 else /* Searching backwards. */
4351 int room
= (startpos
>= size1
4352 ? size2
+ size1
- startpos
4353 : size1
- startpos
);
4356 buf_ch
= STRING_CHAR (d
, room
);
4357 buf_ch
= TRANSLATE (buf_ch
);
4358 if (! fastmap
[CHAR_LEADING_CODE (buf_ch
)])
4363 if (! fastmap
[TRANSLATE (*d
)])
4369 /* If can't match the null string, and that's all we have left, fail. */
4370 if (range
>= 0 && startpos
== total_size
&& fastmap
4371 && !bufp
->can_be_null
)
4374 val
= re_match_2_internal (bufp
, string1
, size1
, string2
, size2
,
4375 startpos
, regs
, stop
);
4376 #ifndef REGEX_MALLOC
4393 /* Update STARTPOS to the next character boundary. */
4396 re_char
*p
= POS_ADDR_VSTRING (startpos
);
4397 re_char
*pend
= STOP_ADDR_VSTRING (startpos
);
4398 int len
= MULTIBYTE_FORM_LENGTH (p
, pend
- p
);
4416 /* Update STARTPOS to the previous character boundary. */
4419 re_char
*p
= POS_ADDR_VSTRING (startpos
) + 1;
4421 re_char
*phead
= HEAD_ADDR_VSTRING (startpos
);
4423 /* Find the head of multibyte form. */
4424 PREV_CHAR_BOUNDARY (p
, phead
);
4425 range
+= p0
- 1 - p
;
4429 startpos
-= p0
- 1 - p
;
4435 WEAK_ALIAS (__re_search_2
, re_search_2
)
4437 /* Declarations and macros for re_match_2. */
4439 static int bcmp_translate
_RE_ARGS((re_char
*s1
, re_char
*s2
,
4441 RE_TRANSLATE_TYPE translate
,
4442 const int multibyte
));
4444 /* This converts PTR, a pointer into one of the search strings `string1'
4445 and `string2' into an offset from the beginning of that string. */
4446 #define POINTER_TO_OFFSET(ptr) \
4447 (FIRST_STRING_P (ptr) \
4448 ? ((regoff_t) ((ptr) - string1)) \
4449 : ((regoff_t) ((ptr) - string2 + size1)))
4451 /* Call before fetching a character with *d. This switches over to
4452 string2 if necessary.
4453 Check re_match_2_internal for a discussion of why end_match_2 might
4454 not be within string2 (but be equal to end_match_1 instead). */
4455 #define PREFETCH() \
4458 /* End of string2 => fail. */ \
4459 if (dend == end_match_2) \
4461 /* End of string1 => advance to string2. */ \
4463 dend = end_match_2; \
4466 /* Call before fetching a char with *d if you already checked other limits.
4467 This is meant for use in lookahead operations like wordend, etc..
4468 where we might need to look at parts of the string that might be
4469 outside of the LIMITs (i.e past `stop'). */
4470 #define PREFETCH_NOLIMIT() \
4474 dend = end_match_2; \
4477 /* Test if at very beginning or at very end of the virtual concatenation
4478 of `string1' and `string2'. If only one string, it's `string2'. */
4479 #define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2)
4480 #define AT_STRINGS_END(d) ((d) == end2)
4483 /* Test if D points to a character which is word-constituent. We have
4484 two special cases to check for: if past the end of string1, look at
4485 the first character in string2; and if before the beginning of
4486 string2, look at the last character in string1. */
4487 #define WORDCHAR_P(d) \
4488 (SYNTAX ((d) == end1 ? *string2 \
4489 : (d) == string2 - 1 ? *(end1 - 1) : *(d)) \
4492 /* Disabled due to a compiler bug -- see comment at case wordbound */
4494 /* The comment at case wordbound is following one, but we don't use
4495 AT_WORD_BOUNDARY anymore to support multibyte form.
4497 The DEC Alpha C compiler 3.x generates incorrect code for the
4498 test WORDCHAR_P (d - 1) != WORDCHAR_P (d) in the expansion of
4499 AT_WORD_BOUNDARY, so this code is disabled. Expanding the
4500 macro and introducing temporary variables works around the bug. */
4503 /* Test if the character before D and the one at D differ with respect
4504 to being word-constituent. */
4505 #define AT_WORD_BOUNDARY(d) \
4506 (AT_STRINGS_BEG (d) || AT_STRINGS_END (d) \
4507 || WORDCHAR_P (d - 1) != WORDCHAR_P (d))
4510 /* Free everything we malloc. */
4511 #ifdef MATCH_MAY_ALLOCATE
4512 # define FREE_VAR(var) if (var) { REGEX_FREE (var); var = NULL; } else
4513 # define FREE_VARIABLES() \
4515 REGEX_FREE_STACK (fail_stack.stack); \
4516 FREE_VAR (regstart); \
4517 FREE_VAR (regend); \
4518 FREE_VAR (best_regstart); \
4519 FREE_VAR (best_regend); \
4522 # define FREE_VARIABLES() ((void)0) /* Do nothing! But inhibit gcc warning. */
4523 #endif /* not MATCH_MAY_ALLOCATE */
4526 /* Optimization routines. */
4528 /* If the operation is a match against one or more chars,
4529 return a pointer to the next operation, else return NULL. */
4534 switch (SWITCH_ENUM_CAST (*p
++))
4545 if (CHARSET_RANGE_TABLE_EXISTS_P (p
- 1))
4548 p
= CHARSET_RANGE_TABLE (p
- 1);
4549 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
4550 p
= CHARSET_RANGE_TABLE_END (p
, mcnt
);
4553 p
+= 1 + CHARSET_BITMAP_SIZE (p
- 1);
4560 case notcategoryspec
:
4572 /* Jump over non-matching operations. */
4574 skip_noops (p
, pend
)
4580 switch (SWITCH_ENUM_CAST ((re_opcode_t
) *p
))
4589 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
4600 /* Non-zero if "p1 matches something" implies "p2 fails". */
4602 mutually_exclusive_p (bufp
, p1
, p2
)
4603 struct re_pattern_buffer
*bufp
;
4607 const boolean multibyte
= RE_MULTIBYTE_P (bufp
);
4608 unsigned char *pend
= bufp
->buffer
+ bufp
->used
;
4610 assert (p1
>= bufp
->buffer
&& p1
< pend
4611 && p2
>= bufp
->buffer
&& p2
<= pend
);
4613 /* Skip over open/close-group commands.
4614 If what follows this loop is a ...+ construct,
4615 look at what begins its body, since we will have to
4616 match at least one of that. */
4617 p2
= skip_noops (p2
, pend
);
4618 /* The same skip can be done for p1, except that this function
4619 is only used in the case where p1 is a simple match operator. */
4620 /* p1 = skip_noops (p1, pend); */
4622 assert (p1
>= bufp
->buffer
&& p1
< pend
4623 && p2
>= bufp
->buffer
&& p2
<= pend
);
4625 op2
= p2
== pend
? succeed
: *p2
;
4627 switch (SWITCH_ENUM_CAST (op2
))
4631 /* If we're at the end of the pattern, we can change. */
4632 if (skip_one_char (p1
))
4634 DEBUG_PRINT1 (" End of pattern: fast loop.\n");
4642 register re_wchar_t c
4643 = (re_opcode_t
) *p2
== endline
? '\n'
4644 : RE_STRING_CHAR (p2
+ 2, pend
- p2
- 2);
4646 if ((re_opcode_t
) *p1
== exactn
)
4648 if (c
!= RE_STRING_CHAR (p1
+ 2, pend
- p1
- 2))
4650 DEBUG_PRINT3 (" '%c' != '%c' => fast loop.\n", c
, p1
[2]);
4655 else if ((re_opcode_t
) *p1
== charset
4656 || (re_opcode_t
) *p1
== charset_not
)
4658 int not = (re_opcode_t
) *p1
== charset_not
;
4660 /* Test if C is listed in charset (or charset_not)
4662 if (! multibyte
|| IS_REAL_ASCII (c
))
4664 if (c
< CHARSET_BITMAP_SIZE (p1
) * BYTEWIDTH
4665 && p1
[2 + c
/ BYTEWIDTH
] & (1 << (c
% BYTEWIDTH
)))
4668 else if (CHARSET_RANGE_TABLE_EXISTS_P (p1
))
4669 CHARSET_LOOKUP_RANGE_TABLE (not, c
, p1
);
4671 /* `not' is equal to 1 if c would match, which means
4672 that we can't change to pop_failure_jump. */
4675 DEBUG_PRINT1 (" No match => fast loop.\n");
4679 else if ((re_opcode_t
) *p1
== anychar
4682 DEBUG_PRINT1 (" . != \\n => fast loop.\n");
4690 if ((re_opcode_t
) *p1
== exactn
)
4691 /* Reuse the code above. */
4692 return mutually_exclusive_p (bufp
, p2
, p1
);
4694 /* It is hard to list up all the character in charset
4695 P2 if it includes multibyte character. Give up in
4697 else if (!multibyte
|| !CHARSET_RANGE_TABLE_EXISTS_P (p2
))
4699 /* Now, we are sure that P2 has no range table.
4700 So, for the size of bitmap in P2, `p2[1]' is
4701 enough. But P1 may have range table, so the
4702 size of bitmap table of P1 is extracted by
4703 using macro `CHARSET_BITMAP_SIZE'.
4705 In a multibyte case, we know that all the character
4706 listed in P2 is ASCII. In a unibyte case, P1 has only a
4707 bitmap table. So, in both cases, it is enough to test
4708 only the bitmap table of P1. */
4710 if ((re_opcode_t
) *p1
== charset
)
4713 /* We win if the charset inside the loop
4714 has no overlap with the one after the loop. */
4717 && idx
< CHARSET_BITMAP_SIZE (p1
));
4719 if ((p2
[2 + idx
] & p1
[2 + idx
]) != 0)
4723 || idx
== CHARSET_BITMAP_SIZE (p1
))
4725 DEBUG_PRINT1 (" No match => fast loop.\n");
4729 else if ((re_opcode_t
) *p1
== charset_not
)
4732 /* We win if the charset_not inside the loop lists
4733 every character listed in the charset after. */
4734 for (idx
= 0; idx
< (int) p2
[1]; idx
++)
4735 if (! (p2
[2 + idx
] == 0
4736 || (idx
< CHARSET_BITMAP_SIZE (p1
)
4737 && ((p2
[2 + idx
] & ~ p1
[2 + idx
]) == 0))))
4742 DEBUG_PRINT1 (" No match => fast loop.\n");
4751 switch (SWITCH_ENUM_CAST (*p1
))
4755 /* Reuse the code above. */
4756 return mutually_exclusive_p (bufp
, p2
, p1
);
4758 /* When we have two charset_not, it's very unlikely that
4759 they don't overlap. The union of the two sets of excluded
4760 chars should cover all possible chars, which, as a matter of
4761 fact, is virtually impossible in multibyte buffers. */
4767 return ((re_opcode_t
) *p1
== syntaxspec
&& p1
[1] == Sword
);
4769 return ((re_opcode_t
) *p1
== syntaxspec
4770 && (p1
[1] == Ssymbol
|| p1
[1] == Sword
));
4772 return ((re_opcode_t
) *p1
== syntaxspec
&& p1
[1] == p2
[1]);
4775 return ((re_opcode_t
) *p1
== notsyntaxspec
&& p1
[1] == Sword
);
4777 return ((re_opcode_t
) *p1
== notsyntaxspec
4778 && (p1
[1] == Ssymbol
|| p1
[1] == Sword
));
4780 return ((re_opcode_t
) *p1
== notsyntaxspec
&& p1
[1] == p2
[1]);
4783 return (((re_opcode_t
) *p1
== notsyntaxspec
4784 || (re_opcode_t
) *p1
== syntaxspec
)
4789 return ((re_opcode_t
) *p1
== notcategoryspec
&& p1
[1] == p2
[1]);
4790 case notcategoryspec
:
4791 return ((re_opcode_t
) *p1
== categoryspec
&& p1
[1] == p2
[1]);
4803 /* Matching routines. */
4805 #ifndef emacs /* Emacs never uses this. */
4806 /* re_match is like re_match_2 except it takes only a single string. */
4809 re_match (bufp
, string
, size
, pos
, regs
)
4810 struct re_pattern_buffer
*bufp
;
4813 struct re_registers
*regs
;
4815 int result
= re_match_2_internal (bufp
, NULL
, 0, (re_char
*) string
, size
,
4817 # if defined C_ALLOCA && !defined REGEX_MALLOC
4822 WEAK_ALIAS (__re_match
, re_match
)
4823 #endif /* not emacs */
4826 /* In Emacs, this is the string or buffer in which we
4827 are matching. It is used for looking up syntax properties. */
4828 Lisp_Object re_match_object
;
4831 /* re_match_2 matches the compiled pattern in BUFP against the
4832 the (virtual) concatenation of STRING1 and STRING2 (of length SIZE1
4833 and SIZE2, respectively). We start matching at POS, and stop
4836 If REGS is non-null and the `no_sub' field of BUFP is nonzero, we
4837 store offsets for the substring each group matched in REGS. See the
4838 documentation for exactly how many groups we fill.
4840 We return -1 if no match, -2 if an internal error (such as the
4841 failure stack overflowing). Otherwise, we return the length of the
4842 matched substring. */
4845 re_match_2 (bufp
, string1
, size1
, string2
, size2
, pos
, regs
, stop
)
4846 struct re_pattern_buffer
*bufp
;
4847 const char *string1
, *string2
;
4850 struct re_registers
*regs
;
4857 gl_state
.object
= re_match_object
;
4858 charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (POS_AS_IN_BUFFER (pos
));
4859 SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object
, charpos
, 1);
4862 result
= re_match_2_internal (bufp
, (re_char
*) string1
, size1
,
4863 (re_char
*) string2
, size2
,
4865 #if defined C_ALLOCA && !defined REGEX_MALLOC
4870 WEAK_ALIAS (__re_match_2
, re_match_2
)
4873 #define TRANSLATE_VIA_MULTIBYTE(c) \
4876 (c) = TRANSLATE (c); \
4879 MAKE_CHAR_MULTIBYTE (c); \
4880 (c) = TRANSLATE (c); \
4881 MAKE_CHAR_UNIBYTE (c); \
4886 #define TRANSLATE_VIA_MULTIBYTE(c) ((c) = TRANSLATE (c))
4890 /* This is a separate function so that we can force an alloca cleanup
4893 re_match_2_internal (bufp
, string1
, size1
, string2
, size2
, pos
, regs
, stop
)
4894 struct re_pattern_buffer
*bufp
;
4895 re_char
*string1
, *string2
;
4898 struct re_registers
*regs
;
4901 /* General temporaries. */
4906 /* Just past the end of the corresponding string. */
4907 re_char
*end1
, *end2
;
4909 /* Pointers into string1 and string2, just past the last characters in
4910 each to consider matching. */
4911 re_char
*end_match_1
, *end_match_2
;
4913 /* Where we are in the data, and the end of the current string. */
4916 /* Used sometimes to remember where we were before starting matching
4917 an operator so that we can go back in case of failure. This "atomic"
4918 behavior of matching opcodes is indispensable to the correctness
4919 of the on_failure_keep_string_jump optimization. */
4922 /* Where we are in the pattern, and the end of the pattern. */
4923 re_char
*p
= bufp
->buffer
;
4924 re_char
*pend
= p
+ bufp
->used
;
4926 /* We use this to map every character in the string. */
4927 RE_TRANSLATE_TYPE translate
= bufp
->translate
;
4929 /* Nonzero if BUFP is setup for multibyte characters. We are sure
4930 that it is the same as RE_TARGET_MULTIBYTE_P (bufp). */
4931 const boolean multibyte
= RE_MULTIBYTE_P (bufp
);
4933 /* Failure point stack. Each place that can handle a failure further
4934 down the line pushes a failure point on this stack. It consists of
4935 regstart, and regend for all registers corresponding to
4936 the subexpressions we're currently inside, plus the number of such
4937 registers, and, finally, two char *'s. The first char * is where
4938 to resume scanning the pattern; the second one is where to resume
4939 scanning the strings. */
4940 #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
4941 fail_stack_type fail_stack
;
4944 unsigned nfailure_points_pushed
= 0, nfailure_points_popped
= 0;
4947 #if defined REL_ALLOC && defined REGEX_MALLOC
4948 /* This holds the pointer to the failure stack, when
4949 it is allocated relocatably. */
4950 fail_stack_elt_t
*failure_stack_ptr
;
4953 /* We fill all the registers internally, independent of what we
4954 return, for use in backreferences. The number here includes
4955 an element for register zero. */
4956 size_t num_regs
= bufp
->re_nsub
+ 1;
4958 /* Information on the contents of registers. These are pointers into
4959 the input strings; they record just what was matched (on this
4960 attempt) by a subexpression part of the pattern, that is, the
4961 regnum-th regstart pointer points to where in the pattern we began
4962 matching and the regnum-th regend points to right after where we
4963 stopped matching the regnum-th subexpression. (The zeroth register
4964 keeps track of what the whole pattern matches.) */
4965 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
4966 re_char
**regstart
, **regend
;
4969 /* The following record the register info as found in the above
4970 variables when we find a match better than any we've seen before.
4971 This happens as we backtrack through the failure points, which in
4972 turn happens only if we have not yet matched the entire string. */
4973 unsigned best_regs_set
= false;
4974 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
4975 re_char
**best_regstart
, **best_regend
;
4978 /* Logically, this is `best_regend[0]'. But we don't want to have to
4979 allocate space for that if we're not allocating space for anything
4980 else (see below). Also, we never need info about register 0 for
4981 any of the other register vectors, and it seems rather a kludge to
4982 treat `best_regend' differently than the rest. So we keep track of
4983 the end of the best match so far in a separate variable. We
4984 initialize this to NULL so that when we backtrack the first time
4985 and need to test it, it's not garbage. */
4986 re_char
*match_end
= NULL
;
4989 /* Counts the total number of registers pushed. */
4990 unsigned num_regs_pushed
= 0;
4993 DEBUG_PRINT1 ("\n\nEntering re_match_2.\n");
4997 #ifdef MATCH_MAY_ALLOCATE
4998 /* Do not bother to initialize all the register variables if there are
4999 no groups in the pattern, as it takes a fair amount of time. If
5000 there are groups, we include space for register 0 (the whole
5001 pattern), even though we never use it, since it simplifies the
5002 array indexing. We should fix this. */
5005 regstart
= REGEX_TALLOC (num_regs
, re_char
*);
5006 regend
= REGEX_TALLOC (num_regs
, re_char
*);
5007 best_regstart
= REGEX_TALLOC (num_regs
, re_char
*);
5008 best_regend
= REGEX_TALLOC (num_regs
, re_char
*);
5010 if (!(regstart
&& regend
&& best_regstart
&& best_regend
))
5018 /* We must initialize all our variables to NULL, so that
5019 `FREE_VARIABLES' doesn't try to free them. */
5020 regstart
= regend
= best_regstart
= best_regend
= NULL
;
5022 #endif /* MATCH_MAY_ALLOCATE */
5024 /* The starting position is bogus. */
5025 if (pos
< 0 || pos
> size1
+ size2
)
5031 /* Initialize subexpression text positions to -1 to mark ones that no
5032 start_memory/stop_memory has been seen for. Also initialize the
5033 register information struct. */
5034 for (reg
= 1; reg
< num_regs
; reg
++)
5035 regstart
[reg
] = regend
[reg
] = NULL
;
5037 /* We move `string1' into `string2' if the latter's empty -- but not if
5038 `string1' is null. */
5039 if (size2
== 0 && string1
!= NULL
)
5046 end1
= string1
+ size1
;
5047 end2
= string2
+ size2
;
5049 /* `p' scans through the pattern as `d' scans through the data.
5050 `dend' is the end of the input string that `d' points within. `d'
5051 is advanced into the following input string whenever necessary, but
5052 this happens before fetching; therefore, at the beginning of the
5053 loop, `d' can be pointing at the end of a string, but it cannot
5057 /* Only match within string2. */
5058 d
= string2
+ pos
- size1
;
5059 dend
= end_match_2
= string2
+ stop
- size1
;
5060 end_match_1
= end1
; /* Just to give it a value. */
5066 /* Only match within string1. */
5067 end_match_1
= string1
+ stop
;
5069 When we reach end_match_1, PREFETCH normally switches to string2.
5070 But in the present case, this means that just doing a PREFETCH
5071 makes us jump from `stop' to `gap' within the string.
5072 What we really want here is for the search to stop as
5073 soon as we hit end_match_1. That's why we set end_match_2
5074 to end_match_1 (since PREFETCH fails as soon as we hit
5076 end_match_2
= end_match_1
;
5079 { /* It's important to use this code when stop == size so that
5080 moving `d' from end1 to string2 will not prevent the d == dend
5081 check from catching the end of string. */
5083 end_match_2
= string2
+ stop
- size1
;
5089 DEBUG_PRINT1 ("The compiled pattern is: ");
5090 DEBUG_PRINT_COMPILED_PATTERN (bufp
, p
, pend
);
5091 DEBUG_PRINT1 ("The string to match is: `");
5092 DEBUG_PRINT_DOUBLE_STRING (d
, string1
, size1
, string2
, size2
);
5093 DEBUG_PRINT1 ("'\n");
5095 /* This loops over pattern commands. It exits by returning from the
5096 function if the match is complete, or it drops through if the match
5097 fails at this starting point in the input data. */
5100 DEBUG_PRINT2 ("\n%p: ", p
);
5103 { /* End of pattern means we might have succeeded. */
5104 DEBUG_PRINT1 ("end of pattern ... ");
5106 /* If we haven't matched the entire string, and we want the
5107 longest match, try backtracking. */
5108 if (d
!= end_match_2
)
5110 /* 1 if this match ends in the same string (string1 or string2)
5111 as the best previous match. */
5112 boolean same_str_p
= (FIRST_STRING_P (match_end
)
5113 == FIRST_STRING_P (d
));
5114 /* 1 if this match is the best seen so far. */
5115 boolean best_match_p
;
5117 /* AIX compiler got confused when this was combined
5118 with the previous declaration. */
5120 best_match_p
= d
> match_end
;
5122 best_match_p
= !FIRST_STRING_P (d
);
5124 DEBUG_PRINT1 ("backtracking.\n");
5126 if (!FAIL_STACK_EMPTY ())
5127 { /* More failure points to try. */
5129 /* If exceeds best match so far, save it. */
5130 if (!best_regs_set
|| best_match_p
)
5132 best_regs_set
= true;
5135 DEBUG_PRINT1 ("\nSAVING match as best so far.\n");
5137 for (reg
= 1; reg
< num_regs
; reg
++)
5139 best_regstart
[reg
] = regstart
[reg
];
5140 best_regend
[reg
] = regend
[reg
];
5146 /* If no failure points, don't restore garbage. And if
5147 last match is real best match, don't restore second
5149 else if (best_regs_set
&& !best_match_p
)
5152 /* Restore best match. It may happen that `dend ==
5153 end_match_1' while the restored d is in string2.
5154 For example, the pattern `x.*y.*z' against the
5155 strings `x-' and `y-z-', if the two strings are
5156 not consecutive in memory. */
5157 DEBUG_PRINT1 ("Restoring best registers.\n");
5160 dend
= ((d
>= string1
&& d
<= end1
)
5161 ? end_match_1
: end_match_2
);
5163 for (reg
= 1; reg
< num_regs
; reg
++)
5165 regstart
[reg
] = best_regstart
[reg
];
5166 regend
[reg
] = best_regend
[reg
];
5169 } /* d != end_match_2 */
5172 DEBUG_PRINT1 ("Accepting match.\n");
5174 /* If caller wants register contents data back, do it. */
5175 if (regs
&& !bufp
->no_sub
)
5177 /* Have the register data arrays been allocated? */
5178 if (bufp
->regs_allocated
== REGS_UNALLOCATED
)
5179 { /* No. So allocate them with malloc. We need one
5180 extra element beyond `num_regs' for the `-1' marker
5182 regs
->num_regs
= MAX (RE_NREGS
, num_regs
+ 1);
5183 regs
->start
= TALLOC (regs
->num_regs
, regoff_t
);
5184 regs
->end
= TALLOC (regs
->num_regs
, regoff_t
);
5185 if (regs
->start
== NULL
|| regs
->end
== NULL
)
5190 bufp
->regs_allocated
= REGS_REALLOCATE
;
5192 else if (bufp
->regs_allocated
== REGS_REALLOCATE
)
5193 { /* Yes. If we need more elements than were already
5194 allocated, reallocate them. If we need fewer, just
5196 if (regs
->num_regs
< num_regs
+ 1)
5198 regs
->num_regs
= num_regs
+ 1;
5199 RETALLOC (regs
->start
, regs
->num_regs
, regoff_t
);
5200 RETALLOC (regs
->end
, regs
->num_regs
, regoff_t
);
5201 if (regs
->start
== NULL
|| regs
->end
== NULL
)
5210 /* These braces fend off a "empty body in an else-statement"
5211 warning under GCC when assert expands to nothing. */
5212 assert (bufp
->regs_allocated
== REGS_FIXED
);
5215 /* Convert the pointer data in `regstart' and `regend' to
5216 indices. Register zero has to be set differently,
5217 since we haven't kept track of any info for it. */
5218 if (regs
->num_regs
> 0)
5220 regs
->start
[0] = pos
;
5221 regs
->end
[0] = POINTER_TO_OFFSET (d
);
5224 /* Go through the first `min (num_regs, regs->num_regs)'
5225 registers, since that is all we initialized. */
5226 for (reg
= 1; reg
< MIN (num_regs
, regs
->num_regs
); reg
++)
5228 if (REG_UNSET (regstart
[reg
]) || REG_UNSET (regend
[reg
]))
5229 regs
->start
[reg
] = regs
->end
[reg
] = -1;
5233 = (regoff_t
) POINTER_TO_OFFSET (regstart
[reg
]);
5235 = (regoff_t
) POINTER_TO_OFFSET (regend
[reg
]);
5239 /* If the regs structure we return has more elements than
5240 were in the pattern, set the extra elements to -1. If
5241 we (re)allocated the registers, this is the case,
5242 because we always allocate enough to have at least one
5244 for (reg
= num_regs
; reg
< regs
->num_regs
; reg
++)
5245 regs
->start
[reg
] = regs
->end
[reg
] = -1;
5246 } /* regs && !bufp->no_sub */
5248 DEBUG_PRINT4 ("%u failure points pushed, %u popped (%u remain).\n",
5249 nfailure_points_pushed
, nfailure_points_popped
,
5250 nfailure_points_pushed
- nfailure_points_popped
);
5251 DEBUG_PRINT2 ("%u registers pushed.\n", num_regs_pushed
);
5253 mcnt
= POINTER_TO_OFFSET (d
) - pos
;
5255 DEBUG_PRINT2 ("Returning %d from re_match_2.\n", mcnt
);
5261 /* Otherwise match next pattern command. */
5262 switch (SWITCH_ENUM_CAST ((re_opcode_t
) *p
++))
5264 /* Ignore these. Used to ignore the n of succeed_n's which
5265 currently have n == 0. */
5267 DEBUG_PRINT1 ("EXECUTING no_op.\n");
5271 DEBUG_PRINT1 ("EXECUTING succeed.\n");
5274 /* Match the next n pattern characters exactly. The following
5275 byte in the pattern defines n, and the n bytes after that
5276 are the characters to match. */
5279 DEBUG_PRINT2 ("EXECUTING exactn %d.\n", mcnt
);
5281 /* Remember the start point to rollback upon failure. */
5285 /* This is written out as an if-else so we don't waste time
5286 testing `translate' inside the loop. */
5287 if (RE_TRANSLATE_P (translate
))
5291 if (RE_TRANSLATE (translate
, *d
) != *p
++)
5311 /* The cost of testing `translate' is comparatively small. */
5315 int pat_charlen
, buf_charlen
;
5316 unsigned int pat_ch
, buf_ch
;
5319 pat_ch
= STRING_CHAR_AND_LENGTH (p
, pend
- p
, pat_charlen
);
5320 buf_ch
= STRING_CHAR_AND_LENGTH (d
, dend
- d
, buf_charlen
);
5322 if (TRANSLATE (buf_ch
) != pat_ch
)
5330 mcnt
-= pat_charlen
;
5336 unsigned int buf_ch
;
5340 TRANSLATE_VIA_MULTIBYTE (buf_ch
);
5352 /* Match any character except possibly a newline or a null. */
5358 DEBUG_PRINT1 ("EXECUTING anychar.\n");
5361 buf_ch
= RE_STRING_CHAR_AND_LENGTH (d
, dend
- d
, buf_charlen
);
5362 buf_ch
= TRANSLATE (buf_ch
);
5364 if ((!(bufp
->syntax
& RE_DOT_NEWLINE
)
5366 || ((bufp
->syntax
& RE_DOT_NOT_NULL
)
5367 && buf_ch
== '\000'))
5370 DEBUG_PRINT2 (" Matched `%d'.\n", *d
);
5379 register unsigned int c
;
5380 boolean
not = (re_opcode_t
) *(p
- 1) == charset_not
;
5383 /* Start of actual range_table, or end of bitmap if there is no
5385 re_char
*range_table
;
5387 /* Nonzero if there is a range table. */
5388 int range_table_exists
;
5390 /* Number of ranges of range table. This is not included
5391 in the initial byte-length of the command. */
5394 DEBUG_PRINT2 ("EXECUTING charset%s.\n", not ? "_not" : "");
5396 range_table_exists
= CHARSET_RANGE_TABLE_EXISTS_P (&p
[-1]);
5398 if (range_table_exists
)
5400 range_table
= CHARSET_RANGE_TABLE (&p
[-1]); /* Past the bitmap. */
5401 EXTRACT_NUMBER_AND_INCR (count
, range_table
);
5405 c
= RE_STRING_CHAR_AND_LENGTH (d
, dend
- d
, len
);
5406 TRANSLATE_VIA_MULTIBYTE (c
); /* The character to match. */
5408 if (! multibyte
|| IS_REAL_ASCII (c
))
5409 { /* Lookup bitmap. */
5410 /* Cast to `unsigned' instead of `unsigned char' in
5411 case the bit list is a full 32 bytes long. */
5412 if (c
< (unsigned) (CHARSET_BITMAP_SIZE (&p
[-1]) * BYTEWIDTH
)
5413 && p
[1 + c
/ BYTEWIDTH
] & (1 << (c
% BYTEWIDTH
)))
5417 else if (range_table_exists
)
5419 int class_bits
= CHARSET_RANGE_TABLE_BITS (&p
[-1]);
5421 if ( (class_bits
& BIT_LOWER
&& ISLOWER (c
))
5422 | (class_bits
& BIT_MULTIBYTE
)
5423 | (class_bits
& BIT_PUNCT
&& ISPUNCT (c
))
5424 | (class_bits
& BIT_SPACE
&& ISSPACE (c
))
5425 | (class_bits
& BIT_UPPER
&& ISUPPER (c
))
5426 | (class_bits
& BIT_WORD
&& ISWORD (c
)))
5429 CHARSET_LOOKUP_RANGE_TABLE_RAW (not, c
, range_table
, count
);
5433 if (range_table_exists
)
5434 p
= CHARSET_RANGE_TABLE_END (range_table
, count
);
5436 p
+= CHARSET_BITMAP_SIZE (&p
[-1]) + 1;
5438 if (!not) goto fail
;
5445 /* The beginning of a group is represented by start_memory.
5446 The argument is the register number. The text
5447 matched within the group is recorded (in the internal
5448 registers data structure) under the register number. */
5450 DEBUG_PRINT2 ("EXECUTING start_memory %d:\n", *p
);
5452 /* In case we need to undo this operation (via backtracking). */
5453 PUSH_FAILURE_REG ((unsigned int)*p
);
5456 regend
[*p
] = NULL
; /* probably unnecessary. -sm */
5457 DEBUG_PRINT2 (" regstart: %d\n", POINTER_TO_OFFSET (regstart
[*p
]));
5459 /* Move past the register number and inner group count. */
5464 /* The stop_memory opcode represents the end of a group. Its
5465 argument is the same as start_memory's: the register number. */
5467 DEBUG_PRINT2 ("EXECUTING stop_memory %d:\n", *p
);
5469 assert (!REG_UNSET (regstart
[*p
]));
5470 /* Strictly speaking, there should be code such as:
5472 assert (REG_UNSET (regend[*p]));
5473 PUSH_FAILURE_REGSTOP ((unsigned int)*p);
5475 But the only info to be pushed is regend[*p] and it is known to
5476 be UNSET, so there really isn't anything to push.
5477 Not pushing anything, on the other hand deprives us from the
5478 guarantee that regend[*p] is UNSET since undoing this operation
5479 will not reset its value properly. This is not important since
5480 the value will only be read on the next start_memory or at
5481 the very end and both events can only happen if this stop_memory
5485 DEBUG_PRINT2 (" regend: %d\n", POINTER_TO_OFFSET (regend
[*p
]));
5487 /* Move past the register number and the inner group count. */
5492 /* \<digit> has been turned into a `duplicate' command which is
5493 followed by the numeric value of <digit> as the register number. */
5496 register re_char
*d2
, *dend2
;
5497 int regno
= *p
++; /* Get which register to match against. */
5498 DEBUG_PRINT2 ("EXECUTING duplicate %d.\n", regno
);
5500 /* Can't back reference a group which we've never matched. */
5501 if (REG_UNSET (regstart
[regno
]) || REG_UNSET (regend
[regno
]))
5504 /* Where in input to try to start matching. */
5505 d2
= regstart
[regno
];
5507 /* Remember the start point to rollback upon failure. */
5510 /* Where to stop matching; if both the place to start and
5511 the place to stop matching are in the same string, then
5512 set to the place to stop, otherwise, for now have to use
5513 the end of the first string. */
5515 dend2
= ((FIRST_STRING_P (regstart
[regno
])
5516 == FIRST_STRING_P (regend
[regno
]))
5517 ? regend
[regno
] : end_match_1
);
5520 /* If necessary, advance to next segment in register
5524 if (dend2
== end_match_2
) break;
5525 if (dend2
== regend
[regno
]) break;
5527 /* End of string1 => advance to string2. */
5529 dend2
= regend
[regno
];
5531 /* At end of register contents => success */
5532 if (d2
== dend2
) break;
5534 /* If necessary, advance to next segment in data. */
5537 /* How many characters left in this segment to match. */
5540 /* Want how many consecutive characters we can match in
5541 one shot, so, if necessary, adjust the count. */
5542 if (mcnt
> dend2
- d2
)
5545 /* Compare that many; failure if mismatch, else move
5547 if (RE_TRANSLATE_P (translate
)
5548 ? bcmp_translate (d
, d2
, mcnt
, translate
, multibyte
)
5549 : memcmp (d
, d2
, mcnt
))
5554 d
+= mcnt
, d2
+= mcnt
;
5560 /* begline matches the empty string at the beginning of the string
5561 (unless `not_bol' is set in `bufp'), and after newlines. */
5563 DEBUG_PRINT1 ("EXECUTING begline.\n");
5565 if (AT_STRINGS_BEG (d
))
5567 if (!bufp
->not_bol
) break;
5572 GET_CHAR_BEFORE_2 (c
, d
, string1
, end1
, string2
, end2
);
5576 /* In all other cases, we fail. */
5580 /* endline is the dual of begline. */
5582 DEBUG_PRINT1 ("EXECUTING endline.\n");
5584 if (AT_STRINGS_END (d
))
5586 if (!bufp
->not_eol
) break;
5590 PREFETCH_NOLIMIT ();
5597 /* Match at the very beginning of the data. */
5599 DEBUG_PRINT1 ("EXECUTING begbuf.\n");
5600 if (AT_STRINGS_BEG (d
))
5605 /* Match at the very end of the data. */
5607 DEBUG_PRINT1 ("EXECUTING endbuf.\n");
5608 if (AT_STRINGS_END (d
))
5613 /* on_failure_keep_string_jump is used to optimize `.*\n'. It
5614 pushes NULL as the value for the string on the stack. Then
5615 `POP_FAILURE_POINT' will keep the current value for the
5616 string, instead of restoring it. To see why, consider
5617 matching `foo\nbar' against `.*\n'. The .* matches the foo;
5618 then the . fails against the \n. But the next thing we want
5619 to do is match the \n against the \n; if we restored the
5620 string value, we would be back at the foo.
5622 Because this is used only in specific cases, we don't need to
5623 check all the things that `on_failure_jump' does, to make
5624 sure the right things get saved on the stack. Hence we don't
5625 share its code. The only reason to push anything on the
5626 stack at all is that otherwise we would have to change
5627 `anychar's code to do something besides goto fail in this
5628 case; that seems worse than this. */
5629 case on_failure_keep_string_jump
:
5630 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5631 DEBUG_PRINT3 ("EXECUTING on_failure_keep_string_jump %d (to %p):\n",
5634 PUSH_FAILURE_POINT (p
- 3, NULL
);
5637 /* A nasty loop is introduced by the non-greedy *? and +?.
5638 With such loops, the stack only ever contains one failure point
5639 at a time, so that a plain on_failure_jump_loop kind of
5640 cycle detection cannot work. Worse yet, such a detection
5641 can not only fail to detect a cycle, but it can also wrongly
5642 detect a cycle (between different instantiations of the same
5644 So the method used for those nasty loops is a little different:
5645 We use a special cycle-detection-stack-frame which is pushed
5646 when the on_failure_jump_nastyloop failure-point is *popped*.
5647 This special frame thus marks the beginning of one iteration
5648 through the loop and we can hence easily check right here
5649 whether something matched between the beginning and the end of
5651 case on_failure_jump_nastyloop
:
5652 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5653 DEBUG_PRINT3 ("EXECUTING on_failure_jump_nastyloop %d (to %p):\n",
5656 assert ((re_opcode_t
)p
[-4] == no_op
);
5659 CHECK_INFINITE_LOOP (p
- 4, d
);
5661 /* If there's a cycle, just continue without pushing
5662 this failure point. The failure point is the "try again"
5663 option, which shouldn't be tried.
5664 We want (x?)*?y\1z to match both xxyz and xxyxz. */
5665 PUSH_FAILURE_POINT (p
- 3, d
);
5669 /* Simple loop detecting on_failure_jump: just check on the
5670 failure stack if the same spot was already hit earlier. */
5671 case on_failure_jump_loop
:
5673 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5674 DEBUG_PRINT3 ("EXECUTING on_failure_jump_loop %d (to %p):\n",
5678 CHECK_INFINITE_LOOP (p
- 3, d
);
5680 /* If there's a cycle, get out of the loop, as if the matching
5681 had failed. We used to just `goto fail' here, but that was
5682 aborting the search a bit too early: we want to keep the
5683 empty-loop-match and keep matching after the loop.
5684 We want (x?)*y\1z to match both xxyz and xxyxz. */
5687 PUSH_FAILURE_POINT (p
- 3, d
);
5692 /* Uses of on_failure_jump:
5694 Each alternative starts with an on_failure_jump that points
5695 to the beginning of the next alternative. Each alternative
5696 except the last ends with a jump that in effect jumps past
5697 the rest of the alternatives. (They really jump to the
5698 ending jump of the following alternative, because tensioning
5699 these jumps is a hassle.)
5701 Repeats start with an on_failure_jump that points past both
5702 the repetition text and either the following jump or
5703 pop_failure_jump back to this on_failure_jump. */
5704 case on_failure_jump
:
5705 IMMEDIATE_QUIT_CHECK
;
5706 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5707 DEBUG_PRINT3 ("EXECUTING on_failure_jump %d (to %p):\n",
5710 PUSH_FAILURE_POINT (p
-3, d
);
5713 /* This operation is used for greedy *.
5714 Compare the beginning of the repeat with what in the
5715 pattern follows its end. If we can establish that there
5716 is nothing that they would both match, i.e., that we
5717 would have to backtrack because of (as in, e.g., `a*a')
5718 then we can use a non-backtracking loop based on
5719 on_failure_keep_string_jump instead of on_failure_jump. */
5720 case on_failure_jump_smart
:
5721 IMMEDIATE_QUIT_CHECK
;
5722 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5723 DEBUG_PRINT3 ("EXECUTING on_failure_jump_smart %d (to %p).\n",
5726 re_char
*p1
= p
; /* Next operation. */
5727 /* Here, we discard `const', making re_match non-reentrant. */
5728 unsigned char *p2
= (unsigned char*) p
+ mcnt
; /* Jump dest. */
5729 unsigned char *p3
= (unsigned char*) p
- 3; /* opcode location. */
5731 p
-= 3; /* Reset so that we will re-execute the
5732 instruction once it's been changed. */
5734 EXTRACT_NUMBER (mcnt
, p2
- 2);
5736 /* Ensure this is a indeed the trivial kind of loop
5737 we are expecting. */
5738 assert (skip_one_char (p1
) == p2
- 3);
5739 assert ((re_opcode_t
) p2
[-3] == jump
&& p2
+ mcnt
== p
);
5740 DEBUG_STATEMENT (debug
+= 2);
5741 if (mutually_exclusive_p (bufp
, p1
, p2
))
5743 /* Use a fast `on_failure_keep_string_jump' loop. */
5744 DEBUG_PRINT1 (" smart exclusive => fast loop.\n");
5745 *p3
= (unsigned char) on_failure_keep_string_jump
;
5746 STORE_NUMBER (p2
- 2, mcnt
+ 3);
5750 /* Default to a safe `on_failure_jump' loop. */
5751 DEBUG_PRINT1 (" smart default => slow loop.\n");
5752 *p3
= (unsigned char) on_failure_jump
;
5754 DEBUG_STATEMENT (debug
-= 2);
5758 /* Unconditionally jump (without popping any failure points). */
5761 IMMEDIATE_QUIT_CHECK
;
5762 EXTRACT_NUMBER_AND_INCR (mcnt
, p
); /* Get the amount to jump. */
5763 DEBUG_PRINT2 ("EXECUTING jump %d ", mcnt
);
5764 p
+= mcnt
; /* Do the jump. */
5765 DEBUG_PRINT2 ("(to %p).\n", p
);
5769 /* Have to succeed matching what follows at least n times.
5770 After that, handle like `on_failure_jump'. */
5772 /* Signedness doesn't matter since we only compare MCNT to 0. */
5773 EXTRACT_NUMBER (mcnt
, p
+ 2);
5774 DEBUG_PRINT2 ("EXECUTING succeed_n %d.\n", mcnt
);
5776 /* Originally, mcnt is how many times we HAVE to succeed. */
5779 /* Here, we discard `const', making re_match non-reentrant. */
5780 unsigned char *p2
= (unsigned char*) p
+ 2; /* counter loc. */
5783 PUSH_NUMBER (p2
, mcnt
);
5786 /* The two bytes encoding mcnt == 0 are two no_op opcodes. */
5791 /* Signedness doesn't matter since we only compare MCNT to 0. */
5792 EXTRACT_NUMBER (mcnt
, p
+ 2);
5793 DEBUG_PRINT2 ("EXECUTING jump_n %d.\n", mcnt
);
5795 /* Originally, this is how many times we CAN jump. */
5798 /* Here, we discard `const', making re_match non-reentrant. */
5799 unsigned char *p2
= (unsigned char*) p
+ 2; /* counter loc. */
5801 PUSH_NUMBER (p2
, mcnt
);
5802 goto unconditional_jump
;
5804 /* If don't have to jump any more, skip over the rest of command. */
5811 unsigned char *p2
; /* Location of the counter. */
5812 DEBUG_PRINT1 ("EXECUTING set_number_at.\n");
5814 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5815 /* Here, we discard `const', making re_match non-reentrant. */
5816 p2
= (unsigned char*) p
+ mcnt
;
5817 /* Signedness doesn't matter since we only copy MCNT's bits . */
5818 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5819 DEBUG_PRINT3 (" Setting %p to %d.\n", p2
, mcnt
);
5820 PUSH_NUMBER (p2
, mcnt
);
5826 not = (re_opcode_t
) *(p
- 1) == notwordbound
;
5827 DEBUG_PRINT2 ("EXECUTING %swordbound.\n", not?"not":"");
5829 /* We SUCCEED (or FAIL) in one of the following cases: */
5831 /* Case 1: D is at the beginning or the end of string. */
5832 if (AT_STRINGS_BEG (d
) || AT_STRINGS_END (d
))
5836 /* C1 is the character before D, S1 is the syntax of C1, C2
5837 is the character at D, and S2 is the syntax of C2. */
5842 int offset
= PTR_TO_OFFSET (d
- 1);
5843 int charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
5844 UPDATE_SYNTAX_TABLE (charpos
);
5846 GET_CHAR_BEFORE_2 (c1
, d
, string1
, end1
, string2
, end2
);
5849 UPDATE_SYNTAX_TABLE_FORWARD (charpos
+ 1);
5851 PREFETCH_NOLIMIT ();
5852 GET_CHAR_AFTER (c2
, d
, dummy
);
5855 if (/* Case 2: Only one of S1 and S2 is Sword. */
5856 ((s1
== Sword
) != (s2
== Sword
))
5857 /* Case 3: Both of S1 and S2 are Sword, and macro
5858 WORD_BOUNDARY_P (C1, C2) returns nonzero. */
5859 || ((s1
== Sword
) && WORD_BOUNDARY_P (c1
, c2
)))
5868 DEBUG_PRINT1 ("EXECUTING wordbeg.\n");
5870 /* We FAIL in one of the following cases: */
5872 /* Case 1: D is at the end of string. */
5873 if (AT_STRINGS_END (d
))
5877 /* C1 is the character before D, S1 is the syntax of C1, C2
5878 is the character at D, and S2 is the syntax of C2. */
5883 int offset
= PTR_TO_OFFSET (d
);
5884 int charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
5885 UPDATE_SYNTAX_TABLE (charpos
);
5888 GET_CHAR_AFTER (c2
, d
, dummy
);
5891 /* Case 2: S2 is not Sword. */
5895 /* Case 3: D is not at the beginning of string ... */
5896 if (!AT_STRINGS_BEG (d
))
5898 GET_CHAR_BEFORE_2 (c1
, d
, string1
, end1
, string2
, end2
);
5900 UPDATE_SYNTAX_TABLE_BACKWARD (charpos
- 1);
5904 /* ... and S1 is Sword, and WORD_BOUNDARY_P (C1, C2)
5906 if ((s1
== Sword
) && !WORD_BOUNDARY_P (c1
, c2
))
5913 DEBUG_PRINT1 ("EXECUTING wordend.\n");
5915 /* We FAIL in one of the following cases: */
5917 /* Case 1: D is at the beginning of string. */
5918 if (AT_STRINGS_BEG (d
))
5922 /* C1 is the character before D, S1 is the syntax of C1, C2
5923 is the character at D, and S2 is the syntax of C2. */
5928 int offset
= PTR_TO_OFFSET (d
) - 1;
5929 int charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
5930 UPDATE_SYNTAX_TABLE (charpos
);
5932 GET_CHAR_BEFORE_2 (c1
, d
, string1
, end1
, string2
, end2
);
5935 /* Case 2: S1 is not Sword. */
5939 /* Case 3: D is not at the end of string ... */
5940 if (!AT_STRINGS_END (d
))
5942 PREFETCH_NOLIMIT ();
5943 GET_CHAR_AFTER (c2
, d
, dummy
);
5945 UPDATE_SYNTAX_TABLE_FORWARD (charpos
);
5949 /* ... and S2 is Sword, and WORD_BOUNDARY_P (C1, C2)
5951 if ((s2
== Sword
) && !WORD_BOUNDARY_P (c1
, c2
))
5958 DEBUG_PRINT1 ("EXECUTING symbeg.\n");
5960 /* We FAIL in one of the following cases: */
5962 /* Case 1: D is at the end of string. */
5963 if (AT_STRINGS_END (d
))
5967 /* C1 is the character before D, S1 is the syntax of C1, C2
5968 is the character at D, and S2 is the syntax of C2. */
5972 int offset
= PTR_TO_OFFSET (d
);
5973 int charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
5974 UPDATE_SYNTAX_TABLE (charpos
);
5977 c2
= RE_STRING_CHAR (d
, dend
- d
);
5980 /* Case 2: S2 is neither Sword nor Ssymbol. */
5981 if (s2
!= Sword
&& s2
!= Ssymbol
)
5984 /* Case 3: D is not at the beginning of string ... */
5985 if (!AT_STRINGS_BEG (d
))
5987 GET_CHAR_BEFORE_2 (c1
, d
, string1
, end1
, string2
, end2
);
5989 UPDATE_SYNTAX_TABLE_BACKWARD (charpos
- 1);
5993 /* ... and S1 is Sword or Ssymbol. */
5994 if (s1
== Sword
|| s1
== Ssymbol
)
6001 DEBUG_PRINT1 ("EXECUTING symend.\n");
6003 /* We FAIL in one of the following cases: */
6005 /* Case 1: D is at the beginning of string. */
6006 if (AT_STRINGS_BEG (d
))
6010 /* C1 is the character before D, S1 is the syntax of C1, C2
6011 is the character at D, and S2 is the syntax of C2. */
6015 int offset
= PTR_TO_OFFSET (d
) - 1;
6016 int charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
6017 UPDATE_SYNTAX_TABLE (charpos
);
6019 GET_CHAR_BEFORE_2 (c1
, d
, string1
, end1
, string2
, end2
);
6022 /* Case 2: S1 is neither Ssymbol nor Sword. */
6023 if (s1
!= Sword
&& s1
!= Ssymbol
)
6026 /* Case 3: D is not at the end of string ... */
6027 if (!AT_STRINGS_END (d
))
6029 PREFETCH_NOLIMIT ();
6030 c2
= RE_STRING_CHAR (d
, dend
- d
);
6032 UPDATE_SYNTAX_TABLE_FORWARD (charpos
);
6036 /* ... and S2 is Sword or Ssymbol. */
6037 if (s2
== Sword
|| s2
== Ssymbol
)
6045 not = (re_opcode_t
) *(p
- 1) == notsyntaxspec
;
6047 DEBUG_PRINT3 ("EXECUTING %ssyntaxspec %d.\n", not?"not":"", mcnt
);
6051 int offset
= PTR_TO_OFFSET (d
);
6052 int pos1
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
6053 UPDATE_SYNTAX_TABLE (pos1
);
6060 GET_CHAR_AFTER (c
, d
, len
);
6061 if ((SYNTAX (c
) != (enum syntaxcode
) mcnt
) ^ not)
6069 DEBUG_PRINT1 ("EXECUTING before_dot.\n");
6070 if (PTR_BYTE_POS (d
) >= PT_BYTE
)
6075 DEBUG_PRINT1 ("EXECUTING at_dot.\n");
6076 if (PTR_BYTE_POS (d
) != PT_BYTE
)
6081 DEBUG_PRINT1 ("EXECUTING after_dot.\n");
6082 if (PTR_BYTE_POS (d
) <= PT_BYTE
)
6087 case notcategoryspec
:
6088 not = (re_opcode_t
) *(p
- 1) == notcategoryspec
;
6090 DEBUG_PRINT3 ("EXECUTING %scategoryspec %d.\n", not?"not":"", mcnt
);
6096 GET_CHAR_AFTER (c
, d
, len
);
6097 if ((!CHAR_HAS_CATEGORY (c
, mcnt
)) ^ not)
6108 continue; /* Successfully executed one pattern command; keep going. */
6111 /* We goto here if a matching operation fails. */
6113 IMMEDIATE_QUIT_CHECK
;
6114 if (!FAIL_STACK_EMPTY ())
6117 /* A restart point is known. Restore to that state. */
6118 DEBUG_PRINT1 ("\nFAIL:\n");
6119 POP_FAILURE_POINT (str
, pat
);
6120 switch (SWITCH_ENUM_CAST ((re_opcode_t
) *pat
++))
6122 case on_failure_keep_string_jump
:
6123 assert (str
== NULL
);
6124 goto continue_failure_jump
;
6126 case on_failure_jump_nastyloop
:
6127 assert ((re_opcode_t
)pat
[-2] == no_op
);
6128 PUSH_FAILURE_POINT (pat
- 2, str
);
6131 case on_failure_jump_loop
:
6132 case on_failure_jump
:
6135 continue_failure_jump
:
6136 EXTRACT_NUMBER_AND_INCR (mcnt
, pat
);
6141 /* A special frame used for nastyloops. */
6148 assert (p
>= bufp
->buffer
&& p
<= pend
);
6150 if (d
>= string1
&& d
<= end1
)
6154 break; /* Matching at this starting point really fails. */
6158 goto restore_best_regs
;
6162 return -1; /* Failure to match. */
6165 /* Subroutine definitions for re_match_2. */
6167 /* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
6168 bytes; nonzero otherwise. */
6171 bcmp_translate (s1
, s2
, len
, translate
, multibyte
)
6174 RE_TRANSLATE_TYPE translate
;
6175 const int multibyte
;
6177 register re_char
*p1
= s1
, *p2
= s2
;
6178 re_char
*p1_end
= s1
+ len
;
6179 re_char
*p2_end
= s2
+ len
;
6181 /* FIXME: Checking both p1 and p2 presumes that the two strings might have
6182 different lengths, but relying on a single `len' would break this. -sm */
6183 while (p1
< p1_end
&& p2
< p2_end
)
6185 int p1_charlen
, p2_charlen
;
6186 re_wchar_t p1_ch
, p2_ch
;
6188 GET_CHAR_AFTER (p1_ch
, p1
, p1_charlen
);
6189 GET_CHAR_AFTER (p2_ch
, p2
, p2_charlen
);
6191 if (RE_TRANSLATE (translate
, p1_ch
)
6192 != RE_TRANSLATE (translate
, p2_ch
))
6195 p1
+= p1_charlen
, p2
+= p2_charlen
;
6198 if (p1
!= p1_end
|| p2
!= p2_end
)
6204 /* Entry points for GNU code. */
6206 /* re_compile_pattern is the GNU regular expression compiler: it
6207 compiles PATTERN (of length SIZE) and puts the result in BUFP.
6208 Returns 0 if the pattern was valid, otherwise an error string.
6210 Assumes the `allocated' (and perhaps `buffer') and `translate' fields
6211 are set in BUFP on entry.
6213 We call regex_compile to do the actual compilation. */
6216 re_compile_pattern (pattern
, length
, bufp
)
6217 const char *pattern
;
6219 struct re_pattern_buffer
*bufp
;
6223 /* GNU code is written to assume at least RE_NREGS registers will be set
6224 (and at least one extra will be -1). */
6225 bufp
->regs_allocated
= REGS_UNALLOCATED
;
6227 /* And GNU code determines whether or not to get register information
6228 by passing null for the REGS argument to re_match, etc., not by
6232 ret
= regex_compile ((re_char
*) pattern
, length
, re_syntax_options
, bufp
);
6236 return gettext (re_error_msgid
[(int) ret
]);
6238 WEAK_ALIAS (__re_compile_pattern
, re_compile_pattern
)
6240 /* Entry points compatible with 4.2 BSD regex library. We don't define
6241 them unless specifically requested. */
6243 #if defined _REGEX_RE_COMP || defined _LIBC
6245 /* BSD has one and only one pattern buffer. */
6246 static struct re_pattern_buffer re_comp_buf
;
6250 /* Make these definitions weak in libc, so POSIX programs can redefine
6251 these names if they don't use our functions, and still use
6252 regcomp/regexec below without link errors. */
6262 if (!re_comp_buf
.buffer
)
6263 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6264 return (char *) gettext ("No previous regular expression");
6268 if (!re_comp_buf
.buffer
)
6270 re_comp_buf
.buffer
= (unsigned char *) malloc (200);
6271 if (re_comp_buf
.buffer
== NULL
)
6272 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6273 return (char *) gettext (re_error_msgid
[(int) REG_ESPACE
]);
6274 re_comp_buf
.allocated
= 200;
6276 re_comp_buf
.fastmap
= (char *) malloc (1 << BYTEWIDTH
);
6277 if (re_comp_buf
.fastmap
== NULL
)
6278 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6279 return (char *) gettext (re_error_msgid
[(int) REG_ESPACE
]);
6282 /* Since `re_exec' always passes NULL for the `regs' argument, we
6283 don't need to initialize the pattern buffer fields which affect it. */
6285 ret
= regex_compile (s
, strlen (s
), re_syntax_options
, &re_comp_buf
);
6290 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6291 return (char *) gettext (re_error_msgid
[(int) ret
]);
6302 const int len
= strlen (s
);
6304 0 <= re_search (&re_comp_buf
, s
, len
, 0, len
, (struct re_registers
*) 0);
6306 #endif /* _REGEX_RE_COMP */
6308 /* POSIX.2 functions. Don't define these for Emacs. */
6312 /* regcomp takes a regular expression as a string and compiles it.
6314 PREG is a regex_t *. We do not expect any fields to be initialized,
6315 since POSIX says we shouldn't. Thus, we set
6317 `buffer' to the compiled pattern;
6318 `used' to the length of the compiled pattern;
6319 `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
6320 REG_EXTENDED bit in CFLAGS is set; otherwise, to
6321 RE_SYNTAX_POSIX_BASIC;
6322 `fastmap' to an allocated space for the fastmap;
6323 `fastmap_accurate' to zero;
6324 `re_nsub' to the number of subexpressions in PATTERN.
6326 PATTERN is the address of the pattern string.
6328 CFLAGS is a series of bits which affect compilation.
6330 If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
6331 use POSIX basic syntax.
6333 If REG_NEWLINE is set, then . and [^...] don't match newline.
6334 Also, regexec will try a match beginning after every newline.
6336 If REG_ICASE is set, then we considers upper- and lowercase
6337 versions of letters to be equivalent when matching.
6339 If REG_NOSUB is set, then when PREG is passed to regexec, that
6340 routine will report only success or failure, and nothing about the
6343 It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
6344 the return codes and their meanings.) */
6347 regcomp (preg
, pattern
, cflags
)
6348 regex_t
*__restrict preg
;
6349 const char *__restrict pattern
;
6354 = (cflags
& REG_EXTENDED
) ?
6355 RE_SYNTAX_POSIX_EXTENDED
: RE_SYNTAX_POSIX_BASIC
;
6357 /* regex_compile will allocate the space for the compiled pattern. */
6359 preg
->allocated
= 0;
6362 /* Try to allocate space for the fastmap. */
6363 preg
->fastmap
= (char *) malloc (1 << BYTEWIDTH
);
6365 if (cflags
& REG_ICASE
)
6370 = (RE_TRANSLATE_TYPE
) malloc (CHAR_SET_SIZE
6371 * sizeof (*(RE_TRANSLATE_TYPE
)0));
6372 if (preg
->translate
== NULL
)
6373 return (int) REG_ESPACE
;
6375 /* Map uppercase characters to corresponding lowercase ones. */
6376 for (i
= 0; i
< CHAR_SET_SIZE
; i
++)
6377 preg
->translate
[i
] = ISUPPER (i
) ? TOLOWER (i
) : i
;
6380 preg
->translate
= NULL
;
6382 /* If REG_NEWLINE is set, newlines are treated differently. */
6383 if (cflags
& REG_NEWLINE
)
6384 { /* REG_NEWLINE implies neither . nor [^...] match newline. */
6385 syntax
&= ~RE_DOT_NEWLINE
;
6386 syntax
|= RE_HAT_LISTS_NOT_NEWLINE
;
6389 syntax
|= RE_NO_NEWLINE_ANCHOR
;
6391 preg
->no_sub
= !!(cflags
& REG_NOSUB
);
6393 /* POSIX says a null character in the pattern terminates it, so we
6394 can use strlen here in compiling the pattern. */
6395 ret
= regex_compile ((re_char
*) pattern
, strlen (pattern
), syntax
, preg
);
6397 /* POSIX doesn't distinguish between an unmatched open-group and an
6398 unmatched close-group: both are REG_EPAREN. */
6399 if (ret
== REG_ERPAREN
)
6402 if (ret
== REG_NOERROR
&& preg
->fastmap
)
6403 { /* Compute the fastmap now, since regexec cannot modify the pattern
6405 re_compile_fastmap (preg
);
6406 if (preg
->can_be_null
)
6407 { /* The fastmap can't be used anyway. */
6408 free (preg
->fastmap
);
6409 preg
->fastmap
= NULL
;
6414 WEAK_ALIAS (__regcomp
, regcomp
)
6417 /* regexec searches for a given pattern, specified by PREG, in the
6420 If NMATCH is zero or REG_NOSUB was set in the cflags argument to
6421 `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
6422 least NMATCH elements, and we set them to the offsets of the
6423 corresponding matched substrings.
6425 EFLAGS specifies `execution flags' which affect matching: if
6426 REG_NOTBOL is set, then ^ does not match at the beginning of the
6427 string; if REG_NOTEOL is set, then $ does not match at the end.
6429 We return 0 if we find a match and REG_NOMATCH if not. */
6432 regexec (preg
, string
, nmatch
, pmatch
, eflags
)
6433 const regex_t
*__restrict preg
;
6434 const char *__restrict string
;
6436 regmatch_t pmatch
[__restrict_arr
];
6440 struct re_registers regs
;
6441 regex_t private_preg
;
6442 int len
= strlen (string
);
6443 boolean want_reg_info
= !preg
->no_sub
&& nmatch
> 0 && pmatch
;
6445 private_preg
= *preg
;
6447 private_preg
.not_bol
= !!(eflags
& REG_NOTBOL
);
6448 private_preg
.not_eol
= !!(eflags
& REG_NOTEOL
);
6450 /* The user has told us exactly how many registers to return
6451 information about, via `nmatch'. We have to pass that on to the
6452 matching routines. */
6453 private_preg
.regs_allocated
= REGS_FIXED
;
6457 regs
.num_regs
= nmatch
;
6458 regs
.start
= TALLOC (nmatch
* 2, regoff_t
);
6459 if (regs
.start
== NULL
)
6460 return (int) REG_NOMATCH
;
6461 regs
.end
= regs
.start
+ nmatch
;
6464 /* Instead of using not_eol to implement REG_NOTEOL, we could simply
6465 pass (&private_preg, string, len + 1, 0, len, ...) pretending the string
6466 was a little bit longer but still only matching the real part.
6467 This works because the `endline' will check for a '\n' and will find a
6468 '\0', correctly deciding that this is not the end of a line.
6469 But it doesn't work out so nicely for REG_NOTBOL, since we don't have
6470 a convenient '\0' there. For all we know, the string could be preceded
6471 by '\n' which would throw things off. */
6473 /* Perform the searching operation. */
6474 ret
= re_search (&private_preg
, string
, len
,
6475 /* start: */ 0, /* range: */ len
,
6476 want_reg_info
? ®s
: (struct re_registers
*) 0);
6478 /* Copy the register information to the POSIX structure. */
6485 for (r
= 0; r
< nmatch
; r
++)
6487 pmatch
[r
].rm_so
= regs
.start
[r
];
6488 pmatch
[r
].rm_eo
= regs
.end
[r
];
6492 /* If we needed the temporary register info, free the space now. */
6496 /* We want zero return to mean success, unlike `re_search'. */
6497 return ret
>= 0 ? (int) REG_NOERROR
: (int) REG_NOMATCH
;
6499 WEAK_ALIAS (__regexec
, regexec
)
6502 /* Returns a message corresponding to an error code, ERRCODE, returned
6503 from either regcomp or regexec. We don't use PREG here. */
6506 regerror (errcode
, preg
, errbuf
, errbuf_size
)
6508 const regex_t
*preg
;
6516 || errcode
>= (sizeof (re_error_msgid
) / sizeof (re_error_msgid
[0])))
6517 /* Only error codes returned by the rest of the code should be passed
6518 to this routine. If we are given anything else, or if other regex
6519 code generates an invalid error code, then the program has a bug.
6520 Dump core so we can fix it. */
6523 msg
= gettext (re_error_msgid
[errcode
]);
6525 msg_size
= strlen (msg
) + 1; /* Includes the null. */
6527 if (errbuf_size
!= 0)
6529 if (msg_size
> errbuf_size
)
6531 strncpy (errbuf
, msg
, errbuf_size
- 1);
6532 errbuf
[errbuf_size
- 1] = 0;
6535 strcpy (errbuf
, msg
);
6540 WEAK_ALIAS (__regerror
, regerror
)
6543 /* Free dynamically allocated space used by PREG. */
6549 if (preg
->buffer
!= NULL
)
6550 free (preg
->buffer
);
6551 preg
->buffer
= NULL
;
6553 preg
->allocated
= 0;
6556 if (preg
->fastmap
!= NULL
)
6557 free (preg
->fastmap
);
6558 preg
->fastmap
= NULL
;
6559 preg
->fastmap_accurate
= 0;
6561 if (preg
->translate
!= NULL
)
6562 free (preg
->translate
);
6563 preg
->translate
= NULL
;
6565 WEAK_ALIAS (__regfree
, regfree
)
6567 #endif /* not emacs */
6569 /* arch-tag: 4ffd68ba-2a9e-435b-a21a-018990f9eeb2
6570 (do not change this comment) */