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