This is a big redesign of failure-stack and register handling, prompted
[emacs.git] / src / regex.c
blob809a7d24219e35a2b2acc6a3c072f7f09632e2b2
1 /* Extended regular expression matching and search library, version
2 0.12. (Implements POSIX draft P10003.2/D11.2, except for
3 internationalization features.)
5 Copyright (C) 1993,94,95,96,97,98,2000 Free Software Foundation, Inc.
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
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
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
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 USA. */
22 /* TODO:
23 - detect nasty infinite loops like "\\(\\)+?ab" when matching "ac".
24 - use `keep_string' more often than just .*\n.
25 - structure the opcode space into opcode+flag.
26 - merge with glic's regex.[ch]
28 That's it for now -sm */
30 /* AIX requires this to be the first thing in the file. */
31 #if defined (_AIX) && !defined (REGEX_MALLOC)
32 #pragma alloca
33 #endif
35 #undef _GNU_SOURCE
36 #define _GNU_SOURCE
38 #ifdef emacs
39 /* Converts the pointer to the char to BEG-based offset from the start. */
40 #define PTR_TO_OFFSET(d) \
41 POS_AS_IN_BUFFER (MATCHING_IN_FIRST_STRING \
42 ? (d) - string1 : (d) - (string2 - size1))
43 #define POS_AS_IN_BUFFER(p) ((p) + (NILP (re_match_object) || BUFFERP (re_match_object)))
44 #else
45 #define PTR_TO_OFFSET(d) 0
46 #endif
48 #ifdef HAVE_CONFIG_H
49 #include <config.h>
50 #endif
52 /* We need this for `regex.h', and perhaps for the Emacs include files. */
53 #include <sys/types.h>
55 /* This is for other GNU distributions with internationalized messages. */
56 #if HAVE_LIBINTL_H || defined (_LIBC)
57 # include <libintl.h>
58 #else
59 # define gettext(msgid) (msgid)
60 #endif
62 #ifndef gettext_noop
63 /* This define is so xgettext can find the internationalizable
64 strings. */
65 #define gettext_noop(String) String
66 #endif
68 /* The `emacs' switch turns on certain matching commands
69 that make sense only in Emacs. */
70 #ifdef emacs
72 #include "lisp.h"
73 #include "buffer.h"
75 /* Make syntax table lookup grant data in gl_state. */
76 #define SYNTAX_ENTRY_VIA_PROPERTY
78 #include "syntax.h"
79 #include "charset.h"
80 #include "category.h"
82 #define malloc xmalloc
83 #define realloc xrealloc
84 #define free xfree
86 #else /* not emacs */
88 /* If we are not linking with Emacs proper,
89 we can't use the relocating allocator
90 even if config.h says that we can. */
91 #undef REL_ALLOC
93 #if defined (STDC_HEADERS) || defined (_LIBC)
94 #include <stdlib.h>
95 #else
96 char *malloc ();
97 char *realloc ();
98 #endif
100 /* When used in Emacs's lib-src, we need to get bzero and bcopy somehow.
101 If nothing else has been done, use the method below. */
102 #ifdef INHIBIT_STRING_HEADER
103 #if !(defined (HAVE_BZERO) && defined (HAVE_BCOPY))
104 #if !defined (bzero) && !defined (bcopy)
105 #undef INHIBIT_STRING_HEADER
106 #endif
107 #endif
108 #endif
110 /* This is the normal way of making sure we have a bcopy and a bzero.
111 This is used in most programs--a few other programs avoid this
112 by defining INHIBIT_STRING_HEADER. */
113 #ifndef INHIBIT_STRING_HEADER
114 #if defined (HAVE_STRING_H) || defined (STDC_HEADERS) || defined (_LIBC)
115 #include <string.h>
116 #ifndef bcmp
117 #define bcmp(s1, s2, n) memcmp ((s1), (s2), (n))
118 #endif
119 #ifndef bcopy
120 #define bcopy(s, d, n) memcpy ((d), (s), (n))
121 #endif
122 #ifndef bzero
123 #define bzero(s, n) memset ((s), 0, (n))
124 #endif
125 #else
126 #include <strings.h>
127 #endif
128 #endif
130 /* Define the syntax stuff for \<, \>, etc. */
132 /* This must be nonzero for the wordchar and notwordchar pattern
133 commands in re_match_2. */
134 #ifndef Sword
135 #define Sword 1
136 #endif
138 #ifdef SWITCH_ENUM_BUG
139 #define SWITCH_ENUM_CAST(x) ((int)(x))
140 #else
141 #define SWITCH_ENUM_CAST(x) (x)
142 #endif
144 #ifdef SYNTAX_TABLE
146 extern char *re_syntax_table;
148 #else /* not SYNTAX_TABLE */
150 /* How many characters in the character set. */
151 #define CHAR_SET_SIZE 256
153 static char re_syntax_table[CHAR_SET_SIZE];
155 static void
156 init_syntax_once ()
158 register int c;
159 static int done = 0;
161 if (done)
162 return;
164 bzero (re_syntax_table, sizeof re_syntax_table);
166 for (c = 'a'; c <= 'z'; c++)
167 re_syntax_table[c] = Sword;
169 for (c = 'A'; c <= 'Z'; c++)
170 re_syntax_table[c] = Sword;
172 for (c = '0'; c <= '9'; c++)
173 re_syntax_table[c] = Sword;
175 re_syntax_table['_'] = Sword;
177 done = 1;
180 #endif /* not SYNTAX_TABLE */
182 #define SYNTAX(c) re_syntax_table[c]
184 /* Dummy macros for non-Emacs environments. */
185 #define BASE_LEADING_CODE_P(c) (0)
186 #define WORD_BOUNDARY_P(c1, c2) (0)
187 #define CHAR_HEAD_P(p) (1)
188 #define SINGLE_BYTE_CHAR_P(c) (1)
189 #define SAME_CHARSET_P(c1, c2) (1)
190 #define MULTIBYTE_FORM_LENGTH(p, s) (1)
191 #define STRING_CHAR(p, s) (*(p))
192 #define STRING_CHAR_AND_LENGTH(p, s, actual_len) ((actual_len) = 1, *(p))
193 #define GET_CHAR_AFTER_2(c, p, str1, end1, str2, end2) \
194 (c = ((p) == (end1) ? *(str2) : *(p)))
195 #define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2) \
196 (c = ((p) == (str2) ? *((end1) - 1) : *((p) - 1)))
197 #endif /* not emacs */
199 /* Get the interface, including the syntax bits. */
200 #include "regex.h"
202 /* isalpha etc. are used for the character classes. */
203 #include <ctype.h>
205 #ifdef emacs
207 /* 1 if C is an ASCII character. */
208 #define IS_REAL_ASCII(c) ((c) < 0200)
210 /* 1 if C is a unibyte character. */
211 #define ISUNIBYTE(c) (SINGLE_BYTE_CHAR_P ((c)))
213 /* The Emacs definitions should not be directly affected by locales. */
215 /* In Emacs, these are only used for single-byte characters. */
216 #define ISDIGIT(c) ((c) >= '0' && (c) <= '9')
217 #define ISCNTRL(c) ((c) < ' ')
218 #define ISXDIGIT(c) (((c) >= '0' && (c) <= '9') \
219 || ((c) >= 'a' && (c) <= 'f') \
220 || ((c) >= 'A' && (c) <= 'F'))
222 /* This is only used for single-byte characters. */
223 #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
225 /* The rest must handle multibyte characters. */
227 #define ISGRAPH(c) (SINGLE_BYTE_CHAR_P (c) \
228 ? (c) > ' ' && !((c) >= 0177 && (c) <= 0237) \
229 : 1)
231 #define ISPRINT(c) (SINGLE_BYTE_CHAR_P (c) \
232 ? (c) >= ' ' && !((c) >= 0177 && (c) <= 0237) \
233 : 1)
235 #define ISALNUM(c) (IS_REAL_ASCII (c) \
236 ? (((c) >= 'a' && (c) <= 'z') \
237 || ((c) >= 'A' && (c) <= 'Z') \
238 || ((c) >= '0' && (c) <= '9')) \
239 : SYNTAX (c) == Sword)
241 #define ISALPHA(c) (IS_REAL_ASCII (c) \
242 ? (((c) >= 'a' && (c) <= 'z') \
243 || ((c) >= 'A' && (c) <= 'Z')) \
244 : SYNTAX (c) == Sword)
246 #define ISLOWER(c) (LOWERCASEP (c))
248 #define ISPUNCT(c) (IS_REAL_ASCII (c) \
249 ? ((c) > ' ' && (c) < 0177 \
250 && !(((c) >= 'a' && (c) <= 'z') \
251 || ((c) >= 'A' && (c) <= 'Z') \
252 || ((c) >= '0' && (c) <= '9'))) \
253 : SYNTAX (c) != Sword)
255 #define ISSPACE(c) (SYNTAX (c) == Swhitespace)
257 #define ISUPPER(c) (UPPERCASEP (c))
259 #define ISWORD(c) (SYNTAX (c) == Sword)
261 #else /* not emacs */
263 /* Jim Meyering writes:
265 "... Some ctype macros are valid only for character codes that
266 isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
267 using /bin/cc or gcc but without giving an ansi option). So, all
268 ctype uses should be through macros like ISPRINT... If
269 STDC_HEADERS is defined, then autoconf has verified that the ctype
270 macros don't need to be guarded with references to isascii. ...
271 Defining isascii to 1 should let any compiler worth its salt
272 eliminate the && through constant folding." */
274 #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
275 #define ISASCII(c) 1
276 #else
277 #define ISASCII(c) isascii(c)
278 #endif
280 /* 1 if C is an ASCII character. */
281 #define IS_REAL_ASCII(c) ((c) < 0200)
283 /* This distinction is not meaningful, except in Emacs. */
284 #define ISUNIBYTE(c) 1
286 #define ISDIGIT(c) (ISASCII (c) && isdigit (c))
287 #define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
288 #define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
290 #ifdef isblank
291 #define ISBLANK(c) (ISASCII (c) && isblank (c))
292 #else
293 #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
294 #endif
295 #ifdef isgraph
296 #define ISGRAPH(c) (ISASCII (c) && isgraph (c))
297 #else
298 #define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
299 #endif
301 #define ISPRINT(c) (ISASCII (c) && isprint (c))
302 #define ISDIGIT(c) (ISASCII (c) && isdigit (c))
303 #define ISALNUM(c) (ISASCII (c) && isalnum (c))
304 #define ISALPHA(c) (ISASCII (c) && isalpha (c))
305 #define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
306 #define ISLOWER(c) (ISASCII (c) && islower (c))
307 #define ISPUNCT(c) (ISASCII (c) && ispunct (c))
308 #define ISSPACE(c) (ISASCII (c) && isspace (c))
309 #define ISUPPER(c) (ISASCII (c) && isupper (c))
310 #define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
312 #define ISWORD(c) ISALPHA(c)
314 #endif /* not emacs */
316 #ifndef NULL
317 #define NULL (void *)0
318 #endif
320 /* We remove any previous definition of `SIGN_EXTEND_CHAR',
321 since ours (we hope) works properly with all combinations of
322 machines, compilers, `char' and `unsigned char' argument types.
323 (Per Bothner suggested the basic approach.) */
324 #undef SIGN_EXTEND_CHAR
325 #if __STDC__
326 #define SIGN_EXTEND_CHAR(c) ((signed char) (c))
327 #else /* not __STDC__ */
328 /* As in Harbison and Steele. */
329 #define SIGN_EXTEND_CHAR(c) ((((unsigned char) (c)) ^ 128) - 128)
330 #endif
332 /* Should we use malloc or alloca? If REGEX_MALLOC is not defined, we
333 use `alloca' instead of `malloc'. This is because using malloc in
334 re_search* or re_match* could cause memory leaks when C-g is used in
335 Emacs; also, malloc is slower and causes storage fragmentation. On
336 the other hand, malloc is more portable, and easier to debug.
338 Because we sometimes use alloca, some routines have to be macros,
339 not functions -- `alloca'-allocated space disappears at the end of the
340 function it is called in. */
342 #ifdef REGEX_MALLOC
344 #define REGEX_ALLOCATE malloc
345 #define REGEX_REALLOCATE(source, osize, nsize) realloc (source, nsize)
346 #define REGEX_FREE free
348 #else /* not REGEX_MALLOC */
350 /* Emacs already defines alloca, sometimes. */
351 #ifndef alloca
353 /* Make alloca work the best possible way. */
354 #ifdef __GNUC__
355 #define alloca __builtin_alloca
356 #else /* not __GNUC__ */
357 #if HAVE_ALLOCA_H
358 #include <alloca.h>
359 #else /* not __GNUC__ or HAVE_ALLOCA_H */
360 #if 0 /* It is a bad idea to declare alloca. We always cast the result. */
361 #ifndef _AIX /* Already did AIX, up at the top. */
362 char *alloca ();
363 #endif /* not _AIX */
364 #endif
365 #endif /* not HAVE_ALLOCA_H */
366 #endif /* not __GNUC__ */
368 #endif /* not alloca */
370 #define REGEX_ALLOCATE alloca
372 /* Assumes a `char *destination' variable. */
373 #define REGEX_REALLOCATE(source, osize, nsize) \
374 (destination = (char *) alloca (nsize), \
375 bcopy (source, destination, osize), \
376 destination)
378 /* No need to do anything to free, after alloca. */
379 #define REGEX_FREE(arg) ((void)0) /* Do nothing! But inhibit gcc warning. */
381 #endif /* not REGEX_MALLOC */
383 /* Define how to allocate the failure stack. */
385 #if defined (REL_ALLOC) && defined (REGEX_MALLOC)
387 #define REGEX_ALLOCATE_STACK(size) \
388 r_alloc (&failure_stack_ptr, (size))
389 #define REGEX_REALLOCATE_STACK(source, osize, nsize) \
390 r_re_alloc (&failure_stack_ptr, (nsize))
391 #define REGEX_FREE_STACK(ptr) \
392 r_alloc_free (&failure_stack_ptr)
394 #else /* not using relocating allocator */
396 #ifdef REGEX_MALLOC
398 #define REGEX_ALLOCATE_STACK malloc
399 #define REGEX_REALLOCATE_STACK(source, osize, nsize) realloc (source, nsize)
400 #define REGEX_FREE_STACK free
402 #else /* not REGEX_MALLOC */
404 #define REGEX_ALLOCATE_STACK alloca
406 #define REGEX_REALLOCATE_STACK(source, osize, nsize) \
407 REGEX_REALLOCATE (source, osize, nsize)
408 /* No need to explicitly free anything. */
409 #define REGEX_FREE_STACK(arg)
411 #endif /* not REGEX_MALLOC */
412 #endif /* not using relocating allocator */
415 /* True if `size1' is non-NULL and PTR is pointing anywhere inside
416 `string1' or just past its end. This works if PTR is NULL, which is
417 a good thing. */
418 #define FIRST_STRING_P(ptr) \
419 (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
421 /* (Re)Allocate N items of type T using malloc, or fail. */
422 #define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t)))
423 #define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
424 #define RETALLOC_IF(addr, n, t) \
425 if (addr) RETALLOC((addr), (n), t); else (addr) = TALLOC ((n), t)
426 #define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
428 #define BYTEWIDTH 8 /* In bits. */
430 #define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
432 #undef MAX
433 #undef MIN
434 #define MAX(a, b) ((a) > (b) ? (a) : (b))
435 #define MIN(a, b) ((a) < (b) ? (a) : (b))
437 typedef char boolean;
438 #define false 0
439 #define true 1
441 static int re_match_2_internal ();
443 /* These are the command codes that appear in compiled regular
444 expressions. Some opcodes are followed by argument bytes. A
445 command code can specify any interpretation whatsoever for its
446 arguments. Zero bytes may appear in the compiled regular expression. */
448 typedef enum
450 no_op = 0,
452 /* Succeed right away--no more backtracking. */
453 succeed,
455 /* Followed by one byte giving n, then by n literal bytes. */
456 exactn,
458 /* Matches any (more or less) character. */
459 anychar,
461 /* Matches any one char belonging to specified set. First
462 following byte is number of bitmap bytes. Then come bytes
463 for a bitmap saying which chars are in. Bits in each byte
464 are ordered low-bit-first. A character is in the set if its
465 bit is 1. A character too large to have a bit in the map is
466 automatically not in the set.
468 If the length byte has the 0x80 bit set, then that stuff
469 is followed by a range table:
470 2 bytes of flags for character sets (low 8 bits, high 8 bits)
471 See RANGE_TABLE_WORK_BITS below.
472 2 bytes, the number of pairs that follow
473 pairs, each 2 multibyte characters,
474 each multibyte character represented as 3 bytes. */
475 charset,
477 /* Same parameters as charset, but match any character that is
478 not one of those specified. */
479 charset_not,
481 /* Start remembering the text that is matched, for storing in a
482 register. Followed by one byte with the register number, in
483 the range 0 to one less than the pattern buffer's re_nsub
484 field. */
485 start_memory,
487 /* Stop remembering the text that is matched and store it in a
488 memory register. Followed by one byte with the register
489 number, in the range 0 to one less than `re_nsub' in the
490 pattern buffer. */
491 stop_memory,
493 /* Match a duplicate of something remembered. Followed by one
494 byte containing the register number. */
495 duplicate,
497 /* Fail unless at beginning of line. */
498 begline,
500 /* Fail unless at end of line. */
501 endline,
503 /* Succeeds if at beginning of buffer (if emacs) or at beginning
504 of string to be matched (if not). */
505 begbuf,
507 /* Analogously, for end of buffer/string. */
508 endbuf,
510 /* Followed by two byte relative address to which to jump. */
511 jump,
513 /* Followed by two-byte relative address of place to resume at
514 in case of failure. */
515 on_failure_jump,
517 /* Like on_failure_jump, but pushes a placeholder instead of the
518 current string position when executed. */
519 on_failure_keep_string_jump,
521 /* Like `on_failure_jump', except that it assumes that the
522 pattern following it is mutually exclusive with the pattern
523 at the destination of the jump: if one matches something,
524 the other won't match at all.
525 Always used via `on_failure_jump_smart'. */
526 on_failure_jump_exclusive,
528 /* Just like `on_failure_jump', except that it checks that we
529 don't get stuck in an infinite loop (matching an empty string
530 indefinitely). */
531 on_failure_jump_loop,
533 /* A smart `on_failure_jump' used for greedy * and + operators.
534 It analyses the loop before which it is put and if the
535 loop does not require backtracking, it changes itself to
536 `on_failure_jump_exclusive', else it just defaults to
537 changing itself into `on_failure_jump_loop'. */
538 on_failure_jump_smart,
540 /* Followed by two-byte relative address and two-byte number n.
541 After matching N times, jump to the address upon failure. */
542 succeed_n,
544 /* Followed by two-byte relative address, and two-byte number n.
545 Jump to the address N times, then fail. */
546 jump_n,
548 /* Set the following two-byte relative address to the
549 subsequent two-byte number. The address *includes* the two
550 bytes of number. */
551 set_number_at,
553 wordchar, /* Matches any word-constituent character. */
554 notwordchar, /* Matches any char that is not a word-constituent. */
556 wordbeg, /* Succeeds if at word beginning. */
557 wordend, /* Succeeds if at word end. */
559 wordbound, /* Succeeds if at a word boundary. */
560 notwordbound /* Succeeds if not at a word boundary. */
562 #ifdef emacs
563 ,before_dot, /* Succeeds if before point. */
564 at_dot, /* Succeeds if at point. */
565 after_dot, /* Succeeds if after point. */
567 /* Matches any character whose syntax is specified. Followed by
568 a byte which contains a syntax code, e.g., Sword. */
569 syntaxspec,
571 /* Matches any character whose syntax is not that specified. */
572 notsyntaxspec,
574 /* Matches any character whose category-set contains the specified
575 category. The operator is followed by a byte which contains a
576 category code (mnemonic ASCII character). */
577 categoryspec,
579 /* Matches any character whose category-set does not contain the
580 specified category. The operator is followed by a byte which
581 contains the category code (mnemonic ASCII character). */
582 notcategoryspec
583 #endif /* emacs */
584 } re_opcode_t;
586 /* Common operations on the compiled pattern. */
588 /* Store NUMBER in two contiguous bytes starting at DESTINATION. */
590 #define STORE_NUMBER(destination, number) \
591 do { \
592 (destination)[0] = (number) & 0377; \
593 (destination)[1] = (number) >> 8; \
594 } while (0)
596 /* Same as STORE_NUMBER, except increment DESTINATION to
597 the byte after where the number is stored. Therefore, DESTINATION
598 must be an lvalue. */
600 #define STORE_NUMBER_AND_INCR(destination, number) \
601 do { \
602 STORE_NUMBER (destination, number); \
603 (destination) += 2; \
604 } while (0)
606 /* Put into DESTINATION a number stored in two contiguous bytes starting
607 at SOURCE. */
609 #define EXTRACT_NUMBER(destination, source) \
610 do { \
611 (destination) = *(source) & 0377; \
612 (destination) += SIGN_EXTEND_CHAR (*((source) + 1)) << 8; \
613 } while (0)
615 #ifdef DEBUG
616 static void
617 extract_number (dest, source)
618 int *dest;
619 unsigned char *source;
621 int temp = SIGN_EXTEND_CHAR (*(source + 1));
622 *dest = *source & 0377;
623 *dest += temp << 8;
626 #ifndef EXTRACT_MACROS /* To debug the macros. */
627 #undef EXTRACT_NUMBER
628 #define EXTRACT_NUMBER(dest, src) extract_number (&dest, src)
629 #endif /* not EXTRACT_MACROS */
631 #endif /* DEBUG */
633 /* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
634 SOURCE must be an lvalue. */
636 #define EXTRACT_NUMBER_AND_INCR(destination, source) \
637 do { \
638 EXTRACT_NUMBER (destination, source); \
639 (source) += 2; \
640 } while (0)
642 #ifdef DEBUG
643 static void
644 extract_number_and_incr (destination, source)
645 int *destination;
646 unsigned char **source;
648 extract_number (destination, *source);
649 *source += 2;
652 #ifndef EXTRACT_MACROS
653 #undef EXTRACT_NUMBER_AND_INCR
654 #define EXTRACT_NUMBER_AND_INCR(dest, src) \
655 extract_number_and_incr (&dest, &src)
656 #endif /* not EXTRACT_MACROS */
658 #endif /* DEBUG */
660 /* Store a multibyte character in three contiguous bytes starting
661 DESTINATION, and increment DESTINATION to the byte after where the
662 character is stored. Therefore, DESTINATION must be an lvalue. */
664 #define STORE_CHARACTER_AND_INCR(destination, character) \
665 do { \
666 (destination)[0] = (character) & 0377; \
667 (destination)[1] = ((character) >> 8) & 0377; \
668 (destination)[2] = (character) >> 16; \
669 (destination) += 3; \
670 } while (0)
672 /* Put into DESTINATION a character stored in three contiguous bytes
673 starting at SOURCE. */
675 #define EXTRACT_CHARACTER(destination, source) \
676 do { \
677 (destination) = ((source)[0] \
678 | ((source)[1] << 8) \
679 | ((source)[2] << 16)); \
680 } while (0)
683 /* Macros for charset. */
685 /* Size of bitmap of charset P in bytes. P is a start of charset,
686 i.e. *P is (re_opcode_t) charset or (re_opcode_t) charset_not. */
687 #define CHARSET_BITMAP_SIZE(p) ((p)[1] & 0x7F)
689 /* Nonzero if charset P has range table. */
690 #define CHARSET_RANGE_TABLE_EXISTS_P(p) ((p)[1] & 0x80)
692 /* Return the address of range table of charset P. But not the start
693 of table itself, but the before where the number of ranges is
694 stored. `2 +' means to skip re_opcode_t and size of bitmap,
695 and the 2 bytes of flags at the start of the range table. */
696 #define CHARSET_RANGE_TABLE(p) (&(p)[4 + CHARSET_BITMAP_SIZE (p)])
698 /* Extract the bit flags that start a range table. */
699 #define CHARSET_RANGE_TABLE_BITS(p) \
700 ((p)[2 + CHARSET_BITMAP_SIZE (p)] \
701 + (p)[3 + CHARSET_BITMAP_SIZE (p)] * 0x100)
703 /* Test if C is listed in the bitmap of charset P. */
704 #define CHARSET_LOOKUP_BITMAP(p, c) \
705 ((c) < CHARSET_BITMAP_SIZE (p) * BYTEWIDTH \
706 && (p)[2 + (c) / BYTEWIDTH] & (1 << ((c) % BYTEWIDTH)))
708 /* Return the address of end of RANGE_TABLE. COUNT is number of
709 ranges (which is a pair of (start, end)) in the RANGE_TABLE. `* 2'
710 is start of range and end of range. `* 3' is size of each start
711 and end. */
712 #define CHARSET_RANGE_TABLE_END(range_table, count) \
713 ((range_table) + (count) * 2 * 3)
715 /* Test if C is in RANGE_TABLE. A flag NOT is negated if C is in.
716 COUNT is number of ranges in RANGE_TABLE. */
717 #define CHARSET_LOOKUP_RANGE_TABLE_RAW(not, c, range_table, count) \
718 do \
720 int range_start, range_end; \
721 unsigned char *p; \
722 unsigned char *range_table_end \
723 = CHARSET_RANGE_TABLE_END ((range_table), (count)); \
725 for (p = (range_table); p < range_table_end; p += 2 * 3) \
727 EXTRACT_CHARACTER (range_start, p); \
728 EXTRACT_CHARACTER (range_end, p + 3); \
730 if (range_start <= (c) && (c) <= range_end) \
732 (not) = !(not); \
733 break; \
737 while (0)
739 /* Test if C is in range table of CHARSET. The flag NOT is negated if
740 C is listed in it. */
741 #define CHARSET_LOOKUP_RANGE_TABLE(not, c, charset) \
742 do \
744 /* Number of ranges in range table. */ \
745 int count; \
746 unsigned char *range_table = CHARSET_RANGE_TABLE (charset); \
748 EXTRACT_NUMBER_AND_INCR (count, range_table); \
749 CHARSET_LOOKUP_RANGE_TABLE_RAW ((not), (c), range_table, count); \
751 while (0)
753 /* If DEBUG is defined, Regex prints many voluminous messages about what
754 it is doing (if the variable `debug' is nonzero). If linked with the
755 main program in `iregex.c', you can enter patterns and strings
756 interactively. And if linked with the main program in `main.c' and
757 the other test files, you can run the already-written tests. */
759 #ifdef DEBUG
761 /* We use standard I/O for debugging. */
762 #include <stdio.h>
764 /* It is useful to test things that ``must'' be true when debugging. */
765 #include <assert.h>
767 static int debug = 0;
769 #define DEBUG_STATEMENT(e) e
770 #define DEBUG_PRINT1(x) if (debug) printf (x)
771 #define DEBUG_PRINT2(x1, x2) if (debug) printf (x1, x2)
772 #define DEBUG_PRINT3(x1, x2, x3) if (debug) printf (x1, x2, x3)
773 #define DEBUG_PRINT4(x1, x2, x3, x4) if (debug) printf (x1, x2, x3, x4)
774 #define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) \
775 if (debug) print_partial_compiled_pattern (s, e)
776 #define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \
777 if (debug) print_double_string (w, s1, sz1, s2, sz2)
780 /* Print the fastmap in human-readable form. */
782 void
783 print_fastmap (fastmap)
784 char *fastmap;
786 unsigned was_a_range = 0;
787 unsigned i = 0;
789 while (i < (1 << BYTEWIDTH))
791 if (fastmap[i++])
793 was_a_range = 0;
794 putchar (i - 1);
795 while (i < (1 << BYTEWIDTH) && fastmap[i])
797 was_a_range = 1;
798 i++;
800 if (was_a_range)
802 printf ("-");
803 putchar (i - 1);
807 putchar ('\n');
811 /* Print a compiled pattern string in human-readable form, starting at
812 the START pointer into it and ending just before the pointer END. */
814 void
815 print_partial_compiled_pattern (start, end)
816 unsigned char *start;
817 unsigned char *end;
819 int mcnt, mcnt2;
820 unsigned char *p = start;
821 unsigned char *pend = end;
823 if (start == NULL)
825 printf ("(null)\n");
826 return;
829 /* Loop over pattern commands. */
830 while (p < pend)
832 printf ("%d:\t", p - start);
834 switch ((re_opcode_t) *p++)
836 case no_op:
837 printf ("/no_op");
838 break;
840 case exactn:
841 mcnt = *p++;
842 printf ("/exactn/%d", mcnt);
845 putchar ('/');
846 putchar (*p++);
848 while (--mcnt);
849 break;
851 case start_memory:
852 printf ("/start_memory/%d", *p++);
853 break;
855 case stop_memory:
856 printf ("/stop_memory/%d", *p++);
857 break;
859 case duplicate:
860 printf ("/duplicate/%d", *p++);
861 break;
863 case anychar:
864 printf ("/anychar");
865 break;
867 case charset:
868 case charset_not:
870 register int c, last = -100;
871 register int in_range = 0;
872 int length = *p & 0x7f;
873 int has_range_table = *p & 0x80;
874 int range_length = p[length + 2] + p[length + 3] * 0x100;
876 printf ("/charset [%s",
877 (re_opcode_t) *(p - 1) == charset_not ? "^" : "");
879 assert (p + *p < pend);
881 for (c = 0; c < 256; c++)
882 if (c / 8 < length
883 && (p[1 + (c/8)] & (1 << (c % 8))))
885 /* Are we starting a range? */
886 if (last + 1 == c && ! in_range)
888 putchar ('-');
889 in_range = 1;
891 /* Have we broken a range? */
892 else if (last + 1 != c && in_range)
894 putchar (last);
895 in_range = 0;
898 if (! in_range)
899 putchar (c);
901 last = c;
904 p += 1 + length;
906 if (in_range)
907 putchar (last);
909 putchar (']');
911 if (has_range_table)
912 printf ("has-range-table");
914 /* ??? Should print the range table; for now,
915 just skip it. */
916 if (has_range_table)
917 p += 4 + 6 * range_length;
919 break;
921 case begline:
922 printf ("/begline");
923 break;
925 case endline:
926 printf ("/endline");
927 break;
929 case on_failure_jump:
930 extract_number_and_incr (&mcnt, &p);
931 printf ("/on_failure_jump to %d", p + mcnt - start);
932 break;
934 case on_failure_keep_string_jump:
935 extract_number_and_incr (&mcnt, &p);
936 printf ("/on_failure_keep_string_jump to %d", p + mcnt - start);
937 break;
939 case on_failure_jump_exclusive:
940 extract_number_and_incr (&mcnt, &p);
941 printf ("/on_failure_jump_exclusive to %d", p + mcnt - start);
942 break;
944 case on_failure_jump_loop:
945 extract_number_and_incr (&mcnt, &p);
946 printf ("/on_failure_jump_loop to %d", p + mcnt - start);
947 break;
949 case on_failure_jump_smart:
950 extract_number_and_incr (&mcnt, &p);
951 printf ("/on_failure_jump_smart to %d", p + mcnt - start);
952 break;
954 case jump:
955 extract_number_and_incr (&mcnt, &p);
956 printf ("/jump to %d", p + mcnt - start);
957 break;
959 case succeed_n:
960 extract_number_and_incr (&mcnt, &p);
961 extract_number_and_incr (&mcnt2, &p);
962 printf ("/succeed_n to %d, %d times", p + mcnt - start, mcnt2);
963 break;
965 case jump_n:
966 extract_number_and_incr (&mcnt, &p);
967 extract_number_and_incr (&mcnt2, &p);
968 printf ("/jump_n to %d, %d times", p + mcnt - start, mcnt2);
969 break;
971 case set_number_at:
972 extract_number_and_incr (&mcnt, &p);
973 extract_number_and_incr (&mcnt2, &p);
974 printf ("/set_number_at location %d to %d", p + mcnt - start, mcnt2);
975 break;
977 case wordbound:
978 printf ("/wordbound");
979 break;
981 case notwordbound:
982 printf ("/notwordbound");
983 break;
985 case wordbeg:
986 printf ("/wordbeg");
987 break;
989 case wordend:
990 printf ("/wordend");
992 #ifdef emacs
993 case before_dot:
994 printf ("/before_dot");
995 break;
997 case at_dot:
998 printf ("/at_dot");
999 break;
1001 case after_dot:
1002 printf ("/after_dot");
1003 break;
1005 case syntaxspec:
1006 printf ("/syntaxspec");
1007 mcnt = *p++;
1008 printf ("/%d", mcnt);
1009 break;
1011 case notsyntaxspec:
1012 printf ("/notsyntaxspec");
1013 mcnt = *p++;
1014 printf ("/%d", mcnt);
1015 break;
1016 #endif /* emacs */
1018 case wordchar:
1019 printf ("/wordchar");
1020 break;
1022 case notwordchar:
1023 printf ("/notwordchar");
1024 break;
1026 case begbuf:
1027 printf ("/begbuf");
1028 break;
1030 case endbuf:
1031 printf ("/endbuf");
1032 break;
1034 default:
1035 printf ("?%d", *(p-1));
1038 putchar ('\n');
1041 printf ("%d:\tend of pattern.\n", p - start);
1045 void
1046 print_compiled_pattern (bufp)
1047 struct re_pattern_buffer *bufp;
1049 unsigned char *buffer = bufp->buffer;
1051 print_partial_compiled_pattern (buffer, buffer + bufp->used);
1052 printf ("%ld bytes used/%ld bytes allocated.\n", bufp->used, bufp->allocated);
1054 if (bufp->fastmap_accurate && bufp->fastmap)
1056 printf ("fastmap: ");
1057 print_fastmap (bufp->fastmap);
1060 printf ("re_nsub: %d\t", bufp->re_nsub);
1061 printf ("regs_alloc: %d\t", bufp->regs_allocated);
1062 printf ("can_be_null: %d\t", bufp->can_be_null);
1063 printf ("newline_anchor: %d\n", bufp->newline_anchor);
1064 printf ("no_sub: %d\t", bufp->no_sub);
1065 printf ("not_bol: %d\t", bufp->not_bol);
1066 printf ("not_eol: %d\t", bufp->not_eol);
1067 printf ("syntax: %d\n", bufp->syntax);
1068 fflush (stdout);
1069 /* Perhaps we should print the translate table? */
1073 void
1074 print_double_string (where, string1, size1, string2, size2)
1075 const char *where;
1076 const char *string1;
1077 const char *string2;
1078 int size1;
1079 int size2;
1081 unsigned this_char;
1083 if (where == NULL)
1084 printf ("(null)");
1085 else
1087 if (FIRST_STRING_P (where))
1089 for (this_char = where - string1; this_char < size1; this_char++)
1090 putchar (string1[this_char]);
1092 where = string2;
1095 for (this_char = where - string2; this_char < size2; this_char++)
1096 putchar (string2[this_char]);
1100 #else /* not DEBUG */
1102 #undef assert
1103 #define assert(e)
1105 #define DEBUG_STATEMENT(e)
1106 #define DEBUG_PRINT1(x)
1107 #define DEBUG_PRINT2(x1, x2)
1108 #define DEBUG_PRINT3(x1, x2, x3)
1109 #define DEBUG_PRINT4(x1, x2, x3, x4)
1110 #define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
1111 #define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
1113 #endif /* not DEBUG */
1115 /* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
1116 also be assigned to arbitrarily: each pattern buffer stores its own
1117 syntax, so it can be changed between regex compilations. */
1118 /* This has no initializer because initialized variables in Emacs
1119 become read-only after dumping. */
1120 reg_syntax_t re_syntax_options;
1123 /* Specify the precise syntax of regexps for compilation. This provides
1124 for compatibility for various utilities which historically have
1125 different, incompatible syntaxes.
1127 The argument SYNTAX is a bit mask comprised of the various bits
1128 defined in regex.h. We return the old syntax. */
1130 reg_syntax_t
1131 re_set_syntax (syntax)
1132 reg_syntax_t syntax;
1134 reg_syntax_t ret = re_syntax_options;
1136 re_syntax_options = syntax;
1137 return ret;
1140 /* This table gives an error message for each of the error codes listed
1141 in regex.h. Obviously the order here has to be same as there.
1142 POSIX doesn't require that we do anything for REG_NOERROR,
1143 but why not be nice? */
1145 static const char *re_error_msgid[] =
1147 gettext_noop ("Success"), /* REG_NOERROR */
1148 gettext_noop ("No match"), /* REG_NOMATCH */
1149 gettext_noop ("Invalid regular expression"), /* REG_BADPAT */
1150 gettext_noop ("Invalid collation character"), /* REG_ECOLLATE */
1151 gettext_noop ("Invalid character class name"), /* REG_ECTYPE */
1152 gettext_noop ("Trailing backslash"), /* REG_EESCAPE */
1153 gettext_noop ("Invalid back reference"), /* REG_ESUBREG */
1154 gettext_noop ("Unmatched [ or [^"), /* REG_EBRACK */
1155 gettext_noop ("Unmatched ( or \\("), /* REG_EPAREN */
1156 gettext_noop ("Unmatched \\{"), /* REG_EBRACE */
1157 gettext_noop ("Invalid content of \\{\\}"), /* REG_BADBR */
1158 gettext_noop ("Invalid range end"), /* REG_ERANGE */
1159 gettext_noop ("Memory exhausted"), /* REG_ESPACE */
1160 gettext_noop ("Invalid preceding regular expression"), /* REG_BADRPT */
1161 gettext_noop ("Premature end of regular expression"), /* REG_EEND */
1162 gettext_noop ("Regular expression too big"), /* REG_ESIZE */
1163 gettext_noop ("Unmatched ) or \\)"), /* REG_ERPAREN */
1166 /* Avoiding alloca during matching, to placate r_alloc. */
1168 /* Define MATCH_MAY_ALLOCATE unless we need to make sure that the
1169 searching and matching functions should not call alloca. On some
1170 systems, alloca is implemented in terms of malloc, and if we're
1171 using the relocating allocator routines, then malloc could cause a
1172 relocation, which might (if the strings being searched are in the
1173 ralloc heap) shift the data out from underneath the regexp
1174 routines.
1176 Here's another reason to avoid allocation: Emacs
1177 processes input from X in a signal handler; processing X input may
1178 call malloc; if input arrives while a matching routine is calling
1179 malloc, then we're scrod. But Emacs can't just block input while
1180 calling matching routines; then we don't notice interrupts when
1181 they come in. So, Emacs blocks input around all regexp calls
1182 except the matching calls, which it leaves unprotected, in the
1183 faith that they will not malloc. */
1185 /* Normally, this is fine. */
1186 #define MATCH_MAY_ALLOCATE
1188 /* When using GNU C, we are not REALLY using the C alloca, no matter
1189 what config.h may say. So don't take precautions for it. */
1190 #ifdef __GNUC__
1191 #undef C_ALLOCA
1192 #endif
1194 /* The match routines may not allocate if (1) they would do it with malloc
1195 and (2) it's not safe for them to use malloc.
1196 Note that if REL_ALLOC is defined, matching would not use malloc for the
1197 failure stack, but we would still use it for the register vectors;
1198 so REL_ALLOC should not affect this. */
1199 #if (defined (C_ALLOCA) || defined (REGEX_MALLOC)) && defined (emacs)
1200 #undef MATCH_MAY_ALLOCATE
1201 #endif
1204 /* Failure stack declarations and macros; both re_compile_fastmap and
1205 re_match_2 use a failure stack. These have to be macros because of
1206 REGEX_ALLOCATE_STACK. */
1209 /* Approximate number of failure points for which to initially allocate space
1210 when matching. If this number is exceeded, we allocate more
1211 space, so it is not a hard limit. */
1212 #ifndef INIT_FAILURE_ALLOC
1213 #define INIT_FAILURE_ALLOC 20
1214 #endif
1216 /* Roughly the maximum number of failure points on the stack. Would be
1217 exactly that if always used TYPICAL_FAILURE_SIZE items each time we failed.
1218 This is a variable only so users of regex can assign to it; we never
1219 change it ourselves. */
1220 #if defined (MATCH_MAY_ALLOCATE)
1221 /* Note that 4400 is enough to cause a crash on Alpha OSF/1,
1222 whose default stack limit is 2mb. In order for a larger
1223 value to work reliably, you have to try to make it accord
1224 with the process stack limit. */
1225 int re_max_failures = 40000;
1226 #else
1227 int re_max_failures = 4000;
1228 #endif
1230 union fail_stack_elt
1232 unsigned char *pointer;
1233 unsigned int integer;
1236 typedef union fail_stack_elt fail_stack_elt_t;
1238 typedef struct
1240 fail_stack_elt_t *stack;
1241 unsigned size;
1242 unsigned avail; /* Offset of next open position. */
1243 unsigned frame; /* Offset of the cur constructed frame. */
1244 } fail_stack_type;
1246 #define PATTERN_STACK_EMPTY() (fail_stack.avail == 0)
1247 #define FAIL_STACK_EMPTY() (fail_stack.frame == 0)
1248 #define FAIL_STACK_FULL() (fail_stack.avail == fail_stack.size)
1251 /* Define macros to initialize and free the failure stack.
1252 Do `return -2' if the alloc fails. */
1254 #ifdef MATCH_MAY_ALLOCATE
1255 #define INIT_FAIL_STACK() \
1256 do { \
1257 fail_stack.stack = (fail_stack_elt_t *) \
1258 REGEX_ALLOCATE_STACK (INIT_FAILURE_ALLOC * TYPICAL_FAILURE_SIZE \
1259 * sizeof (fail_stack_elt_t)); \
1261 if (fail_stack.stack == NULL) \
1262 return -2; \
1264 fail_stack.size = INIT_FAILURE_ALLOC; \
1265 fail_stack.avail = 0; \
1266 fail_stack.frame = 0; \
1267 } while (0)
1269 #define RESET_FAIL_STACK() REGEX_FREE_STACK (fail_stack.stack)
1270 #else
1271 #define INIT_FAIL_STACK() \
1272 do { \
1273 fail_stack.avail = 0; \
1274 fail_stack.frame = 0; \
1275 } while (0)
1277 #define RESET_FAIL_STACK()
1278 #endif
1281 /* Double the size of FAIL_STACK, up to a limit
1282 which allows approximately `re_max_failures' items.
1284 Return 1 if succeeds, and 0 if either ran out of memory
1285 allocating space for it or it was already too large.
1287 REGEX_REALLOCATE_STACK requires `destination' be declared. */
1289 /* Factor to increase the failure stack size by
1290 when we increase it.
1291 This used to be 2, but 2 was too wasteful
1292 because the old discarded stacks added up to as much space
1293 were as ultimate, maximum-size stack. */
1294 #define FAIL_STACK_GROWTH_FACTOR 4
1296 #define GROW_FAIL_STACK(fail_stack) \
1297 (((fail_stack).size * sizeof (fail_stack_elt_t) \
1298 >= re_max_failures * TYPICAL_FAILURE_SIZE) \
1299 ? 0 \
1300 : ((fail_stack).stack \
1301 = (fail_stack_elt_t *) \
1302 REGEX_REALLOCATE_STACK ((fail_stack).stack, \
1303 (fail_stack).size * sizeof (fail_stack_elt_t), \
1304 MIN (re_max_failures * TYPICAL_FAILURE_SIZE, \
1305 ((fail_stack).size * sizeof (fail_stack_elt_t) \
1306 * FAIL_STACK_GROWTH_FACTOR))), \
1308 (fail_stack).stack == NULL \
1309 ? 0 \
1310 : ((fail_stack).size \
1311 = (MIN (re_max_failures * TYPICAL_FAILURE_SIZE, \
1312 ((fail_stack).size * sizeof (fail_stack_elt_t) \
1313 * FAIL_STACK_GROWTH_FACTOR)) \
1314 / sizeof (fail_stack_elt_t)), \
1315 1)))
1318 /* Push pointer POINTER on FAIL_STACK.
1319 Return 1 if was able to do so and 0 if ran out of memory allocating
1320 space to do so. */
1321 #define PUSH_PATTERN_OP(POINTER, FAIL_STACK) \
1322 ((FAIL_STACK_FULL () \
1323 && !GROW_FAIL_STACK (FAIL_STACK)) \
1324 ? 0 \
1325 : ((FAIL_STACK).stack[(FAIL_STACK).avail++].pointer = POINTER, \
1327 #define POP_PATTERN_OP() POP_FAILURE_POINTER ()
1329 /* Push a pointer value onto the failure stack.
1330 Assumes the variable `fail_stack'. Probably should only
1331 be called from within `PUSH_FAILURE_POINT'. */
1332 #define PUSH_FAILURE_POINTER(item) \
1333 fail_stack.stack[fail_stack.avail++].pointer = (unsigned char *) (item)
1335 /* This pushes an integer-valued item onto the failure stack.
1336 Assumes the variable `fail_stack'. Probably should only
1337 be called from within `PUSH_FAILURE_POINT'. */
1338 #define PUSH_FAILURE_INT(item) \
1339 fail_stack.stack[fail_stack.avail++].integer = (item)
1341 /* Push a fail_stack_elt_t value onto the failure stack.
1342 Assumes the variable `fail_stack'. Probably should only
1343 be called from within `PUSH_FAILURE_POINT'. */
1344 #define PUSH_FAILURE_ELT(item) \
1345 fail_stack.stack[fail_stack.avail++] = (item)
1347 /* These three POP... operations complement the three PUSH... operations.
1348 All assume that `fail_stack' is nonempty. */
1349 #define POP_FAILURE_POINTER() fail_stack.stack[--fail_stack.avail].pointer
1350 #define POP_FAILURE_INT() fail_stack.stack[--fail_stack.avail].integer
1351 #define POP_FAILURE_ELT() fail_stack.stack[--fail_stack.avail]
1353 /* Individual items aside from the registers. */
1354 #define NUM_NONREG_ITEMS 3
1356 /* Used to examine the stack (to detect infinite loops). */
1357 #define FAILURE_PAT(h) fail_stack.stack[(h) - 1].pointer
1358 #define FAILURE_STR(h) ((char*)fail_stack.stack[(h) - 2].pointer)
1359 #define NEXT_FAILURE_HANDLE(h) fail_stack.stack[(h) - 3].integer
1360 #define TOP_FAILURE_HANDLE() fail_stack.frame
1363 #define ENSURE_FAIL_STACK(space) \
1364 while (REMAINING_AVAIL_SLOTS <= space) { \
1365 if (!GROW_FAIL_STACK (fail_stack)) \
1366 return -2; \
1367 DEBUG_PRINT2 ("\n Doubled stack; size now: %d\n", (fail_stack).size);\
1368 DEBUG_PRINT2 (" slots available: %d\n", REMAINING_AVAIL_SLOTS);\
1371 /* Push register NUM onto the stack. */
1372 #define PUSH_FAILURE_REG(num) \
1373 do { \
1374 char *destination; \
1375 ENSURE_FAIL_STACK(3); \
1376 DEBUG_PRINT4 (" Push reg %d (spanning %p -> %p)\n", \
1377 num, regstart[num], regend[num]); \
1378 PUSH_FAILURE_POINTER (regstart[num]); \
1379 PUSH_FAILURE_POINTER (regend[num]); \
1380 PUSH_FAILURE_INT (num); \
1381 } while (0)
1383 /* Pop a saved register off the stack. */
1384 #define POP_FAILURE_REG() \
1385 do { \
1386 int reg = POP_FAILURE_INT (); \
1387 regend[reg] = POP_FAILURE_POINTER (); \
1388 regstart[reg] = POP_FAILURE_POINTER (); \
1389 DEBUG_PRINT4 (" Pop reg %d (spanning %p -> %p)\n", \
1390 reg, regstart[reg], regend[reg]); \
1391 } while (0)
1393 /* Check that we are not stuck in an infinite loop. */
1394 #define CHECK_INFINITE_LOOP(pat_cur, string_place) \
1395 do { \
1396 int failure = TOP_FAILURE_HANDLE(); \
1397 /* Check for infinite matching loops */ \
1398 while (failure > 0 && \
1399 (FAILURE_STR (failure) == string_place \
1400 || FAILURE_STR (failure) == NULL)) \
1402 assert (FAILURE_PAT (failure) >= bufp->buffer \
1403 && FAILURE_PAT (failure) <= bufp->buffer + bufp->used);\
1404 if (FAILURE_PAT (failure) == pat_cur) \
1405 goto fail; \
1406 DEBUG_PRINT2 (" Other pattern: %p\n", FAILURE_PAT (failure));\
1407 failure = NEXT_FAILURE_HANDLE(failure); \
1409 DEBUG_PRINT2 (" Other string: %p\n", FAILURE_STR (failure)); \
1410 } while (0)
1412 /* Push the information about the state we will need
1413 if we ever fail back to it.
1415 Requires variables fail_stack, regstart, regend and
1416 num_regs be declared. GROW_FAIL_STACK requires `destination' be
1417 declared.
1419 Does `return FAILURE_CODE' if runs out of memory. */
1421 #define PUSH_FAILURE_POINT(pattern, string_place) \
1422 do { \
1423 char *destination; \
1424 /* Must be int, so when we don't save any registers, the arithmetic \
1425 of 0 + -1 isn't done as unsigned. */ \
1427 DEBUG_STATEMENT (failure_id++); \
1428 DEBUG_STATEMENT (nfailure_points_pushed++); \
1429 DEBUG_PRINT2 ("\nPUSH_FAILURE_POINT #%u:\n", failure_id); \
1430 DEBUG_PRINT2 (" Before push, next avail: %d\n", (fail_stack).avail); \
1431 DEBUG_PRINT2 (" size: %d\n", (fail_stack).size);\
1433 ENSURE_FAIL_STACK (NUM_NONREG_ITEMS); \
1435 DEBUG_PRINT1 ("\n"); \
1437 DEBUG_PRINT2 (" Push frame index: %d\n", fail_stack.frame); \
1438 PUSH_FAILURE_INT (fail_stack.frame); \
1440 DEBUG_PRINT2 (" Push string %p: `", string_place); \
1441 DEBUG_PRINT_DOUBLE_STRING (string_place, string1, size1, string2, size2);\
1442 DEBUG_PRINT1 ("'\n"); \
1443 PUSH_FAILURE_POINTER (string_place); \
1445 DEBUG_PRINT2 (" Push pattern %p: ", pattern); \
1446 DEBUG_PRINT_COMPILED_PATTERN (bufp, pattern, pend); \
1447 PUSH_FAILURE_POINTER (pattern); \
1449 /* Close the frame by moving the frame pointer past it. */ \
1450 fail_stack.frame = fail_stack.avail; \
1451 } while (0)
1453 /* Estimate the size of data pushed by a typical failure stack entry.
1454 An estimate is all we need, because all we use this for
1455 is to choose a limit for how big to make the failure stack. */
1457 #define TYPICAL_FAILURE_SIZE 20
1459 /* How many items can still be added to the stack without overflowing it. */
1460 #define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail)
1463 /* Pops what PUSH_FAIL_STACK pushes.
1465 We restore into the parameters, all of which should be lvalues:
1466 STR -- the saved data position.
1467 PAT -- the saved pattern position.
1468 REGSTART, REGEND -- arrays of string positions.
1470 Also assumes the variables `fail_stack' and (if debugging), `bufp',
1471 `pend', `string1', `size1', `string2', and `size2'. */
1473 #define POP_FAILURE_POINT(str, pat) \
1474 do { \
1475 assert (!FAIL_STACK_EMPTY ()); \
1477 /* Remove failure points and point to how many regs pushed. */ \
1478 DEBUG_PRINT1 ("POP_FAILURE_POINT:\n"); \
1479 DEBUG_PRINT2 (" Before pop, next avail: %d\n", fail_stack.avail); \
1480 DEBUG_PRINT2 (" size: %d\n", fail_stack.size); \
1482 /* Pop the saved registers. */ \
1483 while (fail_stack.frame < fail_stack.avail) \
1484 POP_FAILURE_REG (); \
1486 pat = (unsigned char *) POP_FAILURE_POINTER (); \
1487 DEBUG_PRINT2 (" Popping pattern %p: ", pat); \
1488 DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend); \
1490 /* If the saved string location is NULL, it came from an \
1491 on_failure_keep_string_jump opcode, and we want to throw away the \
1492 saved NULL, thus retaining our current position in the string. */ \
1493 str = (char *) POP_FAILURE_POINTER (); \
1494 DEBUG_PRINT2 (" Popping string %p: `", str); \
1495 DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2); \
1496 DEBUG_PRINT1 ("'\n"); \
1498 fail_stack.frame = POP_FAILURE_INT (); \
1499 DEBUG_PRINT2 (" Popping frame index: %d\n", fail_stack.frame); \
1501 assert (fail_stack.avail >= 0); \
1502 assert (fail_stack.frame <= fail_stack.avail); \
1504 DEBUG_STATEMENT (nfailure_points_popped++); \
1505 } while (0) /* POP_FAILURE_POINT */
1509 /* Registers are set to a sentinel when they haven't yet matched. */
1510 #define REG_UNSET_VALUE NULL
1511 #define REG_UNSET(e) ((e) == REG_UNSET_VALUE)
1513 /* Subroutine declarations and macros for regex_compile. */
1515 static void store_op1 (), store_op2 ();
1516 static void insert_op1 (), insert_op2 ();
1517 static boolean at_begline_loc_p (), at_endline_loc_p ();
1518 static boolean group_in_compile_stack ();
1520 /* Fetch the next character in the uncompiled pattern---translating it
1521 if necessary. Also cast from a signed character in the constant
1522 string passed to us by the user to an unsigned char that we can use
1523 as an array index (in, e.g., `translate'). */
1524 #ifndef PATFETCH
1525 #define PATFETCH(c) \
1526 do {if (p == pend) return REG_EEND; \
1527 c = (unsigned char) *p++; \
1528 if (RE_TRANSLATE_P (translate)) c = RE_TRANSLATE (translate, c); \
1529 } while (0)
1530 #endif
1532 /* Fetch the next character in the uncompiled pattern, with no
1533 translation. */
1534 #define PATFETCH_RAW(c) \
1535 do {if (p == pend) return REG_EEND; \
1536 c = (unsigned char) *p++; \
1537 } while (0)
1539 /* Go backwards one character in the pattern. */
1540 #define PATUNFETCH p--
1543 /* If `translate' is non-null, return translate[D], else just D. We
1544 cast the subscript to translate because some data is declared as
1545 `char *', to avoid warnings when a string constant is passed. But
1546 when we use a character as a subscript we must make it unsigned. */
1547 #ifndef TRANSLATE
1548 #define TRANSLATE(d) \
1549 (RE_TRANSLATE_P (translate) \
1550 ? (unsigned) RE_TRANSLATE (translate, (unsigned) (d)) : (d))
1551 #endif
1554 /* Macros for outputting the compiled pattern into `buffer'. */
1556 /* If the buffer isn't allocated when it comes in, use this. */
1557 #define INIT_BUF_SIZE 32
1559 /* Make sure we have at least N more bytes of space in buffer. */
1560 #define GET_BUFFER_SPACE(n) \
1561 while (b - bufp->buffer + (n) > bufp->allocated) \
1562 EXTEND_BUFFER ()
1564 /* Make sure we have one more byte of buffer space and then add C to it. */
1565 #define BUF_PUSH(c) \
1566 do { \
1567 GET_BUFFER_SPACE (1); \
1568 *b++ = (unsigned char) (c); \
1569 } while (0)
1572 /* Ensure we have two more bytes of buffer space and then append C1 and C2. */
1573 #define BUF_PUSH_2(c1, c2) \
1574 do { \
1575 GET_BUFFER_SPACE (2); \
1576 *b++ = (unsigned char) (c1); \
1577 *b++ = (unsigned char) (c2); \
1578 } while (0)
1581 /* As with BUF_PUSH_2, except for three bytes. */
1582 #define BUF_PUSH_3(c1, c2, c3) \
1583 do { \
1584 GET_BUFFER_SPACE (3); \
1585 *b++ = (unsigned char) (c1); \
1586 *b++ = (unsigned char) (c2); \
1587 *b++ = (unsigned char) (c3); \
1588 } while (0)
1591 /* Store a jump with opcode OP at LOC to location TO. We store a
1592 relative address offset by the three bytes the jump itself occupies. */
1593 #define STORE_JUMP(op, loc, to) \
1594 store_op1 (op, loc, (to) - (loc) - 3)
1596 /* Likewise, for a two-argument jump. */
1597 #define STORE_JUMP2(op, loc, to, arg) \
1598 store_op2 (op, loc, (to) - (loc) - 3, arg)
1600 /* Like `STORE_JUMP', but for inserting. Assume `b' is the buffer end. */
1601 #define INSERT_JUMP(op, loc, to) \
1602 insert_op1 (op, loc, (to) - (loc) - 3, b)
1604 /* Like `STORE_JUMP2', but for inserting. Assume `b' is the buffer end. */
1605 #define INSERT_JUMP2(op, loc, to, arg) \
1606 insert_op2 (op, loc, (to) - (loc) - 3, arg, b)
1609 /* This is not an arbitrary limit: the arguments which represent offsets
1610 into the pattern are two bytes long. So if 2^16 bytes turns out to
1611 be too small, many things would have to change. */
1612 #define MAX_BUF_SIZE (1L << 16)
1615 /* Extend the buffer by twice its current size via realloc and
1616 reset the pointers that pointed into the old block to point to the
1617 correct places in the new one. If extending the buffer results in it
1618 being larger than MAX_BUF_SIZE, then flag memory exhausted. */
1619 #define EXTEND_BUFFER() \
1620 do { \
1621 unsigned char *old_buffer = bufp->buffer; \
1622 if (bufp->allocated == MAX_BUF_SIZE) \
1623 return REG_ESIZE; \
1624 bufp->allocated <<= 1; \
1625 if (bufp->allocated > MAX_BUF_SIZE) \
1626 bufp->allocated = MAX_BUF_SIZE; \
1627 bufp->buffer = (unsigned char *) realloc (bufp->buffer, bufp->allocated);\
1628 if (bufp->buffer == NULL) \
1629 return REG_ESPACE; \
1630 /* If the buffer moved, move all the pointers into it. */ \
1631 if (old_buffer != bufp->buffer) \
1633 b = (b - old_buffer) + bufp->buffer; \
1634 begalt = (begalt - old_buffer) + bufp->buffer; \
1635 if (fixup_alt_jump) \
1636 fixup_alt_jump = (fixup_alt_jump - old_buffer) + bufp->buffer;\
1637 if (laststart) \
1638 laststart = (laststart - old_buffer) + bufp->buffer; \
1639 if (pending_exact) \
1640 pending_exact = (pending_exact - old_buffer) + bufp->buffer; \
1642 } while (0)
1645 /* Since we have one byte reserved for the register number argument to
1646 {start,stop}_memory, the maximum number of groups we can report
1647 things about is what fits in that byte. */
1648 #define MAX_REGNUM 255
1650 /* But patterns can have more than `MAX_REGNUM' registers. We just
1651 ignore the excess. */
1652 typedef unsigned regnum_t;
1655 /* Macros for the compile stack. */
1657 /* Since offsets can go either forwards or backwards, this type needs to
1658 be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1. */
1659 typedef int pattern_offset_t;
1661 typedef struct
1663 pattern_offset_t begalt_offset;
1664 pattern_offset_t fixup_alt_jump;
1665 pattern_offset_t laststart_offset;
1666 regnum_t regnum;
1667 } compile_stack_elt_t;
1670 typedef struct
1672 compile_stack_elt_t *stack;
1673 unsigned size;
1674 unsigned avail; /* Offset of next open position. */
1675 } compile_stack_type;
1678 #define INIT_COMPILE_STACK_SIZE 32
1680 #define COMPILE_STACK_EMPTY (compile_stack.avail == 0)
1681 #define COMPILE_STACK_FULL (compile_stack.avail == compile_stack.size)
1683 /* The next available element. */
1684 #define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
1687 /* Structure to manage work area for range table. */
1688 struct range_table_work_area
1690 int *table; /* actual work area. */
1691 int allocated; /* allocated size for work area in bytes. */
1692 int used; /* actually used size in words. */
1693 int bits; /* flag to record character classes */
1696 /* Make sure that WORK_AREA can hold more N multibyte characters. */
1697 #define EXTEND_RANGE_TABLE_WORK_AREA(work_area, n) \
1698 do { \
1699 if (((work_area).used + (n)) * sizeof (int) > (work_area).allocated) \
1701 (work_area).allocated += 16 * sizeof (int); \
1702 if ((work_area).table) \
1703 (work_area).table \
1704 = (int *) realloc ((work_area).table, (work_area).allocated); \
1705 else \
1706 (work_area).table \
1707 = (int *) malloc ((work_area).allocated); \
1708 if ((work_area).table == 0) \
1709 FREE_STACK_RETURN (REG_ESPACE); \
1711 } while (0)
1713 #define SET_RANGE_TABLE_WORK_AREA_BIT(work_area, bit) \
1714 (work_area).bits |= (bit)
1716 /* These bits represent the various character classes such as [:alnum:]
1717 in a charset's range table. */
1718 #define BIT_ALNUM 0x1
1719 #define BIT_ALPHA 0x2
1720 #define BIT_WORD 0x4
1721 #define BIT_ASCII 0x8
1722 #define BIT_NONASCII 0x10
1723 #define BIT_GRAPH 0x20
1724 #define BIT_LOWER 0x40
1725 #define BIT_PRINT 0x80
1726 #define BIT_PUNCT 0x100
1727 #define BIT_SPACE 0x200
1728 #define BIT_UPPER 0x400
1729 #define BIT_UNIBYTE 0x800
1730 #define BIT_MULTIBYTE 0x1000
1732 /* Set a range (RANGE_START, RANGE_END) to WORK_AREA. */
1733 #define SET_RANGE_TABLE_WORK_AREA(work_area, range_start, range_end) \
1734 do { \
1735 EXTEND_RANGE_TABLE_WORK_AREA ((work_area), 2); \
1736 (work_area).table[(work_area).used++] = (range_start); \
1737 (work_area).table[(work_area).used++] = (range_end); \
1738 } while (0)
1740 /* Free allocated memory for WORK_AREA. */
1741 #define FREE_RANGE_TABLE_WORK_AREA(work_area) \
1742 do { \
1743 if ((work_area).table) \
1744 free ((work_area).table); \
1745 } while (0)
1747 #define CLEAR_RANGE_TABLE_WORK_USED(work_area) ((work_area).used = 0, (work_area).bits = 0)
1748 #define RANGE_TABLE_WORK_USED(work_area) ((work_area).used)
1749 #define RANGE_TABLE_WORK_BITS(work_area) ((work_area).bits)
1750 #define RANGE_TABLE_WORK_ELT(work_area, i) ((work_area).table[i])
1753 /* Set the bit for character C in a list. */
1754 #define SET_LIST_BIT(c) \
1755 (b[((unsigned char) (c)) / BYTEWIDTH] \
1756 |= 1 << (((unsigned char) c) % BYTEWIDTH))
1759 /* Get the next unsigned number in the uncompiled pattern. */
1760 #define GET_UNSIGNED_NUMBER(num) \
1761 { if (p != pend) \
1763 PATFETCH (c); \
1764 while (ISDIGIT (c)) \
1766 if (num < 0) \
1767 num = 0; \
1768 num = num * 10 + c - '0'; \
1769 if (p == pend) \
1770 break; \
1771 PATFETCH (c); \
1776 #define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
1778 #define IS_CHAR_CLASS(string) \
1779 (STREQ (string, "alpha") || STREQ (string, "upper") \
1780 || STREQ (string, "lower") || STREQ (string, "digit") \
1781 || STREQ (string, "alnum") || STREQ (string, "xdigit") \
1782 || STREQ (string, "space") || STREQ (string, "print") \
1783 || STREQ (string, "punct") || STREQ (string, "graph") \
1784 || STREQ (string, "cntrl") || STREQ (string, "blank") \
1785 || STREQ (string, "word") \
1786 || STREQ (string, "ascii") || STREQ (string, "nonascii") \
1787 || STREQ (string, "unibyte") || STREQ (string, "multibyte"))
1789 #ifndef MATCH_MAY_ALLOCATE
1791 /* If we cannot allocate large objects within re_match_2_internal,
1792 we make the fail stack and register vectors global.
1793 The fail stack, we grow to the maximum size when a regexp
1794 is compiled.
1795 The register vectors, we adjust in size each time we
1796 compile a regexp, according to the number of registers it needs. */
1798 static fail_stack_type fail_stack;
1800 /* Size with which the following vectors are currently allocated.
1801 That is so we can make them bigger as needed,
1802 but never make them smaller. */
1803 static int regs_allocated_size;
1805 static const char ** regstart, ** regend;
1806 static const char **best_regstart, **best_regend;
1808 /* Make the register vectors big enough for NUM_REGS registers,
1809 but don't make them smaller. */
1811 static
1812 regex_grow_registers (num_regs)
1813 int num_regs;
1815 if (num_regs > regs_allocated_size)
1817 RETALLOC_IF (regstart, num_regs, const char *);
1818 RETALLOC_IF (regend, num_regs, const char *);
1819 RETALLOC_IF (best_regstart, num_regs, const char *);
1820 RETALLOC_IF (best_regend, num_regs, const char *);
1822 regs_allocated_size = num_regs;
1826 #endif /* not MATCH_MAY_ALLOCATE */
1828 /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
1829 Returns one of error codes defined in `regex.h', or zero for success.
1831 Assumes the `allocated' (and perhaps `buffer') and `translate'
1832 fields are set in BUFP on entry.
1834 If it succeeds, results are put in BUFP (if it returns an error, the
1835 contents of BUFP are undefined):
1836 `buffer' is the compiled pattern;
1837 `syntax' is set to SYNTAX;
1838 `used' is set to the length of the compiled pattern;
1839 `fastmap_accurate' is zero;
1840 `re_nsub' is the number of subexpressions in PATTERN;
1841 `not_bol' and `not_eol' are zero;
1843 The `fastmap' and `newline_anchor' fields are neither
1844 examined nor set. */
1846 /* Insert the `jump' from the end of last alternative to "here".
1847 The space for the jump has already been allocated. */
1848 #define FIXUP_ALT_JUMP() \
1849 do { \
1850 if (fixup_alt_jump) \
1851 STORE_JUMP (jump, fixup_alt_jump, b); \
1852 } while (0)
1855 /* Return, freeing storage we allocated. */
1856 #define FREE_STACK_RETURN(value) \
1857 do { \
1858 FREE_RANGE_TABLE_WORK_AREA (range_table_work); \
1859 free (compile_stack.stack); \
1860 return value; \
1861 } while (0)
1863 static reg_errcode_t
1864 regex_compile (pattern, size, syntax, bufp)
1865 const char *pattern;
1866 int size;
1867 reg_syntax_t syntax;
1868 struct re_pattern_buffer *bufp;
1870 /* We fetch characters from PATTERN here. Even though PATTERN is
1871 `char *' (i.e., signed), we declare these variables as unsigned, so
1872 they can be reliably used as array indices. */
1873 register unsigned int c, c1;
1875 /* A random temporary spot in PATTERN. */
1876 const char *p1;
1878 /* Points to the end of the buffer, where we should append. */
1879 register unsigned char *b;
1881 /* Keeps track of unclosed groups. */
1882 compile_stack_type compile_stack;
1884 /* Points to the current (ending) position in the pattern. */
1885 #ifdef AIX
1886 /* `const' makes AIX compiler fail. */
1887 char *p = pattern;
1888 #else
1889 const char *p = pattern;
1890 #endif
1891 const char *pend = pattern + size;
1893 /* How to translate the characters in the pattern. */
1894 RE_TRANSLATE_TYPE translate = bufp->translate;
1896 /* Address of the count-byte of the most recently inserted `exactn'
1897 command. This makes it possible to tell if a new exact-match
1898 character can be added to that command or if the character requires
1899 a new `exactn' command. */
1900 unsigned char *pending_exact = 0;
1902 /* Address of start of the most recently finished expression.
1903 This tells, e.g., postfix * where to find the start of its
1904 operand. Reset at the beginning of groups and alternatives. */
1905 unsigned char *laststart = 0;
1907 /* Address of beginning of regexp, or inside of last group. */
1908 unsigned char *begalt;
1910 /* Place in the uncompiled pattern (i.e., the {) to
1911 which to go back if the interval is invalid. */
1912 const char *beg_interval;
1914 /* Address of the place where a forward jump should go to the end of
1915 the containing expression. Each alternative of an `or' -- except the
1916 last -- ends with a forward jump of this sort. */
1917 unsigned char *fixup_alt_jump = 0;
1919 /* Counts open-groups as they are encountered. Remembered for the
1920 matching close-group on the compile stack, so the same register
1921 number is put in the stop_memory as the start_memory. */
1922 regnum_t regnum = 0;
1924 /* Work area for range table of charset. */
1925 struct range_table_work_area range_table_work;
1927 #ifdef DEBUG
1928 /* debug = 1; */
1929 DEBUG_PRINT1 ("\nCompiling pattern: ");
1930 if (debug)
1932 unsigned debug_count;
1934 for (debug_count = 0; debug_count < size; debug_count++)
1935 putchar (pattern[debug_count]);
1936 putchar ('\n');
1938 #endif /* DEBUG */
1940 /* Initialize the compile stack. */
1941 compile_stack.stack = TALLOC (INIT_COMPILE_STACK_SIZE, compile_stack_elt_t);
1942 if (compile_stack.stack == NULL)
1943 return REG_ESPACE;
1945 compile_stack.size = INIT_COMPILE_STACK_SIZE;
1946 compile_stack.avail = 0;
1948 range_table_work.table = 0;
1949 range_table_work.allocated = 0;
1951 /* Initialize the pattern buffer. */
1952 bufp->syntax = syntax;
1953 bufp->fastmap_accurate = 0;
1954 bufp->not_bol = bufp->not_eol = 0;
1956 /* Set `used' to zero, so that if we return an error, the pattern
1957 printer (for debugging) will think there's no pattern. We reset it
1958 at the end. */
1959 bufp->used = 0;
1961 /* Always count groups, whether or not bufp->no_sub is set. */
1962 bufp->re_nsub = 0;
1964 #ifdef emacs
1965 /* bufp->multibyte is set before regex_compile is called, so don't alter
1966 it. */
1967 #else /* not emacs */
1968 /* Nothing is recognized as a multibyte character. */
1969 bufp->multibyte = 0;
1970 #endif
1972 #if !defined (emacs) && !defined (SYNTAX_TABLE)
1973 /* Initialize the syntax table. */
1974 init_syntax_once ();
1975 #endif
1977 if (bufp->allocated == 0)
1979 if (bufp->buffer)
1980 { /* If zero allocated, but buffer is non-null, try to realloc
1981 enough space. This loses if buffer's address is bogus, but
1982 that is the user's responsibility. */
1983 RETALLOC (bufp->buffer, INIT_BUF_SIZE, unsigned char);
1985 else
1986 { /* Caller did not allocate a buffer. Do it for them. */
1987 bufp->buffer = TALLOC (INIT_BUF_SIZE, unsigned char);
1989 if (!bufp->buffer) FREE_STACK_RETURN (REG_ESPACE);
1991 bufp->allocated = INIT_BUF_SIZE;
1994 begalt = b = bufp->buffer;
1996 /* Loop through the uncompiled pattern until we're at the end. */
1997 while (p != pend)
1999 PATFETCH (c);
2001 switch (c)
2003 case '^':
2005 if ( /* If at start of pattern, it's an operator. */
2006 p == pattern + 1
2007 /* If context independent, it's an operator. */
2008 || syntax & RE_CONTEXT_INDEP_ANCHORS
2009 /* Otherwise, depends on what's come before. */
2010 || at_begline_loc_p (pattern, p, syntax))
2011 BUF_PUSH (begline);
2012 else
2013 goto normal_char;
2015 break;
2018 case '$':
2020 if ( /* If at end of pattern, it's an operator. */
2021 p == pend
2022 /* If context independent, it's an operator. */
2023 || syntax & RE_CONTEXT_INDEP_ANCHORS
2024 /* Otherwise, depends on what's next. */
2025 || at_endline_loc_p (p, pend, syntax))
2026 BUF_PUSH (endline);
2027 else
2028 goto normal_char;
2030 break;
2033 case '+':
2034 case '?':
2035 if ((syntax & RE_BK_PLUS_QM)
2036 || (syntax & RE_LIMITED_OPS))
2037 goto normal_char;
2038 handle_plus:
2039 case '*':
2040 /* If there is no previous pattern... */
2041 if (!laststart)
2043 if (syntax & RE_CONTEXT_INVALID_OPS)
2044 FREE_STACK_RETURN (REG_BADRPT);
2045 else if (!(syntax & RE_CONTEXT_INDEP_OPS))
2046 goto normal_char;
2050 /* Are we optimizing this jump? */
2051 boolean keep_string_p = false;
2053 /* 1 means zero (many) matches is allowed. */
2054 char zero_times_ok = 0, many_times_ok = 0;
2055 char greedy = 1;
2057 /* If there is a sequence of repetition chars, collapse it
2058 down to just one (the right one). We can't combine
2059 interval operators with these because of, e.g., `a{2}*',
2060 which should only match an even number of `a's. */
2062 for (;;)
2064 if (!(syntax & RE_ALL_GREEDY)
2065 && c == '?' && (zero_times_ok || many_times_ok))
2066 greedy = 0;
2067 else
2069 zero_times_ok |= c != '+';
2070 many_times_ok |= c != '?';
2073 if (p == pend)
2074 break;
2076 PATFETCH (c);
2078 if (c == '*'
2079 || (!(syntax & RE_BK_PLUS_QM) && (c == '+' || c == '?')))
2082 else if (syntax & RE_BK_PLUS_QM && c == '\\')
2084 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
2086 PATFETCH (c1);
2087 if (!(c1 == '+' || c1 == '?'))
2089 PATUNFETCH;
2090 PATUNFETCH;
2091 break;
2094 c = c1;
2096 else
2098 PATUNFETCH;
2099 break;
2102 /* If we get here, we found another repeat character. */
2105 /* Star, etc. applied to an empty pattern is equivalent
2106 to an empty pattern. */
2107 if (!laststart)
2108 break;
2110 /* Now we know whether or not zero matches is allowed
2111 and also whether or not two or more matches is allowed. */
2112 if (greedy)
2114 if (many_times_ok)
2115 { /* More than one repetition is allowed, so put in at the
2116 end a backward relative jump from `b' to before the next
2117 jump we're going to put in below (which jumps from
2118 laststart to after this jump).
2120 But if we are at the `*' in the exact sequence `.*\n',
2121 insert an unconditional jump backwards to the .,
2122 instead of the beginning of the loop. This way we only
2123 push a failure point once, instead of every time
2124 through the loop. */
2125 assert (p - 1 > pattern);
2127 /* Allocate the space for the jump. */
2128 GET_BUFFER_SPACE (3);
2130 /* We know we are not at the first character of the pattern,
2131 because laststart was nonzero. And we've already
2132 incremented `p', by the way, to be the character after
2133 the `*'. Do we have to do something analogous here
2134 for null bytes, because of RE_DOT_NOT_NULL? */
2135 if (TRANSLATE ((unsigned char)*(p - 2)) == TRANSLATE ('.')
2136 && zero_times_ok
2137 && p < pend
2138 && TRANSLATE ((unsigned char)*p) == TRANSLATE ('\n')
2139 && !(syntax & RE_DOT_NEWLINE))
2140 { /* We have .*\n. */
2141 STORE_JUMP (jump, b, laststart);
2142 keep_string_p = true;
2144 else
2145 STORE_JUMP (jump, b, laststart - 3);
2147 /* We've added more stuff to the buffer. */
2148 b += 3;
2151 /* On failure, jump from laststart to b + 3, which will be the
2152 end of the buffer after this jump is inserted. */
2153 GET_BUFFER_SPACE (3);
2154 if (!zero_times_ok)
2156 assert (many_times_ok);
2157 INSERT_JUMP (on_failure_jump_smart, b - 3, b + 3);
2158 pending_exact = 0;
2159 b += 3;
2161 else
2163 INSERT_JUMP (keep_string_p ? on_failure_keep_string_jump
2164 : !many_times_ok ?
2165 on_failure_jump : on_failure_jump_smart,
2166 laststart, b + 3);
2167 pending_exact = 0;
2168 b += 3;
2171 else /* not greedy */
2172 { /* I wish the greedy and non-greedy cases could be merged. */
2174 if (many_times_ok)
2176 /* The non-greedy multiple match looks like a repeat..until:
2177 we only need a conditional jump at the end of the loop */
2178 GET_BUFFER_SPACE (3);
2179 STORE_JUMP (on_failure_jump, b, laststart);
2180 b += 3;
2181 if (zero_times_ok)
2183 /* The repeat...until naturally matches one or more.
2184 To also match zero times, we need to first jump to
2185 the end of the loop (its conditional jump). */
2186 GET_BUFFER_SPACE (3);
2187 INSERT_JUMP (jump, laststart, b);
2188 b += 3;
2191 else
2193 /* non-greedy a?? */
2194 GET_BUFFER_SPACE (6);
2195 INSERT_JUMP (jump, laststart, b + 3);
2196 b += 3;
2197 INSERT_JUMP (on_failure_jump, laststart, laststart + 6);
2198 b += 3;
2202 break;
2205 case '.':
2206 laststart = b;
2207 BUF_PUSH (anychar);
2208 break;
2211 case '[':
2213 CLEAR_RANGE_TABLE_WORK_USED (range_table_work);
2215 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2217 /* Ensure that we have enough space to push a charset: the
2218 opcode, the length count, and the bitset; 34 bytes in all. */
2219 GET_BUFFER_SPACE (34);
2221 laststart = b;
2223 /* We test `*p == '^' twice, instead of using an if
2224 statement, so we only need one BUF_PUSH. */
2225 BUF_PUSH (*p == '^' ? charset_not : charset);
2226 if (*p == '^')
2227 p++;
2229 /* Remember the first position in the bracket expression. */
2230 p1 = p;
2232 /* Push the number of bytes in the bitmap. */
2233 BUF_PUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
2235 /* Clear the whole map. */
2236 bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH);
2238 /* charset_not matches newline according to a syntax bit. */
2239 if ((re_opcode_t) b[-2] == charset_not
2240 && (syntax & RE_HAT_LISTS_NOT_NEWLINE))
2241 SET_LIST_BIT ('\n');
2243 /* Read in characters and ranges, setting map bits. */
2244 for (;;)
2246 int len;
2247 boolean escaped_char = false;
2249 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2251 PATFETCH (c);
2253 /* \ might escape characters inside [...] and [^...]. */
2254 if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) && c == '\\')
2256 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
2258 PATFETCH (c);
2259 escaped_char = true;
2261 else
2263 /* Could be the end of the bracket expression. If it's
2264 not (i.e., when the bracket expression is `[]' so
2265 far), the ']' character bit gets set way below. */
2266 if (c == ']' && p != p1 + 1)
2267 break;
2270 /* If C indicates start of multibyte char, get the
2271 actual character code in C, and set the pattern
2272 pointer P to the next character boundary. */
2273 if (bufp->multibyte && BASE_LEADING_CODE_P (c))
2275 PATUNFETCH;
2276 c = STRING_CHAR_AND_LENGTH (p, pend - p, len);
2277 p += len;
2279 /* What should we do for the character which is
2280 greater than 0x7F, but not BASE_LEADING_CODE_P?
2281 XXX */
2283 /* See if we're at the beginning of a possible character
2284 class. */
2286 else if (!escaped_char &&
2287 syntax & RE_CHAR_CLASSES && c == '[' && *p == ':')
2289 /* Leave room for the null. */
2290 char str[CHAR_CLASS_MAX_LENGTH + 1];
2292 PATFETCH (c);
2293 c1 = 0;
2295 /* If pattern is `[[:'. */
2296 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2298 for (;;)
2300 PATFETCH (c);
2301 if (c == ':' || c == ']' || p == pend
2302 || c1 == CHAR_CLASS_MAX_LENGTH)
2303 break;
2304 str[c1++] = c;
2306 str[c1] = '\0';
2308 /* If isn't a word bracketed by `[:' and `:]':
2309 undo the ending character, the letters, and
2310 leave the leading `:' and `[' (but set bits for
2311 them). */
2312 if (c == ':' && *p == ']')
2314 int ch;
2315 boolean is_alnum = STREQ (str, "alnum");
2316 boolean is_alpha = STREQ (str, "alpha");
2317 boolean is_ascii = STREQ (str, "ascii");
2318 boolean is_blank = STREQ (str, "blank");
2319 boolean is_cntrl = STREQ (str, "cntrl");
2320 boolean is_digit = STREQ (str, "digit");
2321 boolean is_graph = STREQ (str, "graph");
2322 boolean is_lower = STREQ (str, "lower");
2323 boolean is_multibyte = STREQ (str, "multibyte");
2324 boolean is_nonascii = STREQ (str, "nonascii");
2325 boolean is_print = STREQ (str, "print");
2326 boolean is_punct = STREQ (str, "punct");
2327 boolean is_space = STREQ (str, "space");
2328 boolean is_unibyte = STREQ (str, "unibyte");
2329 boolean is_upper = STREQ (str, "upper");
2330 boolean is_word = STREQ (str, "word");
2331 boolean is_xdigit = STREQ (str, "xdigit");
2333 if (!IS_CHAR_CLASS (str))
2334 FREE_STACK_RETURN (REG_ECTYPE);
2336 /* Throw away the ] at the end of the character
2337 class. */
2338 PATFETCH (c);
2340 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2342 /* Most character classes in a multibyte match
2343 just set a flag. Exceptions are is_blank,
2344 is_digit, is_cntrl, and is_xdigit, since
2345 they can only match ASCII characters. We
2346 don't need to handle them for multibyte. */
2348 if (bufp->multibyte)
2350 int bit = 0;
2352 if (is_alnum) bit = BIT_ALNUM;
2353 if (is_alpha) bit = BIT_ALPHA;
2354 if (is_ascii) bit = BIT_ASCII;
2355 if (is_graph) bit = BIT_GRAPH;
2356 if (is_lower) bit = BIT_LOWER;
2357 if (is_multibyte) bit = BIT_MULTIBYTE;
2358 if (is_nonascii) bit = BIT_NONASCII;
2359 if (is_print) bit = BIT_PRINT;
2360 if (is_punct) bit = BIT_PUNCT;
2361 if (is_space) bit = BIT_SPACE;
2362 if (is_unibyte) bit = BIT_UNIBYTE;
2363 if (is_upper) bit = BIT_UPPER;
2364 if (is_word) bit = BIT_WORD;
2365 if (bit)
2366 SET_RANGE_TABLE_WORK_AREA_BIT (range_table_work,
2367 bit);
2370 /* Handle character classes for ASCII characters. */
2371 for (ch = 0; ch < 1 << BYTEWIDTH; ch++)
2373 int translated = TRANSLATE (ch);
2374 /* This was split into 3 if's to
2375 avoid an arbitrary limit in some compiler. */
2376 if ( (is_alnum && ISALNUM (ch))
2377 || (is_alpha && ISALPHA (ch))
2378 || (is_blank && ISBLANK (ch))
2379 || (is_cntrl && ISCNTRL (ch)))
2380 SET_LIST_BIT (translated);
2381 if ( (is_digit && ISDIGIT (ch))
2382 || (is_graph && ISGRAPH (ch))
2383 || (is_lower && ISLOWER (ch))
2384 || (is_print && ISPRINT (ch)))
2385 SET_LIST_BIT (translated);
2386 if ( (is_punct && ISPUNCT (ch))
2387 || (is_space && ISSPACE (ch))
2388 || (is_upper && ISUPPER (ch))
2389 || (is_xdigit && ISXDIGIT (ch)))
2390 SET_LIST_BIT (translated);
2391 if ( (is_ascii && IS_REAL_ASCII (ch))
2392 || (is_nonascii && !IS_REAL_ASCII (ch))
2393 || (is_unibyte && ISUNIBYTE (ch))
2394 || (is_multibyte && !ISUNIBYTE (ch)))
2395 SET_LIST_BIT (translated);
2397 if ( (is_word && ISWORD (ch)))
2398 SET_LIST_BIT (translated);
2401 /* Repeat the loop. */
2402 continue;
2404 else
2406 c1++;
2407 while (c1--)
2408 PATUNFETCH;
2409 SET_LIST_BIT ('[');
2411 /* Because the `:' may starts the range, we
2412 can't simply set bit and repeat the loop.
2413 Instead, just set it to C and handle below. */
2414 c = ':';
2418 if (p < pend && p[0] == '-' && p[1] != ']')
2421 /* Discard the `-'. */
2422 PATFETCH (c1);
2424 /* Fetch the character which ends the range. */
2425 PATFETCH (c1);
2426 if (bufp->multibyte && BASE_LEADING_CODE_P (c1))
2428 PATUNFETCH;
2429 c1 = STRING_CHAR_AND_LENGTH (p, pend - p, len);
2430 p += len;
2433 if (SINGLE_BYTE_CHAR_P (c)
2434 && ! SINGLE_BYTE_CHAR_P (c1))
2436 /* Handle a range such as \177-\377 in multibyte mode.
2437 Split that into two ranges,,
2438 the low one ending at 0237, and the high one
2439 starting at ...040. */
2440 /* Unless I'm missing something,
2441 this line is useless. -sm
2442 int c1_base = (c1 & ~0177) | 040; */
2443 SET_RANGE_TABLE_WORK_AREA (range_table_work, c, c1);
2444 c1 = 0237;
2446 else if (!SAME_CHARSET_P (c, c1))
2447 FREE_STACK_RETURN (REG_ERANGE);
2449 else
2450 /* Range from C to C. */
2451 c1 = c;
2453 /* Set the range ... */
2454 if (SINGLE_BYTE_CHAR_P (c))
2455 /* ... into bitmap. */
2457 unsigned this_char;
2458 int range_start = c, range_end = c1;
2460 /* If the start is after the end, the range is empty. */
2461 if (range_start > range_end)
2463 if (syntax & RE_NO_EMPTY_RANGES)
2464 FREE_STACK_RETURN (REG_ERANGE);
2465 /* Else, repeat the loop. */
2467 else
2469 for (this_char = range_start; this_char <= range_end;
2470 this_char++)
2471 SET_LIST_BIT (TRANSLATE (this_char));
2474 else
2475 /* ... into range table. */
2476 SET_RANGE_TABLE_WORK_AREA (range_table_work, c, c1);
2479 /* Discard any (non)matching list bytes that are all 0 at the
2480 end of the map. Decrease the map-length byte too. */
2481 while ((int) b[-1] > 0 && b[b[-1] - 1] == 0)
2482 b[-1]--;
2483 b += b[-1];
2485 /* Build real range table from work area. */
2486 if (RANGE_TABLE_WORK_USED (range_table_work)
2487 || RANGE_TABLE_WORK_BITS (range_table_work))
2489 int i;
2490 int used = RANGE_TABLE_WORK_USED (range_table_work);
2492 /* Allocate space for COUNT + RANGE_TABLE. Needs two
2493 bytes for flags, two for COUNT, and three bytes for
2494 each character. */
2495 GET_BUFFER_SPACE (4 + used * 3);
2497 /* Indicate the existence of range table. */
2498 laststart[1] |= 0x80;
2500 /* Store the character class flag bits into the range table.
2501 If not in emacs, these flag bits are always 0. */
2502 *b++ = RANGE_TABLE_WORK_BITS (range_table_work) & 0xff;
2503 *b++ = RANGE_TABLE_WORK_BITS (range_table_work) >> 8;
2505 STORE_NUMBER_AND_INCR (b, used / 2);
2506 for (i = 0; i < used; i++)
2507 STORE_CHARACTER_AND_INCR
2508 (b, RANGE_TABLE_WORK_ELT (range_table_work, i));
2511 break;
2514 case '(':
2515 if (syntax & RE_NO_BK_PARENS)
2516 goto handle_open;
2517 else
2518 goto normal_char;
2521 case ')':
2522 if (syntax & RE_NO_BK_PARENS)
2523 goto handle_close;
2524 else
2525 goto normal_char;
2528 case '\n':
2529 if (syntax & RE_NEWLINE_ALT)
2530 goto handle_alt;
2531 else
2532 goto normal_char;
2535 case '|':
2536 if (syntax & RE_NO_BK_VBAR)
2537 goto handle_alt;
2538 else
2539 goto normal_char;
2542 case '{':
2543 if (syntax & RE_INTERVALS && syntax & RE_NO_BK_BRACES)
2544 goto handle_interval;
2545 else
2546 goto normal_char;
2549 case '\\':
2550 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
2552 /* Do not translate the character after the \, so that we can
2553 distinguish, e.g., \B from \b, even if we normally would
2554 translate, e.g., B to b. */
2555 PATFETCH_RAW (c);
2557 switch (c)
2559 case '(':
2560 if (syntax & RE_NO_BK_PARENS)
2561 goto normal_backslash;
2563 handle_open:
2565 int shy = 0;
2566 if (p+1 < pend)
2568 /* Look for a special (?...) construct */
2569 PATFETCH (c);
2570 if ((syntax & RE_SHY_GROUPS) && c == '?')
2572 PATFETCH (c);
2573 switch (c)
2575 case ':': shy = 1; break;
2576 default:
2577 /* Only (?:...) is supported right now. */
2578 FREE_STACK_RETURN (REG_BADPAT);
2581 else PATUNFETCH;
2584 if (!shy)
2586 bufp->re_nsub++;
2587 regnum++;
2590 if (COMPILE_STACK_FULL)
2592 RETALLOC (compile_stack.stack, compile_stack.size << 1,
2593 compile_stack_elt_t);
2594 if (compile_stack.stack == NULL) return REG_ESPACE;
2596 compile_stack.size <<= 1;
2599 /* These are the values to restore when we hit end of this
2600 group. They are all relative offsets, so that if the
2601 whole pattern moves because of realloc, they will still
2602 be valid. */
2603 COMPILE_STACK_TOP.begalt_offset = begalt - bufp->buffer;
2604 COMPILE_STACK_TOP.fixup_alt_jump
2605 = fixup_alt_jump ? fixup_alt_jump - bufp->buffer + 1 : 0;
2606 COMPILE_STACK_TOP.laststart_offset = b - bufp->buffer;
2607 COMPILE_STACK_TOP.regnum = shy ? -regnum : regnum;
2609 /* Do not push a
2610 start_memory for groups beyond the last one we can
2611 represent in the compiled pattern. */
2612 if (regnum <= MAX_REGNUM && !shy)
2613 BUF_PUSH_2 (start_memory, regnum);
2615 compile_stack.avail++;
2617 fixup_alt_jump = 0;
2618 laststart = 0;
2619 begalt = b;
2620 /* If we've reached MAX_REGNUM groups, then this open
2621 won't actually generate any code, so we'll have to
2622 clear pending_exact explicitly. */
2623 pending_exact = 0;
2624 break;
2627 case ')':
2628 if (syntax & RE_NO_BK_PARENS) goto normal_backslash;
2630 if (COMPILE_STACK_EMPTY)
2632 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
2633 goto normal_backslash;
2634 else
2635 FREE_STACK_RETURN (REG_ERPAREN);
2638 handle_close:
2639 FIXUP_ALT_JUMP ();
2641 /* See similar code for backslashed left paren above. */
2642 if (COMPILE_STACK_EMPTY)
2644 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
2645 goto normal_char;
2646 else
2647 FREE_STACK_RETURN (REG_ERPAREN);
2650 /* Since we just checked for an empty stack above, this
2651 ``can't happen''. */
2652 assert (compile_stack.avail != 0);
2654 /* We don't just want to restore into `regnum', because
2655 later groups should continue to be numbered higher,
2656 as in `(ab)c(de)' -- the second group is #2. */
2657 regnum_t this_group_regnum;
2659 compile_stack.avail--;
2660 begalt = bufp->buffer + COMPILE_STACK_TOP.begalt_offset;
2661 fixup_alt_jump
2662 = COMPILE_STACK_TOP.fixup_alt_jump
2663 ? bufp->buffer + COMPILE_STACK_TOP.fixup_alt_jump - 1
2664 : 0;
2665 laststart = bufp->buffer + COMPILE_STACK_TOP.laststart_offset;
2666 this_group_regnum = COMPILE_STACK_TOP.regnum;
2667 /* If we've reached MAX_REGNUM groups, then this open
2668 won't actually generate any code, so we'll have to
2669 clear pending_exact explicitly. */
2670 pending_exact = 0;
2672 /* We're at the end of the group, so now we know how many
2673 groups were inside this one. */
2674 if (this_group_regnum <= MAX_REGNUM && this_group_regnum > 0)
2675 BUF_PUSH_2 (stop_memory, this_group_regnum);
2677 break;
2680 case '|': /* `\|'. */
2681 if (syntax & RE_LIMITED_OPS || syntax & RE_NO_BK_VBAR)
2682 goto normal_backslash;
2683 handle_alt:
2684 if (syntax & RE_LIMITED_OPS)
2685 goto normal_char;
2687 /* Insert before the previous alternative a jump which
2688 jumps to this alternative if the former fails. */
2689 GET_BUFFER_SPACE (3);
2690 INSERT_JUMP (on_failure_jump, begalt, b + 6);
2691 pending_exact = 0;
2692 b += 3;
2694 /* The alternative before this one has a jump after it
2695 which gets executed if it gets matched. Adjust that
2696 jump so it will jump to this alternative's analogous
2697 jump (put in below, which in turn will jump to the next
2698 (if any) alternative's such jump, etc.). The last such
2699 jump jumps to the correct final destination. A picture:
2700 _____ _____
2701 | | | |
2702 | v | v
2703 a | b | c
2705 If we are at `b', then fixup_alt_jump right now points to a
2706 three-byte space after `a'. We'll put in the jump, set
2707 fixup_alt_jump to right after `b', and leave behind three
2708 bytes which we'll fill in when we get to after `c'. */
2710 FIXUP_ALT_JUMP ();
2712 /* Mark and leave space for a jump after this alternative,
2713 to be filled in later either by next alternative or
2714 when know we're at the end of a series of alternatives. */
2715 fixup_alt_jump = b;
2716 GET_BUFFER_SPACE (3);
2717 b += 3;
2719 laststart = 0;
2720 begalt = b;
2721 break;
2724 case '{':
2725 /* If \{ is a literal. */
2726 if (!(syntax & RE_INTERVALS)
2727 /* If we're at `\{' and it's not the open-interval
2728 operator. */
2729 || ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
2730 || (p - 2 == pattern && p == pend))
2731 goto normal_backslash;
2733 handle_interval:
2735 /* If got here, then the syntax allows intervals. */
2737 /* At least (most) this many matches must be made. */
2738 int lower_bound = -1, upper_bound = -1;
2740 beg_interval = p - 1;
2742 if (p == pend)
2744 if (syntax & RE_NO_BK_BRACES)
2745 goto unfetch_interval;
2746 else
2747 FREE_STACK_RETURN (REG_EBRACE);
2750 GET_UNSIGNED_NUMBER (lower_bound);
2752 if (c == ',')
2754 GET_UNSIGNED_NUMBER (upper_bound);
2755 if (upper_bound < 0) upper_bound = RE_DUP_MAX;
2757 else
2758 /* Interval such as `{1}' => match exactly once. */
2759 upper_bound = lower_bound;
2761 if (lower_bound < 0 || upper_bound > RE_DUP_MAX
2762 || lower_bound > upper_bound)
2764 if (syntax & RE_NO_BK_BRACES)
2765 goto unfetch_interval;
2766 else
2767 FREE_STACK_RETURN (REG_BADBR);
2770 if (!(syntax & RE_NO_BK_BRACES))
2772 if (c != '\\') FREE_STACK_RETURN (REG_EBRACE);
2774 PATFETCH (c);
2777 if (c != '}')
2779 if (syntax & RE_NO_BK_BRACES)
2780 goto unfetch_interval;
2781 else
2782 FREE_STACK_RETURN (REG_BADBR);
2785 /* We just parsed a valid interval. */
2787 /* If it's invalid to have no preceding re. */
2788 if (!laststart)
2790 if (syntax & RE_CONTEXT_INVALID_OPS)
2791 FREE_STACK_RETURN (REG_BADRPT);
2792 else if (syntax & RE_CONTEXT_INDEP_OPS)
2793 laststart = b;
2794 else
2795 goto unfetch_interval;
2798 /* If the upper bound is zero, don't want to succeed at
2799 all; jump from `laststart' to `b + 3', which will be
2800 the end of the buffer after we insert the jump. */
2801 if (upper_bound == 0)
2803 GET_BUFFER_SPACE (3);
2804 INSERT_JUMP (jump, laststart, b + 3);
2805 b += 3;
2808 /* Otherwise, we have a nontrivial interval. When
2809 we're all done, the pattern will look like:
2810 set_number_at <jump count> <upper bound>
2811 set_number_at <succeed_n count> <lower bound>
2812 succeed_n <after jump addr> <succeed_n count>
2813 <body of loop>
2814 jump_n <succeed_n addr> <jump count>
2815 (The upper bound and `jump_n' are omitted if
2816 `upper_bound' is 1, though.) */
2817 else
2818 { /* If the upper bound is > 1, we need to insert
2819 more at the end of the loop. */
2820 unsigned nbytes = 10 + (upper_bound > 1) * 10;
2822 GET_BUFFER_SPACE (nbytes);
2824 /* Initialize lower bound of the `succeed_n', even
2825 though it will be set during matching by its
2826 attendant `set_number_at' (inserted next),
2827 because `re_compile_fastmap' needs to know.
2828 Jump to the `jump_n' we might insert below. */
2829 INSERT_JUMP2 (succeed_n, laststart,
2830 b + 5 + (upper_bound > 1) * 5,
2831 lower_bound);
2832 b += 5;
2834 /* Code to initialize the lower bound. Insert
2835 before the `succeed_n'. The `5' is the last two
2836 bytes of this `set_number_at', plus 3 bytes of
2837 the following `succeed_n'. */
2838 insert_op2 (set_number_at, laststart, 5, lower_bound, b);
2839 b += 5;
2841 if (upper_bound > 1)
2842 { /* More than one repetition is allowed, so
2843 append a backward jump to the `succeed_n'
2844 that starts this interval.
2846 When we've reached this during matching,
2847 we'll have matched the interval once, so
2848 jump back only `upper_bound - 1' times. */
2849 STORE_JUMP2 (jump_n, b, laststart + 5,
2850 upper_bound - 1);
2851 b += 5;
2853 /* The location we want to set is the second
2854 parameter of the `jump_n'; that is `b-2' as
2855 an absolute address. `laststart' will be
2856 the `set_number_at' we're about to insert;
2857 `laststart+3' the number to set, the source
2858 for the relative address. But we are
2859 inserting into the middle of the pattern --
2860 so everything is getting moved up by 5.
2861 Conclusion: (b - 2) - (laststart + 3) + 5,
2862 i.e., b - laststart.
2864 We insert this at the beginning of the loop
2865 so that if we fail during matching, we'll
2866 reinitialize the bounds. */
2867 insert_op2 (set_number_at, laststart, b - laststart,
2868 upper_bound - 1, b);
2869 b += 5;
2872 pending_exact = 0;
2873 beg_interval = NULL;
2875 break;
2877 unfetch_interval:
2878 /* If an invalid interval, match the characters as literals. */
2879 assert (beg_interval);
2880 p = beg_interval;
2881 beg_interval = NULL;
2883 /* normal_char and normal_backslash need `c'. */
2884 PATFETCH (c);
2886 if (!(syntax & RE_NO_BK_BRACES))
2888 if (p > pattern && p[-1] == '\\')
2889 goto normal_backslash;
2891 goto normal_char;
2893 #ifdef emacs
2894 /* There is no way to specify the before_dot and after_dot
2895 operators. rms says this is ok. --karl */
2896 case '=':
2897 BUF_PUSH (at_dot);
2898 break;
2900 case 's':
2901 laststart = b;
2902 PATFETCH (c);
2903 BUF_PUSH_2 (syntaxspec, syntax_spec_code[c]);
2904 break;
2906 case 'S':
2907 laststart = b;
2908 PATFETCH (c);
2909 BUF_PUSH_2 (notsyntaxspec, syntax_spec_code[c]);
2910 break;
2912 case 'c':
2913 laststart = b;
2914 PATFETCH_RAW (c);
2915 BUF_PUSH_2 (categoryspec, c);
2916 break;
2918 case 'C':
2919 laststart = b;
2920 PATFETCH_RAW (c);
2921 BUF_PUSH_2 (notcategoryspec, c);
2922 break;
2923 #endif /* emacs */
2926 case 'w':
2927 laststart = b;
2928 BUF_PUSH (wordchar);
2929 break;
2932 case 'W':
2933 laststart = b;
2934 BUF_PUSH (notwordchar);
2935 break;
2938 case '<':
2939 BUF_PUSH (wordbeg);
2940 break;
2942 case '>':
2943 BUF_PUSH (wordend);
2944 break;
2946 case 'b':
2947 BUF_PUSH (wordbound);
2948 break;
2950 case 'B':
2951 BUF_PUSH (notwordbound);
2952 break;
2954 case '`':
2955 BUF_PUSH (begbuf);
2956 break;
2958 case '\'':
2959 BUF_PUSH (endbuf);
2960 break;
2962 case '1': case '2': case '3': case '4': case '5':
2963 case '6': case '7': case '8': case '9':
2964 if (syntax & RE_NO_BK_REFS)
2965 goto normal_char;
2967 c1 = c - '0';
2969 if (c1 > regnum)
2970 FREE_STACK_RETURN (REG_ESUBREG);
2972 /* Can't back reference to a subexpression if inside of it. */
2973 if (group_in_compile_stack (compile_stack, c1))
2974 goto normal_char;
2976 laststart = b;
2977 BUF_PUSH_2 (duplicate, c1);
2978 break;
2981 case '+':
2982 case '?':
2983 if (syntax & RE_BK_PLUS_QM)
2984 goto handle_plus;
2985 else
2986 goto normal_backslash;
2988 default:
2989 normal_backslash:
2990 /* You might think it would be useful for \ to mean
2991 not to translate; but if we don't translate it
2992 it will never match anything. */
2993 c = TRANSLATE (c);
2994 goto normal_char;
2996 break;
2999 default:
3000 /* Expects the character in `c'. */
3001 normal_char:
3002 p1 = p - 1; /* P1 points the head of C. */
3003 #ifdef emacs
3004 if (bufp->multibyte)
3006 c = STRING_CHAR (p1, pend - p1);
3007 c = TRANSLATE (c);
3008 /* Set P to the next character boundary. */
3009 p += MULTIBYTE_FORM_LENGTH (p1, pend - p1) - 1;
3011 #endif
3012 /* If no exactn currently being built. */
3013 if (!pending_exact
3015 /* If last exactn not at current position. */
3016 || pending_exact + *pending_exact + 1 != b
3018 /* We have only one byte following the exactn for the count. */
3019 || *pending_exact >= (1 << BYTEWIDTH) - (p - p1)
3021 /* If followed by a repetition operator. */
3022 || (p != pend && (*p == '*' || *p == '^'))
3023 || ((syntax & RE_BK_PLUS_QM)
3024 ? p + 1 < pend && *p == '\\' && (p[1] == '+' || p[1] == '?')
3025 : p != pend && (*p == '+' || *p == '?'))
3026 || ((syntax & RE_INTERVALS)
3027 && ((syntax & RE_NO_BK_BRACES)
3028 ? p != pend && *p == '{'
3029 : p + 1 < pend && p[0] == '\\' && p[1] == '{')))
3031 /* Start building a new exactn. */
3033 laststart = b;
3035 BUF_PUSH_2 (exactn, 0);
3036 pending_exact = b - 1;
3039 #ifdef emacs
3040 if (! SINGLE_BYTE_CHAR_P (c))
3042 unsigned char str[MAX_MULTIBYTE_LENGTH];
3043 int i = CHAR_STRING (c, str);
3044 int j;
3045 for (j = 0; j < i; j++)
3047 BUF_PUSH (str[j]);
3048 (*pending_exact)++;
3051 else
3052 #endif
3054 BUF_PUSH (c);
3055 (*pending_exact)++;
3057 break;
3058 } /* switch (c) */
3059 } /* while p != pend */
3062 /* Through the pattern now. */
3064 FIXUP_ALT_JUMP ();
3066 if (!COMPILE_STACK_EMPTY)
3067 FREE_STACK_RETURN (REG_EPAREN);
3069 /* If we don't want backtracking, force success
3070 the first time we reach the end of the compiled pattern. */
3071 if (syntax & RE_NO_POSIX_BACKTRACKING)
3072 BUF_PUSH (succeed);
3074 free (compile_stack.stack);
3076 /* We have succeeded; set the length of the buffer. */
3077 bufp->used = b - bufp->buffer;
3079 #ifdef DEBUG
3080 if (debug)
3082 re_compile_fastmap (bufp);
3083 DEBUG_PRINT1 ("\nCompiled pattern: \n");
3084 print_compiled_pattern (bufp);
3085 /* debug = 0; */
3087 #endif /* DEBUG */
3089 #ifndef MATCH_MAY_ALLOCATE
3090 /* Initialize the failure stack to the largest possible stack. This
3091 isn't necessary unless we're trying to avoid calling alloca in
3092 the search and match routines. */
3094 int num_regs = bufp->re_nsub + 1;
3096 if (fail_stack.size < re_max_failures * TYPICAL_FAILURE_SIZE)
3098 fail_stack.size = re_max_failures * TYPICAL_FAILURE_SIZE;
3100 if (! fail_stack.stack)
3101 fail_stack.stack
3102 = (fail_stack_elt_t *) malloc (fail_stack.size
3103 * sizeof (fail_stack_elt_t));
3104 else
3105 fail_stack.stack
3106 = (fail_stack_elt_t *) realloc (fail_stack.stack,
3107 (fail_stack.size
3108 * sizeof (fail_stack_elt_t)));
3111 regex_grow_registers (num_regs);
3113 #endif /* not MATCH_MAY_ALLOCATE */
3115 return REG_NOERROR;
3116 } /* regex_compile */
3118 /* Subroutines for `regex_compile'. */
3120 /* Store OP at LOC followed by two-byte integer parameter ARG. */
3122 static void
3123 store_op1 (op, loc, arg)
3124 re_opcode_t op;
3125 unsigned char *loc;
3126 int arg;
3128 *loc = (unsigned char) op;
3129 STORE_NUMBER (loc + 1, arg);
3133 /* Like `store_op1', but for two two-byte parameters ARG1 and ARG2. */
3135 static void
3136 store_op2 (op, loc, arg1, arg2)
3137 re_opcode_t op;
3138 unsigned char *loc;
3139 int arg1, arg2;
3141 *loc = (unsigned char) op;
3142 STORE_NUMBER (loc + 1, arg1);
3143 STORE_NUMBER (loc + 3, arg2);
3147 /* Copy the bytes from LOC to END to open up three bytes of space at LOC
3148 for OP followed by two-byte integer parameter ARG. */
3150 static void
3151 insert_op1 (op, loc, arg, end)
3152 re_opcode_t op;
3153 unsigned char *loc;
3154 int arg;
3155 unsigned char *end;
3157 register unsigned char *pfrom = end;
3158 register unsigned char *pto = end + 3;
3160 while (pfrom != loc)
3161 *--pto = *--pfrom;
3163 store_op1 (op, loc, arg);
3167 /* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2. */
3169 static void
3170 insert_op2 (op, loc, arg1, arg2, end)
3171 re_opcode_t op;
3172 unsigned char *loc;
3173 int arg1, arg2;
3174 unsigned char *end;
3176 register unsigned char *pfrom = end;
3177 register unsigned char *pto = end + 5;
3179 while (pfrom != loc)
3180 *--pto = *--pfrom;
3182 store_op2 (op, loc, arg1, arg2);
3186 /* P points to just after a ^ in PATTERN. Return true if that ^ comes
3187 after an alternative or a begin-subexpression. We assume there is at
3188 least one character before the ^. */
3190 static boolean
3191 at_begline_loc_p (pattern, p, syntax)
3192 const char *pattern, *p;
3193 reg_syntax_t syntax;
3195 const char *prev = p - 2;
3196 boolean prev_prev_backslash = prev > pattern && prev[-1] == '\\';
3198 return
3199 /* After a subexpression? */
3200 (*prev == '(' && (syntax & RE_NO_BK_PARENS || prev_prev_backslash))
3201 /* After an alternative? */
3202 || (*prev == '|' && (syntax & RE_NO_BK_VBAR || prev_prev_backslash));
3206 /* The dual of at_begline_loc_p. This one is for $. We assume there is
3207 at least one character after the $, i.e., `P < PEND'. */
3209 static boolean
3210 at_endline_loc_p (p, pend, syntax)
3211 const char *p, *pend;
3212 int syntax;
3214 const char *next = p;
3215 boolean next_backslash = *next == '\\';
3216 const char *next_next = p + 1 < pend ? p + 1 : 0;
3218 return
3219 /* Before a subexpression? */
3220 (syntax & RE_NO_BK_PARENS ? *next == ')'
3221 : next_backslash && next_next && *next_next == ')')
3222 /* Before an alternative? */
3223 || (syntax & RE_NO_BK_VBAR ? *next == '|'
3224 : next_backslash && next_next && *next_next == '|');
3228 /* Returns true if REGNUM is in one of COMPILE_STACK's elements and
3229 false if it's not. */
3231 static boolean
3232 group_in_compile_stack (compile_stack, regnum)
3233 compile_stack_type compile_stack;
3234 regnum_t regnum;
3236 int this_element;
3238 for (this_element = compile_stack.avail - 1;
3239 this_element >= 0;
3240 this_element--)
3241 if (compile_stack.stack[this_element].regnum == regnum)
3242 return true;
3244 return false;
3247 /* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
3248 BUFP. A fastmap records which of the (1 << BYTEWIDTH) possible
3249 characters can start a string that matches the pattern. This fastmap
3250 is used by re_search to skip quickly over impossible starting points.
3252 Character codes above (1 << BYTEWIDTH) are not represented in the
3253 fastmap, but the leading codes are represented. Thus, the fastmap
3254 indicates which character sets could start a match.
3256 The caller must supply the address of a (1 << BYTEWIDTH)-byte data
3257 area as BUFP->fastmap.
3259 We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in
3260 the pattern buffer.
3262 Returns 0 if we succeed, -2 if an internal error. */
3265 re_compile_fastmap (bufp)
3266 struct re_pattern_buffer *bufp;
3268 int j, k;
3269 #ifdef MATCH_MAY_ALLOCATE
3270 fail_stack_type fail_stack;
3271 #endif
3272 #ifndef REGEX_MALLOC
3273 char *destination;
3274 #endif
3276 register char *fastmap = bufp->fastmap;
3277 unsigned char *pattern = bufp->buffer;
3278 unsigned long size = bufp->used;
3279 unsigned char *p = pattern;
3280 register unsigned char *pend = pattern + size;
3282 /* This holds the pointer to the failure stack, when
3283 it is allocated relocatably. */
3284 fail_stack_elt_t *failure_stack_ptr;
3286 /* Assume that each path through the pattern can be null until
3287 proven otherwise. We set this false at the bottom of switch
3288 statement, to which we get only if a particular path doesn't
3289 match the empty string. */
3290 boolean path_can_be_null = true;
3292 /* We aren't doing a `succeed_n' to begin with. */
3293 boolean succeed_n_p = false;
3295 /* If all elements for base leading-codes in fastmap is set, this
3296 flag is set true. */
3297 boolean match_any_multibyte_characters = false;
3299 /* Maximum code of simple (single byte) character. */
3300 int simple_char_max;
3302 assert (fastmap != NULL && p != NULL);
3304 INIT_FAIL_STACK ();
3305 bzero (fastmap, 1 << BYTEWIDTH); /* Assume nothing's valid. */
3306 bufp->fastmap_accurate = 1; /* It will be when we're done. */
3307 bufp->can_be_null = 0;
3309 /* The loop below works as follows:
3310 - It has a working-list kept in the PATTERN_STACK and which basically
3311 starts by only containing a pointer to the first operation.
3312 - If the opcode we're looking at is a match against some set of
3313 chars, then we add those chars to the fastmap and go on to the
3314 next work element from the worklist (done via `break').
3315 - If the opcode is a control operator on the other hand, we either
3316 ignore it (if it's meaningless at this point, such as `start_memory')
3317 or execute it (if it's a jump). If the jump has several destinations
3318 (i.e. `on_failure_jump'), then we push the other destination onto the
3319 worklist.
3320 We guarantee termination by ignoring backward jumps (more or less),
3321 so that `p' is monotonically increasing. More to the point, we
3322 never set `p' (or push) anything `<= p1'. */
3324 /* If can_be_null is set, then the fastmap will not be used anyway. */
3325 while (!bufp->can_be_null)
3327 /* `p1' is used as a marker of how far back a `on_failure_jump'
3328 can go without being ignored. It is normally equal to `p'
3329 (which prevents any backward `on_failure_jump') except right
3330 after a plain `jump', to allow patterns such as:
3331 0: jump 10
3332 3..9: <body>
3333 10: on_failure_jump 3
3334 as used for the *? operator. */
3335 unsigned char *p1 = p;
3337 if (p == pend || *p == succeed)
3339 /* We have reached the (effective) end of pattern. */
3340 if (!PATTERN_STACK_EMPTY ())
3342 bufp->can_be_null |= path_can_be_null;
3344 /* Reset for next path. */
3345 path_can_be_null = true;
3347 p = POP_PATTERN_OP ();
3349 continue;
3351 else
3352 break;
3355 /* We should never be about to go beyond the end of the pattern. */
3356 assert (p < pend);
3358 switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++))
3361 case duplicate:
3362 /* If the first character has to match a backreference, that means
3363 that the group was empty (since it already matched). Since this
3364 is the only case that interests us here, we can assume that the
3365 backreference must match the empty string. */
3366 p++;
3367 continue;
3370 /* Following are the cases which match a character. These end
3371 with `break'. */
3373 case exactn:
3374 fastmap[p[1]] = 1;
3375 break;
3378 #ifndef emacs
3379 case charset:
3381 int length = (*p & 0x7f);;
3382 p++;
3384 for (j = length * BYTEWIDTH - 1; j >= 0; j--)
3385 if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
3386 fastmap[j] = 1;
3388 break;
3390 case charset_not:
3391 /* Chars beyond end of map must be allowed. */
3393 int length = (*p & 0x7f);;
3394 p++;
3396 for (j = length * BYTEWIDTH; j < (1 << BYTEWIDTH); j++)
3397 fastmap[j] = 1;
3399 for (j = length * BYTEWIDTH - 1; j >= 0; j--)
3400 if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
3401 fastmap[j] = 1;
3403 break;
3405 case wordchar:
3406 for (j = 0; j < (1 << BYTEWIDTH); j++)
3407 if (SYNTAX (j) == Sword)
3408 fastmap[j] = 1;
3409 break;
3412 case notwordchar:
3413 for (j = 0; j < (1 << BYTEWIDTH); j++)
3414 if (SYNTAX (j) != Sword)
3415 fastmap[j] = 1;
3416 break;
3417 #else /* emacs */
3418 case charset:
3419 for (j = CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH - 1, p++;
3420 j >= 0; j--)
3421 if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
3422 fastmap[j] = 1;
3424 /* If we can match a character class, we can match
3425 any character set. */
3426 if (CHARSET_RANGE_TABLE_EXISTS_P (&p[-2])
3427 && CHARSET_RANGE_TABLE_BITS (&p[-2]) != 0)
3428 goto set_fastmap_for_multibyte_characters;
3430 if (CHARSET_RANGE_TABLE_EXISTS_P (&p[-2])
3431 && match_any_multibyte_characters == false)
3433 /* Set fastmap[I] 1 where I is a base leading code of each
3434 multibyte character in the range table. */
3435 int c, count;
3437 /* Make P points the range table. */
3438 p += CHARSET_BITMAP_SIZE (&p[-2]);
3440 /* Extract the number of ranges in range table into COUNT. */
3441 EXTRACT_NUMBER_AND_INCR (count, p);
3442 for (; count > 0; count--, p += 2 * 3) /* XXX */
3444 /* Extract the start of each range. */
3445 EXTRACT_CHARACTER (c, p);
3446 j = CHAR_CHARSET (c);
3447 fastmap[CHARSET_LEADING_CODE_BASE (j)] = 1;
3450 break;
3453 case charset_not:
3454 /* Chars beyond end of bitmap are possible matches.
3455 All the single-byte codes can occur in multibyte buffers.
3456 So any that are not listed in the charset
3457 are possible matches, even in multibyte buffers. */
3458 simple_char_max = (1 << BYTEWIDTH);
3459 for (j = CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH;
3460 j < simple_char_max; j++)
3461 fastmap[j] = 1;
3463 for (j = CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH - 1, p++;
3464 j >= 0; j--)
3465 if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
3466 fastmap[j] = 1;
3468 if (bufp->multibyte)
3469 /* Any character set can possibly contain a character
3470 which doesn't match the specified set of characters. */
3472 set_fastmap_for_multibyte_characters:
3473 if (match_any_multibyte_characters == false)
3475 for (j = 0x80; j < 0xA0; j++) /* XXX */
3476 if (BASE_LEADING_CODE_P (j))
3477 fastmap[j] = 1;
3478 match_any_multibyte_characters = true;
3481 break;
3484 case wordchar:
3485 /* All the single-byte codes can occur in multibyte buffers,
3486 and they may have word syntax. So do consider them. */
3487 simple_char_max = (1 << BYTEWIDTH);
3488 for (j = 0; j < simple_char_max; j++)
3489 if (SYNTAX (j) == Sword)
3490 fastmap[j] = 1;
3492 if (bufp->multibyte)
3493 /* Any character set can possibly contain a character
3494 whose syntax is `Sword'. */
3495 goto set_fastmap_for_multibyte_characters;
3496 break;
3499 case notwordchar:
3500 /* All the single-byte codes can occur in multibyte buffers,
3501 and they may not have word syntax. So do consider them. */
3502 simple_char_max = (1 << BYTEWIDTH);
3503 for (j = 0; j < simple_char_max; j++)
3504 if (SYNTAX (j) != Sword)
3505 fastmap[j] = 1;
3507 if (bufp->multibyte)
3508 /* Any character set can possibly contain a character
3509 whose syntax is not `Sword'. */
3510 goto set_fastmap_for_multibyte_characters;
3511 break;
3512 #endif
3514 case anychar:
3516 int fastmap_newline = fastmap['\n'];
3518 /* `.' matches anything, except perhaps newline.
3519 Even in a multibyte buffer, it should match any
3520 conceivable byte value for the fastmap. */
3521 if (bufp->multibyte)
3522 match_any_multibyte_characters = true;
3524 simple_char_max = (1 << BYTEWIDTH);
3525 for (j = 0; j < simple_char_max; j++)
3526 fastmap[j] = 1;
3528 /* ... except perhaps newline. */
3529 if (!(bufp->syntax & RE_DOT_NEWLINE))
3530 fastmap['\n'] = fastmap_newline;
3532 /* Otherwise, have to check alternative paths. */
3533 break;
3536 #ifdef emacs
3537 case wordbound:
3538 case notwordbound:
3539 case wordbeg:
3540 case wordend:
3541 case notsyntaxspec:
3542 case syntaxspec:
3543 /* This match depends on text properties. These end with
3544 aborting optimizations. */
3545 bufp->can_be_null = 1;
3546 continue;
3547 #if 0
3548 k = *p++;
3549 simple_char_max = bufp->multibyte ? 0x80 : (1 << BYTEWIDTH);
3550 for (j = 0; j < simple_char_max; j++)
3551 if (SYNTAX (j) == (enum syntaxcode) k)
3552 fastmap[j] = 1;
3554 if (bufp->multibyte)
3555 /* Any character set can possibly contain a character
3556 whose syntax is K. */
3557 goto set_fastmap_for_multibyte_characters;
3558 break;
3560 case notsyntaxspec:
3561 k = *p++;
3562 simple_char_max = bufp->multibyte ? 0x80 : (1 << BYTEWIDTH);
3563 for (j = 0; j < simple_char_max; j++)
3564 if (SYNTAX (j) != (enum syntaxcode) k)
3565 fastmap[j] = 1;
3567 if (bufp->multibyte)
3568 /* Any character set can possibly contain a character
3569 whose syntax is not K. */
3570 goto set_fastmap_for_multibyte_characters;
3571 break;
3572 #endif
3575 case categoryspec:
3576 k = *p++;
3577 simple_char_max = (1 << BYTEWIDTH);
3578 for (j = 0; j < simple_char_max; j++)
3579 if (CHAR_HAS_CATEGORY (j, k))
3580 fastmap[j] = 1;
3582 if (bufp->multibyte)
3583 /* Any character set can possibly contain a character
3584 whose category is K. */
3585 goto set_fastmap_for_multibyte_characters;
3586 break;
3589 case notcategoryspec:
3590 k = *p++;
3591 simple_char_max = (1 << BYTEWIDTH);
3592 for (j = 0; j < simple_char_max; j++)
3593 if (!CHAR_HAS_CATEGORY (j, k))
3594 fastmap[j] = 1;
3596 if (bufp->multibyte)
3597 /* Any character set can possibly contain a character
3598 whose category is not K. */
3599 goto set_fastmap_for_multibyte_characters;
3600 break;
3602 /* All cases after this match the empty string. These end with
3603 `continue'. */
3606 case before_dot:
3607 case at_dot:
3608 case after_dot:
3609 continue;
3610 #endif /* emacs */
3613 case no_op:
3614 case begline:
3615 case endline:
3616 case begbuf:
3617 case endbuf:
3618 #ifndef emacs
3619 case wordbound:
3620 case notwordbound:
3621 case wordbeg:
3622 case wordend:
3623 #endif
3624 continue;
3627 case jump_n:
3628 case jump:
3629 EXTRACT_NUMBER_AND_INCR (j, p);
3630 if (j < 0)
3631 /* Backward jumps can only go back to code that we've already
3632 visited. `re_compile' should make sure this is true. */
3633 break;
3634 p += j;
3635 switch (SWITCH_ENUM_CAST ((re_opcode_t) *p))
3637 case on_failure_jump:
3638 case on_failure_keep_string_jump:
3639 case on_failure_jump_exclusive:
3640 case on_failure_jump_loop:
3641 case on_failure_jump_smart:
3642 p++;
3643 break;
3644 default:
3645 continue;
3647 /* Keep `p1' to allow the `on_failure_jump' we are jumping to
3648 to jump back to "just after here". */
3649 /* Fallthrough */
3651 case on_failure_jump:
3652 case on_failure_keep_string_jump:
3653 case on_failure_jump_exclusive:
3654 case on_failure_jump_loop:
3655 case on_failure_jump_smart:
3656 handle_on_failure_jump:
3657 EXTRACT_NUMBER_AND_INCR (j, p);
3659 /* For some patterns, e.g., `(a?)?', `p+j' here points to the
3660 end of the pattern. We don't want to push such a point,
3661 since when we restore it above, entering the switch will
3662 increment `p' past the end of the pattern. We don't need
3663 to push such a point since we obviously won't find any more
3664 fastmap entries beyond `pend'. Such a pattern can match
3665 the null string, though. */
3666 if (p + j <= p1)
3667 /* Backward jump to be ignored. */
3669 else if (p + j < pend)
3671 if (!PUSH_PATTERN_OP (p + j, fail_stack))
3673 RESET_FAIL_STACK ();
3674 return -2;
3677 else
3678 bufp->can_be_null = 1;
3680 if (succeed_n_p)
3682 EXTRACT_NUMBER_AND_INCR (k, p); /* Skip the n. */
3683 succeed_n_p = false;
3686 continue;
3689 case succeed_n:
3690 /* Get to the number of times to succeed. */
3691 p += 2;
3693 /* Increment p past the n for when k != 0. */
3694 EXTRACT_NUMBER_AND_INCR (k, p);
3695 if (k == 0)
3697 p -= 4;
3698 succeed_n_p = true; /* Spaghetti code alert. */
3699 goto handle_on_failure_jump;
3701 continue;
3704 case set_number_at:
3705 p += 4;
3706 continue;
3709 case start_memory:
3710 case stop_memory:
3711 p += 1;
3712 continue;
3715 default:
3716 abort (); /* We have listed all the cases. */
3717 } /* switch *p++ */
3719 /* Getting here means we have found the possible starting
3720 characters for one path of the pattern -- and that the empty
3721 string does not match. We need not follow this path further.
3722 Instead, look at the next alternative (remembered on the
3723 stack), or quit if no more. The test at the top of the loop
3724 does these things. */
3725 path_can_be_null = false;
3726 p = pend;
3727 } /* while p */
3729 /* Set `can_be_null' for the last path (also the first path, if the
3730 pattern is empty). */
3731 bufp->can_be_null |= path_can_be_null;
3732 RESET_FAIL_STACK ();
3733 return 0;
3734 } /* re_compile_fastmap */
3736 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
3737 ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
3738 this memory for recording register information. STARTS and ENDS
3739 must be allocated using the malloc library routine, and must each
3740 be at least NUM_REGS * sizeof (regoff_t) bytes long.
3742 If NUM_REGS == 0, then subsequent matches should allocate their own
3743 register data.
3745 Unless this function is called, the first search or match using
3746 PATTERN_BUFFER will allocate its own register data, without
3747 freeing the old data. */
3749 void
3750 re_set_registers (bufp, regs, num_regs, starts, ends)
3751 struct re_pattern_buffer *bufp;
3752 struct re_registers *regs;
3753 unsigned num_regs;
3754 regoff_t *starts, *ends;
3756 if (num_regs)
3758 bufp->regs_allocated = REGS_REALLOCATE;
3759 regs->num_regs = num_regs;
3760 regs->start = starts;
3761 regs->end = ends;
3763 else
3765 bufp->regs_allocated = REGS_UNALLOCATED;
3766 regs->num_regs = 0;
3767 regs->start = regs->end = (regoff_t *) 0;
3771 /* Searching routines. */
3773 /* Like re_search_2, below, but only one string is specified, and
3774 doesn't let you say where to stop matching. */
3777 re_search (bufp, string, size, startpos, range, regs)
3778 struct re_pattern_buffer *bufp;
3779 const char *string;
3780 int size, startpos, range;
3781 struct re_registers *regs;
3783 return re_search_2 (bufp, NULL, 0, string, size, startpos, range,
3784 regs, size);
3787 /* End address of virtual concatenation of string. */
3788 #define STOP_ADDR_VSTRING(P) \
3789 (((P) >= size1 ? string2 + size2 : string1 + size1))
3791 /* Address of POS in the concatenation of virtual string. */
3792 #define POS_ADDR_VSTRING(POS) \
3793 (((POS) >= size1 ? string2 - size1 : string1) + (POS))
3795 /* Using the compiled pattern in BUFP->buffer, first tries to match the
3796 virtual concatenation of STRING1 and STRING2, starting first at index
3797 STARTPOS, then at STARTPOS + 1, and so on.
3799 STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
3801 RANGE is how far to scan while trying to match. RANGE = 0 means try
3802 only at STARTPOS; in general, the last start tried is STARTPOS +
3803 RANGE.
3805 In REGS, return the indices of the virtual concatenation of STRING1
3806 and STRING2 that matched the entire BUFP->buffer and its contained
3807 subexpressions.
3809 Do not consider matching one past the index STOP in the virtual
3810 concatenation of STRING1 and STRING2.
3812 We return either the position in the strings at which the match was
3813 found, -1 if no match, or -2 if error (such as failure
3814 stack overflow). */
3817 re_search_2 (bufp, string1, size1, string2, size2, startpos, range, regs, stop)
3818 struct re_pattern_buffer *bufp;
3819 const char *string1, *string2;
3820 int size1, size2;
3821 int startpos;
3822 int range;
3823 struct re_registers *regs;
3824 int stop;
3826 int val;
3827 register char *fastmap = bufp->fastmap;
3828 register RE_TRANSLATE_TYPE translate = bufp->translate;
3829 int total_size = size1 + size2;
3830 int endpos = startpos + range;
3831 int anchored_start = 0;
3833 /* Nonzero if we have to concern multibyte character. */
3834 int multibyte = bufp->multibyte;
3836 /* Check for out-of-range STARTPOS. */
3837 if (startpos < 0 || startpos > total_size)
3838 return -1;
3840 /* Fix up RANGE if it might eventually take us outside
3841 the virtual concatenation of STRING1 and STRING2.
3842 Make sure we won't move STARTPOS below 0 or above TOTAL_SIZE. */
3843 if (endpos < 0)
3844 range = 0 - startpos;
3845 else if (endpos > total_size)
3846 range = total_size - startpos;
3848 /* If the search isn't to be a backwards one, don't waste time in a
3849 search for a pattern anchored at beginning of buffer. */
3850 if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == begbuf && range > 0)
3852 if (startpos > 0)
3853 return -1;
3854 else
3855 range = 0;
3858 #ifdef emacs
3859 /* In a forward search for something that starts with \=.
3860 don't keep searching past point. */
3861 if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == at_dot && range > 0)
3863 range = PT_BYTE - BEGV_BYTE - startpos;
3864 if (range < 0)
3865 return -1;
3867 #endif /* emacs */
3869 /* Update the fastmap now if not correct already. */
3870 if (fastmap && !bufp->fastmap_accurate)
3871 if (re_compile_fastmap (bufp) == -2)
3872 return -2;
3874 /* See whether the pattern is anchored. */
3875 if (bufp->buffer[0] == begline)
3876 anchored_start = 1;
3878 #ifdef emacs
3879 gl_state.object = re_match_object;
3881 int adjpos = NILP (re_match_object) || BUFFERP (re_match_object);
3882 int charpos = SYNTAX_TABLE_BYTE_TO_CHAR (startpos + adjpos);
3884 SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object, charpos, 1);
3886 #endif
3888 /* Loop through the string, looking for a place to start matching. */
3889 for (;;)
3891 /* If the pattern is anchored,
3892 skip quickly past places we cannot match.
3893 We don't bother to treat startpos == 0 specially
3894 because that case doesn't repeat. */
3895 if (anchored_start && startpos > 0)
3897 if (! (bufp->newline_anchor
3898 && ((startpos <= size1 ? string1[startpos - 1]
3899 : string2[startpos - size1 - 1])
3900 == '\n')))
3901 goto advance;
3904 /* If a fastmap is supplied, skip quickly over characters that
3905 cannot be the start of a match. If the pattern can match the
3906 null string, however, we don't need to skip characters; we want
3907 the first null string. */
3908 if (fastmap && startpos < total_size && !bufp->can_be_null)
3910 register const char *d;
3911 register unsigned int buf_ch;
3913 d = POS_ADDR_VSTRING (startpos);
3915 if (range > 0) /* Searching forwards. */
3917 register int lim = 0;
3918 int irange = range;
3920 if (startpos < size1 && startpos + range >= size1)
3921 lim = range - (size1 - startpos);
3923 /* Written out as an if-else to avoid testing `translate'
3924 inside the loop. */
3925 if (RE_TRANSLATE_P (translate))
3927 if (multibyte)
3928 while (range > lim)
3930 int buf_charlen;
3932 buf_ch = STRING_CHAR_AND_LENGTH (d, range - lim,
3933 buf_charlen);
3935 buf_ch = RE_TRANSLATE (translate, buf_ch);
3936 if (buf_ch >= 0400
3937 || fastmap[buf_ch])
3938 break;
3940 range -= buf_charlen;
3941 d += buf_charlen;
3943 else
3944 while (range > lim
3945 && !fastmap[(unsigned char)
3946 RE_TRANSLATE (translate, (unsigned char) *d)])
3948 d++;
3949 range--;
3952 else
3953 while (range > lim && !fastmap[(unsigned char) *d])
3955 d++;
3956 range--;
3959 startpos += irange - range;
3961 else /* Searching backwards. */
3963 int room = (size1 == 0 || startpos >= size1
3964 ? size2 + size1 - startpos
3965 : size1 - startpos);
3967 buf_ch = STRING_CHAR (d, room);
3968 if (RE_TRANSLATE_P (translate))
3969 buf_ch = RE_TRANSLATE (translate, buf_ch);
3971 if (! (buf_ch >= 0400
3972 || fastmap[buf_ch]))
3973 goto advance;
3977 /* If can't match the null string, and that's all we have left, fail. */
3978 if (range >= 0 && startpos == total_size && fastmap
3979 && !bufp->can_be_null)
3980 return -1;
3982 val = re_match_2_internal (bufp, string1, size1, string2, size2,
3983 startpos, regs, stop);
3984 #ifndef REGEX_MALLOC
3985 #ifdef C_ALLOCA
3986 alloca (0);
3987 #endif
3988 #endif
3990 if (val >= 0)
3991 return startpos;
3993 if (val == -2)
3994 return -2;
3996 advance:
3997 if (!range)
3998 break;
3999 else if (range > 0)
4001 /* Update STARTPOS to the next character boundary. */
4002 if (multibyte)
4004 const unsigned char *p
4005 = (const unsigned char *) POS_ADDR_VSTRING (startpos);
4006 const unsigned char *pend
4007 = (const unsigned char *) STOP_ADDR_VSTRING (startpos);
4008 int len = MULTIBYTE_FORM_LENGTH (p, pend - p);
4010 range -= len;
4011 if (range < 0)
4012 break;
4013 startpos += len;
4015 else
4017 range--;
4018 startpos++;
4021 else
4023 range++;
4024 startpos--;
4026 /* Update STARTPOS to the previous character boundary. */
4027 if (multibyte)
4029 const unsigned char *p
4030 = (const unsigned char *) POS_ADDR_VSTRING (startpos);
4031 int len = 0;
4033 /* Find the head of multibyte form. */
4034 while (!CHAR_HEAD_P (*p))
4035 p--, len++;
4037 /* Adjust it. */
4038 #if 0 /* XXX */
4039 if (MULTIBYTE_FORM_LENGTH (p, len + 1) != (len + 1))
4041 else
4042 #endif
4044 range += len;
4045 if (range > 0)
4046 break;
4048 startpos -= len;
4053 return -1;
4054 } /* re_search_2 */
4056 /* Declarations and macros for re_match_2. */
4058 static int bcmp_translate ();
4060 /* This converts PTR, a pointer into one of the search strings `string1'
4061 and `string2' into an offset from the beginning of that string. */
4062 #define POINTER_TO_OFFSET(ptr) \
4063 (FIRST_STRING_P (ptr) \
4064 ? ((regoff_t) ((ptr) - string1)) \
4065 : ((regoff_t) ((ptr) - string2 + size1)))
4067 /* Macros for dealing with the split strings in re_match_2. */
4069 #define MATCHING_IN_FIRST_STRING (dend == end_match_1)
4071 /* Call before fetching a character with *d. This switches over to
4072 string2 if necessary. */
4073 #define PREFETCH() \
4074 while (d == dend) \
4076 /* End of string2 => fail. */ \
4077 if (dend == end_match_2) \
4078 goto fail; \
4079 /* End of string1 => advance to string2. */ \
4080 d = string2; \
4081 dend = end_match_2; \
4085 /* Test if at very beginning or at very end of the virtual concatenation
4086 of `string1' and `string2'. If only one string, it's `string2'. */
4087 #define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2)
4088 #define AT_STRINGS_END(d) ((d) == end2)
4091 /* Test if D points to a character which is word-constituent. We have
4092 two special cases to check for: if past the end of string1, look at
4093 the first character in string2; and if before the beginning of
4094 string2, look at the last character in string1. */
4095 #define WORDCHAR_P(d) \
4096 (SYNTAX ((d) == end1 ? *string2 \
4097 : (d) == string2 - 1 ? *(end1 - 1) : *(d)) \
4098 == Sword)
4100 /* Disabled due to a compiler bug -- see comment at case wordbound */
4102 /* The comment at case wordbound is following one, but we don't use
4103 AT_WORD_BOUNDARY anymore to support multibyte form.
4105 The DEC Alpha C compiler 3.x generates incorrect code for the
4106 test WORDCHAR_P (d - 1) != WORDCHAR_P (d) in the expansion of
4107 AT_WORD_BOUNDARY, so this code is disabled. Expanding the
4108 macro and introducing temporary variables works around the bug. */
4110 #if 0
4111 /* Test if the character before D and the one at D differ with respect
4112 to being word-constituent. */
4113 #define AT_WORD_BOUNDARY(d) \
4114 (AT_STRINGS_BEG (d) || AT_STRINGS_END (d) \
4115 || WORDCHAR_P (d - 1) != WORDCHAR_P (d))
4116 #endif
4118 /* Free everything we malloc. */
4119 #ifdef MATCH_MAY_ALLOCATE
4120 #define FREE_VAR(var) if (var) { REGEX_FREE (var); var = NULL; } else
4121 #define FREE_VARIABLES() \
4122 do { \
4123 REGEX_FREE_STACK (fail_stack.stack); \
4124 FREE_VAR (regstart); \
4125 FREE_VAR (regend); \
4126 FREE_VAR (best_regstart); \
4127 FREE_VAR (best_regend); \
4128 } while (0)
4129 #else
4130 #define FREE_VARIABLES() ((void)0) /* Do nothing! But inhibit gcc warning. */
4131 #endif /* not MATCH_MAY_ALLOCATE */
4134 /* Optimization routines. */
4136 /* Jump over non-matching operations. */
4137 static unsigned char *
4138 skip_noops (p, pend, memory)
4139 unsigned char *p, *pend;
4140 int memory;
4142 int mcnt;
4143 while (p < pend)
4145 switch (SWITCH_ENUM_CAST ((re_opcode_t) *p))
4147 case start_memory:
4148 if (!memory)
4149 return p;
4150 case stop_memory:
4151 p += 2; break;
4152 case no_op:
4153 p += 1; break;
4154 case jump:
4155 p += 1;
4156 EXTRACT_NUMBER_AND_INCR (mcnt, p);
4157 p += mcnt;
4158 break;
4159 default:
4160 return p;
4163 assert (p == pend);
4164 return p;
4167 /* Non-zero if "p1 matches something" implies "p2 fails". */
4168 static int
4169 mutually_exclusive_p (bufp, p1, p2)
4170 struct re_pattern_buffer *bufp;
4171 unsigned char *p1, *p2;
4173 int multibyte = bufp->multibyte;
4174 unsigned char *pend = bufp->buffer + bufp->used;
4176 assert (p1 >= bufp->buffer && p1 <= pend
4177 && p2 >= bufp->buffer && p2 <= pend);
4179 /* Skip over open/close-group commands.
4180 If what follows this loop is a ...+ construct,
4181 look at what begins its body, since we will have to
4182 match at least one of that. */
4183 p2 = skip_noops (p2, pend, 1);
4184 /* The same skip can be done for p1, except that skipping over
4185 start_memory is not a good idea (if there's a group inside
4186 the loop delimited by on_failure_jump_exclusive, then it
4187 can't optimize the push away (it still works properly, but
4188 slightly slower rather than faster)). */
4189 p1 = skip_noops (p1, pend, 0);
4191 /* If we're at the end of the pattern, we can change. */
4192 if (p2 == pend)
4194 switch (SWITCH_ENUM_CAST ((re_opcode_t) *p1))
4196 case anychar:
4197 case charset_not:
4198 case charset:
4199 case exactn:
4200 DEBUG_PRINT1 (" End of pattern: fast loop.\n");
4201 return 1;
4202 default:
4203 return 0;
4207 else if ((re_opcode_t) *p2 == exactn
4208 || (bufp->newline_anchor && (re_opcode_t) *p2 == endline))
4210 register unsigned int c
4211 = *p2 == (unsigned char) endline ? '\n' : p2[2];
4213 if ((re_opcode_t) *p1 == exactn)
4215 if (!(multibyte /* && (c != '\n') */
4216 && BASE_LEADING_CODE_P (c))
4217 ? c != p1[2]
4218 : (STRING_CHAR (&p2[2], pend - &p2[2])
4219 != STRING_CHAR (&p1[2], pend - &p1[2])))
4221 DEBUG_PRINT3 (" '%c' != '%c' => fast loop.\n", c, p1[2]);
4222 return 1;
4226 else if ((re_opcode_t) *p1 == charset
4227 || (re_opcode_t) *p1 == charset_not)
4229 int not = (re_opcode_t) *p1 == charset_not;
4231 if (multibyte /* && (c != '\n') */
4232 && BASE_LEADING_CODE_P (c))
4233 c = STRING_CHAR (&p2[2], pend - &p2[2]);
4235 /* Test if C is listed in charset (or charset_not)
4236 at `p1'. */
4237 if (SINGLE_BYTE_CHAR_P (c))
4239 if (c < CHARSET_BITMAP_SIZE (p1) * BYTEWIDTH
4240 && p1[2 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
4241 not = !not;
4243 else if (CHARSET_RANGE_TABLE_EXISTS_P (p1))
4244 CHARSET_LOOKUP_RANGE_TABLE (not, c, p1);
4246 /* `not' is equal to 1 if c would match, which means
4247 that we can't change to pop_failure_jump. */
4248 if (!not)
4250 DEBUG_PRINT1 (" No match => fast loop.\n");
4251 return 1;
4254 else if ((re_opcode_t) *p1 == anychar
4255 && c == '\n')
4257 DEBUG_PRINT1 (" . != \\n => fast loop.\n");
4258 return 1;
4261 else if ((re_opcode_t) *p2 == charset
4262 || (re_opcode_t) *p2 == charset_not)
4264 if ((re_opcode_t) *p1 == exactn)
4265 /* Reuse the code above. */
4266 return mutually_exclusive_p (bufp, p2, p1);
4269 /* It is hard to list up all the character in charset
4270 P2 if it includes multibyte character. Give up in
4271 such case. */
4272 else if (!multibyte || !CHARSET_RANGE_TABLE_EXISTS_P (p2))
4274 /* Now, we are sure that P2 has no range table.
4275 So, for the size of bitmap in P2, `p2[1]' is
4276 enough. But P1 may have range table, so the
4277 size of bitmap table of P1 is extracted by
4278 using macro `CHARSET_BITMAP_SIZE'.
4280 Since we know that all the character listed in
4281 P2 is ASCII, it is enough to test only bitmap
4282 table of P1. */
4284 if (*p1 == *p2)
4286 int idx;
4287 /* We win if the charset inside the loop
4288 has no overlap with the one after the loop. */
4289 for (idx = 0;
4290 (idx < (int) p2[1]
4291 && idx < CHARSET_BITMAP_SIZE (p1));
4292 idx++)
4293 if ((p2[2 + idx] & p1[2 + idx]) != 0)
4294 break;
4296 if (idx == p2[1]
4297 || idx == CHARSET_BITMAP_SIZE (p1))
4299 DEBUG_PRINT1 (" No match => fast loop.\n");
4300 return 1;
4303 else if ((re_opcode_t) *p1 == charset
4304 || (re_opcode_t) *p1 == charset_not)
4306 int idx;
4307 /* We win if the charset_not inside the loop lists
4308 every character listed in the charset after. */
4309 for (idx = 0; idx < (int) p2[1]; idx++)
4310 if (! (p2[2 + idx] == 0
4311 || (idx < CHARSET_BITMAP_SIZE (p1)
4312 && ((p2[2 + idx] & ~ p1[2 + idx]) == 0))))
4313 break;
4315 if (idx == p2[1])
4317 DEBUG_PRINT1 (" No match => fast loop.\n");
4318 return 1;
4324 /* Safe default. */
4325 return 0;
4329 /* Matching routines. */
4331 #ifndef emacs /* Emacs never uses this. */
4332 /* re_match is like re_match_2 except it takes only a single string. */
4335 re_match (bufp, string, size, pos, regs)
4336 struct re_pattern_buffer *bufp;
4337 const char *string;
4338 int size, pos;
4339 struct re_registers *regs;
4341 int result = re_match_2_internal (bufp, NULL, 0, string, size,
4342 pos, regs, size);
4343 alloca (0);
4344 return result;
4346 #endif /* not emacs */
4348 #ifdef emacs
4349 /* In Emacs, this is the string or buffer in which we
4350 are matching. It is used for looking up syntax properties. */
4351 Lisp_Object re_match_object;
4352 #endif
4354 /* re_match_2 matches the compiled pattern in BUFP against the
4355 the (virtual) concatenation of STRING1 and STRING2 (of length SIZE1
4356 and SIZE2, respectively). We start matching at POS, and stop
4357 matching at STOP.
4359 If REGS is non-null and the `no_sub' field of BUFP is nonzero, we
4360 store offsets for the substring each group matched in REGS. See the
4361 documentation for exactly how many groups we fill.
4363 We return -1 if no match, -2 if an internal error (such as the
4364 failure stack overflowing). Otherwise, we return the length of the
4365 matched substring. */
4368 re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
4369 struct re_pattern_buffer *bufp;
4370 const char *string1, *string2;
4371 int size1, size2;
4372 int pos;
4373 struct re_registers *regs;
4374 int stop;
4376 int result;
4378 #ifdef emacs
4379 int charpos;
4380 int adjpos = NILP (re_match_object) || BUFFERP (re_match_object);
4381 gl_state.object = re_match_object;
4382 charpos = SYNTAX_TABLE_BYTE_TO_CHAR (pos + adjpos);
4383 SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object, charpos, 1);
4384 #endif
4386 result = re_match_2_internal (bufp, string1, size1, string2, size2,
4387 pos, regs, stop);
4388 alloca (0);
4389 return result;
4392 /* This is a separate function so that we can force an alloca cleanup
4393 afterwards. */
4394 static int
4395 re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
4396 struct re_pattern_buffer *bufp;
4397 const char *string1, *string2;
4398 int size1, size2;
4399 int pos;
4400 struct re_registers *regs;
4401 int stop;
4403 /* General temporaries. */
4404 int mcnt;
4405 unsigned char *p1;
4407 /* Just past the end of the corresponding string. */
4408 const char *end1, *end2;
4410 /* Pointers into string1 and string2, just past the last characters in
4411 each to consider matching. */
4412 const char *end_match_1, *end_match_2;
4414 /* Where we are in the data, and the end of the current string. */
4415 const char *d, *dend;
4417 /* Where we are in the pattern, and the end of the pattern. */
4418 unsigned char *p = bufp->buffer;
4419 register unsigned char *pend = p + bufp->used;
4421 /* We use this to map every character in the string. */
4422 RE_TRANSLATE_TYPE translate = bufp->translate;
4424 /* Nonzero if we have to concern multibyte character. */
4425 int multibyte = bufp->multibyte;
4427 /* Failure point stack. Each place that can handle a failure further
4428 down the line pushes a failure point on this stack. It consists of
4429 regstart, and regend for all registers corresponding to
4430 the subexpressions we're currently inside, plus the number of such
4431 registers, and, finally, two char *'s. The first char * is where
4432 to resume scanning the pattern; the second one is where to resume
4433 scanning the strings. */
4434 #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
4435 fail_stack_type fail_stack;
4436 #endif
4437 #ifdef DEBUG
4438 static unsigned failure_id = 0;
4439 unsigned nfailure_points_pushed = 0, nfailure_points_popped = 0;
4440 #endif
4442 /* This holds the pointer to the failure stack, when
4443 it is allocated relocatably. */
4444 fail_stack_elt_t *failure_stack_ptr;
4446 /* We fill all the registers internally, independent of what we
4447 return, for use in backreferences. The number here includes
4448 an element for register zero. */
4449 unsigned num_regs = bufp->re_nsub + 1;
4451 /* Information on the contents of registers. These are pointers into
4452 the input strings; they record just what was matched (on this
4453 attempt) by a subexpression part of the pattern, that is, the
4454 regnum-th regstart pointer points to where in the pattern we began
4455 matching and the regnum-th regend points to right after where we
4456 stopped matching the regnum-th subexpression. (The zeroth register
4457 keeps track of what the whole pattern matches.) */
4458 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
4459 const char **regstart, **regend;
4460 #endif
4462 /* The following record the register info as found in the above
4463 variables when we find a match better than any we've seen before.
4464 This happens as we backtrack through the failure points, which in
4465 turn happens only if we have not yet matched the entire string. */
4466 unsigned best_regs_set = false;
4467 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
4468 const char **best_regstart, **best_regend;
4469 #endif
4471 /* Logically, this is `best_regend[0]'. But we don't want to have to
4472 allocate space for that if we're not allocating space for anything
4473 else (see below). Also, we never need info about register 0 for
4474 any of the other register vectors, and it seems rather a kludge to
4475 treat `best_regend' differently than the rest. So we keep track of
4476 the end of the best match so far in a separate variable. We
4477 initialize this to NULL so that when we backtrack the first time
4478 and need to test it, it's not garbage. */
4479 const char *match_end = NULL;
4481 #ifdef DEBUG
4482 /* Counts the total number of registers pushed. */
4483 unsigned num_regs_pushed = 0;
4484 #endif
4486 DEBUG_PRINT1 ("\n\nEntering re_match_2.\n");
4488 INIT_FAIL_STACK ();
4490 #ifdef MATCH_MAY_ALLOCATE
4491 /* Do not bother to initialize all the register variables if there are
4492 no groups in the pattern, as it takes a fair amount of time. If
4493 there are groups, we include space for register 0 (the whole
4494 pattern), even though we never use it, since it simplifies the
4495 array indexing. We should fix this. */
4496 if (bufp->re_nsub)
4498 regstart = REGEX_TALLOC (num_regs, const char *);
4499 regend = REGEX_TALLOC (num_regs, const char *);
4500 best_regstart = REGEX_TALLOC (num_regs, const char *);
4501 best_regend = REGEX_TALLOC (num_regs, const char *);
4503 if (!(regstart && regend && best_regstart && best_regend))
4505 FREE_VARIABLES ();
4506 return -2;
4509 else
4511 /* We must initialize all our variables to NULL, so that
4512 `FREE_VARIABLES' doesn't try to free them. */
4513 regstart = regend = best_regstart = best_regend = NULL;
4515 #endif /* MATCH_MAY_ALLOCATE */
4517 /* The starting position is bogus. */
4518 if (pos < 0 || pos > size1 + size2)
4520 FREE_VARIABLES ();
4521 return -1;
4524 /* Initialize subexpression text positions to -1 to mark ones that no
4525 start_memory/stop_memory has been seen for. Also initialize the
4526 register information struct. */
4527 for (mcnt = 1; mcnt < num_regs; mcnt++)
4529 regstart[mcnt] = regend[mcnt] = REG_UNSET_VALUE;
4532 /* We move `string1' into `string2' if the latter's empty -- but not if
4533 `string1' is null. */
4534 if (size2 == 0 && string1 != NULL)
4536 string2 = string1;
4537 size2 = size1;
4538 string1 = 0;
4539 size1 = 0;
4541 end1 = string1 + size1;
4542 end2 = string2 + size2;
4544 /* Compute where to stop matching, within the two strings. */
4545 if (stop <= size1)
4547 end_match_1 = string1 + stop;
4548 end_match_2 = string2;
4550 else
4552 end_match_1 = end1;
4553 end_match_2 = string2 + stop - size1;
4556 /* `p' scans through the pattern as `d' scans through the data.
4557 `dend' is the end of the input string that `d' points within. `d'
4558 is advanced into the following input string whenever necessary, but
4559 this happens before fetching; therefore, at the beginning of the
4560 loop, `d' can be pointing at the end of a string, but it cannot
4561 equal `string2'. */
4562 if (size1 > 0 && pos <= size1)
4564 d = string1 + pos;
4565 dend = end_match_1;
4567 else
4569 d = string2 + pos - size1;
4570 dend = end_match_2;
4573 DEBUG_PRINT1 ("The compiled pattern is: ");
4574 DEBUG_PRINT_COMPILED_PATTERN (bufp, p, pend);
4575 DEBUG_PRINT1 ("The string to match is: `");
4576 DEBUG_PRINT_DOUBLE_STRING (d, string1, size1, string2, size2);
4577 DEBUG_PRINT1 ("'\n");
4579 /* This loops over pattern commands. It exits by returning from the
4580 function if the match is complete, or it drops through if the match
4581 fails at this starting point in the input data. */
4582 for (;;)
4584 DEBUG_PRINT2 ("\n%p: ", p);
4586 if (p == pend)
4587 { /* End of pattern means we might have succeeded. */
4588 DEBUG_PRINT1 ("end of pattern ... ");
4590 /* If we haven't matched the entire string, and we want the
4591 longest match, try backtracking. */
4592 if (d != end_match_2)
4594 /* 1 if this match ends in the same string (string1 or string2)
4595 as the best previous match. */
4596 boolean same_str_p = (FIRST_STRING_P (match_end)
4597 == MATCHING_IN_FIRST_STRING);
4598 /* 1 if this match is the best seen so far. */
4599 boolean best_match_p;
4601 /* AIX compiler got confused when this was combined
4602 with the previous declaration. */
4603 if (same_str_p)
4604 best_match_p = d > match_end;
4605 else
4606 best_match_p = !MATCHING_IN_FIRST_STRING;
4608 DEBUG_PRINT1 ("backtracking.\n");
4610 if (!FAIL_STACK_EMPTY ())
4611 { /* More failure points to try. */
4613 /* If exceeds best match so far, save it. */
4614 if (!best_regs_set || best_match_p)
4616 best_regs_set = true;
4617 match_end = d;
4619 DEBUG_PRINT1 ("\nSAVING match as best so far.\n");
4621 for (mcnt = 1; mcnt < num_regs; mcnt++)
4623 best_regstart[mcnt] = regstart[mcnt];
4624 best_regend[mcnt] = regend[mcnt];
4627 goto fail;
4630 /* If no failure points, don't restore garbage. And if
4631 last match is real best match, don't restore second
4632 best one. */
4633 else if (best_regs_set && !best_match_p)
4635 restore_best_regs:
4636 /* Restore best match. It may happen that `dend ==
4637 end_match_1' while the restored d is in string2.
4638 For example, the pattern `x.*y.*z' against the
4639 strings `x-' and `y-z-', if the two strings are
4640 not consecutive in memory. */
4641 DEBUG_PRINT1 ("Restoring best registers.\n");
4643 d = match_end;
4644 dend = ((d >= string1 && d <= end1)
4645 ? end_match_1 : end_match_2);
4647 for (mcnt = 1; mcnt < num_regs; mcnt++)
4649 regstart[mcnt] = best_regstart[mcnt];
4650 regend[mcnt] = best_regend[mcnt];
4653 } /* d != end_match_2 */
4655 succeed_label:
4656 DEBUG_PRINT1 ("Accepting match.\n");
4658 /* If caller wants register contents data back, do it. */
4659 if (regs && !bufp->no_sub)
4661 /* Have the register data arrays been allocated? */
4662 if (bufp->regs_allocated == REGS_UNALLOCATED)
4663 { /* No. So allocate them with malloc. We need one
4664 extra element beyond `num_regs' for the `-1' marker
4665 GNU code uses. */
4666 regs->num_regs = MAX (RE_NREGS, num_regs + 1);
4667 regs->start = TALLOC (regs->num_regs, regoff_t);
4668 regs->end = TALLOC (regs->num_regs, regoff_t);
4669 if (regs->start == NULL || regs->end == NULL)
4671 FREE_VARIABLES ();
4672 return -2;
4674 bufp->regs_allocated = REGS_REALLOCATE;
4676 else if (bufp->regs_allocated == REGS_REALLOCATE)
4677 { /* Yes. If we need more elements than were already
4678 allocated, reallocate them. If we need fewer, just
4679 leave it alone. */
4680 if (regs->num_regs < num_regs + 1)
4682 regs->num_regs = num_regs + 1;
4683 RETALLOC (regs->start, regs->num_regs, regoff_t);
4684 RETALLOC (regs->end, regs->num_regs, regoff_t);
4685 if (regs->start == NULL || regs->end == NULL)
4687 FREE_VARIABLES ();
4688 return -2;
4692 else
4694 /* These braces fend off a "empty body in an else-statement"
4695 warning under GCC when assert expands to nothing. */
4696 assert (bufp->regs_allocated == REGS_FIXED);
4699 /* Convert the pointer data in `regstart' and `regend' to
4700 indices. Register zero has to be set differently,
4701 since we haven't kept track of any info for it. */
4702 if (regs->num_regs > 0)
4704 regs->start[0] = pos;
4705 regs->end[0] = (MATCHING_IN_FIRST_STRING
4706 ? ((regoff_t) (d - string1))
4707 : ((regoff_t) (d - string2 + size1)));
4710 /* Go through the first `min (num_regs, regs->num_regs)'
4711 registers, since that is all we initialized. */
4712 for (mcnt = 1; mcnt < MIN (num_regs, regs->num_regs); mcnt++)
4714 if (REG_UNSET (regstart[mcnt]) || REG_UNSET (regend[mcnt]))
4715 regs->start[mcnt] = regs->end[mcnt] = -1;
4716 else
4718 regs->start[mcnt]
4719 = (regoff_t) POINTER_TO_OFFSET (regstart[mcnt]);
4720 regs->end[mcnt]
4721 = (regoff_t) POINTER_TO_OFFSET (regend[mcnt]);
4725 /* If the regs structure we return has more elements than
4726 were in the pattern, set the extra elements to -1. If
4727 we (re)allocated the registers, this is the case,
4728 because we always allocate enough to have at least one
4729 -1 at the end. */
4730 for (mcnt = num_regs; mcnt < regs->num_regs; mcnt++)
4731 regs->start[mcnt] = regs->end[mcnt] = -1;
4732 } /* regs && !bufp->no_sub */
4734 DEBUG_PRINT4 ("%u failure points pushed, %u popped (%u remain).\n",
4735 nfailure_points_pushed, nfailure_points_popped,
4736 nfailure_points_pushed - nfailure_points_popped);
4737 DEBUG_PRINT2 ("%u registers pushed.\n", num_regs_pushed);
4739 mcnt = d - pos - (MATCHING_IN_FIRST_STRING
4740 ? string1
4741 : string2 - size1);
4743 DEBUG_PRINT2 ("Returning %d from re_match_2.\n", mcnt);
4745 FREE_VARIABLES ();
4746 return mcnt;
4749 /* Otherwise match next pattern command. */
4750 switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++))
4752 /* Ignore these. Used to ignore the n of succeed_n's which
4753 currently have n == 0. */
4754 case no_op:
4755 DEBUG_PRINT1 ("EXECUTING no_op.\n");
4756 break;
4758 case succeed:
4759 DEBUG_PRINT1 ("EXECUTING succeed.\n");
4760 goto succeed_label;
4762 /* Match the next n pattern characters exactly. The following
4763 byte in the pattern defines n, and the n bytes after that
4764 are the characters to match. */
4765 case exactn:
4766 mcnt = *p++;
4767 DEBUG_PRINT2 ("EXECUTING exactn %d.\n", mcnt);
4769 /* This is written out as an if-else so we don't waste time
4770 testing `translate' inside the loop. */
4771 if (RE_TRANSLATE_P (translate))
4773 #ifdef emacs
4774 if (multibyte)
4777 int pat_charlen, buf_charlen;
4778 unsigned int pat_ch, buf_ch;
4780 PREFETCH ();
4781 pat_ch = STRING_CHAR_AND_LENGTH (p, pend - p, pat_charlen);
4782 buf_ch = STRING_CHAR_AND_LENGTH (d, dend - d, buf_charlen);
4784 if (RE_TRANSLATE (translate, buf_ch)
4785 != pat_ch)
4786 goto fail;
4788 p += pat_charlen;
4789 d += buf_charlen;
4790 mcnt -= pat_charlen;
4792 while (mcnt > 0);
4793 else
4794 #endif /* not emacs */
4797 PREFETCH ();
4798 if ((unsigned char) RE_TRANSLATE (translate, (unsigned char) *d)
4799 != (unsigned char) *p++)
4800 goto fail;
4801 d++;
4803 while (--mcnt);
4805 else
4809 PREFETCH ();
4810 if (*d++ != (char) *p++) goto fail;
4812 while (--mcnt);
4814 break;
4817 /* Match any character except possibly a newline or a null. */
4818 case anychar:
4820 int buf_charlen;
4821 unsigned int buf_ch;
4823 DEBUG_PRINT1 ("EXECUTING anychar.\n");
4825 PREFETCH ();
4827 #ifdef emacs
4828 if (multibyte)
4829 buf_ch = STRING_CHAR_AND_LENGTH (d, dend - d, buf_charlen);
4830 else
4831 #endif /* not emacs */
4833 buf_ch = (unsigned char) *d;
4834 buf_charlen = 1;
4837 buf_ch = TRANSLATE (buf_ch);
4839 if ((!(bufp->syntax & RE_DOT_NEWLINE)
4840 && buf_ch == '\n')
4841 || ((bufp->syntax & RE_DOT_NOT_NULL)
4842 && buf_ch == '\000'))
4843 goto fail;
4845 DEBUG_PRINT2 (" Matched `%d'.\n", *d);
4846 d += buf_charlen;
4848 break;
4851 case charset:
4852 case charset_not:
4854 register unsigned int c;
4855 boolean not = (re_opcode_t) *(p - 1) == charset_not;
4856 int len;
4858 /* Start of actual range_table, or end of bitmap if there is no
4859 range table. */
4860 unsigned char *range_table;
4862 /* Nonzero if there is a range table. */
4863 int range_table_exists;
4865 /* Number of ranges of range table. This is not included
4866 in the initial byte-length of the command. */
4867 int count = 0;
4869 DEBUG_PRINT2 ("EXECUTING charset%s.\n", not ? "_not" : "");
4871 PREFETCH ();
4872 c = (unsigned char) *d;
4874 range_table_exists = CHARSET_RANGE_TABLE_EXISTS_P (&p[-1]);
4876 #ifdef emacs
4877 if (range_table_exists)
4879 range_table = CHARSET_RANGE_TABLE (&p[-1]); /* Past the bitmap. */
4880 EXTRACT_NUMBER_AND_INCR (count, range_table);
4883 if (multibyte && BASE_LEADING_CODE_P (c))
4884 c = STRING_CHAR_AND_LENGTH (d, dend - d, len);
4885 #endif /* emacs */
4887 if (SINGLE_BYTE_CHAR_P (c))
4888 { /* Lookup bitmap. */
4889 c = TRANSLATE (c); /* The character to match. */
4890 len = 1;
4892 /* Cast to `unsigned' instead of `unsigned char' in
4893 case the bit list is a full 32 bytes long. */
4894 if (c < (unsigned) (CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH)
4895 && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
4896 not = !not;
4898 #ifdef emacs
4899 else if (range_table_exists)
4901 int class_bits = CHARSET_RANGE_TABLE_BITS (&p[-1]);
4903 if ( (class_bits & BIT_ALNUM && ISALNUM (c))
4904 | (class_bits & BIT_ALPHA && ISALPHA (c))
4905 | (class_bits & BIT_ASCII && IS_REAL_ASCII (c))
4906 | (class_bits & BIT_GRAPH && ISGRAPH (c))
4907 | (class_bits & BIT_LOWER && ISLOWER (c))
4908 | (class_bits & BIT_MULTIBYTE && !ISUNIBYTE (c))
4909 | (class_bits & BIT_NONASCII && !IS_REAL_ASCII (c))
4910 | (class_bits & BIT_PRINT && ISPRINT (c))
4911 | (class_bits & BIT_PUNCT && ISPUNCT (c))
4912 | (class_bits & BIT_SPACE && ISSPACE (c))
4913 | (class_bits & BIT_UNIBYTE && ISUNIBYTE (c))
4914 | (class_bits & BIT_UPPER && ISUPPER (c))
4915 | (class_bits & BIT_WORD && ISWORD (c)))
4916 not = !not;
4917 else
4918 CHARSET_LOOKUP_RANGE_TABLE_RAW (not, c, range_table, count);
4920 #endif /* emacs */
4922 if (range_table_exists)
4923 p = CHARSET_RANGE_TABLE_END (range_table, count);
4924 else
4925 p += CHARSET_BITMAP_SIZE (&p[-1]) + 1;
4927 if (!not) goto fail;
4929 d += len;
4930 break;
4934 /* The beginning of a group is represented by start_memory.
4935 The argument is the register number. The text
4936 matched within the group is recorded (in the internal
4937 registers data structure) under the register number. */
4938 case start_memory:
4939 DEBUG_PRINT2 ("EXECUTING start_memory %d:\n", *p);
4941 /* In case we need to undo this operation (via backtracking). */
4942 PUSH_FAILURE_REG ((unsigned int)*p);
4944 regstart[*p] = d;
4945 regend[*p] = REG_UNSET_VALUE; /* probably unnecessary. -sm */
4946 DEBUG_PRINT2 (" regstart: %d\n", POINTER_TO_OFFSET (regstart[*p]));
4948 /* Move past the register number and inner group count. */
4949 p += 1;
4950 break;
4953 /* The stop_memory opcode represents the end of a group. Its
4954 argument is the same as start_memory's: the register number. */
4955 case stop_memory:
4956 DEBUG_PRINT2 ("EXECUTING stop_memory %d:\n", *p);
4958 assert (!REG_UNSET (regstart[*p]));
4959 /* Strictly speaking, there should be code such as:
4961 assert (REG_UNSET (regend[*p]));
4962 PUSH_FAILURE_REGSTOP ((unsigned int)*p);
4964 But the only info to be pushed is regend[*p] and it is known to
4965 be UNSET, so there really isn't anything to push.
4966 Not pushing anything, on the other hand deprives us from the
4967 guarantee that regend[*p] is UNSET since undoing this operation
4968 will not reset its value properly. This is not important since
4969 the value will only be read on the next start_memory or at
4970 the very end and both events can only happen if this stop_memory
4971 is *not* undone. */
4973 regend[*p] = d;
4974 DEBUG_PRINT2 (" regend: %d\n", POINTER_TO_OFFSET (regend[*p]));
4976 /* Move past the register number and the inner group count. */
4977 p += 1;
4978 break;
4981 /* \<digit> has been turned into a `duplicate' command which is
4982 followed by the numeric value of <digit> as the register number. */
4983 case duplicate:
4985 register const char *d2, *dend2;
4986 int regno = *p++; /* Get which register to match against. */
4987 DEBUG_PRINT2 ("EXECUTING duplicate %d.\n", regno);
4989 /* Can't back reference a group which we've never matched. */
4990 if (REG_UNSET (regstart[regno]) || REG_UNSET (regend[regno]))
4991 goto fail;
4993 /* Where in input to try to start matching. */
4994 d2 = regstart[regno];
4996 /* Where to stop matching; if both the place to start and
4997 the place to stop matching are in the same string, then
4998 set to the place to stop, otherwise, for now have to use
4999 the end of the first string. */
5001 dend2 = ((FIRST_STRING_P (regstart[regno])
5002 == FIRST_STRING_P (regend[regno]))
5003 ? regend[regno] : end_match_1);
5004 for (;;)
5006 /* If necessary, advance to next segment in register
5007 contents. */
5008 while (d2 == dend2)
5010 if (dend2 == end_match_2) break;
5011 if (dend2 == regend[regno]) break;
5013 /* End of string1 => advance to string2. */
5014 d2 = string2;
5015 dend2 = regend[regno];
5017 /* At end of register contents => success */
5018 if (d2 == dend2) break;
5020 /* If necessary, advance to next segment in data. */
5021 PREFETCH ();
5023 /* How many characters left in this segment to match. */
5024 mcnt = dend - d;
5026 /* Want how many consecutive characters we can match in
5027 one shot, so, if necessary, adjust the count. */
5028 if (mcnt > dend2 - d2)
5029 mcnt = dend2 - d2;
5031 /* Compare that many; failure if mismatch, else move
5032 past them. */
5033 if (RE_TRANSLATE_P (translate)
5034 ? bcmp_translate (d, d2, mcnt, translate)
5035 : bcmp (d, d2, mcnt))
5036 goto fail;
5037 d += mcnt, d2 += mcnt;
5040 break;
5043 /* begline matches the empty string at the beginning of the string
5044 (unless `not_bol' is set in `bufp'), and, if
5045 `newline_anchor' is set, after newlines. */
5046 case begline:
5047 DEBUG_PRINT1 ("EXECUTING begline.\n");
5049 if (AT_STRINGS_BEG (d))
5051 if (!bufp->not_bol) break;
5053 else if (d[-1] == '\n' && bufp->newline_anchor)
5055 break;
5057 /* In all other cases, we fail. */
5058 goto fail;
5061 /* endline is the dual of begline. */
5062 case endline:
5063 DEBUG_PRINT1 ("EXECUTING endline.\n");
5065 if (AT_STRINGS_END (d))
5067 if (!bufp->not_eol) break;
5070 /* We have to ``prefetch'' the next character. */
5071 else if ((d == end1 ? *string2 : *d) == '\n'
5072 && bufp->newline_anchor)
5074 break;
5076 goto fail;
5079 /* Match at the very beginning of the data. */
5080 case begbuf:
5081 DEBUG_PRINT1 ("EXECUTING begbuf.\n");
5082 if (AT_STRINGS_BEG (d))
5083 break;
5084 goto fail;
5087 /* Match at the very end of the data. */
5088 case endbuf:
5089 DEBUG_PRINT1 ("EXECUTING endbuf.\n");
5090 if (AT_STRINGS_END (d))
5091 break;
5092 goto fail;
5095 /* on_failure_keep_string_jump is used to optimize `.*\n'. It
5096 pushes NULL as the value for the string on the stack. Then
5097 `POP_FAILURE_POINT' will keep the current value for the
5098 string, instead of restoring it. To see why, consider
5099 matching `foo\nbar' against `.*\n'. The .* matches the foo;
5100 then the . fails against the \n. But the next thing we want
5101 to do is match the \n against the \n; if we restored the
5102 string value, we would be back at the foo.
5104 Because this is used only in specific cases, we don't need to
5105 check all the things that `on_failure_jump' does, to make
5106 sure the right things get saved on the stack. Hence we don't
5107 share its code. The only reason to push anything on the
5108 stack at all is that otherwise we would have to change
5109 `anychar's code to do something besides goto fail in this
5110 case; that seems worse than this. */
5111 case on_failure_keep_string_jump:
5112 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5113 DEBUG_PRINT3 ("EXECUTING on_failure_keep_string_jump %d (to %p):\n",
5114 mcnt, p + mcnt);
5116 PUSH_FAILURE_POINT (p - 3, NULL);
5117 break;
5119 case on_failure_jump_exclusive:
5120 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5121 DEBUG_PRINT3 ("EXECUTING on_failure_jump_exclusive %d (to %p):\n",
5122 mcnt, p + mcnt);
5124 if (! FAIL_STACK_EMPTY ()
5125 && FAILURE_PAT (TOP_FAILURE_HANDLE ()) == (p - 3)
5126 && fail_stack.avail == fail_stack.frame)
5128 /* We are trying to push failure F2 onto the stack but there
5129 is already a failure F1 pushed from the same instruction.
5130 Between F1 and now, something has matched (else this is an
5131 improper use of on_failure_jump_exclusive), so that we know
5132 that the fail-destination of F1 cannot match, hence we can
5133 pop F1 before pushing F2. Instead of doing this pop/push,
5134 we manually turn F1 into F2.
5135 `fail_stack.avail == fail_stack.frame' makes sure
5136 that popping F1 doesn't involve registers, else
5137 this optimization cannot be done so trivially. */
5138 assert (FAILURE_STR (TOP_FAILURE_HANDLE ()) != d);
5139 FAILURE_STR (TOP_FAILURE_HANDLE ()) = d;
5141 else
5142 PUSH_FAILURE_POINT (p - 3, d);
5143 break;
5145 case on_failure_jump_loop:
5146 on_failure:
5147 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5148 DEBUG_PRINT3 ("EXECUTING on_failure_jump_loop %d (to %p):\n",
5149 mcnt, p + mcnt);
5151 CHECK_INFINITE_LOOP (p - 3, d);
5152 PUSH_FAILURE_POINT (p - 3, d);
5153 break;
5156 /* Uses of on_failure_jump:
5158 Each alternative starts with an on_failure_jump that points
5159 to the beginning of the next alternative. Each alternative
5160 except the last ends with a jump that in effect jumps past
5161 the rest of the alternatives. (They really jump to the
5162 ending jump of the following alternative, because tensioning
5163 these jumps is a hassle.)
5165 Repeats start with an on_failure_jump that points past both
5166 the repetition text and either the following jump or
5167 pop_failure_jump back to this on_failure_jump. */
5168 case on_failure_jump:
5170 #if defined (WINDOWSNT) && defined (emacs)
5171 QUIT;
5172 #endif
5174 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5175 DEBUG_PRINT3 ("EXECUTING on_failure_jump %d (to %p):\n",
5176 mcnt, p + mcnt);
5178 PUSH_FAILURE_POINT (p -3, d);
5179 break;
5181 /* This operation is used for greedy * and +.
5182 Compare the beginning of the repeat with what in the
5183 pattern follows its end. If we can establish that there
5184 is nothing that they would both match, i.e., that we
5185 would have to backtrack because of (as in, e.g., `a*a')
5186 then we can use a non-backtracking loop based on
5187 on_failure_jump_exclusive instead of on_failure_jump_loop. */
5188 case on_failure_jump_smart:
5189 #if defined (WINDOWSNT) && defined (emacs)
5190 QUIT;
5191 #endif
5192 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5193 DEBUG_PRINT3 ("EXECUTING on_failure_jump_smart %d (to %p).\n",
5194 mcnt, p + mcnt);
5196 unsigned char *p1 = p; /* Next operation. */
5197 unsigned char *p2 = p + mcnt; /* Destination of the jump. */
5199 p -= 3; /* Reset so that we will re-execute the
5200 instruction once it's been changed. */
5202 /* DEBUG_STATEMENT (debug = 1); */
5203 if (mutually_exclusive_p (bufp, p1, p2))
5205 /* Use a fast `on_failure_keep_string_jump' loop. */
5206 *p = (unsigned char) on_failure_jump_exclusive;
5207 /* STORE_NUMBER (p2 - 2, mcnt + 3); */
5209 else
5211 /* Default to a safe `on_failure_jump' loop. */
5212 DEBUG_PRINT1 (" smart default => slow loop.\n");
5213 *p = (unsigned char) on_failure_jump_loop;
5215 /* DEBUG_STATEMENT (debug = 0); */
5217 break;
5219 /* Unconditionally jump (without popping any failure points). */
5220 case jump:
5221 unconditional_jump:
5222 #if defined (WINDOWSNT) && defined (emacs)
5223 QUIT;
5224 #endif
5225 EXTRACT_NUMBER_AND_INCR (mcnt, p); /* Get the amount to jump. */
5226 DEBUG_PRINT2 ("EXECUTING jump %d ", mcnt);
5227 p += mcnt; /* Do the jump. */
5228 DEBUG_PRINT2 ("(to %p).\n", p);
5229 break;
5232 /* Have to succeed matching what follows at least n times.
5233 After that, handle like `on_failure_jump'. */
5234 case succeed_n:
5235 EXTRACT_NUMBER (mcnt, p + 2);
5236 DEBUG_PRINT2 ("EXECUTING succeed_n %d.\n", mcnt);
5238 assert (mcnt >= 0);
5239 /* Originally, this is how many times we HAVE to succeed. */
5240 if (mcnt > 0)
5242 mcnt--;
5243 p += 2;
5244 STORE_NUMBER_AND_INCR (p, mcnt);
5245 DEBUG_PRINT3 (" Setting %p to %d.\n", p, mcnt);
5247 else if (mcnt == 0)
5249 DEBUG_PRINT2 (" Setting two bytes from %p to no_op.\n", p+2);
5250 p[2] = (unsigned char) no_op;
5251 p[3] = (unsigned char) no_op;
5252 goto on_failure;
5254 break;
5256 case jump_n:
5257 EXTRACT_NUMBER (mcnt, p + 2);
5258 DEBUG_PRINT2 ("EXECUTING jump_n %d.\n", mcnt);
5260 /* Originally, this is how many times we CAN jump. */
5261 if (mcnt)
5263 mcnt--;
5264 STORE_NUMBER (p + 2, mcnt);
5265 goto unconditional_jump;
5267 /* If don't have to jump any more, skip over the rest of command. */
5268 else
5269 p += 4;
5270 break;
5272 case set_number_at:
5274 DEBUG_PRINT1 ("EXECUTING set_number_at.\n");
5276 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5277 p1 = p + mcnt;
5278 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5279 DEBUG_PRINT3 (" Setting %p to %d.\n", p1, mcnt);
5280 STORE_NUMBER (p1, mcnt);
5281 break;
5284 case wordbound:
5285 DEBUG_PRINT1 ("EXECUTING wordbound.\n");
5287 /* We SUCCEED in one of the following cases: */
5289 /* Case 1: D is at the beginning or the end of string. */
5290 if (AT_STRINGS_BEG (d) || AT_STRINGS_END (d))
5291 break;
5292 else
5294 /* C1 is the character before D, S1 is the syntax of C1, C2
5295 is the character at D, and S2 is the syntax of C2. */
5296 int c1, c2, s1, s2;
5297 int pos1 = PTR_TO_OFFSET (d - 1);
5298 int charpos;
5300 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
5301 GET_CHAR_AFTER_2 (c2, d, string1, end1, string2, end2);
5302 #ifdef emacs
5303 charpos = SYNTAX_TABLE_BYTE_TO_CHAR (pos1);
5304 UPDATE_SYNTAX_TABLE (charpos);
5305 #endif
5306 s1 = SYNTAX (c1);
5307 #ifdef emacs
5308 UPDATE_SYNTAX_TABLE_FORWARD (charpos + 1);
5309 #endif
5310 s2 = SYNTAX (c2);
5312 if (/* Case 2: Only one of S1 and S2 is Sword. */
5313 ((s1 == Sword) != (s2 == Sword))
5314 /* Case 3: Both of S1 and S2 are Sword, and macro
5315 WORD_BOUNDARY_P (C1, C2) returns nonzero. */
5316 || ((s1 == Sword) && WORD_BOUNDARY_P (c1, c2)))
5317 break;
5319 goto fail;
5321 case notwordbound:
5322 DEBUG_PRINT1 ("EXECUTING notwordbound.\n");
5324 /* We FAIL in one of the following cases: */
5326 /* Case 1: D is at the beginning or the end of string. */
5327 if (AT_STRINGS_BEG (d) || AT_STRINGS_END (d))
5328 goto fail;
5329 else
5331 /* C1 is the character before D, S1 is the syntax of C1, C2
5332 is the character at D, and S2 is the syntax of C2. */
5333 int c1, c2, s1, s2;
5334 int pos1 = PTR_TO_OFFSET (d - 1);
5335 int charpos;
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 charpos = SYNTAX_TABLE_BYTE_TO_CHAR (pos1);
5341 UPDATE_SYNTAX_TABLE (charpos);
5342 #endif
5343 s1 = SYNTAX (c1);
5344 #ifdef emacs
5345 UPDATE_SYNTAX_TABLE_FORWARD (charpos + 1);
5346 #endif
5347 s2 = SYNTAX (c2);
5349 if (/* Case 2: Only one of S1 and S2 is Sword. */
5350 ((s1 == Sword) != (s2 == Sword))
5351 /* Case 3: Both of S1 and S2 are Sword, and macro
5352 WORD_BOUNDARY_P (C1, C2) returns nonzero. */
5353 || ((s1 == Sword) && WORD_BOUNDARY_P (c1, c2)))
5354 goto fail;
5356 break;
5358 case wordbeg:
5359 DEBUG_PRINT1 ("EXECUTING wordbeg.\n");
5361 /* We FAIL in one of the following cases: */
5363 /* Case 1: D is at the end of string. */
5364 if (AT_STRINGS_END (d))
5365 goto fail;
5366 else
5368 /* C1 is the character before D, S1 is the syntax of C1, C2
5369 is the character at D, and S2 is the syntax of C2. */
5370 int c1, c2, s1, s2;
5371 int pos1 = PTR_TO_OFFSET (d);
5372 int charpos;
5374 GET_CHAR_AFTER_2 (c2, d, string1, end1, string2, end2);
5375 #ifdef emacs
5376 charpos = SYNTAX_TABLE_BYTE_TO_CHAR (pos1);
5377 UPDATE_SYNTAX_TABLE (charpos);
5378 #endif
5379 s2 = SYNTAX (c2);
5381 /* Case 2: S2 is not Sword. */
5382 if (s2 != Sword)
5383 goto fail;
5385 /* Case 3: D is not at the beginning of string ... */
5386 if (!AT_STRINGS_BEG (d))
5388 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
5389 #ifdef emacs
5390 UPDATE_SYNTAX_TABLE_BACKWARD (charpos - 1);
5391 #endif
5392 s1 = SYNTAX (c1);
5394 /* ... and S1 is Sword, and WORD_BOUNDARY_P (C1, C2)
5395 returns 0. */
5396 if ((s1 == Sword) && !WORD_BOUNDARY_P (c1, c2))
5397 goto fail;
5400 break;
5402 case wordend:
5403 DEBUG_PRINT1 ("EXECUTING wordend.\n");
5405 /* We FAIL in one of the following cases: */
5407 /* Case 1: D is at the beginning of string. */
5408 if (AT_STRINGS_BEG (d))
5409 goto fail;
5410 else
5412 /* C1 is the character before D, S1 is the syntax of C1, C2
5413 is the character at D, and S2 is the syntax of C2. */
5414 int c1, c2, s1, s2;
5415 int pos1 = PTR_TO_OFFSET (d);
5416 int charpos;
5418 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
5419 #ifdef emacs
5420 charpos = SYNTAX_TABLE_BYTE_TO_CHAR (pos1 - 1);
5421 UPDATE_SYNTAX_TABLE (charpos);
5422 #endif
5423 s1 = SYNTAX (c1);
5425 /* Case 2: S1 is not Sword. */
5426 if (s1 != Sword)
5427 goto fail;
5429 /* Case 3: D is not at the end of string ... */
5430 if (!AT_STRINGS_END (d))
5432 GET_CHAR_AFTER_2 (c2, d, string1, end1, string2, end2);
5433 #ifdef emacs
5434 UPDATE_SYNTAX_TABLE_FORWARD (charpos);
5435 #endif
5436 s2 = SYNTAX (c2);
5438 /* ... and S2 is Sword, and WORD_BOUNDARY_P (C1, C2)
5439 returns 0. */
5440 if ((s2 == Sword) && !WORD_BOUNDARY_P (c1, c2))
5441 goto fail;
5444 break;
5446 #ifdef emacs
5447 case before_dot:
5448 DEBUG_PRINT1 ("EXECUTING before_dot.\n");
5449 if (PTR_BYTE_POS ((unsigned char *) d) >= PT_BYTE)
5450 goto fail;
5451 break;
5453 case at_dot:
5454 DEBUG_PRINT1 ("EXECUTING at_dot.\n");
5455 if (PTR_BYTE_POS ((unsigned char *) d) != PT_BYTE)
5456 goto fail;
5457 break;
5459 case after_dot:
5460 DEBUG_PRINT1 ("EXECUTING after_dot.\n");
5461 if (PTR_BYTE_POS ((unsigned char *) d) <= PT_BYTE)
5462 goto fail;
5463 break;
5465 case syntaxspec:
5466 DEBUG_PRINT2 ("EXECUTING syntaxspec %d.\n", mcnt);
5467 mcnt = *p++;
5468 goto matchsyntax;
5470 case wordchar:
5471 DEBUG_PRINT1 ("EXECUTING Emacs wordchar.\n");
5472 mcnt = (int) Sword;
5473 matchsyntax:
5474 PREFETCH ();
5475 #ifdef emacs
5477 int pos1 = SYNTAX_TABLE_BYTE_TO_CHAR (PTR_TO_OFFSET (d));
5478 UPDATE_SYNTAX_TABLE (pos1);
5480 #endif
5482 int c, len;
5484 if (multibyte)
5485 /* we must concern about multibyte form, ... */
5486 c = STRING_CHAR_AND_LENGTH (d, dend - d, len);
5487 else
5488 /* everything should be handled as ASCII, even though it
5489 looks like multibyte form. */
5490 c = *d, len = 1;
5492 if (SYNTAX (c) != (enum syntaxcode) mcnt)
5493 goto fail;
5494 d += len;
5496 break;
5498 case notsyntaxspec:
5499 DEBUG_PRINT2 ("EXECUTING notsyntaxspec %d.\n", mcnt);
5500 mcnt = *p++;
5501 goto matchnotsyntax;
5503 case notwordchar:
5504 DEBUG_PRINT1 ("EXECUTING Emacs notwordchar.\n");
5505 mcnt = (int) Sword;
5506 matchnotsyntax:
5507 PREFETCH ();
5508 #ifdef emacs
5510 int pos1 = SYNTAX_TABLE_BYTE_TO_CHAR (PTR_TO_OFFSET (d));
5511 UPDATE_SYNTAX_TABLE (pos1);
5513 #endif
5515 int c, len;
5517 if (multibyte)
5518 c = STRING_CHAR_AND_LENGTH (d, dend - d, len);
5519 else
5520 c = *d, len = 1;
5522 if (SYNTAX (c) == (enum syntaxcode) mcnt)
5523 goto fail;
5524 d += len;
5526 break;
5528 case categoryspec:
5529 DEBUG_PRINT2 ("EXECUTING categoryspec %d.\n", *p);
5530 mcnt = *p++;
5531 PREFETCH ();
5533 int c, len;
5535 if (multibyte)
5536 c = STRING_CHAR_AND_LENGTH (d, dend - d, len);
5537 else
5538 c = *d, len = 1;
5540 if (!CHAR_HAS_CATEGORY (c, mcnt))
5541 goto fail;
5542 d += len;
5544 break;
5546 case notcategoryspec:
5547 DEBUG_PRINT2 ("EXECUTING notcategoryspec %d.\n", *p);
5548 mcnt = *p++;
5549 PREFETCH ();
5551 int c, len;
5553 if (multibyte)
5554 c = STRING_CHAR_AND_LENGTH (d, dend - d, len);
5555 else
5556 c = *d, len = 1;
5558 if (CHAR_HAS_CATEGORY (c, mcnt))
5559 goto fail;
5560 d += len;
5562 break;
5564 #else /* not emacs */
5565 case wordchar:
5566 DEBUG_PRINT1 ("EXECUTING non-Emacs wordchar.\n");
5567 PREFETCH ();
5568 if (!WORDCHAR_P (d))
5569 goto fail;
5570 d++;
5571 break;
5573 case notwordchar:
5574 DEBUG_PRINT1 ("EXECUTING non-Emacs notwordchar.\n");
5575 PREFETCH ();
5576 if (WORDCHAR_P (d))
5577 goto fail;
5578 d++;
5579 break;
5580 #endif /* not emacs */
5582 default:
5583 abort ();
5585 continue; /* Successfully executed one pattern command; keep going. */
5588 /* We goto here if a matching operation fails. */
5589 fail:
5590 #if defined (WINDOWSNT) && defined (emacs)
5591 QUIT;
5592 #endif
5593 if (!FAIL_STACK_EMPTY ())
5595 char *str;
5596 unsigned char *pat;
5597 /* A restart point is known. Restore to that state. */
5598 DEBUG_PRINT1 ("\nFAIL:\n");
5599 POP_FAILURE_POINT (str, pat);
5600 switch (SWITCH_ENUM_CAST ((re_opcode_t) *pat++))
5602 case on_failure_keep_string_jump:
5603 assert (str == NULL);
5604 goto continue_failure_jump;
5606 case on_failure_jump_exclusive:
5607 /* If something has matched, the alternative will not match,
5608 so we might as well keep popping right away. */
5609 if (0 /* d != str && d != string2 */) /* Don't bother. -sm */
5610 /* (d == string2 && str == end1) => (d =~ str) */
5611 goto fail;
5612 /* Fallthrough */
5614 case on_failure_jump_loop:
5615 case on_failure_jump:
5616 case succeed_n:
5617 d = str;
5618 continue_failure_jump:
5619 EXTRACT_NUMBER_AND_INCR (mcnt, pat);
5620 p = pat + mcnt;
5621 break;
5623 default:
5624 abort();
5627 assert (p >= bufp->buffer && p <= pend);
5629 if (d >= string1 && d <= end1)
5630 dend = end_match_1;
5632 else
5633 break; /* Matching at this starting point really fails. */
5634 } /* for (;;) */
5636 if (best_regs_set)
5637 goto restore_best_regs;
5639 FREE_VARIABLES ();
5641 return -1; /* Failure to match. */
5642 } /* re_match_2 */
5644 /* Subroutine definitions for re_match_2. */
5646 /* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
5647 bytes; nonzero otherwise. */
5649 static int
5650 bcmp_translate (s1, s2, len, translate)
5651 unsigned char *s1, *s2;
5652 register int len;
5653 RE_TRANSLATE_TYPE translate;
5655 register unsigned char *p1 = s1, *p2 = s2;
5656 unsigned char *p1_end = s1 + len;
5657 unsigned char *p2_end = s2 + len;
5659 while (p1 != p1_end && p2 != p2_end)
5661 int p1_charlen, p2_charlen;
5662 int p1_ch, p2_ch;
5664 p1_ch = STRING_CHAR_AND_LENGTH (p1, p1_end - p1, p1_charlen);
5665 p2_ch = STRING_CHAR_AND_LENGTH (p2, p2_end - p2, p2_charlen);
5667 if (RE_TRANSLATE (translate, p1_ch)
5668 != RE_TRANSLATE (translate, p2_ch))
5669 return 1;
5671 p1 += p1_charlen, p2 += p2_charlen;
5674 if (p1 != p1_end || p2 != p2_end)
5675 return 1;
5677 return 0;
5680 /* Entry points for GNU code. */
5682 /* re_compile_pattern is the GNU regular expression compiler: it
5683 compiles PATTERN (of length SIZE) and puts the result in BUFP.
5684 Returns 0 if the pattern was valid, otherwise an error string.
5686 Assumes the `allocated' (and perhaps `buffer') and `translate' fields
5687 are set in BUFP on entry.
5689 We call regex_compile to do the actual compilation. */
5691 const char *
5692 re_compile_pattern (pattern, length, bufp)
5693 const char *pattern;
5694 int length;
5695 struct re_pattern_buffer *bufp;
5697 reg_errcode_t ret;
5699 /* GNU code is written to assume at least RE_NREGS registers will be set
5700 (and at least one extra will be -1). */
5701 bufp->regs_allocated = REGS_UNALLOCATED;
5703 /* And GNU code determines whether or not to get register information
5704 by passing null for the REGS argument to re_match, etc., not by
5705 setting no_sub. */
5706 bufp->no_sub = 0;
5708 /* Match anchors at newline. */
5709 bufp->newline_anchor = 1;
5711 ret = regex_compile (pattern, length, re_syntax_options, bufp);
5713 if (!ret)
5714 return NULL;
5715 return gettext (re_error_msgid[(int) ret]);
5718 /* Entry points compatible with 4.2 BSD regex library. We don't define
5719 them unless specifically requested. */
5721 #if defined (_REGEX_RE_COMP) || defined (_LIBC)
5723 /* BSD has one and only one pattern buffer. */
5724 static struct re_pattern_buffer re_comp_buf;
5726 char *
5727 #ifdef _LIBC
5728 /* Make these definitions weak in libc, so POSIX programs can redefine
5729 these names if they don't use our functions, and still use
5730 regcomp/regexec below without link errors. */
5731 weak_function
5732 #endif
5733 re_comp (s)
5734 const char *s;
5736 reg_errcode_t ret;
5738 if (!s)
5740 if (!re_comp_buf.buffer)
5741 return gettext ("No previous regular expression");
5742 return 0;
5745 if (!re_comp_buf.buffer)
5747 re_comp_buf.buffer = (unsigned char *) malloc (200);
5748 if (re_comp_buf.buffer == NULL)
5749 return gettext (re_error_msgid[(int) REG_ESPACE]);
5750 re_comp_buf.allocated = 200;
5752 re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH);
5753 if (re_comp_buf.fastmap == NULL)
5754 return gettext (re_error_msgid[(int) REG_ESPACE]);
5757 /* Since `re_exec' always passes NULL for the `regs' argument, we
5758 don't need to initialize the pattern buffer fields which affect it. */
5760 /* Match anchors at newlines. */
5761 re_comp_buf.newline_anchor = 1;
5763 ret = regex_compile (s, strlen (s), re_syntax_options, &re_comp_buf);
5765 if (!ret)
5766 return NULL;
5768 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
5769 return (char *) gettext (re_error_msgid[(int) ret]);
5774 #ifdef _LIBC
5775 weak_function
5776 #endif
5777 re_exec (s)
5778 const char *s;
5780 const int len = strlen (s);
5781 return
5782 0 <= re_search (&re_comp_buf, s, len, 0, len, (struct re_registers *) 0);
5784 #endif /* _REGEX_RE_COMP */
5786 /* POSIX.2 functions. Don't define these for Emacs. */
5788 #ifndef emacs
5790 /* regcomp takes a regular expression as a string and compiles it.
5792 PREG is a regex_t *. We do not expect any fields to be initialized,
5793 since POSIX says we shouldn't. Thus, we set
5795 `buffer' to the compiled pattern;
5796 `used' to the length of the compiled pattern;
5797 `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
5798 REG_EXTENDED bit in CFLAGS is set; otherwise, to
5799 RE_SYNTAX_POSIX_BASIC;
5800 `newline_anchor' to REG_NEWLINE being set in CFLAGS;
5801 `fastmap' and `fastmap_accurate' to zero;
5802 `re_nsub' to the number of subexpressions in PATTERN.
5804 PATTERN is the address of the pattern string.
5806 CFLAGS is a series of bits which affect compilation.
5808 If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
5809 use POSIX basic syntax.
5811 If REG_NEWLINE is set, then . and [^...] don't match newline.
5812 Also, regexec will try a match beginning after every newline.
5814 If REG_ICASE is set, then we considers upper- and lowercase
5815 versions of letters to be equivalent when matching.
5817 If REG_NOSUB is set, then when PREG is passed to regexec, that
5818 routine will report only success or failure, and nothing about the
5819 registers.
5821 It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
5822 the return codes and their meanings.) */
5825 regcomp (preg, pattern, cflags)
5826 regex_t *preg;
5827 const char *pattern;
5828 int cflags;
5830 reg_errcode_t ret;
5831 unsigned syntax
5832 = (cflags & REG_EXTENDED) ?
5833 RE_SYNTAX_POSIX_EXTENDED : RE_SYNTAX_POSIX_BASIC;
5835 /* regex_compile will allocate the space for the compiled pattern. */
5836 preg->buffer = 0;
5837 preg->allocated = 0;
5838 preg->used = 0;
5840 /* Don't bother to use a fastmap when searching. This simplifies the
5841 REG_NEWLINE case: if we used a fastmap, we'd have to put all the
5842 characters after newlines into the fastmap. This way, we just try
5843 every character. */
5844 preg->fastmap = 0;
5846 if (cflags & REG_ICASE)
5848 unsigned i;
5850 preg->translate
5851 = (RE_TRANSLATE_TYPE) malloc (CHAR_SET_SIZE
5852 * sizeof (*(RE_TRANSLATE_TYPE)0));
5853 if (preg->translate == NULL)
5854 return (int) REG_ESPACE;
5856 /* Map uppercase characters to corresponding lowercase ones. */
5857 for (i = 0; i < CHAR_SET_SIZE; i++)
5858 preg->translate[i] = ISUPPER (i) ? tolower (i) : i;
5860 else
5861 preg->translate = NULL;
5863 /* If REG_NEWLINE is set, newlines are treated differently. */
5864 if (cflags & REG_NEWLINE)
5865 { /* REG_NEWLINE implies neither . nor [^...] match newline. */
5866 syntax &= ~RE_DOT_NEWLINE;
5867 syntax |= RE_HAT_LISTS_NOT_NEWLINE;
5868 /* It also changes the matching behavior. */
5869 preg->newline_anchor = 1;
5871 else
5872 preg->newline_anchor = 0;
5874 preg->no_sub = !!(cflags & REG_NOSUB);
5876 /* POSIX says a null character in the pattern terminates it, so we
5877 can use strlen here in compiling the pattern. */
5878 ret = regex_compile (pattern, strlen (pattern), syntax, preg);
5880 /* POSIX doesn't distinguish between an unmatched open-group and an
5881 unmatched close-group: both are REG_EPAREN. */
5882 if (ret == REG_ERPAREN) ret = REG_EPAREN;
5884 return (int) ret;
5888 /* regexec searches for a given pattern, specified by PREG, in the
5889 string STRING.
5891 If NMATCH is zero or REG_NOSUB was set in the cflags argument to
5892 `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
5893 least NMATCH elements, and we set them to the offsets of the
5894 corresponding matched substrings.
5896 EFLAGS specifies `execution flags' which affect matching: if
5897 REG_NOTBOL is set, then ^ does not match at the beginning of the
5898 string; if REG_NOTEOL is set, then $ does not match at the end.
5900 We return 0 if we find a match and REG_NOMATCH if not. */
5903 regexec (preg, string, nmatch, pmatch, eflags)
5904 const regex_t *preg;
5905 const char *string;
5906 size_t nmatch;
5907 regmatch_t pmatch[];
5908 int eflags;
5910 int ret;
5911 struct re_registers regs;
5912 regex_t private_preg;
5913 int len = strlen (string);
5914 boolean want_reg_info = !preg->no_sub && nmatch > 0;
5916 private_preg = *preg;
5918 private_preg.not_bol = !!(eflags & REG_NOTBOL);
5919 private_preg.not_eol = !!(eflags & REG_NOTEOL);
5921 /* The user has told us exactly how many registers to return
5922 information about, via `nmatch'. We have to pass that on to the
5923 matching routines. */
5924 private_preg.regs_allocated = REGS_FIXED;
5926 if (want_reg_info)
5928 regs.num_regs = nmatch;
5929 regs.start = TALLOC (nmatch, regoff_t);
5930 regs.end = TALLOC (nmatch, regoff_t);
5931 if (regs.start == NULL || regs.end == NULL)
5932 return (int) REG_NOMATCH;
5935 /* Perform the searching operation. */
5936 ret = re_search (&private_preg, string, len,
5937 /* start: */ 0, /* range: */ len,
5938 want_reg_info ? &regs : (struct re_registers *) 0);
5940 /* Copy the register information to the POSIX structure. */
5941 if (want_reg_info)
5943 if (ret >= 0)
5945 unsigned r;
5947 for (r = 0; r < nmatch; r++)
5949 pmatch[r].rm_so = regs.start[r];
5950 pmatch[r].rm_eo = regs.end[r];
5954 /* If we needed the temporary register info, free the space now. */
5955 free (regs.start);
5956 free (regs.end);
5959 /* We want zero return to mean success, unlike `re_search'. */
5960 return ret >= 0 ? (int) REG_NOERROR : (int) REG_NOMATCH;
5964 /* Returns a message corresponding to an error code, ERRCODE, returned
5965 from either regcomp or regexec. We don't use PREG here. */
5967 size_t
5968 regerror (errcode, preg, errbuf, errbuf_size)
5969 int errcode;
5970 const regex_t *preg;
5971 char *errbuf;
5972 size_t errbuf_size;
5974 const char *msg;
5975 size_t msg_size;
5977 if (errcode < 0
5978 || errcode >= (sizeof (re_error_msgid) / sizeof (re_error_msgid[0])))
5979 /* Only error codes returned by the rest of the code should be passed
5980 to this routine. If we are given anything else, or if other regex
5981 code generates an invalid error code, then the program has a bug.
5982 Dump core so we can fix it. */
5983 abort ();
5985 msg = gettext (re_error_msgid[errcode]);
5987 msg_size = strlen (msg) + 1; /* Includes the null. */
5989 if (errbuf_size != 0)
5991 if (msg_size > errbuf_size)
5993 strncpy (errbuf, msg, errbuf_size - 1);
5994 errbuf[errbuf_size - 1] = 0;
5996 else
5997 strcpy (errbuf, msg);
6000 return msg_size;
6004 /* Free dynamically allocated space used by PREG. */
6006 void
6007 regfree (preg)
6008 regex_t *preg;
6010 if (preg->buffer != NULL)
6011 free (preg->buffer);
6012 preg->buffer = NULL;
6014 preg->allocated = 0;
6015 preg->used = 0;
6017 if (preg->fastmap != NULL)
6018 free (preg->fastmap);
6019 preg->fastmap = NULL;
6020 preg->fastmap_accurate = 0;
6022 if (preg->translate != NULL)
6023 free (preg->translate);
6024 preg->translate = NULL;
6027 #endif /* not emacs */