Merge from origin/emacs-25
[emacs.git] / src / regex.c
blob1917a8480ae4ee263251c8bc3e8eb5068f2fbf95
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-2016 Free Software Foundation, Inc.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* TODO:
21 - structure the opcode space into opcode+flag.
22 - merge with glibc's regex.[ch].
23 - replace (succeed_n + jump_n + set_number_at) with something that doesn't
24 need to modify the compiled regexp so that re_match can be reentrant.
25 - get rid of on_failure_jump_smart by doing the optimization in re_comp
26 rather than at run-time, so that re_match can be reentrant.
29 /* AIX requires this to be the first thing in the file. */
30 #if defined _AIX && !defined REGEX_MALLOC
31 #pragma alloca
32 #endif
34 /* Ignore some GCC warnings for now. This section should go away
35 once the Emacs and Gnulib regex code is merged. */
36 #if 4 < __GNUC__ + (5 <= __GNUC_MINOR__) || defined __clang__
37 # pragma GCC diagnostic ignored "-Wstrict-overflow"
38 # ifndef emacs
39 # pragma GCC diagnostic ignored "-Wunused-function"
40 # pragma GCC diagnostic ignored "-Wunused-macros"
41 # pragma GCC diagnostic ignored "-Wunused-result"
42 # pragma GCC diagnostic ignored "-Wunused-variable"
43 # endif
44 #endif
46 #if 4 < __GNUC__ + (6 <= __GNUC_MINOR__) && ! defined __clang__
47 # pragma GCC diagnostic ignored "-Wunused-but-set-variable"
48 #endif
50 #include <config.h>
52 #include <stddef.h>
53 #include <stdlib.h>
55 #ifdef emacs
56 /* We need this for `regex.h', and perhaps for the Emacs include files. */
57 # include <sys/types.h>
58 #endif
60 /* Whether to use ISO C Amendment 1 wide char functions.
61 Those should not be used for Emacs since it uses its own. */
62 #if defined _LIBC
63 #define WIDE_CHAR_SUPPORT 1
64 #else
65 #define WIDE_CHAR_SUPPORT \
66 (HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_BTOWC && !emacs)
67 #endif
69 /* For platform which support the ISO C amendment 1 functionality we
70 support user defined character classes. */
71 #if WIDE_CHAR_SUPPORT
72 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
73 # include <wchar.h>
74 # include <wctype.h>
75 #endif
77 #ifdef _LIBC
78 /* We have to keep the namespace clean. */
79 # define regfree(preg) __regfree (preg)
80 # define regexec(pr, st, nm, pm, ef) __regexec (pr, st, nm, pm, ef)
81 # define regcomp(preg, pattern, cflags) __regcomp (preg, pattern, cflags)
82 # define regerror(err_code, preg, errbuf, errbuf_size) \
83 __regerror (err_code, preg, errbuf, errbuf_size)
84 # define re_set_registers(bu, re, nu, st, en) \
85 __re_set_registers (bu, re, nu, st, en)
86 # define re_match_2(bufp, string1, size1, string2, size2, pos, regs, stop) \
87 __re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
88 # define re_match(bufp, string, size, pos, regs) \
89 __re_match (bufp, string, size, pos, regs)
90 # define re_search(bufp, string, size, startpos, range, regs) \
91 __re_search (bufp, string, size, startpos, range, regs)
92 # define re_compile_pattern(pattern, length, bufp) \
93 __re_compile_pattern (pattern, length, bufp)
94 # define re_set_syntax(syntax) __re_set_syntax (syntax)
95 # define re_search_2(bufp, st1, s1, st2, s2, startpos, range, regs, stop) \
96 __re_search_2 (bufp, st1, s1, st2, s2, startpos, range, regs, stop)
97 # define re_compile_fastmap(bufp) __re_compile_fastmap (bufp)
99 /* Make sure we call libc's function even if the user overrides them. */
100 # define btowc __btowc
101 # define iswctype __iswctype
102 # define wctype __wctype
104 # define WEAK_ALIAS(a,b) weak_alias (a, b)
106 /* We are also using some library internals. */
107 # include <locale/localeinfo.h>
108 # include <locale/elem-hash.h>
109 # include <langinfo.h>
110 #else
111 # define WEAK_ALIAS(a,b)
112 #endif
114 /* This is for other GNU distributions with internationalized messages. */
115 #if HAVE_LIBINTL_H || defined _LIBC
116 # include <libintl.h>
117 #else
118 # define gettext(msgid) (msgid)
119 #endif
121 #ifndef gettext_noop
122 /* This define is so xgettext can find the internationalizable
123 strings. */
124 # define gettext_noop(String) String
125 #endif
127 /* The `emacs' switch turns on certain matching commands
128 that make sense only in Emacs. */
129 #ifdef emacs
131 # include "lisp.h"
132 # include "character.h"
133 # include "buffer.h"
135 # include "syntax.h"
136 # include "category.h"
138 /* Make syntax table lookup grant data in gl_state. */
139 # define SYNTAX(c) syntax_property (c, 1)
141 # ifdef malloc
142 # undef malloc
143 # endif
144 # define malloc xmalloc
145 # ifdef realloc
146 # undef realloc
147 # endif
148 # define realloc xrealloc
149 # ifdef free
150 # undef free
151 # endif
152 # define free xfree
154 /* Converts the pointer to the char to BEG-based offset from the start. */
155 # define PTR_TO_OFFSET(d) POS_AS_IN_BUFFER (POINTER_TO_OFFSET (d))
156 # define POS_AS_IN_BUFFER(p) ((p) + (NILP (re_match_object) || BUFFERP (re_match_object)))
158 # define RE_MULTIBYTE_P(bufp) ((bufp)->multibyte)
159 # define RE_TARGET_MULTIBYTE_P(bufp) ((bufp)->target_multibyte)
160 # define RE_STRING_CHAR(p, multibyte) \
161 (multibyte ? (STRING_CHAR (p)) : (*(p)))
162 # define RE_STRING_CHAR_AND_LENGTH(p, len, multibyte) \
163 (multibyte ? (STRING_CHAR_AND_LENGTH (p, len)) : ((len) = 1, *(p)))
165 # define RE_CHAR_TO_MULTIBYTE(c) UNIBYTE_TO_CHAR (c)
167 # define RE_CHAR_TO_UNIBYTE(c) CHAR_TO_BYTE_SAFE (c)
169 /* Set C a (possibly converted to multibyte) character before P. P
170 points into a string which is the virtual concatenation of STR1
171 (which ends at END1) or STR2 (which ends at END2). */
172 # define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2) \
173 do { \
174 if (target_multibyte) \
176 re_char *dtemp = (p) == (str2) ? (end1) : (p); \
177 re_char *dlimit = ((p) > (str2) && (p) <= (end2)) ? (str2) : (str1); \
178 while (dtemp-- > dlimit && !CHAR_HEAD_P (*dtemp)); \
179 c = STRING_CHAR (dtemp); \
181 else \
183 (c = ((p) == (str2) ? (end1) : (p))[-1]); \
184 (c) = RE_CHAR_TO_MULTIBYTE (c); \
186 } while (0)
188 /* Set C a (possibly converted to multibyte) character at P, and set
189 LEN to the byte length of that character. */
190 # define GET_CHAR_AFTER(c, p, len) \
191 do { \
192 if (target_multibyte) \
193 (c) = STRING_CHAR_AND_LENGTH (p, len); \
194 else \
196 (c) = *p; \
197 len = 1; \
198 (c) = RE_CHAR_TO_MULTIBYTE (c); \
200 } while (0)
202 #else /* not emacs */
204 /* If we are not linking with Emacs proper,
205 we can't use the relocating allocator
206 even if config.h says that we can. */
207 # undef REL_ALLOC
209 # include <unistd.h>
211 /* When used in Emacs's lib-src, we need xmalloc and xrealloc. */
213 static void *
214 xmalloc (size_t size)
216 void *val = malloc (size);
217 if (!val && size)
219 write (STDERR_FILENO, "virtual memory exhausted\n", 25);
220 exit (1);
222 return val;
225 static void *
226 xrealloc (void *block, size_t size)
228 void *val;
229 /* We must call malloc explicitly when BLOCK is 0, since some
230 reallocs don't do this. */
231 if (! block)
232 val = malloc (size);
233 else
234 val = realloc (block, size);
235 if (!val && size)
237 write (STDERR_FILENO, "virtual memory exhausted\n", 25);
238 exit (1);
240 return val;
243 # ifdef malloc
244 # undef malloc
245 # endif
246 # define malloc xmalloc
247 # ifdef realloc
248 # undef realloc
249 # endif
250 # define realloc xrealloc
252 # include <stdbool.h>
253 # include <string.h>
255 /* Define the syntax stuff for \<, \>, etc. */
257 /* Sword must be nonzero for the wordchar pattern commands in re_match_2. */
258 enum syntaxcode { Swhitespace = 0, Sword = 1, Ssymbol = 2 };
260 /* Dummy macros for non-Emacs environments. */
261 # define MAX_MULTIBYTE_LENGTH 1
262 # define RE_MULTIBYTE_P(x) 0
263 # define RE_TARGET_MULTIBYTE_P(x) 0
264 # define WORD_BOUNDARY_P(c1, c2) (0)
265 # define BYTES_BY_CHAR_HEAD(p) (1)
266 # define PREV_CHAR_BOUNDARY(p, limit) ((p)--)
267 # define STRING_CHAR(p) (*(p))
268 # define RE_STRING_CHAR(p, multibyte) STRING_CHAR (p)
269 # define CHAR_STRING(c, s) (*(s) = (c), 1)
270 # define STRING_CHAR_AND_LENGTH(p, actual_len) ((actual_len) = 1, *(p))
271 # define RE_STRING_CHAR_AND_LENGTH(p, len, multibyte) STRING_CHAR_AND_LENGTH (p, len)
272 # define RE_CHAR_TO_MULTIBYTE(c) (c)
273 # define RE_CHAR_TO_UNIBYTE(c) (c)
274 # define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2) \
275 (c = ((p) == (str2) ? *((end1) - 1) : *((p) - 1)))
276 # define GET_CHAR_AFTER(c, p, len) \
277 (c = *p, len = 1)
278 # define CHAR_BYTE8_P(c) (0)
279 # define CHAR_LEADING_CODE(c) (c)
281 #endif /* not emacs */
283 #ifndef RE_TRANSLATE
284 # define RE_TRANSLATE(TBL, C) ((unsigned char)(TBL)[C])
285 # define RE_TRANSLATE_P(TBL) (TBL)
286 #endif
288 /* Get the interface, including the syntax bits. */
289 #include "regex.h"
291 /* isalpha etc. are used for the character classes. */
292 #include <ctype.h>
294 #ifdef emacs
296 /* 1 if C is an ASCII character. */
297 # define IS_REAL_ASCII(c) ((c) < 0200)
299 /* 1 if C is a unibyte character. */
300 # define ISUNIBYTE(c) (SINGLE_BYTE_CHAR_P ((c)))
302 /* The Emacs definitions should not be directly affected by locales. */
304 /* In Emacs, these are only used for single-byte characters. */
305 # define ISDIGIT(c) ((c) >= '0' && (c) <= '9')
306 # define ISCNTRL(c) ((c) < ' ')
307 # define ISXDIGIT(c) (((c) >= '0' && (c) <= '9') \
308 || ((c) >= 'a' && (c) <= 'f') \
309 || ((c) >= 'A' && (c) <= 'F'))
311 /* This is only used for single-byte characters. */
312 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
314 /* The rest must handle multibyte characters. */
316 # define ISGRAPH(c) (SINGLE_BYTE_CHAR_P (c) \
317 ? (c) > ' ' && !((c) >= 0177 && (c) <= 0240) \
318 : graphicp (c))
320 # define ISPRINT(c) (SINGLE_BYTE_CHAR_P (c) \
321 ? (c) >= ' ' && !((c) >= 0177 && (c) <= 0237) \
322 : printablep (c))
324 # define ISALNUM(c) (IS_REAL_ASCII (c) \
325 ? (((c) >= 'a' && (c) <= 'z') \
326 || ((c) >= 'A' && (c) <= 'Z') \
327 || ((c) >= '0' && (c) <= '9')) \
328 : alphanumericp (c))
330 # define ISALPHA(c) (IS_REAL_ASCII (c) \
331 ? (((c) >= 'a' && (c) <= 'z') \
332 || ((c) >= 'A' && (c) <= 'Z')) \
333 : alphabeticp (c))
335 # define ISLOWER(c) lowercasep (c)
337 # define ISPUNCT(c) (IS_REAL_ASCII (c) \
338 ? ((c) > ' ' && (c) < 0177 \
339 && !(((c) >= 'a' && (c) <= 'z') \
340 || ((c) >= 'A' && (c) <= 'Z') \
341 || ((c) >= '0' && (c) <= '9'))) \
342 : SYNTAX (c) != Sword)
344 # define ISSPACE(c) (SYNTAX (c) == Swhitespace)
346 # define ISUPPER(c) uppercasep (c)
348 # define ISWORD(c) (SYNTAX (c) == Sword)
350 #else /* not emacs */
352 /* 1 if C is an ASCII character. */
353 # define IS_REAL_ASCII(c) ((c) < 0200)
355 /* This distinction is not meaningful, except in Emacs. */
356 # define ISUNIBYTE(c) 1
358 # ifdef isblank
359 # define ISBLANK(c) isblank (c)
360 # else
361 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
362 # endif
363 # ifdef isgraph
364 # define ISGRAPH(c) isgraph (c)
365 # else
366 # define ISGRAPH(c) (isprint (c) && !isspace (c))
367 # endif
369 /* Solaris defines ISPRINT so we must undefine it first. */
370 # undef ISPRINT
371 # define ISPRINT(c) isprint (c)
372 # define ISDIGIT(c) isdigit (c)
373 # define ISALNUM(c) isalnum (c)
374 # define ISALPHA(c) isalpha (c)
375 # define ISCNTRL(c) iscntrl (c)
376 # define ISLOWER(c) islower (c)
377 # define ISPUNCT(c) ispunct (c)
378 # define ISSPACE(c) isspace (c)
379 # define ISUPPER(c) isupper (c)
380 # define ISXDIGIT(c) isxdigit (c)
382 # define ISWORD(c) ISALPHA (c)
384 # ifdef _tolower
385 # define TOLOWER(c) _tolower (c)
386 # else
387 # define TOLOWER(c) tolower (c)
388 # endif
390 /* How many characters in the character set. */
391 # define CHAR_SET_SIZE 256
393 # ifdef SYNTAX_TABLE
395 extern char *re_syntax_table;
397 # else /* not SYNTAX_TABLE */
399 static char re_syntax_table[CHAR_SET_SIZE];
401 static void
402 init_syntax_once (void)
404 register int c;
405 static int done = 0;
407 if (done)
408 return;
410 memset (re_syntax_table, 0, sizeof re_syntax_table);
412 for (c = 0; c < CHAR_SET_SIZE; ++c)
413 if (ISALNUM (c))
414 re_syntax_table[c] = Sword;
416 re_syntax_table['_'] = Ssymbol;
418 done = 1;
421 # endif /* not SYNTAX_TABLE */
423 # define SYNTAX(c) re_syntax_table[(c)]
425 #endif /* not emacs */
427 #define SIGN_EXTEND_CHAR(c) ((signed char) (c))
429 /* Should we use malloc or alloca? If REGEX_MALLOC is not defined, we
430 use `alloca' instead of `malloc'. This is because using malloc in
431 re_search* or re_match* could cause memory leaks when C-g is used in
432 Emacs; also, malloc is slower and causes storage fragmentation. On
433 the other hand, malloc is more portable, and easier to debug.
435 Because we sometimes use alloca, some routines have to be macros,
436 not functions -- `alloca'-allocated space disappears at the end of the
437 function it is called in. */
439 #ifdef REGEX_MALLOC
441 # define REGEX_ALLOCATE malloc
442 # define REGEX_REALLOCATE(source, osize, nsize) realloc (source, nsize)
443 # define REGEX_FREE free
445 #else /* not REGEX_MALLOC */
447 # ifdef emacs
448 # define REGEX_USE_SAFE_ALLOCA USE_SAFE_ALLOCA
449 # define REGEX_SAFE_FREE() SAFE_FREE ()
450 # define REGEX_ALLOCATE SAFE_ALLOCA
451 # else
452 # include <alloca.h>
453 # define REGEX_ALLOCATE alloca
454 # endif
456 /* Assumes a `char *destination' variable. */
457 # define REGEX_REALLOCATE(source, osize, nsize) \
458 (destination = REGEX_ALLOCATE (nsize), \
459 memcpy (destination, source, osize))
461 /* No need to do anything to free, after alloca. */
462 # define REGEX_FREE(arg) ((void)0) /* Do nothing! But inhibit gcc warning. */
464 #endif /* not REGEX_MALLOC */
466 #ifndef REGEX_USE_SAFE_ALLOCA
467 # define REGEX_USE_SAFE_ALLOCA ((void) 0)
468 # define REGEX_SAFE_FREE() ((void) 0)
469 #endif
471 /* Define how to allocate the failure stack. */
473 #if defined REL_ALLOC && defined REGEX_MALLOC
475 # define REGEX_ALLOCATE_STACK(size) \
476 r_alloc (&failure_stack_ptr, (size))
477 # define REGEX_REALLOCATE_STACK(source, osize, nsize) \
478 r_re_alloc (&failure_stack_ptr, (nsize))
479 # define REGEX_FREE_STACK(ptr) \
480 r_alloc_free (&failure_stack_ptr)
482 #else /* not using relocating allocator */
484 # define REGEX_ALLOCATE_STACK(size) REGEX_ALLOCATE (size)
485 # define REGEX_REALLOCATE_STACK(source, o, n) REGEX_REALLOCATE (source, o, n)
486 # define REGEX_FREE_STACK(ptr) REGEX_FREE (ptr)
488 #endif /* not using relocating allocator */
491 /* True if `size1' is non-NULL and PTR is pointing anywhere inside
492 `string1' or just past its end. This works if PTR is NULL, which is
493 a good thing. */
494 #define FIRST_STRING_P(ptr) \
495 (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
497 /* (Re)Allocate N items of type T using malloc, or fail. */
498 #define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t)))
499 #define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
500 #define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
502 #define BYTEWIDTH 8 /* In bits. */
504 #ifndef emacs
505 # undef max
506 # undef min
507 # define max(a, b) ((a) > (b) ? (a) : (b))
508 # define min(a, b) ((a) < (b) ? (a) : (b))
509 #endif
511 /* Type of source-pattern and string chars. */
512 #ifdef _MSC_VER
513 typedef unsigned char re_char;
514 typedef const re_char const_re_char;
515 #else
516 typedef const unsigned char re_char;
517 typedef re_char const_re_char;
518 #endif
520 typedef char boolean;
522 static regoff_t re_match_2_internal (struct re_pattern_buffer *bufp,
523 re_char *string1, size_t size1,
524 re_char *string2, size_t size2,
525 ssize_t pos,
526 struct re_registers *regs,
527 ssize_t stop);
529 /* These are the command codes that appear in compiled regular
530 expressions. Some opcodes are followed by argument bytes. A
531 command code can specify any interpretation whatsoever for its
532 arguments. Zero bytes may appear in the compiled regular expression. */
534 typedef enum
536 no_op = 0,
538 /* Succeed right away--no more backtracking. */
539 succeed,
541 /* Followed by one byte giving n, then by n literal bytes. */
542 exactn,
544 /* Matches any (more or less) character. */
545 anychar,
547 /* Matches any one char belonging to specified set. First
548 following byte is number of bitmap bytes. Then come bytes
549 for a bitmap saying which chars are in. Bits in each byte
550 are ordered low-bit-first. A character is in the set if its
551 bit is 1. A character too large to have a bit in the map is
552 automatically not in the set.
554 If the length byte has the 0x80 bit set, then that stuff
555 is followed by a range table:
556 2 bytes of flags for character sets (low 8 bits, high 8 bits)
557 See RANGE_TABLE_WORK_BITS below.
558 2 bytes, the number of pairs that follow (upto 32767)
559 pairs, each 2 multibyte characters,
560 each multibyte character represented as 3 bytes. */
561 charset,
563 /* Same parameters as charset, but match any character that is
564 not one of those specified. */
565 charset_not,
567 /* Start remembering the text that is matched, for storing in a
568 register. Followed by one byte with the register number, in
569 the range 0 to one less than the pattern buffer's re_nsub
570 field. */
571 start_memory,
573 /* Stop remembering the text that is matched and store it in a
574 memory register. Followed by one byte with the register
575 number, in the range 0 to one less than `re_nsub' in the
576 pattern buffer. */
577 stop_memory,
579 /* Match a duplicate of something remembered. Followed by one
580 byte containing the register number. */
581 duplicate,
583 /* Fail unless at beginning of line. */
584 begline,
586 /* Fail unless at end of line. */
587 endline,
589 /* Succeeds if at beginning of buffer (if emacs) or at beginning
590 of string to be matched (if not). */
591 begbuf,
593 /* Analogously, for end of buffer/string. */
594 endbuf,
596 /* Followed by two byte relative address to which to jump. */
597 jump,
599 /* Followed by two-byte relative address of place to resume at
600 in case of failure. */
601 on_failure_jump,
603 /* Like on_failure_jump, but pushes a placeholder instead of the
604 current string position when executed. */
605 on_failure_keep_string_jump,
607 /* Just like `on_failure_jump', except that it checks that we
608 don't get stuck in an infinite loop (matching an empty string
609 indefinitely). */
610 on_failure_jump_loop,
612 /* Just like `on_failure_jump_loop', except that it checks for
613 a different kind of loop (the kind that shows up with non-greedy
614 operators). This operation has to be immediately preceded
615 by a `no_op'. */
616 on_failure_jump_nastyloop,
618 /* A smart `on_failure_jump' used for greedy * and + operators.
619 It analyzes the loop before which it is put and if the
620 loop does not require backtracking, it changes itself to
621 `on_failure_keep_string_jump' and short-circuits the loop,
622 else it just defaults to changing itself into `on_failure_jump'.
623 It assumes that it is pointing to just past a `jump'. */
624 on_failure_jump_smart,
626 /* Followed by two-byte relative address and two-byte number n.
627 After matching N times, jump to the address upon failure.
628 Does not work if N starts at 0: use on_failure_jump_loop
629 instead. */
630 succeed_n,
632 /* Followed by two-byte relative address, and two-byte number n.
633 Jump to the address N times, then fail. */
634 jump_n,
636 /* Set the following two-byte relative address to the
637 subsequent two-byte number. The address *includes* the two
638 bytes of number. */
639 set_number_at,
641 wordbeg, /* Succeeds if at word beginning. */
642 wordend, /* Succeeds if at word end. */
644 wordbound, /* Succeeds if at a word boundary. */
645 notwordbound, /* Succeeds if not at a word boundary. */
647 symbeg, /* Succeeds if at symbol beginning. */
648 symend, /* Succeeds if at symbol end. */
650 /* Matches any character whose syntax is specified. Followed by
651 a byte which contains a syntax code, e.g., Sword. */
652 syntaxspec,
654 /* Matches any character whose syntax is not that specified. */
655 notsyntaxspec
657 #ifdef emacs
658 , at_dot, /* Succeeds if at point. */
660 /* Matches any character whose category-set contains the specified
661 category. The operator is followed by a byte which contains a
662 category code (mnemonic ASCII character). */
663 categoryspec,
665 /* Matches any character whose category-set does not contain the
666 specified category. The operator is followed by a byte which
667 contains the category code (mnemonic ASCII character). */
668 notcategoryspec
669 #endif /* emacs */
670 } re_opcode_t;
672 /* Common operations on the compiled pattern. */
674 /* Store NUMBER in two contiguous bytes starting at DESTINATION. */
676 #define STORE_NUMBER(destination, number) \
677 do { \
678 (destination)[0] = (number) & 0377; \
679 (destination)[1] = (number) >> 8; \
680 } while (0)
682 /* Same as STORE_NUMBER, except increment DESTINATION to
683 the byte after where the number is stored. Therefore, DESTINATION
684 must be an lvalue. */
686 #define STORE_NUMBER_AND_INCR(destination, number) \
687 do { \
688 STORE_NUMBER (destination, number); \
689 (destination) += 2; \
690 } while (0)
692 /* Put into DESTINATION a number stored in two contiguous bytes starting
693 at SOURCE. */
695 #define EXTRACT_NUMBER(destination, source) \
696 ((destination) = extract_number (source))
698 static int
699 extract_number (re_char *source)
701 unsigned leading_byte = SIGN_EXTEND_CHAR (source[1]);
702 return (leading_byte << 8) + source[0];
705 /* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
706 SOURCE must be an lvalue. */
708 #define EXTRACT_NUMBER_AND_INCR(destination, source) \
709 ((destination) = extract_number_and_incr (&source))
711 static int
712 extract_number_and_incr (re_char **source)
714 int num = extract_number (*source);
715 *source += 2;
716 return num;
719 /* Store a multibyte character in three contiguous bytes starting
720 DESTINATION, and increment DESTINATION to the byte after where the
721 character is stored. Therefore, DESTINATION must be an lvalue. */
723 #define STORE_CHARACTER_AND_INCR(destination, character) \
724 do { \
725 (destination)[0] = (character) & 0377; \
726 (destination)[1] = ((character) >> 8) & 0377; \
727 (destination)[2] = (character) >> 16; \
728 (destination) += 3; \
729 } while (0)
731 /* Put into DESTINATION a character stored in three contiguous bytes
732 starting at SOURCE. */
734 #define EXTRACT_CHARACTER(destination, source) \
735 do { \
736 (destination) = ((source)[0] \
737 | ((source)[1] << 8) \
738 | ((source)[2] << 16)); \
739 } while (0)
742 /* Macros for charset. */
744 /* Size of bitmap of charset P in bytes. P is a start of charset,
745 i.e. *P is (re_opcode_t) charset or (re_opcode_t) charset_not. */
746 #define CHARSET_BITMAP_SIZE(p) ((p)[1] & 0x7F)
748 /* Nonzero if charset P has range table. */
749 #define CHARSET_RANGE_TABLE_EXISTS_P(p) ((p)[1] & 0x80)
751 /* Return the address of range table of charset P. But not the start
752 of table itself, but the before where the number of ranges is
753 stored. `2 +' means to skip re_opcode_t and size of bitmap,
754 and the 2 bytes of flags at the start of the range table. */
755 #define CHARSET_RANGE_TABLE(p) (&(p)[4 + CHARSET_BITMAP_SIZE (p)])
757 #ifdef emacs
758 /* Extract the bit flags that start a range table. */
759 #define CHARSET_RANGE_TABLE_BITS(p) \
760 ((p)[2 + CHARSET_BITMAP_SIZE (p)] \
761 + (p)[3 + CHARSET_BITMAP_SIZE (p)] * 0x100)
762 #endif
764 /* Return the address of end of RANGE_TABLE. COUNT is number of
765 ranges (which is a pair of (start, end)) in the RANGE_TABLE. `* 2'
766 is start of range and end of range. `* 3' is size of each start
767 and end. */
768 #define CHARSET_RANGE_TABLE_END(range_table, count) \
769 ((range_table) + (count) * 2 * 3)
771 /* If DEBUG is defined, Regex prints many voluminous messages about what
772 it is doing (if the variable `debug' is nonzero). If linked with the
773 main program in `iregex.c', you can enter patterns and strings
774 interactively. And if linked with the main program in `main.c' and
775 the other test files, you can run the already-written tests. */
777 #ifdef DEBUG
779 /* We use standard I/O for debugging. */
780 # include <stdio.h>
782 /* It is useful to test things that ``must'' be true when debugging. */
783 # include <assert.h>
785 static int debug = -100000;
787 # define DEBUG_STATEMENT(e) e
788 # define DEBUG_PRINT(...) if (debug > 0) printf (__VA_ARGS__)
789 # define DEBUG_COMPILES_ARGUMENTS
790 # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) \
791 if (debug > 0) print_partial_compiled_pattern (s, e)
792 # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \
793 if (debug > 0) print_double_string (w, s1, sz1, s2, sz2)
796 /* Print the fastmap in human-readable form. */
798 static void
799 print_fastmap (char *fastmap)
801 unsigned was_a_range = 0;
802 unsigned i = 0;
804 while (i < (1 << BYTEWIDTH))
806 if (fastmap[i++])
808 was_a_range = 0;
809 putchar (i - 1);
810 while (i < (1 << BYTEWIDTH) && fastmap[i])
812 was_a_range = 1;
813 i++;
815 if (was_a_range)
817 printf ("-");
818 putchar (i - 1);
822 putchar ('\n');
826 /* Print a compiled pattern string in human-readable form, starting at
827 the START pointer into it and ending just before the pointer END. */
829 static void
830 print_partial_compiled_pattern (re_char *start, re_char *end)
832 int mcnt, mcnt2;
833 re_char *p = start;
834 re_char *pend = end;
836 if (start == NULL)
838 fprintf (stderr, "(null)\n");
839 return;
842 /* Loop over pattern commands. */
843 while (p < pend)
845 fprintf (stderr, "%td:\t", p - start);
847 switch ((re_opcode_t) *p++)
849 case no_op:
850 fprintf (stderr, "/no_op");
851 break;
853 case succeed:
854 fprintf (stderr, "/succeed");
855 break;
857 case exactn:
858 mcnt = *p++;
859 fprintf (stderr, "/exactn/%d", mcnt);
862 fprintf (stderr, "/%c", *p++);
864 while (--mcnt);
865 break;
867 case start_memory:
868 fprintf (stderr, "/start_memory/%d", *p++);
869 break;
871 case stop_memory:
872 fprintf (stderr, "/stop_memory/%d", *p++);
873 break;
875 case duplicate:
876 fprintf (stderr, "/duplicate/%d", *p++);
877 break;
879 case anychar:
880 fprintf (stderr, "/anychar");
881 break;
883 case charset:
884 case charset_not:
886 register int c, last = -100;
887 register int in_range = 0;
888 int length = CHARSET_BITMAP_SIZE (p - 1);
889 int has_range_table = CHARSET_RANGE_TABLE_EXISTS_P (p - 1);
891 fprintf (stderr, "/charset [%s",
892 (re_opcode_t) *(p - 1) == charset_not ? "^" : "");
894 if (p + *p >= pend)
895 fprintf (stderr, " !extends past end of pattern! ");
897 for (c = 0; c < 256; c++)
898 if (c / 8 < length
899 && (p[1 + (c/8)] & (1 << (c % 8))))
901 /* Are we starting a range? */
902 if (last + 1 == c && ! in_range)
904 fprintf (stderr, "-");
905 in_range = 1;
907 /* Have we broken a range? */
908 else if (last + 1 != c && in_range)
910 fprintf (stderr, "%c", last);
911 in_range = 0;
914 if (! in_range)
915 fprintf (stderr, "%c", c);
917 last = c;
920 if (in_range)
921 fprintf (stderr, "%c", last);
923 fprintf (stderr, "]");
925 p += 1 + length;
927 if (has_range_table)
929 int count;
930 fprintf (stderr, "has-range-table");
932 /* ??? Should print the range table; for now, just skip it. */
933 p += 2; /* skip range table bits */
934 EXTRACT_NUMBER_AND_INCR (count, p);
935 p = CHARSET_RANGE_TABLE_END (p, count);
938 break;
940 case begline:
941 fprintf (stderr, "/begline");
942 break;
944 case endline:
945 fprintf (stderr, "/endline");
946 break;
948 case on_failure_jump:
949 EXTRACT_NUMBER_AND_INCR (mcnt, p);
950 fprintf (stderr, "/on_failure_jump to %td", p + mcnt - start);
951 break;
953 case on_failure_keep_string_jump:
954 EXTRACT_NUMBER_AND_INCR (mcnt, p);
955 fprintf (stderr, "/on_failure_keep_string_jump to %td",
956 p + mcnt - start);
957 break;
959 case on_failure_jump_nastyloop:
960 EXTRACT_NUMBER_AND_INCR (mcnt, p);
961 fprintf (stderr, "/on_failure_jump_nastyloop to %td",
962 p + mcnt - start);
963 break;
965 case on_failure_jump_loop:
966 EXTRACT_NUMBER_AND_INCR (mcnt, p);
967 fprintf (stderr, "/on_failure_jump_loop to %td",
968 p + mcnt - start);
969 break;
971 case on_failure_jump_smart:
972 EXTRACT_NUMBER_AND_INCR (mcnt, p);
973 fprintf (stderr, "/on_failure_jump_smart to %td",
974 p + mcnt - start);
975 break;
977 case jump:
978 EXTRACT_NUMBER_AND_INCR (mcnt, p);
979 fprintf (stderr, "/jump to %td", p + mcnt - start);
980 break;
982 case succeed_n:
983 EXTRACT_NUMBER_AND_INCR (mcnt, p);
984 EXTRACT_NUMBER_AND_INCR (mcnt2, p);
985 fprintf (stderr, "/succeed_n to %td, %d times",
986 p - 2 + mcnt - start, mcnt2);
987 break;
989 case jump_n:
990 EXTRACT_NUMBER_AND_INCR (mcnt, p);
991 EXTRACT_NUMBER_AND_INCR (mcnt2, p);
992 fprintf (stderr, "/jump_n to %td, %d times",
993 p - 2 + mcnt - start, mcnt2);
994 break;
996 case set_number_at:
997 EXTRACT_NUMBER_AND_INCR (mcnt, p);
998 EXTRACT_NUMBER_AND_INCR (mcnt2, p);
999 fprintf (stderr, "/set_number_at location %td to %d",
1000 p - 2 + mcnt - start, mcnt2);
1001 break;
1003 case wordbound:
1004 fprintf (stderr, "/wordbound");
1005 break;
1007 case notwordbound:
1008 fprintf (stderr, "/notwordbound");
1009 break;
1011 case wordbeg:
1012 fprintf (stderr, "/wordbeg");
1013 break;
1015 case wordend:
1016 fprintf (stderr, "/wordend");
1017 break;
1019 case symbeg:
1020 fprintf (stderr, "/symbeg");
1021 break;
1023 case symend:
1024 fprintf (stderr, "/symend");
1025 break;
1027 case syntaxspec:
1028 fprintf (stderr, "/syntaxspec");
1029 mcnt = *p++;
1030 fprintf (stderr, "/%d", mcnt);
1031 break;
1033 case notsyntaxspec:
1034 fprintf (stderr, "/notsyntaxspec");
1035 mcnt = *p++;
1036 fprintf (stderr, "/%d", mcnt);
1037 break;
1039 # ifdef emacs
1040 case at_dot:
1041 fprintf (stderr, "/at_dot");
1042 break;
1044 case categoryspec:
1045 fprintf (stderr, "/categoryspec");
1046 mcnt = *p++;
1047 fprintf (stderr, "/%d", mcnt);
1048 break;
1050 case notcategoryspec:
1051 fprintf (stderr, "/notcategoryspec");
1052 mcnt = *p++;
1053 fprintf (stderr, "/%d", mcnt);
1054 break;
1055 # endif /* emacs */
1057 case begbuf:
1058 fprintf (stderr, "/begbuf");
1059 break;
1061 case endbuf:
1062 fprintf (stderr, "/endbuf");
1063 break;
1065 default:
1066 fprintf (stderr, "?%d", *(p-1));
1069 fprintf (stderr, "\n");
1072 fprintf (stderr, "%td:\tend of pattern.\n", p - start);
1076 static void
1077 print_compiled_pattern (struct re_pattern_buffer *bufp)
1079 re_char *buffer = bufp->buffer;
1081 print_partial_compiled_pattern (buffer, buffer + bufp->used);
1082 printf ("%ld bytes used/%ld bytes allocated.\n",
1083 bufp->used, bufp->allocated);
1085 if (bufp->fastmap_accurate && bufp->fastmap)
1087 printf ("fastmap: ");
1088 print_fastmap (bufp->fastmap);
1091 printf ("re_nsub: %zu\t", bufp->re_nsub);
1092 printf ("regs_alloc: %d\t", bufp->regs_allocated);
1093 printf ("can_be_null: %d\t", bufp->can_be_null);
1094 printf ("no_sub: %d\t", bufp->no_sub);
1095 printf ("not_bol: %d\t", bufp->not_bol);
1096 printf ("not_eol: %d\t", bufp->not_eol);
1097 #ifndef emacs
1098 printf ("syntax: %lx\n", bufp->syntax);
1099 #endif
1100 fflush (stdout);
1101 /* Perhaps we should print the translate table? */
1105 static void
1106 print_double_string (re_char *where, re_char *string1, ssize_t size1,
1107 re_char *string2, ssize_t size2)
1109 ssize_t this_char;
1111 if (where == NULL)
1112 printf ("(null)");
1113 else
1115 if (FIRST_STRING_P (where))
1117 for (this_char = where - string1; this_char < size1; this_char++)
1118 putchar (string1[this_char]);
1120 where = string2;
1123 for (this_char = where - string2; this_char < size2; this_char++)
1124 putchar (string2[this_char]);
1128 #else /* not DEBUG */
1130 # undef assert
1131 # define assert(e)
1133 # define DEBUG_STATEMENT(e)
1134 # define DEBUG_PRINT(...)
1135 # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
1136 # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
1138 #endif /* not DEBUG */
1140 #ifndef emacs
1142 /* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
1143 also be assigned to arbitrarily: each pattern buffer stores its own
1144 syntax, so it can be changed between regex compilations. */
1145 /* This has no initializer because initialized variables in Emacs
1146 become read-only after dumping. */
1147 reg_syntax_t re_syntax_options;
1150 /* Specify the precise syntax of regexps for compilation. This provides
1151 for compatibility for various utilities which historically have
1152 different, incompatible syntaxes.
1154 The argument SYNTAX is a bit mask comprised of the various bits
1155 defined in regex.h. We return the old syntax. */
1157 reg_syntax_t
1158 re_set_syntax (reg_syntax_t syntax)
1160 reg_syntax_t ret = re_syntax_options;
1162 re_syntax_options = syntax;
1163 return ret;
1165 WEAK_ALIAS (__re_set_syntax, re_set_syntax)
1167 #endif
1169 /* This table gives an error message for each of the error codes listed
1170 in regex.h. Obviously the order here has to be same as there.
1171 POSIX doesn't require that we do anything for REG_NOERROR,
1172 but why not be nice? */
1174 static const char *re_error_msgid[] =
1176 gettext_noop ("Success"), /* REG_NOERROR */
1177 gettext_noop ("No match"), /* REG_NOMATCH */
1178 gettext_noop ("Invalid regular expression"), /* REG_BADPAT */
1179 gettext_noop ("Invalid collation character"), /* REG_ECOLLATE */
1180 gettext_noop ("Invalid character class name"), /* REG_ECTYPE */
1181 gettext_noop ("Trailing backslash"), /* REG_EESCAPE */
1182 gettext_noop ("Invalid back reference"), /* REG_ESUBREG */
1183 gettext_noop ("Unmatched [ or [^"), /* REG_EBRACK */
1184 gettext_noop ("Unmatched ( or \\("), /* REG_EPAREN */
1185 gettext_noop ("Unmatched \\{"), /* REG_EBRACE */
1186 gettext_noop ("Invalid content of \\{\\}"), /* REG_BADBR */
1187 gettext_noop ("Invalid range end"), /* REG_ERANGE */
1188 gettext_noop ("Memory exhausted"), /* REG_ESPACE */
1189 gettext_noop ("Invalid preceding regular expression"), /* REG_BADRPT */
1190 gettext_noop ("Premature end of regular expression"), /* REG_EEND */
1191 gettext_noop ("Regular expression too big"), /* REG_ESIZE */
1192 gettext_noop ("Unmatched ) or \\)"), /* REG_ERPAREN */
1193 gettext_noop ("Range striding over charsets") /* REG_ERANGEX */
1196 /* Avoiding alloca during matching, to placate r_alloc. */
1198 /* Define MATCH_MAY_ALLOCATE unless we need to make sure that the
1199 searching and matching functions should not call alloca. On some
1200 systems, alloca is implemented in terms of malloc, and if we're
1201 using the relocating allocator routines, then malloc could cause a
1202 relocation, which might (if the strings being searched are in the
1203 ralloc heap) shift the data out from underneath the regexp
1204 routines.
1206 Here's another reason to avoid allocation: Emacs
1207 processes input from X in a signal handler; processing X input may
1208 call malloc; if input arrives while a matching routine is calling
1209 malloc, then we're scrod. But Emacs can't just block input while
1210 calling matching routines; then we don't notice interrupts when
1211 they come in. So, Emacs blocks input around all regexp calls
1212 except the matching calls, which it leaves unprotected, in the
1213 faith that they will not malloc. */
1215 /* Normally, this is fine. */
1216 #define MATCH_MAY_ALLOCATE
1218 /* The match routines may not allocate if (1) they would do it with malloc
1219 and (2) it's not safe for them to use malloc.
1220 Note that if REL_ALLOC is defined, matching would not use malloc for the
1221 failure stack, but we would still use it for the register vectors;
1222 so REL_ALLOC should not affect this. */
1223 #if defined REGEX_MALLOC && defined emacs
1224 # undef MATCH_MAY_ALLOCATE
1225 #endif
1228 /* Failure stack declarations and macros; both re_compile_fastmap and
1229 re_match_2 use a failure stack. These have to be macros because of
1230 REGEX_ALLOCATE_STACK. */
1233 /* Approximate number of failure points for which to initially allocate space
1234 when matching. If this number is exceeded, we allocate more
1235 space, so it is not a hard limit. */
1236 #ifndef INIT_FAILURE_ALLOC
1237 # define INIT_FAILURE_ALLOC 20
1238 #endif
1240 /* Roughly the maximum number of failure points on the stack. Would be
1241 exactly that if always used TYPICAL_FAILURE_SIZE items each time we failed.
1242 This is a variable only so users of regex can assign to it; we never
1243 change it ourselves. We always multiply it by TYPICAL_FAILURE_SIZE
1244 before using it, so it should probably be a byte-count instead. */
1245 # if defined MATCH_MAY_ALLOCATE
1246 /* Note that 4400 was enough to cause a crash on Alpha OSF/1,
1247 whose default stack limit is 2mb. In order for a larger
1248 value to work reliably, you have to try to make it accord
1249 with the process stack limit. */
1250 size_t re_max_failures = 40000;
1251 # else
1252 size_t re_max_failures = 4000;
1253 # endif
1255 union fail_stack_elt
1257 re_char *pointer;
1258 /* This should be the biggest `int' that's no bigger than a pointer. */
1259 long integer;
1262 typedef union fail_stack_elt fail_stack_elt_t;
1264 typedef struct
1266 fail_stack_elt_t *stack;
1267 size_t size;
1268 size_t avail; /* Offset of next open position. */
1269 size_t frame; /* Offset of the cur constructed frame. */
1270 } fail_stack_type;
1272 #define FAIL_STACK_EMPTY() (fail_stack.frame == 0)
1275 /* Define macros to initialize and free the failure stack.
1276 Do `return -2' if the alloc fails. */
1278 #ifdef MATCH_MAY_ALLOCATE
1279 # define INIT_FAIL_STACK() \
1280 do { \
1281 fail_stack.stack = \
1282 REGEX_ALLOCATE_STACK (INIT_FAILURE_ALLOC * TYPICAL_FAILURE_SIZE \
1283 * sizeof (fail_stack_elt_t)); \
1285 if (fail_stack.stack == NULL) \
1286 return -2; \
1288 fail_stack.size = INIT_FAILURE_ALLOC; \
1289 fail_stack.avail = 0; \
1290 fail_stack.frame = 0; \
1291 } while (0)
1292 #else
1293 # define INIT_FAIL_STACK() \
1294 do { \
1295 fail_stack.avail = 0; \
1296 fail_stack.frame = 0; \
1297 } while (0)
1299 # define RETALLOC_IF(addr, n, t) \
1300 if (addr) RETALLOC((addr), (n), t); else (addr) = TALLOC ((n), t)
1301 #endif
1304 /* Double the size of FAIL_STACK, up to a limit
1305 which allows approximately `re_max_failures' items.
1307 Return 1 if succeeds, and 0 if either ran out of memory
1308 allocating space for it or it was already too large.
1310 REGEX_REALLOCATE_STACK requires `destination' be declared. */
1312 /* Factor to increase the failure stack size by
1313 when we increase it.
1314 This used to be 2, but 2 was too wasteful
1315 because the old discarded stacks added up to as much space
1316 were as ultimate, maximum-size stack. */
1317 #define FAIL_STACK_GROWTH_FACTOR 4
1319 #define GROW_FAIL_STACK(fail_stack) \
1320 (((fail_stack).size * sizeof (fail_stack_elt_t) \
1321 >= re_max_failures * TYPICAL_FAILURE_SIZE) \
1322 ? 0 \
1323 : ((fail_stack).stack \
1324 = REGEX_REALLOCATE_STACK ((fail_stack).stack, \
1325 (fail_stack).size * sizeof (fail_stack_elt_t), \
1326 min (re_max_failures * TYPICAL_FAILURE_SIZE, \
1327 ((fail_stack).size * sizeof (fail_stack_elt_t) \
1328 * FAIL_STACK_GROWTH_FACTOR))), \
1330 (fail_stack).stack == NULL \
1331 ? 0 \
1332 : ((fail_stack).size \
1333 = (min (re_max_failures * TYPICAL_FAILURE_SIZE, \
1334 ((fail_stack).size * sizeof (fail_stack_elt_t) \
1335 * FAIL_STACK_GROWTH_FACTOR)) \
1336 / sizeof (fail_stack_elt_t)), \
1337 1)))
1340 /* Push a pointer value onto the failure stack.
1341 Assumes the variable `fail_stack'. Probably should only
1342 be called from within `PUSH_FAILURE_POINT'. */
1343 #define PUSH_FAILURE_POINTER(item) \
1344 fail_stack.stack[fail_stack.avail++].pointer = (item)
1346 /* This pushes an integer-valued item onto the failure stack.
1347 Assumes the variable `fail_stack'. Probably should only
1348 be called from within `PUSH_FAILURE_POINT'. */
1349 #define PUSH_FAILURE_INT(item) \
1350 fail_stack.stack[fail_stack.avail++].integer = (item)
1352 /* These POP... operations complement the PUSH... operations.
1353 All assume that `fail_stack' is nonempty. */
1354 #define POP_FAILURE_POINTER() fail_stack.stack[--fail_stack.avail].pointer
1355 #define POP_FAILURE_INT() fail_stack.stack[--fail_stack.avail].integer
1357 /* Individual items aside from the registers. */
1358 #define NUM_NONREG_ITEMS 3
1360 /* Used to examine the stack (to detect infinite loops). */
1361 #define FAILURE_PAT(h) fail_stack.stack[(h) - 1].pointer
1362 #define FAILURE_STR(h) (fail_stack.stack[(h) - 2].pointer)
1363 #define NEXT_FAILURE_HANDLE(h) fail_stack.stack[(h) - 3].integer
1364 #define TOP_FAILURE_HANDLE() fail_stack.frame
1367 #define ENSURE_FAIL_STACK(space) \
1368 while (REMAINING_AVAIL_SLOTS <= space) { \
1369 if (!GROW_FAIL_STACK (fail_stack)) \
1370 return -2; \
1371 DEBUG_PRINT ("\n Doubled stack; size now: %zd\n", (fail_stack).size);\
1372 DEBUG_PRINT (" slots available: %zd\n", REMAINING_AVAIL_SLOTS);\
1375 /* Push register NUM onto the stack. */
1376 #define PUSH_FAILURE_REG(num) \
1377 do { \
1378 char *destination; \
1379 long n = num; \
1380 ENSURE_FAIL_STACK(3); \
1381 DEBUG_PRINT (" Push reg %ld (spanning %p -> %p)\n", \
1382 n, regstart[n], regend[n]); \
1383 PUSH_FAILURE_POINTER (regstart[n]); \
1384 PUSH_FAILURE_POINTER (regend[n]); \
1385 PUSH_FAILURE_INT (n); \
1386 } while (0)
1388 /* Change the counter's value to VAL, but make sure that it will
1389 be reset when backtracking. */
1390 #define PUSH_NUMBER(ptr,val) \
1391 do { \
1392 char *destination; \
1393 int c; \
1394 ENSURE_FAIL_STACK(3); \
1395 EXTRACT_NUMBER (c, ptr); \
1396 DEBUG_PRINT (" Push number %p = %d -> %d\n", ptr, c, val); \
1397 PUSH_FAILURE_INT (c); \
1398 PUSH_FAILURE_POINTER (ptr); \
1399 PUSH_FAILURE_INT (-1); \
1400 STORE_NUMBER (ptr, val); \
1401 } while (0)
1403 /* Pop a saved register off the stack. */
1404 #define POP_FAILURE_REG_OR_COUNT() \
1405 do { \
1406 long pfreg = POP_FAILURE_INT (); \
1407 if (pfreg == -1) \
1409 /* It's a counter. */ \
1410 /* Here, we discard `const', making re_match non-reentrant. */ \
1411 unsigned char *ptr = (unsigned char*) POP_FAILURE_POINTER (); \
1412 pfreg = POP_FAILURE_INT (); \
1413 STORE_NUMBER (ptr, pfreg); \
1414 DEBUG_PRINT (" Pop counter %p = %ld\n", ptr, pfreg); \
1416 else \
1418 regend[pfreg] = POP_FAILURE_POINTER (); \
1419 regstart[pfreg] = POP_FAILURE_POINTER (); \
1420 DEBUG_PRINT (" Pop reg %ld (spanning %p -> %p)\n", \
1421 pfreg, regstart[pfreg], regend[pfreg]); \
1423 } while (0)
1425 /* Check that we are not stuck in an infinite loop. */
1426 #define CHECK_INFINITE_LOOP(pat_cur, string_place) \
1427 do { \
1428 ssize_t failure = TOP_FAILURE_HANDLE (); \
1429 /* Check for infinite matching loops */ \
1430 while (failure > 0 \
1431 && (FAILURE_STR (failure) == string_place \
1432 || FAILURE_STR (failure) == NULL)) \
1434 assert (FAILURE_PAT (failure) >= bufp->buffer \
1435 && FAILURE_PAT (failure) <= bufp->buffer + bufp->used); \
1436 if (FAILURE_PAT (failure) == pat_cur) \
1438 cycle = 1; \
1439 break; \
1441 DEBUG_PRINT (" Other pattern: %p\n", FAILURE_PAT (failure)); \
1442 failure = NEXT_FAILURE_HANDLE(failure); \
1444 DEBUG_PRINT (" Other string: %p\n", FAILURE_STR (failure)); \
1445 } while (0)
1447 /* Push the information about the state we will need
1448 if we ever fail back to it.
1450 Requires variables fail_stack, regstart, regend and
1451 num_regs be declared. GROW_FAIL_STACK requires `destination' be
1452 declared.
1454 Does `return FAILURE_CODE' if runs out of memory. */
1456 #define PUSH_FAILURE_POINT(pattern, string_place) \
1457 do { \
1458 char *destination; \
1459 /* Must be int, so when we don't save any registers, the arithmetic \
1460 of 0 + -1 isn't done as unsigned. */ \
1462 DEBUG_STATEMENT (nfailure_points_pushed++); \
1463 DEBUG_PRINT ("\nPUSH_FAILURE_POINT:\n"); \
1464 DEBUG_PRINT (" Before push, next avail: %zd\n", (fail_stack).avail); \
1465 DEBUG_PRINT (" size: %zd\n", (fail_stack).size);\
1467 ENSURE_FAIL_STACK (NUM_NONREG_ITEMS); \
1469 DEBUG_PRINT ("\n"); \
1471 DEBUG_PRINT (" Push frame index: %zd\n", fail_stack.frame); \
1472 PUSH_FAILURE_INT (fail_stack.frame); \
1474 DEBUG_PRINT (" Push string %p: \"", string_place); \
1475 DEBUG_PRINT_DOUBLE_STRING (string_place, string1, size1, string2, size2);\
1476 DEBUG_PRINT ("\"\n"); \
1477 PUSH_FAILURE_POINTER (string_place); \
1479 DEBUG_PRINT (" Push pattern %p: ", pattern); \
1480 DEBUG_PRINT_COMPILED_PATTERN (bufp, pattern, pend); \
1481 PUSH_FAILURE_POINTER (pattern); \
1483 /* Close the frame by moving the frame pointer past it. */ \
1484 fail_stack.frame = fail_stack.avail; \
1485 } while (0)
1487 /* Estimate the size of data pushed by a typical failure stack entry.
1488 An estimate is all we need, because all we use this for
1489 is to choose a limit for how big to make the failure stack. */
1490 /* BEWARE, the value `20' is hard-coded in emacs.c:main(). */
1491 #define TYPICAL_FAILURE_SIZE 20
1493 /* How many items can still be added to the stack without overflowing it. */
1494 #define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail)
1497 /* Pops what PUSH_FAIL_STACK pushes.
1499 We restore into the parameters, all of which should be lvalues:
1500 STR -- the saved data position.
1501 PAT -- the saved pattern position.
1502 REGSTART, REGEND -- arrays of string positions.
1504 Also assumes the variables `fail_stack' and (if debugging), `bufp',
1505 `pend', `string1', `size1', `string2', and `size2'. */
1507 #define POP_FAILURE_POINT(str, pat) \
1508 do { \
1509 assert (!FAIL_STACK_EMPTY ()); \
1511 /* Remove failure points and point to how many regs pushed. */ \
1512 DEBUG_PRINT ("POP_FAILURE_POINT:\n"); \
1513 DEBUG_PRINT (" Before pop, next avail: %zd\n", fail_stack.avail); \
1514 DEBUG_PRINT (" size: %zd\n", fail_stack.size); \
1516 /* Pop the saved registers. */ \
1517 while (fail_stack.frame < fail_stack.avail) \
1518 POP_FAILURE_REG_OR_COUNT (); \
1520 pat = POP_FAILURE_POINTER (); \
1521 DEBUG_PRINT (" Popping pattern %p: ", pat); \
1522 DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend); \
1524 /* If the saved string location is NULL, it came from an \
1525 on_failure_keep_string_jump opcode, and we want to throw away the \
1526 saved NULL, thus retaining our current position in the string. */ \
1527 str = POP_FAILURE_POINTER (); \
1528 DEBUG_PRINT (" Popping string %p: \"", str); \
1529 DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2); \
1530 DEBUG_PRINT ("\"\n"); \
1532 fail_stack.frame = POP_FAILURE_INT (); \
1533 DEBUG_PRINT (" Popping frame index: %zd\n", fail_stack.frame); \
1535 assert (fail_stack.avail >= 0); \
1536 assert (fail_stack.frame <= fail_stack.avail); \
1538 DEBUG_STATEMENT (nfailure_points_popped++); \
1539 } while (0) /* POP_FAILURE_POINT */
1543 /* Registers are set to a sentinel when they haven't yet matched. */
1544 #define REG_UNSET(e) ((e) == NULL)
1546 /* Subroutine declarations and macros for regex_compile. */
1548 static reg_errcode_t regex_compile (re_char *pattern, size_t size,
1549 #ifdef emacs
1550 bool posix_backtracking,
1551 const char *whitespace_regexp,
1552 #else
1553 reg_syntax_t syntax,
1554 #endif
1555 struct re_pattern_buffer *bufp);
1556 static void store_op1 (re_opcode_t op, unsigned char *loc, int arg);
1557 static void store_op2 (re_opcode_t op, unsigned char *loc, int arg1, int arg2);
1558 static void insert_op1 (re_opcode_t op, unsigned char *loc,
1559 int arg, unsigned char *end);
1560 static void insert_op2 (re_opcode_t op, unsigned char *loc,
1561 int arg1, int arg2, unsigned char *end);
1562 static boolean at_begline_loc_p (re_char *pattern, re_char *p,
1563 reg_syntax_t syntax);
1564 static boolean at_endline_loc_p (re_char *p, re_char *pend,
1565 reg_syntax_t syntax);
1566 static re_char *skip_one_char (re_char *p);
1567 static int analyze_first (re_char *p, re_char *pend,
1568 char *fastmap, const int multibyte);
1570 /* Fetch the next character in the uncompiled pattern, with no
1571 translation. */
1572 #define PATFETCH(c) \
1573 do { \
1574 int len; \
1575 if (p == pend) return REG_EEND; \
1576 c = RE_STRING_CHAR_AND_LENGTH (p, len, multibyte); \
1577 p += len; \
1578 } while (0)
1581 /* If `translate' is non-null, return translate[D], else just D. We
1582 cast the subscript to translate because some data is declared as
1583 `char *', to avoid warnings when a string constant is passed. But
1584 when we use a character as a subscript we must make it unsigned. */
1585 #ifndef TRANSLATE
1586 # define TRANSLATE(d) \
1587 (RE_TRANSLATE_P (translate) ? RE_TRANSLATE (translate, (d)) : (d))
1588 #endif
1591 /* Macros for outputting the compiled pattern into `buffer'. */
1593 /* If the buffer isn't allocated when it comes in, use this. */
1594 #define INIT_BUF_SIZE 32
1596 /* Make sure we have at least N more bytes of space in buffer. */
1597 #define GET_BUFFER_SPACE(n) \
1598 while ((size_t) (b - bufp->buffer + (n)) > bufp->allocated) \
1599 EXTEND_BUFFER ()
1601 /* Make sure we have one more byte of buffer space and then add C to it. */
1602 #define BUF_PUSH(c) \
1603 do { \
1604 GET_BUFFER_SPACE (1); \
1605 *b++ = (unsigned char) (c); \
1606 } while (0)
1609 /* Ensure we have two more bytes of buffer space and then append C1 and C2. */
1610 #define BUF_PUSH_2(c1, c2) \
1611 do { \
1612 GET_BUFFER_SPACE (2); \
1613 *b++ = (unsigned char) (c1); \
1614 *b++ = (unsigned char) (c2); \
1615 } while (0)
1618 /* Store a jump with opcode OP at LOC to location TO. We store a
1619 relative address offset by the three bytes the jump itself occupies. */
1620 #define STORE_JUMP(op, loc, to) \
1621 store_op1 (op, loc, (to) - (loc) - 3)
1623 /* Likewise, for a two-argument jump. */
1624 #define STORE_JUMP2(op, loc, to, arg) \
1625 store_op2 (op, loc, (to) - (loc) - 3, arg)
1627 /* Like `STORE_JUMP', but for inserting. Assume `b' is the buffer end. */
1628 #define INSERT_JUMP(op, loc, to) \
1629 insert_op1 (op, loc, (to) - (loc) - 3, b)
1631 /* Like `STORE_JUMP2', but for inserting. Assume `b' is the buffer end. */
1632 #define INSERT_JUMP2(op, loc, to, arg) \
1633 insert_op2 (op, loc, (to) - (loc) - 3, arg, b)
1636 /* This is not an arbitrary limit: the arguments which represent offsets
1637 into the pattern are two bytes long. So if 2^15 bytes turns out to
1638 be too small, many things would have to change. */
1639 # define MAX_BUF_SIZE (1L << 15)
1641 /* Extend the buffer by twice its current size via realloc and
1642 reset the pointers that pointed into the old block to point to the
1643 correct places in the new one. If extending the buffer results in it
1644 being larger than MAX_BUF_SIZE, then flag memory exhausted. */
1645 #if __BOUNDED_POINTERS__
1646 # define SET_HIGH_BOUND(P) (__ptrhigh (P) = __ptrlow (P) + bufp->allocated)
1647 # define MOVE_BUFFER_POINTER(P) \
1648 (__ptrlow (P) = new_buffer + (__ptrlow (P) - old_buffer), \
1649 SET_HIGH_BOUND (P), \
1650 __ptrvalue (P) = new_buffer + (__ptrvalue (P) - old_buffer))
1651 # define ELSE_EXTEND_BUFFER_HIGH_BOUND \
1652 else \
1654 SET_HIGH_BOUND (b); \
1655 SET_HIGH_BOUND (begalt); \
1656 if (fixup_alt_jump) \
1657 SET_HIGH_BOUND (fixup_alt_jump); \
1658 if (laststart) \
1659 SET_HIGH_BOUND (laststart); \
1660 if (pending_exact) \
1661 SET_HIGH_BOUND (pending_exact); \
1663 #else
1664 # define MOVE_BUFFER_POINTER(P) ((P) = new_buffer + ((P) - old_buffer))
1665 # define ELSE_EXTEND_BUFFER_HIGH_BOUND
1666 #endif
1667 #define EXTEND_BUFFER() \
1668 do { \
1669 unsigned char *old_buffer = bufp->buffer; \
1670 if (bufp->allocated == MAX_BUF_SIZE) \
1671 return REG_ESIZE; \
1672 bufp->allocated <<= 1; \
1673 if (bufp->allocated > MAX_BUF_SIZE) \
1674 bufp->allocated = MAX_BUF_SIZE; \
1675 RETALLOC (bufp->buffer, bufp->allocated, unsigned char); \
1676 if (bufp->buffer == NULL) \
1677 return REG_ESPACE; \
1678 /* If the buffer moved, move all the pointers into it. */ \
1679 if (old_buffer != bufp->buffer) \
1681 unsigned char *new_buffer = bufp->buffer; \
1682 MOVE_BUFFER_POINTER (b); \
1683 MOVE_BUFFER_POINTER (begalt); \
1684 if (fixup_alt_jump) \
1685 MOVE_BUFFER_POINTER (fixup_alt_jump); \
1686 if (laststart) \
1687 MOVE_BUFFER_POINTER (laststart); \
1688 if (pending_exact) \
1689 MOVE_BUFFER_POINTER (pending_exact); \
1691 ELSE_EXTEND_BUFFER_HIGH_BOUND \
1692 } while (0)
1695 /* Since we have one byte reserved for the register number argument to
1696 {start,stop}_memory, the maximum number of groups we can report
1697 things about is what fits in that byte. */
1698 #define MAX_REGNUM 255
1700 /* But patterns can have more than `MAX_REGNUM' registers. We just
1701 ignore the excess. */
1702 typedef int regnum_t;
1705 /* Macros for the compile stack. */
1707 /* Since offsets can go either forwards or backwards, this type needs to
1708 be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1. */
1709 /* int may be not enough when sizeof(int) == 2. */
1710 typedef long pattern_offset_t;
1712 typedef struct
1714 pattern_offset_t begalt_offset;
1715 pattern_offset_t fixup_alt_jump;
1716 pattern_offset_t laststart_offset;
1717 regnum_t regnum;
1718 } compile_stack_elt_t;
1721 typedef struct
1723 compile_stack_elt_t *stack;
1724 size_t size;
1725 size_t avail; /* Offset of next open position. */
1726 } compile_stack_type;
1729 #define INIT_COMPILE_STACK_SIZE 32
1731 #define COMPILE_STACK_EMPTY (compile_stack.avail == 0)
1732 #define COMPILE_STACK_FULL (compile_stack.avail == compile_stack.size)
1734 /* The next available element. */
1735 #define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
1737 /* Explicit quit checking is needed for Emacs, which uses polling to
1738 process input events. */
1739 #ifdef emacs
1740 # define IMMEDIATE_QUIT_CHECK \
1741 do { \
1742 if (immediate_quit) QUIT; \
1743 } while (0)
1744 #else
1745 # define IMMEDIATE_QUIT_CHECK ((void)0)
1746 #endif
1748 /* Structure to manage work area for range table. */
1749 struct range_table_work_area
1751 int *table; /* actual work area. */
1752 int allocated; /* allocated size for work area in bytes. */
1753 int used; /* actually used size in words. */
1754 int bits; /* flag to record character classes */
1757 #ifdef emacs
1759 /* Make sure that WORK_AREA can hold more N multibyte characters.
1760 This is used only in set_image_of_range and set_image_of_range_1.
1761 It expects WORK_AREA to be a pointer.
1762 If it can't get the space, it returns from the surrounding function. */
1764 #define EXTEND_RANGE_TABLE(work_area, n) \
1765 do { \
1766 if (((work_area).used + (n)) * sizeof (int) > (work_area).allocated) \
1768 extend_range_table_work_area (&work_area); \
1769 if ((work_area).table == 0) \
1770 return (REG_ESPACE); \
1772 } while (0)
1774 #define SET_RANGE_TABLE_WORK_AREA_BIT(work_area, bit) \
1775 (work_area).bits |= (bit)
1777 /* Set a range (RANGE_START, RANGE_END) to WORK_AREA. */
1778 #define SET_RANGE_TABLE_WORK_AREA(work_area, range_start, range_end) \
1779 do { \
1780 EXTEND_RANGE_TABLE ((work_area), 2); \
1781 (work_area).table[(work_area).used++] = (range_start); \
1782 (work_area).table[(work_area).used++] = (range_end); \
1783 } while (0)
1785 #endif /* emacs */
1787 /* Free allocated memory for WORK_AREA. */
1788 #define FREE_RANGE_TABLE_WORK_AREA(work_area) \
1789 do { \
1790 if ((work_area).table) \
1791 free ((work_area).table); \
1792 } while (0)
1794 #define CLEAR_RANGE_TABLE_WORK_USED(work_area) ((work_area).used = 0, (work_area).bits = 0)
1795 #define RANGE_TABLE_WORK_USED(work_area) ((work_area).used)
1796 #define RANGE_TABLE_WORK_BITS(work_area) ((work_area).bits)
1797 #define RANGE_TABLE_WORK_ELT(work_area, i) ((work_area).table[i])
1799 /* Bits used to implement the multibyte-part of the various character classes
1800 such as [:alnum:] in a charset's range table. The code currently assumes
1801 that only the low 16 bits are used. */
1802 #define BIT_WORD 0x1
1803 #define BIT_LOWER 0x2
1804 #define BIT_PUNCT 0x4
1805 #define BIT_SPACE 0x8
1806 #define BIT_UPPER 0x10
1807 #define BIT_MULTIBYTE 0x20
1808 #define BIT_ALPHA 0x40
1809 #define BIT_ALNUM 0x80
1810 #define BIT_GRAPH 0x100
1811 #define BIT_PRINT 0x200
1814 /* Set the bit for character C in a list. */
1815 #define SET_LIST_BIT(c) (b[((c)) / BYTEWIDTH] |= 1 << ((c) % BYTEWIDTH))
1818 #ifdef emacs
1820 /* Store characters in the range FROM to TO in the bitmap at B (for
1821 ASCII and unibyte characters) and WORK_AREA (for multibyte
1822 characters) while translating them and paying attention to the
1823 continuity of translated characters.
1825 Implementation note: It is better to implement these fairly big
1826 macros by a function, but it's not that easy because macros called
1827 in this macro assume various local variables already declared. */
1829 /* Both FROM and TO are ASCII characters. */
1831 #define SETUP_ASCII_RANGE(work_area, FROM, TO) \
1832 do { \
1833 int C0, C1; \
1835 for (C0 = (FROM); C0 <= (TO); C0++) \
1837 C1 = TRANSLATE (C0); \
1838 if (! ASCII_CHAR_P (C1)) \
1840 SET_RANGE_TABLE_WORK_AREA ((work_area), C1, C1); \
1841 if ((C1 = RE_CHAR_TO_UNIBYTE (C1)) < 0) \
1842 C1 = C0; \
1844 SET_LIST_BIT (C1); \
1846 } while (0)
1849 /* Both FROM and TO are unibyte characters (0x80..0xFF). */
1851 #define SETUP_UNIBYTE_RANGE(work_area, FROM, TO) \
1852 do { \
1853 int C0, C1, C2, I; \
1854 int USED = RANGE_TABLE_WORK_USED (work_area); \
1856 for (C0 = (FROM); C0 <= (TO); C0++) \
1858 C1 = RE_CHAR_TO_MULTIBYTE (C0); \
1859 if (CHAR_BYTE8_P (C1)) \
1860 SET_LIST_BIT (C0); \
1861 else \
1863 C2 = TRANSLATE (C1); \
1864 if (C2 == C1 \
1865 || (C1 = RE_CHAR_TO_UNIBYTE (C2)) < 0) \
1866 C1 = C0; \
1867 SET_LIST_BIT (C1); \
1868 for (I = RANGE_TABLE_WORK_USED (work_area) - 2; I >= USED; I -= 2) \
1870 int from = RANGE_TABLE_WORK_ELT (work_area, I); \
1871 int to = RANGE_TABLE_WORK_ELT (work_area, I + 1); \
1873 if (C2 >= from - 1 && C2 <= to + 1) \
1875 if (C2 == from - 1) \
1876 RANGE_TABLE_WORK_ELT (work_area, I)--; \
1877 else if (C2 == to + 1) \
1878 RANGE_TABLE_WORK_ELT (work_area, I + 1)++; \
1879 break; \
1882 if (I < USED) \
1883 SET_RANGE_TABLE_WORK_AREA ((work_area), C2, C2); \
1886 } while (0)
1889 /* Both FROM and TO are multibyte characters. */
1891 #define SETUP_MULTIBYTE_RANGE(work_area, FROM, TO) \
1892 do { \
1893 int C0, C1, C2, I, USED = RANGE_TABLE_WORK_USED (work_area); \
1895 SET_RANGE_TABLE_WORK_AREA ((work_area), (FROM), (TO)); \
1896 for (C0 = (FROM); C0 <= (TO); C0++) \
1898 C1 = TRANSLATE (C0); \
1899 if ((C2 = RE_CHAR_TO_UNIBYTE (C1)) >= 0 \
1900 || (C1 != C0 && (C2 = RE_CHAR_TO_UNIBYTE (C0)) >= 0)) \
1901 SET_LIST_BIT (C2); \
1902 if (C1 >= (FROM) && C1 <= (TO)) \
1903 continue; \
1904 for (I = RANGE_TABLE_WORK_USED (work_area) - 2; I >= USED; I -= 2) \
1906 int from = RANGE_TABLE_WORK_ELT (work_area, I); \
1907 int to = RANGE_TABLE_WORK_ELT (work_area, I + 1); \
1909 if (C1 >= from - 1 && C1 <= to + 1) \
1911 if (C1 == from - 1) \
1912 RANGE_TABLE_WORK_ELT (work_area, I)--; \
1913 else if (C1 == to + 1) \
1914 RANGE_TABLE_WORK_ELT (work_area, I + 1)++; \
1915 break; \
1918 if (I < USED) \
1919 SET_RANGE_TABLE_WORK_AREA ((work_area), C1, C1); \
1921 } while (0)
1923 #endif /* emacs */
1925 /* Get the next unsigned number in the uncompiled pattern. */
1926 #define GET_INTERVAL_COUNT(num) \
1927 do { \
1928 if (p == pend) \
1929 FREE_STACK_RETURN (REG_EBRACE); \
1930 else \
1932 PATFETCH (c); \
1933 while ('0' <= c && c <= '9') \
1935 if (num < 0) \
1936 num = 0; \
1937 if (RE_DUP_MAX / 10 - (RE_DUP_MAX % 10 < c - '0') < num) \
1938 FREE_STACK_RETURN (REG_BADBR); \
1939 num = num * 10 + c - '0'; \
1940 if (p == pend) \
1941 FREE_STACK_RETURN (REG_EBRACE); \
1942 PATFETCH (c); \
1945 } while (0)
1947 #if ! WIDE_CHAR_SUPPORT
1949 /* Parse a character class, i.e. string such as "[:name:]". *strp
1950 points to the string to be parsed and limit is length, in bytes, of
1951 that string.
1953 If *strp point to a string that begins with "[:name:]", where name is
1954 a non-empty sequence of lower case letters, *strp will be advanced past the
1955 closing square bracket and RECC_* constant which maps to the name will be
1956 returned. If name is not a valid character class name zero, or RECC_ERROR,
1957 is returned.
1959 Otherwise, if *strp doesn’t begin with "[:name:]", -1 is returned.
1961 The function can be used on ASCII and multibyte (UTF-8-encoded) strings.
1963 re_wctype_t
1964 re_wctype_parse (const unsigned char **strp, unsigned limit)
1966 const char *beg = (const char *)*strp, *it;
1968 if (limit < 4 || beg[0] != '[' || beg[1] != ':')
1969 return -1;
1971 beg += 2; /* skip opening ‘[:’ */
1972 limit -= 3; /* opening ‘[:’ and half of closing ‘:]’; --limit handles rest */
1973 for (it = beg; it[0] != ':' || it[1] != ']'; ++it)
1974 if (!--limit)
1975 return -1;
1977 *strp = (const unsigned char *)(it + 2);
1979 /* Sort tests in the length=five case by frequency the classes to minimize
1980 number of times we fail the comparison. The frequencies of character class
1981 names used in Emacs sources as of 2016-07-27:
1983 $ find \( -name \*.c -o -name \*.el \) -exec grep -h '\[:[a-z]*:]' {} + |
1984 sed 's/]/]\n/g' |grep -o '\[:[a-z]*:]' |sort |uniq -c |sort -nr
1985 213 [:alnum:]
1986 104 [:alpha:]
1987 62 [:space:]
1988 39 [:digit:]
1989 36 [:blank:]
1990 26 [:word:]
1991 26 [:upper:]
1992 21 [:lower:]
1993 10 [:xdigit:]
1994 10 [:punct:]
1995 10 [:ascii:]
1996 4 [:nonascii:]
1997 4 [:graph:]
1998 2 [:print:]
1999 2 [:cntrl:]
2000 1 [:ff:]
2002 If you update this list, consider also updating chain of or’ed conditions
2003 in execute_charset function.
2006 switch (it - beg) {
2007 case 4:
2008 if (!memcmp (beg, "word", 4)) return RECC_WORD;
2009 break;
2010 case 5:
2011 if (!memcmp (beg, "alnum", 5)) return RECC_ALNUM;
2012 if (!memcmp (beg, "alpha", 5)) return RECC_ALPHA;
2013 if (!memcmp (beg, "space", 5)) return RECC_SPACE;
2014 if (!memcmp (beg, "digit", 5)) return RECC_DIGIT;
2015 if (!memcmp (beg, "blank", 5)) return RECC_BLANK;
2016 if (!memcmp (beg, "upper", 5)) return RECC_UPPER;
2017 if (!memcmp (beg, "lower", 5)) return RECC_LOWER;
2018 if (!memcmp (beg, "punct", 5)) return RECC_PUNCT;
2019 if (!memcmp (beg, "ascii", 5)) return RECC_ASCII;
2020 if (!memcmp (beg, "graph", 5)) return RECC_GRAPH;
2021 if (!memcmp (beg, "print", 5)) return RECC_PRINT;
2022 if (!memcmp (beg, "cntrl", 5)) return RECC_CNTRL;
2023 break;
2024 case 6:
2025 if (!memcmp (beg, "xdigit", 6)) return RECC_XDIGIT;
2026 break;
2027 case 7:
2028 if (!memcmp (beg, "unibyte", 7)) return RECC_UNIBYTE;
2029 break;
2030 case 8:
2031 if (!memcmp (beg, "nonascii", 8)) return RECC_NONASCII;
2032 break;
2033 case 9:
2034 if (!memcmp (beg, "multibyte", 9)) return RECC_MULTIBYTE;
2035 break;
2038 return RECC_ERROR;
2041 /* True if CH is in the char class CC. */
2042 boolean
2043 re_iswctype (int ch, re_wctype_t cc)
2045 switch (cc)
2047 case RECC_ALNUM: return ISALNUM (ch) != 0;
2048 case RECC_ALPHA: return ISALPHA (ch) != 0;
2049 case RECC_BLANK: return ISBLANK (ch) != 0;
2050 case RECC_CNTRL: return ISCNTRL (ch) != 0;
2051 case RECC_DIGIT: return ISDIGIT (ch) != 0;
2052 case RECC_GRAPH: return ISGRAPH (ch) != 0;
2053 case RECC_LOWER: return ISLOWER (ch) != 0;
2054 case RECC_PRINT: return ISPRINT (ch) != 0;
2055 case RECC_PUNCT: return ISPUNCT (ch) != 0;
2056 case RECC_SPACE: return ISSPACE (ch) != 0;
2057 case RECC_UPPER: return ISUPPER (ch) != 0;
2058 case RECC_XDIGIT: return ISXDIGIT (ch) != 0;
2059 case RECC_ASCII: return IS_REAL_ASCII (ch) != 0;
2060 case RECC_NONASCII: return !IS_REAL_ASCII (ch);
2061 case RECC_UNIBYTE: return ISUNIBYTE (ch) != 0;
2062 case RECC_MULTIBYTE: return !ISUNIBYTE (ch);
2063 case RECC_WORD: return ISWORD (ch) != 0;
2064 case RECC_ERROR: return false;
2065 default:
2066 abort ();
2070 /* Return a bit-pattern to use in the range-table bits to match multibyte
2071 chars of class CC. */
2072 static int
2073 re_wctype_to_bit (re_wctype_t cc)
2075 switch (cc)
2077 case RECC_NONASCII:
2078 case RECC_MULTIBYTE: return BIT_MULTIBYTE;
2079 case RECC_ALPHA: return BIT_ALPHA;
2080 case RECC_ALNUM: return BIT_ALNUM;
2081 case RECC_WORD: return BIT_WORD;
2082 case RECC_LOWER: return BIT_LOWER;
2083 case RECC_UPPER: return BIT_UPPER;
2084 case RECC_PUNCT: return BIT_PUNCT;
2085 case RECC_SPACE: return BIT_SPACE;
2086 case RECC_GRAPH: return BIT_GRAPH;
2087 case RECC_PRINT: return BIT_PRINT;
2088 case RECC_ASCII: case RECC_DIGIT: case RECC_XDIGIT: case RECC_CNTRL:
2089 case RECC_BLANK: case RECC_UNIBYTE: case RECC_ERROR: return 0;
2090 default:
2091 abort ();
2094 #endif
2096 /* Filling in the work area of a range. */
2098 /* Actually extend the space in WORK_AREA. */
2100 static void
2101 extend_range_table_work_area (struct range_table_work_area *work_area)
2103 work_area->allocated += 16 * sizeof (int);
2104 work_area->table = realloc (work_area->table, work_area->allocated);
2107 #if 0
2108 #ifdef emacs
2110 /* Carefully find the ranges of codes that are equivalent
2111 under case conversion to the range start..end when passed through
2112 TRANSLATE. Handle the case where non-letters can come in between
2113 two upper-case letters (which happens in Latin-1).
2114 Also handle the case of groups of more than 2 case-equivalent chars.
2116 The basic method is to look at consecutive characters and see
2117 if they can form a run that can be handled as one.
2119 Returns -1 if successful, REG_ESPACE if ran out of space. */
2121 static int
2122 set_image_of_range_1 (struct range_table_work_area *work_area,
2123 re_wchar_t start, re_wchar_t end,
2124 RE_TRANSLATE_TYPE translate)
2126 /* `one_case' indicates a character, or a run of characters,
2127 each of which is an isolate (no case-equivalents).
2128 This includes all ASCII non-letters.
2130 `two_case' indicates a character, or a run of characters,
2131 each of which has two case-equivalent forms.
2132 This includes all ASCII letters.
2134 `strange' indicates a character that has more than one
2135 case-equivalent. */
2137 enum case_type {one_case, two_case, strange};
2139 /* Describe the run that is in progress,
2140 which the next character can try to extend.
2141 If run_type is strange, that means there really is no run.
2142 If run_type is one_case, then run_start...run_end is the run.
2143 If run_type is two_case, then the run is run_start...run_end,
2144 and the case-equivalents end at run_eqv_end. */
2146 enum case_type run_type = strange;
2147 int run_start, run_end, run_eqv_end;
2149 Lisp_Object eqv_table;
2151 if (!RE_TRANSLATE_P (translate))
2153 EXTEND_RANGE_TABLE (work_area, 2);
2154 work_area->table[work_area->used++] = (start);
2155 work_area->table[work_area->used++] = (end);
2156 return -1;
2159 eqv_table = XCHAR_TABLE (translate)->extras[2];
2161 for (; start <= end; start++)
2163 enum case_type this_type;
2164 int eqv = RE_TRANSLATE (eqv_table, start);
2165 int minchar, maxchar;
2167 /* Classify this character */
2168 if (eqv == start)
2169 this_type = one_case;
2170 else if (RE_TRANSLATE (eqv_table, eqv) == start)
2171 this_type = two_case;
2172 else
2173 this_type = strange;
2175 if (start < eqv)
2176 minchar = start, maxchar = eqv;
2177 else
2178 minchar = eqv, maxchar = start;
2180 /* Can this character extend the run in progress? */
2181 if (this_type == strange || this_type != run_type
2182 || !(minchar == run_end + 1
2183 && (run_type == two_case
2184 ? maxchar == run_eqv_end + 1 : 1)))
2186 /* No, end the run.
2187 Record each of its equivalent ranges. */
2188 if (run_type == one_case)
2190 EXTEND_RANGE_TABLE (work_area, 2);
2191 work_area->table[work_area->used++] = run_start;
2192 work_area->table[work_area->used++] = run_end;
2194 else if (run_type == two_case)
2196 EXTEND_RANGE_TABLE (work_area, 4);
2197 work_area->table[work_area->used++] = run_start;
2198 work_area->table[work_area->used++] = run_end;
2199 work_area->table[work_area->used++]
2200 = RE_TRANSLATE (eqv_table, run_start);
2201 work_area->table[work_area->used++]
2202 = RE_TRANSLATE (eqv_table, run_end);
2204 run_type = strange;
2207 if (this_type == strange)
2209 /* For a strange character, add each of its equivalents, one
2210 by one. Don't start a range. */
2213 EXTEND_RANGE_TABLE (work_area, 2);
2214 work_area->table[work_area->used++] = eqv;
2215 work_area->table[work_area->used++] = eqv;
2216 eqv = RE_TRANSLATE (eqv_table, eqv);
2218 while (eqv != start);
2221 /* Add this char to the run, or start a new run. */
2222 else if (run_type == strange)
2224 /* Initialize a new range. */
2225 run_type = this_type;
2226 run_start = start;
2227 run_end = start;
2228 run_eqv_end = RE_TRANSLATE (eqv_table, run_end);
2230 else
2232 /* Extend a running range. */
2233 run_end = minchar;
2234 run_eqv_end = RE_TRANSLATE (eqv_table, run_end);
2238 /* If a run is still in progress at the end, finish it now
2239 by recording its equivalent ranges. */
2240 if (run_type == one_case)
2242 EXTEND_RANGE_TABLE (work_area, 2);
2243 work_area->table[work_area->used++] = run_start;
2244 work_area->table[work_area->used++] = run_end;
2246 else if (run_type == two_case)
2248 EXTEND_RANGE_TABLE (work_area, 4);
2249 work_area->table[work_area->used++] = run_start;
2250 work_area->table[work_area->used++] = run_end;
2251 work_area->table[work_area->used++]
2252 = RE_TRANSLATE (eqv_table, run_start);
2253 work_area->table[work_area->used++]
2254 = RE_TRANSLATE (eqv_table, run_end);
2257 return -1;
2260 #endif /* emacs */
2262 /* Record the image of the range start..end when passed through
2263 TRANSLATE. This is not necessarily TRANSLATE(start)..TRANSLATE(end)
2264 and is not even necessarily contiguous.
2265 Normally we approximate it with the smallest contiguous range that contains
2266 all the chars we need. However, for Latin-1 we go to extra effort
2267 to do a better job.
2269 This function is not called for ASCII ranges.
2271 Returns -1 if successful, REG_ESPACE if ran out of space. */
2273 static int
2274 set_image_of_range (struct range_table_work_area *work_area,
2275 re_wchar_t start, re_wchar_t end,
2276 RE_TRANSLATE_TYPE translate)
2278 re_wchar_t cmin, cmax;
2280 #ifdef emacs
2281 /* For Latin-1 ranges, use set_image_of_range_1
2282 to get proper handling of ranges that include letters and nonletters.
2283 For a range that includes the whole of Latin-1, this is not necessary.
2284 For other character sets, we don't bother to get this right. */
2285 if (RE_TRANSLATE_P (translate) && start < 04400
2286 && !(start < 04200 && end >= 04377))
2288 int newend;
2289 int tem;
2290 newend = end;
2291 if (newend > 04377)
2292 newend = 04377;
2293 tem = set_image_of_range_1 (work_area, start, newend, translate);
2294 if (tem > 0)
2295 return tem;
2297 start = 04400;
2298 if (end < 04400)
2299 return -1;
2301 #endif
2303 EXTEND_RANGE_TABLE (work_area, 2);
2304 work_area->table[work_area->used++] = (start);
2305 work_area->table[work_area->used++] = (end);
2307 cmin = -1, cmax = -1;
2309 if (RE_TRANSLATE_P (translate))
2311 int ch;
2313 for (ch = start; ch <= end; ch++)
2315 re_wchar_t c = TRANSLATE (ch);
2316 if (! (start <= c && c <= end))
2318 if (cmin == -1)
2319 cmin = c, cmax = c;
2320 else
2322 cmin = min (cmin, c);
2323 cmax = max (cmax, c);
2328 if (cmin != -1)
2330 EXTEND_RANGE_TABLE (work_area, 2);
2331 work_area->table[work_area->used++] = (cmin);
2332 work_area->table[work_area->used++] = (cmax);
2336 return -1;
2338 #endif /* 0 */
2340 #ifndef MATCH_MAY_ALLOCATE
2342 /* If we cannot allocate large objects within re_match_2_internal,
2343 we make the fail stack and register vectors global.
2344 The fail stack, we grow to the maximum size when a regexp
2345 is compiled.
2346 The register vectors, we adjust in size each time we
2347 compile a regexp, according to the number of registers it needs. */
2349 static fail_stack_type fail_stack;
2351 /* Size with which the following vectors are currently allocated.
2352 That is so we can make them bigger as needed,
2353 but never make them smaller. */
2354 static int regs_allocated_size;
2356 static re_char ** regstart, ** regend;
2357 static re_char **best_regstart, **best_regend;
2359 /* Make the register vectors big enough for NUM_REGS registers,
2360 but don't make them smaller. */
2362 static
2363 regex_grow_registers (int num_regs)
2365 if (num_regs > regs_allocated_size)
2367 RETALLOC_IF (regstart, num_regs, re_char *);
2368 RETALLOC_IF (regend, num_regs, re_char *);
2369 RETALLOC_IF (best_regstart, num_regs, re_char *);
2370 RETALLOC_IF (best_regend, num_regs, re_char *);
2372 regs_allocated_size = num_regs;
2376 #endif /* not MATCH_MAY_ALLOCATE */
2378 static boolean group_in_compile_stack (compile_stack_type compile_stack,
2379 regnum_t regnum);
2381 /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
2382 Returns one of error codes defined in `regex.h', or zero for success.
2384 If WHITESPACE_REGEXP is given (only #ifdef emacs), it is used instead of
2385 a space character in PATTERN.
2387 Assumes the `allocated' (and perhaps `buffer') and `translate'
2388 fields are set in BUFP on entry.
2390 If it succeeds, results are put in BUFP (if it returns an error, the
2391 contents of BUFP are undefined):
2392 `buffer' is the compiled pattern;
2393 `syntax' is set to SYNTAX;
2394 `used' is set to the length of the compiled pattern;
2395 `fastmap_accurate' is zero;
2396 `re_nsub' is the number of subexpressions in PATTERN;
2397 `not_bol' and `not_eol' are zero;
2399 The `fastmap' field is neither examined nor set. */
2401 /* Insert the `jump' from the end of last alternative to "here".
2402 The space for the jump has already been allocated. */
2403 #define FIXUP_ALT_JUMP() \
2404 do { \
2405 if (fixup_alt_jump) \
2406 STORE_JUMP (jump, fixup_alt_jump, b); \
2407 } while (0)
2410 /* Return, freeing storage we allocated. */
2411 #define FREE_STACK_RETURN(value) \
2412 do { \
2413 FREE_RANGE_TABLE_WORK_AREA (range_table_work); \
2414 free (compile_stack.stack); \
2415 return value; \
2416 } while (0)
2418 static reg_errcode_t
2419 regex_compile (const_re_char *pattern, size_t size,
2420 #ifdef emacs
2421 # define syntax RE_SYNTAX_EMACS
2422 bool posix_backtracking,
2423 const char *whitespace_regexp,
2424 #else
2425 reg_syntax_t syntax,
2426 # define posix_backtracking (!(syntax & RE_NO_POSIX_BACKTRACKING))
2427 #endif
2428 struct re_pattern_buffer *bufp)
2430 /* We fetch characters from PATTERN here. */
2431 register re_wchar_t c, c1;
2433 /* Points to the end of the buffer, where we should append. */
2434 register unsigned char *b;
2436 /* Keeps track of unclosed groups. */
2437 compile_stack_type compile_stack;
2439 /* Points to the current (ending) position in the pattern. */
2440 #ifdef AIX
2441 /* `const' makes AIX compiler fail. */
2442 unsigned char *p = pattern;
2443 #else
2444 re_char *p = pattern;
2445 #endif
2446 re_char *pend = pattern + size;
2448 /* How to translate the characters in the pattern. */
2449 RE_TRANSLATE_TYPE translate = bufp->translate;
2451 /* Address of the count-byte of the most recently inserted `exactn'
2452 command. This makes it possible to tell if a new exact-match
2453 character can be added to that command or if the character requires
2454 a new `exactn' command. */
2455 unsigned char *pending_exact = 0;
2457 /* Address of start of the most recently finished expression.
2458 This tells, e.g., postfix * where to find the start of its
2459 operand. Reset at the beginning of groups and alternatives. */
2460 unsigned char *laststart = 0;
2462 /* Address of beginning of regexp, or inside of last group. */
2463 unsigned char *begalt;
2465 /* Place in the uncompiled pattern (i.e., the {) to
2466 which to go back if the interval is invalid. */
2467 re_char *beg_interval;
2469 /* Address of the place where a forward jump should go to the end of
2470 the containing expression. Each alternative of an `or' -- except the
2471 last -- ends with a forward jump of this sort. */
2472 unsigned char *fixup_alt_jump = 0;
2474 /* Work area for range table of charset. */
2475 struct range_table_work_area range_table_work;
2477 /* If the object matched can contain multibyte characters. */
2478 const boolean multibyte = RE_MULTIBYTE_P (bufp);
2480 #ifdef emacs
2481 /* Nonzero if we have pushed down into a subpattern. */
2482 int in_subpattern = 0;
2484 /* These hold the values of p, pattern, and pend from the main
2485 pattern when we have pushed into a subpattern. */
2486 re_char *main_p;
2487 re_char *main_pattern;
2488 re_char *main_pend;
2489 #endif
2491 #ifdef DEBUG
2492 debug++;
2493 DEBUG_PRINT ("\nCompiling pattern: ");
2494 if (debug > 0)
2496 unsigned debug_count;
2498 for (debug_count = 0; debug_count < size; debug_count++)
2499 putchar (pattern[debug_count]);
2500 putchar ('\n');
2502 #endif /* DEBUG */
2504 /* Initialize the compile stack. */
2505 compile_stack.stack = TALLOC (INIT_COMPILE_STACK_SIZE, compile_stack_elt_t);
2506 if (compile_stack.stack == NULL)
2507 return REG_ESPACE;
2509 compile_stack.size = INIT_COMPILE_STACK_SIZE;
2510 compile_stack.avail = 0;
2512 range_table_work.table = 0;
2513 range_table_work.allocated = 0;
2515 /* Initialize the pattern buffer. */
2516 #ifndef emacs
2517 bufp->syntax = syntax;
2518 #endif
2519 bufp->fastmap_accurate = 0;
2520 bufp->not_bol = bufp->not_eol = 0;
2521 bufp->used_syntax = 0;
2523 /* Set `used' to zero, so that if we return an error, the pattern
2524 printer (for debugging) will think there's no pattern. We reset it
2525 at the end. */
2526 bufp->used = 0;
2528 /* Always count groups, whether or not bufp->no_sub is set. */
2529 bufp->re_nsub = 0;
2531 #if !defined emacs && !defined SYNTAX_TABLE
2532 /* Initialize the syntax table. */
2533 init_syntax_once ();
2534 #endif
2536 if (bufp->allocated == 0)
2538 if (bufp->buffer)
2539 { /* If zero allocated, but buffer is non-null, try to realloc
2540 enough space. This loses if buffer's address is bogus, but
2541 that is the user's responsibility. */
2542 RETALLOC (bufp->buffer, INIT_BUF_SIZE, unsigned char);
2544 else
2545 { /* Caller did not allocate a buffer. Do it for them. */
2546 bufp->buffer = TALLOC (INIT_BUF_SIZE, unsigned char);
2548 if (!bufp->buffer) FREE_STACK_RETURN (REG_ESPACE);
2550 bufp->allocated = INIT_BUF_SIZE;
2553 begalt = b = bufp->buffer;
2555 /* Loop through the uncompiled pattern until we're at the end. */
2556 while (1)
2558 if (p == pend)
2560 #ifdef emacs
2561 /* If this is the end of an included regexp,
2562 pop back to the main regexp and try again. */
2563 if (in_subpattern)
2565 in_subpattern = 0;
2566 pattern = main_pattern;
2567 p = main_p;
2568 pend = main_pend;
2569 continue;
2571 #endif
2572 /* If this is the end of the main regexp, we are done. */
2573 break;
2576 PATFETCH (c);
2578 switch (c)
2580 #ifdef emacs
2581 case ' ':
2583 re_char *p1 = p;
2585 /* If there's no special whitespace regexp, treat
2586 spaces normally. And don't try to do this recursively. */
2587 if (!whitespace_regexp || in_subpattern)
2588 goto normal_char;
2590 /* Peek past following spaces. */
2591 while (p1 != pend)
2593 if (*p1 != ' ')
2594 break;
2595 p1++;
2597 /* If the spaces are followed by a repetition op,
2598 treat them normally. */
2599 if (p1 != pend
2600 && (*p1 == '*' || *p1 == '+' || *p1 == '?'
2601 || (*p1 == '\\' && p1 + 1 != pend && p1[1] == '{')))
2602 goto normal_char;
2604 /* Replace the spaces with the whitespace regexp. */
2605 in_subpattern = 1;
2606 main_p = p1;
2607 main_pend = pend;
2608 main_pattern = pattern;
2609 p = pattern = (re_char *) whitespace_regexp;
2610 pend = p + strlen (whitespace_regexp);
2611 break;
2613 #endif
2615 case '^':
2617 if ( /* If at start of pattern, it's an operator. */
2618 p == pattern + 1
2619 /* If context independent, it's an operator. */
2620 || syntax & RE_CONTEXT_INDEP_ANCHORS
2621 /* Otherwise, depends on what's come before. */
2622 || at_begline_loc_p (pattern, p, syntax))
2623 BUF_PUSH ((syntax & RE_NO_NEWLINE_ANCHOR) ? begbuf : begline);
2624 else
2625 goto normal_char;
2627 break;
2630 case '$':
2632 if ( /* If at end of pattern, it's an operator. */
2633 p == pend
2634 /* If context independent, it's an operator. */
2635 || syntax & RE_CONTEXT_INDEP_ANCHORS
2636 /* Otherwise, depends on what's next. */
2637 || at_endline_loc_p (p, pend, syntax))
2638 BUF_PUSH ((syntax & RE_NO_NEWLINE_ANCHOR) ? endbuf : endline);
2639 else
2640 goto normal_char;
2642 break;
2645 case '+':
2646 case '?':
2647 if ((syntax & RE_BK_PLUS_QM)
2648 || (syntax & RE_LIMITED_OPS))
2649 goto normal_char;
2650 handle_plus:
2651 case '*':
2652 /* If there is no previous pattern... */
2653 if (!laststart)
2655 if (syntax & RE_CONTEXT_INVALID_OPS)
2656 FREE_STACK_RETURN (REG_BADRPT);
2657 else if (!(syntax & RE_CONTEXT_INDEP_OPS))
2658 goto normal_char;
2662 /* 1 means zero (many) matches is allowed. */
2663 boolean zero_times_ok = 0, many_times_ok = 0;
2664 boolean greedy = 1;
2666 /* If there is a sequence of repetition chars, collapse it
2667 down to just one (the right one). We can't combine
2668 interval operators with these because of, e.g., `a{2}*',
2669 which should only match an even number of `a's. */
2671 for (;;)
2673 if ((syntax & RE_FRUGAL)
2674 && c == '?' && (zero_times_ok || many_times_ok))
2675 greedy = 0;
2676 else
2678 zero_times_ok |= c != '+';
2679 many_times_ok |= c != '?';
2682 if (p == pend)
2683 break;
2684 else if (*p == '*'
2685 || (!(syntax & RE_BK_PLUS_QM)
2686 && (*p == '+' || *p == '?')))
2688 else if (syntax & RE_BK_PLUS_QM && *p == '\\')
2690 if (p+1 == pend)
2691 FREE_STACK_RETURN (REG_EESCAPE);
2692 if (p[1] == '+' || p[1] == '?')
2693 PATFETCH (c); /* Gobble up the backslash. */
2694 else
2695 break;
2697 else
2698 break;
2699 /* If we get here, we found another repeat character. */
2700 PATFETCH (c);
2703 /* Star, etc. applied to an empty pattern is equivalent
2704 to an empty pattern. */
2705 if (!laststart || laststart == b)
2706 break;
2708 /* Now we know whether or not zero matches is allowed
2709 and also whether or not two or more matches is allowed. */
2710 if (greedy)
2712 if (many_times_ok)
2714 boolean simple = skip_one_char (laststart) == b;
2715 size_t startoffset = 0;
2716 re_opcode_t ofj =
2717 /* Check if the loop can match the empty string. */
2718 (simple || !analyze_first (laststart, b, NULL, 0))
2719 ? on_failure_jump : on_failure_jump_loop;
2720 assert (skip_one_char (laststart) <= b);
2722 if (!zero_times_ok && simple)
2723 { /* Since simple * loops can be made faster by using
2724 on_failure_keep_string_jump, we turn simple P+
2725 into PP* if P is simple. */
2726 unsigned char *p1, *p2;
2727 startoffset = b - laststart;
2728 GET_BUFFER_SPACE (startoffset);
2729 p1 = b; p2 = laststart;
2730 while (p2 < p1)
2731 *b++ = *p2++;
2732 zero_times_ok = 1;
2735 GET_BUFFER_SPACE (6);
2736 if (!zero_times_ok)
2737 /* A + loop. */
2738 STORE_JUMP (ofj, b, b + 6);
2739 else
2740 /* Simple * loops can use on_failure_keep_string_jump
2741 depending on what follows. But since we don't know
2742 that yet, we leave the decision up to
2743 on_failure_jump_smart. */
2744 INSERT_JUMP (simple ? on_failure_jump_smart : ofj,
2745 laststart + startoffset, b + 6);
2746 b += 3;
2747 STORE_JUMP (jump, b, laststart + startoffset);
2748 b += 3;
2750 else
2752 /* A simple ? pattern. */
2753 assert (zero_times_ok);
2754 GET_BUFFER_SPACE (3);
2755 INSERT_JUMP (on_failure_jump, laststart, b + 3);
2756 b += 3;
2759 else /* not greedy */
2760 { /* I wish the greedy and non-greedy cases could be merged. */
2762 GET_BUFFER_SPACE (7); /* We might use less. */
2763 if (many_times_ok)
2765 boolean emptyp = analyze_first (laststart, b, NULL, 0);
2767 /* The non-greedy multiple match looks like
2768 a repeat..until: we only need a conditional jump
2769 at the end of the loop. */
2770 if (emptyp) BUF_PUSH (no_op);
2771 STORE_JUMP (emptyp ? on_failure_jump_nastyloop
2772 : on_failure_jump, b, laststart);
2773 b += 3;
2774 if (zero_times_ok)
2776 /* The repeat...until naturally matches one or more.
2777 To also match zero times, we need to first jump to
2778 the end of the loop (its conditional jump). */
2779 INSERT_JUMP (jump, laststart, b);
2780 b += 3;
2783 else
2785 /* non-greedy a?? */
2786 INSERT_JUMP (jump, laststart, b + 3);
2787 b += 3;
2788 INSERT_JUMP (on_failure_jump, laststart, laststart + 6);
2789 b += 3;
2793 pending_exact = 0;
2794 break;
2797 case '.':
2798 laststart = b;
2799 BUF_PUSH (anychar);
2800 break;
2803 case '[':
2805 re_char *p1;
2807 CLEAR_RANGE_TABLE_WORK_USED (range_table_work);
2809 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2811 /* Ensure that we have enough space to push a charset: the
2812 opcode, the length count, and the bitset; 34 bytes in all. */
2813 GET_BUFFER_SPACE (34);
2815 laststart = b;
2817 /* We test `*p == '^' twice, instead of using an if
2818 statement, so we only need one BUF_PUSH. */
2819 BUF_PUSH (*p == '^' ? charset_not : charset);
2820 if (*p == '^')
2821 p++;
2823 /* Remember the first position in the bracket expression. */
2824 p1 = p;
2826 /* Push the number of bytes in the bitmap. */
2827 BUF_PUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
2829 /* Clear the whole map. */
2830 memset (b, 0, (1 << BYTEWIDTH) / BYTEWIDTH);
2832 /* charset_not matches newline according to a syntax bit. */
2833 if ((re_opcode_t) b[-2] == charset_not
2834 && (syntax & RE_HAT_LISTS_NOT_NEWLINE))
2835 SET_LIST_BIT ('\n');
2837 /* Read in characters and ranges, setting map bits. */
2838 for (;;)
2840 boolean escaped_char = false;
2841 const unsigned char *p2 = p;
2842 re_wctype_t cc;
2843 re_wchar_t ch;
2845 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2847 /* See if we're at the beginning of a possible character
2848 class. */
2849 if (syntax & RE_CHAR_CLASSES &&
2850 (cc = re_wctype_parse(&p, pend - p)) != -1)
2852 if (cc == 0)
2853 FREE_STACK_RETURN (REG_ECTYPE);
2855 if (p == pend)
2856 FREE_STACK_RETURN (REG_EBRACK);
2858 #ifndef emacs
2859 for (ch = 0; ch < (1 << BYTEWIDTH); ++ch)
2860 if (re_iswctype (btowc (ch), cc))
2862 c = TRANSLATE (ch);
2863 if (c < (1 << BYTEWIDTH))
2864 SET_LIST_BIT (c);
2866 #else /* emacs */
2867 /* Most character classes in a multibyte match just set
2868 a flag. Exceptions are is_blank, is_digit, is_cntrl, and
2869 is_xdigit, since they can only match ASCII characters.
2870 We don't need to handle them for multibyte. */
2872 /* Setup the gl_state object to its buffer-defined value.
2873 This hardcodes the buffer-global syntax-table for ASCII
2874 chars, while the other chars will obey syntax-table
2875 properties. It's not ideal, but it's the way it's been
2876 done until now. */
2877 SETUP_BUFFER_SYNTAX_TABLE ();
2879 for (c = 0; c < 0x80; ++c)
2880 if (re_iswctype (c, cc))
2882 SET_LIST_BIT (c);
2883 c1 = TRANSLATE (c);
2884 if (c1 == c)
2885 continue;
2886 if (ASCII_CHAR_P (c1))
2887 SET_LIST_BIT (c1);
2888 else if ((c1 = RE_CHAR_TO_UNIBYTE (c1)) >= 0)
2889 SET_LIST_BIT (c1);
2891 SET_RANGE_TABLE_WORK_AREA_BIT
2892 (range_table_work, re_wctype_to_bit (cc));
2893 #endif /* emacs */
2894 /* In most cases the matching rule for char classes only
2895 uses the syntax table for multibyte chars, so that the
2896 content of the syntax-table is not hardcoded in the
2897 range_table. SPACE and WORD are the two exceptions. */
2898 if ((1 << cc) & ((1 << RECC_SPACE) | (1 << RECC_WORD)))
2899 bufp->used_syntax = 1;
2901 /* Repeat the loop. */
2902 continue;
2905 /* Don't translate yet. The range TRANSLATE(X..Y) cannot
2906 always be determined from TRANSLATE(X) and TRANSLATE(Y)
2907 So the translation is done later in a loop. Example:
2908 (let ((case-fold-search t)) (string-match "[A-_]" "A")) */
2909 PATFETCH (c);
2911 /* \ might escape characters inside [...] and [^...]. */
2912 if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) && c == '\\')
2914 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
2916 PATFETCH (c);
2917 escaped_char = true;
2919 else
2921 /* Could be the end of the bracket expression. If it's
2922 not (i.e., when the bracket expression is `[]' so
2923 far), the ']' character bit gets set way below. */
2924 if (c == ']' && p2 != p1)
2925 break;
2928 if (p < pend && p[0] == '-' && p[1] != ']')
2931 /* Discard the `-'. */
2932 PATFETCH (c1);
2934 /* Fetch the character which ends the range. */
2935 PATFETCH (c1);
2936 #ifdef emacs
2937 if (CHAR_BYTE8_P (c1)
2938 && ! ASCII_CHAR_P (c) && ! CHAR_BYTE8_P (c))
2939 /* Treat the range from a multibyte character to
2940 raw-byte character as empty. */
2941 c = c1 + 1;
2942 #endif /* emacs */
2944 else
2945 /* Range from C to C. */
2946 c1 = c;
2948 if (c > c1)
2950 if (syntax & RE_NO_EMPTY_RANGES)
2951 FREE_STACK_RETURN (REG_ERANGEX);
2952 /* Else, repeat the loop. */
2954 else
2956 #ifndef emacs
2957 /* Set the range into bitmap */
2958 for (; c <= c1; c++)
2960 ch = TRANSLATE (c);
2961 if (ch < (1 << BYTEWIDTH))
2962 SET_LIST_BIT (ch);
2964 #else /* emacs */
2965 if (c < 128)
2967 ch = min (127, c1);
2968 SETUP_ASCII_RANGE (range_table_work, c, ch);
2969 c = ch + 1;
2970 if (CHAR_BYTE8_P (c1))
2971 c = BYTE8_TO_CHAR (128);
2973 if (c <= c1)
2975 if (CHAR_BYTE8_P (c))
2977 c = CHAR_TO_BYTE8 (c);
2978 c1 = CHAR_TO_BYTE8 (c1);
2979 for (; c <= c1; c++)
2980 SET_LIST_BIT (c);
2982 else if (multibyte)
2984 SETUP_MULTIBYTE_RANGE (range_table_work, c, c1);
2986 else
2988 SETUP_UNIBYTE_RANGE (range_table_work, c, c1);
2991 #endif /* emacs */
2995 /* Discard any (non)matching list bytes that are all 0 at the
2996 end of the map. Decrease the map-length byte too. */
2997 while ((int) b[-1] > 0 && b[b[-1] - 1] == 0)
2998 b[-1]--;
2999 b += b[-1];
3001 /* Build real range table from work area. */
3002 if (RANGE_TABLE_WORK_USED (range_table_work)
3003 || RANGE_TABLE_WORK_BITS (range_table_work))
3005 int i;
3006 int used = RANGE_TABLE_WORK_USED (range_table_work);
3008 /* Allocate space for COUNT + RANGE_TABLE. Needs two
3009 bytes for flags, two for COUNT, and three bytes for
3010 each character. */
3011 GET_BUFFER_SPACE (4 + used * 3);
3013 /* Indicate the existence of range table. */
3014 laststart[1] |= 0x80;
3016 /* Store the character class flag bits into the range table.
3017 If not in emacs, these flag bits are always 0. */
3018 *b++ = RANGE_TABLE_WORK_BITS (range_table_work) & 0xff;
3019 *b++ = RANGE_TABLE_WORK_BITS (range_table_work) >> 8;
3021 STORE_NUMBER_AND_INCR (b, used / 2);
3022 for (i = 0; i < used; i++)
3023 STORE_CHARACTER_AND_INCR
3024 (b, RANGE_TABLE_WORK_ELT (range_table_work, i));
3027 break;
3030 case '(':
3031 if (syntax & RE_NO_BK_PARENS)
3032 goto handle_open;
3033 else
3034 goto normal_char;
3037 case ')':
3038 if (syntax & RE_NO_BK_PARENS)
3039 goto handle_close;
3040 else
3041 goto normal_char;
3044 case '\n':
3045 if (syntax & RE_NEWLINE_ALT)
3046 goto handle_alt;
3047 else
3048 goto normal_char;
3051 case '|':
3052 if (syntax & RE_NO_BK_VBAR)
3053 goto handle_alt;
3054 else
3055 goto normal_char;
3058 case '{':
3059 if (syntax & RE_INTERVALS && syntax & RE_NO_BK_BRACES)
3060 goto handle_interval;
3061 else
3062 goto normal_char;
3065 case '\\':
3066 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
3068 /* Do not translate the character after the \, so that we can
3069 distinguish, e.g., \B from \b, even if we normally would
3070 translate, e.g., B to b. */
3071 PATFETCH (c);
3073 switch (c)
3075 case '(':
3076 if (syntax & RE_NO_BK_PARENS)
3077 goto normal_backslash;
3079 handle_open:
3081 int shy = 0;
3082 regnum_t regnum = 0;
3083 if (p+1 < pend)
3085 /* Look for a special (?...) construct */
3086 if ((syntax & RE_SHY_GROUPS) && *p == '?')
3088 PATFETCH (c); /* Gobble up the '?'. */
3089 while (!shy)
3091 PATFETCH (c);
3092 switch (c)
3094 case ':': shy = 1; break;
3095 case '0':
3096 /* An explicitly specified regnum must start
3097 with non-0. */
3098 if (regnum == 0)
3099 FREE_STACK_RETURN (REG_BADPAT);
3100 case '1': case '2': case '3': case '4':
3101 case '5': case '6': case '7': case '8': case '9':
3102 regnum = 10*regnum + (c - '0'); break;
3103 default:
3104 /* Only (?:...) is supported right now. */
3105 FREE_STACK_RETURN (REG_BADPAT);
3111 if (!shy)
3112 regnum = ++bufp->re_nsub;
3113 else if (regnum)
3114 { /* It's actually not shy, but explicitly numbered. */
3115 shy = 0;
3116 if (regnum > bufp->re_nsub)
3117 bufp->re_nsub = regnum;
3118 else if (regnum > bufp->re_nsub
3119 /* Ideally, we'd want to check that the specified
3120 group can't have matched (i.e. all subgroups
3121 using the same regnum are in other branches of
3122 OR patterns), but we don't currently keep track
3123 of enough info to do that easily. */
3124 || group_in_compile_stack (compile_stack, regnum))
3125 FREE_STACK_RETURN (REG_BADPAT);
3127 else
3128 /* It's really shy. */
3129 regnum = - bufp->re_nsub;
3131 if (COMPILE_STACK_FULL)
3133 RETALLOC (compile_stack.stack, compile_stack.size << 1,
3134 compile_stack_elt_t);
3135 if (compile_stack.stack == NULL) return REG_ESPACE;
3137 compile_stack.size <<= 1;
3140 /* These are the values to restore when we hit end of this
3141 group. They are all relative offsets, so that if the
3142 whole pattern moves because of realloc, they will still
3143 be valid. */
3144 COMPILE_STACK_TOP.begalt_offset = begalt - bufp->buffer;
3145 COMPILE_STACK_TOP.fixup_alt_jump
3146 = fixup_alt_jump ? fixup_alt_jump - bufp->buffer + 1 : 0;
3147 COMPILE_STACK_TOP.laststart_offset = b - bufp->buffer;
3148 COMPILE_STACK_TOP.regnum = regnum;
3150 /* Do not push a start_memory for groups beyond the last one
3151 we can represent in the compiled pattern. */
3152 if (regnum <= MAX_REGNUM && regnum > 0)
3153 BUF_PUSH_2 (start_memory, regnum);
3155 compile_stack.avail++;
3157 fixup_alt_jump = 0;
3158 laststart = 0;
3159 begalt = b;
3160 /* If we've reached MAX_REGNUM groups, then this open
3161 won't actually generate any code, so we'll have to
3162 clear pending_exact explicitly. */
3163 pending_exact = 0;
3164 break;
3167 case ')':
3168 if (syntax & RE_NO_BK_PARENS) goto normal_backslash;
3170 if (COMPILE_STACK_EMPTY)
3172 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
3173 goto normal_backslash;
3174 else
3175 FREE_STACK_RETURN (REG_ERPAREN);
3178 handle_close:
3179 FIXUP_ALT_JUMP ();
3181 /* See similar code for backslashed left paren above. */
3182 if (COMPILE_STACK_EMPTY)
3184 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
3185 goto normal_char;
3186 else
3187 FREE_STACK_RETURN (REG_ERPAREN);
3190 /* Since we just checked for an empty stack above, this
3191 ``can't happen''. */
3192 assert (compile_stack.avail != 0);
3194 /* We don't just want to restore into `regnum', because
3195 later groups should continue to be numbered higher,
3196 as in `(ab)c(de)' -- the second group is #2. */
3197 regnum_t regnum;
3199 compile_stack.avail--;
3200 begalt = bufp->buffer + COMPILE_STACK_TOP.begalt_offset;
3201 fixup_alt_jump
3202 = COMPILE_STACK_TOP.fixup_alt_jump
3203 ? bufp->buffer + COMPILE_STACK_TOP.fixup_alt_jump - 1
3204 : 0;
3205 laststart = bufp->buffer + COMPILE_STACK_TOP.laststart_offset;
3206 regnum = COMPILE_STACK_TOP.regnum;
3207 /* If we've reached MAX_REGNUM groups, then this open
3208 won't actually generate any code, so we'll have to
3209 clear pending_exact explicitly. */
3210 pending_exact = 0;
3212 /* We're at the end of the group, so now we know how many
3213 groups were inside this one. */
3214 if (regnum <= MAX_REGNUM && regnum > 0)
3215 BUF_PUSH_2 (stop_memory, regnum);
3217 break;
3220 case '|': /* `\|'. */
3221 if (syntax & RE_LIMITED_OPS || syntax & RE_NO_BK_VBAR)
3222 goto normal_backslash;
3223 handle_alt:
3224 if (syntax & RE_LIMITED_OPS)
3225 goto normal_char;
3227 /* Insert before the previous alternative a jump which
3228 jumps to this alternative if the former fails. */
3229 GET_BUFFER_SPACE (3);
3230 INSERT_JUMP (on_failure_jump, begalt, b + 6);
3231 pending_exact = 0;
3232 b += 3;
3234 /* The alternative before this one has a jump after it
3235 which gets executed if it gets matched. Adjust that
3236 jump so it will jump to this alternative's analogous
3237 jump (put in below, which in turn will jump to the next
3238 (if any) alternative's such jump, etc.). The last such
3239 jump jumps to the correct final destination. A picture:
3240 _____ _____
3241 | | | |
3242 | v | v
3243 a | b | c
3245 If we are at `b', then fixup_alt_jump right now points to a
3246 three-byte space after `a'. We'll put in the jump, set
3247 fixup_alt_jump to right after `b', and leave behind three
3248 bytes which we'll fill in when we get to after `c'. */
3250 FIXUP_ALT_JUMP ();
3252 /* Mark and leave space for a jump after this alternative,
3253 to be filled in later either by next alternative or
3254 when know we're at the end of a series of alternatives. */
3255 fixup_alt_jump = b;
3256 GET_BUFFER_SPACE (3);
3257 b += 3;
3259 laststart = 0;
3260 begalt = b;
3261 break;
3264 case '{':
3265 /* If \{ is a literal. */
3266 if (!(syntax & RE_INTERVALS)
3267 /* If we're at `\{' and it's not the open-interval
3268 operator. */
3269 || (syntax & RE_NO_BK_BRACES))
3270 goto normal_backslash;
3272 handle_interval:
3274 /* If got here, then the syntax allows intervals. */
3276 /* At least (most) this many matches must be made. */
3277 int lower_bound = 0, upper_bound = -1;
3279 beg_interval = p;
3281 GET_INTERVAL_COUNT (lower_bound);
3283 if (c == ',')
3284 GET_INTERVAL_COUNT (upper_bound);
3285 else
3286 /* Interval such as `{1}' => match exactly once. */
3287 upper_bound = lower_bound;
3289 if (lower_bound < 0
3290 || (0 <= upper_bound && upper_bound < lower_bound))
3291 FREE_STACK_RETURN (REG_BADBR);
3293 if (!(syntax & RE_NO_BK_BRACES))
3295 if (c != '\\')
3296 FREE_STACK_RETURN (REG_BADBR);
3297 if (p == pend)
3298 FREE_STACK_RETURN (REG_EESCAPE);
3299 PATFETCH (c);
3302 if (c != '}')
3303 FREE_STACK_RETURN (REG_BADBR);
3305 /* We just parsed a valid interval. */
3307 /* If it's invalid to have no preceding re. */
3308 if (!laststart)
3310 if (syntax & RE_CONTEXT_INVALID_OPS)
3311 FREE_STACK_RETURN (REG_BADRPT);
3312 else if (syntax & RE_CONTEXT_INDEP_OPS)
3313 laststart = b;
3314 else
3315 goto unfetch_interval;
3318 if (upper_bound == 0)
3319 /* If the upper bound is zero, just drop the sub pattern
3320 altogether. */
3321 b = laststart;
3322 else if (lower_bound == 1 && upper_bound == 1)
3323 /* Just match it once: nothing to do here. */
3326 /* Otherwise, we have a nontrivial interval. When
3327 we're all done, the pattern will look like:
3328 set_number_at <jump count> <upper bound>
3329 set_number_at <succeed_n count> <lower bound>
3330 succeed_n <after jump addr> <succeed_n count>
3331 <body of loop>
3332 jump_n <succeed_n addr> <jump count>
3333 (The upper bound and `jump_n' are omitted if
3334 `upper_bound' is 1, though.) */
3335 else
3336 { /* If the upper bound is > 1, we need to insert
3337 more at the end of the loop. */
3338 unsigned int nbytes = (upper_bound < 0 ? 3
3339 : upper_bound > 1 ? 5 : 0);
3340 unsigned int startoffset = 0;
3342 GET_BUFFER_SPACE (20); /* We might use less. */
3344 if (lower_bound == 0)
3346 /* A succeed_n that starts with 0 is really a
3347 a simple on_failure_jump_loop. */
3348 INSERT_JUMP (on_failure_jump_loop, laststart,
3349 b + 3 + nbytes);
3350 b += 3;
3352 else
3354 /* Initialize lower bound of the `succeed_n', even
3355 though it will be set during matching by its
3356 attendant `set_number_at' (inserted next),
3357 because `re_compile_fastmap' needs to know.
3358 Jump to the `jump_n' we might insert below. */
3359 INSERT_JUMP2 (succeed_n, laststart,
3360 b + 5 + nbytes,
3361 lower_bound);
3362 b += 5;
3364 /* Code to initialize the lower bound. Insert
3365 before the `succeed_n'. The `5' is the last two
3366 bytes of this `set_number_at', plus 3 bytes of
3367 the following `succeed_n'. */
3368 insert_op2 (set_number_at, laststart, 5, lower_bound, b);
3369 b += 5;
3370 startoffset += 5;
3373 if (upper_bound < 0)
3375 /* A negative upper bound stands for infinity,
3376 in which case it degenerates to a plain jump. */
3377 STORE_JUMP (jump, b, laststart + startoffset);
3378 b += 3;
3380 else if (upper_bound > 1)
3381 { /* More than one repetition is allowed, so
3382 append a backward jump to the `succeed_n'
3383 that starts this interval.
3385 When we've reached this during matching,
3386 we'll have matched the interval once, so
3387 jump back only `upper_bound - 1' times. */
3388 STORE_JUMP2 (jump_n, b, laststart + startoffset,
3389 upper_bound - 1);
3390 b += 5;
3392 /* The location we want to set is the second
3393 parameter of the `jump_n'; that is `b-2' as
3394 an absolute address. `laststart' will be
3395 the `set_number_at' we're about to insert;
3396 `laststart+3' the number to set, the source
3397 for the relative address. But we are
3398 inserting into the middle of the pattern --
3399 so everything is getting moved up by 5.
3400 Conclusion: (b - 2) - (laststart + 3) + 5,
3401 i.e., b - laststart.
3403 We insert this at the beginning of the loop
3404 so that if we fail during matching, we'll
3405 reinitialize the bounds. */
3406 insert_op2 (set_number_at, laststart, b - laststart,
3407 upper_bound - 1, b);
3408 b += 5;
3411 pending_exact = 0;
3412 beg_interval = NULL;
3414 break;
3416 unfetch_interval:
3417 /* If an invalid interval, match the characters as literals. */
3418 assert (beg_interval);
3419 p = beg_interval;
3420 beg_interval = NULL;
3422 /* normal_char and normal_backslash need `c'. */
3423 c = '{';
3425 if (!(syntax & RE_NO_BK_BRACES))
3427 assert (p > pattern && p[-1] == '\\');
3428 goto normal_backslash;
3430 else
3431 goto normal_char;
3433 #ifdef emacs
3434 case '=':
3435 laststart = b;
3436 BUF_PUSH (at_dot);
3437 break;
3439 case 's':
3440 laststart = b;
3441 PATFETCH (c);
3442 BUF_PUSH_2 (syntaxspec, syntax_spec_code[c]);
3443 break;
3445 case 'S':
3446 laststart = b;
3447 PATFETCH (c);
3448 BUF_PUSH_2 (notsyntaxspec, syntax_spec_code[c]);
3449 break;
3451 case 'c':
3452 laststart = b;
3453 PATFETCH (c);
3454 BUF_PUSH_2 (categoryspec, c);
3455 break;
3457 case 'C':
3458 laststart = b;
3459 PATFETCH (c);
3460 BUF_PUSH_2 (notcategoryspec, c);
3461 break;
3462 #endif /* emacs */
3465 case 'w':
3466 if (syntax & RE_NO_GNU_OPS)
3467 goto normal_char;
3468 laststart = b;
3469 BUF_PUSH_2 (syntaxspec, Sword);
3470 break;
3473 case 'W':
3474 if (syntax & RE_NO_GNU_OPS)
3475 goto normal_char;
3476 laststart = b;
3477 BUF_PUSH_2 (notsyntaxspec, Sword);
3478 break;
3481 case '<':
3482 if (syntax & RE_NO_GNU_OPS)
3483 goto normal_char;
3484 laststart = b;
3485 BUF_PUSH (wordbeg);
3486 break;
3488 case '>':
3489 if (syntax & RE_NO_GNU_OPS)
3490 goto normal_char;
3491 laststart = b;
3492 BUF_PUSH (wordend);
3493 break;
3495 case '_':
3496 if (syntax & RE_NO_GNU_OPS)
3497 goto normal_char;
3498 laststart = b;
3499 PATFETCH (c);
3500 if (c == '<')
3501 BUF_PUSH (symbeg);
3502 else if (c == '>')
3503 BUF_PUSH (symend);
3504 else
3505 FREE_STACK_RETURN (REG_BADPAT);
3506 break;
3508 case 'b':
3509 if (syntax & RE_NO_GNU_OPS)
3510 goto normal_char;
3511 BUF_PUSH (wordbound);
3512 break;
3514 case 'B':
3515 if (syntax & RE_NO_GNU_OPS)
3516 goto normal_char;
3517 BUF_PUSH (notwordbound);
3518 break;
3520 case '`':
3521 if (syntax & RE_NO_GNU_OPS)
3522 goto normal_char;
3523 BUF_PUSH (begbuf);
3524 break;
3526 case '\'':
3527 if (syntax & RE_NO_GNU_OPS)
3528 goto normal_char;
3529 BUF_PUSH (endbuf);
3530 break;
3532 case '1': case '2': case '3': case '4': case '5':
3533 case '6': case '7': case '8': case '9':
3535 regnum_t reg;
3537 if (syntax & RE_NO_BK_REFS)
3538 goto normal_backslash;
3540 reg = c - '0';
3542 if (reg > bufp->re_nsub || reg < 1
3543 /* Can't back reference to a subexp before its end. */
3544 || group_in_compile_stack (compile_stack, reg))
3545 FREE_STACK_RETURN (REG_ESUBREG);
3547 laststart = b;
3548 BUF_PUSH_2 (duplicate, reg);
3550 break;
3553 case '+':
3554 case '?':
3555 if (syntax & RE_BK_PLUS_QM)
3556 goto handle_plus;
3557 else
3558 goto normal_backslash;
3560 default:
3561 normal_backslash:
3562 /* You might think it would be useful for \ to mean
3563 not to translate; but if we don't translate it
3564 it will never match anything. */
3565 goto normal_char;
3567 break;
3570 default:
3571 /* Expects the character in `c'. */
3572 normal_char:
3573 /* If no exactn currently being built. */
3574 if (!pending_exact
3576 /* If last exactn not at current position. */
3577 || pending_exact + *pending_exact + 1 != b
3579 /* We have only one byte following the exactn for the count. */
3580 || *pending_exact >= (1 << BYTEWIDTH) - MAX_MULTIBYTE_LENGTH
3582 /* If followed by a repetition operator. */
3583 || (p != pend && (*p == '*' || *p == '^'))
3584 || ((syntax & RE_BK_PLUS_QM)
3585 ? p + 1 < pend && *p == '\\' && (p[1] == '+' || p[1] == '?')
3586 : p != pend && (*p == '+' || *p == '?'))
3587 || ((syntax & RE_INTERVALS)
3588 && ((syntax & RE_NO_BK_BRACES)
3589 ? p != pend && *p == '{'
3590 : p + 1 < pend && p[0] == '\\' && p[1] == '{')))
3592 /* Start building a new exactn. */
3594 laststart = b;
3596 BUF_PUSH_2 (exactn, 0);
3597 pending_exact = b - 1;
3600 GET_BUFFER_SPACE (MAX_MULTIBYTE_LENGTH);
3602 int len;
3604 if (multibyte)
3606 c = TRANSLATE (c);
3607 len = CHAR_STRING (c, b);
3608 b += len;
3610 else
3612 c1 = RE_CHAR_TO_MULTIBYTE (c);
3613 if (! CHAR_BYTE8_P (c1))
3615 re_wchar_t c2 = TRANSLATE (c1);
3617 if (c1 != c2 && (c1 = RE_CHAR_TO_UNIBYTE (c2)) >= 0)
3618 c = c1;
3620 *b++ = c;
3621 len = 1;
3623 (*pending_exact) += len;
3626 break;
3627 } /* switch (c) */
3628 } /* while p != pend */
3631 /* Through the pattern now. */
3633 FIXUP_ALT_JUMP ();
3635 if (!COMPILE_STACK_EMPTY)
3636 FREE_STACK_RETURN (REG_EPAREN);
3638 /* If we don't want backtracking, force success
3639 the first time we reach the end of the compiled pattern. */
3640 if (!posix_backtracking)
3641 BUF_PUSH (succeed);
3643 /* We have succeeded; set the length of the buffer. */
3644 bufp->used = b - bufp->buffer;
3646 #ifdef DEBUG
3647 if (debug > 0)
3649 re_compile_fastmap (bufp);
3650 DEBUG_PRINT ("\nCompiled pattern: \n");
3651 print_compiled_pattern (bufp);
3653 debug--;
3654 #endif /* DEBUG */
3656 #ifndef MATCH_MAY_ALLOCATE
3657 /* Initialize the failure stack to the largest possible stack. This
3658 isn't necessary unless we're trying to avoid calling alloca in
3659 the search and match routines. */
3661 int num_regs = bufp->re_nsub + 1;
3663 if (fail_stack.size < re_max_failures * TYPICAL_FAILURE_SIZE)
3665 fail_stack.size = re_max_failures * TYPICAL_FAILURE_SIZE;
3666 falk_stack.stack = realloc (fail_stack.stack,
3667 fail_stack.size * sizeof *falk_stack.stack);
3670 regex_grow_registers (num_regs);
3672 #endif /* not MATCH_MAY_ALLOCATE */
3674 FREE_STACK_RETURN (REG_NOERROR);
3676 #ifdef emacs
3677 # undef syntax
3678 #else
3679 # undef posix_backtracking
3680 #endif
3681 } /* regex_compile */
3683 /* Subroutines for `regex_compile'. */
3685 /* Store OP at LOC followed by two-byte integer parameter ARG. */
3687 static void
3688 store_op1 (re_opcode_t op, unsigned char *loc, int arg)
3690 *loc = (unsigned char) op;
3691 STORE_NUMBER (loc + 1, arg);
3695 /* Like `store_op1', but for two two-byte parameters ARG1 and ARG2. */
3697 static void
3698 store_op2 (re_opcode_t op, unsigned char *loc, int arg1, int arg2)
3700 *loc = (unsigned char) op;
3701 STORE_NUMBER (loc + 1, arg1);
3702 STORE_NUMBER (loc + 3, arg2);
3706 /* Copy the bytes from LOC to END to open up three bytes of space at LOC
3707 for OP followed by two-byte integer parameter ARG. */
3709 static void
3710 insert_op1 (re_opcode_t op, unsigned char *loc, int arg, unsigned char *end)
3712 register unsigned char *pfrom = end;
3713 register unsigned char *pto = end + 3;
3715 while (pfrom != loc)
3716 *--pto = *--pfrom;
3718 store_op1 (op, loc, arg);
3722 /* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2. */
3724 static void
3725 insert_op2 (re_opcode_t op, unsigned char *loc, int arg1, int arg2, unsigned char *end)
3727 register unsigned char *pfrom = end;
3728 register unsigned char *pto = end + 5;
3730 while (pfrom != loc)
3731 *--pto = *--pfrom;
3733 store_op2 (op, loc, arg1, arg2);
3737 /* P points to just after a ^ in PATTERN. Return true if that ^ comes
3738 after an alternative or a begin-subexpression. We assume there is at
3739 least one character before the ^. */
3741 static boolean
3742 at_begline_loc_p (const_re_char *pattern, const_re_char *p, reg_syntax_t syntax)
3744 re_char *prev = p - 2;
3745 boolean odd_backslashes;
3747 /* After a subexpression? */
3748 if (*prev == '(')
3749 odd_backslashes = (syntax & RE_NO_BK_PARENS) == 0;
3751 /* After an alternative? */
3752 else if (*prev == '|')
3753 odd_backslashes = (syntax & RE_NO_BK_VBAR) == 0;
3755 /* After a shy subexpression? */
3756 else if (*prev == ':' && (syntax & RE_SHY_GROUPS))
3758 /* Skip over optional regnum. */
3759 while (prev - 1 >= pattern && prev[-1] >= '0' && prev[-1] <= '9')
3760 --prev;
3762 if (!(prev - 2 >= pattern
3763 && prev[-1] == '?' && prev[-2] == '('))
3764 return false;
3765 prev -= 2;
3766 odd_backslashes = (syntax & RE_NO_BK_PARENS) == 0;
3768 else
3769 return false;
3771 /* Count the number of preceding backslashes. */
3772 p = prev;
3773 while (prev - 1 >= pattern && prev[-1] == '\\')
3774 --prev;
3775 return (p - prev) & odd_backslashes;
3779 /* The dual of at_begline_loc_p. This one is for $. We assume there is
3780 at least one character after the $, i.e., `P < PEND'. */
3782 static boolean
3783 at_endline_loc_p (const_re_char *p, const_re_char *pend, reg_syntax_t syntax)
3785 re_char *next = p;
3786 boolean next_backslash = *next == '\\';
3787 re_char *next_next = p + 1 < pend ? p + 1 : 0;
3789 return
3790 /* Before a subexpression? */
3791 (syntax & RE_NO_BK_PARENS ? *next == ')'
3792 : next_backslash && next_next && *next_next == ')')
3793 /* Before an alternative? */
3794 || (syntax & RE_NO_BK_VBAR ? *next == '|'
3795 : next_backslash && next_next && *next_next == '|');
3799 /* Returns true if REGNUM is in one of COMPILE_STACK's elements and
3800 false if it's not. */
3802 static boolean
3803 group_in_compile_stack (compile_stack_type compile_stack, regnum_t regnum)
3805 ssize_t this_element;
3807 for (this_element = compile_stack.avail - 1;
3808 this_element >= 0;
3809 this_element--)
3810 if (compile_stack.stack[this_element].regnum == regnum)
3811 return true;
3813 return false;
3816 /* analyze_first.
3817 If fastmap is non-NULL, go through the pattern and fill fastmap
3818 with all the possible leading chars. If fastmap is NULL, don't
3819 bother filling it up (obviously) and only return whether the
3820 pattern could potentially match the empty string.
3822 Return 1 if p..pend might match the empty string.
3823 Return 0 if p..pend matches at least one char.
3824 Return -1 if fastmap was not updated accurately. */
3826 static int
3827 analyze_first (const_re_char *p, const_re_char *pend, char *fastmap,
3828 const int multibyte)
3830 int j, k;
3831 boolean not;
3833 /* If all elements for base leading-codes in fastmap is set, this
3834 flag is set true. */
3835 boolean match_any_multibyte_characters = false;
3837 assert (p);
3839 /* The loop below works as follows:
3840 - It has a working-list kept in the PATTERN_STACK and which basically
3841 starts by only containing a pointer to the first operation.
3842 - If the opcode we're looking at is a match against some set of
3843 chars, then we add those chars to the fastmap and go on to the
3844 next work element from the worklist (done via `break').
3845 - If the opcode is a control operator on the other hand, we either
3846 ignore it (if it's meaningless at this point, such as `start_memory')
3847 or execute it (if it's a jump). If the jump has several destinations
3848 (i.e. `on_failure_jump'), then we push the other destination onto the
3849 worklist.
3850 We guarantee termination by ignoring backward jumps (more or less),
3851 so that `p' is monotonically increasing. More to the point, we
3852 never set `p' (or push) anything `<= p1'. */
3854 while (p < pend)
3856 /* `p1' is used as a marker of how far back a `on_failure_jump'
3857 can go without being ignored. It is normally equal to `p'
3858 (which prevents any backward `on_failure_jump') except right
3859 after a plain `jump', to allow patterns such as:
3860 0: jump 10
3861 3..9: <body>
3862 10: on_failure_jump 3
3863 as used for the *? operator. */
3864 re_char *p1 = p;
3866 switch (*p++)
3868 case succeed:
3869 return 1;
3871 case duplicate:
3872 /* If the first character has to match a backreference, that means
3873 that the group was empty (since it already matched). Since this
3874 is the only case that interests us here, we can assume that the
3875 backreference must match the empty string. */
3876 p++;
3877 continue;
3880 /* Following are the cases which match a character. These end
3881 with `break'. */
3883 case exactn:
3884 if (fastmap)
3886 /* If multibyte is nonzero, the first byte of each
3887 character is an ASCII or a leading code. Otherwise,
3888 each byte is a character. Thus, this works in both
3889 cases. */
3890 fastmap[p[1]] = 1;
3891 if (! multibyte)
3893 /* For the case of matching this unibyte regex
3894 against multibyte, we must set a leading code of
3895 the corresponding multibyte character. */
3896 int c = RE_CHAR_TO_MULTIBYTE (p[1]);
3898 fastmap[CHAR_LEADING_CODE (c)] = 1;
3901 break;
3904 case anychar:
3905 /* We could put all the chars except for \n (and maybe \0)
3906 but we don't bother since it is generally not worth it. */
3907 if (!fastmap) break;
3908 return -1;
3911 case charset_not:
3912 if (!fastmap) break;
3914 /* Chars beyond end of bitmap are possible matches. */
3915 for (j = CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH;
3916 j < (1 << BYTEWIDTH); j++)
3917 fastmap[j] = 1;
3920 /* Fallthrough */
3921 case charset:
3922 if (!fastmap) break;
3923 not = (re_opcode_t) *(p - 1) == charset_not;
3924 for (j = CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH - 1, p++;
3925 j >= 0; j--)
3926 if (!!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))) ^ not)
3927 fastmap[j] = 1;
3929 #ifdef emacs
3930 if (/* Any leading code can possibly start a character
3931 which doesn't match the specified set of characters. */
3934 /* If we can match a character class, we can match any
3935 multibyte characters. */
3936 (CHARSET_RANGE_TABLE_EXISTS_P (&p[-2])
3937 && CHARSET_RANGE_TABLE_BITS (&p[-2]) != 0))
3940 if (match_any_multibyte_characters == false)
3942 for (j = MIN_MULTIBYTE_LEADING_CODE;
3943 j <= MAX_MULTIBYTE_LEADING_CODE; j++)
3944 fastmap[j] = 1;
3945 match_any_multibyte_characters = true;
3949 else if (!not && CHARSET_RANGE_TABLE_EXISTS_P (&p[-2])
3950 && match_any_multibyte_characters == false)
3952 /* Set fastmap[I] to 1 where I is a leading code of each
3953 multibyte character in the range table. */
3954 int c, count;
3955 unsigned char lc1, lc2;
3957 /* Make P points the range table. `+ 2' is to skip flag
3958 bits for a character class. */
3959 p += CHARSET_BITMAP_SIZE (&p[-2]) + 2;
3961 /* Extract the number of ranges in range table into COUNT. */
3962 EXTRACT_NUMBER_AND_INCR (count, p);
3963 for (; count > 0; count--, p += 3)
3965 /* Extract the start and end of each range. */
3966 EXTRACT_CHARACTER (c, p);
3967 lc1 = CHAR_LEADING_CODE (c);
3968 p += 3;
3969 EXTRACT_CHARACTER (c, p);
3970 lc2 = CHAR_LEADING_CODE (c);
3971 for (j = lc1; j <= lc2; j++)
3972 fastmap[j] = 1;
3975 #endif
3976 break;
3978 case syntaxspec:
3979 case notsyntaxspec:
3980 if (!fastmap) break;
3981 #ifndef emacs
3982 not = (re_opcode_t)p[-1] == notsyntaxspec;
3983 k = *p++;
3984 for (j = 0; j < (1 << BYTEWIDTH); j++)
3985 if ((SYNTAX (j) == (enum syntaxcode) k) ^ not)
3986 fastmap[j] = 1;
3987 break;
3988 #else /* emacs */
3989 /* This match depends on text properties. These end with
3990 aborting optimizations. */
3991 return -1;
3993 case categoryspec:
3994 case notcategoryspec:
3995 if (!fastmap) break;
3996 not = (re_opcode_t)p[-1] == notcategoryspec;
3997 k = *p++;
3998 for (j = (1 << BYTEWIDTH); j >= 0; j--)
3999 if ((CHAR_HAS_CATEGORY (j, k)) ^ not)
4000 fastmap[j] = 1;
4002 /* Any leading code can possibly start a character which
4003 has or doesn't has the specified category. */
4004 if (match_any_multibyte_characters == false)
4006 for (j = MIN_MULTIBYTE_LEADING_CODE;
4007 j <= MAX_MULTIBYTE_LEADING_CODE; j++)
4008 fastmap[j] = 1;
4009 match_any_multibyte_characters = true;
4011 break;
4013 /* All cases after this match the empty string. These end with
4014 `continue'. */
4016 case at_dot:
4017 #endif /* !emacs */
4018 case no_op:
4019 case begline:
4020 case endline:
4021 case begbuf:
4022 case endbuf:
4023 case wordbound:
4024 case notwordbound:
4025 case wordbeg:
4026 case wordend:
4027 case symbeg:
4028 case symend:
4029 continue;
4032 case jump:
4033 EXTRACT_NUMBER_AND_INCR (j, p);
4034 if (j < 0)
4035 /* Backward jumps can only go back to code that we've already
4036 visited. `re_compile' should make sure this is true. */
4037 break;
4038 p += j;
4039 switch (*p)
4041 case on_failure_jump:
4042 case on_failure_keep_string_jump:
4043 case on_failure_jump_loop:
4044 case on_failure_jump_nastyloop:
4045 case on_failure_jump_smart:
4046 p++;
4047 break;
4048 default:
4049 continue;
4051 /* Keep `p1' to allow the `on_failure_jump' we are jumping to
4052 to jump back to "just after here". */
4053 /* Fallthrough */
4055 case on_failure_jump:
4056 case on_failure_keep_string_jump:
4057 case on_failure_jump_nastyloop:
4058 case on_failure_jump_loop:
4059 case on_failure_jump_smart:
4060 EXTRACT_NUMBER_AND_INCR (j, p);
4061 if (p + j <= p1)
4062 ; /* Backward jump to be ignored. */
4063 else
4064 { /* We have to look down both arms.
4065 We first go down the "straight" path so as to minimize
4066 stack usage when going through alternatives. */
4067 int r = analyze_first (p, pend, fastmap, multibyte);
4068 if (r) return r;
4069 p += j;
4071 continue;
4074 case jump_n:
4075 /* This code simply does not properly handle forward jump_n. */
4076 DEBUG_STATEMENT (EXTRACT_NUMBER (j, p); assert (j < 0));
4077 p += 4;
4078 /* jump_n can either jump or fall through. The (backward) jump
4079 case has already been handled, so we only need to look at the
4080 fallthrough case. */
4081 continue;
4083 case succeed_n:
4084 /* If N == 0, it should be an on_failure_jump_loop instead. */
4085 DEBUG_STATEMENT (EXTRACT_NUMBER (j, p + 2); assert (j > 0));
4086 p += 4;
4087 /* We only care about one iteration of the loop, so we don't
4088 need to consider the case where this behaves like an
4089 on_failure_jump. */
4090 continue;
4093 case set_number_at:
4094 p += 4;
4095 continue;
4098 case start_memory:
4099 case stop_memory:
4100 p += 1;
4101 continue;
4104 default:
4105 abort (); /* We have listed all the cases. */
4106 } /* switch *p++ */
4108 /* Getting here means we have found the possible starting
4109 characters for one path of the pattern -- and that the empty
4110 string does not match. We need not follow this path further. */
4111 return 0;
4112 } /* while p */
4114 /* We reached the end without matching anything. */
4115 return 1;
4117 } /* analyze_first */
4119 /* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
4120 BUFP. A fastmap records which of the (1 << BYTEWIDTH) possible
4121 characters can start a string that matches the pattern. This fastmap
4122 is used by re_search to skip quickly over impossible starting points.
4124 Character codes above (1 << BYTEWIDTH) are not represented in the
4125 fastmap, but the leading codes are represented. Thus, the fastmap
4126 indicates which character sets could start a match.
4128 The caller must supply the address of a (1 << BYTEWIDTH)-byte data
4129 area as BUFP->fastmap.
4131 We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in
4132 the pattern buffer.
4134 Returns 0 if we succeed, -2 if an internal error. */
4137 re_compile_fastmap (struct re_pattern_buffer *bufp)
4139 char *fastmap = bufp->fastmap;
4140 int analysis;
4142 assert (fastmap && bufp->buffer);
4144 memset (fastmap, 0, 1 << BYTEWIDTH); /* Assume nothing's valid. */
4145 bufp->fastmap_accurate = 1; /* It will be when we're done. */
4147 analysis = analyze_first (bufp->buffer, bufp->buffer + bufp->used,
4148 fastmap, RE_MULTIBYTE_P (bufp));
4149 bufp->can_be_null = (analysis != 0);
4150 return 0;
4151 } /* re_compile_fastmap */
4153 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
4154 ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
4155 this memory for recording register information. STARTS and ENDS
4156 must be allocated using the malloc library routine, and must each
4157 be at least NUM_REGS * sizeof (regoff_t) bytes long.
4159 If NUM_REGS == 0, then subsequent matches should allocate their own
4160 register data.
4162 Unless this function is called, the first search or match using
4163 PATTERN_BUFFER will allocate its own register data, without
4164 freeing the old data. */
4166 void
4167 re_set_registers (struct re_pattern_buffer *bufp, struct re_registers *regs, unsigned int num_regs, regoff_t *starts, regoff_t *ends)
4169 if (num_regs)
4171 bufp->regs_allocated = REGS_REALLOCATE;
4172 regs->num_regs = num_regs;
4173 regs->start = starts;
4174 regs->end = ends;
4176 else
4178 bufp->regs_allocated = REGS_UNALLOCATED;
4179 regs->num_regs = 0;
4180 regs->start = regs->end = 0;
4183 WEAK_ALIAS (__re_set_registers, re_set_registers)
4185 /* Searching routines. */
4187 /* Like re_search_2, below, but only one string is specified, and
4188 doesn't let you say where to stop matching. */
4190 regoff_t
4191 re_search (struct re_pattern_buffer *bufp, const char *string, size_t size,
4192 ssize_t startpos, ssize_t range, struct re_registers *regs)
4194 return re_search_2 (bufp, NULL, 0, string, size, startpos, range,
4195 regs, size);
4197 WEAK_ALIAS (__re_search, re_search)
4199 /* Head address of virtual concatenation of string. */
4200 #define HEAD_ADDR_VSTRING(P) \
4201 (((P) >= size1 ? string2 : string1))
4203 /* Address of POS in the concatenation of virtual string. */
4204 #define POS_ADDR_VSTRING(POS) \
4205 (((POS) >= size1 ? string2 - size1 : string1) + (POS))
4207 /* Using the compiled pattern in BUFP->buffer, first tries to match the
4208 virtual concatenation of STRING1 and STRING2, starting first at index
4209 STARTPOS, then at STARTPOS + 1, and so on.
4211 STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
4213 RANGE is how far to scan while trying to match. RANGE = 0 means try
4214 only at STARTPOS; in general, the last start tried is STARTPOS +
4215 RANGE.
4217 In REGS, return the indices of the virtual concatenation of STRING1
4218 and STRING2 that matched the entire BUFP->buffer and its contained
4219 subexpressions.
4221 Do not consider matching one past the index STOP in the virtual
4222 concatenation of STRING1 and STRING2.
4224 We return either the position in the strings at which the match was
4225 found, -1 if no match, or -2 if error (such as failure
4226 stack overflow). */
4228 regoff_t
4229 re_search_2 (struct re_pattern_buffer *bufp, const char *str1, size_t size1,
4230 const char *str2, size_t size2, ssize_t startpos, ssize_t range,
4231 struct re_registers *regs, ssize_t stop)
4233 regoff_t val;
4234 re_char *string1 = (re_char*) str1;
4235 re_char *string2 = (re_char*) str2;
4236 register char *fastmap = bufp->fastmap;
4237 register RE_TRANSLATE_TYPE translate = bufp->translate;
4238 size_t total_size = size1 + size2;
4239 ssize_t endpos = startpos + range;
4240 boolean anchored_start;
4241 /* Nonzero if we are searching multibyte string. */
4242 const boolean multibyte = RE_TARGET_MULTIBYTE_P (bufp);
4244 /* Check for out-of-range STARTPOS. */
4245 if (startpos < 0 || startpos > total_size)
4246 return -1;
4248 /* Fix up RANGE if it might eventually take us outside
4249 the virtual concatenation of STRING1 and STRING2.
4250 Make sure we won't move STARTPOS below 0 or above TOTAL_SIZE. */
4251 if (endpos < 0)
4252 range = 0 - startpos;
4253 else if (endpos > total_size)
4254 range = total_size - startpos;
4256 /* If the search isn't to be a backwards one, don't waste time in a
4257 search for a pattern anchored at beginning of buffer. */
4258 if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == begbuf && range > 0)
4260 if (startpos > 0)
4261 return -1;
4262 else
4263 range = 0;
4266 #ifdef emacs
4267 /* In a forward search for something that starts with \=.
4268 don't keep searching past point. */
4269 if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == at_dot && range > 0)
4271 range = PT_BYTE - BEGV_BYTE - startpos;
4272 if (range < 0)
4273 return -1;
4275 #endif /* emacs */
4277 /* Update the fastmap now if not correct already. */
4278 if (fastmap && !bufp->fastmap_accurate)
4279 re_compile_fastmap (bufp);
4281 /* See whether the pattern is anchored. */
4282 anchored_start = (bufp->buffer[0] == begline);
4284 #ifdef emacs
4285 gl_state.object = re_match_object; /* Used by SYNTAX_TABLE_BYTE_TO_CHAR. */
4287 ssize_t charpos = SYNTAX_TABLE_BYTE_TO_CHAR (POS_AS_IN_BUFFER (startpos));
4289 SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object, charpos, 1);
4291 #endif
4293 /* Loop through the string, looking for a place to start matching. */
4294 for (;;)
4296 /* If the pattern is anchored,
4297 skip quickly past places we cannot match.
4298 We don't bother to treat startpos == 0 specially
4299 because that case doesn't repeat. */
4300 if (anchored_start && startpos > 0)
4302 if (! ((startpos <= size1 ? string1[startpos - 1]
4303 : string2[startpos - size1 - 1])
4304 == '\n'))
4305 goto advance;
4308 /* If a fastmap is supplied, skip quickly over characters that
4309 cannot be the start of a match. If the pattern can match the
4310 null string, however, we don't need to skip characters; we want
4311 the first null string. */
4312 if (fastmap && startpos < total_size && !bufp->can_be_null)
4314 register re_char *d;
4315 register re_wchar_t buf_ch;
4317 d = POS_ADDR_VSTRING (startpos);
4319 if (range > 0) /* Searching forwards. */
4321 ssize_t irange = range, lim = 0;
4323 if (startpos < size1 && startpos + range >= size1)
4324 lim = range - (size1 - startpos);
4326 /* Written out as an if-else to avoid testing `translate'
4327 inside the loop. */
4328 if (RE_TRANSLATE_P (translate))
4330 if (multibyte)
4331 while (range > lim)
4333 int buf_charlen;
4335 buf_ch = STRING_CHAR_AND_LENGTH (d, buf_charlen);
4336 buf_ch = RE_TRANSLATE (translate, buf_ch);
4337 if (fastmap[CHAR_LEADING_CODE (buf_ch)])
4338 break;
4340 range -= buf_charlen;
4341 d += buf_charlen;
4343 else
4344 while (range > lim)
4346 register re_wchar_t ch, translated;
4348 buf_ch = *d;
4349 ch = RE_CHAR_TO_MULTIBYTE (buf_ch);
4350 translated = RE_TRANSLATE (translate, ch);
4351 if (translated != ch
4352 && (ch = RE_CHAR_TO_UNIBYTE (translated)) >= 0)
4353 buf_ch = ch;
4354 if (fastmap[buf_ch])
4355 break;
4356 d++;
4357 range--;
4360 else
4362 if (multibyte)
4363 while (range > lim)
4365 int buf_charlen;
4367 buf_ch = STRING_CHAR_AND_LENGTH (d, buf_charlen);
4368 if (fastmap[CHAR_LEADING_CODE (buf_ch)])
4369 break;
4370 range -= buf_charlen;
4371 d += buf_charlen;
4373 else
4374 while (range > lim && !fastmap[*d])
4376 d++;
4377 range--;
4380 startpos += irange - range;
4382 else /* Searching backwards. */
4384 if (multibyte)
4386 buf_ch = STRING_CHAR (d);
4387 buf_ch = TRANSLATE (buf_ch);
4388 if (! fastmap[CHAR_LEADING_CODE (buf_ch)])
4389 goto advance;
4391 else
4393 register re_wchar_t ch, translated;
4395 buf_ch = *d;
4396 ch = RE_CHAR_TO_MULTIBYTE (buf_ch);
4397 translated = TRANSLATE (ch);
4398 if (translated != ch
4399 && (ch = RE_CHAR_TO_UNIBYTE (translated)) >= 0)
4400 buf_ch = ch;
4401 if (! fastmap[TRANSLATE (buf_ch)])
4402 goto advance;
4407 /* If can't match the null string, and that's all we have left, fail. */
4408 if (range >= 0 && startpos == total_size && fastmap
4409 && !bufp->can_be_null)
4410 return -1;
4412 val = re_match_2_internal (bufp, string1, size1, string2, size2,
4413 startpos, regs, stop);
4415 if (val >= 0)
4416 return startpos;
4418 if (val == -2)
4419 return -2;
4421 advance:
4422 if (!range)
4423 break;
4424 else if (range > 0)
4426 /* Update STARTPOS to the next character boundary. */
4427 if (multibyte)
4429 re_char *p = POS_ADDR_VSTRING (startpos);
4430 int len = BYTES_BY_CHAR_HEAD (*p);
4432 range -= len;
4433 if (range < 0)
4434 break;
4435 startpos += len;
4437 else
4439 range--;
4440 startpos++;
4443 else
4445 range++;
4446 startpos--;
4448 /* Update STARTPOS to the previous character boundary. */
4449 if (multibyte)
4451 re_char *p = POS_ADDR_VSTRING (startpos) + 1;
4452 re_char *p0 = p;
4453 re_char *phead = HEAD_ADDR_VSTRING (startpos);
4455 /* Find the head of multibyte form. */
4456 PREV_CHAR_BOUNDARY (p, phead);
4457 range += p0 - 1 - p;
4458 if (range > 0)
4459 break;
4461 startpos -= p0 - 1 - p;
4465 return -1;
4466 } /* re_search_2 */
4467 WEAK_ALIAS (__re_search_2, re_search_2)
4469 /* Declarations and macros for re_match_2. */
4471 static int bcmp_translate (re_char *s1, re_char *s2,
4472 register ssize_t len,
4473 RE_TRANSLATE_TYPE translate,
4474 const int multibyte);
4476 /* This converts PTR, a pointer into one of the search strings `string1'
4477 and `string2' into an offset from the beginning of that string. */
4478 #define POINTER_TO_OFFSET(ptr) \
4479 (FIRST_STRING_P (ptr) \
4480 ? (ptr) - string1 \
4481 : (ptr) - string2 + (ptrdiff_t) size1)
4483 /* Call before fetching a character with *d. This switches over to
4484 string2 if necessary.
4485 Check re_match_2_internal for a discussion of why end_match_2 might
4486 not be within string2 (but be equal to end_match_1 instead). */
4487 #define PREFETCH() \
4488 while (d == dend) \
4490 /* End of string2 => fail. */ \
4491 if (dend == end_match_2) \
4492 goto fail; \
4493 /* End of string1 => advance to string2. */ \
4494 d = string2; \
4495 dend = end_match_2; \
4498 /* Call before fetching a char with *d if you already checked other limits.
4499 This is meant for use in lookahead operations like wordend, etc..
4500 where we might need to look at parts of the string that might be
4501 outside of the LIMITs (i.e past `stop'). */
4502 #define PREFETCH_NOLIMIT() \
4503 if (d == end1) \
4505 d = string2; \
4506 dend = end_match_2; \
4509 /* Test if at very beginning or at very end of the virtual concatenation
4510 of `string1' and `string2'. If only one string, it's `string2'. */
4511 #define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2)
4512 #define AT_STRINGS_END(d) ((d) == end2)
4514 /* Disabled due to a compiler bug -- see comment at case wordbound */
4516 /* The comment at case wordbound is following one, but we don't use
4517 AT_WORD_BOUNDARY anymore to support multibyte form.
4519 The DEC Alpha C compiler 3.x generates incorrect code for the
4520 test WORDCHAR_P (d - 1) != WORDCHAR_P (d) in the expansion of
4521 AT_WORD_BOUNDARY, so this code is disabled. Expanding the
4522 macro and introducing temporary variables works around the bug. */
4524 #if 0
4525 /* Test if D points to a character which is word-constituent. We have
4526 two special cases to check for: if past the end of string1, look at
4527 the first character in string2; and if before the beginning of
4528 string2, look at the last character in string1. */
4529 #define WORDCHAR_P(d) \
4530 (SYNTAX ((d) == end1 ? *string2 \
4531 : (d) == string2 - 1 ? *(end1 - 1) : *(d)) \
4532 == Sword)
4534 /* Test if the character before D and the one at D differ with respect
4535 to being word-constituent. */
4536 #define AT_WORD_BOUNDARY(d) \
4537 (AT_STRINGS_BEG (d) || AT_STRINGS_END (d) \
4538 || WORDCHAR_P (d - 1) != WORDCHAR_P (d))
4539 #endif
4541 /* Free everything we malloc. */
4542 #ifdef MATCH_MAY_ALLOCATE
4543 # define FREE_VAR(var) \
4544 do { \
4545 if (var) \
4547 REGEX_FREE (var); \
4548 var = NULL; \
4550 } while (0)
4551 # define FREE_VARIABLES() \
4552 do { \
4553 REGEX_FREE_STACK (fail_stack.stack); \
4554 FREE_VAR (regstart); \
4555 FREE_VAR (regend); \
4556 FREE_VAR (best_regstart); \
4557 FREE_VAR (best_regend); \
4558 REGEX_SAFE_FREE (); \
4559 } while (0)
4560 #else
4561 # define FREE_VARIABLES() ((void)0) /* Do nothing! But inhibit gcc warning. */
4562 #endif /* not MATCH_MAY_ALLOCATE */
4565 /* Optimization routines. */
4567 /* If the operation is a match against one or more chars,
4568 return a pointer to the next operation, else return NULL. */
4569 static re_char *
4570 skip_one_char (const_re_char *p)
4572 switch (*p++)
4574 case anychar:
4575 break;
4577 case exactn:
4578 p += *p + 1;
4579 break;
4581 case charset_not:
4582 case charset:
4583 if (CHARSET_RANGE_TABLE_EXISTS_P (p - 1))
4585 int mcnt;
4586 p = CHARSET_RANGE_TABLE (p - 1);
4587 EXTRACT_NUMBER_AND_INCR (mcnt, p);
4588 p = CHARSET_RANGE_TABLE_END (p, mcnt);
4590 else
4591 p += 1 + CHARSET_BITMAP_SIZE (p - 1);
4592 break;
4594 case syntaxspec:
4595 case notsyntaxspec:
4596 #ifdef emacs
4597 case categoryspec:
4598 case notcategoryspec:
4599 #endif /* emacs */
4600 p++;
4601 break;
4603 default:
4604 p = NULL;
4606 return p;
4610 /* Jump over non-matching operations. */
4611 static re_char *
4612 skip_noops (const_re_char *p, const_re_char *pend)
4614 int mcnt;
4615 while (p < pend)
4617 switch (*p)
4619 case start_memory:
4620 case stop_memory:
4621 p += 2; break;
4622 case no_op:
4623 p += 1; break;
4624 case jump:
4625 p += 1;
4626 EXTRACT_NUMBER_AND_INCR (mcnt, p);
4627 p += mcnt;
4628 break;
4629 default:
4630 return p;
4633 assert (p == pend);
4634 return p;
4637 /* Test if C matches charset op. *PP points to the charset or charset_not
4638 opcode. When the function finishes, *PP will be advanced past that opcode.
4639 C is character to test (possibly after translations) and CORIG is original
4640 character (i.e. without any translations). UNIBYTE denotes whether c is
4641 unibyte or multibyte character. */
4642 static bool
4643 execute_charset (const_re_char **pp, unsigned c, unsigned corig, bool unibyte)
4645 re_char *p = *pp, *rtp = NULL;
4646 bool not = (re_opcode_t) *p == charset_not;
4648 if (CHARSET_RANGE_TABLE_EXISTS_P (p))
4650 int count;
4651 rtp = CHARSET_RANGE_TABLE (p);
4652 EXTRACT_NUMBER_AND_INCR (count, rtp);
4653 *pp = CHARSET_RANGE_TABLE_END ((rtp), (count));
4655 else
4656 *pp += 2 + CHARSET_BITMAP_SIZE (p);
4658 if (unibyte && c < (1 << BYTEWIDTH))
4659 { /* Lookup bitmap. */
4660 /* Cast to `unsigned' instead of `unsigned char' in
4661 case the bit list is a full 32 bytes long. */
4662 if (c < (unsigned) (CHARSET_BITMAP_SIZE (p) * BYTEWIDTH)
4663 && p[2 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
4664 return !not;
4666 #ifdef emacs
4667 else if (rtp)
4669 int class_bits = CHARSET_RANGE_TABLE_BITS (p);
4670 re_wchar_t range_start, range_end;
4672 /* Sort tests by the most commonly used classes with some adjustment to which
4673 tests are easiest to perform. Take a look at comment in re_wctype_parse
4674 for table with frequencies of character class names. */
4676 if ((class_bits & BIT_MULTIBYTE) ||
4677 (class_bits & BIT_ALNUM && ISALNUM (c)) ||
4678 (class_bits & BIT_ALPHA && ISALPHA (c)) ||
4679 (class_bits & BIT_SPACE && ISSPACE (c)) ||
4680 (class_bits & BIT_WORD && ISWORD (c)) ||
4681 ((class_bits & BIT_UPPER) &&
4682 (ISUPPER (c) || (corig != c &&
4683 c == downcase (corig) && ISLOWER (c)))) ||
4684 ((class_bits & BIT_LOWER) &&
4685 (ISLOWER (c) || (corig != c &&
4686 c == upcase (corig) && ISUPPER(c)))) ||
4687 (class_bits & BIT_PUNCT && ISPUNCT (c)) ||
4688 (class_bits & BIT_GRAPH && ISGRAPH (c)) ||
4689 (class_bits & BIT_PRINT && ISPRINT (c)))
4690 return !not;
4692 for (p = *pp; rtp < p; rtp += 2 * 3)
4694 EXTRACT_CHARACTER (range_start, rtp);
4695 EXTRACT_CHARACTER (range_end, rtp + 3);
4696 if (range_start <= c && c <= range_end)
4697 return !not;
4700 #endif /* emacs */
4701 return not;
4704 /* Non-zero if "p1 matches something" implies "p2 fails". */
4705 static int
4706 mutually_exclusive_p (struct re_pattern_buffer *bufp, const_re_char *p1,
4707 const_re_char *p2)
4709 re_opcode_t op2;
4710 const boolean multibyte = RE_MULTIBYTE_P (bufp);
4711 unsigned char *pend = bufp->buffer + bufp->used;
4713 assert (p1 >= bufp->buffer && p1 < pend
4714 && p2 >= bufp->buffer && p2 <= pend);
4716 /* Skip over open/close-group commands.
4717 If what follows this loop is a ...+ construct,
4718 look at what begins its body, since we will have to
4719 match at least one of that. */
4720 p2 = skip_noops (p2, pend);
4721 /* The same skip can be done for p1, except that this function
4722 is only used in the case where p1 is a simple match operator. */
4723 /* p1 = skip_noops (p1, pend); */
4725 assert (p1 >= bufp->buffer && p1 < pend
4726 && p2 >= bufp->buffer && p2 <= pend);
4728 op2 = p2 == pend ? succeed : *p2;
4730 switch (op2)
4732 case succeed:
4733 case endbuf:
4734 /* If we're at the end of the pattern, we can change. */
4735 if (skip_one_char (p1))
4737 DEBUG_PRINT (" End of pattern: fast loop.\n");
4738 return 1;
4740 break;
4742 case endline:
4743 case exactn:
4745 register re_wchar_t c
4746 = (re_opcode_t) *p2 == endline ? '\n'
4747 : RE_STRING_CHAR (p2 + 2, multibyte);
4749 if ((re_opcode_t) *p1 == exactn)
4751 if (c != RE_STRING_CHAR (p1 + 2, multibyte))
4753 DEBUG_PRINT (" '%c' != '%c' => fast loop.\n", c, p1[2]);
4754 return 1;
4758 else if ((re_opcode_t) *p1 == charset
4759 || (re_opcode_t) *p1 == charset_not)
4761 if (!execute_charset (&p1, c, c, !multibyte || IS_REAL_ASCII (c)))
4763 DEBUG_PRINT (" No match => fast loop.\n");
4764 return 1;
4767 else if ((re_opcode_t) *p1 == anychar
4768 && c == '\n')
4770 DEBUG_PRINT (" . != \\n => fast loop.\n");
4771 return 1;
4774 break;
4776 case charset:
4778 if ((re_opcode_t) *p1 == exactn)
4779 /* Reuse the code above. */
4780 return mutually_exclusive_p (bufp, p2, p1);
4782 /* It is hard to list up all the character in charset
4783 P2 if it includes multibyte character. Give up in
4784 such case. */
4785 else if (!multibyte || !CHARSET_RANGE_TABLE_EXISTS_P (p2))
4787 /* Now, we are sure that P2 has no range table.
4788 So, for the size of bitmap in P2, `p2[1]' is
4789 enough. But P1 may have range table, so the
4790 size of bitmap table of P1 is extracted by
4791 using macro `CHARSET_BITMAP_SIZE'.
4793 In a multibyte case, we know that all the character
4794 listed in P2 is ASCII. In a unibyte case, P1 has only a
4795 bitmap table. So, in both cases, it is enough to test
4796 only the bitmap table of P1. */
4798 if ((re_opcode_t) *p1 == charset)
4800 int idx;
4801 /* We win if the charset inside the loop
4802 has no overlap with the one after the loop. */
4803 for (idx = 0;
4804 (idx < (int) p2[1]
4805 && idx < CHARSET_BITMAP_SIZE (p1));
4806 idx++)
4807 if ((p2[2 + idx] & p1[2 + idx]) != 0)
4808 break;
4810 if (idx == p2[1]
4811 || idx == CHARSET_BITMAP_SIZE (p1))
4813 DEBUG_PRINT (" No match => fast loop.\n");
4814 return 1;
4817 else if ((re_opcode_t) *p1 == charset_not)
4819 int idx;
4820 /* We win if the charset_not inside the loop lists
4821 every character listed in the charset after. */
4822 for (idx = 0; idx < (int) p2[1]; idx++)
4823 if (! (p2[2 + idx] == 0
4824 || (idx < CHARSET_BITMAP_SIZE (p1)
4825 && ((p2[2 + idx] & ~ p1[2 + idx]) == 0))))
4826 break;
4828 if (idx == p2[1])
4830 DEBUG_PRINT (" No match => fast loop.\n");
4831 return 1;
4836 break;
4838 case charset_not:
4839 switch (*p1)
4841 case exactn:
4842 case charset:
4843 /* Reuse the code above. */
4844 return mutually_exclusive_p (bufp, p2, p1);
4845 case charset_not:
4846 /* When we have two charset_not, it's very unlikely that
4847 they don't overlap. The union of the two sets of excluded
4848 chars should cover all possible chars, which, as a matter of
4849 fact, is virtually impossible in multibyte buffers. */
4850 break;
4852 break;
4854 case wordend:
4855 return ((re_opcode_t) *p1 == syntaxspec && p1[1] == Sword);
4856 case symend:
4857 return ((re_opcode_t) *p1 == syntaxspec
4858 && (p1[1] == Ssymbol || p1[1] == Sword));
4859 case notsyntaxspec:
4860 return ((re_opcode_t) *p1 == syntaxspec && p1[1] == p2[1]);
4862 case wordbeg:
4863 return ((re_opcode_t) *p1 == notsyntaxspec && p1[1] == Sword);
4864 case symbeg:
4865 return ((re_opcode_t) *p1 == notsyntaxspec
4866 && (p1[1] == Ssymbol || p1[1] == Sword));
4867 case syntaxspec:
4868 return ((re_opcode_t) *p1 == notsyntaxspec && p1[1] == p2[1]);
4870 case wordbound:
4871 return (((re_opcode_t) *p1 == notsyntaxspec
4872 || (re_opcode_t) *p1 == syntaxspec)
4873 && p1[1] == Sword);
4875 #ifdef emacs
4876 case categoryspec:
4877 return ((re_opcode_t) *p1 == notcategoryspec && p1[1] == p2[1]);
4878 case notcategoryspec:
4879 return ((re_opcode_t) *p1 == categoryspec && p1[1] == p2[1]);
4880 #endif /* emacs */
4882 default:
4886 /* Safe default. */
4887 return 0;
4891 /* Matching routines. */
4893 #ifndef emacs /* Emacs never uses this. */
4894 /* re_match is like re_match_2 except it takes only a single string. */
4896 regoff_t
4897 re_match (struct re_pattern_buffer *bufp, const char *string,
4898 size_t size, ssize_t pos, struct re_registers *regs)
4900 regoff_t result = re_match_2_internal (bufp, NULL, 0, (re_char*) string,
4901 size, pos, regs, size);
4902 return result;
4904 WEAK_ALIAS (__re_match, re_match)
4905 #endif /* not emacs */
4907 #ifdef emacs
4908 /* In Emacs, this is the string or buffer in which we
4909 are matching. It is used for looking up syntax properties. */
4910 Lisp_Object re_match_object;
4911 #endif
4913 /* re_match_2 matches the compiled pattern in BUFP against the
4914 the (virtual) concatenation of STRING1 and STRING2 (of length SIZE1
4915 and SIZE2, respectively). We start matching at POS, and stop
4916 matching at STOP.
4918 If REGS is non-null and the `no_sub' field of BUFP is nonzero, we
4919 store offsets for the substring each group matched in REGS. See the
4920 documentation for exactly how many groups we fill.
4922 We return -1 if no match, -2 if an internal error (such as the
4923 failure stack overflowing). Otherwise, we return the length of the
4924 matched substring. */
4926 regoff_t
4927 re_match_2 (struct re_pattern_buffer *bufp, const char *string1,
4928 size_t size1, const char *string2, size_t size2, ssize_t pos,
4929 struct re_registers *regs, ssize_t stop)
4931 regoff_t result;
4933 #ifdef emacs
4934 ssize_t charpos;
4935 gl_state.object = re_match_object; /* Used by SYNTAX_TABLE_BYTE_TO_CHAR. */
4936 charpos = SYNTAX_TABLE_BYTE_TO_CHAR (POS_AS_IN_BUFFER (pos));
4937 SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object, charpos, 1);
4938 #endif
4940 result = re_match_2_internal (bufp, (re_char*) string1, size1,
4941 (re_char*) string2, size2,
4942 pos, regs, stop);
4943 return result;
4945 WEAK_ALIAS (__re_match_2, re_match_2)
4948 /* This is a separate function so that we can force an alloca cleanup
4949 afterwards. */
4950 static regoff_t
4951 re_match_2_internal (struct re_pattern_buffer *bufp, const_re_char *string1,
4952 size_t size1, const_re_char *string2, size_t size2,
4953 ssize_t pos, struct re_registers *regs, ssize_t stop)
4955 /* General temporaries. */
4956 int mcnt;
4957 size_t reg;
4959 /* Just past the end of the corresponding string. */
4960 re_char *end1, *end2;
4962 /* Pointers into string1 and string2, just past the last characters in
4963 each to consider matching. */
4964 re_char *end_match_1, *end_match_2;
4966 /* Where we are in the data, and the end of the current string. */
4967 re_char *d, *dend;
4969 /* Used sometimes to remember where we were before starting matching
4970 an operator so that we can go back in case of failure. This "atomic"
4971 behavior of matching opcodes is indispensable to the correctness
4972 of the on_failure_keep_string_jump optimization. */
4973 re_char *dfail;
4975 /* Where we are in the pattern, and the end of the pattern. */
4976 re_char *p = bufp->buffer;
4977 re_char *pend = p + bufp->used;
4979 /* We use this to map every character in the string. */
4980 RE_TRANSLATE_TYPE translate = bufp->translate;
4982 /* Nonzero if BUFP is setup from a multibyte regex. */
4983 const boolean multibyte = RE_MULTIBYTE_P (bufp);
4985 /* Nonzero if STRING1/STRING2 are multibyte. */
4986 const boolean target_multibyte = RE_TARGET_MULTIBYTE_P (bufp);
4988 /* Failure point stack. Each place that can handle a failure further
4989 down the line pushes a failure point on this stack. It consists of
4990 regstart, and regend for all registers corresponding to
4991 the subexpressions we're currently inside, plus the number of such
4992 registers, and, finally, two char *'s. The first char * is where
4993 to resume scanning the pattern; the second one is where to resume
4994 scanning the strings. */
4995 #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
4996 fail_stack_type fail_stack;
4997 #endif
4998 #ifdef DEBUG_COMPILES_ARGUMENTS
4999 unsigned nfailure_points_pushed = 0, nfailure_points_popped = 0;
5000 #endif
5002 #if defined REL_ALLOC && defined REGEX_MALLOC
5003 /* This holds the pointer to the failure stack, when
5004 it is allocated relocatably. */
5005 fail_stack_elt_t *failure_stack_ptr;
5006 #endif
5008 /* We fill all the registers internally, independent of what we
5009 return, for use in backreferences. The number here includes
5010 an element for register zero. */
5011 size_t num_regs = bufp->re_nsub + 1;
5013 /* Information on the contents of registers. These are pointers into
5014 the input strings; they record just what was matched (on this
5015 attempt) by a subexpression part of the pattern, that is, the
5016 regnum-th regstart pointer points to where in the pattern we began
5017 matching and the regnum-th regend points to right after where we
5018 stopped matching the regnum-th subexpression. (The zeroth register
5019 keeps track of what the whole pattern matches.) */
5020 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
5021 re_char **regstart, **regend;
5022 #endif
5024 /* The following record the register info as found in the above
5025 variables when we find a match better than any we've seen before.
5026 This happens as we backtrack through the failure points, which in
5027 turn happens only if we have not yet matched the entire string. */
5028 unsigned best_regs_set = false;
5029 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
5030 re_char **best_regstart, **best_regend;
5031 #endif
5033 /* Logically, this is `best_regend[0]'. But we don't want to have to
5034 allocate space for that if we're not allocating space for anything
5035 else (see below). Also, we never need info about register 0 for
5036 any of the other register vectors, and it seems rather a kludge to
5037 treat `best_regend' differently than the rest. So we keep track of
5038 the end of the best match so far in a separate variable. We
5039 initialize this to NULL so that when we backtrack the first time
5040 and need to test it, it's not garbage. */
5041 re_char *match_end = NULL;
5043 #ifdef DEBUG_COMPILES_ARGUMENTS
5044 /* Counts the total number of registers pushed. */
5045 unsigned num_regs_pushed = 0;
5046 #endif
5048 DEBUG_PRINT ("\n\nEntering re_match_2.\n");
5050 REGEX_USE_SAFE_ALLOCA;
5052 INIT_FAIL_STACK ();
5054 #ifdef MATCH_MAY_ALLOCATE
5055 /* Do not bother to initialize all the register variables if there are
5056 no groups in the pattern, as it takes a fair amount of time. If
5057 there are groups, we include space for register 0 (the whole
5058 pattern), even though we never use it, since it simplifies the
5059 array indexing. We should fix this. */
5060 if (bufp->re_nsub)
5062 regstart = REGEX_TALLOC (num_regs, re_char *);
5063 regend = REGEX_TALLOC (num_regs, re_char *);
5064 best_regstart = REGEX_TALLOC (num_regs, re_char *);
5065 best_regend = REGEX_TALLOC (num_regs, re_char *);
5067 if (!(regstart && regend && best_regstart && best_regend))
5069 FREE_VARIABLES ();
5070 return -2;
5073 else
5075 /* We must initialize all our variables to NULL, so that
5076 `FREE_VARIABLES' doesn't try to free them. */
5077 regstart = regend = best_regstart = best_regend = NULL;
5079 #endif /* MATCH_MAY_ALLOCATE */
5081 /* The starting position is bogus. */
5082 if (pos < 0 || pos > size1 + size2)
5084 FREE_VARIABLES ();
5085 return -1;
5088 /* Initialize subexpression text positions to -1 to mark ones that no
5089 start_memory/stop_memory has been seen for. Also initialize the
5090 register information struct. */
5091 for (reg = 1; reg < num_regs; reg++)
5092 regstart[reg] = regend[reg] = NULL;
5094 /* We move `string1' into `string2' if the latter's empty -- but not if
5095 `string1' is null. */
5096 if (size2 == 0 && string1 != NULL)
5098 string2 = string1;
5099 size2 = size1;
5100 string1 = 0;
5101 size1 = 0;
5103 end1 = string1 + size1;
5104 end2 = string2 + size2;
5106 /* `p' scans through the pattern as `d' scans through the data.
5107 `dend' is the end of the input string that `d' points within. `d'
5108 is advanced into the following input string whenever necessary, but
5109 this happens before fetching; therefore, at the beginning of the
5110 loop, `d' can be pointing at the end of a string, but it cannot
5111 equal `string2'. */
5112 if (pos >= size1)
5114 /* Only match within string2. */
5115 d = string2 + pos - size1;
5116 dend = end_match_2 = string2 + stop - size1;
5117 end_match_1 = end1; /* Just to give it a value. */
5119 else
5121 if (stop < size1)
5123 /* Only match within string1. */
5124 end_match_1 = string1 + stop;
5125 /* BEWARE!
5126 When we reach end_match_1, PREFETCH normally switches to string2.
5127 But in the present case, this means that just doing a PREFETCH
5128 makes us jump from `stop' to `gap' within the string.
5129 What we really want here is for the search to stop as
5130 soon as we hit end_match_1. That's why we set end_match_2
5131 to end_match_1 (since PREFETCH fails as soon as we hit
5132 end_match_2). */
5133 end_match_2 = end_match_1;
5135 else
5136 { /* It's important to use this code when stop == size so that
5137 moving `d' from end1 to string2 will not prevent the d == dend
5138 check from catching the end of string. */
5139 end_match_1 = end1;
5140 end_match_2 = string2 + stop - size1;
5142 d = string1 + pos;
5143 dend = end_match_1;
5146 DEBUG_PRINT ("The compiled pattern is: ");
5147 DEBUG_PRINT_COMPILED_PATTERN (bufp, p, pend);
5148 DEBUG_PRINT ("The string to match is: \"");
5149 DEBUG_PRINT_DOUBLE_STRING (d, string1, size1, string2, size2);
5150 DEBUG_PRINT ("\"\n");
5152 /* This loops over pattern commands. It exits by returning from the
5153 function if the match is complete, or it drops through if the match
5154 fails at this starting point in the input data. */
5155 for (;;)
5157 DEBUG_PRINT ("\n%p: ", p);
5159 if (p == pend)
5161 /* End of pattern means we might have succeeded. */
5162 DEBUG_PRINT ("end of pattern ... ");
5164 /* If we haven't matched the entire string, and we want the
5165 longest match, try backtracking. */
5166 if (d != end_match_2)
5168 /* True if this match is the best seen so far. */
5169 bool best_match_p;
5172 /* True if this match ends in the same string (string1
5173 or string2) as the best previous match. */
5174 bool same_str_p = (FIRST_STRING_P (match_end)
5175 == FIRST_STRING_P (d));
5177 /* AIX compiler got confused when this was combined
5178 with the previous declaration. */
5179 if (same_str_p)
5180 best_match_p = d > match_end;
5181 else
5182 best_match_p = !FIRST_STRING_P (d);
5185 DEBUG_PRINT ("backtracking.\n");
5187 if (!FAIL_STACK_EMPTY ())
5188 { /* More failure points to try. */
5190 /* If exceeds best match so far, save it. */
5191 if (!best_regs_set || best_match_p)
5193 best_regs_set = true;
5194 match_end = d;
5196 DEBUG_PRINT ("\nSAVING match as best so far.\n");
5198 for (reg = 1; reg < num_regs; reg++)
5200 best_regstart[reg] = regstart[reg];
5201 best_regend[reg] = regend[reg];
5204 goto fail;
5207 /* If no failure points, don't restore garbage. And if
5208 last match is real best match, don't restore second
5209 best one. */
5210 else if (best_regs_set && !best_match_p)
5212 restore_best_regs:
5213 /* Restore best match. It may happen that `dend ==
5214 end_match_1' while the restored d is in string2.
5215 For example, the pattern `x.*y.*z' against the
5216 strings `x-' and `y-z-', if the two strings are
5217 not consecutive in memory. */
5218 DEBUG_PRINT ("Restoring best registers.\n");
5220 d = match_end;
5221 dend = ((d >= string1 && d <= end1)
5222 ? end_match_1 : end_match_2);
5224 for (reg = 1; reg < num_regs; reg++)
5226 regstart[reg] = best_regstart[reg];
5227 regend[reg] = best_regend[reg];
5230 } /* d != end_match_2 */
5232 succeed_label:
5233 DEBUG_PRINT ("Accepting match.\n");
5235 /* If caller wants register contents data back, do it. */
5236 if (regs && !bufp->no_sub)
5238 /* Have the register data arrays been allocated? */
5239 if (bufp->regs_allocated == REGS_UNALLOCATED)
5240 { /* No. So allocate them with malloc. We need one
5241 extra element beyond `num_regs' for the `-1' marker
5242 GNU code uses. */
5243 regs->num_regs = max (RE_NREGS, num_regs + 1);
5244 regs->start = TALLOC (regs->num_regs, regoff_t);
5245 regs->end = TALLOC (regs->num_regs, regoff_t);
5246 if (regs->start == NULL || regs->end == NULL)
5248 FREE_VARIABLES ();
5249 return -2;
5251 bufp->regs_allocated = REGS_REALLOCATE;
5253 else if (bufp->regs_allocated == REGS_REALLOCATE)
5254 { /* Yes. If we need more elements than were already
5255 allocated, reallocate them. If we need fewer, just
5256 leave it alone. */
5257 if (regs->num_regs < num_regs + 1)
5259 regs->num_regs = num_regs + 1;
5260 RETALLOC (regs->start, regs->num_regs, regoff_t);
5261 RETALLOC (regs->end, regs->num_regs, regoff_t);
5262 if (regs->start == NULL || regs->end == NULL)
5264 FREE_VARIABLES ();
5265 return -2;
5269 else
5271 /* These braces fend off a "empty body in an else-statement"
5272 warning under GCC when assert expands to nothing. */
5273 assert (bufp->regs_allocated == REGS_FIXED);
5276 /* Convert the pointer data in `regstart' and `regend' to
5277 indices. Register zero has to be set differently,
5278 since we haven't kept track of any info for it. */
5279 if (regs->num_regs > 0)
5281 regs->start[0] = pos;
5282 regs->end[0] = POINTER_TO_OFFSET (d);
5285 /* Go through the first `min (num_regs, regs->num_regs)'
5286 registers, since that is all we initialized. */
5287 for (reg = 1; reg < min (num_regs, regs->num_regs); reg++)
5289 if (REG_UNSET (regstart[reg]) || REG_UNSET (regend[reg]))
5290 regs->start[reg] = regs->end[reg] = -1;
5291 else
5293 regs->start[reg] = POINTER_TO_OFFSET (regstart[reg]);
5294 regs->end[reg] = POINTER_TO_OFFSET (regend[reg]);
5298 /* If the regs structure we return has more elements than
5299 were in the pattern, set the extra elements to -1. If
5300 we (re)allocated the registers, this is the case,
5301 because we always allocate enough to have at least one
5302 -1 at the end. */
5303 for (reg = num_regs; reg < regs->num_regs; reg++)
5304 regs->start[reg] = regs->end[reg] = -1;
5305 } /* regs && !bufp->no_sub */
5307 DEBUG_PRINT ("%u failure points pushed, %u popped (%u remain).\n",
5308 nfailure_points_pushed, nfailure_points_popped,
5309 nfailure_points_pushed - nfailure_points_popped);
5310 DEBUG_PRINT ("%u registers pushed.\n", num_regs_pushed);
5312 ptrdiff_t dcnt = POINTER_TO_OFFSET (d) - pos;
5314 DEBUG_PRINT ("Returning %td from re_match_2.\n", dcnt);
5316 FREE_VARIABLES ();
5317 return dcnt;
5320 /* Otherwise match next pattern command. */
5321 switch (*p++)
5323 /* Ignore these. Used to ignore the n of succeed_n's which
5324 currently have n == 0. */
5325 case no_op:
5326 DEBUG_PRINT ("EXECUTING no_op.\n");
5327 break;
5329 case succeed:
5330 DEBUG_PRINT ("EXECUTING succeed.\n");
5331 goto succeed_label;
5333 /* Match the next n pattern characters exactly. The following
5334 byte in the pattern defines n, and the n bytes after that
5335 are the characters to match. */
5336 case exactn:
5337 mcnt = *p++;
5338 DEBUG_PRINT ("EXECUTING exactn %d.\n", mcnt);
5340 /* Remember the start point to rollback upon failure. */
5341 dfail = d;
5343 #ifndef emacs
5344 /* This is written out as an if-else so we don't waste time
5345 testing `translate' inside the loop. */
5346 if (RE_TRANSLATE_P (translate))
5349 PREFETCH ();
5350 if (RE_TRANSLATE (translate, *d) != *p++)
5352 d = dfail;
5353 goto fail;
5355 d++;
5357 while (--mcnt);
5358 else
5361 PREFETCH ();
5362 if (*d++ != *p++)
5364 d = dfail;
5365 goto fail;
5368 while (--mcnt);
5369 #else /* emacs */
5370 /* The cost of testing `translate' is comparatively small. */
5371 if (target_multibyte)
5374 int pat_charlen, buf_charlen;
5375 int pat_ch, buf_ch;
5377 PREFETCH ();
5378 if (multibyte)
5379 pat_ch = STRING_CHAR_AND_LENGTH (p, pat_charlen);
5380 else
5382 pat_ch = RE_CHAR_TO_MULTIBYTE (*p);
5383 pat_charlen = 1;
5385 buf_ch = STRING_CHAR_AND_LENGTH (d, buf_charlen);
5387 if (TRANSLATE (buf_ch) != pat_ch)
5389 d = dfail;
5390 goto fail;
5393 p += pat_charlen;
5394 d += buf_charlen;
5395 mcnt -= pat_charlen;
5397 while (mcnt > 0);
5398 else
5401 int pat_charlen;
5402 int pat_ch, buf_ch;
5404 PREFETCH ();
5405 if (multibyte)
5407 pat_ch = STRING_CHAR_AND_LENGTH (p, pat_charlen);
5408 pat_ch = RE_CHAR_TO_UNIBYTE (pat_ch);
5410 else
5412 pat_ch = *p;
5413 pat_charlen = 1;
5415 buf_ch = RE_CHAR_TO_MULTIBYTE (*d);
5416 if (! CHAR_BYTE8_P (buf_ch))
5418 buf_ch = TRANSLATE (buf_ch);
5419 buf_ch = RE_CHAR_TO_UNIBYTE (buf_ch);
5420 if (buf_ch < 0)
5421 buf_ch = *d;
5423 else
5424 buf_ch = *d;
5425 if (buf_ch != pat_ch)
5427 d = dfail;
5428 goto fail;
5430 p += pat_charlen;
5431 d++;
5433 while (--mcnt);
5434 #endif
5435 break;
5438 /* Match any character except possibly a newline or a null. */
5439 case anychar:
5441 int buf_charlen;
5442 re_wchar_t buf_ch;
5443 reg_syntax_t syntax;
5445 DEBUG_PRINT ("EXECUTING anychar.\n");
5447 PREFETCH ();
5448 buf_ch = RE_STRING_CHAR_AND_LENGTH (d, buf_charlen,
5449 target_multibyte);
5450 buf_ch = TRANSLATE (buf_ch);
5452 #ifdef emacs
5453 syntax = RE_SYNTAX_EMACS;
5454 #else
5455 syntax = bufp->syntax;
5456 #endif
5458 if ((!(syntax & RE_DOT_NEWLINE) && buf_ch == '\n')
5459 || ((syntax & RE_DOT_NOT_NULL) && buf_ch == '\000'))
5460 goto fail;
5462 DEBUG_PRINT (" Matched \"%d\".\n", *d);
5463 d += buf_charlen;
5465 break;
5468 case charset:
5469 case charset_not:
5471 register unsigned int c, corig;
5472 int len;
5474 /* Whether matching against a unibyte character. */
5475 boolean unibyte_char = false;
5477 DEBUG_PRINT ("EXECUTING charset%s.\n",
5478 (re_opcode_t) *(p - 1) == charset_not ? "_not" : "");
5480 PREFETCH ();
5481 corig = c = RE_STRING_CHAR_AND_LENGTH (d, len, target_multibyte);
5482 if (target_multibyte)
5484 int c1;
5486 c = TRANSLATE (c);
5487 c1 = RE_CHAR_TO_UNIBYTE (c);
5488 if (c1 >= 0)
5490 unibyte_char = true;
5491 c = c1;
5494 else
5496 int c1 = RE_CHAR_TO_MULTIBYTE (c);
5498 if (! CHAR_BYTE8_P (c1))
5500 c1 = TRANSLATE (c1);
5501 c1 = RE_CHAR_TO_UNIBYTE (c1);
5502 if (c1 >= 0)
5504 unibyte_char = true;
5505 c = c1;
5508 else
5509 unibyte_char = true;
5512 p -= 1;
5513 if (!execute_charset (&p, c, corig, unibyte_char))
5514 goto fail;
5516 d += len;
5518 break;
5521 /* The beginning of a group is represented by start_memory.
5522 The argument is the register number. The text
5523 matched within the group is recorded (in the internal
5524 registers data structure) under the register number. */
5525 case start_memory:
5526 DEBUG_PRINT ("EXECUTING start_memory %d:\n", *p);
5528 /* In case we need to undo this operation (via backtracking). */
5529 PUSH_FAILURE_REG (*p);
5531 regstart[*p] = d;
5532 regend[*p] = NULL; /* probably unnecessary. -sm */
5533 DEBUG_PRINT (" regstart: %td\n", POINTER_TO_OFFSET (regstart[*p]));
5535 /* Move past the register number and inner group count. */
5536 p += 1;
5537 break;
5540 /* The stop_memory opcode represents the end of a group. Its
5541 argument is the same as start_memory's: the register number. */
5542 case stop_memory:
5543 DEBUG_PRINT ("EXECUTING stop_memory %d:\n", *p);
5545 assert (!REG_UNSET (regstart[*p]));
5546 /* Strictly speaking, there should be code such as:
5548 assert (REG_UNSET (regend[*p]));
5549 PUSH_FAILURE_REGSTOP ((unsigned int)*p);
5551 But the only info to be pushed is regend[*p] and it is known to
5552 be UNSET, so there really isn't anything to push.
5553 Not pushing anything, on the other hand deprives us from the
5554 guarantee that regend[*p] is UNSET since undoing this operation
5555 will not reset its value properly. This is not important since
5556 the value will only be read on the next start_memory or at
5557 the very end and both events can only happen if this stop_memory
5558 is *not* undone. */
5560 regend[*p] = d;
5561 DEBUG_PRINT (" regend: %td\n", POINTER_TO_OFFSET (regend[*p]));
5563 /* Move past the register number and the inner group count. */
5564 p += 1;
5565 break;
5568 /* \<digit> has been turned into a `duplicate' command which is
5569 followed by the numeric value of <digit> as the register number. */
5570 case duplicate:
5572 register re_char *d2, *dend2;
5573 int regno = *p++; /* Get which register to match against. */
5574 DEBUG_PRINT ("EXECUTING duplicate %d.\n", regno);
5576 /* Can't back reference a group which we've never matched. */
5577 if (REG_UNSET (regstart[regno]) || REG_UNSET (regend[regno]))
5578 goto fail;
5580 /* Where in input to try to start matching. */
5581 d2 = regstart[regno];
5583 /* Remember the start point to rollback upon failure. */
5584 dfail = d;
5586 /* Where to stop matching; if both the place to start and
5587 the place to stop matching are in the same string, then
5588 set to the place to stop, otherwise, for now have to use
5589 the end of the first string. */
5591 dend2 = ((FIRST_STRING_P (regstart[regno])
5592 == FIRST_STRING_P (regend[regno]))
5593 ? regend[regno] : end_match_1);
5594 for (;;)
5596 ptrdiff_t dcnt;
5598 /* If necessary, advance to next segment in register
5599 contents. */
5600 while (d2 == dend2)
5602 if (dend2 == end_match_2) break;
5603 if (dend2 == regend[regno]) break;
5605 /* End of string1 => advance to string2. */
5606 d2 = string2;
5607 dend2 = regend[regno];
5609 /* At end of register contents => success */
5610 if (d2 == dend2) break;
5612 /* If necessary, advance to next segment in data. */
5613 PREFETCH ();
5615 /* How many characters left in this segment to match. */
5616 dcnt = dend - d;
5618 /* Want how many consecutive characters we can match in
5619 one shot, so, if necessary, adjust the count. */
5620 if (dcnt > dend2 - d2)
5621 dcnt = dend2 - d2;
5623 /* Compare that many; failure if mismatch, else move
5624 past them. */
5625 if (RE_TRANSLATE_P (translate)
5626 ? bcmp_translate (d, d2, dcnt, translate, target_multibyte)
5627 : memcmp (d, d2, dcnt))
5629 d = dfail;
5630 goto fail;
5632 d += dcnt, d2 += dcnt;
5635 break;
5638 /* begline matches the empty string at the beginning of the string
5639 (unless `not_bol' is set in `bufp'), and after newlines. */
5640 case begline:
5641 DEBUG_PRINT ("EXECUTING begline.\n");
5643 if (AT_STRINGS_BEG (d))
5645 if (!bufp->not_bol) break;
5647 else
5649 unsigned c;
5650 GET_CHAR_BEFORE_2 (c, d, string1, end1, string2, end2);
5651 if (c == '\n')
5652 break;
5654 /* In all other cases, we fail. */
5655 goto fail;
5658 /* endline is the dual of begline. */
5659 case endline:
5660 DEBUG_PRINT ("EXECUTING endline.\n");
5662 if (AT_STRINGS_END (d))
5664 if (!bufp->not_eol) break;
5666 else
5668 PREFETCH_NOLIMIT ();
5669 if (*d == '\n')
5670 break;
5672 goto fail;
5675 /* Match at the very beginning of the data. */
5676 case begbuf:
5677 DEBUG_PRINT ("EXECUTING begbuf.\n");
5678 if (AT_STRINGS_BEG (d))
5679 break;
5680 goto fail;
5683 /* Match at the very end of the data. */
5684 case endbuf:
5685 DEBUG_PRINT ("EXECUTING endbuf.\n");
5686 if (AT_STRINGS_END (d))
5687 break;
5688 goto fail;
5691 /* on_failure_keep_string_jump is used to optimize `.*\n'. It
5692 pushes NULL as the value for the string on the stack. Then
5693 `POP_FAILURE_POINT' will keep the current value for the
5694 string, instead of restoring it. To see why, consider
5695 matching `foo\nbar' against `.*\n'. The .* matches the foo;
5696 then the . fails against the \n. But the next thing we want
5697 to do is match the \n against the \n; if we restored the
5698 string value, we would be back at the foo.
5700 Because this is used only in specific cases, we don't need to
5701 check all the things that `on_failure_jump' does, to make
5702 sure the right things get saved on the stack. Hence we don't
5703 share its code. The only reason to push anything on the
5704 stack at all is that otherwise we would have to change
5705 `anychar's code to do something besides goto fail in this
5706 case; that seems worse than this. */
5707 case on_failure_keep_string_jump:
5708 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5709 DEBUG_PRINT ("EXECUTING on_failure_keep_string_jump %d (to %p):\n",
5710 mcnt, p + mcnt);
5712 PUSH_FAILURE_POINT (p - 3, NULL);
5713 break;
5715 /* A nasty loop is introduced by the non-greedy *? and +?.
5716 With such loops, the stack only ever contains one failure point
5717 at a time, so that a plain on_failure_jump_loop kind of
5718 cycle detection cannot work. Worse yet, such a detection
5719 can not only fail to detect a cycle, but it can also wrongly
5720 detect a cycle (between different instantiations of the same
5721 loop).
5722 So the method used for those nasty loops is a little different:
5723 We use a special cycle-detection-stack-frame which is pushed
5724 when the on_failure_jump_nastyloop failure-point is *popped*.
5725 This special frame thus marks the beginning of one iteration
5726 through the loop and we can hence easily check right here
5727 whether something matched between the beginning and the end of
5728 the loop. */
5729 case on_failure_jump_nastyloop:
5730 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5731 DEBUG_PRINT ("EXECUTING on_failure_jump_nastyloop %d (to %p):\n",
5732 mcnt, p + mcnt);
5734 assert ((re_opcode_t)p[-4] == no_op);
5736 int cycle = 0;
5737 CHECK_INFINITE_LOOP (p - 4, d);
5738 if (!cycle)
5739 /* If there's a cycle, just continue without pushing
5740 this failure point. The failure point is the "try again"
5741 option, which shouldn't be tried.
5742 We want (x?)*?y\1z to match both xxyz and xxyxz. */
5743 PUSH_FAILURE_POINT (p - 3, d);
5745 break;
5747 /* Simple loop detecting on_failure_jump: just check on the
5748 failure stack if the same spot was already hit earlier. */
5749 case on_failure_jump_loop:
5750 on_failure:
5751 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5752 DEBUG_PRINT ("EXECUTING on_failure_jump_loop %d (to %p):\n",
5753 mcnt, p + mcnt);
5755 int cycle = 0;
5756 CHECK_INFINITE_LOOP (p - 3, d);
5757 if (cycle)
5758 /* If there's a cycle, get out of the loop, as if the matching
5759 had failed. We used to just `goto fail' here, but that was
5760 aborting the search a bit too early: we want to keep the
5761 empty-loop-match and keep matching after the loop.
5762 We want (x?)*y\1z to match both xxyz and xxyxz. */
5763 p += mcnt;
5764 else
5765 PUSH_FAILURE_POINT (p - 3, d);
5767 break;
5770 /* Uses of on_failure_jump:
5772 Each alternative starts with an on_failure_jump that points
5773 to the beginning of the next alternative. Each alternative
5774 except the last ends with a jump that in effect jumps past
5775 the rest of the alternatives. (They really jump to the
5776 ending jump of the following alternative, because tensioning
5777 these jumps is a hassle.)
5779 Repeats start with an on_failure_jump that points past both
5780 the repetition text and either the following jump or
5781 pop_failure_jump back to this on_failure_jump. */
5782 case on_failure_jump:
5783 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5784 DEBUG_PRINT ("EXECUTING on_failure_jump %d (to %p):\n",
5785 mcnt, p + mcnt);
5787 PUSH_FAILURE_POINT (p -3, d);
5788 break;
5790 /* This operation is used for greedy *.
5791 Compare the beginning of the repeat with what in the
5792 pattern follows its end. If we can establish that there
5793 is nothing that they would both match, i.e., that we
5794 would have to backtrack because of (as in, e.g., `a*a')
5795 then we can use a non-backtracking loop based on
5796 on_failure_keep_string_jump instead of on_failure_jump. */
5797 case on_failure_jump_smart:
5798 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5799 DEBUG_PRINT ("EXECUTING on_failure_jump_smart %d (to %p).\n",
5800 mcnt, p + mcnt);
5802 re_char *p1 = p; /* Next operation. */
5803 /* Here, we discard `const', making re_match non-reentrant. */
5804 unsigned char *p2 = (unsigned char*) p + mcnt; /* Jump dest. */
5805 unsigned char *p3 = (unsigned char*) p - 3; /* opcode location. */
5807 p -= 3; /* Reset so that we will re-execute the
5808 instruction once it's been changed. */
5810 EXTRACT_NUMBER (mcnt, p2 - 2);
5812 /* Ensure this is a indeed the trivial kind of loop
5813 we are expecting. */
5814 assert (skip_one_char (p1) == p2 - 3);
5815 assert ((re_opcode_t) p2[-3] == jump && p2 + mcnt == p);
5816 DEBUG_STATEMENT (debug += 2);
5817 if (mutually_exclusive_p (bufp, p1, p2))
5819 /* Use a fast `on_failure_keep_string_jump' loop. */
5820 DEBUG_PRINT (" smart exclusive => fast loop.\n");
5821 *p3 = (unsigned char) on_failure_keep_string_jump;
5822 STORE_NUMBER (p2 - 2, mcnt + 3);
5824 else
5826 /* Default to a safe `on_failure_jump' loop. */
5827 DEBUG_PRINT (" smart default => slow loop.\n");
5828 *p3 = (unsigned char) on_failure_jump;
5830 DEBUG_STATEMENT (debug -= 2);
5832 break;
5834 /* Unconditionally jump (without popping any failure points). */
5835 case jump:
5836 unconditional_jump:
5837 IMMEDIATE_QUIT_CHECK;
5838 EXTRACT_NUMBER_AND_INCR (mcnt, p); /* Get the amount to jump. */
5839 DEBUG_PRINT ("EXECUTING jump %d ", mcnt);
5840 p += mcnt; /* Do the jump. */
5841 DEBUG_PRINT ("(to %p).\n", p);
5842 break;
5845 /* Have to succeed matching what follows at least n times.
5846 After that, handle like `on_failure_jump'. */
5847 case succeed_n:
5848 /* Signedness doesn't matter since we only compare MCNT to 0. */
5849 EXTRACT_NUMBER (mcnt, p + 2);
5850 DEBUG_PRINT ("EXECUTING succeed_n %d.\n", mcnt);
5852 /* Originally, mcnt is how many times we HAVE to succeed. */
5853 if (mcnt != 0)
5855 /* Here, we discard `const', making re_match non-reentrant. */
5856 unsigned char *p2 = (unsigned char*) p + 2; /* counter loc. */
5857 mcnt--;
5858 p += 4;
5859 PUSH_NUMBER (p2, mcnt);
5861 else
5862 /* The two bytes encoding mcnt == 0 are two no_op opcodes. */
5863 goto on_failure;
5864 break;
5866 case jump_n:
5867 /* Signedness doesn't matter since we only compare MCNT to 0. */
5868 EXTRACT_NUMBER (mcnt, p + 2);
5869 DEBUG_PRINT ("EXECUTING jump_n %d.\n", mcnt);
5871 /* Originally, this is how many times we CAN jump. */
5872 if (mcnt != 0)
5874 /* Here, we discard `const', making re_match non-reentrant. */
5875 unsigned char *p2 = (unsigned char*) p + 2; /* counter loc. */
5876 mcnt--;
5877 PUSH_NUMBER (p2, mcnt);
5878 goto unconditional_jump;
5880 /* If don't have to jump any more, skip over the rest of command. */
5881 else
5882 p += 4;
5883 break;
5885 case set_number_at:
5887 unsigned char *p2; /* Location of the counter. */
5888 DEBUG_PRINT ("EXECUTING set_number_at.\n");
5890 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5891 /* Here, we discard `const', making re_match non-reentrant. */
5892 p2 = (unsigned char*) p + mcnt;
5893 /* Signedness doesn't matter since we only copy MCNT's bits. */
5894 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5895 DEBUG_PRINT (" Setting %p to %d.\n", p2, mcnt);
5896 PUSH_NUMBER (p2, mcnt);
5897 break;
5900 case wordbound:
5901 case notwordbound:
5903 boolean not = (re_opcode_t) *(p - 1) == notwordbound;
5904 DEBUG_PRINT ("EXECUTING %swordbound.\n", not ? "not" : "");
5906 /* We SUCCEED (or FAIL) in one of the following cases: */
5908 /* Case 1: D is at the beginning or the end of string. */
5909 if (AT_STRINGS_BEG (d) || AT_STRINGS_END (d))
5910 not = !not;
5911 else
5913 /* C1 is the character before D, S1 is the syntax of C1, C2
5914 is the character at D, and S2 is the syntax of C2. */
5915 re_wchar_t c1, c2;
5916 int s1, s2;
5917 int dummy;
5918 #ifdef emacs
5919 ssize_t offset = PTR_TO_OFFSET (d - 1);
5920 ssize_t charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
5921 UPDATE_SYNTAX_TABLE_FAST (charpos);
5922 #endif
5923 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
5924 s1 = SYNTAX (c1);
5925 #ifdef emacs
5926 UPDATE_SYNTAX_TABLE_FORWARD_FAST (charpos + 1);
5927 #endif
5928 PREFETCH_NOLIMIT ();
5929 GET_CHAR_AFTER (c2, d, dummy);
5930 s2 = SYNTAX (c2);
5932 if (/* Case 2: Only one of S1 and S2 is Sword. */
5933 ((s1 == Sword) != (s2 == Sword))
5934 /* Case 3: Both of S1 and S2 are Sword, and macro
5935 WORD_BOUNDARY_P (C1, C2) returns nonzero. */
5936 || ((s1 == Sword) && WORD_BOUNDARY_P (c1, c2)))
5937 not = !not;
5939 if (not)
5940 break;
5941 else
5942 goto fail;
5945 case wordbeg:
5946 DEBUG_PRINT ("EXECUTING wordbeg.\n");
5948 /* We FAIL in one of the following cases: */
5950 /* Case 1: D is at the end of string. */
5951 if (AT_STRINGS_END (d))
5952 goto fail;
5953 else
5955 /* C1 is the character before D, S1 is the syntax of C1, C2
5956 is the character at D, and S2 is the syntax of C2. */
5957 re_wchar_t c1, c2;
5958 int s1, s2;
5959 int dummy;
5960 #ifdef emacs
5961 ssize_t offset = PTR_TO_OFFSET (d);
5962 ssize_t charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
5963 UPDATE_SYNTAX_TABLE_FAST (charpos);
5964 #endif
5965 PREFETCH ();
5966 GET_CHAR_AFTER (c2, d, dummy);
5967 s2 = SYNTAX (c2);
5969 /* Case 2: S2 is not Sword. */
5970 if (s2 != Sword)
5971 goto fail;
5973 /* Case 3: D is not at the beginning of string ... */
5974 if (!AT_STRINGS_BEG (d))
5976 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
5977 #ifdef emacs
5978 UPDATE_SYNTAX_TABLE_BACKWARD (charpos - 1);
5979 #endif
5980 s1 = SYNTAX (c1);
5982 /* ... and S1 is Sword, and WORD_BOUNDARY_P (C1, C2)
5983 returns 0. */
5984 if ((s1 == Sword) && !WORD_BOUNDARY_P (c1, c2))
5985 goto fail;
5988 break;
5990 case wordend:
5991 DEBUG_PRINT ("EXECUTING wordend.\n");
5993 /* We FAIL in one of the following cases: */
5995 /* Case 1: D is at the beginning of string. */
5996 if (AT_STRINGS_BEG (d))
5997 goto fail;
5998 else
6000 /* C1 is the character before D, S1 is the syntax of C1, C2
6001 is the character at D, and S2 is the syntax of C2. */
6002 re_wchar_t c1, c2;
6003 int s1, s2;
6004 int dummy;
6005 #ifdef emacs
6006 ssize_t offset = PTR_TO_OFFSET (d) - 1;
6007 ssize_t charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
6008 UPDATE_SYNTAX_TABLE_FAST (charpos);
6009 #endif
6010 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
6011 s1 = SYNTAX (c1);
6013 /* Case 2: S1 is not Sword. */
6014 if (s1 != Sword)
6015 goto fail;
6017 /* Case 3: D is not at the end of string ... */
6018 if (!AT_STRINGS_END (d))
6020 PREFETCH_NOLIMIT ();
6021 GET_CHAR_AFTER (c2, d, dummy);
6022 #ifdef emacs
6023 UPDATE_SYNTAX_TABLE_FORWARD_FAST (charpos);
6024 #endif
6025 s2 = SYNTAX (c2);
6027 /* ... and S2 is Sword, and WORD_BOUNDARY_P (C1, C2)
6028 returns 0. */
6029 if ((s2 == Sword) && !WORD_BOUNDARY_P (c1, c2))
6030 goto fail;
6033 break;
6035 case symbeg:
6036 DEBUG_PRINT ("EXECUTING symbeg.\n");
6038 /* We FAIL in one of the following cases: */
6040 /* Case 1: D is at the end of string. */
6041 if (AT_STRINGS_END (d))
6042 goto fail;
6043 else
6045 /* C1 is the character before D, S1 is the syntax of C1, C2
6046 is the character at D, and S2 is the syntax of C2. */
6047 re_wchar_t c1, c2;
6048 int s1, s2;
6049 #ifdef emacs
6050 ssize_t offset = PTR_TO_OFFSET (d);
6051 ssize_t charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
6052 UPDATE_SYNTAX_TABLE_FAST (charpos);
6053 #endif
6054 PREFETCH ();
6055 c2 = RE_STRING_CHAR (d, target_multibyte);
6056 s2 = SYNTAX (c2);
6058 /* Case 2: S2 is neither Sword nor Ssymbol. */
6059 if (s2 != Sword && s2 != Ssymbol)
6060 goto fail;
6062 /* Case 3: D is not at the beginning of string ... */
6063 if (!AT_STRINGS_BEG (d))
6065 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
6066 #ifdef emacs
6067 UPDATE_SYNTAX_TABLE_BACKWARD (charpos - 1);
6068 #endif
6069 s1 = SYNTAX (c1);
6071 /* ... and S1 is Sword or Ssymbol. */
6072 if (s1 == Sword || s1 == Ssymbol)
6073 goto fail;
6076 break;
6078 case symend:
6079 DEBUG_PRINT ("EXECUTING symend.\n");
6081 /* We FAIL in one of the following cases: */
6083 /* Case 1: D is at the beginning of string. */
6084 if (AT_STRINGS_BEG (d))
6085 goto fail;
6086 else
6088 /* C1 is the character before D, S1 is the syntax of C1, C2
6089 is the character at D, and S2 is the syntax of C2. */
6090 re_wchar_t c1, c2;
6091 int s1, s2;
6092 #ifdef emacs
6093 ssize_t offset = PTR_TO_OFFSET (d) - 1;
6094 ssize_t charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
6095 UPDATE_SYNTAX_TABLE_FAST (charpos);
6096 #endif
6097 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
6098 s1 = SYNTAX (c1);
6100 /* Case 2: S1 is neither Ssymbol nor Sword. */
6101 if (s1 != Sword && s1 != Ssymbol)
6102 goto fail;
6104 /* Case 3: D is not at the end of string ... */
6105 if (!AT_STRINGS_END (d))
6107 PREFETCH_NOLIMIT ();
6108 c2 = RE_STRING_CHAR (d, target_multibyte);
6109 #ifdef emacs
6110 UPDATE_SYNTAX_TABLE_FORWARD_FAST (charpos + 1);
6111 #endif
6112 s2 = SYNTAX (c2);
6114 /* ... and S2 is Sword or Ssymbol. */
6115 if (s2 == Sword || s2 == Ssymbol)
6116 goto fail;
6119 break;
6121 case syntaxspec:
6122 case notsyntaxspec:
6124 boolean not = (re_opcode_t) *(p - 1) == notsyntaxspec;
6125 mcnt = *p++;
6126 DEBUG_PRINT ("EXECUTING %ssyntaxspec %d.\n", not ? "not" : "",
6127 mcnt);
6128 PREFETCH ();
6129 #ifdef emacs
6131 ssize_t offset = PTR_TO_OFFSET (d);
6132 ssize_t pos1 = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
6133 UPDATE_SYNTAX_TABLE_FAST (pos1);
6135 #endif
6137 int len;
6138 re_wchar_t c;
6140 GET_CHAR_AFTER (c, d, len);
6141 if ((SYNTAX (c) != (enum syntaxcode) mcnt) ^ not)
6142 goto fail;
6143 d += len;
6146 break;
6148 #ifdef emacs
6149 case at_dot:
6150 DEBUG_PRINT ("EXECUTING at_dot.\n");
6151 if (PTR_BYTE_POS (d) != PT_BYTE)
6152 goto fail;
6153 break;
6155 case categoryspec:
6156 case notcategoryspec:
6158 boolean not = (re_opcode_t) *(p - 1) == notcategoryspec;
6159 mcnt = *p++;
6160 DEBUG_PRINT ("EXECUTING %scategoryspec %d.\n",
6161 not ? "not" : "", mcnt);
6162 PREFETCH ();
6165 int len;
6166 re_wchar_t c;
6167 GET_CHAR_AFTER (c, d, len);
6168 if ((!CHAR_HAS_CATEGORY (c, mcnt)) ^ not)
6169 goto fail;
6170 d += len;
6173 break;
6175 #endif /* emacs */
6177 default:
6178 abort ();
6180 continue; /* Successfully executed one pattern command; keep going. */
6183 /* We goto here if a matching operation fails. */
6184 fail:
6185 IMMEDIATE_QUIT_CHECK;
6186 if (!FAIL_STACK_EMPTY ())
6188 re_char *str, *pat;
6189 /* A restart point is known. Restore to that state. */
6190 DEBUG_PRINT ("\nFAIL:\n");
6191 POP_FAILURE_POINT (str, pat);
6192 switch (*pat++)
6194 case on_failure_keep_string_jump:
6195 assert (str == NULL);
6196 goto continue_failure_jump;
6198 case on_failure_jump_nastyloop:
6199 assert ((re_opcode_t)pat[-2] == no_op);
6200 PUSH_FAILURE_POINT (pat - 2, str);
6201 /* Fallthrough */
6203 case on_failure_jump_loop:
6204 case on_failure_jump:
6205 case succeed_n:
6206 d = str;
6207 continue_failure_jump:
6208 EXTRACT_NUMBER_AND_INCR (mcnt, pat);
6209 p = pat + mcnt;
6210 break;
6212 case no_op:
6213 /* A special frame used for nastyloops. */
6214 goto fail;
6216 default:
6217 abort ();
6220 assert (p >= bufp->buffer && p <= pend);
6222 if (d >= string1 && d <= end1)
6223 dend = end_match_1;
6225 else
6226 break; /* Matching at this starting point really fails. */
6227 } /* for (;;) */
6229 if (best_regs_set)
6230 goto restore_best_regs;
6232 FREE_VARIABLES ();
6234 return -1; /* Failure to match. */
6237 /* Subroutine definitions for re_match_2. */
6239 /* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
6240 bytes; nonzero otherwise. */
6242 static int
6243 bcmp_translate (const_re_char *s1, const_re_char *s2, register ssize_t len,
6244 RE_TRANSLATE_TYPE translate, const int target_multibyte)
6246 register re_char *p1 = s1, *p2 = s2;
6247 re_char *p1_end = s1 + len;
6248 re_char *p2_end = s2 + len;
6250 /* FIXME: Checking both p1 and p2 presumes that the two strings might have
6251 different lengths, but relying on a single `len' would break this. -sm */
6252 while (p1 < p1_end && p2 < p2_end)
6254 int p1_charlen, p2_charlen;
6255 re_wchar_t p1_ch, p2_ch;
6257 GET_CHAR_AFTER (p1_ch, p1, p1_charlen);
6258 GET_CHAR_AFTER (p2_ch, p2, p2_charlen);
6260 if (RE_TRANSLATE (translate, p1_ch)
6261 != RE_TRANSLATE (translate, p2_ch))
6262 return 1;
6264 p1 += p1_charlen, p2 += p2_charlen;
6267 if (p1 != p1_end || p2 != p2_end)
6268 return 1;
6270 return 0;
6273 /* Entry points for GNU code. */
6275 /* re_compile_pattern is the GNU regular expression compiler: it
6276 compiles PATTERN (of length SIZE) and puts the result in BUFP.
6277 Returns 0 if the pattern was valid, otherwise an error string.
6279 Assumes the `allocated' (and perhaps `buffer') and `translate' fields
6280 are set in BUFP on entry.
6282 We call regex_compile to do the actual compilation. */
6284 const char *
6285 re_compile_pattern (const char *pattern, size_t length,
6286 #ifdef emacs
6287 bool posix_backtracking, const char *whitespace_regexp,
6288 #endif
6289 struct re_pattern_buffer *bufp)
6291 reg_errcode_t ret;
6293 /* GNU code is written to assume at least RE_NREGS registers will be set
6294 (and at least one extra will be -1). */
6295 bufp->regs_allocated = REGS_UNALLOCATED;
6297 /* And GNU code determines whether or not to get register information
6298 by passing null for the REGS argument to re_match, etc., not by
6299 setting no_sub. */
6300 bufp->no_sub = 0;
6302 ret = regex_compile ((re_char*) pattern, length,
6303 #ifdef emacs
6304 posix_backtracking,
6305 whitespace_regexp,
6306 #else
6307 re_syntax_options,
6308 #endif
6309 bufp);
6311 if (!ret)
6312 return NULL;
6313 return gettext (re_error_msgid[(int) ret]);
6315 WEAK_ALIAS (__re_compile_pattern, re_compile_pattern)
6317 /* Entry points compatible with 4.2 BSD regex library. We don't define
6318 them unless specifically requested. */
6320 #if defined _REGEX_RE_COMP || defined _LIBC
6322 /* BSD has one and only one pattern buffer. */
6323 static struct re_pattern_buffer re_comp_buf;
6325 char *
6326 # ifdef _LIBC
6327 /* Make these definitions weak in libc, so POSIX programs can redefine
6328 these names if they don't use our functions, and still use
6329 regcomp/regexec below without link errors. */
6330 weak_function
6331 # endif
6332 re_comp (const char *s)
6334 reg_errcode_t ret;
6336 if (!s)
6338 if (!re_comp_buf.buffer)
6339 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6340 return (char *) gettext ("No previous regular expression");
6341 return 0;
6344 if (!re_comp_buf.buffer)
6346 re_comp_buf.buffer = malloc (200);
6347 if (re_comp_buf.buffer == NULL)
6348 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6349 return (char *) gettext (re_error_msgid[(int) REG_ESPACE]);
6350 re_comp_buf.allocated = 200;
6352 re_comp_buf.fastmap = malloc (1 << BYTEWIDTH);
6353 if (re_comp_buf.fastmap == NULL)
6354 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6355 return (char *) gettext (re_error_msgid[(int) REG_ESPACE]);
6358 /* Since `re_exec' always passes NULL for the `regs' argument, we
6359 don't need to initialize the pattern buffer fields which affect it. */
6361 ret = regex_compile (s, strlen (s), re_syntax_options, &re_comp_buf);
6363 if (!ret)
6364 return NULL;
6366 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6367 return (char *) gettext (re_error_msgid[(int) ret]);
6372 # ifdef _LIBC
6373 weak_function
6374 # endif
6375 re_exec (const char *s)
6377 const size_t len = strlen (s);
6378 return re_search (&re_comp_buf, s, len, 0, len, 0) >= 0;
6380 #endif /* _REGEX_RE_COMP */
6382 /* POSIX.2 functions. Don't define these for Emacs. */
6384 #ifndef emacs
6386 /* regcomp takes a regular expression as a string and compiles it.
6388 PREG is a regex_t *. We do not expect any fields to be initialized,
6389 since POSIX says we shouldn't. Thus, we set
6391 `buffer' to the compiled pattern;
6392 `used' to the length of the compiled pattern;
6393 `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
6394 REG_EXTENDED bit in CFLAGS is set; otherwise, to
6395 RE_SYNTAX_POSIX_BASIC;
6396 `fastmap' to an allocated space for the fastmap;
6397 `fastmap_accurate' to zero;
6398 `re_nsub' to the number of subexpressions in PATTERN.
6400 PATTERN is the address of the pattern string.
6402 CFLAGS is a series of bits which affect compilation.
6404 If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
6405 use POSIX basic syntax.
6407 If REG_NEWLINE is set, then . and [^...] don't match newline.
6408 Also, regexec will try a match beginning after every newline.
6410 If REG_ICASE is set, then we considers upper- and lowercase
6411 versions of letters to be equivalent when matching.
6413 If REG_NOSUB is set, then when PREG is passed to regexec, that
6414 routine will report only success or failure, and nothing about the
6415 registers.
6417 It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
6418 the return codes and their meanings.) */
6420 reg_errcode_t
6421 regcomp (regex_t *_Restrict_ preg, const char *_Restrict_ pattern,
6422 int cflags)
6424 reg_errcode_t ret;
6425 reg_syntax_t syntax
6426 = (cflags & REG_EXTENDED) ?
6427 RE_SYNTAX_POSIX_EXTENDED : RE_SYNTAX_POSIX_BASIC;
6429 /* regex_compile will allocate the space for the compiled pattern. */
6430 preg->buffer = 0;
6431 preg->allocated = 0;
6432 preg->used = 0;
6434 /* Try to allocate space for the fastmap. */
6435 preg->fastmap = malloc (1 << BYTEWIDTH);
6437 if (cflags & REG_ICASE)
6439 unsigned i;
6441 preg->translate = malloc (CHAR_SET_SIZE * sizeof *preg->translate);
6442 if (preg->translate == NULL)
6443 return (int) REG_ESPACE;
6445 /* Map uppercase characters to corresponding lowercase ones. */
6446 for (i = 0; i < CHAR_SET_SIZE; i++)
6447 preg->translate[i] = ISUPPER (i) ? TOLOWER (i) : i;
6449 else
6450 preg->translate = NULL;
6452 /* If REG_NEWLINE is set, newlines are treated differently. */
6453 if (cflags & REG_NEWLINE)
6454 { /* REG_NEWLINE implies neither . nor [^...] match newline. */
6455 syntax &= ~RE_DOT_NEWLINE;
6456 syntax |= RE_HAT_LISTS_NOT_NEWLINE;
6458 else
6459 syntax |= RE_NO_NEWLINE_ANCHOR;
6461 preg->no_sub = !!(cflags & REG_NOSUB);
6463 /* POSIX says a null character in the pattern terminates it, so we
6464 can use strlen here in compiling the pattern. */
6465 ret = regex_compile ((re_char*) pattern, strlen (pattern), syntax, preg);
6467 /* POSIX doesn't distinguish between an unmatched open-group and an
6468 unmatched close-group: both are REG_EPAREN. */
6469 if (ret == REG_ERPAREN)
6470 ret = REG_EPAREN;
6472 if (ret == REG_NOERROR && preg->fastmap)
6473 { /* Compute the fastmap now, since regexec cannot modify the pattern
6474 buffer. */
6475 re_compile_fastmap (preg);
6476 if (preg->can_be_null)
6477 { /* The fastmap can't be used anyway. */
6478 free (preg->fastmap);
6479 preg->fastmap = NULL;
6482 return ret;
6484 WEAK_ALIAS (__regcomp, regcomp)
6487 /* regexec searches for a given pattern, specified by PREG, in the
6488 string STRING.
6490 If NMATCH is zero or REG_NOSUB was set in the cflags argument to
6491 `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
6492 least NMATCH elements, and we set them to the offsets of the
6493 corresponding matched substrings.
6495 EFLAGS specifies `execution flags' which affect matching: if
6496 REG_NOTBOL is set, then ^ does not match at the beginning of the
6497 string; if REG_NOTEOL is set, then $ does not match at the end.
6499 We return 0 if we find a match and REG_NOMATCH if not. */
6501 reg_errcode_t
6502 regexec (const regex_t *_Restrict_ preg, const char *_Restrict_ string,
6503 size_t nmatch, regmatch_t pmatch[_Restrict_arr_], int eflags)
6505 regoff_t ret;
6506 struct re_registers regs;
6507 regex_t private_preg;
6508 size_t len = strlen (string);
6509 boolean want_reg_info = !preg->no_sub && nmatch > 0 && pmatch;
6511 private_preg = *preg;
6513 private_preg.not_bol = !!(eflags & REG_NOTBOL);
6514 private_preg.not_eol = !!(eflags & REG_NOTEOL);
6516 /* The user has told us exactly how many registers to return
6517 information about, via `nmatch'. We have to pass that on to the
6518 matching routines. */
6519 private_preg.regs_allocated = REGS_FIXED;
6521 if (want_reg_info)
6523 regs.num_regs = nmatch;
6524 regs.start = TALLOC (nmatch * 2, regoff_t);
6525 if (regs.start == NULL)
6526 return REG_NOMATCH;
6527 regs.end = regs.start + nmatch;
6530 /* Instead of using not_eol to implement REG_NOTEOL, we could simply
6531 pass (&private_preg, string, len + 1, 0, len, ...) pretending the string
6532 was a little bit longer but still only matching the real part.
6533 This works because the `endline' will check for a '\n' and will find a
6534 '\0', correctly deciding that this is not the end of a line.
6535 But it doesn't work out so nicely for REG_NOTBOL, since we don't have
6536 a convenient '\0' there. For all we know, the string could be preceded
6537 by '\n' which would throw things off. */
6539 /* Perform the searching operation. */
6540 ret = re_search (&private_preg, string, len,
6541 /* start: */ 0, /* range: */ len,
6542 want_reg_info ? &regs : 0);
6544 /* Copy the register information to the POSIX structure. */
6545 if (want_reg_info)
6547 if (ret >= 0)
6549 unsigned r;
6551 for (r = 0; r < nmatch; r++)
6553 pmatch[r].rm_so = regs.start[r];
6554 pmatch[r].rm_eo = regs.end[r];
6558 /* If we needed the temporary register info, free the space now. */
6559 free (regs.start);
6562 /* We want zero return to mean success, unlike `re_search'. */
6563 return ret >= 0 ? REG_NOERROR : REG_NOMATCH;
6565 WEAK_ALIAS (__regexec, regexec)
6568 /* Returns a message corresponding to an error code, ERR_CODE, returned
6569 from either regcomp or regexec. We don't use PREG here.
6571 ERR_CODE was previously called ERRCODE, but that name causes an
6572 error with msvc8 compiler. */
6574 size_t
6575 regerror (int err_code, const regex_t *preg, char *errbuf, size_t errbuf_size)
6577 const char *msg;
6578 size_t msg_size;
6580 if (err_code < 0
6581 || err_code >= (sizeof (re_error_msgid) / sizeof (re_error_msgid[0])))
6582 /* Only error codes returned by the rest of the code should be passed
6583 to this routine. If we are given anything else, or if other regex
6584 code generates an invalid error code, then the program has a bug.
6585 Dump core so we can fix it. */
6586 abort ();
6588 msg = gettext (re_error_msgid[err_code]);
6590 msg_size = strlen (msg) + 1; /* Includes the null. */
6592 if (errbuf_size != 0)
6594 if (msg_size > errbuf_size)
6596 memcpy (errbuf, msg, errbuf_size - 1);
6597 errbuf[errbuf_size - 1] = 0;
6599 else
6600 strcpy (errbuf, msg);
6603 return msg_size;
6605 WEAK_ALIAS (__regerror, regerror)
6608 /* Free dynamically allocated space used by PREG. */
6610 void
6611 regfree (regex_t *preg)
6613 free (preg->buffer);
6614 preg->buffer = NULL;
6616 preg->allocated = 0;
6617 preg->used = 0;
6619 free (preg->fastmap);
6620 preg->fastmap = NULL;
6621 preg->fastmap_accurate = 0;
6623 free (preg->translate);
6624 preg->translate = NULL;
6626 WEAK_ALIAS (__regfree, regfree)
6628 #endif /* not emacs */