Fix open-network-stream responsiveness
[emacs.git] / src / regex.c
blobf92bcb7923b3dbbb7440cd70d56fca85d39032c2
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>
54 #ifdef emacs
55 /* We need this for `regex.h', and perhaps for the Emacs include files. */
56 # include <sys/types.h>
57 #endif
59 /* Whether to use ISO C Amendment 1 wide char functions.
60 Those should not be used for Emacs since it uses its own. */
61 #if defined _LIBC
62 #define WIDE_CHAR_SUPPORT 1
63 #else
64 #define WIDE_CHAR_SUPPORT \
65 (HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_BTOWC && !emacs)
66 #endif
68 /* For platform which support the ISO C amendment 1 functionality we
69 support user defined character classes. */
70 #if WIDE_CHAR_SUPPORT
71 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
72 # include <wchar.h>
73 # include <wctype.h>
74 #endif
76 #ifdef _LIBC
77 /* We have to keep the namespace clean. */
78 # define regfree(preg) __regfree (preg)
79 # define regexec(pr, st, nm, pm, ef) __regexec (pr, st, nm, pm, ef)
80 # define regcomp(preg, pattern, cflags) __regcomp (preg, pattern, cflags)
81 # define regerror(err_code, preg, errbuf, errbuf_size) \
82 __regerror (err_code, preg, errbuf, errbuf_size)
83 # define re_set_registers(bu, re, nu, st, en) \
84 __re_set_registers (bu, re, nu, st, en)
85 # define re_match_2(bufp, string1, size1, string2, size2, pos, regs, stop) \
86 __re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
87 # define re_match(bufp, string, size, pos, regs) \
88 __re_match (bufp, string, size, pos, regs)
89 # define re_search(bufp, string, size, startpos, range, regs) \
90 __re_search (bufp, string, size, startpos, range, regs)
91 # define re_compile_pattern(pattern, length, bufp) \
92 __re_compile_pattern (pattern, length, bufp)
93 # define re_set_syntax(syntax) __re_set_syntax (syntax)
94 # define re_search_2(bufp, st1, s1, st2, s2, startpos, range, regs, stop) \
95 __re_search_2 (bufp, st1, s1, st2, s2, startpos, range, regs, stop)
96 # define re_compile_fastmap(bufp) __re_compile_fastmap (bufp)
98 /* Make sure we call libc's function even if the user overrides them. */
99 # define btowc __btowc
100 # define iswctype __iswctype
101 # define wctype __wctype
103 # define WEAK_ALIAS(a,b) weak_alias (a, b)
105 /* We are also using some library internals. */
106 # include <locale/localeinfo.h>
107 # include <locale/elem-hash.h>
108 # include <langinfo.h>
109 #else
110 # define WEAK_ALIAS(a,b)
111 #endif
113 /* This is for other GNU distributions with internationalized messages. */
114 #if HAVE_LIBINTL_H || defined _LIBC
115 # include <libintl.h>
116 #else
117 # define gettext(msgid) (msgid)
118 #endif
120 #ifndef gettext_noop
121 /* This define is so xgettext can find the internationalizable
122 strings. */
123 # define gettext_noop(String) String
124 #endif
126 /* The `emacs' switch turns on certain matching commands
127 that make sense only in Emacs. */
128 #ifdef emacs
130 # include "lisp.h"
131 # include "character.h"
132 # include "buffer.h"
134 # include "syntax.h"
135 # include "category.h"
137 /* Make syntax table lookup grant data in gl_state. */
138 # define SYNTAX(c) syntax_property (c, 1)
140 # ifdef malloc
141 # undef malloc
142 # endif
143 # define malloc xmalloc
144 # ifdef realloc
145 # undef realloc
146 # endif
147 # define realloc xrealloc
148 # ifdef free
149 # undef free
150 # endif
151 # define free xfree
153 /* Converts the pointer to the char to BEG-based offset from the start. */
154 # define PTR_TO_OFFSET(d) POS_AS_IN_BUFFER (POINTER_TO_OFFSET (d))
155 # define POS_AS_IN_BUFFER(p) ((p) + (NILP (re_match_object) || BUFFERP (re_match_object)))
157 # define RE_MULTIBYTE_P(bufp) ((bufp)->multibyte)
158 # define RE_TARGET_MULTIBYTE_P(bufp) ((bufp)->target_multibyte)
159 # define RE_STRING_CHAR(p, multibyte) \
160 (multibyte ? (STRING_CHAR (p)) : (*(p)))
161 # define RE_STRING_CHAR_AND_LENGTH(p, len, multibyte) \
162 (multibyte ? (STRING_CHAR_AND_LENGTH (p, len)) : ((len) = 1, *(p)))
164 # define RE_CHAR_TO_MULTIBYTE(c) UNIBYTE_TO_CHAR (c)
166 # define RE_CHAR_TO_UNIBYTE(c) CHAR_TO_BYTE_SAFE (c)
168 /* Set C a (possibly converted to multibyte) character before P. P
169 points into a string which is the virtual concatenation of STR1
170 (which ends at END1) or STR2 (which ends at END2). */
171 # define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2) \
172 do { \
173 if (target_multibyte) \
175 re_char *dtemp = (p) == (str2) ? (end1) : (p); \
176 re_char *dlimit = ((p) > (str2) && (p) <= (end2)) ? (str2) : (str1); \
177 while (dtemp-- > dlimit && !CHAR_HEAD_P (*dtemp)); \
178 c = STRING_CHAR (dtemp); \
180 else \
182 (c = ((p) == (str2) ? (end1) : (p))[-1]); \
183 (c) = RE_CHAR_TO_MULTIBYTE (c); \
185 } while (0)
187 /* Set C a (possibly converted to multibyte) character at P, and set
188 LEN to the byte length of that character. */
189 # define GET_CHAR_AFTER(c, p, len) \
190 do { \
191 if (target_multibyte) \
192 (c) = STRING_CHAR_AND_LENGTH (p, len); \
193 else \
195 (c) = *p; \
196 len = 1; \
197 (c) = RE_CHAR_TO_MULTIBYTE (c); \
199 } while (0)
201 #else /* not emacs */
203 /* If we are not linking with Emacs proper,
204 we can't use the relocating allocator
205 even if config.h says that we can. */
206 # undef REL_ALLOC
208 # include <unistd.h>
210 /* When used in Emacs's lib-src, we need xmalloc and xrealloc. */
212 static void *
213 xmalloc (size_t size)
215 void *val = malloc (size);
216 if (!val && size)
218 write (STDERR_FILENO, "virtual memory exhausted\n", 25);
219 exit (1);
221 return val;
224 static void *
225 xrealloc (void *block, size_t size)
227 void *val;
228 /* We must call malloc explicitly when BLOCK is 0, since some
229 reallocs don't do this. */
230 if (! block)
231 val = malloc (size);
232 else
233 val = realloc (block, size);
234 if (!val && size)
236 write (STDERR_FILENO, "virtual memory exhausted\n", 25);
237 exit (1);
239 return val;
242 # ifdef malloc
243 # undef malloc
244 # endif
245 # define malloc xmalloc
246 # ifdef realloc
247 # undef realloc
248 # endif
249 # define realloc xrealloc
251 # include <stdbool.h>
252 # include <string.h>
254 /* Define the syntax stuff for \<, \>, etc. */
256 /* Sword must be nonzero for the wordchar pattern commands in re_match_2. */
257 enum syntaxcode { Swhitespace = 0, Sword = 1, Ssymbol = 2 };
259 /* Dummy macros for non-Emacs environments. */
260 # define MAX_MULTIBYTE_LENGTH 1
261 # define RE_MULTIBYTE_P(x) 0
262 # define RE_TARGET_MULTIBYTE_P(x) 0
263 # define WORD_BOUNDARY_P(c1, c2) (0)
264 # define BYTES_BY_CHAR_HEAD(p) (1)
265 # define PREV_CHAR_BOUNDARY(p, limit) ((p)--)
266 # define STRING_CHAR(p) (*(p))
267 # define RE_STRING_CHAR(p, multibyte) STRING_CHAR (p)
268 # define CHAR_STRING(c, s) (*(s) = (c), 1)
269 # define STRING_CHAR_AND_LENGTH(p, actual_len) ((actual_len) = 1, *(p))
270 # define RE_STRING_CHAR_AND_LENGTH(p, len, multibyte) STRING_CHAR_AND_LENGTH (p, len)
271 # define RE_CHAR_TO_MULTIBYTE(c) (c)
272 # define RE_CHAR_TO_UNIBYTE(c) (c)
273 # define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2) \
274 (c = ((p) == (str2) ? *((end1) - 1) : *((p) - 1)))
275 # define GET_CHAR_AFTER(c, p, len) \
276 (c = *p, len = 1)
277 # define CHAR_BYTE8_P(c) (0)
278 # define CHAR_LEADING_CODE(c) (c)
280 #endif /* not emacs */
282 #ifndef RE_TRANSLATE
283 # define RE_TRANSLATE(TBL, C) ((unsigned char)(TBL)[C])
284 # define RE_TRANSLATE_P(TBL) (TBL)
285 #endif
287 /* Get the interface, including the syntax bits. */
288 #include "regex.h"
290 /* isalpha etc. are used for the character classes. */
291 #include <ctype.h>
293 #ifdef emacs
295 /* 1 if C is an ASCII character. */
296 # define IS_REAL_ASCII(c) ((c) < 0200)
298 /* 1 if C is a unibyte character. */
299 # define ISUNIBYTE(c) (SINGLE_BYTE_CHAR_P ((c)))
301 /* The Emacs definitions should not be directly affected by locales. */
303 /* In Emacs, these are only used for single-byte characters. */
304 # define ISDIGIT(c) ((c) >= '0' && (c) <= '9')
305 # define ISCNTRL(c) ((c) < ' ')
306 # define ISXDIGIT(c) (((c) >= '0' && (c) <= '9') \
307 || ((c) >= 'a' && (c) <= 'f') \
308 || ((c) >= 'A' && (c) <= 'F'))
310 /* This is only used for single-byte characters. */
311 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
313 /* The rest must handle multibyte characters. */
315 # define ISGRAPH(c) (SINGLE_BYTE_CHAR_P (c) \
316 ? (c) > ' ' && !((c) >= 0177 && (c) <= 0240) \
317 : graphicp (c))
319 # define ISPRINT(c) (SINGLE_BYTE_CHAR_P (c) \
320 ? (c) >= ' ' && !((c) >= 0177 && (c) <= 0237) \
321 : printablep (c))
323 # define ISALNUM(c) (IS_REAL_ASCII (c) \
324 ? (((c) >= 'a' && (c) <= 'z') \
325 || ((c) >= 'A' && (c) <= 'Z') \
326 || ((c) >= '0' && (c) <= '9')) \
327 : (alphabeticp (c) || decimalnump (c)))
329 # define ISALPHA(c) (IS_REAL_ASCII (c) \
330 ? (((c) >= 'a' && (c) <= 'z') \
331 || ((c) >= 'A' && (c) <= 'Z')) \
332 : alphabeticp (c))
334 # define ISLOWER(c) lowercasep (c)
336 # define ISPUNCT(c) (IS_REAL_ASCII (c) \
337 ? ((c) > ' ' && (c) < 0177 \
338 && !(((c) >= 'a' && (c) <= 'z') \
339 || ((c) >= 'A' && (c) <= 'Z') \
340 || ((c) >= '0' && (c) <= '9'))) \
341 : SYNTAX (c) != Sword)
343 # define ISSPACE(c) (SYNTAX (c) == Swhitespace)
345 # define ISUPPER(c) uppercasep (c)
347 # define ISWORD(c) (SYNTAX (c) == Sword)
349 #else /* not emacs */
351 /* 1 if C is an ASCII character. */
352 # define IS_REAL_ASCII(c) ((c) < 0200)
354 /* This distinction is not meaningful, except in Emacs. */
355 # define ISUNIBYTE(c) 1
357 # ifdef isblank
358 # define ISBLANK(c) isblank (c)
359 # else
360 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
361 # endif
362 # ifdef isgraph
363 # define ISGRAPH(c) isgraph (c)
364 # else
365 # define ISGRAPH(c) (isprint (c) && !isspace (c))
366 # endif
368 /* Solaris defines ISPRINT so we must undefine it first. */
369 # undef ISPRINT
370 # define ISPRINT(c) isprint (c)
371 # define ISDIGIT(c) isdigit (c)
372 # define ISALNUM(c) isalnum (c)
373 # define ISALPHA(c) isalpha (c)
374 # define ISCNTRL(c) iscntrl (c)
375 # define ISLOWER(c) islower (c)
376 # define ISPUNCT(c) ispunct (c)
377 # define ISSPACE(c) isspace (c)
378 # define ISUPPER(c) isupper (c)
379 # define ISXDIGIT(c) isxdigit (c)
381 # define ISWORD(c) ISALPHA (c)
383 # ifdef _tolower
384 # define TOLOWER(c) _tolower (c)
385 # else
386 # define TOLOWER(c) tolower (c)
387 # endif
389 /* How many characters in the character set. */
390 # define CHAR_SET_SIZE 256
392 # ifdef SYNTAX_TABLE
394 extern char *re_syntax_table;
396 # else /* not SYNTAX_TABLE */
398 static char re_syntax_table[CHAR_SET_SIZE];
400 static void
401 init_syntax_once (void)
403 register int c;
404 static int done = 0;
406 if (done)
407 return;
409 memset (re_syntax_table, 0, sizeof re_syntax_table);
411 for (c = 0; c < CHAR_SET_SIZE; ++c)
412 if (ISALNUM (c))
413 re_syntax_table[c] = Sword;
415 re_syntax_table['_'] = Ssymbol;
417 done = 1;
420 # endif /* not SYNTAX_TABLE */
422 # define SYNTAX(c) re_syntax_table[(c)]
424 #endif /* not emacs */
426 #define SIGN_EXTEND_CHAR(c) ((signed char) (c))
428 /* Should we use malloc or alloca? If REGEX_MALLOC is not defined, we
429 use `alloca' instead of `malloc'. This is because using malloc in
430 re_search* or re_match* could cause memory leaks when C-g is used in
431 Emacs; also, malloc is slower and causes storage fragmentation. On
432 the other hand, malloc is more portable, and easier to debug.
434 Because we sometimes use alloca, some routines have to be macros,
435 not functions -- `alloca'-allocated space disappears at the end of the
436 function it is called in. */
438 #ifdef REGEX_MALLOC
440 # define REGEX_ALLOCATE malloc
441 # define REGEX_REALLOCATE(source, osize, nsize) realloc (source, nsize)
442 # define REGEX_FREE free
444 #else /* not REGEX_MALLOC */
446 /* Emacs already defines alloca, sometimes. */
447 # ifndef alloca
449 /* Make alloca work the best possible way. */
450 # ifdef __GNUC__
451 # define alloca __builtin_alloca
452 # else /* not __GNUC__ */
453 # ifdef HAVE_ALLOCA_H
454 # include <alloca.h>
455 # endif /* HAVE_ALLOCA_H */
456 # endif /* not __GNUC__ */
458 # endif /* not alloca */
460 # ifdef emacs
461 # define REGEX_USE_SAFE_ALLOCA USE_SAFE_ALLOCA
462 # define REGEX_SAFE_FREE() SAFE_FREE ()
463 # define REGEX_ALLOCATE SAFE_ALLOCA
464 # else
465 # define REGEX_ALLOCATE alloca
466 # endif
468 /* Assumes a `char *destination' variable. */
469 # define REGEX_REALLOCATE(source, osize, nsize) \
470 (destination = REGEX_ALLOCATE (nsize), \
471 memcpy (destination, source, osize))
473 /* No need to do anything to free, after alloca. */
474 # define REGEX_FREE(arg) ((void)0) /* Do nothing! But inhibit gcc warning. */
476 #endif /* not REGEX_MALLOC */
478 #ifndef REGEX_USE_SAFE_ALLOCA
479 # define REGEX_USE_SAFE_ALLOCA ((void) 0)
480 # define REGEX_SAFE_FREE() ((void) 0)
481 #endif
483 /* Define how to allocate the failure stack. */
485 #if defined REL_ALLOC && defined REGEX_MALLOC
487 # define REGEX_ALLOCATE_STACK(size) \
488 r_alloc (&failure_stack_ptr, (size))
489 # define REGEX_REALLOCATE_STACK(source, osize, nsize) \
490 r_re_alloc (&failure_stack_ptr, (nsize))
491 # define REGEX_FREE_STACK(ptr) \
492 r_alloc_free (&failure_stack_ptr)
494 #else /* not using relocating allocator */
496 # define REGEX_ALLOCATE_STACK(size) REGEX_ALLOCATE (size)
497 # define REGEX_REALLOCATE_STACK(source, o, n) REGEX_REALLOCATE (source, o, n)
498 # define REGEX_FREE_STACK(ptr) REGEX_FREE (ptr)
500 #endif /* not using relocating allocator */
503 /* True if `size1' is non-NULL and PTR is pointing anywhere inside
504 `string1' or just past its end. This works if PTR is NULL, which is
505 a good thing. */
506 #define FIRST_STRING_P(ptr) \
507 (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
509 /* (Re)Allocate N items of type T using malloc, or fail. */
510 #define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t)))
511 #define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
512 #define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
514 #define BYTEWIDTH 8 /* In bits. */
516 #define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
518 #ifndef emacs
519 # undef max
520 # undef min
521 # define max(a, b) ((a) > (b) ? (a) : (b))
522 # define min(a, b) ((a) < (b) ? (a) : (b))
523 #endif
525 /* Type of source-pattern and string chars. */
526 #ifdef _MSC_VER
527 typedef unsigned char re_char;
528 typedef const re_char const_re_char;
529 #else
530 typedef const unsigned char re_char;
531 typedef re_char const_re_char;
532 #endif
534 typedef char boolean;
536 static regoff_t re_match_2_internal (struct re_pattern_buffer *bufp,
537 re_char *string1, size_t size1,
538 re_char *string2, size_t size2,
539 ssize_t pos,
540 struct re_registers *regs,
541 ssize_t stop);
543 /* These are the command codes that appear in compiled regular
544 expressions. Some opcodes are followed by argument bytes. A
545 command code can specify any interpretation whatsoever for its
546 arguments. Zero bytes may appear in the compiled regular expression. */
548 typedef enum
550 no_op = 0,
552 /* Succeed right away--no more backtracking. */
553 succeed,
555 /* Followed by one byte giving n, then by n literal bytes. */
556 exactn,
558 /* Matches any (more or less) character. */
559 anychar,
561 /* Matches any one char belonging to specified set. First
562 following byte is number of bitmap bytes. Then come bytes
563 for a bitmap saying which chars are in. Bits in each byte
564 are ordered low-bit-first. A character is in the set if its
565 bit is 1. A character too large to have a bit in the map is
566 automatically not in the set.
568 If the length byte has the 0x80 bit set, then that stuff
569 is followed by a range table:
570 2 bytes of flags for character sets (low 8 bits, high 8 bits)
571 See RANGE_TABLE_WORK_BITS below.
572 2 bytes, the number of pairs that follow (upto 32767)
573 pairs, each 2 multibyte characters,
574 each multibyte character represented as 3 bytes. */
575 charset,
577 /* Same parameters as charset, but match any character that is
578 not one of those specified. */
579 charset_not,
581 /* Start remembering the text that is matched, for storing in a
582 register. Followed by one byte with the register number, in
583 the range 0 to one less than the pattern buffer's re_nsub
584 field. */
585 start_memory,
587 /* Stop remembering the text that is matched and store it in a
588 memory register. Followed by one byte with the register
589 number, in the range 0 to one less than `re_nsub' in the
590 pattern buffer. */
591 stop_memory,
593 /* Match a duplicate of something remembered. Followed by one
594 byte containing the register number. */
595 duplicate,
597 /* Fail unless at beginning of line. */
598 begline,
600 /* Fail unless at end of line. */
601 endline,
603 /* Succeeds if at beginning of buffer (if emacs) or at beginning
604 of string to be matched (if not). */
605 begbuf,
607 /* Analogously, for end of buffer/string. */
608 endbuf,
610 /* Followed by two byte relative address to which to jump. */
611 jump,
613 /* Followed by two-byte relative address of place to resume at
614 in case of failure. */
615 on_failure_jump,
617 /* Like on_failure_jump, but pushes a placeholder instead of the
618 current string position when executed. */
619 on_failure_keep_string_jump,
621 /* Just like `on_failure_jump', except that it checks that we
622 don't get stuck in an infinite loop (matching an empty string
623 indefinitely). */
624 on_failure_jump_loop,
626 /* Just like `on_failure_jump_loop', except that it checks for
627 a different kind of loop (the kind that shows up with non-greedy
628 operators). This operation has to be immediately preceded
629 by a `no_op'. */
630 on_failure_jump_nastyloop,
632 /* A smart `on_failure_jump' used for greedy * and + operators.
633 It analyzes the loop before which it is put and if the
634 loop does not require backtracking, it changes itself to
635 `on_failure_keep_string_jump' and short-circuits the loop,
636 else it just defaults to changing itself into `on_failure_jump'.
637 It assumes that it is pointing to just past a `jump'. */
638 on_failure_jump_smart,
640 /* Followed by two-byte relative address and two-byte number n.
641 After matching N times, jump to the address upon failure.
642 Does not work if N starts at 0: use on_failure_jump_loop
643 instead. */
644 succeed_n,
646 /* Followed by two-byte relative address, and two-byte number n.
647 Jump to the address N times, then fail. */
648 jump_n,
650 /* Set the following two-byte relative address to the
651 subsequent two-byte number. The address *includes* the two
652 bytes of number. */
653 set_number_at,
655 wordbeg, /* Succeeds if at word beginning. */
656 wordend, /* Succeeds if at word end. */
658 wordbound, /* Succeeds if at a word boundary. */
659 notwordbound, /* Succeeds if not at a word boundary. */
661 symbeg, /* Succeeds if at symbol beginning. */
662 symend, /* Succeeds if at symbol end. */
664 /* Matches any character whose syntax is specified. Followed by
665 a byte which contains a syntax code, e.g., Sword. */
666 syntaxspec,
668 /* Matches any character whose syntax is not that specified. */
669 notsyntaxspec
671 #ifdef emacs
672 ,before_dot, /* Succeeds if before point. */
673 at_dot, /* Succeeds if at point. */
674 after_dot, /* Succeeds if after point. */
676 /* Matches any character whose category-set contains the specified
677 category. The operator is followed by a byte which contains a
678 category code (mnemonic ASCII character). */
679 categoryspec,
681 /* Matches any character whose category-set does not contain the
682 specified category. The operator is followed by a byte which
683 contains the category code (mnemonic ASCII character). */
684 notcategoryspec
685 #endif /* emacs */
686 } re_opcode_t;
688 /* Common operations on the compiled pattern. */
690 /* Store NUMBER in two contiguous bytes starting at DESTINATION. */
692 #define STORE_NUMBER(destination, number) \
693 do { \
694 (destination)[0] = (number) & 0377; \
695 (destination)[1] = (number) >> 8; \
696 } while (0)
698 /* Same as STORE_NUMBER, except increment DESTINATION to
699 the byte after where the number is stored. Therefore, DESTINATION
700 must be an lvalue. */
702 #define STORE_NUMBER_AND_INCR(destination, number) \
703 do { \
704 STORE_NUMBER (destination, number); \
705 (destination) += 2; \
706 } while (0)
708 /* Put into DESTINATION a number stored in two contiguous bytes starting
709 at SOURCE. */
711 #define EXTRACT_NUMBER(destination, source) \
712 ((destination) = extract_number (source))
714 static int
715 extract_number (re_char *source)
717 unsigned leading_byte = SIGN_EXTEND_CHAR (source[1]);
718 return (leading_byte << 8) + source[0];
721 /* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
722 SOURCE must be an lvalue. */
724 #define EXTRACT_NUMBER_AND_INCR(destination, source) \
725 ((destination) = extract_number_and_incr (&source))
727 static int
728 extract_number_and_incr (re_char **source)
730 int num = extract_number (*source);
731 *source += 2;
732 return num;
735 /* Store a multibyte character in three contiguous bytes starting
736 DESTINATION, and increment DESTINATION to the byte after where the
737 character is stored. Therefore, DESTINATION must be an lvalue. */
739 #define STORE_CHARACTER_AND_INCR(destination, character) \
740 do { \
741 (destination)[0] = (character) & 0377; \
742 (destination)[1] = ((character) >> 8) & 0377; \
743 (destination)[2] = (character) >> 16; \
744 (destination) += 3; \
745 } while (0)
747 /* Put into DESTINATION a character stored in three contiguous bytes
748 starting at SOURCE. */
750 #define EXTRACT_CHARACTER(destination, source) \
751 do { \
752 (destination) = ((source)[0] \
753 | ((source)[1] << 8) \
754 | ((source)[2] << 16)); \
755 } while (0)
758 /* Macros for charset. */
760 /* Size of bitmap of charset P in bytes. P is a start of charset,
761 i.e. *P is (re_opcode_t) charset or (re_opcode_t) charset_not. */
762 #define CHARSET_BITMAP_SIZE(p) ((p)[1] & 0x7F)
764 /* Nonzero if charset P has range table. */
765 #define CHARSET_RANGE_TABLE_EXISTS_P(p) ((p)[1] & 0x80)
767 /* Return the address of range table of charset P. But not the start
768 of table itself, but the before where the number of ranges is
769 stored. `2 +' means to skip re_opcode_t and size of bitmap,
770 and the 2 bytes of flags at the start of the range table. */
771 #define CHARSET_RANGE_TABLE(p) (&(p)[4 + CHARSET_BITMAP_SIZE (p)])
773 #ifdef emacs
774 /* Extract the bit flags that start a range table. */
775 #define CHARSET_RANGE_TABLE_BITS(p) \
776 ((p)[2 + CHARSET_BITMAP_SIZE (p)] \
777 + (p)[3 + CHARSET_BITMAP_SIZE (p)] * 0x100)
778 #endif
780 /* Return the address of end of RANGE_TABLE. COUNT is number of
781 ranges (which is a pair of (start, end)) in the RANGE_TABLE. `* 2'
782 is start of range and end of range. `* 3' is size of each start
783 and end. */
784 #define CHARSET_RANGE_TABLE_END(range_table, count) \
785 ((range_table) + (count) * 2 * 3)
787 /* Test if C is in RANGE_TABLE. A flag NOT is negated if C is in.
788 COUNT is number of ranges in RANGE_TABLE. */
789 #define CHARSET_LOOKUP_RANGE_TABLE_RAW(not, c, range_table, count) \
790 do \
792 re_wchar_t range_start, range_end; \
793 re_char *rtp; \
794 re_char *range_table_end \
795 = CHARSET_RANGE_TABLE_END ((range_table), (count)); \
797 for (rtp = (range_table); rtp < range_table_end; rtp += 2 * 3) \
799 EXTRACT_CHARACTER (range_start, rtp); \
800 EXTRACT_CHARACTER (range_end, rtp + 3); \
802 if (range_start <= (c) && (c) <= range_end) \
804 (not) = !(not); \
805 break; \
809 while (0)
811 /* Test if C is in range table of CHARSET. The flag NOT is negated if
812 C is listed in it. */
813 #define CHARSET_LOOKUP_RANGE_TABLE(not, c, charset) \
814 do \
816 /* Number of ranges in range table. */ \
817 int count; \
818 re_char *range_table = CHARSET_RANGE_TABLE (charset); \
820 EXTRACT_NUMBER_AND_INCR (count, range_table); \
821 CHARSET_LOOKUP_RANGE_TABLE_RAW ((not), (c), range_table, count); \
823 while (0)
825 /* If DEBUG is defined, Regex prints many voluminous messages about what
826 it is doing (if the variable `debug' is nonzero). If linked with the
827 main program in `iregex.c', you can enter patterns and strings
828 interactively. And if linked with the main program in `main.c' and
829 the other test files, you can run the already-written tests. */
831 #ifdef DEBUG
833 /* We use standard I/O for debugging. */
834 # include <stdio.h>
836 /* It is useful to test things that ``must'' be true when debugging. */
837 # include <assert.h>
839 static int debug = -100000;
841 # define DEBUG_STATEMENT(e) e
842 # define DEBUG_PRINT(...) if (debug > 0) printf (__VA_ARGS__)
843 # define DEBUG_COMPILES_ARGUMENTS
844 # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) \
845 if (debug > 0) print_partial_compiled_pattern (s, e)
846 # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \
847 if (debug > 0) print_double_string (w, s1, sz1, s2, sz2)
850 /* Print the fastmap in human-readable form. */
852 static void
853 print_fastmap (char *fastmap)
855 unsigned was_a_range = 0;
856 unsigned i = 0;
858 while (i < (1 << BYTEWIDTH))
860 if (fastmap[i++])
862 was_a_range = 0;
863 putchar (i - 1);
864 while (i < (1 << BYTEWIDTH) && fastmap[i])
866 was_a_range = 1;
867 i++;
869 if (was_a_range)
871 printf ("-");
872 putchar (i - 1);
876 putchar ('\n');
880 /* Print a compiled pattern string in human-readable form, starting at
881 the START pointer into it and ending just before the pointer END. */
883 static void
884 print_partial_compiled_pattern (re_char *start, re_char *end)
886 int mcnt, mcnt2;
887 re_char *p = start;
888 re_char *pend = end;
890 if (start == NULL)
892 fprintf (stderr, "(null)\n");
893 return;
896 /* Loop over pattern commands. */
897 while (p < pend)
899 fprintf (stderr, "%td:\t", p - start);
901 switch ((re_opcode_t) *p++)
903 case no_op:
904 fprintf (stderr, "/no_op");
905 break;
907 case succeed:
908 fprintf (stderr, "/succeed");
909 break;
911 case exactn:
912 mcnt = *p++;
913 fprintf (stderr, "/exactn/%d", mcnt);
916 fprintf (stderr, "/%c", *p++);
918 while (--mcnt);
919 break;
921 case start_memory:
922 fprintf (stderr, "/start_memory/%d", *p++);
923 break;
925 case stop_memory:
926 fprintf (stderr, "/stop_memory/%d", *p++);
927 break;
929 case duplicate:
930 fprintf (stderr, "/duplicate/%d", *p++);
931 break;
933 case anychar:
934 fprintf (stderr, "/anychar");
935 break;
937 case charset:
938 case charset_not:
940 register int c, last = -100;
941 register int in_range = 0;
942 int length = CHARSET_BITMAP_SIZE (p - 1);
943 int has_range_table = CHARSET_RANGE_TABLE_EXISTS_P (p - 1);
945 fprintf (stderr, "/charset [%s",
946 (re_opcode_t) *(p - 1) == charset_not ? "^" : "");
948 if (p + *p >= pend)
949 fprintf (stderr, " !extends past end of pattern! ");
951 for (c = 0; c < 256; c++)
952 if (c / 8 < length
953 && (p[1 + (c/8)] & (1 << (c % 8))))
955 /* Are we starting a range? */
956 if (last + 1 == c && ! in_range)
958 fprintf (stderr, "-");
959 in_range = 1;
961 /* Have we broken a range? */
962 else if (last + 1 != c && in_range)
964 fprintf (stderr, "%c", last);
965 in_range = 0;
968 if (! in_range)
969 fprintf (stderr, "%c", c);
971 last = c;
974 if (in_range)
975 fprintf (stderr, "%c", last);
977 fprintf (stderr, "]");
979 p += 1 + length;
981 if (has_range_table)
983 int count;
984 fprintf (stderr, "has-range-table");
986 /* ??? Should print the range table; for now, just skip it. */
987 p += 2; /* skip range table bits */
988 EXTRACT_NUMBER_AND_INCR (count, p);
989 p = CHARSET_RANGE_TABLE_END (p, count);
992 break;
994 case begline:
995 fprintf (stderr, "/begline");
996 break;
998 case endline:
999 fprintf (stderr, "/endline");
1000 break;
1002 case on_failure_jump:
1003 EXTRACT_NUMBER_AND_INCR (mcnt, p);
1004 fprintf (stderr, "/on_failure_jump to %td", p + mcnt - start);
1005 break;
1007 case on_failure_keep_string_jump:
1008 EXTRACT_NUMBER_AND_INCR (mcnt, p);
1009 fprintf (stderr, "/on_failure_keep_string_jump to %td",
1010 p + mcnt - start);
1011 break;
1013 case on_failure_jump_nastyloop:
1014 EXTRACT_NUMBER_AND_INCR (mcnt, p);
1015 fprintf (stderr, "/on_failure_jump_nastyloop to %td",
1016 p + mcnt - start);
1017 break;
1019 case on_failure_jump_loop:
1020 EXTRACT_NUMBER_AND_INCR (mcnt, p);
1021 fprintf (stderr, "/on_failure_jump_loop to %td",
1022 p + mcnt - start);
1023 break;
1025 case on_failure_jump_smart:
1026 EXTRACT_NUMBER_AND_INCR (mcnt, p);
1027 fprintf (stderr, "/on_failure_jump_smart to %td",
1028 p + mcnt - start);
1029 break;
1031 case jump:
1032 EXTRACT_NUMBER_AND_INCR (mcnt, p);
1033 fprintf (stderr, "/jump to %td", p + mcnt - start);
1034 break;
1036 case succeed_n:
1037 EXTRACT_NUMBER_AND_INCR (mcnt, p);
1038 EXTRACT_NUMBER_AND_INCR (mcnt2, p);
1039 fprintf (stderr, "/succeed_n to %td, %d times",
1040 p - 2 + mcnt - start, mcnt2);
1041 break;
1043 case jump_n:
1044 EXTRACT_NUMBER_AND_INCR (mcnt, p);
1045 EXTRACT_NUMBER_AND_INCR (mcnt2, p);
1046 fprintf (stderr, "/jump_n to %td, %d times",
1047 p - 2 + mcnt - start, mcnt2);
1048 break;
1050 case set_number_at:
1051 EXTRACT_NUMBER_AND_INCR (mcnt, p);
1052 EXTRACT_NUMBER_AND_INCR (mcnt2, p);
1053 fprintf (stderr, "/set_number_at location %td to %d",
1054 p - 2 + mcnt - start, mcnt2);
1055 break;
1057 case wordbound:
1058 fprintf (stderr, "/wordbound");
1059 break;
1061 case notwordbound:
1062 fprintf (stderr, "/notwordbound");
1063 break;
1065 case wordbeg:
1066 fprintf (stderr, "/wordbeg");
1067 break;
1069 case wordend:
1070 fprintf (stderr, "/wordend");
1071 break;
1073 case symbeg:
1074 fprintf (stderr, "/symbeg");
1075 break;
1077 case symend:
1078 fprintf (stderr, "/symend");
1079 break;
1081 case syntaxspec:
1082 fprintf (stderr, "/syntaxspec");
1083 mcnt = *p++;
1084 fprintf (stderr, "/%d", mcnt);
1085 break;
1087 case notsyntaxspec:
1088 fprintf (stderr, "/notsyntaxspec");
1089 mcnt = *p++;
1090 fprintf (stderr, "/%d", mcnt);
1091 break;
1093 # ifdef emacs
1094 case before_dot:
1095 fprintf (stderr, "/before_dot");
1096 break;
1098 case at_dot:
1099 fprintf (stderr, "/at_dot");
1100 break;
1102 case after_dot:
1103 fprintf (stderr, "/after_dot");
1104 break;
1106 case categoryspec:
1107 fprintf (stderr, "/categoryspec");
1108 mcnt = *p++;
1109 fprintf (stderr, "/%d", mcnt);
1110 break;
1112 case notcategoryspec:
1113 fprintf (stderr, "/notcategoryspec");
1114 mcnt = *p++;
1115 fprintf (stderr, "/%d", mcnt);
1116 break;
1117 # endif /* emacs */
1119 case begbuf:
1120 fprintf (stderr, "/begbuf");
1121 break;
1123 case endbuf:
1124 fprintf (stderr, "/endbuf");
1125 break;
1127 default:
1128 fprintf (stderr, "?%d", *(p-1));
1131 fprintf (stderr, "\n");
1134 fprintf (stderr, "%td:\tend of pattern.\n", p - start);
1138 static void
1139 print_compiled_pattern (struct re_pattern_buffer *bufp)
1141 re_char *buffer = bufp->buffer;
1143 print_partial_compiled_pattern (buffer, buffer + bufp->used);
1144 printf ("%ld bytes used/%ld bytes allocated.\n",
1145 bufp->used, bufp->allocated);
1147 if (bufp->fastmap_accurate && bufp->fastmap)
1149 printf ("fastmap: ");
1150 print_fastmap (bufp->fastmap);
1153 printf ("re_nsub: %zu\t", bufp->re_nsub);
1154 printf ("regs_alloc: %d\t", bufp->regs_allocated);
1155 printf ("can_be_null: %d\t", bufp->can_be_null);
1156 printf ("no_sub: %d\t", bufp->no_sub);
1157 printf ("not_bol: %d\t", bufp->not_bol);
1158 printf ("not_eol: %d\t", bufp->not_eol);
1159 printf ("syntax: %lx\n", bufp->syntax);
1160 fflush (stdout);
1161 /* Perhaps we should print the translate table? */
1165 static void
1166 print_double_string (re_char *where, re_char *string1, ssize_t size1,
1167 re_char *string2, ssize_t size2)
1169 ssize_t this_char;
1171 if (where == NULL)
1172 printf ("(null)");
1173 else
1175 if (FIRST_STRING_P (where))
1177 for (this_char = where - string1; this_char < size1; this_char++)
1178 putchar (string1[this_char]);
1180 where = string2;
1183 for (this_char = where - string2; this_char < size2; this_char++)
1184 putchar (string2[this_char]);
1188 #else /* not DEBUG */
1190 # undef assert
1191 # define assert(e)
1193 # define DEBUG_STATEMENT(e)
1194 # define DEBUG_PRINT(...)
1195 # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
1196 # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
1198 #endif /* not DEBUG */
1200 /* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
1201 also be assigned to arbitrarily: each pattern buffer stores its own
1202 syntax, so it can be changed between regex compilations. */
1203 /* This has no initializer because initialized variables in Emacs
1204 become read-only after dumping. */
1205 reg_syntax_t re_syntax_options;
1208 /* Specify the precise syntax of regexps for compilation. This provides
1209 for compatibility for various utilities which historically have
1210 different, incompatible syntaxes.
1212 The argument SYNTAX is a bit mask comprised of the various bits
1213 defined in regex.h. We return the old syntax. */
1215 reg_syntax_t
1216 re_set_syntax (reg_syntax_t syntax)
1218 reg_syntax_t ret = re_syntax_options;
1220 re_syntax_options = syntax;
1221 return ret;
1223 WEAK_ALIAS (__re_set_syntax, re_set_syntax)
1225 /* Regexp to use to replace spaces, or NULL meaning don't. */
1226 static const_re_char *whitespace_regexp;
1228 void
1229 re_set_whitespace_regexp (const char *regexp)
1231 whitespace_regexp = (const_re_char *) regexp;
1233 WEAK_ALIAS (__re_set_syntax, re_set_syntax)
1235 /* This table gives an error message for each of the error codes listed
1236 in regex.h. Obviously the order here has to be same as there.
1237 POSIX doesn't require that we do anything for REG_NOERROR,
1238 but why not be nice? */
1240 static const char *re_error_msgid[] =
1242 gettext_noop ("Success"), /* REG_NOERROR */
1243 gettext_noop ("No match"), /* REG_NOMATCH */
1244 gettext_noop ("Invalid regular expression"), /* REG_BADPAT */
1245 gettext_noop ("Invalid collation character"), /* REG_ECOLLATE */
1246 gettext_noop ("Invalid character class name"), /* REG_ECTYPE */
1247 gettext_noop ("Trailing backslash"), /* REG_EESCAPE */
1248 gettext_noop ("Invalid back reference"), /* REG_ESUBREG */
1249 gettext_noop ("Unmatched [ or [^"), /* REG_EBRACK */
1250 gettext_noop ("Unmatched ( or \\("), /* REG_EPAREN */
1251 gettext_noop ("Unmatched \\{"), /* REG_EBRACE */
1252 gettext_noop ("Invalid content of \\{\\}"), /* REG_BADBR */
1253 gettext_noop ("Invalid range end"), /* REG_ERANGE */
1254 gettext_noop ("Memory exhausted"), /* REG_ESPACE */
1255 gettext_noop ("Invalid preceding regular expression"), /* REG_BADRPT */
1256 gettext_noop ("Premature end of regular expression"), /* REG_EEND */
1257 gettext_noop ("Regular expression too big"), /* REG_ESIZE */
1258 gettext_noop ("Unmatched ) or \\)"), /* REG_ERPAREN */
1259 gettext_noop ("Range striding over charsets") /* REG_ERANGEX */
1262 /* Avoiding alloca during matching, to placate r_alloc. */
1264 /* Define MATCH_MAY_ALLOCATE unless we need to make sure that the
1265 searching and matching functions should not call alloca. On some
1266 systems, alloca is implemented in terms of malloc, and if we're
1267 using the relocating allocator routines, then malloc could cause a
1268 relocation, which might (if the strings being searched are in the
1269 ralloc heap) shift the data out from underneath the regexp
1270 routines.
1272 Here's another reason to avoid allocation: Emacs
1273 processes input from X in a signal handler; processing X input may
1274 call malloc; if input arrives while a matching routine is calling
1275 malloc, then we're scrod. But Emacs can't just block input while
1276 calling matching routines; then we don't notice interrupts when
1277 they come in. So, Emacs blocks input around all regexp calls
1278 except the matching calls, which it leaves unprotected, in the
1279 faith that they will not malloc. */
1281 /* Normally, this is fine. */
1282 #define MATCH_MAY_ALLOCATE
1284 /* The match routines may not allocate if (1) they would do it with malloc
1285 and (2) it's not safe for them to use malloc.
1286 Note that if REL_ALLOC is defined, matching would not use malloc for the
1287 failure stack, but we would still use it for the register vectors;
1288 so REL_ALLOC should not affect this. */
1289 #if defined REGEX_MALLOC && defined emacs
1290 # undef MATCH_MAY_ALLOCATE
1291 #endif
1294 /* Failure stack declarations and macros; both re_compile_fastmap and
1295 re_match_2 use a failure stack. These have to be macros because of
1296 REGEX_ALLOCATE_STACK. */
1299 /* Approximate number of failure points for which to initially allocate space
1300 when matching. If this number is exceeded, we allocate more
1301 space, so it is not a hard limit. */
1302 #ifndef INIT_FAILURE_ALLOC
1303 # define INIT_FAILURE_ALLOC 20
1304 #endif
1306 /* Roughly the maximum number of failure points on the stack. Would be
1307 exactly that if always used TYPICAL_FAILURE_SIZE items each time we failed.
1308 This is a variable only so users of regex can assign to it; we never
1309 change it ourselves. We always multiply it by TYPICAL_FAILURE_SIZE
1310 before using it, so it should probably be a byte-count instead. */
1311 # if defined MATCH_MAY_ALLOCATE
1312 /* Note that 4400 was enough to cause a crash on Alpha OSF/1,
1313 whose default stack limit is 2mb. In order for a larger
1314 value to work reliably, you have to try to make it accord
1315 with the process stack limit. */
1316 size_t re_max_failures = 40000;
1317 # else
1318 size_t re_max_failures = 4000;
1319 # endif
1321 union fail_stack_elt
1323 re_char *pointer;
1324 /* This should be the biggest `int' that's no bigger than a pointer. */
1325 long integer;
1328 typedef union fail_stack_elt fail_stack_elt_t;
1330 typedef struct
1332 fail_stack_elt_t *stack;
1333 size_t size;
1334 size_t avail; /* Offset of next open position. */
1335 size_t frame; /* Offset of the cur constructed frame. */
1336 } fail_stack_type;
1338 #define FAIL_STACK_EMPTY() (fail_stack.frame == 0)
1341 /* Define macros to initialize and free the failure stack.
1342 Do `return -2' if the alloc fails. */
1344 #ifdef MATCH_MAY_ALLOCATE
1345 # define INIT_FAIL_STACK() \
1346 do { \
1347 fail_stack.stack = \
1348 REGEX_ALLOCATE_STACK (INIT_FAILURE_ALLOC * TYPICAL_FAILURE_SIZE \
1349 * sizeof (fail_stack_elt_t)); \
1351 if (fail_stack.stack == NULL) \
1352 return -2; \
1354 fail_stack.size = INIT_FAILURE_ALLOC; \
1355 fail_stack.avail = 0; \
1356 fail_stack.frame = 0; \
1357 } while (0)
1358 #else
1359 # define INIT_FAIL_STACK() \
1360 do { \
1361 fail_stack.avail = 0; \
1362 fail_stack.frame = 0; \
1363 } while (0)
1365 # define RETALLOC_IF(addr, n, t) \
1366 if (addr) RETALLOC((addr), (n), t); else (addr) = TALLOC ((n), t)
1367 #endif
1370 /* Double the size of FAIL_STACK, up to a limit
1371 which allows approximately `re_max_failures' items.
1373 Return 1 if succeeds, and 0 if either ran out of memory
1374 allocating space for it or it was already too large.
1376 REGEX_REALLOCATE_STACK requires `destination' be declared. */
1378 /* Factor to increase the failure stack size by
1379 when we increase it.
1380 This used to be 2, but 2 was too wasteful
1381 because the old discarded stacks added up to as much space
1382 were as ultimate, maximum-size stack. */
1383 #define FAIL_STACK_GROWTH_FACTOR 4
1385 #define GROW_FAIL_STACK(fail_stack) \
1386 (((fail_stack).size * sizeof (fail_stack_elt_t) \
1387 >= re_max_failures * TYPICAL_FAILURE_SIZE) \
1388 ? 0 \
1389 : ((fail_stack).stack \
1390 = REGEX_REALLOCATE_STACK ((fail_stack).stack, \
1391 (fail_stack).size * sizeof (fail_stack_elt_t), \
1392 min (re_max_failures * TYPICAL_FAILURE_SIZE, \
1393 ((fail_stack).size * sizeof (fail_stack_elt_t) \
1394 * FAIL_STACK_GROWTH_FACTOR))), \
1396 (fail_stack).stack == NULL \
1397 ? 0 \
1398 : ((fail_stack).size \
1399 = (min (re_max_failures * TYPICAL_FAILURE_SIZE, \
1400 ((fail_stack).size * sizeof (fail_stack_elt_t) \
1401 * FAIL_STACK_GROWTH_FACTOR)) \
1402 / sizeof (fail_stack_elt_t)), \
1403 1)))
1406 /* Push a pointer value onto the failure stack.
1407 Assumes the variable `fail_stack'. Probably should only
1408 be called from within `PUSH_FAILURE_POINT'. */
1409 #define PUSH_FAILURE_POINTER(item) \
1410 fail_stack.stack[fail_stack.avail++].pointer = (item)
1412 /* This pushes an integer-valued item onto the failure stack.
1413 Assumes the variable `fail_stack'. Probably should only
1414 be called from within `PUSH_FAILURE_POINT'. */
1415 #define PUSH_FAILURE_INT(item) \
1416 fail_stack.stack[fail_stack.avail++].integer = (item)
1418 /* These POP... operations complement the PUSH... operations.
1419 All assume that `fail_stack' is nonempty. */
1420 #define POP_FAILURE_POINTER() fail_stack.stack[--fail_stack.avail].pointer
1421 #define POP_FAILURE_INT() fail_stack.stack[--fail_stack.avail].integer
1423 /* Individual items aside from the registers. */
1424 #define NUM_NONREG_ITEMS 3
1426 /* Used to examine the stack (to detect infinite loops). */
1427 #define FAILURE_PAT(h) fail_stack.stack[(h) - 1].pointer
1428 #define FAILURE_STR(h) (fail_stack.stack[(h) - 2].pointer)
1429 #define NEXT_FAILURE_HANDLE(h) fail_stack.stack[(h) - 3].integer
1430 #define TOP_FAILURE_HANDLE() fail_stack.frame
1433 #define ENSURE_FAIL_STACK(space) \
1434 while (REMAINING_AVAIL_SLOTS <= space) { \
1435 if (!GROW_FAIL_STACK (fail_stack)) \
1436 return -2; \
1437 DEBUG_PRINT ("\n Doubled stack; size now: %zd\n", (fail_stack).size);\
1438 DEBUG_PRINT (" slots available: %zd\n", REMAINING_AVAIL_SLOTS);\
1441 /* Push register NUM onto the stack. */
1442 #define PUSH_FAILURE_REG(num) \
1443 do { \
1444 char *destination; \
1445 long n = num; \
1446 ENSURE_FAIL_STACK(3); \
1447 DEBUG_PRINT (" Push reg %ld (spanning %p -> %p)\n", \
1448 n, regstart[n], regend[n]); \
1449 PUSH_FAILURE_POINTER (regstart[n]); \
1450 PUSH_FAILURE_POINTER (regend[n]); \
1451 PUSH_FAILURE_INT (n); \
1452 } while (0)
1454 /* Change the counter's value to VAL, but make sure that it will
1455 be reset when backtracking. */
1456 #define PUSH_NUMBER(ptr,val) \
1457 do { \
1458 char *destination; \
1459 int c; \
1460 ENSURE_FAIL_STACK(3); \
1461 EXTRACT_NUMBER (c, ptr); \
1462 DEBUG_PRINT (" Push number %p = %d -> %d\n", ptr, c, val); \
1463 PUSH_FAILURE_INT (c); \
1464 PUSH_FAILURE_POINTER (ptr); \
1465 PUSH_FAILURE_INT (-1); \
1466 STORE_NUMBER (ptr, val); \
1467 } while (0)
1469 /* Pop a saved register off the stack. */
1470 #define POP_FAILURE_REG_OR_COUNT() \
1471 do { \
1472 long pfreg = POP_FAILURE_INT (); \
1473 if (pfreg == -1) \
1475 /* It's a counter. */ \
1476 /* Here, we discard `const', making re_match non-reentrant. */ \
1477 unsigned char *ptr = (unsigned char*) POP_FAILURE_POINTER (); \
1478 pfreg = POP_FAILURE_INT (); \
1479 STORE_NUMBER (ptr, pfreg); \
1480 DEBUG_PRINT (" Pop counter %p = %ld\n", ptr, pfreg); \
1482 else \
1484 regend[pfreg] = POP_FAILURE_POINTER (); \
1485 regstart[pfreg] = POP_FAILURE_POINTER (); \
1486 DEBUG_PRINT (" Pop reg %ld (spanning %p -> %p)\n", \
1487 pfreg, regstart[pfreg], regend[pfreg]); \
1489 } while (0)
1491 /* Check that we are not stuck in an infinite loop. */
1492 #define CHECK_INFINITE_LOOP(pat_cur, string_place) \
1493 do { \
1494 ssize_t failure = TOP_FAILURE_HANDLE (); \
1495 /* Check for infinite matching loops */ \
1496 while (failure > 0 \
1497 && (FAILURE_STR (failure) == string_place \
1498 || FAILURE_STR (failure) == NULL)) \
1500 assert (FAILURE_PAT (failure) >= bufp->buffer \
1501 && FAILURE_PAT (failure) <= bufp->buffer + bufp->used); \
1502 if (FAILURE_PAT (failure) == pat_cur) \
1504 cycle = 1; \
1505 break; \
1507 DEBUG_PRINT (" Other pattern: %p\n", FAILURE_PAT (failure)); \
1508 failure = NEXT_FAILURE_HANDLE(failure); \
1510 DEBUG_PRINT (" Other string: %p\n", FAILURE_STR (failure)); \
1511 } while (0)
1513 /* Push the information about the state we will need
1514 if we ever fail back to it.
1516 Requires variables fail_stack, regstart, regend and
1517 num_regs be declared. GROW_FAIL_STACK requires `destination' be
1518 declared.
1520 Does `return FAILURE_CODE' if runs out of memory. */
1522 #define PUSH_FAILURE_POINT(pattern, string_place) \
1523 do { \
1524 char *destination; \
1525 /* Must be int, so when we don't save any registers, the arithmetic \
1526 of 0 + -1 isn't done as unsigned. */ \
1528 DEBUG_STATEMENT (nfailure_points_pushed++); \
1529 DEBUG_PRINT ("\nPUSH_FAILURE_POINT:\n"); \
1530 DEBUG_PRINT (" Before push, next avail: %zd\n", (fail_stack).avail); \
1531 DEBUG_PRINT (" size: %zd\n", (fail_stack).size);\
1533 ENSURE_FAIL_STACK (NUM_NONREG_ITEMS); \
1535 DEBUG_PRINT ("\n"); \
1537 DEBUG_PRINT (" Push frame index: %zd\n", fail_stack.frame); \
1538 PUSH_FAILURE_INT (fail_stack.frame); \
1540 DEBUG_PRINT (" Push string %p: \"", string_place); \
1541 DEBUG_PRINT_DOUBLE_STRING (string_place, string1, size1, string2, size2);\
1542 DEBUG_PRINT ("\"\n"); \
1543 PUSH_FAILURE_POINTER (string_place); \
1545 DEBUG_PRINT (" Push pattern %p: ", pattern); \
1546 DEBUG_PRINT_COMPILED_PATTERN (bufp, pattern, pend); \
1547 PUSH_FAILURE_POINTER (pattern); \
1549 /* Close the frame by moving the frame pointer past it. */ \
1550 fail_stack.frame = fail_stack.avail; \
1551 } while (0)
1553 /* Estimate the size of data pushed by a typical failure stack entry.
1554 An estimate is all we need, because all we use this for
1555 is to choose a limit for how big to make the failure stack. */
1556 /* BEWARE, the value `20' is hard-coded in emacs.c:main(). */
1557 #define TYPICAL_FAILURE_SIZE 20
1559 /* How many items can still be added to the stack without overflowing it. */
1560 #define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail)
1563 /* Pops what PUSH_FAIL_STACK pushes.
1565 We restore into the parameters, all of which should be lvalues:
1566 STR -- the saved data position.
1567 PAT -- the saved pattern position.
1568 REGSTART, REGEND -- arrays of string positions.
1570 Also assumes the variables `fail_stack' and (if debugging), `bufp',
1571 `pend', `string1', `size1', `string2', and `size2'. */
1573 #define POP_FAILURE_POINT(str, pat) \
1574 do { \
1575 assert (!FAIL_STACK_EMPTY ()); \
1577 /* Remove failure points and point to how many regs pushed. */ \
1578 DEBUG_PRINT ("POP_FAILURE_POINT:\n"); \
1579 DEBUG_PRINT (" Before pop, next avail: %zd\n", fail_stack.avail); \
1580 DEBUG_PRINT (" size: %zd\n", fail_stack.size); \
1582 /* Pop the saved registers. */ \
1583 while (fail_stack.frame < fail_stack.avail) \
1584 POP_FAILURE_REG_OR_COUNT (); \
1586 pat = POP_FAILURE_POINTER (); \
1587 DEBUG_PRINT (" Popping pattern %p: ", pat); \
1588 DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend); \
1590 /* If the saved string location is NULL, it came from an \
1591 on_failure_keep_string_jump opcode, and we want to throw away the \
1592 saved NULL, thus retaining our current position in the string. */ \
1593 str = POP_FAILURE_POINTER (); \
1594 DEBUG_PRINT (" Popping string %p: \"", str); \
1595 DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2); \
1596 DEBUG_PRINT ("\"\n"); \
1598 fail_stack.frame = POP_FAILURE_INT (); \
1599 DEBUG_PRINT (" Popping frame index: %zd\n", fail_stack.frame); \
1601 assert (fail_stack.avail >= 0); \
1602 assert (fail_stack.frame <= fail_stack.avail); \
1604 DEBUG_STATEMENT (nfailure_points_popped++); \
1605 } while (0) /* POP_FAILURE_POINT */
1609 /* Registers are set to a sentinel when they haven't yet matched. */
1610 #define REG_UNSET(e) ((e) == NULL)
1612 /* Subroutine declarations and macros for regex_compile. */
1614 static reg_errcode_t regex_compile (re_char *pattern, size_t size,
1615 reg_syntax_t syntax,
1616 struct re_pattern_buffer *bufp);
1617 static void store_op1 (re_opcode_t op, unsigned char *loc, int arg);
1618 static void store_op2 (re_opcode_t op, unsigned char *loc, int arg1, int arg2);
1619 static void insert_op1 (re_opcode_t op, unsigned char *loc,
1620 int arg, unsigned char *end);
1621 static void insert_op2 (re_opcode_t op, unsigned char *loc,
1622 int arg1, int arg2, unsigned char *end);
1623 static boolean at_begline_loc_p (re_char *pattern, re_char *p,
1624 reg_syntax_t syntax);
1625 static boolean at_endline_loc_p (re_char *p, re_char *pend,
1626 reg_syntax_t syntax);
1627 static re_char *skip_one_char (re_char *p);
1628 static int analyze_first (re_char *p, re_char *pend,
1629 char *fastmap, const int multibyte);
1631 /* Fetch the next character in the uncompiled pattern, with no
1632 translation. */
1633 #define PATFETCH(c) \
1634 do { \
1635 int len; \
1636 if (p == pend) return REG_EEND; \
1637 c = RE_STRING_CHAR_AND_LENGTH (p, len, multibyte); \
1638 p += len; \
1639 } while (0)
1642 /* If `translate' is non-null, return translate[D], else just D. We
1643 cast the subscript to translate because some data is declared as
1644 `char *', to avoid warnings when a string constant is passed. But
1645 when we use a character as a subscript we must make it unsigned. */
1646 #ifndef TRANSLATE
1647 # define TRANSLATE(d) \
1648 (RE_TRANSLATE_P (translate) ? RE_TRANSLATE (translate, (d)) : (d))
1649 #endif
1652 /* Macros for outputting the compiled pattern into `buffer'. */
1654 /* If the buffer isn't allocated when it comes in, use this. */
1655 #define INIT_BUF_SIZE 32
1657 /* Make sure we have at least N more bytes of space in buffer. */
1658 #define GET_BUFFER_SPACE(n) \
1659 while ((size_t) (b - bufp->buffer + (n)) > bufp->allocated) \
1660 EXTEND_BUFFER ()
1662 /* Make sure we have one more byte of buffer space and then add C to it. */
1663 #define BUF_PUSH(c) \
1664 do { \
1665 GET_BUFFER_SPACE (1); \
1666 *b++ = (unsigned char) (c); \
1667 } while (0)
1670 /* Ensure we have two more bytes of buffer space and then append C1 and C2. */
1671 #define BUF_PUSH_2(c1, c2) \
1672 do { \
1673 GET_BUFFER_SPACE (2); \
1674 *b++ = (unsigned char) (c1); \
1675 *b++ = (unsigned char) (c2); \
1676 } while (0)
1679 /* Store a jump with opcode OP at LOC to location TO. We store a
1680 relative address offset by the three bytes the jump itself occupies. */
1681 #define STORE_JUMP(op, loc, to) \
1682 store_op1 (op, loc, (to) - (loc) - 3)
1684 /* Likewise, for a two-argument jump. */
1685 #define STORE_JUMP2(op, loc, to, arg) \
1686 store_op2 (op, loc, (to) - (loc) - 3, arg)
1688 /* Like `STORE_JUMP', but for inserting. Assume `b' is the buffer end. */
1689 #define INSERT_JUMP(op, loc, to) \
1690 insert_op1 (op, loc, (to) - (loc) - 3, b)
1692 /* Like `STORE_JUMP2', but for inserting. Assume `b' is the buffer end. */
1693 #define INSERT_JUMP2(op, loc, to, arg) \
1694 insert_op2 (op, loc, (to) - (loc) - 3, arg, b)
1697 /* This is not an arbitrary limit: the arguments which represent offsets
1698 into the pattern are two bytes long. So if 2^15 bytes turns out to
1699 be too small, many things would have to change. */
1700 # define MAX_BUF_SIZE (1L << 15)
1702 /* Extend the buffer by twice its current size via realloc and
1703 reset the pointers that pointed into the old block to point to the
1704 correct places in the new one. If extending the buffer results in it
1705 being larger than MAX_BUF_SIZE, then flag memory exhausted. */
1706 #if __BOUNDED_POINTERS__
1707 # define SET_HIGH_BOUND(P) (__ptrhigh (P) = __ptrlow (P) + bufp->allocated)
1708 # define MOVE_BUFFER_POINTER(P) \
1709 (__ptrlow (P) = new_buffer + (__ptrlow (P) - old_buffer), \
1710 SET_HIGH_BOUND (P), \
1711 __ptrvalue (P) = new_buffer + (__ptrvalue (P) - old_buffer))
1712 # define ELSE_EXTEND_BUFFER_HIGH_BOUND \
1713 else \
1715 SET_HIGH_BOUND (b); \
1716 SET_HIGH_BOUND (begalt); \
1717 if (fixup_alt_jump) \
1718 SET_HIGH_BOUND (fixup_alt_jump); \
1719 if (laststart) \
1720 SET_HIGH_BOUND (laststart); \
1721 if (pending_exact) \
1722 SET_HIGH_BOUND (pending_exact); \
1724 #else
1725 # define MOVE_BUFFER_POINTER(P) ((P) = new_buffer + ((P) - old_buffer))
1726 # define ELSE_EXTEND_BUFFER_HIGH_BOUND
1727 #endif
1728 #define EXTEND_BUFFER() \
1729 do { \
1730 unsigned char *old_buffer = bufp->buffer; \
1731 if (bufp->allocated == MAX_BUF_SIZE) \
1732 return REG_ESIZE; \
1733 bufp->allocated <<= 1; \
1734 if (bufp->allocated > MAX_BUF_SIZE) \
1735 bufp->allocated = MAX_BUF_SIZE; \
1736 RETALLOC (bufp->buffer, bufp->allocated, unsigned char); \
1737 if (bufp->buffer == NULL) \
1738 return REG_ESPACE; \
1739 /* If the buffer moved, move all the pointers into it. */ \
1740 if (old_buffer != bufp->buffer) \
1742 unsigned char *new_buffer = bufp->buffer; \
1743 MOVE_BUFFER_POINTER (b); \
1744 MOVE_BUFFER_POINTER (begalt); \
1745 if (fixup_alt_jump) \
1746 MOVE_BUFFER_POINTER (fixup_alt_jump); \
1747 if (laststart) \
1748 MOVE_BUFFER_POINTER (laststart); \
1749 if (pending_exact) \
1750 MOVE_BUFFER_POINTER (pending_exact); \
1752 ELSE_EXTEND_BUFFER_HIGH_BOUND \
1753 } while (0)
1756 /* Since we have one byte reserved for the register number argument to
1757 {start,stop}_memory, the maximum number of groups we can report
1758 things about is what fits in that byte. */
1759 #define MAX_REGNUM 255
1761 /* But patterns can have more than `MAX_REGNUM' registers. We just
1762 ignore the excess. */
1763 typedef int regnum_t;
1766 /* Macros for the compile stack. */
1768 /* Since offsets can go either forwards or backwards, this type needs to
1769 be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1. */
1770 /* int may be not enough when sizeof(int) == 2. */
1771 typedef long pattern_offset_t;
1773 typedef struct
1775 pattern_offset_t begalt_offset;
1776 pattern_offset_t fixup_alt_jump;
1777 pattern_offset_t laststart_offset;
1778 regnum_t regnum;
1779 } compile_stack_elt_t;
1782 typedef struct
1784 compile_stack_elt_t *stack;
1785 size_t size;
1786 size_t avail; /* Offset of next open position. */
1787 } compile_stack_type;
1790 #define INIT_COMPILE_STACK_SIZE 32
1792 #define COMPILE_STACK_EMPTY (compile_stack.avail == 0)
1793 #define COMPILE_STACK_FULL (compile_stack.avail == compile_stack.size)
1795 /* The next available element. */
1796 #define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
1798 /* Explicit quit checking is needed for Emacs, which uses polling to
1799 process input events. */
1800 #ifdef emacs
1801 # define IMMEDIATE_QUIT_CHECK \
1802 do { \
1803 if (immediate_quit) QUIT; \
1804 } while (0)
1805 #else
1806 # define IMMEDIATE_QUIT_CHECK ((void)0)
1807 #endif
1809 /* Structure to manage work area for range table. */
1810 struct range_table_work_area
1812 int *table; /* actual work area. */
1813 int allocated; /* allocated size for work area in bytes. */
1814 int used; /* actually used size in words. */
1815 int bits; /* flag to record character classes */
1818 #ifdef emacs
1820 /* Make sure that WORK_AREA can hold more N multibyte characters.
1821 This is used only in set_image_of_range and set_image_of_range_1.
1822 It expects WORK_AREA to be a pointer.
1823 If it can't get the space, it returns from the surrounding function. */
1825 #define EXTEND_RANGE_TABLE(work_area, n) \
1826 do { \
1827 if (((work_area).used + (n)) * sizeof (int) > (work_area).allocated) \
1829 extend_range_table_work_area (&work_area); \
1830 if ((work_area).table == 0) \
1831 return (REG_ESPACE); \
1833 } while (0)
1835 #define SET_RANGE_TABLE_WORK_AREA_BIT(work_area, bit) \
1836 (work_area).bits |= (bit)
1838 /* Set a range (RANGE_START, RANGE_END) to WORK_AREA. */
1839 #define SET_RANGE_TABLE_WORK_AREA(work_area, range_start, range_end) \
1840 do { \
1841 EXTEND_RANGE_TABLE ((work_area), 2); \
1842 (work_area).table[(work_area).used++] = (range_start); \
1843 (work_area).table[(work_area).used++] = (range_end); \
1844 } while (0)
1846 #endif /* emacs */
1848 /* Free allocated memory for WORK_AREA. */
1849 #define FREE_RANGE_TABLE_WORK_AREA(work_area) \
1850 do { \
1851 if ((work_area).table) \
1852 free ((work_area).table); \
1853 } while (0)
1855 #define CLEAR_RANGE_TABLE_WORK_USED(work_area) ((work_area).used = 0, (work_area).bits = 0)
1856 #define RANGE_TABLE_WORK_USED(work_area) ((work_area).used)
1857 #define RANGE_TABLE_WORK_BITS(work_area) ((work_area).bits)
1858 #define RANGE_TABLE_WORK_ELT(work_area, i) ((work_area).table[i])
1860 /* Bits used to implement the multibyte-part of the various character classes
1861 such as [:alnum:] in a charset's range table. The code currently assumes
1862 that only the low 16 bits are used. */
1863 #define BIT_WORD 0x1
1864 #define BIT_LOWER 0x2
1865 #define BIT_PUNCT 0x4
1866 #define BIT_SPACE 0x8
1867 #define BIT_UPPER 0x10
1868 #define BIT_MULTIBYTE 0x20
1869 #define BIT_ALPHA 0x40
1870 #define BIT_ALNUM 0x80
1871 #define BIT_GRAPH 0x100
1872 #define BIT_PRINT 0x200
1875 /* Set the bit for character C in a list. */
1876 #define SET_LIST_BIT(c) (b[((c)) / BYTEWIDTH] |= 1 << ((c) % BYTEWIDTH))
1879 #ifdef emacs
1881 /* Store characters in the range FROM to TO in the bitmap at B (for
1882 ASCII and unibyte characters) and WORK_AREA (for multibyte
1883 characters) while translating them and paying attention to the
1884 continuity of translated characters.
1886 Implementation note: It is better to implement these fairly big
1887 macros by a function, but it's not that easy because macros called
1888 in this macro assume various local variables already declared. */
1890 /* Both FROM and TO are ASCII characters. */
1892 #define SETUP_ASCII_RANGE(work_area, FROM, TO) \
1893 do { \
1894 int C0, C1; \
1896 for (C0 = (FROM); C0 <= (TO); C0++) \
1898 C1 = TRANSLATE (C0); \
1899 if (! ASCII_CHAR_P (C1)) \
1901 SET_RANGE_TABLE_WORK_AREA ((work_area), C1, C1); \
1902 if ((C1 = RE_CHAR_TO_UNIBYTE (C1)) < 0) \
1903 C1 = C0; \
1905 SET_LIST_BIT (C1); \
1907 } while (0)
1910 /* Both FROM and TO are unibyte characters (0x80..0xFF). */
1912 #define SETUP_UNIBYTE_RANGE(work_area, FROM, TO) \
1913 do { \
1914 int C0, C1, C2, I; \
1915 int USED = RANGE_TABLE_WORK_USED (work_area); \
1917 for (C0 = (FROM); C0 <= (TO); C0++) \
1919 C1 = RE_CHAR_TO_MULTIBYTE (C0); \
1920 if (CHAR_BYTE8_P (C1)) \
1921 SET_LIST_BIT (C0); \
1922 else \
1924 C2 = TRANSLATE (C1); \
1925 if (C2 == C1 \
1926 || (C1 = RE_CHAR_TO_UNIBYTE (C2)) < 0) \
1927 C1 = C0; \
1928 SET_LIST_BIT (C1); \
1929 for (I = RANGE_TABLE_WORK_USED (work_area) - 2; I >= USED; I -= 2) \
1931 int from = RANGE_TABLE_WORK_ELT (work_area, I); \
1932 int to = RANGE_TABLE_WORK_ELT (work_area, I + 1); \
1934 if (C2 >= from - 1 && C2 <= to + 1) \
1936 if (C2 == from - 1) \
1937 RANGE_TABLE_WORK_ELT (work_area, I)--; \
1938 else if (C2 == to + 1) \
1939 RANGE_TABLE_WORK_ELT (work_area, I + 1)++; \
1940 break; \
1943 if (I < USED) \
1944 SET_RANGE_TABLE_WORK_AREA ((work_area), C2, C2); \
1947 } while (0)
1950 /* Both FROM and TO are multibyte characters. */
1952 #define SETUP_MULTIBYTE_RANGE(work_area, FROM, TO) \
1953 do { \
1954 int C0, C1, C2, I, USED = RANGE_TABLE_WORK_USED (work_area); \
1956 SET_RANGE_TABLE_WORK_AREA ((work_area), (FROM), (TO)); \
1957 for (C0 = (FROM); C0 <= (TO); C0++) \
1959 C1 = TRANSLATE (C0); \
1960 if ((C2 = RE_CHAR_TO_UNIBYTE (C1)) >= 0 \
1961 || (C1 != C0 && (C2 = RE_CHAR_TO_UNIBYTE (C0)) >= 0)) \
1962 SET_LIST_BIT (C2); \
1963 if (C1 >= (FROM) && C1 <= (TO)) \
1964 continue; \
1965 for (I = RANGE_TABLE_WORK_USED (work_area) - 2; I >= USED; I -= 2) \
1967 int from = RANGE_TABLE_WORK_ELT (work_area, I); \
1968 int to = RANGE_TABLE_WORK_ELT (work_area, I + 1); \
1970 if (C1 >= from - 1 && C1 <= to + 1) \
1972 if (C1 == from - 1) \
1973 RANGE_TABLE_WORK_ELT (work_area, I)--; \
1974 else if (C1 == to + 1) \
1975 RANGE_TABLE_WORK_ELT (work_area, I + 1)++; \
1976 break; \
1979 if (I < USED) \
1980 SET_RANGE_TABLE_WORK_AREA ((work_area), C1, C1); \
1982 } while (0)
1984 #endif /* emacs */
1986 /* Get the next unsigned number in the uncompiled pattern. */
1987 #define GET_INTERVAL_COUNT(num) \
1988 do { \
1989 if (p == pend) \
1990 FREE_STACK_RETURN (REG_EBRACE); \
1991 else \
1993 PATFETCH (c); \
1994 while ('0' <= c && c <= '9') \
1996 if (num < 0) \
1997 num = 0; \
1998 if (RE_DUP_MAX / 10 - (RE_DUP_MAX % 10 < c - '0') < num) \
1999 FREE_STACK_RETURN (REG_BADBR); \
2000 num = num * 10 + c - '0'; \
2001 if (p == pend) \
2002 FREE_STACK_RETURN (REG_EBRACE); \
2003 PATFETCH (c); \
2006 } while (0)
2008 #if ! WIDE_CHAR_SUPPORT
2010 /* Map a string to the char class it names (if any). */
2011 re_wctype_t
2012 re_wctype (const_re_char *str)
2014 const char *string = (const char *) str;
2015 if (STREQ (string, "alnum")) return RECC_ALNUM;
2016 else if (STREQ (string, "alpha")) return RECC_ALPHA;
2017 else if (STREQ (string, "word")) return RECC_WORD;
2018 else if (STREQ (string, "ascii")) return RECC_ASCII;
2019 else if (STREQ (string, "nonascii")) return RECC_NONASCII;
2020 else if (STREQ (string, "graph")) return RECC_GRAPH;
2021 else if (STREQ (string, "lower")) return RECC_LOWER;
2022 else if (STREQ (string, "print")) return RECC_PRINT;
2023 else if (STREQ (string, "punct")) return RECC_PUNCT;
2024 else if (STREQ (string, "space")) return RECC_SPACE;
2025 else if (STREQ (string, "upper")) return RECC_UPPER;
2026 else if (STREQ (string, "unibyte")) return RECC_UNIBYTE;
2027 else if (STREQ (string, "multibyte")) return RECC_MULTIBYTE;
2028 else if (STREQ (string, "digit")) return RECC_DIGIT;
2029 else if (STREQ (string, "xdigit")) return RECC_XDIGIT;
2030 else if (STREQ (string, "cntrl")) return RECC_CNTRL;
2031 else if (STREQ (string, "blank")) return RECC_BLANK;
2032 else return 0;
2035 /* True if CH is in the char class CC. */
2036 boolean
2037 re_iswctype (int ch, re_wctype_t cc)
2039 switch (cc)
2041 case RECC_ALNUM: return ISALNUM (ch) != 0;
2042 case RECC_ALPHA: return ISALPHA (ch) != 0;
2043 case RECC_BLANK: return ISBLANK (ch) != 0;
2044 case RECC_CNTRL: return ISCNTRL (ch) != 0;
2045 case RECC_DIGIT: return ISDIGIT (ch) != 0;
2046 case RECC_GRAPH: return ISGRAPH (ch) != 0;
2047 case RECC_LOWER: return ISLOWER (ch) != 0;
2048 case RECC_PRINT: return ISPRINT (ch) != 0;
2049 case RECC_PUNCT: return ISPUNCT (ch) != 0;
2050 case RECC_SPACE: return ISSPACE (ch) != 0;
2051 case RECC_UPPER: return ISUPPER (ch) != 0;
2052 case RECC_XDIGIT: return ISXDIGIT (ch) != 0;
2053 case RECC_ASCII: return IS_REAL_ASCII (ch) != 0;
2054 case RECC_NONASCII: return !IS_REAL_ASCII (ch);
2055 case RECC_UNIBYTE: return ISUNIBYTE (ch) != 0;
2056 case RECC_MULTIBYTE: return !ISUNIBYTE (ch);
2057 case RECC_WORD: return ISWORD (ch) != 0;
2058 case RECC_ERROR: return false;
2059 default:
2060 abort ();
2064 /* Return a bit-pattern to use in the range-table bits to match multibyte
2065 chars of class CC. */
2066 static int
2067 re_wctype_to_bit (re_wctype_t cc)
2069 switch (cc)
2071 case RECC_NONASCII:
2072 case RECC_MULTIBYTE: return BIT_MULTIBYTE;
2073 case RECC_ALPHA: return BIT_ALPHA;
2074 case RECC_ALNUM: return BIT_ALNUM;
2075 case RECC_WORD: return BIT_WORD;
2076 case RECC_LOWER: return BIT_LOWER;
2077 case RECC_UPPER: return BIT_UPPER;
2078 case RECC_PUNCT: return BIT_PUNCT;
2079 case RECC_SPACE: return BIT_SPACE;
2080 case RECC_GRAPH: return BIT_GRAPH;
2081 case RECC_PRINT: return BIT_PRINT;
2082 case RECC_ASCII: case RECC_DIGIT: case RECC_XDIGIT: case RECC_CNTRL:
2083 case RECC_BLANK: case RECC_UNIBYTE: case RECC_ERROR: return 0;
2084 default:
2085 abort ();
2088 #endif
2090 /* Filling in the work area of a range. */
2092 /* Actually extend the space in WORK_AREA. */
2094 static void
2095 extend_range_table_work_area (struct range_table_work_area *work_area)
2097 work_area->allocated += 16 * sizeof (int);
2098 work_area->table = realloc (work_area->table, work_area->allocated);
2101 #if 0
2102 #ifdef emacs
2104 /* Carefully find the ranges of codes that are equivalent
2105 under case conversion to the range start..end when passed through
2106 TRANSLATE. Handle the case where non-letters can come in between
2107 two upper-case letters (which happens in Latin-1).
2108 Also handle the case of groups of more than 2 case-equivalent chars.
2110 The basic method is to look at consecutive characters and see
2111 if they can form a run that can be handled as one.
2113 Returns -1 if successful, REG_ESPACE if ran out of space. */
2115 static int
2116 set_image_of_range_1 (struct range_table_work_area *work_area,
2117 re_wchar_t start, re_wchar_t end,
2118 RE_TRANSLATE_TYPE translate)
2120 /* `one_case' indicates a character, or a run of characters,
2121 each of which is an isolate (no case-equivalents).
2122 This includes all ASCII non-letters.
2124 `two_case' indicates a character, or a run of characters,
2125 each of which has two case-equivalent forms.
2126 This includes all ASCII letters.
2128 `strange' indicates a character that has more than one
2129 case-equivalent. */
2131 enum case_type {one_case, two_case, strange};
2133 /* Describe the run that is in progress,
2134 which the next character can try to extend.
2135 If run_type is strange, that means there really is no run.
2136 If run_type is one_case, then run_start...run_end is the run.
2137 If run_type is two_case, then the run is run_start...run_end,
2138 and the case-equivalents end at run_eqv_end. */
2140 enum case_type run_type = strange;
2141 int run_start, run_end, run_eqv_end;
2143 Lisp_Object eqv_table;
2145 if (!RE_TRANSLATE_P (translate))
2147 EXTEND_RANGE_TABLE (work_area, 2);
2148 work_area->table[work_area->used++] = (start);
2149 work_area->table[work_area->used++] = (end);
2150 return -1;
2153 eqv_table = XCHAR_TABLE (translate)->extras[2];
2155 for (; start <= end; start++)
2157 enum case_type this_type;
2158 int eqv = RE_TRANSLATE (eqv_table, start);
2159 int minchar, maxchar;
2161 /* Classify this character */
2162 if (eqv == start)
2163 this_type = one_case;
2164 else if (RE_TRANSLATE (eqv_table, eqv) == start)
2165 this_type = two_case;
2166 else
2167 this_type = strange;
2169 if (start < eqv)
2170 minchar = start, maxchar = eqv;
2171 else
2172 minchar = eqv, maxchar = start;
2174 /* Can this character extend the run in progress? */
2175 if (this_type == strange || this_type != run_type
2176 || !(minchar == run_end + 1
2177 && (run_type == two_case
2178 ? maxchar == run_eqv_end + 1 : 1)))
2180 /* No, end the run.
2181 Record each of its equivalent ranges. */
2182 if (run_type == one_case)
2184 EXTEND_RANGE_TABLE (work_area, 2);
2185 work_area->table[work_area->used++] = run_start;
2186 work_area->table[work_area->used++] = run_end;
2188 else if (run_type == two_case)
2190 EXTEND_RANGE_TABLE (work_area, 4);
2191 work_area->table[work_area->used++] = run_start;
2192 work_area->table[work_area->used++] = run_end;
2193 work_area->table[work_area->used++]
2194 = RE_TRANSLATE (eqv_table, run_start);
2195 work_area->table[work_area->used++]
2196 = RE_TRANSLATE (eqv_table, run_end);
2198 run_type = strange;
2201 if (this_type == strange)
2203 /* For a strange character, add each of its equivalents, one
2204 by one. Don't start a range. */
2207 EXTEND_RANGE_TABLE (work_area, 2);
2208 work_area->table[work_area->used++] = eqv;
2209 work_area->table[work_area->used++] = eqv;
2210 eqv = RE_TRANSLATE (eqv_table, eqv);
2212 while (eqv != start);
2215 /* Add this char to the run, or start a new run. */
2216 else if (run_type == strange)
2218 /* Initialize a new range. */
2219 run_type = this_type;
2220 run_start = start;
2221 run_end = start;
2222 run_eqv_end = RE_TRANSLATE (eqv_table, run_end);
2224 else
2226 /* Extend a running range. */
2227 run_end = minchar;
2228 run_eqv_end = RE_TRANSLATE (eqv_table, run_end);
2232 /* If a run is still in progress at the end, finish it now
2233 by recording its equivalent ranges. */
2234 if (run_type == one_case)
2236 EXTEND_RANGE_TABLE (work_area, 2);
2237 work_area->table[work_area->used++] = run_start;
2238 work_area->table[work_area->used++] = run_end;
2240 else if (run_type == two_case)
2242 EXTEND_RANGE_TABLE (work_area, 4);
2243 work_area->table[work_area->used++] = run_start;
2244 work_area->table[work_area->used++] = run_end;
2245 work_area->table[work_area->used++]
2246 = RE_TRANSLATE (eqv_table, run_start);
2247 work_area->table[work_area->used++]
2248 = RE_TRANSLATE (eqv_table, run_end);
2251 return -1;
2254 #endif /* emacs */
2256 /* Record the image of the range start..end when passed through
2257 TRANSLATE. This is not necessarily TRANSLATE(start)..TRANSLATE(end)
2258 and is not even necessarily contiguous.
2259 Normally we approximate it with the smallest contiguous range that contains
2260 all the chars we need. However, for Latin-1 we go to extra effort
2261 to do a better job.
2263 This function is not called for ASCII ranges.
2265 Returns -1 if successful, REG_ESPACE if ran out of space. */
2267 static int
2268 set_image_of_range (struct range_table_work_area *work_area,
2269 re_wchar_t start, re_wchar_t end,
2270 RE_TRANSLATE_TYPE translate)
2272 re_wchar_t cmin, cmax;
2274 #ifdef emacs
2275 /* For Latin-1 ranges, use set_image_of_range_1
2276 to get proper handling of ranges that include letters and nonletters.
2277 For a range that includes the whole of Latin-1, this is not necessary.
2278 For other character sets, we don't bother to get this right. */
2279 if (RE_TRANSLATE_P (translate) && start < 04400
2280 && !(start < 04200 && end >= 04377))
2282 int newend;
2283 int tem;
2284 newend = end;
2285 if (newend > 04377)
2286 newend = 04377;
2287 tem = set_image_of_range_1 (work_area, start, newend, translate);
2288 if (tem > 0)
2289 return tem;
2291 start = 04400;
2292 if (end < 04400)
2293 return -1;
2295 #endif
2297 EXTEND_RANGE_TABLE (work_area, 2);
2298 work_area->table[work_area->used++] = (start);
2299 work_area->table[work_area->used++] = (end);
2301 cmin = -1, cmax = -1;
2303 if (RE_TRANSLATE_P (translate))
2305 int ch;
2307 for (ch = start; ch <= end; ch++)
2309 re_wchar_t c = TRANSLATE (ch);
2310 if (! (start <= c && c <= end))
2312 if (cmin == -1)
2313 cmin = c, cmax = c;
2314 else
2316 cmin = min (cmin, c);
2317 cmax = max (cmax, c);
2322 if (cmin != -1)
2324 EXTEND_RANGE_TABLE (work_area, 2);
2325 work_area->table[work_area->used++] = (cmin);
2326 work_area->table[work_area->used++] = (cmax);
2330 return -1;
2332 #endif /* 0 */
2334 #ifndef MATCH_MAY_ALLOCATE
2336 /* If we cannot allocate large objects within re_match_2_internal,
2337 we make the fail stack and register vectors global.
2338 The fail stack, we grow to the maximum size when a regexp
2339 is compiled.
2340 The register vectors, we adjust in size each time we
2341 compile a regexp, according to the number of registers it needs. */
2343 static fail_stack_type fail_stack;
2345 /* Size with which the following vectors are currently allocated.
2346 That is so we can make them bigger as needed,
2347 but never make them smaller. */
2348 static int regs_allocated_size;
2350 static re_char ** regstart, ** regend;
2351 static re_char **best_regstart, **best_regend;
2353 /* Make the register vectors big enough for NUM_REGS registers,
2354 but don't make them smaller. */
2356 static
2357 regex_grow_registers (int num_regs)
2359 if (num_regs > regs_allocated_size)
2361 RETALLOC_IF (regstart, num_regs, re_char *);
2362 RETALLOC_IF (regend, num_regs, re_char *);
2363 RETALLOC_IF (best_regstart, num_regs, re_char *);
2364 RETALLOC_IF (best_regend, num_regs, re_char *);
2366 regs_allocated_size = num_regs;
2370 #endif /* not MATCH_MAY_ALLOCATE */
2372 static boolean group_in_compile_stack (compile_stack_type compile_stack,
2373 regnum_t regnum);
2375 /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
2376 Returns one of error codes defined in `regex.h', or zero for success.
2378 Assumes the `allocated' (and perhaps `buffer') and `translate'
2379 fields are set in BUFP on entry.
2381 If it succeeds, results are put in BUFP (if it returns an error, the
2382 contents of BUFP are undefined):
2383 `buffer' is the compiled pattern;
2384 `syntax' is set to SYNTAX;
2385 `used' is set to the length of the compiled pattern;
2386 `fastmap_accurate' is zero;
2387 `re_nsub' is the number of subexpressions in PATTERN;
2388 `not_bol' and `not_eol' are zero;
2390 The `fastmap' field is neither examined nor set. */
2392 /* Insert the `jump' from the end of last alternative to "here".
2393 The space for the jump has already been allocated. */
2394 #define FIXUP_ALT_JUMP() \
2395 do { \
2396 if (fixup_alt_jump) \
2397 STORE_JUMP (jump, fixup_alt_jump, b); \
2398 } while (0)
2401 /* Return, freeing storage we allocated. */
2402 #define FREE_STACK_RETURN(value) \
2403 do { \
2404 FREE_RANGE_TABLE_WORK_AREA (range_table_work); \
2405 free (compile_stack.stack); \
2406 return value; \
2407 } while (0)
2409 static reg_errcode_t
2410 regex_compile (const_re_char *pattern, size_t size, reg_syntax_t syntax,
2411 struct re_pattern_buffer *bufp)
2413 /* We fetch characters from PATTERN here. */
2414 register re_wchar_t c, c1;
2416 /* Points to the end of the buffer, where we should append. */
2417 register unsigned char *b;
2419 /* Keeps track of unclosed groups. */
2420 compile_stack_type compile_stack;
2422 /* Points to the current (ending) position in the pattern. */
2423 #ifdef AIX
2424 /* `const' makes AIX compiler fail. */
2425 unsigned char *p = pattern;
2426 #else
2427 re_char *p = pattern;
2428 #endif
2429 re_char *pend = pattern + size;
2431 /* How to translate the characters in the pattern. */
2432 RE_TRANSLATE_TYPE translate = bufp->translate;
2434 /* Address of the count-byte of the most recently inserted `exactn'
2435 command. This makes it possible to tell if a new exact-match
2436 character can be added to that command or if the character requires
2437 a new `exactn' command. */
2438 unsigned char *pending_exact = 0;
2440 /* Address of start of the most recently finished expression.
2441 This tells, e.g., postfix * where to find the start of its
2442 operand. Reset at the beginning of groups and alternatives. */
2443 unsigned char *laststart = 0;
2445 /* Address of beginning of regexp, or inside of last group. */
2446 unsigned char *begalt;
2448 /* Place in the uncompiled pattern (i.e., the {) to
2449 which to go back if the interval is invalid. */
2450 re_char *beg_interval;
2452 /* Address of the place where a forward jump should go to the end of
2453 the containing expression. Each alternative of an `or' -- except the
2454 last -- ends with a forward jump of this sort. */
2455 unsigned char *fixup_alt_jump = 0;
2457 /* Work area for range table of charset. */
2458 struct range_table_work_area range_table_work;
2460 /* If the object matched can contain multibyte characters. */
2461 const boolean multibyte = RE_MULTIBYTE_P (bufp);
2463 /* Nonzero if we have pushed down into a subpattern. */
2464 int in_subpattern = 0;
2466 /* These hold the values of p, pattern, and pend from the main
2467 pattern when we have pushed into a subpattern. */
2468 re_char *main_p;
2469 re_char *main_pattern;
2470 re_char *main_pend;
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 bufp->syntax = syntax;
2498 bufp->fastmap_accurate = 0;
2499 bufp->not_bol = bufp->not_eol = 0;
2500 bufp->used_syntax = 0;
2502 /* Set `used' to zero, so that if we return an error, the pattern
2503 printer (for debugging) will think there's no pattern. We reset it
2504 at the end. */
2505 bufp->used = 0;
2507 /* Always count groups, whether or not bufp->no_sub is set. */
2508 bufp->re_nsub = 0;
2510 #if !defined emacs && !defined SYNTAX_TABLE
2511 /* Initialize the syntax table. */
2512 init_syntax_once ();
2513 #endif
2515 if (bufp->allocated == 0)
2517 if (bufp->buffer)
2518 { /* If zero allocated, but buffer is non-null, try to realloc
2519 enough space. This loses if buffer's address is bogus, but
2520 that is the user's responsibility. */
2521 RETALLOC (bufp->buffer, INIT_BUF_SIZE, unsigned char);
2523 else
2524 { /* Caller did not allocate a buffer. Do it for them. */
2525 bufp->buffer = TALLOC (INIT_BUF_SIZE, unsigned char);
2527 if (!bufp->buffer) FREE_STACK_RETURN (REG_ESPACE);
2529 bufp->allocated = INIT_BUF_SIZE;
2532 begalt = b = bufp->buffer;
2534 /* Loop through the uncompiled pattern until we're at the end. */
2535 while (1)
2537 if (p == pend)
2539 /* If this is the end of an included regexp,
2540 pop back to the main regexp and try again. */
2541 if (in_subpattern)
2543 in_subpattern = 0;
2544 pattern = main_pattern;
2545 p = main_p;
2546 pend = main_pend;
2547 continue;
2549 /* If this is the end of the main regexp, we are done. */
2550 break;
2553 PATFETCH (c);
2555 switch (c)
2557 case ' ':
2559 re_char *p1 = p;
2561 /* If there's no special whitespace regexp, treat
2562 spaces normally. And don't try to do this recursively. */
2563 if (!whitespace_regexp || in_subpattern)
2564 goto normal_char;
2566 /* Peek past following spaces. */
2567 while (p1 != pend)
2569 if (*p1 != ' ')
2570 break;
2571 p1++;
2573 /* If the spaces are followed by a repetition op,
2574 treat them normally. */
2575 if (p1 != pend
2576 && (*p1 == '*' || *p1 == '+' || *p1 == '?'
2577 || (*p1 == '\\' && p1 + 1 != pend && p1[1] == '{')))
2578 goto normal_char;
2580 /* Replace the spaces with the whitespace regexp. */
2581 in_subpattern = 1;
2582 main_p = p1;
2583 main_pend = pend;
2584 main_pattern = pattern;
2585 p = pattern = whitespace_regexp;
2586 pend = p + strlen ((const char *) p);
2587 break;
2590 case '^':
2592 if ( /* If at start of pattern, it's an operator. */
2593 p == pattern + 1
2594 /* If context independent, it's an operator. */
2595 || syntax & RE_CONTEXT_INDEP_ANCHORS
2596 /* Otherwise, depends on what's come before. */
2597 || at_begline_loc_p (pattern, p, syntax))
2598 BUF_PUSH ((syntax & RE_NO_NEWLINE_ANCHOR) ? begbuf : begline);
2599 else
2600 goto normal_char;
2602 break;
2605 case '$':
2607 if ( /* If at end of pattern, it's an operator. */
2608 p == pend
2609 /* If context independent, it's an operator. */
2610 || syntax & RE_CONTEXT_INDEP_ANCHORS
2611 /* Otherwise, depends on what's next. */
2612 || at_endline_loc_p (p, pend, syntax))
2613 BUF_PUSH ((syntax & RE_NO_NEWLINE_ANCHOR) ? endbuf : endline);
2614 else
2615 goto normal_char;
2617 break;
2620 case '+':
2621 case '?':
2622 if ((syntax & RE_BK_PLUS_QM)
2623 || (syntax & RE_LIMITED_OPS))
2624 goto normal_char;
2625 handle_plus:
2626 case '*':
2627 /* If there is no previous pattern... */
2628 if (!laststart)
2630 if (syntax & RE_CONTEXT_INVALID_OPS)
2631 FREE_STACK_RETURN (REG_BADRPT);
2632 else if (!(syntax & RE_CONTEXT_INDEP_OPS))
2633 goto normal_char;
2637 /* 1 means zero (many) matches is allowed. */
2638 boolean zero_times_ok = 0, many_times_ok = 0;
2639 boolean greedy = 1;
2641 /* If there is a sequence of repetition chars, collapse it
2642 down to just one (the right one). We can't combine
2643 interval operators with these because of, e.g., `a{2}*',
2644 which should only match an even number of `a's. */
2646 for (;;)
2648 if ((syntax & RE_FRUGAL)
2649 && c == '?' && (zero_times_ok || many_times_ok))
2650 greedy = 0;
2651 else
2653 zero_times_ok |= c != '+';
2654 many_times_ok |= c != '?';
2657 if (p == pend)
2658 break;
2659 else if (*p == '*'
2660 || (!(syntax & RE_BK_PLUS_QM)
2661 && (*p == '+' || *p == '?')))
2663 else if (syntax & RE_BK_PLUS_QM && *p == '\\')
2665 if (p+1 == pend)
2666 FREE_STACK_RETURN (REG_EESCAPE);
2667 if (p[1] == '+' || p[1] == '?')
2668 PATFETCH (c); /* Gobble up the backslash. */
2669 else
2670 break;
2672 else
2673 break;
2674 /* If we get here, we found another repeat character. */
2675 PATFETCH (c);
2678 /* Star, etc. applied to an empty pattern is equivalent
2679 to an empty pattern. */
2680 if (!laststart || laststart == b)
2681 break;
2683 /* Now we know whether or not zero matches is allowed
2684 and also whether or not two or more matches is allowed. */
2685 if (greedy)
2687 if (many_times_ok)
2689 boolean simple = skip_one_char (laststart) == b;
2690 size_t startoffset = 0;
2691 re_opcode_t ofj =
2692 /* Check if the loop can match the empty string. */
2693 (simple || !analyze_first (laststart, b, NULL, 0))
2694 ? on_failure_jump : on_failure_jump_loop;
2695 assert (skip_one_char (laststart) <= b);
2697 if (!zero_times_ok && simple)
2698 { /* Since simple * loops can be made faster by using
2699 on_failure_keep_string_jump, we turn simple P+
2700 into PP* if P is simple. */
2701 unsigned char *p1, *p2;
2702 startoffset = b - laststart;
2703 GET_BUFFER_SPACE (startoffset);
2704 p1 = b; p2 = laststart;
2705 while (p2 < p1)
2706 *b++ = *p2++;
2707 zero_times_ok = 1;
2710 GET_BUFFER_SPACE (6);
2711 if (!zero_times_ok)
2712 /* A + loop. */
2713 STORE_JUMP (ofj, b, b + 6);
2714 else
2715 /* Simple * loops can use on_failure_keep_string_jump
2716 depending on what follows. But since we don't know
2717 that yet, we leave the decision up to
2718 on_failure_jump_smart. */
2719 INSERT_JUMP (simple ? on_failure_jump_smart : ofj,
2720 laststart + startoffset, b + 6);
2721 b += 3;
2722 STORE_JUMP (jump, b, laststart + startoffset);
2723 b += 3;
2725 else
2727 /* A simple ? pattern. */
2728 assert (zero_times_ok);
2729 GET_BUFFER_SPACE (3);
2730 INSERT_JUMP (on_failure_jump, laststart, b + 3);
2731 b += 3;
2734 else /* not greedy */
2735 { /* I wish the greedy and non-greedy cases could be merged. */
2737 GET_BUFFER_SPACE (7); /* We might use less. */
2738 if (many_times_ok)
2740 boolean emptyp = analyze_first (laststart, b, NULL, 0);
2742 /* The non-greedy multiple match looks like
2743 a repeat..until: we only need a conditional jump
2744 at the end of the loop. */
2745 if (emptyp) BUF_PUSH (no_op);
2746 STORE_JUMP (emptyp ? on_failure_jump_nastyloop
2747 : on_failure_jump, b, laststart);
2748 b += 3;
2749 if (zero_times_ok)
2751 /* The repeat...until naturally matches one or more.
2752 To also match zero times, we need to first jump to
2753 the end of the loop (its conditional jump). */
2754 INSERT_JUMP (jump, laststart, b);
2755 b += 3;
2758 else
2760 /* non-greedy a?? */
2761 INSERT_JUMP (jump, laststart, b + 3);
2762 b += 3;
2763 INSERT_JUMP (on_failure_jump, laststart, laststart + 6);
2764 b += 3;
2768 pending_exact = 0;
2769 break;
2772 case '.':
2773 laststart = b;
2774 BUF_PUSH (anychar);
2775 break;
2778 case '[':
2780 re_char *p1;
2782 CLEAR_RANGE_TABLE_WORK_USED (range_table_work);
2784 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2786 /* Ensure that we have enough space to push a charset: the
2787 opcode, the length count, and the bitset; 34 bytes in all. */
2788 GET_BUFFER_SPACE (34);
2790 laststart = b;
2792 /* We test `*p == '^' twice, instead of using an if
2793 statement, so we only need one BUF_PUSH. */
2794 BUF_PUSH (*p == '^' ? charset_not : charset);
2795 if (*p == '^')
2796 p++;
2798 /* Remember the first position in the bracket expression. */
2799 p1 = p;
2801 /* Push the number of bytes in the bitmap. */
2802 BUF_PUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
2804 /* Clear the whole map. */
2805 memset (b, 0, (1 << BYTEWIDTH) / BYTEWIDTH);
2807 /* charset_not matches newline according to a syntax bit. */
2808 if ((re_opcode_t) b[-2] == charset_not
2809 && (syntax & RE_HAT_LISTS_NOT_NEWLINE))
2810 SET_LIST_BIT ('\n');
2812 /* Read in characters and ranges, setting map bits. */
2813 for (;;)
2815 boolean escaped_char = false;
2816 const unsigned char *p2 = p;
2817 re_wchar_t ch;
2819 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2821 /* Don't translate yet. The range TRANSLATE(X..Y) cannot
2822 always be determined from TRANSLATE(X) and TRANSLATE(Y)
2823 So the translation is done later in a loop. Example:
2824 (let ((case-fold-search t)) (string-match "[A-_]" "A")) */
2825 PATFETCH (c);
2827 /* \ might escape characters inside [...] and [^...]. */
2828 if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) && c == '\\')
2830 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
2832 PATFETCH (c);
2833 escaped_char = true;
2835 else
2837 /* Could be the end of the bracket expression. If it's
2838 not (i.e., when the bracket expression is `[]' so
2839 far), the ']' character bit gets set way below. */
2840 if (c == ']' && p2 != p1)
2841 break;
2844 /* See if we're at the beginning of a possible character
2845 class. */
2847 if (!escaped_char &&
2848 syntax & RE_CHAR_CLASSES && c == '[' && *p == ':')
2850 /* Leave room for the null. */
2851 unsigned char str[CHAR_CLASS_MAX_LENGTH + 1];
2852 const unsigned char *class_beg;
2854 PATFETCH (c);
2855 c1 = 0;
2856 class_beg = p;
2858 /* If pattern is `[[:'. */
2859 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2861 for (;;)
2863 PATFETCH (c);
2864 if ((c == ':' && *p == ']') || p == pend)
2865 break;
2866 if (c1 < CHAR_CLASS_MAX_LENGTH)
2867 str[c1++] = c;
2868 else
2869 /* This is in any case an invalid class name. */
2870 str[0] = '\0';
2872 str[c1] = '\0';
2874 /* If isn't a word bracketed by `[:' and `:]':
2875 undo the ending character, the letters, and
2876 leave the leading `:' and `[' (but set bits for
2877 them). */
2878 if (c == ':' && *p == ']')
2880 re_wctype_t cc = re_wctype (str);
2882 if (cc == 0)
2883 FREE_STACK_RETURN (REG_ECTYPE);
2885 /* Throw away the ] at the end of the character
2886 class. */
2887 PATFETCH (c);
2889 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2891 #ifndef emacs
2892 for (ch = 0; ch < (1 << BYTEWIDTH); ++ch)
2893 if (re_iswctype (btowc (ch), cc))
2895 c = TRANSLATE (ch);
2896 if (c < (1 << BYTEWIDTH))
2897 SET_LIST_BIT (c);
2899 #else /* emacs */
2900 /* Most character classes in a multibyte match
2901 just set a flag. Exceptions are is_blank,
2902 is_digit, is_cntrl, and is_xdigit, since
2903 they can only match ASCII characters. We
2904 don't need to handle them for multibyte.
2905 They are distinguished by a negative wctype. */
2907 /* Setup the gl_state object to its buffer-defined
2908 value. This hardcodes the buffer-global
2909 syntax-table for ASCII chars, while the other chars
2910 will obey syntax-table properties. It's not ideal,
2911 but it's the way it's been done until now. */
2912 SETUP_BUFFER_SYNTAX_TABLE ();
2914 for (ch = 0; ch < 256; ++ch)
2916 c = RE_CHAR_TO_MULTIBYTE (ch);
2917 if (! CHAR_BYTE8_P (c)
2918 && re_iswctype (c, cc))
2920 SET_LIST_BIT (ch);
2921 c1 = TRANSLATE (c);
2922 if (c1 == c)
2923 continue;
2924 if (ASCII_CHAR_P (c1))
2925 SET_LIST_BIT (c1);
2926 else if ((c1 = RE_CHAR_TO_UNIBYTE (c1)) >= 0)
2927 SET_LIST_BIT (c1);
2930 SET_RANGE_TABLE_WORK_AREA_BIT
2931 (range_table_work, re_wctype_to_bit (cc));
2932 #endif /* emacs */
2933 /* In most cases the matching rule for char classes
2934 only uses the syntax table for multibyte chars,
2935 so that the content of the syntax-table is not
2936 hardcoded in the range_table. SPACE and WORD are
2937 the two exceptions. */
2938 if ((1 << cc) & ((1 << RECC_SPACE) | (1 << RECC_WORD)))
2939 bufp->used_syntax = 1;
2941 /* Repeat the loop. */
2942 continue;
2944 else
2946 /* Go back to right after the "[:". */
2947 p = class_beg;
2948 SET_LIST_BIT ('[');
2950 /* Because the `:' may start the range, we
2951 can't simply set bit and repeat the loop.
2952 Instead, just set it to C and handle below. */
2953 c = ':';
2957 if (p < pend && p[0] == '-' && p[1] != ']')
2960 /* Discard the `-'. */
2961 PATFETCH (c1);
2963 /* Fetch the character which ends the range. */
2964 PATFETCH (c1);
2965 #ifdef emacs
2966 if (CHAR_BYTE8_P (c1)
2967 && ! ASCII_CHAR_P (c) && ! CHAR_BYTE8_P (c))
2968 /* Treat the range from a multibyte character to
2969 raw-byte character as empty. */
2970 c = c1 + 1;
2971 #endif /* emacs */
2973 else
2974 /* Range from C to C. */
2975 c1 = c;
2977 if (c > c1)
2979 if (syntax & RE_NO_EMPTY_RANGES)
2980 FREE_STACK_RETURN (REG_ERANGEX);
2981 /* Else, repeat the loop. */
2983 else
2985 #ifndef emacs
2986 /* Set the range into bitmap */
2987 for (; c <= c1; c++)
2989 ch = TRANSLATE (c);
2990 if (ch < (1 << BYTEWIDTH))
2991 SET_LIST_BIT (ch);
2993 #else /* emacs */
2994 if (c < 128)
2996 ch = min (127, c1);
2997 SETUP_ASCII_RANGE (range_table_work, c, ch);
2998 c = ch + 1;
2999 if (CHAR_BYTE8_P (c1))
3000 c = BYTE8_TO_CHAR (128);
3002 if (c <= c1)
3004 if (CHAR_BYTE8_P (c))
3006 c = CHAR_TO_BYTE8 (c);
3007 c1 = CHAR_TO_BYTE8 (c1);
3008 for (; c <= c1; c++)
3009 SET_LIST_BIT (c);
3011 else if (multibyte)
3013 SETUP_MULTIBYTE_RANGE (range_table_work, c, c1);
3015 else
3017 SETUP_UNIBYTE_RANGE (range_table_work, c, c1);
3020 #endif /* emacs */
3024 /* Discard any (non)matching list bytes that are all 0 at the
3025 end of the map. Decrease the map-length byte too. */
3026 while ((int) b[-1] > 0 && b[b[-1] - 1] == 0)
3027 b[-1]--;
3028 b += b[-1];
3030 /* Build real range table from work area. */
3031 if (RANGE_TABLE_WORK_USED (range_table_work)
3032 || RANGE_TABLE_WORK_BITS (range_table_work))
3034 int i;
3035 int used = RANGE_TABLE_WORK_USED (range_table_work);
3037 /* Allocate space for COUNT + RANGE_TABLE. Needs two
3038 bytes for flags, two for COUNT, and three bytes for
3039 each character. */
3040 GET_BUFFER_SPACE (4 + used * 3);
3042 /* Indicate the existence of range table. */
3043 laststart[1] |= 0x80;
3045 /* Store the character class flag bits into the range table.
3046 If not in emacs, these flag bits are always 0. */
3047 *b++ = RANGE_TABLE_WORK_BITS (range_table_work) & 0xff;
3048 *b++ = RANGE_TABLE_WORK_BITS (range_table_work) >> 8;
3050 STORE_NUMBER_AND_INCR (b, used / 2);
3051 for (i = 0; i < used; i++)
3052 STORE_CHARACTER_AND_INCR
3053 (b, RANGE_TABLE_WORK_ELT (range_table_work, i));
3056 break;
3059 case '(':
3060 if (syntax & RE_NO_BK_PARENS)
3061 goto handle_open;
3062 else
3063 goto normal_char;
3066 case ')':
3067 if (syntax & RE_NO_BK_PARENS)
3068 goto handle_close;
3069 else
3070 goto normal_char;
3073 case '\n':
3074 if (syntax & RE_NEWLINE_ALT)
3075 goto handle_alt;
3076 else
3077 goto normal_char;
3080 case '|':
3081 if (syntax & RE_NO_BK_VBAR)
3082 goto handle_alt;
3083 else
3084 goto normal_char;
3087 case '{':
3088 if (syntax & RE_INTERVALS && syntax & RE_NO_BK_BRACES)
3089 goto handle_interval;
3090 else
3091 goto normal_char;
3094 case '\\':
3095 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
3097 /* Do not translate the character after the \, so that we can
3098 distinguish, e.g., \B from \b, even if we normally would
3099 translate, e.g., B to b. */
3100 PATFETCH (c);
3102 switch (c)
3104 case '(':
3105 if (syntax & RE_NO_BK_PARENS)
3106 goto normal_backslash;
3108 handle_open:
3110 int shy = 0;
3111 regnum_t regnum = 0;
3112 if (p+1 < pend)
3114 /* Look for a special (?...) construct */
3115 if ((syntax & RE_SHY_GROUPS) && *p == '?')
3117 PATFETCH (c); /* Gobble up the '?'. */
3118 while (!shy)
3120 PATFETCH (c);
3121 switch (c)
3123 case ':': shy = 1; break;
3124 case '0':
3125 /* An explicitly specified regnum must start
3126 with non-0. */
3127 if (regnum == 0)
3128 FREE_STACK_RETURN (REG_BADPAT);
3129 case '1': case '2': case '3': case '4':
3130 case '5': case '6': case '7': case '8': case '9':
3131 regnum = 10*regnum + (c - '0'); break;
3132 default:
3133 /* Only (?:...) is supported right now. */
3134 FREE_STACK_RETURN (REG_BADPAT);
3140 if (!shy)
3141 regnum = ++bufp->re_nsub;
3142 else if (regnum)
3143 { /* It's actually not shy, but explicitly numbered. */
3144 shy = 0;
3145 if (regnum > bufp->re_nsub)
3146 bufp->re_nsub = regnum;
3147 else if (regnum > bufp->re_nsub
3148 /* Ideally, we'd want to check that the specified
3149 group can't have matched (i.e. all subgroups
3150 using the same regnum are in other branches of
3151 OR patterns), but we don't currently keep track
3152 of enough info to do that easily. */
3153 || group_in_compile_stack (compile_stack, regnum))
3154 FREE_STACK_RETURN (REG_BADPAT);
3156 else
3157 /* It's really shy. */
3158 regnum = - bufp->re_nsub;
3160 if (COMPILE_STACK_FULL)
3162 RETALLOC (compile_stack.stack, compile_stack.size << 1,
3163 compile_stack_elt_t);
3164 if (compile_stack.stack == NULL) return REG_ESPACE;
3166 compile_stack.size <<= 1;
3169 /* These are the values to restore when we hit end of this
3170 group. They are all relative offsets, so that if the
3171 whole pattern moves because of realloc, they will still
3172 be valid. */
3173 COMPILE_STACK_TOP.begalt_offset = begalt - bufp->buffer;
3174 COMPILE_STACK_TOP.fixup_alt_jump
3175 = fixup_alt_jump ? fixup_alt_jump - bufp->buffer + 1 : 0;
3176 COMPILE_STACK_TOP.laststart_offset = b - bufp->buffer;
3177 COMPILE_STACK_TOP.regnum = regnum;
3179 /* Do not push a start_memory for groups beyond the last one
3180 we can represent in the compiled pattern. */
3181 if (regnum <= MAX_REGNUM && regnum > 0)
3182 BUF_PUSH_2 (start_memory, regnum);
3184 compile_stack.avail++;
3186 fixup_alt_jump = 0;
3187 laststart = 0;
3188 begalt = b;
3189 /* If we've reached MAX_REGNUM groups, then this open
3190 won't actually generate any code, so we'll have to
3191 clear pending_exact explicitly. */
3192 pending_exact = 0;
3193 break;
3196 case ')':
3197 if (syntax & RE_NO_BK_PARENS) goto normal_backslash;
3199 if (COMPILE_STACK_EMPTY)
3201 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
3202 goto normal_backslash;
3203 else
3204 FREE_STACK_RETURN (REG_ERPAREN);
3207 handle_close:
3208 FIXUP_ALT_JUMP ();
3210 /* See similar code for backslashed left paren above. */
3211 if (COMPILE_STACK_EMPTY)
3213 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
3214 goto normal_char;
3215 else
3216 FREE_STACK_RETURN (REG_ERPAREN);
3219 /* Since we just checked for an empty stack above, this
3220 ``can't happen''. */
3221 assert (compile_stack.avail != 0);
3223 /* We don't just want to restore into `regnum', because
3224 later groups should continue to be numbered higher,
3225 as in `(ab)c(de)' -- the second group is #2. */
3226 regnum_t regnum;
3228 compile_stack.avail--;
3229 begalt = bufp->buffer + COMPILE_STACK_TOP.begalt_offset;
3230 fixup_alt_jump
3231 = COMPILE_STACK_TOP.fixup_alt_jump
3232 ? bufp->buffer + COMPILE_STACK_TOP.fixup_alt_jump - 1
3233 : 0;
3234 laststart = bufp->buffer + COMPILE_STACK_TOP.laststart_offset;
3235 regnum = COMPILE_STACK_TOP.regnum;
3236 /* If we've reached MAX_REGNUM groups, then this open
3237 won't actually generate any code, so we'll have to
3238 clear pending_exact explicitly. */
3239 pending_exact = 0;
3241 /* We're at the end of the group, so now we know how many
3242 groups were inside this one. */
3243 if (regnum <= MAX_REGNUM && regnum > 0)
3244 BUF_PUSH_2 (stop_memory, regnum);
3246 break;
3249 case '|': /* `\|'. */
3250 if (syntax & RE_LIMITED_OPS || syntax & RE_NO_BK_VBAR)
3251 goto normal_backslash;
3252 handle_alt:
3253 if (syntax & RE_LIMITED_OPS)
3254 goto normal_char;
3256 /* Insert before the previous alternative a jump which
3257 jumps to this alternative if the former fails. */
3258 GET_BUFFER_SPACE (3);
3259 INSERT_JUMP (on_failure_jump, begalt, b + 6);
3260 pending_exact = 0;
3261 b += 3;
3263 /* The alternative before this one has a jump after it
3264 which gets executed if it gets matched. Adjust that
3265 jump so it will jump to this alternative's analogous
3266 jump (put in below, which in turn will jump to the next
3267 (if any) alternative's such jump, etc.). The last such
3268 jump jumps to the correct final destination. A picture:
3269 _____ _____
3270 | | | |
3271 | v | v
3272 a | b | c
3274 If we are at `b', then fixup_alt_jump right now points to a
3275 three-byte space after `a'. We'll put in the jump, set
3276 fixup_alt_jump to right after `b', and leave behind three
3277 bytes which we'll fill in when we get to after `c'. */
3279 FIXUP_ALT_JUMP ();
3281 /* Mark and leave space for a jump after this alternative,
3282 to be filled in later either by next alternative or
3283 when know we're at the end of a series of alternatives. */
3284 fixup_alt_jump = b;
3285 GET_BUFFER_SPACE (3);
3286 b += 3;
3288 laststart = 0;
3289 begalt = b;
3290 break;
3293 case '{':
3294 /* If \{ is a literal. */
3295 if (!(syntax & RE_INTERVALS)
3296 /* If we're at `\{' and it's not the open-interval
3297 operator. */
3298 || (syntax & RE_NO_BK_BRACES))
3299 goto normal_backslash;
3301 handle_interval:
3303 /* If got here, then the syntax allows intervals. */
3305 /* At least (most) this many matches must be made. */
3306 int lower_bound = 0, upper_bound = -1;
3308 beg_interval = p;
3310 GET_INTERVAL_COUNT (lower_bound);
3312 if (c == ',')
3313 GET_INTERVAL_COUNT (upper_bound);
3314 else
3315 /* Interval such as `{1}' => match exactly once. */
3316 upper_bound = lower_bound;
3318 if (lower_bound < 0
3319 || (0 <= upper_bound && upper_bound < lower_bound))
3320 FREE_STACK_RETURN (REG_BADBR);
3322 if (!(syntax & RE_NO_BK_BRACES))
3324 if (c != '\\')
3325 FREE_STACK_RETURN (REG_BADBR);
3326 if (p == pend)
3327 FREE_STACK_RETURN (REG_EESCAPE);
3328 PATFETCH (c);
3331 if (c != '}')
3332 FREE_STACK_RETURN (REG_BADBR);
3334 /* We just parsed a valid interval. */
3336 /* If it's invalid to have no preceding re. */
3337 if (!laststart)
3339 if (syntax & RE_CONTEXT_INVALID_OPS)
3340 FREE_STACK_RETURN (REG_BADRPT);
3341 else if (syntax & RE_CONTEXT_INDEP_OPS)
3342 laststart = b;
3343 else
3344 goto unfetch_interval;
3347 if (upper_bound == 0)
3348 /* If the upper bound is zero, just drop the sub pattern
3349 altogether. */
3350 b = laststart;
3351 else if (lower_bound == 1 && upper_bound == 1)
3352 /* Just match it once: nothing to do here. */
3355 /* Otherwise, we have a nontrivial interval. When
3356 we're all done, the pattern will look like:
3357 set_number_at <jump count> <upper bound>
3358 set_number_at <succeed_n count> <lower bound>
3359 succeed_n <after jump addr> <succeed_n count>
3360 <body of loop>
3361 jump_n <succeed_n addr> <jump count>
3362 (The upper bound and `jump_n' are omitted if
3363 `upper_bound' is 1, though.) */
3364 else
3365 { /* If the upper bound is > 1, we need to insert
3366 more at the end of the loop. */
3367 unsigned int nbytes = (upper_bound < 0 ? 3
3368 : upper_bound > 1 ? 5 : 0);
3369 unsigned int startoffset = 0;
3371 GET_BUFFER_SPACE (20); /* We might use less. */
3373 if (lower_bound == 0)
3375 /* A succeed_n that starts with 0 is really a
3376 a simple on_failure_jump_loop. */
3377 INSERT_JUMP (on_failure_jump_loop, laststart,
3378 b + 3 + nbytes);
3379 b += 3;
3381 else
3383 /* Initialize lower bound of the `succeed_n', even
3384 though it will be set during matching by its
3385 attendant `set_number_at' (inserted next),
3386 because `re_compile_fastmap' needs to know.
3387 Jump to the `jump_n' we might insert below. */
3388 INSERT_JUMP2 (succeed_n, laststart,
3389 b + 5 + nbytes,
3390 lower_bound);
3391 b += 5;
3393 /* Code to initialize the lower bound. Insert
3394 before the `succeed_n'. The `5' is the last two
3395 bytes of this `set_number_at', plus 3 bytes of
3396 the following `succeed_n'. */
3397 insert_op2 (set_number_at, laststart, 5, lower_bound, b);
3398 b += 5;
3399 startoffset += 5;
3402 if (upper_bound < 0)
3404 /* A negative upper bound stands for infinity,
3405 in which case it degenerates to a plain jump. */
3406 STORE_JUMP (jump, b, laststart + startoffset);
3407 b += 3;
3409 else if (upper_bound > 1)
3410 { /* More than one repetition is allowed, so
3411 append a backward jump to the `succeed_n'
3412 that starts this interval.
3414 When we've reached this during matching,
3415 we'll have matched the interval once, so
3416 jump back only `upper_bound - 1' times. */
3417 STORE_JUMP2 (jump_n, b, laststart + startoffset,
3418 upper_bound - 1);
3419 b += 5;
3421 /* The location we want to set is the second
3422 parameter of the `jump_n'; that is `b-2' as
3423 an absolute address. `laststart' will be
3424 the `set_number_at' we're about to insert;
3425 `laststart+3' the number to set, the source
3426 for the relative address. But we are
3427 inserting into the middle of the pattern --
3428 so everything is getting moved up by 5.
3429 Conclusion: (b - 2) - (laststart + 3) + 5,
3430 i.e., b - laststart.
3432 We insert this at the beginning of the loop
3433 so that if we fail during matching, we'll
3434 reinitialize the bounds. */
3435 insert_op2 (set_number_at, laststart, b - laststart,
3436 upper_bound - 1, b);
3437 b += 5;
3440 pending_exact = 0;
3441 beg_interval = NULL;
3443 break;
3445 unfetch_interval:
3446 /* If an invalid interval, match the characters as literals. */
3447 assert (beg_interval);
3448 p = beg_interval;
3449 beg_interval = NULL;
3451 /* normal_char and normal_backslash need `c'. */
3452 c = '{';
3454 if (!(syntax & RE_NO_BK_BRACES))
3456 assert (p > pattern && p[-1] == '\\');
3457 goto normal_backslash;
3459 else
3460 goto normal_char;
3462 #ifdef emacs
3463 /* There is no way to specify the before_dot and after_dot
3464 operators. rms says this is ok. --karl */
3465 case '=':
3466 laststart = b;
3467 BUF_PUSH (at_dot);
3468 break;
3470 case 's':
3471 laststart = b;
3472 PATFETCH (c);
3473 BUF_PUSH_2 (syntaxspec, syntax_spec_code[c]);
3474 break;
3476 case 'S':
3477 laststart = b;
3478 PATFETCH (c);
3479 BUF_PUSH_2 (notsyntaxspec, syntax_spec_code[c]);
3480 break;
3482 case 'c':
3483 laststart = b;
3484 PATFETCH (c);
3485 BUF_PUSH_2 (categoryspec, c);
3486 break;
3488 case 'C':
3489 laststart = b;
3490 PATFETCH (c);
3491 BUF_PUSH_2 (notcategoryspec, c);
3492 break;
3493 #endif /* emacs */
3496 case 'w':
3497 if (syntax & RE_NO_GNU_OPS)
3498 goto normal_char;
3499 laststart = b;
3500 BUF_PUSH_2 (syntaxspec, Sword);
3501 break;
3504 case 'W':
3505 if (syntax & RE_NO_GNU_OPS)
3506 goto normal_char;
3507 laststart = b;
3508 BUF_PUSH_2 (notsyntaxspec, Sword);
3509 break;
3512 case '<':
3513 if (syntax & RE_NO_GNU_OPS)
3514 goto normal_char;
3515 laststart = b;
3516 BUF_PUSH (wordbeg);
3517 break;
3519 case '>':
3520 if (syntax & RE_NO_GNU_OPS)
3521 goto normal_char;
3522 laststart = b;
3523 BUF_PUSH (wordend);
3524 break;
3526 case '_':
3527 if (syntax & RE_NO_GNU_OPS)
3528 goto normal_char;
3529 laststart = b;
3530 PATFETCH (c);
3531 if (c == '<')
3532 BUF_PUSH (symbeg);
3533 else if (c == '>')
3534 BUF_PUSH (symend);
3535 else
3536 FREE_STACK_RETURN (REG_BADPAT);
3537 break;
3539 case 'b':
3540 if (syntax & RE_NO_GNU_OPS)
3541 goto normal_char;
3542 BUF_PUSH (wordbound);
3543 break;
3545 case 'B':
3546 if (syntax & RE_NO_GNU_OPS)
3547 goto normal_char;
3548 BUF_PUSH (notwordbound);
3549 break;
3551 case '`':
3552 if (syntax & RE_NO_GNU_OPS)
3553 goto normal_char;
3554 BUF_PUSH (begbuf);
3555 break;
3557 case '\'':
3558 if (syntax & RE_NO_GNU_OPS)
3559 goto normal_char;
3560 BUF_PUSH (endbuf);
3561 break;
3563 case '1': case '2': case '3': case '4': case '5':
3564 case '6': case '7': case '8': case '9':
3566 regnum_t reg;
3568 if (syntax & RE_NO_BK_REFS)
3569 goto normal_backslash;
3571 reg = c - '0';
3573 if (reg > bufp->re_nsub || reg < 1
3574 /* Can't back reference to a subexp before its end. */
3575 || group_in_compile_stack (compile_stack, reg))
3576 FREE_STACK_RETURN (REG_ESUBREG);
3578 laststart = b;
3579 BUF_PUSH_2 (duplicate, reg);
3581 break;
3584 case '+':
3585 case '?':
3586 if (syntax & RE_BK_PLUS_QM)
3587 goto handle_plus;
3588 else
3589 goto normal_backslash;
3591 default:
3592 normal_backslash:
3593 /* You might think it would be useful for \ to mean
3594 not to translate; but if we don't translate it
3595 it will never match anything. */
3596 goto normal_char;
3598 break;
3601 default:
3602 /* Expects the character in `c'. */
3603 normal_char:
3604 /* If no exactn currently being built. */
3605 if (!pending_exact
3607 /* If last exactn not at current position. */
3608 || pending_exact + *pending_exact + 1 != b
3610 /* We have only one byte following the exactn for the count. */
3611 || *pending_exact >= (1 << BYTEWIDTH) - MAX_MULTIBYTE_LENGTH
3613 /* If followed by a repetition operator. */
3614 || (p != pend && (*p == '*' || *p == '^'))
3615 || ((syntax & RE_BK_PLUS_QM)
3616 ? p + 1 < pend && *p == '\\' && (p[1] == '+' || p[1] == '?')
3617 : p != pend && (*p == '+' || *p == '?'))
3618 || ((syntax & RE_INTERVALS)
3619 && ((syntax & RE_NO_BK_BRACES)
3620 ? p != pend && *p == '{'
3621 : p + 1 < pend && p[0] == '\\' && p[1] == '{')))
3623 /* Start building a new exactn. */
3625 laststart = b;
3627 BUF_PUSH_2 (exactn, 0);
3628 pending_exact = b - 1;
3631 GET_BUFFER_SPACE (MAX_MULTIBYTE_LENGTH);
3633 int len;
3635 if (multibyte)
3637 c = TRANSLATE (c);
3638 len = CHAR_STRING (c, b);
3639 b += len;
3641 else
3643 c1 = RE_CHAR_TO_MULTIBYTE (c);
3644 if (! CHAR_BYTE8_P (c1))
3646 re_wchar_t c2 = TRANSLATE (c1);
3648 if (c1 != c2 && (c1 = RE_CHAR_TO_UNIBYTE (c2)) >= 0)
3649 c = c1;
3651 *b++ = c;
3652 len = 1;
3654 (*pending_exact) += len;
3657 break;
3658 } /* switch (c) */
3659 } /* while p != pend */
3662 /* Through the pattern now. */
3664 FIXUP_ALT_JUMP ();
3666 if (!COMPILE_STACK_EMPTY)
3667 FREE_STACK_RETURN (REG_EPAREN);
3669 /* If we don't want backtracking, force success
3670 the first time we reach the end of the compiled pattern. */
3671 if (syntax & RE_NO_POSIX_BACKTRACKING)
3672 BUF_PUSH (succeed);
3674 /* We have succeeded; set the length of the buffer. */
3675 bufp->used = b - bufp->buffer;
3677 #ifdef DEBUG
3678 if (debug > 0)
3680 re_compile_fastmap (bufp);
3681 DEBUG_PRINT ("\nCompiled pattern: \n");
3682 print_compiled_pattern (bufp);
3684 debug--;
3685 #endif /* DEBUG */
3687 #ifndef MATCH_MAY_ALLOCATE
3688 /* Initialize the failure stack to the largest possible stack. This
3689 isn't necessary unless we're trying to avoid calling alloca in
3690 the search and match routines. */
3692 int num_regs = bufp->re_nsub + 1;
3694 if (fail_stack.size < re_max_failures * TYPICAL_FAILURE_SIZE)
3696 fail_stack.size = re_max_failures * TYPICAL_FAILURE_SIZE;
3697 falk_stack.stack = realloc (fail_stack.stack,
3698 fail_stack.size * sizeof *falk_stack.stack);
3701 regex_grow_registers (num_regs);
3703 #endif /* not MATCH_MAY_ALLOCATE */
3705 FREE_STACK_RETURN (REG_NOERROR);
3706 } /* regex_compile */
3708 /* Subroutines for `regex_compile'. */
3710 /* Store OP at LOC followed by two-byte integer parameter ARG. */
3712 static void
3713 store_op1 (re_opcode_t op, unsigned char *loc, int arg)
3715 *loc = (unsigned char) op;
3716 STORE_NUMBER (loc + 1, arg);
3720 /* Like `store_op1', but for two two-byte parameters ARG1 and ARG2. */
3722 static void
3723 store_op2 (re_opcode_t op, unsigned char *loc, int arg1, int arg2)
3725 *loc = (unsigned char) op;
3726 STORE_NUMBER (loc + 1, arg1);
3727 STORE_NUMBER (loc + 3, arg2);
3731 /* Copy the bytes from LOC to END to open up three bytes of space at LOC
3732 for OP followed by two-byte integer parameter ARG. */
3734 static void
3735 insert_op1 (re_opcode_t op, unsigned char *loc, int arg, unsigned char *end)
3737 register unsigned char *pfrom = end;
3738 register unsigned char *pto = end + 3;
3740 while (pfrom != loc)
3741 *--pto = *--pfrom;
3743 store_op1 (op, loc, arg);
3747 /* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2. */
3749 static void
3750 insert_op2 (re_opcode_t op, unsigned char *loc, int arg1, int arg2, unsigned char *end)
3752 register unsigned char *pfrom = end;
3753 register unsigned char *pto = end + 5;
3755 while (pfrom != loc)
3756 *--pto = *--pfrom;
3758 store_op2 (op, loc, arg1, arg2);
3762 /* P points to just after a ^ in PATTERN. Return true if that ^ comes
3763 after an alternative or a begin-subexpression. We assume there is at
3764 least one character before the ^. */
3766 static boolean
3767 at_begline_loc_p (const_re_char *pattern, const_re_char *p, reg_syntax_t syntax)
3769 re_char *prev = p - 2;
3770 boolean odd_backslashes;
3772 /* After a subexpression? */
3773 if (*prev == '(')
3774 odd_backslashes = (syntax & RE_NO_BK_PARENS) == 0;
3776 /* After an alternative? */
3777 else if (*prev == '|')
3778 odd_backslashes = (syntax & RE_NO_BK_VBAR) == 0;
3780 /* After a shy subexpression? */
3781 else if (*prev == ':' && (syntax & RE_SHY_GROUPS))
3783 /* Skip over optional regnum. */
3784 while (prev - 1 >= pattern && prev[-1] >= '0' && prev[-1] <= '9')
3785 --prev;
3787 if (!(prev - 2 >= pattern
3788 && prev[-1] == '?' && prev[-2] == '('))
3789 return false;
3790 prev -= 2;
3791 odd_backslashes = (syntax & RE_NO_BK_PARENS) == 0;
3793 else
3794 return false;
3796 /* Count the number of preceding backslashes. */
3797 p = prev;
3798 while (prev - 1 >= pattern && prev[-1] == '\\')
3799 --prev;
3800 return (p - prev) & odd_backslashes;
3804 /* The dual of at_begline_loc_p. This one is for $. We assume there is
3805 at least one character after the $, i.e., `P < PEND'. */
3807 static boolean
3808 at_endline_loc_p (const_re_char *p, const_re_char *pend, reg_syntax_t syntax)
3810 re_char *next = p;
3811 boolean next_backslash = *next == '\\';
3812 re_char *next_next = p + 1 < pend ? p + 1 : 0;
3814 return
3815 /* Before a subexpression? */
3816 (syntax & RE_NO_BK_PARENS ? *next == ')'
3817 : next_backslash && next_next && *next_next == ')')
3818 /* Before an alternative? */
3819 || (syntax & RE_NO_BK_VBAR ? *next == '|'
3820 : next_backslash && next_next && *next_next == '|');
3824 /* Returns true if REGNUM is in one of COMPILE_STACK's elements and
3825 false if it's not. */
3827 static boolean
3828 group_in_compile_stack (compile_stack_type compile_stack, regnum_t regnum)
3830 ssize_t this_element;
3832 for (this_element = compile_stack.avail - 1;
3833 this_element >= 0;
3834 this_element--)
3835 if (compile_stack.stack[this_element].regnum == regnum)
3836 return true;
3838 return false;
3841 /* analyze_first.
3842 If fastmap is non-NULL, go through the pattern and fill fastmap
3843 with all the possible leading chars. If fastmap is NULL, don't
3844 bother filling it up (obviously) and only return whether the
3845 pattern could potentially match the empty string.
3847 Return 1 if p..pend might match the empty string.
3848 Return 0 if p..pend matches at least one char.
3849 Return -1 if fastmap was not updated accurately. */
3851 static int
3852 analyze_first (const_re_char *p, const_re_char *pend, char *fastmap,
3853 const int multibyte)
3855 int j, k;
3856 boolean not;
3858 /* If all elements for base leading-codes in fastmap is set, this
3859 flag is set true. */
3860 boolean match_any_multibyte_characters = false;
3862 assert (p);
3864 /* The loop below works as follows:
3865 - It has a working-list kept in the PATTERN_STACK and which basically
3866 starts by only containing a pointer to the first operation.
3867 - If the opcode we're looking at is a match against some set of
3868 chars, then we add those chars to the fastmap and go on to the
3869 next work element from the worklist (done via `break').
3870 - If the opcode is a control operator on the other hand, we either
3871 ignore it (if it's meaningless at this point, such as `start_memory')
3872 or execute it (if it's a jump). If the jump has several destinations
3873 (i.e. `on_failure_jump'), then we push the other destination onto the
3874 worklist.
3875 We guarantee termination by ignoring backward jumps (more or less),
3876 so that `p' is monotonically increasing. More to the point, we
3877 never set `p' (or push) anything `<= p1'. */
3879 while (p < pend)
3881 /* `p1' is used as a marker of how far back a `on_failure_jump'
3882 can go without being ignored. It is normally equal to `p'
3883 (which prevents any backward `on_failure_jump') except right
3884 after a plain `jump', to allow patterns such as:
3885 0: jump 10
3886 3..9: <body>
3887 10: on_failure_jump 3
3888 as used for the *? operator. */
3889 re_char *p1 = p;
3891 switch (*p++)
3893 case succeed:
3894 return 1;
3896 case duplicate:
3897 /* If the first character has to match a backreference, that means
3898 that the group was empty (since it already matched). Since this
3899 is the only case that interests us here, we can assume that the
3900 backreference must match the empty string. */
3901 p++;
3902 continue;
3905 /* Following are the cases which match a character. These end
3906 with `break'. */
3908 case exactn:
3909 if (fastmap)
3911 /* If multibyte is nonzero, the first byte of each
3912 character is an ASCII or a leading code. Otherwise,
3913 each byte is a character. Thus, this works in both
3914 cases. */
3915 fastmap[p[1]] = 1;
3916 if (! multibyte)
3918 /* For the case of matching this unibyte regex
3919 against multibyte, we must set a leading code of
3920 the corresponding multibyte character. */
3921 int c = RE_CHAR_TO_MULTIBYTE (p[1]);
3923 fastmap[CHAR_LEADING_CODE (c)] = 1;
3926 break;
3929 case anychar:
3930 /* We could put all the chars except for \n (and maybe \0)
3931 but we don't bother since it is generally not worth it. */
3932 if (!fastmap) break;
3933 return -1;
3936 case charset_not:
3937 if (!fastmap) break;
3939 /* Chars beyond end of bitmap are possible matches. */
3940 for (j = CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH;
3941 j < (1 << BYTEWIDTH); j++)
3942 fastmap[j] = 1;
3945 /* Fallthrough */
3946 case charset:
3947 if (!fastmap) break;
3948 not = (re_opcode_t) *(p - 1) == charset_not;
3949 for (j = CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH - 1, p++;
3950 j >= 0; j--)
3951 if (!!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))) ^ not)
3952 fastmap[j] = 1;
3954 #ifdef emacs
3955 if (/* Any leading code can possibly start a character
3956 which doesn't match the specified set of characters. */
3959 /* If we can match a character class, we can match any
3960 multibyte characters. */
3961 (CHARSET_RANGE_TABLE_EXISTS_P (&p[-2])
3962 && CHARSET_RANGE_TABLE_BITS (&p[-2]) != 0))
3965 if (match_any_multibyte_characters == false)
3967 for (j = MIN_MULTIBYTE_LEADING_CODE;
3968 j <= MAX_MULTIBYTE_LEADING_CODE; j++)
3969 fastmap[j] = 1;
3970 match_any_multibyte_characters = true;
3974 else if (!not && CHARSET_RANGE_TABLE_EXISTS_P (&p[-2])
3975 && match_any_multibyte_characters == false)
3977 /* Set fastmap[I] to 1 where I is a leading code of each
3978 multibyte character in the range table. */
3979 int c, count;
3980 unsigned char lc1, lc2;
3982 /* Make P points the range table. `+ 2' is to skip flag
3983 bits for a character class. */
3984 p += CHARSET_BITMAP_SIZE (&p[-2]) + 2;
3986 /* Extract the number of ranges in range table into COUNT. */
3987 EXTRACT_NUMBER_AND_INCR (count, p);
3988 for (; count > 0; count--, p += 3)
3990 /* Extract the start and end of each range. */
3991 EXTRACT_CHARACTER (c, p);
3992 lc1 = CHAR_LEADING_CODE (c);
3993 p += 3;
3994 EXTRACT_CHARACTER (c, p);
3995 lc2 = CHAR_LEADING_CODE (c);
3996 for (j = lc1; j <= lc2; j++)
3997 fastmap[j] = 1;
4000 #endif
4001 break;
4003 case syntaxspec:
4004 case notsyntaxspec:
4005 if (!fastmap) break;
4006 #ifndef emacs
4007 not = (re_opcode_t)p[-1] == notsyntaxspec;
4008 k = *p++;
4009 for (j = 0; j < (1 << BYTEWIDTH); j++)
4010 if ((SYNTAX (j) == (enum syntaxcode) k) ^ not)
4011 fastmap[j] = 1;
4012 break;
4013 #else /* emacs */
4014 /* This match depends on text properties. These end with
4015 aborting optimizations. */
4016 return -1;
4018 case categoryspec:
4019 case notcategoryspec:
4020 if (!fastmap) break;
4021 not = (re_opcode_t)p[-1] == notcategoryspec;
4022 k = *p++;
4023 for (j = (1 << BYTEWIDTH); j >= 0; j--)
4024 if ((CHAR_HAS_CATEGORY (j, k)) ^ not)
4025 fastmap[j] = 1;
4027 /* Any leading code can possibly start a character which
4028 has or doesn't has the specified category. */
4029 if (match_any_multibyte_characters == false)
4031 for (j = MIN_MULTIBYTE_LEADING_CODE;
4032 j <= MAX_MULTIBYTE_LEADING_CODE; j++)
4033 fastmap[j] = 1;
4034 match_any_multibyte_characters = true;
4036 break;
4038 /* All cases after this match the empty string. These end with
4039 `continue'. */
4041 case before_dot:
4042 case at_dot:
4043 case after_dot:
4044 #endif /* !emacs */
4045 case no_op:
4046 case begline:
4047 case endline:
4048 case begbuf:
4049 case endbuf:
4050 case wordbound:
4051 case notwordbound:
4052 case wordbeg:
4053 case wordend:
4054 case symbeg:
4055 case symend:
4056 continue;
4059 case jump:
4060 EXTRACT_NUMBER_AND_INCR (j, p);
4061 if (j < 0)
4062 /* Backward jumps can only go back to code that we've already
4063 visited. `re_compile' should make sure this is true. */
4064 break;
4065 p += j;
4066 switch (*p)
4068 case on_failure_jump:
4069 case on_failure_keep_string_jump:
4070 case on_failure_jump_loop:
4071 case on_failure_jump_nastyloop:
4072 case on_failure_jump_smart:
4073 p++;
4074 break;
4075 default:
4076 continue;
4078 /* Keep `p1' to allow the `on_failure_jump' we are jumping to
4079 to jump back to "just after here". */
4080 /* Fallthrough */
4082 case on_failure_jump:
4083 case on_failure_keep_string_jump:
4084 case on_failure_jump_nastyloop:
4085 case on_failure_jump_loop:
4086 case on_failure_jump_smart:
4087 EXTRACT_NUMBER_AND_INCR (j, p);
4088 if (p + j <= p1)
4089 ; /* Backward jump to be ignored. */
4090 else
4091 { /* We have to look down both arms.
4092 We first go down the "straight" path so as to minimize
4093 stack usage when going through alternatives. */
4094 int r = analyze_first (p, pend, fastmap, multibyte);
4095 if (r) return r;
4096 p += j;
4098 continue;
4101 case jump_n:
4102 /* This code simply does not properly handle forward jump_n. */
4103 DEBUG_STATEMENT (EXTRACT_NUMBER (j, p); assert (j < 0));
4104 p += 4;
4105 /* jump_n can either jump or fall through. The (backward) jump
4106 case has already been handled, so we only need to look at the
4107 fallthrough case. */
4108 continue;
4110 case succeed_n:
4111 /* If N == 0, it should be an on_failure_jump_loop instead. */
4112 DEBUG_STATEMENT (EXTRACT_NUMBER (j, p + 2); assert (j > 0));
4113 p += 4;
4114 /* We only care about one iteration of the loop, so we don't
4115 need to consider the case where this behaves like an
4116 on_failure_jump. */
4117 continue;
4120 case set_number_at:
4121 p += 4;
4122 continue;
4125 case start_memory:
4126 case stop_memory:
4127 p += 1;
4128 continue;
4131 default:
4132 abort (); /* We have listed all the cases. */
4133 } /* switch *p++ */
4135 /* Getting here means we have found the possible starting
4136 characters for one path of the pattern -- and that the empty
4137 string does not match. We need not follow this path further. */
4138 return 0;
4139 } /* while p */
4141 /* We reached the end without matching anything. */
4142 return 1;
4144 } /* analyze_first */
4146 /* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
4147 BUFP. A fastmap records which of the (1 << BYTEWIDTH) possible
4148 characters can start a string that matches the pattern. This fastmap
4149 is used by re_search to skip quickly over impossible starting points.
4151 Character codes above (1 << BYTEWIDTH) are not represented in the
4152 fastmap, but the leading codes are represented. Thus, the fastmap
4153 indicates which character sets could start a match.
4155 The caller must supply the address of a (1 << BYTEWIDTH)-byte data
4156 area as BUFP->fastmap.
4158 We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in
4159 the pattern buffer.
4161 Returns 0 if we succeed, -2 if an internal error. */
4164 re_compile_fastmap (struct re_pattern_buffer *bufp)
4166 char *fastmap = bufp->fastmap;
4167 int analysis;
4169 assert (fastmap && bufp->buffer);
4171 memset (fastmap, 0, 1 << BYTEWIDTH); /* Assume nothing's valid. */
4172 bufp->fastmap_accurate = 1; /* It will be when we're done. */
4174 analysis = analyze_first (bufp->buffer, bufp->buffer + bufp->used,
4175 fastmap, RE_MULTIBYTE_P (bufp));
4176 bufp->can_be_null = (analysis != 0);
4177 return 0;
4178 } /* re_compile_fastmap */
4180 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
4181 ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
4182 this memory for recording register information. STARTS and ENDS
4183 must be allocated using the malloc library routine, and must each
4184 be at least NUM_REGS * sizeof (regoff_t) bytes long.
4186 If NUM_REGS == 0, then subsequent matches should allocate their own
4187 register data.
4189 Unless this function is called, the first search or match using
4190 PATTERN_BUFFER will allocate its own register data, without
4191 freeing the old data. */
4193 void
4194 re_set_registers (struct re_pattern_buffer *bufp, struct re_registers *regs, unsigned int num_regs, regoff_t *starts, regoff_t *ends)
4196 if (num_regs)
4198 bufp->regs_allocated = REGS_REALLOCATE;
4199 regs->num_regs = num_regs;
4200 regs->start = starts;
4201 regs->end = ends;
4203 else
4205 bufp->regs_allocated = REGS_UNALLOCATED;
4206 regs->num_regs = 0;
4207 regs->start = regs->end = 0;
4210 WEAK_ALIAS (__re_set_registers, re_set_registers)
4212 /* Searching routines. */
4214 /* Like re_search_2, below, but only one string is specified, and
4215 doesn't let you say where to stop matching. */
4217 regoff_t
4218 re_search (struct re_pattern_buffer *bufp, const char *string, size_t size,
4219 ssize_t startpos, ssize_t range, struct re_registers *regs)
4221 return re_search_2 (bufp, NULL, 0, string, size, startpos, range,
4222 regs, size);
4224 WEAK_ALIAS (__re_search, re_search)
4226 /* Head address of virtual concatenation of string. */
4227 #define HEAD_ADDR_VSTRING(P) \
4228 (((P) >= size1 ? string2 : string1))
4230 /* Address of POS in the concatenation of virtual string. */
4231 #define POS_ADDR_VSTRING(POS) \
4232 (((POS) >= size1 ? string2 - size1 : string1) + (POS))
4234 /* Using the compiled pattern in BUFP->buffer, first tries to match the
4235 virtual concatenation of STRING1 and STRING2, starting first at index
4236 STARTPOS, then at STARTPOS + 1, and so on.
4238 STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
4240 RANGE is how far to scan while trying to match. RANGE = 0 means try
4241 only at STARTPOS; in general, the last start tried is STARTPOS +
4242 RANGE.
4244 In REGS, return the indices of the virtual concatenation of STRING1
4245 and STRING2 that matched the entire BUFP->buffer and its contained
4246 subexpressions.
4248 Do not consider matching one past the index STOP in the virtual
4249 concatenation of STRING1 and STRING2.
4251 We return either the position in the strings at which the match was
4252 found, -1 if no match, or -2 if error (such as failure
4253 stack overflow). */
4255 regoff_t
4256 re_search_2 (struct re_pattern_buffer *bufp, const char *str1, size_t size1,
4257 const char *str2, size_t size2, ssize_t startpos, ssize_t range,
4258 struct re_registers *regs, ssize_t stop)
4260 regoff_t val;
4261 re_char *string1 = (re_char*) str1;
4262 re_char *string2 = (re_char*) str2;
4263 register char *fastmap = bufp->fastmap;
4264 register RE_TRANSLATE_TYPE translate = bufp->translate;
4265 size_t total_size = size1 + size2;
4266 ssize_t endpos = startpos + range;
4267 boolean anchored_start;
4268 /* Nonzero if we are searching multibyte string. */
4269 const boolean multibyte = RE_TARGET_MULTIBYTE_P (bufp);
4271 /* Check for out-of-range STARTPOS. */
4272 if (startpos < 0 || startpos > total_size)
4273 return -1;
4275 /* Fix up RANGE if it might eventually take us outside
4276 the virtual concatenation of STRING1 and STRING2.
4277 Make sure we won't move STARTPOS below 0 or above TOTAL_SIZE. */
4278 if (endpos < 0)
4279 range = 0 - startpos;
4280 else if (endpos > total_size)
4281 range = total_size - startpos;
4283 /* If the search isn't to be a backwards one, don't waste time in a
4284 search for a pattern anchored at beginning of buffer. */
4285 if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == begbuf && range > 0)
4287 if (startpos > 0)
4288 return -1;
4289 else
4290 range = 0;
4293 #ifdef emacs
4294 /* In a forward search for something that starts with \=.
4295 don't keep searching past point. */
4296 if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == at_dot && range > 0)
4298 range = PT_BYTE - BEGV_BYTE - startpos;
4299 if (range < 0)
4300 return -1;
4302 #endif /* emacs */
4304 /* Update the fastmap now if not correct already. */
4305 if (fastmap && !bufp->fastmap_accurate)
4306 re_compile_fastmap (bufp);
4308 /* See whether the pattern is anchored. */
4309 anchored_start = (bufp->buffer[0] == begline);
4311 #ifdef emacs
4312 gl_state.object = re_match_object; /* Used by SYNTAX_TABLE_BYTE_TO_CHAR. */
4314 ssize_t charpos = SYNTAX_TABLE_BYTE_TO_CHAR (POS_AS_IN_BUFFER (startpos));
4316 SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object, charpos, 1);
4318 #endif
4320 /* Loop through the string, looking for a place to start matching. */
4321 for (;;)
4323 /* If the pattern is anchored,
4324 skip quickly past places we cannot match.
4325 We don't bother to treat startpos == 0 specially
4326 because that case doesn't repeat. */
4327 if (anchored_start && startpos > 0)
4329 if (! ((startpos <= size1 ? string1[startpos - 1]
4330 : string2[startpos - size1 - 1])
4331 == '\n'))
4332 goto advance;
4335 /* If a fastmap is supplied, skip quickly over characters that
4336 cannot be the start of a match. If the pattern can match the
4337 null string, however, we don't need to skip characters; we want
4338 the first null string. */
4339 if (fastmap && startpos < total_size && !bufp->can_be_null)
4341 register re_char *d;
4342 register re_wchar_t buf_ch;
4344 d = POS_ADDR_VSTRING (startpos);
4346 if (range > 0) /* Searching forwards. */
4348 ssize_t irange = range, lim = 0;
4350 if (startpos < size1 && startpos + range >= size1)
4351 lim = range - (size1 - startpos);
4353 /* Written out as an if-else to avoid testing `translate'
4354 inside the loop. */
4355 if (RE_TRANSLATE_P (translate))
4357 if (multibyte)
4358 while (range > lim)
4360 int buf_charlen;
4362 buf_ch = STRING_CHAR_AND_LENGTH (d, buf_charlen);
4363 buf_ch = RE_TRANSLATE (translate, buf_ch);
4364 if (fastmap[CHAR_LEADING_CODE (buf_ch)])
4365 break;
4367 range -= buf_charlen;
4368 d += buf_charlen;
4370 else
4371 while (range > lim)
4373 register re_wchar_t ch, translated;
4375 buf_ch = *d;
4376 ch = RE_CHAR_TO_MULTIBYTE (buf_ch);
4377 translated = RE_TRANSLATE (translate, ch);
4378 if (translated != ch
4379 && (ch = RE_CHAR_TO_UNIBYTE (translated)) >= 0)
4380 buf_ch = ch;
4381 if (fastmap[buf_ch])
4382 break;
4383 d++;
4384 range--;
4387 else
4389 if (multibyte)
4390 while (range > lim)
4392 int buf_charlen;
4394 buf_ch = STRING_CHAR_AND_LENGTH (d, buf_charlen);
4395 if (fastmap[CHAR_LEADING_CODE (buf_ch)])
4396 break;
4397 range -= buf_charlen;
4398 d += buf_charlen;
4400 else
4401 while (range > lim && !fastmap[*d])
4403 d++;
4404 range--;
4407 startpos += irange - range;
4409 else /* Searching backwards. */
4411 if (multibyte)
4413 buf_ch = STRING_CHAR (d);
4414 buf_ch = TRANSLATE (buf_ch);
4415 if (! fastmap[CHAR_LEADING_CODE (buf_ch)])
4416 goto advance;
4418 else
4420 register re_wchar_t ch, translated;
4422 buf_ch = *d;
4423 ch = RE_CHAR_TO_MULTIBYTE (buf_ch);
4424 translated = TRANSLATE (ch);
4425 if (translated != ch
4426 && (ch = RE_CHAR_TO_UNIBYTE (translated)) >= 0)
4427 buf_ch = ch;
4428 if (! fastmap[TRANSLATE (buf_ch)])
4429 goto advance;
4434 /* If can't match the null string, and that's all we have left, fail. */
4435 if (range >= 0 && startpos == total_size && fastmap
4436 && !bufp->can_be_null)
4437 return -1;
4439 val = re_match_2_internal (bufp, string1, size1, string2, size2,
4440 startpos, regs, stop);
4442 if (val >= 0)
4443 return startpos;
4445 if (val == -2)
4446 return -2;
4448 advance:
4449 if (!range)
4450 break;
4451 else if (range > 0)
4453 /* Update STARTPOS to the next character boundary. */
4454 if (multibyte)
4456 re_char *p = POS_ADDR_VSTRING (startpos);
4457 int len = BYTES_BY_CHAR_HEAD (*p);
4459 range -= len;
4460 if (range < 0)
4461 break;
4462 startpos += len;
4464 else
4466 range--;
4467 startpos++;
4470 else
4472 range++;
4473 startpos--;
4475 /* Update STARTPOS to the previous character boundary. */
4476 if (multibyte)
4478 re_char *p = POS_ADDR_VSTRING (startpos) + 1;
4479 re_char *p0 = p;
4480 re_char *phead = HEAD_ADDR_VSTRING (startpos);
4482 /* Find the head of multibyte form. */
4483 PREV_CHAR_BOUNDARY (p, phead);
4484 range += p0 - 1 - p;
4485 if (range > 0)
4486 break;
4488 startpos -= p0 - 1 - p;
4492 return -1;
4493 } /* re_search_2 */
4494 WEAK_ALIAS (__re_search_2, re_search_2)
4496 /* Declarations and macros for re_match_2. */
4498 static int bcmp_translate (re_char *s1, re_char *s2,
4499 register ssize_t len,
4500 RE_TRANSLATE_TYPE translate,
4501 const int multibyte);
4503 /* This converts PTR, a pointer into one of the search strings `string1'
4504 and `string2' into an offset from the beginning of that string. */
4505 #define POINTER_TO_OFFSET(ptr) \
4506 (FIRST_STRING_P (ptr) \
4507 ? (ptr) - string1 \
4508 : (ptr) - string2 + (ptrdiff_t) size1)
4510 /* Call before fetching a character with *d. This switches over to
4511 string2 if necessary.
4512 Check re_match_2_internal for a discussion of why end_match_2 might
4513 not be within string2 (but be equal to end_match_1 instead). */
4514 #define PREFETCH() \
4515 while (d == dend) \
4517 /* End of string2 => fail. */ \
4518 if (dend == end_match_2) \
4519 goto fail; \
4520 /* End of string1 => advance to string2. */ \
4521 d = string2; \
4522 dend = end_match_2; \
4525 /* Call before fetching a char with *d if you already checked other limits.
4526 This is meant for use in lookahead operations like wordend, etc..
4527 where we might need to look at parts of the string that might be
4528 outside of the LIMITs (i.e past `stop'). */
4529 #define PREFETCH_NOLIMIT() \
4530 if (d == end1) \
4532 d = string2; \
4533 dend = end_match_2; \
4536 /* Test if at very beginning or at very end of the virtual concatenation
4537 of `string1' and `string2'. If only one string, it's `string2'. */
4538 #define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2)
4539 #define AT_STRINGS_END(d) ((d) == end2)
4541 /* Disabled due to a compiler bug -- see comment at case wordbound */
4543 /* The comment at case wordbound is following one, but we don't use
4544 AT_WORD_BOUNDARY anymore to support multibyte form.
4546 The DEC Alpha C compiler 3.x generates incorrect code for the
4547 test WORDCHAR_P (d - 1) != WORDCHAR_P (d) in the expansion of
4548 AT_WORD_BOUNDARY, so this code is disabled. Expanding the
4549 macro and introducing temporary variables works around the bug. */
4551 #if 0
4552 /* Test if D points to a character which is word-constituent. We have
4553 two special cases to check for: if past the end of string1, look at
4554 the first character in string2; and if before the beginning of
4555 string2, look at the last character in string1. */
4556 #define WORDCHAR_P(d) \
4557 (SYNTAX ((d) == end1 ? *string2 \
4558 : (d) == string2 - 1 ? *(end1 - 1) : *(d)) \
4559 == Sword)
4561 /* Test if the character before D and the one at D differ with respect
4562 to being word-constituent. */
4563 #define AT_WORD_BOUNDARY(d) \
4564 (AT_STRINGS_BEG (d) || AT_STRINGS_END (d) \
4565 || WORDCHAR_P (d - 1) != WORDCHAR_P (d))
4566 #endif
4568 /* Free everything we malloc. */
4569 #ifdef MATCH_MAY_ALLOCATE
4570 # define FREE_VAR(var) \
4571 do { \
4572 if (var) \
4574 REGEX_FREE (var); \
4575 var = NULL; \
4577 } while (0)
4578 # define FREE_VARIABLES() \
4579 do { \
4580 REGEX_FREE_STACK (fail_stack.stack); \
4581 FREE_VAR (regstart); \
4582 FREE_VAR (regend); \
4583 FREE_VAR (best_regstart); \
4584 FREE_VAR (best_regend); \
4585 REGEX_SAFE_FREE (); \
4586 } while (0)
4587 #else
4588 # define FREE_VARIABLES() ((void)0) /* Do nothing! But inhibit gcc warning. */
4589 #endif /* not MATCH_MAY_ALLOCATE */
4592 /* Optimization routines. */
4594 /* If the operation is a match against one or more chars,
4595 return a pointer to the next operation, else return NULL. */
4596 static re_char *
4597 skip_one_char (const_re_char *p)
4599 switch (*p++)
4601 case anychar:
4602 break;
4604 case exactn:
4605 p += *p + 1;
4606 break;
4608 case charset_not:
4609 case charset:
4610 if (CHARSET_RANGE_TABLE_EXISTS_P (p - 1))
4612 int mcnt;
4613 p = CHARSET_RANGE_TABLE (p - 1);
4614 EXTRACT_NUMBER_AND_INCR (mcnt, p);
4615 p = CHARSET_RANGE_TABLE_END (p, mcnt);
4617 else
4618 p += 1 + CHARSET_BITMAP_SIZE (p - 1);
4619 break;
4621 case syntaxspec:
4622 case notsyntaxspec:
4623 #ifdef emacs
4624 case categoryspec:
4625 case notcategoryspec:
4626 #endif /* emacs */
4627 p++;
4628 break;
4630 default:
4631 p = NULL;
4633 return p;
4637 /* Jump over non-matching operations. */
4638 static re_char *
4639 skip_noops (const_re_char *p, const_re_char *pend)
4641 int mcnt;
4642 while (p < pend)
4644 switch (*p)
4646 case start_memory:
4647 case stop_memory:
4648 p += 2; break;
4649 case no_op:
4650 p += 1; break;
4651 case jump:
4652 p += 1;
4653 EXTRACT_NUMBER_AND_INCR (mcnt, p);
4654 p += mcnt;
4655 break;
4656 default:
4657 return p;
4660 assert (p == pend);
4661 return p;
4664 /* Non-zero if "p1 matches something" implies "p2 fails". */
4665 static int
4666 mutually_exclusive_p (struct re_pattern_buffer *bufp, const_re_char *p1,
4667 const_re_char *p2)
4669 re_opcode_t op2;
4670 const boolean multibyte = RE_MULTIBYTE_P (bufp);
4671 unsigned char *pend = bufp->buffer + bufp->used;
4673 assert (p1 >= bufp->buffer && p1 < pend
4674 && p2 >= bufp->buffer && p2 <= pend);
4676 /* Skip over open/close-group commands.
4677 If what follows this loop is a ...+ construct,
4678 look at what begins its body, since we will have to
4679 match at least one of that. */
4680 p2 = skip_noops (p2, pend);
4681 /* The same skip can be done for p1, except that this function
4682 is only used in the case where p1 is a simple match operator. */
4683 /* p1 = skip_noops (p1, pend); */
4685 assert (p1 >= bufp->buffer && p1 < pend
4686 && p2 >= bufp->buffer && p2 <= pend);
4688 op2 = p2 == pend ? succeed : *p2;
4690 switch (op2)
4692 case succeed:
4693 case endbuf:
4694 /* If we're at the end of the pattern, we can change. */
4695 if (skip_one_char (p1))
4697 DEBUG_PRINT (" End of pattern: fast loop.\n");
4698 return 1;
4700 break;
4702 case endline:
4703 case exactn:
4705 register re_wchar_t c
4706 = (re_opcode_t) *p2 == endline ? '\n'
4707 : RE_STRING_CHAR (p2 + 2, multibyte);
4709 if ((re_opcode_t) *p1 == exactn)
4711 if (c != RE_STRING_CHAR (p1 + 2, multibyte))
4713 DEBUG_PRINT (" '%c' != '%c' => fast loop.\n", c, p1[2]);
4714 return 1;
4718 else if ((re_opcode_t) *p1 == charset
4719 || (re_opcode_t) *p1 == charset_not)
4721 int not = (re_opcode_t) *p1 == charset_not;
4723 /* Test if C is listed in charset (or charset_not)
4724 at `p1'. */
4725 if (! multibyte || IS_REAL_ASCII (c))
4727 if (c < CHARSET_BITMAP_SIZE (p1) * BYTEWIDTH
4728 && p1[2 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
4729 not = !not;
4731 else if (CHARSET_RANGE_TABLE_EXISTS_P (p1))
4732 CHARSET_LOOKUP_RANGE_TABLE (not, c, p1);
4734 /* `not' is equal to 1 if c would match, which means
4735 that we can't change to pop_failure_jump. */
4736 if (!not)
4738 DEBUG_PRINT (" No match => fast loop.\n");
4739 return 1;
4742 else if ((re_opcode_t) *p1 == anychar
4743 && c == '\n')
4745 DEBUG_PRINT (" . != \\n => fast loop.\n");
4746 return 1;
4749 break;
4751 case charset:
4753 if ((re_opcode_t) *p1 == exactn)
4754 /* Reuse the code above. */
4755 return mutually_exclusive_p (bufp, p2, p1);
4757 /* It is hard to list up all the character in charset
4758 P2 if it includes multibyte character. Give up in
4759 such case. */
4760 else if (!multibyte || !CHARSET_RANGE_TABLE_EXISTS_P (p2))
4762 /* Now, we are sure that P2 has no range table.
4763 So, for the size of bitmap in P2, `p2[1]' is
4764 enough. But P1 may have range table, so the
4765 size of bitmap table of P1 is extracted by
4766 using macro `CHARSET_BITMAP_SIZE'.
4768 In a multibyte case, we know that all the character
4769 listed in P2 is ASCII. In a unibyte case, P1 has only a
4770 bitmap table. So, in both cases, it is enough to test
4771 only the bitmap table of P1. */
4773 if ((re_opcode_t) *p1 == charset)
4775 int idx;
4776 /* We win if the charset inside the loop
4777 has no overlap with the one after the loop. */
4778 for (idx = 0;
4779 (idx < (int) p2[1]
4780 && idx < CHARSET_BITMAP_SIZE (p1));
4781 idx++)
4782 if ((p2[2 + idx] & p1[2 + idx]) != 0)
4783 break;
4785 if (idx == p2[1]
4786 || idx == CHARSET_BITMAP_SIZE (p1))
4788 DEBUG_PRINT (" No match => fast loop.\n");
4789 return 1;
4792 else if ((re_opcode_t) *p1 == charset_not)
4794 int idx;
4795 /* We win if the charset_not inside the loop lists
4796 every character listed in the charset after. */
4797 for (idx = 0; idx < (int) p2[1]; idx++)
4798 if (! (p2[2 + idx] == 0
4799 || (idx < CHARSET_BITMAP_SIZE (p1)
4800 && ((p2[2 + idx] & ~ p1[2 + idx]) == 0))))
4801 break;
4803 if (idx == p2[1])
4805 DEBUG_PRINT (" No match => fast loop.\n");
4806 return 1;
4811 break;
4813 case charset_not:
4814 switch (*p1)
4816 case exactn:
4817 case charset:
4818 /* Reuse the code above. */
4819 return mutually_exclusive_p (bufp, p2, p1);
4820 case charset_not:
4821 /* When we have two charset_not, it's very unlikely that
4822 they don't overlap. The union of the two sets of excluded
4823 chars should cover all possible chars, which, as a matter of
4824 fact, is virtually impossible in multibyte buffers. */
4825 break;
4827 break;
4829 case wordend:
4830 return ((re_opcode_t) *p1 == syntaxspec && p1[1] == Sword);
4831 case symend:
4832 return ((re_opcode_t) *p1 == syntaxspec
4833 && (p1[1] == Ssymbol || p1[1] == Sword));
4834 case notsyntaxspec:
4835 return ((re_opcode_t) *p1 == syntaxspec && p1[1] == p2[1]);
4837 case wordbeg:
4838 return ((re_opcode_t) *p1 == notsyntaxspec && p1[1] == Sword);
4839 case symbeg:
4840 return ((re_opcode_t) *p1 == notsyntaxspec
4841 && (p1[1] == Ssymbol || p1[1] == Sword));
4842 case syntaxspec:
4843 return ((re_opcode_t) *p1 == notsyntaxspec && p1[1] == p2[1]);
4845 case wordbound:
4846 return (((re_opcode_t) *p1 == notsyntaxspec
4847 || (re_opcode_t) *p1 == syntaxspec)
4848 && p1[1] == Sword);
4850 #ifdef emacs
4851 case categoryspec:
4852 return ((re_opcode_t) *p1 == notcategoryspec && p1[1] == p2[1]);
4853 case notcategoryspec:
4854 return ((re_opcode_t) *p1 == categoryspec && p1[1] == p2[1]);
4855 #endif /* emacs */
4857 default:
4861 /* Safe default. */
4862 return 0;
4866 /* Matching routines. */
4868 #ifndef emacs /* Emacs never uses this. */
4869 /* re_match is like re_match_2 except it takes only a single string. */
4871 regoff_t
4872 re_match (struct re_pattern_buffer *bufp, const char *string,
4873 size_t size, ssize_t pos, struct re_registers *regs)
4875 regoff_t result = re_match_2_internal (bufp, NULL, 0, (re_char*) string,
4876 size, pos, regs, size);
4877 return result;
4879 WEAK_ALIAS (__re_match, re_match)
4880 #endif /* not emacs */
4882 #ifdef emacs
4883 /* In Emacs, this is the string or buffer in which we
4884 are matching. It is used for looking up syntax properties. */
4885 Lisp_Object re_match_object;
4886 #endif
4888 /* re_match_2 matches the compiled pattern in BUFP against the
4889 the (virtual) concatenation of STRING1 and STRING2 (of length SIZE1
4890 and SIZE2, respectively). We start matching at POS, and stop
4891 matching at STOP.
4893 If REGS is non-null and the `no_sub' field of BUFP is nonzero, we
4894 store offsets for the substring each group matched in REGS. See the
4895 documentation for exactly how many groups we fill.
4897 We return -1 if no match, -2 if an internal error (such as the
4898 failure stack overflowing). Otherwise, we return the length of the
4899 matched substring. */
4901 regoff_t
4902 re_match_2 (struct re_pattern_buffer *bufp, const char *string1,
4903 size_t size1, const char *string2, size_t size2, ssize_t pos,
4904 struct re_registers *regs, ssize_t stop)
4906 regoff_t result;
4908 #ifdef emacs
4909 ssize_t charpos;
4910 gl_state.object = re_match_object; /* Used by SYNTAX_TABLE_BYTE_TO_CHAR. */
4911 charpos = SYNTAX_TABLE_BYTE_TO_CHAR (POS_AS_IN_BUFFER (pos));
4912 SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object, charpos, 1);
4913 #endif
4915 result = re_match_2_internal (bufp, (re_char*) string1, size1,
4916 (re_char*) string2, size2,
4917 pos, regs, stop);
4918 return result;
4920 WEAK_ALIAS (__re_match_2, re_match_2)
4923 /* This is a separate function so that we can force an alloca cleanup
4924 afterwards. */
4925 static regoff_t
4926 re_match_2_internal (struct re_pattern_buffer *bufp, const_re_char *string1,
4927 size_t size1, const_re_char *string2, size_t size2,
4928 ssize_t pos, struct re_registers *regs, ssize_t stop)
4930 /* General temporaries. */
4931 int mcnt;
4932 size_t reg;
4934 /* Just past the end of the corresponding string. */
4935 re_char *end1, *end2;
4937 /* Pointers into string1 and string2, just past the last characters in
4938 each to consider matching. */
4939 re_char *end_match_1, *end_match_2;
4941 /* Where we are in the data, and the end of the current string. */
4942 re_char *d, *dend;
4944 /* Used sometimes to remember where we were before starting matching
4945 an operator so that we can go back in case of failure. This "atomic"
4946 behavior of matching opcodes is indispensable to the correctness
4947 of the on_failure_keep_string_jump optimization. */
4948 re_char *dfail;
4950 /* Where we are in the pattern, and the end of the pattern. */
4951 re_char *p = bufp->buffer;
4952 re_char *pend = p + bufp->used;
4954 /* We use this to map every character in the string. */
4955 RE_TRANSLATE_TYPE translate = bufp->translate;
4957 /* Nonzero if BUFP is setup from a multibyte regex. */
4958 const boolean multibyte = RE_MULTIBYTE_P (bufp);
4960 /* Nonzero if STRING1/STRING2 are multibyte. */
4961 const boolean target_multibyte = RE_TARGET_MULTIBYTE_P (bufp);
4963 /* Failure point stack. Each place that can handle a failure further
4964 down the line pushes a failure point on this stack. It consists of
4965 regstart, and regend for all registers corresponding to
4966 the subexpressions we're currently inside, plus the number of such
4967 registers, and, finally, two char *'s. The first char * is where
4968 to resume scanning the pattern; the second one is where to resume
4969 scanning the strings. */
4970 #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
4971 fail_stack_type fail_stack;
4972 #endif
4973 #ifdef DEBUG_COMPILES_ARGUMENTS
4974 unsigned nfailure_points_pushed = 0, nfailure_points_popped = 0;
4975 #endif
4977 #if defined REL_ALLOC && defined REGEX_MALLOC
4978 /* This holds the pointer to the failure stack, when
4979 it is allocated relocatably. */
4980 fail_stack_elt_t *failure_stack_ptr;
4981 #endif
4983 /* We fill all the registers internally, independent of what we
4984 return, for use in backreferences. The number here includes
4985 an element for register zero. */
4986 size_t num_regs = bufp->re_nsub + 1;
4988 /* Information on the contents of registers. These are pointers into
4989 the input strings; they record just what was matched (on this
4990 attempt) by a subexpression part of the pattern, that is, the
4991 regnum-th regstart pointer points to where in the pattern we began
4992 matching and the regnum-th regend points to right after where we
4993 stopped matching the regnum-th subexpression. (The zeroth register
4994 keeps track of what the whole pattern matches.) */
4995 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
4996 re_char **regstart, **regend;
4997 #endif
4999 /* The following record the register info as found in the above
5000 variables when we find a match better than any we've seen before.
5001 This happens as we backtrack through the failure points, which in
5002 turn happens only if we have not yet matched the entire string. */
5003 unsigned best_regs_set = false;
5004 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
5005 re_char **best_regstart, **best_regend;
5006 #endif
5008 /* Logically, this is `best_regend[0]'. But we don't want to have to
5009 allocate space for that if we're not allocating space for anything
5010 else (see below). Also, we never need info about register 0 for
5011 any of the other register vectors, and it seems rather a kludge to
5012 treat `best_regend' differently than the rest. So we keep track of
5013 the end of the best match so far in a separate variable. We
5014 initialize this to NULL so that when we backtrack the first time
5015 and need to test it, it's not garbage. */
5016 re_char *match_end = NULL;
5018 #ifdef DEBUG_COMPILES_ARGUMENTS
5019 /* Counts the total number of registers pushed. */
5020 unsigned num_regs_pushed = 0;
5021 #endif
5023 DEBUG_PRINT ("\n\nEntering re_match_2.\n");
5025 REGEX_USE_SAFE_ALLOCA;
5027 INIT_FAIL_STACK ();
5029 #ifdef MATCH_MAY_ALLOCATE
5030 /* Do not bother to initialize all the register variables if there are
5031 no groups in the pattern, as it takes a fair amount of time. If
5032 there are groups, we include space for register 0 (the whole
5033 pattern), even though we never use it, since it simplifies the
5034 array indexing. We should fix this. */
5035 if (bufp->re_nsub)
5037 regstart = REGEX_TALLOC (num_regs, re_char *);
5038 regend = REGEX_TALLOC (num_regs, re_char *);
5039 best_regstart = REGEX_TALLOC (num_regs, re_char *);
5040 best_regend = REGEX_TALLOC (num_regs, re_char *);
5042 if (!(regstart && regend && best_regstart && best_regend))
5044 FREE_VARIABLES ();
5045 return -2;
5048 else
5050 /* We must initialize all our variables to NULL, so that
5051 `FREE_VARIABLES' doesn't try to free them. */
5052 regstart = regend = best_regstart = best_regend = NULL;
5054 #endif /* MATCH_MAY_ALLOCATE */
5056 /* The starting position is bogus. */
5057 if (pos < 0 || pos > size1 + size2)
5059 FREE_VARIABLES ();
5060 return -1;
5063 /* Initialize subexpression text positions to -1 to mark ones that no
5064 start_memory/stop_memory has been seen for. Also initialize the
5065 register information struct. */
5066 for (reg = 1; reg < num_regs; reg++)
5067 regstart[reg] = regend[reg] = NULL;
5069 /* We move `string1' into `string2' if the latter's empty -- but not if
5070 `string1' is null. */
5071 if (size2 == 0 && string1 != NULL)
5073 string2 = string1;
5074 size2 = size1;
5075 string1 = 0;
5076 size1 = 0;
5078 end1 = string1 + size1;
5079 end2 = string2 + size2;
5081 /* `p' scans through the pattern as `d' scans through the data.
5082 `dend' is the end of the input string that `d' points within. `d'
5083 is advanced into the following input string whenever necessary, but
5084 this happens before fetching; therefore, at the beginning of the
5085 loop, `d' can be pointing at the end of a string, but it cannot
5086 equal `string2'. */
5087 if (pos >= size1)
5089 /* Only match within string2. */
5090 d = string2 + pos - size1;
5091 dend = end_match_2 = string2 + stop - size1;
5092 end_match_1 = end1; /* Just to give it a value. */
5094 else
5096 if (stop < size1)
5098 /* Only match within string1. */
5099 end_match_1 = string1 + stop;
5100 /* BEWARE!
5101 When we reach end_match_1, PREFETCH normally switches to string2.
5102 But in the present case, this means that just doing a PREFETCH
5103 makes us jump from `stop' to `gap' within the string.
5104 What we really want here is for the search to stop as
5105 soon as we hit end_match_1. That's why we set end_match_2
5106 to end_match_1 (since PREFETCH fails as soon as we hit
5107 end_match_2). */
5108 end_match_2 = end_match_1;
5110 else
5111 { /* It's important to use this code when stop == size so that
5112 moving `d' from end1 to string2 will not prevent the d == dend
5113 check from catching the end of string. */
5114 end_match_1 = end1;
5115 end_match_2 = string2 + stop - size1;
5117 d = string1 + pos;
5118 dend = end_match_1;
5121 DEBUG_PRINT ("The compiled pattern is: ");
5122 DEBUG_PRINT_COMPILED_PATTERN (bufp, p, pend);
5123 DEBUG_PRINT ("The string to match is: \"");
5124 DEBUG_PRINT_DOUBLE_STRING (d, string1, size1, string2, size2);
5125 DEBUG_PRINT ("\"\n");
5127 /* This loops over pattern commands. It exits by returning from the
5128 function if the match is complete, or it drops through if the match
5129 fails at this starting point in the input data. */
5130 for (;;)
5132 DEBUG_PRINT ("\n%p: ", p);
5134 if (p == pend)
5136 /* End of pattern means we might have succeeded. */
5137 DEBUG_PRINT ("end of pattern ... ");
5139 /* If we haven't matched the entire string, and we want the
5140 longest match, try backtracking. */
5141 if (d != end_match_2)
5143 /* True if this match is the best seen so far. */
5144 bool best_match_p;
5147 /* True if this match ends in the same string (string1
5148 or string2) as the best previous match. */
5149 bool same_str_p = (FIRST_STRING_P (match_end)
5150 == FIRST_STRING_P (d));
5152 /* AIX compiler got confused when this was combined
5153 with the previous declaration. */
5154 if (same_str_p)
5155 best_match_p = d > match_end;
5156 else
5157 best_match_p = !FIRST_STRING_P (d);
5160 DEBUG_PRINT ("backtracking.\n");
5162 if (!FAIL_STACK_EMPTY ())
5163 { /* More failure points to try. */
5165 /* If exceeds best match so far, save it. */
5166 if (!best_regs_set || best_match_p)
5168 best_regs_set = true;
5169 match_end = d;
5171 DEBUG_PRINT ("\nSAVING match as best so far.\n");
5173 for (reg = 1; reg < num_regs; reg++)
5175 best_regstart[reg] = regstart[reg];
5176 best_regend[reg] = regend[reg];
5179 goto fail;
5182 /* If no failure points, don't restore garbage. And if
5183 last match is real best match, don't restore second
5184 best one. */
5185 else if (best_regs_set && !best_match_p)
5187 restore_best_regs:
5188 /* Restore best match. It may happen that `dend ==
5189 end_match_1' while the restored d is in string2.
5190 For example, the pattern `x.*y.*z' against the
5191 strings `x-' and `y-z-', if the two strings are
5192 not consecutive in memory. */
5193 DEBUG_PRINT ("Restoring best registers.\n");
5195 d = match_end;
5196 dend = ((d >= string1 && d <= end1)
5197 ? end_match_1 : end_match_2);
5199 for (reg = 1; reg < num_regs; reg++)
5201 regstart[reg] = best_regstart[reg];
5202 regend[reg] = best_regend[reg];
5205 } /* d != end_match_2 */
5207 succeed_label:
5208 DEBUG_PRINT ("Accepting match.\n");
5210 /* If caller wants register contents data back, do it. */
5211 if (regs && !bufp->no_sub)
5213 /* Have the register data arrays been allocated? */
5214 if (bufp->regs_allocated == REGS_UNALLOCATED)
5215 { /* No. So allocate them with malloc. We need one
5216 extra element beyond `num_regs' for the `-1' marker
5217 GNU code uses. */
5218 regs->num_regs = max (RE_NREGS, num_regs + 1);
5219 regs->start = TALLOC (regs->num_regs, regoff_t);
5220 regs->end = TALLOC (regs->num_regs, regoff_t);
5221 if (regs->start == NULL || regs->end == NULL)
5223 FREE_VARIABLES ();
5224 return -2;
5226 bufp->regs_allocated = REGS_REALLOCATE;
5228 else if (bufp->regs_allocated == REGS_REALLOCATE)
5229 { /* Yes. If we need more elements than were already
5230 allocated, reallocate them. If we need fewer, just
5231 leave it alone. */
5232 if (regs->num_regs < num_regs + 1)
5234 regs->num_regs = num_regs + 1;
5235 RETALLOC (regs->start, regs->num_regs, regoff_t);
5236 RETALLOC (regs->end, regs->num_regs, regoff_t);
5237 if (regs->start == NULL || regs->end == NULL)
5239 FREE_VARIABLES ();
5240 return -2;
5244 else
5246 /* These braces fend off a "empty body in an else-statement"
5247 warning under GCC when assert expands to nothing. */
5248 assert (bufp->regs_allocated == REGS_FIXED);
5251 /* Convert the pointer data in `regstart' and `regend' to
5252 indices. Register zero has to be set differently,
5253 since we haven't kept track of any info for it. */
5254 if (regs->num_regs > 0)
5256 regs->start[0] = pos;
5257 regs->end[0] = POINTER_TO_OFFSET (d);
5260 /* Go through the first `min (num_regs, regs->num_regs)'
5261 registers, since that is all we initialized. */
5262 for (reg = 1; reg < min (num_regs, regs->num_regs); reg++)
5264 if (REG_UNSET (regstart[reg]) || REG_UNSET (regend[reg]))
5265 regs->start[reg] = regs->end[reg] = -1;
5266 else
5268 regs->start[reg] = POINTER_TO_OFFSET (regstart[reg]);
5269 regs->end[reg] = POINTER_TO_OFFSET (regend[reg]);
5273 /* If the regs structure we return has more elements than
5274 were in the pattern, set the extra elements to -1. If
5275 we (re)allocated the registers, this is the case,
5276 because we always allocate enough to have at least one
5277 -1 at the end. */
5278 for (reg = num_regs; reg < regs->num_regs; reg++)
5279 regs->start[reg] = regs->end[reg] = -1;
5280 } /* regs && !bufp->no_sub */
5282 DEBUG_PRINT ("%u failure points pushed, %u popped (%u remain).\n",
5283 nfailure_points_pushed, nfailure_points_popped,
5284 nfailure_points_pushed - nfailure_points_popped);
5285 DEBUG_PRINT ("%u registers pushed.\n", num_regs_pushed);
5287 ptrdiff_t dcnt = POINTER_TO_OFFSET (d) - pos;
5289 DEBUG_PRINT ("Returning %td from re_match_2.\n", dcnt);
5291 FREE_VARIABLES ();
5292 return dcnt;
5295 /* Otherwise match next pattern command. */
5296 switch (*p++)
5298 /* Ignore these. Used to ignore the n of succeed_n's which
5299 currently have n == 0. */
5300 case no_op:
5301 DEBUG_PRINT ("EXECUTING no_op.\n");
5302 break;
5304 case succeed:
5305 DEBUG_PRINT ("EXECUTING succeed.\n");
5306 goto succeed_label;
5308 /* Match the next n pattern characters exactly. The following
5309 byte in the pattern defines n, and the n bytes after that
5310 are the characters to match. */
5311 case exactn:
5312 mcnt = *p++;
5313 DEBUG_PRINT ("EXECUTING exactn %d.\n", mcnt);
5315 /* Remember the start point to rollback upon failure. */
5316 dfail = d;
5318 #ifndef emacs
5319 /* This is written out as an if-else so we don't waste time
5320 testing `translate' inside the loop. */
5321 if (RE_TRANSLATE_P (translate))
5324 PREFETCH ();
5325 if (RE_TRANSLATE (translate, *d) != *p++)
5327 d = dfail;
5328 goto fail;
5330 d++;
5332 while (--mcnt);
5333 else
5336 PREFETCH ();
5337 if (*d++ != *p++)
5339 d = dfail;
5340 goto fail;
5343 while (--mcnt);
5344 #else /* emacs */
5345 /* The cost of testing `translate' is comparatively small. */
5346 if (target_multibyte)
5349 int pat_charlen, buf_charlen;
5350 int pat_ch, buf_ch;
5352 PREFETCH ();
5353 if (multibyte)
5354 pat_ch = STRING_CHAR_AND_LENGTH (p, pat_charlen);
5355 else
5357 pat_ch = RE_CHAR_TO_MULTIBYTE (*p);
5358 pat_charlen = 1;
5360 buf_ch = STRING_CHAR_AND_LENGTH (d, buf_charlen);
5362 if (TRANSLATE (buf_ch) != pat_ch)
5364 d = dfail;
5365 goto fail;
5368 p += pat_charlen;
5369 d += buf_charlen;
5370 mcnt -= pat_charlen;
5372 while (mcnt > 0);
5373 else
5376 int pat_charlen;
5377 int pat_ch, buf_ch;
5379 PREFETCH ();
5380 if (multibyte)
5382 pat_ch = STRING_CHAR_AND_LENGTH (p, pat_charlen);
5383 pat_ch = RE_CHAR_TO_UNIBYTE (pat_ch);
5385 else
5387 pat_ch = *p;
5388 pat_charlen = 1;
5390 buf_ch = RE_CHAR_TO_MULTIBYTE (*d);
5391 if (! CHAR_BYTE8_P (buf_ch))
5393 buf_ch = TRANSLATE (buf_ch);
5394 buf_ch = RE_CHAR_TO_UNIBYTE (buf_ch);
5395 if (buf_ch < 0)
5396 buf_ch = *d;
5398 else
5399 buf_ch = *d;
5400 if (buf_ch != pat_ch)
5402 d = dfail;
5403 goto fail;
5405 p += pat_charlen;
5406 d++;
5408 while (--mcnt);
5409 #endif
5410 break;
5413 /* Match any character except possibly a newline or a null. */
5414 case anychar:
5416 int buf_charlen;
5417 re_wchar_t buf_ch;
5419 DEBUG_PRINT ("EXECUTING anychar.\n");
5421 PREFETCH ();
5422 buf_ch = RE_STRING_CHAR_AND_LENGTH (d, buf_charlen,
5423 target_multibyte);
5424 buf_ch = TRANSLATE (buf_ch);
5426 if ((!(bufp->syntax & RE_DOT_NEWLINE)
5427 && buf_ch == '\n')
5428 || ((bufp->syntax & RE_DOT_NOT_NULL)
5429 && buf_ch == '\000'))
5430 goto fail;
5432 DEBUG_PRINT (" Matched \"%d\".\n", *d);
5433 d += buf_charlen;
5435 break;
5438 case charset:
5439 case charset_not:
5441 register unsigned int c, corig;
5442 boolean not = (re_opcode_t) *(p - 1) == charset_not;
5443 int len;
5445 /* Start of actual range_table, or end of bitmap if there is no
5446 range table. */
5447 re_char *range_table UNINIT;
5449 /* Nonzero if there is a range table. */
5450 int range_table_exists;
5452 /* Number of ranges of range table. This is not included
5453 in the initial byte-length of the command. */
5454 int count = 0;
5456 /* Whether matching against a unibyte character. */
5457 boolean unibyte_char = false;
5459 DEBUG_PRINT ("EXECUTING charset%s.\n", not ? "_not" : "");
5461 range_table_exists = CHARSET_RANGE_TABLE_EXISTS_P (&p[-1]);
5463 if (range_table_exists)
5465 range_table = CHARSET_RANGE_TABLE (&p[-1]); /* Past the bitmap. */
5466 EXTRACT_NUMBER_AND_INCR (count, range_table);
5469 PREFETCH ();
5470 corig = c = RE_STRING_CHAR_AND_LENGTH (d, len, target_multibyte);
5471 if (target_multibyte)
5473 int c1;
5475 c = TRANSLATE (c);
5476 c1 = RE_CHAR_TO_UNIBYTE (c);
5477 if (c1 >= 0)
5479 unibyte_char = true;
5480 c = c1;
5483 else
5485 int c1 = RE_CHAR_TO_MULTIBYTE (c);
5487 if (! CHAR_BYTE8_P (c1))
5489 c1 = TRANSLATE (c1);
5490 c1 = RE_CHAR_TO_UNIBYTE (c1);
5491 if (c1 >= 0)
5493 unibyte_char = true;
5494 c = c1;
5497 else
5498 unibyte_char = true;
5501 if (unibyte_char && c < (1 << BYTEWIDTH))
5502 { /* Lookup bitmap. */
5503 /* Cast to `unsigned' instead of `unsigned char' in
5504 case the bit list is a full 32 bytes long. */
5505 if (c < (unsigned) (CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH)
5506 && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
5507 not = !not;
5509 #ifdef emacs
5510 else if (range_table_exists)
5512 int class_bits = CHARSET_RANGE_TABLE_BITS (&p[-1]);
5514 if ( (class_bits & BIT_LOWER
5515 && (ISLOWER (c)
5516 || (corig != c
5517 && c == upcase (corig) && ISUPPER(c))))
5518 | (class_bits & BIT_MULTIBYTE)
5519 | (class_bits & BIT_PUNCT && ISPUNCT (c))
5520 | (class_bits & BIT_SPACE && ISSPACE (c))
5521 | (class_bits & BIT_UPPER
5522 && (ISUPPER (c)
5523 || (corig != c
5524 && c == downcase (corig) && ISLOWER (c))))
5525 | (class_bits & BIT_WORD && ISWORD (c))
5526 | (class_bits & BIT_ALPHA && ISALPHA (c))
5527 | (class_bits & BIT_ALNUM && ISALNUM (c))
5528 | (class_bits & BIT_GRAPH && ISGRAPH (c))
5529 | (class_bits & BIT_PRINT && ISPRINT (c)))
5530 not = !not;
5531 else
5532 CHARSET_LOOKUP_RANGE_TABLE_RAW (not, c, range_table, count);
5534 #endif /* emacs */
5536 if (range_table_exists)
5537 p = CHARSET_RANGE_TABLE_END (range_table, count);
5538 else
5539 p += CHARSET_BITMAP_SIZE (&p[-1]) + 1;
5541 if (!not) goto fail;
5543 d += len;
5545 break;
5548 /* The beginning of a group is represented by start_memory.
5549 The argument is the register number. The text
5550 matched within the group is recorded (in the internal
5551 registers data structure) under the register number. */
5552 case start_memory:
5553 DEBUG_PRINT ("EXECUTING start_memory %d:\n", *p);
5555 /* In case we need to undo this operation (via backtracking). */
5556 PUSH_FAILURE_REG (*p);
5558 regstart[*p] = d;
5559 regend[*p] = NULL; /* probably unnecessary. -sm */
5560 DEBUG_PRINT (" regstart: %td\n", POINTER_TO_OFFSET (regstart[*p]));
5562 /* Move past the register number and inner group count. */
5563 p += 1;
5564 break;
5567 /* The stop_memory opcode represents the end of a group. Its
5568 argument is the same as start_memory's: the register number. */
5569 case stop_memory:
5570 DEBUG_PRINT ("EXECUTING stop_memory %d:\n", *p);
5572 assert (!REG_UNSET (regstart[*p]));
5573 /* Strictly speaking, there should be code such as:
5575 assert (REG_UNSET (regend[*p]));
5576 PUSH_FAILURE_REGSTOP ((unsigned int)*p);
5578 But the only info to be pushed is regend[*p] and it is known to
5579 be UNSET, so there really isn't anything to push.
5580 Not pushing anything, on the other hand deprives us from the
5581 guarantee that regend[*p] is UNSET since undoing this operation
5582 will not reset its value properly. This is not important since
5583 the value will only be read on the next start_memory or at
5584 the very end and both events can only happen if this stop_memory
5585 is *not* undone. */
5587 regend[*p] = d;
5588 DEBUG_PRINT (" regend: %td\n", POINTER_TO_OFFSET (regend[*p]));
5590 /* Move past the register number and the inner group count. */
5591 p += 1;
5592 break;
5595 /* \<digit> has been turned into a `duplicate' command which is
5596 followed by the numeric value of <digit> as the register number. */
5597 case duplicate:
5599 register re_char *d2, *dend2;
5600 int regno = *p++; /* Get which register to match against. */
5601 DEBUG_PRINT ("EXECUTING duplicate %d.\n", regno);
5603 /* Can't back reference a group which we've never matched. */
5604 if (REG_UNSET (regstart[regno]) || REG_UNSET (regend[regno]))
5605 goto fail;
5607 /* Where in input to try to start matching. */
5608 d2 = regstart[regno];
5610 /* Remember the start point to rollback upon failure. */
5611 dfail = d;
5613 /* Where to stop matching; if both the place to start and
5614 the place to stop matching are in the same string, then
5615 set to the place to stop, otherwise, for now have to use
5616 the end of the first string. */
5618 dend2 = ((FIRST_STRING_P (regstart[regno])
5619 == FIRST_STRING_P (regend[regno]))
5620 ? regend[regno] : end_match_1);
5621 for (;;)
5623 ptrdiff_t dcnt;
5625 /* If necessary, advance to next segment in register
5626 contents. */
5627 while (d2 == dend2)
5629 if (dend2 == end_match_2) break;
5630 if (dend2 == regend[regno]) break;
5632 /* End of string1 => advance to string2. */
5633 d2 = string2;
5634 dend2 = regend[regno];
5636 /* At end of register contents => success */
5637 if (d2 == dend2) break;
5639 /* If necessary, advance to next segment in data. */
5640 PREFETCH ();
5642 /* How many characters left in this segment to match. */
5643 dcnt = dend - d;
5645 /* Want how many consecutive characters we can match in
5646 one shot, so, if necessary, adjust the count. */
5647 if (dcnt > dend2 - d2)
5648 dcnt = dend2 - d2;
5650 /* Compare that many; failure if mismatch, else move
5651 past them. */
5652 if (RE_TRANSLATE_P (translate)
5653 ? bcmp_translate (d, d2, dcnt, translate, target_multibyte)
5654 : memcmp (d, d2, dcnt))
5656 d = dfail;
5657 goto fail;
5659 d += dcnt, d2 += dcnt;
5662 break;
5665 /* begline matches the empty string at the beginning of the string
5666 (unless `not_bol' is set in `bufp'), and after newlines. */
5667 case begline:
5668 DEBUG_PRINT ("EXECUTING begline.\n");
5670 if (AT_STRINGS_BEG (d))
5672 if (!bufp->not_bol) break;
5674 else
5676 unsigned c;
5677 GET_CHAR_BEFORE_2 (c, d, string1, end1, string2, end2);
5678 if (c == '\n')
5679 break;
5681 /* In all other cases, we fail. */
5682 goto fail;
5685 /* endline is the dual of begline. */
5686 case endline:
5687 DEBUG_PRINT ("EXECUTING endline.\n");
5689 if (AT_STRINGS_END (d))
5691 if (!bufp->not_eol) break;
5693 else
5695 PREFETCH_NOLIMIT ();
5696 if (*d == '\n')
5697 break;
5699 goto fail;
5702 /* Match at the very beginning of the data. */
5703 case begbuf:
5704 DEBUG_PRINT ("EXECUTING begbuf.\n");
5705 if (AT_STRINGS_BEG (d))
5706 break;
5707 goto fail;
5710 /* Match at the very end of the data. */
5711 case endbuf:
5712 DEBUG_PRINT ("EXECUTING endbuf.\n");
5713 if (AT_STRINGS_END (d))
5714 break;
5715 goto fail;
5718 /* on_failure_keep_string_jump is used to optimize `.*\n'. It
5719 pushes NULL as the value for the string on the stack. Then
5720 `POP_FAILURE_POINT' will keep the current value for the
5721 string, instead of restoring it. To see why, consider
5722 matching `foo\nbar' against `.*\n'. The .* matches the foo;
5723 then the . fails against the \n. But the next thing we want
5724 to do is match the \n against the \n; if we restored the
5725 string value, we would be back at the foo.
5727 Because this is used only in specific cases, we don't need to
5728 check all the things that `on_failure_jump' does, to make
5729 sure the right things get saved on the stack. Hence we don't
5730 share its code. The only reason to push anything on the
5731 stack at all is that otherwise we would have to change
5732 `anychar's code to do something besides goto fail in this
5733 case; that seems worse than this. */
5734 case on_failure_keep_string_jump:
5735 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5736 DEBUG_PRINT ("EXECUTING on_failure_keep_string_jump %d (to %p):\n",
5737 mcnt, p + mcnt);
5739 PUSH_FAILURE_POINT (p - 3, NULL);
5740 break;
5742 /* A nasty loop is introduced by the non-greedy *? and +?.
5743 With such loops, the stack only ever contains one failure point
5744 at a time, so that a plain on_failure_jump_loop kind of
5745 cycle detection cannot work. Worse yet, such a detection
5746 can not only fail to detect a cycle, but it can also wrongly
5747 detect a cycle (between different instantiations of the same
5748 loop).
5749 So the method used for those nasty loops is a little different:
5750 We use a special cycle-detection-stack-frame which is pushed
5751 when the on_failure_jump_nastyloop failure-point is *popped*.
5752 This special frame thus marks the beginning of one iteration
5753 through the loop and we can hence easily check right here
5754 whether something matched between the beginning and the end of
5755 the loop. */
5756 case on_failure_jump_nastyloop:
5757 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5758 DEBUG_PRINT ("EXECUTING on_failure_jump_nastyloop %d (to %p):\n",
5759 mcnt, p + mcnt);
5761 assert ((re_opcode_t)p[-4] == no_op);
5763 int cycle = 0;
5764 CHECK_INFINITE_LOOP (p - 4, d);
5765 if (!cycle)
5766 /* If there's a cycle, just continue without pushing
5767 this failure point. The failure point is the "try again"
5768 option, which shouldn't be tried.
5769 We want (x?)*?y\1z to match both xxyz and xxyxz. */
5770 PUSH_FAILURE_POINT (p - 3, d);
5772 break;
5774 /* Simple loop detecting on_failure_jump: just check on the
5775 failure stack if the same spot was already hit earlier. */
5776 case on_failure_jump_loop:
5777 on_failure:
5778 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5779 DEBUG_PRINT ("EXECUTING on_failure_jump_loop %d (to %p):\n",
5780 mcnt, p + mcnt);
5782 int cycle = 0;
5783 CHECK_INFINITE_LOOP (p - 3, d);
5784 if (cycle)
5785 /* If there's a cycle, get out of the loop, as if the matching
5786 had failed. We used to just `goto fail' here, but that was
5787 aborting the search a bit too early: we want to keep the
5788 empty-loop-match and keep matching after the loop.
5789 We want (x?)*y\1z to match both xxyz and xxyxz. */
5790 p += mcnt;
5791 else
5792 PUSH_FAILURE_POINT (p - 3, d);
5794 break;
5797 /* Uses of on_failure_jump:
5799 Each alternative starts with an on_failure_jump that points
5800 to the beginning of the next alternative. Each alternative
5801 except the last ends with a jump that in effect jumps past
5802 the rest of the alternatives. (They really jump to the
5803 ending jump of the following alternative, because tensioning
5804 these jumps is a hassle.)
5806 Repeats start with an on_failure_jump that points past both
5807 the repetition text and either the following jump or
5808 pop_failure_jump back to this on_failure_jump. */
5809 case on_failure_jump:
5810 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5811 DEBUG_PRINT ("EXECUTING on_failure_jump %d (to %p):\n",
5812 mcnt, p + mcnt);
5814 PUSH_FAILURE_POINT (p -3, d);
5815 break;
5817 /* This operation is used for greedy *.
5818 Compare the beginning of the repeat with what in the
5819 pattern follows its end. If we can establish that there
5820 is nothing that they would both match, i.e., that we
5821 would have to backtrack because of (as in, e.g., `a*a')
5822 then we can use a non-backtracking loop based on
5823 on_failure_keep_string_jump instead of on_failure_jump. */
5824 case on_failure_jump_smart:
5825 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5826 DEBUG_PRINT ("EXECUTING on_failure_jump_smart %d (to %p).\n",
5827 mcnt, p + mcnt);
5829 re_char *p1 = p; /* Next operation. */
5830 /* Here, we discard `const', making re_match non-reentrant. */
5831 unsigned char *p2 = (unsigned char*) p + mcnt; /* Jump dest. */
5832 unsigned char *p3 = (unsigned char*) p - 3; /* opcode location. */
5834 p -= 3; /* Reset so that we will re-execute the
5835 instruction once it's been changed. */
5837 EXTRACT_NUMBER (mcnt, p2 - 2);
5839 /* Ensure this is a indeed the trivial kind of loop
5840 we are expecting. */
5841 assert (skip_one_char (p1) == p2 - 3);
5842 assert ((re_opcode_t) p2[-3] == jump && p2 + mcnt == p);
5843 DEBUG_STATEMENT (debug += 2);
5844 if (mutually_exclusive_p (bufp, p1, p2))
5846 /* Use a fast `on_failure_keep_string_jump' loop. */
5847 DEBUG_PRINT (" smart exclusive => fast loop.\n");
5848 *p3 = (unsigned char) on_failure_keep_string_jump;
5849 STORE_NUMBER (p2 - 2, mcnt + 3);
5851 else
5853 /* Default to a safe `on_failure_jump' loop. */
5854 DEBUG_PRINT (" smart default => slow loop.\n");
5855 *p3 = (unsigned char) on_failure_jump;
5857 DEBUG_STATEMENT (debug -= 2);
5859 break;
5861 /* Unconditionally jump (without popping any failure points). */
5862 case jump:
5863 unconditional_jump:
5864 IMMEDIATE_QUIT_CHECK;
5865 EXTRACT_NUMBER_AND_INCR (mcnt, p); /* Get the amount to jump. */
5866 DEBUG_PRINT ("EXECUTING jump %d ", mcnt);
5867 p += mcnt; /* Do the jump. */
5868 DEBUG_PRINT ("(to %p).\n", p);
5869 break;
5872 /* Have to succeed matching what follows at least n times.
5873 After that, handle like `on_failure_jump'. */
5874 case succeed_n:
5875 /* Signedness doesn't matter since we only compare MCNT to 0. */
5876 EXTRACT_NUMBER (mcnt, p + 2);
5877 DEBUG_PRINT ("EXECUTING succeed_n %d.\n", mcnt);
5879 /* Originally, mcnt is how many times we HAVE to succeed. */
5880 if (mcnt != 0)
5882 /* Here, we discard `const', making re_match non-reentrant. */
5883 unsigned char *p2 = (unsigned char*) p + 2; /* counter loc. */
5884 mcnt--;
5885 p += 4;
5886 PUSH_NUMBER (p2, mcnt);
5888 else
5889 /* The two bytes encoding mcnt == 0 are two no_op opcodes. */
5890 goto on_failure;
5891 break;
5893 case jump_n:
5894 /* Signedness doesn't matter since we only compare MCNT to 0. */
5895 EXTRACT_NUMBER (mcnt, p + 2);
5896 DEBUG_PRINT ("EXECUTING jump_n %d.\n", mcnt);
5898 /* Originally, this is how many times we CAN jump. */
5899 if (mcnt != 0)
5901 /* Here, we discard `const', making re_match non-reentrant. */
5902 unsigned char *p2 = (unsigned char*) p + 2; /* counter loc. */
5903 mcnt--;
5904 PUSH_NUMBER (p2, mcnt);
5905 goto unconditional_jump;
5907 /* If don't have to jump any more, skip over the rest of command. */
5908 else
5909 p += 4;
5910 break;
5912 case set_number_at:
5914 unsigned char *p2; /* Location of the counter. */
5915 DEBUG_PRINT ("EXECUTING set_number_at.\n");
5917 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5918 /* Here, we discard `const', making re_match non-reentrant. */
5919 p2 = (unsigned char*) p + mcnt;
5920 /* Signedness doesn't matter since we only copy MCNT's bits. */
5921 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5922 DEBUG_PRINT (" Setting %p to %d.\n", p2, mcnt);
5923 PUSH_NUMBER (p2, mcnt);
5924 break;
5927 case wordbound:
5928 case notwordbound:
5930 boolean not = (re_opcode_t) *(p - 1) == notwordbound;
5931 DEBUG_PRINT ("EXECUTING %swordbound.\n", not ? "not" : "");
5933 /* We SUCCEED (or FAIL) in one of the following cases: */
5935 /* Case 1: D is at the beginning or the end of string. */
5936 if (AT_STRINGS_BEG (d) || AT_STRINGS_END (d))
5937 not = !not;
5938 else
5940 /* C1 is the character before D, S1 is the syntax of C1, C2
5941 is the character at D, and S2 is the syntax of C2. */
5942 re_wchar_t c1, c2;
5943 int s1, s2;
5944 int dummy;
5945 #ifdef emacs
5946 ssize_t offset = PTR_TO_OFFSET (d - 1);
5947 ssize_t charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
5948 UPDATE_SYNTAX_TABLE_FAST (charpos);
5949 #endif
5950 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
5951 s1 = SYNTAX (c1);
5952 #ifdef emacs
5953 UPDATE_SYNTAX_TABLE_FORWARD_FAST (charpos + 1);
5954 #endif
5955 PREFETCH_NOLIMIT ();
5956 GET_CHAR_AFTER (c2, d, dummy);
5957 s2 = SYNTAX (c2);
5959 if (/* Case 2: Only one of S1 and S2 is Sword. */
5960 ((s1 == Sword) != (s2 == Sword))
5961 /* Case 3: Both of S1 and S2 are Sword, and macro
5962 WORD_BOUNDARY_P (C1, C2) returns nonzero. */
5963 || ((s1 == Sword) && WORD_BOUNDARY_P (c1, c2)))
5964 not = !not;
5966 if (not)
5967 break;
5968 else
5969 goto fail;
5972 case wordbeg:
5973 DEBUG_PRINT ("EXECUTING wordbeg.\n");
5975 /* We FAIL in one of the following cases: */
5977 /* Case 1: D is at the end of string. */
5978 if (AT_STRINGS_END (d))
5979 goto fail;
5980 else
5982 /* C1 is the character before D, S1 is the syntax of C1, C2
5983 is the character at D, and S2 is the syntax of C2. */
5984 re_wchar_t c1, c2;
5985 int s1, s2;
5986 int dummy;
5987 #ifdef emacs
5988 ssize_t offset = PTR_TO_OFFSET (d);
5989 ssize_t charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
5990 UPDATE_SYNTAX_TABLE_FAST (charpos);
5991 #endif
5992 PREFETCH ();
5993 GET_CHAR_AFTER (c2, d, dummy);
5994 s2 = SYNTAX (c2);
5996 /* Case 2: S2 is not Sword. */
5997 if (s2 != Sword)
5998 goto fail;
6000 /* Case 3: D is not at the beginning of string ... */
6001 if (!AT_STRINGS_BEG (d))
6003 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
6004 #ifdef emacs
6005 UPDATE_SYNTAX_TABLE_BACKWARD (charpos - 1);
6006 #endif
6007 s1 = SYNTAX (c1);
6009 /* ... and S1 is Sword, and WORD_BOUNDARY_P (C1, C2)
6010 returns 0. */
6011 if ((s1 == Sword) && !WORD_BOUNDARY_P (c1, c2))
6012 goto fail;
6015 break;
6017 case wordend:
6018 DEBUG_PRINT ("EXECUTING wordend.\n");
6020 /* We FAIL in one of the following cases: */
6022 /* Case 1: D is at the beginning of string. */
6023 if (AT_STRINGS_BEG (d))
6024 goto fail;
6025 else
6027 /* C1 is the character before D, S1 is the syntax of C1, C2
6028 is the character at D, and S2 is the syntax of C2. */
6029 re_wchar_t c1, c2;
6030 int s1, s2;
6031 int dummy;
6032 #ifdef emacs
6033 ssize_t offset = PTR_TO_OFFSET (d) - 1;
6034 ssize_t charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
6035 UPDATE_SYNTAX_TABLE_FAST (charpos);
6036 #endif
6037 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
6038 s1 = SYNTAX (c1);
6040 /* Case 2: S1 is not Sword. */
6041 if (s1 != Sword)
6042 goto fail;
6044 /* Case 3: D is not at the end of string ... */
6045 if (!AT_STRINGS_END (d))
6047 PREFETCH_NOLIMIT ();
6048 GET_CHAR_AFTER (c2, d, dummy);
6049 #ifdef emacs
6050 UPDATE_SYNTAX_TABLE_FORWARD_FAST (charpos);
6051 #endif
6052 s2 = SYNTAX (c2);
6054 /* ... and S2 is Sword, and WORD_BOUNDARY_P (C1, C2)
6055 returns 0. */
6056 if ((s2 == Sword) && !WORD_BOUNDARY_P (c1, c2))
6057 goto fail;
6060 break;
6062 case symbeg:
6063 DEBUG_PRINT ("EXECUTING symbeg.\n");
6065 /* We FAIL in one of the following cases: */
6067 /* Case 1: D is at the end of string. */
6068 if (AT_STRINGS_END (d))
6069 goto fail;
6070 else
6072 /* C1 is the character before D, S1 is the syntax of C1, C2
6073 is the character at D, and S2 is the syntax of C2. */
6074 re_wchar_t c1, c2;
6075 int s1, s2;
6076 #ifdef emacs
6077 ssize_t offset = PTR_TO_OFFSET (d);
6078 ssize_t charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
6079 UPDATE_SYNTAX_TABLE_FAST (charpos);
6080 #endif
6081 PREFETCH ();
6082 c2 = RE_STRING_CHAR (d, target_multibyte);
6083 s2 = SYNTAX (c2);
6085 /* Case 2: S2 is neither Sword nor Ssymbol. */
6086 if (s2 != Sword && s2 != Ssymbol)
6087 goto fail;
6089 /* Case 3: D is not at the beginning of string ... */
6090 if (!AT_STRINGS_BEG (d))
6092 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
6093 #ifdef emacs
6094 UPDATE_SYNTAX_TABLE_BACKWARD (charpos - 1);
6095 #endif
6096 s1 = SYNTAX (c1);
6098 /* ... and S1 is Sword or Ssymbol. */
6099 if (s1 == Sword || s1 == Ssymbol)
6100 goto fail;
6103 break;
6105 case symend:
6106 DEBUG_PRINT ("EXECUTING symend.\n");
6108 /* We FAIL in one of the following cases: */
6110 /* Case 1: D is at the beginning of string. */
6111 if (AT_STRINGS_BEG (d))
6112 goto fail;
6113 else
6115 /* C1 is the character before D, S1 is the syntax of C1, C2
6116 is the character at D, and S2 is the syntax of C2. */
6117 re_wchar_t c1, c2;
6118 int s1, s2;
6119 #ifdef emacs
6120 ssize_t offset = PTR_TO_OFFSET (d) - 1;
6121 ssize_t charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
6122 UPDATE_SYNTAX_TABLE_FAST (charpos);
6123 #endif
6124 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
6125 s1 = SYNTAX (c1);
6127 /* Case 2: S1 is neither Ssymbol nor Sword. */
6128 if (s1 != Sword && s1 != Ssymbol)
6129 goto fail;
6131 /* Case 3: D is not at the end of string ... */
6132 if (!AT_STRINGS_END (d))
6134 PREFETCH_NOLIMIT ();
6135 c2 = RE_STRING_CHAR (d, target_multibyte);
6136 #ifdef emacs
6137 UPDATE_SYNTAX_TABLE_FORWARD_FAST (charpos + 1);
6138 #endif
6139 s2 = SYNTAX (c2);
6141 /* ... and S2 is Sword or Ssymbol. */
6142 if (s2 == Sword || s2 == Ssymbol)
6143 goto fail;
6146 break;
6148 case syntaxspec:
6149 case notsyntaxspec:
6151 boolean not = (re_opcode_t) *(p - 1) == notsyntaxspec;
6152 mcnt = *p++;
6153 DEBUG_PRINT ("EXECUTING %ssyntaxspec %d.\n", not ? "not" : "",
6154 mcnt);
6155 PREFETCH ();
6156 #ifdef emacs
6158 ssize_t offset = PTR_TO_OFFSET (d);
6159 ssize_t pos1 = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
6160 UPDATE_SYNTAX_TABLE_FAST (pos1);
6162 #endif
6164 int len;
6165 re_wchar_t c;
6167 GET_CHAR_AFTER (c, d, len);
6168 if ((SYNTAX (c) != (enum syntaxcode) mcnt) ^ not)
6169 goto fail;
6170 d += len;
6173 break;
6175 #ifdef emacs
6176 case before_dot:
6177 DEBUG_PRINT ("EXECUTING before_dot.\n");
6178 if (PTR_BYTE_POS (d) >= PT_BYTE)
6179 goto fail;
6180 break;
6182 case at_dot:
6183 DEBUG_PRINT ("EXECUTING at_dot.\n");
6184 if (PTR_BYTE_POS (d) != PT_BYTE)
6185 goto fail;
6186 break;
6188 case after_dot:
6189 DEBUG_PRINT ("EXECUTING after_dot.\n");
6190 if (PTR_BYTE_POS (d) <= PT_BYTE)
6191 goto fail;
6192 break;
6194 case categoryspec:
6195 case notcategoryspec:
6197 boolean not = (re_opcode_t) *(p - 1) == notcategoryspec;
6198 mcnt = *p++;
6199 DEBUG_PRINT ("EXECUTING %scategoryspec %d.\n",
6200 not ? "not" : "", mcnt);
6201 PREFETCH ();
6204 int len;
6205 re_wchar_t c;
6206 GET_CHAR_AFTER (c, d, len);
6207 if ((!CHAR_HAS_CATEGORY (c, mcnt)) ^ not)
6208 goto fail;
6209 d += len;
6212 break;
6214 #endif /* emacs */
6216 default:
6217 abort ();
6219 continue; /* Successfully executed one pattern command; keep going. */
6222 /* We goto here if a matching operation fails. */
6223 fail:
6224 IMMEDIATE_QUIT_CHECK;
6225 if (!FAIL_STACK_EMPTY ())
6227 re_char *str, *pat;
6228 /* A restart point is known. Restore to that state. */
6229 DEBUG_PRINT ("\nFAIL:\n");
6230 POP_FAILURE_POINT (str, pat);
6231 switch (*pat++)
6233 case on_failure_keep_string_jump:
6234 assert (str == NULL);
6235 goto continue_failure_jump;
6237 case on_failure_jump_nastyloop:
6238 assert ((re_opcode_t)pat[-2] == no_op);
6239 PUSH_FAILURE_POINT (pat - 2, str);
6240 /* Fallthrough */
6242 case on_failure_jump_loop:
6243 case on_failure_jump:
6244 case succeed_n:
6245 d = str;
6246 continue_failure_jump:
6247 EXTRACT_NUMBER_AND_INCR (mcnt, pat);
6248 p = pat + mcnt;
6249 break;
6251 case no_op:
6252 /* A special frame used for nastyloops. */
6253 goto fail;
6255 default:
6256 abort ();
6259 assert (p >= bufp->buffer && p <= pend);
6261 if (d >= string1 && d <= end1)
6262 dend = end_match_1;
6264 else
6265 break; /* Matching at this starting point really fails. */
6266 } /* for (;;) */
6268 if (best_regs_set)
6269 goto restore_best_regs;
6271 FREE_VARIABLES ();
6273 return -1; /* Failure to match. */
6276 /* Subroutine definitions for re_match_2. */
6278 /* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
6279 bytes; nonzero otherwise. */
6281 static int
6282 bcmp_translate (const_re_char *s1, const_re_char *s2, register ssize_t len,
6283 RE_TRANSLATE_TYPE translate, const int target_multibyte)
6285 register re_char *p1 = s1, *p2 = s2;
6286 re_char *p1_end = s1 + len;
6287 re_char *p2_end = s2 + len;
6289 /* FIXME: Checking both p1 and p2 presumes that the two strings might have
6290 different lengths, but relying on a single `len' would break this. -sm */
6291 while (p1 < p1_end && p2 < p2_end)
6293 int p1_charlen, p2_charlen;
6294 re_wchar_t p1_ch, p2_ch;
6296 GET_CHAR_AFTER (p1_ch, p1, p1_charlen);
6297 GET_CHAR_AFTER (p2_ch, p2, p2_charlen);
6299 if (RE_TRANSLATE (translate, p1_ch)
6300 != RE_TRANSLATE (translate, p2_ch))
6301 return 1;
6303 p1 += p1_charlen, p2 += p2_charlen;
6306 if (p1 != p1_end || p2 != p2_end)
6307 return 1;
6309 return 0;
6312 /* Entry points for GNU code. */
6314 /* re_compile_pattern is the GNU regular expression compiler: it
6315 compiles PATTERN (of length SIZE) and puts the result in BUFP.
6316 Returns 0 if the pattern was valid, otherwise an error string.
6318 Assumes the `allocated' (and perhaps `buffer') and `translate' fields
6319 are set in BUFP on entry.
6321 We call regex_compile to do the actual compilation. */
6323 const char *
6324 re_compile_pattern (const char *pattern, size_t length,
6325 struct re_pattern_buffer *bufp)
6327 reg_errcode_t ret;
6329 /* GNU code is written to assume at least RE_NREGS registers will be set
6330 (and at least one extra will be -1). */
6331 bufp->regs_allocated = REGS_UNALLOCATED;
6333 /* And GNU code determines whether or not to get register information
6334 by passing null for the REGS argument to re_match, etc., not by
6335 setting no_sub. */
6336 bufp->no_sub = 0;
6338 ret = regex_compile ((re_char*) pattern, length, re_syntax_options, bufp);
6340 if (!ret)
6341 return NULL;
6342 return gettext (re_error_msgid[(int) ret]);
6344 WEAK_ALIAS (__re_compile_pattern, re_compile_pattern)
6346 /* Entry points compatible with 4.2 BSD regex library. We don't define
6347 them unless specifically requested. */
6349 #if defined _REGEX_RE_COMP || defined _LIBC
6351 /* BSD has one and only one pattern buffer. */
6352 static struct re_pattern_buffer re_comp_buf;
6354 char *
6355 # ifdef _LIBC
6356 /* Make these definitions weak in libc, so POSIX programs can redefine
6357 these names if they don't use our functions, and still use
6358 regcomp/regexec below without link errors. */
6359 weak_function
6360 # endif
6361 re_comp (const char *s)
6363 reg_errcode_t ret;
6365 if (!s)
6367 if (!re_comp_buf.buffer)
6368 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6369 return (char *) gettext ("No previous regular expression");
6370 return 0;
6373 if (!re_comp_buf.buffer)
6375 re_comp_buf.buffer = malloc (200);
6376 if (re_comp_buf.buffer == NULL)
6377 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6378 return (char *) gettext (re_error_msgid[(int) REG_ESPACE]);
6379 re_comp_buf.allocated = 200;
6381 re_comp_buf.fastmap = malloc (1 << BYTEWIDTH);
6382 if (re_comp_buf.fastmap == NULL)
6383 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6384 return (char *) gettext (re_error_msgid[(int) REG_ESPACE]);
6387 /* Since `re_exec' always passes NULL for the `regs' argument, we
6388 don't need to initialize the pattern buffer fields which affect it. */
6390 ret = regex_compile (s, strlen (s), re_syntax_options, &re_comp_buf);
6392 if (!ret)
6393 return NULL;
6395 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6396 return (char *) gettext (re_error_msgid[(int) ret]);
6401 # ifdef _LIBC
6402 weak_function
6403 # endif
6404 re_exec (const char *s)
6406 const size_t len = strlen (s);
6407 return re_search (&re_comp_buf, s, len, 0, len, 0) >= 0;
6409 #endif /* _REGEX_RE_COMP */
6411 /* POSIX.2 functions. Don't define these for Emacs. */
6413 #ifndef emacs
6415 /* regcomp takes a regular expression as a string and compiles it.
6417 PREG is a regex_t *. We do not expect any fields to be initialized,
6418 since POSIX says we shouldn't. Thus, we set
6420 `buffer' to the compiled pattern;
6421 `used' to the length of the compiled pattern;
6422 `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
6423 REG_EXTENDED bit in CFLAGS is set; otherwise, to
6424 RE_SYNTAX_POSIX_BASIC;
6425 `fastmap' to an allocated space for the fastmap;
6426 `fastmap_accurate' to zero;
6427 `re_nsub' to the number of subexpressions in PATTERN.
6429 PATTERN is the address of the pattern string.
6431 CFLAGS is a series of bits which affect compilation.
6433 If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
6434 use POSIX basic syntax.
6436 If REG_NEWLINE is set, then . and [^...] don't match newline.
6437 Also, regexec will try a match beginning after every newline.
6439 If REG_ICASE is set, then we considers upper- and lowercase
6440 versions of letters to be equivalent when matching.
6442 If REG_NOSUB is set, then when PREG is passed to regexec, that
6443 routine will report only success or failure, and nothing about the
6444 registers.
6446 It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
6447 the return codes and their meanings.) */
6449 reg_errcode_t
6450 regcomp (regex_t *_Restrict_ preg, const char *_Restrict_ pattern,
6451 int cflags)
6453 reg_errcode_t ret;
6454 reg_syntax_t syntax
6455 = (cflags & REG_EXTENDED) ?
6456 RE_SYNTAX_POSIX_EXTENDED : RE_SYNTAX_POSIX_BASIC;
6458 /* regex_compile will allocate the space for the compiled pattern. */
6459 preg->buffer = 0;
6460 preg->allocated = 0;
6461 preg->used = 0;
6463 /* Try to allocate space for the fastmap. */
6464 preg->fastmap = malloc (1 << BYTEWIDTH);
6466 if (cflags & REG_ICASE)
6468 unsigned i;
6470 preg->translate = malloc (CHAR_SET_SIZE * sizeof *preg->translate);
6471 if (preg->translate == NULL)
6472 return (int) REG_ESPACE;
6474 /* Map uppercase characters to corresponding lowercase ones. */
6475 for (i = 0; i < CHAR_SET_SIZE; i++)
6476 preg->translate[i] = ISUPPER (i) ? TOLOWER (i) : i;
6478 else
6479 preg->translate = NULL;
6481 /* If REG_NEWLINE is set, newlines are treated differently. */
6482 if (cflags & REG_NEWLINE)
6483 { /* REG_NEWLINE implies neither . nor [^...] match newline. */
6484 syntax &= ~RE_DOT_NEWLINE;
6485 syntax |= RE_HAT_LISTS_NOT_NEWLINE;
6487 else
6488 syntax |= RE_NO_NEWLINE_ANCHOR;
6490 preg->no_sub = !!(cflags & REG_NOSUB);
6492 /* POSIX says a null character in the pattern terminates it, so we
6493 can use strlen here in compiling the pattern. */
6494 ret = regex_compile ((re_char*) pattern, strlen (pattern), syntax, preg);
6496 /* POSIX doesn't distinguish between an unmatched open-group and an
6497 unmatched close-group: both are REG_EPAREN. */
6498 if (ret == REG_ERPAREN)
6499 ret = REG_EPAREN;
6501 if (ret == REG_NOERROR && preg->fastmap)
6502 { /* Compute the fastmap now, since regexec cannot modify the pattern
6503 buffer. */
6504 re_compile_fastmap (preg);
6505 if (preg->can_be_null)
6506 { /* The fastmap can't be used anyway. */
6507 free (preg->fastmap);
6508 preg->fastmap = NULL;
6511 return ret;
6513 WEAK_ALIAS (__regcomp, regcomp)
6516 /* regexec searches for a given pattern, specified by PREG, in the
6517 string STRING.
6519 If NMATCH is zero or REG_NOSUB was set in the cflags argument to
6520 `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
6521 least NMATCH elements, and we set them to the offsets of the
6522 corresponding matched substrings.
6524 EFLAGS specifies `execution flags' which affect matching: if
6525 REG_NOTBOL is set, then ^ does not match at the beginning of the
6526 string; if REG_NOTEOL is set, then $ does not match at the end.
6528 We return 0 if we find a match and REG_NOMATCH if not. */
6530 reg_errcode_t
6531 regexec (const regex_t *_Restrict_ preg, const char *_Restrict_ string,
6532 size_t nmatch, regmatch_t pmatch[_Restrict_arr_], int eflags)
6534 regoff_t ret;
6535 struct re_registers regs;
6536 regex_t private_preg;
6537 size_t len = strlen (string);
6538 boolean want_reg_info = !preg->no_sub && nmatch > 0 && pmatch;
6540 private_preg = *preg;
6542 private_preg.not_bol = !!(eflags & REG_NOTBOL);
6543 private_preg.not_eol = !!(eflags & REG_NOTEOL);
6545 /* The user has told us exactly how many registers to return
6546 information about, via `nmatch'. We have to pass that on to the
6547 matching routines. */
6548 private_preg.regs_allocated = REGS_FIXED;
6550 if (want_reg_info)
6552 regs.num_regs = nmatch;
6553 regs.start = TALLOC (nmatch * 2, regoff_t);
6554 if (regs.start == NULL)
6555 return REG_NOMATCH;
6556 regs.end = regs.start + nmatch;
6559 /* Instead of using not_eol to implement REG_NOTEOL, we could simply
6560 pass (&private_preg, string, len + 1, 0, len, ...) pretending the string
6561 was a little bit longer but still only matching the real part.
6562 This works because the `endline' will check for a '\n' and will find a
6563 '\0', correctly deciding that this is not the end of a line.
6564 But it doesn't work out so nicely for REG_NOTBOL, since we don't have
6565 a convenient '\0' there. For all we know, the string could be preceded
6566 by '\n' which would throw things off. */
6568 /* Perform the searching operation. */
6569 ret = re_search (&private_preg, string, len,
6570 /* start: */ 0, /* range: */ len,
6571 want_reg_info ? &regs : 0);
6573 /* Copy the register information to the POSIX structure. */
6574 if (want_reg_info)
6576 if (ret >= 0)
6578 unsigned r;
6580 for (r = 0; r < nmatch; r++)
6582 pmatch[r].rm_so = regs.start[r];
6583 pmatch[r].rm_eo = regs.end[r];
6587 /* If we needed the temporary register info, free the space now. */
6588 free (regs.start);
6591 /* We want zero return to mean success, unlike `re_search'. */
6592 return ret >= 0 ? REG_NOERROR : REG_NOMATCH;
6594 WEAK_ALIAS (__regexec, regexec)
6597 /* Returns a message corresponding to an error code, ERR_CODE, returned
6598 from either regcomp or regexec. We don't use PREG here.
6600 ERR_CODE was previously called ERRCODE, but that name causes an
6601 error with msvc8 compiler. */
6603 size_t
6604 regerror (int err_code, const regex_t *preg, char *errbuf, size_t errbuf_size)
6606 const char *msg;
6607 size_t msg_size;
6609 if (err_code < 0
6610 || err_code >= (sizeof (re_error_msgid) / sizeof (re_error_msgid[0])))
6611 /* Only error codes returned by the rest of the code should be passed
6612 to this routine. If we are given anything else, or if other regex
6613 code generates an invalid error code, then the program has a bug.
6614 Dump core so we can fix it. */
6615 abort ();
6617 msg = gettext (re_error_msgid[err_code]);
6619 msg_size = strlen (msg) + 1; /* Includes the null. */
6621 if (errbuf_size != 0)
6623 if (msg_size > errbuf_size)
6625 memcpy (errbuf, msg, errbuf_size - 1);
6626 errbuf[errbuf_size - 1] = 0;
6628 else
6629 strcpy (errbuf, msg);
6632 return msg_size;
6634 WEAK_ALIAS (__regerror, regerror)
6637 /* Free dynamically allocated space used by PREG. */
6639 void
6640 regfree (regex_t *preg)
6642 free (preg->buffer);
6643 preg->buffer = NULL;
6645 preg->allocated = 0;
6646 preg->used = 0;
6648 free (preg->fastmap);
6649 preg->fastmap = NULL;
6650 preg->fastmap_accurate = 0;
6652 free (preg->translate);
6653 preg->translate = NULL;
6655 WEAK_ALIAS (__regfree, regfree)
6657 #endif /* not emacs */