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