ibuffer-decompose-filter: Avoid side effects on error
[emacs.git] / src / regex.c
blobafd0d180316ea8f67fec8ce381d7ec49525b6d5e
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 /* Strings are 0-indexed, buffers are 1-indexed; we pun on the boolean
157 result to get the right base index. */
158 # define POS_AS_IN_BUFFER(p) ((p) + (NILP (re_match_object) || BUFFERP (re_match_object)))
160 # define RE_MULTIBYTE_P(bufp) ((bufp)->multibyte)
161 # define RE_TARGET_MULTIBYTE_P(bufp) ((bufp)->target_multibyte)
162 # define RE_STRING_CHAR(p, multibyte) \
163 (multibyte ? (STRING_CHAR (p)) : (*(p)))
164 # define RE_STRING_CHAR_AND_LENGTH(p, len, multibyte) \
165 (multibyte ? (STRING_CHAR_AND_LENGTH (p, len)) : ((len) = 1, *(p)))
167 # define RE_CHAR_TO_MULTIBYTE(c) UNIBYTE_TO_CHAR (c)
169 # define RE_CHAR_TO_UNIBYTE(c) CHAR_TO_BYTE_SAFE (c)
171 /* Set C a (possibly converted to multibyte) character before P. P
172 points into a string which is the virtual concatenation of STR1
173 (which ends at END1) or STR2 (which ends at END2). */
174 # define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2) \
175 do { \
176 if (target_multibyte) \
178 re_char *dtemp = (p) == (str2) ? (end1) : (p); \
179 re_char *dlimit = ((p) > (str2) && (p) <= (end2)) ? (str2) : (str1); \
180 while (dtemp-- > dlimit && !CHAR_HEAD_P (*dtemp)); \
181 c = STRING_CHAR (dtemp); \
183 else \
185 (c = ((p) == (str2) ? (end1) : (p))[-1]); \
186 (c) = RE_CHAR_TO_MULTIBYTE (c); \
188 } while (0)
190 /* Set C a (possibly converted to multibyte) character at P, and set
191 LEN to the byte length of that character. */
192 # define GET_CHAR_AFTER(c, p, len) \
193 do { \
194 if (target_multibyte) \
195 (c) = STRING_CHAR_AND_LENGTH (p, len); \
196 else \
198 (c) = *p; \
199 len = 1; \
200 (c) = RE_CHAR_TO_MULTIBYTE (c); \
202 } while (0)
204 #else /* not emacs */
206 /* If we are not linking with Emacs proper,
207 we can't use the relocating allocator
208 even if config.h says that we can. */
209 # undef REL_ALLOC
211 # include <unistd.h>
213 /* When used in Emacs's lib-src, we need xmalloc and xrealloc. */
215 static void *
216 xmalloc (size_t size)
218 void *val = malloc (size);
219 if (!val && size)
221 write (STDERR_FILENO, "virtual memory exhausted\n", 25);
222 exit (1);
224 return val;
227 static void *
228 xrealloc (void *block, size_t size)
230 void *val;
231 /* We must call malloc explicitly when BLOCK is 0, since some
232 reallocs don't do this. */
233 if (! block)
234 val = malloc (size);
235 else
236 val = realloc (block, size);
237 if (!val && size)
239 write (STDERR_FILENO, "virtual memory exhausted\n", 25);
240 exit (1);
242 return val;
245 # ifdef malloc
246 # undef malloc
247 # endif
248 # define malloc xmalloc
249 # ifdef realloc
250 # undef realloc
251 # endif
252 # define realloc xrealloc
254 # include <stdbool.h>
255 # include <string.h>
257 /* Define the syntax stuff for \<, \>, etc. */
259 /* Sword must be nonzero for the wordchar pattern commands in re_match_2. */
260 enum syntaxcode { Swhitespace = 0, Sword = 1, Ssymbol = 2 };
262 /* Dummy macros for non-Emacs environments. */
263 # define MAX_MULTIBYTE_LENGTH 1
264 # define RE_MULTIBYTE_P(x) 0
265 # define RE_TARGET_MULTIBYTE_P(x) 0
266 # define WORD_BOUNDARY_P(c1, c2) (0)
267 # define BYTES_BY_CHAR_HEAD(p) (1)
268 # define PREV_CHAR_BOUNDARY(p, limit) ((p)--)
269 # define STRING_CHAR(p) (*(p))
270 # define RE_STRING_CHAR(p, multibyte) STRING_CHAR (p)
271 # define CHAR_STRING(c, s) (*(s) = (c), 1)
272 # define STRING_CHAR_AND_LENGTH(p, actual_len) ((actual_len) = 1, *(p))
273 # define RE_STRING_CHAR_AND_LENGTH(p, len, multibyte) STRING_CHAR_AND_LENGTH (p, len)
274 # define RE_CHAR_TO_MULTIBYTE(c) (c)
275 # define RE_CHAR_TO_UNIBYTE(c) (c)
276 # define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2) \
277 (c = ((p) == (str2) ? *((end1) - 1) : *((p) - 1)))
278 # define GET_CHAR_AFTER(c, p, len) \
279 (c = *p, len = 1)
280 # define CHAR_BYTE8_P(c) (0)
281 # define CHAR_LEADING_CODE(c) (c)
283 #endif /* not emacs */
285 #ifndef RE_TRANSLATE
286 # define RE_TRANSLATE(TBL, C) ((unsigned char)(TBL)[C])
287 # define RE_TRANSLATE_P(TBL) (TBL)
288 #endif
290 /* Get the interface, including the syntax bits. */
291 #include "regex.h"
293 /* isalpha etc. are used for the character classes. */
294 #include <ctype.h>
296 #ifdef emacs
298 /* 1 if C is an ASCII character. */
299 # define IS_REAL_ASCII(c) ((c) < 0200)
301 /* 1 if C is a unibyte character. */
302 # define ISUNIBYTE(c) (SINGLE_BYTE_CHAR_P ((c)))
304 /* The Emacs definitions should not be directly affected by locales. */
306 /* In Emacs, these are only used for single-byte characters. */
307 # define ISDIGIT(c) ((c) >= '0' && (c) <= '9')
308 # define ISCNTRL(c) ((c) < ' ')
309 # define ISXDIGIT(c) (((c) >= '0' && (c) <= '9') \
310 || ((c) >= 'a' && (c) <= 'f') \
311 || ((c) >= 'A' && (c) <= 'F'))
313 /* This is only used for single-byte characters. */
314 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
316 /* The rest must handle multibyte characters. */
318 # define ISGRAPH(c) (SINGLE_BYTE_CHAR_P (c) \
319 ? (c) > ' ' && !((c) >= 0177 && (c) <= 0240) \
320 : graphicp (c))
322 # define ISPRINT(c) (SINGLE_BYTE_CHAR_P (c) \
323 ? (c) >= ' ' && !((c) >= 0177 && (c) <= 0237) \
324 : printablep (c))
326 # define ISALNUM(c) (IS_REAL_ASCII (c) \
327 ? (((c) >= 'a' && (c) <= 'z') \
328 || ((c) >= 'A' && (c) <= 'Z') \
329 || ((c) >= '0' && (c) <= '9')) \
330 : alphanumericp (c))
332 # define ISALPHA(c) (IS_REAL_ASCII (c) \
333 ? (((c) >= 'a' && (c) <= 'z') \
334 || ((c) >= 'A' && (c) <= 'Z')) \
335 : alphabeticp (c))
337 # define ISLOWER(c) lowercasep (c)
339 # define ISPUNCT(c) (IS_REAL_ASCII (c) \
340 ? ((c) > ' ' && (c) < 0177 \
341 && !(((c) >= 'a' && (c) <= 'z') \
342 || ((c) >= 'A' && (c) <= 'Z') \
343 || ((c) >= '0' && (c) <= '9'))) \
344 : SYNTAX (c) != Sword)
346 # define ISSPACE(c) (SYNTAX (c) == Swhitespace)
348 # define ISUPPER(c) uppercasep (c)
350 # define ISWORD(c) (SYNTAX (c) == Sword)
352 #else /* not emacs */
354 /* 1 if C is an ASCII character. */
355 # define IS_REAL_ASCII(c) ((c) < 0200)
357 /* This distinction is not meaningful, except in Emacs. */
358 # define ISUNIBYTE(c) 1
360 # ifdef isblank
361 # define ISBLANK(c) isblank (c)
362 # else
363 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
364 # endif
365 # ifdef isgraph
366 # define ISGRAPH(c) isgraph (c)
367 # else
368 # define ISGRAPH(c) (isprint (c) && !isspace (c))
369 # endif
371 /* Solaris defines ISPRINT so we must undefine it first. */
372 # undef ISPRINT
373 # define ISPRINT(c) isprint (c)
374 # define ISDIGIT(c) isdigit (c)
375 # define ISALNUM(c) isalnum (c)
376 # define ISALPHA(c) isalpha (c)
377 # define ISCNTRL(c) iscntrl (c)
378 # define ISLOWER(c) islower (c)
379 # define ISPUNCT(c) ispunct (c)
380 # define ISSPACE(c) isspace (c)
381 # define ISUPPER(c) isupper (c)
382 # define ISXDIGIT(c) isxdigit (c)
384 # define ISWORD(c) ISALPHA (c)
386 # ifdef _tolower
387 # define TOLOWER(c) _tolower (c)
388 # else
389 # define TOLOWER(c) tolower (c)
390 # endif
392 /* How many characters in the character set. */
393 # define CHAR_SET_SIZE 256
395 # ifdef SYNTAX_TABLE
397 extern char *re_syntax_table;
399 # else /* not SYNTAX_TABLE */
401 static char re_syntax_table[CHAR_SET_SIZE];
403 static void
404 init_syntax_once (void)
406 register int c;
407 static int done = 0;
409 if (done)
410 return;
412 memset (re_syntax_table, 0, sizeof re_syntax_table);
414 for (c = 0; c < CHAR_SET_SIZE; ++c)
415 if (ISALNUM (c))
416 re_syntax_table[c] = Sword;
418 re_syntax_table['_'] = Ssymbol;
420 done = 1;
423 # endif /* not SYNTAX_TABLE */
425 # define SYNTAX(c) re_syntax_table[(c)]
427 #endif /* not emacs */
429 #define SIGN_EXTEND_CHAR(c) ((signed char) (c))
431 /* Should we use malloc or alloca? If REGEX_MALLOC is not defined, we
432 use `alloca' instead of `malloc'. This is because using malloc in
433 re_search* or re_match* could cause memory leaks when C-g is used in
434 Emacs; also, malloc is slower and causes storage fragmentation. On
435 the other hand, malloc is more portable, and easier to debug.
437 Because we sometimes use alloca, some routines have to be macros,
438 not functions -- `alloca'-allocated space disappears at the end of the
439 function it is called in. */
441 #ifdef REGEX_MALLOC
443 # define REGEX_ALLOCATE malloc
444 # define REGEX_REALLOCATE(source, osize, nsize) realloc (source, nsize)
445 # define REGEX_FREE free
447 #else /* not REGEX_MALLOC */
449 # ifdef emacs
450 # define REGEX_USE_SAFE_ALLOCA USE_SAFE_ALLOCA
451 # define REGEX_SAFE_FREE() SAFE_FREE ()
452 # define REGEX_ALLOCATE SAFE_ALLOCA
453 # else
454 # include <alloca.h>
455 # define REGEX_ALLOCATE alloca
456 # endif
458 /* Assumes a `char *destination' variable. */
459 # define REGEX_REALLOCATE(source, osize, nsize) \
460 (destination = REGEX_ALLOCATE (nsize), \
461 memcpy (destination, source, osize))
463 /* No need to do anything to free, after alloca. */
464 # define REGEX_FREE(arg) ((void)0) /* Do nothing! But inhibit gcc warning. */
466 #endif /* not REGEX_MALLOC */
468 #ifndef REGEX_USE_SAFE_ALLOCA
469 # define REGEX_USE_SAFE_ALLOCA ((void) 0)
470 # define REGEX_SAFE_FREE() ((void) 0)
471 #endif
473 /* Define how to allocate the failure stack. */
475 #if defined REL_ALLOC && defined REGEX_MALLOC
477 # define REGEX_ALLOCATE_STACK(size) \
478 r_alloc (&failure_stack_ptr, (size))
479 # define REGEX_REALLOCATE_STACK(source, osize, nsize) \
480 r_re_alloc (&failure_stack_ptr, (nsize))
481 # define REGEX_FREE_STACK(ptr) \
482 r_alloc_free (&failure_stack_ptr)
484 #else /* not using relocating allocator */
486 # define REGEX_ALLOCATE_STACK(size) REGEX_ALLOCATE (size)
487 # define REGEX_REALLOCATE_STACK(source, o, n) REGEX_REALLOCATE (source, o, n)
488 # define REGEX_FREE_STACK(ptr) REGEX_FREE (ptr)
490 #endif /* not using relocating allocator */
493 /* True if `size1' is non-NULL and PTR is pointing anywhere inside
494 `string1' or just past its end. This works if PTR is NULL, which is
495 a good thing. */
496 #define FIRST_STRING_P(ptr) \
497 (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
499 /* (Re)Allocate N items of type T using malloc, or fail. */
500 #define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t)))
501 #define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
502 #define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
504 #define BYTEWIDTH 8 /* In bits. */
506 #ifndef emacs
507 # undef max
508 # undef min
509 # define max(a, b) ((a) > (b) ? (a) : (b))
510 # define min(a, b) ((a) < (b) ? (a) : (b))
511 #endif
513 /* Type of source-pattern and string chars. */
514 #ifdef _MSC_VER
515 typedef unsigned char re_char;
516 typedef const re_char const_re_char;
517 #else
518 typedef const unsigned char re_char;
519 typedef re_char const_re_char;
520 #endif
522 typedef char boolean;
524 static regoff_t re_match_2_internal (struct re_pattern_buffer *bufp,
525 re_char *string1, size_t size1,
526 re_char *string2, size_t size2,
527 ssize_t pos,
528 struct re_registers *regs,
529 ssize_t stop);
531 /* These are the command codes that appear in compiled regular
532 expressions. Some opcodes are followed by argument bytes. A
533 command code can specify any interpretation whatsoever for its
534 arguments. Zero bytes may appear in the compiled regular expression. */
536 typedef enum
538 no_op = 0,
540 /* Succeed right away--no more backtracking. */
541 succeed,
543 /* Followed by one byte giving n, then by n literal bytes. */
544 exactn,
546 /* Matches any (more or less) character. */
547 anychar,
549 /* Matches any one char belonging to specified set. First
550 following byte is number of bitmap bytes. Then come bytes
551 for a bitmap saying which chars are in. Bits in each byte
552 are ordered low-bit-first. A character is in the set if its
553 bit is 1. A character too large to have a bit in the map is
554 automatically not in the set.
556 If the length byte has the 0x80 bit set, then that stuff
557 is followed by a range table:
558 2 bytes of flags for character sets (low 8 bits, high 8 bits)
559 See RANGE_TABLE_WORK_BITS below.
560 2 bytes, the number of pairs that follow (upto 32767)
561 pairs, each 2 multibyte characters,
562 each multibyte character represented as 3 bytes. */
563 charset,
565 /* Same parameters as charset, but match any character that is
566 not one of those specified. */
567 charset_not,
569 /* Start remembering the text that is matched, for storing in a
570 register. Followed by one byte with the register number, in
571 the range 0 to one less than the pattern buffer's re_nsub
572 field. */
573 start_memory,
575 /* Stop remembering the text that is matched and store it in a
576 memory register. Followed by one byte with the register
577 number, in the range 0 to one less than `re_nsub' in the
578 pattern buffer. */
579 stop_memory,
581 /* Match a duplicate of something remembered. Followed by one
582 byte containing the register number. */
583 duplicate,
585 /* Fail unless at beginning of line. */
586 begline,
588 /* Fail unless at end of line. */
589 endline,
591 /* Succeeds if at beginning of buffer (if emacs) or at beginning
592 of string to be matched (if not). */
593 begbuf,
595 /* Analogously, for end of buffer/string. */
596 endbuf,
598 /* Followed by two byte relative address to which to jump. */
599 jump,
601 /* Followed by two-byte relative address of place to resume at
602 in case of failure. */
603 on_failure_jump,
605 /* Like on_failure_jump, but pushes a placeholder instead of the
606 current string position when executed. */
607 on_failure_keep_string_jump,
609 /* Just like `on_failure_jump', except that it checks that we
610 don't get stuck in an infinite loop (matching an empty string
611 indefinitely). */
612 on_failure_jump_loop,
614 /* Just like `on_failure_jump_loop', except that it checks for
615 a different kind of loop (the kind that shows up with non-greedy
616 operators). This operation has to be immediately preceded
617 by a `no_op'. */
618 on_failure_jump_nastyloop,
620 /* A smart `on_failure_jump' used for greedy * and + operators.
621 It analyzes the loop before which it is put and if the
622 loop does not require backtracking, it changes itself to
623 `on_failure_keep_string_jump' and short-circuits the loop,
624 else it just defaults to changing itself into `on_failure_jump'.
625 It assumes that it is pointing to just past a `jump'. */
626 on_failure_jump_smart,
628 /* Followed by two-byte relative address and two-byte number n.
629 After matching N times, jump to the address upon failure.
630 Does not work if N starts at 0: use on_failure_jump_loop
631 instead. */
632 succeed_n,
634 /* Followed by two-byte relative address, and two-byte number n.
635 Jump to the address N times, then fail. */
636 jump_n,
638 /* Set the following two-byte relative address to the
639 subsequent two-byte number. The address *includes* the two
640 bytes of number. */
641 set_number_at,
643 wordbeg, /* Succeeds if at word beginning. */
644 wordend, /* Succeeds if at word end. */
646 wordbound, /* Succeeds if at a word boundary. */
647 notwordbound, /* Succeeds if not at a word boundary. */
649 symbeg, /* Succeeds if at symbol beginning. */
650 symend, /* Succeeds if at symbol end. */
652 /* Matches any character whose syntax is specified. Followed by
653 a byte which contains a syntax code, e.g., Sword. */
654 syntaxspec,
656 /* Matches any character whose syntax is not that specified. */
657 notsyntaxspec
659 #ifdef emacs
660 , at_dot, /* Succeeds if at point. */
662 /* Matches any character whose category-set contains the specified
663 category. The operator is followed by a byte which contains a
664 category code (mnemonic ASCII character). */
665 categoryspec,
667 /* Matches any character whose category-set does not contain the
668 specified category. The operator is followed by a byte which
669 contains the category code (mnemonic ASCII character). */
670 notcategoryspec
671 #endif /* emacs */
672 } re_opcode_t;
674 /* Common operations on the compiled pattern. */
676 /* Store NUMBER in two contiguous bytes starting at DESTINATION. */
678 #define STORE_NUMBER(destination, number) \
679 do { \
680 (destination)[0] = (number) & 0377; \
681 (destination)[1] = (number) >> 8; \
682 } while (0)
684 /* Same as STORE_NUMBER, except increment DESTINATION to
685 the byte after where the number is stored. Therefore, DESTINATION
686 must be an lvalue. */
688 #define STORE_NUMBER_AND_INCR(destination, number) \
689 do { \
690 STORE_NUMBER (destination, number); \
691 (destination) += 2; \
692 } while (0)
694 /* Put into DESTINATION a number stored in two contiguous bytes starting
695 at SOURCE. */
697 #define EXTRACT_NUMBER(destination, source) \
698 ((destination) = extract_number (source))
700 static int
701 extract_number (re_char *source)
703 unsigned leading_byte = SIGN_EXTEND_CHAR (source[1]);
704 return (leading_byte << 8) + source[0];
707 /* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
708 SOURCE must be an lvalue. */
710 #define EXTRACT_NUMBER_AND_INCR(destination, source) \
711 ((destination) = extract_number_and_incr (&source))
713 static int
714 extract_number_and_incr (re_char **source)
716 int num = extract_number (*source);
717 *source += 2;
718 return num;
721 /* Store a multibyte character in three contiguous bytes starting
722 DESTINATION, and increment DESTINATION to the byte after where the
723 character is stored. Therefore, DESTINATION must be an lvalue. */
725 #define STORE_CHARACTER_AND_INCR(destination, character) \
726 do { \
727 (destination)[0] = (character) & 0377; \
728 (destination)[1] = ((character) >> 8) & 0377; \
729 (destination)[2] = (character) >> 16; \
730 (destination) += 3; \
731 } while (0)
733 /* Put into DESTINATION a character stored in three contiguous bytes
734 starting at SOURCE. */
736 #define EXTRACT_CHARACTER(destination, source) \
737 do { \
738 (destination) = ((source)[0] \
739 | ((source)[1] << 8) \
740 | ((source)[2] << 16)); \
741 } while (0)
744 /* Macros for charset. */
746 /* Size of bitmap of charset P in bytes. P is a start of charset,
747 i.e. *P is (re_opcode_t) charset or (re_opcode_t) charset_not. */
748 #define CHARSET_BITMAP_SIZE(p) ((p)[1] & 0x7F)
750 /* Nonzero if charset P has range table. */
751 #define CHARSET_RANGE_TABLE_EXISTS_P(p) ((p)[1] & 0x80)
753 /* Return the address of range table of charset P. But not the start
754 of table itself, but the before where the number of ranges is
755 stored. `2 +' means to skip re_opcode_t and size of bitmap,
756 and the 2 bytes of flags at the start of the range table. */
757 #define CHARSET_RANGE_TABLE(p) (&(p)[4 + CHARSET_BITMAP_SIZE (p)])
759 #ifdef emacs
760 /* Extract the bit flags that start a range table. */
761 #define CHARSET_RANGE_TABLE_BITS(p) \
762 ((p)[2 + CHARSET_BITMAP_SIZE (p)] \
763 + (p)[3 + CHARSET_BITMAP_SIZE (p)] * 0x100)
764 #endif
766 /* Return the address of end of RANGE_TABLE. COUNT is number of
767 ranges (which is a pair of (start, end)) in the RANGE_TABLE. `* 2'
768 is start of range and end of range. `* 3' is size of each start
769 and end. */
770 #define CHARSET_RANGE_TABLE_END(range_table, count) \
771 ((range_table) + (count) * 2 * 3)
773 /* If DEBUG is defined, Regex prints many voluminous messages about what
774 it is doing (if the variable `debug' is nonzero). If linked with the
775 main program in `iregex.c', you can enter patterns and strings
776 interactively. And if linked with the main program in `main.c' and
777 the other test files, you can run the already-written tests. */
779 #ifdef DEBUG
781 /* We use standard I/O for debugging. */
782 # include <stdio.h>
784 /* It is useful to test things that ``must'' be true when debugging. */
785 # include <assert.h>
787 static int debug = -100000;
789 # define DEBUG_STATEMENT(e) e
790 # define DEBUG_PRINT(...) if (debug > 0) printf (__VA_ARGS__)
791 # define DEBUG_COMPILES_ARGUMENTS
792 # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) \
793 if (debug > 0) print_partial_compiled_pattern (s, e)
794 # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \
795 if (debug > 0) print_double_string (w, s1, sz1, s2, sz2)
798 /* Print the fastmap in human-readable form. */
800 static void
801 print_fastmap (char *fastmap)
803 unsigned was_a_range = 0;
804 unsigned i = 0;
806 while (i < (1 << BYTEWIDTH))
808 if (fastmap[i++])
810 was_a_range = 0;
811 putchar (i - 1);
812 while (i < (1 << BYTEWIDTH) && fastmap[i])
814 was_a_range = 1;
815 i++;
817 if (was_a_range)
819 printf ("-");
820 putchar (i - 1);
824 putchar ('\n');
828 /* Print a compiled pattern string in human-readable form, starting at
829 the START pointer into it and ending just before the pointer END. */
831 static void
832 print_partial_compiled_pattern (re_char *start, re_char *end)
834 int mcnt, mcnt2;
835 re_char *p = start;
836 re_char *pend = end;
838 if (start == NULL)
840 fprintf (stderr, "(null)\n");
841 return;
844 /* Loop over pattern commands. */
845 while (p < pend)
847 fprintf (stderr, "%td:\t", p - start);
849 switch ((re_opcode_t) *p++)
851 case no_op:
852 fprintf (stderr, "/no_op");
853 break;
855 case succeed:
856 fprintf (stderr, "/succeed");
857 break;
859 case exactn:
860 mcnt = *p++;
861 fprintf (stderr, "/exactn/%d", mcnt);
864 fprintf (stderr, "/%c", *p++);
866 while (--mcnt);
867 break;
869 case start_memory:
870 fprintf (stderr, "/start_memory/%d", *p++);
871 break;
873 case stop_memory:
874 fprintf (stderr, "/stop_memory/%d", *p++);
875 break;
877 case duplicate:
878 fprintf (stderr, "/duplicate/%d", *p++);
879 break;
881 case anychar:
882 fprintf (stderr, "/anychar");
883 break;
885 case charset:
886 case charset_not:
888 register int c, last = -100;
889 register int in_range = 0;
890 int length = CHARSET_BITMAP_SIZE (p - 1);
891 int has_range_table = CHARSET_RANGE_TABLE_EXISTS_P (p - 1);
893 fprintf (stderr, "/charset [%s",
894 (re_opcode_t) *(p - 1) == charset_not ? "^" : "");
896 if (p + *p >= pend)
897 fprintf (stderr, " !extends past end of pattern! ");
899 for (c = 0; c < 256; c++)
900 if (c / 8 < length
901 && (p[1 + (c/8)] & (1 << (c % 8))))
903 /* Are we starting a range? */
904 if (last + 1 == c && ! in_range)
906 fprintf (stderr, "-");
907 in_range = 1;
909 /* Have we broken a range? */
910 else if (last + 1 != c && in_range)
912 fprintf (stderr, "%c", last);
913 in_range = 0;
916 if (! in_range)
917 fprintf (stderr, "%c", c);
919 last = c;
922 if (in_range)
923 fprintf (stderr, "%c", last);
925 fprintf (stderr, "]");
927 p += 1 + length;
929 if (has_range_table)
931 int count;
932 fprintf (stderr, "has-range-table");
934 /* ??? Should print the range table; for now, just skip it. */
935 p += 2; /* skip range table bits */
936 EXTRACT_NUMBER_AND_INCR (count, p);
937 p = CHARSET_RANGE_TABLE_END (p, count);
940 break;
942 case begline:
943 fprintf (stderr, "/begline");
944 break;
946 case endline:
947 fprintf (stderr, "/endline");
948 break;
950 case on_failure_jump:
951 EXTRACT_NUMBER_AND_INCR (mcnt, p);
952 fprintf (stderr, "/on_failure_jump to %td", p + mcnt - start);
953 break;
955 case on_failure_keep_string_jump:
956 EXTRACT_NUMBER_AND_INCR (mcnt, p);
957 fprintf (stderr, "/on_failure_keep_string_jump to %td",
958 p + mcnt - start);
959 break;
961 case on_failure_jump_nastyloop:
962 EXTRACT_NUMBER_AND_INCR (mcnt, p);
963 fprintf (stderr, "/on_failure_jump_nastyloop to %td",
964 p + mcnt - start);
965 break;
967 case on_failure_jump_loop:
968 EXTRACT_NUMBER_AND_INCR (mcnt, p);
969 fprintf (stderr, "/on_failure_jump_loop to %td",
970 p + mcnt - start);
971 break;
973 case on_failure_jump_smart:
974 EXTRACT_NUMBER_AND_INCR (mcnt, p);
975 fprintf (stderr, "/on_failure_jump_smart to %td",
976 p + mcnt - start);
977 break;
979 case jump:
980 EXTRACT_NUMBER_AND_INCR (mcnt, p);
981 fprintf (stderr, "/jump to %td", p + mcnt - start);
982 break;
984 case succeed_n:
985 EXTRACT_NUMBER_AND_INCR (mcnt, p);
986 EXTRACT_NUMBER_AND_INCR (mcnt2, p);
987 fprintf (stderr, "/succeed_n to %td, %d times",
988 p - 2 + mcnt - start, mcnt2);
989 break;
991 case jump_n:
992 EXTRACT_NUMBER_AND_INCR (mcnt, p);
993 EXTRACT_NUMBER_AND_INCR (mcnt2, p);
994 fprintf (stderr, "/jump_n to %td, %d times",
995 p - 2 + mcnt - start, mcnt2);
996 break;
998 case set_number_at:
999 EXTRACT_NUMBER_AND_INCR (mcnt, p);
1000 EXTRACT_NUMBER_AND_INCR (mcnt2, p);
1001 fprintf (stderr, "/set_number_at location %td to %d",
1002 p - 2 + mcnt - start, mcnt2);
1003 break;
1005 case wordbound:
1006 fprintf (stderr, "/wordbound");
1007 break;
1009 case notwordbound:
1010 fprintf (stderr, "/notwordbound");
1011 break;
1013 case wordbeg:
1014 fprintf (stderr, "/wordbeg");
1015 break;
1017 case wordend:
1018 fprintf (stderr, "/wordend");
1019 break;
1021 case symbeg:
1022 fprintf (stderr, "/symbeg");
1023 break;
1025 case symend:
1026 fprintf (stderr, "/symend");
1027 break;
1029 case syntaxspec:
1030 fprintf (stderr, "/syntaxspec");
1031 mcnt = *p++;
1032 fprintf (stderr, "/%d", mcnt);
1033 break;
1035 case notsyntaxspec:
1036 fprintf (stderr, "/notsyntaxspec");
1037 mcnt = *p++;
1038 fprintf (stderr, "/%d", mcnt);
1039 break;
1041 # ifdef emacs
1042 case at_dot:
1043 fprintf (stderr, "/at_dot");
1044 break;
1046 case categoryspec:
1047 fprintf (stderr, "/categoryspec");
1048 mcnt = *p++;
1049 fprintf (stderr, "/%d", mcnt);
1050 break;
1052 case notcategoryspec:
1053 fprintf (stderr, "/notcategoryspec");
1054 mcnt = *p++;
1055 fprintf (stderr, "/%d", mcnt);
1056 break;
1057 # endif /* emacs */
1059 case begbuf:
1060 fprintf (stderr, "/begbuf");
1061 break;
1063 case endbuf:
1064 fprintf (stderr, "/endbuf");
1065 break;
1067 default:
1068 fprintf (stderr, "?%d", *(p-1));
1071 fprintf (stderr, "\n");
1074 fprintf (stderr, "%td:\tend of pattern.\n", p - start);
1078 static void
1079 print_compiled_pattern (struct re_pattern_buffer *bufp)
1081 re_char *buffer = bufp->buffer;
1083 print_partial_compiled_pattern (buffer, buffer + bufp->used);
1084 printf ("%ld bytes used/%ld bytes allocated.\n",
1085 bufp->used, bufp->allocated);
1087 if (bufp->fastmap_accurate && bufp->fastmap)
1089 printf ("fastmap: ");
1090 print_fastmap (bufp->fastmap);
1093 printf ("re_nsub: %zu\t", bufp->re_nsub);
1094 printf ("regs_alloc: %d\t", bufp->regs_allocated);
1095 printf ("can_be_null: %d\t", bufp->can_be_null);
1096 printf ("no_sub: %d\t", bufp->no_sub);
1097 printf ("not_bol: %d\t", bufp->not_bol);
1098 printf ("not_eol: %d\t", bufp->not_eol);
1099 #ifndef emacs
1100 printf ("syntax: %lx\n", bufp->syntax);
1101 #endif
1102 fflush (stdout);
1103 /* Perhaps we should print the translate table? */
1107 static void
1108 print_double_string (re_char *where, re_char *string1, ssize_t size1,
1109 re_char *string2, ssize_t size2)
1111 ssize_t this_char;
1113 if (where == NULL)
1114 printf ("(null)");
1115 else
1117 if (FIRST_STRING_P (where))
1119 for (this_char = where - string1; this_char < size1; this_char++)
1120 putchar (string1[this_char]);
1122 where = string2;
1125 for (this_char = where - string2; this_char < size2; this_char++)
1126 putchar (string2[this_char]);
1130 #else /* not DEBUG */
1132 # undef assert
1133 # define assert(e)
1135 # define DEBUG_STATEMENT(e)
1136 # define DEBUG_PRINT(...)
1137 # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
1138 # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
1140 #endif /* not DEBUG */
1142 #ifndef emacs
1144 /* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
1145 also be assigned to arbitrarily: each pattern buffer stores its own
1146 syntax, so it can be changed between regex compilations. */
1147 /* This has no initializer because initialized variables in Emacs
1148 become read-only after dumping. */
1149 reg_syntax_t re_syntax_options;
1152 /* Specify the precise syntax of regexps for compilation. This provides
1153 for compatibility for various utilities which historically have
1154 different, incompatible syntaxes.
1156 The argument SYNTAX is a bit mask comprised of the various bits
1157 defined in regex.h. We return the old syntax. */
1159 reg_syntax_t
1160 re_set_syntax (reg_syntax_t syntax)
1162 reg_syntax_t ret = re_syntax_options;
1164 re_syntax_options = syntax;
1165 return ret;
1167 WEAK_ALIAS (__re_set_syntax, re_set_syntax)
1169 #endif
1171 /* This table gives an error message for each of the error codes listed
1172 in regex.h. Obviously the order here has to be same as there.
1173 POSIX doesn't require that we do anything for REG_NOERROR,
1174 but why not be nice? */
1176 static const char *re_error_msgid[] =
1178 gettext_noop ("Success"), /* REG_NOERROR */
1179 gettext_noop ("No match"), /* REG_NOMATCH */
1180 gettext_noop ("Invalid regular expression"), /* REG_BADPAT */
1181 gettext_noop ("Invalid collation character"), /* REG_ECOLLATE */
1182 gettext_noop ("Invalid character class name"), /* REG_ECTYPE */
1183 gettext_noop ("Trailing backslash"), /* REG_EESCAPE */
1184 gettext_noop ("Invalid back reference"), /* REG_ESUBREG */
1185 gettext_noop ("Unmatched [ or [^"), /* REG_EBRACK */
1186 gettext_noop ("Unmatched ( or \\("), /* REG_EPAREN */
1187 gettext_noop ("Unmatched \\{"), /* REG_EBRACE */
1188 gettext_noop ("Invalid content of \\{\\}"), /* REG_BADBR */
1189 gettext_noop ("Invalid range end"), /* REG_ERANGE */
1190 gettext_noop ("Memory exhausted"), /* REG_ESPACE */
1191 gettext_noop ("Invalid preceding regular expression"), /* REG_BADRPT */
1192 gettext_noop ("Premature end of regular expression"), /* REG_EEND */
1193 gettext_noop ("Regular expression too big"), /* REG_ESIZE */
1194 gettext_noop ("Unmatched ) or \\)"), /* REG_ERPAREN */
1195 gettext_noop ("Range striding over charsets") /* REG_ERANGEX */
1198 /* Avoiding alloca during matching, to placate r_alloc. */
1200 /* Define MATCH_MAY_ALLOCATE unless we need to make sure that the
1201 searching and matching functions should not call alloca. On some
1202 systems, alloca is implemented in terms of malloc, and if we're
1203 using the relocating allocator routines, then malloc could cause a
1204 relocation, which might (if the strings being searched are in the
1205 ralloc heap) shift the data out from underneath the regexp
1206 routines.
1208 Here's another reason to avoid allocation: Emacs
1209 processes input from X in a signal handler; processing X input may
1210 call malloc; if input arrives while a matching routine is calling
1211 malloc, then we're scrod. But Emacs can't just block input while
1212 calling matching routines; then we don't notice interrupts when
1213 they come in. So, Emacs blocks input around all regexp calls
1214 except the matching calls, which it leaves unprotected, in the
1215 faith that they will not malloc. */
1217 /* Normally, this is fine. */
1218 #define MATCH_MAY_ALLOCATE
1220 /* The match routines may not allocate if (1) they would do it with malloc
1221 and (2) it's not safe for them to use malloc.
1222 Note that if REL_ALLOC is defined, matching would not use malloc for the
1223 failure stack, but we would still use it for the register vectors;
1224 so REL_ALLOC should not affect this. */
1225 #if defined REGEX_MALLOC && defined emacs
1226 # undef MATCH_MAY_ALLOCATE
1227 #endif
1230 /* Failure stack declarations and macros; both re_compile_fastmap and
1231 re_match_2 use a failure stack. These have to be macros because of
1232 REGEX_ALLOCATE_STACK. */
1235 /* Approximate number of failure points for which to initially allocate space
1236 when matching. If this number is exceeded, we allocate more
1237 space, so it is not a hard limit. */
1238 #ifndef INIT_FAILURE_ALLOC
1239 # define INIT_FAILURE_ALLOC 20
1240 #endif
1242 /* Roughly the maximum number of failure points on the stack. Would be
1243 exactly that if always used TYPICAL_FAILURE_SIZE items each time we failed.
1244 This is a variable only so users of regex can assign to it; we never
1245 change it ourselves. We always multiply it by TYPICAL_FAILURE_SIZE
1246 before using it, so it should probably be a byte-count instead. */
1247 # if defined MATCH_MAY_ALLOCATE
1248 /* Note that 4400 was enough to cause a crash on Alpha OSF/1,
1249 whose default stack limit is 2mb. In order for a larger
1250 value to work reliably, you have to try to make it accord
1251 with the process stack limit. */
1252 size_t re_max_failures = 40000;
1253 # else
1254 size_t re_max_failures = 4000;
1255 # endif
1257 union fail_stack_elt
1259 re_char *pointer;
1260 /* This should be the biggest `int' that's no bigger than a pointer. */
1261 long integer;
1264 typedef union fail_stack_elt fail_stack_elt_t;
1266 typedef struct
1268 fail_stack_elt_t *stack;
1269 size_t size;
1270 size_t avail; /* Offset of next open position. */
1271 size_t frame; /* Offset of the cur constructed frame. */
1272 } fail_stack_type;
1274 #define FAIL_STACK_EMPTY() (fail_stack.frame == 0)
1277 /* Define macros to initialize and free the failure stack.
1278 Do `return -2' if the alloc fails. */
1280 #ifdef MATCH_MAY_ALLOCATE
1281 # define INIT_FAIL_STACK() \
1282 do { \
1283 fail_stack.stack = \
1284 REGEX_ALLOCATE_STACK (INIT_FAILURE_ALLOC * TYPICAL_FAILURE_SIZE \
1285 * sizeof (fail_stack_elt_t)); \
1287 if (fail_stack.stack == NULL) \
1288 return -2; \
1290 fail_stack.size = INIT_FAILURE_ALLOC; \
1291 fail_stack.avail = 0; \
1292 fail_stack.frame = 0; \
1293 } while (0)
1294 #else
1295 # define INIT_FAIL_STACK() \
1296 do { \
1297 fail_stack.avail = 0; \
1298 fail_stack.frame = 0; \
1299 } while (0)
1301 # define RETALLOC_IF(addr, n, t) \
1302 if (addr) RETALLOC((addr), (n), t); else (addr) = TALLOC ((n), t)
1303 #endif
1306 /* Double the size of FAIL_STACK, up to a limit
1307 which allows approximately `re_max_failures' items.
1309 Return 1 if succeeds, and 0 if either ran out of memory
1310 allocating space for it or it was already too large.
1312 REGEX_REALLOCATE_STACK requires `destination' be declared. */
1314 /* Factor to increase the failure stack size by
1315 when we increase it.
1316 This used to be 2, but 2 was too wasteful
1317 because the old discarded stacks added up to as much space
1318 were as ultimate, maximum-size stack. */
1319 #define FAIL_STACK_GROWTH_FACTOR 4
1321 #define GROW_FAIL_STACK(fail_stack) \
1322 (((fail_stack).size * sizeof (fail_stack_elt_t) \
1323 >= re_max_failures * TYPICAL_FAILURE_SIZE) \
1324 ? 0 \
1325 : ((fail_stack).stack \
1326 = REGEX_REALLOCATE_STACK ((fail_stack).stack, \
1327 (fail_stack).size * sizeof (fail_stack_elt_t), \
1328 min (re_max_failures * TYPICAL_FAILURE_SIZE, \
1329 ((fail_stack).size * sizeof (fail_stack_elt_t) \
1330 * FAIL_STACK_GROWTH_FACTOR))), \
1332 (fail_stack).stack == NULL \
1333 ? 0 \
1334 : ((fail_stack).size \
1335 = (min (re_max_failures * TYPICAL_FAILURE_SIZE, \
1336 ((fail_stack).size * sizeof (fail_stack_elt_t) \
1337 * FAIL_STACK_GROWTH_FACTOR)) \
1338 / sizeof (fail_stack_elt_t)), \
1339 1)))
1342 /* Push a pointer value onto the failure stack.
1343 Assumes the variable `fail_stack'. Probably should only
1344 be called from within `PUSH_FAILURE_POINT'. */
1345 #define PUSH_FAILURE_POINTER(item) \
1346 fail_stack.stack[fail_stack.avail++].pointer = (item)
1348 /* This pushes an integer-valued item onto the failure stack.
1349 Assumes the variable `fail_stack'. Probably should only
1350 be called from within `PUSH_FAILURE_POINT'. */
1351 #define PUSH_FAILURE_INT(item) \
1352 fail_stack.stack[fail_stack.avail++].integer = (item)
1354 /* These POP... operations complement the PUSH... operations.
1355 All assume that `fail_stack' is nonempty. */
1356 #define POP_FAILURE_POINTER() fail_stack.stack[--fail_stack.avail].pointer
1357 #define POP_FAILURE_INT() fail_stack.stack[--fail_stack.avail].integer
1359 /* Individual items aside from the registers. */
1360 #define NUM_NONREG_ITEMS 3
1362 /* Used to examine the stack (to detect infinite loops). */
1363 #define FAILURE_PAT(h) fail_stack.stack[(h) - 1].pointer
1364 #define FAILURE_STR(h) (fail_stack.stack[(h) - 2].pointer)
1365 #define NEXT_FAILURE_HANDLE(h) fail_stack.stack[(h) - 3].integer
1366 #define TOP_FAILURE_HANDLE() fail_stack.frame
1369 #define ENSURE_FAIL_STACK(space) \
1370 while (REMAINING_AVAIL_SLOTS <= space) { \
1371 if (!GROW_FAIL_STACK (fail_stack)) \
1372 return -2; \
1373 DEBUG_PRINT ("\n Doubled stack; size now: %zd\n", (fail_stack).size);\
1374 DEBUG_PRINT (" slots available: %zd\n", REMAINING_AVAIL_SLOTS);\
1377 /* Push register NUM onto the stack. */
1378 #define PUSH_FAILURE_REG(num) \
1379 do { \
1380 char *destination; \
1381 long n = num; \
1382 ENSURE_FAIL_STACK(3); \
1383 DEBUG_PRINT (" Push reg %ld (spanning %p -> %p)\n", \
1384 n, regstart[n], regend[n]); \
1385 PUSH_FAILURE_POINTER (regstart[n]); \
1386 PUSH_FAILURE_POINTER (regend[n]); \
1387 PUSH_FAILURE_INT (n); \
1388 } while (0)
1390 /* Change the counter's value to VAL, but make sure that it will
1391 be reset when backtracking. */
1392 #define PUSH_NUMBER(ptr,val) \
1393 do { \
1394 char *destination; \
1395 int c; \
1396 ENSURE_FAIL_STACK(3); \
1397 EXTRACT_NUMBER (c, ptr); \
1398 DEBUG_PRINT (" Push number %p = %d -> %d\n", ptr, c, val); \
1399 PUSH_FAILURE_INT (c); \
1400 PUSH_FAILURE_POINTER (ptr); \
1401 PUSH_FAILURE_INT (-1); \
1402 STORE_NUMBER (ptr, val); \
1403 } while (0)
1405 /* Pop a saved register off the stack. */
1406 #define POP_FAILURE_REG_OR_COUNT() \
1407 do { \
1408 long pfreg = POP_FAILURE_INT (); \
1409 if (pfreg == -1) \
1411 /* It's a counter. */ \
1412 /* Here, we discard `const', making re_match non-reentrant. */ \
1413 unsigned char *ptr = (unsigned char*) POP_FAILURE_POINTER (); \
1414 pfreg = POP_FAILURE_INT (); \
1415 STORE_NUMBER (ptr, pfreg); \
1416 DEBUG_PRINT (" Pop counter %p = %ld\n", ptr, pfreg); \
1418 else \
1420 regend[pfreg] = POP_FAILURE_POINTER (); \
1421 regstart[pfreg] = POP_FAILURE_POINTER (); \
1422 DEBUG_PRINT (" Pop reg %ld (spanning %p -> %p)\n", \
1423 pfreg, regstart[pfreg], regend[pfreg]); \
1425 } while (0)
1427 /* Check that we are not stuck in an infinite loop. */
1428 #define CHECK_INFINITE_LOOP(pat_cur, string_place) \
1429 do { \
1430 ssize_t failure = TOP_FAILURE_HANDLE (); \
1431 /* Check for infinite matching loops */ \
1432 while (failure > 0 \
1433 && (FAILURE_STR (failure) == string_place \
1434 || FAILURE_STR (failure) == NULL)) \
1436 assert (FAILURE_PAT (failure) >= bufp->buffer \
1437 && FAILURE_PAT (failure) <= bufp->buffer + bufp->used); \
1438 if (FAILURE_PAT (failure) == pat_cur) \
1440 cycle = 1; \
1441 break; \
1443 DEBUG_PRINT (" Other pattern: %p\n", FAILURE_PAT (failure)); \
1444 failure = NEXT_FAILURE_HANDLE(failure); \
1446 DEBUG_PRINT (" Other string: %p\n", FAILURE_STR (failure)); \
1447 } while (0)
1449 /* Push the information about the state we will need
1450 if we ever fail back to it.
1452 Requires variables fail_stack, regstart, regend and
1453 num_regs be declared. GROW_FAIL_STACK requires `destination' be
1454 declared.
1456 Does `return FAILURE_CODE' if runs out of memory. */
1458 #define PUSH_FAILURE_POINT(pattern, string_place) \
1459 do { \
1460 char *destination; \
1461 /* Must be int, so when we don't save any registers, the arithmetic \
1462 of 0 + -1 isn't done as unsigned. */ \
1464 DEBUG_STATEMENT (nfailure_points_pushed++); \
1465 DEBUG_PRINT ("\nPUSH_FAILURE_POINT:\n"); \
1466 DEBUG_PRINT (" Before push, next avail: %zd\n", (fail_stack).avail); \
1467 DEBUG_PRINT (" size: %zd\n", (fail_stack).size);\
1469 ENSURE_FAIL_STACK (NUM_NONREG_ITEMS); \
1471 DEBUG_PRINT ("\n"); \
1473 DEBUG_PRINT (" Push frame index: %zd\n", fail_stack.frame); \
1474 PUSH_FAILURE_INT (fail_stack.frame); \
1476 DEBUG_PRINT (" Push string %p: \"", string_place); \
1477 DEBUG_PRINT_DOUBLE_STRING (string_place, string1, size1, string2, size2);\
1478 DEBUG_PRINT ("\"\n"); \
1479 PUSH_FAILURE_POINTER (string_place); \
1481 DEBUG_PRINT (" Push pattern %p: ", pattern); \
1482 DEBUG_PRINT_COMPILED_PATTERN (bufp, pattern, pend); \
1483 PUSH_FAILURE_POINTER (pattern); \
1485 /* Close the frame by moving the frame pointer past it. */ \
1486 fail_stack.frame = fail_stack.avail; \
1487 } while (0)
1489 /* Estimate the size of data pushed by a typical failure stack entry.
1490 An estimate is all we need, because all we use this for
1491 is to choose a limit for how big to make the failure stack. */
1492 /* BEWARE, the value `20' is hard-coded in emacs.c:main(). */
1493 #define TYPICAL_FAILURE_SIZE 20
1495 /* How many items can still be added to the stack without overflowing it. */
1496 #define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail)
1499 /* Pops what PUSH_FAIL_STACK pushes.
1501 We restore into the parameters, all of which should be lvalues:
1502 STR -- the saved data position.
1503 PAT -- the saved pattern position.
1504 REGSTART, REGEND -- arrays of string positions.
1506 Also assumes the variables `fail_stack' and (if debugging), `bufp',
1507 `pend', `string1', `size1', `string2', and `size2'. */
1509 #define POP_FAILURE_POINT(str, pat) \
1510 do { \
1511 assert (!FAIL_STACK_EMPTY ()); \
1513 /* Remove failure points and point to how many regs pushed. */ \
1514 DEBUG_PRINT ("POP_FAILURE_POINT:\n"); \
1515 DEBUG_PRINT (" Before pop, next avail: %zd\n", fail_stack.avail); \
1516 DEBUG_PRINT (" size: %zd\n", fail_stack.size); \
1518 /* Pop the saved registers. */ \
1519 while (fail_stack.frame < fail_stack.avail) \
1520 POP_FAILURE_REG_OR_COUNT (); \
1522 pat = POP_FAILURE_POINTER (); \
1523 DEBUG_PRINT (" Popping pattern %p: ", pat); \
1524 DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend); \
1526 /* If the saved string location is NULL, it came from an \
1527 on_failure_keep_string_jump opcode, and we want to throw away the \
1528 saved NULL, thus retaining our current position in the string. */ \
1529 str = POP_FAILURE_POINTER (); \
1530 DEBUG_PRINT (" Popping string %p: \"", str); \
1531 DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2); \
1532 DEBUG_PRINT ("\"\n"); \
1534 fail_stack.frame = POP_FAILURE_INT (); \
1535 DEBUG_PRINT (" Popping frame index: %zd\n", fail_stack.frame); \
1537 assert (fail_stack.avail >= 0); \
1538 assert (fail_stack.frame <= fail_stack.avail); \
1540 DEBUG_STATEMENT (nfailure_points_popped++); \
1541 } while (0) /* POP_FAILURE_POINT */
1545 /* Registers are set to a sentinel when they haven't yet matched. */
1546 #define REG_UNSET(e) ((e) == NULL)
1548 /* Subroutine declarations and macros for regex_compile. */
1550 static reg_errcode_t regex_compile (re_char *pattern, size_t size,
1551 #ifdef emacs
1552 bool posix_backtracking,
1553 const char *whitespace_regexp,
1554 #else
1555 reg_syntax_t syntax,
1556 #endif
1557 struct re_pattern_buffer *bufp);
1558 static void store_op1 (re_opcode_t op, unsigned char *loc, int arg);
1559 static void store_op2 (re_opcode_t op, unsigned char *loc, int arg1, int arg2);
1560 static void insert_op1 (re_opcode_t op, unsigned char *loc,
1561 int arg, unsigned char *end);
1562 static void insert_op2 (re_opcode_t op, unsigned char *loc,
1563 int arg1, int arg2, unsigned char *end);
1564 static boolean at_begline_loc_p (re_char *pattern, re_char *p,
1565 reg_syntax_t syntax);
1566 static boolean at_endline_loc_p (re_char *p, re_char *pend,
1567 reg_syntax_t syntax);
1568 static re_char *skip_one_char (re_char *p);
1569 static int analyze_first (re_char *p, re_char *pend,
1570 char *fastmap, const int multibyte);
1572 /* Fetch the next character in the uncompiled pattern, with no
1573 translation. */
1574 #define PATFETCH(c) \
1575 do { \
1576 int len; \
1577 if (p == pend) return REG_EEND; \
1578 c = RE_STRING_CHAR_AND_LENGTH (p, len, multibyte); \
1579 p += len; \
1580 } while (0)
1583 /* If `translate' is non-null, return translate[D], else just D. We
1584 cast the subscript to translate because some data is declared as
1585 `char *', to avoid warnings when a string constant is passed. But
1586 when we use a character as a subscript we must make it unsigned. */
1587 #ifndef TRANSLATE
1588 # define TRANSLATE(d) \
1589 (RE_TRANSLATE_P (translate) ? RE_TRANSLATE (translate, (d)) : (d))
1590 #endif
1593 /* Macros for outputting the compiled pattern into `buffer'. */
1595 /* If the buffer isn't allocated when it comes in, use this. */
1596 #define INIT_BUF_SIZE 32
1598 /* Make sure we have at least N more bytes of space in buffer. */
1599 #define GET_BUFFER_SPACE(n) \
1600 while ((size_t) (b - bufp->buffer + (n)) > bufp->allocated) \
1601 EXTEND_BUFFER ()
1603 /* Make sure we have one more byte of buffer space and then add C to it. */
1604 #define BUF_PUSH(c) \
1605 do { \
1606 GET_BUFFER_SPACE (1); \
1607 *b++ = (unsigned char) (c); \
1608 } while (0)
1611 /* Ensure we have two more bytes of buffer space and then append C1 and C2. */
1612 #define BUF_PUSH_2(c1, c2) \
1613 do { \
1614 GET_BUFFER_SPACE (2); \
1615 *b++ = (unsigned char) (c1); \
1616 *b++ = (unsigned char) (c2); \
1617 } while (0)
1620 /* Store a jump with opcode OP at LOC to location TO. We store a
1621 relative address offset by the three bytes the jump itself occupies. */
1622 #define STORE_JUMP(op, loc, to) \
1623 store_op1 (op, loc, (to) - (loc) - 3)
1625 /* Likewise, for a two-argument jump. */
1626 #define STORE_JUMP2(op, loc, to, arg) \
1627 store_op2 (op, loc, (to) - (loc) - 3, arg)
1629 /* Like `STORE_JUMP', but for inserting. Assume `b' is the buffer end. */
1630 #define INSERT_JUMP(op, loc, to) \
1631 insert_op1 (op, loc, (to) - (loc) - 3, b)
1633 /* Like `STORE_JUMP2', but for inserting. Assume `b' is the buffer end. */
1634 #define INSERT_JUMP2(op, loc, to, arg) \
1635 insert_op2 (op, loc, (to) - (loc) - 3, arg, b)
1638 /* This is not an arbitrary limit: the arguments which represent offsets
1639 into the pattern are two bytes long. So if 2^15 bytes turns out to
1640 be too small, many things would have to change. */
1641 # define MAX_BUF_SIZE (1L << 15)
1643 /* Extend the buffer by twice its current size via realloc and
1644 reset the pointers that pointed into the old block to point to the
1645 correct places in the new one. If extending the buffer results in it
1646 being larger than MAX_BUF_SIZE, then flag memory exhausted. */
1647 #define EXTEND_BUFFER() \
1648 do { \
1649 unsigned char *old_buffer = bufp->buffer; \
1650 if (bufp->allocated == MAX_BUF_SIZE) \
1651 return REG_ESIZE; \
1652 bufp->allocated <<= 1; \
1653 if (bufp->allocated > MAX_BUF_SIZE) \
1654 bufp->allocated = MAX_BUF_SIZE; \
1655 ptrdiff_t b_off = b - old_buffer; \
1656 ptrdiff_t begalt_off = begalt - old_buffer; \
1657 bool fixup_alt_jump_set = !!fixup_alt_jump; \
1658 bool laststart_set = !!laststart; \
1659 bool pending_exact_set = !!pending_exact; \
1660 ptrdiff_t fixup_alt_jump_off, laststart_off, pending_exact_off; \
1661 if (fixup_alt_jump_set) fixup_alt_jump_off = fixup_alt_jump - old_buffer; \
1662 if (laststart_set) laststart_off = laststart - old_buffer; \
1663 if (pending_exact_set) pending_exact_off = pending_exact - old_buffer; \
1664 RETALLOC (bufp->buffer, bufp->allocated, unsigned char); \
1665 if (bufp->buffer == NULL) \
1666 return REG_ESPACE; \
1667 unsigned char *new_buffer = bufp->buffer; \
1668 b = new_buffer + b_off; \
1669 begalt = new_buffer + begalt_off; \
1670 if (fixup_alt_jump_set) fixup_alt_jump = new_buffer + fixup_alt_jump_off; \
1671 if (laststart_set) laststart = new_buffer + laststart_off; \
1672 if (pending_exact_set) pending_exact = new_buffer + pending_exact_off; \
1673 } while (0)
1676 /* Since we have one byte reserved for the register number argument to
1677 {start,stop}_memory, the maximum number of groups we can report
1678 things about is what fits in that byte. */
1679 #define MAX_REGNUM 255
1681 /* But patterns can have more than `MAX_REGNUM' registers. We just
1682 ignore the excess. */
1683 typedef int regnum_t;
1686 /* Macros for the compile stack. */
1688 /* Since offsets can go either forwards or backwards, this type needs to
1689 be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1. */
1690 /* int may be not enough when sizeof(int) == 2. */
1691 typedef long pattern_offset_t;
1693 typedef struct
1695 pattern_offset_t begalt_offset;
1696 pattern_offset_t fixup_alt_jump;
1697 pattern_offset_t laststart_offset;
1698 regnum_t regnum;
1699 } compile_stack_elt_t;
1702 typedef struct
1704 compile_stack_elt_t *stack;
1705 size_t size;
1706 size_t avail; /* Offset of next open position. */
1707 } compile_stack_type;
1710 #define INIT_COMPILE_STACK_SIZE 32
1712 #define COMPILE_STACK_EMPTY (compile_stack.avail == 0)
1713 #define COMPILE_STACK_FULL (compile_stack.avail == compile_stack.size)
1715 /* The next available element. */
1716 #define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
1718 /* Explicit quit checking is needed for Emacs, which uses polling to
1719 process input events. */
1720 #ifdef emacs
1721 # define IMMEDIATE_QUIT_CHECK \
1722 do { \
1723 if (immediate_quit) QUIT; \
1724 } while (0)
1725 #else
1726 # define IMMEDIATE_QUIT_CHECK ((void)0)
1727 #endif
1729 /* Structure to manage work area for range table. */
1730 struct range_table_work_area
1732 int *table; /* actual work area. */
1733 int allocated; /* allocated size for work area in bytes. */
1734 int used; /* actually used size in words. */
1735 int bits; /* flag to record character classes */
1738 #ifdef emacs
1740 /* Make sure that WORK_AREA can hold more N multibyte characters.
1741 This is used only in set_image_of_range and set_image_of_range_1.
1742 It expects WORK_AREA to be a pointer.
1743 If it can't get the space, it returns from the surrounding function. */
1745 #define EXTEND_RANGE_TABLE(work_area, n) \
1746 do { \
1747 if (((work_area).used + (n)) * sizeof (int) > (work_area).allocated) \
1749 extend_range_table_work_area (&work_area); \
1750 if ((work_area).table == 0) \
1751 return (REG_ESPACE); \
1753 } while (0)
1755 #define SET_RANGE_TABLE_WORK_AREA_BIT(work_area, bit) \
1756 (work_area).bits |= (bit)
1758 /* Set a range (RANGE_START, RANGE_END) to WORK_AREA. */
1759 #define SET_RANGE_TABLE_WORK_AREA(work_area, range_start, range_end) \
1760 do { \
1761 EXTEND_RANGE_TABLE ((work_area), 2); \
1762 (work_area).table[(work_area).used++] = (range_start); \
1763 (work_area).table[(work_area).used++] = (range_end); \
1764 } while (0)
1766 #endif /* emacs */
1768 /* Free allocated memory for WORK_AREA. */
1769 #define FREE_RANGE_TABLE_WORK_AREA(work_area) \
1770 do { \
1771 if ((work_area).table) \
1772 free ((work_area).table); \
1773 } while (0)
1775 #define CLEAR_RANGE_TABLE_WORK_USED(work_area) ((work_area).used = 0, (work_area).bits = 0)
1776 #define RANGE_TABLE_WORK_USED(work_area) ((work_area).used)
1777 #define RANGE_TABLE_WORK_BITS(work_area) ((work_area).bits)
1778 #define RANGE_TABLE_WORK_ELT(work_area, i) ((work_area).table[i])
1780 /* Bits used to implement the multibyte-part of the various character classes
1781 such as [:alnum:] in a charset's range table. The code currently assumes
1782 that only the low 16 bits are used. */
1783 #define BIT_WORD 0x1
1784 #define BIT_LOWER 0x2
1785 #define BIT_PUNCT 0x4
1786 #define BIT_SPACE 0x8
1787 #define BIT_UPPER 0x10
1788 #define BIT_MULTIBYTE 0x20
1789 #define BIT_ALPHA 0x40
1790 #define BIT_ALNUM 0x80
1791 #define BIT_GRAPH 0x100
1792 #define BIT_PRINT 0x200
1795 /* Set the bit for character C in a list. */
1796 #define SET_LIST_BIT(c) (b[((c)) / BYTEWIDTH] |= 1 << ((c) % BYTEWIDTH))
1799 #ifdef emacs
1801 /* Store characters in the range FROM to TO in the bitmap at B (for
1802 ASCII and unibyte characters) and WORK_AREA (for multibyte
1803 characters) while translating them and paying attention to the
1804 continuity of translated characters.
1806 Implementation note: It is better to implement these fairly big
1807 macros by a function, but it's not that easy because macros called
1808 in this macro assume various local variables already declared. */
1810 /* Both FROM and TO are ASCII characters. */
1812 #define SETUP_ASCII_RANGE(work_area, FROM, TO) \
1813 do { \
1814 int C0, C1; \
1816 for (C0 = (FROM); C0 <= (TO); C0++) \
1818 C1 = TRANSLATE (C0); \
1819 if (! ASCII_CHAR_P (C1)) \
1821 SET_RANGE_TABLE_WORK_AREA ((work_area), C1, C1); \
1822 if ((C1 = RE_CHAR_TO_UNIBYTE (C1)) < 0) \
1823 C1 = C0; \
1825 SET_LIST_BIT (C1); \
1827 } while (0)
1830 /* Both FROM and TO are unibyte characters (0x80..0xFF). */
1832 #define SETUP_UNIBYTE_RANGE(work_area, FROM, TO) \
1833 do { \
1834 int C0, C1, C2, I; \
1835 int USED = RANGE_TABLE_WORK_USED (work_area); \
1837 for (C0 = (FROM); C0 <= (TO); C0++) \
1839 C1 = RE_CHAR_TO_MULTIBYTE (C0); \
1840 if (CHAR_BYTE8_P (C1)) \
1841 SET_LIST_BIT (C0); \
1842 else \
1844 C2 = TRANSLATE (C1); \
1845 if (C2 == C1 \
1846 || (C1 = RE_CHAR_TO_UNIBYTE (C2)) < 0) \
1847 C1 = C0; \
1848 SET_LIST_BIT (C1); \
1849 for (I = RANGE_TABLE_WORK_USED (work_area) - 2; I >= USED; I -= 2) \
1851 int from = RANGE_TABLE_WORK_ELT (work_area, I); \
1852 int to = RANGE_TABLE_WORK_ELT (work_area, I + 1); \
1854 if (C2 >= from - 1 && C2 <= to + 1) \
1856 if (C2 == from - 1) \
1857 RANGE_TABLE_WORK_ELT (work_area, I)--; \
1858 else if (C2 == to + 1) \
1859 RANGE_TABLE_WORK_ELT (work_area, I + 1)++; \
1860 break; \
1863 if (I < USED) \
1864 SET_RANGE_TABLE_WORK_AREA ((work_area), C2, C2); \
1867 } while (0)
1870 /* Both FROM and TO are multibyte characters. */
1872 #define SETUP_MULTIBYTE_RANGE(work_area, FROM, TO) \
1873 do { \
1874 int C0, C1, C2, I, USED = RANGE_TABLE_WORK_USED (work_area); \
1876 SET_RANGE_TABLE_WORK_AREA ((work_area), (FROM), (TO)); \
1877 for (C0 = (FROM); C0 <= (TO); C0++) \
1879 C1 = TRANSLATE (C0); \
1880 if ((C2 = RE_CHAR_TO_UNIBYTE (C1)) >= 0 \
1881 || (C1 != C0 && (C2 = RE_CHAR_TO_UNIBYTE (C0)) >= 0)) \
1882 SET_LIST_BIT (C2); \
1883 if (C1 >= (FROM) && C1 <= (TO)) \
1884 continue; \
1885 for (I = RANGE_TABLE_WORK_USED (work_area) - 2; I >= USED; I -= 2) \
1887 int from = RANGE_TABLE_WORK_ELT (work_area, I); \
1888 int to = RANGE_TABLE_WORK_ELT (work_area, I + 1); \
1890 if (C1 >= from - 1 && C1 <= to + 1) \
1892 if (C1 == from - 1) \
1893 RANGE_TABLE_WORK_ELT (work_area, I)--; \
1894 else if (C1 == to + 1) \
1895 RANGE_TABLE_WORK_ELT (work_area, I + 1)++; \
1896 break; \
1899 if (I < USED) \
1900 SET_RANGE_TABLE_WORK_AREA ((work_area), C1, C1); \
1902 } while (0)
1904 #endif /* emacs */
1906 /* Get the next unsigned number in the uncompiled pattern. */
1907 #define GET_INTERVAL_COUNT(num) \
1908 do { \
1909 if (p == pend) \
1910 FREE_STACK_RETURN (REG_EBRACE); \
1911 else \
1913 PATFETCH (c); \
1914 while ('0' <= c && c <= '9') \
1916 if (num < 0) \
1917 num = 0; \
1918 if (RE_DUP_MAX / 10 - (RE_DUP_MAX % 10 < c - '0') < num) \
1919 FREE_STACK_RETURN (REG_BADBR); \
1920 num = num * 10 + c - '0'; \
1921 if (p == pend) \
1922 FREE_STACK_RETURN (REG_EBRACE); \
1923 PATFETCH (c); \
1926 } while (0)
1928 #if ! WIDE_CHAR_SUPPORT
1930 /* Parse a character class, i.e. string such as "[:name:]". *strp
1931 points to the string to be parsed and limit is length, in bytes, of
1932 that string.
1934 If *strp point to a string that begins with "[:name:]", where name is
1935 a non-empty sequence of lower case letters, *strp will be advanced past the
1936 closing square bracket and RECC_* constant which maps to the name will be
1937 returned. If name is not a valid character class name zero, or RECC_ERROR,
1938 is returned.
1940 Otherwise, if *strp doesn’t begin with "[:name:]", -1 is returned.
1942 The function can be used on ASCII and multibyte (UTF-8-encoded) strings.
1944 re_wctype_t
1945 re_wctype_parse (const unsigned char **strp, unsigned limit)
1947 const char *beg = (const char *)*strp, *it;
1949 if (limit < 4 || beg[0] != '[' || beg[1] != ':')
1950 return -1;
1952 beg += 2; /* skip opening ‘[:’ */
1953 limit -= 3; /* opening ‘[:’ and half of closing ‘:]’; --limit handles rest */
1954 for (it = beg; it[0] != ':' || it[1] != ']'; ++it)
1955 if (!--limit)
1956 return -1;
1958 *strp = (const unsigned char *)(it + 2);
1960 /* Sort tests in the length=five case by frequency the classes to minimize
1961 number of times we fail the comparison. The frequencies of character class
1962 names used in Emacs sources as of 2016-07-27:
1964 $ find \( -name \*.c -o -name \*.el \) -exec grep -h '\[:[a-z]*:]' {} + |
1965 sed 's/]/]\n/g' |grep -o '\[:[a-z]*:]' |sort |uniq -c |sort -nr
1966 213 [:alnum:]
1967 104 [:alpha:]
1968 62 [:space:]
1969 39 [:digit:]
1970 36 [:blank:]
1971 26 [:word:]
1972 26 [:upper:]
1973 21 [:lower:]
1974 10 [:xdigit:]
1975 10 [:punct:]
1976 10 [:ascii:]
1977 4 [:nonascii:]
1978 4 [:graph:]
1979 2 [:print:]
1980 2 [:cntrl:]
1981 1 [:ff:]
1983 If you update this list, consider also updating chain of or’ed conditions
1984 in execute_charset function.
1987 switch (it - beg) {
1988 case 4:
1989 if (!memcmp (beg, "word", 4)) return RECC_WORD;
1990 break;
1991 case 5:
1992 if (!memcmp (beg, "alnum", 5)) return RECC_ALNUM;
1993 if (!memcmp (beg, "alpha", 5)) return RECC_ALPHA;
1994 if (!memcmp (beg, "space", 5)) return RECC_SPACE;
1995 if (!memcmp (beg, "digit", 5)) return RECC_DIGIT;
1996 if (!memcmp (beg, "blank", 5)) return RECC_BLANK;
1997 if (!memcmp (beg, "upper", 5)) return RECC_UPPER;
1998 if (!memcmp (beg, "lower", 5)) return RECC_LOWER;
1999 if (!memcmp (beg, "punct", 5)) return RECC_PUNCT;
2000 if (!memcmp (beg, "ascii", 5)) return RECC_ASCII;
2001 if (!memcmp (beg, "graph", 5)) return RECC_GRAPH;
2002 if (!memcmp (beg, "print", 5)) return RECC_PRINT;
2003 if (!memcmp (beg, "cntrl", 5)) return RECC_CNTRL;
2004 break;
2005 case 6:
2006 if (!memcmp (beg, "xdigit", 6)) return RECC_XDIGIT;
2007 break;
2008 case 7:
2009 if (!memcmp (beg, "unibyte", 7)) return RECC_UNIBYTE;
2010 break;
2011 case 8:
2012 if (!memcmp (beg, "nonascii", 8)) return RECC_NONASCII;
2013 break;
2014 case 9:
2015 if (!memcmp (beg, "multibyte", 9)) return RECC_MULTIBYTE;
2016 break;
2019 return RECC_ERROR;
2022 /* True if CH is in the char class CC. */
2023 boolean
2024 re_iswctype (int ch, re_wctype_t cc)
2026 switch (cc)
2028 case RECC_ALNUM: return ISALNUM (ch) != 0;
2029 case RECC_ALPHA: return ISALPHA (ch) != 0;
2030 case RECC_BLANK: return ISBLANK (ch) != 0;
2031 case RECC_CNTRL: return ISCNTRL (ch) != 0;
2032 case RECC_DIGIT: return ISDIGIT (ch) != 0;
2033 case RECC_GRAPH: return ISGRAPH (ch) != 0;
2034 case RECC_LOWER: return ISLOWER (ch) != 0;
2035 case RECC_PRINT: return ISPRINT (ch) != 0;
2036 case RECC_PUNCT: return ISPUNCT (ch) != 0;
2037 case RECC_SPACE: return ISSPACE (ch) != 0;
2038 case RECC_UPPER: return ISUPPER (ch) != 0;
2039 case RECC_XDIGIT: return ISXDIGIT (ch) != 0;
2040 case RECC_ASCII: return IS_REAL_ASCII (ch) != 0;
2041 case RECC_NONASCII: return !IS_REAL_ASCII (ch);
2042 case RECC_UNIBYTE: return ISUNIBYTE (ch) != 0;
2043 case RECC_MULTIBYTE: return !ISUNIBYTE (ch);
2044 case RECC_WORD: return ISWORD (ch) != 0;
2045 case RECC_ERROR: return false;
2046 default:
2047 abort ();
2051 /* Return a bit-pattern to use in the range-table bits to match multibyte
2052 chars of class CC. */
2053 static int
2054 re_wctype_to_bit (re_wctype_t cc)
2056 switch (cc)
2058 case RECC_NONASCII:
2059 case RECC_MULTIBYTE: return BIT_MULTIBYTE;
2060 case RECC_ALPHA: return BIT_ALPHA;
2061 case RECC_ALNUM: return BIT_ALNUM;
2062 case RECC_WORD: return BIT_WORD;
2063 case RECC_LOWER: return BIT_LOWER;
2064 case RECC_UPPER: return BIT_UPPER;
2065 case RECC_PUNCT: return BIT_PUNCT;
2066 case RECC_SPACE: return BIT_SPACE;
2067 case RECC_GRAPH: return BIT_GRAPH;
2068 case RECC_PRINT: return BIT_PRINT;
2069 case RECC_ASCII: case RECC_DIGIT: case RECC_XDIGIT: case RECC_CNTRL:
2070 case RECC_BLANK: case RECC_UNIBYTE: case RECC_ERROR: return 0;
2071 default:
2072 abort ();
2075 #endif
2077 /* Filling in the work area of a range. */
2079 /* Actually extend the space in WORK_AREA. */
2081 static void
2082 extend_range_table_work_area (struct range_table_work_area *work_area)
2084 work_area->allocated += 16 * sizeof (int);
2085 work_area->table = realloc (work_area->table, work_area->allocated);
2088 #if 0
2089 #ifdef emacs
2091 /* Carefully find the ranges of codes that are equivalent
2092 under case conversion to the range start..end when passed through
2093 TRANSLATE. Handle the case where non-letters can come in between
2094 two upper-case letters (which happens in Latin-1).
2095 Also handle the case of groups of more than 2 case-equivalent chars.
2097 The basic method is to look at consecutive characters and see
2098 if they can form a run that can be handled as one.
2100 Returns -1 if successful, REG_ESPACE if ran out of space. */
2102 static int
2103 set_image_of_range_1 (struct range_table_work_area *work_area,
2104 re_wchar_t start, re_wchar_t end,
2105 RE_TRANSLATE_TYPE translate)
2107 /* `one_case' indicates a character, or a run of characters,
2108 each of which is an isolate (no case-equivalents).
2109 This includes all ASCII non-letters.
2111 `two_case' indicates a character, or a run of characters,
2112 each of which has two case-equivalent forms.
2113 This includes all ASCII letters.
2115 `strange' indicates a character that has more than one
2116 case-equivalent. */
2118 enum case_type {one_case, two_case, strange};
2120 /* Describe the run that is in progress,
2121 which the next character can try to extend.
2122 If run_type is strange, that means there really is no run.
2123 If run_type is one_case, then run_start...run_end is the run.
2124 If run_type is two_case, then the run is run_start...run_end,
2125 and the case-equivalents end at run_eqv_end. */
2127 enum case_type run_type = strange;
2128 int run_start, run_end, run_eqv_end;
2130 Lisp_Object eqv_table;
2132 if (!RE_TRANSLATE_P (translate))
2134 EXTEND_RANGE_TABLE (work_area, 2);
2135 work_area->table[work_area->used++] = (start);
2136 work_area->table[work_area->used++] = (end);
2137 return -1;
2140 eqv_table = XCHAR_TABLE (translate)->extras[2];
2142 for (; start <= end; start++)
2144 enum case_type this_type;
2145 int eqv = RE_TRANSLATE (eqv_table, start);
2146 int minchar, maxchar;
2148 /* Classify this character */
2149 if (eqv == start)
2150 this_type = one_case;
2151 else if (RE_TRANSLATE (eqv_table, eqv) == start)
2152 this_type = two_case;
2153 else
2154 this_type = strange;
2156 if (start < eqv)
2157 minchar = start, maxchar = eqv;
2158 else
2159 minchar = eqv, maxchar = start;
2161 /* Can this character extend the run in progress? */
2162 if (this_type == strange || this_type != run_type
2163 || !(minchar == run_end + 1
2164 && (run_type == two_case
2165 ? maxchar == run_eqv_end + 1 : 1)))
2167 /* No, end the run.
2168 Record each of its equivalent ranges. */
2169 if (run_type == one_case)
2171 EXTEND_RANGE_TABLE (work_area, 2);
2172 work_area->table[work_area->used++] = run_start;
2173 work_area->table[work_area->used++] = run_end;
2175 else if (run_type == two_case)
2177 EXTEND_RANGE_TABLE (work_area, 4);
2178 work_area->table[work_area->used++] = run_start;
2179 work_area->table[work_area->used++] = run_end;
2180 work_area->table[work_area->used++]
2181 = RE_TRANSLATE (eqv_table, run_start);
2182 work_area->table[work_area->used++]
2183 = RE_TRANSLATE (eqv_table, run_end);
2185 run_type = strange;
2188 if (this_type == strange)
2190 /* For a strange character, add each of its equivalents, one
2191 by one. Don't start a range. */
2194 EXTEND_RANGE_TABLE (work_area, 2);
2195 work_area->table[work_area->used++] = eqv;
2196 work_area->table[work_area->used++] = eqv;
2197 eqv = RE_TRANSLATE (eqv_table, eqv);
2199 while (eqv != start);
2202 /* Add this char to the run, or start a new run. */
2203 else if (run_type == strange)
2205 /* Initialize a new range. */
2206 run_type = this_type;
2207 run_start = start;
2208 run_end = start;
2209 run_eqv_end = RE_TRANSLATE (eqv_table, run_end);
2211 else
2213 /* Extend a running range. */
2214 run_end = minchar;
2215 run_eqv_end = RE_TRANSLATE (eqv_table, run_end);
2219 /* If a run is still in progress at the end, finish it now
2220 by recording its equivalent ranges. */
2221 if (run_type == one_case)
2223 EXTEND_RANGE_TABLE (work_area, 2);
2224 work_area->table[work_area->used++] = run_start;
2225 work_area->table[work_area->used++] = run_end;
2227 else if (run_type == two_case)
2229 EXTEND_RANGE_TABLE (work_area, 4);
2230 work_area->table[work_area->used++] = run_start;
2231 work_area->table[work_area->used++] = run_end;
2232 work_area->table[work_area->used++]
2233 = RE_TRANSLATE (eqv_table, run_start);
2234 work_area->table[work_area->used++]
2235 = RE_TRANSLATE (eqv_table, run_end);
2238 return -1;
2241 #endif /* emacs */
2243 /* Record the image of the range start..end when passed through
2244 TRANSLATE. This is not necessarily TRANSLATE(start)..TRANSLATE(end)
2245 and is not even necessarily contiguous.
2246 Normally we approximate it with the smallest contiguous range that contains
2247 all the chars we need. However, for Latin-1 we go to extra effort
2248 to do a better job.
2250 This function is not called for ASCII ranges.
2252 Returns -1 if successful, REG_ESPACE if ran out of space. */
2254 static int
2255 set_image_of_range (struct range_table_work_area *work_area,
2256 re_wchar_t start, re_wchar_t end,
2257 RE_TRANSLATE_TYPE translate)
2259 re_wchar_t cmin, cmax;
2261 #ifdef emacs
2262 /* For Latin-1 ranges, use set_image_of_range_1
2263 to get proper handling of ranges that include letters and nonletters.
2264 For a range that includes the whole of Latin-1, this is not necessary.
2265 For other character sets, we don't bother to get this right. */
2266 if (RE_TRANSLATE_P (translate) && start < 04400
2267 && !(start < 04200 && end >= 04377))
2269 int newend;
2270 int tem;
2271 newend = end;
2272 if (newend > 04377)
2273 newend = 04377;
2274 tem = set_image_of_range_1 (work_area, start, newend, translate);
2275 if (tem > 0)
2276 return tem;
2278 start = 04400;
2279 if (end < 04400)
2280 return -1;
2282 #endif
2284 EXTEND_RANGE_TABLE (work_area, 2);
2285 work_area->table[work_area->used++] = (start);
2286 work_area->table[work_area->used++] = (end);
2288 cmin = -1, cmax = -1;
2290 if (RE_TRANSLATE_P (translate))
2292 int ch;
2294 for (ch = start; ch <= end; ch++)
2296 re_wchar_t c = TRANSLATE (ch);
2297 if (! (start <= c && c <= end))
2299 if (cmin == -1)
2300 cmin = c, cmax = c;
2301 else
2303 cmin = min (cmin, c);
2304 cmax = max (cmax, c);
2309 if (cmin != -1)
2311 EXTEND_RANGE_TABLE (work_area, 2);
2312 work_area->table[work_area->used++] = (cmin);
2313 work_area->table[work_area->used++] = (cmax);
2317 return -1;
2319 #endif /* 0 */
2321 #ifndef MATCH_MAY_ALLOCATE
2323 /* If we cannot allocate large objects within re_match_2_internal,
2324 we make the fail stack and register vectors global.
2325 The fail stack, we grow to the maximum size when a regexp
2326 is compiled.
2327 The register vectors, we adjust in size each time we
2328 compile a regexp, according to the number of registers it needs. */
2330 static fail_stack_type fail_stack;
2332 /* Size with which the following vectors are currently allocated.
2333 That is so we can make them bigger as needed,
2334 but never make them smaller. */
2335 static int regs_allocated_size;
2337 static re_char ** regstart, ** regend;
2338 static re_char **best_regstart, **best_regend;
2340 /* Make the register vectors big enough for NUM_REGS registers,
2341 but don't make them smaller. */
2343 static
2344 regex_grow_registers (int num_regs)
2346 if (num_regs > regs_allocated_size)
2348 RETALLOC_IF (regstart, num_regs, re_char *);
2349 RETALLOC_IF (regend, num_regs, re_char *);
2350 RETALLOC_IF (best_regstart, num_regs, re_char *);
2351 RETALLOC_IF (best_regend, num_regs, re_char *);
2353 regs_allocated_size = num_regs;
2357 #endif /* not MATCH_MAY_ALLOCATE */
2359 static boolean group_in_compile_stack (compile_stack_type compile_stack,
2360 regnum_t regnum);
2362 /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
2363 Returns one of error codes defined in `regex.h', or zero for success.
2365 If WHITESPACE_REGEXP is given (only #ifdef emacs), it is used instead of
2366 a space character in PATTERN.
2368 Assumes the `allocated' (and perhaps `buffer') and `translate'
2369 fields are set in BUFP on entry.
2371 If it succeeds, results are put in BUFP (if it returns an error, the
2372 contents of BUFP are undefined):
2373 `buffer' is the compiled pattern;
2374 `syntax' is set to SYNTAX;
2375 `used' is set to the length of the compiled pattern;
2376 `fastmap_accurate' is zero;
2377 `re_nsub' is the number of subexpressions in PATTERN;
2378 `not_bol' and `not_eol' are zero;
2380 The `fastmap' field is neither examined nor set. */
2382 /* Insert the `jump' from the end of last alternative to "here".
2383 The space for the jump has already been allocated. */
2384 #define FIXUP_ALT_JUMP() \
2385 do { \
2386 if (fixup_alt_jump) \
2387 STORE_JUMP (jump, fixup_alt_jump, b); \
2388 } while (0)
2391 /* Return, freeing storage we allocated. */
2392 #define FREE_STACK_RETURN(value) \
2393 do { \
2394 FREE_RANGE_TABLE_WORK_AREA (range_table_work); \
2395 free (compile_stack.stack); \
2396 return value; \
2397 } while (0)
2399 static reg_errcode_t
2400 regex_compile (const_re_char *pattern, size_t size,
2401 #ifdef emacs
2402 # define syntax RE_SYNTAX_EMACS
2403 bool posix_backtracking,
2404 const char *whitespace_regexp,
2405 #else
2406 reg_syntax_t syntax,
2407 # define posix_backtracking (!(syntax & RE_NO_POSIX_BACKTRACKING))
2408 #endif
2409 struct re_pattern_buffer *bufp)
2411 /* We fetch characters from PATTERN here. */
2412 register re_wchar_t c, c1;
2414 /* Points to the end of the buffer, where we should append. */
2415 register unsigned char *b;
2417 /* Keeps track of unclosed groups. */
2418 compile_stack_type compile_stack;
2420 /* Points to the current (ending) position in the pattern. */
2421 #ifdef AIX
2422 /* `const' makes AIX compiler fail. */
2423 unsigned char *p = pattern;
2424 #else
2425 re_char *p = pattern;
2426 #endif
2427 re_char *pend = pattern + size;
2429 /* How to translate the characters in the pattern. */
2430 RE_TRANSLATE_TYPE translate = bufp->translate;
2432 /* Address of the count-byte of the most recently inserted `exactn'
2433 command. This makes it possible to tell if a new exact-match
2434 character can be added to that command or if the character requires
2435 a new `exactn' command. */
2436 unsigned char *pending_exact = 0;
2438 /* Address of start of the most recently finished expression.
2439 This tells, e.g., postfix * where to find the start of its
2440 operand. Reset at the beginning of groups and alternatives. */
2441 unsigned char *laststart = 0;
2443 /* Address of beginning of regexp, or inside of last group. */
2444 unsigned char *begalt;
2446 /* Place in the uncompiled pattern (i.e., the {) to
2447 which to go back if the interval is invalid. */
2448 re_char *beg_interval;
2450 /* Address of the place where a forward jump should go to the end of
2451 the containing expression. Each alternative of an `or' -- except the
2452 last -- ends with a forward jump of this sort. */
2453 unsigned char *fixup_alt_jump = 0;
2455 /* Work area for range table of charset. */
2456 struct range_table_work_area range_table_work;
2458 /* If the object matched can contain multibyte characters. */
2459 const boolean multibyte = RE_MULTIBYTE_P (bufp);
2461 #ifdef emacs
2462 /* Nonzero if we have pushed down into a subpattern. */
2463 int in_subpattern = 0;
2465 /* These hold the values of p, pattern, and pend from the main
2466 pattern when we have pushed into a subpattern. */
2467 re_char *main_p;
2468 re_char *main_pattern;
2469 re_char *main_pend;
2470 #endif
2472 #ifdef DEBUG
2473 debug++;
2474 DEBUG_PRINT ("\nCompiling pattern: ");
2475 if (debug > 0)
2477 unsigned debug_count;
2479 for (debug_count = 0; debug_count < size; debug_count++)
2480 putchar (pattern[debug_count]);
2481 putchar ('\n');
2483 #endif /* DEBUG */
2485 /* Initialize the compile stack. */
2486 compile_stack.stack = TALLOC (INIT_COMPILE_STACK_SIZE, compile_stack_elt_t);
2487 if (compile_stack.stack == NULL)
2488 return REG_ESPACE;
2490 compile_stack.size = INIT_COMPILE_STACK_SIZE;
2491 compile_stack.avail = 0;
2493 range_table_work.table = 0;
2494 range_table_work.allocated = 0;
2496 /* Initialize the pattern buffer. */
2497 #ifndef emacs
2498 bufp->syntax = syntax;
2499 #endif
2500 bufp->fastmap_accurate = 0;
2501 bufp->not_bol = bufp->not_eol = 0;
2502 bufp->used_syntax = 0;
2504 /* Set `used' to zero, so that if we return an error, the pattern
2505 printer (for debugging) will think there's no pattern. We reset it
2506 at the end. */
2507 bufp->used = 0;
2509 /* Always count groups, whether or not bufp->no_sub is set. */
2510 bufp->re_nsub = 0;
2512 #if !defined emacs && !defined SYNTAX_TABLE
2513 /* Initialize the syntax table. */
2514 init_syntax_once ();
2515 #endif
2517 if (bufp->allocated == 0)
2519 if (bufp->buffer)
2520 { /* If zero allocated, but buffer is non-null, try to realloc
2521 enough space. This loses if buffer's address is bogus, but
2522 that is the user's responsibility. */
2523 RETALLOC (bufp->buffer, INIT_BUF_SIZE, unsigned char);
2525 else
2526 { /* Caller did not allocate a buffer. Do it for them. */
2527 bufp->buffer = TALLOC (INIT_BUF_SIZE, unsigned char);
2529 if (!bufp->buffer) FREE_STACK_RETURN (REG_ESPACE);
2531 bufp->allocated = INIT_BUF_SIZE;
2534 begalt = b = bufp->buffer;
2536 /* Loop through the uncompiled pattern until we're at the end. */
2537 while (1)
2539 if (p == pend)
2541 #ifdef emacs
2542 /* If this is the end of an included regexp,
2543 pop back to the main regexp and try again. */
2544 if (in_subpattern)
2546 in_subpattern = 0;
2547 pattern = main_pattern;
2548 p = main_p;
2549 pend = main_pend;
2550 continue;
2552 #endif
2553 /* If this is the end of the main regexp, we are done. */
2554 break;
2557 PATFETCH (c);
2559 switch (c)
2561 #ifdef emacs
2562 case ' ':
2564 re_char *p1 = p;
2566 /* If there's no special whitespace regexp, treat
2567 spaces normally. And don't try to do this recursively. */
2568 if (!whitespace_regexp || in_subpattern)
2569 goto normal_char;
2571 /* Peek past following spaces. */
2572 while (p1 != pend)
2574 if (*p1 != ' ')
2575 break;
2576 p1++;
2578 /* If the spaces are followed by a repetition op,
2579 treat them normally. */
2580 if (p1 != pend
2581 && (*p1 == '*' || *p1 == '+' || *p1 == '?'
2582 || (*p1 == '\\' && p1 + 1 != pend && p1[1] == '{')))
2583 goto normal_char;
2585 /* Replace the spaces with the whitespace regexp. */
2586 in_subpattern = 1;
2587 main_p = p1;
2588 main_pend = pend;
2589 main_pattern = pattern;
2590 p = pattern = (re_char *) whitespace_regexp;
2591 pend = p + strlen (whitespace_regexp);
2592 break;
2594 #endif
2596 case '^':
2598 if ( /* If at start of pattern, it's an operator. */
2599 p == pattern + 1
2600 /* If context independent, it's an operator. */
2601 || syntax & RE_CONTEXT_INDEP_ANCHORS
2602 /* Otherwise, depends on what's come before. */
2603 || at_begline_loc_p (pattern, p, syntax))
2604 BUF_PUSH ((syntax & RE_NO_NEWLINE_ANCHOR) ? begbuf : begline);
2605 else
2606 goto normal_char;
2608 break;
2611 case '$':
2613 if ( /* If at end of pattern, it's an operator. */
2614 p == pend
2615 /* If context independent, it's an operator. */
2616 || syntax & RE_CONTEXT_INDEP_ANCHORS
2617 /* Otherwise, depends on what's next. */
2618 || at_endline_loc_p (p, pend, syntax))
2619 BUF_PUSH ((syntax & RE_NO_NEWLINE_ANCHOR) ? endbuf : endline);
2620 else
2621 goto normal_char;
2623 break;
2626 case '+':
2627 case '?':
2628 if ((syntax & RE_BK_PLUS_QM)
2629 || (syntax & RE_LIMITED_OPS))
2630 goto normal_char;
2631 handle_plus:
2632 case '*':
2633 /* If there is no previous pattern... */
2634 if (!laststart)
2636 if (syntax & RE_CONTEXT_INVALID_OPS)
2637 FREE_STACK_RETURN (REG_BADRPT);
2638 else if (!(syntax & RE_CONTEXT_INDEP_OPS))
2639 goto normal_char;
2643 /* 1 means zero (many) matches is allowed. */
2644 boolean zero_times_ok = 0, many_times_ok = 0;
2645 boolean greedy = 1;
2647 /* If there is a sequence of repetition chars, collapse it
2648 down to just one (the right one). We can't combine
2649 interval operators with these because of, e.g., `a{2}*',
2650 which should only match an even number of `a's. */
2652 for (;;)
2654 if ((syntax & RE_FRUGAL)
2655 && c == '?' && (zero_times_ok || many_times_ok))
2656 greedy = 0;
2657 else
2659 zero_times_ok |= c != '+';
2660 many_times_ok |= c != '?';
2663 if (p == pend)
2664 break;
2665 else if (*p == '*'
2666 || (!(syntax & RE_BK_PLUS_QM)
2667 && (*p == '+' || *p == '?')))
2669 else if (syntax & RE_BK_PLUS_QM && *p == '\\')
2671 if (p+1 == pend)
2672 FREE_STACK_RETURN (REG_EESCAPE);
2673 if (p[1] == '+' || p[1] == '?')
2674 PATFETCH (c); /* Gobble up the backslash. */
2675 else
2676 break;
2678 else
2679 break;
2680 /* If we get here, we found another repeat character. */
2681 PATFETCH (c);
2684 /* Star, etc. applied to an empty pattern is equivalent
2685 to an empty pattern. */
2686 if (!laststart || laststart == b)
2687 break;
2689 /* Now we know whether or not zero matches is allowed
2690 and also whether or not two or more matches is allowed. */
2691 if (greedy)
2693 if (many_times_ok)
2695 boolean simple = skip_one_char (laststart) == b;
2696 size_t startoffset = 0;
2697 re_opcode_t ofj =
2698 /* Check if the loop can match the empty string. */
2699 (simple || !analyze_first (laststart, b, NULL, 0))
2700 ? on_failure_jump : on_failure_jump_loop;
2701 assert (skip_one_char (laststart) <= b);
2703 if (!zero_times_ok && simple)
2704 { /* Since simple * loops can be made faster by using
2705 on_failure_keep_string_jump, we turn simple P+
2706 into PP* if P is simple. */
2707 unsigned char *p1, *p2;
2708 startoffset = b - laststart;
2709 GET_BUFFER_SPACE (startoffset);
2710 p1 = b; p2 = laststart;
2711 while (p2 < p1)
2712 *b++ = *p2++;
2713 zero_times_ok = 1;
2716 GET_BUFFER_SPACE (6);
2717 if (!zero_times_ok)
2718 /* A + loop. */
2719 STORE_JUMP (ofj, b, b + 6);
2720 else
2721 /* Simple * loops can use on_failure_keep_string_jump
2722 depending on what follows. But since we don't know
2723 that yet, we leave the decision up to
2724 on_failure_jump_smart. */
2725 INSERT_JUMP (simple ? on_failure_jump_smart : ofj,
2726 laststart + startoffset, b + 6);
2727 b += 3;
2728 STORE_JUMP (jump, b, laststart + startoffset);
2729 b += 3;
2731 else
2733 /* A simple ? pattern. */
2734 assert (zero_times_ok);
2735 GET_BUFFER_SPACE (3);
2736 INSERT_JUMP (on_failure_jump, laststart, b + 3);
2737 b += 3;
2740 else /* not greedy */
2741 { /* I wish the greedy and non-greedy cases could be merged. */
2743 GET_BUFFER_SPACE (7); /* We might use less. */
2744 if (many_times_ok)
2746 boolean emptyp = analyze_first (laststart, b, NULL, 0);
2748 /* The non-greedy multiple match looks like
2749 a repeat..until: we only need a conditional jump
2750 at the end of the loop. */
2751 if (emptyp) BUF_PUSH (no_op);
2752 STORE_JUMP (emptyp ? on_failure_jump_nastyloop
2753 : on_failure_jump, b, laststart);
2754 b += 3;
2755 if (zero_times_ok)
2757 /* The repeat...until naturally matches one or more.
2758 To also match zero times, we need to first jump to
2759 the end of the loop (its conditional jump). */
2760 INSERT_JUMP (jump, laststart, b);
2761 b += 3;
2764 else
2766 /* non-greedy a?? */
2767 INSERT_JUMP (jump, laststart, b + 3);
2768 b += 3;
2769 INSERT_JUMP (on_failure_jump, laststart, laststart + 6);
2770 b += 3;
2774 pending_exact = 0;
2775 break;
2778 case '.':
2779 laststart = b;
2780 BUF_PUSH (anychar);
2781 break;
2784 case '[':
2786 re_char *p1;
2788 CLEAR_RANGE_TABLE_WORK_USED (range_table_work);
2790 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2792 /* Ensure that we have enough space to push a charset: the
2793 opcode, the length count, and the bitset; 34 bytes in all. */
2794 GET_BUFFER_SPACE (34);
2796 laststart = b;
2798 /* We test `*p == '^' twice, instead of using an if
2799 statement, so we only need one BUF_PUSH. */
2800 BUF_PUSH (*p == '^' ? charset_not : charset);
2801 if (*p == '^')
2802 p++;
2804 /* Remember the first position in the bracket expression. */
2805 p1 = p;
2807 /* Push the number of bytes in the bitmap. */
2808 BUF_PUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
2810 /* Clear the whole map. */
2811 memset (b, 0, (1 << BYTEWIDTH) / BYTEWIDTH);
2813 /* charset_not matches newline according to a syntax bit. */
2814 if ((re_opcode_t) b[-2] == charset_not
2815 && (syntax & RE_HAT_LISTS_NOT_NEWLINE))
2816 SET_LIST_BIT ('\n');
2818 /* Read in characters and ranges, setting map bits. */
2819 for (;;)
2821 boolean escaped_char = false;
2822 const unsigned char *p2 = p;
2823 re_wctype_t cc;
2824 re_wchar_t ch;
2826 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2828 /* See if we're at the beginning of a possible character
2829 class. */
2830 if (syntax & RE_CHAR_CLASSES &&
2831 (cc = re_wctype_parse(&p, pend - p)) != -1)
2833 if (cc == 0)
2834 FREE_STACK_RETURN (REG_ECTYPE);
2836 if (p == pend)
2837 FREE_STACK_RETURN (REG_EBRACK);
2839 #ifndef emacs
2840 for (ch = 0; ch < (1 << BYTEWIDTH); ++ch)
2841 if (re_iswctype (btowc (ch), cc))
2843 c = TRANSLATE (ch);
2844 if (c < (1 << BYTEWIDTH))
2845 SET_LIST_BIT (c);
2847 #else /* emacs */
2848 /* Most character classes in a multibyte match just set
2849 a flag. Exceptions are is_blank, is_digit, is_cntrl, and
2850 is_xdigit, since they can only match ASCII characters.
2851 We don't need to handle them for multibyte. */
2853 /* Setup the gl_state object to its buffer-defined value.
2854 This hardcodes the buffer-global syntax-table for ASCII
2855 chars, while the other chars will obey syntax-table
2856 properties. It's not ideal, but it's the way it's been
2857 done until now. */
2858 SETUP_BUFFER_SYNTAX_TABLE ();
2860 for (c = 0; c < 0x80; ++c)
2861 if (re_iswctype (c, cc))
2863 SET_LIST_BIT (c);
2864 c1 = TRANSLATE (c);
2865 if (c1 == c)
2866 continue;
2867 if (ASCII_CHAR_P (c1))
2868 SET_LIST_BIT (c1);
2869 else if ((c1 = RE_CHAR_TO_UNIBYTE (c1)) >= 0)
2870 SET_LIST_BIT (c1);
2872 SET_RANGE_TABLE_WORK_AREA_BIT
2873 (range_table_work, re_wctype_to_bit (cc));
2874 #endif /* emacs */
2875 /* In most cases the matching rule for char classes only
2876 uses the syntax table for multibyte chars, so that the
2877 content of the syntax-table is not hardcoded in the
2878 range_table. SPACE and WORD are the two exceptions. */
2879 if ((1 << cc) & ((1 << RECC_SPACE) | (1 << RECC_WORD)))
2880 bufp->used_syntax = 1;
2882 /* Repeat the loop. */
2883 continue;
2886 /* Don't translate yet. The range TRANSLATE(X..Y) cannot
2887 always be determined from TRANSLATE(X) and TRANSLATE(Y)
2888 So the translation is done later in a loop. Example:
2889 (let ((case-fold-search t)) (string-match "[A-_]" "A")) */
2890 PATFETCH (c);
2892 /* \ might escape characters inside [...] and [^...]. */
2893 if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) && c == '\\')
2895 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
2897 PATFETCH (c);
2898 escaped_char = true;
2900 else
2902 /* Could be the end of the bracket expression. If it's
2903 not (i.e., when the bracket expression is `[]' so
2904 far), the ']' character bit gets set way below. */
2905 if (c == ']' && p2 != p1)
2906 break;
2909 if (p < pend && p[0] == '-' && p[1] != ']')
2912 /* Discard the `-'. */
2913 PATFETCH (c1);
2915 /* Fetch the character which ends the range. */
2916 PATFETCH (c1);
2917 #ifdef emacs
2918 if (CHAR_BYTE8_P (c1)
2919 && ! ASCII_CHAR_P (c) && ! CHAR_BYTE8_P (c))
2920 /* Treat the range from a multibyte character to
2921 raw-byte character as empty. */
2922 c = c1 + 1;
2923 #endif /* emacs */
2925 else
2926 /* Range from C to C. */
2927 c1 = c;
2929 if (c > c1)
2931 if (syntax & RE_NO_EMPTY_RANGES)
2932 FREE_STACK_RETURN (REG_ERANGEX);
2933 /* Else, repeat the loop. */
2935 else
2937 #ifndef emacs
2938 /* Set the range into bitmap */
2939 for (; c <= c1; c++)
2941 ch = TRANSLATE (c);
2942 if (ch < (1 << BYTEWIDTH))
2943 SET_LIST_BIT (ch);
2945 #else /* emacs */
2946 if (c < 128)
2948 ch = min (127, c1);
2949 SETUP_ASCII_RANGE (range_table_work, c, ch);
2950 c = ch + 1;
2951 if (CHAR_BYTE8_P (c1))
2952 c = BYTE8_TO_CHAR (128);
2954 if (c <= c1)
2956 if (CHAR_BYTE8_P (c))
2958 c = CHAR_TO_BYTE8 (c);
2959 c1 = CHAR_TO_BYTE8 (c1);
2960 for (; c <= c1; c++)
2961 SET_LIST_BIT (c);
2963 else if (multibyte)
2965 SETUP_MULTIBYTE_RANGE (range_table_work, c, c1);
2967 else
2969 SETUP_UNIBYTE_RANGE (range_table_work, c, c1);
2972 #endif /* emacs */
2976 /* Discard any (non)matching list bytes that are all 0 at the
2977 end of the map. Decrease the map-length byte too. */
2978 while ((int) b[-1] > 0 && b[b[-1] - 1] == 0)
2979 b[-1]--;
2980 b += b[-1];
2982 /* Build real range table from work area. */
2983 if (RANGE_TABLE_WORK_USED (range_table_work)
2984 || RANGE_TABLE_WORK_BITS (range_table_work))
2986 int i;
2987 int used = RANGE_TABLE_WORK_USED (range_table_work);
2989 /* Allocate space for COUNT + RANGE_TABLE. Needs two
2990 bytes for flags, two for COUNT, and three bytes for
2991 each character. */
2992 GET_BUFFER_SPACE (4 + used * 3);
2994 /* Indicate the existence of range table. */
2995 laststart[1] |= 0x80;
2997 /* Store the character class flag bits into the range table.
2998 If not in emacs, these flag bits are always 0. */
2999 *b++ = RANGE_TABLE_WORK_BITS (range_table_work) & 0xff;
3000 *b++ = RANGE_TABLE_WORK_BITS (range_table_work) >> 8;
3002 STORE_NUMBER_AND_INCR (b, used / 2);
3003 for (i = 0; i < used; i++)
3004 STORE_CHARACTER_AND_INCR
3005 (b, RANGE_TABLE_WORK_ELT (range_table_work, i));
3008 break;
3011 case '(':
3012 if (syntax & RE_NO_BK_PARENS)
3013 goto handle_open;
3014 else
3015 goto normal_char;
3018 case ')':
3019 if (syntax & RE_NO_BK_PARENS)
3020 goto handle_close;
3021 else
3022 goto normal_char;
3025 case '\n':
3026 if (syntax & RE_NEWLINE_ALT)
3027 goto handle_alt;
3028 else
3029 goto normal_char;
3032 case '|':
3033 if (syntax & RE_NO_BK_VBAR)
3034 goto handle_alt;
3035 else
3036 goto normal_char;
3039 case '{':
3040 if (syntax & RE_INTERVALS && syntax & RE_NO_BK_BRACES)
3041 goto handle_interval;
3042 else
3043 goto normal_char;
3046 case '\\':
3047 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
3049 /* Do not translate the character after the \, so that we can
3050 distinguish, e.g., \B from \b, even if we normally would
3051 translate, e.g., B to b. */
3052 PATFETCH (c);
3054 switch (c)
3056 case '(':
3057 if (syntax & RE_NO_BK_PARENS)
3058 goto normal_backslash;
3060 handle_open:
3062 int shy = 0;
3063 regnum_t regnum = 0;
3064 if (p+1 < pend)
3066 /* Look for a special (?...) construct */
3067 if ((syntax & RE_SHY_GROUPS) && *p == '?')
3069 PATFETCH (c); /* Gobble up the '?'. */
3070 while (!shy)
3072 PATFETCH (c);
3073 switch (c)
3075 case ':': shy = 1; break;
3076 case '0':
3077 /* An explicitly specified regnum must start
3078 with non-0. */
3079 if (regnum == 0)
3080 FREE_STACK_RETURN (REG_BADPAT);
3081 case '1': case '2': case '3': case '4':
3082 case '5': case '6': case '7': case '8': case '9':
3083 regnum = 10*regnum + (c - '0'); break;
3084 default:
3085 /* Only (?:...) is supported right now. */
3086 FREE_STACK_RETURN (REG_BADPAT);
3092 if (!shy)
3093 regnum = ++bufp->re_nsub;
3094 else if (regnum)
3095 { /* It's actually not shy, but explicitly numbered. */
3096 shy = 0;
3097 if (regnum > bufp->re_nsub)
3098 bufp->re_nsub = regnum;
3099 else if (regnum > bufp->re_nsub
3100 /* Ideally, we'd want to check that the specified
3101 group can't have matched (i.e. all subgroups
3102 using the same regnum are in other branches of
3103 OR patterns), but we don't currently keep track
3104 of enough info to do that easily. */
3105 || group_in_compile_stack (compile_stack, regnum))
3106 FREE_STACK_RETURN (REG_BADPAT);
3108 else
3109 /* It's really shy. */
3110 regnum = - bufp->re_nsub;
3112 if (COMPILE_STACK_FULL)
3114 RETALLOC (compile_stack.stack, compile_stack.size << 1,
3115 compile_stack_elt_t);
3116 if (compile_stack.stack == NULL) return REG_ESPACE;
3118 compile_stack.size <<= 1;
3121 /* These are the values to restore when we hit end of this
3122 group. They are all relative offsets, so that if the
3123 whole pattern moves because of realloc, they will still
3124 be valid. */
3125 COMPILE_STACK_TOP.begalt_offset = begalt - bufp->buffer;
3126 COMPILE_STACK_TOP.fixup_alt_jump
3127 = fixup_alt_jump ? fixup_alt_jump - bufp->buffer + 1 : 0;
3128 COMPILE_STACK_TOP.laststart_offset = b - bufp->buffer;
3129 COMPILE_STACK_TOP.regnum = regnum;
3131 /* Do not push a start_memory for groups beyond the last one
3132 we can represent in the compiled pattern. */
3133 if (regnum <= MAX_REGNUM && regnum > 0)
3134 BUF_PUSH_2 (start_memory, regnum);
3136 compile_stack.avail++;
3138 fixup_alt_jump = 0;
3139 laststart = 0;
3140 begalt = b;
3141 /* If we've reached MAX_REGNUM groups, then this open
3142 won't actually generate any code, so we'll have to
3143 clear pending_exact explicitly. */
3144 pending_exact = 0;
3145 break;
3148 case ')':
3149 if (syntax & RE_NO_BK_PARENS) goto normal_backslash;
3151 if (COMPILE_STACK_EMPTY)
3153 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
3154 goto normal_backslash;
3155 else
3156 FREE_STACK_RETURN (REG_ERPAREN);
3159 handle_close:
3160 FIXUP_ALT_JUMP ();
3162 /* See similar code for backslashed left paren above. */
3163 if (COMPILE_STACK_EMPTY)
3165 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
3166 goto normal_char;
3167 else
3168 FREE_STACK_RETURN (REG_ERPAREN);
3171 /* Since we just checked for an empty stack above, this
3172 ``can't happen''. */
3173 assert (compile_stack.avail != 0);
3175 /* We don't just want to restore into `regnum', because
3176 later groups should continue to be numbered higher,
3177 as in `(ab)c(de)' -- the second group is #2. */
3178 regnum_t regnum;
3180 compile_stack.avail--;
3181 begalt = bufp->buffer + COMPILE_STACK_TOP.begalt_offset;
3182 fixup_alt_jump
3183 = COMPILE_STACK_TOP.fixup_alt_jump
3184 ? bufp->buffer + COMPILE_STACK_TOP.fixup_alt_jump - 1
3185 : 0;
3186 laststart = bufp->buffer + COMPILE_STACK_TOP.laststart_offset;
3187 regnum = COMPILE_STACK_TOP.regnum;
3188 /* If we've reached MAX_REGNUM groups, then this open
3189 won't actually generate any code, so we'll have to
3190 clear pending_exact explicitly. */
3191 pending_exact = 0;
3193 /* We're at the end of the group, so now we know how many
3194 groups were inside this one. */
3195 if (regnum <= MAX_REGNUM && regnum > 0)
3196 BUF_PUSH_2 (stop_memory, regnum);
3198 break;
3201 case '|': /* `\|'. */
3202 if (syntax & RE_LIMITED_OPS || syntax & RE_NO_BK_VBAR)
3203 goto normal_backslash;
3204 handle_alt:
3205 if (syntax & RE_LIMITED_OPS)
3206 goto normal_char;
3208 /* Insert before the previous alternative a jump which
3209 jumps to this alternative if the former fails. */
3210 GET_BUFFER_SPACE (3);
3211 INSERT_JUMP (on_failure_jump, begalt, b + 6);
3212 pending_exact = 0;
3213 b += 3;
3215 /* The alternative before this one has a jump after it
3216 which gets executed if it gets matched. Adjust that
3217 jump so it will jump to this alternative's analogous
3218 jump (put in below, which in turn will jump to the next
3219 (if any) alternative's such jump, etc.). The last such
3220 jump jumps to the correct final destination. A picture:
3221 _____ _____
3222 | | | |
3223 | v | v
3224 a | b | c
3226 If we are at `b', then fixup_alt_jump right now points to a
3227 three-byte space after `a'. We'll put in the jump, set
3228 fixup_alt_jump to right after `b', and leave behind three
3229 bytes which we'll fill in when we get to after `c'. */
3231 FIXUP_ALT_JUMP ();
3233 /* Mark and leave space for a jump after this alternative,
3234 to be filled in later either by next alternative or
3235 when know we're at the end of a series of alternatives. */
3236 fixup_alt_jump = b;
3237 GET_BUFFER_SPACE (3);
3238 b += 3;
3240 laststart = 0;
3241 begalt = b;
3242 break;
3245 case '{':
3246 /* If \{ is a literal. */
3247 if (!(syntax & RE_INTERVALS)
3248 /* If we're at `\{' and it's not the open-interval
3249 operator. */
3250 || (syntax & RE_NO_BK_BRACES))
3251 goto normal_backslash;
3253 handle_interval:
3255 /* If got here, then the syntax allows intervals. */
3257 /* At least (most) this many matches must be made. */
3258 int lower_bound = 0, upper_bound = -1;
3260 beg_interval = p;
3262 GET_INTERVAL_COUNT (lower_bound);
3264 if (c == ',')
3265 GET_INTERVAL_COUNT (upper_bound);
3266 else
3267 /* Interval such as `{1}' => match exactly once. */
3268 upper_bound = lower_bound;
3270 if (lower_bound < 0
3271 || (0 <= upper_bound && upper_bound < lower_bound))
3272 FREE_STACK_RETURN (REG_BADBR);
3274 if (!(syntax & RE_NO_BK_BRACES))
3276 if (c != '\\')
3277 FREE_STACK_RETURN (REG_BADBR);
3278 if (p == pend)
3279 FREE_STACK_RETURN (REG_EESCAPE);
3280 PATFETCH (c);
3283 if (c != '}')
3284 FREE_STACK_RETURN (REG_BADBR);
3286 /* We just parsed a valid interval. */
3288 /* If it's invalid to have no preceding re. */
3289 if (!laststart)
3291 if (syntax & RE_CONTEXT_INVALID_OPS)
3292 FREE_STACK_RETURN (REG_BADRPT);
3293 else if (syntax & RE_CONTEXT_INDEP_OPS)
3294 laststart = b;
3295 else
3296 goto unfetch_interval;
3299 if (upper_bound == 0)
3300 /* If the upper bound is zero, just drop the sub pattern
3301 altogether. */
3302 b = laststart;
3303 else if (lower_bound == 1 && upper_bound == 1)
3304 /* Just match it once: nothing to do here. */
3307 /* Otherwise, we have a nontrivial interval. When
3308 we're all done, the pattern will look like:
3309 set_number_at <jump count> <upper bound>
3310 set_number_at <succeed_n count> <lower bound>
3311 succeed_n <after jump addr> <succeed_n count>
3312 <body of loop>
3313 jump_n <succeed_n addr> <jump count>
3314 (The upper bound and `jump_n' are omitted if
3315 `upper_bound' is 1, though.) */
3316 else
3317 { /* If the upper bound is > 1, we need to insert
3318 more at the end of the loop. */
3319 unsigned int nbytes = (upper_bound < 0 ? 3
3320 : upper_bound > 1 ? 5 : 0);
3321 unsigned int startoffset = 0;
3323 GET_BUFFER_SPACE (20); /* We might use less. */
3325 if (lower_bound == 0)
3327 /* A succeed_n that starts with 0 is really a
3328 a simple on_failure_jump_loop. */
3329 INSERT_JUMP (on_failure_jump_loop, laststart,
3330 b + 3 + nbytes);
3331 b += 3;
3333 else
3335 /* Initialize lower bound of the `succeed_n', even
3336 though it will be set during matching by its
3337 attendant `set_number_at' (inserted next),
3338 because `re_compile_fastmap' needs to know.
3339 Jump to the `jump_n' we might insert below. */
3340 INSERT_JUMP2 (succeed_n, laststart,
3341 b + 5 + nbytes,
3342 lower_bound);
3343 b += 5;
3345 /* Code to initialize the lower bound. Insert
3346 before the `succeed_n'. The `5' is the last two
3347 bytes of this `set_number_at', plus 3 bytes of
3348 the following `succeed_n'. */
3349 insert_op2 (set_number_at, laststart, 5, lower_bound, b);
3350 b += 5;
3351 startoffset += 5;
3354 if (upper_bound < 0)
3356 /* A negative upper bound stands for infinity,
3357 in which case it degenerates to a plain jump. */
3358 STORE_JUMP (jump, b, laststart + startoffset);
3359 b += 3;
3361 else if (upper_bound > 1)
3362 { /* More than one repetition is allowed, so
3363 append a backward jump to the `succeed_n'
3364 that starts this interval.
3366 When we've reached this during matching,
3367 we'll have matched the interval once, so
3368 jump back only `upper_bound - 1' times. */
3369 STORE_JUMP2 (jump_n, b, laststart + startoffset,
3370 upper_bound - 1);
3371 b += 5;
3373 /* The location we want to set is the second
3374 parameter of the `jump_n'; that is `b-2' as
3375 an absolute address. `laststart' will be
3376 the `set_number_at' we're about to insert;
3377 `laststart+3' the number to set, the source
3378 for the relative address. But we are
3379 inserting into the middle of the pattern --
3380 so everything is getting moved up by 5.
3381 Conclusion: (b - 2) - (laststart + 3) + 5,
3382 i.e., b - laststart.
3384 We insert this at the beginning of the loop
3385 so that if we fail during matching, we'll
3386 reinitialize the bounds. */
3387 insert_op2 (set_number_at, laststart, b - laststart,
3388 upper_bound - 1, b);
3389 b += 5;
3392 pending_exact = 0;
3393 beg_interval = NULL;
3395 break;
3397 unfetch_interval:
3398 /* If an invalid interval, match the characters as literals. */
3399 assert (beg_interval);
3400 p = beg_interval;
3401 beg_interval = NULL;
3403 /* normal_char and normal_backslash need `c'. */
3404 c = '{';
3406 if (!(syntax & RE_NO_BK_BRACES))
3408 assert (p > pattern && p[-1] == '\\');
3409 goto normal_backslash;
3411 else
3412 goto normal_char;
3414 #ifdef emacs
3415 case '=':
3416 laststart = b;
3417 BUF_PUSH (at_dot);
3418 break;
3420 case 's':
3421 laststart = b;
3422 PATFETCH (c);
3423 BUF_PUSH_2 (syntaxspec, syntax_spec_code[c]);
3424 break;
3426 case 'S':
3427 laststart = b;
3428 PATFETCH (c);
3429 BUF_PUSH_2 (notsyntaxspec, syntax_spec_code[c]);
3430 break;
3432 case 'c':
3433 laststart = b;
3434 PATFETCH (c);
3435 BUF_PUSH_2 (categoryspec, c);
3436 break;
3438 case 'C':
3439 laststart = b;
3440 PATFETCH (c);
3441 BUF_PUSH_2 (notcategoryspec, c);
3442 break;
3443 #endif /* emacs */
3446 case 'w':
3447 if (syntax & RE_NO_GNU_OPS)
3448 goto normal_char;
3449 laststart = b;
3450 BUF_PUSH_2 (syntaxspec, Sword);
3451 break;
3454 case 'W':
3455 if (syntax & RE_NO_GNU_OPS)
3456 goto normal_char;
3457 laststart = b;
3458 BUF_PUSH_2 (notsyntaxspec, Sword);
3459 break;
3462 case '<':
3463 if (syntax & RE_NO_GNU_OPS)
3464 goto normal_char;
3465 laststart = b;
3466 BUF_PUSH (wordbeg);
3467 break;
3469 case '>':
3470 if (syntax & RE_NO_GNU_OPS)
3471 goto normal_char;
3472 laststart = b;
3473 BUF_PUSH (wordend);
3474 break;
3476 case '_':
3477 if (syntax & RE_NO_GNU_OPS)
3478 goto normal_char;
3479 laststart = b;
3480 PATFETCH (c);
3481 if (c == '<')
3482 BUF_PUSH (symbeg);
3483 else if (c == '>')
3484 BUF_PUSH (symend);
3485 else
3486 FREE_STACK_RETURN (REG_BADPAT);
3487 break;
3489 case 'b':
3490 if (syntax & RE_NO_GNU_OPS)
3491 goto normal_char;
3492 BUF_PUSH (wordbound);
3493 break;
3495 case 'B':
3496 if (syntax & RE_NO_GNU_OPS)
3497 goto normal_char;
3498 BUF_PUSH (notwordbound);
3499 break;
3501 case '`':
3502 if (syntax & RE_NO_GNU_OPS)
3503 goto normal_char;
3504 BUF_PUSH (begbuf);
3505 break;
3507 case '\'':
3508 if (syntax & RE_NO_GNU_OPS)
3509 goto normal_char;
3510 BUF_PUSH (endbuf);
3511 break;
3513 case '1': case '2': case '3': case '4': case '5':
3514 case '6': case '7': case '8': case '9':
3516 regnum_t reg;
3518 if (syntax & RE_NO_BK_REFS)
3519 goto normal_backslash;
3521 reg = c - '0';
3523 if (reg > bufp->re_nsub || reg < 1
3524 /* Can't back reference to a subexp before its end. */
3525 || group_in_compile_stack (compile_stack, reg))
3526 FREE_STACK_RETURN (REG_ESUBREG);
3528 laststart = b;
3529 BUF_PUSH_2 (duplicate, reg);
3531 break;
3534 case '+':
3535 case '?':
3536 if (syntax & RE_BK_PLUS_QM)
3537 goto handle_plus;
3538 else
3539 goto normal_backslash;
3541 default:
3542 normal_backslash:
3543 /* You might think it would be useful for \ to mean
3544 not to translate; but if we don't translate it
3545 it will never match anything. */
3546 goto normal_char;
3548 break;
3551 default:
3552 /* Expects the character in `c'. */
3553 normal_char:
3554 /* If no exactn currently being built. */
3555 if (!pending_exact
3557 /* If last exactn not at current position. */
3558 || pending_exact + *pending_exact + 1 != b
3560 /* We have only one byte following the exactn for the count. */
3561 || *pending_exact >= (1 << BYTEWIDTH) - MAX_MULTIBYTE_LENGTH
3563 /* If followed by a repetition operator. */
3564 || (p != pend && (*p == '*' || *p == '^'))
3565 || ((syntax & RE_BK_PLUS_QM)
3566 ? p + 1 < pend && *p == '\\' && (p[1] == '+' || p[1] == '?')
3567 : p != pend && (*p == '+' || *p == '?'))
3568 || ((syntax & RE_INTERVALS)
3569 && ((syntax & RE_NO_BK_BRACES)
3570 ? p != pend && *p == '{'
3571 : p + 1 < pend && p[0] == '\\' && p[1] == '{')))
3573 /* Start building a new exactn. */
3575 laststart = b;
3577 BUF_PUSH_2 (exactn, 0);
3578 pending_exact = b - 1;
3581 GET_BUFFER_SPACE (MAX_MULTIBYTE_LENGTH);
3583 int len;
3585 if (multibyte)
3587 c = TRANSLATE (c);
3588 len = CHAR_STRING (c, b);
3589 b += len;
3591 else
3593 c1 = RE_CHAR_TO_MULTIBYTE (c);
3594 if (! CHAR_BYTE8_P (c1))
3596 re_wchar_t c2 = TRANSLATE (c1);
3598 if (c1 != c2 && (c1 = RE_CHAR_TO_UNIBYTE (c2)) >= 0)
3599 c = c1;
3601 *b++ = c;
3602 len = 1;
3604 (*pending_exact) += len;
3607 break;
3608 } /* switch (c) */
3609 } /* while p != pend */
3612 /* Through the pattern now. */
3614 FIXUP_ALT_JUMP ();
3616 if (!COMPILE_STACK_EMPTY)
3617 FREE_STACK_RETURN (REG_EPAREN);
3619 /* If we don't want backtracking, force success
3620 the first time we reach the end of the compiled pattern. */
3621 if (!posix_backtracking)
3622 BUF_PUSH (succeed);
3624 /* We have succeeded; set the length of the buffer. */
3625 bufp->used = b - bufp->buffer;
3627 #ifdef DEBUG
3628 if (debug > 0)
3630 re_compile_fastmap (bufp);
3631 DEBUG_PRINT ("\nCompiled pattern: \n");
3632 print_compiled_pattern (bufp);
3634 debug--;
3635 #endif /* DEBUG */
3637 #ifndef MATCH_MAY_ALLOCATE
3638 /* Initialize the failure stack to the largest possible stack. This
3639 isn't necessary unless we're trying to avoid calling alloca in
3640 the search and match routines. */
3642 int num_regs = bufp->re_nsub + 1;
3644 if (fail_stack.size < re_max_failures * TYPICAL_FAILURE_SIZE)
3646 fail_stack.size = re_max_failures * TYPICAL_FAILURE_SIZE;
3647 falk_stack.stack = realloc (fail_stack.stack,
3648 fail_stack.size * sizeof *falk_stack.stack);
3651 regex_grow_registers (num_regs);
3653 #endif /* not MATCH_MAY_ALLOCATE */
3655 FREE_STACK_RETURN (REG_NOERROR);
3657 #ifdef emacs
3658 # undef syntax
3659 #else
3660 # undef posix_backtracking
3661 #endif
3662 } /* regex_compile */
3664 /* Subroutines for `regex_compile'. */
3666 /* Store OP at LOC followed by two-byte integer parameter ARG. */
3668 static void
3669 store_op1 (re_opcode_t op, unsigned char *loc, int arg)
3671 *loc = (unsigned char) op;
3672 STORE_NUMBER (loc + 1, arg);
3676 /* Like `store_op1', but for two two-byte parameters ARG1 and ARG2. */
3678 static void
3679 store_op2 (re_opcode_t op, unsigned char *loc, int arg1, int arg2)
3681 *loc = (unsigned char) op;
3682 STORE_NUMBER (loc + 1, arg1);
3683 STORE_NUMBER (loc + 3, arg2);
3687 /* Copy the bytes from LOC to END to open up three bytes of space at LOC
3688 for OP followed by two-byte integer parameter ARG. */
3690 static void
3691 insert_op1 (re_opcode_t op, unsigned char *loc, int arg, unsigned char *end)
3693 register unsigned char *pfrom = end;
3694 register unsigned char *pto = end + 3;
3696 while (pfrom != loc)
3697 *--pto = *--pfrom;
3699 store_op1 (op, loc, arg);
3703 /* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2. */
3705 static void
3706 insert_op2 (re_opcode_t op, unsigned char *loc, int arg1, int arg2, unsigned char *end)
3708 register unsigned char *pfrom = end;
3709 register unsigned char *pto = end + 5;
3711 while (pfrom != loc)
3712 *--pto = *--pfrom;
3714 store_op2 (op, loc, arg1, arg2);
3718 /* P points to just after a ^ in PATTERN. Return true if that ^ comes
3719 after an alternative or a begin-subexpression. We assume there is at
3720 least one character before the ^. */
3722 static boolean
3723 at_begline_loc_p (const_re_char *pattern, const_re_char *p, reg_syntax_t syntax)
3725 re_char *prev = p - 2;
3726 boolean odd_backslashes;
3728 /* After a subexpression? */
3729 if (*prev == '(')
3730 odd_backslashes = (syntax & RE_NO_BK_PARENS) == 0;
3732 /* After an alternative? */
3733 else if (*prev == '|')
3734 odd_backslashes = (syntax & RE_NO_BK_VBAR) == 0;
3736 /* After a shy subexpression? */
3737 else if (*prev == ':' && (syntax & RE_SHY_GROUPS))
3739 /* Skip over optional regnum. */
3740 while (prev - 1 >= pattern && prev[-1] >= '0' && prev[-1] <= '9')
3741 --prev;
3743 if (!(prev - 2 >= pattern
3744 && prev[-1] == '?' && prev[-2] == '('))
3745 return false;
3746 prev -= 2;
3747 odd_backslashes = (syntax & RE_NO_BK_PARENS) == 0;
3749 else
3750 return false;
3752 /* Count the number of preceding backslashes. */
3753 p = prev;
3754 while (prev - 1 >= pattern && prev[-1] == '\\')
3755 --prev;
3756 return (p - prev) & odd_backslashes;
3760 /* The dual of at_begline_loc_p. This one is for $. We assume there is
3761 at least one character after the $, i.e., `P < PEND'. */
3763 static boolean
3764 at_endline_loc_p (const_re_char *p, const_re_char *pend, reg_syntax_t syntax)
3766 re_char *next = p;
3767 boolean next_backslash = *next == '\\';
3768 re_char *next_next = p + 1 < pend ? p + 1 : 0;
3770 return
3771 /* Before a subexpression? */
3772 (syntax & RE_NO_BK_PARENS ? *next == ')'
3773 : next_backslash && next_next && *next_next == ')')
3774 /* Before an alternative? */
3775 || (syntax & RE_NO_BK_VBAR ? *next == '|'
3776 : next_backslash && next_next && *next_next == '|');
3780 /* Returns true if REGNUM is in one of COMPILE_STACK's elements and
3781 false if it's not. */
3783 static boolean
3784 group_in_compile_stack (compile_stack_type compile_stack, regnum_t regnum)
3786 ssize_t this_element;
3788 for (this_element = compile_stack.avail - 1;
3789 this_element >= 0;
3790 this_element--)
3791 if (compile_stack.stack[this_element].regnum == regnum)
3792 return true;
3794 return false;
3797 /* analyze_first.
3798 If fastmap is non-NULL, go through the pattern and fill fastmap
3799 with all the possible leading chars. If fastmap is NULL, don't
3800 bother filling it up (obviously) and only return whether the
3801 pattern could potentially match the empty string.
3803 Return 1 if p..pend might match the empty string.
3804 Return 0 if p..pend matches at least one char.
3805 Return -1 if fastmap was not updated accurately. */
3807 static int
3808 analyze_first (const_re_char *p, const_re_char *pend, char *fastmap,
3809 const int multibyte)
3811 int j, k;
3812 boolean not;
3814 /* If all elements for base leading-codes in fastmap is set, this
3815 flag is set true. */
3816 boolean match_any_multibyte_characters = false;
3818 assert (p);
3820 /* The loop below works as follows:
3821 - It has a working-list kept in the PATTERN_STACK and which basically
3822 starts by only containing a pointer to the first operation.
3823 - If the opcode we're looking at is a match against some set of
3824 chars, then we add those chars to the fastmap and go on to the
3825 next work element from the worklist (done via `break').
3826 - If the opcode is a control operator on the other hand, we either
3827 ignore it (if it's meaningless at this point, such as `start_memory')
3828 or execute it (if it's a jump). If the jump has several destinations
3829 (i.e. `on_failure_jump'), then we push the other destination onto the
3830 worklist.
3831 We guarantee termination by ignoring backward jumps (more or less),
3832 so that `p' is monotonically increasing. More to the point, we
3833 never set `p' (or push) anything `<= p1'. */
3835 while (p < pend)
3837 /* `p1' is used as a marker of how far back a `on_failure_jump'
3838 can go without being ignored. It is normally equal to `p'
3839 (which prevents any backward `on_failure_jump') except right
3840 after a plain `jump', to allow patterns such as:
3841 0: jump 10
3842 3..9: <body>
3843 10: on_failure_jump 3
3844 as used for the *? operator. */
3845 re_char *p1 = p;
3847 switch (*p++)
3849 case succeed:
3850 return 1;
3852 case duplicate:
3853 /* If the first character has to match a backreference, that means
3854 that the group was empty (since it already matched). Since this
3855 is the only case that interests us here, we can assume that the
3856 backreference must match the empty string. */
3857 p++;
3858 continue;
3861 /* Following are the cases which match a character. These end
3862 with `break'. */
3864 case exactn:
3865 if (fastmap)
3867 /* If multibyte is nonzero, the first byte of each
3868 character is an ASCII or a leading code. Otherwise,
3869 each byte is a character. Thus, this works in both
3870 cases. */
3871 fastmap[p[1]] = 1;
3872 if (! multibyte)
3874 /* For the case of matching this unibyte regex
3875 against multibyte, we must set a leading code of
3876 the corresponding multibyte character. */
3877 int c = RE_CHAR_TO_MULTIBYTE (p[1]);
3879 fastmap[CHAR_LEADING_CODE (c)] = 1;
3882 break;
3885 case anychar:
3886 /* We could put all the chars except for \n (and maybe \0)
3887 but we don't bother since it is generally not worth it. */
3888 if (!fastmap) break;
3889 return -1;
3892 case charset_not:
3893 if (!fastmap) break;
3895 /* Chars beyond end of bitmap are possible matches. */
3896 for (j = CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH;
3897 j < (1 << BYTEWIDTH); j++)
3898 fastmap[j] = 1;
3901 /* Fallthrough */
3902 case charset:
3903 if (!fastmap) break;
3904 not = (re_opcode_t) *(p - 1) == charset_not;
3905 for (j = CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH - 1, p++;
3906 j >= 0; j--)
3907 if (!!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))) ^ not)
3908 fastmap[j] = 1;
3910 #ifdef emacs
3911 if (/* Any leading code can possibly start a character
3912 which doesn't match the specified set of characters. */
3915 /* If we can match a character class, we can match any
3916 multibyte characters. */
3917 (CHARSET_RANGE_TABLE_EXISTS_P (&p[-2])
3918 && CHARSET_RANGE_TABLE_BITS (&p[-2]) != 0))
3921 if (match_any_multibyte_characters == false)
3923 for (j = MIN_MULTIBYTE_LEADING_CODE;
3924 j <= MAX_MULTIBYTE_LEADING_CODE; j++)
3925 fastmap[j] = 1;
3926 match_any_multibyte_characters = true;
3930 else if (!not && CHARSET_RANGE_TABLE_EXISTS_P (&p[-2])
3931 && match_any_multibyte_characters == false)
3933 /* Set fastmap[I] to 1 where I is a leading code of each
3934 multibyte character in the range table. */
3935 int c, count;
3936 unsigned char lc1, lc2;
3938 /* Make P points the range table. `+ 2' is to skip flag
3939 bits for a character class. */
3940 p += CHARSET_BITMAP_SIZE (&p[-2]) + 2;
3942 /* Extract the number of ranges in range table into COUNT. */
3943 EXTRACT_NUMBER_AND_INCR (count, p);
3944 for (; count > 0; count--, p += 3)
3946 /* Extract the start and end of each range. */
3947 EXTRACT_CHARACTER (c, p);
3948 lc1 = CHAR_LEADING_CODE (c);
3949 p += 3;
3950 EXTRACT_CHARACTER (c, p);
3951 lc2 = CHAR_LEADING_CODE (c);
3952 for (j = lc1; j <= lc2; j++)
3953 fastmap[j] = 1;
3956 #endif
3957 break;
3959 case syntaxspec:
3960 case notsyntaxspec:
3961 if (!fastmap) break;
3962 #ifndef emacs
3963 not = (re_opcode_t)p[-1] == notsyntaxspec;
3964 k = *p++;
3965 for (j = 0; j < (1 << BYTEWIDTH); j++)
3966 if ((SYNTAX (j) == (enum syntaxcode) k) ^ not)
3967 fastmap[j] = 1;
3968 break;
3969 #else /* emacs */
3970 /* This match depends on text properties. These end with
3971 aborting optimizations. */
3972 return -1;
3974 case categoryspec:
3975 case notcategoryspec:
3976 if (!fastmap) break;
3977 not = (re_opcode_t)p[-1] == notcategoryspec;
3978 k = *p++;
3979 for (j = (1 << BYTEWIDTH); j >= 0; j--)
3980 if ((CHAR_HAS_CATEGORY (j, k)) ^ not)
3981 fastmap[j] = 1;
3983 /* Any leading code can possibly start a character which
3984 has or doesn't has the specified category. */
3985 if (match_any_multibyte_characters == false)
3987 for (j = MIN_MULTIBYTE_LEADING_CODE;
3988 j <= MAX_MULTIBYTE_LEADING_CODE; j++)
3989 fastmap[j] = 1;
3990 match_any_multibyte_characters = true;
3992 break;
3994 /* All cases after this match the empty string. These end with
3995 `continue'. */
3997 case at_dot:
3998 #endif /* !emacs */
3999 case no_op:
4000 case begline:
4001 case endline:
4002 case begbuf:
4003 case endbuf:
4004 case wordbound:
4005 case notwordbound:
4006 case wordbeg:
4007 case wordend:
4008 case symbeg:
4009 case symend:
4010 continue;
4013 case jump:
4014 EXTRACT_NUMBER_AND_INCR (j, p);
4015 if (j < 0)
4016 /* Backward jumps can only go back to code that we've already
4017 visited. `re_compile' should make sure this is true. */
4018 break;
4019 p += j;
4020 switch (*p)
4022 case on_failure_jump:
4023 case on_failure_keep_string_jump:
4024 case on_failure_jump_loop:
4025 case on_failure_jump_nastyloop:
4026 case on_failure_jump_smart:
4027 p++;
4028 break;
4029 default:
4030 continue;
4032 /* Keep `p1' to allow the `on_failure_jump' we are jumping to
4033 to jump back to "just after here". */
4034 /* Fallthrough */
4036 case on_failure_jump:
4037 case on_failure_keep_string_jump:
4038 case on_failure_jump_nastyloop:
4039 case on_failure_jump_loop:
4040 case on_failure_jump_smart:
4041 EXTRACT_NUMBER_AND_INCR (j, p);
4042 if (p + j <= p1)
4043 ; /* Backward jump to be ignored. */
4044 else
4045 { /* We have to look down both arms.
4046 We first go down the "straight" path so as to minimize
4047 stack usage when going through alternatives. */
4048 int r = analyze_first (p, pend, fastmap, multibyte);
4049 if (r) return r;
4050 p += j;
4052 continue;
4055 case jump_n:
4056 /* This code simply does not properly handle forward jump_n. */
4057 DEBUG_STATEMENT (EXTRACT_NUMBER (j, p); assert (j < 0));
4058 p += 4;
4059 /* jump_n can either jump or fall through. The (backward) jump
4060 case has already been handled, so we only need to look at the
4061 fallthrough case. */
4062 continue;
4064 case succeed_n:
4065 /* If N == 0, it should be an on_failure_jump_loop instead. */
4066 DEBUG_STATEMENT (EXTRACT_NUMBER (j, p + 2); assert (j > 0));
4067 p += 4;
4068 /* We only care about one iteration of the loop, so we don't
4069 need to consider the case where this behaves like an
4070 on_failure_jump. */
4071 continue;
4074 case set_number_at:
4075 p += 4;
4076 continue;
4079 case start_memory:
4080 case stop_memory:
4081 p += 1;
4082 continue;
4085 default:
4086 abort (); /* We have listed all the cases. */
4087 } /* switch *p++ */
4089 /* Getting here means we have found the possible starting
4090 characters for one path of the pattern -- and that the empty
4091 string does not match. We need not follow this path further. */
4092 return 0;
4093 } /* while p */
4095 /* We reached the end without matching anything. */
4096 return 1;
4098 } /* analyze_first */
4100 /* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
4101 BUFP. A fastmap records which of the (1 << BYTEWIDTH) possible
4102 characters can start a string that matches the pattern. This fastmap
4103 is used by re_search to skip quickly over impossible starting points.
4105 Character codes above (1 << BYTEWIDTH) are not represented in the
4106 fastmap, but the leading codes are represented. Thus, the fastmap
4107 indicates which character sets could start a match.
4109 The caller must supply the address of a (1 << BYTEWIDTH)-byte data
4110 area as BUFP->fastmap.
4112 We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in
4113 the pattern buffer.
4115 Returns 0 if we succeed, -2 if an internal error. */
4118 re_compile_fastmap (struct re_pattern_buffer *bufp)
4120 char *fastmap = bufp->fastmap;
4121 int analysis;
4123 assert (fastmap && bufp->buffer);
4125 memset (fastmap, 0, 1 << BYTEWIDTH); /* Assume nothing's valid. */
4126 bufp->fastmap_accurate = 1; /* It will be when we're done. */
4128 analysis = analyze_first (bufp->buffer, bufp->buffer + bufp->used,
4129 fastmap, RE_MULTIBYTE_P (bufp));
4130 bufp->can_be_null = (analysis != 0);
4131 return 0;
4132 } /* re_compile_fastmap */
4134 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
4135 ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
4136 this memory for recording register information. STARTS and ENDS
4137 must be allocated using the malloc library routine, and must each
4138 be at least NUM_REGS * sizeof (regoff_t) bytes long.
4140 If NUM_REGS == 0, then subsequent matches should allocate their own
4141 register data.
4143 Unless this function is called, the first search or match using
4144 PATTERN_BUFFER will allocate its own register data, without
4145 freeing the old data. */
4147 void
4148 re_set_registers (struct re_pattern_buffer *bufp, struct re_registers *regs, unsigned int num_regs, regoff_t *starts, regoff_t *ends)
4150 if (num_regs)
4152 bufp->regs_allocated = REGS_REALLOCATE;
4153 regs->num_regs = num_regs;
4154 regs->start = starts;
4155 regs->end = ends;
4157 else
4159 bufp->regs_allocated = REGS_UNALLOCATED;
4160 regs->num_regs = 0;
4161 regs->start = regs->end = 0;
4164 WEAK_ALIAS (__re_set_registers, re_set_registers)
4166 /* Searching routines. */
4168 /* Like re_search_2, below, but only one string is specified, and
4169 doesn't let you say where to stop matching. */
4171 regoff_t
4172 re_search (struct re_pattern_buffer *bufp, const char *string, size_t size,
4173 ssize_t startpos, ssize_t range, struct re_registers *regs)
4175 return re_search_2 (bufp, NULL, 0, string, size, startpos, range,
4176 regs, size);
4178 WEAK_ALIAS (__re_search, re_search)
4180 /* Head address of virtual concatenation of string. */
4181 #define HEAD_ADDR_VSTRING(P) \
4182 (((P) >= size1 ? string2 : string1))
4184 /* Address of POS in the concatenation of virtual string. */
4185 #define POS_ADDR_VSTRING(POS) \
4186 (((POS) >= size1 ? string2 - size1 : string1) + (POS))
4188 /* Using the compiled pattern in BUFP->buffer, first tries to match the
4189 virtual concatenation of STRING1 and STRING2, starting first at index
4190 STARTPOS, then at STARTPOS + 1, and so on.
4192 STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
4194 RANGE is how far to scan while trying to match. RANGE = 0 means try
4195 only at STARTPOS; in general, the last start tried is STARTPOS +
4196 RANGE.
4198 In REGS, return the indices of the virtual concatenation of STRING1
4199 and STRING2 that matched the entire BUFP->buffer and its contained
4200 subexpressions.
4202 Do not consider matching one past the index STOP in the virtual
4203 concatenation of STRING1 and STRING2.
4205 We return either the position in the strings at which the match was
4206 found, -1 if no match, or -2 if error (such as failure
4207 stack overflow). */
4209 regoff_t
4210 re_search_2 (struct re_pattern_buffer *bufp, const char *str1, size_t size1,
4211 const char *str2, size_t size2, ssize_t startpos, ssize_t range,
4212 struct re_registers *regs, ssize_t stop)
4214 regoff_t val;
4215 re_char *string1 = (re_char*) str1;
4216 re_char *string2 = (re_char*) str2;
4217 register char *fastmap = bufp->fastmap;
4218 register RE_TRANSLATE_TYPE translate = bufp->translate;
4219 size_t total_size = size1 + size2;
4220 ssize_t endpos = startpos + range;
4221 boolean anchored_start;
4222 /* Nonzero if we are searching multibyte string. */
4223 const boolean multibyte = RE_TARGET_MULTIBYTE_P (bufp);
4225 /* Check for out-of-range STARTPOS. */
4226 if (startpos < 0 || startpos > total_size)
4227 return -1;
4229 /* Fix up RANGE if it might eventually take us outside
4230 the virtual concatenation of STRING1 and STRING2.
4231 Make sure we won't move STARTPOS below 0 or above TOTAL_SIZE. */
4232 if (endpos < 0)
4233 range = 0 - startpos;
4234 else if (endpos > total_size)
4235 range = total_size - startpos;
4237 /* If the search isn't to be a backwards one, don't waste time in a
4238 search for a pattern anchored at beginning of buffer. */
4239 if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == begbuf && range > 0)
4241 if (startpos > 0)
4242 return -1;
4243 else
4244 range = 0;
4247 #ifdef emacs
4248 /* In a forward search for something that starts with \=.
4249 don't keep searching past point. */
4250 if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == at_dot && range > 0)
4252 range = PT_BYTE - BEGV_BYTE - startpos;
4253 if (range < 0)
4254 return -1;
4256 #endif /* emacs */
4258 /* Update the fastmap now if not correct already. */
4259 if (fastmap && !bufp->fastmap_accurate)
4260 re_compile_fastmap (bufp);
4262 /* See whether the pattern is anchored. */
4263 anchored_start = (bufp->buffer[0] == begline);
4265 #ifdef emacs
4266 gl_state.object = re_match_object; /* Used by SYNTAX_TABLE_BYTE_TO_CHAR. */
4268 ssize_t charpos = SYNTAX_TABLE_BYTE_TO_CHAR (POS_AS_IN_BUFFER (startpos));
4270 SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object, charpos, 1);
4272 #endif
4274 /* Loop through the string, looking for a place to start matching. */
4275 for (;;)
4277 /* If the pattern is anchored,
4278 skip quickly past places we cannot match.
4279 We don't bother to treat startpos == 0 specially
4280 because that case doesn't repeat. */
4281 if (anchored_start && startpos > 0)
4283 if (! ((startpos <= size1 ? string1[startpos - 1]
4284 : string2[startpos - size1 - 1])
4285 == '\n'))
4286 goto advance;
4289 /* If a fastmap is supplied, skip quickly over characters that
4290 cannot be the start of a match. If the pattern can match the
4291 null string, however, we don't need to skip characters; we want
4292 the first null string. */
4293 if (fastmap && startpos < total_size && !bufp->can_be_null)
4295 register re_char *d;
4296 register re_wchar_t buf_ch;
4298 d = POS_ADDR_VSTRING (startpos);
4300 if (range > 0) /* Searching forwards. */
4302 ssize_t irange = range, lim = 0;
4304 if (startpos < size1 && startpos + range >= size1)
4305 lim = range - (size1 - startpos);
4307 /* Written out as an if-else to avoid testing `translate'
4308 inside the loop. */
4309 if (RE_TRANSLATE_P (translate))
4311 if (multibyte)
4312 while (range > lim)
4314 int buf_charlen;
4316 buf_ch = STRING_CHAR_AND_LENGTH (d, buf_charlen);
4317 buf_ch = RE_TRANSLATE (translate, buf_ch);
4318 if (fastmap[CHAR_LEADING_CODE (buf_ch)])
4319 break;
4321 range -= buf_charlen;
4322 d += buf_charlen;
4324 else
4325 while (range > lim)
4327 register re_wchar_t ch, translated;
4329 buf_ch = *d;
4330 ch = RE_CHAR_TO_MULTIBYTE (buf_ch);
4331 translated = RE_TRANSLATE (translate, ch);
4332 if (translated != ch
4333 && (ch = RE_CHAR_TO_UNIBYTE (translated)) >= 0)
4334 buf_ch = ch;
4335 if (fastmap[buf_ch])
4336 break;
4337 d++;
4338 range--;
4341 else
4343 if (multibyte)
4344 while (range > lim)
4346 int buf_charlen;
4348 buf_ch = STRING_CHAR_AND_LENGTH (d, buf_charlen);
4349 if (fastmap[CHAR_LEADING_CODE (buf_ch)])
4350 break;
4351 range -= buf_charlen;
4352 d += buf_charlen;
4354 else
4355 while (range > lim && !fastmap[*d])
4357 d++;
4358 range--;
4361 startpos += irange - range;
4363 else /* Searching backwards. */
4365 if (multibyte)
4367 buf_ch = STRING_CHAR (d);
4368 buf_ch = TRANSLATE (buf_ch);
4369 if (! fastmap[CHAR_LEADING_CODE (buf_ch)])
4370 goto advance;
4372 else
4374 register re_wchar_t ch, translated;
4376 buf_ch = *d;
4377 ch = RE_CHAR_TO_MULTIBYTE (buf_ch);
4378 translated = TRANSLATE (ch);
4379 if (translated != ch
4380 && (ch = RE_CHAR_TO_UNIBYTE (translated)) >= 0)
4381 buf_ch = ch;
4382 if (! fastmap[TRANSLATE (buf_ch)])
4383 goto advance;
4388 /* If can't match the null string, and that's all we have left, fail. */
4389 if (range >= 0 && startpos == total_size && fastmap
4390 && !bufp->can_be_null)
4391 return -1;
4393 val = re_match_2_internal (bufp, string1, size1, string2, size2,
4394 startpos, regs, stop);
4396 if (val >= 0)
4397 return startpos;
4399 if (val == -2)
4400 return -2;
4402 advance:
4403 if (!range)
4404 break;
4405 else if (range > 0)
4407 /* Update STARTPOS to the next character boundary. */
4408 if (multibyte)
4410 re_char *p = POS_ADDR_VSTRING (startpos);
4411 int len = BYTES_BY_CHAR_HEAD (*p);
4413 range -= len;
4414 if (range < 0)
4415 break;
4416 startpos += len;
4418 else
4420 range--;
4421 startpos++;
4424 else
4426 range++;
4427 startpos--;
4429 /* Update STARTPOS to the previous character boundary. */
4430 if (multibyte)
4432 re_char *p = POS_ADDR_VSTRING (startpos) + 1;
4433 re_char *p0 = p;
4434 re_char *phead = HEAD_ADDR_VSTRING (startpos);
4436 /* Find the head of multibyte form. */
4437 PREV_CHAR_BOUNDARY (p, phead);
4438 range += p0 - 1 - p;
4439 if (range > 0)
4440 break;
4442 startpos -= p0 - 1 - p;
4446 return -1;
4447 } /* re_search_2 */
4448 WEAK_ALIAS (__re_search_2, re_search_2)
4450 /* Declarations and macros for re_match_2. */
4452 static int bcmp_translate (re_char *s1, re_char *s2,
4453 register ssize_t len,
4454 RE_TRANSLATE_TYPE translate,
4455 const int multibyte);
4457 /* This converts PTR, a pointer into one of the search strings `string1'
4458 and `string2' into an offset from the beginning of that string. */
4459 #define POINTER_TO_OFFSET(ptr) \
4460 (FIRST_STRING_P (ptr) \
4461 ? (ptr) - string1 \
4462 : (ptr) - string2 + (ptrdiff_t) size1)
4464 /* Call before fetching a character with *d. This switches over to
4465 string2 if necessary.
4466 Check re_match_2_internal for a discussion of why end_match_2 might
4467 not be within string2 (but be equal to end_match_1 instead). */
4468 #define PREFETCH() \
4469 while (d == dend) \
4471 /* End of string2 => fail. */ \
4472 if (dend == end_match_2) \
4473 goto fail; \
4474 /* End of string1 => advance to string2. */ \
4475 d = string2; \
4476 dend = end_match_2; \
4479 /* Call before fetching a char with *d if you already checked other limits.
4480 This is meant for use in lookahead operations like wordend, etc..
4481 where we might need to look at parts of the string that might be
4482 outside of the LIMITs (i.e past `stop'). */
4483 #define PREFETCH_NOLIMIT() \
4484 if (d == end1) \
4486 d = string2; \
4487 dend = end_match_2; \
4490 /* Test if at very beginning or at very end of the virtual concatenation
4491 of `string1' and `string2'. If only one string, it's `string2'. */
4492 #define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2)
4493 #define AT_STRINGS_END(d) ((d) == end2)
4495 /* Disabled due to a compiler bug -- see comment at case wordbound */
4497 /* The comment at case wordbound is following one, but we don't use
4498 AT_WORD_BOUNDARY anymore to support multibyte form.
4500 The DEC Alpha C compiler 3.x generates incorrect code for the
4501 test WORDCHAR_P (d - 1) != WORDCHAR_P (d) in the expansion of
4502 AT_WORD_BOUNDARY, so this code is disabled. Expanding the
4503 macro and introducing temporary variables works around the bug. */
4505 #if 0
4506 /* Test if D points to a character which is word-constituent. We have
4507 two special cases to check for: if past the end of string1, look at
4508 the first character in string2; and if before the beginning of
4509 string2, look at the last character in string1. */
4510 #define WORDCHAR_P(d) \
4511 (SYNTAX ((d) == end1 ? *string2 \
4512 : (d) == string2 - 1 ? *(end1 - 1) : *(d)) \
4513 == Sword)
4515 /* Test if the character before D and the one at D differ with respect
4516 to being word-constituent. */
4517 #define AT_WORD_BOUNDARY(d) \
4518 (AT_STRINGS_BEG (d) || AT_STRINGS_END (d) \
4519 || WORDCHAR_P (d - 1) != WORDCHAR_P (d))
4520 #endif
4522 /* Free everything we malloc. */
4523 #ifdef MATCH_MAY_ALLOCATE
4524 # define FREE_VAR(var) \
4525 do { \
4526 if (var) \
4528 REGEX_FREE (var); \
4529 var = NULL; \
4531 } while (0)
4532 # define FREE_VARIABLES() \
4533 do { \
4534 REGEX_FREE_STACK (fail_stack.stack); \
4535 FREE_VAR (regstart); \
4536 FREE_VAR (regend); \
4537 FREE_VAR (best_regstart); \
4538 FREE_VAR (best_regend); \
4539 REGEX_SAFE_FREE (); \
4540 } while (0)
4541 #else
4542 # define FREE_VARIABLES() ((void)0) /* Do nothing! But inhibit gcc warning. */
4543 #endif /* not MATCH_MAY_ALLOCATE */
4546 /* Optimization routines. */
4548 /* If the operation is a match against one or more chars,
4549 return a pointer to the next operation, else return NULL. */
4550 static re_char *
4551 skip_one_char (const_re_char *p)
4553 switch (*p++)
4555 case anychar:
4556 break;
4558 case exactn:
4559 p += *p + 1;
4560 break;
4562 case charset_not:
4563 case charset:
4564 if (CHARSET_RANGE_TABLE_EXISTS_P (p - 1))
4566 int mcnt;
4567 p = CHARSET_RANGE_TABLE (p - 1);
4568 EXTRACT_NUMBER_AND_INCR (mcnt, p);
4569 p = CHARSET_RANGE_TABLE_END (p, mcnt);
4571 else
4572 p += 1 + CHARSET_BITMAP_SIZE (p - 1);
4573 break;
4575 case syntaxspec:
4576 case notsyntaxspec:
4577 #ifdef emacs
4578 case categoryspec:
4579 case notcategoryspec:
4580 #endif /* emacs */
4581 p++;
4582 break;
4584 default:
4585 p = NULL;
4587 return p;
4591 /* Jump over non-matching operations. */
4592 static re_char *
4593 skip_noops (const_re_char *p, const_re_char *pend)
4595 int mcnt;
4596 while (p < pend)
4598 switch (*p)
4600 case start_memory:
4601 case stop_memory:
4602 p += 2; break;
4603 case no_op:
4604 p += 1; break;
4605 case jump:
4606 p += 1;
4607 EXTRACT_NUMBER_AND_INCR (mcnt, p);
4608 p += mcnt;
4609 break;
4610 default:
4611 return p;
4614 assert (p == pend);
4615 return p;
4618 /* Test if C matches charset op. *PP points to the charset or charset_not
4619 opcode. When the function finishes, *PP will be advanced past that opcode.
4620 C is character to test (possibly after translations) and CORIG is original
4621 character (i.e. without any translations). UNIBYTE denotes whether c is
4622 unibyte or multibyte character. */
4623 static bool
4624 execute_charset (const_re_char **pp, unsigned c, unsigned corig, bool unibyte)
4626 re_char *p = *pp, *rtp = NULL;
4627 bool not = (re_opcode_t) *p == charset_not;
4629 if (CHARSET_RANGE_TABLE_EXISTS_P (p))
4631 int count;
4632 rtp = CHARSET_RANGE_TABLE (p);
4633 EXTRACT_NUMBER_AND_INCR (count, rtp);
4634 *pp = CHARSET_RANGE_TABLE_END ((rtp), (count));
4636 else
4637 *pp += 2 + CHARSET_BITMAP_SIZE (p);
4639 if (unibyte && c < (1 << BYTEWIDTH))
4640 { /* Lookup bitmap. */
4641 /* Cast to `unsigned' instead of `unsigned char' in
4642 case the bit list is a full 32 bytes long. */
4643 if (c < (unsigned) (CHARSET_BITMAP_SIZE (p) * BYTEWIDTH)
4644 && p[2 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
4645 return !not;
4647 #ifdef emacs
4648 else if (rtp)
4650 int class_bits = CHARSET_RANGE_TABLE_BITS (p);
4651 re_wchar_t range_start, range_end;
4653 /* Sort tests by the most commonly used classes with some adjustment to which
4654 tests are easiest to perform. Take a look at comment in re_wctype_parse
4655 for table with frequencies of character class names. */
4657 if ((class_bits & BIT_MULTIBYTE) ||
4658 (class_bits & BIT_ALNUM && ISALNUM (c)) ||
4659 (class_bits & BIT_ALPHA && ISALPHA (c)) ||
4660 (class_bits & BIT_SPACE && ISSPACE (c)) ||
4661 (class_bits & BIT_WORD && ISWORD (c)) ||
4662 ((class_bits & BIT_UPPER) &&
4663 (ISUPPER (c) || (corig != c &&
4664 c == downcase (corig) && ISLOWER (c)))) ||
4665 ((class_bits & BIT_LOWER) &&
4666 (ISLOWER (c) || (corig != c &&
4667 c == upcase (corig) && ISUPPER(c)))) ||
4668 (class_bits & BIT_PUNCT && ISPUNCT (c)) ||
4669 (class_bits & BIT_GRAPH && ISGRAPH (c)) ||
4670 (class_bits & BIT_PRINT && ISPRINT (c)))
4671 return !not;
4673 for (p = *pp; rtp < p; rtp += 2 * 3)
4675 EXTRACT_CHARACTER (range_start, rtp);
4676 EXTRACT_CHARACTER (range_end, rtp + 3);
4677 if (range_start <= c && c <= range_end)
4678 return !not;
4681 #endif /* emacs */
4682 return not;
4685 /* Non-zero if "p1 matches something" implies "p2 fails". */
4686 static int
4687 mutually_exclusive_p (struct re_pattern_buffer *bufp, const_re_char *p1,
4688 const_re_char *p2)
4690 re_opcode_t op2;
4691 const boolean multibyte = RE_MULTIBYTE_P (bufp);
4692 unsigned char *pend = bufp->buffer + bufp->used;
4694 assert (p1 >= bufp->buffer && p1 < pend
4695 && p2 >= bufp->buffer && p2 <= pend);
4697 /* Skip over open/close-group commands.
4698 If what follows this loop is a ...+ construct,
4699 look at what begins its body, since we will have to
4700 match at least one of that. */
4701 p2 = skip_noops (p2, pend);
4702 /* The same skip can be done for p1, except that this function
4703 is only used in the case where p1 is a simple match operator. */
4704 /* p1 = skip_noops (p1, pend); */
4706 assert (p1 >= bufp->buffer && p1 < pend
4707 && p2 >= bufp->buffer && p2 <= pend);
4709 op2 = p2 == pend ? succeed : *p2;
4711 switch (op2)
4713 case succeed:
4714 case endbuf:
4715 /* If we're at the end of the pattern, we can change. */
4716 if (skip_one_char (p1))
4718 DEBUG_PRINT (" End of pattern: fast loop.\n");
4719 return 1;
4721 break;
4723 case endline:
4724 case exactn:
4726 register re_wchar_t c
4727 = (re_opcode_t) *p2 == endline ? '\n'
4728 : RE_STRING_CHAR (p2 + 2, multibyte);
4730 if ((re_opcode_t) *p1 == exactn)
4732 if (c != RE_STRING_CHAR (p1 + 2, multibyte))
4734 DEBUG_PRINT (" '%c' != '%c' => fast loop.\n", c, p1[2]);
4735 return 1;
4739 else if ((re_opcode_t) *p1 == charset
4740 || (re_opcode_t) *p1 == charset_not)
4742 if (!execute_charset (&p1, c, c, !multibyte || IS_REAL_ASCII (c)))
4744 DEBUG_PRINT (" No match => fast loop.\n");
4745 return 1;
4748 else if ((re_opcode_t) *p1 == anychar
4749 && c == '\n')
4751 DEBUG_PRINT (" . != \\n => fast loop.\n");
4752 return 1;
4755 break;
4757 case charset:
4759 if ((re_opcode_t) *p1 == exactn)
4760 /* Reuse the code above. */
4761 return mutually_exclusive_p (bufp, p2, p1);
4763 /* It is hard to list up all the character in charset
4764 P2 if it includes multibyte character. Give up in
4765 such case. */
4766 else if (!multibyte || !CHARSET_RANGE_TABLE_EXISTS_P (p2))
4768 /* Now, we are sure that P2 has no range table.
4769 So, for the size of bitmap in P2, `p2[1]' is
4770 enough. But P1 may have range table, so the
4771 size of bitmap table of P1 is extracted by
4772 using macro `CHARSET_BITMAP_SIZE'.
4774 In a multibyte case, we know that all the character
4775 listed in P2 is ASCII. In a unibyte case, P1 has only a
4776 bitmap table. So, in both cases, it is enough to test
4777 only the bitmap table of P1. */
4779 if ((re_opcode_t) *p1 == charset)
4781 int idx;
4782 /* We win if the charset inside the loop
4783 has no overlap with the one after the loop. */
4784 for (idx = 0;
4785 (idx < (int) p2[1]
4786 && idx < CHARSET_BITMAP_SIZE (p1));
4787 idx++)
4788 if ((p2[2 + idx] & p1[2 + idx]) != 0)
4789 break;
4791 if (idx == p2[1]
4792 || idx == CHARSET_BITMAP_SIZE (p1))
4794 DEBUG_PRINT (" No match => fast loop.\n");
4795 return 1;
4798 else if ((re_opcode_t) *p1 == charset_not)
4800 int idx;
4801 /* We win if the charset_not inside the loop lists
4802 every character listed in the charset after. */
4803 for (idx = 0; idx < (int) p2[1]; idx++)
4804 if (! (p2[2 + idx] == 0
4805 || (idx < CHARSET_BITMAP_SIZE (p1)
4806 && ((p2[2 + idx] & ~ p1[2 + idx]) == 0))))
4807 break;
4809 if (idx == p2[1])
4811 DEBUG_PRINT (" No match => fast loop.\n");
4812 return 1;
4817 break;
4819 case charset_not:
4820 switch (*p1)
4822 case exactn:
4823 case charset:
4824 /* Reuse the code above. */
4825 return mutually_exclusive_p (bufp, p2, p1);
4826 case charset_not:
4827 /* When we have two charset_not, it's very unlikely that
4828 they don't overlap. The union of the two sets of excluded
4829 chars should cover all possible chars, which, as a matter of
4830 fact, is virtually impossible in multibyte buffers. */
4831 break;
4833 break;
4835 case wordend:
4836 return ((re_opcode_t) *p1 == syntaxspec && p1[1] == Sword);
4837 case symend:
4838 return ((re_opcode_t) *p1 == syntaxspec
4839 && (p1[1] == Ssymbol || p1[1] == Sword));
4840 case notsyntaxspec:
4841 return ((re_opcode_t) *p1 == syntaxspec && p1[1] == p2[1]);
4843 case wordbeg:
4844 return ((re_opcode_t) *p1 == notsyntaxspec && p1[1] == Sword);
4845 case symbeg:
4846 return ((re_opcode_t) *p1 == notsyntaxspec
4847 && (p1[1] == Ssymbol || p1[1] == Sword));
4848 case syntaxspec:
4849 return ((re_opcode_t) *p1 == notsyntaxspec && p1[1] == p2[1]);
4851 case wordbound:
4852 return (((re_opcode_t) *p1 == notsyntaxspec
4853 || (re_opcode_t) *p1 == syntaxspec)
4854 && p1[1] == Sword);
4856 #ifdef emacs
4857 case categoryspec:
4858 return ((re_opcode_t) *p1 == notcategoryspec && p1[1] == p2[1]);
4859 case notcategoryspec:
4860 return ((re_opcode_t) *p1 == categoryspec && p1[1] == p2[1]);
4861 #endif /* emacs */
4863 default:
4867 /* Safe default. */
4868 return 0;
4872 /* Matching routines. */
4874 #ifndef emacs /* Emacs never uses this. */
4875 /* re_match is like re_match_2 except it takes only a single string. */
4877 regoff_t
4878 re_match (struct re_pattern_buffer *bufp, const char *string,
4879 size_t size, ssize_t pos, struct re_registers *regs)
4881 regoff_t result = re_match_2_internal (bufp, NULL, 0, (re_char*) string,
4882 size, pos, regs, size);
4883 return result;
4885 WEAK_ALIAS (__re_match, re_match)
4886 #endif /* not emacs */
4888 #ifdef emacs
4889 /* In Emacs, this is the string or buffer in which we are matching.
4890 See the declaration in regex.h for details. */
4891 Lisp_Object re_match_object;
4892 #endif
4894 /* re_match_2 matches the compiled pattern in BUFP against the
4895 the (virtual) concatenation of STRING1 and STRING2 (of length SIZE1
4896 and SIZE2, respectively). We start matching at POS, and stop
4897 matching at STOP.
4899 If REGS is non-null and the `no_sub' field of BUFP is nonzero, we
4900 store offsets for the substring each group matched in REGS. See the
4901 documentation for exactly how many groups we fill.
4903 We return -1 if no match, -2 if an internal error (such as the
4904 failure stack overflowing). Otherwise, we return the length of the
4905 matched substring. */
4907 regoff_t
4908 re_match_2 (struct re_pattern_buffer *bufp, const char *string1,
4909 size_t size1, const char *string2, size_t size2, ssize_t pos,
4910 struct re_registers *regs, ssize_t stop)
4912 regoff_t result;
4914 #ifdef emacs
4915 ssize_t charpos;
4916 gl_state.object = re_match_object; /* Used by SYNTAX_TABLE_BYTE_TO_CHAR. */
4917 charpos = SYNTAX_TABLE_BYTE_TO_CHAR (POS_AS_IN_BUFFER (pos));
4918 SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object, charpos, 1);
4919 #endif
4921 result = re_match_2_internal (bufp, (re_char*) string1, size1,
4922 (re_char*) string2, size2,
4923 pos, regs, stop);
4924 return result;
4926 WEAK_ALIAS (__re_match_2, re_match_2)
4929 /* This is a separate function so that we can force an alloca cleanup
4930 afterwards. */
4931 static regoff_t
4932 re_match_2_internal (struct re_pattern_buffer *bufp, const_re_char *string1,
4933 size_t size1, const_re_char *string2, size_t size2,
4934 ssize_t pos, struct re_registers *regs, ssize_t stop)
4936 /* General temporaries. */
4937 int mcnt;
4938 size_t reg;
4940 /* Just past the end of the corresponding string. */
4941 re_char *end1, *end2;
4943 /* Pointers into string1 and string2, just past the last characters in
4944 each to consider matching. */
4945 re_char *end_match_1, *end_match_2;
4947 /* Where we are in the data, and the end of the current string. */
4948 re_char *d, *dend;
4950 /* Used sometimes to remember where we were before starting matching
4951 an operator so that we can go back in case of failure. This "atomic"
4952 behavior of matching opcodes is indispensable to the correctness
4953 of the on_failure_keep_string_jump optimization. */
4954 re_char *dfail;
4956 /* Where we are in the pattern, and the end of the pattern. */
4957 re_char *p = bufp->buffer;
4958 re_char *pend = p + bufp->used;
4960 /* We use this to map every character in the string. */
4961 RE_TRANSLATE_TYPE translate = bufp->translate;
4963 /* Nonzero if BUFP is setup from a multibyte regex. */
4964 const boolean multibyte = RE_MULTIBYTE_P (bufp);
4966 /* Nonzero if STRING1/STRING2 are multibyte. */
4967 const boolean target_multibyte = RE_TARGET_MULTIBYTE_P (bufp);
4969 /* Failure point stack. Each place that can handle a failure further
4970 down the line pushes a failure point on this stack. It consists of
4971 regstart, and regend for all registers corresponding to
4972 the subexpressions we're currently inside, plus the number of such
4973 registers, and, finally, two char *'s. The first char * is where
4974 to resume scanning the pattern; the second one is where to resume
4975 scanning the strings. */
4976 #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
4977 fail_stack_type fail_stack;
4978 #endif
4979 #ifdef DEBUG_COMPILES_ARGUMENTS
4980 unsigned nfailure_points_pushed = 0, nfailure_points_popped = 0;
4981 #endif
4983 #if defined REL_ALLOC && defined REGEX_MALLOC
4984 /* This holds the pointer to the failure stack, when
4985 it is allocated relocatably. */
4986 fail_stack_elt_t *failure_stack_ptr;
4987 #endif
4989 /* We fill all the registers internally, independent of what we
4990 return, for use in backreferences. The number here includes
4991 an element for register zero. */
4992 size_t num_regs = bufp->re_nsub + 1;
4994 /* Information on the contents of registers. These are pointers into
4995 the input strings; they record just what was matched (on this
4996 attempt) by a subexpression part of the pattern, that is, the
4997 regnum-th regstart pointer points to where in the pattern we began
4998 matching and the regnum-th regend points to right after where we
4999 stopped matching the regnum-th subexpression. (The zeroth register
5000 keeps track of what the whole pattern matches.) */
5001 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
5002 re_char **regstart, **regend;
5003 #endif
5005 /* The following record the register info as found in the above
5006 variables when we find a match better than any we've seen before.
5007 This happens as we backtrack through the failure points, which in
5008 turn happens only if we have not yet matched the entire string. */
5009 unsigned best_regs_set = false;
5010 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
5011 re_char **best_regstart, **best_regend;
5012 #endif
5014 /* Logically, this is `best_regend[0]'. But we don't want to have to
5015 allocate space for that if we're not allocating space for anything
5016 else (see below). Also, we never need info about register 0 for
5017 any of the other register vectors, and it seems rather a kludge to
5018 treat `best_regend' differently than the rest. So we keep track of
5019 the end of the best match so far in a separate variable. We
5020 initialize this to NULL so that when we backtrack the first time
5021 and need to test it, it's not garbage. */
5022 re_char *match_end = NULL;
5024 #ifdef DEBUG_COMPILES_ARGUMENTS
5025 /* Counts the total number of registers pushed. */
5026 unsigned num_regs_pushed = 0;
5027 #endif
5029 DEBUG_PRINT ("\n\nEntering re_match_2.\n");
5031 REGEX_USE_SAFE_ALLOCA;
5033 INIT_FAIL_STACK ();
5035 #ifdef MATCH_MAY_ALLOCATE
5036 /* Do not bother to initialize all the register variables if there are
5037 no groups in the pattern, as it takes a fair amount of time. If
5038 there are groups, we include space for register 0 (the whole
5039 pattern), even though we never use it, since it simplifies the
5040 array indexing. We should fix this. */
5041 if (bufp->re_nsub)
5043 regstart = REGEX_TALLOC (num_regs, re_char *);
5044 regend = REGEX_TALLOC (num_regs, re_char *);
5045 best_regstart = REGEX_TALLOC (num_regs, re_char *);
5046 best_regend = REGEX_TALLOC (num_regs, re_char *);
5048 if (!(regstart && regend && best_regstart && best_regend))
5050 FREE_VARIABLES ();
5051 return -2;
5054 else
5056 /* We must initialize all our variables to NULL, so that
5057 `FREE_VARIABLES' doesn't try to free them. */
5058 regstart = regend = best_regstart = best_regend = NULL;
5060 #endif /* MATCH_MAY_ALLOCATE */
5062 /* The starting position is bogus. */
5063 if (pos < 0 || pos > size1 + size2)
5065 FREE_VARIABLES ();
5066 return -1;
5069 /* Initialize subexpression text positions to -1 to mark ones that no
5070 start_memory/stop_memory has been seen for. Also initialize the
5071 register information struct. */
5072 for (reg = 1; reg < num_regs; reg++)
5073 regstart[reg] = regend[reg] = NULL;
5075 /* We move `string1' into `string2' if the latter's empty -- but not if
5076 `string1' is null. */
5077 if (size2 == 0 && string1 != NULL)
5079 string2 = string1;
5080 size2 = size1;
5081 string1 = 0;
5082 size1 = 0;
5084 end1 = string1 + size1;
5085 end2 = string2 + size2;
5087 /* `p' scans through the pattern as `d' scans through the data.
5088 `dend' is the end of the input string that `d' points within. `d'
5089 is advanced into the following input string whenever necessary, but
5090 this happens before fetching; therefore, at the beginning of the
5091 loop, `d' can be pointing at the end of a string, but it cannot
5092 equal `string2'. */
5093 if (pos >= size1)
5095 /* Only match within string2. */
5096 d = string2 + pos - size1;
5097 dend = end_match_2 = string2 + stop - size1;
5098 end_match_1 = end1; /* Just to give it a value. */
5100 else
5102 if (stop < size1)
5104 /* Only match within string1. */
5105 end_match_1 = string1 + stop;
5106 /* BEWARE!
5107 When we reach end_match_1, PREFETCH normally switches to string2.
5108 But in the present case, this means that just doing a PREFETCH
5109 makes us jump from `stop' to `gap' within the string.
5110 What we really want here is for the search to stop as
5111 soon as we hit end_match_1. That's why we set end_match_2
5112 to end_match_1 (since PREFETCH fails as soon as we hit
5113 end_match_2). */
5114 end_match_2 = end_match_1;
5116 else
5117 { /* It's important to use this code when stop == size so that
5118 moving `d' from end1 to string2 will not prevent the d == dend
5119 check from catching the end of string. */
5120 end_match_1 = end1;
5121 end_match_2 = string2 + stop - size1;
5123 d = string1 + pos;
5124 dend = end_match_1;
5127 DEBUG_PRINT ("The compiled pattern is: ");
5128 DEBUG_PRINT_COMPILED_PATTERN (bufp, p, pend);
5129 DEBUG_PRINT ("The string to match is: \"");
5130 DEBUG_PRINT_DOUBLE_STRING (d, string1, size1, string2, size2);
5131 DEBUG_PRINT ("\"\n");
5133 /* This loops over pattern commands. It exits by returning from the
5134 function if the match is complete, or it drops through if the match
5135 fails at this starting point in the input data. */
5136 for (;;)
5138 DEBUG_PRINT ("\n%p: ", p);
5140 if (p == pend)
5142 /* End of pattern means we might have succeeded. */
5143 DEBUG_PRINT ("end of pattern ... ");
5145 /* If we haven't matched the entire string, and we want the
5146 longest match, try backtracking. */
5147 if (d != end_match_2)
5149 /* True if this match is the best seen so far. */
5150 bool best_match_p;
5153 /* True if this match ends in the same string (string1
5154 or string2) as the best previous match. */
5155 bool same_str_p = (FIRST_STRING_P (match_end)
5156 == FIRST_STRING_P (d));
5158 /* AIX compiler got confused when this was combined
5159 with the previous declaration. */
5160 if (same_str_p)
5161 best_match_p = d > match_end;
5162 else
5163 best_match_p = !FIRST_STRING_P (d);
5166 DEBUG_PRINT ("backtracking.\n");
5168 if (!FAIL_STACK_EMPTY ())
5169 { /* More failure points to try. */
5171 /* If exceeds best match so far, save it. */
5172 if (!best_regs_set || best_match_p)
5174 best_regs_set = true;
5175 match_end = d;
5177 DEBUG_PRINT ("\nSAVING match as best so far.\n");
5179 for (reg = 1; reg < num_regs; reg++)
5181 best_regstart[reg] = regstart[reg];
5182 best_regend[reg] = regend[reg];
5185 goto fail;
5188 /* If no failure points, don't restore garbage. And if
5189 last match is real best match, don't restore second
5190 best one. */
5191 else if (best_regs_set && !best_match_p)
5193 restore_best_regs:
5194 /* Restore best match. It may happen that `dend ==
5195 end_match_1' while the restored d is in string2.
5196 For example, the pattern `x.*y.*z' against the
5197 strings `x-' and `y-z-', if the two strings are
5198 not consecutive in memory. */
5199 DEBUG_PRINT ("Restoring best registers.\n");
5201 d = match_end;
5202 dend = ((d >= string1 && d <= end1)
5203 ? end_match_1 : end_match_2);
5205 for (reg = 1; reg < num_regs; reg++)
5207 regstart[reg] = best_regstart[reg];
5208 regend[reg] = best_regend[reg];
5211 } /* d != end_match_2 */
5213 succeed_label:
5214 DEBUG_PRINT ("Accepting match.\n");
5216 /* If caller wants register contents data back, do it. */
5217 if (regs && !bufp->no_sub)
5219 /* Have the register data arrays been allocated? */
5220 if (bufp->regs_allocated == REGS_UNALLOCATED)
5221 { /* No. So allocate them with malloc. We need one
5222 extra element beyond `num_regs' for the `-1' marker
5223 GNU code uses. */
5224 regs->num_regs = max (RE_NREGS, num_regs + 1);
5225 regs->start = TALLOC (regs->num_regs, regoff_t);
5226 regs->end = TALLOC (regs->num_regs, regoff_t);
5227 if (regs->start == NULL || regs->end == NULL)
5229 FREE_VARIABLES ();
5230 return -2;
5232 bufp->regs_allocated = REGS_REALLOCATE;
5234 else if (bufp->regs_allocated == REGS_REALLOCATE)
5235 { /* Yes. If we need more elements than were already
5236 allocated, reallocate them. If we need fewer, just
5237 leave it alone. */
5238 if (regs->num_regs < num_regs + 1)
5240 regs->num_regs = num_regs + 1;
5241 RETALLOC (regs->start, regs->num_regs, regoff_t);
5242 RETALLOC (regs->end, regs->num_regs, regoff_t);
5243 if (regs->start == NULL || regs->end == NULL)
5245 FREE_VARIABLES ();
5246 return -2;
5250 else
5252 /* These braces fend off a "empty body in an else-statement"
5253 warning under GCC when assert expands to nothing. */
5254 assert (bufp->regs_allocated == REGS_FIXED);
5257 /* Convert the pointer data in `regstart' and `regend' to
5258 indices. Register zero has to be set differently,
5259 since we haven't kept track of any info for it. */
5260 if (regs->num_regs > 0)
5262 regs->start[0] = pos;
5263 regs->end[0] = POINTER_TO_OFFSET (d);
5266 /* Go through the first `min (num_regs, regs->num_regs)'
5267 registers, since that is all we initialized. */
5268 for (reg = 1; reg < min (num_regs, regs->num_regs); reg++)
5270 if (REG_UNSET (regstart[reg]) || REG_UNSET (regend[reg]))
5271 regs->start[reg] = regs->end[reg] = -1;
5272 else
5274 regs->start[reg] = POINTER_TO_OFFSET (regstart[reg]);
5275 regs->end[reg] = POINTER_TO_OFFSET (regend[reg]);
5279 /* If the regs structure we return has more elements than
5280 were in the pattern, set the extra elements to -1. If
5281 we (re)allocated the registers, this is the case,
5282 because we always allocate enough to have at least one
5283 -1 at the end. */
5284 for (reg = num_regs; reg < regs->num_regs; reg++)
5285 regs->start[reg] = regs->end[reg] = -1;
5286 } /* regs && !bufp->no_sub */
5288 DEBUG_PRINT ("%u failure points pushed, %u popped (%u remain).\n",
5289 nfailure_points_pushed, nfailure_points_popped,
5290 nfailure_points_pushed - nfailure_points_popped);
5291 DEBUG_PRINT ("%u registers pushed.\n", num_regs_pushed);
5293 ptrdiff_t dcnt = POINTER_TO_OFFSET (d) - pos;
5295 DEBUG_PRINT ("Returning %td from re_match_2.\n", dcnt);
5297 FREE_VARIABLES ();
5298 return dcnt;
5301 /* Otherwise match next pattern command. */
5302 switch (*p++)
5304 /* Ignore these. Used to ignore the n of succeed_n's which
5305 currently have n == 0. */
5306 case no_op:
5307 DEBUG_PRINT ("EXECUTING no_op.\n");
5308 break;
5310 case succeed:
5311 DEBUG_PRINT ("EXECUTING succeed.\n");
5312 goto succeed_label;
5314 /* Match the next n pattern characters exactly. The following
5315 byte in the pattern defines n, and the n bytes after that
5316 are the characters to match. */
5317 case exactn:
5318 mcnt = *p++;
5319 DEBUG_PRINT ("EXECUTING exactn %d.\n", mcnt);
5321 /* Remember the start point to rollback upon failure. */
5322 dfail = d;
5324 #ifndef emacs
5325 /* This is written out as an if-else so we don't waste time
5326 testing `translate' inside the loop. */
5327 if (RE_TRANSLATE_P (translate))
5330 PREFETCH ();
5331 if (RE_TRANSLATE (translate, *d) != *p++)
5333 d = dfail;
5334 goto fail;
5336 d++;
5338 while (--mcnt);
5339 else
5342 PREFETCH ();
5343 if (*d++ != *p++)
5345 d = dfail;
5346 goto fail;
5349 while (--mcnt);
5350 #else /* emacs */
5351 /* The cost of testing `translate' is comparatively small. */
5352 if (target_multibyte)
5355 int pat_charlen, buf_charlen;
5356 int pat_ch, buf_ch;
5358 PREFETCH ();
5359 if (multibyte)
5360 pat_ch = STRING_CHAR_AND_LENGTH (p, pat_charlen);
5361 else
5363 pat_ch = RE_CHAR_TO_MULTIBYTE (*p);
5364 pat_charlen = 1;
5366 buf_ch = STRING_CHAR_AND_LENGTH (d, buf_charlen);
5368 if (TRANSLATE (buf_ch) != pat_ch)
5370 d = dfail;
5371 goto fail;
5374 p += pat_charlen;
5375 d += buf_charlen;
5376 mcnt -= pat_charlen;
5378 while (mcnt > 0);
5379 else
5382 int pat_charlen;
5383 int pat_ch, buf_ch;
5385 PREFETCH ();
5386 if (multibyte)
5388 pat_ch = STRING_CHAR_AND_LENGTH (p, pat_charlen);
5389 pat_ch = RE_CHAR_TO_UNIBYTE (pat_ch);
5391 else
5393 pat_ch = *p;
5394 pat_charlen = 1;
5396 buf_ch = RE_CHAR_TO_MULTIBYTE (*d);
5397 if (! CHAR_BYTE8_P (buf_ch))
5399 buf_ch = TRANSLATE (buf_ch);
5400 buf_ch = RE_CHAR_TO_UNIBYTE (buf_ch);
5401 if (buf_ch < 0)
5402 buf_ch = *d;
5404 else
5405 buf_ch = *d;
5406 if (buf_ch != pat_ch)
5408 d = dfail;
5409 goto fail;
5411 p += pat_charlen;
5412 d++;
5414 while (--mcnt);
5415 #endif
5416 break;
5419 /* Match any character except possibly a newline or a null. */
5420 case anychar:
5422 int buf_charlen;
5423 re_wchar_t buf_ch;
5424 reg_syntax_t syntax;
5426 DEBUG_PRINT ("EXECUTING anychar.\n");
5428 PREFETCH ();
5429 buf_ch = RE_STRING_CHAR_AND_LENGTH (d, buf_charlen,
5430 target_multibyte);
5431 buf_ch = TRANSLATE (buf_ch);
5433 #ifdef emacs
5434 syntax = RE_SYNTAX_EMACS;
5435 #else
5436 syntax = bufp->syntax;
5437 #endif
5439 if ((!(syntax & RE_DOT_NEWLINE) && buf_ch == '\n')
5440 || ((syntax & RE_DOT_NOT_NULL) && buf_ch == '\000'))
5441 goto fail;
5443 DEBUG_PRINT (" Matched \"%d\".\n", *d);
5444 d += buf_charlen;
5446 break;
5449 case charset:
5450 case charset_not:
5452 register unsigned int c, corig;
5453 int len;
5455 /* Whether matching against a unibyte character. */
5456 boolean unibyte_char = false;
5458 DEBUG_PRINT ("EXECUTING charset%s.\n",
5459 (re_opcode_t) *(p - 1) == charset_not ? "_not" : "");
5461 PREFETCH ();
5462 corig = c = RE_STRING_CHAR_AND_LENGTH (d, len, target_multibyte);
5463 if (target_multibyte)
5465 int c1;
5467 c = TRANSLATE (c);
5468 c1 = RE_CHAR_TO_UNIBYTE (c);
5469 if (c1 >= 0)
5471 unibyte_char = true;
5472 c = c1;
5475 else
5477 int c1 = RE_CHAR_TO_MULTIBYTE (c);
5479 if (! CHAR_BYTE8_P (c1))
5481 c1 = TRANSLATE (c1);
5482 c1 = RE_CHAR_TO_UNIBYTE (c1);
5483 if (c1 >= 0)
5485 unibyte_char = true;
5486 c = c1;
5489 else
5490 unibyte_char = true;
5493 p -= 1;
5494 if (!execute_charset (&p, c, corig, unibyte_char))
5495 goto fail;
5497 d += len;
5499 break;
5502 /* The beginning of a group is represented by start_memory.
5503 The argument is the register number. The text
5504 matched within the group is recorded (in the internal
5505 registers data structure) under the register number. */
5506 case start_memory:
5507 DEBUG_PRINT ("EXECUTING start_memory %d:\n", *p);
5509 /* In case we need to undo this operation (via backtracking). */
5510 PUSH_FAILURE_REG (*p);
5512 regstart[*p] = d;
5513 regend[*p] = NULL; /* probably unnecessary. -sm */
5514 DEBUG_PRINT (" regstart: %td\n", POINTER_TO_OFFSET (regstart[*p]));
5516 /* Move past the register number and inner group count. */
5517 p += 1;
5518 break;
5521 /* The stop_memory opcode represents the end of a group. Its
5522 argument is the same as start_memory's: the register number. */
5523 case stop_memory:
5524 DEBUG_PRINT ("EXECUTING stop_memory %d:\n", *p);
5526 assert (!REG_UNSET (regstart[*p]));
5527 /* Strictly speaking, there should be code such as:
5529 assert (REG_UNSET (regend[*p]));
5530 PUSH_FAILURE_REGSTOP ((unsigned int)*p);
5532 But the only info to be pushed is regend[*p] and it is known to
5533 be UNSET, so there really isn't anything to push.
5534 Not pushing anything, on the other hand deprives us from the
5535 guarantee that regend[*p] is UNSET since undoing this operation
5536 will not reset its value properly. This is not important since
5537 the value will only be read on the next start_memory or at
5538 the very end and both events can only happen if this stop_memory
5539 is *not* undone. */
5541 regend[*p] = d;
5542 DEBUG_PRINT (" regend: %td\n", POINTER_TO_OFFSET (regend[*p]));
5544 /* Move past the register number and the inner group count. */
5545 p += 1;
5546 break;
5549 /* \<digit> has been turned into a `duplicate' command which is
5550 followed by the numeric value of <digit> as the register number. */
5551 case duplicate:
5553 register re_char *d2, *dend2;
5554 int regno = *p++; /* Get which register to match against. */
5555 DEBUG_PRINT ("EXECUTING duplicate %d.\n", regno);
5557 /* Can't back reference a group which we've never matched. */
5558 if (REG_UNSET (regstart[regno]) || REG_UNSET (regend[regno]))
5559 goto fail;
5561 /* Where in input to try to start matching. */
5562 d2 = regstart[regno];
5564 /* Remember the start point to rollback upon failure. */
5565 dfail = d;
5567 /* Where to stop matching; if both the place to start and
5568 the place to stop matching are in the same string, then
5569 set to the place to stop, otherwise, for now have to use
5570 the end of the first string. */
5572 dend2 = ((FIRST_STRING_P (regstart[regno])
5573 == FIRST_STRING_P (regend[regno]))
5574 ? regend[regno] : end_match_1);
5575 for (;;)
5577 ptrdiff_t dcnt;
5579 /* If necessary, advance to next segment in register
5580 contents. */
5581 while (d2 == dend2)
5583 if (dend2 == end_match_2) break;
5584 if (dend2 == regend[regno]) break;
5586 /* End of string1 => advance to string2. */
5587 d2 = string2;
5588 dend2 = regend[regno];
5590 /* At end of register contents => success */
5591 if (d2 == dend2) break;
5593 /* If necessary, advance to next segment in data. */
5594 PREFETCH ();
5596 /* How many characters left in this segment to match. */
5597 dcnt = dend - d;
5599 /* Want how many consecutive characters we can match in
5600 one shot, so, if necessary, adjust the count. */
5601 if (dcnt > dend2 - d2)
5602 dcnt = dend2 - d2;
5604 /* Compare that many; failure if mismatch, else move
5605 past them. */
5606 if (RE_TRANSLATE_P (translate)
5607 ? bcmp_translate (d, d2, dcnt, translate, target_multibyte)
5608 : memcmp (d, d2, dcnt))
5610 d = dfail;
5611 goto fail;
5613 d += dcnt, d2 += dcnt;
5616 break;
5619 /* begline matches the empty string at the beginning of the string
5620 (unless `not_bol' is set in `bufp'), and after newlines. */
5621 case begline:
5622 DEBUG_PRINT ("EXECUTING begline.\n");
5624 if (AT_STRINGS_BEG (d))
5626 if (!bufp->not_bol) break;
5628 else
5630 unsigned c;
5631 GET_CHAR_BEFORE_2 (c, d, string1, end1, string2, end2);
5632 if (c == '\n')
5633 break;
5635 /* In all other cases, we fail. */
5636 goto fail;
5639 /* endline is the dual of begline. */
5640 case endline:
5641 DEBUG_PRINT ("EXECUTING endline.\n");
5643 if (AT_STRINGS_END (d))
5645 if (!bufp->not_eol) break;
5647 else
5649 PREFETCH_NOLIMIT ();
5650 if (*d == '\n')
5651 break;
5653 goto fail;
5656 /* Match at the very beginning of the data. */
5657 case begbuf:
5658 DEBUG_PRINT ("EXECUTING begbuf.\n");
5659 if (AT_STRINGS_BEG (d))
5660 break;
5661 goto fail;
5664 /* Match at the very end of the data. */
5665 case endbuf:
5666 DEBUG_PRINT ("EXECUTING endbuf.\n");
5667 if (AT_STRINGS_END (d))
5668 break;
5669 goto fail;
5672 /* on_failure_keep_string_jump is used to optimize `.*\n'. It
5673 pushes NULL as the value for the string on the stack. Then
5674 `POP_FAILURE_POINT' will keep the current value for the
5675 string, instead of restoring it. To see why, consider
5676 matching `foo\nbar' against `.*\n'. The .* matches the foo;
5677 then the . fails against the \n. But the next thing we want
5678 to do is match the \n against the \n; if we restored the
5679 string value, we would be back at the foo.
5681 Because this is used only in specific cases, we don't need to
5682 check all the things that `on_failure_jump' does, to make
5683 sure the right things get saved on the stack. Hence we don't
5684 share its code. The only reason to push anything on the
5685 stack at all is that otherwise we would have to change
5686 `anychar's code to do something besides goto fail in this
5687 case; that seems worse than this. */
5688 case on_failure_keep_string_jump:
5689 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5690 DEBUG_PRINT ("EXECUTING on_failure_keep_string_jump %d (to %p):\n",
5691 mcnt, p + mcnt);
5693 PUSH_FAILURE_POINT (p - 3, NULL);
5694 break;
5696 /* A nasty loop is introduced by the non-greedy *? and +?.
5697 With such loops, the stack only ever contains one failure point
5698 at a time, so that a plain on_failure_jump_loop kind of
5699 cycle detection cannot work. Worse yet, such a detection
5700 can not only fail to detect a cycle, but it can also wrongly
5701 detect a cycle (between different instantiations of the same
5702 loop).
5703 So the method used for those nasty loops is a little different:
5704 We use a special cycle-detection-stack-frame which is pushed
5705 when the on_failure_jump_nastyloop failure-point is *popped*.
5706 This special frame thus marks the beginning of one iteration
5707 through the loop and we can hence easily check right here
5708 whether something matched between the beginning and the end of
5709 the loop. */
5710 case on_failure_jump_nastyloop:
5711 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5712 DEBUG_PRINT ("EXECUTING on_failure_jump_nastyloop %d (to %p):\n",
5713 mcnt, p + mcnt);
5715 assert ((re_opcode_t)p[-4] == no_op);
5717 int cycle = 0;
5718 CHECK_INFINITE_LOOP (p - 4, d);
5719 if (!cycle)
5720 /* If there's a cycle, just continue without pushing
5721 this failure point. The failure point is the "try again"
5722 option, which shouldn't be tried.
5723 We want (x?)*?y\1z to match both xxyz and xxyxz. */
5724 PUSH_FAILURE_POINT (p - 3, d);
5726 break;
5728 /* Simple loop detecting on_failure_jump: just check on the
5729 failure stack if the same spot was already hit earlier. */
5730 case on_failure_jump_loop:
5731 on_failure:
5732 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5733 DEBUG_PRINT ("EXECUTING on_failure_jump_loop %d (to %p):\n",
5734 mcnt, p + mcnt);
5736 int cycle = 0;
5737 CHECK_INFINITE_LOOP (p - 3, d);
5738 if (cycle)
5739 /* If there's a cycle, get out of the loop, as if the matching
5740 had failed. We used to just `goto fail' here, but that was
5741 aborting the search a bit too early: we want to keep the
5742 empty-loop-match and keep matching after the loop.
5743 We want (x?)*y\1z to match both xxyz and xxyxz. */
5744 p += mcnt;
5745 else
5746 PUSH_FAILURE_POINT (p - 3, d);
5748 break;
5751 /* Uses of on_failure_jump:
5753 Each alternative starts with an on_failure_jump that points
5754 to the beginning of the next alternative. Each alternative
5755 except the last ends with a jump that in effect jumps past
5756 the rest of the alternatives. (They really jump to the
5757 ending jump of the following alternative, because tensioning
5758 these jumps is a hassle.)
5760 Repeats start with an on_failure_jump that points past both
5761 the repetition text and either the following jump or
5762 pop_failure_jump back to this on_failure_jump. */
5763 case on_failure_jump:
5764 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5765 DEBUG_PRINT ("EXECUTING on_failure_jump %d (to %p):\n",
5766 mcnt, p + mcnt);
5768 PUSH_FAILURE_POINT (p -3, d);
5769 break;
5771 /* This operation is used for greedy *.
5772 Compare the beginning of the repeat with what in the
5773 pattern follows its end. If we can establish that there
5774 is nothing that they would both match, i.e., that we
5775 would have to backtrack because of (as in, e.g., `a*a')
5776 then we can use a non-backtracking loop based on
5777 on_failure_keep_string_jump instead of on_failure_jump. */
5778 case on_failure_jump_smart:
5779 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5780 DEBUG_PRINT ("EXECUTING on_failure_jump_smart %d (to %p).\n",
5781 mcnt, p + mcnt);
5783 re_char *p1 = p; /* Next operation. */
5784 /* Here, we discard `const', making re_match non-reentrant. */
5785 unsigned char *p2 = (unsigned char*) p + mcnt; /* Jump dest. */
5786 unsigned char *p3 = (unsigned char*) p - 3; /* opcode location. */
5788 p -= 3; /* Reset so that we will re-execute the
5789 instruction once it's been changed. */
5791 EXTRACT_NUMBER (mcnt, p2 - 2);
5793 /* Ensure this is a indeed the trivial kind of loop
5794 we are expecting. */
5795 assert (skip_one_char (p1) == p2 - 3);
5796 assert ((re_opcode_t) p2[-3] == jump && p2 + mcnt == p);
5797 DEBUG_STATEMENT (debug += 2);
5798 if (mutually_exclusive_p (bufp, p1, p2))
5800 /* Use a fast `on_failure_keep_string_jump' loop. */
5801 DEBUG_PRINT (" smart exclusive => fast loop.\n");
5802 *p3 = (unsigned char) on_failure_keep_string_jump;
5803 STORE_NUMBER (p2 - 2, mcnt + 3);
5805 else
5807 /* Default to a safe `on_failure_jump' loop. */
5808 DEBUG_PRINT (" smart default => slow loop.\n");
5809 *p3 = (unsigned char) on_failure_jump;
5811 DEBUG_STATEMENT (debug -= 2);
5813 break;
5815 /* Unconditionally jump (without popping any failure points). */
5816 case jump:
5817 unconditional_jump:
5818 IMMEDIATE_QUIT_CHECK;
5819 EXTRACT_NUMBER_AND_INCR (mcnt, p); /* Get the amount to jump. */
5820 DEBUG_PRINT ("EXECUTING jump %d ", mcnt);
5821 p += mcnt; /* Do the jump. */
5822 DEBUG_PRINT ("(to %p).\n", p);
5823 break;
5826 /* Have to succeed matching what follows at least n times.
5827 After that, handle like `on_failure_jump'. */
5828 case succeed_n:
5829 /* Signedness doesn't matter since we only compare MCNT to 0. */
5830 EXTRACT_NUMBER (mcnt, p + 2);
5831 DEBUG_PRINT ("EXECUTING succeed_n %d.\n", mcnt);
5833 /* Originally, mcnt is how many times we HAVE to succeed. */
5834 if (mcnt != 0)
5836 /* Here, we discard `const', making re_match non-reentrant. */
5837 unsigned char *p2 = (unsigned char*) p + 2; /* counter loc. */
5838 mcnt--;
5839 p += 4;
5840 PUSH_NUMBER (p2, mcnt);
5842 else
5843 /* The two bytes encoding mcnt == 0 are two no_op opcodes. */
5844 goto on_failure;
5845 break;
5847 case jump_n:
5848 /* Signedness doesn't matter since we only compare MCNT to 0. */
5849 EXTRACT_NUMBER (mcnt, p + 2);
5850 DEBUG_PRINT ("EXECUTING jump_n %d.\n", mcnt);
5852 /* Originally, this is how many times we CAN jump. */
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 PUSH_NUMBER (p2, mcnt);
5859 goto unconditional_jump;
5861 /* If don't have to jump any more, skip over the rest of command. */
5862 else
5863 p += 4;
5864 break;
5866 case set_number_at:
5868 unsigned char *p2; /* Location of the counter. */
5869 DEBUG_PRINT ("EXECUTING set_number_at.\n");
5871 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5872 /* Here, we discard `const', making re_match non-reentrant. */
5873 p2 = (unsigned char*) p + mcnt;
5874 /* Signedness doesn't matter since we only copy MCNT's bits. */
5875 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5876 DEBUG_PRINT (" Setting %p to %d.\n", p2, mcnt);
5877 PUSH_NUMBER (p2, mcnt);
5878 break;
5881 case wordbound:
5882 case notwordbound:
5884 boolean not = (re_opcode_t) *(p - 1) == notwordbound;
5885 DEBUG_PRINT ("EXECUTING %swordbound.\n", not ? "not" : "");
5887 /* We SUCCEED (or FAIL) in one of the following cases: */
5889 /* Case 1: D is at the beginning or the end of string. */
5890 if (AT_STRINGS_BEG (d) || AT_STRINGS_END (d))
5891 not = !not;
5892 else
5894 /* C1 is the character before D, S1 is the syntax of C1, C2
5895 is the character at D, and S2 is the syntax of C2. */
5896 re_wchar_t c1, c2;
5897 int s1, s2;
5898 int dummy;
5899 #ifdef emacs
5900 ssize_t offset = PTR_TO_OFFSET (d - 1);
5901 ssize_t charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
5902 UPDATE_SYNTAX_TABLE_FAST (charpos);
5903 #endif
5904 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
5905 s1 = SYNTAX (c1);
5906 #ifdef emacs
5907 UPDATE_SYNTAX_TABLE_FORWARD_FAST (charpos + 1);
5908 #endif
5909 PREFETCH_NOLIMIT ();
5910 GET_CHAR_AFTER (c2, d, dummy);
5911 s2 = SYNTAX (c2);
5913 if (/* Case 2: Only one of S1 and S2 is Sword. */
5914 ((s1 == Sword) != (s2 == Sword))
5915 /* Case 3: Both of S1 and S2 are Sword, and macro
5916 WORD_BOUNDARY_P (C1, C2) returns nonzero. */
5917 || ((s1 == Sword) && WORD_BOUNDARY_P (c1, c2)))
5918 not = !not;
5920 if (not)
5921 break;
5922 else
5923 goto fail;
5926 case wordbeg:
5927 DEBUG_PRINT ("EXECUTING wordbeg.\n");
5929 /* We FAIL in one of the following cases: */
5931 /* Case 1: D is at the end of string. */
5932 if (AT_STRINGS_END (d))
5933 goto fail;
5934 else
5936 /* C1 is the character before D, S1 is the syntax of C1, C2
5937 is the character at D, and S2 is the syntax of C2. */
5938 re_wchar_t c1, c2;
5939 int s1, s2;
5940 int dummy;
5941 #ifdef emacs
5942 ssize_t offset = PTR_TO_OFFSET (d);
5943 ssize_t charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
5944 UPDATE_SYNTAX_TABLE_FAST (charpos);
5945 #endif
5946 PREFETCH ();
5947 GET_CHAR_AFTER (c2, d, dummy);
5948 s2 = SYNTAX (c2);
5950 /* Case 2: S2 is not Sword. */
5951 if (s2 != Sword)
5952 goto fail;
5954 /* Case 3: D is not at the beginning of string ... */
5955 if (!AT_STRINGS_BEG (d))
5957 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
5958 #ifdef emacs
5959 UPDATE_SYNTAX_TABLE_BACKWARD (charpos - 1);
5960 #endif
5961 s1 = SYNTAX (c1);
5963 /* ... and S1 is Sword, and WORD_BOUNDARY_P (C1, C2)
5964 returns 0. */
5965 if ((s1 == Sword) && !WORD_BOUNDARY_P (c1, c2))
5966 goto fail;
5969 break;
5971 case wordend:
5972 DEBUG_PRINT ("EXECUTING wordend.\n");
5974 /* We FAIL in one of the following cases: */
5976 /* Case 1: D is at the beginning of string. */
5977 if (AT_STRINGS_BEG (d))
5978 goto fail;
5979 else
5981 /* C1 is the character before D, S1 is the syntax of C1, C2
5982 is the character at D, and S2 is the syntax of C2. */
5983 re_wchar_t c1, c2;
5984 int s1, s2;
5985 int dummy;
5986 #ifdef emacs
5987 ssize_t offset = PTR_TO_OFFSET (d) - 1;
5988 ssize_t charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
5989 UPDATE_SYNTAX_TABLE_FAST (charpos);
5990 #endif
5991 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
5992 s1 = SYNTAX (c1);
5994 /* Case 2: S1 is not Sword. */
5995 if (s1 != Sword)
5996 goto fail;
5998 /* Case 3: D is not at the end of string ... */
5999 if (!AT_STRINGS_END (d))
6001 PREFETCH_NOLIMIT ();
6002 GET_CHAR_AFTER (c2, d, dummy);
6003 #ifdef emacs
6004 UPDATE_SYNTAX_TABLE_FORWARD_FAST (charpos);
6005 #endif
6006 s2 = SYNTAX (c2);
6008 /* ... and S2 is Sword, and WORD_BOUNDARY_P (C1, C2)
6009 returns 0. */
6010 if ((s2 == Sword) && !WORD_BOUNDARY_P (c1, c2))
6011 goto fail;
6014 break;
6016 case symbeg:
6017 DEBUG_PRINT ("EXECUTING symbeg.\n");
6019 /* We FAIL in one of the following cases: */
6021 /* Case 1: D is at the end of string. */
6022 if (AT_STRINGS_END (d))
6023 goto fail;
6024 else
6026 /* C1 is the character before D, S1 is the syntax of C1, C2
6027 is the character at D, and S2 is the syntax of C2. */
6028 re_wchar_t c1, c2;
6029 int s1, s2;
6030 #ifdef emacs
6031 ssize_t offset = PTR_TO_OFFSET (d);
6032 ssize_t charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
6033 UPDATE_SYNTAX_TABLE_FAST (charpos);
6034 #endif
6035 PREFETCH ();
6036 c2 = RE_STRING_CHAR (d, target_multibyte);
6037 s2 = SYNTAX (c2);
6039 /* Case 2: S2 is neither Sword nor Ssymbol. */
6040 if (s2 != Sword && s2 != Ssymbol)
6041 goto fail;
6043 /* Case 3: D is not at the beginning of string ... */
6044 if (!AT_STRINGS_BEG (d))
6046 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
6047 #ifdef emacs
6048 UPDATE_SYNTAX_TABLE_BACKWARD (charpos - 1);
6049 #endif
6050 s1 = SYNTAX (c1);
6052 /* ... and S1 is Sword or Ssymbol. */
6053 if (s1 == Sword || s1 == Ssymbol)
6054 goto fail;
6057 break;
6059 case symend:
6060 DEBUG_PRINT ("EXECUTING symend.\n");
6062 /* We FAIL in one of the following cases: */
6064 /* Case 1: D is at the beginning of string. */
6065 if (AT_STRINGS_BEG (d))
6066 goto fail;
6067 else
6069 /* C1 is the character before D, S1 is the syntax of C1, C2
6070 is the character at D, and S2 is the syntax of C2. */
6071 re_wchar_t c1, c2;
6072 int s1, s2;
6073 #ifdef emacs
6074 ssize_t offset = PTR_TO_OFFSET (d) - 1;
6075 ssize_t charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
6076 UPDATE_SYNTAX_TABLE_FAST (charpos);
6077 #endif
6078 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
6079 s1 = SYNTAX (c1);
6081 /* Case 2: S1 is neither Ssymbol nor Sword. */
6082 if (s1 != Sword && s1 != Ssymbol)
6083 goto fail;
6085 /* Case 3: D is not at the end of string ... */
6086 if (!AT_STRINGS_END (d))
6088 PREFETCH_NOLIMIT ();
6089 c2 = RE_STRING_CHAR (d, target_multibyte);
6090 #ifdef emacs
6091 UPDATE_SYNTAX_TABLE_FORWARD_FAST (charpos + 1);
6092 #endif
6093 s2 = SYNTAX (c2);
6095 /* ... and S2 is Sword or Ssymbol. */
6096 if (s2 == Sword || s2 == Ssymbol)
6097 goto fail;
6100 break;
6102 case syntaxspec:
6103 case notsyntaxspec:
6105 boolean not = (re_opcode_t) *(p - 1) == notsyntaxspec;
6106 mcnt = *p++;
6107 DEBUG_PRINT ("EXECUTING %ssyntaxspec %d.\n", not ? "not" : "",
6108 mcnt);
6109 PREFETCH ();
6110 #ifdef emacs
6112 ssize_t offset = PTR_TO_OFFSET (d);
6113 ssize_t pos1 = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
6114 UPDATE_SYNTAX_TABLE_FAST (pos1);
6116 #endif
6118 int len;
6119 re_wchar_t c;
6121 GET_CHAR_AFTER (c, d, len);
6122 if ((SYNTAX (c) != (enum syntaxcode) mcnt) ^ not)
6123 goto fail;
6124 d += len;
6127 break;
6129 #ifdef emacs
6130 case at_dot:
6131 DEBUG_PRINT ("EXECUTING at_dot.\n");
6132 if (PTR_BYTE_POS (d) != PT_BYTE)
6133 goto fail;
6134 break;
6136 case categoryspec:
6137 case notcategoryspec:
6139 boolean not = (re_opcode_t) *(p - 1) == notcategoryspec;
6140 mcnt = *p++;
6141 DEBUG_PRINT ("EXECUTING %scategoryspec %d.\n",
6142 not ? "not" : "", mcnt);
6143 PREFETCH ();
6146 int len;
6147 re_wchar_t c;
6148 GET_CHAR_AFTER (c, d, len);
6149 if ((!CHAR_HAS_CATEGORY (c, mcnt)) ^ not)
6150 goto fail;
6151 d += len;
6154 break;
6156 #endif /* emacs */
6158 default:
6159 abort ();
6161 continue; /* Successfully executed one pattern command; keep going. */
6164 /* We goto here if a matching operation fails. */
6165 fail:
6166 IMMEDIATE_QUIT_CHECK;
6167 if (!FAIL_STACK_EMPTY ())
6169 re_char *str, *pat;
6170 /* A restart point is known. Restore to that state. */
6171 DEBUG_PRINT ("\nFAIL:\n");
6172 POP_FAILURE_POINT (str, pat);
6173 switch (*pat++)
6175 case on_failure_keep_string_jump:
6176 assert (str == NULL);
6177 goto continue_failure_jump;
6179 case on_failure_jump_nastyloop:
6180 assert ((re_opcode_t)pat[-2] == no_op);
6181 PUSH_FAILURE_POINT (pat - 2, str);
6182 /* Fallthrough */
6184 case on_failure_jump_loop:
6185 case on_failure_jump:
6186 case succeed_n:
6187 d = str;
6188 continue_failure_jump:
6189 EXTRACT_NUMBER_AND_INCR (mcnt, pat);
6190 p = pat + mcnt;
6191 break;
6193 case no_op:
6194 /* A special frame used for nastyloops. */
6195 goto fail;
6197 default:
6198 abort ();
6201 assert (p >= bufp->buffer && p <= pend);
6203 if (d >= string1 && d <= end1)
6204 dend = end_match_1;
6206 else
6207 break; /* Matching at this starting point really fails. */
6208 } /* for (;;) */
6210 if (best_regs_set)
6211 goto restore_best_regs;
6213 FREE_VARIABLES ();
6215 return -1; /* Failure to match. */
6218 /* Subroutine definitions for re_match_2. */
6220 /* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
6221 bytes; nonzero otherwise. */
6223 static int
6224 bcmp_translate (const_re_char *s1, const_re_char *s2, register ssize_t len,
6225 RE_TRANSLATE_TYPE translate, const int target_multibyte)
6227 register re_char *p1 = s1, *p2 = s2;
6228 re_char *p1_end = s1 + len;
6229 re_char *p2_end = s2 + len;
6231 /* FIXME: Checking both p1 and p2 presumes that the two strings might have
6232 different lengths, but relying on a single `len' would break this. -sm */
6233 while (p1 < p1_end && p2 < p2_end)
6235 int p1_charlen, p2_charlen;
6236 re_wchar_t p1_ch, p2_ch;
6238 GET_CHAR_AFTER (p1_ch, p1, p1_charlen);
6239 GET_CHAR_AFTER (p2_ch, p2, p2_charlen);
6241 if (RE_TRANSLATE (translate, p1_ch)
6242 != RE_TRANSLATE (translate, p2_ch))
6243 return 1;
6245 p1 += p1_charlen, p2 += p2_charlen;
6248 if (p1 != p1_end || p2 != p2_end)
6249 return 1;
6251 return 0;
6254 /* Entry points for GNU code. */
6256 /* re_compile_pattern is the GNU regular expression compiler: it
6257 compiles PATTERN (of length SIZE) and puts the result in BUFP.
6258 Returns 0 if the pattern was valid, otherwise an error string.
6260 Assumes the `allocated' (and perhaps `buffer') and `translate' fields
6261 are set in BUFP on entry.
6263 We call regex_compile to do the actual compilation. */
6265 const char *
6266 re_compile_pattern (const char *pattern, size_t length,
6267 #ifdef emacs
6268 bool posix_backtracking, const char *whitespace_regexp,
6269 #endif
6270 struct re_pattern_buffer *bufp)
6272 reg_errcode_t ret;
6274 /* GNU code is written to assume at least RE_NREGS registers will be set
6275 (and at least one extra will be -1). */
6276 bufp->regs_allocated = REGS_UNALLOCATED;
6278 /* And GNU code determines whether or not to get register information
6279 by passing null for the REGS argument to re_match, etc., not by
6280 setting no_sub. */
6281 bufp->no_sub = 0;
6283 ret = regex_compile ((re_char*) pattern, length,
6284 #ifdef emacs
6285 posix_backtracking,
6286 whitespace_regexp,
6287 #else
6288 re_syntax_options,
6289 #endif
6290 bufp);
6292 if (!ret)
6293 return NULL;
6294 return gettext (re_error_msgid[(int) ret]);
6296 WEAK_ALIAS (__re_compile_pattern, re_compile_pattern)
6298 /* Entry points compatible with 4.2 BSD regex library. We don't define
6299 them unless specifically requested. */
6301 #if defined _REGEX_RE_COMP || defined _LIBC
6303 /* BSD has one and only one pattern buffer. */
6304 static struct re_pattern_buffer re_comp_buf;
6306 char *
6307 # ifdef _LIBC
6308 /* Make these definitions weak in libc, so POSIX programs can redefine
6309 these names if they don't use our functions, and still use
6310 regcomp/regexec below without link errors. */
6311 weak_function
6312 # endif
6313 re_comp (const char *s)
6315 reg_errcode_t ret;
6317 if (!s)
6319 if (!re_comp_buf.buffer)
6320 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6321 return (char *) gettext ("No previous regular expression");
6322 return 0;
6325 if (!re_comp_buf.buffer)
6327 re_comp_buf.buffer = malloc (200);
6328 if (re_comp_buf.buffer == NULL)
6329 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6330 return (char *) gettext (re_error_msgid[(int) REG_ESPACE]);
6331 re_comp_buf.allocated = 200;
6333 re_comp_buf.fastmap = malloc (1 << BYTEWIDTH);
6334 if (re_comp_buf.fastmap == NULL)
6335 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6336 return (char *) gettext (re_error_msgid[(int) REG_ESPACE]);
6339 /* Since `re_exec' always passes NULL for the `regs' argument, we
6340 don't need to initialize the pattern buffer fields which affect it. */
6342 ret = regex_compile (s, strlen (s), re_syntax_options, &re_comp_buf);
6344 if (!ret)
6345 return NULL;
6347 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6348 return (char *) gettext (re_error_msgid[(int) ret]);
6353 # ifdef _LIBC
6354 weak_function
6355 # endif
6356 re_exec (const char *s)
6358 const size_t len = strlen (s);
6359 return re_search (&re_comp_buf, s, len, 0, len, 0) >= 0;
6361 #endif /* _REGEX_RE_COMP */
6363 /* POSIX.2 functions. Don't define these for Emacs. */
6365 #ifndef emacs
6367 /* regcomp takes a regular expression as a string and compiles it.
6369 PREG is a regex_t *. We do not expect any fields to be initialized,
6370 since POSIX says we shouldn't. Thus, we set
6372 `buffer' to the compiled pattern;
6373 `used' to the length of the compiled pattern;
6374 `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
6375 REG_EXTENDED bit in CFLAGS is set; otherwise, to
6376 RE_SYNTAX_POSIX_BASIC;
6377 `fastmap' to an allocated space for the fastmap;
6378 `fastmap_accurate' to zero;
6379 `re_nsub' to the number of subexpressions in PATTERN.
6381 PATTERN is the address of the pattern string.
6383 CFLAGS is a series of bits which affect compilation.
6385 If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
6386 use POSIX basic syntax.
6388 If REG_NEWLINE is set, then . and [^...] don't match newline.
6389 Also, regexec will try a match beginning after every newline.
6391 If REG_ICASE is set, then we considers upper- and lowercase
6392 versions of letters to be equivalent when matching.
6394 If REG_NOSUB is set, then when PREG is passed to regexec, that
6395 routine will report only success or failure, and nothing about the
6396 registers.
6398 It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
6399 the return codes and their meanings.) */
6401 reg_errcode_t
6402 regcomp (regex_t *_Restrict_ preg, const char *_Restrict_ pattern,
6403 int cflags)
6405 reg_errcode_t ret;
6406 reg_syntax_t syntax
6407 = (cflags & REG_EXTENDED) ?
6408 RE_SYNTAX_POSIX_EXTENDED : RE_SYNTAX_POSIX_BASIC;
6410 /* regex_compile will allocate the space for the compiled pattern. */
6411 preg->buffer = 0;
6412 preg->allocated = 0;
6413 preg->used = 0;
6415 /* Try to allocate space for the fastmap. */
6416 preg->fastmap = malloc (1 << BYTEWIDTH);
6418 if (cflags & REG_ICASE)
6420 unsigned i;
6422 preg->translate = malloc (CHAR_SET_SIZE * sizeof *preg->translate);
6423 if (preg->translate == NULL)
6424 return (int) REG_ESPACE;
6426 /* Map uppercase characters to corresponding lowercase ones. */
6427 for (i = 0; i < CHAR_SET_SIZE; i++)
6428 preg->translate[i] = ISUPPER (i) ? TOLOWER (i) : i;
6430 else
6431 preg->translate = NULL;
6433 /* If REG_NEWLINE is set, newlines are treated differently. */
6434 if (cflags & REG_NEWLINE)
6435 { /* REG_NEWLINE implies neither . nor [^...] match newline. */
6436 syntax &= ~RE_DOT_NEWLINE;
6437 syntax |= RE_HAT_LISTS_NOT_NEWLINE;
6439 else
6440 syntax |= RE_NO_NEWLINE_ANCHOR;
6442 preg->no_sub = !!(cflags & REG_NOSUB);
6444 /* POSIX says a null character in the pattern terminates it, so we
6445 can use strlen here in compiling the pattern. */
6446 ret = regex_compile ((re_char*) pattern, strlen (pattern), syntax, preg);
6448 /* POSIX doesn't distinguish between an unmatched open-group and an
6449 unmatched close-group: both are REG_EPAREN. */
6450 if (ret == REG_ERPAREN)
6451 ret = REG_EPAREN;
6453 if (ret == REG_NOERROR && preg->fastmap)
6454 { /* Compute the fastmap now, since regexec cannot modify the pattern
6455 buffer. */
6456 re_compile_fastmap (preg);
6457 if (preg->can_be_null)
6458 { /* The fastmap can't be used anyway. */
6459 free (preg->fastmap);
6460 preg->fastmap = NULL;
6463 return ret;
6465 WEAK_ALIAS (__regcomp, regcomp)
6468 /* regexec searches for a given pattern, specified by PREG, in the
6469 string STRING.
6471 If NMATCH is zero or REG_NOSUB was set in the cflags argument to
6472 `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
6473 least NMATCH elements, and we set them to the offsets of the
6474 corresponding matched substrings.
6476 EFLAGS specifies `execution flags' which affect matching: if
6477 REG_NOTBOL is set, then ^ does not match at the beginning of the
6478 string; if REG_NOTEOL is set, then $ does not match at the end.
6480 We return 0 if we find a match and REG_NOMATCH if not. */
6482 reg_errcode_t
6483 regexec (const regex_t *_Restrict_ preg, const char *_Restrict_ string,
6484 size_t nmatch, regmatch_t pmatch[_Restrict_arr_], int eflags)
6486 regoff_t ret;
6487 struct re_registers regs;
6488 regex_t private_preg;
6489 size_t len = strlen (string);
6490 boolean want_reg_info = !preg->no_sub && nmatch > 0 && pmatch;
6492 private_preg = *preg;
6494 private_preg.not_bol = !!(eflags & REG_NOTBOL);
6495 private_preg.not_eol = !!(eflags & REG_NOTEOL);
6497 /* The user has told us exactly how many registers to return
6498 information about, via `nmatch'. We have to pass that on to the
6499 matching routines. */
6500 private_preg.regs_allocated = REGS_FIXED;
6502 if (want_reg_info)
6504 regs.num_regs = nmatch;
6505 regs.start = TALLOC (nmatch * 2, regoff_t);
6506 if (regs.start == NULL)
6507 return REG_NOMATCH;
6508 regs.end = regs.start + nmatch;
6511 /* Instead of using not_eol to implement REG_NOTEOL, we could simply
6512 pass (&private_preg, string, len + 1, 0, len, ...) pretending the string
6513 was a little bit longer but still only matching the real part.
6514 This works because the `endline' will check for a '\n' and will find a
6515 '\0', correctly deciding that this is not the end of a line.
6516 But it doesn't work out so nicely for REG_NOTBOL, since we don't have
6517 a convenient '\0' there. For all we know, the string could be preceded
6518 by '\n' which would throw things off. */
6520 /* Perform the searching operation. */
6521 ret = re_search (&private_preg, string, len,
6522 /* start: */ 0, /* range: */ len,
6523 want_reg_info ? &regs : 0);
6525 /* Copy the register information to the POSIX structure. */
6526 if (want_reg_info)
6528 if (ret >= 0)
6530 unsigned r;
6532 for (r = 0; r < nmatch; r++)
6534 pmatch[r].rm_so = regs.start[r];
6535 pmatch[r].rm_eo = regs.end[r];
6539 /* If we needed the temporary register info, free the space now. */
6540 free (regs.start);
6543 /* We want zero return to mean success, unlike `re_search'. */
6544 return ret >= 0 ? REG_NOERROR : REG_NOMATCH;
6546 WEAK_ALIAS (__regexec, regexec)
6549 /* Returns a message corresponding to an error code, ERR_CODE, returned
6550 from either regcomp or regexec. We don't use PREG here.
6552 ERR_CODE was previously called ERRCODE, but that name causes an
6553 error with msvc8 compiler. */
6555 size_t
6556 regerror (int err_code, const regex_t *preg, char *errbuf, size_t errbuf_size)
6558 const char *msg;
6559 size_t msg_size;
6561 if (err_code < 0
6562 || err_code >= (sizeof (re_error_msgid) / sizeof (re_error_msgid[0])))
6563 /* Only error codes returned by the rest of the code should be passed
6564 to this routine. If we are given anything else, or if other regex
6565 code generates an invalid error code, then the program has a bug.
6566 Dump core so we can fix it. */
6567 abort ();
6569 msg = gettext (re_error_msgid[err_code]);
6571 msg_size = strlen (msg) + 1; /* Includes the null. */
6573 if (errbuf_size != 0)
6575 if (msg_size > errbuf_size)
6577 memcpy (errbuf, msg, errbuf_size - 1);
6578 errbuf[errbuf_size - 1] = 0;
6580 else
6581 strcpy (errbuf, msg);
6584 return msg_size;
6586 WEAK_ALIAS (__regerror, regerror)
6589 /* Free dynamically allocated space used by PREG. */
6591 void
6592 regfree (regex_t *preg)
6594 free (preg->buffer);
6595 preg->buffer = NULL;
6597 preg->allocated = 0;
6598 preg->used = 0;
6600 free (preg->fastmap);
6601 preg->fastmap = NULL;
6602 preg->fastmap_accurate = 0;
6604 free (preg->translate);
6605 preg->translate = NULL;
6607 WEAK_ALIAS (__regfree, regfree)
6609 #endif /* not emacs */