1 /* Extended regular expression matching and search library, version
2 0.12. (Implements POSIX draft P10003.2/D11.2, except for
3 internationalization features.)
5 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
22 /* AIX requires this to be the first thing in the file. */
23 #if defined (_AIX) && !defined (REGEX_MALLOC)
31 /* Converts the pointer to the char to BEG-based offset from the start. */
32 #define PTR_TO_OFFSET(d) \
33 POS_AS_IN_BUFFER (MATCHING_IN_FIRST_STRING \
34 ? (d) - string1 : (d) - (string2 - size1))
35 #define POS_AS_IN_BUFFER(p) ((p) + (NILP (re_match_object) || BUFFERP (re_match_object)))
37 #define PTR_TO_OFFSET(d) 0
44 /* We need this for `regex.h', and perhaps for the Emacs include files. */
45 #include <sys/types.h>
47 /* This is for other GNU distributions with internationalized messages. */
48 #if HAVE_LIBINTL_H || defined (_LIBC)
51 # define gettext(msgid) (msgid)
55 /* This define is so xgettext can find the internationalizable
57 #define gettext_noop(String) String
60 /* The `emacs' switch turns on certain matching commands
61 that make sense only in Emacs. */
67 /* Make syntax table lookup grant data in gl_state. */
68 #define SYNTAX_ENTRY_VIA_PROPERTY
74 #define malloc xmalloc
75 #define realloc xrealloc
80 /* If we are not linking with Emacs proper,
81 we can't use the relocating allocator
82 even if config.h says that we can. */
85 #if defined (STDC_HEADERS) || defined (_LIBC)
92 /* When used in Emacs's lib-src, we need to get bzero and bcopy somehow.
93 If nothing else has been done, use the method below. */
94 #ifdef INHIBIT_STRING_HEADER
95 #if !(defined (HAVE_BZERO) && defined (HAVE_BCOPY))
96 #if !defined (bzero) && !defined (bcopy)
97 #undef INHIBIT_STRING_HEADER
102 /* This is the normal way of making sure we have a bcopy and a bzero.
103 This is used in most programs--a few other programs avoid this
104 by defining INHIBIT_STRING_HEADER. */
105 #ifndef INHIBIT_STRING_HEADER
106 #if defined (HAVE_STRING_H) || defined (STDC_HEADERS) || defined (_LIBC)
109 #define bcmp(s1, s2, n) memcmp ((s1), (s2), (n))
112 #define bcopy(s, d, n) memcpy ((d), (s), (n))
115 #define bzero(s, n) memset ((s), 0, (n))
122 /* Define the syntax stuff for \<, \>, etc. */
124 /* This must be nonzero for the wordchar and notwordchar pattern
125 commands in re_match_2. */
130 #ifdef SWITCH_ENUM_BUG
131 #define SWITCH_ENUM_CAST(x) ((int)(x))
133 #define SWITCH_ENUM_CAST(x) (x)
138 extern char *re_syntax_table;
140 #else /* not SYNTAX_TABLE */
142 /* How many characters in the character set. */
143 #define CHAR_SET_SIZE 256
145 static char re_syntax_table[CHAR_SET_SIZE];
156 bzero (re_syntax_table, sizeof re_syntax_table);
158 for (c = 'a'; c <= 'z'; c++)
159 re_syntax_table[c] = Sword;
161 for (c = 'A'; c <= 'Z'; c++)
162 re_syntax_table[c] = Sword;
164 for (c = '0'; c <= '9'; c++)
165 re_syntax_table[c] = Sword;
167 re_syntax_table['_'] = Sword;
172 #endif /* not SYNTAX_TABLE */
174 #define SYNTAX(c) re_syntax_table[c]
176 /* Dummy macros for non-Emacs environments. */
177 #define BASE_LEADING_CODE_P(c) (0)
178 #define WORD_BOUNDARY_P(c1, c2) (0)
179 #define CHAR_HEAD_P(p) (1)
180 #define SINGLE_BYTE_CHAR_P(c) (1)
181 #define SAME_CHARSET_P(c1, c2) (1)
182 #define MULTIBYTE_FORM_LENGTH(p, s) (1)
183 #define STRING_CHAR(p, s) (*(p))
184 #define STRING_CHAR_AND_LENGTH(p, s, actual_len) ((actual_len) = 1, *(p))
185 #define GET_CHAR_AFTER_2(c, p, str1, end1, str2, end2) \
186 (c = ((p) == (end1) ? *(str2) : *(p)))
187 #define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2) \
188 (c = ((p) == (str2) ? *((end1) - 1) : *((p) - 1)))
189 #endif /* not emacs */
191 /* Get the interface, including the syntax bits. */
194 /* Jim Meyering writes:
196 "... Some ctype macros are valid only for character codes that
197 isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
198 using /bin/cc or gcc but without giving an ansi option). So, all
199 ctype uses should be through macros like ISPRINT... If
200 STDC_HEADERS is defined, then autoconf has verified that the ctype
201 macros don't need to be guarded with references to isascii. ...
202 Defining isascii to 1 should let any compiler worth its salt
203 eliminate the && through constant folding." */
205 #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
208 #define ISASCII(c) isascii(c)
211 /* isalpha etc. are used for the character classes. */
214 /* In Emacs, these are only used for single-byte characters. */
215 #define ISDIGIT(c) (ISASCII (c) && isdigit (c))
216 #define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
217 #define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
221 /* This is only used for single-byte characters. */
222 #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
224 /* The rest must handle multibyte characters. */
226 #define ISGRAPH(c) (SINGLE_BYTE_CHAR_P (c) \
227 ? ISASCII (c) && isprint (c) && !isspace (c) \
230 #define ISPRINT(c) (SINGLE_BYTE_CHAR_P (c) \
231 ? ISASCII (c) && isalnum (c) \
234 #define ISALNUM(c) (SINGLE_BYTE_CHAR_P (c) \
235 ? ISASCII (c) && isalnum (c) \
236 : SYNTAX (c) == Sword)
238 #define ISALPHA(c) (SINGLE_BYTE_CHAR_P (c) \
239 ? ISASCII (c) && isalpha (c) \
240 : SYNTAX (c) == Sword)
242 #define ISLOWER(c) (LOWERCASEP (c))
244 #define ISPUNCT(c) (SINGLE_BYTE_CHAR_P (c) \
245 ? ISASCII (c) && ispunct (c) \
246 : SYNTAX (c) != Sword)
248 #define ISSPACE(c) (SYNTAX (c) == Swhitespace)
250 #define ISUPPER(c) (UPPERCASEP (c))
252 #define ISWORD(c) (SYNTAX (c) == Sword)
254 #else /* not emacs */
257 #define ISBLANK(c) (ISASCII (c) && isblank (c))
259 #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
262 #define ISGRAPH(c) (ISASCII (c) && isgraph (c))
264 #define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
267 #define ISPRINT(c) (ISASCII (c) && isprint (c))
268 #define ISDIGIT(c) (ISASCII (c) && isdigit (c))
269 #define ISALNUM(c) (ISASCII (c) && isalnum (c))
270 #define ISALPHA(c) (ISASCII (c) && isalpha (c))
271 #define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
272 #define ISLOWER(c) (ISASCII (c) && islower (c))
273 #define ISPUNCT(c) (ISASCII (c) && ispunct (c))
274 #define ISSPACE(c) (ISASCII (c) && isspace (c))
275 #define ISUPPER(c) (ISASCII (c) && isupper (c))
276 #define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
278 #define ISWORD(c) ISALPHA(c)
280 #endif /* not emacs */
283 #define NULL (void *)0
286 /* We remove any previous definition of `SIGN_EXTEND_CHAR',
287 since ours (we hope) works properly with all combinations of
288 machines, compilers, `char' and `unsigned char' argument types.
289 (Per Bothner suggested the basic approach.) */
290 #undef SIGN_EXTEND_CHAR
292 #define SIGN_EXTEND_CHAR(c) ((signed char) (c))
293 #else /* not __STDC__ */
294 /* As in Harbison and Steele. */
295 #define SIGN_EXTEND_CHAR(c) ((((unsigned char) (c)) ^ 128) - 128)
298 /* Should we use malloc or alloca? If REGEX_MALLOC is not defined, we
299 use `alloca' instead of `malloc'. This is because using malloc in
300 re_search* or re_match* could cause memory leaks when C-g is used in
301 Emacs; also, malloc is slower and causes storage fragmentation. On
302 the other hand, malloc is more portable, and easier to debug.
304 Because we sometimes use alloca, some routines have to be macros,
305 not functions -- `alloca'-allocated space disappears at the end of the
306 function it is called in. */
310 #define REGEX_ALLOCATE malloc
311 #define REGEX_REALLOCATE(source, osize, nsize) realloc (source, nsize)
312 #define REGEX_FREE free
314 #else /* not REGEX_MALLOC */
316 /* Emacs already defines alloca, sometimes. */
319 /* Make alloca work the best possible way. */
321 #define alloca __builtin_alloca
322 #else /* not __GNUC__ */
325 #else /* not __GNUC__ or HAVE_ALLOCA_H */
326 #if 0 /* It is a bad idea to declare alloca. We always cast the result. */
327 #ifndef _AIX /* Already did AIX, up at the top. */
329 #endif /* not _AIX */
331 #endif /* not HAVE_ALLOCA_H */
332 #endif /* not __GNUC__ */
334 #endif /* not alloca */
336 #define REGEX_ALLOCATE alloca
338 /* Assumes a `char *destination' variable. */
339 #define REGEX_REALLOCATE(source, osize, nsize) \
340 (destination = (char *) alloca (nsize), \
341 bcopy (source, destination, osize), \
344 /* No need to do anything to free, after alloca. */
345 #define REGEX_FREE(arg) ((void)0) /* Do nothing! But inhibit gcc warning. */
347 #endif /* not REGEX_MALLOC */
349 /* Define how to allocate the failure stack. */
351 #if defined (REL_ALLOC) && defined (REGEX_MALLOC)
353 #define REGEX_ALLOCATE_STACK(size) \
354 r_alloc (&failure_stack_ptr, (size))
355 #define REGEX_REALLOCATE_STACK(source, osize, nsize) \
356 r_re_alloc (&failure_stack_ptr, (nsize))
357 #define REGEX_FREE_STACK(ptr) \
358 r_alloc_free (&failure_stack_ptr)
360 #else /* not using relocating allocator */
364 #define REGEX_ALLOCATE_STACK malloc
365 #define REGEX_REALLOCATE_STACK(source, osize, nsize) realloc (source, nsize)
366 #define REGEX_FREE_STACK free
368 #else /* not REGEX_MALLOC */
370 #define REGEX_ALLOCATE_STACK alloca
372 #define REGEX_REALLOCATE_STACK(source, osize, nsize) \
373 REGEX_REALLOCATE (source, osize, nsize)
374 /* No need to explicitly free anything. */
375 #define REGEX_FREE_STACK(arg)
377 #endif /* not REGEX_MALLOC */
378 #endif /* not using relocating allocator */
381 /* True if `size1' is non-NULL and PTR is pointing anywhere inside
382 `string1' or just past its end. This works if PTR is NULL, which is
384 #define FIRST_STRING_P(ptr) \
385 (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
387 /* (Re)Allocate N items of type T using malloc, or fail. */
388 #define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t)))
389 #define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
390 #define RETALLOC_IF(addr, n, t) \
391 if (addr) RETALLOC((addr), (n), t); else (addr) = TALLOC ((n), t)
392 #define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
394 #define BYTEWIDTH 8 /* In bits. */
396 #define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
400 #define MAX(a, b) ((a) > (b) ? (a) : (b))
401 #define MIN(a, b) ((a) < (b) ? (a) : (b))
403 typedef char boolean;
407 static int re_match_2_internal ();
409 /* These are the command codes that appear in compiled regular
410 expressions. Some opcodes are followed by argument bytes. A
411 command code can specify any interpretation whatsoever for its
412 arguments. Zero bytes may appear in the compiled regular expression. */
418 /* Succeed right away--no more backtracking. */
421 /* Followed by one byte giving n, then by n literal bytes. */
424 /* Matches any (more or less) character. */
427 /* Matches any one char belonging to specified set. First
428 following byte is number of bitmap bytes. Then come bytes
429 for a bitmap saying which chars are in. Bits in each byte
430 are ordered low-bit-first. A character is in the set if its
431 bit is 1. A character too large to have a bit in the map is
432 automatically not in the set.
434 If the length byte has the 0x80 bit set, then that stuff
435 is followed by a range table:
436 2 bytes of flags for character sets (low 8 bits, high 8 bits)
437 See RANGE_TABLE_WORK_BITS below.
438 2 bytes, the number of pairs that follow
439 pairs, each 2 multibyte characters,
440 each multibyte character represented as 3 bytes. */
443 /* Same parameters as charset, but match any character that is
444 not one of those specified. */
447 /* Start remembering the text that is matched, for storing in a
448 register. Followed by one byte with the register number, in
449 the range 0 to one less than the pattern buffer's re_nsub
450 field. Then followed by one byte with the number of groups
451 inner to this one. (This last has to be part of the
452 start_memory only because we need it in the on_failure_jump
456 /* Stop remembering the text that is matched and store it in a
457 memory register. Followed by one byte with the register
458 number, in the range 0 to one less than `re_nsub' in the
459 pattern buffer, and one byte with the number of inner groups,
460 just like `start_memory'. (We need the number of inner
461 groups here because we don't have any easy way of finding the
462 corresponding start_memory when we're at a stop_memory.) */
465 /* Match a duplicate of something remembered. Followed by one
466 byte containing the register number. */
469 /* Fail unless at beginning of line. */
472 /* Fail unless at end of line. */
475 /* Succeeds if at beginning of buffer (if emacs) or at beginning
476 of string to be matched (if not). */
479 /* Analogously, for end of buffer/string. */
482 /* Followed by two byte relative address to which to jump. */
485 /* Same as jump, but marks the end of an alternative. */
488 /* Followed by two-byte relative address of place to resume at
489 in case of failure. */
492 /* Like on_failure_jump, but pushes a placeholder instead of the
493 current string position when executed. */
494 on_failure_keep_string_jump,
496 /* Throw away latest failure point and then jump to following
497 two-byte relative address. */
500 /* Change to pop_failure_jump if know won't have to backtrack to
501 match; otherwise change to jump. This is used to jump
502 back to the beginning of a repeat. If what follows this jump
503 clearly won't match what the repeat does, such that we can be
504 sure that there is no use backtracking out of repetitions
505 already matched, then we change it to a pop_failure_jump.
506 Followed by two-byte address. */
509 /* Jump to following two-byte address, and push a dummy failure
510 point. This failure point will be thrown away if an attempt
511 is made to use it for a failure. A `+' construct makes this
512 before the first repeat. Also used as an intermediary kind
513 of jump when compiling an alternative. */
516 /* Push a dummy failure point and continue. Used at the end of
520 /* Followed by two-byte relative address and two-byte number n.
521 After matching N times, jump to the address upon failure. */
524 /* Followed by two-byte relative address, and two-byte number n.
525 Jump to the address N times, then fail. */
528 /* Set the following two-byte relative address to the
529 subsequent two-byte number. The address *includes* the two
533 wordchar, /* Matches any word-constituent character. */
534 notwordchar, /* Matches any char that is not a word-constituent. */
536 wordbeg, /* Succeeds if at word beginning. */
537 wordend, /* Succeeds if at word end. */
539 wordbound, /* Succeeds if at a word boundary. */
540 notwordbound /* Succeeds if not at a word boundary. */
543 ,before_dot, /* Succeeds if before point. */
544 at_dot, /* Succeeds if at point. */
545 after_dot, /* Succeeds if after point. */
547 /* Matches any character whose syntax is specified. Followed by
548 a byte which contains a syntax code, e.g., Sword. */
551 /* Matches any character whose syntax is not that specified. */
554 /* Matches any character whose category-set contains the specified
555 category. The operator is followed by a byte which contains a
556 category code (mnemonic ASCII character). */
559 /* Matches any character whose category-set does not contain the
560 specified category. The operator is followed by a byte which
561 contains the category code (mnemonic ASCII character). */
566 /* Common operations on the compiled pattern. */
568 /* Store NUMBER in two contiguous bytes starting at DESTINATION. */
570 #define STORE_NUMBER(destination, number) \
572 (destination)[0] = (number) & 0377; \
573 (destination)[1] = (number) >> 8; \
576 /* Same as STORE_NUMBER, except increment DESTINATION to
577 the byte after where the number is stored. Therefore, DESTINATION
578 must be an lvalue. */
580 #define STORE_NUMBER_AND_INCR(destination, number) \
582 STORE_NUMBER (destination, number); \
583 (destination) += 2; \
586 /* Put into DESTINATION a number stored in two contiguous bytes starting
589 #define EXTRACT_NUMBER(destination, source) \
591 (destination) = *(source) & 0377; \
592 (destination) += SIGN_EXTEND_CHAR (*((source) + 1)) << 8; \
597 extract_number (dest, source)
599 unsigned char *source;
601 int temp = SIGN_EXTEND_CHAR (*(source + 1));
602 *dest = *source & 0377;
606 #ifndef EXTRACT_MACROS /* To debug the macros. */
607 #undef EXTRACT_NUMBER
608 #define EXTRACT_NUMBER(dest, src) extract_number (&dest, src)
609 #endif /* not EXTRACT_MACROS */
613 /* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
614 SOURCE must be an lvalue. */
616 #define EXTRACT_NUMBER_AND_INCR(destination, source) \
618 EXTRACT_NUMBER (destination, source); \
624 extract_number_and_incr (destination, source)
626 unsigned char **source;
628 extract_number (destination, *source);
632 #ifndef EXTRACT_MACROS
633 #undef EXTRACT_NUMBER_AND_INCR
634 #define EXTRACT_NUMBER_AND_INCR(dest, src) \
635 extract_number_and_incr (&dest, &src)
636 #endif /* not EXTRACT_MACROS */
640 /* Store a multibyte character in three contiguous bytes starting
641 DESTINATION, and increment DESTINATION to the byte after where the
642 character is stored. Therefore, DESTINATION must be an lvalue. */
644 #define STORE_CHARACTER_AND_INCR(destination, character) \
646 (destination)[0] = (character) & 0377; \
647 (destination)[1] = ((character) >> 8) & 0377; \
648 (destination)[2] = (character) >> 16; \
649 (destination) += 3; \
652 /* Put into DESTINATION a character stored in three contiguous bytes
653 starting at SOURCE. */
655 #define EXTRACT_CHARACTER(destination, source) \
657 (destination) = ((source)[0] \
658 | ((source)[1] << 8) \
659 | ((source)[2] << 16)); \
663 /* Macros for charset. */
665 /* Size of bitmap of charset P in bytes. P is a start of charset,
666 i.e. *P is (re_opcode_t) charset or (re_opcode_t) charset_not. */
667 #define CHARSET_BITMAP_SIZE(p) ((p)[1] & 0x7F)
669 /* Nonzero if charset P has range table. */
670 #define CHARSET_RANGE_TABLE_EXISTS_P(p) ((p)[1] & 0x80)
672 /* Return the address of range table of charset P. But not the start
673 of table itself, but the before where the number of ranges is
674 stored. `2 +' means to skip re_opcode_t and size of bitmap,
675 and the 2 bytes of flags at the start of the range table. */
676 #define CHARSET_RANGE_TABLE(p) (&(p)[4 + CHARSET_BITMAP_SIZE (p)])
678 /* Extract the bit flags that start a range table. */
679 #define CHARSET_RANGE_TABLE_BITS(p) \
680 ((p)[2 + CHARSET_BITMAP_SIZE (p)] \
681 + (p)[3 + CHARSET_BITMAP_SIZE (p)] * 0x100)
683 /* Test if C is listed in the bitmap of charset P. */
684 #define CHARSET_LOOKUP_BITMAP(p, c) \
685 ((c) < CHARSET_BITMAP_SIZE (p) * BYTEWIDTH \
686 && (p)[2 + (c) / BYTEWIDTH] & (1 << ((c) % BYTEWIDTH)))
688 /* Return the address of end of RANGE_TABLE. COUNT is number of
689 ranges (which is a pair of (start, end)) in the RANGE_TABLE. `* 2'
690 is start of range and end of range. `* 3' is size of each start
692 #define CHARSET_RANGE_TABLE_END(range_table, count) \
693 ((range_table) + (count) * 2 * 3)
695 /* Test if C is in RANGE_TABLE. A flag NOT is negated if C is in.
696 COUNT is number of ranges in RANGE_TABLE. */
697 #define CHARSET_LOOKUP_RANGE_TABLE_RAW(not, c, range_table, count) \
700 int range_start, range_end; \
702 unsigned char *range_table_end \
703 = CHARSET_RANGE_TABLE_END ((range_table), (count)); \
705 for (p = (range_table); p < range_table_end; p += 2 * 3) \
707 EXTRACT_CHARACTER (range_start, p); \
708 EXTRACT_CHARACTER (range_end, p + 3); \
710 if (range_start <= (c) && (c) <= range_end) \
719 /* Test if C is in range table of CHARSET. The flag NOT is negated if
720 C is listed in it. */
721 #define CHARSET_LOOKUP_RANGE_TABLE(not, c, charset) \
724 /* Number of ranges in range table. */ \
726 unsigned char *range_table = CHARSET_RANGE_TABLE (charset); \
728 EXTRACT_NUMBER_AND_INCR (count, range_table); \
729 CHARSET_LOOKUP_RANGE_TABLE_RAW ((not), (c), range_table, count); \
733 /* If DEBUG is defined, Regex prints many voluminous messages about what
734 it is doing (if the variable `debug' is nonzero). If linked with the
735 main program in `iregex.c', you can enter patterns and strings
736 interactively. And if linked with the main program in `main.c' and
737 the other test files, you can run the already-written tests. */
741 /* We use standard I/O for debugging. */
744 /* It is useful to test things that ``must'' be true when debugging. */
747 static int debug = 0;
749 #define DEBUG_STATEMENT(e) e
750 #define DEBUG_PRINT1(x) if (debug) printf (x)
751 #define DEBUG_PRINT2(x1, x2) if (debug) printf (x1, x2)
752 #define DEBUG_PRINT3(x1, x2, x3) if (debug) printf (x1, x2, x3)
753 #define DEBUG_PRINT4(x1, x2, x3, x4) if (debug) printf (x1, x2, x3, x4)
754 #define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) \
755 if (debug) print_partial_compiled_pattern (s, e)
756 #define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \
757 if (debug) print_double_string (w, s1, sz1, s2, sz2)
760 /* Print the fastmap in human-readable form. */
763 print_fastmap (fastmap)
766 unsigned was_a_range = 0;
769 while (i < (1 << BYTEWIDTH))
775 while (i < (1 << BYTEWIDTH) && fastmap[i])
791 /* Print a compiled pattern string in human-readable form, starting at
792 the START pointer into it and ending just before the pointer END. */
795 print_partial_compiled_pattern (start, end)
796 unsigned char *start;
800 unsigned char *p = start;
801 unsigned char *pend = end;
809 /* Loop over pattern commands. */
812 printf ("%d:\t", p - start);
814 switch ((re_opcode_t) *p++)
822 printf ("/exactn/%d", mcnt);
833 printf ("/start_memory/%d/%d", mcnt, *p++);
838 printf ("/stop_memory/%d/%d", mcnt, *p++);
842 printf ("/duplicate/%d", *p++);
852 register int c, last = -100;
853 register int in_range = 0;
854 int length = *p & 0x7f;
855 int has_range_table = *p & 0x80;
856 int range_length = p[length + 2] + p[length + 3] * 0x100;
858 printf ("/charset [%s",
859 (re_opcode_t) *(p - 1) == charset_not ? "^" : "");
861 assert (p + *p < pend);
863 for (c = 0; c < 256; c++)
865 && (p[1 + (c/8)] & (1 << (c % 8))))
867 /* Are we starting a range? */
868 if (last + 1 == c && ! in_range)
873 /* Have we broken a range? */
874 else if (last + 1 != c && in_range)
894 printf ("has-range-table");
896 /* ??? Should print the range table; for now,
899 p += 4 + 6 * range_length;
911 case on_failure_jump:
912 extract_number_and_incr (&mcnt, &p);
913 printf ("/on_failure_jump to %d", p + mcnt - start);
916 case on_failure_keep_string_jump:
917 extract_number_and_incr (&mcnt, &p);
918 printf ("/on_failure_keep_string_jump to %d", p + mcnt - start);
921 case dummy_failure_jump:
922 extract_number_and_incr (&mcnt, &p);
923 printf ("/dummy_failure_jump to %d", p + mcnt - start);
926 case push_dummy_failure:
927 printf ("/push_dummy_failure");
931 extract_number_and_incr (&mcnt, &p);
932 printf ("/maybe_pop_jump to %d", p + mcnt - start);
935 case pop_failure_jump:
936 extract_number_and_incr (&mcnt, &p);
937 printf ("/pop_failure_jump to %d", p + mcnt - start);
941 extract_number_and_incr (&mcnt, &p);
942 printf ("/jump_past_alt to %d", p + mcnt - start);
946 extract_number_and_incr (&mcnt, &p);
947 printf ("/jump to %d", p + mcnt - start);
951 extract_number_and_incr (&mcnt, &p);
952 extract_number_and_incr (&mcnt2, &p);
953 printf ("/succeed_n to %d, %d times", p + mcnt - start, mcnt2);
957 extract_number_and_incr (&mcnt, &p);
958 extract_number_and_incr (&mcnt2, &p);
959 printf ("/jump_n to %d, %d times", p + mcnt - start, mcnt2);
963 extract_number_and_incr (&mcnt, &p);
964 extract_number_and_incr (&mcnt2, &p);
965 printf ("/set_number_at location %d to %d", p + mcnt - start, mcnt2);
969 printf ("/wordbound");
973 printf ("/notwordbound");
985 printf ("/before_dot");
993 printf ("/after_dot");
997 printf ("/syntaxspec");
999 printf ("/%d", mcnt);
1003 printf ("/notsyntaxspec");
1005 printf ("/%d", mcnt);
1010 printf ("/wordchar");
1014 printf ("/notwordchar");
1026 printf ("?%d", *(p-1));
1032 printf ("%d:\tend of pattern.\n", p - start);
1037 print_compiled_pattern (bufp)
1038 struct re_pattern_buffer *bufp;
1040 unsigned char *buffer = bufp->buffer;
1042 print_partial_compiled_pattern (buffer, buffer + bufp->used);
1043 printf ("%d bytes used/%d bytes allocated.\n", bufp->used, bufp->allocated);
1045 if (bufp->fastmap_accurate && bufp->fastmap)
1047 printf ("fastmap: ");
1048 print_fastmap (bufp->fastmap);
1051 printf ("re_nsub: %d\t", bufp->re_nsub);
1052 printf ("regs_alloc: %d\t", bufp->regs_allocated);
1053 printf ("can_be_null: %d\t", bufp->can_be_null);
1054 printf ("newline_anchor: %d\n", bufp->newline_anchor);
1055 printf ("no_sub: %d\t", bufp->no_sub);
1056 printf ("not_bol: %d\t", bufp->not_bol);
1057 printf ("not_eol: %d\t", bufp->not_eol);
1058 printf ("syntax: %d\n", bufp->syntax);
1059 /* Perhaps we should print the translate table? */
1064 print_double_string (where, string1, size1, string2, size2)
1066 const char *string1;
1067 const char *string2;
1077 if (FIRST_STRING_P (where))
1079 for (this_char = where - string1; this_char < size1; this_char++)
1080 putchar (string1[this_char]);
1085 for (this_char = where - string2; this_char < size2; this_char++)
1086 putchar (string2[this_char]);
1090 #else /* not DEBUG */
1095 #define DEBUG_STATEMENT(e)
1096 #define DEBUG_PRINT1(x)
1097 #define DEBUG_PRINT2(x1, x2)
1098 #define DEBUG_PRINT3(x1, x2, x3)
1099 #define DEBUG_PRINT4(x1, x2, x3, x4)
1100 #define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
1101 #define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
1103 #endif /* not DEBUG */
1105 /* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
1106 also be assigned to arbitrarily: each pattern buffer stores its own
1107 syntax, so it can be changed between regex compilations. */
1108 /* This has no initializer because initialized variables in Emacs
1109 become read-only after dumping. */
1110 reg_syntax_t re_syntax_options;
1113 /* Specify the precise syntax of regexps for compilation. This provides
1114 for compatibility for various utilities which historically have
1115 different, incompatible syntaxes.
1117 The argument SYNTAX is a bit mask comprised of the various bits
1118 defined in regex.h. We return the old syntax. */
1121 re_set_syntax (syntax)
1122 reg_syntax_t syntax;
1124 reg_syntax_t ret = re_syntax_options;
1126 re_syntax_options = syntax;
1130 /* This table gives an error message for each of the error codes listed
1131 in regex.h. Obviously the order here has to be same as there.
1132 POSIX doesn't require that we do anything for REG_NOERROR,
1133 but why not be nice? */
1135 static const char *re_error_msgid[] =
1137 gettext_noop ("Success"), /* REG_NOERROR */
1138 gettext_noop ("No match"), /* REG_NOMATCH */
1139 gettext_noop ("Invalid regular expression"), /* REG_BADPAT */
1140 gettext_noop ("Invalid collation character"), /* REG_ECOLLATE */
1141 gettext_noop ("Invalid character class name"), /* REG_ECTYPE */
1142 gettext_noop ("Trailing backslash"), /* REG_EESCAPE */
1143 gettext_noop ("Invalid back reference"), /* REG_ESUBREG */
1144 gettext_noop ("Unmatched [ or [^"), /* REG_EBRACK */
1145 gettext_noop ("Unmatched ( or \\("), /* REG_EPAREN */
1146 gettext_noop ("Unmatched \\{"), /* REG_EBRACE */
1147 gettext_noop ("Invalid content of \\{\\}"), /* REG_BADBR */
1148 gettext_noop ("Invalid range end"), /* REG_ERANGE */
1149 gettext_noop ("Memory exhausted"), /* REG_ESPACE */
1150 gettext_noop ("Invalid preceding regular expression"), /* REG_BADRPT */
1151 gettext_noop ("Premature end of regular expression"), /* REG_EEND */
1152 gettext_noop ("Regular expression too big"), /* REG_ESIZE */
1153 gettext_noop ("Unmatched ) or \\)"), /* REG_ERPAREN */
1156 /* Avoiding alloca during matching, to placate r_alloc. */
1158 /* Define MATCH_MAY_ALLOCATE unless we need to make sure that the
1159 searching and matching functions should not call alloca. On some
1160 systems, alloca is implemented in terms of malloc, and if we're
1161 using the relocating allocator routines, then malloc could cause a
1162 relocation, which might (if the strings being searched are in the
1163 ralloc heap) shift the data out from underneath the regexp
1166 Here's another reason to avoid allocation: Emacs
1167 processes input from X in a signal handler; processing X input may
1168 call malloc; if input arrives while a matching routine is calling
1169 malloc, then we're scrod. But Emacs can't just block input while
1170 calling matching routines; then we don't notice interrupts when
1171 they come in. So, Emacs blocks input around all regexp calls
1172 except the matching calls, which it leaves unprotected, in the
1173 faith that they will not malloc. */
1175 /* Normally, this is fine. */
1176 #define MATCH_MAY_ALLOCATE
1178 /* When using GNU C, we are not REALLY using the C alloca, no matter
1179 what config.h may say. So don't take precautions for it. */
1184 /* The match routines may not allocate if (1) they would do it with malloc
1185 and (2) it's not safe for them to use malloc.
1186 Note that if REL_ALLOC is defined, matching would not use malloc for the
1187 failure stack, but we would still use it for the register vectors;
1188 so REL_ALLOC should not affect this. */
1189 #if (defined (C_ALLOCA) || defined (REGEX_MALLOC)) && defined (emacs)
1190 #undef MATCH_MAY_ALLOCATE
1194 /* Failure stack declarations and macros; both re_compile_fastmap and
1195 re_match_2 use a failure stack. These have to be macros because of
1196 REGEX_ALLOCATE_STACK. */
1199 /* Approximate number of failure points for which to initially allocate space
1200 when matching. If this number is exceeded, we allocate more
1201 space, so it is not a hard limit. */
1202 #ifndef INIT_FAILURE_ALLOC
1203 #define INIT_FAILURE_ALLOC 20
1206 /* Roughly the maximum number of failure points on the stack. Would be
1207 exactly that if always used TYPICAL_FAILURE_SIZE items each time we failed.
1208 This is a variable only so users of regex can assign to it; we never
1209 change it ourselves. */
1210 #if defined (MATCH_MAY_ALLOCATE)
1211 /* Note that 4400 is enough to cause a crash on Alpha OSF/1,
1212 whose default stack limit is 2mb. In order for a larger
1213 value to work reliably, you have to try to make it accord
1214 with the process stack limit. */
1215 int re_max_failures = 40000;
1217 int re_max_failures = 4000;
1220 union fail_stack_elt
1222 unsigned char *pointer;
1226 typedef union fail_stack_elt fail_stack_elt_t;
1230 fail_stack_elt_t *stack;
1232 unsigned avail; /* Offset of next open position. */
1235 #define FAIL_STACK_EMPTY() (fail_stack.avail == 0)
1236 #define FAIL_STACK_PTR_EMPTY() (fail_stack_ptr->avail == 0)
1237 #define FAIL_STACK_FULL() (fail_stack.avail == fail_stack.size)
1240 /* Define macros to initialize and free the failure stack.
1241 Do `return -2' if the alloc fails. */
1243 #ifdef MATCH_MAY_ALLOCATE
1244 #define INIT_FAIL_STACK() \
1246 fail_stack.stack = (fail_stack_elt_t *) \
1247 REGEX_ALLOCATE_STACK (INIT_FAILURE_ALLOC * TYPICAL_FAILURE_SIZE \
1248 * sizeof (fail_stack_elt_t)); \
1250 if (fail_stack.stack == NULL) \
1253 fail_stack.size = INIT_FAILURE_ALLOC; \
1254 fail_stack.avail = 0; \
1257 #define RESET_FAIL_STACK() REGEX_FREE_STACK (fail_stack.stack)
1259 #define INIT_FAIL_STACK() \
1261 fail_stack.avail = 0; \
1264 #define RESET_FAIL_STACK()
1268 /* Double the size of FAIL_STACK, up to a limit
1269 which allows approximately `re_max_failures' items.
1271 Return 1 if succeeds, and 0 if either ran out of memory
1272 allocating space for it or it was already too large.
1274 REGEX_REALLOCATE_STACK requires `destination' be declared. */
1276 /* Factor to increase the failure stack size by
1277 when we increase it.
1278 This used to be 2, but 2 was too wasteful
1279 because the old discarded stacks added up to as much space
1280 were as ultimate, maximum-size stack. */
1281 #define FAIL_STACK_GROWTH_FACTOR 4
1283 #define GROW_FAIL_STACK(fail_stack) \
1284 (((fail_stack).size * sizeof (fail_stack_elt_t) \
1285 >= re_max_failures * TYPICAL_FAILURE_SIZE) \
1287 : ((fail_stack).stack \
1288 = (fail_stack_elt_t *) \
1289 REGEX_REALLOCATE_STACK ((fail_stack).stack, \
1290 (fail_stack).size * sizeof (fail_stack_elt_t), \
1291 MIN (re_max_failures * TYPICAL_FAILURE_SIZE, \
1292 ((fail_stack).size * sizeof (fail_stack_elt_t) \
1293 * FAIL_STACK_GROWTH_FACTOR))), \
1295 (fail_stack).stack == NULL \
1297 : ((fail_stack).size \
1298 = (MIN (re_max_failures * TYPICAL_FAILURE_SIZE, \
1299 ((fail_stack).size * sizeof (fail_stack_elt_t) \
1300 * FAIL_STACK_GROWTH_FACTOR)) \
1301 / sizeof (fail_stack_elt_t)), \
1305 /* Push pointer POINTER on FAIL_STACK.
1306 Return 1 if was able to do so and 0 if ran out of memory allocating
1308 #define PUSH_PATTERN_OP(POINTER, FAIL_STACK) \
1309 ((FAIL_STACK_FULL () \
1310 && !GROW_FAIL_STACK (FAIL_STACK)) \
1312 : ((FAIL_STACK).stack[(FAIL_STACK).avail++].pointer = POINTER, \
1315 /* Push a pointer value onto the failure stack.
1316 Assumes the variable `fail_stack'. Probably should only
1317 be called from within `PUSH_FAILURE_POINT'. */
1318 #define PUSH_FAILURE_POINTER(item) \
1319 fail_stack.stack[fail_stack.avail++].pointer = (unsigned char *) (item)
1321 /* This pushes an integer-valued item onto the failure stack.
1322 Assumes the variable `fail_stack'. Probably should only
1323 be called from within `PUSH_FAILURE_POINT'. */
1324 #define PUSH_FAILURE_INT(item) \
1325 fail_stack.stack[fail_stack.avail++].integer = (item)
1327 /* Push a fail_stack_elt_t value onto the failure stack.
1328 Assumes the variable `fail_stack'. Probably should only
1329 be called from within `PUSH_FAILURE_POINT'. */
1330 #define PUSH_FAILURE_ELT(item) \
1331 fail_stack.stack[fail_stack.avail++] = (item)
1333 /* These three POP... operations complement the three PUSH... operations.
1334 All assume that `fail_stack' is nonempty. */
1335 #define POP_FAILURE_POINTER() fail_stack.stack[--fail_stack.avail].pointer
1336 #define POP_FAILURE_INT() fail_stack.stack[--fail_stack.avail].integer
1337 #define POP_FAILURE_ELT() fail_stack.stack[--fail_stack.avail]
1339 /* Used to omit pushing failure point id's when we're not debugging. */
1341 #define DEBUG_PUSH PUSH_FAILURE_INT
1342 #define DEBUG_POP(item_addr) *(item_addr) = POP_FAILURE_INT ()
1344 #define DEBUG_PUSH(item)
1345 #define DEBUG_POP(item_addr)
1349 /* Push the information about the state we will need
1350 if we ever fail back to it.
1352 Requires variables fail_stack, regstart, regend, reg_info, and
1353 num_regs be declared. GROW_FAIL_STACK requires `destination' be
1356 Does `return FAILURE_CODE' if runs out of memory. */
1358 #define PUSH_FAILURE_POINT(pattern_place, string_place, failure_code) \
1360 char *destination; \
1361 /* Must be int, so when we don't save any registers, the arithmetic \
1362 of 0 + -1 isn't done as unsigned. */ \
1365 DEBUG_STATEMENT (failure_id++); \
1366 DEBUG_STATEMENT (nfailure_points_pushed++); \
1367 DEBUG_PRINT2 ("\nPUSH_FAILURE_POINT #%u:\n", failure_id); \
1368 DEBUG_PRINT2 (" Before push, next avail: %d\n", (fail_stack).avail);\
1369 DEBUG_PRINT2 (" size: %d\n", (fail_stack).size);\
1371 DEBUG_PRINT2 (" slots needed: %d\n", NUM_FAILURE_ITEMS); \
1372 DEBUG_PRINT2 (" available: %d\n", REMAINING_AVAIL_SLOTS); \
1374 /* Ensure we have enough space allocated for what we will push. */ \
1375 while (REMAINING_AVAIL_SLOTS < NUM_FAILURE_ITEMS) \
1377 if (!GROW_FAIL_STACK (fail_stack)) \
1378 return failure_code; \
1380 DEBUG_PRINT2 ("\n Doubled stack; size now: %d\n", \
1381 (fail_stack).size); \
1382 DEBUG_PRINT2 (" slots available: %d\n", REMAINING_AVAIL_SLOTS);\
1385 /* Push the info, starting with the registers. */ \
1386 DEBUG_PRINT1 ("\n"); \
1389 for (this_reg = lowest_active_reg; this_reg <= highest_active_reg; \
1392 DEBUG_PRINT2 (" Pushing reg: %d\n", this_reg); \
1393 DEBUG_STATEMENT (num_regs_pushed++); \
1395 DEBUG_PRINT2 (" start: 0x%x\n", regstart[this_reg]); \
1396 PUSH_FAILURE_POINTER (regstart[this_reg]); \
1398 DEBUG_PRINT2 (" end: 0x%x\n", regend[this_reg]); \
1399 PUSH_FAILURE_POINTER (regend[this_reg]); \
1401 DEBUG_PRINT2 (" info: 0x%x\n ", reg_info[this_reg]); \
1402 DEBUG_PRINT2 (" match_null=%d", \
1403 REG_MATCH_NULL_STRING_P (reg_info[this_reg])); \
1404 DEBUG_PRINT2 (" active=%d", IS_ACTIVE (reg_info[this_reg])); \
1405 DEBUG_PRINT2 (" matched_something=%d", \
1406 MATCHED_SOMETHING (reg_info[this_reg])); \
1407 DEBUG_PRINT2 (" ever_matched=%d", \
1408 EVER_MATCHED_SOMETHING (reg_info[this_reg])); \
1409 DEBUG_PRINT1 ("\n"); \
1410 PUSH_FAILURE_ELT (reg_info[this_reg].word); \
1413 DEBUG_PRINT2 (" Pushing low active reg: %d\n", lowest_active_reg);\
1414 PUSH_FAILURE_INT (lowest_active_reg); \
1416 DEBUG_PRINT2 (" Pushing high active reg: %d\n", highest_active_reg);\
1417 PUSH_FAILURE_INT (highest_active_reg); \
1419 DEBUG_PRINT2 (" Pushing pattern 0x%x: ", pattern_place); \
1420 DEBUG_PRINT_COMPILED_PATTERN (bufp, pattern_place, pend); \
1421 PUSH_FAILURE_POINTER (pattern_place); \
1423 DEBUG_PRINT2 (" Pushing string 0x%x: `", string_place); \
1424 DEBUG_PRINT_DOUBLE_STRING (string_place, string1, size1, string2, \
1426 DEBUG_PRINT1 ("'\n"); \
1427 PUSH_FAILURE_POINTER (string_place); \
1429 DEBUG_PRINT2 (" Pushing failure id: %u\n", failure_id); \
1430 DEBUG_PUSH (failure_id); \
1433 /* This is the number of items that are pushed and popped on the stack
1434 for each register. */
1435 #define NUM_REG_ITEMS 3
1437 /* Individual items aside from the registers. */
1439 #define NUM_NONREG_ITEMS 5 /* Includes failure point id. */
1441 #define NUM_NONREG_ITEMS 4
1444 /* Estimate the size of data pushed by a typical failure stack entry.
1445 An estimate is all we need, because all we use this for
1446 is to choose a limit for how big to make the failure stack. */
1448 #define TYPICAL_FAILURE_SIZE 20
1450 /* This is how many items we actually use for a failure point.
1451 It depends on the regexp. */
1452 #define NUM_FAILURE_ITEMS \
1454 ? 0 : highest_active_reg - lowest_active_reg + 1) \
1458 /* How many items can still be added to the stack without overflowing it. */
1459 #define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail)
1462 /* Pops what PUSH_FAIL_STACK pushes.
1464 We restore into the parameters, all of which should be lvalues:
1465 STR -- the saved data position.
1466 PAT -- the saved pattern position.
1467 LOW_REG, HIGH_REG -- the highest and lowest active registers.
1468 REGSTART, REGEND -- arrays of string positions.
1469 REG_INFO -- array of information about each subexpression.
1471 Also assumes the variables `fail_stack' and (if debugging), `bufp',
1472 `pend', `string1', `size1', `string2', and `size2'. */
1474 #define POP_FAILURE_POINT(str, pat, low_reg, high_reg, regstart, regend, reg_info)\
1476 DEBUG_STATEMENT (fail_stack_elt_t failure_id;) \
1478 const unsigned char *string_temp; \
1480 assert (!FAIL_STACK_EMPTY ()); \
1482 /* Remove failure points and point to how many regs pushed. */ \
1483 DEBUG_PRINT1 ("POP_FAILURE_POINT:\n"); \
1484 DEBUG_PRINT2 (" Before pop, next avail: %d\n", fail_stack.avail); \
1485 DEBUG_PRINT2 (" size: %d\n", fail_stack.size); \
1487 assert (fail_stack.avail >= NUM_NONREG_ITEMS); \
1489 DEBUG_POP (&failure_id); \
1490 DEBUG_PRINT2 (" Popping failure id: %u\n", failure_id); \
1492 /* If the saved string location is NULL, it came from an \
1493 on_failure_keep_string_jump opcode, and we want to throw away the \
1494 saved NULL, thus retaining our current position in the string. */ \
1495 string_temp = POP_FAILURE_POINTER (); \
1496 if (string_temp != NULL) \
1497 str = (const char *) string_temp; \
1499 DEBUG_PRINT2 (" Popping string 0x%x: `", str); \
1500 DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2); \
1501 DEBUG_PRINT1 ("'\n"); \
1503 pat = (unsigned char *) POP_FAILURE_POINTER (); \
1504 DEBUG_PRINT2 (" Popping pattern 0x%x: ", pat); \
1505 DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend); \
1507 /* Restore register info. */ \
1508 high_reg = (unsigned) POP_FAILURE_INT (); \
1509 DEBUG_PRINT2 (" Popping high active reg: %d\n", high_reg); \
1511 low_reg = (unsigned) POP_FAILURE_INT (); \
1512 DEBUG_PRINT2 (" Popping low active reg: %d\n", low_reg); \
1515 for (this_reg = high_reg; this_reg >= low_reg; this_reg--) \
1517 DEBUG_PRINT2 (" Popping reg: %d\n", this_reg); \
1519 reg_info[this_reg].word = POP_FAILURE_ELT (); \
1520 DEBUG_PRINT2 (" info: 0x%x\n", reg_info[this_reg]); \
1522 regend[this_reg] = (const char *) POP_FAILURE_POINTER (); \
1523 DEBUG_PRINT2 (" end: 0x%x\n", regend[this_reg]); \
1525 regstart[this_reg] = (const char *) POP_FAILURE_POINTER (); \
1526 DEBUG_PRINT2 (" start: 0x%x\n", regstart[this_reg]); \
1530 for (this_reg = highest_active_reg; this_reg > high_reg; this_reg--) \
1532 reg_info[this_reg].word.integer = 0; \
1533 regend[this_reg] = 0; \
1534 regstart[this_reg] = 0; \
1536 highest_active_reg = high_reg; \
1539 set_regs_matched_done = 0; \
1540 DEBUG_STATEMENT (nfailure_points_popped++); \
1541 } /* POP_FAILURE_POINT */
1545 /* Structure for per-register (a.k.a. per-group) information.
1546 Other register information, such as the
1547 starting and ending positions (which are addresses), and the list of
1548 inner groups (which is a bits list) are maintained in separate
1551 We are making a (strictly speaking) nonportable assumption here: that
1552 the compiler will pack our bit fields into something that fits into
1553 the type of `word', i.e., is something that fits into one item on the
1558 fail_stack_elt_t word;
1561 /* This field is one if this group can match the empty string,
1562 zero if not. If not yet determined, `MATCH_NULL_UNSET_VALUE'. */
1563 #define MATCH_NULL_UNSET_VALUE 3
1564 unsigned match_null_string_p : 2;
1565 unsigned is_active : 1;
1566 unsigned matched_something : 1;
1567 unsigned ever_matched_something : 1;
1569 } register_info_type;
1571 #define REG_MATCH_NULL_STRING_P(R) ((R).bits.match_null_string_p)
1572 #define IS_ACTIVE(R) ((R).bits.is_active)
1573 #define MATCHED_SOMETHING(R) ((R).bits.matched_something)
1574 #define EVER_MATCHED_SOMETHING(R) ((R).bits.ever_matched_something)
1577 /* Call this when have matched a real character; it sets `matched' flags
1578 for the subexpressions which we are currently inside. Also records
1579 that those subexprs have matched. */
1580 #define SET_REGS_MATCHED() \
1583 if (!set_regs_matched_done) \
1586 set_regs_matched_done = 1; \
1587 for (r = lowest_active_reg; r <= highest_active_reg; r++) \
1589 MATCHED_SOMETHING (reg_info[r]) \
1590 = EVER_MATCHED_SOMETHING (reg_info[r]) \
1597 /* Registers are set to a sentinel when they haven't yet matched. */
1598 static char reg_unset_dummy;
1599 #define REG_UNSET_VALUE (®_unset_dummy)
1600 #define REG_UNSET(e) ((e) == REG_UNSET_VALUE)
1602 /* Subroutine declarations and macros for regex_compile. */
1604 static void store_op1 (), store_op2 ();
1605 static void insert_op1 (), insert_op2 ();
1606 static boolean at_begline_loc_p (), at_endline_loc_p ();
1607 static boolean group_in_compile_stack ();
1608 static reg_errcode_t compile_range ();
1610 /* Fetch the next character in the uncompiled pattern---translating it
1611 if necessary. Also cast from a signed character in the constant
1612 string passed to us by the user to an unsigned char that we can use
1613 as an array index (in, e.g., `translate'). */
1615 #define PATFETCH(c) \
1616 do {if (p == pend) return REG_EEND; \
1617 c = (unsigned char) *p++; \
1618 if (RE_TRANSLATE_P (translate)) c = RE_TRANSLATE (translate, c); \
1622 /* Fetch the next character in the uncompiled pattern, with no
1624 #define PATFETCH_RAW(c) \
1625 do {if (p == pend) return REG_EEND; \
1626 c = (unsigned char) *p++; \
1629 /* Go backwards one character in the pattern. */
1630 #define PATUNFETCH p--
1633 /* If `translate' is non-null, return translate[D], else just D. We
1634 cast the subscript to translate because some data is declared as
1635 `char *', to avoid warnings when a string constant is passed. But
1636 when we use a character as a subscript we must make it unsigned. */
1638 #define TRANSLATE(d) \
1639 (RE_TRANSLATE_P (translate) \
1640 ? (unsigned) RE_TRANSLATE (translate, (unsigned) (d)) : (d))
1644 /* Macros for outputting the compiled pattern into `buffer'. */
1646 /* If the buffer isn't allocated when it comes in, use this. */
1647 #define INIT_BUF_SIZE 32
1649 /* Make sure we have at least N more bytes of space in buffer. */
1650 #define GET_BUFFER_SPACE(n) \
1651 while (b - bufp->buffer + (n) > bufp->allocated) \
1654 /* Make sure we have one more byte of buffer space and then add C to it. */
1655 #define BUF_PUSH(c) \
1657 GET_BUFFER_SPACE (1); \
1658 *b++ = (unsigned char) (c); \
1662 /* Ensure we have two more bytes of buffer space and then append C1 and C2. */
1663 #define BUF_PUSH_2(c1, c2) \
1665 GET_BUFFER_SPACE (2); \
1666 *b++ = (unsigned char) (c1); \
1667 *b++ = (unsigned char) (c2); \
1671 /* As with BUF_PUSH_2, except for three bytes. */
1672 #define BUF_PUSH_3(c1, c2, c3) \
1674 GET_BUFFER_SPACE (3); \
1675 *b++ = (unsigned char) (c1); \
1676 *b++ = (unsigned char) (c2); \
1677 *b++ = (unsigned char) (c3); \
1681 /* Store a jump with opcode OP at LOC to location TO. We store a
1682 relative address offset by the three bytes the jump itself occupies. */
1683 #define STORE_JUMP(op, loc, to) \
1684 store_op1 (op, loc, (to) - (loc) - 3)
1686 /* Likewise, for a two-argument jump. */
1687 #define STORE_JUMP2(op, loc, to, arg) \
1688 store_op2 (op, loc, (to) - (loc) - 3, arg)
1690 /* Like `STORE_JUMP', but for inserting. Assume `b' is the buffer end. */
1691 #define INSERT_JUMP(op, loc, to) \
1692 insert_op1 (op, loc, (to) - (loc) - 3, b)
1694 /* Like `STORE_JUMP2', but for inserting. Assume `b' is the buffer end. */
1695 #define INSERT_JUMP2(op, loc, to, arg) \
1696 insert_op2 (op, loc, (to) - (loc) - 3, arg, b)
1699 /* This is not an arbitrary limit: the arguments which represent offsets
1700 into the pattern are two bytes long. So if 2^16 bytes turns out to
1701 be too small, many things would have to change. */
1702 #define MAX_BUF_SIZE (1L << 16)
1705 /* Extend the buffer by twice its current size via realloc and
1706 reset the pointers that pointed into the old block to point to the
1707 correct places in the new one. If extending the buffer results in it
1708 being larger than MAX_BUF_SIZE, then flag memory exhausted. */
1709 #define EXTEND_BUFFER() \
1711 unsigned char *old_buffer = bufp->buffer; \
1712 if (bufp->allocated == MAX_BUF_SIZE) \
1714 bufp->allocated <<= 1; \
1715 if (bufp->allocated > MAX_BUF_SIZE) \
1716 bufp->allocated = MAX_BUF_SIZE; \
1717 bufp->buffer = (unsigned char *) realloc (bufp->buffer, bufp->allocated);\
1718 if (bufp->buffer == NULL) \
1719 return REG_ESPACE; \
1720 /* If the buffer moved, move all the pointers into it. */ \
1721 if (old_buffer != bufp->buffer) \
1723 b = (b - old_buffer) + bufp->buffer; \
1724 begalt = (begalt - old_buffer) + bufp->buffer; \
1725 if (fixup_alt_jump) \
1726 fixup_alt_jump = (fixup_alt_jump - old_buffer) + bufp->buffer;\
1728 laststart = (laststart - old_buffer) + bufp->buffer; \
1729 if (pending_exact) \
1730 pending_exact = (pending_exact - old_buffer) + bufp->buffer; \
1735 /* Since we have one byte reserved for the register number argument to
1736 {start,stop}_memory, the maximum number of groups we can report
1737 things about is what fits in that byte. */
1738 #define MAX_REGNUM 255
1740 /* But patterns can have more than `MAX_REGNUM' registers. We just
1741 ignore the excess. */
1742 typedef unsigned regnum_t;
1745 /* Macros for the compile stack. */
1747 /* Since offsets can go either forwards or backwards, this type needs to
1748 be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1. */
1749 typedef int pattern_offset_t;
1753 pattern_offset_t begalt_offset;
1754 pattern_offset_t fixup_alt_jump;
1755 pattern_offset_t inner_group_offset;
1756 pattern_offset_t laststart_offset;
1758 } compile_stack_elt_t;
1763 compile_stack_elt_t *stack;
1765 unsigned avail; /* Offset of next open position. */
1766 } compile_stack_type;
1769 #define INIT_COMPILE_STACK_SIZE 32
1771 #define COMPILE_STACK_EMPTY (compile_stack.avail == 0)
1772 #define COMPILE_STACK_FULL (compile_stack.avail == compile_stack.size)
1774 /* The next available element. */
1775 #define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
1778 /* Structure to manage work area for range table. */
1779 struct range_table_work_area
1781 int *table; /* actual work area. */
1782 int allocated; /* allocated size for work area in bytes. */
1783 int used; /* actually used size in words. */
1784 int bits; /* flag to record character classes */
1787 /* Make sure that WORK_AREA can hold more N multibyte characters. */
1788 #define EXTEND_RANGE_TABLE_WORK_AREA(work_area, n) \
1790 if (((work_area).used + (n)) * sizeof (int) > (work_area).allocated) \
1792 (work_area).allocated += 16 * sizeof (int); \
1793 if ((work_area).table) \
1795 = (int *) realloc ((work_area).table, (work_area).allocated); \
1798 = (int *) malloc ((work_area).allocated); \
1799 if ((work_area).table == 0) \
1800 FREE_STACK_RETURN (REG_ESPACE); \
1804 #define SET_RANGE_TABLE_WORK_AREA_BIT(work_area, bit) \
1805 (work_area).bits |= (bit)
1807 /* These bits represent the various character classes such as [:alnum:]
1808 in a charset's range table. */
1809 #define BIT_ALNUM 0x1
1810 #define BIT_ALPHA 0x2
1811 #define BIT_WORD 0x4
1812 #define BIT_GRAPH 0x20
1813 #define BIT_LOWER 0x40
1814 #define BIT_PRINT 0x80
1815 #define BIT_PUNCT 0x100
1816 #define BIT_SPACE 0x200
1817 #define BIT_UPPER 0x400
1819 /* Set a range (RANGE_START, RANGE_END) to WORK_AREA. */
1820 #define SET_RANGE_TABLE_WORK_AREA(work_area, range_start, range_end) \
1822 EXTEND_RANGE_TABLE_WORK_AREA ((work_area), 2); \
1823 (work_area).table[(work_area).used++] = (range_start); \
1824 (work_area).table[(work_area).used++] = (range_end); \
1827 /* Free allocated memory for WORK_AREA. */
1828 #define FREE_RANGE_TABLE_WORK_AREA(work_area) \
1830 if ((work_area).table) \
1831 free ((work_area).table); \
1834 #define CLEAR_RANGE_TABLE_WORK_USED(work_area) ((work_area).used = 0, (work_area).bits = 0)
1835 #define RANGE_TABLE_WORK_USED(work_area) ((work_area).used)
1836 #define RANGE_TABLE_WORK_BITS(work_area) ((work_area).bits)
1837 #define RANGE_TABLE_WORK_ELT(work_area, i) ((work_area).table[i])
1840 /* Set the bit for character C in a list. */
1841 #define SET_LIST_BIT(c) \
1842 (b[((unsigned char) (c)) / BYTEWIDTH] \
1843 |= 1 << (((unsigned char) c) % BYTEWIDTH))
1846 /* Get the next unsigned number in the uncompiled pattern. */
1847 #define GET_UNSIGNED_NUMBER(num) \
1851 while (ISDIGIT (c)) \
1855 num = num * 10 + c - '0'; \
1863 #define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
1865 #define IS_CHAR_CLASS(string) \
1866 (STREQ (string, "alpha") || STREQ (string, "upper") \
1867 || STREQ (string, "lower") || STREQ (string, "digit") \
1868 || STREQ (string, "alnum") || STREQ (string, "xdigit") \
1869 || STREQ (string, "space") || STREQ (string, "print") \
1870 || STREQ (string, "punct") || STREQ (string, "graph") \
1871 || STREQ (string, "cntrl") || STREQ (string, "blank") \
1872 || STREQ (string, "word"))
1874 #ifndef MATCH_MAY_ALLOCATE
1876 /* If we cannot allocate large objects within re_match_2_internal,
1877 we make the fail stack and register vectors global.
1878 The fail stack, we grow to the maximum size when a regexp
1880 The register vectors, we adjust in size each time we
1881 compile a regexp, according to the number of registers it needs. */
1883 static fail_stack_type fail_stack;
1885 /* Size with which the following vectors are currently allocated.
1886 That is so we can make them bigger as needed,
1887 but never make them smaller. */
1888 static int regs_allocated_size;
1890 static const char ** regstart, ** regend;
1891 static const char ** old_regstart, ** old_regend;
1892 static const char **best_regstart, **best_regend;
1893 static register_info_type *reg_info;
1894 static const char **reg_dummy;
1895 static register_info_type *reg_info_dummy;
1897 /* Make the register vectors big enough for NUM_REGS registers,
1898 but don't make them smaller. */
1901 regex_grow_registers (num_regs)
1904 if (num_regs > regs_allocated_size)
1906 RETALLOC_IF (regstart, num_regs, const char *);
1907 RETALLOC_IF (regend, num_regs, const char *);
1908 RETALLOC_IF (old_regstart, num_regs, const char *);
1909 RETALLOC_IF (old_regend, num_regs, const char *);
1910 RETALLOC_IF (best_regstart, num_regs, const char *);
1911 RETALLOC_IF (best_regend, num_regs, const char *);
1912 RETALLOC_IF (reg_info, num_regs, register_info_type);
1913 RETALLOC_IF (reg_dummy, num_regs, const char *);
1914 RETALLOC_IF (reg_info_dummy, num_regs, register_info_type);
1916 regs_allocated_size = num_regs;
1920 #endif /* not MATCH_MAY_ALLOCATE */
1922 /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
1923 Returns one of error codes defined in `regex.h', or zero for success.
1925 Assumes the `allocated' (and perhaps `buffer') and `translate'
1926 fields are set in BUFP on entry.
1928 If it succeeds, results are put in BUFP (if it returns an error, the
1929 contents of BUFP are undefined):
1930 `buffer' is the compiled pattern;
1931 `syntax' is set to SYNTAX;
1932 `used' is set to the length of the compiled pattern;
1933 `fastmap_accurate' is zero;
1934 `re_nsub' is the number of subexpressions in PATTERN;
1935 `not_bol' and `not_eol' are zero;
1937 The `fastmap' and `newline_anchor' fields are neither
1938 examined nor set. */
1940 /* Return, freeing storage we allocated. */
1941 #define FREE_STACK_RETURN(value) \
1943 FREE_RANGE_TABLE_WORK_AREA (range_table_work); \
1944 free (compile_stack.stack); \
1948 static reg_errcode_t
1949 regex_compile (pattern, size, syntax, bufp)
1950 const char *pattern;
1952 reg_syntax_t syntax;
1953 struct re_pattern_buffer *bufp;
1955 /* We fetch characters from PATTERN here. Even though PATTERN is
1956 `char *' (i.e., signed), we declare these variables as unsigned, so
1957 they can be reliably used as array indices. */
1958 register unsigned int c, c1;
1960 /* A random temporary spot in PATTERN. */
1963 /* Points to the end of the buffer, where we should append. */
1964 register unsigned char *b;
1966 /* Keeps track of unclosed groups. */
1967 compile_stack_type compile_stack;
1969 /* Points to the current (ending) position in the pattern. */
1971 /* `const' makes AIX compiler fail. */
1974 const char *p = pattern;
1976 const char *pend = pattern + size;
1978 /* How to translate the characters in the pattern. */
1979 RE_TRANSLATE_TYPE translate = bufp->translate;
1981 /* Address of the count-byte of the most recently inserted `exactn'
1982 command. This makes it possible to tell if a new exact-match
1983 character can be added to that command or if the character requires
1984 a new `exactn' command. */
1985 unsigned char *pending_exact = 0;
1987 /* Address of start of the most recently finished expression.
1988 This tells, e.g., postfix * where to find the start of its
1989 operand. Reset at the beginning of groups and alternatives. */
1990 unsigned char *laststart = 0;
1992 /* Address of beginning of regexp, or inside of last group. */
1993 unsigned char *begalt;
1995 /* Place in the uncompiled pattern (i.e., the {) to
1996 which to go back if the interval is invalid. */
1997 const char *beg_interval;
1999 /* Address of the place where a forward jump should go to the end of
2000 the containing expression. Each alternative of an `or' -- except the
2001 last -- ends with a forward jump of this sort. */
2002 unsigned char *fixup_alt_jump = 0;
2004 /* Counts open-groups as they are encountered. Remembered for the
2005 matching close-group on the compile stack, so the same register
2006 number is put in the stop_memory as the start_memory. */
2007 regnum_t regnum = 0;
2009 /* Work area for range table of charset. */
2010 struct range_table_work_area range_table_work;
2013 DEBUG_PRINT1 ("\nCompiling pattern: ");
2016 unsigned debug_count;
2018 for (debug_count = 0; debug_count < size; debug_count++)
2019 putchar (pattern[debug_count]);
2024 /* Initialize the compile stack. */
2025 compile_stack.stack = TALLOC (INIT_COMPILE_STACK_SIZE, compile_stack_elt_t);
2026 if (compile_stack.stack == NULL)
2029 compile_stack.size = INIT_COMPILE_STACK_SIZE;
2030 compile_stack.avail = 0;
2032 range_table_work.table = 0;
2033 range_table_work.allocated = 0;
2035 /* Initialize the pattern buffer. */
2036 bufp->syntax = syntax;
2037 bufp->fastmap_accurate = 0;
2038 bufp->not_bol = bufp->not_eol = 0;
2040 /* Set `used' to zero, so that if we return an error, the pattern
2041 printer (for debugging) will think there's no pattern. We reset it
2045 /* Always count groups, whether or not bufp->no_sub is set. */
2049 /* bufp->multibyte is set before regex_compile is called, so don't alter
2051 #else /* not emacs */
2052 /* Nothing is recognized as a multibyte character. */
2053 bufp->multibyte = 0;
2056 #if !defined (emacs) && !defined (SYNTAX_TABLE)
2057 /* Initialize the syntax table. */
2058 init_syntax_once ();
2061 if (bufp->allocated == 0)
2064 { /* If zero allocated, but buffer is non-null, try to realloc
2065 enough space. This loses if buffer's address is bogus, but
2066 that is the user's responsibility. */
2067 RETALLOC (bufp->buffer, INIT_BUF_SIZE, unsigned char);
2070 { /* Caller did not allocate a buffer. Do it for them. */
2071 bufp->buffer = TALLOC (INIT_BUF_SIZE, unsigned char);
2073 if (!bufp->buffer) FREE_STACK_RETURN (REG_ESPACE);
2075 bufp->allocated = INIT_BUF_SIZE;
2078 begalt = b = bufp->buffer;
2080 /* Loop through the uncompiled pattern until we're at the end. */
2089 if ( /* If at start of pattern, it's an operator. */
2091 /* If context independent, it's an operator. */
2092 || syntax & RE_CONTEXT_INDEP_ANCHORS
2093 /* Otherwise, depends on what's come before. */
2094 || at_begline_loc_p (pattern, p, syntax))
2104 if ( /* If at end of pattern, it's an operator. */
2106 /* If context independent, it's an operator. */
2107 || syntax & RE_CONTEXT_INDEP_ANCHORS
2108 /* Otherwise, depends on what's next. */
2109 || at_endline_loc_p (p, pend, syntax))
2119 if ((syntax & RE_BK_PLUS_QM)
2120 || (syntax & RE_LIMITED_OPS))
2124 /* If there is no previous pattern... */
2127 if (syntax & RE_CONTEXT_INVALID_OPS)
2128 FREE_STACK_RETURN (REG_BADRPT);
2129 else if (!(syntax & RE_CONTEXT_INDEP_OPS))
2134 /* Are we optimizing this jump? */
2135 boolean keep_string_p = false;
2137 /* 1 means zero (many) matches is allowed. */
2138 char zero_times_ok = 0, many_times_ok = 0;
2140 /* If there is a sequence of repetition chars, collapse it
2141 down to just one (the right one). We can't combine
2142 interval operators with these because of, e.g., `a{2}*',
2143 which should only match an even number of `a's. */
2147 zero_times_ok |= c != '+';
2148 many_times_ok |= c != '?';
2156 || (!(syntax & RE_BK_PLUS_QM) && (c == '+' || c == '?')))
2159 else if (syntax & RE_BK_PLUS_QM && c == '\\')
2161 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
2164 if (!(c1 == '+' || c1 == '?'))
2179 /* If we get here, we found another repeat character. */
2182 /* Star, etc. applied to an empty pattern is equivalent
2183 to an empty pattern. */
2187 /* Now we know whether or not zero matches is allowed
2188 and also whether or not two or more matches is allowed. */
2190 { /* More than one repetition is allowed, so put in at the
2191 end a backward relative jump from `b' to before the next
2192 jump we're going to put in below (which jumps from
2193 laststart to after this jump).
2195 But if we are at the `*' in the exact sequence `.*\n',
2196 insert an unconditional jump backwards to the .,
2197 instead of the beginning of the loop. This way we only
2198 push a failure point once, instead of every time
2199 through the loop. */
2200 assert (p - 1 > pattern);
2202 /* Allocate the space for the jump. */
2203 GET_BUFFER_SPACE (3);
2205 /* We know we are not at the first character of the pattern,
2206 because laststart was nonzero. And we've already
2207 incremented `p', by the way, to be the character after
2208 the `*'. Do we have to do something analogous here
2209 for null bytes, because of RE_DOT_NOT_NULL? */
2210 if (TRANSLATE ((unsigned char)*(p - 2)) == TRANSLATE ('.')
2213 && TRANSLATE ((unsigned char)*p) == TRANSLATE ('\n')
2214 && !(syntax & RE_DOT_NEWLINE))
2215 { /* We have .*\n. */
2216 STORE_JUMP (jump, b, laststart);
2217 keep_string_p = true;
2220 /* Anything else. */
2221 STORE_JUMP (maybe_pop_jump, b, laststart - 3);
2223 /* We've added more stuff to the buffer. */
2227 /* On failure, jump from laststart to b + 3, which will be the
2228 end of the buffer after this jump is inserted. */
2229 GET_BUFFER_SPACE (3);
2230 INSERT_JUMP (keep_string_p ? on_failure_keep_string_jump
2238 /* At least one repetition is required, so insert a
2239 `dummy_failure_jump' before the initial
2240 `on_failure_jump' instruction of the loop. This
2241 effects a skip over that instruction the first time
2242 we hit that loop. */
2243 GET_BUFFER_SPACE (3);
2244 INSERT_JUMP (dummy_failure_jump, laststart, laststart + 6);
2259 CLEAR_RANGE_TABLE_WORK_USED (range_table_work);
2261 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2263 /* Ensure that we have enough space to push a charset: the
2264 opcode, the length count, and the bitset; 34 bytes in all. */
2265 GET_BUFFER_SPACE (34);
2269 /* We test `*p == '^' twice, instead of using an if
2270 statement, so we only need one BUF_PUSH. */
2271 BUF_PUSH (*p == '^' ? charset_not : charset);
2275 /* Remember the first position in the bracket expression. */
2278 /* Push the number of bytes in the bitmap. */
2279 BUF_PUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
2281 /* Clear the whole map. */
2282 bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH);
2284 /* charset_not matches newline according to a syntax bit. */
2285 if ((re_opcode_t) b[-2] == charset_not
2286 && (syntax & RE_HAT_LISTS_NOT_NEWLINE))
2287 SET_LIST_BIT ('\n');
2289 /* Read in characters and ranges, setting map bits. */
2293 boolean escaped_char = false;
2295 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2299 /* \ might escape characters inside [...] and [^...]. */
2300 if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) && c == '\\')
2302 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
2305 escaped_char = true;
2309 /* Could be the end of the bracket expression. If it's
2310 not (i.e., when the bracket expression is `[]' so
2311 far), the ']' character bit gets set way below. */
2312 if (c == ']' && p != p1 + 1)
2316 /* If C indicates start of multibyte char, get the
2317 actual character code in C, and set the pattern
2318 pointer P to the next character boundary. */
2319 if (bufp->multibyte && BASE_LEADING_CODE_P (c))
2322 c = STRING_CHAR_AND_LENGTH (p, pend - p, len);
2325 /* What should we do for the character which is
2326 greater than 0x7F, but not BASE_LEADING_CODE_P?
2329 /* See if we're at the beginning of a possible character
2332 else if (!escaped_char &&
2333 syntax & RE_CHAR_CLASSES && c == '[' && *p == ':')
2335 /* Leave room for the null. */
2336 char str[CHAR_CLASS_MAX_LENGTH + 1];
2341 /* If pattern is `[[:'. */
2342 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2347 if (c == ':' || c == ']' || p == pend
2348 || c1 == CHAR_CLASS_MAX_LENGTH)
2354 /* If isn't a word bracketed by `[:' and `:]':
2355 undo the ending character, the letters, and
2356 leave the leading `:' and `[' (but set bits for
2358 if (c == ':' && *p == ']')
2361 boolean is_alnum = STREQ (str, "alnum");
2362 boolean is_alpha = STREQ (str, "alpha");
2363 boolean is_blank = STREQ (str, "blank");
2364 boolean is_cntrl = STREQ (str, "cntrl");
2365 boolean is_digit = STREQ (str, "digit");
2366 boolean is_graph = STREQ (str, "graph");
2367 boolean is_lower = STREQ (str, "lower");
2368 boolean is_print = STREQ (str, "print");
2369 boolean is_punct = STREQ (str, "punct");
2370 boolean is_space = STREQ (str, "space");
2371 boolean is_upper = STREQ (str, "upper");
2372 boolean is_xdigit = STREQ (str, "xdigit");
2373 boolean is_word = STREQ (str, "word");
2375 if (!IS_CHAR_CLASS (str))
2376 FREE_STACK_RETURN (REG_ECTYPE);
2378 /* Throw away the ] at the end of the character
2382 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2384 /* Most character classes in a multibyte match
2385 just set a flag. Exceptions are is_blank,
2386 is_digit, is_cntrl, and is_xdigit, since
2387 they can only match ASCII characters. We
2388 don't need to handle them for multibyte. */
2390 if (bufp->multibyte)
2394 if (is_alnum) bit = BIT_ALNUM;
2395 if (is_alpha) bit = BIT_ALPHA;
2396 if (is_graph) bit = BIT_GRAPH;
2397 if (is_lower) bit = BIT_LOWER;
2398 if (is_print) bit = BIT_PRINT;
2399 if (is_punct) bit = BIT_PUNCT;
2400 if (is_space) bit = BIT_SPACE;
2401 if (is_upper) bit = BIT_UPPER;
2402 if (is_word) bit = BIT_WORD;
2404 SET_RANGE_TABLE_WORK_AREA_BIT (range_table_work,
2408 /* Handle character classes for ASCII characters. */
2409 for (ch = 0; ch < 1 << BYTEWIDTH; ch++)
2411 int translated = TRANSLATE (ch);
2412 /* This was split into 3 if's to
2413 avoid an arbitrary limit in some compiler. */
2414 if ( (is_alnum && ISALNUM (ch))
2415 || (is_alpha && ISALPHA (ch))
2416 || (is_blank && ISBLANK (ch))
2417 || (is_cntrl && ISCNTRL (ch)))
2418 SET_LIST_BIT (translated);
2419 if ( (is_digit && ISDIGIT (ch))
2420 || (is_graph && ISGRAPH (ch))
2421 || (is_lower && ISLOWER (ch))
2422 || (is_print && ISPRINT (ch)))
2423 SET_LIST_BIT (translated);
2424 if ( (is_punct && ISPUNCT (ch))
2425 || (is_space && ISSPACE (ch))
2426 || (is_upper && ISUPPER (ch))
2427 || (is_xdigit && ISXDIGIT (ch)))
2428 SET_LIST_BIT (translated);
2429 if ( (is_word && ISWORD (ch)))
2430 SET_LIST_BIT (translated);
2433 /* Repeat the loop. */
2443 /* Because the `:' may starts the range, we
2444 can't simply set bit and repeat the loop.
2445 Instead, just set it to C and handle below. */
2450 if (p < pend && p[0] == '-' && p[1] != ']')
2453 /* Discard the `-'. */
2456 /* Fetch the character which ends the range. */
2458 if (bufp->multibyte && BASE_LEADING_CODE_P (c1))
2461 c1 = STRING_CHAR_AND_LENGTH (p, pend - p, len);
2465 if (SINGLE_BYTE_CHAR_P (c)
2466 && ! SINGLE_BYTE_CHAR_P (c1))
2468 /* Handle a range such as \177-\377 in multibyte mode.
2469 Split that into two ranges,,
2470 the low one ending at 0237, and the high one
2471 starting at ...040. */
2472 int c1_base = (c1 & ~0177) | 040;
2473 SET_RANGE_TABLE_WORK_AREA (range_table_work, c, c1);
2476 else if (!SAME_CHARSET_P (c, c1))
2477 FREE_STACK_RETURN (REG_ERANGE);
2480 /* Range from C to C. */
2483 /* Set the range ... */
2484 if (SINGLE_BYTE_CHAR_P (c))
2485 /* ... into bitmap. */
2488 int range_start = c, range_end = c1;
2490 /* If the start is after the end, the range is empty. */
2491 if (range_start > range_end)
2493 if (syntax & RE_NO_EMPTY_RANGES)
2494 FREE_STACK_RETURN (REG_ERANGE);
2495 /* Else, repeat the loop. */
2499 for (this_char = range_start; this_char <= range_end;
2501 SET_LIST_BIT (TRANSLATE (this_char));
2505 /* ... into range table. */
2506 SET_RANGE_TABLE_WORK_AREA (range_table_work, c, c1);
2509 /* Discard any (non)matching list bytes that are all 0 at the
2510 end of the map. Decrease the map-length byte too. */
2511 while ((int) b[-1] > 0 && b[b[-1] - 1] == 0)
2515 /* Build real range table from work area. */
2516 if (RANGE_TABLE_WORK_USED (range_table_work)
2517 || RANGE_TABLE_WORK_BITS (range_table_work))
2520 int used = RANGE_TABLE_WORK_USED (range_table_work);
2522 /* Allocate space for COUNT + RANGE_TABLE. Needs two
2523 bytes for flags, two for COUNT, and three bytes for
2525 GET_BUFFER_SPACE (4 + used * 3);
2527 /* Indicate the existence of range table. */
2528 laststart[1] |= 0x80;
2530 /* Store the character class flag bits into the range table.
2531 If not in emacs, these flag bits are always 0. */
2532 *b++ = RANGE_TABLE_WORK_BITS (range_table_work) & 0xff;
2533 *b++ = RANGE_TABLE_WORK_BITS (range_table_work) >> 8;
2535 STORE_NUMBER_AND_INCR (b, used / 2);
2536 for (i = 0; i < used; i++)
2537 STORE_CHARACTER_AND_INCR
2538 (b, RANGE_TABLE_WORK_ELT (range_table_work, i));
2545 if (syntax & RE_NO_BK_PARENS)
2552 if (syntax & RE_NO_BK_PARENS)
2559 if (syntax & RE_NEWLINE_ALT)
2566 if (syntax & RE_NO_BK_VBAR)
2573 if (syntax & RE_INTERVALS && syntax & RE_NO_BK_BRACES)
2574 goto handle_interval;
2580 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
2582 /* Do not translate the character after the \, so that we can
2583 distinguish, e.g., \B from \b, even if we normally would
2584 translate, e.g., B to b. */
2590 if (syntax & RE_NO_BK_PARENS)
2591 goto normal_backslash;
2597 if (COMPILE_STACK_FULL)
2599 RETALLOC (compile_stack.stack, compile_stack.size << 1,
2600 compile_stack_elt_t);
2601 if (compile_stack.stack == NULL) return REG_ESPACE;
2603 compile_stack.size <<= 1;
2606 /* These are the values to restore when we hit end of this
2607 group. They are all relative offsets, so that if the
2608 whole pattern moves because of realloc, they will still
2610 COMPILE_STACK_TOP.begalt_offset = begalt - bufp->buffer;
2611 COMPILE_STACK_TOP.fixup_alt_jump
2612 = fixup_alt_jump ? fixup_alt_jump - bufp->buffer + 1 : 0;
2613 COMPILE_STACK_TOP.laststart_offset = b - bufp->buffer;
2614 COMPILE_STACK_TOP.regnum = regnum;
2616 /* We will eventually replace the 0 with the number of
2617 groups inner to this one. But do not push a
2618 start_memory for groups beyond the last one we can
2619 represent in the compiled pattern. */
2620 if (regnum <= MAX_REGNUM)
2622 COMPILE_STACK_TOP.inner_group_offset = b - bufp->buffer + 2;
2623 BUF_PUSH_3 (start_memory, regnum, 0);
2626 compile_stack.avail++;
2631 /* If we've reached MAX_REGNUM groups, then this open
2632 won't actually generate any code, so we'll have to
2633 clear pending_exact explicitly. */
2639 if (syntax & RE_NO_BK_PARENS) goto normal_backslash;
2641 if (COMPILE_STACK_EMPTY)
2642 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
2643 goto normal_backslash;
2645 FREE_STACK_RETURN (REG_ERPAREN);
2649 { /* Push a dummy failure point at the end of the
2650 alternative for a possible future
2651 `pop_failure_jump' to pop. See comments at
2652 `push_dummy_failure' in `re_match_2'. */
2653 BUF_PUSH (push_dummy_failure);
2655 /* We allocated space for this jump when we assigned
2656 to `fixup_alt_jump', in the `handle_alt' case below. */
2657 STORE_JUMP (jump_past_alt, fixup_alt_jump, b - 1);
2660 /* See similar code for backslashed left paren above. */
2661 if (COMPILE_STACK_EMPTY)
2662 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
2665 FREE_STACK_RETURN (REG_ERPAREN);
2667 /* Since we just checked for an empty stack above, this
2668 ``can't happen''. */
2669 assert (compile_stack.avail != 0);
2671 /* We don't just want to restore into `regnum', because
2672 later groups should continue to be numbered higher,
2673 as in `(ab)c(de)' -- the second group is #2. */
2674 regnum_t this_group_regnum;
2676 compile_stack.avail--;
2677 begalt = bufp->buffer + COMPILE_STACK_TOP.begalt_offset;
2679 = COMPILE_STACK_TOP.fixup_alt_jump
2680 ? bufp->buffer + COMPILE_STACK_TOP.fixup_alt_jump - 1
2682 laststart = bufp->buffer + COMPILE_STACK_TOP.laststart_offset;
2683 this_group_regnum = COMPILE_STACK_TOP.regnum;
2684 /* If we've reached MAX_REGNUM groups, then this open
2685 won't actually generate any code, so we'll have to
2686 clear pending_exact explicitly. */
2689 /* We're at the end of the group, so now we know how many
2690 groups were inside this one. */
2691 if (this_group_regnum <= MAX_REGNUM)
2693 unsigned char *inner_group_loc
2694 = bufp->buffer + COMPILE_STACK_TOP.inner_group_offset;
2696 *inner_group_loc = regnum - this_group_regnum;
2697 BUF_PUSH_3 (stop_memory, this_group_regnum,
2698 regnum - this_group_regnum);
2704 case '|': /* `\|'. */
2705 if (syntax & RE_LIMITED_OPS || syntax & RE_NO_BK_VBAR)
2706 goto normal_backslash;
2708 if (syntax & RE_LIMITED_OPS)
2711 /* Insert before the previous alternative a jump which
2712 jumps to this alternative if the former fails. */
2713 GET_BUFFER_SPACE (3);
2714 INSERT_JUMP (on_failure_jump, begalt, b + 6);
2718 /* The alternative before this one has a jump after it
2719 which gets executed if it gets matched. Adjust that
2720 jump so it will jump to this alternative's analogous
2721 jump (put in below, which in turn will jump to the next
2722 (if any) alternative's such jump, etc.). The last such
2723 jump jumps to the correct final destination. A picture:
2729 If we are at `b', then fixup_alt_jump right now points to a
2730 three-byte space after `a'. We'll put in the jump, set
2731 fixup_alt_jump to right after `b', and leave behind three
2732 bytes which we'll fill in when we get to after `c'. */
2735 STORE_JUMP (jump_past_alt, fixup_alt_jump, b);
2737 /* Mark and leave space for a jump after this alternative,
2738 to be filled in later either by next alternative or
2739 when know we're at the end of a series of alternatives. */
2741 GET_BUFFER_SPACE (3);
2750 /* If \{ is a literal. */
2751 if (!(syntax & RE_INTERVALS)
2752 /* If we're at `\{' and it's not the open-interval
2754 || ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
2755 || (p - 2 == pattern && p == pend))
2756 goto normal_backslash;
2760 /* If got here, then the syntax allows intervals. */
2762 /* At least (most) this many matches must be made. */
2763 int lower_bound = -1, upper_bound = -1;
2765 beg_interval = p - 1;
2769 if (syntax & RE_NO_BK_BRACES)
2770 goto unfetch_interval;
2772 FREE_STACK_RETURN (REG_EBRACE);
2775 GET_UNSIGNED_NUMBER (lower_bound);
2779 GET_UNSIGNED_NUMBER (upper_bound);
2780 if (upper_bound < 0) upper_bound = RE_DUP_MAX;
2783 /* Interval such as `{1}' => match exactly once. */
2784 upper_bound = lower_bound;
2786 if (lower_bound < 0 || upper_bound > RE_DUP_MAX
2787 || lower_bound > upper_bound)
2789 if (syntax & RE_NO_BK_BRACES)
2790 goto unfetch_interval;
2792 FREE_STACK_RETURN (REG_BADBR);
2795 if (!(syntax & RE_NO_BK_BRACES))
2797 if (c != '\\') FREE_STACK_RETURN (REG_EBRACE);
2804 if (syntax & RE_NO_BK_BRACES)
2805 goto unfetch_interval;
2807 FREE_STACK_RETURN (REG_BADBR);
2810 /* We just parsed a valid interval. */
2812 /* If it's invalid to have no preceding re. */
2815 if (syntax & RE_CONTEXT_INVALID_OPS)
2816 FREE_STACK_RETURN (REG_BADRPT);
2817 else if (syntax & RE_CONTEXT_INDEP_OPS)
2820 goto unfetch_interval;
2823 /* If the upper bound is zero, don't want to succeed at
2824 all; jump from `laststart' to `b + 3', which will be
2825 the end of the buffer after we insert the jump. */
2826 if (upper_bound == 0)
2828 GET_BUFFER_SPACE (3);
2829 INSERT_JUMP (jump, laststart, b + 3);
2833 /* Otherwise, we have a nontrivial interval. When
2834 we're all done, the pattern will look like:
2835 set_number_at <jump count> <upper bound>
2836 set_number_at <succeed_n count> <lower bound>
2837 succeed_n <after jump addr> <succeed_n count>
2839 jump_n <succeed_n addr> <jump count>
2840 (The upper bound and `jump_n' are omitted if
2841 `upper_bound' is 1, though.) */
2843 { /* If the upper bound is > 1, we need to insert
2844 more at the end of the loop. */
2845 unsigned nbytes = 10 + (upper_bound > 1) * 10;
2847 GET_BUFFER_SPACE (nbytes);
2849 /* Initialize lower bound of the `succeed_n', even
2850 though it will be set during matching by its
2851 attendant `set_number_at' (inserted next),
2852 because `re_compile_fastmap' needs to know.
2853 Jump to the `jump_n' we might insert below. */
2854 INSERT_JUMP2 (succeed_n, laststart,
2855 b + 5 + (upper_bound > 1) * 5,
2859 /* Code to initialize the lower bound. Insert
2860 before the `succeed_n'. The `5' is the last two
2861 bytes of this `set_number_at', plus 3 bytes of
2862 the following `succeed_n'. */
2863 insert_op2 (set_number_at, laststart, 5, lower_bound, b);
2866 if (upper_bound > 1)
2867 { /* More than one repetition is allowed, so
2868 append a backward jump to the `succeed_n'
2869 that starts this interval.
2871 When we've reached this during matching,
2872 we'll have matched the interval once, so
2873 jump back only `upper_bound - 1' times. */
2874 STORE_JUMP2 (jump_n, b, laststart + 5,
2878 /* The location we want to set is the second
2879 parameter of the `jump_n'; that is `b-2' as
2880 an absolute address. `laststart' will be
2881 the `set_number_at' we're about to insert;
2882 `laststart+3' the number to set, the source
2883 for the relative address. But we are
2884 inserting into the middle of the pattern --
2885 so everything is getting moved up by 5.
2886 Conclusion: (b - 2) - (laststart + 3) + 5,
2887 i.e., b - laststart.
2889 We insert this at the beginning of the loop
2890 so that if we fail during matching, we'll
2891 reinitialize the bounds. */
2892 insert_op2 (set_number_at, laststart, b - laststart,
2893 upper_bound - 1, b);
2898 beg_interval = NULL;
2903 /* If an invalid interval, match the characters as literals. */
2904 assert (beg_interval);
2906 beg_interval = NULL;
2908 /* normal_char and normal_backslash need `c'. */
2911 if (!(syntax & RE_NO_BK_BRACES))
2913 if (p > pattern && p[-1] == '\\')
2914 goto normal_backslash;
2919 /* There is no way to specify the before_dot and after_dot
2920 operators. rms says this is ok. --karl */
2928 BUF_PUSH_2 (syntaxspec, syntax_spec_code[c]);
2934 BUF_PUSH_2 (notsyntaxspec, syntax_spec_code[c]);
2940 BUF_PUSH_2 (categoryspec, c);
2946 BUF_PUSH_2 (notcategoryspec, c);
2953 BUF_PUSH (wordchar);
2959 BUF_PUSH (notwordchar);
2972 BUF_PUSH (wordbound);
2976 BUF_PUSH (notwordbound);
2987 case '1': case '2': case '3': case '4': case '5':
2988 case '6': case '7': case '8': case '9':
2989 if (syntax & RE_NO_BK_REFS)
2995 FREE_STACK_RETURN (REG_ESUBREG);
2997 /* Can't back reference to a subexpression if inside of it. */
2998 if (group_in_compile_stack (compile_stack, c1))
3002 BUF_PUSH_2 (duplicate, c1);
3008 if (syntax & RE_BK_PLUS_QM)
3011 goto normal_backslash;
3015 /* You might think it would be useful for \ to mean
3016 not to translate; but if we don't translate it
3017 it will never match anything. */
3025 /* Expects the character in `c'. */
3027 p1 = p - 1; /* P1 points the head of C. */
3029 if (bufp->multibyte)
3031 c = STRING_CHAR (p1, pend - p1);
3033 /* Set P to the next character boundary. */
3034 p += MULTIBYTE_FORM_LENGTH (p1, pend - p1) - 1;
3037 /* If no exactn currently being built. */
3040 /* If last exactn not at current position. */
3041 || pending_exact + *pending_exact + 1 != b
3043 /* We have only one byte following the exactn for the count. */
3044 || *pending_exact >= (1 << BYTEWIDTH) - (p - p1)
3046 /* If followed by a repetition operator. */
3047 || (p != pend && (*p == '*' || *p == '^'))
3048 || ((syntax & RE_BK_PLUS_QM)
3049 ? p + 1 < pend && *p == '\\' && (p[1] == '+' || p[1] == '?')
3050 : p != pend && (*p == '+' || *p == '?'))
3051 || ((syntax & RE_INTERVALS)
3052 && ((syntax & RE_NO_BK_BRACES)
3053 ? p != pend && *p == '{'
3054 : p + 1 < pend && p[0] == '\\' && p[1] == '{')))
3056 /* Start building a new exactn. */
3060 BUF_PUSH_2 (exactn, 0);
3061 pending_exact = b - 1;
3065 if (! SINGLE_BYTE_CHAR_P (c))
3067 unsigned char work[4], *str;
3068 int i = CHAR_STRING (c, work, str);
3070 for (j = 0; j < i; j++)
3084 } /* while p != pend */
3087 /* Through the pattern now. */
3090 STORE_JUMP (jump_past_alt, fixup_alt_jump, b);
3092 if (!COMPILE_STACK_EMPTY)
3093 FREE_STACK_RETURN (REG_EPAREN);
3095 /* If we don't want backtracking, force success
3096 the first time we reach the end of the compiled pattern. */
3097 if (syntax & RE_NO_POSIX_BACKTRACKING)
3100 free (compile_stack.stack);
3102 /* We have succeeded; set the length of the buffer. */
3103 bufp->used = b - bufp->buffer;
3108 DEBUG_PRINT1 ("\nCompiled pattern: \n");
3109 print_compiled_pattern (bufp);
3113 #ifndef MATCH_MAY_ALLOCATE
3114 /* Initialize the failure stack to the largest possible stack. This
3115 isn't necessary unless we're trying to avoid calling alloca in
3116 the search and match routines. */
3118 int num_regs = bufp->re_nsub + 1;
3120 if (fail_stack.size < re_max_failures * TYPICAL_FAILURE_SIZE)
3122 fail_stack.size = re_max_failures * TYPICAL_FAILURE_SIZE;
3125 if (! fail_stack.stack)
3127 = (fail_stack_elt_t *) xmalloc (fail_stack.size
3128 * sizeof (fail_stack_elt_t));
3131 = (fail_stack_elt_t *) xrealloc (fail_stack.stack,
3133 * sizeof (fail_stack_elt_t)));
3134 #else /* not emacs */
3135 if (! fail_stack.stack)
3137 = (fail_stack_elt_t *) malloc (fail_stack.size
3138 * sizeof (fail_stack_elt_t));
3141 = (fail_stack_elt_t *) realloc (fail_stack.stack,
3143 * sizeof (fail_stack_elt_t)));
3144 #endif /* not emacs */
3147 regex_grow_registers (num_regs);
3149 #endif /* not MATCH_MAY_ALLOCATE */
3152 } /* regex_compile */
3154 /* Subroutines for `regex_compile'. */
3156 /* Store OP at LOC followed by two-byte integer parameter ARG. */
3159 store_op1 (op, loc, arg)
3164 *loc = (unsigned char) op;
3165 STORE_NUMBER (loc + 1, arg);
3169 /* Like `store_op1', but for two two-byte parameters ARG1 and ARG2. */
3172 store_op2 (op, loc, arg1, arg2)
3177 *loc = (unsigned char) op;
3178 STORE_NUMBER (loc + 1, arg1);
3179 STORE_NUMBER (loc + 3, arg2);
3183 /* Copy the bytes from LOC to END to open up three bytes of space at LOC
3184 for OP followed by two-byte integer parameter ARG. */
3187 insert_op1 (op, loc, arg, end)
3193 register unsigned char *pfrom = end;
3194 register unsigned char *pto = end + 3;
3196 while (pfrom != loc)
3199 store_op1 (op, loc, arg);
3203 /* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2. */
3206 insert_op2 (op, loc, arg1, arg2, end)
3212 register unsigned char *pfrom = end;
3213 register unsigned char *pto = end + 5;
3215 while (pfrom != loc)
3218 store_op2 (op, loc, arg1, arg2);
3222 /* P points to just after a ^ in PATTERN. Return true if that ^ comes
3223 after an alternative or a begin-subexpression. We assume there is at
3224 least one character before the ^. */
3227 at_begline_loc_p (pattern, p, syntax)
3228 const char *pattern, *p;
3229 reg_syntax_t syntax;
3231 const char *prev = p - 2;
3232 boolean prev_prev_backslash = prev > pattern && prev[-1] == '\\';
3235 /* After a subexpression? */
3236 (*prev == '(' && (syntax & RE_NO_BK_PARENS || prev_prev_backslash))
3237 /* After an alternative? */
3238 || (*prev == '|' && (syntax & RE_NO_BK_VBAR || prev_prev_backslash));
3242 /* The dual of at_begline_loc_p. This one is for $. We assume there is
3243 at least one character after the $, i.e., `P < PEND'. */
3246 at_endline_loc_p (p, pend, syntax)
3247 const char *p, *pend;
3250 const char *next = p;
3251 boolean next_backslash = *next == '\\';
3252 const char *next_next = p + 1 < pend ? p + 1 : 0;
3255 /* Before a subexpression? */
3256 (syntax & RE_NO_BK_PARENS ? *next == ')'
3257 : next_backslash && next_next && *next_next == ')')
3258 /* Before an alternative? */
3259 || (syntax & RE_NO_BK_VBAR ? *next == '|'
3260 : next_backslash && next_next && *next_next == '|');
3264 /* Returns true if REGNUM is in one of COMPILE_STACK's elements and
3265 false if it's not. */
3268 group_in_compile_stack (compile_stack, regnum)
3269 compile_stack_type compile_stack;
3274 for (this_element = compile_stack.avail - 1;
3277 if (compile_stack.stack[this_element].regnum == regnum)
3283 /* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
3284 BUFP. A fastmap records which of the (1 << BYTEWIDTH) possible
3285 characters can start a string that matches the pattern. This fastmap
3286 is used by re_search to skip quickly over impossible starting points.
3288 Character codes above (1 << BYTEWIDTH) are not represented in the
3289 fastmap, but the leading codes are represented. Thus, the fastmap
3290 indicates which character sets could start a match.
3292 The caller must supply the address of a (1 << BYTEWIDTH)-byte data
3293 area as BUFP->fastmap.
3295 We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in
3298 Returns 0 if we succeed, -2 if an internal error. */
3301 re_compile_fastmap (bufp)
3302 struct re_pattern_buffer *bufp;
3305 #ifdef MATCH_MAY_ALLOCATE
3306 fail_stack_type fail_stack;
3308 #ifndef REGEX_MALLOC
3311 /* We don't push any register information onto the failure stack. */
3312 unsigned num_regs = 0;
3314 register char *fastmap = bufp->fastmap;
3315 unsigned char *pattern = bufp->buffer;
3316 unsigned long size = bufp->used;
3317 unsigned char *p = pattern;
3318 register unsigned char *pend = pattern + size;
3320 /* This holds the pointer to the failure stack, when
3321 it is allocated relocatably. */
3322 fail_stack_elt_t *failure_stack_ptr;
3324 /* Assume that each path through the pattern can be null until
3325 proven otherwise. We set this false at the bottom of switch
3326 statement, to which we get only if a particular path doesn't
3327 match the empty string. */
3328 boolean path_can_be_null = true;
3330 /* We aren't doing a `succeed_n' to begin with. */
3331 boolean succeed_n_p = false;
3333 /* If all elements for base leading-codes in fastmap is set, this
3334 flag is set true. */
3335 boolean match_any_multibyte_characters = false;
3337 /* Maximum code of simple (single byte) character. */
3338 int simple_char_max;
3340 assert (fastmap != NULL && p != NULL);
3343 bzero (fastmap, 1 << BYTEWIDTH); /* Assume nothing's valid. */
3344 bufp->fastmap_accurate = 1; /* It will be when we're done. */
3345 bufp->can_be_null = 0;
3349 if (p == pend || *p == succeed)
3351 /* We have reached the (effective) end of pattern. */
3352 if (!FAIL_STACK_EMPTY ())
3354 bufp->can_be_null |= path_can_be_null;
3356 /* Reset for next path. */
3357 path_can_be_null = true;
3359 p = fail_stack.stack[--fail_stack.avail].pointer;
3367 /* We should never be about to go beyond the end of the pattern. */
3370 switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++))
3373 /* I guess the idea here is to simply not bother with a fastmap
3374 if a backreference is used, since it's too hard to figure out
3375 the fastmap for the corresponding group. Setting
3376 `can_be_null' stops `re_search_2' from using the fastmap, so
3377 that is all we do. */
3379 bufp->can_be_null = 1;
3383 /* Following are the cases which match a character. These end
3394 int length = (*p & 0x7f);;
3397 for (j = length * BYTEWIDTH - 1; j >= 0; j--)
3398 if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
3404 /* Chars beyond end of map must be allowed. */
3406 int length = (*p & 0x7f);;
3409 for (j = length * BYTEWIDTH; j < (1 << BYTEWIDTH); j++)
3412 for (j = length * BYTEWIDTH - 1; j >= 0; j--)
3413 if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
3419 for (j = 0; j < (1 << BYTEWIDTH); j++)
3420 if (SYNTAX (j) == Sword)
3426 for (j = 0; j < (1 << BYTEWIDTH); j++)
3427 if (SYNTAX (j) != Sword)
3432 for (j = CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH - 1, p++;
3434 if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
3437 /* If we can match a syntax class, we can match
3438 any character set. */
3439 if (CHARSET_RANGE_TABLE_EXISTS_P (&p[-2])
3440 && CHARSET_RANGE_TABLE_BITS (&p[-2]) != 0)
3441 goto set_fastmap_for_multibyte_characters;
3443 if (CHARSET_RANGE_TABLE_EXISTS_P (&p[-2])
3444 && match_any_multibyte_characters == false)
3446 /* Set fastmap[I] 1 where I is a base leading code of each
3447 multibyte character in the range table. */
3450 /* Make P points the range table. */
3451 p += CHARSET_BITMAP_SIZE (&p[-2]);
3453 /* Extract the number of ranges in range table into
3455 EXTRACT_NUMBER_AND_INCR (count, p);
3456 for (; count > 0; count--, p += 2 * 3) /* XXX */
3458 /* Extract the start of each range. */
3459 EXTRACT_CHARACTER (c, p);
3460 j = CHAR_CHARSET (c);
3461 fastmap[CHARSET_LEADING_CODE_BASE (j)] = 1;
3468 /* Chars beyond end of bitmap are possible matches.
3469 All the single-byte codes can occur in multibyte buffers.
3470 So any that are not listed in the charset
3471 are possible matches, even in multibyte buffers. */
3472 simple_char_max = (1 << BYTEWIDTH);
3473 for (j = CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH;
3474 j < simple_char_max; j++)
3477 for (j = CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH - 1, p++;
3479 if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
3482 if (bufp->multibyte)
3483 /* Any character set can possibly contain a character
3484 which doesn't match the specified set of characters. */
3486 set_fastmap_for_multibyte_characters:
3487 if (match_any_multibyte_characters == false)
3489 for (j = 0x80; j < 0xA0; j++) /* XXX */
3490 if (BASE_LEADING_CODE_P (j))
3492 match_any_multibyte_characters = true;
3499 /* All the single-byte codes can occur in multibyte buffers,
3500 and they may have word syntax. So do consider them. */
3501 simple_char_max = (1 << BYTEWIDTH);
3502 for (j = 0; j < simple_char_max; j++)
3503 if (SYNTAX (j) == Sword)
3506 if (bufp->multibyte)
3507 /* Any character set can possibly contain a character
3508 whose syntax is `Sword'. */
3509 goto set_fastmap_for_multibyte_characters;
3514 /* All the single-byte codes can occur in multibyte buffers,
3515 and they may not have word syntax. So do consider them. */
3516 simple_char_max = (1 << BYTEWIDTH);
3517 for (j = 0; j < simple_char_max; j++)
3518 if (SYNTAX (j) != Sword)
3521 if (bufp->multibyte)
3522 /* Any character set can possibly contain a character
3523 whose syntax is not `Sword'. */
3524 goto set_fastmap_for_multibyte_characters;
3530 int fastmap_newline = fastmap['\n'];
3532 /* `.' matches anything, except perhaps newline.
3533 Even in a multibyte buffer, it should match any
3534 conceivable byte value for the fastmap. */
3535 if (bufp->multibyte)
3536 match_any_multibyte_characters = true;
3538 simple_char_max = (1 << BYTEWIDTH);
3539 for (j = 0; j < simple_char_max; j++)
3542 /* ... except perhaps newline. */
3543 if (!(bufp->syntax & RE_DOT_NEWLINE))
3544 fastmap['\n'] = fastmap_newline;
3546 /* Return if we have already set `can_be_null'; if we have,
3547 then the fastmap is irrelevant. Something's wrong here. */
3548 else if (bufp->can_be_null)
3551 /* Otherwise, have to check alternative paths. */
3562 /* This match depends on text properties. These end with
3563 aborting optimizations. */
3564 bufp->can_be_null = 1;
3568 simple_char_max = bufp->multibyte ? 0x80 : (1 << BYTEWIDTH);
3569 for (j = 0; j < simple_char_max; j++)
3570 if (SYNTAX (j) == (enum syntaxcode) k)
3573 if (bufp->multibyte)
3574 /* Any character set can possibly contain a character
3575 whose syntax is K. */
3576 goto set_fastmap_for_multibyte_characters;
3581 simple_char_max = bufp->multibyte ? 0x80 : (1 << BYTEWIDTH);
3582 for (j = 0; j < simple_char_max; j++)
3583 if (SYNTAX (j) != (enum syntaxcode) k)
3586 if (bufp->multibyte)
3587 /* Any character set can possibly contain a character
3588 whose syntax is not K. */
3589 goto set_fastmap_for_multibyte_characters;
3596 simple_char_max = (1 << BYTEWIDTH);
3597 for (j = 0; j < simple_char_max; j++)
3598 if (CHAR_HAS_CATEGORY (j, k))
3601 if (bufp->multibyte)
3602 /* Any character set can possibly contain a character
3603 whose category is K. */
3604 goto set_fastmap_for_multibyte_characters;
3608 case notcategoryspec:
3610 simple_char_max = (1 << BYTEWIDTH);
3611 for (j = 0; j < simple_char_max; j++)
3612 if (!CHAR_HAS_CATEGORY (j, k))
3615 if (bufp->multibyte)
3616 /* Any character set can possibly contain a character
3617 whose category is not K. */
3618 goto set_fastmap_for_multibyte_characters;
3621 /* All cases after this match the empty string. These end with
3643 case push_dummy_failure:
3648 case pop_failure_jump:
3649 case maybe_pop_jump:
3652 case dummy_failure_jump:
3653 EXTRACT_NUMBER_AND_INCR (j, p);
3658 /* Jump backward implies we just went through the body of a
3659 loop and matched nothing. Opcode jumped to should be
3660 `on_failure_jump' or `succeed_n'. Just treat it like an
3661 ordinary jump. For a * loop, it has pushed its failure
3662 point already; if so, discard that as redundant. */
3663 if ((re_opcode_t) *p != on_failure_jump
3664 && (re_opcode_t) *p != succeed_n)
3668 EXTRACT_NUMBER_AND_INCR (j, p);
3671 /* If what's on the stack is where we are now, pop it. */
3672 if (!FAIL_STACK_EMPTY ()
3673 && fail_stack.stack[fail_stack.avail - 1].pointer == p)
3679 case on_failure_jump:
3680 case on_failure_keep_string_jump:
3681 handle_on_failure_jump:
3682 EXTRACT_NUMBER_AND_INCR (j, p);
3684 /* For some patterns, e.g., `(a?)?', `p+j' here points to the
3685 end of the pattern. We don't want to push such a point,
3686 since when we restore it above, entering the switch will
3687 increment `p' past the end of the pattern. We don't need
3688 to push such a point since we obviously won't find any more
3689 fastmap entries beyond `pend'. Such a pattern can match
3690 the null string, though. */
3693 if (!PUSH_PATTERN_OP (p + j, fail_stack))
3695 RESET_FAIL_STACK ();
3700 bufp->can_be_null = 1;
3704 EXTRACT_NUMBER_AND_INCR (k, p); /* Skip the n. */
3705 succeed_n_p = false;
3712 /* Get to the number of times to succeed. */
3715 /* Increment p past the n for when k != 0. */
3716 EXTRACT_NUMBER_AND_INCR (k, p);
3720 succeed_n_p = true; /* Spaghetti code alert. */
3721 goto handle_on_failure_jump;
3738 abort (); /* We have listed all the cases. */
3741 /* Getting here means we have found the possible starting
3742 characters for one path of the pattern -- and that the empty
3743 string does not match. We need not follow this path further.
3744 Instead, look at the next alternative (remembered on the
3745 stack), or quit if no more. The test at the top of the loop
3746 does these things. */
3747 path_can_be_null = false;
3751 /* Set `can_be_null' for the last path (also the first path, if the
3752 pattern is empty). */
3753 bufp->can_be_null |= path_can_be_null;
3756 RESET_FAIL_STACK ();
3758 } /* re_compile_fastmap */
3760 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
3761 ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
3762 this memory for recording register information. STARTS and ENDS
3763 must be allocated using the malloc library routine, and must each
3764 be at least NUM_REGS * sizeof (regoff_t) bytes long.
3766 If NUM_REGS == 0, then subsequent matches should allocate their own
3769 Unless this function is called, the first search or match using
3770 PATTERN_BUFFER will allocate its own register data, without
3771 freeing the old data. */
3774 re_set_registers (bufp, regs, num_regs, starts, ends)
3775 struct re_pattern_buffer *bufp;
3776 struct re_registers *regs;
3778 regoff_t *starts, *ends;
3782 bufp->regs_allocated = REGS_REALLOCATE;
3783 regs->num_regs = num_regs;
3784 regs->start = starts;
3789 bufp->regs_allocated = REGS_UNALLOCATED;
3791 regs->start = regs->end = (regoff_t *) 0;
3795 /* Searching routines. */
3797 /* Like re_search_2, below, but only one string is specified, and
3798 doesn't let you say where to stop matching. */
3801 re_search (bufp, string, size, startpos, range, regs)
3802 struct re_pattern_buffer *bufp;
3804 int size, startpos, range;
3805 struct re_registers *regs;
3807 return re_search_2 (bufp, NULL, 0, string, size, startpos, range,
3811 /* End address of virtual concatenation of string. */
3812 #define STOP_ADDR_VSTRING(P) \
3813 (((P) >= size1 ? string2 + size2 : string1 + size1))
3815 /* Address of POS in the concatenation of virtual string. */
3816 #define POS_ADDR_VSTRING(POS) \
3817 (((POS) >= size1 ? string2 - size1 : string1) + (POS))
3819 /* Using the compiled pattern in BUFP->buffer, first tries to match the
3820 virtual concatenation of STRING1 and STRING2, starting first at index
3821 STARTPOS, then at STARTPOS + 1, and so on.
3823 STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
3825 RANGE is how far to scan while trying to match. RANGE = 0 means try
3826 only at STARTPOS; in general, the last start tried is STARTPOS +
3829 In REGS, return the indices of the virtual concatenation of STRING1
3830 and STRING2 that matched the entire BUFP->buffer and its contained
3833 Do not consider matching one past the index STOP in the virtual
3834 concatenation of STRING1 and STRING2.
3836 We return either the position in the strings at which the match was
3837 found, -1 if no match, or -2 if error (such as failure
3841 re_search_2 (bufp, string1, size1, string2, size2, startpos, range, regs, stop)
3842 struct re_pattern_buffer *bufp;
3843 const char *string1, *string2;
3847 struct re_registers *regs;
3851 register char *fastmap = bufp->fastmap;
3852 register RE_TRANSLATE_TYPE translate = bufp->translate;
3853 int total_size = size1 + size2;
3854 int endpos = startpos + range;
3855 int anchored_start = 0;
3857 /* Nonzero if we have to concern multibyte character. */
3858 int multibyte = bufp->multibyte;
3860 /* Check for out-of-range STARTPOS. */
3861 if (startpos < 0 || startpos > total_size)
3864 /* Fix up RANGE if it might eventually take us outside
3865 the virtual concatenation of STRING1 and STRING2.
3866 Make sure we won't move STARTPOS below 0 or above TOTAL_SIZE. */
3868 range = 0 - startpos;
3869 else if (endpos > total_size)
3870 range = total_size - startpos;
3872 /* If the search isn't to be a backwards one, don't waste time in a
3873 search for a pattern anchored at beginning of buffer. */
3874 if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == begbuf && range > 0)
3883 /* In a forward search for something that starts with \=.
3884 don't keep searching past point. */
3885 if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == at_dot && range > 0)
3887 range = PT_BYTE - BEGV_BYTE - startpos;
3893 /* Update the fastmap now if not correct already. */
3894 if (fastmap && !bufp->fastmap_accurate)
3895 if (re_compile_fastmap (bufp) == -2)
3898 /* See whether the pattern is anchored. */
3899 if (bufp->buffer[0] == begline)
3903 gl_state.object = re_match_object;
3905 int adjpos = NILP (re_match_object) || BUFFERP (re_match_object);
3906 int charpos = SYNTAX_TABLE_BYTE_TO_CHAR (startpos + adjpos);
3908 SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object, charpos, 1);
3912 /* Loop through the string, looking for a place to start matching. */
3915 /* If the pattern is anchored,
3916 skip quickly past places we cannot match.
3917 We don't bother to treat startpos == 0 specially
3918 because that case doesn't repeat. */
3919 if (anchored_start && startpos > 0)
3921 if (! (bufp->newline_anchor
3922 && ((startpos <= size1 ? string1[startpos - 1]
3923 : string2[startpos - size1 - 1])
3928 /* If a fastmap is supplied, skip quickly over characters that
3929 cannot be the start of a match. If the pattern can match the
3930 null string, however, we don't need to skip characters; we want
3931 the first null string. */
3932 if (fastmap && startpos < total_size && !bufp->can_be_null)
3934 register const char *d;
3935 register unsigned int buf_ch;
3937 d = POS_ADDR_VSTRING (startpos);
3939 if (range > 0) /* Searching forwards. */
3941 register int lim = 0;
3944 if (startpos < size1 && startpos + range >= size1)
3945 lim = range - (size1 - startpos);
3947 /* Written out as an if-else to avoid testing `translate'
3949 if (RE_TRANSLATE_P (translate))
3956 buf_ch = STRING_CHAR_AND_LENGTH (d, range - lim,
3959 buf_ch = RE_TRANSLATE (translate, buf_ch);
3964 range -= buf_charlen;
3969 && !fastmap[(unsigned char)
3970 RE_TRANSLATE (translate, (unsigned char) *d)])
3977 while (range > lim && !fastmap[(unsigned char) *d])
3983 startpos += irange - range;
3985 else /* Searching backwards. */
3987 int room = (size1 == 0 || startpos >= size1
3988 ? size2 + size1 - startpos
3989 : size1 - startpos);
3991 buf_ch = STRING_CHAR (d, room);
3992 if (RE_TRANSLATE_P (translate))
3993 buf_ch = RE_TRANSLATE (translate, buf_ch);
3995 if (! (buf_ch >= 0400
3996 || fastmap[buf_ch]))
4001 /* If can't match the null string, and that's all we have left, fail. */
4002 if (range >= 0 && startpos == total_size && fastmap
4003 && !bufp->can_be_null)
4006 val = re_match_2_internal (bufp, string1, size1, string2, size2,
4007 startpos, regs, stop);
4008 #ifndef REGEX_MALLOC
4025 /* Update STARTPOS to the next character boundary. */
4028 const unsigned char *p
4029 = (const unsigned char *) POS_ADDR_VSTRING (startpos);
4030 const unsigned char *pend
4031 = (const unsigned char *) STOP_ADDR_VSTRING (startpos);
4032 int len = MULTIBYTE_FORM_LENGTH (p, pend - p);
4050 /* Update STARTPOS to the previous character boundary. */
4053 const unsigned char *p
4054 = (const unsigned char *) POS_ADDR_VSTRING (startpos);
4057 /* Find the head of multibyte form. */
4058 while (!CHAR_HEAD_P (*p))
4063 if (MULTIBYTE_FORM_LENGTH (p, len + 1) != (len + 1))
4080 /* Declarations and macros for re_match_2. */
4082 static int bcmp_translate ();
4083 static boolean alt_match_null_string_p (),
4084 common_op_match_null_string_p (),
4085 group_match_null_string_p ();
4087 /* This converts PTR, a pointer into one of the search strings `string1'
4088 and `string2' into an offset from the beginning of that string. */
4089 #define POINTER_TO_OFFSET(ptr) \
4090 (FIRST_STRING_P (ptr) \
4091 ? ((regoff_t) ((ptr) - string1)) \
4092 : ((regoff_t) ((ptr) - string2 + size1)))
4094 /* Macros for dealing with the split strings in re_match_2. */
4096 #define MATCHING_IN_FIRST_STRING (dend == end_match_1)
4098 /* Call before fetching a character with *d. This switches over to
4099 string2 if necessary. */
4100 #define PREFETCH() \
4103 /* End of string2 => fail. */ \
4104 if (dend == end_match_2) \
4106 /* End of string1 => advance to string2. */ \
4108 dend = end_match_2; \
4112 /* Test if at very beginning or at very end of the virtual concatenation
4113 of `string1' and `string2'. If only one string, it's `string2'. */
4114 #define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2)
4115 #define AT_STRINGS_END(d) ((d) == end2)
4118 /* Test if D points to a character which is word-constituent. We have
4119 two special cases to check for: if past the end of string1, look at
4120 the first character in string2; and if before the beginning of
4121 string2, look at the last character in string1. */
4122 #define WORDCHAR_P(d) \
4123 (SYNTAX ((d) == end1 ? *string2 \
4124 : (d) == string2 - 1 ? *(end1 - 1) : *(d)) \
4127 /* Disabled due to a compiler bug -- see comment at case wordbound */
4129 /* The comment at case wordbound is following one, but we don't use
4130 AT_WORD_BOUNDARY anymore to support multibyte form.
4132 The DEC Alpha C compiler 3.x generates incorrect code for the
4133 test WORDCHAR_P (d - 1) != WORDCHAR_P (d) in the expansion of
4134 AT_WORD_BOUNDARY, so this code is disabled. Expanding the
4135 macro and introducing temporary variables works around the bug. */
4138 /* Test if the character before D and the one at D differ with respect
4139 to being word-constituent. */
4140 #define AT_WORD_BOUNDARY(d) \
4141 (AT_STRINGS_BEG (d) || AT_STRINGS_END (d) \
4142 || WORDCHAR_P (d - 1) != WORDCHAR_P (d))
4145 /* Free everything we malloc. */
4146 #ifdef MATCH_MAY_ALLOCATE
4147 #define FREE_VAR(var) if (var) { REGEX_FREE (var); var = NULL; } else
4148 #define FREE_VARIABLES() \
4150 REGEX_FREE_STACK (fail_stack.stack); \
4151 FREE_VAR (regstart); \
4152 FREE_VAR (regend); \
4153 FREE_VAR (old_regstart); \
4154 FREE_VAR (old_regend); \
4155 FREE_VAR (best_regstart); \
4156 FREE_VAR (best_regend); \
4157 FREE_VAR (reg_info); \
4158 FREE_VAR (reg_dummy); \
4159 FREE_VAR (reg_info_dummy); \
4162 #define FREE_VARIABLES() ((void)0) /* Do nothing! But inhibit gcc warning. */
4163 #endif /* not MATCH_MAY_ALLOCATE */
4165 /* These values must meet several constraints. They must not be valid
4166 register values; since we have a limit of 255 registers (because
4167 we use only one byte in the pattern for the register number), we can
4168 use numbers larger than 255. They must differ by 1, because of
4169 NUM_FAILURE_ITEMS above. And the value for the lowest register must
4170 be larger than the value for the highest register, so we do not try
4171 to actually save any registers when none are active. */
4172 #define NO_HIGHEST_ACTIVE_REG (1 << BYTEWIDTH)
4173 #define NO_LOWEST_ACTIVE_REG (NO_HIGHEST_ACTIVE_REG + 1)
4175 /* Matching routines. */
4177 #ifndef emacs /* Emacs never uses this. */
4178 /* re_match is like re_match_2 except it takes only a single string. */
4181 re_match (bufp, string, size, pos, regs)
4182 struct re_pattern_buffer *bufp;
4185 struct re_registers *regs;
4187 int result = re_match_2_internal (bufp, NULL, 0, string, size,
4192 #endif /* not emacs */
4195 /* In Emacs, this is the string or buffer in which we
4196 are matching. It is used for looking up syntax properties. */
4197 Lisp_Object re_match_object;
4200 /* re_match_2 matches the compiled pattern in BUFP against the
4201 the (virtual) concatenation of STRING1 and STRING2 (of length SIZE1
4202 and SIZE2, respectively). We start matching at POS, and stop
4205 If REGS is non-null and the `no_sub' field of BUFP is nonzero, we
4206 store offsets for the substring each group matched in REGS. See the
4207 documentation for exactly how many groups we fill.
4209 We return -1 if no match, -2 if an internal error (such as the
4210 failure stack overflowing). Otherwise, we return the length of the
4211 matched substring. */
4214 re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
4215 struct re_pattern_buffer *bufp;
4216 const char *string1, *string2;
4219 struct re_registers *regs;
4226 int adjpos = NILP (re_match_object) || BUFFERP (re_match_object);
4227 gl_state.object = re_match_object;
4228 charpos = SYNTAX_TABLE_BYTE_TO_CHAR (pos + adjpos);
4229 SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object, charpos, 1);
4232 result = re_match_2_internal (bufp, string1, size1, string2, size2,
4238 /* This is a separate function so that we can force an alloca cleanup
4241 re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
4242 struct re_pattern_buffer *bufp;
4243 const char *string1, *string2;
4246 struct re_registers *regs;
4249 /* General temporaries. */
4253 /* Just past the end of the corresponding string. */
4254 const char *end1, *end2;
4256 /* Pointers into string1 and string2, just past the last characters in
4257 each to consider matching. */
4258 const char *end_match_1, *end_match_2;
4260 /* Where we are in the data, and the end of the current string. */
4261 const char *d, *dend;
4263 /* Where we are in the pattern, and the end of the pattern. */
4264 unsigned char *p = bufp->buffer;
4265 register unsigned char *pend = p + bufp->used;
4267 /* Mark the opcode just after a start_memory, so we can test for an
4268 empty subpattern when we get to the stop_memory. */
4269 unsigned char *just_past_start_mem = 0;
4271 /* We use this to map every character in the string. */
4272 RE_TRANSLATE_TYPE translate = bufp->translate;
4274 /* Nonzero if we have to concern multibyte character. */
4275 int multibyte = bufp->multibyte;
4277 /* Failure point stack. Each place that can handle a failure further
4278 down the line pushes a failure point on this stack. It consists of
4279 restart, regend, and reg_info for all registers corresponding to
4280 the subexpressions we're currently inside, plus the number of such
4281 registers, and, finally, two char *'s. The first char * is where
4282 to resume scanning the pattern; the second one is where to resume
4283 scanning the strings. If the latter is zero, the failure point is
4284 a ``dummy''; if a failure happens and the failure point is a dummy,
4285 it gets discarded and the next next one is tried. */
4286 #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
4287 fail_stack_type fail_stack;
4290 static unsigned failure_id = 0;
4291 unsigned nfailure_points_pushed = 0, nfailure_points_popped = 0;
4294 /* This holds the pointer to the failure stack, when
4295 it is allocated relocatably. */
4296 fail_stack_elt_t *failure_stack_ptr;
4298 /* We fill all the registers internally, independent of what we
4299 return, for use in backreferences. The number here includes
4300 an element for register zero. */
4301 unsigned num_regs = bufp->re_nsub + 1;
4303 /* The currently active registers. */
4304 unsigned lowest_active_reg = NO_LOWEST_ACTIVE_REG;
4305 unsigned highest_active_reg = NO_HIGHEST_ACTIVE_REG;
4307 /* Information on the contents of registers. These are pointers into
4308 the input strings; they record just what was matched (on this
4309 attempt) by a subexpression part of the pattern, that is, the
4310 regnum-th regstart pointer points to where in the pattern we began
4311 matching and the regnum-th regend points to right after where we
4312 stopped matching the regnum-th subexpression. (The zeroth register
4313 keeps track of what the whole pattern matches.) */
4314 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
4315 const char **regstart, **regend;
4318 /* If a group that's operated upon by a repetition operator fails to
4319 match anything, then the register for its start will need to be
4320 restored because it will have been set to wherever in the string we
4321 are when we last see its open-group operator. Similarly for a
4323 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
4324 const char **old_regstart, **old_regend;
4327 /* The is_active field of reg_info helps us keep track of which (possibly
4328 nested) subexpressions we are currently in. The matched_something
4329 field of reg_info[reg_num] helps us tell whether or not we have
4330 matched any of the pattern so far this time through the reg_num-th
4331 subexpression. These two fields get reset each time through any
4332 loop their register is in. */
4333 #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
4334 register_info_type *reg_info;
4337 /* The following record the register info as found in the above
4338 variables when we find a match better than any we've seen before.
4339 This happens as we backtrack through the failure points, which in
4340 turn happens only if we have not yet matched the entire string. */
4341 unsigned best_regs_set = false;
4342 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
4343 const char **best_regstart, **best_regend;
4346 /* Logically, this is `best_regend[0]'. But we don't want to have to
4347 allocate space for that if we're not allocating space for anything
4348 else (see below). Also, we never need info about register 0 for
4349 any of the other register vectors, and it seems rather a kludge to
4350 treat `best_regend' differently than the rest. So we keep track of
4351 the end of the best match so far in a separate variable. We
4352 initialize this to NULL so that when we backtrack the first time
4353 and need to test it, it's not garbage. */
4354 const char *match_end = NULL;
4356 /* This helps SET_REGS_MATCHED avoid doing redundant work. */
4357 int set_regs_matched_done = 0;
4359 /* Used when we pop values we don't care about. */
4360 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
4361 const char **reg_dummy;
4362 register_info_type *reg_info_dummy;
4366 /* Counts the total number of registers pushed. */
4367 unsigned num_regs_pushed = 0;
4370 DEBUG_PRINT1 ("\n\nEntering re_match_2.\n");
4374 #ifdef MATCH_MAY_ALLOCATE
4375 /* Do not bother to initialize all the register variables if there are
4376 no groups in the pattern, as it takes a fair amount of time. If
4377 there are groups, we include space for register 0 (the whole
4378 pattern), even though we never use it, since it simplifies the
4379 array indexing. We should fix this. */
4382 regstart = REGEX_TALLOC (num_regs, const char *);
4383 regend = REGEX_TALLOC (num_regs, const char *);
4384 old_regstart = REGEX_TALLOC (num_regs, const char *);
4385 old_regend = REGEX_TALLOC (num_regs, const char *);
4386 best_regstart = REGEX_TALLOC (num_regs, const char *);
4387 best_regend = REGEX_TALLOC (num_regs, const char *);
4388 reg_info = REGEX_TALLOC (num_regs, register_info_type);
4389 reg_dummy = REGEX_TALLOC (num_regs, const char *);
4390 reg_info_dummy = REGEX_TALLOC (num_regs, register_info_type);
4392 if (!(regstart && regend && old_regstart && old_regend && reg_info
4393 && best_regstart && best_regend && reg_dummy && reg_info_dummy))
4401 /* We must initialize all our variables to NULL, so that
4402 `FREE_VARIABLES' doesn't try to free them. */
4403 regstart = regend = old_regstart = old_regend = best_regstart
4404 = best_regend = reg_dummy = NULL;
4405 reg_info = reg_info_dummy = (register_info_type *) NULL;
4407 #endif /* MATCH_MAY_ALLOCATE */
4409 /* The starting position is bogus. */
4410 if (pos < 0 || pos > size1 + size2)
4416 /* Initialize subexpression text positions to -1 to mark ones that no
4417 start_memory/stop_memory has been seen for. Also initialize the
4418 register information struct. */
4419 for (mcnt = 1; mcnt < num_regs; mcnt++)
4421 regstart[mcnt] = regend[mcnt]
4422 = old_regstart[mcnt] = old_regend[mcnt] = REG_UNSET_VALUE;
4424 REG_MATCH_NULL_STRING_P (reg_info[mcnt]) = MATCH_NULL_UNSET_VALUE;
4425 IS_ACTIVE (reg_info[mcnt]) = 0;
4426 MATCHED_SOMETHING (reg_info[mcnt]) = 0;
4427 EVER_MATCHED_SOMETHING (reg_info[mcnt]) = 0;
4430 /* We move `string1' into `string2' if the latter's empty -- but not if
4431 `string1' is null. */
4432 if (size2 == 0 && string1 != NULL)
4439 end1 = string1 + size1;
4440 end2 = string2 + size2;
4442 /* Compute where to stop matching, within the two strings. */
4445 end_match_1 = string1 + stop;
4446 end_match_2 = string2;
4451 end_match_2 = string2 + stop - size1;
4454 /* `p' scans through the pattern as `d' scans through the data.
4455 `dend' is the end of the input string that `d' points within. `d'
4456 is advanced into the following input string whenever necessary, but
4457 this happens before fetching; therefore, at the beginning of the
4458 loop, `d' can be pointing at the end of a string, but it cannot
4460 if (size1 > 0 && pos <= size1)
4467 d = string2 + pos - size1;
4471 DEBUG_PRINT1 ("The compiled pattern is: ");
4472 DEBUG_PRINT_COMPILED_PATTERN (bufp, p, pend);
4473 DEBUG_PRINT1 ("The string to match is: `");
4474 DEBUG_PRINT_DOUBLE_STRING (d, string1, size1, string2, size2);
4475 DEBUG_PRINT1 ("'\n");
4477 /* This loops over pattern commands. It exits by returning from the
4478 function if the match is complete, or it drops through if the match
4479 fails at this starting point in the input data. */
4482 DEBUG_PRINT2 ("\n0x%x: ", p);
4485 { /* End of pattern means we might have succeeded. */
4486 DEBUG_PRINT1 ("end of pattern ... ");
4488 /* If we haven't matched the entire string, and we want the
4489 longest match, try backtracking. */
4490 if (d != end_match_2)
4492 /* 1 if this match ends in the same string (string1 or string2)
4493 as the best previous match. */
4494 boolean same_str_p = (FIRST_STRING_P (match_end)
4495 == MATCHING_IN_FIRST_STRING);
4496 /* 1 if this match is the best seen so far. */
4497 boolean best_match_p;
4499 /* AIX compiler got confused when this was combined
4500 with the previous declaration. */
4502 best_match_p = d > match_end;
4504 best_match_p = !MATCHING_IN_FIRST_STRING;
4506 DEBUG_PRINT1 ("backtracking.\n");
4508 if (!FAIL_STACK_EMPTY ())
4509 { /* More failure points to try. */
4511 /* If exceeds best match so far, save it. */
4512 if (!best_regs_set || best_match_p)
4514 best_regs_set = true;
4517 DEBUG_PRINT1 ("\nSAVING match as best so far.\n");
4519 for (mcnt = 1; mcnt < num_regs; mcnt++)
4521 best_regstart[mcnt] = regstart[mcnt];
4522 best_regend[mcnt] = regend[mcnt];
4528 /* If no failure points, don't restore garbage. And if
4529 last match is real best match, don't restore second
4531 else if (best_regs_set && !best_match_p)
4534 /* Restore best match. It may happen that `dend ==
4535 end_match_1' while the restored d is in string2.
4536 For example, the pattern `x.*y.*z' against the
4537 strings `x-' and `y-z-', if the two strings are
4538 not consecutive in memory. */
4539 DEBUG_PRINT1 ("Restoring best registers.\n");
4542 dend = ((d >= string1 && d <= end1)
4543 ? end_match_1 : end_match_2);
4545 for (mcnt = 1; mcnt < num_regs; mcnt++)
4547 regstart[mcnt] = best_regstart[mcnt];
4548 regend[mcnt] = best_regend[mcnt];
4551 } /* d != end_match_2 */
4554 DEBUG_PRINT1 ("Accepting match.\n");
4556 /* If caller wants register contents data back, do it. */
4557 if (regs && !bufp->no_sub)
4559 /* Have the register data arrays been allocated? */
4560 if (bufp->regs_allocated == REGS_UNALLOCATED)
4561 { /* No. So allocate them with malloc. We need one
4562 extra element beyond `num_regs' for the `-1' marker
4564 regs->num_regs = MAX (RE_NREGS, num_regs + 1);
4565 regs->start = TALLOC (regs->num_regs, regoff_t);
4566 regs->end = TALLOC (regs->num_regs, regoff_t);
4567 if (regs->start == NULL || regs->end == NULL)
4572 bufp->regs_allocated = REGS_REALLOCATE;
4574 else if (bufp->regs_allocated == REGS_REALLOCATE)
4575 { /* Yes. If we need more elements than were already
4576 allocated, reallocate them. If we need fewer, just
4578 if (regs->num_regs < num_regs + 1)
4580 regs->num_regs = num_regs + 1;
4581 RETALLOC (regs->start, regs->num_regs, regoff_t);
4582 RETALLOC (regs->end, regs->num_regs, regoff_t);
4583 if (regs->start == NULL || regs->end == NULL)
4592 /* These braces fend off a "empty body in an else-statement"
4593 warning under GCC when assert expands to nothing. */
4594 assert (bufp->regs_allocated == REGS_FIXED);
4597 /* Convert the pointer data in `regstart' and `regend' to
4598 indices. Register zero has to be set differently,
4599 since we haven't kept track of any info for it. */
4600 if (regs->num_regs > 0)
4602 regs->start[0] = pos;
4603 regs->end[0] = (MATCHING_IN_FIRST_STRING
4604 ? ((regoff_t) (d - string1))
4605 : ((regoff_t) (d - string2 + size1)));
4608 /* Go through the first `min (num_regs, regs->num_regs)'
4609 registers, since that is all we initialized. */
4610 for (mcnt = 1; mcnt < MIN (num_regs, regs->num_regs); mcnt++)
4612 if (REG_UNSET (regstart[mcnt]) || REG_UNSET (regend[mcnt]))
4613 regs->start[mcnt] = regs->end[mcnt] = -1;
4617 = (regoff_t) POINTER_TO_OFFSET (regstart[mcnt]);
4619 = (regoff_t) POINTER_TO_OFFSET (regend[mcnt]);
4623 /* If the regs structure we return has more elements than
4624 were in the pattern, set the extra elements to -1. If
4625 we (re)allocated the registers, this is the case,
4626 because we always allocate enough to have at least one
4628 for (mcnt = num_regs; mcnt < regs->num_regs; mcnt++)
4629 regs->start[mcnt] = regs->end[mcnt] = -1;
4630 } /* regs && !bufp->no_sub */
4632 DEBUG_PRINT4 ("%u failure points pushed, %u popped (%u remain).\n",
4633 nfailure_points_pushed, nfailure_points_popped,
4634 nfailure_points_pushed - nfailure_points_popped);
4635 DEBUG_PRINT2 ("%u registers pushed.\n", num_regs_pushed);
4637 mcnt = d - pos - (MATCHING_IN_FIRST_STRING
4641 DEBUG_PRINT2 ("Returning %d from re_match_2.\n", mcnt);
4647 /* Otherwise match next pattern command. */
4648 switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++))
4650 /* Ignore these. Used to ignore the n of succeed_n's which
4651 currently have n == 0. */
4653 DEBUG_PRINT1 ("EXECUTING no_op.\n");
4657 DEBUG_PRINT1 ("EXECUTING succeed.\n");
4660 /* Match the next n pattern characters exactly. The following
4661 byte in the pattern defines n, and the n bytes after that
4662 are the characters to match. */
4665 DEBUG_PRINT2 ("EXECUTING exactn %d.\n", mcnt);
4667 /* This is written out as an if-else so we don't waste time
4668 testing `translate' inside the loop. */
4669 if (RE_TRANSLATE_P (translate))
4675 int pat_charlen, buf_charlen;
4676 unsigned int pat_ch, buf_ch;
4679 pat_ch = STRING_CHAR_AND_LENGTH (p, pend - p, pat_charlen);
4680 buf_ch = STRING_CHAR_AND_LENGTH (d, dend - d, buf_charlen);
4682 if (RE_TRANSLATE (translate, buf_ch)
4688 mcnt -= pat_charlen;
4692 #endif /* not emacs */
4696 if ((unsigned char) RE_TRANSLATE (translate, (unsigned char) *d)
4697 != (unsigned char) *p++)
4708 if (*d++ != (char) *p++) goto fail;
4712 SET_REGS_MATCHED ();
4716 /* Match any character except possibly a newline or a null. */
4720 unsigned int buf_ch;
4722 DEBUG_PRINT1 ("EXECUTING anychar.\n");
4728 buf_ch = STRING_CHAR_AND_LENGTH (d, dend - d, buf_charlen);
4730 #endif /* not emacs */
4732 buf_ch = (unsigned char) *d;
4736 buf_ch = TRANSLATE (buf_ch);
4738 if ((!(bufp->syntax & RE_DOT_NEWLINE)
4740 || ((bufp->syntax & RE_DOT_NOT_NULL)
4741 && buf_ch == '\000'))
4744 SET_REGS_MATCHED ();
4745 DEBUG_PRINT2 (" Matched `%d'.\n", *d);
4754 register unsigned int c;
4755 boolean not = (re_opcode_t) *(p - 1) == charset_not;
4758 /* Start of actual range_table, or end of bitmap if there is no
4760 unsigned char *range_table;
4762 /* Nonzero if there is a range table. */
4763 int range_table_exists;
4765 /* Number of ranges of range table. This is not included
4766 in the initial byte-length of the command. */
4769 DEBUG_PRINT2 ("EXECUTING charset%s.\n", not ? "_not" : "");
4772 c = (unsigned char) *d;
4774 range_table_exists = CHARSET_RANGE_TABLE_EXISTS_P (&p[-1]);
4777 if (range_table_exists)
4779 range_table = CHARSET_RANGE_TABLE (&p[-1]); /* Past the bitmap. */
4780 EXTRACT_NUMBER_AND_INCR (count, range_table);
4783 if (multibyte && BASE_LEADING_CODE_P (c))
4784 c = STRING_CHAR_AND_LENGTH (d, dend - d, len);
4787 if (SINGLE_BYTE_CHAR_P (c))
4788 { /* Lookup bitmap. */
4789 c = TRANSLATE (c); /* The character to match. */
4792 /* Cast to `unsigned' instead of `unsigned char' in
4793 case the bit list is a full 32 bytes long. */
4794 if (c < (unsigned) (CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH)
4795 && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
4799 else if (range_table_exists)
4801 int class_bits = CHARSET_RANGE_TABLE_BITS (&p[-1]);
4803 if ( (class_bits & BIT_ALNUM && ISALNUM (c))
4804 | (class_bits & BIT_ALPHA && ISALPHA (c))
4805 | (class_bits & BIT_GRAPH && ISGRAPH (c))
4806 | (class_bits & BIT_LOWER && ISLOWER (c))
4807 | (class_bits & BIT_PRINT && ISPRINT (c))
4808 | (class_bits & BIT_PUNCT && ISPUNCT (c))
4809 | (class_bits & BIT_SPACE && ISSPACE (c))
4810 | (class_bits & BIT_UPPER && ISUPPER (c))
4811 | (class_bits & BIT_WORD && ISWORD (c)))
4814 CHARSET_LOOKUP_RANGE_TABLE_RAW (not, c, range_table, count);
4818 if (range_table_exists)
4819 p = CHARSET_RANGE_TABLE_END (range_table, count);
4821 p += CHARSET_BITMAP_SIZE (&p[-1]) + 1;
4823 if (!not) goto fail;
4825 SET_REGS_MATCHED ();
4831 /* The beginning of a group is represented by start_memory.
4832 The arguments are the register number in the next byte, and the
4833 number of groups inner to this one in the next. The text
4834 matched within the group is recorded (in the internal
4835 registers data structure) under the register number. */
4837 DEBUG_PRINT3 ("EXECUTING start_memory %d (%d):\n", *p, p[1]);
4839 /* Find out if this group can match the empty string. */
4840 p1 = p; /* To send to group_match_null_string_p. */
4842 if (REG_MATCH_NULL_STRING_P (reg_info[*p]) == MATCH_NULL_UNSET_VALUE)
4843 REG_MATCH_NULL_STRING_P (reg_info[*p])
4844 = group_match_null_string_p (&p1, pend, reg_info);
4846 /* Save the position in the string where we were the last time
4847 we were at this open-group operator in case the group is
4848 operated upon by a repetition operator, e.g., with `(a*)*b'
4849 against `ab'; then we want to ignore where we are now in
4850 the string in case this attempt to match fails. */
4851 old_regstart[*p] = REG_MATCH_NULL_STRING_P (reg_info[*p])
4852 ? REG_UNSET (regstart[*p]) ? d : regstart[*p]
4854 DEBUG_PRINT2 (" old_regstart: %d\n",
4855 POINTER_TO_OFFSET (old_regstart[*p]));
4858 DEBUG_PRINT2 (" regstart: %d\n", POINTER_TO_OFFSET (regstart[*p]));
4860 IS_ACTIVE (reg_info[*p]) = 1;
4861 MATCHED_SOMETHING (reg_info[*p]) = 0;
4863 /* Clear this whenever we change the register activity status. */
4864 set_regs_matched_done = 0;
4866 /* This is the new highest active register. */
4867 highest_active_reg = *p;
4869 /* If nothing was active before, this is the new lowest active
4871 if (lowest_active_reg == NO_LOWEST_ACTIVE_REG)
4872 lowest_active_reg = *p;
4874 /* Move past the register number and inner group count. */
4876 just_past_start_mem = p;
4881 /* The stop_memory opcode represents the end of a group. Its
4882 arguments are the same as start_memory's: the register
4883 number, and the number of inner groups. */
4885 DEBUG_PRINT3 ("EXECUTING stop_memory %d (%d):\n", *p, p[1]);
4887 /* We need to save the string position the last time we were at
4888 this close-group operator in case the group is operated
4889 upon by a repetition operator, e.g., with `((a*)*(b*)*)*'
4890 against `aba'; then we want to ignore where we are now in
4891 the string in case this attempt to match fails. */
4892 old_regend[*p] = REG_MATCH_NULL_STRING_P (reg_info[*p])
4893 ? REG_UNSET (regend[*p]) ? d : regend[*p]
4895 DEBUG_PRINT2 (" old_regend: %d\n",
4896 POINTER_TO_OFFSET (old_regend[*p]));
4899 DEBUG_PRINT2 (" regend: %d\n", POINTER_TO_OFFSET (regend[*p]));
4901 /* This register isn't active anymore. */
4902 IS_ACTIVE (reg_info[*p]) = 0;
4904 /* Clear this whenever we change the register activity status. */
4905 set_regs_matched_done = 0;
4907 /* If this was the only register active, nothing is active
4909 if (lowest_active_reg == highest_active_reg)
4911 lowest_active_reg = NO_LOWEST_ACTIVE_REG;
4912 highest_active_reg = NO_HIGHEST_ACTIVE_REG;
4915 { /* We must scan for the new highest active register, since
4916 it isn't necessarily one less than now: consider
4917 (a(b)c(d(e)f)g). When group 3 ends, after the f), the
4918 new highest active register is 1. */
4919 unsigned char r = *p - 1;
4920 while (r > 0 && !IS_ACTIVE (reg_info[r]))
4923 /* If we end up at register zero, that means that we saved
4924 the registers as the result of an `on_failure_jump', not
4925 a `start_memory', and we jumped to past the innermost
4926 `stop_memory'. For example, in ((.)*) we save
4927 registers 1 and 2 as a result of the *, but when we pop
4928 back to the second ), we are at the stop_memory 1.
4929 Thus, nothing is active. */
4932 lowest_active_reg = NO_LOWEST_ACTIVE_REG;
4933 highest_active_reg = NO_HIGHEST_ACTIVE_REG;
4936 highest_active_reg = r;
4939 /* If just failed to match something this time around with a
4940 group that's operated on by a repetition operator, try to
4941 force exit from the ``loop'', and restore the register
4942 information for this group that we had before trying this
4944 if ((!MATCHED_SOMETHING (reg_info[*p])
4945 || just_past_start_mem == p - 1)
4948 boolean is_a_jump_n = false;
4952 switch ((re_opcode_t) *p1++)
4956 case pop_failure_jump:
4957 case maybe_pop_jump:
4959 case dummy_failure_jump:
4960 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
4970 /* If the next operation is a jump backwards in the pattern
4971 to an on_failure_jump right before the start_memory
4972 corresponding to this stop_memory, exit from the loop
4973 by forcing a failure after pushing on the stack the
4974 on_failure_jump's jump in the pattern, and d. */
4975 if (mcnt < 0 && (re_opcode_t) *p1 == on_failure_jump
4976 && (re_opcode_t) p1[3] == start_memory && p1[4] == *p)
4978 /* If this group ever matched anything, then restore
4979 what its registers were before trying this last
4980 failed match, e.g., with `(a*)*b' against `ab' for
4981 regstart[1], and, e.g., with `((a*)*(b*)*)*'
4982 against `aba' for regend[3].
4984 Also restore the registers for inner groups for,
4985 e.g., `((a*)(b*))*' against `aba' (register 3 would
4986 otherwise get trashed). */
4988 if (EVER_MATCHED_SOMETHING (reg_info[*p]))
4992 EVER_MATCHED_SOMETHING (reg_info[*p]) = 0;
4994 /* Restore this and inner groups' (if any) registers. */
4995 for (r = *p; r < *p + *(p + 1); r++)
4997 regstart[r] = old_regstart[r];
4999 /* xx why this test? */
5000 if (old_regend[r] >= regstart[r])
5001 regend[r] = old_regend[r];
5005 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
5006 PUSH_FAILURE_POINT (p1 + mcnt, d, -2);
5012 /* Move past the register number and the inner group count. */
5017 /* \<digit> has been turned into a `duplicate' command which is
5018 followed by the numeric value of <digit> as the register number. */
5021 register const char *d2, *dend2;
5022 int regno = *p++; /* Get which register to match against. */
5023 DEBUG_PRINT2 ("EXECUTING duplicate %d.\n", regno);
5025 /* Can't back reference a group which we've never matched. */
5026 if (REG_UNSET (regstart[regno]) || REG_UNSET (regend[regno]))
5029 /* Where in input to try to start matching. */
5030 d2 = regstart[regno];
5032 /* Where to stop matching; if both the place to start and
5033 the place to stop matching are in the same string, then
5034 set to the place to stop, otherwise, for now have to use
5035 the end of the first string. */
5037 dend2 = ((FIRST_STRING_P (regstart[regno])
5038 == FIRST_STRING_P (regend[regno]))
5039 ? regend[regno] : end_match_1);
5042 /* If necessary, advance to next segment in register
5046 if (dend2 == end_match_2) break;
5047 if (dend2 == regend[regno]) break;
5049 /* End of string1 => advance to string2. */
5051 dend2 = regend[regno];
5053 /* At end of register contents => success */
5054 if (d2 == dend2) break;
5056 /* If necessary, advance to next segment in data. */
5059 /* How many characters left in this segment to match. */
5062 /* Want how many consecutive characters we can match in
5063 one shot, so, if necessary, adjust the count. */
5064 if (mcnt > dend2 - d2)
5067 /* Compare that many; failure if mismatch, else move
5069 if (RE_TRANSLATE_P (translate)
5070 ? bcmp_translate (d, d2, mcnt, translate)
5071 : bcmp (d, d2, mcnt))
5073 d += mcnt, d2 += mcnt;
5075 /* Do this because we've match some characters. */
5076 SET_REGS_MATCHED ();
5082 /* begline matches the empty string at the beginning of the string
5083 (unless `not_bol' is set in `bufp'), and, if
5084 `newline_anchor' is set, after newlines. */
5086 DEBUG_PRINT1 ("EXECUTING begline.\n");
5088 if (AT_STRINGS_BEG (d))
5090 if (!bufp->not_bol) break;
5092 else if (d[-1] == '\n' && bufp->newline_anchor)
5096 /* In all other cases, we fail. */
5100 /* endline is the dual of begline. */
5102 DEBUG_PRINT1 ("EXECUTING endline.\n");
5104 if (AT_STRINGS_END (d))
5106 if (!bufp->not_eol) break;
5109 /* We have to ``prefetch'' the next character. */
5110 else if ((d == end1 ? *string2 : *d) == '\n'
5111 && bufp->newline_anchor)
5118 /* Match at the very beginning of the data. */
5120 DEBUG_PRINT1 ("EXECUTING begbuf.\n");
5121 if (AT_STRINGS_BEG (d))
5126 /* Match at the very end of the data. */
5128 DEBUG_PRINT1 ("EXECUTING endbuf.\n");
5129 if (AT_STRINGS_END (d))
5134 /* on_failure_keep_string_jump is used to optimize `.*\n'. It
5135 pushes NULL as the value for the string on the stack. Then
5136 `pop_failure_point' will keep the current value for the
5137 string, instead of restoring it. To see why, consider
5138 matching `foo\nbar' against `.*\n'. The .* matches the foo;
5139 then the . fails against the \n. But the next thing we want
5140 to do is match the \n against the \n; if we restored the
5141 string value, we would be back at the foo.
5143 Because this is used only in specific cases, we don't need to
5144 check all the things that `on_failure_jump' does, to make
5145 sure the right things get saved on the stack. Hence we don't
5146 share its code. The only reason to push anything on the
5147 stack at all is that otherwise we would have to change
5148 `anychar's code to do something besides goto fail in this
5149 case; that seems worse than this. */
5150 case on_failure_keep_string_jump:
5151 DEBUG_PRINT1 ("EXECUTING on_failure_keep_string_jump");
5153 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5154 DEBUG_PRINT3 (" %d (to 0x%x):\n", mcnt, p + mcnt);
5156 PUSH_FAILURE_POINT (p + mcnt, NULL, -2);
5160 /* Uses of on_failure_jump:
5162 Each alternative starts with an on_failure_jump that points
5163 to the beginning of the next alternative. Each alternative
5164 except the last ends with a jump that in effect jumps past
5165 the rest of the alternatives. (They really jump to the
5166 ending jump of the following alternative, because tensioning
5167 these jumps is a hassle.)
5169 Repeats start with an on_failure_jump that points past both
5170 the repetition text and either the following jump or
5171 pop_failure_jump back to this on_failure_jump. */
5172 case on_failure_jump:
5174 DEBUG_PRINT1 ("EXECUTING on_failure_jump");
5176 #if defined (WINDOWSNT) && defined (emacs)
5180 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5181 DEBUG_PRINT3 (" %d (to 0x%x)", mcnt, p + mcnt);
5183 /* If this on_failure_jump comes right before a group (i.e.,
5184 the original * applied to a group), save the information
5185 for that group and all inner ones, so that if we fail back
5186 to this point, the group's information will be correct.
5187 For example, in \(a*\)*\1, we need the preceding group,
5188 and in \(zz\(a*\)b*\)\2, we need the inner group. */
5190 /* We can't use `p' to check ahead because we push
5191 a failure point to `p + mcnt' after we do this. */
5194 /* We need to skip no_op's before we look for the
5195 start_memory in case this on_failure_jump is happening as
5196 the result of a completed succeed_n, as in \(a\)\{1,3\}b\1
5198 while (p1 < pend && (re_opcode_t) *p1 == no_op)
5201 if (p1 < pend && (re_opcode_t) *p1 == start_memory)
5203 /* We have a new highest active register now. This will
5204 get reset at the start_memory we are about to get to,
5205 but we will have saved all the registers relevant to
5206 this repetition op, as described above. */
5207 highest_active_reg = *(p1 + 1) + *(p1 + 2);
5208 if (lowest_active_reg == NO_LOWEST_ACTIVE_REG)
5209 lowest_active_reg = *(p1 + 1);
5212 DEBUG_PRINT1 (":\n");
5213 PUSH_FAILURE_POINT (p + mcnt, d, -2);
5217 /* A smart repeat ends with `maybe_pop_jump'.
5218 We change it to either `pop_failure_jump' or `jump'. */
5219 case maybe_pop_jump:
5220 #if defined (WINDOWSNT) && defined (emacs)
5223 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5224 DEBUG_PRINT2 ("EXECUTING maybe_pop_jump %d.\n", mcnt);
5226 register unsigned char *p2 = p;
5228 /* Compare the beginning of the repeat with what in the
5229 pattern follows its end. If we can establish that there
5230 is nothing that they would both match, i.e., that we
5231 would have to backtrack because of (as in, e.g., `a*a')
5232 then we can change to pop_failure_jump, because we'll
5233 never have to backtrack.
5235 This is not true in the case of alternatives: in
5236 `(a|ab)*' we do need to backtrack to the `ab' alternative
5237 (e.g., if the string was `ab'). But instead of trying to
5238 detect that here, the alternative has put on a dummy
5239 failure point which is what we will end up popping. */
5241 /* Skip over open/close-group commands.
5242 If what follows this loop is a ...+ construct,
5243 look at what begins its body, since we will have to
5244 match at least one of that. */
5248 && ((re_opcode_t) *p2 == stop_memory
5249 || (re_opcode_t) *p2 == start_memory))
5251 else if (p2 + 6 < pend
5252 && (re_opcode_t) *p2 == dummy_failure_jump)
5259 /* p1[0] ... p1[2] are the `on_failure_jump' corresponding
5260 to the `maybe_finalize_jump' of this case. Examine what
5263 /* If we're at the end of the pattern, we can change. */
5266 /* Consider what happens when matching ":\(.*\)"
5267 against ":/". I don't really understand this code
5269 p[-3] = (unsigned char) pop_failure_jump;
5271 (" End of pattern: change to `pop_failure_jump'.\n");
5274 else if ((re_opcode_t) *p2 == exactn
5275 || (bufp->newline_anchor && (re_opcode_t) *p2 == endline))
5277 register unsigned int c
5278 = *p2 == (unsigned char) endline ? '\n' : p2[2];
5280 if ((re_opcode_t) p1[3] == exactn)
5282 if (!(multibyte /* && (c != '\n') */
5283 && BASE_LEADING_CODE_P (c))
5285 : (STRING_CHAR (&p2[2], pend - &p2[2])
5286 != STRING_CHAR (&p1[5], pend - &p1[5])))
5288 p[-3] = (unsigned char) pop_failure_jump;
5289 DEBUG_PRINT3 (" %c != %c => pop_failure_jump.\n",
5294 else if ((re_opcode_t) p1[3] == charset
5295 || (re_opcode_t) p1[3] == charset_not)
5297 int not = (re_opcode_t) p1[3] == charset_not;
5299 if (multibyte /* && (c != '\n') */
5300 && BASE_LEADING_CODE_P (c))
5301 c = STRING_CHAR (&p2[2], pend - &p2[2]);
5303 /* Test if C is listed in charset (or charset_not)
5305 if (SINGLE_BYTE_CHAR_P (c))
5307 if (c < CHARSET_BITMAP_SIZE (&p1[3]) * BYTEWIDTH
5308 && p1[5 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
5311 else if (CHARSET_RANGE_TABLE_EXISTS_P (&p1[3]))
5312 CHARSET_LOOKUP_RANGE_TABLE (not, c, &p1[3]);
5314 /* `not' is equal to 1 if c would match, which means
5315 that we can't change to pop_failure_jump. */
5318 p[-3] = (unsigned char) pop_failure_jump;
5319 DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
5323 else if ((re_opcode_t) *p2 == charset)
5325 if ((re_opcode_t) p1[3] == exactn)
5327 register unsigned int c = p1[5];
5330 if (multibyte && BASE_LEADING_CODE_P (c))
5331 c = STRING_CHAR (&p1[5], pend - &p1[5]);
5333 /* Test if C is listed in charset at `p2'. */
5334 if (SINGLE_BYTE_CHAR_P (c))
5336 if (c < CHARSET_BITMAP_SIZE (p2) * BYTEWIDTH
5337 && (p2[2 + c / BYTEWIDTH]
5338 & (1 << (c % BYTEWIDTH))))
5341 else if (CHARSET_RANGE_TABLE_EXISTS_P (p2))
5342 CHARSET_LOOKUP_RANGE_TABLE (not, c, p2);
5346 p[-3] = (unsigned char) pop_failure_jump;
5347 DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
5351 /* It is hard to list up all the character in charset
5352 P2 if it includes multibyte character. Give up in
5354 else if (!multibyte || !CHARSET_RANGE_TABLE_EXISTS_P (p2))
5356 /* Now, we are sure that P2 has no range table.
5357 So, for the size of bitmap in P2, `p2[1]' is
5358 enough. But P1 may have range table, so the
5359 size of bitmap table of P1 is extracted by
5360 using macro `CHARSET_BITMAP_SIZE'.
5362 Since we know that all the character listed in
5363 P2 is ASCII, it is enough to test only bitmap
5366 if ((re_opcode_t) p1[3] == charset_not)
5369 /* We win if the charset_not inside the loop lists
5370 every character listed in the charset after. */
5371 for (idx = 0; idx < (int) p2[1]; idx++)
5372 if (! (p2[2 + idx] == 0
5373 || (idx < CHARSET_BITMAP_SIZE (&p1[3])
5374 && ((p2[2 + idx] & ~ p1[5 + idx]) == 0))))
5379 p[-3] = (unsigned char) pop_failure_jump;
5380 DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
5383 else if ((re_opcode_t) p1[3] == charset)
5386 /* We win if the charset inside the loop
5387 has no overlap with the one after the loop. */
5390 && idx < CHARSET_BITMAP_SIZE (&p1[3]));
5392 if ((p2[2 + idx] & p1[5 + idx]) != 0)
5396 || idx == CHARSET_BITMAP_SIZE (&p1[3]))
5398 p[-3] = (unsigned char) pop_failure_jump;
5399 DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
5405 p -= 2; /* Point at relative address again. */
5406 if ((re_opcode_t) p[-1] != pop_failure_jump)
5408 p[-1] = (unsigned char) jump;
5409 DEBUG_PRINT1 (" Match => jump.\n");
5410 goto unconditional_jump;
5412 /* Note fall through. */
5415 /* The end of a simple repeat has a pop_failure_jump back to
5416 its matching on_failure_jump, where the latter will push a
5417 failure point. The pop_failure_jump takes off failure
5418 points put on by this pop_failure_jump's matching
5419 on_failure_jump; we got through the pattern to here from the
5420 matching on_failure_jump, so didn't fail. */
5421 case pop_failure_jump:
5423 /* We need to pass separate storage for the lowest and
5424 highest registers, even though we don't care about the
5425 actual values. Otherwise, we will restore only one
5426 register from the stack, since lowest will == highest in
5427 `pop_failure_point'. */
5428 unsigned dummy_low_reg, dummy_high_reg;
5429 unsigned char *pdummy;
5432 DEBUG_PRINT1 ("EXECUTING pop_failure_jump.\n");
5433 POP_FAILURE_POINT (sdummy, pdummy,
5434 dummy_low_reg, dummy_high_reg,
5435 reg_dummy, reg_dummy, reg_info_dummy);
5437 /* Note fall through. */
5440 /* Unconditionally jump (without popping any failure points). */
5443 #if defined (WINDOWSNT) && defined (emacs)
5446 EXTRACT_NUMBER_AND_INCR (mcnt, p); /* Get the amount to jump. */
5447 DEBUG_PRINT2 ("EXECUTING jump %d ", mcnt);
5448 p += mcnt; /* Do the jump. */
5449 DEBUG_PRINT2 ("(to 0x%x).\n", p);
5453 /* We need this opcode so we can detect where alternatives end
5454 in `group_match_null_string_p' et al. */
5456 DEBUG_PRINT1 ("EXECUTING jump_past_alt.\n");
5457 goto unconditional_jump;
5460 /* Normally, the on_failure_jump pushes a failure point, which
5461 then gets popped at pop_failure_jump. We will end up at
5462 pop_failure_jump, also, and with a pattern of, say, `a+', we
5463 are skipping over the on_failure_jump, so we have to push
5464 something meaningless for pop_failure_jump to pop. */
5465 case dummy_failure_jump:
5466 DEBUG_PRINT1 ("EXECUTING dummy_failure_jump.\n");
5467 /* It doesn't matter what we push for the string here. What
5468 the code at `fail' tests is the value for the pattern. */
5469 PUSH_FAILURE_POINT (0, 0, -2);
5470 goto unconditional_jump;
5473 /* At the end of an alternative, we need to push a dummy failure
5474 point in case we are followed by a `pop_failure_jump', because
5475 we don't want the failure point for the alternative to be
5476 popped. For example, matching `(a|ab)*' against `aab'
5477 requires that we match the `ab' alternative. */
5478 case push_dummy_failure:
5479 DEBUG_PRINT1 ("EXECUTING push_dummy_failure.\n");
5480 /* See comments just above at `dummy_failure_jump' about the
5482 PUSH_FAILURE_POINT (0, 0, -2);
5485 /* Have to succeed matching what follows at least n times.
5486 After that, handle like `on_failure_jump'. */
5488 EXTRACT_NUMBER (mcnt, p + 2);
5489 DEBUG_PRINT2 ("EXECUTING succeed_n %d.\n", mcnt);
5492 /* Originally, this is how many times we HAVE to succeed. */
5497 STORE_NUMBER_AND_INCR (p, mcnt);
5498 DEBUG_PRINT3 (" Setting 0x%x to %d.\n", p, mcnt);
5502 DEBUG_PRINT2 (" Setting two bytes from 0x%x to no_op.\n", p+2);
5503 p[2] = (unsigned char) no_op;
5504 p[3] = (unsigned char) no_op;
5510 EXTRACT_NUMBER (mcnt, p + 2);
5511 DEBUG_PRINT2 ("EXECUTING jump_n %d.\n", mcnt);
5513 /* Originally, this is how many times we CAN jump. */
5517 STORE_NUMBER (p + 2, mcnt);
5518 goto unconditional_jump;
5520 /* If don't have to jump any more, skip over the rest of command. */
5527 DEBUG_PRINT1 ("EXECUTING set_number_at.\n");
5529 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5531 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5532 DEBUG_PRINT3 (" Setting 0x%x to %d.\n", p1, mcnt);
5533 STORE_NUMBER (p1, mcnt);
5538 DEBUG_PRINT1 ("EXECUTING wordbound.\n");
5540 /* We SUCCEED in one of the following cases: */
5542 /* Case 1: D is at the beginning or the end of string. */
5543 if (AT_STRINGS_BEG (d) || AT_STRINGS_END (d))
5547 /* C1 is the character before D, S1 is the syntax of C1, C2
5548 is the character at D, and S2 is the syntax of C2. */
5550 int pos1 = PTR_TO_OFFSET (d - 1);
5553 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
5554 GET_CHAR_AFTER_2 (c2, d, string1, end1, string2, end2);
5556 charpos = SYNTAX_TABLE_BYTE_TO_CHAR (pos1);
5557 UPDATE_SYNTAX_TABLE (charpos);
5561 UPDATE_SYNTAX_TABLE_FORWARD (charpos + 1);
5565 if (/* Case 2: Only one of S1 and S2 is Sword. */
5566 ((s1 == Sword) != (s2 == Sword))
5567 /* Case 3: Both of S1 and S2 are Sword, and macro
5568 WORD_BOUNDARY_P (C1, C2) returns nonzero. */
5569 || ((s1 == Sword) && WORD_BOUNDARY_P (c1, c2)))
5575 DEBUG_PRINT1 ("EXECUTING notwordbound.\n");
5577 /* We FAIL in one of the following cases: */
5579 /* Case 1: D is at the beginning or the end of string. */
5580 if (AT_STRINGS_BEG (d) || AT_STRINGS_END (d))
5584 /* C1 is the character before D, S1 is the syntax of C1, C2
5585 is the character at D, and S2 is the syntax of C2. */
5587 int pos1 = PTR_TO_OFFSET (d - 1);
5590 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
5591 GET_CHAR_AFTER_2 (c2, d, string1, end1, string2, end2);
5593 charpos = SYNTAX_TABLE_BYTE_TO_CHAR (pos1);
5594 UPDATE_SYNTAX_TABLE (charpos);
5598 UPDATE_SYNTAX_TABLE_FORWARD (charpos + 1);
5602 if (/* Case 2: Only one of S1 and S2 is Sword. */
5603 ((s1 == Sword) != (s2 == Sword))
5604 /* Case 3: Both of S1 and S2 are Sword, and macro
5605 WORD_BOUNDARY_P (C1, C2) returns nonzero. */
5606 || ((s1 == Sword) && WORD_BOUNDARY_P (c1, c2)))
5612 DEBUG_PRINT1 ("EXECUTING wordbeg.\n");
5614 /* We FAIL in one of the following cases: */
5616 /* Case 1: D is at the end of string. */
5617 if (AT_STRINGS_END (d))
5621 /* C1 is the character before D, S1 is the syntax of C1, C2
5622 is the character at D, and S2 is the syntax of C2. */
5624 int pos1 = PTR_TO_OFFSET (d);
5627 GET_CHAR_AFTER_2 (c2, d, string1, end1, string2, end2);
5629 charpos = SYNTAX_TABLE_BYTE_TO_CHAR (pos1);
5630 UPDATE_SYNTAX_TABLE (charpos);
5634 /* Case 2: S2 is not Sword. */
5638 /* Case 3: D is not at the beginning of string ... */
5639 if (!AT_STRINGS_BEG (d))
5641 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
5643 UPDATE_SYNTAX_TABLE_BACKWARD (charpos - 1);
5647 /* ... and S1 is Sword, and WORD_BOUNDARY_P (C1, C2)
5649 if ((s1 == Sword) && !WORD_BOUNDARY_P (c1, c2))
5656 DEBUG_PRINT1 ("EXECUTING wordend.\n");
5658 /* We FAIL in one of the following cases: */
5660 /* Case 1: D is at the beginning of string. */
5661 if (AT_STRINGS_BEG (d))
5665 /* C1 is the character before D, S1 is the syntax of C1, C2
5666 is the character at D, and S2 is the syntax of C2. */
5668 int pos1 = PTR_TO_OFFSET (d);
5671 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
5673 charpos = SYNTAX_TABLE_BYTE_TO_CHAR (pos1 - 1);
5674 UPDATE_SYNTAX_TABLE (charpos);
5678 /* Case 2: S1 is not Sword. */
5682 /* Case 3: D is not at the end of string ... */
5683 if (!AT_STRINGS_END (d))
5685 GET_CHAR_AFTER_2 (c2, d, string1, end1, string2, end2);
5687 UPDATE_SYNTAX_TABLE_FORWARD (charpos);
5691 /* ... and S2 is Sword, and WORD_BOUNDARY_P (C1, C2)
5693 if ((s2 == Sword) && !WORD_BOUNDARY_P (c1, c2))
5701 DEBUG_PRINT1 ("EXECUTING before_dot.\n");
5702 if (PTR_BYTE_POS ((unsigned char *) d) >= PT_BYTE)
5707 DEBUG_PRINT1 ("EXECUTING at_dot.\n");
5708 if (PTR_BYTE_POS ((unsigned char *) d) != PT_BYTE)
5713 DEBUG_PRINT1 ("EXECUTING after_dot.\n");
5714 if (PTR_BYTE_POS ((unsigned char *) d) <= PT_BYTE)
5719 DEBUG_PRINT2 ("EXECUTING syntaxspec %d.\n", mcnt);
5724 DEBUG_PRINT1 ("EXECUTING Emacs wordchar.\n");
5730 int pos1 = SYNTAX_TABLE_BYTE_TO_CHAR (PTR_TO_OFFSET (d));
5731 UPDATE_SYNTAX_TABLE (pos1);
5738 /* we must concern about multibyte form, ... */
5739 c = STRING_CHAR_AND_LENGTH (d, dend - d, len);
5741 /* everything should be handled as ASCII, even though it
5742 looks like multibyte form. */
5745 if (SYNTAX (c) != (enum syntaxcode) mcnt)
5749 SET_REGS_MATCHED ();
5753 DEBUG_PRINT2 ("EXECUTING notsyntaxspec %d.\n", mcnt);
5755 goto matchnotsyntax;
5758 DEBUG_PRINT1 ("EXECUTING Emacs notwordchar.\n");
5764 int pos1 = SYNTAX_TABLE_BYTE_TO_CHAR (PTR_TO_OFFSET (d));
5765 UPDATE_SYNTAX_TABLE (pos1);
5772 c = STRING_CHAR_AND_LENGTH (d, dend - d, len);
5776 if (SYNTAX (c) == (enum syntaxcode) mcnt)
5780 SET_REGS_MATCHED ();
5784 DEBUG_PRINT2 ("EXECUTING categoryspec %d.\n", *p);
5791 c = STRING_CHAR_AND_LENGTH (d, dend - d, len);
5795 if (!CHAR_HAS_CATEGORY (c, mcnt))
5799 SET_REGS_MATCHED ();
5802 case notcategoryspec:
5803 DEBUG_PRINT2 ("EXECUTING notcategoryspec %d.\n", *p);
5810 c = STRING_CHAR_AND_LENGTH (d, dend - d, len);
5814 if (CHAR_HAS_CATEGORY (c, mcnt))
5818 SET_REGS_MATCHED ();
5821 #else /* not emacs */
5823 DEBUG_PRINT1 ("EXECUTING non-Emacs wordchar.\n");
5825 if (!WORDCHAR_P (d))
5827 SET_REGS_MATCHED ();
5832 DEBUG_PRINT1 ("EXECUTING non-Emacs notwordchar.\n");
5836 SET_REGS_MATCHED ();
5839 #endif /* not emacs */
5844 continue; /* Successfully executed one pattern command; keep going. */
5847 /* We goto here if a matching operation fails. */
5849 #if defined (WINDOWSNT) && defined (emacs)
5852 if (!FAIL_STACK_EMPTY ())
5853 { /* A restart point is known. Restore to that state. */
5854 DEBUG_PRINT1 ("\nFAIL:\n");
5855 POP_FAILURE_POINT (d, p,
5856 lowest_active_reg, highest_active_reg,
5857 regstart, regend, reg_info);
5859 /* If this failure point is a dummy, try the next one. */
5863 /* If we failed to the end of the pattern, don't examine *p. */
5867 boolean is_a_jump_n = false;
5869 /* If failed to a backwards jump that's part of a repetition
5870 loop, need to pop this failure point and use the next one. */
5871 switch ((re_opcode_t) *p)
5875 case maybe_pop_jump:
5876 case pop_failure_jump:
5879 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
5882 if ((is_a_jump_n && (re_opcode_t) *p1 == succeed_n)
5884 && (re_opcode_t) *p1 == on_failure_jump))
5892 if (d >= string1 && d <= end1)
5896 break; /* Matching at this starting point really fails. */
5900 goto restore_best_regs;
5904 return -1; /* Failure to match. */
5907 /* Subroutine definitions for re_match_2. */
5910 /* We are passed P pointing to a register number after a start_memory.
5912 Return true if the pattern up to the corresponding stop_memory can
5913 match the empty string, and false otherwise.
5915 If we find the matching stop_memory, sets P to point to one past its number.
5916 Otherwise, sets P to an undefined byte less than or equal to END.
5918 We don't handle duplicates properly (yet). */
5921 group_match_null_string_p (p, end, reg_info)
5922 unsigned char **p, *end;
5923 register_info_type *reg_info;
5926 /* Point to after the args to the start_memory. */
5927 unsigned char *p1 = *p + 2;
5931 /* Skip over opcodes that can match nothing, and return true or
5932 false, as appropriate, when we get to one that can't, or to the
5933 matching stop_memory. */
5935 switch ((re_opcode_t) *p1)
5937 /* Could be either a loop or a series of alternatives. */
5938 case on_failure_jump:
5940 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
5942 /* If the next operation is not a jump backwards in the
5947 /* Go through the on_failure_jumps of the alternatives,
5948 seeing if any of the alternatives cannot match nothing.
5949 The last alternative starts with only a jump,
5950 whereas the rest start with on_failure_jump and end
5951 with a jump, e.g., here is the pattern for `a|b|c':
5953 /on_failure_jump/0/6/exactn/1/a/jump_past_alt/0/6
5954 /on_failure_jump/0/6/exactn/1/b/jump_past_alt/0/3
5957 So, we have to first go through the first (n-1)
5958 alternatives and then deal with the last one separately. */
5961 /* Deal with the first (n-1) alternatives, which start
5962 with an on_failure_jump (see above) that jumps to right
5963 past a jump_past_alt. */
5965 while ((re_opcode_t) p1[mcnt-3] == jump_past_alt)
5967 /* `mcnt' holds how many bytes long the alternative
5968 is, including the ending `jump_past_alt' and
5971 if (!alt_match_null_string_p (p1, p1 + mcnt - 3,
5975 /* Move to right after this alternative, including the
5979 /* Break if it's the beginning of an n-th alternative
5980 that doesn't begin with an on_failure_jump. */
5981 if ((re_opcode_t) *p1 != on_failure_jump)
5984 /* Still have to check that it's not an n-th
5985 alternative that starts with an on_failure_jump. */
5987 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
5988 if ((re_opcode_t) p1[mcnt-3] != jump_past_alt)
5990 /* Get to the beginning of the n-th alternative. */
5996 /* Deal with the last alternative: go back and get number
5997 of the `jump_past_alt' just before it. `mcnt' contains
5998 the length of the alternative. */
5999 EXTRACT_NUMBER (mcnt, p1 - 2);
6001 if (!alt_match_null_string_p (p1, p1 + mcnt, reg_info))
6004 p1 += mcnt; /* Get past the n-th alternative. */
6010 assert (p1[1] == **p);
6016 if (!common_op_match_null_string_p (&p1, end, reg_info))
6019 } /* while p1 < end */
6022 } /* group_match_null_string_p */
6025 /* Similar to group_match_null_string_p, but doesn't deal with alternatives:
6026 It expects P to be the first byte of a single alternative and END one
6027 byte past the last. The alternative can contain groups. */
6030 alt_match_null_string_p (p, end, reg_info)
6031 unsigned char *p, *end;
6032 register_info_type *reg_info;
6035 unsigned char *p1 = p;
6039 /* Skip over opcodes that can match nothing, and break when we get
6040 to one that can't. */
6042 switch ((re_opcode_t) *p1)
6045 case on_failure_jump:
6047 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
6052 if (!common_op_match_null_string_p (&p1, end, reg_info))
6055 } /* while p1 < end */
6058 } /* alt_match_null_string_p */
6061 /* Deals with the ops common to group_match_null_string_p and
6062 alt_match_null_string_p.
6064 Sets P to one after the op and its arguments, if any. */
6067 common_op_match_null_string_p (p, end, reg_info)
6068 unsigned char **p, *end;
6069 register_info_type *reg_info;
6074 unsigned char *p1 = *p;
6076 switch ((re_opcode_t) *p1++)
6096 assert (reg_no > 0 && reg_no <= MAX_REGNUM);
6097 ret = group_match_null_string_p (&p1, end, reg_info);
6099 /* Have to set this here in case we're checking a group which
6100 contains a group and a back reference to it. */
6102 if (REG_MATCH_NULL_STRING_P (reg_info[reg_no]) == MATCH_NULL_UNSET_VALUE)
6103 REG_MATCH_NULL_STRING_P (reg_info[reg_no]) = ret;
6109 /* If this is an optimized succeed_n for zero times, make the jump. */
6111 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
6119 /* Get to the number of times to succeed. */
6121 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
6126 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
6134 if (!REG_MATCH_NULL_STRING_P (reg_info[*p1]))
6142 /* All other opcodes mean we cannot match the empty string. */
6148 } /* common_op_match_null_string_p */
6151 /* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
6152 bytes; nonzero otherwise. */
6155 bcmp_translate (s1, s2, len, translate)
6156 unsigned char *s1, *s2;
6158 RE_TRANSLATE_TYPE translate;
6160 register unsigned char *p1 = s1, *p2 = s2;
6161 unsigned char *p1_end = s1 + len;
6162 unsigned char *p2_end = s2 + len;
6164 while (p1 != p1_end && p2 != p2_end)
6166 int p1_charlen, p2_charlen;
6169 p1_ch = STRING_CHAR_AND_LENGTH (p1, p1_end - p1, p1_charlen);
6170 p2_ch = STRING_CHAR_AND_LENGTH (p2, p2_end - p2, p2_charlen);
6172 if (RE_TRANSLATE (translate, p1_ch)
6173 != RE_TRANSLATE (translate, p2_ch))
6176 p1 += p1_charlen, p2 += p2_charlen;
6179 if (p1 != p1_end || p2 != p2_end)
6185 /* Entry points for GNU code. */
6187 /* re_compile_pattern is the GNU regular expression compiler: it
6188 compiles PATTERN (of length SIZE) and puts the result in BUFP.
6189 Returns 0 if the pattern was valid, otherwise an error string.
6191 Assumes the `allocated' (and perhaps `buffer') and `translate' fields
6192 are set in BUFP on entry.
6194 We call regex_compile to do the actual compilation. */
6197 re_compile_pattern (pattern, length, bufp)
6198 const char *pattern;
6200 struct re_pattern_buffer *bufp;
6204 /* GNU code is written to assume at least RE_NREGS registers will be set
6205 (and at least one extra will be -1). */
6206 bufp->regs_allocated = REGS_UNALLOCATED;
6208 /* And GNU code determines whether or not to get register information
6209 by passing null for the REGS argument to re_match, etc., not by
6213 /* Match anchors at newline. */
6214 bufp->newline_anchor = 1;
6216 ret = regex_compile (pattern, length, re_syntax_options, bufp);
6220 return gettext (re_error_msgid[(int) ret]);
6223 /* Entry points compatible with 4.2 BSD regex library. We don't define
6224 them unless specifically requested. */
6226 #if defined (_REGEX_RE_COMP) || defined (_LIBC)
6228 /* BSD has one and only one pattern buffer. */
6229 static struct re_pattern_buffer re_comp_buf;
6233 /* Make these definitions weak in libc, so POSIX programs can redefine
6234 these names if they don't use our functions, and still use
6235 regcomp/regexec below without link errors. */
6245 if (!re_comp_buf.buffer)
6246 return gettext ("No previous regular expression");
6250 if (!re_comp_buf.buffer)
6252 re_comp_buf.buffer = (unsigned char *) malloc (200);
6253 if (re_comp_buf.buffer == NULL)
6254 return gettext (re_error_msgid[(int) REG_ESPACE]);
6255 re_comp_buf.allocated = 200;
6257 re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH);
6258 if (re_comp_buf.fastmap == NULL)
6259 return gettext (re_error_msgid[(int) REG_ESPACE]);
6262 /* Since `re_exec' always passes NULL for the `regs' argument, we
6263 don't need to initialize the pattern buffer fields which affect it. */
6265 /* Match anchors at newlines. */
6266 re_comp_buf.newline_anchor = 1;
6268 ret = regex_compile (s, strlen (s), re_syntax_options, &re_comp_buf);
6273 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6274 return (char *) gettext (re_error_msgid[(int) ret]);
6285 const int len = strlen (s);
6287 0 <= re_search (&re_comp_buf, s, len, 0, len, (struct re_registers *) 0);
6289 #endif /* _REGEX_RE_COMP */
6291 /* POSIX.2 functions. Don't define these for Emacs. */
6295 /* regcomp takes a regular expression as a string and compiles it.
6297 PREG is a regex_t *. We do not expect any fields to be initialized,
6298 since POSIX says we shouldn't. Thus, we set
6300 `buffer' to the compiled pattern;
6301 `used' to the length of the compiled pattern;
6302 `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
6303 REG_EXTENDED bit in CFLAGS is set; otherwise, to
6304 RE_SYNTAX_POSIX_BASIC;
6305 `newline_anchor' to REG_NEWLINE being set in CFLAGS;
6306 `fastmap' and `fastmap_accurate' to zero;
6307 `re_nsub' to the number of subexpressions in PATTERN.
6309 PATTERN is the address of the pattern string.
6311 CFLAGS is a series of bits which affect compilation.
6313 If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
6314 use POSIX basic syntax.
6316 If REG_NEWLINE is set, then . and [^...] don't match newline.
6317 Also, regexec will try a match beginning after every newline.
6319 If REG_ICASE is set, then we considers upper- and lowercase
6320 versions of letters to be equivalent when matching.
6322 If REG_NOSUB is set, then when PREG is passed to regexec, that
6323 routine will report only success or failure, and nothing about the
6326 It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
6327 the return codes and their meanings.) */
6330 regcomp (preg, pattern, cflags)
6332 const char *pattern;
6337 = (cflags & REG_EXTENDED) ?
6338 RE_SYNTAX_POSIX_EXTENDED : RE_SYNTAX_POSIX_BASIC;
6340 /* regex_compile will allocate the space for the compiled pattern. */
6342 preg->allocated = 0;
6345 /* Don't bother to use a fastmap when searching. This simplifies the
6346 REG_NEWLINE case: if we used a fastmap, we'd have to put all the
6347 characters after newlines into the fastmap. This way, we just try
6351 if (cflags & REG_ICASE)
6356 = (RE_TRANSLATE_TYPE) malloc (CHAR_SET_SIZE
6357 * sizeof (*(RE_TRANSLATE_TYPE)0));
6358 if (preg->translate == NULL)
6359 return (int) REG_ESPACE;
6361 /* Map uppercase characters to corresponding lowercase ones. */
6362 for (i = 0; i < CHAR_SET_SIZE; i++)
6363 preg->translate[i] = ISUPPER (i) ? tolower (i) : i;
6366 preg->translate = NULL;
6368 /* If REG_NEWLINE is set, newlines are treated differently. */
6369 if (cflags & REG_NEWLINE)
6370 { /* REG_NEWLINE implies neither . nor [^...] match newline. */
6371 syntax &= ~RE_DOT_NEWLINE;
6372 syntax |= RE_HAT_LISTS_NOT_NEWLINE;
6373 /* It also changes the matching behavior. */
6374 preg->newline_anchor = 1;
6377 preg->newline_anchor = 0;
6379 preg->no_sub = !!(cflags & REG_NOSUB);
6381 /* POSIX says a null character in the pattern terminates it, so we
6382 can use strlen here in compiling the pattern. */
6383 ret = regex_compile (pattern, strlen (pattern), syntax, preg);
6385 /* POSIX doesn't distinguish between an unmatched open-group and an
6386 unmatched close-group: both are REG_EPAREN. */
6387 if (ret == REG_ERPAREN) ret = REG_EPAREN;
6393 /* regexec searches for a given pattern, specified by PREG, in the
6396 If NMATCH is zero or REG_NOSUB was set in the cflags argument to
6397 `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
6398 least NMATCH elements, and we set them to the offsets of the
6399 corresponding matched substrings.
6401 EFLAGS specifies `execution flags' which affect matching: if
6402 REG_NOTBOL is set, then ^ does not match at the beginning of the
6403 string; if REG_NOTEOL is set, then $ does not match at the end.
6405 We return 0 if we find a match and REG_NOMATCH if not. */
6408 regexec (preg, string, nmatch, pmatch, eflags)
6409 const regex_t *preg;
6412 regmatch_t pmatch[];
6416 struct re_registers regs;
6417 regex_t private_preg;
6418 int len = strlen (string);
6419 boolean want_reg_info = !preg->no_sub && nmatch > 0;
6421 private_preg = *preg;
6423 private_preg.not_bol = !!(eflags & REG_NOTBOL);
6424 private_preg.not_eol = !!(eflags & REG_NOTEOL);
6426 /* The user has told us exactly how many registers to return
6427 information about, via `nmatch'. We have to pass that on to the
6428 matching routines. */
6429 private_preg.regs_allocated = REGS_FIXED;
6433 regs.num_regs = nmatch;
6434 regs.start = TALLOC (nmatch, regoff_t);
6435 regs.end = TALLOC (nmatch, regoff_t);
6436 if (regs.start == NULL || regs.end == NULL)
6437 return (int) REG_NOMATCH;
6440 /* Perform the searching operation. */
6441 ret = re_search (&private_preg, string, len,
6442 /* start: */ 0, /* range: */ len,
6443 want_reg_info ? ®s : (struct re_registers *) 0);
6445 /* Copy the register information to the POSIX structure. */
6452 for (r = 0; r < nmatch; r++)
6454 pmatch[r].rm_so = regs.start[r];
6455 pmatch[r].rm_eo = regs.end[r];
6459 /* If we needed the temporary register info, free the space now. */
6464 /* We want zero return to mean success, unlike `re_search'. */
6465 return ret >= 0 ? (int) REG_NOERROR : (int) REG_NOMATCH;
6469 /* Returns a message corresponding to an error code, ERRCODE, returned
6470 from either regcomp or regexec. We don't use PREG here. */
6473 regerror (errcode, preg, errbuf, errbuf_size)
6475 const regex_t *preg;
6483 || errcode >= (sizeof (re_error_msgid) / sizeof (re_error_msgid[0])))
6484 /* Only error codes returned by the rest of the code should be passed
6485 to this routine. If we are given anything else, or if other regex
6486 code generates an invalid error code, then the program has a bug.
6487 Dump core so we can fix it. */
6490 msg = gettext (re_error_msgid[errcode]);
6492 msg_size = strlen (msg) + 1; /* Includes the null. */
6494 if (errbuf_size != 0)
6496 if (msg_size > errbuf_size)
6498 strncpy (errbuf, msg, errbuf_size - 1);
6499 errbuf[errbuf_size - 1] = 0;
6502 strcpy (errbuf, msg);
6509 /* Free dynamically allocated space used by PREG. */
6515 if (preg->buffer != NULL)
6516 free (preg->buffer);
6517 preg->buffer = NULL;
6519 preg->allocated = 0;
6522 if (preg->fastmap != NULL)
6523 free (preg->fastmap);
6524 preg->fastmap = NULL;
6525 preg->fastmap_accurate = 0;
6527 if (preg->translate != NULL)
6528 free (preg->translate);
6529 preg->translate = NULL;
6532 #endif /* not emacs */