]> code.delx.au - gnu-emacs/blob - src/regex.c
Revision: miles@gnu.org--gnu-2005/emacs--unicode--0--patch-81
[gnu-emacs] / src / regex.c
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.)
4
5 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
6 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 USA. */
22
23 /* TODO:
24 - structure the opcode space into opcode+flag.
25 - merge with glibc's regex.[ch].
26 - replace (succeed_n + jump_n + set_number_at) with something that doesn't
27 need to modify the compiled regexp so that re_match can be reentrant.
28 - get rid of on_failure_jump_smart by doing the optimization in re_comp
29 rather than at run-time, so that re_match can be reentrant.
30 */
31
32 /* AIX requires this to be the first thing in the file. */
33 #if defined _AIX && !defined REGEX_MALLOC
34 #pragma alloca
35 #endif
36
37 #ifdef HAVE_CONFIG_H
38 # include <config.h>
39 #endif
40
41 #if defined STDC_HEADERS && !defined emacs
42 # include <stddef.h>
43 #else
44 /* We need this for `regex.h', and perhaps for the Emacs include files. */
45 # include <sys/types.h>
46 #endif
47
48 /* Whether to use ISO C Amendment 1 wide char functions.
49 Those should not be used for Emacs since it uses its own. */
50 #if defined _LIBC
51 #define WIDE_CHAR_SUPPORT 1
52 #else
53 #define WIDE_CHAR_SUPPORT \
54 (HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_BTOWC && !emacs)
55 #endif
56
57 /* For platform which support the ISO C amendement 1 functionality we
58 support user defined character classes. */
59 #if WIDE_CHAR_SUPPORT
60 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
61 # include <wchar.h>
62 # include <wctype.h>
63 #endif
64
65 #ifdef _LIBC
66 /* We have to keep the namespace clean. */
67 # define regfree(preg) __regfree (preg)
68 # define regexec(pr, st, nm, pm, ef) __regexec (pr, st, nm, pm, ef)
69 # define regcomp(preg, pattern, cflags) __regcomp (preg, pattern, cflags)
70 # define regerror(errcode, preg, errbuf, errbuf_size) \
71 __regerror(errcode, preg, errbuf, errbuf_size)
72 # define re_set_registers(bu, re, nu, st, en) \
73 __re_set_registers (bu, re, nu, st, en)
74 # define re_match_2(bufp, string1, size1, string2, size2, pos, regs, stop) \
75 __re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
76 # define re_match(bufp, string, size, pos, regs) \
77 __re_match (bufp, string, size, pos, regs)
78 # define re_search(bufp, string, size, startpos, range, regs) \
79 __re_search (bufp, string, size, startpos, range, regs)
80 # define re_compile_pattern(pattern, length, bufp) \
81 __re_compile_pattern (pattern, length, bufp)
82 # define re_set_syntax(syntax) __re_set_syntax (syntax)
83 # define re_search_2(bufp, st1, s1, st2, s2, startpos, range, regs, stop) \
84 __re_search_2 (bufp, st1, s1, st2, s2, startpos, range, regs, stop)
85 # define re_compile_fastmap(bufp) __re_compile_fastmap (bufp)
86
87 /* Make sure we call libc's function even if the user overrides them. */
88 # define btowc __btowc
89 # define iswctype __iswctype
90 # define wctype __wctype
91
92 # define WEAK_ALIAS(a,b) weak_alias (a, b)
93
94 /* We are also using some library internals. */
95 # include <locale/localeinfo.h>
96 # include <locale/elem-hash.h>
97 # include <langinfo.h>
98 #else
99 # define WEAK_ALIAS(a,b)
100 #endif
101
102 /* This is for other GNU distributions with internationalized messages. */
103 #if HAVE_LIBINTL_H || defined _LIBC
104 # include <libintl.h>
105 #else
106 # define gettext(msgid) (msgid)
107 #endif
108
109 #ifndef gettext_noop
110 /* This define is so xgettext can find the internationalizable
111 strings. */
112 # define gettext_noop(String) String
113 #endif
114
115 /* The `emacs' switch turns on certain matching commands
116 that make sense only in Emacs. */
117 #ifdef emacs
118
119 # include "lisp.h"
120 # include "buffer.h"
121
122 /* Make syntax table lookup grant data in gl_state. */
123 # define SYNTAX_ENTRY_VIA_PROPERTY
124
125 # include "syntax.h"
126 # include "character.h"
127 # include "category.h"
128
129 # ifdef malloc
130 # undef malloc
131 # endif
132 # define malloc xmalloc
133 # ifdef realloc
134 # undef realloc
135 # endif
136 # define realloc xrealloc
137 # ifdef free
138 # undef free
139 # endif
140 # define free xfree
141
142 /* Converts the pointer to the char to BEG-based offset from the start. */
143 # define PTR_TO_OFFSET(d) POS_AS_IN_BUFFER (POINTER_TO_OFFSET (d))
144 # define POS_AS_IN_BUFFER(p) ((p) + (NILP (re_match_object) || BUFFERP (re_match_object)))
145
146 # define RE_MULTIBYTE_P(bufp) ((bufp)->multibyte)
147 # define RE_TARGET_MULTIBYTE_P(bufp) ((bufp)->target_multibyte)
148 # define RE_STRING_CHAR(p, s) \
149 (multibyte ? (STRING_CHAR (p, s)) : (*(p)))
150 # define RE_STRING_CHAR_AND_LENGTH(p, s, len) \
151 (multibyte ? (STRING_CHAR_AND_LENGTH (p, s, len)) : ((len) = 1, *(p)))
152
153 /* Set C a (possibly converted to multibyte) character before P. P
154 points into a string which is the virtual concatenation of STR1
155 (which ends at END1) or STR2 (which ends at END2). */
156 # define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2) \
157 do { \
158 if (multibyte) \
159 { \
160 re_char *dtemp = (p) == (str2) ? (end1) : (p); \
161 re_char *dlimit = ((p) > (str2) && (p) <= (end2)) ? (str2) : (str1); \
162 while (dtemp-- > dlimit && !CHAR_HEAD_P (*dtemp)); \
163 c = STRING_CHAR (dtemp, (p) - dtemp); \
164 } \
165 else \
166 { \
167 (c = ((p) == (str2) ? (end1) : (p))[-1]); \
168 MAKE_CHAR_MULTIBYTE (c); \
169 } \
170 } while (0)
171
172 /* Set C a (possibly converted to multibyte) character at P, and set
173 LEN to the byte length of that character. */
174 # define GET_CHAR_AFTER(c, p, len) \
175 do { \
176 if (multibyte) \
177 c = STRING_CHAR_AND_LENGTH (p, 0, len); \
178 else \
179 { \
180 c = *p; \
181 len = 1; \
182 MAKE_CHAR_MULTIBYTE (c); \
183 } \
184 } while (0)
185
186 #else /* not emacs */
187
188 /* If we are not linking with Emacs proper,
189 we can't use the relocating allocator
190 even if config.h says that we can. */
191 # undef REL_ALLOC
192
193 # if defined STDC_HEADERS || defined _LIBC
194 # include <stdlib.h>
195 # else
196 char *malloc ();
197 char *realloc ();
198 # endif
199
200 /* When used in Emacs's lib-src, we need to get bzero and bcopy somehow.
201 If nothing else has been done, use the method below. */
202 # ifdef INHIBIT_STRING_HEADER
203 # if !(defined HAVE_BZERO && defined HAVE_BCOPY)
204 # if !defined bzero && !defined bcopy
205 # undef INHIBIT_STRING_HEADER
206 # endif
207 # endif
208 # endif
209
210 /* This is the normal way of making sure we have memcpy, memcmp and bzero.
211 This is used in most programs--a few other programs avoid this
212 by defining INHIBIT_STRING_HEADER. */
213 # ifndef INHIBIT_STRING_HEADER
214 # if defined HAVE_STRING_H || defined STDC_HEADERS || defined _LIBC
215 # include <string.h>
216 # ifndef bzero
217 # ifndef _LIBC
218 # define bzero(s, n) (memset (s, '\0', n), (s))
219 # else
220 # define bzero(s, n) __bzero (s, n)
221 # endif
222 # endif
223 # else
224 # include <strings.h>
225 # ifndef memcmp
226 # define memcmp(s1, s2, n) bcmp (s1, s2, n)
227 # endif
228 # ifndef memcpy
229 # define memcpy(d, s, n) (bcopy (s, d, n), (d))
230 # endif
231 # endif
232 # endif
233
234 /* Define the syntax stuff for \<, \>, etc. */
235
236 /* Sword must be nonzero for the wordchar pattern commands in re_match_2. */
237 enum syntaxcode { Swhitespace = 0, Sword = 1, Ssymbol = 2 };
238
239 # ifdef SWITCH_ENUM_BUG
240 # define SWITCH_ENUM_CAST(x) ((int)(x))
241 # else
242 # define SWITCH_ENUM_CAST(x) (x)
243 # endif
244
245 /* Dummy macros for non-Emacs environments. */
246 # define BASE_LEADING_CODE_P(c) (0)
247 # define CHAR_CHARSET(c) 0
248 # define CHARSET_LEADING_CODE_BASE(c) 0
249 # define MAX_MULTIBYTE_LENGTH 1
250 # define RE_MULTIBYTE_P(x) 0
251 # define RE_TARGET_MULTIBYTE_P(x) 0
252 # define WORD_BOUNDARY_P(c1, c2) (0)
253 # define CHAR_HEAD_P(p) (1)
254 # define SINGLE_BYTE_CHAR_P(c) (1)
255 # define SAME_CHARSET_P(c1, c2) (1)
256 # define MULTIBYTE_FORM_LENGTH(p, s) (1)
257 # define PREV_CHAR_BOUNDARY(p, limit) ((p)--)
258 # define STRING_CHAR(p, s) (*(p))
259 # define RE_STRING_CHAR STRING_CHAR
260 # define CHAR_STRING(c, s) (*(s) = (c), 1)
261 # define STRING_CHAR_AND_LENGTH(p, s, actual_len) ((actual_len) = 1, *(p))
262 # define RE_STRING_CHAR_AND_LENGTH STRING_CHAR_AND_LENGTH
263 # define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2) \
264 (c = ((p) == (str2) ? *((end1) - 1) : *((p) - 1)))
265 # define GET_CHAR_AFTER(c, p, len) \
266 (c = *p, len = 1)
267 # define MAKE_CHAR(charset, c1, c2) (c1)
268 # define BYTE8_TO_CHAR(c) (c)
269 # define CHAR_BYTE8_P(c) (0)
270 # define MAKE_CHAR_MULTIBYTE(c) (c)
271 # define MAKE_CHAR_UNIBYTE(c) (c)
272 # define CHAR_LEADING_CODE(c) (c)
273
274 #endif /* not emacs */
275
276 #ifndef RE_TRANSLATE
277 # define RE_TRANSLATE(TBL, C) ((unsigned char)(TBL)[C])
278 # define RE_TRANSLATE_P(TBL) (TBL)
279 #endif
280 \f
281 /* Get the interface, including the syntax bits. */
282 #include "regex.h"
283
284 /* isalpha etc. are used for the character classes. */
285 #include <ctype.h>
286
287 #ifdef emacs
288
289 /* 1 if C is an ASCII character. */
290 # define IS_REAL_ASCII(c) ((c) < 0200)
291
292 /* 1 if C is a unibyte character. */
293 # define ISUNIBYTE(c) (SINGLE_BYTE_CHAR_P ((c)))
294
295 /* The Emacs definitions should not be directly affected by locales. */
296
297 /* In Emacs, these are only used for single-byte characters. */
298 # define ISDIGIT(c) ((c) >= '0' && (c) <= '9')
299 # define ISCNTRL(c) ((c) < ' ')
300 # define ISXDIGIT(c) (((c) >= '0' && (c) <= '9') \
301 || ((c) >= 'a' && (c) <= 'f') \
302 || ((c) >= 'A' && (c) <= 'F'))
303
304 /* This is only used for single-byte characters. */
305 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
306
307 /* The rest must handle multibyte characters. */
308
309 # define ISGRAPH(c) (SINGLE_BYTE_CHAR_P (c) \
310 ? (c) > ' ' && !((c) >= 0177 && (c) <= 0237) \
311 : 1)
312
313 # define ISPRINT(c) (SINGLE_BYTE_CHAR_P (c) \
314 ? (c) >= ' ' && !((c) >= 0177 && (c) <= 0237) \
315 : 1)
316
317 # define ISALNUM(c) (IS_REAL_ASCII (c) \
318 ? (((c) >= 'a' && (c) <= 'z') \
319 || ((c) >= 'A' && (c) <= 'Z') \
320 || ((c) >= '0' && (c) <= '9')) \
321 : SYNTAX (c) == Sword)
322
323 # define ISALPHA(c) (IS_REAL_ASCII (c) \
324 ? (((c) >= 'a' && (c) <= 'z') \
325 || ((c) >= 'A' && (c) <= 'Z')) \
326 : SYNTAX (c) == Sword)
327
328 # define ISLOWER(c) (LOWERCASEP (c))
329
330 # define ISPUNCT(c) (IS_REAL_ASCII (c) \
331 ? ((c) > ' ' && (c) < 0177 \
332 && !(((c) >= 'a' && (c) <= 'z') \
333 || ((c) >= 'A' && (c) <= 'Z') \
334 || ((c) >= '0' && (c) <= '9'))) \
335 : SYNTAX (c) != Sword)
336
337 # define ISSPACE(c) (SYNTAX (c) == Swhitespace)
338
339 # define ISUPPER(c) (UPPERCASEP (c))
340
341 # define ISWORD(c) (SYNTAX (c) == Sword)
342
343 #else /* not emacs */
344
345 /* Jim Meyering writes:
346
347 "... Some ctype macros are valid only for character codes that
348 isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
349 using /bin/cc or gcc but without giving an ansi option). So, all
350 ctype uses should be through macros like ISPRINT... If
351 STDC_HEADERS is defined, then autoconf has verified that the ctype
352 macros don't need to be guarded with references to isascii. ...
353 Defining isascii to 1 should let any compiler worth its salt
354 eliminate the && through constant folding."
355 Solaris defines some of these symbols so we must undefine them first. */
356
357 # undef ISASCII
358 # if defined STDC_HEADERS || (!defined isascii && !defined HAVE_ISASCII)
359 # define ISASCII(c) 1
360 # else
361 # define ISASCII(c) isascii(c)
362 # endif
363
364 /* 1 if C is an ASCII character. */
365 # define IS_REAL_ASCII(c) ((c) < 0200)
366
367 /* This distinction is not meaningful, except in Emacs. */
368 # define ISUNIBYTE(c) 1
369
370 # ifdef isblank
371 # define ISBLANK(c) (ISASCII (c) && isblank (c))
372 # else
373 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
374 # endif
375 # ifdef isgraph
376 # define ISGRAPH(c) (ISASCII (c) && isgraph (c))
377 # else
378 # define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
379 # endif
380
381 # undef ISPRINT
382 # define ISPRINT(c) (ISASCII (c) && isprint (c))
383 # define ISDIGIT(c) (ISASCII (c) && isdigit (c))
384 # define ISALNUM(c) (ISASCII (c) && isalnum (c))
385 # define ISALPHA(c) (ISASCII (c) && isalpha (c))
386 # define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
387 # define ISLOWER(c) (ISASCII (c) && islower (c))
388 # define ISPUNCT(c) (ISASCII (c) && ispunct (c))
389 # define ISSPACE(c) (ISASCII (c) && isspace (c))
390 # define ISUPPER(c) (ISASCII (c) && isupper (c))
391 # define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
392
393 # define ISWORD(c) ISALPHA(c)
394
395 # ifdef _tolower
396 # define TOLOWER(c) _tolower(c)
397 # else
398 # define TOLOWER(c) tolower(c)
399 # endif
400
401 /* How many characters in the character set. */
402 # define CHAR_SET_SIZE 256
403
404 # ifdef SYNTAX_TABLE
405
406 extern char *re_syntax_table;
407
408 # else /* not SYNTAX_TABLE */
409
410 static char re_syntax_table[CHAR_SET_SIZE];
411
412 static void
413 init_syntax_once ()
414 {
415 register int c;
416 static int done = 0;
417
418 if (done)
419 return;
420
421 bzero (re_syntax_table, sizeof re_syntax_table);
422
423 for (c = 0; c < CHAR_SET_SIZE; ++c)
424 if (ISALNUM (c))
425 re_syntax_table[c] = Sword;
426
427 re_syntax_table['_'] = Ssymbol;
428
429 done = 1;
430 }
431
432 # endif /* not SYNTAX_TABLE */
433
434 # define SYNTAX(c) re_syntax_table[(c)]
435
436 #endif /* not emacs */
437 \f
438 #ifndef NULL
439 # define NULL (void *)0
440 #endif
441
442 /* We remove any previous definition of `SIGN_EXTEND_CHAR',
443 since ours (we hope) works properly with all combinations of
444 machines, compilers, `char' and `unsigned char' argument types.
445 (Per Bothner suggested the basic approach.) */
446 #undef SIGN_EXTEND_CHAR
447 #if __STDC__
448 # define SIGN_EXTEND_CHAR(c) ((signed char) (c))
449 #else /* not __STDC__ */
450 /* As in Harbison and Steele. */
451 # define SIGN_EXTEND_CHAR(c) ((((unsigned char) (c)) ^ 128) - 128)
452 #endif
453 \f
454 /* Should we use malloc or alloca? If REGEX_MALLOC is not defined, we
455 use `alloca' instead of `malloc'. This is because using malloc in
456 re_search* or re_match* could cause memory leaks when C-g is used in
457 Emacs; also, malloc is slower and causes storage fragmentation. On
458 the other hand, malloc is more portable, and easier to debug.
459
460 Because we sometimes use alloca, some routines have to be macros,
461 not functions -- `alloca'-allocated space disappears at the end of the
462 function it is called in. */
463
464 #ifdef REGEX_MALLOC
465
466 # define REGEX_ALLOCATE malloc
467 # define REGEX_REALLOCATE(source, osize, nsize) realloc (source, nsize)
468 # define REGEX_FREE free
469
470 #else /* not REGEX_MALLOC */
471
472 /* Emacs already defines alloca, sometimes. */
473 # ifndef alloca
474
475 /* Make alloca work the best possible way. */
476 # ifdef __GNUC__
477 # define alloca __builtin_alloca
478 # else /* not __GNUC__ */
479 # ifdef HAVE_ALLOCA_H
480 # include <alloca.h>
481 # endif /* HAVE_ALLOCA_H */
482 # endif /* not __GNUC__ */
483
484 # endif /* not alloca */
485
486 # define REGEX_ALLOCATE alloca
487
488 /* Assumes a `char *destination' variable. */
489 # define REGEX_REALLOCATE(source, osize, nsize) \
490 (destination = (char *) alloca (nsize), \
491 memcpy (destination, source, osize))
492
493 /* No need to do anything to free, after alloca. */
494 # define REGEX_FREE(arg) ((void)0) /* Do nothing! But inhibit gcc warning. */
495
496 #endif /* not REGEX_MALLOC */
497
498 /* Define how to allocate the failure stack. */
499
500 #if defined REL_ALLOC && defined REGEX_MALLOC
501
502 # define REGEX_ALLOCATE_STACK(size) \
503 r_alloc (&failure_stack_ptr, (size))
504 # define REGEX_REALLOCATE_STACK(source, osize, nsize) \
505 r_re_alloc (&failure_stack_ptr, (nsize))
506 # define REGEX_FREE_STACK(ptr) \
507 r_alloc_free (&failure_stack_ptr)
508
509 #else /* not using relocating allocator */
510
511 # ifdef REGEX_MALLOC
512
513 # define REGEX_ALLOCATE_STACK malloc
514 # define REGEX_REALLOCATE_STACK(source, osize, nsize) realloc (source, nsize)
515 # define REGEX_FREE_STACK free
516
517 # else /* not REGEX_MALLOC */
518
519 # define REGEX_ALLOCATE_STACK alloca
520
521 # define REGEX_REALLOCATE_STACK(source, osize, nsize) \
522 REGEX_REALLOCATE (source, osize, nsize)
523 /* No need to explicitly free anything. */
524 # define REGEX_FREE_STACK(arg) ((void)0)
525
526 # endif /* not REGEX_MALLOC */
527 #endif /* not using relocating allocator */
528
529
530 /* True if `size1' is non-NULL and PTR is pointing anywhere inside
531 `string1' or just past its end. This works if PTR is NULL, which is
532 a good thing. */
533 #define FIRST_STRING_P(ptr) \
534 (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
535
536 /* (Re)Allocate N items of type T using malloc, or fail. */
537 #define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t)))
538 #define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
539 #define RETALLOC_IF(addr, n, t) \
540 if (addr) RETALLOC((addr), (n), t); else (addr) = TALLOC ((n), t)
541 #define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
542
543 #define BYTEWIDTH 8 /* In bits. */
544
545 #define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
546
547 #undef MAX
548 #undef MIN
549 #define MAX(a, b) ((a) > (b) ? (a) : (b))
550 #define MIN(a, b) ((a) < (b) ? (a) : (b))
551
552 /* Type of source-pattern and string chars. */
553 typedef const unsigned char re_char;
554
555 typedef char boolean;
556 #define false 0
557 #define true 1
558
559 static int re_match_2_internal _RE_ARGS ((struct re_pattern_buffer *bufp,
560 re_char *string1, int size1,
561 re_char *string2, int size2,
562 int pos,
563 struct re_registers *regs,
564 int stop));
565 \f
566 /* These are the command codes that appear in compiled regular
567 expressions. Some opcodes are followed by argument bytes. A
568 command code can specify any interpretation whatsoever for its
569 arguments. Zero bytes may appear in the compiled regular expression. */
570
571 typedef enum
572 {
573 no_op = 0,
574
575 /* Succeed right away--no more backtracking. */
576 succeed,
577
578 /* Followed by one byte giving n, then by n literal bytes. */
579 exactn,
580
581 /* Matches any (more or less) character. */
582 anychar,
583
584 /* Matches any one char belonging to specified set. First
585 following byte is number of bitmap bytes. Then come bytes
586 for a bitmap saying which chars are in. Bits in each byte
587 are ordered low-bit-first. A character is in the set if its
588 bit is 1. A character too large to have a bit in the map is
589 automatically not in the set.
590
591 If the length byte has the 0x80 bit set, then that stuff
592 is followed by a range table:
593 2 bytes of flags for character sets (low 8 bits, high 8 bits)
594 See RANGE_TABLE_WORK_BITS below.
595 2 bytes, the number of pairs that follow (upto 32767)
596 pairs, each 2 multibyte characters,
597 each multibyte character represented as 3 bytes. */
598 charset,
599
600 /* Same parameters as charset, but match any character that is
601 not one of those specified. */
602 charset_not,
603
604 /* Start remembering the text that is matched, for storing in a
605 register. Followed by one byte with the register number, in
606 the range 0 to one less than the pattern buffer's re_nsub
607 field. */
608 start_memory,
609
610 /* Stop remembering the text that is matched and store it in a
611 memory register. Followed by one byte with the register
612 number, in the range 0 to one less than `re_nsub' in the
613 pattern buffer. */
614 stop_memory,
615
616 /* Match a duplicate of something remembered. Followed by one
617 byte containing the register number. */
618 duplicate,
619
620 /* Fail unless at beginning of line. */
621 begline,
622
623 /* Fail unless at end of line. */
624 endline,
625
626 /* Succeeds if at beginning of buffer (if emacs) or at beginning
627 of string to be matched (if not). */
628 begbuf,
629
630 /* Analogously, for end of buffer/string. */
631 endbuf,
632
633 /* Followed by two byte relative address to which to jump. */
634 jump,
635
636 /* Followed by two-byte relative address of place to resume at
637 in case of failure. */
638 on_failure_jump,
639
640 /* Like on_failure_jump, but pushes a placeholder instead of the
641 current string position when executed. */
642 on_failure_keep_string_jump,
643
644 /* Just like `on_failure_jump', except that it checks that we
645 don't get stuck in an infinite loop (matching an empty string
646 indefinitely). */
647 on_failure_jump_loop,
648
649 /* Just like `on_failure_jump_loop', except that it checks for
650 a different kind of loop (the kind that shows up with non-greedy
651 operators). This operation has to be immediately preceded
652 by a `no_op'. */
653 on_failure_jump_nastyloop,
654
655 /* A smart `on_failure_jump' used for greedy * and + operators.
656 It analyses the loop before which it is put and if the
657 loop does not require backtracking, it changes itself to
658 `on_failure_keep_string_jump' and short-circuits the loop,
659 else it just defaults to changing itself into `on_failure_jump'.
660 It assumes that it is pointing to just past a `jump'. */
661 on_failure_jump_smart,
662
663 /* Followed by two-byte relative address and two-byte number n.
664 After matching N times, jump to the address upon failure.
665 Does not work if N starts at 0: use on_failure_jump_loop
666 instead. */
667 succeed_n,
668
669 /* Followed by two-byte relative address, and two-byte number n.
670 Jump to the address N times, then fail. */
671 jump_n,
672
673 /* Set the following two-byte relative address to the
674 subsequent two-byte number. The address *includes* the two
675 bytes of number. */
676 set_number_at,
677
678 wordbeg, /* Succeeds if at word beginning. */
679 wordend, /* Succeeds if at word end. */
680
681 wordbound, /* Succeeds if at a word boundary. */
682 notwordbound, /* Succeeds if not at a word boundary. */
683
684 symbeg, /* Succeeds if at symbol beginning. */
685 symend, /* Succeeds if at symbol end. */
686
687 /* Matches any character whose syntax is specified. Followed by
688 a byte which contains a syntax code, e.g., Sword. */
689 syntaxspec,
690
691 /* Matches any character whose syntax is not that specified. */
692 notsyntaxspec
693
694 #ifdef emacs
695 ,before_dot, /* Succeeds if before point. */
696 at_dot, /* Succeeds if at point. */
697 after_dot, /* Succeeds if after point. */
698
699 /* Matches any character whose category-set contains the specified
700 category. The operator is followed by a byte which contains a
701 category code (mnemonic ASCII character). */
702 categoryspec,
703
704 /* Matches any character whose category-set does not contain the
705 specified category. The operator is followed by a byte which
706 contains the category code (mnemonic ASCII character). */
707 notcategoryspec
708 #endif /* emacs */
709 } re_opcode_t;
710 \f
711 /* Common operations on the compiled pattern. */
712
713 /* Store NUMBER in two contiguous bytes starting at DESTINATION. */
714
715 #define STORE_NUMBER(destination, number) \
716 do { \
717 (destination)[0] = (number) & 0377; \
718 (destination)[1] = (number) >> 8; \
719 } while (0)
720
721 /* Same as STORE_NUMBER, except increment DESTINATION to
722 the byte after where the number is stored. Therefore, DESTINATION
723 must be an lvalue. */
724
725 #define STORE_NUMBER_AND_INCR(destination, number) \
726 do { \
727 STORE_NUMBER (destination, number); \
728 (destination) += 2; \
729 } while (0)
730
731 /* Put into DESTINATION a number stored in two contiguous bytes starting
732 at SOURCE. */
733
734 #define EXTRACT_NUMBER(destination, source) \
735 do { \
736 (destination) = *(source) & 0377; \
737 (destination) += SIGN_EXTEND_CHAR (*((source) + 1)) << 8; \
738 } while (0)
739
740 #ifdef DEBUG
741 static void extract_number _RE_ARGS ((int *dest, re_char *source));
742 static void
743 extract_number (dest, source)
744 int *dest;
745 re_char *source;
746 {
747 int temp = SIGN_EXTEND_CHAR (*(source + 1));
748 *dest = *source & 0377;
749 *dest += temp << 8;
750 }
751
752 # ifndef EXTRACT_MACROS /* To debug the macros. */
753 # undef EXTRACT_NUMBER
754 # define EXTRACT_NUMBER(dest, src) extract_number (&dest, src)
755 # endif /* not EXTRACT_MACROS */
756
757 #endif /* DEBUG */
758
759 /* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
760 SOURCE must be an lvalue. */
761
762 #define EXTRACT_NUMBER_AND_INCR(destination, source) \
763 do { \
764 EXTRACT_NUMBER (destination, source); \
765 (source) += 2; \
766 } while (0)
767
768 #ifdef DEBUG
769 static void extract_number_and_incr _RE_ARGS ((int *destination,
770 re_char **source));
771 static void
772 extract_number_and_incr (destination, source)
773 int *destination;
774 re_char **source;
775 {
776 extract_number (destination, *source);
777 *source += 2;
778 }
779
780 # ifndef EXTRACT_MACROS
781 # undef EXTRACT_NUMBER_AND_INCR
782 # define EXTRACT_NUMBER_AND_INCR(dest, src) \
783 extract_number_and_incr (&dest, &src)
784 # endif /* not EXTRACT_MACROS */
785
786 #endif /* DEBUG */
787 \f
788 /* Store a multibyte character in three contiguous bytes starting
789 DESTINATION, and increment DESTINATION to the byte after where the
790 character is stored. Therefore, DESTINATION must be an lvalue. */
791
792 #define STORE_CHARACTER_AND_INCR(destination, character) \
793 do { \
794 (destination)[0] = (character) & 0377; \
795 (destination)[1] = ((character) >> 8) & 0377; \
796 (destination)[2] = (character) >> 16; \
797 (destination) += 3; \
798 } while (0)
799
800 /* Put into DESTINATION a character stored in three contiguous bytes
801 starting at SOURCE. */
802
803 #define EXTRACT_CHARACTER(destination, source) \
804 do { \
805 (destination) = ((source)[0] \
806 | ((source)[1] << 8) \
807 | ((source)[2] << 16)); \
808 } while (0)
809
810
811 /* Macros for charset. */
812
813 /* Size of bitmap of charset P in bytes. P is a start of charset,
814 i.e. *P is (re_opcode_t) charset or (re_opcode_t) charset_not. */
815 #define CHARSET_BITMAP_SIZE(p) ((p)[1] & 0x7F)
816
817 /* Nonzero if charset P has range table. */
818 #define CHARSET_RANGE_TABLE_EXISTS_P(p) ((p)[1] & 0x80)
819
820 /* Return the address of range table of charset P. But not the start
821 of table itself, but the before where the number of ranges is
822 stored. `2 +' means to skip re_opcode_t and size of bitmap,
823 and the 2 bytes of flags at the start of the range table. */
824 #define CHARSET_RANGE_TABLE(p) (&(p)[4 + CHARSET_BITMAP_SIZE (p)])
825
826 /* Extract the bit flags that start a range table. */
827 #define CHARSET_RANGE_TABLE_BITS(p) \
828 ((p)[2 + CHARSET_BITMAP_SIZE (p)] \
829 + (p)[3 + CHARSET_BITMAP_SIZE (p)] * 0x100)
830
831 /* Test if C is listed in the bitmap of charset P. */
832 #define CHARSET_LOOKUP_BITMAP(p, c) \
833 ((c) < CHARSET_BITMAP_SIZE (p) * BYTEWIDTH \
834 && (p)[2 + (c) / BYTEWIDTH] & (1 << ((c) % BYTEWIDTH)))
835
836 /* Return the address of end of RANGE_TABLE. COUNT is number of
837 ranges (which is a pair of (start, end)) in the RANGE_TABLE. `* 2'
838 is start of range and end of range. `* 3' is size of each start
839 and end. */
840 #define CHARSET_RANGE_TABLE_END(range_table, count) \
841 ((range_table) + (count) * 2 * 3)
842
843 /* Test if C is in RANGE_TABLE. A flag NOT is negated if C is in.
844 COUNT is number of ranges in RANGE_TABLE. */
845 #define CHARSET_LOOKUP_RANGE_TABLE_RAW(not, c, range_table, count) \
846 do \
847 { \
848 re_wchar_t range_start, range_end; \
849 re_char *p; \
850 re_char *range_table_end \
851 = CHARSET_RANGE_TABLE_END ((range_table), (count)); \
852 \
853 for (p = (range_table); p < range_table_end; p += 2 * 3) \
854 { \
855 EXTRACT_CHARACTER (range_start, p); \
856 EXTRACT_CHARACTER (range_end, p + 3); \
857 \
858 if (range_start <= (c) && (c) <= range_end) \
859 { \
860 (not) = !(not); \
861 break; \
862 } \
863 } \
864 } \
865 while (0)
866
867 /* Test if C is in range table of CHARSET. The flag NOT is negated if
868 C is listed in it. */
869 #define CHARSET_LOOKUP_RANGE_TABLE(not, c, charset) \
870 do \
871 { \
872 /* Number of ranges in range table. */ \
873 int count; \
874 re_char *range_table = CHARSET_RANGE_TABLE (charset); \
875 \
876 EXTRACT_NUMBER_AND_INCR (count, range_table); \
877 CHARSET_LOOKUP_RANGE_TABLE_RAW ((not), (c), range_table, count); \
878 } \
879 while (0)
880 \f
881 /* If DEBUG is defined, Regex prints many voluminous messages about what
882 it is doing (if the variable `debug' is nonzero). If linked with the
883 main program in `iregex.c', you can enter patterns and strings
884 interactively. And if linked with the main program in `main.c' and
885 the other test files, you can run the already-written tests. */
886
887 #ifdef DEBUG
888
889 /* We use standard I/O for debugging. */
890 # include <stdio.h>
891
892 /* It is useful to test things that ``must'' be true when debugging. */
893 # include <assert.h>
894
895 static int debug = -100000;
896
897 # define DEBUG_STATEMENT(e) e
898 # define DEBUG_PRINT1(x) if (debug > 0) printf (x)
899 # define DEBUG_PRINT2(x1, x2) if (debug > 0) printf (x1, x2)
900 # define DEBUG_PRINT3(x1, x2, x3) if (debug > 0) printf (x1, x2, x3)
901 # define DEBUG_PRINT4(x1, x2, x3, x4) if (debug > 0) printf (x1, x2, x3, x4)
902 # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) \
903 if (debug > 0) print_partial_compiled_pattern (s, e)
904 # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \
905 if (debug > 0) print_double_string (w, s1, sz1, s2, sz2)
906
907
908 /* Print the fastmap in human-readable form. */
909
910 void
911 print_fastmap (fastmap)
912 char *fastmap;
913 {
914 unsigned was_a_range = 0;
915 unsigned i = 0;
916
917 while (i < (1 << BYTEWIDTH))
918 {
919 if (fastmap[i++])
920 {
921 was_a_range = 0;
922 putchar (i - 1);
923 while (i < (1 << BYTEWIDTH) && fastmap[i])
924 {
925 was_a_range = 1;
926 i++;
927 }
928 if (was_a_range)
929 {
930 printf ("-");
931 putchar (i - 1);
932 }
933 }
934 }
935 putchar ('\n');
936 }
937
938
939 /* Print a compiled pattern string in human-readable form, starting at
940 the START pointer into it and ending just before the pointer END. */
941
942 void
943 print_partial_compiled_pattern (start, end)
944 re_char *start;
945 re_char *end;
946 {
947 int mcnt, mcnt2;
948 re_char *p = start;
949 re_char *pend = end;
950
951 if (start == NULL)
952 {
953 fprintf (stderr, "(null)\n");
954 return;
955 }
956
957 /* Loop over pattern commands. */
958 while (p < pend)
959 {
960 fprintf (stderr, "%d:\t", p - start);
961
962 switch ((re_opcode_t) *p++)
963 {
964 case no_op:
965 fprintf (stderr, "/no_op");
966 break;
967
968 case succeed:
969 fprintf (stderr, "/succeed");
970 break;
971
972 case exactn:
973 mcnt = *p++;
974 fprintf (stderr, "/exactn/%d", mcnt);
975 do
976 {
977 fprintf (stderr, "/%c", *p++);
978 }
979 while (--mcnt);
980 break;
981
982 case start_memory:
983 fprintf (stderr, "/start_memory/%d", *p++);
984 break;
985
986 case stop_memory:
987 fprintf (stderr, "/stop_memory/%d", *p++);
988 break;
989
990 case duplicate:
991 fprintf (stderr, "/duplicate/%d", *p++);
992 break;
993
994 case anychar:
995 fprintf (stderr, "/anychar");
996 break;
997
998 case charset:
999 case charset_not:
1000 {
1001 register int c, last = -100;
1002 register int in_range = 0;
1003 int length = CHARSET_BITMAP_SIZE (p - 1);
1004 int has_range_table = CHARSET_RANGE_TABLE_EXISTS_P (p - 1);
1005
1006 fprintf (stderr, "/charset [%s",
1007 (re_opcode_t) *(p - 1) == charset_not ? "^" : "");
1008
1009 if (p + *p >= pend)
1010 fprintf (stderr, " !extends past end of pattern! ");
1011
1012 for (c = 0; c < 256; c++)
1013 if (c / 8 < length
1014 && (p[1 + (c/8)] & (1 << (c % 8))))
1015 {
1016 /* Are we starting a range? */
1017 if (last + 1 == c && ! in_range)
1018 {
1019 fprintf (stderr, "-");
1020 in_range = 1;
1021 }
1022 /* Have we broken a range? */
1023 else if (last + 1 != c && in_range)
1024 {
1025 fprintf (stderr, "%c", last);
1026 in_range = 0;
1027 }
1028
1029 if (! in_range)
1030 fprintf (stderr, "%c", c);
1031
1032 last = c;
1033 }
1034
1035 if (in_range)
1036 fprintf (stderr, "%c", last);
1037
1038 fprintf (stderr, "]");
1039
1040 p += 1 + length;
1041
1042 if (has_range_table)
1043 {
1044 int count;
1045 fprintf (stderr, "has-range-table");
1046
1047 /* ??? Should print the range table; for now, just skip it. */
1048 p += 2; /* skip range table bits */
1049 EXTRACT_NUMBER_AND_INCR (count, p);
1050 p = CHARSET_RANGE_TABLE_END (p, count);
1051 }
1052 }
1053 break;
1054
1055 case begline:
1056 fprintf (stderr, "/begline");
1057 break;
1058
1059 case endline:
1060 fprintf (stderr, "/endline");
1061 break;
1062
1063 case on_failure_jump:
1064 extract_number_and_incr (&mcnt, &p);
1065 fprintf (stderr, "/on_failure_jump to %d", p + mcnt - start);
1066 break;
1067
1068 case on_failure_keep_string_jump:
1069 extract_number_and_incr (&mcnt, &p);
1070 fprintf (stderr, "/on_failure_keep_string_jump to %d", p + mcnt - start);
1071 break;
1072
1073 case on_failure_jump_nastyloop:
1074 extract_number_and_incr (&mcnt, &p);
1075 fprintf (stderr, "/on_failure_jump_nastyloop to %d", p + mcnt - start);
1076 break;
1077
1078 case on_failure_jump_loop:
1079 extract_number_and_incr (&mcnt, &p);
1080 fprintf (stderr, "/on_failure_jump_loop to %d", p + mcnt - start);
1081 break;
1082
1083 case on_failure_jump_smart:
1084 extract_number_and_incr (&mcnt, &p);
1085 fprintf (stderr, "/on_failure_jump_smart to %d", p + mcnt - start);
1086 break;
1087
1088 case jump:
1089 extract_number_and_incr (&mcnt, &p);
1090 fprintf (stderr, "/jump to %d", p + mcnt - start);
1091 break;
1092
1093 case succeed_n:
1094 extract_number_and_incr (&mcnt, &p);
1095 extract_number_and_incr (&mcnt2, &p);
1096 fprintf (stderr, "/succeed_n to %d, %d times", p - 2 + mcnt - start, mcnt2);
1097 break;
1098
1099 case jump_n:
1100 extract_number_and_incr (&mcnt, &p);
1101 extract_number_and_incr (&mcnt2, &p);
1102 fprintf (stderr, "/jump_n to %d, %d times", p - 2 + mcnt - start, mcnt2);
1103 break;
1104
1105 case set_number_at:
1106 extract_number_and_incr (&mcnt, &p);
1107 extract_number_and_incr (&mcnt2, &p);
1108 fprintf (stderr, "/set_number_at location %d to %d", p - 2 + mcnt - start, mcnt2);
1109 break;
1110
1111 case wordbound:
1112 fprintf (stderr, "/wordbound");
1113 break;
1114
1115 case notwordbound:
1116 fprintf (stderr, "/notwordbound");
1117 break;
1118
1119 case wordbeg:
1120 fprintf (stderr, "/wordbeg");
1121 break;
1122
1123 case wordend:
1124 fprintf (stderr, "/wordend");
1125 break;
1126
1127 case symbeg:
1128 fprintf (stderr, "/symbeg");
1129 break;
1130
1131 case symend:
1132 fprintf (stderr, "/symend");
1133 break;
1134
1135 case syntaxspec:
1136 fprintf (stderr, "/syntaxspec");
1137 mcnt = *p++;
1138 fprintf (stderr, "/%d", mcnt);
1139 break;
1140
1141 case notsyntaxspec:
1142 fprintf (stderr, "/notsyntaxspec");
1143 mcnt = *p++;
1144 fprintf (stderr, "/%d", mcnt);
1145 break;
1146
1147 # ifdef emacs
1148 case before_dot:
1149 fprintf (stderr, "/before_dot");
1150 break;
1151
1152 case at_dot:
1153 fprintf (stderr, "/at_dot");
1154 break;
1155
1156 case after_dot:
1157 fprintf (stderr, "/after_dot");
1158 break;
1159
1160 case categoryspec:
1161 fprintf (stderr, "/categoryspec");
1162 mcnt = *p++;
1163 fprintf (stderr, "/%d", mcnt);
1164 break;
1165
1166 case notcategoryspec:
1167 fprintf (stderr, "/notcategoryspec");
1168 mcnt = *p++;
1169 fprintf (stderr, "/%d", mcnt);
1170 break;
1171 # endif /* emacs */
1172
1173 case begbuf:
1174 fprintf (stderr, "/begbuf");
1175 break;
1176
1177 case endbuf:
1178 fprintf (stderr, "/endbuf");
1179 break;
1180
1181 default:
1182 fprintf (stderr, "?%d", *(p-1));
1183 }
1184
1185 fprintf (stderr, "\n");
1186 }
1187
1188 fprintf (stderr, "%d:\tend of pattern.\n", p - start);
1189 }
1190
1191
1192 void
1193 print_compiled_pattern (bufp)
1194 struct re_pattern_buffer *bufp;
1195 {
1196 re_char *buffer = bufp->buffer;
1197
1198 print_partial_compiled_pattern (buffer, buffer + bufp->used);
1199 printf ("%ld bytes used/%ld bytes allocated.\n",
1200 bufp->used, bufp->allocated);
1201
1202 if (bufp->fastmap_accurate && bufp->fastmap)
1203 {
1204 printf ("fastmap: ");
1205 print_fastmap (bufp->fastmap);
1206 }
1207
1208 printf ("re_nsub: %d\t", bufp->re_nsub);
1209 printf ("regs_alloc: %d\t", bufp->regs_allocated);
1210 printf ("can_be_null: %d\t", bufp->can_be_null);
1211 printf ("no_sub: %d\t", bufp->no_sub);
1212 printf ("not_bol: %d\t", bufp->not_bol);
1213 printf ("not_eol: %d\t", bufp->not_eol);
1214 printf ("syntax: %lx\n", bufp->syntax);
1215 fflush (stdout);
1216 /* Perhaps we should print the translate table? */
1217 }
1218
1219
1220 void
1221 print_double_string (where, string1, size1, string2, size2)
1222 re_char *where;
1223 re_char *string1;
1224 re_char *string2;
1225 int size1;
1226 int size2;
1227 {
1228 int this_char;
1229
1230 if (where == NULL)
1231 printf ("(null)");
1232 else
1233 {
1234 if (FIRST_STRING_P (where))
1235 {
1236 for (this_char = where - string1; this_char < size1; this_char++)
1237 putchar (string1[this_char]);
1238
1239 where = string2;
1240 }
1241
1242 for (this_char = where - string2; this_char < size2; this_char++)
1243 putchar (string2[this_char]);
1244 }
1245 }
1246
1247 #else /* not DEBUG */
1248
1249 # undef assert
1250 # define assert(e)
1251
1252 # define DEBUG_STATEMENT(e)
1253 # define DEBUG_PRINT1(x)
1254 # define DEBUG_PRINT2(x1, x2)
1255 # define DEBUG_PRINT3(x1, x2, x3)
1256 # define DEBUG_PRINT4(x1, x2, x3, x4)
1257 # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
1258 # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
1259
1260 #endif /* not DEBUG */
1261 \f
1262 /* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
1263 also be assigned to arbitrarily: each pattern buffer stores its own
1264 syntax, so it can be changed between regex compilations. */
1265 /* This has no initializer because initialized variables in Emacs
1266 become read-only after dumping. */
1267 reg_syntax_t re_syntax_options;
1268
1269
1270 /* Specify the precise syntax of regexps for compilation. This provides
1271 for compatibility for various utilities which historically have
1272 different, incompatible syntaxes.
1273
1274 The argument SYNTAX is a bit mask comprised of the various bits
1275 defined in regex.h. We return the old syntax. */
1276
1277 reg_syntax_t
1278 re_set_syntax (syntax)
1279 reg_syntax_t syntax;
1280 {
1281 reg_syntax_t ret = re_syntax_options;
1282
1283 re_syntax_options = syntax;
1284 return ret;
1285 }
1286 WEAK_ALIAS (__re_set_syntax, re_set_syntax)
1287
1288 /* Regexp to use to replace spaces, or NULL meaning don't. */
1289 static re_char *whitespace_regexp;
1290
1291 void
1292 re_set_whitespace_regexp (regexp)
1293 re_char *regexp;
1294 {
1295 whitespace_regexp = regexp;
1296 }
1297 WEAK_ALIAS (__re_set_syntax, re_set_syntax)
1298 \f
1299 /* This table gives an error message for each of the error codes listed
1300 in regex.h. Obviously the order here has to be same as there.
1301 POSIX doesn't require that we do anything for REG_NOERROR,
1302 but why not be nice? */
1303
1304 static const char *re_error_msgid[] =
1305 {
1306 gettext_noop ("Success"), /* REG_NOERROR */
1307 gettext_noop ("No match"), /* REG_NOMATCH */
1308 gettext_noop ("Invalid regular expression"), /* REG_BADPAT */
1309 gettext_noop ("Invalid collation character"), /* REG_ECOLLATE */
1310 gettext_noop ("Invalid character class name"), /* REG_ECTYPE */
1311 gettext_noop ("Trailing backslash"), /* REG_EESCAPE */
1312 gettext_noop ("Invalid back reference"), /* REG_ESUBREG */
1313 gettext_noop ("Unmatched [ or [^"), /* REG_EBRACK */
1314 gettext_noop ("Unmatched ( or \\("), /* REG_EPAREN */
1315 gettext_noop ("Unmatched \\{"), /* REG_EBRACE */
1316 gettext_noop ("Invalid content of \\{\\}"), /* REG_BADBR */
1317 gettext_noop ("Invalid range end"), /* REG_ERANGE */
1318 gettext_noop ("Memory exhausted"), /* REG_ESPACE */
1319 gettext_noop ("Invalid preceding regular expression"), /* REG_BADRPT */
1320 gettext_noop ("Premature end of regular expression"), /* REG_EEND */
1321 gettext_noop ("Regular expression too big"), /* REG_ESIZE */
1322 gettext_noop ("Unmatched ) or \\)"), /* REG_ERPAREN */
1323 gettext_noop ("Range striding over charsets") /* REG_ERANGEX */
1324 };
1325 \f
1326 /* Avoiding alloca during matching, to placate r_alloc. */
1327
1328 /* Define MATCH_MAY_ALLOCATE unless we need to make sure that the
1329 searching and matching functions should not call alloca. On some
1330 systems, alloca is implemented in terms of malloc, and if we're
1331 using the relocating allocator routines, then malloc could cause a
1332 relocation, which might (if the strings being searched are in the
1333 ralloc heap) shift the data out from underneath the regexp
1334 routines.
1335
1336 Here's another reason to avoid allocation: Emacs
1337 processes input from X in a signal handler; processing X input may
1338 call malloc; if input arrives while a matching routine is calling
1339 malloc, then we're scrod. But Emacs can't just block input while
1340 calling matching routines; then we don't notice interrupts when
1341 they come in. So, Emacs blocks input around all regexp calls
1342 except the matching calls, which it leaves unprotected, in the
1343 faith that they will not malloc. */
1344
1345 /* Normally, this is fine. */
1346 #define MATCH_MAY_ALLOCATE
1347
1348 /* When using GNU C, we are not REALLY using the C alloca, no matter
1349 what config.h may say. So don't take precautions for it. */
1350 #ifdef __GNUC__
1351 # undef C_ALLOCA
1352 #endif
1353
1354 /* The match routines may not allocate if (1) they would do it with malloc
1355 and (2) it's not safe for them to use malloc.
1356 Note that if REL_ALLOC is defined, matching would not use malloc for the
1357 failure stack, but we would still use it for the register vectors;
1358 so REL_ALLOC should not affect this. */
1359 #if (defined C_ALLOCA || defined REGEX_MALLOC) && defined emacs
1360 # undef MATCH_MAY_ALLOCATE
1361 #endif
1362
1363 \f
1364 /* Failure stack declarations and macros; both re_compile_fastmap and
1365 re_match_2 use a failure stack. These have to be macros because of
1366 REGEX_ALLOCATE_STACK. */
1367
1368
1369 /* Approximate number of failure points for which to initially allocate space
1370 when matching. If this number is exceeded, we allocate more
1371 space, so it is not a hard limit. */
1372 #ifndef INIT_FAILURE_ALLOC
1373 # define INIT_FAILURE_ALLOC 20
1374 #endif
1375
1376 /* Roughly the maximum number of failure points on the stack. Would be
1377 exactly that if always used TYPICAL_FAILURE_SIZE items each time we failed.
1378 This is a variable only so users of regex can assign to it; we never
1379 change it ourselves. We always multiply it by TYPICAL_FAILURE_SIZE
1380 before using it, so it should probably be a byte-count instead. */
1381 # if defined MATCH_MAY_ALLOCATE
1382 /* Note that 4400 was enough to cause a crash on Alpha OSF/1,
1383 whose default stack limit is 2mb. In order for a larger
1384 value to work reliably, you have to try to make it accord
1385 with the process stack limit. */
1386 size_t re_max_failures = 40000;
1387 # else
1388 size_t re_max_failures = 4000;
1389 # endif
1390
1391 union fail_stack_elt
1392 {
1393 re_char *pointer;
1394 /* This should be the biggest `int' that's no bigger than a pointer. */
1395 long integer;
1396 };
1397
1398 typedef union fail_stack_elt fail_stack_elt_t;
1399
1400 typedef struct
1401 {
1402 fail_stack_elt_t *stack;
1403 size_t size;
1404 size_t avail; /* Offset of next open position. */
1405 size_t frame; /* Offset of the cur constructed frame. */
1406 } fail_stack_type;
1407
1408 #define FAIL_STACK_EMPTY() (fail_stack.frame == 0)
1409 #define FAIL_STACK_FULL() (fail_stack.avail == fail_stack.size)
1410
1411
1412 /* Define macros to initialize and free the failure stack.
1413 Do `return -2' if the alloc fails. */
1414
1415 #ifdef MATCH_MAY_ALLOCATE
1416 # define INIT_FAIL_STACK() \
1417 do { \
1418 fail_stack.stack = (fail_stack_elt_t *) \
1419 REGEX_ALLOCATE_STACK (INIT_FAILURE_ALLOC * TYPICAL_FAILURE_SIZE \
1420 * sizeof (fail_stack_elt_t)); \
1421 \
1422 if (fail_stack.stack == NULL) \
1423 return -2; \
1424 \
1425 fail_stack.size = INIT_FAILURE_ALLOC; \
1426 fail_stack.avail = 0; \
1427 fail_stack.frame = 0; \
1428 } while (0)
1429
1430 # define RESET_FAIL_STACK() REGEX_FREE_STACK (fail_stack.stack)
1431 #else
1432 # define INIT_FAIL_STACK() \
1433 do { \
1434 fail_stack.avail = 0; \
1435 fail_stack.frame = 0; \
1436 } while (0)
1437
1438 # define RESET_FAIL_STACK() ((void)0)
1439 #endif
1440
1441
1442 /* Double the size of FAIL_STACK, up to a limit
1443 which allows approximately `re_max_failures' items.
1444
1445 Return 1 if succeeds, and 0 if either ran out of memory
1446 allocating space for it or it was already too large.
1447
1448 REGEX_REALLOCATE_STACK requires `destination' be declared. */
1449
1450 /* Factor to increase the failure stack size by
1451 when we increase it.
1452 This used to be 2, but 2 was too wasteful
1453 because the old discarded stacks added up to as much space
1454 were as ultimate, maximum-size stack. */
1455 #define FAIL_STACK_GROWTH_FACTOR 4
1456
1457 #define GROW_FAIL_STACK(fail_stack) \
1458 (((fail_stack).size * sizeof (fail_stack_elt_t) \
1459 >= re_max_failures * TYPICAL_FAILURE_SIZE) \
1460 ? 0 \
1461 : ((fail_stack).stack \
1462 = (fail_stack_elt_t *) \
1463 REGEX_REALLOCATE_STACK ((fail_stack).stack, \
1464 (fail_stack).size * sizeof (fail_stack_elt_t), \
1465 MIN (re_max_failures * TYPICAL_FAILURE_SIZE, \
1466 ((fail_stack).size * sizeof (fail_stack_elt_t) \
1467 * FAIL_STACK_GROWTH_FACTOR))), \
1468 \
1469 (fail_stack).stack == NULL \
1470 ? 0 \
1471 : ((fail_stack).size \
1472 = (MIN (re_max_failures * TYPICAL_FAILURE_SIZE, \
1473 ((fail_stack).size * sizeof (fail_stack_elt_t) \
1474 * FAIL_STACK_GROWTH_FACTOR)) \
1475 / sizeof (fail_stack_elt_t)), \
1476 1)))
1477
1478
1479 /* Push a pointer value onto the failure stack.
1480 Assumes the variable `fail_stack'. Probably should only
1481 be called from within `PUSH_FAILURE_POINT'. */
1482 #define PUSH_FAILURE_POINTER(item) \
1483 fail_stack.stack[fail_stack.avail++].pointer = (item)
1484
1485 /* This pushes an integer-valued item onto the failure stack.
1486 Assumes the variable `fail_stack'. Probably should only
1487 be called from within `PUSH_FAILURE_POINT'. */
1488 #define PUSH_FAILURE_INT(item) \
1489 fail_stack.stack[fail_stack.avail++].integer = (item)
1490
1491 /* Push a fail_stack_elt_t value onto the failure stack.
1492 Assumes the variable `fail_stack'. Probably should only
1493 be called from within `PUSH_FAILURE_POINT'. */
1494 #define PUSH_FAILURE_ELT(item) \
1495 fail_stack.stack[fail_stack.avail++] = (item)
1496
1497 /* These three POP... operations complement the three PUSH... operations.
1498 All assume that `fail_stack' is nonempty. */
1499 #define POP_FAILURE_POINTER() fail_stack.stack[--fail_stack.avail].pointer
1500 #define POP_FAILURE_INT() fail_stack.stack[--fail_stack.avail].integer
1501 #define POP_FAILURE_ELT() fail_stack.stack[--fail_stack.avail]
1502
1503 /* Individual items aside from the registers. */
1504 #define NUM_NONREG_ITEMS 3
1505
1506 /* Used to examine the stack (to detect infinite loops). */
1507 #define FAILURE_PAT(h) fail_stack.stack[(h) - 1].pointer
1508 #define FAILURE_STR(h) (fail_stack.stack[(h) - 2].pointer)
1509 #define NEXT_FAILURE_HANDLE(h) fail_stack.stack[(h) - 3].integer
1510 #define TOP_FAILURE_HANDLE() fail_stack.frame
1511
1512
1513 #define ENSURE_FAIL_STACK(space) \
1514 while (REMAINING_AVAIL_SLOTS <= space) { \
1515 if (!GROW_FAIL_STACK (fail_stack)) \
1516 return -2; \
1517 DEBUG_PRINT2 ("\n Doubled stack; size now: %d\n", (fail_stack).size);\
1518 DEBUG_PRINT2 (" slots available: %d\n", REMAINING_AVAIL_SLOTS);\
1519 }
1520
1521 /* Push register NUM onto the stack. */
1522 #define PUSH_FAILURE_REG(num) \
1523 do { \
1524 char *destination; \
1525 ENSURE_FAIL_STACK(3); \
1526 DEBUG_PRINT4 (" Push reg %d (spanning %p -> %p)\n", \
1527 num, regstart[num], regend[num]); \
1528 PUSH_FAILURE_POINTER (regstart[num]); \
1529 PUSH_FAILURE_POINTER (regend[num]); \
1530 PUSH_FAILURE_INT (num); \
1531 } while (0)
1532
1533 /* Change the counter's value to VAL, but make sure that it will
1534 be reset when backtracking. */
1535 #define PUSH_NUMBER(ptr,val) \
1536 do { \
1537 char *destination; \
1538 int c; \
1539 ENSURE_FAIL_STACK(3); \
1540 EXTRACT_NUMBER (c, ptr); \
1541 DEBUG_PRINT4 (" Push number %p = %d -> %d\n", ptr, c, val); \
1542 PUSH_FAILURE_INT (c); \
1543 PUSH_FAILURE_POINTER (ptr); \
1544 PUSH_FAILURE_INT (-1); \
1545 STORE_NUMBER (ptr, val); \
1546 } while (0)
1547
1548 /* Pop a saved register off the stack. */
1549 #define POP_FAILURE_REG_OR_COUNT() \
1550 do { \
1551 int reg = POP_FAILURE_INT (); \
1552 if (reg == -1) \
1553 { \
1554 /* It's a counter. */ \
1555 /* Here, we discard `const', making re_match non-reentrant. */ \
1556 unsigned char *ptr = (unsigned char*) POP_FAILURE_POINTER (); \
1557 reg = POP_FAILURE_INT (); \
1558 STORE_NUMBER (ptr, reg); \
1559 DEBUG_PRINT3 (" Pop counter %p = %d\n", ptr, reg); \
1560 } \
1561 else \
1562 { \
1563 regend[reg] = POP_FAILURE_POINTER (); \
1564 regstart[reg] = POP_FAILURE_POINTER (); \
1565 DEBUG_PRINT4 (" Pop reg %d (spanning %p -> %p)\n", \
1566 reg, regstart[reg], regend[reg]); \
1567 } \
1568 } while (0)
1569
1570 /* Check that we are not stuck in an infinite loop. */
1571 #define CHECK_INFINITE_LOOP(pat_cur, string_place) \
1572 do { \
1573 int failure = TOP_FAILURE_HANDLE (); \
1574 /* Check for infinite matching loops */ \
1575 while (failure > 0 \
1576 && (FAILURE_STR (failure) == string_place \
1577 || FAILURE_STR (failure) == NULL)) \
1578 { \
1579 assert (FAILURE_PAT (failure) >= bufp->buffer \
1580 && FAILURE_PAT (failure) <= bufp->buffer + bufp->used); \
1581 if (FAILURE_PAT (failure) == pat_cur) \
1582 { \
1583 cycle = 1; \
1584 break; \
1585 } \
1586 DEBUG_PRINT2 (" Other pattern: %p\n", FAILURE_PAT (failure)); \
1587 failure = NEXT_FAILURE_HANDLE(failure); \
1588 } \
1589 DEBUG_PRINT2 (" Other string: %p\n", FAILURE_STR (failure)); \
1590 } while (0)
1591
1592 /* Push the information about the state we will need
1593 if we ever fail back to it.
1594
1595 Requires variables fail_stack, regstart, regend and
1596 num_regs be declared. GROW_FAIL_STACK requires `destination' be
1597 declared.
1598
1599 Does `return FAILURE_CODE' if runs out of memory. */
1600
1601 #define PUSH_FAILURE_POINT(pattern, string_place) \
1602 do { \
1603 char *destination; \
1604 /* Must be int, so when we don't save any registers, the arithmetic \
1605 of 0 + -1 isn't done as unsigned. */ \
1606 \
1607 DEBUG_STATEMENT (nfailure_points_pushed++); \
1608 DEBUG_PRINT1 ("\nPUSH_FAILURE_POINT:\n"); \
1609 DEBUG_PRINT2 (" Before push, next avail: %d\n", (fail_stack).avail); \
1610 DEBUG_PRINT2 (" size: %d\n", (fail_stack).size);\
1611 \
1612 ENSURE_FAIL_STACK (NUM_NONREG_ITEMS); \
1613 \
1614 DEBUG_PRINT1 ("\n"); \
1615 \
1616 DEBUG_PRINT2 (" Push frame index: %d\n", fail_stack.frame); \
1617 PUSH_FAILURE_INT (fail_stack.frame); \
1618 \
1619 DEBUG_PRINT2 (" Push string %p: `", string_place); \
1620 DEBUG_PRINT_DOUBLE_STRING (string_place, string1, size1, string2, size2);\
1621 DEBUG_PRINT1 ("'\n"); \
1622 PUSH_FAILURE_POINTER (string_place); \
1623 \
1624 DEBUG_PRINT2 (" Push pattern %p: ", pattern); \
1625 DEBUG_PRINT_COMPILED_PATTERN (bufp, pattern, pend); \
1626 PUSH_FAILURE_POINTER (pattern); \
1627 \
1628 /* Close the frame by moving the frame pointer past it. */ \
1629 fail_stack.frame = fail_stack.avail; \
1630 } while (0)
1631
1632 /* Estimate the size of data pushed by a typical failure stack entry.
1633 An estimate is all we need, because all we use this for
1634 is to choose a limit for how big to make the failure stack. */
1635 /* BEWARE, the value `20' is hard-coded in emacs.c:main(). */
1636 #define TYPICAL_FAILURE_SIZE 20
1637
1638 /* How many items can still be added to the stack without overflowing it. */
1639 #define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail)
1640
1641
1642 /* Pops what PUSH_FAIL_STACK pushes.
1643
1644 We restore into the parameters, all of which should be lvalues:
1645 STR -- the saved data position.
1646 PAT -- the saved pattern position.
1647 REGSTART, REGEND -- arrays of string positions.
1648
1649 Also assumes the variables `fail_stack' and (if debugging), `bufp',
1650 `pend', `string1', `size1', `string2', and `size2'. */
1651
1652 #define POP_FAILURE_POINT(str, pat) \
1653 do { \
1654 assert (!FAIL_STACK_EMPTY ()); \
1655 \
1656 /* Remove failure points and point to how many regs pushed. */ \
1657 DEBUG_PRINT1 ("POP_FAILURE_POINT:\n"); \
1658 DEBUG_PRINT2 (" Before pop, next avail: %d\n", fail_stack.avail); \
1659 DEBUG_PRINT2 (" size: %d\n", fail_stack.size); \
1660 \
1661 /* Pop the saved registers. */ \
1662 while (fail_stack.frame < fail_stack.avail) \
1663 POP_FAILURE_REG_OR_COUNT (); \
1664 \
1665 pat = POP_FAILURE_POINTER (); \
1666 DEBUG_PRINT2 (" Popping pattern %p: ", pat); \
1667 DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend); \
1668 \
1669 /* If the saved string location is NULL, it came from an \
1670 on_failure_keep_string_jump opcode, and we want to throw away the \
1671 saved NULL, thus retaining our current position in the string. */ \
1672 str = POP_FAILURE_POINTER (); \
1673 DEBUG_PRINT2 (" Popping string %p: `", str); \
1674 DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2); \
1675 DEBUG_PRINT1 ("'\n"); \
1676 \
1677 fail_stack.frame = POP_FAILURE_INT (); \
1678 DEBUG_PRINT2 (" Popping frame index: %d\n", fail_stack.frame); \
1679 \
1680 assert (fail_stack.avail >= 0); \
1681 assert (fail_stack.frame <= fail_stack.avail); \
1682 \
1683 DEBUG_STATEMENT (nfailure_points_popped++); \
1684 } while (0) /* POP_FAILURE_POINT */
1685
1686
1687 \f
1688 /* Registers are set to a sentinel when they haven't yet matched. */
1689 #define REG_UNSET(e) ((e) == NULL)
1690 \f
1691 /* Subroutine declarations and macros for regex_compile. */
1692
1693 static reg_errcode_t regex_compile _RE_ARGS ((re_char *pattern, size_t size,
1694 reg_syntax_t syntax,
1695 struct re_pattern_buffer *bufp));
1696 static void store_op1 _RE_ARGS ((re_opcode_t op, unsigned char *loc, int arg));
1697 static void store_op2 _RE_ARGS ((re_opcode_t op, unsigned char *loc,
1698 int arg1, int arg2));
1699 static void insert_op1 _RE_ARGS ((re_opcode_t op, unsigned char *loc,
1700 int arg, unsigned char *end));
1701 static void insert_op2 _RE_ARGS ((re_opcode_t op, unsigned char *loc,
1702 int arg1, int arg2, unsigned char *end));
1703 static boolean at_begline_loc_p _RE_ARGS ((re_char *pattern,
1704 re_char *p,
1705 reg_syntax_t syntax));
1706 static boolean at_endline_loc_p _RE_ARGS ((re_char *p,
1707 re_char *pend,
1708 reg_syntax_t syntax));
1709 static re_char *skip_one_char _RE_ARGS ((re_char *p));
1710 static int analyse_first _RE_ARGS ((re_char *p, re_char *pend,
1711 char *fastmap, const int multibyte));
1712
1713 /* Fetch the next character in the uncompiled pattern, with no
1714 translation. */
1715 #define PATFETCH(c) \
1716 do { \
1717 int len; \
1718 if (p == pend) return REG_EEND; \
1719 c = RE_STRING_CHAR_AND_LENGTH (p, pend - p, len); \
1720 p += len; \
1721 } while (0)
1722
1723
1724 /* If `translate' is non-null, return translate[D], else just D. We
1725 cast the subscript to translate because some data is declared as
1726 `char *', to avoid warnings when a string constant is passed. But
1727 when we use a character as a subscript we must make it unsigned. */
1728 #ifndef TRANSLATE
1729 # define TRANSLATE(d) \
1730 (RE_TRANSLATE_P (translate) ? RE_TRANSLATE (translate, (d)) : (d))
1731 #endif
1732
1733
1734 /* Macros for outputting the compiled pattern into `buffer'. */
1735
1736 /* If the buffer isn't allocated when it comes in, use this. */
1737 #define INIT_BUF_SIZE 32
1738
1739 /* Make sure we have at least N more bytes of space in buffer. */
1740 #define GET_BUFFER_SPACE(n) \
1741 while ((size_t) (b - bufp->buffer + (n)) > bufp->allocated) \
1742 EXTEND_BUFFER ()
1743
1744 /* Make sure we have one more byte of buffer space and then add C to it. */
1745 #define BUF_PUSH(c) \
1746 do { \
1747 GET_BUFFER_SPACE (1); \
1748 *b++ = (unsigned char) (c); \
1749 } while (0)
1750
1751
1752 /* Ensure we have two more bytes of buffer space and then append C1 and C2. */
1753 #define BUF_PUSH_2(c1, c2) \
1754 do { \
1755 GET_BUFFER_SPACE (2); \
1756 *b++ = (unsigned char) (c1); \
1757 *b++ = (unsigned char) (c2); \
1758 } while (0)
1759
1760
1761 /* As with BUF_PUSH_2, except for three bytes. */
1762 #define BUF_PUSH_3(c1, c2, c3) \
1763 do { \
1764 GET_BUFFER_SPACE (3); \
1765 *b++ = (unsigned char) (c1); \
1766 *b++ = (unsigned char) (c2); \
1767 *b++ = (unsigned char) (c3); \
1768 } while (0)
1769
1770
1771 /* Store a jump with opcode OP at LOC to location TO. We store a
1772 relative address offset by the three bytes the jump itself occupies. */
1773 #define STORE_JUMP(op, loc, to) \
1774 store_op1 (op, loc, (to) - (loc) - 3)
1775
1776 /* Likewise, for a two-argument jump. */
1777 #define STORE_JUMP2(op, loc, to, arg) \
1778 store_op2 (op, loc, (to) - (loc) - 3, arg)
1779
1780 /* Like `STORE_JUMP', but for inserting. Assume `b' is the buffer end. */
1781 #define INSERT_JUMP(op, loc, to) \
1782 insert_op1 (op, loc, (to) - (loc) - 3, b)
1783
1784 /* Like `STORE_JUMP2', but for inserting. Assume `b' is the buffer end. */
1785 #define INSERT_JUMP2(op, loc, to, arg) \
1786 insert_op2 (op, loc, (to) - (loc) - 3, arg, b)
1787
1788
1789 /* This is not an arbitrary limit: the arguments which represent offsets
1790 into the pattern are two bytes long. So if 2^15 bytes turns out to
1791 be too small, many things would have to change. */
1792 # define MAX_BUF_SIZE (1L << 15)
1793
1794 #if 0 /* This is when we thought it could be 2^16 bytes. */
1795 /* Any other compiler which, like MSC, has allocation limit below 2^16
1796 bytes will have to use approach similar to what was done below for
1797 MSC and drop MAX_BUF_SIZE a bit. Otherwise you may end up
1798 reallocating to 0 bytes. Such thing is not going to work too well.
1799 You have been warned!! */
1800 #if defined _MSC_VER && !defined WIN32
1801 /* Microsoft C 16-bit versions limit malloc to approx 65512 bytes. */
1802 # define MAX_BUF_SIZE 65500L
1803 #else
1804 # define MAX_BUF_SIZE (1L << 16)
1805 #endif
1806 #endif /* 0 */
1807
1808 /* Extend the buffer by twice its current size via realloc and
1809 reset the pointers that pointed into the old block to point to the
1810 correct places in the new one. If extending the buffer results in it
1811 being larger than MAX_BUF_SIZE, then flag memory exhausted. */
1812 #if __BOUNDED_POINTERS__
1813 # define SET_HIGH_BOUND(P) (__ptrhigh (P) = __ptrlow (P) + bufp->allocated)
1814 # define MOVE_BUFFER_POINTER(P) \
1815 (__ptrlow (P) += incr, SET_HIGH_BOUND (P), __ptrvalue (P) += incr)
1816 # define ELSE_EXTEND_BUFFER_HIGH_BOUND \
1817 else \
1818 { \
1819 SET_HIGH_BOUND (b); \
1820 SET_HIGH_BOUND (begalt); \
1821 if (fixup_alt_jump) \
1822 SET_HIGH_BOUND (fixup_alt_jump); \
1823 if (laststart) \
1824 SET_HIGH_BOUND (laststart); \
1825 if (pending_exact) \
1826 SET_HIGH_BOUND (pending_exact); \
1827 }
1828 #else
1829 # define MOVE_BUFFER_POINTER(P) (P) += incr
1830 # define ELSE_EXTEND_BUFFER_HIGH_BOUND
1831 #endif
1832 #define EXTEND_BUFFER() \
1833 do { \
1834 re_char *old_buffer = bufp->buffer; \
1835 if (bufp->allocated == MAX_BUF_SIZE) \
1836 return REG_ESIZE; \
1837 bufp->allocated <<= 1; \
1838 if (bufp->allocated > MAX_BUF_SIZE) \
1839 bufp->allocated = MAX_BUF_SIZE; \
1840 RETALLOC (bufp->buffer, bufp->allocated, unsigned char); \
1841 if (bufp->buffer == NULL) \
1842 return REG_ESPACE; \
1843 /* If the buffer moved, move all the pointers into it. */ \
1844 if (old_buffer != bufp->buffer) \
1845 { \
1846 int incr = bufp->buffer - old_buffer; \
1847 MOVE_BUFFER_POINTER (b); \
1848 MOVE_BUFFER_POINTER (begalt); \
1849 if (fixup_alt_jump) \
1850 MOVE_BUFFER_POINTER (fixup_alt_jump); \
1851 if (laststart) \
1852 MOVE_BUFFER_POINTER (laststart); \
1853 if (pending_exact) \
1854 MOVE_BUFFER_POINTER (pending_exact); \
1855 } \
1856 ELSE_EXTEND_BUFFER_HIGH_BOUND \
1857 } while (0)
1858
1859
1860 /* Since we have one byte reserved for the register number argument to
1861 {start,stop}_memory, the maximum number of groups we can report
1862 things about is what fits in that byte. */
1863 #define MAX_REGNUM 255
1864
1865 /* But patterns can have more than `MAX_REGNUM' registers. We just
1866 ignore the excess. */
1867 typedef int regnum_t;
1868
1869
1870 /* Macros for the compile stack. */
1871
1872 /* Since offsets can go either forwards or backwards, this type needs to
1873 be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1. */
1874 /* int may be not enough when sizeof(int) == 2. */
1875 typedef long pattern_offset_t;
1876
1877 typedef struct
1878 {
1879 pattern_offset_t begalt_offset;
1880 pattern_offset_t fixup_alt_jump;
1881 pattern_offset_t laststart_offset;
1882 regnum_t regnum;
1883 } compile_stack_elt_t;
1884
1885
1886 typedef struct
1887 {
1888 compile_stack_elt_t *stack;
1889 unsigned size;
1890 unsigned avail; /* Offset of next open position. */
1891 } compile_stack_type;
1892
1893
1894 #define INIT_COMPILE_STACK_SIZE 32
1895
1896 #define COMPILE_STACK_EMPTY (compile_stack.avail == 0)
1897 #define COMPILE_STACK_FULL (compile_stack.avail == compile_stack.size)
1898
1899 /* The next available element. */
1900 #define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
1901
1902 /* Explicit quit checking is only used on NTemacs. */
1903 #if defined WINDOWSNT && defined emacs && defined QUIT
1904 extern int immediate_quit;
1905 # define IMMEDIATE_QUIT_CHECK \
1906 do { \
1907 if (immediate_quit) QUIT; \
1908 } while (0)
1909 #else
1910 # define IMMEDIATE_QUIT_CHECK ((void)0)
1911 #endif
1912 \f
1913 /* Structure to manage work area for range table. */
1914 struct range_table_work_area
1915 {
1916 int *table; /* actual work area. */
1917 int allocated; /* allocated size for work area in bytes. */
1918 int used; /* actually used size in words. */
1919 int bits; /* flag to record character classes */
1920 };
1921
1922 /* Make sure that WORK_AREA can hold more N multibyte characters.
1923 This is used only in set_image_of_range and set_image_of_range_1.
1924 It expects WORK_AREA to be a pointer.
1925 If it can't get the space, it returns from the surrounding function. */
1926
1927 #define EXTEND_RANGE_TABLE(work_area, n) \
1928 do { \
1929 if (((work_area).used + (n)) * sizeof (int) > (work_area).allocated) \
1930 { \
1931 extend_range_table_work_area (&work_area); \
1932 if ((work_area).table == 0) \
1933 return (REG_ESPACE); \
1934 } \
1935 } while (0)
1936
1937 #define SET_RANGE_TABLE_WORK_AREA_BIT(work_area, bit) \
1938 (work_area).bits |= (bit)
1939
1940 /* Bits used to implement the multibyte-part of the various character classes
1941 such as [:alnum:] in a charset's range table. */
1942 #define BIT_WORD 0x1
1943 #define BIT_LOWER 0x2
1944 #define BIT_PUNCT 0x4
1945 #define BIT_SPACE 0x8
1946 #define BIT_UPPER 0x10
1947 #define BIT_MULTIBYTE 0x20
1948
1949 /* Set a range (RANGE_START, RANGE_END) to WORK_AREA. */
1950 #define SET_RANGE_TABLE_WORK_AREA(work_area, range_start, range_end) \
1951 do { \
1952 EXTEND_RANGE_TABLE ((work_area), 2); \
1953 (work_area).table[(work_area).used++] = (range_start); \
1954 (work_area).table[(work_area).used++] = (range_end); \
1955 } while (0)
1956
1957 /* Free allocated memory for WORK_AREA. */
1958 #define FREE_RANGE_TABLE_WORK_AREA(work_area) \
1959 do { \
1960 if ((work_area).table) \
1961 free ((work_area).table); \
1962 } while (0)
1963
1964 #define CLEAR_RANGE_TABLE_WORK_USED(work_area) ((work_area).used = 0, (work_area).bits = 0)
1965 #define RANGE_TABLE_WORK_USED(work_area) ((work_area).used)
1966 #define RANGE_TABLE_WORK_BITS(work_area) ((work_area).bits)
1967 #define RANGE_TABLE_WORK_ELT(work_area, i) ((work_area).table[i])
1968 \f
1969
1970 /* Set the bit for character C in a list. */
1971 #define SET_LIST_BIT(c) (b[((c)) / BYTEWIDTH] |= 1 << ((c) % BYTEWIDTH))
1972
1973
1974 #ifdef emacs
1975
1976 /* Store characters in the rage range C0 to C1 in WORK_AREA while
1977 translating them and paying attention to the continuity of
1978 translated characters.
1979
1980 Implementation note: It is better to implement this fairly big
1981 macro by a function, but it's not that easy because macros called
1982 in this macro assume various local variables already declared. */
1983
1984 #define SETUP_MULTIBYTE_RANGE(work_area, c0, c1) \
1985 do { \
1986 re_wchar_t c, t, t_last; \
1987 int n; \
1988 \
1989 c = (c0); \
1990 t_last = multibyte ? TRANSLATE (c) : TRANSLATE (MAKE_CHAR_MULTIBYTE (c)); \
1991 for (c++, n = 1; c <= (c1); c++, n++) \
1992 { \
1993 t = multibyte ? TRANSLATE (c) : TRANSLATE (MAKE_CHAR_MULTIBYTE (c)); \
1994 if (t_last + n == t) \
1995 continue; \
1996 SET_RANGE_TABLE_WORK_AREA ((work_area), t_last, t_last + n - 1); \
1997 t_last = t; \
1998 n = 0; \
1999 } \
2000 if (n > 0) \
2001 SET_RANGE_TABLE_WORK_AREA ((work_area), t_last, t_last + n - 1); \
2002 } while (0)
2003
2004 #endif /* emacs */
2005
2006 /* Get the next unsigned number in the uncompiled pattern. */
2007 #define GET_UNSIGNED_NUMBER(num) \
2008 do { \
2009 if (p == pend) \
2010 FREE_STACK_RETURN (REG_EBRACE); \
2011 else \
2012 { \
2013 PATFETCH (c); \
2014 while ('0' <= c && c <= '9') \
2015 { \
2016 int prev; \
2017 if (num < 0) \
2018 num = 0; \
2019 prev = num; \
2020 num = num * 10 + c - '0'; \
2021 if (num / 10 != prev) \
2022 FREE_STACK_RETURN (REG_BADBR); \
2023 if (p == pend) \
2024 FREE_STACK_RETURN (REG_EBRACE); \
2025 PATFETCH (c); \
2026 } \
2027 } \
2028 } while (0)
2029 \f
2030 #if ! WIDE_CHAR_SUPPORT
2031
2032 /* Map a string to the char class it names (if any). */
2033 re_wctype_t
2034 re_wctype (str)
2035 re_char *str;
2036 {
2037 const char *string = str;
2038 if (STREQ (string, "alnum")) return RECC_ALNUM;
2039 else if (STREQ (string, "alpha")) return RECC_ALPHA;
2040 else if (STREQ (string, "word")) return RECC_WORD;
2041 else if (STREQ (string, "ascii")) return RECC_ASCII;
2042 else if (STREQ (string, "nonascii")) return RECC_NONASCII;
2043 else if (STREQ (string, "graph")) return RECC_GRAPH;
2044 else if (STREQ (string, "lower")) return RECC_LOWER;
2045 else if (STREQ (string, "print")) return RECC_PRINT;
2046 else if (STREQ (string, "punct")) return RECC_PUNCT;
2047 else if (STREQ (string, "space")) return RECC_SPACE;
2048 else if (STREQ (string, "upper")) return RECC_UPPER;
2049 else if (STREQ (string, "unibyte")) return RECC_UNIBYTE;
2050 else if (STREQ (string, "multibyte")) return RECC_MULTIBYTE;
2051 else if (STREQ (string, "digit")) return RECC_DIGIT;
2052 else if (STREQ (string, "xdigit")) return RECC_XDIGIT;
2053 else if (STREQ (string, "cntrl")) return RECC_CNTRL;
2054 else if (STREQ (string, "blank")) return RECC_BLANK;
2055 else return 0;
2056 }
2057
2058 /* True iff CH is in the char class CC. */
2059 boolean
2060 re_iswctype (ch, cc)
2061 int ch;
2062 re_wctype_t cc;
2063 {
2064 switch (cc)
2065 {
2066 case RECC_ALNUM: return ISALNUM (ch);
2067 case RECC_ALPHA: return ISALPHA (ch);
2068 case RECC_BLANK: return ISBLANK (ch);
2069 case RECC_CNTRL: return ISCNTRL (ch);
2070 case RECC_DIGIT: return ISDIGIT (ch);
2071 case RECC_GRAPH: return ISGRAPH (ch);
2072 case RECC_LOWER: return ISLOWER (ch);
2073 case RECC_PRINT: return ISPRINT (ch);
2074 case RECC_PUNCT: return ISPUNCT (ch);
2075 case RECC_SPACE: return ISSPACE (ch);
2076 case RECC_UPPER: return ISUPPER (ch);
2077 case RECC_XDIGIT: return ISXDIGIT (ch);
2078 case RECC_ASCII: return IS_REAL_ASCII (ch);
2079 case RECC_NONASCII: return !IS_REAL_ASCII (ch);
2080 case RECC_UNIBYTE: return ISUNIBYTE (ch);
2081 case RECC_MULTIBYTE: return !ISUNIBYTE (ch);
2082 case RECC_WORD: return ISWORD (ch);
2083 case RECC_ERROR: return false;
2084 default:
2085 abort();
2086 }
2087 }
2088
2089 /* Return a bit-pattern to use in the range-table bits to match multibyte
2090 chars of class CC. */
2091 static int
2092 re_wctype_to_bit (cc)
2093 re_wctype_t cc;
2094 {
2095 switch (cc)
2096 {
2097 case RECC_NONASCII: case RECC_PRINT: case RECC_GRAPH:
2098 case RECC_MULTIBYTE: return BIT_MULTIBYTE;
2099 case RECC_ALPHA: case RECC_ALNUM: case RECC_WORD: return BIT_WORD;
2100 case RECC_LOWER: return BIT_LOWER;
2101 case RECC_UPPER: return BIT_UPPER;
2102 case RECC_PUNCT: return BIT_PUNCT;
2103 case RECC_SPACE: return BIT_SPACE;
2104 case RECC_ASCII: case RECC_DIGIT: case RECC_XDIGIT: case RECC_CNTRL:
2105 case RECC_BLANK: case RECC_UNIBYTE: case RECC_ERROR: return 0;
2106 default:
2107 abort();
2108 }
2109 }
2110 #endif
2111 \f
2112 /* Filling in the work area of a range. */
2113
2114 /* Actually extend the space in WORK_AREA. */
2115
2116 static void
2117 extend_range_table_work_area (work_area)
2118 struct range_table_work_area *work_area;
2119 {
2120 work_area->allocated += 16 * sizeof (int);
2121 if (work_area->table)
2122 work_area->table
2123 = (int *) realloc (work_area->table, work_area->allocated);
2124 else
2125 work_area->table
2126 = (int *) malloc (work_area->allocated);
2127 }
2128
2129 #if 0
2130 #ifdef emacs
2131
2132 /* Carefully find the ranges of codes that are equivalent
2133 under case conversion to the range start..end when passed through
2134 TRANSLATE. Handle the case where non-letters can come in between
2135 two upper-case letters (which happens in Latin-1).
2136 Also handle the case of groups of more than 2 case-equivalent chars.
2137
2138 The basic method is to look at consecutive characters and see
2139 if they can form a run that can be handled as one.
2140
2141 Returns -1 if successful, REG_ESPACE if ran out of space. */
2142
2143 static int
2144 set_image_of_range_1 (work_area, start, end, translate)
2145 RE_TRANSLATE_TYPE translate;
2146 struct range_table_work_area *work_area;
2147 re_wchar_t start, end;
2148 {
2149 /* `one_case' indicates a character, or a run of characters,
2150 each of which is an isolate (no case-equivalents).
2151 This includes all ASCII non-letters.
2152
2153 `two_case' indicates a character, or a run of characters,
2154 each of which has two case-equivalent forms.
2155 This includes all ASCII letters.
2156
2157 `strange' indicates a character that has more than one
2158 case-equivalent. */
2159
2160 enum case_type {one_case, two_case, strange};
2161
2162 /* Describe the run that is in progress,
2163 which the next character can try to extend.
2164 If run_type is strange, that means there really is no run.
2165 If run_type is one_case, then run_start...run_end is the run.
2166 If run_type is two_case, then the run is run_start...run_end,
2167 and the case-equivalents end at run_eqv_end. */
2168
2169 enum case_type run_type = strange;
2170 int run_start, run_end, run_eqv_end;
2171
2172 Lisp_Object eqv_table;
2173
2174 if (!RE_TRANSLATE_P (translate))
2175 {
2176 EXTEND_RANGE_TABLE (work_area, 2);
2177 work_area->table[work_area->used++] = (start);
2178 work_area->table[work_area->used++] = (end);
2179 return -1;
2180 }
2181
2182 eqv_table = XCHAR_TABLE (translate)->extras[2];
2183
2184 for (; start <= end; start++)
2185 {
2186 enum case_type this_type;
2187 int eqv = RE_TRANSLATE (eqv_table, start);
2188 int minchar, maxchar;
2189
2190 /* Classify this character */
2191 if (eqv == start)
2192 this_type = one_case;
2193 else if (RE_TRANSLATE (eqv_table, eqv) == start)
2194 this_type = two_case;
2195 else
2196 this_type = strange;
2197
2198 if (start < eqv)
2199 minchar = start, maxchar = eqv;
2200 else
2201 minchar = eqv, maxchar = start;
2202
2203 /* Can this character extend the run in progress? */
2204 if (this_type == strange || this_type != run_type
2205 || !(minchar == run_end + 1
2206 && (run_type == two_case
2207 ? maxchar == run_eqv_end + 1 : 1)))
2208 {
2209 /* No, end the run.
2210 Record each of its equivalent ranges. */
2211 if (run_type == one_case)
2212 {
2213 EXTEND_RANGE_TABLE (work_area, 2);
2214 work_area->table[work_area->used++] = run_start;
2215 work_area->table[work_area->used++] = run_end;
2216 }
2217 else if (run_type == two_case)
2218 {
2219 EXTEND_RANGE_TABLE (work_area, 4);
2220 work_area->table[work_area->used++] = run_start;
2221 work_area->table[work_area->used++] = run_end;
2222 work_area->table[work_area->used++]
2223 = RE_TRANSLATE (eqv_table, run_start);
2224 work_area->table[work_area->used++]
2225 = RE_TRANSLATE (eqv_table, run_end);
2226 }
2227 run_type = strange;
2228 }
2229
2230 if (this_type == strange)
2231 {
2232 /* For a strange character, add each of its equivalents, one
2233 by one. Don't start a range. */
2234 do
2235 {
2236 EXTEND_RANGE_TABLE (work_area, 2);
2237 work_area->table[work_area->used++] = eqv;
2238 work_area->table[work_area->used++] = eqv;
2239 eqv = RE_TRANSLATE (eqv_table, eqv);
2240 }
2241 while (eqv != start);
2242 }
2243
2244 /* Add this char to the run, or start a new run. */
2245 else if (run_type == strange)
2246 {
2247 /* Initialize a new range. */
2248 run_type = this_type;
2249 run_start = start;
2250 run_end = start;
2251 run_eqv_end = RE_TRANSLATE (eqv_table, run_end);
2252 }
2253 else
2254 {
2255 /* Extend a running range. */
2256 run_end = minchar;
2257 run_eqv_end = RE_TRANSLATE (eqv_table, run_end);
2258 }
2259 }
2260
2261 /* If a run is still in progress at the end, finish it now
2262 by recording its equivalent ranges. */
2263 if (run_type == one_case)
2264 {
2265 EXTEND_RANGE_TABLE (work_area, 2);
2266 work_area->table[work_area->used++] = run_start;
2267 work_area->table[work_area->used++] = run_end;
2268 }
2269 else if (run_type == two_case)
2270 {
2271 EXTEND_RANGE_TABLE (work_area, 4);
2272 work_area->table[work_area->used++] = run_start;
2273 work_area->table[work_area->used++] = run_end;
2274 work_area->table[work_area->used++]
2275 = RE_TRANSLATE (eqv_table, run_start);
2276 work_area->table[work_area->used++]
2277 = RE_TRANSLATE (eqv_table, run_end);
2278 }
2279
2280 return -1;
2281 }
2282
2283 #endif /* emacs */
2284
2285 /* Record the the image of the range start..end when passed through
2286 TRANSLATE. This is not necessarily TRANSLATE(start)..TRANSLATE(end)
2287 and is not even necessarily contiguous.
2288 Normally we approximate it with the smallest contiguous range that contains
2289 all the chars we need. However, for Latin-1 we go to extra effort
2290 to do a better job.
2291
2292 This function is not called for ASCII ranges.
2293
2294 Returns -1 if successful, REG_ESPACE if ran out of space. */
2295
2296 static int
2297 set_image_of_range (work_area, start, end, translate)
2298 RE_TRANSLATE_TYPE translate;
2299 struct range_table_work_area *work_area;
2300 re_wchar_t start, end;
2301 {
2302 re_wchar_t cmin, cmax;
2303
2304 #ifdef emacs
2305 /* For Latin-1 ranges, use set_image_of_range_1
2306 to get proper handling of ranges that include letters and nonletters.
2307 For a range that includes the whole of Latin-1, this is not necessary.
2308 For other character sets, we don't bother to get this right. */
2309 if (RE_TRANSLATE_P (translate) && start < 04400
2310 && !(start < 04200 && end >= 04377))
2311 {
2312 int newend;
2313 int tem;
2314 newend = end;
2315 if (newend > 04377)
2316 newend = 04377;
2317 tem = set_image_of_range_1 (work_area, start, newend, translate);
2318 if (tem > 0)
2319 return tem;
2320
2321 start = 04400;
2322 if (end < 04400)
2323 return -1;
2324 }
2325 #endif
2326
2327 EXTEND_RANGE_TABLE (work_area, 2);
2328 work_area->table[work_area->used++] = (start);
2329 work_area->table[work_area->used++] = (end);
2330
2331 cmin = -1, cmax = -1;
2332
2333 if (RE_TRANSLATE_P (translate))
2334 {
2335 int ch;
2336
2337 for (ch = start; ch <= end; ch++)
2338 {
2339 re_wchar_t c = TRANSLATE (ch);
2340 if (! (start <= c && c <= end))
2341 {
2342 if (cmin == -1)
2343 cmin = c, cmax = c;
2344 else
2345 {
2346 cmin = MIN (cmin, c);
2347 cmax = MAX (cmax, c);
2348 }
2349 }
2350 }
2351
2352 if (cmin != -1)
2353 {
2354 EXTEND_RANGE_TABLE (work_area, 2);
2355 work_area->table[work_area->used++] = (cmin);
2356 work_area->table[work_area->used++] = (cmax);
2357 }
2358 }
2359
2360 return -1;
2361 }
2362 #endif /* 0 */
2363 \f
2364 #ifndef MATCH_MAY_ALLOCATE
2365
2366 /* If we cannot allocate large objects within re_match_2_internal,
2367 we make the fail stack and register vectors global.
2368 The fail stack, we grow to the maximum size when a regexp
2369 is compiled.
2370 The register vectors, we adjust in size each time we
2371 compile a regexp, according to the number of registers it needs. */
2372
2373 static fail_stack_type fail_stack;
2374
2375 /* Size with which the following vectors are currently allocated.
2376 That is so we can make them bigger as needed,
2377 but never make them smaller. */
2378 static int regs_allocated_size;
2379
2380 static re_char ** regstart, ** regend;
2381 static re_char **best_regstart, **best_regend;
2382
2383 /* Make the register vectors big enough for NUM_REGS registers,
2384 but don't make them smaller. */
2385
2386 static
2387 regex_grow_registers (num_regs)
2388 int num_regs;
2389 {
2390 if (num_regs > regs_allocated_size)
2391 {
2392 RETALLOC_IF (regstart, num_regs, re_char *);
2393 RETALLOC_IF (regend, num_regs, re_char *);
2394 RETALLOC_IF (best_regstart, num_regs, re_char *);
2395 RETALLOC_IF (best_regend, num_regs, re_char *);
2396
2397 regs_allocated_size = num_regs;
2398 }
2399 }
2400
2401 #endif /* not MATCH_MAY_ALLOCATE */
2402 \f
2403 static boolean group_in_compile_stack _RE_ARGS ((compile_stack_type
2404 compile_stack,
2405 regnum_t regnum));
2406
2407 /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
2408 Returns one of error codes defined in `regex.h', or zero for success.
2409
2410 Assumes the `allocated' (and perhaps `buffer') and `translate'
2411 fields are set in BUFP on entry.
2412
2413 If it succeeds, results are put in BUFP (if it returns an error, the
2414 contents of BUFP are undefined):
2415 `buffer' is the compiled pattern;
2416 `syntax' is set to SYNTAX;
2417 `used' is set to the length of the compiled pattern;
2418 `fastmap_accurate' is zero;
2419 `re_nsub' is the number of subexpressions in PATTERN;
2420 `not_bol' and `not_eol' are zero;
2421
2422 The `fastmap' field is neither examined nor set. */
2423
2424 /* Insert the `jump' from the end of last alternative to "here".
2425 The space for the jump has already been allocated. */
2426 #define FIXUP_ALT_JUMP() \
2427 do { \
2428 if (fixup_alt_jump) \
2429 STORE_JUMP (jump, fixup_alt_jump, b); \
2430 } while (0)
2431
2432
2433 /* Return, freeing storage we allocated. */
2434 #define FREE_STACK_RETURN(value) \
2435 do { \
2436 FREE_RANGE_TABLE_WORK_AREA (range_table_work); \
2437 free (compile_stack.stack); \
2438 return value; \
2439 } while (0)
2440
2441 static reg_errcode_t
2442 regex_compile (pattern, size, syntax, bufp)
2443 re_char *pattern;
2444 size_t size;
2445 reg_syntax_t syntax;
2446 struct re_pattern_buffer *bufp;
2447 {
2448 /* We fetch characters from PATTERN here. */
2449 register re_wchar_t c, c1;
2450
2451 /* A random temporary spot in PATTERN. */
2452 re_char *p1;
2453
2454 /* Points to the end of the buffer, where we should append. */
2455 register unsigned char *b;
2456
2457 /* Keeps track of unclosed groups. */
2458 compile_stack_type compile_stack;
2459
2460 /* Points to the current (ending) position in the pattern. */
2461 #ifdef AIX
2462 /* `const' makes AIX compiler fail. */
2463 unsigned char *p = pattern;
2464 #else
2465 re_char *p = pattern;
2466 #endif
2467 re_char *pend = pattern + size;
2468
2469 /* How to translate the characters in the pattern. */
2470 RE_TRANSLATE_TYPE translate = bufp->translate;
2471
2472 /* Address of the count-byte of the most recently inserted `exactn'
2473 command. This makes it possible to tell if a new exact-match
2474 character can be added to that command or if the character requires
2475 a new `exactn' command. */
2476 unsigned char *pending_exact = 0;
2477
2478 /* Address of start of the most recently finished expression.
2479 This tells, e.g., postfix * where to find the start of its
2480 operand. Reset at the beginning of groups and alternatives. */
2481 unsigned char *laststart = 0;
2482
2483 /* Address of beginning of regexp, or inside of last group. */
2484 unsigned char *begalt;
2485
2486 /* Place in the uncompiled pattern (i.e., the {) to
2487 which to go back if the interval is invalid. */
2488 re_char *beg_interval;
2489
2490 /* Address of the place where a forward jump should go to the end of
2491 the containing expression. Each alternative of an `or' -- except the
2492 last -- ends with a forward jump of this sort. */
2493 unsigned char *fixup_alt_jump = 0;
2494
2495 /* Counts open-groups as they are encountered. Remembered for the
2496 matching close-group on the compile stack, so the same register
2497 number is put in the stop_memory as the start_memory. */
2498 regnum_t regnum = 0;
2499
2500 /* Work area for range table of charset. */
2501 struct range_table_work_area range_table_work;
2502
2503 /* If the object matched can contain multibyte characters. */
2504 const boolean multibyte = RE_MULTIBYTE_P (bufp);
2505
2506 /* If a target of matching can contain multibyte characters. */
2507 const boolean target_multibyte = RE_TARGET_MULTIBYTE_P (bufp);
2508
2509 /* Nonzero if we have pushed down into a subpattern. */
2510 int in_subpattern = 0;
2511
2512 /* These hold the values of p, pattern, and pend from the main
2513 pattern when we have pushed into a subpattern. */
2514 re_char *main_p;
2515 re_char *main_pattern;
2516 re_char *main_pend;
2517
2518 #ifdef DEBUG
2519 debug++;
2520 DEBUG_PRINT1 ("\nCompiling pattern: ");
2521 if (debug > 0)
2522 {
2523 unsigned debug_count;
2524
2525 for (debug_count = 0; debug_count < size; debug_count++)
2526 putchar (pattern[debug_count]);
2527 putchar ('\n');
2528 }
2529 #endif /* DEBUG */
2530
2531 /* Initialize the compile stack. */
2532 compile_stack.stack = TALLOC (INIT_COMPILE_STACK_SIZE, compile_stack_elt_t);
2533 if (compile_stack.stack == NULL)
2534 return REG_ESPACE;
2535
2536 compile_stack.size = INIT_COMPILE_STACK_SIZE;
2537 compile_stack.avail = 0;
2538
2539 range_table_work.table = 0;
2540 range_table_work.allocated = 0;
2541
2542 /* Initialize the pattern buffer. */
2543 bufp->syntax = syntax;
2544 bufp->fastmap_accurate = 0;
2545 bufp->not_bol = bufp->not_eol = 0;
2546
2547 /* Set `used' to zero, so that if we return an error, the pattern
2548 printer (for debugging) will think there's no pattern. We reset it
2549 at the end. */
2550 bufp->used = 0;
2551
2552 /* Always count groups, whether or not bufp->no_sub is set. */
2553 bufp->re_nsub = 0;
2554
2555 #if !defined emacs && !defined SYNTAX_TABLE
2556 /* Initialize the syntax table. */
2557 init_syntax_once ();
2558 #endif
2559
2560 if (bufp->allocated == 0)
2561 {
2562 if (bufp->buffer)
2563 { /* If zero allocated, but buffer is non-null, try to realloc
2564 enough space. This loses if buffer's address is bogus, but
2565 that is the user's responsibility. */
2566 RETALLOC (bufp->buffer, INIT_BUF_SIZE, unsigned char);
2567 }
2568 else
2569 { /* Caller did not allocate a buffer. Do it for them. */
2570 bufp->buffer = TALLOC (INIT_BUF_SIZE, unsigned char);
2571 }
2572 if (!bufp->buffer) FREE_STACK_RETURN (REG_ESPACE);
2573
2574 bufp->allocated = INIT_BUF_SIZE;
2575 }
2576
2577 begalt = b = bufp->buffer;
2578
2579 /* Loop through the uncompiled pattern until we're at the end. */
2580 while (1)
2581 {
2582 if (p == pend)
2583 {
2584 /* If this is the end of an included regexp,
2585 pop back to the main regexp and try again. */
2586 if (in_subpattern)
2587 {
2588 in_subpattern = 0;
2589 pattern = main_pattern;
2590 p = main_p;
2591 pend = main_pend;
2592 continue;
2593 }
2594 /* If this is the end of the main regexp, we are done. */
2595 break;
2596 }
2597
2598 PATFETCH (c);
2599
2600 switch (c)
2601 {
2602 case ' ':
2603 {
2604 re_char *p1 = p;
2605
2606 /* If there's no special whitespace regexp, treat
2607 spaces normally. And don't try to do this recursively. */
2608 if (!whitespace_regexp || in_subpattern)
2609 goto normal_char;
2610
2611 /* Peek past following spaces. */
2612 while (p1 != pend)
2613 {
2614 if (*p1 != ' ')
2615 break;
2616 p1++;
2617 }
2618 /* If the spaces are followed by a repetition op,
2619 treat them normally. */
2620 if (p1 != pend
2621 && (*p1 == '*' || *p1 == '+' || *p1 == '?'
2622 || (*p1 == '\\' && p1 + 1 != pend && p1[1] == '{')))
2623 goto normal_char;
2624
2625 /* Replace the spaces with the whitespace regexp. */
2626 in_subpattern = 1;
2627 main_p = p1;
2628 main_pend = pend;
2629 main_pattern = pattern;
2630 p = pattern = whitespace_regexp;
2631 pend = p + strlen (p);
2632 break;
2633 }
2634
2635 case '^':
2636 {
2637 if ( /* If at start of pattern, it's an operator. */
2638 p == pattern + 1
2639 /* If context independent, it's an operator. */
2640 || syntax & RE_CONTEXT_INDEP_ANCHORS
2641 /* Otherwise, depends on what's come before. */
2642 || at_begline_loc_p (pattern, p, syntax))
2643 BUF_PUSH ((syntax & RE_NO_NEWLINE_ANCHOR) ? begbuf : begline);
2644 else
2645 goto normal_char;
2646 }
2647 break;
2648
2649
2650 case '$':
2651 {
2652 if ( /* If at end of pattern, it's an operator. */
2653 p == pend
2654 /* If context independent, it's an operator. */
2655 || syntax & RE_CONTEXT_INDEP_ANCHORS
2656 /* Otherwise, depends on what's next. */
2657 || at_endline_loc_p (p, pend, syntax))
2658 BUF_PUSH ((syntax & RE_NO_NEWLINE_ANCHOR) ? endbuf : endline);
2659 else
2660 goto normal_char;
2661 }
2662 break;
2663
2664
2665 case '+':
2666 case '?':
2667 if ((syntax & RE_BK_PLUS_QM)
2668 || (syntax & RE_LIMITED_OPS))
2669 goto normal_char;
2670 handle_plus:
2671 case '*':
2672 /* If there is no previous pattern... */
2673 if (!laststart)
2674 {
2675 if (syntax & RE_CONTEXT_INVALID_OPS)
2676 FREE_STACK_RETURN (REG_BADRPT);
2677 else if (!(syntax & RE_CONTEXT_INDEP_OPS))
2678 goto normal_char;
2679 }
2680
2681 {
2682 /* 1 means zero (many) matches is allowed. */
2683 boolean zero_times_ok = 0, many_times_ok = 0;
2684 boolean greedy = 1;
2685
2686 /* If there is a sequence of repetition chars, collapse it
2687 down to just one (the right one). We can't combine
2688 interval operators with these because of, e.g., `a{2}*',
2689 which should only match an even number of `a's. */
2690
2691 for (;;)
2692 {
2693 if ((syntax & RE_FRUGAL)
2694 && c == '?' && (zero_times_ok || many_times_ok))
2695 greedy = 0;
2696 else
2697 {
2698 zero_times_ok |= c != '+';
2699 many_times_ok |= c != '?';
2700 }
2701
2702 if (p == pend)
2703 break;
2704 else if (*p == '*'
2705 || (!(syntax & RE_BK_PLUS_QM)
2706 && (*p == '+' || *p == '?')))
2707 ;
2708 else if (syntax & RE_BK_PLUS_QM && *p == '\\')
2709 {
2710 if (p+1 == pend)
2711 FREE_STACK_RETURN (REG_EESCAPE);
2712 if (p[1] == '+' || p[1] == '?')
2713 PATFETCH (c); /* Gobble up the backslash. */
2714 else
2715 break;
2716 }
2717 else
2718 break;
2719 /* If we get here, we found another repeat character. */
2720 PATFETCH (c);
2721 }
2722
2723 /* Star, etc. applied to an empty pattern is equivalent
2724 to an empty pattern. */
2725 if (!laststart || laststart == b)
2726 break;
2727
2728 /* Now we know whether or not zero matches is allowed
2729 and also whether or not two or more matches is allowed. */
2730 if (greedy)
2731 {
2732 if (many_times_ok)
2733 {
2734 boolean simple = skip_one_char (laststart) == b;
2735 unsigned int startoffset = 0;
2736 re_opcode_t ofj =
2737 /* Check if the loop can match the empty string. */
2738 (simple || !analyse_first (laststart, b, NULL, 0))
2739 ? on_failure_jump : on_failure_jump_loop;
2740 assert (skip_one_char (laststart) <= b);
2741
2742 if (!zero_times_ok && simple)
2743 { /* Since simple * loops can be made faster by using
2744 on_failure_keep_string_jump, we turn simple P+
2745 into PP* if P is simple. */
2746 unsigned char *p1, *p2;
2747 startoffset = b - laststart;
2748 GET_BUFFER_SPACE (startoffset);
2749 p1 = b; p2 = laststart;
2750 while (p2 < p1)
2751 *b++ = *p2++;
2752 zero_times_ok = 1;
2753 }
2754
2755 GET_BUFFER_SPACE (6);
2756 if (!zero_times_ok)
2757 /* A + loop. */
2758 STORE_JUMP (ofj, b, b + 6);
2759 else
2760 /* Simple * loops can use on_failure_keep_string_jump
2761 depending on what follows. But since we don't know
2762 that yet, we leave the decision up to
2763 on_failure_jump_smart. */
2764 INSERT_JUMP (simple ? on_failure_jump_smart : ofj,
2765 laststart + startoffset, b + 6);
2766 b += 3;
2767 STORE_JUMP (jump, b, laststart + startoffset);
2768 b += 3;
2769 }
2770 else
2771 {
2772 /* A simple ? pattern. */
2773 assert (zero_times_ok);
2774 GET_BUFFER_SPACE (3);
2775 INSERT_JUMP (on_failure_jump, laststart, b + 3);
2776 b += 3;
2777 }
2778 }
2779 else /* not greedy */
2780 { /* I wish the greedy and non-greedy cases could be merged. */
2781
2782 GET_BUFFER_SPACE (7); /* We might use less. */
2783 if (many_times_ok)
2784 {
2785 boolean emptyp = analyse_first (laststart, b, NULL, 0);
2786
2787 /* The non-greedy multiple match looks like
2788 a repeat..until: we only need a conditional jump
2789 at the end of the loop. */
2790 if (emptyp) BUF_PUSH (no_op);
2791 STORE_JUMP (emptyp ? on_failure_jump_nastyloop
2792 : on_failure_jump, b, laststart);
2793 b += 3;
2794 if (zero_times_ok)
2795 {
2796 /* The repeat...until naturally matches one or more.
2797 To also match zero times, we need to first jump to
2798 the end of the loop (its conditional jump). */
2799 INSERT_JUMP (jump, laststart, b);
2800 b += 3;
2801 }
2802 }
2803 else
2804 {
2805 /* non-greedy a?? */
2806 INSERT_JUMP (jump, laststart, b + 3);
2807 b += 3;
2808 INSERT_JUMP (on_failure_jump, laststart, laststart + 6);
2809 b += 3;
2810 }
2811 }
2812 }
2813 pending_exact = 0;
2814 break;
2815
2816
2817 case '.':
2818 laststart = b;
2819 BUF_PUSH (anychar);
2820 break;
2821
2822
2823 case '[':
2824 {
2825 CLEAR_RANGE_TABLE_WORK_USED (range_table_work);
2826
2827 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2828
2829 /* Ensure that we have enough space to push a charset: the
2830 opcode, the length count, and the bitset; 34 bytes in all. */
2831 GET_BUFFER_SPACE (34);
2832
2833 laststart = b;
2834
2835 /* We test `*p == '^' twice, instead of using an if
2836 statement, so we only need one BUF_PUSH. */
2837 BUF_PUSH (*p == '^' ? charset_not : charset);
2838 if (*p == '^')
2839 p++;
2840
2841 /* Remember the first position in the bracket expression. */
2842 p1 = p;
2843
2844 /* Push the number of bytes in the bitmap. */
2845 BUF_PUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
2846
2847 /* Clear the whole map. */
2848 bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH);
2849
2850 /* charset_not matches newline according to a syntax bit. */
2851 if ((re_opcode_t) b[-2] == charset_not
2852 && (syntax & RE_HAT_LISTS_NOT_NEWLINE))
2853 SET_LIST_BIT ('\n');
2854
2855 /* Read in characters and ranges, setting map bits. */
2856 for (;;)
2857 {
2858 boolean escaped_char = false;
2859 const unsigned char *p2 = p;
2860
2861 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2862
2863 /* Don't translate yet. The range TRANSLATE(X..Y) cannot
2864 always be determined from TRANSLATE(X) and TRANSLATE(Y)
2865 So the translation is done later in a loop. Example:
2866 (let ((case-fold-search t)) (string-match "[A-_]" "A")) */
2867 PATFETCH (c);
2868
2869 /* \ might escape characters inside [...] and [^...]. */
2870 if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) && c == '\\')
2871 {
2872 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
2873
2874 PATFETCH (c);
2875 escaped_char = true;
2876 }
2877 else
2878 {
2879 /* Could be the end of the bracket expression. If it's
2880 not (i.e., when the bracket expression is `[]' so
2881 far), the ']' character bit gets set way below. */
2882 if (c == ']' && p2 != p1)
2883 break;
2884 }
2885
2886 /* See if we're at the beginning of a possible character
2887 class. */
2888
2889 if (!escaped_char &&
2890 syntax & RE_CHAR_CLASSES && c == '[' && *p == ':')
2891 {
2892 /* Leave room for the null. */
2893 unsigned char str[CHAR_CLASS_MAX_LENGTH + 1];
2894 const unsigned char *class_beg;
2895
2896 PATFETCH (c);
2897 c1 = 0;
2898 class_beg = p;
2899
2900 /* If pattern is `[[:'. */
2901 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2902
2903 for (;;)
2904 {
2905 PATFETCH (c);
2906 if ((c == ':' && *p == ']') || p == pend)
2907 break;
2908 if (c1 < CHAR_CLASS_MAX_LENGTH)
2909 str[c1++] = c;
2910 else
2911 /* This is in any case an invalid class name. */
2912 str[0] = '\0';
2913 }
2914 str[c1] = '\0';
2915
2916 /* If isn't a word bracketed by `[:' and `:]':
2917 undo the ending character, the letters, and
2918 leave the leading `:' and `[' (but set bits for
2919 them). */
2920 if (c == ':' && *p == ']')
2921 {
2922 re_wchar_t ch;
2923 re_wctype_t cc;
2924 int limit;
2925
2926 cc = re_wctype (str);
2927
2928 if (cc == 0)
2929 FREE_STACK_RETURN (REG_ECTYPE);
2930
2931 /* Throw away the ] at the end of the character
2932 class. */
2933 PATFETCH (c);
2934
2935 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2936
2937 /* Most character classes in a multibyte match
2938 just set a flag. Exceptions are is_blank,
2939 is_digit, is_cntrl, and is_xdigit, since
2940 they can only match ASCII characters. We
2941 don't need to handle them for multibyte.
2942 They are distinguished by a negative wctype. */
2943
2944 for (ch = 0; ch < 128; ++ch)
2945 if (re_iswctype (btowc (ch), cc))
2946 {
2947 c = TRANSLATE (ch);
2948 SET_LIST_BIT (c);
2949 }
2950
2951 if (target_multibyte)
2952 {
2953 SET_RANGE_TABLE_WORK_AREA_BIT
2954 (range_table_work, re_wctype_to_bit (cc));
2955 }
2956 else
2957 {
2958 for (ch = 0; ch < (1 << BYTEWIDTH); ++ch)
2959 {
2960 c = ch;
2961 MAKE_CHAR_MULTIBYTE (c);
2962 if (re_iswctype (btowc (c), cc))
2963 {
2964 c = TRANSLATE (c);
2965 MAKE_CHAR_UNIBYTE (c);
2966 SET_LIST_BIT (c);
2967 }
2968 }
2969 }
2970
2971 /* Repeat the loop. */
2972 continue;
2973 }
2974 else
2975 {
2976 /* Go back to right after the "[:". */
2977 p = class_beg;
2978 SET_LIST_BIT ('[');
2979
2980 /* Because the `:' may starts the range, we
2981 can't simply set bit and repeat the loop.
2982 Instead, just set it to C and handle below. */
2983 c = ':';
2984 }
2985 }
2986
2987 if (p < pend && p[0] == '-' && p[1] != ']')
2988 {
2989
2990 /* Discard the `-'. */
2991 PATFETCH (c1);
2992
2993 /* Fetch the character which ends the range. */
2994 PATFETCH (c1);
2995 if (c > c1)
2996 {
2997 if (syntax & RE_NO_EMPTY_RANGES)
2998 FREE_STACK_RETURN (REG_ERANGEX);
2999 /* Else, repeat the loop. */
3000 }
3001 }
3002 else
3003 /* Range from C to C. */
3004 c1 = c;
3005
3006 #ifndef emacs
3007 c = TRANSLATE (c);
3008 c1 = TRANSLATE (c1);
3009 /* Set the range into bitmap */
3010 for (; c <= c1; c++)
3011 SET_LIST_BIT (TRANSLATE (c));
3012 #else /* not emacs */
3013 if (target_multibyte)
3014 {
3015 if (c1 >= 128)
3016 {
3017 re_wchar_t c0 = MAX (c, 128);
3018
3019 SETUP_MULTIBYTE_RANGE (range_table_work, c0, c1);
3020 c1 = 127;
3021 }
3022 for (; c <= c1; c++)
3023 SET_LIST_BIT (TRANSLATE (c));
3024 }
3025 else
3026 {
3027 re_wchar_t c0;
3028
3029 for (; c <= c1; c++)
3030 {
3031 c0 = c;
3032 if (! multibyte)
3033 MAKE_CHAR_MULTIBYTE (c0);
3034 c0 = TRANSLATE (c0);
3035 MAKE_CHAR_UNIBYTE (c0);
3036 SET_LIST_BIT (c0);
3037 }
3038 }
3039 #endif /* not emacs */
3040 }
3041
3042 /* Discard any (non)matching list bytes that are all 0 at the
3043 end of the map. Decrease the map-length byte too. */
3044 while ((int) b[-1] > 0 && b[b[-1] - 1] == 0)
3045 b[-1]--;
3046 b += b[-1];
3047
3048 /* Build real range table from work area. */
3049 if (RANGE_TABLE_WORK_USED (range_table_work)
3050 || RANGE_TABLE_WORK_BITS (range_table_work))
3051 {
3052 int i;
3053 int used = RANGE_TABLE_WORK_USED (range_table_work);
3054
3055 /* Allocate space for COUNT + RANGE_TABLE. Needs two
3056 bytes for flags, two for COUNT, and three bytes for
3057 each character. */
3058 GET_BUFFER_SPACE (4 + used * 3);
3059
3060 /* Indicate the existence of range table. */
3061 laststart[1] |= 0x80;
3062
3063 /* Store the character class flag bits into the range table.
3064 If not in emacs, these flag bits are always 0. */
3065 *b++ = RANGE_TABLE_WORK_BITS (range_table_work) & 0xff;
3066 *b++ = RANGE_TABLE_WORK_BITS (range_table_work) >> 8;
3067
3068 STORE_NUMBER_AND_INCR (b, used / 2);
3069 for (i = 0; i < used; i++)
3070 STORE_CHARACTER_AND_INCR
3071 (b, RANGE_TABLE_WORK_ELT (range_table_work, i));
3072 }
3073 }
3074 break;
3075
3076
3077 case '(':
3078 if (syntax & RE_NO_BK_PARENS)
3079 goto handle_open;
3080 else
3081 goto normal_char;
3082
3083
3084 case ')':
3085 if (syntax & RE_NO_BK_PARENS)
3086 goto handle_close;
3087 else
3088 goto normal_char;
3089
3090
3091 case '\n':
3092 if (syntax & RE_NEWLINE_ALT)
3093 goto handle_alt;
3094 else
3095 goto normal_char;
3096
3097
3098 case '|':
3099 if (syntax & RE_NO_BK_VBAR)
3100 goto handle_alt;
3101 else
3102 goto normal_char;
3103
3104
3105 case '{':
3106 if (syntax & RE_INTERVALS && syntax & RE_NO_BK_BRACES)
3107 goto handle_interval;
3108 else
3109 goto normal_char;
3110
3111
3112 case '\\':
3113 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
3114
3115 /* Do not translate the character after the \, so that we can
3116 distinguish, e.g., \B from \b, even if we normally would
3117 translate, e.g., B to b. */
3118 PATFETCH (c);
3119
3120 switch (c)
3121 {
3122 case '(':
3123 if (syntax & RE_NO_BK_PARENS)
3124 goto normal_backslash;
3125
3126 handle_open:
3127 {
3128 int shy = 0;
3129 if (p+1 < pend)
3130 {
3131 /* Look for a special (?...) construct */
3132 if ((syntax & RE_SHY_GROUPS) && *p == '?')
3133 {
3134 PATFETCH (c); /* Gobble up the '?'. */
3135 PATFETCH (c);
3136 switch (c)
3137 {
3138 case ':': shy = 1; break;
3139 default:
3140 /* Only (?:...) is supported right now. */
3141 FREE_STACK_RETURN (REG_BADPAT);
3142 }
3143 }
3144 }
3145
3146 if (!shy)
3147 {
3148 bufp->re_nsub++;
3149 regnum++;
3150 }
3151
3152 if (COMPILE_STACK_FULL)
3153 {
3154 RETALLOC (compile_stack.stack, compile_stack.size << 1,
3155 compile_stack_elt_t);
3156 if (compile_stack.stack == NULL) return REG_ESPACE;
3157
3158 compile_stack.size <<= 1;
3159 }
3160
3161 /* These are the values to restore when we hit end of this
3162 group. They are all relative offsets, so that if the
3163 whole pattern moves because of realloc, they will still
3164 be valid. */
3165 COMPILE_STACK_TOP.begalt_offset = begalt - bufp->buffer;
3166 COMPILE_STACK_TOP.fixup_alt_jump
3167 = fixup_alt_jump ? fixup_alt_jump - bufp->buffer + 1 : 0;
3168 COMPILE_STACK_TOP.laststart_offset = b - bufp->buffer;
3169 COMPILE_STACK_TOP.regnum = shy ? -regnum : regnum;
3170
3171 /* Do not push a
3172 start_memory for groups beyond the last one we can
3173 represent in the compiled pattern. */
3174 if (regnum <= MAX_REGNUM && !shy)
3175 BUF_PUSH_2 (start_memory, regnum);
3176
3177 compile_stack.avail++;
3178
3179 fixup_alt_jump = 0;
3180 laststart = 0;
3181 begalt = b;
3182 /* If we've reached MAX_REGNUM groups, then this open
3183 won't actually generate any code, so we'll have to
3184 clear pending_exact explicitly. */
3185 pending_exact = 0;
3186 break;
3187 }
3188
3189 case ')':
3190 if (syntax & RE_NO_BK_PARENS) goto normal_backslash;
3191
3192 if (COMPILE_STACK_EMPTY)
3193 {
3194 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
3195 goto normal_backslash;
3196 else
3197 FREE_STACK_RETURN (REG_ERPAREN);
3198 }
3199
3200 handle_close:
3201 FIXUP_ALT_JUMP ();
3202
3203 /* See similar code for backslashed left paren above. */
3204 if (COMPILE_STACK_EMPTY)
3205 {
3206 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
3207 goto normal_char;
3208 else
3209 FREE_STACK_RETURN (REG_ERPAREN);
3210 }
3211
3212 /* Since we just checked for an empty stack above, this
3213 ``can't happen''. */
3214 assert (compile_stack.avail != 0);
3215 {
3216 /* We don't just want to restore into `regnum', because
3217 later groups should continue to be numbered higher,
3218 as in `(ab)c(de)' -- the second group is #2. */
3219 regnum_t this_group_regnum;
3220
3221 compile_stack.avail--;
3222 begalt = bufp->buffer + COMPILE_STACK_TOP.begalt_offset;
3223 fixup_alt_jump
3224 = COMPILE_STACK_TOP.fixup_alt_jump
3225 ? bufp->buffer + COMPILE_STACK_TOP.fixup_alt_jump - 1
3226 : 0;
3227 laststart = bufp->buffer + COMPILE_STACK_TOP.laststart_offset;
3228 this_group_regnum = COMPILE_STACK_TOP.regnum;
3229 /* If we've reached MAX_REGNUM groups, then this open
3230 won't actually generate any code, so we'll have to
3231 clear pending_exact explicitly. */
3232 pending_exact = 0;
3233
3234 /* We're at the end of the group, so now we know how many
3235 groups were inside this one. */
3236 if (this_group_regnum <= MAX_REGNUM && this_group_regnum > 0)
3237 BUF_PUSH_2 (stop_memory, this_group_regnum);
3238 }
3239 break;
3240
3241
3242 case '|': /* `\|'. */
3243 if (syntax & RE_LIMITED_OPS || syntax & RE_NO_BK_VBAR)
3244 goto normal_backslash;
3245 handle_alt:
3246 if (syntax & RE_LIMITED_OPS)
3247 goto normal_char;
3248
3249 /* Insert before the previous alternative a jump which
3250 jumps to this alternative if the former fails. */
3251 GET_BUFFER_SPACE (3);
3252 INSERT_JUMP (on_failure_jump, begalt, b + 6);
3253 pending_exact = 0;
3254 b += 3;
3255
3256 /* The alternative before this one has a jump after it
3257 which gets executed if it gets matched. Adjust that
3258 jump so it will jump to this alternative's analogous
3259 jump (put in below, which in turn will jump to the next
3260 (if any) alternative's such jump, etc.). The last such
3261 jump jumps to the correct final destination. A picture:
3262 _____ _____
3263 | | | |
3264 | v | v
3265 a | b | c
3266
3267 If we are at `b', then fixup_alt_jump right now points to a
3268 three-byte space after `a'. We'll put in the jump, set
3269 fixup_alt_jump to right after `b', and leave behind three
3270 bytes which we'll fill in when we get to after `c'. */
3271
3272 FIXUP_ALT_JUMP ();
3273
3274 /* Mark and leave space for a jump after this alternative,
3275 to be filled in later either by next alternative or
3276 when know we're at the end of a series of alternatives. */
3277 fixup_alt_jump = b;
3278 GET_BUFFER_SPACE (3);
3279 b += 3;
3280
3281 laststart = 0;
3282 begalt = b;
3283 break;
3284
3285
3286 case '{':
3287 /* If \{ is a literal. */
3288 if (!(syntax & RE_INTERVALS)
3289 /* If we're at `\{' and it's not the open-interval
3290 operator. */
3291 || (syntax & RE_NO_BK_BRACES))
3292 goto normal_backslash;
3293
3294 handle_interval:
3295 {
3296 /* If got here, then the syntax allows intervals. */
3297
3298 /* At least (most) this many matches must be made. */
3299 int lower_bound = 0, upper_bound = -1;
3300
3301 beg_interval = p;
3302
3303 GET_UNSIGNED_NUMBER (lower_bound);
3304
3305 if (c == ',')
3306 GET_UNSIGNED_NUMBER (upper_bound);
3307 else
3308 /* Interval such as `{1}' => match exactly once. */
3309 upper_bound = lower_bound;
3310
3311 if (lower_bound < 0 || upper_bound > RE_DUP_MAX
3312 || (upper_bound >= 0 && lower_bound > upper_bound))
3313 FREE_STACK_RETURN (REG_BADBR);
3314
3315 if (!(syntax & RE_NO_BK_BRACES))
3316 {
3317 if (c != '\\')
3318 FREE_STACK_RETURN (REG_BADBR);
3319 if (p == pend)
3320 FREE_STACK_RETURN (REG_EESCAPE);
3321 PATFETCH (c);
3322 }
3323
3324 if (c != '}')
3325 FREE_STACK_RETURN (REG_BADBR);
3326
3327 /* We just parsed a valid interval. */
3328
3329 /* If it's invalid to have no preceding re. */
3330 if (!laststart)
3331 {
3332 if (syntax & RE_CONTEXT_INVALID_OPS)
3333 FREE_STACK_RETURN (REG_BADRPT);
3334 else if (syntax & RE_CONTEXT_INDEP_OPS)
3335 laststart = b;
3336 else
3337 goto unfetch_interval;
3338 }
3339
3340 if (upper_bound == 0)
3341 /* If the upper bound is zero, just drop the sub pattern
3342 altogether. */
3343 b = laststart;
3344 else if (lower_bound == 1 && upper_bound == 1)
3345 /* Just match it once: nothing to do here. */
3346 ;
3347
3348 /* Otherwise, we have a nontrivial interval. When
3349 we're all done, the pattern will look like:
3350 set_number_at <jump count> <upper bound>
3351 set_number_at <succeed_n count> <lower bound>
3352 succeed_n <after jump addr> <succeed_n count>
3353 <body of loop>
3354 jump_n <succeed_n addr> <jump count>
3355 (The upper bound and `jump_n' are omitted if
3356 `upper_bound' is 1, though.) */
3357 else
3358 { /* If the upper bound is > 1, we need to insert
3359 more at the end of the loop. */
3360 unsigned int nbytes = (upper_bound < 0 ? 3
3361 : upper_bound > 1 ? 5 : 0);
3362 unsigned int startoffset = 0;
3363
3364 GET_BUFFER_SPACE (20); /* We might use less. */
3365
3366 if (lower_bound == 0)
3367 {
3368 /* A succeed_n that starts with 0 is really a
3369 a simple on_failure_jump_loop. */
3370 INSERT_JUMP (on_failure_jump_loop, laststart,
3371 b + 3 + nbytes);
3372 b += 3;
3373 }
3374 else
3375 {
3376 /* Initialize lower bound of the `succeed_n', even
3377 though it will be set during matching by its
3378 attendant `set_number_at' (inserted next),
3379 because `re_compile_fastmap' needs to know.
3380 Jump to the `jump_n' we might insert below. */
3381 INSERT_JUMP2 (succeed_n, laststart,
3382 b + 5 + nbytes,
3383 lower_bound);
3384 b += 5;
3385
3386 /* Code to initialize the lower bound. Insert
3387 before the `succeed_n'. The `5' is the last two
3388 bytes of this `set_number_at', plus 3 bytes of
3389 the following `succeed_n'. */
3390 insert_op2 (set_number_at, laststart, 5, lower_bound, b);
3391 b += 5;
3392 startoffset += 5;
3393 }
3394
3395 if (upper_bound < 0)
3396 {
3397 /* A negative upper bound stands for infinity,
3398 in which case it degenerates to a plain jump. */
3399 STORE_JUMP (jump, b, laststart + startoffset);
3400 b += 3;
3401 }
3402 else if (upper_bound > 1)
3403 { /* More than one repetition is allowed, so
3404 append a backward jump to the `succeed_n'
3405 that starts this interval.
3406
3407 When we've reached this during matching,
3408 we'll have matched the interval once, so
3409 jump back only `upper_bound - 1' times. */
3410 STORE_JUMP2 (jump_n, b, laststart + startoffset,
3411 upper_bound - 1);
3412 b += 5;
3413
3414 /* The location we want to set is the second
3415 parameter of the `jump_n'; that is `b-2' as
3416 an absolute address. `laststart' will be
3417 the `set_number_at' we're about to insert;
3418 `laststart+3' the number to set, the source
3419 for the relative address. But we are
3420 inserting into the middle of the pattern --
3421 so everything is getting moved up by 5.
3422 Conclusion: (b - 2) - (laststart + 3) + 5,
3423 i.e., b - laststart.
3424
3425 We insert this at the beginning of the loop
3426 so that if we fail during matching, we'll
3427 reinitialize the bounds. */
3428 insert_op2 (set_number_at, laststart, b - laststart,
3429 upper_bound - 1, b);
3430 b += 5;
3431 }
3432 }
3433 pending_exact = 0;
3434 beg_interval = NULL;
3435 }
3436 break;
3437
3438 unfetch_interval:
3439 /* If an invalid interval, match the characters as literals. */
3440 assert (beg_interval);
3441 p = beg_interval;
3442 beg_interval = NULL;
3443
3444 /* normal_char and normal_backslash need `c'. */
3445 c = '{';
3446
3447 if (!(syntax & RE_NO_BK_BRACES))
3448 {
3449 assert (p > pattern && p[-1] == '\\');
3450 goto normal_backslash;
3451 }
3452 else
3453 goto normal_char;
3454
3455 #ifdef emacs
3456 /* There is no way to specify the before_dot and after_dot
3457 operators. rms says this is ok. --karl */
3458 case '=':
3459 BUF_PUSH (at_dot);
3460 break;
3461
3462 case 's':
3463 laststart = b;
3464 PATFETCH (c);
3465 BUF_PUSH_2 (syntaxspec, syntax_spec_code[c]);
3466 break;
3467
3468 case 'S':
3469 laststart = b;
3470 PATFETCH (c);
3471 BUF_PUSH_2 (notsyntaxspec, syntax_spec_code[c]);
3472 break;
3473
3474 case 'c':
3475 laststart = b;
3476 PATFETCH (c);
3477 BUF_PUSH_2 (categoryspec, c);
3478 break;
3479
3480 case 'C':
3481 laststart = b;
3482 PATFETCH (c);
3483 BUF_PUSH_2 (notcategoryspec, c);
3484 break;
3485 #endif /* emacs */
3486
3487
3488 case 'w':
3489 if (syntax & RE_NO_GNU_OPS)
3490 goto normal_char;
3491 laststart = b;
3492 BUF_PUSH_2 (syntaxspec, Sword);
3493 break;
3494
3495
3496 case 'W':
3497 if (syntax & RE_NO_GNU_OPS)
3498 goto normal_char;
3499 laststart = b;
3500 BUF_PUSH_2 (notsyntaxspec, Sword);
3501 break;
3502
3503
3504 case '<':
3505 if (syntax & RE_NO_GNU_OPS)
3506 goto normal_char;
3507 BUF_PUSH (wordbeg);
3508 break;
3509
3510 case '>':
3511 if (syntax & RE_NO_GNU_OPS)
3512 goto normal_char;
3513 BUF_PUSH (wordend);
3514 break;
3515
3516 case '_':
3517 if (syntax & RE_NO_GNU_OPS)
3518 goto normal_char;
3519 laststart = b;
3520 PATFETCH (c);
3521 if (c == '<')
3522 BUF_PUSH (symbeg);
3523 else if (c == '>')
3524 BUF_PUSH (symend);
3525 else
3526 FREE_STACK_RETURN (REG_BADPAT);
3527 break;
3528
3529 case 'b':
3530 if (syntax & RE_NO_GNU_OPS)
3531 goto normal_char;
3532 BUF_PUSH (wordbound);
3533 break;
3534
3535 case 'B':
3536 if (syntax & RE_NO_GNU_OPS)
3537 goto normal_char;
3538 BUF_PUSH (notwordbound);
3539 break;
3540
3541 case '`':
3542 if (syntax & RE_NO_GNU_OPS)
3543 goto normal_char;
3544 BUF_PUSH (begbuf);
3545 break;
3546
3547 case '\'':
3548 if (syntax & RE_NO_GNU_OPS)
3549 goto normal_char;
3550 BUF_PUSH (endbuf);
3551 break;
3552
3553 case '1': case '2': case '3': case '4': case '5':
3554 case '6': case '7': case '8': case '9':
3555 {
3556 regnum_t reg;
3557
3558 if (syntax & RE_NO_BK_REFS)
3559 goto normal_backslash;
3560
3561 reg = c - '0';
3562
3563 /* Can't back reference to a subexpression before its end. */
3564 if (reg > regnum || group_in_compile_stack (compile_stack, reg))
3565 FREE_STACK_RETURN (REG_ESUBREG);
3566
3567 laststart = b;
3568 BUF_PUSH_2 (duplicate, reg);
3569 }
3570 break;
3571
3572
3573 case '+':
3574 case '?':
3575 if (syntax & RE_BK_PLUS_QM)
3576 goto handle_plus;
3577 else
3578 goto normal_backslash;
3579
3580 default:
3581 normal_backslash:
3582 /* You might think it would be useful for \ to mean
3583 not to translate; but if we don't translate it
3584 it will never match anything. */
3585 goto normal_char;
3586 }
3587 break;
3588
3589
3590 default:
3591 /* Expects the character in `c'. */
3592 normal_char:
3593 /* If no exactn currently being built. */
3594 if (!pending_exact
3595
3596 /* If last exactn not at current position. */
3597 || pending_exact + *pending_exact + 1 != b
3598
3599 /* We have only one byte following the exactn for the count. */
3600 || *pending_exact >= (1 << BYTEWIDTH) - MAX_MULTIBYTE_LENGTH
3601
3602 /* If followed by a repetition operator. */
3603 || (p != pend && (*p == '*' || *p == '^'))
3604 || ((syntax & RE_BK_PLUS_QM)
3605 ? p + 1 < pend && *p == '\\' && (p[1] == '+' || p[1] == '?')
3606 : p != pend && (*p == '+' || *p == '?'))
3607 || ((syntax & RE_INTERVALS)
3608 && ((syntax & RE_NO_BK_BRACES)
3609 ? p != pend && *p == '{'
3610 : p + 1 < pend && p[0] == '\\' && p[1] == '{')))
3611 {
3612 /* Start building a new exactn. */
3613
3614 laststart = b;
3615
3616 BUF_PUSH_2 (exactn, 0);
3617 pending_exact = b - 1;
3618 }
3619
3620 GET_BUFFER_SPACE (MAX_MULTIBYTE_LENGTH);
3621 {
3622 int len;
3623
3624 if (! multibyte)
3625 MAKE_CHAR_MULTIBYTE (c);
3626 c = TRANSLATE (c);
3627 if (target_multibyte)
3628 {
3629 len = CHAR_STRING (c, b);
3630 b += len;
3631 }
3632 else
3633 {
3634 MAKE_CHAR_UNIBYTE (c);
3635 *b++ = c;
3636 len = 1;
3637 }
3638 (*pending_exact) += len;
3639 }
3640
3641 break;
3642 } /* switch (c) */
3643 } /* while p != pend */
3644
3645
3646 /* Through the pattern now. */
3647
3648 FIXUP_ALT_JUMP ();
3649
3650 if (!COMPILE_STACK_EMPTY)
3651 FREE_STACK_RETURN (REG_EPAREN);
3652
3653 /* If we don't want backtracking, force success
3654 the first time we reach the end of the compiled pattern. */
3655 if (syntax & RE_NO_POSIX_BACKTRACKING)
3656 BUF_PUSH (succeed);
3657
3658 /* We have succeeded; set the length of the buffer. */
3659 bufp->used = b - bufp->buffer;
3660
3661 #ifdef emacs
3662 /* Now the buffer is adjusted for the multibyteness of a target. */
3663 bufp->multibyte = bufp->target_multibyte;
3664 #endif
3665
3666 #ifdef DEBUG
3667 if (debug > 0)
3668 {
3669 re_compile_fastmap (bufp);
3670 DEBUG_PRINT1 ("\nCompiled pattern: \n");
3671 print_compiled_pattern (bufp);
3672 }
3673 debug--;
3674 #endif /* DEBUG */
3675
3676 #ifndef MATCH_MAY_ALLOCATE
3677 /* Initialize the failure stack to the largest possible stack. This
3678 isn't necessary unless we're trying to avoid calling alloca in
3679 the search and match routines. */
3680 {
3681 int num_regs = bufp->re_nsub + 1;
3682
3683 if (fail_stack.size < re_max_failures * TYPICAL_FAILURE_SIZE)
3684 {
3685 fail_stack.size = re_max_failures * TYPICAL_FAILURE_SIZE;
3686
3687 if (! fail_stack.stack)
3688 fail_stack.stack
3689 = (fail_stack_elt_t *) malloc (fail_stack.size
3690 * sizeof (fail_stack_elt_t));
3691 else
3692 fail_stack.stack
3693 = (fail_stack_elt_t *) realloc (fail_stack.stack,
3694 (fail_stack.size
3695 * sizeof (fail_stack_elt_t)));
3696 }
3697
3698 regex_grow_registers (num_regs);
3699 }
3700 #endif /* not MATCH_MAY_ALLOCATE */
3701
3702 FREE_STACK_RETURN (REG_NOERROR);
3703 } /* regex_compile */
3704 \f
3705 /* Subroutines for `regex_compile'. */
3706
3707 /* Store OP at LOC followed by two-byte integer parameter ARG. */
3708
3709 static void
3710 store_op1 (op, loc, arg)
3711 re_opcode_t op;
3712 unsigned char *loc;
3713 int arg;
3714 {
3715 *loc = (unsigned char) op;
3716 STORE_NUMBER (loc + 1, arg);
3717 }
3718
3719
3720 /* Like `store_op1', but for two two-byte parameters ARG1 and ARG2. */
3721
3722 static void
3723 store_op2 (op, loc, arg1, arg2)
3724 re_opcode_t op;
3725 unsigned char *loc;
3726 int arg1, arg2;
3727 {
3728 *loc = (unsigned char) op;
3729 STORE_NUMBER (loc + 1, arg1);
3730 STORE_NUMBER (loc + 3, arg2);
3731 }
3732
3733
3734 /* Copy the bytes from LOC to END to open up three bytes of space at LOC
3735 for OP followed by two-byte integer parameter ARG. */
3736
3737 static void
3738 insert_op1 (op, loc, arg, end)
3739 re_opcode_t op;
3740 unsigned char *loc;
3741 int arg;
3742 unsigned char *end;
3743 {
3744 register unsigned char *pfrom = end;
3745 register unsigned char *pto = end + 3;
3746
3747 while (pfrom != loc)
3748 *--pto = *--pfrom;
3749
3750 store_op1 (op, loc, arg);
3751 }
3752
3753
3754 /* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2. */
3755
3756 static void
3757 insert_op2 (op, loc, arg1, arg2, end)
3758 re_opcode_t op;
3759 unsigned char *loc;
3760 int arg1, arg2;
3761 unsigned char *end;
3762 {
3763 register unsigned char *pfrom = end;
3764 register unsigned char *pto = end + 5;
3765
3766 while (pfrom != loc)
3767 *--pto = *--pfrom;
3768
3769 store_op2 (op, loc, arg1, arg2);
3770 }
3771
3772
3773 /* P points to just after a ^ in PATTERN. Return true if that ^ comes
3774 after an alternative or a begin-subexpression. We assume there is at
3775 least one character before the ^. */
3776
3777 static boolean
3778 at_begline_loc_p (pattern, p, syntax)
3779 re_char *pattern, *p;
3780 reg_syntax_t syntax;
3781 {
3782 re_char *prev = p - 2;
3783 boolean prev_prev_backslash = prev > pattern && prev[-1] == '\\';
3784
3785 return
3786 /* After a subexpression? */
3787 (*prev == '(' && (syntax & RE_NO_BK_PARENS || prev_prev_backslash))
3788 /* After an alternative? */
3789 || (*prev == '|' && (syntax & RE_NO_BK_VBAR || prev_prev_backslash))
3790 /* After a shy subexpression? */
3791 || ((syntax & RE_SHY_GROUPS) && prev - 2 >= pattern
3792 && prev[-1] == '?' && prev[-2] == '('
3793 && (syntax & RE_NO_BK_PARENS
3794 || (prev - 3 >= pattern && prev[-3] == '\\')));
3795 }
3796
3797
3798 /* The dual of at_begline_loc_p. This one is for $. We assume there is
3799 at least one character after the $, i.e., `P < PEND'. */
3800
3801 static boolean
3802 at_endline_loc_p (p, pend, syntax)
3803 re_char *p, *pend;
3804 reg_syntax_t syntax;
3805 {
3806 re_char *next = p;
3807 boolean next_backslash = *next == '\\';
3808 re_char *next_next = p + 1 < pend ? p + 1 : 0;
3809
3810 return
3811 /* Before a subexpression? */
3812 (syntax & RE_NO_BK_PARENS ? *next == ')'
3813 : next_backslash && next_next && *next_next == ')')
3814 /* Before an alternative? */
3815 || (syntax & RE_NO_BK_VBAR ? *next == '|'
3816 : next_backslash && next_next && *next_next == '|');
3817 }
3818
3819
3820 /* Returns true if REGNUM is in one of COMPILE_STACK's elements and
3821 false if it's not. */
3822
3823 static boolean
3824 group_in_compile_stack (compile_stack, regnum)
3825 compile_stack_type compile_stack;
3826 regnum_t regnum;
3827 {
3828 int this_element;
3829
3830 for (this_element = compile_stack.avail - 1;
3831 this_element >= 0;
3832 this_element--)
3833 if (compile_stack.stack[this_element].regnum == regnum)
3834 return true;
3835
3836 return false;
3837 }
3838 \f
3839 /* analyse_first.
3840 If fastmap is non-NULL, go through the pattern and fill fastmap
3841 with all the possible leading chars. If fastmap is NULL, don't
3842 bother filling it up (obviously) and only return whether the
3843 pattern could potentially match the empty string.
3844
3845 Return 1 if p..pend might match the empty string.
3846 Return 0 if p..pend matches at least one char.
3847 Return -1 if fastmap was not updated accurately. */
3848
3849 static int
3850 analyse_first (p, pend, fastmap, multibyte)
3851 re_char *p, *pend;
3852 char *fastmap;
3853 const int multibyte;
3854 {
3855 int j, k;
3856 boolean not;
3857
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;
3861
3862 assert (p);
3863
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'. */
3878
3879 while (p < pend)
3880 {
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;
3890
3891 switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++))
3892 {
3893 case succeed:
3894 return 1;
3895 continue;
3896
3897 case duplicate:
3898 /* If the first character has to match a backreference, that means
3899 that the group was empty (since it already matched). Since this
3900 is the only case that interests us here, we can assume that the
3901 backreference must match the empty string. */
3902 p++;
3903 continue;
3904
3905
3906 /* Following are the cases which match a character. These end
3907 with `break'. */
3908
3909 case exactn:
3910 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 break;
3917
3918
3919 case anychar:
3920 /* We could put all the chars except for \n (and maybe \0)
3921 but we don't bother since it is generally not worth it. */
3922 if (!fastmap) break;
3923 return -1;
3924
3925
3926 case charset_not:
3927 if (!fastmap) break;
3928 {
3929 /* Chars beyond end of bitmap are possible matches. */
3930 /* In a multibyte case, the bitmap is used only for ASCII
3931 characters. */
3932 int limit = multibyte ? 128 : (1 << BYTEWIDTH);
3933
3934 for (j = CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH;
3935 j < limit; j++)
3936 fastmap[j] = 1;
3937 }
3938
3939 /* Fallthrough */
3940 case charset:
3941 if (!fastmap) break;
3942 not = (re_opcode_t) *(p - 1) == charset_not;
3943 for (j = CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH - 1, p++;
3944 j >= 0; j--)
3945 if (!!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))) ^ not)
3946 fastmap[j] = 1;
3947
3948 if ((not && multibyte)
3949 /* Any leading code can possibly start a character
3950 which doesn't match the specified set of characters. */
3951 || (CHARSET_RANGE_TABLE_EXISTS_P (&p[-2])
3952 && CHARSET_RANGE_TABLE_BITS (&p[-2]) != 0))
3953 /* If we can match a character class, we can match
3954 any multibyte characters. */
3955 {
3956 if (match_any_multibyte_characters == false)
3957 {
3958 for (j = 0x80; j < (1 << BYTEWIDTH); j++)
3959 fastmap[j] = 1;
3960 match_any_multibyte_characters = true;
3961 }
3962 }
3963
3964 else if (!not && CHARSET_RANGE_TABLE_EXISTS_P (&p[-2])
3965 && match_any_multibyte_characters == false)
3966 {
3967 /* Set fastmap[I] to 1 where I is a leading code of each
3968 multibyte characer in the range table. */
3969 int c, count;
3970 unsigned char lc1, lc2;
3971
3972 /* Make P points the range table. `+ 2' is to skip flag
3973 bits for a character class. */
3974 p += CHARSET_BITMAP_SIZE (&p[-2]) + 2;
3975
3976 /* Extract the number of ranges in range table into COUNT. */
3977 EXTRACT_NUMBER_AND_INCR (count, p);
3978 for (; count > 0; count--, p += 2 * 3) /* XXX */
3979 {
3980 /* Extract the start and end of each range. */
3981 EXTRACT_CHARACTER (c, p);
3982 lc1 = CHAR_LEADING_CODE (c);
3983 p += 3;
3984 EXTRACT_CHARACTER (c, p);
3985 lc2 = CHAR_LEADING_CODE (c);
3986 for (j = lc1; j <= lc2; j++)
3987 fastmap[j] = 1;
3988 }
3989 }
3990 break;
3991
3992 case syntaxspec:
3993 case notsyntaxspec:
3994 if (!fastmap) break;
3995 #ifndef emacs
3996 not = (re_opcode_t)p[-1] == notsyntaxspec;
3997 k = *p++;
3998 for (j = 0; j < (1 << BYTEWIDTH); j++)
3999 if ((SYNTAX (j) == (enum syntaxcode) k) ^ not)
4000 fastmap[j] = 1;
4001 break;
4002 #else /* emacs */
4003 /* This match depends on text properties. These end with
4004 aborting optimizations. */
4005 return -1;
4006
4007 case categoryspec:
4008 case notcategoryspec:
4009 if (!fastmap) break;
4010 not = (re_opcode_t)p[-1] == notcategoryspec;
4011 k = *p++;
4012 for (j = (multibyte ? 127 : (1 << BYTEWIDTH)); j >= 0; j--)
4013 if ((CHAR_HAS_CATEGORY (j, k)) ^ not)
4014 fastmap[j] = 1;
4015
4016 if (multibyte)
4017 {
4018 /* Any character set can possibly contain a character
4019 whose category is K (or not). */
4020 if (match_any_multibyte_characters == false)
4021 {
4022 for (j = 0x80; j < (1 << BYTEWIDTH); j++)
4023 fastmap[j] = 1;
4024 match_any_multibyte_characters = true;
4025 }
4026 }
4027 break;
4028
4029 /* All cases after this match the empty string. These end with
4030 `continue'. */
4031
4032 case before_dot:
4033 case at_dot:
4034 case after_dot:
4035 #endif /* !emacs */
4036 case no_op:
4037 case begline:
4038 case endline:
4039 case begbuf:
4040 case endbuf:
4041 case wordbound:
4042 case notwordbound:
4043 case wordbeg:
4044 case wordend:
4045 case symbeg:
4046 case symend:
4047 continue;
4048
4049
4050 case jump:
4051 EXTRACT_NUMBER_AND_INCR (j, p);
4052 if (j < 0)
4053 /* Backward jumps can only go back to code that we've already
4054 visited. `re_compile' should make sure this is true. */
4055 break;
4056 p += j;
4057 switch (SWITCH_ENUM_CAST ((re_opcode_t) *p))
4058 {
4059 case on_failure_jump:
4060 case on_failure_keep_string_jump:
4061 case on_failure_jump_loop:
4062 case on_failure_jump_nastyloop:
4063 case on_failure_jump_smart:
4064 p++;
4065 break;
4066 default:
4067 continue;
4068 };
4069 /* Keep `p1' to allow the `on_failure_jump' we are jumping to
4070 to jump back to "just after here". */
4071 /* Fallthrough */
4072
4073 case on_failure_jump:
4074 case on_failure_keep_string_jump:
4075 case on_failure_jump_nastyloop:
4076 case on_failure_jump_loop:
4077 case on_failure_jump_smart:
4078 EXTRACT_NUMBER_AND_INCR (j, p);
4079 if (p + j <= p1)
4080 ; /* Backward jump to be ignored. */
4081 else
4082 { /* We have to look down both arms.
4083 We first go down the "straight" path so as to minimize
4084 stack usage when going through alternatives. */
4085 int r = analyse_first (p, pend, fastmap, multibyte);
4086 if (r) return r;
4087 p += j;
4088 }
4089 continue;
4090
4091
4092 case jump_n:
4093 /* This code simply does not properly handle forward jump_n. */
4094 DEBUG_STATEMENT (EXTRACT_NUMBER (j, p); assert (j < 0));
4095 p += 4;
4096 /* jump_n can either jump or fall through. The (backward) jump
4097 case has already been handled, so we only need to look at the
4098 fallthrough case. */
4099 continue;
4100
4101 case succeed_n:
4102 /* If N == 0, it should be an on_failure_jump_loop instead. */
4103 DEBUG_STATEMENT (EXTRACT_NUMBER (j, p + 2); assert (j > 0));
4104 p += 4;
4105 /* We only care about one iteration of the loop, so we don't
4106 need to consider the case where this behaves like an
4107 on_failure_jump. */
4108 continue;
4109
4110
4111 case set_number_at:
4112 p += 4;
4113 continue;
4114
4115
4116 case start_memory:
4117 case stop_memory:
4118 p += 1;
4119 continue;
4120
4121
4122 default:
4123 abort (); /* We have listed all the cases. */
4124 } /* switch *p++ */
4125
4126 /* Getting here means we have found the possible starting
4127 characters for one path of the pattern -- and that the empty
4128 string does not match. We need not follow this path further. */
4129 return 0;
4130 } /* while p */
4131
4132 /* We reached the end without matching anything. */
4133 return 1;
4134
4135 } /* analyse_first */
4136 \f
4137 /* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
4138 BUFP. A fastmap records which of the (1 << BYTEWIDTH) possible
4139 characters can start a string that matches the pattern. This fastmap
4140 is used by re_search to skip quickly over impossible starting points.
4141
4142 Character codes above (1 << BYTEWIDTH) are not represented in the
4143 fastmap, but the leading codes are represented. Thus, the fastmap
4144 indicates which character sets could start a match.
4145
4146 The caller must supply the address of a (1 << BYTEWIDTH)-byte data
4147 area as BUFP->fastmap.
4148
4149 We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in
4150 the pattern buffer.
4151
4152 Returns 0 if we succeed, -2 if an internal error. */
4153
4154 int
4155 re_compile_fastmap (bufp)
4156 struct re_pattern_buffer *bufp;
4157 {
4158 char *fastmap = bufp->fastmap;
4159 int analysis;
4160
4161 assert (fastmap && bufp->buffer);
4162
4163 bzero (fastmap, 1 << BYTEWIDTH); /* Assume nothing's valid. */
4164 bufp->fastmap_accurate = 1; /* It will be when we're done. */
4165
4166 analysis = analyse_first (bufp->buffer, bufp->buffer + bufp->used,
4167 fastmap, RE_MULTIBYTE_P (bufp));
4168 bufp->can_be_null = (analysis != 0);
4169 return 0;
4170 } /* re_compile_fastmap */
4171 \f
4172 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
4173 ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
4174 this memory for recording register information. STARTS and ENDS
4175 must be allocated using the malloc library routine, and must each
4176 be at least NUM_REGS * sizeof (regoff_t) bytes long.
4177
4178 If NUM_REGS == 0, then subsequent matches should allocate their own
4179 register data.
4180
4181 Unless this function is called, the first search or match using
4182 PATTERN_BUFFER will allocate its own register data, without
4183 freeing the old data. */
4184
4185 void
4186 re_set_registers (bufp, regs, num_regs, starts, ends)
4187 struct re_pattern_buffer *bufp;
4188 struct re_registers *regs;
4189 unsigned num_regs;
4190 regoff_t *starts, *ends;
4191 {
4192 if (num_regs)
4193 {
4194 bufp->regs_allocated = REGS_REALLOCATE;
4195 regs->num_regs = num_regs;
4196 regs->start = starts;
4197 regs->end = ends;
4198 }
4199 else
4200 {
4201 bufp->regs_allocated = REGS_UNALLOCATED;
4202 regs->num_regs = 0;
4203 regs->start = regs->end = (regoff_t *) 0;
4204 }
4205 }
4206 WEAK_ALIAS (__re_set_registers, re_set_registers)
4207 \f
4208 /* Searching routines. */
4209
4210 /* Like re_search_2, below, but only one string is specified, and
4211 doesn't let you say where to stop matching. */
4212
4213 int
4214 re_search (bufp, string, size, startpos, range, regs)
4215 struct re_pattern_buffer *bufp;
4216 const char *string;
4217 int size, startpos, range;
4218 struct re_registers *regs;
4219 {
4220 return re_search_2 (bufp, NULL, 0, string, size, startpos, range,
4221 regs, size);
4222 }
4223 WEAK_ALIAS (__re_search, re_search)
4224
4225 /* Head address of virtual concatenation of string. */
4226 #define HEAD_ADDR_VSTRING(P) \
4227 (((P) >= size1 ? string2 : string1))
4228
4229 /* End address of virtual concatenation of string. */
4230 #define STOP_ADDR_VSTRING(P) \
4231 (((P) >= size1 ? string2 + size2 : string1 + size1))
4232
4233 /* Address of POS in the concatenation of virtual string. */
4234 #define POS_ADDR_VSTRING(POS) \
4235 (((POS) >= size1 ? string2 - size1 : string1) + (POS))
4236
4237 /* Using the compiled pattern in BUFP->buffer, first tries to match the
4238 virtual concatenation of STRING1 and STRING2, starting first at index
4239 STARTPOS, then at STARTPOS + 1, and so on.
4240
4241 STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
4242
4243 RANGE is how far to scan while trying to match. RANGE = 0 means try
4244 only at STARTPOS; in general, the last start tried is STARTPOS +
4245 RANGE.
4246
4247 In REGS, return the indices of the virtual concatenation of STRING1
4248 and STRING2 that matched the entire BUFP->buffer and its contained
4249 subexpressions.
4250
4251 Do not consider matching one past the index STOP in the virtual
4252 concatenation of STRING1 and STRING2.
4253
4254 We return either the position in the strings at which the match was
4255 found, -1 if no match, or -2 if error (such as failure
4256 stack overflow). */
4257
4258 int
4259 re_search_2 (bufp, str1, size1, str2, size2, startpos, range, regs, stop)
4260 struct re_pattern_buffer *bufp;
4261 const char *str1, *str2;
4262 int size1, size2;
4263 int startpos;
4264 int range;
4265 struct re_registers *regs;
4266 int stop;
4267 {
4268 int val;
4269 re_char *string1 = (re_char*) str1;
4270 re_char *string2 = (re_char*) str2;
4271 register char *fastmap = bufp->fastmap;
4272 register RE_TRANSLATE_TYPE translate = bufp->translate;
4273 int total_size = size1 + size2;
4274 int endpos = startpos + range;
4275 boolean anchored_start;
4276 /* Nonzero if BUFP is setup for multibyte characters. We are sure
4277 that it is the same as RE_TARGET_MULTIBYTE_P (bufp). */
4278 const boolean multibyte = RE_MULTIBYTE_P (bufp);
4279
4280 /* Check for out-of-range STARTPOS. */
4281 if (startpos < 0 || startpos > total_size)
4282 return -1;
4283
4284 /* Fix up RANGE if it might eventually take us outside
4285 the virtual concatenation of STRING1 and STRING2.
4286 Make sure we won't move STARTPOS below 0 or above TOTAL_SIZE. */
4287 if (endpos < 0)
4288 range = 0 - startpos;
4289 else if (endpos > total_size)
4290 range = total_size - startpos;
4291
4292 /* If the search isn't to be a backwards one, don't waste time in a
4293 search for a pattern anchored at beginning of buffer. */
4294 if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == begbuf && range > 0)
4295 {
4296 if (startpos > 0)
4297 return -1;
4298 else
4299 range = 0;
4300 }
4301
4302 #ifdef emacs
4303 /* In a forward search for something that starts with \=.
4304 don't keep searching past point. */
4305 if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == at_dot && range > 0)
4306 {
4307 range = PT_BYTE - BEGV_BYTE - startpos;
4308 if (range < 0)
4309 return -1;
4310 }
4311 #endif /* emacs */
4312
4313 /* Update the fastmap now if not correct already. */
4314 if (fastmap && !bufp->fastmap_accurate)
4315 re_compile_fastmap (bufp);
4316
4317 /* See whether the pattern is anchored. */
4318 anchored_start = (bufp->buffer[0] == begline);
4319
4320 #ifdef emacs
4321 gl_state.object = re_match_object;
4322 {
4323 int charpos = SYNTAX_TABLE_BYTE_TO_CHAR (POS_AS_IN_BUFFER (startpos));
4324
4325 SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object, charpos, 1);
4326 }
4327 #endif
4328
4329 /* Loop through the string, looking for a place to start matching. */
4330 for (;;)
4331 {
4332 /* If the pattern is anchored,
4333 skip quickly past places we cannot match.
4334 We don't bother to treat startpos == 0 specially
4335 because that case doesn't repeat. */
4336 if (anchored_start && startpos > 0)
4337 {
4338 if (! ((startpos <= size1 ? string1[startpos - 1]
4339 : string2[startpos - size1 - 1])
4340 == '\n'))
4341 goto advance;
4342 }
4343
4344 /* If a fastmap is supplied, skip quickly over characters that
4345 cannot be the start of a match. If the pattern can match the
4346 null string, however, we don't need to skip characters; we want
4347 the first null string. */
4348 if (fastmap && startpos < total_size && !bufp->can_be_null)
4349 {
4350 register re_char *d;
4351 register re_wchar_t buf_ch;
4352
4353 d = POS_ADDR_VSTRING (startpos);
4354
4355 if (range > 0) /* Searching forwards. */
4356 {
4357 register int lim = 0;
4358 int irange = range;
4359
4360 if (startpos < size1 && startpos + range >= size1)
4361 lim = range - (size1 - startpos);
4362
4363 /* Written out as an if-else to avoid testing `translate'
4364 inside the loop. */
4365 if (RE_TRANSLATE_P (translate))
4366 {
4367 if (multibyte)
4368 while (range > lim)
4369 {
4370 int buf_charlen;
4371
4372 buf_ch = STRING_CHAR_AND_LENGTH (d, range - lim,
4373 buf_charlen);
4374 buf_ch = RE_TRANSLATE (translate, buf_ch);
4375 if (fastmap[CHAR_LEADING_CODE (buf_ch)])
4376 break;
4377
4378 range -= buf_charlen;
4379 d += buf_charlen;
4380 }
4381 else
4382 while (range > lim)
4383 {
4384 buf_ch = *d;
4385 MAKE_CHAR_MULTIBYTE (buf_ch);
4386 buf_ch = RE_TRANSLATE (translate, buf_ch);
4387 MAKE_CHAR_UNIBYTE (buf_ch);
4388 if (fastmap[buf_ch])
4389 break;
4390 d++;
4391 range--;
4392 }
4393 }
4394 else
4395 {
4396 if (multibyte)
4397 while (range > lim)
4398 {
4399 int buf_charlen;
4400
4401 buf_ch = STRING_CHAR_AND_LENGTH (d, range - lim,
4402 buf_charlen);
4403 if (fastmap[CHAR_LEADING_CODE (buf_ch)])
4404 break;
4405 range -= buf_charlen;
4406 d += buf_charlen;
4407 }
4408 else
4409 while (range > lim && !fastmap[*d])
4410 {
4411 d++;
4412 range--;
4413 }
4414 }
4415 startpos += irange - range;
4416 }
4417 else /* Searching backwards. */
4418 {
4419 int room = (startpos >= size1
4420 ? size2 + size1 - startpos
4421 : size1 - startpos);
4422 if (multibyte)
4423 {
4424 buf_ch = STRING_CHAR (d, room);
4425 buf_ch = TRANSLATE (buf_ch);
4426 if (! fastmap[CHAR_LEADING_CODE (buf_ch)])
4427 goto advance;
4428 }
4429 else
4430 {
4431 if (! fastmap[TRANSLATE (*d)])
4432 goto advance;
4433 }
4434 }
4435 }
4436
4437 /* If can't match the null string, and that's all we have left, fail. */
4438 if (range >= 0 && startpos == total_size && fastmap
4439 && !bufp->can_be_null)
4440 return -1;
4441
4442 val = re_match_2_internal (bufp, string1, size1, string2, size2,
4443 startpos, regs, stop);
4444 #ifndef REGEX_MALLOC
4445 # ifdef C_ALLOCA
4446 alloca (0);
4447 # endif
4448 #endif
4449
4450 if (val >= 0)
4451 return startpos;
4452
4453 if (val == -2)
4454 return -2;
4455
4456 advance:
4457 if (!range)
4458 break;
4459 else if (range > 0)
4460 {
4461 /* Update STARTPOS to the next character boundary. */
4462 if (multibyte)
4463 {
4464 re_char *p = POS_ADDR_VSTRING (startpos);
4465 re_char *pend = STOP_ADDR_VSTRING (startpos);
4466 int len = MULTIBYTE_FORM_LENGTH (p, pend - p);
4467
4468 range -= len;
4469 if (range < 0)
4470 break;
4471 startpos += len;
4472 }
4473 else
4474 {
4475 range--;
4476 startpos++;
4477 }
4478 }
4479 else
4480 {
4481 range++;
4482 startpos--;
4483
4484 /* Update STARTPOS to the previous character boundary. */
4485 if (multibyte)
4486 {
4487 re_char *p = POS_ADDR_VSTRING (startpos) + 1;
4488 re_char *p0 = p;
4489 re_char *phead = HEAD_ADDR_VSTRING (startpos);
4490
4491 /* Find the head of multibyte form. */
4492 PREV_CHAR_BOUNDARY (p, phead);
4493 range += p0 - 1 - p;
4494 if (range > 0)
4495 break;
4496
4497 startpos -= p0 - 1 - p;
4498 }
4499 }
4500 }
4501 return -1;
4502 } /* re_search_2 */
4503 WEAK_ALIAS (__re_search_2, re_search_2)
4504 \f
4505 /* Declarations and macros for re_match_2. */
4506
4507 static int bcmp_translate _RE_ARGS((re_char *s1, re_char *s2,
4508 register int len,
4509 RE_TRANSLATE_TYPE translate,
4510 const int multibyte));
4511
4512 /* This converts PTR, a pointer into one of the search strings `string1'
4513 and `string2' into an offset from the beginning of that string. */
4514 #define POINTER_TO_OFFSET(ptr) \
4515 (FIRST_STRING_P (ptr) \
4516 ? ((regoff_t) ((ptr) - string1)) \
4517 : ((regoff_t) ((ptr) - string2 + size1)))
4518
4519 /* Call before fetching a character with *d. This switches over to
4520 string2 if necessary.
4521 Check re_match_2_internal for a discussion of why end_match_2 might
4522 not be within string2 (but be equal to end_match_1 instead). */
4523 #define PREFETCH() \
4524 while (d == dend) \
4525 { \
4526 /* End of string2 => fail. */ \
4527 if (dend == end_match_2) \
4528 goto fail; \
4529 /* End of string1 => advance to string2. */ \
4530 d = string2; \
4531 dend = end_match_2; \
4532 }
4533
4534 /* Call before fetching a char with *d if you already checked other limits.
4535 This is meant for use in lookahead operations like wordend, etc..
4536 where we might need to look at parts of the string that might be
4537 outside of the LIMITs (i.e past `stop'). */
4538 #define PREFETCH_NOLIMIT() \
4539 if (d == end1) \
4540 { \
4541 d = string2; \
4542 dend = end_match_2; \
4543 } \
4544
4545 /* Test if at very beginning or at very end of the virtual concatenation
4546 of `string1' and `string2'. If only one string, it's `string2'. */
4547 #define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2)
4548 #define AT_STRINGS_END(d) ((d) == end2)
4549
4550
4551 /* Test if D points to a character which is word-constituent. We have
4552 two special cases to check for: if past the end of string1, look at
4553 the first character in string2; and if before the beginning of
4554 string2, look at the last character in string1. */
4555 #define WORDCHAR_P(d) \
4556 (SYNTAX ((d) == end1 ? *string2 \
4557 : (d) == string2 - 1 ? *(end1 - 1) : *(d)) \
4558 == Sword)
4559
4560 /* Disabled due to a compiler bug -- see comment at case wordbound */
4561
4562 /* The comment at case wordbound is following one, but we don't use
4563 AT_WORD_BOUNDARY anymore to support multibyte form.
4564
4565 The DEC Alpha C compiler 3.x generates incorrect code for the
4566 test WORDCHAR_P (d - 1) != WORDCHAR_P (d) in the expansion of
4567 AT_WORD_BOUNDARY, so this code is disabled. Expanding the
4568 macro and introducing temporary variables works around the bug. */
4569
4570 #if 0
4571 /* Test if the character before D and the one at D differ with respect
4572 to being word-constituent. */
4573 #define AT_WORD_BOUNDARY(d) \
4574 (AT_STRINGS_BEG (d) || AT_STRINGS_END (d) \
4575 || WORDCHAR_P (d - 1) != WORDCHAR_P (d))
4576 #endif
4577
4578 /* Free everything we malloc. */
4579 #ifdef MATCH_MAY_ALLOCATE
4580 # define FREE_VAR(var) if (var) { REGEX_FREE (var); var = NULL; } else
4581 # define FREE_VARIABLES() \
4582 do { \
4583 REGEX_FREE_STACK (fail_stack.stack); \
4584 FREE_VAR (regstart); \
4585 FREE_VAR (regend); \
4586 FREE_VAR (best_regstart); \
4587 FREE_VAR (best_regend); \
4588 } while (0)
4589 #else
4590 # define FREE_VARIABLES() ((void)0) /* Do nothing! But inhibit gcc warning. */
4591 #endif /* not MATCH_MAY_ALLOCATE */
4592
4593 \f
4594 /* Optimization routines. */
4595
4596 /* If the operation is a match against one or more chars,
4597 return a pointer to the next operation, else return NULL. */
4598 static re_char *
4599 skip_one_char (p)
4600 re_char *p;
4601 {
4602 switch (SWITCH_ENUM_CAST (*p++))
4603 {
4604 case anychar:
4605 break;
4606
4607 case exactn:
4608 p += *p + 1;
4609 break;
4610
4611 case charset_not:
4612 case charset:
4613 if (CHARSET_RANGE_TABLE_EXISTS_P (p - 1))
4614 {
4615 int mcnt;
4616 p = CHARSET_RANGE_TABLE (p - 1);
4617 EXTRACT_NUMBER_AND_INCR (mcnt, p);
4618 p = CHARSET_RANGE_TABLE_END (p, mcnt);
4619 }
4620 else
4621 p += 1 + CHARSET_BITMAP_SIZE (p - 1);
4622 break;
4623
4624 case syntaxspec:
4625 case notsyntaxspec:
4626 #ifdef emacs
4627 case categoryspec:
4628 case notcategoryspec:
4629 #endif /* emacs */
4630 p++;
4631 break;
4632
4633 default:
4634 p = NULL;
4635 }
4636 return p;
4637 }
4638
4639
4640 /* Jump over non-matching operations. */
4641 static re_char *
4642 skip_noops (p, pend)
4643 re_char *p, *pend;
4644 {
4645 int mcnt;
4646 while (p < pend)
4647 {
4648 switch (SWITCH_ENUM_CAST ((re_opcode_t) *p))
4649 {
4650 case start_memory:
4651 case stop_memory:
4652 p += 2; break;
4653 case no_op:
4654 p += 1; break;
4655 case jump:
4656 p += 1;
4657 EXTRACT_NUMBER_AND_INCR (mcnt, p);
4658 p += mcnt;
4659 break;
4660 default:
4661 return p;
4662 }
4663 }
4664 assert (p == pend);
4665 return p;
4666 }
4667
4668 /* Non-zero if "p1 matches something" implies "p2 fails". */
4669 static int
4670 mutually_exclusive_p (bufp, p1, p2)
4671 struct re_pattern_buffer *bufp;
4672 re_char *p1, *p2;
4673 {
4674 re_opcode_t op2;
4675 const boolean multibyte = RE_MULTIBYTE_P (bufp);
4676 unsigned char *pend = bufp->buffer + bufp->used;
4677
4678 assert (p1 >= bufp->buffer && p1 < pend
4679 && p2 >= bufp->buffer && p2 <= pend);
4680
4681 /* Skip over open/close-group commands.
4682 If what follows this loop is a ...+ construct,
4683 look at what begins its body, since we will have to
4684 match at least one of that. */
4685 p2 = skip_noops (p2, pend);
4686 /* The same skip can be done for p1, except that this function
4687 is only used in the case where p1 is a simple match operator. */
4688 /* p1 = skip_noops (p1, pend); */
4689
4690 assert (p1 >= bufp->buffer && p1 < pend
4691 && p2 >= bufp->buffer && p2 <= pend);
4692
4693 op2 = p2 == pend ? succeed : *p2;
4694
4695 switch (SWITCH_ENUM_CAST (op2))
4696 {
4697 case succeed:
4698 case endbuf:
4699 /* If we're at the end of the pattern, we can change. */
4700 if (skip_one_char (p1))
4701 {
4702 DEBUG_PRINT1 (" End of pattern: fast loop.\n");
4703 return 1;
4704 }
4705 break;
4706
4707 case endline:
4708 case exactn:
4709 {
4710 register re_wchar_t c
4711 = (re_opcode_t) *p2 == endline ? '\n'
4712 : RE_STRING_CHAR (p2 + 2, pend - p2 - 2);
4713
4714 if ((re_opcode_t) *p1 == exactn)
4715 {
4716 if (c != RE_STRING_CHAR (p1 + 2, pend - p1 - 2))
4717 {
4718 DEBUG_PRINT3 (" '%c' != '%c' => fast loop.\n", c, p1[2]);
4719 return 1;
4720 }
4721 }
4722
4723 else if ((re_opcode_t) *p1 == charset
4724 || (re_opcode_t) *p1 == charset_not)
4725 {
4726 int not = (re_opcode_t) *p1 == charset_not;
4727
4728 /* Test if C is listed in charset (or charset_not)
4729 at `p1'. */
4730 if (! multibyte || IS_REAL_ASCII (c))
4731 {
4732 if (c < CHARSET_BITMAP_SIZE (p1) * BYTEWIDTH
4733 && p1[2 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
4734 not = !not;
4735 }
4736 else if (CHARSET_RANGE_TABLE_EXISTS_P (p1))
4737 CHARSET_LOOKUP_RANGE_TABLE (not, c, p1);
4738
4739 /* `not' is equal to 1 if c would match, which means
4740 that we can't change to pop_failure_jump. */
4741 if (!not)
4742 {
4743 DEBUG_PRINT1 (" No match => fast loop.\n");
4744 return 1;
4745 }
4746 }
4747 else if ((re_opcode_t) *p1 == anychar
4748 && c == '\n')
4749 {
4750 DEBUG_PRINT1 (" . != \\n => fast loop.\n");
4751 return 1;
4752 }
4753 }
4754 break;
4755
4756 case charset:
4757 {
4758 if ((re_opcode_t) *p1 == exactn)
4759 /* Reuse the code above. */
4760 return mutually_exclusive_p (bufp, p2, p1);
4761
4762 /* It is hard to list up all the character in charset
4763 P2 if it includes multibyte character. Give up in
4764 such case. */
4765 else if (!multibyte || !CHARSET_RANGE_TABLE_EXISTS_P (p2))
4766 {
4767 /* Now, we are sure that P2 has no range table.
4768 So, for the size of bitmap in P2, `p2[1]' is
4769 enough. But P1 may have range table, so the
4770 size of bitmap table of P1 is extracted by
4771 using macro `CHARSET_BITMAP_SIZE'.
4772
4773 In a multibyte case, we know that all the character
4774 listed in P2 is ASCII. In a unibyte case, P1 has only a
4775 bitmap table. So, in both cases, it is enough to test
4776 only the bitmap table of P1. */
4777
4778 if ((re_opcode_t) *p1 == charset)
4779 {
4780 int idx;
4781 /* We win if the charset inside the loop
4782 has no overlap with the one after the loop. */
4783 for (idx = 0;
4784 (idx < (int) p2[1]
4785 && idx < CHARSET_BITMAP_SIZE (p1));
4786 idx++)
4787 if ((p2[2 + idx] & p1[2 + idx]) != 0)
4788 break;
4789
4790 if (idx == p2[1]
4791 || idx == CHARSET_BITMAP_SIZE (p1))
4792 {
4793 DEBUG_PRINT1 (" No match => fast loop.\n");
4794 return 1;
4795 }
4796 }
4797 else if ((re_opcode_t) *p1 == charset_not)
4798 {
4799 int idx;
4800 /* We win if the charset_not inside the loop lists
4801 every character listed in the charset after. */
4802 for (idx = 0; idx < (int) p2[1]; idx++)
4803 if (! (p2[2 + idx] == 0
4804 || (idx < CHARSET_BITMAP_SIZE (p1)
4805 && ((p2[2 + idx] & ~ p1[2 + idx]) == 0))))
4806 break;
4807
4808 if (idx == p2[1])
4809 {
4810 DEBUG_PRINT1 (" No match => fast loop.\n");
4811 return 1;
4812 }
4813 }
4814 }
4815 }
4816 break;
4817
4818 case charset_not:
4819 switch (SWITCH_ENUM_CAST (*p1))
4820 {
4821 case exactn:
4822 case charset:
4823 /* Reuse the code above. */
4824 return mutually_exclusive_p (bufp, p2, p1);
4825 case charset_not:
4826 /* When we have two charset_not, it's very unlikely that
4827 they don't overlap. The union of the two sets of excluded
4828 chars should cover all possible chars, which, as a matter of
4829 fact, is virtually impossible in multibyte buffers. */
4830 break;
4831 }
4832 break;
4833
4834 case wordend:
4835 return ((re_opcode_t) *p1 == syntaxspec && p1[1] == Sword);
4836 case symend:
4837 return ((re_opcode_t) *p1 == syntaxspec
4838 && (p1[1] == Ssymbol || p1[1] == Sword));
4839 case notsyntaxspec:
4840 return ((re_opcode_t) *p1 == syntaxspec && p1[1] == p2[1]);
4841
4842 case wordbeg:
4843 return ((re_opcode_t) *p1 == notsyntaxspec && p1[1] == Sword);
4844 case symbeg:
4845 return ((re_opcode_t) *p1 == notsyntaxspec
4846 && (p1[1] == Ssymbol || p1[1] == Sword));
4847 case syntaxspec:
4848 return ((re_opcode_t) *p1 == notsyntaxspec && p1[1] == p2[1]);
4849
4850 case wordbound:
4851 return (((re_opcode_t) *p1 == notsyntaxspec
4852 || (re_opcode_t) *p1 == syntaxspec)
4853 && p1[1] == Sword);
4854
4855 #ifdef emacs
4856 case categoryspec:
4857 return ((re_opcode_t) *p1 == notcategoryspec && p1[1] == p2[1]);
4858 case notcategoryspec:
4859 return ((re_opcode_t) *p1 == categoryspec && p1[1] == p2[1]);
4860 #endif /* emacs */
4861
4862 default:
4863 ;
4864 }
4865
4866 /* Safe default. */
4867 return 0;
4868 }
4869
4870 \f
4871 /* Matching routines. */
4872
4873 #ifndef emacs /* Emacs never uses this. */
4874 /* re_match is like re_match_2 except it takes only a single string. */
4875
4876 int
4877 re_match (bufp, string, size, pos, regs)
4878 struct re_pattern_buffer *bufp;
4879 const char *string;
4880 int size, pos;
4881 struct re_registers *regs;
4882 {
4883 int result = re_match_2_internal (bufp, NULL, 0, (re_char*) string, size,
4884 pos, regs, size);
4885 # if defined C_ALLOCA && !defined REGEX_MALLOC
4886 alloca (0);
4887 # endif
4888 return result;
4889 }
4890 WEAK_ALIAS (__re_match, re_match)
4891 #endif /* not emacs */
4892
4893 #ifdef emacs
4894 /* In Emacs, this is the string or buffer in which we
4895 are matching. It is used for looking up syntax properties. */
4896 Lisp_Object re_match_object;
4897 #endif
4898
4899 /* re_match_2 matches the compiled pattern in BUFP against the
4900 the (virtual) concatenation of STRING1 and STRING2 (of length SIZE1
4901 and SIZE2, respectively). We start matching at POS, and stop
4902 matching at STOP.
4903
4904 If REGS is non-null and the `no_sub' field of BUFP is nonzero, we
4905 store offsets for the substring each group matched in REGS. See the
4906 documentation for exactly how many groups we fill.
4907
4908 We return -1 if no match, -2 if an internal error (such as the
4909 failure stack overflowing). Otherwise, we return the length of the
4910 matched substring. */
4911
4912 int
4913 re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
4914 struct re_pattern_buffer *bufp;
4915 const char *string1, *string2;
4916 int size1, size2;
4917 int pos;
4918 struct re_registers *regs;
4919 int stop;
4920 {
4921 int result;
4922
4923 #ifdef emacs
4924 int charpos;
4925 gl_state.object = re_match_object;
4926 charpos = SYNTAX_TABLE_BYTE_TO_CHAR (POS_AS_IN_BUFFER (pos));
4927 SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object, charpos, 1);
4928 #endif
4929
4930 result = re_match_2_internal (bufp, (re_char*) string1, size1,
4931 (re_char*) string2, size2,
4932 pos, regs, stop);
4933 #if defined C_ALLOCA && !defined REGEX_MALLOC
4934 alloca (0);
4935 #endif
4936 return result;
4937 }
4938 WEAK_ALIAS (__re_match_2, re_match_2)
4939
4940 #ifdef emacs
4941 #define TRANSLATE_VIA_MULTIBYTE(c) \
4942 do { \
4943 if (multibyte) \
4944 (c) = TRANSLATE (c); \
4945 else \
4946 { \
4947 MAKE_CHAR_MULTIBYTE (c); \
4948 (c) = TRANSLATE (c); \
4949 MAKE_CHAR_UNIBYTE (c); \
4950 } \
4951 } while (0)
4952
4953 #else
4954 #define TRANSLATE_VIA_MULTIBYTE(c) ((c) = TRANSLATE (c))
4955 #endif
4956
4957
4958 /* This is a separate function so that we can force an alloca cleanup
4959 afterwards. */
4960 static int
4961 re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
4962 struct re_pattern_buffer *bufp;
4963 re_char *string1, *string2;
4964 int size1, size2;
4965 int pos;
4966 struct re_registers *regs;
4967 int stop;
4968 {
4969 /* General temporaries. */
4970 int mcnt;
4971 size_t reg;
4972 boolean not;
4973
4974 /* Just past the end of the corresponding string. */
4975 re_char *end1, *end2;
4976
4977 /* Pointers into string1 and string2, just past the last characters in
4978 each to consider matching. */
4979 re_char *end_match_1, *end_match_2;
4980
4981 /* Where we are in the data, and the end of the current string. */
4982 re_char *d, *dend;
4983
4984 /* Used sometimes to remember where we were before starting matching
4985 an operator so that we can go back in case of failure. This "atomic"
4986 behavior of matching opcodes is indispensable to the correctness
4987 of the on_failure_keep_string_jump optimization. */
4988 re_char *dfail;
4989
4990 /* Where we are in the pattern, and the end of the pattern. */
4991 re_char *p = bufp->buffer;
4992 re_char *pend = p + bufp->used;
4993
4994 /* We use this to map every character in the string. */
4995 RE_TRANSLATE_TYPE translate = bufp->translate;
4996
4997 /* Nonzero if BUFP is setup for multibyte characters. We are sure
4998 that it is the same as RE_TARGET_MULTIBYTE_P (bufp). */
4999 const boolean multibyte = RE_MULTIBYTE_P (bufp);
5000
5001 /* Failure point stack. Each place that can handle a failure further
5002 down the line pushes a failure point on this stack. It consists of
5003 regstart, and regend for all registers corresponding to
5004 the subexpressions we're currently inside, plus the number of such
5005 registers, and, finally, two char *'s. The first char * is where
5006 to resume scanning the pattern; the second one is where to resume
5007 scanning the strings. */
5008 #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
5009 fail_stack_type fail_stack;
5010 #endif
5011 #ifdef DEBUG
5012 unsigned nfailure_points_pushed = 0, nfailure_points_popped = 0;
5013 #endif
5014
5015 #if defined REL_ALLOC && defined REGEX_MALLOC
5016 /* This holds the pointer to the failure stack, when
5017 it is allocated relocatably. */
5018 fail_stack_elt_t *failure_stack_ptr;
5019 #endif
5020
5021 /* We fill all the registers internally, independent of what we
5022 return, for use in backreferences. The number here includes
5023 an element for register zero. */
5024 size_t num_regs = bufp->re_nsub + 1;
5025
5026 /* Information on the contents of registers. These are pointers into
5027 the input strings; they record just what was matched (on this
5028 attempt) by a subexpression part of the pattern, that is, the
5029 regnum-th regstart pointer points to where in the pattern we began
5030 matching and the regnum-th regend points to right after where we
5031 stopped matching the regnum-th subexpression. (The zeroth register
5032 keeps track of what the whole pattern matches.) */
5033 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
5034 re_char **regstart, **regend;
5035 #endif
5036
5037 /* The following record the register info as found in the above
5038 variables when we find a match better than any we've seen before.
5039 This happens as we backtrack through the failure points, which in
5040 turn happens only if we have not yet matched the entire string. */
5041 unsigned best_regs_set = false;
5042 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
5043 re_char **best_regstart, **best_regend;
5044 #endif
5045
5046 /* Logically, this is `best_regend[0]'. But we don't want to have to
5047 allocate space for that if we're not allocating space for anything
5048 else (see below). Also, we never need info about register 0 for
5049 any of the other register vectors, and it seems rather a kludge to
5050 treat `best_regend' differently than the rest. So we keep track of
5051 the end of the best match so far in a separate variable. We
5052 initialize this to NULL so that when we backtrack the first time
5053 and need to test it, it's not garbage. */
5054 re_char *match_end = NULL;
5055
5056 #ifdef DEBUG
5057 /* Counts the total number of registers pushed. */
5058 unsigned num_regs_pushed = 0;
5059 #endif
5060
5061 DEBUG_PRINT1 ("\n\nEntering re_match_2.\n");
5062
5063 INIT_FAIL_STACK ();
5064
5065 #ifdef MATCH_MAY_ALLOCATE
5066 /* Do not bother to initialize all the register variables if there are
5067 no groups in the pattern, as it takes a fair amount of time. If
5068 there are groups, we include space for register 0 (the whole
5069 pattern), even though we never use it, since it simplifies the
5070 array indexing. We should fix this. */
5071 if (bufp->re_nsub)
5072 {
5073 regstart = REGEX_TALLOC (num_regs, re_char *);
5074 regend = REGEX_TALLOC (num_regs, re_char *);
5075 best_regstart = REGEX_TALLOC (num_regs, re_char *);
5076 best_regend = REGEX_TALLOC (num_regs, re_char *);
5077
5078 if (!(regstart && regend && best_regstart && best_regend))
5079 {
5080 FREE_VARIABLES ();
5081 return -2;
5082 }
5083 }
5084 else
5085 {
5086 /* We must initialize all our variables to NULL, so that
5087 `FREE_VARIABLES' doesn't try to free them. */
5088 regstart = regend = best_regstart = best_regend = NULL;
5089 }
5090 #endif /* MATCH_MAY_ALLOCATE */
5091
5092 /* The starting position is bogus. */
5093 if (pos < 0 || pos > size1 + size2)
5094 {
5095 FREE_VARIABLES ();
5096 return -1;
5097 }
5098
5099 /* Initialize subexpression text positions to -1 to mark ones that no
5100 start_memory/stop_memory has been seen for. Also initialize the
5101 register information struct. */
5102 for (reg = 1; reg < num_regs; reg++)
5103 regstart[reg] = regend[reg] = NULL;
5104
5105 /* We move `string1' into `string2' if the latter's empty -- but not if
5106 `string1' is null. */
5107 if (size2 == 0 && string1 != NULL)
5108 {
5109 string2 = string1;
5110 size2 = size1;
5111 string1 = 0;
5112 size1 = 0;
5113 }
5114 end1 = string1 + size1;
5115 end2 = string2 + size2;
5116
5117 /* `p' scans through the pattern as `d' scans through the data.
5118 `dend' is the end of the input string that `d' points within. `d'
5119 is advanced into the following input string whenever necessary, but
5120 this happens before fetching; therefore, at the beginning of the
5121 loop, `d' can be pointing at the end of a string, but it cannot
5122 equal `string2'. */
5123 if (pos >= size1)
5124 {
5125 /* Only match within string2. */
5126 d = string2 + pos - size1;
5127 dend = end_match_2 = string2 + stop - size1;
5128 end_match_1 = end1; /* Just to give it a value. */
5129 }
5130 else
5131 {
5132 if (stop < size1)
5133 {
5134 /* Only match within string1. */
5135 end_match_1 = string1 + stop;
5136 /* BEWARE!
5137 When we reach end_match_1, PREFETCH normally switches to string2.
5138 But in the present case, this means that just doing a PREFETCH
5139 makes us jump from `stop' to `gap' within the string.
5140 What we really want here is for the search to stop as
5141 soon as we hit end_match_1. That's why we set end_match_2
5142 to end_match_1 (since PREFETCH fails as soon as we hit
5143 end_match_2). */
5144 end_match_2 = end_match_1;
5145 }
5146 else
5147 { /* It's important to use this code when stop == size so that
5148 moving `d' from end1 to string2 will not prevent the d == dend
5149 check from catching the end of string. */
5150 end_match_1 = end1;
5151 end_match_2 = string2 + stop - size1;
5152 }
5153 d = string1 + pos;
5154 dend = end_match_1;
5155 }
5156
5157 DEBUG_PRINT1 ("The compiled pattern is: ");
5158 DEBUG_PRINT_COMPILED_PATTERN (bufp, p, pend);
5159 DEBUG_PRINT1 ("The string to match is: `");
5160 DEBUG_PRINT_DOUBLE_STRING (d, string1, size1, string2, size2);
5161 DEBUG_PRINT1 ("'\n");
5162
5163 /* This loops over pattern commands. It exits by returning from the
5164 function if the match is complete, or it drops through if the match
5165 fails at this starting point in the input data. */
5166 for (;;)
5167 {
5168 DEBUG_PRINT2 ("\n%p: ", p);
5169
5170 if (p == pend)
5171 { /* End of pattern means we might have succeeded. */
5172 DEBUG_PRINT1 ("end of pattern ... ");
5173
5174 /* If we haven't matched the entire string, and we want the
5175 longest match, try backtracking. */
5176 if (d != end_match_2)
5177 {
5178 /* 1 if this match ends in the same string (string1 or string2)
5179 as the best previous match. */
5180 boolean same_str_p = (FIRST_STRING_P (match_end)
5181 == FIRST_STRING_P (d));
5182 /* 1 if this match is the best seen so far. */
5183 boolean best_match_p;
5184
5185 /* AIX compiler got confused when this was combined
5186 with the previous declaration. */
5187 if (same_str_p)
5188 best_match_p = d > match_end;
5189 else
5190 best_match_p = !FIRST_STRING_P (d);
5191
5192 DEBUG_PRINT1 ("backtracking.\n");
5193
5194 if (!FAIL_STACK_EMPTY ())
5195 { /* More failure points to try. */
5196
5197 /* If exceeds best match so far, save it. */
5198 if (!best_regs_set || best_match_p)
5199 {
5200 best_regs_set = true;
5201 match_end = d;
5202
5203 DEBUG_PRINT1 ("\nSAVING match as best so far.\n");
5204
5205 for (reg = 1; reg < num_regs; reg++)
5206 {
5207 best_regstart[reg] = regstart[reg];
5208 best_regend[reg] = regend[reg];
5209 }
5210 }
5211 goto fail;
5212 }
5213
5214 /* If no failure points, don't restore garbage. And if
5215 last match is real best match, don't restore second
5216 best one. */
5217 else if (best_regs_set && !best_match_p)
5218 {
5219 restore_best_regs:
5220 /* Restore best match. It may happen that `dend ==
5221 end_match_1' while the restored d is in string2.
5222 For example, the pattern `x.*y.*z' against the
5223 strings `x-' and `y-z-', if the two strings are
5224 not consecutive in memory. */
5225 DEBUG_PRINT1 ("Restoring best registers.\n");
5226
5227 d = match_end;
5228 dend = ((d >= string1 && d <= end1)
5229 ? end_match_1 : end_match_2);
5230
5231 for (reg = 1; reg < num_regs; reg++)
5232 {
5233 regstart[reg] = best_regstart[reg];
5234 regend[reg] = best_regend[reg];
5235 }
5236 }
5237 } /* d != end_match_2 */
5238
5239 succeed_label:
5240 DEBUG_PRINT1 ("Accepting match.\n");
5241
5242 /* If caller wants register contents data back, do it. */
5243 if (regs && !bufp->no_sub)
5244 {
5245 /* Have the register data arrays been allocated? */
5246 if (bufp->regs_allocated == REGS_UNALLOCATED)
5247 { /* No. So allocate them with malloc. We need one
5248 extra element beyond `num_regs' for the `-1' marker
5249 GNU code uses. */
5250 regs->num_regs = MAX (RE_NREGS, num_regs + 1);
5251 regs->start = TALLOC (regs->num_regs, regoff_t);
5252 regs->end = TALLOC (regs->num_regs, regoff_t);
5253 if (regs->start == NULL || regs->end == NULL)
5254 {
5255 FREE_VARIABLES ();
5256 return -2;
5257 }
5258 bufp->regs_allocated = REGS_REALLOCATE;
5259 }
5260 else if (bufp->regs_allocated == REGS_REALLOCATE)
5261 { /* Yes. If we need more elements than were already
5262 allocated, reallocate them. If we need fewer, just
5263 leave it alone. */
5264 if (regs->num_regs < num_regs + 1)
5265 {
5266 regs->num_regs = num_regs + 1;
5267 RETALLOC (regs->start, regs->num_regs, regoff_t);
5268 RETALLOC (regs->end, regs->num_regs, regoff_t);
5269 if (regs->start == NULL || regs->end == NULL)
5270 {
5271 FREE_VARIABLES ();
5272 return -2;
5273 }
5274 }
5275 }
5276 else
5277 {
5278 /* These braces fend off a "empty body in an else-statement"
5279 warning under GCC when assert expands to nothing. */
5280 assert (bufp->regs_allocated == REGS_FIXED);
5281 }
5282
5283 /* Convert the pointer data in `regstart' and `regend' to
5284 indices. Register zero has to be set differently,
5285 since we haven't kept track of any info for it. */
5286 if (regs->num_regs > 0)
5287 {
5288 regs->start[0] = pos;
5289 regs->end[0] = POINTER_TO_OFFSET (d);
5290 }
5291
5292 /* Go through the first `min (num_regs, regs->num_regs)'
5293 registers, since that is all we initialized. */
5294 for (reg = 1; reg < MIN (num_regs, regs->num_regs); reg++)
5295 {
5296 if (REG_UNSET (regstart[reg]) || REG_UNSET (regend[reg]))
5297 regs->start[reg] = regs->end[reg] = -1;
5298 else
5299 {
5300 regs->start[reg]
5301 = (regoff_t) POINTER_TO_OFFSET (regstart[reg]);
5302 regs->end[reg]
5303 = (regoff_t) POINTER_TO_OFFSET (regend[reg]);
5304 }
5305 }
5306
5307 /* If the regs structure we return has more elements than
5308 were in the pattern, set the extra elements to -1. If
5309 we (re)allocated the registers, this is the case,
5310 because we always allocate enough to have at least one
5311 -1 at the end. */
5312 for (reg = num_regs; reg < regs->num_regs; reg++)
5313 regs->start[reg] = regs->end[reg] = -1;
5314 } /* regs && !bufp->no_sub */
5315
5316 DEBUG_PRINT4 ("%u failure points pushed, %u popped (%u remain).\n",
5317 nfailure_points_pushed, nfailure_points_popped,
5318 nfailure_points_pushed - nfailure_points_popped);
5319 DEBUG_PRINT2 ("%u registers pushed.\n", num_regs_pushed);
5320
5321 mcnt = POINTER_TO_OFFSET (d) - pos;
5322
5323 DEBUG_PRINT2 ("Returning %d from re_match_2.\n", mcnt);
5324
5325 FREE_VARIABLES ();
5326 return mcnt;
5327 }
5328
5329 /* Otherwise match next pattern command. */
5330 switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++))
5331 {
5332 /* Ignore these. Used to ignore the n of succeed_n's which
5333 currently have n == 0. */
5334 case no_op:
5335 DEBUG_PRINT1 ("EXECUTING no_op.\n");
5336 break;
5337
5338 case succeed:
5339 DEBUG_PRINT1 ("EXECUTING succeed.\n");
5340 goto succeed_label;
5341
5342 /* Match the next n pattern characters exactly. The following
5343 byte in the pattern defines n, and the n bytes after that
5344 are the characters to match. */
5345 case exactn:
5346 mcnt = *p++;
5347 DEBUG_PRINT2 ("EXECUTING exactn %d.\n", mcnt);
5348
5349 /* Remember the start point to rollback upon failure. */
5350 dfail = d;
5351
5352 #ifndef emacs
5353 /* This is written out as an if-else so we don't waste time
5354 testing `translate' inside the loop. */
5355 if (RE_TRANSLATE_P (translate))
5356 do
5357 {
5358 PREFETCH ();
5359 if (RE_TRANSLATE (translate, *d) != *p++)
5360 {
5361 d = dfail;
5362 goto fail;
5363 }
5364 d++;
5365 }
5366 while (--mcnt);
5367 else
5368 do
5369 {
5370 PREFETCH ();
5371 if (*d++ != *p++)
5372 {
5373 d = dfail;
5374 goto fail;
5375 }
5376 }
5377 while (--mcnt);
5378 #else /* emacs */
5379 /* The cost of testing `translate' is comparatively small. */
5380 if (multibyte)
5381 do
5382 {
5383 int pat_charlen, buf_charlen;
5384 unsigned int pat_ch, buf_ch;
5385
5386 PREFETCH ();
5387 pat_ch = STRING_CHAR_AND_LENGTH (p, pend - p, pat_charlen);
5388 buf_ch = STRING_CHAR_AND_LENGTH (d, dend - d, buf_charlen);
5389
5390 if (TRANSLATE (buf_ch) != pat_ch)
5391 {
5392 d = dfail;
5393 goto fail;
5394 }
5395
5396 p += pat_charlen;
5397 d += buf_charlen;
5398 mcnt -= pat_charlen;
5399 }
5400 while (mcnt > 0);
5401 else
5402 do
5403 {
5404 unsigned int buf_ch;
5405
5406 PREFETCH ();
5407 buf_ch = *d++;
5408 TRANSLATE_VIA_MULTIBYTE (buf_ch);
5409 if (buf_ch != *p++)
5410 {
5411 d = dfail;
5412 goto fail;
5413 }
5414 }
5415 while (--mcnt);
5416 #endif
5417 break;
5418
5419
5420 /* Match any character except possibly a newline or a null. */
5421 case anychar:
5422 {
5423 int buf_charlen;
5424 re_wchar_t buf_ch;
5425
5426 DEBUG_PRINT1 ("EXECUTING anychar.\n");
5427
5428 PREFETCH ();
5429 buf_ch = RE_STRING_CHAR_AND_LENGTH (d, dend - d, buf_charlen);
5430 buf_ch = TRANSLATE (buf_ch);
5431
5432 if ((!(bufp->syntax & RE_DOT_NEWLINE)
5433 && buf_ch == '\n')
5434 || ((bufp->syntax & RE_DOT_NOT_NULL)
5435 && buf_ch == '\000'))
5436 goto fail;
5437
5438 DEBUG_PRINT2 (" Matched `%d'.\n", *d);
5439 d += buf_charlen;
5440 }
5441 break;
5442
5443
5444 case charset:
5445 case charset_not:
5446 {
5447 register unsigned int c;
5448 boolean not = (re_opcode_t) *(p - 1) == charset_not;
5449 int len;
5450
5451 /* Start of actual range_table, or end of bitmap if there is no
5452 range table. */
5453 re_char *range_table;
5454
5455 /* Nonzero if there is a range table. */
5456 int range_table_exists;
5457
5458 /* Number of ranges of range table. This is not included
5459 in the initial byte-length of the command. */
5460 int count = 0;
5461
5462 DEBUG_PRINT2 ("EXECUTING charset%s.\n", not ? "_not" : "");
5463
5464 range_table_exists = CHARSET_RANGE_TABLE_EXISTS_P (&p[-1]);
5465
5466 if (range_table_exists)
5467 {
5468 range_table = CHARSET_RANGE_TABLE (&p[-1]); /* Past the bitmap. */
5469 EXTRACT_NUMBER_AND_INCR (count, range_table);
5470 }
5471
5472 PREFETCH ();
5473 c = RE_STRING_CHAR_AND_LENGTH (d, dend - d, len);
5474 TRANSLATE_VIA_MULTIBYTE (c); /* The character to match. */
5475
5476 if (! multibyte || IS_REAL_ASCII (c))
5477 { /* Lookup bitmap. */
5478 /* Cast to `unsigned' instead of `unsigned char' in
5479 case the bit list is a full 32 bytes long. */
5480 if (c < (unsigned) (CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH)
5481 && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
5482 not = !not;
5483 }
5484 #ifdef emacs
5485 else if (range_table_exists)
5486 {
5487 int class_bits = CHARSET_RANGE_TABLE_BITS (&p[-1]);
5488
5489 if ( (class_bits & BIT_LOWER && ISLOWER (c))
5490 | (class_bits & BIT_MULTIBYTE)
5491 | (class_bits & BIT_PUNCT && ISPUNCT (c))
5492 | (class_bits & BIT_SPACE && ISSPACE (c))
5493 | (class_bits & BIT_UPPER && ISUPPER (c))
5494 | (class_bits & BIT_WORD && ISWORD (c)))
5495 not = !not;
5496 else
5497 CHARSET_LOOKUP_RANGE_TABLE_RAW (not, c, range_table, count);
5498 }
5499 #endif /* emacs */
5500
5501 if (range_table_exists)
5502 p = CHARSET_RANGE_TABLE_END (range_table, count);
5503 else
5504 p += CHARSET_BITMAP_SIZE (&p[-1]) + 1;
5505
5506 if (!not) goto fail;
5507
5508 d += len;
5509 break;
5510 }
5511
5512
5513 /* The beginning of a group is represented by start_memory.
5514 The argument is the register number. The text
5515 matched within the group is recorded (in the internal
5516 registers data structure) under the register number. */
5517 case start_memory:
5518 DEBUG_PRINT2 ("EXECUTING start_memory %d:\n", *p);
5519
5520 /* In case we need to undo this operation (via backtracking). */
5521 PUSH_FAILURE_REG ((unsigned int)*p);
5522
5523 regstart[*p] = d;
5524 regend[*p] = NULL; /* probably unnecessary. -sm */
5525 DEBUG_PRINT2 (" regstart: %d\n", POINTER_TO_OFFSET (regstart[*p]));
5526
5527 /* Move past the register number and inner group count. */
5528 p += 1;
5529 break;
5530
5531
5532 /* The stop_memory opcode represents the end of a group. Its
5533 argument is the same as start_memory's: the register number. */
5534 case stop_memory:
5535 DEBUG_PRINT2 ("EXECUTING stop_memory %d:\n", *p);
5536
5537 assert (!REG_UNSET (regstart[*p]));
5538 /* Strictly speaking, there should be code such as:
5539
5540 assert (REG_UNSET (regend[*p]));
5541 PUSH_FAILURE_REGSTOP ((unsigned int)*p);
5542
5543 But the only info to be pushed is regend[*p] and it is known to
5544 be UNSET, so there really isn't anything to push.
5545 Not pushing anything, on the other hand deprives us from the
5546 guarantee that regend[*p] is UNSET since undoing this operation
5547 will not reset its value properly. This is not important since
5548 the value will only be read on the next start_memory or at
5549 the very end and both events can only happen if this stop_memory
5550 is *not* undone. */
5551
5552 regend[*p] = d;
5553 DEBUG_PRINT2 (" regend: %d\n", POINTER_TO_OFFSET (regend[*p]));
5554
5555 /* Move past the register number and the inner group count. */
5556 p += 1;
5557 break;
5558
5559
5560 /* \<digit> has been turned into a `duplicate' command which is
5561 followed by the numeric value of <digit> as the register number. */
5562 case duplicate:
5563 {
5564 register re_char *d2, *dend2;
5565 int regno = *p++; /* Get which register to match against. */
5566 DEBUG_PRINT2 ("EXECUTING duplicate %d.\n", regno);
5567
5568 /* Can't back reference a group which we've never matched. */
5569 if (REG_UNSET (regstart[regno]) || REG_UNSET (regend[regno]))
5570 goto fail;
5571
5572 /* Where in input to try to start matching. */
5573 d2 = regstart[regno];
5574
5575 /* Remember the start point to rollback upon failure. */
5576 dfail = d;
5577
5578 /* Where to stop matching; if both the place to start and
5579 the place to stop matching are in the same string, then
5580 set to the place to stop, otherwise, for now have to use
5581 the end of the first string. */
5582
5583 dend2 = ((FIRST_STRING_P (regstart[regno])
5584 == FIRST_STRING_P (regend[regno]))
5585 ? regend[regno] : end_match_1);
5586 for (;;)
5587 {
5588 /* If necessary, advance to next segment in register
5589 contents. */
5590 while (d2 == dend2)
5591 {
5592 if (dend2 == end_match_2) break;
5593 if (dend2 == regend[regno]) break;
5594
5595 /* End of string1 => advance to string2. */
5596 d2 = string2;
5597 dend2 = regend[regno];
5598 }
5599 /* At end of register contents => success */
5600 if (d2 == dend2) break;
5601
5602 /* If necessary, advance to next segment in data. */
5603 PREFETCH ();
5604
5605 /* How many characters left in this segment to match. */
5606 mcnt = dend - d;
5607
5608 /* Want how many consecutive characters we can match in
5609 one shot, so, if necessary, adjust the count. */
5610 if (mcnt > dend2 - d2)
5611 mcnt = dend2 - d2;
5612
5613 /* Compare that many; failure if mismatch, else move
5614 past them. */
5615 if (RE_TRANSLATE_P (translate)
5616 ? bcmp_translate (d, d2, mcnt, translate, multibyte)
5617 : memcmp (d, d2, mcnt))
5618 {
5619 d = dfail;
5620 goto fail;
5621 }
5622 d += mcnt, d2 += mcnt;
5623 }
5624 }
5625 break;
5626
5627
5628 /* begline matches the empty string at the beginning of the string
5629 (unless `not_bol' is set in `bufp'), and after newlines. */
5630 case begline:
5631 DEBUG_PRINT1 ("EXECUTING begline.\n");
5632
5633 if (AT_STRINGS_BEG (d))
5634 {
5635 if (!bufp->not_bol) break;
5636 }
5637 else
5638 {
5639 unsigned c;
5640 GET_CHAR_BEFORE_2 (c, d, string1, end1, string2, end2);
5641 if (c == '\n')
5642 break;
5643 }
5644 /* In all other cases, we fail. */
5645 goto fail;
5646
5647
5648 /* endline is the dual of begline. */
5649 case endline:
5650 DEBUG_PRINT1 ("EXECUTING endline.\n");
5651
5652 if (AT_STRINGS_END (d))
5653 {
5654 if (!bufp->not_eol) break;
5655 }
5656 else
5657 {
5658 PREFETCH_NOLIMIT ();
5659 if (*d == '\n')
5660 break;
5661 }
5662 goto fail;
5663
5664
5665 /* Match at the very beginning of the data. */
5666 case begbuf:
5667 DEBUG_PRINT1 ("EXECUTING begbuf.\n");
5668 if (AT_STRINGS_BEG (d))
5669 break;
5670 goto fail;
5671
5672
5673 /* Match at the very end of the data. */
5674 case endbuf:
5675 DEBUG_PRINT1 ("EXECUTING endbuf.\n");
5676 if (AT_STRINGS_END (d))
5677 break;
5678 goto fail;
5679
5680
5681 /* on_failure_keep_string_jump is used to optimize `.*\n'. It
5682 pushes NULL as the value for the string on the stack. Then
5683 `POP_FAILURE_POINT' will keep the current value for the
5684 string, instead of restoring it. To see why, consider
5685 matching `foo\nbar' against `.*\n'. The .* matches the foo;
5686 then the . fails against the \n. But the next thing we want
5687 to do is match the \n against the \n; if we restored the
5688 string value, we would be back at the foo.
5689
5690 Because this is used only in specific cases, we don't need to
5691 check all the things that `on_failure_jump' does, to make
5692 sure the right things get saved on the stack. Hence we don't
5693 share its code. The only reason to push anything on the
5694 stack at all is that otherwise we would have to change
5695 `anychar's code to do something besides goto fail in this
5696 case; that seems worse than this. */
5697 case on_failure_keep_string_jump:
5698 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5699 DEBUG_PRINT3 ("EXECUTING on_failure_keep_string_jump %d (to %p):\n",
5700 mcnt, p + mcnt);
5701
5702 PUSH_FAILURE_POINT (p - 3, NULL);
5703 break;
5704
5705 /* A nasty loop is introduced by the non-greedy *? and +?.
5706 With such loops, the stack only ever contains one failure point
5707 at a time, so that a plain on_failure_jump_loop kind of
5708 cycle detection cannot work. Worse yet, such a detection
5709 can not only fail to detect a cycle, but it can also wrongly
5710 detect a cycle (between different instantiations of the same
5711 loop).
5712 So the method used for those nasty loops is a little different:
5713 We use a special cycle-detection-stack-frame which is pushed
5714 when the on_failure_jump_nastyloop failure-point is *popped*.
5715 This special frame thus marks the beginning of one iteration
5716 through the loop and we can hence easily check right here
5717 whether something matched between the beginning and the end of
5718 the loop. */
5719 case on_failure_jump_nastyloop:
5720 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5721 DEBUG_PRINT3 ("EXECUTING on_failure_jump_nastyloop %d (to %p):\n",
5722 mcnt, p + mcnt);
5723
5724 assert ((re_opcode_t)p[-4] == no_op);
5725 {
5726 int cycle = 0;
5727 CHECK_INFINITE_LOOP (p - 4, d);
5728 if (!cycle)
5729 /* If there's a cycle, just continue without pushing
5730 this failure point. The failure point is the "try again"
5731 option, which shouldn't be tried.
5732 We want (x?)*?y\1z to match both xxyz and xxyxz. */
5733 PUSH_FAILURE_POINT (p - 3, d);
5734 }
5735 break;
5736
5737 /* Simple loop detecting on_failure_jump: just check on the
5738 failure stack if the same spot was already hit earlier. */
5739 case on_failure_jump_loop:
5740 on_failure:
5741 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5742 DEBUG_PRINT3 ("EXECUTING on_failure_jump_loop %d (to %p):\n",
5743 mcnt, p + mcnt);
5744 {
5745 int cycle = 0;
5746 CHECK_INFINITE_LOOP (p - 3, d);
5747 if (cycle)
5748 /* If there's a cycle, get out of the loop, as if the matching
5749 had failed. We used to just `goto fail' here, but that was
5750 aborting the search a bit too early: we want to keep the
5751 empty-loop-match and keep matching after the loop.
5752 We want (x?)*y\1z to match both xxyz and xxyxz. */
5753 p += mcnt;
5754 else
5755 PUSH_FAILURE_POINT (p - 3, d);
5756 }
5757 break;
5758
5759
5760 /* Uses of on_failure_jump:
5761
5762 Each alternative starts with an on_failure_jump that points
5763 to the beginning of the next alternative. Each alternative
5764 except the last ends with a jump that in effect jumps past
5765 the rest of the alternatives. (They really jump to the
5766 ending jump of the following alternative, because tensioning
5767 these jumps is a hassle.)
5768
5769 Repeats start with an on_failure_jump that points past both
5770 the repetition text and either the following jump or
5771 pop_failure_jump back to this on_failure_jump. */
5772 case on_failure_jump:
5773 IMMEDIATE_QUIT_CHECK;
5774 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5775 DEBUG_PRINT3 ("EXECUTING on_failure_jump %d (to %p):\n",
5776 mcnt, p + mcnt);
5777
5778 PUSH_FAILURE_POINT (p -3, d);
5779 break;
5780
5781 /* This operation is used for greedy *.
5782 Compare the beginning of the repeat with what in the
5783 pattern follows its end. If we can establish that there
5784 is nothing that they would both match, i.e., that we
5785 would have to backtrack because of (as in, e.g., `a*a')
5786 then we can use a non-backtracking loop based on
5787 on_failure_keep_string_jump instead of on_failure_jump. */
5788 case on_failure_jump_smart:
5789 IMMEDIATE_QUIT_CHECK;
5790 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5791 DEBUG_PRINT3 ("EXECUTING on_failure_jump_smart %d (to %p).\n",
5792 mcnt, p + mcnt);
5793 {
5794 re_char *p1 = p; /* Next operation. */
5795 /* Here, we discard `const', making re_match non-reentrant. */
5796 unsigned char *p2 = (unsigned char*) p + mcnt; /* Jump dest. */
5797 unsigned char *p3 = (unsigned char*) p - 3; /* opcode location. */
5798
5799 p -= 3; /* Reset so that we will re-execute the
5800 instruction once it's been changed. */
5801
5802 EXTRACT_NUMBER (mcnt, p2 - 2);
5803
5804 /* Ensure this is a indeed the trivial kind of loop
5805 we are expecting. */
5806 assert (skip_one_char (p1) == p2 - 3);
5807 assert ((re_opcode_t) p2[-3] == jump && p2 + mcnt == p);
5808 DEBUG_STATEMENT (debug += 2);
5809 if (mutually_exclusive_p (bufp, p1, p2))
5810 {
5811 /* Use a fast `on_failure_keep_string_jump' loop. */
5812 DEBUG_PRINT1 (" smart exclusive => fast loop.\n");
5813 *p3 = (unsigned char) on_failure_keep_string_jump;
5814 STORE_NUMBER (p2 - 2, mcnt + 3);
5815 }
5816 else
5817 {
5818 /* Default to a safe `on_failure_jump' loop. */
5819 DEBUG_PRINT1 (" smart default => slow loop.\n");
5820 *p3 = (unsigned char) on_failure_jump;
5821 }
5822 DEBUG_STATEMENT (debug -= 2);
5823 }
5824 break;
5825
5826 /* Unconditionally jump (without popping any failure points). */
5827 case jump:
5828 unconditional_jump:
5829 IMMEDIATE_QUIT_CHECK;
5830 EXTRACT_NUMBER_AND_INCR (mcnt, p); /* Get the amount to jump. */
5831 DEBUG_PRINT2 ("EXECUTING jump %d ", mcnt);
5832 p += mcnt; /* Do the jump. */
5833 DEBUG_PRINT2 ("(to %p).\n", p);
5834 break;
5835
5836
5837 /* Have to succeed matching what follows at least n times.
5838 After that, handle like `on_failure_jump'. */
5839 case succeed_n:
5840 /* Signedness doesn't matter since we only compare MCNT to 0. */
5841 EXTRACT_NUMBER (mcnt, p + 2);
5842 DEBUG_PRINT2 ("EXECUTING succeed_n %d.\n", mcnt);
5843
5844 /* Originally, mcnt is how many times we HAVE to succeed. */
5845 if (mcnt != 0)
5846 {
5847 /* Here, we discard `const', making re_match non-reentrant. */
5848 unsigned char *p2 = (unsigned char*) p + 2; /* counter loc. */
5849 mcnt--;
5850 p += 4;
5851 PUSH_NUMBER (p2, mcnt);
5852 }
5853 else
5854 /* The two bytes encoding mcnt == 0 are two no_op opcodes. */
5855 goto on_failure;
5856 break;
5857
5858 case jump_n:
5859 /* Signedness doesn't matter since we only compare MCNT to 0. */
5860 EXTRACT_NUMBER (mcnt, p + 2);
5861 DEBUG_PRINT2 ("EXECUTING jump_n %d.\n", mcnt);
5862
5863 /* Originally, this is how many times we CAN jump. */
5864 if (mcnt != 0)
5865 {
5866 /* Here, we discard `const', making re_match non-reentrant. */
5867 unsigned char *p2 = (unsigned char*) p + 2; /* counter loc. */
5868 mcnt--;
5869 PUSH_NUMBER (p2, mcnt);
5870 goto unconditional_jump;
5871 }
5872 /* If don't have to jump any more, skip over the rest of command. */
5873 else
5874 p += 4;
5875 break;
5876
5877 case set_number_at:
5878 {
5879 unsigned char *p2; /* Location of the counter. */
5880 DEBUG_PRINT1 ("EXECUTING set_number_at.\n");
5881
5882 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5883 /* Here, we discard `const', making re_match non-reentrant. */
5884 p2 = (unsigned char*) p + mcnt;
5885 /* Signedness doesn't matter since we only copy MCNT's bits . */
5886 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5887 DEBUG_PRINT3 (" Setting %p to %d.\n", p2, mcnt);
5888 PUSH_NUMBER (p2, mcnt);
5889 break;
5890 }
5891
5892 case wordbound:
5893 case notwordbound:
5894 not = (re_opcode_t) *(p - 1) == notwordbound;
5895 DEBUG_PRINT2 ("EXECUTING %swordbound.\n", not?"not":"");
5896
5897 /* We SUCCEED (or FAIL) in one of the following cases: */
5898
5899 /* Case 1: D is at the beginning or the end of string. */
5900 if (AT_STRINGS_BEG (d) || AT_STRINGS_END (d))
5901 not = !not;
5902 else
5903 {
5904 /* C1 is the character before D, S1 is the syntax of C1, C2
5905 is the character at D, and S2 is the syntax of C2. */
5906 re_wchar_t c1, c2;
5907 int s1, s2;
5908 int dummy;
5909 #ifdef emacs
5910 int offset = PTR_TO_OFFSET (d - 1);
5911 int charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
5912 UPDATE_SYNTAX_TABLE (charpos);
5913 #endif
5914 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
5915 s1 = SYNTAX (c1);
5916 #ifdef emacs
5917 UPDATE_SYNTAX_TABLE_FORWARD (charpos + 1);
5918 #endif
5919 PREFETCH_NOLIMIT ();
5920 GET_CHAR_AFTER (c2, d, dummy);
5921 s2 = SYNTAX (c2);
5922
5923 if (/* Case 2: Only one of S1 and S2 is Sword. */
5924 ((s1 == Sword) != (s2 == Sword))
5925 /* Case 3: Both of S1 and S2 are Sword, and macro
5926 WORD_BOUNDARY_P (C1, C2) returns nonzero. */
5927 || ((s1 == Sword) && WORD_BOUNDARY_P (c1, c2)))
5928 not = !not;
5929 }
5930 if (not)
5931 break;
5932 else
5933 goto fail;
5934
5935 case wordbeg:
5936 DEBUG_PRINT1 ("EXECUTING wordbeg.\n");
5937
5938 /* We FAIL in one of the following cases: */
5939
5940 /* Case 1: D is at the end of string. */
5941 if (AT_STRINGS_END (d))
5942 goto fail;
5943 else
5944 {
5945 /* C1 is the character before D, S1 is the syntax of C1, C2
5946 is the character at D, and S2 is the syntax of C2. */
5947 re_wchar_t c1, c2;
5948 int s1, s2;
5949 int dummy;
5950 #ifdef emacs
5951 int offset = PTR_TO_OFFSET (d);
5952 int charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
5953 UPDATE_SYNTAX_TABLE (charpos);
5954 #endif
5955 PREFETCH ();
5956 GET_CHAR_AFTER (c2, d, dummy);
5957 s2 = SYNTAX (c2);
5958
5959 /* Case 2: S2 is not Sword. */
5960 if (s2 != Sword)
5961 goto fail;
5962
5963 /* Case 3: D is not at the beginning of string ... */
5964 if (!AT_STRINGS_BEG (d))
5965 {
5966 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
5967 #ifdef emacs
5968 UPDATE_SYNTAX_TABLE_BACKWARD (charpos - 1);
5969 #endif
5970 s1 = SYNTAX (c1);
5971
5972 /* ... and S1 is Sword, and WORD_BOUNDARY_P (C1, C2)
5973 returns 0. */
5974 if ((s1 == Sword) && !WORD_BOUNDARY_P (c1, c2))
5975 goto fail;
5976 }
5977 }
5978 break;
5979
5980 case wordend:
5981 DEBUG_PRINT1 ("EXECUTING wordend.\n");
5982
5983 /* We FAIL in one of the following cases: */
5984
5985 /* Case 1: D is at the beginning of string. */
5986 if (AT_STRINGS_BEG (d))
5987 goto fail;
5988 else
5989 {
5990 /* C1 is the character before D, S1 is the syntax of C1, C2
5991 is the character at D, and S2 is the syntax of C2. */
5992 re_wchar_t c1, c2;
5993 int s1, s2;
5994 int dummy;
5995 #ifdef emacs
5996 int offset = PTR_TO_OFFSET (d) - 1;
5997 int charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
5998 UPDATE_SYNTAX_TABLE (charpos);
5999 #endif
6000 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
6001 s1 = SYNTAX (c1);
6002
6003 /* Case 2: S1 is not Sword. */
6004 if (s1 != Sword)
6005 goto fail;
6006
6007 /* Case 3: D is not at the end of string ... */
6008 if (!AT_STRINGS_END (d))
6009 {
6010 PREFETCH_NOLIMIT ();
6011 GET_CHAR_AFTER (c2, d, dummy);
6012 #ifdef emacs
6013 UPDATE_SYNTAX_TABLE_FORWARD (charpos);
6014 #endif
6015 s2 = SYNTAX (c2);
6016
6017 /* ... and S2 is Sword, and WORD_BOUNDARY_P (C1, C2)
6018 returns 0. */
6019 if ((s2 == Sword) && !WORD_BOUNDARY_P (c1, c2))
6020 goto fail;
6021 }
6022 }
6023 break;
6024
6025 case symbeg:
6026 DEBUG_PRINT1 ("EXECUTING symbeg.\n");
6027
6028 /* We FAIL in one of the following cases: */
6029
6030 /* Case 1: D is at the end of string. */
6031 if (AT_STRINGS_END (d))
6032 goto fail;
6033 else
6034 {
6035 /* C1 is the character before D, S1 is the syntax of C1, C2
6036 is the character at D, and S2 is the syntax of C2. */
6037 re_wchar_t c1, c2;
6038 int s1, s2;
6039 #ifdef emacs
6040 int offset = PTR_TO_OFFSET (d);
6041 int charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
6042 UPDATE_SYNTAX_TABLE (charpos);
6043 #endif
6044 PREFETCH ();
6045 c2 = RE_STRING_CHAR (d, dend - d);
6046 s2 = SYNTAX (c2);
6047
6048 /* Case 2: S2 is neither Sword nor Ssymbol. */
6049 if (s2 != Sword && s2 != Ssymbol)
6050 goto fail;
6051
6052 /* Case 3: D is not at the beginning of string ... */
6053 if (!AT_STRINGS_BEG (d))
6054 {
6055 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
6056 #ifdef emacs
6057 UPDATE_SYNTAX_TABLE_BACKWARD (charpos - 1);
6058 #endif
6059 s1 = SYNTAX (c1);
6060
6061 /* ... and S1 is Sword or Ssymbol. */
6062 if (s1 == Sword || s1 == Ssymbol)
6063 goto fail;
6064 }
6065 }
6066 break;
6067
6068 case symend:
6069 DEBUG_PRINT1 ("EXECUTING symend.\n");
6070
6071 /* We FAIL in one of the following cases: */
6072
6073 /* Case 1: D is at the beginning of string. */
6074 if (AT_STRINGS_BEG (d))
6075 goto fail;
6076 else
6077 {
6078 /* C1 is the character before D, S1 is the syntax of C1, C2
6079 is the character at D, and S2 is the syntax of C2. */
6080 re_wchar_t c1, c2;
6081 int s1, s2;
6082 #ifdef emacs
6083 int offset = PTR_TO_OFFSET (d) - 1;
6084 int charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
6085 UPDATE_SYNTAX_TABLE (charpos);
6086 #endif
6087 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
6088 s1 = SYNTAX (c1);
6089
6090 /* Case 2: S1 is neither Ssymbol nor Sword. */
6091 if (s1 != Sword && s1 != Ssymbol)
6092 goto fail;
6093
6094 /* Case 3: D is not at the end of string ... */
6095 if (!AT_STRINGS_END (d))
6096 {
6097 PREFETCH_NOLIMIT ();
6098 c2 = RE_STRING_CHAR (d, dend - d);
6099 #ifdef emacs
6100 UPDATE_SYNTAX_TABLE_FORWARD (charpos + 1);
6101 #endif
6102 s2 = SYNTAX (c2);
6103
6104 /* ... and S2 is Sword or Ssymbol. */
6105 if (s2 == Sword || s2 == Ssymbol)
6106 goto fail;
6107 }
6108 }
6109 break;
6110
6111 case syntaxspec:
6112 case notsyntaxspec:
6113 not = (re_opcode_t) *(p - 1) == notsyntaxspec;
6114 mcnt = *p++;
6115 DEBUG_PRINT3 ("EXECUTING %ssyntaxspec %d.\n", not?"not":"", mcnt);
6116 PREFETCH ();
6117 #ifdef emacs
6118 {
6119 int offset = PTR_TO_OFFSET (d);
6120 int pos1 = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
6121 UPDATE_SYNTAX_TABLE (pos1);
6122 }
6123 #endif
6124 {
6125 int len;
6126 re_wchar_t c;
6127
6128 GET_CHAR_AFTER (c, d, len);
6129 if ((SYNTAX (c) != (enum syntaxcode) mcnt) ^ not)
6130 goto fail;
6131 d += len;
6132 }
6133 break;
6134
6135 #ifdef emacs
6136 case before_dot:
6137 DEBUG_PRINT1 ("EXECUTING before_dot.\n");
6138 if (PTR_BYTE_POS (d) >= PT_BYTE)
6139 goto fail;
6140 break;
6141
6142 case at_dot:
6143 DEBUG_PRINT1 ("EXECUTING at_dot.\n");
6144 if (PTR_BYTE_POS (d) != PT_BYTE)
6145 goto fail;
6146 break;
6147
6148 case after_dot:
6149 DEBUG_PRINT1 ("EXECUTING after_dot.\n");
6150 if (PTR_BYTE_POS (d) <= PT_BYTE)
6151 goto fail;
6152 break;
6153
6154 case categoryspec:
6155 case notcategoryspec:
6156 not = (re_opcode_t) *(p - 1) == notcategoryspec;
6157 mcnt = *p++;
6158 DEBUG_PRINT3 ("EXECUTING %scategoryspec %d.\n", not?"not":"", mcnt);
6159 PREFETCH ();
6160 {
6161 int len;
6162 re_wchar_t c;
6163
6164 GET_CHAR_AFTER (c, d, len);
6165 if ((!CHAR_HAS_CATEGORY (c, mcnt)) ^ not)
6166 goto fail;
6167 d += len;
6168 }
6169 break;
6170
6171 #endif /* emacs */
6172
6173 default:
6174 abort ();
6175 }
6176 continue; /* Successfully executed one pattern command; keep going. */
6177
6178
6179 /* We goto here if a matching operation fails. */
6180 fail:
6181 IMMEDIATE_QUIT_CHECK;
6182 if (!FAIL_STACK_EMPTY ())
6183 {
6184 re_char *str, *pat;
6185 /* A restart point is known. Restore to that state. */
6186 DEBUG_PRINT1 ("\nFAIL:\n");
6187 POP_FAILURE_POINT (str, pat);
6188 switch (SWITCH_ENUM_CAST ((re_opcode_t) *pat++))
6189 {
6190 case on_failure_keep_string_jump:
6191 assert (str == NULL);
6192 goto continue_failure_jump;
6193
6194 case on_failure_jump_nastyloop:
6195 assert ((re_opcode_t)pat[-2] == no_op);
6196 PUSH_FAILURE_POINT (pat - 2, str);
6197 /* Fallthrough */
6198
6199 case on_failure_jump_loop:
6200 case on_failure_jump:
6201 case succeed_n:
6202 d = str;
6203 continue_failure_jump:
6204 EXTRACT_NUMBER_AND_INCR (mcnt, pat);
6205 p = pat + mcnt;
6206 break;
6207
6208 case no_op:
6209 /* A special frame used for nastyloops. */
6210 goto fail;
6211
6212 default:
6213 abort();
6214 }
6215
6216 assert (p >= bufp->buffer && p <= pend);
6217
6218 if (d >= string1 && d <= end1)
6219 dend = end_match_1;
6220 }
6221 else
6222 break; /* Matching at this starting point really fails. */
6223 } /* for (;;) */
6224
6225 if (best_regs_set)
6226 goto restore_best_regs;
6227
6228 FREE_VARIABLES ();
6229
6230 return -1; /* Failure to match. */
6231 } /* re_match_2 */
6232 \f
6233 /* Subroutine definitions for re_match_2. */
6234
6235 /* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
6236 bytes; nonzero otherwise. */
6237
6238 static int
6239 bcmp_translate (s1, s2, len, translate, multibyte)
6240 re_char *s1, *s2;
6241 register int len;
6242 RE_TRANSLATE_TYPE translate;
6243 const int multibyte;
6244 {
6245 register re_char *p1 = s1, *p2 = s2;
6246 re_char *p1_end = s1 + len;
6247 re_char *p2_end = s2 + len;
6248
6249 /* FIXME: Checking both p1 and p2 presumes that the two strings might have
6250 different lengths, but relying on a single `len' would break this. -sm */
6251 while (p1 < p1_end && p2 < p2_end)
6252 {
6253 int p1_charlen, p2_charlen;
6254 re_wchar_t p1_ch, p2_ch;
6255
6256 GET_CHAR_AFTER (p1_ch, p1, p1_charlen);
6257 GET_CHAR_AFTER (p2_ch, p2, p2_charlen);
6258
6259 if (RE_TRANSLATE (translate, p1_ch)
6260 != RE_TRANSLATE (translate, p2_ch))
6261 return 1;
6262
6263 p1 += p1_charlen, p2 += p2_charlen;
6264 }
6265
6266 if (p1 != p1_end || p2 != p2_end)
6267 return 1;
6268
6269 return 0;
6270 }
6271 \f
6272 /* Entry points for GNU code. */
6273
6274 /* re_compile_pattern is the GNU regular expression compiler: it
6275 compiles PATTERN (of length SIZE) and puts the result in BUFP.
6276 Returns 0 if the pattern was valid, otherwise an error string.
6277
6278 Assumes the `allocated' (and perhaps `buffer') and `translate' fields
6279 are set in BUFP on entry.
6280
6281 We call regex_compile to do the actual compilation. */
6282
6283 const char *
6284 re_compile_pattern (pattern, length, bufp)
6285 const char *pattern;
6286 size_t length;
6287 struct re_pattern_buffer *bufp;
6288 {
6289 reg_errcode_t ret;
6290
6291 /* GNU code is written to assume at least RE_NREGS registers will be set
6292 (and at least one extra will be -1). */
6293 bufp->regs_allocated = REGS_UNALLOCATED;
6294
6295 /* And GNU code determines whether or not to get register information
6296 by passing null for the REGS argument to re_match, etc., not by
6297 setting no_sub. */
6298 bufp->no_sub = 0;
6299
6300 ret = regex_compile ((re_char*) pattern, length, re_syntax_options, bufp);
6301
6302 if (!ret)
6303 return NULL;
6304 return gettext (re_error_msgid[(int) ret]);
6305 }
6306 WEAK_ALIAS (__re_compile_pattern, re_compile_pattern)
6307 \f
6308 /* Entry points compatible with 4.2 BSD regex library. We don't define
6309 them unless specifically requested. */
6310
6311 #if defined _REGEX_RE_COMP || defined _LIBC
6312
6313 /* BSD has one and only one pattern buffer. */
6314 static struct re_pattern_buffer re_comp_buf;
6315
6316 char *
6317 # ifdef _LIBC
6318 /* Make these definitions weak in libc, so POSIX programs can redefine
6319 these names if they don't use our functions, and still use
6320 regcomp/regexec below without link errors. */
6321 weak_function
6322 # endif
6323 re_comp (s)
6324 const char *s;
6325 {
6326 reg_errcode_t ret;
6327
6328 if (!s)
6329 {
6330 if (!re_comp_buf.buffer)
6331 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6332 return (char *) gettext ("No previous regular expression");
6333 return 0;
6334 }
6335
6336 if (!re_comp_buf.buffer)
6337 {
6338 re_comp_buf.buffer = (unsigned char *) malloc (200);
6339 if (re_comp_buf.buffer == NULL)
6340 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6341 return (char *) gettext (re_error_msgid[(int) REG_ESPACE]);
6342 re_comp_buf.allocated = 200;
6343
6344 re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH);
6345 if (re_comp_buf.fastmap == NULL)
6346 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6347 return (char *) gettext (re_error_msgid[(int) REG_ESPACE]);
6348 }
6349
6350 /* Since `re_exec' always passes NULL for the `regs' argument, we
6351 don't need to initialize the pattern buffer fields which affect it. */
6352
6353 ret = regex_compile (s, strlen (s), re_syntax_options, &re_comp_buf);
6354
6355 if (!ret)
6356 return NULL;
6357
6358 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
6359 return (char *) gettext (re_error_msgid[(int) ret]);
6360 }
6361
6362
6363 int
6364 # ifdef _LIBC
6365 weak_function
6366 # endif
6367 re_exec (s)
6368 const char *s;
6369 {
6370 const int len = strlen (s);
6371 return
6372 0 <= re_search (&re_comp_buf, s, len, 0, len, (struct re_registers *) 0);
6373 }
6374 #endif /* _REGEX_RE_COMP */
6375 \f
6376 /* POSIX.2 functions. Don't define these for Emacs. */
6377
6378 #ifndef emacs
6379
6380 /* regcomp takes a regular expression as a string and compiles it.
6381
6382 PREG is a regex_t *. We do not expect any fields to be initialized,
6383 since POSIX says we shouldn't. Thus, we set
6384
6385 `buffer' to the compiled pattern;
6386 `used' to the length of the compiled pattern;
6387 `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
6388 REG_EXTENDED bit in CFLAGS is set; otherwise, to
6389 RE_SYNTAX_POSIX_BASIC;
6390 `fastmap' to an allocated space for the fastmap;
6391 `fastmap_accurate' to zero;
6392 `re_nsub' to the number of subexpressions in PATTERN.
6393
6394 PATTERN is the address of the pattern string.
6395
6396 CFLAGS is a series of bits which affect compilation.
6397
6398 If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
6399 use POSIX basic syntax.
6400
6401 If REG_NEWLINE is set, then . and [^...] don't match newline.
6402 Also, regexec will try a match beginning after every newline.
6403
6404 If REG_ICASE is set, then we considers upper- and lowercase
6405 versions of letters to be equivalent when matching.
6406
6407 If REG_NOSUB is set, then when PREG is passed to regexec, that
6408 routine will report only success or failure, and nothing about the
6409 registers.
6410
6411 It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
6412 the return codes and their meanings.) */
6413
6414 int
6415 regcomp (preg, pattern, cflags)
6416 regex_t *__restrict preg;
6417 const char *__restrict pattern;
6418 int cflags;
6419 {
6420 reg_errcode_t ret;
6421 reg_syntax_t syntax
6422 = (cflags & REG_EXTENDED) ?
6423 RE_SYNTAX_POSIX_EXTENDED : RE_SYNTAX_POSIX_BASIC;
6424
6425 /* regex_compile will allocate the space for the compiled pattern. */
6426 preg->buffer = 0;
6427 preg->allocated = 0;
6428 preg->used = 0;
6429
6430 /* Try to allocate space for the fastmap. */
6431 preg->fastmap = (char *) malloc (1 << BYTEWIDTH);
6432
6433 if (cflags & REG_ICASE)
6434 {
6435 unsigned i;
6436
6437 preg->translate
6438 = (RE_TRANSLATE_TYPE) malloc (CHAR_SET_SIZE
6439 * sizeof (*(RE_TRANSLATE_TYPE)0));
6440 if (preg->translate == NULL)
6441 return (int) REG_ESPACE;
6442
6443 /* Map uppercase characters to corresponding lowercase ones. */
6444 for (i = 0; i < CHAR_SET_SIZE; i++)
6445 preg->translate[i] = ISUPPER (i) ? TOLOWER (i) : i;
6446 }
6447 else
6448 preg->translate = NULL;
6449
6450 /* If REG_NEWLINE is set, newlines are treated differently. */
6451 if (cflags & REG_NEWLINE)
6452 { /* REG_NEWLINE implies neither . nor [^...] match newline. */
6453 syntax &= ~RE_DOT_NEWLINE;
6454 syntax |= RE_HAT_LISTS_NOT_NEWLINE;
6455 }
6456 else
6457 syntax |= RE_NO_NEWLINE_ANCHOR;
6458
6459 preg->no_sub = !!(cflags & REG_NOSUB);
6460
6461 /* POSIX says a null character in the pattern terminates it, so we
6462 can use strlen here in compiling the pattern. */
6463 ret = regex_compile ((re_char*) pattern, strlen (pattern), syntax, preg);
6464
6465 /* POSIX doesn't distinguish between an unmatched open-group and an
6466 unmatched close-group: both are REG_EPAREN. */
6467 if (ret == REG_ERPAREN)
6468 ret = REG_EPAREN;
6469
6470 if (ret == REG_NOERROR && preg->fastmap)
6471 { /* Compute the fastmap now, since regexec cannot modify the pattern
6472 buffer. */
6473 re_compile_fastmap (preg);
6474 if (preg->can_be_null)
6475 { /* The fastmap can't be used anyway. */
6476 free (preg->fastmap);
6477 preg->fastmap = NULL;
6478 }
6479 }
6480 return (int) ret;
6481 }
6482 WEAK_ALIAS (__regcomp, regcomp)
6483
6484
6485 /* regexec searches for a given pattern, specified by PREG, in the
6486 string STRING.
6487
6488 If NMATCH is zero or REG_NOSUB was set in the cflags argument to
6489 `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
6490 least NMATCH elements, and we set them to the offsets of the
6491 corresponding matched substrings.
6492
6493 EFLAGS specifies `execution flags' which affect matching: if
6494 REG_NOTBOL is set, then ^ does not match at the beginning of the
6495 string; if REG_NOTEOL is set, then $ does not match at the end.
6496
6497 We return 0 if we find a match and REG_NOMATCH if not. */
6498
6499 int
6500 regexec (preg, string, nmatch, pmatch, eflags)
6501 const regex_t *__restrict preg;
6502 const char *__restrict string;
6503 size_t nmatch;
6504 regmatch_t pmatch[__restrict_arr];
6505 int eflags;
6506 {
6507 int ret;
6508 struct re_registers regs;
6509 regex_t private_preg;
6510 int len = strlen (string);
6511 boolean want_reg_info = !preg->no_sub && nmatch > 0 && pmatch;
6512
6513 private_preg = *preg;
6514
6515 private_preg.not_bol = !!(eflags & REG_NOTBOL);
6516 private_preg.not_eol = !!(eflags & REG_NOTEOL);
6517
6518 /* The user has told us exactly how many registers to return
6519 information about, via `nmatch'. We have to pass that on to the
6520 matching routines. */
6521 private_preg.regs_allocated = REGS_FIXED;
6522
6523 if (want_reg_info)
6524 {
6525 regs.num_regs = nmatch;
6526 regs.start = TALLOC (nmatch * 2, regoff_t);
6527 if (regs.start == NULL)
6528 return (int) REG_NOMATCH;
6529 regs.end = regs.start + nmatch;
6530 }
6531
6532 /* Instead of using not_eol to implement REG_NOTEOL, we could simply
6533 pass (&private_preg, string, len + 1, 0, len, ...) pretending the string
6534 was a little bit longer but still only matching the real part.
6535 This works because the `endline' will check for a '\n' and will find a
6536 '\0', correctly deciding that this is not the end of a line.
6537 But it doesn't work out so nicely for REG_NOTBOL, since we don't have
6538 a convenient '\0' there. For all we know, the string could be preceded
6539 by '\n' which would throw things off. */
6540
6541 /* Perform the searching operation. */
6542 ret = re_search (&private_preg, string, len,
6543 /* start: */ 0, /* range: */ len,
6544 want_reg_info ? &regs : (struct re_registers *) 0);
6545
6546 /* Copy the register information to the POSIX structure. */
6547 if (want_reg_info)
6548 {
6549 if (ret >= 0)
6550 {
6551 unsigned r;
6552
6553 for (r = 0; r < nmatch; r++)
6554 {
6555 pmatch[r].rm_so = regs.start[r];
6556 pmatch[r].rm_eo = regs.end[r];
6557 }
6558 }
6559
6560 /* If we needed the temporary register info, free the space now. */
6561 free (regs.start);
6562 }
6563
6564 /* We want zero return to mean success, unlike `re_search'. */
6565 return ret >= 0 ? (int) REG_NOERROR : (int) REG_NOMATCH;
6566 }
6567 WEAK_ALIAS (__regexec, regexec)
6568
6569
6570 /* Returns a message corresponding to an error code, ERRCODE, returned
6571 from either regcomp or regexec. We don't use PREG here. */
6572
6573 size_t
6574 regerror (errcode, preg, errbuf, errbuf_size)
6575 int errcode;
6576 const regex_t *preg;
6577 char *errbuf;
6578 size_t errbuf_size;
6579 {
6580 const char *msg;
6581 size_t msg_size;
6582
6583 if (errcode < 0
6584 || errcode >= (sizeof (re_error_msgid) / sizeof (re_error_msgid[0])))
6585 /* Only error codes returned by the rest of the code should be passed
6586 to this routine. If we are given anything else, or if other regex
6587 code generates an invalid error code, then the program has a bug.
6588 Dump core so we can fix it. */
6589 abort ();
6590
6591 msg = gettext (re_error_msgid[errcode]);
6592
6593 msg_size = strlen (msg) + 1; /* Includes the null. */
6594
6595 if (errbuf_size != 0)
6596 {
6597 if (msg_size > errbuf_size)
6598 {
6599 strncpy (errbuf, msg, errbuf_size - 1);
6600 errbuf[errbuf_size - 1] = 0;
6601 }
6602 else
6603 strcpy (errbuf, msg);
6604 }
6605
6606 return msg_size;
6607 }
6608 WEAK_ALIAS (__regerror, regerror)
6609
6610
6611 /* Free dynamically allocated space used by PREG. */
6612
6613 void
6614 regfree (preg)
6615 regex_t *preg;
6616 {
6617 if (preg->buffer != NULL)
6618 free (preg->buffer);
6619 preg->buffer = NULL;
6620
6621 preg->allocated = 0;
6622 preg->used = 0;
6623
6624 if (preg->fastmap != NULL)
6625 free (preg->fastmap);
6626 preg->fastmap = NULL;
6627 preg->fastmap_accurate = 0;
6628
6629 if (preg->translate != NULL)
6630 free (preg->translate);
6631 preg->translate = NULL;
6632 }
6633 WEAK_ALIAS (__regfree, regfree)
6634
6635 #endif /* not emacs */
6636
6637 /* arch-tag: 4ffd68ba-2a9e-435b-a21a-018990f9eeb2
6638 (do not change this comment) */