1 /* Extended regular expression matching and search library,
3 (Implements POSIX draft P1003.2/D11.2, except for some of the
4 internationalization features.)
5 Copyright (C) 1993-1999, 2000, 2001, 2002 Free Software Foundation, Inc.
6 This file is part of the GNU C Library.
8 The GNU C Library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
13 The GNU C Library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with the GNU C Library; if not, write to the Free
20 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 /* This file has been modified for usage in libiberty. It includes "xregex.h"
24 instead of <regex.h>. The "xregex.h" header file renames all external
25 routines with an "x" prefix so they do not collide with the native regex
26 routines or with other components regex routines. */
27 /* AIX requires this to be the first thing in the file. */
28 #if defined _AIX && !defined __GNUC__ && !defined REGEX_MALLOC
35 #ifndef INSIDE_RECURSION
43 #ifndef INSIDE_RECURSION
45 # if defined STDC_HEADERS && !defined emacs
48 /* We need this for `regex.h', and perhaps for the Emacs include files. */
49 # include <sys/types.h>
52 # define WIDE_CHAR_SUPPORT (HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_BTOWC)
54 /* For platform which support the ISO C amendement 1 functionality we
55 support user defined character classes. */
56 # if defined _LIBC || WIDE_CHAR_SUPPORT
57 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
63 /* We have to keep the namespace clean. */
64 # define regfree(preg) __regfree (preg)
65 # define regexec(pr, st, nm, pm, ef) __regexec (pr, st, nm, pm, ef)
66 # define regcomp(preg, pattern, cflags) __regcomp (preg, pattern, cflags)
67 # define regerror(errcode, preg, errbuf, errbuf_size) \
68 __regerror(errcode, preg, errbuf, errbuf_size)
69 # define re_set_registers(bu, re, nu, st, en) \
70 __re_set_registers (bu, re, nu, st, en)
71 # define re_match_2(bufp, string1, size1, string2, size2, pos, regs, stop) \
72 __re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
73 # define re_match(bufp, string, size, pos, regs) \
74 __re_match (bufp, string, size, pos, regs)
75 # define re_search(bufp, string, size, startpos, range, regs) \
76 __re_search (bufp, string, size, startpos, range, regs)
77 # define re_compile_pattern(pattern, length, bufp) \
78 __re_compile_pattern (pattern, length, bufp)
79 # define re_set_syntax(syntax) __re_set_syntax (syntax)
80 # define re_search_2(bufp, st1, s1, st2, s2, startpos, range, regs, stop) \
81 __re_search_2 (bufp, st1, s1, st2, s2, startpos, range, regs, stop)
82 # define re_compile_fastmap(bufp) __re_compile_fastmap (bufp)
84 # define btowc __btowc
86 /* We are also using some library internals. */
87 # include <locale/localeinfo.h>
88 # include <locale/elem-hash.h>
89 # include <langinfo.h>
90 # include <locale/coll-lookup.h>
93 /* This is for other GNU distributions with internationalized messages. */
94 # if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
98 # define gettext(msgid) __dcgettext ("libc", msgid, LC_MESSAGES)
101 # define gettext(msgid) (msgid)
104 # ifndef gettext_noop
105 /* This define is so xgettext can find the internationalizable
107 # define gettext_noop(String) String
110 /* The `emacs' switch turns on certain matching commands
111 that make sense only in Emacs. */
118 # else /* not emacs */
120 /* If we are not linking with Emacs proper,
121 we can't use the relocating allocator
122 even if config.h says that we can. */
125 # if defined STDC_HEADERS || defined _LIBC
132 /* When used in Emacs's lib-src, we need to get bzero and bcopy somehow.
133 If nothing else has been done, use the method below. */
134 # ifdef INHIBIT_STRING_HEADER
135 # if !(defined HAVE_BZERO && defined HAVE_BCOPY)
136 # if !defined bzero && !defined bcopy
137 # undef INHIBIT_STRING_HEADER
142 /* This is the normal way of making sure we have a bcopy and a bzero.
143 This is used in most programs--a few other programs avoid this
144 by defining INHIBIT_STRING_HEADER. */
145 # ifndef INHIBIT_STRING_HEADER
146 # if defined HAVE_STRING_H || defined STDC_HEADERS || defined _LIBC
150 # define bzero(s, n) (memset (s, '\0', n), (s))
152 # define bzero(s, n) __bzero (s, n)
156 # include <strings.h>
158 # define memcmp(s1, s2, n) bcmp (s1, s2, n)
161 # define memcpy(d, s, n) (bcopy (s, d, n), (d))
166 /* Define the syntax stuff for \<, \>, etc. */
168 /* This must be nonzero for the wordchar and notwordchar pattern
169 commands in re_match_2. */
174 # ifdef SWITCH_ENUM_BUG
175 # define SWITCH_ENUM_CAST(x) ((int)(x))
177 # define SWITCH_ENUM_CAST(x) (x)
180 # endif /* not emacs */
182 # if defined _LIBC || HAVE_LIMITS_H
187 # define MB_LEN_MAX 1
190 /* Get the interface, including the syntax bits. */
191 # include "xregex.h" /* change for libiberty */
193 /* isalpha etc. are used for the character classes. */
196 /* Jim Meyering writes:
198 "... Some ctype macros are valid only for character codes that
199 isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
200 using /bin/cc or gcc but without giving an ansi option). So, all
201 ctype uses should be through macros like ISPRINT... If
202 STDC_HEADERS is defined, then autoconf has verified that the ctype
203 macros don't need to be guarded with references to isascii. ...
204 Defining isascii to 1 should let any compiler worth its salt
205 eliminate the && through constant folding."
206 Solaris defines some of these symbols so we must undefine them first. */
209 # if defined STDC_HEADERS || (!defined isascii && !defined HAVE_ISASCII)
210 # define ISASCII(c) 1
212 # define ISASCII(c) isascii(c)
216 # define ISBLANK(c) (ISASCII (c) && isblank (c))
218 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
221 # define ISGRAPH(c) (ISASCII (c) && isgraph (c))
223 # define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
227 # define ISPRINT(c) (ISASCII (c) && isprint (c))
228 # define ISDIGIT(c) (ISASCII (c) && isdigit (c))
229 # define ISALNUM(c) (ISASCII (c) && isalnum (c))
230 # define ISALPHA(c) (ISASCII (c) && isalpha (c))
231 # define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
232 # define ISLOWER(c) (ISASCII (c) && islower (c))
233 # define ISPUNCT(c) (ISASCII (c) && ispunct (c))
234 # define ISSPACE(c) (ISASCII (c) && isspace (c))
235 # define ISUPPER(c) (ISASCII (c) && isupper (c))
236 # define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
239 # define TOLOWER(c) _tolower(c)
241 # define TOLOWER(c) tolower(c)
245 # define NULL (void *)0
248 /* We remove any previous definition of `SIGN_EXTEND_CHAR',
249 since ours (we hope) works properly with all combinations of
250 machines, compilers, `char' and `unsigned char' argument types.
251 (Per Bothner suggested the basic approach.) */
252 # undef SIGN_EXTEND_CHAR
254 # define SIGN_EXTEND_CHAR(c) ((signed char) (c))
255 # else /* not __STDC__ */
256 /* As in Harbison and Steele. */
257 # define SIGN_EXTEND_CHAR(c) ((((unsigned char) (c)) ^ 128) - 128)
261 /* How many characters in the character set. */
262 # define CHAR_SET_SIZE 256
266 extern char *re_syntax_table
;
268 # else /* not SYNTAX_TABLE */
270 static char re_syntax_table
[CHAR_SET_SIZE
];
272 static void init_syntax_once (void);
275 init_syntax_once (void)
282 bzero (re_syntax_table
, sizeof re_syntax_table
);
284 for (c
= 0; c
< CHAR_SET_SIZE
; ++c
)
286 re_syntax_table
[c
] = Sword
;
288 re_syntax_table
['_'] = Sword
;
293 # endif /* not SYNTAX_TABLE */
295 # define SYNTAX(c) re_syntax_table[(unsigned char) (c)]
299 /* Integer type for pointers. */
300 # if !defined _LIBC && !defined HAVE_UINTPTR_T
301 typedef unsigned long int uintptr_t;
304 /* Should we use malloc or alloca? If REGEX_MALLOC is not defined, we
305 use `alloca' instead of `malloc'. This is because using malloc in
306 re_search* or re_match* could cause memory leaks when C-g is used in
307 Emacs; also, malloc is slower and causes storage fragmentation. On
308 the other hand, malloc is more portable, and easier to debug.
310 Because we sometimes use alloca, some routines have to be macros,
311 not functions -- `alloca'-allocated space disappears at the end of the
312 function it is called in. */
316 # define REGEX_ALLOCATE malloc
317 # define REGEX_REALLOCATE(source, osize, nsize) realloc (source, nsize)
318 # define REGEX_FREE free
320 # else /* not REGEX_MALLOC */
322 /* Emacs already defines alloca, sometimes. */
325 /* Make alloca work the best possible way. */
327 # define alloca __builtin_alloca
328 # else /* not __GNUC__ */
331 # endif /* HAVE_ALLOCA_H */
332 # endif /* not __GNUC__ */
334 # endif /* not alloca */
336 # define REGEX_ALLOCATE alloca
338 /* Assumes a `char *destination' variable. */
339 # define REGEX_REALLOCATE(source, osize, nsize) \
340 (destination = (char *) alloca (nsize), \
341 memcpy (destination, source, osize))
343 /* No need to do anything to free, after alloca. */
344 # define REGEX_FREE(arg) ((void)0) /* Do nothing! But inhibit gcc warning. */
346 # endif /* not REGEX_MALLOC */
348 /* Define how to allocate the failure stack. */
350 # if defined REL_ALLOC && defined REGEX_MALLOC
352 # define REGEX_ALLOCATE_STACK(size) \
353 r_alloc (&failure_stack_ptr, (size))
354 # define REGEX_REALLOCATE_STACK(source, osize, nsize) \
355 r_re_alloc (&failure_stack_ptr, (nsize))
356 # define REGEX_FREE_STACK(ptr) \
357 r_alloc_free (&failure_stack_ptr)
359 # else /* not using relocating allocator */
363 # define REGEX_ALLOCATE_STACK malloc
364 # define REGEX_REALLOCATE_STACK(source, osize, nsize) realloc (source, nsize)
365 # define REGEX_FREE_STACK free
367 # else /* not REGEX_MALLOC */
369 # define REGEX_ALLOCATE_STACK alloca
371 # define REGEX_REALLOCATE_STACK(source, osize, nsize) \
372 REGEX_REALLOCATE (source, osize, nsize)
373 /* No need to explicitly free anything. */
374 # define REGEX_FREE_STACK(arg)
376 # endif /* not REGEX_MALLOC */
377 # endif /* not using relocating allocator */
380 /* True if `size1' is non-NULL and PTR is pointing anywhere inside
381 `string1' or just past its end. This works if PTR is NULL, which is
383 # define FIRST_STRING_P(ptr) \
384 (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
386 /* (Re)Allocate N items of type T using malloc, or fail. */
387 # define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t)))
388 # define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
389 # define RETALLOC_IF(addr, n, t) \
390 if (addr) RETALLOC((addr), (n), t); else (addr) = TALLOC ((n), t)
391 # define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
393 # define BYTEWIDTH 8 /* In bits. */
395 # define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
399 # define MAX(a, b) ((a) > (b) ? (a) : (b))
400 # define MIN(a, b) ((a) < (b) ? (a) : (b))
402 typedef char boolean
;
406 static reg_errcode_t
byte_regex_compile (const char *pattern
, size_t size
,
408 struct re_pattern_buffer
*bufp
);
410 static int byte_re_match_2_internal (struct re_pattern_buffer
*bufp
,
411 const char *string1
, int size1
,
412 const char *string2
, int size2
,
414 struct re_registers
*regs
,
416 static int byte_re_search_2 (struct re_pattern_buffer
*bufp
,
417 const char *string1
, int size1
,
418 const char *string2
, int size2
,
419 int startpos
, int range
,
420 struct re_registers
*regs
, int stop
);
421 static int byte_re_compile_fastmap (struct re_pattern_buffer
*bufp
);
424 static reg_errcode_t
wcs_regex_compile (const char *pattern
, size_t size
,
426 struct re_pattern_buffer
*bufp
);
429 static int wcs_re_match_2_internal (struct re_pattern_buffer
*bufp
,
430 const char *cstring1
, int csize1
,
431 const char *cstring2
, int csize2
,
433 struct re_registers
*regs
,
435 wchar_t *string1
, int size1
,
436 wchar_t *string2
, int size2
,
437 int *mbs_offset1
, int *mbs_offset2
);
438 static int wcs_re_search_2 (struct re_pattern_buffer
*bufp
,
439 const char *string1
, int size1
,
440 const char *string2
, int size2
,
441 int startpos
, int range
,
442 struct re_registers
*regs
, int stop
);
443 static int wcs_re_compile_fastmap (struct re_pattern_buffer
*bufp
);
446 /* These are the command codes that appear in compiled regular
447 expressions. Some opcodes are followed by argument bytes. A
448 command code can specify any interpretation whatsoever for its
449 arguments. Zero bytes may appear in the compiled regular expression. */
455 /* Succeed right away--no more backtracking. */
458 /* Followed by one byte giving n, then by n literal bytes. */
462 /* Same as exactn, but contains binary data. */
466 /* Matches any (more or less) character. */
469 /* Matches any one char belonging to specified set. First
470 following byte is number of bitmap bytes. Then come bytes
471 for a bitmap saying which chars are in. Bits in each byte
472 are ordered low-bit-first. A character is in the set if its
473 bit is 1. A character too large to have a bit in the map is
474 automatically not in the set. */
475 /* ifdef MBS_SUPPORT, following element is length of character
476 classes, length of collating symbols, length of equivalence
477 classes, length of character ranges, and length of characters.
478 Next, character class element, collating symbols elements,
479 equivalence class elements, range elements, and character
481 See regex_compile function. */
484 /* Same parameters as charset, but match any character that is
485 not one of those specified. */
488 /* Start remembering the text that is matched, for storing in a
489 register. Followed by one byte with the register number, in
490 the range 0 to one less than the pattern buffer's re_nsub
491 field. Then followed by one byte with the number of groups
492 inner to this one. (This last has to be part of the
493 start_memory only because we need it in the on_failure_jump
497 /* Stop remembering the text that is matched and store it in a
498 memory register. Followed by one byte with the register
499 number, in the range 0 to one less than `re_nsub' in the
500 pattern buffer, and one byte with the number of inner groups,
501 just like `start_memory'. (We need the number of inner
502 groups here because we don't have any easy way of finding the
503 corresponding start_memory when we're at a stop_memory.) */
506 /* Match a duplicate of something remembered. Followed by one
507 byte containing the register number. */
510 /* Fail unless at beginning of line. */
513 /* Fail unless at end of line. */
516 /* Succeeds if at beginning of buffer (if emacs) or at beginning
517 of string to be matched (if not). */
520 /* Analogously, for end of buffer/string. */
523 /* Followed by two byte relative address to which to jump. */
526 /* Same as jump, but marks the end of an alternative. */
529 /* Followed by two-byte relative address of place to resume at
530 in case of failure. */
531 /* ifdef MBS_SUPPORT, the size of address is 1. */
534 /* Like on_failure_jump, but pushes a placeholder instead of the
535 current string position when executed. */
536 on_failure_keep_string_jump
,
538 /* Throw away latest failure point and then jump to following
539 two-byte relative address. */
540 /* ifdef MBS_SUPPORT, the size of address is 1. */
543 /* Change to pop_failure_jump if know won't have to backtrack to
544 match; otherwise change to jump. This is used to jump
545 back to the beginning of a repeat. If what follows this jump
546 clearly won't match what the repeat does, such that we can be
547 sure that there is no use backtracking out of repetitions
548 already matched, then we change it to a pop_failure_jump.
549 Followed by two-byte address. */
550 /* ifdef MBS_SUPPORT, the size of address is 1. */
553 /* Jump to following two-byte address, and push a dummy failure
554 point. This failure point will be thrown away if an attempt
555 is made to use it for a failure. A `+' construct makes this
556 before the first repeat. Also used as an intermediary kind
557 of jump when compiling an alternative. */
558 /* ifdef MBS_SUPPORT, the size of address is 1. */
561 /* Push a dummy failure point and continue. Used at the end of
565 /* Followed by two-byte relative address and two-byte number n.
566 After matching N times, jump to the address upon failure. */
567 /* ifdef MBS_SUPPORT, the size of address is 1. */
570 /* Followed by two-byte relative address, and two-byte number n.
571 Jump to the address N times, then fail. */
572 /* ifdef MBS_SUPPORT, the size of address is 1. */
575 /* Set the following two-byte relative address to the
576 subsequent two-byte number. The address *includes* the two
578 /* ifdef MBS_SUPPORT, the size of address is 1. */
581 wordchar
, /* Matches any word-constituent character. */
582 notwordchar
, /* Matches any char that is not a word-constituent. */
584 wordbeg
, /* Succeeds if at word beginning. */
585 wordend
, /* Succeeds if at word end. */
587 wordbound
, /* Succeeds if at a word boundary. */
588 notwordbound
/* Succeeds if not at a word boundary. */
591 ,before_dot
, /* Succeeds if before point. */
592 at_dot
, /* Succeeds if at point. */
593 after_dot
, /* Succeeds if after point. */
595 /* Matches any character whose syntax is specified. Followed by
596 a byte which contains a syntax code, e.g., Sword. */
599 /* Matches any character whose syntax is not that specified. */
603 #endif /* not INSIDE_RECURSION */
608 # define UCHAR_T unsigned char
609 # define COMPILED_BUFFER_VAR bufp->buffer
610 # define OFFSET_ADDRESS_SIZE 2
611 # define PREFIX(name) byte_##name
612 # define ARG_PREFIX(name) name
613 # define PUT_CHAR(c) putchar (c)
616 # define CHAR_T wchar_t
617 # define UCHAR_T wchar_t
618 # define COMPILED_BUFFER_VAR wc_buffer
619 # define OFFSET_ADDRESS_SIZE 1 /* the size which STORE_NUMBER macro use */
620 # define CHAR_CLASS_SIZE ((__alignof__(wctype_t)+sizeof(wctype_t))/sizeof(CHAR_T)+1)
621 # define PREFIX(name) wcs_##name
622 # define ARG_PREFIX(name) c##name
623 /* Should we use wide stream?? */
624 # define PUT_CHAR(c) printf ("%C", c);
630 # define INSIDE_RECURSION
632 # undef INSIDE_RECURSION
635 # define INSIDE_RECURSION
637 # undef INSIDE_RECURSION
641 #ifdef INSIDE_RECURSION
642 /* Common operations on the compiled pattern. */
644 /* Store NUMBER in two contiguous bytes starting at DESTINATION. */
645 /* ifdef MBS_SUPPORT, we store NUMBER in 1 element. */
648 # define STORE_NUMBER(destination, number) \
650 *(destination) = (UCHAR_T)(number); \
653 # define STORE_NUMBER(destination, number) \
655 (destination)[0] = (number) & 0377; \
656 (destination)[1] = (number) >> 8; \
660 /* Same as STORE_NUMBER, except increment DESTINATION to
661 the byte after where the number is stored. Therefore, DESTINATION
662 must be an lvalue. */
663 /* ifdef MBS_SUPPORT, we store NUMBER in 1 element. */
665 # define STORE_NUMBER_AND_INCR(destination, number) \
667 STORE_NUMBER (destination, number); \
668 (destination) += OFFSET_ADDRESS_SIZE; \
671 /* Put into DESTINATION a number stored in two contiguous bytes starting
673 /* ifdef MBS_SUPPORT, we store NUMBER in 1 element. */
676 # define EXTRACT_NUMBER(destination, source) \
678 (destination) = *(source); \
681 # define EXTRACT_NUMBER(destination, source) \
683 (destination) = *(source) & 0377; \
684 (destination) += SIGN_EXTEND_CHAR (*((source) + 1)) << 8; \
689 static void PREFIX(extract_number
) (int *dest
, UCHAR_T
*source
);
691 PREFIX(extract_number
) (int *dest
, UCHAR_T
*source
)
696 int temp
= SIGN_EXTEND_CHAR (*(source
+ 1));
697 *dest
= *source
& 0377;
702 # ifndef EXTRACT_MACROS /* To debug the macros. */
703 # undef EXTRACT_NUMBER
704 # define EXTRACT_NUMBER(dest, src) PREFIX(extract_number) (&dest, src)
705 # endif /* not EXTRACT_MACROS */
709 /* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
710 SOURCE must be an lvalue. */
712 # define EXTRACT_NUMBER_AND_INCR(destination, source) \
714 EXTRACT_NUMBER (destination, source); \
715 (source) += OFFSET_ADDRESS_SIZE; \
719 static void PREFIX(extract_number_and_incr
) (int *destination
,
722 PREFIX(extract_number_and_incr
) (int *destination
, UCHAR_T
**source
)
724 PREFIX(extract_number
) (destination
, *source
);
725 *source
+= OFFSET_ADDRESS_SIZE
;
728 # ifndef EXTRACT_MACROS
729 # undef EXTRACT_NUMBER_AND_INCR
730 # define EXTRACT_NUMBER_AND_INCR(dest, src) \
731 PREFIX(extract_number_and_incr) (&dest, &src)
732 # endif /* not EXTRACT_MACROS */
738 /* If DEBUG is defined, Regex prints many voluminous messages about what
739 it is doing (if the variable `debug' is nonzero). If linked with the
740 main program in `iregex.c', you can enter patterns and strings
741 interactively. And if linked with the main program in `main.c' and
742 the other test files, you can run the already-written tests. */
746 # ifndef DEFINED_ONCE
748 /* We use standard I/O for debugging. */
751 /* It is useful to test things that ``must'' be true when debugging. */
756 # define DEBUG_STATEMENT(e) e
757 # define DEBUG_PRINT1(x) if (debug) printf (x)
758 # define DEBUG_PRINT2(x1, x2) if (debug) printf (x1, x2)
759 # define DEBUG_PRINT3(x1, x2, x3) if (debug) printf (x1, x2, x3)
760 # define DEBUG_PRINT4(x1, x2, x3, x4) if (debug) printf (x1, x2, x3, x4)
761 # endif /* not DEFINED_ONCE */
763 # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) \
764 if (debug) PREFIX(print_partial_compiled_pattern) (s, e)
765 # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \
766 if (debug) PREFIX(print_double_string) (w, s1, sz1, s2, sz2)
769 /* Print the fastmap in human-readable form. */
771 # ifndef DEFINED_ONCE
773 print_fastmap (char *fastmap
)
775 unsigned was_a_range
= 0;
778 while (i
< (1 << BYTEWIDTH
))
784 while (i
< (1 << BYTEWIDTH
) && fastmap
[i
])
798 # endif /* not DEFINED_ONCE */
801 /* Print a compiled pattern string in human-readable form, starting at
802 the START pointer into it and ending just before the pointer END. */
805 PREFIX(print_partial_compiled_pattern
) (UCHAR_T
*start
, UCHAR_T
*end
)
818 /* Loop over pattern commands. */
822 printf ("%td:\t", p
- start
);
824 printf ("%ld:\t", (long int) (p
- start
));
827 switch ((re_opcode_t
) *p
++)
835 printf ("/exactn/%d", mcnt
);
847 printf ("/exactn_bin/%d", mcnt
);
850 printf("/%lx", (long int) *p
++);
854 # endif /* MBS_SUPPORT */
858 printf ("/start_memory/%d/%ld", mcnt
, (long int) *p
++);
863 printf ("/stop_memory/%d/%ld", mcnt
, (long int) *p
++);
867 printf ("/duplicate/%ld", (long int) *p
++);
880 printf ("/charset [%s",
881 (re_opcode_t
) *(workp
- 1) == charset_not
? "^" : "");
883 length
= *workp
++; /* the length of char_classes */
884 for (i
=0 ; i
<length
; i
++)
885 printf("[:%lx:]", (long int) *p
++);
886 length
= *workp
++; /* the length of collating_symbol */
887 for (i
=0 ; i
<length
;)
891 PUT_CHAR((i
++,*p
++));
895 length
= *workp
++; /* the length of equivalence_class */
896 for (i
=0 ; i
<length
;)
900 PUT_CHAR((i
++,*p
++));
904 length
= *workp
++; /* the length of char_range */
905 for (i
=0 ; i
<length
; i
++)
907 wchar_t range_start
= *p
++;
908 wchar_t range_end
= *p
++;
909 printf("%C-%C", range_start
, range_end
);
911 length
= *workp
++; /* the length of char */
912 for (i
=0 ; i
<length
; i
++)
916 register int c
, last
= -100;
917 register int in_range
= 0;
919 printf ("/charset [%s",
920 (re_opcode_t
) *(p
- 1) == charset_not
? "^" : "");
922 assert (p
+ *p
< pend
);
924 for (c
= 0; c
< 256; c
++)
926 && (p
[1 + (c
/8)] & (1 << (c
% 8))))
928 /* Are we starting a range? */
929 if (last
+ 1 == c
&& ! in_range
)
934 /* Have we broken a range? */
935 else if (last
+ 1 != c
&& in_range
)
965 case on_failure_jump
:
966 PREFIX(extract_number_and_incr
) (&mcnt
, &p
);
968 printf ("/on_failure_jump to %td", p
+ mcnt
- start
);
970 printf ("/on_failure_jump to %ld", (long int) (p
+ mcnt
- start
));
974 case on_failure_keep_string_jump
:
975 PREFIX(extract_number_and_incr
) (&mcnt
, &p
);
977 printf ("/on_failure_keep_string_jump to %td", p
+ mcnt
- start
);
979 printf ("/on_failure_keep_string_jump to %ld",
980 (long int) (p
+ mcnt
- start
));
984 case dummy_failure_jump
:
985 PREFIX(extract_number_and_incr
) (&mcnt
, &p
);
987 printf ("/dummy_failure_jump to %td", p
+ mcnt
- start
);
989 printf ("/dummy_failure_jump to %ld", (long int) (p
+ mcnt
- start
));
993 case push_dummy_failure
:
994 printf ("/push_dummy_failure");
998 PREFIX(extract_number_and_incr
) (&mcnt
, &p
);
1000 printf ("/maybe_pop_jump to %td", p
+ mcnt
- start
);
1002 printf ("/maybe_pop_jump to %ld", (long int) (p
+ mcnt
- start
));
1006 case pop_failure_jump
:
1007 PREFIX(extract_number_and_incr
) (&mcnt
, &p
);
1009 printf ("/pop_failure_jump to %td", p
+ mcnt
- start
);
1011 printf ("/pop_failure_jump to %ld", (long int) (p
+ mcnt
- start
));
1016 PREFIX(extract_number_and_incr
) (&mcnt
, &p
);
1018 printf ("/jump_past_alt to %td", p
+ mcnt
- start
);
1020 printf ("/jump_past_alt to %ld", (long int) (p
+ mcnt
- start
));
1025 PREFIX(extract_number_and_incr
) (&mcnt
, &p
);
1027 printf ("/jump to %td", p
+ mcnt
- start
);
1029 printf ("/jump to %ld", (long int) (p
+ mcnt
- start
));
1034 PREFIX(extract_number_and_incr
) (&mcnt
, &p
);
1036 PREFIX(extract_number_and_incr
) (&mcnt2
, &p
);
1038 printf ("/succeed_n to %td, %d times", p1
- start
, mcnt2
);
1040 printf ("/succeed_n to %ld, %d times",
1041 (long int) (p1
- start
), mcnt2
);
1046 PREFIX(extract_number_and_incr
) (&mcnt
, &p
);
1048 PREFIX(extract_number_and_incr
) (&mcnt2
, &p
);
1049 printf ("/jump_n to %d, %d times", p1
- start
, mcnt2
);
1053 PREFIX(extract_number_and_incr
) (&mcnt
, &p
);
1055 PREFIX(extract_number_and_incr
) (&mcnt2
, &p
);
1057 printf ("/set_number_at location %td to %d", p1
- start
, mcnt2
);
1059 printf ("/set_number_at location %ld to %d",
1060 (long int) (p1
- start
), mcnt2
);
1065 printf ("/wordbound");
1069 printf ("/notwordbound");
1073 printf ("/wordbeg");
1077 printf ("/wordend");
1082 printf ("/before_dot");
1090 printf ("/after_dot");
1094 printf ("/syntaxspec");
1096 printf ("/%d", mcnt
);
1100 printf ("/notsyntaxspec");
1102 printf ("/%d", mcnt
);
1107 printf ("/wordchar");
1111 printf ("/notwordchar");
1123 printf ("?%ld", (long int) *(p
-1));
1130 printf ("%td:\tend of pattern.\n", p
- start
);
1132 printf ("%ld:\tend of pattern.\n", (long int) (p
- start
));
1138 PREFIX(print_compiled_pattern
) (struct re_pattern_buffer
*bufp
)
1140 UCHAR_T
*buffer
= (UCHAR_T
*) bufp
->buffer
;
1142 PREFIX(print_partial_compiled_pattern
) (buffer
, buffer
1143 + bufp
->used
/ sizeof(UCHAR_T
));
1144 printf ("%ld bytes used/%ld bytes allocated.\n",
1145 bufp
->used
, bufp
->allocated
);
1147 if (bufp
->fastmap_accurate
&& bufp
->fastmap
)
1149 printf ("fastmap: ");
1150 print_fastmap (bufp
->fastmap
);
1154 printf ("re_nsub: %Zd\t", bufp
->re_nsub
);
1156 printf ("re_nsub: %ld\t", (long int) bufp
->re_nsub
);
1158 printf ("regs_alloc: %d\t", bufp
->regs_allocated
);
1159 printf ("can_be_null: %d\t", bufp
->can_be_null
);
1160 printf ("newline_anchor: %d\n", bufp
->newline_anchor
);
1161 printf ("no_sub: %d\t", bufp
->no_sub
);
1162 printf ("not_bol: %d\t", bufp
->not_bol
);
1163 printf ("not_eol: %d\t", bufp
->not_eol
);
1164 printf ("syntax: %lx\n", bufp
->syntax
);
1165 /* Perhaps we should print the translate table? */
1170 PREFIX(print_double_string
) (const CHAR_T
*where
, const CHAR_T
*string1
,
1171 int size1
, const CHAR_T
*string2
, int size2
)
1181 if (FIRST_STRING_P (where
))
1183 for (this_char
= where
- string1
; this_char
< size1
; this_char
++)
1184 PUT_CHAR (string1
[this_char
]);
1190 for (this_char
= where
- string2
; this_char
< size2
; this_char
++)
1192 PUT_CHAR (string2
[this_char
]);
1195 fputs ("...", stdout
);
1202 # ifndef DEFINED_ONCE
1210 # else /* not DEBUG */
1212 # ifndef DEFINED_ONCE
1216 # define DEBUG_STATEMENT(e)
1217 # define DEBUG_PRINT1(x)
1218 # define DEBUG_PRINT2(x1, x2)
1219 # define DEBUG_PRINT3(x1, x2, x3)
1220 # define DEBUG_PRINT4(x1, x2, x3, x4)
1221 # endif /* not DEFINED_ONCE */
1222 # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
1223 # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
1225 # endif /* not DEBUG */
1230 /* This convert a multibyte string to a wide character string.
1231 And write their correspondances to offset_buffer(see below)
1232 and write whether each wchar_t is binary data to is_binary.
1233 This assume invalid multibyte sequences as binary data.
1234 We assume offset_buffer and is_binary is already allocated
1237 static size_t convert_mbs_to_wcs (CHAR_T
*dest
, const unsigned char* src
,
1238 size_t len
, int *offset_buffer
,
1241 convert_mbs_to_wcs (CHAR_T
*dest
, const unsigned char*src
, size_t len
,
1242 int *offset_buffer
, char *is_binary
)
1243 /* It hold correspondances between src(char string) and
1244 dest(wchar_t string) for optimization.
1246 dest = {'X', 'Y', 'Z'}
1247 (each "xxx", "y" and "zz" represent one multibyte character
1248 corresponding to 'X', 'Y' and 'Z'.)
1249 offset_buffer = {0, 0+3("xxx"), 0+3+1("y"), 0+3+1+2("zz")}
1253 wchar_t *pdest
= dest
;
1254 const unsigned char *psrc
= src
;
1255 size_t wc_count
= 0;
1259 size_t mb_remain
= len
;
1260 size_t mb_count
= 0;
1262 /* Initialize the conversion state. */
1263 memset (&mbs
, 0, sizeof (mbstate_t));
1265 offset_buffer
[0] = 0;
1266 for( ; mb_remain
> 0 ; ++wc_count
, ++pdest
, mb_remain
-= consumed
,
1270 consumed
= __mbrtowc (pdest
, psrc
, mb_remain
, &mbs
);
1272 consumed
= mbrtowc (pdest
, psrc
, mb_remain
, &mbs
);
1276 /* failed to convert. maybe src contains binary data.
1277 So we consume 1 byte manualy. */
1281 is_binary
[wc_count
] = TRUE
;
1284 is_binary
[wc_count
] = FALSE
;
1285 /* In sjis encoding, we use yen sign as escape character in
1286 place of reverse solidus. So we convert 0x5c(yen sign in
1287 sjis) to not 0xa5(yen sign in UCS2) but 0x5c(reverse
1288 solidus in UCS2). */
1289 if (consumed
== 1 && (int) *psrc
== 0x5c && (int) *pdest
== 0xa5)
1290 *pdest
= (wchar_t) *psrc
;
1292 offset_buffer
[wc_count
+ 1] = mb_count
+= consumed
;
1295 /* Fill remain of the buffer with sentinel. */
1296 for (i
= wc_count
+ 1 ; i
<= len
; i
++)
1297 offset_buffer
[i
] = mb_count
+ 1;
1304 #else /* not INSIDE_RECURSION */
1306 /* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
1307 also be assigned to arbitrarily: each pattern buffer stores its own
1308 syntax, so it can be changed between regex compilations. */
1309 /* This has no initializer because initialized variables in Emacs
1310 become read-only after dumping. */
1311 reg_syntax_t re_syntax_options
;
1314 /* Specify the precise syntax of regexps for compilation. This provides
1315 for compatibility for various utilities which historically have
1316 different, incompatible syntaxes.
1318 The argument SYNTAX is a bit mask comprised of the various bits
1319 defined in regex.h. We return the old syntax. */
1322 re_set_syntax (reg_syntax_t syntax
)
1324 reg_syntax_t ret
= re_syntax_options
;
1326 re_syntax_options
= syntax
;
1328 if (syntax
& RE_DEBUG
)
1330 else if (debug
) /* was on but now is not */
1336 weak_alias (__re_set_syntax
, re_set_syntax
)
1339 /* This table gives an error message for each of the error codes listed
1340 in regex.h. Obviously the order here has to be same as there.
1341 POSIX doesn't require that we do anything for REG_NOERROR,
1342 but why not be nice? */
1344 static const char *re_error_msgid
[] =
1346 gettext_noop ("Success"), /* REG_NOERROR */
1347 gettext_noop ("No match"), /* REG_NOMATCH */
1348 gettext_noop ("Invalid regular expression"), /* REG_BADPAT */
1349 gettext_noop ("Invalid collation character"), /* REG_ECOLLATE */
1350 gettext_noop ("Invalid character class name"), /* REG_ECTYPE */
1351 gettext_noop ("Trailing backslash"), /* REG_EESCAPE */
1352 gettext_noop ("Invalid back reference"), /* REG_ESUBREG */
1353 gettext_noop ("Unmatched [ or [^"), /* REG_EBRACK */
1354 gettext_noop ("Unmatched ( or \\("), /* REG_EPAREN */
1355 gettext_noop ("Unmatched \\{"), /* REG_EBRACE */
1356 gettext_noop ("Invalid content of \\{\\}"), /* REG_BADBR */
1357 gettext_noop ("Invalid range end"), /* REG_ERANGE */
1358 gettext_noop ("Memory exhausted"), /* REG_ESPACE */
1359 gettext_noop ("Invalid preceding regular expression"), /* REG_BADRPT */
1360 gettext_noop ("Premature end of regular expression"), /* REG_EEND */
1361 gettext_noop ("Regular expression too big"), /* REG_ESIZE */
1362 gettext_noop ("Unmatched ) or \\)") /* REG_ERPAREN */
1365 #endif /* INSIDE_RECURSION */
1367 #ifndef DEFINED_ONCE
1368 /* Avoiding alloca during matching, to placate r_alloc. */
1370 /* Define MATCH_MAY_ALLOCATE unless we need to make sure that the
1371 searching and matching functions should not call alloca. On some
1372 systems, alloca is implemented in terms of malloc, and if we're
1373 using the relocating allocator routines, then malloc could cause a
1374 relocation, which might (if the strings being searched are in the
1375 ralloc heap) shift the data out from underneath the regexp
1378 Here's another reason to avoid allocation: Emacs
1379 processes input from X in a signal handler; processing X input may
1380 call malloc; if input arrives while a matching routine is calling
1381 malloc, then we're scrod. But Emacs can't just block input while
1382 calling matching routines; then we don't notice interrupts when
1383 they come in. So, Emacs blocks input around all regexp calls
1384 except the matching calls, which it leaves unprotected, in the
1385 faith that they will not malloc. */
1387 /* Normally, this is fine. */
1388 # define MATCH_MAY_ALLOCATE
1390 /* When using GNU C, we are not REALLY using the C alloca, no matter
1391 what config.h may say. So don't take precautions for it. */
1396 /* The match routines may not allocate if (1) they would do it with malloc
1397 and (2) it's not safe for them to use malloc.
1398 Note that if REL_ALLOC is defined, matching would not use malloc for the
1399 failure stack, but we would still use it for the register vectors;
1400 so REL_ALLOC should not affect this. */
1401 # if (defined C_ALLOCA || defined REGEX_MALLOC) && defined emacs
1402 # undef MATCH_MAY_ALLOCATE
1404 #endif /* not DEFINED_ONCE */
1406 #ifdef INSIDE_RECURSION
1407 /* Failure stack declarations and macros; both re_compile_fastmap and
1408 re_match_2 use a failure stack. These have to be macros because of
1409 REGEX_ALLOCATE_STACK. */
1412 /* Number of failure points for which to initially allocate space
1413 when matching. If this number is exceeded, we allocate more
1414 space, so it is not a hard limit. */
1415 # ifndef INIT_FAILURE_ALLOC
1416 # define INIT_FAILURE_ALLOC 5
1419 /* Roughly the maximum number of failure points on the stack. Would be
1420 exactly that if always used MAX_FAILURE_ITEMS items each time we failed.
1421 This is a variable only so users of regex can assign to it; we never
1422 change it ourselves. */
1424 # ifdef INT_IS_16BIT
1426 # ifndef DEFINED_ONCE
1427 # if defined MATCH_MAY_ALLOCATE
1428 /* 4400 was enough to cause a crash on Alpha OSF/1,
1429 whose default stack limit is 2mb. */
1430 long int re_max_failures
= 4000;
1432 long int re_max_failures
= 2000;
1436 union PREFIX(fail_stack_elt
)
1442 typedef union PREFIX(fail_stack_elt
) PREFIX(fail_stack_elt_t
);
1446 PREFIX(fail_stack_elt_t
) *stack
;
1447 unsigned long int size
;
1448 unsigned long int avail
; /* Offset of next open position. */
1449 } PREFIX(fail_stack_type
);
1451 # else /* not INT_IS_16BIT */
1453 # ifndef DEFINED_ONCE
1454 # if defined MATCH_MAY_ALLOCATE
1455 /* 4400 was enough to cause a crash on Alpha OSF/1,
1456 whose default stack limit is 2mb. */
1457 int re_max_failures
= 4000;
1459 int re_max_failures
= 2000;
1463 union PREFIX(fail_stack_elt
)
1469 typedef union PREFIX(fail_stack_elt
) PREFIX(fail_stack_elt_t
);
1473 PREFIX(fail_stack_elt_t
) *stack
;
1475 unsigned avail
; /* Offset of next open position. */
1476 } PREFIX(fail_stack_type
);
1478 # endif /* INT_IS_16BIT */
1480 # ifndef DEFINED_ONCE
1481 # define FAIL_STACK_EMPTY() (fail_stack.avail == 0)
1482 # define FAIL_STACK_PTR_EMPTY() (fail_stack_ptr->avail == 0)
1483 # define FAIL_STACK_FULL() (fail_stack.avail == fail_stack.size)
1487 /* Define macros to initialize and free the failure stack.
1488 Do `return -2' if the alloc fails. */
1490 # ifdef MATCH_MAY_ALLOCATE
1491 # define INIT_FAIL_STACK() \
1493 fail_stack.stack = (PREFIX(fail_stack_elt_t) *) \
1494 REGEX_ALLOCATE_STACK (INIT_FAILURE_ALLOC * sizeof (PREFIX(fail_stack_elt_t))); \
1496 if (fail_stack.stack == NULL) \
1499 fail_stack.size = INIT_FAILURE_ALLOC; \
1500 fail_stack.avail = 0; \
1503 # define RESET_FAIL_STACK() REGEX_FREE_STACK (fail_stack.stack)
1505 # define INIT_FAIL_STACK() \
1507 fail_stack.avail = 0; \
1510 # define RESET_FAIL_STACK()
1514 /* Double the size of FAIL_STACK, up to approximately `re_max_failures' items.
1516 Return 1 if succeeds, and 0 if either ran out of memory
1517 allocating space for it or it was already too large.
1519 REGEX_REALLOCATE_STACK requires `destination' be declared. */
1521 # define DOUBLE_FAIL_STACK(fail_stack) \
1522 ((fail_stack).size > (unsigned) (re_max_failures * MAX_FAILURE_ITEMS) \
1524 : ((fail_stack).stack = (PREFIX(fail_stack_elt_t) *) \
1525 REGEX_REALLOCATE_STACK ((fail_stack).stack, \
1526 (fail_stack).size * sizeof (PREFIX(fail_stack_elt_t)), \
1527 ((fail_stack).size << 1) * sizeof (PREFIX(fail_stack_elt_t))),\
1529 (fail_stack).stack == NULL \
1531 : ((fail_stack).size <<= 1, \
1535 /* Push pointer POINTER on FAIL_STACK.
1536 Return 1 if was able to do so and 0 if ran out of memory allocating
1538 # define PUSH_PATTERN_OP(POINTER, FAIL_STACK) \
1539 ((FAIL_STACK_FULL () \
1540 && !DOUBLE_FAIL_STACK (FAIL_STACK)) \
1542 : ((FAIL_STACK).stack[(FAIL_STACK).avail++].pointer = POINTER, \
1545 /* Push a pointer value onto the failure stack.
1546 Assumes the variable `fail_stack'. Probably should only
1547 be called from within `PUSH_FAILURE_POINT'. */
1548 # define PUSH_FAILURE_POINTER(item) \
1549 fail_stack.stack[fail_stack.avail++].pointer = (UCHAR_T *) (item)
1551 /* This pushes an integer-valued item onto the failure stack.
1552 Assumes the variable `fail_stack'. Probably should only
1553 be called from within `PUSH_FAILURE_POINT'. */
1554 # define PUSH_FAILURE_INT(item) \
1555 fail_stack.stack[fail_stack.avail++].integer = (item)
1557 /* Push a fail_stack_elt_t value onto the failure stack.
1558 Assumes the variable `fail_stack'. Probably should only
1559 be called from within `PUSH_FAILURE_POINT'. */
1560 # define PUSH_FAILURE_ELT(item) \
1561 fail_stack.stack[fail_stack.avail++] = (item)
1563 /* These three POP... operations complement the three PUSH... operations.
1564 All assume that `fail_stack' is nonempty. */
1565 # define POP_FAILURE_POINTER() fail_stack.stack[--fail_stack.avail].pointer
1566 # define POP_FAILURE_INT() fail_stack.stack[--fail_stack.avail].integer
1567 # define POP_FAILURE_ELT() fail_stack.stack[--fail_stack.avail]
1569 /* Used to omit pushing failure point id's when we're not debugging. */
1571 # define DEBUG_PUSH PUSH_FAILURE_INT
1572 # define DEBUG_POP(item_addr) *(item_addr) = POP_FAILURE_INT ()
1574 # define DEBUG_PUSH(item)
1575 # define DEBUG_POP(item_addr)
1579 /* Push the information about the state we will need
1580 if we ever fail back to it.
1582 Requires variables fail_stack, regstart, regend, reg_info, and
1583 num_regs_pushed be declared. DOUBLE_FAIL_STACK requires `destination'
1586 Does `return FAILURE_CODE' if runs out of memory. */
1588 # define PUSH_FAILURE_POINT(pattern_place, string_place, failure_code) \
1590 char *destination; \
1591 /* Must be int, so when we don't save any registers, the arithmetic \
1592 of 0 + -1 isn't done as unsigned. */ \
1593 /* Can't be int, since there is not a shred of a guarantee that int \
1594 is wide enough to hold a value of something to which pointer can \
1596 active_reg_t this_reg; \
1598 DEBUG_STATEMENT (failure_id++); \
1599 DEBUG_STATEMENT (nfailure_points_pushed++); \
1600 DEBUG_PRINT2 ("\nPUSH_FAILURE_POINT #%u:\n", failure_id); \
1601 DEBUG_PRINT2 (" Before push, next avail: %d\n", (fail_stack).avail);\
1602 DEBUG_PRINT2 (" size: %d\n", (fail_stack).size);\
1604 DEBUG_PRINT2 (" slots needed: %ld\n", NUM_FAILURE_ITEMS); \
1605 DEBUG_PRINT2 (" available: %d\n", REMAINING_AVAIL_SLOTS); \
1607 /* Ensure we have enough space allocated for what we will push. */ \
1608 while (REMAINING_AVAIL_SLOTS < NUM_FAILURE_ITEMS) \
1610 if (!DOUBLE_FAIL_STACK (fail_stack)) \
1611 return failure_code; \
1613 DEBUG_PRINT2 ("\n Doubled stack; size now: %d\n", \
1614 (fail_stack).size); \
1615 DEBUG_PRINT2 (" slots available: %d\n", REMAINING_AVAIL_SLOTS);\
1618 /* Push the info, starting with the registers. */ \
1619 DEBUG_PRINT1 ("\n"); \
1622 for (this_reg = lowest_active_reg; this_reg <= highest_active_reg; \
1625 DEBUG_PRINT2 (" Pushing reg: %lu\n", this_reg); \
1626 DEBUG_STATEMENT (num_regs_pushed++); \
1628 DEBUG_PRINT2 (" start: %p\n", regstart[this_reg]); \
1629 PUSH_FAILURE_POINTER (regstart[this_reg]); \
1631 DEBUG_PRINT2 (" end: %p\n", regend[this_reg]); \
1632 PUSH_FAILURE_POINTER (regend[this_reg]); \
1634 DEBUG_PRINT2 (" info: %p\n ", \
1635 reg_info[this_reg].word.pointer); \
1636 DEBUG_PRINT2 (" match_null=%d", \
1637 REG_MATCH_NULL_STRING_P (reg_info[this_reg])); \
1638 DEBUG_PRINT2 (" active=%d", IS_ACTIVE (reg_info[this_reg])); \
1639 DEBUG_PRINT2 (" matched_something=%d", \
1640 MATCHED_SOMETHING (reg_info[this_reg])); \
1641 DEBUG_PRINT2 (" ever_matched=%d", \
1642 EVER_MATCHED_SOMETHING (reg_info[this_reg])); \
1643 DEBUG_PRINT1 ("\n"); \
1644 PUSH_FAILURE_ELT (reg_info[this_reg].word); \
1647 DEBUG_PRINT2 (" Pushing low active reg: %ld\n", lowest_active_reg);\
1648 PUSH_FAILURE_INT (lowest_active_reg); \
1650 DEBUG_PRINT2 (" Pushing high active reg: %ld\n", highest_active_reg);\
1651 PUSH_FAILURE_INT (highest_active_reg); \
1653 DEBUG_PRINT2 (" Pushing pattern %p:\n", pattern_place); \
1654 DEBUG_PRINT_COMPILED_PATTERN (bufp, pattern_place, pend); \
1655 PUSH_FAILURE_POINTER (pattern_place); \
1657 DEBUG_PRINT2 (" Pushing string %p: `", string_place); \
1658 DEBUG_PRINT_DOUBLE_STRING (string_place, string1, size1, string2, \
1660 DEBUG_PRINT1 ("'\n"); \
1661 PUSH_FAILURE_POINTER (string_place); \
1663 DEBUG_PRINT2 (" Pushing failure id: %u\n", failure_id); \
1664 DEBUG_PUSH (failure_id); \
1667 # ifndef DEFINED_ONCE
1668 /* This is the number of items that are pushed and popped on the stack
1669 for each register. */
1670 # define NUM_REG_ITEMS 3
1672 /* Individual items aside from the registers. */
1674 # define NUM_NONREG_ITEMS 5 /* Includes failure point id. */
1676 # define NUM_NONREG_ITEMS 4
1679 /* We push at most this many items on the stack. */
1680 /* We used to use (num_regs - 1), which is the number of registers
1681 this regexp will save; but that was changed to 5
1682 to avoid stack overflow for a regexp with lots of parens. */
1683 # define MAX_FAILURE_ITEMS (5 * NUM_REG_ITEMS + NUM_NONREG_ITEMS)
1685 /* We actually push this many items. */
1686 # define NUM_FAILURE_ITEMS \
1688 ? 0 : highest_active_reg - lowest_active_reg + 1) \
1692 /* How many items can still be added to the stack without overflowing it. */
1693 # define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail)
1694 # endif /* not DEFINED_ONCE */
1697 /* Pops what PUSH_FAIL_STACK pushes.
1699 We restore into the parameters, all of which should be lvalues:
1700 STR -- the saved data position.
1701 PAT -- the saved pattern position.
1702 LOW_REG, HIGH_REG -- the highest and lowest active registers.
1703 REGSTART, REGEND -- arrays of string positions.
1704 REG_INFO -- array of information about each subexpression.
1706 Also assumes the variables `fail_stack' and (if debugging), `bufp',
1707 `pend', `string1', `size1', `string2', and `size2'. */
1708 # define POP_FAILURE_POINT(str, pat, low_reg, high_reg, regstart, regend, reg_info)\
1710 DEBUG_STATEMENT (unsigned failure_id;) \
1711 active_reg_t this_reg; \
1712 const UCHAR_T *string_temp; \
1714 assert (!FAIL_STACK_EMPTY ()); \
1716 /* Remove failure points and point to how many regs pushed. */ \
1717 DEBUG_PRINT1 ("POP_FAILURE_POINT:\n"); \
1718 DEBUG_PRINT2 (" Before pop, next avail: %d\n", fail_stack.avail); \
1719 DEBUG_PRINT2 (" size: %d\n", fail_stack.size); \
1721 assert (fail_stack.avail >= NUM_NONREG_ITEMS); \
1723 DEBUG_POP (&failure_id); \
1724 DEBUG_PRINT2 (" Popping failure id: %u\n", failure_id); \
1726 /* If the saved string location is NULL, it came from an \
1727 on_failure_keep_string_jump opcode, and we want to throw away the \
1728 saved NULL, thus retaining our current position in the string. */ \
1729 string_temp = POP_FAILURE_POINTER (); \
1730 if (string_temp != NULL) \
1731 str = (const CHAR_T *) string_temp; \
1733 DEBUG_PRINT2 (" Popping string %p: `", str); \
1734 DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2); \
1735 DEBUG_PRINT1 ("'\n"); \
1737 pat = (UCHAR_T *) POP_FAILURE_POINTER (); \
1738 DEBUG_PRINT2 (" Popping pattern %p:\n", pat); \
1739 DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend); \
1741 /* Restore register info. */ \
1742 high_reg = (active_reg_t) POP_FAILURE_INT (); \
1743 DEBUG_PRINT2 (" Popping high active reg: %ld\n", high_reg); \
1745 low_reg = (active_reg_t) POP_FAILURE_INT (); \
1746 DEBUG_PRINT2 (" Popping low active reg: %ld\n", low_reg); \
1749 for (this_reg = high_reg; this_reg >= low_reg; this_reg--) \
1751 DEBUG_PRINT2 (" Popping reg: %ld\n", this_reg); \
1753 reg_info[this_reg].word = POP_FAILURE_ELT (); \
1754 DEBUG_PRINT2 (" info: %p\n", \
1755 reg_info[this_reg].word.pointer); \
1757 regend[this_reg] = (const CHAR_T *) POP_FAILURE_POINTER (); \
1758 DEBUG_PRINT2 (" end: %p\n", regend[this_reg]); \
1760 regstart[this_reg] = (const CHAR_T *) POP_FAILURE_POINTER (); \
1761 DEBUG_PRINT2 (" start: %p\n", regstart[this_reg]); \
1765 for (this_reg = highest_active_reg; this_reg > high_reg; this_reg--) \
1767 reg_info[this_reg].word.integer = 0; \
1768 regend[this_reg] = 0; \
1769 regstart[this_reg] = 0; \
1771 highest_active_reg = high_reg; \
1774 set_regs_matched_done = 0; \
1775 DEBUG_STATEMENT (nfailure_points_popped++); \
1776 } /* POP_FAILURE_POINT */
1778 /* Structure for per-register (a.k.a. per-group) information.
1779 Other register information, such as the
1780 starting and ending positions (which are addresses), and the list of
1781 inner groups (which is a bits list) are maintained in separate
1784 We are making a (strictly speaking) nonportable assumption here: that
1785 the compiler will pack our bit fields into something that fits into
1786 the type of `word', i.e., is something that fits into one item on the
1790 /* Declarations and macros for re_match_2. */
1794 PREFIX(fail_stack_elt_t
) word
;
1797 /* This field is one if this group can match the empty string,
1798 zero if not. If not yet determined, `MATCH_NULL_UNSET_VALUE'. */
1799 # define MATCH_NULL_UNSET_VALUE 3
1800 unsigned match_null_string_p
: 2;
1801 unsigned is_active
: 1;
1802 unsigned matched_something
: 1;
1803 unsigned ever_matched_something
: 1;
1805 } PREFIX(register_info_type
);
1807 # ifndef DEFINED_ONCE
1808 # define REG_MATCH_NULL_STRING_P(R) ((R).bits.match_null_string_p)
1809 # define IS_ACTIVE(R) ((R).bits.is_active)
1810 # define MATCHED_SOMETHING(R) ((R).bits.matched_something)
1811 # define EVER_MATCHED_SOMETHING(R) ((R).bits.ever_matched_something)
1814 /* Call this when have matched a real character; it sets `matched' flags
1815 for the subexpressions which we are currently inside. Also records
1816 that those subexprs have matched. */
1817 # define SET_REGS_MATCHED() \
1820 if (!set_regs_matched_done) \
1823 set_regs_matched_done = 1; \
1824 for (r = lowest_active_reg; r <= highest_active_reg; r++) \
1826 MATCHED_SOMETHING (reg_info[r]) \
1827 = EVER_MATCHED_SOMETHING (reg_info[r]) \
1833 # endif /* not DEFINED_ONCE */
1835 /* Registers are set to a sentinel when they haven't yet matched. */
1836 static CHAR_T
PREFIX(reg_unset_dummy
);
1837 # define REG_UNSET_VALUE (&PREFIX(reg_unset_dummy))
1838 # define REG_UNSET(e) ((e) == REG_UNSET_VALUE)
1840 /* Subroutine declarations and macros for regex_compile. */
1841 static void PREFIX(store_op1
) (re_opcode_t op
, UCHAR_T
*loc
, int arg
);
1842 static void PREFIX(store_op2
) (re_opcode_t op
, UCHAR_T
*loc
,
1843 int arg1
, int arg2
);
1844 static void PREFIX(insert_op1
) (re_opcode_t op
, UCHAR_T
*loc
,
1845 int arg
, UCHAR_T
*end
);
1846 static void PREFIX(insert_op2
) (re_opcode_t op
, UCHAR_T
*loc
,
1847 int arg1
, int arg2
, UCHAR_T
*end
);
1848 static boolean
PREFIX(at_begline_loc_p
) (const CHAR_T
*pattern
,
1850 reg_syntax_t syntax
);
1851 static boolean
PREFIX(at_endline_loc_p
) (const CHAR_T
*p
,
1853 reg_syntax_t syntax
);
1855 static reg_errcode_t
wcs_compile_range (CHAR_T range_start
,
1856 const CHAR_T
**p_ptr
,
1859 reg_syntax_t syntax
,
1862 static void insert_space (int num
, CHAR_T
*loc
, CHAR_T
*end
);
1864 static reg_errcode_t
byte_compile_range (unsigned int range_start
,
1868 reg_syntax_t syntax
,
1872 /* Fetch the next character in the uncompiled pattern---translating it
1873 if necessary. Also cast from a signed character in the constant
1874 string passed to us by the user to an unsigned char that we can use
1875 as an array index (in, e.g., `translate'). */
1876 /* ifdef MBS_SUPPORT, we translate only if character <= 0xff,
1877 because it is impossible to allocate 4GB array for some encodings
1878 which have 4 byte character_set like UCS4. */
1881 # define PATFETCH(c) \
1882 do {if (p == pend) return REG_EEND; \
1883 c = (UCHAR_T) *p++; \
1884 if (translate && (c <= 0xff)) c = (UCHAR_T) translate[c]; \
1887 # define PATFETCH(c) \
1888 do {if (p == pend) return REG_EEND; \
1889 c = (unsigned char) *p++; \
1890 if (translate) c = (unsigned char) translate[c]; \
1895 /* Fetch the next character in the uncompiled pattern, with no
1897 # define PATFETCH_RAW(c) \
1898 do {if (p == pend) return REG_EEND; \
1899 c = (UCHAR_T) *p++; \
1902 /* Go backwards one character in the pattern. */
1903 # define PATUNFETCH p--
1906 /* If `translate' is non-null, return translate[D], else just D. We
1907 cast the subscript to translate because some data is declared as
1908 `char *', to avoid warnings when a string constant is passed. But
1909 when we use a character as a subscript we must make it unsigned. */
1910 /* ifdef MBS_SUPPORT, we translate only if character <= 0xff,
1911 because it is impossible to allocate 4GB array for some encodings
1912 which have 4 byte character_set like UCS4. */
1916 # define TRANSLATE(d) \
1917 ((translate && ((UCHAR_T) (d)) <= 0xff) \
1918 ? (char) translate[(unsigned char) (d)] : (d))
1920 # define TRANSLATE(d) \
1921 (translate ? (char) translate[(unsigned char) (d)] : (d))
1926 /* Macros for outputting the compiled pattern into `buffer'. */
1928 /* If the buffer isn't allocated when it comes in, use this. */
1929 # define INIT_BUF_SIZE (32 * sizeof(UCHAR_T))
1931 /* Make sure we have at least N more bytes of space in buffer. */
1933 # define GET_BUFFER_SPACE(n) \
1934 while (((unsigned long)b - (unsigned long)COMPILED_BUFFER_VAR \
1935 + (n)*sizeof(CHAR_T)) > bufp->allocated) \
1938 # define GET_BUFFER_SPACE(n) \
1939 while ((unsigned long) (b - bufp->buffer + (n)) > bufp->allocated) \
1943 /* Make sure we have one more byte of buffer space and then add C to it. */
1944 # define BUF_PUSH(c) \
1946 GET_BUFFER_SPACE (1); \
1947 *b++ = (UCHAR_T) (c); \
1951 /* Ensure we have two more bytes of buffer space and then append C1 and C2. */
1952 # define BUF_PUSH_2(c1, c2) \
1954 GET_BUFFER_SPACE (2); \
1955 *b++ = (UCHAR_T) (c1); \
1956 *b++ = (UCHAR_T) (c2); \
1960 /* As with BUF_PUSH_2, except for three bytes. */
1961 # define BUF_PUSH_3(c1, c2, c3) \
1963 GET_BUFFER_SPACE (3); \
1964 *b++ = (UCHAR_T) (c1); \
1965 *b++ = (UCHAR_T) (c2); \
1966 *b++ = (UCHAR_T) (c3); \
1969 /* Store a jump with opcode OP at LOC to location TO. We store a
1970 relative address offset by the three bytes the jump itself occupies. */
1971 # define STORE_JUMP(op, loc, to) \
1972 PREFIX(store_op1) (op, loc, (int) ((to) - (loc) - (1 + OFFSET_ADDRESS_SIZE)))
1974 /* Likewise, for a two-argument jump. */
1975 # define STORE_JUMP2(op, loc, to, arg) \
1976 PREFIX(store_op2) (op, loc, (int) ((to) - (loc) - (1 + OFFSET_ADDRESS_SIZE)), arg)
1978 /* Like `STORE_JUMP', but for inserting. Assume `b' is the buffer end. */
1979 # define INSERT_JUMP(op, loc, to) \
1980 PREFIX(insert_op1) (op, loc, (int) ((to) - (loc) - (1 + OFFSET_ADDRESS_SIZE)), b)
1982 /* Like `STORE_JUMP2', but for inserting. Assume `b' is the buffer end. */
1983 # define INSERT_JUMP2(op, loc, to, arg) \
1984 PREFIX(insert_op2) (op, loc, (int) ((to) - (loc) - (1 + OFFSET_ADDRESS_SIZE)),\
1987 /* This is not an arbitrary limit: the arguments which represent offsets
1988 into the pattern are two bytes long. So if 2^16 bytes turns out to
1989 be too small, many things would have to change. */
1990 /* Any other compiler which, like MSC, has allocation limit below 2^16
1991 bytes will have to use approach similar to what was done below for
1992 MSC and drop MAX_BUF_SIZE a bit. Otherwise you may end up
1993 reallocating to 0 bytes. Such thing is not going to work too well.
1994 You have been warned!! */
1995 # ifndef DEFINED_ONCE
1996 # if defined _MSC_VER && !defined WIN32
1997 /* Microsoft C 16-bit versions limit malloc to approx 65512 bytes.
1998 The REALLOC define eliminates a flurry of conversion warnings,
1999 but is not required. */
2000 # define MAX_BUF_SIZE 65500L
2001 # define REALLOC(p,s) realloc ((p), (size_t) (s))
2003 # define MAX_BUF_SIZE (1L << 16)
2004 # define REALLOC(p,s) realloc ((p), (s))
2007 /* Extend the buffer by twice its current size via realloc and
2008 reset the pointers that pointed into the old block to point to the
2009 correct places in the new one. If extending the buffer results in it
2010 being larger than MAX_BUF_SIZE, then flag memory exhausted. */
2011 # if __BOUNDED_POINTERS__
2012 # define SET_HIGH_BOUND(P) (__ptrhigh (P) = __ptrlow (P) + bufp->allocated)
2013 # define MOVE_BUFFER_POINTER(P) \
2014 (__ptrlow (P) += incr, SET_HIGH_BOUND (P), __ptrvalue (P) += incr)
2015 # define ELSE_EXTEND_BUFFER_HIGH_BOUND \
2018 SET_HIGH_BOUND (b); \
2019 SET_HIGH_BOUND (begalt); \
2020 if (fixup_alt_jump) \
2021 SET_HIGH_BOUND (fixup_alt_jump); \
2023 SET_HIGH_BOUND (laststart); \
2024 if (pending_exact) \
2025 SET_HIGH_BOUND (pending_exact); \
2028 # define MOVE_BUFFER_POINTER(P) (P) += incr
2029 # define ELSE_EXTEND_BUFFER_HIGH_BOUND
2031 # endif /* not DEFINED_ONCE */
2034 # define EXTEND_BUFFER() \
2036 UCHAR_T *old_buffer = COMPILED_BUFFER_VAR; \
2038 if (bufp->allocated + sizeof(UCHAR_T) > MAX_BUF_SIZE) \
2040 bufp->allocated <<= 1; \
2041 if (bufp->allocated > MAX_BUF_SIZE) \
2042 bufp->allocated = MAX_BUF_SIZE; \
2043 /* How many characters the new buffer can have? */ \
2044 wchar_count = bufp->allocated / sizeof(UCHAR_T); \
2045 if (wchar_count == 0) wchar_count = 1; \
2046 /* Truncate the buffer to CHAR_T align. */ \
2047 bufp->allocated = wchar_count * sizeof(UCHAR_T); \
2048 RETALLOC (COMPILED_BUFFER_VAR, wchar_count, UCHAR_T); \
2049 bufp->buffer = (char*)COMPILED_BUFFER_VAR; \
2050 if (COMPILED_BUFFER_VAR == NULL) \
2051 return REG_ESPACE; \
2052 /* If the buffer moved, move all the pointers into it. */ \
2053 if (old_buffer != COMPILED_BUFFER_VAR) \
2055 int incr = COMPILED_BUFFER_VAR - old_buffer; \
2056 MOVE_BUFFER_POINTER (b); \
2057 MOVE_BUFFER_POINTER (begalt); \
2058 if (fixup_alt_jump) \
2059 MOVE_BUFFER_POINTER (fixup_alt_jump); \
2061 MOVE_BUFFER_POINTER (laststart); \
2062 if (pending_exact) \
2063 MOVE_BUFFER_POINTER (pending_exact); \
2065 ELSE_EXTEND_BUFFER_HIGH_BOUND \
2068 # define EXTEND_BUFFER() \
2070 UCHAR_T *old_buffer = COMPILED_BUFFER_VAR; \
2071 if (bufp->allocated == MAX_BUF_SIZE) \
2073 bufp->allocated <<= 1; \
2074 if (bufp->allocated > MAX_BUF_SIZE) \
2075 bufp->allocated = MAX_BUF_SIZE; \
2076 bufp->buffer = (UCHAR_T *) REALLOC (COMPILED_BUFFER_VAR, \
2078 if (COMPILED_BUFFER_VAR == NULL) \
2079 return REG_ESPACE; \
2080 /* If the buffer moved, move all the pointers into it. */ \
2081 if (old_buffer != COMPILED_BUFFER_VAR) \
2083 int incr = COMPILED_BUFFER_VAR - old_buffer; \
2084 MOVE_BUFFER_POINTER (b); \
2085 MOVE_BUFFER_POINTER (begalt); \
2086 if (fixup_alt_jump) \
2087 MOVE_BUFFER_POINTER (fixup_alt_jump); \
2089 MOVE_BUFFER_POINTER (laststart); \
2090 if (pending_exact) \
2091 MOVE_BUFFER_POINTER (pending_exact); \
2093 ELSE_EXTEND_BUFFER_HIGH_BOUND \
2097 # ifndef DEFINED_ONCE
2098 /* Since we have one byte reserved for the register number argument to
2099 {start,stop}_memory, the maximum number of groups we can report
2100 things about is what fits in that byte. */
2101 # define MAX_REGNUM 255
2103 /* But patterns can have more than `MAX_REGNUM' registers. We just
2104 ignore the excess. */
2105 typedef unsigned regnum_t
;
2108 /* Macros for the compile stack. */
2110 /* Since offsets can go either forwards or backwards, this type needs to
2111 be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1. */
2112 /* int may be not enough when sizeof(int) == 2. */
2113 typedef long pattern_offset_t
;
2117 pattern_offset_t begalt_offset
;
2118 pattern_offset_t fixup_alt_jump
;
2119 pattern_offset_t inner_group_offset
;
2120 pattern_offset_t laststart_offset
;
2122 } compile_stack_elt_t
;
2127 compile_stack_elt_t
*stack
;
2129 unsigned avail
; /* Offset of next open position. */
2130 } compile_stack_type
;
2133 # define INIT_COMPILE_STACK_SIZE 32
2135 # define COMPILE_STACK_EMPTY (compile_stack.avail == 0)
2136 # define COMPILE_STACK_FULL (compile_stack.avail == compile_stack.size)
2138 /* The next available element. */
2139 # define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
2141 # endif /* not DEFINED_ONCE */
2143 /* Set the bit for character C in a list. */
2144 # ifndef DEFINED_ONCE
2145 # define SET_LIST_BIT(c) \
2146 (b[((unsigned char) (c)) / BYTEWIDTH] \
2147 |= 1 << (((unsigned char) c) % BYTEWIDTH))
2148 # endif /* DEFINED_ONCE */
2150 /* Get the next unsigned number in the uncompiled pattern. */
2151 # define GET_UNSIGNED_NUMBER(num) \
2156 if (c < '0' || c > '9') \
2158 if (num <= RE_DUP_MAX) \
2162 num = num * 10 + c - '0'; \
2167 # ifndef DEFINED_ONCE
2168 # if defined _LIBC || WIDE_CHAR_SUPPORT
2169 /* The GNU C library provides support for user-defined character classes
2170 and the functions from ISO C amendement 1. */
2171 # ifdef CHARCLASS_NAME_MAX
2172 # define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
2174 /* This shouldn't happen but some implementation might still have this
2175 problem. Use a reasonable default value. */
2176 # define CHAR_CLASS_MAX_LENGTH 256
2180 # define IS_CHAR_CLASS(string) __wctype (string)
2182 # define IS_CHAR_CLASS(string) wctype (string)
2185 # define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
2187 # define IS_CHAR_CLASS(string) \
2188 (STREQ (string, "alpha") || STREQ (string, "upper") \
2189 || STREQ (string, "lower") || STREQ (string, "digit") \
2190 || STREQ (string, "alnum") || STREQ (string, "xdigit") \
2191 || STREQ (string, "space") || STREQ (string, "print") \
2192 || STREQ (string, "punct") || STREQ (string, "graph") \
2193 || STREQ (string, "cntrl") || STREQ (string, "blank"))
2195 # endif /* DEFINED_ONCE */
2197 # ifndef MATCH_MAY_ALLOCATE
2199 /* If we cannot allocate large objects within re_match_2_internal,
2200 we make the fail stack and register vectors global.
2201 The fail stack, we grow to the maximum size when a regexp
2203 The register vectors, we adjust in size each time we
2204 compile a regexp, according to the number of registers it needs. */
2206 static PREFIX(fail_stack_type
) fail_stack
;
2208 /* Size with which the following vectors are currently allocated.
2209 That is so we can make them bigger as needed,
2210 but never make them smaller. */
2211 # ifdef DEFINED_ONCE
2212 static int regs_allocated_size
;
2214 static const char ** regstart
, ** regend
;
2215 static const char ** old_regstart
, ** old_regend
;
2216 static const char **best_regstart
, **best_regend
;
2217 static const char **reg_dummy
;
2218 # endif /* DEFINED_ONCE */
2220 static PREFIX(register_info_type
) *PREFIX(reg_info
);
2221 static PREFIX(register_info_type
) *PREFIX(reg_info_dummy
);
2223 /* Make the register vectors big enough for NUM_REGS registers,
2224 but don't make them smaller. */
2227 PREFIX(regex_grow_registers
) (int num_regs
)
2229 if (num_regs
> regs_allocated_size
)
2231 RETALLOC_IF (regstart
, num_regs
, const char *);
2232 RETALLOC_IF (regend
, num_regs
, const char *);
2233 RETALLOC_IF (old_regstart
, num_regs
, const char *);
2234 RETALLOC_IF (old_regend
, num_regs
, const char *);
2235 RETALLOC_IF (best_regstart
, num_regs
, const char *);
2236 RETALLOC_IF (best_regend
, num_regs
, const char *);
2237 RETALLOC_IF (PREFIX(reg_info
), num_regs
, PREFIX(register_info_type
));
2238 RETALLOC_IF (reg_dummy
, num_regs
, const char *);
2239 RETALLOC_IF (PREFIX(reg_info_dummy
), num_regs
, PREFIX(register_info_type
));
2241 regs_allocated_size
= num_regs
;
2245 # endif /* not MATCH_MAY_ALLOCATE */
2247 # ifndef DEFINED_ONCE
2248 static boolean
group_in_compile_stack (compile_stack_type compile_stack
,
2250 # endif /* not DEFINED_ONCE */
2252 /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
2253 Returns one of error codes defined in `regex.h', or zero for success.
2255 Assumes the `allocated' (and perhaps `buffer') and `translate'
2256 fields are set in BUFP on entry.
2258 If it succeeds, results are put in BUFP (if it returns an error, the
2259 contents of BUFP are undefined):
2260 `buffer' is the compiled pattern;
2261 `syntax' is set to SYNTAX;
2262 `used' is set to the length of the compiled pattern;
2263 `fastmap_accurate' is zero;
2264 `re_nsub' is the number of subexpressions in PATTERN;
2265 `not_bol' and `not_eol' are zero;
2267 The `fastmap' and `newline_anchor' fields are neither
2268 examined nor set. */
2270 /* Return, freeing storage we allocated. */
2272 # define FREE_STACK_RETURN(value) \
2273 return (free(pattern), free(mbs_offset), free(is_binary), free (compile_stack.stack), value)
2275 # define FREE_STACK_RETURN(value) \
2276 return (free (compile_stack.stack), value)
2279 static reg_errcode_t
2280 PREFIX(regex_compile
) (const char *ARG_PREFIX(pattern
),
2281 size_t ARG_PREFIX(size
), reg_syntax_t syntax
,
2282 struct re_pattern_buffer
*bufp
)
2284 /* We fetch characters from PATTERN here. Even though PATTERN is
2285 `char *' (i.e., signed), we declare these variables as unsigned, so
2286 they can be reliably used as array indices. */
2287 register UCHAR_T c
, c1
;
2290 /* A temporary space to keep wchar_t pattern and compiled pattern. */
2291 CHAR_T
*pattern
, *COMPILED_BUFFER_VAR
;
2293 /* offset buffer for optimization. See convert_mbs_to_wc. */
2294 int *mbs_offset
= NULL
;
2295 /* It hold whether each wchar_t is binary data or not. */
2296 char *is_binary
= NULL
;
2297 /* A flag whether exactn is handling binary data or not. */
2298 char is_exactn_bin
= FALSE
;
2301 /* A random temporary spot in PATTERN. */
2304 /* Points to the end of the buffer, where we should append. */
2305 register UCHAR_T
*b
;
2307 /* Keeps track of unclosed groups. */
2308 compile_stack_type compile_stack
;
2310 /* Points to the current (ending) position in the pattern. */
2315 const CHAR_T
*p
= pattern
;
2316 const CHAR_T
*pend
= pattern
+ size
;
2319 /* How to translate the characters in the pattern. */
2320 RE_TRANSLATE_TYPE translate
= bufp
->translate
;
2322 /* Address of the count-byte of the most recently inserted `exactn'
2323 command. This makes it possible to tell if a new exact-match
2324 character can be added to that command or if the character requires
2325 a new `exactn' command. */
2326 UCHAR_T
*pending_exact
= 0;
2328 /* Address of start of the most recently finished expression.
2329 This tells, e.g., postfix * where to find the start of its
2330 operand. Reset at the beginning of groups and alternatives. */
2331 UCHAR_T
*laststart
= 0;
2333 /* Address of beginning of regexp, or inside of last group. */
2336 /* Address of the place where a forward jump should go to the end of
2337 the containing expression. Each alternative of an `or' -- except the
2338 last -- ends with a forward jump of this sort. */
2339 UCHAR_T
*fixup_alt_jump
= 0;
2341 /* Counts open-groups as they are encountered. Remembered for the
2342 matching close-group on the compile stack, so the same register
2343 number is put in the stop_memory as the start_memory. */
2344 regnum_t regnum
= 0;
2347 /* Initialize the wchar_t PATTERN and offset_buffer. */
2348 p
= pend
= pattern
= TALLOC(csize
+ 1, CHAR_T
);
2349 mbs_offset
= TALLOC(csize
+ 1, int);
2350 is_binary
= TALLOC(csize
+ 1, char);
2351 if (pattern
== NULL
|| mbs_offset
== NULL
|| is_binary
== NULL
)
2358 pattern
[csize
] = L
'\0'; /* sentinel */
2359 size
= convert_mbs_to_wcs(pattern
, cpattern
, csize
, mbs_offset
, is_binary
);
2371 DEBUG_PRINT1 ("\nCompiling pattern: ");
2374 unsigned debug_count
;
2376 for (debug_count
= 0; debug_count
< size
; debug_count
++)
2377 PUT_CHAR (pattern
[debug_count
]);
2382 /* Initialize the compile stack. */
2383 compile_stack
.stack
= TALLOC (INIT_COMPILE_STACK_SIZE
, compile_stack_elt_t
);
2384 if (compile_stack
.stack
== NULL
)
2394 compile_stack
.size
= INIT_COMPILE_STACK_SIZE
;
2395 compile_stack
.avail
= 0;
2397 /* Initialize the pattern buffer. */
2398 bufp
->syntax
= syntax
;
2399 bufp
->fastmap_accurate
= 0;
2400 bufp
->not_bol
= bufp
->not_eol
= 0;
2402 /* Set `used' to zero, so that if we return an error, the pattern
2403 printer (for debugging) will think there's no pattern. We reset it
2407 /* Always count groups, whether or not bufp->no_sub is set. */
2410 #if !defined emacs && !defined SYNTAX_TABLE
2411 /* Initialize the syntax table. */
2412 init_syntax_once ();
2415 if (bufp
->allocated
== 0)
2418 { /* If zero allocated, but buffer is non-null, try to realloc
2419 enough space. This loses if buffer's address is bogus, but
2420 that is the user's responsibility. */
2422 /* Free bufp->buffer and allocate an array for wchar_t pattern
2425 COMPILED_BUFFER_VAR
= TALLOC (INIT_BUF_SIZE
/sizeof(UCHAR_T
),
2428 RETALLOC (COMPILED_BUFFER_VAR
, INIT_BUF_SIZE
, UCHAR_T
);
2432 { /* Caller did not allocate a buffer. Do it for them. */
2433 COMPILED_BUFFER_VAR
= TALLOC (INIT_BUF_SIZE
/ sizeof(UCHAR_T
),
2437 if (!COMPILED_BUFFER_VAR
) FREE_STACK_RETURN (REG_ESPACE
);
2439 bufp
->buffer
= (char*)COMPILED_BUFFER_VAR
;
2441 bufp
->allocated
= INIT_BUF_SIZE
;
2445 COMPILED_BUFFER_VAR
= (UCHAR_T
*) bufp
->buffer
;
2448 begalt
= b
= COMPILED_BUFFER_VAR
;
2450 /* Loop through the uncompiled pattern until we're at the end. */
2459 if ( /* If at start of pattern, it's an operator. */
2461 /* If context independent, it's an operator. */
2462 || syntax
& RE_CONTEXT_INDEP_ANCHORS
2463 /* Otherwise, depends on what's come before. */
2464 || PREFIX(at_begline_loc_p
) (pattern
, p
, syntax
))
2474 if ( /* If at end of pattern, it's an operator. */
2476 /* If context independent, it's an operator. */
2477 || syntax
& RE_CONTEXT_INDEP_ANCHORS
2478 /* Otherwise, depends on what's next. */
2479 || PREFIX(at_endline_loc_p
) (p
, pend
, syntax
))
2489 if ((syntax
& RE_BK_PLUS_QM
)
2490 || (syntax
& RE_LIMITED_OPS
))
2494 /* If there is no previous pattern... */
2497 if (syntax
& RE_CONTEXT_INVALID_OPS
)
2498 FREE_STACK_RETURN (REG_BADRPT
);
2499 else if (!(syntax
& RE_CONTEXT_INDEP_OPS
))
2504 /* Are we optimizing this jump? */
2505 boolean keep_string_p
= false;
2507 /* 1 means zero (many) matches is allowed. */
2508 char zero_times_ok
= 0, many_times_ok
= 0;
2510 /* If there is a sequence of repetition chars, collapse it
2511 down to just one (the right one). We can't combine
2512 interval operators with these because of, e.g., `a{2}*',
2513 which should only match an even number of `a's. */
2517 zero_times_ok
|= c
!= '+';
2518 many_times_ok
|= c
!= '?';
2526 || (!(syntax
& RE_BK_PLUS_QM
) && (c
== '+' || c
== '?')))
2529 else if (syntax
& RE_BK_PLUS_QM
&& c
== '\\')
2531 if (p
== pend
) FREE_STACK_RETURN (REG_EESCAPE
);
2534 if (!(c1
== '+' || c1
== '?'))
2549 /* If we get here, we found another repeat character. */
2552 /* Star, etc. applied to an empty pattern is equivalent
2553 to an empty pattern. */
2557 /* Now we know whether or not zero matches is allowed
2558 and also whether or not two or more matches is allowed. */
2560 { /* More than one repetition is allowed, so put in at the
2561 end a backward relative jump from `b' to before the next
2562 jump we're going to put in below (which jumps from
2563 laststart to after this jump).
2565 But if we are at the `*' in the exact sequence `.*\n',
2566 insert an unconditional jump backwards to the .,
2567 instead of the beginning of the loop. This way we only
2568 push a failure point once, instead of every time
2569 through the loop. */
2570 assert (p
- 1 > pattern
);
2572 /* Allocate the space for the jump. */
2573 GET_BUFFER_SPACE (1 + OFFSET_ADDRESS_SIZE
);
2575 /* We know we are not at the first character of the pattern,
2576 because laststart was nonzero. And we've already
2577 incremented `p', by the way, to be the character after
2578 the `*'. Do we have to do something analogous here
2579 for null bytes, because of RE_DOT_NOT_NULL? */
2580 if (TRANSLATE (*(p
- 2)) == TRANSLATE ('.')
2582 && p
< pend
&& TRANSLATE (*p
) == TRANSLATE ('\n')
2583 && !(syntax
& RE_DOT_NEWLINE
))
2584 { /* We have .*\n. */
2585 STORE_JUMP (jump
, b
, laststart
);
2586 keep_string_p
= true;
2589 /* Anything else. */
2590 STORE_JUMP (maybe_pop_jump
, b
, laststart
-
2591 (1 + OFFSET_ADDRESS_SIZE
));
2593 /* We've added more stuff to the buffer. */
2594 b
+= 1 + OFFSET_ADDRESS_SIZE
;
2597 /* On failure, jump from laststart to b + 3, which will be the
2598 end of the buffer after this jump is inserted. */
2599 /* ifdef WCHAR, 'b + 1 + OFFSET_ADDRESS_SIZE' instead of
2601 GET_BUFFER_SPACE (1 + OFFSET_ADDRESS_SIZE
);
2602 INSERT_JUMP (keep_string_p
? on_failure_keep_string_jump
2604 laststart
, b
+ 1 + OFFSET_ADDRESS_SIZE
);
2606 b
+= 1 + OFFSET_ADDRESS_SIZE
;
2610 /* At least one repetition is required, so insert a
2611 `dummy_failure_jump' before the initial
2612 `on_failure_jump' instruction of the loop. This
2613 effects a skip over that instruction the first time
2614 we hit that loop. */
2615 GET_BUFFER_SPACE (1 + OFFSET_ADDRESS_SIZE
);
2616 INSERT_JUMP (dummy_failure_jump
, laststart
, laststart
+
2617 2 + 2 * OFFSET_ADDRESS_SIZE
);
2618 b
+= 1 + OFFSET_ADDRESS_SIZE
;
2632 boolean had_char_class
= false;
2634 CHAR_T range_start
= 0xffffffff;
2636 unsigned int range_start
= 0xffffffff;
2638 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2641 /* We assume a charset(_not) structure as a wchar_t array.
2642 charset[0] = (re_opcode_t) charset(_not)
2643 charset[1] = l (= length of char_classes)
2644 charset[2] = m (= length of collating_symbols)
2645 charset[3] = n (= length of equivalence_classes)
2646 charset[4] = o (= length of char_ranges)
2647 charset[5] = p (= length of chars)
2649 charset[6] = char_class (wctype_t)
2650 charset[6+CHAR_CLASS_SIZE] = char_class (wctype_t)
2652 charset[l+5] = char_class (wctype_t)
2654 charset[l+6] = collating_symbol (wchar_t)
2656 charset[l+m+5] = collating_symbol (wchar_t)
2657 ifdef _LIBC we use the index if
2658 _NL_COLLATE_SYMB_EXTRAMB instead of
2661 charset[l+m+6] = equivalence_classes (wchar_t)
2663 charset[l+m+n+5] = equivalence_classes (wchar_t)
2664 ifdef _LIBC we use the index in
2665 _NL_COLLATE_WEIGHT instead of
2668 charset[l+m+n+6] = range_start
2669 charset[l+m+n+7] = range_end
2671 charset[l+m+n+2o+4] = range_start
2672 charset[l+m+n+2o+5] = range_end
2673 ifdef _LIBC we use the value looked up
2674 in _NL_COLLATE_COLLSEQ instead of
2677 charset[l+m+n+2o+6] = char
2679 charset[l+m+n+2o+p+5] = char
2683 /* We need at least 6 spaces: the opcode, the length of
2684 char_classes, the length of collating_symbols, the length of
2685 equivalence_classes, the length of char_ranges, the length of
2687 GET_BUFFER_SPACE (6);
2689 /* Save b as laststart. And We use laststart as the pointer
2690 to the first element of the charset here.
2691 In other words, laststart[i] indicates charset[i]. */
2694 /* We test `*p == '^' twice, instead of using an if
2695 statement, so we only need one BUF_PUSH. */
2696 BUF_PUSH (*p
== '^' ? charset_not
: charset
);
2700 /* Push the length of char_classes, the length of
2701 collating_symbols, the length of equivalence_classes, the
2702 length of char_ranges and the length of chars. */
2703 BUF_PUSH_3 (0, 0, 0);
2706 /* Remember the first position in the bracket expression. */
2709 /* charset_not matches newline according to a syntax bit. */
2710 if ((re_opcode_t
) b
[-6] == charset_not
2711 && (syntax
& RE_HAT_LISTS_NOT_NEWLINE
))
2714 laststart
[5]++; /* Update the length of characters */
2717 /* Read in characters and ranges, setting map bits. */
2720 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2724 /* \ might escape characters inside [...] and [^...]. */
2725 if ((syntax
& RE_BACKSLASH_ESCAPE_IN_LISTS
) && c
== '\\')
2727 if (p
== pend
) FREE_STACK_RETURN (REG_EESCAPE
);
2731 laststart
[5]++; /* Update the length of chars */
2736 /* Could be the end of the bracket expression. If it's
2737 not (i.e., when the bracket expression is `[]' so
2738 far), the ']' character bit gets set way below. */
2739 if (c
== ']' && p
!= p1
+ 1)
2742 /* Look ahead to see if it's a range when the last thing
2743 was a character class. */
2744 if (had_char_class
&& c
== '-' && *p
!= ']')
2745 FREE_STACK_RETURN (REG_ERANGE
);
2747 /* Look ahead to see if it's a range when the last thing
2748 was a character: if this is a hyphen not at the
2749 beginning or the end of a list, then it's the range
2752 && !(p
- 2 >= pattern
&& p
[-2] == '[')
2753 && !(p
- 3 >= pattern
&& p
[-3] == '[' && p
[-2] == '^')
2757 /* Allocate the space for range_start and range_end. */
2758 GET_BUFFER_SPACE (2);
2759 /* Update the pointer to indicate end of buffer. */
2761 ret
= wcs_compile_range (range_start
, &p
, pend
, translate
,
2762 syntax
, b
, laststart
);
2763 if (ret
!= REG_NOERROR
) FREE_STACK_RETURN (ret
);
2764 range_start
= 0xffffffff;
2766 else if (p
[0] == '-' && p
[1] != ']')
2767 { /* This handles ranges made up of characters only. */
2770 /* Move past the `-'. */
2772 /* Allocate the space for range_start and range_end. */
2773 GET_BUFFER_SPACE (2);
2774 /* Update the pointer to indicate end of buffer. */
2776 ret
= wcs_compile_range (c
, &p
, pend
, translate
, syntax
, b
,
2778 if (ret
!= REG_NOERROR
) FREE_STACK_RETURN (ret
);
2779 range_start
= 0xffffffff;
2782 /* See if we're at the beginning of a possible character
2784 else if (syntax
& RE_CHAR_CLASSES
&& c
== '[' && *p
== ':')
2785 { /* Leave room for the null. */
2786 char str
[CHAR_CLASS_MAX_LENGTH
+ 1];
2791 /* If pattern is `[[:'. */
2792 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2797 if ((c
== ':' && *p
== ']') || p
== pend
)
2799 if (c1
< CHAR_CLASS_MAX_LENGTH
)
2802 /* This is in any case an invalid class name. */
2807 /* If isn't a word bracketed by `[:' and `:]':
2808 undo the ending character, the letters, and leave
2809 the leading `:' and `[' (but store them as character). */
2810 if (c
== ':' && *p
== ']')
2815 /* Query the character class as wctype_t. */
2816 wt
= IS_CHAR_CLASS (str
);
2818 FREE_STACK_RETURN (REG_ECTYPE
);
2820 /* Throw away the ] at the end of the character
2824 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2826 /* Allocate the space for character class. */
2827 GET_BUFFER_SPACE(CHAR_CLASS_SIZE
);
2828 /* Update the pointer to indicate end of buffer. */
2829 b
+= CHAR_CLASS_SIZE
;
2830 /* Move data which follow character classes
2831 not to violate the data. */
2832 insert_space(CHAR_CLASS_SIZE
,
2833 laststart
+ 6 + laststart
[1],
2835 alignedp
= ((uintptr_t)(laststart
+ 6 + laststart
[1])
2836 + __alignof__(wctype_t) - 1)
2837 & ~(uintptr_t)(__alignof__(wctype_t) - 1);
2838 /* Store the character class. */
2839 *((wctype_t*)alignedp
) = wt
;
2840 /* Update length of char_classes */
2841 laststart
[1] += CHAR_CLASS_SIZE
;
2843 had_char_class
= true;
2852 laststart
[5] += 2; /* Update the length of characters */
2854 had_char_class
= false;
2857 else if (syntax
& RE_CHAR_CLASSES
&& c
== '[' && (*p
== '='
2860 CHAR_T str
[128]; /* Should be large enough. */
2861 CHAR_T delim
= *p
; /* '=' or '.' */
2864 _NL_CURRENT_WORD (LC_COLLATE
, _NL_COLLATE_NRULES
);
2869 /* If pattern is `[[=' or '[[.'. */
2870 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2875 if ((c
== delim
&& *p
== ']') || p
== pend
)
2877 if (c1
< sizeof (str
) - 1)
2880 /* This is in any case an invalid class name. */
2885 if (c
== delim
&& *p
== ']' && str
[0] != '\0')
2887 unsigned int i
, offset
;
2888 /* If we have no collation data we use the default
2889 collation in which each character is in a class
2890 by itself. It also means that ASCII is the
2891 character set and therefore we cannot have character
2892 with more than one byte in the multibyte
2895 /* If not defined _LIBC, we push the name and
2896 `\0' for the sake of matching performance. */
2897 int datasize
= c1
+ 1;
2905 FREE_STACK_RETURN (REG_ECOLLATE
);
2910 const int32_t *table
;
2911 const int32_t *weights
;
2912 const int32_t *extra
;
2913 const int32_t *indirect
;
2916 /* This #include defines a local function! */
2917 # include <locale/weightwc.h>
2921 /* We push the index for equivalence class. */
2924 table
= (const int32_t *)
2925 _NL_CURRENT (LC_COLLATE
,
2926 _NL_COLLATE_TABLEWC
);
2927 weights
= (const int32_t *)
2928 _NL_CURRENT (LC_COLLATE
,
2929 _NL_COLLATE_WEIGHTWC
);
2930 extra
= (const int32_t *)
2931 _NL_CURRENT (LC_COLLATE
,
2932 _NL_COLLATE_EXTRAWC
);
2933 indirect
= (const int32_t *)
2934 _NL_CURRENT (LC_COLLATE
,
2935 _NL_COLLATE_INDIRECTWC
);
2937 idx
= findidx ((const wint_t**)&cp
);
2938 if (idx
== 0 || cp
< (wint_t*) str
+ c1
)
2939 /* This is no valid character. */
2940 FREE_STACK_RETURN (REG_ECOLLATE
);
2942 str
[0] = (wchar_t)idx
;
2944 else /* delim == '.' */
2946 /* We push collation sequence value
2947 for collating symbol. */
2949 const int32_t *symb_table
;
2950 const unsigned char *extra
;
2957 /* We have to convert the name to a single-byte
2958 string. This is possible since the names
2959 consist of ASCII characters and the internal
2960 representation is UCS4. */
2961 for (i
= 0; i
< c1
; ++i
)
2962 char_str
[i
] = str
[i
];
2965 _NL_CURRENT_WORD (LC_COLLATE
,
2966 _NL_COLLATE_SYMB_HASH_SIZEMB
);
2967 symb_table
= (const int32_t *)
2968 _NL_CURRENT (LC_COLLATE
,
2969 _NL_COLLATE_SYMB_TABLEMB
);
2970 extra
= (const unsigned char *)
2971 _NL_CURRENT (LC_COLLATE
,
2972 _NL_COLLATE_SYMB_EXTRAMB
);
2974 /* Locate the character in the hashing table. */
2975 hash
= elem_hash (char_str
, c1
);
2978 elem
= hash
% table_size
;
2979 second
= hash
% (table_size
- 2);
2980 while (symb_table
[2 * elem
] != 0)
2982 /* First compare the hashing value. */
2983 if (symb_table
[2 * elem
] == hash
2984 && c1
== extra
[symb_table
[2 * elem
+ 1]]
2985 && memcmp (char_str
,
2986 &extra
[symb_table
[2 * elem
+ 1]
2989 /* Yep, this is the entry. */
2990 idx
= symb_table
[2 * elem
+ 1];
2991 idx
+= 1 + extra
[idx
];
2999 if (symb_table
[2 * elem
] != 0)
3001 /* Compute the index of the byte sequence
3003 idx
+= 1 + extra
[idx
];
3004 /* Adjust for the alignment. */
3005 idx
= (idx
+ 3) & ~3;
3007 str
[0] = (wchar_t) idx
+ 4;
3009 else if (symb_table
[2 * elem
] == 0 && c1
== 1)
3011 /* No valid character. Match it as a
3012 single byte character. */
3013 had_char_class
= false;
3015 /* Update the length of characters */
3017 range_start
= str
[0];
3019 /* Throw away the ] at the end of the
3020 collating symbol. */
3022 /* exit from the switch block. */
3026 FREE_STACK_RETURN (REG_ECOLLATE
);
3031 /* Throw away the ] at the end of the equivalence
3032 class (or collating symbol). */
3035 /* Allocate the space for the equivalence class
3036 (or collating symbol) (and '\0' if needed). */
3037 GET_BUFFER_SPACE(datasize
);
3038 /* Update the pointer to indicate end of buffer. */
3042 { /* equivalence class */
3043 /* Calculate the offset of char_ranges,
3044 which is next to equivalence_classes. */
3045 offset
= laststart
[1] + laststart
[2]
3048 insert_space(datasize
, laststart
+ offset
, b
- 1);
3050 /* Write the equivalence_class and \0. */
3051 for (i
= 0 ; i
< datasize
; i
++)
3052 laststart
[offset
+ i
] = str
[i
];
3054 /* Update the length of equivalence_classes. */
3055 laststart
[3] += datasize
;
3056 had_char_class
= true;
3058 else /* delim == '.' */
3059 { /* collating symbol */
3060 /* Calculate the offset of the equivalence_classes,
3061 which is next to collating_symbols. */
3062 offset
= laststart
[1] + laststart
[2] + 6;
3063 /* Insert space and write the collationg_symbol
3065 insert_space(datasize
, laststart
+ offset
, b
-1);
3066 for (i
= 0 ; i
< datasize
; i
++)
3067 laststart
[offset
+ i
] = str
[i
];
3069 /* In re_match_2_internal if range_start < -1, we
3070 assume -range_start is the offset of the
3071 collating symbol which is specified as
3072 the character of the range start. So we assign
3073 -(laststart[1] + laststart[2] + 6) to
3075 range_start
= -(laststart
[1] + laststart
[2] + 6);
3076 /* Update the length of collating_symbol. */
3077 laststart
[2] += datasize
;
3078 had_char_class
= false;
3088 laststart
[5] += 2; /* Update the length of characters */
3089 range_start
= delim
;
3090 had_char_class
= false;
3095 had_char_class
= false;
3097 laststart
[5]++; /* Update the length of characters */
3103 /* Ensure that we have enough space to push a charset: the
3104 opcode, the length count, and the bitset; 34 bytes in all. */
3105 GET_BUFFER_SPACE (34);
3109 /* We test `*p == '^' twice, instead of using an if
3110 statement, so we only need one BUF_PUSH. */
3111 BUF_PUSH (*p
== '^' ? charset_not
: charset
);
3115 /* Remember the first position in the bracket expression. */
3118 /* Push the number of bytes in the bitmap. */
3119 BUF_PUSH ((1 << BYTEWIDTH
) / BYTEWIDTH
);
3121 /* Clear the whole map. */
3122 bzero (b
, (1 << BYTEWIDTH
) / BYTEWIDTH
);
3124 /* charset_not matches newline according to a syntax bit. */
3125 if ((re_opcode_t
) b
[-2] == charset_not
3126 && (syntax
& RE_HAT_LISTS_NOT_NEWLINE
))
3127 SET_LIST_BIT ('\n');
3129 /* Read in characters and ranges, setting map bits. */
3132 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
3136 /* \ might escape characters inside [...] and [^...]. */
3137 if ((syntax
& RE_BACKSLASH_ESCAPE_IN_LISTS
) && c
== '\\')
3139 if (p
== pend
) FREE_STACK_RETURN (REG_EESCAPE
);
3147 /* Could be the end of the bracket expression. If it's
3148 not (i.e., when the bracket expression is `[]' so
3149 far), the ']' character bit gets set way below. */
3150 if (c
== ']' && p
!= p1
+ 1)
3153 /* Look ahead to see if it's a range when the last thing
3154 was a character class. */
3155 if (had_char_class
&& c
== '-' && *p
!= ']')
3156 FREE_STACK_RETURN (REG_ERANGE
);
3158 /* Look ahead to see if it's a range when the last thing
3159 was a character: if this is a hyphen not at the
3160 beginning or the end of a list, then it's the range
3163 && !(p
- 2 >= pattern
&& p
[-2] == '[')
3164 && !(p
- 3 >= pattern
&& p
[-3] == '[' && p
[-2] == '^')
3168 = byte_compile_range (range_start
, &p
, pend
, translate
,
3170 if (ret
!= REG_NOERROR
) FREE_STACK_RETURN (ret
);
3171 range_start
= 0xffffffff;
3174 else if (p
[0] == '-' && p
[1] != ']')
3175 { /* This handles ranges made up of characters only. */
3178 /* Move past the `-'. */
3181 ret
= byte_compile_range (c
, &p
, pend
, translate
, syntax
, b
);
3182 if (ret
!= REG_NOERROR
) FREE_STACK_RETURN (ret
);
3183 range_start
= 0xffffffff;
3186 /* See if we're at the beginning of a possible character
3189 else if (syntax
& RE_CHAR_CLASSES
&& c
== '[' && *p
== ':')
3190 { /* Leave room for the null. */
3191 char str
[CHAR_CLASS_MAX_LENGTH
+ 1];
3196 /* If pattern is `[[:'. */
3197 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
3202 if ((c
== ':' && *p
== ']') || p
== pend
)
3204 if (c1
< CHAR_CLASS_MAX_LENGTH
)
3207 /* This is in any case an invalid class name. */
3212 /* If isn't a word bracketed by `[:' and `:]':
3213 undo the ending character, the letters, and leave
3214 the leading `:' and `[' (but set bits for them). */
3215 if (c
== ':' && *p
== ']')
3217 # if defined _LIBC || WIDE_CHAR_SUPPORT
3218 boolean is_lower
= STREQ (str
, "lower");
3219 boolean is_upper
= STREQ (str
, "upper");
3223 wt
= IS_CHAR_CLASS (str
);
3225 FREE_STACK_RETURN (REG_ECTYPE
);
3227 /* Throw away the ] at the end of the character
3231 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
3233 for (ch
= 0; ch
< 1 << BYTEWIDTH
; ++ch
)
3236 if (__iswctype (__btowc (ch
), wt
))
3239 if (iswctype (btowc (ch
), wt
))
3243 if (translate
&& (is_upper
|| is_lower
)
3244 && (ISUPPER (ch
) || ISLOWER (ch
)))
3248 had_char_class
= true;
3251 boolean is_alnum
= STREQ (str
, "alnum");
3252 boolean is_alpha
= STREQ (str
, "alpha");
3253 boolean is_blank
= STREQ (str
, "blank");
3254 boolean is_cntrl
= STREQ (str
, "cntrl");
3255 boolean is_digit
= STREQ (str
, "digit");
3256 boolean is_graph
= STREQ (str
, "graph");
3257 boolean is_lower
= STREQ (str
, "lower");
3258 boolean is_print
= STREQ (str
, "print");
3259 boolean is_punct
= STREQ (str
, "punct");
3260 boolean is_space
= STREQ (str
, "space");
3261 boolean is_upper
= STREQ (str
, "upper");
3262 boolean is_xdigit
= STREQ (str
, "xdigit");
3264 if (!IS_CHAR_CLASS (str
))
3265 FREE_STACK_RETURN (REG_ECTYPE
);
3267 /* Throw away the ] at the end of the character
3271 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
3273 for (ch
= 0; ch
< 1 << BYTEWIDTH
; ch
++)
3275 /* This was split into 3 if's to
3276 avoid an arbitrary limit in some compiler. */
3277 if ( (is_alnum
&& ISALNUM (ch
))
3278 || (is_alpha
&& ISALPHA (ch
))
3279 || (is_blank
&& ISBLANK (ch
))
3280 || (is_cntrl
&& ISCNTRL (ch
)))
3282 if ( (is_digit
&& ISDIGIT (ch
))
3283 || (is_graph
&& ISGRAPH (ch
))
3284 || (is_lower
&& ISLOWER (ch
))
3285 || (is_print
&& ISPRINT (ch
)))
3287 if ( (is_punct
&& ISPUNCT (ch
))
3288 || (is_space
&& ISSPACE (ch
))
3289 || (is_upper
&& ISUPPER (ch
))
3290 || (is_xdigit
&& ISXDIGIT (ch
)))
3292 if ( translate
&& (is_upper
|| is_lower
)
3293 && (ISUPPER (ch
) || ISLOWER (ch
)))
3296 had_char_class
= true;
3297 # endif /* libc || wctype.h */
3307 had_char_class
= false;
3310 else if (syntax
& RE_CHAR_CLASSES
&& c
== '[' && *p
== '=')
3312 unsigned char str
[MB_LEN_MAX
+ 1];
3315 _NL_CURRENT_WORD (LC_COLLATE
, _NL_COLLATE_NRULES
);
3321 /* If pattern is `[[='. */
3322 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
3327 if ((c
== '=' && *p
== ']') || p
== pend
)
3329 if (c1
< MB_LEN_MAX
)
3332 /* This is in any case an invalid class name. */
3337 if (c
== '=' && *p
== ']' && str
[0] != '\0')
3339 /* If we have no collation data we use the default
3340 collation in which each character is in a class
3341 by itself. It also means that ASCII is the
3342 character set and therefore we cannot have character
3343 with more than one byte in the multibyte
3350 FREE_STACK_RETURN (REG_ECOLLATE
);
3352 /* Throw away the ] at the end of the equivalence
3356 /* Set the bit for the character. */
3357 SET_LIST_BIT (str
[0]);
3362 /* Try to match the byte sequence in `str' against
3363 those known to the collate implementation.
3364 First find out whether the bytes in `str' are
3365 actually from exactly one character. */
3366 const int32_t *table
;
3367 const unsigned char *weights
;
3368 const unsigned char *extra
;
3369 const int32_t *indirect
;
3371 const unsigned char *cp
= str
;
3374 /* This #include defines a local function! */
3375 # include <locale/weight.h>
3377 table
= (const int32_t *)
3378 _NL_CURRENT (LC_COLLATE
, _NL_COLLATE_TABLEMB
);
3379 weights
= (const unsigned char *)
3380 _NL_CURRENT (LC_COLLATE
, _NL_COLLATE_WEIGHTMB
);
3381 extra
= (const unsigned char *)
3382 _NL_CURRENT (LC_COLLATE
, _NL_COLLATE_EXTRAMB
);
3383 indirect
= (const int32_t *)
3384 _NL_CURRENT (LC_COLLATE
, _NL_COLLATE_INDIRECTMB
);
3386 idx
= findidx (&cp
);
3387 if (idx
== 0 || cp
< str
+ c1
)
3388 /* This is no valid character. */
3389 FREE_STACK_RETURN (REG_ECOLLATE
);
3391 /* Throw away the ] at the end of the equivalence
3395 /* Now we have to go throught the whole table
3396 and find all characters which have the same
3399 XXX Note that this is not entirely correct.
3400 we would have to match multibyte sequences
3401 but this is not possible with the current
3403 for (ch
= 1; ch
< 256; ++ch
)
3404 /* XXX This test would have to be changed if we
3405 would allow matching multibyte sequences. */
3408 int32_t idx2
= table
[ch
];
3409 size_t len
= weights
[idx2
];
3411 /* Test whether the lenghts match. */
3412 if (weights
[idx
] == len
)
3414 /* They do. New compare the bytes of
3419 && (weights
[idx
+ 1 + cnt
]
3420 == weights
[idx2
+ 1 + cnt
]))
3424 /* They match. Mark the character as
3431 had_char_class
= true;
3441 had_char_class
= false;
3444 else if (syntax
& RE_CHAR_CLASSES
&& c
== '[' && *p
== '.')
3446 unsigned char str
[128]; /* Should be large enough. */
3449 _NL_CURRENT_WORD (LC_COLLATE
, _NL_COLLATE_NRULES
);
3455 /* If pattern is `[[.'. */
3456 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
3461 if ((c
== '.' && *p
== ']') || p
== pend
)
3463 if (c1
< sizeof (str
))
3466 /* This is in any case an invalid class name. */
3471 if (c
== '.' && *p
== ']' && str
[0] != '\0')
3473 /* If we have no collation data we use the default
3474 collation in which each character is the name
3475 for its own class which contains only the one
3476 character. It also means that ASCII is the
3477 character set and therefore we cannot have character
3478 with more than one byte in the multibyte
3485 FREE_STACK_RETURN (REG_ECOLLATE
);
3487 /* Throw away the ] at the end of the equivalence
3491 /* Set the bit for the character. */
3492 SET_LIST_BIT (str
[0]);
3493 range_start
= ((const unsigned char *) str
)[0];
3498 /* Try to match the byte sequence in `str' against
3499 those known to the collate implementation.
3500 First find out whether the bytes in `str' are
3501 actually from exactly one character. */
3503 const int32_t *symb_table
;
3504 const unsigned char *extra
;
3511 _NL_CURRENT_WORD (LC_COLLATE
,
3512 _NL_COLLATE_SYMB_HASH_SIZEMB
);
3513 symb_table
= (const int32_t *)
3514 _NL_CURRENT (LC_COLLATE
,
3515 _NL_COLLATE_SYMB_TABLEMB
);
3516 extra
= (const unsigned char *)
3517 _NL_CURRENT (LC_COLLATE
,
3518 _NL_COLLATE_SYMB_EXTRAMB
);
3520 /* Locate the character in the hashing table. */
3521 hash
= elem_hash (str
, c1
);
3524 elem
= hash
% table_size
;
3525 second
= hash
% (table_size
- 2);
3526 while (symb_table
[2 * elem
] != 0)
3528 /* First compare the hashing value. */
3529 if (symb_table
[2 * elem
] == hash
3530 && c1
== extra
[symb_table
[2 * elem
+ 1]]
3532 &extra
[symb_table
[2 * elem
+ 1]
3536 /* Yep, this is the entry. */
3537 idx
= symb_table
[2 * elem
+ 1];
3538 idx
+= 1 + extra
[idx
];
3546 if (symb_table
[2 * elem
] == 0)
3547 /* This is no valid character. */
3548 FREE_STACK_RETURN (REG_ECOLLATE
);
3550 /* Throw away the ] at the end of the equivalence
3554 /* Now add the multibyte character(s) we found
3557 XXX Note that this is not entirely correct.
3558 we would have to match multibyte sequences
3559 but this is not possible with the current
3560 implementation. Also, we have to match
3561 collating symbols, which expand to more than
3562 one file, as a whole and not allow the
3563 individual bytes. */
3566 range_start
= extra
[idx
];
3569 SET_LIST_BIT (extra
[idx
]);
3574 had_char_class
= false;
3584 had_char_class
= false;
3589 had_char_class
= false;
3595 /* Discard any (non)matching list bytes that are all 0 at the
3596 end of the map. Decrease the map-length byte too. */
3597 while ((int) b
[-1] > 0 && b
[b
[-1] - 1] == 0)
3606 if (syntax
& RE_NO_BK_PARENS
)
3613 if (syntax
& RE_NO_BK_PARENS
)
3620 if (syntax
& RE_NEWLINE_ALT
)
3627 if (syntax
& RE_NO_BK_VBAR
)
3634 if (syntax
& RE_INTERVALS
&& syntax
& RE_NO_BK_BRACES
)
3635 goto handle_interval
;
3641 if (p
== pend
) FREE_STACK_RETURN (REG_EESCAPE
);
3643 /* Do not translate the character after the \, so that we can
3644 distinguish, e.g., \B from \b, even if we normally would
3645 translate, e.g., B to b. */
3651 if (syntax
& RE_NO_BK_PARENS
)
3652 goto normal_backslash
;
3658 if (COMPILE_STACK_FULL
)
3660 RETALLOC (compile_stack
.stack
, compile_stack
.size
<< 1,
3661 compile_stack_elt_t
);
3662 if (compile_stack
.stack
== NULL
) return REG_ESPACE
;
3664 compile_stack
.size
<<= 1;
3667 /* These are the values to restore when we hit end of this
3668 group. They are all relative offsets, so that if the
3669 whole pattern moves because of realloc, they will still
3671 COMPILE_STACK_TOP
.begalt_offset
= begalt
- COMPILED_BUFFER_VAR
;
3672 COMPILE_STACK_TOP
.fixup_alt_jump
3673 = fixup_alt_jump
? fixup_alt_jump
- COMPILED_BUFFER_VAR
+ 1 : 0;
3674 COMPILE_STACK_TOP
.laststart_offset
= b
- COMPILED_BUFFER_VAR
;
3675 COMPILE_STACK_TOP
.regnum
= regnum
;
3677 /* We will eventually replace the 0 with the number of
3678 groups inner to this one. But do not push a
3679 start_memory for groups beyond the last one we can
3680 represent in the compiled pattern. */
3681 if (regnum
<= MAX_REGNUM
)
3683 COMPILE_STACK_TOP
.inner_group_offset
= b
3684 - COMPILED_BUFFER_VAR
+ 2;
3685 BUF_PUSH_3 (start_memory
, regnum
, 0);
3688 compile_stack
.avail
++;
3693 /* If we've reached MAX_REGNUM groups, then this open
3694 won't actually generate any code, so we'll have to
3695 clear pending_exact explicitly. */
3701 if (syntax
& RE_NO_BK_PARENS
) goto normal_backslash
;
3703 if (COMPILE_STACK_EMPTY
)
3705 if (syntax
& RE_UNMATCHED_RIGHT_PAREN_ORD
)
3706 goto normal_backslash
;
3708 FREE_STACK_RETURN (REG_ERPAREN
);
3713 { /* Push a dummy failure point at the end of the
3714 alternative for a possible future
3715 `pop_failure_jump' to pop. See comments at
3716 `push_dummy_failure' in `re_match_2'. */
3717 BUF_PUSH (push_dummy_failure
);
3719 /* We allocated space for this jump when we assigned
3720 to `fixup_alt_jump', in the `handle_alt' case below. */
3721 STORE_JUMP (jump_past_alt
, fixup_alt_jump
, b
- 1);
3724 /* See similar code for backslashed left paren above. */
3725 if (COMPILE_STACK_EMPTY
)
3727 if (syntax
& RE_UNMATCHED_RIGHT_PAREN_ORD
)
3730 FREE_STACK_RETURN (REG_ERPAREN
);
3733 /* Since we just checked for an empty stack above, this
3734 ``can't happen''. */
3735 assert (compile_stack
.avail
!= 0);
3737 /* We don't just want to restore into `regnum', because
3738 later groups should continue to be numbered higher,
3739 as in `(ab)c(de)' -- the second group is #2. */
3740 regnum_t this_group_regnum
;
3742 compile_stack
.avail
--;
3743 begalt
= COMPILED_BUFFER_VAR
+ COMPILE_STACK_TOP
.begalt_offset
;
3745 = COMPILE_STACK_TOP
.fixup_alt_jump
3746 ? COMPILED_BUFFER_VAR
+ COMPILE_STACK_TOP
.fixup_alt_jump
- 1
3748 laststart
= COMPILED_BUFFER_VAR
+ COMPILE_STACK_TOP
.laststart_offset
;
3749 this_group_regnum
= COMPILE_STACK_TOP
.regnum
;
3750 /* If we've reached MAX_REGNUM groups, then this open
3751 won't actually generate any code, so we'll have to
3752 clear pending_exact explicitly. */
3755 /* We're at the end of the group, so now we know how many
3756 groups were inside this one. */
3757 if (this_group_regnum
<= MAX_REGNUM
)
3759 UCHAR_T
*inner_group_loc
3760 = COMPILED_BUFFER_VAR
+ COMPILE_STACK_TOP
.inner_group_offset
;
3762 *inner_group_loc
= regnum
- this_group_regnum
;
3763 BUF_PUSH_3 (stop_memory
, this_group_regnum
,
3764 regnum
- this_group_regnum
);
3770 case '|': /* `\|'. */
3771 if (syntax
& RE_LIMITED_OPS
|| syntax
& RE_NO_BK_VBAR
)
3772 goto normal_backslash
;
3774 if (syntax
& RE_LIMITED_OPS
)
3777 /* Insert before the previous alternative a jump which
3778 jumps to this alternative if the former fails. */
3779 GET_BUFFER_SPACE (1 + OFFSET_ADDRESS_SIZE
);
3780 INSERT_JUMP (on_failure_jump
, begalt
,
3781 b
+ 2 + 2 * OFFSET_ADDRESS_SIZE
);
3783 b
+= 1 + OFFSET_ADDRESS_SIZE
;
3785 /* The alternative before this one has a jump after it
3786 which gets executed if it gets matched. Adjust that
3787 jump so it will jump to this alternative's analogous
3788 jump (put in below, which in turn will jump to the next
3789 (if any) alternative's such jump, etc.). The last such
3790 jump jumps to the correct final destination. A picture:
3796 If we are at `b', then fixup_alt_jump right now points to a
3797 three-byte space after `a'. We'll put in the jump, set
3798 fixup_alt_jump to right after `b', and leave behind three
3799 bytes which we'll fill in when we get to after `c'. */
3802 STORE_JUMP (jump_past_alt
, fixup_alt_jump
, b
);
3804 /* Mark and leave space for a jump after this alternative,
3805 to be filled in later either by next alternative or
3806 when know we're at the end of a series of alternatives. */
3808 GET_BUFFER_SPACE (1 + OFFSET_ADDRESS_SIZE
);
3809 b
+= 1 + OFFSET_ADDRESS_SIZE
;
3817 /* If \{ is a literal. */
3818 if (!(syntax
& RE_INTERVALS
)
3819 /* If we're at `\{' and it's not the open-interval
3821 || (syntax
& RE_NO_BK_BRACES
))
3822 goto normal_backslash
;
3826 /* If got here, then the syntax allows intervals. */
3828 /* At least (most) this many matches must be made. */
3829 int lower_bound
= -1, upper_bound
= -1;
3831 /* Place in the uncompiled pattern (i.e., just after
3832 the '{') to go back to if the interval is invalid. */
3833 const CHAR_T
*beg_interval
= p
;
3836 goto invalid_interval
;
3838 GET_UNSIGNED_NUMBER (lower_bound
);
3842 GET_UNSIGNED_NUMBER (upper_bound
);
3843 if (upper_bound
< 0)
3844 upper_bound
= RE_DUP_MAX
;
3847 /* Interval such as `{1}' => match exactly once. */
3848 upper_bound
= lower_bound
;
3850 if (! (0 <= lower_bound
&& lower_bound
<= upper_bound
))
3851 goto invalid_interval
;
3853 if (!(syntax
& RE_NO_BK_BRACES
))
3855 if (c
!= '\\' || p
== pend
)
3856 goto invalid_interval
;
3861 goto invalid_interval
;
3863 /* If it's invalid to have no preceding re. */
3866 if (syntax
& RE_CONTEXT_INVALID_OPS
3867 && !(syntax
& RE_INVALID_INTERVAL_ORD
))
3868 FREE_STACK_RETURN (REG_BADRPT
);
3869 else if (syntax
& RE_CONTEXT_INDEP_OPS
)
3872 goto unfetch_interval
;
3875 /* We just parsed a valid interval. */
3877 if (RE_DUP_MAX
< upper_bound
)
3878 FREE_STACK_RETURN (REG_BADBR
);
3880 /* If the upper bound is zero, don't want to succeed at
3881 all; jump from `laststart' to `b + 3', which will be
3882 the end of the buffer after we insert the jump. */
3883 /* ifdef WCHAR, 'b + 1 + OFFSET_ADDRESS_SIZE'
3884 instead of 'b + 3'. */
3885 if (upper_bound
== 0)
3887 GET_BUFFER_SPACE (1 + OFFSET_ADDRESS_SIZE
);
3888 INSERT_JUMP (jump
, laststart
, b
+ 1
3889 + OFFSET_ADDRESS_SIZE
);
3890 b
+= 1 + OFFSET_ADDRESS_SIZE
;
3893 /* Otherwise, we have a nontrivial interval. When
3894 we're all done, the pattern will look like:
3895 set_number_at <jump count> <upper bound>
3896 set_number_at <succeed_n count> <lower bound>
3897 succeed_n <after jump addr> <succeed_n count>
3899 jump_n <succeed_n addr> <jump count>
3900 (The upper bound and `jump_n' are omitted if
3901 `upper_bound' is 1, though.) */
3903 { /* If the upper bound is > 1, we need to insert
3904 more at the end of the loop. */
3905 unsigned nbytes
= 2 + 4 * OFFSET_ADDRESS_SIZE
+
3906 (upper_bound
> 1) * (2 + 4 * OFFSET_ADDRESS_SIZE
);
3908 GET_BUFFER_SPACE (nbytes
);
3910 /* Initialize lower bound of the `succeed_n', even
3911 though it will be set during matching by its
3912 attendant `set_number_at' (inserted next),
3913 because `re_compile_fastmap' needs to know.
3914 Jump to the `jump_n' we might insert below. */
3915 INSERT_JUMP2 (succeed_n
, laststart
,
3916 b
+ 1 + 2 * OFFSET_ADDRESS_SIZE
3917 + (upper_bound
> 1) * (1 + 2 * OFFSET_ADDRESS_SIZE
)
3919 b
+= 1 + 2 * OFFSET_ADDRESS_SIZE
;
3921 /* Code to initialize the lower bound. Insert
3922 before the `succeed_n'. The `5' is the last two
3923 bytes of this `set_number_at', plus 3 bytes of
3924 the following `succeed_n'. */
3925 /* ifdef WCHAR, The '1+2*OFFSET_ADDRESS_SIZE'
3926 is the 'set_number_at', plus '1+OFFSET_ADDRESS_SIZE'
3927 of the following `succeed_n'. */
3928 PREFIX(insert_op2
) (set_number_at
, laststart
, 1
3929 + 2 * OFFSET_ADDRESS_SIZE
, lower_bound
, b
);
3930 b
+= 1 + 2 * OFFSET_ADDRESS_SIZE
;
3932 if (upper_bound
> 1)
3933 { /* More than one repetition is allowed, so
3934 append a backward jump to the `succeed_n'
3935 that starts this interval.
3937 When we've reached this during matching,
3938 we'll have matched the interval once, so
3939 jump back only `upper_bound - 1' times. */
3940 STORE_JUMP2 (jump_n
, b
, laststart
3941 + 2 * OFFSET_ADDRESS_SIZE
+ 1,
3943 b
+= 1 + 2 * OFFSET_ADDRESS_SIZE
;
3945 /* The location we want to set is the second
3946 parameter of the `jump_n'; that is `b-2' as
3947 an absolute address. `laststart' will be
3948 the `set_number_at' we're about to insert;
3949 `laststart+3' the number to set, the source
3950 for the relative address. But we are
3951 inserting into the middle of the pattern --
3952 so everything is getting moved up by 5.
3953 Conclusion: (b - 2) - (laststart + 3) + 5,
3954 i.e., b - laststart.
3956 We insert this at the beginning of the loop
3957 so that if we fail during matching, we'll
3958 reinitialize the bounds. */
3959 PREFIX(insert_op2
) (set_number_at
, laststart
,
3961 upper_bound
- 1, b
);
3962 b
+= 1 + 2 * OFFSET_ADDRESS_SIZE
;
3969 if (!(syntax
& RE_INVALID_INTERVAL_ORD
))
3970 FREE_STACK_RETURN (p
== pend
? REG_EBRACE
: REG_BADBR
);
3972 /* Match the characters as literals. */
3975 if (syntax
& RE_NO_BK_BRACES
)
3978 goto normal_backslash
;
3982 /* There is no way to specify the before_dot and after_dot
3983 operators. rms says this is ok. --karl */
3991 BUF_PUSH_2 (syntaxspec
, syntax_spec_code
[c
]);
3997 BUF_PUSH_2 (notsyntaxspec
, syntax_spec_code
[c
]);
4003 if (syntax
& RE_NO_GNU_OPS
)
4006 BUF_PUSH (wordchar
);
4011 if (syntax
& RE_NO_GNU_OPS
)
4014 BUF_PUSH (notwordchar
);
4019 if (syntax
& RE_NO_GNU_OPS
)
4025 if (syntax
& RE_NO_GNU_OPS
)
4031 if (syntax
& RE_NO_GNU_OPS
)
4033 BUF_PUSH (wordbound
);
4037 if (syntax
& RE_NO_GNU_OPS
)
4039 BUF_PUSH (notwordbound
);
4043 if (syntax
& RE_NO_GNU_OPS
)
4049 if (syntax
& RE_NO_GNU_OPS
)
4054 case '1': case '2': case '3': case '4': case '5':
4055 case '6': case '7': case '8': case '9':
4056 if (syntax
& RE_NO_BK_REFS
)
4062 FREE_STACK_RETURN (REG_ESUBREG
);
4064 /* Can't back reference to a subexpression if inside of it. */
4065 if (group_in_compile_stack (compile_stack
, (regnum_t
) c1
))
4069 BUF_PUSH_2 (duplicate
, c1
);
4075 if (syntax
& RE_BK_PLUS_QM
)
4078 goto normal_backslash
;
4082 /* You might think it would be useful for \ to mean
4083 not to translate; but if we don't translate it
4084 it will never match anything. */
4092 /* Expects the character in `c'. */
4094 /* If no exactn currently being built. */
4097 /* If last exactn handle binary(or character) and
4098 new exactn handle character(or binary). */
4099 || is_exactn_bin
!= is_binary
[p
- 1 - pattern
]
4102 /* If last exactn not at current position. */
4103 || pending_exact
+ *pending_exact
+ 1 != b
4105 /* We have only one byte following the exactn for the count. */
4106 || *pending_exact
== (1 << BYTEWIDTH
) - 1
4108 /* If followed by a repetition operator. */
4109 || *p
== '*' || *p
== '^'
4110 || ((syntax
& RE_BK_PLUS_QM
)
4111 ? *p
== '\\' && (p
[1] == '+' || p
[1] == '?')
4112 : (*p
== '+' || *p
== '?'))
4113 || ((syntax
& RE_INTERVALS
)
4114 && ((syntax
& RE_NO_BK_BRACES
)
4116 : (p
[0] == '\\' && p
[1] == '{'))))
4118 /* Start building a new exactn. */
4123 /* Is this exactn binary data or character? */
4124 is_exactn_bin
= is_binary
[p
- 1 - pattern
];
4126 BUF_PUSH_2 (exactn_bin
, 0);
4128 BUF_PUSH_2 (exactn
, 0);
4130 BUF_PUSH_2 (exactn
, 0);
4132 pending_exact
= b
- 1;
4139 } /* while p != pend */
4142 /* Through the pattern now. */
4145 STORE_JUMP (jump_past_alt
, fixup_alt_jump
, b
);
4147 if (!COMPILE_STACK_EMPTY
)
4148 FREE_STACK_RETURN (REG_EPAREN
);
4150 /* If we don't want backtracking, force success
4151 the first time we reach the end of the compiled pattern. */
4152 if (syntax
& RE_NO_POSIX_BACKTRACKING
)
4160 free (compile_stack
.stack
);
4162 /* We have succeeded; set the length of the buffer. */
4164 bufp
->used
= (uintptr_t) b
- (uintptr_t) COMPILED_BUFFER_VAR
;
4166 bufp
->used
= b
- bufp
->buffer
;
4172 DEBUG_PRINT1 ("\nCompiled pattern: \n");
4173 PREFIX(print_compiled_pattern
) (bufp
);
4177 #ifndef MATCH_MAY_ALLOCATE
4178 /* Initialize the failure stack to the largest possible stack. This
4179 isn't necessary unless we're trying to avoid calling alloca in
4180 the search and match routines. */
4182 int num_regs
= bufp
->re_nsub
+ 1;
4184 /* Since DOUBLE_FAIL_STACK refuses to double only if the current size
4185 is strictly greater than re_max_failures, the largest possible stack
4186 is 2 * re_max_failures failure points. */
4187 if (fail_stack
.size
< (2 * re_max_failures
* MAX_FAILURE_ITEMS
))
4189 fail_stack
.size
= (2 * re_max_failures
* MAX_FAILURE_ITEMS
);
4192 if (! fail_stack
.stack
)
4194 = (PREFIX(fail_stack_elt_t
) *) xmalloc (fail_stack
.size
4195 * sizeof (PREFIX(fail_stack_elt_t
)));
4198 = (PREFIX(fail_stack_elt_t
) *) xrealloc (fail_stack
.stack
,
4200 * sizeof (PREFIX(fail_stack_elt_t
))));
4201 # else /* not emacs */
4202 if (! fail_stack
.stack
)
4204 = (PREFIX(fail_stack_elt_t
) *) malloc (fail_stack
.size
4205 * sizeof (PREFIX(fail_stack_elt_t
)));
4208 = (PREFIX(fail_stack_elt_t
) *) realloc (fail_stack
.stack
,
4210 * sizeof (PREFIX(fail_stack_elt_t
))));
4211 # endif /* not emacs */
4214 PREFIX(regex_grow_registers
) (num_regs
);
4216 #endif /* not MATCH_MAY_ALLOCATE */
4219 } /* regex_compile */
4221 /* Subroutines for `regex_compile'. */
4223 /* Store OP at LOC followed by two-byte integer parameter ARG. */
4224 /* ifdef WCHAR, integer parameter is 1 wchar_t. */
4227 PREFIX(store_op1
) (re_opcode_t op
, UCHAR_T
*loc
, int arg
)
4229 *loc
= (UCHAR_T
) op
;
4230 STORE_NUMBER (loc
+ 1, arg
);
4234 /* Like `store_op1', but for two two-byte parameters ARG1 and ARG2. */
4235 /* ifdef WCHAR, integer parameter is 1 wchar_t. */
4238 PREFIX(store_op2
) (re_opcode_t op
, UCHAR_T
*loc
, int arg1
, int arg2
)
4240 *loc
= (UCHAR_T
) op
;
4241 STORE_NUMBER (loc
+ 1, arg1
);
4242 STORE_NUMBER (loc
+ 1 + OFFSET_ADDRESS_SIZE
, arg2
);
4246 /* Copy the bytes from LOC to END to open up three bytes of space at LOC
4247 for OP followed by two-byte integer parameter ARG. */
4248 /* ifdef WCHAR, integer parameter is 1 wchar_t. */
4251 PREFIX(insert_op1
) (re_opcode_t op
, UCHAR_T
*loc
, int arg
, UCHAR_T
*end
)
4253 register UCHAR_T
*pfrom
= end
;
4254 register UCHAR_T
*pto
= end
+ 1 + OFFSET_ADDRESS_SIZE
;
4256 while (pfrom
!= loc
)
4259 PREFIX(store_op1
) (op
, loc
, arg
);
4263 /* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2. */
4264 /* ifdef WCHAR, integer parameter is 1 wchar_t. */
4267 PREFIX(insert_op2
) (re_opcode_t op
, UCHAR_T
*loc
, int arg1
,
4268 int arg2
, UCHAR_T
*end
)
4270 register UCHAR_T
*pfrom
= end
;
4271 register UCHAR_T
*pto
= end
+ 1 + 2 * OFFSET_ADDRESS_SIZE
;
4273 while (pfrom
!= loc
)
4276 PREFIX(store_op2
) (op
, loc
, arg1
, arg2
);
4280 /* P points to just after a ^ in PATTERN. Return true if that ^ comes
4281 after an alternative or a begin-subexpression. We assume there is at
4282 least one character before the ^. */
4285 PREFIX(at_begline_loc_p
) (const CHAR_T
*pattern
, const CHAR_T
*p
,
4286 reg_syntax_t syntax
)
4288 const CHAR_T
*prev
= p
- 2;
4289 boolean prev_prev_backslash
= prev
> pattern
&& prev
[-1] == '\\';
4292 /* After a subexpression? */
4293 (*prev
== '(' && (syntax
& RE_NO_BK_PARENS
|| prev_prev_backslash
))
4294 /* After an alternative? */
4295 || (*prev
== '|' && (syntax
& RE_NO_BK_VBAR
|| prev_prev_backslash
));
4299 /* The dual of at_begline_loc_p. This one is for $. We assume there is
4300 at least one character after the $, i.e., `P < PEND'. */
4303 PREFIX(at_endline_loc_p
) (const CHAR_T
*p
, const CHAR_T
*pend
,
4304 reg_syntax_t syntax
)
4306 const CHAR_T
*next
= p
;
4307 boolean next_backslash
= *next
== '\\';
4308 const CHAR_T
*next_next
= p
+ 1 < pend
? p
+ 1 : 0;
4311 /* Before a subexpression? */
4312 (syntax
& RE_NO_BK_PARENS
? *next
== ')'
4313 : next_backslash
&& next_next
&& *next_next
== ')')
4314 /* Before an alternative? */
4315 || (syntax
& RE_NO_BK_VBAR
? *next
== '|'
4316 : next_backslash
&& next_next
&& *next_next
== '|');
4319 #else /* not INSIDE_RECURSION */
4321 /* Returns true if REGNUM is in one of COMPILE_STACK's elements and
4322 false if it's not. */
4325 group_in_compile_stack (compile_stack_type compile_stack
, regnum_t regnum
)
4329 for (this_element
= compile_stack
.avail
- 1;
4332 if (compile_stack
.stack
[this_element
].regnum
== regnum
)
4337 #endif /* not INSIDE_RECURSION */
4339 #ifdef INSIDE_RECURSION
4342 /* This insert space, which size is "num", into the pattern at "loc".
4343 "end" must point the end of the allocated buffer. */
4345 insert_space (int num
, CHAR_T
*loc
, CHAR_T
*end
)
4347 register CHAR_T
*pto
= end
;
4348 register CHAR_T
*pfrom
= end
- num
;
4350 while (pfrom
>= loc
)
4356 static reg_errcode_t
4357 wcs_compile_range (CHAR_T range_start_char
, const CHAR_T
**p_ptr
,
4358 const CHAR_T
*pend
, RE_TRANSLATE_TYPE translate
,
4359 reg_syntax_t syntax
, CHAR_T
*b
, CHAR_T
*char_set
)
4361 const CHAR_T
*p
= *p_ptr
;
4362 CHAR_T range_start
, range_end
;
4366 uint32_t start_val
, end_val
;
4372 nrules
= _NL_CURRENT_WORD (LC_COLLATE
, _NL_COLLATE_NRULES
);
4375 const char *collseq
= (const char *) _NL_CURRENT(LC_COLLATE
,
4376 _NL_COLLATE_COLLSEQWC
);
4377 const unsigned char *extra
= (const unsigned char *)
4378 _NL_CURRENT (LC_COLLATE
, _NL_COLLATE_SYMB_EXTRAMB
);
4380 if (range_start_char
< -1)
4382 /* range_start is a collating symbol. */
4384 /* Retreive the index and get collation sequence value. */
4385 wextra
= (int32_t*)(extra
+ char_set
[-range_start_char
]);
4386 start_val
= wextra
[1 + *wextra
];
4389 start_val
= collseq_table_lookup(collseq
, TRANSLATE(range_start_char
));
4391 end_val
= collseq_table_lookup (collseq
, TRANSLATE (p
[0]));
4393 /* Report an error if the range is empty and the syntax prohibits
4395 ret
= ((syntax
& RE_NO_EMPTY_RANGES
)
4396 && (start_val
> end_val
))? REG_ERANGE
: REG_NOERROR
;
4398 /* Insert space to the end of the char_ranges. */
4399 insert_space(2, b
- char_set
[5] - 2, b
- 1);
4400 *(b
- char_set
[5] - 2) = (wchar_t)start_val
;
4401 *(b
- char_set
[5] - 1) = (wchar_t)end_val
;
4402 char_set
[4]++; /* ranges_index */
4407 range_start
= (range_start_char
>= 0)? TRANSLATE (range_start_char
):
4409 range_end
= TRANSLATE (p
[0]);
4410 /* Report an error if the range is empty and the syntax prohibits
4412 ret
= ((syntax
& RE_NO_EMPTY_RANGES
)
4413 && (range_start
> range_end
))? REG_ERANGE
: REG_NOERROR
;
4415 /* Insert space to the end of the char_ranges. */
4416 insert_space(2, b
- char_set
[5] - 2, b
- 1);
4417 *(b
- char_set
[5] - 2) = range_start
;
4418 *(b
- char_set
[5] - 1) = range_end
;
4419 char_set
[4]++; /* ranges_index */
4421 /* Have to increment the pointer into the pattern string, so the
4422 caller isn't still at the ending character. */
4428 /* Read the ending character of a range (in a bracket expression) from the
4429 uncompiled pattern *P_PTR (which ends at PEND). We assume the
4430 starting character is in `P[-2]'. (`P[-1]' is the character `-'.)
4431 Then we set the translation of all bits between the starting and
4432 ending characters (inclusive) in the compiled pattern B.
4434 Return an error code.
4436 We use these short variable names so we can use the same macros as
4437 `regex_compile' itself. */
4439 static reg_errcode_t
4440 byte_compile_range (unsigned int range_start_char
, const char **p_ptr
,
4441 const char *pend
, RE_TRANSLATE_TYPE translate
,
4442 reg_syntax_t syntax
, unsigned char *b
)
4445 const char *p
= *p_ptr
;
4448 const unsigned char *collseq
;
4449 unsigned int start_colseq
;
4450 unsigned int end_colseq
;
4458 /* Have to increment the pointer into the pattern string, so the
4459 caller isn't still at the ending character. */
4462 /* Report an error if the range is empty and the syntax prohibits this. */
4463 ret
= syntax
& RE_NO_EMPTY_RANGES
? REG_ERANGE
: REG_NOERROR
;
4466 collseq
= (const unsigned char *) _NL_CURRENT (LC_COLLATE
,
4467 _NL_COLLATE_COLLSEQMB
);
4469 start_colseq
= collseq
[(unsigned char) TRANSLATE (range_start_char
)];
4470 end_colseq
= collseq
[(unsigned char) TRANSLATE (p
[0])];
4471 for (this_char
= 0; this_char
<= (unsigned char) -1; ++this_char
)
4473 unsigned int this_colseq
= collseq
[(unsigned char) TRANSLATE (this_char
)];
4475 if (start_colseq
<= this_colseq
&& this_colseq
<= end_colseq
)
4477 SET_LIST_BIT (TRANSLATE (this_char
));
4482 /* Here we see why `this_char' has to be larger than an `unsigned
4483 char' -- we would otherwise go into an infinite loop, since all
4484 characters <= 0xff. */
4485 range_start_char
= TRANSLATE (range_start_char
);
4486 /* TRANSLATE(p[0]) is casted to char (not unsigned char) in TRANSLATE,
4487 and some compilers cast it to int implicitly, so following for_loop
4488 may fall to (almost) infinite loop.
4489 e.g. If translate[p[0]] = 0xff, end_char may equals to 0xffffffff.
4490 To avoid this, we cast p[0] to unsigned int and truncate it. */
4491 end_char
= ((unsigned)TRANSLATE(p
[0]) & ((1 << BYTEWIDTH
) - 1));
4493 for (this_char
= range_start_char
; this_char
<= end_char
; ++this_char
)
4495 SET_LIST_BIT (TRANSLATE (this_char
));
4504 /* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
4505 BUFP. A fastmap records which of the (1 << BYTEWIDTH) possible
4506 characters can start a string that matches the pattern. This fastmap
4507 is used by re_search to skip quickly over impossible starting points.
4509 The caller must supply the address of a (1 << BYTEWIDTH)-byte data
4510 area as BUFP->fastmap.
4512 We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in
4515 Returns 0 if we succeed, -2 if an internal error. */
4518 /* local function for re_compile_fastmap.
4519 truncate wchar_t character to char. */
4520 static unsigned char truncate_wchar (CHAR_T c
);
4522 static unsigned char
4523 truncate_wchar (CHAR_T c
)
4525 unsigned char buf
[MB_CUR_MAX
];
4528 memset (&state
, '\0', sizeof (state
));
4530 retval
= __wcrtomb (buf
, c
, &state
);
4532 retval
= wcrtomb (buf
, c
, &state
);
4534 return retval
> 0 ? buf
[0] : (unsigned char) c
;
4539 PREFIX(re_compile_fastmap
) (struct re_pattern_buffer
*bufp
)
4542 #ifdef MATCH_MAY_ALLOCATE
4543 PREFIX(fail_stack_type
) fail_stack
;
4545 #ifndef REGEX_MALLOC
4549 register char *fastmap
= bufp
->fastmap
;
4552 /* We need to cast pattern to (wchar_t*), because we casted this compiled
4553 pattern to (char*) in regex_compile. */
4554 UCHAR_T
*pattern
= (UCHAR_T
*)bufp
->buffer
;
4555 register UCHAR_T
*pend
= (UCHAR_T
*) (bufp
->buffer
+ bufp
->used
);
4557 UCHAR_T
*pattern
= bufp
->buffer
;
4558 register UCHAR_T
*pend
= pattern
+ bufp
->used
;
4560 UCHAR_T
*p
= pattern
;
4563 /* This holds the pointer to the failure stack, when
4564 it is allocated relocatably. */
4565 fail_stack_elt_t
*failure_stack_ptr
;
4568 /* Assume that each path through the pattern can be null until
4569 proven otherwise. We set this false at the bottom of switch
4570 statement, to which we get only if a particular path doesn't
4571 match the empty string. */
4572 boolean path_can_be_null
= true;
4574 /* We aren't doing a `succeed_n' to begin with. */
4575 boolean succeed_n_p
= false;
4577 assert (fastmap
!= NULL
&& p
!= NULL
);
4580 bzero (fastmap
, 1 << BYTEWIDTH
); /* Assume nothing's valid. */
4581 bufp
->fastmap_accurate
= 1; /* It will be when we're done. */
4582 bufp
->can_be_null
= 0;
4586 if (p
== pend
|| *p
== (UCHAR_T
) succeed
)
4588 /* We have reached the (effective) end of pattern. */
4589 if (!FAIL_STACK_EMPTY ())
4591 bufp
->can_be_null
|= path_can_be_null
;
4593 /* Reset for next path. */
4594 path_can_be_null
= true;
4596 p
= fail_stack
.stack
[--fail_stack
.avail
].pointer
;
4604 /* We should never be about to go beyond the end of the pattern. */
4607 switch (SWITCH_ENUM_CAST ((re_opcode_t
) *p
++))
4610 /* I guess the idea here is to simply not bother with a fastmap
4611 if a backreference is used, since it's too hard to figure out
4612 the fastmap for the corresponding group. Setting
4613 `can_be_null' stops `re_search_2' from using the fastmap, so
4614 that is all we do. */
4616 bufp
->can_be_null
= 1;
4620 /* Following are the cases which match a character. These end
4625 fastmap
[truncate_wchar(p
[1])] = 1;
4639 /* It is hard to distinguish fastmap from (multi byte) characters
4640 which depends on current locale. */
4645 bufp
->can_be_null
= 1;
4649 for (j
= *p
++ * BYTEWIDTH
- 1; j
>= 0; j
--)
4650 if (p
[j
/ BYTEWIDTH
] & (1 << (j
% BYTEWIDTH
)))
4656 /* Chars beyond end of map must be allowed. */
4657 for (j
= *p
* BYTEWIDTH
; j
< (1 << BYTEWIDTH
); j
++)
4660 for (j
= *p
++ * BYTEWIDTH
- 1; j
>= 0; j
--)
4661 if (!(p
[j
/ BYTEWIDTH
] & (1 << (j
% BYTEWIDTH
))))
4667 for (j
= 0; j
< (1 << BYTEWIDTH
); j
++)
4668 if (SYNTAX (j
) == Sword
)
4674 for (j
= 0; j
< (1 << BYTEWIDTH
); j
++)
4675 if (SYNTAX (j
) != Sword
)
4682 int fastmap_newline
= fastmap
['\n'];
4684 /* `.' matches anything ... */
4685 for (j
= 0; j
< (1 << BYTEWIDTH
); j
++)
4688 /* ... except perhaps newline. */
4689 if (!(bufp
->syntax
& RE_DOT_NEWLINE
))
4690 fastmap
['\n'] = fastmap_newline
;
4692 /* Return if we have already set `can_be_null'; if we have,
4693 then the fastmap is irrelevant. Something's wrong here. */
4694 else if (bufp
->can_be_null
)
4697 /* Otherwise, have to check alternative paths. */
4704 for (j
= 0; j
< (1 << BYTEWIDTH
); j
++)
4705 if (SYNTAX (j
) == (enum syntaxcode
) k
)
4712 for (j
= 0; j
< (1 << BYTEWIDTH
); j
++)
4713 if (SYNTAX (j
) != (enum syntaxcode
) k
)
4718 /* All cases after this match the empty string. These end with
4738 case push_dummy_failure
:
4743 case pop_failure_jump
:
4744 case maybe_pop_jump
:
4747 case dummy_failure_jump
:
4748 EXTRACT_NUMBER_AND_INCR (j
, p
);
4753 /* Jump backward implies we just went through the body of a
4754 loop and matched nothing. Opcode jumped to should be
4755 `on_failure_jump' or `succeed_n'. Just treat it like an
4756 ordinary jump. For a * loop, it has pushed its failure
4757 point already; if so, discard that as redundant. */
4758 if ((re_opcode_t
) *p
!= on_failure_jump
4759 && (re_opcode_t
) *p
!= succeed_n
)
4763 EXTRACT_NUMBER_AND_INCR (j
, p
);
4766 /* If what's on the stack is where we are now, pop it. */
4767 if (!FAIL_STACK_EMPTY ()
4768 && fail_stack
.stack
[fail_stack
.avail
- 1].pointer
== p
)
4774 case on_failure_jump
:
4775 case on_failure_keep_string_jump
:
4776 handle_on_failure_jump
:
4777 EXTRACT_NUMBER_AND_INCR (j
, p
);
4779 /* For some patterns, e.g., `(a?)?', `p+j' here points to the
4780 end of the pattern. We don't want to push such a point,
4781 since when we restore it above, entering the switch will
4782 increment `p' past the end of the pattern. We don't need
4783 to push such a point since we obviously won't find any more
4784 fastmap entries beyond `pend'. Such a pattern can match
4785 the null string, though. */
4788 if (!PUSH_PATTERN_OP (p
+ j
, fail_stack
))
4790 RESET_FAIL_STACK ();
4795 bufp
->can_be_null
= 1;
4799 EXTRACT_NUMBER_AND_INCR (k
, p
); /* Skip the n. */
4800 succeed_n_p
= false;
4807 /* Get to the number of times to succeed. */
4808 p
+= OFFSET_ADDRESS_SIZE
;
4810 /* Increment p past the n for when k != 0. */
4811 EXTRACT_NUMBER_AND_INCR (k
, p
);
4814 p
-= 2 * OFFSET_ADDRESS_SIZE
;
4815 succeed_n_p
= true; /* Spaghetti code alert. */
4816 goto handle_on_failure_jump
;
4822 p
+= 2 * OFFSET_ADDRESS_SIZE
;
4833 abort (); /* We have listed all the cases. */
4836 /* Getting here means we have found the possible starting
4837 characters for one path of the pattern -- and that the empty
4838 string does not match. We need not follow this path further.
4839 Instead, look at the next alternative (remembered on the
4840 stack), or quit if no more. The test at the top of the loop
4841 does these things. */
4842 path_can_be_null
= false;
4846 /* Set `can_be_null' for the last path (also the first path, if the
4847 pattern is empty). */
4848 bufp
->can_be_null
|= path_can_be_null
;
4851 RESET_FAIL_STACK ();
4855 #else /* not INSIDE_RECURSION */
4858 re_compile_fastmap (struct re_pattern_buffer
*bufp
)
4861 if (MB_CUR_MAX
!= 1)
4862 return wcs_re_compile_fastmap(bufp
);
4865 return byte_re_compile_fastmap(bufp
);
4866 } /* re_compile_fastmap */
4868 weak_alias (__re_compile_fastmap
, re_compile_fastmap
)
4872 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
4873 ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
4874 this memory for recording register information. STARTS and ENDS
4875 must be allocated using the malloc library routine, and must each
4876 be at least NUM_REGS * sizeof (regoff_t) bytes long.
4878 If NUM_REGS == 0, then subsequent matches should allocate their own
4881 Unless this function is called, the first search or match using
4882 PATTERN_BUFFER will allocate its own register data, without
4883 freeing the old data. */
4886 re_set_registers (struct re_pattern_buffer
*bufp
,
4887 struct re_registers
*regs
, unsigned num_regs
,
4888 regoff_t
*starts
, regoff_t
*ends
)
4892 bufp
->regs_allocated
= REGS_REALLOCATE
;
4893 regs
->num_regs
= num_regs
;
4894 regs
->start
= starts
;
4899 bufp
->regs_allocated
= REGS_UNALLOCATED
;
4901 regs
->start
= regs
->end
= (regoff_t
*) 0;
4905 weak_alias (__re_set_registers
, re_set_registers
)
4908 /* Searching routines. */
4910 /* Like re_search_2, below, but only one string is specified, and
4911 doesn't let you say where to stop matching. */
4914 re_search (struct re_pattern_buffer
*bufp
, const char *string
, int size
,
4915 int startpos
, int range
, struct re_registers
*regs
)
4917 return re_search_2 (bufp
, NULL
, 0, string
, size
, startpos
, range
,
4921 weak_alias (__re_search
, re_search
)
4925 /* Using the compiled pattern in BUFP->buffer, first tries to match the
4926 virtual concatenation of STRING1 and STRING2, starting first at index
4927 STARTPOS, then at STARTPOS + 1, and so on.
4929 STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
4931 RANGE is how far to scan while trying to match. RANGE = 0 means try
4932 only at STARTPOS; in general, the last start tried is STARTPOS +
4935 In REGS, return the indices of the virtual concatenation of STRING1
4936 and STRING2 that matched the entire BUFP->buffer and its contained
4939 Do not consider matching one past the index STOP in the virtual
4940 concatenation of STRING1 and STRING2.
4942 We return either the position in the strings at which the match was
4943 found, -1 if no match, or -2 if error (such as failure
4947 re_search_2 (struct re_pattern_buffer
*bufp
, const char *string1
, int size1
,
4948 const char *string2
, int size2
, int startpos
, int range
,
4949 struct re_registers
*regs
, int stop
)
4952 if (MB_CUR_MAX
!= 1)
4953 return wcs_re_search_2 (bufp
, string1
, size1
, string2
, size2
, startpos
,
4957 return byte_re_search_2 (bufp
, string1
, size1
, string2
, size2
, startpos
,
4961 weak_alias (__re_search_2
, re_search_2
)
4964 #endif /* not INSIDE_RECURSION */
4966 #ifdef INSIDE_RECURSION
4968 #ifdef MATCH_MAY_ALLOCATE
4969 # define FREE_VAR(var) if (var) REGEX_FREE (var); var = NULL
4971 # define FREE_VAR(var) if (var) free (var); var = NULL
4975 # define MAX_ALLOCA_SIZE 2000
4977 # define FREE_WCS_BUFFERS() \
4979 if (size1 > MAX_ALLOCA_SIZE) \
4981 free (wcs_string1); \
4982 free (mbs_offset1); \
4986 FREE_VAR (wcs_string1); \
4987 FREE_VAR (mbs_offset1); \
4989 if (size2 > MAX_ALLOCA_SIZE) \
4991 free (wcs_string2); \
4992 free (mbs_offset2); \
4996 FREE_VAR (wcs_string2); \
4997 FREE_VAR (mbs_offset2); \
5005 PREFIX(re_search_2
) (struct re_pattern_buffer
*bufp
, const char *string1
,
5006 int size1
, const char *string2
, int size2
,
5007 int startpos
, int range
,
5008 struct re_registers
*regs
, int stop
)
5011 register char *fastmap
= bufp
->fastmap
;
5012 register RE_TRANSLATE_TYPE translate
= bufp
->translate
;
5013 int total_size
= size1
+ size2
;
5014 int endpos
= startpos
+ range
;
5016 /* We need wchar_t* buffers correspond to cstring1, cstring2. */
5017 wchar_t *wcs_string1
= NULL
, *wcs_string2
= NULL
;
5018 /* We need the size of wchar_t buffers correspond to csize1, csize2. */
5019 int wcs_size1
= 0, wcs_size2
= 0;
5020 /* offset buffer for optimizatoin. See convert_mbs_to_wc. */
5021 int *mbs_offset1
= NULL
, *mbs_offset2
= NULL
;
5022 /* They hold whether each wchar_t is binary data or not. */
5023 char *is_binary
= NULL
;
5026 /* Check for out-of-range STARTPOS. */
5027 if (startpos
< 0 || startpos
> total_size
)
5030 /* Fix up RANGE if it might eventually take us outside
5031 the virtual concatenation of STRING1 and STRING2.
5032 Make sure we won't move STARTPOS below 0 or above TOTAL_SIZE. */
5034 range
= 0 - startpos
;
5035 else if (endpos
> total_size
)
5036 range
= total_size
- startpos
;
5038 /* If the search isn't to be a backwards one, don't waste time in a
5039 search for a pattern that must be anchored. */
5040 if (bufp
->used
> 0 && range
> 0
5041 && ((re_opcode_t
) bufp
->buffer
[0] == begbuf
5042 /* `begline' is like `begbuf' if it cannot match at newlines. */
5043 || ((re_opcode_t
) bufp
->buffer
[0] == begline
5044 && !bufp
->newline_anchor
)))
5053 /* In a forward search for something that starts with \=.
5054 don't keep searching past point. */
5055 if (bufp
->used
> 0 && (re_opcode_t
) bufp
->buffer
[0] == at_dot
&& range
> 0)
5057 range
= PT
- startpos
;
5063 /* Update the fastmap now if not correct already. */
5064 if (fastmap
&& !bufp
->fastmap_accurate
)
5065 if (re_compile_fastmap (bufp
) == -2)
5069 /* Allocate wchar_t array for wcs_string1 and wcs_string2 and
5070 fill them with converted string. */
5073 if (size1
> MAX_ALLOCA_SIZE
)
5075 wcs_string1
= TALLOC (size1
+ 1, CHAR_T
);
5076 mbs_offset1
= TALLOC (size1
+ 1, int);
5077 is_binary
= TALLOC (size1
+ 1, char);
5081 wcs_string1
= REGEX_TALLOC (size1
+ 1, CHAR_T
);
5082 mbs_offset1
= REGEX_TALLOC (size1
+ 1, int);
5083 is_binary
= REGEX_TALLOC (size1
+ 1, char);
5085 if (!wcs_string1
|| !mbs_offset1
|| !is_binary
)
5087 if (size1
> MAX_ALLOCA_SIZE
)
5095 FREE_VAR (wcs_string1
);
5096 FREE_VAR (mbs_offset1
);
5097 FREE_VAR (is_binary
);
5101 wcs_size1
= convert_mbs_to_wcs(wcs_string1
, string1
, size1
,
5102 mbs_offset1
, is_binary
);
5103 wcs_string1
[wcs_size1
] = L
'\0'; /* for a sentinel */
5104 if (size1
> MAX_ALLOCA_SIZE
)
5107 FREE_VAR (is_binary
);
5111 if (size2
> MAX_ALLOCA_SIZE
)
5113 wcs_string2
= TALLOC (size2
+ 1, CHAR_T
);
5114 mbs_offset2
= TALLOC (size2
+ 1, int);
5115 is_binary
= TALLOC (size2
+ 1, char);
5119 wcs_string2
= REGEX_TALLOC (size2
+ 1, CHAR_T
);
5120 mbs_offset2
= REGEX_TALLOC (size2
+ 1, int);
5121 is_binary
= REGEX_TALLOC (size2
+ 1, char);
5123 if (!wcs_string2
|| !mbs_offset2
|| !is_binary
)
5125 FREE_WCS_BUFFERS ();
5126 if (size2
> MAX_ALLOCA_SIZE
)
5129 FREE_VAR (is_binary
);
5132 wcs_size2
= convert_mbs_to_wcs(wcs_string2
, string2
, size2
,
5133 mbs_offset2
, is_binary
);
5134 wcs_string2
[wcs_size2
] = L
'\0'; /* for a sentinel */
5135 if (size2
> MAX_ALLOCA_SIZE
)
5138 FREE_VAR (is_binary
);
5143 /* Loop through the string, looking for a place to start matching. */
5146 /* If a fastmap is supplied, skip quickly over characters that
5147 cannot be the start of a match. If the pattern can match the
5148 null string, however, we don't need to skip characters; we want
5149 the first null string. */
5150 if (fastmap
&& startpos
< total_size
&& !bufp
->can_be_null
)
5152 if (range
> 0) /* Searching forwards. */
5154 register const char *d
;
5155 register int lim
= 0;
5158 if (startpos
< size1
&& startpos
+ range
>= size1
)
5159 lim
= range
- (size1
- startpos
);
5161 d
= (startpos
>= size1
? string2
- size1
: string1
) + startpos
;
5163 /* Written out as an if-else to avoid testing `translate'
5167 && !fastmap
[(unsigned char)
5168 translate
[(unsigned char) *d
++]])
5171 while (range
> lim
&& !fastmap
[(unsigned char) *d
++])
5174 startpos
+= irange
- range
;
5176 else /* Searching backwards. */
5178 register CHAR_T c
= (size1
== 0 || startpos
>= size1
5179 ? string2
[startpos
- size1
]
5180 : string1
[startpos
]);
5182 if (!fastmap
[(unsigned char) TRANSLATE (c
)])
5187 /* If can't match the null string, and that's all we have left, fail. */
5188 if (range
>= 0 && startpos
== total_size
&& fastmap
5189 && !bufp
->can_be_null
)
5192 FREE_WCS_BUFFERS ();
5198 val
= wcs_re_match_2_internal (bufp
, string1
, size1
, string2
,
5199 size2
, startpos
, regs
, stop
,
5200 wcs_string1
, wcs_size1
,
5201 wcs_string2
, wcs_size2
,
5202 mbs_offset1
, mbs_offset2
);
5204 val
= byte_re_match_2_internal (bufp
, string1
, size1
, string2
,
5205 size2
, startpos
, regs
, stop
);
5208 #ifndef REGEX_MALLOC
5217 FREE_WCS_BUFFERS ();
5225 FREE_WCS_BUFFERS ();
5245 FREE_WCS_BUFFERS ();
5251 /* This converts PTR, a pointer into one of the search wchar_t strings
5252 `string1' and `string2' into an multibyte string offset from the
5253 beginning of that string. We use mbs_offset to optimize.
5254 See convert_mbs_to_wcs. */
5255 # define POINTER_TO_OFFSET(ptr) \
5256 (FIRST_STRING_P (ptr) \
5257 ? ((regoff_t)(mbs_offset1 != NULL? mbs_offset1[(ptr)-string1] : 0)) \
5258 : ((regoff_t)((mbs_offset2 != NULL? mbs_offset2[(ptr)-string2] : 0) \
5261 /* This converts PTR, a pointer into one of the search strings `string1'
5262 and `string2' into an offset from the beginning of that string. */
5263 # define POINTER_TO_OFFSET(ptr) \
5264 (FIRST_STRING_P (ptr) \
5265 ? ((regoff_t) ((ptr) - string1)) \
5266 : ((regoff_t) ((ptr) - string2 + size1)))
5269 /* Macros for dealing with the split strings in re_match_2. */
5271 #define MATCHING_IN_FIRST_STRING (dend == end_match_1)
5273 /* Call before fetching a character with *d. This switches over to
5274 string2 if necessary. */
5275 #define PREFETCH() \
5278 /* End of string2 => fail. */ \
5279 if (dend == end_match_2) \
5281 /* End of string1 => advance to string2. */ \
5283 dend = end_match_2; \
5286 /* Test if at very beginning or at very end of the virtual concatenation
5287 of `string1' and `string2'. If only one string, it's `string2'. */
5288 #define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2)
5289 #define AT_STRINGS_END(d) ((d) == end2)
5292 /* Test if D points to a character which is word-constituent. We have
5293 two special cases to check for: if past the end of string1, look at
5294 the first character in string2; and if before the beginning of
5295 string2, look at the last character in string1. */
5297 /* Use internationalized API instead of SYNTAX. */
5298 # define WORDCHAR_P(d) \
5299 (iswalnum ((wint_t)((d) == end1 ? *string2 \
5300 : (d) == string2 - 1 ? *(end1 - 1) : *(d))) != 0 \
5301 || ((d) == end1 ? *string2 \
5302 : (d) == string2 - 1 ? *(end1 - 1) : *(d)) == L'_')
5304 # define WORDCHAR_P(d) \
5305 (SYNTAX ((d) == end1 ? *string2 \
5306 : (d) == string2 - 1 ? *(end1 - 1) : *(d)) \
5310 /* Disabled due to a compiler bug -- see comment at case wordbound */
5312 /* Test if the character before D and the one at D differ with respect
5313 to being word-constituent. */
5314 #define AT_WORD_BOUNDARY(d) \
5315 (AT_STRINGS_BEG (d) || AT_STRINGS_END (d) \
5316 || WORDCHAR_P (d - 1) != WORDCHAR_P (d))
5319 /* Free everything we malloc. */
5320 #ifdef MATCH_MAY_ALLOCATE
5322 # define FREE_VARIABLES() \
5324 REGEX_FREE_STACK (fail_stack.stack); \
5325 FREE_VAR (regstart); \
5326 FREE_VAR (regend); \
5327 FREE_VAR (old_regstart); \
5328 FREE_VAR (old_regend); \
5329 FREE_VAR (best_regstart); \
5330 FREE_VAR (best_regend); \
5331 FREE_VAR (reg_info); \
5332 FREE_VAR (reg_dummy); \
5333 FREE_VAR (reg_info_dummy); \
5334 if (!cant_free_wcs_buf) \
5336 FREE_VAR (string1); \
5337 FREE_VAR (string2); \
5338 FREE_VAR (mbs_offset1); \
5339 FREE_VAR (mbs_offset2); \
5343 # define FREE_VARIABLES() \
5345 REGEX_FREE_STACK (fail_stack.stack); \
5346 FREE_VAR (regstart); \
5347 FREE_VAR (regend); \
5348 FREE_VAR (old_regstart); \
5349 FREE_VAR (old_regend); \
5350 FREE_VAR (best_regstart); \
5351 FREE_VAR (best_regend); \
5352 FREE_VAR (reg_info); \
5353 FREE_VAR (reg_dummy); \
5354 FREE_VAR (reg_info_dummy); \
5359 # define FREE_VARIABLES() \
5361 if (!cant_free_wcs_buf) \
5363 FREE_VAR (string1); \
5364 FREE_VAR (string2); \
5365 FREE_VAR (mbs_offset1); \
5366 FREE_VAR (mbs_offset2); \
5370 # define FREE_VARIABLES() ((void)0) /* Do nothing! But inhibit gcc warning. */
5372 #endif /* not MATCH_MAY_ALLOCATE */
5374 /* These values must meet several constraints. They must not be valid
5375 register values; since we have a limit of 255 registers (because
5376 we use only one byte in the pattern for the register number), we can
5377 use numbers larger than 255. They must differ by 1, because of
5378 NUM_FAILURE_ITEMS above. And the value for the lowest register must
5379 be larger than the value for the highest register, so we do not try
5380 to actually save any registers when none are active. */
5381 #define NO_HIGHEST_ACTIVE_REG (1 << BYTEWIDTH)
5382 #define NO_LOWEST_ACTIVE_REG (NO_HIGHEST_ACTIVE_REG + 1)
5384 #else /* not INSIDE_RECURSION */
5385 /* Matching routines. */
5387 #ifndef emacs /* Emacs never uses this. */
5388 /* re_match is like re_match_2 except it takes only a single string. */
5391 re_match (struct re_pattern_buffer
*bufp
, const char *string
,
5392 int size
, int pos
, struct re_registers
*regs
)
5396 if (MB_CUR_MAX
!= 1)
5397 result
= wcs_re_match_2_internal (bufp
, NULL
, 0, string
, size
,
5399 NULL
, 0, NULL
, 0, NULL
, NULL
);
5402 result
= byte_re_match_2_internal (bufp
, NULL
, 0, string
, size
,
5404 # ifndef REGEX_MALLOC
5412 weak_alias (__re_match
, re_match
)
5414 #endif /* not emacs */
5416 #endif /* not INSIDE_RECURSION */
5418 #ifdef INSIDE_RECURSION
5419 static boolean
PREFIX(group_match_null_string_p
) (UCHAR_T
**p
,
5421 PREFIX(register_info_type
) *reg_info
);
5422 static boolean
PREFIX(alt_match_null_string_p
) (UCHAR_T
*p
,
5424 PREFIX(register_info_type
) *reg_info
);
5425 static boolean
PREFIX(common_op_match_null_string_p
) (UCHAR_T
**p
,
5427 PREFIX(register_info_type
) *reg_info
);
5428 static int PREFIX(bcmp_translate
) (const CHAR_T
*s1
, const CHAR_T
*s2
,
5429 int len
, char *translate
);
5430 #else /* not INSIDE_RECURSION */
5432 /* re_match_2 matches the compiled pattern in BUFP against the
5433 the (virtual) concatenation of STRING1 and STRING2 (of length SIZE1
5434 and SIZE2, respectively). We start matching at POS, and stop
5437 If REGS is non-null and the `no_sub' field of BUFP is nonzero, we
5438 store offsets for the substring each group matched in REGS. See the
5439 documentation for exactly how many groups we fill.
5441 We return -1 if no match, -2 if an internal error (such as the
5442 failure stack overflowing). Otherwise, we return the length of the
5443 matched substring. */
5446 re_match_2 (struct re_pattern_buffer
*bufp
, const char *string1
, int size1
,
5447 const char *string2
, int size2
, int pos
,
5448 struct re_registers
*regs
, int stop
)
5452 if (MB_CUR_MAX
!= 1)
5453 result
= wcs_re_match_2_internal (bufp
, string1
, size1
, string2
, size2
,
5455 NULL
, 0, NULL
, 0, NULL
, NULL
);
5458 result
= byte_re_match_2_internal (bufp
, string1
, size1
, string2
, size2
,
5461 #ifndef REGEX_MALLOC
5469 weak_alias (__re_match_2
, re_match_2
)
5472 #endif /* not INSIDE_RECURSION */
5474 #ifdef INSIDE_RECURSION
5477 static int count_mbs_length (int *, int);
5479 /* This check the substring (from 0, to length) of the multibyte string,
5480 to which offset_buffer correspond. And count how many wchar_t_characters
5481 the substring occupy. We use offset_buffer to optimization.
5482 See convert_mbs_to_wcs. */
5485 count_mbs_length(int *offset_buffer
, int length
)
5489 /* Check whether the size is valid. */
5493 if (offset_buffer
== NULL
)
5496 /* If there are no multibyte character, offset_buffer[i] == i.
5497 Optmize for this case. */
5498 if (offset_buffer
[length
] == length
)
5501 /* Set up upper with length. (because for all i, offset_buffer[i] >= i) */
5507 int middle
= (lower
+ upper
) / 2;
5508 if (middle
== lower
|| middle
== upper
)
5510 if (offset_buffer
[middle
] > length
)
5512 else if (offset_buffer
[middle
] < length
)
5522 /* This is a separate function so that we can force an alloca cleanup
5526 wcs_re_match_2_internal (struct re_pattern_buffer
*bufp
,
5527 const char *cstring1
, int csize1
,
5528 const char *cstring2
, int csize2
,
5530 struct re_registers
*regs
,
5532 /* string1 == string2 == NULL means string1/2, size1/2 and
5533 mbs_offset1/2 need seting up in this function. */
5534 /* We need wchar_t* buffers correspond to cstring1, cstring2. */
5535 wchar_t *string1
, int size1
,
5536 wchar_t *string2
, int size2
,
5537 /* offset buffer for optimizatoin. See convert_mbs_to_wc. */
5538 int *mbs_offset1
, int *mbs_offset2
)
5541 byte_re_match_2_internal (struct re_pattern_buffer
*bufp
,
5542 const char *string1
, int size1
,
5543 const char *string2
, int size2
,
5545 struct re_registers
*regs
, int stop
)
5548 /* General temporaries. */
5552 /* They hold whether each wchar_t is binary data or not. */
5553 char *is_binary
= NULL
;
5554 /* If true, we can't free string1/2, mbs_offset1/2. */
5555 int cant_free_wcs_buf
= 1;
5558 /* Just past the end of the corresponding string. */
5559 const CHAR_T
*end1
, *end2
;
5561 /* Pointers into string1 and string2, just past the last characters in
5562 each to consider matching. */
5563 const CHAR_T
*end_match_1
, *end_match_2
;
5565 /* Where we are in the data, and the end of the current string. */
5566 const CHAR_T
*d
, *dend
;
5568 /* Where we are in the pattern, and the end of the pattern. */
5570 UCHAR_T
*pattern
, *p
;
5571 register UCHAR_T
*pend
;
5573 UCHAR_T
*p
= bufp
->buffer
;
5574 register UCHAR_T
*pend
= p
+ bufp
->used
;
5577 /* Mark the opcode just after a start_memory, so we can test for an
5578 empty subpattern when we get to the stop_memory. */
5579 UCHAR_T
*just_past_start_mem
= 0;
5581 /* We use this to map every character in the string. */
5582 RE_TRANSLATE_TYPE translate
= bufp
->translate
;
5584 /* Failure point stack. Each place that can handle a failure further
5585 down the line pushes a failure point on this stack. It consists of
5586 restart, regend, and reg_info for all registers corresponding to
5587 the subexpressions we're currently inside, plus the number of such
5588 registers, and, finally, two char *'s. The first char * is where
5589 to resume scanning the pattern; the second one is where to resume
5590 scanning the strings. If the latter is zero, the failure point is
5591 a ``dummy''; if a failure happens and the failure point is a dummy,
5592 it gets discarded and the next next one is tried. */
5593 #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
5594 PREFIX(fail_stack_type
) fail_stack
;
5597 static unsigned failure_id
;
5598 unsigned nfailure_points_pushed
= 0, nfailure_points_popped
= 0;
5602 /* This holds the pointer to the failure stack, when
5603 it is allocated relocatably. */
5604 fail_stack_elt_t
*failure_stack_ptr
;
5607 /* We fill all the registers internally, independent of what we
5608 return, for use in backreferences. The number here includes
5609 an element for register zero. */
5610 size_t num_regs
= bufp
->re_nsub
+ 1;
5612 /* The currently active registers. */
5613 active_reg_t lowest_active_reg
= NO_LOWEST_ACTIVE_REG
;
5614 active_reg_t highest_active_reg
= NO_HIGHEST_ACTIVE_REG
;
5616 /* Information on the contents of registers. These are pointers into
5617 the input strings; they record just what was matched (on this
5618 attempt) by a subexpression part of the pattern, that is, the
5619 regnum-th regstart pointer points to where in the pattern we began
5620 matching and the regnum-th regend points to right after where we
5621 stopped matching the regnum-th subexpression. (The zeroth register
5622 keeps track of what the whole pattern matches.) */
5623 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
5624 const CHAR_T
**regstart
, **regend
;
5627 /* If a group that's operated upon by a repetition operator fails to
5628 match anything, then the register for its start will need to be
5629 restored because it will have been set to wherever in the string we
5630 are when we last see its open-group operator. Similarly for a
5632 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
5633 const CHAR_T
**old_regstart
, **old_regend
;
5636 /* The is_active field of reg_info helps us keep track of which (possibly
5637 nested) subexpressions we are currently in. The matched_something
5638 field of reg_info[reg_num] helps us tell whether or not we have
5639 matched any of the pattern so far this time through the reg_num-th
5640 subexpression. These two fields get reset each time through any
5641 loop their register is in. */
5642 #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
5643 PREFIX(register_info_type
) *reg_info
;
5646 /* The following record the register info as found in the above
5647 variables when we find a match better than any we've seen before.
5648 This happens as we backtrack through the failure points, which in
5649 turn happens only if we have not yet matched the entire string. */
5650 unsigned best_regs_set
= false;
5651 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
5652 const CHAR_T
**best_regstart
, **best_regend
;
5655 /* Logically, this is `best_regend[0]'. But we don't want to have to
5656 allocate space for that if we're not allocating space for anything
5657 else (see below). Also, we never need info about register 0 for
5658 any of the other register vectors, and it seems rather a kludge to
5659 treat `best_regend' differently than the rest. So we keep track of
5660 the end of the best match so far in a separate variable. We
5661 initialize this to NULL so that when we backtrack the first time
5662 and need to test it, it's not garbage. */
5663 const CHAR_T
*match_end
= NULL
;
5665 /* This helps SET_REGS_MATCHED avoid doing redundant work. */
5666 int set_regs_matched_done
= 0;
5668 /* Used when we pop values we don't care about. */
5669 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
5670 const CHAR_T
**reg_dummy
;
5671 PREFIX(register_info_type
) *reg_info_dummy
;
5675 /* Counts the total number of registers pushed. */
5676 unsigned num_regs_pushed
= 0;
5679 DEBUG_PRINT1 ("\n\nEntering re_match_2.\n");
5683 #ifdef MATCH_MAY_ALLOCATE
5684 /* Do not bother to initialize all the register variables if there are
5685 no groups in the pattern, as it takes a fair amount of time. If
5686 there are groups, we include space for register 0 (the whole
5687 pattern), even though we never use it, since it simplifies the
5688 array indexing. We should fix this. */
5691 regstart
= REGEX_TALLOC (num_regs
, const CHAR_T
*);
5692 regend
= REGEX_TALLOC (num_regs
, const CHAR_T
*);
5693 old_regstart
= REGEX_TALLOC (num_regs
, const CHAR_T
*);
5694 old_regend
= REGEX_TALLOC (num_regs
, const CHAR_T
*);
5695 best_regstart
= REGEX_TALLOC (num_regs
, const CHAR_T
*);
5696 best_regend
= REGEX_TALLOC (num_regs
, const CHAR_T
*);
5697 reg_info
= REGEX_TALLOC (num_regs
, PREFIX(register_info_type
));
5698 reg_dummy
= REGEX_TALLOC (num_regs
, const CHAR_T
*);
5699 reg_info_dummy
= REGEX_TALLOC (num_regs
, PREFIX(register_info_type
));
5701 if (!(regstart
&& regend
&& old_regstart
&& old_regend
&& reg_info
5702 && best_regstart
&& best_regend
&& reg_dummy
&& reg_info_dummy
))
5710 /* We must initialize all our variables to NULL, so that
5711 `FREE_VARIABLES' doesn't try to free them. */
5712 regstart
= regend
= old_regstart
= old_regend
= best_regstart
5713 = best_regend
= reg_dummy
= NULL
;
5714 reg_info
= reg_info_dummy
= (PREFIX(register_info_type
) *) NULL
;
5716 #endif /* MATCH_MAY_ALLOCATE */
5718 /* The starting position is bogus. */
5720 if (pos
< 0 || pos
> csize1
+ csize2
)
5722 if (pos
< 0 || pos
> size1
+ size2
)
5730 /* Allocate wchar_t array for string1 and string2 and
5731 fill them with converted string. */
5732 if (string1
== NULL
&& string2
== NULL
)
5734 /* We need seting up buffers here. */
5736 /* We must free wcs buffers in this function. */
5737 cant_free_wcs_buf
= 0;
5741 string1
= REGEX_TALLOC (csize1
+ 1, CHAR_T
);
5742 mbs_offset1
= REGEX_TALLOC (csize1
+ 1, int);
5743 is_binary
= REGEX_TALLOC (csize1
+ 1, char);
5744 if (!string1
|| !mbs_offset1
|| !is_binary
)
5747 FREE_VAR (mbs_offset1
);
5748 FREE_VAR (is_binary
);
5754 string2
= REGEX_TALLOC (csize2
+ 1, CHAR_T
);
5755 mbs_offset2
= REGEX_TALLOC (csize2
+ 1, int);
5756 is_binary
= REGEX_TALLOC (csize2
+ 1, char);
5757 if (!string2
|| !mbs_offset2
|| !is_binary
)
5760 FREE_VAR (mbs_offset1
);
5762 FREE_VAR (mbs_offset2
);
5763 FREE_VAR (is_binary
);
5766 size2
= convert_mbs_to_wcs(string2
, cstring2
, csize2
,
5767 mbs_offset2
, is_binary
);
5768 string2
[size2
] = L
'\0'; /* for a sentinel */
5769 FREE_VAR (is_binary
);
5773 /* We need to cast pattern to (wchar_t*), because we casted this compiled
5774 pattern to (char*) in regex_compile. */
5775 p
= pattern
= (CHAR_T
*)bufp
->buffer
;
5776 pend
= (CHAR_T
*)(bufp
->buffer
+ bufp
->used
);
5780 /* Initialize subexpression text positions to -1 to mark ones that no
5781 start_memory/stop_memory has been seen for. Also initialize the
5782 register information struct. */
5783 for (mcnt
= 1; (unsigned) mcnt
< num_regs
; mcnt
++)
5785 regstart
[mcnt
] = regend
[mcnt
]
5786 = old_regstart
[mcnt
] = old_regend
[mcnt
] = REG_UNSET_VALUE
;
5788 REG_MATCH_NULL_STRING_P (reg_info
[mcnt
]) = MATCH_NULL_UNSET_VALUE
;
5789 IS_ACTIVE (reg_info
[mcnt
]) = 0;
5790 MATCHED_SOMETHING (reg_info
[mcnt
]) = 0;
5791 EVER_MATCHED_SOMETHING (reg_info
[mcnt
]) = 0;
5794 /* We move `string1' into `string2' if the latter's empty -- but not if
5795 `string1' is null. */
5796 if (size2
== 0 && string1
!= NULL
)
5803 mbs_offset2
= mbs_offset1
;
5809 end1
= string1
+ size1
;
5810 end2
= string2
+ size2
;
5812 /* Compute where to stop matching, within the two strings. */
5816 mcnt
= count_mbs_length(mbs_offset1
, stop
);
5817 end_match_1
= string1
+ mcnt
;
5818 end_match_2
= string2
;
5822 if (stop
> csize1
+ csize2
)
5823 stop
= csize1
+ csize2
;
5825 mcnt
= count_mbs_length(mbs_offset2
, stop
-csize1
);
5826 end_match_2
= string2
+ mcnt
;
5829 { /* count_mbs_length return error. */
5836 end_match_1
= string1
+ stop
;
5837 end_match_2
= string2
;
5842 end_match_2
= string2
+ stop
- size1
;
5846 /* `p' scans through the pattern as `d' scans through the data.
5847 `dend' is the end of the input string that `d' points within. `d'
5848 is advanced into the following input string whenever necessary, but
5849 this happens before fetching; therefore, at the beginning of the
5850 loop, `d' can be pointing at the end of a string, but it cannot
5853 if (size1
> 0 && pos
<= csize1
)
5855 mcnt
= count_mbs_length(mbs_offset1
, pos
);
5861 mcnt
= count_mbs_length(mbs_offset2
, pos
-csize1
);
5867 { /* count_mbs_length return error. */
5872 if (size1
> 0 && pos
<= size1
)
5879 d
= string2
+ pos
- size1
;
5884 DEBUG_PRINT1 ("The compiled pattern is:\n");
5885 DEBUG_PRINT_COMPILED_PATTERN (bufp
, p
, pend
);
5886 DEBUG_PRINT1 ("The string to match is: `");
5887 DEBUG_PRINT_DOUBLE_STRING (d
, string1
, size1
, string2
, size2
);
5888 DEBUG_PRINT1 ("'\n");
5890 /* This loops over pattern commands. It exits by returning from the
5891 function if the match is complete, or it drops through if the match
5892 fails at this starting point in the input data. */
5896 DEBUG_PRINT2 ("\n%p: ", p
);
5898 DEBUG_PRINT2 ("\n0x%x: ", p
);
5902 { /* End of pattern means we might have succeeded. */
5903 DEBUG_PRINT1 ("end of pattern ... ");
5905 /* If we haven't matched the entire string, and we want the
5906 longest match, try backtracking. */
5907 if (d
!= end_match_2
)
5909 /* 1 if this match ends in the same string (string1 or string2)
5910 as the best previous match. */
5911 boolean same_str_p
= (FIRST_STRING_P (match_end
)
5912 == MATCHING_IN_FIRST_STRING
);
5913 /* 1 if this match is the best seen so far. */
5914 boolean best_match_p
;
5916 /* AIX compiler got confused when this was combined
5917 with the previous declaration. */
5919 best_match_p
= d
> match_end
;
5921 best_match_p
= !MATCHING_IN_FIRST_STRING
;
5923 DEBUG_PRINT1 ("backtracking.\n");
5925 if (!FAIL_STACK_EMPTY ())
5926 { /* More failure points to try. */
5928 /* If exceeds best match so far, save it. */
5929 if (!best_regs_set
|| best_match_p
)
5931 best_regs_set
= true;
5934 DEBUG_PRINT1 ("\nSAVING match as best so far.\n");
5936 for (mcnt
= 1; (unsigned) mcnt
< num_regs
; mcnt
++)
5938 best_regstart
[mcnt
] = regstart
[mcnt
];
5939 best_regend
[mcnt
] = regend
[mcnt
];
5945 /* If no failure points, don't restore garbage. And if
5946 last match is real best match, don't restore second
5948 else if (best_regs_set
&& !best_match_p
)
5951 /* Restore best match. It may happen that `dend ==
5952 end_match_1' while the restored d is in string2.
5953 For example, the pattern `x.*y.*z' against the
5954 strings `x-' and `y-z-', if the two strings are
5955 not consecutive in memory. */
5956 DEBUG_PRINT1 ("Restoring best registers.\n");
5959 dend
= ((d
>= string1
&& d
<= end1
)
5960 ? end_match_1
: end_match_2
);
5962 for (mcnt
= 1; (unsigned) mcnt
< num_regs
; mcnt
++)
5964 regstart
[mcnt
] = best_regstart
[mcnt
];
5965 regend
[mcnt
] = best_regend
[mcnt
];
5968 } /* d != end_match_2 */
5971 DEBUG_PRINT1 ("Accepting match.\n");
5972 /* If caller wants register contents data back, do it. */
5973 if (regs
&& !bufp
->no_sub
)
5975 /* Have the register data arrays been allocated? */
5976 if (bufp
->regs_allocated
== REGS_UNALLOCATED
)
5977 { /* No. So allocate them with malloc. We need one
5978 extra element beyond `num_regs' for the `-1' marker
5980 regs
->num_regs
= MAX (RE_NREGS
, num_regs
+ 1);
5981 regs
->start
= TALLOC (regs
->num_regs
, regoff_t
);
5982 regs
->end
= TALLOC (regs
->num_regs
, regoff_t
);
5983 if (regs
->start
== NULL
|| regs
->end
== NULL
)
5988 bufp
->regs_allocated
= REGS_REALLOCATE
;
5990 else if (bufp
->regs_allocated
== REGS_REALLOCATE
)
5991 { /* Yes. If we need more elements than were already
5992 allocated, reallocate them. If we need fewer, just
5994 if (regs
->num_regs
< num_regs
+ 1)
5996 regs
->num_regs
= num_regs
+ 1;
5997 RETALLOC (regs
->start
, regs
->num_regs
, regoff_t
);
5998 RETALLOC (regs
->end
, regs
->num_regs
, regoff_t
);
5999 if (regs
->start
== NULL
|| regs
->end
== NULL
)
6008 /* These braces fend off a "empty body in an else-statement"
6009 warning under GCC when assert expands to nothing. */
6010 assert (bufp
->regs_allocated
== REGS_FIXED
);
6013 /* Convert the pointer data in `regstart' and `regend' to
6014 indices. Register zero has to be set differently,
6015 since we haven't kept track of any info for it. */
6016 if (regs
->num_regs
> 0)
6018 regs
->start
[0] = pos
;
6020 if (MATCHING_IN_FIRST_STRING
)
6021 regs
->end
[0] = mbs_offset1
!= NULL
?
6022 mbs_offset1
[d
-string1
] : 0;
6024 regs
->end
[0] = csize1
+ (mbs_offset2
!= NULL
?
6025 mbs_offset2
[d
-string2
] : 0);
6027 regs
->end
[0] = (MATCHING_IN_FIRST_STRING
6028 ? ((regoff_t
) (d
- string1
))
6029 : ((regoff_t
) (d
- string2
+ size1
)));
6033 /* Go through the first `min (num_regs, regs->num_regs)'
6034 registers, since that is all we initialized. */
6035 for (mcnt
= 1; (unsigned) mcnt
< MIN (num_regs
, regs
->num_regs
);
6038 if (REG_UNSET (regstart
[mcnt
]) || REG_UNSET (regend
[mcnt
]))
6039 regs
->start
[mcnt
] = regs
->end
[mcnt
] = -1;
6043 = (regoff_t
) POINTER_TO_OFFSET (regstart
[mcnt
]);
6045 = (regoff_t
) POINTER_TO_OFFSET (regend
[mcnt
]);
6049 /* If the regs structure we return has more elements than
6050 were in the pattern, set the extra elements to -1. If
6051 we (re)allocated the registers, this is the case,
6052 because we always allocate enough to have at least one
6054 for (mcnt
= num_regs
; (unsigned) mcnt
< regs
->num_regs
; mcnt
++)
6055 regs
->start
[mcnt
] = regs
->end
[mcnt
] = -1;
6056 } /* regs && !bufp->no_sub */
6058 DEBUG_PRINT4 ("%u failure points pushed, %u popped (%u remain).\n",
6059 nfailure_points_pushed
, nfailure_points_popped
,
6060 nfailure_points_pushed
- nfailure_points_popped
);
6061 DEBUG_PRINT2 ("%u registers pushed.\n", num_regs_pushed
);
6064 if (MATCHING_IN_FIRST_STRING
)
6065 mcnt
= mbs_offset1
!= NULL
? mbs_offset1
[d
-string1
] : 0;
6067 mcnt
= (mbs_offset2
!= NULL
? mbs_offset2
[d
-string2
] : 0) +
6071 mcnt
= d
- pos
- (MATCHING_IN_FIRST_STRING
6076 DEBUG_PRINT2 ("Returning %d from re_match_2.\n", mcnt
);
6082 /* Otherwise match next pattern command. */
6083 switch (SWITCH_ENUM_CAST ((re_opcode_t
) *p
++))
6085 /* Ignore these. Used to ignore the n of succeed_n's which
6086 currently have n == 0. */
6088 DEBUG_PRINT1 ("EXECUTING no_op.\n");
6092 DEBUG_PRINT1 ("EXECUTING succeed.\n");
6095 /* Match the next n pattern characters exactly. The following
6096 byte in the pattern defines n, and the n bytes after that
6097 are the characters to match. */
6103 DEBUG_PRINT2 ("EXECUTING exactn %d.\n", mcnt
);
6105 /* This is written out as an if-else so we don't waste time
6106 testing `translate' inside the loop. */
6115 if ((UCHAR_T
) translate
[(unsigned char) *d
++]
6121 if (*d
++ != (CHAR_T
) *p
++)
6125 if ((UCHAR_T
) translate
[(unsigned char) *d
++]
6137 if (*d
++ != (CHAR_T
) *p
++) goto fail
;
6141 SET_REGS_MATCHED ();
6145 /* Match any character except possibly a newline or a null. */
6147 DEBUG_PRINT1 ("EXECUTING anychar.\n");
6151 if ((!(bufp
->syntax
& RE_DOT_NEWLINE
) && TRANSLATE (*d
) == '\n')
6152 || (bufp
->syntax
& RE_DOT_NOT_NULL
&& TRANSLATE (*d
) == '\000'))
6155 SET_REGS_MATCHED ();
6156 DEBUG_PRINT2 (" Matched `%ld'.\n", (long int) *d
);
6166 unsigned int i
, char_class_length
, coll_symbol_length
,
6167 equiv_class_length
, ranges_length
, chars_length
, length
;
6168 CHAR_T
*workp
, *workp2
, *charset_top
;
6169 #define WORK_BUFFER_SIZE 128
6170 CHAR_T str_buf
[WORK_BUFFER_SIZE
];
6175 boolean
not = (re_opcode_t
) *(p
- 1) == charset_not
;
6177 DEBUG_PRINT2 ("EXECUTING charset%s.\n", not ? "_not" : "");
6179 c
= TRANSLATE (*d
); /* The character to match. */
6182 nrules
= _NL_CURRENT_WORD (LC_COLLATE
, _NL_COLLATE_NRULES
);
6184 charset_top
= p
- 1;
6185 char_class_length
= *p
++;
6186 coll_symbol_length
= *p
++;
6187 equiv_class_length
= *p
++;
6188 ranges_length
= *p
++;
6189 chars_length
= *p
++;
6190 /* p points charset[6], so the address of the next instruction
6191 (charset[l+m+n+2o+k+p']) equals p[l+m+n+2*o+p'],
6192 where l=length of char_classes, m=length of collating_symbol,
6193 n=equivalence_class, o=length of char_range,
6194 p'=length of character. */
6196 /* Update p to indicate the next instruction. */
6197 p
+= char_class_length
+ coll_symbol_length
+ equiv_class_length
+
6198 2*ranges_length
+ chars_length
;
6200 /* match with char_class? */
6201 for (i
= 0; i
< char_class_length
; i
+= CHAR_CLASS_SIZE
)
6204 uintptr_t alignedp
= ((uintptr_t)workp
6205 + __alignof__(wctype_t) - 1)
6206 & ~(uintptr_t)(__alignof__(wctype_t) - 1);
6207 wctype
= *((wctype_t*)alignedp
);
6208 workp
+= CHAR_CLASS_SIZE
;
6210 if (__iswctype((wint_t)c
, wctype
))
6211 goto char_set_matched
;
6213 if (iswctype((wint_t)c
, wctype
))
6214 goto char_set_matched
;
6218 /* match with collating_symbol? */
6222 const unsigned char *extra
= (const unsigned char *)
6223 _NL_CURRENT (LC_COLLATE
, _NL_COLLATE_SYMB_EXTRAMB
);
6225 for (workp2
= workp
+ coll_symbol_length
; workp
< workp2
;
6229 wextra
= (int32_t*)(extra
+ *workp
++);
6230 for (i
= 0; i
< *wextra
; ++i
)
6231 if (TRANSLATE(d
[i
]) != wextra
[1 + i
])
6236 /* Update d, however d will be incremented at
6237 char_set_matched:, we decrement d here. */
6239 goto char_set_matched
;
6243 else /* (nrules == 0) */
6245 /* If we can't look up collation data, we use wcscoll
6248 for (workp2
= workp
+ coll_symbol_length
; workp
< workp2
;)
6250 const CHAR_T
*backup_d
= d
, *backup_dend
= dend
;
6252 length
= __wcslen (workp
);
6254 length
= wcslen (workp
);
6257 /* If wcscoll(the collating symbol, whole string) > 0,
6258 any substring of the string never match with the
6259 collating symbol. */
6261 if (__wcscoll (workp
, d
) > 0)
6263 if (wcscoll (workp
, d
) > 0)
6266 workp
+= length
+ 1;
6270 /* First, we compare the collating symbol with
6271 the first character of the string.
6272 If it don't match, we add the next character to
6273 the compare buffer in turn. */
6274 for (i
= 0 ; i
< WORK_BUFFER_SIZE
-1 ; i
++, d
++)
6279 if (dend
== end_match_2
)
6285 /* add next character to the compare buffer. */
6286 str_buf
[i
] = TRANSLATE(*d
);
6287 str_buf
[i
+1] = '\0';
6290 match
= __wcscoll (workp
, str_buf
);
6292 match
= wcscoll (workp
, str_buf
);
6295 goto char_set_matched
;
6298 /* (str_buf > workp) indicate (str_buf + X > workp),
6299 because for all X (str_buf + X > str_buf).
6300 So we don't need continue this loop. */
6303 /* Otherwise(str_buf < workp),
6304 (str_buf+next_character) may equals (workp).
6305 So we continue this loop. */
6310 workp
+= length
+ 1;
6313 /* match with equivalence_class? */
6317 const CHAR_T
*backup_d
= d
, *backup_dend
= dend
;
6318 /* Try to match the equivalence class against
6319 those known to the collate implementation. */
6320 const int32_t *table
;
6321 const int32_t *weights
;
6322 const int32_t *extra
;
6323 const int32_t *indirect
;
6328 /* This #include defines a local function! */
6329 # include <locale/weightwc.h>
6331 table
= (const int32_t *)
6332 _NL_CURRENT (LC_COLLATE
, _NL_COLLATE_TABLEWC
);
6333 weights
= (const wint_t *)
6334 _NL_CURRENT (LC_COLLATE
, _NL_COLLATE_WEIGHTWC
);
6335 extra
= (const wint_t *)
6336 _NL_CURRENT (LC_COLLATE
, _NL_COLLATE_EXTRAWC
);
6337 indirect
= (const int32_t *)
6338 _NL_CURRENT (LC_COLLATE
, _NL_COLLATE_INDIRECTWC
);
6340 /* Write 1 collating element to str_buf, and
6344 for (i
= 0 ; idx2
== 0 && i
< WORK_BUFFER_SIZE
- 1; i
++)
6346 cp
= (wint_t*)str_buf
;
6349 if (dend
== end_match_2
)
6354 str_buf
[i
] = TRANSLATE(*(d
+i
));
6355 str_buf
[i
+1] = '\0'; /* sentinel */
6356 idx2
= findidx ((const wint_t**)&cp
);
6359 /* Update d, however d will be incremented at
6360 char_set_matched:, we decrement d here. */
6361 d
= backup_d
+ ((wchar_t*)cp
- (wchar_t*)str_buf
- 1);
6364 if (dend
== end_match_2
)
6373 len
= weights
[idx2
];
6375 for (workp2
= workp
+ equiv_class_length
; workp
< workp2
;
6378 idx
= (int32_t)*workp
;
6379 /* We already checked idx != 0 in regex_compile. */
6381 if (idx2
!= 0 && len
== weights
[idx
])
6384 while (cnt
< len
&& (weights
[idx
+ 1 + cnt
]
6385 == weights
[idx2
+ 1 + cnt
]))
6389 goto char_set_matched
;
6396 else /* (nrules == 0) */
6398 /* If we can't look up collation data, we use wcscoll
6401 for (workp2
= workp
+ equiv_class_length
; workp
< workp2
;)
6403 const CHAR_T
*backup_d
= d
, *backup_dend
= dend
;
6405 length
= __wcslen (workp
);
6407 length
= wcslen (workp
);
6410 /* If wcscoll(the collating symbol, whole string) > 0,
6411 any substring of the string never match with the
6412 collating symbol. */
6414 if (__wcscoll (workp
, d
) > 0)
6416 if (wcscoll (workp
, d
) > 0)
6419 workp
+= length
+ 1;
6423 /* First, we compare the equivalence class with
6424 the first character of the string.
6425 If it don't match, we add the next character to
6426 the compare buffer in turn. */
6427 for (i
= 0 ; i
< WORK_BUFFER_SIZE
- 1 ; i
++, d
++)
6432 if (dend
== end_match_2
)
6438 /* add next character to the compare buffer. */
6439 str_buf
[i
] = TRANSLATE(*d
);
6440 str_buf
[i
+1] = '\0';
6443 match
= __wcscoll (workp
, str_buf
);
6445 match
= wcscoll (workp
, str_buf
);
6449 goto char_set_matched
;
6452 /* (str_buf > workp) indicate (str_buf + X > workp),
6453 because for all X (str_buf + X > str_buf).
6454 So we don't need continue this loop. */
6457 /* Otherwise(str_buf < workp),
6458 (str_buf+next_character) may equals (workp).
6459 So we continue this loop. */
6464 workp
+= length
+ 1;
6468 /* match with char_range? */
6472 uint32_t collseqval
;
6473 const char *collseq
= (const char *)
6474 _NL_CURRENT(LC_COLLATE
, _NL_COLLATE_COLLSEQWC
);
6476 collseqval
= collseq_table_lookup (collseq
, c
);
6478 for (; workp
< p
- chars_length
;)
6480 uint32_t start_val
, end_val
;
6482 /* We already compute the collation sequence value
6483 of the characters (or collating symbols). */
6484 start_val
= (uint32_t) *workp
++; /* range_start */
6485 end_val
= (uint32_t) *workp
++; /* range_end */
6487 if (start_val
<= collseqval
&& collseqval
<= end_val
)
6488 goto char_set_matched
;
6494 /* We set range_start_char at str_buf[0], range_end_char
6495 at str_buf[4], and compared char at str_buf[2]. */
6500 for (; workp
< p
- chars_length
;)
6502 wchar_t *range_start_char
, *range_end_char
;
6504 /* match if (range_start_char <= c <= range_end_char). */
6506 /* If range_start(or end) < 0, we assume -range_start(end)
6507 is the offset of the collating symbol which is specified
6508 as the character of the range start(end). */
6512 range_start_char
= charset_top
- (*workp
++);
6515 str_buf
[0] = *workp
++;
6516 range_start_char
= str_buf
;
6521 range_end_char
= charset_top
- (*workp
++);
6524 str_buf
[4] = *workp
++;
6525 range_end_char
= str_buf
+ 4;
6529 if (__wcscoll (range_start_char
, str_buf
+2) <= 0
6530 && __wcscoll (str_buf
+2, range_end_char
) <= 0)
6532 if (wcscoll (range_start_char
, str_buf
+2) <= 0
6533 && wcscoll (str_buf
+2, range_end_char
) <= 0)
6535 goto char_set_matched
;
6539 /* match with char? */
6540 for (; workp
< p
; workp
++)
6542 goto char_set_matched
;
6549 /* Cast to `unsigned' instead of `unsigned char' in case the
6550 bit list is a full 32 bytes long. */
6551 if (c
< (unsigned) (*p
* BYTEWIDTH
)
6552 && p
[1 + c
/ BYTEWIDTH
] & (1 << (c
% BYTEWIDTH
)))
6557 if (!not) goto fail
;
6558 #undef WORK_BUFFER_SIZE
6560 SET_REGS_MATCHED ();
6566 /* The beginning of a group is represented by start_memory.
6567 The arguments are the register number in the next byte, and the
6568 number of groups inner to this one in the next. The text
6569 matched within the group is recorded (in the internal
6570 registers data structure) under the register number. */
6572 DEBUG_PRINT3 ("EXECUTING start_memory %ld (%ld):\n",
6573 (long int) *p
, (long int) p
[1]);
6575 /* Find out if this group can match the empty string. */
6576 p1
= p
; /* To send to group_match_null_string_p. */
6578 if (REG_MATCH_NULL_STRING_P (reg_info
[*p
]) == MATCH_NULL_UNSET_VALUE
)
6579 REG_MATCH_NULL_STRING_P (reg_info
[*p
])
6580 = PREFIX(group_match_null_string_p
) (&p1
, pend
, reg_info
);
6582 /* Save the position in the string where we were the last time
6583 we were at this open-group operator in case the group is
6584 operated upon by a repetition operator, e.g., with `(a*)*b'
6585 against `ab'; then we want to ignore where we are now in
6586 the string in case this attempt to match fails. */
6587 old_regstart
[*p
] = REG_MATCH_NULL_STRING_P (reg_info
[*p
])
6588 ? REG_UNSET (regstart
[*p
]) ? d
: regstart
[*p
]
6590 DEBUG_PRINT2 (" old_regstart: %d\n",
6591 POINTER_TO_OFFSET (old_regstart
[*p
]));
6594 DEBUG_PRINT2 (" regstart: %d\n", POINTER_TO_OFFSET (regstart
[*p
]));
6596 IS_ACTIVE (reg_info
[*p
]) = 1;
6597 MATCHED_SOMETHING (reg_info
[*p
]) = 0;
6599 /* Clear this whenever we change the register activity status. */
6600 set_regs_matched_done
= 0;
6602 /* This is the new highest active register. */
6603 highest_active_reg
= *p
;
6605 /* If nothing was active before, this is the new lowest active
6607 if (lowest_active_reg
== NO_LOWEST_ACTIVE_REG
)
6608 lowest_active_reg
= *p
;
6610 /* Move past the register number and inner group count. */
6612 just_past_start_mem
= p
;
6617 /* The stop_memory opcode represents the end of a group. Its
6618 arguments are the same as start_memory's: the register
6619 number, and the number of inner groups. */
6621 DEBUG_PRINT3 ("EXECUTING stop_memory %ld (%ld):\n",
6622 (long int) *p
, (long int) p
[1]);
6624 /* We need to save the string position the last time we were at
6625 this close-group operator in case the group is operated
6626 upon by a repetition operator, e.g., with `((a*)*(b*)*)*'
6627 against `aba'; then we want to ignore where we are now in
6628 the string in case this attempt to match fails. */
6629 old_regend
[*p
] = REG_MATCH_NULL_STRING_P (reg_info
[*p
])
6630 ? REG_UNSET (regend
[*p
]) ? d
: regend
[*p
]
6632 DEBUG_PRINT2 (" old_regend: %d\n",
6633 POINTER_TO_OFFSET (old_regend
[*p
]));
6636 DEBUG_PRINT2 (" regend: %d\n", POINTER_TO_OFFSET (regend
[*p
]));
6638 /* This register isn't active anymore. */
6639 IS_ACTIVE (reg_info
[*p
]) = 0;
6641 /* Clear this whenever we change the register activity status. */
6642 set_regs_matched_done
= 0;
6644 /* If this was the only register active, nothing is active
6646 if (lowest_active_reg
== highest_active_reg
)
6648 lowest_active_reg
= NO_LOWEST_ACTIVE_REG
;
6649 highest_active_reg
= NO_HIGHEST_ACTIVE_REG
;
6652 { /* We must scan for the new highest active register, since
6653 it isn't necessarily one less than now: consider
6654 (a(b)c(d(e)f)g). When group 3 ends, after the f), the
6655 new highest active register is 1. */
6657 while (r
> 0 && !IS_ACTIVE (reg_info
[r
]))
6660 /* If we end up at register zero, that means that we saved
6661 the registers as the result of an `on_failure_jump', not
6662 a `start_memory', and we jumped to past the innermost
6663 `stop_memory'. For example, in ((.)*) we save
6664 registers 1 and 2 as a result of the *, but when we pop
6665 back to the second ), we are at the stop_memory 1.
6666 Thus, nothing is active. */
6669 lowest_active_reg
= NO_LOWEST_ACTIVE_REG
;
6670 highest_active_reg
= NO_HIGHEST_ACTIVE_REG
;
6673 highest_active_reg
= r
;
6676 /* If just failed to match something this time around with a
6677 group that's operated on by a repetition operator, try to
6678 force exit from the ``loop'', and restore the register
6679 information for this group that we had before trying this
6681 if ((!MATCHED_SOMETHING (reg_info
[*p
])
6682 || just_past_start_mem
== p
- 1)
6685 boolean is_a_jump_n
= false;
6689 switch ((re_opcode_t
) *p1
++)
6693 case pop_failure_jump
:
6694 case maybe_pop_jump
:
6696 case dummy_failure_jump
:
6697 EXTRACT_NUMBER_AND_INCR (mcnt
, p1
);
6699 p1
+= OFFSET_ADDRESS_SIZE
;
6707 /* If the next operation is a jump backwards in the pattern
6708 to an on_failure_jump right before the start_memory
6709 corresponding to this stop_memory, exit from the loop
6710 by forcing a failure after pushing on the stack the
6711 on_failure_jump's jump in the pattern, and d. */
6712 if (mcnt
< 0 && (re_opcode_t
) *p1
== on_failure_jump
6713 && (re_opcode_t
) p1
[1+OFFSET_ADDRESS_SIZE
] == start_memory
6714 && p1
[2+OFFSET_ADDRESS_SIZE
] == *p
)
6716 /* If this group ever matched anything, then restore
6717 what its registers were before trying this last
6718 failed match, e.g., with `(a*)*b' against `ab' for
6719 regstart[1], and, e.g., with `((a*)*(b*)*)*'
6720 against `aba' for regend[3].
6722 Also restore the registers for inner groups for,
6723 e.g., `((a*)(b*))*' against `aba' (register 3 would
6724 otherwise get trashed). */
6726 if (EVER_MATCHED_SOMETHING (reg_info
[*p
]))
6730 EVER_MATCHED_SOMETHING (reg_info
[*p
]) = 0;
6732 /* Restore this and inner groups' (if any) registers. */
6733 for (r
= *p
; r
< (unsigned) *p
+ (unsigned) *(p
+ 1);
6736 regstart
[r
] = old_regstart
[r
];
6738 /* xx why this test? */
6739 if (old_regend
[r
] >= regstart
[r
])
6740 regend
[r
] = old_regend
[r
];
6744 EXTRACT_NUMBER_AND_INCR (mcnt
, p1
);
6745 PUSH_FAILURE_POINT (p1
+ mcnt
, d
, -2);
6751 /* Move past the register number and the inner group count. */
6756 /* \<digit> has been turned into a `duplicate' command which is
6757 followed by the numeric value of <digit> as the register number. */
6760 register const CHAR_T
*d2
, *dend2
;
6761 int regno
= *p
++; /* Get which register to match against. */
6762 DEBUG_PRINT2 ("EXECUTING duplicate %d.\n", regno
);
6764 /* Can't back reference a group which we've never matched. */
6765 if (REG_UNSET (regstart
[regno
]) || REG_UNSET (regend
[regno
]))
6768 /* Where in input to try to start matching. */
6769 d2
= regstart
[regno
];
6771 /* Where to stop matching; if both the place to start and
6772 the place to stop matching are in the same string, then
6773 set to the place to stop, otherwise, for now have to use
6774 the end of the first string. */
6776 dend2
= ((FIRST_STRING_P (regstart
[regno
])
6777 == FIRST_STRING_P (regend
[regno
]))
6778 ? regend
[regno
] : end_match_1
);
6781 /* If necessary, advance to next segment in register
6785 if (dend2
== end_match_2
) break;
6786 if (dend2
== regend
[regno
]) break;
6788 /* End of string1 => advance to string2. */
6790 dend2
= regend
[regno
];
6792 /* At end of register contents => success */
6793 if (d2
== dend2
) break;
6795 /* If necessary, advance to next segment in data. */
6798 /* How many characters left in this segment to match. */
6801 /* Want how many consecutive characters we can match in
6802 one shot, so, if necessary, adjust the count. */
6803 if (mcnt
> dend2
- d2
)
6806 /* Compare that many; failure if mismatch, else move
6809 ? PREFIX(bcmp_translate
) (d
, d2
, mcnt
, translate
)
6810 : memcmp (d
, d2
, mcnt
*sizeof(UCHAR_T
)))
6812 d
+= mcnt
, d2
+= mcnt
;
6814 /* Do this because we've match some characters. */
6815 SET_REGS_MATCHED ();
6821 /* begline matches the empty string at the beginning of the string
6822 (unless `not_bol' is set in `bufp'), and, if
6823 `newline_anchor' is set, after newlines. */
6825 DEBUG_PRINT1 ("EXECUTING begline.\n");
6827 if (AT_STRINGS_BEG (d
))
6829 if (!bufp
->not_bol
) break;
6831 else if (d
[-1] == '\n' && bufp
->newline_anchor
)
6835 /* In all other cases, we fail. */
6839 /* endline is the dual of begline. */
6841 DEBUG_PRINT1 ("EXECUTING endline.\n");
6843 if (AT_STRINGS_END (d
))
6845 if (!bufp
->not_eol
) break;
6848 /* We have to ``prefetch'' the next character. */
6849 else if ((d
== end1
? *string2
: *d
) == '\n'
6850 && bufp
->newline_anchor
)
6857 /* Match at the very beginning of the data. */
6859 DEBUG_PRINT1 ("EXECUTING begbuf.\n");
6860 if (AT_STRINGS_BEG (d
))
6865 /* Match at the very end of the data. */
6867 DEBUG_PRINT1 ("EXECUTING endbuf.\n");
6868 if (AT_STRINGS_END (d
))
6873 /* on_failure_keep_string_jump is used to optimize `.*\n'. It
6874 pushes NULL as the value for the string on the stack. Then
6875 `pop_failure_point' will keep the current value for the
6876 string, instead of restoring it. To see why, consider
6877 matching `foo\nbar' against `.*\n'. The .* matches the foo;
6878 then the . fails against the \n. But the next thing we want
6879 to do is match the \n against the \n; if we restored the
6880 string value, we would be back at the foo.
6882 Because this is used only in specific cases, we don't need to
6883 check all the things that `on_failure_jump' does, to make
6884 sure the right things get saved on the stack. Hence we don't
6885 share its code. The only reason to push anything on the
6886 stack at all is that otherwise we would have to change
6887 `anychar's code to do something besides goto fail in this
6888 case; that seems worse than this. */
6889 case on_failure_keep_string_jump
:
6890 DEBUG_PRINT1 ("EXECUTING on_failure_keep_string_jump");
6892 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
6894 DEBUG_PRINT3 (" %d (to %p):\n", mcnt
, p
+ mcnt
);
6896 DEBUG_PRINT3 (" %d (to 0x%x):\n", mcnt
, p
+ mcnt
);
6899 PUSH_FAILURE_POINT (p
+ mcnt
, NULL
, -2);
6903 /* Uses of on_failure_jump:
6905 Each alternative starts with an on_failure_jump that points
6906 to the beginning of the next alternative. Each alternative
6907 except the last ends with a jump that in effect jumps past
6908 the rest of the alternatives. (They really jump to the
6909 ending jump of the following alternative, because tensioning
6910 these jumps is a hassle.)
6912 Repeats start with an on_failure_jump that points past both
6913 the repetition text and either the following jump or
6914 pop_failure_jump back to this on_failure_jump. */
6915 case on_failure_jump
:
6917 DEBUG_PRINT1 ("EXECUTING on_failure_jump");
6919 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
6921 DEBUG_PRINT3 (" %d (to %p)", mcnt
, p
+ mcnt
);
6923 DEBUG_PRINT3 (" %d (to 0x%x)", mcnt
, p
+ mcnt
);
6926 /* If this on_failure_jump comes right before a group (i.e.,
6927 the original * applied to a group), save the information
6928 for that group and all inner ones, so that if we fail back
6929 to this point, the group's information will be correct.
6930 For example, in \(a*\)*\1, we need the preceding group,
6931 and in \(zz\(a*\)b*\)\2, we need the inner group. */
6933 /* We can't use `p' to check ahead because we push
6934 a failure point to `p + mcnt' after we do this. */
6937 /* We need to skip no_op's before we look for the
6938 start_memory in case this on_failure_jump is happening as
6939 the result of a completed succeed_n, as in \(a\)\{1,3\}b\1
6941 while (p1
< pend
&& (re_opcode_t
) *p1
== no_op
)
6944 if (p1
< pend
&& (re_opcode_t
) *p1
== start_memory
)
6946 /* We have a new highest active register now. This will
6947 get reset at the start_memory we are about to get to,
6948 but we will have saved all the registers relevant to
6949 this repetition op, as described above. */
6950 highest_active_reg
= *(p1
+ 1) + *(p1
+ 2);
6951 if (lowest_active_reg
== NO_LOWEST_ACTIVE_REG
)
6952 lowest_active_reg
= *(p1
+ 1);
6955 DEBUG_PRINT1 (":\n");
6956 PUSH_FAILURE_POINT (p
+ mcnt
, d
, -2);
6960 /* A smart repeat ends with `maybe_pop_jump'.
6961 We change it to either `pop_failure_jump' or `jump'. */
6962 case maybe_pop_jump
:
6963 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
6964 DEBUG_PRINT2 ("EXECUTING maybe_pop_jump %d.\n", mcnt
);
6966 register UCHAR_T
*p2
= p
;
6968 /* Compare the beginning of the repeat with what in the
6969 pattern follows its end. If we can establish that there
6970 is nothing that they would both match, i.e., that we
6971 would have to backtrack because of (as in, e.g., `a*a')
6972 then we can change to pop_failure_jump, because we'll
6973 never have to backtrack.
6975 This is not true in the case of alternatives: in
6976 `(a|ab)*' we do need to backtrack to the `ab' alternative
6977 (e.g., if the string was `ab'). But instead of trying to
6978 detect that here, the alternative has put on a dummy
6979 failure point which is what we will end up popping. */
6981 /* Skip over open/close-group commands.
6982 If what follows this loop is a ...+ construct,
6983 look at what begins its body, since we will have to
6984 match at least one of that. */
6988 && ((re_opcode_t
) *p2
== stop_memory
6989 || (re_opcode_t
) *p2
== start_memory
))
6991 else if (p2
+ 2 + 2 * OFFSET_ADDRESS_SIZE
< pend
6992 && (re_opcode_t
) *p2
== dummy_failure_jump
)
6993 p2
+= 2 + 2 * OFFSET_ADDRESS_SIZE
;
6999 /* p1[0] ... p1[2] are the `on_failure_jump' corresponding
7000 to the `maybe_finalize_jump' of this case. Examine what
7003 /* If we're at the end of the pattern, we can change. */
7006 /* Consider what happens when matching ":\(.*\)"
7007 against ":/". I don't really understand this code
7009 p
[-(1+OFFSET_ADDRESS_SIZE
)] = (UCHAR_T
)
7012 (" End of pattern: change to `pop_failure_jump'.\n");
7015 else if ((re_opcode_t
) *p2
== exactn
7017 || (re_opcode_t
) *p2
== exactn_bin
7019 || (bufp
->newline_anchor
&& (re_opcode_t
) *p2
== endline
))
7022 = *p2
== (UCHAR_T
) endline
? '\n' : p2
[2];
7024 if (((re_opcode_t
) p1
[1+OFFSET_ADDRESS_SIZE
] == exactn
7026 || (re_opcode_t
) p1
[1+OFFSET_ADDRESS_SIZE
] == exactn_bin
7028 ) && p1
[3+OFFSET_ADDRESS_SIZE
] != c
)
7030 p
[-(1+OFFSET_ADDRESS_SIZE
)] = (UCHAR_T
)
7033 DEBUG_PRINT3 (" %C != %C => pop_failure_jump.\n",
7035 (wint_t) p1
[3+OFFSET_ADDRESS_SIZE
]);
7037 DEBUG_PRINT3 (" %c != %c => pop_failure_jump.\n",
7039 (char) p1
[3+OFFSET_ADDRESS_SIZE
]);
7044 else if ((re_opcode_t
) p1
[3] == charset
7045 || (re_opcode_t
) p1
[3] == charset_not
)
7047 int not = (re_opcode_t
) p1
[3] == charset_not
;
7049 if (c
< (unsigned) (p1
[4] * BYTEWIDTH
)
7050 && p1
[5 + c
/ BYTEWIDTH
] & (1 << (c
% BYTEWIDTH
)))
7053 /* `not' is equal to 1 if c would match, which means
7054 that we can't change to pop_failure_jump. */
7057 p
[-3] = (unsigned char) pop_failure_jump
;
7058 DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
7061 #endif /* not WCHAR */
7064 else if ((re_opcode_t
) *p2
== charset
)
7066 /* We win if the first character of the loop is not part
7068 if ((re_opcode_t
) p1
[3] == exactn
7069 && ! ((int) p2
[1] * BYTEWIDTH
> (int) p1
[5]
7070 && (p2
[2 + p1
[5] / BYTEWIDTH
]
7071 & (1 << (p1
[5] % BYTEWIDTH
)))))
7073 p
[-3] = (unsigned char) pop_failure_jump
;
7074 DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
7077 else if ((re_opcode_t
) p1
[3] == charset_not
)
7080 /* We win if the charset_not inside the loop
7081 lists every character listed in the charset after. */
7082 for (idx
= 0; idx
< (int) p2
[1]; idx
++)
7083 if (! (p2
[2 + idx
] == 0
7084 || (idx
< (int) p1
[4]
7085 && ((p2
[2 + idx
] & ~ p1
[5 + idx
]) == 0))))
7090 p
[-3] = (unsigned char) pop_failure_jump
;
7091 DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
7094 else if ((re_opcode_t
) p1
[3] == charset
)
7097 /* We win if the charset inside the loop
7098 has no overlap with the one after the loop. */
7100 idx
< (int) p2
[1] && idx
< (int) p1
[4];
7102 if ((p2
[2 + idx
] & p1
[5 + idx
]) != 0)
7105 if (idx
== p2
[1] || idx
== p1
[4])
7107 p
[-3] = (unsigned char) pop_failure_jump
;
7108 DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
7112 #endif /* not WCHAR */
7114 p
-= OFFSET_ADDRESS_SIZE
; /* Point at relative address again. */
7115 if ((re_opcode_t
) p
[-1] != pop_failure_jump
)
7117 p
[-1] = (UCHAR_T
) jump
;
7118 DEBUG_PRINT1 (" Match => jump.\n");
7119 goto unconditional_jump
;
7121 /* Note fall through. */
7124 /* The end of a simple repeat has a pop_failure_jump back to
7125 its matching on_failure_jump, where the latter will push a
7126 failure point. The pop_failure_jump takes off failure
7127 points put on by this pop_failure_jump's matching
7128 on_failure_jump; we got through the pattern to here from the
7129 matching on_failure_jump, so didn't fail. */
7130 case pop_failure_jump
:
7132 /* We need to pass separate storage for the lowest and
7133 highest registers, even though we don't care about the
7134 actual values. Otherwise, we will restore only one
7135 register from the stack, since lowest will == highest in
7136 `pop_failure_point'. */
7137 active_reg_t dummy_low_reg
, dummy_high_reg
;
7138 UCHAR_T
*pdummy
= NULL
;
7139 const CHAR_T
*sdummy
= NULL
;
7141 DEBUG_PRINT1 ("EXECUTING pop_failure_jump.\n");
7142 POP_FAILURE_POINT (sdummy
, pdummy
,
7143 dummy_low_reg
, dummy_high_reg
,
7144 reg_dummy
, reg_dummy
, reg_info_dummy
);
7146 /* Note fall through. */
7150 DEBUG_PRINT2 ("\n%p: ", p
);
7152 DEBUG_PRINT2 ("\n0x%x: ", p
);
7154 /* Note fall through. */
7156 /* Unconditionally jump (without popping any failure points). */
7158 EXTRACT_NUMBER_AND_INCR (mcnt
, p
); /* Get the amount to jump. */
7159 DEBUG_PRINT2 ("EXECUTING jump %d ", mcnt
);
7160 p
+= mcnt
; /* Do the jump. */
7162 DEBUG_PRINT2 ("(to %p).\n", p
);
7164 DEBUG_PRINT2 ("(to 0x%x).\n", p
);
7169 /* We need this opcode so we can detect where alternatives end
7170 in `group_match_null_string_p' et al. */
7172 DEBUG_PRINT1 ("EXECUTING jump_past_alt.\n");
7173 goto unconditional_jump
;
7176 /* Normally, the on_failure_jump pushes a failure point, which
7177 then gets popped at pop_failure_jump. We will end up at
7178 pop_failure_jump, also, and with a pattern of, say, `a+', we
7179 are skipping over the on_failure_jump, so we have to push
7180 something meaningless for pop_failure_jump to pop. */
7181 case dummy_failure_jump
:
7182 DEBUG_PRINT1 ("EXECUTING dummy_failure_jump.\n");
7183 /* It doesn't matter what we push for the string here. What
7184 the code at `fail' tests is the value for the pattern. */
7185 PUSH_FAILURE_POINT (NULL
, NULL
, -2);
7186 goto unconditional_jump
;
7189 /* At the end of an alternative, we need to push a dummy failure
7190 point in case we are followed by a `pop_failure_jump', because
7191 we don't want the failure point for the alternative to be
7192 popped. For example, matching `(a|ab)*' against `aab'
7193 requires that we match the `ab' alternative. */
7194 case push_dummy_failure
:
7195 DEBUG_PRINT1 ("EXECUTING push_dummy_failure.\n");
7196 /* See comments just above at `dummy_failure_jump' about the
7198 PUSH_FAILURE_POINT (NULL
, NULL
, -2);
7201 /* Have to succeed matching what follows at least n times.
7202 After that, handle like `on_failure_jump'. */
7204 EXTRACT_NUMBER (mcnt
, p
+ OFFSET_ADDRESS_SIZE
);
7205 DEBUG_PRINT2 ("EXECUTING succeed_n %d.\n", mcnt
);
7208 /* Originally, this is how many times we HAVE to succeed. */
7212 p
+= OFFSET_ADDRESS_SIZE
;
7213 STORE_NUMBER_AND_INCR (p
, mcnt
);
7215 DEBUG_PRINT3 (" Setting %p to %d.\n", p
- OFFSET_ADDRESS_SIZE
7218 DEBUG_PRINT3 (" Setting 0x%x to %d.\n", p
- OFFSET_ADDRESS_SIZE
7225 DEBUG_PRINT2 (" Setting two bytes from %p to no_op.\n",
7226 p
+ OFFSET_ADDRESS_SIZE
);
7228 DEBUG_PRINT2 (" Setting two bytes from 0x%x to no_op.\n",
7229 p
+ OFFSET_ADDRESS_SIZE
);
7233 p
[1] = (UCHAR_T
) no_op
;
7235 p
[2] = (UCHAR_T
) no_op
;
7236 p
[3] = (UCHAR_T
) no_op
;
7243 EXTRACT_NUMBER (mcnt
, p
+ OFFSET_ADDRESS_SIZE
);
7244 DEBUG_PRINT2 ("EXECUTING jump_n %d.\n", mcnt
);
7246 /* Originally, this is how many times we CAN jump. */
7250 STORE_NUMBER (p
+ OFFSET_ADDRESS_SIZE
, mcnt
);
7253 DEBUG_PRINT3 (" Setting %p to %d.\n", p
+ OFFSET_ADDRESS_SIZE
,
7256 DEBUG_PRINT3 (" Setting 0x%x to %d.\n", p
+ OFFSET_ADDRESS_SIZE
,
7259 goto unconditional_jump
;
7261 /* If don't have to jump any more, skip over the rest of command. */
7263 p
+= 2 * OFFSET_ADDRESS_SIZE
;
7268 DEBUG_PRINT1 ("EXECUTING set_number_at.\n");
7270 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
7272 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
7274 DEBUG_PRINT3 (" Setting %p to %d.\n", p1
, mcnt
);
7276 DEBUG_PRINT3 (" Setting 0x%x to %d.\n", p1
, mcnt
);
7278 STORE_NUMBER (p1
, mcnt
);
7283 /* The DEC Alpha C compiler 3.x generates incorrect code for the
7284 test WORDCHAR_P (d - 1) != WORDCHAR_P (d) in the expansion of
7285 AT_WORD_BOUNDARY, so this code is disabled. Expanding the
7286 macro and introducing temporary variables works around the bug. */
7289 DEBUG_PRINT1 ("EXECUTING wordbound.\n");
7290 if (AT_WORD_BOUNDARY (d
))
7295 DEBUG_PRINT1 ("EXECUTING notwordbound.\n");
7296 if (AT_WORD_BOUNDARY (d
))
7302 boolean prevchar
, thischar
;
7304 DEBUG_PRINT1 ("EXECUTING wordbound.\n");
7305 if (AT_STRINGS_BEG (d
) || AT_STRINGS_END (d
))
7308 prevchar
= WORDCHAR_P (d
- 1);
7309 thischar
= WORDCHAR_P (d
);
7310 if (prevchar
!= thischar
)
7317 boolean prevchar
, thischar
;
7319 DEBUG_PRINT1 ("EXECUTING notwordbound.\n");
7320 if (AT_STRINGS_BEG (d
) || AT_STRINGS_END (d
))
7323 prevchar
= WORDCHAR_P (d
- 1);
7324 thischar
= WORDCHAR_P (d
);
7325 if (prevchar
!= thischar
)
7332 DEBUG_PRINT1 ("EXECUTING wordbeg.\n");
7333 if (!AT_STRINGS_END (d
) && WORDCHAR_P (d
)
7334 && (AT_STRINGS_BEG (d
) || !WORDCHAR_P (d
- 1)))
7339 DEBUG_PRINT1 ("EXECUTING wordend.\n");
7340 if (!AT_STRINGS_BEG (d
) && WORDCHAR_P (d
- 1)
7341 && (AT_STRINGS_END (d
) || !WORDCHAR_P (d
)))
7347 DEBUG_PRINT1 ("EXECUTING before_dot.\n");
7348 if (PTR_CHAR_POS ((unsigned char *) d
) >= point
)
7353 DEBUG_PRINT1 ("EXECUTING at_dot.\n");
7354 if (PTR_CHAR_POS ((unsigned char *) d
) != point
)
7359 DEBUG_PRINT1 ("EXECUTING after_dot.\n");
7360 if (PTR_CHAR_POS ((unsigned char *) d
) <= point
)
7365 DEBUG_PRINT2 ("EXECUTING syntaxspec %d.\n", mcnt
);
7370 DEBUG_PRINT1 ("EXECUTING Emacs wordchar.\n");
7374 /* Can't use *d++ here; SYNTAX may be an unsafe macro. */
7376 if (SYNTAX (d
[-1]) != (enum syntaxcode
) mcnt
)
7378 SET_REGS_MATCHED ();
7382 DEBUG_PRINT2 ("EXECUTING notsyntaxspec %d.\n", mcnt
);
7384 goto matchnotsyntax
;
7387 DEBUG_PRINT1 ("EXECUTING Emacs notwordchar.\n");
7391 /* Can't use *d++ here; SYNTAX may be an unsafe macro. */
7393 if (SYNTAX (d
[-1]) == (enum syntaxcode
) mcnt
)
7395 SET_REGS_MATCHED ();
7398 #else /* not emacs */
7400 DEBUG_PRINT1 ("EXECUTING non-Emacs wordchar.\n");
7402 if (!WORDCHAR_P (d
))
7404 SET_REGS_MATCHED ();
7409 DEBUG_PRINT1 ("EXECUTING non-Emacs notwordchar.\n");
7413 SET_REGS_MATCHED ();
7416 #endif /* not emacs */
7421 continue; /* Successfully executed one pattern command; keep going. */
7424 /* We goto here if a matching operation fails. */
7426 if (!FAIL_STACK_EMPTY ())
7427 { /* A restart point is known. Restore to that state. */
7428 DEBUG_PRINT1 ("\nFAIL:\n");
7429 POP_FAILURE_POINT (d
, p
,
7430 lowest_active_reg
, highest_active_reg
,
7431 regstart
, regend
, reg_info
);
7433 /* If this failure point is a dummy, try the next one. */
7437 /* If we failed to the end of the pattern, don't examine *p. */
7441 boolean is_a_jump_n
= false;
7443 /* If failed to a backwards jump that's part of a repetition
7444 loop, need to pop this failure point and use the next one. */
7445 switch ((re_opcode_t
) *p
)
7449 case maybe_pop_jump
:
7450 case pop_failure_jump
:
7453 EXTRACT_NUMBER_AND_INCR (mcnt
, p1
);
7456 if ((is_a_jump_n
&& (re_opcode_t
) *p1
== succeed_n
)
7458 && (re_opcode_t
) *p1
== on_failure_jump
))
7466 if (d
>= string1
&& d
<= end1
)
7470 break; /* Matching at this starting point really fails. */
7474 goto restore_best_regs
;
7478 return -1; /* Failure to match. */
7481 /* Subroutine definitions for re_match_2. */
7484 /* We are passed P pointing to a register number after a start_memory.
7486 Return true if the pattern up to the corresponding stop_memory can
7487 match the empty string, and false otherwise.
7489 If we find the matching stop_memory, sets P to point to one past its number.
7490 Otherwise, sets P to an undefined byte less than or equal to END.
7492 We don't handle duplicates properly (yet). */
7495 PREFIX(group_match_null_string_p
) (UCHAR_T
**p
, UCHAR_T
*end
,
7496 PREFIX(register_info_type
) *reg_info
)
7499 /* Point to after the args to the start_memory. */
7500 UCHAR_T
*p1
= *p
+ 2;
7504 /* Skip over opcodes that can match nothing, and return true or
7505 false, as appropriate, when we get to one that can't, or to the
7506 matching stop_memory. */
7508 switch ((re_opcode_t
) *p1
)
7510 /* Could be either a loop or a series of alternatives. */
7511 case on_failure_jump
:
7513 EXTRACT_NUMBER_AND_INCR (mcnt
, p1
);
7515 /* If the next operation is not a jump backwards in the
7520 /* Go through the on_failure_jumps of the alternatives,
7521 seeing if any of the alternatives cannot match nothing.
7522 The last alternative starts with only a jump,
7523 whereas the rest start with on_failure_jump and end
7524 with a jump, e.g., here is the pattern for `a|b|c':
7526 /on_failure_jump/0/6/exactn/1/a/jump_past_alt/0/6
7527 /on_failure_jump/0/6/exactn/1/b/jump_past_alt/0/3
7530 So, we have to first go through the first (n-1)
7531 alternatives and then deal with the last one separately. */
7534 /* Deal with the first (n-1) alternatives, which start
7535 with an on_failure_jump (see above) that jumps to right
7536 past a jump_past_alt. */
7538 while ((re_opcode_t
) p1
[mcnt
-(1+OFFSET_ADDRESS_SIZE
)] ==
7541 /* `mcnt' holds how many bytes long the alternative
7542 is, including the ending `jump_past_alt' and
7545 if (!PREFIX(alt_match_null_string_p
) (p1
, p1
+ mcnt
-
7546 (1 + OFFSET_ADDRESS_SIZE
),
7550 /* Move to right after this alternative, including the
7554 /* Break if it's the beginning of an n-th alternative
7555 that doesn't begin with an on_failure_jump. */
7556 if ((re_opcode_t
) *p1
!= on_failure_jump
)
7559 /* Still have to check that it's not an n-th
7560 alternative that starts with an on_failure_jump. */
7562 EXTRACT_NUMBER_AND_INCR (mcnt
, p1
);
7563 if ((re_opcode_t
) p1
[mcnt
-(1+OFFSET_ADDRESS_SIZE
)] !=
7566 /* Get to the beginning of the n-th alternative. */
7567 p1
-= 1 + OFFSET_ADDRESS_SIZE
;
7572 /* Deal with the last alternative: go back and get number
7573 of the `jump_past_alt' just before it. `mcnt' contains
7574 the length of the alternative. */
7575 EXTRACT_NUMBER (mcnt
, p1
- OFFSET_ADDRESS_SIZE
);
7577 if (!PREFIX(alt_match_null_string_p
) (p1
, p1
+ mcnt
, reg_info
))
7580 p1
+= mcnt
; /* Get past the n-th alternative. */
7586 assert (p1
[1] == **p
);
7592 if (!PREFIX(common_op_match_null_string_p
) (&p1
, end
, reg_info
))
7595 } /* while p1 < end */
7598 } /* group_match_null_string_p */
7601 /* Similar to group_match_null_string_p, but doesn't deal with alternatives:
7602 It expects P to be the first byte of a single alternative and END one
7603 byte past the last. The alternative can contain groups. */
7606 PREFIX(alt_match_null_string_p
) (UCHAR_T
*p
, UCHAR_T
*end
,
7607 PREFIX(register_info_type
) *reg_info
)
7614 /* Skip over opcodes that can match nothing, and break when we get
7615 to one that can't. */
7617 switch ((re_opcode_t
) *p1
)
7620 case on_failure_jump
:
7622 EXTRACT_NUMBER_AND_INCR (mcnt
, p1
);
7627 if (!PREFIX(common_op_match_null_string_p
) (&p1
, end
, reg_info
))
7630 } /* while p1 < end */
7633 } /* alt_match_null_string_p */
7636 /* Deals with the ops common to group_match_null_string_p and
7637 alt_match_null_string_p.
7639 Sets P to one after the op and its arguments, if any. */
7642 PREFIX(common_op_match_null_string_p
) (UCHAR_T
**p
, UCHAR_T
*end
,
7643 PREFIX(register_info_type
) *reg_info
)
7650 switch ((re_opcode_t
) *p1
++)
7670 assert (reg_no
> 0 && reg_no
<= MAX_REGNUM
);
7671 ret
= PREFIX(group_match_null_string_p
) (&p1
, end
, reg_info
);
7673 /* Have to set this here in case we're checking a group which
7674 contains a group and a back reference to it. */
7676 if (REG_MATCH_NULL_STRING_P (reg_info
[reg_no
]) == MATCH_NULL_UNSET_VALUE
)
7677 REG_MATCH_NULL_STRING_P (reg_info
[reg_no
]) = ret
;
7683 /* If this is an optimized succeed_n for zero times, make the jump. */
7685 EXTRACT_NUMBER_AND_INCR (mcnt
, p1
);
7693 /* Get to the number of times to succeed. */
7694 p1
+= OFFSET_ADDRESS_SIZE
;
7695 EXTRACT_NUMBER_AND_INCR (mcnt
, p1
);
7699 p1
-= 2 * OFFSET_ADDRESS_SIZE
;
7700 EXTRACT_NUMBER_AND_INCR (mcnt
, p1
);
7708 if (!REG_MATCH_NULL_STRING_P (reg_info
[*p1
]))
7713 p1
+= 2 * OFFSET_ADDRESS_SIZE
;
7716 /* All other opcodes mean we cannot match the empty string. */
7722 } /* common_op_match_null_string_p */
7725 /* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
7726 bytes; nonzero otherwise. */
7729 PREFIX(bcmp_translate
) (const CHAR_T
*s1
, const CHAR_T
*s2
, register int len
,
7730 RE_TRANSLATE_TYPE translate
)
7732 register const UCHAR_T
*p1
= (const UCHAR_T
*) s1
;
7733 register const UCHAR_T
*p2
= (const UCHAR_T
*) s2
;
7737 if (((*p1
<=0xff)?translate
[*p1
++]:*p1
++)
7738 != ((*p2
<=0xff)?translate
[*p2
++]:*p2
++))
7741 if (translate
[*p1
++] != translate
[*p2
++]) return 1;
7749 #else /* not INSIDE_RECURSION */
7751 /* Entry points for GNU code. */
7753 /* re_compile_pattern is the GNU regular expression compiler: it
7754 compiles PATTERN (of length SIZE) and puts the result in BUFP.
7755 Returns 0 if the pattern was valid, otherwise an error string.
7757 Assumes the `allocated' (and perhaps `buffer') and `translate' fields
7758 are set in BUFP on entry.
7760 We call regex_compile to do the actual compilation. */
7763 re_compile_pattern (const char *pattern
, size_t length
,
7764 struct re_pattern_buffer
*bufp
)
7768 /* GNU code is written to assume at least RE_NREGS registers will be set
7769 (and at least one extra will be -1). */
7770 bufp
->regs_allocated
= REGS_UNALLOCATED
;
7772 /* And GNU code determines whether or not to get register information
7773 by passing null for the REGS argument to re_match, etc., not by
7777 /* Match anchors at newline. */
7778 bufp
->newline_anchor
= 1;
7781 if (MB_CUR_MAX
!= 1)
7782 ret
= wcs_regex_compile (pattern
, length
, re_syntax_options
, bufp
);
7785 ret
= byte_regex_compile (pattern
, length
, re_syntax_options
, bufp
);
7789 return gettext (re_error_msgid
[(int) ret
]);
7792 weak_alias (__re_compile_pattern
, re_compile_pattern
)
7795 /* Entry points compatible with 4.2 BSD regex library. We don't define
7796 them unless specifically requested. */
7798 #if defined _REGEX_RE_COMP || defined _LIBC
7800 /* BSD has one and only one pattern buffer. */
7801 static struct re_pattern_buffer re_comp_buf
;
7805 /* Make these definitions weak in libc, so POSIX programs can redefine
7806 these names if they don't use our functions, and still use
7807 regcomp/regexec below without link errors. */
7810 re_comp (const char *s
)
7816 if (!re_comp_buf
.buffer
)
7817 return gettext ("No previous regular expression");
7821 if (!re_comp_buf
.buffer
)
7823 re_comp_buf
.buffer
= (unsigned char *) malloc (200);
7824 if (re_comp_buf
.buffer
== NULL
)
7825 return (char *) gettext (re_error_msgid
[(int) REG_ESPACE
]);
7826 re_comp_buf
.allocated
= 200;
7828 re_comp_buf
.fastmap
= (char *) malloc (1 << BYTEWIDTH
);
7829 if (re_comp_buf
.fastmap
== NULL
)
7830 return (char *) gettext (re_error_msgid
[(int) REG_ESPACE
]);
7833 /* Since `re_exec' always passes NULL for the `regs' argument, we
7834 don't need to initialize the pattern buffer fields which affect it. */
7836 /* Match anchors at newlines. */
7837 re_comp_buf
.newline_anchor
= 1;
7840 if (MB_CUR_MAX
!= 1)
7841 ret
= wcs_regex_compile (s
, strlen (s
), re_syntax_options
, &re_comp_buf
);
7844 ret
= byte_regex_compile (s
, strlen (s
), re_syntax_options
, &re_comp_buf
);
7849 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
7850 return (char *) gettext (re_error_msgid
[(int) ret
]);
7858 re_exec (const char *s
)
7860 const int len
= strlen (s
);
7862 0 <= re_search (&re_comp_buf
, s
, len
, 0, len
, (struct re_registers
*) 0);
7865 #endif /* _REGEX_RE_COMP */
7867 /* POSIX.2 functions. Don't define these for Emacs. */
7871 /* regcomp takes a regular expression as a string and compiles it.
7873 PREG is a regex_t *. We do not expect any fields to be initialized,
7874 since POSIX says we shouldn't. Thus, we set
7876 `buffer' to the compiled pattern;
7877 `used' to the length of the compiled pattern;
7878 `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
7879 REG_EXTENDED bit in CFLAGS is set; otherwise, to
7880 RE_SYNTAX_POSIX_BASIC;
7881 `newline_anchor' to REG_NEWLINE being set in CFLAGS;
7882 `fastmap' to an allocated space for the fastmap;
7883 `fastmap_accurate' to zero;
7884 `re_nsub' to the number of subexpressions in PATTERN.
7886 PATTERN is the address of the pattern string.
7888 CFLAGS is a series of bits which affect compilation.
7890 If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
7891 use POSIX basic syntax.
7893 If REG_NEWLINE is set, then . and [^...] don't match newline.
7894 Also, regexec will try a match beginning after every newline.
7896 If REG_ICASE is set, then we considers upper- and lowercase
7897 versions of letters to be equivalent when matching.
7899 If REG_NOSUB is set, then when PREG is passed to regexec, that
7900 routine will report only success or failure, and nothing about the
7903 It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
7904 the return codes and their meanings.) */
7907 regcomp (regex_t
*preg
, const char *pattern
, int cflags
)
7911 = (cflags
& REG_EXTENDED
) ?
7912 RE_SYNTAX_POSIX_EXTENDED
: RE_SYNTAX_POSIX_BASIC
;
7914 /* regex_compile will allocate the space for the compiled pattern. */
7916 preg
->allocated
= 0;
7919 /* Try to allocate space for the fastmap. */
7920 preg
->fastmap
= (char *) malloc (1 << BYTEWIDTH
);
7922 if (cflags
& REG_ICASE
)
7927 = (RE_TRANSLATE_TYPE
) malloc (CHAR_SET_SIZE
7928 * sizeof (*(RE_TRANSLATE_TYPE
)0));
7929 if (preg
->translate
== NULL
)
7930 return (int) REG_ESPACE
;
7932 /* Map uppercase characters to corresponding lowercase ones. */
7933 for (i
= 0; i
< CHAR_SET_SIZE
; i
++)
7934 preg
->translate
[i
] = ISUPPER (i
) ? TOLOWER (i
) : (int) i
;
7937 preg
->translate
= NULL
;
7939 /* If REG_NEWLINE is set, newlines are treated differently. */
7940 if (cflags
& REG_NEWLINE
)
7941 { /* REG_NEWLINE implies neither . nor [^...] match newline. */
7942 syntax
&= ~RE_DOT_NEWLINE
;
7943 syntax
|= RE_HAT_LISTS_NOT_NEWLINE
;
7944 /* It also changes the matching behavior. */
7945 preg
->newline_anchor
= 1;
7948 preg
->newline_anchor
= 0;
7950 preg
->no_sub
= !!(cflags
& REG_NOSUB
);
7952 /* POSIX says a null character in the pattern terminates it, so we
7953 can use strlen here in compiling the pattern. */
7955 if (MB_CUR_MAX
!= 1)
7956 ret
= wcs_regex_compile (pattern
, strlen (pattern
), syntax
, preg
);
7959 ret
= byte_regex_compile (pattern
, strlen (pattern
), syntax
, preg
);
7961 /* POSIX doesn't distinguish between an unmatched open-group and an
7962 unmatched close-group: both are REG_EPAREN. */
7963 if (ret
== REG_ERPAREN
) ret
= REG_EPAREN
;
7965 if (ret
== REG_NOERROR
&& preg
->fastmap
)
7967 /* Compute the fastmap now, since regexec cannot modify the pattern
7969 if (re_compile_fastmap (preg
) == -2)
7971 /* Some error occurred while computing the fastmap, just forget
7973 free (preg
->fastmap
);
7974 preg
->fastmap
= NULL
;
7981 weak_alias (__regcomp
, regcomp
)
7985 /* regexec searches for a given pattern, specified by PREG, in the
7988 If NMATCH is zero or REG_NOSUB was set in the cflags argument to
7989 `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
7990 least NMATCH elements, and we set them to the offsets of the
7991 corresponding matched substrings.
7993 EFLAGS specifies `execution flags' which affect matching: if
7994 REG_NOTBOL is set, then ^ does not match at the beginning of the
7995 string; if REG_NOTEOL is set, then $ does not match at the end.
7997 We return 0 if we find a match and REG_NOMATCH if not. */
8000 regexec (const regex_t
*preg
, const char *string
, size_t nmatch
,
8001 regmatch_t pmatch
[], int eflags
)
8004 struct re_registers regs
;
8005 regex_t private_preg
;
8006 int len
= strlen (string
);
8007 boolean want_reg_info
= !preg
->no_sub
&& nmatch
> 0;
8009 private_preg
= *preg
;
8011 private_preg
.not_bol
= !!(eflags
& REG_NOTBOL
);
8012 private_preg
.not_eol
= !!(eflags
& REG_NOTEOL
);
8014 /* The user has told us exactly how many registers to return
8015 information about, via `nmatch'. We have to pass that on to the
8016 matching routines. */
8017 private_preg
.regs_allocated
= REGS_FIXED
;
8021 regs
.num_regs
= nmatch
;
8022 regs
.start
= TALLOC (nmatch
* 2, regoff_t
);
8023 if (regs
.start
== NULL
)
8024 return (int) REG_NOMATCH
;
8025 regs
.end
= regs
.start
+ nmatch
;
8028 /* Perform the searching operation. */
8029 ret
= re_search (&private_preg
, string
, len
,
8030 /* start: */ 0, /* range: */ len
,
8031 want_reg_info
? ®s
: (struct re_registers
*) 0);
8033 /* Copy the register information to the POSIX structure. */
8040 for (r
= 0; r
< nmatch
; r
++)
8042 pmatch
[r
].rm_so
= regs
.start
[r
];
8043 pmatch
[r
].rm_eo
= regs
.end
[r
];
8047 /* If we needed the temporary register info, free the space now. */
8051 /* We want zero return to mean success, unlike `re_search'. */
8052 return ret
>= 0 ? (int) REG_NOERROR
: (int) REG_NOMATCH
;
8055 weak_alias (__regexec
, regexec
)
8059 /* Returns a message corresponding to an error code, ERRCODE, returned
8060 from either regcomp or regexec. We don't use PREG here. */
8063 regerror (int errcode
, const regex_t
*preg ATTRIBUTE_UNUSED
,
8064 char *errbuf
, size_t errbuf_size
)
8070 || errcode
>= (int) (sizeof (re_error_msgid
)
8071 / sizeof (re_error_msgid
[0])))
8072 /* Only error codes returned by the rest of the code should be passed
8073 to this routine. If we are given anything else, or if other regex
8074 code generates an invalid error code, then the program has a bug.
8075 Dump core so we can fix it. */
8078 msg
= gettext (re_error_msgid
[errcode
]);
8080 msg_size
= strlen (msg
) + 1; /* Includes the null. */
8082 if (errbuf_size
!= 0)
8084 if (msg_size
> errbuf_size
)
8086 #if defined HAVE_MEMPCPY || defined _LIBC
8087 *((char *) mempcpy (errbuf
, msg
, errbuf_size
- 1)) = '\0';
8089 memcpy (errbuf
, msg
, errbuf_size
- 1);
8090 errbuf
[errbuf_size
- 1] = 0;
8094 memcpy (errbuf
, msg
, msg_size
);
8100 weak_alias (__regerror
, regerror
)
8104 /* Free dynamically allocated space used by PREG. */
8107 regfree (regex_t
*preg
)
8109 if (preg
->buffer
!= NULL
)
8110 free (preg
->buffer
);
8111 preg
->buffer
= NULL
;
8113 preg
->allocated
= 0;
8116 if (preg
->fastmap
!= NULL
)
8117 free (preg
->fastmap
);
8118 preg
->fastmap
= NULL
;
8119 preg
->fastmap_accurate
= 0;
8121 if (preg
->translate
!= NULL
)
8122 free (preg
->translate
);
8123 preg
->translate
= NULL
;
8126 weak_alias (__regfree
, regfree
)
8129 #endif /* not emacs */
8131 #endif /* not INSIDE_RECURSION */
8135 #undef STORE_NUMBER_AND_INCR
8136 #undef EXTRACT_NUMBER
8137 #undef EXTRACT_NUMBER_AND_INCR
8139 #undef DEBUG_PRINT_COMPILED_PATTERN
8140 #undef DEBUG_PRINT_DOUBLE_STRING
8142 #undef INIT_FAIL_STACK
8143 #undef RESET_FAIL_STACK
8144 #undef DOUBLE_FAIL_STACK
8145 #undef PUSH_PATTERN_OP
8146 #undef PUSH_FAILURE_POINTER
8147 #undef PUSH_FAILURE_INT
8148 #undef PUSH_FAILURE_ELT
8149 #undef POP_FAILURE_POINTER
8150 #undef POP_FAILURE_INT
8151 #undef POP_FAILURE_ELT
8154 #undef PUSH_FAILURE_POINT
8155 #undef POP_FAILURE_POINT
8157 #undef REG_UNSET_VALUE
8165 #undef INIT_BUF_SIZE
8166 #undef GET_BUFFER_SPACE
8174 #undef EXTEND_BUFFER
8175 #undef GET_UNSIGNED_NUMBER
8176 #undef FREE_STACK_RETURN
8178 # undef POINTER_TO_OFFSET
8179 # undef MATCHING_IN_FRST_STRING
8181 # undef AT_STRINGS_BEG
8182 # undef AT_STRINGS_END
8185 # undef FREE_VARIABLES
8186 # undef NO_HIGHEST_ACTIVE_REG
8187 # undef NO_LOWEST_ACTIVE_REG
8191 # undef COMPILED_BUFFER_VAR
8192 # undef OFFSET_ADDRESS_SIZE
8193 # undef CHAR_CLASS_SIZE
8200 # define DEFINED_ONCE