5 /* Extended regular expression matching and search library,
7 * (Implements POSIX draft P10003.2/D11.2, except for
8 * internationalization features.)
10 * Copyright (C) 1993 Free Software Foundation, Inc.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. */
26 /* AIX requires this to be the first thing in the file. */
27 #if defined (_AIX) && !defined(REGEX_MALLOC)
37 #if USE_GNUREGEX /* only if squid needs it. Usually not */
40 #define REGEX_MALLOC 1
43 /* We used to test for `BSTRING' here, but only GCC and Emacs define
44 * `BSTRING', as far as I know, and neither of them use this code. */
45 #if HAVE_STRING_H || STDC_HEADERS
51 /* Define the syntax stuff for \<, \>, etc. */
53 /* This must be nonzero for the wordchar and notwordchar pattern
54 * commands in re_match_2. */
61 extern char *re_syntax_table
;
63 #else /* not SYNTAX_TABLE */
65 /* How many characters in the character set. */
66 #define CHAR_SET_SIZE 256
68 static char re_syntax_table
[CHAR_SET_SIZE
];
71 init_syntax_once(void)
79 memset(re_syntax_table
, 0, sizeof re_syntax_table
);
81 for (c
= 'a'; c
<= 'z'; c
++)
82 re_syntax_table
[c
] = Sword
;
84 for (c
= 'A'; c
<= 'Z'; c
++)
85 re_syntax_table
[c
] = Sword
;
87 for (c
= '0'; c
<= '9'; c
++)
88 re_syntax_table
[c
] = Sword
;
90 re_syntax_table
['_'] = Sword
;
95 #endif /* not SYNTAX_TABLE */
97 #define SYNTAX(c) re_syntax_table[c]
99 /* Get the interface, including the syntax bits. */
100 #include "compat/GnuRegex.h"
102 /* Compile a fastmap for the compiled pattern in BUFFER; used to
103 * accelerate searches. Return 0 if successful and -2 if was an
105 static int re_compile_fastmap(struct re_pattern_buffer
* buffer
);
107 /* Search in the string STRING (with length LENGTH) for the pattern
108 * compiled into BUFFER. Start searching at position START, for RANGE
109 * characters. Return the starting position of the match, -1 for no
110 * match, or -2 for an internal error. Also return register
111 * information in REGS (if REGS and BUFFER->no_sub are nonzero). */
112 static int re_search(struct re_pattern_buffer
* buffer
, const char *string
,
113 int length
, int start
, int range
, struct re_registers
* regs
);
115 /* Like `re_search', but search in the concatenation of STRING1 and
116 * STRING2. Also, stop searching at index START + STOP. */
117 static int re_search_2(struct re_pattern_buffer
* buffer
, const char *string1
,
118 int length1
, const char *string2
, int length2
,
119 int start
, int range
, struct re_registers
* regs
, int stop
);
121 /* Like `re_search_2', but return how many characters in STRING the regexp
122 * in BUFFER matched, starting at position START. */
123 static int re_match_2(struct re_pattern_buffer
* buffer
, const char *string1
,
124 int length1
, const char *string2
, int length2
,
125 int start
, struct re_registers
* regs
, int stop
);
127 /* isalpha etc. are used for the character classes. */
135 #define ISBLANK(c) (isascii ((unsigned char)c) && isblank ((unsigned char)c))
137 #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
140 #define ISGRAPH(c) (isascii ((unsigned char)c) && isgraph ((unsigned char)c))
142 #define ISGRAPH(c) (isascii ((unsigned char)c) && isprint ((unsigned char)c) && !isspace ((unsigned char)c))
145 #define ISPRINT(c) (isascii ((unsigned char)c) && isprint ((unsigned char)c))
146 #define ISDIGIT(c) (isascii ((unsigned char)c) && isdigit ((unsigned char)c))
147 #define ISALNUM(c) (isascii ((unsigned char)c) && isalnum ((unsigned char)c))
148 #define ISALPHA(c) (isascii ((unsigned char)c) && isalpha ((unsigned char)c))
149 #define ISCNTRL(c) (isascii ((unsigned char)c) && iscntrl ((unsigned char)c))
150 #define ISLOWER(c) (isascii ((unsigned char)c) && islower ((unsigned char)c))
151 #define ISPUNCT(c) (isascii ((unsigned char)c) && ispunct ((unsigned char)c))
152 #define ISSPACE(c) (isascii ((unsigned char)c) && isspace ((unsigned char)c))
153 #define ISUPPER(c) (isascii ((unsigned char)c) && isupper ((unsigned char)c))
154 #define ISXDIGIT(c) (isascii ((unsigned char)c) && isxdigit ((unsigned char)c))
156 /* We remove any previous definition of `SIGN_EXTEND_CHAR',
157 * since ours (we hope) works properly with all combinations of
158 * machines, compilers, `char' and `unsigned char' argument types.
159 * (Per Bothner suggested the basic approach.) */
160 #undef SIGN_EXTEND_CHAR
162 #define SIGN_EXTEND_CHAR(c) ((signed char) (c))
163 #else /* not __STDC__ */
164 /* As in Harbison and Steele. */
165 #define SIGN_EXTEND_CHAR(c) ((((unsigned char) (c)) ^ 128) - 128)
168 /* Should we use malloc or alloca? If REGEX_MALLOC is not defined, we
169 * use `alloca' instead of `malloc'. This is because using malloc in
170 * re_search* or re_match* could cause memory leaks when C-g is used in
171 * Emacs; also, malloc is slower and causes storage fragmentation. On
172 * the other hand, malloc is more portable, and easier to debug.
174 * Because we sometimes use alloca, some routines have to be macros,
175 * not functions -- `alloca'-allocated space disappears at the end of the
176 * function it is called in. */
180 #define REGEX_ALLOCATE malloc
181 #define REGEX_REALLOCATE(source, osize, nsize) realloc (source, nsize)
183 #else /* not REGEX_MALLOC */
185 /* Emacs already defines alloca, sometimes. */
188 /* Make alloca work the best possible way. */
190 #define alloca __builtin_alloca
191 #else /* not __GNUC__ */
194 #else /* not __GNUC__ or HAVE_ALLOCA_H */
195 #ifndef _AIX /* Already did AIX, up at the top. */
197 #endif /* not _AIX */
198 #endif /* not HAVE_ALLOCA_H */
199 #endif /* not __GNUC__ */
201 #endif /* not alloca */
203 #define REGEX_ALLOCATE alloca
205 /* Assumes a `char *destination' variable. */
206 #define REGEX_REALLOCATE(source, osize, nsize) \
207 (destination = (char *) alloca (nsize), \
208 memcpy (destination, source, osize), \
211 #endif /* not REGEX_MALLOC */
213 /* True if `size1' is non-NULL and PTR is pointing anywhere inside
214 * `string1' or just past its end. This works if PTR is NULL, which is
216 #define FIRST_STRING_P(ptr) \
217 (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
219 /* (Re)Allocate N items of type T using malloc, or fail. */
220 #define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t)))
221 #define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
222 #define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
224 #define BYTEWIDTH 8 /* In bits. */
226 #define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
228 #if !defined(__MINGW32__) /* MinGW defines boolean */
229 typedef char boolean
;
234 /* These are the command codes that appear in compiled regular
235 * expressions. Some opcodes are followed by argument bytes. A
236 * command code can specify any interpretation whatsoever for its
237 * arguments. Zero bytes may appear in the compiled regular expression.
239 * The value of `exactn' is needed in search.c (search_buffer) in Emacs.
240 * So regex.h defines a symbol `RE_EXACTN_VALUE' to be 1; the value of
241 * `exactn' we use here must also be 1. */
246 /* Followed by one byte giving n, then by n literal bytes. */
249 /* Matches any (more or less) character. */
252 /* Matches any one char belonging to specified set. First
253 * following byte is number of bitmap bytes. Then come bytes
254 * for a bitmap saying which chars are in. Bits in each byte
255 * are ordered low-bit-first. A character is in the set if its
256 * bit is 1. A character too large to have a bit in the map is
257 * automatically not in the set. */
260 /* Same parameters as charset, but match any character that is
261 * not one of those specified. */
264 /* Start remembering the text that is matched, for storing in a
265 * register. Followed by one byte with the register number, in
266 * the range 0 to one less than the pattern buffer's re_nsub
267 * field. Then followed by one byte with the number of groups
268 * inner to this one. (This last has to be part of the
269 * start_memory only because we need it in the on_failure_jump
273 /* Stop remembering the text that is matched and store it in a
274 * memory register. Followed by one byte with the register
275 * number, in the range 0 to one less than `re_nsub' in the
276 * pattern buffer, and one byte with the number of inner groups,
277 * just like `start_memory'. (We need the number of inner
278 * groups here because we don't have any easy way of finding the
279 * corresponding start_memory when we're at a stop_memory.) */
282 /* Match a duplicate of something remembered. Followed by one
283 * byte containing the register number. */
286 /* Fail unless at beginning of line. */
289 /* Fail unless at end of line. */
292 /* Succeeds if or at beginning of string to be matched. */
295 /* Analogously, for end of buffer/string. */
298 /* Followed by two byte relative address to which to jump. */
301 /* Same as jump, but marks the end of an alternative. */
304 /* Followed by two-byte relative address of place to resume at
305 * in case of failure. */
308 /* Like on_failure_jump, but pushes a placeholder instead of the
309 * current string position when executed. */
310 on_failure_keep_string_jump
,
312 /* Throw away latest failure point and then jump to following
313 * two-byte relative address. */
316 /* Change to pop_failure_jump if know won't have to backtrack to
317 * match; otherwise change to jump. This is used to jump
318 * back to the beginning of a repeat. If what follows this jump
319 * clearly won't match what the repeat does, such that we can be
320 * sure that there is no use backtracking out of repetitions
321 * already matched, then we change it to a pop_failure_jump.
322 * Followed by two-byte address. */
325 /* Jump to following two-byte address, and push a dummy failure
326 * point. This failure point will be thrown away if an attempt
327 * is made to use it for a failure. A `+' construct makes this
328 * before the first repeat. Also used as an intermediary kind
329 * of jump when compiling an alternative. */
332 /* Push a dummy failure point and continue. Used at the end of
336 /* Followed by two-byte relative address and two-byte number n.
337 * After matching N times, jump to the address upon failure. */
340 /* Followed by two-byte relative address, and two-byte number n.
341 * Jump to the address N times, then fail. */
344 /* Set the following two-byte relative address to the
345 * subsequent two-byte number. The address *includes* the two
346 * bytes of number. */
349 wordchar
, /* Matches any word-constituent character. */
350 notwordchar
, /* Matches any char that is not a word-constituent. */
352 wordbeg
, /* Succeeds if at word beginning. */
353 wordend
, /* Succeeds if at word end. */
355 wordbound
, /* Succeeds if at a word boundary. */
356 notwordbound
/* Succeeds if not at a word boundary. */
360 /* Common operations on the compiled pattern. */
362 /* Store NUMBER in two contiguous bytes starting at DESTINATION. */
364 #define STORE_NUMBER(destination, number) \
366 (destination)[0] = (number) & 0377; \
367 (destination)[1] = (number) >> 8; \
370 /* Same as STORE_NUMBER, except increment DESTINATION to
371 * the byte after where the number is stored. Therefore, DESTINATION
372 * must be an lvalue. */
374 #define STORE_NUMBER_AND_INCR(destination, number) \
376 STORE_NUMBER (destination, number); \
377 (destination) += 2; \
380 /* Put into DESTINATION a number stored in two contiguous bytes starting
383 #define EXTRACT_NUMBER(destination, source) \
385 (destination) = *(source) & 0377; \
386 (destination) += SIGN_EXTEND_CHAR (*((source) + 1)) << 8; \
391 extract_number(dest
, source
)
393 unsigned char *source
;
395 int temp
= SIGN_EXTEND_CHAR(*(source
+ 1));
396 *dest
= *source
& 0377;
400 #ifndef EXTRACT_MACROS /* To debug the macros. */
401 #undef EXTRACT_NUMBER
402 #define EXTRACT_NUMBER(dest, src) extract_number (&dest, src)
403 #endif /* not EXTRACT_MACROS */
407 /* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
408 * SOURCE must be an lvalue. */
410 #define EXTRACT_NUMBER_AND_INCR(destination, source) \
412 EXTRACT_NUMBER (destination, source); \
418 extract_number_and_incr(destination
, source
)
420 unsigned char **source
;
422 extract_number(destination
, *source
);
426 #ifndef EXTRACT_MACROS
427 #undef EXTRACT_NUMBER_AND_INCR
428 #define EXTRACT_NUMBER_AND_INCR(dest, src) \
429 extract_number_and_incr (&dest, &src)
430 #endif /* not EXTRACT_MACROS */
434 /* If DEBUG is defined, Regex prints many voluminous messages about what
435 * it is doing (if the variable `debug' is nonzero). If linked with the
436 * main program in `iregex.c', you can enter patterns and strings
437 * interactively. And if linked with the main program in `main.c' and
438 * the other test files, you can run the already-written tests. */
442 static int debug
= 0;
444 #define DEBUG_STATEMENT(e) e
445 #define DEBUG_PRINT1(x) if (debug) printf (x)
446 #define DEBUG_PRINT2(x1, x2) if (debug) printf (x1, x2)
447 #define DEBUG_PRINT3(x1, x2, x3) if (debug) printf (x1, x2, x3)
448 #define DEBUG_PRINT4(x1, x2, x3, x4) if (debug) printf (x1, x2, x3, x4)
449 #define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) \
450 if (debug) print_partial_compiled_pattern (s, e)
451 #define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \
452 if (debug) print_double_string (w, s1, sz1, s2, sz2)
454 extern void printchar();
456 /* Print the fastmap in human-readable form. */
459 print_fastmap(fastmap
)
462 unsigned was_a_range
= 0;
465 while (i
< (1 << BYTEWIDTH
)) {
469 while (i
< (1 << BYTEWIDTH
) && fastmap
[i
]) {
482 /* Print a compiled pattern string in human-readable form, starting at
483 * the START pointer into it and ending just before the pointer END. */
486 print_partial_compiled_pattern(start
, end
)
487 unsigned char *start
;
491 unsigned char *p
= start
;
492 unsigned char *pend
= end
;
498 /* Loop over pattern commands. */
500 switch ((re_opcode_t
) * p
++) {
507 printf("/exactn/%d", mcnt
);
516 printf("/start_memory/%d/%d", mcnt
, *p
++);
521 printf("/stop_memory/%d/%d", mcnt
, *p
++);
525 printf("/duplicate/%d", *p
++);
537 (re_opcode_t
) * (p
- 1) == charset_not
? "_not" : "");
539 assert(p
+ *p
< pend
);
541 for (c
= 0; c
< *p
; c
++) {
543 unsigned char map_byte
= p
[1 + c
];
547 for (bit
= 0; bit
< BYTEWIDTH
; bit
++)
548 if (map_byte
& (1 << bit
))
549 printchar(c
* BYTEWIDTH
+ bit
);
563 case on_failure_jump
:
564 extract_number_and_incr(&mcnt
, &p
);
565 printf("/on_failure_jump/0/%d", mcnt
);
568 case on_failure_keep_string_jump
:
569 extract_number_and_incr(&mcnt
, &p
);
570 printf("/on_failure_keep_string_jump/0/%d", mcnt
);
573 case dummy_failure_jump
:
574 extract_number_and_incr(&mcnt
, &p
);
575 printf("/dummy_failure_jump/0/%d", mcnt
);
578 case push_dummy_failure
:
579 printf("/push_dummy_failure");
583 extract_number_and_incr(&mcnt
, &p
);
584 printf("/maybe_pop_jump/0/%d", mcnt
);
587 case pop_failure_jump
:
588 extract_number_and_incr(&mcnt
, &p
);
589 printf("/pop_failure_jump/0/%d", mcnt
);
593 extract_number_and_incr(&mcnt
, &p
);
594 printf("/jump_past_alt/0/%d", mcnt
);
598 extract_number_and_incr(&mcnt
, &p
);
599 printf("/jump/0/%d", mcnt
);
603 extract_number_and_incr(&mcnt
, &p
);
604 extract_number_and_incr(&mcnt2
, &p
);
605 printf("/succeed_n/0/%d/0/%d", mcnt
, mcnt2
);
609 extract_number_and_incr(&mcnt
, &p
);
610 extract_number_and_incr(&mcnt2
, &p
);
611 printf("/jump_n/0/%d/0/%d", mcnt
, mcnt2
);
615 extract_number_and_incr(&mcnt
, &p
);
616 extract_number_and_incr(&mcnt2
, &p
);
617 printf("/set_number_at/0/%d/0/%d", mcnt
, mcnt2
);
621 printf("/wordbound");
625 printf("/notwordbound");
640 printf("/notwordchar");
652 printf("?%d", *(p
- 1));
659 print_compiled_pattern(bufp
)
660 struct re_pattern_buffer
*bufp
;
662 unsigned char *buffer
= bufp
->buffer
;
664 print_partial_compiled_pattern(buffer
, buffer
+ bufp
->used
);
665 printf("%d bytes used/%d bytes allocated.\n", bufp
->used
, bufp
->allocated
);
667 if (bufp
->fastmap_accurate
&& bufp
->fastmap
) {
669 print_fastmap(bufp
->fastmap
);
671 printf("re_nsub: %d\t", bufp
->re_nsub
);
672 printf("regs_alloc: %d\t", bufp
->regs_allocated
);
673 printf("can_be_null: %d\t", bufp
->can_be_null
);
674 printf("newline_anchor: %d\n", bufp
->newline_anchor
);
675 printf("no_sub: %d\t", bufp
->no_sub
);
676 printf("not_bol: %d\t", bufp
->not_bol
);
677 printf("not_eol: %d\t", bufp
->not_eol
);
678 printf("syntax: %d\n", bufp
->syntax
);
679 /* Perhaps we should print the translate table? */
683 print_double_string(where
, string1
, size1
, string2
, size2
)
695 if (FIRST_STRING_P(where
)) {
696 for (this_char
= where
- string1
; this_char
< size1
; this_char
++)
697 printchar(string1
[this_char
]);
701 for (this_char
= where
- string2
; this_char
< size2
; this_char
++)
702 printchar(string2
[this_char
]);
706 #else /* not DEBUG */
711 #define DEBUG_STATEMENT(e)
712 #define DEBUG_PRINT1(x)
713 #define DEBUG_PRINT2(x1, x2)
714 #define DEBUG_PRINT3(x1, x2, x3)
715 #define DEBUG_PRINT4(x1, x2, x3, x4)
716 #define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
717 #define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
719 #endif /* not DEBUG */
721 /* This table gives an error message for each of the error codes listed
722 * in regex.h. Obviously the order here has to be same as there. */
724 static const char *re_error_msg
[] = {NULL
, /* REG_NOERROR */
725 "No match", /* REG_NOMATCH */
726 "Invalid regular expression", /* REG_BADPAT */
727 "Invalid collation character", /* REG_ECOLLATE */
728 "Invalid character class name", /* REG_ECTYPE */
729 "Trailing backslash", /* REG_EESCAPE */
730 "Invalid back reference", /* REG_ESUBREG */
731 "Unmatched [ or [^", /* REG_EBRACK */
732 "Unmatched ( or \\(", /* REG_EPAREN */
733 "Unmatched \\{", /* REG_EBRACE */
734 "Invalid content of \\{\\}", /* REG_BADBR */
735 "Invalid range end", /* REG_ERANGE */
736 "Memory exhausted", /* REG_ESPACE */
737 "Invalid preceding regular expression", /* REG_BADRPT */
738 "Premature end of regular expression", /* REG_EEND */
739 "Regular expression too big", /* REG_ESIZE */
740 "Unmatched ) or \\)", /* REG_ERPAREN */
743 /* Subroutine declarations and macros for regex_compile. */
745 /* Fetch the next character in the uncompiled pattern---translating it
746 * if necessary. Also cast from a signed character in the constant
747 * string passed to us by the user to an unsigned char that we can use
748 * as an array index (in, e.g., `translate'). */
749 #define PATFETCH(c) \
750 do {if (p == pend) return REG_EEND; \
751 c = (unsigned char) *p++; \
752 if (translate) c = translate[c]; \
755 /* Fetch the next character in the uncompiled pattern, with no
757 #define PATFETCH_RAW(c) \
758 do {if (p == pend) return REG_EEND; \
759 c = (unsigned char) *p++; \
762 /* Go backwards one character in the pattern. */
763 #define PATUNFETCH p--
765 /* If `translate' is non-null, return translate[D], else just D. We
766 * cast the subscript to translate because some data is declared as
767 * `char *', to avoid warnings when a string constant is passed. But
768 * when we use a character as a subscript we must make it unsigned. */
769 #define TRANSLATE(d) (translate ? translate[(unsigned char) (d)] : (d))
771 /* Macros for outputting the compiled pattern into `buffer'. */
773 /* If the buffer isn't allocated when it comes in, use this. */
774 #define INIT_BUF_SIZE 32
776 /* Make sure we have at least N more bytes of space in buffer. */
777 #define GET_BUFFER_SPACE(n) \
778 while (b - bufp->buffer + (n) > bufp->allocated) \
781 /* Make sure we have one more byte of buffer space and then add C to it. */
782 #define BUF_PUSH(c) \
784 GET_BUFFER_SPACE (1); \
785 *b++ = (unsigned char) (c); \
788 /* Ensure we have two more bytes of buffer space and then append C1 and C2. */
789 #define BUF_PUSH_2(c1, c2) \
791 GET_BUFFER_SPACE (2); \
792 *b++ = (unsigned char) (c1); \
793 *b++ = (unsigned char) (c2); \
796 /* As with BUF_PUSH_2, except for three bytes. */
797 #define BUF_PUSH_3(c1, c2, c3) \
799 GET_BUFFER_SPACE (3); \
800 *b++ = (unsigned char) (c1); \
801 *b++ = (unsigned char) (c2); \
802 *b++ = (unsigned char) (c3); \
805 /* Store a jump with opcode OP at LOC to location TO. We store a
806 * relative address offset by the three bytes the jump itself occupies. */
807 #define STORE_JUMP(op, loc, to) \
808 store_op1 (op, loc, (to) - (loc) - 3)
810 /* Likewise, for a two-argument jump. */
811 #define STORE_JUMP2(op, loc, to, arg) \
812 store_op2 (op, loc, (to) - (loc) - 3, arg)
814 /* Like `STORE_JUMP', but for inserting. Assume `b' is the buffer end. */
815 #define INSERT_JUMP(op, loc, to) \
816 insert_op1 (op, loc, (to) - (loc) - 3, b)
818 /* Like `STORE_JUMP2', but for inserting. Assume `b' is the buffer end. */
819 #define INSERT_JUMP2(op, loc, to, arg) \
820 insert_op2 (op, loc, (to) - (loc) - 3, arg, b)
822 /* This is not an arbitrary limit: the arguments which represent offsets
823 * into the pattern are two bytes long. So if 2^16 bytes turns out to
824 * be too small, many things would have to change. */
825 #define MAX_BUF_SIZE (1L << 16)
827 /* Extend the buffer by twice its current size via realloc and
828 * reset the pointers that pointed into the old block to point to the
829 * correct places in the new one. If extending the buffer results in it
830 * being larger than MAX_BUF_SIZE, then flag memory exhausted. */
831 #define EXTEND_BUFFER() \
833 unsigned char *old_buffer = bufp->buffer; \
834 if (bufp->allocated == MAX_BUF_SIZE) \
836 bufp->allocated <<= 1; \
837 if (bufp->allocated > MAX_BUF_SIZE) \
838 bufp->allocated = MAX_BUF_SIZE; \
839 bufp->buffer = (unsigned char *) realloc (bufp->buffer, bufp->allocated);\
840 if (bufp->buffer == NULL) \
842 /* If the buffer moved, move all the pointers into it. */ \
843 if (old_buffer != bufp->buffer) \
845 b = (b - old_buffer) + bufp->buffer; \
846 begalt = (begalt - old_buffer) + bufp->buffer; \
847 if (fixup_alt_jump) \
848 fixup_alt_jump = (fixup_alt_jump - old_buffer) + bufp->buffer;\
850 laststart = (laststart - old_buffer) + bufp->buffer; \
852 pending_exact = (pending_exact - old_buffer) + bufp->buffer; \
856 /* Since we have one byte reserved for the register number argument to
857 * {start,stop}_memory, the maximum number of groups we can report
858 * things about is what fits in that byte. */
859 #define MAX_REGNUM 255
861 /* But patterns can have more than `MAX_REGNUM' registers. We just
862 * ignore the excess. */
863 typedef unsigned regnum_t
;
865 /* Macros for the compile stack. */
867 /* Since offsets can go either forwards or backwards, this type needs to
868 * be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1. */
869 typedef int pattern_offset_t
;
872 pattern_offset_t begalt_offset
;
873 pattern_offset_t fixup_alt_jump
;
874 pattern_offset_t inner_group_offset
;
875 pattern_offset_t laststart_offset
;
877 } compile_stack_elt_t
;
880 compile_stack_elt_t
*stack
;
882 unsigned avail
; /* Offset of next open position. */
883 } compile_stack_type
;
885 static void store_op1(re_opcode_t op
, unsigned char *loc
, int arg
);
886 static void store_op2( re_opcode_t op
, unsigned char *loc
, int arg1
, int arg2
);
887 static void insert_op1(re_opcode_t op
, unsigned char *loc
, int arg
, unsigned char *end
);
888 static void insert_op2(re_opcode_t op
, unsigned char *loc
, int arg1
, int arg2
, unsigned char *end
);
889 static boolean
at_begline_loc_p(const char * pattern
, const char *p
, reg_syntax_t syntax
);
890 static boolean
at_endline_loc_p(const char *p
, const char *pend
, int syntax
);
891 static boolean
group_in_compile_stack(compile_stack_type compile_stack
, regnum_t regnum
);
892 static reg_errcode_t
compile_range(const char **p_ptr
, const char *pend
, char *translate
, reg_syntax_t syntax
, unsigned char *b
);
894 #define INIT_COMPILE_STACK_SIZE 32
896 #define COMPILE_STACK_EMPTY (compile_stack.avail == 0)
897 #define COMPILE_STACK_FULL (compile_stack.avail == compile_stack.size)
899 /* The next available element. */
900 #define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
902 /* Set the bit for character C in a list. */
903 #define SET_LIST_BIT(c) \
904 (b[((unsigned char) (c)) / BYTEWIDTH] \
905 |= 1 << (((unsigned char) c) % BYTEWIDTH))
907 /* Get the next unsigned number in the uncompiled pattern. */
908 #define GET_UNSIGNED_NUMBER(num) \
912 while (ISDIGIT (c)) \
916 num = num * 10 + c - '0'; \
924 #define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
926 #define IS_CHAR_CLASS(string) \
927 (STREQ (string, "alpha") || STREQ (string, "upper") \
928 || STREQ (string, "lower") || STREQ (string, "digit") \
929 || STREQ (string, "alnum") || STREQ (string, "xdigit") \
930 || STREQ (string, "space") || STREQ (string, "print") \
931 || STREQ (string, "punct") || STREQ (string, "graph") \
932 || STREQ (string, "cntrl") || STREQ (string, "blank"))
934 /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
935 * Returns one of error codes defined in `regex.h', or zero for success.
937 * Assumes the `allocated' (and perhaps `buffer') and `translate'
938 * fields are set in BUFP on entry.
940 * If it succeeds, results are put in BUFP (if it returns an error, the
941 * contents of BUFP are undefined):
942 * `buffer' is the compiled pattern;
943 * `syntax' is set to SYNTAX;
944 * `used' is set to the length of the compiled pattern;
945 * `fastmap_accurate' is zero;
946 * `re_nsub' is the number of subexpressions in PATTERN;
947 * `not_bol' and `not_eol' are zero;
949 * The `fastmap' and `newline_anchor' fields are neither
950 * examined nor set. */
953 regex_compile(const char *pattern
, int size
, reg_syntax_t syntax
, struct re_pattern_buffer
*bufp
)
955 /* We fetch characters from PATTERN here. Even though PATTERN is
956 * `char *' (i.e., signed), we declare these variables as unsigned, so
957 * they can be reliably used as array indices. */
958 register unsigned char c
, c1
;
960 /* A random tempory spot in PATTERN. */
963 /* Points to the end of the buffer, where we should append. */
964 register unsigned char *b
;
966 /* Keeps track of unclosed groups. */
967 compile_stack_type compile_stack
;
969 /* Points to the current (ending) position in the pattern. */
970 const char *p
= pattern
;
971 const char *pend
= pattern
+ size
;
973 /* How to translate the characters in the pattern. */
974 char *translate
= bufp
->translate
;
976 /* Address of the count-byte of the most recently inserted `exactn'
977 * command. This makes it possible to tell if a new exact-match
978 * character can be added to that command or if the character requires
979 * a new `exactn' command. */
980 unsigned char *pending_exact
= 0;
982 /* Address of start of the most recently finished expression.
983 * This tells, e.g., postfix * where to find the start of its
984 * operand. Reset at the beginning of groups and alternatives. */
985 unsigned char *laststart
= 0;
987 /* Address of beginning of regexp, or inside of last group. */
988 unsigned char *begalt
;
990 /* Place in the uncompiled pattern (i.e., the {) to
991 * which to go back if the interval is invalid. */
992 const char *beg_interval
;
994 /* Address of the place where a forward jump should go to the end of
995 * the containing expression. Each alternative of an `or' -- except the
996 * last -- ends with a forward jump of this sort. */
997 unsigned char *fixup_alt_jump
= 0;
999 /* Counts open-groups as they are encountered. Remembered for the
1000 * matching close-group on the compile stack, so the same register
1001 * number is put in the stop_memory as the start_memory. */
1002 regnum_t regnum
= 0;
1005 DEBUG_PRINT1("\nCompiling pattern: ");
1007 unsigned debug_count
;
1009 for (debug_count
= 0; debug_count
< size
; debug_count
++)
1010 printchar(pattern
[debug_count
]);
1015 /* Initialize the compile stack. */
1016 compile_stack
.stack
= TALLOC(INIT_COMPILE_STACK_SIZE
, compile_stack_elt_t
);
1017 if (compile_stack
.stack
== NULL
)
1020 compile_stack
.size
= INIT_COMPILE_STACK_SIZE
;
1021 compile_stack
.avail
= 0;
1023 /* Initialize the pattern buffer. */
1024 bufp
->syntax
= syntax
;
1025 bufp
->fastmap_accurate
= 0;
1026 bufp
->not_bol
= bufp
->not_eol
= 0;
1028 /* Set `used' to zero, so that if we return an error, the pattern
1029 * printer (for debugging) will think there's no pattern. We reset it
1033 /* Always count groups, whether or not bufp->no_sub is set. */
1036 #if !defined (SYNTAX_TABLE)
1037 /* Initialize the syntax table. */
1041 if (bufp
->allocated
== 0) {
1042 if (bufp
->buffer
) { /* If zero allocated, but buffer is non-null, try to realloc
1043 * enough space. This loses if buffer's address is bogus, but
1044 * that is the user's responsibility. */
1045 RETALLOC(bufp
->buffer
, INIT_BUF_SIZE
, unsigned char);
1046 } else { /* Caller did not allocate a buffer. Do it for them. */
1047 bufp
->buffer
= TALLOC(INIT_BUF_SIZE
, unsigned char);
1052 bufp
->allocated
= INIT_BUF_SIZE
;
1054 begalt
= b
= bufp
->buffer
;
1056 /* Loop through the uncompiled pattern until we're at the end. */
1062 if ( /* If at start of pattern, it's an operator. */
1064 /* If context independent, it's an operator. */
1065 || syntax
& RE_CONTEXT_INDEP_ANCHORS
1066 /* Otherwise, depends on what's come before. */
1067 || at_begline_loc_p(pattern
, p
, syntax
))
1075 if ( /* If at end of pattern, it's an operator. */
1077 /* If context independent, it's an operator. */
1078 || syntax
& RE_CONTEXT_INDEP_ANCHORS
1079 /* Otherwise, depends on what's next. */
1080 || at_endline_loc_p(p
, pend
, syntax
))
1089 if ((syntax
& RE_BK_PLUS_QM
)
1090 || (syntax
& RE_LIMITED_OPS
))
1094 /* If there is no previous pattern... */
1096 if (syntax
& RE_CONTEXT_INVALID_OPS
)
1098 else if (!(syntax
& RE_CONTEXT_INDEP_OPS
))
1101 /* Are we optimizing this jump? */
1102 boolean keep_string_p
= false;
1104 /* 1 means zero (many) matches is allowed. */
1105 char zero_times_ok
= 0, many_times_ok
= 0;
1107 /* If there is a sequence of repetition chars, collapse it
1108 * down to just one (the right one). We can't combine
1109 * interval operators with these because of, e.g., `a{2}*',
1110 * which should only match an even number of `a's. */
1113 zero_times_ok
|= c
!= '+';
1114 many_times_ok
|= c
!= '?';
1122 || (!(syntax
& RE_BK_PLUS_QM
) && (c
== '+' || c
== '?')));
1124 else if (syntax
& RE_BK_PLUS_QM
&& c
== '\\') {
1129 if (!(c1
== '+' || c1
== '?')) {
1140 /* If we get here, we found another repeat character. */
1143 /* Star, etc. applied to an empty pattern is equivalent
1144 * to an empty pattern. */
1148 /* Now we know whether or not zero matches is allowed
1149 * and also whether or not two or more matches is allowed. */
1150 if (many_times_ok
) { /* More than one repetition is allowed, so put in at the
1151 * end a backward relative jump from `b' to before the next
1152 * jump we're going to put in below (which jumps from
1153 * laststart to after this jump).
1155 * But if we are at the `*' in the exact sequence `.*\n',
1156 * insert an unconditional jump backwards to the .,
1157 * instead of the beginning of the loop. This way we only
1158 * push a failure point once, instead of every time
1159 * through the loop. */
1160 assert(p
- 1 > pattern
);
1162 /* Allocate the space for the jump. */
1163 GET_BUFFER_SPACE(3);
1165 /* We know we are not at the first character of the pattern,
1166 * because laststart was nonzero. And we've already
1167 * incremented `p', by the way, to be the character after
1168 * the `*'. Do we have to do something analogous here
1169 * for null bytes, because of RE_DOT_NOT_NULL? */
1170 if (TRANSLATE(*(p
- 2)) == TRANSLATE('.')
1172 && p
< pend
&& TRANSLATE(*p
) == TRANSLATE('\n')
1173 && !(syntax
& RE_DOT_NEWLINE
)) { /* We have .*\n. */
1174 STORE_JUMP(jump
, b
, laststart
);
1175 keep_string_p
= true;
1177 /* Anything else. */
1178 STORE_JUMP(maybe_pop_jump
, b
, laststart
- 3);
1180 /* We've added more stuff to the buffer. */
1183 /* On failure, jump from laststart to b + 3, which will be the
1184 * end of the buffer after this jump is inserted. */
1185 GET_BUFFER_SPACE(3);
1186 INSERT_JUMP(keep_string_p
? on_failure_keep_string_jump
1192 if (!zero_times_ok
) {
1193 /* At least one repetition is required, so insert a
1194 * `dummy_failure_jump' before the initial
1195 * `on_failure_jump' instruction of the loop. This
1196 * effects a skip over that instruction the first time
1197 * we hit that loop. */
1198 GET_BUFFER_SPACE(3);
1199 INSERT_JUMP(dummy_failure_jump
, laststart
, laststart
+ 6);
1211 boolean had_char_class
= false;
1216 /* Ensure that we have enough space to push a charset: the
1217 * opcode, the length count, and the bitset; 34 bytes in all. */
1218 GET_BUFFER_SPACE(34);
1222 /* We test `*p == '^' twice, instead of using an if
1223 * statement, so we only need one BUF_PUSH. */
1224 BUF_PUSH(*p
== '^' ? charset_not
: charset
);
1228 /* Remember the first position in the bracket expression. */
1231 /* Push the number of bytes in the bitmap. */
1232 BUF_PUSH((1 << BYTEWIDTH
) / BYTEWIDTH
);
1234 /* Clear the whole map. */
1235 memset(b
, 0, (1 << BYTEWIDTH
) / BYTEWIDTH
);
1237 /* charset_not matches newline according to a syntax bit. */
1238 if ((re_opcode_t
) b
[-2] == charset_not
1239 && (syntax
& RE_HAT_LISTS_NOT_NEWLINE
))
1242 /* Read in characters and ranges, setting map bits. */
1249 /* \ might escape characters inside [...] and [^...]. */
1250 if ((syntax
& RE_BACKSLASH_ESCAPE_IN_LISTS
) && c
== '\\') {
1258 /* Could be the end of the bracket expression. If it's
1259 * not (i.e., when the bracket expression is `[]' so
1260 * far), the ']' character bit gets set way below. */
1261 if (c
== ']' && p
!= p1
+ 1)
1264 /* Look ahead to see if it's a range when the last thing
1265 * was a character class. */
1266 if (had_char_class
&& c
== '-' && *p
!= ']')
1269 /* Look ahead to see if it's a range when the last thing
1270 * was a character: if this is a hyphen not at the
1271 * beginning or the end of a list, then it's the range
1274 && !(p
- 2 >= pattern
&& p
[-2] == '[')
1275 && !(p
- 3 >= pattern
&& p
[-3] == '[' && p
[-2] == '^')
1278 = compile_range(&p
, pend
, translate
, syntax
, b
);
1279 if (ret
!= REG_NOERROR
)
1281 } else if (p
[0] == '-' && p
[1] != ']') { /* This handles ranges made up of characters only. */
1284 /* Move past the `-'. */
1287 ret
= compile_range(&p
, pend
, translate
, syntax
, b
);
1288 if (ret
!= REG_NOERROR
)
1291 /* See if we're at the beginning of a possible character
1294 else if (syntax
& RE_CHAR_CLASSES
&& c
== '[' && *p
== ':') { /* Leave room for the null. */
1295 char str
[CHAR_CLASS_MAX_LENGTH
+ 1];
1300 /* If pattern is `[[:'. */
1306 if (c
== ':' || c
== ']' || p
== pend
1307 || c1
== CHAR_CLASS_MAX_LENGTH
)
1313 /* If isn't a word bracketed by `[:' and:`]':
1314 * undo the ending character, the letters, and leave
1315 * the leading `:' and `[' (but set bits for them). */
1316 if (c
== ':' && *p
== ']') {
1318 boolean is_alnum
= STREQ(str
, "alnum");
1319 boolean is_alpha
= STREQ(str
, "alpha");
1320 boolean is_blank
= STREQ(str
, "blank");
1321 boolean is_cntrl
= STREQ(str
, "cntrl");
1322 boolean is_digit
= STREQ(str
, "digit");
1323 boolean is_graph
= STREQ(str
, "graph");
1324 boolean is_lower
= STREQ(str
, "lower");
1325 boolean is_print
= STREQ(str
, "print");
1326 boolean is_punct
= STREQ(str
, "punct");
1327 boolean is_space
= STREQ(str
, "space");
1328 boolean is_upper
= STREQ(str
, "upper");
1329 boolean is_xdigit
= STREQ(str
, "xdigit");
1331 if (!IS_CHAR_CLASS(str
))
1334 /* Throw away the ] at the end of the character
1341 for (ch
= 0; ch
< 1 << BYTEWIDTH
; ch
++) {
1342 if ((is_alnum
&& ISALNUM(ch
))
1343 || (is_alpha
&& ISALPHA(ch
))
1344 || (is_blank
&& ISBLANK(ch
))
1345 || (is_cntrl
&& ISCNTRL(ch
))
1346 || (is_digit
&& ISDIGIT(ch
))
1347 || (is_graph
&& ISGRAPH(ch
))
1348 || (is_lower
&& ISLOWER(ch
))
1349 || (is_print
&& ISPRINT(ch
))
1350 || (is_punct
&& ISPUNCT(ch
))
1351 || (is_space
&& ISSPACE(ch
))
1352 || (is_upper
&& ISUPPER(ch
))
1353 || (is_xdigit
&& ISXDIGIT(ch
)))
1356 had_char_class
= true;
1363 had_char_class
= false;
1366 had_char_class
= false;
1371 /* Discard any (non)matching list bytes that are all 0 at the
1372 * end of the map. Decrease the map-length byte too. */
1373 while ((int) b
[-1] > 0 && b
[b
[-1] - 1] == 0)
1380 if (syntax
& RE_NO_BK_PARENS
)
1386 if (syntax
& RE_NO_BK_PARENS
)
1392 if (syntax
& RE_NEWLINE_ALT
)
1398 if (syntax
& RE_NO_BK_VBAR
)
1404 if (syntax
& RE_INTERVALS
&& syntax
& RE_NO_BK_BRACES
)
1405 goto handle_interval
;
1413 /* Do not translate the character after the \, so that we can
1414 * distinguish, e.g., \B from \b, even if we normally would
1415 * translate, e.g., B to b. */
1420 if (syntax
& RE_NO_BK_PARENS
)
1421 goto normal_backslash
;
1427 if (COMPILE_STACK_FULL
) {
1428 RETALLOC(compile_stack
.stack
, compile_stack
.size
<< 1,
1429 compile_stack_elt_t
);
1430 if (compile_stack
.stack
== NULL
)
1433 compile_stack
.size
<<= 1;
1435 /* These are the values to restore when we hit end of this
1436 * group. They are all relative offsets, so that if the
1437 * whole pattern moves because of realloc, they will still
1439 COMPILE_STACK_TOP
.begalt_offset
= begalt
- bufp
->buffer
;
1440 COMPILE_STACK_TOP
.fixup_alt_jump
1441 = fixup_alt_jump
? fixup_alt_jump
- bufp
->buffer
+ 1 : 0;
1442 COMPILE_STACK_TOP
.laststart_offset
= b
- bufp
->buffer
;
1443 COMPILE_STACK_TOP
.regnum
= regnum
;
1445 /* We will eventually replace the 0 with the number of
1446 * groups inner to this one. But do not push a
1447 * start_memory for groups beyond the last one we can
1448 * represent in the compiled pattern. */
1449 if (regnum
<= MAX_REGNUM
) {
1450 COMPILE_STACK_TOP
.inner_group_offset
= b
- bufp
->buffer
+ 2;
1451 BUF_PUSH_3(start_memory
, regnum
, 0);
1453 compile_stack
.avail
++;
1458 /* If we've reached MAX_REGNUM groups, then this open
1459 * won't actually generate any code, so we'll have to
1460 * clear pending_exact explicitly. */
1465 if (syntax
& RE_NO_BK_PARENS
)
1466 goto normal_backslash
;
1468 if (COMPILE_STACK_EMPTY
) {
1469 if (syntax
& RE_UNMATCHED_RIGHT_PAREN_ORD
)
1470 goto normal_backslash
;
1475 if (fixup_alt_jump
) { /* Push a dummy failure point at the end of the
1476 * alternative for a possible future
1477 * `pop_failure_jump' to pop. See comments at
1478 * `push_dummy_failure' in `re_match_2'. */
1479 BUF_PUSH(push_dummy_failure
);
1481 /* We allocated space for this jump when we assigned
1482 * to `fixup_alt_jump', in the `handle_alt' case below. */
1483 STORE_JUMP(jump_past_alt
, fixup_alt_jump
, b
- 1);
1485 /* See similar code for backslashed left paren above. */
1486 if (COMPILE_STACK_EMPTY
) {
1487 if (syntax
& RE_UNMATCHED_RIGHT_PAREN_ORD
)
1492 /* Since we just checked for an empty stack above, this
1493 * ``can't happen''. */
1494 assert(compile_stack
.avail
!= 0);
1496 /* We don't just want to restore into `regnum', because
1497 * later groups should continue to be numbered higher,
1498 * as in `(ab)c(de)' -- the second group is #2. */
1499 regnum_t this_group_regnum
;
1501 compile_stack
.avail
--;
1502 begalt
= bufp
->buffer
+ COMPILE_STACK_TOP
.begalt_offset
;
1504 = COMPILE_STACK_TOP
.fixup_alt_jump
1505 ? bufp
->buffer
+ COMPILE_STACK_TOP
.fixup_alt_jump
- 1
1507 laststart
= bufp
->buffer
+ COMPILE_STACK_TOP
.laststart_offset
;
1508 this_group_regnum
= COMPILE_STACK_TOP
.regnum
;
1509 /* If we've reached MAX_REGNUM groups, then this open
1510 * won't actually generate any code, so we'll have to
1511 * clear pending_exact explicitly. */
1514 /* We're at the end of the group, so now we know how many
1515 * groups were inside this one. */
1516 if (this_group_regnum
<= MAX_REGNUM
) {
1517 unsigned char *inner_group_loc
1518 = bufp
->buffer
+ COMPILE_STACK_TOP
.inner_group_offset
;
1520 *inner_group_loc
= regnum
- this_group_regnum
;
1521 BUF_PUSH_3(stop_memory
, this_group_regnum
,
1522 regnum
- this_group_regnum
);
1527 case '|': /* `\|'. */
1528 if (syntax
& RE_LIMITED_OPS
|| syntax
& RE_NO_BK_VBAR
)
1529 goto normal_backslash
;
1531 if (syntax
& RE_LIMITED_OPS
)
1534 /* Insert before the previous alternative a jump which
1535 * jumps to this alternative if the former fails. */
1536 GET_BUFFER_SPACE(3);
1537 INSERT_JUMP(on_failure_jump
, begalt
, b
+ 6);
1541 /* The alternative before this one has a jump after it
1542 * which gets executed if it gets matched. Adjust that
1543 * jump so it will jump to this alternative's analogous
1544 * jump (put in below, which in turn will jump to the next
1545 * (if any) alternative's such jump, etc.). The last such
1546 * jump jumps to the correct final destination. A picture:
1552 * If we are at `b', then fixup_alt_jump right now points to a
1553 * three-byte space after `a'. We'll put in the jump, set
1554 * fixup_alt_jump to right after `b', and leave behind three
1555 * bytes which we'll fill in when we get to after `c'. */
1558 STORE_JUMP(jump_past_alt
, fixup_alt_jump
, b
);
1560 /* Mark and leave space for a jump after this alternative,
1561 * to be filled in later either by next alternative or
1562 * when know we're at the end of a series of alternatives. */
1564 GET_BUFFER_SPACE(3);
1572 /* If \{ is a literal. */
1573 if (!(syntax
& RE_INTERVALS
)
1574 /* If we're at `\{' and it's not the open-interval
1576 || ((syntax
& RE_INTERVALS
) && (syntax
& RE_NO_BK_BRACES
))
1577 || (p
- 2 == pattern
&& p
== pend
))
1578 goto normal_backslash
;
1581 /* If got here, then the syntax allows intervals. */
1583 /* At least (most) this many matches must be made. */
1584 int lower_bound
= -1, upper_bound
= -1;
1586 beg_interval
= p
- 1;
1589 if (syntax
& RE_NO_BK_BRACES
)
1590 goto unfetch_interval
;
1594 GET_UNSIGNED_NUMBER(lower_bound
);
1597 GET_UNSIGNED_NUMBER(upper_bound
);
1598 if (upper_bound
< 0)
1599 upper_bound
= RE_DUP_MAX
;
1601 /* Interval such as `{1}' => match exactly once. */
1602 upper_bound
= lower_bound
;
1604 if (lower_bound
< 0 || upper_bound
> RE_DUP_MAX
1605 || lower_bound
> upper_bound
) {
1606 if (syntax
& RE_NO_BK_BRACES
)
1607 goto unfetch_interval
;
1611 if (!(syntax
& RE_NO_BK_BRACES
)) {
1618 if (syntax
& RE_NO_BK_BRACES
)
1619 goto unfetch_interval
;
1623 /* We just parsed a valid interval. */
1625 /* If it's invalid to have no preceding re. */
1627 if (syntax
& RE_CONTEXT_INVALID_OPS
)
1629 else if (syntax
& RE_CONTEXT_INDEP_OPS
)
1632 goto unfetch_interval
;
1634 /* If the upper bound is zero, don't want to succeed at
1635 * all; jump from `laststart' to `b + 3', which will be
1636 * the end of the buffer after we insert the jump. */
1637 if (upper_bound
== 0) {
1638 GET_BUFFER_SPACE(3);
1639 INSERT_JUMP(jump
, laststart
, b
+ 3);
1642 /* Otherwise, we have a nontrivial interval. When
1643 * we're all done, the pattern will look like:
1644 * set_number_at <jump count> <upper bound>
1645 * set_number_at <succeed_n count> <lower bound>
1646 * succeed_n <after jump addr> <succed_n count>
1648 * jump_n <succeed_n addr> <jump count>
1649 * (The upper bound and `jump_n' are omitted if
1650 * `upper_bound' is 1, though.) */
1651 else { /* If the upper bound is > 1, we need to insert
1652 * more at the end of the loop. */
1653 unsigned nbytes
= 10 + (upper_bound
> 1) * 10;
1655 GET_BUFFER_SPACE(nbytes
);
1657 /* Initialize lower bound of the `succeed_n', even
1658 * though it will be set during matching by its
1659 * attendant `set_number_at' (inserted next),
1660 * because `re_compile_fastmap' needs to know.
1661 * Jump to the `jump_n' we might insert below. */
1662 INSERT_JUMP2(succeed_n
, laststart
,
1663 b
+ 5 + (upper_bound
> 1) * 5,
1667 /* Code to initialize the lower bound. Insert
1668 * before the `succeed_n'. The `5' is the last two
1669 * bytes of this `set_number_at', plus 3 bytes of
1670 * the following `succeed_n'. */
1671 insert_op2(set_number_at
, laststart
, 5, lower_bound
, b
);
1674 if (upper_bound
> 1) { /* More than one repetition is allowed, so
1675 * append a backward jump to the `succeed_n'
1676 * that starts this interval.
1678 * When we've reached this during matching,
1679 * we'll have matched the interval once, so
1680 * jump back only `upper_bound - 1' times. */
1681 STORE_JUMP2(jump_n
, b
, laststart
+ 5,
1685 /* The location we want to set is the second
1686 * parameter of the `jump_n'; that is `b-2' as
1687 * an absolute address. `laststart' will be
1688 * the `set_number_at' we're about to insert;
1689 * `laststart+3' the number to set, the source
1690 * for the relative address. But we are
1691 * inserting into the middle of the pattern --
1692 * so everything is getting moved up by 5.
1693 * Conclusion: (b - 2) - (laststart + 3) + 5,
1694 * i.e., b - laststart.
1696 * We insert this at the beginning of the loop
1697 * so that if we fail during matching, we'll
1698 * reinitialize the bounds. */
1699 insert_op2(set_number_at
, laststart
, b
- laststart
,
1700 upper_bound
- 1, b
);
1705 beg_interval
= NULL
;
1710 /* If an invalid interval, match the characters as literals. */
1711 assert(beg_interval
);
1713 beg_interval
= NULL
;
1715 /* normal_char and normal_backslash need `c'. */
1718 if (!(syntax
& RE_NO_BK_BRACES
)) {
1719 if (p
> pattern
&& p
[-1] == '\\')
1720 goto normal_backslash
;
1731 BUF_PUSH(notwordchar
);
1743 BUF_PUSH(wordbound
);
1747 BUF_PUSH(notwordbound
);
1767 if (syntax
& RE_NO_BK_REFS
)
1775 /* Can't back reference to a subexpression if inside of it. */
1776 if (group_in_compile_stack(compile_stack
, c1
))
1780 BUF_PUSH_2(duplicate
, c1
);
1785 if (syntax
& RE_BK_PLUS_QM
)
1788 goto normal_backslash
;
1792 /* You might think it would be useful for \ to mean
1793 * not to translate; but if we don't translate it
1794 * it will never match anything. */
1801 /* Expects the character in `c'. */
1803 /* If no exactn currently being built. */
1806 /* If last exactn not at current position. */
1807 || pending_exact
+ *pending_exact
+ 1 != b
1809 /* We have only one byte following the exactn for the count. */
1810 || *pending_exact
== (1 << BYTEWIDTH
) - 1
1812 /* If followed by a repetition operator. */
1813 || *p
== '*' || *p
== '^'
1814 || ((syntax
& RE_BK_PLUS_QM
)
1815 ? *p
== '\\' && (p
[1] == '+' || p
[1] == '?')
1816 : (*p
== '+' || *p
== '?'))
1817 || ((syntax
& RE_INTERVALS
)
1818 && ((syntax
& RE_NO_BK_BRACES
)
1820 : (p
[0] == '\\' && p
[1] == '{')))) {
1821 /* Start building a new exactn. */
1825 BUF_PUSH_2(exactn
, 0);
1826 pending_exact
= b
- 1;
1832 } /* while p != pend */
1834 /* Through the pattern now. */
1837 STORE_JUMP(jump_past_alt
, fixup_alt_jump
, b
);
1839 if (!COMPILE_STACK_EMPTY
)
1842 free(compile_stack
.stack
);
1844 /* We have succeeded; set the length of the buffer. */
1845 bufp
->used
= b
- bufp
->buffer
;
1849 DEBUG_PRINT1("\nCompiled pattern: ");
1850 print_compiled_pattern(bufp
);
1855 } /* regex_compile */
1857 /* Subroutines for `regex_compile'. */
1859 /* Store OP at LOC followed by two-byte integer parameter ARG. */
1861 void store_op1(re_opcode_t op
, unsigned char *loc
, int arg
)
1863 *loc
= (unsigned char) op
;
1864 STORE_NUMBER(loc
+ 1, arg
);
1867 /* Like `store_op1', but for two two-byte parameters ARG1 and ARG2. */
1870 store_op2( re_opcode_t op
, unsigned char *loc
, int arg1
, int arg2
)
1872 *loc
= (unsigned char) op
;
1873 STORE_NUMBER(loc
+ 1, arg1
);
1874 STORE_NUMBER(loc
+ 3, arg2
);
1877 /* Copy the bytes from LOC to END to open up three bytes of space at LOC
1878 * for OP followed by two-byte integer parameter ARG. */
1881 insert_op1(re_opcode_t op
, unsigned char *loc
, int arg
, unsigned char *end
)
1883 register unsigned char *pfrom
= end
;
1884 register unsigned char *pto
= end
+ 3;
1886 while (pfrom
!= loc
)
1889 store_op1(op
, loc
, arg
);
1892 /* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2. */
1895 insert_op2(re_opcode_t op
, unsigned char *loc
, int arg1
, int arg2
, unsigned char *end
)
1897 register unsigned char *pfrom
= end
;
1898 register unsigned char *pto
= end
+ 5;
1900 while (pfrom
!= loc
)
1903 store_op2(op
, loc
, arg1
, arg2
);
1906 /* P points to just after a ^ in PATTERN. Return true if that ^ comes
1907 * after an alternative or a begin-subexpression. We assume there is at
1908 * least one character before the ^. */
1911 at_begline_loc_p(const char * pattern
, const char *p
, reg_syntax_t syntax
)
1913 const char *prev
= p
- 2;
1914 boolean prev_prev_backslash
= prev
> pattern
&& prev
[-1] == '\\';
1917 /* After a subexpression? */
1918 (*prev
== '(' && (syntax
& RE_NO_BK_PARENS
|| prev_prev_backslash
))
1919 /* After an alternative? */
1920 || (*prev
== '|' && (syntax
& RE_NO_BK_VBAR
|| prev_prev_backslash
));
1923 /* The dual of at_begline_loc_p. This one is for $. We assume there is
1924 * at least one character after the $, i.e., `P < PEND'. */
1927 at_endline_loc_p(const char *p
, const char *pend
, int syntax
)
1929 const char *next
= p
;
1930 boolean next_backslash
= *next
== '\\';
1931 const char *next_next
= p
+ 1 < pend
? p
+ 1 : NULL
;
1934 /* Before a subexpression? */
1935 (syntax
& RE_NO_BK_PARENS
? *next
== ')'
1936 : next_backslash
&& next_next
&& *next_next
== ')')
1937 /* Before an alternative? */
1938 || (syntax
& RE_NO_BK_VBAR
? *next
== '|'
1939 : next_backslash
&& next_next
&& *next_next
== '|');
1942 /* Returns true if REGNUM is in one of COMPILE_STACK's elements and
1943 * false if it's not. */
1946 group_in_compile_stack(compile_stack_type compile_stack
, regnum_t regnum
)
1950 for (this_element
= compile_stack
.avail
- 1;
1953 if (compile_stack
.stack
[this_element
].regnum
== regnum
)
1959 /* Read the ending character of a range (in a bracket expression) from the
1960 * uncompiled pattern *P_PTR (which ends at PEND). We assume the
1961 * starting character is in `P[-2]'. (`P[-1]' is the character `-'.)
1962 * Then we set the translation of all bits between the starting and
1963 * ending characters (inclusive) in the compiled pattern B.
1965 * Return an error code.
1967 * We use these short variable names so we can use the same macros as
1968 * `regex_compile' itself. */
1971 compile_range(const char **p_ptr
, const char *pend
, char *translate
, reg_syntax_t syntax
, unsigned char *b
)
1975 const char *p
= *p_ptr
;
1976 int range_start
, range_end
;
1981 /* Even though the pattern is a signed `char *', we need to fetch
1982 * with unsigned char *'s; if the high bit of the pattern character
1983 * is set, the range endpoints will be negative if we fetch using a
1986 * We also want to fetch the endpoints without translating them; the
1987 * appropriate translation is done in the bit-setting loop below. */
1988 range_start
= ((unsigned char *) p
)[-2];
1989 range_end
= ((unsigned char *) p
)[0];
1991 /* Have to increment the pointer into the pattern string, so the
1992 * caller isn't still at the ending character. */
1995 /* If the start is after the end, the range is empty. */
1996 if (range_start
> range_end
)
1997 return syntax
& RE_NO_EMPTY_RANGES
? REG_ERANGE
: REG_NOERROR
;
1999 /* Here we see why `this_char' has to be larger than an `unsigned
2000 * char' -- the range is inclusive, so if `range_end' == 0xff
2001 * (assuming 8-bit characters), we would otherwise go into an infinite
2002 * loop, since all characters <= 0xff. */
2003 for (this_char
= range_start
; this_char
<= range_end
; this_char
++) {
2004 SET_LIST_BIT(TRANSLATE(this_char
));
2010 /* Failure stack declarations and macros; both re_compile_fastmap and
2011 * re_match_2 use a failure stack. These have to be macros because of
2012 * REGEX_ALLOCATE. */
2014 /* Number of failure points for which to initially allocate space
2015 * when matching. If this number is exceeded, we allocate more
2016 * space, so it is not a hard limit. */
2017 #ifndef INIT_FAILURE_ALLOC
2018 #define INIT_FAILURE_ALLOC 5
2021 /* Roughly the maximum number of failure points on the stack. Would be
2022 * exactly that if always used MAX_FAILURE_SPACE each time we failed.
2023 * This is a variable only so users of regex can assign to it; we never
2024 * change it ourselves. */
2025 int re_max_failures
= 2000;
2027 typedef const unsigned char *fail_stack_elt_t
;
2030 fail_stack_elt_t
*stack
;
2032 unsigned avail
; /* Offset of next open position. */
2035 #define FAIL_STACK_EMPTY() (fail_stack.avail == 0)
2036 #define FAIL_STACK_PTR_EMPTY() (fail_stack_ptr->avail == 0)
2037 #define FAIL_STACK_FULL() (fail_stack.avail == fail_stack.size)
2038 #define FAIL_STACK_TOP() (fail_stack.stack[fail_stack.avail])
2040 /* Initialize `fail_stack'. Do `return -2' if the alloc fails. */
2042 #define INIT_FAIL_STACK() \
2044 fail_stack.stack = (fail_stack_elt_t *) \
2045 REGEX_ALLOCATE (INIT_FAILURE_ALLOC * sizeof (fail_stack_elt_t)); \
2047 if (fail_stack.stack == NULL) \
2050 fail_stack.size = INIT_FAILURE_ALLOC; \
2051 fail_stack.avail = 0; \
2054 /* Double the size of FAIL_STACK, up to approximately `re_max_failures' items.
2056 * Return 1 if succeeds, and 0 if either ran out of memory
2057 * allocating space for it or it was already too large.
2059 * REGEX_REALLOCATE requires `destination' be declared. */
2061 #define DOUBLE_FAIL_STACK(fail_stack) \
2062 ((fail_stack).size > re_max_failures * MAX_FAILURE_ITEMS \
2064 : ((fail_stack).stack = (fail_stack_elt_t *) \
2065 REGEX_REALLOCATE ((fail_stack).stack, \
2066 (fail_stack).size * sizeof (fail_stack_elt_t), \
2067 ((fail_stack).size << 1) * sizeof (fail_stack_elt_t)), \
2069 (fail_stack).stack == NULL \
2071 : ((fail_stack).size <<= 1, \
2074 /* Push PATTERN_OP on FAIL_STACK.
2076 * Return 1 if was able to do so and 0 if ran out of memory allocating
2077 * space to do so. */
2078 #define PUSH_PATTERN_OP(pattern_op, fail_stack) \
2079 ((FAIL_STACK_FULL () \
2080 && !DOUBLE_FAIL_STACK (fail_stack)) \
2082 : ((fail_stack).stack[(fail_stack).avail++] = pattern_op, \
2085 /* This pushes an item onto the failure stack. Must be a four-byte
2086 * value. Assumes the variable `fail_stack'. Probably should only
2087 * be called from within `PUSH_FAILURE_POINT'. */
2088 #define PUSH_FAILURE_ITEM(item) \
2089 fail_stack.stack[fail_stack.avail++] = (fail_stack_elt_t) item
2091 /* The complement operation. Assumes `fail_stack' is nonempty. */
2092 #define POP_FAILURE_ITEM() fail_stack.stack[--fail_stack.avail]
2094 /* Used to omit pushing failure point id's when we're not debugging. */
2096 #define DEBUG_PUSH PUSH_FAILURE_ITEM
2097 #define DEBUG_POP(item_addr) *(item_addr) = POP_FAILURE_ITEM ()
2099 #define DEBUG_PUSH(item)
2100 #define DEBUG_POP(item_addr)
2103 /* Push the information about the state we will need
2104 * if we ever fail back to it.
2106 * Requires variables fail_stack, regstart, regend, reg_info, and
2107 * num_regs be declared. DOUBLE_FAIL_STACK requires `destination' be
2110 * Does `return FAILURE_CODE' if runs out of memory. */
2112 #define PUSH_FAILURE_POINT(pattern_place, string_place, failure_code) \
2114 char *destination; \
2115 /* Must be int, so when we don't save any registers, the arithmetic \
2116 of 0 + -1 isn't done as unsigned. */ \
2119 DEBUG_STATEMENT (failure_id++); \
2120 DEBUG_STATEMENT (nfailure_points_pushed++); \
2121 DEBUG_PRINT2 ("\nPUSH_FAILURE_POINT #%u:\n", failure_id); \
2122 DEBUG_PRINT2 (" Before push, next avail: %d\n", (fail_stack).avail);\
2123 DEBUG_PRINT2 (" size: %d\n", (fail_stack).size);\
2125 DEBUG_PRINT2 (" slots needed: %d\n", NUM_FAILURE_ITEMS); \
2126 DEBUG_PRINT2 (" available: %d\n", REMAINING_AVAIL_SLOTS); \
2128 /* Ensure we have enough space allocated for what we will push. */ \
2129 while (REMAINING_AVAIL_SLOTS < NUM_FAILURE_ITEMS) \
2131 if (!DOUBLE_FAIL_STACK (fail_stack)) \
2132 return failure_code; \
2134 DEBUG_PRINT2 ("\n Doubled stack; size now: %d\n", \
2135 (fail_stack).size); \
2136 DEBUG_PRINT2 (" slots available: %d\n", REMAINING_AVAIL_SLOTS);\
2139 /* Push the info, starting with the registers. */ \
2140 DEBUG_PRINT1 ("\n"); \
2142 for (this_reg = lowest_active_reg; this_reg <= highest_active_reg; \
2145 DEBUG_PRINT2 (" Pushing reg: %d\n", this_reg); \
2146 DEBUG_STATEMENT (num_regs_pushed++); \
2148 DEBUG_PRINT2 (" start: 0x%x\n", regstart[this_reg]); \
2149 PUSH_FAILURE_ITEM (regstart[this_reg]); \
2151 DEBUG_PRINT2 (" end: 0x%x\n", regend[this_reg]); \
2152 PUSH_FAILURE_ITEM (regend[this_reg]); \
2154 DEBUG_PRINT2 (" info: 0x%x\n ", reg_info[this_reg]); \
2155 DEBUG_PRINT2 (" match_null=%d", \
2156 REG_MATCH_NULL_STRING_P (reg_info[this_reg])); \
2157 DEBUG_PRINT2 (" active=%d", IS_ACTIVE (reg_info[this_reg])); \
2158 DEBUG_PRINT2 (" matched_something=%d", \
2159 MATCHED_SOMETHING (reg_info[this_reg])); \
2160 DEBUG_PRINT2 (" ever_matched=%d", \
2161 EVER_MATCHED_SOMETHING (reg_info[this_reg])); \
2162 DEBUG_PRINT1 ("\n"); \
2163 PUSH_FAILURE_ITEM (reg_info[this_reg].word); \
2166 DEBUG_PRINT2 (" Pushing low active reg: %d\n", lowest_active_reg);\
2167 PUSH_FAILURE_ITEM (lowest_active_reg); \
2169 DEBUG_PRINT2 (" Pushing high active reg: %d\n", highest_active_reg);\
2170 PUSH_FAILURE_ITEM (highest_active_reg); \
2172 DEBUG_PRINT2 (" Pushing pattern 0x%x: ", pattern_place); \
2173 DEBUG_PRINT_COMPILED_PATTERN (bufp, pattern_place, pend); \
2174 PUSH_FAILURE_ITEM (pattern_place); \
2176 DEBUG_PRINT2 (" Pushing string 0x%x: `", string_place); \
2177 DEBUG_PRINT_DOUBLE_STRING (string_place, string1, size1, string2, \
2179 DEBUG_PRINT1 ("'\n"); \
2180 PUSH_FAILURE_ITEM (string_place); \
2182 DEBUG_PRINT2 (" Pushing failure id: %u\n", failure_id); \
2183 DEBUG_PUSH (failure_id); \
2186 /* This is the number of items that are pushed and popped on the stack
2187 * for each register. */
2188 #define NUM_REG_ITEMS 3
2190 /* Individual items aside from the registers. */
2192 #define NUM_NONREG_ITEMS 5 /* Includes failure point id. */
2194 #define NUM_NONREG_ITEMS 4
2197 /* We push at most this many items on the stack. */
2198 #define MAX_FAILURE_ITEMS ((num_regs - 1) * NUM_REG_ITEMS + NUM_NONREG_ITEMS)
2200 /* We actually push this many items. */
2201 #define NUM_FAILURE_ITEMS \
2202 ((highest_active_reg - lowest_active_reg + 1) * NUM_REG_ITEMS \
2205 /* How many items can still be added to the stack without overflowing it. */
2206 #define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail)
2208 /* Pops what PUSH_FAIL_STACK pushes.
2210 * We restore into the parameters, all of which should be lvalues:
2211 * STR -- the saved data position.
2212 * PAT -- the saved pattern position.
2213 * LOW_REG, HIGH_REG -- the highest and lowest active registers.
2214 * REGSTART, REGEND -- arrays of string positions.
2215 * REG_INFO -- array of information about each subexpression.
2217 * Also assumes the variables `fail_stack' and (if debugging), `bufp',
2218 * `pend', `string1', `size1', `string2', and `size2'. */
2220 #define POP_FAILURE_POINT(str, pat, low_reg, high_reg, regstart, regend, reg_info)\
2222 DEBUG_STATEMENT (fail_stack_elt_t failure_id;) \
2224 const unsigned char *string_temp; \
2226 assert (!FAIL_STACK_EMPTY ()); \
2228 /* Remove failure points and point to how many regs pushed. */ \
2229 DEBUG_PRINT1 ("POP_FAILURE_POINT:\n"); \
2230 DEBUG_PRINT2 (" Before pop, next avail: %d\n", fail_stack.avail); \
2231 DEBUG_PRINT2 (" size: %d\n", fail_stack.size); \
2233 assert (fail_stack.avail >= NUM_NONREG_ITEMS); \
2235 DEBUG_POP (&failure_id); \
2236 DEBUG_PRINT2 (" Popping failure id: %u\n", failure_id); \
2238 /* If the saved string location is NULL, it came from an \
2239 on_failure_keep_string_jump opcode, and we want to throw away the \
2240 saved NULL, thus retaining our current position in the string. */ \
2241 string_temp = POP_FAILURE_ITEM (); \
2242 if (string_temp != NULL) \
2243 str = (const char *) string_temp; \
2245 DEBUG_PRINT2 (" Popping string 0x%x: `", str); \
2246 DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2); \
2247 DEBUG_PRINT1 ("'\n"); \
2249 pat = (unsigned char *) POP_FAILURE_ITEM (); \
2250 DEBUG_PRINT2 (" Popping pattern 0x%x: ", pat); \
2251 DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend); \
2253 /* Restore register info. */ \
2254 high_reg = (unsigned long) POP_FAILURE_ITEM (); \
2255 DEBUG_PRINT2 (" Popping high active reg: %d\n", high_reg); \
2257 low_reg = (unsigned long) POP_FAILURE_ITEM (); \
2258 DEBUG_PRINT2 (" Popping low active reg: %d\n", low_reg); \
2260 for (this_reg = high_reg; this_reg >= low_reg; this_reg--) \
2262 DEBUG_PRINT2 (" Popping reg: %d\n", this_reg); \
2264 reg_info[this_reg].word = POP_FAILURE_ITEM (); \
2265 DEBUG_PRINT2 (" info: 0x%x\n", reg_info[this_reg]); \
2267 regend[this_reg] = (const char *) POP_FAILURE_ITEM (); \
2268 DEBUG_PRINT2 (" end: 0x%x\n", regend[this_reg]); \
2270 regstart[this_reg] = (const char *) POP_FAILURE_ITEM (); \
2271 DEBUG_PRINT2 (" start: 0x%x\n", regstart[this_reg]); \
2274 DEBUG_STATEMENT (nfailure_points_popped++); \
2275 } /* POP_FAILURE_POINT */
2277 /* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
2278 * BUFP. A fastmap records which of the (1 << BYTEWIDTH) possible
2279 * characters can start a string that matches the pattern. This fastmap
2280 * is used by re_search to skip quickly over impossible starting points.
2282 * The caller must supply the address of a (1 << BYTEWIDTH)-byte data
2283 * area as BUFP->fastmap.
2285 * We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in
2286 * the pattern buffer.
2288 * Returns 0 if we succeed, -2 if an internal error. */
2291 re_compile_fastmap(struct re_pattern_buffer
*bufp
)
2294 re_compile_fastmap(bufp
)
2295 struct re_pattern_buffer
*bufp
;
2299 fail_stack_type fail_stack
;
2300 #ifndef REGEX_MALLOC
2303 /* We don't push any register information onto the failure stack. */
2304 unsigned num_regs
= 0;
2306 register char *fastmap
= bufp
->fastmap
;
2307 unsigned char *pattern
= bufp
->buffer
;
2308 unsigned long size
= bufp
->used
;
2309 const unsigned char *p
= pattern
;
2310 register unsigned char *pend
= pattern
+ size
;
2312 /* Assume that each path through the pattern can be null until
2313 * proven otherwise. We set this false at the bottom of switch
2314 * statement, to which we get only if a particular path doesn't
2315 * match the empty string. */
2316 boolean path_can_be_null
= true;
2318 /* We aren't doing a `succeed_n' to begin with. */
2319 boolean succeed_n_p
= false;
2321 assert(fastmap
!= NULL
&& p
!= NULL
);
2324 memset(fastmap
, 0, 1 << BYTEWIDTH
); /* Assume nothing's valid. */
2325 bufp
->fastmap_accurate
= 1; /* It will be when we're done. */
2326 bufp
->can_be_null
= 0;
2328 while (p
!= pend
|| !FAIL_STACK_EMPTY()) {
2330 bufp
->can_be_null
|= path_can_be_null
;
2332 /* Reset for next path. */
2333 path_can_be_null
= true;
2335 p
= fail_stack
.stack
[--fail_stack
.avail
];
2337 /* We should never be about to go beyond the end of the pattern. */
2340 #ifdef SWITCH_ENUM_BUG
2341 switch ((int) ((re_opcode_t
) * p
++))
2343 switch ((re_opcode_t
) * p
++)
2347 /* I guess the idea here is to simply not bother with a fastmap
2348 * if a backreference is used, since it's too hard to figure out
2349 * the fastmap for the corresponding group. Setting
2350 * `can_be_null' stops `re_search_2' from using the fastmap, so
2351 * that is all we do. */
2353 bufp
->can_be_null
= 1;
2356 /* Following are the cases which match a character. These end
2364 for (j
= *p
++ * BYTEWIDTH
- 1; j
>= 0; j
--)
2365 if (p
[j
/ BYTEWIDTH
] & (1 << (j
% BYTEWIDTH
)))
2370 /* Chars beyond end of map must be allowed. */
2371 for (j
= *p
* BYTEWIDTH
; j
< (1 << BYTEWIDTH
); j
++)
2374 for (j
= *p
++ * BYTEWIDTH
- 1; j
>= 0; j
--)
2375 if (!(p
[j
/ BYTEWIDTH
] & (1 << (j
% BYTEWIDTH
))))
2380 for (j
= 0; j
< (1 << BYTEWIDTH
); j
++)
2381 if (SYNTAX(j
) == Sword
)
2386 for (j
= 0; j
< (1 << BYTEWIDTH
); j
++)
2387 if (SYNTAX(j
) != Sword
)
2392 /* `.' matches anything ... */
2393 for (j
= 0; j
< (1 << BYTEWIDTH
); j
++)
2396 /* ... except perhaps newline. */
2397 if (!(bufp
->syntax
& RE_DOT_NEWLINE
))
2400 /* Return if we have already set `can_be_null'; if we have,
2401 * then the fastmap is irrelevant. Something's wrong here. */
2402 else if (bufp
->can_be_null
)
2405 /* Otherwise, have to check alternative paths. */
2417 case push_dummy_failure
:
2421 case pop_failure_jump
:
2422 case maybe_pop_jump
:
2425 case dummy_failure_jump
:
2426 EXTRACT_NUMBER_AND_INCR(j
, p
);
2431 /* Jump backward implies we just went through the body of a
2432 * loop and matched nothing. Opcode jumped to should be
2433 * `on_failure_jump' or `succeed_n'. Just treat it like an
2434 * ordinary jump. For a * loop, it has pushed its failure
2435 * point already; if so, discard that as redundant. */
2436 if ((re_opcode_t
) * p
!= on_failure_jump
2437 && (re_opcode_t
) * p
!= succeed_n
)
2441 EXTRACT_NUMBER_AND_INCR(j
, p
);
2444 /* If what's on the stack is where we are now, pop it. */
2445 if (!FAIL_STACK_EMPTY()
2446 && fail_stack
.stack
[fail_stack
.avail
- 1] == p
)
2451 case on_failure_jump
:
2452 case on_failure_keep_string_jump
:
2453 handle_on_failure_jump
:
2454 EXTRACT_NUMBER_AND_INCR(j
, p
);
2456 /* For some patterns, e.g., `(a?)?', `p+j' here points to the
2457 * end of the pattern. We don't want to push such a point,
2458 * since when we restore it above, entering the switch will
2459 * increment `p' past the end of the pattern. We don't need
2460 * to push such a point since we obviously won't find any more
2461 * fastmap entries beyond `pend'. Such a pattern can match
2462 * the null string, though. */
2464 if (!PUSH_PATTERN_OP(p
+ j
, fail_stack
))
2467 bufp
->can_be_null
= 1;
2470 EXTRACT_NUMBER_AND_INCR(k
, p
); /* Skip the n. */
2471 succeed_n_p
= false;
2476 /* Get to the number of times to succeed. */
2479 /* Increment p past the n for when k != 0. */
2480 EXTRACT_NUMBER_AND_INCR(k
, p
);
2483 succeed_n_p
= true; /* Spaghetti code alert. */
2484 goto handle_on_failure_jump
;
2498 abort(); /* We have listed all the cases. */
2501 /* Getting here means we have found the possible starting
2502 * characters for one path of the pattern -- and that the empty
2503 * string does not match. We need not follow this path further.
2504 * Instead, look at the next alternative (remembered on the
2505 * stack), or quit if no more. The test at the top of the loop
2506 * does these things. */
2507 path_can_be_null
= false;
2511 /* Set `can_be_null' for the last path (also the first path, if the
2512 * pattern is empty). */
2513 bufp
->can_be_null
|= path_can_be_null
;
2515 } /* re_compile_fastmap */
2517 /* Searching routines. */
2519 /* Like re_search_2, below, but only one string is specified, and
2520 * doesn't let you say where to stop matching. */
2523 re_search(bufp
, string
, size
, startpos
, range
, regs
)
2524 struct re_pattern_buffer
*bufp
;
2526 int size
, startpos
, range
;
2527 struct re_registers
*regs
;
2529 return re_search_2(bufp
, NULL
, 0, string
, size
, startpos
, range
,
2533 /* Using the compiled pattern in BUFP->buffer, first tries to match the
2534 * virtual concatenation of STRING1 and STRING2, starting first at index
2535 * STARTPOS, then at STARTPOS + 1, and so on.
2537 * STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
2539 * RANGE is how far to scan while trying to match. RANGE = 0 means try
2540 * only at STARTPOS; in general, the last start tried is STARTPOS +
2543 * In REGS, return the indices of the virtual concatenation of STRING1
2544 * and STRING2 that matched the entire BUFP->buffer and its contained
2547 * Do not consider matching one past the index STOP in the virtual
2548 * concatenation of STRING1 and STRING2.
2550 * We return either the position in the strings at which the match was
2551 * found, -1 if no match, or -2 if error (such as failure
2552 * stack overflow). */
2555 re_search_2(bufp
, string1
, size1
, string2
, size2
, startpos
, range
, regs
, stop
)
2556 struct re_pattern_buffer
*bufp
;
2557 const char *string1
, *string2
;
2561 struct re_registers
*regs
;
2565 register char *fastmap
= bufp
->fastmap
;
2566 register char *translate
= bufp
->translate
;
2567 int total_size
= size1
+ size2
;
2568 int endpos
= startpos
+ range
;
2570 /* Check for out-of-range STARTPOS. */
2571 if (startpos
< 0 || startpos
> total_size
)
2574 /* Fix up RANGE if it might eventually take us outside
2575 * the virtual concatenation of STRING1 and STRING2. */
2577 range
= -1 - startpos
;
2578 else if (endpos
> total_size
)
2579 range
= total_size
- startpos
;
2581 /* If the search isn't to be a backwards one, don't waste time in a
2582 * search for a pattern that must be anchored. */
2583 if (bufp
->used
> 0 && (re_opcode_t
) bufp
->buffer
[0] == begbuf
&& range
> 0) {
2589 /* Update the fastmap now if not correct already. */
2590 if (fastmap
&& !bufp
->fastmap_accurate
)
2591 if (re_compile_fastmap(bufp
) == -2)
2594 /* Loop through the string, looking for a place to start matching. */
2596 /* If a fastmap is supplied, skip quickly over characters that
2597 * cannot be the start of a match. If the pattern can match the
2598 * null string, however, we don't need to skip characters; we want
2599 * the first null string. */
2600 if (fastmap
&& startpos
< total_size
&& !bufp
->can_be_null
) {
2601 if (range
> 0) { /* Searching forwards. */
2602 register const char *d
;
2603 register int lim
= 0;
2606 if (startpos
< size1
&& startpos
+ range
>= size1
)
2607 lim
= range
- (size1
- startpos
);
2609 d
= (startpos
>= size1
? string2
- size1
: string1
) + startpos
;
2611 /* Written out as an if-else to avoid testing `translate'
2612 * inside the loop. */
2615 && !fastmap
[(unsigned char)
2616 translate
[(unsigned char) *d
++]])
2619 while (range
> lim
&& !fastmap
[(unsigned char) *d
++])
2622 startpos
+= irange
- range
;
2623 } else { /* Searching backwards. */
2624 register char c
= (size1
== 0 || startpos
>= size1
2625 ? string2
[startpos
- size1
]
2626 : string1
[startpos
]);
2628 if (!fastmap
[(unsigned char) TRANSLATE(c
)])
2632 /* If can't match the null string, and that's all we have left, fail. */
2633 if (range
>= 0 && startpos
== total_size
&& fastmap
2634 && !bufp
->can_be_null
)
2637 val
= re_match_2(bufp
, string1
, size1
, string2
, size2
,
2638 startpos
, regs
, stop
);
2648 else if (range
> 0) {
2659 /* Declarations and macros for re_match_2. */
2661 /* Structure for per-register (a.k.a. per-group) information.
2662 * This must not be longer than one word, because we push this value
2663 * onto the failure stack. Other register information, such as the
2664 * starting and ending positions (which are addresses), and the list of
2665 * inner groups (which is a bits list) are maintained in separate
2668 * We are making a (strictly speaking) nonportable assumption here: that
2669 * the compiler will pack our bit fields into something that fits into
2670 * the type of `word', i.e., is something that fits into one item on the
2673 fail_stack_elt_t word
;
2675 /* This field is one if this group can match the empty string,
2676 * zero if not. If not yet determined, `MATCH_NULL_UNSET_VALUE'. */
2677 #define MATCH_NULL_UNSET_VALUE 3
2678 unsigned match_null_string_p
:2;
2679 unsigned is_active
:1;
2680 unsigned matched_something
:1;
2681 unsigned ever_matched_something
:1;
2683 } register_info_type
;
2684 static boolean
alt_match_null_string_p(unsigned char *p
, unsigned char *end
, register_info_type
*reg_info
);
2685 static boolean
common_op_match_null_string_p( unsigned char **p
, unsigned char *end
, register_info_type
*reg_info
);
2686 static int bcmp_translate(unsigned char const *s1
, unsigned char const *s2
, register int len
, char *translate
);
2687 static boolean
group_match_null_string_p(unsigned char **p
, unsigned char *end
, register_info_type
*reg_info
);
2689 #define REG_MATCH_NULL_STRING_P(R) ((R).bits.match_null_string_p)
2690 #define IS_ACTIVE(R) ((R).bits.is_active)
2691 #define MATCHED_SOMETHING(R) ((R).bits.matched_something)
2692 #define EVER_MATCHED_SOMETHING(R) ((R).bits.ever_matched_something)
2694 /* Call this when have matched a real character; it sets `matched' flags
2695 * for the subexpressions which we are currently inside. Also records
2696 * that those subexprs have matched. */
2697 #define SET_REGS_MATCHED() \
2701 for (r = lowest_active_reg; r <= highest_active_reg; r++) \
2703 MATCHED_SOMETHING (reg_info[r]) \
2704 = EVER_MATCHED_SOMETHING (reg_info[r]) \
2710 /* This converts PTR, a pointer into one of the search strings `string1'
2711 * and `string2' into an offset from the beginning of that string. */
2712 #define POINTER_TO_OFFSET(ptr) \
2713 (FIRST_STRING_P (ptr) ? (ptr) - string1 : (ptr) - string2 + size1)
2715 /* Registers are set to a sentinel when they haven't yet matched. */
2716 #define REG_UNSET_VALUE ((char *) -1)
2717 #define REG_UNSET(e) ((e) == REG_UNSET_VALUE)
2719 /* Macros for dealing with the split strings in re_match_2. */
2721 #define MATCHING_IN_FIRST_STRING (dend == end_match_1)
2723 /* Call before fetching a character with *d. This switches over to
2724 * string2 if necessary. */
2725 #define PREFETCH() \
2728 /* End of string2 => fail. */ \
2729 if (dend == end_match_2) \
2731 /* End of string1 => advance to string2. */ \
2733 dend = end_match_2; \
2736 /* Test if at very beginning or at very end of the virtual concatenation
2737 * of `string1' and `string2'. If only one string, it's `string2'. */
2738 #define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2)
2739 #define AT_STRINGS_END(d) ((d) == end2)
2741 /* Test if D points to a character which is word-constituent. We have
2742 * two special cases to check for: if past the end of string1, look at
2743 * the first character in string2; and if before the beginning of
2744 * string2, look at the last character in string1. */
2745 #define WORDCHAR_P(d) \
2746 (SYNTAX ((d) == end1 ? *string2 \
2747 : (d) == string2 - 1 ? *(end1 - 1) : *(d)) \
2750 /* Test if the character before D and the one at D differ with respect
2751 * to being word-constituent. */
2752 #define AT_WORD_BOUNDARY(d) \
2753 (AT_STRINGS_BEG (d) || AT_STRINGS_END (d) \
2754 || WORDCHAR_P (d - 1) != WORDCHAR_P (d))
2756 /* Free everything we malloc. */
2758 #define FREE_VAR(var) if (var) free (var); var = NULL
2759 #define FREE_VARIABLES() \
2761 FREE_VAR (fail_stack.stack); \
2762 FREE_VAR (regstart); \
2763 FREE_VAR (regend); \
2764 FREE_VAR (old_regstart); \
2765 FREE_VAR (old_regend); \
2766 FREE_VAR (best_regstart); \
2767 FREE_VAR (best_regend); \
2768 FREE_VAR (reg_info); \
2769 FREE_VAR (reg_dummy); \
2770 FREE_VAR (reg_info_dummy); \
2772 #else /* not REGEX_MALLOC */
2773 /* Some MIPS systems (at least) want this to free alloca'd storage. */
2774 #define FREE_VARIABLES() alloca (0)
2775 #endif /* not REGEX_MALLOC */
2777 /* These values must meet several constraints. They must not be valid
2778 * register values; since we have a limit of 255 registers (because
2779 * we use only one byte in the pattern for the register number), we can
2780 * use numbers larger than 255. They must differ by 1, because of
2781 * NUM_FAILURE_ITEMS above. And the value for the lowest register must
2782 * be larger than the value for the highest register, so we do not try
2783 * to actually save any registers when none are active. */
2784 #define NO_HIGHEST_ACTIVE_REG (1 << BYTEWIDTH)
2785 #define NO_LOWEST_ACTIVE_REG (NO_HIGHEST_ACTIVE_REG + 1)
2787 /* Matching routines. */
2789 /* re_match_2 matches the compiled pattern in BUFP against the
2790 * the (virtual) concatenation of STRING1 and STRING2 (of length SIZE1
2791 * and SIZE2, respectively). We start matching at POS, and stop
2794 * If REGS is non-null and the `no_sub' field of BUFP is nonzero, we
2795 * store offsets for the substring each group matched in REGS. See the
2796 * documentation for exactly how many groups we fill.
2798 * We return -1 if no match, -2 if an internal error (such as the
2799 * failure stack overflowing). Otherwise, we return the length of the
2800 * matched substring. */
2803 re_match_2(bufp
, string1
, size1
, string2
, size2
, pos
, regs
, stop
)
2804 struct re_pattern_buffer
*bufp
;
2805 const char *string1
, *string2
;
2808 struct re_registers
*regs
;
2811 /* General temporaries. */
2815 /* Just past the end of the corresponding string. */
2816 const char *end1
, *end2
;
2818 /* Pointers into string1 and string2, just past the last characters in
2819 * each to consider matching. */
2820 const char *end_match_1
, *end_match_2
;
2822 /* Where we are in the data, and the end of the current string. */
2823 const char *d
, *dend
;
2825 /* Where we are in the pattern, and the end of the pattern. */
2826 unsigned char *p
= bufp
->buffer
;
2827 register unsigned char *pend
= p
+ bufp
->used
;
2829 /* We use this to map every character in the string. */
2830 char *translate
= bufp
->translate
;
2832 /* Failure point stack. Each place that can handle a failure further
2833 * down the line pushes a failure point on this stack. It consists of
2834 * restart, regend, and reg_info for all registers corresponding to
2835 * the subexpressions we're currently inside, plus the number of such
2836 * registers, and, finally, two char *'s. The first char * is where
2837 * to resume scanning the pattern; the second one is where to resume
2838 * scanning the strings. If the latter is zero, the failure point is
2839 * a ``dummy''; if a failure happens and the failure point is a dummy,
2840 * it gets discarded and the next next one is tried. */
2841 fail_stack_type fail_stack
;
2843 static unsigned failure_id
= 0;
2844 unsigned nfailure_points_pushed
= 0, nfailure_points_popped
= 0;
2847 /* We fill all the registers internally, independent of what we
2848 * return, for use in backreferences. The number here includes
2849 * an element for register zero. */
2850 unsigned num_regs
= bufp
->re_nsub
+ 1;
2852 /* The currently active registers. */
2853 unsigned long lowest_active_reg
= NO_LOWEST_ACTIVE_REG
;
2854 unsigned long highest_active_reg
= NO_HIGHEST_ACTIVE_REG
;
2856 /* Information on the contents of registers. These are pointers into
2857 * the input strings; they record just what was matched (on this
2858 * attempt) by a subexpression part of the pattern, that is, the
2859 * regnum-th regstart pointer points to where in the pattern we began
2860 * matching and the regnum-th regend points to right after where we
2861 * stopped matching the regnum-th subexpression. (The zeroth register
2862 * keeps track of what the whole pattern matches.) */
2863 const char **regstart
= NULL
, **regend
= NULL
;
2865 /* If a group that's operated upon by a repetition operator fails to
2866 * match anything, then the register for its start will need to be
2867 * restored because it will have been set to wherever in the string we
2868 * are when we last see its open-group operator. Similarly for a
2869 * register's end. */
2870 const char **old_regstart
= NULL
, **old_regend
= NULL
;
2872 /* The is_active field of reg_info helps us keep track of which (possibly
2873 * nested) subexpressions we are currently in. The matched_something
2874 * field of reg_info[reg_num] helps us tell whether or not we have
2875 * matched any of the pattern so far this time through the reg_num-th
2876 * subexpression. These two fields get reset each time through any
2877 * loop their register is in. */
2878 register_info_type
*reg_info
= NULL
;
2880 /* The following record the register info as found in the above
2881 * variables when we find a match better than any we've seen before.
2882 * This happens as we backtrack through the failure points, which in
2883 * turn happens only if we have not yet matched the entire string. */
2884 unsigned best_regs_set
= false;
2885 const char **best_regstart
= NULL
, **best_regend
= NULL
;
2887 /* Logically, this is `best_regend[0]'. But we don't want to have to
2888 * allocate space for that if we're not allocating space for anything
2889 * else (see below). Also, we never need info about register 0 for
2890 * any of the other register vectors, and it seems rather a kludge to
2891 * treat `best_regend' differently than the rest. So we keep track of
2892 * the end of the best match so far in a separate variable. We
2893 * initialize this to NULL so that when we backtrack the first time
2894 * and need to test it, it's not garbage. */
2895 const char *match_end
= NULL
;
2897 /* Used when we pop values we don't care about. */
2898 const char **reg_dummy
= NULL
;
2899 register_info_type
*reg_info_dummy
= NULL
;
2902 /* Counts the total number of registers pushed. */
2903 unsigned num_regs_pushed
= 0;
2906 DEBUG_PRINT1("\n\nEntering re_match_2.\n");
2910 /* Do not bother to initialize all the register variables if there are
2911 * no groups in the pattern, as it takes a fair amount of time. If
2912 * there are groups, we include space for register 0 (the whole
2913 * pattern), even though we never use it, since it simplifies the
2914 * array indexing. We should fix this. */
2915 if (bufp
->re_nsub
) {
2916 regstart
= REGEX_TALLOC(num_regs
, const char *);
2917 regend
= REGEX_TALLOC(num_regs
, const char *);
2918 old_regstart
= REGEX_TALLOC(num_regs
, const char *);
2919 old_regend
= REGEX_TALLOC(num_regs
, const char *);
2920 best_regstart
= REGEX_TALLOC(num_regs
, const char *);
2921 best_regend
= REGEX_TALLOC(num_regs
, const char *);
2922 reg_info
= REGEX_TALLOC(num_regs
, register_info_type
);
2923 reg_dummy
= REGEX_TALLOC(num_regs
, const char *);
2924 reg_info_dummy
= REGEX_TALLOC(num_regs
, register_info_type
);
2926 if (!(regstart
&& regend
&& old_regstart
&& old_regend
&& reg_info
2927 && best_regstart
&& best_regend
&& reg_dummy
&& reg_info_dummy
)) {
2934 /* We must initialize all our variables to NULL, so that
2935 * `FREE_VARIABLES' doesn't try to free them. */
2936 regstart
= regend
= old_regstart
= old_regend
= best_regstart
2937 = best_regend
= reg_dummy
= NULL
;
2938 reg_info
= reg_info_dummy
= (register_info_type
*) NULL
;
2940 #endif /* REGEX_MALLOC */
2942 /* The starting position is bogus. */
2943 if (pos
< 0 || pos
> size1
+ size2
) {
2947 /* Initialize subexpression text positions to -1 to mark ones that no
2948 * start_memory/stop_memory has been seen for. Also initialize the
2949 * register information struct. */
2950 for (mcnt
= 1; mcnt
< num_regs
; mcnt
++) {
2951 regstart
[mcnt
] = regend
[mcnt
]
2952 = old_regstart
[mcnt
] = old_regend
[mcnt
] = REG_UNSET_VALUE
;
2954 REG_MATCH_NULL_STRING_P(reg_info
[mcnt
]) = MATCH_NULL_UNSET_VALUE
;
2955 IS_ACTIVE(reg_info
[mcnt
]) = 0;
2956 MATCHED_SOMETHING(reg_info
[mcnt
]) = 0;
2957 EVER_MATCHED_SOMETHING(reg_info
[mcnt
]) = 0;
2960 /* We move `string1' into `string2' if the latter's empty -- but not if
2961 * `string1' is null. */
2962 if (size2
== 0 && string1
!= NULL
) {
2968 end1
= string1
+ size1
;
2969 end2
= string2
+ size2
;
2971 /* Compute where to stop matching, within the two strings. */
2972 if (stop
<= size1
) {
2973 end_match_1
= string1
+ stop
;
2974 end_match_2
= string2
;
2977 end_match_2
= string2
+ stop
- size1
;
2980 /* `p' scans through the pattern as `d' scans through the data.
2981 * `dend' is the end of the input string that `d' points within. `d'
2982 * is advanced into the following input string whenever necessary, but
2983 * this happens before fetching; therefore, at the beginning of the
2984 * loop, `d' can be pointing at the end of a string, but it cannot
2985 * equal `string2'. */
2986 if (size1
> 0 && pos
<= size1
) {
2990 d
= string2
+ pos
- size1
;
2994 DEBUG_PRINT1("The compiled pattern is: ");
2995 DEBUG_PRINT_COMPILED_PATTERN(bufp
, p
, pend
);
2996 DEBUG_PRINT1("The string to match is: `");
2997 DEBUG_PRINT_DOUBLE_STRING(d
, string1
, size1
, string2
, size2
);
2998 DEBUG_PRINT1("'\n");
3000 /* This loops over pattern commands. It exits by returning from the
3001 * function if the match is complete, or it drops through if the match
3002 * fails at this starting point in the input data. */
3004 DEBUG_PRINT2("\n0x%x: ", p
);
3006 if (p
== pend
) { /* End of pattern means we might have succeeded. */
3007 DEBUG_PRINT1("end of pattern ... ");
3009 /* If we haven't matched the entire string, and we want the
3010 * longest match, try backtracking. */
3011 if (d
!= end_match_2
) {
3012 DEBUG_PRINT1("backtracking.\n");
3014 if (!FAIL_STACK_EMPTY()) { /* More failure points to try. */
3015 boolean same_str_p
= (FIRST_STRING_P(match_end
)
3016 == MATCHING_IN_FIRST_STRING
);
3018 /* If exceeds best match so far, save it. */
3020 || (same_str_p
&& d
> match_end
)
3021 || (!same_str_p
&& !MATCHING_IN_FIRST_STRING
)) {
3022 best_regs_set
= true;
3025 DEBUG_PRINT1("\nSAVING match as best so far.\n");
3027 for (mcnt
= 1; mcnt
< num_regs
; mcnt
++) {
3028 best_regstart
[mcnt
] = regstart
[mcnt
];
3029 best_regend
[mcnt
] = regend
[mcnt
];
3034 /* If no failure points, don't restore garbage. */
3035 else if (best_regs_set
) {
3037 /* Restore best match. It may happen that `dend ==
3038 * end_match_1' while the restored d is in string2.
3039 * For example, the pattern `x.*y.*z' against the
3040 * strings `x-' and `y-z-', if the two strings are
3041 * not consecutive in memory. */
3042 DEBUG_PRINT1("Restoring best registers.\n");
3045 dend
= ((d
>= string1
&& d
<= end1
)
3046 ? end_match_1
: end_match_2
);
3048 for (mcnt
= 1; mcnt
< num_regs
; mcnt
++) {
3049 regstart
[mcnt
] = best_regstart
[mcnt
];
3050 regend
[mcnt
] = best_regend
[mcnt
];
3053 } /* d != end_match_2 */
3054 DEBUG_PRINT1("Accepting match.\n");
3056 /* If caller wants register contents data back, do it. */
3057 if (regs
&& !bufp
->no_sub
) {
3058 /* Have the register data arrays been allocated? */
3059 if (bufp
->regs_allocated
== REGS_UNALLOCATED
) { /* No. So allocate them with malloc. We need one
3060 * extra element beyond `num_regs' for the `-1' marker
3062 regs
->num_regs
= max(RE_NREGS
, num_regs
+ 1);
3063 regs
->start
= TALLOC(regs
->num_regs
, regoff_t
);
3064 regs
->end
= TALLOC(regs
->num_regs
, regoff_t
);
3065 if (regs
->start
== NULL
|| regs
->end
== NULL
)
3067 bufp
->regs_allocated
= REGS_REALLOCATE
;
3068 } else if (bufp
->regs_allocated
== REGS_REALLOCATE
) { /* Yes. If we need more elements than were already
3069 * allocated, reallocate them. If we need fewer, just
3070 * leave it alone. */
3071 if (regs
->num_regs
< num_regs
+ 1) {
3072 regs
->num_regs
= num_regs
+ 1;
3073 RETALLOC(regs
->start
, regs
->num_regs
, regoff_t
);
3074 RETALLOC(regs
->end
, regs
->num_regs
, regoff_t
);
3075 if (regs
->start
== NULL
|| regs
->end
== NULL
)
3079 assert(bufp
->regs_allocated
== REGS_FIXED
);
3081 /* Convert the pointer data in `regstart' and `regend' to
3082 * indices. Register zero has to be set differently,
3083 * since we haven't kept track of any info for it. */
3084 if (regs
->num_regs
> 0) {
3085 regs
->start
[0] = pos
;
3086 regs
->end
[0] = (MATCHING_IN_FIRST_STRING
? d
- string1
3087 : d
- string2
+ size1
);
3089 /* Go through the first `min (num_regs, regs->num_regs)'
3090 * registers, since that is all we initialized. */
3091 for (mcnt
= 1; mcnt
< min(num_regs
, regs
->num_regs
); mcnt
++) {
3092 if (REG_UNSET(regstart
[mcnt
]) || REG_UNSET(regend
[mcnt
]))
3093 regs
->start
[mcnt
] = regs
->end
[mcnt
] = -1;
3095 regs
->start
[mcnt
] = POINTER_TO_OFFSET(regstart
[mcnt
]);
3096 regs
->end
[mcnt
] = POINTER_TO_OFFSET(regend
[mcnt
]);
3100 /* If the regs structure we return has more elements than
3101 * were in the pattern, set the extra elements to -1. If
3102 * we (re)allocated the registers, this is the case,
3103 * because we always allocate enough to have at least one
3105 for (mcnt
= num_regs
; mcnt
< regs
->num_regs
; mcnt
++)
3106 regs
->start
[mcnt
] = regs
->end
[mcnt
] = -1;
3107 } /* regs && !bufp->no_sub */
3109 DEBUG_PRINT4("%u failure points pushed, %u popped (%u remain).\n",
3110 nfailure_points_pushed
, nfailure_points_popped
,
3111 nfailure_points_pushed
- nfailure_points_popped
);
3112 DEBUG_PRINT2("%u registers pushed.\n", num_regs_pushed
);
3114 mcnt
= d
- pos
- (MATCHING_IN_FIRST_STRING
3118 DEBUG_PRINT2("Returning %d from re_match_2.\n", mcnt
);
3122 /* Otherwise match next pattern command. */
3123 #ifdef SWITCH_ENUM_BUG
3124 switch ((int) ((re_opcode_t
) * p
++))
3126 switch ((re_opcode_t
) * p
++)
3129 /* Ignore these. Used to ignore the n of succeed_n's which
3130 * currently have n == 0. */
3132 DEBUG_PRINT1("EXECUTING no_op.\n");
3135 /* Match the next n pattern characters exactly. The following
3136 * byte in the pattern defines n, and the n bytes after that
3137 * are the characters to match. */
3140 DEBUG_PRINT2("EXECUTING exactn %d.\n", mcnt
);
3142 /* This is written out as an if-else so we don't waste time
3143 * testing `translate' inside the loop. */
3147 if (translate
[(unsigned char) *d
++] != (char) *p
++)
3153 if (*d
++ != (char) *p
++)
3160 /* Match any character except possibly a newline or a null. */
3162 DEBUG_PRINT1("EXECUTING anychar.\n");
3166 if ((!(bufp
->syntax
& RE_DOT_NEWLINE
) && TRANSLATE(*d
) == '\n')
3167 || (bufp
->syntax
& RE_DOT_NOT_NULL
&& TRANSLATE(*d
) == '\000'))
3171 DEBUG_PRINT2(" Matched `%d'.\n", *d
);
3177 register unsigned char c
;
3178 boolean
not = (re_opcode_t
) * (p
- 1) == charset_not
;
3180 DEBUG_PRINT2("EXECUTING charset%s.\n", not ? "_not" : "");
3183 c
= TRANSLATE(*d
); /* The character to match. */
3185 /* Cast to `unsigned' instead of `unsigned char' in case the
3186 * bit list is a full 32 bytes long. */
3187 if (c
< (unsigned) (*p
* BYTEWIDTH
)
3188 && p
[1 + c
/ BYTEWIDTH
] & (1 << (c
% BYTEWIDTH
)))
3201 /* The beginning of a group is represented by start_memory.
3202 * The arguments are the register number in the next byte, and the
3203 * number of groups inner to this one in the next. The text
3204 * matched within the group is recorded (in the internal
3205 * registers data structure) under the register number. */
3207 DEBUG_PRINT3("EXECUTING start_memory %d (%d):\n", *p
, p
[1]);
3209 /* Find out if this group can match the empty string. */
3210 p1
= p
; /* To send to group_match_null_string_p. */
3212 if (REG_MATCH_NULL_STRING_P(reg_info
[*p
]) == MATCH_NULL_UNSET_VALUE
)
3213 REG_MATCH_NULL_STRING_P(reg_info
[*p
])
3214 = group_match_null_string_p(&p1
, pend
, reg_info
);
3216 /* Save the position in the string where we were the last time
3217 * we were at this open-group operator in case the group is
3218 * operated upon by a repetition operator, e.g., with `(a*)*b'
3219 * against `ab'; then we want to ignore where we are now in
3220 * the string in case this attempt to match fails. */
3221 old_regstart
[*p
] = REG_MATCH_NULL_STRING_P(reg_info
[*p
])
3222 ? REG_UNSET(regstart
[*p
]) ? d
: regstart
[*p
]
3224 DEBUG_PRINT2(" old_regstart: %d\n",
3225 POINTER_TO_OFFSET(old_regstart
[*p
]));
3228 DEBUG_PRINT2(" regstart: %d\n", POINTER_TO_OFFSET(regstart
[*p
]));
3230 IS_ACTIVE(reg_info
[*p
]) = 1;
3231 MATCHED_SOMETHING(reg_info
[*p
]) = 0;
3233 /* This is the new highest active register. */
3234 highest_active_reg
= *p
;
3236 /* If nothing was active before, this is the new lowest active
3238 if (lowest_active_reg
== NO_LOWEST_ACTIVE_REG
)
3239 lowest_active_reg
= *p
;
3241 /* Move past the register number and inner group count. */
3245 /* The stop_memory opcode represents the end of a group. Its
3246 * arguments are the same as start_memory's: the register
3247 * number, and the number of inner groups. */
3249 DEBUG_PRINT3("EXECUTING stop_memory %d (%d):\n", *p
, p
[1]);
3251 /* We need to save the string position the last time we were at
3252 * this close-group operator in case the group is operated
3253 * upon by a repetition operator, e.g., with `((a*)*(b*)*)*'
3254 * against `aba'; then we want to ignore where we are now in
3255 * the string in case this attempt to match fails. */
3256 old_regend
[*p
] = REG_MATCH_NULL_STRING_P(reg_info
[*p
])
3257 ? REG_UNSET(regend
[*p
]) ? d
: regend
[*p
]
3259 DEBUG_PRINT2(" old_regend: %d\n",
3260 POINTER_TO_OFFSET(old_regend
[*p
]));
3263 DEBUG_PRINT2(" regend: %d\n", POINTER_TO_OFFSET(regend
[*p
]));
3265 /* This register isn't active anymore. */
3266 IS_ACTIVE(reg_info
[*p
]) = 0;
3268 /* If this was the only register active, nothing is active
3270 if (lowest_active_reg
== highest_active_reg
) {
3271 lowest_active_reg
= NO_LOWEST_ACTIVE_REG
;
3272 highest_active_reg
= NO_HIGHEST_ACTIVE_REG
;
3273 } else { /* We must scan for the new highest active register, since
3274 * it isn't necessarily one less than now: consider
3275 * (a(b)c(d(e)f)g). When group 3 ends, after the f), the
3276 * new highest active register is 1. */
3277 unsigned char r
= *p
- 1;
3278 while (r
> 0 && !IS_ACTIVE(reg_info
[r
]))
3281 /* If we end up at register zero, that means that we saved
3282 * the registers as the result of an `on_failure_jump', not
3283 * a `start_memory', and we jumped to past the innermost
3284 * `stop_memory'. For example, in ((.)*) we save
3285 * registers 1 and 2 as a result of the *, but when we pop
3286 * back to the second ), we are at the stop_memory 1.
3287 * Thus, nothing is active. */
3289 lowest_active_reg
= NO_LOWEST_ACTIVE_REG
;
3290 highest_active_reg
= NO_HIGHEST_ACTIVE_REG
;
3292 highest_active_reg
= r
;
3295 /* If just failed to match something this time around with a
3296 * group that's operated on by a repetition operator, try to
3297 * force exit from the ``loop'', and restore the register
3298 * information for this group that we had before trying this
3300 if ((!MATCHED_SOMETHING(reg_info
[*p
])
3301 || (re_opcode_t
) p
[-3] == start_memory
)
3302 && (p
+ 2) < pend
) {
3303 boolean is_a_jump_n
= false;
3307 switch ((re_opcode_t
) * p1
++) {
3310 case pop_failure_jump
:
3311 case maybe_pop_jump
:
3313 case dummy_failure_jump
:
3314 EXTRACT_NUMBER_AND_INCR(mcnt
, p1
);
3325 /* If the next operation is a jump backwards in the pattern
3326 * to an on_failure_jump right before the start_memory
3327 * corresponding to this stop_memory, exit from the loop
3328 * by forcing a failure after pushing on the stack the
3329 * on_failure_jump's jump in the pattern, and d. */
3330 if (mcnt
< 0 && (re_opcode_t
) * p1
== on_failure_jump
3331 && (re_opcode_t
) p1
[3] == start_memory
&& p1
[4] == *p
) {
3332 /* If this group ever matched anything, then restore
3333 * what its registers were before trying this last
3334 * failed match, e.g., with `(a*)*b' against `ab' for
3335 * regstart[1], and, e.g., with `((a*)*(b*)*)*'
3336 * against `aba' for regend[3].
3338 * Also restore the registers for inner groups for,
3339 * e.g., `((a*)(b*))*' against `aba' (register 3 would
3340 * otherwise get trashed). */
3342 if (EVER_MATCHED_SOMETHING(reg_info
[*p
])) {
3345 EVER_MATCHED_SOMETHING(reg_info
[*p
]) = 0;
3347 /* Restore this and inner groups' (if any) registers. */
3348 for (r
= *p
; r
< *p
+ *(p
+ 1); r
++) {
3349 regstart
[r
] = old_regstart
[r
];
3351 /* xx why this test? */
3352 if ((long) old_regend
[r
] >= (long) regstart
[r
])
3353 regend
[r
] = old_regend
[r
];
3357 EXTRACT_NUMBER_AND_INCR(mcnt
, p1
);
3358 PUSH_FAILURE_POINT(p1
+ mcnt
, d
, -2);
3363 /* Move past the register number and the inner group count. */
3367 /* \<digit> has been turned into a `duplicate' command which is
3368 * followed by the numeric value of <digit> as the register number. */
3370 register const char *d2
, *dend2
;
3371 int regno
= *p
++; /* Get which register to match against. */
3372 DEBUG_PRINT2("EXECUTING duplicate %d.\n", regno
);
3374 /* Can't back reference a group which we've never matched. */
3375 if (REG_UNSET(regstart
[regno
]) || REG_UNSET(regend
[regno
]))
3378 /* Where in input to try to start matching. */
3379 d2
= regstart
[regno
];
3381 /* Where to stop matching; if both the place to start and
3382 * the place to stop matching are in the same string, then
3383 * set to the place to stop, otherwise, for now have to use
3384 * the end of the first string. */
3386 dend2
= ((FIRST_STRING_P(regstart
[regno
])
3387 == FIRST_STRING_P(regend
[regno
]))
3388 ? regend
[regno
] : end_match_1
);
3390 /* If necessary, advance to next segment in register
3392 while (d2
== dend2
) {
3393 if (dend2
== end_match_2
)
3395 if (dend2
== regend
[regno
])
3398 /* End of string1 => advance to string2. */
3400 dend2
= regend
[regno
];
3402 /* At end of register contents => success */
3406 /* If necessary, advance to next segment in data. */
3409 /* How many characters left in this segment to match. */
3412 /* Want how many consecutive characters we can match in
3413 * one shot, so, if necessary, adjust the count. */
3414 if (mcnt
> dend2
- d2
)
3417 /* Compare that many; failure if mismatch, else move
3420 ? bcmp_translate((unsigned char *)d
, (unsigned char *)d2
, mcnt
, translate
)
3421 : memcmp(d
, d2
, mcnt
))
3423 d
+= mcnt
, d2
+= mcnt
;
3428 /* begline matches the empty string at the beginning of the string
3429 * (unless `not_bol' is set in `bufp'), and, if
3430 * `newline_anchor' is set, after newlines. */
3432 DEBUG_PRINT1("EXECUTING begline.\n");
3434 if (AT_STRINGS_BEG(d
)) {
3437 } else if (d
[-1] == '\n' && bufp
->newline_anchor
) {
3440 /* In all other cases, we fail. */
3443 /* endline is the dual of begline. */
3445 DEBUG_PRINT1("EXECUTING endline.\n");
3447 if (AT_STRINGS_END(d
)) {
3451 /* We have to ``prefetch'' the next character. */
3452 else if ((d
== end1
? *string2
: *d
) == '\n'
3453 && bufp
->newline_anchor
) {
3458 /* Match at the very beginning of the data. */
3460 DEBUG_PRINT1("EXECUTING begbuf.\n");
3461 if (AT_STRINGS_BEG(d
))
3465 /* Match at the very end of the data. */
3467 DEBUG_PRINT1("EXECUTING endbuf.\n");
3468 if (AT_STRINGS_END(d
))
3472 /* on_failure_keep_string_jump is used to optimize `.*\n'. It
3473 * pushes NULL as the value for the string on the stack. Then
3474 * `pop_failure_point' will keep the current value for the
3475 * string, instead of restoring it. To see why, consider
3476 * matching `foo\nbar' against `.*\n'. The .* matches the foo;
3477 * then the . fails against the \n. But the next thing we want
3478 * to do is match the \n against the \n; if we restored the
3479 * string value, we would be back at the foo.
3481 * Because this is used only in specific cases, we don't need to
3482 * check all the things that `on_failure_jump' does, to make
3483 * sure the right things get saved on the stack. Hence we don't
3484 * share its code. The only reason to push anything on the
3485 * stack at all is that otherwise we would have to change
3486 * `anychar's code to do something besides goto fail in this
3487 * case; that seems worse than this. */
3488 case on_failure_keep_string_jump
:
3489 DEBUG_PRINT1("EXECUTING on_failure_keep_string_jump");
3491 EXTRACT_NUMBER_AND_INCR(mcnt
, p
);
3492 DEBUG_PRINT3(" %d (to 0x%x):\n", mcnt
, p
+ mcnt
);
3494 PUSH_FAILURE_POINT(p
+ mcnt
, NULL
, -2);
3497 /* Uses of on_failure_jump:
3499 * Each alternative starts with an on_failure_jump that points
3500 * to the beginning of the next alternative. Each alternative
3501 * except the last ends with a jump that in effect jumps past
3502 * the rest of the alternatives. (They really jump to the
3503 * ending jump of the following alternative, because tensioning
3504 * these jumps is a hassle.)
3506 * Repeats start with an on_failure_jump that points past both
3507 * the repetition text and either the following jump or
3508 * pop_failure_jump back to this on_failure_jump. */
3509 case on_failure_jump
:
3511 DEBUG_PRINT1("EXECUTING on_failure_jump");
3513 EXTRACT_NUMBER_AND_INCR(mcnt
, p
);
3514 DEBUG_PRINT3(" %d (to 0x%x)", mcnt
, p
+ mcnt
);
3516 /* If this on_failure_jump comes right before a group (i.e.,
3517 * the original * applied to a group), save the information
3518 * for that group and all inner ones, so that if we fail back
3519 * to this point, the group's information will be correct.
3520 * For example, in \(a*\)*\1, we need the preceding group,
3521 * and in \(\(a*\)b*\)\2, we need the inner group. */
3523 /* We can't use `p' to check ahead because we push
3524 * a failure point to `p + mcnt' after we do this. */
3527 /* We need to skip no_op's before we look for the
3528 * start_memory in case this on_failure_jump is happening as
3529 * the result of a completed succeed_n, as in \(a\)\{1,3\}b\1
3531 while (p1
< pend
&& (re_opcode_t
) * p1
== no_op
)
3534 if (p1
< pend
&& (re_opcode_t
) * p1
== start_memory
) {
3535 /* We have a new highest active register now. This will
3536 * get reset at the start_memory we are about to get to,
3537 * but we will have saved all the registers relevant to
3538 * this repetition op, as described above. */
3539 highest_active_reg
= *(p1
+ 1) + *(p1
+ 2);
3540 if (lowest_active_reg
== NO_LOWEST_ACTIVE_REG
)
3541 lowest_active_reg
= *(p1
+ 1);
3543 DEBUG_PRINT1(":\n");
3544 PUSH_FAILURE_POINT(p
+ mcnt
, d
, -2);
3547 /* A smart repeat ends with `maybe_pop_jump'.
3548 * We change it to either `pop_failure_jump' or `jump'. */
3549 case maybe_pop_jump
:
3550 EXTRACT_NUMBER_AND_INCR(mcnt
, p
);
3551 DEBUG_PRINT2("EXECUTING maybe_pop_jump %d.\n", mcnt
);
3553 register unsigned char *p2
= p
;
3555 /* Compare the beginning of the repeat with what in the
3556 * pattern follows its end. If we can establish that there
3557 * is nothing that they would both match, i.e., that we
3558 * would have to backtrack because of (as in, e.g., `a*a')
3559 * then we can change to pop_failure_jump, because we'll
3560 * never have to backtrack.
3562 * This is not true in the case of alternatives: in
3563 * `(a|ab)*' we do need to backtrack to the `ab' alternative
3564 * (e.g., if the string was `ab'). But instead of trying to
3565 * detect that here, the alternative has put on a dummy
3566 * failure point which is what we will end up popping. */
3568 /* Skip over open/close-group commands. */
3569 while (p2
+ 2 < pend
3570 && ((re_opcode_t
) * p2
== stop_memory
3571 || (re_opcode_t
) * p2
== start_memory
))
3572 p2
+= 3; /* Skip over args, too. */
3574 /* If we're at the end of the pattern, we can change. */
3576 /* Consider what happens when matching ":\(.*\)"
3577 * against ":/". I don't really understand this code
3579 p
[-3] = (unsigned char) pop_failure_jump
;
3581 (" End of pattern: change to `pop_failure_jump'.\n");
3582 } else if ((re_opcode_t
) * p2
== exactn
3583 || (bufp
->newline_anchor
&& (re_opcode_t
) * p2
== endline
)) {
3584 register unsigned char c
3585 = *p2
== (unsigned char) endline
? '\n' : p2
[2];
3588 /* p1[0] ... p1[2] are the `on_failure_jump' corresponding
3589 * to the `maybe_finalize_jump' of this case. Examine what
3591 if ((re_opcode_t
) p1
[3] == exactn
&& p1
[5] != c
) {
3592 p
[-3] = (unsigned char) pop_failure_jump
;
3593 DEBUG_PRINT3(" %c != %c => pop_failure_jump.\n",
3595 } else if ((re_opcode_t
) p1
[3] == charset
3596 || (re_opcode_t
) p1
[3] == charset_not
) {
3597 int not = (re_opcode_t
) p1
[3] == charset_not
;
3599 if (c
< (unsigned char) (p1
[4] * BYTEWIDTH
)
3600 && p1
[5 + c
/ BYTEWIDTH
] & (1 << (c
% BYTEWIDTH
)))
3603 /* `not' is equal to 1 if c would match, which means
3604 * that we can't change to pop_failure_jump. */
3606 p
[-3] = (unsigned char) pop_failure_jump
;
3607 DEBUG_PRINT1(" No match => pop_failure_jump.\n");
3612 p
-= 2; /* Point at relative address again. */
3613 if ((re_opcode_t
) p
[-1] != pop_failure_jump
) {
3614 p
[-1] = (unsigned char) jump
;
3615 DEBUG_PRINT1(" Match => jump.\n");
3616 goto unconditional_jump
;
3618 /* Note fall through. */
3620 /* The end of a simple repeat has a pop_failure_jump back to
3621 * its matching on_failure_jump, where the latter will push a
3622 * failure point. The pop_failure_jump takes off failure
3623 * points put on by this pop_failure_jump's matching
3624 * on_failure_jump; we got through the pattern to here from the
3625 * matching on_failure_jump, so didn't fail. */
3626 case pop_failure_jump
: {
3627 /* We need to pass separate storage for the lowest and
3628 * highest registers, even though we don't care about the
3629 * actual values. Otherwise, we will restore only one
3630 * register from the stack, since lowest will == highest in
3631 * `pop_failure_point'. */
3632 unsigned long dummy_low_reg
, dummy_high_reg
;
3633 unsigned char *pdummy
;
3636 DEBUG_PRINT1("EXECUTING pop_failure_jump.\n");
3637 POP_FAILURE_POINT(sdummy
, pdummy
,
3638 dummy_low_reg
, dummy_high_reg
,
3639 reg_dummy
, reg_dummy
, reg_info_dummy
);
3640 /* avoid GCC 4.6 set but unused variables warning. Does not matter here. */
3641 if (pdummy
|| sdummy
)
3644 /* Note fall through. */
3646 /* Unconditionally jump (without popping any failure points). */
3649 EXTRACT_NUMBER_AND_INCR(mcnt
, p
); /* Get the amount to jump. */
3650 DEBUG_PRINT2("EXECUTING jump %d ", mcnt
);
3651 p
+= mcnt
; /* Do the jump. */
3652 DEBUG_PRINT2("(to 0x%x).\n", p
);
3655 /* We need this opcode so we can detect where alternatives end
3656 * in `group_match_null_string_p' et al. */
3658 DEBUG_PRINT1("EXECUTING jump_past_alt.\n");
3659 goto unconditional_jump
;
3661 /* Normally, the on_failure_jump pushes a failure point, which
3662 * then gets popped at pop_failure_jump. We will end up at
3663 * pop_failure_jump, also, and with a pattern of, say, `a+', we
3664 * are skipping over the on_failure_jump, so we have to push
3665 * something meaningless for pop_failure_jump to pop. */
3666 case dummy_failure_jump
:
3667 DEBUG_PRINT1("EXECUTING dummy_failure_jump.\n");
3668 /* It doesn't matter what we push for the string here. What
3669 * the code at `fail' tests is the value for the pattern. */
3670 PUSH_FAILURE_POINT(0, 0, -2);
3671 goto unconditional_jump
;
3673 /* At the end of an alternative, we need to push a dummy failure
3674 * point in case we are followed by a `pop_failure_jump', because
3675 * we don't want the failure point for the alternative to be
3676 * popped. For example, matching `(a|ab)*' against `aab'
3677 * requires that we match the `ab' alternative. */
3678 case push_dummy_failure
:
3679 DEBUG_PRINT1("EXECUTING push_dummy_failure.\n");
3680 /* See comments just above at `dummy_failure_jump' about the
3682 PUSH_FAILURE_POINT(0, 0, -2);
3685 /* Have to succeed matching what follows at least n times.
3686 * After that, handle like `on_failure_jump'. */
3688 EXTRACT_NUMBER(mcnt
, p
+ 2);
3689 DEBUG_PRINT2("EXECUTING succeed_n %d.\n", mcnt
);
3692 /* Originally, this is how many times we HAVE to succeed. */
3696 STORE_NUMBER_AND_INCR(p
, mcnt
);
3697 DEBUG_PRINT3(" Setting 0x%x to %d.\n", p
, mcnt
);
3698 } else if (mcnt
== 0) {
3699 DEBUG_PRINT2(" Setting two bytes from 0x%x to no_op.\n", p
+ 2);
3700 p
[2] = (unsigned char) no_op
;
3701 p
[3] = (unsigned char) no_op
;
3707 EXTRACT_NUMBER(mcnt
, p
+ 2);
3708 DEBUG_PRINT2("EXECUTING jump_n %d.\n", mcnt
);
3710 /* Originally, this is how many times we CAN jump. */
3713 STORE_NUMBER(p
+ 2, mcnt
);
3714 goto unconditional_jump
;
3716 /* If don't have to jump any more, skip over the rest of command. */
3721 case set_number_at
: {
3722 DEBUG_PRINT1("EXECUTING set_number_at.\n");
3724 EXTRACT_NUMBER_AND_INCR(mcnt
, p
);
3726 EXTRACT_NUMBER_AND_INCR(mcnt
, p
);
3727 DEBUG_PRINT3(" Setting 0x%x to %d.\n", p1
, mcnt
);
3728 STORE_NUMBER(p1
, mcnt
);
3733 DEBUG_PRINT1("EXECUTING wordbound.\n");
3734 if (AT_WORD_BOUNDARY(d
))
3739 DEBUG_PRINT1("EXECUTING notwordbound.\n");
3740 if (AT_WORD_BOUNDARY(d
))
3745 DEBUG_PRINT1("EXECUTING wordbeg.\n");
3746 if (WORDCHAR_P(d
) && (AT_STRINGS_BEG(d
) || !WORDCHAR_P(d
- 1)))
3751 DEBUG_PRINT1("EXECUTING wordend.\n");
3752 if (!AT_STRINGS_BEG(d
) && WORDCHAR_P(d
- 1)
3753 && (!WORDCHAR_P(d
) || AT_STRINGS_END(d
)))
3758 DEBUG_PRINT1("EXECUTING non-Emacs wordchar.\n");
3767 DEBUG_PRINT1("EXECUTING non-Emacs notwordchar.\n");
3778 continue; /* Successfully executed one pattern command; keep going. */
3780 /* We goto here if a matching operation fails. */
3782 if (!FAIL_STACK_EMPTY()) { /* A restart point is known. Restore to that state. */
3783 DEBUG_PRINT1("\nFAIL:\n");
3784 POP_FAILURE_POINT(d
, p
,
3785 lowest_active_reg
, highest_active_reg
,
3786 regstart
, regend
, reg_info
);
3788 /* If this failure point is a dummy, try the next one. */
3792 /* If we failed to the end of the pattern, don't examine *p. */
3795 boolean is_a_jump_n
= false;
3797 /* If failed to a backwards jump that's part of a repetition
3798 * loop, need to pop this failure point and use the next one. */
3799 switch ((re_opcode_t
) * p
) {
3802 case maybe_pop_jump
:
3803 case pop_failure_jump
:
3806 EXTRACT_NUMBER_AND_INCR(mcnt
, p1
);
3809 if ((is_a_jump_n
&& (re_opcode_t
) * p1
== succeed_n
)
3811 && (re_opcode_t
) * p1
== on_failure_jump
))
3819 if (d
>= string1
&& d
<= end1
)
3822 break; /* Matching at this starting point really fails. */
3826 goto restore_best_regs
;
3830 return -1; /* Failure to match. */
3833 /* Subroutine definitions for re_match_2. */
3835 /* We are passed P pointing to a register number after a start_memory.
3837 * Return true if the pattern up to the corresponding stop_memory can
3838 * match the empty string, and false otherwise.
3840 * If we find the matching stop_memory, sets P to point to one past its number.
3841 * Otherwise, sets P to an undefined byte less than or equal to END.
3843 * We don't handle duplicates properly (yet). */
3846 group_match_null_string_p(unsigned char **p
, unsigned char *end
, register_info_type
*reg_info
)
3849 /* Point to after the args to the start_memory. */
3850 unsigned char *p1
= *p
+ 2;
3853 /* Skip over opcodes that can match nothing, and return true or
3854 * false, as appropriate, when we get to one that can't, or to the
3855 * matching stop_memory. */
3857 switch ((re_opcode_t
) * p1
) {
3858 /* Could be either a loop or a series of alternatives. */
3859 case on_failure_jump
:
3861 EXTRACT_NUMBER_AND_INCR(mcnt
, p1
);
3863 /* If the next operation is not a jump backwards in the
3867 /* Go through the on_failure_jumps of the alternatives,
3868 * seeing if any of the alternatives cannot match nothing.
3869 * The last alternative starts with only a jump,
3870 * whereas the rest start with on_failure_jump and end
3871 * with a jump, e.g., here is the pattern for `a|b|c':
3873 * /on_failure_jump/0/6/exactn/1/a/jump_past_alt/0/6
3874 * /on_failure_jump/0/6/exactn/1/b/jump_past_alt/0/3
3877 * So, we have to first go through the first (n-1)
3878 * alternatives and then deal with the last one separately. */
3880 /* Deal with the first (n-1) alternatives, which start
3881 * with an on_failure_jump (see above) that jumps to right
3882 * past a jump_past_alt. */
3884 while ((re_opcode_t
) p1
[mcnt
- 3] == jump_past_alt
) {
3885 /* `mcnt' holds how many bytes long the alternative
3886 * is, including the ending `jump_past_alt' and
3889 if (!alt_match_null_string_p(p1
, p1
+ mcnt
- 3,
3893 /* Move to right after this alternative, including the
3897 /* Break if it's the beginning of an n-th alternative
3898 * that doesn't begin with an on_failure_jump. */
3899 if ((re_opcode_t
) * p1
!= on_failure_jump
)
3902 /* Still have to check that it's not an n-th
3903 * alternative that starts with an on_failure_jump. */
3905 EXTRACT_NUMBER_AND_INCR(mcnt
, p1
);
3906 if ((re_opcode_t
) p1
[mcnt
- 3] != jump_past_alt
) {
3907 /* Get to the beginning of the n-th alternative. */
3913 /* Deal with the last alternative: go back and get number
3914 * of the `jump_past_alt' just before it. `mcnt' contains
3915 * the length of the alternative. */
3916 EXTRACT_NUMBER(mcnt
, p1
- 2);
3918 if (!alt_match_null_string_p(p1
, p1
+ mcnt
, reg_info
))
3921 p1
+= mcnt
; /* Get past the n-th alternative. */
3926 assert(p1
[1] == **p
);
3931 if (!common_op_match_null_string_p(&p1
, end
, reg_info
))
3934 } /* while p1 < end */
3937 } /* group_match_null_string_p */
3939 /* Similar to group_match_null_string_p, but doesn't deal with alternatives:
3940 * It expects P to be the first byte of a single alternative and END one
3941 * byte past the last. The alternative can contain groups. */
3944 alt_match_null_string_p(unsigned char *p
, unsigned char *end
, register_info_type
*reg_info
)
3947 unsigned char *p1
= p
;
3950 /* Skip over opcodes that can match nothing, and break when we get
3951 * to one that can't. */
3953 switch ((re_opcode_t
) * p1
) {
3955 case on_failure_jump
:
3957 EXTRACT_NUMBER_AND_INCR(mcnt
, p1
);
3962 if (!common_op_match_null_string_p(&p1
, end
, reg_info
))
3965 } /* while p1 < end */
3968 } /* alt_match_null_string_p */
3970 /* Deals with the ops common to group_match_null_string_p and
3971 * alt_match_null_string_p.
3973 * Sets P to one after the op and its arguments, if any. */
3976 common_op_match_null_string_p( unsigned char **p
, unsigned char *end
, register_info_type
*reg_info
)
3981 unsigned char *p1
= *p
;
3983 switch ((re_opcode_t
) * p1
++) {
3997 assert(reg_no
> 0 && reg_no
<= MAX_REGNUM
);
3998 ret
= group_match_null_string_p(&p1
, end
, reg_info
);
4000 /* Have to set this here in case we're checking a group which
4001 * contains a group and a back reference to it. */
4003 if (REG_MATCH_NULL_STRING_P(reg_info
[reg_no
]) == MATCH_NULL_UNSET_VALUE
)
4004 REG_MATCH_NULL_STRING_P(reg_info
[reg_no
]) = ret
;
4010 /* If this is an optimized succeed_n for zero times, make the jump. */
4012 EXTRACT_NUMBER_AND_INCR(mcnt
, p1
);
4020 /* Get to the number of times to succeed. */
4022 EXTRACT_NUMBER_AND_INCR(mcnt
, p1
);
4026 EXTRACT_NUMBER_AND_INCR(mcnt
, p1
);
4033 if (!REG_MATCH_NULL_STRING_P(reg_info
[*p1
]))
4041 /* All other opcodes mean we cannot match the empty string. */
4047 } /* common_op_match_null_string_p */
4049 /* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
4050 * bytes; nonzero otherwise. */
4053 bcmp_translate(unsigned char const *s1
, unsigned char const*s2
, register int len
, char *translate
)
4055 register unsigned char const *p1
= s1
, *p2
= s2
;
4057 if (translate
[*p1
++] != translate
[*p2
++])
4064 /* Entry points for GNU code. */
4066 /* POSIX.2 functions */
4068 /* regcomp takes a regular expression as a string and compiles it.
4070 * PREG is a regex_t *. We do not expect any fields to be initialized,
4071 * since POSIX says we shouldn't. Thus, we set
4073 * `buffer' to the compiled pattern;
4074 * `used' to the length of the compiled pattern;
4075 * `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
4076 * REG_EXTENDED bit in CFLAGS is set; otherwise, to
4077 * RE_SYNTAX_POSIX_BASIC;
4078 * `newline_anchor' to REG_NEWLINE being set in CFLAGS;
4079 * `fastmap' and `fastmap_accurate' to zero;
4080 * `re_nsub' to the number of subexpressions in PATTERN.
4082 * PATTERN is the address of the pattern string.
4084 * CFLAGS is a series of bits which affect compilation.
4086 * If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
4087 * use POSIX basic syntax.
4089 * If REG_NEWLINE is set, then . and [^...] don't match newline.
4090 * Also, regexec will try a match beginning after every newline.
4092 * If REG_ICASE is set, then we considers upper- and lowercase
4093 * versions of letters to be equivalent when matching.
4095 * If REG_NOSUB is set, then when PREG is passed to regexec, that
4096 * routine will report only success or failure, and nothing about the
4099 * It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
4100 * the return codes and their meanings.) */
4103 regcomp(preg
, pattern
, cflags
)
4105 const char *pattern
;
4110 = (cflags
& REG_EXTENDED
) ?
4111 RE_SYNTAX_POSIX_EXTENDED
: RE_SYNTAX_POSIX_BASIC
;
4113 /* regex_compile will allocate the space for the compiled pattern. */
4115 preg
->allocated
= 0;
4117 /* Don't bother to use a fastmap when searching. This simplifies the
4118 * REG_NEWLINE case: if we used a fastmap, we'd have to put all the
4119 * characters after newlines into the fastmap. This way, we just try
4120 * every character. */
4123 if (cflags
& REG_ICASE
) {
4126 preg
->translate
= (char *) malloc(CHAR_SET_SIZE
);
4127 if (preg
->translate
== NULL
)
4128 return (int) REG_ESPACE
;
4130 /* Map uppercase characters to corresponding lowercase ones. */
4131 for (i
= 0; i
< CHAR_SET_SIZE
; i
++)
4132 preg
->translate
[i
] = ISUPPER(i
) ? tolower(i
) : i
;
4134 preg
->translate
= NULL
;
4136 /* If REG_NEWLINE is set, newlines are treated differently. */
4137 if (cflags
& REG_NEWLINE
) { /* REG_NEWLINE implies neither . nor [^...] match newline. */
4138 syntax
&= ~RE_DOT_NEWLINE
;
4139 syntax
|= RE_HAT_LISTS_NOT_NEWLINE
;
4140 /* It also changes the matching behavior. */
4141 preg
->newline_anchor
= 1;
4143 preg
->newline_anchor
= 0;
4145 preg
->no_sub
= !!(cflags
& REG_NOSUB
);
4147 /* POSIX says a null character in the pattern terminates it, so we
4148 * can use strlen here in compiling the pattern. */
4149 ret
= regex_compile(pattern
, strlen(pattern
), syntax
, preg
);
4151 /* POSIX doesn't distinguish between an unmatched open-group and an
4152 * unmatched close-group: both are REG_EPAREN. */
4153 if (ret
== REG_ERPAREN
)
4159 /* regexec searches for a given pattern, specified by PREG, in the
4162 * If NMATCH is zero or REG_NOSUB was set in the cflags argument to
4163 * `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
4164 * least NMATCH elements, and we set them to the offsets of the
4165 * corresponding matched substrings.
4167 * EFLAGS specifies `execution flags' which affect matching: if
4168 * REG_NOTBOL is set, then ^ does not match at the beginning of the
4169 * string; if REG_NOTEOL is set, then $ does not match at the end.
4171 * We return 0 if we find a match and REG_NOMATCH if not. */
4174 regexec(preg
, string
, nmatch
, pmatch
, eflags
)
4175 const regex_t
*preg
;
4178 regmatch_t pmatch
[];
4182 struct re_registers regs
;
4183 regex_t private_preg
;
4184 int len
= strlen(string
);
4185 boolean want_reg_info
= !preg
->no_sub
&& nmatch
> 0;
4187 private_preg
= *preg
;
4189 private_preg
.not_bol
= !!(eflags
& REG_NOTBOL
);
4190 private_preg
.not_eol
= !!(eflags
& REG_NOTEOL
);
4192 /* The user has told us exactly how many registers to return
4193 * information about, via `nmatch'. We have to pass that on to the
4194 * matching routines. */
4195 private_preg
.regs_allocated
= REGS_FIXED
;
4197 if (want_reg_info
) {
4198 regs
.num_regs
= nmatch
;
4199 regs
.start
= TALLOC(nmatch
, regoff_t
);
4200 regs
.end
= TALLOC(nmatch
, regoff_t
);
4201 if (regs
.start
== NULL
|| regs
.end
== NULL
)
4202 return (int) REG_NOMATCH
;
4204 /* Perform the searching operation. */
4205 ret
= re_search(&private_preg
, string
, len
,
4206 /* start: */ 0, /* range: */ len
,
4207 want_reg_info
? ®s
: (struct re_registers
*) 0);
4209 /* Copy the register information to the POSIX structure. */
4210 if (want_reg_info
) {
4214 for (r
= 0; r
< nmatch
; r
++) {
4215 pmatch
[r
].rm_so
= regs
.start
[r
];
4216 pmatch
[r
].rm_eo
= regs
.end
[r
];
4219 /* If we needed the temporary register info, free the space now. */
4223 /* We want zero return to mean success, unlike `re_search'. */
4224 return ret
>= 0 ? (int) REG_NOERROR
: (int) REG_NOMATCH
;
4227 /* Returns a message corresponding to an error code, ERRCODE, returned
4228 * from either regcomp or regexec. We don't use PREG here. */
4231 regerror(int errcode
, const regex_t
*preg
, char *errbuf
, size_t errbuf_size
)
4237 || errcode
>= (sizeof(re_error_msg
) / sizeof(re_error_msg
[0])))
4238 /* Only error codes returned by the rest of the code should be passed
4239 * to this routine. If we are given anything else, or if other regex
4240 * code generates an invalid error code, then the program has a bug.
4241 * Dump core so we can fix it. */
4244 msg
= re_error_msg
[errcode
];
4246 /* POSIX doesn't require that we do anything in this case, but why
4251 msg_size
= strlen(msg
) + 1; /* Includes the null. */
4253 if (errbuf_size
!= 0) {
4254 if (msg_size
> errbuf_size
) {
4255 strncpy(errbuf
, msg
, errbuf_size
- 1);
4256 errbuf
[errbuf_size
- 1] = 0;
4258 strcpy(errbuf
, msg
);
4263 /* Free dynamically allocated space used by PREG. */
4269 if (preg
->buffer
!= NULL
)
4271 preg
->buffer
= NULL
;
4273 preg
->allocated
= 0;
4276 if (preg
->fastmap
!= NULL
)
4277 free(preg
->fastmap
);
4278 preg
->fastmap
= NULL
;
4279 preg
->fastmap_accurate
= 0;
4281 if (preg
->translate
!= NULL
)
4282 free(preg
->translate
);
4283 preg
->translate
= NULL
;
4285 #endif /* USE_GNUREGEX */
4289 * make-backup-files: t
4290 * version-control: t
4291 * trim-versions-without-asking: nil