1 /* Extended regular expression matching and search library,
2 version 0.12, extended for XEmacs.
3 (Implements POSIX draft P10003.2/D11.2, except for
4 internationalization features.)
6 Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
7 Copyright (C) 1995 Sun Microsystems, Inc.
8 Copyright (C) 1995 Ben Wing.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; see the file COPYING. If not, write to
22 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
25 /* Synched up with: FSF 19.29. */
27 /* Changes made for XEmacs:
29 (1) the REGEX_BEGLINE_CHECK code from the XEmacs v18 regex routines
30 was added. This causes a huge speedup in font-locking.
31 (2) Rel-alloc is disabled when the MMAP version of rel-alloc is
32 being used, because it's too slow -- all those calls to mmap()
33 add humongous overhead.
34 (3) Lots and lots of changes for Mule. They are bracketed by
35 `#ifdef MULE' or with comments that have `XEmacs' in them.
42 #ifndef REGISTER /* Rigidly enforced as of 20.3 */
50 /* We assume non-Mule if emacs isn't defined. */
55 /* We need this for `regex.h', and perhaps for the Emacs include files. */
56 #include <sys/types.h>
58 /* This is for other GNU distributions with internationalized messages. */
59 #if defined (I18N3) && (defined (HAVE_LIBINTL_H) || defined (_LIBC))
62 # define gettext(msgid) (msgid)
65 /* XEmacs: define this to add in a speedup for patterns anchored at
66 the beginning of a line. Keep the ifdefs so that it's easier to
67 tell where/why this code has diverged from v19. */
68 #define REGEX_BEGLINE_CHECK
70 /* XEmacs: the current mmap-based ralloc handles small blocks very
71 poorly, so we disable it here. */
73 #if (defined (REL_ALLOC) && defined (HAVE_MMAP)) || defined(DOUG_LEA_MALLOC)
77 /* The `emacs' switch turns on certain matching commands
78 that make sense only in Emacs. */
85 #if (defined (DEBUG_XEMACS) && !defined (DEBUG))
91 Lisp_Object Vthe_lisp_rangetab;
94 complex_vars_of_regex (void)
96 Vthe_lisp_rangetab = Fmake_range_table ();
97 staticpro (&Vthe_lisp_rangetab);
103 complex_vars_of_regex (void)
109 #define RE_TRANSLATE(ch) TRT_TABLE_OF (translate, (Emchar) ch)
110 #define TRANSLATE_P(tr) (!NILP (tr))
112 #else /* not emacs */
114 /* If we are not linking with Emacs proper,
115 we can't use the relocating allocator
116 even if config.h says that we can. */
119 #if defined (STDC_HEADERS) || defined (_LIBC)
126 #define charptr_emchar(str) ((Emchar) (str)[0])
128 #if (LONGBITS > INTBITS)
129 # define EMACS_INT long
131 # define EMACS_INT int
136 #define INC_CHARPTR(p) ((p)++)
137 #define DEC_CHARPTR(p) ((p)--)
141 /* Define the syntax stuff for \<, \>, etc. */
143 /* This must be nonzero for the wordchar and notwordchar pattern
144 commands in re_match_2. */
151 extern char *re_syntax_table;
153 #else /* not SYNTAX_TABLE */
155 /* How many characters in the character set. */
156 #define CHAR_SET_SIZE 256
158 static char re_syntax_table[CHAR_SET_SIZE];
161 init_syntax_once (void)
167 const char *word_syntax_chars =
168 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
170 memset (re_syntax_table, 0, sizeof (re_syntax_table));
172 while (*word_syntax_chars)
173 re_syntax_table[(unsigned int)(*word_syntax_chars++)] = Sword;
179 #endif /* SYNTAX_TABLE */
181 #define SYNTAX_UNSAFE(ignored, c) re_syntax_table[c]
183 #define RE_TRANSLATE(c) translate[(unsigned char) (c)]
184 #define TRANSLATE_P(tr) tr
188 /* Under XEmacs, this is needed because we don't define it elsewhere. */
189 #ifdef SWITCH_ENUM_BUG
190 #define SWITCH_ENUM_CAST(x) ((int)(x))
192 #define SWITCH_ENUM_CAST(x) (x)
196 /* Get the interface, including the syntax bits. */
199 /* isalpha etc. are used for the character classes. */
202 /* Jim Meyering writes:
204 "... Some ctype macros are valid only for character codes that
205 isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
206 using /bin/cc or gcc but without giving an ansi option). So, all
207 ctype uses should be through macros like ISPRINT... If
208 STDC_HEADERS is defined, then autoconf has verified that the ctype
209 macros don't need to be guarded with references to isascii. ...
210 Defining isascii to 1 should let any compiler worth its salt
211 eliminate the && through constant folding." */
213 #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
214 #define ISASCII_1(c) 1
216 #define ISASCII_1(c) isascii(c)
220 /* The IS*() macros can be passed any character, including an extended
221 one. We need to make sure there are no crashes, which would occur
222 otherwise due to out-of-bounds array references. */
223 #define ISASCII(c) (((EMACS_UINT) (c)) < 0x100 && ISASCII_1 (c))
225 #define ISASCII(c) ISASCII_1 (c)
229 #define ISBLANK(c) (ISASCII (c) && isblank (c))
231 #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
234 #define ISGRAPH(c) (ISASCII (c) && isgraph (c))
236 #define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
239 #define ISPRINT(c) (ISASCII (c) && isprint (c))
240 #define ISDIGIT(c) (ISASCII (c) && isdigit (c))
241 #define ISALNUM(c) (ISASCII (c) && isalnum (c))
242 #define ISALPHA(c) (ISASCII (c) && isalpha (c))
243 #define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
244 #define ISLOWER(c) (ISASCII (c) && islower (c))
245 #define ISPUNCT(c) (ISASCII (c) && ispunct (c))
246 #define ISSPACE(c) (ISASCII (c) && isspace (c))
247 #define ISUPPER(c) (ISASCII (c) && isupper (c))
248 #define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
251 #define NULL (void *)0
254 /* We remove any previous definition of `SIGN_EXTEND_CHAR',
255 since ours (we hope) works properly with all combinations of
256 machines, compilers, `char' and `unsigned char' argument types.
257 (Per Bothner suggested the basic approach.) */
258 #undef SIGN_EXTEND_CHAR
260 #define SIGN_EXTEND_CHAR(c) ((signed char) (c))
261 #else /* not __STDC__ */
262 /* As in Harbison and Steele. */
263 #define SIGN_EXTEND_CHAR(c) ((((unsigned char) (c)) ^ 128) - 128)
266 /* Should we use malloc or alloca? If REGEX_MALLOC is not defined, we
267 use `alloca' instead of `malloc'. This is because using malloc in
268 re_search* or re_match* could cause memory leaks when C-g is used in
269 Emacs; also, malloc is slower and causes storage fragmentation. On
270 the other hand, malloc is more portable, and easier to debug.
272 Because we sometimes use alloca, some routines have to be macros,
273 not functions -- `alloca'-allocated space disappears at the end of the
274 function it is called in. */
278 #define REGEX_ALLOCATE malloc
279 #define REGEX_REALLOCATE(source, osize, nsize) realloc (source, nsize)
280 #define REGEX_FREE free
282 #else /* not REGEX_MALLOC */
284 /* Emacs already defines alloca, sometimes. */
287 /* Make alloca work the best possible way. */
289 #define alloca __builtin_alloca
290 #else /* not __GNUC__ */
293 #else /* not __GNUC__ or HAVE_ALLOCA_H */
294 #ifndef _AIX /* Already did AIX, up at the top. */
296 #endif /* not _AIX */
297 #endif /* HAVE_ALLOCA_H */
298 #endif /* __GNUC__ */
300 #endif /* not alloca */
302 #define REGEX_ALLOCATE alloca
304 /* Assumes a `char *destination' variable. */
305 #define REGEX_REALLOCATE(source, osize, nsize) \
306 (destination = (char *) alloca (nsize), \
307 memmove (destination, source, osize), \
310 /* No need to do anything to free, after alloca. */
311 #define REGEX_FREE(arg) ((void)0) /* Do nothing! But inhibit gcc warning. */
313 #endif /* REGEX_MALLOC */
315 /* Define how to allocate the failure stack. */
318 #define REGEX_ALLOCATE_STACK(size) \
319 r_alloc ((char **) &failure_stack_ptr, (size))
320 #define REGEX_REALLOCATE_STACK(source, osize, nsize) \
321 r_re_alloc ((char **) &failure_stack_ptr, (nsize))
322 #define REGEX_FREE_STACK(ptr) \
323 r_alloc_free ((void **) &failure_stack_ptr)
325 #else /* not REL_ALLOC */
329 #define REGEX_ALLOCATE_STACK malloc
330 #define REGEX_REALLOCATE_STACK(source, osize, nsize) realloc (source, nsize)
331 #define REGEX_FREE_STACK free
333 #else /* not REGEX_MALLOC */
335 #define REGEX_ALLOCATE_STACK alloca
337 #define REGEX_REALLOCATE_STACK(source, osize, nsize) \
338 REGEX_REALLOCATE (source, osize, nsize)
339 /* No need to explicitly free anything. */
340 #define REGEX_FREE_STACK(arg)
342 #endif /* REGEX_MALLOC */
343 #endif /* REL_ALLOC */
346 /* True if `size1' is non-NULL and PTR is pointing anywhere inside
347 `string1' or just past its end. This works if PTR is NULL, which is
349 #define FIRST_STRING_P(ptr) \
350 (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
352 /* (Re)Allocate N items of type T using malloc, or fail. */
353 #define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t)))
354 #define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
355 #define RETALLOC_IF(addr, n, t) \
356 if (addr) RETALLOC((addr), (n), t); else (addr) = TALLOC ((n), t)
357 #define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
359 #define BYTEWIDTH 8 /* In bits. */
361 #define STREQ(s1, s2) (strcmp (s1, s2) == 0)
365 #define MAX(a, b) ((a) > (b) ? (a) : (b))
366 #define MIN(a, b) ((a) < (b) ? (a) : (b))
368 /* Type of source-pattern and string chars. */
369 typedef const unsigned char re_char;
371 typedef char boolean;
376 /* These are the command codes that appear in compiled regular
377 expressions. Some opcodes are followed by argument bytes. A
378 command code can specify any interpretation whatsoever for its
379 arguments. Zero bytes may appear in the compiled regular expression. */
385 /* Succeed right away--no more backtracking. */
388 /* Followed by one byte giving n, then by n literal bytes. */
391 /* Matches any (more or less) character. */
394 /* Matches any one char belonging to specified set. First
395 following byte is number of bitmap bytes. Then come bytes
396 for a bitmap saying which chars are in. Bits in each byte
397 are ordered low-bit-first. A character is in the set if its
398 bit is 1. A character too large to have a bit in the map is
399 automatically not in the set. */
402 /* Same parameters as charset, but match any character that is
403 not one of those specified. */
406 /* Start remembering the text that is matched, for storing in a
407 register. Followed by one byte with the register number, in
408 the range 0 to one less than the pattern buffer's re_nsub
409 field. Then followed by one byte with the number of groups
410 inner to this one. (This last has to be part of the
411 start_memory only because we need it in the on_failure_jump
415 /* Stop remembering the text that is matched and store it in a
416 memory register. Followed by one byte with the register
417 number, in the range 0 to one less than `re_nsub' in the
418 pattern buffer, and one byte with the number of inner groups,
419 just like `start_memory'. (We need the number of inner
420 groups here because we don't have any easy way of finding the
421 corresponding start_memory when we're at a stop_memory.) */
424 /* Match a duplicate of something remembered. Followed by one
425 byte containing the register number. */
428 /* Fail unless at beginning of line. */
431 /* Fail unless at end of line. */
434 /* Succeeds if at beginning of buffer (if emacs) or at beginning
435 of string to be matched (if not). */
438 /* Analogously, for end of buffer/string. */
441 /* Followed by two byte relative address to which to jump. */
444 /* Same as jump, but marks the end of an alternative. */
447 /* Followed by two-byte relative address of place to resume at
448 in case of failure. */
451 /* Like on_failure_jump, but pushes a placeholder instead of the
452 current string position when executed. */
453 on_failure_keep_string_jump,
455 /* Throw away latest failure point and then jump to following
456 two-byte relative address. */
459 /* Change to pop_failure_jump if know won't have to backtrack to
460 match; otherwise change to jump. This is used to jump
461 back to the beginning of a repeat. If what follows this jump
462 clearly won't match what the repeat does, such that we can be
463 sure that there is no use backtracking out of repetitions
464 already matched, then we change it to a pop_failure_jump.
465 Followed by two-byte address. */
468 /* Jump to following two-byte address, and push a dummy failure
469 point. This failure point will be thrown away if an attempt
470 is made to use it for a failure. A `+' construct makes this
471 before the first repeat. Also used as an intermediary kind
472 of jump when compiling an alternative. */
475 /* Push a dummy failure point and continue. Used at the end of
479 /* Followed by two-byte relative address and two-byte number n.
480 After matching N times, jump to the address upon failure. */
483 /* Followed by two-byte relative address, and two-byte number n.
484 Jump to the address N times, then fail. */
487 /* Set the following two-byte relative address to the
488 subsequent two-byte number. The address *includes* the two
492 wordchar, /* Matches any word-constituent character. */
493 notwordchar, /* Matches any char that is not a word-constituent. */
495 wordbeg, /* Succeeds if at word beginning. */
496 wordend, /* Succeeds if at word end. */
498 wordbound, /* Succeeds if at a word boundary. */
499 notwordbound /* Succeeds if not at a word boundary. */
502 ,before_dot, /* Succeeds if before point. */
503 at_dot, /* Succeeds if at point. */
504 after_dot, /* Succeeds if after point. */
506 /* Matches any character whose syntax is specified. Followed by
507 a byte which contains a syntax code, e.g., Sword. */
510 /* Matches any character whose syntax is not that specified. */
516 /* need extra stuff to be able to properly work with XEmacs/Mule
517 characters (which may take up more than one byte) */
519 ,charset_mule, /* Matches any character belonging to specified set.
520 The set is stored in "unified range-table
521 format"; see rangetab.c. Unlike the `charset'
522 opcode, this can handle arbitrary characters. */
524 charset_mule_not /* Same parameters as charset_mule, but match any
525 character that is not one of those specified. */
527 /* 97/2/17 jhod: The following two were merged back in from the Mule
528 2.3 code to enable some language specific processing */
529 ,categoryspec, /* Matches entries in the character category tables */
530 notcategoryspec /* The opposite of the above */
535 /* Common operations on the compiled pattern. */
537 /* Store NUMBER in two contiguous bytes starting at DESTINATION. */
539 #define STORE_NUMBER(destination, number) \
541 (destination)[0] = (number) & 0377; \
542 (destination)[1] = (number) >> 8; \
545 /* Same as STORE_NUMBER, except increment DESTINATION to
546 the byte after where the number is stored. Therefore, DESTINATION
547 must be an lvalue. */
549 #define STORE_NUMBER_AND_INCR(destination, number) \
551 STORE_NUMBER (destination, number); \
552 (destination) += 2; \
555 /* Put into DESTINATION a number stored in two contiguous bytes starting
558 #define EXTRACT_NUMBER(destination, source) \
560 (destination) = *(source) & 0377; \
561 (destination) += SIGN_EXTEND_CHAR (*((source) + 1)) << 8; \
566 extract_number (int *dest, re_char *source)
568 int temp = SIGN_EXTEND_CHAR (*(source + 1));
569 *dest = *source & 0377;
573 #ifndef EXTRACT_MACROS /* To debug the macros. */
574 #undef EXTRACT_NUMBER
575 #define EXTRACT_NUMBER(dest, src) extract_number (&dest, src)
576 #endif /* not EXTRACT_MACROS */
580 /* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
581 SOURCE must be an lvalue. */
583 #define EXTRACT_NUMBER_AND_INCR(destination, source) \
585 EXTRACT_NUMBER (destination, source); \
591 extract_number_and_incr (int *destination, unsigned char **source)
593 extract_number (destination, *source);
597 #ifndef EXTRACT_MACROS
598 #undef EXTRACT_NUMBER_AND_INCR
599 #define EXTRACT_NUMBER_AND_INCR(dest, src) \
600 extract_number_and_incr (&dest, &src)
601 #endif /* not EXTRACT_MACROS */
605 /* If DEBUG is defined, Regex prints many voluminous messages about what
606 it is doing (if the variable `debug' is nonzero). If linked with the
607 main program in `iregex.c', you can enter patterns and strings
608 interactively. And if linked with the main program in `main.c' and
609 the other test files, you can run the already-written tests. */
613 /* We use standard I/O for debugging. */
617 /* XEmacs provides its own version of assert() */
618 /* It is useful to test things that ``must'' be true when debugging. */
622 static int debug = 0;
624 #define DEBUG_STATEMENT(e) e
625 #define DEBUG_PRINT1(x) if (debug) printf (x)
626 #define DEBUG_PRINT2(x1, x2) if (debug) printf (x1, x2)
627 #define DEBUG_PRINT3(x1, x2, x3) if (debug) printf (x1, x2, x3)
628 #define DEBUG_PRINT4(x1, x2, x3, x4) if (debug) printf (x1, x2, x3, x4)
629 #define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) \
630 if (debug) print_partial_compiled_pattern (s, e)
631 #define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \
632 if (debug) print_double_string (w, s1, sz1, s2, sz2)
635 /* Print the fastmap in human-readable form. */
638 print_fastmap (char *fastmap)
640 unsigned was_a_range = 0;
643 while (i < (1 << BYTEWIDTH))
649 while (i < (1 << BYTEWIDTH) && fastmap[i])
665 /* Print a compiled pattern string in human-readable form, starting at
666 the START pointer into it and ending just before the pointer END. */
669 print_partial_compiled_pattern (re_char *start, re_char *end)
672 unsigned char *p = (unsigned char *) start;
681 /* Loop over pattern commands. */
684 printf ("%ld:\t", (long)(p - start));
686 switch ((re_opcode_t) *p++)
694 printf ("/exactn/%d", mcnt);
705 printf ("/start_memory/%d/%d", mcnt, *p++);
710 printf ("/stop_memory/%d/%d", mcnt, *p++);
714 printf ("/duplicate/%d", *p++);
724 REGISTER int c, last = -100;
725 REGISTER int in_range = 0;
727 printf ("/charset [%s",
728 (re_opcode_t) *(p - 1) == charset_not ? "^" : "");
730 assert (p + *p < pend);
732 for (c = 0; c < 256; c++)
733 if (((unsigned char) (c / 8) < *p)
734 && (p[1 + (c/8)] & (1 << (c % 8))))
736 /* Are we starting a range? */
737 if (last + 1 == c && ! in_range)
742 /* Have we broken a range? */
743 else if (last + 1 != c && in_range)
766 case charset_mule_not:
770 printf ("/charset_mule [%s",
771 (re_opcode_t) *(p - 1) == charset_mule_not ? "^" : "");
772 nentries = unified_range_table_nentries (p);
773 for (i = 0; i < nentries; i++)
775 EMACS_INT first, last;
776 Lisp_Object dummy_val;
778 unified_range_table_get_range (p, i, &first, &last,
783 printf ("(0x%lx)", (long)first);
790 printf ("(0x%lx)", (long)last);
794 p += unified_range_table_bytes_used (p);
807 case on_failure_jump:
808 extract_number_and_incr (&mcnt, &p);
809 printf ("/on_failure_jump to %ld", (long)(p + mcnt - start));
812 case on_failure_keep_string_jump:
813 extract_number_and_incr (&mcnt, &p);
814 printf ("/on_failure_keep_string_jump to %ld", (long)(p + mcnt - start));
817 case dummy_failure_jump:
818 extract_number_and_incr (&mcnt, &p);
819 printf ("/dummy_failure_jump to %ld", (long)(p + mcnt - start));
822 case push_dummy_failure:
823 printf ("/push_dummy_failure");
827 extract_number_and_incr (&mcnt, &p);
828 printf ("/maybe_pop_jump to %ld", (long)(p + mcnt - start));
831 case pop_failure_jump:
832 extract_number_and_incr (&mcnt, &p);
833 printf ("/pop_failure_jump to %ld", (long)(p + mcnt - start));
837 extract_number_and_incr (&mcnt, &p);
838 printf ("/jump_past_alt to %ld", (long)(p + mcnt - start));
842 extract_number_and_incr (&mcnt, &p);
843 printf ("/jump to %ld", (long)(p + mcnt - start));
847 extract_number_and_incr (&mcnt, &p);
848 extract_number_and_incr (&mcnt2, &p);
849 printf ("/succeed_n to %ld, %d times", (long)(p + mcnt - start), mcnt2);
853 extract_number_and_incr (&mcnt, &p);
854 extract_number_and_incr (&mcnt2, &p);
855 printf ("/jump_n to %ld, %d times", (long)(p + mcnt - start), mcnt2);
859 extract_number_and_incr (&mcnt, &p);
860 extract_number_and_incr (&mcnt2, &p);
861 printf ("/set_number_at location %ld to %d", (long)(p + mcnt - start), mcnt2);
865 printf ("/wordbound");
869 printf ("/notwordbound");
881 printf ("/before_dot");
889 printf ("/after_dot");
893 printf ("/syntaxspec");
895 printf ("/%d", mcnt);
899 printf ("/notsyntaxspec");
901 printf ("/%d", mcnt);
905 /* 97/2/17 jhod Mule category patch */
907 printf ("/categoryspec");
909 printf ("/%d", mcnt);
912 case notcategoryspec:
913 printf ("/notcategoryspec");
915 printf ("/%d", mcnt);
917 /* end of category patch */
922 printf ("/wordchar");
926 printf ("/notwordchar");
938 printf ("?%d", *(p-1));
944 printf ("%ld:\tend of pattern.\n", (long)(p - start));
949 print_compiled_pattern (struct re_pattern_buffer *bufp)
951 re_char *buffer = bufp->buffer;
953 print_partial_compiled_pattern (buffer, buffer + bufp->used);
954 printf ("%ld bytes used/%ld bytes allocated.\n", bufp->used,
957 if (bufp->fastmap_accurate && bufp->fastmap)
959 printf ("fastmap: ");
960 print_fastmap (bufp->fastmap);
963 printf ("re_nsub: %ld\t", (long)bufp->re_nsub);
964 printf ("regs_alloc: %d\t", bufp->regs_allocated);
965 printf ("can_be_null: %d\t", bufp->can_be_null);
966 printf ("newline_anchor: %d\n", bufp->newline_anchor);
967 printf ("no_sub: %d\t", bufp->no_sub);
968 printf ("not_bol: %d\t", bufp->not_bol);
969 printf ("not_eol: %d\t", bufp->not_eol);
970 printf ("syntax: %d\n", bufp->syntax);
971 /* Perhaps we should print the translate table? */
972 /* and maybe the category table? */
977 print_double_string (re_char *where, re_char *string1, int size1,
978 re_char *string2, int size2)
984 unsigned int this_char;
986 if (FIRST_STRING_P (where))
988 for (this_char = where - string1; this_char < size1; this_char++)
989 putchar (string1[this_char]);
994 for (this_char = where - string2; this_char < size2; this_char++)
995 putchar (string2[this_char]);
999 #else /* not DEBUG */
1004 #define DEBUG_STATEMENT(e)
1005 #define DEBUG_PRINT1(x)
1006 #define DEBUG_PRINT2(x1, x2)
1007 #define DEBUG_PRINT3(x1, x2, x3)
1008 #define DEBUG_PRINT4(x1, x2, x3, x4)
1009 #define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
1010 #define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
1014 /* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
1015 also be assigned to arbitrarily: each pattern buffer stores its own
1016 syntax, so it can be changed between regex compilations. */
1017 /* This has no initializer because initialized variables in Emacs
1018 become read-only after dumping. */
1019 reg_syntax_t re_syntax_options;
1022 /* Specify the precise syntax of regexps for compilation. This provides
1023 for compatibility for various utilities which historically have
1024 different, incompatible syntaxes.
1026 The argument SYNTAX is a bit mask comprised of the various bits
1027 defined in regex.h. We return the old syntax. */
1030 re_set_syntax (reg_syntax_t syntax)
1032 reg_syntax_t ret = re_syntax_options;
1034 re_syntax_options = syntax;
1038 /* This table gives an error message for each of the error codes listed
1039 in regex.h. Obviously the order here has to be same as there.
1040 POSIX doesn't require that we do anything for REG_NOERROR,
1041 but why not be nice? */
1043 static const char *re_error_msgid[] =
1045 "Success", /* REG_NOERROR */
1046 "No match", /* REG_NOMATCH */
1047 "Invalid regular expression", /* REG_BADPAT */
1048 "Invalid collation character", /* REG_ECOLLATE */
1049 "Invalid character class name", /* REG_ECTYPE */
1050 "Trailing backslash", /* REG_EESCAPE */
1051 "Invalid back reference", /* REG_ESUBREG */
1052 "Unmatched [ or [^", /* REG_EBRACK */
1053 "Unmatched ( or \\(", /* REG_EPAREN */
1054 "Unmatched \\{", /* REG_EBRACE */
1055 "Invalid content of \\{\\}", /* REG_BADBR */
1056 "Invalid range end", /* REG_ERANGE */
1057 "Memory exhausted", /* REG_ESPACE */
1058 "Invalid preceding regular expression", /* REG_BADRPT */
1059 "Premature end of regular expression", /* REG_EEND */
1060 "Regular expression too big", /* REG_ESIZE */
1061 "Unmatched ) or \\)", /* REG_ERPAREN */
1063 "Invalid syntax designator", /* REG_ESYNTAX */
1066 "Ranges may not span charsets", /* REG_ERANGESPAN */
1067 "Invalid category designator", /* REG_ECATEGORY */
1071 /* Avoiding alloca during matching, to placate r_alloc. */
1073 /* Define MATCH_MAY_ALLOCATE unless we need to make sure that the
1074 searching and matching functions should not call alloca. On some
1075 systems, alloca is implemented in terms of malloc, and if we're
1076 using the relocating allocator routines, then malloc could cause a
1077 relocation, which might (if the strings being searched are in the
1078 ralloc heap) shift the data out from underneath the regexp
1081 Here's another reason to avoid allocation: Emacs
1082 processes input from X in a signal handler; processing X input may
1083 call malloc; if input arrives while a matching routine is calling
1084 malloc, then we're scrod. But Emacs can't just block input while
1085 calling matching routines; then we don't notice interrupts when
1086 they come in. So, Emacs blocks input around all regexp calls
1087 except the matching calls, which it leaves unprotected, in the
1088 faith that they will not malloc. */
1090 /* Normally, this is fine. */
1091 #define MATCH_MAY_ALLOCATE
1093 /* When using GNU C, we are not REALLY using the C alloca, no matter
1094 what config.h may say. So don't take precautions for it. */
1099 /* The match routines may not allocate if (1) they would do it with malloc
1100 and (2) it's not safe for them to use malloc.
1101 Note that if REL_ALLOC is defined, matching would not use malloc for the
1102 failure stack, but we would still use it for the register vectors;
1103 so REL_ALLOC should not affect this. */
1104 #if (defined (C_ALLOCA) || defined (REGEX_MALLOC)) && defined (emacs)
1105 #undef MATCH_MAY_ALLOCATE
1109 /* Failure stack declarations and macros; both re_compile_fastmap and
1110 re_match_2 use a failure stack. These have to be macros because of
1111 REGEX_ALLOCATE_STACK. */
1114 /* Number of failure points for which to initially allocate space
1115 when matching. If this number is exceeded, we allocate more
1116 space, so it is not a hard limit. */
1117 #ifndef INIT_FAILURE_ALLOC
1118 #define INIT_FAILURE_ALLOC 5
1121 /* Roughly the maximum number of failure points on the stack. Would be
1122 exactly that if always used MAX_FAILURE_SPACE each time we failed.
1123 This is a variable only so users of regex can assign to it; we never
1124 change it ourselves. */
1125 #if defined (MATCH_MAY_ALLOCATE)
1126 /* 4400 was enough to cause a crash on Alpha OSF/1,
1127 whose default stack limit is 2mb. */
1128 int re_max_failures = 20000;
1130 int re_max_failures = 2000;
1133 union fail_stack_elt
1139 typedef union fail_stack_elt fail_stack_elt_t;
1143 fail_stack_elt_t *stack;
1145 size_t avail; /* Offset of next open position. */
1148 #define FAIL_STACK_EMPTY() (fail_stack.avail == 0)
1149 #define FAIL_STACK_PTR_EMPTY() (fail_stack_ptr->avail == 0)
1150 #define FAIL_STACK_FULL() (fail_stack.avail == fail_stack.size)
1153 /* Define macros to initialize and free the failure stack.
1154 Do `return -2' if the alloc fails. */
1156 #ifdef MATCH_MAY_ALLOCATE
1157 #define INIT_FAIL_STACK() \
1159 fail_stack.stack = (fail_stack_elt_t *) \
1160 REGEX_ALLOCATE_STACK (INIT_FAILURE_ALLOC * sizeof (fail_stack_elt_t)); \
1162 if (fail_stack.stack == NULL) \
1165 fail_stack.size = INIT_FAILURE_ALLOC; \
1166 fail_stack.avail = 0; \
1169 #define RESET_FAIL_STACK() REGEX_FREE_STACK (fail_stack.stack)
1171 #define INIT_FAIL_STACK() \
1173 fail_stack.avail = 0; \
1176 #define RESET_FAIL_STACK()
1180 /* Double the size of FAIL_STACK, up to approximately `re_max_failures' items.
1182 Return 1 if succeeds, and 0 if either ran out of memory
1183 allocating space for it or it was already too large.
1185 REGEX_REALLOCATE_STACK requires `destination' be declared. */
1187 #define DOUBLE_FAIL_STACK(fail_stack) \
1188 ((fail_stack).size > re_max_failures * MAX_FAILURE_ITEMS \
1190 : ((fail_stack).stack = (fail_stack_elt_t *) \
1191 REGEX_REALLOCATE_STACK ((fail_stack).stack, \
1192 (fail_stack).size * sizeof (fail_stack_elt_t), \
1193 ((fail_stack).size << 1) * sizeof (fail_stack_elt_t)), \
1195 (fail_stack).stack == NULL \
1197 : ((fail_stack).size <<= 1, \
1201 /* Push pointer POINTER on FAIL_STACK.
1202 Return 1 if was able to do so and 0 if ran out of memory allocating
1204 #define PUSH_PATTERN_OP(POINTER, FAIL_STACK) \
1205 ((FAIL_STACK_FULL () \
1206 && !DOUBLE_FAIL_STACK (FAIL_STACK)) \
1208 : ((FAIL_STACK).stack[(FAIL_STACK).avail++].pointer = POINTER, \
1211 /* Push a pointer value onto the failure stack.
1212 Assumes the variable `fail_stack'. Probably should only
1213 be called from within `PUSH_FAILURE_POINT'. */
1214 #define PUSH_FAILURE_POINTER(item) \
1215 fail_stack.stack[fail_stack.avail++].pointer = (unsigned char *) (item)
1217 /* This pushes an integer-valued item onto the failure stack.
1218 Assumes the variable `fail_stack'. Probably should only
1219 be called from within `PUSH_FAILURE_POINT'. */
1220 #define PUSH_FAILURE_INT(item) \
1221 fail_stack.stack[fail_stack.avail++].integer = (item)
1223 /* Push a fail_stack_elt_t value onto the failure stack.
1224 Assumes the variable `fail_stack'. Probably should only
1225 be called from within `PUSH_FAILURE_POINT'. */
1226 #define PUSH_FAILURE_ELT(item) \
1227 fail_stack.stack[fail_stack.avail++] = (item)
1229 /* These three POP... operations complement the three PUSH... operations.
1230 All assume that `fail_stack' is nonempty. */
1231 #define POP_FAILURE_POINTER() fail_stack.stack[--fail_stack.avail].pointer
1232 #define POP_FAILURE_INT() fail_stack.stack[--fail_stack.avail].integer
1233 #define POP_FAILURE_ELT() fail_stack.stack[--fail_stack.avail]
1235 /* Used to omit pushing failure point id's when we're not debugging. */
1237 #define DEBUG_PUSH PUSH_FAILURE_INT
1238 #define DEBUG_POP(item_addr) *(item_addr) = POP_FAILURE_INT ()
1240 #define DEBUG_PUSH(item)
1241 #define DEBUG_POP(item_addr)
1245 /* Push the information about the state we will need
1246 if we ever fail back to it.
1248 Requires variables fail_stack, regstart, regend, reg_info, and
1249 num_regs be declared. DOUBLE_FAIL_STACK requires `destination' be
1252 Does `return FAILURE_CODE' if runs out of memory. */
1254 #if !defined (REGEX_MALLOC) && !defined (REL_ALLOC)
1255 #define DECLARE_DESTINATION char *destination;
1257 #define DECLARE_DESTINATION
1260 #define PUSH_FAILURE_POINT(pattern_place, string_place, failure_code) \
1262 DECLARE_DESTINATION \
1263 /* Must be int, so when we don't save any registers, the arithmetic \
1264 of 0 + -1 isn't done as unsigned. */ \
1267 DEBUG_STATEMENT (failure_id++); \
1268 DEBUG_STATEMENT (nfailure_points_pushed++); \
1269 DEBUG_PRINT2 ("\nPUSH_FAILURE_POINT #%u:\n", failure_id); \
1270 DEBUG_PRINT2 (" Before push, next avail: %d\n", (fail_stack).avail);\
1271 DEBUG_PRINT2 (" size: %d\n", (fail_stack).size);\
1273 DEBUG_PRINT2 (" slots needed: %d\n", NUM_FAILURE_ITEMS); \
1274 DEBUG_PRINT2 (" available: %d\n", REMAINING_AVAIL_SLOTS); \
1276 /* Ensure we have enough space allocated for what we will push. */ \
1277 while (REMAINING_AVAIL_SLOTS < NUM_FAILURE_ITEMS) \
1279 if (!DOUBLE_FAIL_STACK (fail_stack)) \
1280 return failure_code; \
1282 DEBUG_PRINT2 ("\n Doubled stack; size now: %d\n", \
1283 (fail_stack).size); \
1284 DEBUG_PRINT2 (" slots available: %d\n", REMAINING_AVAIL_SLOTS);\
1287 /* Push the info, starting with the registers. */ \
1288 DEBUG_PRINT1 ("\n"); \
1290 for (this_reg = lowest_active_reg; this_reg <= highest_active_reg; \
1293 DEBUG_PRINT2 (" Pushing reg: %d\n", this_reg); \
1294 DEBUG_STATEMENT (num_regs_pushed++); \
1296 DEBUG_PRINT2 (" start: 0x%lx\n", (long) regstart[this_reg]); \
1297 PUSH_FAILURE_POINTER (regstart[this_reg]); \
1299 DEBUG_PRINT2 (" end: 0x%lx\n", (long) regend[this_reg]); \
1300 PUSH_FAILURE_POINTER (regend[this_reg]); \
1302 DEBUG_PRINT2 (" info: 0x%lx\n ", \
1303 * (long *) (®_info[this_reg])); \
1304 DEBUG_PRINT2 (" match_null=%d", \
1305 REG_MATCH_NULL_STRING_P (reg_info[this_reg])); \
1306 DEBUG_PRINT2 (" active=%d", IS_ACTIVE (reg_info[this_reg])); \
1307 DEBUG_PRINT2 (" matched_something=%d", \
1308 MATCHED_SOMETHING (reg_info[this_reg])); \
1309 DEBUG_PRINT2 (" ever_matched=%d", \
1310 EVER_MATCHED_SOMETHING (reg_info[this_reg])); \
1311 DEBUG_PRINT1 ("\n"); \
1312 PUSH_FAILURE_ELT (reg_info[this_reg].word); \
1315 DEBUG_PRINT2 (" Pushing low active reg: %d\n", lowest_active_reg);\
1316 PUSH_FAILURE_INT (lowest_active_reg); \
1318 DEBUG_PRINT2 (" Pushing high active reg: %d\n", highest_active_reg);\
1319 PUSH_FAILURE_INT (highest_active_reg); \
1321 DEBUG_PRINT2 (" Pushing pattern 0x%lx: \n", (long) pattern_place); \
1322 DEBUG_PRINT_COMPILED_PATTERN (bufp, pattern_place, pend); \
1323 PUSH_FAILURE_POINTER (pattern_place); \
1325 DEBUG_PRINT2 (" Pushing string 0x%lx: `", (long) string_place); \
1326 DEBUG_PRINT_DOUBLE_STRING (string_place, string1, size1, string2, \
1328 DEBUG_PRINT1 ("'\n"); \
1329 PUSH_FAILURE_POINTER (string_place); \
1331 DEBUG_PRINT2 (" Pushing failure id: %u\n", failure_id); \
1332 DEBUG_PUSH (failure_id); \
1335 /* This is the number of items that are pushed and popped on the stack
1336 for each register. */
1337 #define NUM_REG_ITEMS 3
1339 /* Individual items aside from the registers. */
1341 #define NUM_NONREG_ITEMS 5 /* Includes failure point id. */
1343 #define NUM_NONREG_ITEMS 4
1346 /* We push at most this many items on the stack. */
1347 /* We used to use (num_regs - 1), which is the number of registers
1348 this regexp will save; but that was changed to 5
1349 to avoid stack overflow for a regexp with lots of parens. */
1350 #define MAX_FAILURE_ITEMS (5 * NUM_REG_ITEMS + NUM_NONREG_ITEMS)
1352 /* We actually push this many items. */
1353 #define NUM_FAILURE_ITEMS \
1354 ((highest_active_reg - lowest_active_reg + 1) * NUM_REG_ITEMS \
1357 /* How many items can still be added to the stack without overflowing it. */
1358 #define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail)
1361 /* Pops what PUSH_FAIL_STACK pushes.
1363 We restore into the parameters, all of which should be lvalues:
1364 STR -- the saved data position.
1365 PAT -- the saved pattern position.
1366 LOW_REG, HIGH_REG -- the highest and lowest active registers.
1367 REGSTART, REGEND -- arrays of string positions.
1368 REG_INFO -- array of information about each subexpression.
1370 Also assumes the variables `fail_stack' and (if debugging), `bufp',
1371 `pend', `string1', `size1', `string2', and `size2'. */
1373 #define POP_FAILURE_POINT(str, pat, low_reg, high_reg, regstart, regend, reg_info)\
1375 DEBUG_STATEMENT (fail_stack_elt_t ffailure_id;) \
1377 const unsigned char *string_temp; \
1379 assert (!FAIL_STACK_EMPTY ()); \
1381 /* Remove failure points and point to how many regs pushed. */ \
1382 DEBUG_PRINT1 ("POP_FAILURE_POINT:\n"); \
1383 DEBUG_PRINT2 (" Before pop, next avail: %d\n", fail_stack.avail); \
1384 DEBUG_PRINT2 (" size: %d\n", fail_stack.size); \
1386 assert (fail_stack.avail >= NUM_NONREG_ITEMS); \
1388 DEBUG_POP (&ffailure_id.integer); \
1389 DEBUG_PRINT2 (" Popping failure id: %u\n", \
1390 * (unsigned int *) &ffailure_id); \
1392 /* If the saved string location is NULL, it came from an \
1393 on_failure_keep_string_jump opcode, and we want to throw away the \
1394 saved NULL, thus retaining our current position in the string. */ \
1395 string_temp = POP_FAILURE_POINTER (); \
1396 if (string_temp != NULL) \
1397 str = string_temp; \
1399 DEBUG_PRINT2 (" Popping string 0x%lx: `", (long) str); \
1400 DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2); \
1401 DEBUG_PRINT1 ("'\n"); \
1403 pat = (unsigned char *) POP_FAILURE_POINTER (); \
1404 DEBUG_PRINT2 (" Popping pattern 0x%lx: ", (long) pat); \
1405 DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend); \
1407 /* Restore register info. */ \
1408 high_reg = (unsigned) POP_FAILURE_INT (); \
1409 DEBUG_PRINT2 (" Popping high active reg: %d\n", high_reg); \
1411 low_reg = (unsigned) POP_FAILURE_INT (); \
1412 DEBUG_PRINT2 (" Popping low active reg: %d\n", low_reg); \
1414 for (this_reg = high_reg; this_reg >= low_reg; this_reg--) \
1416 DEBUG_PRINT2 (" Popping reg: %d\n", this_reg); \
1418 reg_info[this_reg].word = POP_FAILURE_ELT (); \
1419 DEBUG_PRINT2 (" info: 0x%lx\n", \
1420 * (long *) ®_info[this_reg]); \
1422 regend[this_reg] = POP_FAILURE_POINTER (); \
1423 DEBUG_PRINT2 (" end: 0x%lx\n", (long) regend[this_reg]); \
1425 regstart[this_reg] = POP_FAILURE_POINTER (); \
1426 DEBUG_PRINT2 (" start: 0x%lx\n", (long) regstart[this_reg]); \
1429 set_regs_matched_done = 0; \
1430 DEBUG_STATEMENT (nfailure_points_popped++); \
1431 } /* POP_FAILURE_POINT */
1435 /* Structure for per-register (a.k.a. per-group) information.
1436 Other register information, such as the
1437 starting and ending positions (which are addresses), and the list of
1438 inner groups (which is a bits list) are maintained in separate
1441 We are making a (strictly speaking) nonportable assumption here: that
1442 the compiler will pack our bit fields into something that fits into
1443 the type of `word', i.e., is something that fits into one item on the
1448 fail_stack_elt_t word;
1451 /* This field is one if this group can match the empty string,
1452 zero if not. If not yet determined, `MATCH_NULL_UNSET_VALUE'. */
1453 #define MATCH_NULL_UNSET_VALUE 3
1454 unsigned match_null_string_p : 2;
1455 unsigned is_active : 1;
1456 unsigned matched_something : 1;
1457 unsigned ever_matched_something : 1;
1459 } register_info_type;
1461 #define REG_MATCH_NULL_STRING_P(R) ((R).bits.match_null_string_p)
1462 #define IS_ACTIVE(R) ((R).bits.is_active)
1463 #define MATCHED_SOMETHING(R) ((R).bits.matched_something)
1464 #define EVER_MATCHED_SOMETHING(R) ((R).bits.ever_matched_something)
1467 /* Call this when have matched a real character; it sets `matched' flags
1468 for the subexpressions which we are currently inside. Also records
1469 that those subexprs have matched. */
1470 #define SET_REGS_MATCHED() \
1473 if (!set_regs_matched_done) \
1476 set_regs_matched_done = 1; \
1477 for (r = lowest_active_reg; r <= highest_active_reg; r++) \
1479 MATCHED_SOMETHING (reg_info[r]) \
1480 = EVER_MATCHED_SOMETHING (reg_info[r]) \
1487 /* Registers are set to a sentinel when they haven't yet matched. */
1488 static unsigned char reg_unset_dummy;
1489 #define REG_UNSET_VALUE (®_unset_dummy)
1490 #define REG_UNSET(e) ((e) == REG_UNSET_VALUE)
1492 /* Subroutine declarations and macros for regex_compile. */
1494 /* Fetch the next character in the uncompiled pattern---translating it
1495 if necessary. Also cast from a signed character in the constant
1496 string passed to us by the user to an unsigned char that we can use
1497 as an array index (in, e.g., `translate'). */
1498 #define PATFETCH(c) \
1501 c = TRANSLATE (c); \
1504 /* Fetch the next character in the uncompiled pattern, with no
1506 #define PATFETCH_RAW(c) \
1507 do {if (p == pend) return REG_EEND; \
1508 assert (p < pend); \
1509 c = charptr_emchar (p); \
1513 /* Go backwards one character in the pattern. */
1514 #define PATUNFETCH DEC_CHARPTR (p)
1518 #define PATFETCH_EXTENDED(emch) \
1519 do {if (p == pend) return REG_EEND; \
1520 assert (p < pend); \
1521 emch = charptr_emchar ((const Bufbyte *) p); \
1523 if (TRANSLATE_P (translate) && emch < 0x80) \
1524 emch = (Emchar) (unsigned char) RE_TRANSLATE (emch); \
1527 #define PATFETCH_RAW_EXTENDED(emch) \
1528 do {if (p == pend) return REG_EEND; \
1529 assert (p < pend); \
1530 emch = charptr_emchar ((const Bufbyte *) p); \
1534 #define PATUNFETCH_EXTENDED DEC_CHARPTR (p)
1536 #define PATFETCH_EITHER(emch) \
1538 if (has_extended_chars) \
1539 PATFETCH_EXTENDED (emch); \
1544 #define PATFETCH_RAW_EITHER(emch) \
1546 if (has_extended_chars) \
1547 PATFETCH_RAW_EXTENDED (emch); \
1549 PATFETCH_RAW (emch); \
1552 #define PATUNFETCH_EITHER \
1554 if (has_extended_chars) \
1555 PATUNFETCH_EXTENDED (emch); \
1557 PATUNFETCH (emch); \
1560 #else /* not MULE */
1562 #define PATFETCH_EITHER(emch) PATFETCH (emch)
1563 #define PATFETCH_RAW_EITHER(emch) PATFETCH_RAW (emch)
1564 #define PATUNFETCH_EITHER PATUNFETCH
1568 /* If `translate' is non-null, return translate[D], else just D. We
1569 cast the subscript to translate because some data is declared as
1570 `char *', to avoid warnings when a string constant is passed. But
1571 when we use a character as a subscript we must make it unsigned. */
1572 #define TRANSLATE(d) (TRANSLATE_P (translate) ? RE_TRANSLATE (d) : (d))
1576 #define TRANSLATE_EXTENDED_UNSAFE(emch) \
1577 (TRANSLATE_P (translate) && emch < 0x80 ? RE_TRANSLATE (emch) : (emch))
1581 /* Macros for outputting the compiled pattern into `buffer'. */
1583 /* If the buffer isn't allocated when it comes in, use this. */
1584 #define INIT_BUF_SIZE 32
1586 /* Make sure we have at least N more bytes of space in buffer. */
1587 #define GET_BUFFER_SPACE(n) \
1588 while (buf_end - bufp->buffer + (n) > bufp->allocated) \
1591 /* Make sure we have one more byte of buffer space and then add C to it. */
1592 #define BUF_PUSH(c) \
1594 GET_BUFFER_SPACE (1); \
1595 *buf_end++ = (unsigned char) (c); \
1599 /* Ensure we have two more bytes of buffer space and then append C1 and C2. */
1600 #define BUF_PUSH_2(c1, c2) \
1602 GET_BUFFER_SPACE (2); \
1603 *buf_end++ = (unsigned char) (c1); \
1604 *buf_end++ = (unsigned char) (c2); \
1608 /* As with BUF_PUSH_2, except for three bytes. */
1609 #define BUF_PUSH_3(c1, c2, c3) \
1611 GET_BUFFER_SPACE (3); \
1612 *buf_end++ = (unsigned char) (c1); \
1613 *buf_end++ = (unsigned char) (c2); \
1614 *buf_end++ = (unsigned char) (c3); \
1618 /* Store a jump with opcode OP at LOC to location TO. We store a
1619 relative address offset by the three bytes the jump itself occupies. */
1620 #define STORE_JUMP(op, loc, to) \
1621 store_op1 (op, loc, (to) - (loc) - 3)
1623 /* Likewise, for a two-argument jump. */
1624 #define STORE_JUMP2(op, loc, to, arg) \
1625 store_op2 (op, loc, (to) - (loc) - 3, arg)
1627 /* Like `STORE_JUMP', but for inserting. Assume `buf_end' is the
1629 #define INSERT_JUMP(op, loc, to) \
1630 insert_op1 (op, loc, (to) - (loc) - 3, buf_end)
1632 /* Like `STORE_JUMP2', but for inserting. Assume `buf_end' is the
1634 #define INSERT_JUMP2(op, loc, to, arg) \
1635 insert_op2 (op, loc, (to) - (loc) - 3, arg, buf_end)
1638 /* This is not an arbitrary limit: the arguments which represent offsets
1639 into the pattern are two bytes long. So if 2^16 bytes turns out to
1640 be too small, many things would have to change. */
1641 #define MAX_BUF_SIZE (1L << 16)
1644 /* Extend the buffer by twice its current size via realloc and
1645 reset the pointers that pointed into the old block to point to the
1646 correct places in the new one. If extending the buffer results in it
1647 being larger than MAX_BUF_SIZE, then flag memory exhausted. */
1648 #define EXTEND_BUFFER() \
1650 re_char *old_buffer = bufp->buffer; \
1651 if (bufp->allocated == MAX_BUF_SIZE) \
1653 bufp->allocated <<= 1; \
1654 if (bufp->allocated > MAX_BUF_SIZE) \
1655 bufp->allocated = MAX_BUF_SIZE; \
1656 bufp->buffer = (unsigned char *) realloc (bufp->buffer, bufp->allocated);\
1657 if (bufp->buffer == NULL) \
1658 return REG_ESPACE; \
1659 /* If the buffer moved, move all the pointers into it. */ \
1660 if (old_buffer != bufp->buffer) \
1662 buf_end = (buf_end - old_buffer) + bufp->buffer; \
1663 begalt = (begalt - old_buffer) + bufp->buffer; \
1664 if (fixup_alt_jump) \
1665 fixup_alt_jump = (fixup_alt_jump - old_buffer) + bufp->buffer;\
1667 laststart = (laststart - old_buffer) + bufp->buffer; \
1668 if (pending_exact) \
1669 pending_exact = (pending_exact - old_buffer) + bufp->buffer; \
1674 /* Since we have one byte reserved for the register number argument to
1675 {start,stop}_memory, the maximum number of groups we can report
1676 things about is what fits in that byte. */
1677 #define MAX_REGNUM 255
1679 /* But patterns can have more than `MAX_REGNUM' registers. We just
1680 ignore the excess. */
1681 typedef unsigned regnum_t;
1684 /* Macros for the compile stack. */
1686 /* Since offsets can go either forwards or backwards, this type needs to
1687 be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1. */
1688 typedef int pattern_offset_t;
1692 pattern_offset_t begalt_offset;
1693 pattern_offset_t fixup_alt_jump;
1694 pattern_offset_t inner_group_offset;
1695 pattern_offset_t laststart_offset;
1697 } compile_stack_elt_t;
1702 compile_stack_elt_t *stack;
1704 unsigned avail; /* Offset of next open position. */
1705 } compile_stack_type;
1708 #define INIT_COMPILE_STACK_SIZE 32
1710 #define COMPILE_STACK_EMPTY (compile_stack.avail == 0)
1711 #define COMPILE_STACK_FULL (compile_stack.avail == compile_stack.size)
1713 /* The next available element. */
1714 #define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
1717 /* Set the bit for character C in a bit vector. */
1718 #define SET_LIST_BIT(c) \
1719 (buf_end[((unsigned char) (c)) / BYTEWIDTH] \
1720 |= 1 << (((unsigned char) c) % BYTEWIDTH))
1724 /* Set the "bit" for character C in a range table. */
1725 #define SET_RANGETAB_BIT(c) put_range_table (rtab, c, c, Qt)
1727 /* Set the "bit" for character c in the appropriate table. */
1728 #define SET_EITHER_BIT(c) \
1730 if (has_extended_chars) \
1731 SET_RANGETAB_BIT (c); \
1736 #else /* not MULE */
1738 #define SET_EITHER_BIT(c) SET_LIST_BIT (c)
1743 /* Get the next unsigned number in the uncompiled pattern. */
1744 #define GET_UNSIGNED_NUMBER(num) \
1748 while (ISDIGIT (c)) \
1752 num = num * 10 + c - '0'; \
1760 #define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
1762 #define IS_CHAR_CLASS(string) \
1763 (STREQ (string, "alpha") || STREQ (string, "upper") \
1764 || STREQ (string, "lower") || STREQ (string, "digit") \
1765 || STREQ (string, "alnum") || STREQ (string, "xdigit") \
1766 || STREQ (string, "space") || STREQ (string, "print") \
1767 || STREQ (string, "punct") || STREQ (string, "graph") \
1768 || STREQ (string, "cntrl") || STREQ (string, "blank"))
1770 static void store_op1 (re_opcode_t op, unsigned char *loc, int arg);
1771 static void store_op2 (re_opcode_t op, unsigned char *loc, int arg1, int arg2);
1772 static void insert_op1 (re_opcode_t op, unsigned char *loc, int arg,
1773 unsigned char *end);
1774 static void insert_op2 (re_opcode_t op, unsigned char *loc, int arg1, int arg2,
1775 unsigned char *end);
1776 static boolean at_begline_loc_p (re_char *pattern, re_char *p,
1777 reg_syntax_t syntax);
1778 static boolean at_endline_loc_p (re_char *p, re_char *pend, int syntax);
1779 static boolean group_in_compile_stack (compile_stack_type compile_stack,
1781 static reg_errcode_t compile_range (re_char **p_ptr, re_char *pend,
1782 RE_TRANSLATE_TYPE translate,
1783 reg_syntax_t syntax,
1786 static reg_errcode_t compile_extended_range (re_char **p_ptr,
1788 RE_TRANSLATE_TYPE translate,
1789 reg_syntax_t syntax,
1792 static boolean group_match_null_string_p (unsigned char **p,
1794 register_info_type *reg_info);
1795 static boolean alt_match_null_string_p (unsigned char *p, unsigned char *end,
1796 register_info_type *reg_info);
1797 static boolean common_op_match_null_string_p (unsigned char **p,
1799 register_info_type *reg_info);
1800 static int bcmp_translate (const unsigned char *s1, const unsigned char *s2,
1801 REGISTER int len, RE_TRANSLATE_TYPE translate);
1802 static int re_match_2_internal (struct re_pattern_buffer *bufp,
1803 re_char *string1, int size1,
1804 re_char *string2, int size2, int pos,
1805 struct re_registers *regs, int stop);
1807 #ifndef MATCH_MAY_ALLOCATE
1809 /* If we cannot allocate large objects within re_match_2_internal,
1810 we make the fail stack and register vectors global.
1811 The fail stack, we grow to the maximum size when a regexp
1813 The register vectors, we adjust in size each time we
1814 compile a regexp, according to the number of registers it needs. */
1816 static fail_stack_type fail_stack;
1818 /* Size with which the following vectors are currently allocated.
1819 That is so we can make them bigger as needed,
1820 but never make them smaller. */
1821 static int regs_allocated_size;
1823 static re_char ** regstart, ** regend;
1824 static re_char ** old_regstart, ** old_regend;
1825 static re_char **best_regstart, **best_regend;
1826 static register_info_type *reg_info;
1827 static re_char **reg_dummy;
1828 static register_info_type *reg_info_dummy;
1830 /* Make the register vectors big enough for NUM_REGS registers,
1831 but don't make them smaller. */
1834 regex_grow_registers (int num_regs)
1836 if (num_regs > regs_allocated_size)
1838 RETALLOC_IF (regstart, num_regs, re_char *);
1839 RETALLOC_IF (regend, num_regs, re_char *);
1840 RETALLOC_IF (old_regstart, num_regs, re_char *);
1841 RETALLOC_IF (old_regend, num_regs, re_char *);
1842 RETALLOC_IF (best_regstart, num_regs, re_char *);
1843 RETALLOC_IF (best_regend, num_regs, re_char *);
1844 RETALLOC_IF (reg_info, num_regs, register_info_type);
1845 RETALLOC_IF (reg_dummy, num_regs, re_char *);
1846 RETALLOC_IF (reg_info_dummy, num_regs, register_info_type);
1848 regs_allocated_size = num_regs;
1852 #endif /* not MATCH_MAY_ALLOCATE */
1854 /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
1855 Returns one of error codes defined in `regex.h', or zero for success.
1857 Assumes the `allocated' (and perhaps `buffer') and `translate'
1858 fields are set in BUFP on entry.
1860 If it succeeds, results are put in BUFP (if it returns an error, the
1861 contents of BUFP are undefined):
1862 `buffer' is the compiled pattern;
1863 `syntax' is set to SYNTAX;
1864 `used' is set to the length of the compiled pattern;
1865 `fastmap_accurate' is zero;
1866 `re_nsub' is the number of subexpressions in PATTERN;
1867 `not_bol' and `not_eol' are zero;
1869 The `fastmap' and `newline_anchor' fields are neither
1870 examined nor set. */
1872 /* Return, freeing storage we allocated. */
1873 #define FREE_STACK_RETURN(value) \
1874 return (free (compile_stack.stack), value)
1876 static reg_errcode_t
1877 regex_compile (re_char *pattern, int size, reg_syntax_t syntax,
1878 struct re_pattern_buffer *bufp)
1880 /* We fetch characters from PATTERN here. We declare these as int
1881 (or possibly long) so that chars above 127 can be used as
1882 array indices. The macros that fetch a character from the pattern
1883 make sure to coerce to unsigned char before assigning, so we won't
1884 get bitten by negative numbers here. */
1885 /* XEmacs change: used to be unsigned char. */
1886 REGISTER EMACS_INT c, c1;
1888 /* A random temporary spot in PATTERN. */
1891 /* Points to the end of the buffer, where we should append. */
1892 REGISTER unsigned char *buf_end;
1894 /* Keeps track of unclosed groups. */
1895 compile_stack_type compile_stack;
1897 /* Points to the current (ending) position in the pattern. */
1898 re_char *p = pattern;
1899 re_char *pend = pattern + size;
1901 /* How to translate the characters in the pattern. */
1902 RE_TRANSLATE_TYPE translate = bufp->translate;
1904 /* Address of the count-byte of the most recently inserted `exactn'
1905 command. This makes it possible to tell if a new exact-match
1906 character can be added to that command or if the character requires
1907 a new `exactn' command. */
1908 unsigned char *pending_exact = 0;
1910 /* Address of start of the most recently finished expression.
1911 This tells, e.g., postfix * where to find the start of its
1912 operand. Reset at the beginning of groups and alternatives. */
1913 unsigned char *laststart = 0;
1915 /* Address of beginning of regexp, or inside of last group. */
1916 unsigned char *begalt;
1918 /* Place in the uncompiled pattern (i.e., the {) to
1919 which to go back if the interval is invalid. */
1920 re_char *beg_interval;
1922 /* Address of the place where a forward jump should go to the end of
1923 the containing expression. Each alternative of an `or' -- except the
1924 last -- ends with a forward jump of this sort. */
1925 unsigned char *fixup_alt_jump = 0;
1927 /* Counts open-groups as they are encountered. Remembered for the
1928 matching close-group on the compile stack, so the same register
1929 number is put in the stop_memory as the start_memory. */
1930 regnum_t regnum = 0;
1933 DEBUG_PRINT1 ("\nCompiling pattern: ");
1936 unsigned debug_count;
1938 for (debug_count = 0; debug_count < size; debug_count++)
1939 putchar (pattern[debug_count]);
1944 /* Initialize the compile stack. */
1945 compile_stack.stack = TALLOC (INIT_COMPILE_STACK_SIZE, compile_stack_elt_t);
1946 if (compile_stack.stack == NULL)
1949 compile_stack.size = INIT_COMPILE_STACK_SIZE;
1950 compile_stack.avail = 0;
1952 /* Initialize the pattern buffer. */
1953 bufp->syntax = syntax;
1954 bufp->fastmap_accurate = 0;
1955 bufp->not_bol = bufp->not_eol = 0;
1957 /* Set `used' to zero, so that if we return an error, the pattern
1958 printer (for debugging) will think there's no pattern. We reset it
1962 /* Always count groups, whether or not bufp->no_sub is set. */
1965 #if !defined (emacs) && !defined (SYNTAX_TABLE)
1966 /* Initialize the syntax table. */
1967 init_syntax_once ();
1970 if (bufp->allocated == 0)
1973 { /* If zero allocated, but buffer is non-null, try to realloc
1974 enough space. This loses if buffer's address is bogus, but
1975 that is the user's responsibility. */
1976 RETALLOC (bufp->buffer, INIT_BUF_SIZE, unsigned char);
1979 { /* Caller did not allocate a buffer. Do it for them. */
1980 bufp->buffer = TALLOC (INIT_BUF_SIZE, unsigned char);
1982 if (!bufp->buffer) FREE_STACK_RETURN (REG_ESPACE);
1984 bufp->allocated = INIT_BUF_SIZE;
1987 begalt = buf_end = bufp->buffer;
1989 /* Loop through the uncompiled pattern until we're at the end. */
1998 if ( /* If at start of pattern, it's an operator. */
2000 /* If context independent, it's an operator. */
2001 || syntax & RE_CONTEXT_INDEP_ANCHORS
2002 /* Otherwise, depends on what's come before. */
2003 || at_begline_loc_p (pattern, p, syntax))
2013 if ( /* If at end of pattern, it's an operator. */
2015 /* If context independent, it's an operator. */
2016 || syntax & RE_CONTEXT_INDEP_ANCHORS
2017 /* Otherwise, depends on what's next. */
2018 || at_endline_loc_p (p, pend, syntax))
2028 if ((syntax & RE_BK_PLUS_QM)
2029 || (syntax & RE_LIMITED_OPS))
2033 /* If there is no previous pattern... */
2036 if (syntax & RE_CONTEXT_INVALID_OPS)
2037 FREE_STACK_RETURN (REG_BADRPT);
2038 else if (!(syntax & RE_CONTEXT_INDEP_OPS))
2043 /* true means zero/many matches are allowed. */
2044 boolean zero_times_ok = c != '+';
2045 boolean many_times_ok = c != '?';
2047 /* true means match shortest string possible. */
2048 boolean minimal = false;
2050 /* If there is a sequence of repetition chars, collapse it
2051 down to just one (the right one). We can't combine
2052 interval operators with these because of, e.g., `a{2}*',
2053 which should only match an even number of `a's. */
2058 if (c == '*' || (!(syntax & RE_BK_PLUS_QM)
2059 && (c == '+' || c == '?')))
2062 else if (syntax & RE_BK_PLUS_QM && c == '\\')
2064 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
2067 if (!(c1 == '+' || c1 == '?'))
2082 /* If we get here, we found another repeat character. */
2083 if (!(syntax & RE_NO_MINIMAL_MATCHING))
2085 /* "*?" and "+?" and "??" are okay (and mean match
2086 minimally), but other sequences (such as "*??" and
2087 "+++") are rejected (reserved for future use). */
2088 if (minimal || c != '?')
2089 FREE_STACK_RETURN (REG_BADRPT);
2094 zero_times_ok |= c != '+';
2095 many_times_ok |= c != '?';
2099 /* Star, etc. applied to an empty pattern is equivalent
2100 to an empty pattern. */
2104 /* Now we know whether zero matches is allowed
2105 and whether two or more matches is allowed
2106 and whether we want minimal or maximal matching. */
2112 0: /on_failure_jump to 6
2117 GET_BUFFER_SPACE (6);
2118 INSERT_JUMP (jump, laststart, buf_end + 3);
2120 INSERT_JUMP (on_failure_jump, laststart, laststart + 6);
2123 else if (zero_times_ok)
2128 6: /on_failure_jump to 3
2131 GET_BUFFER_SPACE (6);
2132 INSERT_JUMP (jump, laststart, buf_end + 3);
2134 STORE_JUMP (on_failure_jump, buf_end, laststart + 3);
2141 3: /on_failure_jump to 0
2144 GET_BUFFER_SPACE (3);
2145 STORE_JUMP (on_failure_jump, buf_end, laststart);
2151 /* Are we optimizing this jump? */
2152 boolean keep_string_p = false;
2155 { /* More than one repetition is allowed, so put in
2156 at the end a backward relative jump from
2157 `buf_end' to before the next jump we're going
2158 to put in below (which jumps from laststart to
2161 But if we are at the `*' in the exact sequence `.*\n',
2162 insert an unconditional jump backwards to the .,
2163 instead of the beginning of the loop. This way we only
2164 push a failure point once, instead of every time
2165 through the loop. */
2166 assert (p - 1 > pattern);
2168 /* Allocate the space for the jump. */
2169 GET_BUFFER_SPACE (3);
2171 /* We know we are not at the first character of the
2172 pattern, because laststart was nonzero. And we've
2173 already incremented `p', by the way, to be the
2174 character after the `*'. Do we have to do something
2175 analogous here for null bytes, because of
2179 && p < pend && *p == '\n'
2180 && !(syntax & RE_DOT_NEWLINE))
2181 { /* We have .*\n. */
2182 STORE_JUMP (jump, buf_end, laststart);
2183 keep_string_p = true;
2186 /* Anything else. */
2187 STORE_JUMP (maybe_pop_jump, buf_end, laststart - 3);
2189 /* We've added more stuff to the buffer. */
2193 /* On failure, jump from laststart to buf_end + 3,
2194 which will be the end of the buffer after this jump
2196 GET_BUFFER_SPACE (3);
2197 INSERT_JUMP (keep_string_p ? on_failure_keep_string_jump
2199 laststart, buf_end + 3);
2204 /* At least one repetition is required, so insert a
2205 `dummy_failure_jump' before the initial
2206 `on_failure_jump' instruction of the loop. This
2207 effects a skip over that instruction the first time
2208 we hit that loop. */
2209 GET_BUFFER_SPACE (3);
2210 INSERT_JUMP (dummy_failure_jump, laststart, laststart + 6);
2220 laststart = buf_end;
2227 /* XEmacs change: this whole section */
2228 boolean had_char_class = false;
2230 boolean has_extended_chars = false;
2231 REGISTER Lisp_Object rtab = Qnil;
2234 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2236 /* Ensure that we have enough space to push a charset: the
2237 opcode, the length count, and the bitset; 34 bytes in all. */
2238 GET_BUFFER_SPACE (34);
2240 laststart = buf_end;
2242 /* We test `*p == '^' twice, instead of using an if
2243 statement, so we only need one BUF_PUSH. */
2244 BUF_PUSH (*p == '^' ? charset_not : charset);
2248 /* Remember the first position in the bracket expression. */
2251 /* Push the number of bytes in the bitmap. */
2252 BUF_PUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
2254 /* Clear the whole map. */
2255 memset (buf_end, 0, (1 << BYTEWIDTH) / BYTEWIDTH);
2257 /* charset_not matches newline according to a syntax bit. */
2258 if ((re_opcode_t) buf_end[-2] == charset_not
2259 && (syntax & RE_HAT_LISTS_NOT_NEWLINE))
2260 SET_LIST_BIT ('\n');
2263 start_over_with_extended:
2264 if (has_extended_chars)
2266 /* There are extended chars here, which means we need to start
2267 over and shift to unified range-table format. */
2268 if (buf_end[-2] == charset)
2269 buf_end[-2] = charset_mule;
2271 buf_end[-2] = charset_mule_not;
2273 p = p1; /* go back to the beginning of the charset, after
2275 rtab = Vthe_lisp_rangetab;
2276 Fclear_range_table (rtab);
2278 /* charset_not matches newline according to a syntax bit. */
2279 if ((re_opcode_t) buf_end[-1] == charset_mule_not
2280 && (syntax & RE_HAT_LISTS_NOT_NEWLINE))
2281 SET_EITHER_BIT ('\n');
2285 /* Read in characters and ranges, setting map bits. */
2288 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2293 if (c >= 0x80 && !has_extended_chars)
2295 has_extended_chars = 1;
2296 /* Frumble-bumble, we've found some extended chars.
2297 Need to start over, process everything using
2298 the general extended-char mechanism, and need
2299 to use charset_mule and charset_mule_not instead
2300 of charset and charset_not. */
2301 goto start_over_with_extended;
2304 /* \ might escape characters inside [...] and [^...]. */
2305 if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) && c == '\\')
2307 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
2311 if (c1 >= 0x80 && !has_extended_chars)
2313 has_extended_chars = 1;
2314 goto start_over_with_extended;
2317 SET_EITHER_BIT (c1);
2321 /* Could be the end of the bracket expression. If it's
2322 not (i.e., when the bracket expression is `[]' so
2323 far), the ']' character bit gets set way below. */
2324 if (c == ']' && p != p1 + 1)
2327 /* Look ahead to see if it's a range when the last thing
2328 was a character class. */
2329 if (had_char_class && c == '-' && *p != ']')
2330 FREE_STACK_RETURN (REG_ERANGE);
2332 /* Look ahead to see if it's a range when the last thing
2333 was a character: if this is a hyphen not at the
2334 beginning or the end of a list, then it's the range
2337 && !(p - 2 >= pattern && p[-2] == '[')
2338 && !(p - 3 >= pattern && p[-3] == '[' && p[-2] == '^')
2344 if (* (unsigned char *) p >= 0x80 && !has_extended_chars)
2346 has_extended_chars = 1;
2347 goto start_over_with_extended;
2349 if (has_extended_chars)
2350 ret = compile_extended_range (&p, pend, translate,
2354 ret = compile_range (&p, pend, translate, syntax, buf_end);
2355 if (ret != REG_NOERROR) FREE_STACK_RETURN (ret);
2358 else if (p[0] == '-' && p[1] != ']')
2359 { /* This handles ranges made up of characters only. */
2362 /* Move past the `-'. */
2366 if (* (unsigned char *) p >= 0x80 && !has_extended_chars)
2368 has_extended_chars = 1;
2369 goto start_over_with_extended;
2371 if (has_extended_chars)
2372 ret = compile_extended_range (&p, pend, translate,
2376 ret = compile_range (&p, pend, translate, syntax, buf_end);
2377 if (ret != REG_NOERROR) FREE_STACK_RETURN (ret);
2380 /* See if we're at the beginning of a possible character
2383 else if (syntax & RE_CHAR_CLASSES && c == '[' && *p == ':')
2384 { /* Leave room for the null. */
2385 char str[CHAR_CLASS_MAX_LENGTH + 1];
2390 /* If pattern is `[[:'. */
2391 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2395 /* #### This code is unused.
2396 Correctness is not checked after TRT
2399 if (c == ':' || c == ']' || p == pend
2400 || c1 == CHAR_CLASS_MAX_LENGTH)
2402 str[c1++] = (char) c;
2406 /* If isn't a word bracketed by `[:' and `:]':
2407 undo the ending character, the letters, and leave
2408 the leading `:' and `[' (but set bits for them). */
2409 if (c == ':' && *p == ']')
2412 boolean is_alnum = STREQ (str, "alnum");
2413 boolean is_alpha = STREQ (str, "alpha");
2414 boolean is_blank = STREQ (str, "blank");
2415 boolean is_cntrl = STREQ (str, "cntrl");
2416 boolean is_digit = STREQ (str, "digit");
2417 boolean is_graph = STREQ (str, "graph");
2418 boolean is_lower = STREQ (str, "lower");
2419 boolean is_print = STREQ (str, "print");
2420 boolean is_punct = STREQ (str, "punct");
2421 boolean is_space = STREQ (str, "space");
2422 boolean is_upper = STREQ (str, "upper");
2423 boolean is_xdigit = STREQ (str, "xdigit");
2425 if (!IS_CHAR_CLASS (str))
2426 FREE_STACK_RETURN (REG_ECTYPE);
2428 /* Throw away the ] at the end of the character
2432 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2434 for (ch = 0; ch < 1 << BYTEWIDTH; ch++)
2436 /* This was split into 3 if's to
2437 avoid an arbitrary limit in some compiler. */
2438 if ( (is_alnum && ISALNUM (ch))
2439 || (is_alpha && ISALPHA (ch))
2440 || (is_blank && ISBLANK (ch))
2441 || (is_cntrl && ISCNTRL (ch)))
2442 SET_EITHER_BIT (ch);
2443 if ( (is_digit && ISDIGIT (ch))
2444 || (is_graph && ISGRAPH (ch))
2445 || (is_lower && ISLOWER (ch))
2446 || (is_print && ISPRINT (ch)))
2447 SET_EITHER_BIT (ch);
2448 if ( (is_punct && ISPUNCT (ch))
2449 || (is_space && ISSPACE (ch))
2450 || (is_upper && ISUPPER (ch))
2451 || (is_xdigit && ISXDIGIT (ch)))
2452 SET_EITHER_BIT (ch);
2454 had_char_class = true;
2461 SET_EITHER_BIT ('[');
2462 SET_EITHER_BIT (':');
2463 had_char_class = false;
2468 had_char_class = false;
2474 if (has_extended_chars)
2476 /* We have a range table, not a bit vector. */
2478 unified_range_table_bytes_needed (rtab);
2479 GET_BUFFER_SPACE (bytes_needed);
2480 unified_range_table_copy_data (rtab, buf_end);
2481 buf_end += unified_range_table_bytes_used (buf_end);
2485 /* Discard any (non)matching list bytes that are all 0 at the
2486 end of the map. Decrease the map-length byte too. */
2487 while ((int) buf_end[-1] > 0 && buf_end[buf_end[-1] - 1] == 0)
2489 buf_end += buf_end[-1];
2495 if (syntax & RE_NO_BK_PARENS)
2502 if (syntax & RE_NO_BK_PARENS)
2509 if (syntax & RE_NEWLINE_ALT)
2516 if (syntax & RE_NO_BK_VBAR)
2523 if (syntax & RE_INTERVALS && syntax & RE_NO_BK_BRACES)
2524 goto handle_interval;
2530 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
2532 /* Do not translate the character after the \, so that we can
2533 distinguish, e.g., \B from \b, even if we normally would
2534 translate, e.g., B to b. */
2540 if (syntax & RE_NO_BK_PARENS)
2541 goto normal_backslash;
2547 if (!(syntax & RE_NO_SHY_GROUPS)
2555 case ':': /* shy groups */
2559 /* All others are reserved for future constructs. */
2561 FREE_STACK_RETURN (REG_BADPAT);
2570 if (COMPILE_STACK_FULL)
2572 RETALLOC (compile_stack.stack, compile_stack.size << 1,
2573 compile_stack_elt_t);
2574 if (compile_stack.stack == NULL) return REG_ESPACE;
2576 compile_stack.size <<= 1;
2579 /* These are the values to restore when we hit end of this
2580 group. They are all relative offsets, so that if the
2581 whole pattern moves because of realloc, they will still
2583 COMPILE_STACK_TOP.begalt_offset = begalt - bufp->buffer;
2584 COMPILE_STACK_TOP.fixup_alt_jump
2585 = fixup_alt_jump ? fixup_alt_jump - bufp->buffer + 1 : 0;
2586 COMPILE_STACK_TOP.laststart_offset = buf_end - bufp->buffer;
2587 COMPILE_STACK_TOP.regnum = r;
2589 /* We will eventually replace the 0 with the number of
2590 groups inner to this one. But do not push a
2591 start_memory for groups beyond the last one we can
2592 represent in the compiled pattern. */
2593 if (r <= MAX_REGNUM)
2595 COMPILE_STACK_TOP.inner_group_offset
2596 = buf_end - bufp->buffer + 2;
2597 BUF_PUSH_3 (start_memory, r, 0);
2600 compile_stack.avail++;
2605 /* If we've reached MAX_REGNUM groups, then this open
2606 won't actually generate any code, so we'll have to
2607 clear pending_exact explicitly. */
2614 if (syntax & RE_NO_BK_PARENS) goto normal_backslash;
2616 if (COMPILE_STACK_EMPTY) {
2617 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
2618 goto normal_backslash;
2620 FREE_STACK_RETURN (REG_ERPAREN);
2625 { /* Push a dummy failure point at the end of the
2626 alternative for a possible future
2627 `pop_failure_jump' to pop. See comments at
2628 `push_dummy_failure' in `re_match_2'. */
2629 BUF_PUSH (push_dummy_failure);
2631 /* We allocated space for this jump when we assigned
2632 to `fixup_alt_jump', in the `handle_alt' case below. */
2633 STORE_JUMP (jump_past_alt, fixup_alt_jump, buf_end - 1);
2636 /* See similar code for backslashed left paren above. */
2637 if (COMPILE_STACK_EMPTY) {
2638 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
2641 FREE_STACK_RETURN (REG_ERPAREN);
2644 /* Since we just checked for an empty stack above, this
2645 ``can't happen''. */
2646 assert (compile_stack.avail != 0);
2648 /* We don't just want to restore into `regnum', because
2649 later groups should continue to be numbered higher,
2650 as in `(ab)c(de)' -- the second group is #2. */
2651 regnum_t this_group_regnum;
2653 compile_stack.avail--;
2654 begalt = bufp->buffer + COMPILE_STACK_TOP.begalt_offset;
2656 = COMPILE_STACK_TOP.fixup_alt_jump
2657 ? bufp->buffer + COMPILE_STACK_TOP.fixup_alt_jump - 1
2659 laststart = bufp->buffer + COMPILE_STACK_TOP.laststart_offset;
2660 this_group_regnum = COMPILE_STACK_TOP.regnum;
2661 /* If we've reached MAX_REGNUM groups, then this open
2662 won't actually generate any code, so we'll have to
2663 clear pending_exact explicitly. */
2666 /* We're at the end of the group, so now we know how many
2667 groups were inside this one. */
2668 if (this_group_regnum <= MAX_REGNUM)
2670 unsigned char *inner_group_loc
2671 = bufp->buffer + COMPILE_STACK_TOP.inner_group_offset;
2673 *inner_group_loc = regnum - this_group_regnum;
2674 BUF_PUSH_3 (stop_memory, this_group_regnum,
2675 regnum - this_group_regnum);
2681 case '|': /* `\|'. */
2682 if (syntax & RE_LIMITED_OPS || syntax & RE_NO_BK_VBAR)
2683 goto normal_backslash;
2685 if (syntax & RE_LIMITED_OPS)
2688 /* Insert before the previous alternative a jump which
2689 jumps to this alternative if the former fails. */
2690 GET_BUFFER_SPACE (3);
2691 INSERT_JUMP (on_failure_jump, begalt, buf_end + 6);
2695 /* The alternative before this one has a jump after it
2696 which gets executed if it gets matched. Adjust that
2697 jump so it will jump to this alternative's analogous
2698 jump (put in below, which in turn will jump to the next
2699 (if any) alternative's such jump, etc.). The last such
2700 jump jumps to the correct final destination. A picture:
2706 If we are at `b', then fixup_alt_jump right now points to a
2707 three-byte space after `a'. We'll put in the jump, set
2708 fixup_alt_jump to right after `b', and leave behind three
2709 bytes which we'll fill in when we get to after `c'. */
2712 STORE_JUMP (jump_past_alt, fixup_alt_jump, buf_end);
2714 /* Mark and leave space for a jump after this alternative,
2715 to be filled in later either by next alternative or
2716 when know we're at the end of a series of alternatives. */
2717 fixup_alt_jump = buf_end;
2718 GET_BUFFER_SPACE (3);
2727 /* If \{ is a literal. */
2728 if (!(syntax & RE_INTERVALS)
2729 /* If we're at `\{' and it's not the open-interval
2731 || ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
2732 || (p - 2 == pattern && p == pend))
2733 goto normal_backslash;
2737 /* If got here, then the syntax allows intervals. */
2739 /* At least (most) this many matches must be made. */
2740 int lower_bound = -1, upper_bound = -1;
2742 beg_interval = p - 1;
2746 if (syntax & RE_NO_BK_BRACES)
2747 goto unfetch_interval;
2749 FREE_STACK_RETURN (REG_EBRACE);
2752 GET_UNSIGNED_NUMBER (lower_bound);
2756 GET_UNSIGNED_NUMBER (upper_bound);
2757 if (upper_bound < 0) upper_bound = RE_DUP_MAX;
2760 /* Interval such as `{1}' => match exactly once. */
2761 upper_bound = lower_bound;
2763 if (lower_bound < 0 || upper_bound > RE_DUP_MAX
2764 || lower_bound > upper_bound)
2766 if (syntax & RE_NO_BK_BRACES)
2767 goto unfetch_interval;
2769 FREE_STACK_RETURN (REG_BADBR);
2772 if (!(syntax & RE_NO_BK_BRACES))
2774 if (c != '\\') FREE_STACK_RETURN (REG_EBRACE);
2781 if (syntax & RE_NO_BK_BRACES)
2782 goto unfetch_interval;
2784 FREE_STACK_RETURN (REG_BADBR);
2787 /* We just parsed a valid interval. */
2789 /* If it's invalid to have no preceding re. */
2792 if (syntax & RE_CONTEXT_INVALID_OPS)
2793 FREE_STACK_RETURN (REG_BADRPT);
2794 else if (syntax & RE_CONTEXT_INDEP_OPS)
2795 laststart = buf_end;
2797 goto unfetch_interval;
2800 /* If the upper bound is zero, don't want to succeed at
2801 all; jump from `laststart' to `b + 3', which will be
2802 the end of the buffer after we insert the jump. */
2803 if (upper_bound == 0)
2805 GET_BUFFER_SPACE (3);
2806 INSERT_JUMP (jump, laststart, buf_end + 3);
2810 /* Otherwise, we have a nontrivial interval. When
2811 we're all done, the pattern will look like:
2812 set_number_at <jump count> <upper bound>
2813 set_number_at <succeed_n count> <lower bound>
2814 succeed_n <after jump addr> <succeed_n count>
2816 jump_n <succeed_n addr> <jump count>
2817 (The upper bound and `jump_n' are omitted if
2818 `upper_bound' is 1, though.) */
2820 { /* If the upper bound is > 1, we need to insert
2821 more at the end of the loop. */
2822 unsigned nbytes = 10 + (upper_bound > 1) * 10;
2824 GET_BUFFER_SPACE (nbytes);
2826 /* Initialize lower bound of the `succeed_n', even
2827 though it will be set during matching by its
2828 attendant `set_number_at' (inserted next),
2829 because `re_compile_fastmap' needs to know.
2830 Jump to the `jump_n' we might insert below. */
2831 INSERT_JUMP2 (succeed_n, laststart,
2832 buf_end + 5 + (upper_bound > 1) * 5,
2836 /* Code to initialize the lower bound. Insert
2837 before the `succeed_n'. The `5' is the last two
2838 bytes of this `set_number_at', plus 3 bytes of
2839 the following `succeed_n'. */
2840 insert_op2 (set_number_at, laststart, 5, lower_bound, buf_end);
2843 if (upper_bound > 1)
2844 { /* More than one repetition is allowed, so
2845 append a backward jump to the `succeed_n'
2846 that starts this interval.
2848 When we've reached this during matching,
2849 we'll have matched the interval once, so
2850 jump back only `upper_bound - 1' times. */
2851 STORE_JUMP2 (jump_n, buf_end, laststart + 5,
2855 /* The location we want to set is the second
2856 parameter of the `jump_n'; that is `b-2' as
2857 an absolute address. `laststart' will be
2858 the `set_number_at' we're about to insert;
2859 `laststart+3' the number to set, the source
2860 for the relative address. But we are
2861 inserting into the middle of the pattern --
2862 so everything is getting moved up by 5.
2863 Conclusion: (b - 2) - (laststart + 3) + 5,
2864 i.e., b - laststart.
2866 We insert this at the beginning of the loop
2867 so that if we fail during matching, we'll
2868 reinitialize the bounds. */
2869 insert_op2 (set_number_at, laststart,
2870 buf_end - laststart,
2871 upper_bound - 1, buf_end);
2876 beg_interval = NULL;
2881 /* If an invalid interval, match the characters as literals. */
2882 assert (beg_interval);
2884 beg_interval = NULL;
2886 /* normal_char and normal_backslash need `c'. */
2889 if (!(syntax & RE_NO_BK_BRACES))
2891 if (p > pattern && p[-1] == '\\')
2892 goto normal_backslash;
2897 /* There is no way to specify the before_dot and after_dot
2898 operators. rms says this is ok. --karl */
2904 laststart = buf_end;
2906 /* XEmacs addition */
2907 if (c >= 0x80 || syntax_spec_code[c] == 0377)
2908 FREE_STACK_RETURN (REG_ESYNTAX);
2909 BUF_PUSH_2 (syntaxspec, syntax_spec_code[c]);
2913 laststart = buf_end;
2915 /* XEmacs addition */
2916 if (c >= 0x80 || syntax_spec_code[c] == 0377)
2917 FREE_STACK_RETURN (REG_ESYNTAX);
2918 BUF_PUSH_2 (notsyntaxspec, syntax_spec_code[c]);
2922 /* 97.2.17 jhod merged in to XEmacs from mule-2.3 */
2924 laststart = buf_end;
2926 if (c < 32 || c > 127)
2927 FREE_STACK_RETURN (REG_ECATEGORY);
2928 BUF_PUSH_2 (categoryspec, c);
2932 laststart = buf_end;
2934 if (c < 32 || c > 127)
2935 FREE_STACK_RETURN (REG_ECATEGORY);
2936 BUF_PUSH_2 (notcategoryspec, c);
2938 /* end of category patch */
2944 laststart = buf_end;
2945 BUF_PUSH (wordchar);
2950 laststart = buf_end;
2951 BUF_PUSH (notwordchar);
2964 BUF_PUSH (wordbound);
2968 BUF_PUSH (notwordbound);
2979 case '1': case '2': case '3': case '4': case '5':
2980 case '6': case '7': case '8': case '9':
2983 if (syntax & RE_NO_BK_REFS)
2989 FREE_STACK_RETURN (REG_ESUBREG);
2991 /* Can't back reference to a subexpression if inside of it. */
2992 if (group_in_compile_stack (compile_stack, reg))
2995 laststart = buf_end;
2996 BUF_PUSH_2 (duplicate, reg);
3003 if (syntax & RE_BK_PLUS_QM)
3006 goto normal_backslash;
3010 /* You might think it would be useful for \ to mean
3011 not to translate; but if we don't translate it,
3012 it will never match anything. */
3020 /* Expects the character in `c'. */
3021 /* `p' points to the location after where `c' came from. */
3024 /* XEmacs: modifications here for Mule. */
3025 /* `q' points to the beginning of the next char. */
3028 /* If no exactn currently being built. */
3031 /* If last exactn not at current position. */
3032 || pending_exact + *pending_exact + 1 != buf_end
3034 /* We have only one byte following the exactn for the count. */
3035 || ((unsigned int) (*pending_exact + (q - p)) >=
3036 ((unsigned int) (1 << BYTEWIDTH) - 1))
3038 /* If followed by a repetition operator. */
3039 || *q == '*' || *q == '^'
3040 || ((syntax & RE_BK_PLUS_QM)
3041 ? *q == '\\' && (q[1] == '+' || q[1] == '?')
3042 : (*q == '+' || *q == '?'))
3043 || ((syntax & RE_INTERVALS)
3044 && ((syntax & RE_NO_BK_BRACES)
3046 : (q[0] == '\\' && q[1] == '{'))))
3048 /* Start building a new exactn. */
3050 laststart = buf_end;
3052 BUF_PUSH_2 (exactn, 0);
3053 pending_exact = buf_end - 1;
3062 Bufbyte tmp_buf[MAX_EMCHAR_LEN];
3065 bt_count = set_charptr_emchar (tmp_buf, c);
3067 for (i = 0; i < bt_count; i++)
3069 BUF_PUSH (tmp_buf[i]);
3077 } /* while p != pend */
3080 /* Through the pattern now. */
3083 STORE_JUMP (jump_past_alt, fixup_alt_jump, buf_end);
3085 if (!COMPILE_STACK_EMPTY)
3086 FREE_STACK_RETURN (REG_EPAREN);
3088 /* If we don't want backtracking, force success
3089 the first time we reach the end of the compiled pattern. */
3090 if (syntax & RE_NO_POSIX_BACKTRACKING)
3093 free (compile_stack.stack);
3095 /* We have succeeded; set the length of the buffer. */
3096 bufp->used = buf_end - bufp->buffer;
3101 DEBUG_PRINT1 ("\nCompiled pattern: \n");
3102 print_compiled_pattern (bufp);
3106 #ifndef MATCH_MAY_ALLOCATE
3107 /* Initialize the failure stack to the largest possible stack. This
3108 isn't necessary unless we're trying to avoid calling alloca in
3109 the search and match routines. */
3111 int num_regs = bufp->re_nsub + 1;
3113 /* Since DOUBLE_FAIL_STACK refuses to double only if the current size
3114 is strictly greater than re_max_failures, the largest possible stack
3115 is 2 * re_max_failures failure points. */
3116 if (fail_stack.size < (2 * re_max_failures * MAX_FAILURE_ITEMS))
3118 fail_stack.size = (2 * re_max_failures * MAX_FAILURE_ITEMS);
3121 if (! fail_stack.stack)
3123 = (fail_stack_elt_t *) xmalloc (fail_stack.size
3124 * sizeof (fail_stack_elt_t));
3127 = (fail_stack_elt_t *) xrealloc (fail_stack.stack,
3129 * sizeof (fail_stack_elt_t)));
3130 #else /* not emacs */
3131 if (! fail_stack.stack)
3133 = (fail_stack_elt_t *) malloc (fail_stack.size
3134 * sizeof (fail_stack_elt_t));
3137 = (fail_stack_elt_t *) realloc (fail_stack.stack,
3139 * sizeof (fail_stack_elt_t)));
3143 regex_grow_registers (num_regs);
3145 #endif /* not MATCH_MAY_ALLOCATE */
3148 } /* regex_compile */
3150 /* Subroutines for `regex_compile'. */
3152 /* Store OP at LOC followed by two-byte integer parameter ARG. */
3155 store_op1 (re_opcode_t op, unsigned char *loc, int arg)
3157 *loc = (unsigned char) op;
3158 STORE_NUMBER (loc + 1, arg);
3162 /* Like `store_op1', but for two two-byte parameters ARG1 and ARG2. */
3165 store_op2 (re_opcode_t op, unsigned char *loc, int arg1, int arg2)
3167 *loc = (unsigned char) op;
3168 STORE_NUMBER (loc + 1, arg1);
3169 STORE_NUMBER (loc + 3, arg2);
3173 /* Copy the bytes from LOC to END to open up three bytes of space at LOC
3174 for OP followed by two-byte integer parameter ARG. */
3177 insert_op1 (re_opcode_t op, unsigned char *loc, int arg, unsigned char *end)
3179 REGISTER unsigned char *pfrom = end;
3180 REGISTER unsigned char *pto = end + 3;
3182 while (pfrom != loc)
3185 store_op1 (op, loc, arg);
3189 /* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2. */
3192 insert_op2 (re_opcode_t op, unsigned char *loc, int arg1, int arg2,
3195 REGISTER unsigned char *pfrom = end;
3196 REGISTER unsigned char *pto = end + 5;
3198 while (pfrom != loc)
3201 store_op2 (op, loc, arg1, arg2);
3205 /* P points to just after a ^ in PATTERN. Return true if that ^ comes
3206 after an alternative or a begin-subexpression. We assume there is at
3207 least one character before the ^. */
3210 at_begline_loc_p (re_char *pattern, re_char *p, reg_syntax_t syntax)
3212 re_char *prev = p - 2;
3213 boolean prev_prev_backslash = prev > pattern && prev[-1] == '\\';
3216 /* After a subexpression? */
3217 (*prev == '(' && (syntax & RE_NO_BK_PARENS || prev_prev_backslash))
3218 /* After an alternative? */
3219 || (*prev == '|' && (syntax & RE_NO_BK_VBAR || prev_prev_backslash));
3223 /* The dual of at_begline_loc_p. This one is for $. We assume there is
3224 at least one character after the $, i.e., `P < PEND'. */
3227 at_endline_loc_p (re_char *p, re_char *pend, int syntax)
3230 boolean next_backslash = *next == '\\';
3231 re_char *next_next = p + 1 < pend ? p + 1 : 0;
3234 /* Before a subexpression? */
3235 (syntax & RE_NO_BK_PARENS ? *next == ')'
3236 : next_backslash && next_next && *next_next == ')')
3237 /* Before an alternative? */
3238 || (syntax & RE_NO_BK_VBAR ? *next == '|'
3239 : next_backslash && next_next && *next_next == '|');
3243 /* Returns true if REGNUM is in one of COMPILE_STACK's elements and
3244 false if it's not. */
3247 group_in_compile_stack (compile_stack_type compile_stack, regnum_t regnum)
3251 for (this_element = compile_stack.avail - 1;
3254 if (compile_stack.stack[this_element].regnum == regnum)
3261 /* Read the ending character of a range (in a bracket expression) from the
3262 uncompiled pattern *P_PTR (which ends at PEND). We assume the
3263 starting character is in `P[-2]'. (`P[-1]' is the character `-'.)
3264 Then we set the translation of all bits between the starting and
3265 ending characters (inclusive) in the compiled pattern B.
3267 Return an error code.
3269 We use these short variable names so we can use the same macros as
3270 `regex_compile' itself. */
3272 static reg_errcode_t
3273 compile_range (re_char **p_ptr, re_char *pend, RE_TRANSLATE_TYPE translate,
3274 reg_syntax_t syntax, unsigned char *buf_end)
3278 re_char *p = *p_ptr;
3279 int range_start, range_end;
3284 /* Even though the pattern is a signed `char *', we need to fetch
3285 with unsigned char *'s; if the high bit of the pattern character
3286 is set, the range endpoints will be negative if we fetch using a
3289 We also want to fetch the endpoints without translating them; the
3290 appropriate translation is done in the bit-setting loop below. */
3291 /* The SVR4 compiler on the 3B2 had trouble with unsigned const char *. */
3292 range_start = ((const unsigned char *) p)[-2];
3293 range_end = ((const unsigned char *) p)[0];
3295 /* Have to increment the pointer into the pattern string, so the
3296 caller isn't still at the ending character. */
3299 /* If the start is after the end, the range is empty. */
3300 if (range_start > range_end)
3301 return syntax & RE_NO_EMPTY_RANGES ? REG_ERANGE : REG_NOERROR;
3303 /* Here we see why `this_char' has to be larger than an `unsigned
3304 char' -- the range is inclusive, so if `range_end' == 0xff
3305 (assuming 8-bit characters), we would otherwise go into an infinite
3306 loop, since all characters <= 0xff. */
3307 for (this_char = range_start; this_char <= range_end; this_char++)
3309 SET_LIST_BIT (TRANSLATE (this_char));
3317 static reg_errcode_t
3318 compile_extended_range (re_char **p_ptr, re_char *pend,
3319 RE_TRANSLATE_TYPE translate,
3320 reg_syntax_t syntax, Lisp_Object rtab)
3322 Emchar this_char, range_start, range_end;
3328 p = (const Bufbyte *) *p_ptr;
3329 range_end = charptr_emchar (p);
3330 p--; /* back to '-' */
3331 DEC_CHARPTR (p); /* back to start of range */
3332 /* We also want to fetch the endpoints without translating them; the
3333 appropriate translation is done in the bit-setting loop below. */
3334 range_start = charptr_emchar (p);
3335 INC_CHARPTR (*p_ptr);
3337 /* If the start is after the end, the range is empty. */
3338 if (range_start > range_end)
3339 return syntax & RE_NO_EMPTY_RANGES ? REG_ERANGE : REG_NOERROR;
3341 /* Can't have ranges spanning different charsets, except maybe for
3342 ranges entirely within the first 256 chars. */
3344 if ((range_start >= 0x100 || range_end >= 0x100)
3345 && CHAR_LEADING_BYTE (range_start) !=
3346 CHAR_LEADING_BYTE (range_end))
3347 return REG_ERANGESPAN;
3349 /* As advertised, translations only work over the 0 - 0x7F range.
3350 Making this kind of stuff work generally is much harder.
3351 Iterating over the whole range like this would be way efficient
3352 if the range encompasses 10,000 chars or something. You'd have
3353 to do something like this:
3357 map over translation table in [range_start, range_end] of
3358 (put the mapped range in a;
3359 put the translation in b)
3360 invert the range in a and truncate to [range_start, range_end]
3361 compute the union of a, b
3362 union the result into rtab
3364 for (this_char = range_start;
3365 this_char <= range_end && this_char < 0x80; this_char++)
3367 SET_RANGETAB_BIT (TRANSLATE (this_char));
3370 if (this_char <= range_end)
3371 put_range_table (rtab, this_char, range_end, Qt);
3378 /* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
3379 BUFP. A fastmap records which of the (1 << BYTEWIDTH) possible
3380 characters can start a string that matches the pattern. This fastmap
3381 is used by re_search to skip quickly over impossible starting points.
3383 The caller must supply the address of a (1 << BYTEWIDTH)-byte data
3384 area as BUFP->fastmap.
3386 We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in
3389 Returns 0 if we succeed, -2 if an internal error. */
3392 re_compile_fastmap (struct re_pattern_buffer *bufp)
3395 #ifdef MATCH_MAY_ALLOCATE
3396 fail_stack_type fail_stack;
3399 /* We don't push any register information onto the failure stack. */
3401 REGISTER char *fastmap = bufp->fastmap;
3402 unsigned char *pattern = bufp->buffer;
3403 unsigned long size = bufp->used;
3404 unsigned char *p = pattern;
3405 REGISTER unsigned char *pend = pattern + size;
3408 /* This holds the pointer to the failure stack, when
3409 it is allocated relocatably. */
3410 fail_stack_elt_t *failure_stack_ptr;
3413 /* Assume that each path through the pattern can be null until
3414 proven otherwise. We set this false at the bottom of switch
3415 statement, to which we get only if a particular path doesn't
3416 match the empty string. */
3417 boolean path_can_be_null = true;
3419 /* We aren't doing a `succeed_n' to begin with. */
3420 boolean succeed_n_p = false;
3422 assert (fastmap != NULL && p != NULL);
3425 memset (fastmap, 0, 1 << BYTEWIDTH); /* Assume nothing's valid. */
3426 bufp->fastmap_accurate = 1; /* It will be when we're done. */
3427 bufp->can_be_null = 0;
3431 if (p == pend || *p == succeed)
3433 /* We have reached the (effective) end of pattern. */
3434 if (!FAIL_STACK_EMPTY ())
3436 bufp->can_be_null |= path_can_be_null;
3438 /* Reset for next path. */
3439 path_can_be_null = true;
3441 p = (unsigned char *) fail_stack.stack[--fail_stack.avail].pointer;
3449 /* We should never be about to go beyond the end of the pattern. */
3452 switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++))
3455 /* I guess the idea here is to simply not bother with a fastmap
3456 if a backreference is used, since it's too hard to figure out
3457 the fastmap for the corresponding group. Setting
3458 `can_be_null' stops `re_search_2' from using the fastmap, so
3459 that is all we do. */
3461 bufp->can_be_null = 1;
3465 /* Following are the cases which match a character. These end
3474 /* XEmacs: Under Mule, these bit vectors will
3475 only contain values for characters below 0x80. */
3476 for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
3477 if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
3483 /* Chars beyond end of map must be allowed. */
3485 for (j = *p * BYTEWIDTH; j < 0x80; j++)
3487 /* And all extended characters must be allowed, too. */
3488 for (j = 0x80; j < 0xA0; j++)
3490 #else /* not MULE */
3491 for (j = *p * BYTEWIDTH; j < (1 << BYTEWIDTH); j++)
3495 for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
3496 if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
3506 nentries = unified_range_table_nentries (p);
3507 for (i = 0; i < nentries; i++)
3509 EMACS_INT first, last;
3510 Lisp_Object dummy_val;
3512 Bufbyte strr[MAX_EMCHAR_LEN];
3514 unified_range_table_get_range (p, i, &first, &last,
3516 for (jj = first; jj <= last && jj < 0x80; jj++)
3518 /* Ranges below 0x100 can span charsets, but there
3519 are only two (Control-1 and Latin-1), and
3520 either first or last has to be in them. */
3521 set_charptr_emchar (strr, first);
3525 set_charptr_emchar (strr, last);
3532 case charset_mule_not:
3537 nentries = unified_range_table_nentries (p);
3538 for (i = 0; i < nentries; i++)
3540 EMACS_INT first, last;
3541 Lisp_Object dummy_val;
3543 int smallest_prev = 0;
3545 unified_range_table_get_range (p, i, &first, &last,
3547 for (jj = smallest_prev; jj < first && jj < 0x80; jj++)
3549 smallest_prev = last + 1;
3550 if (smallest_prev >= 0x80)
3553 /* Calculating which leading bytes are actually allowed
3554 here is rather difficult, so we just punt and allow
3556 for (i = 0x80; i < 0xA0; i++)
3568 for (j = 0; j < (1 << BYTEWIDTH); j++)
3571 (regex_emacs_buffer->mirror_syntax_table), j) == Sword)
3580 goto matchnotsyntax;
3582 for (j = 0; j < (1 << BYTEWIDTH); j++)
3585 (regex_emacs_buffer->mirror_syntax_table), j) != Sword)
3593 int fastmap_newline = fastmap['\n'];
3595 /* `.' matches anything ... */
3597 /* "anything" only includes bytes that can be the
3598 first byte of a character. */
3599 for (j = 0; j < 0xA0; j++)
3602 for (j = 0; j < (1 << BYTEWIDTH); j++)
3606 /* ... except perhaps newline. */
3607 if (!(bufp->syntax & RE_DOT_NEWLINE))
3608 fastmap['\n'] = fastmap_newline;
3610 /* Return if we have already set `can_be_null'; if we have,
3611 then the fastmap is irrelevant. Something's wrong here. */
3612 else if (bufp->can_be_null)
3615 /* Otherwise, have to check alternative paths. */
3624 for (j = 0; j < 0x80; j++)
3627 (regex_emacs_buffer->mirror_syntax_table), j) ==
3628 (enum syntaxcode) k)
3630 for (j = 0x80; j < 0xA0; j++)
3632 if (LEADING_BYTE_PREFIX_P(j))
3633 /* too complicated to calculate this right */
3640 cset = CHARSET_BY_LEADING_BYTE (j);
3641 if (CHARSETP (cset))
3643 if (charset_syntax (regex_emacs_buffer, cset,
3645 == Sword || multi_p)
3650 #else /* not MULE */
3651 for (j = 0; j < (1 << BYTEWIDTH); j++)
3654 (regex_emacs_buffer->mirror_syntax_table), j) ==
3655 (enum syntaxcode) k)
3665 for (j = 0; j < 0x80; j++)
3668 (regex_emacs_buffer->mirror_syntax_table), j) !=
3669 (enum syntaxcode) k)
3671 for (j = 0x80; j < 0xA0; j++)
3673 if (LEADING_BYTE_PREFIX_P(j))
3674 /* too complicated to calculate this right */
3681 cset = CHARSET_BY_LEADING_BYTE (j);
3682 if (CHARSETP (cset))
3684 if (charset_syntax (regex_emacs_buffer, cset,
3686 != Sword || multi_p)
3691 #else /* not MULE */
3692 for (j = 0; j < (1 << BYTEWIDTH); j++)
3695 (regex_emacs_buffer->mirror_syntax_table), j) !=
3696 (enum syntaxcode) k)
3702 /* 97/2/17 jhod category patch */
3704 case notcategoryspec:
3705 bufp->can_be_null = 1;
3707 /* end if category patch */
3710 /* All cases after this match the empty string. These end with
3718 #endif /* not emacs */
3730 case push_dummy_failure:
3735 case pop_failure_jump:
3736 case maybe_pop_jump:
3739 case dummy_failure_jump:
3740 EXTRACT_NUMBER_AND_INCR (j, p);
3745 /* Jump backward implies we just went through the body of a
3746 loop and matched nothing. Opcode jumped to should be
3747 `on_failure_jump' or `succeed_n'. Just treat it like an
3748 ordinary jump. For a * loop, it has pushed its failure
3749 point already; if so, discard that as redundant. */
3750 if ((re_opcode_t) *p != on_failure_jump
3751 && (re_opcode_t) *p != succeed_n)
3755 EXTRACT_NUMBER_AND_INCR (j, p);
3758 /* If what's on the stack is where we are now, pop it. */
3759 if (!FAIL_STACK_EMPTY ()
3760 && fail_stack.stack[fail_stack.avail - 1].pointer == p)
3766 case on_failure_jump:
3767 case on_failure_keep_string_jump:
3768 handle_on_failure_jump:
3769 EXTRACT_NUMBER_AND_INCR (j, p);
3771 /* For some patterns, e.g., `(a?)?', `p+j' here points to the
3772 end of the pattern. We don't want to push such a point,
3773 since when we restore it above, entering the switch will
3774 increment `p' past the end of the pattern. We don't need
3775 to push such a point since we obviously won't find any more
3776 fastmap entries beyond `pend'. Such a pattern can match
3777 the null string, though. */
3780 if (!PUSH_PATTERN_OP (p + j, fail_stack))
3782 RESET_FAIL_STACK ();
3787 bufp->can_be_null = 1;
3791 EXTRACT_NUMBER_AND_INCR (k, p); /* Skip the n. */
3792 succeed_n_p = false;
3799 /* Get to the number of times to succeed. */
3802 /* Increment p past the n for when k != 0. */
3803 EXTRACT_NUMBER_AND_INCR (k, p);
3807 succeed_n_p = true; /* Spaghetti code alert. */
3808 goto handle_on_failure_jump;
3825 abort (); /* We have listed all the cases. */
3828 /* Getting here means we have found the possible starting
3829 characters for one path of the pattern -- and that the empty
3830 string does not match. We need not follow this path further.
3831 Instead, look at the next alternative (remembered on the
3832 stack), or quit if no more. The test at the top of the loop
3833 does these things. */
3834 path_can_be_null = false;
3838 /* Set `can_be_null' for the last path (also the first path, if the
3839 pattern is empty). */
3840 bufp->can_be_null |= path_can_be_null;
3843 RESET_FAIL_STACK ();
3845 } /* re_compile_fastmap */
3847 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
3848 ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
3849 this memory for recording register information. STARTS and ENDS
3850 must be allocated using the malloc library routine, and must each
3851 be at least NUM_REGS * sizeof (regoff_t) bytes long.
3853 If NUM_REGS == 0, then subsequent matches should allocate their own
3856 Unless this function is called, the first search or match using
3857 PATTERN_BUFFER will allocate its own register data, without
3858 freeing the old data. */
3861 re_set_registers (struct re_pattern_buffer *bufp, struct re_registers *regs,
3862 unsigned num_regs, regoff_t *starts, regoff_t *ends)
3866 bufp->regs_allocated = REGS_REALLOCATE;
3867 regs->num_regs = num_regs;
3868 regs->start = starts;
3873 bufp->regs_allocated = REGS_UNALLOCATED;
3875 regs->start = regs->end = (regoff_t *) 0;
3879 /* Searching routines. */
3881 /* Like re_search_2, below, but only one string is specified, and
3882 doesn't let you say where to stop matching. */
3885 re_search (struct re_pattern_buffer *bufp, const char *string, int size,
3886 int startpos, int range, struct re_registers *regs)
3888 return re_search_2 (bufp, NULL, 0, string, size, startpos, range,
3893 /* Snarfed from src/lisp.h, needed for compiling [ce]tags. */
3894 # define bytecount_to_charcount(ptr, len) (len)
3895 # define charcount_to_bytecount(ptr, len) (len)
3896 typedef int Charcount;
3899 /* Using the compiled pattern in BUFP->buffer, first tries to match the
3900 virtual concatenation of STRING1 and STRING2, starting first at index
3901 STARTPOS, then at STARTPOS + 1, and so on.
3903 With MULE, STARTPOS is a byte position, not a char position. And the
3904 search will increment STARTPOS by the width of the current leading
3907 STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
3909 RANGE is how far to scan while trying to match. RANGE = 0 means try
3910 only at STARTPOS; in general, the last start tried is STARTPOS +
3913 With MULE, RANGE is a byte position, not a char position. The last
3914 start tried is the character starting <= STARTPOS + RANGE.
3916 In REGS, return the indices of the virtual concatenation of STRING1
3917 and STRING2 that matched the entire BUFP->buffer and its contained
3920 Do not consider matching one past the index STOP in the virtual
3921 concatenation of STRING1 and STRING2.
3923 We return either the position in the strings at which the match was
3924 found, -1 if no match, or -2 if error (such as failure
3928 re_search_2 (struct re_pattern_buffer *bufp, const char *str1,
3929 int size1, const char *str2, int size2, int startpos,
3930 int range, struct re_registers *regs, int stop)
3933 re_char *string1 = (re_char *) str1;
3934 re_char *string2 = (re_char *) str2;
3935 REGISTER char *fastmap = bufp->fastmap;
3936 REGISTER RE_TRANSLATE_TYPE translate = bufp->translate;
3937 int total_size = size1 + size2;
3938 int endpos = startpos + range;
3939 #ifdef REGEX_BEGLINE_CHECK
3940 int anchored_at_begline = 0;
3945 /* Check for out-of-range STARTPOS. */
3946 if (startpos < 0 || startpos > total_size)
3949 /* Fix up RANGE if it might eventually take us outside
3950 the virtual concatenation of STRING1 and STRING2. */
3952 range = 0 - startpos;
3953 else if (endpos > total_size)
3954 range = total_size - startpos;
3956 /* If the search isn't to be a backwards one, don't waste time in a
3957 search for a pattern that must be anchored. */
3958 if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == begbuf && range > 0)
3964 d = ((const unsigned char *)
3965 (startpos >= size1 ? string2 - size1 : string1) + startpos);
3966 range = charcount_to_bytecount (d, 1);
3970 /* Update the fastmap now if not correct already. */
3971 if (fastmap && !bufp->fastmap_accurate)
3972 if (re_compile_fastmap (bufp) == -2)
3975 #ifdef REGEX_BEGLINE_CHECK
3979 while (i < bufp->used)
3981 if (bufp->buffer[i] == start_memory ||
3982 bufp->buffer[i] == stop_memory)
3987 anchored_at_begline = i < bufp->used && bufp->buffer[i] == begline;
3991 /* Loop through the string, looking for a place to start matching. */
3994 #ifdef REGEX_BEGLINE_CHECK
3995 /* If the regex is anchored at the beginning of a line (i.e. with a ^),
3996 then we can speed things up by skipping to the next beginning-of-
3998 if (anchored_at_begline && startpos > 0 && startpos != size1 &&
4001 /* whose stupid idea was it anyway to make this
4002 function take two strings to match?? */
4006 if (startpos < size1 && startpos + range >= size1)
4007 lim = range - (size1 - startpos);
4009 d = ((const unsigned char *)
4010 (startpos >= size1 ? string2 - size1 : string1) + startpos);
4011 DEC_CHARPTR(d); /* Ok, since startpos != size1. */
4012 d_size = charcount_to_bytecount (d, 1);
4014 if (TRANSLATE_P (translate))
4015 while (range > lim && *d != '\n')
4017 d += d_size; /* Speedier INC_CHARPTR(d) */
4018 d_size = charcount_to_bytecount (d, 1);
4022 while (range > lim && *d != '\n')
4024 d += d_size; /* Speedier INC_CHARPTR(d) */
4025 d_size = charcount_to_bytecount (d, 1);
4029 startpos += irange - range;
4031 #endif /* REGEX_BEGLINE_CHECK */
4033 /* If a fastmap is supplied, skip quickly over characters that
4034 cannot be the start of a match. If the pattern can match the
4035 null string, however, we don't need to skip characters; we want
4036 the first null string. */
4037 if (fastmap && startpos < total_size && !bufp->can_be_null)
4039 if (range > 0) /* Searching forwards. */
4044 if (startpos < size1 && startpos + range >= size1)
4045 lim = range - (size1 - startpos);
4047 d = ((const unsigned char *)
4048 (startpos >= size1 ? string2 - size1 : string1) + startpos);
4050 /* Written out as an if-else to avoid testing `translate'
4052 if (TRANSLATE_P (translate))
4058 buf_ch = charptr_emchar (d);
4059 buf_ch = RE_TRANSLATE (buf_ch);
4060 if (buf_ch >= 0200 || fastmap[(unsigned char) buf_ch])
4063 if (fastmap[(unsigned char)RE_TRANSLATE (*d)])
4066 d_size = charcount_to_bytecount (d, 1);
4068 d += d_size; /* Speedier INC_CHARPTR(d) */
4071 while (range > lim && !fastmap[*d])
4073 d_size = charcount_to_bytecount (d, 1);
4075 d += d_size; /* Speedier INC_CHARPTR(d) */
4078 startpos += irange - range;
4080 else /* Searching backwards. */
4082 Emchar c = (size1 == 0 || startpos >= size1
4083 ? charptr_emchar (string2 + startpos - size1)
4084 : charptr_emchar (string1 + startpos));
4087 if (!(c >= 0200 || fastmap[(unsigned char) c]))
4090 if (!fastmap[(unsigned char) c])
4096 /* If can't match the null string, and that's all we have left, fail. */
4097 if (range >= 0 && startpos == total_size && fastmap
4098 && !bufp->can_be_null)
4101 #ifdef emacs /* XEmacs added, w/removal of immediate_quit */
4102 if (!no_quit_in_re_search)
4105 val = re_match_2_internal (bufp, string1, size1, string2, size2,
4106 startpos, regs, stop);
4107 #ifndef REGEX_MALLOC
4124 d = ((const unsigned char *)
4125 (startpos >= size1 ? string2 - size1 : string1) + startpos);
4126 d_size = charcount_to_bytecount (d, 1);
4132 /* Note startpos > size1 not >=. If we are on the
4133 string1/string2 boundary, we want to backup into string1. */
4134 d = ((const unsigned char *)
4135 (startpos > size1 ? string2 - size1 : string1) + startpos);
4137 d_size = charcount_to_bytecount (d, 1);
4145 /* Declarations and macros for re_match_2. */
4147 /* This converts PTR, a pointer into one of the search strings `string1'
4148 and `string2' into an offset from the beginning of that string. */
4149 #define POINTER_TO_OFFSET(ptr) \
4150 (FIRST_STRING_P (ptr) \
4151 ? ((regoff_t) ((ptr) - string1)) \
4152 : ((regoff_t) ((ptr) - string2 + size1)))
4154 /* Macros for dealing with the split strings in re_match_2. */
4156 #define MATCHING_IN_FIRST_STRING (dend == end_match_1)
4158 /* Call before fetching a character with *d. This switches over to
4159 string2 if necessary. */
4160 #define REGEX_PREFETCH() \
4163 /* End of string2 => fail. */ \
4164 if (dend == end_match_2) \
4166 /* End of string1 => advance to string2. */ \
4168 dend = end_match_2; \
4172 /* Test if at very beginning or at very end of the virtual concatenation
4173 of `string1' and `string2'. If only one string, it's `string2'. */
4174 #define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2)
4175 #define AT_STRINGS_END(d) ((d) == end2)
4178 If the given position straddles the string gap, return the equivalent
4179 position that is before or after the gap, respectively; otherwise,
4180 return the same position. */
4181 #define POS_BEFORE_GAP_UNSAFE(d) ((d) == string2 ? end1 : (d))
4182 #define POS_AFTER_GAP_UNSAFE(d) ((d) == end1 ? string2 : (d))
4184 /* Test if CH is a word-constituent character. (XEmacs change) */
4185 #define WORDCHAR_P_UNSAFE(ch) \
4186 (SYNTAX_UNSAFE (XCHAR_TABLE (regex_emacs_buffer->mirror_syntax_table), \
4189 /* Free everything we malloc. */
4190 #ifdef MATCH_MAY_ALLOCATE
4191 #define FREE_VAR(var) if (var) REGEX_FREE (var); var = NULL
4192 #define FREE_VARIABLES() \
4194 REGEX_FREE_STACK (fail_stack.stack); \
4195 FREE_VAR (regstart); \
4196 FREE_VAR (regend); \
4197 FREE_VAR (old_regstart); \
4198 FREE_VAR (old_regend); \
4199 FREE_VAR (best_regstart); \
4200 FREE_VAR (best_regend); \
4201 FREE_VAR (reg_info); \
4202 FREE_VAR (reg_dummy); \
4203 FREE_VAR (reg_info_dummy); \
4205 #else /* not MATCH_MAY_ALLOCATE */
4206 #define FREE_VARIABLES() ((void)0) /* Do nothing! But inhibit gcc warning. */
4207 #endif /* MATCH_MAY_ALLOCATE */
4209 /* These values must meet several constraints. They must not be valid
4210 register values; since we have a limit of 255 registers (because
4211 we use only one byte in the pattern for the register number), we can
4212 use numbers larger than 255. They must differ by 1, because of
4213 NUM_FAILURE_ITEMS above. And the value for the lowest register must
4214 be larger than the value for the highest register, so we do not try
4215 to actually save any registers when none are active. */
4216 #define NO_HIGHEST_ACTIVE_REG (1 << BYTEWIDTH)
4217 #define NO_LOWEST_ACTIVE_REG (NO_HIGHEST_ACTIVE_REG + 1)
4219 /* Matching routines. */
4221 #ifndef emacs /* Emacs never uses this. */
4222 /* re_match is like re_match_2 except it takes only a single string. */
4225 re_match (struct re_pattern_buffer *bufp, const char *string, int size,
4226 int pos, struct re_registers *regs)
4228 int result = re_match_2_internal (bufp, NULL, 0, (re_char *) string, size,
4233 #endif /* not emacs */
4236 /* re_match_2 matches the compiled pattern in BUFP against the
4237 (virtual) concatenation of STRING1 and STRING2 (of length SIZE1 and
4238 SIZE2, respectively). We start matching at POS, and stop matching
4241 If REGS is non-null and the `no_sub' field of BUFP is nonzero, we
4242 store offsets for the substring each group matched in REGS. See the
4243 documentation for exactly how many groups we fill.
4245 We return -1 if no match, -2 if an internal error (such as the
4246 failure stack overflowing). Otherwise, we return the length of the
4247 matched substring. */
4250 re_match_2 (struct re_pattern_buffer *bufp, const char *string1,
4251 int size1, const char *string2, int size2, int pos,
4252 struct re_registers *regs, int stop)
4254 int result = re_match_2_internal (bufp, (re_char *) string1, size1,
4255 (re_char *) string2, size2,
4261 /* This is a separate function so that we can force an alloca cleanup
4264 re_match_2_internal (struct re_pattern_buffer *bufp, re_char *string1,
4265 int size1, re_char *string2, int size2, int pos,
4266 struct re_registers *regs, int stop)
4268 /* General temporaries. */
4271 int should_succeed; /* XEmacs change */
4273 /* Just past the end of the corresponding string. */
4274 re_char *end1, *end2;
4276 /* Pointers into string1 and string2, just past the last characters in
4277 each to consider matching. */
4278 re_char *end_match_1, *end_match_2;
4280 /* Where we are in the data, and the end of the current string. */
4283 /* Where we are in the pattern, and the end of the pattern. */
4284 unsigned char *p = bufp->buffer;
4285 REGISTER unsigned char *pend = p + bufp->used;
4287 /* Mark the opcode just after a start_memory, so we can test for an
4288 empty subpattern when we get to the stop_memory. */
4289 re_char *just_past_start_mem = 0;
4291 /* We use this to map every character in the string. */
4292 RE_TRANSLATE_TYPE translate = bufp->translate;
4294 /* Failure point stack. Each place that can handle a failure further
4295 down the line pushes a failure point on this stack. It consists of
4296 restart, regend, and reg_info for all registers corresponding to
4297 the subexpressions we're currently inside, plus the number of such
4298 registers, and, finally, two char *'s. The first char * is where
4299 to resume scanning the pattern; the second one is where to resume
4300 scanning the strings. If the latter is zero, the failure point is
4301 a ``dummy''; if a failure happens and the failure point is a dummy,
4302 it gets discarded and the next one is tried. */
4303 #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
4304 fail_stack_type fail_stack;
4307 static unsigned failure_id;
4308 unsigned nfailure_points_pushed = 0, nfailure_points_popped = 0;
4312 /* This holds the pointer to the failure stack, when
4313 it is allocated relocatably. */
4314 fail_stack_elt_t *failure_stack_ptr;
4317 /* We fill all the registers internally, independent of what we
4318 return, for use in backreferences. The number here includes
4319 an element for register zero. */
4320 unsigned num_regs = bufp->re_nsub + 1;
4322 /* The currently active registers. */
4323 unsigned lowest_active_reg = NO_LOWEST_ACTIVE_REG;
4324 unsigned highest_active_reg = NO_HIGHEST_ACTIVE_REG;
4326 /* Information on the contents of registers. These are pointers into
4327 the input strings; they record just what was matched (on this
4328 attempt) by a subexpression part of the pattern, that is, the
4329 regnum-th regstart pointer points to where in the pattern we began
4330 matching and the regnum-th regend points to right after where we
4331 stopped matching the regnum-th subexpression. (The zeroth register
4332 keeps track of what the whole pattern matches.) */
4333 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
4334 re_char **regstart, **regend;
4337 /* If a group that's operated upon by a repetition operator fails to
4338 match anything, then the register for its start will need to be
4339 restored because it will have been set to wherever in the string we
4340 are when we last see its open-group operator. Similarly for a
4342 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
4343 re_char **old_regstart, **old_regend;
4346 /* The is_active field of reg_info helps us keep track of which (possibly
4347 nested) subexpressions we are currently in. The matched_something
4348 field of reg_info[reg_num] helps us tell whether or not we have
4349 matched any of the pattern so far this time through the reg_num-th
4350 subexpression. These two fields get reset each time through any
4351 loop their register is in. */
4352 #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
4353 register_info_type *reg_info;
4356 /* The following record the register info as found in the above
4357 variables when we find a match better than any we've seen before.
4358 This happens as we backtrack through the failure points, which in
4359 turn happens only if we have not yet matched the entire string. */
4360 unsigned best_regs_set = false;
4361 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
4362 re_char **best_regstart, **best_regend;
4365 /* Logically, this is `best_regend[0]'. But we don't want to have to
4366 allocate space for that if we're not allocating space for anything
4367 else (see below). Also, we never need info about register 0 for
4368 any of the other register vectors, and it seems rather a kludge to
4369 treat `best_regend' differently than the rest. So we keep track of
4370 the end of the best match so far in a separate variable. We
4371 initialize this to NULL so that when we backtrack the first time
4372 and need to test it, it's not garbage. */
4373 re_char *match_end = NULL;
4375 /* This helps SET_REGS_MATCHED avoid doing redundant work. */
4376 int set_regs_matched_done = 0;
4378 /* Used when we pop values we don't care about. */
4379 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
4380 re_char **reg_dummy;
4381 register_info_type *reg_info_dummy;
4385 /* Counts the total number of registers pushed. */
4386 unsigned num_regs_pushed = 0;
4389 /* 1 if this match ends in the same string (string1 or string2)
4390 as the best previous match. */
4393 /* 1 if this match is the best seen so far. */
4394 boolean best_match_p;
4396 DEBUG_PRINT1 ("\n\nEntering re_match_2.\n");
4400 #ifdef MATCH_MAY_ALLOCATE
4401 /* Do not bother to initialize all the register variables if there are
4402 no groups in the pattern, as it takes a fair amount of time. If
4403 there are groups, we include space for register 0 (the whole
4404 pattern), even though we never use it, since it simplifies the
4405 array indexing. We should fix this. */
4408 regstart = REGEX_TALLOC (num_regs, re_char *);
4409 regend = REGEX_TALLOC (num_regs, re_char *);
4410 old_regstart = REGEX_TALLOC (num_regs, re_char *);
4411 old_regend = REGEX_TALLOC (num_regs, re_char *);
4412 best_regstart = REGEX_TALLOC (num_regs, re_char *);
4413 best_regend = REGEX_TALLOC (num_regs, re_char *);
4414 reg_info = REGEX_TALLOC (num_regs, register_info_type);
4415 reg_dummy = REGEX_TALLOC (num_regs, re_char *);
4416 reg_info_dummy = REGEX_TALLOC (num_regs, register_info_type);
4418 if (!(regstart && regend && old_regstart && old_regend && reg_info
4419 && best_regstart && best_regend && reg_dummy && reg_info_dummy))
4427 /* We must initialize all our variables to NULL, so that
4428 `FREE_VARIABLES' doesn't try to free them. */
4429 regstart = regend = old_regstart = old_regend = best_regstart
4430 = best_regend = reg_dummy = NULL;
4431 reg_info = reg_info_dummy = (register_info_type *) NULL;
4433 #endif /* MATCH_MAY_ALLOCATE */
4435 /* The starting position is bogus. */
4436 if (pos < 0 || pos > size1 + size2)
4442 /* Initialize subexpression text positions to -1 to mark ones that no
4443 start_memory/stop_memory has been seen for. Also initialize the
4444 register information struct. */
4445 for (mcnt = 1; mcnt < num_regs; mcnt++)
4447 regstart[mcnt] = regend[mcnt]
4448 = old_regstart[mcnt] = old_regend[mcnt] = REG_UNSET_VALUE;
4450 REG_MATCH_NULL_STRING_P (reg_info[mcnt]) = MATCH_NULL_UNSET_VALUE;
4451 IS_ACTIVE (reg_info[mcnt]) = 0;
4452 MATCHED_SOMETHING (reg_info[mcnt]) = 0;
4453 EVER_MATCHED_SOMETHING (reg_info[mcnt]) = 0;
4455 /* We move `string1' into `string2' if the latter's empty -- but not if
4456 `string1' is null. */
4457 if (size2 == 0 && string1 != NULL)
4464 end1 = string1 + size1;
4465 end2 = string2 + size2;
4467 /* Compute where to stop matching, within the two strings. */
4470 end_match_1 = string1 + stop;
4471 end_match_2 = string2;
4476 end_match_2 = string2 + stop - size1;
4479 /* `p' scans through the pattern as `d' scans through the data.
4480 `dend' is the end of the input string that `d' points within. `d'
4481 is advanced into the following input string whenever necessary, but
4482 this happens before fetching; therefore, at the beginning of the
4483 loop, `d' can be pointing at the end of a string, but it cannot
4485 if (size1 > 0 && pos <= size1)
4492 d = string2 + pos - size1;
4496 DEBUG_PRINT1 ("The compiled pattern is: \n");
4497 DEBUG_PRINT_COMPILED_PATTERN (bufp, p, pend);
4498 DEBUG_PRINT1 ("The string to match is: `");
4499 DEBUG_PRINT_DOUBLE_STRING (d, string1, size1, string2, size2);
4500 DEBUG_PRINT1 ("'\n");
4502 /* This loops over pattern commands. It exits by returning from the
4503 function if the match is complete, or it drops through if the match
4504 fails at this starting point in the input data. */
4507 DEBUG_PRINT2 ("\n0x%lx: ", (long) p);
4508 #ifdef emacs /* XEmacs added, w/removal of immediate_quit */
4509 if (!no_quit_in_re_search)
4514 { /* End of pattern means we might have succeeded. */
4515 DEBUG_PRINT1 ("end of pattern ... ");
4517 /* If we haven't matched the entire string, and we want the
4518 longest match, try backtracking. */
4519 if (d != end_match_2)
4521 same_str_p = (FIRST_STRING_P (match_end)
4522 == MATCHING_IN_FIRST_STRING);
4524 /* AIX compiler got confused when this was combined
4525 with the previous declaration. */
4527 best_match_p = d > match_end;
4529 best_match_p = !MATCHING_IN_FIRST_STRING;
4531 DEBUG_PRINT1 ("backtracking.\n");
4533 if (!FAIL_STACK_EMPTY ())
4534 { /* More failure points to try. */
4536 /* If exceeds best match so far, save it. */
4537 if (!best_regs_set || best_match_p)
4539 best_regs_set = true;
4542 DEBUG_PRINT1 ("\nSAVING match as best so far.\n");
4544 for (mcnt = 1; mcnt < num_regs; mcnt++)
4546 best_regstart[mcnt] = regstart[mcnt];
4547 best_regend[mcnt] = regend[mcnt];
4553 /* If no failure points, don't restore garbage. And if
4554 last match is real best match, don't restore second
4556 else if (best_regs_set && !best_match_p)
4559 /* Restore best match. It may happen that `dend ==
4560 end_match_1' while the restored d is in string2.
4561 For example, the pattern `x.*y.*z' against the
4562 strings `x-' and `y-z-', if the two strings are
4563 not consecutive in memory. */
4564 DEBUG_PRINT1 ("Restoring best registers.\n");
4567 dend = ((d >= string1 && d <= end1)
4568 ? end_match_1 : end_match_2);
4570 for (mcnt = 1; mcnt < num_regs; mcnt++)
4572 regstart[mcnt] = best_regstart[mcnt];
4573 regend[mcnt] = best_regend[mcnt];
4576 } /* d != end_match_2 */
4579 DEBUG_PRINT1 ("Accepting match.\n");
4581 /* If caller wants register contents data back, do it. */
4582 if (regs && !bufp->no_sub)
4584 /* Have the register data arrays been allocated? */
4585 if (bufp->regs_allocated == REGS_UNALLOCATED)
4586 { /* No. So allocate them with malloc. We need one
4587 extra element beyond `num_regs' for the `-1' marker
4589 regs->num_regs = MAX (RE_NREGS, num_regs + 1);
4590 regs->start = TALLOC (regs->num_regs, regoff_t);
4591 regs->end = TALLOC (regs->num_regs, regoff_t);
4592 if (regs->start == NULL || regs->end == NULL)
4597 bufp->regs_allocated = REGS_REALLOCATE;
4599 else if (bufp->regs_allocated == REGS_REALLOCATE)
4600 { /* Yes. If we need more elements than were already
4601 allocated, reallocate them. If we need fewer, just
4603 if (regs->num_regs < num_regs + 1)
4605 regs->num_regs = num_regs + 1;
4606 RETALLOC (regs->start, regs->num_regs, regoff_t);
4607 RETALLOC (regs->end, regs->num_regs, regoff_t);
4608 if (regs->start == NULL || regs->end == NULL)
4617 /* These braces fend off a "empty body in an else-statement"
4618 warning under GCC when assert expands to nothing. */
4619 assert (bufp->regs_allocated == REGS_FIXED);
4622 /* Convert the pointer data in `regstart' and `regend' to
4623 indices. Register zero has to be set differently,
4624 since we haven't kept track of any info for it. */
4625 if (regs->num_regs > 0)
4627 regs->start[0] = pos;
4628 regs->end[0] = (MATCHING_IN_FIRST_STRING
4629 ? ((regoff_t) (d - string1))
4630 : ((regoff_t) (d - string2 + size1)));
4633 /* Go through the first `min (num_regs, regs->num_regs)'
4634 registers, since that is all we initialized. */
4635 for (mcnt = 1; mcnt < MIN (num_regs, regs->num_regs); mcnt++)
4637 if (REG_UNSET (regstart[mcnt]) || REG_UNSET (regend[mcnt]))
4638 regs->start[mcnt] = regs->end[mcnt] = -1;
4642 = (regoff_t) POINTER_TO_OFFSET (regstart[mcnt]);
4644 = (regoff_t) POINTER_TO_OFFSET (regend[mcnt]);
4648 /* If the regs structure we return has more elements than
4649 were in the pattern, set the extra elements to -1. If
4650 we (re)allocated the registers, this is the case,
4651 because we always allocate enough to have at least one
4653 for (mcnt = num_regs; mcnt < regs->num_regs; mcnt++)
4654 regs->start[mcnt] = regs->end[mcnt] = -1;
4655 } /* regs && !bufp->no_sub */
4657 DEBUG_PRINT4 ("%u failure points pushed, %u popped (%u remain).\n",
4658 nfailure_points_pushed, nfailure_points_popped,
4659 nfailure_points_pushed - nfailure_points_popped);
4660 DEBUG_PRINT2 ("%u registers pushed.\n", num_regs_pushed);
4662 mcnt = d - pos - (MATCHING_IN_FIRST_STRING
4666 DEBUG_PRINT2 ("Returning %d from re_match_2.\n", mcnt);
4672 /* Otherwise match next pattern command. */
4673 switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++))
4675 /* Ignore these. Used to ignore the n of succeed_n's which
4676 currently have n == 0. */
4678 DEBUG_PRINT1 ("EXECUTING no_op.\n");
4682 DEBUG_PRINT1 ("EXECUTING succeed.\n");
4685 /* Match the next n pattern characters exactly. The following
4686 byte in the pattern defines n, and the n bytes after that
4687 are the characters to match. */
4690 DEBUG_PRINT2 ("EXECUTING exactn %d.\n", mcnt);
4692 /* This is written out as an if-else so we don't waste time
4693 testing `translate' inside the loop. */
4694 if (TRANSLATE_P (translate))
4699 Emchar pat_ch, buf_ch;
4703 pat_ch = charptr_emchar (p);
4704 buf_ch = charptr_emchar (d);
4705 if (RE_TRANSLATE (buf_ch) != pat_ch)
4708 pat_len = charcount_to_bytecount (p, 1);
4713 #else /* not MULE */
4715 if ((unsigned char) RE_TRANSLATE (*d++) != *p++)
4727 if (*d++ != *p++) goto fail;
4731 SET_REGS_MATCHED ();
4735 /* Match any character except possibly a newline or a null. */
4737 DEBUG_PRINT1 ("EXECUTING anychar.\n");
4741 if ((!(bufp->syntax & RE_DOT_NEWLINE) && TRANSLATE (*d) == '\n')
4742 || (bufp->syntax & RE_DOT_NOT_NULL && TRANSLATE (*d) == '\000'))
4745 SET_REGS_MATCHED ();
4746 DEBUG_PRINT2 (" Matched `%d'.\n", *d);
4747 INC_CHARPTR (d); /* XEmacs change */
4754 REGISTER unsigned char c;
4755 boolean not = (re_opcode_t) *(p - 1) == charset_not;
4757 DEBUG_PRINT2 ("EXECUTING charset%s.\n", not ? "_not" : "");
4760 c = TRANSLATE (*d); /* The character to match. */
4762 /* Cast to `unsigned' instead of `unsigned char' in case the
4763 bit list is a full 32 bytes long. */
4764 if (c < (unsigned) (*p * BYTEWIDTH)
4765 && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
4770 if (!not) goto fail;
4772 SET_REGS_MATCHED ();
4773 INC_CHARPTR (d); /* XEmacs change */
4779 case charset_mule_not:
4782 boolean not = (re_opcode_t) *(p - 1) == charset_mule_not;
4784 DEBUG_PRINT2 ("EXECUTING charset_mule%s.\n", not ? "_not" : "");
4787 c = charptr_emchar ((const Bufbyte *) d);
4788 c = TRANSLATE_EXTENDED_UNSAFE (c); /* The character to match. */
4790 if (EQ (Qt, unified_range_table_lookup (p, c, Qnil)))
4793 p += unified_range_table_bytes_used (p);
4795 if (!not) goto fail;
4797 SET_REGS_MATCHED ();
4804 /* The beginning of a group is represented by start_memory.
4805 The arguments are the register number in the next byte, and the
4806 number of groups inner to this one in the next. The text
4807 matched within the group is recorded (in the internal
4808 registers data structure) under the register number. */
4810 DEBUG_PRINT3 ("EXECUTING start_memory %d (%d):\n", *p, p[1]);
4812 /* Find out if this group can match the empty string. */
4813 p1 = p; /* To send to group_match_null_string_p. */
4815 if (REG_MATCH_NULL_STRING_P (reg_info[*p]) == MATCH_NULL_UNSET_VALUE)
4816 REG_MATCH_NULL_STRING_P (reg_info[*p])
4817 = group_match_null_string_p (&p1, pend, reg_info);
4819 /* Save the position in the string where we were the last time
4820 we were at this open-group operator in case the group is
4821 operated upon by a repetition operator, e.g., with `(a*)*b'
4822 against `ab'; then we want to ignore where we are now in
4823 the string in case this attempt to match fails. */
4824 old_regstart[*p] = REG_MATCH_NULL_STRING_P (reg_info[*p])
4825 ? REG_UNSET (regstart[*p]) ? d : regstart[*p]
4827 DEBUG_PRINT2 (" old_regstart: %d\n",
4828 POINTER_TO_OFFSET (old_regstart[*p]));
4831 DEBUG_PRINT2 (" regstart: %d\n", POINTER_TO_OFFSET (regstart[*p]));
4833 IS_ACTIVE (reg_info[*p]) = 1;
4834 MATCHED_SOMETHING (reg_info[*p]) = 0;
4836 /* Clear this whenever we change the register activity status. */
4837 set_regs_matched_done = 0;
4839 /* This is the new highest active register. */
4840 highest_active_reg = *p;
4842 /* If nothing was active before, this is the new lowest active
4844 if (lowest_active_reg == NO_LOWEST_ACTIVE_REG)
4845 lowest_active_reg = *p;
4847 /* Move past the register number and inner group count. */
4849 just_past_start_mem = p;
4854 /* The stop_memory opcode represents the end of a group. Its
4855 arguments are the same as start_memory's: the register
4856 number, and the number of inner groups. */
4858 DEBUG_PRINT3 ("EXECUTING stop_memory %d (%d):\n", *p, p[1]);
4860 /* We need to save the string position the last time we were at
4861 this close-group operator in case the group is operated
4862 upon by a repetition operator, e.g., with `((a*)*(b*)*)*'
4863 against `aba'; then we want to ignore where we are now in
4864 the string in case this attempt to match fails. */
4865 old_regend[*p] = REG_MATCH_NULL_STRING_P (reg_info[*p])
4866 ? REG_UNSET (regend[*p]) ? d : regend[*p]
4868 DEBUG_PRINT2 (" old_regend: %d\n",
4869 POINTER_TO_OFFSET (old_regend[*p]));
4872 DEBUG_PRINT2 (" regend: %d\n", POINTER_TO_OFFSET (regend[*p]));
4874 /* This register isn't active anymore. */
4875 IS_ACTIVE (reg_info[*p]) = 0;
4877 /* Clear this whenever we change the register activity status. */
4878 set_regs_matched_done = 0;
4880 /* If this was the only register active, nothing is active
4882 if (lowest_active_reg == highest_active_reg)
4884 lowest_active_reg = NO_LOWEST_ACTIVE_REG;
4885 highest_active_reg = NO_HIGHEST_ACTIVE_REG;
4888 { /* We must scan for the new highest active register, since
4889 it isn't necessarily one less than now: consider
4890 (a(b)c(d(e)f)g). When group 3 ends, after the f), the
4891 new highest active register is 1. */
4892 unsigned char r = *p - 1;
4893 while (r > 0 && !IS_ACTIVE (reg_info[r]))
4896 /* If we end up at register zero, that means that we saved
4897 the registers as the result of an `on_failure_jump', not
4898 a `start_memory', and we jumped to past the innermost
4899 `stop_memory'. For example, in ((.)*) we save
4900 registers 1 and 2 as a result of the *, but when we pop
4901 back to the second ), we are at the stop_memory 1.
4902 Thus, nothing is active. */
4905 lowest_active_reg = NO_LOWEST_ACTIVE_REG;
4906 highest_active_reg = NO_HIGHEST_ACTIVE_REG;
4910 highest_active_reg = r;
4912 /* 98/9/21 jhod: We've also gotta set lowest_active_reg, don't we? */
4914 while (r < highest_active_reg && !IS_ACTIVE(reg_info[r]))
4916 lowest_active_reg = r;
4920 /* If just failed to match something this time around with a
4921 group that's operated on by a repetition operator, try to
4922 force exit from the ``loop'', and restore the register
4923 information for this group that we had before trying this
4925 if ((!MATCHED_SOMETHING (reg_info[*p])
4926 || just_past_start_mem == p - 1)
4929 boolean is_a_jump_n = false;
4933 switch ((re_opcode_t) *p1++)
4937 case pop_failure_jump:
4938 case maybe_pop_jump:
4940 case dummy_failure_jump:
4941 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
4951 /* If the next operation is a jump backwards in the pattern
4952 to an on_failure_jump right before the start_memory
4953 corresponding to this stop_memory, exit from the loop
4954 by forcing a failure after pushing on the stack the
4955 on_failure_jump's jump in the pattern, and d. */
4956 if (mcnt < 0 && (re_opcode_t) *p1 == on_failure_jump
4957 && (re_opcode_t) p1[3] == start_memory && p1[4] == *p)
4959 /* If this group ever matched anything, then restore
4960 what its registers were before trying this last
4961 failed match, e.g., with `(a*)*b' against `ab' for
4962 regstart[1], and, e.g., with `((a*)*(b*)*)*'
4963 against `aba' for regend[3].
4965 Also restore the registers for inner groups for,
4966 e.g., `((a*)(b*))*' against `aba' (register 3 would
4967 otherwise get trashed). */
4969 if (EVER_MATCHED_SOMETHING (reg_info[*p]))
4973 EVER_MATCHED_SOMETHING (reg_info[*p]) = 0;
4975 /* Restore this and inner groups' (if any) registers. */
4976 for (r = *p; r < *p + *(p + 1); r++)
4978 regstart[r] = old_regstart[r];
4980 /* xx why this test? */
4981 if (old_regend[r] >= regstart[r])
4982 regend[r] = old_regend[r];
4986 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
4987 PUSH_FAILURE_POINT (p1 + mcnt, d, -2);
4993 /* Move past the register number and the inner group count. */
4998 /* \<digit> has been turned into a `duplicate' command which is
4999 followed by the numeric value of <digit> as the register number. */
5002 REGISTER re_char *d2, *dend2;
5003 int regno = *p++; /* Get which register to match against. */
5004 DEBUG_PRINT2 ("EXECUTING duplicate %d.\n", regno);
5006 /* Can't back reference a group which we've never matched. */
5007 if (REG_UNSET (regstart[regno]) || REG_UNSET (regend[regno]))
5010 /* Where in input to try to start matching. */
5011 d2 = regstart[regno];
5013 /* Where to stop matching; if both the place to start and
5014 the place to stop matching are in the same string, then
5015 set to the place to stop, otherwise, for now have to use
5016 the end of the first string. */
5018 dend2 = ((FIRST_STRING_P (regstart[regno])
5019 == FIRST_STRING_P (regend[regno]))
5020 ? regend[regno] : end_match_1);
5023 /* If necessary, advance to next segment in register
5027 if (dend2 == end_match_2) break;
5028 if (dend2 == regend[regno]) break;
5030 /* End of string1 => advance to string2. */
5032 dend2 = regend[regno];
5034 /* At end of register contents => success */
5035 if (d2 == dend2) break;
5037 /* If necessary, advance to next segment in data. */
5040 /* How many characters left in this segment to match. */
5043 /* Want how many consecutive characters we can match in
5044 one shot, so, if necessary, adjust the count. */
5045 if (mcnt > dend2 - d2)
5048 /* Compare that many; failure if mismatch, else move
5050 if (TRANSLATE_P (translate)
5051 ? bcmp_translate ((unsigned char *) d,
5052 (unsigned char *) d2, mcnt, translate)
5053 : memcmp (d, d2, mcnt))
5055 d += mcnt, d2 += mcnt;
5057 /* Do this because we've match some characters. */
5058 SET_REGS_MATCHED ();
5064 /* begline matches the empty string at the beginning of the string
5065 (unless `not_bol' is set in `bufp'), and, if
5066 `newline_anchor' is set, after newlines. */
5068 DEBUG_PRINT1 ("EXECUTING begline.\n");
5070 if (AT_STRINGS_BEG (d))
5072 if (!bufp->not_bol) break;
5074 else if (d[-1] == '\n' && bufp->newline_anchor)
5078 /* In all other cases, we fail. */
5082 /* endline is the dual of begline. */
5084 DEBUG_PRINT1 ("EXECUTING endline.\n");
5086 if (AT_STRINGS_END (d))
5088 if (!bufp->not_eol) break;
5091 /* We have to ``prefetch'' the next character. */
5092 else if ((d == end1 ? *string2 : *d) == '\n'
5093 && bufp->newline_anchor)
5100 /* Match at the very beginning of the data. */
5102 DEBUG_PRINT1 ("EXECUTING begbuf.\n");
5103 if (AT_STRINGS_BEG (d))
5108 /* Match at the very end of the data. */
5110 DEBUG_PRINT1 ("EXECUTING endbuf.\n");
5111 if (AT_STRINGS_END (d))
5116 /* on_failure_keep_string_jump is used to optimize `.*\n'. It
5117 pushes NULL as the value for the string on the stack. Then
5118 `pop_failure_point' will keep the current value for the
5119 string, instead of restoring it. To see why, consider
5120 matching `foo\nbar' against `.*\n'. The .* matches the foo;
5121 then the . fails against the \n. But the next thing we want
5122 to do is match the \n against the \n; if we restored the
5123 string value, we would be back at the foo.
5125 Because this is used only in specific cases, we don't need to
5126 check all the things that `on_failure_jump' does, to make
5127 sure the right things get saved on the stack. Hence we don't
5128 share its code. The only reason to push anything on the
5129 stack at all is that otherwise we would have to change
5130 `anychar's code to do something besides goto fail in this
5131 case; that seems worse than this. */
5132 case on_failure_keep_string_jump:
5133 DEBUG_PRINT1 ("EXECUTING on_failure_keep_string_jump");
5135 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5136 DEBUG_PRINT3 (" %d (to 0x%lx):\n", mcnt, (long) (p + mcnt));
5138 PUSH_FAILURE_POINT (p + mcnt, (unsigned char *) 0, -2);
5142 /* Uses of on_failure_jump:
5144 Each alternative starts with an on_failure_jump that points
5145 to the beginning of the next alternative. Each alternative
5146 except the last ends with a jump that in effect jumps past
5147 the rest of the alternatives. (They really jump to the
5148 ending jump of the following alternative, because tensioning
5149 these jumps is a hassle.)
5151 Repeats start with an on_failure_jump that points past both
5152 the repetition text and either the following jump or
5153 pop_failure_jump back to this on_failure_jump. */
5154 case on_failure_jump:
5156 DEBUG_PRINT1 ("EXECUTING on_failure_jump");
5158 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5159 DEBUG_PRINT3 (" %d (to 0x%lx)", mcnt, (long) (p + mcnt));
5161 /* If this on_failure_jump comes right before a group (i.e.,
5162 the original * applied to a group), save the information
5163 for that group and all inner ones, so that if we fail back
5164 to this point, the group's information will be correct.
5165 For example, in \(a*\)*\1, we need the preceding group,
5166 and in \(\(a*\)b*\)\2, we need the inner group. */
5168 /* We can't use `p' to check ahead because we push
5169 a failure point to `p + mcnt' after we do this. */
5172 /* We need to skip no_op's before we look for the
5173 start_memory in case this on_failure_jump is happening as
5174 the result of a completed succeed_n, as in \(a\)\{1,3\}b\1
5176 while (p1 < pend && (re_opcode_t) *p1 == no_op)
5179 if (p1 < pend && (re_opcode_t) *p1 == start_memory)
5181 /* We have a new highest active register now. This will
5182 get reset at the start_memory we are about to get to,
5183 but we will have saved all the registers relevant to
5184 this repetition op, as described above. */
5185 highest_active_reg = *(p1 + 1) + *(p1 + 2);
5186 if (lowest_active_reg == NO_LOWEST_ACTIVE_REG)
5187 lowest_active_reg = *(p1 + 1);
5190 DEBUG_PRINT1 (":\n");
5191 PUSH_FAILURE_POINT (p + mcnt, d, -2);
5195 /* A smart repeat ends with `maybe_pop_jump'.
5196 We change it to either `pop_failure_jump' or `jump'. */
5197 case maybe_pop_jump:
5198 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5199 DEBUG_PRINT2 ("EXECUTING maybe_pop_jump %d.\n", mcnt);
5201 REGISTER unsigned char *p2 = p;
5203 /* Compare the beginning of the repeat with what in the
5204 pattern follows its end. If we can establish that there
5205 is nothing that they would both match, i.e., that we
5206 would have to backtrack because of (as in, e.g., `a*a')
5207 then we can change to pop_failure_jump, because we'll
5208 never have to backtrack.
5210 This is not true in the case of alternatives: in
5211 `(a|ab)*' we do need to backtrack to the `ab' alternative
5212 (e.g., if the string was `ab'). But instead of trying to
5213 detect that here, the alternative has put on a dummy
5214 failure point which is what we will end up popping. */
5216 /* Skip over open/close-group commands.
5217 If what follows this loop is a ...+ construct,
5218 look at what begins its body, since we will have to
5219 match at least one of that. */
5223 && ((re_opcode_t) *p2 == stop_memory
5224 || (re_opcode_t) *p2 == start_memory))
5226 else if (p2 + 6 < pend
5227 && (re_opcode_t) *p2 == dummy_failure_jump)
5234 /* p1[0] ... p1[2] are the `on_failure_jump' corresponding
5235 to the `maybe_finalize_jump' of this case. Examine what
5238 /* If we're at the end of the pattern, we can change. */
5241 /* Consider what happens when matching ":\(.*\)"
5242 against ":/". I don't really understand this code
5244 p[-3] = (unsigned char) pop_failure_jump;
5246 (" End of pattern: change to `pop_failure_jump'.\n");
5249 else if ((re_opcode_t) *p2 == exactn
5250 || (bufp->newline_anchor && (re_opcode_t) *p2 == endline))
5252 REGISTER unsigned char c
5253 = *p2 == (unsigned char) endline ? '\n' : p2[2];
5255 if ((re_opcode_t) p1[3] == exactn && p1[5] != c)
5257 p[-3] = (unsigned char) pop_failure_jump;
5258 DEBUG_PRINT3 (" %c != %c => pop_failure_jump.\n",
5262 else if ((re_opcode_t) p1[3] == charset
5263 || (re_opcode_t) p1[3] == charset_not)
5265 int not = (re_opcode_t) p1[3] == charset_not;
5267 if (c < (unsigned char) (p1[4] * BYTEWIDTH)
5268 && p1[5 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
5271 /* `not' is equal to 1 if c would match, which means
5272 that we can't change to pop_failure_jump. */
5275 p[-3] = (unsigned char) pop_failure_jump;
5276 DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
5280 else if ((re_opcode_t) *p2 == charset)
5283 REGISTER unsigned char c
5284 = *p2 == (unsigned char) endline ? '\n' : p2[2];
5287 if ((re_opcode_t) p1[3] == exactn
5288 && ! ((int) p2[1] * BYTEWIDTH > (int) p1[5]
5289 && (p2[2 + p1[5] / BYTEWIDTH]
5290 & (1 << (p1[5] % BYTEWIDTH)))))
5292 p[-3] = (unsigned char) pop_failure_jump;
5293 DEBUG_PRINT3 (" %c != %c => pop_failure_jump.\n",
5297 else if ((re_opcode_t) p1[3] == charset_not)
5300 /* We win if the charset_not inside the loop
5301 lists every character listed in the charset after. */
5302 for (idx = 0; idx < (int) p2[1]; idx++)
5303 if (! (p2[2 + idx] == 0
5304 || (idx < (int) p1[4]
5305 && ((p2[2 + idx] & ~ p1[5 + idx]) == 0))))
5310 p[-3] = (unsigned char) pop_failure_jump;
5311 DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
5314 else if ((re_opcode_t) p1[3] == charset)
5317 /* We win if the charset inside the loop
5318 has no overlap with the one after the loop. */
5320 idx < (int) p2[1] && idx < (int) p1[4];
5322 if ((p2[2 + idx] & p1[5 + idx]) != 0)
5325 if (idx == p2[1] || idx == p1[4])
5327 p[-3] = (unsigned char) pop_failure_jump;
5328 DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
5333 p -= 2; /* Point at relative address again. */
5334 if ((re_opcode_t) p[-1] != pop_failure_jump)
5336 p[-1] = (unsigned char) jump;
5337 DEBUG_PRINT1 (" Match => jump.\n");
5338 goto unconditional_jump;
5340 /* Note fall through. */
5343 /* The end of a simple repeat has a pop_failure_jump back to
5344 its matching on_failure_jump, where the latter will push a
5345 failure point. The pop_failure_jump takes off failure
5346 points put on by this pop_failure_jump's matching
5347 on_failure_jump; we got through the pattern to here from the
5348 matching on_failure_jump, so didn't fail. */
5349 case pop_failure_jump:
5351 /* We need to pass separate storage for the lowest and
5352 highest registers, even though we don't care about the
5353 actual values. Otherwise, we will restore only one
5354 register from the stack, since lowest will == highest in
5355 `pop_failure_point'. */
5356 unsigned dummy_low_reg, dummy_high_reg;
5357 unsigned char *pdummy;
5358 re_char *sdummy = NULL;
5360 DEBUG_PRINT1 ("EXECUTING pop_failure_jump.\n");
5361 POP_FAILURE_POINT (sdummy, pdummy,
5362 dummy_low_reg, dummy_high_reg,
5363 reg_dummy, reg_dummy, reg_info_dummy);
5365 /* Note fall through. */
5368 /* Unconditionally jump (without popping any failure points). */
5371 EXTRACT_NUMBER_AND_INCR (mcnt, p); /* Get the amount to jump. */
5372 DEBUG_PRINT2 ("EXECUTING jump %d ", mcnt);
5373 p += mcnt; /* Do the jump. */
5374 DEBUG_PRINT2 ("(to 0x%lx).\n", (long) p);
5378 /* We need this opcode so we can detect where alternatives end
5379 in `group_match_null_string_p' et al. */
5381 DEBUG_PRINT1 ("EXECUTING jump_past_alt.\n");
5382 goto unconditional_jump;
5385 /* Normally, the on_failure_jump pushes a failure point, which
5386 then gets popped at pop_failure_jump. We will end up at
5387 pop_failure_jump, also, and with a pattern of, say, `a+', we
5388 are skipping over the on_failure_jump, so we have to push
5389 something meaningless for pop_failure_jump to pop. */
5390 case dummy_failure_jump:
5391 DEBUG_PRINT1 ("EXECUTING dummy_failure_jump.\n");
5392 /* It doesn't matter what we push for the string here. What
5393 the code at `fail' tests is the value for the pattern. */
5394 PUSH_FAILURE_POINT ((unsigned char *) 0, (unsigned char *) 0, -2);
5395 goto unconditional_jump;
5398 /* At the end of an alternative, we need to push a dummy failure
5399 point in case we are followed by a `pop_failure_jump', because
5400 we don't want the failure point for the alternative to be
5401 popped. For example, matching `(a|ab)*' against `aab'
5402 requires that we match the `ab' alternative. */
5403 case push_dummy_failure:
5404 DEBUG_PRINT1 ("EXECUTING push_dummy_failure.\n");
5405 /* See comments just above at `dummy_failure_jump' about the
5407 PUSH_FAILURE_POINT ((unsigned char *) 0, (unsigned char *) 0, -2);
5410 /* Have to succeed matching what follows at least n times.
5411 After that, handle like `on_failure_jump'. */
5413 EXTRACT_NUMBER (mcnt, p + 2);
5414 DEBUG_PRINT2 ("EXECUTING succeed_n %d.\n", mcnt);
5417 /* Originally, this is how many times we HAVE to succeed. */
5422 STORE_NUMBER_AND_INCR (p, mcnt);
5423 DEBUG_PRINT3 (" Setting 0x%lx to %d.\n", (long) p, mcnt);
5427 DEBUG_PRINT2 (" Setting two bytes from 0x%lx to no_op.\n",
5429 p[2] = (unsigned char) no_op;
5430 p[3] = (unsigned char) no_op;
5436 EXTRACT_NUMBER (mcnt, p + 2);
5437 DEBUG_PRINT2 ("EXECUTING jump_n %d.\n", mcnt);
5439 /* Originally, this is how many times we CAN jump. */
5443 STORE_NUMBER (p + 2, mcnt);
5444 goto unconditional_jump;
5446 /* If don't have to jump any more, skip over the rest of command. */
5453 DEBUG_PRINT1 ("EXECUTING set_number_at.\n");
5455 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5457 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5458 DEBUG_PRINT3 (" Setting 0x%lx to %d.\n", (long) p1, mcnt);
5459 STORE_NUMBER (p1, mcnt);
5464 DEBUG_PRINT1 ("EXECUTING wordbound.\n");
5470 if (AT_STRINGS_BEG (d) || AT_STRINGS_END (d))
5474 const unsigned char *d_before =
5475 (const unsigned char *) POS_BEFORE_GAP_UNSAFE (d);
5476 const unsigned char *d_after =
5477 (const unsigned char *) POS_AFTER_GAP_UNSAFE (d);
5478 Emchar emch1, emch2;
5480 DEC_CHARPTR (d_before);
5481 emch1 = charptr_emchar (d_before);
5482 emch2 = charptr_emchar (d_after);
5483 result = (WORDCHAR_P_UNSAFE (emch1) !=
5484 WORDCHAR_P_UNSAFE (emch2));
5486 if (result == should_succeed)
5492 DEBUG_PRINT1 ("EXECUTING notwordbound.\n");
5494 goto matchwordbound;
5497 DEBUG_PRINT1 ("EXECUTING wordbeg.\n");
5499 /* XEmacs: this originally read:
5501 if (WORDCHAR_P (d) && (AT_STRINGS_BEG (d) || !WORDCHAR_P (d - 1)))
5505 const unsigned char *dtmp =
5506 (const unsigned char *) POS_AFTER_GAP_UNSAFE (d);
5507 Emchar emch = charptr_emchar (dtmp);
5508 if (!WORDCHAR_P_UNSAFE (emch))
5510 if (AT_STRINGS_BEG (d))
5512 dtmp = (const unsigned char *) POS_BEFORE_GAP_UNSAFE (d);
5514 emch = charptr_emchar (dtmp);
5515 if (!WORDCHAR_P_UNSAFE (emch))
5521 DEBUG_PRINT1 ("EXECUTING wordend.\n");
5523 /* XEmacs: this originally read:
5525 if (!AT_STRINGS_BEG (d) && WORDCHAR_P (d - 1)
5526 && (!WORDCHAR_P (d) || AT_STRINGS_END (d)))
5529 The or condition is incorrect (reversed).
5531 const unsigned char *dtmp;
5533 if (AT_STRINGS_BEG (d))
5535 dtmp = (const unsigned char *) POS_BEFORE_GAP_UNSAFE (d);
5537 emch = charptr_emchar (dtmp);
5538 if (!WORDCHAR_P_UNSAFE (emch))
5540 if (AT_STRINGS_END (d))
5542 dtmp = (const unsigned char *) POS_AFTER_GAP_UNSAFE (d);
5543 emch = charptr_emchar (dtmp);
5544 if (!WORDCHAR_P_UNSAFE (emch))
5551 DEBUG_PRINT1 ("EXECUTING before_dot.\n");
5552 if (!regex_emacs_buffer_p
5553 || (BUF_PTR_BYTE_POS (regex_emacs_buffer, (unsigned char *) d)
5554 >= BUF_PT (regex_emacs_buffer)))
5559 DEBUG_PRINT1 ("EXECUTING at_dot.\n");
5560 if (!regex_emacs_buffer_p
5561 || (BUF_PTR_BYTE_POS (regex_emacs_buffer, (unsigned char *) d)
5562 != BUF_PT (regex_emacs_buffer)))
5567 DEBUG_PRINT1 ("EXECUTING after_dot.\n");
5568 if (!regex_emacs_buffer_p
5569 || (BUF_PTR_BYTE_POS (regex_emacs_buffer, (unsigned char *) d)
5570 <= BUF_PT (regex_emacs_buffer)))
5573 #if 0 /* not emacs19 */
5575 DEBUG_PRINT1 ("EXECUTING at_dot.\n");
5576 if (BUF_PTR_BYTE_POS (regex_emacs_buffer, (unsigned char *) d) + 1
5577 != BUF_PT (regex_emacs_buffer))
5580 #endif /* not emacs19 */
5583 DEBUG_PRINT2 ("EXECUTING syntaxspec %d.\n", mcnt);
5588 DEBUG_PRINT1 ("EXECUTING Emacs wordchar.\n");
5598 emch = charptr_emchar ((const Bufbyte *) d);
5599 matches = (SYNTAX_UNSAFE
5600 (XCHAR_TABLE (regex_emacs_buffer->mirror_syntax_table),
5601 emch) == (enum syntaxcode) mcnt);
5603 if (matches != should_succeed)
5605 SET_REGS_MATCHED ();
5610 DEBUG_PRINT2 ("EXECUTING notsyntaxspec %d.\n", mcnt);
5612 goto matchnotsyntax;
5615 DEBUG_PRINT1 ("EXECUTING Emacs notwordchar.\n");
5619 goto matchornotsyntax;
5622 /* 97/2/17 jhod Mule category code patch */
5631 emch = charptr_emchar ((const Bufbyte *) d);
5633 if (check_category_char(emch, regex_emacs_buffer->category_table,
5634 mcnt, should_succeed))
5636 SET_REGS_MATCHED ();
5640 case notcategoryspec:
5642 goto matchornotcategory;
5643 /* end of category patch */
5645 #else /* not emacs */
5647 DEBUG_PRINT1 ("EXECUTING non-Emacs wordchar.\n");
5649 if (!WORDCHAR_P_UNSAFE ((int) (*d)))
5651 SET_REGS_MATCHED ();
5656 DEBUG_PRINT1 ("EXECUTING non-Emacs notwordchar.\n");
5658 if (!WORDCHAR_P_UNSAFE ((int) (*d)))
5660 SET_REGS_MATCHED ();
5668 continue; /* Successfully executed one pattern command; keep going. */
5671 /* We goto here if a matching operation fails. */
5673 if (!FAIL_STACK_EMPTY ())
5674 { /* A restart point is known. Restore to that state. */
5675 DEBUG_PRINT1 ("\nFAIL:\n");
5676 POP_FAILURE_POINT (d, p,
5677 lowest_active_reg, highest_active_reg,
5678 regstart, regend, reg_info);
5680 /* If this failure point is a dummy, try the next one. */
5684 /* If we failed to the end of the pattern, don't examine *p. */
5688 boolean is_a_jump_n = false;
5690 /* If failed to a backwards jump that's part of a repetition
5691 loop, need to pop this failure point and use the next one. */
5692 switch ((re_opcode_t) *p)
5696 case maybe_pop_jump:
5697 case pop_failure_jump:
5700 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
5703 if ((is_a_jump_n && (re_opcode_t) *p1 == succeed_n)
5705 && (re_opcode_t) *p1 == on_failure_jump))
5713 if (d >= string1 && d <= end1)
5717 break; /* Matching at this starting point really fails. */
5721 goto restore_best_regs;
5725 return -1; /* Failure to match. */
5728 /* Subroutine definitions for re_match_2. */
5731 /* We are passed P pointing to a register number after a start_memory.
5733 Return true if the pattern up to the corresponding stop_memory can
5734 match the empty string, and false otherwise.
5736 If we find the matching stop_memory, sets P to point to one past its number.
5737 Otherwise, sets P to an undefined byte less than or equal to END.
5739 We don't handle duplicates properly (yet). */
5742 group_match_null_string_p (unsigned char **p, unsigned char *end,
5743 register_info_type *reg_info)
5746 /* Point to after the args to the start_memory. */
5747 unsigned char *p1 = *p + 2;
5751 /* Skip over opcodes that can match nothing, and return true or
5752 false, as appropriate, when we get to one that can't, or to the
5753 matching stop_memory. */
5755 switch ((re_opcode_t) *p1)
5757 /* Could be either a loop or a series of alternatives. */
5758 case on_failure_jump:
5760 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
5762 /* If the next operation is not a jump backwards in the
5767 /* Go through the on_failure_jumps of the alternatives,
5768 seeing if any of the alternatives cannot match nothing.
5769 The last alternative starts with only a jump,
5770 whereas the rest start with on_failure_jump and end
5771 with a jump, e.g., here is the pattern for `a|b|c':
5773 /on_failure_jump/0/6/exactn/1/a/jump_past_alt/0/6
5774 /on_failure_jump/0/6/exactn/1/b/jump_past_alt/0/3
5777 So, we have to first go through the first (n-1)
5778 alternatives and then deal with the last one separately. */
5781 /* Deal with the first (n-1) alternatives, which start
5782 with an on_failure_jump (see above) that jumps to right
5783 past a jump_past_alt. */
5785 while ((re_opcode_t) p1[mcnt-3] == jump_past_alt)
5787 /* `mcnt' holds how many bytes long the alternative
5788 is, including the ending `jump_past_alt' and
5791 if (!alt_match_null_string_p (p1, p1 + mcnt - 3,
5795 /* Move to right after this alternative, including the
5799 /* Break if it's the beginning of an n-th alternative
5800 that doesn't begin with an on_failure_jump. */
5801 if ((re_opcode_t) *p1 != on_failure_jump)
5804 /* Still have to check that it's not an n-th
5805 alternative that starts with an on_failure_jump. */
5807 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
5808 if ((re_opcode_t) p1[mcnt-3] != jump_past_alt)
5810 /* Get to the beginning of the n-th alternative. */
5816 /* Deal with the last alternative: go back and get number
5817 of the `jump_past_alt' just before it. `mcnt' contains
5818 the length of the alternative. */
5819 EXTRACT_NUMBER (mcnt, p1 - 2);
5821 if (!alt_match_null_string_p (p1, p1 + mcnt, reg_info))
5824 p1 += mcnt; /* Get past the n-th alternative. */
5830 assert (p1[1] == **p);
5836 if (!common_op_match_null_string_p (&p1, end, reg_info))
5839 } /* while p1 < end */
5842 } /* group_match_null_string_p */
5845 /* Similar to group_match_null_string_p, but doesn't deal with alternatives:
5846 It expects P to be the first byte of a single alternative and END one
5847 byte past the last. The alternative can contain groups. */
5850 alt_match_null_string_p (unsigned char *p, unsigned char *end,
5851 register_info_type *reg_info)
5854 unsigned char *p1 = p;
5858 /* Skip over opcodes that can match nothing, and break when we get
5859 to one that can't. */
5861 switch ((re_opcode_t) *p1)
5864 case on_failure_jump:
5866 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
5871 if (!common_op_match_null_string_p (&p1, end, reg_info))
5874 } /* while p1 < end */
5877 } /* alt_match_null_string_p */
5880 /* Deals with the ops common to group_match_null_string_p and
5881 alt_match_null_string_p.
5883 Sets P to one after the op and its arguments, if any. */
5886 common_op_match_null_string_p (unsigned char **p, unsigned char *end,
5887 register_info_type *reg_info)
5892 unsigned char *p1 = *p;
5894 switch ((re_opcode_t) *p1++)
5914 assert (reg_no > 0 && reg_no <= MAX_REGNUM);
5915 ret = group_match_null_string_p (&p1, end, reg_info);
5917 /* Have to set this here in case we're checking a group which
5918 contains a group and a back reference to it. */
5920 if (REG_MATCH_NULL_STRING_P (reg_info[reg_no]) == MATCH_NULL_UNSET_VALUE)
5921 REG_MATCH_NULL_STRING_P (reg_info[reg_no]) = ret;
5927 /* If this is an optimized succeed_n for zero times, make the jump. */
5929 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
5937 /* Get to the number of times to succeed. */
5939 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
5944 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
5952 if (!REG_MATCH_NULL_STRING_P (reg_info[*p1]))
5960 /* All other opcodes mean we cannot match the empty string. */
5966 } /* common_op_match_null_string_p */
5969 /* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
5970 bytes; nonzero otherwise. */
5973 bcmp_translate (re_char *s1, re_char *s2,
5974 REGISTER int len, RE_TRANSLATE_TYPE translate)
5976 REGISTER const unsigned char *p1 = s1, *p2 = s2;
5978 const unsigned char *p1_end = s1 + len;
5979 const unsigned char *p2_end = s2 + len;
5981 while (p1 != p1_end && p2 != p2_end)
5983 Emchar p1_ch, p2_ch;
5985 p1_ch = charptr_emchar (p1);
5986 p2_ch = charptr_emchar (p2);
5988 if (RE_TRANSLATE (p1_ch)
5989 != RE_TRANSLATE (p2_ch))
5994 #else /* not MULE */
5997 if (RE_TRANSLATE (*p1++) != RE_TRANSLATE (*p2++)) return 1;
6004 /* Entry points for GNU code. */
6006 /* re_compile_pattern is the GNU regular expression compiler: it
6007 compiles PATTERN (of length SIZE) and puts the result in BUFP.
6008 Returns 0 if the pattern was valid, otherwise an error string.
6010 Assumes the `allocated' (and perhaps `buffer') and `translate' fields
6011 are set in BUFP on entry.
6013 We call regex_compile to do the actual compilation. */
6016 re_compile_pattern (const char *pattern, int length,
6017 struct re_pattern_buffer *bufp)
6021 /* GNU code is written to assume at least RE_NREGS registers will be set
6022 (and at least one extra will be -1). */
6023 bufp->regs_allocated = REGS_UNALLOCATED;
6025 /* And GNU code determines whether or not to get register information
6026 by passing null for the REGS argument to re_match, etc., not by
6030 /* Match anchors at newline. */
6031 bufp->newline_anchor = 1;
6033 ret = regex_compile ((unsigned char *) pattern, length, re_syntax_options, bufp);
6037 return gettext (re_error_msgid[(int) ret]);
6040 /* Entry points compatible with 4.2 BSD regex library. We don't define
6041 them unless specifically requested. */
6043 #ifdef _REGEX_RE_COMP
6045 /* BSD has one and only one pattern buffer. */
6046 static struct re_pattern_buffer re_comp_buf;
6049 re_comp (const char *s)
6055 if (!re_comp_buf.buffer)
6056 return gettext ("No previous regular expression");
6060 if (!re_comp_buf.buffer)
6062 re_comp_buf.buffer = (unsigned char *) malloc (200);
6063 if (re_comp_buf.buffer == NULL)
6064 return gettext (re_error_msgid[(int) REG_ESPACE]);
6065 re_comp_buf.allocated = 200;
6067 re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH);
6068 if (re_comp_buf.fastmap == NULL)
6069 return gettext (re_error_msgid[(int) REG_ESPACE]);
6072 /* Since `re_exec' always passes NULL for the `regs' argument, we
6073 don't need to initialize the pattern buffer fields which affect it. */
6075 /* Match anchors at newlines. */
6076 re_comp_buf.newline_anchor = 1;
6078 ret = regex_compile ((unsigned char *)s, strlen (s), re_syntax_options, &re_comp_buf);
6083 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6084 return (char *) gettext (re_error_msgid[(int) ret]);
6089 re_exec (const char *s)
6091 const int len = strlen (s);
6093 0 <= re_search (&re_comp_buf, s, len, 0, len, (struct re_registers *) 0);
6095 #endif /* _REGEX_RE_COMP */
6097 /* POSIX.2 functions. Don't define these for Emacs. */
6101 /* regcomp takes a regular expression as a string and compiles it.
6103 PREG is a regex_t *. We do not expect any fields to be initialized,
6104 since POSIX says we shouldn't. Thus, we set
6106 `buffer' to the compiled pattern;
6107 `used' to the length of the compiled pattern;
6108 `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
6109 REG_EXTENDED bit in CFLAGS is set; otherwise, to
6110 RE_SYNTAX_POSIX_BASIC;
6111 `newline_anchor' to REG_NEWLINE being set in CFLAGS;
6112 `fastmap' and `fastmap_accurate' to zero;
6113 `re_nsub' to the number of subexpressions in PATTERN.
6115 PATTERN is the address of the pattern string.
6117 CFLAGS is a series of bits which affect compilation.
6119 If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
6120 use POSIX basic syntax.
6122 If REG_NEWLINE is set, then . and [^...] don't match newline.
6123 Also, regexec will try a match beginning after every newline.
6125 If REG_ICASE is set, then we considers upper- and lowercase
6126 versions of letters to be equivalent when matching.
6128 If REG_NOSUB is set, then when PREG is passed to regexec, that
6129 routine will report only success or failure, and nothing about the
6132 It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
6133 the return codes and their meanings.) */
6136 regcomp (regex_t *preg, const char *pattern, int cflags)
6140 = (cflags & REG_EXTENDED) ?
6141 RE_SYNTAX_POSIX_EXTENDED : RE_SYNTAX_POSIX_BASIC;
6143 /* regex_compile will allocate the space for the compiled pattern. */
6145 preg->allocated = 0;
6148 /* Don't bother to use a fastmap when searching. This simplifies the
6149 REG_NEWLINE case: if we used a fastmap, we'd have to put all the
6150 characters after newlines into the fastmap. This way, we just try
6154 if (cflags & REG_ICASE)
6158 preg->translate = (char *) malloc (CHAR_SET_SIZE);
6159 if (preg->translate == NULL)
6160 return (int) REG_ESPACE;
6162 /* Map uppercase characters to corresponding lowercase ones. */
6163 for (i = 0; i < CHAR_SET_SIZE; i++)
6164 preg->translate[i] = ISUPPER (i) ? tolower (i) : i;
6167 preg->translate = NULL;
6169 /* If REG_NEWLINE is set, newlines are treated differently. */
6170 if (cflags & REG_NEWLINE)
6171 { /* REG_NEWLINE implies neither . nor [^...] match newline. */
6172 syntax &= ~RE_DOT_NEWLINE;
6173 syntax |= RE_HAT_LISTS_NOT_NEWLINE;
6174 /* It also changes the matching behavior. */
6175 preg->newline_anchor = 1;
6178 preg->newline_anchor = 0;
6180 preg->no_sub = !!(cflags & REG_NOSUB);
6182 /* POSIX says a null character in the pattern terminates it, so we
6183 can use strlen here in compiling the pattern. */
6184 ret = regex_compile ((unsigned char *) pattern, strlen (pattern), syntax, preg);
6186 /* POSIX doesn't distinguish between an unmatched open-group and an
6187 unmatched close-group: both are REG_EPAREN. */
6188 if (ret == REG_ERPAREN) ret = REG_EPAREN;
6194 /* regexec searches for a given pattern, specified by PREG, in the
6197 If NMATCH is zero or REG_NOSUB was set in the cflags argument to
6198 `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
6199 least NMATCH elements, and we set them to the offsets of the
6200 corresponding matched substrings.
6202 EFLAGS specifies `execution flags' which affect matching: if
6203 REG_NOTBOL is set, then ^ does not match at the beginning of the
6204 string; if REG_NOTEOL is set, then $ does not match at the end.
6206 We return 0 if we find a match and REG_NOMATCH if not. */
6209 regexec (const regex_t *preg, const char *string, size_t nmatch,
6210 regmatch_t pmatch[], int eflags)
6213 struct re_registers regs;
6214 regex_t private_preg;
6215 int len = strlen (string);
6216 boolean want_reg_info = !preg->no_sub && nmatch > 0;
6218 private_preg = *preg;
6220 private_preg.not_bol = !!(eflags & REG_NOTBOL);
6221 private_preg.not_eol = !!(eflags & REG_NOTEOL);
6223 /* The user has told us exactly how many registers to return
6224 information about, via `nmatch'. We have to pass that on to the
6225 matching routines. */
6226 private_preg.regs_allocated = REGS_FIXED;
6230 regs.num_regs = nmatch;
6231 regs.start = TALLOC (nmatch, regoff_t);
6232 regs.end = TALLOC (nmatch, regoff_t);
6233 if (regs.start == NULL || regs.end == NULL)
6234 return (int) REG_NOMATCH;
6237 /* Perform the searching operation. */
6238 ret = re_search (&private_preg, string, len,
6239 /* start: */ 0, /* range: */ len,
6240 want_reg_info ? ®s : (struct re_registers *) 0);
6242 /* Copy the register information to the POSIX structure. */
6249 for (r = 0; r < nmatch; r++)
6251 pmatch[r].rm_so = regs.start[r];
6252 pmatch[r].rm_eo = regs.end[r];
6256 /* If we needed the temporary register info, free the space now. */
6261 /* We want zero return to mean success, unlike `re_search'. */
6262 return ret >= 0 ? (int) REG_NOERROR : (int) REG_NOMATCH;
6266 /* Returns a message corresponding to an error code, ERRCODE, returned
6267 from either regcomp or regexec. We don't use PREG here. */
6270 regerror (int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
6276 || errcode >= (sizeof (re_error_msgid) / sizeof (re_error_msgid[0])))
6277 /* Only error codes returned by the rest of the code should be passed
6278 to this routine. If we are given anything else, or if other regex
6279 code generates an invalid error code, then the program has a bug.
6280 Dump core so we can fix it. */
6283 msg = gettext (re_error_msgid[errcode]);
6285 msg_size = strlen (msg) + 1; /* Includes the null. */
6287 if (errbuf_size != 0)
6289 if (msg_size > errbuf_size)
6291 strncpy (errbuf, msg, errbuf_size - 1);
6292 errbuf[errbuf_size - 1] = 0;
6295 strcpy (errbuf, msg);
6302 /* Free dynamically allocated space used by PREG. */
6305 regfree (regex_t *preg)
6307 if (preg->buffer != NULL)
6308 free (preg->buffer);
6309 preg->buffer = NULL;
6311 preg->allocated = 0;
6314 if (preg->fastmap != NULL)
6315 free (preg->fastmap);
6316 preg->fastmap = NULL;
6317 preg->fastmap_accurate = 0;
6319 if (preg->translate != NULL)
6320 free (preg->translate);
6321 preg->translate = NULL;
6324 #endif /* not emacs */
6328 make-backup-files: t
6330 trim-versions-without-asking: nil