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