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