*** empty log message ***
[emacs.git] / src / regex.c
blobe793df3da8175d6218a53ee2e33abfae7fb5cbe5
1 /* Extended regular expression matching and search library, version
2 0.12. (Implements POSIX draft P1003.2/D11.2, except for some of the
3 internationalization features.)
5 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
6 2002, 2003, 2004, 2005, 2006, 2007
7 Free Software Foundation, Inc.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 USA. */
24 /* TODO:
25 - structure the opcode space into opcode+flag.
26 - merge with glibc's regex.[ch].
27 - replace (succeed_n + jump_n + set_number_at) with something that doesn't
28 need to modify the compiled regexp so that re_match can be reentrant.
29 - get rid of on_failure_jump_smart by doing the optimization in re_comp
30 rather than at run-time, so that re_match can be reentrant.
33 /* AIX requires this to be the first thing in the file. */
34 #if defined _AIX && !defined REGEX_MALLOC
35 #pragma alloca
36 #endif
38 #ifdef HAVE_CONFIG_H
39 # include <config.h>
40 #endif
42 #if defined STDC_HEADERS && !defined emacs
43 # include <stddef.h>
44 #else
45 /* We need this for `regex.h', and perhaps for the Emacs include files. */
46 # include <sys/types.h>
47 #endif
49 /* Whether to use ISO C Amendment 1 wide char functions.
50 Those should not be used for Emacs since it uses its own. */
51 #if defined _LIBC
52 #define WIDE_CHAR_SUPPORT 1
53 #else
54 #define WIDE_CHAR_SUPPORT \
55 (HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_BTOWC && !emacs)
56 #endif
58 /* For platform which support the ISO C amendement 1 functionality we
59 support user defined character classes. */
60 #if WIDE_CHAR_SUPPORT
61 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
62 # include <wchar.h>
63 # include <wctype.h>
64 #endif
66 #ifdef _LIBC
67 /* We have to keep the namespace clean. */
68 # define regfree(preg) __regfree (preg)
69 # define regexec(pr, st, nm, pm, ef) __regexec (pr, st, nm, pm, ef)
70 # define regcomp(preg, pattern, cflags) __regcomp (preg, pattern, cflags)
71 # define regerror(err_code, preg, errbuf, errbuf_size) \
72 __regerror(err_code, preg, errbuf, errbuf_size)
73 # define re_set_registers(bu, re, nu, st, en) \
74 __re_set_registers (bu, re, nu, st, en)
75 # define re_match_2(bufp, string1, size1, string2, size2, pos, regs, stop) \
76 __re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
77 # define re_match(bufp, string, size, pos, regs) \
78 __re_match (bufp, string, size, pos, regs)
79 # define re_search(bufp, string, size, startpos, range, regs) \
80 __re_search (bufp, string, size, startpos, range, regs)
81 # define re_compile_pattern(pattern, length, bufp) \
82 __re_compile_pattern (pattern, length, bufp)
83 # define re_set_syntax(syntax) __re_set_syntax (syntax)
84 # define re_search_2(bufp, st1, s1, st2, s2, startpos, range, regs, stop) \
85 __re_search_2 (bufp, st1, s1, st2, s2, startpos, range, regs, stop)
86 # define re_compile_fastmap(bufp) __re_compile_fastmap (bufp)
88 /* Make sure we call libc's function even if the user overrides them. */
89 # define btowc __btowc
90 # define iswctype __iswctype
91 # define wctype __wctype
93 # define WEAK_ALIAS(a,b) weak_alias (a, b)
95 /* We are also using some library internals. */
96 # include <locale/localeinfo.h>
97 # include <locale/elem-hash.h>
98 # include <langinfo.h>
99 #else
100 # define WEAK_ALIAS(a,b)
101 #endif
103 /* This is for other GNU distributions with internationalized messages. */
104 #if HAVE_LIBINTL_H || defined _LIBC
105 # include <libintl.h>
106 #else
107 # define gettext(msgid) (msgid)
108 #endif
110 #ifndef gettext_noop
111 /* This define is so xgettext can find the internationalizable
112 strings. */
113 # define gettext_noop(String) String
114 #endif
116 /* The `emacs' switch turns on certain matching commands
117 that make sense only in Emacs. */
118 #ifdef emacs
120 # include "lisp.h"
121 # include "buffer.h"
123 /* Make syntax table lookup grant data in gl_state. */
124 # define SYNTAX_ENTRY_VIA_PROPERTY
126 # include "syntax.h"
127 # include "charset.h"
128 # include "category.h"
130 # ifdef malloc
131 # undef malloc
132 # endif
133 # define malloc xmalloc
134 # ifdef realloc
135 # undef realloc
136 # endif
137 # define realloc xrealloc
138 # ifdef free
139 # undef free
140 # endif
141 # define free xfree
143 /* Converts the pointer to the char to BEG-based offset from the start. */
144 # define PTR_TO_OFFSET(d) POS_AS_IN_BUFFER (POINTER_TO_OFFSET (d))
145 # define POS_AS_IN_BUFFER(p) ((p) + (NILP (re_match_object) || BUFFERP (re_match_object)))
147 # define RE_MULTIBYTE_P(bufp) ((bufp)->multibyte)
148 # define RE_STRING_CHAR(p, s) \
149 (multibyte ? (STRING_CHAR (p, s)) : (*(p)))
150 # define RE_STRING_CHAR_AND_LENGTH(p, s, len) \
151 (multibyte ? (STRING_CHAR_AND_LENGTH (p, s, len)) : ((len) = 1, *(p)))
153 /* Set C a (possibly multibyte) character before P. P points into a
154 string which is the virtual concatenation of STR1 (which ends at
155 END1) or STR2 (which ends at END2). */
156 # define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2) \
157 do { \
158 if (multibyte) \
160 re_char *dtemp = (p) == (str2) ? (end1) : (p); \
161 re_char *dlimit = ((p) > (str2) && (p) <= (end2)) ? (str2) : (str1); \
162 re_char *d0 = dtemp; \
163 PREV_CHAR_BOUNDARY (d0, dlimit); \
164 c = STRING_CHAR (d0, dtemp - d0); \
166 else \
167 (c = ((p) == (str2) ? (end1) : (p))[-1]); \
168 } while (0)
171 #else /* not emacs */
173 /* If we are not linking with Emacs proper,
174 we can't use the relocating allocator
175 even if config.h says that we can. */
176 # undef REL_ALLOC
178 # if defined STDC_HEADERS || defined _LIBC
179 # include <stdlib.h>
180 # else
181 char *malloc ();
182 char *realloc ();
183 # endif
185 /* When used in Emacs's lib-src, we need xmalloc and xrealloc. */
187 void *
188 xmalloc (size)
189 size_t size;
191 register void *val;
192 val = (void *) malloc (size);
193 if (!val && size)
195 write (2, "virtual memory exhausted\n", 25);
196 exit (1);
198 return val;
201 void *
202 xrealloc (block, size)
203 void *block;
204 size_t size;
206 register void *val;
207 /* We must call malloc explicitly when BLOCK is 0, since some
208 reallocs don't do this. */
209 if (! block)
210 val = (void *) malloc (size);
211 else
212 val = (void *) realloc (block, size);
213 if (!val && size)
215 write (2, "virtual memory exhausted\n", 25);
216 exit (1);
218 return val;
221 # ifdef malloc
222 # undef malloc
223 # endif
224 # define malloc xmalloc
225 # ifdef realloc
226 # undef realloc
227 # endif
228 # define realloc xrealloc
230 /* When used in Emacs's lib-src, we need to get bzero and bcopy somehow.
231 If nothing else has been done, use the method below. */
232 # ifdef INHIBIT_STRING_HEADER
233 # if !(defined HAVE_BZERO && defined HAVE_BCOPY)
234 # if !defined bzero && !defined bcopy
235 # undef INHIBIT_STRING_HEADER
236 # endif
237 # endif
238 # endif
240 /* This is the normal way of making sure we have memcpy, memcmp and bzero.
241 This is used in most programs--a few other programs avoid this
242 by defining INHIBIT_STRING_HEADER. */
243 # ifndef INHIBIT_STRING_HEADER
244 # if defined HAVE_STRING_H || defined STDC_HEADERS || defined _LIBC
245 # include <string.h>
246 # ifndef bzero
247 # ifndef _LIBC
248 # define bzero(s, n) (memset (s, '\0', n), (s))
249 # else
250 # define bzero(s, n) __bzero (s, n)
251 # endif
252 # endif
253 # else
254 # include <strings.h>
255 # ifndef memcmp
256 # define memcmp(s1, s2, n) bcmp (s1, s2, n)
257 # endif
258 # ifndef memcpy
259 # define memcpy(d, s, n) (bcopy (s, d, n), (d))
260 # endif
261 # endif
262 # endif
264 /* Define the syntax stuff for \<, \>, etc. */
266 /* Sword must be nonzero for the wordchar pattern commands in re_match_2. */
267 enum syntaxcode { Swhitespace = 0, Sword = 1, Ssymbol = 2 };
269 # ifdef SWITCH_ENUM_BUG
270 # define SWITCH_ENUM_CAST(x) ((int)(x))
271 # else
272 # define SWITCH_ENUM_CAST(x) (x)
273 # endif
275 /* Dummy macros for non-Emacs environments. */
276 # define BASE_LEADING_CODE_P(c) (0)
277 # define CHAR_CHARSET(c) 0
278 # define CHARSET_LEADING_CODE_BASE(c) 0
279 # define MAX_MULTIBYTE_LENGTH 1
280 # define RE_MULTIBYTE_P(x) 0
281 # define WORD_BOUNDARY_P(c1, c2) (0)
282 # define CHAR_HEAD_P(p) (1)
283 # define SINGLE_BYTE_CHAR_P(c) (1)
284 # define SAME_CHARSET_P(c1, c2) (1)
285 # define MULTIBYTE_FORM_LENGTH(p, s) (1)
286 # define PREV_CHAR_BOUNDARY(p, limit) ((p)--)
287 # define STRING_CHAR(p, s) (*(p))
288 # define RE_STRING_CHAR STRING_CHAR
289 # define CHAR_STRING(c, s) (*(s) = (c), 1)
290 # define STRING_CHAR_AND_LENGTH(p, s, actual_len) ((actual_len) = 1, *(p))
291 # define RE_STRING_CHAR_AND_LENGTH STRING_CHAR_AND_LENGTH
292 # define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2) \
293 (c = ((p) == (str2) ? *((end1) - 1) : *((p) - 1)))
294 # define MAKE_CHAR(charset, c1, c2) (c1)
295 #endif /* not emacs */
297 #ifndef RE_TRANSLATE
298 # define RE_TRANSLATE(TBL, C) ((unsigned char)(TBL)[C])
299 # define RE_TRANSLATE_P(TBL) (TBL)
300 #endif
302 /* Get the interface, including the syntax bits. */
303 #include "regex.h"
305 /* isalpha etc. are used for the character classes. */
306 #include <ctype.h>
308 #ifdef emacs
310 /* 1 if C is an ASCII character. */
311 # define IS_REAL_ASCII(c) ((c) < 0200)
313 /* 1 if C is a unibyte character. */
314 # define ISUNIBYTE(c) (SINGLE_BYTE_CHAR_P ((c)))
316 /* The Emacs definitions should not be directly affected by locales. */
318 /* In Emacs, these are only used for single-byte characters. */
319 # define ISDIGIT(c) ((c) >= '0' && (c) <= '9')
320 # define ISCNTRL(c) ((c) < ' ')
321 # define ISXDIGIT(c) (((c) >= '0' && (c) <= '9') \
322 || ((c) >= 'a' && (c) <= 'f') \
323 || ((c) >= 'A' && (c) <= 'F'))
325 /* This is only used for single-byte characters. */
326 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
328 /* The rest must handle multibyte characters. */
330 # define ISGRAPH(c) (SINGLE_BYTE_CHAR_P (c) \
331 ? (c) > ' ' && !((c) >= 0177 && (c) <= 0237) \
332 : 1)
334 # define ISPRINT(c) (SINGLE_BYTE_CHAR_P (c) \
335 ? (c) >= ' ' && !((c) >= 0177 && (c) <= 0237) \
336 : 1)
338 # define ISALNUM(c) (IS_REAL_ASCII (c) \
339 ? (((c) >= 'a' && (c) <= 'z') \
340 || ((c) >= 'A' && (c) <= 'Z') \
341 || ((c) >= '0' && (c) <= '9')) \
342 : SYNTAX (c) == Sword)
344 # define ISALPHA(c) (IS_REAL_ASCII (c) \
345 ? (((c) >= 'a' && (c) <= 'z') \
346 || ((c) >= 'A' && (c) <= 'Z')) \
347 : SYNTAX (c) == Sword)
349 # define ISLOWER(c) (LOWERCASEP (c))
351 # define ISPUNCT(c) (IS_REAL_ASCII (c) \
352 ? ((c) > ' ' && (c) < 0177 \
353 && !(((c) >= 'a' && (c) <= 'z') \
354 || ((c) >= 'A' && (c) <= 'Z') \
355 || ((c) >= '0' && (c) <= '9'))) \
356 : SYNTAX (c) != Sword)
358 # define ISSPACE(c) (SYNTAX (c) == Swhitespace)
360 # define ISUPPER(c) (UPPERCASEP (c))
362 # define ISWORD(c) (SYNTAX (c) == Sword)
364 #else /* not emacs */
366 /* Jim Meyering writes:
368 "... Some ctype macros are valid only for character codes that
369 isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
370 using /bin/cc or gcc but without giving an ansi option). So, all
371 ctype uses should be through macros like ISPRINT... If
372 STDC_HEADERS is defined, then autoconf has verified that the ctype
373 macros don't need to be guarded with references to isascii. ...
374 Defining isascii to 1 should let any compiler worth its salt
375 eliminate the && through constant folding."
376 Solaris defines some of these symbols so we must undefine them first. */
378 # undef ISASCII
379 # if defined STDC_HEADERS || (!defined isascii && !defined HAVE_ISASCII)
380 # define ISASCII(c) 1
381 # else
382 # define ISASCII(c) isascii(c)
383 # endif
385 /* 1 if C is an ASCII character. */
386 # define IS_REAL_ASCII(c) ((c) < 0200)
388 /* This distinction is not meaningful, except in Emacs. */
389 # define ISUNIBYTE(c) 1
391 # ifdef isblank
392 # define ISBLANK(c) (ISASCII (c) && isblank (c))
393 # else
394 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
395 # endif
396 # ifdef isgraph
397 # define ISGRAPH(c) (ISASCII (c) && isgraph (c))
398 # else
399 # define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
400 # endif
402 # undef ISPRINT
403 # define ISPRINT(c) (ISASCII (c) && isprint (c))
404 # define ISDIGIT(c) (ISASCII (c) && isdigit (c))
405 # define ISALNUM(c) (ISASCII (c) && isalnum (c))
406 # define ISALPHA(c) (ISASCII (c) && isalpha (c))
407 # define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
408 # define ISLOWER(c) (ISASCII (c) && islower (c))
409 # define ISPUNCT(c) (ISASCII (c) && ispunct (c))
410 # define ISSPACE(c) (ISASCII (c) && isspace (c))
411 # define ISUPPER(c) (ISASCII (c) && isupper (c))
412 # define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
414 # define ISWORD(c) ISALPHA(c)
416 # ifdef _tolower
417 # define TOLOWER(c) _tolower(c)
418 # else
419 # define TOLOWER(c) tolower(c)
420 # endif
422 /* How many characters in the character set. */
423 # define CHAR_SET_SIZE 256
425 # ifdef SYNTAX_TABLE
427 extern char *re_syntax_table;
429 # else /* not SYNTAX_TABLE */
431 static char re_syntax_table[CHAR_SET_SIZE];
433 static void
434 init_syntax_once ()
436 register int c;
437 static int done = 0;
439 if (done)
440 return;
442 bzero (re_syntax_table, sizeof re_syntax_table);
444 for (c = 0; c < CHAR_SET_SIZE; ++c)
445 if (ISALNUM (c))
446 re_syntax_table[c] = Sword;
448 re_syntax_table['_'] = Ssymbol;
450 done = 1;
453 # endif /* not SYNTAX_TABLE */
455 # define SYNTAX(c) re_syntax_table[(c)]
457 #endif /* not emacs */
459 #ifndef NULL
460 # define NULL (void *)0
461 #endif
463 /* We remove any previous definition of `SIGN_EXTEND_CHAR',
464 since ours (we hope) works properly with all combinations of
465 machines, compilers, `char' and `unsigned char' argument types.
466 (Per Bothner suggested the basic approach.) */
467 #undef SIGN_EXTEND_CHAR
468 #if __STDC__
469 # define SIGN_EXTEND_CHAR(c) ((signed char) (c))
470 #else /* not __STDC__ */
471 /* As in Harbison and Steele. */
472 # define SIGN_EXTEND_CHAR(c) ((((unsigned char) (c)) ^ 128) - 128)
473 #endif
475 /* Should we use malloc or alloca? If REGEX_MALLOC is not defined, we
476 use `alloca' instead of `malloc'. This is because using malloc in
477 re_search* or re_match* could cause memory leaks when C-g is used in
478 Emacs; also, malloc is slower and causes storage fragmentation. On
479 the other hand, malloc is more portable, and easier to debug.
481 Because we sometimes use alloca, some routines have to be macros,
482 not functions -- `alloca'-allocated space disappears at the end of the
483 function it is called in. */
485 #ifdef REGEX_MALLOC
487 # define REGEX_ALLOCATE malloc
488 # define REGEX_REALLOCATE(source, osize, nsize) realloc (source, nsize)
489 # define REGEX_FREE free
491 #else /* not REGEX_MALLOC */
493 /* Emacs already defines alloca, sometimes. */
494 # ifndef alloca
496 /* Make alloca work the best possible way. */
497 # ifdef __GNUC__
498 # define alloca __builtin_alloca
499 # else /* not __GNUC__ */
500 # if HAVE_ALLOCA_H
501 # include <alloca.h>
502 # endif /* HAVE_ALLOCA_H */
503 # endif /* not __GNUC__ */
505 # endif /* not alloca */
507 # define REGEX_ALLOCATE alloca
509 /* Assumes a `char *destination' variable. */
510 # define REGEX_REALLOCATE(source, osize, nsize) \
511 (destination = (char *) alloca (nsize), \
512 memcpy (destination, source, osize))
514 /* No need to do anything to free, after alloca. */
515 # define REGEX_FREE(arg) ((void)0) /* Do nothing! But inhibit gcc warning. */
517 #endif /* not REGEX_MALLOC */
519 /* Define how to allocate the failure stack. */
521 #if defined REL_ALLOC && defined REGEX_MALLOC
523 # define REGEX_ALLOCATE_STACK(size) \
524 r_alloc (&failure_stack_ptr, (size))
525 # define REGEX_REALLOCATE_STACK(source, osize, nsize) \
526 r_re_alloc (&failure_stack_ptr, (nsize))
527 # define REGEX_FREE_STACK(ptr) \
528 r_alloc_free (&failure_stack_ptr)
530 #else /* not using relocating allocator */
532 # ifdef REGEX_MALLOC
534 # define REGEX_ALLOCATE_STACK malloc
535 # define REGEX_REALLOCATE_STACK(source, osize, nsize) realloc (source, nsize)
536 # define REGEX_FREE_STACK free
538 # else /* not REGEX_MALLOC */
540 # define REGEX_ALLOCATE_STACK alloca
542 # define REGEX_REALLOCATE_STACK(source, osize, nsize) \
543 REGEX_REALLOCATE (source, osize, nsize)
544 /* No need to explicitly free anything. */
545 # define REGEX_FREE_STACK(arg) ((void)0)
547 # endif /* not REGEX_MALLOC */
548 #endif /* not using relocating allocator */
551 /* True if `size1' is non-NULL and PTR is pointing anywhere inside
552 `string1' or just past its end. This works if PTR is NULL, which is
553 a good thing. */
554 #define FIRST_STRING_P(ptr) \
555 (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
557 /* (Re)Allocate N items of type T using malloc, or fail. */
558 #define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t)))
559 #define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
560 #define RETALLOC_IF(addr, n, t) \
561 if (addr) RETALLOC((addr), (n), t); else (addr) = TALLOC ((n), t)
562 #define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
564 #define BYTEWIDTH 8 /* In bits. */
566 #define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
568 #undef MAX
569 #undef MIN
570 #define MAX(a, b) ((a) > (b) ? (a) : (b))
571 #define MIN(a, b) ((a) < (b) ? (a) : (b))
573 /* Type of source-pattern and string chars. */
574 typedef const unsigned char re_char;
576 typedef char boolean;
577 #define false 0
578 #define true 1
580 static int re_match_2_internal _RE_ARGS ((struct re_pattern_buffer *bufp,
581 re_char *string1, int size1,
582 re_char *string2, int size2,
583 int pos,
584 struct re_registers *regs,
585 int stop));
587 /* These are the command codes that appear in compiled regular
588 expressions. Some opcodes are followed by argument bytes. A
589 command code can specify any interpretation whatsoever for its
590 arguments. Zero bytes may appear in the compiled regular expression. */
592 typedef enum
594 no_op = 0,
596 /* Succeed right away--no more backtracking. */
597 succeed,
599 /* Followed by one byte giving n, then by n literal bytes. */
600 exactn,
602 /* Matches any (more or less) character. */
603 anychar,
605 /* Matches any one char belonging to specified set. First
606 following byte is number of bitmap bytes. Then come bytes
607 for a bitmap saying which chars are in. Bits in each byte
608 are ordered low-bit-first. A character is in the set if its
609 bit is 1. A character too large to have a bit in the map is
610 automatically not in the set.
612 If the length byte has the 0x80 bit set, then that stuff
613 is followed by a range table:
614 2 bytes of flags for character sets (low 8 bits, high 8 bits)
615 See RANGE_TABLE_WORK_BITS below.
616 2 bytes, the number of pairs that follow (upto 32767)
617 pairs, each 2 multibyte characters,
618 each multibyte character represented as 3 bytes. */
619 charset,
621 /* Same parameters as charset, but match any character that is
622 not one of those specified. */
623 charset_not,
625 /* Start remembering the text that is matched, for storing in a
626 register. Followed by one byte with the register number, in
627 the range 0 to one less than the pattern buffer's re_nsub
628 field. */
629 start_memory,
631 /* Stop remembering the text that is matched and store it in a
632 memory register. Followed by one byte with the register
633 number, in the range 0 to one less than `re_nsub' in the
634 pattern buffer. */
635 stop_memory,
637 /* Match a duplicate of something remembered. Followed by one
638 byte containing the register number. */
639 duplicate,
641 /* Fail unless at beginning of line. */
642 begline,
644 /* Fail unless at end of line. */
645 endline,
647 /* Succeeds if at beginning of buffer (if emacs) or at beginning
648 of string to be matched (if not). */
649 begbuf,
651 /* Analogously, for end of buffer/string. */
652 endbuf,
654 /* Followed by two byte relative address to which to jump. */
655 jump,
657 /* Followed by two-byte relative address of place to resume at
658 in case of failure. */
659 on_failure_jump,
661 /* Like on_failure_jump, but pushes a placeholder instead of the
662 current string position when executed. */
663 on_failure_keep_string_jump,
665 /* Just like `on_failure_jump', except that it checks that we
666 don't get stuck in an infinite loop (matching an empty string
667 indefinitely). */
668 on_failure_jump_loop,
670 /* Just like `on_failure_jump_loop', except that it checks for
671 a different kind of loop (the kind that shows up with non-greedy
672 operators). This operation has to be immediately preceded
673 by a `no_op'. */
674 on_failure_jump_nastyloop,
676 /* A smart `on_failure_jump' used for greedy * and + operators.
677 It analyses the loop before which it is put and if the
678 loop does not require backtracking, it changes itself to
679 `on_failure_keep_string_jump' and short-circuits the loop,
680 else it just defaults to changing itself into `on_failure_jump'.
681 It assumes that it is pointing to just past a `jump'. */
682 on_failure_jump_smart,
684 /* Followed by two-byte relative address and two-byte number n.
685 After matching N times, jump to the address upon failure.
686 Does not work if N starts at 0: use on_failure_jump_loop
687 instead. */
688 succeed_n,
690 /* Followed by two-byte relative address, and two-byte number n.
691 Jump to the address N times, then fail. */
692 jump_n,
694 /* Set the following two-byte relative address to the
695 subsequent two-byte number. The address *includes* the two
696 bytes of number. */
697 set_number_at,
699 wordbeg, /* Succeeds if at word beginning. */
700 wordend, /* Succeeds if at word end. */
702 wordbound, /* Succeeds if at a word boundary. */
703 notwordbound, /* Succeeds if not at a word boundary. */
705 symbeg, /* Succeeds if at symbol beginning. */
706 symend, /* Succeeds if at symbol end. */
708 /* Matches any character whose syntax is specified. Followed by
709 a byte which contains a syntax code, e.g., Sword. */
710 syntaxspec,
712 /* Matches any character whose syntax is not that specified. */
713 notsyntaxspec
715 #ifdef emacs
716 ,before_dot, /* Succeeds if before point. */
717 at_dot, /* Succeeds if at point. */
718 after_dot, /* Succeeds if after point. */
720 /* Matches any character whose category-set contains the specified
721 category. The operator is followed by a byte which contains a
722 category code (mnemonic ASCII character). */
723 categoryspec,
725 /* Matches any character whose category-set does not contain the
726 specified category. The operator is followed by a byte which
727 contains the category code (mnemonic ASCII character). */
728 notcategoryspec
729 #endif /* emacs */
730 } re_opcode_t;
732 /* Common operations on the compiled pattern. */
734 /* Store NUMBER in two contiguous bytes starting at DESTINATION. */
736 #define STORE_NUMBER(destination, number) \
737 do { \
738 (destination)[0] = (number) & 0377; \
739 (destination)[1] = (number) >> 8; \
740 } while (0)
742 /* Same as STORE_NUMBER, except increment DESTINATION to
743 the byte after where the number is stored. Therefore, DESTINATION
744 must be an lvalue. */
746 #define STORE_NUMBER_AND_INCR(destination, number) \
747 do { \
748 STORE_NUMBER (destination, number); \
749 (destination) += 2; \
750 } while (0)
752 /* Put into DESTINATION a number stored in two contiguous bytes starting
753 at SOURCE. */
755 #define EXTRACT_NUMBER(destination, source) \
756 do { \
757 (destination) = *(source) & 0377; \
758 (destination) += SIGN_EXTEND_CHAR (*((source) + 1)) << 8; \
759 } while (0)
761 #ifdef DEBUG
762 static void extract_number _RE_ARGS ((int *dest, re_char *source));
763 static void
764 extract_number (dest, source)
765 int *dest;
766 re_char *source;
768 int temp = SIGN_EXTEND_CHAR (*(source + 1));
769 *dest = *source & 0377;
770 *dest += temp << 8;
773 # ifndef EXTRACT_MACROS /* To debug the macros. */
774 # undef EXTRACT_NUMBER
775 # define EXTRACT_NUMBER(dest, src) extract_number (&dest, src)
776 # endif /* not EXTRACT_MACROS */
778 #endif /* DEBUG */
780 /* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
781 SOURCE must be an lvalue. */
783 #define EXTRACT_NUMBER_AND_INCR(destination, source) \
784 do { \
785 EXTRACT_NUMBER (destination, source); \
786 (source) += 2; \
787 } while (0)
789 #ifdef DEBUG
790 static void extract_number_and_incr _RE_ARGS ((int *destination,
791 re_char **source));
792 static void
793 extract_number_and_incr (destination, source)
794 int *destination;
795 re_char **source;
797 extract_number (destination, *source);
798 *source += 2;
801 # ifndef EXTRACT_MACROS
802 # undef EXTRACT_NUMBER_AND_INCR
803 # define EXTRACT_NUMBER_AND_INCR(dest, src) \
804 extract_number_and_incr (&dest, &src)
805 # endif /* not EXTRACT_MACROS */
807 #endif /* DEBUG */
809 /* Store a multibyte character in three contiguous bytes starting
810 DESTINATION, and increment DESTINATION to the byte after where the
811 character is stored. Therefore, DESTINATION must be an lvalue. */
813 #define STORE_CHARACTER_AND_INCR(destination, character) \
814 do { \
815 (destination)[0] = (character) & 0377; \
816 (destination)[1] = ((character) >> 8) & 0377; \
817 (destination)[2] = (character) >> 16; \
818 (destination) += 3; \
819 } while (0)
821 /* Put into DESTINATION a character stored in three contiguous bytes
822 starting at SOURCE. */
824 #define EXTRACT_CHARACTER(destination, source) \
825 do { \
826 (destination) = ((source)[0] \
827 | ((source)[1] << 8) \
828 | ((source)[2] << 16)); \
829 } while (0)
832 /* Macros for charset. */
834 /* Size of bitmap of charset P in bytes. P is a start of charset,
835 i.e. *P is (re_opcode_t) charset or (re_opcode_t) charset_not. */
836 #define CHARSET_BITMAP_SIZE(p) ((p)[1] & 0x7F)
838 /* Nonzero if charset P has range table. */
839 #define CHARSET_RANGE_TABLE_EXISTS_P(p) ((p)[1] & 0x80)
841 /* Return the address of range table of charset P. But not the start
842 of table itself, but the before where the number of ranges is
843 stored. `2 +' means to skip re_opcode_t and size of bitmap,
844 and the 2 bytes of flags at the start of the range table. */
845 #define CHARSET_RANGE_TABLE(p) (&(p)[4 + CHARSET_BITMAP_SIZE (p)])
847 /* Extract the bit flags that start a range table. */
848 #define CHARSET_RANGE_TABLE_BITS(p) \
849 ((p)[2 + CHARSET_BITMAP_SIZE (p)] \
850 + (p)[3 + CHARSET_BITMAP_SIZE (p)] * 0x100)
852 /* Test if C is listed in the bitmap of charset P. */
853 #define CHARSET_LOOKUP_BITMAP(p, c) \
854 ((c) < CHARSET_BITMAP_SIZE (p) * BYTEWIDTH \
855 && (p)[2 + (c) / BYTEWIDTH] & (1 << ((c) % BYTEWIDTH)))
857 /* Return the address of end of RANGE_TABLE. COUNT is number of
858 ranges (which is a pair of (start, end)) in the RANGE_TABLE. `* 2'
859 is start of range and end of range. `* 3' is size of each start
860 and end. */
861 #define CHARSET_RANGE_TABLE_END(range_table, count) \
862 ((range_table) + (count) * 2 * 3)
864 /* Test if C is in RANGE_TABLE. A flag NOT is negated if C is in.
865 COUNT is number of ranges in RANGE_TABLE. */
866 #define CHARSET_LOOKUP_RANGE_TABLE_RAW(not, c, range_table, count) \
867 do \
869 re_wchar_t range_start, range_end; \
870 re_char *p; \
871 re_char *range_table_end \
872 = CHARSET_RANGE_TABLE_END ((range_table), (count)); \
874 for (p = (range_table); p < range_table_end; p += 2 * 3) \
876 EXTRACT_CHARACTER (range_start, p); \
877 EXTRACT_CHARACTER (range_end, p + 3); \
879 if (range_start <= (c) && (c) <= range_end) \
881 (not) = !(not); \
882 break; \
886 while (0)
888 /* Test if C is in range table of CHARSET. The flag NOT is negated if
889 C is listed in it. */
890 #define CHARSET_LOOKUP_RANGE_TABLE(not, c, charset) \
891 do \
893 /* Number of ranges in range table. */ \
894 int count; \
895 re_char *range_table = CHARSET_RANGE_TABLE (charset); \
897 EXTRACT_NUMBER_AND_INCR (count, range_table); \
898 CHARSET_LOOKUP_RANGE_TABLE_RAW ((not), (c), range_table, count); \
900 while (0)
902 /* If DEBUG is defined, Regex prints many voluminous messages about what
903 it is doing (if the variable `debug' is nonzero). If linked with the
904 main program in `iregex.c', you can enter patterns and strings
905 interactively. And if linked with the main program in `main.c' and
906 the other test files, you can run the already-written tests. */
908 #ifdef DEBUG
910 /* We use standard I/O for debugging. */
911 # include <stdio.h>
913 /* It is useful to test things that ``must'' be true when debugging. */
914 # include <assert.h>
916 static int debug = -100000;
918 # define DEBUG_STATEMENT(e) e
919 # define DEBUG_PRINT1(x) if (debug > 0) printf (x)
920 # define DEBUG_PRINT2(x1, x2) if (debug > 0) printf (x1, x2)
921 # define DEBUG_PRINT3(x1, x2, x3) if (debug > 0) printf (x1, x2, x3)
922 # define DEBUG_PRINT4(x1, x2, x3, x4) if (debug > 0) printf (x1, x2, x3, x4)
923 # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) \
924 if (debug > 0) print_partial_compiled_pattern (s, e)
925 # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \
926 if (debug > 0) print_double_string (w, s1, sz1, s2, sz2)
929 /* Print the fastmap in human-readable form. */
931 void
932 print_fastmap (fastmap)
933 char *fastmap;
935 unsigned was_a_range = 0;
936 unsigned i = 0;
938 while (i < (1 << BYTEWIDTH))
940 if (fastmap[i++])
942 was_a_range = 0;
943 putchar (i - 1);
944 while (i < (1 << BYTEWIDTH) && fastmap[i])
946 was_a_range = 1;
947 i++;
949 if (was_a_range)
951 printf ("-");
952 putchar (i - 1);
956 putchar ('\n');
960 /* Print a compiled pattern string in human-readable form, starting at
961 the START pointer into it and ending just before the pointer END. */
963 void
964 print_partial_compiled_pattern (start, end)
965 re_char *start;
966 re_char *end;
968 int mcnt, mcnt2;
969 re_char *p = start;
970 re_char *pend = end;
972 if (start == NULL)
974 fprintf (stderr, "(null)\n");
975 return;
978 /* Loop over pattern commands. */
979 while (p < pend)
981 fprintf (stderr, "%d:\t", p - start);
983 switch ((re_opcode_t) *p++)
985 case no_op:
986 fprintf (stderr, "/no_op");
987 break;
989 case succeed:
990 fprintf (stderr, "/succeed");
991 break;
993 case exactn:
994 mcnt = *p++;
995 fprintf (stderr, "/exactn/%d", mcnt);
998 fprintf (stderr, "/%c", *p++);
1000 while (--mcnt);
1001 break;
1003 case start_memory:
1004 fprintf (stderr, "/start_memory/%d", *p++);
1005 break;
1007 case stop_memory:
1008 fprintf (stderr, "/stop_memory/%d", *p++);
1009 break;
1011 case duplicate:
1012 fprintf (stderr, "/duplicate/%d", *p++);
1013 break;
1015 case anychar:
1016 fprintf (stderr, "/anychar");
1017 break;
1019 case charset:
1020 case charset_not:
1022 register int c, last = -100;
1023 register int in_range = 0;
1024 int length = CHARSET_BITMAP_SIZE (p - 1);
1025 int has_range_table = CHARSET_RANGE_TABLE_EXISTS_P (p - 1);
1027 fprintf (stderr, "/charset [%s",
1028 (re_opcode_t) *(p - 1) == charset_not ? "^" : "");
1030 if (p + *p >= pend)
1031 fprintf (stderr, " !extends past end of pattern! ");
1033 for (c = 0; c < 256; c++)
1034 if (c / 8 < length
1035 && (p[1 + (c/8)] & (1 << (c % 8))))
1037 /* Are we starting a range? */
1038 if (last + 1 == c && ! in_range)
1040 fprintf (stderr, "-");
1041 in_range = 1;
1043 /* Have we broken a range? */
1044 else if (last + 1 != c && in_range)
1046 fprintf (stderr, "%c", last);
1047 in_range = 0;
1050 if (! in_range)
1051 fprintf (stderr, "%c", c);
1053 last = c;
1056 if (in_range)
1057 fprintf (stderr, "%c", last);
1059 fprintf (stderr, "]");
1061 p += 1 + length;
1063 if (has_range_table)
1065 int count;
1066 fprintf (stderr, "has-range-table");
1068 /* ??? Should print the range table; for now, just skip it. */
1069 p += 2; /* skip range table bits */
1070 EXTRACT_NUMBER_AND_INCR (count, p);
1071 p = CHARSET_RANGE_TABLE_END (p, count);
1074 break;
1076 case begline:
1077 fprintf (stderr, "/begline");
1078 break;
1080 case endline:
1081 fprintf (stderr, "/endline");
1082 break;
1084 case on_failure_jump:
1085 extract_number_and_incr (&mcnt, &p);
1086 fprintf (stderr, "/on_failure_jump to %d", p + mcnt - start);
1087 break;
1089 case on_failure_keep_string_jump:
1090 extract_number_and_incr (&mcnt, &p);
1091 fprintf (stderr, "/on_failure_keep_string_jump to %d", p + mcnt - start);
1092 break;
1094 case on_failure_jump_nastyloop:
1095 extract_number_and_incr (&mcnt, &p);
1096 fprintf (stderr, "/on_failure_jump_nastyloop to %d", p + mcnt - start);
1097 break;
1099 case on_failure_jump_loop:
1100 extract_number_and_incr (&mcnt, &p);
1101 fprintf (stderr, "/on_failure_jump_loop to %d", p + mcnt - start);
1102 break;
1104 case on_failure_jump_smart:
1105 extract_number_and_incr (&mcnt, &p);
1106 fprintf (stderr, "/on_failure_jump_smart to %d", p + mcnt - start);
1107 break;
1109 case jump:
1110 extract_number_and_incr (&mcnt, &p);
1111 fprintf (stderr, "/jump to %d", p + mcnt - start);
1112 break;
1114 case succeed_n:
1115 extract_number_and_incr (&mcnt, &p);
1116 extract_number_and_incr (&mcnt2, &p);
1117 fprintf (stderr, "/succeed_n to %d, %d times", p - 2 + mcnt - start, mcnt2);
1118 break;
1120 case jump_n:
1121 extract_number_and_incr (&mcnt, &p);
1122 extract_number_and_incr (&mcnt2, &p);
1123 fprintf (stderr, "/jump_n to %d, %d times", p - 2 + mcnt - start, mcnt2);
1124 break;
1126 case set_number_at:
1127 extract_number_and_incr (&mcnt, &p);
1128 extract_number_and_incr (&mcnt2, &p);
1129 fprintf (stderr, "/set_number_at location %d to %d", p - 2 + mcnt - start, mcnt2);
1130 break;
1132 case wordbound:
1133 fprintf (stderr, "/wordbound");
1134 break;
1136 case notwordbound:
1137 fprintf (stderr, "/notwordbound");
1138 break;
1140 case wordbeg:
1141 fprintf (stderr, "/wordbeg");
1142 break;
1144 case wordend:
1145 fprintf (stderr, "/wordend");
1146 break;
1148 case symbeg:
1149 fprintf (stderr, "/symbeg");
1150 break;
1152 case symend:
1153 fprintf (stderr, "/symend");
1154 break;
1156 case syntaxspec:
1157 fprintf (stderr, "/syntaxspec");
1158 mcnt = *p++;
1159 fprintf (stderr, "/%d", mcnt);
1160 break;
1162 case notsyntaxspec:
1163 fprintf (stderr, "/notsyntaxspec");
1164 mcnt = *p++;
1165 fprintf (stderr, "/%d", mcnt);
1166 break;
1168 # ifdef emacs
1169 case before_dot:
1170 fprintf (stderr, "/before_dot");
1171 break;
1173 case at_dot:
1174 fprintf (stderr, "/at_dot");
1175 break;
1177 case after_dot:
1178 fprintf (stderr, "/after_dot");
1179 break;
1181 case categoryspec:
1182 fprintf (stderr, "/categoryspec");
1183 mcnt = *p++;
1184 fprintf (stderr, "/%d", mcnt);
1185 break;
1187 case notcategoryspec:
1188 fprintf (stderr, "/notcategoryspec");
1189 mcnt = *p++;
1190 fprintf (stderr, "/%d", mcnt);
1191 break;
1192 # endif /* emacs */
1194 case begbuf:
1195 fprintf (stderr, "/begbuf");
1196 break;
1198 case endbuf:
1199 fprintf (stderr, "/endbuf");
1200 break;
1202 default:
1203 fprintf (stderr, "?%d", *(p-1));
1206 fprintf (stderr, "\n");
1209 fprintf (stderr, "%d:\tend of pattern.\n", p - start);
1213 void
1214 print_compiled_pattern (bufp)
1215 struct re_pattern_buffer *bufp;
1217 re_char *buffer = bufp->buffer;
1219 print_partial_compiled_pattern (buffer, buffer + bufp->used);
1220 printf ("%ld bytes used/%ld bytes allocated.\n",
1221 bufp->used, bufp->allocated);
1223 if (bufp->fastmap_accurate && bufp->fastmap)
1225 printf ("fastmap: ");
1226 print_fastmap (bufp->fastmap);
1229 printf ("re_nsub: %d\t", bufp->re_nsub);
1230 printf ("regs_alloc: %d\t", bufp->regs_allocated);
1231 printf ("can_be_null: %d\t", bufp->can_be_null);
1232 printf ("no_sub: %d\t", bufp->no_sub);
1233 printf ("not_bol: %d\t", bufp->not_bol);
1234 printf ("not_eol: %d\t", bufp->not_eol);
1235 printf ("syntax: %lx\n", bufp->syntax);
1236 fflush (stdout);
1237 /* Perhaps we should print the translate table? */
1241 void
1242 print_double_string (where, string1, size1, string2, size2)
1243 re_char *where;
1244 re_char *string1;
1245 re_char *string2;
1246 int size1;
1247 int size2;
1249 int this_char;
1251 if (where == NULL)
1252 printf ("(null)");
1253 else
1255 if (FIRST_STRING_P (where))
1257 for (this_char = where - string1; this_char < size1; this_char++)
1258 putchar (string1[this_char]);
1260 where = string2;
1263 for (this_char = where - string2; this_char < size2; this_char++)
1264 putchar (string2[this_char]);
1268 #else /* not DEBUG */
1270 # undef assert
1271 # define assert(e)
1273 # define DEBUG_STATEMENT(e)
1274 # define DEBUG_PRINT1(x)
1275 # define DEBUG_PRINT2(x1, x2)
1276 # define DEBUG_PRINT3(x1, x2, x3)
1277 # define DEBUG_PRINT4(x1, x2, x3, x4)
1278 # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
1279 # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
1281 #endif /* not DEBUG */
1283 /* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
1284 also be assigned to arbitrarily: each pattern buffer stores its own
1285 syntax, so it can be changed between regex compilations. */
1286 /* This has no initializer because initialized variables in Emacs
1287 become read-only after dumping. */
1288 reg_syntax_t re_syntax_options;
1291 /* Specify the precise syntax of regexps for compilation. This provides
1292 for compatibility for various utilities which historically have
1293 different, incompatible syntaxes.
1295 The argument SYNTAX is a bit mask comprised of the various bits
1296 defined in regex.h. We return the old syntax. */
1298 reg_syntax_t
1299 re_set_syntax (syntax)
1300 reg_syntax_t syntax;
1302 reg_syntax_t ret = re_syntax_options;
1304 re_syntax_options = syntax;
1305 return ret;
1307 WEAK_ALIAS (__re_set_syntax, re_set_syntax)
1309 /* Regexp to use to replace spaces, or NULL meaning don't. */
1310 static re_char *whitespace_regexp;
1312 void
1313 re_set_whitespace_regexp (regexp)
1314 const char *regexp;
1316 whitespace_regexp = (re_char *) regexp;
1318 WEAK_ALIAS (__re_set_syntax, re_set_syntax)
1320 /* This table gives an error message for each of the error codes listed
1321 in regex.h. Obviously the order here has to be same as there.
1322 POSIX doesn't require that we do anything for REG_NOERROR,
1323 but why not be nice? */
1325 static const char *re_error_msgid[] =
1327 gettext_noop ("Success"), /* REG_NOERROR */
1328 gettext_noop ("No match"), /* REG_NOMATCH */
1329 gettext_noop ("Invalid regular expression"), /* REG_BADPAT */
1330 gettext_noop ("Invalid collation character"), /* REG_ECOLLATE */
1331 gettext_noop ("Invalid character class name"), /* REG_ECTYPE */
1332 gettext_noop ("Trailing backslash"), /* REG_EESCAPE */
1333 gettext_noop ("Invalid back reference"), /* REG_ESUBREG */
1334 gettext_noop ("Unmatched [ or [^"), /* REG_EBRACK */
1335 gettext_noop ("Unmatched ( or \\("), /* REG_EPAREN */
1336 gettext_noop ("Unmatched \\{"), /* REG_EBRACE */
1337 gettext_noop ("Invalid content of \\{\\}"), /* REG_BADBR */
1338 gettext_noop ("Invalid range end"), /* REG_ERANGE */
1339 gettext_noop ("Memory exhausted"), /* REG_ESPACE */
1340 gettext_noop ("Invalid preceding regular expression"), /* REG_BADRPT */
1341 gettext_noop ("Premature end of regular expression"), /* REG_EEND */
1342 gettext_noop ("Regular expression too big"), /* REG_ESIZE */
1343 gettext_noop ("Unmatched ) or \\)"), /* REG_ERPAREN */
1344 gettext_noop ("Range striding over charsets") /* REG_ERANGEX */
1347 /* Avoiding alloca during matching, to placate r_alloc. */
1349 /* Define MATCH_MAY_ALLOCATE unless we need to make sure that the
1350 searching and matching functions should not call alloca. On some
1351 systems, alloca is implemented in terms of malloc, and if we're
1352 using the relocating allocator routines, then malloc could cause a
1353 relocation, which might (if the strings being searched are in the
1354 ralloc heap) shift the data out from underneath the regexp
1355 routines.
1357 Here's another reason to avoid allocation: Emacs
1358 processes input from X in a signal handler; processing X input may
1359 call malloc; if input arrives while a matching routine is calling
1360 malloc, then we're scrod. But Emacs can't just block input while
1361 calling matching routines; then we don't notice interrupts when
1362 they come in. So, Emacs blocks input around all regexp calls
1363 except the matching calls, which it leaves unprotected, in the
1364 faith that they will not malloc. */
1366 /* Normally, this is fine. */
1367 #define MATCH_MAY_ALLOCATE
1369 /* When using GNU C, we are not REALLY using the C alloca, no matter
1370 what config.h may say. So don't take precautions for it. */
1371 #ifdef __GNUC__
1372 # undef C_ALLOCA
1373 #endif
1375 /* The match routines may not allocate if (1) they would do it with malloc
1376 and (2) it's not safe for them to use malloc.
1377 Note that if REL_ALLOC is defined, matching would not use malloc for the
1378 failure stack, but we would still use it for the register vectors;
1379 so REL_ALLOC should not affect this. */
1380 #if (defined C_ALLOCA || defined REGEX_MALLOC) && defined emacs
1381 # undef MATCH_MAY_ALLOCATE
1382 #endif
1385 /* Failure stack declarations and macros; both re_compile_fastmap and
1386 re_match_2 use a failure stack. These have to be macros because of
1387 REGEX_ALLOCATE_STACK. */
1390 /* Approximate number of failure points for which to initially allocate space
1391 when matching. If this number is exceeded, we allocate more
1392 space, so it is not a hard limit. */
1393 #ifndef INIT_FAILURE_ALLOC
1394 # define INIT_FAILURE_ALLOC 20
1395 #endif
1397 /* Roughly the maximum number of failure points on the stack. Would be
1398 exactly that if always used TYPICAL_FAILURE_SIZE items each time we failed.
1399 This is a variable only so users of regex can assign to it; we never
1400 change it ourselves. We always multiply it by TYPICAL_FAILURE_SIZE
1401 before using it, so it should probably be a byte-count instead. */
1402 # if defined MATCH_MAY_ALLOCATE
1403 /* Note that 4400 was enough to cause a crash on Alpha OSF/1,
1404 whose default stack limit is 2mb. In order for a larger
1405 value to work reliably, you have to try to make it accord
1406 with the process stack limit. */
1407 size_t re_max_failures = 40000;
1408 # else
1409 size_t re_max_failures = 4000;
1410 # endif
1412 union fail_stack_elt
1414 re_char *pointer;
1415 /* This should be the biggest `int' that's no bigger than a pointer. */
1416 long integer;
1419 typedef union fail_stack_elt fail_stack_elt_t;
1421 typedef struct
1423 fail_stack_elt_t *stack;
1424 size_t size;
1425 size_t avail; /* Offset of next open position. */
1426 size_t frame; /* Offset of the cur constructed frame. */
1427 } fail_stack_type;
1429 #define FAIL_STACK_EMPTY() (fail_stack.frame == 0)
1430 #define FAIL_STACK_FULL() (fail_stack.avail == fail_stack.size)
1433 /* Define macros to initialize and free the failure stack.
1434 Do `return -2' if the alloc fails. */
1436 #ifdef MATCH_MAY_ALLOCATE
1437 # define INIT_FAIL_STACK() \
1438 do { \
1439 fail_stack.stack = (fail_stack_elt_t *) \
1440 REGEX_ALLOCATE_STACK (INIT_FAILURE_ALLOC * TYPICAL_FAILURE_SIZE \
1441 * sizeof (fail_stack_elt_t)); \
1443 if (fail_stack.stack == NULL) \
1444 return -2; \
1446 fail_stack.size = INIT_FAILURE_ALLOC; \
1447 fail_stack.avail = 0; \
1448 fail_stack.frame = 0; \
1449 } while (0)
1451 # define RESET_FAIL_STACK() REGEX_FREE_STACK (fail_stack.stack)
1452 #else
1453 # define INIT_FAIL_STACK() \
1454 do { \
1455 fail_stack.avail = 0; \
1456 fail_stack.frame = 0; \
1457 } while (0)
1459 # define RESET_FAIL_STACK() ((void)0)
1460 #endif
1463 /* Double the size of FAIL_STACK, up to a limit
1464 which allows approximately `re_max_failures' items.
1466 Return 1 if succeeds, and 0 if either ran out of memory
1467 allocating space for it or it was already too large.
1469 REGEX_REALLOCATE_STACK requires `destination' be declared. */
1471 /* Factor to increase the failure stack size by
1472 when we increase it.
1473 This used to be 2, but 2 was too wasteful
1474 because the old discarded stacks added up to as much space
1475 were as ultimate, maximum-size stack. */
1476 #define FAIL_STACK_GROWTH_FACTOR 4
1478 #define GROW_FAIL_STACK(fail_stack) \
1479 (((fail_stack).size * sizeof (fail_stack_elt_t) \
1480 >= re_max_failures * TYPICAL_FAILURE_SIZE) \
1481 ? 0 \
1482 : ((fail_stack).stack \
1483 = (fail_stack_elt_t *) \
1484 REGEX_REALLOCATE_STACK ((fail_stack).stack, \
1485 (fail_stack).size * sizeof (fail_stack_elt_t), \
1486 MIN (re_max_failures * TYPICAL_FAILURE_SIZE, \
1487 ((fail_stack).size * sizeof (fail_stack_elt_t) \
1488 * FAIL_STACK_GROWTH_FACTOR))), \
1490 (fail_stack).stack == NULL \
1491 ? 0 \
1492 : ((fail_stack).size \
1493 = (MIN (re_max_failures * TYPICAL_FAILURE_SIZE, \
1494 ((fail_stack).size * sizeof (fail_stack_elt_t) \
1495 * FAIL_STACK_GROWTH_FACTOR)) \
1496 / sizeof (fail_stack_elt_t)), \
1497 1)))
1500 /* Push a pointer value onto the failure stack.
1501 Assumes the variable `fail_stack'. Probably should only
1502 be called from within `PUSH_FAILURE_POINT'. */
1503 #define PUSH_FAILURE_POINTER(item) \
1504 fail_stack.stack[fail_stack.avail++].pointer = (item)
1506 /* This pushes an integer-valued item onto the failure stack.
1507 Assumes the variable `fail_stack'. Probably should only
1508 be called from within `PUSH_FAILURE_POINT'. */
1509 #define PUSH_FAILURE_INT(item) \
1510 fail_stack.stack[fail_stack.avail++].integer = (item)
1512 /* Push a fail_stack_elt_t value onto the failure stack.
1513 Assumes the variable `fail_stack'. Probably should only
1514 be called from within `PUSH_FAILURE_POINT'. */
1515 #define PUSH_FAILURE_ELT(item) \
1516 fail_stack.stack[fail_stack.avail++] = (item)
1518 /* These three POP... operations complement the three PUSH... operations.
1519 All assume that `fail_stack' is nonempty. */
1520 #define POP_FAILURE_POINTER() fail_stack.stack[--fail_stack.avail].pointer
1521 #define POP_FAILURE_INT() fail_stack.stack[--fail_stack.avail].integer
1522 #define POP_FAILURE_ELT() fail_stack.stack[--fail_stack.avail]
1524 /* Individual items aside from the registers. */
1525 #define NUM_NONREG_ITEMS 3
1527 /* Used to examine the stack (to detect infinite loops). */
1528 #define FAILURE_PAT(h) fail_stack.stack[(h) - 1].pointer
1529 #define FAILURE_STR(h) (fail_stack.stack[(h) - 2].pointer)
1530 #define NEXT_FAILURE_HANDLE(h) fail_stack.stack[(h) - 3].integer
1531 #define TOP_FAILURE_HANDLE() fail_stack.frame
1534 #define ENSURE_FAIL_STACK(space) \
1535 while (REMAINING_AVAIL_SLOTS <= space) { \
1536 if (!GROW_FAIL_STACK (fail_stack)) \
1537 return -2; \
1538 DEBUG_PRINT2 ("\n Doubled stack; size now: %d\n", (fail_stack).size);\
1539 DEBUG_PRINT2 (" slots available: %d\n", REMAINING_AVAIL_SLOTS);\
1542 /* Push register NUM onto the stack. */
1543 #define PUSH_FAILURE_REG(num) \
1544 do { \
1545 char *destination; \
1546 ENSURE_FAIL_STACK(3); \
1547 DEBUG_PRINT4 (" Push reg %d (spanning %p -> %p)\n", \
1548 num, regstart[num], regend[num]); \
1549 PUSH_FAILURE_POINTER (regstart[num]); \
1550 PUSH_FAILURE_POINTER (regend[num]); \
1551 PUSH_FAILURE_INT (num); \
1552 } while (0)
1554 /* Change the counter's value to VAL, but make sure that it will
1555 be reset when backtracking. */
1556 #define PUSH_NUMBER(ptr,val) \
1557 do { \
1558 char *destination; \
1559 int c; \
1560 ENSURE_FAIL_STACK(3); \
1561 EXTRACT_NUMBER (c, ptr); \
1562 DEBUG_PRINT4 (" Push number %p = %d -> %d\n", ptr, c, val); \
1563 PUSH_FAILURE_INT (c); \
1564 PUSH_FAILURE_POINTER (ptr); \
1565 PUSH_FAILURE_INT (-1); \
1566 STORE_NUMBER (ptr, val); \
1567 } while (0)
1569 /* Pop a saved register off the stack. */
1570 #define POP_FAILURE_REG_OR_COUNT() \
1571 do { \
1572 int reg = POP_FAILURE_INT (); \
1573 if (reg == -1) \
1575 /* It's a counter. */ \
1576 /* Here, we discard `const', making re_match non-reentrant. */ \
1577 unsigned char *ptr = (unsigned char*) POP_FAILURE_POINTER (); \
1578 reg = POP_FAILURE_INT (); \
1579 STORE_NUMBER (ptr, reg); \
1580 DEBUG_PRINT3 (" Pop counter %p = %d\n", ptr, reg); \
1582 else \
1584 regend[reg] = POP_FAILURE_POINTER (); \
1585 regstart[reg] = POP_FAILURE_POINTER (); \
1586 DEBUG_PRINT4 (" Pop reg %d (spanning %p -> %p)\n", \
1587 reg, regstart[reg], regend[reg]); \
1589 } while (0)
1591 /* Check that we are not stuck in an infinite loop. */
1592 #define CHECK_INFINITE_LOOP(pat_cur, string_place) \
1593 do { \
1594 int failure = TOP_FAILURE_HANDLE (); \
1595 /* Check for infinite matching loops */ \
1596 while (failure > 0 \
1597 && (FAILURE_STR (failure) == string_place \
1598 || FAILURE_STR (failure) == NULL)) \
1600 assert (FAILURE_PAT (failure) >= bufp->buffer \
1601 && FAILURE_PAT (failure) <= bufp->buffer + bufp->used); \
1602 if (FAILURE_PAT (failure) == pat_cur) \
1604 cycle = 1; \
1605 break; \
1607 DEBUG_PRINT2 (" Other pattern: %p\n", FAILURE_PAT (failure)); \
1608 failure = NEXT_FAILURE_HANDLE(failure); \
1610 DEBUG_PRINT2 (" Other string: %p\n", FAILURE_STR (failure)); \
1611 } while (0)
1613 /* Push the information about the state we will need
1614 if we ever fail back to it.
1616 Requires variables fail_stack, regstart, regend and
1617 num_regs be declared. GROW_FAIL_STACK requires `destination' be
1618 declared.
1620 Does `return FAILURE_CODE' if runs out of memory. */
1622 #define PUSH_FAILURE_POINT(pattern, string_place) \
1623 do { \
1624 char *destination; \
1625 /* Must be int, so when we don't save any registers, the arithmetic \
1626 of 0 + -1 isn't done as unsigned. */ \
1628 DEBUG_STATEMENT (nfailure_points_pushed++); \
1629 DEBUG_PRINT1 ("\nPUSH_FAILURE_POINT:\n"); \
1630 DEBUG_PRINT2 (" Before push, next avail: %d\n", (fail_stack).avail); \
1631 DEBUG_PRINT2 (" size: %d\n", (fail_stack).size);\
1633 ENSURE_FAIL_STACK (NUM_NONREG_ITEMS); \
1635 DEBUG_PRINT1 ("\n"); \
1637 DEBUG_PRINT2 (" Push frame index: %d\n", fail_stack.frame); \
1638 PUSH_FAILURE_INT (fail_stack.frame); \
1640 DEBUG_PRINT2 (" Push string %p: `", string_place); \
1641 DEBUG_PRINT_DOUBLE_STRING (string_place, string1, size1, string2, size2);\
1642 DEBUG_PRINT1 ("'\n"); \
1643 PUSH_FAILURE_POINTER (string_place); \
1645 DEBUG_PRINT2 (" Push pattern %p: ", pattern); \
1646 DEBUG_PRINT_COMPILED_PATTERN (bufp, pattern, pend); \
1647 PUSH_FAILURE_POINTER (pattern); \
1649 /* Close the frame by moving the frame pointer past it. */ \
1650 fail_stack.frame = fail_stack.avail; \
1651 } while (0)
1653 /* Estimate the size of data pushed by a typical failure stack entry.
1654 An estimate is all we need, because all we use this for
1655 is to choose a limit for how big to make the failure stack. */
1656 /* BEWARE, the value `20' is hard-coded in emacs.c:main(). */
1657 #define TYPICAL_FAILURE_SIZE 20
1659 /* How many items can still be added to the stack without overflowing it. */
1660 #define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail)
1663 /* Pops what PUSH_FAIL_STACK pushes.
1665 We restore into the parameters, all of which should be lvalues:
1666 STR -- the saved data position.
1667 PAT -- the saved pattern position.
1668 REGSTART, REGEND -- arrays of string positions.
1670 Also assumes the variables `fail_stack' and (if debugging), `bufp',
1671 `pend', `string1', `size1', `string2', and `size2'. */
1673 #define POP_FAILURE_POINT(str, pat) \
1674 do { \
1675 assert (!FAIL_STACK_EMPTY ()); \
1677 /* Remove failure points and point to how many regs pushed. */ \
1678 DEBUG_PRINT1 ("POP_FAILURE_POINT:\n"); \
1679 DEBUG_PRINT2 (" Before pop, next avail: %d\n", fail_stack.avail); \
1680 DEBUG_PRINT2 (" size: %d\n", fail_stack.size); \
1682 /* Pop the saved registers. */ \
1683 while (fail_stack.frame < fail_stack.avail) \
1684 POP_FAILURE_REG_OR_COUNT (); \
1686 pat = POP_FAILURE_POINTER (); \
1687 DEBUG_PRINT2 (" Popping pattern %p: ", pat); \
1688 DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend); \
1690 /* If the saved string location is NULL, it came from an \
1691 on_failure_keep_string_jump opcode, and we want to throw away the \
1692 saved NULL, thus retaining our current position in the string. */ \
1693 str = POP_FAILURE_POINTER (); \
1694 DEBUG_PRINT2 (" Popping string %p: `", str); \
1695 DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2); \
1696 DEBUG_PRINT1 ("'\n"); \
1698 fail_stack.frame = POP_FAILURE_INT (); \
1699 DEBUG_PRINT2 (" Popping frame index: %d\n", fail_stack.frame); \
1701 assert (fail_stack.avail >= 0); \
1702 assert (fail_stack.frame <= fail_stack.avail); \
1704 DEBUG_STATEMENT (nfailure_points_popped++); \
1705 } while (0) /* POP_FAILURE_POINT */
1709 /* Registers are set to a sentinel when they haven't yet matched. */
1710 #define REG_UNSET(e) ((e) == NULL)
1712 /* Subroutine declarations and macros for regex_compile. */
1714 static reg_errcode_t regex_compile _RE_ARGS ((re_char *pattern, size_t size,
1715 reg_syntax_t syntax,
1716 struct re_pattern_buffer *bufp));
1717 static void store_op1 _RE_ARGS ((re_opcode_t op, unsigned char *loc, int arg));
1718 static void store_op2 _RE_ARGS ((re_opcode_t op, unsigned char *loc,
1719 int arg1, int arg2));
1720 static void insert_op1 _RE_ARGS ((re_opcode_t op, unsigned char *loc,
1721 int arg, unsigned char *end));
1722 static void insert_op2 _RE_ARGS ((re_opcode_t op, unsigned char *loc,
1723 int arg1, int arg2, unsigned char *end));
1724 static boolean at_begline_loc_p _RE_ARGS ((re_char *pattern,
1725 re_char *p,
1726 reg_syntax_t syntax));
1727 static boolean at_endline_loc_p _RE_ARGS ((re_char *p,
1728 re_char *pend,
1729 reg_syntax_t syntax));
1730 static re_char *skip_one_char _RE_ARGS ((re_char *p));
1731 static int analyse_first _RE_ARGS ((re_char *p, re_char *pend,
1732 char *fastmap, const int multibyte));
1734 /* Fetch the next character in the uncompiled pattern, with no
1735 translation. */
1736 #define PATFETCH(c) \
1737 do { \
1738 int len; \
1739 if (p == pend) return REG_EEND; \
1740 c = RE_STRING_CHAR_AND_LENGTH (p, pend - p, len); \
1741 p += len; \
1742 } while (0)
1745 /* If `translate' is non-null, return translate[D], else just D. We
1746 cast the subscript to translate because some data is declared as
1747 `char *', to avoid warnings when a string constant is passed. But
1748 when we use a character as a subscript we must make it unsigned. */
1749 #ifndef TRANSLATE
1750 # define TRANSLATE(d) \
1751 (RE_TRANSLATE_P (translate) ? RE_TRANSLATE (translate, (d)) : (d))
1752 #endif
1755 /* Macros for outputting the compiled pattern into `buffer'. */
1757 /* If the buffer isn't allocated when it comes in, use this. */
1758 #define INIT_BUF_SIZE 32
1760 /* Make sure we have at least N more bytes of space in buffer. */
1761 #define GET_BUFFER_SPACE(n) \
1762 while ((size_t) (b - bufp->buffer + (n)) > bufp->allocated) \
1763 EXTEND_BUFFER ()
1765 /* Make sure we have one more byte of buffer space and then add C to it. */
1766 #define BUF_PUSH(c) \
1767 do { \
1768 GET_BUFFER_SPACE (1); \
1769 *b++ = (unsigned char) (c); \
1770 } while (0)
1773 /* Ensure we have two more bytes of buffer space and then append C1 and C2. */
1774 #define BUF_PUSH_2(c1, c2) \
1775 do { \
1776 GET_BUFFER_SPACE (2); \
1777 *b++ = (unsigned char) (c1); \
1778 *b++ = (unsigned char) (c2); \
1779 } while (0)
1782 /* As with BUF_PUSH_2, except for three bytes. */
1783 #define BUF_PUSH_3(c1, c2, c3) \
1784 do { \
1785 GET_BUFFER_SPACE (3); \
1786 *b++ = (unsigned char) (c1); \
1787 *b++ = (unsigned char) (c2); \
1788 *b++ = (unsigned char) (c3); \
1789 } while (0)
1792 /* Store a jump with opcode OP at LOC to location TO. We store a
1793 relative address offset by the three bytes the jump itself occupies. */
1794 #define STORE_JUMP(op, loc, to) \
1795 store_op1 (op, loc, (to) - (loc) - 3)
1797 /* Likewise, for a two-argument jump. */
1798 #define STORE_JUMP2(op, loc, to, arg) \
1799 store_op2 (op, loc, (to) - (loc) - 3, arg)
1801 /* Like `STORE_JUMP', but for inserting. Assume `b' is the buffer end. */
1802 #define INSERT_JUMP(op, loc, to) \
1803 insert_op1 (op, loc, (to) - (loc) - 3, b)
1805 /* Like `STORE_JUMP2', but for inserting. Assume `b' is the buffer end. */
1806 #define INSERT_JUMP2(op, loc, to, arg) \
1807 insert_op2 (op, loc, (to) - (loc) - 3, arg, b)
1810 /* This is not an arbitrary limit: the arguments which represent offsets
1811 into the pattern are two bytes long. So if 2^15 bytes turns out to
1812 be too small, many things would have to change. */
1813 # define MAX_BUF_SIZE (1L << 15)
1815 #if 0 /* This is when we thought it could be 2^16 bytes. */
1816 /* Any other compiler which, like MSC, has allocation limit below 2^16
1817 bytes will have to use approach similar to what was done below for
1818 MSC and drop MAX_BUF_SIZE a bit. Otherwise you may end up
1819 reallocating to 0 bytes. Such thing is not going to work too well.
1820 You have been warned!! */
1821 #if defined _MSC_VER && !defined WIN32
1822 /* Microsoft C 16-bit versions limit malloc to approx 65512 bytes. */
1823 # define MAX_BUF_SIZE 65500L
1824 #else
1825 # define MAX_BUF_SIZE (1L << 16)
1826 #endif
1827 #endif /* 0 */
1829 /* Extend the buffer by twice its current size via realloc and
1830 reset the pointers that pointed into the old block to point to the
1831 correct places in the new one. If extending the buffer results in it
1832 being larger than MAX_BUF_SIZE, then flag memory exhausted. */
1833 #if __BOUNDED_POINTERS__
1834 # define SET_HIGH_BOUND(P) (__ptrhigh (P) = __ptrlow (P) + bufp->allocated)
1835 # define MOVE_BUFFER_POINTER(P) \
1836 (__ptrlow (P) = new_buffer + (__ptrlow (P) - old_buffer), \
1837 SET_HIGH_BOUND (P), \
1838 __ptrvalue (P) = new_buffer + (__ptrvalue (P) - old_buffer))
1839 # define ELSE_EXTEND_BUFFER_HIGH_BOUND \
1840 else \
1842 SET_HIGH_BOUND (b); \
1843 SET_HIGH_BOUND (begalt); \
1844 if (fixup_alt_jump) \
1845 SET_HIGH_BOUND (fixup_alt_jump); \
1846 if (laststart) \
1847 SET_HIGH_BOUND (laststart); \
1848 if (pending_exact) \
1849 SET_HIGH_BOUND (pending_exact); \
1851 #else
1852 # define MOVE_BUFFER_POINTER(P) ((P) = new_buffer + ((P) - old_buffer))
1853 # define ELSE_EXTEND_BUFFER_HIGH_BOUND
1854 #endif
1855 #define EXTEND_BUFFER() \
1856 do { \
1857 unsigned char *old_buffer = bufp->buffer; \
1858 if (bufp->allocated == MAX_BUF_SIZE) \
1859 return REG_ESIZE; \
1860 bufp->allocated <<= 1; \
1861 if (bufp->allocated > MAX_BUF_SIZE) \
1862 bufp->allocated = MAX_BUF_SIZE; \
1863 RETALLOC (bufp->buffer, bufp->allocated, unsigned char); \
1864 if (bufp->buffer == NULL) \
1865 return REG_ESPACE; \
1866 /* If the buffer moved, move all the pointers into it. */ \
1867 if (old_buffer != bufp->buffer) \
1869 unsigned char *new_buffer = bufp->buffer; \
1870 MOVE_BUFFER_POINTER (b); \
1871 MOVE_BUFFER_POINTER (begalt); \
1872 if (fixup_alt_jump) \
1873 MOVE_BUFFER_POINTER (fixup_alt_jump); \
1874 if (laststart) \
1875 MOVE_BUFFER_POINTER (laststart); \
1876 if (pending_exact) \
1877 MOVE_BUFFER_POINTER (pending_exact); \
1879 ELSE_EXTEND_BUFFER_HIGH_BOUND \
1880 } while (0)
1883 /* Since we have one byte reserved for the register number argument to
1884 {start,stop}_memory, the maximum number of groups we can report
1885 things about is what fits in that byte. */
1886 #define MAX_REGNUM 255
1888 /* But patterns can have more than `MAX_REGNUM' registers. We just
1889 ignore the excess. */
1890 typedef int regnum_t;
1893 /* Macros for the compile stack. */
1895 /* Since offsets can go either forwards or backwards, this type needs to
1896 be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1. */
1897 /* int may be not enough when sizeof(int) == 2. */
1898 typedef long pattern_offset_t;
1900 typedef struct
1902 pattern_offset_t begalt_offset;
1903 pattern_offset_t fixup_alt_jump;
1904 pattern_offset_t laststart_offset;
1905 regnum_t regnum;
1906 } compile_stack_elt_t;
1909 typedef struct
1911 compile_stack_elt_t *stack;
1912 unsigned size;
1913 unsigned avail; /* Offset of next open position. */
1914 } compile_stack_type;
1917 #define INIT_COMPILE_STACK_SIZE 32
1919 #define COMPILE_STACK_EMPTY (compile_stack.avail == 0)
1920 #define COMPILE_STACK_FULL (compile_stack.avail == compile_stack.size)
1922 /* The next available element. */
1923 #define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
1925 /* Explicit quit checking is only used on NTemacs and whenever we
1926 use polling to process input events. */
1927 #if defined emacs && (defined WINDOWSNT || defined SYNC_INPUT) && defined QUIT
1928 extern int immediate_quit;
1929 # define IMMEDIATE_QUIT_CHECK \
1930 do { \
1931 if (immediate_quit) QUIT; \
1932 } while (0)
1933 #else
1934 # define IMMEDIATE_QUIT_CHECK ((void)0)
1935 #endif
1937 /* Structure to manage work area for range table. */
1938 struct range_table_work_area
1940 int *table; /* actual work area. */
1941 int allocated; /* allocated size for work area in bytes. */
1942 int used; /* actually used size in words. */
1943 int bits; /* flag to record character classes */
1946 /* Make sure that WORK_AREA can hold more N multibyte characters.
1947 This is used only in set_image_of_range and set_image_of_range_1.
1948 It expects WORK_AREA to be a pointer.
1949 If it can't get the space, it returns from the surrounding function. */
1951 #define EXTEND_RANGE_TABLE(work_area, n) \
1952 do { \
1953 if (((work_area)->used + (n)) * sizeof (int) > (work_area)->allocated) \
1955 extend_range_table_work_area (work_area); \
1956 if ((work_area)->table == 0) \
1957 return (REG_ESPACE); \
1959 } while (0)
1961 #define SET_RANGE_TABLE_WORK_AREA_BIT(work_area, bit) \
1962 (work_area).bits |= (bit)
1964 /* Bits used to implement the multibyte-part of the various character classes
1965 such as [:alnum:] in a charset's range table. */
1966 #define BIT_WORD 0x1
1967 #define BIT_LOWER 0x2
1968 #define BIT_PUNCT 0x4
1969 #define BIT_SPACE 0x8
1970 #define BIT_UPPER 0x10
1971 #define BIT_MULTIBYTE 0x20
1973 /* Set a range START..END to WORK_AREA.
1974 The range is passed through TRANSLATE, so START and END
1975 should be untranslated. */
1976 #define SET_RANGE_TABLE_WORK_AREA(work_area, start, end) \
1977 do { \
1978 int tem; \
1979 tem = set_image_of_range (&work_area, start, end, translate); \
1980 if (tem > 0) \
1981 FREE_STACK_RETURN (tem); \
1982 } while (0)
1984 /* Free allocated memory for WORK_AREA. */
1985 #define FREE_RANGE_TABLE_WORK_AREA(work_area) \
1986 do { \
1987 if ((work_area).table) \
1988 free ((work_area).table); \
1989 } while (0)
1991 #define CLEAR_RANGE_TABLE_WORK_USED(work_area) ((work_area).used = 0, (work_area).bits = 0)
1992 #define RANGE_TABLE_WORK_USED(work_area) ((work_area).used)
1993 #define RANGE_TABLE_WORK_BITS(work_area) ((work_area).bits)
1994 #define RANGE_TABLE_WORK_ELT(work_area, i) ((work_area).table[i])
1997 /* Set the bit for character C in a list. */
1998 #define SET_LIST_BIT(c) (b[((c)) / BYTEWIDTH] |= 1 << ((c) % BYTEWIDTH))
2001 /* Get the next unsigned number in the uncompiled pattern. */
2002 #define GET_UNSIGNED_NUMBER(num) \
2003 do { \
2004 if (p == pend) \
2005 FREE_STACK_RETURN (REG_EBRACE); \
2006 else \
2008 PATFETCH (c); \
2009 while ('0' <= c && c <= '9') \
2011 int prev; \
2012 if (num < 0) \
2013 num = 0; \
2014 prev = num; \
2015 num = num * 10 + c - '0'; \
2016 if (num / 10 != prev) \
2017 FREE_STACK_RETURN (REG_BADBR); \
2018 if (p == pend) \
2019 FREE_STACK_RETURN (REG_EBRACE); \
2020 PATFETCH (c); \
2023 } while (0)
2025 #if ! WIDE_CHAR_SUPPORT
2027 /* Map a string to the char class it names (if any). */
2028 re_wctype_t
2029 re_wctype (str)
2030 re_char *str;
2032 const char *string = str;
2033 if (STREQ (string, "alnum")) return RECC_ALNUM;
2034 else if (STREQ (string, "alpha")) return RECC_ALPHA;
2035 else if (STREQ (string, "word")) return RECC_WORD;
2036 else if (STREQ (string, "ascii")) return RECC_ASCII;
2037 else if (STREQ (string, "nonascii")) return RECC_NONASCII;
2038 else if (STREQ (string, "graph")) return RECC_GRAPH;
2039 else if (STREQ (string, "lower")) return RECC_LOWER;
2040 else if (STREQ (string, "print")) return RECC_PRINT;
2041 else if (STREQ (string, "punct")) return RECC_PUNCT;
2042 else if (STREQ (string, "space")) return RECC_SPACE;
2043 else if (STREQ (string, "upper")) return RECC_UPPER;
2044 else if (STREQ (string, "unibyte")) return RECC_UNIBYTE;
2045 else if (STREQ (string, "multibyte")) return RECC_MULTIBYTE;
2046 else if (STREQ (string, "digit")) return RECC_DIGIT;
2047 else if (STREQ (string, "xdigit")) return RECC_XDIGIT;
2048 else if (STREQ (string, "cntrl")) return RECC_CNTRL;
2049 else if (STREQ (string, "blank")) return RECC_BLANK;
2050 else return 0;
2053 /* True if CH is in the char class CC. */
2054 boolean
2055 re_iswctype (ch, cc)
2056 int ch;
2057 re_wctype_t cc;
2059 switch (cc)
2061 case RECC_ALNUM: return ISALNUM (ch);
2062 case RECC_ALPHA: return ISALPHA (ch);
2063 case RECC_BLANK: return ISBLANK (ch);
2064 case RECC_CNTRL: return ISCNTRL (ch);
2065 case RECC_DIGIT: return ISDIGIT (ch);
2066 case RECC_GRAPH: return ISGRAPH (ch);
2067 case RECC_LOWER: return ISLOWER (ch);
2068 case RECC_PRINT: return ISPRINT (ch);
2069 case RECC_PUNCT: return ISPUNCT (ch);
2070 case RECC_SPACE: return ISSPACE (ch);
2071 case RECC_UPPER: return ISUPPER (ch);
2072 case RECC_XDIGIT: return ISXDIGIT (ch);
2073 case RECC_ASCII: return IS_REAL_ASCII (ch);
2074 case RECC_NONASCII: return !IS_REAL_ASCII (ch);
2075 case RECC_UNIBYTE: return ISUNIBYTE (ch);
2076 case RECC_MULTIBYTE: return !ISUNIBYTE (ch);
2077 case RECC_WORD: return ISWORD (ch);
2078 case RECC_ERROR: return false;
2079 default:
2080 abort();
2084 /* Return a bit-pattern to use in the range-table bits to match multibyte
2085 chars of class CC. */
2086 static int
2087 re_wctype_to_bit (cc)
2088 re_wctype_t cc;
2090 switch (cc)
2092 case RECC_NONASCII: case RECC_PRINT: case RECC_GRAPH:
2093 case RECC_MULTIBYTE: return BIT_MULTIBYTE;
2094 case RECC_ALPHA: case RECC_ALNUM: case RECC_WORD: return BIT_WORD;
2095 case RECC_LOWER: return BIT_LOWER;
2096 case RECC_UPPER: return BIT_UPPER;
2097 case RECC_PUNCT: return BIT_PUNCT;
2098 case RECC_SPACE: return BIT_SPACE;
2099 case RECC_ASCII: case RECC_DIGIT: case RECC_XDIGIT: case RECC_CNTRL:
2100 case RECC_BLANK: case RECC_UNIBYTE: case RECC_ERROR: return 0;
2101 default:
2102 abort();
2105 #endif
2107 /* Filling in the work area of a range. */
2109 /* Actually extend the space in WORK_AREA. */
2111 static void
2112 extend_range_table_work_area (work_area)
2113 struct range_table_work_area *work_area;
2115 work_area->allocated += 16 * sizeof (int);
2116 if (work_area->table)
2117 work_area->table
2118 = (int *) realloc (work_area->table, work_area->allocated);
2119 else
2120 work_area->table
2121 = (int *) malloc (work_area->allocated);
2124 #ifdef emacs
2126 /* Carefully find the ranges of codes that are equivalent
2127 under case conversion to the range start..end when passed through
2128 TRANSLATE. Handle the case where non-letters can come in between
2129 two upper-case letters (which happens in Latin-1).
2130 Also handle the case of groups of more than 2 case-equivalent chars.
2132 The basic method is to look at consecutive characters and see
2133 if they can form a run that can be handled as one.
2135 Returns -1 if successful, REG_ESPACE if ran out of space. */
2137 static int
2138 set_image_of_range_1 (work_area, start, end, translate)
2139 RE_TRANSLATE_TYPE translate;
2140 struct range_table_work_area *work_area;
2141 re_wchar_t start, end;
2143 /* `one_case' indicates a character, or a run of characters,
2144 each of which is an isolate (no case-equivalents).
2145 This includes all ASCII non-letters.
2147 `two_case' indicates a character, or a run of characters,
2148 each of which has two case-equivalent forms.
2149 This includes all ASCII letters.
2151 `strange' indicates a character that has more than one
2152 case-equivalent. */
2154 enum case_type {one_case, two_case, strange};
2156 /* Describe the run that is in progress,
2157 which the next character can try to extend.
2158 If run_type is strange, that means there really is no run.
2159 If run_type is one_case, then run_start...run_end is the run.
2160 If run_type is two_case, then the run is run_start...run_end,
2161 and the case-equivalents end at run_eqv_end. */
2163 enum case_type run_type = strange;
2164 int run_start, run_end, run_eqv_end;
2166 Lisp_Object eqv_table;
2168 if (!RE_TRANSLATE_P (translate))
2170 EXTEND_RANGE_TABLE (work_area, 2);
2171 work_area->table[work_area->used++] = (start);
2172 work_area->table[work_area->used++] = (end);
2173 return -1;
2176 eqv_table = XCHAR_TABLE (translate)->extras[2];
2178 for (; start <= end; start++)
2180 enum case_type this_type;
2181 int eqv = RE_TRANSLATE (eqv_table, start);
2182 int minchar, maxchar;
2184 /* Classify this character */
2185 if (eqv == start)
2186 this_type = one_case;
2187 else if (RE_TRANSLATE (eqv_table, eqv) == start)
2188 this_type = two_case;
2189 else
2190 this_type = strange;
2192 if (start < eqv)
2193 minchar = start, maxchar = eqv;
2194 else
2195 minchar = eqv, maxchar = start;
2197 /* Can this character extend the run in progress? */
2198 if (this_type == strange || this_type != run_type
2199 || !(minchar == run_end + 1
2200 && (run_type == two_case
2201 ? maxchar == run_eqv_end + 1 : 1)))
2203 /* No, end the run.
2204 Record each of its equivalent ranges. */
2205 if (run_type == one_case)
2207 EXTEND_RANGE_TABLE (work_area, 2);
2208 work_area->table[work_area->used++] = run_start;
2209 work_area->table[work_area->used++] = run_end;
2211 else if (run_type == two_case)
2213 EXTEND_RANGE_TABLE (work_area, 4);
2214 work_area->table[work_area->used++] = run_start;
2215 work_area->table[work_area->used++] = run_end;
2216 work_area->table[work_area->used++]
2217 = RE_TRANSLATE (eqv_table, run_start);
2218 work_area->table[work_area->used++]
2219 = RE_TRANSLATE (eqv_table, run_end);
2221 run_type = strange;
2224 if (this_type == strange)
2226 /* For a strange character, add each of its equivalents, one
2227 by one. Don't start a range. */
2230 EXTEND_RANGE_TABLE (work_area, 2);
2231 work_area->table[work_area->used++] = eqv;
2232 work_area->table[work_area->used++] = eqv;
2233 eqv = RE_TRANSLATE (eqv_table, eqv);
2235 while (eqv != start);
2238 /* Add this char to the run, or start a new run. */
2239 else if (run_type == strange)
2241 /* Initialize a new range. */
2242 run_type = this_type;
2243 run_start = start;
2244 run_end = start;
2245 run_eqv_end = RE_TRANSLATE (eqv_table, run_end);
2247 else
2249 /* Extend a running range. */
2250 run_end = minchar;
2251 run_eqv_end = RE_TRANSLATE (eqv_table, run_end);
2255 /* If a run is still in progress at the end, finish it now
2256 by recording its equivalent ranges. */
2257 if (run_type == one_case)
2259 EXTEND_RANGE_TABLE (work_area, 2);
2260 work_area->table[work_area->used++] = run_start;
2261 work_area->table[work_area->used++] = run_end;
2263 else if (run_type == two_case)
2265 EXTEND_RANGE_TABLE (work_area, 4);
2266 work_area->table[work_area->used++] = run_start;
2267 work_area->table[work_area->used++] = run_end;
2268 work_area->table[work_area->used++]
2269 = RE_TRANSLATE (eqv_table, run_start);
2270 work_area->table[work_area->used++]
2271 = RE_TRANSLATE (eqv_table, run_end);
2274 return -1;
2277 #endif /* emacs */
2279 /* Record the the image of the range start..end when passed through
2280 TRANSLATE. This is not necessarily TRANSLATE(start)..TRANSLATE(end)
2281 and is not even necessarily contiguous.
2282 Normally we approximate it with the smallest contiguous range that contains
2283 all the chars we need. However, for Latin-1 we go to extra effort
2284 to do a better job.
2286 This function is not called for ASCII ranges.
2288 Returns -1 if successful, REG_ESPACE if ran out of space. */
2290 static int
2291 set_image_of_range (work_area, start, end, translate)
2292 RE_TRANSLATE_TYPE translate;
2293 struct range_table_work_area *work_area;
2294 re_wchar_t start, end;
2296 re_wchar_t cmin, cmax;
2298 #ifdef emacs
2299 /* For Latin-1 ranges, use set_image_of_range_1
2300 to get proper handling of ranges that include letters and nonletters.
2301 For a range that includes the whole of Latin-1, this is not necessary.
2302 For other character sets, we don't bother to get this right. */
2303 if (RE_TRANSLATE_P (translate) && start < 04400
2304 && !(start < 04200 && end >= 04377))
2306 int newend;
2307 int tem;
2308 newend = end;
2309 if (newend > 04377)
2310 newend = 04377;
2311 tem = set_image_of_range_1 (work_area, start, newend, translate);
2312 if (tem > 0)
2313 return tem;
2315 start = 04400;
2316 if (end < 04400)
2317 return -1;
2319 #endif
2321 EXTEND_RANGE_TABLE (work_area, 2);
2322 work_area->table[work_area->used++] = (start);
2323 work_area->table[work_area->used++] = (end);
2325 cmin = -1, cmax = -1;
2327 if (RE_TRANSLATE_P (translate))
2329 int ch;
2331 for (ch = start; ch <= end; ch++)
2333 re_wchar_t c = TRANSLATE (ch);
2334 if (! (start <= c && c <= end))
2336 if (cmin == -1)
2337 cmin = c, cmax = c;
2338 else
2340 cmin = MIN (cmin, c);
2341 cmax = MAX (cmax, c);
2346 if (cmin != -1)
2348 EXTEND_RANGE_TABLE (work_area, 2);
2349 work_area->table[work_area->used++] = (cmin);
2350 work_area->table[work_area->used++] = (cmax);
2354 return -1;
2357 #ifndef MATCH_MAY_ALLOCATE
2359 /* If we cannot allocate large objects within re_match_2_internal,
2360 we make the fail stack and register vectors global.
2361 The fail stack, we grow to the maximum size when a regexp
2362 is compiled.
2363 The register vectors, we adjust in size each time we
2364 compile a regexp, according to the number of registers it needs. */
2366 static fail_stack_type fail_stack;
2368 /* Size with which the following vectors are currently allocated.
2369 That is so we can make them bigger as needed,
2370 but never make them smaller. */
2371 static int regs_allocated_size;
2373 static re_char ** regstart, ** regend;
2374 static re_char **best_regstart, **best_regend;
2376 /* Make the register vectors big enough for NUM_REGS registers,
2377 but don't make them smaller. */
2379 static
2380 regex_grow_registers (num_regs)
2381 int num_regs;
2383 if (num_regs > regs_allocated_size)
2385 RETALLOC_IF (regstart, num_regs, re_char *);
2386 RETALLOC_IF (regend, num_regs, re_char *);
2387 RETALLOC_IF (best_regstart, num_regs, re_char *);
2388 RETALLOC_IF (best_regend, num_regs, re_char *);
2390 regs_allocated_size = num_regs;
2394 #endif /* not MATCH_MAY_ALLOCATE */
2396 static boolean group_in_compile_stack _RE_ARGS ((compile_stack_type
2397 compile_stack,
2398 regnum_t regnum));
2400 /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
2401 Returns one of error codes defined in `regex.h', or zero for success.
2403 Assumes the `allocated' (and perhaps `buffer') and `translate'
2404 fields are set in BUFP on entry.
2406 If it succeeds, results are put in BUFP (if it returns an error, the
2407 contents of BUFP are undefined):
2408 `buffer' is the compiled pattern;
2409 `syntax' is set to SYNTAX;
2410 `used' is set to the length of the compiled pattern;
2411 `fastmap_accurate' is zero;
2412 `re_nsub' is the number of subexpressions in PATTERN;
2413 `not_bol' and `not_eol' are zero;
2415 The `fastmap' field is neither examined nor set. */
2417 /* Insert the `jump' from the end of last alternative to "here".
2418 The space for the jump has already been allocated. */
2419 #define FIXUP_ALT_JUMP() \
2420 do { \
2421 if (fixup_alt_jump) \
2422 STORE_JUMP (jump, fixup_alt_jump, b); \
2423 } while (0)
2426 /* Return, freeing storage we allocated. */
2427 #define FREE_STACK_RETURN(value) \
2428 do { \
2429 FREE_RANGE_TABLE_WORK_AREA (range_table_work); \
2430 free (compile_stack.stack); \
2431 return value; \
2432 } while (0)
2434 static reg_errcode_t
2435 regex_compile (pattern, size, syntax, bufp)
2436 re_char *pattern;
2437 size_t size;
2438 reg_syntax_t syntax;
2439 struct re_pattern_buffer *bufp;
2441 /* We fetch characters from PATTERN here. */
2442 register re_wchar_t c, c1;
2444 /* A random temporary spot in PATTERN. */
2445 re_char *p1;
2447 /* Points to the end of the buffer, where we should append. */
2448 register unsigned char *b;
2450 /* Keeps track of unclosed groups. */
2451 compile_stack_type compile_stack;
2453 /* Points to the current (ending) position in the pattern. */
2454 #ifdef AIX
2455 /* `const' makes AIX compiler fail. */
2456 unsigned char *p = pattern;
2457 #else
2458 re_char *p = pattern;
2459 #endif
2460 re_char *pend = pattern + size;
2462 /* How to translate the characters in the pattern. */
2463 RE_TRANSLATE_TYPE translate = bufp->translate;
2465 /* Address of the count-byte of the most recently inserted `exactn'
2466 command. This makes it possible to tell if a new exact-match
2467 character can be added to that command or if the character requires
2468 a new `exactn' command. */
2469 unsigned char *pending_exact = 0;
2471 /* Address of start of the most recently finished expression.
2472 This tells, e.g., postfix * where to find the start of its
2473 operand. Reset at the beginning of groups and alternatives. */
2474 unsigned char *laststart = 0;
2476 /* Address of beginning of regexp, or inside of last group. */
2477 unsigned char *begalt;
2479 /* Place in the uncompiled pattern (i.e., the {) to
2480 which to go back if the interval is invalid. */
2481 re_char *beg_interval;
2483 /* Address of the place where a forward jump should go to the end of
2484 the containing expression. Each alternative of an `or' -- except the
2485 last -- ends with a forward jump of this sort. */
2486 unsigned char *fixup_alt_jump = 0;
2488 /* Counts open-groups as they are encountered. Remembered for the
2489 matching close-group on the compile stack, so the same register
2490 number is put in the stop_memory as the start_memory. */
2491 regnum_t regnum = 0;
2493 /* Work area for range table of charset. */
2494 struct range_table_work_area range_table_work;
2496 /* If the object matched can contain multibyte characters. */
2497 const boolean multibyte = RE_MULTIBYTE_P (bufp);
2499 /* Nonzero if we have pushed down into a subpattern. */
2500 int in_subpattern = 0;
2502 /* These hold the values of p, pattern, and pend from the main
2503 pattern when we have pushed into a subpattern. */
2504 re_char *main_p;
2505 re_char *main_pattern;
2506 re_char *main_pend;
2508 #ifdef DEBUG
2509 debug++;
2510 DEBUG_PRINT1 ("\nCompiling pattern: ");
2511 if (debug > 0)
2513 unsigned debug_count;
2515 for (debug_count = 0; debug_count < size; debug_count++)
2516 putchar (pattern[debug_count]);
2517 putchar ('\n');
2519 #endif /* DEBUG */
2521 /* Initialize the compile stack. */
2522 compile_stack.stack = TALLOC (INIT_COMPILE_STACK_SIZE, compile_stack_elt_t);
2523 if (compile_stack.stack == NULL)
2524 return REG_ESPACE;
2526 compile_stack.size = INIT_COMPILE_STACK_SIZE;
2527 compile_stack.avail = 0;
2529 range_table_work.table = 0;
2530 range_table_work.allocated = 0;
2532 /* Initialize the pattern buffer. */
2533 bufp->syntax = syntax;
2534 bufp->fastmap_accurate = 0;
2535 bufp->not_bol = bufp->not_eol = 0;
2536 bufp->used_syntax = 0;
2538 /* Set `used' to zero, so that if we return an error, the pattern
2539 printer (for debugging) will think there's no pattern. We reset it
2540 at the end. */
2541 bufp->used = 0;
2543 /* Always count groups, whether or not bufp->no_sub is set. */
2544 bufp->re_nsub = 0;
2546 #if !defined emacs && !defined SYNTAX_TABLE
2547 /* Initialize the syntax table. */
2548 init_syntax_once ();
2549 #endif
2551 if (bufp->allocated == 0)
2553 if (bufp->buffer)
2554 { /* If zero allocated, but buffer is non-null, try to realloc
2555 enough space. This loses if buffer's address is bogus, but
2556 that is the user's responsibility. */
2557 RETALLOC (bufp->buffer, INIT_BUF_SIZE, unsigned char);
2559 else
2560 { /* Caller did not allocate a buffer. Do it for them. */
2561 bufp->buffer = TALLOC (INIT_BUF_SIZE, unsigned char);
2563 if (!bufp->buffer) FREE_STACK_RETURN (REG_ESPACE);
2565 bufp->allocated = INIT_BUF_SIZE;
2568 begalt = b = bufp->buffer;
2570 /* Loop through the uncompiled pattern until we're at the end. */
2571 while (1)
2573 if (p == pend)
2575 /* If this is the end of an included regexp,
2576 pop back to the main regexp and try again. */
2577 if (in_subpattern)
2579 in_subpattern = 0;
2580 pattern = main_pattern;
2581 p = main_p;
2582 pend = main_pend;
2583 continue;
2585 /* If this is the end of the main regexp, we are done. */
2586 break;
2589 PATFETCH (c);
2591 switch (c)
2593 case ' ':
2595 re_char *p1 = p;
2597 /* If there's no special whitespace regexp, treat
2598 spaces normally. And don't try to do this recursively. */
2599 if (!whitespace_regexp || in_subpattern)
2600 goto normal_char;
2602 /* Peek past following spaces. */
2603 while (p1 != pend)
2605 if (*p1 != ' ')
2606 break;
2607 p1++;
2609 /* If the spaces are followed by a repetition op,
2610 treat them normally. */
2611 if (p1 != pend
2612 && (*p1 == '*' || *p1 == '+' || *p1 == '?'
2613 || (*p1 == '\\' && p1 + 1 != pend && p1[1] == '{')))
2614 goto normal_char;
2616 /* Replace the spaces with the whitespace regexp. */
2617 in_subpattern = 1;
2618 main_p = p1;
2619 main_pend = pend;
2620 main_pattern = pattern;
2621 p = pattern = whitespace_regexp;
2622 pend = p + strlen (p);
2623 break;
2626 case '^':
2628 if ( /* If at start of pattern, it's an operator. */
2629 p == pattern + 1
2630 /* If context independent, it's an operator. */
2631 || syntax & RE_CONTEXT_INDEP_ANCHORS
2632 /* Otherwise, depends on what's come before. */
2633 || at_begline_loc_p (pattern, p, syntax))
2634 BUF_PUSH ((syntax & RE_NO_NEWLINE_ANCHOR) ? begbuf : begline);
2635 else
2636 goto normal_char;
2638 break;
2641 case '$':
2643 if ( /* If at end of pattern, it's an operator. */
2644 p == pend
2645 /* If context independent, it's an operator. */
2646 || syntax & RE_CONTEXT_INDEP_ANCHORS
2647 /* Otherwise, depends on what's next. */
2648 || at_endline_loc_p (p, pend, syntax))
2649 BUF_PUSH ((syntax & RE_NO_NEWLINE_ANCHOR) ? endbuf : endline);
2650 else
2651 goto normal_char;
2653 break;
2656 case '+':
2657 case '?':
2658 if ((syntax & RE_BK_PLUS_QM)
2659 || (syntax & RE_LIMITED_OPS))
2660 goto normal_char;
2661 handle_plus:
2662 case '*':
2663 /* If there is no previous pattern... */
2664 if (!laststart)
2666 if (syntax & RE_CONTEXT_INVALID_OPS)
2667 FREE_STACK_RETURN (REG_BADRPT);
2668 else if (!(syntax & RE_CONTEXT_INDEP_OPS))
2669 goto normal_char;
2673 /* 1 means zero (many) matches is allowed. */
2674 boolean zero_times_ok = 0, many_times_ok = 0;
2675 boolean greedy = 1;
2677 /* If there is a sequence of repetition chars, collapse it
2678 down to just one (the right one). We can't combine
2679 interval operators with these because of, e.g., `a{2}*',
2680 which should only match an even number of `a's. */
2682 for (;;)
2684 if ((syntax & RE_FRUGAL)
2685 && c == '?' && (zero_times_ok || many_times_ok))
2686 greedy = 0;
2687 else
2689 zero_times_ok |= c != '+';
2690 many_times_ok |= c != '?';
2693 if (p == pend)
2694 break;
2695 else if (*p == '*'
2696 || (!(syntax & RE_BK_PLUS_QM)
2697 && (*p == '+' || *p == '?')))
2699 else if (syntax & RE_BK_PLUS_QM && *p == '\\')
2701 if (p+1 == pend)
2702 FREE_STACK_RETURN (REG_EESCAPE);
2703 if (p[1] == '+' || p[1] == '?')
2704 PATFETCH (c); /* Gobble up the backslash. */
2705 else
2706 break;
2708 else
2709 break;
2710 /* If we get here, we found another repeat character. */
2711 PATFETCH (c);
2714 /* Star, etc. applied to an empty pattern is equivalent
2715 to an empty pattern. */
2716 if (!laststart || laststart == b)
2717 break;
2719 /* Now we know whether or not zero matches is allowed
2720 and also whether or not two or more matches is allowed. */
2721 if (greedy)
2723 if (many_times_ok)
2725 boolean simple = skip_one_char (laststart) == b;
2726 unsigned int startoffset = 0;
2727 re_opcode_t ofj =
2728 /* Check if the loop can match the empty string. */
2729 (simple || !analyse_first (laststart, b, NULL, 0))
2730 ? on_failure_jump : on_failure_jump_loop;
2731 assert (skip_one_char (laststart) <= b);
2733 if (!zero_times_ok && simple)
2734 { /* Since simple * loops can be made faster by using
2735 on_failure_keep_string_jump, we turn simple P+
2736 into PP* if P is simple. */
2737 unsigned char *p1, *p2;
2738 startoffset = b - laststart;
2739 GET_BUFFER_SPACE (startoffset);
2740 p1 = b; p2 = laststart;
2741 while (p2 < p1)
2742 *b++ = *p2++;
2743 zero_times_ok = 1;
2746 GET_BUFFER_SPACE (6);
2747 if (!zero_times_ok)
2748 /* A + loop. */
2749 STORE_JUMP (ofj, b, b + 6);
2750 else
2751 /* Simple * loops can use on_failure_keep_string_jump
2752 depending on what follows. But since we don't know
2753 that yet, we leave the decision up to
2754 on_failure_jump_smart. */
2755 INSERT_JUMP (simple ? on_failure_jump_smart : ofj,
2756 laststart + startoffset, b + 6);
2757 b += 3;
2758 STORE_JUMP (jump, b, laststart + startoffset);
2759 b += 3;
2761 else
2763 /* A simple ? pattern. */
2764 assert (zero_times_ok);
2765 GET_BUFFER_SPACE (3);
2766 INSERT_JUMP (on_failure_jump, laststart, b + 3);
2767 b += 3;
2770 else /* not greedy */
2771 { /* I wish the greedy and non-greedy cases could be merged. */
2773 GET_BUFFER_SPACE (7); /* We might use less. */
2774 if (many_times_ok)
2776 boolean emptyp = analyse_first (laststart, b, NULL, 0);
2778 /* The non-greedy multiple match looks like
2779 a repeat..until: we only need a conditional jump
2780 at the end of the loop. */
2781 if (emptyp) BUF_PUSH (no_op);
2782 STORE_JUMP (emptyp ? on_failure_jump_nastyloop
2783 : on_failure_jump, b, laststart);
2784 b += 3;
2785 if (zero_times_ok)
2787 /* The repeat...until naturally matches one or more.
2788 To also match zero times, we need to first jump to
2789 the end of the loop (its conditional jump). */
2790 INSERT_JUMP (jump, laststart, b);
2791 b += 3;
2794 else
2796 /* non-greedy a?? */
2797 INSERT_JUMP (jump, laststart, b + 3);
2798 b += 3;
2799 INSERT_JUMP (on_failure_jump, laststart, laststart + 6);
2800 b += 3;
2804 pending_exact = 0;
2805 break;
2808 case '.':
2809 laststart = b;
2810 BUF_PUSH (anychar);
2811 break;
2814 case '[':
2816 CLEAR_RANGE_TABLE_WORK_USED (range_table_work);
2818 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2820 /* Ensure that we have enough space to push a charset: the
2821 opcode, the length count, and the bitset; 34 bytes in all. */
2822 GET_BUFFER_SPACE (34);
2824 laststart = b;
2826 /* We test `*p == '^' twice, instead of using an if
2827 statement, so we only need one BUF_PUSH. */
2828 BUF_PUSH (*p == '^' ? charset_not : charset);
2829 if (*p == '^')
2830 p++;
2832 /* Remember the first position in the bracket expression. */
2833 p1 = p;
2835 /* Push the number of bytes in the bitmap. */
2836 BUF_PUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
2838 /* Clear the whole map. */
2839 bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH);
2841 /* charset_not matches newline according to a syntax bit. */
2842 if ((re_opcode_t) b[-2] == charset_not
2843 && (syntax & RE_HAT_LISTS_NOT_NEWLINE))
2844 SET_LIST_BIT ('\n');
2846 /* Read in characters and ranges, setting map bits. */
2847 for (;;)
2849 boolean escaped_char = false;
2850 const unsigned char *p2 = p;
2852 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2854 /* Don't translate yet. The range TRANSLATE(X..Y) cannot
2855 always be determined from TRANSLATE(X) and TRANSLATE(Y)
2856 So the translation is done later in a loop. Example:
2857 (let ((case-fold-search t)) (string-match "[A-_]" "A")) */
2858 PATFETCH (c);
2860 /* \ might escape characters inside [...] and [^...]. */
2861 if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) && c == '\\')
2863 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
2865 PATFETCH (c);
2866 escaped_char = true;
2868 else
2870 /* Could be the end of the bracket expression. If it's
2871 not (i.e., when the bracket expression is `[]' so
2872 far), the ']' character bit gets set way below. */
2873 if (c == ']' && p2 != p1)
2874 break;
2877 /* What should we do for the character which is
2878 greater than 0x7F, but not BASE_LEADING_CODE_P?
2879 XXX */
2881 /* See if we're at the beginning of a possible character
2882 class. */
2884 if (!escaped_char &&
2885 syntax & RE_CHAR_CLASSES && c == '[' && *p == ':')
2887 /* Leave room for the null. */
2888 unsigned char str[CHAR_CLASS_MAX_LENGTH + 1];
2889 const unsigned char *class_beg;
2891 PATFETCH (c);
2892 c1 = 0;
2893 class_beg = p;
2895 /* If pattern is `[[:'. */
2896 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2898 for (;;)
2900 PATFETCH (c);
2901 if ((c == ':' && *p == ']') || p == pend)
2902 break;
2903 if (c1 < CHAR_CLASS_MAX_LENGTH)
2904 str[c1++] = c;
2905 else
2906 /* This is in any case an invalid class name. */
2907 str[0] = '\0';
2909 str[c1] = '\0';
2911 /* If isn't a word bracketed by `[:' and `:]':
2912 undo the ending character, the letters, and
2913 leave the leading `:' and `[' (but set bits for
2914 them). */
2915 if (c == ':' && *p == ']')
2917 re_wchar_t ch;
2918 re_wctype_t cc;
2920 cc = re_wctype (str);
2922 if (cc == 0)
2923 FREE_STACK_RETURN (REG_ECTYPE);
2925 /* Throw away the ] at the end of the character
2926 class. */
2927 PATFETCH (c);
2929 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2931 /* Most character classes in a multibyte match
2932 just set a flag. Exceptions are is_blank,
2933 is_digit, is_cntrl, and is_xdigit, since
2934 they can only match ASCII characters. We
2935 don't need to handle them for multibyte.
2936 They are distinguished by a negative wctype. */
2938 if (multibyte)
2939 SET_RANGE_TABLE_WORK_AREA_BIT (range_table_work,
2940 re_wctype_to_bit (cc));
2942 for (ch = 0; ch < 1 << BYTEWIDTH; ++ch)
2944 int translated = TRANSLATE (ch);
2945 if (translated < (1 << BYTEWIDTH)
2946 && re_iswctype (btowc (ch), cc))
2947 SET_LIST_BIT (translated);
2950 /* In most cases the matching rule for char classes
2951 only uses the syntax table for multibyte chars,
2952 so that the content of the syntax-table it is not
2953 hardcoded in the range_table. SPACE and WORD are
2954 the two exceptions. */
2955 if ((1 << cc) & ((1 << RECC_SPACE) | (1 << RECC_WORD)))
2956 bufp->used_syntax = 1;
2958 /* Repeat the loop. */
2959 continue;
2961 else
2963 /* Go back to right after the "[:". */
2964 p = class_beg;
2965 SET_LIST_BIT ('[');
2967 /* Because the `:' may starts the range, we
2968 can't simply set bit and repeat the loop.
2969 Instead, just set it to C and handle below. */
2970 c = ':';
2974 if (p < pend && p[0] == '-' && p[1] != ']')
2977 /* Discard the `-'. */
2978 PATFETCH (c1);
2980 /* Fetch the character which ends the range. */
2981 PATFETCH (c1);
2983 if (SINGLE_BYTE_CHAR_P (c))
2985 if (! SINGLE_BYTE_CHAR_P (c1))
2987 /* Handle a range starting with a
2988 character of less than 256, and ending
2989 with a character of not less than 256.
2990 Split that into two ranges, the low one
2991 ending at 0377, and the high one
2992 starting at the smallest character in
2993 the charset of C1 and ending at C1. */
2994 int charset = CHAR_CHARSET (c1);
2995 re_wchar_t c2 = MAKE_CHAR (charset, 0, 0);
2997 SET_RANGE_TABLE_WORK_AREA (range_table_work,
2998 c2, c1);
2999 c1 = 0377;
3002 else if (!SAME_CHARSET_P (c, c1))
3003 FREE_STACK_RETURN (REG_ERANGEX);
3005 else
3006 /* Range from C to C. */
3007 c1 = c;
3009 /* Set the range ... */
3010 if (SINGLE_BYTE_CHAR_P (c))
3011 /* ... into bitmap. */
3013 re_wchar_t this_char;
3014 re_wchar_t range_start = c, range_end = c1;
3016 /* If the start is after the end, the range is empty. */
3017 if (range_start > range_end)
3019 if (syntax & RE_NO_EMPTY_RANGES)
3020 FREE_STACK_RETURN (REG_ERANGE);
3021 /* Else, repeat the loop. */
3023 else
3025 for (this_char = range_start; this_char <= range_end;
3026 this_char++)
3028 int translated = TRANSLATE (this_char);
3029 if (translated < (1 << BYTEWIDTH))
3030 SET_LIST_BIT (translated);
3031 else
3032 SET_RANGE_TABLE_WORK_AREA
3033 (range_table_work, translated, translated);
3037 else
3038 /* ... into range table. */
3039 SET_RANGE_TABLE_WORK_AREA (range_table_work, c, c1);
3042 /* Discard any (non)matching list bytes that are all 0 at the
3043 end of the map. Decrease the map-length byte too. */
3044 while ((int) b[-1] > 0 && b[b[-1] - 1] == 0)
3045 b[-1]--;
3046 b += b[-1];
3048 /* Build real range table from work area. */
3049 if (RANGE_TABLE_WORK_USED (range_table_work)
3050 || RANGE_TABLE_WORK_BITS (range_table_work))
3052 int i;
3053 int used = RANGE_TABLE_WORK_USED (range_table_work);
3055 /* Allocate space for COUNT + RANGE_TABLE. Needs two
3056 bytes for flags, two for COUNT, and three bytes for
3057 each character. */
3058 GET_BUFFER_SPACE (4 + used * 3);
3060 /* Indicate the existence of range table. */
3061 laststart[1] |= 0x80;
3063 /* Store the character class flag bits into the range table.
3064 If not in emacs, these flag bits are always 0. */
3065 *b++ = RANGE_TABLE_WORK_BITS (range_table_work) & 0xff;
3066 *b++ = RANGE_TABLE_WORK_BITS (range_table_work) >> 8;
3068 STORE_NUMBER_AND_INCR (b, used / 2);
3069 for (i = 0; i < used; i++)
3070 STORE_CHARACTER_AND_INCR
3071 (b, RANGE_TABLE_WORK_ELT (range_table_work, i));
3074 break;
3077 case '(':
3078 if (syntax & RE_NO_BK_PARENS)
3079 goto handle_open;
3080 else
3081 goto normal_char;
3084 case ')':
3085 if (syntax & RE_NO_BK_PARENS)
3086 goto handle_close;
3087 else
3088 goto normal_char;
3091 case '\n':
3092 if (syntax & RE_NEWLINE_ALT)
3093 goto handle_alt;
3094 else
3095 goto normal_char;
3098 case '|':
3099 if (syntax & RE_NO_BK_VBAR)
3100 goto handle_alt;
3101 else
3102 goto normal_char;
3105 case '{':
3106 if (syntax & RE_INTERVALS && syntax & RE_NO_BK_BRACES)
3107 goto handle_interval;
3108 else
3109 goto normal_char;
3112 case '\\':
3113 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
3115 /* Do not translate the character after the \, so that we can
3116 distinguish, e.g., \B from \b, even if we normally would
3117 translate, e.g., B to b. */
3118 PATFETCH (c);
3120 switch (c)
3122 case '(':
3123 if (syntax & RE_NO_BK_PARENS)
3124 goto normal_backslash;
3126 handle_open:
3128 int shy = 0;
3129 if (p+1 < pend)
3131 /* Look for a special (?...) construct */
3132 if ((syntax & RE_SHY_GROUPS) && *p == '?')
3134 PATFETCH (c); /* Gobble up the '?'. */
3135 PATFETCH (c);
3136 switch (c)
3138 case ':': shy = 1; break;
3139 default:
3140 /* Only (?:...) is supported right now. */
3141 FREE_STACK_RETURN (REG_BADPAT);
3146 if (!shy)
3148 bufp->re_nsub++;
3149 regnum++;
3152 if (COMPILE_STACK_FULL)
3154 RETALLOC (compile_stack.stack, compile_stack.size << 1,
3155 compile_stack_elt_t);
3156 if (compile_stack.stack == NULL) return REG_ESPACE;
3158 compile_stack.size <<= 1;
3161 /* These are the values to restore when we hit end of this
3162 group. They are all relative offsets, so that if the
3163 whole pattern moves because of realloc, they will still
3164 be valid. */
3165 COMPILE_STACK_TOP.begalt_offset = begalt - bufp->buffer;
3166 COMPILE_STACK_TOP.fixup_alt_jump
3167 = fixup_alt_jump ? fixup_alt_jump - bufp->buffer + 1 : 0;
3168 COMPILE_STACK_TOP.laststart_offset = b - bufp->buffer;
3169 COMPILE_STACK_TOP.regnum = shy ? -regnum : regnum;
3171 /* Do not push a
3172 start_memory for groups beyond the last one we can
3173 represent in the compiled pattern. */
3174 if (regnum <= MAX_REGNUM && !shy)
3175 BUF_PUSH_2 (start_memory, regnum);
3177 compile_stack.avail++;
3179 fixup_alt_jump = 0;
3180 laststart = 0;
3181 begalt = b;
3182 /* If we've reached MAX_REGNUM groups, then this open
3183 won't actually generate any code, so we'll have to
3184 clear pending_exact explicitly. */
3185 pending_exact = 0;
3186 break;
3189 case ')':
3190 if (syntax & RE_NO_BK_PARENS) goto normal_backslash;
3192 if (COMPILE_STACK_EMPTY)
3194 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
3195 goto normal_backslash;
3196 else
3197 FREE_STACK_RETURN (REG_ERPAREN);
3200 handle_close:
3201 FIXUP_ALT_JUMP ();
3203 /* See similar code for backslashed left paren above. */
3204 if (COMPILE_STACK_EMPTY)
3206 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
3207 goto normal_char;
3208 else
3209 FREE_STACK_RETURN (REG_ERPAREN);
3212 /* Since we just checked for an empty stack above, this
3213 ``can't happen''. */
3214 assert (compile_stack.avail != 0);
3216 /* We don't just want to restore into `regnum', because
3217 later groups should continue to be numbered higher,
3218 as in `(ab)c(de)' -- the second group is #2. */
3219 regnum_t this_group_regnum;
3221 compile_stack.avail--;
3222 begalt = bufp->buffer + COMPILE_STACK_TOP.begalt_offset;
3223 fixup_alt_jump
3224 = COMPILE_STACK_TOP.fixup_alt_jump
3225 ? bufp->buffer + COMPILE_STACK_TOP.fixup_alt_jump - 1
3226 : 0;
3227 laststart = bufp->buffer + COMPILE_STACK_TOP.laststart_offset;
3228 this_group_regnum = COMPILE_STACK_TOP.regnum;
3229 /* If we've reached MAX_REGNUM groups, then this open
3230 won't actually generate any code, so we'll have to
3231 clear pending_exact explicitly. */
3232 pending_exact = 0;
3234 /* We're at the end of the group, so now we know how many
3235 groups were inside this one. */
3236 if (this_group_regnum <= MAX_REGNUM && this_group_regnum > 0)
3237 BUF_PUSH_2 (stop_memory, this_group_regnum);
3239 break;
3242 case '|': /* `\|'. */
3243 if (syntax & RE_LIMITED_OPS || syntax & RE_NO_BK_VBAR)
3244 goto normal_backslash;
3245 handle_alt:
3246 if (syntax & RE_LIMITED_OPS)
3247 goto normal_char;
3249 /* Insert before the previous alternative a jump which
3250 jumps to this alternative if the former fails. */
3251 GET_BUFFER_SPACE (3);
3252 INSERT_JUMP (on_failure_jump, begalt, b + 6);
3253 pending_exact = 0;
3254 b += 3;
3256 /* The alternative before this one has a jump after it
3257 which gets executed if it gets matched. Adjust that
3258 jump so it will jump to this alternative's analogous
3259 jump (put in below, which in turn will jump to the next
3260 (if any) alternative's such jump, etc.). The last such
3261 jump jumps to the correct final destination. A picture:
3262 _____ _____
3263 | | | |
3264 | v | v
3265 a | b | c
3267 If we are at `b', then fixup_alt_jump right now points to a
3268 three-byte space after `a'. We'll put in the jump, set
3269 fixup_alt_jump to right after `b', and leave behind three
3270 bytes which we'll fill in when we get to after `c'. */
3272 FIXUP_ALT_JUMP ();
3274 /* Mark and leave space for a jump after this alternative,
3275 to be filled in later either by next alternative or
3276 when know we're at the end of a series of alternatives. */
3277 fixup_alt_jump = b;
3278 GET_BUFFER_SPACE (3);
3279 b += 3;
3281 laststart = 0;
3282 begalt = b;
3283 break;
3286 case '{':
3287 /* If \{ is a literal. */
3288 if (!(syntax & RE_INTERVALS)
3289 /* If we're at `\{' and it's not the open-interval
3290 operator. */
3291 || (syntax & RE_NO_BK_BRACES))
3292 goto normal_backslash;
3294 handle_interval:
3296 /* If got here, then the syntax allows intervals. */
3298 /* At least (most) this many matches must be made. */
3299 int lower_bound = 0, upper_bound = -1;
3301 beg_interval = p;
3303 GET_UNSIGNED_NUMBER (lower_bound);
3305 if (c == ',')
3306 GET_UNSIGNED_NUMBER (upper_bound);
3307 else
3308 /* Interval such as `{1}' => match exactly once. */
3309 upper_bound = lower_bound;
3311 if (lower_bound < 0 || upper_bound > RE_DUP_MAX
3312 || (upper_bound >= 0 && lower_bound > upper_bound))
3313 FREE_STACK_RETURN (REG_BADBR);
3315 if (!(syntax & RE_NO_BK_BRACES))
3317 if (c != '\\')
3318 FREE_STACK_RETURN (REG_BADBR);
3319 if (p == pend)
3320 FREE_STACK_RETURN (REG_EESCAPE);
3321 PATFETCH (c);
3324 if (c != '}')
3325 FREE_STACK_RETURN (REG_BADBR);
3327 /* We just parsed a valid interval. */
3329 /* If it's invalid to have no preceding re. */
3330 if (!laststart)
3332 if (syntax & RE_CONTEXT_INVALID_OPS)
3333 FREE_STACK_RETURN (REG_BADRPT);
3334 else if (syntax & RE_CONTEXT_INDEP_OPS)
3335 laststart = b;
3336 else
3337 goto unfetch_interval;
3340 if (upper_bound == 0)
3341 /* If the upper bound is zero, just drop the sub pattern
3342 altogether. */
3343 b = laststart;
3344 else if (lower_bound == 1 && upper_bound == 1)
3345 /* Just match it once: nothing to do here. */
3348 /* Otherwise, we have a nontrivial interval. When
3349 we're all done, the pattern will look like:
3350 set_number_at <jump count> <upper bound>
3351 set_number_at <succeed_n count> <lower bound>
3352 succeed_n <after jump addr> <succeed_n count>
3353 <body of loop>
3354 jump_n <succeed_n addr> <jump count>
3355 (The upper bound and `jump_n' are omitted if
3356 `upper_bound' is 1, though.) */
3357 else
3358 { /* If the upper bound is > 1, we need to insert
3359 more at the end of the loop. */
3360 unsigned int nbytes = (upper_bound < 0 ? 3
3361 : upper_bound > 1 ? 5 : 0);
3362 unsigned int startoffset = 0;
3364 GET_BUFFER_SPACE (20); /* We might use less. */
3366 if (lower_bound == 0)
3368 /* A succeed_n that starts with 0 is really a
3369 a simple on_failure_jump_loop. */
3370 INSERT_JUMP (on_failure_jump_loop, laststart,
3371 b + 3 + nbytes);
3372 b += 3;
3374 else
3376 /* Initialize lower bound of the `succeed_n', even
3377 though it will be set during matching by its
3378 attendant `set_number_at' (inserted next),
3379 because `re_compile_fastmap' needs to know.
3380 Jump to the `jump_n' we might insert below. */
3381 INSERT_JUMP2 (succeed_n, laststart,
3382 b + 5 + nbytes,
3383 lower_bound);
3384 b += 5;
3386 /* Code to initialize the lower bound. Insert
3387 before the `succeed_n'. The `5' is the last two
3388 bytes of this `set_number_at', plus 3 bytes of
3389 the following `succeed_n'. */
3390 insert_op2 (set_number_at, laststart, 5, lower_bound, b);
3391 b += 5;
3392 startoffset += 5;
3395 if (upper_bound < 0)
3397 /* A negative upper bound stands for infinity,
3398 in which case it degenerates to a plain jump. */
3399 STORE_JUMP (jump, b, laststart + startoffset);
3400 b += 3;
3402 else if (upper_bound > 1)
3403 { /* More than one repetition is allowed, so
3404 append a backward jump to the `succeed_n'
3405 that starts this interval.
3407 When we've reached this during matching,
3408 we'll have matched the interval once, so
3409 jump back only `upper_bound - 1' times. */
3410 STORE_JUMP2 (jump_n, b, laststart + startoffset,
3411 upper_bound - 1);
3412 b += 5;
3414 /* The location we want to set is the second
3415 parameter of the `jump_n'; that is `b-2' as
3416 an absolute address. `laststart' will be
3417 the `set_number_at' we're about to insert;
3418 `laststart+3' the number to set, the source
3419 for the relative address. But we are
3420 inserting into the middle of the pattern --
3421 so everything is getting moved up by 5.
3422 Conclusion: (b - 2) - (laststart + 3) + 5,
3423 i.e., b - laststart.
3425 We insert this at the beginning of the loop
3426 so that if we fail during matching, we'll
3427 reinitialize the bounds. */
3428 insert_op2 (set_number_at, laststart, b - laststart,
3429 upper_bound - 1, b);
3430 b += 5;
3433 pending_exact = 0;
3434 beg_interval = NULL;
3436 break;
3438 unfetch_interval:
3439 /* If an invalid interval, match the characters as literals. */
3440 assert (beg_interval);
3441 p = beg_interval;
3442 beg_interval = NULL;
3444 /* normal_char and normal_backslash need `c'. */
3445 c = '{';
3447 if (!(syntax & RE_NO_BK_BRACES))
3449 assert (p > pattern && p[-1] == '\\');
3450 goto normal_backslash;
3452 else
3453 goto normal_char;
3455 #ifdef emacs
3456 /* There is no way to specify the before_dot and after_dot
3457 operators. rms says this is ok. --karl */
3458 case '=':
3459 BUF_PUSH (at_dot);
3460 break;
3462 case 's':
3463 laststart = b;
3464 PATFETCH (c);
3465 BUF_PUSH_2 (syntaxspec, syntax_spec_code[c]);
3466 break;
3468 case 'S':
3469 laststart = b;
3470 PATFETCH (c);
3471 BUF_PUSH_2 (notsyntaxspec, syntax_spec_code[c]);
3472 break;
3474 case 'c':
3475 laststart = b;
3476 PATFETCH (c);
3477 BUF_PUSH_2 (categoryspec, c);
3478 break;
3480 case 'C':
3481 laststart = b;
3482 PATFETCH (c);
3483 BUF_PUSH_2 (notcategoryspec, c);
3484 break;
3485 #endif /* emacs */
3488 case 'w':
3489 if (syntax & RE_NO_GNU_OPS)
3490 goto normal_char;
3491 laststart = b;
3492 BUF_PUSH_2 (syntaxspec, Sword);
3493 break;
3496 case 'W':
3497 if (syntax & RE_NO_GNU_OPS)
3498 goto normal_char;
3499 laststart = b;
3500 BUF_PUSH_2 (notsyntaxspec, Sword);
3501 break;
3504 case '<':
3505 if (syntax & RE_NO_GNU_OPS)
3506 goto normal_char;
3507 BUF_PUSH (wordbeg);
3508 break;
3510 case '>':
3511 if (syntax & RE_NO_GNU_OPS)
3512 goto normal_char;
3513 BUF_PUSH (wordend);
3514 break;
3516 case '_':
3517 if (syntax & RE_NO_GNU_OPS)
3518 goto normal_char;
3519 laststart = b;
3520 PATFETCH (c);
3521 if (c == '<')
3522 BUF_PUSH (symbeg);
3523 else if (c == '>')
3524 BUF_PUSH (symend);
3525 else
3526 FREE_STACK_RETURN (REG_BADPAT);
3527 break;
3529 case 'b':
3530 if (syntax & RE_NO_GNU_OPS)
3531 goto normal_char;
3532 BUF_PUSH (wordbound);
3533 break;
3535 case 'B':
3536 if (syntax & RE_NO_GNU_OPS)
3537 goto normal_char;
3538 BUF_PUSH (notwordbound);
3539 break;
3541 case '`':
3542 if (syntax & RE_NO_GNU_OPS)
3543 goto normal_char;
3544 BUF_PUSH (begbuf);
3545 break;
3547 case '\'':
3548 if (syntax & RE_NO_GNU_OPS)
3549 goto normal_char;
3550 BUF_PUSH (endbuf);
3551 break;
3553 case '1': case '2': case '3': case '4': case '5':
3554 case '6': case '7': case '8': case '9':
3556 regnum_t reg;
3558 if (syntax & RE_NO_BK_REFS)
3559 goto normal_backslash;
3561 reg = c - '0';
3563 /* Can't back reference to a subexpression before its end. */
3564 if (reg > regnum || group_in_compile_stack (compile_stack, reg))
3565 FREE_STACK_RETURN (REG_ESUBREG);
3567 laststart = b;
3568 BUF_PUSH_2 (duplicate, reg);
3570 break;
3573 case '+':
3574 case '?':
3575 if (syntax & RE_BK_PLUS_QM)
3576 goto handle_plus;
3577 else
3578 goto normal_backslash;
3580 default:
3581 normal_backslash:
3582 /* You might think it would be useful for \ to mean
3583 not to translate; but if we don't translate it
3584 it will never match anything. */
3585 goto normal_char;
3587 break;
3590 default:
3591 /* Expects the character in `c'. */
3592 normal_char:
3593 /* If no exactn currently being built. */
3594 if (!pending_exact
3596 /* If last exactn not at current position. */
3597 || pending_exact + *pending_exact + 1 != b
3599 /* We have only one byte following the exactn for the count. */
3600 || *pending_exact >= (1 << BYTEWIDTH) - MAX_MULTIBYTE_LENGTH
3602 /* If followed by a repetition operator. */
3603 || (p != pend && (*p == '*' || *p == '^'))
3604 || ((syntax & RE_BK_PLUS_QM)
3605 ? p + 1 < pend && *p == '\\' && (p[1] == '+' || p[1] == '?')
3606 : p != pend && (*p == '+' || *p == '?'))
3607 || ((syntax & RE_INTERVALS)
3608 && ((syntax & RE_NO_BK_BRACES)
3609 ? p != pend && *p == '{'
3610 : p + 1 < pend && p[0] == '\\' && p[1] == '{')))
3612 /* Start building a new exactn. */
3614 laststart = b;
3616 BUF_PUSH_2 (exactn, 0);
3617 pending_exact = b - 1;
3620 GET_BUFFER_SPACE (MAX_MULTIBYTE_LENGTH);
3622 int len;
3624 c = TRANSLATE (c);
3625 if (multibyte)
3626 len = CHAR_STRING (c, b);
3627 else
3628 *b = c, len = 1;
3629 b += len;
3630 (*pending_exact) += len;
3633 break;
3634 } /* switch (c) */
3635 } /* while p != pend */
3638 /* Through the pattern now. */
3640 FIXUP_ALT_JUMP ();
3642 if (!COMPILE_STACK_EMPTY)
3643 FREE_STACK_RETURN (REG_EPAREN);
3645 /* If we don't want backtracking, force success
3646 the first time we reach the end of the compiled pattern. */
3647 if (syntax & RE_NO_POSIX_BACKTRACKING)
3648 BUF_PUSH (succeed);
3650 /* We have succeeded; set the length of the buffer. */
3651 bufp->used = b - bufp->buffer;
3653 #ifdef DEBUG
3654 if (debug > 0)
3656 re_compile_fastmap (bufp);
3657 DEBUG_PRINT1 ("\nCompiled pattern: \n");
3658 print_compiled_pattern (bufp);
3660 debug--;
3661 #endif /* DEBUG */
3663 #ifndef MATCH_MAY_ALLOCATE
3664 /* Initialize the failure stack to the largest possible stack. This
3665 isn't necessary unless we're trying to avoid calling alloca in
3666 the search and match routines. */
3668 int num_regs = bufp->re_nsub + 1;
3670 if (fail_stack.size < re_max_failures * TYPICAL_FAILURE_SIZE)
3672 fail_stack.size = re_max_failures * TYPICAL_FAILURE_SIZE;
3674 if (! fail_stack.stack)
3675 fail_stack.stack
3676 = (fail_stack_elt_t *) malloc (fail_stack.size
3677 * sizeof (fail_stack_elt_t));
3678 else
3679 fail_stack.stack
3680 = (fail_stack_elt_t *) realloc (fail_stack.stack,
3681 (fail_stack.size
3682 * sizeof (fail_stack_elt_t)));
3685 regex_grow_registers (num_regs);
3687 #endif /* not MATCH_MAY_ALLOCATE */
3689 FREE_STACK_RETURN (REG_NOERROR);
3690 } /* regex_compile */
3692 /* Subroutines for `regex_compile'. */
3694 /* Store OP at LOC followed by two-byte integer parameter ARG. */
3696 static void
3697 store_op1 (op, loc, arg)
3698 re_opcode_t op;
3699 unsigned char *loc;
3700 int arg;
3702 *loc = (unsigned char) op;
3703 STORE_NUMBER (loc + 1, arg);
3707 /* Like `store_op1', but for two two-byte parameters ARG1 and ARG2. */
3709 static void
3710 store_op2 (op, loc, arg1, arg2)
3711 re_opcode_t op;
3712 unsigned char *loc;
3713 int arg1, arg2;
3715 *loc = (unsigned char) op;
3716 STORE_NUMBER (loc + 1, arg1);
3717 STORE_NUMBER (loc + 3, arg2);
3721 /* Copy the bytes from LOC to END to open up three bytes of space at LOC
3722 for OP followed by two-byte integer parameter ARG. */
3724 static void
3725 insert_op1 (op, loc, arg, end)
3726 re_opcode_t op;
3727 unsigned char *loc;
3728 int arg;
3729 unsigned char *end;
3731 register unsigned char *pfrom = end;
3732 register unsigned char *pto = end + 3;
3734 while (pfrom != loc)
3735 *--pto = *--pfrom;
3737 store_op1 (op, loc, arg);
3741 /* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2. */
3743 static void
3744 insert_op2 (op, loc, arg1, arg2, end)
3745 re_opcode_t op;
3746 unsigned char *loc;
3747 int arg1, arg2;
3748 unsigned char *end;
3750 register unsigned char *pfrom = end;
3751 register unsigned char *pto = end + 5;
3753 while (pfrom != loc)
3754 *--pto = *--pfrom;
3756 store_op2 (op, loc, arg1, arg2);
3760 /* P points to just after a ^ in PATTERN. Return true if that ^ comes
3761 after an alternative or a begin-subexpression. We assume there is at
3762 least one character before the ^. */
3764 static boolean
3765 at_begline_loc_p (pattern, p, syntax)
3766 re_char *pattern, *p;
3767 reg_syntax_t syntax;
3769 re_char *prev = p - 2;
3770 boolean prev_prev_backslash = prev > pattern && prev[-1] == '\\';
3772 return
3773 /* After a subexpression? */
3774 (*prev == '(' && (syntax & RE_NO_BK_PARENS || prev_prev_backslash))
3775 /* After an alternative? */
3776 || (*prev == '|' && (syntax & RE_NO_BK_VBAR || prev_prev_backslash))
3777 /* After a shy subexpression? */
3778 || ((syntax & RE_SHY_GROUPS) && prev - 2 >= pattern
3779 && prev[-1] == '?' && prev[-2] == '('
3780 && (syntax & RE_NO_BK_PARENS
3781 || (prev - 3 >= pattern && prev[-3] == '\\')));
3785 /* The dual of at_begline_loc_p. This one is for $. We assume there is
3786 at least one character after the $, i.e., `P < PEND'. */
3788 static boolean
3789 at_endline_loc_p (p, pend, syntax)
3790 re_char *p, *pend;
3791 reg_syntax_t syntax;
3793 re_char *next = p;
3794 boolean next_backslash = *next == '\\';
3795 re_char *next_next = p + 1 < pend ? p + 1 : 0;
3797 return
3798 /* Before a subexpression? */
3799 (syntax & RE_NO_BK_PARENS ? *next == ')'
3800 : next_backslash && next_next && *next_next == ')')
3801 /* Before an alternative? */
3802 || (syntax & RE_NO_BK_VBAR ? *next == '|'
3803 : next_backslash && next_next && *next_next == '|');
3807 /* Returns true if REGNUM is in one of COMPILE_STACK's elements and
3808 false if it's not. */
3810 static boolean
3811 group_in_compile_stack (compile_stack, regnum)
3812 compile_stack_type compile_stack;
3813 regnum_t regnum;
3815 int this_element;
3817 for (this_element = compile_stack.avail - 1;
3818 this_element >= 0;
3819 this_element--)
3820 if (compile_stack.stack[this_element].regnum == regnum)
3821 return true;
3823 return false;
3826 /* analyse_first.
3827 If fastmap is non-NULL, go through the pattern and fill fastmap
3828 with all the possible leading chars. If fastmap is NULL, don't
3829 bother filling it up (obviously) and only return whether the
3830 pattern could potentially match the empty string.
3832 Return 1 if p..pend might match the empty string.
3833 Return 0 if p..pend matches at least one char.
3834 Return -1 if fastmap was not updated accurately. */
3836 static int
3837 analyse_first (p, pend, fastmap, multibyte)
3838 re_char *p, *pend;
3839 char *fastmap;
3840 const int multibyte;
3842 int j, k;
3843 boolean not;
3845 /* If all elements for base leading-codes in fastmap is set, this
3846 flag is set true. */
3847 boolean match_any_multibyte_characters = false;
3849 assert (p);
3851 /* The loop below works as follows:
3852 - It has a working-list kept in the PATTERN_STACK and which basically
3853 starts by only containing a pointer to the first operation.
3854 - If the opcode we're looking at is a match against some set of
3855 chars, then we add those chars to the fastmap and go on to the
3856 next work element from the worklist (done via `break').
3857 - If the opcode is a control operator on the other hand, we either
3858 ignore it (if it's meaningless at this point, such as `start_memory')
3859 or execute it (if it's a jump). If the jump has several destinations
3860 (i.e. `on_failure_jump'), then we push the other destination onto the
3861 worklist.
3862 We guarantee termination by ignoring backward jumps (more or less),
3863 so that `p' is monotonically increasing. More to the point, we
3864 never set `p' (or push) anything `<= p1'. */
3866 while (p < pend)
3868 /* `p1' is used as a marker of how far back a `on_failure_jump'
3869 can go without being ignored. It is normally equal to `p'
3870 (which prevents any backward `on_failure_jump') except right
3871 after a plain `jump', to allow patterns such as:
3872 0: jump 10
3873 3..9: <body>
3874 10: on_failure_jump 3
3875 as used for the *? operator. */
3876 re_char *p1 = p;
3878 switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++))
3880 case succeed:
3881 return 1;
3882 continue;
3884 case duplicate:
3885 /* If the first character has to match a backreference, that means
3886 that the group was empty (since it already matched). Since this
3887 is the only case that interests us here, we can assume that the
3888 backreference must match the empty string. */
3889 p++;
3890 continue;
3893 /* Following are the cases which match a character. These end
3894 with `break'. */
3896 case exactn:
3897 if (fastmap)
3899 int c = RE_STRING_CHAR (p + 1, pend - p);
3900 /* When fast-scanning, the fastmap can be indexed either with
3901 a char (smaller than 256) or with the first byte of
3902 a char's byte sequence. So we have to conservatively add
3903 both to the table. */
3904 if (SINGLE_BYTE_CHAR_P (c))
3905 fastmap[c] = 1;
3906 fastmap[p[1]] = 1;
3908 break;
3911 case anychar:
3912 /* We could put all the chars except for \n (and maybe \0)
3913 but we don't bother since it is generally not worth it. */
3914 if (!fastmap) break;
3915 return -1;
3918 case charset_not:
3919 /* Chars beyond end of bitmap are possible matches.
3920 All the single-byte codes can occur in multibyte buffers.
3921 So any that are not listed in the charset
3922 are possible matches, even in multibyte buffers. */
3923 if (!fastmap) break;
3924 /* We don't need to mark LEADING_CODE_8_BIT_CONTROL specially
3925 because it will automatically be set when needed by virtue of
3926 being larger than the highest char of its charset (0xbf) but
3927 smaller than (1<<BYTEWIDTH). */
3928 for (j = CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH;
3929 j < (1 << BYTEWIDTH); j++)
3930 fastmap[j] = 1;
3931 /* Fallthrough */
3932 case charset:
3933 if (!fastmap) break;
3934 not = (re_opcode_t) *(p - 1) == charset_not;
3935 for (j = CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH - 1, p++;
3936 j >= 0; j--)
3937 if (!!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))) ^ not)
3939 fastmap[j] = 1;
3940 #ifdef emacs
3941 if (j >= 0x80 && j < 0xa0)
3942 fastmap[LEADING_CODE_8_BIT_CONTROL] = 1;
3943 #endif
3946 if ((not && multibyte)
3947 /* Any character set can possibly contain a character
3948 which doesn't match the specified set of characters. */
3949 || (CHARSET_RANGE_TABLE_EXISTS_P (&p[-2])
3950 && CHARSET_RANGE_TABLE_BITS (&p[-2]) != 0))
3951 /* If we can match a character class, we can match
3952 any character set. */
3954 set_fastmap_for_multibyte_characters:
3955 if (match_any_multibyte_characters == false)
3957 for (j = 0x80; j < 0xA0; j++) /* XXX */
3958 if (BASE_LEADING_CODE_P (j))
3959 fastmap[j] = 1;
3960 match_any_multibyte_characters = true;
3964 else if (!not && CHARSET_RANGE_TABLE_EXISTS_P (&p[-2])
3965 && match_any_multibyte_characters == false)
3967 /* Set fastmap[I] 1 where I is a base leading code of each
3968 multibyte character in the range table. */
3969 int c, count;
3971 /* Make P points the range table. `+ 2' is to skip flag
3972 bits for a character class. */
3973 p += CHARSET_BITMAP_SIZE (&p[-2]) + 2;
3975 /* Extract the number of ranges in range table into COUNT. */
3976 EXTRACT_NUMBER_AND_INCR (count, p);
3977 for (; count > 0; count--, p += 2 * 3) /* XXX */
3979 /* Extract the start of each range. */
3980 EXTRACT_CHARACTER (c, p);
3981 j = CHAR_CHARSET (c);
3982 fastmap[CHARSET_LEADING_CODE_BASE (j)] = 1;
3985 break;
3987 case syntaxspec:
3988 case notsyntaxspec:
3989 if (!fastmap) break;
3990 #ifndef emacs
3991 not = (re_opcode_t)p[-1] == notsyntaxspec;
3992 k = *p++;
3993 for (j = 0; j < (1 << BYTEWIDTH); j++)
3994 if ((SYNTAX (j) == (enum syntaxcode) k) ^ not)
3995 fastmap[j] = 1;
3996 break;
3997 #else /* emacs */
3998 /* This match depends on text properties. These end with
3999 aborting optimizations. */
4000 return -1;
4002 case categoryspec:
4003 case notcategoryspec:
4004 if (!fastmap) break;
4005 not = (re_opcode_t)p[-1] == notcategoryspec;
4006 k = *p++;
4007 for (j = 0; j < (1 << BYTEWIDTH); j++)
4008 if ((CHAR_HAS_CATEGORY (j, k)) ^ not)
4009 fastmap[j] = 1;
4011 if (multibyte)
4012 /* Any character set can possibly contain a character
4013 whose category is K (or not). */
4014 goto set_fastmap_for_multibyte_characters;
4015 break;
4017 /* All cases after this match the empty string. These end with
4018 `continue'. */
4020 case before_dot:
4021 case at_dot:
4022 case after_dot:
4023 #endif /* !emacs */
4024 case no_op:
4025 case begline:
4026 case endline:
4027 case begbuf:
4028 case endbuf:
4029 case wordbound:
4030 case notwordbound:
4031 case wordbeg:
4032 case wordend:
4033 case symbeg:
4034 case symend:
4035 continue;
4038 case jump:
4039 EXTRACT_NUMBER_AND_INCR (j, p);
4040 if (j < 0)
4041 /* Backward jumps can only go back to code that we've already
4042 visited. `re_compile' should make sure this is true. */
4043 break;
4044 p += j;
4045 switch (SWITCH_ENUM_CAST ((re_opcode_t) *p))
4047 case on_failure_jump:
4048 case on_failure_keep_string_jump:
4049 case on_failure_jump_loop:
4050 case on_failure_jump_nastyloop:
4051 case on_failure_jump_smart:
4052 p++;
4053 break;
4054 default:
4055 continue;
4057 /* Keep `p1' to allow the `on_failure_jump' we are jumping to
4058 to jump back to "just after here". */
4059 /* Fallthrough */
4061 case on_failure_jump:
4062 case on_failure_keep_string_jump:
4063 case on_failure_jump_nastyloop:
4064 case on_failure_jump_loop:
4065 case on_failure_jump_smart:
4066 EXTRACT_NUMBER_AND_INCR (j, p);
4067 if (p + j <= p1)
4068 ; /* Backward jump to be ignored. */
4069 else
4070 { /* We have to look down both arms.
4071 We first go down the "straight" path so as to minimize
4072 stack usage when going through alternatives. */
4073 int r = analyse_first (p, pend, fastmap, multibyte);
4074 if (r) return r;
4075 p += j;
4077 continue;
4080 case jump_n:
4081 /* This code simply does not properly handle forward jump_n. */
4082 DEBUG_STATEMENT (EXTRACT_NUMBER (j, p); assert (j < 0));
4083 p += 4;
4084 /* jump_n can either jump or fall through. The (backward) jump
4085 case has already been handled, so we only need to look at the
4086 fallthrough case. */
4087 continue;
4089 case succeed_n:
4090 /* If N == 0, it should be an on_failure_jump_loop instead. */
4091 DEBUG_STATEMENT (EXTRACT_NUMBER (j, p + 2); assert (j > 0));
4092 p += 4;
4093 /* We only care about one iteration of the loop, so we don't
4094 need to consider the case where this behaves like an
4095 on_failure_jump. */
4096 continue;
4099 case set_number_at:
4100 p += 4;
4101 continue;
4104 case start_memory:
4105 case stop_memory:
4106 p += 1;
4107 continue;
4110 default:
4111 abort (); /* We have listed all the cases. */
4112 } /* switch *p++ */
4114 /* Getting here means we have found the possible starting
4115 characters for one path of the pattern -- and that the empty
4116 string does not match. We need not follow this path further. */
4117 return 0;
4118 } /* while p */
4120 /* We reached the end without matching anything. */
4121 return 1;
4123 } /* analyse_first */
4125 /* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
4126 BUFP. A fastmap records which of the (1 << BYTEWIDTH) possible
4127 characters can start a string that matches the pattern. This fastmap
4128 is used by re_search to skip quickly over impossible starting points.
4130 Character codes above (1 << BYTEWIDTH) are not represented in the
4131 fastmap, but the leading codes are represented. Thus, the fastmap
4132 indicates which character sets could start a match.
4134 The caller must supply the address of a (1 << BYTEWIDTH)-byte data
4135 area as BUFP->fastmap.
4137 We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in
4138 the pattern buffer.
4140 Returns 0 if we succeed, -2 if an internal error. */
4143 re_compile_fastmap (bufp)
4144 struct re_pattern_buffer *bufp;
4146 char *fastmap = bufp->fastmap;
4147 int analysis;
4149 assert (fastmap && bufp->buffer);
4151 bzero (fastmap, 1 << BYTEWIDTH); /* Assume nothing's valid. */
4152 bufp->fastmap_accurate = 1; /* It will be when we're done. */
4154 analysis = analyse_first (bufp->buffer, bufp->buffer + bufp->used,
4155 fastmap, RE_MULTIBYTE_P (bufp));
4156 bufp->can_be_null = (analysis != 0);
4157 return 0;
4158 } /* re_compile_fastmap */
4160 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
4161 ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
4162 this memory for recording register information. STARTS and ENDS
4163 must be allocated using the malloc library routine, and must each
4164 be at least NUM_REGS * sizeof (regoff_t) bytes long.
4166 If NUM_REGS == 0, then subsequent matches should allocate their own
4167 register data.
4169 Unless this function is called, the first search or match using
4170 PATTERN_BUFFER will allocate its own register data, without
4171 freeing the old data. */
4173 void
4174 re_set_registers (bufp, regs, num_regs, starts, ends)
4175 struct re_pattern_buffer *bufp;
4176 struct re_registers *regs;
4177 unsigned num_regs;
4178 regoff_t *starts, *ends;
4180 if (num_regs)
4182 bufp->regs_allocated = REGS_REALLOCATE;
4183 regs->num_regs = num_regs;
4184 regs->start = starts;
4185 regs->end = ends;
4187 else
4189 bufp->regs_allocated = REGS_UNALLOCATED;
4190 regs->num_regs = 0;
4191 regs->start = regs->end = (regoff_t *) 0;
4194 WEAK_ALIAS (__re_set_registers, re_set_registers)
4196 /* Searching routines. */
4198 /* Like re_search_2, below, but only one string is specified, and
4199 doesn't let you say where to stop matching. */
4202 re_search (bufp, string, size, startpos, range, regs)
4203 struct re_pattern_buffer *bufp;
4204 const char *string;
4205 int size, startpos, range;
4206 struct re_registers *regs;
4208 return re_search_2 (bufp, NULL, 0, string, size, startpos, range,
4209 regs, size);
4211 WEAK_ALIAS (__re_search, re_search)
4213 /* Head address of virtual concatenation of string. */
4214 #define HEAD_ADDR_VSTRING(P) \
4215 (((P) >= size1 ? string2 : string1))
4217 /* End address of virtual concatenation of string. */
4218 #define STOP_ADDR_VSTRING(P) \
4219 (((P) >= size1 ? string2 + size2 : string1 + size1))
4221 /* Address of POS in the concatenation of virtual string. */
4222 #define POS_ADDR_VSTRING(POS) \
4223 (((POS) >= size1 ? string2 - size1 : string1) + (POS))
4225 /* Using the compiled pattern in BUFP->buffer, first tries to match the
4226 virtual concatenation of STRING1 and STRING2, starting first at index
4227 STARTPOS, then at STARTPOS + 1, and so on.
4229 STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
4231 RANGE is how far to scan while trying to match. RANGE = 0 means try
4232 only at STARTPOS; in general, the last start tried is STARTPOS +
4233 RANGE.
4235 In REGS, return the indices of the virtual concatenation of STRING1
4236 and STRING2 that matched the entire BUFP->buffer and its contained
4237 subexpressions.
4239 Do not consider matching one past the index STOP in the virtual
4240 concatenation of STRING1 and STRING2.
4242 We return either the position in the strings at which the match was
4243 found, -1 if no match, or -2 if error (such as failure
4244 stack overflow). */
4247 re_search_2 (bufp, str1, size1, str2, size2, startpos, range, regs, stop)
4248 struct re_pattern_buffer *bufp;
4249 const char *str1, *str2;
4250 int size1, size2;
4251 int startpos;
4252 int range;
4253 struct re_registers *regs;
4254 int stop;
4256 int val;
4257 re_char *string1 = (re_char*) str1;
4258 re_char *string2 = (re_char*) str2;
4259 register char *fastmap = bufp->fastmap;
4260 register RE_TRANSLATE_TYPE translate = bufp->translate;
4261 int total_size = size1 + size2;
4262 int endpos = startpos + range;
4263 boolean anchored_start;
4265 /* Nonzero if we have to concern multibyte character. */
4266 const boolean multibyte = RE_MULTIBYTE_P (bufp);
4268 /* Check for out-of-range STARTPOS. */
4269 if (startpos < 0 || startpos > total_size)
4270 return -1;
4272 /* Fix up RANGE if it might eventually take us outside
4273 the virtual concatenation of STRING1 and STRING2.
4274 Make sure we won't move STARTPOS below 0 or above TOTAL_SIZE. */
4275 if (endpos < 0)
4276 range = 0 - startpos;
4277 else if (endpos > total_size)
4278 range = total_size - startpos;
4280 /* If the search isn't to be a backwards one, don't waste time in a
4281 search for a pattern anchored at beginning of buffer. */
4282 if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == begbuf && range > 0)
4284 if (startpos > 0)
4285 return -1;
4286 else
4287 range = 0;
4290 #ifdef emacs
4291 /* In a forward search for something that starts with \=.
4292 don't keep searching past point. */
4293 if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == at_dot && range > 0)
4295 range = PT_BYTE - BEGV_BYTE - startpos;
4296 if (range < 0)
4297 return -1;
4299 #endif /* emacs */
4301 /* Update the fastmap now if not correct already. */
4302 if (fastmap && !bufp->fastmap_accurate)
4303 re_compile_fastmap (bufp);
4305 /* See whether the pattern is anchored. */
4306 anchored_start = (bufp->buffer[0] == begline);
4308 #ifdef emacs
4309 gl_state.object = re_match_object;
4311 int charpos = SYNTAX_TABLE_BYTE_TO_CHAR (POS_AS_IN_BUFFER (startpos));
4313 SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object, charpos, 1);
4315 #endif
4317 /* Loop through the string, looking for a place to start matching. */
4318 for (;;)
4320 /* If the pattern is anchored,
4321 skip quickly past places we cannot match.
4322 We don't bother to treat startpos == 0 specially
4323 because that case doesn't repeat. */
4324 if (anchored_start && startpos > 0)
4326 if (! ((startpos <= size1 ? string1[startpos - 1]
4327 : string2[startpos - size1 - 1])
4328 == '\n'))
4329 goto advance;
4332 /* If a fastmap is supplied, skip quickly over characters that
4333 cannot be the start of a match. If the pattern can match the
4334 null string, however, we don't need to skip characters; we want
4335 the first null string. */
4336 if (fastmap && startpos < total_size && !bufp->can_be_null)
4338 register re_char *d;
4339 register re_wchar_t buf_ch;
4341 d = POS_ADDR_VSTRING (startpos);
4343 if (range > 0) /* Searching forwards. */
4345 register int lim = 0;
4346 int irange = range;
4348 if (startpos < size1 && startpos + range >= size1)
4349 lim = range - (size1 - startpos);
4351 /* Written out as an if-else to avoid testing `translate'
4352 inside the loop. */
4353 if (RE_TRANSLATE_P (translate))
4355 if (multibyte)
4356 while (range > lim)
4358 int buf_charlen;
4360 buf_ch = STRING_CHAR_AND_LENGTH (d, range - lim,
4361 buf_charlen);
4363 buf_ch = RE_TRANSLATE (translate, buf_ch);
4364 if (buf_ch >= 0400
4365 || fastmap[buf_ch])
4366 break;
4368 range -= buf_charlen;
4369 d += buf_charlen;
4371 else
4373 /* Convert *d to integer to shut up GCC's
4374 whining about comparison that is always
4375 true. */
4376 int di = *d;
4378 while (range > lim
4379 && !fastmap[RE_TRANSLATE (translate, di)])
4381 di = *(++d);
4382 range--;
4386 else
4389 re_char *d_start = d;
4390 while (range > lim && !fastmap[*d])
4392 d++;
4393 range--;
4395 #ifdef emacs
4396 if (multibyte && range > lim)
4398 /* Check that we are at the beginning of a char. */
4399 int at_boundary;
4400 AT_CHAR_BOUNDARY_P (at_boundary, d, d_start);
4401 if (at_boundary)
4402 break;
4403 else
4404 { /* We have matched an internal byte of a char
4405 rather than the leading byte, so it's a false
4406 positive: we should keep scanning. */
4407 d++; range--;
4410 else
4411 #endif
4412 break;
4413 } while (1);
4415 startpos += irange - range;
4417 else /* Searching backwards. */
4419 int room = (startpos >= size1
4420 ? size2 + size1 - startpos
4421 : size1 - startpos);
4422 buf_ch = RE_STRING_CHAR (d, room);
4423 buf_ch = TRANSLATE (buf_ch);
4425 if (! (buf_ch >= 0400
4426 || fastmap[buf_ch]))
4427 goto advance;
4431 /* If can't match the null string, and that's all we have left, fail. */
4432 if (range >= 0 && startpos == total_size && fastmap
4433 && !bufp->can_be_null)
4434 return -1;
4436 val = re_match_2_internal (bufp, string1, size1, string2, size2,
4437 startpos, regs, stop);
4438 #ifndef REGEX_MALLOC
4439 # ifdef C_ALLOCA
4440 alloca (0);
4441 # endif
4442 #endif
4444 if (val >= 0)
4445 return startpos;
4447 if (val == -2)
4448 return -2;
4450 advance:
4451 if (!range)
4452 break;
4453 else if (range > 0)
4455 /* Update STARTPOS to the next character boundary. */
4456 if (multibyte)
4458 re_char *p = POS_ADDR_VSTRING (startpos);
4459 re_char *pend = STOP_ADDR_VSTRING (startpos);
4460 int len = MULTIBYTE_FORM_LENGTH (p, pend - p);
4462 range -= len;
4463 if (range < 0)
4464 break;
4465 startpos += len;
4467 else
4469 range--;
4470 startpos++;
4473 else
4475 range++;
4476 startpos--;
4478 /* Update STARTPOS to the previous character boundary. */
4479 if (multibyte)
4481 re_char *p = POS_ADDR_VSTRING (startpos) + 1;
4482 re_char *p0 = p;
4483 re_char *phead = HEAD_ADDR_VSTRING (startpos);
4485 /* Find the head of multibyte form. */
4486 PREV_CHAR_BOUNDARY (p, phead);
4487 range += p0 - 1 - p;
4488 if (range > 0)
4489 break;
4491 startpos -= p0 - 1 - p;
4495 return -1;
4496 } /* re_search_2 */
4497 WEAK_ALIAS (__re_search_2, re_search_2)
4499 /* Declarations and macros for re_match_2. */
4501 static int bcmp_translate _RE_ARGS((re_char *s1, re_char *s2,
4502 register int len,
4503 RE_TRANSLATE_TYPE translate,
4504 const int multibyte));
4506 /* This converts PTR, a pointer into one of the search strings `string1'
4507 and `string2' into an offset from the beginning of that string. */
4508 #define POINTER_TO_OFFSET(ptr) \
4509 (FIRST_STRING_P (ptr) \
4510 ? ((regoff_t) ((ptr) - string1)) \
4511 : ((regoff_t) ((ptr) - string2 + size1)))
4513 /* Call before fetching a character with *d. This switches over to
4514 string2 if necessary.
4515 Check re_match_2_internal for a discussion of why end_match_2 might
4516 not be within string2 (but be equal to end_match_1 instead). */
4517 #define PREFETCH() \
4518 while (d == dend) \
4520 /* End of string2 => fail. */ \
4521 if (dend == end_match_2) \
4522 goto fail; \
4523 /* End of string1 => advance to string2. */ \
4524 d = string2; \
4525 dend = end_match_2; \
4528 /* Call before fetching a char with *d if you already checked other limits.
4529 This is meant for use in lookahead operations like wordend, etc..
4530 where we might need to look at parts of the string that might be
4531 outside of the LIMITs (i.e past `stop'). */
4532 #define PREFETCH_NOLIMIT() \
4533 if (d == end1) \
4535 d = string2; \
4536 dend = end_match_2; \
4539 /* Test if at very beginning or at very end of the virtual concatenation
4540 of `string1' and `string2'. If only one string, it's `string2'. */
4541 #define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2)
4542 #define AT_STRINGS_END(d) ((d) == end2)
4545 /* Test if D points to a character which is word-constituent. We have
4546 two special cases to check for: if past the end of string1, look at
4547 the first character in string2; and if before the beginning of
4548 string2, look at the last character in string1. */
4549 #define WORDCHAR_P(d) \
4550 (SYNTAX ((d) == end1 ? *string2 \
4551 : (d) == string2 - 1 ? *(end1 - 1) : *(d)) \
4552 == Sword)
4554 /* Disabled due to a compiler bug -- see comment at case wordbound */
4556 /* The comment at case wordbound is following one, but we don't use
4557 AT_WORD_BOUNDARY anymore to support multibyte form.
4559 The DEC Alpha C compiler 3.x generates incorrect code for the
4560 test WORDCHAR_P (d - 1) != WORDCHAR_P (d) in the expansion of
4561 AT_WORD_BOUNDARY, so this code is disabled. Expanding the
4562 macro and introducing temporary variables works around the bug. */
4564 #if 0
4565 /* Test if the character before D and the one at D differ with respect
4566 to being word-constituent. */
4567 #define AT_WORD_BOUNDARY(d) \
4568 (AT_STRINGS_BEG (d) || AT_STRINGS_END (d) \
4569 || WORDCHAR_P (d - 1) != WORDCHAR_P (d))
4570 #endif
4572 /* Free everything we malloc. */
4573 #ifdef MATCH_MAY_ALLOCATE
4574 # define FREE_VAR(var) if (var) { REGEX_FREE (var); var = NULL; } else
4575 # define FREE_VARIABLES() \
4576 do { \
4577 REGEX_FREE_STACK (fail_stack.stack); \
4578 FREE_VAR (regstart); \
4579 FREE_VAR (regend); \
4580 FREE_VAR (best_regstart); \
4581 FREE_VAR (best_regend); \
4582 } while (0)
4583 #else
4584 # define FREE_VARIABLES() ((void)0) /* Do nothing! But inhibit gcc warning. */
4585 #endif /* not MATCH_MAY_ALLOCATE */
4588 /* Optimization routines. */
4590 /* If the operation is a match against one or more chars,
4591 return a pointer to the next operation, else return NULL. */
4592 static re_char *
4593 skip_one_char (p)
4594 re_char *p;
4596 switch (SWITCH_ENUM_CAST (*p++))
4598 case anychar:
4599 break;
4601 case exactn:
4602 p += *p + 1;
4603 break;
4605 case charset_not:
4606 case charset:
4607 if (CHARSET_RANGE_TABLE_EXISTS_P (p - 1))
4609 int mcnt;
4610 p = CHARSET_RANGE_TABLE (p - 1);
4611 EXTRACT_NUMBER_AND_INCR (mcnt, p);
4612 p = CHARSET_RANGE_TABLE_END (p, mcnt);
4614 else
4615 p += 1 + CHARSET_BITMAP_SIZE (p - 1);
4616 break;
4618 case syntaxspec:
4619 case notsyntaxspec:
4620 #ifdef emacs
4621 case categoryspec:
4622 case notcategoryspec:
4623 #endif /* emacs */
4624 p++;
4625 break;
4627 default:
4628 p = NULL;
4630 return p;
4634 /* Jump over non-matching operations. */
4635 static re_char *
4636 skip_noops (p, pend)
4637 re_char *p, *pend;
4639 int mcnt;
4640 while (p < pend)
4642 switch (SWITCH_ENUM_CAST ((re_opcode_t) *p))
4644 case start_memory:
4645 case stop_memory:
4646 p += 2; break;
4647 case no_op:
4648 p += 1; break;
4649 case jump:
4650 p += 1;
4651 EXTRACT_NUMBER_AND_INCR (mcnt, p);
4652 p += mcnt;
4653 break;
4654 default:
4655 return p;
4658 assert (p == pend);
4659 return p;
4662 /* Non-zero if "p1 matches something" implies "p2 fails". */
4663 static int
4664 mutually_exclusive_p (bufp, p1, p2)
4665 struct re_pattern_buffer *bufp;
4666 re_char *p1, *p2;
4668 re_opcode_t op2;
4669 const boolean multibyte = RE_MULTIBYTE_P (bufp);
4670 unsigned char *pend = bufp->buffer + bufp->used;
4672 assert (p1 >= bufp->buffer && p1 < pend
4673 && p2 >= bufp->buffer && p2 <= pend);
4675 /* Skip over open/close-group commands.
4676 If what follows this loop is a ...+ construct,
4677 look at what begins its body, since we will have to
4678 match at least one of that. */
4679 p2 = skip_noops (p2, pend);
4680 /* The same skip can be done for p1, except that this function
4681 is only used in the case where p1 is a simple match operator. */
4682 /* p1 = skip_noops (p1, pend); */
4684 assert (p1 >= bufp->buffer && p1 < pend
4685 && p2 >= bufp->buffer && p2 <= pend);
4687 op2 = p2 == pend ? succeed : *p2;
4689 switch (SWITCH_ENUM_CAST (op2))
4691 case succeed:
4692 case endbuf:
4693 /* If we're at the end of the pattern, we can change. */
4694 if (skip_one_char (p1))
4696 DEBUG_PRINT1 (" End of pattern: fast loop.\n");
4697 return 1;
4699 break;
4701 case endline:
4702 case exactn:
4704 register re_wchar_t c
4705 = (re_opcode_t) *p2 == endline ? '\n'
4706 : RE_STRING_CHAR (p2 + 2, pend - p2 - 2);
4708 if ((re_opcode_t) *p1 == exactn)
4710 if (c != RE_STRING_CHAR (p1 + 2, pend - p1 - 2))
4712 DEBUG_PRINT3 (" '%c' != '%c' => fast loop.\n", c, p1[2]);
4713 return 1;
4717 else if ((re_opcode_t) *p1 == charset
4718 || (re_opcode_t) *p1 == charset_not)
4720 int not = (re_opcode_t) *p1 == charset_not;
4722 /* Test if C is listed in charset (or charset_not)
4723 at `p1'. */
4724 if (SINGLE_BYTE_CHAR_P (c))
4726 if (c < CHARSET_BITMAP_SIZE (p1) * BYTEWIDTH
4727 && p1[2 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
4728 not = !not;
4730 else if (CHARSET_RANGE_TABLE_EXISTS_P (p1))
4731 CHARSET_LOOKUP_RANGE_TABLE (not, c, p1);
4733 /* `not' is equal to 1 if c would match, which means
4734 that we can't change to pop_failure_jump. */
4735 if (!not)
4737 DEBUG_PRINT1 (" No match => fast loop.\n");
4738 return 1;
4741 else if ((re_opcode_t) *p1 == anychar
4742 && c == '\n')
4744 DEBUG_PRINT1 (" . != \\n => fast loop.\n");
4745 return 1;
4748 break;
4750 case charset:
4752 if ((re_opcode_t) *p1 == exactn)
4753 /* Reuse the code above. */
4754 return mutually_exclusive_p (bufp, p2, p1);
4756 /* It is hard to list up all the character in charset
4757 P2 if it includes multibyte character. Give up in
4758 such case. */
4759 else if (!multibyte || !CHARSET_RANGE_TABLE_EXISTS_P (p2))
4761 /* Now, we are sure that P2 has no range table.
4762 So, for the size of bitmap in P2, `p2[1]' is
4763 enough. But P1 may have range table, so the
4764 size of bitmap table of P1 is extracted by
4765 using macro `CHARSET_BITMAP_SIZE'.
4767 Since we know that all the character listed in
4768 P2 is ASCII, it is enough to test only bitmap
4769 table of P1. */
4771 if ((re_opcode_t) *p1 == charset)
4773 int idx;
4774 /* We win if the charset inside the loop
4775 has no overlap with the one after the loop. */
4776 for (idx = 0;
4777 (idx < (int) p2[1]
4778 && idx < CHARSET_BITMAP_SIZE (p1));
4779 idx++)
4780 if ((p2[2 + idx] & p1[2 + idx]) != 0)
4781 break;
4783 if (idx == p2[1]
4784 || idx == CHARSET_BITMAP_SIZE (p1))
4786 DEBUG_PRINT1 (" No match => fast loop.\n");
4787 return 1;
4790 else if ((re_opcode_t) *p1 == charset_not)
4792 int idx;
4793 /* We win if the charset_not inside the loop lists
4794 every character listed in the charset after. */
4795 for (idx = 0; idx < (int) p2[1]; idx++)
4796 if (! (p2[2 + idx] == 0
4797 || (idx < CHARSET_BITMAP_SIZE (p1)
4798 && ((p2[2 + idx] & ~ p1[2 + idx]) == 0))))
4799 break;
4801 if (idx == p2[1])
4803 DEBUG_PRINT1 (" No match => fast loop.\n");
4804 return 1;
4809 break;
4811 case charset_not:
4812 switch (SWITCH_ENUM_CAST (*p1))
4814 case exactn:
4815 case charset:
4816 /* Reuse the code above. */
4817 return mutually_exclusive_p (bufp, p2, p1);
4818 case charset_not:
4819 /* When we have two charset_not, it's very unlikely that
4820 they don't overlap. The union of the two sets of excluded
4821 chars should cover all possible chars, which, as a matter of
4822 fact, is virtually impossible in multibyte buffers. */
4823 break;
4825 break;
4827 case wordend:
4828 return ((re_opcode_t) *p1 == syntaxspec && p1[1] == Sword);
4829 case symend:
4830 return ((re_opcode_t) *p1 == syntaxspec
4831 && (p1[1] == Ssymbol || p1[1] == Sword));
4832 case notsyntaxspec:
4833 return ((re_opcode_t) *p1 == syntaxspec && p1[1] == p2[1]);
4835 case wordbeg:
4836 return ((re_opcode_t) *p1 == notsyntaxspec && p1[1] == Sword);
4837 case symbeg:
4838 return ((re_opcode_t) *p1 == notsyntaxspec
4839 && (p1[1] == Ssymbol || p1[1] == Sword));
4840 case syntaxspec:
4841 return ((re_opcode_t) *p1 == notsyntaxspec && p1[1] == p2[1]);
4843 case wordbound:
4844 return (((re_opcode_t) *p1 == notsyntaxspec
4845 || (re_opcode_t) *p1 == syntaxspec)
4846 && p1[1] == Sword);
4848 #ifdef emacs
4849 case categoryspec:
4850 return ((re_opcode_t) *p1 == notcategoryspec && p1[1] == p2[1]);
4851 case notcategoryspec:
4852 return ((re_opcode_t) *p1 == categoryspec && p1[1] == p2[1]);
4853 #endif /* emacs */
4855 default:
4859 /* Safe default. */
4860 return 0;
4864 /* Matching routines. */
4866 #ifndef emacs /* Emacs never uses this. */
4867 /* re_match is like re_match_2 except it takes only a single string. */
4870 re_match (bufp, string, size, pos, regs)
4871 struct re_pattern_buffer *bufp;
4872 const char *string;
4873 int size, pos;
4874 struct re_registers *regs;
4876 int result = re_match_2_internal (bufp, NULL, 0, (re_char*) string, size,
4877 pos, regs, size);
4878 # if defined C_ALLOCA && !defined REGEX_MALLOC
4879 alloca (0);
4880 # endif
4881 return result;
4883 WEAK_ALIAS (__re_match, re_match)
4884 #endif /* not emacs */
4886 #ifdef emacs
4887 /* In Emacs, this is the string or buffer in which we
4888 are matching. It is used for looking up syntax properties. */
4889 Lisp_Object re_match_object;
4890 #endif
4892 /* re_match_2 matches the compiled pattern in BUFP against the
4893 the (virtual) concatenation of STRING1 and STRING2 (of length SIZE1
4894 and SIZE2, respectively). We start matching at POS, and stop
4895 matching at STOP.
4897 If REGS is non-null and the `no_sub' field of BUFP is nonzero, we
4898 store offsets for the substring each group matched in REGS. See the
4899 documentation for exactly how many groups we fill.
4901 We return -1 if no match, -2 if an internal error (such as the
4902 failure stack overflowing). Otherwise, we return the length of the
4903 matched substring. */
4906 re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
4907 struct re_pattern_buffer *bufp;
4908 const char *string1, *string2;
4909 int size1, size2;
4910 int pos;
4911 struct re_registers *regs;
4912 int stop;
4914 int result;
4916 #ifdef emacs
4917 int charpos;
4918 gl_state.object = re_match_object;
4919 charpos = SYNTAX_TABLE_BYTE_TO_CHAR (POS_AS_IN_BUFFER (pos));
4920 SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object, charpos, 1);
4921 #endif
4923 result = re_match_2_internal (bufp, (re_char*) string1, size1,
4924 (re_char*) string2, size2,
4925 pos, regs, stop);
4926 #if defined C_ALLOCA && !defined REGEX_MALLOC
4927 alloca (0);
4928 #endif
4929 return result;
4931 WEAK_ALIAS (__re_match_2, re_match_2)
4933 /* This is a separate function so that we can force an alloca cleanup
4934 afterwards. */
4935 static int
4936 re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
4937 struct re_pattern_buffer *bufp;
4938 re_char *string1, *string2;
4939 int size1, size2;
4940 int pos;
4941 struct re_registers *regs;
4942 int stop;
4944 /* General temporaries. */
4945 int mcnt;
4946 size_t reg;
4947 boolean not;
4949 /* Just past the end of the corresponding string. */
4950 re_char *end1, *end2;
4952 /* Pointers into string1 and string2, just past the last characters in
4953 each to consider matching. */
4954 re_char *end_match_1, *end_match_2;
4956 /* Where we are in the data, and the end of the current string. */
4957 re_char *d, *dend;
4959 /* Used sometimes to remember where we were before starting matching
4960 an operator so that we can go back in case of failure. This "atomic"
4961 behavior of matching opcodes is indispensable to the correctness
4962 of the on_failure_keep_string_jump optimization. */
4963 re_char *dfail;
4965 /* Where we are in the pattern, and the end of the pattern. */
4966 re_char *p = bufp->buffer;
4967 re_char *pend = p + bufp->used;
4969 /* We use this to map every character in the string. */
4970 RE_TRANSLATE_TYPE translate = bufp->translate;
4972 /* Nonzero if we have to concern multibyte character. */
4973 const boolean multibyte = RE_MULTIBYTE_P (bufp);
4975 /* Failure point stack. Each place that can handle a failure further
4976 down the line pushes a failure point on this stack. It consists of
4977 regstart, and regend for all registers corresponding to
4978 the subexpressions we're currently inside, plus the number of such
4979 registers, and, finally, two char *'s. The first char * is where
4980 to resume scanning the pattern; the second one is where to resume
4981 scanning the strings. */
4982 #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
4983 fail_stack_type fail_stack;
4984 #endif
4985 #ifdef DEBUG
4986 unsigned nfailure_points_pushed = 0, nfailure_points_popped = 0;
4987 #endif
4989 #if defined REL_ALLOC && defined REGEX_MALLOC
4990 /* This holds the pointer to the failure stack, when
4991 it is allocated relocatably. */
4992 fail_stack_elt_t *failure_stack_ptr;
4993 #endif
4995 /* We fill all the registers internally, independent of what we
4996 return, for use in backreferences. The number here includes
4997 an element for register zero. */
4998 size_t num_regs = bufp->re_nsub + 1;
5000 /* Information on the contents of registers. These are pointers into
5001 the input strings; they record just what was matched (on this
5002 attempt) by a subexpression part of the pattern, that is, the
5003 regnum-th regstart pointer points to where in the pattern we began
5004 matching and the regnum-th regend points to right after where we
5005 stopped matching the regnum-th subexpression. (The zeroth register
5006 keeps track of what the whole pattern matches.) */
5007 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
5008 re_char **regstart, **regend;
5009 #endif
5011 /* The following record the register info as found in the above
5012 variables when we find a match better than any we've seen before.
5013 This happens as we backtrack through the failure points, which in
5014 turn happens only if we have not yet matched the entire string. */
5015 unsigned best_regs_set = false;
5016 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
5017 re_char **best_regstart, **best_regend;
5018 #endif
5020 /* Logically, this is `best_regend[0]'. But we don't want to have to
5021 allocate space for that if we're not allocating space for anything
5022 else (see below). Also, we never need info about register 0 for
5023 any of the other register vectors, and it seems rather a kludge to
5024 treat `best_regend' differently than the rest. So we keep track of
5025 the end of the best match so far in a separate variable. We
5026 initialize this to NULL so that when we backtrack the first time
5027 and need to test it, it's not garbage. */
5028 re_char *match_end = NULL;
5030 #ifdef DEBUG
5031 /* Counts the total number of registers pushed. */
5032 unsigned num_regs_pushed = 0;
5033 #endif
5035 DEBUG_PRINT1 ("\n\nEntering re_match_2.\n");
5037 INIT_FAIL_STACK ();
5039 #ifdef MATCH_MAY_ALLOCATE
5040 /* Do not bother to initialize all the register variables if there are
5041 no groups in the pattern, as it takes a fair amount of time. If
5042 there are groups, we include space for register 0 (the whole
5043 pattern), even though we never use it, since it simplifies the
5044 array indexing. We should fix this. */
5045 if (bufp->re_nsub)
5047 regstart = REGEX_TALLOC (num_regs, re_char *);
5048 regend = REGEX_TALLOC (num_regs, re_char *);
5049 best_regstart = REGEX_TALLOC (num_regs, re_char *);
5050 best_regend = REGEX_TALLOC (num_regs, re_char *);
5052 if (!(regstart && regend && best_regstart && best_regend))
5054 FREE_VARIABLES ();
5055 return -2;
5058 else
5060 /* We must initialize all our variables to NULL, so that
5061 `FREE_VARIABLES' doesn't try to free them. */
5062 regstart = regend = best_regstart = best_regend = NULL;
5064 #endif /* MATCH_MAY_ALLOCATE */
5066 /* The starting position is bogus. */
5067 if (pos < 0 || pos > size1 + size2)
5069 FREE_VARIABLES ();
5070 return -1;
5073 /* Initialize subexpression text positions to -1 to mark ones that no
5074 start_memory/stop_memory has been seen for. Also initialize the
5075 register information struct. */
5076 for (reg = 1; reg < num_regs; reg++)
5077 regstart[reg] = regend[reg] = NULL;
5079 /* We move `string1' into `string2' if the latter's empty -- but not if
5080 `string1' is null. */
5081 if (size2 == 0 && string1 != NULL)
5083 string2 = string1;
5084 size2 = size1;
5085 string1 = 0;
5086 size1 = 0;
5088 end1 = string1 + size1;
5089 end2 = string2 + size2;
5091 /* `p' scans through the pattern as `d' scans through the data.
5092 `dend' is the end of the input string that `d' points within. `d'
5093 is advanced into the following input string whenever necessary, but
5094 this happens before fetching; therefore, at the beginning of the
5095 loop, `d' can be pointing at the end of a string, but it cannot
5096 equal `string2'. */
5097 if (pos >= size1)
5099 /* Only match within string2. */
5100 d = string2 + pos - size1;
5101 dend = end_match_2 = string2 + stop - size1;
5102 end_match_1 = end1; /* Just to give it a value. */
5104 else
5106 if (stop < size1)
5108 /* Only match within string1. */
5109 end_match_1 = string1 + stop;
5110 /* BEWARE!
5111 When we reach end_match_1, PREFETCH normally switches to string2.
5112 But in the present case, this means that just doing a PREFETCH
5113 makes us jump from `stop' to `gap' within the string.
5114 What we really want here is for the search to stop as
5115 soon as we hit end_match_1. That's why we set end_match_2
5116 to end_match_1 (since PREFETCH fails as soon as we hit
5117 end_match_2). */
5118 end_match_2 = end_match_1;
5120 else
5121 { /* It's important to use this code when stop == size so that
5122 moving `d' from end1 to string2 will not prevent the d == dend
5123 check from catching the end of string. */
5124 end_match_1 = end1;
5125 end_match_2 = string2 + stop - size1;
5127 d = string1 + pos;
5128 dend = end_match_1;
5131 DEBUG_PRINT1 ("The compiled pattern is: ");
5132 DEBUG_PRINT_COMPILED_PATTERN (bufp, p, pend);
5133 DEBUG_PRINT1 ("The string to match is: `");
5134 DEBUG_PRINT_DOUBLE_STRING (d, string1, size1, string2, size2);
5135 DEBUG_PRINT1 ("'\n");
5137 /* This loops over pattern commands. It exits by returning from the
5138 function if the match is complete, or it drops through if the match
5139 fails at this starting point in the input data. */
5140 for (;;)
5142 DEBUG_PRINT2 ("\n%p: ", p);
5144 if (p == pend)
5145 { /* End of pattern means we might have succeeded. */
5146 DEBUG_PRINT1 ("end of pattern ... ");
5148 /* If we haven't matched the entire string, and we want the
5149 longest match, try backtracking. */
5150 if (d != end_match_2)
5152 /* 1 if this match ends in the same string (string1 or string2)
5153 as the best previous match. */
5154 boolean same_str_p = (FIRST_STRING_P (match_end)
5155 == FIRST_STRING_P (d));
5156 /* 1 if this match is the best seen so far. */
5157 boolean best_match_p;
5159 /* AIX compiler got confused when this was combined
5160 with the previous declaration. */
5161 if (same_str_p)
5162 best_match_p = d > match_end;
5163 else
5164 best_match_p = !FIRST_STRING_P (d);
5166 DEBUG_PRINT1 ("backtracking.\n");
5168 if (!FAIL_STACK_EMPTY ())
5169 { /* More failure points to try. */
5171 /* If exceeds best match so far, save it. */
5172 if (!best_regs_set || best_match_p)
5174 best_regs_set = true;
5175 match_end = d;
5177 DEBUG_PRINT1 ("\nSAVING match as best so far.\n");
5179 for (reg = 1; reg < num_regs; reg++)
5181 best_regstart[reg] = regstart[reg];
5182 best_regend[reg] = regend[reg];
5185 goto fail;
5188 /* If no failure points, don't restore garbage. And if
5189 last match is real best match, don't restore second
5190 best one. */
5191 else if (best_regs_set && !best_match_p)
5193 restore_best_regs:
5194 /* Restore best match. It may happen that `dend ==
5195 end_match_1' while the restored d is in string2.
5196 For example, the pattern `x.*y.*z' against the
5197 strings `x-' and `y-z-', if the two strings are
5198 not consecutive in memory. */
5199 DEBUG_PRINT1 ("Restoring best registers.\n");
5201 d = match_end;
5202 dend = ((d >= string1 && d <= end1)
5203 ? end_match_1 : end_match_2);
5205 for (reg = 1; reg < num_regs; reg++)
5207 regstart[reg] = best_regstart[reg];
5208 regend[reg] = best_regend[reg];
5211 } /* d != end_match_2 */
5213 succeed_label:
5214 DEBUG_PRINT1 ("Accepting match.\n");
5216 /* If caller wants register contents data back, do it. */
5217 if (regs && !bufp->no_sub)
5219 /* Have the register data arrays been allocated? */
5220 if (bufp->regs_allocated == REGS_UNALLOCATED)
5221 { /* No. So allocate them with malloc. We need one
5222 extra element beyond `num_regs' for the `-1' marker
5223 GNU code uses. */
5224 regs->num_regs = MAX (RE_NREGS, num_regs + 1);
5225 regs->start = TALLOC (regs->num_regs, regoff_t);
5226 regs->end = TALLOC (regs->num_regs, regoff_t);
5227 if (regs->start == NULL || regs->end == NULL)
5229 FREE_VARIABLES ();
5230 return -2;
5232 bufp->regs_allocated = REGS_REALLOCATE;
5234 else if (bufp->regs_allocated == REGS_REALLOCATE)
5235 { /* Yes. If we need more elements than were already
5236 allocated, reallocate them. If we need fewer, just
5237 leave it alone. */
5238 if (regs->num_regs < num_regs + 1)
5240 regs->num_regs = num_regs + 1;
5241 RETALLOC (regs->start, regs->num_regs, regoff_t);
5242 RETALLOC (regs->end, regs->num_regs, regoff_t);
5243 if (regs->start == NULL || regs->end == NULL)
5245 FREE_VARIABLES ();
5246 return -2;
5250 else
5252 /* These braces fend off a "empty body in an else-statement"
5253 warning under GCC when assert expands to nothing. */
5254 assert (bufp->regs_allocated == REGS_FIXED);
5257 /* Convert the pointer data in `regstart' and `regend' to
5258 indices. Register zero has to be set differently,
5259 since we haven't kept track of any info for it. */
5260 if (regs->num_regs > 0)
5262 regs->start[0] = pos;
5263 regs->end[0] = POINTER_TO_OFFSET (d);
5266 /* Go through the first `min (num_regs, regs->num_regs)'
5267 registers, since that is all we initialized. */
5268 for (reg = 1; reg < MIN (num_regs, regs->num_regs); reg++)
5270 if (REG_UNSET (regstart[reg]) || REG_UNSET (regend[reg]))
5271 regs->start[reg] = regs->end[reg] = -1;
5272 else
5274 regs->start[reg]
5275 = (regoff_t) POINTER_TO_OFFSET (regstart[reg]);
5276 regs->end[reg]
5277 = (regoff_t) POINTER_TO_OFFSET (regend[reg]);
5281 /* If the regs structure we return has more elements than
5282 were in the pattern, set the extra elements to -1. If
5283 we (re)allocated the registers, this is the case,
5284 because we always allocate enough to have at least one
5285 -1 at the end. */
5286 for (reg = num_regs; reg < regs->num_regs; reg++)
5287 regs->start[reg] = regs->end[reg] = -1;
5288 } /* regs && !bufp->no_sub */
5290 DEBUG_PRINT4 ("%u failure points pushed, %u popped (%u remain).\n",
5291 nfailure_points_pushed, nfailure_points_popped,
5292 nfailure_points_pushed - nfailure_points_popped);
5293 DEBUG_PRINT2 ("%u registers pushed.\n", num_regs_pushed);
5295 mcnt = POINTER_TO_OFFSET (d) - pos;
5297 DEBUG_PRINT2 ("Returning %d from re_match_2.\n", mcnt);
5299 FREE_VARIABLES ();
5300 return mcnt;
5303 /* Otherwise match next pattern command. */
5304 switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++))
5306 /* Ignore these. Used to ignore the n of succeed_n's which
5307 currently have n == 0. */
5308 case no_op:
5309 DEBUG_PRINT1 ("EXECUTING no_op.\n");
5310 break;
5312 case succeed:
5313 DEBUG_PRINT1 ("EXECUTING succeed.\n");
5314 goto succeed_label;
5316 /* Match the next n pattern characters exactly. The following
5317 byte in the pattern defines n, and the n bytes after that
5318 are the characters to match. */
5319 case exactn:
5320 mcnt = *p++;
5321 DEBUG_PRINT2 ("EXECUTING exactn %d.\n", mcnt);
5323 /* Remember the start point to rollback upon failure. */
5324 dfail = d;
5326 /* This is written out as an if-else so we don't waste time
5327 testing `translate' inside the loop. */
5328 if (RE_TRANSLATE_P (translate))
5330 if (multibyte)
5333 int pat_charlen, buf_charlen;
5334 unsigned int pat_ch, buf_ch;
5336 PREFETCH ();
5337 pat_ch = STRING_CHAR_AND_LENGTH (p, pend - p, pat_charlen);
5338 buf_ch = STRING_CHAR_AND_LENGTH (d, dend - d, buf_charlen);
5340 if (RE_TRANSLATE (translate, buf_ch)
5341 != pat_ch)
5343 d = dfail;
5344 goto fail;
5347 p += pat_charlen;
5348 d += buf_charlen;
5349 mcnt -= pat_charlen;
5351 while (mcnt > 0);
5352 else
5355 /* Avoid compiler whining about comparison being
5356 always true. */
5357 int di;
5359 PREFETCH ();
5360 di = *d;
5361 if (RE_TRANSLATE (translate, di) != *p++)
5363 d = dfail;
5364 goto fail;
5366 d++;
5368 while (--mcnt);
5370 else
5374 PREFETCH ();
5375 if (*d++ != *p++)
5377 d = dfail;
5378 goto fail;
5381 while (--mcnt);
5383 break;
5386 /* Match any character except possibly a newline or a null. */
5387 case anychar:
5389 int buf_charlen;
5390 re_wchar_t buf_ch;
5392 DEBUG_PRINT1 ("EXECUTING anychar.\n");
5394 PREFETCH ();
5395 buf_ch = RE_STRING_CHAR_AND_LENGTH (d, dend - d, buf_charlen);
5396 buf_ch = TRANSLATE (buf_ch);
5398 if ((!(bufp->syntax & RE_DOT_NEWLINE)
5399 && buf_ch == '\n')
5400 || ((bufp->syntax & RE_DOT_NOT_NULL)
5401 && buf_ch == '\000'))
5402 goto fail;
5404 DEBUG_PRINT2 (" Matched `%d'.\n", *d);
5405 d += buf_charlen;
5407 break;
5410 case charset:
5411 case charset_not:
5413 register unsigned int c;
5414 boolean not = (re_opcode_t) *(p - 1) == charset_not;
5415 int len;
5417 /* Start of actual range_table, or end of bitmap if there is no
5418 range table. */
5419 re_char *range_table;
5421 /* Nonzero if there is a range table. */
5422 int range_table_exists;
5424 /* Number of ranges of range table. This is not included
5425 in the initial byte-length of the command. */
5426 int count = 0;
5428 DEBUG_PRINT2 ("EXECUTING charset%s.\n", not ? "_not" : "");
5430 range_table_exists = CHARSET_RANGE_TABLE_EXISTS_P (&p[-1]);
5432 if (range_table_exists)
5434 range_table = CHARSET_RANGE_TABLE (&p[-1]); /* Past the bitmap. */
5435 EXTRACT_NUMBER_AND_INCR (count, range_table);
5438 PREFETCH ();
5439 c = RE_STRING_CHAR_AND_LENGTH (d, dend - d, len);
5440 c = TRANSLATE (c); /* The character to match. */
5442 if (SINGLE_BYTE_CHAR_P (c))
5443 { /* Lookup bitmap. */
5444 /* Cast to `unsigned' instead of `unsigned char' in
5445 case the bit list is a full 32 bytes long. */
5446 if (c < (unsigned) (CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH)
5447 && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
5448 not = !not;
5450 #ifdef emacs
5451 else if (range_table_exists)
5453 int class_bits = CHARSET_RANGE_TABLE_BITS (&p[-1]);
5455 if ( (class_bits & BIT_LOWER && ISLOWER (c))
5456 | (class_bits & BIT_MULTIBYTE)
5457 | (class_bits & BIT_PUNCT && ISPUNCT (c))
5458 | (class_bits & BIT_SPACE && ISSPACE (c))
5459 | (class_bits & BIT_UPPER && ISUPPER (c))
5460 | (class_bits & BIT_WORD && ISWORD (c)))
5461 not = !not;
5462 else
5463 CHARSET_LOOKUP_RANGE_TABLE_RAW (not, c, range_table, count);
5465 #endif /* emacs */
5467 if (range_table_exists)
5468 p = CHARSET_RANGE_TABLE_END (range_table, count);
5469 else
5470 p += CHARSET_BITMAP_SIZE (&p[-1]) + 1;
5472 if (!not) goto fail;
5474 d += len;
5475 break;
5479 /* The beginning of a group is represented by start_memory.
5480 The argument is the register number. The text
5481 matched within the group is recorded (in the internal
5482 registers data structure) under the register number. */
5483 case start_memory:
5484 DEBUG_PRINT2 ("EXECUTING start_memory %d:\n", *p);
5486 /* In case we need to undo this operation (via backtracking). */
5487 PUSH_FAILURE_REG ((unsigned int)*p);
5489 regstart[*p] = d;
5490 regend[*p] = NULL; /* probably unnecessary. -sm */
5491 DEBUG_PRINT2 (" regstart: %d\n", POINTER_TO_OFFSET (regstart[*p]));
5493 /* Move past the register number and inner group count. */
5494 p += 1;
5495 break;
5498 /* The stop_memory opcode represents the end of a group. Its
5499 argument is the same as start_memory's: the register number. */
5500 case stop_memory:
5501 DEBUG_PRINT2 ("EXECUTING stop_memory %d:\n", *p);
5503 assert (!REG_UNSET (regstart[*p]));
5504 /* Strictly speaking, there should be code such as:
5506 assert (REG_UNSET (regend[*p]));
5507 PUSH_FAILURE_REGSTOP ((unsigned int)*p);
5509 But the only info to be pushed is regend[*p] and it is known to
5510 be UNSET, so there really isn't anything to push.
5511 Not pushing anything, on the other hand deprives us from the
5512 guarantee that regend[*p] is UNSET since undoing this operation
5513 will not reset its value properly. This is not important since
5514 the value will only be read on the next start_memory or at
5515 the very end and both events can only happen if this stop_memory
5516 is *not* undone. */
5518 regend[*p] = d;
5519 DEBUG_PRINT2 (" regend: %d\n", POINTER_TO_OFFSET (regend[*p]));
5521 /* Move past the register number and the inner group count. */
5522 p += 1;
5523 break;
5526 /* \<digit> has been turned into a `duplicate' command which is
5527 followed by the numeric value of <digit> as the register number. */
5528 case duplicate:
5530 register re_char *d2, *dend2;
5531 int regno = *p++; /* Get which register to match against. */
5532 DEBUG_PRINT2 ("EXECUTING duplicate %d.\n", regno);
5534 /* Can't back reference a group which we've never matched. */
5535 if (REG_UNSET (regstart[regno]) || REG_UNSET (regend[regno]))
5536 goto fail;
5538 /* Where in input to try to start matching. */
5539 d2 = regstart[regno];
5541 /* Remember the start point to rollback upon failure. */
5542 dfail = d;
5544 /* Where to stop matching; if both the place to start and
5545 the place to stop matching are in the same string, then
5546 set to the place to stop, otherwise, for now have to use
5547 the end of the first string. */
5549 dend2 = ((FIRST_STRING_P (regstart[regno])
5550 == FIRST_STRING_P (regend[regno]))
5551 ? regend[regno] : end_match_1);
5552 for (;;)
5554 /* If necessary, advance to next segment in register
5555 contents. */
5556 while (d2 == dend2)
5558 if (dend2 == end_match_2) break;
5559 if (dend2 == regend[regno]) break;
5561 /* End of string1 => advance to string2. */
5562 d2 = string2;
5563 dend2 = regend[regno];
5565 /* At end of register contents => success */
5566 if (d2 == dend2) break;
5568 /* If necessary, advance to next segment in data. */
5569 PREFETCH ();
5571 /* How many characters left in this segment to match. */
5572 mcnt = dend - d;
5574 /* Want how many consecutive characters we can match in
5575 one shot, so, if necessary, adjust the count. */
5576 if (mcnt > dend2 - d2)
5577 mcnt = dend2 - d2;
5579 /* Compare that many; failure if mismatch, else move
5580 past them. */
5581 if (RE_TRANSLATE_P (translate)
5582 ? bcmp_translate (d, d2, mcnt, translate, multibyte)
5583 : memcmp (d, d2, mcnt))
5585 d = dfail;
5586 goto fail;
5588 d += mcnt, d2 += mcnt;
5591 break;
5594 /* begline matches the empty string at the beginning of the string
5595 (unless `not_bol' is set in `bufp'), and after newlines. */
5596 case begline:
5597 DEBUG_PRINT1 ("EXECUTING begline.\n");
5599 if (AT_STRINGS_BEG (d))
5601 if (!bufp->not_bol) break;
5603 else
5605 unsigned char c;
5606 GET_CHAR_BEFORE_2 (c, d, string1, end1, string2, end2);
5607 if (c == '\n')
5608 break;
5610 /* In all other cases, we fail. */
5611 goto fail;
5614 /* endline is the dual of begline. */
5615 case endline:
5616 DEBUG_PRINT1 ("EXECUTING endline.\n");
5618 if (AT_STRINGS_END (d))
5620 if (!bufp->not_eol) break;
5622 else
5624 PREFETCH_NOLIMIT ();
5625 if (*d == '\n')
5626 break;
5628 goto fail;
5631 /* Match at the very beginning of the data. */
5632 case begbuf:
5633 DEBUG_PRINT1 ("EXECUTING begbuf.\n");
5634 if (AT_STRINGS_BEG (d))
5635 break;
5636 goto fail;
5639 /* Match at the very end of the data. */
5640 case endbuf:
5641 DEBUG_PRINT1 ("EXECUTING endbuf.\n");
5642 if (AT_STRINGS_END (d))
5643 break;
5644 goto fail;
5647 /* on_failure_keep_string_jump is used to optimize `.*\n'. It
5648 pushes NULL as the value for the string on the stack. Then
5649 `POP_FAILURE_POINT' will keep the current value for the
5650 string, instead of restoring it. To see why, consider
5651 matching `foo\nbar' against `.*\n'. The .* matches the foo;
5652 then the . fails against the \n. But the next thing we want
5653 to do is match the \n against the \n; if we restored the
5654 string value, we would be back at the foo.
5656 Because this is used only in specific cases, we don't need to
5657 check all the things that `on_failure_jump' does, to make
5658 sure the right things get saved on the stack. Hence we don't
5659 share its code. The only reason to push anything on the
5660 stack at all is that otherwise we would have to change
5661 `anychar's code to do something besides goto fail in this
5662 case; that seems worse than this. */
5663 case on_failure_keep_string_jump:
5664 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5665 DEBUG_PRINT3 ("EXECUTING on_failure_keep_string_jump %d (to %p):\n",
5666 mcnt, p + mcnt);
5668 PUSH_FAILURE_POINT (p - 3, NULL);
5669 break;
5671 /* A nasty loop is introduced by the non-greedy *? and +?.
5672 With such loops, the stack only ever contains one failure point
5673 at a time, so that a plain on_failure_jump_loop kind of
5674 cycle detection cannot work. Worse yet, such a detection
5675 can not only fail to detect a cycle, but it can also wrongly
5676 detect a cycle (between different instantiations of the same
5677 loop).
5678 So the method used for those nasty loops is a little different:
5679 We use a special cycle-detection-stack-frame which is pushed
5680 when the on_failure_jump_nastyloop failure-point is *popped*.
5681 This special frame thus marks the beginning of one iteration
5682 through the loop and we can hence easily check right here
5683 whether something matched between the beginning and the end of
5684 the loop. */
5685 case on_failure_jump_nastyloop:
5686 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5687 DEBUG_PRINT3 ("EXECUTING on_failure_jump_nastyloop %d (to %p):\n",
5688 mcnt, p + mcnt);
5690 assert ((re_opcode_t)p[-4] == no_op);
5692 int cycle = 0;
5693 CHECK_INFINITE_LOOP (p - 4, d);
5694 if (!cycle)
5695 /* If there's a cycle, just continue without pushing
5696 this failure point. The failure point is the "try again"
5697 option, which shouldn't be tried.
5698 We want (x?)*?y\1z to match both xxyz and xxyxz. */
5699 PUSH_FAILURE_POINT (p - 3, d);
5701 break;
5703 /* Simple loop detecting on_failure_jump: just check on the
5704 failure stack if the same spot was already hit earlier. */
5705 case on_failure_jump_loop:
5706 on_failure:
5707 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5708 DEBUG_PRINT3 ("EXECUTING on_failure_jump_loop %d (to %p):\n",
5709 mcnt, p + mcnt);
5711 int cycle = 0;
5712 CHECK_INFINITE_LOOP (p - 3, d);
5713 if (cycle)
5714 /* If there's a cycle, get out of the loop, as if the matching
5715 had failed. We used to just `goto fail' here, but that was
5716 aborting the search a bit too early: we want to keep the
5717 empty-loop-match and keep matching after the loop.
5718 We want (x?)*y\1z to match both xxyz and xxyxz. */
5719 p += mcnt;
5720 else
5721 PUSH_FAILURE_POINT (p - 3, d);
5723 break;
5726 /* Uses of on_failure_jump:
5728 Each alternative starts with an on_failure_jump that points
5729 to the beginning of the next alternative. Each alternative
5730 except the last ends with a jump that in effect jumps past
5731 the rest of the alternatives. (They really jump to the
5732 ending jump of the following alternative, because tensioning
5733 these jumps is a hassle.)
5735 Repeats start with an on_failure_jump that points past both
5736 the repetition text and either the following jump or
5737 pop_failure_jump back to this on_failure_jump. */
5738 case on_failure_jump:
5739 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5740 DEBUG_PRINT3 ("EXECUTING on_failure_jump %d (to %p):\n",
5741 mcnt, p + mcnt);
5743 PUSH_FAILURE_POINT (p -3, d);
5744 break;
5746 /* This operation is used for greedy *.
5747 Compare the beginning of the repeat with what in the
5748 pattern follows its end. If we can establish that there
5749 is nothing that they would both match, i.e., that we
5750 would have to backtrack because of (as in, e.g., `a*a')
5751 then we can use a non-backtracking loop based on
5752 on_failure_keep_string_jump instead of on_failure_jump. */
5753 case on_failure_jump_smart:
5754 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5755 DEBUG_PRINT3 ("EXECUTING on_failure_jump_smart %d (to %p).\n",
5756 mcnt, p + mcnt);
5758 re_char *p1 = p; /* Next operation. */
5759 /* Here, we discard `const', making re_match non-reentrant. */
5760 unsigned char *p2 = (unsigned char*) p + mcnt; /* Jump dest. */
5761 unsigned char *p3 = (unsigned char*) p - 3; /* opcode location. */
5763 p -= 3; /* Reset so that we will re-execute the
5764 instruction once it's been changed. */
5766 EXTRACT_NUMBER (mcnt, p2 - 2);
5768 /* Ensure this is a indeed the trivial kind of loop
5769 we are expecting. */
5770 assert (skip_one_char (p1) == p2 - 3);
5771 assert ((re_opcode_t) p2[-3] == jump && p2 + mcnt == p);
5772 DEBUG_STATEMENT (debug += 2);
5773 if (mutually_exclusive_p (bufp, p1, p2))
5775 /* Use a fast `on_failure_keep_string_jump' loop. */
5776 DEBUG_PRINT1 (" smart exclusive => fast loop.\n");
5777 *p3 = (unsigned char) on_failure_keep_string_jump;
5778 STORE_NUMBER (p2 - 2, mcnt + 3);
5780 else
5782 /* Default to a safe `on_failure_jump' loop. */
5783 DEBUG_PRINT1 (" smart default => slow loop.\n");
5784 *p3 = (unsigned char) on_failure_jump;
5786 DEBUG_STATEMENT (debug -= 2);
5788 break;
5790 /* Unconditionally jump (without popping any failure points). */
5791 case jump:
5792 unconditional_jump:
5793 IMMEDIATE_QUIT_CHECK;
5794 EXTRACT_NUMBER_AND_INCR (mcnt, p); /* Get the amount to jump. */
5795 DEBUG_PRINT2 ("EXECUTING jump %d ", mcnt);
5796 p += mcnt; /* Do the jump. */
5797 DEBUG_PRINT2 ("(to %p).\n", p);
5798 break;
5801 /* Have to succeed matching what follows at least n times.
5802 After that, handle like `on_failure_jump'. */
5803 case succeed_n:
5804 /* Signedness doesn't matter since we only compare MCNT to 0. */
5805 EXTRACT_NUMBER (mcnt, p + 2);
5806 DEBUG_PRINT2 ("EXECUTING succeed_n %d.\n", mcnt);
5808 /* Originally, mcnt is how many times we HAVE to succeed. */
5809 if (mcnt != 0)
5811 /* Here, we discard `const', making re_match non-reentrant. */
5812 unsigned char *p2 = (unsigned char*) p + 2; /* counter loc. */
5813 mcnt--;
5814 p += 4;
5815 PUSH_NUMBER (p2, mcnt);
5817 else
5818 /* The two bytes encoding mcnt == 0 are two no_op opcodes. */
5819 goto on_failure;
5820 break;
5822 case jump_n:
5823 /* Signedness doesn't matter since we only compare MCNT to 0. */
5824 EXTRACT_NUMBER (mcnt, p + 2);
5825 DEBUG_PRINT2 ("EXECUTING jump_n %d.\n", mcnt);
5827 /* Originally, this is how many times we CAN jump. */
5828 if (mcnt != 0)
5830 /* Here, we discard `const', making re_match non-reentrant. */
5831 unsigned char *p2 = (unsigned char*) p + 2; /* counter loc. */
5832 mcnt--;
5833 PUSH_NUMBER (p2, mcnt);
5834 goto unconditional_jump;
5836 /* If don't have to jump any more, skip over the rest of command. */
5837 else
5838 p += 4;
5839 break;
5841 case set_number_at:
5843 unsigned char *p2; /* Location of the counter. */
5844 DEBUG_PRINT1 ("EXECUTING set_number_at.\n");
5846 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5847 /* Here, we discard `const', making re_match non-reentrant. */
5848 p2 = (unsigned char*) p + mcnt;
5849 /* Signedness doesn't matter since we only copy MCNT's bits . */
5850 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5851 DEBUG_PRINT3 (" Setting %p to %d.\n", p2, mcnt);
5852 PUSH_NUMBER (p2, mcnt);
5853 break;
5856 case wordbound:
5857 case notwordbound:
5858 not = (re_opcode_t) *(p - 1) == notwordbound;
5859 DEBUG_PRINT2 ("EXECUTING %swordbound.\n", not?"not":"");
5861 /* We SUCCEED (or FAIL) in one of the following cases: */
5863 /* Case 1: D is at the beginning or the end of string. */
5864 if (AT_STRINGS_BEG (d) || AT_STRINGS_END (d))
5865 not = !not;
5866 else
5868 /* C1 is the character before D, S1 is the syntax of C1, C2
5869 is the character at D, and S2 is the syntax of C2. */
5870 re_wchar_t c1, c2;
5871 int s1, s2;
5872 #ifdef emacs
5873 int offset = PTR_TO_OFFSET (d - 1);
5874 int charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
5875 UPDATE_SYNTAX_TABLE (charpos);
5876 #endif
5877 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
5878 s1 = SYNTAX (c1);
5879 #ifdef emacs
5880 UPDATE_SYNTAX_TABLE_FORWARD (charpos + 1);
5881 #endif
5882 PREFETCH_NOLIMIT ();
5883 c2 = RE_STRING_CHAR (d, dend - d);
5884 s2 = SYNTAX (c2);
5886 if (/* Case 2: Only one of S1 and S2 is Sword. */
5887 ((s1 == Sword) != (s2 == Sword))
5888 /* Case 3: Both of S1 and S2 are Sword, and macro
5889 WORD_BOUNDARY_P (C1, C2) returns nonzero. */
5890 || ((s1 == Sword) && WORD_BOUNDARY_P (c1, c2)))
5891 not = !not;
5893 if (not)
5894 break;
5895 else
5896 goto fail;
5898 case wordbeg:
5899 DEBUG_PRINT1 ("EXECUTING wordbeg.\n");
5901 /* We FAIL in one of the following cases: */
5903 /* Case 1: D is at the end of string. */
5904 if (AT_STRINGS_END (d))
5905 goto fail;
5906 else
5908 /* C1 is the character before D, S1 is the syntax of C1, C2
5909 is the character at D, and S2 is the syntax of C2. */
5910 re_wchar_t c1, c2;
5911 int s1, s2;
5912 #ifdef emacs
5913 int offset = PTR_TO_OFFSET (d);
5914 int charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
5915 UPDATE_SYNTAX_TABLE (charpos);
5916 #endif
5917 PREFETCH ();
5918 c2 = RE_STRING_CHAR (d, dend - d);
5919 s2 = SYNTAX (c2);
5921 /* Case 2: S2 is not Sword. */
5922 if (s2 != Sword)
5923 goto fail;
5925 /* Case 3: D is not at the beginning of string ... */
5926 if (!AT_STRINGS_BEG (d))
5928 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
5929 #ifdef emacs
5930 UPDATE_SYNTAX_TABLE_BACKWARD (charpos - 1);
5931 #endif
5932 s1 = SYNTAX (c1);
5934 /* ... and S1 is Sword, and WORD_BOUNDARY_P (C1, C2)
5935 returns 0. */
5936 if ((s1 == Sword) && !WORD_BOUNDARY_P (c1, c2))
5937 goto fail;
5940 break;
5942 case wordend:
5943 DEBUG_PRINT1 ("EXECUTING wordend.\n");
5945 /* We FAIL in one of the following cases: */
5947 /* Case 1: D is at the beginning of string. */
5948 if (AT_STRINGS_BEG (d))
5949 goto fail;
5950 else
5952 /* C1 is the character before D, S1 is the syntax of C1, C2
5953 is the character at D, and S2 is the syntax of C2. */
5954 re_wchar_t c1, c2;
5955 int s1, s2;
5956 #ifdef emacs
5957 int offset = PTR_TO_OFFSET (d) - 1;
5958 int charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
5959 UPDATE_SYNTAX_TABLE (charpos);
5960 #endif
5961 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
5962 s1 = SYNTAX (c1);
5964 /* Case 2: S1 is not Sword. */
5965 if (s1 != Sword)
5966 goto fail;
5968 /* Case 3: D is not at the end of string ... */
5969 if (!AT_STRINGS_END (d))
5971 PREFETCH_NOLIMIT ();
5972 c2 = RE_STRING_CHAR (d, dend - d);
5973 #ifdef emacs
5974 UPDATE_SYNTAX_TABLE_FORWARD (charpos + 1);
5975 #endif
5976 s2 = SYNTAX (c2);
5978 /* ... and S2 is Sword, and WORD_BOUNDARY_P (C1, C2)
5979 returns 0. */
5980 if ((s2 == Sword) && !WORD_BOUNDARY_P (c1, c2))
5981 goto fail;
5984 break;
5986 case symbeg:
5987 DEBUG_PRINT1 ("EXECUTING symbeg.\n");
5989 /* We FAIL in one of the following cases: */
5991 /* Case 1: D is at the end of string. */
5992 if (AT_STRINGS_END (d))
5993 goto fail;
5994 else
5996 /* C1 is the character before D, S1 is the syntax of C1, C2
5997 is the character at D, and S2 is the syntax of C2. */
5998 re_wchar_t c1, c2;
5999 int s1, s2;
6000 #ifdef emacs
6001 int offset = PTR_TO_OFFSET (d);
6002 int charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
6003 UPDATE_SYNTAX_TABLE (charpos);
6004 #endif
6005 PREFETCH ();
6006 c2 = RE_STRING_CHAR (d, dend - d);
6007 s2 = SYNTAX (c2);
6009 /* Case 2: S2 is neither Sword nor Ssymbol. */
6010 if (s2 != Sword && s2 != Ssymbol)
6011 goto fail;
6013 /* Case 3: D is not at the beginning of string ... */
6014 if (!AT_STRINGS_BEG (d))
6016 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
6017 #ifdef emacs
6018 UPDATE_SYNTAX_TABLE_BACKWARD (charpos - 1);
6019 #endif
6020 s1 = SYNTAX (c1);
6022 /* ... and S1 is Sword or Ssymbol. */
6023 if (s1 == Sword || s1 == Ssymbol)
6024 goto fail;
6027 break;
6029 case symend:
6030 DEBUG_PRINT1 ("EXECUTING symend.\n");
6032 /* We FAIL in one of the following cases: */
6034 /* Case 1: D is at the beginning of string. */
6035 if (AT_STRINGS_BEG (d))
6036 goto fail;
6037 else
6039 /* C1 is the character before D, S1 is the syntax of C1, C2
6040 is the character at D, and S2 is the syntax of C2. */
6041 re_wchar_t c1, c2;
6042 int s1, s2;
6043 #ifdef emacs
6044 int offset = PTR_TO_OFFSET (d) - 1;
6045 int charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
6046 UPDATE_SYNTAX_TABLE (charpos);
6047 #endif
6048 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
6049 s1 = SYNTAX (c1);
6051 /* Case 2: S1 is neither Ssymbol nor Sword. */
6052 if (s1 != Sword && s1 != Ssymbol)
6053 goto fail;
6055 /* Case 3: D is not at the end of string ... */
6056 if (!AT_STRINGS_END (d))
6058 PREFETCH_NOLIMIT ();
6059 c2 = RE_STRING_CHAR (d, dend - d);
6060 #ifdef emacs
6061 UPDATE_SYNTAX_TABLE_FORWARD (charpos + 1);
6062 #endif
6063 s2 = SYNTAX (c2);
6065 /* ... and S2 is Sword or Ssymbol. */
6066 if (s2 == Sword || s2 == Ssymbol)
6067 goto fail;
6070 break;
6072 case syntaxspec:
6073 case notsyntaxspec:
6074 not = (re_opcode_t) *(p - 1) == notsyntaxspec;
6075 mcnt = *p++;
6076 DEBUG_PRINT3 ("EXECUTING %ssyntaxspec %d.\n", not?"not":"", mcnt);
6077 PREFETCH ();
6078 #ifdef emacs
6080 int offset = PTR_TO_OFFSET (d);
6081 int pos1 = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
6082 UPDATE_SYNTAX_TABLE (pos1);
6084 #endif
6086 int len;
6087 re_wchar_t c;
6089 c = RE_STRING_CHAR_AND_LENGTH (d, dend - d, len);
6091 if ((SYNTAX (c) != (enum syntaxcode) mcnt) ^ not)
6092 goto fail;
6093 d += len;
6095 break;
6097 #ifdef emacs
6098 case before_dot:
6099 DEBUG_PRINT1 ("EXECUTING before_dot.\n");
6100 if (PTR_BYTE_POS (d) >= PT_BYTE)
6101 goto fail;
6102 break;
6104 case at_dot:
6105 DEBUG_PRINT1 ("EXECUTING at_dot.\n");
6106 if (PTR_BYTE_POS (d) != PT_BYTE)
6107 goto fail;
6108 break;
6110 case after_dot:
6111 DEBUG_PRINT1 ("EXECUTING after_dot.\n");
6112 if (PTR_BYTE_POS (d) <= PT_BYTE)
6113 goto fail;
6114 break;
6116 case categoryspec:
6117 case notcategoryspec:
6118 not = (re_opcode_t) *(p - 1) == notcategoryspec;
6119 mcnt = *p++;
6120 DEBUG_PRINT3 ("EXECUTING %scategoryspec %d.\n", not?"not":"", mcnt);
6121 PREFETCH ();
6123 int len;
6124 re_wchar_t c;
6126 c = RE_STRING_CHAR_AND_LENGTH (d, dend - d, len);
6128 if ((!CHAR_HAS_CATEGORY (c, mcnt)) ^ not)
6129 goto fail;
6130 d += len;
6132 break;
6134 #endif /* emacs */
6136 default:
6137 abort ();
6139 continue; /* Successfully executed one pattern command; keep going. */
6142 /* We goto here if a matching operation fails. */
6143 fail:
6144 IMMEDIATE_QUIT_CHECK;
6145 if (!FAIL_STACK_EMPTY ())
6147 re_char *str, *pat;
6148 /* A restart point is known. Restore to that state. */
6149 DEBUG_PRINT1 ("\nFAIL:\n");
6150 POP_FAILURE_POINT (str, pat);
6151 switch (SWITCH_ENUM_CAST ((re_opcode_t) *pat++))
6153 case on_failure_keep_string_jump:
6154 assert (str == NULL);
6155 goto continue_failure_jump;
6157 case on_failure_jump_nastyloop:
6158 assert ((re_opcode_t)pat[-2] == no_op);
6159 PUSH_FAILURE_POINT (pat - 2, str);
6160 /* Fallthrough */
6162 case on_failure_jump_loop:
6163 case on_failure_jump:
6164 case succeed_n:
6165 d = str;
6166 continue_failure_jump:
6167 EXTRACT_NUMBER_AND_INCR (mcnt, pat);
6168 p = pat + mcnt;
6169 break;
6171 case no_op:
6172 /* A special frame used for nastyloops. */
6173 goto fail;
6175 default:
6176 abort();
6179 assert (p >= bufp->buffer && p <= pend);
6181 if (d >= string1 && d <= end1)
6182 dend = end_match_1;
6184 else
6185 break; /* Matching at this starting point really fails. */
6186 } /* for (;;) */
6188 if (best_regs_set)
6189 goto restore_best_regs;
6191 FREE_VARIABLES ();
6193 return -1; /* Failure to match. */
6194 } /* re_match_2 */
6196 /* Subroutine definitions for re_match_2. */
6198 /* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
6199 bytes; nonzero otherwise. */
6201 static int
6202 bcmp_translate (s1, s2, len, translate, multibyte)
6203 re_char *s1, *s2;
6204 register int len;
6205 RE_TRANSLATE_TYPE translate;
6206 const int multibyte;
6208 register re_char *p1 = s1, *p2 = s2;
6209 re_char *p1_end = s1 + len;
6210 re_char *p2_end = s2 + len;
6212 /* FIXME: Checking both p1 and p2 presumes that the two strings might have
6213 different lengths, but relying on a single `len' would break this. -sm */
6214 while (p1 < p1_end && p2 < p2_end)
6216 int p1_charlen, p2_charlen;
6217 re_wchar_t p1_ch, p2_ch;
6219 p1_ch = RE_STRING_CHAR_AND_LENGTH (p1, p1_end - p1, p1_charlen);
6220 p2_ch = RE_STRING_CHAR_AND_LENGTH (p2, p2_end - p2, p2_charlen);
6222 if (RE_TRANSLATE (translate, p1_ch)
6223 != RE_TRANSLATE (translate, p2_ch))
6224 return 1;
6226 p1 += p1_charlen, p2 += p2_charlen;
6229 if (p1 != p1_end || p2 != p2_end)
6230 return 1;
6232 return 0;
6235 /* Entry points for GNU code. */
6237 /* re_compile_pattern is the GNU regular expression compiler: it
6238 compiles PATTERN (of length SIZE) and puts the result in BUFP.
6239 Returns 0 if the pattern was valid, otherwise an error string.
6241 Assumes the `allocated' (and perhaps `buffer') and `translate' fields
6242 are set in BUFP on entry.
6244 We call regex_compile to do the actual compilation. */
6246 const char *
6247 re_compile_pattern (pattern, length, bufp)
6248 const char *pattern;
6249 size_t length;
6250 struct re_pattern_buffer *bufp;
6252 reg_errcode_t ret;
6254 #ifdef emacs
6255 gl_state.current_syntax_table = current_buffer->syntax_table;
6256 #endif
6258 /* GNU code is written to assume at least RE_NREGS registers will be set
6259 (and at least one extra will be -1). */
6260 bufp->regs_allocated = REGS_UNALLOCATED;
6262 /* And GNU code determines whether or not to get register information
6263 by passing null for the REGS argument to re_match, etc., not by
6264 setting no_sub. */
6265 bufp->no_sub = 0;
6267 ret = regex_compile ((re_char*) pattern, length, re_syntax_options, bufp);
6269 if (!ret)
6270 return NULL;
6271 return gettext (re_error_msgid[(int) ret]);
6273 WEAK_ALIAS (__re_compile_pattern, re_compile_pattern)
6275 /* Entry points compatible with 4.2 BSD regex library. We don't define
6276 them unless specifically requested. */
6278 #if defined _REGEX_RE_COMP || defined _LIBC
6280 /* BSD has one and only one pattern buffer. */
6281 static struct re_pattern_buffer re_comp_buf;
6283 char *
6284 # ifdef _LIBC
6285 /* Make these definitions weak in libc, so POSIX programs can redefine
6286 these names if they don't use our functions, and still use
6287 regcomp/regexec below without link errors. */
6288 weak_function
6289 # endif
6290 re_comp (s)
6291 const char *s;
6293 reg_errcode_t ret;
6295 if (!s)
6297 if (!re_comp_buf.buffer)
6298 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6299 return (char *) gettext ("No previous regular expression");
6300 return 0;
6303 if (!re_comp_buf.buffer)
6305 re_comp_buf.buffer = (unsigned char *) malloc (200);
6306 if (re_comp_buf.buffer == NULL)
6307 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6308 return (char *) gettext (re_error_msgid[(int) REG_ESPACE]);
6309 re_comp_buf.allocated = 200;
6311 re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH);
6312 if (re_comp_buf.fastmap == NULL)
6313 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6314 return (char *) gettext (re_error_msgid[(int) REG_ESPACE]);
6317 /* Since `re_exec' always passes NULL for the `regs' argument, we
6318 don't need to initialize the pattern buffer fields which affect it. */
6320 ret = regex_compile (s, strlen (s), re_syntax_options, &re_comp_buf);
6322 if (!ret)
6323 return NULL;
6325 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6326 return (char *) gettext (re_error_msgid[(int) ret]);
6331 # ifdef _LIBC
6332 weak_function
6333 # endif
6334 re_exec (s)
6335 const char *s;
6337 const int len = strlen (s);
6338 return
6339 0 <= re_search (&re_comp_buf, s, len, 0, len, (struct re_registers *) 0);
6341 #endif /* _REGEX_RE_COMP */
6343 /* POSIX.2 functions. Don't define these for Emacs. */
6345 #ifndef emacs
6347 /* regcomp takes a regular expression as a string and compiles it.
6349 PREG is a regex_t *. We do not expect any fields to be initialized,
6350 since POSIX says we shouldn't. Thus, we set
6352 `buffer' to the compiled pattern;
6353 `used' to the length of the compiled pattern;
6354 `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
6355 REG_EXTENDED bit in CFLAGS is set; otherwise, to
6356 RE_SYNTAX_POSIX_BASIC;
6357 `fastmap' to an allocated space for the fastmap;
6358 `fastmap_accurate' to zero;
6359 `re_nsub' to the number of subexpressions in PATTERN.
6361 PATTERN is the address of the pattern string.
6363 CFLAGS is a series of bits which affect compilation.
6365 If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
6366 use POSIX basic syntax.
6368 If REG_NEWLINE is set, then . and [^...] don't match newline.
6369 Also, regexec will try a match beginning after every newline.
6371 If REG_ICASE is set, then we considers upper- and lowercase
6372 versions of letters to be equivalent when matching.
6374 If REG_NOSUB is set, then when PREG is passed to regexec, that
6375 routine will report only success or failure, and nothing about the
6376 registers.
6378 It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
6379 the return codes and their meanings.) */
6382 regcomp (preg, pattern, cflags)
6383 regex_t *__restrict preg;
6384 const char *__restrict pattern;
6385 int cflags;
6387 reg_errcode_t ret;
6388 reg_syntax_t syntax
6389 = (cflags & REG_EXTENDED) ?
6390 RE_SYNTAX_POSIX_EXTENDED : RE_SYNTAX_POSIX_BASIC;
6392 /* regex_compile will allocate the space for the compiled pattern. */
6393 preg->buffer = 0;
6394 preg->allocated = 0;
6395 preg->used = 0;
6397 /* Try to allocate space for the fastmap. */
6398 preg->fastmap = (char *) malloc (1 << BYTEWIDTH);
6400 if (cflags & REG_ICASE)
6402 unsigned i;
6404 preg->translate
6405 = (RE_TRANSLATE_TYPE) malloc (CHAR_SET_SIZE
6406 * sizeof (*(RE_TRANSLATE_TYPE)0));
6407 if (preg->translate == NULL)
6408 return (int) REG_ESPACE;
6410 /* Map uppercase characters to corresponding lowercase ones. */
6411 for (i = 0; i < CHAR_SET_SIZE; i++)
6412 preg->translate[i] = ISUPPER (i) ? TOLOWER (i) : i;
6414 else
6415 preg->translate = NULL;
6417 /* If REG_NEWLINE is set, newlines are treated differently. */
6418 if (cflags & REG_NEWLINE)
6419 { /* REG_NEWLINE implies neither . nor [^...] match newline. */
6420 syntax &= ~RE_DOT_NEWLINE;
6421 syntax |= RE_HAT_LISTS_NOT_NEWLINE;
6423 else
6424 syntax |= RE_NO_NEWLINE_ANCHOR;
6426 preg->no_sub = !!(cflags & REG_NOSUB);
6428 /* POSIX says a null character in the pattern terminates it, so we
6429 can use strlen here in compiling the pattern. */
6430 ret = regex_compile ((re_char*) pattern, strlen (pattern), syntax, preg);
6432 /* POSIX doesn't distinguish between an unmatched open-group and an
6433 unmatched close-group: both are REG_EPAREN. */
6434 if (ret == REG_ERPAREN)
6435 ret = REG_EPAREN;
6437 if (ret == REG_NOERROR && preg->fastmap)
6438 { /* Compute the fastmap now, since regexec cannot modify the pattern
6439 buffer. */
6440 re_compile_fastmap (preg);
6441 if (preg->can_be_null)
6442 { /* The fastmap can't be used anyway. */
6443 free (preg->fastmap);
6444 preg->fastmap = NULL;
6447 return (int) ret;
6449 WEAK_ALIAS (__regcomp, regcomp)
6452 /* regexec searches for a given pattern, specified by PREG, in the
6453 string STRING.
6455 If NMATCH is zero or REG_NOSUB was set in the cflags argument to
6456 `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
6457 least NMATCH elements, and we set them to the offsets of the
6458 corresponding matched substrings.
6460 EFLAGS specifies `execution flags' which affect matching: if
6461 REG_NOTBOL is set, then ^ does not match at the beginning of the
6462 string; if REG_NOTEOL is set, then $ does not match at the end.
6464 We return 0 if we find a match and REG_NOMATCH if not. */
6467 regexec (preg, string, nmatch, pmatch, eflags)
6468 const regex_t *__restrict preg;
6469 const char *__restrict string;
6470 size_t nmatch;
6471 regmatch_t pmatch[__restrict_arr];
6472 int eflags;
6474 int ret;
6475 struct re_registers regs;
6476 regex_t private_preg;
6477 int len = strlen (string);
6478 boolean want_reg_info = !preg->no_sub && nmatch > 0 && pmatch;
6480 private_preg = *preg;
6482 private_preg.not_bol = !!(eflags & REG_NOTBOL);
6483 private_preg.not_eol = !!(eflags & REG_NOTEOL);
6485 /* The user has told us exactly how many registers to return
6486 information about, via `nmatch'. We have to pass that on to the
6487 matching routines. */
6488 private_preg.regs_allocated = REGS_FIXED;
6490 if (want_reg_info)
6492 regs.num_regs = nmatch;
6493 regs.start = TALLOC (nmatch * 2, regoff_t);
6494 if (regs.start == NULL)
6495 return (int) REG_NOMATCH;
6496 regs.end = regs.start + nmatch;
6499 /* Instead of using not_eol to implement REG_NOTEOL, we could simply
6500 pass (&private_preg, string, len + 1, 0, len, ...) pretending the string
6501 was a little bit longer but still only matching the real part.
6502 This works because the `endline' will check for a '\n' and will find a
6503 '\0', correctly deciding that this is not the end of a line.
6504 But it doesn't work out so nicely for REG_NOTBOL, since we don't have
6505 a convenient '\0' there. For all we know, the string could be preceded
6506 by '\n' which would throw things off. */
6508 /* Perform the searching operation. */
6509 ret = re_search (&private_preg, string, len,
6510 /* start: */ 0, /* range: */ len,
6511 want_reg_info ? &regs : (struct re_registers *) 0);
6513 /* Copy the register information to the POSIX structure. */
6514 if (want_reg_info)
6516 if (ret >= 0)
6518 unsigned r;
6520 for (r = 0; r < nmatch; r++)
6522 pmatch[r].rm_so = regs.start[r];
6523 pmatch[r].rm_eo = regs.end[r];
6527 /* If we needed the temporary register info, free the space now. */
6528 free (regs.start);
6531 /* We want zero return to mean success, unlike `re_search'. */
6532 return ret >= 0 ? (int) REG_NOERROR : (int) REG_NOMATCH;
6534 WEAK_ALIAS (__regexec, regexec)
6537 /* Returns a message corresponding to an error code, ERR_CODE, returned
6538 from either regcomp or regexec. We don't use PREG here.
6540 ERR_CODE was previously called ERRCODE, but that name causes an
6541 error with msvc8 compiler. */
6543 size_t
6544 regerror (err_code, preg, errbuf, errbuf_size)
6545 int err_code;
6546 const regex_t *preg;
6547 char *errbuf;
6548 size_t errbuf_size;
6550 const char *msg;
6551 size_t msg_size;
6553 if (err_code < 0
6554 || err_code >= (sizeof (re_error_msgid) / sizeof (re_error_msgid[0])))
6555 /* Only error codes returned by the rest of the code should be passed
6556 to this routine. If we are given anything else, or if other regex
6557 code generates an invalid error code, then the program has a bug.
6558 Dump core so we can fix it. */
6559 abort ();
6561 msg = gettext (re_error_msgid[err_code]);
6563 msg_size = strlen (msg) + 1; /* Includes the null. */
6565 if (errbuf_size != 0)
6567 if (msg_size > errbuf_size)
6569 strncpy (errbuf, msg, errbuf_size - 1);
6570 errbuf[errbuf_size - 1] = 0;
6572 else
6573 strcpy (errbuf, msg);
6576 return msg_size;
6578 WEAK_ALIAS (__regerror, regerror)
6581 /* Free dynamically allocated space used by PREG. */
6583 void
6584 regfree (preg)
6585 regex_t *preg;
6587 if (preg->buffer != NULL)
6588 free (preg->buffer);
6589 preg->buffer = NULL;
6591 preg->allocated = 0;
6592 preg->used = 0;
6594 if (preg->fastmap != NULL)
6595 free (preg->fastmap);
6596 preg->fastmap = NULL;
6597 preg->fastmap_accurate = 0;
6599 if (preg->translate != NULL)
6600 free (preg->translate);
6601 preg->translate = NULL;
6603 WEAK_ALIAS (__regfree, regfree)
6605 #endif /* not emacs */
6607 /* arch-tag: 4ffd68ba-2a9e-435b-a21a-018990f9eeb2
6608 (do not change this comment) */