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