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