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