comparison regex.c @ 939:ace17b6e9d29

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