* regex.c: Declare a new type `re_char' used throughout the code for the
[emacs.git] / src / regex.c
blobc118af0fdb665b3a94c3adf5d94b7b5586b1e760
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 /* Type of source-pattern and string chars. */
438 typedef const unsigned char re_char;
440 typedef char boolean;
441 #define false 0
442 #define true 1
444 static int re_match_2_internal ();
446 /* These are the command codes that appear in compiled regular
447 expressions. Some opcodes are followed by argument bytes. A
448 command code can specify any interpretation whatsoever for its
449 arguments. Zero bytes may appear in the compiled regular expression. */
451 typedef enum
453 no_op = 0,
455 /* Succeed right away--no more backtracking. */
456 succeed,
458 /* Followed by one byte giving n, then by n literal bytes. */
459 exactn,
461 /* Matches any (more or less) character. */
462 anychar,
464 /* Matches any one char belonging to specified set. First
465 following byte is number of bitmap bytes. Then come bytes
466 for a bitmap saying which chars are in. Bits in each byte
467 are ordered low-bit-first. A character is in the set if its
468 bit is 1. A character too large to have a bit in the map is
469 automatically not in the set.
471 If the length byte has the 0x80 bit set, then that stuff
472 is followed by a range table:
473 2 bytes of flags for character sets (low 8 bits, high 8 bits)
474 See RANGE_TABLE_WORK_BITS below.
475 2 bytes, the number of pairs that follow
476 pairs, each 2 multibyte characters,
477 each multibyte character represented as 3 bytes. */
478 charset,
480 /* Same parameters as charset, but match any character that is
481 not one of those specified. */
482 charset_not,
484 /* Start remembering the text that is matched, for storing in a
485 register. Followed by one byte with the register number, in
486 the range 0 to one less than the pattern buffer's re_nsub
487 field. */
488 start_memory,
490 /* Stop remembering the text that is matched and store it in a
491 memory register. Followed by one byte with the register
492 number, in the range 0 to one less than `re_nsub' in the
493 pattern buffer. */
494 stop_memory,
496 /* Match a duplicate of something remembered. Followed by one
497 byte containing the register number. */
498 duplicate,
500 /* Fail unless at beginning of line. */
501 begline,
503 /* Fail unless at end of line. */
504 endline,
506 /* Succeeds if at beginning of buffer (if emacs) or at beginning
507 of string to be matched (if not). */
508 begbuf,
510 /* Analogously, for end of buffer/string. */
511 endbuf,
513 /* Followed by two byte relative address to which to jump. */
514 jump,
516 /* Followed by two-byte relative address of place to resume at
517 in case of failure. */
518 on_failure_jump,
520 /* Like on_failure_jump, but pushes a placeholder instead of the
521 current string position when executed. */
522 on_failure_keep_string_jump,
524 /* Like `on_failure_jump', except that it assumes that the
525 pattern following it is mutually exclusive with the pattern
526 at the destination of the jump: if one matches something,
527 the other won't match at all.
528 Always used via `on_failure_jump_smart'. */
529 on_failure_jump_exclusive,
531 /* Just like `on_failure_jump', except that it checks that we
532 don't get stuck in an infinite loop (matching an empty string
533 indefinitely). */
534 on_failure_jump_loop,
536 /* A smart `on_failure_jump' used for greedy * and + operators.
537 It analyses the loop before which it is put and if the
538 loop does not require backtracking, it changes itself to
539 `on_failure_jump_exclusive', else it just defaults to
540 changing itself into `on_failure_jump_loop'. */
541 on_failure_jump_smart,
543 /* Followed by two-byte relative address and two-byte number n.
544 After matching N times, jump to the address upon failure. */
545 succeed_n,
547 /* Followed by two-byte relative address, and two-byte number n.
548 Jump to the address N times, then fail. */
549 jump_n,
551 /* Set the following two-byte relative address to the
552 subsequent two-byte number. The address *includes* the two
553 bytes of number. */
554 set_number_at,
556 wordchar, /* Matches any word-constituent character. */
557 notwordchar, /* Matches any char that is not a word-constituent. */
559 wordbeg, /* Succeeds if at word beginning. */
560 wordend, /* Succeeds if at word end. */
562 wordbound, /* Succeeds if at a word boundary. */
563 notwordbound /* Succeeds if not at a word boundary. */
565 #ifdef emacs
566 ,before_dot, /* Succeeds if before point. */
567 at_dot, /* Succeeds if at point. */
568 after_dot, /* Succeeds if after point. */
570 /* Matches any character whose syntax is specified. Followed by
571 a byte which contains a syntax code, e.g., Sword. */
572 syntaxspec,
574 /* Matches any character whose syntax is not that specified. */
575 notsyntaxspec,
577 /* Matches any character whose category-set contains the specified
578 category. The operator is followed by a byte which contains a
579 category code (mnemonic ASCII character). */
580 categoryspec,
582 /* Matches any character whose category-set does not contain the
583 specified category. The operator is followed by a byte which
584 contains the category code (mnemonic ASCII character). */
585 notcategoryspec
586 #endif /* emacs */
587 } re_opcode_t;
589 /* Common operations on the compiled pattern. */
591 /* Store NUMBER in two contiguous bytes starting at DESTINATION. */
593 #define STORE_NUMBER(destination, number) \
594 do { \
595 (destination)[0] = (number) & 0377; \
596 (destination)[1] = (number) >> 8; \
597 } while (0)
599 /* Same as STORE_NUMBER, except increment DESTINATION to
600 the byte after where the number is stored. Therefore, DESTINATION
601 must be an lvalue. */
603 #define STORE_NUMBER_AND_INCR(destination, number) \
604 do { \
605 STORE_NUMBER (destination, number); \
606 (destination) += 2; \
607 } while (0)
609 /* Put into DESTINATION a number stored in two contiguous bytes starting
610 at SOURCE. */
612 #define EXTRACT_NUMBER(destination, source) \
613 do { \
614 (destination) = *(source) & 0377; \
615 (destination) += SIGN_EXTEND_CHAR (*((source) + 1)) << 8; \
616 } while (0)
618 #ifdef DEBUG
619 static void
620 extract_number (dest, source)
621 int *dest;
622 unsigned char *source;
624 int temp = SIGN_EXTEND_CHAR (*(source + 1));
625 *dest = *source & 0377;
626 *dest += temp << 8;
629 #ifndef EXTRACT_MACROS /* To debug the macros. */
630 #undef EXTRACT_NUMBER
631 #define EXTRACT_NUMBER(dest, src) extract_number (&dest, src)
632 #endif /* not EXTRACT_MACROS */
634 #endif /* DEBUG */
636 /* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
637 SOURCE must be an lvalue. */
639 #define EXTRACT_NUMBER_AND_INCR(destination, source) \
640 do { \
641 EXTRACT_NUMBER (destination, source); \
642 (source) += 2; \
643 } while (0)
645 #ifdef DEBUG
646 static void
647 extract_number_and_incr (destination, source)
648 int *destination;
649 unsigned char **source;
651 extract_number (destination, *source);
652 *source += 2;
655 #ifndef EXTRACT_MACROS
656 #undef EXTRACT_NUMBER_AND_INCR
657 #define EXTRACT_NUMBER_AND_INCR(dest, src) \
658 extract_number_and_incr (&dest, &src)
659 #endif /* not EXTRACT_MACROS */
661 #endif /* DEBUG */
663 /* Store a multibyte character in three contiguous bytes starting
664 DESTINATION, and increment DESTINATION to the byte after where the
665 character is stored. Therefore, DESTINATION must be an lvalue. */
667 #define STORE_CHARACTER_AND_INCR(destination, character) \
668 do { \
669 (destination)[0] = (character) & 0377; \
670 (destination)[1] = ((character) >> 8) & 0377; \
671 (destination)[2] = (character) >> 16; \
672 (destination) += 3; \
673 } while (0)
675 /* Put into DESTINATION a character stored in three contiguous bytes
676 starting at SOURCE. */
678 #define EXTRACT_CHARACTER(destination, source) \
679 do { \
680 (destination) = ((source)[0] \
681 | ((source)[1] << 8) \
682 | ((source)[2] << 16)); \
683 } while (0)
686 /* Macros for charset. */
688 /* Size of bitmap of charset P in bytes. P is a start of charset,
689 i.e. *P is (re_opcode_t) charset or (re_opcode_t) charset_not. */
690 #define CHARSET_BITMAP_SIZE(p) ((p)[1] & 0x7F)
692 /* Nonzero if charset P has range table. */
693 #define CHARSET_RANGE_TABLE_EXISTS_P(p) ((p)[1] & 0x80)
695 /* Return the address of range table of charset P. But not the start
696 of table itself, but the before where the number of ranges is
697 stored. `2 +' means to skip re_opcode_t and size of bitmap,
698 and the 2 bytes of flags at the start of the range table. */
699 #define CHARSET_RANGE_TABLE(p) (&(p)[4 + CHARSET_BITMAP_SIZE (p)])
701 /* Extract the bit flags that start a range table. */
702 #define CHARSET_RANGE_TABLE_BITS(p) \
703 ((p)[2 + CHARSET_BITMAP_SIZE (p)] \
704 + (p)[3 + CHARSET_BITMAP_SIZE (p)] * 0x100)
706 /* Test if C is listed in the bitmap of charset P. */
707 #define CHARSET_LOOKUP_BITMAP(p, c) \
708 ((c) < CHARSET_BITMAP_SIZE (p) * BYTEWIDTH \
709 && (p)[2 + (c) / BYTEWIDTH] & (1 << ((c) % BYTEWIDTH)))
711 /* Return the address of end of RANGE_TABLE. COUNT is number of
712 ranges (which is a pair of (start, end)) in the RANGE_TABLE. `* 2'
713 is start of range and end of range. `* 3' is size of each start
714 and end. */
715 #define CHARSET_RANGE_TABLE_END(range_table, count) \
716 ((range_table) + (count) * 2 * 3)
718 /* Test if C is in RANGE_TABLE. A flag NOT is negated if C is in.
719 COUNT is number of ranges in RANGE_TABLE. */
720 #define CHARSET_LOOKUP_RANGE_TABLE_RAW(not, c, range_table, count) \
721 do \
723 int range_start, range_end; \
724 unsigned char *p; \
725 unsigned char *range_table_end \
726 = CHARSET_RANGE_TABLE_END ((range_table), (count)); \
728 for (p = (range_table); p < range_table_end; p += 2 * 3) \
730 EXTRACT_CHARACTER (range_start, p); \
731 EXTRACT_CHARACTER (range_end, p + 3); \
733 if (range_start <= (c) && (c) <= range_end) \
735 (not) = !(not); \
736 break; \
740 while (0)
742 /* Test if C is in range table of CHARSET. The flag NOT is negated if
743 C is listed in it. */
744 #define CHARSET_LOOKUP_RANGE_TABLE(not, c, charset) \
745 do \
747 /* Number of ranges in range table. */ \
748 int count; \
749 unsigned char *range_table = CHARSET_RANGE_TABLE (charset); \
751 EXTRACT_NUMBER_AND_INCR (count, range_table); \
752 CHARSET_LOOKUP_RANGE_TABLE_RAW ((not), (c), range_table, count); \
754 while (0)
756 /* If DEBUG is defined, Regex prints many voluminous messages about what
757 it is doing (if the variable `debug' is nonzero). If linked with the
758 main program in `iregex.c', you can enter patterns and strings
759 interactively. And if linked with the main program in `main.c' and
760 the other test files, you can run the already-written tests. */
762 #ifdef DEBUG
764 /* We use standard I/O for debugging. */
765 #include <stdio.h>
767 /* It is useful to test things that ``must'' be true when debugging. */
768 #include <assert.h>
770 static int debug = 0;
772 #define DEBUG_STATEMENT(e) e
773 #define DEBUG_PRINT1(x) if (debug) printf (x)
774 #define DEBUG_PRINT2(x1, x2) if (debug) printf (x1, x2)
775 #define DEBUG_PRINT3(x1, x2, x3) if (debug) printf (x1, x2, x3)
776 #define DEBUG_PRINT4(x1, x2, x3, x4) if (debug) printf (x1, x2, x3, x4)
777 #define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) \
778 if (debug) print_partial_compiled_pattern (s, e)
779 #define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \
780 if (debug) print_double_string (w, s1, sz1, s2, sz2)
783 /* Print the fastmap in human-readable form. */
785 void
786 print_fastmap (fastmap)
787 char *fastmap;
789 unsigned was_a_range = 0;
790 unsigned i = 0;
792 while (i < (1 << BYTEWIDTH))
794 if (fastmap[i++])
796 was_a_range = 0;
797 putchar (i - 1);
798 while (i < (1 << BYTEWIDTH) && fastmap[i])
800 was_a_range = 1;
801 i++;
803 if (was_a_range)
805 printf ("-");
806 putchar (i - 1);
810 putchar ('\n');
814 /* Print a compiled pattern string in human-readable form, starting at
815 the START pointer into it and ending just before the pointer END. */
817 void
818 print_partial_compiled_pattern (start, end)
819 unsigned char *start;
820 unsigned char *end;
822 int mcnt, mcnt2;
823 unsigned char *p = start;
824 unsigned char *pend = end;
826 if (start == NULL)
828 printf ("(null)\n");
829 return;
832 /* Loop over pattern commands. */
833 while (p < pend)
835 printf ("%d:\t", p - start);
837 switch ((re_opcode_t) *p++)
839 case no_op:
840 printf ("/no_op");
841 break;
843 case exactn:
844 mcnt = *p++;
845 printf ("/exactn/%d", mcnt);
848 putchar ('/');
849 putchar (*p++);
851 while (--mcnt);
852 break;
854 case start_memory:
855 printf ("/start_memory/%d", *p++);
856 break;
858 case stop_memory:
859 printf ("/stop_memory/%d", *p++);
860 break;
862 case duplicate:
863 printf ("/duplicate/%d", *p++);
864 break;
866 case anychar:
867 printf ("/anychar");
868 break;
870 case charset:
871 case charset_not:
873 register int c, last = -100;
874 register int in_range = 0;
875 int length = *p & 0x7f;
876 int has_range_table = *p & 0x80;
877 int range_length = p[length + 2] + p[length + 3] * 0x100;
879 printf ("/charset [%s",
880 (re_opcode_t) *(p - 1) == charset_not ? "^" : "");
882 assert (p + *p < pend);
884 for (c = 0; c < 256; c++)
885 if (c / 8 < length
886 && (p[1 + (c/8)] & (1 << (c % 8))))
888 /* Are we starting a range? */
889 if (last + 1 == c && ! in_range)
891 putchar ('-');
892 in_range = 1;
894 /* Have we broken a range? */
895 else if (last + 1 != c && in_range)
897 putchar (last);
898 in_range = 0;
901 if (! in_range)
902 putchar (c);
904 last = c;
907 p += 1 + length;
909 if (in_range)
910 putchar (last);
912 putchar (']');
914 if (has_range_table)
915 printf ("has-range-table");
917 /* ??? Should print the range table; for now,
918 just skip it. */
919 if (has_range_table)
920 p += 4 + 6 * range_length;
922 break;
924 case begline:
925 printf ("/begline");
926 break;
928 case endline:
929 printf ("/endline");
930 break;
932 case on_failure_jump:
933 extract_number_and_incr (&mcnt, &p);
934 printf ("/on_failure_jump to %d", p + mcnt - start);
935 break;
937 case on_failure_keep_string_jump:
938 extract_number_and_incr (&mcnt, &p);
939 printf ("/on_failure_keep_string_jump to %d", p + mcnt - start);
940 break;
942 case on_failure_jump_exclusive:
943 extract_number_and_incr (&mcnt, &p);
944 printf ("/on_failure_jump_exclusive to %d", p + mcnt - start);
945 break;
947 case on_failure_jump_loop:
948 extract_number_and_incr (&mcnt, &p);
949 printf ("/on_failure_jump_loop to %d", p + mcnt - start);
950 break;
952 case on_failure_jump_smart:
953 extract_number_and_incr (&mcnt, &p);
954 printf ("/on_failure_jump_smart to %d", p + mcnt - start);
955 break;
957 case jump:
958 extract_number_and_incr (&mcnt, &p);
959 printf ("/jump to %d", p + mcnt - start);
960 break;
962 case succeed_n:
963 extract_number_and_incr (&mcnt, &p);
964 extract_number_and_incr (&mcnt2, &p);
965 printf ("/succeed_n to %d, %d times", p + mcnt - start, mcnt2);
966 break;
968 case jump_n:
969 extract_number_and_incr (&mcnt, &p);
970 extract_number_and_incr (&mcnt2, &p);
971 printf ("/jump_n to %d, %d times", p + mcnt - start, mcnt2);
972 break;
974 case set_number_at:
975 extract_number_and_incr (&mcnt, &p);
976 extract_number_and_incr (&mcnt2, &p);
977 printf ("/set_number_at location %d to %d", p + mcnt - start, mcnt2);
978 break;
980 case wordbound:
981 printf ("/wordbound");
982 break;
984 case notwordbound:
985 printf ("/notwordbound");
986 break;
988 case wordbeg:
989 printf ("/wordbeg");
990 break;
992 case wordend:
993 printf ("/wordend");
995 #ifdef emacs
996 case before_dot:
997 printf ("/before_dot");
998 break;
1000 case at_dot:
1001 printf ("/at_dot");
1002 break;
1004 case after_dot:
1005 printf ("/after_dot");
1006 break;
1008 case syntaxspec:
1009 printf ("/syntaxspec");
1010 mcnt = *p++;
1011 printf ("/%d", mcnt);
1012 break;
1014 case notsyntaxspec:
1015 printf ("/notsyntaxspec");
1016 mcnt = *p++;
1017 printf ("/%d", mcnt);
1018 break;
1019 #endif /* emacs */
1021 case wordchar:
1022 printf ("/wordchar");
1023 break;
1025 case notwordchar:
1026 printf ("/notwordchar");
1027 break;
1029 case begbuf:
1030 printf ("/begbuf");
1031 break;
1033 case endbuf:
1034 printf ("/endbuf");
1035 break;
1037 default:
1038 printf ("?%d", *(p-1));
1041 putchar ('\n');
1044 printf ("%d:\tend of pattern.\n", p - start);
1048 void
1049 print_compiled_pattern (bufp)
1050 struct re_pattern_buffer *bufp;
1052 unsigned char *buffer = bufp->buffer;
1054 print_partial_compiled_pattern (buffer, buffer + bufp->used);
1055 printf ("%ld bytes used/%ld bytes allocated.\n", bufp->used, bufp->allocated);
1057 if (bufp->fastmap_accurate && bufp->fastmap)
1059 printf ("fastmap: ");
1060 print_fastmap (bufp->fastmap);
1063 printf ("re_nsub: %d\t", bufp->re_nsub);
1064 printf ("regs_alloc: %d\t", bufp->regs_allocated);
1065 printf ("can_be_null: %d\t", bufp->can_be_null);
1066 printf ("newline_anchor: %d\n", bufp->newline_anchor);
1067 printf ("no_sub: %d\t", bufp->no_sub);
1068 printf ("not_bol: %d\t", bufp->not_bol);
1069 printf ("not_eol: %d\t", bufp->not_eol);
1070 printf ("syntax: %d\n", bufp->syntax);
1071 fflush (stdout);
1072 /* Perhaps we should print the translate table? */
1076 void
1077 print_double_string (where, string1, size1, string2, size2)
1078 re_char *where;
1079 re_char *string1;
1080 re_char *string2;
1081 int size1;
1082 int size2;
1084 unsigned this_char;
1086 if (where == NULL)
1087 printf ("(null)");
1088 else
1090 if (FIRST_STRING_P (where))
1092 for (this_char = where - string1; this_char < size1; this_char++)
1093 putchar (string1[this_char]);
1095 where = string2;
1098 for (this_char = where - string2; this_char < size2; this_char++)
1099 putchar (string2[this_char]);
1103 #else /* not DEBUG */
1105 #undef assert
1106 #define assert(e)
1108 #define DEBUG_STATEMENT(e)
1109 #define DEBUG_PRINT1(x)
1110 #define DEBUG_PRINT2(x1, x2)
1111 #define DEBUG_PRINT3(x1, x2, x3)
1112 #define DEBUG_PRINT4(x1, x2, x3, x4)
1113 #define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
1114 #define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
1116 #endif /* not DEBUG */
1118 /* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
1119 also be assigned to arbitrarily: each pattern buffer stores its own
1120 syntax, so it can be changed between regex compilations. */
1121 /* This has no initializer because initialized variables in Emacs
1122 become read-only after dumping. */
1123 reg_syntax_t re_syntax_options;
1126 /* Specify the precise syntax of regexps for compilation. This provides
1127 for compatibility for various utilities which historically have
1128 different, incompatible syntaxes.
1130 The argument SYNTAX is a bit mask comprised of the various bits
1131 defined in regex.h. We return the old syntax. */
1133 reg_syntax_t
1134 re_set_syntax (syntax)
1135 reg_syntax_t syntax;
1137 reg_syntax_t ret = re_syntax_options;
1139 re_syntax_options = syntax;
1140 return ret;
1143 /* This table gives an error message for each of the error codes listed
1144 in regex.h. Obviously the order here has to be same as there.
1145 POSIX doesn't require that we do anything for REG_NOERROR,
1146 but why not be nice? */
1148 static const char *re_error_msgid[] =
1150 gettext_noop ("Success"), /* REG_NOERROR */
1151 gettext_noop ("No match"), /* REG_NOMATCH */
1152 gettext_noop ("Invalid regular expression"), /* REG_BADPAT */
1153 gettext_noop ("Invalid collation character"), /* REG_ECOLLATE */
1154 gettext_noop ("Invalid character class name"), /* REG_ECTYPE */
1155 gettext_noop ("Trailing backslash"), /* REG_EESCAPE */
1156 gettext_noop ("Invalid back reference"), /* REG_ESUBREG */
1157 gettext_noop ("Unmatched [ or [^"), /* REG_EBRACK */
1158 gettext_noop ("Unmatched ( or \\("), /* REG_EPAREN */
1159 gettext_noop ("Unmatched \\{"), /* REG_EBRACE */
1160 gettext_noop ("Invalid content of \\{\\}"), /* REG_BADBR */
1161 gettext_noop ("Invalid range end"), /* REG_ERANGE */
1162 gettext_noop ("Memory exhausted"), /* REG_ESPACE */
1163 gettext_noop ("Invalid preceding regular expression"), /* REG_BADRPT */
1164 gettext_noop ("Premature end of regular expression"), /* REG_EEND */
1165 gettext_noop ("Regular expression too big"), /* REG_ESIZE */
1166 gettext_noop ("Unmatched ) or \\)"), /* REG_ERPAREN */
1169 /* Avoiding alloca during matching, to placate r_alloc. */
1171 /* Define MATCH_MAY_ALLOCATE unless we need to make sure that the
1172 searching and matching functions should not call alloca. On some
1173 systems, alloca is implemented in terms of malloc, and if we're
1174 using the relocating allocator routines, then malloc could cause a
1175 relocation, which might (if the strings being searched are in the
1176 ralloc heap) shift the data out from underneath the regexp
1177 routines.
1179 Here's another reason to avoid allocation: Emacs
1180 processes input from X in a signal handler; processing X input may
1181 call malloc; if input arrives while a matching routine is calling
1182 malloc, then we're scrod. But Emacs can't just block input while
1183 calling matching routines; then we don't notice interrupts when
1184 they come in. So, Emacs blocks input around all regexp calls
1185 except the matching calls, which it leaves unprotected, in the
1186 faith that they will not malloc. */
1188 /* Normally, this is fine. */
1189 #define MATCH_MAY_ALLOCATE
1191 /* When using GNU C, we are not REALLY using the C alloca, no matter
1192 what config.h may say. So don't take precautions for it. */
1193 #ifdef __GNUC__
1194 #undef C_ALLOCA
1195 #endif
1197 /* The match routines may not allocate if (1) they would do it with malloc
1198 and (2) it's not safe for them to use malloc.
1199 Note that if REL_ALLOC is defined, matching would not use malloc for the
1200 failure stack, but we would still use it for the register vectors;
1201 so REL_ALLOC should not affect this. */
1202 #if (defined (C_ALLOCA) || defined (REGEX_MALLOC)) && defined (emacs)
1203 #undef MATCH_MAY_ALLOCATE
1204 #endif
1207 /* Failure stack declarations and macros; both re_compile_fastmap and
1208 re_match_2 use a failure stack. These have to be macros because of
1209 REGEX_ALLOCATE_STACK. */
1212 /* Approximate number of failure points for which to initially allocate space
1213 when matching. If this number is exceeded, we allocate more
1214 space, so it is not a hard limit. */
1215 #ifndef INIT_FAILURE_ALLOC
1216 #define INIT_FAILURE_ALLOC 20
1217 #endif
1219 /* Roughly the maximum number of failure points on the stack. Would be
1220 exactly that if always used TYPICAL_FAILURE_SIZE items each time we failed.
1221 This is a variable only so users of regex can assign to it; we never
1222 change it ourselves. */
1223 #if defined (MATCH_MAY_ALLOCATE)
1224 /* Note that 4400 is enough to cause a crash on Alpha OSF/1,
1225 whose default stack limit is 2mb. In order for a larger
1226 value to work reliably, you have to try to make it accord
1227 with the process stack limit. */
1228 int re_max_failures = 40000;
1229 #else
1230 int re_max_failures = 4000;
1231 #endif
1233 union fail_stack_elt
1235 const unsigned char *pointer;
1236 unsigned int integer;
1239 typedef union fail_stack_elt fail_stack_elt_t;
1241 typedef struct
1243 fail_stack_elt_t *stack;
1244 unsigned size;
1245 unsigned avail; /* Offset of next open position. */
1246 unsigned frame; /* Offset of the cur constructed frame. */
1247 } fail_stack_type;
1249 #define PATTERN_STACK_EMPTY() (fail_stack.avail == 0)
1250 #define FAIL_STACK_EMPTY() (fail_stack.frame == 0)
1251 #define FAIL_STACK_FULL() (fail_stack.avail == fail_stack.size)
1254 /* Define macros to initialize and free the failure stack.
1255 Do `return -2' if the alloc fails. */
1257 #ifdef MATCH_MAY_ALLOCATE
1258 #define INIT_FAIL_STACK() \
1259 do { \
1260 fail_stack.stack = (fail_stack_elt_t *) \
1261 REGEX_ALLOCATE_STACK (INIT_FAILURE_ALLOC * TYPICAL_FAILURE_SIZE \
1262 * sizeof (fail_stack_elt_t)); \
1264 if (fail_stack.stack == NULL) \
1265 return -2; \
1267 fail_stack.size = INIT_FAILURE_ALLOC; \
1268 fail_stack.avail = 0; \
1269 fail_stack.frame = 0; \
1270 } while (0)
1272 #define RESET_FAIL_STACK() REGEX_FREE_STACK (fail_stack.stack)
1273 #else
1274 #define INIT_FAIL_STACK() \
1275 do { \
1276 fail_stack.avail = 0; \
1277 fail_stack.frame = 0; \
1278 } while (0)
1280 #define RESET_FAIL_STACK()
1281 #endif
1284 /* Double the size of FAIL_STACK, up to a limit
1285 which allows approximately `re_max_failures' items.
1287 Return 1 if succeeds, and 0 if either ran out of memory
1288 allocating space for it or it was already too large.
1290 REGEX_REALLOCATE_STACK requires `destination' be declared. */
1292 /* Factor to increase the failure stack size by
1293 when we increase it.
1294 This used to be 2, but 2 was too wasteful
1295 because the old discarded stacks added up to as much space
1296 were as ultimate, maximum-size stack. */
1297 #define FAIL_STACK_GROWTH_FACTOR 4
1299 #define GROW_FAIL_STACK(fail_stack) \
1300 (((fail_stack).size * sizeof (fail_stack_elt_t) \
1301 >= re_max_failures * TYPICAL_FAILURE_SIZE) \
1302 ? 0 \
1303 : ((fail_stack).stack \
1304 = (fail_stack_elt_t *) \
1305 REGEX_REALLOCATE_STACK ((fail_stack).stack, \
1306 (fail_stack).size * sizeof (fail_stack_elt_t), \
1307 MIN (re_max_failures * TYPICAL_FAILURE_SIZE, \
1308 ((fail_stack).size * sizeof (fail_stack_elt_t) \
1309 * FAIL_STACK_GROWTH_FACTOR))), \
1311 (fail_stack).stack == NULL \
1312 ? 0 \
1313 : ((fail_stack).size \
1314 = (MIN (re_max_failures * TYPICAL_FAILURE_SIZE, \
1315 ((fail_stack).size * sizeof (fail_stack_elt_t) \
1316 * FAIL_STACK_GROWTH_FACTOR)) \
1317 / sizeof (fail_stack_elt_t)), \
1318 1)))
1321 /* Push pointer POINTER on FAIL_STACK.
1322 Return 1 if was able to do so and 0 if ran out of memory allocating
1323 space to do so. */
1324 #define PUSH_PATTERN_OP(POINTER, FAIL_STACK) \
1325 ((FAIL_STACK_FULL () \
1326 && !GROW_FAIL_STACK (FAIL_STACK)) \
1327 ? 0 \
1328 : ((FAIL_STACK).stack[(FAIL_STACK).avail++].pointer = POINTER, \
1330 #define POP_PATTERN_OP() POP_FAILURE_POINTER ()
1332 /* Push a pointer value onto the failure stack.
1333 Assumes the variable `fail_stack'. Probably should only
1334 be called from within `PUSH_FAILURE_POINT'. */
1335 #define PUSH_FAILURE_POINTER(item) \
1336 fail_stack.stack[fail_stack.avail++].pointer = (unsigned char *) (item)
1338 /* This pushes an integer-valued item onto the failure stack.
1339 Assumes the variable `fail_stack'. Probably should only
1340 be called from within `PUSH_FAILURE_POINT'. */
1341 #define PUSH_FAILURE_INT(item) \
1342 fail_stack.stack[fail_stack.avail++].integer = (item)
1344 /* Push a fail_stack_elt_t value onto the failure stack.
1345 Assumes the variable `fail_stack'. Probably should only
1346 be called from within `PUSH_FAILURE_POINT'. */
1347 #define PUSH_FAILURE_ELT(item) \
1348 fail_stack.stack[fail_stack.avail++] = (item)
1350 /* These three POP... operations complement the three PUSH... operations.
1351 All assume that `fail_stack' is nonempty. */
1352 #define POP_FAILURE_POINTER() fail_stack.stack[--fail_stack.avail].pointer
1353 #define POP_FAILURE_INT() fail_stack.stack[--fail_stack.avail].integer
1354 #define POP_FAILURE_ELT() fail_stack.stack[--fail_stack.avail]
1356 /* Individual items aside from the registers. */
1357 #define NUM_NONREG_ITEMS 3
1359 /* Used to examine the stack (to detect infinite loops). */
1360 #define FAILURE_PAT(h) fail_stack.stack[(h) - 1].pointer
1361 #define FAILURE_STR(h) (fail_stack.stack[(h) - 2].pointer)
1362 #define NEXT_FAILURE_HANDLE(h) fail_stack.stack[(h) - 3].integer
1363 #define TOP_FAILURE_HANDLE() fail_stack.frame
1366 #define ENSURE_FAIL_STACK(space) \
1367 while (REMAINING_AVAIL_SLOTS <= space) { \
1368 if (!GROW_FAIL_STACK (fail_stack)) \
1369 return -2; \
1370 DEBUG_PRINT2 ("\n Doubled stack; size now: %d\n", (fail_stack).size);\
1371 DEBUG_PRINT2 (" slots available: %d\n", REMAINING_AVAIL_SLOTS);\
1374 /* Push register NUM onto the stack. */
1375 #define PUSH_FAILURE_REG(num) \
1376 do { \
1377 char *destination; \
1378 ENSURE_FAIL_STACK(3); \
1379 DEBUG_PRINT4 (" Push reg %d (spanning %p -> %p)\n", \
1380 num, regstart[num], regend[num]); \
1381 PUSH_FAILURE_POINTER (regstart[num]); \
1382 PUSH_FAILURE_POINTER (regend[num]); \
1383 PUSH_FAILURE_INT (num); \
1384 } while (0)
1386 /* Pop a saved register off the stack. */
1387 #define POP_FAILURE_REG() \
1388 do { \
1389 int reg = POP_FAILURE_INT (); \
1390 regend[reg] = POP_FAILURE_POINTER (); \
1391 regstart[reg] = POP_FAILURE_POINTER (); \
1392 DEBUG_PRINT4 (" Pop reg %d (spanning %p -> %p)\n", \
1393 reg, regstart[reg], regend[reg]); \
1394 } while (0)
1396 /* Check that we are not stuck in an infinite loop. */
1397 #define CHECK_INFINITE_LOOP(pat_cur, string_place) \
1398 do { \
1399 int failure = TOP_FAILURE_HANDLE(); \
1400 /* Check for infinite matching loops */ \
1401 while (failure > 0 && \
1402 (FAILURE_STR (failure) == string_place \
1403 || FAILURE_STR (failure) == NULL)) \
1405 assert (FAILURE_PAT (failure) >= bufp->buffer \
1406 && FAILURE_PAT (failure) <= bufp->buffer + bufp->used); \
1407 if (FAILURE_PAT (failure) == pat_cur) \
1408 goto fail; \
1409 DEBUG_PRINT2 (" Other pattern: %p\n", FAILURE_PAT (failure)); \
1410 failure = NEXT_FAILURE_HANDLE(failure); \
1412 DEBUG_PRINT2 (" Other string: %p\n", FAILURE_STR (failure)); \
1413 } while (0)
1415 /* Push the information about the state we will need
1416 if we ever fail back to it.
1418 Requires variables fail_stack, regstart, regend and
1419 num_regs be declared. GROW_FAIL_STACK requires `destination' be
1420 declared.
1422 Does `return FAILURE_CODE' if runs out of memory. */
1424 #define PUSH_FAILURE_POINT(pattern, string_place) \
1425 do { \
1426 char *destination; \
1427 /* Must be int, so when we don't save any registers, the arithmetic \
1428 of 0 + -1 isn't done as unsigned. */ \
1430 DEBUG_STATEMENT (failure_id++); \
1431 DEBUG_STATEMENT (nfailure_points_pushed++); \
1432 DEBUG_PRINT2 ("\nPUSH_FAILURE_POINT #%u:\n", failure_id); \
1433 DEBUG_PRINT2 (" Before push, next avail: %d\n", (fail_stack).avail); \
1434 DEBUG_PRINT2 (" size: %d\n", (fail_stack).size);\
1436 ENSURE_FAIL_STACK (NUM_NONREG_ITEMS); \
1438 DEBUG_PRINT1 ("\n"); \
1440 DEBUG_PRINT2 (" Push frame index: %d\n", fail_stack.frame); \
1441 PUSH_FAILURE_INT (fail_stack.frame); \
1443 DEBUG_PRINT2 (" Push string %p: `", string_place); \
1444 DEBUG_PRINT_DOUBLE_STRING (string_place, string1, size1, string2, size2);\
1445 DEBUG_PRINT1 ("'\n"); \
1446 PUSH_FAILURE_POINTER (string_place); \
1448 DEBUG_PRINT2 (" Push pattern %p: ", pattern); \
1449 DEBUG_PRINT_COMPILED_PATTERN (bufp, pattern, pend); \
1450 PUSH_FAILURE_POINTER (pattern); \
1452 /* Close the frame by moving the frame pointer past it. */ \
1453 fail_stack.frame = fail_stack.avail; \
1454 } while (0)
1456 /* Estimate the size of data pushed by a typical failure stack entry.
1457 An estimate is all we need, because all we use this for
1458 is to choose a limit for how big to make the failure stack. */
1460 #define TYPICAL_FAILURE_SIZE 20
1462 /* How many items can still be added to the stack without overflowing it. */
1463 #define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail)
1466 /* Pops what PUSH_FAIL_STACK pushes.
1468 We restore into the parameters, all of which should be lvalues:
1469 STR -- the saved data position.
1470 PAT -- the saved pattern position.
1471 REGSTART, REGEND -- arrays of string positions.
1473 Also assumes the variables `fail_stack' and (if debugging), `bufp',
1474 `pend', `string1', `size1', `string2', and `size2'. */
1476 #define POP_FAILURE_POINT(str, pat) \
1477 do { \
1478 assert (!FAIL_STACK_EMPTY ()); \
1480 /* Remove failure points and point to how many regs pushed. */ \
1481 DEBUG_PRINT1 ("POP_FAILURE_POINT:\n"); \
1482 DEBUG_PRINT2 (" Before pop, next avail: %d\n", fail_stack.avail); \
1483 DEBUG_PRINT2 (" size: %d\n", fail_stack.size); \
1485 /* Pop the saved registers. */ \
1486 while (fail_stack.frame < fail_stack.avail) \
1487 POP_FAILURE_REG (); \
1489 pat = (unsigned char *) POP_FAILURE_POINTER (); \
1490 DEBUG_PRINT2 (" Popping pattern %p: ", pat); \
1491 DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend); \
1493 /* If the saved string location is NULL, it came from an \
1494 on_failure_keep_string_jump opcode, and we want to throw away the \
1495 saved NULL, thus retaining our current position in the string. */ \
1496 str = (re_char *) POP_FAILURE_POINTER (); \
1497 DEBUG_PRINT2 (" Popping string %p: `", str); \
1498 DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2); \
1499 DEBUG_PRINT1 ("'\n"); \
1501 fail_stack.frame = POP_FAILURE_INT (); \
1502 DEBUG_PRINT2 (" Popping frame index: %d\n", fail_stack.frame); \
1504 assert (fail_stack.avail >= 0); \
1505 assert (fail_stack.frame <= fail_stack.avail); \
1507 DEBUG_STATEMENT (nfailure_points_popped++); \
1508 } while (0) /* POP_FAILURE_POINT */
1512 /* Registers are set to a sentinel when they haven't yet matched. */
1513 #define REG_UNSET_VALUE NULL
1514 #define REG_UNSET(e) ((e) == REG_UNSET_VALUE)
1516 /* Subroutine declarations and macros for regex_compile. */
1518 static void store_op1 (), store_op2 ();
1519 static void insert_op1 (), insert_op2 ();
1520 static boolean at_begline_loc_p (), at_endline_loc_p ();
1521 static boolean group_in_compile_stack ();
1523 /* Fetch the next character in the uncompiled pattern---translating it
1524 if necessary. Also cast from a signed character in the constant
1525 string passed to us by the user to an unsigned char that we can use
1526 as an array index (in, e.g., `translate'). */
1527 #ifndef PATFETCH
1528 #define PATFETCH(c) \
1529 do {if (p == pend) return REG_EEND; \
1530 c = *p++; \
1531 if (RE_TRANSLATE_P (translate)) c = RE_TRANSLATE (translate, c); \
1532 } while (0)
1533 #endif
1535 /* Fetch the next character in the uncompiled pattern, with no
1536 translation. */
1537 #define PATFETCH_RAW(c) \
1538 do {if (p == pend) return REG_EEND; \
1539 c = *p++; \
1540 } while (0)
1542 /* Go backwards one character in the pattern. */
1543 #define PATUNFETCH p--
1546 /* If `translate' is non-null, return translate[D], else just D. We
1547 cast the subscript to translate because some data is declared as
1548 `char *', to avoid warnings when a string constant is passed. But
1549 when we use a character as a subscript we must make it unsigned. */
1550 #ifndef TRANSLATE
1551 #define TRANSLATE(d) \
1552 (RE_TRANSLATE_P (translate) ? RE_TRANSLATE (translate, (d)) : (d))
1553 #endif
1556 /* Macros for outputting the compiled pattern into `buffer'. */
1558 /* If the buffer isn't allocated when it comes in, use this. */
1559 #define INIT_BUF_SIZE 32
1561 /* Make sure we have at least N more bytes of space in buffer. */
1562 #define GET_BUFFER_SPACE(n) \
1563 while (b - bufp->buffer + (n) > bufp->allocated) \
1564 EXTEND_BUFFER ()
1566 /* Make sure we have one more byte of buffer space and then add C to it. */
1567 #define BUF_PUSH(c) \
1568 do { \
1569 GET_BUFFER_SPACE (1); \
1570 *b++ = (unsigned char) (c); \
1571 } while (0)
1574 /* Ensure we have two more bytes of buffer space and then append C1 and C2. */
1575 #define BUF_PUSH_2(c1, c2) \
1576 do { \
1577 GET_BUFFER_SPACE (2); \
1578 *b++ = (unsigned char) (c1); \
1579 *b++ = (unsigned char) (c2); \
1580 } while (0)
1583 /* As with BUF_PUSH_2, except for three bytes. */
1584 #define BUF_PUSH_3(c1, c2, c3) \
1585 do { \
1586 GET_BUFFER_SPACE (3); \
1587 *b++ = (unsigned char) (c1); \
1588 *b++ = (unsigned char) (c2); \
1589 *b++ = (unsigned char) (c3); \
1590 } while (0)
1593 /* Store a jump with opcode OP at LOC to location TO. We store a
1594 relative address offset by the three bytes the jump itself occupies. */
1595 #define STORE_JUMP(op, loc, to) \
1596 store_op1 (op, loc, (to) - (loc) - 3)
1598 /* Likewise, for a two-argument jump. */
1599 #define STORE_JUMP2(op, loc, to, arg) \
1600 store_op2 (op, loc, (to) - (loc) - 3, arg)
1602 /* Like `STORE_JUMP', but for inserting. Assume `b' is the buffer end. */
1603 #define INSERT_JUMP(op, loc, to) \
1604 insert_op1 (op, loc, (to) - (loc) - 3, b)
1606 /* Like `STORE_JUMP2', but for inserting. Assume `b' is the buffer end. */
1607 #define INSERT_JUMP2(op, loc, to, arg) \
1608 insert_op2 (op, loc, (to) - (loc) - 3, arg, b)
1611 /* This is not an arbitrary limit: the arguments which represent offsets
1612 into the pattern are two bytes long. So if 2^16 bytes turns out to
1613 be too small, many things would have to change. */
1614 #define MAX_BUF_SIZE (1L << 16)
1617 /* Extend the buffer by twice its current size via realloc and
1618 reset the pointers that pointed into the old block to point to the
1619 correct places in the new one. If extending the buffer results in it
1620 being larger than MAX_BUF_SIZE, then flag memory exhausted. */
1621 #define EXTEND_BUFFER() \
1622 do { \
1623 unsigned char *old_buffer = bufp->buffer; \
1624 if (bufp->allocated == MAX_BUF_SIZE) \
1625 return REG_ESIZE; \
1626 bufp->allocated <<= 1; \
1627 if (bufp->allocated > MAX_BUF_SIZE) \
1628 bufp->allocated = MAX_BUF_SIZE; \
1629 bufp->buffer = (unsigned char *) realloc (bufp->buffer, bufp->allocated);\
1630 if (bufp->buffer == NULL) \
1631 return REG_ESPACE; \
1632 /* If the buffer moved, move all the pointers into it. */ \
1633 if (old_buffer != bufp->buffer) \
1635 b = (b - old_buffer) + bufp->buffer; \
1636 begalt = (begalt - old_buffer) + bufp->buffer; \
1637 if (fixup_alt_jump) \
1638 fixup_alt_jump = (fixup_alt_jump - old_buffer) + bufp->buffer;\
1639 if (laststart) \
1640 laststart = (laststart - old_buffer) + bufp->buffer; \
1641 if (pending_exact) \
1642 pending_exact = (pending_exact - old_buffer) + bufp->buffer; \
1644 } while (0)
1647 /* Since we have one byte reserved for the register number argument to
1648 {start,stop}_memory, the maximum number of groups we can report
1649 things about is what fits in that byte. */
1650 #define MAX_REGNUM 255
1652 /* But patterns can have more than `MAX_REGNUM' registers. We just
1653 ignore the excess. */
1654 typedef unsigned regnum_t;
1657 /* Macros for the compile stack. */
1659 /* Since offsets can go either forwards or backwards, this type needs to
1660 be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1. */
1661 typedef int pattern_offset_t;
1663 typedef struct
1665 pattern_offset_t begalt_offset;
1666 pattern_offset_t fixup_alt_jump;
1667 pattern_offset_t laststart_offset;
1668 regnum_t regnum;
1669 } compile_stack_elt_t;
1672 typedef struct
1674 compile_stack_elt_t *stack;
1675 unsigned size;
1676 unsigned avail; /* Offset of next open position. */
1677 } compile_stack_type;
1680 #define INIT_COMPILE_STACK_SIZE 32
1682 #define COMPILE_STACK_EMPTY (compile_stack.avail == 0)
1683 #define COMPILE_STACK_FULL (compile_stack.avail == compile_stack.size)
1685 /* The next available element. */
1686 #define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
1689 /* Structure to manage work area for range table. */
1690 struct range_table_work_area
1692 int *table; /* actual work area. */
1693 int allocated; /* allocated size for work area in bytes. */
1694 int used; /* actually used size in words. */
1695 int bits; /* flag to record character classes */
1698 /* Make sure that WORK_AREA can hold more N multibyte characters. */
1699 #define EXTEND_RANGE_TABLE_WORK_AREA(work_area, n) \
1700 do { \
1701 if (((work_area).used + (n)) * sizeof (int) > (work_area).allocated) \
1703 (work_area).allocated += 16 * sizeof (int); \
1704 if ((work_area).table) \
1705 (work_area).table \
1706 = (int *) realloc ((work_area).table, (work_area).allocated); \
1707 else \
1708 (work_area).table \
1709 = (int *) malloc ((work_area).allocated); \
1710 if ((work_area).table == 0) \
1711 FREE_STACK_RETURN (REG_ESPACE); \
1713 } while (0)
1715 #define SET_RANGE_TABLE_WORK_AREA_BIT(work_area, bit) \
1716 (work_area).bits |= (bit)
1718 /* These bits represent the various character classes such as [:alnum:]
1719 in a charset's range table. */
1720 #define BIT_ALNUM 0x1
1721 #define BIT_ALPHA 0x2
1722 #define BIT_WORD 0x4
1723 #define BIT_ASCII 0x8
1724 #define BIT_NONASCII 0x10
1725 #define BIT_GRAPH 0x20
1726 #define BIT_LOWER 0x40
1727 #define BIT_PRINT 0x80
1728 #define BIT_PUNCT 0x100
1729 #define BIT_SPACE 0x200
1730 #define BIT_UPPER 0x400
1731 #define BIT_UNIBYTE 0x800
1732 #define BIT_MULTIBYTE 0x1000
1734 /* Set a range (RANGE_START, RANGE_END) to WORK_AREA. */
1735 #define SET_RANGE_TABLE_WORK_AREA(work_area, range_start, range_end) \
1736 do { \
1737 EXTEND_RANGE_TABLE_WORK_AREA ((work_area), 2); \
1738 (work_area).table[(work_area).used++] = (range_start); \
1739 (work_area).table[(work_area).used++] = (range_end); \
1740 } while (0)
1742 /* Free allocated memory for WORK_AREA. */
1743 #define FREE_RANGE_TABLE_WORK_AREA(work_area) \
1744 do { \
1745 if ((work_area).table) \
1746 free ((work_area).table); \
1747 } while (0)
1749 #define CLEAR_RANGE_TABLE_WORK_USED(work_area) ((work_area).used = 0, (work_area).bits = 0)
1750 #define RANGE_TABLE_WORK_USED(work_area) ((work_area).used)
1751 #define RANGE_TABLE_WORK_BITS(work_area) ((work_area).bits)
1752 #define RANGE_TABLE_WORK_ELT(work_area, i) ((work_area).table[i])
1755 /* Set the bit for character C in a list. */
1756 #define SET_LIST_BIT(c) \
1757 (b[((unsigned char) (c)) / BYTEWIDTH] \
1758 |= 1 << (((unsigned char) c) % BYTEWIDTH))
1761 /* Get the next unsigned number in the uncompiled pattern. */
1762 #define GET_UNSIGNED_NUMBER(num) \
1763 { if (p != pend) \
1765 PATFETCH (c); \
1766 while (ISDIGIT (c)) \
1768 if (num < 0) \
1769 num = 0; \
1770 num = num * 10 + c - '0'; \
1771 if (p == pend) \
1772 break; \
1773 PATFETCH (c); \
1778 #define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
1780 #define IS_CHAR_CLASS(string) \
1781 (STREQ (string, "alpha") || STREQ (string, "upper") \
1782 || STREQ (string, "lower") || STREQ (string, "digit") \
1783 || STREQ (string, "alnum") || STREQ (string, "xdigit") \
1784 || STREQ (string, "space") || STREQ (string, "print") \
1785 || STREQ (string, "punct") || STREQ (string, "graph") \
1786 || STREQ (string, "cntrl") || STREQ (string, "blank") \
1787 || STREQ (string, "word") \
1788 || STREQ (string, "ascii") || STREQ (string, "nonascii") \
1789 || STREQ (string, "unibyte") || STREQ (string, "multibyte"))
1791 #ifndef MATCH_MAY_ALLOCATE
1793 /* If we cannot allocate large objects within re_match_2_internal,
1794 we make the fail stack and register vectors global.
1795 The fail stack, we grow to the maximum size when a regexp
1796 is compiled.
1797 The register vectors, we adjust in size each time we
1798 compile a regexp, according to the number of registers it needs. */
1800 static fail_stack_type fail_stack;
1802 /* Size with which the following vectors are currently allocated.
1803 That is so we can make them bigger as needed,
1804 but never make them smaller. */
1805 static int regs_allocated_size;
1807 static re_char ** regstart, ** regend;
1808 static re_char **best_regstart, **best_regend;
1810 /* Make the register vectors big enough for NUM_REGS registers,
1811 but don't make them smaller. */
1813 static
1814 regex_grow_registers (num_regs)
1815 int num_regs;
1817 if (num_regs > regs_allocated_size)
1819 RETALLOC_IF (regstart, num_regs, re_char *);
1820 RETALLOC_IF (regend, num_regs, re_char *);
1821 RETALLOC_IF (best_regstart, num_regs, re_char *);
1822 RETALLOC_IF (best_regend, num_regs, re_char *);
1824 regs_allocated_size = num_regs;
1828 #endif /* not MATCH_MAY_ALLOCATE */
1830 /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
1831 Returns one of error codes defined in `regex.h', or zero for success.
1833 Assumes the `allocated' (and perhaps `buffer') and `translate'
1834 fields are set in BUFP on entry.
1836 If it succeeds, results are put in BUFP (if it returns an error, the
1837 contents of BUFP are undefined):
1838 `buffer' is the compiled pattern;
1839 `syntax' is set to SYNTAX;
1840 `used' is set to the length of the compiled pattern;
1841 `fastmap_accurate' is zero;
1842 `re_nsub' is the number of subexpressions in PATTERN;
1843 `not_bol' and `not_eol' are zero;
1845 The `fastmap' and `newline_anchor' fields are neither
1846 examined nor set. */
1848 /* Insert the `jump' from the end of last alternative to "here".
1849 The space for the jump has already been allocated. */
1850 #define FIXUP_ALT_JUMP() \
1851 do { \
1852 if (fixup_alt_jump) \
1853 STORE_JUMP (jump, fixup_alt_jump, b); \
1854 } while (0)
1857 /* Return, freeing storage we allocated. */
1858 #define FREE_STACK_RETURN(value) \
1859 do { \
1860 FREE_RANGE_TABLE_WORK_AREA (range_table_work); \
1861 free (compile_stack.stack); \
1862 return value; \
1863 } while (0)
1865 static reg_errcode_t
1866 regex_compile (pattern, size, syntax, bufp)
1867 re_char *pattern;
1868 int size;
1869 reg_syntax_t syntax;
1870 struct re_pattern_buffer *bufp;
1872 /* We fetch characters from PATTERN here. Even though PATTERN is
1873 `char *' (i.e., signed), we declare these variables as unsigned, so
1874 they can be reliably used as array indices. */
1875 register unsigned int c, c1;
1877 /* A random temporary spot in PATTERN. */
1878 re_char *p1;
1880 /* Points to the end of the buffer, where we should append. */
1881 register unsigned char *b;
1883 /* Keeps track of unclosed groups. */
1884 compile_stack_type compile_stack;
1886 /* Points to the current (ending) position in the pattern. */
1887 #ifdef AIX
1888 /* `const' makes AIX compiler fail. */
1889 unsigned char *p = pattern;
1890 #else
1891 re_char *p = pattern;
1892 #endif
1893 re_char *pend = pattern + size;
1895 /* How to translate the characters in the pattern. */
1896 RE_TRANSLATE_TYPE translate = bufp->translate;
1898 /* Address of the count-byte of the most recently inserted `exactn'
1899 command. This makes it possible to tell if a new exact-match
1900 character can be added to that command or if the character requires
1901 a new `exactn' command. */
1902 unsigned char *pending_exact = 0;
1904 /* Address of start of the most recently finished expression.
1905 This tells, e.g., postfix * where to find the start of its
1906 operand. Reset at the beginning of groups and alternatives. */
1907 unsigned char *laststart = 0;
1909 /* Address of beginning of regexp, or inside of last group. */
1910 unsigned char *begalt;
1912 /* Place in the uncompiled pattern (i.e., the {) to
1913 which to go back if the interval is invalid. */
1914 re_char *beg_interval;
1916 /* Address of the place where a forward jump should go to the end of
1917 the containing expression. Each alternative of an `or' -- except the
1918 last -- ends with a forward jump of this sort. */
1919 unsigned char *fixup_alt_jump = 0;
1921 /* Counts open-groups as they are encountered. Remembered for the
1922 matching close-group on the compile stack, so the same register
1923 number is put in the stop_memory as the start_memory. */
1924 regnum_t regnum = 0;
1926 /* Work area for range table of charset. */
1927 struct range_table_work_area range_table_work;
1929 #ifdef DEBUG
1930 /* debug = 1; */
1931 DEBUG_PRINT1 ("\nCompiling pattern: ");
1932 if (debug)
1934 unsigned debug_count;
1936 for (debug_count = 0; debug_count < size; debug_count++)
1937 putchar (pattern[debug_count]);
1938 putchar ('\n');
1940 #endif /* DEBUG */
1942 /* Initialize the compile stack. */
1943 compile_stack.stack = TALLOC (INIT_COMPILE_STACK_SIZE, compile_stack_elt_t);
1944 if (compile_stack.stack == NULL)
1945 return REG_ESPACE;
1947 compile_stack.size = INIT_COMPILE_STACK_SIZE;
1948 compile_stack.avail = 0;
1950 range_table_work.table = 0;
1951 range_table_work.allocated = 0;
1953 /* Initialize the pattern buffer. */
1954 bufp->syntax = syntax;
1955 bufp->fastmap_accurate = 0;
1956 bufp->not_bol = bufp->not_eol = 0;
1958 /* Set `used' to zero, so that if we return an error, the pattern
1959 printer (for debugging) will think there's no pattern. We reset it
1960 at the end. */
1961 bufp->used = 0;
1963 /* Always count groups, whether or not bufp->no_sub is set. */
1964 bufp->re_nsub = 0;
1966 #ifdef emacs
1967 /* bufp->multibyte is set before regex_compile is called, so don't alter
1968 it. */
1969 #else /* not emacs */
1970 /* Nothing is recognized as a multibyte character. */
1971 bufp->multibyte = 0;
1972 #endif
1974 #if !defined (emacs) && !defined (SYNTAX_TABLE)
1975 /* Initialize the syntax table. */
1976 init_syntax_once ();
1977 #endif
1979 if (bufp->allocated == 0)
1981 if (bufp->buffer)
1982 { /* If zero allocated, but buffer is non-null, try to realloc
1983 enough space. This loses if buffer's address is bogus, but
1984 that is the user's responsibility. */
1985 RETALLOC (bufp->buffer, INIT_BUF_SIZE, unsigned char);
1987 else
1988 { /* Caller did not allocate a buffer. Do it for them. */
1989 bufp->buffer = TALLOC (INIT_BUF_SIZE, unsigned char);
1991 if (!bufp->buffer) FREE_STACK_RETURN (REG_ESPACE);
1993 bufp->allocated = INIT_BUF_SIZE;
1996 begalt = b = bufp->buffer;
1998 /* Loop through the uncompiled pattern until we're at the end. */
1999 while (p != pend)
2001 PATFETCH (c);
2003 switch (c)
2005 case '^':
2007 if ( /* If at start of pattern, it's an operator. */
2008 p == pattern + 1
2009 /* If context independent, it's an operator. */
2010 || syntax & RE_CONTEXT_INDEP_ANCHORS
2011 /* Otherwise, depends on what's come before. */
2012 || at_begline_loc_p (pattern, p, syntax))
2013 BUF_PUSH (begline);
2014 else
2015 goto normal_char;
2017 break;
2020 case '$':
2022 if ( /* If at end of pattern, it's an operator. */
2023 p == pend
2024 /* If context independent, it's an operator. */
2025 || syntax & RE_CONTEXT_INDEP_ANCHORS
2026 /* Otherwise, depends on what's next. */
2027 || at_endline_loc_p (p, pend, syntax))
2028 BUF_PUSH (endline);
2029 else
2030 goto normal_char;
2032 break;
2035 case '+':
2036 case '?':
2037 if ((syntax & RE_BK_PLUS_QM)
2038 || (syntax & RE_LIMITED_OPS))
2039 goto normal_char;
2040 handle_plus:
2041 case '*':
2042 /* If there is no previous pattern... */
2043 if (!laststart)
2045 if (syntax & RE_CONTEXT_INVALID_OPS)
2046 FREE_STACK_RETURN (REG_BADRPT);
2047 else if (!(syntax & RE_CONTEXT_INDEP_OPS))
2048 goto normal_char;
2052 /* Are we optimizing this jump? */
2053 boolean keep_string_p = false;
2055 /* 1 means zero (many) matches is allowed. */
2056 boolean zero_times_ok = 0, many_times_ok = 0;
2057 boolean greedy = 1;
2059 /* If there is a sequence of repetition chars, collapse it
2060 down to just one (the right one). We can't combine
2061 interval operators with these because of, e.g., `a{2}*',
2062 which should only match an even number of `a's. */
2064 for (;;)
2066 if (!(syntax & RE_ALL_GREEDY)
2067 && c == '?' && (zero_times_ok || many_times_ok))
2068 greedy = 0;
2069 else
2071 zero_times_ok |= c != '+';
2072 many_times_ok |= c != '?';
2075 if (p == pend)
2076 break;
2078 PATFETCH (c);
2080 if (c == '*'
2081 || (!(syntax & RE_BK_PLUS_QM) && (c == '+' || c == '?')))
2084 else if (syntax & RE_BK_PLUS_QM && c == '\\')
2086 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
2088 PATFETCH (c1);
2089 if (!(c1 == '+' || c1 == '?'))
2091 PATUNFETCH;
2092 PATUNFETCH;
2093 break;
2096 c = c1;
2098 else
2100 PATUNFETCH;
2101 break;
2104 /* If we get here, we found another repeat character. */
2107 /* Star, etc. applied to an empty pattern is equivalent
2108 to an empty pattern. */
2109 if (!laststart)
2110 break;
2112 /* Now we know whether or not zero matches is allowed
2113 and also whether or not two or more matches is allowed. */
2114 if (greedy)
2116 if (many_times_ok)
2117 { /* More than one repetition is allowed, so put in at the
2118 end a backward relative jump from `b' to before the next
2119 jump we're going to put in below (which jumps from
2120 laststart to after this jump).
2122 But if we are at the `*' in the exact sequence `.*\n',
2123 insert an unconditional jump backwards to the .,
2124 instead of the beginning of the loop. This way we only
2125 push a failure point once, instead of every time
2126 through the loop. */
2127 assert (p - 1 > pattern);
2129 /* Allocate the space for the jump. */
2130 GET_BUFFER_SPACE (3);
2132 /* We know we are not at the first character of the pattern,
2133 because laststart was nonzero. And we've already
2134 incremented `p', by the way, to be the character after
2135 the `*'. Do we have to do something analogous here
2136 for null bytes, because of RE_DOT_NOT_NULL? */
2137 if (TRANSLATE (*(p - 2)) == TRANSLATE ('.')
2138 && zero_times_ok
2139 && p < pend
2140 && TRANSLATE (*p) == TRANSLATE ('\n')
2141 && !(syntax & RE_DOT_NEWLINE))
2142 { /* We have .*\n. */
2143 STORE_JUMP (jump, b, laststart);
2144 keep_string_p = true;
2146 else
2147 STORE_JUMP (jump, b, laststart - 3);
2149 /* We've added more stuff to the buffer. */
2150 b += 3;
2153 /* On failure, jump from laststart to b + 3, which will be the
2154 end of the buffer after this jump is inserted. */
2155 GET_BUFFER_SPACE (3);
2156 if (!zero_times_ok)
2158 assert (many_times_ok);
2159 INSERT_JUMP (on_failure_jump_smart, b - 3, b + 3);
2160 pending_exact = 0;
2161 b += 3;
2163 else
2165 INSERT_JUMP (keep_string_p ? on_failure_keep_string_jump
2166 : !many_times_ok ?
2167 on_failure_jump : on_failure_jump_smart,
2168 laststart, b + 3);
2169 pending_exact = 0;
2170 b += 3;
2173 else /* not greedy */
2174 { /* I wish the greedy and non-greedy cases could be merged. */
2176 if (many_times_ok)
2178 /* The non-greedy multiple match looks like a repeat..until:
2179 we only need a conditional jump at the end of the loop */
2180 GET_BUFFER_SPACE (3);
2181 STORE_JUMP (on_failure_jump, b, laststart);
2182 b += 3;
2183 if (zero_times_ok)
2185 /* The repeat...until naturally matches one or more.
2186 To also match zero times, we need to first jump to
2187 the end of the loop (its conditional jump). */
2188 GET_BUFFER_SPACE (3);
2189 INSERT_JUMP (jump, laststart, b);
2190 b += 3;
2193 else
2195 /* non-greedy a?? */
2196 GET_BUFFER_SPACE (6);
2197 INSERT_JUMP (jump, laststart, b + 3);
2198 b += 3;
2199 INSERT_JUMP (on_failure_jump, laststart, laststart + 6);
2200 b += 3;
2204 break;
2207 case '.':
2208 laststart = b;
2209 BUF_PUSH (anychar);
2210 break;
2213 case '[':
2215 CLEAR_RANGE_TABLE_WORK_USED (range_table_work);
2217 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2219 /* Ensure that we have enough space to push a charset: the
2220 opcode, the length count, and the bitset; 34 bytes in all. */
2221 GET_BUFFER_SPACE (34);
2223 laststart = b;
2225 /* We test `*p == '^' twice, instead of using an if
2226 statement, so we only need one BUF_PUSH. */
2227 BUF_PUSH (*p == '^' ? charset_not : charset);
2228 if (*p == '^')
2229 p++;
2231 /* Remember the first position in the bracket expression. */
2232 p1 = p;
2234 /* Push the number of bytes in the bitmap. */
2235 BUF_PUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
2237 /* Clear the whole map. */
2238 bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH);
2240 /* charset_not matches newline according to a syntax bit. */
2241 if ((re_opcode_t) b[-2] == charset_not
2242 && (syntax & RE_HAT_LISTS_NOT_NEWLINE))
2243 SET_LIST_BIT ('\n');
2245 /* Read in characters and ranges, setting map bits. */
2246 for (;;)
2248 int len;
2249 boolean escaped_char = false;
2251 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2253 PATFETCH (c);
2255 /* \ might escape characters inside [...] and [^...]. */
2256 if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) && c == '\\')
2258 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
2260 PATFETCH (c);
2261 escaped_char = true;
2263 else
2265 /* Could be the end of the bracket expression. If it's
2266 not (i.e., when the bracket expression is `[]' so
2267 far), the ']' character bit gets set way below. */
2268 if (c == ']' && p != p1 + 1)
2269 break;
2272 /* If C indicates start of multibyte char, get the
2273 actual character code in C, and set the pattern
2274 pointer P to the next character boundary. */
2275 if (bufp->multibyte && BASE_LEADING_CODE_P (c))
2277 PATUNFETCH;
2278 c = STRING_CHAR_AND_LENGTH (p, pend - p, len);
2279 p += len;
2281 /* What should we do for the character which is
2282 greater than 0x7F, but not BASE_LEADING_CODE_P?
2283 XXX */
2285 /* See if we're at the beginning of a possible character
2286 class. */
2288 else if (!escaped_char &&
2289 syntax & RE_CHAR_CLASSES && c == '[' && *p == ':')
2291 /* Leave room for the null. */
2292 char str[CHAR_CLASS_MAX_LENGTH + 1];
2294 PATFETCH (c);
2295 c1 = 0;
2297 /* If pattern is `[[:'. */
2298 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2300 for (;;)
2302 PATFETCH (c);
2303 if (c == ':' || c == ']' || p == pend
2304 || c1 == CHAR_CLASS_MAX_LENGTH)
2305 break;
2306 str[c1++] = c;
2308 str[c1] = '\0';
2310 /* If isn't a word bracketed by `[:' and `:]':
2311 undo the ending character, the letters, and
2312 leave the leading `:' and `[' (but set bits for
2313 them). */
2314 if (c == ':' && *p == ']')
2316 int ch;
2317 boolean is_alnum = STREQ (str, "alnum");
2318 boolean is_alpha = STREQ (str, "alpha");
2319 boolean is_ascii = STREQ (str, "ascii");
2320 boolean is_blank = STREQ (str, "blank");
2321 boolean is_cntrl = STREQ (str, "cntrl");
2322 boolean is_digit = STREQ (str, "digit");
2323 boolean is_graph = STREQ (str, "graph");
2324 boolean is_lower = STREQ (str, "lower");
2325 boolean is_multibyte = STREQ (str, "multibyte");
2326 boolean is_nonascii = STREQ (str, "nonascii");
2327 boolean is_print = STREQ (str, "print");
2328 boolean is_punct = STREQ (str, "punct");
2329 boolean is_space = STREQ (str, "space");
2330 boolean is_unibyte = STREQ (str, "unibyte");
2331 boolean is_upper = STREQ (str, "upper");
2332 boolean is_word = STREQ (str, "word");
2333 boolean is_xdigit = STREQ (str, "xdigit");
2335 if (!IS_CHAR_CLASS (str))
2336 FREE_STACK_RETURN (REG_ECTYPE);
2338 /* Throw away the ] at the end of the character
2339 class. */
2340 PATFETCH (c);
2342 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2344 /* Most character classes in a multibyte match
2345 just set a flag. Exceptions are is_blank,
2346 is_digit, is_cntrl, and is_xdigit, since
2347 they can only match ASCII characters. We
2348 don't need to handle them for multibyte. */
2350 if (bufp->multibyte)
2352 int bit = 0;
2354 if (is_alnum) bit = BIT_ALNUM;
2355 if (is_alpha) bit = BIT_ALPHA;
2356 if (is_ascii) bit = BIT_ASCII;
2357 if (is_graph) bit = BIT_GRAPH;
2358 if (is_lower) bit = BIT_LOWER;
2359 if (is_multibyte) bit = BIT_MULTIBYTE;
2360 if (is_nonascii) bit = BIT_NONASCII;
2361 if (is_print) bit = BIT_PRINT;
2362 if (is_punct) bit = BIT_PUNCT;
2363 if (is_space) bit = BIT_SPACE;
2364 if (is_unibyte) bit = BIT_UNIBYTE;
2365 if (is_upper) bit = BIT_UPPER;
2366 if (is_word) bit = BIT_WORD;
2367 if (bit)
2368 SET_RANGE_TABLE_WORK_AREA_BIT (range_table_work,
2369 bit);
2372 /* Handle character classes for ASCII characters. */
2373 for (ch = 0; ch < 1 << BYTEWIDTH; ch++)
2375 int translated = TRANSLATE (ch);
2376 /* This was split into 3 if's to
2377 avoid an arbitrary limit in some compiler. */
2378 if ( (is_alnum && ISALNUM (ch))
2379 || (is_alpha && ISALPHA (ch))
2380 || (is_blank && ISBLANK (ch))
2381 || (is_cntrl && ISCNTRL (ch)))
2382 SET_LIST_BIT (translated);
2383 if ( (is_digit && ISDIGIT (ch))
2384 || (is_graph && ISGRAPH (ch))
2385 || (is_lower && ISLOWER (ch))
2386 || (is_print && ISPRINT (ch)))
2387 SET_LIST_BIT (translated);
2388 if ( (is_punct && ISPUNCT (ch))
2389 || (is_space && ISSPACE (ch))
2390 || (is_upper && ISUPPER (ch))
2391 || (is_xdigit && ISXDIGIT (ch)))
2392 SET_LIST_BIT (translated);
2393 if ( (is_ascii && IS_REAL_ASCII (ch))
2394 || (is_nonascii && !IS_REAL_ASCII (ch))
2395 || (is_unibyte && ISUNIBYTE (ch))
2396 || (is_multibyte && !ISUNIBYTE (ch)))
2397 SET_LIST_BIT (translated);
2399 if ( (is_word && ISWORD (ch)))
2400 SET_LIST_BIT (translated);
2403 /* Repeat the loop. */
2404 continue;
2406 else
2408 c1++;
2409 while (c1--)
2410 PATUNFETCH;
2411 SET_LIST_BIT ('[');
2413 /* Because the `:' may starts the range, we
2414 can't simply set bit and repeat the loop.
2415 Instead, just set it to C and handle below. */
2416 c = ':';
2420 if (p < pend && p[0] == '-' && p[1] != ']')
2423 /* Discard the `-'. */
2424 PATFETCH (c1);
2426 /* Fetch the character which ends the range. */
2427 PATFETCH (c1);
2428 if (bufp->multibyte && BASE_LEADING_CODE_P (c1))
2430 PATUNFETCH;
2431 c1 = STRING_CHAR_AND_LENGTH (p, pend - p, len);
2432 p += len;
2435 if (SINGLE_BYTE_CHAR_P (c)
2436 && ! SINGLE_BYTE_CHAR_P (c1))
2438 /* Handle a range such as \177-\377 in multibyte mode.
2439 Split that into two ranges,,
2440 the low one ending at 0237, and the high one
2441 starting at ...040. */
2442 /* Unless I'm missing something,
2443 this line is useless. -sm
2444 int c1_base = (c1 & ~0177) | 040; */
2445 SET_RANGE_TABLE_WORK_AREA (range_table_work, c, c1);
2446 c1 = 0237;
2448 else if (!SAME_CHARSET_P (c, c1))
2449 FREE_STACK_RETURN (REG_ERANGE);
2451 else
2452 /* Range from C to C. */
2453 c1 = c;
2455 /* Set the range ... */
2456 if (SINGLE_BYTE_CHAR_P (c))
2457 /* ... into bitmap. */
2459 unsigned this_char;
2460 int range_start = c, range_end = c1;
2462 /* If the start is after the end, the range is empty. */
2463 if (range_start > range_end)
2465 if (syntax & RE_NO_EMPTY_RANGES)
2466 FREE_STACK_RETURN (REG_ERANGE);
2467 /* Else, repeat the loop. */
2469 else
2471 for (this_char = range_start; this_char <= range_end;
2472 this_char++)
2473 SET_LIST_BIT (TRANSLATE (this_char));
2476 else
2477 /* ... into range table. */
2478 SET_RANGE_TABLE_WORK_AREA (range_table_work, c, c1);
2481 /* Discard any (non)matching list bytes that are all 0 at the
2482 end of the map. Decrease the map-length byte too. */
2483 while ((int) b[-1] > 0 && b[b[-1] - 1] == 0)
2484 b[-1]--;
2485 b += b[-1];
2487 /* Build real range table from work area. */
2488 if (RANGE_TABLE_WORK_USED (range_table_work)
2489 || RANGE_TABLE_WORK_BITS (range_table_work))
2491 int i;
2492 int used = RANGE_TABLE_WORK_USED (range_table_work);
2494 /* Allocate space for COUNT + RANGE_TABLE. Needs two
2495 bytes for flags, two for COUNT, and three bytes for
2496 each character. */
2497 GET_BUFFER_SPACE (4 + used * 3);
2499 /* Indicate the existence of range table. */
2500 laststart[1] |= 0x80;
2502 /* Store the character class flag bits into the range table.
2503 If not in emacs, these flag bits are always 0. */
2504 *b++ = RANGE_TABLE_WORK_BITS (range_table_work) & 0xff;
2505 *b++ = RANGE_TABLE_WORK_BITS (range_table_work) >> 8;
2507 STORE_NUMBER_AND_INCR (b, used / 2);
2508 for (i = 0; i < used; i++)
2509 STORE_CHARACTER_AND_INCR
2510 (b, RANGE_TABLE_WORK_ELT (range_table_work, i));
2513 break;
2516 case '(':
2517 if (syntax & RE_NO_BK_PARENS)
2518 goto handle_open;
2519 else
2520 goto normal_char;
2523 case ')':
2524 if (syntax & RE_NO_BK_PARENS)
2525 goto handle_close;
2526 else
2527 goto normal_char;
2530 case '\n':
2531 if (syntax & RE_NEWLINE_ALT)
2532 goto handle_alt;
2533 else
2534 goto normal_char;
2537 case '|':
2538 if (syntax & RE_NO_BK_VBAR)
2539 goto handle_alt;
2540 else
2541 goto normal_char;
2544 case '{':
2545 if (syntax & RE_INTERVALS && syntax & RE_NO_BK_BRACES)
2546 goto handle_interval;
2547 else
2548 goto normal_char;
2551 case '\\':
2552 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
2554 /* Do not translate the character after the \, so that we can
2555 distinguish, e.g., \B from \b, even if we normally would
2556 translate, e.g., B to b. */
2557 PATFETCH_RAW (c);
2559 switch (c)
2561 case '(':
2562 if (syntax & RE_NO_BK_PARENS)
2563 goto normal_backslash;
2565 handle_open:
2567 int shy = 0;
2568 if (p+1 < pend)
2570 /* Look for a special (?...) construct */
2571 PATFETCH (c);
2572 if ((syntax & RE_SHY_GROUPS) && c == '?')
2574 PATFETCH (c);
2575 switch (c)
2577 case ':': shy = 1; break;
2578 default:
2579 /* Only (?:...) is supported right now. */
2580 FREE_STACK_RETURN (REG_BADPAT);
2583 else PATUNFETCH;
2586 if (!shy)
2588 bufp->re_nsub++;
2589 regnum++;
2592 if (COMPILE_STACK_FULL)
2594 RETALLOC (compile_stack.stack, compile_stack.size << 1,
2595 compile_stack_elt_t);
2596 if (compile_stack.stack == NULL) return REG_ESPACE;
2598 compile_stack.size <<= 1;
2601 /* These are the values to restore when we hit end of this
2602 group. They are all relative offsets, so that if the
2603 whole pattern moves because of realloc, they will still
2604 be valid. */
2605 COMPILE_STACK_TOP.begalt_offset = begalt - bufp->buffer;
2606 COMPILE_STACK_TOP.fixup_alt_jump
2607 = fixup_alt_jump ? fixup_alt_jump - bufp->buffer + 1 : 0;
2608 COMPILE_STACK_TOP.laststart_offset = b - bufp->buffer;
2609 COMPILE_STACK_TOP.regnum = shy ? -regnum : regnum;
2611 /* Do not push a
2612 start_memory for groups beyond the last one we can
2613 represent in the compiled pattern. */
2614 if (regnum <= MAX_REGNUM && !shy)
2615 BUF_PUSH_2 (start_memory, regnum);
2617 compile_stack.avail++;
2619 fixup_alt_jump = 0;
2620 laststart = 0;
2621 begalt = b;
2622 /* If we've reached MAX_REGNUM groups, then this open
2623 won't actually generate any code, so we'll have to
2624 clear pending_exact explicitly. */
2625 pending_exact = 0;
2626 break;
2629 case ')':
2630 if (syntax & RE_NO_BK_PARENS) goto normal_backslash;
2632 if (COMPILE_STACK_EMPTY)
2634 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
2635 goto normal_backslash;
2636 else
2637 FREE_STACK_RETURN (REG_ERPAREN);
2640 handle_close:
2641 FIXUP_ALT_JUMP ();
2643 /* See similar code for backslashed left paren above. */
2644 if (COMPILE_STACK_EMPTY)
2646 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
2647 goto normal_char;
2648 else
2649 FREE_STACK_RETURN (REG_ERPAREN);
2652 /* Since we just checked for an empty stack above, this
2653 ``can't happen''. */
2654 assert (compile_stack.avail != 0);
2656 /* We don't just want to restore into `regnum', because
2657 later groups should continue to be numbered higher,
2658 as in `(ab)c(de)' -- the second group is #2. */
2659 regnum_t this_group_regnum;
2661 compile_stack.avail--;
2662 begalt = bufp->buffer + COMPILE_STACK_TOP.begalt_offset;
2663 fixup_alt_jump
2664 = COMPILE_STACK_TOP.fixup_alt_jump
2665 ? bufp->buffer + COMPILE_STACK_TOP.fixup_alt_jump - 1
2666 : 0;
2667 laststart = bufp->buffer + COMPILE_STACK_TOP.laststart_offset;
2668 this_group_regnum = COMPILE_STACK_TOP.regnum;
2669 /* If we've reached MAX_REGNUM groups, then this open
2670 won't actually generate any code, so we'll have to
2671 clear pending_exact explicitly. */
2672 pending_exact = 0;
2674 /* We're at the end of the group, so now we know how many
2675 groups were inside this one. */
2676 if (this_group_regnum <= MAX_REGNUM && this_group_regnum > 0)
2677 BUF_PUSH_2 (stop_memory, this_group_regnum);
2679 break;
2682 case '|': /* `\|'. */
2683 if (syntax & RE_LIMITED_OPS || syntax & RE_NO_BK_VBAR)
2684 goto normal_backslash;
2685 handle_alt:
2686 if (syntax & RE_LIMITED_OPS)
2687 goto normal_char;
2689 /* Insert before the previous alternative a jump which
2690 jumps to this alternative if the former fails. */
2691 GET_BUFFER_SPACE (3);
2692 INSERT_JUMP (on_failure_jump, begalt, b + 6);
2693 pending_exact = 0;
2694 b += 3;
2696 /* The alternative before this one has a jump after it
2697 which gets executed if it gets matched. Adjust that
2698 jump so it will jump to this alternative's analogous
2699 jump (put in below, which in turn will jump to the next
2700 (if any) alternative's such jump, etc.). The last such
2701 jump jumps to the correct final destination. A picture:
2702 _____ _____
2703 | | | |
2704 | v | v
2705 a | b | c
2707 If we are at `b', then fixup_alt_jump right now points to a
2708 three-byte space after `a'. We'll put in the jump, set
2709 fixup_alt_jump to right after `b', and leave behind three
2710 bytes which we'll fill in when we get to after `c'. */
2712 FIXUP_ALT_JUMP ();
2714 /* Mark and leave space for a jump after this alternative,
2715 to be filled in later either by next alternative or
2716 when know we're at the end of a series of alternatives. */
2717 fixup_alt_jump = b;
2718 GET_BUFFER_SPACE (3);
2719 b += 3;
2721 laststart = 0;
2722 begalt = b;
2723 break;
2726 case '{':
2727 /* If \{ is a literal. */
2728 if (!(syntax & RE_INTERVALS)
2729 /* If we're at `\{' and it's not the open-interval
2730 operator. */
2731 || ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
2732 || (p - 2 == pattern && p == pend))
2733 goto normal_backslash;
2735 handle_interval:
2737 /* If got here, then the syntax allows intervals. */
2739 /* At least (most) this many matches must be made. */
2740 int lower_bound = -1, upper_bound = -1;
2742 beg_interval = p - 1;
2744 if (p == pend)
2746 if (syntax & RE_NO_BK_BRACES)
2747 goto unfetch_interval;
2748 else
2749 FREE_STACK_RETURN (REG_EBRACE);
2752 GET_UNSIGNED_NUMBER (lower_bound);
2754 if (c == ',')
2756 GET_UNSIGNED_NUMBER (upper_bound);
2757 if (upper_bound < 0) upper_bound = RE_DUP_MAX;
2759 else
2760 /* Interval such as `{1}' => match exactly once. */
2761 upper_bound = lower_bound;
2763 if (lower_bound < 0 || upper_bound > RE_DUP_MAX
2764 || lower_bound > upper_bound)
2766 if (syntax & RE_NO_BK_BRACES)
2767 goto unfetch_interval;
2768 else
2769 FREE_STACK_RETURN (REG_BADBR);
2772 if (!(syntax & RE_NO_BK_BRACES))
2774 if (c != '\\') FREE_STACK_RETURN (REG_EBRACE);
2776 PATFETCH (c);
2779 if (c != '}')
2781 if (syntax & RE_NO_BK_BRACES)
2782 goto unfetch_interval;
2783 else
2784 FREE_STACK_RETURN (REG_BADBR);
2787 /* We just parsed a valid interval. */
2789 /* If it's invalid to have no preceding re. */
2790 if (!laststart)
2792 if (syntax & RE_CONTEXT_INVALID_OPS)
2793 FREE_STACK_RETURN (REG_BADRPT);
2794 else if (syntax & RE_CONTEXT_INDEP_OPS)
2795 laststart = b;
2796 else
2797 goto unfetch_interval;
2800 /* If the upper bound is zero, don't want to succeed at
2801 all; jump from `laststart' to `b + 3', which will be
2802 the end of the buffer after we insert the jump. */
2803 if (upper_bound == 0)
2805 GET_BUFFER_SPACE (3);
2806 INSERT_JUMP (jump, laststart, b + 3);
2807 b += 3;
2810 /* Otherwise, we have a nontrivial interval. When
2811 we're all done, the pattern will look like:
2812 set_number_at <jump count> <upper bound>
2813 set_number_at <succeed_n count> <lower bound>
2814 succeed_n <after jump addr> <succeed_n count>
2815 <body of loop>
2816 jump_n <succeed_n addr> <jump count>
2817 (The upper bound and `jump_n' are omitted if
2818 `upper_bound' is 1, though.) */
2819 else
2820 { /* If the upper bound is > 1, we need to insert
2821 more at the end of the loop. */
2822 unsigned nbytes = 10 + (upper_bound > 1) * 10;
2824 GET_BUFFER_SPACE (nbytes);
2826 /* Initialize lower bound of the `succeed_n', even
2827 though it will be set during matching by its
2828 attendant `set_number_at' (inserted next),
2829 because `re_compile_fastmap' needs to know.
2830 Jump to the `jump_n' we might insert below. */
2831 INSERT_JUMP2 (succeed_n, laststart,
2832 b + 5 + (upper_bound > 1) * 5,
2833 lower_bound);
2834 b += 5;
2836 /* Code to initialize the lower bound. Insert
2837 before the `succeed_n'. The `5' is the last two
2838 bytes of this `set_number_at', plus 3 bytes of
2839 the following `succeed_n'. */
2840 insert_op2 (set_number_at, laststart, 5, lower_bound, b);
2841 b += 5;
2843 if (upper_bound > 1)
2844 { /* More than one repetition is allowed, so
2845 append a backward jump to the `succeed_n'
2846 that starts this interval.
2848 When we've reached this during matching,
2849 we'll have matched the interval once, so
2850 jump back only `upper_bound - 1' times. */
2851 STORE_JUMP2 (jump_n, b, laststart + 5,
2852 upper_bound - 1);
2853 b += 5;
2855 /* The location we want to set is the second
2856 parameter of the `jump_n'; that is `b-2' as
2857 an absolute address. `laststart' will be
2858 the `set_number_at' we're about to insert;
2859 `laststart+3' the number to set, the source
2860 for the relative address. But we are
2861 inserting into the middle of the pattern --
2862 so everything is getting moved up by 5.
2863 Conclusion: (b - 2) - (laststart + 3) + 5,
2864 i.e., b - laststart.
2866 We insert this at the beginning of the loop
2867 so that if we fail during matching, we'll
2868 reinitialize the bounds. */
2869 insert_op2 (set_number_at, laststart, b - laststart,
2870 upper_bound - 1, b);
2871 b += 5;
2874 pending_exact = 0;
2875 beg_interval = NULL;
2877 break;
2879 unfetch_interval:
2880 /* If an invalid interval, match the characters as literals. */
2881 assert (beg_interval);
2882 p = beg_interval;
2883 beg_interval = NULL;
2885 /* normal_char and normal_backslash need `c'. */
2886 PATFETCH (c);
2888 if (!(syntax & RE_NO_BK_BRACES))
2890 if (p > pattern && p[-1] == '\\')
2891 goto normal_backslash;
2893 goto normal_char;
2895 #ifdef emacs
2896 /* There is no way to specify the before_dot and after_dot
2897 operators. rms says this is ok. --karl */
2898 case '=':
2899 BUF_PUSH (at_dot);
2900 break;
2902 case 's':
2903 laststart = b;
2904 PATFETCH (c);
2905 BUF_PUSH_2 (syntaxspec, syntax_spec_code[c]);
2906 break;
2908 case 'S':
2909 laststart = b;
2910 PATFETCH (c);
2911 BUF_PUSH_2 (notsyntaxspec, syntax_spec_code[c]);
2912 break;
2914 case 'c':
2915 laststart = b;
2916 PATFETCH_RAW (c);
2917 BUF_PUSH_2 (categoryspec, c);
2918 break;
2920 case 'C':
2921 laststart = b;
2922 PATFETCH_RAW (c);
2923 BUF_PUSH_2 (notcategoryspec, c);
2924 break;
2925 #endif /* emacs */
2928 case 'w':
2929 laststart = b;
2930 BUF_PUSH (wordchar);
2931 break;
2934 case 'W':
2935 laststart = b;
2936 BUF_PUSH (notwordchar);
2937 break;
2940 case '<':
2941 BUF_PUSH (wordbeg);
2942 break;
2944 case '>':
2945 BUF_PUSH (wordend);
2946 break;
2948 case 'b':
2949 BUF_PUSH (wordbound);
2950 break;
2952 case 'B':
2953 BUF_PUSH (notwordbound);
2954 break;
2956 case '`':
2957 BUF_PUSH (begbuf);
2958 break;
2960 case '\'':
2961 BUF_PUSH (endbuf);
2962 break;
2964 case '1': case '2': case '3': case '4': case '5':
2965 case '6': case '7': case '8': case '9':
2966 if (syntax & RE_NO_BK_REFS)
2967 goto normal_char;
2969 c1 = c - '0';
2971 if (c1 > regnum)
2972 FREE_STACK_RETURN (REG_ESUBREG);
2974 /* Can't back reference to a subexpression if inside of it. */
2975 if (group_in_compile_stack (compile_stack, c1))
2976 goto normal_char;
2978 laststart = b;
2979 BUF_PUSH_2 (duplicate, c1);
2980 break;
2983 case '+':
2984 case '?':
2985 if (syntax & RE_BK_PLUS_QM)
2986 goto handle_plus;
2987 else
2988 goto normal_backslash;
2990 default:
2991 normal_backslash:
2992 /* You might think it would be useful for \ to mean
2993 not to translate; but if we don't translate it
2994 it will never match anything. */
2995 c = TRANSLATE (c);
2996 goto normal_char;
2998 break;
3001 default:
3002 /* Expects the character in `c'. */
3003 normal_char:
3004 p1 = p - 1; /* P1 points the head of C. */
3005 #ifdef emacs
3006 if (bufp->multibyte)
3008 c = STRING_CHAR (p1, pend - p1);
3009 c = TRANSLATE (c);
3010 /* Set P to the next character boundary. */
3011 p += MULTIBYTE_FORM_LENGTH (p1, pend - p1) - 1;
3013 #endif
3014 /* If no exactn currently being built. */
3015 if (!pending_exact
3017 /* If last exactn not at current position. */
3018 || pending_exact + *pending_exact + 1 != b
3020 /* We have only one byte following the exactn for the count. */
3021 || *pending_exact >= (1 << BYTEWIDTH) - (p - p1)
3023 /* If followed by a repetition operator. */
3024 || (p != pend && (*p == '*' || *p == '^'))
3025 || ((syntax & RE_BK_PLUS_QM)
3026 ? p + 1 < pend && *p == '\\' && (p[1] == '+' || p[1] == '?')
3027 : p != pend && (*p == '+' || *p == '?'))
3028 || ((syntax & RE_INTERVALS)
3029 && ((syntax & RE_NO_BK_BRACES)
3030 ? p != pend && *p == '{'
3031 : p + 1 < pend && p[0] == '\\' && p[1] == '{')))
3033 /* Start building a new exactn. */
3035 laststart = b;
3037 BUF_PUSH_2 (exactn, 0);
3038 pending_exact = b - 1;
3041 #ifdef emacs
3042 if (! SINGLE_BYTE_CHAR_P (c))
3044 unsigned char str[MAX_MULTIBYTE_LENGTH];
3045 int i = CHAR_STRING (c, str);
3046 int j;
3047 for (j = 0; j < i; j++)
3049 BUF_PUSH (str[j]);
3050 (*pending_exact)++;
3053 else
3054 #endif
3056 BUF_PUSH (c);
3057 (*pending_exact)++;
3059 break;
3060 } /* switch (c) */
3061 } /* while p != pend */
3064 /* Through the pattern now. */
3066 FIXUP_ALT_JUMP ();
3068 if (!COMPILE_STACK_EMPTY)
3069 FREE_STACK_RETURN (REG_EPAREN);
3071 /* If we don't want backtracking, force success
3072 the first time we reach the end of the compiled pattern. */
3073 if (syntax & RE_NO_POSIX_BACKTRACKING)
3074 BUF_PUSH (succeed);
3076 free (compile_stack.stack);
3078 /* We have succeeded; set the length of the buffer. */
3079 bufp->used = b - bufp->buffer;
3081 #ifdef DEBUG
3082 if (debug)
3084 re_compile_fastmap (bufp);
3085 DEBUG_PRINT1 ("\nCompiled pattern: \n");
3086 print_compiled_pattern (bufp);
3087 /* debug = 0; */
3089 #endif /* DEBUG */
3091 #ifndef MATCH_MAY_ALLOCATE
3092 /* Initialize the failure stack to the largest possible stack. This
3093 isn't necessary unless we're trying to avoid calling alloca in
3094 the search and match routines. */
3096 int num_regs = bufp->re_nsub + 1;
3098 if (fail_stack.size < re_max_failures * TYPICAL_FAILURE_SIZE)
3100 fail_stack.size = re_max_failures * TYPICAL_FAILURE_SIZE;
3102 if (! fail_stack.stack)
3103 fail_stack.stack
3104 = (fail_stack_elt_t *) malloc (fail_stack.size
3105 * sizeof (fail_stack_elt_t));
3106 else
3107 fail_stack.stack
3108 = (fail_stack_elt_t *) realloc (fail_stack.stack,
3109 (fail_stack.size
3110 * sizeof (fail_stack_elt_t)));
3113 regex_grow_registers (num_regs);
3115 #endif /* not MATCH_MAY_ALLOCATE */
3117 return REG_NOERROR;
3118 } /* regex_compile */
3120 /* Subroutines for `regex_compile'. */
3122 /* Store OP at LOC followed by two-byte integer parameter ARG. */
3124 static void
3125 store_op1 (op, loc, arg)
3126 re_opcode_t op;
3127 unsigned char *loc;
3128 int arg;
3130 *loc = (unsigned char) op;
3131 STORE_NUMBER (loc + 1, arg);
3135 /* Like `store_op1', but for two two-byte parameters ARG1 and ARG2. */
3137 static void
3138 store_op2 (op, loc, arg1, arg2)
3139 re_opcode_t op;
3140 unsigned char *loc;
3141 int arg1, arg2;
3143 *loc = (unsigned char) op;
3144 STORE_NUMBER (loc + 1, arg1);
3145 STORE_NUMBER (loc + 3, arg2);
3149 /* Copy the bytes from LOC to END to open up three bytes of space at LOC
3150 for OP followed by two-byte integer parameter ARG. */
3152 static void
3153 insert_op1 (op, loc, arg, end)
3154 re_opcode_t op;
3155 unsigned char *loc;
3156 int arg;
3157 unsigned char *end;
3159 register unsigned char *pfrom = end;
3160 register unsigned char *pto = end + 3;
3162 while (pfrom != loc)
3163 *--pto = *--pfrom;
3165 store_op1 (op, loc, arg);
3169 /* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2. */
3171 static void
3172 insert_op2 (op, loc, arg1, arg2, end)
3173 re_opcode_t op;
3174 unsigned char *loc;
3175 int arg1, arg2;
3176 unsigned char *end;
3178 register unsigned char *pfrom = end;
3179 register unsigned char *pto = end + 5;
3181 while (pfrom != loc)
3182 *--pto = *--pfrom;
3184 store_op2 (op, loc, arg1, arg2);
3188 /* P points to just after a ^ in PATTERN. Return true if that ^ comes
3189 after an alternative or a begin-subexpression. We assume there is at
3190 least one character before the ^. */
3192 static boolean
3193 at_begline_loc_p (pattern, p, syntax)
3194 re_char *pattern, *p;
3195 reg_syntax_t syntax;
3197 re_char *prev = p - 2;
3198 boolean prev_prev_backslash = prev > pattern && prev[-1] == '\\';
3200 return
3201 /* After a subexpression? */
3202 (*prev == '(' && (syntax & RE_NO_BK_PARENS || prev_prev_backslash))
3203 /* After an alternative? */
3204 || (*prev == '|' && (syntax & RE_NO_BK_VBAR || prev_prev_backslash));
3208 /* The dual of at_begline_loc_p. This one is for $. We assume there is
3209 at least one character after the $, i.e., `P < PEND'. */
3211 static boolean
3212 at_endline_loc_p (p, pend, syntax)
3213 re_char *p, *pend;
3214 int syntax;
3216 re_char *next = p;
3217 boolean next_backslash = *next == '\\';
3218 re_char *next_next = p + 1 < pend ? p + 1 : 0;
3220 return
3221 /* Before a subexpression? */
3222 (syntax & RE_NO_BK_PARENS ? *next == ')'
3223 : next_backslash && next_next && *next_next == ')')
3224 /* Before an alternative? */
3225 || (syntax & RE_NO_BK_VBAR ? *next == '|'
3226 : next_backslash && next_next && *next_next == '|');
3230 /* Returns true if REGNUM is in one of COMPILE_STACK's elements and
3231 false if it's not. */
3233 static boolean
3234 group_in_compile_stack (compile_stack, regnum)
3235 compile_stack_type compile_stack;
3236 regnum_t regnum;
3238 int this_element;
3240 for (this_element = compile_stack.avail - 1;
3241 this_element >= 0;
3242 this_element--)
3243 if (compile_stack.stack[this_element].regnum == regnum)
3244 return true;
3246 return false;
3249 /* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
3250 BUFP. A fastmap records which of the (1 << BYTEWIDTH) possible
3251 characters can start a string that matches the pattern. This fastmap
3252 is used by re_search to skip quickly over impossible starting points.
3254 Character codes above (1 << BYTEWIDTH) are not represented in the
3255 fastmap, but the leading codes are represented. Thus, the fastmap
3256 indicates which character sets could start a match.
3258 The caller must supply the address of a (1 << BYTEWIDTH)-byte data
3259 area as BUFP->fastmap.
3261 We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in
3262 the pattern buffer.
3264 Returns 0 if we succeed, -2 if an internal error. */
3267 re_compile_fastmap (bufp)
3268 struct re_pattern_buffer *bufp;
3270 int j, k;
3271 #ifdef MATCH_MAY_ALLOCATE
3272 fail_stack_type fail_stack;
3273 #endif
3274 #ifndef REGEX_MALLOC
3275 char *destination;
3276 #endif
3278 register char *fastmap = bufp->fastmap;
3279 unsigned char *pattern = bufp->buffer;
3280 unsigned long size = bufp->used;
3281 unsigned char *p = pattern;
3282 register unsigned char *pend = pattern + size;
3284 /* This holds the pointer to the failure stack, when
3285 it is allocated relocatably. */
3286 fail_stack_elt_t *failure_stack_ptr;
3288 /* Assume that each path through the pattern can be null until
3289 proven otherwise. We set this false at the bottom of switch
3290 statement, to which we get only if a particular path doesn't
3291 match the empty string. */
3292 boolean path_can_be_null = true;
3294 /* We aren't doing a `succeed_n' to begin with. */
3295 boolean succeed_n_p = false;
3297 /* If all elements for base leading-codes in fastmap is set, this
3298 flag is set true. */
3299 boolean match_any_multibyte_characters = false;
3301 /* Maximum code of simple (single byte) character. */
3302 int simple_char_max;
3304 assert (fastmap != NULL && p != NULL);
3306 INIT_FAIL_STACK ();
3307 bzero (fastmap, 1 << BYTEWIDTH); /* Assume nothing's valid. */
3308 bufp->fastmap_accurate = 1; /* It will be when we're done. */
3309 bufp->can_be_null = 0;
3311 /* The loop below works as follows:
3312 - It has a working-list kept in the PATTERN_STACK and which basically
3313 starts by only containing a pointer to the first operation.
3314 - If the opcode we're looking at is a match against some set of
3315 chars, then we add those chars to the fastmap and go on to the
3316 next work element from the worklist (done via `break').
3317 - If the opcode is a control operator on the other hand, we either
3318 ignore it (if it's meaningless at this point, such as `start_memory')
3319 or execute it (if it's a jump). If the jump has several destinations
3320 (i.e. `on_failure_jump'), then we push the other destination onto the
3321 worklist.
3322 We guarantee termination by ignoring backward jumps (more or less),
3323 so that `p' is monotonically increasing. More to the point, we
3324 never set `p' (or push) anything `<= p1'. */
3326 /* If can_be_null is set, then the fastmap will not be used anyway. */
3327 while (!bufp->can_be_null)
3329 /* `p1' is used as a marker of how far back a `on_failure_jump'
3330 can go without being ignored. It is normally equal to `p'
3331 (which prevents any backward `on_failure_jump') except right
3332 after a plain `jump', to allow patterns such as:
3333 0: jump 10
3334 3..9: <body>
3335 10: on_failure_jump 3
3336 as used for the *? operator. */
3337 unsigned char *p1 = p;
3339 if (p == pend || *p == succeed)
3341 /* We have reached the (effective) end of pattern. */
3342 if (!PATTERN_STACK_EMPTY ())
3344 bufp->can_be_null |= path_can_be_null;
3346 /* Reset for next path. */
3347 path_can_be_null = true;
3349 p = (unsigned char*) POP_PATTERN_OP ();
3351 continue;
3353 else
3354 break;
3357 /* We should never be about to go beyond the end of the pattern. */
3358 assert (p < pend);
3360 switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++))
3363 case duplicate:
3364 /* If the first character has to match a backreference, that means
3365 that the group was empty (since it already matched). Since this
3366 is the only case that interests us here, we can assume that the
3367 backreference must match the empty string. */
3368 p++;
3369 continue;
3372 /* Following are the cases which match a character. These end
3373 with `break'. */
3375 case exactn:
3376 fastmap[p[1]] = 1;
3377 break;
3380 #ifndef emacs
3381 case charset:
3383 int length = (*p & 0x7f);;
3384 p++;
3386 for (j = length * BYTEWIDTH - 1; j >= 0; j--)
3387 if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
3388 fastmap[j] = 1;
3390 break;
3392 case charset_not:
3393 /* Chars beyond end of map must be allowed. */
3395 int length = (*p & 0x7f);;
3396 p++;
3398 for (j = length * BYTEWIDTH; j < (1 << BYTEWIDTH); j++)
3399 fastmap[j] = 1;
3401 for (j = length * BYTEWIDTH - 1; j >= 0; j--)
3402 if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
3403 fastmap[j] = 1;
3405 break;
3407 case wordchar:
3408 for (j = 0; j < (1 << BYTEWIDTH); j++)
3409 if (SYNTAX (j) == Sword)
3410 fastmap[j] = 1;
3411 break;
3414 case notwordchar:
3415 for (j = 0; j < (1 << BYTEWIDTH); j++)
3416 if (SYNTAX (j) != Sword)
3417 fastmap[j] = 1;
3418 break;
3419 #else /* emacs */
3420 case charset:
3421 for (j = CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH - 1, p++;
3422 j >= 0; j--)
3423 if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
3424 fastmap[j] = 1;
3426 /* If we can match a character class, we can match
3427 any character set. */
3428 if (CHARSET_RANGE_TABLE_EXISTS_P (&p[-2])
3429 && CHARSET_RANGE_TABLE_BITS (&p[-2]) != 0)
3430 goto set_fastmap_for_multibyte_characters;
3432 if (CHARSET_RANGE_TABLE_EXISTS_P (&p[-2])
3433 && match_any_multibyte_characters == false)
3435 /* Set fastmap[I] 1 where I is a base leading code of each
3436 multibyte character in the range table. */
3437 int c, count;
3439 /* Make P points the range table. */
3440 p += CHARSET_BITMAP_SIZE (&p[-2]);
3442 /* Extract the number of ranges in range table into COUNT. */
3443 EXTRACT_NUMBER_AND_INCR (count, p);
3444 for (; count > 0; count--, p += 2 * 3) /* XXX */
3446 /* Extract the start of each range. */
3447 EXTRACT_CHARACTER (c, p);
3448 j = CHAR_CHARSET (c);
3449 fastmap[CHARSET_LEADING_CODE_BASE (j)] = 1;
3452 break;
3455 case charset_not:
3456 /* Chars beyond end of bitmap are possible matches.
3457 All the single-byte codes can occur in multibyte buffers.
3458 So any that are not listed in the charset
3459 are possible matches, even in multibyte buffers. */
3460 simple_char_max = (1 << BYTEWIDTH);
3461 for (j = CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH;
3462 j < simple_char_max; j++)
3463 fastmap[j] = 1;
3465 for (j = CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH - 1, p++;
3466 j >= 0; j--)
3467 if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
3468 fastmap[j] = 1;
3470 if (bufp->multibyte)
3471 /* Any character set can possibly contain a character
3472 which doesn't match the specified set of characters. */
3474 set_fastmap_for_multibyte_characters:
3475 if (match_any_multibyte_characters == false)
3477 for (j = 0x80; j < 0xA0; j++) /* XXX */
3478 if (BASE_LEADING_CODE_P (j))
3479 fastmap[j] = 1;
3480 match_any_multibyte_characters = true;
3483 break;
3486 case wordchar:
3487 /* All the single-byte codes can occur in multibyte buffers,
3488 and they may have word syntax. So do consider them. */
3489 simple_char_max = (1 << BYTEWIDTH);
3490 for (j = 0; j < simple_char_max; j++)
3491 if (SYNTAX (j) == Sword)
3492 fastmap[j] = 1;
3494 if (bufp->multibyte)
3495 /* Any character set can possibly contain a character
3496 whose syntax is `Sword'. */
3497 goto set_fastmap_for_multibyte_characters;
3498 break;
3501 case notwordchar:
3502 /* All the single-byte codes can occur in multibyte buffers,
3503 and they may not have word syntax. So do consider them. */
3504 simple_char_max = (1 << BYTEWIDTH);
3505 for (j = 0; j < simple_char_max; j++)
3506 if (SYNTAX (j) != Sword)
3507 fastmap[j] = 1;
3509 if (bufp->multibyte)
3510 /* Any character set can possibly contain a character
3511 whose syntax is not `Sword'. */
3512 goto set_fastmap_for_multibyte_characters;
3513 break;
3514 #endif
3516 case anychar:
3518 int fastmap_newline = fastmap['\n'];
3520 /* `.' matches anything, except perhaps newline.
3521 Even in a multibyte buffer, it should match any
3522 conceivable byte value for the fastmap. */
3523 if (bufp->multibyte)
3524 match_any_multibyte_characters = true;
3526 simple_char_max = (1 << BYTEWIDTH);
3527 for (j = 0; j < simple_char_max; j++)
3528 fastmap[j] = 1;
3530 /* ... except perhaps newline. */
3531 if (!(bufp->syntax & RE_DOT_NEWLINE))
3532 fastmap['\n'] = fastmap_newline;
3534 /* Otherwise, have to check alternative paths. */
3535 break;
3538 #ifdef emacs
3539 case wordbound:
3540 case notwordbound:
3541 case wordbeg:
3542 case wordend:
3543 case notsyntaxspec:
3544 case syntaxspec:
3545 /* This match depends on text properties. These end with
3546 aborting optimizations. */
3547 bufp->can_be_null = 1;
3548 continue;
3549 #if 0
3550 k = *p++;
3551 simple_char_max = bufp->multibyte ? 0x80 : (1 << BYTEWIDTH);
3552 for (j = 0; j < simple_char_max; j++)
3553 if (SYNTAX (j) == (enum syntaxcode) k)
3554 fastmap[j] = 1;
3556 if (bufp->multibyte)
3557 /* Any character set can possibly contain a character
3558 whose syntax is K. */
3559 goto set_fastmap_for_multibyte_characters;
3560 break;
3562 case notsyntaxspec:
3563 k = *p++;
3564 simple_char_max = bufp->multibyte ? 0x80 : (1 << BYTEWIDTH);
3565 for (j = 0; j < simple_char_max; j++)
3566 if (SYNTAX (j) != (enum syntaxcode) k)
3567 fastmap[j] = 1;
3569 if (bufp->multibyte)
3570 /* Any character set can possibly contain a character
3571 whose syntax is not K. */
3572 goto set_fastmap_for_multibyte_characters;
3573 break;
3574 #endif
3577 case categoryspec:
3578 k = *p++;
3579 simple_char_max = (1 << BYTEWIDTH);
3580 for (j = 0; j < simple_char_max; j++)
3581 if (CHAR_HAS_CATEGORY (j, k))
3582 fastmap[j] = 1;
3584 if (bufp->multibyte)
3585 /* Any character set can possibly contain a character
3586 whose category is K. */
3587 goto set_fastmap_for_multibyte_characters;
3588 break;
3591 case notcategoryspec:
3592 k = *p++;
3593 simple_char_max = (1 << BYTEWIDTH);
3594 for (j = 0; j < simple_char_max; j++)
3595 if (!CHAR_HAS_CATEGORY (j, k))
3596 fastmap[j] = 1;
3598 if (bufp->multibyte)
3599 /* Any character set can possibly contain a character
3600 whose category is not K. */
3601 goto set_fastmap_for_multibyte_characters;
3602 break;
3604 /* All cases after this match the empty string. These end with
3605 `continue'. */
3608 case before_dot:
3609 case at_dot:
3610 case after_dot:
3611 continue;
3612 #endif /* emacs */
3615 case no_op:
3616 case begline:
3617 case endline:
3618 case begbuf:
3619 case endbuf:
3620 #ifndef emacs
3621 case wordbound:
3622 case notwordbound:
3623 case wordbeg:
3624 case wordend:
3625 #endif
3626 continue;
3629 case jump_n:
3630 case jump:
3631 EXTRACT_NUMBER_AND_INCR (j, p);
3632 if (j < 0)
3633 /* Backward jumps can only go back to code that we've already
3634 visited. `re_compile' should make sure this is true. */
3635 break;
3636 p += j;
3637 switch (SWITCH_ENUM_CAST ((re_opcode_t) *p))
3639 case on_failure_jump:
3640 case on_failure_keep_string_jump:
3641 case on_failure_jump_exclusive:
3642 case on_failure_jump_loop:
3643 case on_failure_jump_smart:
3644 p++;
3645 break;
3646 default:
3647 continue;
3649 /* Keep `p1' to allow the `on_failure_jump' we are jumping to
3650 to jump back to "just after here". */
3651 /* Fallthrough */
3653 case on_failure_jump:
3654 case on_failure_keep_string_jump:
3655 case on_failure_jump_exclusive:
3656 case on_failure_jump_loop:
3657 case on_failure_jump_smart:
3658 handle_on_failure_jump:
3659 EXTRACT_NUMBER_AND_INCR (j, p);
3661 /* For some patterns, e.g., `(a?)?', `p+j' here points to the
3662 end of the pattern. We don't want to push such a point,
3663 since when we restore it above, entering the switch will
3664 increment `p' past the end of the pattern. We don't need
3665 to push such a point since we obviously won't find any more
3666 fastmap entries beyond `pend'. Such a pattern can match
3667 the null string, though. */
3668 if (p + j <= p1)
3669 /* Backward jump to be ignored. */
3671 else if (p + j < pend)
3673 if (!PUSH_PATTERN_OP (p + j, fail_stack))
3675 RESET_FAIL_STACK ();
3676 return -2;
3679 else
3680 bufp->can_be_null = 1;
3682 if (succeed_n_p)
3684 EXTRACT_NUMBER_AND_INCR (k, p); /* Skip the n. */
3685 succeed_n_p = false;
3688 continue;
3691 case succeed_n:
3692 /* Get to the number of times to succeed. */
3693 p += 2;
3695 /* Increment p past the n for when k != 0. */
3696 EXTRACT_NUMBER_AND_INCR (k, p);
3697 if (k == 0)
3699 p -= 4;
3700 succeed_n_p = true; /* Spaghetti code alert. */
3701 goto handle_on_failure_jump;
3703 continue;
3706 case set_number_at:
3707 p += 4;
3708 continue;
3711 case start_memory:
3712 case stop_memory:
3713 p += 1;
3714 continue;
3717 default:
3718 abort (); /* We have listed all the cases. */
3719 } /* switch *p++ */
3721 /* Getting here means we have found the possible starting
3722 characters for one path of the pattern -- and that the empty
3723 string does not match. We need not follow this path further.
3724 Instead, look at the next alternative (remembered on the
3725 stack), or quit if no more. The test at the top of the loop
3726 does these things. */
3727 path_can_be_null = false;
3728 p = pend;
3729 } /* while p */
3731 /* Set `can_be_null' for the last path (also the first path, if the
3732 pattern is empty). */
3733 bufp->can_be_null |= path_can_be_null;
3734 RESET_FAIL_STACK ();
3735 return 0;
3736 } /* re_compile_fastmap */
3738 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
3739 ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
3740 this memory for recording register information. STARTS and ENDS
3741 must be allocated using the malloc library routine, and must each
3742 be at least NUM_REGS * sizeof (regoff_t) bytes long.
3744 If NUM_REGS == 0, then subsequent matches should allocate their own
3745 register data.
3747 Unless this function is called, the first search or match using
3748 PATTERN_BUFFER will allocate its own register data, without
3749 freeing the old data. */
3751 void
3752 re_set_registers (bufp, regs, num_regs, starts, ends)
3753 struct re_pattern_buffer *bufp;
3754 struct re_registers *regs;
3755 unsigned num_regs;
3756 regoff_t *starts, *ends;
3758 if (num_regs)
3760 bufp->regs_allocated = REGS_REALLOCATE;
3761 regs->num_regs = num_regs;
3762 regs->start = starts;
3763 regs->end = ends;
3765 else
3767 bufp->regs_allocated = REGS_UNALLOCATED;
3768 regs->num_regs = 0;
3769 regs->start = regs->end = (regoff_t *) 0;
3773 /* Searching routines. */
3775 /* Like re_search_2, below, but only one string is specified, and
3776 doesn't let you say where to stop matching. */
3779 re_search (bufp, string, size, startpos, range, regs)
3780 struct re_pattern_buffer *bufp;
3781 const char *string;
3782 int size, startpos, range;
3783 struct re_registers *regs;
3785 return re_search_2 (bufp, NULL, 0, string, size, startpos, range,
3786 regs, size);
3789 /* End address of virtual concatenation of string. */
3790 #define STOP_ADDR_VSTRING(P) \
3791 (((P) >= size1 ? string2 + size2 : string1 + size1))
3793 /* Address of POS in the concatenation of virtual string. */
3794 #define POS_ADDR_VSTRING(POS) \
3795 (((POS) >= size1 ? string2 - size1 : string1) + (POS))
3797 /* Using the compiled pattern in BUFP->buffer, first tries to match the
3798 virtual concatenation of STRING1 and STRING2, starting first at index
3799 STARTPOS, then at STARTPOS + 1, and so on.
3801 STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
3803 RANGE is how far to scan while trying to match. RANGE = 0 means try
3804 only at STARTPOS; in general, the last start tried is STARTPOS +
3805 RANGE.
3807 In REGS, return the indices of the virtual concatenation of STRING1
3808 and STRING2 that matched the entire BUFP->buffer and its contained
3809 subexpressions.
3811 Do not consider matching one past the index STOP in the virtual
3812 concatenation of STRING1 and STRING2.
3814 We return either the position in the strings at which the match was
3815 found, -1 if no match, or -2 if error (such as failure
3816 stack overflow). */
3819 re_search_2 (bufp, str1, size1, str2, size2, startpos, range, regs, stop)
3820 struct re_pattern_buffer *bufp;
3821 const char *str1, *str2;
3822 int size1, size2;
3823 int startpos;
3824 int range;
3825 struct re_registers *regs;
3826 int stop;
3828 int val;
3829 re_char *string1 = (re_char*) str1;
3830 re_char *string2 = (re_char*) str2;
3831 register char *fastmap = bufp->fastmap;
3832 register RE_TRANSLATE_TYPE translate = bufp->translate;
3833 int total_size = size1 + size2;
3834 int endpos = startpos + range;
3835 int anchored_start = 0;
3837 /* Nonzero if we have to concern multibyte character. */
3838 int multibyte = bufp->multibyte;
3840 /* Check for out-of-range STARTPOS. */
3841 if (startpos < 0 || startpos > total_size)
3842 return -1;
3844 /* Fix up RANGE if it might eventually take us outside
3845 the virtual concatenation of STRING1 and STRING2.
3846 Make sure we won't move STARTPOS below 0 or above TOTAL_SIZE. */
3847 if (endpos < 0)
3848 range = 0 - startpos;
3849 else if (endpos > total_size)
3850 range = total_size - startpos;
3852 /* If the search isn't to be a backwards one, don't waste time in a
3853 search for a pattern anchored at beginning of buffer. */
3854 if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == begbuf && range > 0)
3856 if (startpos > 0)
3857 return -1;
3858 else
3859 range = 0;
3862 #ifdef emacs
3863 /* In a forward search for something that starts with \=.
3864 don't keep searching past point. */
3865 if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == at_dot && range > 0)
3867 range = PT_BYTE - BEGV_BYTE - startpos;
3868 if (range < 0)
3869 return -1;
3871 #endif /* emacs */
3873 /* Update the fastmap now if not correct already. */
3874 if (fastmap && !bufp->fastmap_accurate)
3875 if (re_compile_fastmap (bufp) == -2)
3876 return -2;
3878 /* See whether the pattern is anchored. */
3879 if (bufp->buffer[0] == begline)
3880 anchored_start = 1;
3882 #ifdef emacs
3883 gl_state.object = re_match_object;
3885 int adjpos = NILP (re_match_object) || BUFFERP (re_match_object);
3886 int charpos = SYNTAX_TABLE_BYTE_TO_CHAR (startpos + adjpos);
3888 SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object, charpos, 1);
3890 #endif
3892 /* Loop through the string, looking for a place to start matching. */
3893 for (;;)
3895 /* If the pattern is anchored,
3896 skip quickly past places we cannot match.
3897 We don't bother to treat startpos == 0 specially
3898 because that case doesn't repeat. */
3899 if (anchored_start && startpos > 0)
3901 if (! (bufp->newline_anchor
3902 && ((startpos <= size1 ? string1[startpos - 1]
3903 : string2[startpos - size1 - 1])
3904 == '\n')))
3905 goto advance;
3908 /* If a fastmap is supplied, skip quickly over characters that
3909 cannot be the start of a match. If the pattern can match the
3910 null string, however, we don't need to skip characters; we want
3911 the first null string. */
3912 if (fastmap && startpos < total_size && !bufp->can_be_null)
3914 register re_char *d;
3915 register unsigned int buf_ch;
3917 d = POS_ADDR_VSTRING (startpos);
3919 if (range > 0) /* Searching forwards. */
3921 register int lim = 0;
3922 int irange = range;
3924 if (startpos < size1 && startpos + range >= size1)
3925 lim = range - (size1 - startpos);
3927 /* Written out as an if-else to avoid testing `translate'
3928 inside the loop. */
3929 if (RE_TRANSLATE_P (translate))
3931 if (multibyte)
3932 while (range > lim)
3934 int buf_charlen;
3936 buf_ch = STRING_CHAR_AND_LENGTH (d, range - lim,
3937 buf_charlen);
3939 buf_ch = RE_TRANSLATE (translate, buf_ch);
3940 if (buf_ch >= 0400
3941 || fastmap[buf_ch])
3942 break;
3944 range -= buf_charlen;
3945 d += buf_charlen;
3947 else
3948 while (range > lim
3949 && !fastmap[RE_TRANSLATE (translate, *d)])
3951 d++;
3952 range--;
3955 else
3956 while (range > lim && !fastmap[*d])
3958 d++;
3959 range--;
3962 startpos += irange - range;
3964 else /* Searching backwards. */
3966 int room = (size1 == 0 || startpos >= size1
3967 ? size2 + size1 - startpos
3968 : size1 - startpos);
3970 buf_ch = STRING_CHAR (d, room);
3971 if (RE_TRANSLATE_P (translate))
3972 buf_ch = RE_TRANSLATE (translate, buf_ch);
3974 if (! (buf_ch >= 0400
3975 || fastmap[buf_ch]))
3976 goto advance;
3980 /* If can't match the null string, and that's all we have left, fail. */
3981 if (range >= 0 && startpos == total_size && fastmap
3982 && !bufp->can_be_null)
3983 return -1;
3985 val = re_match_2_internal (bufp, string1, size1, string2, size2,
3986 startpos, regs, stop);
3987 #ifndef REGEX_MALLOC
3988 #ifdef C_ALLOCA
3989 alloca (0);
3990 #endif
3991 #endif
3993 if (val >= 0)
3994 return startpos;
3996 if (val == -2)
3997 return -2;
3999 advance:
4000 if (!range)
4001 break;
4002 else if (range > 0)
4004 /* Update STARTPOS to the next character boundary. */
4005 if (multibyte)
4007 re_char *p = POS_ADDR_VSTRING (startpos);
4008 re_char *pend = STOP_ADDR_VSTRING (startpos);
4009 int len = MULTIBYTE_FORM_LENGTH (p, pend - p);
4011 range -= len;
4012 if (range < 0)
4013 break;
4014 startpos += len;
4016 else
4018 range--;
4019 startpos++;
4022 else
4024 range++;
4025 startpos--;
4027 /* Update STARTPOS to the previous character boundary. */
4028 if (multibyte)
4030 re_char *p = 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 re_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 boolean not;
4406 unsigned char *p1;
4408 /* Just past the end of the corresponding string. */
4409 re_char *end1, *end2;
4411 /* Pointers into string1 and string2, just past the last characters in
4412 each to consider matching. */
4413 re_char *end_match_1, *end_match_2;
4415 /* Where we are in the data, and the end of the current string. */
4416 re_char *d, *dend;
4418 /* Where we are in the pattern, and the end of the pattern. */
4419 unsigned char *p = bufp->buffer;
4420 register unsigned char *pend = p + bufp->used;
4422 /* We use this to map every character in the string. */
4423 RE_TRANSLATE_TYPE translate = bufp->translate;
4425 /* Nonzero if we have to concern multibyte character. */
4426 int multibyte = bufp->multibyte;
4428 /* Failure point stack. Each place that can handle a failure further
4429 down the line pushes a failure point on this stack. It consists of
4430 regstart, and regend for all registers corresponding to
4431 the subexpressions we're currently inside, plus the number of such
4432 registers, and, finally, two char *'s. The first char * is where
4433 to resume scanning the pattern; the second one is where to resume
4434 scanning the strings. */
4435 #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
4436 fail_stack_type fail_stack;
4437 #endif
4438 #ifdef DEBUG
4439 static unsigned failure_id = 0;
4440 unsigned nfailure_points_pushed = 0, nfailure_points_popped = 0;
4441 #endif
4443 /* This holds the pointer to the failure stack, when
4444 it is allocated relocatably. */
4445 fail_stack_elt_t *failure_stack_ptr;
4447 /* We fill all the registers internally, independent of what we
4448 return, for use in backreferences. The number here includes
4449 an element for register zero. */
4450 unsigned num_regs = bufp->re_nsub + 1;
4452 /* Information on the contents of registers. These are pointers into
4453 the input strings; they record just what was matched (on this
4454 attempt) by a subexpression part of the pattern, that is, the
4455 regnum-th regstart pointer points to where in the pattern we began
4456 matching and the regnum-th regend points to right after where we
4457 stopped matching the regnum-th subexpression. (The zeroth register
4458 keeps track of what the whole pattern matches.) */
4459 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
4460 re_char **regstart, **regend;
4461 #endif
4463 /* The following record the register info as found in the above
4464 variables when we find a match better than any we've seen before.
4465 This happens as we backtrack through the failure points, which in
4466 turn happens only if we have not yet matched the entire string. */
4467 unsigned best_regs_set = false;
4468 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
4469 re_char **best_regstart, **best_regend;
4470 #endif
4472 /* Logically, this is `best_regend[0]'. But we don't want to have to
4473 allocate space for that if we're not allocating space for anything
4474 else (see below). Also, we never need info about register 0 for
4475 any of the other register vectors, and it seems rather a kludge to
4476 treat `best_regend' differently than the rest. So we keep track of
4477 the end of the best match so far in a separate variable. We
4478 initialize this to NULL so that when we backtrack the first time
4479 and need to test it, it's not garbage. */
4480 re_char *match_end = NULL;
4482 #ifdef DEBUG
4483 /* Counts the total number of registers pushed. */
4484 unsigned num_regs_pushed = 0;
4485 #endif
4487 DEBUG_PRINT1 ("\n\nEntering re_match_2.\n");
4489 INIT_FAIL_STACK ();
4491 #ifdef MATCH_MAY_ALLOCATE
4492 /* Do not bother to initialize all the register variables if there are
4493 no groups in the pattern, as it takes a fair amount of time. If
4494 there are groups, we include space for register 0 (the whole
4495 pattern), even though we never use it, since it simplifies the
4496 array indexing. We should fix this. */
4497 if (bufp->re_nsub)
4499 regstart = REGEX_TALLOC (num_regs, re_char *);
4500 regend = REGEX_TALLOC (num_regs, re_char *);
4501 best_regstart = REGEX_TALLOC (num_regs, re_char *);
4502 best_regend = REGEX_TALLOC (num_regs, re_char *);
4504 if (!(regstart && regend && best_regstart && best_regend))
4506 FREE_VARIABLES ();
4507 return -2;
4510 else
4512 /* We must initialize all our variables to NULL, so that
4513 `FREE_VARIABLES' doesn't try to free them. */
4514 regstart = regend = best_regstart = best_regend = NULL;
4516 #endif /* MATCH_MAY_ALLOCATE */
4518 /* The starting position is bogus. */
4519 if (pos < 0 || pos > size1 + size2)
4521 FREE_VARIABLES ();
4522 return -1;
4525 /* Initialize subexpression text positions to -1 to mark ones that no
4526 start_memory/stop_memory has been seen for. Also initialize the
4527 register information struct. */
4528 for (mcnt = 1; mcnt < num_regs; mcnt++)
4530 regstart[mcnt] = regend[mcnt] = REG_UNSET_VALUE;
4533 /* We move `string1' into `string2' if the latter's empty -- but not if
4534 `string1' is null. */
4535 if (size2 == 0 && string1 != NULL)
4537 string2 = string1;
4538 size2 = size1;
4539 string1 = 0;
4540 size1 = 0;
4542 end1 = string1 + size1;
4543 end2 = string2 + size2;
4545 /* Compute where to stop matching, within the two strings. */
4546 if (stop <= size1)
4548 end_match_1 = string1 + stop;
4549 end_match_2 = string2;
4551 else
4553 end_match_1 = end1;
4554 end_match_2 = string2 + stop - size1;
4557 /* `p' scans through the pattern as `d' scans through the data.
4558 `dend' is the end of the input string that `d' points within. `d'
4559 is advanced into the following input string whenever necessary, but
4560 this happens before fetching; therefore, at the beginning of the
4561 loop, `d' can be pointing at the end of a string, but it cannot
4562 equal `string2'. */
4563 if (size1 > 0 && pos <= size1)
4565 d = string1 + pos;
4566 dend = end_match_1;
4568 else
4570 d = string2 + pos - size1;
4571 dend = end_match_2;
4574 DEBUG_PRINT1 ("The compiled pattern is: ");
4575 DEBUG_PRINT_COMPILED_PATTERN (bufp, p, pend);
4576 DEBUG_PRINT1 ("The string to match is: `");
4577 DEBUG_PRINT_DOUBLE_STRING (d, string1, size1, string2, size2);
4578 DEBUG_PRINT1 ("'\n");
4580 /* This loops over pattern commands. It exits by returning from the
4581 function if the match is complete, or it drops through if the match
4582 fails at this starting point in the input data. */
4583 for (;;)
4585 DEBUG_PRINT2 ("\n%p: ", p);
4587 if (p == pend)
4588 { /* End of pattern means we might have succeeded. */
4589 DEBUG_PRINT1 ("end of pattern ... ");
4591 /* If we haven't matched the entire string, and we want the
4592 longest match, try backtracking. */
4593 if (d != end_match_2)
4595 /* 1 if this match ends in the same string (string1 or string2)
4596 as the best previous match. */
4597 boolean same_str_p = (FIRST_STRING_P (match_end)
4598 == MATCHING_IN_FIRST_STRING);
4599 /* 1 if this match is the best seen so far. */
4600 boolean best_match_p;
4602 /* AIX compiler got confused when this was combined
4603 with the previous declaration. */
4604 if (same_str_p)
4605 best_match_p = d > match_end;
4606 else
4607 best_match_p = !MATCHING_IN_FIRST_STRING;
4609 DEBUG_PRINT1 ("backtracking.\n");
4611 if (!FAIL_STACK_EMPTY ())
4612 { /* More failure points to try. */
4614 /* If exceeds best match so far, save it. */
4615 if (!best_regs_set || best_match_p)
4617 best_regs_set = true;
4618 match_end = d;
4620 DEBUG_PRINT1 ("\nSAVING match as best so far.\n");
4622 for (mcnt = 1; mcnt < num_regs; mcnt++)
4624 best_regstart[mcnt] = regstart[mcnt];
4625 best_regend[mcnt] = regend[mcnt];
4628 goto fail;
4631 /* If no failure points, don't restore garbage. And if
4632 last match is real best match, don't restore second
4633 best one. */
4634 else if (best_regs_set && !best_match_p)
4636 restore_best_regs:
4637 /* Restore best match. It may happen that `dend ==
4638 end_match_1' while the restored d is in string2.
4639 For example, the pattern `x.*y.*z' against the
4640 strings `x-' and `y-z-', if the two strings are
4641 not consecutive in memory. */
4642 DEBUG_PRINT1 ("Restoring best registers.\n");
4644 d = match_end;
4645 dend = ((d >= string1 && d <= end1)
4646 ? end_match_1 : end_match_2);
4648 for (mcnt = 1; mcnt < num_regs; mcnt++)
4650 regstart[mcnt] = best_regstart[mcnt];
4651 regend[mcnt] = best_regend[mcnt];
4654 } /* d != end_match_2 */
4656 succeed_label:
4657 DEBUG_PRINT1 ("Accepting match.\n");
4659 /* If caller wants register contents data back, do it. */
4660 if (regs && !bufp->no_sub)
4662 /* Have the register data arrays been allocated? */
4663 if (bufp->regs_allocated == REGS_UNALLOCATED)
4664 { /* No. So allocate them with malloc. We need one
4665 extra element beyond `num_regs' for the `-1' marker
4666 GNU code uses. */
4667 regs->num_regs = MAX (RE_NREGS, num_regs + 1);
4668 regs->start = TALLOC (regs->num_regs, regoff_t);
4669 regs->end = TALLOC (regs->num_regs, regoff_t);
4670 if (regs->start == NULL || regs->end == NULL)
4672 FREE_VARIABLES ();
4673 return -2;
4675 bufp->regs_allocated = REGS_REALLOCATE;
4677 else if (bufp->regs_allocated == REGS_REALLOCATE)
4678 { /* Yes. If we need more elements than were already
4679 allocated, reallocate them. If we need fewer, just
4680 leave it alone. */
4681 if (regs->num_regs < num_regs + 1)
4683 regs->num_regs = num_regs + 1;
4684 RETALLOC (regs->start, regs->num_regs, regoff_t);
4685 RETALLOC (regs->end, regs->num_regs, regoff_t);
4686 if (regs->start == NULL || regs->end == NULL)
4688 FREE_VARIABLES ();
4689 return -2;
4693 else
4695 /* These braces fend off a "empty body in an else-statement"
4696 warning under GCC when assert expands to nothing. */
4697 assert (bufp->regs_allocated == REGS_FIXED);
4700 /* Convert the pointer data in `regstart' and `regend' to
4701 indices. Register zero has to be set differently,
4702 since we haven't kept track of any info for it. */
4703 if (regs->num_regs > 0)
4705 regs->start[0] = pos;
4706 regs->end[0] = (MATCHING_IN_FIRST_STRING
4707 ? ((regoff_t) (d - string1))
4708 : ((regoff_t) (d - string2 + size1)));
4711 /* Go through the first `min (num_regs, regs->num_regs)'
4712 registers, since that is all we initialized. */
4713 for (mcnt = 1; mcnt < MIN (num_regs, regs->num_regs); mcnt++)
4715 if (REG_UNSET (regstart[mcnt]) || REG_UNSET (regend[mcnt]))
4716 regs->start[mcnt] = regs->end[mcnt] = -1;
4717 else
4719 regs->start[mcnt]
4720 = (regoff_t) POINTER_TO_OFFSET (regstart[mcnt]);
4721 regs->end[mcnt]
4722 = (regoff_t) POINTER_TO_OFFSET (regend[mcnt]);
4726 /* If the regs structure we return has more elements than
4727 were in the pattern, set the extra elements to -1. If
4728 we (re)allocated the registers, this is the case,
4729 because we always allocate enough to have at least one
4730 -1 at the end. */
4731 for (mcnt = num_regs; mcnt < regs->num_regs; mcnt++)
4732 regs->start[mcnt] = regs->end[mcnt] = -1;
4733 } /* regs && !bufp->no_sub */
4735 DEBUG_PRINT4 ("%u failure points pushed, %u popped (%u remain).\n",
4736 nfailure_points_pushed, nfailure_points_popped,
4737 nfailure_points_pushed - nfailure_points_popped);
4738 DEBUG_PRINT2 ("%u registers pushed.\n", num_regs_pushed);
4740 mcnt = d - pos - (MATCHING_IN_FIRST_STRING
4741 ? string1
4742 : string2 - size1);
4744 DEBUG_PRINT2 ("Returning %d from re_match_2.\n", mcnt);
4746 FREE_VARIABLES ();
4747 return mcnt;
4750 /* Otherwise match next pattern command. */
4751 switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++))
4753 /* Ignore these. Used to ignore the n of succeed_n's which
4754 currently have n == 0. */
4755 case no_op:
4756 DEBUG_PRINT1 ("EXECUTING no_op.\n");
4757 break;
4759 case succeed:
4760 DEBUG_PRINT1 ("EXECUTING succeed.\n");
4761 goto succeed_label;
4763 /* Match the next n pattern characters exactly. The following
4764 byte in the pattern defines n, and the n bytes after that
4765 are the characters to match. */
4766 case exactn:
4767 mcnt = *p++;
4768 DEBUG_PRINT2 ("EXECUTING exactn %d.\n", mcnt);
4770 /* This is written out as an if-else so we don't waste time
4771 testing `translate' inside the loop. */
4772 if (RE_TRANSLATE_P (translate))
4774 #ifdef emacs
4775 if (multibyte)
4778 int pat_charlen, buf_charlen;
4779 unsigned int pat_ch, buf_ch;
4781 PREFETCH ();
4782 pat_ch = STRING_CHAR_AND_LENGTH (p, pend - p, pat_charlen);
4783 buf_ch = STRING_CHAR_AND_LENGTH (d, dend - d, buf_charlen);
4785 if (RE_TRANSLATE (translate, buf_ch)
4786 != pat_ch)
4787 goto fail;
4789 p += pat_charlen;
4790 d += buf_charlen;
4791 mcnt -= pat_charlen;
4793 while (mcnt > 0);
4794 else
4795 #endif /* not emacs */
4798 PREFETCH ();
4799 if (RE_TRANSLATE (translate, *d) != *p++)
4800 goto fail;
4801 d++;
4803 while (--mcnt);
4805 else
4809 PREFETCH ();
4810 if (*d++ != *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 = *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 = *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 re_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 case notwordbound:
5286 not = (re_opcode_t) *(p - 1) == notwordbound;
5287 DEBUG_PRINT2 ("EXECUTING %swordbound.\n", not?"not":"");
5289 /* We SUCCEED in one of the following cases: */
5291 /* Case 1: D is at the beginning or the end of string. */
5292 if (AT_STRINGS_BEG (d) || AT_STRINGS_END (d))
5293 not = !not;
5294 else
5296 /* C1 is the character before D, S1 is the syntax of C1, C2
5297 is the character at D, and S2 is the syntax of C2. */
5298 int c1, c2, s1, s2;
5299 #ifdef emacs
5300 int charpos = SYNTAX_TABLE_BYTE_TO_CHAR (PTR_TO_OFFSET (d - 1));
5301 UPDATE_SYNTAX_TABLE (charpos);
5302 #endif
5303 /* FIXME: This does a STRING_CHAR even for unibyte buffers. */
5304 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
5305 s1 = SYNTAX (c1);
5306 #ifdef emacs
5307 UPDATE_SYNTAX_TABLE_FORWARD (charpos + 1);
5308 #endif
5309 PREFETCH ();
5310 /* FIXME: This does a STRING_CHAR even for unibyte buffers. */
5311 c2 = STRING_CHAR (d, dend - d);
5312 s2 = SYNTAX (c2);
5314 if (/* Case 2: Only one of S1 and S2 is Sword. */
5315 ((s1 == Sword) != (s2 == Sword))
5316 /* Case 3: Both of S1 and S2 are Sword, and macro
5317 WORD_BOUNDARY_P (C1, C2) returns nonzero. */
5318 || ((s1 == Sword) && WORD_BOUNDARY_P (c1, c2)))
5319 not = !not;
5321 if (not)
5322 break;
5323 else
5324 goto fail;
5326 case wordbeg:
5327 DEBUG_PRINT1 ("EXECUTING wordbeg.\n");
5329 /* We FAIL in one of the following cases: */
5331 /* Case 1: D is at the end of string. */
5332 if (AT_STRINGS_END (d))
5333 goto fail;
5334 else
5336 /* C1 is the character before D, S1 is the syntax of C1, C2
5337 is the character at D, and S2 is the syntax of C2. */
5338 int c1, c2, s1, s2;
5339 int pos1 = PTR_TO_OFFSET (d);
5340 int charpos;
5342 PREFETCH ();
5343 c2 = STRING_CHAR (d, dend - d);
5344 #ifdef emacs
5345 charpos = SYNTAX_TABLE_BYTE_TO_CHAR (pos1);
5346 UPDATE_SYNTAX_TABLE (charpos);
5347 #endif
5348 s2 = SYNTAX (c2);
5350 /* Case 2: S2 is not Sword. */
5351 if (s2 != Sword)
5352 goto fail;
5354 /* Case 3: D is not at the beginning of string ... */
5355 if (!AT_STRINGS_BEG (d))
5357 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
5358 #ifdef emacs
5359 UPDATE_SYNTAX_TABLE_BACKWARD (charpos - 1);
5360 #endif
5361 s1 = SYNTAX (c1);
5363 /* ... and S1 is Sword, and WORD_BOUNDARY_P (C1, C2)
5364 returns 0. */
5365 if ((s1 == Sword) && !WORD_BOUNDARY_P (c1, c2))
5366 goto fail;
5369 break;
5371 case wordend:
5372 DEBUG_PRINT1 ("EXECUTING wordend.\n");
5374 /* We FAIL in one of the following cases: */
5376 /* Case 1: D is at the beginning of string. */
5377 if (AT_STRINGS_BEG (d))
5378 goto fail;
5379 else
5381 /* C1 is the character before D, S1 is the syntax of C1, C2
5382 is the character at D, and S2 is the syntax of C2. */
5383 int c1, c2, s1, s2;
5384 int pos1 = PTR_TO_OFFSET (d);
5385 int charpos;
5387 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
5388 #ifdef emacs
5389 charpos = SYNTAX_TABLE_BYTE_TO_CHAR (pos1 - 1);
5390 UPDATE_SYNTAX_TABLE (charpos);
5391 #endif
5392 s1 = SYNTAX (c1);
5394 /* Case 2: S1 is not Sword. */
5395 if (s1 != Sword)
5396 goto fail;
5398 /* Case 3: D is not at the end of string ... */
5399 if (!AT_STRINGS_END (d))
5401 PREFETCH ();
5402 c2 = STRING_CHAR (d, dend - d);
5403 #ifdef emacs
5404 UPDATE_SYNTAX_TABLE_FORWARD (charpos);
5405 #endif
5406 s2 = SYNTAX (c2);
5408 /* ... and S2 is Sword, and WORD_BOUNDARY_P (C1, C2)
5409 returns 0. */
5410 if ((s2 == Sword) && !WORD_BOUNDARY_P (c1, c2))
5411 goto fail;
5414 break;
5416 #ifdef emacs
5417 case before_dot:
5418 DEBUG_PRINT1 ("EXECUTING before_dot.\n");
5419 if (PTR_BYTE_POS (d) >= PT_BYTE)
5420 goto fail;
5421 break;
5423 case at_dot:
5424 DEBUG_PRINT1 ("EXECUTING at_dot.\n");
5425 if (PTR_BYTE_POS (d) != PT_BYTE)
5426 goto fail;
5427 break;
5429 case after_dot:
5430 DEBUG_PRINT1 ("EXECUTING after_dot.\n");
5431 if (PTR_BYTE_POS (d) <= PT_BYTE)
5432 goto fail;
5433 break;
5435 case syntaxspec:
5436 DEBUG_PRINT2 ("EXECUTING syntaxspec %d.\n", mcnt);
5437 mcnt = *p++;
5438 goto matchsyntax;
5440 case wordchar:
5441 DEBUG_PRINT1 ("EXECUTING Emacs wordchar.\n");
5442 mcnt = (int) Sword;
5443 matchsyntax:
5444 PREFETCH ();
5445 #ifdef emacs
5447 int pos1 = SYNTAX_TABLE_BYTE_TO_CHAR (PTR_TO_OFFSET (d));
5448 UPDATE_SYNTAX_TABLE (pos1);
5450 #endif
5452 int c, len;
5454 if (multibyte)
5455 /* we must concern about multibyte form, ... */
5456 c = STRING_CHAR_AND_LENGTH (d, dend - d, len);
5457 else
5458 /* everything should be handled as ASCII, even though it
5459 looks like multibyte form. */
5460 c = *d, len = 1;
5462 if (SYNTAX (c) != (enum syntaxcode) mcnt)
5463 goto fail;
5464 d += len;
5466 break;
5468 case notsyntaxspec:
5469 DEBUG_PRINT2 ("EXECUTING notsyntaxspec %d.\n", mcnt);
5470 mcnt = *p++;
5471 goto matchnotsyntax;
5473 case notwordchar:
5474 DEBUG_PRINT1 ("EXECUTING Emacs notwordchar.\n");
5475 mcnt = (int) Sword;
5476 matchnotsyntax:
5477 PREFETCH ();
5478 #ifdef emacs
5480 int pos1 = SYNTAX_TABLE_BYTE_TO_CHAR (PTR_TO_OFFSET (d));
5481 UPDATE_SYNTAX_TABLE (pos1);
5483 #endif
5485 int c, len;
5487 if (multibyte)
5488 c = STRING_CHAR_AND_LENGTH (d, dend - d, len);
5489 else
5490 c = *d, len = 1;
5492 if (SYNTAX (c) == (enum syntaxcode) mcnt)
5493 goto fail;
5494 d += len;
5496 break;
5498 case categoryspec:
5499 DEBUG_PRINT2 ("EXECUTING categoryspec %d.\n", *p);
5500 mcnt = *p++;
5501 PREFETCH ();
5503 int c, len;
5505 if (multibyte)
5506 c = STRING_CHAR_AND_LENGTH (d, dend - d, len);
5507 else
5508 c = *d, len = 1;
5510 if (!CHAR_HAS_CATEGORY (c, mcnt))
5511 goto fail;
5512 d += len;
5514 break;
5516 case notcategoryspec:
5517 DEBUG_PRINT2 ("EXECUTING notcategoryspec %d.\n", *p);
5518 mcnt = *p++;
5519 PREFETCH ();
5521 int c, len;
5523 if (multibyte)
5524 c = STRING_CHAR_AND_LENGTH (d, dend - d, len);
5525 else
5526 c = *d, len = 1;
5528 if (CHAR_HAS_CATEGORY (c, mcnt))
5529 goto fail;
5530 d += len;
5532 break;
5534 #else /* not emacs */
5535 case wordchar:
5536 DEBUG_PRINT1 ("EXECUTING non-Emacs wordchar.\n");
5537 PREFETCH ();
5538 if (!WORDCHAR_P (d))
5539 goto fail;
5540 d++;
5541 break;
5543 case notwordchar:
5544 DEBUG_PRINT1 ("EXECUTING non-Emacs notwordchar.\n");
5545 PREFETCH ();
5546 if (WORDCHAR_P (d))
5547 goto fail;
5548 d++;
5549 break;
5550 #endif /* not emacs */
5552 default:
5553 abort ();
5555 continue; /* Successfully executed one pattern command; keep going. */
5558 /* We goto here if a matching operation fails. */
5559 fail:
5560 #if defined (WINDOWSNT) && defined (emacs)
5561 QUIT;
5562 #endif
5563 if (!FAIL_STACK_EMPTY ())
5565 re_char *str;
5566 unsigned char *pat;
5567 /* A restart point is known. Restore to that state. */
5568 DEBUG_PRINT1 ("\nFAIL:\n");
5569 POP_FAILURE_POINT (str, pat);
5570 switch (SWITCH_ENUM_CAST ((re_opcode_t) *pat++))
5572 case on_failure_keep_string_jump:
5573 assert (str == NULL);
5574 goto continue_failure_jump;
5576 case on_failure_jump_exclusive:
5577 /* If something has matched, the alternative will not match,
5578 so we might as well keep popping right away. */
5579 if (0 /* d != str && d != string2 */) /* Don't bother. -sm */
5580 /* (d == string2 && str == end1) => (d =~ str) */
5581 goto fail;
5582 /* Fallthrough */
5584 case on_failure_jump_loop:
5585 case on_failure_jump:
5586 case succeed_n:
5587 d = str;
5588 continue_failure_jump:
5589 EXTRACT_NUMBER_AND_INCR (mcnt, pat);
5590 p = pat + mcnt;
5591 break;
5593 default:
5594 abort();
5597 assert (p >= bufp->buffer && p <= pend);
5599 if (d >= string1 && d <= end1)
5600 dend = end_match_1;
5602 else
5603 break; /* Matching at this starting point really fails. */
5604 } /* for (;;) */
5606 if (best_regs_set)
5607 goto restore_best_regs;
5609 FREE_VARIABLES ();
5611 return -1; /* Failure to match. */
5612 } /* re_match_2 */
5614 /* Subroutine definitions for re_match_2. */
5616 /* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
5617 bytes; nonzero otherwise. */
5619 static int
5620 bcmp_translate (s1, s2, len, translate)
5621 unsigned char *s1, *s2;
5622 register int len;
5623 RE_TRANSLATE_TYPE translate;
5625 register unsigned char *p1 = s1, *p2 = s2;
5626 unsigned char *p1_end = s1 + len;
5627 unsigned char *p2_end = s2 + len;
5629 while (p1 != p1_end && p2 != p2_end)
5631 int p1_charlen, p2_charlen;
5632 int p1_ch, p2_ch;
5634 p1_ch = STRING_CHAR_AND_LENGTH (p1, p1_end - p1, p1_charlen);
5635 p2_ch = STRING_CHAR_AND_LENGTH (p2, p2_end - p2, p2_charlen);
5637 if (RE_TRANSLATE (translate, p1_ch)
5638 != RE_TRANSLATE (translate, p2_ch))
5639 return 1;
5641 p1 += p1_charlen, p2 += p2_charlen;
5644 if (p1 != p1_end || p2 != p2_end)
5645 return 1;
5647 return 0;
5650 /* Entry points for GNU code. */
5652 /* re_compile_pattern is the GNU regular expression compiler: it
5653 compiles PATTERN (of length SIZE) and puts the result in BUFP.
5654 Returns 0 if the pattern was valid, otherwise an error string.
5656 Assumes the `allocated' (and perhaps `buffer') and `translate' fields
5657 are set in BUFP on entry.
5659 We call regex_compile to do the actual compilation. */
5661 const char *
5662 re_compile_pattern (pattern, length, bufp)
5663 const char *pattern;
5664 int length;
5665 struct re_pattern_buffer *bufp;
5667 reg_errcode_t ret;
5669 /* GNU code is written to assume at least RE_NREGS registers will be set
5670 (and at least one extra will be -1). */
5671 bufp->regs_allocated = REGS_UNALLOCATED;
5673 /* And GNU code determines whether or not to get register information
5674 by passing null for the REGS argument to re_match, etc., not by
5675 setting no_sub. */
5676 bufp->no_sub = 0;
5678 /* Match anchors at newline. */
5679 bufp->newline_anchor = 1;
5681 ret = regex_compile (pattern, length, re_syntax_options, bufp);
5683 if (!ret)
5684 return NULL;
5685 return gettext (re_error_msgid[(int) ret]);
5688 /* Entry points compatible with 4.2 BSD regex library. We don't define
5689 them unless specifically requested. */
5691 #if defined (_REGEX_RE_COMP) || defined (_LIBC)
5693 /* BSD has one and only one pattern buffer. */
5694 static struct re_pattern_buffer re_comp_buf;
5696 char *
5697 #ifdef _LIBC
5698 /* Make these definitions weak in libc, so POSIX programs can redefine
5699 these names if they don't use our functions, and still use
5700 regcomp/regexec below without link errors. */
5701 weak_function
5702 #endif
5703 re_comp (s)
5704 const char *s;
5706 reg_errcode_t ret;
5708 if (!s)
5710 if (!re_comp_buf.buffer)
5711 return gettext ("No previous regular expression");
5712 return 0;
5715 if (!re_comp_buf.buffer)
5717 re_comp_buf.buffer = (unsigned char *) malloc (200);
5718 if (re_comp_buf.buffer == NULL)
5719 return gettext (re_error_msgid[(int) REG_ESPACE]);
5720 re_comp_buf.allocated = 200;
5722 re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH);
5723 if (re_comp_buf.fastmap == NULL)
5724 return gettext (re_error_msgid[(int) REG_ESPACE]);
5727 /* Since `re_exec' always passes NULL for the `regs' argument, we
5728 don't need to initialize the pattern buffer fields which affect it. */
5730 /* Match anchors at newlines. */
5731 re_comp_buf.newline_anchor = 1;
5733 ret = regex_compile (s, strlen (s), re_syntax_options, &re_comp_buf);
5735 if (!ret)
5736 return NULL;
5738 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
5739 return (char *) gettext (re_error_msgid[(int) ret]);
5744 #ifdef _LIBC
5745 weak_function
5746 #endif
5747 re_exec (s)
5748 const char *s;
5750 const int len = strlen (s);
5751 return
5752 0 <= re_search (&re_comp_buf, s, len, 0, len, (struct re_registers *) 0);
5754 #endif /* _REGEX_RE_COMP */
5756 /* POSIX.2 functions. Don't define these for Emacs. */
5758 #ifndef emacs
5760 /* regcomp takes a regular expression as a string and compiles it.
5762 PREG is a regex_t *. We do not expect any fields to be initialized,
5763 since POSIX says we shouldn't. Thus, we set
5765 `buffer' to the compiled pattern;
5766 `used' to the length of the compiled pattern;
5767 `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
5768 REG_EXTENDED bit in CFLAGS is set; otherwise, to
5769 RE_SYNTAX_POSIX_BASIC;
5770 `newline_anchor' to REG_NEWLINE being set in CFLAGS;
5771 `fastmap' and `fastmap_accurate' to zero;
5772 `re_nsub' to the number of subexpressions in PATTERN.
5774 PATTERN is the address of the pattern string.
5776 CFLAGS is a series of bits which affect compilation.
5778 If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
5779 use POSIX basic syntax.
5781 If REG_NEWLINE is set, then . and [^...] don't match newline.
5782 Also, regexec will try a match beginning after every newline.
5784 If REG_ICASE is set, then we considers upper- and lowercase
5785 versions of letters to be equivalent when matching.
5787 If REG_NOSUB is set, then when PREG is passed to regexec, that
5788 routine will report only success or failure, and nothing about the
5789 registers.
5791 It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
5792 the return codes and their meanings.) */
5795 regcomp (preg, pattern, cflags)
5796 regex_t *preg;
5797 const char *pattern;
5798 int cflags;
5800 reg_errcode_t ret;
5801 unsigned syntax
5802 = (cflags & REG_EXTENDED) ?
5803 RE_SYNTAX_POSIX_EXTENDED : RE_SYNTAX_POSIX_BASIC;
5805 /* regex_compile will allocate the space for the compiled pattern. */
5806 preg->buffer = 0;
5807 preg->allocated = 0;
5808 preg->used = 0;
5810 /* Don't bother to use a fastmap when searching. This simplifies the
5811 REG_NEWLINE case: if we used a fastmap, we'd have to put all the
5812 characters after newlines into the fastmap. This way, we just try
5813 every character. */
5814 preg->fastmap = 0;
5816 if (cflags & REG_ICASE)
5818 unsigned i;
5820 preg->translate
5821 = (RE_TRANSLATE_TYPE) malloc (CHAR_SET_SIZE
5822 * sizeof (*(RE_TRANSLATE_TYPE)0));
5823 if (preg->translate == NULL)
5824 return (int) REG_ESPACE;
5826 /* Map uppercase characters to corresponding lowercase ones. */
5827 for (i = 0; i < CHAR_SET_SIZE; i++)
5828 preg->translate[i] = ISUPPER (i) ? tolower (i) : i;
5830 else
5831 preg->translate = NULL;
5833 /* If REG_NEWLINE is set, newlines are treated differently. */
5834 if (cflags & REG_NEWLINE)
5835 { /* REG_NEWLINE implies neither . nor [^...] match newline. */
5836 syntax &= ~RE_DOT_NEWLINE;
5837 syntax |= RE_HAT_LISTS_NOT_NEWLINE;
5838 /* It also changes the matching behavior. */
5839 preg->newline_anchor = 1;
5841 else
5842 preg->newline_anchor = 0;
5844 preg->no_sub = !!(cflags & REG_NOSUB);
5846 /* POSIX says a null character in the pattern terminates it, so we
5847 can use strlen here in compiling the pattern. */
5848 ret = regex_compile (pattern, strlen (pattern), syntax, preg);
5850 /* POSIX doesn't distinguish between an unmatched open-group and an
5851 unmatched close-group: both are REG_EPAREN. */
5852 if (ret == REG_ERPAREN) ret = REG_EPAREN;
5854 return (int) ret;
5858 /* regexec searches for a given pattern, specified by PREG, in the
5859 string STRING.
5861 If NMATCH is zero or REG_NOSUB was set in the cflags argument to
5862 `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
5863 least NMATCH elements, and we set them to the offsets of the
5864 corresponding matched substrings.
5866 EFLAGS specifies `execution flags' which affect matching: if
5867 REG_NOTBOL is set, then ^ does not match at the beginning of the
5868 string; if REG_NOTEOL is set, then $ does not match at the end.
5870 We return 0 if we find a match and REG_NOMATCH if not. */
5873 regexec (preg, string, nmatch, pmatch, eflags)
5874 const regex_t *preg;
5875 const char *string;
5876 size_t nmatch;
5877 regmatch_t pmatch[];
5878 int eflags;
5880 int ret;
5881 struct re_registers regs;
5882 regex_t private_preg;
5883 int len = strlen (string);
5884 boolean want_reg_info = !preg->no_sub && nmatch > 0;
5886 private_preg = *preg;
5888 private_preg.not_bol = !!(eflags & REG_NOTBOL);
5889 private_preg.not_eol = !!(eflags & REG_NOTEOL);
5891 /* The user has told us exactly how many registers to return
5892 information about, via `nmatch'. We have to pass that on to the
5893 matching routines. */
5894 private_preg.regs_allocated = REGS_FIXED;
5896 if (want_reg_info)
5898 regs.num_regs = nmatch;
5899 regs.start = TALLOC (nmatch, regoff_t);
5900 regs.end = TALLOC (nmatch, regoff_t);
5901 if (regs.start == NULL || regs.end == NULL)
5902 return (int) REG_NOMATCH;
5905 /* Perform the searching operation. */
5906 ret = re_search (&private_preg, string, len,
5907 /* start: */ 0, /* range: */ len,
5908 want_reg_info ? &regs : (struct re_registers *) 0);
5910 /* Copy the register information to the POSIX structure. */
5911 if (want_reg_info)
5913 if (ret >= 0)
5915 unsigned r;
5917 for (r = 0; r < nmatch; r++)
5919 pmatch[r].rm_so = regs.start[r];
5920 pmatch[r].rm_eo = regs.end[r];
5924 /* If we needed the temporary register info, free the space now. */
5925 free (regs.start);
5926 free (regs.end);
5929 /* We want zero return to mean success, unlike `re_search'. */
5930 return ret >= 0 ? (int) REG_NOERROR : (int) REG_NOMATCH;
5934 /* Returns a message corresponding to an error code, ERRCODE, returned
5935 from either regcomp or regexec. We don't use PREG here. */
5937 size_t
5938 regerror (errcode, preg, errbuf, errbuf_size)
5939 int errcode;
5940 const regex_t *preg;
5941 char *errbuf;
5942 size_t errbuf_size;
5944 const char *msg;
5945 size_t msg_size;
5947 if (errcode < 0
5948 || errcode >= (sizeof (re_error_msgid) / sizeof (re_error_msgid[0])))
5949 /* Only error codes returned by the rest of the code should be passed
5950 to this routine. If we are given anything else, or if other regex
5951 code generates an invalid error code, then the program has a bug.
5952 Dump core so we can fix it. */
5953 abort ();
5955 msg = gettext (re_error_msgid[errcode]);
5957 msg_size = strlen (msg) + 1; /* Includes the null. */
5959 if (errbuf_size != 0)
5961 if (msg_size > errbuf_size)
5963 strncpy (errbuf, msg, errbuf_size - 1);
5964 errbuf[errbuf_size - 1] = 0;
5966 else
5967 strcpy (errbuf, msg);
5970 return msg_size;
5974 /* Free dynamically allocated space used by PREG. */
5976 void
5977 regfree (preg)
5978 regex_t *preg;
5980 if (preg->buffer != NULL)
5981 free (preg->buffer);
5982 preg->buffer = NULL;
5984 preg->allocated = 0;
5985 preg->used = 0;
5987 if (preg->fastmap != NULL)
5988 free (preg->fastmap);
5989 preg->fastmap = NULL;
5990 preg->fastmap_accurate = 0;
5992 if (preg->translate != NULL)
5993 free (preg->translate);
5994 preg->translate = NULL;
5997 #endif /* not emacs */