Mercurial > hg > xemacs-beta
annotate src/regex.c @ 5648:3f4a234f4672
Support non-ASCII correctly in character classes, test this.
src/ChangeLog addition:
2012-04-21 Aidan Kehoe <kehoea@parhasard.net>
Support non-ASCII correctly in character classes ([:alnum:] and
friends).
* regex.c:
* regex.c (ISBLANK, ISUNIBYTE): New. Make these and friends
independent of the locale, since we want them to be consistent in
XEmacs.
* regex.c (print_partial_compiled_pattern): Print the flags for
charset_mule; don't print non-ASCII as the character values in
ranges, this breaks with locales.
* regex.c (enum):
Define various flags the charset_mule and charset_mule_not opcodes
can now take.
* regex.c (CHAR_CLASS_MAX_LENGTH): Update this.
* regex.c (re_iswctype, re_wctype): New, from GNU.
* regex.c (re_wctype_can_match_non_ascii): New; used when deciding
on whether to use charset_mule or the ASCII-only regex character
set opcode.
* regex.c (regex_compile):
Error correctly on long, non-existent character class names.
Break out the handling of charsets that can match non-ASCII into a
separate clause. Use compile_char_class when compiling character
classes.
* regex.c (compile_char_class): New. Used in regex_compile when
compiling character sets that may match non-ASCII.
* regex.c (re_compile_fastmap):
If there are flags set for charset_mule or charset_mule_not, we
can't use the fastmap (since we need to check syntax table values
that aren't available there).
* regex.c (re_match_2_internal):
Check the new flags passed to the charset_mule{,_not} opcode,
observe them if appropriate.
* regex.h:
* regex.h (enum):
Expose re_wctype_t here, imported from GNU.
tests/ChangeLog addition:
2012-04-21 Aidan Kehoe <kehoea@parhasard.net>
* automated/regexp-tests.el:
* automated/regexp-tests.el (Assert-char-class):
Check that #'string-match errors correctly with an over-long
character class name.
Add tests for character class functionality that supports
non-ASCII characters. These tests expose bugs in GNU Emacs
24.0.94.2, but pass under current XEmacs.
author | Aidan Kehoe <kehoea@parhasard.net> |
---|---|
date | Sat, 21 Apr 2012 18:58:28 +0100 |
parents | 308d34e9f07d |
children | 3df910176b6a |
rev | line source |
---|---|
428 | 1 /* Extended regular expression matching and search library, |
2 version 0.12, extended for XEmacs. | |
3 (Implements POSIX draft P10003.2/D11.2, except for | |
4 internationalization features.) | |
5 | |
6 Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc. | |
7 Copyright (C) 1995 Sun Microsystems, Inc. | |
5041 | 8 Copyright (C) 1995, 2001, 2002, 2003, 2010 Ben Wing. |
428 | 9 |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5041
diff
changeset
|
10 This file is part of XEmacs. |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5041
diff
changeset
|
11 |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5041
diff
changeset
|
12 XEmacs is free software: you can redistribute it and/or modify it |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5041
diff
changeset
|
13 under the terms of the GNU General Public License as published by the |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5041
diff
changeset
|
14 Free Software Foundation, either version 3 of the License, or (at your |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5041
diff
changeset
|
15 option) any later version. |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5041
diff
changeset
|
16 |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5041
diff
changeset
|
17 XEmacs is distributed in the hope that it will be useful, but WITHOUT |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5041
diff
changeset
|
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5041
diff
changeset
|
19 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5041
diff
changeset
|
20 for more details. |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5041
diff
changeset
|
21 |
428 | 22 You should have received a copy of the GNU General Public License |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5041
diff
changeset
|
23 along with XEmacs. If not, see <http://www.gnu.org/licenses/>. */ |
428 | 24 /* Synched up with: FSF 19.29. */ |
25 | |
26 #ifdef HAVE_CONFIG_H | |
27 #include <config.h> | |
28 #endif | |
29 | |
30 #ifndef _GNU_SOURCE | |
31 #define _GNU_SOURCE 1 | |
32 #endif | |
33 | |
34 /* We assume non-Mule if emacs isn't defined. */ | |
35 #ifndef emacs | |
36 #undef MULE | |
37 #endif | |
38 | |
771 | 39 /* XEmacs addition */ |
40 #ifdef REL_ALLOC | |
41 #define REGEX_REL_ALLOC /* may be undefined below */ | |
42 #endif | |
43 | |
428 | 44 /* XEmacs: define this to add in a speedup for patterns anchored at |
45 the beginning of a line. Keep the ifdefs so that it's easier to | |
46 tell where/why this code has diverged from v19. */ | |
47 #define REGEX_BEGLINE_CHECK | |
48 | |
49 /* XEmacs: the current mmap-based ralloc handles small blocks very | |
50 poorly, so we disable it here. */ | |
51 | |
771 | 52 #if defined (HAVE_MMAP) || defined (DOUG_LEA_MALLOC) |
53 # undef REGEX_REL_ALLOC | |
428 | 54 #endif |
55 | |
56 /* The `emacs' switch turns on certain matching commands | |
57 that make sense only in Emacs. */ | |
58 #ifdef emacs | |
59 | |
60 #include "lisp.h" | |
61 #include "buffer.h" | |
62 #include "syntax.h" | |
63 | |
64 #if (defined (DEBUG_XEMACS) && !defined (DEBUG)) | |
65 #define DEBUG | |
66 #endif | |
67 | |
867 | 68 #define RE_TRANSLATE_1(ch) TRT_TABLE_OF (translate, (Ichar) ch) |
446 | 69 #define TRANSLATE_P(tr) (!NILP (tr)) |
428 | 70 |
826 | 71 /* Converts the pointer to the char to BEG-based offset from the start. */ |
72 #define PTR_TO_OFFSET(d) (MATCHING_IN_FIRST_STRING \ | |
73 ? (d) - string1 : (d) - (string2 - size1)) | |
74 | |
428 | 75 #else /* not emacs */ |
76 | |
2367 | 77 #include <stdlib.h> |
78 #include <sys/types.h> | |
79 #include <stddef.h> /* needed for ptrdiff_t under Solaris */ | |
80 #include <string.h> | |
81 | |
2286 | 82 #include "compiler.h" /* Get compiler-specific definitions like UNUSED */ |
83 | |
2500 | 84 #define ABORT abort |
85 | |
428 | 86 /* If we are not linking with Emacs proper, |
87 we can't use the relocating allocator | |
88 even if config.h says that we can. */ | |
771 | 89 #undef REGEX_REL_ALLOC |
428 | 90 |
544 | 91 /* defined in lisp.h */ |
92 #ifdef REGEX_MALLOC | |
93 #ifndef DECLARE_NOTHING | |
94 #define DECLARE_NOTHING struct nosuchstruct | |
95 #endif | |
96 #endif | |
97 | |
867 | 98 #define itext_ichar(str) ((Ichar) (str)[0]) |
99 #define itext_ichar_fmt(str, fmt, object) ((Ichar) (str)[0]) | |
100 #define itext_ichar_ascii_fmt(str, fmt, object) ((Ichar) (str)[0]) | |
428 | 101 |
102 #if (LONGBITS > INTBITS) | |
103 # define EMACS_INT long | |
104 #else | |
105 # define EMACS_INT int | |
106 #endif | |
107 | |
867 | 108 typedef int Ichar; |
109 | |
110 #define INC_IBYTEPTR(p) ((p)++) | |
111 #define INC_IBYTEPTR_FMT(p, fmt) ((p)++) | |
112 #define DEC_IBYTEPTR(p) ((p)--) | |
113 #define DEC_IBYTEPTR_FMT(p, fmt) ((p)--) | |
4750
b5f21bb36684
Fix crash in regex.c (closes issue630).
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4527
diff
changeset
|
114 #define MAX_ICHAR_LEN 1 |
867 | 115 #define itext_ichar_len(ptr) 1 |
116 #define itext_ichar_len_fmt(ptr, fmt) 1 | |
428 | 117 |
118 /* Define the syntax stuff for \<, \>, etc. */ | |
119 | |
120 /* This must be nonzero for the wordchar and notwordchar pattern | |
121 commands in re_match_2. */ | |
122 #ifndef Sword | |
123 #define Sword 1 | |
124 #endif | |
125 | |
126 #ifdef SYNTAX_TABLE | |
127 | |
128 extern char *re_syntax_table; | |
129 | |
130 #else /* not SYNTAX_TABLE */ | |
131 | |
132 /* How many characters in the character set. */ | |
133 #define CHAR_SET_SIZE 256 | |
134 | |
135 static char re_syntax_table[CHAR_SET_SIZE]; | |
136 | |
137 static void | |
138 init_syntax_once (void) | |
139 { | |
140 static int done = 0; | |
141 | |
142 if (!done) | |
143 { | |
442 | 144 const char *word_syntax_chars = |
428 | 145 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_"; |
146 | |
147 memset (re_syntax_table, 0, sizeof (re_syntax_table)); | |
148 | |
149 while (*word_syntax_chars) | |
647 | 150 re_syntax_table[(unsigned int) (*word_syntax_chars++)] = Sword; |
428 | 151 |
152 done = 1; | |
153 } | |
154 } | |
155 | |
446 | 156 #endif /* SYNTAX_TABLE */ |
428 | 157 |
826 | 158 #define SYNTAX(ignored, c) re_syntax_table[c] |
460 | 159 #undef SYNTAX_FROM_CACHE |
826 | 160 #define SYNTAX_FROM_CACHE SYNTAX |
161 | |
162 #define RE_TRANSLATE_1(c) translate[(unsigned char) (c)] | |
446 | 163 #define TRANSLATE_P(tr) tr |
164 | |
165 #endif /* emacs */ | |
428 | 166 |
2201 | 167 /* This is for other GNU distributions with internationalized messages. */ |
168 #if defined (I18N3) && (defined (HAVE_LIBINTL_H) || defined (_LIBC)) | |
169 # include <libintl.h> | |
170 #else | |
171 # define gettext(msgid) (msgid) | |
172 #endif | |
173 | |
428 | 174 |
175 /* Get the interface, including the syntax bits. */ | |
176 #include "regex.h" | |
177 | |
178 /* isalpha etc. are used for the character classes. */ | |
179 #include <ctype.h> | |
180 | |
5648
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
181 #ifdef emacs |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
182 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
183 /* 1 if C is an ASCII character. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
184 #define ISASCII(c) ((c) < 0x80) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
185 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
186 /* 1 if C is a unibyte character. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
187 #define ISUNIBYTE(c) 0 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
188 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
189 /* The Emacs definitions should not be directly affected by locales. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
190 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
191 /* In Emacs, these are only used for single-byte characters. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
192 #define ISDIGIT(c) ((c) >= '0' && (c) <= '9') |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
193 #define ISCNTRL(c) ((c) < ' ') |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
194 #define ISXDIGIT(c) (ISDIGIT (c) || ((c) >= 'a' && (c) <= 'f') \ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
195 || ((c) >= 'A' && (c) <= 'F')) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
196 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
197 /* This is only used for single-byte characters. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
198 #define ISBLANK(c) ((c) == ' ' || (c) == '\t') |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
199 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
200 /* The rest must handle multibyte characters. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
201 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
202 #define ISGRAPH(c) ((c) > ' ' && (c) != 0x7f) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
203 #define ISPRINT(c) ((c) == ' ' || ISGRAPH (c)) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
204 #define ISALPHA(c) (ISASCII (c) ? (((c) >= 'a' && (c) <= 'z') \ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
205 || ((c) >= 'A' && (c) <= 'Z')) \ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
206 : ISWORD (c)) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
207 #define ISALNUM(c) (ISALPHA (c) || ISDIGIT (c)) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
208 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
209 #define ISLOWER(c) LOWERCASEP (lispbuf, c) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
210 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
211 #define ISPUNCT(c) (ISASCII (c) \ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
212 ? ((c) > ' ' && (c) < 0x7F \ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
213 && !(((c) >= 'a' && (c) <= 'z') \ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
214 || ((c) >= 'A' && (c) <= 'Z') \ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
215 || ((c) >= '0' && (c) <= '9'))) \ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
216 : !ISWORD (c)) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
217 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
218 #define ISSPACE(c) \ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
219 (SYNTAX (BUFFER_MIRROR_SYNTAX_TABLE (lispbuf), c) == Swhitespace) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
220 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
221 #define ISUPPER(c) UPPERCASEP (lispbuf, c) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
222 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
223 #define ISWORD(c) (SYNTAX (BUFFER_MIRROR_SYNTAX_TABLE (lispbuf), c) == Sword) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
224 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
225 #else /* not emacs */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
226 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
227 /* 1 if C is an ASCII character. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
228 #define ISASCII(c) ((c) < 0200) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
229 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
230 /* 1 if C is a unibyte character. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
231 #define ISUNIBYTE(c) 0 |
428 | 232 |
233 #ifdef isblank | |
5648
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
234 # define ISBLANK(c) isblank (c) |
428 | 235 #else |
5648
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
236 # define ISBLANK(c) ((c) == ' ' || (c) == '\t') |
428 | 237 #endif |
238 #ifdef isgraph | |
5648
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
239 # define ISGRAPH(c) isgraph (c) |
428 | 240 #else |
5648
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
241 # define ISGRAPH(c) (isprint (c) && !isspace (c)) |
428 | 242 #endif |
243 | |
5648
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
244 /* Solaris defines ISPRINT so we must undefine it first. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
245 #undef ISPRINT |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
246 #define ISPRINT(c) isprint (c) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
247 #define ISDIGIT(c) isdigit (c) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
248 #define ISALNUM(c) isalnum (c) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
249 #define ISALPHA(c) isalpha (c) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
250 #define ISCNTRL(c) iscntrl (c) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
251 #define ISLOWER(c) islower (c) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
252 #define ISPUNCT(c) ispunct (c) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
253 #define ISSPACE(c) isspace (c) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
254 #define ISUPPER(c) isupper (c) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
255 #define ISXDIGIT(c) isxdigit (c) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
256 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
257 #define ISWORD(c) ISALPHA (c) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
258 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
259 #ifdef _tolower |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
260 # define TOLOWER(c) _tolower (c) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
261 #else |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
262 # define TOLOWER(c) tolower (c) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
263 #endif |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
264 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
265 #endif /* emacs */ |
428 | 266 |
267 #ifndef NULL | |
268 #define NULL (void *)0 | |
269 #endif | |
270 | |
271 /* We remove any previous definition of `SIGN_EXTEND_CHAR', | |
272 since ours (we hope) works properly with all combinations of | |
273 machines, compilers, `char' and `unsigned char' argument types. | |
274 (Per Bothner suggested the basic approach.) */ | |
275 #undef SIGN_EXTEND_CHAR | |
276 #if __STDC__ | |
277 #define SIGN_EXTEND_CHAR(c) ((signed char) (c)) | |
278 #else /* not __STDC__ */ | |
279 /* As in Harbison and Steele. */ | |
280 #define SIGN_EXTEND_CHAR(c) ((((unsigned char) (c)) ^ 128) - 128) | |
281 #endif | |
282 | |
283 /* Should we use malloc or alloca? If REGEX_MALLOC is not defined, we | |
284 use `alloca' instead of `malloc'. This is because using malloc in | |
285 re_search* or re_match* could cause memory leaks when C-g is used in | |
286 Emacs; also, malloc is slower and causes storage fragmentation. On | |
287 the other hand, malloc is more portable, and easier to debug. | |
288 | |
289 Because we sometimes use alloca, some routines have to be macros, | |
290 not functions -- `alloca'-allocated space disappears at the end of the | |
291 function it is called in. */ | |
292 | |
1333 | 293 #ifndef emacs |
294 #define ALLOCA alloca | |
295 #define xmalloc malloc | |
296 #define xrealloc realloc | |
4976
16112448d484
Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents:
4832
diff
changeset
|
297 #define xfree free |
1333 | 298 #endif |
299 | |
300 #ifdef emacs | |
301 #define ALLOCA_GARBAGE_COLLECT() \ | |
302 do \ | |
303 { \ | |
304 if (need_to_check_c_alloca) \ | |
305 xemacs_c_alloca (0); \ | |
306 } while (0) | |
307 #elif defined (C_ALLOCA) | |
308 #define ALLOCA_GARBAGE_COLLECT() alloca (0) | |
309 #else | |
310 #define ALLOCA_GARBAGE_COLLECT() | |
311 #endif | |
312 | |
313 #ifndef emacs | |
314 /* So we can use just it to conditionalize on */ | |
315 #undef ERROR_CHECK_MALLOC | |
316 #endif | |
317 | |
318 #ifdef ERROR_CHECK_MALLOC | |
319 /* When REL_ALLOC, malloc() is problematic because it could potentially | |
320 cause all rel-alloc()ed data -- including buffer text -- to be relocated. | |
321 We deal with this by checking for such relocation whenever we have | |
322 executed a statement that may call malloc() -- or alloca(), which may | |
323 end up calling malloc() in some circumstances -- and recomputing all | |
324 of our string pointers in re_match_2_internal() and re_search_2(). | |
325 However, if malloc() or alloca() happens and we don't know about it, | |
326 we could still be screwed. So we set up a system where we indicate all | |
327 places where we are prepared for malloc() or alloca(), and in any | |
328 other circumstances, calls to those functions (from anywhere inside of | |
2500 | 329 XEmacs!) will ABORT(). We do this even when REL_ALLOC is not defined |
1333 | 330 so that we catch these problems sooner, since many developers and beta |
331 testers will not be running with REL_ALLOC. */ | |
332 int regex_malloc_disallowed; | |
333 #define BEGIN_REGEX_MALLOC_OK() regex_malloc_disallowed = 0 | |
334 #define END_REGEX_MALLOC_OK() regex_malloc_disallowed = 1 | |
335 #define UNBIND_REGEX_MALLOC_CHECK() unbind_to (depth) | |
336 #else | |
337 #define BEGIN_REGEX_MALLOC_OK() | |
338 #define END_REGEX_MALLOC_OK() | |
339 #define UNBIND_REGEX_MALLOC_CHECK() | |
340 #endif | |
341 | |
342 | |
428 | 343 #ifdef REGEX_MALLOC |
344 | |
1333 | 345 #define REGEX_ALLOCATE xmalloc |
346 #define REGEX_REALLOCATE(source, osize, nsize) xrealloc (source, nsize) | |
347 #define REGEX_FREE xfree | |
428 | 348 |
349 #else /* not REGEX_MALLOC */ | |
350 | |
351 /* Emacs already defines alloca, sometimes. */ | |
352 #ifndef alloca | |
353 | |
354 /* Make alloca work the best possible way. */ | |
355 #ifdef __GNUC__ | |
356 #define alloca __builtin_alloca | |
771 | 357 #elif defined (__DECC) /* XEmacs: added next 3 lines, similar to config.h.in */ |
358 #include <alloca.h> | |
359 #pragma intrinsic(alloca) | |
428 | 360 #else /* not __GNUC__ */ |
361 #if HAVE_ALLOCA_H | |
362 #include <alloca.h> | |
363 #else /* not __GNUC__ or HAVE_ALLOCA_H */ | |
364 #ifndef _AIX /* Already did AIX, up at the top. */ | |
444 | 365 void *alloca (); |
428 | 366 #endif /* not _AIX */ |
446 | 367 #endif /* HAVE_ALLOCA_H */ |
368 #endif /* __GNUC__ */ | |
428 | 369 |
370 #endif /* not alloca */ | |
371 | |
1333 | 372 #define REGEX_ALLOCATE ALLOCA |
428 | 373 |
2367 | 374 /* !!#### Needs review */ |
428 | 375 /* Assumes a `char *destination' variable. */ |
376 #define REGEX_REALLOCATE(source, osize, nsize) \ | |
1333 | 377 (destination = (char *) ALLOCA (nsize), \ |
428 | 378 memmove (destination, source, osize), \ |
379 destination) | |
380 | |
1726 | 381 /* No need to do anything to free, after alloca. |
382 Do nothing! But inhibit gcc warning. */ | |
383 #define REGEX_FREE(arg,type) ((void)0) | |
428 | 384 |
446 | 385 #endif /* REGEX_MALLOC */ |
428 | 386 |
387 /* Define how to allocate the failure stack. */ | |
388 | |
771 | 389 #ifdef REGEX_REL_ALLOC |
428 | 390 #define REGEX_ALLOCATE_STACK(size) \ |
1346 | 391 r_alloc ((unsigned char **) &failure_stack_ptr, (size)) |
428 | 392 #define REGEX_REALLOCATE_STACK(source, osize, nsize) \ |
1346 | 393 r_re_alloc ((unsigned char **) &failure_stack_ptr, (nsize)) |
428 | 394 #define REGEX_FREE_STACK(ptr) \ |
1346 | 395 r_alloc_free ((unsigned char **) &failure_stack_ptr) |
428 | 396 |
771 | 397 #else /* not REGEX_REL_ALLOC */ |
428 | 398 |
399 #ifdef REGEX_MALLOC | |
400 | |
1333 | 401 #define REGEX_ALLOCATE_STACK xmalloc |
402 #define REGEX_REALLOCATE_STACK(source, osize, nsize) xrealloc (source, nsize) | |
4976
16112448d484
Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents:
4832
diff
changeset
|
403 #define REGEX_FREE_STACK(arg) xfree (arg) |
428 | 404 |
405 #else /* not REGEX_MALLOC */ | |
406 | |
1333 | 407 #define REGEX_ALLOCATE_STACK ALLOCA |
428 | 408 |
409 #define REGEX_REALLOCATE_STACK(source, osize, nsize) \ | |
410 REGEX_REALLOCATE (source, osize, nsize) | |
411 /* No need to explicitly free anything. */ | |
412 #define REGEX_FREE_STACK(arg) | |
413 | |
446 | 414 #endif /* REGEX_MALLOC */ |
771 | 415 #endif /* REGEX_REL_ALLOC */ |
428 | 416 |
417 | |
418 /* True if `size1' is non-NULL and PTR is pointing anywhere inside | |
419 `string1' or just past its end. This works if PTR is NULL, which is | |
420 a good thing. */ | |
421 #define FIRST_STRING_P(ptr) \ | |
422 (size1 && string1 <= (ptr) && (ptr) <= string1 + size1) | |
423 | |
424 /* (Re)Allocate N items of type T using malloc, or fail. */ | |
1333 | 425 #define TALLOC(n, t) ((t *) xmalloc ((n) * sizeof (t))) |
426 #define RETALLOC(addr, n, t) ((addr) = (t *) xrealloc (addr, (n) * sizeof (t))) | |
428 | 427 #define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t))) |
428 | |
429 #define BYTEWIDTH 8 /* In bits. */ | |
430 | |
434 | 431 #define STREQ(s1, s2) (strcmp (s1, s2) == 0) |
428 | 432 |
433 #undef MAX | |
434 #undef MIN | |
435 #define MAX(a, b) ((a) > (b) ? (a) : (b)) | |
436 #define MIN(a, b) ((a) < (b) ? (a) : (b)) | |
437 | |
446 | 438 /* Type of source-pattern and string chars. */ |
439 typedef const unsigned char re_char; | |
440 | |
460 | 441 typedef char re_bool; |
428 | 442 #define false 0 |
443 #define true 1 | |
444 | |
445 | |
1346 | 446 #ifdef emacs |
447 | |
448 #ifdef MULE | |
449 | |
450 Lisp_Object Vthe_lisp_rangetab; | |
451 | |
452 void | |
453 vars_of_regex (void) | |
454 { | |
2421 | 455 Vthe_lisp_rangetab = Fmake_range_table (Qstart_closed_end_closed); |
1346 | 456 staticpro (&Vthe_lisp_rangetab); |
457 } | |
458 | |
459 #else /* not MULE */ | |
460 | |
461 void | |
462 vars_of_regex (void) | |
463 { | |
464 } | |
465 | |
466 #endif /* MULE */ | |
467 | |
468 /* Convert an offset from the start of the logical text string formed by | |
469 concatenating the two strings together into a character position in the | |
470 Lisp buffer or string that the text represents. Knows that | |
471 when handling buffer text, the "string" we're passed in is always | |
472 BEGV - ZV. */ | |
473 | |
474 static Charxpos | |
475 offset_to_charxpos (Lisp_Object lispobj, int off) | |
476 { | |
477 if (STRINGP (lispobj)) | |
478 return string_index_byte_to_char (lispobj, off); | |
479 else if (BUFFERP (lispobj)) | |
480 return bytebpos_to_charbpos (XBUFFER (lispobj), | |
481 off + BYTE_BUF_BEGV (XBUFFER (lispobj))); | |
482 else | |
483 return 0; | |
484 } | |
485 | |
486 #ifdef REL_ALLOC | |
487 | |
488 /* STRING1 is the value of STRING1 given to re_match_2(). LISPOBJ is | |
489 the Lisp object (if any) from which the string is taken. If LISPOBJ | |
490 is a buffer, return a relocation offset to be added to all pointers to | |
491 string data so that they will be accurate again, after an allocation or | |
492 reallocation that potentially relocated the buffer data. | |
493 */ | |
494 static Bytecount | |
495 offset_post_relocation (Lisp_Object lispobj, Ibyte *orig_buftext) | |
496 { | |
497 if (!BUFFERP (lispobj)) | |
498 return 0; | |
499 return (BYTE_BUF_BYTE_ADDRESS (XBUFFER (lispobj), | |
500 BYTE_BUF_BEGV (XBUFFER (lispobj))) - | |
501 orig_buftext); | |
502 } | |
503 | |
504 #endif /* REL_ALLOC */ | |
505 | |
506 #ifdef ERROR_CHECK_MALLOC | |
507 | |
508 /* NOTE that this can run malloc() so you need to adjust afterwards. */ | |
509 | |
510 static int | |
511 bind_regex_malloc_disallowed (int value) | |
512 { | |
513 /* Tricky, because the act of binding can run malloc(). */ | |
514 int old_regex_malloc_disallowed = regex_malloc_disallowed; | |
515 int depth; | |
516 regex_malloc_disallowed = 0; | |
517 depth = record_unwind_protect_restoring_int (®ex_malloc_disallowed, | |
518 old_regex_malloc_disallowed); | |
519 regex_malloc_disallowed = value; | |
520 return depth; | |
521 } | |
522 | |
523 #endif /* ERROR_CHECK_MALLOC */ | |
524 | |
525 #endif /* emacs */ | |
526 | |
527 | |
428 | 528 /* These are the command codes that appear in compiled regular |
529 expressions. Some opcodes are followed by argument bytes. A | |
530 command code can specify any interpretation whatsoever for its | |
531 arguments. Zero bytes may appear in the compiled regular expression. */ | |
532 | |
533 typedef enum | |
534 { | |
535 no_op = 0, | |
536 | |
537 /* Succeed right away--no more backtracking. */ | |
538 succeed, | |
539 | |
540 /* Followed by one byte giving n, then by n literal bytes. */ | |
541 exactn, | |
542 | |
543 /* Matches any (more or less) character. */ | |
544 anychar, | |
545 | |
546 /* Matches any one char belonging to specified set. First | |
547 following byte is number of bitmap bytes. Then come bytes | |
548 for a bitmap saying which chars are in. Bits in each byte | |
549 are ordered low-bit-first. A character is in the set if its | |
550 bit is 1. A character too large to have a bit in the map is | |
551 automatically not in the set. */ | |
552 charset, | |
553 | |
554 /* Same parameters as charset, but match any character that is | |
555 not one of those specified. */ | |
556 charset_not, | |
557 | |
558 /* Start remembering the text that is matched, for storing in a | |
559 register. Followed by one byte with the register number, in | |
502 | 560 the range 1 to the pattern buffer's re_ngroups |
428 | 561 field. Then followed by one byte with the number of groups |
562 inner to this one. (This last has to be part of the | |
563 start_memory only because we need it in the on_failure_jump | |
564 of re_match_2.) */ | |
565 start_memory, | |
566 | |
567 /* Stop remembering the text that is matched and store it in a | |
568 memory register. Followed by one byte with the register | |
502 | 569 number, in the range 1 to `re_ngroups' in the |
428 | 570 pattern buffer, and one byte with the number of inner groups, |
571 just like `start_memory'. (We need the number of inner | |
572 groups here because we don't have any easy way of finding the | |
573 corresponding start_memory when we're at a stop_memory.) */ | |
574 stop_memory, | |
575 | |
576 /* Match a duplicate of something remembered. Followed by one | |
577 byte containing the register number. */ | |
578 duplicate, | |
579 | |
580 /* Fail unless at beginning of line. */ | |
581 begline, | |
582 | |
583 /* Fail unless at end of line. */ | |
584 endline, | |
585 | |
586 /* Succeeds if at beginning of buffer (if emacs) or at beginning | |
587 of string to be matched (if not). */ | |
588 begbuf, | |
589 | |
590 /* Analogously, for end of buffer/string. */ | |
591 endbuf, | |
592 | |
593 /* Followed by two byte relative address to which to jump. */ | |
594 jump, | |
595 | |
596 /* Same as jump, but marks the end of an alternative. */ | |
597 jump_past_alt, | |
598 | |
599 /* Followed by two-byte relative address of place to resume at | |
600 in case of failure. */ | |
601 on_failure_jump, | |
602 | |
603 /* Like on_failure_jump, but pushes a placeholder instead of the | |
604 current string position when executed. */ | |
605 on_failure_keep_string_jump, | |
606 | |
607 /* Throw away latest failure point and then jump to following | |
608 two-byte relative address. */ | |
609 pop_failure_jump, | |
610 | |
611 /* Change to pop_failure_jump if know won't have to backtrack to | |
612 match; otherwise change to jump. This is used to jump | |
613 back to the beginning of a repeat. If what follows this jump | |
614 clearly won't match what the repeat does, such that we can be | |
615 sure that there is no use backtracking out of repetitions | |
616 already matched, then we change it to a pop_failure_jump. | |
617 Followed by two-byte address. */ | |
618 maybe_pop_jump, | |
619 | |
620 /* Jump to following two-byte address, and push a dummy failure | |
621 point. This failure point will be thrown away if an attempt | |
622 is made to use it for a failure. A `+' construct makes this | |
623 before the first repeat. Also used as an intermediary kind | |
624 of jump when compiling an alternative. */ | |
625 dummy_failure_jump, | |
626 | |
627 /* Push a dummy failure point and continue. Used at the end of | |
628 alternatives. */ | |
629 push_dummy_failure, | |
630 | |
631 /* Followed by two-byte relative address and two-byte number n. | |
632 After matching N times, jump to the address upon failure. */ | |
633 succeed_n, | |
634 | |
635 /* Followed by two-byte relative address, and two-byte number n. | |
636 Jump to the address N times, then fail. */ | |
637 jump_n, | |
638 | |
639 /* Set the following two-byte relative address to the | |
640 subsequent two-byte number. The address *includes* the two | |
641 bytes of number. */ | |
642 set_number_at, | |
643 | |
644 wordchar, /* Matches any word-constituent character. */ | |
645 notwordchar, /* Matches any char that is not a word-constituent. */ | |
646 | |
647 wordbeg, /* Succeeds if at word beginning. */ | |
648 wordend, /* Succeeds if at word end. */ | |
649 | |
650 wordbound, /* Succeeds if at a word boundary. */ | |
651 notwordbound /* Succeeds if not at a word boundary. */ | |
652 | |
653 #ifdef emacs | |
654 ,before_dot, /* Succeeds if before point. */ | |
655 at_dot, /* Succeeds if at point. */ | |
656 after_dot, /* Succeeds if after point. */ | |
657 | |
658 /* Matches any character whose syntax is specified. Followed by | |
659 a byte which contains a syntax code, e.g., Sword. */ | |
660 syntaxspec, | |
661 | |
662 /* Matches any character whose syntax is not that specified. */ | |
663 notsyntaxspec | |
664 | |
665 #endif /* emacs */ | |
666 | |
667 #ifdef MULE | |
668 /* need extra stuff to be able to properly work with XEmacs/Mule | |
669 characters (which may take up more than one byte) */ | |
670 | |
671 ,charset_mule, /* Matches any character belonging to specified set. | |
672 The set is stored in "unified range-table | |
673 format"; see rangetab.c. Unlike the `charset' | |
674 opcode, this can handle arbitrary characters. */ | |
675 | |
676 charset_mule_not /* Same parameters as charset_mule, but match any | |
677 character that is not one of those specified. */ | |
678 | |
679 /* 97/2/17 jhod: The following two were merged back in from the Mule | |
680 2.3 code to enable some language specific processing */ | |
681 ,categoryspec, /* Matches entries in the character category tables */ | |
682 notcategoryspec /* The opposite of the above */ | |
683 #endif /* MULE */ | |
684 | |
685 } re_opcode_t; | |
686 | |
687 /* Common operations on the compiled pattern. */ | |
688 | |
689 /* Store NUMBER in two contiguous bytes starting at DESTINATION. */ | |
690 | |
691 #define STORE_NUMBER(destination, number) \ | |
692 do { \ | |
693 (destination)[0] = (number) & 0377; \ | |
694 (destination)[1] = (number) >> 8; \ | |
695 } while (0) | |
696 | |
697 /* Same as STORE_NUMBER, except increment DESTINATION to | |
698 the byte after where the number is stored. Therefore, DESTINATION | |
699 must be an lvalue. */ | |
700 | |
701 #define STORE_NUMBER_AND_INCR(destination, number) \ | |
702 do { \ | |
703 STORE_NUMBER (destination, number); \ | |
704 (destination) += 2; \ | |
705 } while (0) | |
706 | |
707 /* Put into DESTINATION a number stored in two contiguous bytes starting | |
708 at SOURCE. */ | |
709 | |
710 #define EXTRACT_NUMBER(destination, source) \ | |
711 do { \ | |
712 (destination) = *(source) & 0377; \ | |
713 (destination) += SIGN_EXTEND_CHAR (*((source) + 1)) << 8; \ | |
714 } while (0) | |
715 | |
716 #ifdef DEBUG | |
717 static void | |
446 | 718 extract_number (int *dest, re_char *source) |
428 | 719 { |
720 int temp = SIGN_EXTEND_CHAR (*(source + 1)); | |
721 *dest = *source & 0377; | |
722 *dest += temp << 8; | |
723 } | |
724 | |
725 #ifndef EXTRACT_MACROS /* To debug the macros. */ | |
726 #undef EXTRACT_NUMBER | |
727 #define EXTRACT_NUMBER(dest, src) extract_number (&dest, src) | |
728 #endif /* not EXTRACT_MACROS */ | |
729 | |
730 #endif /* DEBUG */ | |
731 | |
732 /* Same as EXTRACT_NUMBER, except increment SOURCE to after the number. | |
733 SOURCE must be an lvalue. */ | |
734 | |
735 #define EXTRACT_NUMBER_AND_INCR(destination, source) \ | |
736 do { \ | |
737 EXTRACT_NUMBER (destination, source); \ | |
738 (source) += 2; \ | |
739 } while (0) | |
740 | |
741 #ifdef DEBUG | |
742 static void | |
743 extract_number_and_incr (int *destination, unsigned char **source) | |
744 { | |
745 extract_number (destination, *source); | |
746 *source += 2; | |
747 } | |
748 | |
749 #ifndef EXTRACT_MACROS | |
750 #undef EXTRACT_NUMBER_AND_INCR | |
751 #define EXTRACT_NUMBER_AND_INCR(dest, src) \ | |
752 extract_number_and_incr (&dest, &src) | |
753 #endif /* not EXTRACT_MACROS */ | |
754 | |
755 #endif /* DEBUG */ | |
756 | |
757 /* If DEBUG is defined, Regex prints many voluminous messages about what | |
758 it is doing (if the variable `debug' is nonzero). If linked with the | |
759 main program in `iregex.c', you can enter patterns and strings | |
760 interactively. And if linked with the main program in `main.c' and | |
761 the other test files, you can run the already-written tests. */ | |
762 | |
763 #if defined (DEBUG) | |
764 | |
765 /* We use standard I/O for debugging. */ | |
766 #include <stdio.h> | |
767 | |
768 #ifndef emacs | |
769 /* XEmacs provides its own version of assert() */ | |
770 /* It is useful to test things that ``must'' be true when debugging. */ | |
771 #include <assert.h> | |
772 #endif | |
773 | |
5041 | 774 extern int debug_regexps; |
428 | 775 |
776 #define DEBUG_STATEMENT(e) e | |
5041 | 777 |
778 #define DEBUG_PRINT1(x) if (debug_regexps) printf (x) | |
779 #define DEBUG_PRINT2(x1, x2) if (debug_regexps) printf (x1, x2) | |
780 #define DEBUG_PRINT3(x1, x2, x3) if (debug_regexps) printf (x1, x2, x3) | |
781 #define DEBUG_PRINT4(x1, x2, x3, x4) if (debug_regexps) printf (x1, x2, x3, x4) | |
428 | 782 #define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) \ |
5041 | 783 if (debug_regexps) print_partial_compiled_pattern (s, e) |
428 | 784 #define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \ |
5041 | 785 if (debug_regexps) print_double_string (w, s1, sz1, s2, sz2) |
786 | |
787 #define DEBUG_FAIL_PRINT1(x) \ | |
788 if (debug_regexps & RE_DEBUG_FAILURE_POINT) printf (x) | |
789 #define DEBUG_FAIL_PRINT2(x1, x2) \ | |
790 if (debug_regexps & RE_DEBUG_FAILURE_POINT) printf (x1, x2) | |
791 #define DEBUG_FAIL_PRINT3(x1, x2, x3) \ | |
792 if (debug_regexps & RE_DEBUG_FAILURE_POINT) printf (x1, x2, x3) | |
793 #define DEBUG_FAIL_PRINT4(x1, x2, x3, x4) \ | |
794 if (debug_regexps & RE_DEBUG_FAILURE_POINT) printf (x1, x2, x3, x4) | |
795 #define DEBUG_FAIL_PRINT_COMPILED_PATTERN(p, s, e) \ | |
796 if (debug_regexps & RE_DEBUG_FAILURE_POINT) \ | |
797 print_partial_compiled_pattern (s, e) | |
798 #define DEBUG_FAIL_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \ | |
799 if (debug_regexps & RE_DEBUG_FAILURE_POINT) \ | |
800 print_double_string (w, s1, sz1, s2, sz2) | |
801 | |
802 #define DEBUG_MATCH_PRINT1(x) \ | |
803 if (debug_regexps & RE_DEBUG_MATCHING) printf (x) | |
804 #define DEBUG_MATCH_PRINT2(x1, x2) \ | |
805 if (debug_regexps & RE_DEBUG_MATCHING) printf (x1, x2) | |
806 #define DEBUG_MATCH_PRINT3(x1, x2, x3) \ | |
807 if (debug_regexps & RE_DEBUG_MATCHING) printf (x1, x2, x3) | |
808 #define DEBUG_MATCH_PRINT4(x1, x2, x3, x4) \ | |
809 if (debug_regexps & RE_DEBUG_MATCHING) printf (x1, x2, x3, x4) | |
810 #define DEBUG_MATCH_PRINT_COMPILED_PATTERN(p, s, e) \ | |
811 if (debug_regexps & RE_DEBUG_MATCHING) \ | |
812 print_partial_compiled_pattern (s, e) | |
813 #define DEBUG_MATCH_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \ | |
814 if (debug_regexps & RE_DEBUG_MATCHING) \ | |
815 print_double_string (w, s1, sz1, s2, sz2) | |
428 | 816 |
817 | |
818 /* Print the fastmap in human-readable form. */ | |
819 | |
820 static void | |
821 print_fastmap (char *fastmap) | |
822 { | |
647 | 823 int was_a_range = 0; |
824 int i = 0; | |
428 | 825 |
826 while (i < (1 << BYTEWIDTH)) | |
827 { | |
828 if (fastmap[i++]) | |
829 { | |
830 was_a_range = 0; | |
831 putchar (i - 1); | |
832 while (i < (1 << BYTEWIDTH) && fastmap[i]) | |
833 { | |
834 was_a_range = 1; | |
835 i++; | |
836 } | |
837 if (was_a_range) | |
838 { | |
839 putchar ('-'); | |
840 putchar (i - 1); | |
841 } | |
842 } | |
843 } | |
844 putchar ('\n'); | |
845 } | |
846 | |
847 | |
848 /* Print a compiled pattern string in human-readable form, starting at | |
849 the START pointer into it and ending just before the pointer END. */ | |
850 | |
851 static void | |
446 | 852 print_partial_compiled_pattern (re_char *start, re_char *end) |
428 | 853 { |
854 int mcnt, mcnt2; | |
446 | 855 unsigned char *p = (unsigned char *) start; |
856 re_char *pend = end; | |
428 | 857 |
858 if (start == NULL) | |
859 { | |
860 puts ("(null)"); | |
861 return; | |
862 } | |
863 | |
864 /* Loop over pattern commands. */ | |
865 while (p < pend) | |
866 { | |
867 printf ("%ld:\t", (long)(p - start)); | |
868 | |
869 switch ((re_opcode_t) *p++) | |
870 { | |
871 case no_op: | |
872 printf ("/no_op"); | |
873 break; | |
874 | |
875 case exactn: | |
876 mcnt = *p++; | |
877 printf ("/exactn/%d", mcnt); | |
4750
b5f21bb36684
Fix crash in regex.c (closes issue630).
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4527
diff
changeset
|
878 while (mcnt--) |
428 | 879 { |
4750
b5f21bb36684
Fix crash in regex.c (closes issue630).
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4527
diff
changeset
|
880 putchar ('/'); |
428 | 881 putchar (*p++); |
882 } | |
883 break; | |
884 | |
885 case start_memory: | |
886 mcnt = *p++; | |
887 printf ("/start_memory/%d/%d", mcnt, *p++); | |
888 break; | |
889 | |
890 case stop_memory: | |
891 mcnt = *p++; | |
892 printf ("/stop_memory/%d/%d", mcnt, *p++); | |
893 break; | |
894 | |
895 case duplicate: | |
896 printf ("/duplicate/%d", *p++); | |
897 break; | |
898 | |
899 case anychar: | |
900 printf ("/anychar"); | |
901 break; | |
902 | |
903 case charset: | |
904 case charset_not: | |
905 { | |
906 REGISTER int c, last = -100; | |
907 REGISTER int in_range = 0; | |
908 | |
909 printf ("/charset [%s", | |
910 (re_opcode_t) *(p - 1) == charset_not ? "^" : ""); | |
911 | |
912 assert (p + *p < pend); | |
913 | |
914 for (c = 0; c < 256; c++) | |
915 if (((unsigned char) (c / 8) < *p) | |
916 && (p[1 + (c/8)] & (1 << (c % 8)))) | |
917 { | |
918 /* Are we starting a range? */ | |
919 if (last + 1 == c && ! in_range) | |
920 { | |
921 putchar ('-'); | |
922 in_range = 1; | |
923 } | |
924 /* Have we broken a range? */ | |
925 else if (last + 1 != c && in_range) | |
926 { | |
927 putchar (last); | |
928 in_range = 0; | |
929 } | |
930 | |
931 if (! in_range) | |
932 putchar (c); | |
933 | |
934 last = c; | |
935 } | |
936 | |
937 if (in_range) | |
938 putchar (last); | |
939 | |
940 putchar (']'); | |
941 | |
942 p += 1 + *p; | |
943 } | |
944 break; | |
945 | |
946 #ifdef MULE | |
947 case charset_mule: | |
948 case charset_mule_not: | |
949 { | |
950 int nentries, i; | |
951 | |
952 printf ("/charset_mule [%s", | |
953 (re_opcode_t) *(p - 1) == charset_mule_not ? "^" : ""); | |
5648
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
954 printf (" flags: 0x%02x ", *p++); |
428 | 955 nentries = unified_range_table_nentries (p); |
956 for (i = 0; i < nentries; i++) | |
957 { | |
958 EMACS_INT first, last; | |
959 Lisp_Object dummy_val; | |
960 | |
961 unified_range_table_get_range (p, i, &first, &last, | |
962 &dummy_val); | |
5648
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
963 if (first < 0x80) |
428 | 964 putchar (first); |
965 else | |
966 printf ("(0x%lx)", (long)first); | |
967 if (first != last) | |
968 { | |
969 putchar ('-'); | |
5648
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
970 if (last < 0x80) |
428 | 971 putchar (last); |
972 else | |
973 printf ("(0x%lx)", (long)last); | |
974 } | |
975 } | |
976 putchar (']'); | |
977 p += unified_range_table_bytes_used (p); | |
978 } | |
979 break; | |
980 #endif | |
981 | |
982 case begline: | |
983 printf ("/begline"); | |
984 break; | |
985 | |
986 case endline: | |
987 printf ("/endline"); | |
988 break; | |
989 | |
990 case on_failure_jump: | |
991 extract_number_and_incr (&mcnt, &p); | |
992 printf ("/on_failure_jump to %ld", (long)(p + mcnt - start)); | |
993 break; | |
994 | |
995 case on_failure_keep_string_jump: | |
996 extract_number_and_incr (&mcnt, &p); | |
997 printf ("/on_failure_keep_string_jump to %ld", (long)(p + mcnt - start)); | |
998 break; | |
999 | |
1000 case dummy_failure_jump: | |
1001 extract_number_and_incr (&mcnt, &p); | |
1002 printf ("/dummy_failure_jump to %ld", (long)(p + mcnt - start)); | |
1003 break; | |
1004 | |
1005 case push_dummy_failure: | |
1006 printf ("/push_dummy_failure"); | |
1007 break; | |
1008 | |
1009 case maybe_pop_jump: | |
1010 extract_number_and_incr (&mcnt, &p); | |
1011 printf ("/maybe_pop_jump to %ld", (long)(p + mcnt - start)); | |
1012 break; | |
1013 | |
1014 case pop_failure_jump: | |
1015 extract_number_and_incr (&mcnt, &p); | |
1016 printf ("/pop_failure_jump to %ld", (long)(p + mcnt - start)); | |
1017 break; | |
1018 | |
1019 case jump_past_alt: | |
1020 extract_number_and_incr (&mcnt, &p); | |
1021 printf ("/jump_past_alt to %ld", (long)(p + mcnt - start)); | |
1022 break; | |
1023 | |
1024 case jump: | |
1025 extract_number_and_incr (&mcnt, &p); | |
1026 printf ("/jump to %ld", (long)(p + mcnt - start)); | |
1027 break; | |
1028 | |
1029 case succeed_n: | |
1030 extract_number_and_incr (&mcnt, &p); | |
1031 extract_number_and_incr (&mcnt2, &p); | |
1032 printf ("/succeed_n to %ld, %d times", (long)(p + mcnt - start), mcnt2); | |
1033 break; | |
1034 | |
1035 case jump_n: | |
1036 extract_number_and_incr (&mcnt, &p); | |
1037 extract_number_and_incr (&mcnt2, &p); | |
1038 printf ("/jump_n to %ld, %d times", (long)(p + mcnt - start), mcnt2); | |
1039 break; | |
1040 | |
1041 case set_number_at: | |
1042 extract_number_and_incr (&mcnt, &p); | |
1043 extract_number_and_incr (&mcnt2, &p); | |
1044 printf ("/set_number_at location %ld to %d", (long)(p + mcnt - start), mcnt2); | |
1045 break; | |
1046 | |
1047 case wordbound: | |
1048 printf ("/wordbound"); | |
1049 break; | |
1050 | |
1051 case notwordbound: | |
1052 printf ("/notwordbound"); | |
1053 break; | |
1054 | |
1055 case wordbeg: | |
1056 printf ("/wordbeg"); | |
1057 break; | |
1058 | |
1059 case wordend: | |
1060 printf ("/wordend"); | |
1061 | |
1062 #ifdef emacs | |
1063 case before_dot: | |
1064 printf ("/before_dot"); | |
1065 break; | |
1066 | |
1067 case at_dot: | |
1068 printf ("/at_dot"); | |
1069 break; | |
1070 | |
1071 case after_dot: | |
1072 printf ("/after_dot"); | |
1073 break; | |
1074 | |
1075 case syntaxspec: | |
1076 printf ("/syntaxspec"); | |
1077 mcnt = *p++; | |
1078 printf ("/%d", mcnt); | |
1079 break; | |
1080 | |
1081 case notsyntaxspec: | |
1082 printf ("/notsyntaxspec"); | |
1083 mcnt = *p++; | |
1084 printf ("/%d", mcnt); | |
1085 break; | |
1086 | |
1087 #ifdef MULE | |
1088 /* 97/2/17 jhod Mule category patch */ | |
1089 case categoryspec: | |
1090 printf ("/categoryspec"); | |
1091 mcnt = *p++; | |
1092 printf ("/%d", mcnt); | |
1093 break; | |
1094 | |
1095 case notcategoryspec: | |
1096 printf ("/notcategoryspec"); | |
1097 mcnt = *p++; | |
1098 printf ("/%d", mcnt); | |
1099 break; | |
1100 /* end of category patch */ | |
1101 #endif /* MULE */ | |
1102 #endif /* emacs */ | |
1103 | |
1104 case wordchar: | |
1105 printf ("/wordchar"); | |
1106 break; | |
1107 | |
1108 case notwordchar: | |
1109 printf ("/notwordchar"); | |
1110 break; | |
1111 | |
1112 case begbuf: | |
1113 printf ("/begbuf"); | |
1114 break; | |
1115 | |
1116 case endbuf: | |
1117 printf ("/endbuf"); | |
1118 break; | |
1119 | |
1120 default: | |
1121 printf ("?%d", *(p-1)); | |
1122 } | |
1123 | |
1124 putchar ('\n'); | |
1125 } | |
1126 | |
1127 printf ("%ld:\tend of pattern.\n", (long)(p - start)); | |
1128 } | |
1129 | |
1130 | |
1131 static void | |
1132 print_compiled_pattern (struct re_pattern_buffer *bufp) | |
1133 { | |
446 | 1134 re_char *buffer = bufp->buffer; |
428 | 1135 |
1136 print_partial_compiled_pattern (buffer, buffer + bufp->used); | |
1137 printf ("%ld bytes used/%ld bytes allocated.\n", bufp->used, | |
1138 bufp->allocated); | |
1139 | |
1140 if (bufp->fastmap_accurate && bufp->fastmap) | |
1141 { | |
1142 printf ("fastmap: "); | |
1143 print_fastmap (bufp->fastmap); | |
1144 } | |
1145 | |
1146 printf ("re_nsub: %ld\t", (long)bufp->re_nsub); | |
502 | 1147 printf ("re_ngroups: %ld\t", (long)bufp->re_ngroups); |
428 | 1148 printf ("regs_alloc: %d\t", bufp->regs_allocated); |
1149 printf ("can_be_null: %d\t", bufp->can_be_null); | |
1150 printf ("newline_anchor: %d\n", bufp->newline_anchor); | |
1151 printf ("no_sub: %d\t", bufp->no_sub); | |
1152 printf ("not_bol: %d\t", bufp->not_bol); | |
1153 printf ("not_eol: %d\t", bufp->not_eol); | |
1154 printf ("syntax: %d\n", bufp->syntax); | |
1155 /* Perhaps we should print the translate table? */ | |
1156 /* and maybe the category table? */ | |
502 | 1157 |
1158 if (bufp->external_to_internal_register) | |
1159 { | |
1160 int i; | |
1161 | |
1162 printf ("external_to_internal_register:\n"); | |
1163 for (i = 0; i <= bufp->re_nsub; i++) | |
1164 { | |
1165 if (i > 0) | |
1166 printf (", "); | |
1167 printf ("%d -> %d", i, bufp->external_to_internal_register[i]); | |
1168 } | |
1169 printf ("\n"); | |
1170 } | |
428 | 1171 } |
1172 | |
1173 | |
1174 static void | |
446 | 1175 print_double_string (re_char *where, re_char *string1, int size1, |
1176 re_char *string2, int size2) | |
428 | 1177 { |
1178 if (where == NULL) | |
1179 printf ("(null)"); | |
1180 else | |
1181 { | |
647 | 1182 int this_char; |
428 | 1183 |
1184 if (FIRST_STRING_P (where)) | |
1185 { | |
1186 for (this_char = where - string1; this_char < size1; this_char++) | |
1187 putchar (string1[this_char]); | |
1188 | |
1189 where = string2; | |
1190 } | |
1191 | |
1192 for (this_char = where - string2; this_char < size2; this_char++) | |
1193 putchar (string2[this_char]); | |
1194 } | |
1195 } | |
1196 | |
1197 #else /* not DEBUG */ | |
1198 | |
771 | 1199 #ifndef emacs |
428 | 1200 #undef assert |
771 | 1201 #define assert(e) ((void) (1)) |
1202 #endif | |
428 | 1203 |
1204 #define DEBUG_STATEMENT(e) | |
5041 | 1205 |
428 | 1206 #define DEBUG_PRINT1(x) |
1207 #define DEBUG_PRINT2(x1, x2) | |
1208 #define DEBUG_PRINT3(x1, x2, x3) | |
1209 #define DEBUG_PRINT4(x1, x2, x3, x4) | |
1210 #define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) | |
1211 #define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) | |
1212 | |
5041 | 1213 #define DEBUG_FAIL_PRINT1(x) |
1214 #define DEBUG_FAIL_PRINT2(x1, x2) | |
1215 #define DEBUG_FAIL_PRINT3(x1, x2, x3) | |
1216 #define DEBUG_FAIL_PRINT4(x1, x2, x3, x4) | |
1217 #define DEBUG_FAIL_PRINT_COMPILED_PATTERN(p, s, e) | |
1218 #define DEBUG_FAIL_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) | |
1219 | |
1220 #define DEBUG_MATCH_PRINT1(x) | |
1221 #define DEBUG_MATCH_PRINT2(x1, x2) | |
1222 #define DEBUG_MATCH_PRINT3(x1, x2, x3) | |
1223 #define DEBUG_MATCH_PRINT4(x1, x2, x3, x4) | |
1224 #define DEBUG_MATCH_PRINT_COMPILED_PATTERN(p, s, e) | |
1225 #define DEBUG_MATCH_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) | |
1226 | |
446 | 1227 #endif /* DEBUG */ |
428 | 1228 |
1229 /* Set by `re_set_syntax' to the current regexp syntax to recognize. Can | |
1230 also be assigned to arbitrarily: each pattern buffer stores its own | |
1231 syntax, so it can be changed between regex compilations. */ | |
1232 /* This has no initializer because initialized variables in Emacs | |
1233 become read-only after dumping. */ | |
1234 reg_syntax_t re_syntax_options; | |
1235 | |
1236 | |
1237 /* Specify the precise syntax of regexps for compilation. This provides | |
1238 for compatibility for various utilities which historically have | |
1239 different, incompatible syntaxes. | |
1240 | |
1241 The argument SYNTAX is a bit mask comprised of the various bits | |
1242 defined in regex.h. We return the old syntax. */ | |
1243 | |
1244 reg_syntax_t | |
1245 re_set_syntax (reg_syntax_t syntax) | |
1246 { | |
1247 reg_syntax_t ret = re_syntax_options; | |
1248 | |
1249 re_syntax_options = syntax; | |
1250 return ret; | |
1251 } | |
1252 | |
1253 /* This table gives an error message for each of the error codes listed | |
1254 in regex.h. Obviously the order here has to be same as there. | |
1255 POSIX doesn't require that we do anything for REG_NOERROR, | |
1256 but why not be nice? */ | |
1257 | |
442 | 1258 static const char *re_error_msgid[] = |
428 | 1259 { |
1260 "Success", /* REG_NOERROR */ | |
1261 "No match", /* REG_NOMATCH */ | |
1262 "Invalid regular expression", /* REG_BADPAT */ | |
1263 "Invalid collation character", /* REG_ECOLLATE */ | |
1264 "Invalid character class name", /* REG_ECTYPE */ | |
1265 "Trailing backslash", /* REG_EESCAPE */ | |
1266 "Invalid back reference", /* REG_ESUBREG */ | |
1267 "Unmatched [ or [^", /* REG_EBRACK */ | |
1268 "Unmatched ( or \\(", /* REG_EPAREN */ | |
1269 "Unmatched \\{", /* REG_EBRACE */ | |
1270 "Invalid content of \\{\\}", /* REG_BADBR */ | |
1271 "Invalid range end", /* REG_ERANGE */ | |
1272 "Memory exhausted", /* REG_ESPACE */ | |
1273 "Invalid preceding regular expression", /* REG_BADRPT */ | |
1274 "Premature end of regular expression", /* REG_EEND */ | |
1275 "Regular expression too big", /* REG_ESIZE */ | |
1276 "Unmatched ) or \\)", /* REG_ERPAREN */ | |
1277 #ifdef emacs | |
1278 "Invalid syntax designator", /* REG_ESYNTAX */ | |
1279 #endif | |
1280 #ifdef MULE | |
1281 "Ranges may not span charsets", /* REG_ERANGESPAN */ | |
1282 "Invalid category designator", /* REG_ECATEGORY */ | |
1283 #endif | |
1284 }; | |
1285 | |
1286 /* Avoiding alloca during matching, to placate r_alloc. */ | |
1287 | |
1333 | 1288 /* About these various flags: |
1289 | |
1290 MATCH_MAY_ALLOCATE indicates that it's OK to do allocation in the | |
1291 searching and matching functions. In this case, we use local variables | |
1292 to hold the values allocated. If not, we use *global* variables, which | |
1293 are pre-allocated. NOTE: XEmacs ***MUST*** run with MATCH_MAY_ALLOCATE, | |
1294 because the regexp routines may get called reentrantly as a result of | |
1295 QUIT processing (e.g. under Windows: re_match -> QUIT -> quit_p -> drain | |
1296 events -> process WM_INITMENU -> call filter -> re_match; see stack | |
1297 trace in signal.c), so we cannot have any global variables (unless we do | |
1298 lots of trickiness including some unwind-protects, which isn't worth it | |
1299 at this point). | |
1300 | |
1301 REL_ALLOC means that the relocating allocator is in use, for buffers | |
1302 and such. REGEX_REL_ALLOC means that we use rel-alloc to manage the | |
1303 fail stack, which may grow quite large. REGEX_MALLOC means we use | |
1304 malloc() in place of alloca() to allocate the fail stack -- only | |
1305 applicable if REGEX_REL_ALLOC is not defined. | |
1306 */ | |
1307 | |
428 | 1308 /* Define MATCH_MAY_ALLOCATE unless we need to make sure that the |
1309 searching and matching functions should not call alloca. On some | |
1310 systems, alloca is implemented in terms of malloc, and if we're | |
1311 using the relocating allocator routines, then malloc could cause a | |
1312 relocation, which might (if the strings being searched are in the | |
1313 ralloc heap) shift the data out from underneath the regexp | |
771 | 1314 routines. [To clarify: The purpose of rel-alloc is to allow data to |
1315 be moved in memory from one place to another so that all data | |
1316 blocks can be consolidated together and excess memory released back | |
1317 to the operating system. This requires that all the blocks that | |
1318 are managed by rel-alloc go at the very end of the program's heap, | |
1319 after all regularly malloc()ed data. malloc(), however, is used to | |
1320 owning the end of the heap, so that when more memory is needed, it | |
1321 just expands the heap using sbrk(). This is reconciled by using a | |
1322 malloc() (such as malloc.c, gmalloc.c, or recent versions of | |
1323 malloc() in libc) where the sbrk() call can be replaced with a | |
1324 user-specified call -- in this case, to rel-alloc's r_alloc_sbrk() | |
1325 routine. This routine calls the real sbrk(), but then shifts all | |
1326 the rel-alloc-managed blocks forward to the end of the heap again, | |
1327 so that malloc() gets the memory it needs in the location it needs | |
1328 it at. The regex routines may well have pointers to buffer data as | |
1329 their arguments, and buffers are managed by rel-alloc if rel-alloc | |
1330 has been enabled, so calling malloc() may potentially screw things | |
1331 up badly if it runs out of space and asks for more from the OS.] | |
1332 | |
1333 [[Here's another reason to avoid allocation: Emacs processes input | |
1334 from X in a signal handler; processing X input may call malloc; if | |
1335 input arrives while a matching routine is calling malloc, then | |
1336 we're scrod. But Emacs can't just block input while calling | |
1337 matching routines; then we don't notice interrupts when they come | |
1338 in. So, Emacs blocks input around all regexp calls except the | |
1339 matching calls, which it leaves unprotected, in the faith that they | |
1333 | 1340 will not malloc.]] This previous paragraph is irrelevant under XEmacs, |
1341 as we *do not* do anything so stupid as process input from within a | |
1342 signal handler. | |
1343 | |
1344 However, the regexp routines may get called reentrantly as a result of | |
1345 QUIT processing (e.g. under Windows: re_match -> QUIT -> quit_p -> drain | |
1346 events -> process WM_INITMENU -> call filter -> re_match; see stack | |
1347 trace in signal.c), so we cannot have any global variables (unless we do | |
1348 lots of trickiness including some unwind-protects, which isn't worth it | |
1349 at this point). Hence we MUST have MATCH_MAY_ALLOCATE defined. | |
1350 | |
1351 Also, the first paragraph does not make complete sense to me -- what | |
1352 about the use of rel-alloc to handle the fail stacks? Shouldn't these | |
1353 reallocations potentially cause buffer data to be relocated as well? I | |
826 | 1354 must be missing something, though -- perhaps the writer above is |
1355 assuming that the failure stack(s) will always be allocated after the | |
1356 buffer data, and thus reallocating them with rel-alloc won't move buffer | |
1333 | 1357 data. (In fact, a cursory glance at the code in ralloc.c seems to |
1358 confirm this.) --ben */ | |
428 | 1359 |
1360 /* Normally, this is fine. */ | |
1361 #define MATCH_MAY_ALLOCATE | |
1362 | |
1363 /* When using GNU C, we are not REALLY using the C alloca, no matter | |
1364 what config.h may say. So don't take precautions for it. */ | |
1365 #ifdef __GNUC__ | |
1366 #undef C_ALLOCA | |
1367 #endif | |
1368 | |
1369 /* The match routines may not allocate if (1) they would do it with malloc | |
1370 and (2) it's not safe for them to use malloc. | |
1371 Note that if REL_ALLOC is defined, matching would not use malloc for the | |
1372 failure stack, but we would still use it for the register vectors; | |
1373 so REL_ALLOC should not affect this. */ | |
771 | 1374 |
1333 | 1375 /* XEmacs can handle REL_ALLOC and malloc() OK */ |
1376 #if !defined (emacs) && (defined (C_ALLOCA) || defined (REGEX_MALLOC)) && defined (REL_ALLOC) | |
428 | 1377 #undef MATCH_MAY_ALLOCATE |
1378 #endif | |
1379 | |
1333 | 1380 #if !defined (MATCH_MAY_ALLOCATE) && defined (emacs) |
771 | 1381 #error regex must be handle reentrancy; MATCH_MAY_ALLOCATE must be defined |
1382 #endif | |
1383 | |
428 | 1384 |
1385 /* Failure stack declarations and macros; both re_compile_fastmap and | |
1386 re_match_2 use a failure stack. These have to be macros because of | |
1387 REGEX_ALLOCATE_STACK. */ | |
1388 | |
1389 | |
1390 /* Number of failure points for which to initially allocate space | |
1391 when matching. If this number is exceeded, we allocate more | |
1392 space, so it is not a hard limit. */ | |
1393 #ifndef INIT_FAILURE_ALLOC | |
3300 | 1394 #define INIT_FAILURE_ALLOC 20 |
428 | 1395 #endif |
1396 | |
1397 /* Roughly the maximum number of failure points on the stack. Would be | |
1398 exactly that if always used MAX_FAILURE_SPACE each time we failed. | |
1399 This is a variable only so users of regex can assign to it; we never | |
1400 change it ourselves. */ | |
1401 #if defined (MATCH_MAY_ALLOCATE) | |
1402 /* 4400 was enough to cause a crash on Alpha OSF/1, | |
1403 whose default stack limit is 2mb. */ | |
3300 | 1404 int re_max_failures = 40000; |
428 | 1405 #else |
3300 | 1406 int re_max_failures = 4000; |
428 | 1407 #endif |
1408 | |
1409 union fail_stack_elt | |
1410 { | |
446 | 1411 re_char *pointer; |
428 | 1412 int integer; |
1413 }; | |
1414 | |
1415 typedef union fail_stack_elt fail_stack_elt_t; | |
1416 | |
1417 typedef struct | |
1418 { | |
1419 fail_stack_elt_t *stack; | |
665 | 1420 Elemcount size; |
1421 Elemcount avail; /* Offset of next open position. */ | |
428 | 1422 } fail_stack_type; |
1423 | |
1424 #define FAIL_STACK_EMPTY() (fail_stack.avail == 0) | |
1425 #define FAIL_STACK_PTR_EMPTY() (fail_stack_ptr->avail == 0) | |
1426 #define FAIL_STACK_FULL() (fail_stack.avail == fail_stack.size) | |
1427 | |
1428 | |
1429 /* Define macros to initialize and free the failure stack. | |
1430 Do `return -2' if the alloc fails. */ | |
1431 | |
1432 #ifdef MATCH_MAY_ALLOCATE | |
1333 | 1433 #define INIT_FAIL_STACK() \ |
1434 do { \ | |
1435 fail_stack.stack = (fail_stack_elt_t *) \ | |
1436 REGEX_ALLOCATE_STACK (INIT_FAILURE_ALLOC * \ | |
1437 sizeof (fail_stack_elt_t)); \ | |
1438 \ | |
1439 if (fail_stack.stack == NULL) \ | |
1440 { \ | |
1441 UNBIND_REGEX_MALLOC_CHECK (); \ | |
1442 return -2; \ | |
1443 } \ | |
1444 \ | |
1445 fail_stack.size = INIT_FAILURE_ALLOC; \ | |
1446 fail_stack.avail = 0; \ | |
428 | 1447 } while (0) |
1448 | |
1449 #define RESET_FAIL_STACK() REGEX_FREE_STACK (fail_stack.stack) | |
1450 #else | |
1451 #define INIT_FAIL_STACK() \ | |
1452 do { \ | |
1453 fail_stack.avail = 0; \ | |
1454 } while (0) | |
1455 | |
1456 #define RESET_FAIL_STACK() | |
1457 #endif | |
1458 | |
1459 | |
1460 /* Double the size of FAIL_STACK, up to approximately `re_max_failures' items. | |
1461 | |
1462 Return 1 if succeeds, and 0 if either ran out of memory | |
1463 allocating space for it or it was already too large. | |
1464 | |
1465 REGEX_REALLOCATE_STACK requires `destination' be declared. */ | |
1466 | |
1467 #define DOUBLE_FAIL_STACK(fail_stack) \ | |
1468 ((fail_stack).size > re_max_failures * MAX_FAILURE_ITEMS \ | |
1469 ? 0 \ | |
1470 : ((fail_stack).stack = (fail_stack_elt_t *) \ | |
1471 REGEX_REALLOCATE_STACK ((fail_stack).stack, \ | |
1472 (fail_stack).size * sizeof (fail_stack_elt_t), \ | |
1473 ((fail_stack).size << 1) * sizeof (fail_stack_elt_t)), \ | |
1474 \ | |
1475 (fail_stack).stack == NULL \ | |
1476 ? 0 \ | |
1477 : ((fail_stack).size <<= 1, \ | |
1478 1))) | |
1479 | |
1333 | 1480 #if !defined (emacs) || !defined (REL_ALLOC) |
1481 #define RE_MATCH_RELOCATE_MOVEABLE_DATA_POINTERS() | |
1482 #else | |
1483 /* Don't change NULL pointers */ | |
1484 #define ADD_IF_NZ(val) if (val) val += rmdp_offset | |
1346 | 1485 #define RE_MATCH_RELOCATE_MOVEABLE_DATA_POINTERS() \ |
1486 do \ | |
1487 { \ | |
1488 Bytecount rmdp_offset = offset_post_relocation (lispobj, orig_buftext); \ | |
1489 \ | |
1490 if (rmdp_offset) \ | |
1491 { \ | |
1492 int i; \ | |
1493 \ | |
1494 ADD_IF_NZ (string1); \ | |
1495 ADD_IF_NZ (string2); \ | |
1496 ADD_IF_NZ (d); \ | |
1497 ADD_IF_NZ (dend); \ | |
1498 ADD_IF_NZ (end1); \ | |
1499 ADD_IF_NZ (end2); \ | |
1500 ADD_IF_NZ (end_match_1); \ | |
1501 ADD_IF_NZ (end_match_2); \ | |
1502 \ | |
1503 if (bufp->re_ngroups) \ | |
1504 { \ | |
1505 for (i = 0; i < num_regs; i++) \ | |
1506 { \ | |
1507 ADD_IF_NZ (regstart[i]); \ | |
1508 ADD_IF_NZ (regend[i]); \ | |
1509 ADD_IF_NZ (old_regstart[i]); \ | |
1510 ADD_IF_NZ (old_regend[i]); \ | |
1511 ADD_IF_NZ (best_regstart[i]); \ | |
1512 ADD_IF_NZ (best_regend[i]); \ | |
1513 ADD_IF_NZ (reg_dummy[i]); \ | |
1514 } \ | |
1515 } \ | |
1516 \ | |
1517 ADD_IF_NZ (match_end); \ | |
1518 } \ | |
1333 | 1519 } while (0) |
1520 #endif /* !defined (emacs) || !defined (REL_ALLOC) */ | |
1521 | |
1522 #if !defined (emacs) || !defined (REL_ALLOC) | |
1523 #define RE_SEARCH_RELOCATE_MOVEABLE_DATA_POINTERS() | |
1524 #else | |
1346 | 1525 #define RE_SEARCH_RELOCATE_MOVEABLE_DATA_POINTERS() \ |
1526 do \ | |
1527 { \ | |
1528 Bytecount rmdp_offset = offset_post_relocation (lispobj, orig_buftext); \ | |
1529 \ | |
1530 if (rmdp_offset) \ | |
1531 { \ | |
1532 ADD_IF_NZ (str1); \ | |
1533 ADD_IF_NZ (str2); \ | |
1534 ADD_IF_NZ (string1); \ | |
1535 ADD_IF_NZ (string2); \ | |
1536 ADD_IF_NZ (d); \ | |
1537 } \ | |
1333 | 1538 } while (0) |
1539 | |
1540 #endif /* emacs */ | |
428 | 1541 |
1542 /* Push pointer POINTER on FAIL_STACK. | |
1543 Return 1 if was able to do so and 0 if ran out of memory allocating | |
1544 space to do so. */ | |
1545 #define PUSH_PATTERN_OP(POINTER, FAIL_STACK) \ | |
1546 ((FAIL_STACK_FULL () \ | |
1547 && !DOUBLE_FAIL_STACK (FAIL_STACK)) \ | |
1548 ? 0 \ | |
1549 : ((FAIL_STACK).stack[(FAIL_STACK).avail++].pointer = POINTER, \ | |
1550 1)) | |
1551 | |
1552 /* Push a pointer value onto the failure stack. | |
1553 Assumes the variable `fail_stack'. Probably should only | |
1554 be called from within `PUSH_FAILURE_POINT'. */ | |
1555 #define PUSH_FAILURE_POINTER(item) \ | |
1556 fail_stack.stack[fail_stack.avail++].pointer = (unsigned char *) (item) | |
1557 | |
1558 /* This pushes an integer-valued item onto the failure stack. | |
1559 Assumes the variable `fail_stack'. Probably should only | |
1560 be called from within `PUSH_FAILURE_POINT'. */ | |
1561 #define PUSH_FAILURE_INT(item) \ | |
1562 fail_stack.stack[fail_stack.avail++].integer = (item) | |
1563 | |
1564 /* Push a fail_stack_elt_t value onto the failure stack. | |
1565 Assumes the variable `fail_stack'. Probably should only | |
1566 be called from within `PUSH_FAILURE_POINT'. */ | |
1567 #define PUSH_FAILURE_ELT(item) \ | |
1568 fail_stack.stack[fail_stack.avail++] = (item) | |
1569 | |
1570 /* These three POP... operations complement the three PUSH... operations. | |
1571 All assume that `fail_stack' is nonempty. */ | |
1572 #define POP_FAILURE_POINTER() fail_stack.stack[--fail_stack.avail].pointer | |
1573 #define POP_FAILURE_INT() fail_stack.stack[--fail_stack.avail].integer | |
1574 #define POP_FAILURE_ELT() fail_stack.stack[--fail_stack.avail] | |
1575 | |
1576 /* Used to omit pushing failure point id's when we're not debugging. */ | |
1577 #ifdef DEBUG | |
1578 #define DEBUG_PUSH PUSH_FAILURE_INT | |
1579 #define DEBUG_POP(item_addr) *(item_addr) = POP_FAILURE_INT () | |
1580 #else | |
1581 #define DEBUG_PUSH(item) | |
1582 #define DEBUG_POP(item_addr) | |
1583 #endif | |
1584 | |
1585 | |
1586 /* Push the information about the state we will need | |
1587 if we ever fail back to it. | |
1588 | |
1589 Requires variables fail_stack, regstart, regend, reg_info, and | |
1590 num_regs be declared. DOUBLE_FAIL_STACK requires `destination' be | |
1591 declared. | |
1592 | |
1593 Does `return FAILURE_CODE' if runs out of memory. */ | |
1594 | |
771 | 1595 #if !defined (REGEX_MALLOC) && !defined (REGEX_REL_ALLOC) |
456 | 1596 #define DECLARE_DESTINATION char *destination |
428 | 1597 #else |
456 | 1598 #define DECLARE_DESTINATION DECLARE_NOTHING |
428 | 1599 #endif |
1600 | |
1601 #define PUSH_FAILURE_POINT(pattern_place, string_place, failure_code) \ | |
456 | 1602 do { \ |
1603 DECLARE_DESTINATION; \ | |
1604 /* Must be int, so when we don't save any registers, the arithmetic \ | |
1605 of 0 + -1 isn't done as unsigned. */ \ | |
1606 int this_reg; \ | |
428 | 1607 \ |
456 | 1608 DEBUG_STATEMENT (failure_id++); \ |
1609 DEBUG_STATEMENT (nfailure_points_pushed++); \ | |
5041 | 1610 DEBUG_FAIL_PRINT2 ("\nPUSH_FAILURE_POINT #%d:\n", failure_id); \ |
1611 DEBUG_FAIL_PRINT2 (" Before push, next avail: %ld\n", \ | |
647 | 1612 (long) (fail_stack).avail); \ |
5041 | 1613 DEBUG_FAIL_PRINT2 (" size: %ld\n", \ |
647 | 1614 (long) (fail_stack).size); \ |
456 | 1615 \ |
5041 | 1616 DEBUG_FAIL_PRINT2 (" slots needed: %d\n", NUM_FAILURE_ITEMS); \ |
1617 DEBUG_FAIL_PRINT2 (" available: %ld\n", \ | |
456 | 1618 (long) REMAINING_AVAIL_SLOTS); \ |
428 | 1619 \ |
456 | 1620 /* Ensure we have enough space allocated for what we will push. */ \ |
1621 while (REMAINING_AVAIL_SLOTS < NUM_FAILURE_ITEMS) \ | |
1622 { \ | |
1333 | 1623 BEGIN_REGEX_MALLOC_OK (); \ |
456 | 1624 if (!DOUBLE_FAIL_STACK (fail_stack)) \ |
1333 | 1625 { \ |
1626 END_REGEX_MALLOC_OK (); \ | |
1627 UNBIND_REGEX_MALLOC_CHECK (); \ | |
1628 return failure_code; \ | |
1629 } \ | |
1630 END_REGEX_MALLOC_OK (); \ | |
5041 | 1631 DEBUG_FAIL_PRINT2 ("\n Doubled stack; size now: %ld\n", \ |
647 | 1632 (long) (fail_stack).size); \ |
5041 | 1633 DEBUG_FAIL_PRINT2 (" slots available: %ld\n", \ |
456 | 1634 (long) REMAINING_AVAIL_SLOTS); \ |
1333 | 1635 \ |
1636 RE_MATCH_RELOCATE_MOVEABLE_DATA_POINTERS (); \ | |
456 | 1637 } \ |
428 | 1638 \ |
456 | 1639 /* Push the info, starting with the registers. */ \ |
5041 | 1640 DEBUG_FAIL_PRINT1 ("\n"); \ |
428 | 1641 \ |
456 | 1642 for (this_reg = lowest_active_reg; this_reg <= highest_active_reg; \ |
1643 this_reg++) \ | |
1644 { \ | |
5041 | 1645 DEBUG_FAIL_PRINT2 (" Pushing reg: %d\n", this_reg); \ |
456 | 1646 DEBUG_STATEMENT (num_regs_pushed++); \ |
428 | 1647 \ |
5041 | 1648 DEBUG_FAIL_PRINT2 (" start: 0x%lx\n", (long) regstart[this_reg]); \ |
456 | 1649 PUSH_FAILURE_POINTER (regstart[this_reg]); \ |
1650 \ | |
5041 | 1651 DEBUG_FAIL_PRINT2 (" end: 0x%lx\n", (long) regend[this_reg]); \ |
456 | 1652 PUSH_FAILURE_POINTER (regend[this_reg]); \ |
428 | 1653 \ |
5041 | 1654 DEBUG_FAIL_PRINT2 (" info: 0x%lx\n ", \ |
456 | 1655 * (long *) (®_info[this_reg])); \ |
5041 | 1656 DEBUG_FAIL_PRINT2 (" match_null=%d", \ |
456 | 1657 REG_MATCH_NULL_STRING_P (reg_info[this_reg])); \ |
5041 | 1658 DEBUG_FAIL_PRINT2 (" active=%d", IS_ACTIVE (reg_info[this_reg])); \ |
1659 DEBUG_FAIL_PRINT2 (" matched_something=%d", \ | |
456 | 1660 MATCHED_SOMETHING (reg_info[this_reg])); \ |
5041 | 1661 DEBUG_FAIL_PRINT2 (" ever_matched_something=%d", \ |
456 | 1662 EVER_MATCHED_SOMETHING (reg_info[this_reg])); \ |
5041 | 1663 DEBUG_FAIL_PRINT1 ("\n"); \ |
456 | 1664 PUSH_FAILURE_ELT (reg_info[this_reg].word); \ |
1665 } \ | |
428 | 1666 \ |
5041 | 1667 DEBUG_FAIL_PRINT2 (" Pushing low active reg: %d\n", lowest_active_reg); \ |
456 | 1668 PUSH_FAILURE_INT (lowest_active_reg); \ |
428 | 1669 \ |
5041 | 1670 DEBUG_FAIL_PRINT2 (" Pushing high active reg: %d\n", highest_active_reg); \ |
456 | 1671 PUSH_FAILURE_INT (highest_active_reg); \ |
428 | 1672 \ |
5041 | 1673 DEBUG_FAIL_PRINT2 (" Pushing pattern 0x%lx: \n", (long) pattern_place); \ |
1674 DEBUG_FAIL_PRINT_COMPILED_PATTERN (bufp, pattern_place, pend); \ | |
456 | 1675 PUSH_FAILURE_POINTER (pattern_place); \ |
428 | 1676 \ |
5041 | 1677 DEBUG_FAIL_PRINT2 (" Pushing string 0x%lx: `", (long) string_place); \ |
1678 DEBUG_FAIL_PRINT_DOUBLE_STRING (string_place, string1, size1, string2, \ | |
456 | 1679 size2); \ |
5041 | 1680 DEBUG_FAIL_PRINT1 ("'\n"); \ |
456 | 1681 PUSH_FAILURE_POINTER (string_place); \ |
428 | 1682 \ |
5041 | 1683 DEBUG_FAIL_PRINT2 (" Pushing failure id: %u\n", failure_id); \ |
456 | 1684 DEBUG_PUSH (failure_id); \ |
1685 } while (0) | |
428 | 1686 |
1687 /* This is the number of items that are pushed and popped on the stack | |
1688 for each register. */ | |
1689 #define NUM_REG_ITEMS 3 | |
1690 | |
1691 /* Individual items aside from the registers. */ | |
1692 #ifdef DEBUG | |
1693 #define NUM_NONREG_ITEMS 5 /* Includes failure point id. */ | |
1694 #else | |
1695 #define NUM_NONREG_ITEMS 4 | |
1696 #endif | |
1697 | |
1698 /* We push at most this many items on the stack. */ | |
1699 /* We used to use (num_regs - 1), which is the number of registers | |
1700 this regexp will save; but that was changed to 5 | |
1701 to avoid stack overflow for a regexp with lots of parens. */ | |
1702 #define MAX_FAILURE_ITEMS (5 * NUM_REG_ITEMS + NUM_NONREG_ITEMS) | |
1703 | |
1704 /* We actually push this many items. */ | |
1705 #define NUM_FAILURE_ITEMS \ | |
1706 ((highest_active_reg - lowest_active_reg + 1) * NUM_REG_ITEMS \ | |
1707 + NUM_NONREG_ITEMS) | |
1708 | |
1709 /* How many items can still be added to the stack without overflowing it. */ | |
1710 #define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail) | |
1711 | |
1712 | |
1713 /* Pops what PUSH_FAIL_STACK pushes. | |
1714 | |
1715 We restore into the parameters, all of which should be lvalues: | |
1716 STR -- the saved data position. | |
1717 PAT -- the saved pattern position. | |
1718 LOW_REG, HIGH_REG -- the highest and lowest active registers. | |
1719 REGSTART, REGEND -- arrays of string positions. | |
1720 REG_INFO -- array of information about each subexpression. | |
1721 | |
1722 Also assumes the variables `fail_stack' and (if debugging), `bufp', | |
1723 `pend', `string1', `size1', `string2', and `size2'. */ | |
1724 | |
456 | 1725 #define POP_FAILURE_POINT(str, pat, low_reg, high_reg, \ |
1726 regstart, regend, reg_info) \ | |
1727 do { \ | |
428 | 1728 DEBUG_STATEMENT (fail_stack_elt_t ffailure_id;) \ |
1729 int this_reg; \ | |
442 | 1730 const unsigned char *string_temp; \ |
428 | 1731 \ |
1732 assert (!FAIL_STACK_EMPTY ()); \ | |
1733 \ | |
1734 /* Remove failure points and point to how many regs pushed. */ \ | |
5041 | 1735 DEBUG_FAIL_PRINT1 ("POP_FAILURE_POINT:\n"); \ |
1736 DEBUG_FAIL_PRINT2 (" Before pop, next avail: %ld\n", \ | |
647 | 1737 (long) fail_stack.avail); \ |
5041 | 1738 DEBUG_FAIL_PRINT2 (" size: %ld\n", \ |
647 | 1739 (long) fail_stack.size); \ |
428 | 1740 \ |
1741 assert (fail_stack.avail >= NUM_NONREG_ITEMS); \ | |
1742 \ | |
1743 DEBUG_POP (&ffailure_id.integer); \ | |
5041 | 1744 DEBUG_FAIL_PRINT2 (" Popping failure id: %d\n", \ |
647 | 1745 * (int *) &ffailure_id); \ |
428 | 1746 \ |
1747 /* If the saved string location is NULL, it came from an \ | |
1748 on_failure_keep_string_jump opcode, and we want to throw away the \ | |
1749 saved NULL, thus retaining our current position in the string. */ \ | |
1750 string_temp = POP_FAILURE_POINTER (); \ | |
1751 if (string_temp != NULL) \ | |
446 | 1752 str = string_temp; \ |
428 | 1753 \ |
5041 | 1754 DEBUG_FAIL_PRINT2 (" Popping string 0x%lx: `", (long) str); \ |
1755 DEBUG_FAIL_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2); \ | |
1756 DEBUG_FAIL_PRINT1 ("'\n"); \ | |
428 | 1757 \ |
1758 pat = (unsigned char *) POP_FAILURE_POINTER (); \ | |
5041 | 1759 DEBUG_FAIL_PRINT2 (" Popping pattern 0x%lx: ", (long) pat); \ |
1760 DEBUG_FAIL_PRINT_COMPILED_PATTERN (bufp, pat, pend); \ | |
428 | 1761 \ |
1762 /* Restore register info. */ \ | |
647 | 1763 high_reg = POP_FAILURE_INT (); \ |
5041 | 1764 DEBUG_FAIL_PRINT2 (" Popping high active reg: %d\n", high_reg); \ |
428 | 1765 \ |
647 | 1766 low_reg = POP_FAILURE_INT (); \ |
5041 | 1767 DEBUG_FAIL_PRINT2 (" Popping low active reg: %d\n", low_reg); \ |
428 | 1768 \ |
1769 for (this_reg = high_reg; this_reg >= low_reg; this_reg--) \ | |
1770 { \ | |
5041 | 1771 DEBUG_FAIL_PRINT2 (" Popping reg: %d\n", this_reg); \ |
428 | 1772 \ |
1773 reg_info[this_reg].word = POP_FAILURE_ELT (); \ | |
5041 | 1774 DEBUG_FAIL_PRINT2 (" info: 0x%lx\n", \ |
428 | 1775 * (long *) ®_info[this_reg]); \ |
1776 \ | |
446 | 1777 regend[this_reg] = POP_FAILURE_POINTER (); \ |
5041 | 1778 DEBUG_FAIL_PRINT2 (" end: 0x%lx\n", (long) regend[this_reg]); \ |
428 | 1779 \ |
446 | 1780 regstart[this_reg] = POP_FAILURE_POINTER (); \ |
5041 | 1781 DEBUG_FAIL_PRINT2 (" start: 0x%lx\n", (long) regstart[this_reg]); \ |
428 | 1782 } \ |
1783 \ | |
1784 set_regs_matched_done = 0; \ | |
1785 DEBUG_STATEMENT (nfailure_points_popped++); \ | |
456 | 1786 } while (0) /* POP_FAILURE_POINT */ |
428 | 1787 |
1788 | |
1789 | |
1790 /* Structure for per-register (a.k.a. per-group) information. | |
1791 Other register information, such as the | |
1792 starting and ending positions (which are addresses), and the list of | |
1793 inner groups (which is a bits list) are maintained in separate | |
1794 variables. | |
1795 | |
1796 We are making a (strictly speaking) nonportable assumption here: that | |
1797 the compiler will pack our bit fields into something that fits into | |
1798 the type of `word', i.e., is something that fits into one item on the | |
1799 failure stack. */ | |
1800 | |
1801 typedef union | |
1802 { | |
1803 fail_stack_elt_t word; | |
1804 struct | |
1805 { | |
1806 /* This field is one if this group can match the empty string, | |
1807 zero if not. If not yet determined, `MATCH_NULL_UNSET_VALUE'. */ | |
1808 #define MATCH_NULL_UNSET_VALUE 3 | |
647 | 1809 unsigned int match_null_string_p : 2; |
1810 unsigned int is_active : 1; | |
1811 unsigned int matched_something : 1; | |
1812 unsigned int ever_matched_something : 1; | |
428 | 1813 } bits; |
1814 } register_info_type; | |
1815 | |
1816 #define REG_MATCH_NULL_STRING_P(R) ((R).bits.match_null_string_p) | |
1817 #define IS_ACTIVE(R) ((R).bits.is_active) | |
1818 #define MATCHED_SOMETHING(R) ((R).bits.matched_something) | |
1819 #define EVER_MATCHED_SOMETHING(R) ((R).bits.ever_matched_something) | |
1820 | |
1821 | |
1822 /* Call this when have matched a real character; it sets `matched' flags | |
1823 for the subexpressions which we are currently inside. Also records | |
1824 that those subexprs have matched. */ | |
1825 #define SET_REGS_MATCHED() \ | |
1826 do \ | |
1827 { \ | |
1828 if (!set_regs_matched_done) \ | |
1829 { \ | |
647 | 1830 int r; \ |
428 | 1831 set_regs_matched_done = 1; \ |
1832 for (r = lowest_active_reg; r <= highest_active_reg; r++) \ | |
1833 { \ | |
1834 MATCHED_SOMETHING (reg_info[r]) \ | |
1835 = EVER_MATCHED_SOMETHING (reg_info[r]) \ | |
1836 = 1; \ | |
1837 } \ | |
1838 } \ | |
1839 } \ | |
1840 while (0) | |
1841 | |
1842 /* Registers are set to a sentinel when they haven't yet matched. */ | |
446 | 1843 static unsigned char reg_unset_dummy; |
428 | 1844 #define REG_UNSET_VALUE (®_unset_dummy) |
1845 #define REG_UNSET(e) ((e) == REG_UNSET_VALUE) | |
1846 | |
1847 /* Subroutine declarations and macros for regex_compile. */ | |
1848 | |
1849 /* Fetch the next character in the uncompiled pattern---translating it | |
826 | 1850 if necessary. */ |
428 | 1851 #define PATFETCH(c) \ |
446 | 1852 do { \ |
1853 PATFETCH_RAW (c); \ | |
826 | 1854 c = RE_TRANSLATE (c); \ |
428 | 1855 } while (0) |
1856 | |
1857 /* Fetch the next character in the uncompiled pattern, with no | |
1858 translation. */ | |
1859 #define PATFETCH_RAW(c) \ | |
1860 do {if (p == pend) return REG_EEND; \ | |
1861 assert (p < pend); \ | |
867 | 1862 c = itext_ichar (p); \ |
1863 INC_IBYTEPTR (p); \ | |
428 | 1864 } while (0) |
1865 | |
1866 /* Go backwards one character in the pattern. */ | |
867 | 1867 #define PATUNFETCH DEC_IBYTEPTR (p) |
428 | 1868 |
1869 /* If `translate' is non-null, return translate[D], else just D. We | |
1870 cast the subscript to translate because some data is declared as | |
1871 `char *', to avoid warnings when a string constant is passed. But | |
1872 when we use a character as a subscript we must make it unsigned. */ | |
826 | 1873 #define RE_TRANSLATE(d) \ |
1874 (TRANSLATE_P (translate) ? RE_TRANSLATE_1 (d) : (d)) | |
428 | 1875 |
1876 /* Macros for outputting the compiled pattern into `buffer'. */ | |
1877 | |
1878 /* If the buffer isn't allocated when it comes in, use this. */ | |
1879 #define INIT_BUF_SIZE 32 | |
1880 | |
1881 /* Make sure we have at least N more bytes of space in buffer. */ | |
1882 #define GET_BUFFER_SPACE(n) \ | |
647 | 1883 while (buf_end - bufp->buffer + (n) > (ptrdiff_t) bufp->allocated) \ |
428 | 1884 EXTEND_BUFFER () |
1885 | |
1886 /* Make sure we have one more byte of buffer space and then add C to it. */ | |
1887 #define BUF_PUSH(c) \ | |
1888 do { \ | |
1889 GET_BUFFER_SPACE (1); \ | |
446 | 1890 *buf_end++ = (unsigned char) (c); \ |
428 | 1891 } while (0) |
1892 | |
1893 | |
1894 /* Ensure we have two more bytes of buffer space and then append C1 and C2. */ | |
1895 #define BUF_PUSH_2(c1, c2) \ | |
1896 do { \ | |
1897 GET_BUFFER_SPACE (2); \ | |
446 | 1898 *buf_end++ = (unsigned char) (c1); \ |
1899 *buf_end++ = (unsigned char) (c2); \ | |
428 | 1900 } while (0) |
1901 | |
1902 | |
1903 /* As with BUF_PUSH_2, except for three bytes. */ | |
1904 #define BUF_PUSH_3(c1, c2, c3) \ | |
1905 do { \ | |
1906 GET_BUFFER_SPACE (3); \ | |
446 | 1907 *buf_end++ = (unsigned char) (c1); \ |
1908 *buf_end++ = (unsigned char) (c2); \ | |
1909 *buf_end++ = (unsigned char) (c3); \ | |
428 | 1910 } while (0) |
1911 | |
1912 | |
1913 /* Store a jump with opcode OP at LOC to location TO. We store a | |
1914 relative address offset by the three bytes the jump itself occupies. */ | |
1915 #define STORE_JUMP(op, loc, to) \ | |
1916 store_op1 (op, loc, (to) - (loc) - 3) | |
1917 | |
1918 /* Likewise, for a two-argument jump. */ | |
1919 #define STORE_JUMP2(op, loc, to, arg) \ | |
1920 store_op2 (op, loc, (to) - (loc) - 3, arg) | |
1921 | |
446 | 1922 /* Like `STORE_JUMP', but for inserting. Assume `buf_end' is the |
1923 buffer end. */ | |
428 | 1924 #define INSERT_JUMP(op, loc, to) \ |
446 | 1925 insert_op1 (op, loc, (to) - (loc) - 3, buf_end) |
1926 | |
1927 /* Like `STORE_JUMP2', but for inserting. Assume `buf_end' is the | |
1928 buffer end. */ | |
428 | 1929 #define INSERT_JUMP2(op, loc, to, arg) \ |
446 | 1930 insert_op2 (op, loc, (to) - (loc) - 3, arg, buf_end) |
428 | 1931 |
1932 | |
1933 /* This is not an arbitrary limit: the arguments which represent offsets | |
1934 into the pattern are two bytes long. So if 2^16 bytes turns out to | |
1935 be too small, many things would have to change. */ | |
1936 #define MAX_BUF_SIZE (1L << 16) | |
1937 | |
1938 | |
1939 /* Extend the buffer by twice its current size via realloc and | |
1940 reset the pointers that pointed into the old block to point to the | |
1941 correct places in the new one. If extending the buffer results in it | |
1942 being larger than MAX_BUF_SIZE, then flag memory exhausted. */ | |
1333 | 1943 #define EXTEND_BUFFER() \ |
1944 do { \ | |
1945 re_char *old_buffer = bufp->buffer; \ | |
1946 if (bufp->allocated == MAX_BUF_SIZE) \ | |
1947 return REG_ESIZE; \ | |
1948 bufp->allocated <<= 1; \ | |
1949 if (bufp->allocated > MAX_BUF_SIZE) \ | |
1950 bufp->allocated = MAX_BUF_SIZE; \ | |
1951 bufp->buffer = \ | |
1952 (unsigned char *) xrealloc (bufp->buffer, bufp->allocated); \ | |
1953 if (bufp->buffer == NULL) \ | |
1954 return REG_ESPACE; \ | |
1955 /* If the buffer moved, move all the pointers into it. */ \ | |
1956 if (old_buffer != bufp->buffer) \ | |
1957 { \ | |
1958 buf_end = (buf_end - old_buffer) + bufp->buffer; \ | |
1959 begalt = (begalt - old_buffer) + bufp->buffer; \ | |
1960 if (fixup_alt_jump) \ | |
1961 fixup_alt_jump = (fixup_alt_jump - old_buffer) + bufp->buffer; \ | |
1962 if (laststart) \ | |
1963 laststart = (laststart - old_buffer) + bufp->buffer; \ | |
1964 if (pending_exact) \ | |
1965 pending_exact = (pending_exact - old_buffer) + bufp->buffer; \ | |
1966 } \ | |
428 | 1967 } while (0) |
1968 | |
1969 | |
1970 /* Since we have one byte reserved for the register number argument to | |
1971 {start,stop}_memory, the maximum number of groups we can report | |
1972 things about is what fits in that byte. */ | |
1973 #define MAX_REGNUM 255 | |
1974 | |
1975 /* But patterns can have more than `MAX_REGNUM' registers. We just | |
502 | 1976 ignore the excess. |
1977 #### not true! groups past this will fail in lots of ways, if we | |
1978 ever have to backtrack. | |
1979 */ | |
647 | 1980 typedef int regnum_t; |
428 | 1981 |
502 | 1982 #define INIT_REG_TRANSLATE_SIZE 5 |
428 | 1983 |
1984 /* Macros for the compile stack. */ | |
1985 | |
1986 /* Since offsets can go either forwards or backwards, this type needs to | |
1987 be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1. */ | |
1988 typedef int pattern_offset_t; | |
1989 | |
1990 typedef struct | |
1991 { | |
1992 pattern_offset_t begalt_offset; | |
1993 pattern_offset_t fixup_alt_jump; | |
1994 pattern_offset_t inner_group_offset; | |
1995 pattern_offset_t laststart_offset; | |
1996 regnum_t regnum; | |
1997 } compile_stack_elt_t; | |
1998 | |
1999 | |
2000 typedef struct | |
2001 { | |
2002 compile_stack_elt_t *stack; | |
647 | 2003 int size; |
2004 int avail; /* Offset of next open position. */ | |
428 | 2005 } compile_stack_type; |
2006 | |
2007 | |
2008 #define INIT_COMPILE_STACK_SIZE 32 | |
2009 | |
2010 #define COMPILE_STACK_EMPTY (compile_stack.avail == 0) | |
2011 #define COMPILE_STACK_FULL (compile_stack.avail == compile_stack.size) | |
2012 | |
2013 /* The next available element. */ | |
2014 #define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail]) | |
2015 | |
5648
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2016 /* Bits used to implement the multibyte-part of the various character |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2017 classes such as [:alnum:] in a charset's range table. XEmacs; use an |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2018 enum, so they're visible in the debugger. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2019 enum |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2020 { |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2021 BIT_WORD = (1 << 0), |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2022 BIT_LOWER = (1 << 1), |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2023 BIT_PUNCT = (1 << 2), |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2024 BIT_SPACE = (1 << 3), |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2025 BIT_UPPER = (1 << 4), |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2026 /* XEmacs; we need this, because we unify treatment of ASCII and non-ASCII |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2027 (possible matches) in charset_mule. [:alpha:] matches all characters |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2028 with word syntax, with the exception of [0-9]. We don't need |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2029 BIT_MULTIBYTE. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2030 BIT_ALPHA = (1 << 5) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2031 }; |
428 | 2032 |
2033 /* Set the bit for character C in a bit vector. */ | |
2034 #define SET_LIST_BIT(c) \ | |
446 | 2035 (buf_end[((unsigned char) (c)) / BYTEWIDTH] \ |
428 | 2036 |= 1 << (((unsigned char) c) % BYTEWIDTH)) |
2037 | |
2038 #ifdef MULE | |
2039 | |
2040 /* Set the "bit" for character C in a range table. */ | |
2041 #define SET_RANGETAB_BIT(c) put_range_table (rtab, c, c, Qt) | |
2042 | |
2043 #endif | |
2044 | |
2045 /* Get the next unsigned number in the uncompiled pattern. */ | |
2046 #define GET_UNSIGNED_NUMBER(num) \ | |
2047 { if (p != pend) \ | |
2048 { \ | |
2049 PATFETCH (c); \ | |
2050 while (ISDIGIT (c)) \ | |
2051 { \ | |
2052 if (num < 0) \ | |
2053 num = 0; \ | |
2054 num = num * 10 + c - '0'; \ | |
2055 if (p == pend) \ | |
2056 break; \ | |
2057 PATFETCH (c); \ | |
2058 } \ | |
2059 } \ | |
2060 } | |
2061 | |
5648
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2062 #define CHAR_CLASS_MAX_LENGTH 9 /* Namely, `multibyte'. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2063 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2064 /* Map a string to the char class it names (if any). */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2065 static re_wctype_t |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2066 re_wctype (const char *string) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2067 { |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2068 if (STREQ (string, "alnum")) return RECC_ALNUM; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2069 else if (STREQ (string, "alpha")) return RECC_ALPHA; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2070 else if (STREQ (string, "word")) return RECC_WORD; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2071 else if (STREQ (string, "ascii")) return RECC_ASCII; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2072 else if (STREQ (string, "nonascii")) return RECC_NONASCII; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2073 else if (STREQ (string, "graph")) return RECC_GRAPH; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2074 else if (STREQ (string, "lower")) return RECC_LOWER; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2075 else if (STREQ (string, "print")) return RECC_PRINT; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2076 else if (STREQ (string, "punct")) return RECC_PUNCT; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2077 else if (STREQ (string, "space")) return RECC_SPACE; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2078 else if (STREQ (string, "upper")) return RECC_UPPER; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2079 else if (STREQ (string, "unibyte")) return RECC_UNIBYTE; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2080 else if (STREQ (string, "multibyte")) return RECC_MULTIBYTE; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2081 else if (STREQ (string, "digit")) return RECC_DIGIT; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2082 else if (STREQ (string, "xdigit")) return RECC_XDIGIT; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2083 else if (STREQ (string, "cntrl")) return RECC_CNTRL; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2084 else if (STREQ (string, "blank")) return RECC_BLANK; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2085 else return RECC_ERROR; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2086 } |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2087 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2088 /* True if CH is in the char class CC. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2089 static re_bool |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2090 re_iswctype (int ch, re_wctype_t cc) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2091 { |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2092 #ifdef emacs |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2093 /* This is cheesy, lispbuf isn't available to us when compiling the |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2094 pattern. It's effectively only called (on Mule builds) when the current |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2095 buffer doesn't matter (e.g. for RECC_ASCII, RECC_CNTRL), so it's not a |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2096 big deal. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2097 struct buffer *lispbuf = current_buffer; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2098 #endif |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2099 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2100 switch (cc) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2101 { |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2102 case RECC_ALNUM: return ISALNUM (ch) != 0; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2103 case RECC_ALPHA: return ISALPHA (ch) != 0; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2104 case RECC_BLANK: return ISBLANK (ch) != 0; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2105 case RECC_CNTRL: return ISCNTRL (ch) != 0; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2106 case RECC_DIGIT: return ISDIGIT (ch) != 0; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2107 case RECC_GRAPH: return ISGRAPH (ch) != 0; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2108 case RECC_LOWER: return ISLOWER (ch) != 0; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2109 case RECC_PRINT: return ISPRINT (ch) != 0; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2110 case RECC_PUNCT: return ISPUNCT (ch) != 0; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2111 case RECC_SPACE: return ISSPACE (ch) != 0; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2112 case RECC_UPPER: return ISUPPER (ch) != 0; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2113 case RECC_XDIGIT: return ISXDIGIT (ch) != 0; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2114 case RECC_ASCII: return ISASCII (ch) != 0; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2115 case RECC_NONASCII: case RECC_MULTIBYTE: return !ISASCII (ch); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2116 case RECC_UNIBYTE: return ISUNIBYTE (ch) != 0; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2117 case RECC_WORD: return ISWORD (ch) != 0; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2118 case RECC_ERROR: return false; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2119 default: |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2120 abort (); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2121 } |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2122 } |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2123 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2124 #ifdef MULE |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2125 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2126 static re_bool |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2127 re_wctype_can_match_non_ascii (re_wctype_t cc) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2128 { |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2129 switch (cc) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2130 { |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2131 case RECC_ASCII: |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2132 case RECC_UNIBYTE: |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2133 case RECC_CNTRL: |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2134 case RECC_DIGIT: |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2135 case RECC_XDIGIT: |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2136 case RECC_BLANK: |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2137 return false; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2138 default: |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2139 return true; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2140 } |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2141 } |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2142 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2143 /* Return a bit-pattern to use in the range-table bits to match multibyte |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2144 chars of class CC. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2145 static unsigned char |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2146 re_wctype_to_bit (re_wctype_t cc) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2147 { |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2148 switch (cc) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2149 { |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2150 case RECC_PRINT: case RECC_GRAPH: |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2151 case RECC_ALPHA: return BIT_ALPHA; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2152 case RECC_ALNUM: case RECC_WORD: return BIT_WORD; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2153 case RECC_LOWER: return BIT_LOWER; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2154 case RECC_UPPER: return BIT_UPPER; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2155 case RECC_PUNCT: return BIT_PUNCT; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2156 case RECC_SPACE: return BIT_SPACE; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2157 case RECC_MULTIBYTE: case RECC_NONASCII: |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2158 case RECC_ASCII: case RECC_DIGIT: case RECC_XDIGIT: case RECC_CNTRL: |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2159 case RECC_BLANK: case RECC_UNIBYTE: case RECC_ERROR: return 0; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2160 default: |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2161 abort (); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2162 } |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2163 } |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2164 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2165 #endif /* emacs */ |
428 | 2166 |
2167 static void store_op1 (re_opcode_t op, unsigned char *loc, int arg); | |
2168 static void store_op2 (re_opcode_t op, unsigned char *loc, int arg1, int arg2); | |
2169 static void insert_op1 (re_opcode_t op, unsigned char *loc, int arg, | |
2170 unsigned char *end); | |
2171 static void insert_op2 (re_opcode_t op, unsigned char *loc, int arg1, int arg2, | |
2172 unsigned char *end); | |
460 | 2173 static re_bool at_begline_loc_p (re_char *pattern, re_char *p, |
428 | 2174 reg_syntax_t syntax); |
460 | 2175 static re_bool at_endline_loc_p (re_char *p, re_char *pend, int syntax); |
2176 static re_bool group_in_compile_stack (compile_stack_type compile_stack, | |
428 | 2177 regnum_t regnum); |
446 | 2178 static reg_errcode_t compile_range (re_char **p_ptr, re_char *pend, |
2179 RE_TRANSLATE_TYPE translate, | |
2180 reg_syntax_t syntax, | |
428 | 2181 unsigned char *b); |
2182 #ifdef MULE | |
446 | 2183 static reg_errcode_t compile_extended_range (re_char **p_ptr, |
2184 re_char *pend, | |
2185 RE_TRANSLATE_TYPE translate, | |
428 | 2186 reg_syntax_t syntax, |
2187 Lisp_Object rtab); | |
5648
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2188 static reg_errcode_t compile_char_class (re_wctype_t cc, Lisp_Object rtab, |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2189 Bitbyte *flags_out); |
428 | 2190 #endif /* MULE */ |
460 | 2191 static re_bool group_match_null_string_p (unsigned char **p, |
428 | 2192 unsigned char *end, |
2193 register_info_type *reg_info); | |
460 | 2194 static re_bool alt_match_null_string_p (unsigned char *p, unsigned char *end, |
428 | 2195 register_info_type *reg_info); |
460 | 2196 static re_bool common_op_match_null_string_p (unsigned char **p, |
428 | 2197 unsigned char *end, |
2198 register_info_type *reg_info); | |
826 | 2199 static int bcmp_translate (re_char *s1, re_char *s2, |
2200 REGISTER int len, RE_TRANSLATE_TYPE translate | |
2201 #ifdef emacs | |
2202 , Internal_Format fmt, Lisp_Object lispobj | |
2203 #endif | |
2204 ); | |
428 | 2205 static int re_match_2_internal (struct re_pattern_buffer *bufp, |
446 | 2206 re_char *string1, int size1, |
2207 re_char *string2, int size2, int pos, | |
826 | 2208 struct re_registers *regs, int stop |
2209 RE_LISP_CONTEXT_ARGS_DECL); | |
428 | 2210 |
2211 #ifndef MATCH_MAY_ALLOCATE | |
2212 | |
2213 /* If we cannot allocate large objects within re_match_2_internal, | |
2214 we make the fail stack and register vectors global. | |
2215 The fail stack, we grow to the maximum size when a regexp | |
2216 is compiled. | |
2217 The register vectors, we adjust in size each time we | |
2218 compile a regexp, according to the number of registers it needs. */ | |
2219 | |
2220 static fail_stack_type fail_stack; | |
2221 | |
2222 /* Size with which the following vectors are currently allocated. | |
2223 That is so we can make them bigger as needed, | |
2224 but never make them smaller. */ | |
2225 static int regs_allocated_size; | |
2226 | |
446 | 2227 static re_char ** regstart, ** regend; |
2228 static re_char ** old_regstart, ** old_regend; | |
2229 static re_char **best_regstart, **best_regend; | |
428 | 2230 static register_info_type *reg_info; |
446 | 2231 static re_char **reg_dummy; |
428 | 2232 static register_info_type *reg_info_dummy; |
2233 | |
2234 /* Make the register vectors big enough for NUM_REGS registers, | |
2235 but don't make them smaller. */ | |
2236 | |
2237 static | |
2238 regex_grow_registers (int num_regs) | |
2239 { | |
2240 if (num_regs > regs_allocated_size) | |
2241 { | |
551 | 2242 RETALLOC (regstart, num_regs, re_char *); |
2243 RETALLOC (regend, num_regs, re_char *); | |
2244 RETALLOC (old_regstart, num_regs, re_char *); | |
2245 RETALLOC (old_regend, num_regs, re_char *); | |
2246 RETALLOC (best_regstart, num_regs, re_char *); | |
2247 RETALLOC (best_regend, num_regs, re_char *); | |
2248 RETALLOC (reg_info, num_regs, register_info_type); | |
2249 RETALLOC (reg_dummy, num_regs, re_char *); | |
2250 RETALLOC (reg_info_dummy, num_regs, register_info_type); | |
428 | 2251 |
2252 regs_allocated_size = num_regs; | |
2253 } | |
2254 } | |
2255 | |
2256 #endif /* not MATCH_MAY_ALLOCATE */ | |
2257 | |
2258 /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX. | |
2259 Returns one of error codes defined in `regex.h', or zero for success. | |
2260 | |
2261 Assumes the `allocated' (and perhaps `buffer') and `translate' | |
2262 fields are set in BUFP on entry. | |
2263 | |
2264 If it succeeds, results are put in BUFP (if it returns an error, the | |
2265 contents of BUFP are undefined): | |
2266 `buffer' is the compiled pattern; | |
2267 `syntax' is set to SYNTAX; | |
2268 `used' is set to the length of the compiled pattern; | |
2269 `fastmap_accurate' is zero; | |
502 | 2270 `re_ngroups' is the number of groups/subexpressions (including shy |
2271 groups) in PATTERN; | |
2272 `re_nsub' is the number of non-shy groups in PATTERN; | |
428 | 2273 `not_bol' and `not_eol' are zero; |
2274 | |
2275 The `fastmap' and `newline_anchor' fields are neither | |
2276 examined nor set. */ | |
2277 | |
2278 /* Return, freeing storage we allocated. */ | |
1726 | 2279 #define FREE_STACK_RETURN(value) \ |
2280 do \ | |
2281 { \ | |
4976
16112448d484
Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents:
4832
diff
changeset
|
2282 xfree (compile_stack.stack); \ |
1726 | 2283 return value; \ |
1333 | 2284 } while (0) |
428 | 2285 |
2286 static reg_errcode_t | |
446 | 2287 regex_compile (re_char *pattern, int size, reg_syntax_t syntax, |
428 | 2288 struct re_pattern_buffer *bufp) |
2289 { | |
2290 /* We fetch characters from PATTERN here. We declare these as int | |
2291 (or possibly long) so that chars above 127 can be used as | |
2292 array indices. The macros that fetch a character from the pattern | |
2293 make sure to coerce to unsigned char before assigning, so we won't | |
2294 get bitten by negative numbers here. */ | |
2295 /* XEmacs change: used to be unsigned char. */ | |
2296 REGISTER EMACS_INT c, c1; | |
2297 | |
2298 /* A random temporary spot in PATTERN. */ | |
446 | 2299 re_char *p1; |
428 | 2300 |
2301 /* Points to the end of the buffer, where we should append. */ | |
446 | 2302 REGISTER unsigned char *buf_end; |
428 | 2303 |
2304 /* Keeps track of unclosed groups. */ | |
2305 compile_stack_type compile_stack; | |
2306 | |
2307 /* Points to the current (ending) position in the pattern. */ | |
446 | 2308 re_char *p = pattern; |
2309 re_char *pend = pattern + size; | |
428 | 2310 |
2311 /* How to translate the characters in the pattern. */ | |
446 | 2312 RE_TRANSLATE_TYPE translate = bufp->translate; |
428 | 2313 |
2314 /* Address of the count-byte of the most recently inserted `exactn' | |
2315 command. This makes it possible to tell if a new exact-match | |
2316 character can be added to that command or if the character requires | |
2317 a new `exactn' command. */ | |
2318 unsigned char *pending_exact = 0; | |
2319 | |
2320 /* Address of start of the most recently finished expression. | |
2321 This tells, e.g., postfix * where to find the start of its | |
2322 operand. Reset at the beginning of groups and alternatives. */ | |
2323 unsigned char *laststart = 0; | |
2324 | |
2325 /* Address of beginning of regexp, or inside of last group. */ | |
2326 unsigned char *begalt; | |
2327 | |
2328 /* Place in the uncompiled pattern (i.e., the {) to | |
2329 which to go back if the interval is invalid. */ | |
446 | 2330 re_char *beg_interval; |
428 | 2331 |
2332 /* Address of the place where a forward jump should go to the end of | |
2333 the containing expression. Each alternative of an `or' -- except the | |
2334 last -- ends with a forward jump of this sort. */ | |
2335 unsigned char *fixup_alt_jump = 0; | |
2336 | |
2337 /* Counts open-groups as they are encountered. Remembered for the | |
2338 matching close-group on the compile stack, so the same register | |
2339 number is put in the stop_memory as the start_memory. */ | |
2340 regnum_t regnum = 0; | |
2341 | |
2342 #ifdef DEBUG | |
5041 | 2343 if (debug_regexps & RE_DEBUG_COMPILATION) |
428 | 2344 { |
647 | 2345 int debug_count; |
428 | 2346 |
5041 | 2347 DEBUG_PRINT1 ("\nCompiling pattern: "); |
428 | 2348 for (debug_count = 0; debug_count < size; debug_count++) |
2349 putchar (pattern[debug_count]); | |
2350 putchar ('\n'); | |
2351 } | |
2352 #endif /* DEBUG */ | |
2353 | |
2354 /* Initialize the compile stack. */ | |
2355 compile_stack.stack = TALLOC (INIT_COMPILE_STACK_SIZE, compile_stack_elt_t); | |
2356 if (compile_stack.stack == NULL) | |
2357 return REG_ESPACE; | |
2358 | |
2359 compile_stack.size = INIT_COMPILE_STACK_SIZE; | |
2360 compile_stack.avail = 0; | |
2361 | |
2362 /* Initialize the pattern buffer. */ | |
2363 bufp->syntax = syntax; | |
2364 bufp->fastmap_accurate = 0; | |
2365 bufp->not_bol = bufp->not_eol = 0; | |
2366 | |
2367 /* Set `used' to zero, so that if we return an error, the pattern | |
2368 printer (for debugging) will think there's no pattern. We reset it | |
2369 at the end. */ | |
2370 bufp->used = 0; | |
2371 | |
2372 /* Always count groups, whether or not bufp->no_sub is set. */ | |
2373 bufp->re_nsub = 0; | |
502 | 2374 bufp->re_ngroups = 0; |
2375 | |
2376 bufp->warned_about_incompatible_back_references = 0; | |
2377 | |
2378 if (bufp->external_to_internal_register == 0) | |
2379 { | |
2380 bufp->external_to_internal_register_size = INIT_REG_TRANSLATE_SIZE; | |
2381 RETALLOC (bufp->external_to_internal_register, | |
2382 bufp->external_to_internal_register_size, | |
2383 int); | |
2384 } | |
2385 | |
2386 { | |
2387 int i; | |
2388 | |
2389 bufp->external_to_internal_register[0] = 0; | |
2390 for (i = 1; i < bufp->external_to_internal_register_size; i++) | |
2391 bufp->external_to_internal_register[i] = (int) 0xDEADBEEF; | |
2392 } | |
428 | 2393 |
2394 #if !defined (emacs) && !defined (SYNTAX_TABLE) | |
2395 /* Initialize the syntax table. */ | |
2396 init_syntax_once (); | |
2397 #endif | |
2398 | |
2399 if (bufp->allocated == 0) | |
2400 { | |
2401 if (bufp->buffer) | |
2402 { /* If zero allocated, but buffer is non-null, try to realloc | |
2403 enough space. This loses if buffer's address is bogus, but | |
2404 that is the user's responsibility. */ | |
2405 RETALLOC (bufp->buffer, INIT_BUF_SIZE, unsigned char); | |
2406 } | |
2407 else | |
2408 { /* Caller did not allocate a buffer. Do it for them. */ | |
2409 bufp->buffer = TALLOC (INIT_BUF_SIZE, unsigned char); | |
2410 } | |
2411 if (!bufp->buffer) FREE_STACK_RETURN (REG_ESPACE); | |
2412 | |
2413 bufp->allocated = INIT_BUF_SIZE; | |
2414 } | |
2415 | |
446 | 2416 begalt = buf_end = bufp->buffer; |
428 | 2417 |
2418 /* Loop through the uncompiled pattern until we're at the end. */ | |
2419 while (p != pend) | |
2420 { | |
2421 PATFETCH (c); | |
2422 | |
2423 switch (c) | |
2424 { | |
2425 case '^': | |
2426 { | |
2427 if ( /* If at start of pattern, it's an operator. */ | |
2428 p == pattern + 1 | |
2429 /* If context independent, it's an operator. */ | |
2430 || syntax & RE_CONTEXT_INDEP_ANCHORS | |
2431 /* Otherwise, depends on what's come before. */ | |
2432 || at_begline_loc_p (pattern, p, syntax)) | |
2433 BUF_PUSH (begline); | |
2434 else | |
2435 goto normal_char; | |
2436 } | |
2437 break; | |
2438 | |
2439 | |
2440 case '$': | |
2441 { | |
2442 if ( /* If at end of pattern, it's an operator. */ | |
2443 p == pend | |
2444 /* If context independent, it's an operator. */ | |
2445 || syntax & RE_CONTEXT_INDEP_ANCHORS | |
2446 /* Otherwise, depends on what's next. */ | |
2447 || at_endline_loc_p (p, pend, syntax)) | |
2448 BUF_PUSH (endline); | |
2449 else | |
2450 goto normal_char; | |
2451 } | |
2452 break; | |
2453 | |
2454 | |
2455 case '+': | |
2456 case '?': | |
2457 if ((syntax & RE_BK_PLUS_QM) | |
2458 || (syntax & RE_LIMITED_OPS)) | |
2459 goto normal_char; | |
2460 handle_plus: | |
2461 case '*': | |
2462 /* If there is no previous pattern... */ | |
2463 if (!laststart) | |
2464 { | |
2465 if (syntax & RE_CONTEXT_INVALID_OPS) | |
2466 FREE_STACK_RETURN (REG_BADRPT); | |
2467 else if (!(syntax & RE_CONTEXT_INDEP_OPS)) | |
2468 goto normal_char; | |
2469 } | |
2470 | |
2471 { | |
2472 /* true means zero/many matches are allowed. */ | |
460 | 2473 re_bool zero_times_ok = c != '+'; |
2474 re_bool many_times_ok = c != '?'; | |
428 | 2475 |
2476 /* true means match shortest string possible. */ | |
460 | 2477 re_bool minimal = false; |
428 | 2478 |
2479 /* If there is a sequence of repetition chars, collapse it | |
2480 down to just one (the right one). We can't combine | |
2481 interval operators with these because of, e.g., `a{2}*', | |
2482 which should only match an even number of `a's. */ | |
2483 while (p != pend) | |
2484 { | |
2485 PATFETCH (c); | |
2486 | |
2487 if (c == '*' || (!(syntax & RE_BK_PLUS_QM) | |
2488 && (c == '+' || c == '?'))) | |
2489 ; | |
2490 | |
2491 else if (syntax & RE_BK_PLUS_QM && c == '\\') | |
2492 { | |
2493 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE); | |
2494 | |
2495 PATFETCH (c1); | |
2496 if (!(c1 == '+' || c1 == '?')) | |
2497 { | |
2498 PATUNFETCH; | |
2499 PATUNFETCH; | |
2500 break; | |
2501 } | |
2502 | |
2503 c = c1; | |
2504 } | |
2505 else | |
2506 { | |
2507 PATUNFETCH; | |
2508 break; | |
2509 } | |
2510 | |
2511 /* If we get here, we found another repeat character. */ | |
2512 if (!(syntax & RE_NO_MINIMAL_MATCHING)) | |
2513 { | |
440 | 2514 /* "*?" and "+?" and "??" are okay (and mean match |
2515 minimally), but other sequences (such as "*??" and | |
2516 "+++") are rejected (reserved for future use). */ | |
428 | 2517 if (minimal || c != '?') |
2518 FREE_STACK_RETURN (REG_BADRPT); | |
2519 minimal = true; | |
2520 } | |
2521 else | |
2522 { | |
2523 zero_times_ok |= c != '+'; | |
2524 many_times_ok |= c != '?'; | |
2525 } | |
2526 } | |
2527 | |
2528 /* Star, etc. applied to an empty pattern is equivalent | |
2529 to an empty pattern. */ | |
2530 if (!laststart) | |
2531 break; | |
2532 | |
2533 /* Now we know whether zero matches is allowed | |
2534 and whether two or more matches is allowed | |
2535 and whether we want minimal or maximal matching. */ | |
2536 if (minimal) | |
2537 { | |
2538 if (!many_times_ok) | |
2539 { | |
2540 /* "a??" becomes: | |
2541 0: /on_failure_jump to 6 | |
2542 3: /jump to 9 | |
2543 6: /exactn/1/A | |
2544 9: end of pattern. | |
2545 */ | |
2546 GET_BUFFER_SPACE (6); | |
446 | 2547 INSERT_JUMP (jump, laststart, buf_end + 3); |
2548 buf_end += 3; | |
428 | 2549 INSERT_JUMP (on_failure_jump, laststart, laststart + 6); |
446 | 2550 buf_end += 3; |
428 | 2551 } |
2552 else if (zero_times_ok) | |
2553 { | |
2554 /* "a*?" becomes: | |
2555 0: /jump to 6 | |
2556 3: /exactn/1/A | |
2557 6: /on_failure_jump to 3 | |
2558 9: end of pattern. | |
2559 */ | |
2560 GET_BUFFER_SPACE (6); | |
446 | 2561 INSERT_JUMP (jump, laststart, buf_end + 3); |
2562 buf_end += 3; | |
2563 STORE_JUMP (on_failure_jump, buf_end, laststart + 3); | |
2564 buf_end += 3; | |
428 | 2565 } |
2566 else | |
2567 { | |
2568 /* "a+?" becomes: | |
2569 0: /exactn/1/A | |
2570 3: /on_failure_jump to 0 | |
2571 6: end of pattern. | |
2572 */ | |
2573 GET_BUFFER_SPACE (3); | |
446 | 2574 STORE_JUMP (on_failure_jump, buf_end, laststart); |
2575 buf_end += 3; | |
428 | 2576 } |
2577 } | |
2578 else | |
2579 { | |
2580 /* Are we optimizing this jump? */ | |
460 | 2581 re_bool keep_string_p = false; |
428 | 2582 |
2583 if (many_times_ok) | |
446 | 2584 { /* More than one repetition is allowed, so put in |
2585 at the end a backward relative jump from | |
2586 `buf_end' to before the next jump we're going | |
2587 to put in below (which jumps from laststart to | |
2588 after this jump). | |
428 | 2589 |
2590 But if we are at the `*' in the exact sequence `.*\n', | |
2591 insert an unconditional jump backwards to the ., | |
2592 instead of the beginning of the loop. This way we only | |
2593 push a failure point once, instead of every time | |
2594 through the loop. */ | |
2595 assert (p - 1 > pattern); | |
2596 | |
2597 /* Allocate the space for the jump. */ | |
2598 GET_BUFFER_SPACE (3); | |
2599 | |
2600 /* We know we are not at the first character of the | |
2601 pattern, because laststart was nonzero. And we've | |
2602 already incremented `p', by the way, to be the | |
2603 character after the `*'. Do we have to do something | |
2604 analogous here for null bytes, because of | |
2605 RE_DOT_NOT_NULL? */ | |
446 | 2606 if (*(p - 2) == '.' |
428 | 2607 && zero_times_ok |
446 | 2608 && p < pend && *p == '\n' |
428 | 2609 && !(syntax & RE_DOT_NEWLINE)) |
2610 { /* We have .*\n. */ | |
446 | 2611 STORE_JUMP (jump, buf_end, laststart); |
428 | 2612 keep_string_p = true; |
2613 } | |
2614 else | |
2615 /* Anything else. */ | |
446 | 2616 STORE_JUMP (maybe_pop_jump, buf_end, laststart - 3); |
428 | 2617 |
2618 /* We've added more stuff to the buffer. */ | |
446 | 2619 buf_end += 3; |
428 | 2620 } |
2621 | |
446 | 2622 /* On failure, jump from laststart to buf_end + 3, |
2623 which will be the end of the buffer after this jump | |
2624 is inserted. */ | |
428 | 2625 GET_BUFFER_SPACE (3); |
2626 INSERT_JUMP (keep_string_p ? on_failure_keep_string_jump | |
2627 : on_failure_jump, | |
446 | 2628 laststart, buf_end + 3); |
2629 buf_end += 3; | |
428 | 2630 |
2631 if (!zero_times_ok) | |
2632 { | |
2633 /* At least one repetition is required, so insert a | |
2634 `dummy_failure_jump' before the initial | |
2635 `on_failure_jump' instruction of the loop. This | |
2636 effects a skip over that instruction the first time | |
2637 we hit that loop. */ | |
2638 GET_BUFFER_SPACE (3); | |
2639 INSERT_JUMP (dummy_failure_jump, laststart, laststart + 6); | |
446 | 2640 buf_end += 3; |
428 | 2641 } |
2642 } | |
2643 pending_exact = 0; | |
2644 } | |
2645 break; | |
2646 | |
2647 | |
2648 case '.': | |
446 | 2649 laststart = buf_end; |
428 | 2650 BUF_PUSH (anychar); |
2651 break; | |
2652 | |
5648
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2653 #ifdef MULE |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2654 #define MAYBE_START_OVER_WITH_EXTENDED(ch) \ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2655 if (ch >= 0x80) \ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2656 { \ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2657 goto start_over_with_extended; \ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2658 } while (0) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2659 #else |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2660 #define MAYBE_START_OVER_WITH_EXTENDED(ch) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2661 #endif |
428 | 2662 |
2663 case '[': | |
2664 { | |
2665 /* XEmacs change: this whole section */ | |
460 | 2666 re_bool had_char_class = false; |
428 | 2667 |
2668 if (p == pend) FREE_STACK_RETURN (REG_EBRACK); | |
2669 | |
2670 /* Ensure that we have enough space to push a charset: the | |
2671 opcode, the length count, and the bitset; 34 bytes in all. */ | |
2672 GET_BUFFER_SPACE (34); | |
2673 | |
446 | 2674 laststart = buf_end; |
428 | 2675 |
2676 /* We test `*p == '^' twice, instead of using an if | |
2677 statement, so we only need one BUF_PUSH. */ | |
2678 BUF_PUSH (*p == '^' ? charset_not : charset); | |
2679 if (*p == '^') | |
2680 p++; | |
2681 | |
2682 /* Remember the first position in the bracket expression. */ | |
2683 p1 = p; | |
2684 | |
2685 /* Push the number of bytes in the bitmap. */ | |
2686 BUF_PUSH ((1 << BYTEWIDTH) / BYTEWIDTH); | |
2687 | |
2688 /* Clear the whole map. */ | |
446 | 2689 memset (buf_end, 0, (1 << BYTEWIDTH) / BYTEWIDTH); |
428 | 2690 |
2691 /* charset_not matches newline according to a syntax bit. */ | |
446 | 2692 if ((re_opcode_t) buf_end[-2] == charset_not |
428 | 2693 && (syntax & RE_HAT_LISTS_NOT_NEWLINE)) |
2694 SET_LIST_BIT ('\n'); | |
2695 | |
2696 /* Read in characters and ranges, setting map bits. */ | |
2697 for (;;) | |
2698 { | |
2699 if (p == pend) FREE_STACK_RETURN (REG_EBRACK); | |
2700 | |
446 | 2701 PATFETCH (c); |
428 | 2702 |
5648
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2703 /* Frumble-bumble, we may have found some extended chars. |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2704 Need to start over, process everything using the general |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2705 extended-char mechanism, and need to use charset_mule and |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2706 charset_mule_not instead of charset and charset_not. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2707 MAYBE_START_OVER_WITH_EXTENDED (c); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2708 |
428 | 2709 /* \ might escape characters inside [...] and [^...]. */ |
2710 if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) && c == '\\') | |
2711 { | |
2712 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE); | |
2713 | |
446 | 2714 PATFETCH (c1); |
5648
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2715 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2716 MAYBE_START_OVER_WITH_EXTENDED (c1); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2717 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2718 SET_LIST_BIT (c1); |
428 | 2719 continue; |
2720 } | |
2721 | |
2722 /* Could be the end of the bracket expression. If it's | |
2723 not (i.e., when the bracket expression is `[]' so | |
2724 far), the ']' character bit gets set way below. */ | |
2725 if (c == ']' && p != p1 + 1) | |
2726 break; | |
2727 | |
2728 /* Look ahead to see if it's a range when the last thing | |
2729 was a character class. */ | |
2730 if (had_char_class && c == '-' && *p != ']') | |
2731 FREE_STACK_RETURN (REG_ERANGE); | |
2732 | |
2733 /* Look ahead to see if it's a range when the last thing | |
2734 was a character: if this is a hyphen not at the | |
2735 beginning or the end of a list, then it's the range | |
2736 operator. */ | |
2737 if (c == '-' | |
2738 && !(p - 2 >= pattern && p[-2] == '[') | |
446 | 2739 && !(p - 3 >= pattern && p[-3] == '[' && p[-2] == '^') |
428 | 2740 && *p != ']') |
2741 { | |
2742 reg_errcode_t ret; | |
2743 | |
5648
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2744 MAYBE_START_OVER_WITH_EXTENDED (*(unsigned char *)p); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2745 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2746 ret = compile_range (&p, pend, translate, syntax, |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2747 buf_end); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2748 |
428 | 2749 if (ret != REG_NOERROR) FREE_STACK_RETURN (ret); |
2750 } | |
2751 | |
2752 else if (p[0] == '-' && p[1] != ']') | |
2753 { /* This handles ranges made up of characters only. */ | |
2754 reg_errcode_t ret; | |
2755 | |
2756 /* Move past the `-'. */ | |
2757 PATFETCH (c1); | |
2758 | |
5648
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2759 MAYBE_START_OVER_WITH_EXTENDED (*(unsigned char *)p); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2760 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2761 ret = compile_range (&p, pend, translate, syntax, buf_end); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2762 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2763 if (ret != REG_NOERROR) FREE_STACK_RETURN (ret); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2764 } |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2765 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2766 /* See if we're at the beginning of a possible character |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2767 class. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2768 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2769 else if (syntax & RE_CHAR_CLASSES && c == '[' && *p == ':') |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2770 { /* Leave room for the null. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2771 char str[CHAR_CLASS_MAX_LENGTH + 1]; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2772 int ch = 0; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2773 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2774 PATFETCH (c); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2775 c1 = 0; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2776 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2777 /* If pattern is `[[:'. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2778 if (p == pend) FREE_STACK_RETURN (REG_EBRACK); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2779 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2780 for (;;) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2781 { |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2782 PATFETCH (c); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2783 if ((c == ':' && *p == ']') || p == pend) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2784 break; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2785 if (c1 < CHAR_CLASS_MAX_LENGTH) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2786 str[c1++] = c; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2787 else |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2788 /* This is in any case an invalid class name. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2789 str[0] = '\0'; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2790 } |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2791 str[c1] = '\0'; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2792 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2793 /* If isn't a word bracketed by `[:' and `:]': |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2794 undo the ending character, the letters, and leave |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2795 the leading `:' and `[' (but set bits for them). */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2796 if (c == ':' && *p == ']') |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2797 { |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2798 re_wctype_t cc = re_wctype (str); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2799 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2800 if (cc == RECC_ERROR) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2801 FREE_STACK_RETURN (REG_ECTYPE); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2802 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2803 /* Throw away the ] at the end of the character |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2804 class. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2805 PATFETCH (c); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2806 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2807 if (p == pend) FREE_STACK_RETURN (REG_EBRACK); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2808 |
428 | 2809 #ifdef MULE |
5648
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2810 if (re_wctype_can_match_non_ascii (cc)) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2811 { |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2812 goto start_over_with_extended; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2813 } |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2814 #endif /* MULE */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2815 for (ch = 0; ch < (1 << BYTEWIDTH); ++ch) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2816 { |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2817 if (re_iswctype (ch, cc)) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2818 { |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2819 SET_LIST_BIT (ch); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2820 } |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2821 } |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2822 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2823 had_char_class = true; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2824 } |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2825 else |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2826 { |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2827 c1++; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2828 while (c1--) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2829 PATUNFETCH; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2830 SET_LIST_BIT ('['); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2831 SET_LIST_BIT (':'); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2832 had_char_class = false; |
428 | 2833 } |
5648
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2834 } |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2835 else |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2836 { |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2837 had_char_class = false; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2838 SET_LIST_BIT (c); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2839 } |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2840 } |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2841 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2842 /* Discard any (non)matching list bytes that are all 0 at the |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2843 end of the map. Decrease the map-length byte too. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2844 while ((int) buf_end[-1] > 0 && buf_end[buf_end[-1] - 1] == 0) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2845 buf_end[-1]--; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2846 buf_end += buf_end[-1]; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2847 } |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2848 break; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2849 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2850 #ifdef MULE |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2851 start_over_with_extended: |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2852 { |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2853 REGISTER Lisp_Object rtab = Qnil; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2854 Bitbyte flags = 0; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2855 int bytes_needed = sizeof (flags); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2856 re_bool had_char_class = false; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2857 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2858 /* There are extended chars here, which means we need to use the |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2859 unified range-table format. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2860 if (buf_end[-2] == charset) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2861 buf_end[-2] = charset_mule; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2862 else |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2863 buf_end[-2] = charset_mule_not; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2864 buf_end--; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2865 p = p1; /* go back to the beginning of the charset, after |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2866 a possible ^. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2867 rtab = Vthe_lisp_rangetab; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2868 Fclear_range_table (rtab); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2869 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2870 /* charset_not matches newline according to a syntax bit. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2871 if ((re_opcode_t) buf_end[-1] == charset_mule_not |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2872 && (syntax & RE_HAT_LISTS_NOT_NEWLINE)) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2873 SET_RANGETAB_BIT ('\n'); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2874 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2875 /* Read in characters and ranges, setting map bits. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2876 for (;;) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2877 { |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2878 if (p == pend) FREE_STACK_RETURN (REG_EBRACK); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2879 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2880 PATFETCH (c); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2881 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2882 /* \ might escape characters inside [...] and [^...]. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2883 if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) && c == '\\') |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2884 { |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2885 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2886 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2887 PATFETCH (c1); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2888 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2889 SET_RANGETAB_BIT (c1); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2890 continue; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2891 } |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2892 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2893 /* Could be the end of the bracket expression. If it's |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2894 not (i.e., when the bracket expression is `[]' so |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2895 far), the ']' character bit gets set way below. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2896 if (c == ']' && p != p1 + 1) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2897 break; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2898 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2899 /* Look ahead to see if it's a range when the last thing |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2900 was a character class. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2901 if (had_char_class && c == '-' && *p != ']') |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2902 FREE_STACK_RETURN (REG_ERANGE); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2903 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2904 /* Look ahead to see if it's a range when the last thing |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2905 was a character: if this is a hyphen not at the |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2906 beginning or the end of a list, then it's the range |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2907 operator. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2908 if (c == '-' |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2909 && !(p - 2 >= pattern && p[-2] == '[') |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2910 && !(p - 3 >= pattern && p[-3] == '[' && p[-2] == '^') |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2911 && *p != ']') |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2912 { |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2913 reg_errcode_t ret; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2914 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2915 ret = compile_extended_range (&p, pend, translate, syntax, |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2916 rtab); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2917 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2918 if (ret != REG_NOERROR) FREE_STACK_RETURN (ret); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2919 } |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2920 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2921 else if (p[0] == '-' && p[1] != ']') |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2922 { /* This handles ranges made up of characters only. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2923 reg_errcode_t ret; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2924 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2925 /* Move past the `-'. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2926 PATFETCH (c1); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2927 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2928 ret = compile_extended_range (&p, pend, translate, |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2929 syntax, rtab); |
428 | 2930 if (ret != REG_NOERROR) FREE_STACK_RETURN (ret); |
2931 } | |
2932 | |
2933 /* See if we're at the beginning of a possible character | |
2934 class. */ | |
2935 | |
2936 else if (syntax & RE_CHAR_CLASSES && c == '[' && *p == ':') | |
2937 { /* Leave room for the null. */ | |
2938 char str[CHAR_CLASS_MAX_LENGTH + 1]; | |
2939 | |
2940 PATFETCH (c); | |
2941 c1 = 0; | |
2942 | |
2943 /* If pattern is `[[:'. */ | |
2944 if (p == pend) FREE_STACK_RETURN (REG_EBRACK); | |
2945 | |
2946 for (;;) | |
2947 { | |
2948 PATFETCH (c); | |
5648
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2949 if ((c == ':' && *p == ']') || p == pend) |
428 | 2950 break; |
5648
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2951 if (c1 < CHAR_CLASS_MAX_LENGTH) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2952 str[c1++] = c; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2953 else |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2954 /* This is in any case an invalid class name. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2955 str[0] = '\0'; |
428 | 2956 } |
2957 str[c1] = '\0'; | |
2958 | |
446 | 2959 /* If isn't a word bracketed by `[:' and `:]': |
428 | 2960 undo the ending character, the letters, and leave |
2961 the leading `:' and `[' (but set bits for them). */ | |
2962 if (c == ':' && *p == ']') | |
2963 { | |
5648
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2964 re_wctype_t cc = re_wctype (str); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2965 reg_errcode_t ret = REG_NOERROR; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2966 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2967 if (cc == RECC_ERROR) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2968 FREE_STACK_RETURN (REG_ECTYPE); |
428 | 2969 |
2970 /* Throw away the ] at the end of the character | |
2971 class. */ | |
2972 PATFETCH (c); | |
2973 | |
2974 if (p == pend) FREE_STACK_RETURN (REG_EBRACK); | |
2975 | |
5648
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2976 ret = compile_char_class (cc, rtab, &flags); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2977 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2978 if (ret != REG_NOERROR) FREE_STACK_RETURN (ret); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2979 |
428 | 2980 had_char_class = true; |
2981 } | |
2982 else | |
2983 { | |
2984 c1++; | |
2985 while (c1--) | |
2986 PATUNFETCH; | |
5648
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2987 SET_RANGETAB_BIT ('['); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2988 SET_RANGETAB_BIT (':'); |
428 | 2989 had_char_class = false; |
2990 } | |
2991 } | |
2992 else | |
2993 { | |
2994 had_char_class = false; | |
5648
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2995 SET_RANGETAB_BIT (c); |
428 | 2996 } |
2997 } | |
2998 | |
5648
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
2999 bytes_needed += unified_range_table_bytes_needed (rtab); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3000 GET_BUFFER_SPACE (bytes_needed); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3001 *buf_end++ = flags; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3002 unified_range_table_copy_data (rtab, buf_end); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3003 buf_end += unified_range_table_bytes_used (buf_end); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3004 break; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3005 } |
428 | 3006 #endif /* MULE */ |
3007 | |
3008 case '(': | |
3009 if (syntax & RE_NO_BK_PARENS) | |
3010 goto handle_open; | |
3011 else | |
3012 goto normal_char; | |
3013 | |
3014 | |
3015 case ')': | |
3016 if (syntax & RE_NO_BK_PARENS) | |
3017 goto handle_close; | |
3018 else | |
3019 goto normal_char; | |
3020 | |
3021 | |
3022 case '\n': | |
3023 if (syntax & RE_NEWLINE_ALT) | |
3024 goto handle_alt; | |
3025 else | |
3026 goto normal_char; | |
3027 | |
3028 | |
3029 case '|': | |
3030 if (syntax & RE_NO_BK_VBAR) | |
3031 goto handle_alt; | |
3032 else | |
3033 goto normal_char; | |
3034 | |
3035 | |
3036 case '{': | |
3037 if (syntax & RE_INTERVALS && syntax & RE_NO_BK_BRACES) | |
3038 goto handle_interval; | |
3039 else | |
3040 goto normal_char; | |
3041 | |
3042 | |
3043 case '\\': | |
3044 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE); | |
3045 | |
3046 /* Do not translate the character after the \, so that we can | |
3047 distinguish, e.g., \B from \b, even if we normally would | |
3048 translate, e.g., B to b. */ | |
3049 PATFETCH_RAW (c); | |
3050 | |
3051 switch (c) | |
3052 { | |
3053 case '(': | |
3054 if (syntax & RE_NO_BK_PARENS) | |
3055 goto normal_backslash; | |
3056 | |
3057 handle_open: | |
3058 { | |
3059 regnum_t r; | |
502 | 3060 int shy = 0; |
428 | 3061 |
3062 if (!(syntax & RE_NO_SHY_GROUPS) | |
3063 && p != pend | |
446 | 3064 && *p == '?') |
428 | 3065 { |
3066 p++; | |
446 | 3067 PATFETCH (c); |
428 | 3068 switch (c) |
3069 { | |
3070 case ':': /* shy groups */ | |
502 | 3071 shy = 1; |
428 | 3072 break; |
3073 | |
3074 /* All others are reserved for future constructs. */ | |
3075 default: | |
3076 FREE_STACK_RETURN (REG_BADPAT); | |
3077 } | |
3078 } | |
502 | 3079 |
3080 r = ++regnum; | |
3081 bufp->re_ngroups++; | |
3082 if (!shy) | |
3083 { | |
3084 bufp->re_nsub++; | |
3085 while (bufp->external_to_internal_register_size <= | |
3086 bufp->re_nsub) | |
3087 { | |
3088 int i; | |
3089 int old_size = | |
3090 bufp->external_to_internal_register_size; | |
3091 bufp->external_to_internal_register_size += 5; | |
3092 RETALLOC (bufp->external_to_internal_register, | |
3093 bufp->external_to_internal_register_size, | |
3094 int); | |
3095 /* debugging */ | |
3096 for (i = old_size; | |
3097 i < bufp->external_to_internal_register_size; i++) | |
3098 bufp->external_to_internal_register[i] = | |
3099 (int) 0xDEADBEEF; | |
3100 } | |
3101 | |
3102 bufp->external_to_internal_register[bufp->re_nsub] = | |
3103 bufp->re_ngroups; | |
3104 } | |
428 | 3105 |
3106 if (COMPILE_STACK_FULL) | |
3107 { | |
3108 RETALLOC (compile_stack.stack, compile_stack.size << 1, | |
3109 compile_stack_elt_t); | |
3110 if (compile_stack.stack == NULL) return REG_ESPACE; | |
3111 | |
3112 compile_stack.size <<= 1; | |
3113 } | |
3114 | |
3115 /* These are the values to restore when we hit end of this | |
3116 group. They are all relative offsets, so that if the | |
3117 whole pattern moves because of realloc, they will still | |
3118 be valid. */ | |
3119 COMPILE_STACK_TOP.begalt_offset = begalt - bufp->buffer; | |
3120 COMPILE_STACK_TOP.fixup_alt_jump | |
3121 = fixup_alt_jump ? fixup_alt_jump - bufp->buffer + 1 : 0; | |
446 | 3122 COMPILE_STACK_TOP.laststart_offset = buf_end - bufp->buffer; |
428 | 3123 COMPILE_STACK_TOP.regnum = r; |
3124 | |
3125 /* We will eventually replace the 0 with the number of | |
3126 groups inner to this one. But do not push a | |
3127 start_memory for groups beyond the last one we can | |
502 | 3128 represent in the compiled pattern. |
3129 #### bad bad bad. this will fail in lots of ways, if we | |
3130 ever have to backtrack for these groups. | |
3131 */ | |
428 | 3132 if (r <= MAX_REGNUM) |
3133 { | |
3134 COMPILE_STACK_TOP.inner_group_offset | |
446 | 3135 = buf_end - bufp->buffer + 2; |
428 | 3136 BUF_PUSH_3 (start_memory, r, 0); |
3137 } | |
3138 | |
3139 compile_stack.avail++; | |
3140 | |
3141 fixup_alt_jump = 0; | |
3142 laststart = 0; | |
446 | 3143 begalt = buf_end; |
428 | 3144 /* If we've reached MAX_REGNUM groups, then this open |
3145 won't actually generate any code, so we'll have to | |
3146 clear pending_exact explicitly. */ | |
3147 pending_exact = 0; | |
3148 } | |
3149 break; | |
3150 | |
3151 | |
3152 case ')': | |
3153 if (syntax & RE_NO_BK_PARENS) goto normal_backslash; | |
3154 | |
3155 if (COMPILE_STACK_EMPTY) { | |
3156 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD) | |
3157 goto normal_backslash; | |
3158 else | |
3159 FREE_STACK_RETURN (REG_ERPAREN); | |
3160 } | |
3161 | |
3162 handle_close: | |
3163 if (fixup_alt_jump) | |
3164 { /* Push a dummy failure point at the end of the | |
3165 alternative for a possible future | |
3166 `pop_failure_jump' to pop. See comments at | |
3167 `push_dummy_failure' in `re_match_2'. */ | |
3168 BUF_PUSH (push_dummy_failure); | |
3169 | |
3170 /* We allocated space for this jump when we assigned | |
3171 to `fixup_alt_jump', in the `handle_alt' case below. */ | |
446 | 3172 STORE_JUMP (jump_past_alt, fixup_alt_jump, buf_end - 1); |
428 | 3173 } |
3174 | |
3175 /* See similar code for backslashed left paren above. */ | |
3176 if (COMPILE_STACK_EMPTY) { | |
3177 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD) | |
3178 goto normal_char; | |
3179 else | |
3180 FREE_STACK_RETURN (REG_ERPAREN); | |
3181 } | |
3182 | |
3183 /* Since we just checked for an empty stack above, this | |
3184 ``can't happen''. */ | |
3185 assert (compile_stack.avail != 0); | |
3186 { | |
3187 /* We don't just want to restore into `regnum', because | |
3188 later groups should continue to be numbered higher, | |
3189 as in `(ab)c(de)' -- the second group is #2. */ | |
3190 regnum_t this_group_regnum; | |
3191 | |
3192 compile_stack.avail--; | |
3193 begalt = bufp->buffer + COMPILE_STACK_TOP.begalt_offset; | |
3194 fixup_alt_jump | |
3195 = COMPILE_STACK_TOP.fixup_alt_jump | |
3196 ? bufp->buffer + COMPILE_STACK_TOP.fixup_alt_jump - 1 | |
3197 : 0; | |
3198 laststart = bufp->buffer + COMPILE_STACK_TOP.laststart_offset; | |
3199 this_group_regnum = COMPILE_STACK_TOP.regnum; | |
3200 /* If we've reached MAX_REGNUM groups, then this open | |
3201 won't actually generate any code, so we'll have to | |
3202 clear pending_exact explicitly. */ | |
3203 pending_exact = 0; | |
3204 | |
3205 /* We're at the end of the group, so now we know how many | |
3206 groups were inside this one. */ | |
3207 if (this_group_regnum <= MAX_REGNUM) | |
3208 { | |
3209 unsigned char *inner_group_loc | |
3210 = bufp->buffer + COMPILE_STACK_TOP.inner_group_offset; | |
3211 | |
3212 *inner_group_loc = regnum - this_group_regnum; | |
3213 BUF_PUSH_3 (stop_memory, this_group_regnum, | |
3214 regnum - this_group_regnum); | |
3215 } | |
3216 } | |
3217 break; | |
3218 | |
3219 | |
3220 case '|': /* `\|'. */ | |
3221 if (syntax & RE_LIMITED_OPS || syntax & RE_NO_BK_VBAR) | |
3222 goto normal_backslash; | |
3223 handle_alt: | |
3224 if (syntax & RE_LIMITED_OPS) | |
3225 goto normal_char; | |
3226 | |
3227 /* Insert before the previous alternative a jump which | |
3228 jumps to this alternative if the former fails. */ | |
3229 GET_BUFFER_SPACE (3); | |
446 | 3230 INSERT_JUMP (on_failure_jump, begalt, buf_end + 6); |
428 | 3231 pending_exact = 0; |
446 | 3232 buf_end += 3; |
428 | 3233 |
3234 /* The alternative before this one has a jump after it | |
3235 which gets executed if it gets matched. Adjust that | |
3236 jump so it will jump to this alternative's analogous | |
3237 jump (put in below, which in turn will jump to the next | |
3238 (if any) alternative's such jump, etc.). The last such | |
3239 jump jumps to the correct final destination. A picture: | |
3240 _____ _____ | |
3241 | | | | | |
3242 | v | v | |
3243 a | b | c | |
3244 | |
3245 If we are at `b', then fixup_alt_jump right now points to a | |
3246 three-byte space after `a'. We'll put in the jump, set | |
3247 fixup_alt_jump to right after `b', and leave behind three | |
3248 bytes which we'll fill in when we get to after `c'. */ | |
3249 | |
3250 if (fixup_alt_jump) | |
446 | 3251 STORE_JUMP (jump_past_alt, fixup_alt_jump, buf_end); |
428 | 3252 |
3253 /* Mark and leave space for a jump after this alternative, | |
3254 to be filled in later either by next alternative or | |
3255 when know we're at the end of a series of alternatives. */ | |
446 | 3256 fixup_alt_jump = buf_end; |
428 | 3257 GET_BUFFER_SPACE (3); |
446 | 3258 buf_end += 3; |
428 | 3259 |
3260 laststart = 0; | |
446 | 3261 begalt = buf_end; |
428 | 3262 break; |
3263 | |
3264 | |
3265 case '{': | |
3266 /* If \{ is a literal. */ | |
3267 if (!(syntax & RE_INTERVALS) | |
3268 /* If we're at `\{' and it's not the open-interval | |
3269 operator. */ | |
3270 || ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES)) | |
3271 || (p - 2 == pattern && p == pend)) | |
3272 goto normal_backslash; | |
3273 | |
3274 handle_interval: | |
3275 { | |
3276 /* If got here, then the syntax allows intervals. */ | |
3277 | |
3278 /* At least (most) this many matches must be made. */ | |
3279 int lower_bound = -1, upper_bound = -1; | |
3280 | |
3281 beg_interval = p - 1; | |
3282 | |
3283 if (p == pend) | |
3284 { | |
3285 if (syntax & RE_NO_BK_BRACES) | |
3286 goto unfetch_interval; | |
3287 else | |
3288 FREE_STACK_RETURN (REG_EBRACE); | |
3289 } | |
3290 | |
3291 GET_UNSIGNED_NUMBER (lower_bound); | |
3292 | |
3293 if (c == ',') | |
3294 { | |
3295 GET_UNSIGNED_NUMBER (upper_bound); | |
3296 if (upper_bound < 0) upper_bound = RE_DUP_MAX; | |
3297 } | |
3298 else | |
3299 /* Interval such as `{1}' => match exactly once. */ | |
3300 upper_bound = lower_bound; | |
3301 | |
3302 if (lower_bound < 0 || upper_bound > RE_DUP_MAX | |
3303 || lower_bound > upper_bound) | |
3304 { | |
3305 if (syntax & RE_NO_BK_BRACES) | |
3306 goto unfetch_interval; | |
3307 else | |
3308 FREE_STACK_RETURN (REG_BADBR); | |
3309 } | |
3310 | |
3311 if (!(syntax & RE_NO_BK_BRACES)) | |
3312 { | |
3313 if (c != '\\') FREE_STACK_RETURN (REG_EBRACE); | |
3314 | |
3315 PATFETCH (c); | |
3316 } | |
3317 | |
3318 if (c != '}') | |
3319 { | |
3320 if (syntax & RE_NO_BK_BRACES) | |
3321 goto unfetch_interval; | |
3322 else | |
3323 FREE_STACK_RETURN (REG_BADBR); | |
3324 } | |
3325 | |
3326 /* We just parsed a valid interval. */ | |
3327 | |
3328 /* If it's invalid to have no preceding re. */ | |
3329 if (!laststart) | |
3330 { | |
3331 if (syntax & RE_CONTEXT_INVALID_OPS) | |
3332 FREE_STACK_RETURN (REG_BADRPT); | |
3333 else if (syntax & RE_CONTEXT_INDEP_OPS) | |
446 | 3334 laststart = buf_end; |
428 | 3335 else |
3336 goto unfetch_interval; | |
3337 } | |
3338 | |
3339 /* If the upper bound is zero, don't want to succeed at | |
3340 all; jump from `laststart' to `b + 3', which will be | |
3341 the end of the buffer after we insert the jump. */ | |
3342 if (upper_bound == 0) | |
3343 { | |
3344 GET_BUFFER_SPACE (3); | |
446 | 3345 INSERT_JUMP (jump, laststart, buf_end + 3); |
3346 buf_end += 3; | |
428 | 3347 } |
3348 | |
3349 /* Otherwise, we have a nontrivial interval. When | |
3350 we're all done, the pattern will look like: | |
3351 set_number_at <jump count> <upper bound> | |
3352 set_number_at <succeed_n count> <lower bound> | |
3353 succeed_n <after jump addr> <succeed_n count> | |
3354 <body of loop> | |
3355 jump_n <succeed_n addr> <jump count> | |
3356 (The upper bound and `jump_n' are omitted if | |
3357 `upper_bound' is 1, though.) */ | |
3358 else | |
3359 { /* If the upper bound is > 1, we need to insert | |
3360 more at the end of the loop. */ | |
647 | 3361 int nbytes = 10 + (upper_bound > 1) * 10; |
428 | 3362 |
3363 GET_BUFFER_SPACE (nbytes); | |
3364 | |
3365 /* Initialize lower bound of the `succeed_n', even | |
3366 though it will be set during matching by its | |
3367 attendant `set_number_at' (inserted next), | |
3368 because `re_compile_fastmap' needs to know. | |
3369 Jump to the `jump_n' we might insert below. */ | |
3370 INSERT_JUMP2 (succeed_n, laststart, | |
446 | 3371 buf_end + 5 + (upper_bound > 1) * 5, |
428 | 3372 lower_bound); |
446 | 3373 buf_end += 5; |
428 | 3374 |
3375 /* Code to initialize the lower bound. Insert | |
3376 before the `succeed_n'. The `5' is the last two | |
3377 bytes of this `set_number_at', plus 3 bytes of | |
3378 the following `succeed_n'. */ | |
446 | 3379 insert_op2 (set_number_at, laststart, 5, lower_bound, buf_end); |
3380 buf_end += 5; | |
428 | 3381 |
3382 if (upper_bound > 1) | |
3383 { /* More than one repetition is allowed, so | |
3384 append a backward jump to the `succeed_n' | |
3385 that starts this interval. | |
3386 | |
3387 When we've reached this during matching, | |
3388 we'll have matched the interval once, so | |
3389 jump back only `upper_bound - 1' times. */ | |
446 | 3390 STORE_JUMP2 (jump_n, buf_end, laststart + 5, |
428 | 3391 upper_bound - 1); |
446 | 3392 buf_end += 5; |
428 | 3393 |
3394 /* The location we want to set is the second | |
3395 parameter of the `jump_n'; that is `b-2' as | |
3396 an absolute address. `laststart' will be | |
3397 the `set_number_at' we're about to insert; | |
3398 `laststart+3' the number to set, the source | |
3399 for the relative address. But we are | |
3400 inserting into the middle of the pattern -- | |
3401 so everything is getting moved up by 5. | |
3402 Conclusion: (b - 2) - (laststart + 3) + 5, | |
3403 i.e., b - laststart. | |
3404 | |
3405 We insert this at the beginning of the loop | |
3406 so that if we fail during matching, we'll | |
3407 reinitialize the bounds. */ | |
446 | 3408 insert_op2 (set_number_at, laststart, |
3409 buf_end - laststart, | |
3410 upper_bound - 1, buf_end); | |
3411 buf_end += 5; | |
428 | 3412 } |
3413 } | |
3414 pending_exact = 0; | |
3415 beg_interval = NULL; | |
3416 } | |
3417 break; | |
3418 | |
3419 unfetch_interval: | |
3420 /* If an invalid interval, match the characters as literals. */ | |
3421 assert (beg_interval); | |
3422 p = beg_interval; | |
3423 beg_interval = NULL; | |
3424 | |
3425 /* normal_char and normal_backslash need `c'. */ | |
3426 PATFETCH (c); | |
3427 | |
3428 if (!(syntax & RE_NO_BK_BRACES)) | |
3429 { | |
3430 if (p > pattern && p[-1] == '\\') | |
3431 goto normal_backslash; | |
3432 } | |
3433 goto normal_char; | |
3434 | |
3435 #ifdef emacs | |
3436 /* There is no way to specify the before_dot and after_dot | |
3437 operators. rms says this is ok. --karl */ | |
3438 case '=': | |
3439 BUF_PUSH (at_dot); | |
3440 break; | |
3441 | |
3442 case 's': | |
446 | 3443 laststart = buf_end; |
428 | 3444 PATFETCH (c); |
3445 /* XEmacs addition */ | |
3446 if (c >= 0x80 || syntax_spec_code[c] == 0377) | |
3447 FREE_STACK_RETURN (REG_ESYNTAX); | |
3448 BUF_PUSH_2 (syntaxspec, syntax_spec_code[c]); | |
3449 break; | |
3450 | |
3451 case 'S': | |
446 | 3452 laststart = buf_end; |
428 | 3453 PATFETCH (c); |
3454 /* XEmacs addition */ | |
3455 if (c >= 0x80 || syntax_spec_code[c] == 0377) | |
3456 FREE_STACK_RETURN (REG_ESYNTAX); | |
3457 BUF_PUSH_2 (notsyntaxspec, syntax_spec_code[c]); | |
3458 break; | |
3459 | |
3460 #ifdef MULE | |
3461 /* 97.2.17 jhod merged in to XEmacs from mule-2.3 */ | |
3462 case 'c': | |
446 | 3463 laststart = buf_end; |
428 | 3464 PATFETCH_RAW (c); |
3465 if (c < 32 || c > 127) | |
3466 FREE_STACK_RETURN (REG_ECATEGORY); | |
3467 BUF_PUSH_2 (categoryspec, c); | |
3468 break; | |
3469 | |
3470 case 'C': | |
446 | 3471 laststart = buf_end; |
428 | 3472 PATFETCH_RAW (c); |
3473 if (c < 32 || c > 127) | |
3474 FREE_STACK_RETURN (REG_ECATEGORY); | |
3475 BUF_PUSH_2 (notcategoryspec, c); | |
3476 break; | |
3477 /* end of category patch */ | |
3478 #endif /* MULE */ | |
3479 #endif /* emacs */ | |
3480 | |
3481 | |
3482 case 'w': | |
446 | 3483 laststart = buf_end; |
428 | 3484 BUF_PUSH (wordchar); |
3485 break; | |
3486 | |
3487 | |
3488 case 'W': | |
446 | 3489 laststart = buf_end; |
428 | 3490 BUF_PUSH (notwordchar); |
3491 break; | |
3492 | |
3493 | |
3494 case '<': | |
3495 BUF_PUSH (wordbeg); | |
3496 break; | |
3497 | |
3498 case '>': | |
3499 BUF_PUSH (wordend); | |
3500 break; | |
3501 | |
3502 case 'b': | |
3503 BUF_PUSH (wordbound); | |
3504 break; | |
3505 | |
3506 case 'B': | |
3507 BUF_PUSH (notwordbound); | |
3508 break; | |
3509 | |
3510 case '`': | |
3511 BUF_PUSH (begbuf); | |
3512 break; | |
3513 | |
3514 case '\'': | |
3515 BUF_PUSH (endbuf); | |
3516 break; | |
3517 | |
3518 case '1': case '2': case '3': case '4': case '5': | |
3519 case '6': case '7': case '8': case '9': | |
446 | 3520 { |
502 | 3521 regnum_t reg, regint; |
3522 int may_need_to_unfetch = 0; | |
446 | 3523 if (syntax & RE_NO_BK_REFS) |
3524 goto normal_char; | |
3525 | |
502 | 3526 /* This only goes up to 99. It could be extended to work |
3527 up to 255 (the maximum number of registers that can be | |
3528 handled by the current regexp engine, because it stores | |
3529 its register numbers in the compiled pattern as one byte, | |
3530 ugh). Doing that's a bit trickier, because you might | |
3531 have the case where \25 a back-ref but \255 is not, ... */ | |
446 | 3532 reg = c - '0'; |
502 | 3533 if (p < pend) |
3534 { | |
3535 PATFETCH (c); | |
3536 if (c >= '0' && c <= '9') | |
3537 { | |
3538 regnum_t new_reg = reg * 10 + c - '0'; | |
3539 if (new_reg <= bufp->re_nsub) | |
3540 { | |
3541 reg = new_reg; | |
3542 may_need_to_unfetch = 1; | |
3543 } | |
3544 else | |
3545 PATUNFETCH; | |
3546 } | |
523 | 3547 else |
3548 PATUNFETCH; | |
502 | 3549 } |
3550 | |
3551 if (reg > bufp->re_nsub) | |
446 | 3552 FREE_STACK_RETURN (REG_ESUBREG); |
3553 | |
502 | 3554 regint = bufp->external_to_internal_register[reg]; |
446 | 3555 /* Can't back reference to a subexpression if inside of it. */ |
502 | 3556 if (group_in_compile_stack (compile_stack, regint)) |
3557 { | |
3558 if (may_need_to_unfetch) | |
3559 PATUNFETCH; | |
3560 goto normal_char; | |
3561 } | |
3562 | |
3563 #ifdef emacs | |
3564 if (reg > 9 && | |
3565 bufp->warned_about_incompatible_back_references == 0) | |
3566 { | |
3567 bufp->warned_about_incompatible_back_references = 1; | |
3568 warn_when_safe (intern ("regex"), Qinfo, | |
3569 "Back reference \\%d now has new " | |
3570 "semantics in %s", reg, pattern); | |
3571 } | |
3572 #endif | |
446 | 3573 |
3574 laststart = buf_end; | |
502 | 3575 BUF_PUSH_2 (duplicate, regint); |
446 | 3576 } |
428 | 3577 break; |
3578 | |
3579 | |
3580 case '+': | |
3581 case '?': | |
3582 if (syntax & RE_BK_PLUS_QM) | |
3583 goto handle_plus; | |
3584 else | |
3585 goto normal_backslash; | |
3586 | |
3587 default: | |
3588 normal_backslash: | |
3589 /* You might think it would be useful for \ to mean | |
3590 not to translate; but if we don't translate it, | |
3591 it will never match anything. */ | |
826 | 3592 c = RE_TRANSLATE (c); |
428 | 3593 goto normal_char; |
3594 } | |
3595 break; | |
3596 | |
3597 | |
3598 default: | |
3599 /* Expects the character in `c'. */ | |
3600 /* `p' points to the location after where `c' came from. */ | |
3601 normal_char: | |
3602 { | |
4750
b5f21bb36684
Fix crash in regex.c (closes issue630).
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4527
diff
changeset
|
3603 /* The following conditional synced to GNU Emacs 22.1. */ |
428 | 3604 /* If no exactn currently being built. */ |
3605 if (!pending_exact | |
3606 | |
3607 /* If last exactn not at current position. */ | |
446 | 3608 || pending_exact + *pending_exact + 1 != buf_end |
428 | 3609 |
3610 /* We have only one byte following the exactn for the count. */ | |
4750
b5f21bb36684
Fix crash in regex.c (closes issue630).
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4527
diff
changeset
|
3611 || *pending_exact >= (1 << BYTEWIDTH) - MAX_ICHAR_LEN |
b5f21bb36684
Fix crash in regex.c (closes issue630).
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4527
diff
changeset
|
3612 |
b5f21bb36684
Fix crash in regex.c (closes issue630).
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4527
diff
changeset
|
3613 /* If followed by a repetition operator. |
b5f21bb36684
Fix crash in regex.c (closes issue630).
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4527
diff
changeset
|
3614 If the lookahead fails because of end of pattern, any |
b5f21bb36684
Fix crash in regex.c (closes issue630).
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4527
diff
changeset
|
3615 trailing backslash will get caught later. */ |
b5f21bb36684
Fix crash in regex.c (closes issue630).
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4527
diff
changeset
|
3616 || (p != pend && (*p == '*' || *p == '^')) |
428 | 3617 || ((syntax & RE_BK_PLUS_QM) |
4750
b5f21bb36684
Fix crash in regex.c (closes issue630).
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4527
diff
changeset
|
3618 ? p + 1 < pend && *p == '\\' && (p[1] == '+' || p[1] == '?') |
b5f21bb36684
Fix crash in regex.c (closes issue630).
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4527
diff
changeset
|
3619 : p != pend && (*p == '+' || *p == '?')) |
428 | 3620 || ((syntax & RE_INTERVALS) |
3621 && ((syntax & RE_NO_BK_BRACES) | |
4750
b5f21bb36684
Fix crash in regex.c (closes issue630).
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4527
diff
changeset
|
3622 ? p != pend && *p == '{' |
b5f21bb36684
Fix crash in regex.c (closes issue630).
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4527
diff
changeset
|
3623 : p + 1 < pend && (p[0] == '\\' && p[1] == '{')))) |
428 | 3624 { |
3625 /* Start building a new exactn. */ | |
3626 | |
446 | 3627 laststart = buf_end; |
428 | 3628 |
3629 BUF_PUSH_2 (exactn, 0); | |
446 | 3630 pending_exact = buf_end - 1; |
428 | 3631 } |
3632 | |
446 | 3633 #ifndef MULE |
428 | 3634 BUF_PUSH (c); |
3635 (*pending_exact)++; | |
446 | 3636 #else |
3637 { | |
3638 Bytecount bt_count; | |
867 | 3639 Ibyte tmp_buf[MAX_ICHAR_LEN]; |
446 | 3640 int i; |
3641 | |
867 | 3642 bt_count = set_itext_ichar (tmp_buf, c); |
446 | 3643 |
3644 for (i = 0; i < bt_count; i++) | |
3645 { | |
3646 BUF_PUSH (tmp_buf[i]); | |
3647 (*pending_exact)++; | |
3648 } | |
3649 } | |
3650 #endif | |
428 | 3651 break; |
3652 } | |
3653 } /* switch (c) */ | |
3654 } /* while p != pend */ | |
3655 | |
3656 | |
3657 /* Through the pattern now. */ | |
3658 | |
3659 if (fixup_alt_jump) | |
446 | 3660 STORE_JUMP (jump_past_alt, fixup_alt_jump, buf_end); |
428 | 3661 |
3662 if (!COMPILE_STACK_EMPTY) | |
3663 FREE_STACK_RETURN (REG_EPAREN); | |
3664 | |
3665 /* If we don't want backtracking, force success | |
3666 the first time we reach the end of the compiled pattern. */ | |
3667 if (syntax & RE_NO_POSIX_BACKTRACKING) | |
3668 BUF_PUSH (succeed); | |
3669 | |
4976
16112448d484
Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents:
4832
diff
changeset
|
3670 xfree (compile_stack.stack); |
428 | 3671 |
3672 /* We have succeeded; set the length of the buffer. */ | |
446 | 3673 bufp->used = buf_end - bufp->buffer; |
428 | 3674 |
3675 #ifdef DEBUG | |
5041 | 3676 if (debug_regexps & RE_DEBUG_COMPILATION) |
428 | 3677 { |
3678 DEBUG_PRINT1 ("\nCompiled pattern: \n"); | |
3679 print_compiled_pattern (bufp); | |
3680 } | |
3681 #endif /* DEBUG */ | |
3682 | |
3683 #ifndef MATCH_MAY_ALLOCATE | |
3684 /* Initialize the failure stack to the largest possible stack. This | |
3685 isn't necessary unless we're trying to avoid calling alloca in | |
3686 the search and match routines. */ | |
3687 { | |
502 | 3688 int num_regs = bufp->re_ngroups + 1; |
428 | 3689 |
3690 /* Since DOUBLE_FAIL_STACK refuses to double only if the current size | |
3691 is strictly greater than re_max_failures, the largest possible stack | |
3692 is 2 * re_max_failures failure points. */ | |
3693 if (fail_stack.size < (2 * re_max_failures * MAX_FAILURE_ITEMS)) | |
3694 { | |
3695 fail_stack.size = (2 * re_max_failures * MAX_FAILURE_ITEMS); | |
3696 | |
3697 if (! fail_stack.stack) | |
3698 fail_stack.stack | |
3699 = (fail_stack_elt_t *) xmalloc (fail_stack.size | |
3700 * sizeof (fail_stack_elt_t)); | |
3701 else | |
3702 fail_stack.stack | |
3703 = (fail_stack_elt_t *) xrealloc (fail_stack.stack, | |
3704 (fail_stack.size | |
3705 * sizeof (fail_stack_elt_t))); | |
3706 } | |
3707 | |
3708 regex_grow_registers (num_regs); | |
3709 } | |
3710 #endif /* not MATCH_MAY_ALLOCATE */ | |
3711 | |
3712 return REG_NOERROR; | |
3713 } /* regex_compile */ | |
3714 | |
3715 /* Subroutines for `regex_compile'. */ | |
3716 | |
3717 /* Store OP at LOC followed by two-byte integer parameter ARG. */ | |
3718 | |
3719 static void | |
3720 store_op1 (re_opcode_t op, unsigned char *loc, int arg) | |
3721 { | |
3722 *loc = (unsigned char) op; | |
3723 STORE_NUMBER (loc + 1, arg); | |
3724 } | |
3725 | |
3726 | |
3727 /* Like `store_op1', but for two two-byte parameters ARG1 and ARG2. */ | |
3728 | |
3729 static void | |
3730 store_op2 (re_opcode_t op, unsigned char *loc, int arg1, int arg2) | |
3731 { | |
3732 *loc = (unsigned char) op; | |
3733 STORE_NUMBER (loc + 1, arg1); | |
3734 STORE_NUMBER (loc + 3, arg2); | |
3735 } | |
3736 | |
3737 | |
3738 /* Copy the bytes from LOC to END to open up three bytes of space at LOC | |
3739 for OP followed by two-byte integer parameter ARG. */ | |
3740 | |
3741 static void | |
3742 insert_op1 (re_opcode_t op, unsigned char *loc, int arg, unsigned char *end) | |
3743 { | |
3744 REGISTER unsigned char *pfrom = end; | |
3745 REGISTER unsigned char *pto = end + 3; | |
3746 | |
3747 while (pfrom != loc) | |
3748 *--pto = *--pfrom; | |
3749 | |
3750 store_op1 (op, loc, arg); | |
3751 } | |
3752 | |
3753 | |
3754 /* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2. */ | |
3755 | |
3756 static void | |
3757 insert_op2 (re_opcode_t op, unsigned char *loc, int arg1, int arg2, | |
3758 unsigned char *end) | |
3759 { | |
3760 REGISTER unsigned char *pfrom = end; | |
3761 REGISTER unsigned char *pto = end + 5; | |
3762 | |
3763 while (pfrom != loc) | |
3764 *--pto = *--pfrom; | |
3765 | |
3766 store_op2 (op, loc, arg1, arg2); | |
3767 } | |
3768 | |
3769 | |
3770 /* P points to just after a ^ in PATTERN. Return true if that ^ comes | |
3771 after an alternative or a begin-subexpression. We assume there is at | |
3772 least one character before the ^. */ | |
3773 | |
460 | 3774 static re_bool |
446 | 3775 at_begline_loc_p (re_char *pattern, re_char *p, reg_syntax_t syntax) |
428 | 3776 { |
446 | 3777 re_char *prev = p - 2; |
460 | 3778 re_bool prev_prev_backslash = prev > pattern && prev[-1] == '\\'; |
428 | 3779 |
3780 return | |
3781 /* After a subexpression? */ | |
3782 (*prev == '(' && (syntax & RE_NO_BK_PARENS || prev_prev_backslash)) | |
3783 /* After an alternative? */ | |
3784 || (*prev == '|' && (syntax & RE_NO_BK_VBAR || prev_prev_backslash)); | |
3785 } | |
3786 | |
3787 | |
3788 /* The dual of at_begline_loc_p. This one is for $. We assume there is | |
3789 at least one character after the $, i.e., `P < PEND'. */ | |
3790 | |
460 | 3791 static re_bool |
446 | 3792 at_endline_loc_p (re_char *p, re_char *pend, int syntax) |
428 | 3793 { |
446 | 3794 re_char *next = p; |
460 | 3795 re_bool next_backslash = *next == '\\'; |
446 | 3796 re_char *next_next = p + 1 < pend ? p + 1 : 0; |
428 | 3797 |
3798 return | |
3799 /* Before a subexpression? */ | |
3800 (syntax & RE_NO_BK_PARENS ? *next == ')' | |
3801 : next_backslash && next_next && *next_next == ')') | |
3802 /* Before an alternative? */ | |
3803 || (syntax & RE_NO_BK_VBAR ? *next == '|' | |
3804 : next_backslash && next_next && *next_next == '|'); | |
3805 } | |
3806 | |
3807 | |
3808 /* Returns true if REGNUM is in one of COMPILE_STACK's elements and | |
3809 false if it's not. */ | |
3810 | |
460 | 3811 static re_bool |
428 | 3812 group_in_compile_stack (compile_stack_type compile_stack, regnum_t regnum) |
3813 { | |
3814 int this_element; | |
3815 | |
3816 for (this_element = compile_stack.avail - 1; | |
3817 this_element >= 0; | |
3818 this_element--) | |
3819 if (compile_stack.stack[this_element].regnum == regnum) | |
3820 return true; | |
3821 | |
3822 return false; | |
3823 } | |
3824 | |
3825 | |
3826 /* Read the ending character of a range (in a bracket expression) from the | |
3827 uncompiled pattern *P_PTR (which ends at PEND). We assume the | |
3828 starting character is in `P[-2]'. (`P[-1]' is the character `-'.) | |
3829 Then we set the translation of all bits between the starting and | |
3830 ending characters (inclusive) in the compiled pattern B. | |
3831 | |
3832 Return an error code. | |
3833 | |
3834 We use these short variable names so we can use the same macros as | |
826 | 3835 `regex_compile' itself. |
3836 | |
3837 Under Mule, this is only called when both chars of the range are | |
3838 ASCII. */ | |
428 | 3839 |
3840 static reg_errcode_t | |
446 | 3841 compile_range (re_char **p_ptr, re_char *pend, RE_TRANSLATE_TYPE translate, |
3842 reg_syntax_t syntax, unsigned char *buf_end) | |
428 | 3843 { |
867 | 3844 Ichar this_char; |
428 | 3845 |
446 | 3846 re_char *p = *p_ptr; |
428 | 3847 int range_start, range_end; |
3848 | |
3849 if (p == pend) | |
3850 return REG_ERANGE; | |
3851 | |
3852 /* Even though the pattern is a signed `char *', we need to fetch | |
3853 with unsigned char *'s; if the high bit of the pattern character | |
3854 is set, the range endpoints will be negative if we fetch using a | |
3855 signed char *. | |
3856 | |
3857 We also want to fetch the endpoints without translating them; the | |
3858 appropriate translation is done in the bit-setting loop below. */ | |
442 | 3859 /* The SVR4 compiler on the 3B2 had trouble with unsigned const char *. */ |
3860 range_start = ((const unsigned char *) p)[-2]; | |
3861 range_end = ((const unsigned char *) p)[0]; | |
428 | 3862 |
3863 /* Have to increment the pointer into the pattern string, so the | |
3864 caller isn't still at the ending character. */ | |
3865 (*p_ptr)++; | |
3866 | |
3867 /* If the start is after the end, the range is empty. */ | |
3868 if (range_start > range_end) | |
3869 return syntax & RE_NO_EMPTY_RANGES ? REG_ERANGE : REG_NOERROR; | |
3870 | |
3871 /* Here we see why `this_char' has to be larger than an `unsigned | |
3872 char' -- the range is inclusive, so if `range_end' == 0xff | |
3873 (assuming 8-bit characters), we would otherwise go into an infinite | |
3874 loop, since all characters <= 0xff. */ | |
3875 for (this_char = range_start; this_char <= range_end; this_char++) | |
3876 { | |
826 | 3877 SET_LIST_BIT (RE_TRANSLATE (this_char)); |
428 | 3878 } |
3879 | |
3880 return REG_NOERROR; | |
3881 } | |
3882 | |
3883 #ifdef MULE | |
3884 | |
3885 static reg_errcode_t | |
446 | 3886 compile_extended_range (re_char **p_ptr, re_char *pend, |
3887 RE_TRANSLATE_TYPE translate, | |
428 | 3888 reg_syntax_t syntax, Lisp_Object rtab) |
3889 { | |
867 | 3890 Ichar this_char, range_start, range_end; |
3891 const Ibyte *p; | |
428 | 3892 |
3893 if (*p_ptr == pend) | |
3894 return REG_ERANGE; | |
3895 | |
867 | 3896 p = (const Ibyte *) *p_ptr; |
3897 range_end = itext_ichar (p); | |
428 | 3898 p--; /* back to '-' */ |
867 | 3899 DEC_IBYTEPTR (p); /* back to start of range */ |
428 | 3900 /* We also want to fetch the endpoints without translating them; the |
3901 appropriate translation is done in the bit-setting loop below. */ | |
867 | 3902 range_start = itext_ichar (p); |
3903 INC_IBYTEPTR (*p_ptr); | |
428 | 3904 |
3905 /* If the start is after the end, the range is empty. */ | |
3906 if (range_start > range_end) | |
3907 return syntax & RE_NO_EMPTY_RANGES ? REG_ERANGE : REG_NOERROR; | |
3908 | |
3909 /* Can't have ranges spanning different charsets, except maybe for | |
3910 ranges entirely within the first 256 chars. */ | |
3911 | |
3912 if ((range_start >= 0x100 || range_end >= 0x100) | |
867 | 3913 && ichar_leading_byte (range_start) != |
3914 ichar_leading_byte (range_end)) | |
428 | 3915 return REG_ERANGESPAN; |
3916 | |
826 | 3917 /* #### This might be way inefficient if the range encompasses 10,000 |
3918 chars or something. To be efficient, you'd have to do something like | |
3919 this: | |
428 | 3920 |
3921 range_table a; | |
3922 range_table b; | |
3923 map over translation table in [range_start, range_end] of | |
3924 (put the mapped range in a; | |
3925 put the translation in b) | |
3926 invert the range in a and truncate to [range_start, range_end] | |
3927 compute the union of a, b | |
3928 union the result into rtab | |
3929 */ | |
826 | 3930 for (this_char = range_start; this_char <= range_end; this_char++) |
428 | 3931 { |
826 | 3932 SET_RANGETAB_BIT (RE_TRANSLATE (this_char)); |
428 | 3933 } |
3934 | |
3935 if (this_char <= range_end) | |
3936 put_range_table (rtab, this_char, range_end, Qt); | |
3937 | |
3938 return REG_NOERROR; | |
3939 } | |
3940 | |
5648
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3941 static reg_errcode_t |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3942 compile_char_class (re_wctype_t cc, Lisp_Object rtab, Bitbyte *flags_out) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3943 { |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3944 *flags_out |= re_wctype_to_bit (cc); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3945 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3946 switch (cc) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3947 { |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3948 case RECC_ASCII: |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3949 put_range_table (rtab, 0, 0x7f, Qt); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3950 break; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3951 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3952 case RECC_XDIGIT: |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3953 put_range_table (rtab, 'a', 'f', Qt); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3954 put_range_table (rtab, 'A', 'f', Qt); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3955 /* fallthrough */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3956 case RECC_DIGIT: |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3957 put_range_table (rtab, '0', '9', Qt); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3958 break; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3959 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3960 case RECC_BLANK: |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3961 put_range_table (rtab, ' ', ' ', Qt); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3962 put_range_table (rtab, '\t', '\t', Qt); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3963 break; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3964 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3965 case RECC_PRINT: |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3966 put_range_table (rtab, ' ', 0x7e, Qt); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3967 put_range_table (rtab, 0x80, MOST_POSITIVE_FIXNUM, Qt); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3968 break; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3969 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3970 case RECC_GRAPH: |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3971 put_range_table (rtab, '!', 0x7e, Qt); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3972 put_range_table (rtab, 0x80, MOST_POSITIVE_FIXNUM, Qt); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3973 break; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3974 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3975 case RECC_NONASCII: |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3976 case RECC_MULTIBYTE: |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3977 put_range_table (rtab, 0x80, MOST_POSITIVE_FIXNUM, Qt); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3978 break; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3979 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3980 case RECC_CNTRL: |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3981 put_range_table (rtab, 0x00, 0x1f, Qt); |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3982 break; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3983 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3984 case RECC_UNIBYTE: |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3985 /* Never true in XEmacs. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3986 break; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3987 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3988 /* The following all have their own bits in the class_bits argument to |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3989 charset_mule and charset_mule_not, they don't use the range table |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3990 information. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3991 case RECC_ALPHA: |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3992 case RECC_WORD: |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3993 case RECC_ALNUM: /* Equivalent to RECC_WORD */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3994 case RECC_LOWER: |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3995 case RECC_PUNCT: |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3996 case RECC_SPACE: |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3997 case RECC_UPPER: |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3998 break; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
3999 } |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
4000 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
4001 return REG_NOERROR; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
4002 } |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
4003 |
428 | 4004 #endif /* MULE */ |
4005 | |
4006 /* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in | |
4007 BUFP. A fastmap records which of the (1 << BYTEWIDTH) possible | |
4008 characters can start a string that matches the pattern. This fastmap | |
4009 is used by re_search to skip quickly over impossible starting points. | |
4010 | |
4011 The caller must supply the address of a (1 << BYTEWIDTH)-byte data | |
4012 area as BUFP->fastmap. | |
4013 | |
4014 We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in | |
4015 the pattern buffer. | |
4016 | |
4017 Returns 0 if we succeed, -2 if an internal error. */ | |
4018 | |
4019 int | |
826 | 4020 re_compile_fastmap (struct re_pattern_buffer *bufp |
4021 RE_LISP_SHORT_CONTEXT_ARGS_DECL) | |
428 | 4022 { |
4023 int j, k; | |
4024 #ifdef MATCH_MAY_ALLOCATE | |
4025 fail_stack_type fail_stack; | |
4026 #endif | |
456 | 4027 DECLARE_DESTINATION; |
428 | 4028 /* We don't push any register information onto the failure stack. */ |
4029 | |
826 | 4030 /* &&#### this should be changed for 8-bit-fixed, for efficiency. see |
4031 comment marked with &&#### in re_search_2. */ | |
4032 | |
428 | 4033 REGISTER char *fastmap = bufp->fastmap; |
4034 unsigned char *pattern = bufp->buffer; | |
647 | 4035 long size = bufp->used; |
428 | 4036 unsigned char *p = pattern; |
4037 REGISTER unsigned char *pend = pattern + size; | |
4038 | |
771 | 4039 #ifdef REGEX_REL_ALLOC |
428 | 4040 /* This holds the pointer to the failure stack, when |
4041 it is allocated relocatably. */ | |
4042 fail_stack_elt_t *failure_stack_ptr; | |
4043 #endif | |
4044 | |
4045 /* Assume that each path through the pattern can be null until | |
4046 proven otherwise. We set this false at the bottom of switch | |
4047 statement, to which we get only if a particular path doesn't | |
4048 match the empty string. */ | |
460 | 4049 re_bool path_can_be_null = true; |
428 | 4050 |
4051 /* We aren't doing a `succeed_n' to begin with. */ | |
460 | 4052 re_bool succeed_n_p = false; |
428 | 4053 |
1333 | 4054 #ifdef ERROR_CHECK_MALLOC |
4055 /* The pattern comes from string data, not buffer data. We don't access | |
4056 any buffer data, so we don't have to worry about malloc() (but the | |
4057 disallowed flag may have been set by a caller). */ | |
4058 int depth = bind_regex_malloc_disallowed (0); | |
4059 #endif | |
4060 | |
428 | 4061 assert (fastmap != NULL && p != NULL); |
4062 | |
4063 INIT_FAIL_STACK (); | |
4064 memset (fastmap, 0, 1 << BYTEWIDTH); /* Assume nothing's valid. */ | |
4065 bufp->fastmap_accurate = 1; /* It will be when we're done. */ | |
4066 bufp->can_be_null = 0; | |
4067 | |
4068 while (1) | |
4069 { | |
4070 if (p == pend || *p == succeed) | |
4071 { | |
4072 /* We have reached the (effective) end of pattern. */ | |
4073 if (!FAIL_STACK_EMPTY ()) | |
4074 { | |
4075 bufp->can_be_null |= path_can_be_null; | |
4076 | |
4077 /* Reset for next path. */ | |
4078 path_can_be_null = true; | |
4079 | |
446 | 4080 p = (unsigned char *) fail_stack.stack[--fail_stack.avail].pointer; |
428 | 4081 |
4082 continue; | |
4083 } | |
4084 else | |
4085 break; | |
4086 } | |
4087 | |
4088 /* We should never be about to go beyond the end of the pattern. */ | |
4089 assert (p < pend); | |
4090 | |
4759
aa5ed11f473b
Remove support for obsolete systems. See xemacs-patches message with ID
Jerry James <james@xemacs.org>
parents:
4750
diff
changeset
|
4091 switch ((re_opcode_t) *p++) |
428 | 4092 { |
4093 | |
4094 /* I guess the idea here is to simply not bother with a fastmap | |
4095 if a backreference is used, since it's too hard to figure out | |
4096 the fastmap for the corresponding group. Setting | |
4097 `can_be_null' stops `re_search_2' from using the fastmap, so | |
4098 that is all we do. */ | |
4099 case duplicate: | |
4100 bufp->can_be_null = 1; | |
4101 goto done; | |
4102 | |
4103 | |
4104 /* Following are the cases which match a character. These end | |
4105 with `break'. */ | |
4106 | |
4107 case exactn: | |
4108 fastmap[p[1]] = 1; | |
4109 break; | |
4110 | |
4111 | |
4112 case charset: | |
4113 /* XEmacs: Under Mule, these bit vectors will | |
4114 only contain values for characters below 0x80. */ | |
4115 for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--) | |
4116 if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))) | |
4117 fastmap[j] = 1; | |
4118 break; | |
4119 | |
4120 | |
4121 case charset_not: | |
4122 /* Chars beyond end of map must be allowed. */ | |
4123 #ifdef MULE | |
4124 for (j = *p * BYTEWIDTH; j < 0x80; j++) | |
4125 fastmap[j] = 1; | |
4126 /* And all extended characters must be allowed, too. */ | |
4127 for (j = 0x80; j < 0xA0; j++) | |
4128 fastmap[j] = 1; | |
446 | 4129 #else /* not MULE */ |
428 | 4130 for (j = *p * BYTEWIDTH; j < (1 << BYTEWIDTH); j++) |
4131 fastmap[j] = 1; | |
446 | 4132 #endif /* MULE */ |
428 | 4133 |
4134 for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--) | |
4135 if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))) | |
4136 fastmap[j] = 1; | |
4137 break; | |
4138 | |
4139 #ifdef MULE | |
4140 case charset_mule: | |
4141 { | |
4142 int nentries; | |
4143 int i; | |
5648
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
4144 Bitbyte flags = *p++; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
4145 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
4146 if (flags) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
4147 { |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
4148 /* We need to consult the syntax table, fastmap won't |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
4149 work. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
4150 bufp->can_be_null = 1; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
4151 goto done; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
4152 } |
428 | 4153 |
4154 nentries = unified_range_table_nentries (p); | |
4155 for (i = 0; i < nentries; i++) | |
4156 { | |
4157 EMACS_INT first, last; | |
4158 Lisp_Object dummy_val; | |
4159 int jj; | |
867 | 4160 Ibyte strr[MAX_ICHAR_LEN]; |
428 | 4161 |
4162 unified_range_table_get_range (p, i, &first, &last, | |
4163 &dummy_val); | |
4164 for (jj = first; jj <= last && jj < 0x80; jj++) | |
4165 fastmap[jj] = 1; | |
4166 /* Ranges below 0x100 can span charsets, but there | |
4167 are only two (Control-1 and Latin-1), and | |
4168 either first or last has to be in them. */ | |
867 | 4169 set_itext_ichar (strr, first); |
428 | 4170 fastmap[*strr] = 1; |
4171 if (last < 0x100) | |
4172 { | |
867 | 4173 set_itext_ichar (strr, last); |
428 | 4174 fastmap[*strr] = 1; |
4175 } | |
5648
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
4176 else if (MOST_POSITIVE_FIXNUM == last) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
4177 { |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
4178 /* This is RECC_MULTIBYTE or RECC_NONASCII; true for all |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
4179 non-ASCII characters. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
4180 jj = 0x80; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
4181 while (jj < 0xA0) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
4182 { |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
4183 fastmap[jj++] = 1; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
4184 } |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
4185 } |
428 | 4186 } |
4187 } | |
4188 break; | |
4189 | |
4190 case charset_mule_not: | |
4191 { | |
4192 int nentries; | |
4193 int i; | |
4832
07fa38c30fdf
fix messed-up fastmap calculation in charset_mule_not
Ben Wing <ben@xemacs.org>
parents:
4759
diff
changeset
|
4194 int smallest_prev = 0; |
5648
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
4195 Bitbyte flags = *p++; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
4196 |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
4197 if (flags) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
4198 { |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
4199 /* We need to consult the syntax table, fastmap won't |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
4200 work. */ |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
4201 bufp->can_be_null = 1; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
4202 goto done; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
4203 } |
428 | 4204 |
4205 nentries = unified_range_table_nentries (p); | |
4206 for (i = 0; i < nentries; i++) | |
4207 { | |
4208 EMACS_INT first, last; | |
4209 Lisp_Object dummy_val; | |
4210 int jj; | |
4211 | |
4212 unified_range_table_get_range (p, i, &first, &last, | |
4213 &dummy_val); | |
4214 for (jj = smallest_prev; jj < first && jj < 0x80; jj++) | |
4215 fastmap[jj] = 1; | |
4216 smallest_prev = last + 1; | |
4217 if (smallest_prev >= 0x80) | |
4218 break; | |
4219 } | |
4832
07fa38c30fdf
fix messed-up fastmap calculation in charset_mule_not
Ben Wing <ben@xemacs.org>
parents:
4759
diff
changeset
|
4220 |
07fa38c30fdf
fix messed-up fastmap calculation in charset_mule_not
Ben Wing <ben@xemacs.org>
parents:
4759
diff
changeset
|
4221 /* Also set lead bytes after the end */ |
07fa38c30fdf
fix messed-up fastmap calculation in charset_mule_not
Ben Wing <ben@xemacs.org>
parents:
4759
diff
changeset
|
4222 for (i = smallest_prev; i < 0x80; i++) |
07fa38c30fdf
fix messed-up fastmap calculation in charset_mule_not
Ben Wing <ben@xemacs.org>
parents:
4759
diff
changeset
|
4223 fastmap[i] = 1; |
07fa38c30fdf
fix messed-up fastmap calculation in charset_mule_not
Ben Wing <ben@xemacs.org>
parents:
4759
diff
changeset
|
4224 |
428 | 4225 /* Calculating which leading bytes are actually allowed |
4226 here is rather difficult, so we just punt and allow | |
4227 all of them. */ | |
4228 for (i = 0x80; i < 0xA0; i++) | |
4229 fastmap[i] = 1; | |
4230 } | |
4231 break; | |
4232 #endif /* MULE */ | |
4233 | |
4234 | |
4235 case anychar: | |
4236 { | |
4237 int fastmap_newline = fastmap['\n']; | |
4238 | |
4239 /* `.' matches anything ... */ | |
4240 #ifdef MULE | |
4241 /* "anything" only includes bytes that can be the | |
4242 first byte of a character. */ | |
4243 for (j = 0; j < 0xA0; j++) | |
4244 fastmap[j] = 1; | |
4245 #else | |
4246 for (j = 0; j < (1 << BYTEWIDTH); j++) | |
4247 fastmap[j] = 1; | |
4248 #endif | |
4249 | |
4250 /* ... except perhaps newline. */ | |
4251 if (!(bufp->syntax & RE_DOT_NEWLINE)) | |
4252 fastmap['\n'] = fastmap_newline; | |
4253 | |
4254 /* Return if we have already set `can_be_null'; if we have, | |
4255 then the fastmap is irrelevant. Something's wrong here. */ | |
4256 else if (bufp->can_be_null) | |
4257 goto done; | |
4258 | |
4259 /* Otherwise, have to check alternative paths. */ | |
4260 break; | |
4261 } | |
4262 | |
826 | 4263 #ifndef emacs |
4264 case wordchar: | |
4265 for (j = 0; j < (1 << BYTEWIDTH); j++) | |
4266 if (SYNTAX (ignored, j) == Sword) | |
4267 fastmap[j] = 1; | |
4268 break; | |
4269 | |
4270 case notwordchar: | |
4271 for (j = 0; j < (1 << BYTEWIDTH); j++) | |
4272 if (SYNTAX (ignored, j) != Sword) | |
4273 fastmap[j] = 1; | |
4274 break; | |
4275 #else /* emacs */ | |
4276 case wordchar: | |
4277 case notwordchar: | |
460 | 4278 case wordbound: |
4279 case notwordbound: | |
4280 case wordbeg: | |
4281 case wordend: | |
4282 case notsyntaxspec: | |
4283 case syntaxspec: | |
4284 /* This match depends on text properties. These end with | |
4285 aborting optimizations. */ | |
4286 bufp->can_be_null = 1; | |
4287 goto done; | |
826 | 4288 #if 0 /* all of the following code is unused now that the `syntax-table' |
4289 property exists -- it's trickier to do this than just look in | |
4290 the buffer. &&#### but we could just use the syntax-cache stuff | |
4291 instead; why don't we? --ben */ | |
4292 case wordchar: | |
4293 k = (int) Sword; | |
4294 goto matchsyntax; | |
4295 | |
4296 case notwordchar: | |
4297 k = (int) Sword; | |
4298 goto matchnotsyntax; | |
4299 | |
428 | 4300 case syntaxspec: |
4301 k = *p++; | |
826 | 4302 matchsyntax: |
428 | 4303 #ifdef MULE |
4304 for (j = 0; j < 0x80; j++) | |
826 | 4305 if (SYNTAX |
4306 (XCHAR_TABLE (BUFFER_MIRROR_SYNTAX_TABLE (lispbuf)), j) == | |
428 | 4307 (enum syntaxcode) k) |
4308 fastmap[j] = 1; | |
4309 for (j = 0x80; j < 0xA0; j++) | |
4310 { | |
826 | 4311 if (leading_byte_prefix_p ((unsigned char) j)) |
428 | 4312 /* too complicated to calculate this right */ |
4313 fastmap[j] = 1; | |
4314 else | |
4315 { | |
4316 int multi_p; | |
4317 Lisp_Object cset; | |
4318 | |
826 | 4319 cset = charset_by_leading_byte (j); |
428 | 4320 if (CHARSETP (cset)) |
4321 { | |
826 | 4322 if (charset_syntax (lispbuf, cset, &multi_p) |
428 | 4323 == Sword || multi_p) |
4324 fastmap[j] = 1; | |
4325 } | |
4326 } | |
4327 } | |
446 | 4328 #else /* not MULE */ |
428 | 4329 for (j = 0; j < (1 << BYTEWIDTH); j++) |
826 | 4330 if (SYNTAX |
4331 (XCHAR_TABLE (BUFFER_MIRROR_SYNTAX_TABLE (lispbuf)), j) == | |
428 | 4332 (enum syntaxcode) k) |
4333 fastmap[j] = 1; | |
446 | 4334 #endif /* MULE */ |
428 | 4335 break; |
4336 | |
4337 | |
4338 case notsyntaxspec: | |
4339 k = *p++; | |
826 | 4340 matchnotsyntax: |
428 | 4341 #ifdef MULE |
4342 for (j = 0; j < 0x80; j++) | |
826 | 4343 if (SYNTAX |
428 | 4344 (XCHAR_TABLE |
826 | 4345 (BUFFER_MIRROR_SYNTAX_TABLE (lispbuf)), j) != |
428 | 4346 (enum syntaxcode) k) |
4347 fastmap[j] = 1; | |
4348 for (j = 0x80; j < 0xA0; j++) | |
4349 { | |
826 | 4350 if (leading_byte_prefix_p ((unsigned char) j)) |
428 | 4351 /* too complicated to calculate this right */ |
4352 fastmap[j] = 1; | |
4353 else | |
4354 { | |
4355 int multi_p; | |
4356 Lisp_Object cset; | |
4357 | |
826 | 4358 cset = charset_by_leading_byte (j); |
428 | 4359 if (CHARSETP (cset)) |
4360 { | |
826 | 4361 if (charset_syntax (lispbuf, cset, &multi_p) |
428 | 4362 != Sword || multi_p) |
4363 fastmap[j] = 1; | |
4364 } | |
4365 } | |
4366 } | |
446 | 4367 #else /* not MULE */ |
428 | 4368 for (j = 0; j < (1 << BYTEWIDTH); j++) |
826 | 4369 if (SYNTAX |
428 | 4370 (XCHAR_TABLE |
826 | 4371 (BUFFER_MIRROR_SYNTAX_TABLE (lispbuf)), j) != |
428 | 4372 (enum syntaxcode) k) |
4373 fastmap[j] = 1; | |
446 | 4374 #endif /* MULE */ |
428 | 4375 break; |
826 | 4376 #endif /* 0 */ |
428 | 4377 |
4378 #ifdef MULE | |
4379 /* 97/2/17 jhod category patch */ | |
4380 case categoryspec: | |
4381 case notcategoryspec: | |
4382 bufp->can_be_null = 1; | |
1333 | 4383 UNBIND_REGEX_MALLOC_CHECK (); |
428 | 4384 return 0; |
4385 /* end if category patch */ | |
4386 #endif /* MULE */ | |
4387 | |
4388 /* All cases after this match the empty string. These end with | |
4389 `continue'. */ | |
4390 case before_dot: | |
4391 case at_dot: | |
4392 case after_dot: | |
4393 continue; | |
826 | 4394 #endif /* emacs */ |
428 | 4395 |
4396 | |
4397 case no_op: | |
4398 case begline: | |
4399 case endline: | |
4400 case begbuf: | |
4401 case endbuf: | |
460 | 4402 #ifndef emacs |
428 | 4403 case wordbound: |
4404 case notwordbound: | |
4405 case wordbeg: | |
4406 case wordend: | |
460 | 4407 #endif |
428 | 4408 case push_dummy_failure: |
4409 continue; | |
4410 | |
4411 | |
4412 case jump_n: | |
4413 case pop_failure_jump: | |
4414 case maybe_pop_jump: | |
4415 case jump: | |
4416 case jump_past_alt: | |
4417 case dummy_failure_jump: | |
4418 EXTRACT_NUMBER_AND_INCR (j, p); | |
4419 p += j; | |
4420 if (j > 0) | |
4421 continue; | |
4422 | |
4423 /* Jump backward implies we just went through the body of a | |
4424 loop and matched nothing. Opcode jumped to should be | |
4425 `on_failure_jump' or `succeed_n'. Just treat it like an | |
4426 ordinary jump. For a * loop, it has pushed its failure | |
4427 point already; if so, discard that as redundant. */ | |
4428 if ((re_opcode_t) *p != on_failure_jump | |
4429 && (re_opcode_t) *p != succeed_n) | |
4430 continue; | |
4431 | |
4432 p++; | |
4433 EXTRACT_NUMBER_AND_INCR (j, p); | |
4434 p += j; | |
4435 | |
4436 /* If what's on the stack is where we are now, pop it. */ | |
4437 if (!FAIL_STACK_EMPTY () | |
4438 && fail_stack.stack[fail_stack.avail - 1].pointer == p) | |
4439 fail_stack.avail--; | |
4440 | |
4441 continue; | |
4442 | |
4443 | |
4444 case on_failure_jump: | |
4445 case on_failure_keep_string_jump: | |
4446 handle_on_failure_jump: | |
4447 EXTRACT_NUMBER_AND_INCR (j, p); | |
4448 | |
4449 /* For some patterns, e.g., `(a?)?', `p+j' here points to the | |
4450 end of the pattern. We don't want to push such a point, | |
4451 since when we restore it above, entering the switch will | |
4452 increment `p' past the end of the pattern. We don't need | |
4453 to push such a point since we obviously won't find any more | |
4454 fastmap entries beyond `pend'. Such a pattern can match | |
4455 the null string, though. */ | |
4456 if (p + j < pend) | |
4457 { | |
4458 if (!PUSH_PATTERN_OP (p + j, fail_stack)) | |
4459 { | |
4460 RESET_FAIL_STACK (); | |
1333 | 4461 UNBIND_REGEX_MALLOC_CHECK (); |
428 | 4462 return -2; |
4463 } | |
4464 } | |
4465 else | |
4466 bufp->can_be_null = 1; | |
4467 | |
4468 if (succeed_n_p) | |
4469 { | |
4470 EXTRACT_NUMBER_AND_INCR (k, p); /* Skip the n. */ | |
4471 succeed_n_p = false; | |
4472 } | |
4473 | |
4474 continue; | |
4475 | |
4476 | |
4477 case succeed_n: | |
4478 /* Get to the number of times to succeed. */ | |
4479 p += 2; | |
4480 | |
4481 /* Increment p past the n for when k != 0. */ | |
4482 EXTRACT_NUMBER_AND_INCR (k, p); | |
4483 if (k == 0) | |
4484 { | |
4485 p -= 4; | |
4486 succeed_n_p = true; /* Spaghetti code alert. */ | |
4487 goto handle_on_failure_jump; | |
4488 } | |
4489 continue; | |
4490 | |
4491 | |
4492 case set_number_at: | |
4493 p += 4; | |
4494 continue; | |
4495 | |
4496 | |
4497 case start_memory: | |
4498 case stop_memory: | |
4499 p += 2; | |
4500 continue; | |
4501 | |
4502 | |
4503 default: | |
2500 | 4504 ABORT (); /* We have listed all the cases. */ |
428 | 4505 } /* switch *p++ */ |
4506 | |
4507 /* Getting here means we have found the possible starting | |
4508 characters for one path of the pattern -- and that the empty | |
4509 string does not match. We need not follow this path further. | |
4510 Instead, look at the next alternative (remembered on the | |
4511 stack), or quit if no more. The test at the top of the loop | |
4512 does these things. */ | |
4513 path_can_be_null = false; | |
4514 p = pend; | |
4515 } /* while p */ | |
4516 | |
4517 /* Set `can_be_null' for the last path (also the first path, if the | |
4518 pattern is empty). */ | |
4519 bufp->can_be_null |= path_can_be_null; | |
4520 | |
4521 done: | |
4522 RESET_FAIL_STACK (); | |
1333 | 4523 UNBIND_REGEX_MALLOC_CHECK (); |
428 | 4524 return 0; |
4525 } /* re_compile_fastmap */ | |
4526 | |
4527 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and | |
4528 ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use | |
4529 this memory for recording register information. STARTS and ENDS | |
4530 must be allocated using the malloc library routine, and must each | |
4531 be at least NUM_REGS * sizeof (regoff_t) bytes long. | |
4532 | |
4533 If NUM_REGS == 0, then subsequent matches should allocate their own | |
4534 register data. | |
4535 | |
4536 Unless this function is called, the first search or match using | |
4537 PATTERN_BUFFER will allocate its own register data, without | |
4538 freeing the old data. */ | |
4539 | |
4540 void | |
4541 re_set_registers (struct re_pattern_buffer *bufp, struct re_registers *regs, | |
647 | 4542 int num_regs, regoff_t *starts, regoff_t *ends) |
428 | 4543 { |
4544 if (num_regs) | |
4545 { | |
4546 bufp->regs_allocated = REGS_REALLOCATE; | |
4547 regs->num_regs = num_regs; | |
4548 regs->start = starts; | |
4549 regs->end = ends; | |
4550 } | |
4551 else | |
4552 { | |
4553 bufp->regs_allocated = REGS_UNALLOCATED; | |
4554 regs->num_regs = 0; | |
4555 regs->start = regs->end = (regoff_t *) 0; | |
4556 } | |
4557 } | |
4558 | |
4559 /* Searching routines. */ | |
4560 | |
4561 /* Like re_search_2, below, but only one string is specified, and | |
4562 doesn't let you say where to stop matching. */ | |
4563 | |
4564 int | |
442 | 4565 re_search (struct re_pattern_buffer *bufp, const char *string, int size, |
826 | 4566 int startpos, int range, struct re_registers *regs |
4567 RE_LISP_CONTEXT_ARGS_DECL) | |
428 | 4568 { |
4569 return re_search_2 (bufp, NULL, 0, string, size, startpos, range, | |
826 | 4570 regs, size RE_LISP_CONTEXT_ARGS); |
428 | 4571 } |
4572 | |
4573 /* Using the compiled pattern in BUFP->buffer, first tries to match the | |
4574 virtual concatenation of STRING1 and STRING2, starting first at index | |
4575 STARTPOS, then at STARTPOS + 1, and so on. | |
4576 | |
4577 STRING1 and STRING2 have length SIZE1 and SIZE2, respectively. | |
4578 | |
4579 RANGE is how far to scan while trying to match. RANGE = 0 means try | |
4580 only at STARTPOS; in general, the last start tried is STARTPOS + | |
4581 RANGE. | |
4582 | |
826 | 4583 All sizes and positions refer to bytes (not chars); under Mule, the code |
4584 knows about the format of the text and will only check at positions | |
4585 where a character starts. | |
4586 | |
428 | 4587 With MULE, RANGE is a byte position, not a char position. The last |
4588 start tried is the character starting <= STARTPOS + RANGE. | |
4589 | |
4590 In REGS, return the indices of the virtual concatenation of STRING1 | |
4591 and STRING2 that matched the entire BUFP->buffer and its contained | |
4592 subexpressions. | |
4593 | |
4594 Do not consider matching one past the index STOP in the virtual | |
4595 concatenation of STRING1 and STRING2. | |
4596 | |
4597 We return either the position in the strings at which the match was | |
4598 found, -1 if no match, or -2 if error (such as failure | |
4599 stack overflow). */ | |
4600 | |
4601 int | |
446 | 4602 re_search_2 (struct re_pattern_buffer *bufp, const char *str1, |
4603 int size1, const char *str2, int size2, int startpos, | |
826 | 4604 int range, struct re_registers *regs, int stop |
4605 RE_LISP_CONTEXT_ARGS_DECL) | |
428 | 4606 { |
4607 int val; | |
446 | 4608 re_char *string1 = (re_char *) str1; |
4609 re_char *string2 = (re_char *) str2; | |
428 | 4610 REGISTER char *fastmap = bufp->fastmap; |
446 | 4611 REGISTER RE_TRANSLATE_TYPE translate = bufp->translate; |
428 | 4612 int total_size = size1 + size2; |
4613 int endpos = startpos + range; | |
4614 #ifdef REGEX_BEGLINE_CHECK | |
4615 int anchored_at_begline = 0; | |
4616 #endif | |
446 | 4617 re_char *d; |
826 | 4618 #ifdef emacs |
4619 Internal_Format fmt = buffer_or_other_internal_format (lispobj); | |
1346 | 4620 #ifdef REL_ALLOC |
4621 Ibyte *orig_buftext = | |
4622 BUFFERP (lispobj) ? | |
4623 BYTE_BUF_BYTE_ADDRESS (XBUFFER (lispobj), | |
4624 BYTE_BUF_BEGV (XBUFFER (lispobj))) : | |
4625 0; | |
4626 #endif | |
1333 | 4627 #ifdef ERROR_CHECK_MALLOC |
4628 int depth; | |
4629 #endif | |
826 | 4630 #endif /* emacs */ |
4631 #if 1 | |
4632 int forward_search_p; | |
4633 #endif | |
428 | 4634 |
4635 /* Check for out-of-range STARTPOS. */ | |
4636 if (startpos < 0 || startpos > total_size) | |
4637 return -1; | |
4638 | |
4639 /* Fix up RANGE if it might eventually take us outside | |
4640 the virtual concatenation of STRING1 and STRING2. */ | |
4641 if (endpos < 0) | |
4642 range = 0 - startpos; | |
4643 else if (endpos > total_size) | |
4644 range = total_size - startpos; | |
4645 | |
826 | 4646 #if 1 |
4647 forward_search_p = range > 0; | |
4648 #endif | |
4649 | |
428 | 4650 /* If the search isn't to be a backwards one, don't waste time in a |
4651 search for a pattern that must be anchored. */ | |
4652 if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == begbuf && range > 0) | |
4653 { | |
4654 if (startpos > 0) | |
4655 return -1; | |
4656 else | |
4657 { | |
442 | 4658 d = ((const unsigned char *) |
428 | 4659 (startpos >= size1 ? string2 - size1 : string1) + startpos); |
867 | 4660 range = itext_ichar_len_fmt (d, fmt); |
428 | 4661 } |
4662 } | |
4663 | |
460 | 4664 #ifdef emacs |
4665 /* In a forward search for something that starts with \=. | |
4666 don't keep searching past point. */ | |
4667 if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == at_dot && range > 0) | |
4668 { | |
826 | 4669 if (!BUFFERP (lispobj)) |
4670 return -1; | |
4527
8418d1ad4944
Fix at_dot regex under Mule. <87hc6rv53v.fsf@uwakimon.sk.tsukuba.ac.jp>
Stephen J. Turnbull <stephen@xemacs.org>
parents:
3300
diff
changeset
|
4671 range = (BYTE_BUF_PT (XBUFFER (lispobj)) |
8418d1ad4944
Fix at_dot regex under Mule. <87hc6rv53v.fsf@uwakimon.sk.tsukuba.ac.jp>
Stephen J. Turnbull <stephen@xemacs.org>
parents:
3300
diff
changeset
|
4672 - BYTE_BUF_BEGV (XBUFFER (lispobj)) - startpos); |
460 | 4673 if (range < 0) |
4674 return -1; | |
4675 } | |
4676 #endif /* emacs */ | |
4677 | |
1333 | 4678 #ifdef ERROR_CHECK_MALLOC |
4679 /* Do this after the above return()s. */ | |
4680 depth = bind_regex_malloc_disallowed (1); | |
4681 #endif | |
4682 | |
428 | 4683 /* Update the fastmap now if not correct already. */ |
1333 | 4684 BEGIN_REGEX_MALLOC_OK (); |
428 | 4685 if (fastmap && !bufp->fastmap_accurate) |
826 | 4686 if (re_compile_fastmap (bufp RE_LISP_SHORT_CONTEXT_ARGS) == -2) |
1333 | 4687 { |
4688 END_REGEX_MALLOC_OK (); | |
4689 UNBIND_REGEX_MALLOC_CHECK (); | |
4690 return -2; | |
4691 } | |
4692 | |
4693 END_REGEX_MALLOC_OK (); | |
4694 RE_SEARCH_RELOCATE_MOVEABLE_DATA_POINTERS (); | |
428 | 4695 |
4696 #ifdef REGEX_BEGLINE_CHECK | |
4697 { | |
647 | 4698 long i = 0; |
428 | 4699 |
4700 while (i < bufp->used) | |
4701 { | |
4702 if (bufp->buffer[i] == start_memory || | |
4703 bufp->buffer[i] == stop_memory) | |
4704 i += 2; | |
4705 else | |
4706 break; | |
4707 } | |
4708 anchored_at_begline = i < bufp->used && bufp->buffer[i] == begline; | |
4709 } | |
4710 #endif | |
4711 | |
460 | 4712 #ifdef emacs |
1333 | 4713 BEGIN_REGEX_MALLOC_OK (); |
826 | 4714 scache = setup_syntax_cache (scache, lispobj, lispbuf, |
4715 offset_to_charxpos (lispobj, startpos), | |
4716 1); | |
1333 | 4717 END_REGEX_MALLOC_OK (); |
4718 RE_SEARCH_RELOCATE_MOVEABLE_DATA_POINTERS (); | |
460 | 4719 #endif |
4720 | |
428 | 4721 /* Loop through the string, looking for a place to start matching. */ |
4722 for (;;) | |
4723 { | |
4724 #ifdef REGEX_BEGLINE_CHECK | |
826 | 4725 /* If the regex is anchored at the beginning of a line (i.e. with a |
4726 ^), then we can speed things up by skipping to the next | |
4727 beginning-of-line. However, to determine "beginning of line" we | |
4728 need to look at the previous char, so can't do this check if at | |
4729 beginning of either string. (Well, we could if at the beginning of | |
4730 the second string, but it would require additional code, and this | |
4731 is just an optimization.) */ | |
4732 if (anchored_at_begline && startpos > 0 && startpos != size1) | |
428 | 4733 { |
826 | 4734 if (range > 0) |
4735 { | |
4736 /* whose stupid idea was it anyway to make this | |
4737 function take two strings to match?? */ | |
4738 int lim = 0; | |
4739 re_char *orig_d; | |
4740 re_char *stop_d; | |
4741 | |
4742 /* Compute limit as below in fastmap code, so we are guaranteed | |
4743 to remain within a single string. */ | |
4744 if (startpos < size1 && startpos + range >= size1) | |
4745 lim = range - (size1 - startpos); | |
4746 | |
4747 d = ((const unsigned char *) | |
4748 (startpos >= size1 ? string2 - size1 : string1) + startpos); | |
4749 orig_d = d; | |
4750 stop_d = d + range - lim; | |
4751 | |
4752 /* We want to find the next location (including the current | |
4753 one) where the previous char is a newline, so back up one | |
4754 and search forward for a newline. */ | |
867 | 4755 DEC_IBYTEPTR_FMT (d, fmt); /* Ok, since startpos != size1. */ |
826 | 4756 |
4757 /* Written out as an if-else to avoid testing `translate' | |
4758 inside the loop. */ | |
4759 if (TRANSLATE_P (translate)) | |
4760 while (d < stop_d && | |
867 | 4761 RE_TRANSLATE_1 (itext_ichar_fmt (d, fmt, lispobj)) |
826 | 4762 != '\n') |
867 | 4763 INC_IBYTEPTR_FMT (d, fmt); |
826 | 4764 else |
4765 while (d < stop_d && | |
867 | 4766 itext_ichar_ascii_fmt (d, fmt, lispobj) != '\n') |
4767 INC_IBYTEPTR_FMT (d, fmt); | |
826 | 4768 |
4769 /* If we were stopped by a newline, skip forward over it. | |
4770 Otherwise we will get in an infloop when our start position | |
4771 was at begline. */ | |
4772 if (d < stop_d) | |
867 | 4773 INC_IBYTEPTR_FMT (d, fmt); |
826 | 4774 range -= d - orig_d; |
4775 startpos += d - orig_d; | |
4776 #if 1 | |
4777 assert (!forward_search_p || range >= 0); | |
4778 #endif | |
4779 } | |
4780 else if (range < 0) | |
4781 { | |
4782 /* We're lazy, like in the fastmap code below */ | |
867 | 4783 Ichar c; |
826 | 4784 |
4785 d = ((const unsigned char *) | |
4786 (startpos >= size1 ? string2 - size1 : string1) + startpos); | |
867 | 4787 DEC_IBYTEPTR_FMT (d, fmt); |
4788 c = itext_ichar_fmt (d, fmt, lispobj); | |
826 | 4789 c = RE_TRANSLATE (c); |
4790 if (c != '\n') | |
4791 goto advance; | |
4792 } | |
428 | 4793 } |
4794 #endif /* REGEX_BEGLINE_CHECK */ | |
4795 | |
4796 /* If a fastmap is supplied, skip quickly over characters that | |
4797 cannot be the start of a match. If the pattern can match the | |
4798 null string, however, we don't need to skip characters; we want | |
4799 the first null string. */ | |
4800 if (fastmap && startpos < total_size && !bufp->can_be_null) | |
4801 { | |
826 | 4802 /* For the moment, fastmap always works as if buffer |
4803 is in default format, so convert chars in the search strings | |
4804 into default format as we go along, if necessary. | |
4805 | |
4806 &&#### fastmap needs rethinking for 8-bit-fixed so | |
4807 it's faster. We need it to reflect the raw | |
4808 8-bit-fixed values. That isn't so hard if we assume | |
4809 that the top 96 bytes represent a single 1-byte | |
4810 charset. For 16-bit/32-bit stuff it's probably not | |
4811 worth it to make the fastmap represent the raw, due to | |
4812 its nature -- we'd have to use the LSB for the | |
4813 fastmap, and that causes lots of problems with Mule | |
4814 chars, where it essentially wipes out the usefulness | |
4815 of the fastmap entirely. */ | |
428 | 4816 if (range > 0) /* Searching forwards. */ |
4817 { | |
4818 int lim = 0; | |
4819 int irange = range; | |
4820 | |
4821 if (startpos < size1 && startpos + range >= size1) | |
4822 lim = range - (size1 - startpos); | |
4823 | |
442 | 4824 d = ((const unsigned char *) |
428 | 4825 (startpos >= size1 ? string2 - size1 : string1) + startpos); |
4826 | |
4827 /* Written out as an if-else to avoid testing `translate' | |
4828 inside the loop. */ | |
446 | 4829 if (TRANSLATE_P (translate)) |
826 | 4830 { |
4831 while (range > lim) | |
4832 { | |
4833 re_char *old_d = d; | |
428 | 4834 #ifdef MULE |
867 | 4835 Ibyte tempch[MAX_ICHAR_LEN]; |
4836 Ichar buf_ch = | |
4837 RE_TRANSLATE_1 (itext_ichar_fmt (d, fmt, lispobj)); | |
4838 set_itext_ichar (tempch, buf_ch); | |
826 | 4839 if (fastmap[*tempch]) |
4840 break; | |
446 | 4841 #else |
826 | 4842 if (fastmap[(unsigned char) RE_TRANSLATE_1 (*d)]) |
4843 break; | |
446 | 4844 #endif /* MULE */ |
867 | 4845 INC_IBYTEPTR_FMT (d, fmt); |
826 | 4846 range -= (d - old_d); |
4847 #if 1 | |
1333 | 4848 assert (!forward_search_p || range >= 0); |
826 | 4849 #endif |
4850 } | |
4851 } | |
4852 #ifdef MULE | |
4853 else if (fmt != FORMAT_DEFAULT) | |
4854 { | |
4855 while (range > lim) | |
4856 { | |
4857 re_char *old_d = d; | |
867 | 4858 Ibyte tempch[MAX_ICHAR_LEN]; |
4859 Ichar buf_ch = itext_ichar_fmt (d, fmt, lispobj); | |
4860 set_itext_ichar (tempch, buf_ch); | |
826 | 4861 if (fastmap[*tempch]) |
4862 break; | |
867 | 4863 INC_IBYTEPTR_FMT (d, fmt); |
826 | 4864 range -= (d - old_d); |
4865 #if 1 | |
1333 | 4866 assert (!forward_search_p || range >= 0); |
826 | 4867 #endif |
4868 } | |
4869 } | |
4870 #endif /* MULE */ | |
428 | 4871 else |
826 | 4872 { |
4873 while (range > lim && !fastmap[*d]) | |
4874 { | |
4875 re_char *old_d = d; | |
867 | 4876 INC_IBYTEPTR (d); |
826 | 4877 range -= (d - old_d); |
4878 #if 1 | |
4879 assert (!forward_search_p || range >= 0); | |
4880 #endif | |
4881 } | |
4882 } | |
428 | 4883 |
4884 startpos += irange - range; | |
4885 } | |
4886 else /* Searching backwards. */ | |
4887 { | |
826 | 4888 /* #### It's not clear why we don't just write a loop, like |
4889 for the moving-forward case. Perhaps the writer got lazy, | |
4890 since backward searches aren't so common. */ | |
4891 d = ((const unsigned char *) | |
4892 (startpos >= size1 ? string2 - size1 : string1) + startpos); | |
428 | 4893 #ifdef MULE |
826 | 4894 { |
867 | 4895 Ibyte tempch[MAX_ICHAR_LEN]; |
4896 Ichar buf_ch = | |
4897 RE_TRANSLATE (itext_ichar_fmt (d, fmt, lispobj)); | |
4898 set_itext_ichar (tempch, buf_ch); | |
826 | 4899 if (!fastmap[*tempch]) |
4900 goto advance; | |
4901 } | |
428 | 4902 #else |
826 | 4903 if (!fastmap[(unsigned char) RE_TRANSLATE (*d)]) |
446 | 4904 goto advance; |
826 | 4905 #endif /* MULE */ |
428 | 4906 } |
4907 } | |
4908 | |
4909 /* If can't match the null string, and that's all we have left, fail. */ | |
4910 if (range >= 0 && startpos == total_size && fastmap | |
4911 && !bufp->can_be_null) | |
1333 | 4912 { |
4913 UNBIND_REGEX_MALLOC_CHECK (); | |
4914 return -1; | |
4915 } | |
428 | 4916 |
4917 #ifdef emacs /* XEmacs added, w/removal of immediate_quit */ | |
4918 if (!no_quit_in_re_search) | |
1333 | 4919 { |
4920 BEGIN_REGEX_MALLOC_OK (); | |
4921 QUIT; | |
4922 END_REGEX_MALLOC_OK (); | |
4923 RE_SEARCH_RELOCATE_MOVEABLE_DATA_POINTERS (); | |
4924 } | |
4925 | |
428 | 4926 #endif |
1333 | 4927 BEGIN_REGEX_MALLOC_OK (); |
428 | 4928 val = re_match_2_internal (bufp, string1, size1, string2, size2, |
826 | 4929 startpos, regs, stop |
4930 RE_LISP_CONTEXT_ARGS); | |
428 | 4931 #ifndef REGEX_MALLOC |
1333 | 4932 ALLOCA_GARBAGE_COLLECT (); |
428 | 4933 #endif |
1333 | 4934 END_REGEX_MALLOC_OK (); |
4935 RE_SEARCH_RELOCATE_MOVEABLE_DATA_POINTERS (); | |
428 | 4936 |
4937 if (val >= 0) | |
1333 | 4938 { |
4939 UNBIND_REGEX_MALLOC_CHECK (); | |
4940 return startpos; | |
4941 } | |
428 | 4942 |
4943 if (val == -2) | |
1333 | 4944 { |
4945 UNBIND_REGEX_MALLOC_CHECK (); | |
4946 return -2; | |
4947 } | |
4948 | |
4949 RE_SEARCH_RELOCATE_MOVEABLE_DATA_POINTERS (); | |
428 | 4950 advance: |
4951 if (!range) | |
4952 break; | |
4953 else if (range > 0) | |
4954 { | |
826 | 4955 Bytecount d_size; |
442 | 4956 d = ((const unsigned char *) |
428 | 4957 (startpos >= size1 ? string2 - size1 : string1) + startpos); |
867 | 4958 d_size = itext_ichar_len_fmt (d, fmt); |
428 | 4959 range -= d_size; |
826 | 4960 #if 1 |
4961 assert (!forward_search_p || range >= 0); | |
4962 #endif | |
428 | 4963 startpos += d_size; |
4964 } | |
4965 else | |
4966 { | |
826 | 4967 Bytecount d_size; |
428 | 4968 /* Note startpos > size1 not >=. If we are on the |
4969 string1/string2 boundary, we want to backup into string1. */ | |
442 | 4970 d = ((const unsigned char *) |
428 | 4971 (startpos > size1 ? string2 - size1 : string1) + startpos); |
867 | 4972 DEC_IBYTEPTR_FMT (d, fmt); |
4973 d_size = itext_ichar_len_fmt (d, fmt); | |
428 | 4974 range += d_size; |
826 | 4975 #if 1 |
4976 assert (!forward_search_p || range >= 0); | |
4977 #endif | |
428 | 4978 startpos -= d_size; |
4979 } | |
4980 } | |
1333 | 4981 UNBIND_REGEX_MALLOC_CHECK (); |
428 | 4982 return -1; |
4983 } /* re_search_2 */ | |
826 | 4984 |
428 | 4985 |
4986 /* Declarations and macros for re_match_2. */ | |
4987 | |
4988 /* This converts PTR, a pointer into one of the search strings `string1' | |
4989 and `string2' into an offset from the beginning of that string. */ | |
4990 #define POINTER_TO_OFFSET(ptr) \ | |
4991 (FIRST_STRING_P (ptr) \ | |
4992 ? ((regoff_t) ((ptr) - string1)) \ | |
4993 : ((regoff_t) ((ptr) - string2 + size1))) | |
4994 | |
4995 /* Macros for dealing with the split strings in re_match_2. */ | |
4996 | |
4997 #define MATCHING_IN_FIRST_STRING (dend == end_match_1) | |
4998 | |
4999 /* Call before fetching a character with *d. This switches over to | |
5000 string2 if necessary. */ | |
826 | 5001 #define REGEX_PREFETCH() \ |
428 | 5002 while (d == dend) \ |
5003 { \ | |
5004 /* End of string2 => fail. */ \ | |
5005 if (dend == end_match_2) \ | |
5006 goto fail; \ | |
5007 /* End of string1 => advance to string2. */ \ | |
5008 d = string2; \ | |
5009 dend = end_match_2; \ | |
5010 } | |
5011 | |
5012 | |
5013 /* Test if at very beginning or at very end of the virtual concatenation | |
5014 of `string1' and `string2'. If only one string, it's `string2'. */ | |
5015 #define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2) | |
5016 #define AT_STRINGS_END(d) ((d) == end2) | |
5017 | |
5018 /* XEmacs change: | |
5019 If the given position straddles the string gap, return the equivalent | |
5020 position that is before or after the gap, respectively; otherwise, | |
5021 return the same position. */ | |
5022 #define POS_BEFORE_GAP_UNSAFE(d) ((d) == string2 ? end1 : (d)) | |
5023 #define POS_AFTER_GAP_UNSAFE(d) ((d) == end1 ? string2 : (d)) | |
5024 | |
5025 /* Test if CH is a word-constituent character. (XEmacs change) */ | |
826 | 5026 #define WORDCHAR_P(ch) \ |
5027 (SYNTAX (BUFFER_MIRROR_SYNTAX_TABLE (lispbuf), ch) == Sword) | |
428 | 5028 |
5029 /* Free everything we malloc. */ | |
5030 #ifdef MATCH_MAY_ALLOCATE | |
1726 | 5031 #define FREE_VAR(var,type) if (var) REGEX_FREE (var, type); var = NULL |
428 | 5032 #define FREE_VARIABLES() \ |
5033 do { \ | |
1333 | 5034 UNBIND_REGEX_MALLOC_CHECK (); \ |
428 | 5035 REGEX_FREE_STACK (fail_stack.stack); \ |
1726 | 5036 FREE_VAR (regstart, re_char **); \ |
5037 FREE_VAR (regend, re_char **); \ | |
5038 FREE_VAR (old_regstart, re_char **); \ | |
5039 FREE_VAR (old_regend, re_char **); \ | |
5040 FREE_VAR (best_regstart, re_char **); \ | |
5041 FREE_VAR (best_regend, re_char **); \ | |
5042 FREE_VAR (reg_info, register_info_type *); \ | |
5043 FREE_VAR (reg_dummy, re_char **); \ | |
5044 FREE_VAR (reg_info_dummy, register_info_type *); \ | |
428 | 5045 } while (0) |
446 | 5046 #else /* not MATCH_MAY_ALLOCATE */ |
1333 | 5047 #define FREE_VARIABLES() \ |
5048 do { \ | |
5049 UNBIND_REGEX_MALLOC_CHECK (); \ | |
5050 } while (0) | |
446 | 5051 #endif /* MATCH_MAY_ALLOCATE */ |
428 | 5052 |
5053 /* These values must meet several constraints. They must not be valid | |
5054 register values; since we have a limit of 255 registers (because | |
5055 we use only one byte in the pattern for the register number), we can | |
5056 use numbers larger than 255. They must differ by 1, because of | |
5057 NUM_FAILURE_ITEMS above. And the value for the lowest register must | |
5058 be larger than the value for the highest register, so we do not try | |
5059 to actually save any registers when none are active. */ | |
5060 #define NO_HIGHEST_ACTIVE_REG (1 << BYTEWIDTH) | |
5061 #define NO_LOWEST_ACTIVE_REG (NO_HIGHEST_ACTIVE_REG + 1) | |
5062 | |
5063 /* Matching routines. */ | |
5064 | |
826 | 5065 #ifndef emacs /* XEmacs never uses this. */ |
428 | 5066 /* re_match is like re_match_2 except it takes only a single string. */ |
5067 | |
5068 int | |
442 | 5069 re_match (struct re_pattern_buffer *bufp, const char *string, int size, |
826 | 5070 int pos, struct re_registers *regs |
5071 RE_LISP_CONTEXT_ARGS_DECL) | |
428 | 5072 { |
446 | 5073 int result = re_match_2_internal (bufp, NULL, 0, (re_char *) string, size, |
826 | 5074 pos, regs, size |
5075 RE_LISP_CONTEXT_ARGS); | |
1333 | 5076 ALLOCA_GARBAGE_COLLECT (); |
428 | 5077 return result; |
5078 } | |
5079 #endif /* not emacs */ | |
5080 | |
5081 /* re_match_2 matches the compiled pattern in BUFP against the | |
5082 (virtual) concatenation of STRING1 and STRING2 (of length SIZE1 and | |
5083 SIZE2, respectively). We start matching at POS, and stop matching | |
5084 at STOP. | |
5085 | |
5086 If REGS is non-null and the `no_sub' field of BUFP is nonzero, we | |
5087 store offsets for the substring each group matched in REGS. See the | |
5088 documentation for exactly how many groups we fill. | |
5089 | |
5090 We return -1 if no match, -2 if an internal error (such as the | |
5091 failure stack overflowing). Otherwise, we return the length of the | |
5092 matched substring. */ | |
5093 | |
5094 int | |
442 | 5095 re_match_2 (struct re_pattern_buffer *bufp, const char *string1, |
5096 int size1, const char *string2, int size2, int pos, | |
826 | 5097 struct re_registers *regs, int stop |
5098 RE_LISP_CONTEXT_ARGS_DECL) | |
428 | 5099 { |
460 | 5100 int result; |
5101 | |
5102 #ifdef emacs | |
826 | 5103 scache = setup_syntax_cache (scache, lispobj, lispbuf, |
5104 offset_to_charxpos (lispobj, pos), | |
5105 1); | |
460 | 5106 #endif |
5107 | |
5108 result = re_match_2_internal (bufp, (re_char *) string1, size1, | |
5109 (re_char *) string2, size2, | |
826 | 5110 pos, regs, stop |
5111 RE_LISP_CONTEXT_ARGS); | |
460 | 5112 |
1333 | 5113 ALLOCA_GARBAGE_COLLECT (); |
428 | 5114 return result; |
5115 } | |
5116 | |
5117 /* This is a separate function so that we can force an alloca cleanup | |
5118 afterwards. */ | |
5119 static int | |
446 | 5120 re_match_2_internal (struct re_pattern_buffer *bufp, re_char *string1, |
5121 int size1, re_char *string2, int size2, int pos, | |
826 | 5122 struct re_registers *regs, int stop |
2333 | 5123 RE_LISP_CONTEXT_ARGS_MULE_DECL) |
428 | 5124 { |
5125 /* General temporaries. */ | |
5126 int mcnt; | |
5127 unsigned char *p1; | |
5128 int should_succeed; /* XEmacs change */ | |
5129 | |
5130 /* Just past the end of the corresponding string. */ | |
446 | 5131 re_char *end1, *end2; |
428 | 5132 |
5133 /* Pointers into string1 and string2, just past the last characters in | |
5134 each to consider matching. */ | |
446 | 5135 re_char *end_match_1, *end_match_2; |
428 | 5136 |
5137 /* Where we are in the data, and the end of the current string. */ | |
446 | 5138 re_char *d, *dend; |
428 | 5139 |
5140 /* Where we are in the pattern, and the end of the pattern. */ | |
5141 unsigned char *p = bufp->buffer; | |
5142 REGISTER unsigned char *pend = p + bufp->used; | |
5143 | |
5144 /* Mark the opcode just after a start_memory, so we can test for an | |
5145 empty subpattern when we get to the stop_memory. */ | |
446 | 5146 re_char *just_past_start_mem = 0; |
428 | 5147 |
5148 /* We use this to map every character in the string. */ | |
446 | 5149 RE_TRANSLATE_TYPE translate = bufp->translate; |
428 | 5150 |
5151 /* Failure point stack. Each place that can handle a failure further | |
5152 down the line pushes a failure point on this stack. It consists of | |
5153 restart, regend, and reg_info for all registers corresponding to | |
5154 the subexpressions we're currently inside, plus the number of such | |
5155 registers, and, finally, two char *'s. The first char * is where | |
5156 to resume scanning the pattern; the second one is where to resume | |
5157 scanning the strings. If the latter is zero, the failure point is | |
5158 a ``dummy''; if a failure happens and the failure point is a dummy, | |
5159 it gets discarded and the next one is tried. */ | |
5160 #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */ | |
5161 fail_stack_type fail_stack; | |
5162 #endif | |
5163 #ifdef DEBUG | |
647 | 5164 static int failure_id; |
5165 int nfailure_points_pushed = 0, nfailure_points_popped = 0; | |
428 | 5166 #endif |
5167 | |
771 | 5168 #ifdef REGEX_REL_ALLOC |
428 | 5169 /* This holds the pointer to the failure stack, when |
5170 it is allocated relocatably. */ | |
5171 fail_stack_elt_t *failure_stack_ptr; | |
5172 #endif | |
5173 | |
5174 /* We fill all the registers internally, independent of what we | |
5175 return, for use in backreferences. The number here includes | |
5176 an element for register zero. */ | |
647 | 5177 int num_regs = bufp->re_ngroups + 1; |
428 | 5178 |
5179 /* The currently active registers. */ | |
647 | 5180 int lowest_active_reg = NO_LOWEST_ACTIVE_REG; |
5181 int highest_active_reg = NO_HIGHEST_ACTIVE_REG; | |
428 | 5182 |
5183 /* Information on the contents of registers. These are pointers into | |
5184 the input strings; they record just what was matched (on this | |
5185 attempt) by a subexpression part of the pattern, that is, the | |
5186 regnum-th regstart pointer points to where in the pattern we began | |
5187 matching and the regnum-th regend points to right after where we | |
5188 stopped matching the regnum-th subexpression. (The zeroth register | |
5189 keeps track of what the whole pattern matches.) */ | |
5190 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */ | |
446 | 5191 re_char **regstart, **regend; |
428 | 5192 #endif |
5193 | |
5194 /* If a group that's operated upon by a repetition operator fails to | |
5195 match anything, then the register for its start will need to be | |
5196 restored because it will have been set to wherever in the string we | |
5197 are when we last see its open-group operator. Similarly for a | |
5198 register's end. */ | |
5199 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */ | |
446 | 5200 re_char **old_regstart, **old_regend; |
428 | 5201 #endif |
5202 | |
5203 /* The is_active field of reg_info helps us keep track of which (possibly | |
5204 nested) subexpressions we are currently in. The matched_something | |
5205 field of reg_info[reg_num] helps us tell whether or not we have | |
5206 matched any of the pattern so far this time through the reg_num-th | |
5207 subexpression. These two fields get reset each time through any | |
5208 loop their register is in. */ | |
5209 #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */ | |
5210 register_info_type *reg_info; | |
5211 #endif | |
5212 | |
5213 /* The following record the register info as found in the above | |
5214 variables when we find a match better than any we've seen before. | |
5215 This happens as we backtrack through the failure points, which in | |
5216 turn happens only if we have not yet matched the entire string. */ | |
647 | 5217 int best_regs_set = false; |
428 | 5218 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */ |
446 | 5219 re_char **best_regstart, **best_regend; |
428 | 5220 #endif |
5221 | |
5222 /* Logically, this is `best_regend[0]'. But we don't want to have to | |
5223 allocate space for that if we're not allocating space for anything | |
5224 else (see below). Also, we never need info about register 0 for | |
5225 any of the other register vectors, and it seems rather a kludge to | |
5226 treat `best_regend' differently than the rest. So we keep track of | |
5227 the end of the best match so far in a separate variable. We | |
5228 initialize this to NULL so that when we backtrack the first time | |
5229 and need to test it, it's not garbage. */ | |
446 | 5230 re_char *match_end = NULL; |
428 | 5231 |
5232 /* This helps SET_REGS_MATCHED avoid doing redundant work. */ | |
5233 int set_regs_matched_done = 0; | |
5234 | |
5235 /* Used when we pop values we don't care about. */ | |
5236 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */ | |
446 | 5237 re_char **reg_dummy; |
428 | 5238 register_info_type *reg_info_dummy; |
5239 #endif | |
5240 | |
5241 #ifdef DEBUG | |
5242 /* Counts the total number of registers pushed. */ | |
647 | 5243 int num_regs_pushed = 0; |
428 | 5244 #endif |
5245 | |
5246 /* 1 if this match ends in the same string (string1 or string2) | |
5247 as the best previous match. */ | |
460 | 5248 re_bool same_str_p; |
428 | 5249 |
5250 /* 1 if this match is the best seen so far. */ | |
460 | 5251 re_bool best_match_p; |
428 | 5252 |
826 | 5253 #ifdef emacs |
5254 Internal_Format fmt = buffer_or_other_internal_format (lispobj); | |
1346 | 5255 #ifdef REL_ALLOC |
5256 Ibyte *orig_buftext = | |
5257 BUFFERP (lispobj) ? | |
5258 BYTE_BUF_BYTE_ADDRESS (XBUFFER (lispobj), | |
5259 BYTE_BUF_BEGV (XBUFFER (lispobj))) : | |
5260 0; | |
5261 #endif | |
5262 | |
1333 | 5263 #ifdef ERROR_CHECK_MALLOC |
5264 int depth = bind_regex_malloc_disallowed (1); | |
5265 #endif | |
826 | 5266 #endif /* emacs */ |
771 | 5267 |
5041 | 5268 DEBUG_MATCH_PRINT1 ("\n\nEntering re_match_2.\n"); |
428 | 5269 |
1333 | 5270 BEGIN_REGEX_MALLOC_OK (); |
428 | 5271 INIT_FAIL_STACK (); |
1333 | 5272 END_REGEX_MALLOC_OK (); |
428 | 5273 |
5274 #ifdef MATCH_MAY_ALLOCATE | |
5275 /* Do not bother to initialize all the register variables if there are | |
5276 no groups in the pattern, as it takes a fair amount of time. If | |
5277 there are groups, we include space for register 0 (the whole | |
5278 pattern), even though we never use it, since it simplifies the | |
5279 array indexing. We should fix this. */ | |
502 | 5280 if (bufp->re_ngroups) |
428 | 5281 { |
1333 | 5282 BEGIN_REGEX_MALLOC_OK (); |
446 | 5283 regstart = REGEX_TALLOC (num_regs, re_char *); |
5284 regend = REGEX_TALLOC (num_regs, re_char *); | |
5285 old_regstart = REGEX_TALLOC (num_regs, re_char *); | |
5286 old_regend = REGEX_TALLOC (num_regs, re_char *); | |
5287 best_regstart = REGEX_TALLOC (num_regs, re_char *); | |
5288 best_regend = REGEX_TALLOC (num_regs, re_char *); | |
428 | 5289 reg_info = REGEX_TALLOC (num_regs, register_info_type); |
446 | 5290 reg_dummy = REGEX_TALLOC (num_regs, re_char *); |
428 | 5291 reg_info_dummy = REGEX_TALLOC (num_regs, register_info_type); |
1333 | 5292 END_REGEX_MALLOC_OK (); |
428 | 5293 |
5294 if (!(regstart && regend && old_regstart && old_regend && reg_info | |
5295 && best_regstart && best_regend && reg_dummy && reg_info_dummy)) | |
5296 { | |
5297 FREE_VARIABLES (); | |
5298 return -2; | |
5299 } | |
5300 } | |
5301 else | |
5302 { | |
5303 /* We must initialize all our variables to NULL, so that | |
5304 `FREE_VARIABLES' doesn't try to free them. */ | |
5305 regstart = regend = old_regstart = old_regend = best_regstart | |
5306 = best_regend = reg_dummy = NULL; | |
5307 reg_info = reg_info_dummy = (register_info_type *) NULL; | |
5308 } | |
5309 #endif /* MATCH_MAY_ALLOCATE */ | |
5310 | |
1333 | 5311 #if defined (emacs) && defined (REL_ALLOC) |
5312 { | |
5313 /* If the allocations above (or the call to setup_syntax_cache() in | |
5314 re_match_2) caused a rel-alloc relocation, then fix up the data | |
5315 pointers */ | |
1346 | 5316 Bytecount offset = offset_post_relocation (lispobj, orig_buftext); |
1333 | 5317 if (offset) |
5318 { | |
5319 string1 += offset; | |
5320 string2 += offset; | |
5321 } | |
5322 } | |
5323 #endif /* defined (emacs) && defined (REL_ALLOC) */ | |
5324 | |
428 | 5325 /* The starting position is bogus. */ |
5326 if (pos < 0 || pos > size1 + size2) | |
5327 { | |
5328 FREE_VARIABLES (); | |
5329 return -1; | |
5330 } | |
5331 | |
5332 /* Initialize subexpression text positions to -1 to mark ones that no | |
5333 start_memory/stop_memory has been seen for. Also initialize the | |
5334 register information struct. */ | |
5335 for (mcnt = 1; mcnt < num_regs; mcnt++) | |
5336 { | |
5337 regstart[mcnt] = regend[mcnt] | |
5338 = old_regstart[mcnt] = old_regend[mcnt] = REG_UNSET_VALUE; | |
5339 | |
5340 REG_MATCH_NULL_STRING_P (reg_info[mcnt]) = MATCH_NULL_UNSET_VALUE; | |
5341 IS_ACTIVE (reg_info[mcnt]) = 0; | |
5342 MATCHED_SOMETHING (reg_info[mcnt]) = 0; | |
5343 EVER_MATCHED_SOMETHING (reg_info[mcnt]) = 0; | |
5344 } | |
5345 /* We move `string1' into `string2' if the latter's empty -- but not if | |
5346 `string1' is null. */ | |
5347 if (size2 == 0 && string1 != NULL) | |
5348 { | |
5349 string2 = string1; | |
5350 size2 = size1; | |
5351 string1 = 0; | |
5352 size1 = 0; | |
5353 } | |
5354 end1 = string1 + size1; | |
5355 end2 = string2 + size2; | |
5356 | |
5357 /* Compute where to stop matching, within the two strings. */ | |
5358 if (stop <= size1) | |
5359 { | |
5360 end_match_1 = string1 + stop; | |
5361 end_match_2 = string2; | |
5362 } | |
5363 else | |
5364 { | |
5365 end_match_1 = end1; | |
5366 end_match_2 = string2 + stop - size1; | |
5367 } | |
5368 | |
5369 /* `p' scans through the pattern as `d' scans through the data. | |
5370 `dend' is the end of the input string that `d' points within. `d' | |
5371 is advanced into the following input string whenever necessary, but | |
5372 this happens before fetching; therefore, at the beginning of the | |
5373 loop, `d' can be pointing at the end of a string, but it cannot | |
5374 equal `string2'. */ | |
5375 if (size1 > 0 && pos <= size1) | |
5376 { | |
5377 d = string1 + pos; | |
5378 dend = end_match_1; | |
5379 } | |
5380 else | |
5381 { | |
5382 d = string2 + pos - size1; | |
5383 dend = end_match_2; | |
5384 } | |
5385 | |
5041 | 5386 DEBUG_MATCH_PRINT1 ("The compiled pattern is: \n"); |
5387 DEBUG_MATCH_PRINT_COMPILED_PATTERN (bufp, p, pend); | |
5388 DEBUG_MATCH_PRINT1 ("The string to match is: `"); | |
5389 DEBUG_MATCH_PRINT_DOUBLE_STRING (d, string1, size1, string2, size2); | |
5390 DEBUG_MATCH_PRINT1 ("'\n"); | |
428 | 5391 |
5392 /* This loops over pattern commands. It exits by returning from the | |
5393 function if the match is complete, or it drops through if the match | |
5394 fails at this starting point in the input data. */ | |
5395 for (;;) | |
5396 { | |
5041 | 5397 DEBUG_MATCH_PRINT2 ("\n0x%lx: ", (long) p); |
428 | 5398 #ifdef emacs /* XEmacs added, w/removal of immediate_quit */ |
5399 if (!no_quit_in_re_search) | |
1333 | 5400 { |
5401 BEGIN_REGEX_MALLOC_OK (); | |
5402 QUIT; | |
5403 END_REGEX_MALLOC_OK (); | |
1346 | 5404 RE_MATCH_RELOCATE_MOVEABLE_DATA_POINTERS (); |
1333 | 5405 } |
428 | 5406 #endif |
5407 | |
5408 if (p == pend) | |
5409 { /* End of pattern means we might have succeeded. */ | |
5041 | 5410 DEBUG_MATCH_PRINT1 ("end of pattern ... "); |
428 | 5411 |
5412 /* If we haven't matched the entire string, and we want the | |
5413 longest match, try backtracking. */ | |
5414 if (d != end_match_2) | |
5415 { | |
5416 same_str_p = (FIRST_STRING_P (match_end) | |
5417 == MATCHING_IN_FIRST_STRING); | |
5418 | |
5419 /* AIX compiler got confused when this was combined | |
5420 with the previous declaration. */ | |
5421 if (same_str_p) | |
5422 best_match_p = d > match_end; | |
5423 else | |
5424 best_match_p = !MATCHING_IN_FIRST_STRING; | |
5425 | |
5041 | 5426 DEBUG_MATCH_PRINT1 ("backtracking.\n"); |
428 | 5427 |
5428 if (!FAIL_STACK_EMPTY ()) | |
5429 { /* More failure points to try. */ | |
5430 | |
5431 /* If exceeds best match so far, save it. */ | |
5432 if (!best_regs_set || best_match_p) | |
5433 { | |
5434 best_regs_set = true; | |
5435 match_end = d; | |
5436 | |
5041 | 5437 DEBUG_MATCH_PRINT1 ("\nSAVING match as best so far.\n"); |
428 | 5438 |
5439 for (mcnt = 1; mcnt < num_regs; mcnt++) | |
5440 { | |
5441 best_regstart[mcnt] = regstart[mcnt]; | |
5442 best_regend[mcnt] = regend[mcnt]; | |
5443 } | |
5444 } | |
5445 goto fail; | |
5446 } | |
5447 | |
5448 /* If no failure points, don't restore garbage. And if | |
5449 last match is real best match, don't restore second | |
5450 best one. */ | |
5451 else if (best_regs_set && !best_match_p) | |
5452 { | |
5453 restore_best_regs: | |
5454 /* Restore best match. It may happen that `dend == | |
5455 end_match_1' while the restored d is in string2. | |
5456 For example, the pattern `x.*y.*z' against the | |
5457 strings `x-' and `y-z-', if the two strings are | |
5458 not consecutive in memory. */ | |
5041 | 5459 DEBUG_MATCH_PRINT1 ("Restoring best registers.\n"); |
428 | 5460 |
5461 d = match_end; | |
5462 dend = ((d >= string1 && d <= end1) | |
5463 ? end_match_1 : end_match_2); | |
5464 | |
5465 for (mcnt = 1; mcnt < num_regs; mcnt++) | |
5466 { | |
5467 regstart[mcnt] = best_regstart[mcnt]; | |
5468 regend[mcnt] = best_regend[mcnt]; | |
5469 } | |
5470 } | |
5471 } /* d != end_match_2 */ | |
5472 | |
5473 succeed_label: | |
5041 | 5474 DEBUG_MATCH_PRINT1 ("Accepting match.\n"); |
428 | 5475 |
5476 /* If caller wants register contents data back, do it. */ | |
1028 | 5477 { |
5478 int num_nonshy_regs = bufp->re_nsub + 1; | |
5479 if (regs && !bufp->no_sub) | |
5480 { | |
5481 /* Have the register data arrays been allocated? */ | |
5482 if (bufp->regs_allocated == REGS_UNALLOCATED) | |
5483 { /* No. So allocate them with malloc. We need one | |
5484 extra element beyond `num_regs' for the `-1' marker | |
5485 GNU code uses. */ | |
5486 regs->num_regs = MAX (RE_NREGS, num_nonshy_regs + 1); | |
1333 | 5487 BEGIN_REGEX_MALLOC_OK (); |
1028 | 5488 regs->start = TALLOC (regs->num_regs, regoff_t); |
5489 regs->end = TALLOC (regs->num_regs, regoff_t); | |
1333 | 5490 END_REGEX_MALLOC_OK (); |
5491 RE_MATCH_RELOCATE_MOVEABLE_DATA_POINTERS (); | |
1028 | 5492 if (regs->start == NULL || regs->end == NULL) |
5493 { | |
5494 FREE_VARIABLES (); | |
5495 return -2; | |
5496 } | |
5497 bufp->regs_allocated = REGS_REALLOCATE; | |
5498 } | |
5499 else if (bufp->regs_allocated == REGS_REALLOCATE) | |
5500 { /* Yes. If we need more elements than were already | |
5501 allocated, reallocate them. If we need fewer, just | |
5502 leave it alone. */ | |
5503 if (regs->num_regs < num_nonshy_regs + 1) | |
5504 { | |
5505 regs->num_regs = num_nonshy_regs + 1; | |
1333 | 5506 BEGIN_REGEX_MALLOC_OK (); |
1028 | 5507 RETALLOC (regs->start, regs->num_regs, regoff_t); |
5508 RETALLOC (regs->end, regs->num_regs, regoff_t); | |
1333 | 5509 END_REGEX_MALLOC_OK (); |
5510 RE_MATCH_RELOCATE_MOVEABLE_DATA_POINTERS (); | |
1028 | 5511 if (regs->start == NULL || regs->end == NULL) |
5512 { | |
5513 FREE_VARIABLES (); | |
5514 return -2; | |
5515 } | |
5516 } | |
5517 } | |
5518 else | |
5519 { | |
5520 /* The braces fend off a "empty body in an else-statement" | |
5521 warning under GCC when assert expands to nothing. */ | |
5522 assert (bufp->regs_allocated == REGS_FIXED); | |
5523 } | |
5524 | |
5525 /* Convert the pointer data in `regstart' and `regend' to | |
5526 indices. Register zero has to be set differently, | |
5527 since we haven't kept track of any info for it. */ | |
5528 if (regs->num_regs > 0) | |
5529 { | |
5530 regs->start[0] = pos; | |
5531 regs->end[0] = (MATCHING_IN_FIRST_STRING | |
5532 ? ((regoff_t) (d - string1)) | |
5533 : ((regoff_t) (d - string2 + size1))); | |
5534 } | |
5535 | |
2639 | 5536 /* Map over the NUM_NONSHY_REGS non-shy internal registers. |
5537 Copy each into the corresponding external register. | |
5538 MCNT indexes external registers. */ | |
1028 | 5539 for (mcnt = 1; mcnt < MIN (num_nonshy_regs, regs->num_regs); |
5540 mcnt++) | |
5541 { | |
5542 int internal_reg = bufp->external_to_internal_register[mcnt]; | |
5543 if (REG_UNSET (regstart[internal_reg]) || | |
5544 REG_UNSET (regend[internal_reg])) | |
5545 regs->start[mcnt] = regs->end[mcnt] = -1; | |
5546 else | |
5547 { | |
5548 regs->start[mcnt] = | |
5549 (regoff_t) POINTER_TO_OFFSET (regstart[internal_reg]); | |
5550 regs->end[mcnt] = | |
5551 (regoff_t) POINTER_TO_OFFSET (regend[internal_reg]); | |
5552 } | |
5553 } | |
5554 } /* regs && !bufp->no_sub */ | |
5555 | |
5556 /* If we have regs and the regs structure has more elements than | |
2639 | 5557 were in the pattern, set the extra elements starting with |
5558 NUM_NONSHY_REGS to -1. If we (re)allocated the registers, | |
5559 this is the case, because we always allocate enough to have | |
5560 at least one -1 at the end. | |
1028 | 5561 |
5562 We do this even when no_sub is set because some applications | |
5563 (XEmacs) reuse register structures which may contain stale | |
5564 information, and permit attempts to access those registers. | |
5565 | |
5566 It would be possible to require the caller to do this, but we'd | |
5567 have to change the API for this function to reflect that, and | |
1425 | 5568 audit all callers. Note: as of 2003-04-17 callers in XEmacs |
5569 do clear the registers, but it's safer to leave this code in | |
5570 because of reallocation. | |
5571 */ | |
1028 | 5572 if (regs && regs->num_regs > 0) |
5573 for (mcnt = num_nonshy_regs; mcnt < regs->num_regs; mcnt++) | |
5574 regs->start[mcnt] = regs->end[mcnt] = -1; | |
5575 } | |
5041 | 5576 DEBUG_MATCH_PRINT4 ("%u failure points pushed, %u popped (%u remain).\n", |
428 | 5577 nfailure_points_pushed, nfailure_points_popped, |
5578 nfailure_points_pushed - nfailure_points_popped); | |
5041 | 5579 DEBUG_MATCH_PRINT2 ("%u registers pushed.\n", num_regs_pushed); |
428 | 5580 |
5581 mcnt = d - pos - (MATCHING_IN_FIRST_STRING | |
5582 ? string1 | |
5583 : string2 - size1); | |
5584 | |
5041 | 5585 DEBUG_MATCH_PRINT2 ("Returning %d from re_match_2.\n", mcnt); |
428 | 5586 |
5587 FREE_VARIABLES (); | |
5588 return mcnt; | |
5589 } | |
5590 | |
5591 /* Otherwise match next pattern command. */ | |
4759
aa5ed11f473b
Remove support for obsolete systems. See xemacs-patches message with ID
Jerry James <james@xemacs.org>
parents:
4750
diff
changeset
|
5592 switch ((re_opcode_t) *p++) |
428 | 5593 { |
5594 /* Ignore these. Used to ignore the n of succeed_n's which | |
5595 currently have n == 0. */ | |
5596 case no_op: | |
5041 | 5597 DEBUG_MATCH_PRINT1 ("EXECUTING no_op.\n"); |
428 | 5598 break; |
5599 | |
5600 case succeed: | |
5041 | 5601 DEBUG_MATCH_PRINT1 ("EXECUTING succeed.\n"); |
428 | 5602 goto succeed_label; |
5603 | |
826 | 5604 /* Match exactly a string of length n in the pattern. The |
5605 following byte in the pattern defines n, and the n bytes after | |
5606 that make up the string to match. (Under Mule, this will be in | |
5607 the default internal format.) */ | |
428 | 5608 case exactn: |
5609 mcnt = *p++; | |
5041 | 5610 DEBUG_MATCH_PRINT2 ("EXECUTING exactn %d.\n", mcnt); |
428 | 5611 |
5612 /* This is written out as an if-else so we don't waste time | |
5613 testing `translate' inside the loop. */ | |
446 | 5614 if (TRANSLATE_P (translate)) |
428 | 5615 { |
5616 do | |
5617 { | |
446 | 5618 #ifdef MULE |
5619 Bytecount pat_len; | |
5620 | |
450 | 5621 REGEX_PREFETCH (); |
867 | 5622 if (RE_TRANSLATE_1 (itext_ichar_fmt (d, fmt, lispobj)) |
5623 != itext_ichar (p)) | |
428 | 5624 goto fail; |
446 | 5625 |
867 | 5626 pat_len = itext_ichar_len (p); |
446 | 5627 p += pat_len; |
867 | 5628 INC_IBYTEPTR_FMT (d, fmt); |
446 | 5629 |
5630 mcnt -= pat_len; | |
5631 #else /* not MULE */ | |
450 | 5632 REGEX_PREFETCH (); |
826 | 5633 if ((unsigned char) RE_TRANSLATE_1 (*d++) != *p++) |
446 | 5634 goto fail; |
5635 mcnt--; | |
5636 #endif | |
428 | 5637 } |
446 | 5638 while (mcnt > 0); |
428 | 5639 } |
5640 else | |
5641 { | |
826 | 5642 #ifdef MULE |
5643 /* If buffer format is default, then we can shortcut and just | |
5644 compare the text directly, byte by byte. Otherwise, we | |
5645 need to go character by character. */ | |
5646 if (fmt != FORMAT_DEFAULT) | |
428 | 5647 { |
826 | 5648 do |
5649 { | |
5650 Bytecount pat_len; | |
5651 | |
5652 REGEX_PREFETCH (); | |
867 | 5653 if (itext_ichar_fmt (d, fmt, lispobj) != |
5654 itext_ichar (p)) | |
826 | 5655 goto fail; |
5656 | |
867 | 5657 pat_len = itext_ichar_len (p); |
826 | 5658 p += pat_len; |
867 | 5659 INC_IBYTEPTR_FMT (d, fmt); |
826 | 5660 |
5661 mcnt -= pat_len; | |
5662 } | |
5663 while (mcnt > 0); | |
428 | 5664 } |
826 | 5665 else |
5666 #endif | |
5667 { | |
5668 do | |
5669 { | |
5670 REGEX_PREFETCH (); | |
5671 if (*d++ != *p++) goto fail; | |
5672 mcnt--; | |
5673 } | |
5674 while (mcnt > 0); | |
5675 } | |
428 | 5676 } |
5677 SET_REGS_MATCHED (); | |
5678 break; | |
5679 | |
5680 | |
5681 /* Match any character except possibly a newline or a null. */ | |
5682 case anychar: | |
5041 | 5683 DEBUG_MATCH_PRINT1 ("EXECUTING anychar.\n"); |
428 | 5684 |
450 | 5685 REGEX_PREFETCH (); |
428 | 5686 |
826 | 5687 if ((!(bufp->syntax & RE_DOT_NEWLINE) && |
867 | 5688 RE_TRANSLATE (itext_ichar_fmt (d, fmt, lispobj)) == '\n') |
826 | 5689 || (bufp->syntax & RE_DOT_NOT_NULL && |
867 | 5690 RE_TRANSLATE (itext_ichar_fmt (d, fmt, lispobj)) == |
826 | 5691 '\000')) |
428 | 5692 goto fail; |
5693 | |
5694 SET_REGS_MATCHED (); | |
5041 | 5695 DEBUG_MATCH_PRINT2 (" Matched `%d'.\n", *d); |
867 | 5696 INC_IBYTEPTR_FMT (d, fmt); /* XEmacs change */ |
428 | 5697 break; |
5698 | |
5699 | |
5700 case charset: | |
5701 case charset_not: | |
5702 { | |
1414 | 5703 REGISTER Ichar c; |
460 | 5704 re_bool not_p = (re_opcode_t) *(p - 1) == charset_not; |
458 | 5705 |
5041 | 5706 DEBUG_MATCH_PRINT2 ("EXECUTING charset%s.\n", not_p ? "_not" : ""); |
428 | 5707 |
450 | 5708 REGEX_PREFETCH (); |
867 | 5709 c = itext_ichar_fmt (d, fmt, lispobj); |
826 | 5710 c = RE_TRANSLATE (c); /* The character to match. */ |
428 | 5711 |
647 | 5712 /* Cast to `unsigned int' instead of `unsigned char' in case the |
428 | 5713 bit list is a full 32 bytes long. */ |
1414 | 5714 if ((unsigned int)c < (unsigned int) (*p * BYTEWIDTH) |
428 | 5715 && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH))) |
458 | 5716 not_p = !not_p; |
428 | 5717 |
5718 p += 1 + *p; | |
5719 | |
458 | 5720 if (!not_p) goto fail; |
428 | 5721 |
5722 SET_REGS_MATCHED (); | |
867 | 5723 INC_IBYTEPTR_FMT (d, fmt); /* XEmacs change */ |
428 | 5724 break; |
5725 } | |
5726 | |
5727 #ifdef MULE | |
5728 case charset_mule: | |
5729 case charset_mule_not: | |
5730 { | |
867 | 5731 REGISTER Ichar c; |
460 | 5732 re_bool not_p = (re_opcode_t) *(p - 1) == charset_mule_not; |
5648
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
5733 Bitbyte class_bits = *p++; |
458 | 5734 |
5041 | 5735 DEBUG_MATCH_PRINT2 ("EXECUTING charset_mule%s.\n", not_p ? "_not" : ""); |
450 | 5736 REGEX_PREFETCH (); |
867 | 5737 c = itext_ichar_fmt (d, fmt, lispobj); |
826 | 5738 c = RE_TRANSLATE (c); /* The character to match. */ |
428 | 5739 |
5648
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
5740 if ((class_bits && |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
5741 ((class_bits & BIT_ALPHA && ISALPHA (c)) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
5742 || (class_bits & BIT_SPACE && ISSPACE (c)) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
5743 || (class_bits & BIT_PUNCT && ISPUNCT (c)) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
5744 || (class_bits & BIT_WORD && ISWORD (c)) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
5745 || (TRANSLATE_P (translate) ? |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
5746 (class_bits & (BIT_UPPER | BIT_LOWER) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
5747 && !NOCASEP (lispbuf, c)) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
5748 : ((class_bits & BIT_UPPER && ISUPPER (c)) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
5749 || (class_bits & BIT_LOWER && ISLOWER (c)))))) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
5750 || EQ (Qt, unified_range_table_lookup (p, c, Qnil))) |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
5751 { |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
5752 not_p = !not_p; |
3f4a234f4672
Support non-ASCII correctly in character classes, test this.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
5753 } |
428 | 5754 |
5755 p += unified_range_table_bytes_used (p); | |
5756 | |
458 | 5757 if (!not_p) goto fail; |
428 | 5758 |
5759 SET_REGS_MATCHED (); | |
867 | 5760 INC_IBYTEPTR_FMT (d, fmt); |
428 | 5761 break; |
5762 } | |
5763 #endif /* MULE */ | |
5764 | |
5765 | |
5766 /* The beginning of a group is represented by start_memory. | |
5767 The arguments are the register number in the next byte, and the | |
5768 number of groups inner to this one in the next. The text | |
5769 matched within the group is recorded (in the internal | |
5770 registers data structure) under the register number. */ | |
5771 case start_memory: | |
5041 | 5772 DEBUG_MATCH_PRINT3 ("EXECUTING start_memory %d (%d):\n", *p, p[1]); |
428 | 5773 |
5774 /* Find out if this group can match the empty string. */ | |
5775 p1 = p; /* To send to group_match_null_string_p. */ | |
5776 | |
5777 if (REG_MATCH_NULL_STRING_P (reg_info[*p]) == MATCH_NULL_UNSET_VALUE) | |
2639 | 5778 REG_MATCH_NULL_STRING_P (reg_info[*p]) |
5779 = group_match_null_string_p (&p1, pend, reg_info); | |
5780 | |
5041 | 5781 DEBUG_MATCH_PRINT2 (" group CAN%s match null string\n", |
2639 | 5782 REG_MATCH_NULL_STRING_P (reg_info[*p]) ? "NOT" : ""); |
428 | 5783 |
5784 /* Save the position in the string where we were the last time | |
5785 we were at this open-group operator in case the group is | |
5786 operated upon by a repetition operator, e.g., with `(a*)*b' | |
5787 against `ab'; then we want to ignore where we are now in | |
5788 the string in case this attempt to match fails. */ | |
5789 old_regstart[*p] = REG_MATCH_NULL_STRING_P (reg_info[*p]) | |
5790 ? REG_UNSET (regstart[*p]) ? d : regstart[*p] | |
5791 : regstart[*p]; | |
5041 | 5792 DEBUG_MATCH_PRINT2 (" old_regstart: %d\n", |
428 | 5793 POINTER_TO_OFFSET (old_regstart[*p])); |
5794 | |
5795 regstart[*p] = d; | |
5041 | 5796 DEBUG_MATCH_PRINT2 (" regstart: %d\n", POINTER_TO_OFFSET (regstart[*p])); |
428 | 5797 |
5798 IS_ACTIVE (reg_info[*p]) = 1; | |
5799 MATCHED_SOMETHING (reg_info[*p]) = 0; | |
5800 | |
5801 /* Clear this whenever we change the register activity status. */ | |
5802 set_regs_matched_done = 0; | |
5803 | |
5804 /* This is the new highest active register. */ | |
5805 highest_active_reg = *p; | |
5806 | |
5807 /* If nothing was active before, this is the new lowest active | |
5808 register. */ | |
5809 if (lowest_active_reg == NO_LOWEST_ACTIVE_REG) | |
5810 lowest_active_reg = *p; | |
5811 | |
5812 /* Move past the register number and inner group count. */ | |
5813 p += 2; | |
5814 just_past_start_mem = p; | |
5815 | |
5816 break; | |
5817 | |
5818 | |
5819 /* The stop_memory opcode represents the end of a group. Its | |
5820 arguments are the same as start_memory's: the register | |
5821 number, and the number of inner groups. */ | |
5822 case stop_memory: | |
5041 | 5823 DEBUG_MATCH_PRINT3 ("EXECUTING stop_memory %d (%d):\n", *p, p[1]); |
428 | 5824 |
5825 /* We need to save the string position the last time we were at | |
5826 this close-group operator in case the group is operated | |
5827 upon by a repetition operator, e.g., with `((a*)*(b*)*)*' | |
5828 against `aba'; then we want to ignore where we are now in | |
5829 the string in case this attempt to match fails. */ | |
5830 old_regend[*p] = REG_MATCH_NULL_STRING_P (reg_info[*p]) | |
5831 ? REG_UNSET (regend[*p]) ? d : regend[*p] | |
5832 : regend[*p]; | |
5041 | 5833 DEBUG_MATCH_PRINT2 (" old_regend: %d\n", |
428 | 5834 POINTER_TO_OFFSET (old_regend[*p])); |
5835 | |
5836 regend[*p] = d; | |
5041 | 5837 DEBUG_MATCH_PRINT2 (" regend: %d\n", POINTER_TO_OFFSET (regend[*p])); |
428 | 5838 |
5839 /* This register isn't active anymore. */ | |
5840 IS_ACTIVE (reg_info[*p]) = 0; | |
5841 | |
5842 /* Clear this whenever we change the register activity status. */ | |
5843 set_regs_matched_done = 0; | |
5844 | |
5845 /* If this was the only register active, nothing is active | |
5846 anymore. */ | |
5847 if (lowest_active_reg == highest_active_reg) | |
5848 { | |
5849 lowest_active_reg = NO_LOWEST_ACTIVE_REG; | |
5850 highest_active_reg = NO_HIGHEST_ACTIVE_REG; | |
5851 } | |
5852 else | |
5853 { /* We must scan for the new highest active register, since | |
5854 it isn't necessarily one less than now: consider | |
5855 (a(b)c(d(e)f)g). When group 3 ends, after the f), the | |
5856 new highest active register is 1. */ | |
5857 unsigned char r = *p - 1; | |
5858 while (r > 0 && !IS_ACTIVE (reg_info[r])) | |
5859 r--; | |
5860 | |
5861 /* If we end up at register zero, that means that we saved | |
5862 the registers as the result of an `on_failure_jump', not | |
5863 a `start_memory', and we jumped to past the innermost | |
5864 `stop_memory'. For example, in ((.)*) we save | |
5865 registers 1 and 2 as a result of the *, but when we pop | |
5866 back to the second ), we are at the stop_memory 1. | |
5867 Thus, nothing is active. */ | |
5868 if (r == 0) | |
5869 { | |
5870 lowest_active_reg = NO_LOWEST_ACTIVE_REG; | |
5871 highest_active_reg = NO_HIGHEST_ACTIVE_REG; | |
5872 } | |
5873 else | |
5874 { | |
5875 highest_active_reg = r; | |
5876 | |
5877 /* 98/9/21 jhod: We've also gotta set lowest_active_reg, don't we? */ | |
5878 r = 1; | |
5879 while (r < highest_active_reg && !IS_ACTIVE(reg_info[r])) | |
5880 r++; | |
5881 lowest_active_reg = r; | |
5882 } | |
5883 } | |
5884 | |
5885 /* If just failed to match something this time around with a | |
5886 group that's operated on by a repetition operator, try to | |
5887 force exit from the ``loop'', and restore the register | |
5888 information for this group that we had before trying this | |
5889 last match. */ | |
5890 if ((!MATCHED_SOMETHING (reg_info[*p]) | |
5891 || just_past_start_mem == p - 1) | |
5892 && (p + 2) < pend) | |
5893 { | |
460 | 5894 re_bool is_a_jump_n = false; |
428 | 5895 |
5896 p1 = p + 2; | |
5897 mcnt = 0; | |
5898 switch ((re_opcode_t) *p1++) | |
5899 { | |
5900 case jump_n: | |
5901 is_a_jump_n = true; | |
5902 case pop_failure_jump: | |
5903 case maybe_pop_jump: | |
5904 case jump: | |
5905 case dummy_failure_jump: | |
5906 EXTRACT_NUMBER_AND_INCR (mcnt, p1); | |
5907 if (is_a_jump_n) | |
5908 p1 += 2; | |
5909 break; | |
5910 | |
5911 default: | |
5912 /* do nothing */ ; | |
5913 } | |
5914 p1 += mcnt; | |
5915 | |
5916 /* If the next operation is a jump backwards in the pattern | |
5917 to an on_failure_jump right before the start_memory | |
5918 corresponding to this stop_memory, exit from the loop | |
5919 by forcing a failure after pushing on the stack the | |
5920 on_failure_jump's jump in the pattern, and d. */ | |
5921 if (mcnt < 0 && (re_opcode_t) *p1 == on_failure_jump | |
5922 && (re_opcode_t) p1[3] == start_memory && p1[4] == *p) | |
5923 { | |
5924 /* If this group ever matched anything, then restore | |
5925 what its registers were before trying this last | |
5926 failed match, e.g., with `(a*)*b' against `ab' for | |
5927 regstart[1], and, e.g., with `((a*)*(b*)*)*' | |
5928 against `aba' for regend[3]. | |
5929 | |
5930 Also restore the registers for inner groups for, | |
5931 e.g., `((a*)(b*))*' against `aba' (register 3 would | |
5932 otherwise get trashed). */ | |
5933 | |
5934 if (EVER_MATCHED_SOMETHING (reg_info[*p])) | |
5935 { | |
647 | 5936 int r; |
428 | 5937 |
5938 EVER_MATCHED_SOMETHING (reg_info[*p]) = 0; | |
5939 | |
5940 /* Restore this and inner groups' (if any) registers. */ | |
5941 for (r = *p; r < *p + *(p + 1); r++) | |
5942 { | |
5943 regstart[r] = old_regstart[r]; | |
5944 | |
5945 /* xx why this test? */ | |
5946 if (old_regend[r] >= regstart[r]) | |
5947 regend[r] = old_regend[r]; | |
5948 } | |
5949 } | |
5950 p1++; | |
5951 EXTRACT_NUMBER_AND_INCR (mcnt, p1); | |
5952 PUSH_FAILURE_POINT (p1 + mcnt, d, -2); | |
5953 | |
5954 goto fail; | |
5955 } | |
5956 } | |
5957 | |
5958 /* Move past the register number and the inner group count. */ | |
5959 p += 2; | |
5960 break; | |
5961 | |
5962 | |
5963 /* \<digit> has been turned into a `duplicate' command which is | |
502 | 5964 followed by the numeric value of <digit> as the register number. |
5965 (Already passed through external-to-internal-register mapping, | |
5966 so it refers to the actual group number, not the non-shy-only | |
5967 numbering used in the external world.) */ | |
428 | 5968 case duplicate: |
5969 { | |
446 | 5970 REGISTER re_char *d2, *dend2; |
502 | 5971 /* Get which register to match against. */ |
5972 int regno = *p++; | |
5041 | 5973 DEBUG_MATCH_PRINT2 ("EXECUTING duplicate %d.\n", regno); |
428 | 5974 |
5975 /* Can't back reference a group which we've never matched. */ | |
5976 if (REG_UNSET (regstart[regno]) || REG_UNSET (regend[regno])) | |
5977 goto fail; | |
5978 | |
5979 /* Where in input to try to start matching. */ | |
5980 d2 = regstart[regno]; | |
5981 | |
5982 /* Where to stop matching; if both the place to start and | |
5983 the place to stop matching are in the same string, then | |
5984 set to the place to stop, otherwise, for now have to use | |
5985 the end of the first string. */ | |
5986 | |
5987 dend2 = ((FIRST_STRING_P (regstart[regno]) | |
5988 == FIRST_STRING_P (regend[regno])) | |
5989 ? regend[regno] : end_match_1); | |
5990 for (;;) | |
5991 { | |
5992 /* If necessary, advance to next segment in register | |
5993 contents. */ | |
5994 while (d2 == dend2) | |
5995 { | |
5996 if (dend2 == end_match_2) break; | |
5997 if (dend2 == regend[regno]) break; | |
5998 | |
5999 /* End of string1 => advance to string2. */ | |
6000 d2 = string2; | |
6001 dend2 = regend[regno]; | |
6002 } | |
6003 /* At end of register contents => success */ | |
6004 if (d2 == dend2) break; | |
6005 | |
6006 /* If necessary, advance to next segment in data. */ | |
450 | 6007 REGEX_PREFETCH (); |
428 | 6008 |
6009 /* How many characters left in this segment to match. */ | |
6010 mcnt = dend - d; | |
6011 | |
6012 /* Want how many consecutive characters we can match in | |
6013 one shot, so, if necessary, adjust the count. */ | |
6014 if (mcnt > dend2 - d2) | |
6015 mcnt = dend2 - d2; | |
6016 | |
6017 /* Compare that many; failure if mismatch, else move | |
6018 past them. */ | |
446 | 6019 if (TRANSLATE_P (translate) |
826 | 6020 ? bcmp_translate (d, d2, mcnt, translate |
6021 #ifdef emacs | |
6022 , fmt, lispobj | |
6023 #endif | |
6024 ) | |
428 | 6025 : memcmp (d, d2, mcnt)) |
6026 goto fail; | |
6027 d += mcnt, d2 += mcnt; | |
6028 | |
6029 /* Do this because we've match some characters. */ | |
6030 SET_REGS_MATCHED (); | |
6031 } | |
6032 } | |
6033 break; | |
6034 | |
6035 | |
6036 /* begline matches the empty string at the beginning of the string | |
6037 (unless `not_bol' is set in `bufp'), and, if | |
6038 `newline_anchor' is set, after newlines. */ | |
6039 case begline: | |
5041 | 6040 DEBUG_MATCH_PRINT1 ("EXECUTING begline.\n"); |
428 | 6041 |
6042 if (AT_STRINGS_BEG (d)) | |
6043 { | |
6044 if (!bufp->not_bol) break; | |
6045 } | |
826 | 6046 else |
6047 { | |
6048 re_char *d2 = d; | |
867 | 6049 DEC_IBYTEPTR (d2); |
6050 if (itext_ichar_ascii_fmt (d2, fmt, lispobj) == '\n' && | |
826 | 6051 bufp->newline_anchor) |
6052 break; | |
6053 } | |
428 | 6054 /* In all other cases, we fail. */ |
6055 goto fail; | |
6056 | |
6057 | |
6058 /* endline is the dual of begline. */ | |
6059 case endline: | |
5041 | 6060 DEBUG_MATCH_PRINT1 ("EXECUTING endline.\n"); |
428 | 6061 |
6062 if (AT_STRINGS_END (d)) | |
6063 { | |
6064 if (!bufp->not_eol) break; | |
6065 } | |
6066 | |
6067 /* We have to ``prefetch'' the next character. */ | |
826 | 6068 else if ((d == end1 ? |
867 | 6069 itext_ichar_ascii_fmt (string2, fmt, lispobj) : |
6070 itext_ichar_ascii_fmt (d, fmt, lispobj)) == '\n' | |
428 | 6071 && bufp->newline_anchor) |
6072 { | |
6073 break; | |
6074 } | |
6075 goto fail; | |
6076 | |
6077 | |
6078 /* Match at the very beginning of the data. */ | |
6079 case begbuf: | |
5041 | 6080 DEBUG_MATCH_PRINT1 ("EXECUTING begbuf.\n"); |
428 | 6081 if (AT_STRINGS_BEG (d)) |
6082 break; | |
6083 goto fail; | |
6084 | |
6085 | |
6086 /* Match at the very end of the data. */ | |
6087 case endbuf: | |
5041 | 6088 DEBUG_MATCH_PRINT1 ("EXECUTING endbuf.\n"); |
428 | 6089 if (AT_STRINGS_END (d)) |
6090 break; | |
6091 goto fail; | |
6092 | |
6093 | |
6094 /* on_failure_keep_string_jump is used to optimize `.*\n'. It | |
6095 pushes NULL as the value for the string on the stack. Then | |
6096 `pop_failure_point' will keep the current value for the | |
6097 string, instead of restoring it. To see why, consider | |
6098 matching `foo\nbar' against `.*\n'. The .* matches the foo; | |
6099 then the . fails against the \n. But the next thing we want | |
6100 to do is match the \n against the \n; if we restored the | |
6101 string value, we would be back at the foo. | |
6102 | |
6103 Because this is used only in specific cases, we don't need to | |
6104 check all the things that `on_failure_jump' does, to make | |
6105 sure the right things get saved on the stack. Hence we don't | |
6106 share its code. The only reason to push anything on the | |
6107 stack at all is that otherwise we would have to change | |
6108 `anychar's code to do something besides goto fail in this | |
6109 case; that seems worse than this. */ | |
6110 case on_failure_keep_string_jump: | |
5041 | 6111 DEBUG_MATCH_PRINT1 ("EXECUTING on_failure_keep_string_jump"); |
428 | 6112 |
6113 EXTRACT_NUMBER_AND_INCR (mcnt, p); | |
5041 | 6114 DEBUG_MATCH_PRINT3 (" %d (to 0x%lx):\n", mcnt, (long) (p + mcnt)); |
428 | 6115 |
446 | 6116 PUSH_FAILURE_POINT (p + mcnt, (unsigned char *) 0, -2); |
428 | 6117 break; |
6118 | |
6119 | |
6120 /* Uses of on_failure_jump: | |
6121 | |
6122 Each alternative starts with an on_failure_jump that points | |
6123 to the beginning of the next alternative. Each alternative | |
6124 except the last ends with a jump that in effect jumps past | |
6125 the rest of the alternatives. (They really jump to the | |
6126 ending jump of the following alternative, because tensioning | |
6127 these jumps is a hassle.) | |
6128 | |
6129 Repeats start with an on_failure_jump that points past both | |
6130 the repetition text and either the following jump or | |
6131 pop_failure_jump back to this on_failure_jump. */ | |
6132 case on_failure_jump: | |
6133 on_failure: | |
5041 | 6134 DEBUG_MATCH_PRINT1 ("EXECUTING on_failure_jump"); |
428 | 6135 |
6136 EXTRACT_NUMBER_AND_INCR (mcnt, p); | |
5041 | 6137 DEBUG_MATCH_PRINT3 (" %d (to 0x%lx)", mcnt, (long) (p + mcnt)); |
428 | 6138 |
6139 /* If this on_failure_jump comes right before a group (i.e., | |
6140 the original * applied to a group), save the information | |
6141 for that group and all inner ones, so that if we fail back | |
6142 to this point, the group's information will be correct. | |
6143 For example, in \(a*\)*\1, we need the preceding group, | |
6144 and in \(\(a*\)b*\)\2, we need the inner group. */ | |
6145 | |
6146 /* We can't use `p' to check ahead because we push | |
6147 a failure point to `p + mcnt' after we do this. */ | |
6148 p1 = p; | |
6149 | |
6150 /* We need to skip no_op's before we look for the | |
6151 start_memory in case this on_failure_jump is happening as | |
6152 the result of a completed succeed_n, as in \(a\)\{1,3\}b\1 | |
6153 against aba. */ | |
6154 while (p1 < pend && (re_opcode_t) *p1 == no_op) | |
6155 p1++; | |
6156 | |
6157 if (p1 < pend && (re_opcode_t) *p1 == start_memory) | |
6158 { | |
6159 /* We have a new highest active register now. This will | |
6160 get reset at the start_memory we are about to get to, | |
6161 but we will have saved all the registers relevant to | |
6162 this repetition op, as described above. */ | |
6163 highest_active_reg = *(p1 + 1) + *(p1 + 2); | |
6164 if (lowest_active_reg == NO_LOWEST_ACTIVE_REG) | |
6165 lowest_active_reg = *(p1 + 1); | |
6166 } | |
6167 | |
5041 | 6168 DEBUG_MATCH_PRINT1 (":\n"); |
428 | 6169 PUSH_FAILURE_POINT (p + mcnt, d, -2); |
6170 break; | |
6171 | |
6172 | |
6173 /* A smart repeat ends with `maybe_pop_jump'. | |
6174 We change it to either `pop_failure_jump' or `jump'. */ | |
6175 case maybe_pop_jump: | |
6176 EXTRACT_NUMBER_AND_INCR (mcnt, p); | |
5041 | 6177 DEBUG_MATCH_PRINT2 ("EXECUTING maybe_pop_jump %d.\n", mcnt); |
428 | 6178 { |
6179 REGISTER unsigned char *p2 = p; | |
6180 | |
6181 /* Compare the beginning of the repeat with what in the | |
6182 pattern follows its end. If we can establish that there | |
6183 is nothing that they would both match, i.e., that we | |
6184 would have to backtrack because of (as in, e.g., `a*a') | |
6185 then we can change to pop_failure_jump, because we'll | |
6186 never have to backtrack. | |
6187 | |
6188 This is not true in the case of alternatives: in | |
6189 `(a|ab)*' we do need to backtrack to the `ab' alternative | |
6190 (e.g., if the string was `ab'). But instead of trying to | |
6191 detect that here, the alternative has put on a dummy | |
6192 failure point which is what we will end up popping. */ | |
6193 | |
6194 /* Skip over open/close-group commands. | |
6195 If what follows this loop is a ...+ construct, | |
6196 look at what begins its body, since we will have to | |
6197 match at least one of that. */ | |
6198 while (1) | |
6199 { | |
6200 if (p2 + 2 < pend | |
6201 && ((re_opcode_t) *p2 == stop_memory | |
6202 || (re_opcode_t) *p2 == start_memory)) | |
6203 p2 += 3; | |
6204 else if (p2 + 6 < pend | |
6205 && (re_opcode_t) *p2 == dummy_failure_jump) | |
6206 p2 += 6; | |
6207 else | |
6208 break; | |
6209 } | |
6210 | |
6211 p1 = p + mcnt; | |
6212 /* p1[0] ... p1[2] are the `on_failure_jump' corresponding | |
6213 to the `maybe_finalize_jump' of this case. Examine what | |
6214 follows. */ | |
6215 | |
6216 /* If we're at the end of the pattern, we can change. */ | |
6217 if (p2 == pend) | |
6218 { | |
6219 /* Consider what happens when matching ":\(.*\)" | |
6220 against ":/". I don't really understand this code | |
6221 yet. */ | |
6222 p[-3] = (unsigned char) pop_failure_jump; | |
5041 | 6223 DEBUG_MATCH_PRINT1 |
428 | 6224 (" End of pattern: change to `pop_failure_jump'.\n"); |
6225 } | |
6226 | |
6227 else if ((re_opcode_t) *p2 == exactn | |
6228 || (bufp->newline_anchor && (re_opcode_t) *p2 == endline)) | |
6229 { | |
6230 REGISTER unsigned char c | |
6231 = *p2 == (unsigned char) endline ? '\n' : p2[2]; | |
6232 | |
6233 if ((re_opcode_t) p1[3] == exactn && p1[5] != c) | |
6234 { | |
6235 p[-3] = (unsigned char) pop_failure_jump; | |
5041 | 6236 DEBUG_MATCH_PRINT3 (" %c != %c => pop_failure_jump.\n", |
428 | 6237 c, p1[5]); |
6238 } | |
6239 | |
6240 else if ((re_opcode_t) p1[3] == charset | |
6241 || (re_opcode_t) p1[3] == charset_not) | |
6242 { | |
458 | 6243 int not_p = (re_opcode_t) p1[3] == charset_not; |
428 | 6244 |
6245 if (c < (unsigned char) (p1[4] * BYTEWIDTH) | |
6246 && p1[5 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH))) | |
458 | 6247 not_p = !not_p; |
6248 | |
6249 /* `not_p' is equal to 1 if c would match, which means | |
428 | 6250 that we can't change to pop_failure_jump. */ |
458 | 6251 if (!not_p) |
428 | 6252 { |
6253 p[-3] = (unsigned char) pop_failure_jump; | |
5041 | 6254 DEBUG_MATCH_PRINT1 (" No match => pop_failure_jump.\n"); |
428 | 6255 } |
6256 } | |
6257 } | |
6258 else if ((re_opcode_t) *p2 == charset) | |
6259 { | |
6260 #ifdef DEBUG | |
6261 REGISTER unsigned char c | |
6262 = *p2 == (unsigned char) endline ? '\n' : p2[2]; | |
6263 #endif | |
6264 | |
6265 if ((re_opcode_t) p1[3] == exactn | |
6266 && ! ((int) p2[1] * BYTEWIDTH > (int) p1[5] | |
6267 && (p2[2 + p1[5] / BYTEWIDTH] | |
6268 & (1 << (p1[5] % BYTEWIDTH))))) | |
6269 { | |
6270 p[-3] = (unsigned char) pop_failure_jump; | |
5041 | 6271 DEBUG_MATCH_PRINT3 (" %c != %c => pop_failure_jump.\n", |
428 | 6272 c, p1[5]); |
6273 } | |
6274 | |
6275 else if ((re_opcode_t) p1[3] == charset_not) | |
6276 { | |
6277 int idx; | |
6278 /* We win if the charset_not inside the loop | |
6279 lists every character listed in the charset after. */ | |
6280 for (idx = 0; idx < (int) p2[1]; idx++) | |
6281 if (! (p2[2 + idx] == 0 | |
6282 || (idx < (int) p1[4] | |
6283 && ((p2[2 + idx] & ~ p1[5 + idx]) == 0)))) | |
6284 break; | |
6285 | |
6286 if (idx == p2[1]) | |
6287 { | |
6288 p[-3] = (unsigned char) pop_failure_jump; | |
5041 | 6289 DEBUG_MATCH_PRINT1 (" No match => pop_failure_jump.\n"); |
428 | 6290 } |
6291 } | |
6292 else if ((re_opcode_t) p1[3] == charset) | |
6293 { | |
6294 int idx; | |
6295 /* We win if the charset inside the loop | |
6296 has no overlap with the one after the loop. */ | |
6297 for (idx = 0; | |
6298 idx < (int) p2[1] && idx < (int) p1[4]; | |
6299 idx++) | |
6300 if ((p2[2 + idx] & p1[5 + idx]) != 0) | |
6301 break; | |
6302 | |
6303 if (idx == p2[1] || idx == p1[4]) | |
6304 { | |
6305 p[-3] = (unsigned char) pop_failure_jump; | |
5041 | 6306 DEBUG_MATCH_PRINT1 (" No match => pop_failure_jump.\n"); |
428 | 6307 } |
6308 } | |
6309 } | |
6310 } | |
6311 p -= 2; /* Point at relative address again. */ | |
6312 if ((re_opcode_t) p[-1] != pop_failure_jump) | |
6313 { | |
6314 p[-1] = (unsigned char) jump; | |
5041 | 6315 DEBUG_MATCH_PRINT1 (" Match => jump.\n"); |
428 | 6316 goto unconditional_jump; |
6317 } | |
6318 /* Note fall through. */ | |
6319 | |
6320 | |
6321 /* The end of a simple repeat has a pop_failure_jump back to | |
6322 its matching on_failure_jump, where the latter will push a | |
6323 failure point. The pop_failure_jump takes off failure | |
6324 points put on by this pop_failure_jump's matching | |
6325 on_failure_jump; we got through the pattern to here from the | |
6326 matching on_failure_jump, so didn't fail. */ | |
6327 case pop_failure_jump: | |
6328 { | |
6329 /* We need to pass separate storage for the lowest and | |
6330 highest registers, even though we don't care about the | |
6331 actual values. Otherwise, we will restore only one | |
6332 register from the stack, since lowest will == highest in | |
6333 `pop_failure_point'. */ | |
647 | 6334 int dummy_low_reg, dummy_high_reg; |
428 | 6335 unsigned char *pdummy; |
446 | 6336 re_char *sdummy = NULL; |
428 | 6337 |
5041 | 6338 DEBUG_MATCH_PRINT1 ("EXECUTING pop_failure_jump.\n"); |
428 | 6339 POP_FAILURE_POINT (sdummy, pdummy, |
6340 dummy_low_reg, dummy_high_reg, | |
6341 reg_dummy, reg_dummy, reg_info_dummy); | |
6342 } | |
6343 /* Note fall through. */ | |
6344 | |
6345 | |
6346 /* Unconditionally jump (without popping any failure points). */ | |
6347 case jump: | |
6348 unconditional_jump: | |
6349 EXTRACT_NUMBER_AND_INCR (mcnt, p); /* Get the amount to jump. */ | |
5041 | 6350 DEBUG_MATCH_PRINT2 ("EXECUTING jump %d ", mcnt); |
428 | 6351 p += mcnt; /* Do the jump. */ |
5041 | 6352 DEBUG_MATCH_PRINT2 ("(to 0x%lx).\n", (long) p); |
428 | 6353 break; |
6354 | |
6355 | |
6356 /* We need this opcode so we can detect where alternatives end | |
6357 in `group_match_null_string_p' et al. */ | |
6358 case jump_past_alt: | |
5041 | 6359 DEBUG_MATCH_PRINT1 ("EXECUTING jump_past_alt.\n"); |
428 | 6360 goto unconditional_jump; |
6361 | |
6362 | |
6363 /* Normally, the on_failure_jump pushes a failure point, which | |
6364 then gets popped at pop_failure_jump. We will end up at | |
6365 pop_failure_jump, also, and with a pattern of, say, `a+', we | |
6366 are skipping over the on_failure_jump, so we have to push | |
6367 something meaningless for pop_failure_jump to pop. */ | |
6368 case dummy_failure_jump: | |
5041 | 6369 DEBUG_MATCH_PRINT1 ("EXECUTING dummy_failure_jump.\n"); |
428 | 6370 /* It doesn't matter what we push for the string here. What |
6371 the code at `fail' tests is the value for the pattern. */ | |
446 | 6372 PUSH_FAILURE_POINT ((unsigned char *) 0, (unsigned char *) 0, -2); |
428 | 6373 goto unconditional_jump; |
6374 | |
6375 | |
6376 /* At the end of an alternative, we need to push a dummy failure | |
6377 point in case we are followed by a `pop_failure_jump', because | |
6378 we don't want the failure point for the alternative to be | |
6379 popped. For example, matching `(a|ab)*' against `aab' | |
6380 requires that we match the `ab' alternative. */ | |
6381 case push_dummy_failure: | |
5041 | 6382 DEBUG_MATCH_PRINT1 ("EXECUTING push_dummy_failure.\n"); |
428 | 6383 /* See comments just above at `dummy_failure_jump' about the |
6384 two zeroes. */ | |
446 | 6385 PUSH_FAILURE_POINT ((unsigned char *) 0, (unsigned char *) 0, -2); |
428 | 6386 break; |
6387 | |
6388 /* Have to succeed matching what follows at least n times. | |
6389 After that, handle like `on_failure_jump'. */ | |
6390 case succeed_n: | |
6391 EXTRACT_NUMBER (mcnt, p + 2); | |
5041 | 6392 DEBUG_MATCH_PRINT2 ("EXECUTING succeed_n %d.\n", mcnt); |
428 | 6393 |
6394 assert (mcnt >= 0); | |
6395 /* Originally, this is how many times we HAVE to succeed. */ | |
6396 if (mcnt > 0) | |
6397 { | |
6398 mcnt--; | |
6399 p += 2; | |
6400 STORE_NUMBER_AND_INCR (p, mcnt); | |
5041 | 6401 DEBUG_MATCH_PRINT3 (" Setting 0x%lx to %d.\n", (long) p, mcnt); |
428 | 6402 } |
6403 else if (mcnt == 0) | |
6404 { | |
5041 | 6405 DEBUG_MATCH_PRINT2 (" Setting two bytes from 0x%lx to no_op.\n", |
428 | 6406 (long) (p+2)); |
6407 p[2] = (unsigned char) no_op; | |
6408 p[3] = (unsigned char) no_op; | |
6409 goto on_failure; | |
6410 } | |
6411 break; | |
6412 | |
6413 case jump_n: | |
6414 EXTRACT_NUMBER (mcnt, p + 2); | |
5041 | 6415 DEBUG_MATCH_PRINT2 ("EXECUTING jump_n %d.\n", mcnt); |
428 | 6416 |
6417 /* Originally, this is how many times we CAN jump. */ | |
6418 if (mcnt) | |
6419 { | |
6420 mcnt--; | |
6421 STORE_NUMBER (p + 2, mcnt); | |
6422 goto unconditional_jump; | |
6423 } | |
6424 /* If don't have to jump any more, skip over the rest of command. */ | |
6425 else | |
6426 p += 4; | |
6427 break; | |
6428 | |
6429 case set_number_at: | |
6430 { | |
5041 | 6431 DEBUG_MATCH_PRINT1 ("EXECUTING set_number_at.\n"); |
428 | 6432 |
6433 EXTRACT_NUMBER_AND_INCR (mcnt, p); | |
6434 p1 = p + mcnt; | |
6435 EXTRACT_NUMBER_AND_INCR (mcnt, p); | |
5041 | 6436 DEBUG_MATCH_PRINT3 (" Setting 0x%lx to %d.\n", (long) p1, mcnt); |
428 | 6437 STORE_NUMBER (p1, mcnt); |
6438 break; | |
6439 } | |
6440 | |
6441 case wordbound: | |
5041 | 6442 DEBUG_MATCH_PRINT1 ("EXECUTING wordbound.\n"); |
428 | 6443 should_succeed = 1; |
6444 matchwordbound: | |
6445 { | |
6446 /* XEmacs change */ | |
1377 | 6447 /* Straightforward and (I hope) correct implementation. |
6448 Probably should be optimized by arranging to compute | |
1497 | 6449 charpos only once. */ |
1377 | 6450 /* emch1 is the character before d, syn1 is the syntax of |
6451 emch1, emch2 is the character at d, and syn2 is the | |
6452 syntax of emch2. */ | |
6453 Ichar emch1, emch2; | |
1468 | 6454 int syn1 = 0, |
6455 syn2 = 0; | |
1377 | 6456 re_char *d_before, *d_after; |
6457 int result, | |
6458 at_beg = AT_STRINGS_BEG (d), | |
6459 at_end = AT_STRINGS_END (d); | |
6460 #ifdef emacs | |
1497 | 6461 Charxpos charpos; |
1377 | 6462 #endif |
6463 | |
6464 if (at_beg && at_end) | |
6465 { | |
6466 result = 0; | |
6467 } | |
428 | 6468 else |
6469 { | |
1377 | 6470 if (!at_beg) |
6471 { | |
6472 d_before = POS_BEFORE_GAP_UNSAFE (d); | |
6473 DEC_IBYTEPTR_FMT (d_before, fmt); | |
6474 emch1 = itext_ichar_fmt (d_before, fmt, lispobj); | |
460 | 6475 #ifdef emacs |
1497 | 6476 charpos = offset_to_charxpos (lispobj, |
6477 PTR_TO_OFFSET (d)) - 1; | |
1377 | 6478 BEGIN_REGEX_MALLOC_OK (); |
1497 | 6479 UPDATE_SYNTAX_CACHE (scache, charpos); |
460 | 6480 #endif |
1377 | 6481 syn1 = SYNTAX_FROM_CACHE (scache, emch1); |
6482 END_REGEX_MALLOC_OK (); | |
6483 } | |
6484 if (!at_end) | |
6485 { | |
6486 d_after = POS_AFTER_GAP_UNSAFE (d); | |
6487 emch2 = itext_ichar_fmt (d_after, fmt, lispobj); | |
460 | 6488 #ifdef emacs |
1497 | 6489 charpos = offset_to_charxpos (lispobj, PTR_TO_OFFSET (d)); |
1377 | 6490 BEGIN_REGEX_MALLOC_OK (); |
1497 | 6491 UPDATE_SYNTAX_CACHE_FORWARD (scache, charpos); |
460 | 6492 #endif |
1377 | 6493 syn2 = SYNTAX_FROM_CACHE (scache, emch2); |
6494 END_REGEX_MALLOC_OK (); | |
6495 } | |
1333 | 6496 RE_MATCH_RELOCATE_MOVEABLE_DATA_POINTERS (); |
1377 | 6497 |
6498 if (at_beg) | |
6499 result = (syn2 == Sword); | |
6500 else if (at_end) | |
6501 result = (syn1 == Sword); | |
6502 else | |
6503 result = ((syn1 == Sword) != (syn2 == Sword)); | |
428 | 6504 } |
1377 | 6505 |
428 | 6506 if (result == should_succeed) |
6507 break; | |
6508 goto fail; | |
6509 } | |
6510 | |
6511 case notwordbound: | |
5041 | 6512 DEBUG_MATCH_PRINT1 ("EXECUTING notwordbound.\n"); |
428 | 6513 should_succeed = 0; |
6514 goto matchwordbound; | |
6515 | |
6516 case wordbeg: | |
5041 | 6517 DEBUG_MATCH_PRINT1 ("EXECUTING wordbeg.\n"); |
460 | 6518 if (AT_STRINGS_END (d)) |
6519 goto fail; | |
428 | 6520 { |
6521 /* XEmacs: this originally read: | |
6522 | |
6523 if (WORDCHAR_P (d) && (AT_STRINGS_BEG (d) || !WORDCHAR_P (d - 1))) | |
6524 break; | |
6525 | |
6526 */ | |
460 | 6527 re_char *dtmp = POS_AFTER_GAP_UNSAFE (d); |
867 | 6528 Ichar emch = itext_ichar_fmt (dtmp, fmt, lispobj); |
1333 | 6529 int tempres; |
1347 | 6530 #ifdef emacs |
6531 Charxpos charpos = offset_to_charxpos (lispobj, PTR_TO_OFFSET (d)); | |
6532 #endif | |
1333 | 6533 BEGIN_REGEX_MALLOC_OK (); |
460 | 6534 #ifdef emacs |
826 | 6535 UPDATE_SYNTAX_CACHE (scache, charpos); |
460 | 6536 #endif |
1333 | 6537 tempres = (SYNTAX_FROM_CACHE (scache, emch) != Sword); |
6538 END_REGEX_MALLOC_OK (); | |
6539 RE_MATCH_RELOCATE_MOVEABLE_DATA_POINTERS (); | |
6540 if (tempres) | |
428 | 6541 goto fail; |
6542 if (AT_STRINGS_BEG (d)) | |
6543 break; | |
460 | 6544 dtmp = POS_BEFORE_GAP_UNSAFE (d); |
867 | 6545 DEC_IBYTEPTR_FMT (dtmp, fmt); |
6546 emch = itext_ichar_fmt (dtmp, fmt, lispobj); | |
1333 | 6547 BEGIN_REGEX_MALLOC_OK (); |
460 | 6548 #ifdef emacs |
826 | 6549 UPDATE_SYNTAX_CACHE_BACKWARD (scache, charpos - 1); |
460 | 6550 #endif |
1333 | 6551 tempres = (SYNTAX_FROM_CACHE (scache, emch) != Sword); |
6552 END_REGEX_MALLOC_OK (); | |
6553 RE_MATCH_RELOCATE_MOVEABLE_DATA_POINTERS (); | |
6554 if (tempres) | |
428 | 6555 break; |
6556 goto fail; | |
6557 } | |
6558 | |
6559 case wordend: | |
5041 | 6560 DEBUG_MATCH_PRINT1 ("EXECUTING wordend.\n"); |
460 | 6561 if (AT_STRINGS_BEG (d)) |
6562 goto fail; | |
428 | 6563 { |
6564 /* XEmacs: this originally read: | |
6565 | |
6566 if (!AT_STRINGS_BEG (d) && WORDCHAR_P (d - 1) | |
6567 && (!WORDCHAR_P (d) || AT_STRINGS_END (d))) | |
6568 break; | |
6569 | |
6570 The or condition is incorrect (reversed). | |
6571 */ | |
460 | 6572 re_char *dtmp; |
867 | 6573 Ichar emch; |
1333 | 6574 int tempres; |
460 | 6575 #ifdef emacs |
826 | 6576 Charxpos charpos = offset_to_charxpos (lispobj, PTR_TO_OFFSET (d)); |
1347 | 6577 BEGIN_REGEX_MALLOC_OK (); |
826 | 6578 UPDATE_SYNTAX_CACHE (scache, charpos); |
1333 | 6579 END_REGEX_MALLOC_OK (); |
6580 RE_MATCH_RELOCATE_MOVEABLE_DATA_POINTERS (); | |
1347 | 6581 #endif |
460 | 6582 dtmp = POS_BEFORE_GAP_UNSAFE (d); |
867 | 6583 DEC_IBYTEPTR_FMT (dtmp, fmt); |
6584 emch = itext_ichar_fmt (dtmp, fmt, lispobj); | |
1333 | 6585 BEGIN_REGEX_MALLOC_OK (); |
6586 tempres = (SYNTAX_FROM_CACHE (scache, emch) != Sword); | |
6587 END_REGEX_MALLOC_OK (); | |
6588 RE_MATCH_RELOCATE_MOVEABLE_DATA_POINTERS (); | |
6589 if (tempres) | |
428 | 6590 goto fail; |
6591 if (AT_STRINGS_END (d)) | |
6592 break; | |
460 | 6593 dtmp = POS_AFTER_GAP_UNSAFE (d); |
867 | 6594 emch = itext_ichar_fmt (dtmp, fmt, lispobj); |
1333 | 6595 BEGIN_REGEX_MALLOC_OK (); |
460 | 6596 #ifdef emacs |
826 | 6597 UPDATE_SYNTAX_CACHE_FORWARD (scache, charpos + 1); |
460 | 6598 #endif |
1333 | 6599 tempres = (SYNTAX_FROM_CACHE (scache, emch) != Sword); |
6600 END_REGEX_MALLOC_OK (); | |
6601 RE_MATCH_RELOCATE_MOVEABLE_DATA_POINTERS (); | |
6602 if (tempres) | |
428 | 6603 break; |
6604 goto fail; | |
6605 } | |
6606 | |
6607 #ifdef emacs | |
6608 case before_dot: | |
5041 | 6609 DEBUG_MATCH_PRINT1 ("EXECUTING before_dot.\n"); |
826 | 6610 if (!BUFFERP (lispobj) |
6611 || (BUF_PTR_BYTE_POS (XBUFFER (lispobj), (unsigned char *) d) | |
6612 >= BUF_PT (XBUFFER (lispobj)))) | |
428 | 6613 goto fail; |
6614 break; | |
6615 | |
6616 case at_dot: | |
5041 | 6617 DEBUG_MATCH_PRINT1 ("EXECUTING at_dot.\n"); |
826 | 6618 if (!BUFFERP (lispobj) |
6619 || (BUF_PTR_BYTE_POS (XBUFFER (lispobj), (unsigned char *) d) | |
6620 != BUF_PT (XBUFFER (lispobj)))) | |
428 | 6621 goto fail; |
6622 break; | |
6623 | |
6624 case after_dot: | |
5041 | 6625 DEBUG_MATCH_PRINT1 ("EXECUTING after_dot.\n"); |
826 | 6626 if (!BUFFERP (lispobj) |
6627 || (BUF_PTR_BYTE_POS (XBUFFER (lispobj), (unsigned char *) d) | |
6628 <= BUF_PT (XBUFFER (lispobj)))) | |
428 | 6629 goto fail; |
6630 break; | |
6631 | |
6632 case syntaxspec: | |
5041 | 6633 DEBUG_MATCH_PRINT2 ("EXECUTING syntaxspec %d.\n", mcnt); |
428 | 6634 mcnt = *p++; |
6635 goto matchsyntax; | |
6636 | |
6637 case wordchar: | |
5041 | 6638 DEBUG_MATCH_PRINT1 ("EXECUTING Emacs wordchar.\n"); |
428 | 6639 mcnt = (int) Sword; |
6640 matchsyntax: | |
6641 should_succeed = 1; | |
6642 matchornotsyntax: | |
6643 { | |
6644 int matches; | |
867 | 6645 Ichar emch; |
428 | 6646 |
450 | 6647 REGEX_PREFETCH (); |
1333 | 6648 BEGIN_REGEX_MALLOC_OK (); |
826 | 6649 UPDATE_SYNTAX_CACHE |
6650 (scache, offset_to_charxpos (lispobj, PTR_TO_OFFSET (d))); | |
1333 | 6651 END_REGEX_MALLOC_OK (); |
6652 RE_MATCH_RELOCATE_MOVEABLE_DATA_POINTERS (); | |
826 | 6653 |
867 | 6654 emch = itext_ichar_fmt (d, fmt, lispobj); |
1333 | 6655 BEGIN_REGEX_MALLOC_OK (); |
826 | 6656 matches = (SYNTAX_FROM_CACHE (scache, emch) == |
6657 (enum syntaxcode) mcnt); | |
1333 | 6658 END_REGEX_MALLOC_OK (); |
6659 RE_MATCH_RELOCATE_MOVEABLE_DATA_POINTERS (); | |
867 | 6660 INC_IBYTEPTR_FMT (d, fmt); |
428 | 6661 if (matches != should_succeed) |
6662 goto fail; | |
6663 SET_REGS_MATCHED (); | |
6664 } | |
6665 break; | |
6666 | |
6667 case notsyntaxspec: | |
5041 | 6668 DEBUG_MATCH_PRINT2 ("EXECUTING notsyntaxspec %d.\n", mcnt); |
428 | 6669 mcnt = *p++; |
6670 goto matchnotsyntax; | |
6671 | |
6672 case notwordchar: | |
5041 | 6673 DEBUG_MATCH_PRINT1 ("EXECUTING Emacs notwordchar.\n"); |
428 | 6674 mcnt = (int) Sword; |
6675 matchnotsyntax: | |
6676 should_succeed = 0; | |
6677 goto matchornotsyntax; | |
6678 | |
6679 #ifdef MULE | |
6680 /* 97/2/17 jhod Mule category code patch */ | |
6681 case categoryspec: | |
6682 should_succeed = 1; | |
6683 matchornotcategory: | |
6684 { | |
867 | 6685 Ichar emch; |
428 | 6686 |
6687 mcnt = *p++; | |
450 | 6688 REGEX_PREFETCH (); |
867 | 6689 emch = itext_ichar_fmt (d, fmt, lispobj); |
6690 INC_IBYTEPTR_FMT (d, fmt); | |
826 | 6691 if (check_category_char (emch, BUFFER_CATEGORY_TABLE (lispbuf), |
6692 mcnt, should_succeed)) | |
428 | 6693 goto fail; |
6694 SET_REGS_MATCHED (); | |
6695 } | |
6696 break; | |
6697 | |
6698 case notcategoryspec: | |
6699 should_succeed = 0; | |
6700 goto matchornotcategory; | |
6701 /* end of category patch */ | |
6702 #endif /* MULE */ | |
6703 #else /* not emacs */ | |
6704 case wordchar: | |
5041 | 6705 DEBUG_MATCH_PRINT1 ("EXECUTING non-Emacs wordchar.\n"); |
450 | 6706 REGEX_PREFETCH (); |
826 | 6707 if (!WORDCHAR_P ((int) (*d))) |
428 | 6708 goto fail; |
6709 SET_REGS_MATCHED (); | |
6710 d++; | |
6711 break; | |
6712 | |
6713 case notwordchar: | |
5041 | 6714 DEBUG_MATCH_PRINT1 ("EXECUTING non-Emacs notwordchar.\n"); |
450 | 6715 REGEX_PREFETCH (); |
826 | 6716 if (!WORDCHAR_P ((int) (*d))) |
428 | 6717 goto fail; |
6718 SET_REGS_MATCHED (); | |
6719 d++; | |
6720 break; | |
446 | 6721 #endif /* emacs */ |
428 | 6722 |
6723 default: | |
2500 | 6724 ABORT (); |
428 | 6725 } |
6726 continue; /* Successfully executed one pattern command; keep going. */ | |
6727 | |
6728 | |
6729 /* We goto here if a matching operation fails. */ | |
6730 fail: | |
6731 if (!FAIL_STACK_EMPTY ()) | |
6732 { /* A restart point is known. Restore to that state. */ | |
5041 | 6733 DEBUG_MATCH_PRINT1 ("\nFAIL:\n"); |
428 | 6734 POP_FAILURE_POINT (d, p, |
6735 lowest_active_reg, highest_active_reg, | |
6736 regstart, regend, reg_info); | |
6737 | |
6738 /* If this failure point is a dummy, try the next one. */ | |
6739 if (!p) | |
6740 goto fail; | |
6741 | |
6742 /* If we failed to the end of the pattern, don't examine *p. */ | |
6743 assert (p <= pend); | |
6744 if (p < pend) | |
6745 { | |
460 | 6746 re_bool is_a_jump_n = false; |
428 | 6747 |
6748 /* If failed to a backwards jump that's part of a repetition | |
6749 loop, need to pop this failure point and use the next one. */ | |
6750 switch ((re_opcode_t) *p) | |
6751 { | |
6752 case jump_n: | |
6753 is_a_jump_n = true; | |
6754 case maybe_pop_jump: | |
6755 case pop_failure_jump: | |
6756 case jump: | |
6757 p1 = p + 1; | |
6758 EXTRACT_NUMBER_AND_INCR (mcnt, p1); | |
6759 p1 += mcnt; | |
6760 | |
6761 if ((is_a_jump_n && (re_opcode_t) *p1 == succeed_n) | |
6762 || (!is_a_jump_n | |
6763 && (re_opcode_t) *p1 == on_failure_jump)) | |
6764 goto fail; | |
6765 break; | |
6766 default: | |
6767 /* do nothing */ ; | |
6768 } | |
6769 } | |
6770 | |
6771 if (d >= string1 && d <= end1) | |
6772 dend = end_match_1; | |
6773 } | |
6774 else | |
6775 break; /* Matching at this starting point really fails. */ | |
6776 } /* for (;;) */ | |
6777 | |
6778 if (best_regs_set) | |
6779 goto restore_best_regs; | |
6780 | |
6781 FREE_VARIABLES (); | |
6782 | |
6783 return -1; /* Failure to match. */ | |
1333 | 6784 } /* re_match_2_internal */ |
428 | 6785 |
6786 /* Subroutine definitions for re_match_2. */ | |
6787 | |
6788 | |
6789 /* We are passed P pointing to a register number after a start_memory. | |
6790 | |
6791 Return true if the pattern up to the corresponding stop_memory can | |
6792 match the empty string, and false otherwise. | |
6793 | |
6794 If we find the matching stop_memory, sets P to point to one past its number. | |
6795 Otherwise, sets P to an undefined byte less than or equal to END. | |
6796 | |
6797 We don't handle duplicates properly (yet). */ | |
6798 | |
460 | 6799 static re_bool |
428 | 6800 group_match_null_string_p (unsigned char **p, unsigned char *end, |
6801 register_info_type *reg_info) | |
6802 { | |
6803 int mcnt; | |
6804 /* Point to after the args to the start_memory. */ | |
6805 unsigned char *p1 = *p + 2; | |
6806 | |
6807 while (p1 < end) | |
6808 { | |
6809 /* Skip over opcodes that can match nothing, and return true or | |
6810 false, as appropriate, when we get to one that can't, or to the | |
6811 matching stop_memory. */ | |
6812 | |
6813 switch ((re_opcode_t) *p1) | |
6814 { | |
6815 /* Could be either a loop or a series of alternatives. */ | |
6816 case on_failure_jump: | |
6817 p1++; | |
6818 EXTRACT_NUMBER_AND_INCR (mcnt, p1); | |
6819 | |
6820 /* If the next operation is not a jump backwards in the | |
6821 pattern. */ | |
6822 | |
6823 if (mcnt >= 0) | |
6824 { | |
6825 /* Go through the on_failure_jumps of the alternatives, | |
6826 seeing if any of the alternatives cannot match nothing. | |
6827 The last alternative starts with only a jump, | |
6828 whereas the rest start with on_failure_jump and end | |
6829 with a jump, e.g., here is the pattern for `a|b|c': | |
6830 | |
6831 /on_failure_jump/0/6/exactn/1/a/jump_past_alt/0/6 | |
6832 /on_failure_jump/0/6/exactn/1/b/jump_past_alt/0/3 | |
6833 /exactn/1/c | |
6834 | |
6835 So, we have to first go through the first (n-1) | |
6836 alternatives and then deal with the last one separately. */ | |
6837 | |
6838 | |
6839 /* Deal with the first (n-1) alternatives, which start | |
6840 with an on_failure_jump (see above) that jumps to right | |
6841 past a jump_past_alt. */ | |
6842 | |
6843 while ((re_opcode_t) p1[mcnt-3] == jump_past_alt) | |
6844 { | |
6845 /* `mcnt' holds how many bytes long the alternative | |
6846 is, including the ending `jump_past_alt' and | |
6847 its number. */ | |
6848 | |
6849 if (!alt_match_null_string_p (p1, p1 + mcnt - 3, | |
6850 reg_info)) | |
6851 return false; | |
6852 | |
6853 /* Move to right after this alternative, including the | |
6854 jump_past_alt. */ | |
6855 p1 += mcnt; | |
6856 | |
6857 /* Break if it's the beginning of an n-th alternative | |
6858 that doesn't begin with an on_failure_jump. */ | |
6859 if ((re_opcode_t) *p1 != on_failure_jump) | |
6860 break; | |
6861 | |
6862 /* Still have to check that it's not an n-th | |
6863 alternative that starts with an on_failure_jump. */ | |
6864 p1++; | |
6865 EXTRACT_NUMBER_AND_INCR (mcnt, p1); | |
6866 if ((re_opcode_t) p1[mcnt-3] != jump_past_alt) | |
6867 { | |
6868 /* Get to the beginning of the n-th alternative. */ | |
6869 p1 -= 3; | |
6870 break; | |
6871 } | |
6872 } | |
6873 | |
6874 /* Deal with the last alternative: go back and get number | |
6875 of the `jump_past_alt' just before it. `mcnt' contains | |
6876 the length of the alternative. */ | |
6877 EXTRACT_NUMBER (mcnt, p1 - 2); | |
6878 | |
6879 if (!alt_match_null_string_p (p1, p1 + mcnt, reg_info)) | |
6880 return false; | |
6881 | |
6882 p1 += mcnt; /* Get past the n-th alternative. */ | |
6883 } /* if mcnt > 0 */ | |
6884 break; | |
6885 | |
6886 | |
6887 case stop_memory: | |
6888 assert (p1[1] == **p); | |
6889 *p = p1 + 2; | |
6890 return true; | |
6891 | |
6892 | |
6893 default: | |
6894 if (!common_op_match_null_string_p (&p1, end, reg_info)) | |
6895 return false; | |
6896 } | |
6897 } /* while p1 < end */ | |
6898 | |
6899 return false; | |
6900 } /* group_match_null_string_p */ | |
6901 | |
6902 | |
6903 /* Similar to group_match_null_string_p, but doesn't deal with alternatives: | |
6904 It expects P to be the first byte of a single alternative and END one | |
6905 byte past the last. The alternative can contain groups. */ | |
6906 | |
460 | 6907 static re_bool |
428 | 6908 alt_match_null_string_p (unsigned char *p, unsigned char *end, |
6909 register_info_type *reg_info) | |
6910 { | |
6911 int mcnt; | |
6912 unsigned char *p1 = p; | |
6913 | |
6914 while (p1 < end) | |
6915 { | |
6916 /* Skip over opcodes that can match nothing, and break when we get | |
6917 to one that can't. */ | |
6918 | |
6919 switch ((re_opcode_t) *p1) | |
6920 { | |
6921 /* It's a loop. */ | |
6922 case on_failure_jump: | |
6923 p1++; | |
6924 EXTRACT_NUMBER_AND_INCR (mcnt, p1); | |
6925 p1 += mcnt; | |
6926 break; | |
6927 | |
6928 default: | |
6929 if (!common_op_match_null_string_p (&p1, end, reg_info)) | |
6930 return false; | |
6931 } | |
6932 } /* while p1 < end */ | |
6933 | |
6934 return true; | |
6935 } /* alt_match_null_string_p */ | |
6936 | |
6937 | |
6938 /* Deals with the ops common to group_match_null_string_p and | |
6939 alt_match_null_string_p. | |
6940 | |
6941 Sets P to one after the op and its arguments, if any. */ | |
6942 | |
460 | 6943 static re_bool |
428 | 6944 common_op_match_null_string_p (unsigned char **p, unsigned char *end, |
6945 register_info_type *reg_info) | |
6946 { | |
6947 int mcnt; | |
460 | 6948 re_bool ret; |
428 | 6949 int reg_no; |
6950 unsigned char *p1 = *p; | |
6951 | |
6952 switch ((re_opcode_t) *p1++) | |
6953 { | |
6954 case no_op: | |
6955 case begline: | |
6956 case endline: | |
6957 case begbuf: | |
6958 case endbuf: | |
6959 case wordbeg: | |
6960 case wordend: | |
6961 case wordbound: | |
6962 case notwordbound: | |
6963 #ifdef emacs | |
6964 case before_dot: | |
6965 case at_dot: | |
6966 case after_dot: | |
6967 #endif | |
6968 break; | |
6969 | |
6970 case start_memory: | |
6971 reg_no = *p1; | |
6972 assert (reg_no > 0 && reg_no <= MAX_REGNUM); | |
6973 ret = group_match_null_string_p (&p1, end, reg_info); | |
6974 | |
6975 /* Have to set this here in case we're checking a group which | |
6976 contains a group and a back reference to it. */ | |
6977 | |
6978 if (REG_MATCH_NULL_STRING_P (reg_info[reg_no]) == MATCH_NULL_UNSET_VALUE) | |
6979 REG_MATCH_NULL_STRING_P (reg_info[reg_no]) = ret; | |
6980 | |
6981 if (!ret) | |
6982 return false; | |
6983 break; | |
6984 | |
6985 /* If this is an optimized succeed_n for zero times, make the jump. */ | |
6986 case jump: | |
6987 EXTRACT_NUMBER_AND_INCR (mcnt, p1); | |
6988 if (mcnt >= 0) | |
6989 p1 += mcnt; | |
6990 else | |
6991 return false; | |
6992 break; | |
6993 | |
6994 case succeed_n: | |
6995 /* Get to the number of times to succeed. */ | |
6996 p1 += 2; | |
6997 EXTRACT_NUMBER_AND_INCR (mcnt, p1); | |
6998 | |
6999 if (mcnt == 0) | |
7000 { | |
7001 p1 -= 4; | |
7002 EXTRACT_NUMBER_AND_INCR (mcnt, p1); | |
7003 p1 += mcnt; | |
7004 } | |
7005 else | |
7006 return false; | |
7007 break; | |
7008 | |
7009 case duplicate: | |
7010 if (!REG_MATCH_NULL_STRING_P (reg_info[*p1])) | |
7011 return false; | |
7012 break; | |
7013 | |
7014 case set_number_at: | |
7015 p1 += 4; | |
7016 | |
7017 default: | |
7018 /* All other opcodes mean we cannot match the empty string. */ | |
7019 return false; | |
7020 } | |
7021 | |
7022 *p = p1; | |
7023 return true; | |
7024 } /* common_op_match_null_string_p */ | |
7025 | |
7026 | |
7027 /* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN | |
7028 bytes; nonzero otherwise. */ | |
7029 | |
7030 static int | |
446 | 7031 bcmp_translate (re_char *s1, re_char *s2, |
826 | 7032 REGISTER int len, RE_TRANSLATE_TYPE translate |
7033 #ifdef emacs | |
2333 | 7034 , Internal_Format USED_IF_MULE (fmt), |
7035 Lisp_Object USED_IF_MULE (lispobj) | |
826 | 7036 #endif |
7037 ) | |
428 | 7038 { |
826 | 7039 REGISTER re_char *p1 = s1, *p2 = s2; |
446 | 7040 #ifdef MULE |
826 | 7041 re_char *p1_end = s1 + len; |
7042 re_char *p2_end = s2 + len; | |
446 | 7043 |
7044 while (p1 != p1_end && p2 != p2_end) | |
7045 { | |
867 | 7046 Ichar p1_ch, p2_ch; |
7047 | |
7048 p1_ch = itext_ichar_fmt (p1, fmt, lispobj); | |
7049 p2_ch = itext_ichar_fmt (p2, fmt, lispobj); | |
826 | 7050 |
7051 if (RE_TRANSLATE_1 (p1_ch) | |
7052 != RE_TRANSLATE_1 (p2_ch)) | |
446 | 7053 return 1; |
867 | 7054 INC_IBYTEPTR_FMT (p1, fmt); |
7055 INC_IBYTEPTR_FMT (p2, fmt); | |
446 | 7056 } |
7057 #else /* not MULE */ | |
428 | 7058 while (len) |
7059 { | |
826 | 7060 if (RE_TRANSLATE_1 (*p1++) != RE_TRANSLATE_1 (*p2++)) return 1; |
428 | 7061 len--; |
7062 } | |
446 | 7063 #endif /* MULE */ |
428 | 7064 return 0; |
7065 } | |
7066 | |
7067 /* Entry points for GNU code. */ | |
7068 | |
7069 /* re_compile_pattern is the GNU regular expression compiler: it | |
7070 compiles PATTERN (of length SIZE) and puts the result in BUFP. | |
7071 Returns 0 if the pattern was valid, otherwise an error string. | |
7072 | |
7073 Assumes the `allocated' (and perhaps `buffer') and `translate' fields | |
7074 are set in BUFP on entry. | |
7075 | |
7076 We call regex_compile to do the actual compilation. */ | |
7077 | |
442 | 7078 const char * |
7079 re_compile_pattern (const char *pattern, int length, | |
428 | 7080 struct re_pattern_buffer *bufp) |
7081 { | |
7082 reg_errcode_t ret; | |
7083 | |
7084 /* GNU code is written to assume at least RE_NREGS registers will be set | |
7085 (and at least one extra will be -1). */ | |
7086 bufp->regs_allocated = REGS_UNALLOCATED; | |
7087 | |
7088 /* And GNU code determines whether or not to get register information | |
7089 by passing null for the REGS argument to re_match, etc., not by | |
7090 setting no_sub. */ | |
7091 bufp->no_sub = 0; | |
7092 | |
7093 /* Match anchors at newline. */ | |
7094 bufp->newline_anchor = 1; | |
7095 | |
826 | 7096 ret = regex_compile ((unsigned char *) pattern, length, re_syntax_options, |
7097 bufp); | |
428 | 7098 |
7099 if (!ret) | |
7100 return NULL; | |
7101 return gettext (re_error_msgid[(int) ret]); | |
7102 } | |
7103 | |
7104 /* Entry points compatible with 4.2 BSD regex library. We don't define | |
7105 them unless specifically requested. */ | |
7106 | |
7107 #ifdef _REGEX_RE_COMP | |
7108 | |
7109 /* BSD has one and only one pattern buffer. */ | |
7110 static struct re_pattern_buffer re_comp_buf; | |
7111 | |
7112 char * | |
442 | 7113 re_comp (const char *s) |
428 | 7114 { |
7115 reg_errcode_t ret; | |
7116 | |
7117 if (!s) | |
7118 { | |
7119 if (!re_comp_buf.buffer) | |
7120 return gettext ("No previous regular expression"); | |
7121 return 0; | |
7122 } | |
7123 | |
7124 if (!re_comp_buf.buffer) | |
7125 { | |
1333 | 7126 re_comp_buf.buffer = (unsigned char *) xmalloc (200); |
428 | 7127 if (re_comp_buf.buffer == NULL) |
7128 return gettext (re_error_msgid[(int) REG_ESPACE]); | |
7129 re_comp_buf.allocated = 200; | |
7130 | |
1333 | 7131 re_comp_buf.fastmap = (char *) xmalloc (1 << BYTEWIDTH); |
428 | 7132 if (re_comp_buf.fastmap == NULL) |
7133 return gettext (re_error_msgid[(int) REG_ESPACE]); | |
7134 } | |
7135 | |
7136 /* Since `re_exec' always passes NULL for the `regs' argument, we | |
7137 don't need to initialize the pattern buffer fields which affect it. */ | |
7138 | |
7139 /* Match anchors at newlines. */ | |
7140 re_comp_buf.newline_anchor = 1; | |
7141 | |
826 | 7142 ret = regex_compile ((unsigned char *)s, strlen (s), re_syntax_options, |
7143 &re_comp_buf); | |
428 | 7144 |
7145 if (!ret) | |
7146 return NULL; | |
7147 | |
442 | 7148 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */ |
428 | 7149 return (char *) gettext (re_error_msgid[(int) ret]); |
7150 } | |
7151 | |
7152 | |
7153 int | |
442 | 7154 re_exec (const char *s) |
428 | 7155 { |
442 | 7156 const int len = strlen (s); |
428 | 7157 return |
7158 0 <= re_search (&re_comp_buf, s, len, 0, len, (struct re_registers *) 0); | |
7159 } | |
7160 #endif /* _REGEX_RE_COMP */ | |
7161 | |
7162 /* POSIX.2 functions. Don't define these for Emacs. */ | |
7163 | |
7164 #ifndef emacs | |
7165 | |
7166 /* regcomp takes a regular expression as a string and compiles it. | |
7167 | |
7168 PREG is a regex_t *. We do not expect any fields to be initialized, | |
7169 since POSIX says we shouldn't. Thus, we set | |
7170 | |
7171 `buffer' to the compiled pattern; | |
7172 `used' to the length of the compiled pattern; | |
7173 `syntax' to RE_SYNTAX_POSIX_EXTENDED if the | |
7174 REG_EXTENDED bit in CFLAGS is set; otherwise, to | |
7175 RE_SYNTAX_POSIX_BASIC; | |
7176 `newline_anchor' to REG_NEWLINE being set in CFLAGS; | |
7177 `fastmap' and `fastmap_accurate' to zero; | |
7178 `re_nsub' to the number of subexpressions in PATTERN. | |
502 | 7179 (non-shy of course. POSIX probably doesn't know about |
7180 shy ones, and in any case they should be invisible.) | |
428 | 7181 |
7182 PATTERN is the address of the pattern string. | |
7183 | |
7184 CFLAGS is a series of bits which affect compilation. | |
7185 | |
7186 If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we | |
7187 use POSIX basic syntax. | |
7188 | |
7189 If REG_NEWLINE is set, then . and [^...] don't match newline. | |
7190 Also, regexec will try a match beginning after every newline. | |
7191 | |
7192 If REG_ICASE is set, then we considers upper- and lowercase | |
7193 versions of letters to be equivalent when matching. | |
7194 | |
7195 If REG_NOSUB is set, then when PREG is passed to regexec, that | |
7196 routine will report only success or failure, and nothing about the | |
7197 registers. | |
7198 | |
7199 It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for | |
7200 the return codes and their meanings.) */ | |
7201 | |
7202 int | |
442 | 7203 regcomp (regex_t *preg, const char *pattern, int cflags) |
428 | 7204 { |
7205 reg_errcode_t ret; | |
647 | 7206 unsigned int syntax |
428 | 7207 = (cflags & REG_EXTENDED) ? |
7208 RE_SYNTAX_POSIX_EXTENDED : RE_SYNTAX_POSIX_BASIC; | |
7209 | |
7210 /* regex_compile will allocate the space for the compiled pattern. */ | |
7211 preg->buffer = 0; | |
7212 preg->allocated = 0; | |
7213 preg->used = 0; | |
7214 | |
7215 /* Don't bother to use a fastmap when searching. This simplifies the | |
7216 REG_NEWLINE case: if we used a fastmap, we'd have to put all the | |
7217 characters after newlines into the fastmap. This way, we just try | |
7218 every character. */ | |
7219 preg->fastmap = 0; | |
7220 | |
7221 if (cflags & REG_ICASE) | |
7222 { | |
647 | 7223 int i; |
428 | 7224 |
1333 | 7225 preg->translate = (char *) xmalloc (CHAR_SET_SIZE); |
428 | 7226 if (preg->translate == NULL) |
7227 return (int) REG_ESPACE; | |
7228 | |
7229 /* Map uppercase characters to corresponding lowercase ones. */ | |
7230 for (i = 0; i < CHAR_SET_SIZE; i++) | |
7231 preg->translate[i] = ISUPPER (i) ? tolower (i) : i; | |
7232 } | |
7233 else | |
7234 preg->translate = NULL; | |
7235 | |
7236 /* If REG_NEWLINE is set, newlines are treated differently. */ | |
7237 if (cflags & REG_NEWLINE) | |
7238 { /* REG_NEWLINE implies neither . nor [^...] match newline. */ | |
7239 syntax &= ~RE_DOT_NEWLINE; | |
7240 syntax |= RE_HAT_LISTS_NOT_NEWLINE; | |
7241 /* It also changes the matching behavior. */ | |
7242 preg->newline_anchor = 1; | |
7243 } | |
7244 else | |
7245 preg->newline_anchor = 0; | |
7246 | |
7247 preg->no_sub = !!(cflags & REG_NOSUB); | |
7248 | |
7249 /* POSIX says a null character in the pattern terminates it, so we | |
7250 can use strlen here in compiling the pattern. */ | |
446 | 7251 ret = regex_compile ((unsigned char *) pattern, strlen (pattern), syntax, preg); |
428 | 7252 |
7253 /* POSIX doesn't distinguish between an unmatched open-group and an | |
7254 unmatched close-group: both are REG_EPAREN. */ | |
7255 if (ret == REG_ERPAREN) ret = REG_EPAREN; | |
7256 | |
7257 return (int) ret; | |
7258 } | |
7259 | |
7260 | |
7261 /* regexec searches for a given pattern, specified by PREG, in the | |
7262 string STRING. | |
7263 | |
7264 If NMATCH is zero or REG_NOSUB was set in the cflags argument to | |
7265 `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at | |
7266 least NMATCH elements, and we set them to the offsets of the | |
7267 corresponding matched substrings. | |
7268 | |
7269 EFLAGS specifies `execution flags' which affect matching: if | |
7270 REG_NOTBOL is set, then ^ does not match at the beginning of the | |
7271 string; if REG_NOTEOL is set, then $ does not match at the end. | |
7272 | |
7273 We return 0 if we find a match and REG_NOMATCH if not. */ | |
7274 | |
7275 int | |
442 | 7276 regexec (const regex_t *preg, const char *string, size_t nmatch, |
428 | 7277 regmatch_t pmatch[], int eflags) |
7278 { | |
7279 int ret; | |
7280 struct re_registers regs; | |
7281 regex_t private_preg; | |
7282 int len = strlen (string); | |
460 | 7283 re_bool want_reg_info = !preg->no_sub && nmatch > 0; |
428 | 7284 |
7285 private_preg = *preg; | |
7286 | |
7287 private_preg.not_bol = !!(eflags & REG_NOTBOL); | |
7288 private_preg.not_eol = !!(eflags & REG_NOTEOL); | |
7289 | |
7290 /* The user has told us exactly how many registers to return | |
7291 information about, via `nmatch'. We have to pass that on to the | |
7292 matching routines. */ | |
7293 private_preg.regs_allocated = REGS_FIXED; | |
7294 | |
7295 if (want_reg_info) | |
7296 { | |
647 | 7297 regs.num_regs = (int) nmatch; |
7298 regs.start = TALLOC ((int) nmatch, regoff_t); | |
7299 regs.end = TALLOC ((int) nmatch, regoff_t); | |
428 | 7300 if (regs.start == NULL || regs.end == NULL) |
7301 return (int) REG_NOMATCH; | |
7302 } | |
7303 | |
7304 /* Perform the searching operation. */ | |
7305 ret = re_search (&private_preg, string, len, | |
7306 /* start: */ 0, /* range: */ len, | |
7307 want_reg_info ? ®s : (struct re_registers *) 0); | |
7308 | |
7309 /* Copy the register information to the POSIX structure. */ | |
7310 if (want_reg_info) | |
7311 { | |
7312 if (ret >= 0) | |
7313 { | |
647 | 7314 int r; |
7315 | |
7316 for (r = 0; r < (int) nmatch; r++) | |
428 | 7317 { |
7318 pmatch[r].rm_so = regs.start[r]; | |
7319 pmatch[r].rm_eo = regs.end[r]; | |
7320 } | |
7321 } | |
7322 | |
7323 /* If we needed the temporary register info, free the space now. */ | |
4976
16112448d484
Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents:
4832
diff
changeset
|
7324 xfree (regs.start); |
16112448d484
Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents:
4832
diff
changeset
|
7325 xfree (regs.end); |
428 | 7326 } |
7327 | |
7328 /* We want zero return to mean success, unlike `re_search'. */ | |
7329 return ret >= 0 ? (int) REG_NOERROR : (int) REG_NOMATCH; | |
7330 } | |
7331 | |
7332 | |
7333 /* Returns a message corresponding to an error code, ERRCODE, returned | |
7334 from either regcomp or regexec. We don't use PREG here. */ | |
7335 | |
7336 size_t | |
2286 | 7337 regerror (int errcode, const regex_t *UNUSED (preg), char *errbuf, |
647 | 7338 size_t errbuf_size) |
428 | 7339 { |
442 | 7340 const char *msg; |
665 | 7341 Bytecount msg_size; |
428 | 7342 |
7343 if (errcode < 0 | |
647 | 7344 || errcode >= (int) (sizeof (re_error_msgid) / |
7345 sizeof (re_error_msgid[0]))) | |
428 | 7346 /* Only error codes returned by the rest of the code should be passed |
7347 to this routine. If we are given anything else, or if other regex | |
7348 code generates an invalid error code, then the program has a bug. | |
7349 Dump core so we can fix it. */ | |
2500 | 7350 ABORT (); |
428 | 7351 |
7352 msg = gettext (re_error_msgid[errcode]); | |
7353 | |
7354 msg_size = strlen (msg) + 1; /* Includes the null. */ | |
7355 | |
7356 if (errbuf_size != 0) | |
7357 { | |
665 | 7358 if (msg_size > (Bytecount) errbuf_size) |
428 | 7359 { |
7360 strncpy (errbuf, msg, errbuf_size - 1); | |
7361 errbuf[errbuf_size - 1] = 0; | |
7362 } | |
7363 else | |
7364 strcpy (errbuf, msg); | |
7365 } | |
7366 | |
647 | 7367 return (size_t) msg_size; |
428 | 7368 } |
7369 | |
7370 | |
7371 /* Free dynamically allocated space used by PREG. */ | |
7372 | |
7373 void | |
7374 regfree (regex_t *preg) | |
7375 { | |
7376 if (preg->buffer != NULL) | |
4976
16112448d484
Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents:
4832
diff
changeset
|
7377 xfree (preg->buffer); |
428 | 7378 preg->buffer = NULL; |
7379 | |
7380 preg->allocated = 0; | |
7381 preg->used = 0; | |
7382 | |
7383 if (preg->fastmap != NULL) | |
4976
16112448d484
Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents:
4832
diff
changeset
|
7384 xfree (preg->fastmap); |
428 | 7385 preg->fastmap = NULL; |
7386 preg->fastmap_accurate = 0; | |
7387 | |
7388 if (preg->translate != NULL) | |
4976
16112448d484
Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents:
4832
diff
changeset
|
7389 xfree (preg->translate); |
428 | 7390 preg->translate = NULL; |
7391 } | |
7392 | |
7393 #endif /* not emacs */ | |
7394 |