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