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