Mercurial > emacs
annotate src/regex.c @ 11770:d546535268e0
(mail-font-lock-keywords): Make mail-header-separator into a regexp.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Tue, 09 May 1995 04:09:15 +0000 |
parents | bb59c20c60cb |
children | 471eac3b0f38 |
rev | line source |
---|---|
1155 | 1 /* Extended regular expression matching and search library, |
2454 | 2 version 0.12. |
1155 | 3 (Implements POSIX draft P10003.2/D11.2, except for |
4 internationalization features.) | |
5 | |
7305
c8787065a00e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
7135
diff
changeset
|
6 Copyright (C) 1993, 1994 Free Software Foundation, Inc. |
1155 | 7 |
8 This program is free software; you can redistribute it and/or modify | |
9 it under the terms of the GNU General Public License as published by | |
10 the Free Software Foundation; either version 2, or (at your option) | |
11 any later version. | |
12 | |
13 This program is distributed in the hope that it will be useful, | |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 GNU General Public License for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with this program; if not, write to the Free Software | |
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
21 | |
22 /* AIX requires this to be the first thing in the file. */ | |
23 #if defined (_AIX) && !defined (REGEX_MALLOC) | |
24 #pragma alloca | |
25 #endif | |
26 | |
27 #define _GNU_SOURCE | |
28 | |
4846 | 29 #ifdef HAVE_CONFIG_H |
30 #include <config.h> | |
31 #endif | |
32 | |
1155 | 33 /* We need this for `regex.h', and perhaps for the Emacs include files. */ |
34 #include <sys/types.h> | |
35 | |
10100
461530f66e34
(gettext): Make sure this is always defined, even #if emacs.
Karl Heuer <kwzh@gnu.org>
parents:
10090
diff
changeset
|
36 /* This is for other GNU distributions with internationalized messages. |
461530f66e34
(gettext): Make sure this is always defined, even #if emacs.
Karl Heuer <kwzh@gnu.org>
parents:
10090
diff
changeset
|
37 The GNU C Library itself does not yet support such messages. */ |
461530f66e34
(gettext): Make sure this is always defined, even #if emacs.
Karl Heuer <kwzh@gnu.org>
parents:
10090
diff
changeset
|
38 #if HAVE_LIBINTL_H |
461530f66e34
(gettext): Make sure this is always defined, even #if emacs.
Karl Heuer <kwzh@gnu.org>
parents:
10090
diff
changeset
|
39 # include <libintl.h> |
461530f66e34
(gettext): Make sure this is always defined, even #if emacs.
Karl Heuer <kwzh@gnu.org>
parents:
10090
diff
changeset
|
40 #else |
461530f66e34
(gettext): Make sure this is always defined, even #if emacs.
Karl Heuer <kwzh@gnu.org>
parents:
10090
diff
changeset
|
41 # define gettext(msgid) (msgid) |
461530f66e34
(gettext): Make sure this is always defined, even #if emacs.
Karl Heuer <kwzh@gnu.org>
parents:
10090
diff
changeset
|
42 #endif |
461530f66e34
(gettext): Make sure this is always defined, even #if emacs.
Karl Heuer <kwzh@gnu.org>
parents:
10090
diff
changeset
|
43 |
1155 | 44 /* The `emacs' switch turns on certain matching commands |
45 that make sense only in Emacs. */ | |
46 #ifdef emacs | |
47 | |
48 #include "lisp.h" | |
49 #include "buffer.h" | |
50 #include "syntax.h" | |
51 | |
52 #else /* not emacs */ | |
53 | |
11721
bb59c20c60cb
[! emacs]: #undef REL_ALLOC.
Richard M. Stallman <rms@gnu.org>
parents:
11656
diff
changeset
|
54 /* If we are not linking with Emacs proper, |
bb59c20c60cb
[! emacs]: #undef REL_ALLOC.
Richard M. Stallman <rms@gnu.org>
parents:
11656
diff
changeset
|
55 we can't use the relocating allocator |
bb59c20c60cb
[! emacs]: #undef REL_ALLOC.
Richard M. Stallman <rms@gnu.org>
parents:
11656
diff
changeset
|
56 even if config.h says that we can. */ |
bb59c20c60cb
[! emacs]: #undef REL_ALLOC.
Richard M. Stallman <rms@gnu.org>
parents:
11656
diff
changeset
|
57 #undef REL_ALLOC |
bb59c20c60cb
[! emacs]: #undef REL_ALLOC.
Richard M. Stallman <rms@gnu.org>
parents:
11656
diff
changeset
|
58 |
3766 | 59 #ifdef STDC_HEADERS |
60 #include <stdlib.h> | |
61 #else | |
62 char *malloc (); | |
63 char *realloc (); | |
64 #endif | |
65 | |
1155 | 66 /* We used to test for `BSTRING' here, but only GCC and Emacs define |
67 `BSTRING', as far as I know, and neither of them use this code. */ | |
7135 | 68 #ifndef INHIBIT_STRING_HEADER |
1641
47ae0840b2b9
*** empty log message ***
David J. MacKenzie <djm@gnu.org>
parents:
1637
diff
changeset
|
69 #if HAVE_STRING_H || STDC_HEADERS |
1155 | 70 #include <string.h> |
1637 | 71 #ifndef bcmp |
1155 | 72 #define bcmp(s1, s2, n) memcmp ((s1), (s2), (n)) |
1637 | 73 #endif |
74 #ifndef bcopy | |
1155 | 75 #define bcopy(s, d, n) memcpy ((d), (s), (n)) |
1637 | 76 #endif |
77 #ifndef bzero | |
1155 | 78 #define bzero(s, n) memset ((s), 0, (n)) |
1637 | 79 #endif |
1155 | 80 #else |
81 #include <strings.h> | |
82 #endif | |
7135 | 83 #endif |
1155 | 84 |
85 /* Define the syntax stuff for \<, \>, etc. */ | |
86 | |
87 /* This must be nonzero for the wordchar and notwordchar pattern | |
88 commands in re_match_2. */ | |
89 #ifndef Sword | |
90 #define Sword 1 | |
91 #endif | |
92 | |
10456
9c6110615166
[!emacs] (SWITCH_ENUM_CAST): New macro, from emacs/lisp.h
Karl Heuer <kwzh@gnu.org>
parents:
10297
diff
changeset
|
93 #ifdef SWITCH_ENUM_BUG |
9c6110615166
[!emacs] (SWITCH_ENUM_CAST): New macro, from emacs/lisp.h
Karl Heuer <kwzh@gnu.org>
parents:
10297
diff
changeset
|
94 #define SWITCH_ENUM_CAST(x) ((int)(x)) |
9c6110615166
[!emacs] (SWITCH_ENUM_CAST): New macro, from emacs/lisp.h
Karl Heuer <kwzh@gnu.org>
parents:
10297
diff
changeset
|
95 #else |
9c6110615166
[!emacs] (SWITCH_ENUM_CAST): New macro, from emacs/lisp.h
Karl Heuer <kwzh@gnu.org>
parents:
10297
diff
changeset
|
96 #define SWITCH_ENUM_CAST(x) (x) |
9c6110615166
[!emacs] (SWITCH_ENUM_CAST): New macro, from emacs/lisp.h
Karl Heuer <kwzh@gnu.org>
parents:
10297
diff
changeset
|
97 #endif |
9c6110615166
[!emacs] (SWITCH_ENUM_CAST): New macro, from emacs/lisp.h
Karl Heuer <kwzh@gnu.org>
parents:
10297
diff
changeset
|
98 |
1155 | 99 #ifdef SYNTAX_TABLE |
100 | |
101 extern char *re_syntax_table; | |
102 | |
103 #else /* not SYNTAX_TABLE */ | |
104 | |
105 /* How many characters in the character set. */ | |
106 #define CHAR_SET_SIZE 256 | |
107 | |
108 static char re_syntax_table[CHAR_SET_SIZE]; | |
109 | |
110 static void | |
111 init_syntax_once () | |
112 { | |
113 register int c; | |
114 static int done = 0; | |
115 | |
116 if (done) | |
117 return; | |
118 | |
119 bzero (re_syntax_table, sizeof re_syntax_table); | |
120 | |
121 for (c = 'a'; c <= 'z'; c++) | |
122 re_syntax_table[c] = Sword; | |
123 | |
124 for (c = 'A'; c <= 'Z'; c++) | |
125 re_syntax_table[c] = Sword; | |
126 | |
127 for (c = '0'; c <= '9'; c++) | |
128 re_syntax_table[c] = Sword; | |
129 | |
130 re_syntax_table['_'] = Sword; | |
131 | |
132 done = 1; | |
133 } | |
134 | |
135 #endif /* not SYNTAX_TABLE */ | |
136 | |
137 #define SYNTAX(c) re_syntax_table[c] | |
138 | |
139 #endif /* not emacs */ | |
140 | |
141 /* Get the interface, including the syntax bits. */ | |
142 #include "regex.h" | |
143 | |
144 /* isalpha etc. are used for the character classes. */ | |
145 #include <ctype.h> | |
1668 | 146 |
2465 | 147 /* Jim Meyering writes: |
148 | |
149 "... Some ctype macros are valid only for character codes that | |
150 isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when | |
151 using /bin/cc or gcc but without giving an ansi option). So, all | |
152 ctype uses should be through macros like ISPRINT... If | |
153 STDC_HEADERS is defined, then autoconf has verified that the ctype | |
154 macros don't need to be guarded with references to isascii. ... | |
155 Defining isascii to 1 should let any compiler worth its salt | |
156 eliminate the && through constant folding." */ | |
5076 | 157 |
158 #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII)) | |
159 #define ISASCII(c) 1 | |
160 #else | |
161 #define ISASCII(c) isascii(c) | |
1668 | 162 #endif |
163 | |
164 #ifdef isblank | |
5076 | 165 #define ISBLANK(c) (ISASCII (c) && isblank (c)) |
1668 | 166 #else |
167 #define ISBLANK(c) ((c) == ' ' || (c) == '\t') | |
1155 | 168 #endif |
1668 | 169 #ifdef isgraph |
5076 | 170 #define ISGRAPH(c) (ISASCII (c) && isgraph (c)) |
1668 | 171 #else |
5076 | 172 #define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c)) |
1155 | 173 #endif |
174 | |
5076 | 175 #define ISPRINT(c) (ISASCII (c) && isprint (c)) |
176 #define ISDIGIT(c) (ISASCII (c) && isdigit (c)) | |
177 #define ISALNUM(c) (ISASCII (c) && isalnum (c)) | |
178 #define ISALPHA(c) (ISASCII (c) && isalpha (c)) | |
179 #define ISCNTRL(c) (ISASCII (c) && iscntrl (c)) | |
180 #define ISLOWER(c) (ISASCII (c) && islower (c)) | |
181 #define ISPUNCT(c) (ISASCII (c) && ispunct (c)) | |
182 #define ISSPACE(c) (ISASCII (c) && isspace (c)) | |
183 #define ISUPPER(c) (ISASCII (c) && isupper (c)) | |
184 #define ISXDIGIT(c) (ISASCII (c) && isxdigit (c)) | |
1668 | 185 |
1155 | 186 #ifndef NULL |
187 #define NULL 0 | |
188 #endif | |
189 | |
190 /* We remove any previous definition of `SIGN_EXTEND_CHAR', | |
191 since ours (we hope) works properly with all combinations of | |
192 machines, compilers, `char' and `unsigned char' argument types. | |
193 (Per Bothner suggested the basic approach.) */ | |
194 #undef SIGN_EXTEND_CHAR | |
195 #if __STDC__ | |
196 #define SIGN_EXTEND_CHAR(c) ((signed char) (c)) | |
1637 | 197 #else /* not __STDC__ */ |
1155 | 198 /* As in Harbison and Steele. */ |
199 #define SIGN_EXTEND_CHAR(c) ((((unsigned char) (c)) ^ 128) - 128) | |
200 #endif | |
201 | |
202 /* Should we use malloc or alloca? If REGEX_MALLOC is not defined, we | |
203 use `alloca' instead of `malloc'. This is because using malloc in | |
204 re_search* or re_match* could cause memory leaks when C-g is used in | |
205 Emacs; also, malloc is slower and causes storage fragmentation. On | |
206 the other hand, malloc is more portable, and easier to debug. | |
207 | |
208 Because we sometimes use alloca, some routines have to be macros, | |
209 not functions -- `alloca'-allocated space disappears at the end of the | |
210 function it is called in. */ | |
211 | |
212 #ifdef REGEX_MALLOC | |
213 | |
214 #define REGEX_ALLOCATE malloc | |
215 #define REGEX_REALLOCATE(source, osize, nsize) realloc (source, nsize) | |
11622
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
216 #define REGEX_FREE free |
1155 | 217 |
218 #else /* not REGEX_MALLOC */ | |
219 | |
220 /* Emacs already defines alloca, sometimes. */ | |
221 #ifndef alloca | |
222 | |
223 /* Make alloca work the best possible way. */ | |
224 #ifdef __GNUC__ | |
225 #define alloca __builtin_alloca | |
226 #else /* not __GNUC__ */ | |
227 #if HAVE_ALLOCA_H | |
228 #include <alloca.h> | |
229 #else /* not __GNUC__ or HAVE_ALLOCA_H */ | |
230 #ifndef _AIX /* Already did AIX, up at the top. */ | |
231 char *alloca (); | |
232 #endif /* not _AIX */ | |
233 #endif /* not HAVE_ALLOCA_H */ | |
234 #endif /* not __GNUC__ */ | |
235 | |
236 #endif /* not alloca */ | |
237 | |
238 #define REGEX_ALLOCATE alloca | |
239 | |
240 /* Assumes a `char *destination' variable. */ | |
241 #define REGEX_REALLOCATE(source, osize, nsize) \ | |
242 (destination = (char *) alloca (nsize), \ | |
243 bcopy (source, destination, osize), \ | |
244 destination) | |
245 | |
11622
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
246 /* No need to do anything to free, after alloca. */ |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
247 #define REGEX_FREE(arg) (0) |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
248 |
1155 | 249 #endif /* not REGEX_MALLOC */ |
250 | |
11622
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
251 /* Define how to allocate the failure stack. */ |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
252 |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
253 #ifdef REL_ALLOC |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
254 #define REGEX_ALLOCATE_STACK(size) \ |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
255 r_alloc (&failure_stack_ptr, (size)) |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
256 #define REGEX_REALLOCATE_STACK(source, osize, nsize) \ |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
257 r_re_alloc (&failure_stack_ptr, (nsize)) |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
258 #define REGEX_FREE_STACK(ptr) \ |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
259 r_alloc_free (&failure_stack_ptr) |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
260 |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
261 #else /* not REL_ALLOC */ |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
262 |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
263 #ifdef REGEX_MALLOC |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
264 |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
265 #define REGEX_ALLOCATE_STACK malloc |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
266 #define REGEX_REALLOCATE_STACK(source, osize, nsize) realloc (source, nsize) |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
267 #define REGEX_FREE_STACK free |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
268 |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
269 #else /* not REGEX_MALLOC */ |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
270 |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
271 #define REGEX_ALLOCATE_STACK alloca |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
272 |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
273 #define REGEX_REALLOCATE_STACK(source, osize, nsize) \ |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
274 REGEX_REALLOCATE (source, osize, nsize) |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
275 /* No need to explicitly free anything. */ |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
276 #define REGEX_FREE_STACK(arg) |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
277 |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
278 #endif /* not REGEX_MALLOC */ |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
279 #endif /* not REL_ALLOC */ |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
280 |
1155 | 281 |
282 /* True if `size1' is non-NULL and PTR is pointing anywhere inside | |
283 `string1' or just past its end. This works if PTR is NULL, which is | |
284 a good thing. */ | |
285 #define FIRST_STRING_P(ptr) \ | |
286 (size1 && string1 <= (ptr) && (ptr) <= string1 + size1) | |
287 | |
288 /* (Re)Allocate N items of type T using malloc, or fail. */ | |
289 #define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t))) | |
290 #define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t))) | |
2949 | 291 #define RETALLOC_IF(addr, n, t) \ |
292 if (addr) RETALLOC((addr), (n), t); else (addr) = TALLOC ((n), t) | |
1155 | 293 #define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t))) |
294 | |
295 #define BYTEWIDTH 8 /* In bits. */ | |
296 | |
297 #define STREQ(s1, s2) ((strcmp (s1, s2) == 0)) | |
298 | |
5841 | 299 #undef MAX |
300 #undef MIN | |
1155 | 301 #define MAX(a, b) ((a) > (b) ? (a) : (b)) |
302 #define MIN(a, b) ((a) < (b) ? (a) : (b)) | |
303 | |
304 typedef char boolean; | |
305 #define false 0 | |
306 #define true 1 | |
6083 | 307 |
308 static int re_match_2_internal (); | |
1155 | 309 |
310 /* These are the command codes that appear in compiled regular | |
311 expressions. Some opcodes are followed by argument bytes. A | |
312 command code can specify any interpretation whatsoever for its | |
9585 | 313 arguments. Zero bytes may appear in the compiled regular expression. */ |
1155 | 314 |
315 typedef enum | |
316 { | |
317 no_op = 0, | |
318 | |
9983
ef6e1637c777
(re_opcode_t): New opcode `succeed'
Richard M. Stallman <rms@gnu.org>
parents:
9717
diff
changeset
|
319 /* Succeed right away--no more backtracking. */ |
ef6e1637c777
(re_opcode_t): New opcode `succeed'
Richard M. Stallman <rms@gnu.org>
parents:
9717
diff
changeset
|
320 succeed, |
ef6e1637c777
(re_opcode_t): New opcode `succeed'
Richard M. Stallman <rms@gnu.org>
parents:
9717
diff
changeset
|
321 |
1155 | 322 /* Followed by one byte giving n, then by n literal bytes. */ |
9585 | 323 exactn, |
1155 | 324 |
325 /* Matches any (more or less) character. */ | |
326 anychar, | |
327 | |
328 /* Matches any one char belonging to specified set. First | |
329 following byte is number of bitmap bytes. Then come bytes | |
330 for a bitmap saying which chars are in. Bits in each byte | |
331 are ordered low-bit-first. A character is in the set if its | |
332 bit is 1. A character too large to have a bit in the map is | |
333 automatically not in the set. */ | |
334 charset, | |
335 | |
336 /* Same parameters as charset, but match any character that is | |
337 not one of those specified. */ | |
338 charset_not, | |
339 | |
340 /* Start remembering the text that is matched, for storing in a | |
341 register. Followed by one byte with the register number, in | |
342 the range 0 to one less than the pattern buffer's re_nsub | |
343 field. Then followed by one byte with the number of groups | |
344 inner to this one. (This last has to be part of the | |
345 start_memory only because we need it in the on_failure_jump | |
346 of re_match_2.) */ | |
347 start_memory, | |
348 | |
349 /* Stop remembering the text that is matched and store it in a | |
350 memory register. Followed by one byte with the register | |
351 number, in the range 0 to one less than `re_nsub' in the | |
352 pattern buffer, and one byte with the number of inner groups, | |
353 just like `start_memory'. (We need the number of inner | |
354 groups here because we don't have any easy way of finding the | |
355 corresponding start_memory when we're at a stop_memory.) */ | |
356 stop_memory, | |
357 | |
358 /* Match a duplicate of something remembered. Followed by one | |
359 byte containing the register number. */ | |
360 duplicate, | |
361 | |
362 /* Fail unless at beginning of line. */ | |
363 begline, | |
364 | |
365 /* Fail unless at end of line. */ | |
366 endline, | |
367 | |
368 /* Succeeds if at beginning of buffer (if emacs) or at beginning | |
369 of string to be matched (if not). */ | |
370 begbuf, | |
371 | |
372 /* Analogously, for end of buffer/string. */ | |
373 endbuf, | |
374 | |
375 /* Followed by two byte relative address to which to jump. */ | |
376 jump, | |
377 | |
378 /* Same as jump, but marks the end of an alternative. */ | |
379 jump_past_alt, | |
380 | |
381 /* Followed by two-byte relative address of place to resume at | |
382 in case of failure. */ | |
383 on_failure_jump, | |
384 | |
385 /* Like on_failure_jump, but pushes a placeholder instead of the | |
386 current string position when executed. */ | |
387 on_failure_keep_string_jump, | |
388 | |
389 /* Throw away latest failure point and then jump to following | |
390 two-byte relative address. */ | |
391 pop_failure_jump, | |
392 | |
393 /* Change to pop_failure_jump if know won't have to backtrack to | |
394 match; otherwise change to jump. This is used to jump | |
395 back to the beginning of a repeat. If what follows this jump | |
396 clearly won't match what the repeat does, such that we can be | |
397 sure that there is no use backtracking out of repetitions | |
398 already matched, then we change it to a pop_failure_jump. | |
399 Followed by two-byte address. */ | |
400 maybe_pop_jump, | |
401 | |
402 /* Jump to following two-byte address, and push a dummy failure | |
403 point. This failure point will be thrown away if an attempt | |
404 is made to use it for a failure. A `+' construct makes this | |
405 before the first repeat. Also used as an intermediary kind | |
406 of jump when compiling an alternative. */ | |
407 dummy_failure_jump, | |
408 | |
409 /* Push a dummy failure point and continue. Used at the end of | |
410 alternatives. */ | |
411 push_dummy_failure, | |
412 | |
413 /* Followed by two-byte relative address and two-byte number n. | |
414 After matching N times, jump to the address upon failure. */ | |
415 succeed_n, | |
416 | |
417 /* Followed by two-byte relative address, and two-byte number n. | |
418 Jump to the address N times, then fail. */ | |
419 jump_n, | |
420 | |
421 /* Set the following two-byte relative address to the | |
422 subsequent two-byte number. The address *includes* the two | |
423 bytes of number. */ | |
424 set_number_at, | |
425 | |
426 wordchar, /* Matches any word-constituent character. */ | |
427 notwordchar, /* Matches any char that is not a word-constituent. */ | |
428 | |
429 wordbeg, /* Succeeds if at word beginning. */ | |
430 wordend, /* Succeeds if at word end. */ | |
431 | |
432 wordbound, /* Succeeds if at a word boundary. */ | |
433 notwordbound /* Succeeds if not at a word boundary. */ | |
434 | |
435 #ifdef emacs | |
436 ,before_dot, /* Succeeds if before point. */ | |
437 at_dot, /* Succeeds if at point. */ | |
438 after_dot, /* Succeeds if after point. */ | |
439 | |
440 /* Matches any character whose syntax is specified. Followed by | |
441 a byte which contains a syntax code, e.g., Sword. */ | |
442 syntaxspec, | |
443 | |
444 /* Matches any character whose syntax is not that specified. */ | |
445 notsyntaxspec | |
446 #endif /* emacs */ | |
447 } re_opcode_t; | |
448 | |
449 /* Common operations on the compiled pattern. */ | |
450 | |
451 /* Store NUMBER in two contiguous bytes starting at DESTINATION. */ | |
452 | |
453 #define STORE_NUMBER(destination, number) \ | |
454 do { \ | |
455 (destination)[0] = (number) & 0377; \ | |
456 (destination)[1] = (number) >> 8; \ | |
457 } while (0) | |
458 | |
459 /* Same as STORE_NUMBER, except increment DESTINATION to | |
460 the byte after where the number is stored. Therefore, DESTINATION | |
461 must be an lvalue. */ | |
462 | |
463 #define STORE_NUMBER_AND_INCR(destination, number) \ | |
464 do { \ | |
465 STORE_NUMBER (destination, number); \ | |
466 (destination) += 2; \ | |
467 } while (0) | |
468 | |
469 /* Put into DESTINATION a number stored in two contiguous bytes starting | |
470 at SOURCE. */ | |
471 | |
472 #define EXTRACT_NUMBER(destination, source) \ | |
473 do { \ | |
474 (destination) = *(source) & 0377; \ | |
475 (destination) += SIGN_EXTEND_CHAR (*((source) + 1)) << 8; \ | |
476 } while (0) | |
477 | |
478 #ifdef DEBUG | |
479 static void | |
480 extract_number (dest, source) | |
481 int *dest; | |
482 unsigned char *source; | |
483 { | |
484 int temp = SIGN_EXTEND_CHAR (*(source + 1)); | |
485 *dest = *source & 0377; | |
486 *dest += temp << 8; | |
487 } | |
488 | |
489 #ifndef EXTRACT_MACROS /* To debug the macros. */ | |
490 #undef EXTRACT_NUMBER | |
491 #define EXTRACT_NUMBER(dest, src) extract_number (&dest, src) | |
492 #endif /* not EXTRACT_MACROS */ | |
493 | |
494 #endif /* DEBUG */ | |
495 | |
496 /* Same as EXTRACT_NUMBER, except increment SOURCE to after the number. | |
497 SOURCE must be an lvalue. */ | |
498 | |
499 #define EXTRACT_NUMBER_AND_INCR(destination, source) \ | |
500 do { \ | |
501 EXTRACT_NUMBER (destination, source); \ | |
502 (source) += 2; \ | |
503 } while (0) | |
504 | |
505 #ifdef DEBUG | |
506 static void | |
507 extract_number_and_incr (destination, source) | |
508 int *destination; | |
509 unsigned char **source; | |
510 { | |
511 extract_number (destination, *source); | |
512 *source += 2; | |
513 } | |
514 | |
515 #ifndef EXTRACT_MACROS | |
516 #undef EXTRACT_NUMBER_AND_INCR | |
517 #define EXTRACT_NUMBER_AND_INCR(dest, src) \ | |
518 extract_number_and_incr (&dest, &src) | |
519 #endif /* not EXTRACT_MACROS */ | |
520 | |
521 #endif /* DEBUG */ | |
522 | |
523 /* If DEBUG is defined, Regex prints many voluminous messages about what | |
524 it is doing (if the variable `debug' is nonzero). If linked with the | |
525 main program in `iregex.c', you can enter patterns and strings | |
526 interactively. And if linked with the main program in `main.c' and | |
527 the other test files, you can run the already-written tests. */ | |
528 | |
529 #ifdef DEBUG | |
530 | |
531 /* We use standard I/O for debugging. */ | |
532 #include <stdio.h> | |
533 | |
534 /* It is useful to test things that ``must'' be true when debugging. */ | |
535 #include <assert.h> | |
536 | |
537 static int debug = 0; | |
538 | |
539 #define DEBUG_STATEMENT(e) e | |
540 #define DEBUG_PRINT1(x) if (debug) printf (x) | |
541 #define DEBUG_PRINT2(x1, x2) if (debug) printf (x1, x2) | |
542 #define DEBUG_PRINT3(x1, x2, x3) if (debug) printf (x1, x2, x3) | |
1637 | 543 #define DEBUG_PRINT4(x1, x2, x3, x4) if (debug) printf (x1, x2, x3, x4) |
1155 | 544 #define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) \ |
545 if (debug) print_partial_compiled_pattern (s, e) | |
546 #define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \ | |
547 if (debug) print_double_string (w, s1, sz1, s2, sz2) | |
548 | |
549 | |
550 /* Print the fastmap in human-readable form. */ | |
551 | |
552 void | |
553 print_fastmap (fastmap) | |
554 char *fastmap; | |
555 { | |
556 unsigned was_a_range = 0; | |
557 unsigned i = 0; | |
558 | |
559 while (i < (1 << BYTEWIDTH)) | |
560 { | |
561 if (fastmap[i++]) | |
562 { | |
563 was_a_range = 0; | |
10297
d6ba9ccec333
(regex_compile): Use putchar, not printchar.
Richard M. Stallman <rms@gnu.org>
parents:
10242
diff
changeset
|
564 putchar (i - 1); |
1155 | 565 while (i < (1 << BYTEWIDTH) && fastmap[i]) |
566 { | |
567 was_a_range = 1; | |
568 i++; | |
569 } | |
570 if (was_a_range) | |
571 { | |
572 printf ("-"); | |
10297
d6ba9ccec333
(regex_compile): Use putchar, not printchar.
Richard M. Stallman <rms@gnu.org>
parents:
10242
diff
changeset
|
573 putchar (i - 1); |
1155 | 574 } |
575 } | |
576 } | |
577 putchar ('\n'); | |
578 } | |
579 | |
580 | |
581 /* Print a compiled pattern string in human-readable form, starting at | |
582 the START pointer into it and ending just before the pointer END. */ | |
583 | |
584 void | |
585 print_partial_compiled_pattern (start, end) | |
586 unsigned char *start; | |
587 unsigned char *end; | |
588 { | |
589 int mcnt, mcnt2; | |
590 unsigned char *p = start; | |
591 unsigned char *pend = end; | |
592 | |
593 if (start == NULL) | |
594 { | |
595 printf ("(null)\n"); | |
596 return; | |
597 } | |
598 | |
599 /* Loop over pattern commands. */ | |
600 while (p < pend) | |
601 { | |
2615 | 602 printf ("%d:\t", p - start); |
603 | |
1155 | 604 switch ((re_opcode_t) *p++) |
605 { | |
606 case no_op: | |
607 printf ("/no_op"); | |
608 break; | |
609 | |
610 case exactn: | |
611 mcnt = *p++; | |
612 printf ("/exactn/%d", mcnt); | |
613 do | |
614 { | |
615 putchar ('/'); | |
10297
d6ba9ccec333
(regex_compile): Use putchar, not printchar.
Richard M. Stallman <rms@gnu.org>
parents:
10242
diff
changeset
|
616 putchar (*p++); |
1155 | 617 } |
618 while (--mcnt); | |
619 break; | |
620 | |
621 case start_memory: | |
622 mcnt = *p++; | |
623 printf ("/start_memory/%d/%d", mcnt, *p++); | |
624 break; | |
625 | |
626 case stop_memory: | |
627 mcnt = *p++; | |
628 printf ("/stop_memory/%d/%d", mcnt, *p++); | |
629 break; | |
630 | |
631 case duplicate: | |
632 printf ("/duplicate/%d", *p++); | |
633 break; | |
634 | |
635 case anychar: | |
636 printf ("/anychar"); | |
637 break; | |
638 | |
639 case charset: | |
640 case charset_not: | |
641 { | |
2615 | 642 register int c, last = -100; |
643 register int in_range = 0; | |
644 | |
645 printf ("/charset [%s", | |
646 (re_opcode_t) *(p - 1) == charset_not ? "^" : ""); | |
1155 | 647 |
648 assert (p + *p < pend); | |
649 | |
2615 | 650 for (c = 0; c < 256; c++) |
651 if (c / 8 < *p | |
652 && (p[1 + (c/8)] & (1 << (c % 8)))) | |
653 { | |
654 /* Are we starting a range? */ | |
655 if (last + 1 == c && ! in_range) | |
656 { | |
657 putchar ('-'); | |
658 in_range = 1; | |
659 } | |
660 /* Have we broken a range? */ | |
661 else if (last + 1 != c && in_range) | |
1155 | 662 { |
10297
d6ba9ccec333
(regex_compile): Use putchar, not printchar.
Richard M. Stallman <rms@gnu.org>
parents:
10242
diff
changeset
|
663 putchar (last); |
2615 | 664 in_range = 0; |
665 } | |
1155 | 666 |
2615 | 667 if (! in_range) |
10297
d6ba9ccec333
(regex_compile): Use putchar, not printchar.
Richard M. Stallman <rms@gnu.org>
parents:
10242
diff
changeset
|
668 putchar (c); |
2615 | 669 |
670 last = c; | |
1155 | 671 } |
2615 | 672 |
673 if (in_range) | |
10297
d6ba9ccec333
(regex_compile): Use putchar, not printchar.
Richard M. Stallman <rms@gnu.org>
parents:
10242
diff
changeset
|
674 putchar (last); |
2615 | 675 |
676 putchar (']'); | |
677 | |
1155 | 678 p += 1 + *p; |
679 } | |
2615 | 680 break; |
1155 | 681 |
682 case begline: | |
683 printf ("/begline"); | |
684 break; | |
685 | |
686 case endline: | |
687 printf ("/endline"); | |
688 break; | |
689 | |
690 case on_failure_jump: | |
691 extract_number_and_incr (&mcnt, &p); | |
2615 | 692 printf ("/on_failure_jump to %d", p + mcnt - start); |
1155 | 693 break; |
694 | |
695 case on_failure_keep_string_jump: | |
696 extract_number_and_incr (&mcnt, &p); | |
2615 | 697 printf ("/on_failure_keep_string_jump to %d", p + mcnt - start); |
1155 | 698 break; |
699 | |
700 case dummy_failure_jump: | |
701 extract_number_and_incr (&mcnt, &p); | |
2615 | 702 printf ("/dummy_failure_jump to %d", p + mcnt - start); |
1155 | 703 break; |
704 | |
705 case push_dummy_failure: | |
706 printf ("/push_dummy_failure"); | |
707 break; | |
708 | |
709 case maybe_pop_jump: | |
710 extract_number_and_incr (&mcnt, &p); | |
2615 | 711 printf ("/maybe_pop_jump to %d", p + mcnt - start); |
1155 | 712 break; |
713 | |
714 case pop_failure_jump: | |
715 extract_number_and_incr (&mcnt, &p); | |
2615 | 716 printf ("/pop_failure_jump to %d", p + mcnt - start); |
1155 | 717 break; |
718 | |
719 case jump_past_alt: | |
720 extract_number_and_incr (&mcnt, &p); | |
2615 | 721 printf ("/jump_past_alt to %d", p + mcnt - start); |
1155 | 722 break; |
723 | |
724 case jump: | |
725 extract_number_and_incr (&mcnt, &p); | |
2615 | 726 printf ("/jump to %d", p + mcnt - start); |
1155 | 727 break; |
728 | |
729 case succeed_n: | |
730 extract_number_and_incr (&mcnt, &p); | |
731 extract_number_and_incr (&mcnt2, &p); | |
2615 | 732 printf ("/succeed_n to %d, %d times", p + mcnt - start, mcnt2); |
1155 | 733 break; |
734 | |
735 case jump_n: | |
736 extract_number_and_incr (&mcnt, &p); | |
737 extract_number_and_incr (&mcnt2, &p); | |
2615 | 738 printf ("/jump_n to %d, %d times", p + mcnt - start, mcnt2); |
1155 | 739 break; |
740 | |
741 case set_number_at: | |
742 extract_number_and_incr (&mcnt, &p); | |
743 extract_number_and_incr (&mcnt2, &p); | |
2615 | 744 printf ("/set_number_at location %d to %d", p + mcnt - start, mcnt2); |
1155 | 745 break; |
746 | |
747 case wordbound: | |
748 printf ("/wordbound"); | |
749 break; | |
750 | |
751 case notwordbound: | |
752 printf ("/notwordbound"); | |
753 break; | |
754 | |
755 case wordbeg: | |
756 printf ("/wordbeg"); | |
757 break; | |
758 | |
759 case wordend: | |
760 printf ("/wordend"); | |
761 | |
762 #ifdef emacs | |
763 case before_dot: | |
764 printf ("/before_dot"); | |
765 break; | |
766 | |
767 case at_dot: | |
768 printf ("/at_dot"); | |
769 break; | |
770 | |
771 case after_dot: | |
772 printf ("/after_dot"); | |
773 break; | |
774 | |
775 case syntaxspec: | |
776 printf ("/syntaxspec"); | |
777 mcnt = *p++; | |
778 printf ("/%d", mcnt); | |
779 break; | |
780 | |
781 case notsyntaxspec: | |
782 printf ("/notsyntaxspec"); | |
783 mcnt = *p++; | |
784 printf ("/%d", mcnt); | |
785 break; | |
786 #endif /* emacs */ | |
787 | |
788 case wordchar: | |
789 printf ("/wordchar"); | |
790 break; | |
791 | |
792 case notwordchar: | |
793 printf ("/notwordchar"); | |
794 break; | |
795 | |
796 case begbuf: | |
797 printf ("/begbuf"); | |
798 break; | |
799 | |
800 case endbuf: | |
801 printf ("/endbuf"); | |
802 break; | |
803 | |
804 default: | |
805 printf ("?%d", *(p-1)); | |
806 } | |
2615 | 807 |
808 putchar ('\n'); | |
1155 | 809 } |
2615 | 810 |
811 printf ("%d:\tend of pattern.\n", p - start); | |
1155 | 812 } |
813 | |
814 | |
815 void | |
816 print_compiled_pattern (bufp) | |
817 struct re_pattern_buffer *bufp; | |
818 { | |
819 unsigned char *buffer = bufp->buffer; | |
820 | |
821 print_partial_compiled_pattern (buffer, buffer + bufp->used); | |
822 printf ("%d bytes used/%d bytes allocated.\n", bufp->used, bufp->allocated); | |
823 | |
824 if (bufp->fastmap_accurate && bufp->fastmap) | |
825 { | |
826 printf ("fastmap: "); | |
827 print_fastmap (bufp->fastmap); | |
828 } | |
829 | |
830 printf ("re_nsub: %d\t", bufp->re_nsub); | |
831 printf ("regs_alloc: %d\t", bufp->regs_allocated); | |
832 printf ("can_be_null: %d\t", bufp->can_be_null); | |
833 printf ("newline_anchor: %d\n", bufp->newline_anchor); | |
834 printf ("no_sub: %d\t", bufp->no_sub); | |
835 printf ("not_bol: %d\t", bufp->not_bol); | |
836 printf ("not_eol: %d\t", bufp->not_eol); | |
837 printf ("syntax: %d\n", bufp->syntax); | |
838 /* Perhaps we should print the translate table? */ | |
839 } | |
840 | |
841 | |
842 void | |
843 print_double_string (where, string1, size1, string2, size2) | |
844 const char *where; | |
845 const char *string1; | |
846 const char *string2; | |
847 int size1; | |
848 int size2; | |
849 { | |
850 unsigned this_char; | |
851 | |
852 if (where == NULL) | |
853 printf ("(null)"); | |
854 else | |
855 { | |
856 if (FIRST_STRING_P (where)) | |
857 { | |
858 for (this_char = where - string1; this_char < size1; this_char++) | |
10297
d6ba9ccec333
(regex_compile): Use putchar, not printchar.
Richard M. Stallman <rms@gnu.org>
parents:
10242
diff
changeset
|
859 putchar (string1[this_char]); |
1155 | 860 |
861 where = string2; | |
862 } | |
863 | |
864 for (this_char = where - string2; this_char < size2; this_char++) | |
10297
d6ba9ccec333
(regex_compile): Use putchar, not printchar.
Richard M. Stallman <rms@gnu.org>
parents:
10242
diff
changeset
|
865 putchar (string2[this_char]); |
1155 | 866 } |
867 } | |
868 | |
869 #else /* not DEBUG */ | |
870 | |
871 #undef assert | |
872 #define assert(e) | |
873 | |
874 #define DEBUG_STATEMENT(e) | |
875 #define DEBUG_PRINT1(x) | |
876 #define DEBUG_PRINT2(x1, x2) | |
877 #define DEBUG_PRINT3(x1, x2, x3) | |
1637 | 878 #define DEBUG_PRINT4(x1, x2, x3, x4) |
1155 | 879 #define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) |
880 #define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) | |
881 | |
882 #endif /* not DEBUG */ | |
883 | |
884 /* Set by `re_set_syntax' to the current regexp syntax to recognize. Can | |
885 also be assigned to arbitrarily: each pattern buffer stores its own | |
886 syntax, so it can be changed between regex compilations. */ | |
9983
ef6e1637c777
(re_opcode_t): New opcode `succeed'
Richard M. Stallman <rms@gnu.org>
parents:
9717
diff
changeset
|
887 /* This has no initializer because initialized variables in Emacs |
ef6e1637c777
(re_opcode_t): New opcode `succeed'
Richard M. Stallman <rms@gnu.org>
parents:
9717
diff
changeset
|
888 become read-only after dumping. */ |
ef6e1637c777
(re_opcode_t): New opcode `succeed'
Richard M. Stallman <rms@gnu.org>
parents:
9717
diff
changeset
|
889 reg_syntax_t re_syntax_options; |
1155 | 890 |
891 | |
892 /* Specify the precise syntax of regexps for compilation. This provides | |
893 for compatibility for various utilities which historically have | |
894 different, incompatible syntaxes. | |
895 | |
896 The argument SYNTAX is a bit mask comprised of the various bits | |
897 defined in regex.h. We return the old syntax. */ | |
898 | |
899 reg_syntax_t | |
900 re_set_syntax (syntax) | |
901 reg_syntax_t syntax; | |
902 { | |
903 reg_syntax_t ret = re_syntax_options; | |
904 | |
905 re_syntax_options = syntax; | |
906 return ret; | |
907 } | |
908 | |
909 /* This table gives an error message for each of the error codes listed | |
10090
672e07670453
(re_compile_pattern, re_comp, regerror): Add gettext wrapper.
Paul Eggert <eggert@twinsun.com>
parents:
10023
diff
changeset
|
910 in regex.h. Obviously the order here has to be same as there. |
672e07670453
(re_compile_pattern, re_comp, regerror): Add gettext wrapper.
Paul Eggert <eggert@twinsun.com>
parents:
10023
diff
changeset
|
911 POSIX doesn't require that we do anything for REG_NOERROR, |
672e07670453
(re_compile_pattern, re_comp, regerror): Add gettext wrapper.
Paul Eggert <eggert@twinsun.com>
parents:
10023
diff
changeset
|
912 but why not be nice? */ |
672e07670453
(re_compile_pattern, re_comp, regerror): Add gettext wrapper.
Paul Eggert <eggert@twinsun.com>
parents:
10023
diff
changeset
|
913 |
672e07670453
(re_compile_pattern, re_comp, regerror): Add gettext wrapper.
Paul Eggert <eggert@twinsun.com>
parents:
10023
diff
changeset
|
914 static const char *re_error_msgid[] = |
672e07670453
(re_compile_pattern, re_comp, regerror): Add gettext wrapper.
Paul Eggert <eggert@twinsun.com>
parents:
10023
diff
changeset
|
915 { "Success", /* REG_NOERROR */ |
1155 | 916 "No match", /* REG_NOMATCH */ |
917 "Invalid regular expression", /* REG_BADPAT */ | |
918 "Invalid collation character", /* REG_ECOLLATE */ | |
919 "Invalid character class name", /* REG_ECTYPE */ | |
920 "Trailing backslash", /* REG_EESCAPE */ | |
921 "Invalid back reference", /* REG_ESUBREG */ | |
922 "Unmatched [ or [^", /* REG_EBRACK */ | |
923 "Unmatched ( or \\(", /* REG_EPAREN */ | |
924 "Unmatched \\{", /* REG_EBRACE */ | |
925 "Invalid content of \\{\\}", /* REG_BADBR */ | |
926 "Invalid range end", /* REG_ERANGE */ | |
927 "Memory exhausted", /* REG_ESPACE */ | |
928 "Invalid preceding regular expression", /* REG_BADRPT */ | |
929 "Premature end of regular expression", /* REG_EEND */ | |
930 "Regular expression too big", /* REG_ESIZE */ | |
931 "Unmatched ) or \\)", /* REG_ERPAREN */ | |
932 }; | |
933 | |
2949 | 934 /* Avoiding alloca during matching, to placate r_alloc. */ |
935 | |
7011 | 936 /* Define MATCH_MAY_ALLOCATE unless we need to make sure that the |
2949 | 937 searching and matching functions should not call alloca. On some |
938 systems, alloca is implemented in terms of malloc, and if we're | |
939 using the relocating allocator routines, then malloc could cause a | |
940 relocation, which might (if the strings being searched are in the | |
941 ralloc heap) shift the data out from underneath the regexp | |
3614 | 942 routines. |
943 | |
8142
66a5487be3a7
Be less eager to define MATCH_MAY_ALLOCATE.
Richard M. Stallman <rms@gnu.org>
parents:
8114
diff
changeset
|
944 Here's another reason to avoid allocation: Emacs |
66a5487be3a7
Be less eager to define MATCH_MAY_ALLOCATE.
Richard M. Stallman <rms@gnu.org>
parents:
8114
diff
changeset
|
945 processes input from X in a signal handler; processing X input may |
3614 | 946 call malloc; if input arrives while a matching routine is calling |
947 malloc, then we're scrod. But Emacs can't just block input while | |
948 calling matching routines; then we don't notice interrupts when | |
949 they come in. So, Emacs blocks input around all regexp calls | |
950 except the matching calls, which it leaves unprotected, in the | |
951 faith that they will not malloc. */ | |
2952 | 952 |
953 /* Normally, this is fine. */ | |
954 #define MATCH_MAY_ALLOCATE | |
955 | |
11622
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
956 /* When using GNU C, we are not REALLY using the C alloca, no matter |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
957 what config.h may say. So don't take precautions for it. */ |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
958 #ifdef __GNUC__ |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
959 #undef C_ALLOCA |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
960 #endif |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
961 |
8142
66a5487be3a7
Be less eager to define MATCH_MAY_ALLOCATE.
Richard M. Stallman <rms@gnu.org>
parents:
8114
diff
changeset
|
962 /* The match routines may not allocate if (1) they would do it with malloc |
9585 | 963 and (2) it's not safe for them to use malloc. */ |
8142
66a5487be3a7
Be less eager to define MATCH_MAY_ALLOCATE.
Richard M. Stallman <rms@gnu.org>
parents:
8114
diff
changeset
|
964 #if (defined (C_ALLOCA) || defined (REGEX_MALLOC)) && (defined (emacs) || defined (REL_ALLOC)) |
2952 | 965 #undef MATCH_MAY_ALLOCATE |
2949 | 966 #endif |
967 | |
968 | |
969 /* Failure stack declarations and macros; both re_compile_fastmap and | |
970 re_match_2 use a failure stack. These have to be macros because of | |
11622
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
971 REGEX_ALLOCATE_STACK. */ |
2949 | 972 |
973 | |
974 /* Number of failure points for which to initially allocate space | |
975 when matching. If this number is exceeded, we allocate more | |
976 space, so it is not a hard limit. */ | |
977 #ifndef INIT_FAILURE_ALLOC | |
978 #define INIT_FAILURE_ALLOC 5 | |
979 #endif | |
980 | |
981 /* Roughly the maximum number of failure points on the stack. Would be | |
982 exactly that if always used MAX_FAILURE_SPACE each time we failed. | |
983 This is a variable only so users of regex can assign to it; we never | |
984 change it ourselves. */ | |
11622
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
985 #ifdef REL_ALLOC |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
986 int re_max_failures = 20000000; |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
987 #else |
2949 | 988 int re_max_failures = 2000; |
11622
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
989 #endif |
2949 | 990 |
11656
a40849041718
(union fail_stack_elt): New union.
Richard M. Stallman <rms@gnu.org>
parents:
11622
diff
changeset
|
991 union fail_stack_elt |
a40849041718
(union fail_stack_elt): New union.
Richard M. Stallman <rms@gnu.org>
parents:
11622
diff
changeset
|
992 { |
a40849041718
(union fail_stack_elt): New union.
Richard M. Stallman <rms@gnu.org>
parents:
11622
diff
changeset
|
993 unsigned char *pointer; |
a40849041718
(union fail_stack_elt): New union.
Richard M. Stallman <rms@gnu.org>
parents:
11622
diff
changeset
|
994 int integer; |
a40849041718
(union fail_stack_elt): New union.
Richard M. Stallman <rms@gnu.org>
parents:
11622
diff
changeset
|
995 }; |
a40849041718
(union fail_stack_elt): New union.
Richard M. Stallman <rms@gnu.org>
parents:
11622
diff
changeset
|
996 |
a40849041718
(union fail_stack_elt): New union.
Richard M. Stallman <rms@gnu.org>
parents:
11622
diff
changeset
|
997 typedef union fail_stack_elt fail_stack_elt_t; |
2949 | 998 |
999 typedef struct | |
1000 { | |
1001 fail_stack_elt_t *stack; | |
1002 unsigned size; | |
1003 unsigned avail; /* Offset of next open position. */ | |
1004 } fail_stack_type; | |
1005 | |
1006 #define FAIL_STACK_EMPTY() (fail_stack.avail == 0) | |
1007 #define FAIL_STACK_PTR_EMPTY() (fail_stack_ptr->avail == 0) | |
1008 #define FAIL_STACK_FULL() (fail_stack.avail == fail_stack.size) | |
1009 | |
1010 | |
1011 /* Initialize `fail_stack'. Do `return -2' if the alloc fails. */ | |
1012 | |
2952 | 1013 #ifdef MATCH_MAY_ALLOCATE |
2949 | 1014 #define INIT_FAIL_STACK() \ |
1015 do { \ | |
1016 fail_stack.stack = (fail_stack_elt_t *) \ | |
11622
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1017 REGEX_ALLOCATE_STACK (INIT_FAILURE_ALLOC * sizeof (fail_stack_elt_t)); \ |
2949 | 1018 \ |
1019 if (fail_stack.stack == NULL) \ | |
1020 return -2; \ | |
1021 \ | |
1022 fail_stack.size = INIT_FAILURE_ALLOC; \ | |
1023 fail_stack.avail = 0; \ | |
1024 } while (0) | |
1025 #else | |
1026 #define INIT_FAIL_STACK() \ | |
1027 do { \ | |
1028 fail_stack.avail = 0; \ | |
1029 } while (0) | |
1030 #endif | |
1031 | |
1032 | |
1033 /* Double the size of FAIL_STACK, up to approximately `re_max_failures' items. | |
1034 | |
1035 Return 1 if succeeds, and 0 if either ran out of memory | |
1036 allocating space for it or it was already too large. | |
1037 | |
11622
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1038 REGEX_REALLOCATE_STACK requires `destination' be declared. */ |
2949 | 1039 |
1040 #define DOUBLE_FAIL_STACK(fail_stack) \ | |
1041 ((fail_stack).size > re_max_failures * MAX_FAILURE_ITEMS \ | |
1042 ? 0 \ | |
1043 : ((fail_stack).stack = (fail_stack_elt_t *) \ | |
11622
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1044 REGEX_REALLOCATE_STACK ((fail_stack).stack, \ |
2949 | 1045 (fail_stack).size * sizeof (fail_stack_elt_t), \ |
1046 ((fail_stack).size << 1) * sizeof (fail_stack_elt_t)), \ | |
1047 \ | |
1048 (fail_stack).stack == NULL \ | |
1049 ? 0 \ | |
1050 : ((fail_stack).size <<= 1, \ | |
1051 1))) | |
1052 | |
1053 | |
11656
a40849041718
(union fail_stack_elt): New union.
Richard M. Stallman <rms@gnu.org>
parents:
11622
diff
changeset
|
1054 /* Push pointer POINTER on FAIL_STACK. |
2949 | 1055 Return 1 if was able to do so and 0 if ran out of memory allocating |
1056 space to do so. */ | |
11656
a40849041718
(union fail_stack_elt): New union.
Richard M. Stallman <rms@gnu.org>
parents:
11622
diff
changeset
|
1057 #define PUSH_PATTERN_OP(POINTER, FAIL_STACK) \ |
2949 | 1058 ((FAIL_STACK_FULL () \ |
11656
a40849041718
(union fail_stack_elt): New union.
Richard M. Stallman <rms@gnu.org>
parents:
11622
diff
changeset
|
1059 && !DOUBLE_FAIL_STACK (FAIL_STACK)) \ |
11622
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1060 ? 0 \ |
11656
a40849041718
(union fail_stack_elt): New union.
Richard M. Stallman <rms@gnu.org>
parents:
11622
diff
changeset
|
1061 : ((FAIL_STACK).stack[(FAIL_STACK).avail++].pointer = POINTER, \ |
11622
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1062 1)) |
2949 | 1063 |
11330
20a91bd7b59d
(re_match_2_internal): Eliminate cast of ptr to int.
Richard M. Stallman <rms@gnu.org>
parents:
10850
diff
changeset
|
1064 /* Push a pointer value onto the failure stack. |
20a91bd7b59d
(re_match_2_internal): Eliminate cast of ptr to int.
Richard M. Stallman <rms@gnu.org>
parents:
10850
diff
changeset
|
1065 Assumes the variable `fail_stack'. Probably should only |
2949 | 1066 be called from within `PUSH_FAILURE_POINT'. */ |
11330
20a91bd7b59d
(re_match_2_internal): Eliminate cast of ptr to int.
Richard M. Stallman <rms@gnu.org>
parents:
10850
diff
changeset
|
1067 #define PUSH_FAILURE_POINTER(item) \ |
11656
a40849041718
(union fail_stack_elt): New union.
Richard M. Stallman <rms@gnu.org>
parents:
11622
diff
changeset
|
1068 fail_stack.stack[fail_stack.avail++].pointer = (unsigned char *) (item) |
11330
20a91bd7b59d
(re_match_2_internal): Eliminate cast of ptr to int.
Richard M. Stallman <rms@gnu.org>
parents:
10850
diff
changeset
|
1069 |
20a91bd7b59d
(re_match_2_internal): Eliminate cast of ptr to int.
Richard M. Stallman <rms@gnu.org>
parents:
10850
diff
changeset
|
1070 /* This pushes an integer-valued item onto the failure stack. |
20a91bd7b59d
(re_match_2_internal): Eliminate cast of ptr to int.
Richard M. Stallman <rms@gnu.org>
parents:
10850
diff
changeset
|
1071 Assumes the variable `fail_stack'. Probably should only |
20a91bd7b59d
(re_match_2_internal): Eliminate cast of ptr to int.
Richard M. Stallman <rms@gnu.org>
parents:
10850
diff
changeset
|
1072 be called from within `PUSH_FAILURE_POINT'. */ |
20a91bd7b59d
(re_match_2_internal): Eliminate cast of ptr to int.
Richard M. Stallman <rms@gnu.org>
parents:
10850
diff
changeset
|
1073 #define PUSH_FAILURE_INT(item) \ |
11656
a40849041718
(union fail_stack_elt): New union.
Richard M. Stallman <rms@gnu.org>
parents:
11622
diff
changeset
|
1074 fail_stack.stack[fail_stack.avail++].integer = (item) |
a40849041718
(union fail_stack_elt): New union.
Richard M. Stallman <rms@gnu.org>
parents:
11622
diff
changeset
|
1075 |
a40849041718
(union fail_stack_elt): New union.
Richard M. Stallman <rms@gnu.org>
parents:
11622
diff
changeset
|
1076 /* Push a fail_stack_elt_t value onto the failure stack. |
a40849041718
(union fail_stack_elt): New union.
Richard M. Stallman <rms@gnu.org>
parents:
11622
diff
changeset
|
1077 Assumes the variable `fail_stack'. Probably should only |
a40849041718
(union fail_stack_elt): New union.
Richard M. Stallman <rms@gnu.org>
parents:
11622
diff
changeset
|
1078 be called from within `PUSH_FAILURE_POINT'. */ |
a40849041718
(union fail_stack_elt): New union.
Richard M. Stallman <rms@gnu.org>
parents:
11622
diff
changeset
|
1079 #define PUSH_FAILURE_ELT(item) \ |
a40849041718
(union fail_stack_elt): New union.
Richard M. Stallman <rms@gnu.org>
parents:
11622
diff
changeset
|
1080 fail_stack.stack[fail_stack.avail++] = (item) |
a40849041718
(union fail_stack_elt): New union.
Richard M. Stallman <rms@gnu.org>
parents:
11622
diff
changeset
|
1081 |
a40849041718
(union fail_stack_elt): New union.
Richard M. Stallman <rms@gnu.org>
parents:
11622
diff
changeset
|
1082 /* These three POP... operations complement the three PUSH... operations. |
a40849041718
(union fail_stack_elt): New union.
Richard M. Stallman <rms@gnu.org>
parents:
11622
diff
changeset
|
1083 All assume that `fail_stack' is nonempty. */ |
a40849041718
(union fail_stack_elt): New union.
Richard M. Stallman <rms@gnu.org>
parents:
11622
diff
changeset
|
1084 #define POP_FAILURE_POINTER() fail_stack.stack[--fail_stack.avail].pointer |
a40849041718
(union fail_stack_elt): New union.
Richard M. Stallman <rms@gnu.org>
parents:
11622
diff
changeset
|
1085 #define POP_FAILURE_INT() fail_stack.stack[--fail_stack.avail].integer |
a40849041718
(union fail_stack_elt): New union.
Richard M. Stallman <rms@gnu.org>
parents:
11622
diff
changeset
|
1086 #define POP_FAILURE_ELT() fail_stack.stack[--fail_stack.avail] |
2949 | 1087 |
1088 /* Used to omit pushing failure point id's when we're not debugging. */ | |
1089 #ifdef DEBUG | |
11330
20a91bd7b59d
(re_match_2_internal): Eliminate cast of ptr to int.
Richard M. Stallman <rms@gnu.org>
parents:
10850
diff
changeset
|
1090 #define DEBUG_PUSH PUSH_FAILURE_INT |
20a91bd7b59d
(re_match_2_internal): Eliminate cast of ptr to int.
Richard M. Stallman <rms@gnu.org>
parents:
10850
diff
changeset
|
1091 #define DEBUG_POP(item_addr) *(item_addr) = POP_FAILURE_INT () |
2949 | 1092 #else |
1093 #define DEBUG_PUSH(item) | |
1094 #define DEBUG_POP(item_addr) | |
1095 #endif | |
1096 | |
1097 | |
1098 /* Push the information about the state we will need | |
1099 if we ever fail back to it. | |
1100 | |
1101 Requires variables fail_stack, regstart, regend, reg_info, and | |
1102 num_regs be declared. DOUBLE_FAIL_STACK requires `destination' be | |
1103 declared. | |
1104 | |
1105 Does `return FAILURE_CODE' if runs out of memory. */ | |
1106 | |
1107 #define PUSH_FAILURE_POINT(pattern_place, string_place, failure_code) \ | |
1108 do { \ | |
1109 char *destination; \ | |
1110 /* Must be int, so when we don't save any registers, the arithmetic \ | |
1111 of 0 + -1 isn't done as unsigned. */ \ | |
1112 int this_reg; \ | |
1113 \ | |
1114 DEBUG_STATEMENT (failure_id++); \ | |
1115 DEBUG_STATEMENT (nfailure_points_pushed++); \ | |
1116 DEBUG_PRINT2 ("\nPUSH_FAILURE_POINT #%u:\n", failure_id); \ | |
1117 DEBUG_PRINT2 (" Before push, next avail: %d\n", (fail_stack).avail);\ | |
1118 DEBUG_PRINT2 (" size: %d\n", (fail_stack).size);\ | |
1119 \ | |
1120 DEBUG_PRINT2 (" slots needed: %d\n", NUM_FAILURE_ITEMS); \ | |
1121 DEBUG_PRINT2 (" available: %d\n", REMAINING_AVAIL_SLOTS); \ | |
1122 \ | |
1123 /* Ensure we have enough space allocated for what we will push. */ \ | |
1124 while (REMAINING_AVAIL_SLOTS < NUM_FAILURE_ITEMS) \ | |
1125 { \ | |
11622
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1126 if (!DOUBLE_FAIL_STACK (fail_stack)) \ |
2949 | 1127 return failure_code; \ |
1128 \ | |
1129 DEBUG_PRINT2 ("\n Doubled stack; size now: %d\n", \ | |
1130 (fail_stack).size); \ | |
1131 DEBUG_PRINT2 (" slots available: %d\n", REMAINING_AVAIL_SLOTS);\ | |
1132 } \ | |
1133 \ | |
1134 /* Push the info, starting with the registers. */ \ | |
1135 DEBUG_PRINT1 ("\n"); \ | |
1136 \ | |
1137 for (this_reg = lowest_active_reg; this_reg <= highest_active_reg; \ | |
1138 this_reg++) \ | |
1139 { \ | |
1140 DEBUG_PRINT2 (" Pushing reg: %d\n", this_reg); \ | |
1141 DEBUG_STATEMENT (num_regs_pushed++); \ | |
1142 \ | |
1143 DEBUG_PRINT2 (" start: 0x%x\n", regstart[this_reg]); \ | |
11330
20a91bd7b59d
(re_match_2_internal): Eliminate cast of ptr to int.
Richard M. Stallman <rms@gnu.org>
parents:
10850
diff
changeset
|
1144 PUSH_FAILURE_POINTER (regstart[this_reg]); \ |
2949 | 1145 \ |
1146 DEBUG_PRINT2 (" end: 0x%x\n", regend[this_reg]); \ | |
11330
20a91bd7b59d
(re_match_2_internal): Eliminate cast of ptr to int.
Richard M. Stallman <rms@gnu.org>
parents:
10850
diff
changeset
|
1147 PUSH_FAILURE_POINTER (regend[this_reg]); \ |
2949 | 1148 \ |
1149 DEBUG_PRINT2 (" info: 0x%x\n ", reg_info[this_reg]); \ | |
1150 DEBUG_PRINT2 (" match_null=%d", \ | |
1151 REG_MATCH_NULL_STRING_P (reg_info[this_reg])); \ | |
1152 DEBUG_PRINT2 (" active=%d", IS_ACTIVE (reg_info[this_reg])); \ | |
1153 DEBUG_PRINT2 (" matched_something=%d", \ | |
1154 MATCHED_SOMETHING (reg_info[this_reg])); \ | |
1155 DEBUG_PRINT2 (" ever_matched=%d", \ | |
1156 EVER_MATCHED_SOMETHING (reg_info[this_reg])); \ | |
1157 DEBUG_PRINT1 ("\n"); \ | |
11656
a40849041718
(union fail_stack_elt): New union.
Richard M. Stallman <rms@gnu.org>
parents:
11622
diff
changeset
|
1158 PUSH_FAILURE_ELT (reg_info[this_reg].word); \ |
2949 | 1159 } \ |
1160 \ | |
1161 DEBUG_PRINT2 (" Pushing low active reg: %d\n", lowest_active_reg);\ | |
11330
20a91bd7b59d
(re_match_2_internal): Eliminate cast of ptr to int.
Richard M. Stallman <rms@gnu.org>
parents:
10850
diff
changeset
|
1162 PUSH_FAILURE_INT (lowest_active_reg); \ |
2949 | 1163 \ |
1164 DEBUG_PRINT2 (" Pushing high active reg: %d\n", highest_active_reg);\ | |
11330
20a91bd7b59d
(re_match_2_internal): Eliminate cast of ptr to int.
Richard M. Stallman <rms@gnu.org>
parents:
10850
diff
changeset
|
1165 PUSH_FAILURE_INT (highest_active_reg); \ |
2949 | 1166 \ |
1167 DEBUG_PRINT2 (" Pushing pattern 0x%x: ", pattern_place); \ | |
1168 DEBUG_PRINT_COMPILED_PATTERN (bufp, pattern_place, pend); \ | |
11330
20a91bd7b59d
(re_match_2_internal): Eliminate cast of ptr to int.
Richard M. Stallman <rms@gnu.org>
parents:
10850
diff
changeset
|
1169 PUSH_FAILURE_POINTER (pattern_place); \ |
2949 | 1170 \ |
1171 DEBUG_PRINT2 (" Pushing string 0x%x: `", string_place); \ | |
1172 DEBUG_PRINT_DOUBLE_STRING (string_place, string1, size1, string2, \ | |
1173 size2); \ | |
1174 DEBUG_PRINT1 ("'\n"); \ | |
11330
20a91bd7b59d
(re_match_2_internal): Eliminate cast of ptr to int.
Richard M. Stallman <rms@gnu.org>
parents:
10850
diff
changeset
|
1175 PUSH_FAILURE_POINTER (string_place); \ |
2949 | 1176 \ |
1177 DEBUG_PRINT2 (" Pushing failure id: %u\n", failure_id); \ | |
1178 DEBUG_PUSH (failure_id); \ | |
1179 } while (0) | |
1180 | |
1181 /* This is the number of items that are pushed and popped on the stack | |
1182 for each register. */ | |
1183 #define NUM_REG_ITEMS 3 | |
1184 | |
1185 /* Individual items aside from the registers. */ | |
1186 #ifdef DEBUG | |
1187 #define NUM_NONREG_ITEMS 5 /* Includes failure point id. */ | |
1188 #else | |
1189 #define NUM_NONREG_ITEMS 4 | |
1190 #endif | |
1191 | |
1192 /* We push at most this many items on the stack. */ | |
1193 #define MAX_FAILURE_ITEMS ((num_regs - 1) * NUM_REG_ITEMS + NUM_NONREG_ITEMS) | |
1194 | |
1195 /* We actually push this many items. */ | |
1196 #define NUM_FAILURE_ITEMS \ | |
1197 ((highest_active_reg - lowest_active_reg + 1) * NUM_REG_ITEMS \ | |
1198 + NUM_NONREG_ITEMS) | |
1199 | |
1200 /* How many items can still be added to the stack without overflowing it. */ | |
1201 #define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail) | |
1202 | |
1203 | |
1204 /* Pops what PUSH_FAIL_STACK pushes. | |
1205 | |
1206 We restore into the parameters, all of which should be lvalues: | |
1207 STR -- the saved data position. | |
1208 PAT -- the saved pattern position. | |
1209 LOW_REG, HIGH_REG -- the highest and lowest active registers. | |
1210 REGSTART, REGEND -- arrays of string positions. | |
1211 REG_INFO -- array of information about each subexpression. | |
1212 | |
1213 Also assumes the variables `fail_stack' and (if debugging), `bufp', | |
1214 `pend', `string1', `size1', `string2', and `size2'. */ | |
1215 | |
1216 #define POP_FAILURE_POINT(str, pat, low_reg, high_reg, regstart, regend, reg_info)\ | |
1217 { \ | |
1218 DEBUG_STATEMENT (fail_stack_elt_t failure_id;) \ | |
1219 int this_reg; \ | |
1220 const unsigned char *string_temp; \ | |
1221 \ | |
1222 assert (!FAIL_STACK_EMPTY ()); \ | |
1223 \ | |
1224 /* Remove failure points and point to how many regs pushed. */ \ | |
1225 DEBUG_PRINT1 ("POP_FAILURE_POINT:\n"); \ | |
1226 DEBUG_PRINT2 (" Before pop, next avail: %d\n", fail_stack.avail); \ | |
1227 DEBUG_PRINT2 (" size: %d\n", fail_stack.size); \ | |
1228 \ | |
1229 assert (fail_stack.avail >= NUM_NONREG_ITEMS); \ | |
1230 \ | |
1231 DEBUG_POP (&failure_id); \ | |
1232 DEBUG_PRINT2 (" Popping failure id: %u\n", failure_id); \ | |
1233 \ | |
1234 /* If the saved string location is NULL, it came from an \ | |
1235 on_failure_keep_string_jump opcode, and we want to throw away the \ | |
1236 saved NULL, thus retaining our current position in the string. */ \ | |
11330
20a91bd7b59d
(re_match_2_internal): Eliminate cast of ptr to int.
Richard M. Stallman <rms@gnu.org>
parents:
10850
diff
changeset
|
1237 string_temp = POP_FAILURE_POINTER (); \ |
2949 | 1238 if (string_temp != NULL) \ |
1239 str = (const char *) string_temp; \ | |
1240 \ | |
1241 DEBUG_PRINT2 (" Popping string 0x%x: `", str); \ | |
1242 DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2); \ | |
1243 DEBUG_PRINT1 ("'\n"); \ | |
1244 \ | |
11330
20a91bd7b59d
(re_match_2_internal): Eliminate cast of ptr to int.
Richard M. Stallman <rms@gnu.org>
parents:
10850
diff
changeset
|
1245 pat = (unsigned char *) POP_FAILURE_POINTER (); \ |
2949 | 1246 DEBUG_PRINT2 (" Popping pattern 0x%x: ", pat); \ |
1247 DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend); \ | |
1248 \ | |
1249 /* Restore register info. */ \ | |
11330
20a91bd7b59d
(re_match_2_internal): Eliminate cast of ptr to int.
Richard M. Stallman <rms@gnu.org>
parents:
10850
diff
changeset
|
1250 high_reg = (unsigned) POP_FAILURE_INT (); \ |
2949 | 1251 DEBUG_PRINT2 (" Popping high active reg: %d\n", high_reg); \ |
1252 \ | |
11330
20a91bd7b59d
(re_match_2_internal): Eliminate cast of ptr to int.
Richard M. Stallman <rms@gnu.org>
parents:
10850
diff
changeset
|
1253 low_reg = (unsigned) POP_FAILURE_INT (); \ |
2949 | 1254 DEBUG_PRINT2 (" Popping low active reg: %d\n", low_reg); \ |
1255 \ | |
1256 for (this_reg = high_reg; this_reg >= low_reg; this_reg--) \ | |
1257 { \ | |
1258 DEBUG_PRINT2 (" Popping reg: %d\n", this_reg); \ | |
1259 \ | |
11656
a40849041718
(union fail_stack_elt): New union.
Richard M. Stallman <rms@gnu.org>
parents:
11622
diff
changeset
|
1260 reg_info[this_reg].word = POP_FAILURE_ELT (); \ |
2949 | 1261 DEBUG_PRINT2 (" info: 0x%x\n", reg_info[this_reg]); \ |
1262 \ | |
11330
20a91bd7b59d
(re_match_2_internal): Eliminate cast of ptr to int.
Richard M. Stallman <rms@gnu.org>
parents:
10850
diff
changeset
|
1263 regend[this_reg] = (const char *) POP_FAILURE_POINTER (); \ |
2949 | 1264 DEBUG_PRINT2 (" end: 0x%x\n", regend[this_reg]); \ |
1265 \ | |
11330
20a91bd7b59d
(re_match_2_internal): Eliminate cast of ptr to int.
Richard M. Stallman <rms@gnu.org>
parents:
10850
diff
changeset
|
1266 regstart[this_reg] = (const char *) POP_FAILURE_POINTER (); \ |
2949 | 1267 DEBUG_PRINT2 (" start: 0x%x\n", regstart[this_reg]); \ |
1268 } \ | |
1269 \ | |
10242
8fa35cc770be
(SET_REGS_MATCHED): Do nothing if set_regs_matched_done is 1.
Richard M. Stallman <rms@gnu.org>
parents:
10148
diff
changeset
|
1270 set_regs_matched_done = 0; \ |
2949 | 1271 DEBUG_STATEMENT (nfailure_points_popped++); \ |
1272 } /* POP_FAILURE_POINT */ | |
1273 | |
1274 | |
1275 | |
1276 /* Structure for per-register (a.k.a. per-group) information. | |
11656
a40849041718
(union fail_stack_elt): New union.
Richard M. Stallman <rms@gnu.org>
parents:
11622
diff
changeset
|
1277 Other register information, such as the |
2949 | 1278 starting and ending positions (which are addresses), and the list of |
1279 inner groups (which is a bits list) are maintained in separate | |
1280 variables. | |
1281 | |
1282 We are making a (strictly speaking) nonportable assumption here: that | |
1283 the compiler will pack our bit fields into something that fits into | |
1284 the type of `word', i.e., is something that fits into one item on the | |
1285 failure stack. */ | |
11656
a40849041718
(union fail_stack_elt): New union.
Richard M. Stallman <rms@gnu.org>
parents:
11622
diff
changeset
|
1286 |
2949 | 1287 typedef union |
1288 { | |
1289 fail_stack_elt_t word; | |
1290 struct | |
1291 { | |
1292 /* This field is one if this group can match the empty string, | |
1293 zero if not. If not yet determined, `MATCH_NULL_UNSET_VALUE'. */ | |
1294 #define MATCH_NULL_UNSET_VALUE 3 | |
1295 unsigned match_null_string_p : 2; | |
1296 unsigned is_active : 1; | |
1297 unsigned matched_something : 1; | |
1298 unsigned ever_matched_something : 1; | |
1299 } bits; | |
1300 } register_info_type; | |
1301 | |
1302 #define REG_MATCH_NULL_STRING_P(R) ((R).bits.match_null_string_p) | |
1303 #define IS_ACTIVE(R) ((R).bits.is_active) | |
1304 #define MATCHED_SOMETHING(R) ((R).bits.matched_something) | |
1305 #define EVER_MATCHED_SOMETHING(R) ((R).bits.ever_matched_something) | |
1306 | |
1307 | |
1308 /* Call this when have matched a real character; it sets `matched' flags | |
1309 for the subexpressions which we are currently inside. Also records | |
1310 that those subexprs have matched. */ | |
10850
0794141677db
(SET_REGS_MATCHED): Enclose if-stmt in `do {...} while(0)'
Jim Meyering <jim@meyering.net>
parents:
10456
diff
changeset
|
1311 #define SET_REGS_MATCHED() \ |
0794141677db
(SET_REGS_MATCHED): Enclose if-stmt in `do {...} while(0)'
Jim Meyering <jim@meyering.net>
parents:
10456
diff
changeset
|
1312 do \ |
0794141677db
(SET_REGS_MATCHED): Enclose if-stmt in `do {...} while(0)'
Jim Meyering <jim@meyering.net>
parents:
10456
diff
changeset
|
1313 { \ |
0794141677db
(SET_REGS_MATCHED): Enclose if-stmt in `do {...} while(0)'
Jim Meyering <jim@meyering.net>
parents:
10456
diff
changeset
|
1314 if (!set_regs_matched_done) \ |
0794141677db
(SET_REGS_MATCHED): Enclose if-stmt in `do {...} while(0)'
Jim Meyering <jim@meyering.net>
parents:
10456
diff
changeset
|
1315 { \ |
0794141677db
(SET_REGS_MATCHED): Enclose if-stmt in `do {...} while(0)'
Jim Meyering <jim@meyering.net>
parents:
10456
diff
changeset
|
1316 unsigned r; \ |
0794141677db
(SET_REGS_MATCHED): Enclose if-stmt in `do {...} while(0)'
Jim Meyering <jim@meyering.net>
parents:
10456
diff
changeset
|
1317 set_regs_matched_done = 1; \ |
0794141677db
(SET_REGS_MATCHED): Enclose if-stmt in `do {...} while(0)'
Jim Meyering <jim@meyering.net>
parents:
10456
diff
changeset
|
1318 for (r = lowest_active_reg; r <= highest_active_reg; r++) \ |
0794141677db
(SET_REGS_MATCHED): Enclose if-stmt in `do {...} while(0)'
Jim Meyering <jim@meyering.net>
parents:
10456
diff
changeset
|
1319 { \ |
0794141677db
(SET_REGS_MATCHED): Enclose if-stmt in `do {...} while(0)'
Jim Meyering <jim@meyering.net>
parents:
10456
diff
changeset
|
1320 MATCHED_SOMETHING (reg_info[r]) \ |
0794141677db
(SET_REGS_MATCHED): Enclose if-stmt in `do {...} while(0)'
Jim Meyering <jim@meyering.net>
parents:
10456
diff
changeset
|
1321 = EVER_MATCHED_SOMETHING (reg_info[r]) \ |
0794141677db
(SET_REGS_MATCHED): Enclose if-stmt in `do {...} while(0)'
Jim Meyering <jim@meyering.net>
parents:
10456
diff
changeset
|
1322 = 1; \ |
0794141677db
(SET_REGS_MATCHED): Enclose if-stmt in `do {...} while(0)'
Jim Meyering <jim@meyering.net>
parents:
10456
diff
changeset
|
1323 } \ |
0794141677db
(SET_REGS_MATCHED): Enclose if-stmt in `do {...} while(0)'
Jim Meyering <jim@meyering.net>
parents:
10456
diff
changeset
|
1324 } \ |
0794141677db
(SET_REGS_MATCHED): Enclose if-stmt in `do {...} while(0)'
Jim Meyering <jim@meyering.net>
parents:
10456
diff
changeset
|
1325 } \ |
0794141677db
(SET_REGS_MATCHED): Enclose if-stmt in `do {...} while(0)'
Jim Meyering <jim@meyering.net>
parents:
10456
diff
changeset
|
1326 while (0) |
2949 | 1327 |
1328 /* Registers are set to a sentinel when they haven't yet matched. */ | |
10148
692093cc08d7
[REG_UNSET_VALUE]: Define to the address of a static variable rather than to
Jim Meyering <jim@meyering.net>
parents:
10100
diff
changeset
|
1329 static char reg_unset_dummy; |
692093cc08d7
[REG_UNSET_VALUE]: Define to the address of a static variable rather than to
Jim Meyering <jim@meyering.net>
parents:
10100
diff
changeset
|
1330 #define REG_UNSET_VALUE (®_unset_dummy) |
2949 | 1331 #define REG_UNSET(e) ((e) == REG_UNSET_VALUE) |
1332 | |
1155 | 1333 /* Subroutine declarations and macros for regex_compile. */ |
1334 | |
1335 static void store_op1 (), store_op2 (); | |
1336 static void insert_op1 (), insert_op2 (); | |
1337 static boolean at_begline_loc_p (), at_endline_loc_p (); | |
1338 static boolean group_in_compile_stack (); | |
1339 static reg_errcode_t compile_range (); | |
1340 | |
1341 /* Fetch the next character in the uncompiled pattern---translating it | |
1342 if necessary. Also cast from a signed character in the constant | |
1343 string passed to us by the user to an unsigned char that we can use | |
1344 as an array index (in, e.g., `translate'). */ | |
1345 #define PATFETCH(c) \ | |
1346 do {if (p == pend) return REG_EEND; \ | |
1347 c = (unsigned char) *p++; \ | |
1348 if (translate) c = translate[c]; \ | |
1349 } while (0) | |
1350 | |
1351 /* Fetch the next character in the uncompiled pattern, with no | |
1352 translation. */ | |
1353 #define PATFETCH_RAW(c) \ | |
1354 do {if (p == pend) return REG_EEND; \ | |
1355 c = (unsigned char) *p++; \ | |
1356 } while (0) | |
1357 | |
1358 /* Go backwards one character in the pattern. */ | |
1359 #define PATUNFETCH p-- | |
1360 | |
1361 | |
1362 /* If `translate' is non-null, return translate[D], else just D. We | |
1363 cast the subscript to translate because some data is declared as | |
1364 `char *', to avoid warnings when a string constant is passed. But | |
1365 when we use a character as a subscript we must make it unsigned. */ | |
1366 #define TRANSLATE(d) (translate ? translate[(unsigned char) (d)] : (d)) | |
1367 | |
1368 | |
1369 /* Macros for outputting the compiled pattern into `buffer'. */ | |
1370 | |
1371 /* If the buffer isn't allocated when it comes in, use this. */ | |
1372 #define INIT_BUF_SIZE 32 | |
1373 | |
1374 /* Make sure we have at least N more bytes of space in buffer. */ | |
1375 #define GET_BUFFER_SPACE(n) \ | |
1376 while (b - bufp->buffer + (n) > bufp->allocated) \ | |
1377 EXTEND_BUFFER () | |
1378 | |
1379 /* Make sure we have one more byte of buffer space and then add C to it. */ | |
1380 #define BUF_PUSH(c) \ | |
1381 do { \ | |
1382 GET_BUFFER_SPACE (1); \ | |
1383 *b++ = (unsigned char) (c); \ | |
1384 } while (0) | |
1385 | |
1386 | |
1387 /* Ensure we have two more bytes of buffer space and then append C1 and C2. */ | |
1388 #define BUF_PUSH_2(c1, c2) \ | |
1389 do { \ | |
1390 GET_BUFFER_SPACE (2); \ | |
1391 *b++ = (unsigned char) (c1); \ | |
1392 *b++ = (unsigned char) (c2); \ | |
1393 } while (0) | |
1394 | |
1395 | |
1396 /* As with BUF_PUSH_2, except for three bytes. */ | |
1397 #define BUF_PUSH_3(c1, c2, c3) \ | |
1398 do { \ | |
1399 GET_BUFFER_SPACE (3); \ | |
1400 *b++ = (unsigned char) (c1); \ | |
1401 *b++ = (unsigned char) (c2); \ | |
1402 *b++ = (unsigned char) (c3); \ | |
1403 } while (0) | |
1404 | |
1405 | |
1406 /* Store a jump with opcode OP at LOC to location TO. We store a | |
1407 relative address offset by the three bytes the jump itself occupies. */ | |
1408 #define STORE_JUMP(op, loc, to) \ | |
1409 store_op1 (op, loc, (to) - (loc) - 3) | |
1410 | |
1411 /* Likewise, for a two-argument jump. */ | |
1412 #define STORE_JUMP2(op, loc, to, arg) \ | |
1413 store_op2 (op, loc, (to) - (loc) - 3, arg) | |
1414 | |
1415 /* Like `STORE_JUMP', but for inserting. Assume `b' is the buffer end. */ | |
1416 #define INSERT_JUMP(op, loc, to) \ | |
1417 insert_op1 (op, loc, (to) - (loc) - 3, b) | |
1418 | |
1419 /* Like `STORE_JUMP2', but for inserting. Assume `b' is the buffer end. */ | |
1420 #define INSERT_JUMP2(op, loc, to, arg) \ | |
1421 insert_op2 (op, loc, (to) - (loc) - 3, arg, b) | |
1422 | |
1423 | |
1424 /* This is not an arbitrary limit: the arguments which represent offsets | |
1425 into the pattern are two bytes long. So if 2^16 bytes turns out to | |
1426 be too small, many things would have to change. */ | |
1427 #define MAX_BUF_SIZE (1L << 16) | |
1428 | |
1429 | |
1430 /* Extend the buffer by twice its current size via realloc and | |
1431 reset the pointers that pointed into the old block to point to the | |
1432 correct places in the new one. If extending the buffer results in it | |
1433 being larger than MAX_BUF_SIZE, then flag memory exhausted. */ | |
1434 #define EXTEND_BUFFER() \ | |
1435 do { \ | |
1436 unsigned char *old_buffer = bufp->buffer; \ | |
1437 if (bufp->allocated == MAX_BUF_SIZE) \ | |
1438 return REG_ESIZE; \ | |
1439 bufp->allocated <<= 1; \ | |
1440 if (bufp->allocated > MAX_BUF_SIZE) \ | |
1441 bufp->allocated = MAX_BUF_SIZE; \ | |
1442 bufp->buffer = (unsigned char *) realloc (bufp->buffer, bufp->allocated);\ | |
1443 if (bufp->buffer == NULL) \ | |
1444 return REG_ESPACE; \ | |
1445 /* If the buffer moved, move all the pointers into it. */ \ | |
1446 if (old_buffer != bufp->buffer) \ | |
1447 { \ | |
1448 b = (b - old_buffer) + bufp->buffer; \ | |
1449 begalt = (begalt - old_buffer) + bufp->buffer; \ | |
1450 if (fixup_alt_jump) \ | |
1451 fixup_alt_jump = (fixup_alt_jump - old_buffer) + bufp->buffer;\ | |
1452 if (laststart) \ | |
1453 laststart = (laststart - old_buffer) + bufp->buffer; \ | |
1454 if (pending_exact) \ | |
1455 pending_exact = (pending_exact - old_buffer) + bufp->buffer; \ | |
1456 } \ | |
1457 } while (0) | |
1458 | |
1459 | |
1460 /* Since we have one byte reserved for the register number argument to | |
1461 {start,stop}_memory, the maximum number of groups we can report | |
1462 things about is what fits in that byte. */ | |
1463 #define MAX_REGNUM 255 | |
1464 | |
1465 /* But patterns can have more than `MAX_REGNUM' registers. We just | |
1466 ignore the excess. */ | |
1467 typedef unsigned regnum_t; | |
1468 | |
1469 | |
1470 /* Macros for the compile stack. */ | |
1471 | |
1472 /* Since offsets can go either forwards or backwards, this type needs to | |
1473 be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1. */ | |
1474 typedef int pattern_offset_t; | |
1475 | |
1476 typedef struct | |
1477 { | |
1478 pattern_offset_t begalt_offset; | |
1479 pattern_offset_t fixup_alt_jump; | |
1480 pattern_offset_t inner_group_offset; | |
1481 pattern_offset_t laststart_offset; | |
1482 regnum_t regnum; | |
1483 } compile_stack_elt_t; | |
1484 | |
1485 | |
1486 typedef struct | |
1487 { | |
1488 compile_stack_elt_t *stack; | |
1489 unsigned size; | |
1490 unsigned avail; /* Offset of next open position. */ | |
1491 } compile_stack_type; | |
1492 | |
1493 | |
1494 #define INIT_COMPILE_STACK_SIZE 32 | |
1495 | |
1496 #define COMPILE_STACK_EMPTY (compile_stack.avail == 0) | |
1497 #define COMPILE_STACK_FULL (compile_stack.avail == compile_stack.size) | |
1498 | |
1499 /* The next available element. */ | |
1500 #define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail]) | |
1501 | |
1502 | |
1503 /* Set the bit for character C in a list. */ | |
1504 #define SET_LIST_BIT(c) \ | |
1505 (b[((unsigned char) (c)) / BYTEWIDTH] \ | |
1506 |= 1 << (((unsigned char) c) % BYTEWIDTH)) | |
1507 | |
1508 | |
1509 /* Get the next unsigned number in the uncompiled pattern. */ | |
1510 #define GET_UNSIGNED_NUMBER(num) \ | |
1511 { if (p != pend) \ | |
1512 { \ | |
1513 PATFETCH (c); \ | |
1668 | 1514 while (ISDIGIT (c)) \ |
1155 | 1515 { \ |
1516 if (num < 0) \ | |
1517 num = 0; \ | |
1518 num = num * 10 + c - '0'; \ | |
1519 if (p == pend) \ | |
1520 break; \ | |
1521 PATFETCH (c); \ | |
1522 } \ | |
1523 } \ | |
1524 } | |
1525 | |
1526 #define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */ | |
1527 | |
1528 #define IS_CHAR_CLASS(string) \ | |
1529 (STREQ (string, "alpha") || STREQ (string, "upper") \ | |
1530 || STREQ (string, "lower") || STREQ (string, "digit") \ | |
1531 || STREQ (string, "alnum") || STREQ (string, "xdigit") \ | |
1532 || STREQ (string, "space") || STREQ (string, "print") \ | |
1533 || STREQ (string, "punct") || STREQ (string, "graph") \ | |
1534 || STREQ (string, "cntrl") || STREQ (string, "blank")) | |
1535 | |
11622
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1536 #ifndef MATCH_MAY_ALLOCATE |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1537 |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1538 /* If we cannot allocate large objects within re_match_2_internal, |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1539 we make the fail stack and register vectors global. |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1540 The fail stack, we grow to the maximum size when a regexp |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1541 is compiled. |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1542 The register vectors, we adjust in size each time we |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1543 compile a regexp, according to the number of registers it needs. */ |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1544 |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1545 static fail_stack_type fail_stack; |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1546 |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1547 /* Size with which the following vectors are currently allocated. |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1548 That is so we can make them bigger as needed, |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1549 but never make them smaller. */ |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1550 static int regs_allocated_size; |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1551 |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1552 static const char ** regstart, ** regend; |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1553 static const char ** old_regstart, ** old_regend; |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1554 static const char **best_regstart, **best_regend; |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1555 static register_info_type *reg_info; |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1556 static const char **reg_dummy; |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1557 static register_info_type *reg_info_dummy; |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1558 |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1559 /* Make the register vectors big enough for NUM_REGS registers, |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1560 but don't make them smaller. */ |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1561 |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1562 static |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1563 regex_grow_registers (num_regs) |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1564 int num_regs; |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1565 { |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1566 if (num_regs > regs_allocated_size) |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1567 { |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1568 RETALLOC_IF (regstart, num_regs, const char *); |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1569 RETALLOC_IF (regend, num_regs, const char *); |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1570 RETALLOC_IF (old_regstart, num_regs, const char *); |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1571 RETALLOC_IF (old_regend, num_regs, const char *); |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1572 RETALLOC_IF (best_regstart, num_regs, const char *); |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1573 RETALLOC_IF (best_regend, num_regs, const char *); |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1574 RETALLOC_IF (reg_info, num_regs, register_info_type); |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1575 RETALLOC_IF (reg_dummy, num_regs, const char *); |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1576 RETALLOC_IF (reg_info_dummy, num_regs, register_info_type); |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1577 |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1578 regs_allocated_size = num_regs; |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1579 } |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1580 } |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1581 |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1582 #endif /* not MATCH_MAY_ALLOCATE */ |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
1583 |
1155 | 1584 /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX. |
1585 Returns one of error codes defined in `regex.h', or zero for success. | |
1586 | |
1587 Assumes the `allocated' (and perhaps `buffer') and `translate' | |
1588 fields are set in BUFP on entry. | |
1589 | |
1590 If it succeeds, results are put in BUFP (if it returns an error, the | |
1591 contents of BUFP are undefined): | |
1592 `buffer' is the compiled pattern; | |
1593 `syntax' is set to SYNTAX; | |
1594 `used' is set to the length of the compiled pattern; | |
1637 | 1595 `fastmap_accurate' is zero; |
1596 `re_nsub' is the number of subexpressions in PATTERN; | |
1597 `not_bol' and `not_eol' are zero; | |
1155 | 1598 |
1599 The `fastmap' and `newline_anchor' fields are neither | |
1600 examined nor set. */ | |
1601 | |
8102 | 1602 /* Return, freeing storage we allocated. */ |
1603 #define FREE_STACK_RETURN(value) \ | |
8254
694c4686b446
(re_match_2_internal): Add casts to shut up some compilers.
Richard M. Stallman <rms@gnu.org>
parents:
8142
diff
changeset
|
1604 return (free (compile_stack.stack), value) |
8102 | 1605 |
1155 | 1606 static reg_errcode_t |
1607 regex_compile (pattern, size, syntax, bufp) | |
1608 const char *pattern; | |
1609 int size; | |
1610 reg_syntax_t syntax; | |
1611 struct re_pattern_buffer *bufp; | |
1612 { | |
1613 /* We fetch characters from PATTERN here. Even though PATTERN is | |
1614 `char *' (i.e., signed), we declare these variables as unsigned, so | |
1615 they can be reliably used as array indices. */ | |
1616 register unsigned char c, c1; | |
1617 | |
5841 | 1618 /* A random temporary spot in PATTERN. */ |
1155 | 1619 const char *p1; |
1620 | |
1621 /* Points to the end of the buffer, where we should append. */ | |
1622 register unsigned char *b; | |
1623 | |
1624 /* Keeps track of unclosed groups. */ | |
1625 compile_stack_type compile_stack; | |
1626 | |
1627 /* Points to the current (ending) position in the pattern. */ | |
1628 const char *p = pattern; | |
1629 const char *pend = pattern + size; | |
1630 | |
1631 /* How to translate the characters in the pattern. */ | |
1632 char *translate = bufp->translate; | |
1633 | |
1634 /* Address of the count-byte of the most recently inserted `exactn' | |
1635 command. This makes it possible to tell if a new exact-match | |
1636 character can be added to that command or if the character requires | |
1637 a new `exactn' command. */ | |
1638 unsigned char *pending_exact = 0; | |
1639 | |
1640 /* Address of start of the most recently finished expression. | |
1641 This tells, e.g., postfix * where to find the start of its | |
1642 operand. Reset at the beginning of groups and alternatives. */ | |
1643 unsigned char *laststart = 0; | |
1644 | |
1645 /* Address of beginning of regexp, or inside of last group. */ | |
1646 unsigned char *begalt; | |
1647 | |
1648 /* Place in the uncompiled pattern (i.e., the {) to | |
1649 which to go back if the interval is invalid. */ | |
1650 const char *beg_interval; | |
1651 | |
1652 /* Address of the place where a forward jump should go to the end of | |
1653 the containing expression. Each alternative of an `or' -- except the | |
1654 last -- ends with a forward jump of this sort. */ | |
1655 unsigned char *fixup_alt_jump = 0; | |
1656 | |
1657 /* Counts open-groups as they are encountered. Remembered for the | |
1658 matching close-group on the compile stack, so the same register | |
1659 number is put in the stop_memory as the start_memory. */ | |
1660 regnum_t regnum = 0; | |
1661 | |
1662 #ifdef DEBUG | |
1663 DEBUG_PRINT1 ("\nCompiling pattern: "); | |
1664 if (debug) | |
1665 { | |
1666 unsigned debug_count; | |
1667 | |
1668 for (debug_count = 0; debug_count < size; debug_count++) | |
10297
d6ba9ccec333
(regex_compile): Use putchar, not printchar.
Richard M. Stallman <rms@gnu.org>
parents:
10242
diff
changeset
|
1669 putchar (pattern[debug_count]); |
1155 | 1670 putchar ('\n'); |
1671 } | |
1672 #endif /* DEBUG */ | |
1673 | |
1674 /* Initialize the compile stack. */ | |
1675 compile_stack.stack = TALLOC (INIT_COMPILE_STACK_SIZE, compile_stack_elt_t); | |
1676 if (compile_stack.stack == NULL) | |
1677 return REG_ESPACE; | |
1678 | |
1679 compile_stack.size = INIT_COMPILE_STACK_SIZE; | |
1680 compile_stack.avail = 0; | |
1681 | |
1682 /* Initialize the pattern buffer. */ | |
1683 bufp->syntax = syntax; | |
1684 bufp->fastmap_accurate = 0; | |
1685 bufp->not_bol = bufp->not_eol = 0; | |
1686 | |
1687 /* Set `used' to zero, so that if we return an error, the pattern | |
1688 printer (for debugging) will think there's no pattern. We reset it | |
1689 at the end. */ | |
1690 bufp->used = 0; | |
1691 | |
1692 /* Always count groups, whether or not bufp->no_sub is set. */ | |
1693 bufp->re_nsub = 0; | |
1694 | |
1695 #if !defined (emacs) && !defined (SYNTAX_TABLE) | |
1696 /* Initialize the syntax table. */ | |
1697 init_syntax_once (); | |
1698 #endif | |
1699 | |
1700 if (bufp->allocated == 0) | |
1701 { | |
1702 if (bufp->buffer) | |
1703 { /* If zero allocated, but buffer is non-null, try to realloc | |
1704 enough space. This loses if buffer's address is bogus, but | |
1705 that is the user's responsibility. */ | |
1706 RETALLOC (bufp->buffer, INIT_BUF_SIZE, unsigned char); | |
1707 } | |
1708 else | |
1709 { /* Caller did not allocate a buffer. Do it for them. */ | |
1710 bufp->buffer = TALLOC (INIT_BUF_SIZE, unsigned char); | |
1711 } | |
8102 | 1712 if (!bufp->buffer) FREE_STACK_RETURN (REG_ESPACE); |
1155 | 1713 |
1714 bufp->allocated = INIT_BUF_SIZE; | |
1715 } | |
1716 | |
1717 begalt = b = bufp->buffer; | |
1718 | |
1719 /* Loop through the uncompiled pattern until we're at the end. */ | |
1720 while (p != pend) | |
1721 { | |
1722 PATFETCH (c); | |
1723 | |
1724 switch (c) | |
1725 { | |
1726 case '^': | |
1727 { | |
1728 if ( /* If at start of pattern, it's an operator. */ | |
1729 p == pattern + 1 | |
1730 /* If context independent, it's an operator. */ | |
1731 || syntax & RE_CONTEXT_INDEP_ANCHORS | |
1732 /* Otherwise, depends on what's come before. */ | |
1733 || at_begline_loc_p (pattern, p, syntax)) | |
1734 BUF_PUSH (begline); | |
1735 else | |
1736 goto normal_char; | |
1737 } | |
1738 break; | |
1739 | |
1740 | |
1741 case '$': | |
1742 { | |
1743 if ( /* If at end of pattern, it's an operator. */ | |
1744 p == pend | |
1745 /* If context independent, it's an operator. */ | |
1746 || syntax & RE_CONTEXT_INDEP_ANCHORS | |
1747 /* Otherwise, depends on what's next. */ | |
1748 || at_endline_loc_p (p, pend, syntax)) | |
1749 BUF_PUSH (endline); | |
1750 else | |
1751 goto normal_char; | |
1752 } | |
1753 break; | |
1754 | |
1755 | |
1756 case '+': | |
1757 case '?': | |
1758 if ((syntax & RE_BK_PLUS_QM) | |
1759 || (syntax & RE_LIMITED_OPS)) | |
1760 goto normal_char; | |
1761 handle_plus: | |
1762 case '*': | |
1763 /* If there is no previous pattern... */ | |
1764 if (!laststart) | |
1765 { | |
1766 if (syntax & RE_CONTEXT_INVALID_OPS) | |
8102 | 1767 FREE_STACK_RETURN (REG_BADRPT); |
1155 | 1768 else if (!(syntax & RE_CONTEXT_INDEP_OPS)) |
1769 goto normal_char; | |
1770 } | |
1771 | |
1772 { | |
1773 /* Are we optimizing this jump? */ | |
1774 boolean keep_string_p = false; | |
1775 | |
1776 /* 1 means zero (many) matches is allowed. */ | |
1777 char zero_times_ok = 0, many_times_ok = 0; | |
1778 | |
1779 /* If there is a sequence of repetition chars, collapse it | |
1780 down to just one (the right one). We can't combine | |
1781 interval operators with these because of, e.g., `a{2}*', | |
1782 which should only match an even number of `a's. */ | |
1783 | |
1784 for (;;) | |
1785 { | |
1786 zero_times_ok |= c != '+'; | |
1787 many_times_ok |= c != '?'; | |
1788 | |
1789 if (p == pend) | |
1790 break; | |
1791 | |
1792 PATFETCH (c); | |
1793 | |
1794 if (c == '*' | |
1795 || (!(syntax & RE_BK_PLUS_QM) && (c == '+' || c == '?'))) | |
1796 ; | |
1797 | |
1798 else if (syntax & RE_BK_PLUS_QM && c == '\\') | |
1799 { | |
8102 | 1800 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE); |
1155 | 1801 |
1802 PATFETCH (c1); | |
1803 if (!(c1 == '+' || c1 == '?')) | |
1804 { | |
1805 PATUNFETCH; | |
1806 PATUNFETCH; | |
1807 break; | |
1808 } | |
1809 | |
1810 c = c1; | |
1811 } | |
1812 else | |
1813 { | |
1814 PATUNFETCH; | |
1815 break; | |
1816 } | |
1817 | |
1818 /* If we get here, we found another repeat character. */ | |
1819 } | |
1820 | |
1821 /* Star, etc. applied to an empty pattern is equivalent | |
1822 to an empty pattern. */ | |
1823 if (!laststart) | |
1824 break; | |
1825 | |
1826 /* Now we know whether or not zero matches is allowed | |
1827 and also whether or not two or more matches is allowed. */ | |
1828 if (many_times_ok) | |
1829 { /* More than one repetition is allowed, so put in at the | |
1830 end a backward relative jump from `b' to before the next | |
1831 jump we're going to put in below (which jumps from | |
1832 laststart to after this jump). | |
1833 | |
1834 But if we are at the `*' in the exact sequence `.*\n', | |
1835 insert an unconditional jump backwards to the ., | |
1836 instead of the beginning of the loop. This way we only | |
1837 push a failure point once, instead of every time | |
1838 through the loop. */ | |
1839 assert (p - 1 > pattern); | |
1840 | |
1841 /* Allocate the space for the jump. */ | |
1842 GET_BUFFER_SPACE (3); | |
1843 | |
1844 /* We know we are not at the first character of the pattern, | |
1845 because laststart was nonzero. And we've already | |
1846 incremented `p', by the way, to be the character after | |
1847 the `*'. Do we have to do something analogous here | |
1848 for null bytes, because of RE_DOT_NOT_NULL? */ | |
1849 if (TRANSLATE (*(p - 2)) == TRANSLATE ('.') | |
2453 | 1850 && zero_times_ok |
1155 | 1851 && p < pend && TRANSLATE (*p) == TRANSLATE ('\n') |
1852 && !(syntax & RE_DOT_NEWLINE)) | |
1853 { /* We have .*\n. */ | |
1854 STORE_JUMP (jump, b, laststart); | |
1855 keep_string_p = true; | |
1856 } | |
1857 else | |
1858 /* Anything else. */ | |
1859 STORE_JUMP (maybe_pop_jump, b, laststart - 3); | |
1860 | |
1861 /* We've added more stuff to the buffer. */ | |
1862 b += 3; | |
1863 } | |
1864 | |
1865 /* On failure, jump from laststart to b + 3, which will be the | |
1866 end of the buffer after this jump is inserted. */ | |
1867 GET_BUFFER_SPACE (3); | |
1868 INSERT_JUMP (keep_string_p ? on_failure_keep_string_jump | |
1869 : on_failure_jump, | |
1870 laststart, b + 3); | |
1871 pending_exact = 0; | |
1872 b += 3; | |
1873 | |
1874 if (!zero_times_ok) | |
1875 { | |
1876 /* At least one repetition is required, so insert a | |
1877 `dummy_failure_jump' before the initial | |
1878 `on_failure_jump' instruction of the loop. This | |
1879 effects a skip over that instruction the first time | |
1880 we hit that loop. */ | |
1881 GET_BUFFER_SPACE (3); | |
1882 INSERT_JUMP (dummy_failure_jump, laststart, laststart + 6); | |
1883 b += 3; | |
1884 } | |
1885 } | |
1886 break; | |
1887 | |
1888 | |
1889 case '.': | |
1890 laststart = b; | |
1891 BUF_PUSH (anychar); | |
1892 break; | |
1893 | |
1894 | |
1895 case '[': | |
1896 { | |
1897 boolean had_char_class = false; | |
1898 | |
8102 | 1899 if (p == pend) FREE_STACK_RETURN (REG_EBRACK); |
1155 | 1900 |
1901 /* Ensure that we have enough space to push a charset: the | |
1902 opcode, the length count, and the bitset; 34 bytes in all. */ | |
1903 GET_BUFFER_SPACE (34); | |
1904 | |
1905 laststart = b; | |
1906 | |
1907 /* We test `*p == '^' twice, instead of using an if | |
1908 statement, so we only need one BUF_PUSH. */ | |
1909 BUF_PUSH (*p == '^' ? charset_not : charset); | |
1910 if (*p == '^') | |
1911 p++; | |
1912 | |
1913 /* Remember the first position in the bracket expression. */ | |
1914 p1 = p; | |
1915 | |
1916 /* Push the number of bytes in the bitmap. */ | |
1917 BUF_PUSH ((1 << BYTEWIDTH) / BYTEWIDTH); | |
1918 | |
1919 /* Clear the whole map. */ | |
1920 bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH); | |
1921 | |
1922 /* charset_not matches newline according to a syntax bit. */ | |
1923 if ((re_opcode_t) b[-2] == charset_not | |
1924 && (syntax & RE_HAT_LISTS_NOT_NEWLINE)) | |
1925 SET_LIST_BIT ('\n'); | |
1926 | |
1927 /* Read in characters and ranges, setting map bits. */ | |
1928 for (;;) | |
1929 { | |
8102 | 1930 if (p == pend) FREE_STACK_RETURN (REG_EBRACK); |
1155 | 1931 |
1932 PATFETCH (c); | |
1933 | |
1934 /* \ might escape characters inside [...] and [^...]. */ | |
1935 if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) && c == '\\') | |
1936 { | |
8102 | 1937 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE); |
1155 | 1938 |
1939 PATFETCH (c1); | |
1940 SET_LIST_BIT (c1); | |
1941 continue; | |
1942 } | |
1943 | |
1944 /* Could be the end of the bracket expression. If it's | |
1945 not (i.e., when the bracket expression is `[]' so | |
1946 far), the ']' character bit gets set way below. */ | |
1947 if (c == ']' && p != p1 + 1) | |
1948 break; | |
1949 | |
1950 /* Look ahead to see if it's a range when the last thing | |
1951 was a character class. */ | |
1952 if (had_char_class && c == '-' && *p != ']') | |
8102 | 1953 FREE_STACK_RETURN (REG_ERANGE); |
1155 | 1954 |
1955 /* Look ahead to see if it's a range when the last thing | |
1956 was a character: if this is a hyphen not at the | |
1957 beginning or the end of a list, then it's the range | |
1958 operator. */ | |
1959 if (c == '-' | |
1960 && !(p - 2 >= pattern && p[-2] == '[') | |
1961 && !(p - 3 >= pattern && p[-3] == '[' && p[-2] == '^') | |
1962 && *p != ']') | |
1963 { | |
1964 reg_errcode_t ret | |
1965 = compile_range (&p, pend, translate, syntax, b); | |
8102 | 1966 if (ret != REG_NOERROR) FREE_STACK_RETURN (ret); |
1155 | 1967 } |
1968 | |
1969 else if (p[0] == '-' && p[1] != ']') | |
1970 { /* This handles ranges made up of characters only. */ | |
1971 reg_errcode_t ret; | |
1972 | |
1973 /* Move past the `-'. */ | |
1974 PATFETCH (c1); | |
1975 | |
1976 ret = compile_range (&p, pend, translate, syntax, b); | |
8102 | 1977 if (ret != REG_NOERROR) FREE_STACK_RETURN (ret); |
1155 | 1978 } |
1979 | |
1980 /* See if we're at the beginning of a possible character | |
1981 class. */ | |
1982 | |
1983 else if (syntax & RE_CHAR_CLASSES && c == '[' && *p == ':') | |
1984 { /* Leave room for the null. */ | |
1985 char str[CHAR_CLASS_MAX_LENGTH + 1]; | |
1986 | |
1987 PATFETCH (c); | |
1988 c1 = 0; | |
1989 | |
1990 /* If pattern is `[[:'. */ | |
8102 | 1991 if (p == pend) FREE_STACK_RETURN (REG_EBRACK); |
1155 | 1992 |
1993 for (;;) | |
1994 { | |
1995 PATFETCH (c); | |
1996 if (c == ':' || c == ']' || p == pend | |
1997 || c1 == CHAR_CLASS_MAX_LENGTH) | |
1998 break; | |
1999 str[c1++] = c; | |
2000 } | |
2001 str[c1] = '\0'; | |
2002 | |
2003 /* If isn't a word bracketed by `[:' and:`]': | |
2004 undo the ending character, the letters, and leave | |
2005 the leading `:' and `[' (but set bits for them). */ | |
2006 if (c == ':' && *p == ']') | |
2007 { | |
2008 int ch; | |
2009 boolean is_alnum = STREQ (str, "alnum"); | |
2010 boolean is_alpha = STREQ (str, "alpha"); | |
2011 boolean is_blank = STREQ (str, "blank"); | |
2012 boolean is_cntrl = STREQ (str, "cntrl"); | |
2013 boolean is_digit = STREQ (str, "digit"); | |
2014 boolean is_graph = STREQ (str, "graph"); | |
2015 boolean is_lower = STREQ (str, "lower"); | |
2016 boolean is_print = STREQ (str, "print"); | |
2017 boolean is_punct = STREQ (str, "punct"); | |
2018 boolean is_space = STREQ (str, "space"); | |
2019 boolean is_upper = STREQ (str, "upper"); | |
2020 boolean is_xdigit = STREQ (str, "xdigit"); | |
2021 | |
8102 | 2022 if (!IS_CHAR_CLASS (str)) |
2023 FREE_STACK_RETURN (REG_ECTYPE); | |
1155 | 2024 |
2025 /* Throw away the ] at the end of the character | |
2026 class. */ | |
2027 PATFETCH (c); | |
2028 | |
8102 | 2029 if (p == pend) FREE_STACK_RETURN (REG_EBRACK); |
1155 | 2030 |
2031 for (ch = 0; ch < 1 << BYTEWIDTH; ch++) | |
2032 { | |
8561
dc5353720725
(regex_compile): Split an if to avoid compiler bug.
Richard M. Stallman <rms@gnu.org>
parents:
8402
diff
changeset
|
2033 /* This was split into 3 if's to |
dc5353720725
(regex_compile): Split an if to avoid compiler bug.
Richard M. Stallman <rms@gnu.org>
parents:
8402
diff
changeset
|
2034 avoid an arbitrary limit in some compiler. */ |
1668 | 2035 if ( (is_alnum && ISALNUM (ch)) |
2036 || (is_alpha && ISALPHA (ch)) | |
2037 || (is_blank && ISBLANK (ch)) | |
8561
dc5353720725
(regex_compile): Split an if to avoid compiler bug.
Richard M. Stallman <rms@gnu.org>
parents:
8402
diff
changeset
|
2038 || (is_cntrl && ISCNTRL (ch))) |
dc5353720725
(regex_compile): Split an if to avoid compiler bug.
Richard M. Stallman <rms@gnu.org>
parents:
8402
diff
changeset
|
2039 SET_LIST_BIT (ch); |
dc5353720725
(regex_compile): Split an if to avoid compiler bug.
Richard M. Stallman <rms@gnu.org>
parents:
8402
diff
changeset
|
2040 if ( (is_digit && ISDIGIT (ch)) |
1668 | 2041 || (is_graph && ISGRAPH (ch)) |
2042 || (is_lower && ISLOWER (ch)) | |
8561
dc5353720725
(regex_compile): Split an if to avoid compiler bug.
Richard M. Stallman <rms@gnu.org>
parents:
8402
diff
changeset
|
2043 || (is_print && ISPRINT (ch))) |
dc5353720725
(regex_compile): Split an if to avoid compiler bug.
Richard M. Stallman <rms@gnu.org>
parents:
8402
diff
changeset
|
2044 SET_LIST_BIT (ch); |
dc5353720725
(regex_compile): Split an if to avoid compiler bug.
Richard M. Stallman <rms@gnu.org>
parents:
8402
diff
changeset
|
2045 if ( (is_punct && ISPUNCT (ch)) |
1668 | 2046 || (is_space && ISSPACE (ch)) |
2047 || (is_upper && ISUPPER (ch)) | |
2048 || (is_xdigit && ISXDIGIT (ch))) | |
8561
dc5353720725
(regex_compile): Split an if to avoid compiler bug.
Richard M. Stallman <rms@gnu.org>
parents:
8402
diff
changeset
|
2049 SET_LIST_BIT (ch); |
1155 | 2050 } |
2051 had_char_class = true; | |
2052 } | |
2053 else | |
2054 { | |
2055 c1++; | |
2056 while (c1--) | |
2057 PATUNFETCH; | |
2058 SET_LIST_BIT ('['); | |
2059 SET_LIST_BIT (':'); | |
2060 had_char_class = false; | |
2061 } | |
2062 } | |
2063 else | |
2064 { | |
2065 had_char_class = false; | |
2066 SET_LIST_BIT (c); | |
2067 } | |
2068 } | |
2069 | |
2070 /* Discard any (non)matching list bytes that are all 0 at the | |
2071 end of the map. Decrease the map-length byte too. */ | |
2072 while ((int) b[-1] > 0 && b[b[-1] - 1] == 0) | |
2073 b[-1]--; | |
2074 b += b[-1]; | |
2075 } | |
2076 break; | |
2077 | |
2078 | |
2079 case '(': | |
2080 if (syntax & RE_NO_BK_PARENS) | |
2081 goto handle_open; | |
2082 else | |
2083 goto normal_char; | |
2084 | |
2085 | |
2086 case ')': | |
2087 if (syntax & RE_NO_BK_PARENS) | |
2088 goto handle_close; | |
2089 else | |
2090 goto normal_char; | |
2091 | |
2092 | |
2093 case '\n': | |
2094 if (syntax & RE_NEWLINE_ALT) | |
2095 goto handle_alt; | |
2096 else | |
2097 goto normal_char; | |
2098 | |
2099 | |
2100 case '|': | |
2101 if (syntax & RE_NO_BK_VBAR) | |
2102 goto handle_alt; | |
2103 else | |
2104 goto normal_char; | |
2105 | |
2106 | |
2107 case '{': | |
2108 if (syntax & RE_INTERVALS && syntax & RE_NO_BK_BRACES) | |
2109 goto handle_interval; | |
2110 else | |
2111 goto normal_char; | |
2112 | |
2113 | |
2114 case '\\': | |
8102 | 2115 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE); |
1155 | 2116 |
2117 /* Do not translate the character after the \, so that we can | |
2118 distinguish, e.g., \B from \b, even if we normally would | |
2119 translate, e.g., B to b. */ | |
2120 PATFETCH_RAW (c); | |
2121 | |
2122 switch (c) | |
2123 { | |
2124 case '(': | |
2125 if (syntax & RE_NO_BK_PARENS) | |
2126 goto normal_backslash; | |
2127 | |
2128 handle_open: | |
2129 bufp->re_nsub++; | |
2130 regnum++; | |
2131 | |
2132 if (COMPILE_STACK_FULL) | |
2133 { | |
2134 RETALLOC (compile_stack.stack, compile_stack.size << 1, | |
2135 compile_stack_elt_t); | |
2136 if (compile_stack.stack == NULL) return REG_ESPACE; | |
2137 | |
2138 compile_stack.size <<= 1; | |
2139 } | |
2140 | |
2141 /* These are the values to restore when we hit end of this | |
2142 group. They are all relative offsets, so that if the | |
2143 whole pattern moves because of realloc, they will still | |
2144 be valid. */ | |
2145 COMPILE_STACK_TOP.begalt_offset = begalt - bufp->buffer; | |
2146 COMPILE_STACK_TOP.fixup_alt_jump | |
2147 = fixup_alt_jump ? fixup_alt_jump - bufp->buffer + 1 : 0; | |
2148 COMPILE_STACK_TOP.laststart_offset = b - bufp->buffer; | |
2149 COMPILE_STACK_TOP.regnum = regnum; | |
2150 | |
2151 /* We will eventually replace the 0 with the number of | |
2152 groups inner to this one. But do not push a | |
2153 start_memory for groups beyond the last one we can | |
2154 represent in the compiled pattern. */ | |
2155 if (regnum <= MAX_REGNUM) | |
2156 { | |
2157 COMPILE_STACK_TOP.inner_group_offset = b - bufp->buffer + 2; | |
2158 BUF_PUSH_3 (start_memory, regnum, 0); | |
2159 } | |
2160 | |
2161 compile_stack.avail++; | |
2162 | |
2163 fixup_alt_jump = 0; | |
2164 laststart = 0; | |
2165 begalt = b; | |
2453 | 2166 /* If we've reached MAX_REGNUM groups, then this open |
2167 won't actually generate any code, so we'll have to | |
2168 clear pending_exact explicitly. */ | |
2169 pending_exact = 0; | |
1155 | 2170 break; |
2171 | |
2172 | |
2173 case ')': | |
2174 if (syntax & RE_NO_BK_PARENS) goto normal_backslash; | |
2175 | |
2176 if (COMPILE_STACK_EMPTY) | |
2177 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD) | |
2178 goto normal_backslash; | |
2179 else | |
8102 | 2180 FREE_STACK_RETURN (REG_ERPAREN); |
1155 | 2181 |
2182 handle_close: | |
2183 if (fixup_alt_jump) | |
2184 { /* Push a dummy failure point at the end of the | |
2185 alternative for a possible future | |
2186 `pop_failure_jump' to pop. See comments at | |
2187 `push_dummy_failure' in `re_match_2'. */ | |
2188 BUF_PUSH (push_dummy_failure); | |
2189 | |
2190 /* We allocated space for this jump when we assigned | |
2191 to `fixup_alt_jump', in the `handle_alt' case below. */ | |
2192 STORE_JUMP (jump_past_alt, fixup_alt_jump, b - 1); | |
2193 } | |
2194 | |
2195 /* See similar code for backslashed left paren above. */ | |
2196 if (COMPILE_STACK_EMPTY) | |
2197 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD) | |
2198 goto normal_char; | |
2199 else | |
8102 | 2200 FREE_STACK_RETURN (REG_ERPAREN); |
1155 | 2201 |
2202 /* Since we just checked for an empty stack above, this | |
2203 ``can't happen''. */ | |
2204 assert (compile_stack.avail != 0); | |
2205 { | |
2206 /* We don't just want to restore into `regnum', because | |
2207 later groups should continue to be numbered higher, | |
2208 as in `(ab)c(de)' -- the second group is #2. */ | |
2209 regnum_t this_group_regnum; | |
2210 | |
2211 compile_stack.avail--; | |
2212 begalt = bufp->buffer + COMPILE_STACK_TOP.begalt_offset; | |
2213 fixup_alt_jump | |
2214 = COMPILE_STACK_TOP.fixup_alt_jump | |
2215 ? bufp->buffer + COMPILE_STACK_TOP.fixup_alt_jump - 1 | |
2216 : 0; | |
2217 laststart = bufp->buffer + COMPILE_STACK_TOP.laststart_offset; | |
2218 this_group_regnum = COMPILE_STACK_TOP.regnum; | |
2453 | 2219 /* If we've reached MAX_REGNUM groups, then this open |
2220 won't actually generate any code, so we'll have to | |
2221 clear pending_exact explicitly. */ | |
2222 pending_exact = 0; | |
1155 | 2223 |
2224 /* We're at the end of the group, so now we know how many | |
2225 groups were inside this one. */ | |
2226 if (this_group_regnum <= MAX_REGNUM) | |
2227 { | |
2228 unsigned char *inner_group_loc | |
2229 = bufp->buffer + COMPILE_STACK_TOP.inner_group_offset; | |
2230 | |
2231 *inner_group_loc = regnum - this_group_regnum; | |
2232 BUF_PUSH_3 (stop_memory, this_group_regnum, | |
2233 regnum - this_group_regnum); | |
2234 } | |
2235 } | |
2236 break; | |
2237 | |
2238 | |
2239 case '|': /* `\|'. */ | |
2240 if (syntax & RE_LIMITED_OPS || syntax & RE_NO_BK_VBAR) | |
2241 goto normal_backslash; | |
2242 handle_alt: | |
2243 if (syntax & RE_LIMITED_OPS) | |
2244 goto normal_char; | |
2245 | |
2246 /* Insert before the previous alternative a jump which | |
2247 jumps to this alternative if the former fails. */ | |
2248 GET_BUFFER_SPACE (3); | |
2249 INSERT_JUMP (on_failure_jump, begalt, b + 6); | |
2250 pending_exact = 0; | |
2251 b += 3; | |
2252 | |
2253 /* The alternative before this one has a jump after it | |
2254 which gets executed if it gets matched. Adjust that | |
2255 jump so it will jump to this alternative's analogous | |
2256 jump (put in below, which in turn will jump to the next | |
2257 (if any) alternative's such jump, etc.). The last such | |
2258 jump jumps to the correct final destination. A picture: | |
2259 _____ _____ | |
2260 | | | | | |
2261 | v | v | |
2262 a | b | c | |
2263 | |
1637 | 2264 If we are at `b', then fixup_alt_jump right now points to a |
2265 three-byte space after `a'. We'll put in the jump, set | |
2266 fixup_alt_jump to right after `b', and leave behind three | |
2267 bytes which we'll fill in when we get to after `c'. */ | |
1155 | 2268 |
2269 if (fixup_alt_jump) | |
2270 STORE_JUMP (jump_past_alt, fixup_alt_jump, b); | |
2271 | |
2272 /* Mark and leave space for a jump after this alternative, | |
2273 to be filled in later either by next alternative or | |
2274 when know we're at the end of a series of alternatives. */ | |
2275 fixup_alt_jump = b; | |
2276 GET_BUFFER_SPACE (3); | |
2277 b += 3; | |
2278 | |
2279 laststart = 0; | |
2280 begalt = b; | |
2281 break; | |
2282 | |
2283 | |
2284 case '{': | |
2285 /* If \{ is a literal. */ | |
2286 if (!(syntax & RE_INTERVALS) | |
2287 /* If we're at `\{' and it's not the open-interval | |
2288 operator. */ | |
2289 || ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES)) | |
2290 || (p - 2 == pattern && p == pend)) | |
2291 goto normal_backslash; | |
2292 | |
2293 handle_interval: | |
2294 { | |
2295 /* If got here, then the syntax allows intervals. */ | |
2296 | |
2297 /* At least (most) this many matches must be made. */ | |
2298 int lower_bound = -1, upper_bound = -1; | |
2299 | |
2300 beg_interval = p - 1; | |
2301 | |
2302 if (p == pend) | |
2303 { | |
2304 if (syntax & RE_NO_BK_BRACES) | |
2305 goto unfetch_interval; | |
2306 else | |
8102 | 2307 FREE_STACK_RETURN (REG_EBRACE); |
1155 | 2308 } |
2309 | |
2310 GET_UNSIGNED_NUMBER (lower_bound); | |
2311 | |
2312 if (c == ',') | |
2313 { | |
2314 GET_UNSIGNED_NUMBER (upper_bound); | |
2315 if (upper_bound < 0) upper_bound = RE_DUP_MAX; | |
2316 } | |
2317 else | |
2318 /* Interval such as `{1}' => match exactly once. */ | |
2319 upper_bound = lower_bound; | |
2320 | |
2321 if (lower_bound < 0 || upper_bound > RE_DUP_MAX | |
2322 || lower_bound > upper_bound) | |
2323 { | |
2324 if (syntax & RE_NO_BK_BRACES) | |
2325 goto unfetch_interval; | |
2326 else | |
8102 | 2327 FREE_STACK_RETURN (REG_BADBR); |
1155 | 2328 } |
2329 | |
2330 if (!(syntax & RE_NO_BK_BRACES)) | |
2331 { | |
8102 | 2332 if (c != '\\') FREE_STACK_RETURN (REG_EBRACE); |
1155 | 2333 |
2334 PATFETCH (c); | |
2335 } | |
2336 | |
2337 if (c != '}') | |
2338 { | |
2339 if (syntax & RE_NO_BK_BRACES) | |
2340 goto unfetch_interval; | |
2341 else | |
8102 | 2342 FREE_STACK_RETURN (REG_BADBR); |
1155 | 2343 } |
2344 | |
2345 /* We just parsed a valid interval. */ | |
2346 | |
2347 /* If it's invalid to have no preceding re. */ | |
2348 if (!laststart) | |
2349 { | |
2350 if (syntax & RE_CONTEXT_INVALID_OPS) | |
8102 | 2351 FREE_STACK_RETURN (REG_BADRPT); |
1155 | 2352 else if (syntax & RE_CONTEXT_INDEP_OPS) |
2353 laststart = b; | |
2354 else | |
2355 goto unfetch_interval; | |
2356 } | |
2357 | |
2358 /* If the upper bound is zero, don't want to succeed at | |
2359 all; jump from `laststart' to `b + 3', which will be | |
2360 the end of the buffer after we insert the jump. */ | |
2361 if (upper_bound == 0) | |
2362 { | |
2363 GET_BUFFER_SPACE (3); | |
2364 INSERT_JUMP (jump, laststart, b + 3); | |
2365 b += 3; | |
2366 } | |
2367 | |
2368 /* Otherwise, we have a nontrivial interval. When | |
2369 we're all done, the pattern will look like: | |
2370 set_number_at <jump count> <upper bound> | |
2371 set_number_at <succeed_n count> <lower bound> | |
5841 | 2372 succeed_n <after jump addr> <succeed_n count> |
1155 | 2373 <body of loop> |
2374 jump_n <succeed_n addr> <jump count> | |
2375 (The upper bound and `jump_n' are omitted if | |
2376 `upper_bound' is 1, though.) */ | |
2377 else | |
2378 { /* If the upper bound is > 1, we need to insert | |
2379 more at the end of the loop. */ | |
2380 unsigned nbytes = 10 + (upper_bound > 1) * 10; | |
2381 | |
2382 GET_BUFFER_SPACE (nbytes); | |
2383 | |
2384 /* Initialize lower bound of the `succeed_n', even | |
2385 though it will be set during matching by its | |
2386 attendant `set_number_at' (inserted next), | |
2387 because `re_compile_fastmap' needs to know. | |
2388 Jump to the `jump_n' we might insert below. */ | |
2389 INSERT_JUMP2 (succeed_n, laststart, | |
2390 b + 5 + (upper_bound > 1) * 5, | |
2391 lower_bound); | |
2392 b += 5; | |
2393 | |
2394 /* Code to initialize the lower bound. Insert | |
2395 before the `succeed_n'. The `5' is the last two | |
2396 bytes of this `set_number_at', plus 3 bytes of | |
2397 the following `succeed_n'. */ | |
2398 insert_op2 (set_number_at, laststart, 5, lower_bound, b); | |
2399 b += 5; | |
2400 | |
2401 if (upper_bound > 1) | |
2402 { /* More than one repetition is allowed, so | |
2403 append a backward jump to the `succeed_n' | |
2404 that starts this interval. | |
2405 | |
2406 When we've reached this during matching, | |
2407 we'll have matched the interval once, so | |
2408 jump back only `upper_bound - 1' times. */ | |
2409 STORE_JUMP2 (jump_n, b, laststart + 5, | |
2410 upper_bound - 1); | |
2411 b += 5; | |
2412 | |
2413 /* The location we want to set is the second | |
2414 parameter of the `jump_n'; that is `b-2' as | |
2415 an absolute address. `laststart' will be | |
2416 the `set_number_at' we're about to insert; | |
2417 `laststart+3' the number to set, the source | |
2418 for the relative address. But we are | |
2419 inserting into the middle of the pattern -- | |
2420 so everything is getting moved up by 5. | |
2421 Conclusion: (b - 2) - (laststart + 3) + 5, | |
2422 i.e., b - laststart. | |
2423 | |
2424 We insert this at the beginning of the loop | |
2425 so that if we fail during matching, we'll | |
2426 reinitialize the bounds. */ | |
2427 insert_op2 (set_number_at, laststart, b - laststart, | |
2428 upper_bound - 1, b); | |
2429 b += 5; | |
2430 } | |
2431 } | |
2432 pending_exact = 0; | |
2433 beg_interval = NULL; | |
2434 } | |
2435 break; | |
2436 | |
2437 unfetch_interval: | |
2438 /* If an invalid interval, match the characters as literals. */ | |
2439 assert (beg_interval); | |
2440 p = beg_interval; | |
2441 beg_interval = NULL; | |
2442 | |
2443 /* normal_char and normal_backslash need `c'. */ | |
2444 PATFETCH (c); | |
2445 | |
2446 if (!(syntax & RE_NO_BK_BRACES)) | |
2447 { | |
2448 if (p > pattern && p[-1] == '\\') | |
2449 goto normal_backslash; | |
2450 } | |
2451 goto normal_char; | |
2452 | |
2453 #ifdef emacs | |
2454 /* There is no way to specify the before_dot and after_dot | |
2455 operators. rms says this is ok. --karl */ | |
2456 case '=': | |
2457 BUF_PUSH (at_dot); | |
2458 break; | |
2459 | |
2460 case 's': | |
2461 laststart = b; | |
2462 PATFETCH (c); | |
2463 BUF_PUSH_2 (syntaxspec, syntax_spec_code[c]); | |
2464 break; | |
2465 | |
2466 case 'S': | |
2467 laststart = b; | |
2468 PATFETCH (c); | |
2469 BUF_PUSH_2 (notsyntaxspec, syntax_spec_code[c]); | |
2470 break; | |
2471 #endif /* emacs */ | |
2472 | |
2473 | |
2474 case 'w': | |
2475 laststart = b; | |
2476 BUF_PUSH (wordchar); | |
2477 break; | |
2478 | |
2479 | |
2480 case 'W': | |
2481 laststart = b; | |
2482 BUF_PUSH (notwordchar); | |
2483 break; | |
2484 | |
2485 | |
2486 case '<': | |
2487 BUF_PUSH (wordbeg); | |
2488 break; | |
2489 | |
2490 case '>': | |
2491 BUF_PUSH (wordend); | |
2492 break; | |
2493 | |
2494 case 'b': | |
2495 BUF_PUSH (wordbound); | |
2496 break; | |
2497 | |
2498 case 'B': | |
2499 BUF_PUSH (notwordbound); | |
2500 break; | |
2501 | |
2502 case '`': | |
2503 BUF_PUSH (begbuf); | |
2504 break; | |
2505 | |
2506 case '\'': | |
2507 BUF_PUSH (endbuf); | |
2508 break; | |
2509 | |
2510 case '1': case '2': case '3': case '4': case '5': | |
2511 case '6': case '7': case '8': case '9': | |
2512 if (syntax & RE_NO_BK_REFS) | |
2513 goto normal_char; | |
2514 | |
2515 c1 = c - '0'; | |
2516 | |
2517 if (c1 > regnum) | |
8102 | 2518 FREE_STACK_RETURN (REG_ESUBREG); |
1155 | 2519 |
2520 /* Can't back reference to a subexpression if inside of it. */ | |
2521 if (group_in_compile_stack (compile_stack, c1)) | |
2522 goto normal_char; | |
2523 | |
2524 laststart = b; | |
2525 BUF_PUSH_2 (duplicate, c1); | |
2526 break; | |
2527 | |
2528 | |
2529 case '+': | |
2530 case '?': | |
2531 if (syntax & RE_BK_PLUS_QM) | |
2532 goto handle_plus; | |
2533 else | |
2534 goto normal_backslash; | |
2535 | |
2536 default: | |
2537 normal_backslash: | |
2538 /* You might think it would be useful for \ to mean | |
2539 not to translate; but if we don't translate it | |
2540 it will never match anything. */ | |
2541 c = TRANSLATE (c); | |
2542 goto normal_char; | |
2543 } | |
2544 break; | |
2545 | |
2546 | |
2547 default: | |
2548 /* Expects the character in `c'. */ | |
2549 normal_char: | |
2550 /* If no exactn currently being built. */ | |
2551 if (!pending_exact | |
2552 | |
2553 /* If last exactn not at current position. */ | |
2554 || pending_exact + *pending_exact + 1 != b | |
2555 | |
2556 /* We have only one byte following the exactn for the count. */ | |
2557 || *pending_exact == (1 << BYTEWIDTH) - 1 | |
2558 | |
2559 /* If followed by a repetition operator. */ | |
2560 || *p == '*' || *p == '^' | |
2561 || ((syntax & RE_BK_PLUS_QM) | |
2562 ? *p == '\\' && (p[1] == '+' || p[1] == '?') | |
2563 : (*p == '+' || *p == '?')) | |
2564 || ((syntax & RE_INTERVALS) | |
2565 && ((syntax & RE_NO_BK_BRACES) | |
2566 ? *p == '{' | |
2567 : (p[0] == '\\' && p[1] == '{')))) | |
2568 { | |
2569 /* Start building a new exactn. */ | |
2570 | |
2571 laststart = b; | |
2572 | |
2573 BUF_PUSH_2 (exactn, 0); | |
2574 pending_exact = b - 1; | |
2575 } | |
2576 | |
2577 BUF_PUSH (c); | |
2578 (*pending_exact)++; | |
2579 break; | |
2580 } /* switch (c) */ | |
2581 } /* while p != pend */ | |
2582 | |
2583 | |
2584 /* Through the pattern now. */ | |
2585 | |
2586 if (fixup_alt_jump) | |
2587 STORE_JUMP (jump_past_alt, fixup_alt_jump, b); | |
2588 | |
2589 if (!COMPILE_STACK_EMPTY) | |
8102 | 2590 FREE_STACK_RETURN (REG_EPAREN); |
1155 | 2591 |
9983
ef6e1637c777
(re_opcode_t): New opcode `succeed'
Richard M. Stallman <rms@gnu.org>
parents:
9717
diff
changeset
|
2592 /* If we don't want backtracking, force success |
ef6e1637c777
(re_opcode_t): New opcode `succeed'
Richard M. Stallman <rms@gnu.org>
parents:
9717
diff
changeset
|
2593 the first time we reach the end of the compiled pattern. */ |
ef6e1637c777
(re_opcode_t): New opcode `succeed'
Richard M. Stallman <rms@gnu.org>
parents:
9717
diff
changeset
|
2594 if (syntax & RE_NO_POSIX_BACKTRACKING) |
ef6e1637c777
(re_opcode_t): New opcode `succeed'
Richard M. Stallman <rms@gnu.org>
parents:
9717
diff
changeset
|
2595 BUF_PUSH (succeed); |
ef6e1637c777
(re_opcode_t): New opcode `succeed'
Richard M. Stallman <rms@gnu.org>
parents:
9717
diff
changeset
|
2596 |
1155 | 2597 free (compile_stack.stack); |
2598 | |
2599 /* We have succeeded; set the length of the buffer. */ | |
2600 bufp->used = b - bufp->buffer; | |
2601 | |
2602 #ifdef DEBUG | |
2603 if (debug) | |
2604 { | |
2615 | 2605 DEBUG_PRINT1 ("\nCompiled pattern: \n"); |
1155 | 2606 print_compiled_pattern (bufp); |
2607 } | |
2608 #endif /* DEBUG */ | |
2609 | |
2952 | 2610 #ifndef MATCH_MAY_ALLOCATE |
2949 | 2611 /* Initialize the failure stack to the largest possible stack. This |
2612 isn't necessary unless we're trying to avoid calling alloca in | |
2613 the search and match routines. */ | |
2614 { | |
2615 int num_regs = bufp->re_nsub + 1; | |
2616 | |
2617 /* Since DOUBLE_FAIL_STACK refuses to double only if the current size | |
2618 is strictly greater than re_max_failures, the largest possible stack | |
2619 is 2 * re_max_failures failure points. */ | |
7012 | 2620 if (fail_stack.size < (2 * re_max_failures * MAX_FAILURE_ITEMS)) |
7011 | 2621 { |
7012 | 2622 fail_stack.size = (2 * re_max_failures * MAX_FAILURE_ITEMS); |
7039
403172c099fc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
7012
diff
changeset
|
2623 |
403172c099fc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
7012
diff
changeset
|
2624 #ifdef emacs |
7011 | 2625 if (! fail_stack.stack) |
7039
403172c099fc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
7012
diff
changeset
|
2626 fail_stack.stack |
403172c099fc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
7012
diff
changeset
|
2627 = (fail_stack_elt_t *) xmalloc (fail_stack.size |
403172c099fc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
7012
diff
changeset
|
2628 * sizeof (fail_stack_elt_t)); |
7011 | 2629 else |
7039
403172c099fc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
7012
diff
changeset
|
2630 fail_stack.stack |
403172c099fc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
7012
diff
changeset
|
2631 = (fail_stack_elt_t *) xrealloc (fail_stack.stack, |
403172c099fc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
7012
diff
changeset
|
2632 (fail_stack.size |
403172c099fc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
7012
diff
changeset
|
2633 * sizeof (fail_stack_elt_t))); |
403172c099fc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
7012
diff
changeset
|
2634 #else /* not emacs */ |
403172c099fc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
7012
diff
changeset
|
2635 if (! fail_stack.stack) |
403172c099fc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
7012
diff
changeset
|
2636 fail_stack.stack |
403172c099fc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
7012
diff
changeset
|
2637 = (fail_stack_elt_t *) malloc (fail_stack.size |
403172c099fc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
7012
diff
changeset
|
2638 * sizeof (fail_stack_elt_t)); |
403172c099fc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
7012
diff
changeset
|
2639 else |
403172c099fc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
7012
diff
changeset
|
2640 fail_stack.stack |
403172c099fc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
7012
diff
changeset
|
2641 = (fail_stack_elt_t *) realloc (fail_stack.stack, |
403172c099fc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
7012
diff
changeset
|
2642 (fail_stack.size |
403172c099fc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
7012
diff
changeset
|
2643 * sizeof (fail_stack_elt_t))); |
403172c099fc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
7012
diff
changeset
|
2644 #endif /* not emacs */ |
7011 | 2645 } |
2949 | 2646 |
11622
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
2647 regex_grow_registers (num_regs); |
2949 | 2648 } |
11622
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
2649 #endif /* not MATCH_MAY_ALLOCATE */ |
2949 | 2650 |
1155 | 2651 return REG_NOERROR; |
2652 } /* regex_compile */ | |
2653 | |
2654 /* Subroutines for `regex_compile'. */ | |
2655 | |
2656 /* Store OP at LOC followed by two-byte integer parameter ARG. */ | |
2657 | |
2658 static void | |
2659 store_op1 (op, loc, arg) | |
2660 re_opcode_t op; | |
2661 unsigned char *loc; | |
2662 int arg; | |
2663 { | |
2664 *loc = (unsigned char) op; | |
2665 STORE_NUMBER (loc + 1, arg); | |
2666 } | |
2667 | |
2668 | |
2669 /* Like `store_op1', but for two two-byte parameters ARG1 and ARG2. */ | |
2670 | |
2671 static void | |
2672 store_op2 (op, loc, arg1, arg2) | |
2673 re_opcode_t op; | |
2674 unsigned char *loc; | |
2675 int arg1, arg2; | |
2676 { | |
2677 *loc = (unsigned char) op; | |
2678 STORE_NUMBER (loc + 1, arg1); | |
2679 STORE_NUMBER (loc + 3, arg2); | |
2680 } | |
2681 | |
2682 | |
2683 /* Copy the bytes from LOC to END to open up three bytes of space at LOC | |
2684 for OP followed by two-byte integer parameter ARG. */ | |
2685 | |
2686 static void | |
2687 insert_op1 (op, loc, arg, end) | |
2688 re_opcode_t op; | |
2689 unsigned char *loc; | |
2690 int arg; | |
2691 unsigned char *end; | |
2692 { | |
2693 register unsigned char *pfrom = end; | |
2694 register unsigned char *pto = end + 3; | |
2695 | |
2696 while (pfrom != loc) | |
2697 *--pto = *--pfrom; | |
2698 | |
2699 store_op1 (op, loc, arg); | |
2700 } | |
2701 | |
2702 | |
2703 /* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2. */ | |
2704 | |
2705 static void | |
2706 insert_op2 (op, loc, arg1, arg2, end) | |
2707 re_opcode_t op; | |
2708 unsigned char *loc; | |
2709 int arg1, arg2; | |
2710 unsigned char *end; | |
2711 { | |
2712 register unsigned char *pfrom = end; | |
2713 register unsigned char *pto = end + 5; | |
2714 | |
2715 while (pfrom != loc) | |
2716 *--pto = *--pfrom; | |
2717 | |
2718 store_op2 (op, loc, arg1, arg2); | |
2719 } | |
2720 | |
2721 | |
2722 /* P points to just after a ^ in PATTERN. Return true if that ^ comes | |
2723 after an alternative or a begin-subexpression. We assume there is at | |
2724 least one character before the ^. */ | |
2725 | |
2726 static boolean | |
2727 at_begline_loc_p (pattern, p, syntax) | |
2728 const char *pattern, *p; | |
2729 reg_syntax_t syntax; | |
2730 { | |
2731 const char *prev = p - 2; | |
2732 boolean prev_prev_backslash = prev > pattern && prev[-1] == '\\'; | |
2733 | |
2734 return | |
2735 /* After a subexpression? */ | |
2736 (*prev == '(' && (syntax & RE_NO_BK_PARENS || prev_prev_backslash)) | |
2737 /* After an alternative? */ | |
2738 || (*prev == '|' && (syntax & RE_NO_BK_VBAR || prev_prev_backslash)); | |
2739 } | |
2740 | |
2741 | |
2742 /* The dual of at_begline_loc_p. This one is for $. We assume there is | |
2743 at least one character after the $, i.e., `P < PEND'. */ | |
2744 | |
2745 static boolean | |
2746 at_endline_loc_p (p, pend, syntax) | |
2747 const char *p, *pend; | |
2748 int syntax; | |
2749 { | |
2750 const char *next = p; | |
2751 boolean next_backslash = *next == '\\'; | |
2752 const char *next_next = p + 1 < pend ? p + 1 : NULL; | |
2753 | |
2754 return | |
2755 /* Before a subexpression? */ | |
2756 (syntax & RE_NO_BK_PARENS ? *next == ')' | |
2757 : next_backslash && next_next && *next_next == ')') | |
2758 /* Before an alternative? */ | |
2759 || (syntax & RE_NO_BK_VBAR ? *next == '|' | |
2760 : next_backslash && next_next && *next_next == '|'); | |
2761 } | |
2762 | |
2763 | |
2764 /* Returns true if REGNUM is in one of COMPILE_STACK's elements and | |
2765 false if it's not. */ | |
2766 | |
2767 static boolean | |
2768 group_in_compile_stack (compile_stack, regnum) | |
2769 compile_stack_type compile_stack; | |
2770 regnum_t regnum; | |
2771 { | |
2772 int this_element; | |
2773 | |
2774 for (this_element = compile_stack.avail - 1; | |
2775 this_element >= 0; | |
2776 this_element--) | |
2777 if (compile_stack.stack[this_element].regnum == regnum) | |
2778 return true; | |
2779 | |
2780 return false; | |
2781 } | |
2782 | |
2783 | |
2784 /* Read the ending character of a range (in a bracket expression) from the | |
2785 uncompiled pattern *P_PTR (which ends at PEND). We assume the | |
2786 starting character is in `P[-2]'. (`P[-1]' is the character `-'.) | |
2787 Then we set the translation of all bits between the starting and | |
2788 ending characters (inclusive) in the compiled pattern B. | |
2789 | |
2790 Return an error code. | |
2791 | |
2792 We use these short variable names so we can use the same macros as | |
2793 `regex_compile' itself. */ | |
2794 | |
2795 static reg_errcode_t | |
2796 compile_range (p_ptr, pend, translate, syntax, b) | |
2797 const char **p_ptr, *pend; | |
2798 char *translate; | |
2799 reg_syntax_t syntax; | |
2800 unsigned char *b; | |
2801 { | |
2802 unsigned this_char; | |
2803 | |
2804 const char *p = *p_ptr; | |
1689 | 2805 int range_start, range_end; |
1155 | 2806 |
2807 if (p == pend) | |
2808 return REG_ERANGE; | |
2809 | |
1689 | 2810 /* Even though the pattern is a signed `char *', we need to fetch |
2811 with unsigned char *'s; if the high bit of the pattern character | |
2812 is set, the range endpoints will be negative if we fetch using a | |
2813 signed char *. | |
2814 | |
2815 We also want to fetch the endpoints without translating them; the | |
2816 appropriate translation is done in the bit-setting loop below. */ | |
9209 | 2817 /* The SVR4 compiler on the 3B2 had trouble with unsigned const char *. */ |
9208
0c6e2b5d3850
(compile_range): When casting to const unsigned char *, put const first.
Richard M. Stallman <rms@gnu.org>
parents:
9097
diff
changeset
|
2818 range_start = ((const unsigned char *) p)[-2]; |
0c6e2b5d3850
(compile_range): When casting to const unsigned char *, put const first.
Richard M. Stallman <rms@gnu.org>
parents:
9097
diff
changeset
|
2819 range_end = ((const unsigned char *) p)[0]; |
1155 | 2820 |
2821 /* Have to increment the pointer into the pattern string, so the | |
2822 caller isn't still at the ending character. */ | |
2823 (*p_ptr)++; | |
2824 | |
2825 /* If the start is after the end, the range is empty. */ | |
2826 if (range_start > range_end) | |
2827 return syntax & RE_NO_EMPTY_RANGES ? REG_ERANGE : REG_NOERROR; | |
2828 | |
2829 /* Here we see why `this_char' has to be larger than an `unsigned | |
2830 char' -- the range is inclusive, so if `range_end' == 0xff | |
2831 (assuming 8-bit characters), we would otherwise go into an infinite | |
2832 loop, since all characters <= 0xff. */ | |
2833 for (this_char = range_start; this_char <= range_end; this_char++) | |
2834 { | |
2835 SET_LIST_BIT (TRANSLATE (this_char)); | |
2836 } | |
2837 | |
2838 return REG_NOERROR; | |
2839 } | |
2840 | |
2841 /* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in | |
2842 BUFP. A fastmap records which of the (1 << BYTEWIDTH) possible | |
2843 characters can start a string that matches the pattern. This fastmap | |
2844 is used by re_search to skip quickly over impossible starting points. | |
2845 | |
2846 The caller must supply the address of a (1 << BYTEWIDTH)-byte data | |
2847 area as BUFP->fastmap. | |
2848 | |
2849 We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in | |
2850 the pattern buffer. | |
2851 | |
2852 Returns 0 if we succeed, -2 if an internal error. */ | |
2853 | |
2854 int | |
2855 re_compile_fastmap (bufp) | |
2856 struct re_pattern_buffer *bufp; | |
2857 { | |
2858 int j, k; | |
2952 | 2859 #ifdef MATCH_MAY_ALLOCATE |
1155 | 2860 fail_stack_type fail_stack; |
2949 | 2861 #endif |
1155 | 2862 #ifndef REGEX_MALLOC |
2863 char *destination; | |
2864 #endif | |
2865 /* We don't push any register information onto the failure stack. */ | |
2866 unsigned num_regs = 0; | |
2867 | |
2868 register char *fastmap = bufp->fastmap; | |
2869 unsigned char *pattern = bufp->buffer; | |
2870 unsigned long size = bufp->used; | |
4918
e928d39564ad
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
4846
diff
changeset
|
2871 unsigned char *p = pattern; |
1155 | 2872 register unsigned char *pend = pattern + size; |
2873 | |
11622
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
2874 /* This holds the pointer to the failure stack, when |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
2875 it is allocated relocatably. */ |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
2876 fail_stack_elt_t *failure_stack_ptr; |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
2877 |
1155 | 2878 /* Assume that each path through the pattern can be null until |
2879 proven otherwise. We set this false at the bottom of switch | |
2880 statement, to which we get only if a particular path doesn't | |
2881 match the empty string. */ | |
2882 boolean path_can_be_null = true; | |
2883 | |
2884 /* We aren't doing a `succeed_n' to begin with. */ | |
2885 boolean succeed_n_p = false; | |
2886 | |
2887 assert (fastmap != NULL && p != NULL); | |
2888 | |
2889 INIT_FAIL_STACK (); | |
2890 bzero (fastmap, 1 << BYTEWIDTH); /* Assume nothing's valid. */ | |
2891 bufp->fastmap_accurate = 1; /* It will be when we're done. */ | |
2892 bufp->can_be_null = 0; | |
2893 | |
10021
9203331b118a
(re_compile_fastmap): Really treat `succeed' like end.
Richard M. Stallman <rms@gnu.org>
parents:
10000
diff
changeset
|
2894 while (1) |
1155 | 2895 { |
10000
659346eafd79
(re_compile_fastmap): Treat `succeed' like end of pattern.
Karl Heuer <kwzh@gnu.org>
parents:
9983
diff
changeset
|
2896 if (p == pend || *p == succeed) |
10021
9203331b118a
(re_compile_fastmap): Really treat `succeed' like end.
Richard M. Stallman <rms@gnu.org>
parents:
10000
diff
changeset
|
2897 { |
9203331b118a
(re_compile_fastmap): Really treat `succeed' like end.
Richard M. Stallman <rms@gnu.org>
parents:
10000
diff
changeset
|
2898 /* We have reached the (effective) end of pattern. */ |
9203331b118a
(re_compile_fastmap): Really treat `succeed' like end.
Richard M. Stallman <rms@gnu.org>
parents:
10000
diff
changeset
|
2899 if (!FAIL_STACK_EMPTY ()) |
9203331b118a
(re_compile_fastmap): Really treat `succeed' like end.
Richard M. Stallman <rms@gnu.org>
parents:
10000
diff
changeset
|
2900 { |
9203331b118a
(re_compile_fastmap): Really treat `succeed' like end.
Richard M. Stallman <rms@gnu.org>
parents:
10000
diff
changeset
|
2901 bufp->can_be_null |= path_can_be_null; |
9203331b118a
(re_compile_fastmap): Really treat `succeed' like end.
Richard M. Stallman <rms@gnu.org>
parents:
10000
diff
changeset
|
2902 |
9203331b118a
(re_compile_fastmap): Really treat `succeed' like end.
Richard M. Stallman <rms@gnu.org>
parents:
10000
diff
changeset
|
2903 /* Reset for next path. */ |
9203331b118a
(re_compile_fastmap): Really treat `succeed' like end.
Richard M. Stallman <rms@gnu.org>
parents:
10000
diff
changeset
|
2904 path_can_be_null = true; |
9203331b118a
(re_compile_fastmap): Really treat `succeed' like end.
Richard M. Stallman <rms@gnu.org>
parents:
10000
diff
changeset
|
2905 |
11656
a40849041718
(union fail_stack_elt): New union.
Richard M. Stallman <rms@gnu.org>
parents:
11622
diff
changeset
|
2906 p = fail_stack.stack[--fail_stack.avail].pointer; |
10023
a53798af4794
(re_compile_fastmap): Really, really treat `succeed' like end.
Richard M. Stallman <rms@gnu.org>
parents:
10021
diff
changeset
|
2907 |
a53798af4794
(re_compile_fastmap): Really, really treat `succeed' like end.
Richard M. Stallman <rms@gnu.org>
parents:
10021
diff
changeset
|
2908 continue; |
10021
9203331b118a
(re_compile_fastmap): Really treat `succeed' like end.
Richard M. Stallman <rms@gnu.org>
parents:
10000
diff
changeset
|
2909 } |
9203331b118a
(re_compile_fastmap): Really treat `succeed' like end.
Richard M. Stallman <rms@gnu.org>
parents:
10000
diff
changeset
|
2910 else |
9203331b118a
(re_compile_fastmap): Really treat `succeed' like end.
Richard M. Stallman <rms@gnu.org>
parents:
10000
diff
changeset
|
2911 break; |
1155 | 2912 } |
2913 | |
2914 /* We should never be about to go beyond the end of the pattern. */ | |
2915 assert (p < pend); | |
2916 | |
10456
9c6110615166
[!emacs] (SWITCH_ENUM_CAST): New macro, from emacs/lisp.h
Karl Heuer <kwzh@gnu.org>
parents:
10297
diff
changeset
|
2917 switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++)) |
1155 | 2918 { |
2919 | |
2920 /* I guess the idea here is to simply not bother with a fastmap | |
2921 if a backreference is used, since it's too hard to figure out | |
2922 the fastmap for the corresponding group. Setting | |
2923 `can_be_null' stops `re_search_2' from using the fastmap, so | |
2924 that is all we do. */ | |
2925 case duplicate: | |
2926 bufp->can_be_null = 1; | |
11622
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
2927 goto done; |
1155 | 2928 |
2929 | |
2930 /* Following are the cases which match a character. These end | |
2931 with `break'. */ | |
2932 | |
2933 case exactn: | |
2934 fastmap[p[1]] = 1; | |
2935 break; | |
2936 | |
2937 | |
2938 case charset: | |
2939 for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--) | |
2940 if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))) | |
2941 fastmap[j] = 1; | |
2942 break; | |
2943 | |
2944 | |
2945 case charset_not: | |
2946 /* Chars beyond end of map must be allowed. */ | |
2947 for (j = *p * BYTEWIDTH; j < (1 << BYTEWIDTH); j++) | |
2948 fastmap[j] = 1; | |
2949 | |
2950 for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--) | |
2951 if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))) | |
2952 fastmap[j] = 1; | |
2953 break; | |
2954 | |
2955 | |
2956 case wordchar: | |
2957 for (j = 0; j < (1 << BYTEWIDTH); j++) | |
2958 if (SYNTAX (j) == Sword) | |
2959 fastmap[j] = 1; | |
2960 break; | |
2961 | |
2962 | |
2963 case notwordchar: | |
2964 for (j = 0; j < (1 << BYTEWIDTH); j++) | |
2965 if (SYNTAX (j) != Sword) | |
2966 fastmap[j] = 1; | |
2967 break; | |
2968 | |
2969 | |
2970 case anychar: | |
8114
bebf92e1b110
(re_compile_fastmap): Either set fastmap['\n'] to 1 or don't change it.
Richard M. Stallman <rms@gnu.org>
parents:
8102
diff
changeset
|
2971 { |
bebf92e1b110
(re_compile_fastmap): Either set fastmap['\n'] to 1 or don't change it.
Richard M. Stallman <rms@gnu.org>
parents:
8102
diff
changeset
|
2972 int fastmap_newline = fastmap['\n']; |
bebf92e1b110
(re_compile_fastmap): Either set fastmap['\n'] to 1 or don't change it.
Richard M. Stallman <rms@gnu.org>
parents:
8102
diff
changeset
|
2973 |
bebf92e1b110
(re_compile_fastmap): Either set fastmap['\n'] to 1 or don't change it.
Richard M. Stallman <rms@gnu.org>
parents:
8102
diff
changeset
|
2974 /* `.' matches anything ... */ |
bebf92e1b110
(re_compile_fastmap): Either set fastmap['\n'] to 1 or don't change it.
Richard M. Stallman <rms@gnu.org>
parents:
8102
diff
changeset
|
2975 for (j = 0; j < (1 << BYTEWIDTH); j++) |
bebf92e1b110
(re_compile_fastmap): Either set fastmap['\n'] to 1 or don't change it.
Richard M. Stallman <rms@gnu.org>
parents:
8102
diff
changeset
|
2976 fastmap[j] = 1; |
bebf92e1b110
(re_compile_fastmap): Either set fastmap['\n'] to 1 or don't change it.
Richard M. Stallman <rms@gnu.org>
parents:
8102
diff
changeset
|
2977 |
bebf92e1b110
(re_compile_fastmap): Either set fastmap['\n'] to 1 or don't change it.
Richard M. Stallman <rms@gnu.org>
parents:
8102
diff
changeset
|
2978 /* ... except perhaps newline. */ |
bebf92e1b110
(re_compile_fastmap): Either set fastmap['\n'] to 1 or don't change it.
Richard M. Stallman <rms@gnu.org>
parents:
8102
diff
changeset
|
2979 if (!(bufp->syntax & RE_DOT_NEWLINE)) |
bebf92e1b110
(re_compile_fastmap): Either set fastmap['\n'] to 1 or don't change it.
Richard M. Stallman <rms@gnu.org>
parents:
8102
diff
changeset
|
2980 fastmap['\n'] = fastmap_newline; |
bebf92e1b110
(re_compile_fastmap): Either set fastmap['\n'] to 1 or don't change it.
Richard M. Stallman <rms@gnu.org>
parents:
8102
diff
changeset
|
2981 |
bebf92e1b110
(re_compile_fastmap): Either set fastmap['\n'] to 1 or don't change it.
Richard M. Stallman <rms@gnu.org>
parents:
8102
diff
changeset
|
2982 /* Return if we have already set `can_be_null'; if we have, |
bebf92e1b110
(re_compile_fastmap): Either set fastmap['\n'] to 1 or don't change it.
Richard M. Stallman <rms@gnu.org>
parents:
8102
diff
changeset
|
2983 then the fastmap is irrelevant. Something's wrong here. */ |
bebf92e1b110
(re_compile_fastmap): Either set fastmap['\n'] to 1 or don't change it.
Richard M. Stallman <rms@gnu.org>
parents:
8102
diff
changeset
|
2984 else if (bufp->can_be_null) |
11622
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
2985 goto done; |
8114
bebf92e1b110
(re_compile_fastmap): Either set fastmap['\n'] to 1 or don't change it.
Richard M. Stallman <rms@gnu.org>
parents:
8102
diff
changeset
|
2986 |
bebf92e1b110
(re_compile_fastmap): Either set fastmap['\n'] to 1 or don't change it.
Richard M. Stallman <rms@gnu.org>
parents:
8102
diff
changeset
|
2987 /* Otherwise, have to check alternative paths. */ |
bebf92e1b110
(re_compile_fastmap): Either set fastmap['\n'] to 1 or don't change it.
Richard M. Stallman <rms@gnu.org>
parents:
8102
diff
changeset
|
2988 break; |
bebf92e1b110
(re_compile_fastmap): Either set fastmap['\n'] to 1 or don't change it.
Richard M. Stallman <rms@gnu.org>
parents:
8102
diff
changeset
|
2989 } |
1155 | 2990 |
2991 #ifdef emacs | |
2992 case syntaxspec: | |
2993 k = *p++; | |
2994 for (j = 0; j < (1 << BYTEWIDTH); j++) | |
2995 if (SYNTAX (j) == (enum syntaxcode) k) | |
2996 fastmap[j] = 1; | |
2997 break; | |
2998 | |
2999 | |
3000 case notsyntaxspec: | |
3001 k = *p++; | |
3002 for (j = 0; j < (1 << BYTEWIDTH); j++) | |
3003 if (SYNTAX (j) != (enum syntaxcode) k) | |
3004 fastmap[j] = 1; | |
3005 break; | |
3006 | |
3007 | |
3008 /* All cases after this match the empty string. These end with | |
3009 `continue'. */ | |
3010 | |
3011 | |
3012 case before_dot: | |
3013 case at_dot: | |
3014 case after_dot: | |
3015 continue; | |
3016 #endif /* not emacs */ | |
3017 | |
3018 | |
3019 case no_op: | |
3020 case begline: | |
3021 case endline: | |
3022 case begbuf: | |
3023 case endbuf: | |
3024 case wordbound: | |
3025 case notwordbound: | |
3026 case wordbeg: | |
3027 case wordend: | |
3028 case push_dummy_failure: | |
3029 continue; | |
3030 | |
3031 | |
3032 case jump_n: | |
3033 case pop_failure_jump: | |
3034 case maybe_pop_jump: | |
3035 case jump: | |
3036 case jump_past_alt: | |
3037 case dummy_failure_jump: | |
3038 EXTRACT_NUMBER_AND_INCR (j, p); | |
3039 p += j; | |
3040 if (j > 0) | |
3041 continue; | |
3042 | |
3043 /* Jump backward implies we just went through the body of a | |
3044 loop and matched nothing. Opcode jumped to should be | |
3045 `on_failure_jump' or `succeed_n'. Just treat it like an | |
3046 ordinary jump. For a * loop, it has pushed its failure | |
3047 point already; if so, discard that as redundant. */ | |
3048 if ((re_opcode_t) *p != on_failure_jump | |
3049 && (re_opcode_t) *p != succeed_n) | |
3050 continue; | |
3051 | |
3052 p++; | |
3053 EXTRACT_NUMBER_AND_INCR (j, p); | |
3054 p += j; | |
3055 | |
3056 /* If what's on the stack is where we are now, pop it. */ | |
3057 if (!FAIL_STACK_EMPTY () | |
11656
a40849041718
(union fail_stack_elt): New union.
Richard M. Stallman <rms@gnu.org>
parents:
11622
diff
changeset
|
3058 && fail_stack.stack[fail_stack.avail - 1].pointer == p) |
1155 | 3059 fail_stack.avail--; |
3060 | |
3061 continue; | |
3062 | |
3063 | |
3064 case on_failure_jump: | |
3065 case on_failure_keep_string_jump: | |
3066 handle_on_failure_jump: | |
3067 EXTRACT_NUMBER_AND_INCR (j, p); | |
3068 | |
3069 /* For some patterns, e.g., `(a?)?', `p+j' here points to the | |
3070 end of the pattern. We don't want to push such a point, | |
3071 since when we restore it above, entering the switch will | |
3072 increment `p' past the end of the pattern. We don't need | |
3073 to push such a point since we obviously won't find any more | |
3074 fastmap entries beyond `pend'. Such a pattern can match | |
3075 the null string, though. */ | |
3076 if (p + j < pend) | |
3077 { | |
3078 if (!PUSH_PATTERN_OP (p + j, fail_stack)) | |
11622
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
3079 { |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
3080 REGEX_FREE_STACK (fail_stack.stack); |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
3081 return -2; |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
3082 } |
1155 | 3083 } |
3084 else | |
3085 bufp->can_be_null = 1; | |
3086 | |
3087 if (succeed_n_p) | |
3088 { | |
3089 EXTRACT_NUMBER_AND_INCR (k, p); /* Skip the n. */ | |
3090 succeed_n_p = false; | |
3091 } | |
3092 | |
3093 continue; | |
3094 | |
3095 | |
3096 case succeed_n: | |
3097 /* Get to the number of times to succeed. */ | |
3098 p += 2; | |
3099 | |
3100 /* Increment p past the n for when k != 0. */ | |
3101 EXTRACT_NUMBER_AND_INCR (k, p); | |
3102 if (k == 0) | |
3103 { | |
3104 p -= 4; | |
3105 succeed_n_p = true; /* Spaghetti code alert. */ | |
3106 goto handle_on_failure_jump; | |
3107 } | |
3108 continue; | |
3109 | |
3110 | |
3111 case set_number_at: | |
3112 p += 4; | |
3113 continue; | |
3114 | |
3115 | |
3116 case start_memory: | |
3117 case stop_memory: | |
3118 p += 2; | |
3119 continue; | |
3120 | |
3121 | |
3122 default: | |
3123 abort (); /* We have listed all the cases. */ | |
3124 } /* switch *p++ */ | |
3125 | |
3126 /* Getting here means we have found the possible starting | |
3127 characters for one path of the pattern -- and that the empty | |
3128 string does not match. We need not follow this path further. | |
3129 Instead, look at the next alternative (remembered on the | |
3130 stack), or quit if no more. The test at the top of the loop | |
3131 does these things. */ | |
3132 path_can_be_null = false; | |
3133 p = pend; | |
3134 } /* while p */ | |
3135 | |
3136 /* Set `can_be_null' for the last path (also the first path, if the | |
3137 pattern is empty). */ | |
3138 bufp->can_be_null |= path_can_be_null; | |
11622
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
3139 |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
3140 done: |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
3141 REGEX_FREE_STACK (fail_stack.stack); |
1155 | 3142 return 0; |
3143 } /* re_compile_fastmap */ | |
3144 | |
3145 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and | |
3146 ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use | |
3147 this memory for recording register information. STARTS and ENDS | |
3148 must be allocated using the malloc library routine, and must each | |
3149 be at least NUM_REGS * sizeof (regoff_t) bytes long. | |
3150 | |
3151 If NUM_REGS == 0, then subsequent matches should allocate their own | |
3152 register data. | |
3153 | |
3154 Unless this function is called, the first search or match using | |
3155 PATTERN_BUFFER will allocate its own register data, without | |
3156 freeing the old data. */ | |
3157 | |
3158 void | |
3159 re_set_registers (bufp, regs, num_regs, starts, ends) | |
3160 struct re_pattern_buffer *bufp; | |
3161 struct re_registers *regs; | |
3162 unsigned num_regs; | |
3163 regoff_t *starts, *ends; | |
3164 { | |
3165 if (num_regs) | |
3166 { | |
3167 bufp->regs_allocated = REGS_REALLOCATE; | |
3168 regs->num_regs = num_regs; | |
3169 regs->start = starts; | |
3170 regs->end = ends; | |
3171 } | |
3172 else | |
3173 { | |
3174 bufp->regs_allocated = REGS_UNALLOCATED; | |
3175 regs->num_regs = 0; | |
5014
6062331f7430
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
4918
diff
changeset
|
3176 regs->start = regs->end = (regoff_t *) 0; |
1155 | 3177 } |
3178 } | |
3179 | |
3180 /* Searching routines. */ | |
3181 | |
3182 /* Like re_search_2, below, but only one string is specified, and | |
3183 doesn't let you say where to stop matching. */ | |
3184 | |
3185 int | |
3186 re_search (bufp, string, size, startpos, range, regs) | |
3187 struct re_pattern_buffer *bufp; | |
3188 const char *string; | |
3189 int size, startpos, range; | |
3190 struct re_registers *regs; | |
3191 { | |
3192 return re_search_2 (bufp, NULL, 0, string, size, startpos, range, | |
3193 regs, size); | |
3194 } | |
3195 | |
3196 | |
3197 /* Using the compiled pattern in BUFP->buffer, first tries to match the | |
3198 virtual concatenation of STRING1 and STRING2, starting first at index | |
3199 STARTPOS, then at STARTPOS + 1, and so on. | |
3200 | |
3201 STRING1 and STRING2 have length SIZE1 and SIZE2, respectively. | |
3202 | |
3203 RANGE is how far to scan while trying to match. RANGE = 0 means try | |
3204 only at STARTPOS; in general, the last start tried is STARTPOS + | |
3205 RANGE. | |
3206 | |
3207 In REGS, return the indices of the virtual concatenation of STRING1 | |
3208 and STRING2 that matched the entire BUFP->buffer and its contained | |
3209 subexpressions. | |
3210 | |
3211 Do not consider matching one past the index STOP in the virtual | |
3212 concatenation of STRING1 and STRING2. | |
3213 | |
3214 We return either the position in the strings at which the match was | |
3215 found, -1 if no match, or -2 if error (such as failure | |
3216 stack overflow). */ | |
3217 | |
3218 int | |
3219 re_search_2 (bufp, string1, size1, string2, size2, startpos, range, regs, stop) | |
3220 struct re_pattern_buffer *bufp; | |
3221 const char *string1, *string2; | |
3222 int size1, size2; | |
3223 int startpos; | |
3224 int range; | |
3225 struct re_registers *regs; | |
3226 int stop; | |
3227 { | |
3228 int val; | |
3229 register char *fastmap = bufp->fastmap; | |
3230 register char *translate = bufp->translate; | |
3231 int total_size = size1 + size2; | |
3232 int endpos = startpos + range; | |
3233 | |
3234 /* Check for out-of-range STARTPOS. */ | |
3235 if (startpos < 0 || startpos > total_size) | |
3236 return -1; | |
3237 | |
3238 /* Fix up RANGE if it might eventually take us outside | |
3239 the virtual concatenation of STRING1 and STRING2. */ | |
3240 if (endpos < -1) | |
3241 range = -1 - startpos; | |
3242 else if (endpos > total_size) | |
3243 range = total_size - startpos; | |
3244 | |
3245 /* If the search isn't to be a backwards one, don't waste time in a | |
1637 | 3246 search for a pattern that must be anchored. */ |
3247 if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == begbuf && range > 0) | |
1155 | 3248 { |
3249 if (startpos > 0) | |
3250 return -1; | |
3251 else | |
3252 range = 1; | |
3253 } | |
3254 | |
1637 | 3255 /* Update the fastmap now if not correct already. */ |
3256 if (fastmap && !bufp->fastmap_accurate) | |
3257 if (re_compile_fastmap (bufp) == -2) | |
3258 return -2; | |
3259 | |
3260 /* Loop through the string, looking for a place to start matching. */ | |
1155 | 3261 for (;;) |
3262 { | |
3263 /* If a fastmap is supplied, skip quickly over characters that | |
3264 cannot be the start of a match. If the pattern can match the | |
3265 null string, however, we don't need to skip characters; we want | |
3266 the first null string. */ | |
3267 if (fastmap && startpos < total_size && !bufp->can_be_null) | |
3268 { | |
3269 if (range > 0) /* Searching forwards. */ | |
3270 { | |
3271 register const char *d; | |
3272 register int lim = 0; | |
3273 int irange = range; | |
3274 | |
3275 if (startpos < size1 && startpos + range >= size1) | |
3276 lim = range - (size1 - startpos); | |
3277 | |
3278 d = (startpos >= size1 ? string2 - size1 : string1) + startpos; | |
3279 | |
3280 /* Written out as an if-else to avoid testing `translate' | |
3281 inside the loop. */ | |
3282 if (translate) | |
3283 while (range > lim | |
2078 | 3284 && !fastmap[(unsigned char) |
3285 translate[(unsigned char) *d++]]) | |
1155 | 3286 range--; |
3287 else | |
3288 while (range > lim && !fastmap[(unsigned char) *d++]) | |
3289 range--; | |
3290 | |
3291 startpos += irange - range; | |
3292 } | |
3293 else /* Searching backwards. */ | |
3294 { | |
3295 register char c = (size1 == 0 || startpos >= size1 | |
3296 ? string2[startpos - size1] | |
3297 : string1[startpos]); | |
3298 | |
1637 | 3299 if (!fastmap[(unsigned char) TRANSLATE (c)]) |
1155 | 3300 goto advance; |
3301 } | |
3302 } | |
3303 | |
3304 /* If can't match the null string, and that's all we have left, fail. */ | |
3305 if (range >= 0 && startpos == total_size && fastmap | |
3306 && !bufp->can_be_null) | |
3307 return -1; | |
3308 | |
6083 | 3309 val = re_match_2_internal (bufp, string1, size1, string2, size2, |
3310 startpos, regs, stop); | |
7318
eed73d042fa4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
7305
diff
changeset
|
3311 #ifndef REGEX_MALLOC |
eed73d042fa4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
7305
diff
changeset
|
3312 #ifdef C_ALLOCA |
6083 | 3313 alloca (0); |
7318
eed73d042fa4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
7305
diff
changeset
|
3314 #endif |
eed73d042fa4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
7305
diff
changeset
|
3315 #endif |
6083 | 3316 |
1155 | 3317 if (val >= 0) |
3318 return startpos; | |
3319 | |
3320 if (val == -2) | |
3321 return -2; | |
3322 | |
3323 advance: | |
3324 if (!range) | |
3325 break; | |
3326 else if (range > 0) | |
3327 { | |
3328 range--; | |
3329 startpos++; | |
3330 } | |
3331 else | |
3332 { | |
3333 range++; | |
3334 startpos--; | |
3335 } | |
3336 } | |
3337 return -1; | |
3338 } /* re_search_2 */ | |
3339 | |
3340 /* Declarations and macros for re_match_2. */ | |
3341 | |
3342 static int bcmp_translate (); | |
3343 static boolean alt_match_null_string_p (), | |
3344 common_op_match_null_string_p (), | |
3345 group_match_null_string_p (); | |
3346 | |
3347 /* This converts PTR, a pointer into one of the search strings `string1' | |
3348 and `string2' into an offset from the beginning of that string. */ | |
4918
e928d39564ad
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
4846
diff
changeset
|
3349 #define POINTER_TO_OFFSET(ptr) \ |
e928d39564ad
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
4846
diff
changeset
|
3350 (FIRST_STRING_P (ptr) \ |
e928d39564ad
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
4846
diff
changeset
|
3351 ? ((regoff_t) ((ptr) - string1)) \ |
e928d39564ad
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
4846
diff
changeset
|
3352 : ((regoff_t) ((ptr) - string2 + size1))) |
1155 | 3353 |
3354 /* Macros for dealing with the split strings in re_match_2. */ | |
3355 | |
3356 #define MATCHING_IN_FIRST_STRING (dend == end_match_1) | |
3357 | |
3358 /* Call before fetching a character with *d. This switches over to | |
3359 string2 if necessary. */ | |
3360 #define PREFETCH() \ | |
3361 while (d == dend) \ | |
3362 { \ | |
3363 /* End of string2 => fail. */ \ | |
3364 if (dend == end_match_2) \ | |
3365 goto fail; \ | |
3366 /* End of string1 => advance to string2. */ \ | |
3367 d = string2; \ | |
3368 dend = end_match_2; \ | |
3369 } | |
3370 | |
3371 | |
3372 /* Test if at very beginning or at very end of the virtual concatenation | |
3373 of `string1' and `string2'. If only one string, it's `string2'. */ | |
1637 | 3374 #define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2) |
3375 #define AT_STRINGS_END(d) ((d) == end2) | |
1155 | 3376 |
3377 | |
3378 /* Test if D points to a character which is word-constituent. We have | |
3379 two special cases to check for: if past the end of string1, look at | |
3380 the first character in string2; and if before the beginning of | |
1637 | 3381 string2, look at the last character in string1. */ |
3382 #define WORDCHAR_P(d) \ | |
1155 | 3383 (SYNTAX ((d) == end1 ? *string2 \ |
1637 | 3384 : (d) == string2 - 1 ? *(end1 - 1) : *(d)) \ |
3385 == Sword) | |
1155 | 3386 |
3387 /* Test if the character before D and the one at D differ with respect | |
3388 to being word-constituent. */ | |
3389 #define AT_WORD_BOUNDARY(d) \ | |
1637 | 3390 (AT_STRINGS_BEG (d) || AT_STRINGS_END (d) \ |
3391 || WORDCHAR_P (d - 1) != WORDCHAR_P (d)) | |
1155 | 3392 |
3393 | |
3394 /* Free everything we malloc. */ | |
2952 | 3395 #ifdef MATCH_MAY_ALLOCATE |
11622
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
3396 #define FREE_VAR(var) if (var) REGEX_FREE (var); var = NULL |
1155 | 3397 #define FREE_VARIABLES() \ |
3398 do { \ | |
11622
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
3399 REGEX_FREE_STACK (fail_stack.stack); \ |
1155 | 3400 FREE_VAR (regstart); \ |
3401 FREE_VAR (regend); \ | |
3402 FREE_VAR (old_regstart); \ | |
3403 FREE_VAR (old_regend); \ | |
3404 FREE_VAR (best_regstart); \ | |
3405 FREE_VAR (best_regend); \ | |
3406 FREE_VAR (reg_info); \ | |
3407 FREE_VAR (reg_dummy); \ | |
3408 FREE_VAR (reg_info_dummy); \ | |
3409 } while (0) | |
2952 | 3410 #else |
3411 #define FREE_VARIABLES() /* Do nothing! */ | |
3412 #endif /* not MATCH_MAY_ALLOCATE */ | |
1155 | 3413 |
3414 /* These values must meet several constraints. They must not be valid | |
3415 register values; since we have a limit of 255 registers (because | |
3416 we use only one byte in the pattern for the register number), we can | |
3417 use numbers larger than 255. They must differ by 1, because of | |
3418 NUM_FAILURE_ITEMS above. And the value for the lowest register must | |
3419 be larger than the value for the highest register, so we do not try | |
3420 to actually save any registers when none are active. */ | |
3421 #define NO_HIGHEST_ACTIVE_REG (1 << BYTEWIDTH) | |
3422 #define NO_LOWEST_ACTIVE_REG (NO_HIGHEST_ACTIVE_REG + 1) | |
3423 | |
3424 /* Matching routines. */ | |
3425 | |
3426 #ifndef emacs /* Emacs never uses this. */ | |
3427 /* re_match is like re_match_2 except it takes only a single string. */ | |
3428 | |
3429 int | |
3430 re_match (bufp, string, size, pos, regs) | |
3431 struct re_pattern_buffer *bufp; | |
3432 const char *string; | |
3433 int size, pos; | |
3434 struct re_registers *regs; | |
6083 | 3435 { |
3436 int result = re_match_2_internal (bufp, NULL, 0, string, size, | |
3437 pos, regs, size); | |
3438 alloca (0); | |
3439 return result; | |
1155 | 3440 } |
3441 #endif /* not emacs */ | |
3442 | |
3443 | |
3444 /* re_match_2 matches the compiled pattern in BUFP against the | |
3445 the (virtual) concatenation of STRING1 and STRING2 (of length SIZE1 | |
3446 and SIZE2, respectively). We start matching at POS, and stop | |
3447 matching at STOP. | |
3448 | |
3449 If REGS is non-null and the `no_sub' field of BUFP is nonzero, we | |
3450 store offsets for the substring each group matched in REGS. See the | |
3451 documentation for exactly how many groups we fill. | |
3452 | |
3453 We return -1 if no match, -2 if an internal error (such as the | |
3454 failure stack overflowing). Otherwise, we return the length of the | |
3455 matched substring. */ | |
3456 | |
3457 int | |
3458 re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop) | |
3459 struct re_pattern_buffer *bufp; | |
3460 const char *string1, *string2; | |
3461 int size1, size2; | |
3462 int pos; | |
3463 struct re_registers *regs; | |
3464 int stop; | |
3465 { | |
6083 | 3466 int result = re_match_2_internal (bufp, string1, size1, string2, size2, |
3467 pos, regs, stop); | |
3468 alloca (0); | |
3469 return result; | |
3470 } | |
3471 | |
3472 /* This is a separate function so that we can force an alloca cleanup | |
3473 afterwards. */ | |
3474 static int | |
3475 re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop) | |
3476 struct re_pattern_buffer *bufp; | |
3477 const char *string1, *string2; | |
3478 int size1, size2; | |
3479 int pos; | |
3480 struct re_registers *regs; | |
3481 int stop; | |
3482 { | |
1155 | 3483 /* General temporaries. */ |
3484 int mcnt; | |
3485 unsigned char *p1; | |
3486 | |
3487 /* Just past the end of the corresponding string. */ | |
3488 const char *end1, *end2; | |
3489 | |
3490 /* Pointers into string1 and string2, just past the last characters in | |
3491 each to consider matching. */ | |
3492 const char *end_match_1, *end_match_2; | |
3493 | |
3494 /* Where we are in the data, and the end of the current string. */ | |
3495 const char *d, *dend; | |
3496 | |
3497 /* Where we are in the pattern, and the end of the pattern. */ | |
3498 unsigned char *p = bufp->buffer; | |
3499 register unsigned char *pend = p + bufp->used; | |
3500 | |
5842 | 3501 /* Mark the opcode just after a start_memory, so we can test for an |
3502 empty subpattern when we get to the stop_memory. */ | |
3503 unsigned char *just_past_start_mem = 0; | |
3504 | |
1155 | 3505 /* We use this to map every character in the string. */ |
3506 char *translate = bufp->translate; | |
3507 | |
3508 /* Failure point stack. Each place that can handle a failure further | |
3509 down the line pushes a failure point on this stack. It consists of | |
3510 restart, regend, and reg_info for all registers corresponding to | |
3511 the subexpressions we're currently inside, plus the number of such | |
3512 registers, and, finally, two char *'s. The first char * is where | |
3513 to resume scanning the pattern; the second one is where to resume | |
3514 scanning the strings. If the latter is zero, the failure point is | |
3515 a ``dummy''; if a failure happens and the failure point is a dummy, | |
3516 it gets discarded and the next next one is tried. */ | |
2952 | 3517 #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */ |
1155 | 3518 fail_stack_type fail_stack; |
2949 | 3519 #endif |
1155 | 3520 #ifdef DEBUG |
3521 static unsigned failure_id = 0; | |
1637 | 3522 unsigned nfailure_points_pushed = 0, nfailure_points_popped = 0; |
1155 | 3523 #endif |
3524 | |
11622
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
3525 /* This holds the pointer to the failure stack, when |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
3526 it is allocated relocatably. */ |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
3527 fail_stack_elt_t *failure_stack_ptr; |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
3528 |
1155 | 3529 /* We fill all the registers internally, independent of what we |
3530 return, for use in backreferences. The number here includes | |
3531 an element for register zero. */ | |
3532 unsigned num_regs = bufp->re_nsub + 1; | |
3533 | |
3534 /* The currently active registers. */ | |
3535 unsigned lowest_active_reg = NO_LOWEST_ACTIVE_REG; | |
3536 unsigned highest_active_reg = NO_HIGHEST_ACTIVE_REG; | |
3537 | |
3538 /* Information on the contents of registers. These are pointers into | |
3539 the input strings; they record just what was matched (on this | |
3540 attempt) by a subexpression part of the pattern, that is, the | |
3541 regnum-th regstart pointer points to where in the pattern we began | |
3542 matching and the regnum-th regend points to right after where we | |
3543 stopped matching the regnum-th subexpression. (The zeroth register | |
3544 keeps track of what the whole pattern matches.) */ | |
2952 | 3545 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */ |
1155 | 3546 const char **regstart, **regend; |
2949 | 3547 #endif |
1155 | 3548 |
3549 /* If a group that's operated upon by a repetition operator fails to | |
3550 match anything, then the register for its start will need to be | |
3551 restored because it will have been set to wherever in the string we | |
3552 are when we last see its open-group operator. Similarly for a | |
3553 register's end. */ | |
2952 | 3554 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */ |
1155 | 3555 const char **old_regstart, **old_regend; |
2949 | 3556 #endif |
1155 | 3557 |
3558 /* The is_active field of reg_info helps us keep track of which (possibly | |
3559 nested) subexpressions we are currently in. The matched_something | |
3560 field of reg_info[reg_num] helps us tell whether or not we have | |
3561 matched any of the pattern so far this time through the reg_num-th | |
3562 subexpression. These two fields get reset each time through any | |
3563 loop their register is in. */ | |
2952 | 3564 #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */ |
1155 | 3565 register_info_type *reg_info; |
2949 | 3566 #endif |
1155 | 3567 |
3568 /* The following record the register info as found in the above | |
3569 variables when we find a match better than any we've seen before. | |
3570 This happens as we backtrack through the failure points, which in | |
3571 turn happens only if we have not yet matched the entire string. */ | |
3572 unsigned best_regs_set = false; | |
2952 | 3573 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */ |
1155 | 3574 const char **best_regstart, **best_regend; |
2949 | 3575 #endif |
1155 | 3576 |
3577 /* Logically, this is `best_regend[0]'. But we don't want to have to | |
3578 allocate space for that if we're not allocating space for anything | |
3579 else (see below). Also, we never need info about register 0 for | |
3580 any of the other register vectors, and it seems rather a kludge to | |
3581 treat `best_regend' differently than the rest. So we keep track of | |
3582 the end of the best match so far in a separate variable. We | |
3583 initialize this to NULL so that when we backtrack the first time | |
3584 and need to test it, it's not garbage. */ | |
3585 const char *match_end = NULL; | |
3586 | |
10242
8fa35cc770be
(SET_REGS_MATCHED): Do nothing if set_regs_matched_done is 1.
Richard M. Stallman <rms@gnu.org>
parents:
10148
diff
changeset
|
3587 /* This helps SET_REGS_MATCHED avoid doing redundant work. */ |
8fa35cc770be
(SET_REGS_MATCHED): Do nothing if set_regs_matched_done is 1.
Richard M. Stallman <rms@gnu.org>
parents:
10148
diff
changeset
|
3588 int set_regs_matched_done = 0; |
8fa35cc770be
(SET_REGS_MATCHED): Do nothing if set_regs_matched_done is 1.
Richard M. Stallman <rms@gnu.org>
parents:
10148
diff
changeset
|
3589 |
1155 | 3590 /* Used when we pop values we don't care about. */ |
2952 | 3591 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */ |
1155 | 3592 const char **reg_dummy; |
3593 register_info_type *reg_info_dummy; | |
2949 | 3594 #endif |
1155 | 3595 |
3596 #ifdef DEBUG | |
3597 /* Counts the total number of registers pushed. */ | |
3598 unsigned num_regs_pushed = 0; | |
3599 #endif | |
3600 | |
3601 DEBUG_PRINT1 ("\n\nEntering re_match_2.\n"); | |
3602 | |
3603 INIT_FAIL_STACK (); | |
3604 | |
2952 | 3605 #ifdef MATCH_MAY_ALLOCATE |
1155 | 3606 /* Do not bother to initialize all the register variables if there are |
3607 no groups in the pattern, as it takes a fair amount of time. If | |
3608 there are groups, we include space for register 0 (the whole | |
3609 pattern), even though we never use it, since it simplifies the | |
3610 array indexing. We should fix this. */ | |
3611 if (bufp->re_nsub) | |
3612 { | |
3613 regstart = REGEX_TALLOC (num_regs, const char *); | |
3614 regend = REGEX_TALLOC (num_regs, const char *); | |
3615 old_regstart = REGEX_TALLOC (num_regs, const char *); | |
3616 old_regend = REGEX_TALLOC (num_regs, const char *); | |
3617 best_regstart = REGEX_TALLOC (num_regs, const char *); | |
3618 best_regend = REGEX_TALLOC (num_regs, const char *); | |
3619 reg_info = REGEX_TALLOC (num_regs, register_info_type); | |
3620 reg_dummy = REGEX_TALLOC (num_regs, const char *); | |
3621 reg_info_dummy = REGEX_TALLOC (num_regs, register_info_type); | |
3622 | |
3623 if (!(regstart && regend && old_regstart && old_regend && reg_info | |
3624 && best_regstart && best_regend && reg_dummy && reg_info_dummy)) | |
3625 { | |
3626 FREE_VARIABLES (); | |
3627 return -2; | |
3628 } | |
3629 } | |
3630 else | |
3631 { | |
3632 /* We must initialize all our variables to NULL, so that | |
1637 | 3633 `FREE_VARIABLES' doesn't try to free them. */ |
1155 | 3634 regstart = regend = old_regstart = old_regend = best_regstart |
3635 = best_regend = reg_dummy = NULL; | |
3636 reg_info = reg_info_dummy = (register_info_type *) NULL; | |
3637 } | |
2952 | 3638 #endif /* MATCH_MAY_ALLOCATE */ |
1155 | 3639 |
3640 /* The starting position is bogus. */ | |
3641 if (pos < 0 || pos > size1 + size2) | |
3642 { | |
3643 FREE_VARIABLES (); | |
3644 return -1; | |
3645 } | |
3646 | |
3647 /* Initialize subexpression text positions to -1 to mark ones that no | |
3648 start_memory/stop_memory has been seen for. Also initialize the | |
3649 register information struct. */ | |
3650 for (mcnt = 1; mcnt < num_regs; mcnt++) | |
3651 { | |
3652 regstart[mcnt] = regend[mcnt] | |
3653 = old_regstart[mcnt] = old_regend[mcnt] = REG_UNSET_VALUE; | |
3654 | |
3655 REG_MATCH_NULL_STRING_P (reg_info[mcnt]) = MATCH_NULL_UNSET_VALUE; | |
3656 IS_ACTIVE (reg_info[mcnt]) = 0; | |
3657 MATCHED_SOMETHING (reg_info[mcnt]) = 0; | |
3658 EVER_MATCHED_SOMETHING (reg_info[mcnt]) = 0; | |
3659 } | |
3660 | |
3661 /* We move `string1' into `string2' if the latter's empty -- but not if | |
3662 `string1' is null. */ | |
3663 if (size2 == 0 && string1 != NULL) | |
3664 { | |
3665 string2 = string1; | |
3666 size2 = size1; | |
3667 string1 = 0; | |
3668 size1 = 0; | |
3669 } | |
3670 end1 = string1 + size1; | |
3671 end2 = string2 + size2; | |
3672 | |
3673 /* Compute where to stop matching, within the two strings. */ | |
3674 if (stop <= size1) | |
3675 { | |
3676 end_match_1 = string1 + stop; | |
3677 end_match_2 = string2; | |
3678 } | |
3679 else | |
3680 { | |
3681 end_match_1 = end1; | |
3682 end_match_2 = string2 + stop - size1; | |
3683 } | |
3684 | |
3685 /* `p' scans through the pattern as `d' scans through the data. | |
3686 `dend' is the end of the input string that `d' points within. `d' | |
3687 is advanced into the following input string whenever necessary, but | |
3688 this happens before fetching; therefore, at the beginning of the | |
3689 loop, `d' can be pointing at the end of a string, but it cannot | |
3690 equal `string2'. */ | |
3691 if (size1 > 0 && pos <= size1) | |
3692 { | |
3693 d = string1 + pos; | |
3694 dend = end_match_1; | |
3695 } | |
3696 else | |
3697 { | |
3698 d = string2 + pos - size1; | |
3699 dend = end_match_2; | |
3700 } | |
3701 | |
3702 DEBUG_PRINT1 ("The compiled pattern is: "); | |
3703 DEBUG_PRINT_COMPILED_PATTERN (bufp, p, pend); | |
3704 DEBUG_PRINT1 ("The string to match is: `"); | |
3705 DEBUG_PRINT_DOUBLE_STRING (d, string1, size1, string2, size2); | |
3706 DEBUG_PRINT1 ("'\n"); | |
3707 | |
3708 /* This loops over pattern commands. It exits by returning from the | |
3709 function if the match is complete, or it drops through if the match | |
3710 fails at this starting point in the input data. */ | |
3711 for (;;) | |
3712 { | |
3713 DEBUG_PRINT2 ("\n0x%x: ", p); | |
3714 | |
3715 if (p == pend) | |
3716 { /* End of pattern means we might have succeeded. */ | |
1637 | 3717 DEBUG_PRINT1 ("end of pattern ... "); |
3718 | |
3719 /* If we haven't matched the entire string, and we want the | |
3720 longest match, try backtracking. */ | |
1155 | 3721 if (d != end_match_2) |
3722 { | |
8114
bebf92e1b110
(re_compile_fastmap): Either set fastmap['\n'] to 1 or don't change it.
Richard M. Stallman <rms@gnu.org>
parents:
8102
diff
changeset
|
3723 /* 1 if this match ends in the same string (string1 or string2) |
bebf92e1b110
(re_compile_fastmap): Either set fastmap['\n'] to 1 or don't change it.
Richard M. Stallman <rms@gnu.org>
parents:
8102
diff
changeset
|
3724 as the best previous match. */ |
bebf92e1b110
(re_compile_fastmap): Either set fastmap['\n'] to 1 or don't change it.
Richard M. Stallman <rms@gnu.org>
parents:
8102
diff
changeset
|
3725 boolean same_str_p = (FIRST_STRING_P (match_end) |
bebf92e1b110
(re_compile_fastmap): Either set fastmap['\n'] to 1 or don't change it.
Richard M. Stallman <rms@gnu.org>
parents:
8102
diff
changeset
|
3726 == MATCHING_IN_FIRST_STRING); |
bebf92e1b110
(re_compile_fastmap): Either set fastmap['\n'] to 1 or don't change it.
Richard M. Stallman <rms@gnu.org>
parents:
8102
diff
changeset
|
3727 /* 1 if this match is the best seen so far. */ |
8561
dc5353720725
(regex_compile): Split an if to avoid compiler bug.
Richard M. Stallman <rms@gnu.org>
parents:
8402
diff
changeset
|
3728 boolean best_match_p; |
dc5353720725
(regex_compile): Split an if to avoid compiler bug.
Richard M. Stallman <rms@gnu.org>
parents:
8402
diff
changeset
|
3729 |
dc5353720725
(regex_compile): Split an if to avoid compiler bug.
Richard M. Stallman <rms@gnu.org>
parents:
8402
diff
changeset
|
3730 /* AIX compiler got confused when this was combined |
dc5353720725
(regex_compile): Split an if to avoid compiler bug.
Richard M. Stallman <rms@gnu.org>
parents:
8402
diff
changeset
|
3731 with the previous declaration. */ |
dc5353720725
(regex_compile): Split an if to avoid compiler bug.
Richard M. Stallman <rms@gnu.org>
parents:
8402
diff
changeset
|
3732 if (same_str_p) |
dc5353720725
(regex_compile): Split an if to avoid compiler bug.
Richard M. Stallman <rms@gnu.org>
parents:
8402
diff
changeset
|
3733 best_match_p = d > match_end; |
dc5353720725
(regex_compile): Split an if to avoid compiler bug.
Richard M. Stallman <rms@gnu.org>
parents:
8402
diff
changeset
|
3734 else |
dc5353720725
(regex_compile): Split an if to avoid compiler bug.
Richard M. Stallman <rms@gnu.org>
parents:
8402
diff
changeset
|
3735 best_match_p = !MATCHING_IN_FIRST_STRING; |
8114
bebf92e1b110
(re_compile_fastmap): Either set fastmap['\n'] to 1 or don't change it.
Richard M. Stallman <rms@gnu.org>
parents:
8102
diff
changeset
|
3736 |
1155 | 3737 DEBUG_PRINT1 ("backtracking.\n"); |
3738 | |
3739 if (!FAIL_STACK_EMPTY ()) | |
3740 { /* More failure points to try. */ | |
3741 | |
3742 /* If exceeds best match so far, save it. */ | |
8114
bebf92e1b110
(re_compile_fastmap): Either set fastmap['\n'] to 1 or don't change it.
Richard M. Stallman <rms@gnu.org>
parents:
8102
diff
changeset
|
3743 if (!best_regs_set || best_match_p) |
1155 | 3744 { |
3745 best_regs_set = true; | |
3746 match_end = d; | |
3747 | |
3748 DEBUG_PRINT1 ("\nSAVING match as best so far.\n"); | |
3749 | |
3750 for (mcnt = 1; mcnt < num_regs; mcnt++) | |
3751 { | |
3752 best_regstart[mcnt] = regstart[mcnt]; | |
3753 best_regend[mcnt] = regend[mcnt]; | |
3754 } | |
3755 } | |
3756 goto fail; | |
3757 } | |
3758 | |
8114
bebf92e1b110
(re_compile_fastmap): Either set fastmap['\n'] to 1 or don't change it.
Richard M. Stallman <rms@gnu.org>
parents:
8102
diff
changeset
|
3759 /* If no failure points, don't restore garbage. And if |
bebf92e1b110
(re_compile_fastmap): Either set fastmap['\n'] to 1 or don't change it.
Richard M. Stallman <rms@gnu.org>
parents:
8102
diff
changeset
|
3760 last match is real best match, don't restore second |
bebf92e1b110
(re_compile_fastmap): Either set fastmap['\n'] to 1 or don't change it.
Richard M. Stallman <rms@gnu.org>
parents:
8102
diff
changeset
|
3761 best one. */ |
bebf92e1b110
(re_compile_fastmap): Either set fastmap['\n'] to 1 or don't change it.
Richard M. Stallman <rms@gnu.org>
parents:
8102
diff
changeset
|
3762 else if (best_regs_set && !best_match_p) |
1155 | 3763 { |
3764 restore_best_regs: | |
3765 /* Restore best match. It may happen that `dend == | |
3766 end_match_1' while the restored d is in string2. | |
3767 For example, the pattern `x.*y.*z' against the | |
3768 strings `x-' and `y-z-', if the two strings are | |
3769 not consecutive in memory. */ | |
1637 | 3770 DEBUG_PRINT1 ("Restoring best registers.\n"); |
3771 | |
1155 | 3772 d = match_end; |
3773 dend = ((d >= string1 && d <= end1) | |
3774 ? end_match_1 : end_match_2); | |
3775 | |
3776 for (mcnt = 1; mcnt < num_regs; mcnt++) | |
3777 { | |
3778 regstart[mcnt] = best_regstart[mcnt]; | |
3779 regend[mcnt] = best_regend[mcnt]; | |
3780 } | |
3781 } | |
3782 } /* d != end_match_2 */ | |
3783 | |
11330
20a91bd7b59d
(re_match_2_internal): Eliminate cast of ptr to int.
Richard M. Stallman <rms@gnu.org>
parents:
10850
diff
changeset
|
3784 succeed_label: |
1637 | 3785 DEBUG_PRINT1 ("Accepting match.\n"); |
1155 | 3786 |
3787 /* If caller wants register contents data back, do it. */ | |
3788 if (regs && !bufp->no_sub) | |
3789 { | |
3790 /* Have the register data arrays been allocated? */ | |
3791 if (bufp->regs_allocated == REGS_UNALLOCATED) | |
3792 { /* No. So allocate them with malloc. We need one | |
3793 extra element beyond `num_regs' for the `-1' marker | |
3794 GNU code uses. */ | |
3795 regs->num_regs = MAX (RE_NREGS, num_regs + 1); | |
3796 regs->start = TALLOC (regs->num_regs, regoff_t); | |
3797 regs->end = TALLOC (regs->num_regs, regoff_t); | |
3798 if (regs->start == NULL || regs->end == NULL) | |
11622
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
3799 { |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
3800 FREE_VARIABLES (); |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
3801 return -2; |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
3802 } |
1155 | 3803 bufp->regs_allocated = REGS_REALLOCATE; |
3804 } | |
3805 else if (bufp->regs_allocated == REGS_REALLOCATE) | |
3806 { /* Yes. If we need more elements than were already | |
3807 allocated, reallocate them. If we need fewer, just | |
3808 leave it alone. */ | |
3809 if (regs->num_regs < num_regs + 1) | |
3810 { | |
3811 regs->num_regs = num_regs + 1; | |
3812 RETALLOC (regs->start, regs->num_regs, regoff_t); | |
3813 RETALLOC (regs->end, regs->num_regs, regoff_t); | |
3814 if (regs->start == NULL || regs->end == NULL) | |
11622
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
3815 { |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
3816 FREE_VARIABLES (); |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
3817 return -2; |
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
3818 } |
1155 | 3819 } |
3820 } | |
3821 else | |
2465 | 3822 { |
3823 /* These braces fend off a "empty body in an else-statement" | |
3824 warning under GCC when assert expands to nothing. */ | |
3825 assert (bufp->regs_allocated == REGS_FIXED); | |
3826 } | |
1155 | 3827 |
3828 /* Convert the pointer data in `regstart' and `regend' to | |
3829 indices. Register zero has to be set differently, | |
3830 since we haven't kept track of any info for it. */ | |
3831 if (regs->num_regs > 0) | |
3832 { | |
3833 regs->start[0] = pos; | |
4918
e928d39564ad
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
4846
diff
changeset
|
3834 regs->end[0] = (MATCHING_IN_FIRST_STRING |
e928d39564ad
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
4846
diff
changeset
|
3835 ? ((regoff_t) (d - string1)) |
e928d39564ad
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
4846
diff
changeset
|
3836 : ((regoff_t) (d - string2 + size1))); |
1155 | 3837 } |
3838 | |
3839 /* Go through the first `min (num_regs, regs->num_regs)' | |
3840 registers, since that is all we initialized. */ | |
3841 for (mcnt = 1; mcnt < MIN (num_regs, regs->num_regs); mcnt++) | |
3842 { | |
3843 if (REG_UNSET (regstart[mcnt]) || REG_UNSET (regend[mcnt])) | |
3844 regs->start[mcnt] = regs->end[mcnt] = -1; | |
3845 else | |
3846 { | |
4918
e928d39564ad
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
4846
diff
changeset
|
3847 regs->start[mcnt] |
e928d39564ad
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
4846
diff
changeset
|
3848 = (regoff_t) POINTER_TO_OFFSET (regstart[mcnt]); |
e928d39564ad
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
4846
diff
changeset
|
3849 regs->end[mcnt] |
e928d39564ad
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
4846
diff
changeset
|
3850 = (regoff_t) POINTER_TO_OFFSET (regend[mcnt]); |
1155 | 3851 } |
3852 } | |
3853 | |
3854 /* If the regs structure we return has more elements than | |
3855 were in the pattern, set the extra elements to -1. If | |
3856 we (re)allocated the registers, this is the case, | |
3857 because we always allocate enough to have at least one | |
3858 -1 at the end. */ | |
3859 for (mcnt = num_regs; mcnt < regs->num_regs; mcnt++) | |
3860 regs->start[mcnt] = regs->end[mcnt] = -1; | |
3861 } /* regs && !bufp->no_sub */ | |
3862 | |
1637 | 3863 DEBUG_PRINT4 ("%u failure points pushed, %u popped (%u remain).\n", |
3864 nfailure_points_pushed, nfailure_points_popped, | |
3865 nfailure_points_pushed - nfailure_points_popped); | |
3866 DEBUG_PRINT2 ("%u registers pushed.\n", num_regs_pushed); | |
1155 | 3867 |
3868 mcnt = d - pos - (MATCHING_IN_FIRST_STRING | |
3869 ? string1 | |
3870 : string2 - size1); | |
3871 | |
3872 DEBUG_PRINT2 ("Returning %d from re_match_2.\n", mcnt); | |
3873 | |
11622
eba5c25341ff
(PUSH_FAILURE_INT, POP_FAILURE_INT): Use WIDE_INT.
Richard M. Stallman <rms@gnu.org>
parents:
11330
diff
changeset
|
3874 FREE_VARIABLES (); |
1155 | 3875 return mcnt; |
3876 } | |
3877 | |
3878 /* Otherwise match next pattern command. */ | |
10456
9c6110615166
[!emacs] (SWITCH_ENUM_CAST): New macro, from emacs/lisp.h
Karl Heuer <kwzh@gnu.org>
parents:
10297
diff
changeset
|
3879 switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++)) |
1155 | 3880 { |
3881 /* Ignore these. Used to ignore the n of succeed_n's which | |
3882 currently have n == 0. */ | |
3883 case no_op: | |
3884 DEBUG_PRINT1 ("EXECUTING no_op.\n"); | |
3885 break; | |
3886 | |
9983
ef6e1637c777
(re_opcode_t): New opcode `succeed'
Richard M. Stallman <rms@gnu.org>
parents:
9717
diff
changeset
|
3887 case succeed: |
ef6e1637c777
(re_opcode_t): New opcode `succeed'
Richard M. Stallman <rms@gnu.org>
parents:
9717
diff
changeset
|
3888 DEBUG_PRINT1 ("EXECUTING succeed.\n"); |
11330
20a91bd7b59d
(re_match_2_internal): Eliminate cast of ptr to int.
Richard M. Stallman <rms@gnu.org>
parents:
10850
diff
changeset
|
3889 goto succeed_label; |
1155 | 3890 |
3891 /* Match the next n pattern characters exactly. The following | |
3892 byte in the pattern defines n, and the n bytes after that | |
3893 are the characters to match. */ | |
3894 case exactn: | |
3895 mcnt = *p++; | |
3896 DEBUG_PRINT2 ("EXECUTING exactn %d.\n", mcnt); | |
3897 | |
3898 /* This is written out as an if-else so we don't waste time | |
3899 testing `translate' inside the loop. */ | |
3900 if (translate) | |
3901 { | |
3902 do | |
3903 { | |
3904 PREFETCH (); | |
3905 if (translate[(unsigned char) *d++] != (char) *p++) | |
3906 goto fail; | |
3907 } | |
3908 while (--mcnt); | |
3909 } | |
3910 else | |
3911 { | |
3912 do | |
3913 { | |
3914 PREFETCH (); | |
3915 if (*d++ != (char) *p++) goto fail; | |
3916 } | |
3917 while (--mcnt); | |
3918 } | |
3919 SET_REGS_MATCHED (); | |
3920 break; | |
3921 | |
3922 | |
3923 /* Match any character except possibly a newline or a null. */ | |
3924 case anychar: | |
3925 DEBUG_PRINT1 ("EXECUTING anychar.\n"); | |
3926 | |
3927 PREFETCH (); | |
3928 | |
3929 if ((!(bufp->syntax & RE_DOT_NEWLINE) && TRANSLATE (*d) == '\n') | |
3930 || (bufp->syntax & RE_DOT_NOT_NULL && TRANSLATE (*d) == '\000')) | |
3931 goto fail; | |
3932 | |
3933 SET_REGS_MATCHED (); | |
3934 DEBUG_PRINT2 (" Matched `%d'.\n", *d); | |
3935 d++; | |
3936 break; | |
3937 | |
3938 | |
3939 case charset: | |
3940 case charset_not: | |
3941 { | |
3942 register unsigned char c; | |
3943 boolean not = (re_opcode_t) *(p - 1) == charset_not; | |
3944 | |
3945 DEBUG_PRINT2 ("EXECUTING charset%s.\n", not ? "_not" : ""); | |
3946 | |
3947 PREFETCH (); | |
3948 c = TRANSLATE (*d); /* The character to match. */ | |
3949 | |
3950 /* Cast to `unsigned' instead of `unsigned char' in case the | |
3951 bit list is a full 32 bytes long. */ | |
3952 if (c < (unsigned) (*p * BYTEWIDTH) | |
3953 && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH))) | |
3954 not = !not; | |
3955 | |
3956 p += 1 + *p; | |
3957 | |
3958 if (!not) goto fail; | |
3959 | |
3960 SET_REGS_MATCHED (); | |
3961 d++; | |
3962 break; | |
3963 } | |
3964 | |
3965 | |
3966 /* The beginning of a group is represented by start_memory. | |
3967 The arguments are the register number in the next byte, and the | |
3968 number of groups inner to this one in the next. The text | |
3969 matched within the group is recorded (in the internal | |
3970 registers data structure) under the register number. */ | |
3971 case start_memory: | |
3972 DEBUG_PRINT3 ("EXECUTING start_memory %d (%d):\n", *p, p[1]); | |
3973 | |
3974 /* Find out if this group can match the empty string. */ | |
3975 p1 = p; /* To send to group_match_null_string_p. */ | |
3976 | |
3977 if (REG_MATCH_NULL_STRING_P (reg_info[*p]) == MATCH_NULL_UNSET_VALUE) | |
3978 REG_MATCH_NULL_STRING_P (reg_info[*p]) | |
3979 = group_match_null_string_p (&p1, pend, reg_info); | |
3980 | |
3981 /* Save the position in the string where we were the last time | |
3982 we were at this open-group operator in case the group is | |
3983 operated upon by a repetition operator, e.g., with `(a*)*b' | |
3984 against `ab'; then we want to ignore where we are now in | |
3985 the string in case this attempt to match fails. */ | |
3986 old_regstart[*p] = REG_MATCH_NULL_STRING_P (reg_info[*p]) | |
3987 ? REG_UNSET (regstart[*p]) ? d : regstart[*p] | |
3988 : regstart[*p]; | |
3989 DEBUG_PRINT2 (" old_regstart: %d\n", | |
3990 POINTER_TO_OFFSET (old_regstart[*p])); | |
3991 | |
3992 regstart[*p] = d; | |
3993 DEBUG_PRINT2 (" regstart: %d\n", POINTER_TO_OFFSET (regstart[*p])); | |
3994 | |
3995 IS_ACTIVE (reg_info[*p]) = 1; | |
3996 MATCHED_SOMETHING (reg_info[*p]) = 0; | |
10242
8fa35cc770be
(SET_REGS_MATCHED): Do nothing if set_regs_matched_done is 1.
Richard M. Stallman <rms@gnu.org>
parents:
10148
diff
changeset
|
3997 |
8fa35cc770be
(SET_REGS_MATCHED): Do nothing if set_regs_matched_done is 1.
Richard M. Stallman <rms@gnu.org>
parents:
10148
diff
changeset
|
3998 /* Clear this whenever we change the register activity status. */ |
8fa35cc770be
(SET_REGS_MATCHED): Do nothing if set_regs_matched_done is 1.
Richard M. Stallman <rms@gnu.org>
parents:
10148
diff
changeset
|
3999 set_regs_matched_done = 0; |
1155 | 4000 |
4001 /* This is the new highest active register. */ | |
4002 highest_active_reg = *p; | |
4003 | |
4004 /* If nothing was active before, this is the new lowest active | |
4005 register. */ | |
4006 if (lowest_active_reg == NO_LOWEST_ACTIVE_REG) | |
4007 lowest_active_reg = *p; | |
4008 | |
4009 /* Move past the register number and inner group count. */ | |
4010 p += 2; | |
5842 | 4011 just_past_start_mem = p; |
10242
8fa35cc770be
(SET_REGS_MATCHED): Do nothing if set_regs_matched_done is 1.
Richard M. Stallman <rms@gnu.org>
parents:
10148
diff
changeset
|
4012 |
1155 | 4013 break; |
4014 | |
4015 | |
4016 /* The stop_memory opcode represents the end of a group. Its | |
4017 arguments are the same as start_memory's: the register | |
4018 number, and the number of inner groups. */ | |
4019 case stop_memory: | |
4020 DEBUG_PRINT3 ("EXECUTING stop_memory %d (%d):\n", *p, p[1]); | |
4021 | |
4022 /* We need to save the string position the last time we were at | |
4023 this close-group operator in case the group is operated | |
4024 upon by a repetition operator, e.g., with `((a*)*(b*)*)*' | |
4025 against `aba'; then we want to ignore where we are now in | |
4026 the string in case this attempt to match fails. */ | |
4027 old_regend[*p] = REG_MATCH_NULL_STRING_P (reg_info[*p]) | |
4028 ? REG_UNSET (regend[*p]) ? d : regend[*p] | |
4029 : regend[*p]; | |
4030 DEBUG_PRINT2 (" old_regend: %d\n", | |
4031 POINTER_TO_OFFSET (old_regend[*p])); | |
4032 | |
4033 regend[*p] = d; | |
4034 DEBUG_PRINT2 (" regend: %d\n", POINTER_TO_OFFSET (regend[*p])); | |
4035 | |
4036 /* This register isn't active anymore. */ | |
4037 IS_ACTIVE (reg_info[*p]) = 0; | |
10242
8fa35cc770be
(SET_REGS_MATCHED): Do nothing if set_regs_matched_done is 1.
Richard M. Stallman <rms@gnu.org>
parents:
10148
diff
changeset
|
4038 |
8fa35cc770be
(SET_REGS_MATCHED): Do nothing if set_regs_matched_done is 1.
Richard M. Stallman <rms@gnu.org>
parents:
10148
diff
changeset
|
4039 /* Clear this whenever we change the register activity status. */ |
8fa35cc770be
(SET_REGS_MATCHED): Do nothing if set_regs_matched_done is 1.
Richard M. Stallman <rms@gnu.org>
parents:
10148
diff
changeset
|
4040 set_regs_matched_done = 0; |
8fa35cc770be
(SET_REGS_MATCHED): Do nothing if set_regs_matched_done is 1.
Richard M. Stallman <rms@gnu.org>
parents:
10148
diff
changeset
|
4041 |
1155 | 4042 /* If this was the only register active, nothing is active |
4043 anymore. */ | |
4044 if (lowest_active_reg == highest_active_reg) | |
4045 { | |
4046 lowest_active_reg = NO_LOWEST_ACTIVE_REG; | |
4047 highest_active_reg = NO_HIGHEST_ACTIVE_REG; | |
4048 } | |
4049 else | |
4050 { /* We must scan for the new highest active register, since | |
4051 it isn't necessarily one less than now: consider | |
4052 (a(b)c(d(e)f)g). When group 3 ends, after the f), the | |
4053 new highest active register is 1. */ | |
4054 unsigned char r = *p - 1; | |
4055 while (r > 0 && !IS_ACTIVE (reg_info[r])) | |
4056 r--; | |
4057 | |
4058 /* If we end up at register zero, that means that we saved | |
4059 the registers as the result of an `on_failure_jump', not | |
4060 a `start_memory', and we jumped to past the innermost | |
4061 `stop_memory'. For example, in ((.)*) we save | |
4062 registers 1 and 2 as a result of the *, but when we pop | |
4063 back to the second ), we are at the stop_memory 1. | |
4064 Thus, nothing is active. */ | |
4065 if (r == 0) | |
4066 { | |
4067 lowest_active_reg = NO_LOWEST_ACTIVE_REG; | |
4068 highest_active_reg = NO_HIGHEST_ACTIVE_REG; | |
4069 } | |
4070 else | |
4071 highest_active_reg = r; | |
4072 } | |
4073 | |
4074 /* If just failed to match something this time around with a | |
4075 group that's operated on by a repetition operator, try to | |
1637 | 4076 force exit from the ``loop'', and restore the register |
1155 | 4077 information for this group that we had before trying this |
4078 last match. */ | |
4079 if ((!MATCHED_SOMETHING (reg_info[*p]) | |
5842 | 4080 || just_past_start_mem == p - 1) |
1155 | 4081 && (p + 2) < pend) |
4082 { | |
4083 boolean is_a_jump_n = false; | |
4084 | |
4085 p1 = p + 2; | |
4086 mcnt = 0; | |
4087 switch ((re_opcode_t) *p1++) | |
4088 { | |
4089 case jump_n: | |
4090 is_a_jump_n = true; | |
4091 case pop_failure_jump: | |
4092 case maybe_pop_jump: | |
4093 case jump: | |
4094 case dummy_failure_jump: | |
4095 EXTRACT_NUMBER_AND_INCR (mcnt, p1); | |
4096 if (is_a_jump_n) | |
4097 p1 += 2; | |
4098 break; | |
4099 | |
4100 default: | |
4101 /* do nothing */ ; | |
4102 } | |
4103 p1 += mcnt; | |
4104 | |
4105 /* If the next operation is a jump backwards in the pattern | |
4106 to an on_failure_jump right before the start_memory | |
4107 corresponding to this stop_memory, exit from the loop | |
4108 by forcing a failure after pushing on the stack the | |
4109 on_failure_jump's jump in the pattern, and d. */ | |
4110 if (mcnt < 0 && (re_opcode_t) *p1 == on_failure_jump | |
4111 && (re_opcode_t) p1[3] == start_memory && p1[4] == *p) | |
4112 { | |
4113 /* If this group ever matched anything, then restore | |
4114 what its registers were before trying this last | |
4115 failed match, e.g., with `(a*)*b' against `ab' for | |
4116 regstart[1], and, e.g., with `((a*)*(b*)*)*' | |
4117 against `aba' for regend[3]. | |
4118 | |
4119 Also restore the registers for inner groups for, | |
4120 e.g., `((a*)(b*))*' against `aba' (register 3 would | |
4121 otherwise get trashed). */ | |
4122 | |
4123 if (EVER_MATCHED_SOMETHING (reg_info[*p])) | |
4124 { | |
4125 unsigned r; | |
4126 | |
4127 EVER_MATCHED_SOMETHING (reg_info[*p]) = 0; | |
4128 | |
4129 /* Restore this and inner groups' (if any) registers. */ | |
4130 for (r = *p; r < *p + *(p + 1); r++) | |
4131 { | |
4132 regstart[r] = old_regstart[r]; | |
4133 | |
4134 /* xx why this test? */ | |
11330
20a91bd7b59d
(re_match_2_internal): Eliminate cast of ptr to int.
Richard M. Stallman <rms@gnu.org>
parents:
10850
diff
changeset
|
4135 if (old_regend[r] >= regstart[r]) |
1155 | 4136 regend[r] = old_regend[r]; |
4137 } | |
4138 } | |
4139 p1++; | |
4140 EXTRACT_NUMBER_AND_INCR (mcnt, p1); | |
4141 PUSH_FAILURE_POINT (p1 + mcnt, d, -2); | |
4142 | |
4143 goto fail; | |
4144 } | |
4145 } | |
4146 | |
4147 /* Move past the register number and the inner group count. */ | |
4148 p += 2; | |
4149 break; | |
4150 | |
4151 | |
4152 /* \<digit> has been turned into a `duplicate' command which is | |
4153 followed by the numeric value of <digit> as the register number. */ | |
4154 case duplicate: | |
4155 { | |
4156 register const char *d2, *dend2; | |
4157 int regno = *p++; /* Get which register to match against. */ | |
4158 DEBUG_PRINT2 ("EXECUTING duplicate %d.\n", regno); | |
4159 | |
4160 /* Can't back reference a group which we've never matched. */ | |
4161 if (REG_UNSET (regstart[regno]) || REG_UNSET (regend[regno])) | |
4162 goto fail; | |
4163 | |
4164 /* Where in input to try to start matching. */ | |
4165 d2 = regstart[regno]; | |
4166 | |
4167 /* Where to stop matching; if both the place to start and | |
4168 the place to stop matching are in the same string, then | |
4169 set to the place to stop, otherwise, for now have to use | |
4170 the end of the first string. */ | |
4171 | |
4172 dend2 = ((FIRST_STRING_P (regstart[regno]) | |
4173 == FIRST_STRING_P (regend[regno])) | |
4174 ? regend[regno] : end_match_1); | |
4175 for (;;) | |
4176 { | |
4177 /* If necessary, advance to next segment in register | |
4178 contents. */ | |
4179 while (d2 == dend2) | |
4180 { | |
4181 if (dend2 == end_match_2) break; | |
4182 if (dend2 == regend[regno]) break; | |
4183 | |
4184 /* End of string1 => advance to string2. */ | |
4185 d2 = string2; | |
4186 dend2 = regend[regno]; | |
4187 } | |
4188 /* At end of register contents => success */ | |
4189 if (d2 == dend2) break; | |
4190 | |
4191 /* If necessary, advance to next segment in data. */ | |
4192 PREFETCH (); | |
4193 | |
4194 /* How many characters left in this segment to match. */ | |
4195 mcnt = dend - d; | |
4196 | |
4197 /* Want how many consecutive characters we can match in | |
4198 one shot, so, if necessary, adjust the count. */ | |
4199 if (mcnt > dend2 - d2) | |
4200 mcnt = dend2 - d2; | |
4201 | |
4202 /* Compare that many; failure if mismatch, else move | |
4203 past them. */ | |
4204 if (translate | |
4205 ? bcmp_translate (d, d2, mcnt, translate) | |
4206 : bcmp (d, d2, mcnt)) | |
4207 goto fail; | |
4208 d += mcnt, d2 += mcnt; | |
10242
8fa35cc770be
(SET_REGS_MATCHED): Do nothing if set_regs_matched_done is 1.
Richard M. Stallman <rms@gnu.org>
parents:
10148
diff
changeset
|
4209 |
8fa35cc770be
(SET_REGS_MATCHED): Do nothing if set_regs_matched_done is 1.
Richard M. Stallman <rms@gnu.org>
parents:
10148
diff
changeset
|
4210 /* Do this because we've match some characters. */ |
8fa35cc770be
(SET_REGS_MATCHED): Do nothing if set_regs_matched_done is 1.
Richard M. Stallman <rms@gnu.org>
parents:
10148
diff
changeset
|
4211 SET_REGS_MATCHED (); |
1155 | 4212 } |
4213 } | |
4214 break; | |
4215 | |
4216 | |
4217 /* begline matches the empty string at the beginning of the string | |
4218 (unless `not_bol' is set in `bufp'), and, if | |
4219 `newline_anchor' is set, after newlines. */ | |
4220 case begline: | |
4221 DEBUG_PRINT1 ("EXECUTING begline.\n"); | |
4222 | |
1637 | 4223 if (AT_STRINGS_BEG (d)) |
1155 | 4224 { |
4225 if (!bufp->not_bol) break; | |
4226 } | |
4227 else if (d[-1] == '\n' && bufp->newline_anchor) | |
4228 { | |
4229 break; | |
4230 } | |
4231 /* In all other cases, we fail. */ | |
4232 goto fail; | |
4233 | |
4234 | |
4235 /* endline is the dual of begline. */ | |
4236 case endline: | |
4237 DEBUG_PRINT1 ("EXECUTING endline.\n"); | |
4238 | |
1637 | 4239 if (AT_STRINGS_END (d)) |
1155 | 4240 { |
4241 if (!bufp->not_eol) break; | |
4242 } | |
4243 | |
4244 /* We have to ``prefetch'' the next character. */ | |
4245 else if ((d == end1 ? *string2 : *d) == '\n' | |
4246 && bufp->newline_anchor) | |
4247 { | |
4248 break; | |
4249 } | |
4250 goto fail; | |
4251 | |
4252 | |
4253 /* Match at the very beginning of the data. */ | |
4254 case begbuf: | |
4255 DEBUG_PRINT1 ("EXECUTING begbuf.\n"); | |
1637 | 4256 if (AT_STRINGS_BEG (d)) |
1155 | 4257 break; |
4258 goto fail; | |
4259 | |
4260 | |
4261 /* Match at the very end of the data. */ | |
4262 case endbuf: | |
4263 DEBUG_PRINT1 ("EXECUTING endbuf.\n"); | |
1637 | 4264 if (AT_STRINGS_END (d)) |
1155 | 4265 break; |
4266 goto fail; | |
4267 | |
4268 | |
4269 /* on_failure_keep_string_jump is used to optimize `.*\n'. It | |
4270 pushes NULL as the value for the string on the stack. Then | |
4271 `pop_failure_point' will keep the current value for the | |
4272 string, instead of restoring it. To see why, consider | |
4273 matching `foo\nbar' against `.*\n'. The .* matches the foo; | |
4274 then the . fails against the \n. But the next thing we want | |
4275 to do is match the \n against the \n; if we restored the | |
4276 string value, we would be back at the foo. | |
4277 | |
4278 Because this is used only in specific cases, we don't need to | |
4279 check all the things that `on_failure_jump' does, to make | |
4280 sure the right things get saved on the stack. Hence we don't | |
4281 share its code. The only reason to push anything on the | |
4282 stack at all is that otherwise we would have to change | |
4283 `anychar's code to do something besides goto fail in this | |
4284 case; that seems worse than this. */ | |
4285 case on_failure_keep_string_jump: | |
4286 DEBUG_PRINT1 ("EXECUTING on_failure_keep_string_jump"); | |
4287 | |
4288 EXTRACT_NUMBER_AND_INCR (mcnt, p); | |
4289 DEBUG_PRINT3 (" %d (to 0x%x):\n", mcnt, p + mcnt); | |
4290 | |
4291 PUSH_FAILURE_POINT (p + mcnt, NULL, -2); | |
4292 break; | |
4293 | |
4294 | |
4295 /* Uses of on_failure_jump: | |
4296 | |
4297 Each alternative starts with an on_failure_jump that points | |
4298 to the beginning of the next alternative. Each alternative | |
4299 except the last ends with a jump that in effect jumps past | |
4300 the rest of the alternatives. (They really jump to the | |
4301 ending jump of the following alternative, because tensioning | |
4302 these jumps is a hassle.) | |
4303 | |
4304 Repeats start with an on_failure_jump that points past both | |
4305 the repetition text and either the following jump or | |
4306 pop_failure_jump back to this on_failure_jump. */ | |
4307 case on_failure_jump: | |
4308 on_failure: | |
4309 DEBUG_PRINT1 ("EXECUTING on_failure_jump"); | |
4310 | |
4311 EXTRACT_NUMBER_AND_INCR (mcnt, p); | |
4312 DEBUG_PRINT3 (" %d (to 0x%x)", mcnt, p + mcnt); | |
4313 | |
4314 /* If this on_failure_jump comes right before a group (i.e., | |
4315 the original * applied to a group), save the information | |
4316 for that group and all inner ones, so that if we fail back | |
4317 to this point, the group's information will be correct. | |
1637 | 4318 For example, in \(a*\)*\1, we need the preceding group, |
1155 | 4319 and in \(\(a*\)b*\)\2, we need the inner group. */ |
4320 | |
4321 /* We can't use `p' to check ahead because we push | |
4322 a failure point to `p + mcnt' after we do this. */ | |
4323 p1 = p; | |
4324 | |
4325 /* We need to skip no_op's before we look for the | |
4326 start_memory in case this on_failure_jump is happening as | |
4327 the result of a completed succeed_n, as in \(a\)\{1,3\}b\1 | |
4328 against aba. */ | |
4329 while (p1 < pend && (re_opcode_t) *p1 == no_op) | |
4330 p1++; | |
4331 | |
4332 if (p1 < pend && (re_opcode_t) *p1 == start_memory) | |
4333 { | |
4334 /* We have a new highest active register now. This will | |
4335 get reset at the start_memory we are about to get to, | |
4336 but we will have saved all the registers relevant to | |
4337 this repetition op, as described above. */ | |
4338 highest_active_reg = *(p1 + 1) + *(p1 + 2); | |
4339 if (lowest_active_reg == NO_LOWEST_ACTIVE_REG) | |
4340 lowest_active_reg = *(p1 + 1); | |
4341 } | |
4342 | |
4343 DEBUG_PRINT1 (":\n"); | |
4344 PUSH_FAILURE_POINT (p + mcnt, d, -2); | |
4345 break; | |
4346 | |
4347 | |
1637 | 4348 /* A smart repeat ends with `maybe_pop_jump'. |
4349 We change it to either `pop_failure_jump' or `jump'. */ | |
1155 | 4350 case maybe_pop_jump: |
4351 EXTRACT_NUMBER_AND_INCR (mcnt, p); | |
4352 DEBUG_PRINT2 ("EXECUTING maybe_pop_jump %d.\n", mcnt); | |
4353 { | |
4354 register unsigned char *p2 = p; | |
4355 | |
4356 /* Compare the beginning of the repeat with what in the | |
4357 pattern follows its end. If we can establish that there | |
4358 is nothing that they would both match, i.e., that we | |
4359 would have to backtrack because of (as in, e.g., `a*a') | |
4360 then we can change to pop_failure_jump, because we'll | |
4361 never have to backtrack. | |
4362 | |
4363 This is not true in the case of alternatives: in | |
4364 `(a|ab)*' we do need to backtrack to the `ab' alternative | |
4365 (e.g., if the string was `ab'). But instead of trying to | |
4366 detect that here, the alternative has put on a dummy | |
4367 failure point which is what we will end up popping. */ | |
4368 | |
3541
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4369 /* Skip over open/close-group commands. |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4370 If what follows this loop is a ...+ construct, |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4371 look at what begins its body, since we will have to |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4372 match at least one of that. */ |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4373 while (1) |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4374 { |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4375 if (p2 + 2 < pend |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4376 && ((re_opcode_t) *p2 == stop_memory |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4377 || (re_opcode_t) *p2 == start_memory)) |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4378 p2 += 3; |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4379 else if (p2 + 6 < pend |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4380 && (re_opcode_t) *p2 == dummy_failure_jump) |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4381 p2 += 6; |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4382 else |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4383 break; |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4384 } |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4385 |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4386 p1 = p + mcnt; |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4387 /* p1[0] ... p1[2] are the `on_failure_jump' corresponding |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4388 to the `maybe_finalize_jump' of this case. Examine what |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4389 follows. */ |
1155 | 4390 |
4391 /* If we're at the end of the pattern, we can change. */ | |
4392 if (p2 == pend) | |
1669 | 4393 { |
4394 /* Consider what happens when matching ":\(.*\)" | |
4395 against ":/". I don't really understand this code | |
4396 yet. */ | |
1155 | 4397 p[-3] = (unsigned char) pop_failure_jump; |
1669 | 4398 DEBUG_PRINT1 |
4399 (" End of pattern: change to `pop_failure_jump'.\n"); | |
1155 | 4400 } |
4401 | |
4402 else if ((re_opcode_t) *p2 == exactn | |
4403 || (bufp->newline_anchor && (re_opcode_t) *p2 == endline)) | |
4404 { | |
4405 register unsigned char c | |
4406 = *p2 == (unsigned char) endline ? '\n' : p2[2]; | |
3541
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4407 |
1155 | 4408 if ((re_opcode_t) p1[3] == exactn && p1[5] != c) |
1637 | 4409 { |
4410 p[-3] = (unsigned char) pop_failure_jump; | |
4411 DEBUG_PRINT3 (" %c != %c => pop_failure_jump.\n", | |
4412 c, p1[5]); | |
4413 } | |
4414 | |
1155 | 4415 else if ((re_opcode_t) p1[3] == charset |
4416 || (re_opcode_t) p1[3] == charset_not) | |
4417 { | |
4418 int not = (re_opcode_t) p1[3] == charset_not; | |
4419 | |
4420 if (c < (unsigned char) (p1[4] * BYTEWIDTH) | |
4421 && p1[5 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH))) | |
4422 not = !not; | |
4423 | |
4424 /* `not' is equal to 1 if c would match, which means | |
4425 that we can't change to pop_failure_jump. */ | |
4426 if (!not) | |
4427 { | |
4428 p[-3] = (unsigned char) pop_failure_jump; | |
1637 | 4429 DEBUG_PRINT1 (" No match => pop_failure_jump.\n"); |
1155 | 4430 } |
4431 } | |
4432 } | |
3541
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4433 else if ((re_opcode_t) *p2 == charset) |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4434 { |
7358 | 4435 #ifdef DEBUG |
3541
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4436 register unsigned char c |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4437 = *p2 == (unsigned char) endline ? '\n' : p2[2]; |
7358 | 4438 #endif |
3541
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4439 |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4440 if ((re_opcode_t) p1[3] == exactn |
8254
694c4686b446
(re_match_2_internal): Add casts to shut up some compilers.
Richard M. Stallman <rms@gnu.org>
parents:
8142
diff
changeset
|
4441 && ! ((int) p2[1] * BYTEWIDTH > (int) p1[4] |
3541
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4442 && (p2[1 + p1[4] / BYTEWIDTH] |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4443 & (1 << (p1[4] % BYTEWIDTH))))) |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4444 { |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4445 p[-3] = (unsigned char) pop_failure_jump; |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4446 DEBUG_PRINT3 (" %c != %c => pop_failure_jump.\n", |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4447 c, p1[5]); |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4448 } |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4449 |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4450 else if ((re_opcode_t) p1[3] == charset_not) |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4451 { |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4452 int idx; |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4453 /* We win if the charset_not inside the loop |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4454 lists every character listed in the charset after. */ |
8254
694c4686b446
(re_match_2_internal): Add casts to shut up some compilers.
Richard M. Stallman <rms@gnu.org>
parents:
8142
diff
changeset
|
4455 for (idx = 0; idx < (int) p2[1]; idx++) |
3541
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4456 if (! (p2[2 + idx] == 0 |
8254
694c4686b446
(re_match_2_internal): Add casts to shut up some compilers.
Richard M. Stallman <rms@gnu.org>
parents:
8142
diff
changeset
|
4457 || (idx < (int) p1[4] |
3541
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4458 && ((p2[2 + idx] & ~ p1[5 + idx]) == 0)))) |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4459 break; |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4460 |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4461 if (idx == p2[1]) |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4462 { |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4463 p[-3] = (unsigned char) pop_failure_jump; |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4464 DEBUG_PRINT1 (" No match => pop_failure_jump.\n"); |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4465 } |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4466 } |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4467 else if ((re_opcode_t) p1[3] == charset) |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4468 { |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4469 int idx; |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4470 /* We win if the charset inside the loop |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4471 has no overlap with the one after the loop. */ |
8254
694c4686b446
(re_match_2_internal): Add casts to shut up some compilers.
Richard M. Stallman <rms@gnu.org>
parents:
8142
diff
changeset
|
4472 for (idx = 0; |
694c4686b446
(re_match_2_internal): Add casts to shut up some compilers.
Richard M. Stallman <rms@gnu.org>
parents:
8142
diff
changeset
|
4473 idx < (int) p2[1] && idx < (int) p1[4]; |
694c4686b446
(re_match_2_internal): Add casts to shut up some compilers.
Richard M. Stallman <rms@gnu.org>
parents:
8142
diff
changeset
|
4474 idx++) |
3541
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4475 if ((p2[2 + idx] & p1[5 + idx]) != 0) |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4476 break; |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4477 |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4478 if (idx == p2[1] || idx == p1[4]) |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4479 { |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4480 p[-3] = (unsigned char) pop_failure_jump; |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4481 DEBUG_PRINT1 (" No match => pop_failure_jump.\n"); |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4482 } |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4483 } |
cb4aa2f13edd
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
2952
diff
changeset
|
4484 } |
1155 | 4485 } |
4486 p -= 2; /* Point at relative address again. */ | |
4487 if ((re_opcode_t) p[-1] != pop_failure_jump) | |
4488 { | |
4489 p[-1] = (unsigned char) jump; | |
1637 | 4490 DEBUG_PRINT1 (" Match => jump.\n"); |
1155 | 4491 goto unconditional_jump; |
4492 } | |
4493 /* Note fall through. */ | |
4494 | |
4495 | |
4496 /* The end of a simple repeat has a pop_failure_jump back to | |
4497 its matching on_failure_jump, where the latter will push a | |
4498 failure point. The pop_failure_jump takes off failure | |
4499 points put on by this pop_failure_jump's matching | |
4500 on_failure_jump; we got through the pattern to here from the | |
4501 matching on_failure_jump, so didn't fail. */ | |
4502 case pop_failure_jump: | |
4503 { | |
4504 /* We need to pass separate storage for the lowest and | |
4505 highest registers, even though we don't care about the | |
4506 actual values. Otherwise, we will restore only one | |
4507 register from the stack, since lowest will == highest in | |
4508 `pop_failure_point'. */ | |
4509 unsigned dummy_low_reg, dummy_high_reg; | |
4510 unsigned char *pdummy; | |
4511 const char *sdummy; | |
4512 | |
4513 DEBUG_PRINT1 ("EXECUTING pop_failure_jump.\n"); | |
4514 POP_FAILURE_POINT (sdummy, pdummy, | |
4515 dummy_low_reg, dummy_high_reg, | |
4516 reg_dummy, reg_dummy, reg_info_dummy); | |
4517 } | |
4518 /* Note fall through. */ | |
4519 | |
4520 | |
4521 /* Unconditionally jump (without popping any failure points). */ | |
4522 case jump: | |
4523 unconditional_jump: | |
4524 EXTRACT_NUMBER_AND_INCR (mcnt, p); /* Get the amount to jump. */ | |
4525 DEBUG_PRINT2 ("EXECUTING jump %d ", mcnt); | |
4526 p += mcnt; /* Do the jump. */ | |
4527 DEBUG_PRINT2 ("(to 0x%x).\n", p); | |
4528 break; | |
4529 | |
4530 | |
4531 /* We need this opcode so we can detect where alternatives end | |
4532 in `group_match_null_string_p' et al. */ | |
4533 case jump_past_alt: | |
4534 DEBUG_PRINT1 ("EXECUTING jump_past_alt.\n"); | |
4535 goto unconditional_jump; | |
4536 | |
4537 | |
4538 /* Normally, the on_failure_jump pushes a failure point, which | |
4539 then gets popped at pop_failure_jump. We will end up at | |
4540 pop_failure_jump, also, and with a pattern of, say, `a+', we | |
4541 are skipping over the on_failure_jump, so we have to push | |
4542 something meaningless for pop_failure_jump to pop. */ | |
4543 case dummy_failure_jump: | |
4544 DEBUG_PRINT1 ("EXECUTING dummy_failure_jump.\n"); | |
4545 /* It doesn't matter what we push for the string here. What | |
4546 the code at `fail' tests is the value for the pattern. */ | |
4547 PUSH_FAILURE_POINT (0, 0, -2); | |
4548 goto unconditional_jump; | |
4549 | |
4550 | |
4551 /* At the end of an alternative, we need to push a dummy failure | |
1637 | 4552 point in case we are followed by a `pop_failure_jump', because |
1155 | 4553 we don't want the failure point for the alternative to be |
4554 popped. For example, matching `(a|ab)*' against `aab' | |
4555 requires that we match the `ab' alternative. */ | |
4556 case push_dummy_failure: | |
4557 DEBUG_PRINT1 ("EXECUTING push_dummy_failure.\n"); | |
4558 /* See comments just above at `dummy_failure_jump' about the | |
4559 two zeroes. */ | |
4560 PUSH_FAILURE_POINT (0, 0, -2); | |
4561 break; | |
4562 | |
4563 /* Have to succeed matching what follows at least n times. | |
4564 After that, handle like `on_failure_jump'. */ | |
4565 case succeed_n: | |
4566 EXTRACT_NUMBER (mcnt, p + 2); | |
4567 DEBUG_PRINT2 ("EXECUTING succeed_n %d.\n", mcnt); | |
4568 | |
4569 assert (mcnt >= 0); | |
4570 /* Originally, this is how many times we HAVE to succeed. */ | |
4571 if (mcnt > 0) | |
4572 { | |
4573 mcnt--; | |
4574 p += 2; | |
4575 STORE_NUMBER_AND_INCR (p, mcnt); | |
4576 DEBUG_PRINT3 (" Setting 0x%x to %d.\n", p, mcnt); | |
4577 } | |
4578 else if (mcnt == 0) | |
4579 { | |
4580 DEBUG_PRINT2 (" Setting two bytes from 0x%x to no_op.\n", p+2); | |
4581 p[2] = (unsigned char) no_op; | |
4582 p[3] = (unsigned char) no_op; | |
4583 goto on_failure; | |
4584 } | |
4585 break; | |
4586 | |
4587 case jump_n: | |
4588 EXTRACT_NUMBER (mcnt, p + 2); | |
4589 DEBUG_PRINT2 ("EXECUTING jump_n %d.\n", mcnt); | |
4590 | |
4591 /* Originally, this is how many times we CAN jump. */ | |
4592 if (mcnt) | |
4593 { | |
4594 mcnt--; | |
4595 STORE_NUMBER (p + 2, mcnt); | |
4596 goto unconditional_jump; | |
4597 } | |
4598 /* If don't have to jump any more, skip over the rest of command. */ | |
4599 else | |
4600 p += 4; | |
4601 break; | |
4602 | |
4603 case set_number_at: | |
4604 { | |
4605 DEBUG_PRINT1 ("EXECUTING set_number_at.\n"); | |
4606 | |
4607 EXTRACT_NUMBER_AND_INCR (mcnt, p); | |
4608 p1 = p + mcnt; | |
4609 EXTRACT_NUMBER_AND_INCR (mcnt, p); | |
4610 DEBUG_PRINT3 (" Setting 0x%x to %d.\n", p1, mcnt); | |
4611 STORE_NUMBER (p1, mcnt); | |
4612 break; | |
4613 } | |
4614 | |
4615 case wordbound: | |
4616 DEBUG_PRINT1 ("EXECUTING wordbound.\n"); | |
4617 if (AT_WORD_BOUNDARY (d)) | |
4618 break; | |
4619 goto fail; | |
4620 | |
4621 case notwordbound: | |
4622 DEBUG_PRINT1 ("EXECUTING notwordbound.\n"); | |
4623 if (AT_WORD_BOUNDARY (d)) | |
4624 goto fail; | |
4625 break; | |
4626 | |
4627 case wordbeg: | |
4628 DEBUG_PRINT1 ("EXECUTING wordbeg.\n"); | |
1637 | 4629 if (WORDCHAR_P (d) && (AT_STRINGS_BEG (d) || !WORDCHAR_P (d - 1))) |
1155 | 4630 break; |
4631 goto fail; | |
4632 | |
4633 case wordend: | |
4634 DEBUG_PRINT1 ("EXECUTING wordend.\n"); | |
1637 | 4635 if (!AT_STRINGS_BEG (d) && WORDCHAR_P (d - 1) |
4636 && (!WORDCHAR_P (d) || AT_STRINGS_END (d))) | |
1155 | 4637 break; |
4638 goto fail; | |
4639 | |
4640 #ifdef emacs | |
4641 case before_dot: | |
4642 DEBUG_PRINT1 ("EXECUTING before_dot.\n"); | |
4643 if (PTR_CHAR_POS ((unsigned char *) d) >= point) | |
4644 goto fail; | |
4645 break; | |
4646 | |
4647 case at_dot: | |
4648 DEBUG_PRINT1 ("EXECUTING at_dot.\n"); | |
4649 if (PTR_CHAR_POS ((unsigned char *) d) != point) | |
4650 goto fail; | |
4651 break; | |
4652 | |
4653 case after_dot: | |
4654 DEBUG_PRINT1 ("EXECUTING after_dot.\n"); | |
4655 if (PTR_CHAR_POS ((unsigned char *) d) <= point) | |
4656 goto fail; | |
4657 break; | |
6538
815a2d384b6e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6202
diff
changeset
|
4658 #if 0 /* not emacs19 */ |
1155 | 4659 case at_dot: |
4660 DEBUG_PRINT1 ("EXECUTING at_dot.\n"); | |
4661 if (PTR_CHAR_POS ((unsigned char *) d) + 1 != point) | |
4662 goto fail; | |
4663 break; | |
4664 #endif /* not emacs19 */ | |
4665 | |
4666 case syntaxspec: | |
4667 DEBUG_PRINT2 ("EXECUTING syntaxspec %d.\n", mcnt); | |
4668 mcnt = *p++; | |
4669 goto matchsyntax; | |
4670 | |
4671 case wordchar: | |
1637 | 4672 DEBUG_PRINT1 ("EXECUTING Emacs wordchar.\n"); |
1155 | 4673 mcnt = (int) Sword; |
4674 matchsyntax: | |
4675 PREFETCH (); | |
6047 | 4676 /* Can't use *d++ here; SYNTAX may be an unsafe macro. */ |
4677 d++; | |
4678 if (SYNTAX (d[-1]) != (enum syntaxcode) mcnt) | |
4679 goto fail; | |
1155 | 4680 SET_REGS_MATCHED (); |
4681 break; | |
4682 | |
4683 case notsyntaxspec: | |
4684 DEBUG_PRINT2 ("EXECUTING notsyntaxspec %d.\n", mcnt); | |
4685 mcnt = *p++; | |
4686 goto matchnotsyntax; | |
4687 | |
4688 case notwordchar: | |
1637 | 4689 DEBUG_PRINT1 ("EXECUTING Emacs notwordchar.\n"); |
1155 | 4690 mcnt = (int) Sword; |
1637 | 4691 matchnotsyntax: |
1155 | 4692 PREFETCH (); |
6047 | 4693 /* Can't use *d++ here; SYNTAX may be an unsafe macro. */ |
4694 d++; | |
4695 if (SYNTAX (d[-1]) == (enum syntaxcode) mcnt) | |
4696 goto fail; | |
1155 | 4697 SET_REGS_MATCHED (); |
4698 break; | |
4699 | |
4700 #else /* not emacs */ | |
4701 case wordchar: | |
4702 DEBUG_PRINT1 ("EXECUTING non-Emacs wordchar.\n"); | |
4703 PREFETCH (); | |
1637 | 4704 if (!WORDCHAR_P (d)) |
1155 | 4705 goto fail; |
4706 SET_REGS_MATCHED (); | |
1637 | 4707 d++; |
1155 | 4708 break; |
4709 | |
4710 case notwordchar: | |
4711 DEBUG_PRINT1 ("EXECUTING non-Emacs notwordchar.\n"); | |
4712 PREFETCH (); | |
1637 | 4713 if (WORDCHAR_P (d)) |
1155 | 4714 goto fail; |
4715 SET_REGS_MATCHED (); | |
1637 | 4716 d++; |
1155 | 4717 break; |
4718 #endif /* not emacs */ | |
4719 | |
4720 default: | |
4721 abort (); | |
4722 } | |
4723 continue; /* Successfully executed one pattern command; keep going. */ | |
4724 | |
4725 | |
4726 /* We goto here if a matching operation fails. */ | |
4727 fail: | |
4728 if (!FAIL_STACK_EMPTY ()) | |
4729 { /* A restart point is known. Restore to that state. */ | |
4730 DEBUG_PRINT1 ("\nFAIL:\n"); | |
4731 POP_FAILURE_POINT (d, p, | |
4732 lowest_active_reg, highest_active_reg, | |
4733 regstart, regend, reg_info); | |
4734 | |
4735 /* If this failure point is a dummy, try the next one. */ | |
4736 if (!p) | |
4737 goto fail; | |
4738 | |
4739 /* If we failed to the end of the pattern, don't examine *p. */ | |
4740 assert (p <= pend); | |
4741 if (p < pend) | |
4742 { | |
4743 boolean is_a_jump_n = false; | |
4744 | |
4745 /* If failed to a backwards jump that's part of a repetition | |
4746 loop, need to pop this failure point and use the next one. */ | |
4747 switch ((re_opcode_t) *p) | |
4748 { | |
4749 case jump_n: | |
4750 is_a_jump_n = true; | |
4751 case maybe_pop_jump: | |
4752 case pop_failure_jump: | |
4753 case jump: | |
4754 p1 = p + 1; | |
4755 EXTRACT_NUMBER_AND_INCR (mcnt, p1); | |
4756 p1 += mcnt; | |
4757 | |
4758 if ((is_a_jump_n && (re_opcode_t) *p1 == succeed_n) | |
4759 || (!is_a_jump_n | |
4760 && (re_opcode_t) *p1 == on_failure_jump)) | |
4761 goto fail; | |
4762 break; | |
4763 default: | |
4764 /* do nothing */ ; | |
4765 } | |
4766 } | |
4767 | |
4768 if (d >= string1 && d <= end1) | |
4769 dend = end_match_1; | |
4770 } | |
4771 else | |
4772 break; /* Matching at this starting point really fails. */ | |
4773 } /* for (;;) */ | |
4774 | |
4775 if (best_regs_set) | |
4776 goto restore_best_regs; | |
4777 | |
4778 FREE_VARIABLES (); | |
4779 | |
4780 return -1; /* Failure to match. */ | |
4781 } /* re_match_2 */ | |
4782 | |
4783 /* Subroutine definitions for re_match_2. */ | |
4784 | |
4785 | |
4786 /* We are passed P pointing to a register number after a start_memory. | |
4787 | |
4788 Return true if the pattern up to the corresponding stop_memory can | |
4789 match the empty string, and false otherwise. | |
4790 | |
4791 If we find the matching stop_memory, sets P to point to one past its number. | |
4792 Otherwise, sets P to an undefined byte less than or equal to END. | |
4793 | |
4794 We don't handle duplicates properly (yet). */ | |
4795 | |
4796 static boolean | |
4797 group_match_null_string_p (p, end, reg_info) | |
4798 unsigned char **p, *end; | |
4799 register_info_type *reg_info; | |
4800 { | |
4801 int mcnt; | |
4802 /* Point to after the args to the start_memory. */ | |
4803 unsigned char *p1 = *p + 2; | |
4804 | |
4805 while (p1 < end) | |
4806 { | |
4807 /* Skip over opcodes that can match nothing, and return true or | |
4808 false, as appropriate, when we get to one that can't, or to the | |
4809 matching stop_memory. */ | |
4810 | |
4811 switch ((re_opcode_t) *p1) | |
4812 { | |
4813 /* Could be either a loop or a series of alternatives. */ | |
4814 case on_failure_jump: | |
4815 p1++; | |
4816 EXTRACT_NUMBER_AND_INCR (mcnt, p1); | |
4817 | |
4818 /* If the next operation is not a jump backwards in the | |
4819 pattern. */ | |
4820 | |
4821 if (mcnt >= 0) | |
4822 { | |
4823 /* Go through the on_failure_jumps of the alternatives, | |
4824 seeing if any of the alternatives cannot match nothing. | |
4825 The last alternative starts with only a jump, | |
4826 whereas the rest start with on_failure_jump and end | |
4827 with a jump, e.g., here is the pattern for `a|b|c': | |
4828 | |
4829 /on_failure_jump/0/6/exactn/1/a/jump_past_alt/0/6 | |
4830 /on_failure_jump/0/6/exactn/1/b/jump_past_alt/0/3 | |
4831 /exactn/1/c | |
4832 | |
4833 So, we have to first go through the first (n-1) | |
4834 alternatives and then deal with the last one separately. */ | |
4835 | |
4836 | |
4837 /* Deal with the first (n-1) alternatives, which start | |
4838 with an on_failure_jump (see above) that jumps to right | |
4839 past a jump_past_alt. */ | |
4840 | |
4841 while ((re_opcode_t) p1[mcnt-3] == jump_past_alt) | |
4842 { | |
4843 /* `mcnt' holds how many bytes long the alternative | |
4844 is, including the ending `jump_past_alt' and | |
4845 its number. */ | |
4846 | |
4847 if (!alt_match_null_string_p (p1, p1 + mcnt - 3, | |
4848 reg_info)) | |
4849 return false; | |
4850 | |
4851 /* Move to right after this alternative, including the | |
4852 jump_past_alt. */ | |
4853 p1 += mcnt; | |
4854 | |
4855 /* Break if it's the beginning of an n-th alternative | |
4856 that doesn't begin with an on_failure_jump. */ | |
4857 if ((re_opcode_t) *p1 != on_failure_jump) | |
4858 break; | |
4859 | |
4860 /* Still have to check that it's not an n-th | |
4861 alternative that starts with an on_failure_jump. */ | |
4862 p1++; | |
4863 EXTRACT_NUMBER_AND_INCR (mcnt, p1); | |
4864 if ((re_opcode_t) p1[mcnt-3] != jump_past_alt) | |
4865 { | |
4866 /* Get to the beginning of the n-th alternative. */ | |
4867 p1 -= 3; | |
4868 break; | |
4869 } | |
4870 } | |
4871 | |
4872 /* Deal with the last alternative: go back and get number | |
4873 of the `jump_past_alt' just before it. `mcnt' contains | |
4874 the length of the alternative. */ | |
4875 EXTRACT_NUMBER (mcnt, p1 - 2); | |
4876 | |
4877 if (!alt_match_null_string_p (p1, p1 + mcnt, reg_info)) | |
4878 return false; | |
4879 | |
4880 p1 += mcnt; /* Get past the n-th alternative. */ | |
4881 } /* if mcnt > 0 */ | |
4882 break; | |
4883 | |
4884 | |
4885 case stop_memory: | |
4886 assert (p1[1] == **p); | |
4887 *p = p1 + 2; | |
4888 return true; | |
4889 | |
4890 | |
4891 default: | |
4892 if (!common_op_match_null_string_p (&p1, end, reg_info)) | |
4893 return false; | |
4894 } | |
4895 } /* while p1 < end */ | |
4896 | |
4897 return false; | |
4898 } /* group_match_null_string_p */ | |
4899 | |
4900 | |
4901 /* Similar to group_match_null_string_p, but doesn't deal with alternatives: | |
4902 It expects P to be the first byte of a single alternative and END one | |
4903 byte past the last. The alternative can contain groups. */ | |
4904 | |
4905 static boolean | |
4906 alt_match_null_string_p (p, end, reg_info) | |
4907 unsigned char *p, *end; | |
4908 register_info_type *reg_info; | |
4909 { | |
4910 int mcnt; | |
4911 unsigned char *p1 = p; | |
4912 | |
4913 while (p1 < end) | |
4914 { | |
4915 /* Skip over opcodes that can match nothing, and break when we get | |
4916 to one that can't. */ | |
4917 | |
4918 switch ((re_opcode_t) *p1) | |
4919 { | |
4920 /* It's a loop. */ | |
4921 case on_failure_jump: | |
4922 p1++; | |
4923 EXTRACT_NUMBER_AND_INCR (mcnt, p1); | |
4924 p1 += mcnt; | |
4925 break; | |
4926 | |
4927 default: | |
4928 if (!common_op_match_null_string_p (&p1, end, reg_info)) | |
4929 return false; | |
4930 } | |
4931 } /* while p1 < end */ | |
4932 | |
4933 return true; | |
4934 } /* alt_match_null_string_p */ | |
4935 | |
4936 | |
4937 /* Deals with the ops common to group_match_null_string_p and | |
4938 alt_match_null_string_p. | |
4939 | |
4940 Sets P to one after the op and its arguments, if any. */ | |
4941 | |
4942 static boolean | |
4943 common_op_match_null_string_p (p, end, reg_info) | |
4944 unsigned char **p, *end; | |
4945 register_info_type *reg_info; | |
4946 { | |
4947 int mcnt; | |
4948 boolean ret; | |
4949 int reg_no; | |
4950 unsigned char *p1 = *p; | |
4951 | |
4952 switch ((re_opcode_t) *p1++) | |
4953 { | |
4954 case no_op: | |
4955 case begline: | |
4956 case endline: | |
4957 case begbuf: | |
4958 case endbuf: | |
4959 case wordbeg: | |
4960 case wordend: | |
4961 case wordbound: | |
4962 case notwordbound: | |
4963 #ifdef emacs | |
4964 case before_dot: | |
4965 case at_dot: | |
4966 case after_dot: | |
4967 #endif | |
4968 break; | |
4969 | |
4970 case start_memory: | |
4971 reg_no = *p1; | |
4972 assert (reg_no > 0 && reg_no <= MAX_REGNUM); | |
4973 ret = group_match_null_string_p (&p1, end, reg_info); | |
4974 | |
4975 /* Have to set this here in case we're checking a group which | |
4976 contains a group and a back reference to it. */ | |
4977 | |
4978 if (REG_MATCH_NULL_STRING_P (reg_info[reg_no]) == MATCH_NULL_UNSET_VALUE) | |
4979 REG_MATCH_NULL_STRING_P (reg_info[reg_no]) = ret; | |
4980 | |
4981 if (!ret) | |
4982 return false; | |
4983 break; | |
4984 | |
4985 /* If this is an optimized succeed_n for zero times, make the jump. */ | |
4986 case jump: | |
4987 EXTRACT_NUMBER_AND_INCR (mcnt, p1); | |
4988 if (mcnt >= 0) | |
4989 p1 += mcnt; | |
4990 else | |
4991 return false; | |
4992 break; | |
4993 | |
4994 case succeed_n: | |
4995 /* Get to the number of times to succeed. */ | |
4996 p1 += 2; | |
4997 EXTRACT_NUMBER_AND_INCR (mcnt, p1); | |
4998 | |
4999 if (mcnt == 0) | |
5000 { | |
5001 p1 -= 4; | |
5002 EXTRACT_NUMBER_AND_INCR (mcnt, p1); | |
5003 p1 += mcnt; | |
5004 } | |
5005 else | |
5006 return false; | |
5007 break; | |
5008 | |
5009 case duplicate: | |
5010 if (!REG_MATCH_NULL_STRING_P (reg_info[*p1])) | |
5011 return false; | |
5012 break; | |
5013 | |
5014 case set_number_at: | |
5015 p1 += 4; | |
5016 | |
5017 default: | |
5018 /* All other opcodes mean we cannot match the empty string. */ | |
5019 return false; | |
5020 } | |
5021 | |
5022 *p = p1; | |
5023 return true; | |
5024 } /* common_op_match_null_string_p */ | |
5025 | |
5026 | |
5027 /* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN | |
5028 bytes; nonzero otherwise. */ | |
5029 | |
5030 static int | |
5031 bcmp_translate (s1, s2, len, translate) | |
5032 unsigned char *s1, *s2; | |
5033 register int len; | |
5034 char *translate; | |
5035 { | |
5036 register unsigned char *p1 = s1, *p2 = s2; | |
5037 while (len) | |
5038 { | |
5039 if (translate[*p1++] != translate[*p2++]) return 1; | |
5040 len--; | |
5041 } | |
5042 return 0; | |
5043 } | |
5044 | |
5045 /* Entry points for GNU code. */ | |
5046 | |
5047 /* re_compile_pattern is the GNU regular expression compiler: it | |
5048 compiles PATTERN (of length SIZE) and puts the result in BUFP. | |
5049 Returns 0 if the pattern was valid, otherwise an error string. | |
5050 | |
5051 Assumes the `allocated' (and perhaps `buffer') and `translate' fields | |
5052 are set in BUFP on entry. | |
5053 | |
5054 We call regex_compile to do the actual compilation. */ | |
5055 | |
5056 const char * | |
5057 re_compile_pattern (pattern, length, bufp) | |
5058 const char *pattern; | |
5059 int length; | |
5060 struct re_pattern_buffer *bufp; | |
5061 { | |
5062 reg_errcode_t ret; | |
5063 | |
5064 /* GNU code is written to assume at least RE_NREGS registers will be set | |
5065 (and at least one extra will be -1). */ | |
5066 bufp->regs_allocated = REGS_UNALLOCATED; | |
5067 | |
5068 /* And GNU code determines whether or not to get register information | |
5069 by passing null for the REGS argument to re_match, etc., not by | |
5070 setting no_sub. */ | |
5071 bufp->no_sub = 0; | |
5072 | |
5073 /* Match anchors at newline. */ | |
5074 bufp->newline_anchor = 1; | |
5075 | |
5076 ret = regex_compile (pattern, length, re_syntax_options, bufp); | |
5077 | |
10090
672e07670453
(re_compile_pattern, re_comp, regerror): Add gettext wrapper.
Paul Eggert <eggert@twinsun.com>
parents:
10023
diff
changeset
|
5078 if (!ret) |
672e07670453
(re_compile_pattern, re_comp, regerror): Add gettext wrapper.
Paul Eggert <eggert@twinsun.com>
parents:
10023
diff
changeset
|
5079 return NULL; |
672e07670453
(re_compile_pattern, re_comp, regerror): Add gettext wrapper.
Paul Eggert <eggert@twinsun.com>
parents:
10023
diff
changeset
|
5080 return gettext (re_error_msgid[(int) ret]); |
1155 | 5081 } |
5082 | |
5083 /* Entry points compatible with 4.2 BSD regex library. We don't define | |
9717
96a592ccb751
(re_comp, re_exec): Define this obsolete 4.2bsd
Paul Eggert <eggert@twinsun.com>
parents:
9585
diff
changeset
|
5084 them unless specifically requested. */ |
96a592ccb751
(re_comp, re_exec): Define this obsolete 4.2bsd
Paul Eggert <eggert@twinsun.com>
parents:
9585
diff
changeset
|
5085 |
96a592ccb751
(re_comp, re_exec): Define this obsolete 4.2bsd
Paul Eggert <eggert@twinsun.com>
parents:
9585
diff
changeset
|
5086 #ifdef _REGEX_RE_COMP |
1155 | 5087 |
5088 /* BSD has one and only one pattern buffer. */ | |
5089 static struct re_pattern_buffer re_comp_buf; | |
5090 | |
5091 char * | |
5092 re_comp (s) | |
5093 const char *s; | |
5094 { | |
5095 reg_errcode_t ret; | |
5096 | |
5097 if (!s) | |
5098 { | |
5099 if (!re_comp_buf.buffer) | |
10090
672e07670453
(re_compile_pattern, re_comp, regerror): Add gettext wrapper.
Paul Eggert <eggert@twinsun.com>
parents:
10023
diff
changeset
|
5100 return gettext ("No previous regular expression"); |
1155 | 5101 return 0; |
5102 } | |
5103 | |
5104 if (!re_comp_buf.buffer) | |
5105 { | |
5106 re_comp_buf.buffer = (unsigned char *) malloc (200); | |
5107 if (re_comp_buf.buffer == NULL) | |
10090
672e07670453
(re_compile_pattern, re_comp, regerror): Add gettext wrapper.
Paul Eggert <eggert@twinsun.com>
parents:
10023
diff
changeset
|
5108 return gettext (re_error_msgid[(int) REG_ESPACE]); |
1155 | 5109 re_comp_buf.allocated = 200; |
5110 | |
5111 re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH); | |
5112 if (re_comp_buf.fastmap == NULL) | |
10090
672e07670453
(re_compile_pattern, re_comp, regerror): Add gettext wrapper.
Paul Eggert <eggert@twinsun.com>
parents:
10023
diff
changeset
|
5113 return gettext (re_error_msgid[(int) REG_ESPACE]); |
1155 | 5114 } |
5115 | |
5116 /* Since `re_exec' always passes NULL for the `regs' argument, we | |
5117 don't need to initialize the pattern buffer fields which affect it. */ | |
5118 | |
5119 /* Match anchors at newlines. */ | |
5120 re_comp_buf.newline_anchor = 1; | |
5121 | |
5122 ret = regex_compile (s, strlen (s), re_syntax_options, &re_comp_buf); | |
5123 | |
10090
672e07670453
(re_compile_pattern, re_comp, regerror): Add gettext wrapper.
Paul Eggert <eggert@twinsun.com>
parents:
10023
diff
changeset
|
5124 if (!ret) |
672e07670453
(re_compile_pattern, re_comp, regerror): Add gettext wrapper.
Paul Eggert <eggert@twinsun.com>
parents:
10023
diff
changeset
|
5125 return NULL; |
672e07670453
(re_compile_pattern, re_comp, regerror): Add gettext wrapper.
Paul Eggert <eggert@twinsun.com>
parents:
10023
diff
changeset
|
5126 |
672e07670453
(re_compile_pattern, re_comp, regerror): Add gettext wrapper.
Paul Eggert <eggert@twinsun.com>
parents:
10023
diff
changeset
|
5127 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */ |
672e07670453
(re_compile_pattern, re_comp, regerror): Add gettext wrapper.
Paul Eggert <eggert@twinsun.com>
parents:
10023
diff
changeset
|
5128 return (char *) gettext (re_error_msgid[(int) ret]); |
1155 | 5129 } |
5130 | |
5131 | |
5132 int | |
5133 re_exec (s) | |
5134 const char *s; | |
5135 { | |
5136 const int len = strlen (s); | |
5137 return | |
5138 0 <= re_search (&re_comp_buf, s, len, 0, len, (struct re_registers *) 0); | |
5139 } | |
9717
96a592ccb751
(re_comp, re_exec): Define this obsolete 4.2bsd
Paul Eggert <eggert@twinsun.com>
parents:
9585
diff
changeset
|
5140 #endif /* _REGEX_RE_COMP */ |
1155 | 5141 |
5142 /* POSIX.2 functions. Don't define these for Emacs. */ | |
5143 | |
5144 #ifndef emacs | |
5145 | |
5146 /* regcomp takes a regular expression as a string and compiles it. | |
5147 | |
5148 PREG is a regex_t *. We do not expect any fields to be initialized, | |
5149 since POSIX says we shouldn't. Thus, we set | |
5150 | |
5151 `buffer' to the compiled pattern; | |
5152 `used' to the length of the compiled pattern; | |
5153 `syntax' to RE_SYNTAX_POSIX_EXTENDED if the | |
5154 REG_EXTENDED bit in CFLAGS is set; otherwise, to | |
5155 RE_SYNTAX_POSIX_BASIC; | |
5156 `newline_anchor' to REG_NEWLINE being set in CFLAGS; | |
5157 `fastmap' and `fastmap_accurate' to zero; | |
5158 `re_nsub' to the number of subexpressions in PATTERN. | |
5159 | |
5160 PATTERN is the address of the pattern string. | |
5161 | |
5162 CFLAGS is a series of bits which affect compilation. | |
5163 | |
5164 If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we | |
5165 use POSIX basic syntax. | |
5166 | |
5167 If REG_NEWLINE is set, then . and [^...] don't match newline. | |
5168 Also, regexec will try a match beginning after every newline. | |
5169 | |
5170 If REG_ICASE is set, then we considers upper- and lowercase | |
5171 versions of letters to be equivalent when matching. | |
5172 | |
5173 If REG_NOSUB is set, then when PREG is passed to regexec, that | |
5174 routine will report only success or failure, and nothing about the | |
5175 registers. | |
5176 | |
5177 It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for | |
5178 the return codes and their meanings.) */ | |
5179 | |
5180 int | |
5181 regcomp (preg, pattern, cflags) | |
5182 regex_t *preg; | |
5183 const char *pattern; | |
5184 int cflags; | |
5185 { | |
5186 reg_errcode_t ret; | |
5187 unsigned syntax | |
1642
340feb030df1
*** empty log message ***
David J. MacKenzie <djm@gnu.org>
parents:
1641
diff
changeset
|
5188 = (cflags & REG_EXTENDED) ? |
340feb030df1
*** empty log message ***
David J. MacKenzie <djm@gnu.org>
parents:
1641
diff
changeset
|
5189 RE_SYNTAX_POSIX_EXTENDED : RE_SYNTAX_POSIX_BASIC; |
1155 | 5190 |
5191 /* regex_compile will allocate the space for the compiled pattern. */ | |
5192 preg->buffer = 0; | |
1642
340feb030df1
*** empty log message ***
David J. MacKenzie <djm@gnu.org>
parents:
1641
diff
changeset
|
5193 preg->allocated = 0; |
2758 | 5194 preg->used = 0; |
1155 | 5195 |
5196 /* Don't bother to use a fastmap when searching. This simplifies the | |
5197 REG_NEWLINE case: if we used a fastmap, we'd have to put all the | |
5198 characters after newlines into the fastmap. This way, we just try | |
5199 every character. */ | |
5200 preg->fastmap = 0; | |
5201 | |
5202 if (cflags & REG_ICASE) | |
5203 { | |
5204 unsigned i; | |
5205 | |
5206 preg->translate = (char *) malloc (CHAR_SET_SIZE); | |
5207 if (preg->translate == NULL) | |
5208 return (int) REG_ESPACE; | |
5209 | |
5210 /* Map uppercase characters to corresponding lowercase ones. */ | |
5211 for (i = 0; i < CHAR_SET_SIZE; i++) | |
1668 | 5212 preg->translate[i] = ISUPPER (i) ? tolower (i) : i; |
1155 | 5213 } |
5214 else | |
5215 preg->translate = NULL; | |
5216 | |
5217 /* If REG_NEWLINE is set, newlines are treated differently. */ | |
5218 if (cflags & REG_NEWLINE) | |
5219 { /* REG_NEWLINE implies neither . nor [^...] match newline. */ | |
5220 syntax &= ~RE_DOT_NEWLINE; | |
5221 syntax |= RE_HAT_LISTS_NOT_NEWLINE; | |
5222 /* It also changes the matching behavior. */ | |
5223 preg->newline_anchor = 1; | |
5224 } | |
5225 else | |
5226 preg->newline_anchor = 0; | |
5227 | |
5228 preg->no_sub = !!(cflags & REG_NOSUB); | |
5229 | |
5230 /* POSIX says a null character in the pattern terminates it, so we | |
5231 can use strlen here in compiling the pattern. */ | |
5232 ret = regex_compile (pattern, strlen (pattern), syntax, preg); | |
5233 | |
5234 /* POSIX doesn't distinguish between an unmatched open-group and an | |
5235 unmatched close-group: both are REG_EPAREN. */ | |
5236 if (ret == REG_ERPAREN) ret = REG_EPAREN; | |
5237 | |
5238 return (int) ret; | |
5239 } | |
5240 | |
5241 | |
5242 /* regexec searches for a given pattern, specified by PREG, in the | |
5243 string STRING. | |
5244 | |
5245 If NMATCH is zero or REG_NOSUB was set in the cflags argument to | |
5246 `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at | |
5247 least NMATCH elements, and we set them to the offsets of the | |
5248 corresponding matched substrings. | |
5249 | |
5250 EFLAGS specifies `execution flags' which affect matching: if | |
5251 REG_NOTBOL is set, then ^ does not match at the beginning of the | |
5252 string; if REG_NOTEOL is set, then $ does not match at the end. | |
5253 | |
5254 We return 0 if we find a match and REG_NOMATCH if not. */ | |
5255 | |
5256 int | |
5257 regexec (preg, string, nmatch, pmatch, eflags) | |
5258 const regex_t *preg; | |
5259 const char *string; | |
5260 size_t nmatch; | |
5261 regmatch_t pmatch[]; | |
5262 int eflags; | |
5263 { | |
5264 int ret; | |
5265 struct re_registers regs; | |
5266 regex_t private_preg; | |
5267 int len = strlen (string); | |
5268 boolean want_reg_info = !preg->no_sub && nmatch > 0; | |
5269 | |
5270 private_preg = *preg; | |
5271 | |
5272 private_preg.not_bol = !!(eflags & REG_NOTBOL); | |
5273 private_preg.not_eol = !!(eflags & REG_NOTEOL); | |
5274 | |
5275 /* The user has told us exactly how many registers to return | |
5276 information about, via `nmatch'. We have to pass that on to the | |
5277 matching routines. */ | |
5278 private_preg.regs_allocated = REGS_FIXED; | |
5279 | |
5280 if (want_reg_info) | |
5281 { | |
5282 regs.num_regs = nmatch; | |
5283 regs.start = TALLOC (nmatch, regoff_t); | |
5284 regs.end = TALLOC (nmatch, regoff_t); | |
5285 if (regs.start == NULL || regs.end == NULL) | |
5286 return (int) REG_NOMATCH; | |
5287 } | |
5288 | |
5289 /* Perform the searching operation. */ | |
5290 ret = re_search (&private_preg, string, len, | |
5291 /* start: */ 0, /* range: */ len, | |
5292 want_reg_info ? ®s : (struct re_registers *) 0); | |
5293 | |
5294 /* Copy the register information to the POSIX structure. */ | |
5295 if (want_reg_info) | |
5296 { | |
5297 if (ret >= 0) | |
5298 { | |
5299 unsigned r; | |
5300 | |
5301 for (r = 0; r < nmatch; r++) | |
5302 { | |
5303 pmatch[r].rm_so = regs.start[r]; | |
5304 pmatch[r].rm_eo = regs.end[r]; | |
5305 } | |
5306 } | |
5307 | |
5308 /* If we needed the temporary register info, free the space now. */ | |
5309 free (regs.start); | |
5310 free (regs.end); | |
5311 } | |
5312 | |
5313 /* We want zero return to mean success, unlike `re_search'. */ | |
5314 return ret >= 0 ? (int) REG_NOERROR : (int) REG_NOMATCH; | |
5315 } | |
5316 | |
5317 | |
5318 /* Returns a message corresponding to an error code, ERRCODE, returned | |
1637 | 5319 from either regcomp or regexec. We don't use PREG here. */ |
1155 | 5320 |
5321 size_t | |
5322 regerror (errcode, preg, errbuf, errbuf_size) | |
5323 int errcode; | |
5324 const regex_t *preg; | |
5325 char *errbuf; | |
5326 size_t errbuf_size; | |
5327 { | |
1738 | 5328 const char *msg; |
5329 size_t msg_size; | |
5330 | |
5331 if (errcode < 0 | |
10090
672e07670453
(re_compile_pattern, re_comp, regerror): Add gettext wrapper.
Paul Eggert <eggert@twinsun.com>
parents:
10023
diff
changeset
|
5332 || errcode >= (sizeof (re_error_msgid) / sizeof (re_error_msgid[0]))) |
1738 | 5333 /* Only error codes returned by the rest of the code should be passed |
5334 to this routine. If we are given anything else, or if other regex | |
5335 code generates an invalid error code, then the program has a bug. | |
5336 Dump core so we can fix it. */ | |
5337 abort (); | |
5338 | |
10090
672e07670453
(re_compile_pattern, re_comp, regerror): Add gettext wrapper.
Paul Eggert <eggert@twinsun.com>
parents:
10023
diff
changeset
|
5339 msg = gettext (re_error_msgid[errcode]); |
2453 | 5340 |
1738 | 5341 msg_size = strlen (msg) + 1; /* Includes the null. */ |
1155 | 5342 |
5343 if (errbuf_size != 0) | |
5344 { | |
5345 if (msg_size > errbuf_size) | |
5346 { | |
5347 strncpy (errbuf, msg, errbuf_size - 1); | |
5348 errbuf[errbuf_size - 1] = 0; | |
5349 } | |
5350 else | |
5351 strcpy (errbuf, msg); | |
5352 } | |
5353 | |
5354 return msg_size; | |
5355 } | |
5356 | |
5357 | |
5358 /* Free dynamically allocated space used by PREG. */ | |
5359 | |
5360 void | |
5361 regfree (preg) | |
5362 regex_t *preg; | |
5363 { | |
5364 if (preg->buffer != NULL) | |
5365 free (preg->buffer); | |
5366 preg->buffer = NULL; | |
5367 | |
5368 preg->allocated = 0; | |
5369 preg->used = 0; | |
5370 | |
5371 if (preg->fastmap != NULL) | |
5372 free (preg->fastmap); | |
5373 preg->fastmap = NULL; | |
5374 preg->fastmap_accurate = 0; | |
5375 | |
5376 if (preg->translate != NULL) | |
5377 free (preg->translate); | |
5378 preg->translate = NULL; | |
5379 } | |
5380 | |
5381 #endif /* not emacs */ | |
5382 | |
5383 /* | |
5384 Local variables: | |
5385 make-backup-files: t | |
5386 version-control: t | |
5387 trim-versions-without-asking: nil | |
5388 End: | |
5389 */ |