1 /* Extended regular expression matching and search library, version
2 0.12. (Implements POSIX draft P1003.2/D11.2, except for some of the
3 internationalization features.)
5 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
6 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
24 - structure the opcode space into opcode+flag.
25 - merge with glibc's regex.[ch].
26 - replace (succeed_n + jump_n + set_number_at) with something that doesn't
27 need to modify the compiled regexp so that re_match can be reentrant.
28 - get rid of on_failure_jump_smart by doing the optimization in re_comp
29 rather than at run-time, so that re_match can be reentrant.
32 /* AIX requires this to be the first thing in the file. */
33 #if defined _AIX && !defined REGEX_MALLOC
41 #if defined STDC_HEADERS && !defined emacs
44 /* We need this for `regex.h', and perhaps for the Emacs include files. */
45 # include <sys/types.h>
48 /* Whether to use ISO C Amendment 1 wide char functions.
49 Those should not be used for Emacs since it uses its own. */
51 #define WIDE_CHAR_SUPPORT 1
53 #define WIDE_CHAR_SUPPORT \
54 (HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_BTOWC && !emacs)
57 /* For platform which support the ISO C amendement 1 functionality we
58 support user defined character classes. */
60 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
66 /* We have to keep the namespace clean. */
67 # define regfree(preg) __regfree (preg)
68 # define regexec(pr, st, nm, pm, ef) __regexec (pr, st, nm, pm, ef)
69 # define regcomp(preg, pattern, cflags) __regcomp (preg, pattern, cflags)
70 # define regerror(errcode, preg, errbuf, errbuf_size) \
71 __regerror(errcode, preg, errbuf, errbuf_size)
72 # define re_set_registers(bu, re, nu, st, en) \
73 __re_set_registers (bu, re, nu, st, en)
74 # define re_match_2(bufp, string1, size1, string2, size2, pos, regs, stop) \
75 __re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
76 # define re_match(bufp, string, size, pos, regs) \
77 __re_match (bufp, string, size, pos, regs)
78 # define re_search(bufp, string, size, startpos, range, regs) \
79 __re_search (bufp, string, size, startpos, range, regs)
80 # define re_compile_pattern(pattern, length, bufp) \
81 __re_compile_pattern (pattern, length, bufp)
82 # define re_set_syntax(syntax) __re_set_syntax (syntax)
83 # define re_search_2(bufp, st1, s1, st2, s2, startpos, range, regs, stop) \
84 __re_search_2 (bufp, st1, s1, st2, s2, startpos, range, regs, stop)
85 # define re_compile_fastmap(bufp) __re_compile_fastmap (bufp)
87 /* Make sure we call libc's function even if the user overrides them. */
88 # define btowc __btowc
89 # define iswctype __iswctype
90 # define wctype __wctype
92 # define WEAK_ALIAS(a,b) weak_alias (a, b)
94 /* We are also using some library internals. */
95 # include <locale/localeinfo.h>
96 # include <locale/elem-hash.h>
97 # include <langinfo.h>
99 # define WEAK_ALIAS(a,b)
102 /* This is for other GNU distributions with internationalized messages. */
103 #if HAVE_LIBINTL_H || defined _LIBC
104 # include <libintl.h>
106 # define gettext(msgid) (msgid)
110 /* This define is so xgettext can find the internationalizable
112 # define gettext_noop(String) String
115 /* The `emacs' switch turns on certain matching commands
116 that make sense only in Emacs. */
122 /* Make syntax table lookup grant data in gl_state. */
123 # define SYNTAX_ENTRY_VIA_PROPERTY
126 # include "charset.h"
127 # include "category.h"
132 # define malloc xmalloc
136 # define realloc xrealloc
142 /* Converts the pointer to the char to BEG-based offset from the start. */
143 # define PTR_TO_OFFSET(d) POS_AS_IN_BUFFER (POINTER_TO_OFFSET (d))
144 # define POS_AS_IN_BUFFER(p) ((p) + (NILP (re_match_object) || BUFFERP (re_match_object)))
146 # define RE_MULTIBYTE_P(bufp) ((bufp)->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 multibyte) character before P. P points into a
153 string which is the virtual concatenation of STR1 (which ends at
154 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 re_char *d0 = dtemp; \
162 PREV_CHAR_BOUNDARY (d0, dlimit); \
163 c = STRING_CHAR (d0, dtemp - d0); \
166 (c = ((p) == (str2) ? (end1) : (p))[-1]); \
170 #else /* not emacs */
172 /* If we are not linking with Emacs proper,
173 we can't use the relocating allocator
174 even if config.h says that we can. */
177 # if defined STDC_HEADERS || defined _LIBC
184 /* When used in Emacs's lib-src, we need xmalloc and xrealloc. */
191 val
= (void *) malloc (size
);
194 write (2, "virtual memory exhausted\n", 25);
201 xrealloc (block
, size
)
206 /* We must call malloc explicitly when BLOCK is 0, since some
207 reallocs don't do this. */
209 val
= (void *) malloc (size
);
211 val
= (void *) realloc (block
, size
);
214 write (2, "virtual memory exhausted\n", 25);
223 # define malloc xmalloc
227 # define realloc xrealloc
229 /* When used in Emacs's lib-src, we need to get bzero and bcopy somehow.
230 If nothing else has been done, use the method below. */
231 # ifdef INHIBIT_STRING_HEADER
232 # if !(defined HAVE_BZERO && defined HAVE_BCOPY)
233 # if !defined bzero && !defined bcopy
234 # undef INHIBIT_STRING_HEADER
239 /* This is the normal way of making sure we have memcpy, memcmp and bzero.
240 This is used in most programs--a few other programs avoid this
241 by defining INHIBIT_STRING_HEADER. */
242 # ifndef INHIBIT_STRING_HEADER
243 # if defined HAVE_STRING_H || defined STDC_HEADERS || defined _LIBC
247 # define bzero(s, n) (memset (s, '\0', n), (s))
249 # define bzero(s, n) __bzero (s, n)
253 # include <strings.h>
255 # define memcmp(s1, s2, n) bcmp (s1, s2, n)
258 # define memcpy(d, s, n) (bcopy (s, d, n), (d))
263 /* Define the syntax stuff for \<, \>, etc. */
265 /* Sword must be nonzero for the wordchar pattern commands in re_match_2. */
266 enum syntaxcode
{ Swhitespace
= 0, Sword
= 1, Ssymbol
= 2 };
268 # ifdef SWITCH_ENUM_BUG
269 # define SWITCH_ENUM_CAST(x) ((int)(x))
271 # define SWITCH_ENUM_CAST(x) (x)
274 /* Dummy macros for non-Emacs environments. */
275 # define BASE_LEADING_CODE_P(c) (0)
276 # define CHAR_CHARSET(c) 0
277 # define CHARSET_LEADING_CODE_BASE(c) 0
278 # define MAX_MULTIBYTE_LENGTH 1
279 # define RE_MULTIBYTE_P(x) 0
280 # define WORD_BOUNDARY_P(c1, c2) (0)
281 # define CHAR_HEAD_P(p) (1)
282 # define SINGLE_BYTE_CHAR_P(c) (1)
283 # define SAME_CHARSET_P(c1, c2) (1)
284 # define MULTIBYTE_FORM_LENGTH(p, s) (1)
285 # define PREV_CHAR_BOUNDARY(p, limit) ((p)--)
286 # define STRING_CHAR(p, s) (*(p))
287 # define RE_STRING_CHAR STRING_CHAR
288 # define CHAR_STRING(c, s) (*(s) = (c), 1)
289 # define STRING_CHAR_AND_LENGTH(p, s, actual_len) ((actual_len) = 1, *(p))
290 # define RE_STRING_CHAR_AND_LENGTH STRING_CHAR_AND_LENGTH
291 # define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2) \
292 (c = ((p) == (str2) ? *((end1) - 1) : *((p) - 1)))
293 # define MAKE_CHAR(charset, c1, c2) (c1)
294 #endif /* not emacs */
297 # define RE_TRANSLATE(TBL, C) ((unsigned char)(TBL)[C])
298 # define RE_TRANSLATE_P(TBL) (TBL)
301 /* Get the interface, including the syntax bits. */
304 /* isalpha etc. are used for the character classes. */
309 /* 1 if C is an ASCII character. */
310 # define IS_REAL_ASCII(c) ((c) < 0200)
312 /* 1 if C is a unibyte character. */
313 # define ISUNIBYTE(c) (SINGLE_BYTE_CHAR_P ((c)))
315 /* The Emacs definitions should not be directly affected by locales. */
317 /* In Emacs, these are only used for single-byte characters. */
318 # define ISDIGIT(c) ((c) >= '0' && (c) <= '9')
319 # define ISCNTRL(c) ((c) < ' ')
320 # define ISXDIGIT(c) (((c) >= '0' && (c) <= '9') \
321 || ((c) >= 'a' && (c) <= 'f') \
322 || ((c) >= 'A' && (c) <= 'F'))
324 /* This is only used for single-byte characters. */
325 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
327 /* The rest must handle multibyte characters. */
329 # define ISGRAPH(c) (SINGLE_BYTE_CHAR_P (c) \
330 ? (c) > ' ' && !((c) >= 0177 && (c) <= 0237) \
333 # define ISPRINT(c) (SINGLE_BYTE_CHAR_P (c) \
334 ? (c) >= ' ' && !((c) >= 0177 && (c) <= 0237) \
337 # define ISALNUM(c) (IS_REAL_ASCII (c) \
338 ? (((c) >= 'a' && (c) <= 'z') \
339 || ((c) >= 'A' && (c) <= 'Z') \
340 || ((c) >= '0' && (c) <= '9')) \
341 : SYNTAX (c) == Sword)
343 # define ISALPHA(c) (IS_REAL_ASCII (c) \
344 ? (((c) >= 'a' && (c) <= 'z') \
345 || ((c) >= 'A' && (c) <= 'Z')) \
346 : SYNTAX (c) == Sword)
348 # define ISLOWER(c) (LOWERCASEP (c))
350 # define ISPUNCT(c) (IS_REAL_ASCII (c) \
351 ? ((c) > ' ' && (c) < 0177 \
352 && !(((c) >= 'a' && (c) <= 'z') \
353 || ((c) >= 'A' && (c) <= 'Z') \
354 || ((c) >= '0' && (c) <= '9'))) \
355 : SYNTAX (c) != Sword)
357 # define ISSPACE(c) (SYNTAX (c) == Swhitespace)
359 # define ISUPPER(c) (UPPERCASEP (c))
361 # define ISWORD(c) (SYNTAX (c) == Sword)
363 #else /* not emacs */
365 /* Jim Meyering writes:
367 "... Some ctype macros are valid only for character codes that
368 isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
369 using /bin/cc or gcc but without giving an ansi option). So, all
370 ctype uses should be through macros like ISPRINT... If
371 STDC_HEADERS is defined, then autoconf has verified that the ctype
372 macros don't need to be guarded with references to isascii. ...
373 Defining isascii to 1 should let any compiler worth its salt
374 eliminate the && through constant folding."
375 Solaris defines some of these symbols so we must undefine them first. */
378 # if defined STDC_HEADERS || (!defined isascii && !defined HAVE_ISASCII)
379 # define ISASCII(c) 1
381 # define ISASCII(c) isascii(c)
384 /* 1 if C is an ASCII character. */
385 # define IS_REAL_ASCII(c) ((c) < 0200)
387 /* This distinction is not meaningful, except in Emacs. */
388 # define ISUNIBYTE(c) 1
391 # define ISBLANK(c) (ISASCII (c) && isblank (c))
393 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
396 # define ISGRAPH(c) (ISASCII (c) && isgraph (c))
398 # define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
402 # define ISPRINT(c) (ISASCII (c) && isprint (c))
403 # define ISDIGIT(c) (ISASCII (c) && isdigit (c))
404 # define ISALNUM(c) (ISASCII (c) && isalnum (c))
405 # define ISALPHA(c) (ISASCII (c) && isalpha (c))
406 # define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
407 # define ISLOWER(c) (ISASCII (c) && islower (c))
408 # define ISPUNCT(c) (ISASCII (c) && ispunct (c))
409 # define ISSPACE(c) (ISASCII (c) && isspace (c))
410 # define ISUPPER(c) (ISASCII (c) && isupper (c))
411 # define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
413 # define ISWORD(c) ISALPHA(c)
416 # define TOLOWER(c) _tolower(c)
418 # define TOLOWER(c) tolower(c)
421 /* How many characters in the character set. */
422 # define CHAR_SET_SIZE 256
426 extern char *re_syntax_table
;
428 # else /* not SYNTAX_TABLE */
430 static char re_syntax_table
[CHAR_SET_SIZE
];
441 bzero (re_syntax_table
, sizeof re_syntax_table
);
443 for (c
= 0; c
< CHAR_SET_SIZE
; ++c
)
445 re_syntax_table
[c
] = Sword
;
447 re_syntax_table
['_'] = Ssymbol
;
452 # endif /* not SYNTAX_TABLE */
454 # define SYNTAX(c) re_syntax_table[(c)]
456 #endif /* not emacs */
459 # define NULL (void *)0
462 /* We remove any previous definition of `SIGN_EXTEND_CHAR',
463 since ours (we hope) works properly with all combinations of
464 machines, compilers, `char' and `unsigned char' argument types.
465 (Per Bothner suggested the basic approach.) */
466 #undef SIGN_EXTEND_CHAR
468 # define SIGN_EXTEND_CHAR(c) ((signed char) (c))
469 #else /* not __STDC__ */
470 /* As in Harbison and Steele. */
471 # define SIGN_EXTEND_CHAR(c) ((((unsigned char) (c)) ^ 128) - 128)
474 /* Should we use malloc or alloca? If REGEX_MALLOC is not defined, we
475 use `alloca' instead of `malloc'. This is because using malloc in
476 re_search* or re_match* could cause memory leaks when C-g is used in
477 Emacs; also, malloc is slower and causes storage fragmentation. On
478 the other hand, malloc is more portable, and easier to debug.
480 Because we sometimes use alloca, some routines have to be macros,
481 not functions -- `alloca'-allocated space disappears at the end of the
482 function it is called in. */
486 # define REGEX_ALLOCATE malloc
487 # define REGEX_REALLOCATE(source, osize, nsize) realloc (source, nsize)
488 # define REGEX_FREE free
490 #else /* not REGEX_MALLOC */
492 /* Emacs already defines alloca, sometimes. */
495 /* Make alloca work the best possible way. */
497 # define alloca __builtin_alloca
498 # else /* not __GNUC__ */
501 # endif /* HAVE_ALLOCA_H */
502 # endif /* not __GNUC__ */
504 # endif /* not alloca */
506 # define REGEX_ALLOCATE alloca
508 /* Assumes a `char *destination' variable. */
509 # define REGEX_REALLOCATE(source, osize, nsize) \
510 (destination = (char *) alloca (nsize), \
511 memcpy (destination, source, osize))
513 /* No need to do anything to free, after alloca. */
514 # define REGEX_FREE(arg) ((void)0) /* Do nothing! But inhibit gcc warning. */
516 #endif /* not REGEX_MALLOC */
518 /* Define how to allocate the failure stack. */
520 #if defined REL_ALLOC && defined REGEX_MALLOC
522 # define REGEX_ALLOCATE_STACK(size) \
523 r_alloc (&failure_stack_ptr, (size))
524 # define REGEX_REALLOCATE_STACK(source, osize, nsize) \
525 r_re_alloc (&failure_stack_ptr, (nsize))
526 # define REGEX_FREE_STACK(ptr) \
527 r_alloc_free (&failure_stack_ptr)
529 #else /* not using relocating allocator */
533 # define REGEX_ALLOCATE_STACK malloc
534 # define REGEX_REALLOCATE_STACK(source, osize, nsize) realloc (source, nsize)
535 # define REGEX_FREE_STACK free
537 # else /* not REGEX_MALLOC */
539 # define REGEX_ALLOCATE_STACK alloca
541 # define REGEX_REALLOCATE_STACK(source, osize, nsize) \
542 REGEX_REALLOCATE (source, osize, nsize)
543 /* No need to explicitly free anything. */
544 # define REGEX_FREE_STACK(arg) ((void)0)
546 # endif /* not REGEX_MALLOC */
547 #endif /* not using relocating allocator */
550 /* True if `size1' is non-NULL and PTR is pointing anywhere inside
551 `string1' or just past its end. This works if PTR is NULL, which is
553 #define FIRST_STRING_P(ptr) \
554 (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
556 /* (Re)Allocate N items of type T using malloc, or fail. */
557 #define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t)))
558 #define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
559 #define RETALLOC_IF(addr, n, t) \
560 if (addr) RETALLOC((addr), (n), t); else (addr) = TALLOC ((n), t)
561 #define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
563 #define BYTEWIDTH 8 /* In bits. */
565 #define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
569 #define MAX(a, b) ((a) > (b) ? (a) : (b))
570 #define MIN(a, b) ((a) < (b) ? (a) : (b))
572 /* Type of source-pattern and string chars. */
573 typedef const unsigned char re_char
;
575 typedef char boolean
;
579 static int re_match_2_internal
_RE_ARGS ((struct re_pattern_buffer
*bufp
,
580 re_char
*string1
, int size1
,
581 re_char
*string2
, int size2
,
583 struct re_registers
*regs
,
586 /* These are the command codes that appear in compiled regular
587 expressions. Some opcodes are followed by argument bytes. A
588 command code can specify any interpretation whatsoever for its
589 arguments. Zero bytes may appear in the compiled regular expression. */
595 /* Succeed right away--no more backtracking. */
598 /* Followed by one byte giving n, then by n literal bytes. */
601 /* Matches any (more or less) character. */
604 /* Matches any one char belonging to specified set. First
605 following byte is number of bitmap bytes. Then come bytes
606 for a bitmap saying which chars are in. Bits in each byte
607 are ordered low-bit-first. A character is in the set if its
608 bit is 1. A character too large to have a bit in the map is
609 automatically not in the set.
611 If the length byte has the 0x80 bit set, then that stuff
612 is followed by a range table:
613 2 bytes of flags for character sets (low 8 bits, high 8 bits)
614 See RANGE_TABLE_WORK_BITS below.
615 2 bytes, the number of pairs that follow (upto 32767)
616 pairs, each 2 multibyte characters,
617 each multibyte character represented as 3 bytes. */
620 /* Same parameters as charset, but match any character that is
621 not one of those specified. */
624 /* Start remembering the text that is matched, for storing in a
625 register. Followed by one byte with the register number, in
626 the range 0 to one less than the pattern buffer's re_nsub
630 /* Stop remembering the text that is matched and store it in a
631 memory register. Followed by one byte with the register
632 number, in the range 0 to one less than `re_nsub' in the
636 /* Match a duplicate of something remembered. Followed by one
637 byte containing the register number. */
640 /* Fail unless at beginning of line. */
643 /* Fail unless at end of line. */
646 /* Succeeds if at beginning of buffer (if emacs) or at beginning
647 of string to be matched (if not). */
650 /* Analogously, for end of buffer/string. */
653 /* Followed by two byte relative address to which to jump. */
656 /* Followed by two-byte relative address of place to resume at
657 in case of failure. */
660 /* Like on_failure_jump, but pushes a placeholder instead of the
661 current string position when executed. */
662 on_failure_keep_string_jump
,
664 /* Just like `on_failure_jump', except that it checks that we
665 don't get stuck in an infinite loop (matching an empty string
667 on_failure_jump_loop
,
669 /* Just like `on_failure_jump_loop', except that it checks for
670 a different kind of loop (the kind that shows up with non-greedy
671 operators). This operation has to be immediately preceded
673 on_failure_jump_nastyloop
,
675 /* A smart `on_failure_jump' used for greedy * and + operators.
676 It analyses the loop before which it is put and if the
677 loop does not require backtracking, it changes itself to
678 `on_failure_keep_string_jump' and short-circuits the loop,
679 else it just defaults to changing itself into `on_failure_jump'.
680 It assumes that it is pointing to just past a `jump'. */
681 on_failure_jump_smart
,
683 /* Followed by two-byte relative address and two-byte number n.
684 After matching N times, jump to the address upon failure.
685 Does not work if N starts at 0: use on_failure_jump_loop
689 /* Followed by two-byte relative address, and two-byte number n.
690 Jump to the address N times, then fail. */
693 /* Set the following two-byte relative address to the
694 subsequent two-byte number. The address *includes* the two
698 wordbeg
, /* Succeeds if at word beginning. */
699 wordend
, /* Succeeds if at word end. */
701 wordbound
, /* Succeeds if at a word boundary. */
702 notwordbound
, /* Succeeds if not at a word boundary. */
704 symbeg
, /* Succeeds if at symbol beginning. */
705 symend
, /* Succeeds if at symbol end. */
707 /* Matches any character whose syntax is specified. Followed by
708 a byte which contains a syntax code, e.g., Sword. */
711 /* Matches any character whose syntax is not that specified. */
715 ,before_dot
, /* Succeeds if before point. */
716 at_dot
, /* Succeeds if at point. */
717 after_dot
, /* Succeeds if after point. */
719 /* Matches any character whose category-set contains the specified
720 category. The operator is followed by a byte which contains a
721 category code (mnemonic ASCII character). */
724 /* Matches any character whose category-set does not contain the
725 specified category. The operator is followed by a byte which
726 contains the category code (mnemonic ASCII character). */
731 /* Common operations on the compiled pattern. */
733 /* Store NUMBER in two contiguous bytes starting at DESTINATION. */
735 #define STORE_NUMBER(destination, number) \
737 (destination)[0] = (number) & 0377; \
738 (destination)[1] = (number) >> 8; \
741 /* Same as STORE_NUMBER, except increment DESTINATION to
742 the byte after where the number is stored. Therefore, DESTINATION
743 must be an lvalue. */
745 #define STORE_NUMBER_AND_INCR(destination, number) \
747 STORE_NUMBER (destination, number); \
748 (destination) += 2; \
751 /* Put into DESTINATION a number stored in two contiguous bytes starting
754 #define EXTRACT_NUMBER(destination, source) \
756 (destination) = *(source) & 0377; \
757 (destination) += SIGN_EXTEND_CHAR (*((source) + 1)) << 8; \
761 static void extract_number
_RE_ARGS ((int *dest
, re_char
*source
));
763 extract_number (dest
, source
)
767 int temp
= SIGN_EXTEND_CHAR (*(source
+ 1));
768 *dest
= *source
& 0377;
772 # ifndef EXTRACT_MACROS /* To debug the macros. */
773 # undef EXTRACT_NUMBER
774 # define EXTRACT_NUMBER(dest, src) extract_number (&dest, src)
775 # endif /* not EXTRACT_MACROS */
779 /* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
780 SOURCE must be an lvalue. */
782 #define EXTRACT_NUMBER_AND_INCR(destination, source) \
784 EXTRACT_NUMBER (destination, source); \
789 static void extract_number_and_incr
_RE_ARGS ((int *destination
,
792 extract_number_and_incr (destination
, source
)
796 extract_number (destination
, *source
);
800 # ifndef EXTRACT_MACROS
801 # undef EXTRACT_NUMBER_AND_INCR
802 # define EXTRACT_NUMBER_AND_INCR(dest, src) \
803 extract_number_and_incr (&dest, &src)
804 # endif /* not EXTRACT_MACROS */
808 /* Store a multibyte character in three contiguous bytes starting
809 DESTINATION, and increment DESTINATION to the byte after where the
810 character is stored. Therefore, DESTINATION must be an lvalue. */
812 #define STORE_CHARACTER_AND_INCR(destination, character) \
814 (destination)[0] = (character) & 0377; \
815 (destination)[1] = ((character) >> 8) & 0377; \
816 (destination)[2] = (character) >> 16; \
817 (destination) += 3; \
820 /* Put into DESTINATION a character stored in three contiguous bytes
821 starting at SOURCE. */
823 #define EXTRACT_CHARACTER(destination, source) \
825 (destination) = ((source)[0] \
826 | ((source)[1] << 8) \
827 | ((source)[2] << 16)); \
831 /* Macros for charset. */
833 /* Size of bitmap of charset P in bytes. P is a start of charset,
834 i.e. *P is (re_opcode_t) charset or (re_opcode_t) charset_not. */
835 #define CHARSET_BITMAP_SIZE(p) ((p)[1] & 0x7F)
837 /* Nonzero if charset P has range table. */
838 #define CHARSET_RANGE_TABLE_EXISTS_P(p) ((p)[1] & 0x80)
840 /* Return the address of range table of charset P. But not the start
841 of table itself, but the before where the number of ranges is
842 stored. `2 +' means to skip re_opcode_t and size of bitmap,
843 and the 2 bytes of flags at the start of the range table. */
844 #define CHARSET_RANGE_TABLE(p) (&(p)[4 + CHARSET_BITMAP_SIZE (p)])
846 /* Extract the bit flags that start a range table. */
847 #define CHARSET_RANGE_TABLE_BITS(p) \
848 ((p)[2 + CHARSET_BITMAP_SIZE (p)] \
849 + (p)[3 + CHARSET_BITMAP_SIZE (p)] * 0x100)
851 /* Test if C is listed in the bitmap of charset P. */
852 #define CHARSET_LOOKUP_BITMAP(p, c) \
853 ((c) < CHARSET_BITMAP_SIZE (p) * BYTEWIDTH \
854 && (p)[2 + (c) / BYTEWIDTH] & (1 << ((c) % BYTEWIDTH)))
856 /* Return the address of end of RANGE_TABLE. COUNT is number of
857 ranges (which is a pair of (start, end)) in the RANGE_TABLE. `* 2'
858 is start of range and end of range. `* 3' is size of each start
860 #define CHARSET_RANGE_TABLE_END(range_table, count) \
861 ((range_table) + (count) * 2 * 3)
863 /* Test if C is in RANGE_TABLE. A flag NOT is negated if C is in.
864 COUNT is number of ranges in RANGE_TABLE. */
865 #define CHARSET_LOOKUP_RANGE_TABLE_RAW(not, c, range_table, count) \
868 re_wchar_t range_start, range_end; \
870 re_char *range_table_end \
871 = CHARSET_RANGE_TABLE_END ((range_table), (count)); \
873 for (p = (range_table); p < range_table_end; p += 2 * 3) \
875 EXTRACT_CHARACTER (range_start, p); \
876 EXTRACT_CHARACTER (range_end, p + 3); \
878 if (range_start <= (c) && (c) <= range_end) \
887 /* Test if C is in range table of CHARSET. The flag NOT is negated if
888 C is listed in it. */
889 #define CHARSET_LOOKUP_RANGE_TABLE(not, c, charset) \
892 /* Number of ranges in range table. */ \
894 re_char *range_table = CHARSET_RANGE_TABLE (charset); \
896 EXTRACT_NUMBER_AND_INCR (count, range_table); \
897 CHARSET_LOOKUP_RANGE_TABLE_RAW ((not), (c), range_table, count); \
901 /* If DEBUG is defined, Regex prints many voluminous messages about what
902 it is doing (if the variable `debug' is nonzero). If linked with the
903 main program in `iregex.c', you can enter patterns and strings
904 interactively. And if linked with the main program in `main.c' and
905 the other test files, you can run the already-written tests. */
909 /* We use standard I/O for debugging. */
912 /* It is useful to test things that ``must'' be true when debugging. */
915 static int debug
= -100000;
917 # define DEBUG_STATEMENT(e) e
918 # define DEBUG_PRINT1(x) if (debug > 0) printf (x)
919 # define DEBUG_PRINT2(x1, x2) if (debug > 0) printf (x1, x2)
920 # define DEBUG_PRINT3(x1, x2, x3) if (debug > 0) printf (x1, x2, x3)
921 # define DEBUG_PRINT4(x1, x2, x3, x4) if (debug > 0) printf (x1, x2, x3, x4)
922 # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) \
923 if (debug > 0) print_partial_compiled_pattern (s, e)
924 # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \
925 if (debug > 0) print_double_string (w, s1, sz1, s2, sz2)
928 /* Print the fastmap in human-readable form. */
931 print_fastmap (fastmap
)
934 unsigned was_a_range
= 0;
937 while (i
< (1 << BYTEWIDTH
))
943 while (i
< (1 << BYTEWIDTH
) && fastmap
[i
])
959 /* Print a compiled pattern string in human-readable form, starting at
960 the START pointer into it and ending just before the pointer END. */
963 print_partial_compiled_pattern (start
, end
)
973 fprintf (stderr
, "(null)\n");
977 /* Loop over pattern commands. */
980 fprintf (stderr
, "%d:\t", p
- start
);
982 switch ((re_opcode_t
) *p
++)
985 fprintf (stderr
, "/no_op");
989 fprintf (stderr
, "/succeed");
994 fprintf (stderr
, "/exactn/%d", mcnt
);
997 fprintf (stderr
, "/%c", *p
++);
1003 fprintf (stderr
, "/start_memory/%d", *p
++);
1007 fprintf (stderr
, "/stop_memory/%d", *p
++);
1011 fprintf (stderr
, "/duplicate/%d", *p
++);
1015 fprintf (stderr
, "/anychar");
1021 register int c
, last
= -100;
1022 register int in_range
= 0;
1023 int length
= CHARSET_BITMAP_SIZE (p
- 1);
1024 int has_range_table
= CHARSET_RANGE_TABLE_EXISTS_P (p
- 1);
1026 fprintf (stderr
, "/charset [%s",
1027 (re_opcode_t
) *(p
- 1) == charset_not
? "^" : "");
1030 fprintf (stderr
, " !extends past end of pattern! ");
1032 for (c
= 0; c
< 256; c
++)
1034 && (p
[1 + (c
/8)] & (1 << (c
% 8))))
1036 /* Are we starting a range? */
1037 if (last
+ 1 == c
&& ! in_range
)
1039 fprintf (stderr
, "-");
1042 /* Have we broken a range? */
1043 else if (last
+ 1 != c
&& in_range
)
1045 fprintf (stderr
, "%c", last
);
1050 fprintf (stderr
, "%c", c
);
1056 fprintf (stderr
, "%c", last
);
1058 fprintf (stderr
, "]");
1062 if (has_range_table
)
1065 fprintf (stderr
, "has-range-table");
1067 /* ??? Should print the range table; for now, just skip it. */
1068 p
+= 2; /* skip range table bits */
1069 EXTRACT_NUMBER_AND_INCR (count
, p
);
1070 p
= CHARSET_RANGE_TABLE_END (p
, count
);
1076 fprintf (stderr
, "/begline");
1080 fprintf (stderr
, "/endline");
1083 case on_failure_jump
:
1084 extract_number_and_incr (&mcnt
, &p
);
1085 fprintf (stderr
, "/on_failure_jump to %d", p
+ mcnt
- start
);
1088 case on_failure_keep_string_jump
:
1089 extract_number_and_incr (&mcnt
, &p
);
1090 fprintf (stderr
, "/on_failure_keep_string_jump to %d", p
+ mcnt
- start
);
1093 case on_failure_jump_nastyloop
:
1094 extract_number_and_incr (&mcnt
, &p
);
1095 fprintf (stderr
, "/on_failure_jump_nastyloop to %d", p
+ mcnt
- start
);
1098 case on_failure_jump_loop
:
1099 extract_number_and_incr (&mcnt
, &p
);
1100 fprintf (stderr
, "/on_failure_jump_loop to %d", p
+ mcnt
- start
);
1103 case on_failure_jump_smart
:
1104 extract_number_and_incr (&mcnt
, &p
);
1105 fprintf (stderr
, "/on_failure_jump_smart to %d", p
+ mcnt
- start
);
1109 extract_number_and_incr (&mcnt
, &p
);
1110 fprintf (stderr
, "/jump to %d", p
+ mcnt
- start
);
1114 extract_number_and_incr (&mcnt
, &p
);
1115 extract_number_and_incr (&mcnt2
, &p
);
1116 fprintf (stderr
, "/succeed_n to %d, %d times", p
- 2 + mcnt
- start
, mcnt2
);
1120 extract_number_and_incr (&mcnt
, &p
);
1121 extract_number_and_incr (&mcnt2
, &p
);
1122 fprintf (stderr
, "/jump_n to %d, %d times", p
- 2 + mcnt
- start
, mcnt2
);
1126 extract_number_and_incr (&mcnt
, &p
);
1127 extract_number_and_incr (&mcnt2
, &p
);
1128 fprintf (stderr
, "/set_number_at location %d to %d", p
- 2 + mcnt
- start
, mcnt2
);
1132 fprintf (stderr
, "/wordbound");
1136 fprintf (stderr
, "/notwordbound");
1140 fprintf (stderr
, "/wordbeg");
1144 fprintf (stderr
, "/wordend");
1148 fprintf (stderr
, "/symbeg");
1152 fprintf (stderr
, "/symend");
1156 fprintf (stderr
, "/syntaxspec");
1158 fprintf (stderr
, "/%d", mcnt
);
1162 fprintf (stderr
, "/notsyntaxspec");
1164 fprintf (stderr
, "/%d", mcnt
);
1169 fprintf (stderr
, "/before_dot");
1173 fprintf (stderr
, "/at_dot");
1177 fprintf (stderr
, "/after_dot");
1181 fprintf (stderr
, "/categoryspec");
1183 fprintf (stderr
, "/%d", mcnt
);
1186 case notcategoryspec
:
1187 fprintf (stderr
, "/notcategoryspec");
1189 fprintf (stderr
, "/%d", mcnt
);
1194 fprintf (stderr
, "/begbuf");
1198 fprintf (stderr
, "/endbuf");
1202 fprintf (stderr
, "?%d", *(p
-1));
1205 fprintf (stderr
, "\n");
1208 fprintf (stderr
, "%d:\tend of pattern.\n", p
- start
);
1213 print_compiled_pattern (bufp
)
1214 struct re_pattern_buffer
*bufp
;
1216 re_char
*buffer
= bufp
->buffer
;
1218 print_partial_compiled_pattern (buffer
, buffer
+ bufp
->used
);
1219 printf ("%ld bytes used/%ld bytes allocated.\n",
1220 bufp
->used
, bufp
->allocated
);
1222 if (bufp
->fastmap_accurate
&& bufp
->fastmap
)
1224 printf ("fastmap: ");
1225 print_fastmap (bufp
->fastmap
);
1228 printf ("re_nsub: %d\t", bufp
->re_nsub
);
1229 printf ("regs_alloc: %d\t", bufp
->regs_allocated
);
1230 printf ("can_be_null: %d\t", bufp
->can_be_null
);
1231 printf ("no_sub: %d\t", bufp
->no_sub
);
1232 printf ("not_bol: %d\t", bufp
->not_bol
);
1233 printf ("not_eol: %d\t", bufp
->not_eol
);
1234 printf ("syntax: %lx\n", bufp
->syntax
);
1236 /* Perhaps we should print the translate table? */
1241 print_double_string (where
, string1
, size1
, string2
, size2
)
1254 if (FIRST_STRING_P (where
))
1256 for (this_char
= where
- string1
; this_char
< size1
; this_char
++)
1257 putchar (string1
[this_char
]);
1262 for (this_char
= where
- string2
; this_char
< size2
; this_char
++)
1263 putchar (string2
[this_char
]);
1267 #else /* not DEBUG */
1272 # define DEBUG_STATEMENT(e)
1273 # define DEBUG_PRINT1(x)
1274 # define DEBUG_PRINT2(x1, x2)
1275 # define DEBUG_PRINT3(x1, x2, x3)
1276 # define DEBUG_PRINT4(x1, x2, x3, x4)
1277 # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
1278 # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
1280 #endif /* not DEBUG */
1282 /* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
1283 also be assigned to arbitrarily: each pattern buffer stores its own
1284 syntax, so it can be changed between regex compilations. */
1285 /* This has no initializer because initialized variables in Emacs
1286 become read-only after dumping. */
1287 reg_syntax_t re_syntax_options
;
1290 /* Specify the precise syntax of regexps for compilation. This provides
1291 for compatibility for various utilities which historically have
1292 different, incompatible syntaxes.
1294 The argument SYNTAX is a bit mask comprised of the various bits
1295 defined in regex.h. We return the old syntax. */
1298 re_set_syntax (syntax
)
1299 reg_syntax_t syntax
;
1301 reg_syntax_t ret
= re_syntax_options
;
1303 re_syntax_options
= syntax
;
1306 WEAK_ALIAS (__re_set_syntax
, re_set_syntax
)
1308 /* Regexp to use to replace spaces, or NULL meaning don't. */
1309 static re_char
*whitespace_regexp
;
1312 re_set_whitespace_regexp (regexp
)
1315 whitespace_regexp
= (re_char
*) regexp
;
1317 WEAK_ALIAS (__re_set_syntax
, re_set_syntax
)
1319 /* This table gives an error message for each of the error codes listed
1320 in regex.h. Obviously the order here has to be same as there.
1321 POSIX doesn't require that we do anything for REG_NOERROR,
1322 but why not be nice? */
1324 static const char *re_error_msgid
[] =
1326 gettext_noop ("Success"), /* REG_NOERROR */
1327 gettext_noop ("No match"), /* REG_NOMATCH */
1328 gettext_noop ("Invalid regular expression"), /* REG_BADPAT */
1329 gettext_noop ("Invalid collation character"), /* REG_ECOLLATE */
1330 gettext_noop ("Invalid character class name"), /* REG_ECTYPE */
1331 gettext_noop ("Trailing backslash"), /* REG_EESCAPE */
1332 gettext_noop ("Invalid back reference"), /* REG_ESUBREG */
1333 gettext_noop ("Unmatched [ or [^"), /* REG_EBRACK */
1334 gettext_noop ("Unmatched ( or \\("), /* REG_EPAREN */
1335 gettext_noop ("Unmatched \\{"), /* REG_EBRACE */
1336 gettext_noop ("Invalid content of \\{\\}"), /* REG_BADBR */
1337 gettext_noop ("Invalid range end"), /* REG_ERANGE */
1338 gettext_noop ("Memory exhausted"), /* REG_ESPACE */
1339 gettext_noop ("Invalid preceding regular expression"), /* REG_BADRPT */
1340 gettext_noop ("Premature end of regular expression"), /* REG_EEND */
1341 gettext_noop ("Regular expression too big"), /* REG_ESIZE */
1342 gettext_noop ("Unmatched ) or \\)"), /* REG_ERPAREN */
1343 gettext_noop ("Range striding over charsets") /* REG_ERANGEX */
1346 /* Avoiding alloca during matching, to placate r_alloc. */
1348 /* Define MATCH_MAY_ALLOCATE unless we need to make sure that the
1349 searching and matching functions should not call alloca. On some
1350 systems, alloca is implemented in terms of malloc, and if we're
1351 using the relocating allocator routines, then malloc could cause a
1352 relocation, which might (if the strings being searched are in the
1353 ralloc heap) shift the data out from underneath the regexp
1356 Here's another reason to avoid allocation: Emacs
1357 processes input from X in a signal handler; processing X input may
1358 call malloc; if input arrives while a matching routine is calling
1359 malloc, then we're scrod. But Emacs can't just block input while
1360 calling matching routines; then we don't notice interrupts when
1361 they come in. So, Emacs blocks input around all regexp calls
1362 except the matching calls, which it leaves unprotected, in the
1363 faith that they will not malloc. */
1365 /* Normally, this is fine. */
1366 #define MATCH_MAY_ALLOCATE
1368 /* When using GNU C, we are not REALLY using the C alloca, no matter
1369 what config.h may say. So don't take precautions for it. */
1374 /* The match routines may not allocate if (1) they would do it with malloc
1375 and (2) it's not safe for them to use malloc.
1376 Note that if REL_ALLOC is defined, matching would not use malloc for the
1377 failure stack, but we would still use it for the register vectors;
1378 so REL_ALLOC should not affect this. */
1379 #if (defined C_ALLOCA || defined REGEX_MALLOC) && defined emacs
1380 # undef MATCH_MAY_ALLOCATE
1384 /* Failure stack declarations and macros; both re_compile_fastmap and
1385 re_match_2 use a failure stack. These have to be macros because of
1386 REGEX_ALLOCATE_STACK. */
1389 /* Approximate number of failure points for which to initially allocate space
1390 when matching. If this number is exceeded, we allocate more
1391 space, so it is not a hard limit. */
1392 #ifndef INIT_FAILURE_ALLOC
1393 # define INIT_FAILURE_ALLOC 20
1396 /* Roughly the maximum number of failure points on the stack. Would be
1397 exactly that if always used TYPICAL_FAILURE_SIZE items each time we failed.
1398 This is a variable only so users of regex can assign to it; we never
1399 change it ourselves. We always multiply it by TYPICAL_FAILURE_SIZE
1400 before using it, so it should probably be a byte-count instead. */
1401 # if defined MATCH_MAY_ALLOCATE
1402 /* Note that 4400 was enough to cause a crash on Alpha OSF/1,
1403 whose default stack limit is 2mb. In order for a larger
1404 value to work reliably, you have to try to make it accord
1405 with the process stack limit. */
1406 size_t re_max_failures
= 40000;
1408 size_t re_max_failures
= 4000;
1411 union fail_stack_elt
1414 /* This should be the biggest `int' that's no bigger than a pointer. */
1418 typedef union fail_stack_elt fail_stack_elt_t
;
1422 fail_stack_elt_t
*stack
;
1424 size_t avail
; /* Offset of next open position. */
1425 size_t frame
; /* Offset of the cur constructed frame. */
1428 #define FAIL_STACK_EMPTY() (fail_stack.frame == 0)
1429 #define FAIL_STACK_FULL() (fail_stack.avail == fail_stack.size)
1432 /* Define macros to initialize and free the failure stack.
1433 Do `return -2' if the alloc fails. */
1435 #ifdef MATCH_MAY_ALLOCATE
1436 # define INIT_FAIL_STACK() \
1438 fail_stack.stack = (fail_stack_elt_t *) \
1439 REGEX_ALLOCATE_STACK (INIT_FAILURE_ALLOC * TYPICAL_FAILURE_SIZE \
1440 * sizeof (fail_stack_elt_t)); \
1442 if (fail_stack.stack == NULL) \
1445 fail_stack.size = INIT_FAILURE_ALLOC; \
1446 fail_stack.avail = 0; \
1447 fail_stack.frame = 0; \
1450 # define RESET_FAIL_STACK() REGEX_FREE_STACK (fail_stack.stack)
1452 # define INIT_FAIL_STACK() \
1454 fail_stack.avail = 0; \
1455 fail_stack.frame = 0; \
1458 # define RESET_FAIL_STACK() ((void)0)
1462 /* Double the size of FAIL_STACK, up to a limit
1463 which allows approximately `re_max_failures' items.
1465 Return 1 if succeeds, and 0 if either ran out of memory
1466 allocating space for it or it was already too large.
1468 REGEX_REALLOCATE_STACK requires `destination' be declared. */
1470 /* Factor to increase the failure stack size by
1471 when we increase it.
1472 This used to be 2, but 2 was too wasteful
1473 because the old discarded stacks added up to as much space
1474 were as ultimate, maximum-size stack. */
1475 #define FAIL_STACK_GROWTH_FACTOR 4
1477 #define GROW_FAIL_STACK(fail_stack) \
1478 (((fail_stack).size * sizeof (fail_stack_elt_t) \
1479 >= re_max_failures * TYPICAL_FAILURE_SIZE) \
1481 : ((fail_stack).stack \
1482 = (fail_stack_elt_t *) \
1483 REGEX_REALLOCATE_STACK ((fail_stack).stack, \
1484 (fail_stack).size * sizeof (fail_stack_elt_t), \
1485 MIN (re_max_failures * TYPICAL_FAILURE_SIZE, \
1486 ((fail_stack).size * sizeof (fail_stack_elt_t) \
1487 * FAIL_STACK_GROWTH_FACTOR))), \
1489 (fail_stack).stack == NULL \
1491 : ((fail_stack).size \
1492 = (MIN (re_max_failures * TYPICAL_FAILURE_SIZE, \
1493 ((fail_stack).size * sizeof (fail_stack_elt_t) \
1494 * FAIL_STACK_GROWTH_FACTOR)) \
1495 / sizeof (fail_stack_elt_t)), \
1499 /* Push a pointer value onto the failure stack.
1500 Assumes the variable `fail_stack'. Probably should only
1501 be called from within `PUSH_FAILURE_POINT'. */
1502 #define PUSH_FAILURE_POINTER(item) \
1503 fail_stack.stack[fail_stack.avail++].pointer = (item)
1505 /* This pushes an integer-valued item onto the failure stack.
1506 Assumes the variable `fail_stack'. Probably should only
1507 be called from within `PUSH_FAILURE_POINT'. */
1508 #define PUSH_FAILURE_INT(item) \
1509 fail_stack.stack[fail_stack.avail++].integer = (item)
1511 /* Push a fail_stack_elt_t value onto the failure stack.
1512 Assumes the variable `fail_stack'. Probably should only
1513 be called from within `PUSH_FAILURE_POINT'. */
1514 #define PUSH_FAILURE_ELT(item) \
1515 fail_stack.stack[fail_stack.avail++] = (item)
1517 /* These three POP... operations complement the three PUSH... operations.
1518 All assume that `fail_stack' is nonempty. */
1519 #define POP_FAILURE_POINTER() fail_stack.stack[--fail_stack.avail].pointer
1520 #define POP_FAILURE_INT() fail_stack.stack[--fail_stack.avail].integer
1521 #define POP_FAILURE_ELT() fail_stack.stack[--fail_stack.avail]
1523 /* Individual items aside from the registers. */
1524 #define NUM_NONREG_ITEMS 3
1526 /* Used to examine the stack (to detect infinite loops). */
1527 #define FAILURE_PAT(h) fail_stack.stack[(h) - 1].pointer
1528 #define FAILURE_STR(h) (fail_stack.stack[(h) - 2].pointer)
1529 #define NEXT_FAILURE_HANDLE(h) fail_stack.stack[(h) - 3].integer
1530 #define TOP_FAILURE_HANDLE() fail_stack.frame
1533 #define ENSURE_FAIL_STACK(space) \
1534 while (REMAINING_AVAIL_SLOTS <= space) { \
1535 if (!GROW_FAIL_STACK (fail_stack)) \
1537 DEBUG_PRINT2 ("\n Doubled stack; size now: %d\n", (fail_stack).size);\
1538 DEBUG_PRINT2 (" slots available: %d\n", REMAINING_AVAIL_SLOTS);\
1541 /* Push register NUM onto the stack. */
1542 #define PUSH_FAILURE_REG(num) \
1544 char *destination; \
1545 ENSURE_FAIL_STACK(3); \
1546 DEBUG_PRINT4 (" Push reg %d (spanning %p -> %p)\n", \
1547 num, regstart[num], regend[num]); \
1548 PUSH_FAILURE_POINTER (regstart[num]); \
1549 PUSH_FAILURE_POINTER (regend[num]); \
1550 PUSH_FAILURE_INT (num); \
1553 /* Change the counter's value to VAL, but make sure that it will
1554 be reset when backtracking. */
1555 #define PUSH_NUMBER(ptr,val) \
1557 char *destination; \
1559 ENSURE_FAIL_STACK(3); \
1560 EXTRACT_NUMBER (c, ptr); \
1561 DEBUG_PRINT4 (" Push number %p = %d -> %d\n", ptr, c, val); \
1562 PUSH_FAILURE_INT (c); \
1563 PUSH_FAILURE_POINTER (ptr); \
1564 PUSH_FAILURE_INT (-1); \
1565 STORE_NUMBER (ptr, val); \
1568 /* Pop a saved register off the stack. */
1569 #define POP_FAILURE_REG_OR_COUNT() \
1571 int reg = POP_FAILURE_INT (); \
1574 /* It's a counter. */ \
1575 /* Here, we discard `const', making re_match non-reentrant. */ \
1576 unsigned char *ptr = (unsigned char*) POP_FAILURE_POINTER (); \
1577 reg = POP_FAILURE_INT (); \
1578 STORE_NUMBER (ptr, reg); \
1579 DEBUG_PRINT3 (" Pop counter %p = %d\n", ptr, reg); \
1583 regend[reg] = POP_FAILURE_POINTER (); \
1584 regstart[reg] = POP_FAILURE_POINTER (); \
1585 DEBUG_PRINT4 (" Pop reg %d (spanning %p -> %p)\n", \
1586 reg, regstart[reg], regend[reg]); \
1590 /* Check that we are not stuck in an infinite loop. */
1591 #define CHECK_INFINITE_LOOP(pat_cur, string_place) \
1593 int failure = TOP_FAILURE_HANDLE (); \
1594 /* Check for infinite matching loops */ \
1595 while (failure > 0 \
1596 && (FAILURE_STR (failure) == string_place \
1597 || FAILURE_STR (failure) == NULL)) \
1599 assert (FAILURE_PAT (failure) >= bufp->buffer \
1600 && FAILURE_PAT (failure) <= bufp->buffer + bufp->used); \
1601 if (FAILURE_PAT (failure) == pat_cur) \
1606 DEBUG_PRINT2 (" Other pattern: %p\n", FAILURE_PAT (failure)); \
1607 failure = NEXT_FAILURE_HANDLE(failure); \
1609 DEBUG_PRINT2 (" Other string: %p\n", FAILURE_STR (failure)); \
1612 /* Push the information about the state we will need
1613 if we ever fail back to it.
1615 Requires variables fail_stack, regstart, regend and
1616 num_regs be declared. GROW_FAIL_STACK requires `destination' be
1619 Does `return FAILURE_CODE' if runs out of memory. */
1621 #define PUSH_FAILURE_POINT(pattern, string_place) \
1623 char *destination; \
1624 /* Must be int, so when we don't save any registers, the arithmetic \
1625 of 0 + -1 isn't done as unsigned. */ \
1627 DEBUG_STATEMENT (nfailure_points_pushed++); \
1628 DEBUG_PRINT1 ("\nPUSH_FAILURE_POINT:\n"); \
1629 DEBUG_PRINT2 (" Before push, next avail: %d\n", (fail_stack).avail); \
1630 DEBUG_PRINT2 (" size: %d\n", (fail_stack).size);\
1632 ENSURE_FAIL_STACK (NUM_NONREG_ITEMS); \
1634 DEBUG_PRINT1 ("\n"); \
1636 DEBUG_PRINT2 (" Push frame index: %d\n", fail_stack.frame); \
1637 PUSH_FAILURE_INT (fail_stack.frame); \
1639 DEBUG_PRINT2 (" Push string %p: `", string_place); \
1640 DEBUG_PRINT_DOUBLE_STRING (string_place, string1, size1, string2, size2);\
1641 DEBUG_PRINT1 ("'\n"); \
1642 PUSH_FAILURE_POINTER (string_place); \
1644 DEBUG_PRINT2 (" Push pattern %p: ", pattern); \
1645 DEBUG_PRINT_COMPILED_PATTERN (bufp, pattern, pend); \
1646 PUSH_FAILURE_POINTER (pattern); \
1648 /* Close the frame by moving the frame pointer past it. */ \
1649 fail_stack.frame = fail_stack.avail; \
1652 /* Estimate the size of data pushed by a typical failure stack entry.
1653 An estimate is all we need, because all we use this for
1654 is to choose a limit for how big to make the failure stack. */
1655 /* BEWARE, the value `20' is hard-coded in emacs.c:main(). */
1656 #define TYPICAL_FAILURE_SIZE 20
1658 /* How many items can still be added to the stack without overflowing it. */
1659 #define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail)
1662 /* Pops what PUSH_FAIL_STACK pushes.
1664 We restore into the parameters, all of which should be lvalues:
1665 STR -- the saved data position.
1666 PAT -- the saved pattern position.
1667 REGSTART, REGEND -- arrays of string positions.
1669 Also assumes the variables `fail_stack' and (if debugging), `bufp',
1670 `pend', `string1', `size1', `string2', and `size2'. */
1672 #define POP_FAILURE_POINT(str, pat) \
1674 assert (!FAIL_STACK_EMPTY ()); \
1676 /* Remove failure points and point to how many regs pushed. */ \
1677 DEBUG_PRINT1 ("POP_FAILURE_POINT:\n"); \
1678 DEBUG_PRINT2 (" Before pop, next avail: %d\n", fail_stack.avail); \
1679 DEBUG_PRINT2 (" size: %d\n", fail_stack.size); \
1681 /* Pop the saved registers. */ \
1682 while (fail_stack.frame < fail_stack.avail) \
1683 POP_FAILURE_REG_OR_COUNT (); \
1685 pat = POP_FAILURE_POINTER (); \
1686 DEBUG_PRINT2 (" Popping pattern %p: ", pat); \
1687 DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend); \
1689 /* If the saved string location is NULL, it came from an \
1690 on_failure_keep_string_jump opcode, and we want to throw away the \
1691 saved NULL, thus retaining our current position in the string. */ \
1692 str = POP_FAILURE_POINTER (); \
1693 DEBUG_PRINT2 (" Popping string %p: `", str); \
1694 DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2); \
1695 DEBUG_PRINT1 ("'\n"); \
1697 fail_stack.frame = POP_FAILURE_INT (); \
1698 DEBUG_PRINT2 (" Popping frame index: %d\n", fail_stack.frame); \
1700 assert (fail_stack.avail >= 0); \
1701 assert (fail_stack.frame <= fail_stack.avail); \
1703 DEBUG_STATEMENT (nfailure_points_popped++); \
1704 } while (0) /* POP_FAILURE_POINT */
1708 /* Registers are set to a sentinel when they haven't yet matched. */
1709 #define REG_UNSET(e) ((e) == NULL)
1711 /* Subroutine declarations and macros for regex_compile. */
1713 static reg_errcode_t regex_compile
_RE_ARGS ((re_char
*pattern
, size_t size
,
1714 reg_syntax_t syntax
,
1715 struct re_pattern_buffer
*bufp
));
1716 static void store_op1
_RE_ARGS ((re_opcode_t op
, unsigned char *loc
, int arg
));
1717 static void store_op2
_RE_ARGS ((re_opcode_t op
, unsigned char *loc
,
1718 int arg1
, int arg2
));
1719 static void insert_op1
_RE_ARGS ((re_opcode_t op
, unsigned char *loc
,
1720 int arg
, unsigned char *end
));
1721 static void insert_op2
_RE_ARGS ((re_opcode_t op
, unsigned char *loc
,
1722 int arg1
, int arg2
, unsigned char *end
));
1723 static boolean at_begline_loc_p
_RE_ARGS ((re_char
*pattern
,
1725 reg_syntax_t syntax
));
1726 static boolean at_endline_loc_p
_RE_ARGS ((re_char
*p
,
1728 reg_syntax_t syntax
));
1729 static re_char
*skip_one_char
_RE_ARGS ((re_char
*p
));
1730 static int analyse_first
_RE_ARGS ((re_char
*p
, re_char
*pend
,
1731 char *fastmap
, const int multibyte
));
1733 /* Fetch the next character in the uncompiled pattern, with no
1735 #define PATFETCH(c) \
1738 if (p == pend) return REG_EEND; \
1739 c = RE_STRING_CHAR_AND_LENGTH (p, pend - p, len); \
1744 /* If `translate' is non-null, return translate[D], else just D. We
1745 cast the subscript to translate because some data is declared as
1746 `char *', to avoid warnings when a string constant is passed. But
1747 when we use a character as a subscript we must make it unsigned. */
1749 # define TRANSLATE(d) \
1750 (RE_TRANSLATE_P (translate) ? RE_TRANSLATE (translate, (d)) : (d))
1754 /* Macros for outputting the compiled pattern into `buffer'. */
1756 /* If the buffer isn't allocated when it comes in, use this. */
1757 #define INIT_BUF_SIZE 32
1759 /* Make sure we have at least N more bytes of space in buffer. */
1760 #define GET_BUFFER_SPACE(n) \
1761 while ((size_t) (b - bufp->buffer + (n)) > bufp->allocated) \
1764 /* Make sure we have one more byte of buffer space and then add C to it. */
1765 #define BUF_PUSH(c) \
1767 GET_BUFFER_SPACE (1); \
1768 *b++ = (unsigned char) (c); \
1772 /* Ensure we have two more bytes of buffer space and then append C1 and C2. */
1773 #define BUF_PUSH_2(c1, c2) \
1775 GET_BUFFER_SPACE (2); \
1776 *b++ = (unsigned char) (c1); \
1777 *b++ = (unsigned char) (c2); \
1781 /* As with BUF_PUSH_2, except for three bytes. */
1782 #define BUF_PUSH_3(c1, c2, c3) \
1784 GET_BUFFER_SPACE (3); \
1785 *b++ = (unsigned char) (c1); \
1786 *b++ = (unsigned char) (c2); \
1787 *b++ = (unsigned char) (c3); \
1791 /* Store a jump with opcode OP at LOC to location TO. We store a
1792 relative address offset by the three bytes the jump itself occupies. */
1793 #define STORE_JUMP(op, loc, to) \
1794 store_op1 (op, loc, (to) - (loc) - 3)
1796 /* Likewise, for a two-argument jump. */
1797 #define STORE_JUMP2(op, loc, to, arg) \
1798 store_op2 (op, loc, (to) - (loc) - 3, arg)
1800 /* Like `STORE_JUMP', but for inserting. Assume `b' is the buffer end. */
1801 #define INSERT_JUMP(op, loc, to) \
1802 insert_op1 (op, loc, (to) - (loc) - 3, b)
1804 /* Like `STORE_JUMP2', but for inserting. Assume `b' is the buffer end. */
1805 #define INSERT_JUMP2(op, loc, to, arg) \
1806 insert_op2 (op, loc, (to) - (loc) - 3, arg, b)
1809 /* This is not an arbitrary limit: the arguments which represent offsets
1810 into the pattern are two bytes long. So if 2^15 bytes turns out to
1811 be too small, many things would have to change. */
1812 # define MAX_BUF_SIZE (1L << 15)
1814 #if 0 /* This is when we thought it could be 2^16 bytes. */
1815 /* Any other compiler which, like MSC, has allocation limit below 2^16
1816 bytes will have to use approach similar to what was done below for
1817 MSC and drop MAX_BUF_SIZE a bit. Otherwise you may end up
1818 reallocating to 0 bytes. Such thing is not going to work too well.
1819 You have been warned!! */
1820 #if defined _MSC_VER && !defined WIN32
1821 /* Microsoft C 16-bit versions limit malloc to approx 65512 bytes. */
1822 # define MAX_BUF_SIZE 65500L
1824 # define MAX_BUF_SIZE (1L << 16)
1828 /* Extend the buffer by twice its current size via realloc and
1829 reset the pointers that pointed into the old block to point to the
1830 correct places in the new one. If extending the buffer results in it
1831 being larger than MAX_BUF_SIZE, then flag memory exhausted. */
1832 #if __BOUNDED_POINTERS__
1833 # define SET_HIGH_BOUND(P) (__ptrhigh (P) = __ptrlow (P) + bufp->allocated)
1834 # define MOVE_BUFFER_POINTER(P) \
1835 (__ptrlow (P) += incr, SET_HIGH_BOUND (P), __ptrvalue (P) += incr)
1836 # define ELSE_EXTEND_BUFFER_HIGH_BOUND \
1839 SET_HIGH_BOUND (b); \
1840 SET_HIGH_BOUND (begalt); \
1841 if (fixup_alt_jump) \
1842 SET_HIGH_BOUND (fixup_alt_jump); \
1844 SET_HIGH_BOUND (laststart); \
1845 if (pending_exact) \
1846 SET_HIGH_BOUND (pending_exact); \
1849 # define MOVE_BUFFER_POINTER(P) (P) += incr
1850 # define ELSE_EXTEND_BUFFER_HIGH_BOUND
1852 #define EXTEND_BUFFER() \
1854 re_char *old_buffer = bufp->buffer; \
1855 if (bufp->allocated == MAX_BUF_SIZE) \
1857 bufp->allocated <<= 1; \
1858 if (bufp->allocated > MAX_BUF_SIZE) \
1859 bufp->allocated = MAX_BUF_SIZE; \
1860 RETALLOC (bufp->buffer, bufp->allocated, unsigned char); \
1861 if (bufp->buffer == NULL) \
1862 return REG_ESPACE; \
1863 /* If the buffer moved, move all the pointers into it. */ \
1864 if (old_buffer != bufp->buffer) \
1866 int incr = bufp->buffer - old_buffer; \
1867 MOVE_BUFFER_POINTER (b); \
1868 MOVE_BUFFER_POINTER (begalt); \
1869 if (fixup_alt_jump) \
1870 MOVE_BUFFER_POINTER (fixup_alt_jump); \
1872 MOVE_BUFFER_POINTER (laststart); \
1873 if (pending_exact) \
1874 MOVE_BUFFER_POINTER (pending_exact); \
1876 ELSE_EXTEND_BUFFER_HIGH_BOUND \
1880 /* Since we have one byte reserved for the register number argument to
1881 {start,stop}_memory, the maximum number of groups we can report
1882 things about is what fits in that byte. */
1883 #define MAX_REGNUM 255
1885 /* But patterns can have more than `MAX_REGNUM' registers. We just
1886 ignore the excess. */
1887 typedef int regnum_t
;
1890 /* Macros for the compile stack. */
1892 /* Since offsets can go either forwards or backwards, this type needs to
1893 be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1. */
1894 /* int may be not enough when sizeof(int) == 2. */
1895 typedef long pattern_offset_t
;
1899 pattern_offset_t begalt_offset
;
1900 pattern_offset_t fixup_alt_jump
;
1901 pattern_offset_t laststart_offset
;
1903 } compile_stack_elt_t
;
1908 compile_stack_elt_t
*stack
;
1910 unsigned avail
; /* Offset of next open position. */
1911 } compile_stack_type
;
1914 #define INIT_COMPILE_STACK_SIZE 32
1916 #define COMPILE_STACK_EMPTY (compile_stack.avail == 0)
1917 #define COMPILE_STACK_FULL (compile_stack.avail == compile_stack.size)
1919 /* The next available element. */
1920 #define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
1922 /* Explicit quit checking is only used on NTemacs and whenever we
1923 use polling to process input events. */
1924 #if defined emacs && (defined WINDOWSNT || defined SYNC_INPUT) && defined QUIT
1925 extern int immediate_quit
;
1926 # define IMMEDIATE_QUIT_CHECK \
1928 if (immediate_quit) QUIT; \
1931 # define IMMEDIATE_QUIT_CHECK ((void)0)
1934 /* Structure to manage work area for range table. */
1935 struct range_table_work_area
1937 int *table
; /* actual work area. */
1938 int allocated
; /* allocated size for work area in bytes. */
1939 int used
; /* actually used size in words. */
1940 int bits
; /* flag to record character classes */
1943 /* Make sure that WORK_AREA can hold more N multibyte characters.
1944 This is used only in set_image_of_range and set_image_of_range_1.
1945 It expects WORK_AREA to be a pointer.
1946 If it can't get the space, it returns from the surrounding function. */
1948 #define EXTEND_RANGE_TABLE(work_area, n) \
1950 if (((work_area)->used + (n)) * sizeof (int) > (work_area)->allocated) \
1952 extend_range_table_work_area (work_area); \
1953 if ((work_area)->table == 0) \
1954 return (REG_ESPACE); \
1958 #define SET_RANGE_TABLE_WORK_AREA_BIT(work_area, bit) \
1959 (work_area).bits |= (bit)
1961 /* Bits used to implement the multibyte-part of the various character classes
1962 such as [:alnum:] in a charset's range table. */
1963 #define BIT_WORD 0x1
1964 #define BIT_LOWER 0x2
1965 #define BIT_PUNCT 0x4
1966 #define BIT_SPACE 0x8
1967 #define BIT_UPPER 0x10
1968 #define BIT_MULTIBYTE 0x20
1970 /* Set a range START..END to WORK_AREA.
1971 The range is passed through TRANSLATE, so START and END
1972 should be untranslated. */
1973 #define SET_RANGE_TABLE_WORK_AREA(work_area, start, end) \
1976 tem = set_image_of_range (&work_area, start, end, translate); \
1978 FREE_STACK_RETURN (tem); \
1981 /* Free allocated memory for WORK_AREA. */
1982 #define FREE_RANGE_TABLE_WORK_AREA(work_area) \
1984 if ((work_area).table) \
1985 free ((work_area).table); \
1988 #define CLEAR_RANGE_TABLE_WORK_USED(work_area) ((work_area).used = 0, (work_area).bits = 0)
1989 #define RANGE_TABLE_WORK_USED(work_area) ((work_area).used)
1990 #define RANGE_TABLE_WORK_BITS(work_area) ((work_area).bits)
1991 #define RANGE_TABLE_WORK_ELT(work_area, i) ((work_area).table[i])
1994 /* Set the bit for character C in a list. */
1995 #define SET_LIST_BIT(c) (b[((c)) / BYTEWIDTH] |= 1 << ((c) % BYTEWIDTH))
1998 /* Get the next unsigned number in the uncompiled pattern. */
1999 #define GET_UNSIGNED_NUMBER(num) \
2002 FREE_STACK_RETURN (REG_EBRACE); \
2006 while ('0' <= c && c <= '9') \
2012 num = num * 10 + c - '0'; \
2013 if (num / 10 != prev) \
2014 FREE_STACK_RETURN (REG_BADBR); \
2016 FREE_STACK_RETURN (REG_EBRACE); \
2022 #if ! WIDE_CHAR_SUPPORT
2024 /* Map a string to the char class it names (if any). */
2029 const char *string
= str
;
2030 if (STREQ (string
, "alnum")) return RECC_ALNUM
;
2031 else if (STREQ (string
, "alpha")) return RECC_ALPHA
;
2032 else if (STREQ (string
, "word")) return RECC_WORD
;
2033 else if (STREQ (string
, "ascii")) return RECC_ASCII
;
2034 else if (STREQ (string
, "nonascii")) return RECC_NONASCII
;
2035 else if (STREQ (string
, "graph")) return RECC_GRAPH
;
2036 else if (STREQ (string
, "lower")) return RECC_LOWER
;
2037 else if (STREQ (string
, "print")) return RECC_PRINT
;
2038 else if (STREQ (string
, "punct")) return RECC_PUNCT
;
2039 else if (STREQ (string
, "space")) return RECC_SPACE
;
2040 else if (STREQ (string
, "upper")) return RECC_UPPER
;
2041 else if (STREQ (string
, "unibyte")) return RECC_UNIBYTE
;
2042 else if (STREQ (string
, "multibyte")) return RECC_MULTIBYTE
;
2043 else if (STREQ (string
, "digit")) return RECC_DIGIT
;
2044 else if (STREQ (string
, "xdigit")) return RECC_XDIGIT
;
2045 else if (STREQ (string
, "cntrl")) return RECC_CNTRL
;
2046 else if (STREQ (string
, "blank")) return RECC_BLANK
;
2050 /* True iff CH is in the char class CC. */
2052 re_iswctype (ch
, cc
)
2058 case RECC_ALNUM
: return ISALNUM (ch
);
2059 case RECC_ALPHA
: return ISALPHA (ch
);
2060 case RECC_BLANK
: return ISBLANK (ch
);
2061 case RECC_CNTRL
: return ISCNTRL (ch
);
2062 case RECC_DIGIT
: return ISDIGIT (ch
);
2063 case RECC_GRAPH
: return ISGRAPH (ch
);
2064 case RECC_LOWER
: return ISLOWER (ch
);
2065 case RECC_PRINT
: return ISPRINT (ch
);
2066 case RECC_PUNCT
: return ISPUNCT (ch
);
2067 case RECC_SPACE
: return ISSPACE (ch
);
2068 case RECC_UPPER
: return ISUPPER (ch
);
2069 case RECC_XDIGIT
: return ISXDIGIT (ch
);
2070 case RECC_ASCII
: return IS_REAL_ASCII (ch
);
2071 case RECC_NONASCII
: return !IS_REAL_ASCII (ch
);
2072 case RECC_UNIBYTE
: return ISUNIBYTE (ch
);
2073 case RECC_MULTIBYTE
: return !ISUNIBYTE (ch
);
2074 case RECC_WORD
: return ISWORD (ch
);
2075 case RECC_ERROR
: return false;
2081 /* Return a bit-pattern to use in the range-table bits to match multibyte
2082 chars of class CC. */
2084 re_wctype_to_bit (cc
)
2089 case RECC_NONASCII
: case RECC_PRINT
: case RECC_GRAPH
:
2090 case RECC_MULTIBYTE
: return BIT_MULTIBYTE
;
2091 case RECC_ALPHA
: case RECC_ALNUM
: case RECC_WORD
: return BIT_WORD
;
2092 case RECC_LOWER
: return BIT_LOWER
;
2093 case RECC_UPPER
: return BIT_UPPER
;
2094 case RECC_PUNCT
: return BIT_PUNCT
;
2095 case RECC_SPACE
: return BIT_SPACE
;
2096 case RECC_ASCII
: case RECC_DIGIT
: case RECC_XDIGIT
: case RECC_CNTRL
:
2097 case RECC_BLANK
: case RECC_UNIBYTE
: case RECC_ERROR
: return 0;
2104 /* Filling in the work area of a range. */
2106 /* Actually extend the space in WORK_AREA. */
2109 extend_range_table_work_area (work_area
)
2110 struct range_table_work_area
*work_area
;
2112 work_area
->allocated
+= 16 * sizeof (int);
2113 if (work_area
->table
)
2115 = (int *) realloc (work_area
->table
, work_area
->allocated
);
2118 = (int *) malloc (work_area
->allocated
);
2123 /* Carefully find the ranges of codes that are equivalent
2124 under case conversion to the range start..end when passed through
2125 TRANSLATE. Handle the case where non-letters can come in between
2126 two upper-case letters (which happens in Latin-1).
2127 Also handle the case of groups of more than 2 case-equivalent chars.
2129 The basic method is to look at consecutive characters and see
2130 if they can form a run that can be handled as one.
2132 Returns -1 if successful, REG_ESPACE if ran out of space. */
2135 set_image_of_range_1 (work_area
, start
, end
, translate
)
2136 RE_TRANSLATE_TYPE translate
;
2137 struct range_table_work_area
*work_area
;
2138 re_wchar_t start
, end
;
2140 /* `one_case' indicates a character, or a run of characters,
2141 each of which is an isolate (no case-equivalents).
2142 This includes all ASCII non-letters.
2144 `two_case' indicates a character, or a run of characters,
2145 each of which has two case-equivalent forms.
2146 This includes all ASCII letters.
2148 `strange' indicates a character that has more than one
2151 enum case_type
{one_case
, two_case
, strange
};
2153 /* Describe the run that is in progress,
2154 which the next character can try to extend.
2155 If run_type is strange, that means there really is no run.
2156 If run_type is one_case, then run_start...run_end is the run.
2157 If run_type is two_case, then the run is run_start...run_end,
2158 and the case-equivalents end at run_eqv_end. */
2160 enum case_type run_type
= strange
;
2161 int run_start
, run_end
, run_eqv_end
;
2163 Lisp_Object eqv_table
;
2165 if (!RE_TRANSLATE_P (translate
))
2167 EXTEND_RANGE_TABLE (work_area
, 2);
2168 work_area
->table
[work_area
->used
++] = (start
);
2169 work_area
->table
[work_area
->used
++] = (end
);
2173 eqv_table
= XCHAR_TABLE (translate
)->extras
[2];
2175 for (; start
<= end
; start
++)
2177 enum case_type this_type
;
2178 int eqv
= RE_TRANSLATE (eqv_table
, start
);
2179 int minchar
, maxchar
;
2181 /* Classify this character */
2183 this_type
= one_case
;
2184 else if (RE_TRANSLATE (eqv_table
, eqv
) == start
)
2185 this_type
= two_case
;
2187 this_type
= strange
;
2190 minchar
= start
, maxchar
= eqv
;
2192 minchar
= eqv
, maxchar
= start
;
2194 /* Can this character extend the run in progress? */
2195 if (this_type
== strange
|| this_type
!= run_type
2196 || !(minchar
== run_end
+ 1
2197 && (run_type
== two_case
2198 ? maxchar
== run_eqv_end
+ 1 : 1)))
2201 Record each of its equivalent ranges. */
2202 if (run_type
== one_case
)
2204 EXTEND_RANGE_TABLE (work_area
, 2);
2205 work_area
->table
[work_area
->used
++] = run_start
;
2206 work_area
->table
[work_area
->used
++] = run_end
;
2208 else if (run_type
== two_case
)
2210 EXTEND_RANGE_TABLE (work_area
, 4);
2211 work_area
->table
[work_area
->used
++] = run_start
;
2212 work_area
->table
[work_area
->used
++] = run_end
;
2213 work_area
->table
[work_area
->used
++]
2214 = RE_TRANSLATE (eqv_table
, run_start
);
2215 work_area
->table
[work_area
->used
++]
2216 = RE_TRANSLATE (eqv_table
, run_end
);
2221 if (this_type
== strange
)
2223 /* For a strange character, add each of its equivalents, one
2224 by one. Don't start a range. */
2227 EXTEND_RANGE_TABLE (work_area
, 2);
2228 work_area
->table
[work_area
->used
++] = eqv
;
2229 work_area
->table
[work_area
->used
++] = eqv
;
2230 eqv
= RE_TRANSLATE (eqv_table
, eqv
);
2232 while (eqv
!= start
);
2235 /* Add this char to the run, or start a new run. */
2236 else if (run_type
== strange
)
2238 /* Initialize a new range. */
2239 run_type
= this_type
;
2242 run_eqv_end
= RE_TRANSLATE (eqv_table
, run_end
);
2246 /* Extend a running range. */
2248 run_eqv_end
= RE_TRANSLATE (eqv_table
, run_end
);
2252 /* If a run is still in progress at the end, finish it now
2253 by recording its equivalent ranges. */
2254 if (run_type
== one_case
)
2256 EXTEND_RANGE_TABLE (work_area
, 2);
2257 work_area
->table
[work_area
->used
++] = run_start
;
2258 work_area
->table
[work_area
->used
++] = run_end
;
2260 else if (run_type
== two_case
)
2262 EXTEND_RANGE_TABLE (work_area
, 4);
2263 work_area
->table
[work_area
->used
++] = run_start
;
2264 work_area
->table
[work_area
->used
++] = run_end
;
2265 work_area
->table
[work_area
->used
++]
2266 = RE_TRANSLATE (eqv_table
, run_start
);
2267 work_area
->table
[work_area
->used
++]
2268 = RE_TRANSLATE (eqv_table
, run_end
);
2276 /* Record the the image of the range start..end when passed through
2277 TRANSLATE. This is not necessarily TRANSLATE(start)..TRANSLATE(end)
2278 and is not even necessarily contiguous.
2279 Normally we approximate it with the smallest contiguous range that contains
2280 all the chars we need. However, for Latin-1 we go to extra effort
2283 This function is not called for ASCII ranges.
2285 Returns -1 if successful, REG_ESPACE if ran out of space. */
2288 set_image_of_range (work_area
, start
, end
, translate
)
2289 RE_TRANSLATE_TYPE translate
;
2290 struct range_table_work_area
*work_area
;
2291 re_wchar_t start
, end
;
2293 re_wchar_t cmin
, cmax
;
2296 /* For Latin-1 ranges, use set_image_of_range_1
2297 to get proper handling of ranges that include letters and nonletters.
2298 For a range that includes the whole of Latin-1, this is not necessary.
2299 For other character sets, we don't bother to get this right. */
2300 if (RE_TRANSLATE_P (translate
) && start
< 04400
2301 && !(start
< 04200 && end
>= 04377))
2308 tem
= set_image_of_range_1 (work_area
, start
, newend
, translate
);
2318 EXTEND_RANGE_TABLE (work_area
, 2);
2319 work_area
->table
[work_area
->used
++] = (start
);
2320 work_area
->table
[work_area
->used
++] = (end
);
2322 cmin
= -1, cmax
= -1;
2324 if (RE_TRANSLATE_P (translate
))
2328 for (ch
= start
; ch
<= end
; ch
++)
2330 re_wchar_t c
= TRANSLATE (ch
);
2331 if (! (start
<= c
&& c
<= end
))
2337 cmin
= MIN (cmin
, c
);
2338 cmax
= MAX (cmax
, c
);
2345 EXTEND_RANGE_TABLE (work_area
, 2);
2346 work_area
->table
[work_area
->used
++] = (cmin
);
2347 work_area
->table
[work_area
->used
++] = (cmax
);
2354 #ifndef MATCH_MAY_ALLOCATE
2356 /* If we cannot allocate large objects within re_match_2_internal,
2357 we make the fail stack and register vectors global.
2358 The fail stack, we grow to the maximum size when a regexp
2360 The register vectors, we adjust in size each time we
2361 compile a regexp, according to the number of registers it needs. */
2363 static fail_stack_type fail_stack
;
2365 /* Size with which the following vectors are currently allocated.
2366 That is so we can make them bigger as needed,
2367 but never make them smaller. */
2368 static int regs_allocated_size
;
2370 static re_char
** regstart
, ** regend
;
2371 static re_char
**best_regstart
, **best_regend
;
2373 /* Make the register vectors big enough for NUM_REGS registers,
2374 but don't make them smaller. */
2377 regex_grow_registers (num_regs
)
2380 if (num_regs
> regs_allocated_size
)
2382 RETALLOC_IF (regstart
, num_regs
, re_char
*);
2383 RETALLOC_IF (regend
, num_regs
, re_char
*);
2384 RETALLOC_IF (best_regstart
, num_regs
, re_char
*);
2385 RETALLOC_IF (best_regend
, num_regs
, re_char
*);
2387 regs_allocated_size
= num_regs
;
2391 #endif /* not MATCH_MAY_ALLOCATE */
2393 static boolean group_in_compile_stack
_RE_ARGS ((compile_stack_type
2397 /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
2398 Returns one of error codes defined in `regex.h', or zero for success.
2400 Assumes the `allocated' (and perhaps `buffer') and `translate'
2401 fields are set in BUFP on entry.
2403 If it succeeds, results are put in BUFP (if it returns an error, the
2404 contents of BUFP are undefined):
2405 `buffer' is the compiled pattern;
2406 `syntax' is set to SYNTAX;
2407 `used' is set to the length of the compiled pattern;
2408 `fastmap_accurate' is zero;
2409 `re_nsub' is the number of subexpressions in PATTERN;
2410 `not_bol' and `not_eol' are zero;
2412 The `fastmap' field is neither examined nor set. */
2414 /* Insert the `jump' from the end of last alternative to "here".
2415 The space for the jump has already been allocated. */
2416 #define FIXUP_ALT_JUMP() \
2418 if (fixup_alt_jump) \
2419 STORE_JUMP (jump, fixup_alt_jump, b); \
2423 /* Return, freeing storage we allocated. */
2424 #define FREE_STACK_RETURN(value) \
2426 FREE_RANGE_TABLE_WORK_AREA (range_table_work); \
2427 free (compile_stack.stack); \
2431 static reg_errcode_t
2432 regex_compile (pattern
, size
, syntax
, bufp
)
2435 reg_syntax_t syntax
;
2436 struct re_pattern_buffer
*bufp
;
2438 /* We fetch characters from PATTERN here. */
2439 register re_wchar_t c
, c1
;
2441 /* A random temporary spot in PATTERN. */
2444 /* Points to the end of the buffer, where we should append. */
2445 register unsigned char *b
;
2447 /* Keeps track of unclosed groups. */
2448 compile_stack_type compile_stack
;
2450 /* Points to the current (ending) position in the pattern. */
2452 /* `const' makes AIX compiler fail. */
2453 unsigned char *p
= pattern
;
2455 re_char
*p
= pattern
;
2457 re_char
*pend
= pattern
+ size
;
2459 /* How to translate the characters in the pattern. */
2460 RE_TRANSLATE_TYPE translate
= bufp
->translate
;
2462 /* Address of the count-byte of the most recently inserted `exactn'
2463 command. This makes it possible to tell if a new exact-match
2464 character can be added to that command or if the character requires
2465 a new `exactn' command. */
2466 unsigned char *pending_exact
= 0;
2468 /* Address of start of the most recently finished expression.
2469 This tells, e.g., postfix * where to find the start of its
2470 operand. Reset at the beginning of groups and alternatives. */
2471 unsigned char *laststart
= 0;
2473 /* Address of beginning of regexp, or inside of last group. */
2474 unsigned char *begalt
;
2476 /* Place in the uncompiled pattern (i.e., the {) to
2477 which to go back if the interval is invalid. */
2478 re_char
*beg_interval
;
2480 /* Address of the place where a forward jump should go to the end of
2481 the containing expression. Each alternative of an `or' -- except the
2482 last -- ends with a forward jump of this sort. */
2483 unsigned char *fixup_alt_jump
= 0;
2485 /* Counts open-groups as they are encountered. Remembered for the
2486 matching close-group on the compile stack, so the same register
2487 number is put in the stop_memory as the start_memory. */
2488 regnum_t regnum
= 0;
2490 /* Work area for range table of charset. */
2491 struct range_table_work_area range_table_work
;
2493 /* If the object matched can contain multibyte characters. */
2494 const boolean multibyte
= RE_MULTIBYTE_P (bufp
);
2496 /* Nonzero if we have pushed down into a subpattern. */
2497 int in_subpattern
= 0;
2499 /* These hold the values of p, pattern, and pend from the main
2500 pattern when we have pushed into a subpattern. */
2502 re_char
*main_pattern
;
2507 DEBUG_PRINT1 ("\nCompiling pattern: ");
2510 unsigned debug_count
;
2512 for (debug_count
= 0; debug_count
< size
; debug_count
++)
2513 putchar (pattern
[debug_count
]);
2518 /* Initialize the compile stack. */
2519 compile_stack
.stack
= TALLOC (INIT_COMPILE_STACK_SIZE
, compile_stack_elt_t
);
2520 if (compile_stack
.stack
== NULL
)
2523 compile_stack
.size
= INIT_COMPILE_STACK_SIZE
;
2524 compile_stack
.avail
= 0;
2526 range_table_work
.table
= 0;
2527 range_table_work
.allocated
= 0;
2529 /* Initialize the pattern buffer. */
2530 bufp
->syntax
= syntax
;
2531 bufp
->fastmap_accurate
= 0;
2532 bufp
->not_bol
= bufp
->not_eol
= 0;
2534 /* Set `used' to zero, so that if we return an error, the pattern
2535 printer (for debugging) will think there's no pattern. We reset it
2539 /* Always count groups, whether or not bufp->no_sub is set. */
2542 #if !defined emacs && !defined SYNTAX_TABLE
2543 /* Initialize the syntax table. */
2544 init_syntax_once ();
2547 if (bufp
->allocated
== 0)
2550 { /* If zero allocated, but buffer is non-null, try to realloc
2551 enough space. This loses if buffer's address is bogus, but
2552 that is the user's responsibility. */
2553 RETALLOC (bufp
->buffer
, INIT_BUF_SIZE
, unsigned char);
2556 { /* Caller did not allocate a buffer. Do it for them. */
2557 bufp
->buffer
= TALLOC (INIT_BUF_SIZE
, unsigned char);
2559 if (!bufp
->buffer
) FREE_STACK_RETURN (REG_ESPACE
);
2561 bufp
->allocated
= INIT_BUF_SIZE
;
2564 begalt
= b
= bufp
->buffer
;
2566 /* Loop through the uncompiled pattern until we're at the end. */
2571 /* If this is the end of an included regexp,
2572 pop back to the main regexp and try again. */
2576 pattern
= main_pattern
;
2581 /* If this is the end of the main regexp, we are done. */
2593 /* If there's no special whitespace regexp, treat
2594 spaces normally. And don't try to do this recursively. */
2595 if (!whitespace_regexp
|| in_subpattern
)
2598 /* Peek past following spaces. */
2605 /* If the spaces are followed by a repetition op,
2606 treat them normally. */
2608 && (*p1
== '*' || *p1
== '+' || *p1
== '?'
2609 || (*p1
== '\\' && p1
+ 1 != pend
&& p1
[1] == '{')))
2612 /* Replace the spaces with the whitespace regexp. */
2616 main_pattern
= pattern
;
2617 p
= pattern
= whitespace_regexp
;
2618 pend
= p
+ strlen (p
);
2624 if ( /* If at start of pattern, it's an operator. */
2626 /* If context independent, it's an operator. */
2627 || syntax
& RE_CONTEXT_INDEP_ANCHORS
2628 /* Otherwise, depends on what's come before. */
2629 || at_begline_loc_p (pattern
, p
, syntax
))
2630 BUF_PUSH ((syntax
& RE_NO_NEWLINE_ANCHOR
) ? begbuf
: begline
);
2639 if ( /* If at end of pattern, it's an operator. */
2641 /* If context independent, it's an operator. */
2642 || syntax
& RE_CONTEXT_INDEP_ANCHORS
2643 /* Otherwise, depends on what's next. */
2644 || at_endline_loc_p (p
, pend
, syntax
))
2645 BUF_PUSH ((syntax
& RE_NO_NEWLINE_ANCHOR
) ? endbuf
: endline
);
2654 if ((syntax
& RE_BK_PLUS_QM
)
2655 || (syntax
& RE_LIMITED_OPS
))
2659 /* If there is no previous pattern... */
2662 if (syntax
& RE_CONTEXT_INVALID_OPS
)
2663 FREE_STACK_RETURN (REG_BADRPT
);
2664 else if (!(syntax
& RE_CONTEXT_INDEP_OPS
))
2669 /* 1 means zero (many) matches is allowed. */
2670 boolean zero_times_ok
= 0, many_times_ok
= 0;
2673 /* If there is a sequence of repetition chars, collapse it
2674 down to just one (the right one). We can't combine
2675 interval operators with these because of, e.g., `a{2}*',
2676 which should only match an even number of `a's. */
2680 if ((syntax
& RE_FRUGAL
)
2681 && c
== '?' && (zero_times_ok
|| many_times_ok
))
2685 zero_times_ok
|= c
!= '+';
2686 many_times_ok
|= c
!= '?';
2692 || (!(syntax
& RE_BK_PLUS_QM
)
2693 && (*p
== '+' || *p
== '?')))
2695 else if (syntax
& RE_BK_PLUS_QM
&& *p
== '\\')
2698 FREE_STACK_RETURN (REG_EESCAPE
);
2699 if (p
[1] == '+' || p
[1] == '?')
2700 PATFETCH (c
); /* Gobble up the backslash. */
2706 /* If we get here, we found another repeat character. */
2710 /* Star, etc. applied to an empty pattern is equivalent
2711 to an empty pattern. */
2712 if (!laststart
|| laststart
== b
)
2715 /* Now we know whether or not zero matches is allowed
2716 and also whether or not two or more matches is allowed. */
2721 boolean simple
= skip_one_char (laststart
) == b
;
2722 unsigned int startoffset
= 0;
2724 /* Check if the loop can match the empty string. */
2725 (simple
|| !analyse_first (laststart
, b
, NULL
, 0))
2726 ? on_failure_jump
: on_failure_jump_loop
;
2727 assert (skip_one_char (laststart
) <= b
);
2729 if (!zero_times_ok
&& simple
)
2730 { /* Since simple * loops can be made faster by using
2731 on_failure_keep_string_jump, we turn simple P+
2732 into PP* if P is simple. */
2733 unsigned char *p1
, *p2
;
2734 startoffset
= b
- laststart
;
2735 GET_BUFFER_SPACE (startoffset
);
2736 p1
= b
; p2
= laststart
;
2742 GET_BUFFER_SPACE (6);
2745 STORE_JUMP (ofj
, b
, b
+ 6);
2747 /* Simple * loops can use on_failure_keep_string_jump
2748 depending on what follows. But since we don't know
2749 that yet, we leave the decision up to
2750 on_failure_jump_smart. */
2751 INSERT_JUMP (simple
? on_failure_jump_smart
: ofj
,
2752 laststart
+ startoffset
, b
+ 6);
2754 STORE_JUMP (jump
, b
, laststart
+ startoffset
);
2759 /* A simple ? pattern. */
2760 assert (zero_times_ok
);
2761 GET_BUFFER_SPACE (3);
2762 INSERT_JUMP (on_failure_jump
, laststart
, b
+ 3);
2766 else /* not greedy */
2767 { /* I wish the greedy and non-greedy cases could be merged. */
2769 GET_BUFFER_SPACE (7); /* We might use less. */
2772 boolean emptyp
= analyse_first (laststart
, b
, NULL
, 0);
2774 /* The non-greedy multiple match looks like
2775 a repeat..until: we only need a conditional jump
2776 at the end of the loop. */
2777 if (emptyp
) BUF_PUSH (no_op
);
2778 STORE_JUMP (emptyp
? on_failure_jump_nastyloop
2779 : on_failure_jump
, b
, laststart
);
2783 /* The repeat...until naturally matches one or more.
2784 To also match zero times, we need to first jump to
2785 the end of the loop (its conditional jump). */
2786 INSERT_JUMP (jump
, laststart
, b
);
2792 /* non-greedy a?? */
2793 INSERT_JUMP (jump
, laststart
, b
+ 3);
2795 INSERT_JUMP (on_failure_jump
, laststart
, laststart
+ 6);
2812 CLEAR_RANGE_TABLE_WORK_USED (range_table_work
);
2814 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2816 /* Ensure that we have enough space to push a charset: the
2817 opcode, the length count, and the bitset; 34 bytes in all. */
2818 GET_BUFFER_SPACE (34);
2822 /* We test `*p == '^' twice, instead of using an if
2823 statement, so we only need one BUF_PUSH. */
2824 BUF_PUSH (*p
== '^' ? charset_not
: charset
);
2828 /* Remember the first position in the bracket expression. */
2831 /* Push the number of bytes in the bitmap. */
2832 BUF_PUSH ((1 << BYTEWIDTH
) / BYTEWIDTH
);
2834 /* Clear the whole map. */
2835 bzero (b
, (1 << BYTEWIDTH
) / BYTEWIDTH
);
2837 /* charset_not matches newline according to a syntax bit. */
2838 if ((re_opcode_t
) b
[-2] == charset_not
2839 && (syntax
& RE_HAT_LISTS_NOT_NEWLINE
))
2840 SET_LIST_BIT ('\n');
2842 /* Read in characters and ranges, setting map bits. */
2845 boolean escaped_char
= false;
2846 const unsigned char *p2
= p
;
2848 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2850 /* Don't translate yet. The range TRANSLATE(X..Y) cannot
2851 always be determined from TRANSLATE(X) and TRANSLATE(Y)
2852 So the translation is done later in a loop. Example:
2853 (let ((case-fold-search t)) (string-match "[A-_]" "A")) */
2856 /* \ might escape characters inside [...] and [^...]. */
2857 if ((syntax
& RE_BACKSLASH_ESCAPE_IN_LISTS
) && c
== '\\')
2859 if (p
== pend
) FREE_STACK_RETURN (REG_EESCAPE
);
2862 escaped_char
= true;
2866 /* Could be the end of the bracket expression. If it's
2867 not (i.e., when the bracket expression is `[]' so
2868 far), the ']' character bit gets set way below. */
2869 if (c
== ']' && p2
!= p1
)
2873 /* What should we do for the character which is
2874 greater than 0x7F, but not BASE_LEADING_CODE_P?
2877 /* See if we're at the beginning of a possible character
2880 if (!escaped_char
&&
2881 syntax
& RE_CHAR_CLASSES
&& c
== '[' && *p
== ':')
2883 /* Leave room for the null. */
2884 unsigned char str
[CHAR_CLASS_MAX_LENGTH
+ 1];
2885 const unsigned char *class_beg
;
2891 /* If pattern is `[[:'. */
2892 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2897 if ((c
== ':' && *p
== ']') || p
== pend
)
2899 if (c1
< CHAR_CLASS_MAX_LENGTH
)
2902 /* This is in any case an invalid class name. */
2907 /* If isn't a word bracketed by `[:' and `:]':
2908 undo the ending character, the letters, and
2909 leave the leading `:' and `[' (but set bits for
2911 if (c
== ':' && *p
== ']')
2916 cc
= re_wctype (str
);
2919 FREE_STACK_RETURN (REG_ECTYPE
);
2921 /* Throw away the ] at the end of the character
2925 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2927 /* Most character classes in a multibyte match
2928 just set a flag. Exceptions are is_blank,
2929 is_digit, is_cntrl, and is_xdigit, since
2930 they can only match ASCII characters. We
2931 don't need to handle them for multibyte.
2932 They are distinguished by a negative wctype. */
2935 SET_RANGE_TABLE_WORK_AREA_BIT (range_table_work
,
2936 re_wctype_to_bit (cc
));
2938 for (ch
= 0; ch
< 1 << BYTEWIDTH
; ++ch
)
2940 int translated
= TRANSLATE (ch
);
2941 if (re_iswctype (btowc (ch
), cc
))
2942 SET_LIST_BIT (translated
);
2945 /* Repeat the loop. */
2950 /* Go back to right after the "[:". */
2954 /* Because the `:' may starts the range, we
2955 can't simply set bit and repeat the loop.
2956 Instead, just set it to C and handle below. */
2961 if (p
< pend
&& p
[0] == '-' && p
[1] != ']')
2964 /* Discard the `-'. */
2967 /* Fetch the character which ends the range. */
2970 if (SINGLE_BYTE_CHAR_P (c
))
2972 if (! SINGLE_BYTE_CHAR_P (c1
))
2974 /* Handle a range starting with a
2975 character of less than 256, and ending
2976 with a character of not less than 256.
2977 Split that into two ranges, the low one
2978 ending at 0377, and the high one
2979 starting at the smallest character in
2980 the charset of C1 and ending at C1. */
2981 int charset
= CHAR_CHARSET (c1
);
2982 re_wchar_t c2
= MAKE_CHAR (charset
, 0, 0);
2984 SET_RANGE_TABLE_WORK_AREA (range_table_work
,
2989 else if (!SAME_CHARSET_P (c
, c1
))
2990 FREE_STACK_RETURN (REG_ERANGEX
);
2993 /* Range from C to C. */
2996 /* Set the range ... */
2997 if (SINGLE_BYTE_CHAR_P (c
))
2998 /* ... into bitmap. */
3000 re_wchar_t this_char
;
3001 re_wchar_t range_start
= c
, range_end
= c1
;
3003 /* If the start is after the end, the range is empty. */
3004 if (range_start
> range_end
)
3006 if (syntax
& RE_NO_EMPTY_RANGES
)
3007 FREE_STACK_RETURN (REG_ERANGE
);
3008 /* Else, repeat the loop. */
3012 for (this_char
= range_start
; this_char
<= range_end
;
3014 SET_LIST_BIT (TRANSLATE (this_char
));
3018 /* ... into range table. */
3019 SET_RANGE_TABLE_WORK_AREA (range_table_work
, c
, c1
);
3022 /* Discard any (non)matching list bytes that are all 0 at the
3023 end of the map. Decrease the map-length byte too. */
3024 while ((int) b
[-1] > 0 && b
[b
[-1] - 1] == 0)
3028 /* Build real range table from work area. */
3029 if (RANGE_TABLE_WORK_USED (range_table_work
)
3030 || RANGE_TABLE_WORK_BITS (range_table_work
))
3033 int used
= RANGE_TABLE_WORK_USED (range_table_work
);
3035 /* Allocate space for COUNT + RANGE_TABLE. Needs two
3036 bytes for flags, two for COUNT, and three bytes for
3038 GET_BUFFER_SPACE (4 + used
* 3);
3040 /* Indicate the existence of range table. */
3041 laststart
[1] |= 0x80;
3043 /* Store the character class flag bits into the range table.
3044 If not in emacs, these flag bits are always 0. */
3045 *b
++ = RANGE_TABLE_WORK_BITS (range_table_work
) & 0xff;
3046 *b
++ = RANGE_TABLE_WORK_BITS (range_table_work
) >> 8;
3048 STORE_NUMBER_AND_INCR (b
, used
/ 2);
3049 for (i
= 0; i
< used
; i
++)
3050 STORE_CHARACTER_AND_INCR
3051 (b
, RANGE_TABLE_WORK_ELT (range_table_work
, i
));
3058 if (syntax
& RE_NO_BK_PARENS
)
3065 if (syntax
& RE_NO_BK_PARENS
)
3072 if (syntax
& RE_NEWLINE_ALT
)
3079 if (syntax
& RE_NO_BK_VBAR
)
3086 if (syntax
& RE_INTERVALS
&& syntax
& RE_NO_BK_BRACES
)
3087 goto handle_interval
;
3093 if (p
== pend
) FREE_STACK_RETURN (REG_EESCAPE
);
3095 /* Do not translate the character after the \, so that we can
3096 distinguish, e.g., \B from \b, even if we normally would
3097 translate, e.g., B to b. */
3103 if (syntax
& RE_NO_BK_PARENS
)
3104 goto normal_backslash
;
3111 /* Look for a special (?...) construct */
3112 if ((syntax
& RE_SHY_GROUPS
) && *p
== '?')
3114 PATFETCH (c
); /* Gobble up the '?'. */
3118 case ':': shy
= 1; break;
3120 /* Only (?:...) is supported right now. */
3121 FREE_STACK_RETURN (REG_BADPAT
);
3132 if (COMPILE_STACK_FULL
)
3134 RETALLOC (compile_stack
.stack
, compile_stack
.size
<< 1,
3135 compile_stack_elt_t
);
3136 if (compile_stack
.stack
== NULL
) return REG_ESPACE
;
3138 compile_stack
.size
<<= 1;
3141 /* These are the values to restore when we hit end of this
3142 group. They are all relative offsets, so that if the
3143 whole pattern moves because of realloc, they will still
3145 COMPILE_STACK_TOP
.begalt_offset
= begalt
- bufp
->buffer
;
3146 COMPILE_STACK_TOP
.fixup_alt_jump
3147 = fixup_alt_jump
? fixup_alt_jump
- bufp
->buffer
+ 1 : 0;
3148 COMPILE_STACK_TOP
.laststart_offset
= b
- bufp
->buffer
;
3149 COMPILE_STACK_TOP
.regnum
= shy
? -regnum
: regnum
;
3152 start_memory for groups beyond the last one we can
3153 represent in the compiled pattern. */
3154 if (regnum
<= MAX_REGNUM
&& !shy
)
3155 BUF_PUSH_2 (start_memory
, regnum
);
3157 compile_stack
.avail
++;
3162 /* If we've reached MAX_REGNUM groups, then this open
3163 won't actually generate any code, so we'll have to
3164 clear pending_exact explicitly. */
3170 if (syntax
& RE_NO_BK_PARENS
) goto normal_backslash
;
3172 if (COMPILE_STACK_EMPTY
)
3174 if (syntax
& RE_UNMATCHED_RIGHT_PAREN_ORD
)
3175 goto normal_backslash
;
3177 FREE_STACK_RETURN (REG_ERPAREN
);
3183 /* See similar code for backslashed left paren above. */
3184 if (COMPILE_STACK_EMPTY
)
3186 if (syntax
& RE_UNMATCHED_RIGHT_PAREN_ORD
)
3189 FREE_STACK_RETURN (REG_ERPAREN
);
3192 /* Since we just checked for an empty stack above, this
3193 ``can't happen''. */
3194 assert (compile_stack
.avail
!= 0);
3196 /* We don't just want to restore into `regnum', because
3197 later groups should continue to be numbered higher,
3198 as in `(ab)c(de)' -- the second group is #2. */
3199 regnum_t this_group_regnum
;
3201 compile_stack
.avail
--;
3202 begalt
= bufp
->buffer
+ COMPILE_STACK_TOP
.begalt_offset
;
3204 = COMPILE_STACK_TOP
.fixup_alt_jump
3205 ? bufp
->buffer
+ COMPILE_STACK_TOP
.fixup_alt_jump
- 1
3207 laststart
= bufp
->buffer
+ COMPILE_STACK_TOP
.laststart_offset
;
3208 this_group_regnum
= COMPILE_STACK_TOP
.regnum
;
3209 /* If we've reached MAX_REGNUM groups, then this open
3210 won't actually generate any code, so we'll have to
3211 clear pending_exact explicitly. */
3214 /* We're at the end of the group, so now we know how many
3215 groups were inside this one. */
3216 if (this_group_regnum
<= MAX_REGNUM
&& this_group_regnum
> 0)
3217 BUF_PUSH_2 (stop_memory
, this_group_regnum
);
3222 case '|': /* `\|'. */
3223 if (syntax
& RE_LIMITED_OPS
|| syntax
& RE_NO_BK_VBAR
)
3224 goto normal_backslash
;
3226 if (syntax
& RE_LIMITED_OPS
)
3229 /* Insert before the previous alternative a jump which
3230 jumps to this alternative if the former fails. */
3231 GET_BUFFER_SPACE (3);
3232 INSERT_JUMP (on_failure_jump
, begalt
, b
+ 6);
3236 /* The alternative before this one has a jump after it
3237 which gets executed if it gets matched. Adjust that
3238 jump so it will jump to this alternative's analogous
3239 jump (put in below, which in turn will jump to the next
3240 (if any) alternative's such jump, etc.). The last such
3241 jump jumps to the correct final destination. A picture:
3247 If we are at `b', then fixup_alt_jump right now points to a
3248 three-byte space after `a'. We'll put in the jump, set
3249 fixup_alt_jump to right after `b', and leave behind three
3250 bytes which we'll fill in when we get to after `c'. */
3254 /* Mark and leave space for a jump after this alternative,
3255 to be filled in later either by next alternative or
3256 when know we're at the end of a series of alternatives. */
3258 GET_BUFFER_SPACE (3);
3267 /* If \{ is a literal. */
3268 if (!(syntax
& RE_INTERVALS
)
3269 /* If we're at `\{' and it's not the open-interval
3271 || (syntax
& RE_NO_BK_BRACES
))
3272 goto normal_backslash
;
3276 /* If got here, then the syntax allows intervals. */
3278 /* At least (most) this many matches must be made. */
3279 int lower_bound
= 0, upper_bound
= -1;
3283 GET_UNSIGNED_NUMBER (lower_bound
);
3286 GET_UNSIGNED_NUMBER (upper_bound
);
3288 /* Interval such as `{1}' => match exactly once. */
3289 upper_bound
= lower_bound
;
3291 if (lower_bound
< 0 || upper_bound
> RE_DUP_MAX
3292 || (upper_bound
>= 0 && lower_bound
> upper_bound
))
3293 FREE_STACK_RETURN (REG_BADBR
);
3295 if (!(syntax
& RE_NO_BK_BRACES
))
3298 FREE_STACK_RETURN (REG_BADBR
);
3300 FREE_STACK_RETURN (REG_EESCAPE
);
3305 FREE_STACK_RETURN (REG_BADBR
);
3307 /* We just parsed a valid interval. */
3309 /* If it's invalid to have no preceding re. */
3312 if (syntax
& RE_CONTEXT_INVALID_OPS
)
3313 FREE_STACK_RETURN (REG_BADRPT
);
3314 else if (syntax
& RE_CONTEXT_INDEP_OPS
)
3317 goto unfetch_interval
;
3320 if (upper_bound
== 0)
3321 /* If the upper bound is zero, just drop the sub pattern
3324 else if (lower_bound
== 1 && upper_bound
== 1)
3325 /* Just match it once: nothing to do here. */
3328 /* Otherwise, we have a nontrivial interval. When
3329 we're all done, the pattern will look like:
3330 set_number_at <jump count> <upper bound>
3331 set_number_at <succeed_n count> <lower bound>
3332 succeed_n <after jump addr> <succeed_n count>
3334 jump_n <succeed_n addr> <jump count>
3335 (The upper bound and `jump_n' are omitted if
3336 `upper_bound' is 1, though.) */
3338 { /* If the upper bound is > 1, we need to insert
3339 more at the end of the loop. */
3340 unsigned int nbytes
= (upper_bound
< 0 ? 3
3341 : upper_bound
> 1 ? 5 : 0);
3342 unsigned int startoffset
= 0;
3344 GET_BUFFER_SPACE (20); /* We might use less. */
3346 if (lower_bound
== 0)
3348 /* A succeed_n that starts with 0 is really a
3349 a simple on_failure_jump_loop. */
3350 INSERT_JUMP (on_failure_jump_loop
, laststart
,
3356 /* Initialize lower bound of the `succeed_n', even
3357 though it will be set during matching by its
3358 attendant `set_number_at' (inserted next),
3359 because `re_compile_fastmap' needs to know.
3360 Jump to the `jump_n' we might insert below. */
3361 INSERT_JUMP2 (succeed_n
, laststart
,
3366 /* Code to initialize the lower bound. Insert
3367 before the `succeed_n'. The `5' is the last two
3368 bytes of this `set_number_at', plus 3 bytes of
3369 the following `succeed_n'. */
3370 insert_op2 (set_number_at
, laststart
, 5, lower_bound
, b
);
3375 if (upper_bound
< 0)
3377 /* A negative upper bound stands for infinity,
3378 in which case it degenerates to a plain jump. */
3379 STORE_JUMP (jump
, b
, laststart
+ startoffset
);
3382 else if (upper_bound
> 1)
3383 { /* More than one repetition is allowed, so
3384 append a backward jump to the `succeed_n'
3385 that starts this interval.
3387 When we've reached this during matching,
3388 we'll have matched the interval once, so
3389 jump back only `upper_bound - 1' times. */
3390 STORE_JUMP2 (jump_n
, b
, laststart
+ startoffset
,
3394 /* The location we want to set is the second
3395 parameter of the `jump_n'; that is `b-2' as
3396 an absolute address. `laststart' will be
3397 the `set_number_at' we're about to insert;
3398 `laststart+3' the number to set, the source
3399 for the relative address. But we are
3400 inserting into the middle of the pattern --
3401 so everything is getting moved up by 5.
3402 Conclusion: (b - 2) - (laststart + 3) + 5,
3403 i.e., b - laststart.
3405 We insert this at the beginning of the loop
3406 so that if we fail during matching, we'll
3407 reinitialize the bounds. */
3408 insert_op2 (set_number_at
, laststart
, b
- laststart
,
3409 upper_bound
- 1, b
);
3414 beg_interval
= NULL
;
3419 /* If an invalid interval, match the characters as literals. */
3420 assert (beg_interval
);
3422 beg_interval
= NULL
;
3424 /* normal_char and normal_backslash need `c'. */
3427 if (!(syntax
& RE_NO_BK_BRACES
))
3429 assert (p
> pattern
&& p
[-1] == '\\');
3430 goto normal_backslash
;
3436 /* There is no way to specify the before_dot and after_dot
3437 operators. rms says this is ok. --karl */
3445 BUF_PUSH_2 (syntaxspec
, syntax_spec_code
[c
]);
3451 BUF_PUSH_2 (notsyntaxspec
, syntax_spec_code
[c
]);
3457 BUF_PUSH_2 (categoryspec
, c
);
3463 BUF_PUSH_2 (notcategoryspec
, c
);
3469 if (syntax
& RE_NO_GNU_OPS
)
3472 BUF_PUSH_2 (syntaxspec
, Sword
);
3477 if (syntax
& RE_NO_GNU_OPS
)
3480 BUF_PUSH_2 (notsyntaxspec
, Sword
);
3485 if (syntax
& RE_NO_GNU_OPS
)
3491 if (syntax
& RE_NO_GNU_OPS
)
3497 if (syntax
& RE_NO_GNU_OPS
)
3506 FREE_STACK_RETURN (REG_BADPAT
);
3510 if (syntax
& RE_NO_GNU_OPS
)
3512 BUF_PUSH (wordbound
);
3516 if (syntax
& RE_NO_GNU_OPS
)
3518 BUF_PUSH (notwordbound
);
3522 if (syntax
& RE_NO_GNU_OPS
)
3528 if (syntax
& RE_NO_GNU_OPS
)
3533 case '1': case '2': case '3': case '4': case '5':
3534 case '6': case '7': case '8': case '9':
3538 if (syntax
& RE_NO_BK_REFS
)
3539 goto normal_backslash
;
3543 /* Can't back reference to a subexpression before its end. */
3544 if (reg
> regnum
|| group_in_compile_stack (compile_stack
, reg
))
3545 FREE_STACK_RETURN (REG_ESUBREG
);
3548 BUF_PUSH_2 (duplicate
, reg
);
3555 if (syntax
& RE_BK_PLUS_QM
)
3558 goto normal_backslash
;
3562 /* You might think it would be useful for \ to mean
3563 not to translate; but if we don't translate it
3564 it will never match anything. */
3571 /* Expects the character in `c'. */
3573 /* If no exactn currently being built. */
3576 /* If last exactn not at current position. */
3577 || pending_exact
+ *pending_exact
+ 1 != b
3579 /* We have only one byte following the exactn for the count. */
3580 || *pending_exact
>= (1 << BYTEWIDTH
) - MAX_MULTIBYTE_LENGTH
3582 /* If followed by a repetition operator. */
3583 || (p
!= pend
&& (*p
== '*' || *p
== '^'))
3584 || ((syntax
& RE_BK_PLUS_QM
)
3585 ? p
+ 1 < pend
&& *p
== '\\' && (p
[1] == '+' || p
[1] == '?')
3586 : p
!= pend
&& (*p
== '+' || *p
== '?'))
3587 || ((syntax
& RE_INTERVALS
)
3588 && ((syntax
& RE_NO_BK_BRACES
)
3589 ? p
!= pend
&& *p
== '{'
3590 : p
+ 1 < pend
&& p
[0] == '\\' && p
[1] == '{')))
3592 /* Start building a new exactn. */
3596 BUF_PUSH_2 (exactn
, 0);
3597 pending_exact
= b
- 1;
3600 GET_BUFFER_SPACE (MAX_MULTIBYTE_LENGTH
);
3606 len
= CHAR_STRING (c
, b
);
3610 (*pending_exact
) += len
;
3615 } /* while p != pend */
3618 /* Through the pattern now. */
3622 if (!COMPILE_STACK_EMPTY
)
3623 FREE_STACK_RETURN (REG_EPAREN
);
3625 /* If we don't want backtracking, force success
3626 the first time we reach the end of the compiled pattern. */
3627 if (syntax
& RE_NO_POSIX_BACKTRACKING
)
3630 /* We have succeeded; set the length of the buffer. */
3631 bufp
->used
= b
- bufp
->buffer
;
3636 re_compile_fastmap (bufp
);
3637 DEBUG_PRINT1 ("\nCompiled pattern: \n");
3638 print_compiled_pattern (bufp
);
3643 #ifndef MATCH_MAY_ALLOCATE
3644 /* Initialize the failure stack to the largest possible stack. This
3645 isn't necessary unless we're trying to avoid calling alloca in
3646 the search and match routines. */
3648 int num_regs
= bufp
->re_nsub
+ 1;
3650 if (fail_stack
.size
< re_max_failures
* TYPICAL_FAILURE_SIZE
)
3652 fail_stack
.size
= re_max_failures
* TYPICAL_FAILURE_SIZE
;
3654 if (! fail_stack
.stack
)
3656 = (fail_stack_elt_t
*) malloc (fail_stack
.size
3657 * sizeof (fail_stack_elt_t
));
3660 = (fail_stack_elt_t
*) realloc (fail_stack
.stack
,
3662 * sizeof (fail_stack_elt_t
)));
3665 regex_grow_registers (num_regs
);
3667 #endif /* not MATCH_MAY_ALLOCATE */
3669 FREE_STACK_RETURN (REG_NOERROR
);
3670 } /* regex_compile */
3672 /* Subroutines for `regex_compile'. */
3674 /* Store OP at LOC followed by two-byte integer parameter ARG. */
3677 store_op1 (op
, loc
, arg
)
3682 *loc
= (unsigned char) op
;
3683 STORE_NUMBER (loc
+ 1, arg
);
3687 /* Like `store_op1', but for two two-byte parameters ARG1 and ARG2. */
3690 store_op2 (op
, loc
, arg1
, arg2
)
3695 *loc
= (unsigned char) op
;
3696 STORE_NUMBER (loc
+ 1, arg1
);
3697 STORE_NUMBER (loc
+ 3, arg2
);
3701 /* Copy the bytes from LOC to END to open up three bytes of space at LOC
3702 for OP followed by two-byte integer parameter ARG. */
3705 insert_op1 (op
, loc
, arg
, end
)
3711 register unsigned char *pfrom
= end
;
3712 register unsigned char *pto
= end
+ 3;
3714 while (pfrom
!= loc
)
3717 store_op1 (op
, loc
, arg
);
3721 /* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2. */
3724 insert_op2 (op
, loc
, arg1
, arg2
, end
)
3730 register unsigned char *pfrom
= end
;
3731 register unsigned char *pto
= end
+ 5;
3733 while (pfrom
!= loc
)
3736 store_op2 (op
, loc
, arg1
, arg2
);
3740 /* P points to just after a ^ in PATTERN. Return true if that ^ comes
3741 after an alternative or a begin-subexpression. We assume there is at
3742 least one character before the ^. */
3745 at_begline_loc_p (pattern
, p
, syntax
)
3746 re_char
*pattern
, *p
;
3747 reg_syntax_t syntax
;
3749 re_char
*prev
= p
- 2;
3750 boolean prev_prev_backslash
= prev
> pattern
&& prev
[-1] == '\\';
3753 /* After a subexpression? */
3754 (*prev
== '(' && (syntax
& RE_NO_BK_PARENS
|| prev_prev_backslash
))
3755 /* After an alternative? */
3756 || (*prev
== '|' && (syntax
& RE_NO_BK_VBAR
|| prev_prev_backslash
))
3757 /* After a shy subexpression? */
3758 || ((syntax
& RE_SHY_GROUPS
) && prev
- 2 >= pattern
3759 && prev
[-1] == '?' && prev
[-2] == '('
3760 && (syntax
& RE_NO_BK_PARENS
3761 || (prev
- 3 >= pattern
&& prev
[-3] == '\\')));
3765 /* The dual of at_begline_loc_p. This one is for $. We assume there is
3766 at least one character after the $, i.e., `P < PEND'. */
3769 at_endline_loc_p (p
, pend
, syntax
)
3771 reg_syntax_t syntax
;
3774 boolean next_backslash
= *next
== '\\';
3775 re_char
*next_next
= p
+ 1 < pend
? p
+ 1 : 0;
3778 /* Before a subexpression? */
3779 (syntax
& RE_NO_BK_PARENS
? *next
== ')'
3780 : next_backslash
&& next_next
&& *next_next
== ')')
3781 /* Before an alternative? */
3782 || (syntax
& RE_NO_BK_VBAR
? *next
== '|'
3783 : next_backslash
&& next_next
&& *next_next
== '|');
3787 /* Returns true if REGNUM is in one of COMPILE_STACK's elements and
3788 false if it's not. */
3791 group_in_compile_stack (compile_stack
, regnum
)
3792 compile_stack_type compile_stack
;
3797 for (this_element
= compile_stack
.avail
- 1;
3800 if (compile_stack
.stack
[this_element
].regnum
== regnum
)
3807 If fastmap is non-NULL, go through the pattern and fill fastmap
3808 with all the possible leading chars. If fastmap is NULL, don't
3809 bother filling it up (obviously) and only return whether the
3810 pattern could potentially match the empty string.
3812 Return 1 if p..pend might match the empty string.
3813 Return 0 if p..pend matches at least one char.
3814 Return -1 if fastmap was not updated accurately. */
3817 analyse_first (p
, pend
, fastmap
, multibyte
)
3820 const int multibyte
;
3825 /* If all elements for base leading-codes in fastmap is set, this
3826 flag is set true. */
3827 boolean match_any_multibyte_characters
= false;
3831 /* The loop below works as follows:
3832 - It has a working-list kept in the PATTERN_STACK and which basically
3833 starts by only containing a pointer to the first operation.
3834 - If the opcode we're looking at is a match against some set of
3835 chars, then we add those chars to the fastmap and go on to the
3836 next work element from the worklist (done via `break').
3837 - If the opcode is a control operator on the other hand, we either
3838 ignore it (if it's meaningless at this point, such as `start_memory')
3839 or execute it (if it's a jump). If the jump has several destinations
3840 (i.e. `on_failure_jump'), then we push the other destination onto the
3842 We guarantee termination by ignoring backward jumps (more or less),
3843 so that `p' is monotonically increasing. More to the point, we
3844 never set `p' (or push) anything `<= p1'. */
3848 /* `p1' is used as a marker of how far back a `on_failure_jump'
3849 can go without being ignored. It is normally equal to `p'
3850 (which prevents any backward `on_failure_jump') except right
3851 after a plain `jump', to allow patterns such as:
3854 10: on_failure_jump 3
3855 as used for the *? operator. */
3858 switch (SWITCH_ENUM_CAST ((re_opcode_t
) *p
++))
3865 /* If the first character has to match a backreference, that means
3866 that the group was empty (since it already matched). Since this
3867 is the only case that interests us here, we can assume that the
3868 backreference must match the empty string. */
3873 /* Following are the cases which match a character. These end
3879 int c
= RE_STRING_CHAR (p
+ 1, pend
- p
);
3881 if (SINGLE_BYTE_CHAR_P (c
))
3890 /* We could put all the chars except for \n (and maybe \0)
3891 but we don't bother since it is generally not worth it. */
3892 if (!fastmap
) break;
3897 /* Chars beyond end of bitmap are possible matches.
3898 All the single-byte codes can occur in multibyte buffers.
3899 So any that are not listed in the charset
3900 are possible matches, even in multibyte buffers. */
3901 if (!fastmap
) break;
3902 for (j
= CHARSET_BITMAP_SIZE (&p
[-1]) * BYTEWIDTH
;
3903 j
< (1 << BYTEWIDTH
); j
++)
3907 if (!fastmap
) break;
3908 not = (re_opcode_t
) *(p
- 1) == charset_not
;
3909 for (j
= CHARSET_BITMAP_SIZE (&p
[-1]) * BYTEWIDTH
- 1, p
++;
3911 if (!!(p
[j
/ BYTEWIDTH
] & (1 << (j
% BYTEWIDTH
))) ^ not)
3914 if ((not && multibyte
)
3915 /* Any character set can possibly contain a character
3916 which doesn't match the specified set of characters. */
3917 || (CHARSET_RANGE_TABLE_EXISTS_P (&p
[-2])
3918 && CHARSET_RANGE_TABLE_BITS (&p
[-2]) != 0))
3919 /* If we can match a character class, we can match
3920 any character set. */
3922 set_fastmap_for_multibyte_characters
:
3923 if (match_any_multibyte_characters
== false)
3925 for (j
= 0x80; j
< 0xA0; j
++) /* XXX */
3926 if (BASE_LEADING_CODE_P (j
))
3928 match_any_multibyte_characters
= true;
3932 else if (!not && CHARSET_RANGE_TABLE_EXISTS_P (&p
[-2])
3933 && match_any_multibyte_characters
== false)
3935 /* Set fastmap[I] 1 where I is a base leading code of each
3936 multibyte character in the range table. */
3939 /* Make P points the range table. `+ 2' is to skip flag
3940 bits for a character class. */
3941 p
+= CHARSET_BITMAP_SIZE (&p
[-2]) + 2;
3943 /* Extract the number of ranges in range table into COUNT. */
3944 EXTRACT_NUMBER_AND_INCR (count
, p
);
3945 for (; count
> 0; count
--, p
+= 2 * 3) /* XXX */
3947 /* Extract the start of each range. */
3948 EXTRACT_CHARACTER (c
, p
);
3949 j
= CHAR_CHARSET (c
);
3950 fastmap
[CHARSET_LEADING_CODE_BASE (j
)] = 1;
3957 if (!fastmap
) break;
3959 not = (re_opcode_t
)p
[-1] == notsyntaxspec
;
3961 for (j
= 0; j
< (1 << BYTEWIDTH
); j
++)
3962 if ((SYNTAX (j
) == (enum syntaxcode
) k
) ^ not)
3966 /* This match depends on text properties. These end with
3967 aborting optimizations. */
3971 case notcategoryspec
:
3972 if (!fastmap
) break;
3973 not = (re_opcode_t
)p
[-1] == notcategoryspec
;
3975 for (j
= 0; j
< (1 << BYTEWIDTH
); j
++)
3976 if ((CHAR_HAS_CATEGORY (j
, k
)) ^ not)
3980 /* Any character set can possibly contain a character
3981 whose category is K (or not). */
3982 goto set_fastmap_for_multibyte_characters
;
3985 /* All cases after this match the empty string. These end with
4007 EXTRACT_NUMBER_AND_INCR (j
, p
);
4009 /* Backward jumps can only go back to code that we've already
4010 visited. `re_compile' should make sure this is true. */
4013 switch (SWITCH_ENUM_CAST ((re_opcode_t
) *p
))
4015 case on_failure_jump
:
4016 case on_failure_keep_string_jump
:
4017 case on_failure_jump_loop
:
4018 case on_failure_jump_nastyloop
:
4019 case on_failure_jump_smart
:
4025 /* Keep `p1' to allow the `on_failure_jump' we are jumping to
4026 to jump back to "just after here". */
4029 case on_failure_jump
:
4030 case on_failure_keep_string_jump
:
4031 case on_failure_jump_nastyloop
:
4032 case on_failure_jump_loop
:
4033 case on_failure_jump_smart
:
4034 EXTRACT_NUMBER_AND_INCR (j
, p
);
4036 ; /* Backward jump to be ignored. */
4038 { /* We have to look down both arms.
4039 We first go down the "straight" path so as to minimize
4040 stack usage when going through alternatives. */
4041 int r
= analyse_first (p
, pend
, fastmap
, multibyte
);
4049 /* This code simply does not properly handle forward jump_n. */
4050 DEBUG_STATEMENT (EXTRACT_NUMBER (j
, p
); assert (j
< 0));
4052 /* jump_n can either jump or fall through. The (backward) jump
4053 case has already been handled, so we only need to look at the
4054 fallthrough case. */
4058 /* If N == 0, it should be an on_failure_jump_loop instead. */
4059 DEBUG_STATEMENT (EXTRACT_NUMBER (j
, p
+ 2); assert (j
> 0));
4061 /* We only care about one iteration of the loop, so we don't
4062 need to consider the case where this behaves like an
4079 abort (); /* We have listed all the cases. */
4082 /* Getting here means we have found the possible starting
4083 characters for one path of the pattern -- and that the empty
4084 string does not match. We need not follow this path further. */
4088 /* We reached the end without matching anything. */
4091 } /* analyse_first */
4093 /* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
4094 BUFP. A fastmap records which of the (1 << BYTEWIDTH) possible
4095 characters can start a string that matches the pattern. This fastmap
4096 is used by re_search to skip quickly over impossible starting points.
4098 Character codes above (1 << BYTEWIDTH) are not represented in the
4099 fastmap, but the leading codes are represented. Thus, the fastmap
4100 indicates which character sets could start a match.
4102 The caller must supply the address of a (1 << BYTEWIDTH)-byte data
4103 area as BUFP->fastmap.
4105 We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in
4108 Returns 0 if we succeed, -2 if an internal error. */
4111 re_compile_fastmap (bufp
)
4112 struct re_pattern_buffer
*bufp
;
4114 char *fastmap
= bufp
->fastmap
;
4117 assert (fastmap
&& bufp
->buffer
);
4119 bzero (fastmap
, 1 << BYTEWIDTH
); /* Assume nothing's valid. */
4120 bufp
->fastmap_accurate
= 1; /* It will be when we're done. */
4122 analysis
= analyse_first (bufp
->buffer
, bufp
->buffer
+ bufp
->used
,
4123 fastmap
, RE_MULTIBYTE_P (bufp
));
4124 bufp
->can_be_null
= (analysis
!= 0);
4126 } /* re_compile_fastmap */
4128 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
4129 ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
4130 this memory for recording register information. STARTS and ENDS
4131 must be allocated using the malloc library routine, and must each
4132 be at least NUM_REGS * sizeof (regoff_t) bytes long.
4134 If NUM_REGS == 0, then subsequent matches should allocate their own
4137 Unless this function is called, the first search or match using
4138 PATTERN_BUFFER will allocate its own register data, without
4139 freeing the old data. */
4142 re_set_registers (bufp
, regs
, num_regs
, starts
, ends
)
4143 struct re_pattern_buffer
*bufp
;
4144 struct re_registers
*regs
;
4146 regoff_t
*starts
, *ends
;
4150 bufp
->regs_allocated
= REGS_REALLOCATE
;
4151 regs
->num_regs
= num_regs
;
4152 regs
->start
= starts
;
4157 bufp
->regs_allocated
= REGS_UNALLOCATED
;
4159 regs
->start
= regs
->end
= (regoff_t
*) 0;
4162 WEAK_ALIAS (__re_set_registers
, re_set_registers
)
4164 /* Searching routines. */
4166 /* Like re_search_2, below, but only one string is specified, and
4167 doesn't let you say where to stop matching. */
4170 re_search (bufp
, string
, size
, startpos
, range
, regs
)
4171 struct re_pattern_buffer
*bufp
;
4173 int size
, startpos
, range
;
4174 struct re_registers
*regs
;
4176 return re_search_2 (bufp
, NULL
, 0, string
, size
, startpos
, range
,
4179 WEAK_ALIAS (__re_search
, re_search
)
4181 /* Head address of virtual concatenation of string. */
4182 #define HEAD_ADDR_VSTRING(P) \
4183 (((P) >= size1 ? string2 : string1))
4185 /* End address of virtual concatenation of string. */
4186 #define STOP_ADDR_VSTRING(P) \
4187 (((P) >= size1 ? string2 + size2 : string1 + size1))
4189 /* Address of POS in the concatenation of virtual string. */
4190 #define POS_ADDR_VSTRING(POS) \
4191 (((POS) >= size1 ? string2 - size1 : string1) + (POS))
4193 /* Using the compiled pattern in BUFP->buffer, first tries to match the
4194 virtual concatenation of STRING1 and STRING2, starting first at index
4195 STARTPOS, then at STARTPOS + 1, and so on.
4197 STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
4199 RANGE is how far to scan while trying to match. RANGE = 0 means try
4200 only at STARTPOS; in general, the last start tried is STARTPOS +
4203 In REGS, return the indices of the virtual concatenation of STRING1
4204 and STRING2 that matched the entire BUFP->buffer and its contained
4207 Do not consider matching one past the index STOP in the virtual
4208 concatenation of STRING1 and STRING2.
4210 We return either the position in the strings at which the match was
4211 found, -1 if no match, or -2 if error (such as failure
4215 re_search_2 (bufp
, str1
, size1
, str2
, size2
, startpos
, range
, regs
, stop
)
4216 struct re_pattern_buffer
*bufp
;
4217 const char *str1
, *str2
;
4221 struct re_registers
*regs
;
4225 re_char
*string1
= (re_char
*) str1
;
4226 re_char
*string2
= (re_char
*) str2
;
4227 register char *fastmap
= bufp
->fastmap
;
4228 register RE_TRANSLATE_TYPE translate
= bufp
->translate
;
4229 int total_size
= size1
+ size2
;
4230 int endpos
= startpos
+ range
;
4231 boolean anchored_start
;
4233 /* Nonzero if we have to concern multibyte character. */
4234 const boolean multibyte
= RE_MULTIBYTE_P (bufp
);
4236 /* Check for out-of-range STARTPOS. */
4237 if (startpos
< 0 || startpos
> total_size
)
4240 /* Fix up RANGE if it might eventually take us outside
4241 the virtual concatenation of STRING1 and STRING2.
4242 Make sure we won't move STARTPOS below 0 or above TOTAL_SIZE. */
4244 range
= 0 - startpos
;
4245 else if (endpos
> total_size
)
4246 range
= total_size
- startpos
;
4248 /* If the search isn't to be a backwards one, don't waste time in a
4249 search for a pattern anchored at beginning of buffer. */
4250 if (bufp
->used
> 0 && (re_opcode_t
) bufp
->buffer
[0] == begbuf
&& range
> 0)
4259 /* In a forward search for something that starts with \=.
4260 don't keep searching past point. */
4261 if (bufp
->used
> 0 && (re_opcode_t
) bufp
->buffer
[0] == at_dot
&& range
> 0)
4263 range
= PT_BYTE
- BEGV_BYTE
- startpos
;
4269 /* Update the fastmap now if not correct already. */
4270 if (fastmap
&& !bufp
->fastmap_accurate
)
4271 re_compile_fastmap (bufp
);
4273 /* See whether the pattern is anchored. */
4274 anchored_start
= (bufp
->buffer
[0] == begline
);
4277 gl_state
.object
= re_match_object
;
4279 int charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (POS_AS_IN_BUFFER (startpos
));
4281 SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object
, charpos
, 1);
4285 /* Loop through the string, looking for a place to start matching. */
4288 /* If the pattern is anchored,
4289 skip quickly past places we cannot match.
4290 We don't bother to treat startpos == 0 specially
4291 because that case doesn't repeat. */
4292 if (anchored_start
&& startpos
> 0)
4294 if (! ((startpos
<= size1
? string1
[startpos
- 1]
4295 : string2
[startpos
- size1
- 1])
4300 /* If a fastmap is supplied, skip quickly over characters that
4301 cannot be the start of a match. If the pattern can match the
4302 null string, however, we don't need to skip characters; we want
4303 the first null string. */
4304 if (fastmap
&& startpos
< total_size
&& !bufp
->can_be_null
)
4306 register re_char
*d
;
4307 register re_wchar_t buf_ch
;
4309 d
= POS_ADDR_VSTRING (startpos
);
4311 if (range
> 0) /* Searching forwards. */
4313 register int lim
= 0;
4316 if (startpos
< size1
&& startpos
+ range
>= size1
)
4317 lim
= range
- (size1
- startpos
);
4319 /* Written out as an if-else to avoid testing `translate'
4321 if (RE_TRANSLATE_P (translate
))
4328 buf_ch
= STRING_CHAR_AND_LENGTH (d
, range
- lim
,
4331 buf_ch
= RE_TRANSLATE (translate
, buf_ch
);
4336 range
-= buf_charlen
;
4341 /* Convert *d to integer to shut up GCC's
4342 whining about comparison that is always
4347 && !fastmap
[RE_TRANSLATE (translate
, di
)])
4355 while (range
> lim
&& !fastmap
[*d
])
4361 startpos
+= irange
- range
;
4363 else /* Searching backwards. */
4365 int room
= (startpos
>= size1
4366 ? size2
+ size1
- startpos
4367 : size1
- startpos
);
4368 buf_ch
= RE_STRING_CHAR (d
, room
);
4369 buf_ch
= TRANSLATE (buf_ch
);
4371 if (! (buf_ch
>= 0400
4372 || fastmap
[buf_ch
]))
4377 /* If can't match the null string, and that's all we have left, fail. */
4378 if (range
>= 0 && startpos
== total_size
&& fastmap
4379 && !bufp
->can_be_null
)
4382 val
= re_match_2_internal (bufp
, string1
, size1
, string2
, size2
,
4383 startpos
, regs
, stop
);
4384 #ifndef REGEX_MALLOC
4401 /* Update STARTPOS to the next character boundary. */
4404 re_char
*p
= POS_ADDR_VSTRING (startpos
);
4405 re_char
*pend
= STOP_ADDR_VSTRING (startpos
);
4406 int len
= MULTIBYTE_FORM_LENGTH (p
, pend
- p
);
4424 /* Update STARTPOS to the previous character boundary. */
4427 re_char
*p
= POS_ADDR_VSTRING (startpos
) + 1;
4429 re_char
*phead
= HEAD_ADDR_VSTRING (startpos
);
4431 /* Find the head of multibyte form. */
4432 PREV_CHAR_BOUNDARY (p
, phead
);
4433 range
+= p0
- 1 - p
;
4437 startpos
-= p0
- 1 - p
;
4443 WEAK_ALIAS (__re_search_2
, re_search_2
)
4445 /* Declarations and macros for re_match_2. */
4447 static int bcmp_translate
_RE_ARGS((re_char
*s1
, re_char
*s2
,
4449 RE_TRANSLATE_TYPE translate
,
4450 const int multibyte
));
4452 /* This converts PTR, a pointer into one of the search strings `string1'
4453 and `string2' into an offset from the beginning of that string. */
4454 #define POINTER_TO_OFFSET(ptr) \
4455 (FIRST_STRING_P (ptr) \
4456 ? ((regoff_t) ((ptr) - string1)) \
4457 : ((regoff_t) ((ptr) - string2 + size1)))
4459 /* Call before fetching a character with *d. This switches over to
4460 string2 if necessary.
4461 Check re_match_2_internal for a discussion of why end_match_2 might
4462 not be within string2 (but be equal to end_match_1 instead). */
4463 #define PREFETCH() \
4466 /* End of string2 => fail. */ \
4467 if (dend == end_match_2) \
4469 /* End of string1 => advance to string2. */ \
4471 dend = end_match_2; \
4474 /* Call before fetching a char with *d if you already checked other limits.
4475 This is meant for use in lookahead operations like wordend, etc..
4476 where we might need to look at parts of the string that might be
4477 outside of the LIMITs (i.e past `stop'). */
4478 #define PREFETCH_NOLIMIT() \
4482 dend = end_match_2; \
4485 /* Test if at very beginning or at very end of the virtual concatenation
4486 of `string1' and `string2'. If only one string, it's `string2'. */
4487 #define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2)
4488 #define AT_STRINGS_END(d) ((d) == end2)
4491 /* Test if D points to a character which is word-constituent. We have
4492 two special cases to check for: if past the end of string1, look at
4493 the first character in string2; and if before the beginning of
4494 string2, look at the last character in string1. */
4495 #define WORDCHAR_P(d) \
4496 (SYNTAX ((d) == end1 ? *string2 \
4497 : (d) == string2 - 1 ? *(end1 - 1) : *(d)) \
4500 /* Disabled due to a compiler bug -- see comment at case wordbound */
4502 /* The comment at case wordbound is following one, but we don't use
4503 AT_WORD_BOUNDARY anymore to support multibyte form.
4505 The DEC Alpha C compiler 3.x generates incorrect code for the
4506 test WORDCHAR_P (d - 1) != WORDCHAR_P (d) in the expansion of
4507 AT_WORD_BOUNDARY, so this code is disabled. Expanding the
4508 macro and introducing temporary variables works around the bug. */
4511 /* Test if the character before D and the one at D differ with respect
4512 to being word-constituent. */
4513 #define AT_WORD_BOUNDARY(d) \
4514 (AT_STRINGS_BEG (d) || AT_STRINGS_END (d) \
4515 || WORDCHAR_P (d - 1) != WORDCHAR_P (d))
4518 /* Free everything we malloc. */
4519 #ifdef MATCH_MAY_ALLOCATE
4520 # define FREE_VAR(var) if (var) { REGEX_FREE (var); var = NULL; } else
4521 # define FREE_VARIABLES() \
4523 REGEX_FREE_STACK (fail_stack.stack); \
4524 FREE_VAR (regstart); \
4525 FREE_VAR (regend); \
4526 FREE_VAR (best_regstart); \
4527 FREE_VAR (best_regend); \
4530 # define FREE_VARIABLES() ((void)0) /* Do nothing! But inhibit gcc warning. */
4531 #endif /* not MATCH_MAY_ALLOCATE */
4534 /* Optimization routines. */
4536 /* If the operation is a match against one or more chars,
4537 return a pointer to the next operation, else return NULL. */
4542 switch (SWITCH_ENUM_CAST (*p
++))
4553 if (CHARSET_RANGE_TABLE_EXISTS_P (p
- 1))
4556 p
= CHARSET_RANGE_TABLE (p
- 1);
4557 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
4558 p
= CHARSET_RANGE_TABLE_END (p
, mcnt
);
4561 p
+= 1 + CHARSET_BITMAP_SIZE (p
- 1);
4568 case notcategoryspec
:
4580 /* Jump over non-matching operations. */
4582 skip_noops (p
, pend
)
4588 switch (SWITCH_ENUM_CAST ((re_opcode_t
) *p
))
4597 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
4608 /* Non-zero if "p1 matches something" implies "p2 fails". */
4610 mutually_exclusive_p (bufp
, p1
, p2
)
4611 struct re_pattern_buffer
*bufp
;
4615 const boolean multibyte
= RE_MULTIBYTE_P (bufp
);
4616 unsigned char *pend
= bufp
->buffer
+ bufp
->used
;
4618 assert (p1
>= bufp
->buffer
&& p1
< pend
4619 && p2
>= bufp
->buffer
&& p2
<= pend
);
4621 /* Skip over open/close-group commands.
4622 If what follows this loop is a ...+ construct,
4623 look at what begins its body, since we will have to
4624 match at least one of that. */
4625 p2
= skip_noops (p2
, pend
);
4626 /* The same skip can be done for p1, except that this function
4627 is only used in the case where p1 is a simple match operator. */
4628 /* p1 = skip_noops (p1, pend); */
4630 assert (p1
>= bufp
->buffer
&& p1
< pend
4631 && p2
>= bufp
->buffer
&& p2
<= pend
);
4633 op2
= p2
== pend
? succeed
: *p2
;
4635 switch (SWITCH_ENUM_CAST (op2
))
4639 /* If we're at the end of the pattern, we can change. */
4640 if (skip_one_char (p1
))
4642 DEBUG_PRINT1 (" End of pattern: fast loop.\n");
4650 register re_wchar_t c
4651 = (re_opcode_t
) *p2
== endline
? '\n'
4652 : RE_STRING_CHAR (p2
+ 2, pend
- p2
- 2);
4654 if ((re_opcode_t
) *p1
== exactn
)
4656 if (c
!= RE_STRING_CHAR (p1
+ 2, pend
- p1
- 2))
4658 DEBUG_PRINT3 (" '%c' != '%c' => fast loop.\n", c
, p1
[2]);
4663 else if ((re_opcode_t
) *p1
== charset
4664 || (re_opcode_t
) *p1
== charset_not
)
4666 int not = (re_opcode_t
) *p1
== charset_not
;
4668 /* Test if C is listed in charset (or charset_not)
4670 if (SINGLE_BYTE_CHAR_P (c
))
4672 if (c
< CHARSET_BITMAP_SIZE (p1
) * BYTEWIDTH
4673 && p1
[2 + c
/ BYTEWIDTH
] & (1 << (c
% BYTEWIDTH
)))
4676 else if (CHARSET_RANGE_TABLE_EXISTS_P (p1
))
4677 CHARSET_LOOKUP_RANGE_TABLE (not, c
, p1
);
4679 /* `not' is equal to 1 if c would match, which means
4680 that we can't change to pop_failure_jump. */
4683 DEBUG_PRINT1 (" No match => fast loop.\n");
4687 else if ((re_opcode_t
) *p1
== anychar
4690 DEBUG_PRINT1 (" . != \\n => fast loop.\n");
4698 if ((re_opcode_t
) *p1
== exactn
)
4699 /* Reuse the code above. */
4700 return mutually_exclusive_p (bufp
, p2
, p1
);
4702 /* It is hard to list up all the character in charset
4703 P2 if it includes multibyte character. Give up in
4705 else if (!multibyte
|| !CHARSET_RANGE_TABLE_EXISTS_P (p2
))
4707 /* Now, we are sure that P2 has no range table.
4708 So, for the size of bitmap in P2, `p2[1]' is
4709 enough. But P1 may have range table, so the
4710 size of bitmap table of P1 is extracted by
4711 using macro `CHARSET_BITMAP_SIZE'.
4713 Since we know that all the character listed in
4714 P2 is ASCII, it is enough to test only bitmap
4717 if ((re_opcode_t
) *p1
== charset
)
4720 /* We win if the charset inside the loop
4721 has no overlap with the one after the loop. */
4724 && idx
< CHARSET_BITMAP_SIZE (p1
));
4726 if ((p2
[2 + idx
] & p1
[2 + idx
]) != 0)
4730 || idx
== CHARSET_BITMAP_SIZE (p1
))
4732 DEBUG_PRINT1 (" No match => fast loop.\n");
4736 else if ((re_opcode_t
) *p1
== charset_not
)
4739 /* We win if the charset_not inside the loop lists
4740 every character listed in the charset after. */
4741 for (idx
= 0; idx
< (int) p2
[1]; idx
++)
4742 if (! (p2
[2 + idx
] == 0
4743 || (idx
< CHARSET_BITMAP_SIZE (p1
)
4744 && ((p2
[2 + idx
] & ~ p1
[2 + idx
]) == 0))))
4749 DEBUG_PRINT1 (" No match => fast loop.\n");
4758 switch (SWITCH_ENUM_CAST (*p1
))
4762 /* Reuse the code above. */
4763 return mutually_exclusive_p (bufp
, p2
, p1
);
4765 /* When we have two charset_not, it's very unlikely that
4766 they don't overlap. The union of the two sets of excluded
4767 chars should cover all possible chars, which, as a matter of
4768 fact, is virtually impossible in multibyte buffers. */
4774 return ((re_opcode_t
) *p1
== syntaxspec
&& p1
[1] == Sword
);
4776 return ((re_opcode_t
) *p1
== syntaxspec
4777 && (p1
[1] == Ssymbol
|| p1
[1] == Sword
));
4779 return ((re_opcode_t
) *p1
== syntaxspec
&& p1
[1] == p2
[1]);
4782 return ((re_opcode_t
) *p1
== notsyntaxspec
&& p1
[1] == Sword
);
4784 return ((re_opcode_t
) *p1
== notsyntaxspec
4785 && (p1
[1] == Ssymbol
|| p1
[1] == Sword
));
4787 return ((re_opcode_t
) *p1
== notsyntaxspec
&& p1
[1] == p2
[1]);
4790 return (((re_opcode_t
) *p1
== notsyntaxspec
4791 || (re_opcode_t
) *p1
== syntaxspec
)
4796 return ((re_opcode_t
) *p1
== notcategoryspec
&& p1
[1] == p2
[1]);
4797 case notcategoryspec
:
4798 return ((re_opcode_t
) *p1
== categoryspec
&& p1
[1] == p2
[1]);
4810 /* Matching routines. */
4812 #ifndef emacs /* Emacs never uses this. */
4813 /* re_match is like re_match_2 except it takes only a single string. */
4816 re_match (bufp
, string
, size
, pos
, regs
)
4817 struct re_pattern_buffer
*bufp
;
4820 struct re_registers
*regs
;
4822 int result
= re_match_2_internal (bufp
, NULL
, 0, (re_char
*) string
, size
,
4824 # if defined C_ALLOCA && !defined REGEX_MALLOC
4829 WEAK_ALIAS (__re_match
, re_match
)
4830 #endif /* not emacs */
4833 /* In Emacs, this is the string or buffer in which we
4834 are matching. It is used for looking up syntax properties. */
4835 Lisp_Object re_match_object
;
4838 /* re_match_2 matches the compiled pattern in BUFP against the
4839 the (virtual) concatenation of STRING1 and STRING2 (of length SIZE1
4840 and SIZE2, respectively). We start matching at POS, and stop
4843 If REGS is non-null and the `no_sub' field of BUFP is nonzero, we
4844 store offsets for the substring each group matched in REGS. See the
4845 documentation for exactly how many groups we fill.
4847 We return -1 if no match, -2 if an internal error (such as the
4848 failure stack overflowing). Otherwise, we return the length of the
4849 matched substring. */
4852 re_match_2 (bufp
, string1
, size1
, string2
, size2
, pos
, regs
, stop
)
4853 struct re_pattern_buffer
*bufp
;
4854 const char *string1
, *string2
;
4857 struct re_registers
*regs
;
4864 gl_state
.object
= re_match_object
;
4865 charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (POS_AS_IN_BUFFER (pos
));
4866 SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object
, charpos
, 1);
4869 result
= re_match_2_internal (bufp
, (re_char
*) string1
, size1
,
4870 (re_char
*) string2
, size2
,
4872 #if defined C_ALLOCA && !defined REGEX_MALLOC
4877 WEAK_ALIAS (__re_match_2
, re_match_2
)
4879 /* This is a separate function so that we can force an alloca cleanup
4882 re_match_2_internal (bufp
, string1
, size1
, string2
, size2
, pos
, regs
, stop
)
4883 struct re_pattern_buffer
*bufp
;
4884 re_char
*string1
, *string2
;
4887 struct re_registers
*regs
;
4890 /* General temporaries. */
4895 /* Just past the end of the corresponding string. */
4896 re_char
*end1
, *end2
;
4898 /* Pointers into string1 and string2, just past the last characters in
4899 each to consider matching. */
4900 re_char
*end_match_1
, *end_match_2
;
4902 /* Where we are in the data, and the end of the current string. */
4905 /* Used sometimes to remember where we were before starting matching
4906 an operator so that we can go back in case of failure. This "atomic"
4907 behavior of matching opcodes is indispensable to the correctness
4908 of the on_failure_keep_string_jump optimization. */
4911 /* Where we are in the pattern, and the end of the pattern. */
4912 re_char
*p
= bufp
->buffer
;
4913 re_char
*pend
= p
+ bufp
->used
;
4915 /* We use this to map every character in the string. */
4916 RE_TRANSLATE_TYPE translate
= bufp
->translate
;
4918 /* Nonzero if we have to concern multibyte character. */
4919 const boolean multibyte
= RE_MULTIBYTE_P (bufp
);
4921 /* Failure point stack. Each place that can handle a failure further
4922 down the line pushes a failure point on this stack. It consists of
4923 regstart, and regend for all registers corresponding to
4924 the subexpressions we're currently inside, plus the number of such
4925 registers, and, finally, two char *'s. The first char * is where
4926 to resume scanning the pattern; the second one is where to resume
4927 scanning the strings. */
4928 #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
4929 fail_stack_type fail_stack
;
4932 unsigned nfailure_points_pushed
= 0, nfailure_points_popped
= 0;
4935 #if defined REL_ALLOC && defined REGEX_MALLOC
4936 /* This holds the pointer to the failure stack, when
4937 it is allocated relocatably. */
4938 fail_stack_elt_t
*failure_stack_ptr
;
4941 /* We fill all the registers internally, independent of what we
4942 return, for use in backreferences. The number here includes
4943 an element for register zero. */
4944 size_t num_regs
= bufp
->re_nsub
+ 1;
4946 /* Information on the contents of registers. These are pointers into
4947 the input strings; they record just what was matched (on this
4948 attempt) by a subexpression part of the pattern, that is, the
4949 regnum-th regstart pointer points to where in the pattern we began
4950 matching and the regnum-th regend points to right after where we
4951 stopped matching the regnum-th subexpression. (The zeroth register
4952 keeps track of what the whole pattern matches.) */
4953 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
4954 re_char
**regstart
, **regend
;
4957 /* The following record the register info as found in the above
4958 variables when we find a match better than any we've seen before.
4959 This happens as we backtrack through the failure points, which in
4960 turn happens only if we have not yet matched the entire string. */
4961 unsigned best_regs_set
= false;
4962 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
4963 re_char
**best_regstart
, **best_regend
;
4966 /* Logically, this is `best_regend[0]'. But we don't want to have to
4967 allocate space for that if we're not allocating space for anything
4968 else (see below). Also, we never need info about register 0 for
4969 any of the other register vectors, and it seems rather a kludge to
4970 treat `best_regend' differently than the rest. So we keep track of
4971 the end of the best match so far in a separate variable. We
4972 initialize this to NULL so that when we backtrack the first time
4973 and need to test it, it's not garbage. */
4974 re_char
*match_end
= NULL
;
4977 /* Counts the total number of registers pushed. */
4978 unsigned num_regs_pushed
= 0;
4981 DEBUG_PRINT1 ("\n\nEntering re_match_2.\n");
4985 #ifdef MATCH_MAY_ALLOCATE
4986 /* Do not bother to initialize all the register variables if there are
4987 no groups in the pattern, as it takes a fair amount of time. If
4988 there are groups, we include space for register 0 (the whole
4989 pattern), even though we never use it, since it simplifies the
4990 array indexing. We should fix this. */
4993 regstart
= REGEX_TALLOC (num_regs
, re_char
*);
4994 regend
= REGEX_TALLOC (num_regs
, re_char
*);
4995 best_regstart
= REGEX_TALLOC (num_regs
, re_char
*);
4996 best_regend
= REGEX_TALLOC (num_regs
, re_char
*);
4998 if (!(regstart
&& regend
&& best_regstart
&& best_regend
))
5006 /* We must initialize all our variables to NULL, so that
5007 `FREE_VARIABLES' doesn't try to free them. */
5008 regstart
= regend
= best_regstart
= best_regend
= NULL
;
5010 #endif /* MATCH_MAY_ALLOCATE */
5012 /* The starting position is bogus. */
5013 if (pos
< 0 || pos
> size1
+ size2
)
5019 /* Initialize subexpression text positions to -1 to mark ones that no
5020 start_memory/stop_memory has been seen for. Also initialize the
5021 register information struct. */
5022 for (reg
= 1; reg
< num_regs
; reg
++)
5023 regstart
[reg
] = regend
[reg
] = NULL
;
5025 /* We move `string1' into `string2' if the latter's empty -- but not if
5026 `string1' is null. */
5027 if (size2
== 0 && string1
!= NULL
)
5034 end1
= string1
+ size1
;
5035 end2
= string2
+ size2
;
5037 /* `p' scans through the pattern as `d' scans through the data.
5038 `dend' is the end of the input string that `d' points within. `d'
5039 is advanced into the following input string whenever necessary, but
5040 this happens before fetching; therefore, at the beginning of the
5041 loop, `d' can be pointing at the end of a string, but it cannot
5045 /* Only match within string2. */
5046 d
= string2
+ pos
- size1
;
5047 dend
= end_match_2
= string2
+ stop
- size1
;
5048 end_match_1
= end1
; /* Just to give it a value. */
5054 /* Only match within string1. */
5055 end_match_1
= string1
+ stop
;
5057 When we reach end_match_1, PREFETCH normally switches to string2.
5058 But in the present case, this means that just doing a PREFETCH
5059 makes us jump from `stop' to `gap' within the string.
5060 What we really want here is for the search to stop as
5061 soon as we hit end_match_1. That's why we set end_match_2
5062 to end_match_1 (since PREFETCH fails as soon as we hit
5064 end_match_2
= end_match_1
;
5067 { /* It's important to use this code when stop == size so that
5068 moving `d' from end1 to string2 will not prevent the d == dend
5069 check from catching the end of string. */
5071 end_match_2
= string2
+ stop
- size1
;
5077 DEBUG_PRINT1 ("The compiled pattern is: ");
5078 DEBUG_PRINT_COMPILED_PATTERN (bufp
, p
, pend
);
5079 DEBUG_PRINT1 ("The string to match is: `");
5080 DEBUG_PRINT_DOUBLE_STRING (d
, string1
, size1
, string2
, size2
);
5081 DEBUG_PRINT1 ("'\n");
5083 /* This loops over pattern commands. It exits by returning from the
5084 function if the match is complete, or it drops through if the match
5085 fails at this starting point in the input data. */
5088 DEBUG_PRINT2 ("\n%p: ", p
);
5091 { /* End of pattern means we might have succeeded. */
5092 DEBUG_PRINT1 ("end of pattern ... ");
5094 /* If we haven't matched the entire string, and we want the
5095 longest match, try backtracking. */
5096 if (d
!= end_match_2
)
5098 /* 1 if this match ends in the same string (string1 or string2)
5099 as the best previous match. */
5100 boolean same_str_p
= (FIRST_STRING_P (match_end
)
5101 == FIRST_STRING_P (d
));
5102 /* 1 if this match is the best seen so far. */
5103 boolean best_match_p
;
5105 /* AIX compiler got confused when this was combined
5106 with the previous declaration. */
5108 best_match_p
= d
> match_end
;
5110 best_match_p
= !FIRST_STRING_P (d
);
5112 DEBUG_PRINT1 ("backtracking.\n");
5114 if (!FAIL_STACK_EMPTY ())
5115 { /* More failure points to try. */
5117 /* If exceeds best match so far, save it. */
5118 if (!best_regs_set
|| best_match_p
)
5120 best_regs_set
= true;
5123 DEBUG_PRINT1 ("\nSAVING match as best so far.\n");
5125 for (reg
= 1; reg
< num_regs
; reg
++)
5127 best_regstart
[reg
] = regstart
[reg
];
5128 best_regend
[reg
] = regend
[reg
];
5134 /* If no failure points, don't restore garbage. And if
5135 last match is real best match, don't restore second
5137 else if (best_regs_set
&& !best_match_p
)
5140 /* Restore best match. It may happen that `dend ==
5141 end_match_1' while the restored d is in string2.
5142 For example, the pattern `x.*y.*z' against the
5143 strings `x-' and `y-z-', if the two strings are
5144 not consecutive in memory. */
5145 DEBUG_PRINT1 ("Restoring best registers.\n");
5148 dend
= ((d
>= string1
&& d
<= end1
)
5149 ? end_match_1
: end_match_2
);
5151 for (reg
= 1; reg
< num_regs
; reg
++)
5153 regstart
[reg
] = best_regstart
[reg
];
5154 regend
[reg
] = best_regend
[reg
];
5157 } /* d != end_match_2 */
5160 DEBUG_PRINT1 ("Accepting match.\n");
5162 /* If caller wants register contents data back, do it. */
5163 if (regs
&& !bufp
->no_sub
)
5165 /* Have the register data arrays been allocated? */
5166 if (bufp
->regs_allocated
== REGS_UNALLOCATED
)
5167 { /* No. So allocate them with malloc. We need one
5168 extra element beyond `num_regs' for the `-1' marker
5170 regs
->num_regs
= MAX (RE_NREGS
, num_regs
+ 1);
5171 regs
->start
= TALLOC (regs
->num_regs
, regoff_t
);
5172 regs
->end
= TALLOC (regs
->num_regs
, regoff_t
);
5173 if (regs
->start
== NULL
|| regs
->end
== NULL
)
5178 bufp
->regs_allocated
= REGS_REALLOCATE
;
5180 else if (bufp
->regs_allocated
== REGS_REALLOCATE
)
5181 { /* Yes. If we need more elements than were already
5182 allocated, reallocate them. If we need fewer, just
5184 if (regs
->num_regs
< num_regs
+ 1)
5186 regs
->num_regs
= num_regs
+ 1;
5187 RETALLOC (regs
->start
, regs
->num_regs
, regoff_t
);
5188 RETALLOC (regs
->end
, regs
->num_regs
, regoff_t
);
5189 if (regs
->start
== NULL
|| regs
->end
== NULL
)
5198 /* These braces fend off a "empty body in an else-statement"
5199 warning under GCC when assert expands to nothing. */
5200 assert (bufp
->regs_allocated
== REGS_FIXED
);
5203 /* Convert the pointer data in `regstart' and `regend' to
5204 indices. Register zero has to be set differently,
5205 since we haven't kept track of any info for it. */
5206 if (regs
->num_regs
> 0)
5208 regs
->start
[0] = pos
;
5209 regs
->end
[0] = POINTER_TO_OFFSET (d
);
5212 /* Go through the first `min (num_regs, regs->num_regs)'
5213 registers, since that is all we initialized. */
5214 for (reg
= 1; reg
< MIN (num_regs
, regs
->num_regs
); reg
++)
5216 if (REG_UNSET (regstart
[reg
]) || REG_UNSET (regend
[reg
]))
5217 regs
->start
[reg
] = regs
->end
[reg
] = -1;
5221 = (regoff_t
) POINTER_TO_OFFSET (regstart
[reg
]);
5223 = (regoff_t
) POINTER_TO_OFFSET (regend
[reg
]);
5227 /* If the regs structure we return has more elements than
5228 were in the pattern, set the extra elements to -1. If
5229 we (re)allocated the registers, this is the case,
5230 because we always allocate enough to have at least one
5232 for (reg
= num_regs
; reg
< regs
->num_regs
; reg
++)
5233 regs
->start
[reg
] = regs
->end
[reg
] = -1;
5234 } /* regs && !bufp->no_sub */
5236 DEBUG_PRINT4 ("%u failure points pushed, %u popped (%u remain).\n",
5237 nfailure_points_pushed
, nfailure_points_popped
,
5238 nfailure_points_pushed
- nfailure_points_popped
);
5239 DEBUG_PRINT2 ("%u registers pushed.\n", num_regs_pushed
);
5241 mcnt
= POINTER_TO_OFFSET (d
) - pos
;
5243 DEBUG_PRINT2 ("Returning %d from re_match_2.\n", mcnt
);
5249 /* Otherwise match next pattern command. */
5250 switch (SWITCH_ENUM_CAST ((re_opcode_t
) *p
++))
5252 /* Ignore these. Used to ignore the n of succeed_n's which
5253 currently have n == 0. */
5255 DEBUG_PRINT1 ("EXECUTING no_op.\n");
5259 DEBUG_PRINT1 ("EXECUTING succeed.\n");
5262 /* Match the next n pattern characters exactly. The following
5263 byte in the pattern defines n, and the n bytes after that
5264 are the characters to match. */
5267 DEBUG_PRINT2 ("EXECUTING exactn %d.\n", mcnt
);
5269 /* Remember the start point to rollback upon failure. */
5272 /* This is written out as an if-else so we don't waste time
5273 testing `translate' inside the loop. */
5274 if (RE_TRANSLATE_P (translate
))
5279 int pat_charlen
, buf_charlen
;
5280 unsigned int pat_ch
, buf_ch
;
5283 pat_ch
= STRING_CHAR_AND_LENGTH (p
, pend
- p
, pat_charlen
);
5284 buf_ch
= STRING_CHAR_AND_LENGTH (d
, dend
- d
, buf_charlen
);
5286 if (RE_TRANSLATE (translate
, buf_ch
)
5295 mcnt
-= pat_charlen
;
5301 /* Avoid compiler whining about comparison being
5307 if (RE_TRANSLATE (translate
, di
) != *p
++)
5332 /* Match any character except possibly a newline or a null. */
5338 DEBUG_PRINT1 ("EXECUTING anychar.\n");
5341 buf_ch
= RE_STRING_CHAR_AND_LENGTH (d
, dend
- d
, buf_charlen
);
5342 buf_ch
= TRANSLATE (buf_ch
);
5344 if ((!(bufp
->syntax
& RE_DOT_NEWLINE
)
5346 || ((bufp
->syntax
& RE_DOT_NOT_NULL
)
5347 && buf_ch
== '\000'))
5350 DEBUG_PRINT2 (" Matched `%d'.\n", *d
);
5359 register unsigned int c
;
5360 boolean
not = (re_opcode_t
) *(p
- 1) == charset_not
;
5363 /* Start of actual range_table, or end of bitmap if there is no
5365 re_char
*range_table
;
5367 /* Nonzero if there is a range table. */
5368 int range_table_exists
;
5370 /* Number of ranges of range table. This is not included
5371 in the initial byte-length of the command. */
5374 DEBUG_PRINT2 ("EXECUTING charset%s.\n", not ? "_not" : "");
5376 range_table_exists
= CHARSET_RANGE_TABLE_EXISTS_P (&p
[-1]);
5378 if (range_table_exists
)
5380 range_table
= CHARSET_RANGE_TABLE (&p
[-1]); /* Past the bitmap. */
5381 EXTRACT_NUMBER_AND_INCR (count
, range_table
);
5385 c
= RE_STRING_CHAR_AND_LENGTH (d
, dend
- d
, len
);
5386 c
= TRANSLATE (c
); /* The character to match. */
5388 if (SINGLE_BYTE_CHAR_P (c
))
5389 { /* Lookup bitmap. */
5390 /* Cast to `unsigned' instead of `unsigned char' in
5391 case the bit list is a full 32 bytes long. */
5392 if (c
< (unsigned) (CHARSET_BITMAP_SIZE (&p
[-1]) * BYTEWIDTH
)
5393 && p
[1 + c
/ BYTEWIDTH
] & (1 << (c
% BYTEWIDTH
)))
5397 else if (range_table_exists
)
5399 int class_bits
= CHARSET_RANGE_TABLE_BITS (&p
[-1]);
5401 if ( (class_bits
& BIT_LOWER
&& ISLOWER (c
))
5402 | (class_bits
& BIT_MULTIBYTE
)
5403 | (class_bits
& BIT_PUNCT
&& ISPUNCT (c
))
5404 | (class_bits
& BIT_SPACE
&& ISSPACE (c
))
5405 | (class_bits
& BIT_UPPER
&& ISUPPER (c
))
5406 | (class_bits
& BIT_WORD
&& ISWORD (c
)))
5409 CHARSET_LOOKUP_RANGE_TABLE_RAW (not, c
, range_table
, count
);
5413 if (range_table_exists
)
5414 p
= CHARSET_RANGE_TABLE_END (range_table
, count
);
5416 p
+= CHARSET_BITMAP_SIZE (&p
[-1]) + 1;
5418 if (!not) goto fail
;
5425 /* The beginning of a group is represented by start_memory.
5426 The argument is the register number. The text
5427 matched within the group is recorded (in the internal
5428 registers data structure) under the register number. */
5430 DEBUG_PRINT2 ("EXECUTING start_memory %d:\n", *p
);
5432 /* In case we need to undo this operation (via backtracking). */
5433 PUSH_FAILURE_REG ((unsigned int)*p
);
5436 regend
[*p
] = NULL
; /* probably unnecessary. -sm */
5437 DEBUG_PRINT2 (" regstart: %d\n", POINTER_TO_OFFSET (regstart
[*p
]));
5439 /* Move past the register number and inner group count. */
5444 /* The stop_memory opcode represents the end of a group. Its
5445 argument is the same as start_memory's: the register number. */
5447 DEBUG_PRINT2 ("EXECUTING stop_memory %d:\n", *p
);
5449 assert (!REG_UNSET (regstart
[*p
]));
5450 /* Strictly speaking, there should be code such as:
5452 assert (REG_UNSET (regend[*p]));
5453 PUSH_FAILURE_REGSTOP ((unsigned int)*p);
5455 But the only info to be pushed is regend[*p] and it is known to
5456 be UNSET, so there really isn't anything to push.
5457 Not pushing anything, on the other hand deprives us from the
5458 guarantee that regend[*p] is UNSET since undoing this operation
5459 will not reset its value properly. This is not important since
5460 the value will only be read on the next start_memory or at
5461 the very end and both events can only happen if this stop_memory
5465 DEBUG_PRINT2 (" regend: %d\n", POINTER_TO_OFFSET (regend
[*p
]));
5467 /* Move past the register number and the inner group count. */
5472 /* \<digit> has been turned into a `duplicate' command which is
5473 followed by the numeric value of <digit> as the register number. */
5476 register re_char
*d2
, *dend2
;
5477 int regno
= *p
++; /* Get which register to match against. */
5478 DEBUG_PRINT2 ("EXECUTING duplicate %d.\n", regno
);
5480 /* Can't back reference a group which we've never matched. */
5481 if (REG_UNSET (regstart
[regno
]) || REG_UNSET (regend
[regno
]))
5484 /* Where in input to try to start matching. */
5485 d2
= regstart
[regno
];
5487 /* Remember the start point to rollback upon failure. */
5490 /* Where to stop matching; if both the place to start and
5491 the place to stop matching are in the same string, then
5492 set to the place to stop, otherwise, for now have to use
5493 the end of the first string. */
5495 dend2
= ((FIRST_STRING_P (regstart
[regno
])
5496 == FIRST_STRING_P (regend
[regno
]))
5497 ? regend
[regno
] : end_match_1
);
5500 /* If necessary, advance to next segment in register
5504 if (dend2
== end_match_2
) break;
5505 if (dend2
== regend
[regno
]) break;
5507 /* End of string1 => advance to string2. */
5509 dend2
= regend
[regno
];
5511 /* At end of register contents => success */
5512 if (d2
== dend2
) break;
5514 /* If necessary, advance to next segment in data. */
5517 /* How many characters left in this segment to match. */
5520 /* Want how many consecutive characters we can match in
5521 one shot, so, if necessary, adjust the count. */
5522 if (mcnt
> dend2
- d2
)
5525 /* Compare that many; failure if mismatch, else move
5527 if (RE_TRANSLATE_P (translate
)
5528 ? bcmp_translate (d
, d2
, mcnt
, translate
, multibyte
)
5529 : memcmp (d
, d2
, mcnt
))
5534 d
+= mcnt
, d2
+= mcnt
;
5540 /* begline matches the empty string at the beginning of the string
5541 (unless `not_bol' is set in `bufp'), and after newlines. */
5543 DEBUG_PRINT1 ("EXECUTING begline.\n");
5545 if (AT_STRINGS_BEG (d
))
5547 if (!bufp
->not_bol
) break;
5552 GET_CHAR_BEFORE_2 (c
, d
, string1
, end1
, string2
, end2
);
5556 /* In all other cases, we fail. */
5560 /* endline is the dual of begline. */
5562 DEBUG_PRINT1 ("EXECUTING endline.\n");
5564 if (AT_STRINGS_END (d
))
5566 if (!bufp
->not_eol
) break;
5570 PREFETCH_NOLIMIT ();
5577 /* Match at the very beginning of the data. */
5579 DEBUG_PRINT1 ("EXECUTING begbuf.\n");
5580 if (AT_STRINGS_BEG (d
))
5585 /* Match at the very end of the data. */
5587 DEBUG_PRINT1 ("EXECUTING endbuf.\n");
5588 if (AT_STRINGS_END (d
))
5593 /* on_failure_keep_string_jump is used to optimize `.*\n'. It
5594 pushes NULL as the value for the string on the stack. Then
5595 `POP_FAILURE_POINT' will keep the current value for the
5596 string, instead of restoring it. To see why, consider
5597 matching `foo\nbar' against `.*\n'. The .* matches the foo;
5598 then the . fails against the \n. But the next thing we want
5599 to do is match the \n against the \n; if we restored the
5600 string value, we would be back at the foo.
5602 Because this is used only in specific cases, we don't need to
5603 check all the things that `on_failure_jump' does, to make
5604 sure the right things get saved on the stack. Hence we don't
5605 share its code. The only reason to push anything on the
5606 stack at all is that otherwise we would have to change
5607 `anychar's code to do something besides goto fail in this
5608 case; that seems worse than this. */
5609 case on_failure_keep_string_jump
:
5610 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5611 DEBUG_PRINT3 ("EXECUTING on_failure_keep_string_jump %d (to %p):\n",
5614 PUSH_FAILURE_POINT (p
- 3, NULL
);
5617 /* A nasty loop is introduced by the non-greedy *? and +?.
5618 With such loops, the stack only ever contains one failure point
5619 at a time, so that a plain on_failure_jump_loop kind of
5620 cycle detection cannot work. Worse yet, such a detection
5621 can not only fail to detect a cycle, but it can also wrongly
5622 detect a cycle (between different instantiations of the same
5624 So the method used for those nasty loops is a little different:
5625 We use a special cycle-detection-stack-frame which is pushed
5626 when the on_failure_jump_nastyloop failure-point is *popped*.
5627 This special frame thus marks the beginning of one iteration
5628 through the loop and we can hence easily check right here
5629 whether something matched between the beginning and the end of
5631 case on_failure_jump_nastyloop
:
5632 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5633 DEBUG_PRINT3 ("EXECUTING on_failure_jump_nastyloop %d (to %p):\n",
5636 assert ((re_opcode_t
)p
[-4] == no_op
);
5639 CHECK_INFINITE_LOOP (p
- 4, d
);
5641 /* If there's a cycle, just continue without pushing
5642 this failure point. The failure point is the "try again"
5643 option, which shouldn't be tried.
5644 We want (x?)*?y\1z to match both xxyz and xxyxz. */
5645 PUSH_FAILURE_POINT (p
- 3, d
);
5649 /* Simple loop detecting on_failure_jump: just check on the
5650 failure stack if the same spot was already hit earlier. */
5651 case on_failure_jump_loop
:
5653 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5654 DEBUG_PRINT3 ("EXECUTING on_failure_jump_loop %d (to %p):\n",
5658 CHECK_INFINITE_LOOP (p
- 3, d
);
5660 /* If there's a cycle, get out of the loop, as if the matching
5661 had failed. We used to just `goto fail' here, but that was
5662 aborting the search a bit too early: we want to keep the
5663 empty-loop-match and keep matching after the loop.
5664 We want (x?)*y\1z to match both xxyz and xxyxz. */
5667 PUSH_FAILURE_POINT (p
- 3, d
);
5672 /* Uses of on_failure_jump:
5674 Each alternative starts with an on_failure_jump that points
5675 to the beginning of the next alternative. Each alternative
5676 except the last ends with a jump that in effect jumps past
5677 the rest of the alternatives. (They really jump to the
5678 ending jump of the following alternative, because tensioning
5679 these jumps is a hassle.)
5681 Repeats start with an on_failure_jump that points past both
5682 the repetition text and either the following jump or
5683 pop_failure_jump back to this on_failure_jump. */
5684 case on_failure_jump
:
5685 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5686 DEBUG_PRINT3 ("EXECUTING on_failure_jump %d (to %p):\n",
5689 PUSH_FAILURE_POINT (p
-3, d
);
5692 /* This operation is used for greedy *.
5693 Compare the beginning of the repeat with what in the
5694 pattern follows its end. If we can establish that there
5695 is nothing that they would both match, i.e., that we
5696 would have to backtrack because of (as in, e.g., `a*a')
5697 then we can use a non-backtracking loop based on
5698 on_failure_keep_string_jump instead of on_failure_jump. */
5699 case on_failure_jump_smart
:
5700 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5701 DEBUG_PRINT3 ("EXECUTING on_failure_jump_smart %d (to %p).\n",
5704 re_char
*p1
= p
; /* Next operation. */
5705 /* Here, we discard `const', making re_match non-reentrant. */
5706 unsigned char *p2
= (unsigned char*) p
+ mcnt
; /* Jump dest. */
5707 unsigned char *p3
= (unsigned char*) p
- 3; /* opcode location. */
5709 p
-= 3; /* Reset so that we will re-execute the
5710 instruction once it's been changed. */
5712 EXTRACT_NUMBER (mcnt
, p2
- 2);
5714 /* Ensure this is a indeed the trivial kind of loop
5715 we are expecting. */
5716 assert (skip_one_char (p1
) == p2
- 3);
5717 assert ((re_opcode_t
) p2
[-3] == jump
&& p2
+ mcnt
== p
);
5718 DEBUG_STATEMENT (debug
+= 2);
5719 if (mutually_exclusive_p (bufp
, p1
, p2
))
5721 /* Use a fast `on_failure_keep_string_jump' loop. */
5722 DEBUG_PRINT1 (" smart exclusive => fast loop.\n");
5723 *p3
= (unsigned char) on_failure_keep_string_jump
;
5724 STORE_NUMBER (p2
- 2, mcnt
+ 3);
5728 /* Default to a safe `on_failure_jump' loop. */
5729 DEBUG_PRINT1 (" smart default => slow loop.\n");
5730 *p3
= (unsigned char) on_failure_jump
;
5732 DEBUG_STATEMENT (debug
-= 2);
5736 /* Unconditionally jump (without popping any failure points). */
5739 IMMEDIATE_QUIT_CHECK
;
5740 EXTRACT_NUMBER_AND_INCR (mcnt
, p
); /* Get the amount to jump. */
5741 DEBUG_PRINT2 ("EXECUTING jump %d ", mcnt
);
5742 p
+= mcnt
; /* Do the jump. */
5743 DEBUG_PRINT2 ("(to %p).\n", p
);
5747 /* Have to succeed matching what follows at least n times.
5748 After that, handle like `on_failure_jump'. */
5750 /* Signedness doesn't matter since we only compare MCNT to 0. */
5751 EXTRACT_NUMBER (mcnt
, p
+ 2);
5752 DEBUG_PRINT2 ("EXECUTING succeed_n %d.\n", mcnt
);
5754 /* Originally, mcnt is how many times we HAVE to succeed. */
5757 /* Here, we discard `const', making re_match non-reentrant. */
5758 unsigned char *p2
= (unsigned char*) p
+ 2; /* counter loc. */
5761 PUSH_NUMBER (p2
, mcnt
);
5764 /* The two bytes encoding mcnt == 0 are two no_op opcodes. */
5769 /* Signedness doesn't matter since we only compare MCNT to 0. */
5770 EXTRACT_NUMBER (mcnt
, p
+ 2);
5771 DEBUG_PRINT2 ("EXECUTING jump_n %d.\n", mcnt
);
5773 /* Originally, this is how many times we CAN jump. */
5776 /* Here, we discard `const', making re_match non-reentrant. */
5777 unsigned char *p2
= (unsigned char*) p
+ 2; /* counter loc. */
5779 PUSH_NUMBER (p2
, mcnt
);
5780 goto unconditional_jump
;
5782 /* If don't have to jump any more, skip over the rest of command. */
5789 unsigned char *p2
; /* Location of the counter. */
5790 DEBUG_PRINT1 ("EXECUTING set_number_at.\n");
5792 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5793 /* Here, we discard `const', making re_match non-reentrant. */
5794 p2
= (unsigned char*) p
+ mcnt
;
5795 /* Signedness doesn't matter since we only copy MCNT's bits . */
5796 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
5797 DEBUG_PRINT3 (" Setting %p to %d.\n", p2
, mcnt
);
5798 PUSH_NUMBER (p2
, mcnt
);
5804 not = (re_opcode_t
) *(p
- 1) == notwordbound
;
5805 DEBUG_PRINT2 ("EXECUTING %swordbound.\n", not?"not":"");
5807 /* We SUCCEED (or FAIL) in one of the following cases: */
5809 /* Case 1: D is at the beginning or the end of string. */
5810 if (AT_STRINGS_BEG (d
) || AT_STRINGS_END (d
))
5814 /* C1 is the character before D, S1 is the syntax of C1, C2
5815 is the character at D, and S2 is the syntax of C2. */
5819 int offset
= PTR_TO_OFFSET (d
- 1);
5820 int charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
5821 UPDATE_SYNTAX_TABLE (charpos
);
5823 GET_CHAR_BEFORE_2 (c1
, d
, string1
, end1
, string2
, end2
);
5826 UPDATE_SYNTAX_TABLE_FORWARD (charpos
+ 1);
5828 PREFETCH_NOLIMIT ();
5829 c2
= RE_STRING_CHAR (d
, dend
- d
);
5832 if (/* Case 2: Only one of S1 and S2 is Sword. */
5833 ((s1
== Sword
) != (s2
== Sword
))
5834 /* Case 3: Both of S1 and S2 are Sword, and macro
5835 WORD_BOUNDARY_P (C1, C2) returns nonzero. */
5836 || ((s1
== Sword
) && WORD_BOUNDARY_P (c1
, c2
)))
5845 DEBUG_PRINT1 ("EXECUTING wordbeg.\n");
5847 /* We FAIL in one of the following cases: */
5849 /* Case 1: D is at the end of string. */
5850 if (AT_STRINGS_END (d
))
5854 /* C1 is the character before D, S1 is the syntax of C1, C2
5855 is the character at D, and S2 is the syntax of C2. */
5859 int offset
= PTR_TO_OFFSET (d
);
5860 int charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
5861 UPDATE_SYNTAX_TABLE (charpos
);
5864 c2
= RE_STRING_CHAR (d
, dend
- d
);
5867 /* Case 2: S2 is not Sword. */
5871 /* Case 3: D is not at the beginning of string ... */
5872 if (!AT_STRINGS_BEG (d
))
5874 GET_CHAR_BEFORE_2 (c1
, d
, string1
, end1
, string2
, end2
);
5876 UPDATE_SYNTAX_TABLE_BACKWARD (charpos
- 1);
5880 /* ... and S1 is Sword, and WORD_BOUNDARY_P (C1, C2)
5882 if ((s1
== Sword
) && !WORD_BOUNDARY_P (c1
, c2
))
5889 DEBUG_PRINT1 ("EXECUTING wordend.\n");
5891 /* We FAIL in one of the following cases: */
5893 /* Case 1: D is at the beginning of string. */
5894 if (AT_STRINGS_BEG (d
))
5898 /* C1 is the character before D, S1 is the syntax of C1, C2
5899 is the character at D, and S2 is the syntax of C2. */
5903 int offset
= PTR_TO_OFFSET (d
) - 1;
5904 int charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
5905 UPDATE_SYNTAX_TABLE (charpos
);
5907 GET_CHAR_BEFORE_2 (c1
, d
, string1
, end1
, string2
, end2
);
5910 /* Case 2: S1 is not Sword. */
5914 /* Case 3: D is not at the end of string ... */
5915 if (!AT_STRINGS_END (d
))
5917 PREFETCH_NOLIMIT ();
5918 c2
= RE_STRING_CHAR (d
, dend
- d
);
5920 UPDATE_SYNTAX_TABLE_FORWARD (charpos
+ 1);
5924 /* ... and S2 is Sword, and WORD_BOUNDARY_P (C1, C2)
5926 if ((s2
== Sword
) && !WORD_BOUNDARY_P (c1
, c2
))
5933 DEBUG_PRINT1 ("EXECUTING symbeg.\n");
5935 /* We FAIL in one of the following cases: */
5937 /* Case 1: D is at the end of string. */
5938 if (AT_STRINGS_END (d
))
5942 /* C1 is the character before D, S1 is the syntax of C1, C2
5943 is the character at D, and S2 is the syntax of C2. */
5947 int offset
= PTR_TO_OFFSET (d
);
5948 int charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
5949 UPDATE_SYNTAX_TABLE (charpos
);
5952 c2
= RE_STRING_CHAR (d
, dend
- d
);
5955 /* Case 2: S2 is neither Sword nor Ssymbol. */
5956 if (s2
!= Sword
&& s2
!= Ssymbol
)
5959 /* Case 3: D is not at the beginning of string ... */
5960 if (!AT_STRINGS_BEG (d
))
5962 GET_CHAR_BEFORE_2 (c1
, d
, string1
, end1
, string2
, end2
);
5964 UPDATE_SYNTAX_TABLE_BACKWARD (charpos
- 1);
5968 /* ... and S1 is Sword or Ssymbol. */
5969 if (s1
== Sword
|| s1
== Ssymbol
)
5976 DEBUG_PRINT1 ("EXECUTING symend.\n");
5978 /* We FAIL in one of the following cases: */
5980 /* Case 1: D is at the beginning of string. */
5981 if (AT_STRINGS_BEG (d
))
5985 /* C1 is the character before D, S1 is the syntax of C1, C2
5986 is the character at D, and S2 is the syntax of C2. */
5990 int offset
= PTR_TO_OFFSET (d
) - 1;
5991 int charpos
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
5992 UPDATE_SYNTAX_TABLE (charpos
);
5994 GET_CHAR_BEFORE_2 (c1
, d
, string1
, end1
, string2
, end2
);
5997 /* Case 2: S1 is neither Ssymbol nor Sword. */
5998 if (s1
!= Sword
&& s1
!= Ssymbol
)
6001 /* Case 3: D is not at the end of string ... */
6002 if (!AT_STRINGS_END (d
))
6004 PREFETCH_NOLIMIT ();
6005 c2
= RE_STRING_CHAR (d
, dend
- d
);
6007 UPDATE_SYNTAX_TABLE_FORWARD (charpos
+ 1);
6011 /* ... and S2 is Sword or Ssymbol. */
6012 if (s2
== Sword
|| s2
== Ssymbol
)
6020 not = (re_opcode_t
) *(p
- 1) == notsyntaxspec
;
6022 DEBUG_PRINT3 ("EXECUTING %ssyntaxspec %d.\n", not?"not":"", mcnt
);
6026 int offset
= PTR_TO_OFFSET (d
);
6027 int pos1
= SYNTAX_TABLE_BYTE_TO_CHAR (offset
);
6028 UPDATE_SYNTAX_TABLE (pos1
);
6035 c
= RE_STRING_CHAR_AND_LENGTH (d
, dend
- d
, len
);
6037 if ((SYNTAX (c
) != (enum syntaxcode
) mcnt
) ^ not)
6045 DEBUG_PRINT1 ("EXECUTING before_dot.\n");
6046 if (PTR_BYTE_POS (d
) >= PT_BYTE
)
6051 DEBUG_PRINT1 ("EXECUTING at_dot.\n");
6052 if (PTR_BYTE_POS (d
) != PT_BYTE
)
6057 DEBUG_PRINT1 ("EXECUTING after_dot.\n");
6058 if (PTR_BYTE_POS (d
) <= PT_BYTE
)
6063 case notcategoryspec
:
6064 not = (re_opcode_t
) *(p
- 1) == notcategoryspec
;
6066 DEBUG_PRINT3 ("EXECUTING %scategoryspec %d.\n", not?"not":"", mcnt
);
6072 c
= RE_STRING_CHAR_AND_LENGTH (d
, dend
- d
, len
);
6074 if ((!CHAR_HAS_CATEGORY (c
, mcnt
)) ^ not)
6085 continue; /* Successfully executed one pattern command; keep going. */
6088 /* We goto here if a matching operation fails. */
6090 IMMEDIATE_QUIT_CHECK
;
6091 if (!FAIL_STACK_EMPTY ())
6094 /* A restart point is known. Restore to that state. */
6095 DEBUG_PRINT1 ("\nFAIL:\n");
6096 POP_FAILURE_POINT (str
, pat
);
6097 switch (SWITCH_ENUM_CAST ((re_opcode_t
) *pat
++))
6099 case on_failure_keep_string_jump
:
6100 assert (str
== NULL
);
6101 goto continue_failure_jump
;
6103 case on_failure_jump_nastyloop
:
6104 assert ((re_opcode_t
)pat
[-2] == no_op
);
6105 PUSH_FAILURE_POINT (pat
- 2, str
);
6108 case on_failure_jump_loop
:
6109 case on_failure_jump
:
6112 continue_failure_jump
:
6113 EXTRACT_NUMBER_AND_INCR (mcnt
, pat
);
6118 /* A special frame used for nastyloops. */
6125 assert (p
>= bufp
->buffer
&& p
<= pend
);
6127 if (d
>= string1
&& d
<= end1
)
6131 break; /* Matching at this starting point really fails. */
6135 goto restore_best_regs
;
6139 return -1; /* Failure to match. */
6142 /* Subroutine definitions for re_match_2. */
6144 /* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
6145 bytes; nonzero otherwise. */
6148 bcmp_translate (s1
, s2
, len
, translate
, multibyte
)
6151 RE_TRANSLATE_TYPE translate
;
6152 const int multibyte
;
6154 register re_char
*p1
= s1
, *p2
= s2
;
6155 re_char
*p1_end
= s1
+ len
;
6156 re_char
*p2_end
= s2
+ len
;
6158 /* FIXME: Checking both p1 and p2 presumes that the two strings might have
6159 different lengths, but relying on a single `len' would break this. -sm */
6160 while (p1
< p1_end
&& p2
< p2_end
)
6162 int p1_charlen
, p2_charlen
;
6163 re_wchar_t p1_ch
, p2_ch
;
6165 p1_ch
= RE_STRING_CHAR_AND_LENGTH (p1
, p1_end
- p1
, p1_charlen
);
6166 p2_ch
= RE_STRING_CHAR_AND_LENGTH (p2
, p2_end
- p2
, p2_charlen
);
6168 if (RE_TRANSLATE (translate
, p1_ch
)
6169 != RE_TRANSLATE (translate
, p2_ch
))
6172 p1
+= p1_charlen
, p2
+= p2_charlen
;
6175 if (p1
!= p1_end
|| p2
!= p2_end
)
6181 /* Entry points for GNU code. */
6183 /* re_compile_pattern is the GNU regular expression compiler: it
6184 compiles PATTERN (of length SIZE) and puts the result in BUFP.
6185 Returns 0 if the pattern was valid, otherwise an error string.
6187 Assumes the `allocated' (and perhaps `buffer') and `translate' fields
6188 are set in BUFP on entry.
6190 We call regex_compile to do the actual compilation. */
6193 re_compile_pattern (pattern
, length
, bufp
)
6194 const char *pattern
;
6196 struct re_pattern_buffer
*bufp
;
6200 /* GNU code is written to assume at least RE_NREGS registers will be set
6201 (and at least one extra will be -1). */
6202 bufp
->regs_allocated
= REGS_UNALLOCATED
;
6204 /* And GNU code determines whether or not to get register information
6205 by passing null for the REGS argument to re_match, etc., not by
6209 ret
= regex_compile ((re_char
*) pattern
, length
, re_syntax_options
, bufp
);
6213 return gettext (re_error_msgid
[(int) ret
]);
6215 WEAK_ALIAS (__re_compile_pattern
, re_compile_pattern
)
6217 /* Entry points compatible with 4.2 BSD regex library. We don't define
6218 them unless specifically requested. */
6220 #if defined _REGEX_RE_COMP || defined _LIBC
6222 /* BSD has one and only one pattern buffer. */
6223 static struct re_pattern_buffer re_comp_buf
;
6227 /* Make these definitions weak in libc, so POSIX programs can redefine
6228 these names if they don't use our functions, and still use
6229 regcomp/regexec below without link errors. */
6239 if (!re_comp_buf
.buffer
)
6240 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6241 return (char *) gettext ("No previous regular expression");
6245 if (!re_comp_buf
.buffer
)
6247 re_comp_buf
.buffer
= (unsigned char *) malloc (200);
6248 if (re_comp_buf
.buffer
== NULL
)
6249 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6250 return (char *) gettext (re_error_msgid
[(int) REG_ESPACE
]);
6251 re_comp_buf
.allocated
= 200;
6253 re_comp_buf
.fastmap
= (char *) malloc (1 << BYTEWIDTH
);
6254 if (re_comp_buf
.fastmap
== NULL
)
6255 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6256 return (char *) gettext (re_error_msgid
[(int) REG_ESPACE
]);
6259 /* Since `re_exec' always passes NULL for the `regs' argument, we
6260 don't need to initialize the pattern buffer fields which affect it. */
6262 ret
= regex_compile (s
, strlen (s
), re_syntax_options
, &re_comp_buf
);
6267 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6268 return (char *) gettext (re_error_msgid
[(int) ret
]);
6279 const int len
= strlen (s
);
6281 0 <= re_search (&re_comp_buf
, s
, len
, 0, len
, (struct re_registers
*) 0);
6283 #endif /* _REGEX_RE_COMP */
6285 /* POSIX.2 functions. Don't define these for Emacs. */
6289 /* regcomp takes a regular expression as a string and compiles it.
6291 PREG is a regex_t *. We do not expect any fields to be initialized,
6292 since POSIX says we shouldn't. Thus, we set
6294 `buffer' to the compiled pattern;
6295 `used' to the length of the compiled pattern;
6296 `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
6297 REG_EXTENDED bit in CFLAGS is set; otherwise, to
6298 RE_SYNTAX_POSIX_BASIC;
6299 `fastmap' to an allocated space for the fastmap;
6300 `fastmap_accurate' to zero;
6301 `re_nsub' to the number of subexpressions in PATTERN.
6303 PATTERN is the address of the pattern string.
6305 CFLAGS is a series of bits which affect compilation.
6307 If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
6308 use POSIX basic syntax.
6310 If REG_NEWLINE is set, then . and [^...] don't match newline.
6311 Also, regexec will try a match beginning after every newline.
6313 If REG_ICASE is set, then we considers upper- and lowercase
6314 versions of letters to be equivalent when matching.
6316 If REG_NOSUB is set, then when PREG is passed to regexec, that
6317 routine will report only success or failure, and nothing about the
6320 It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
6321 the return codes and their meanings.) */
6324 regcomp (preg
, pattern
, cflags
)
6325 regex_t
*__restrict preg
;
6326 const char *__restrict pattern
;
6331 = (cflags
& REG_EXTENDED
) ?
6332 RE_SYNTAX_POSIX_EXTENDED
: RE_SYNTAX_POSIX_BASIC
;
6334 /* regex_compile will allocate the space for the compiled pattern. */
6336 preg
->allocated
= 0;
6339 /* Try to allocate space for the fastmap. */
6340 preg
->fastmap
= (char *) malloc (1 << BYTEWIDTH
);
6342 if (cflags
& REG_ICASE
)
6347 = (RE_TRANSLATE_TYPE
) malloc (CHAR_SET_SIZE
6348 * sizeof (*(RE_TRANSLATE_TYPE
)0));
6349 if (preg
->translate
== NULL
)
6350 return (int) REG_ESPACE
;
6352 /* Map uppercase characters to corresponding lowercase ones. */
6353 for (i
= 0; i
< CHAR_SET_SIZE
; i
++)
6354 preg
->translate
[i
] = ISUPPER (i
) ? TOLOWER (i
) : i
;
6357 preg
->translate
= NULL
;
6359 /* If REG_NEWLINE is set, newlines are treated differently. */
6360 if (cflags
& REG_NEWLINE
)
6361 { /* REG_NEWLINE implies neither . nor [^...] match newline. */
6362 syntax
&= ~RE_DOT_NEWLINE
;
6363 syntax
|= RE_HAT_LISTS_NOT_NEWLINE
;
6366 syntax
|= RE_NO_NEWLINE_ANCHOR
;
6368 preg
->no_sub
= !!(cflags
& REG_NOSUB
);
6370 /* POSIX says a null character in the pattern terminates it, so we
6371 can use strlen here in compiling the pattern. */
6372 ret
= regex_compile ((re_char
*) pattern
, strlen (pattern
), syntax
, preg
);
6374 /* POSIX doesn't distinguish between an unmatched open-group and an
6375 unmatched close-group: both are REG_EPAREN. */
6376 if (ret
== REG_ERPAREN
)
6379 if (ret
== REG_NOERROR
&& preg
->fastmap
)
6380 { /* Compute the fastmap now, since regexec cannot modify the pattern
6382 re_compile_fastmap (preg
);
6383 if (preg
->can_be_null
)
6384 { /* The fastmap can't be used anyway. */
6385 free (preg
->fastmap
);
6386 preg
->fastmap
= NULL
;
6391 WEAK_ALIAS (__regcomp
, regcomp
)
6394 /* regexec searches for a given pattern, specified by PREG, in the
6397 If NMATCH is zero or REG_NOSUB was set in the cflags argument to
6398 `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
6399 least NMATCH elements, and we set them to the offsets of the
6400 corresponding matched substrings.
6402 EFLAGS specifies `execution flags' which affect matching: if
6403 REG_NOTBOL is set, then ^ does not match at the beginning of the
6404 string; if REG_NOTEOL is set, then $ does not match at the end.
6406 We return 0 if we find a match and REG_NOMATCH if not. */
6409 regexec (preg
, string
, nmatch
, pmatch
, eflags
)
6410 const regex_t
*__restrict preg
;
6411 const char *__restrict string
;
6413 regmatch_t pmatch
[__restrict_arr
];
6417 struct re_registers regs
;
6418 regex_t private_preg
;
6419 int len
= strlen (string
);
6420 boolean want_reg_info
= !preg
->no_sub
&& nmatch
> 0 && pmatch
;
6422 private_preg
= *preg
;
6424 private_preg
.not_bol
= !!(eflags
& REG_NOTBOL
);
6425 private_preg
.not_eol
= !!(eflags
& REG_NOTEOL
);
6427 /* The user has told us exactly how many registers to return
6428 information about, via `nmatch'. We have to pass that on to the
6429 matching routines. */
6430 private_preg
.regs_allocated
= REGS_FIXED
;
6434 regs
.num_regs
= nmatch
;
6435 regs
.start
= TALLOC (nmatch
* 2, regoff_t
);
6436 if (regs
.start
== NULL
)
6437 return (int) REG_NOMATCH
;
6438 regs
.end
= regs
.start
+ nmatch
;
6441 /* Instead of using not_eol to implement REG_NOTEOL, we could simply
6442 pass (&private_preg, string, len + 1, 0, len, ...) pretending the string
6443 was a little bit longer but still only matching the real part.
6444 This works because the `endline' will check for a '\n' and will find a
6445 '\0', correctly deciding that this is not the end of a line.
6446 But it doesn't work out so nicely for REG_NOTBOL, since we don't have
6447 a convenient '\0' there. For all we know, the string could be preceded
6448 by '\n' which would throw things off. */
6450 /* Perform the searching operation. */
6451 ret
= re_search (&private_preg
, string
, len
,
6452 /* start: */ 0, /* range: */ len
,
6453 want_reg_info
? ®s
: (struct re_registers
*) 0);
6455 /* Copy the register information to the POSIX structure. */
6462 for (r
= 0; r
< nmatch
; r
++)
6464 pmatch
[r
].rm_so
= regs
.start
[r
];
6465 pmatch
[r
].rm_eo
= regs
.end
[r
];
6469 /* If we needed the temporary register info, free the space now. */
6473 /* We want zero return to mean success, unlike `re_search'. */
6474 return ret
>= 0 ? (int) REG_NOERROR
: (int) REG_NOMATCH
;
6476 WEAK_ALIAS (__regexec
, regexec
)
6479 /* Returns a message corresponding to an error code, ERRCODE, returned
6480 from either regcomp or regexec. We don't use PREG here. */
6483 regerror (errcode
, preg
, errbuf
, errbuf_size
)
6485 const regex_t
*preg
;
6493 || errcode
>= (sizeof (re_error_msgid
) / sizeof (re_error_msgid
[0])))
6494 /* Only error codes returned by the rest of the code should be passed
6495 to this routine. If we are given anything else, or if other regex
6496 code generates an invalid error code, then the program has a bug.
6497 Dump core so we can fix it. */
6500 msg
= gettext (re_error_msgid
[errcode
]);
6502 msg_size
= strlen (msg
) + 1; /* Includes the null. */
6504 if (errbuf_size
!= 0)
6506 if (msg_size
> errbuf_size
)
6508 strncpy (errbuf
, msg
, errbuf_size
- 1);
6509 errbuf
[errbuf_size
- 1] = 0;
6512 strcpy (errbuf
, msg
);
6517 WEAK_ALIAS (__regerror
, regerror
)
6520 /* Free dynamically allocated space used by PREG. */
6526 if (preg
->buffer
!= NULL
)
6527 free (preg
->buffer
);
6528 preg
->buffer
= NULL
;
6530 preg
->allocated
= 0;
6533 if (preg
->fastmap
!= NULL
)
6534 free (preg
->fastmap
);
6535 preg
->fastmap
= NULL
;
6536 preg
->fastmap_accurate
= 0;
6538 if (preg
->translate
!= NULL
)
6539 free (preg
->translate
);
6540 preg
->translate
= NULL
;
6542 WEAK_ALIAS (__regfree
, regfree
)
6544 #endif /* not emacs */
6546 /* arch-tag: 4ffd68ba-2a9e-435b-a21a-018990f9eeb2
6547 (do not change this comment) */