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