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