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