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