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