1 /* Extended regular expression matching and search library,
3 (Implements POSIX draft P1003.2/D11.2, except for some of the
4 internationalization features.)
5 Copyright (C) 1993, 94, 95, 96, 97, 98 Free Software Foundation, Inc.
7 NOTE: The canonical source of this file is maintained with the
8 GNU C Library. Bugs can be reported to bug-glibc@prep.ai.mit.edu.
10 This program is free software; you can redistribute it and/or modify it
11 under the terms of the GNU General Public License as published by the
12 Free Software Foundation; either version 2, or (at your option) any
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software Foundation,
22 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 /* AIX requires this to be the first thing in the file. */
25 #if defined _AIX && !defined REGEX_MALLOC
37 # if defined __GNUC__ || (defined __STDC__ && __STDC__)
38 # define PARAMS(args) args
40 # define PARAMS(args) ()
42 #endif /* Not PARAMS. */
44 #if defined STDC_HEADERS && !defined emacs
47 /* We need this for `gnu-regex.h', and perhaps for the Emacs include files. */
48 # include <sys/types.h>
51 /* For platform which support the ISO C amendement 1 functionality we
52 support user defined character classes. */
53 #if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
54 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
59 /* This is for other GNU distributions with internationalized messages. */
60 /* CYGNUS LOCAL: ../intl will handle this for us */
64 # define gettext(msgid) (msgid)
68 /* This define is so xgettext can find the internationalizable
70 # define gettext_noop(String) String
73 /* The `emacs' switch turns on certain matching commands
74 that make sense only in Emacs. */
83 /* If we are not linking with Emacs proper,
84 we can't use the relocating allocator
85 even if config.h says that we can. */
88 # if defined STDC_HEADERS || defined _LIBC
95 /* When used in Emacs's lib-src, we need to get bzero and bcopy somehow.
96 If nothing else has been done, use the method below. */
97 # ifdef INHIBIT_STRING_HEADER
98 # if !(defined HAVE_BZERO && defined HAVE_BCOPY)
99 # if !defined bzero && !defined bcopy
100 # undef INHIBIT_STRING_HEADER
105 /* This is the normal way of making sure we have a bcopy and a bzero.
106 This is used in most programs--a few other programs avoid this
107 by defining INHIBIT_STRING_HEADER. */
108 # ifndef INHIBIT_STRING_HEADER
109 # if defined HAVE_STRING_H || defined STDC_HEADERS || defined _LIBC
113 # define bzero(s, n) (memset (s, '\0', n), (s))
115 # define bzero(s, n) __bzero (s, n)
119 # include <strings.h>
121 # define memcmp(s1, s2, n) bcmp (s1, s2, n)
124 # define memcpy(d, s, n) (bcopy (s, d, n), (d))
129 /* Define the syntax stuff for \<, \>, etc. */
131 /* This must be nonzero for the wordchar and notwordchar pattern
132 commands in re_match_2. */
137 # ifdef SWITCH_ENUM_BUG
138 # define SWITCH_ENUM_CAST(x) ((int)(x))
140 # define SWITCH_ENUM_CAST(x) (x)
143 /* How many characters in the character set. */
144 # define CHAR_SET_SIZE 256
146 /* GDB LOCAL: define _REGEX_RE_COMP to get BSD style re_comp and re_exec */
147 #ifndef _REGEX_RE_COMP
148 #define _REGEX_RE_COMP
153 extern char *re_syntax_table
;
155 # else /* not SYNTAX_TABLE */
157 static char re_syntax_table
[CHAR_SET_SIZE
];
168 bzero (re_syntax_table
, sizeof re_syntax_table
);
170 for (c
= 'a'; c
<= 'z'; c
++)
171 re_syntax_table
[c
] = Sword
;
173 for (c
= 'A'; c
<= 'Z'; c
++)
174 re_syntax_table
[c
] = Sword
;
176 for (c
= '0'; c
<= '9'; c
++)
177 re_syntax_table
[c
] = Sword
;
179 re_syntax_table
['_'] = Sword
;
184 # endif /* not SYNTAX_TABLE */
186 # define SYNTAX(c) re_syntax_table[c]
188 #endif /* not emacs */
190 /* Get the interface, including the syntax bits. */
191 /* CYGNUS LOCAL: call it gnu-regex.h, not regex.h, to avoid name conflicts */
192 #include "gnu-regex.h"
194 /* isalpha etc. are used for the character classes. */
197 /* Jim Meyering writes:
199 "... Some ctype macros are valid only for character codes that
200 isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
201 using /bin/cc or gcc but without giving an ansi option). So, all
202 ctype uses should be through macros like ISPRINT... If
203 STDC_HEADERS is defined, then autoconf has verified that the ctype
204 macros don't need to be guarded with references to isascii. ...
205 Defining isascii to 1 should let any compiler worth its salt
206 eliminate the && through constant folding."
207 Solaris defines some of these symbols so we must undefine them first. */
210 #if defined STDC_HEADERS || (!defined isascii && !defined HAVE_ISASCII)
211 # define ISASCII(c) 1
213 # define ISASCII(c) isascii(c)
217 # define ISBLANK(c) (ISASCII (c) && isblank (c))
219 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
222 # define ISGRAPH(c) (ISASCII (c) && isgraph (c))
224 # define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
228 #define ISPRINT(c) (ISASCII (c) && isprint (c))
229 #define ISDIGIT(c) (ISASCII (c) && isdigit (c))
230 #define ISALNUM(c) (ISASCII (c) && isalnum (c))
231 #define ISALPHA(c) (ISASCII (c) && isalpha (c))
232 #define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
233 #define ISLOWER(c) (ISASCII (c) && islower (c))
234 #define ISPUNCT(c) (ISASCII (c) && ispunct (c))
235 #define ISSPACE(c) (ISASCII (c) && isspace (c))
236 #define ISUPPER(c) (ISASCII (c) && isupper (c))
237 #define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
240 # define NULL (void *)0
243 /* We remove any previous definition of `SIGN_EXTEND_CHAR',
244 since ours (we hope) works properly with all combinations of
245 machines, compilers, `char' and `unsigned char' argument types.
246 (Per Bothner suggested the basic approach.) */
247 #undef SIGN_EXTEND_CHAR
249 # define SIGN_EXTEND_CHAR(c) ((signed char) (c))
250 #else /* not __STDC__ */
251 /* As in Harbison and Steele. */
252 # define SIGN_EXTEND_CHAR(c) ((((unsigned char) (c)) ^ 128) - 128)
255 /* Should we use malloc or alloca? If REGEX_MALLOC is not defined, we
256 use `alloca' instead of `malloc'. This is because using malloc in
257 re_search* or re_match* could cause memory leaks when C-g is used in
258 Emacs; also, malloc is slower and causes storage fragmentation. On
259 the other hand, malloc is more portable, and easier to debug.
261 Because we sometimes use alloca, some routines have to be macros,
262 not functions -- `alloca'-allocated space disappears at the end of the
263 function it is called in. */
267 # define REGEX_ALLOCATE malloc
268 # define REGEX_REALLOCATE(source, osize, nsize) realloc (source, nsize)
269 # define REGEX_FREE free
271 #else /* not REGEX_MALLOC */
273 /* Emacs already defines alloca, sometimes. */
276 /* Make alloca work the best possible way. */
278 # define alloca __builtin_alloca
279 # else /* not __GNUC__ */
282 # endif /* HAVE_ALLOCA_H */
283 # endif /* not __GNUC__ */
285 # endif /* not alloca */
287 # define REGEX_ALLOCATE alloca
289 /* Assumes a `char *destination' variable. */
290 # define REGEX_REALLOCATE(source, osize, nsize) \
291 (destination = (char *) alloca (nsize), \
292 memcpy (destination, source, osize))
294 /* No need to do anything to free, after alloca. */
295 # define REGEX_FREE(arg) ((void)0) /* Do nothing! But inhibit gcc warning. */
297 #endif /* not REGEX_MALLOC */
299 /* Define how to allocate the failure stack. */
301 #if defined REL_ALLOC && defined REGEX_MALLOC
303 # define REGEX_ALLOCATE_STACK(size) \
304 r_alloc (&failure_stack_ptr, (size))
305 # define REGEX_REALLOCATE_STACK(source, osize, nsize) \
306 r_re_alloc (&failure_stack_ptr, (nsize))
307 # define REGEX_FREE_STACK(ptr) \
308 r_alloc_free (&failure_stack_ptr)
310 #else /* not using relocating allocator */
314 # define REGEX_ALLOCATE_STACK malloc
315 # define REGEX_REALLOCATE_STACK(source, osize, nsize) realloc (source, nsize)
316 # define REGEX_FREE_STACK free
318 # else /* not REGEX_MALLOC */
320 # define REGEX_ALLOCATE_STACK alloca
322 # define REGEX_REALLOCATE_STACK(source, osize, nsize) \
323 REGEX_REALLOCATE (source, osize, nsize)
324 /* No need to explicitly free anything. */
325 # define REGEX_FREE_STACK(arg)
327 # endif /* not REGEX_MALLOC */
328 #endif /* not using relocating allocator */
331 /* True if `size1' is non-NULL and PTR is pointing anywhere inside
332 `string1' or just past its end. This works if PTR is NULL, which is
334 #define FIRST_STRING_P(ptr) \
335 (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
337 /* (Re)Allocate N items of type T using malloc, or fail. */
338 #define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t)))
339 #define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
340 #define RETALLOC_IF(addr, n, t) \
341 if (addr) RETALLOC((addr), (n), t); else (addr) = TALLOC ((n), t)
342 #define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
344 #define BYTEWIDTH 8 /* In bits. */
346 #define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
350 #define MAX(a, b) ((a) > (b) ? (a) : (b))
351 #define MIN(a, b) ((a) < (b) ? (a) : (b))
353 typedef char boolean
;
357 static int re_match_2_internal
PARAMS ((struct re_pattern_buffer
*bufp
,
358 const char *string1
, int size1
,
359 const char *string2
, int size2
,
361 struct re_registers
*regs
,
364 /* These are the command codes that appear in compiled regular
365 expressions. Some opcodes are followed by argument bytes. A
366 command code can specify any interpretation whatsoever for its
367 arguments. Zero bytes may appear in the compiled regular expression. */
373 /* Succeed right away--no more backtracking. */
376 /* Followed by one byte giving n, then by n literal bytes. */
379 /* Matches any (more or less) character. */
382 /* Matches any one char belonging to specified set. First
383 following byte is number of bitmap bytes. Then come bytes
384 for a bitmap saying which chars are in. Bits in each byte
385 are ordered low-bit-first. A character is in the set if its
386 bit is 1. A character too large to have a bit in the map is
387 automatically not in the set. */
390 /* Same parameters as charset, but match any character that is
391 not one of those specified. */
394 /* Start remembering the text that is matched, for storing in a
395 register. Followed by one byte with the register number, in
396 the range 0 to one less than the pattern buffer's re_nsub
397 field. Then followed by one byte with the number of groups
398 inner to this one. (This last has to be part of the
399 start_memory only because we need it in the on_failure_jump
403 /* Stop remembering the text that is matched and store it in a
404 memory register. Followed by one byte with the register
405 number, in the range 0 to one less than `re_nsub' in the
406 pattern buffer, and one byte with the number of inner groups,
407 just like `start_memory'. (We need the number of inner
408 groups here because we don't have any easy way of finding the
409 corresponding start_memory when we're at a stop_memory.) */
412 /* Match a duplicate of something remembered. Followed by one
413 byte containing the register number. */
416 /* Fail unless at beginning of line. */
419 /* Fail unless at end of line. */
422 /* Succeeds if at beginning of buffer (if emacs) or at beginning
423 of string to be matched (if not). */
426 /* Analogously, for end of buffer/string. */
429 /* Followed by two byte relative address to which to jump. */
432 /* Same as jump, but marks the end of an alternative. */
435 /* Followed by two-byte relative address of place to resume at
436 in case of failure. */
439 /* Like on_failure_jump, but pushes a placeholder instead of the
440 current string position when executed. */
441 on_failure_keep_string_jump
,
443 /* Throw away latest failure point and then jump to following
444 two-byte relative address. */
447 /* Change to pop_failure_jump if know won't have to backtrack to
448 match; otherwise change to jump. This is used to jump
449 back to the beginning of a repeat. If what follows this jump
450 clearly won't match what the repeat does, such that we can be
451 sure that there is no use backtracking out of repetitions
452 already matched, then we change it to a pop_failure_jump.
453 Followed by two-byte address. */
456 /* Jump to following two-byte address, and push a dummy failure
457 point. This failure point will be thrown away if an attempt
458 is made to use it for a failure. A `+' construct makes this
459 before the first repeat. Also used as an intermediary kind
460 of jump when compiling an alternative. */
463 /* Push a dummy failure point and continue. Used at the end of
467 /* Followed by two-byte relative address and two-byte number n.
468 After matching N times, jump to the address upon failure. */
471 /* Followed by two-byte relative address, and two-byte number n.
472 Jump to the address N times, then fail. */
475 /* Set the following two-byte relative address to the
476 subsequent two-byte number. The address *includes* the two
480 wordchar
, /* Matches any word-constituent character. */
481 notwordchar
, /* Matches any char that is not a word-constituent. */
483 wordbeg
, /* Succeeds if at word beginning. */
484 wordend
, /* Succeeds if at word end. */
486 wordbound
, /* Succeeds if at a word boundary. */
487 notwordbound
/* Succeeds if not at a word boundary. */
490 ,before_dot
, /* Succeeds if before point. */
491 at_dot
, /* Succeeds if at point. */
492 after_dot
, /* Succeeds if after point. */
494 /* Matches any character whose syntax is specified. Followed by
495 a byte which contains a syntax code, e.g., Sword. */
498 /* Matches any character whose syntax is not that specified. */
503 /* Common operations on the compiled pattern. */
505 /* Store NUMBER in two contiguous bytes starting at DESTINATION. */
507 #define STORE_NUMBER(destination, number) \
509 (destination)[0] = (number) & 0377; \
510 (destination)[1] = (number) >> 8; \
513 /* Same as STORE_NUMBER, except increment DESTINATION to
514 the byte after where the number is stored. Therefore, DESTINATION
515 must be an lvalue. */
517 #define STORE_NUMBER_AND_INCR(destination, number) \
519 STORE_NUMBER (destination, number); \
520 (destination) += 2; \
523 /* Put into DESTINATION a number stored in two contiguous bytes starting
526 #define EXTRACT_NUMBER(destination, source) \
528 (destination) = *(source) & 0377; \
529 (destination) += SIGN_EXTEND_CHAR (*((source) + 1)) << 8; \
533 static void extract_number
_RE_ARGS ((int *dest
, unsigned char *source
));
535 extract_number (dest
, source
)
537 unsigned char *source
;
539 int temp
= SIGN_EXTEND_CHAR (*(source
+ 1));
540 *dest
= *source
& 0377;
544 # ifndef EXTRACT_MACROS /* To debug the macros. */
545 # undef EXTRACT_NUMBER
546 # define EXTRACT_NUMBER(dest, src) extract_number (&dest, src)
547 # endif /* not EXTRACT_MACROS */
551 /* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
552 SOURCE must be an lvalue. */
554 #define EXTRACT_NUMBER_AND_INCR(destination, source) \
556 EXTRACT_NUMBER (destination, source); \
561 static void extract_number_and_incr
_RE_ARGS ((int *destination
,
562 unsigned char **source
));
564 extract_number_and_incr (destination
, source
)
566 unsigned char **source
;
568 extract_number (destination
, *source
);
572 # ifndef EXTRACT_MACROS
573 # undef EXTRACT_NUMBER_AND_INCR
574 # define EXTRACT_NUMBER_AND_INCR(dest, src) \
575 extract_number_and_incr (&dest, &src)
576 # endif /* not EXTRACT_MACROS */
580 /* If DEBUG is defined, Regex prints many voluminous messages about what
581 it is doing (if the variable `debug' is nonzero). If linked with the
582 main program in `iregex.c', you can enter patterns and strings
583 interactively. And if linked with the main program in `main.c' and
584 the other test files, you can run the already-written tests. */
588 /* We use standard I/O for debugging. */
591 /* It is useful to test things that ``must'' be true when debugging. */
594 static int debug
= 0;
596 # define DEBUG_STATEMENT(e) e
597 # define DEBUG_PRINT1(x) if (debug) printf (x)
598 # define DEBUG_PRINT2(x1, x2) if (debug) printf (x1, x2)
599 # define DEBUG_PRINT3(x1, x2, x3) if (debug) printf (x1, x2, x3)
600 # define DEBUG_PRINT4(x1, x2, x3, x4) if (debug) printf (x1, x2, x3, x4)
601 # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) \
602 if (debug) print_partial_compiled_pattern (s, e)
603 # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \
604 if (debug) print_double_string (w, s1, sz1, s2, sz2)
607 /* Print the fastmap in human-readable form. */
610 print_fastmap (fastmap
)
613 unsigned was_a_range
= 0;
616 while (i
< (1 << BYTEWIDTH
))
622 while (i
< (1 << BYTEWIDTH
) && fastmap
[i
])
638 /* Print a compiled pattern string in human-readable form, starting at
639 the START pointer into it and ending just before the pointer END. */
642 print_partial_compiled_pattern (start
, end
)
643 unsigned char *start
;
648 unsigned char *p
= start
;
649 unsigned char *pend
= end
;
657 /* Loop over pattern commands. */
660 printf ("%d:\t", p
- start
);
662 switch ((re_opcode_t
) *p
++)
670 printf ("/exactn/%d", mcnt
);
681 printf ("/start_memory/%d/%d", mcnt
, *p
++);
686 printf ("/stop_memory/%d/%d", mcnt
, *p
++);
690 printf ("/duplicate/%d", *p
++);
700 register int c
, last
= -100;
701 register int in_range
= 0;
703 printf ("/charset [%s",
704 (re_opcode_t
) *(p
- 1) == charset_not
? "^" : "");
706 assert (p
+ *p
< pend
);
708 for (c
= 0; c
< 256; c
++)
710 && (p
[1 + (c
/8)] & (1 << (c
% 8))))
712 /* Are we starting a range? */
713 if (last
+ 1 == c
&& ! in_range
)
718 /* Have we broken a range? */
719 else if (last
+ 1 != c
&& in_range
)
748 case on_failure_jump
:
749 extract_number_and_incr (&mcnt
, &p
);
750 printf ("/on_failure_jump to %d", p
+ mcnt
- start
);
753 case on_failure_keep_string_jump
:
754 extract_number_and_incr (&mcnt
, &p
);
755 printf ("/on_failure_keep_string_jump to %d", p
+ mcnt
- start
);
758 case dummy_failure_jump
:
759 extract_number_and_incr (&mcnt
, &p
);
760 printf ("/dummy_failure_jump to %d", p
+ mcnt
- start
);
763 case push_dummy_failure
:
764 printf ("/push_dummy_failure");
768 extract_number_and_incr (&mcnt
, &p
);
769 printf ("/maybe_pop_jump to %d", p
+ mcnt
- start
);
772 case pop_failure_jump
:
773 extract_number_and_incr (&mcnt
, &p
);
774 printf ("/pop_failure_jump to %d", p
+ mcnt
- start
);
778 extract_number_and_incr (&mcnt
, &p
);
779 printf ("/jump_past_alt to %d", p
+ mcnt
- start
);
783 extract_number_and_incr (&mcnt
, &p
);
784 printf ("/jump to %d", p
+ mcnt
- start
);
788 extract_number_and_incr (&mcnt
, &p
);
790 extract_number_and_incr (&mcnt2
, &p
);
791 printf ("/succeed_n to %d, %d times", p1
- start
, mcnt2
);
795 extract_number_and_incr (&mcnt
, &p
);
797 extract_number_and_incr (&mcnt2
, &p
);
798 printf ("/jump_n to %d, %d times", p1
- start
, mcnt2
);
802 extract_number_and_incr (&mcnt
, &p
);
804 extract_number_and_incr (&mcnt2
, &p
);
805 printf ("/set_number_at location %d to %d", p1
- start
, mcnt2
);
809 printf ("/wordbound");
813 printf ("/notwordbound");
825 printf ("/before_dot");
833 printf ("/after_dot");
837 printf ("/syntaxspec");
839 printf ("/%d", mcnt
);
843 printf ("/notsyntaxspec");
845 printf ("/%d", mcnt
);
850 printf ("/wordchar");
854 printf ("/notwordchar");
866 printf ("?%d", *(p
-1));
872 printf ("%d:\tend of pattern.\n", p
- start
);
877 print_compiled_pattern (bufp
)
878 struct re_pattern_buffer
*bufp
;
880 unsigned char *buffer
= bufp
->buffer
;
882 print_partial_compiled_pattern (buffer
, buffer
+ bufp
->used
);
883 printf ("%ld bytes used/%ld bytes allocated.\n",
884 bufp
->used
, bufp
->allocated
);
886 if (bufp
->fastmap_accurate
&& bufp
->fastmap
)
888 printf ("fastmap: ");
889 print_fastmap (bufp
->fastmap
);
892 printf ("re_nsub: %d\t", bufp
->re_nsub
);
893 printf ("regs_alloc: %d\t", bufp
->regs_allocated
);
894 printf ("can_be_null: %d\t", bufp
->can_be_null
);
895 printf ("newline_anchor: %d\n", bufp
->newline_anchor
);
896 printf ("no_sub: %d\t", bufp
->no_sub
);
897 printf ("not_bol: %d\t", bufp
->not_bol
);
898 printf ("not_eol: %d\t", bufp
->not_eol
);
899 printf ("syntax: %lx\n", bufp
->syntax
);
900 /* Perhaps we should print the translate table? */
905 print_double_string (where
, string1
, size1
, string2
, size2
)
918 if (FIRST_STRING_P (where
))
920 for (this_char
= where
- string1
; this_char
< size1
; this_char
++)
921 putchar (string1
[this_char
]);
926 for (this_char
= where
- string2
; this_char
< size2
; this_char
++)
927 putchar (string2
[this_char
]);
938 #else /* not DEBUG */
943 # define DEBUG_STATEMENT(e)
944 # define DEBUG_PRINT1(x)
945 # define DEBUG_PRINT2(x1, x2)
946 # define DEBUG_PRINT3(x1, x2, x3)
947 # define DEBUG_PRINT4(x1, x2, x3, x4)
948 # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
949 # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
951 #endif /* not DEBUG */
953 /* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
954 also be assigned to arbitrarily: each pattern buffer stores its own
955 syntax, so it can be changed between regex compilations. */
956 /* This has no initializer because initialized variables in Emacs
957 become read-only after dumping. */
958 reg_syntax_t re_syntax_options
;
961 /* Specify the precise syntax of regexps for compilation. This provides
962 for compatibility for various utilities which historically have
963 different, incompatible syntaxes.
965 The argument SYNTAX is a bit mask comprised of the various bits
966 defined in gnu-regex.h. We return the old syntax. */
969 re_set_syntax (syntax
)
972 reg_syntax_t ret
= re_syntax_options
;
974 re_syntax_options
= syntax
;
976 if (syntax
& RE_DEBUG
)
978 else if (debug
) /* was on but now is not */
984 weak_alias (__re_set_syntax
, re_set_syntax
)
987 /* This table gives an error message for each of the error codes listed
988 in gnu-regex.h. Obviously the order here has to be same as there.
989 POSIX doesn't require that we do anything for REG_NOERROR,
990 but why not be nice? */
992 static const char *re_error_msgid
[] =
994 gettext_noop ("Success"), /* REG_NOERROR */
995 gettext_noop ("No match"), /* REG_NOMATCH */
996 gettext_noop ("Invalid regular expression"), /* REG_BADPAT */
997 gettext_noop ("Invalid collation character"), /* REG_ECOLLATE */
998 gettext_noop ("Invalid character class name"), /* REG_ECTYPE */
999 gettext_noop ("Trailing backslash"), /* REG_EESCAPE */
1000 gettext_noop ("Invalid back reference"), /* REG_ESUBREG */
1001 gettext_noop ("Unmatched [ or [^"), /* REG_EBRACK */
1002 gettext_noop ("Unmatched ( or \\("), /* REG_EPAREN */
1003 gettext_noop ("Unmatched \\{"), /* REG_EBRACE */
1004 gettext_noop ("Invalid content of \\{\\}"), /* REG_BADBR */
1005 gettext_noop ("Invalid range end"), /* REG_ERANGE */
1006 gettext_noop ("Memory exhausted"), /* REG_ESPACE */
1007 gettext_noop ("Invalid preceding regular expression"), /* REG_BADRPT */
1008 gettext_noop ("Premature end of regular expression"), /* REG_EEND */
1009 gettext_noop ("Regular expression too big"), /* REG_ESIZE */
1010 gettext_noop ("Unmatched ) or \\)"), /* REG_ERPAREN */
1013 /* Avoiding alloca during matching, to placate r_alloc. */
1015 /* Define MATCH_MAY_ALLOCATE unless we need to make sure that the
1016 searching and matching functions should not call alloca. On some
1017 systems, alloca is implemented in terms of malloc, and if we're
1018 using the relocating allocator routines, then malloc could cause a
1019 relocation, which might (if the strings being searched are in the
1020 ralloc heap) shift the data out from underneath the regexp
1023 Here's another reason to avoid allocation: Emacs
1024 processes input from X in a signal handler; processing X input may
1025 call malloc; if input arrives while a matching routine is calling
1026 malloc, then we're scrod. But Emacs can't just block input while
1027 calling matching routines; then we don't notice interrupts when
1028 they come in. So, Emacs blocks input around all regexp calls
1029 except the matching calls, which it leaves unprotected, in the
1030 faith that they will not malloc. */
1032 /* Normally, this is fine. */
1033 #define MATCH_MAY_ALLOCATE
1035 /* When using GNU C, we are not REALLY using the C alloca, no matter
1036 what config.h may say. So don't take precautions for it. */
1041 /* The match routines may not allocate if (1) they would do it with malloc
1042 and (2) it's not safe for them to use malloc.
1043 Note that if REL_ALLOC is defined, matching would not use malloc for the
1044 failure stack, but we would still use it for the register vectors;
1045 so REL_ALLOC should not affect this. */
1046 #if (defined C_ALLOCA || defined REGEX_MALLOC) && defined emacs
1047 # undef MATCH_MAY_ALLOCATE
1051 /* Failure stack declarations and macros; both re_compile_fastmap and
1052 re_match_2 use a failure stack. These have to be macros because of
1053 REGEX_ALLOCATE_STACK. */
1056 /* Number of failure points for which to initially allocate space
1057 when matching. If this number is exceeded, we allocate more
1058 space, so it is not a hard limit. */
1059 #ifndef INIT_FAILURE_ALLOC
1060 # define INIT_FAILURE_ALLOC 5
1063 /* Roughly the maximum number of failure points on the stack. Would be
1064 exactly that if always used MAX_FAILURE_ITEMS items each time we failed.
1065 This is a variable only so users of regex can assign to it; we never
1066 change it ourselves. */
1070 # if defined MATCH_MAY_ALLOCATE
1071 /* 4400 was enough to cause a crash on Alpha OSF/1,
1072 whose default stack limit is 2mb. */
1073 long int re_max_failures
= 4000;
1075 long int re_max_failures
= 2000;
1078 union fail_stack_elt
1080 unsigned char *pointer
;
1084 typedef union fail_stack_elt fail_stack_elt_t
;
1088 fail_stack_elt_t
*stack
;
1089 unsigned long int size
;
1090 unsigned long int avail
; /* Offset of next open position. */
1093 #else /* not INT_IS_16BIT */
1095 # if defined MATCH_MAY_ALLOCATE
1096 /* 4400 was enough to cause a crash on Alpha OSF/1,
1097 whose default stack limit is 2mb. */
1098 int re_max_failures
= 20000;
1100 int re_max_failures
= 2000;
1103 union fail_stack_elt
1105 unsigned char *pointer
;
1109 typedef union fail_stack_elt fail_stack_elt_t
;
1113 fail_stack_elt_t
*stack
;
1115 unsigned avail
; /* Offset of next open position. */
1118 #endif /* INT_IS_16BIT */
1120 #define FAIL_STACK_EMPTY() (fail_stack.avail == 0)
1121 #define FAIL_STACK_PTR_EMPTY() (fail_stack_ptr->avail == 0)
1122 #define FAIL_STACK_FULL() (fail_stack.avail == fail_stack.size)
1125 /* Define macros to initialize and free the failure stack.
1126 Do `return -2' if the alloc fails. */
1128 #ifdef MATCH_MAY_ALLOCATE
1129 # define INIT_FAIL_STACK() \
1131 fail_stack.stack = (fail_stack_elt_t *) \
1132 REGEX_ALLOCATE_STACK (INIT_FAILURE_ALLOC * sizeof (fail_stack_elt_t)); \
1134 if (fail_stack.stack == NULL) \
1137 fail_stack.size = INIT_FAILURE_ALLOC; \
1138 fail_stack.avail = 0; \
1141 # define RESET_FAIL_STACK() REGEX_FREE_STACK (fail_stack.stack)
1143 # define INIT_FAIL_STACK() \
1145 fail_stack.avail = 0; \
1148 # define RESET_FAIL_STACK()
1152 /* Double the size of FAIL_STACK, up to approximately `re_max_failures' items.
1154 Return 1 if succeeds, and 0 if either ran out of memory
1155 allocating space for it or it was already too large.
1157 REGEX_REALLOCATE_STACK requires `destination' be declared. */
1159 #define DOUBLE_FAIL_STACK(fail_stack) \
1160 ((fail_stack).size > (unsigned) (re_max_failures * MAX_FAILURE_ITEMS) \
1162 : ((fail_stack).stack = (fail_stack_elt_t *) \
1163 REGEX_REALLOCATE_STACK ((fail_stack).stack, \
1164 (fail_stack).size * sizeof (fail_stack_elt_t), \
1165 ((fail_stack).size << 1) * sizeof (fail_stack_elt_t)), \
1167 (fail_stack).stack == NULL \
1169 : ((fail_stack).size <<= 1, \
1173 /* Push pointer POINTER on FAIL_STACK.
1174 Return 1 if was able to do so and 0 if ran out of memory allocating
1176 #define PUSH_PATTERN_OP(POINTER, FAIL_STACK) \
1177 ((FAIL_STACK_FULL () \
1178 && !DOUBLE_FAIL_STACK (FAIL_STACK)) \
1180 : ((FAIL_STACK).stack[(FAIL_STACK).avail++].pointer = POINTER, \
1183 /* Push a pointer value onto the failure stack.
1184 Assumes the variable `fail_stack'. Probably should only
1185 be called from within `PUSH_FAILURE_POINT'. */
1186 #define PUSH_FAILURE_POINTER(item) \
1187 fail_stack.stack[fail_stack.avail++].pointer = (unsigned char *) (item)
1189 /* This pushes an integer-valued item onto the failure stack.
1190 Assumes the variable `fail_stack'. Probably should only
1191 be called from within `PUSH_FAILURE_POINT'. */
1192 #define PUSH_FAILURE_INT(item) \
1193 fail_stack.stack[fail_stack.avail++].integer = (item)
1195 /* Push a fail_stack_elt_t value onto the failure stack.
1196 Assumes the variable `fail_stack'. Probably should only
1197 be called from within `PUSH_FAILURE_POINT'. */
1198 #define PUSH_FAILURE_ELT(item) \
1199 fail_stack.stack[fail_stack.avail++] = (item)
1201 /* These three POP... operations complement the three PUSH... operations.
1202 All assume that `fail_stack' is nonempty. */
1203 #define POP_FAILURE_POINTER() fail_stack.stack[--fail_stack.avail].pointer
1204 #define POP_FAILURE_INT() fail_stack.stack[--fail_stack.avail].integer
1205 #define POP_FAILURE_ELT() fail_stack.stack[--fail_stack.avail]
1207 /* Used to omit pushing failure point id's when we're not debugging. */
1209 # define DEBUG_PUSH PUSH_FAILURE_INT
1210 # define DEBUG_POP(item_addr) *(item_addr) = POP_FAILURE_INT ()
1212 # define DEBUG_PUSH(item)
1213 # define DEBUG_POP(item_addr)
1217 /* Push the information about the state we will need
1218 if we ever fail back to it.
1220 Requires variables fail_stack, regstart, regend, reg_info, and
1221 num_regs_pushed be declared. DOUBLE_FAIL_STACK requires `destination'
1224 Does `return FAILURE_CODE' if runs out of memory. */
1226 #define PUSH_FAILURE_POINT(pattern_place, string_place, failure_code) \
1228 char *destination; \
1229 /* Must be int, so when we don't save any registers, the arithmetic \
1230 of 0 + -1 isn't done as unsigned. */ \
1231 /* Can't be int, since there is not a shred of a guarantee that int \
1232 is wide enough to hold a value of something to which pointer can \
1234 active_reg_t this_reg; \
1236 DEBUG_STATEMENT (failure_id++); \
1237 DEBUG_STATEMENT (nfailure_points_pushed++); \
1238 DEBUG_PRINT2 ("\nPUSH_FAILURE_POINT #%u:\n", failure_id); \
1239 DEBUG_PRINT2 (" Before push, next avail: %d\n", (fail_stack).avail);\
1240 DEBUG_PRINT2 (" size: %d\n", (fail_stack).size);\
1242 DEBUG_PRINT2 (" slots needed: %ld\n", NUM_FAILURE_ITEMS); \
1243 DEBUG_PRINT2 (" available: %d\n", REMAINING_AVAIL_SLOTS); \
1245 /* Ensure we have enough space allocated for what we will push. */ \
1246 while (REMAINING_AVAIL_SLOTS < NUM_FAILURE_ITEMS) \
1248 if (!DOUBLE_FAIL_STACK (fail_stack)) \
1249 return failure_code; \
1251 DEBUG_PRINT2 ("\n Doubled stack; size now: %d\n", \
1252 (fail_stack).size); \
1253 DEBUG_PRINT2 (" slots available: %d\n", REMAINING_AVAIL_SLOTS);\
1256 /* Push the info, starting with the registers. */ \
1257 DEBUG_PRINT1 ("\n"); \
1260 for (this_reg = lowest_active_reg; this_reg <= highest_active_reg; \
1263 DEBUG_PRINT2 (" Pushing reg: %lu\n", this_reg); \
1264 DEBUG_STATEMENT (num_regs_pushed++); \
1266 DEBUG_PRINT2 (" start: %p\n", regstart[this_reg]); \
1267 PUSH_FAILURE_POINTER (regstart[this_reg]); \
1269 DEBUG_PRINT2 (" end: %p\n", regend[this_reg]); \
1270 PUSH_FAILURE_POINTER (regend[this_reg]); \
1272 DEBUG_PRINT2 (" info: %p\n ", \
1273 reg_info[this_reg].word.pointer); \
1274 DEBUG_PRINT2 (" match_null=%d", \
1275 REG_MATCH_NULL_STRING_P (reg_info[this_reg])); \
1276 DEBUG_PRINT2 (" active=%d", IS_ACTIVE (reg_info[this_reg])); \
1277 DEBUG_PRINT2 (" matched_something=%d", \
1278 MATCHED_SOMETHING (reg_info[this_reg])); \
1279 DEBUG_PRINT2 (" ever_matched=%d", \
1280 EVER_MATCHED_SOMETHING (reg_info[this_reg])); \
1281 DEBUG_PRINT1 ("\n"); \
1282 PUSH_FAILURE_ELT (reg_info[this_reg].word); \
1285 DEBUG_PRINT2 (" Pushing low active reg: %ld\n", lowest_active_reg);\
1286 PUSH_FAILURE_INT (lowest_active_reg); \
1288 DEBUG_PRINT2 (" Pushing high active reg: %ld\n", highest_active_reg);\
1289 PUSH_FAILURE_INT (highest_active_reg); \
1291 DEBUG_PRINT2 (" Pushing pattern %p:\n", pattern_place); \
1292 DEBUG_PRINT_COMPILED_PATTERN (bufp, pattern_place, pend); \
1293 PUSH_FAILURE_POINTER (pattern_place); \
1295 DEBUG_PRINT2 (" Pushing string %p: `", string_place); \
1296 DEBUG_PRINT_DOUBLE_STRING (string_place, string1, size1, string2, \
1298 DEBUG_PRINT1 ("'\n"); \
1299 PUSH_FAILURE_POINTER (string_place); \
1301 DEBUG_PRINT2 (" Pushing failure id: %u\n", failure_id); \
1302 DEBUG_PUSH (failure_id); \
1305 /* This is the number of items that are pushed and popped on the stack
1306 for each register. */
1307 #define NUM_REG_ITEMS 3
1309 /* Individual items aside from the registers. */
1311 # define NUM_NONREG_ITEMS 5 /* Includes failure point id. */
1313 # define NUM_NONREG_ITEMS 4
1316 /* We push at most this many items on the stack. */
1317 /* We used to use (num_regs - 1), which is the number of registers
1318 this regexp will save; but that was changed to 5
1319 to avoid stack overflow for a regexp with lots of parens. */
1320 #define MAX_FAILURE_ITEMS (5 * NUM_REG_ITEMS + NUM_NONREG_ITEMS)
1322 /* We actually push this many items. */
1323 #define NUM_FAILURE_ITEMS \
1325 ? 0 : highest_active_reg - lowest_active_reg + 1) \
1329 /* How many items can still be added to the stack without overflowing it. */
1330 #define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail)
1333 /* Pops what PUSH_FAIL_STACK pushes.
1335 We restore into the parameters, all of which should be lvalues:
1336 STR -- the saved data position.
1337 PAT -- the saved pattern position.
1338 LOW_REG, HIGH_REG -- the highest and lowest active registers.
1339 REGSTART, REGEND -- arrays of string positions.
1340 REG_INFO -- array of information about each subexpression.
1342 Also assumes the variables `fail_stack' and (if debugging), `bufp',
1343 `pend', `string1', `size1', `string2', and `size2'. */
1345 #define POP_FAILURE_POINT(str, pat, low_reg, high_reg, regstart, regend, reg_info)\
1347 DEBUG_STATEMENT (unsigned failure_id;) \
1348 active_reg_t this_reg; \
1349 const unsigned char *string_temp; \
1351 assert (!FAIL_STACK_EMPTY ()); \
1353 /* Remove failure points and point to how many regs pushed. */ \
1354 DEBUG_PRINT1 ("POP_FAILURE_POINT:\n"); \
1355 DEBUG_PRINT2 (" Before pop, next avail: %d\n", fail_stack.avail); \
1356 DEBUG_PRINT2 (" size: %d\n", fail_stack.size); \
1358 assert (fail_stack.avail >= NUM_NONREG_ITEMS); \
1360 DEBUG_POP (&failure_id); \
1361 DEBUG_PRINT2 (" Popping failure id: %u\n", failure_id); \
1363 /* If the saved string location is NULL, it came from an \
1364 on_failure_keep_string_jump opcode, and we want to throw away the \
1365 saved NULL, thus retaining our current position in the string. */ \
1366 string_temp = POP_FAILURE_POINTER (); \
1367 if (string_temp != NULL) \
1368 str = (const char *) string_temp; \
1370 DEBUG_PRINT2 (" Popping string %p: `", str); \
1371 DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2); \
1372 DEBUG_PRINT1 ("'\n"); \
1374 pat = (unsigned char *) POP_FAILURE_POINTER (); \
1375 DEBUG_PRINT2 (" Popping pattern %p:\n", pat); \
1376 DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend); \
1378 /* Restore register info. */ \
1379 high_reg = (active_reg_t) POP_FAILURE_INT (); \
1380 DEBUG_PRINT2 (" Popping high active reg: %ld\n", high_reg); \
1382 low_reg = (active_reg_t) POP_FAILURE_INT (); \
1383 DEBUG_PRINT2 (" Popping low active reg: %ld\n", low_reg); \
1386 for (this_reg = high_reg; this_reg >= low_reg; this_reg--) \
1388 DEBUG_PRINT2 (" Popping reg: %ld\n", this_reg); \
1390 reg_info[this_reg].word = POP_FAILURE_ELT (); \
1391 DEBUG_PRINT2 (" info: %p\n", \
1392 reg_info[this_reg].word.pointer); \
1394 regend[this_reg] = (const char *) POP_FAILURE_POINTER (); \
1395 DEBUG_PRINT2 (" end: %p\n", regend[this_reg]); \
1397 regstart[this_reg] = (const char *) POP_FAILURE_POINTER (); \
1398 DEBUG_PRINT2 (" start: %p\n", regstart[this_reg]); \
1402 for (this_reg = highest_active_reg; this_reg > high_reg; this_reg--) \
1404 reg_info[this_reg].word.integer = 0; \
1405 regend[this_reg] = 0; \
1406 regstart[this_reg] = 0; \
1408 highest_active_reg = high_reg; \
1411 set_regs_matched_done = 0; \
1412 DEBUG_STATEMENT (nfailure_points_popped++); \
1413 } /* POP_FAILURE_POINT */
1417 /* Structure for per-register (a.k.a. per-group) information.
1418 Other register information, such as the
1419 starting and ending positions (which are addresses), and the list of
1420 inner groups (which is a bits list) are maintained in separate
1423 We are making a (strictly speaking) nonportable assumption here: that
1424 the compiler will pack our bit fields into something that fits into
1425 the type of `word', i.e., is something that fits into one item on the
1429 /* Declarations and macros for re_match_2. */
1433 fail_stack_elt_t word
;
1436 /* This field is one if this group can match the empty string,
1437 zero if not. If not yet determined, `MATCH_NULL_UNSET_VALUE'. */
1438 #define MATCH_NULL_UNSET_VALUE 3
1439 unsigned match_null_string_p
: 2;
1440 unsigned is_active
: 1;
1441 unsigned matched_something
: 1;
1442 unsigned ever_matched_something
: 1;
1444 } register_info_type
;
1446 #define REG_MATCH_NULL_STRING_P(R) ((R).bits.match_null_string_p)
1447 #define IS_ACTIVE(R) ((R).bits.is_active)
1448 #define MATCHED_SOMETHING(R) ((R).bits.matched_something)
1449 #define EVER_MATCHED_SOMETHING(R) ((R).bits.ever_matched_something)
1452 /* Call this when have matched a real character; it sets `matched' flags
1453 for the subexpressions which we are currently inside. Also records
1454 that those subexprs have matched. */
1455 #define SET_REGS_MATCHED() \
1458 if (!set_regs_matched_done) \
1461 set_regs_matched_done = 1; \
1462 for (r = lowest_active_reg; r <= highest_active_reg; r++) \
1464 MATCHED_SOMETHING (reg_info[r]) \
1465 = EVER_MATCHED_SOMETHING (reg_info[r]) \
1472 /* Registers are set to a sentinel when they haven't yet matched. */
1473 static char reg_unset_dummy
;
1474 #define REG_UNSET_VALUE (®_unset_dummy)
1475 #define REG_UNSET(e) ((e) == REG_UNSET_VALUE)
1477 /* Subroutine declarations and macros for regex_compile. */
1479 static reg_errcode_t regex_compile
_RE_ARGS ((const char *pattern
, size_t size
,
1480 reg_syntax_t syntax
,
1481 struct re_pattern_buffer
*bufp
));
1482 static void store_op1
_RE_ARGS ((re_opcode_t op
, unsigned char *loc
, int arg
));
1483 static void store_op2
_RE_ARGS ((re_opcode_t op
, unsigned char *loc
,
1484 int arg1
, int arg2
));
1485 static void insert_op1
_RE_ARGS ((re_opcode_t op
, unsigned char *loc
,
1486 int arg
, unsigned char *end
));
1487 static void insert_op2
_RE_ARGS ((re_opcode_t op
, unsigned char *loc
,
1488 int arg1
, int arg2
, unsigned char *end
));
1489 static boolean at_begline_loc_p
_RE_ARGS ((const char *pattern
, const char *p
,
1490 reg_syntax_t syntax
));
1491 static boolean at_endline_loc_p
_RE_ARGS ((const char *p
, const char *pend
,
1492 reg_syntax_t syntax
));
1493 static reg_errcode_t compile_range
_RE_ARGS ((const char **p_ptr
,
1496 reg_syntax_t syntax
,
1499 /* Fetch the next character in the uncompiled pattern---translating it
1500 if necessary. Also cast from a signed character in the constant
1501 string passed to us by the user to an unsigned char that we can use
1502 as an array index (in, e.g., `translate'). */
1504 # define PATFETCH(c) \
1505 do {if (p == pend) return REG_EEND; \
1506 c = (unsigned char) *p++; \
1507 if (translate) c = (unsigned char) translate[c]; \
1511 /* Fetch the next character in the uncompiled pattern, with no
1513 #define PATFETCH_RAW(c) \
1514 do {if (p == pend) return REG_EEND; \
1515 c = (unsigned char) *p++; \
1518 /* Go backwards one character in the pattern. */
1519 #define PATUNFETCH p--
1522 /* If `translate' is non-null, return translate[D], else just D. We
1523 cast the subscript to translate because some data is declared as
1524 `char *', to avoid warnings when a string constant is passed. But
1525 when we use a character as a subscript we must make it unsigned. */
1527 # define TRANSLATE(d) \
1528 (translate ? (char) translate[(unsigned char) (d)] : (d))
1532 /* Macros for outputting the compiled pattern into `buffer'. */
1534 /* If the buffer isn't allocated when it comes in, use this. */
1535 #define INIT_BUF_SIZE 32
1537 /* Make sure we have at least N more bytes of space in buffer. */
1538 #define GET_BUFFER_SPACE(n) \
1539 while ((unsigned long) (b - bufp->buffer + (n)) > bufp->allocated) \
1542 /* Make sure we have one more byte of buffer space and then add C to it. */
1543 #define BUF_PUSH(c) \
1545 GET_BUFFER_SPACE (1); \
1546 *b++ = (unsigned char) (c); \
1550 /* Ensure we have two more bytes of buffer space and then append C1 and C2. */
1551 #define BUF_PUSH_2(c1, c2) \
1553 GET_BUFFER_SPACE (2); \
1554 *b++ = (unsigned char) (c1); \
1555 *b++ = (unsigned char) (c2); \
1559 /* As with BUF_PUSH_2, except for three bytes. */
1560 #define BUF_PUSH_3(c1, c2, c3) \
1562 GET_BUFFER_SPACE (3); \
1563 *b++ = (unsigned char) (c1); \
1564 *b++ = (unsigned char) (c2); \
1565 *b++ = (unsigned char) (c3); \
1569 /* Store a jump with opcode OP at LOC to location TO. We store a
1570 relative address offset by the three bytes the jump itself occupies. */
1571 #define STORE_JUMP(op, loc, to) \
1572 store_op1 (op, loc, (int) ((to) - (loc) - 3))
1574 /* Likewise, for a two-argument jump. */
1575 #define STORE_JUMP2(op, loc, to, arg) \
1576 store_op2 (op, loc, (int) ((to) - (loc) - 3), arg)
1578 /* Like `STORE_JUMP', but for inserting. Assume `b' is the buffer end. */
1579 #define INSERT_JUMP(op, loc, to) \
1580 insert_op1 (op, loc, (int) ((to) - (loc) - 3), b)
1582 /* Like `STORE_JUMP2', but for inserting. Assume `b' is the buffer end. */
1583 #define INSERT_JUMP2(op, loc, to, arg) \
1584 insert_op2 (op, loc, (int) ((to) - (loc) - 3), arg, b)
1587 /* This is not an arbitrary limit: the arguments which represent offsets
1588 into the pattern are two bytes long. So if 2^16 bytes turns out to
1589 be too small, many things would have to change. */
1590 /* Any other compiler which, like MSC, has allocation limit below 2^16
1591 bytes will have to use approach similar to what was done below for
1592 MSC and drop MAX_BUF_SIZE a bit. Otherwise you may end up
1593 reallocating to 0 bytes. Such thing is not going to work too well.
1594 You have been warned!! */
1595 #if defined _MSC_VER && !defined WIN32
1596 /* Microsoft C 16-bit versions limit malloc to approx 65512 bytes.
1597 The REALLOC define eliminates a flurry of conversion warnings,
1598 but is not required. */
1599 # define MAX_BUF_SIZE 65500L
1600 # define REALLOC(p,s) realloc ((p), (size_t) (s))
1602 # define MAX_BUF_SIZE (1L << 16)
1603 # define REALLOC(p,s) realloc ((p), (s))
1606 /* Extend the buffer by twice its current size via realloc and
1607 reset the pointers that pointed into the old block to point to the
1608 correct places in the new one. If extending the buffer results in it
1609 being larger than MAX_BUF_SIZE, then flag memory exhausted. */
1610 #define EXTEND_BUFFER() \
1612 unsigned char *old_buffer = bufp->buffer; \
1613 if (bufp->allocated == MAX_BUF_SIZE) \
1615 bufp->allocated <<= 1; \
1616 if (bufp->allocated > MAX_BUF_SIZE) \
1617 bufp->allocated = MAX_BUF_SIZE; \
1618 bufp->buffer = (unsigned char *) REALLOC (bufp->buffer, bufp->allocated);\
1619 if (bufp->buffer == NULL) \
1620 return REG_ESPACE; \
1621 /* If the buffer moved, move all the pointers into it. */ \
1622 if (old_buffer != bufp->buffer) \
1624 b = (b - old_buffer) + bufp->buffer; \
1625 begalt = (begalt - old_buffer) + bufp->buffer; \
1626 if (fixup_alt_jump) \
1627 fixup_alt_jump = (fixup_alt_jump - old_buffer) + bufp->buffer;\
1629 laststart = (laststart - old_buffer) + bufp->buffer; \
1630 if (pending_exact) \
1631 pending_exact = (pending_exact - old_buffer) + bufp->buffer; \
1636 /* Since we have one byte reserved for the register number argument to
1637 {start,stop}_memory, the maximum number of groups we can report
1638 things about is what fits in that byte. */
1639 #define MAX_REGNUM 255
1641 /* But patterns can have more than `MAX_REGNUM' registers. We just
1642 ignore the excess. */
1643 typedef unsigned regnum_t
;
1646 /* Macros for the compile stack. */
1648 /* Since offsets can go either forwards or backwards, this type needs to
1649 be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1. */
1650 /* int may be not enough when sizeof(int) == 2. */
1651 typedef long pattern_offset_t
;
1655 pattern_offset_t begalt_offset
;
1656 pattern_offset_t fixup_alt_jump
;
1657 pattern_offset_t inner_group_offset
;
1658 pattern_offset_t laststart_offset
;
1660 } compile_stack_elt_t
;
1665 compile_stack_elt_t
*stack
;
1667 unsigned avail
; /* Offset of next open position. */
1668 } compile_stack_type
;
1671 #define INIT_COMPILE_STACK_SIZE 32
1673 #define COMPILE_STACK_EMPTY (compile_stack.avail == 0)
1674 #define COMPILE_STACK_FULL (compile_stack.avail == compile_stack.size)
1676 /* The next available element. */
1677 #define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
1680 /* Set the bit for character C in a list. */
1681 #define SET_LIST_BIT(c) \
1682 (b[((unsigned char) (c)) / BYTEWIDTH] \
1683 |= 1 << (((unsigned char) c) % BYTEWIDTH))
1686 /* Get the next unsigned number in the uncompiled pattern. */
1687 #define GET_UNSIGNED_NUMBER(num) \
1691 while (ISDIGIT (c)) \
1695 num = num * 10 + c - '0'; \
1703 #if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
1704 /* The GNU C library provides support for user-defined character classes
1705 and the functions from ISO C amendement 1. */
1706 # ifdef CHARCLASS_NAME_MAX
1707 # define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
1709 /* This shouldn't happen but some implementation might still have this
1710 problem. Use a reasonable default value. */
1711 # define CHAR_CLASS_MAX_LENGTH 256
1715 # define IS_CHAR_CLASS(string) __wctype (string)
1717 # define IS_CHAR_CLASS(string) wctype (string)
1720 # define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
1722 # define IS_CHAR_CLASS(string) \
1723 (STREQ (string, "alpha") || STREQ (string, "upper") \
1724 || STREQ (string, "lower") || STREQ (string, "digit") \
1725 || STREQ (string, "alnum") || STREQ (string, "xdigit") \
1726 || STREQ (string, "space") || STREQ (string, "print") \
1727 || STREQ (string, "punct") || STREQ (string, "graph") \
1728 || STREQ (string, "cntrl") || STREQ (string, "blank"))
1731 #ifndef MATCH_MAY_ALLOCATE
1733 /* If we cannot allocate large objects within re_match_2_internal,
1734 we make the fail stack and register vectors global.
1735 The fail stack, we grow to the maximum size when a regexp
1737 The register vectors, we adjust in size each time we
1738 compile a regexp, according to the number of registers it needs. */
1740 static fail_stack_type fail_stack
;
1742 /* Size with which the following vectors are currently allocated.
1743 That is so we can make them bigger as needed,
1744 but never make them smaller. */
1745 static int regs_allocated_size
;
1747 static const char ** regstart
, ** regend
;
1748 static const char ** old_regstart
, ** old_regend
;
1749 static const char **best_regstart
, **best_regend
;
1750 static register_info_type
*reg_info
;
1751 static const char **reg_dummy
;
1752 static register_info_type
*reg_info_dummy
;
1754 /* Make the register vectors big enough for NUM_REGS registers,
1755 but don't make them smaller. */
1758 regex_grow_registers (num_regs
)
1761 if (num_regs
> regs_allocated_size
)
1763 RETALLOC_IF (regstart
, num_regs
, const char *);
1764 RETALLOC_IF (regend
, num_regs
, const char *);
1765 RETALLOC_IF (old_regstart
, num_regs
, const char *);
1766 RETALLOC_IF (old_regend
, num_regs
, const char *);
1767 RETALLOC_IF (best_regstart
, num_regs
, const char *);
1768 RETALLOC_IF (best_regend
, num_regs
, const char *);
1769 RETALLOC_IF (reg_info
, num_regs
, register_info_type
);
1770 RETALLOC_IF (reg_dummy
, num_regs
, const char *);
1771 RETALLOC_IF (reg_info_dummy
, num_regs
, register_info_type
);
1773 regs_allocated_size
= num_regs
;
1777 #endif /* not MATCH_MAY_ALLOCATE */
1779 static boolean group_in_compile_stack
_RE_ARGS ((compile_stack_type
1783 /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
1784 Returns one of error codes defined in `gnu-regex.h', or zero for success.
1786 Assumes the `allocated' (and perhaps `buffer') and `translate'
1787 fields are set in BUFP on entry.
1789 If it succeeds, results are put in BUFP (if it returns an error, the
1790 contents of BUFP are undefined):
1791 `buffer' is the compiled pattern;
1792 `syntax' is set to SYNTAX;
1793 `used' is set to the length of the compiled pattern;
1794 `fastmap_accurate' is zero;
1795 `re_nsub' is the number of subexpressions in PATTERN;
1796 `not_bol' and `not_eol' are zero;
1798 The `fastmap' and `newline_anchor' fields are neither
1799 examined nor set. */
1801 /* Return, freeing storage we allocated. */
1802 #define FREE_STACK_RETURN(value) \
1803 return (free (compile_stack.stack), value)
1805 static reg_errcode_t
1806 regex_compile (pattern
, size
, syntax
, bufp
)
1807 const char *pattern
;
1809 reg_syntax_t syntax
;
1810 struct re_pattern_buffer
*bufp
;
1812 /* We fetch characters from PATTERN here. Even though PATTERN is
1813 `char *' (i.e., signed), we declare these variables as unsigned, so
1814 they can be reliably used as array indices. */
1815 register unsigned char c
, c1
;
1817 /* A random temporary spot in PATTERN. */
1820 /* Points to the end of the buffer, where we should append. */
1821 register unsigned char *b
;
1823 /* Keeps track of unclosed groups. */
1824 compile_stack_type compile_stack
;
1826 /* Points to the current (ending) position in the pattern. */
1827 const char *p
= pattern
;
1828 const char *pend
= pattern
+ size
;
1830 /* How to translate the characters in the pattern. */
1831 RE_TRANSLATE_TYPE translate
= bufp
->translate
;
1833 /* Address of the count-byte of the most recently inserted `exactn'
1834 command. This makes it possible to tell if a new exact-match
1835 character can be added to that command or if the character requires
1836 a new `exactn' command. */
1837 unsigned char *pending_exact
= 0;
1839 /* Address of start of the most recently finished expression.
1840 This tells, e.g., postfix * where to find the start of its
1841 operand. Reset at the beginning of groups and alternatives. */
1842 unsigned char *laststart
= 0;
1844 /* Address of beginning of regexp, or inside of last group. */
1845 unsigned char *begalt
;
1847 /* Place in the uncompiled pattern (i.e., the {) to
1848 which to go back if the interval is invalid. */
1849 const char *beg_interval
;
1851 /* Address of the place where a forward jump should go to the end of
1852 the containing expression. Each alternative of an `or' -- except the
1853 last -- ends with a forward jump of this sort. */
1854 unsigned char *fixup_alt_jump
= 0;
1856 /* Counts open-groups as they are encountered. Remembered for the
1857 matching close-group on the compile stack, so the same register
1858 number is put in the stop_memory as the start_memory. */
1859 regnum_t regnum
= 0;
1862 DEBUG_PRINT1 ("\nCompiling pattern: ");
1865 unsigned debug_count
;
1867 for (debug_count
= 0; debug_count
< size
; debug_count
++)
1868 putchar (pattern
[debug_count
]);
1873 /* Initialize the compile stack. */
1874 compile_stack
.stack
= TALLOC (INIT_COMPILE_STACK_SIZE
, compile_stack_elt_t
);
1875 if (compile_stack
.stack
== NULL
)
1878 compile_stack
.size
= INIT_COMPILE_STACK_SIZE
;
1879 compile_stack
.avail
= 0;
1881 /* Initialize the pattern buffer. */
1882 bufp
->syntax
= syntax
;
1883 bufp
->fastmap_accurate
= 0;
1884 bufp
->not_bol
= bufp
->not_eol
= 0;
1886 /* Set `used' to zero, so that if we return an error, the pattern
1887 printer (for debugging) will think there's no pattern. We reset it
1891 /* Always count groups, whether or not bufp->no_sub is set. */
1894 #if !defined emacs && !defined SYNTAX_TABLE
1895 /* Initialize the syntax table. */
1896 init_syntax_once ();
1899 if (bufp
->allocated
== 0)
1902 { /* If zero allocated, but buffer is non-null, try to realloc
1903 enough space. This loses if buffer's address is bogus, but
1904 that is the user's responsibility. */
1905 RETALLOC (bufp
->buffer
, INIT_BUF_SIZE
, unsigned char);
1908 { /* Caller did not allocate a buffer. Do it for them. */
1909 bufp
->buffer
= TALLOC (INIT_BUF_SIZE
, unsigned char);
1911 if (!bufp
->buffer
) FREE_STACK_RETURN (REG_ESPACE
);
1913 bufp
->allocated
= INIT_BUF_SIZE
;
1916 begalt
= b
= bufp
->buffer
;
1918 /* Loop through the uncompiled pattern until we're at the end. */
1927 if ( /* If at start of pattern, it's an operator. */
1929 /* If context independent, it's an operator. */
1930 || syntax
& RE_CONTEXT_INDEP_ANCHORS
1931 /* Otherwise, depends on what's come before. */
1932 || at_begline_loc_p (pattern
, p
, syntax
))
1942 if ( /* If at end of pattern, it's an operator. */
1944 /* If context independent, it's an operator. */
1945 || syntax
& RE_CONTEXT_INDEP_ANCHORS
1946 /* Otherwise, depends on what's next. */
1947 || at_endline_loc_p (p
, pend
, syntax
))
1957 if ((syntax
& RE_BK_PLUS_QM
)
1958 || (syntax
& RE_LIMITED_OPS
))
1962 /* If there is no previous pattern... */
1965 if (syntax
& RE_CONTEXT_INVALID_OPS
)
1966 FREE_STACK_RETURN (REG_BADRPT
);
1967 else if (!(syntax
& RE_CONTEXT_INDEP_OPS
))
1972 /* Are we optimizing this jump? */
1973 boolean keep_string_p
= false;
1975 /* 1 means zero (many) matches is allowed. */
1976 char zero_times_ok
= 0, many_times_ok
= 0;
1978 /* If there is a sequence of repetition chars, collapse it
1979 down to just one (the right one). We can't combine
1980 interval operators with these because of, e.g., `a{2}*',
1981 which should only match an even number of `a's. */
1985 zero_times_ok
|= c
!= '+';
1986 many_times_ok
|= c
!= '?';
1994 || (!(syntax
& RE_BK_PLUS_QM
) && (c
== '+' || c
== '?')))
1997 else if (syntax
& RE_BK_PLUS_QM
&& c
== '\\')
1999 if (p
== pend
) FREE_STACK_RETURN (REG_EESCAPE
);
2002 if (!(c1
== '+' || c1
== '?'))
2017 /* If we get here, we found another repeat character. */
2020 /* Star, etc. applied to an empty pattern is equivalent
2021 to an empty pattern. */
2025 /* Now we know whether or not zero matches is allowed
2026 and also whether or not two or more matches is allowed. */
2028 { /* More than one repetition is allowed, so put in at the
2029 end a backward relative jump from `b' to before the next
2030 jump we're going to put in below (which jumps from
2031 laststart to after this jump).
2033 But if we are at the `*' in the exact sequence `.*\n',
2034 insert an unconditional jump backwards to the .,
2035 instead of the beginning of the loop. This way we only
2036 push a failure point once, instead of every time
2037 through the loop. */
2038 assert (p
- 1 > pattern
);
2040 /* Allocate the space for the jump. */
2041 GET_BUFFER_SPACE (3);
2043 /* We know we are not at the first character of the pattern,
2044 because laststart was nonzero. And we've already
2045 incremented `p', by the way, to be the character after
2046 the `*'. Do we have to do something analogous here
2047 for null bytes, because of RE_DOT_NOT_NULL? */
2048 if (TRANSLATE (*(p
- 2)) == TRANSLATE ('.')
2050 && p
< pend
&& TRANSLATE (*p
) == TRANSLATE ('\n')
2051 && !(syntax
& RE_DOT_NEWLINE
))
2052 { /* We have .*\n. */
2053 STORE_JUMP (jump
, b
, laststart
);
2054 keep_string_p
= true;
2057 /* Anything else. */
2058 STORE_JUMP (maybe_pop_jump
, b
, laststart
- 3);
2060 /* We've added more stuff to the buffer. */
2064 /* On failure, jump from laststart to b + 3, which will be the
2065 end of the buffer after this jump is inserted. */
2066 GET_BUFFER_SPACE (3);
2067 INSERT_JUMP (keep_string_p
? on_failure_keep_string_jump
2075 /* At least one repetition is required, so insert a
2076 `dummy_failure_jump' before the initial
2077 `on_failure_jump' instruction of the loop. This
2078 effects a skip over that instruction the first time
2079 we hit that loop. */
2080 GET_BUFFER_SPACE (3);
2081 INSERT_JUMP (dummy_failure_jump
, laststart
, laststart
+ 6);
2096 boolean had_char_class
= false;
2098 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2100 /* Ensure that we have enough space to push a charset: the
2101 opcode, the length count, and the bitset; 34 bytes in all. */
2102 GET_BUFFER_SPACE (34);
2106 /* We test `*p == '^' twice, instead of using an if
2107 statement, so we only need one BUF_PUSH. */
2108 BUF_PUSH (*p
== '^' ? charset_not
: charset
);
2112 /* Remember the first position in the bracket expression. */
2115 /* Push the number of bytes in the bitmap. */
2116 BUF_PUSH ((1 << BYTEWIDTH
) / BYTEWIDTH
);
2118 /* Clear the whole map. */
2119 bzero (b
, (1 << BYTEWIDTH
) / BYTEWIDTH
);
2121 /* charset_not matches newline according to a syntax bit. */
2122 if ((re_opcode_t
) b
[-2] == charset_not
2123 && (syntax
& RE_HAT_LISTS_NOT_NEWLINE
))
2124 SET_LIST_BIT ('\n');
2126 /* Read in characters and ranges, setting map bits. */
2129 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2133 /* \ might escape characters inside [...] and [^...]. */
2134 if ((syntax
& RE_BACKSLASH_ESCAPE_IN_LISTS
) && c
== '\\')
2136 if (p
== pend
) FREE_STACK_RETURN (REG_EESCAPE
);
2143 /* Could be the end of the bracket expression. If it's
2144 not (i.e., when the bracket expression is `[]' so
2145 far), the ']' character bit gets set way below. */
2146 if (c
== ']' && p
!= p1
+ 1)
2149 /* Look ahead to see if it's a range when the last thing
2150 was a character class. */
2151 if (had_char_class
&& c
== '-' && *p
!= ']')
2152 FREE_STACK_RETURN (REG_ERANGE
);
2154 /* Look ahead to see if it's a range when the last thing
2155 was a character: if this is a hyphen not at the
2156 beginning or the end of a list, then it's the range
2159 && !(p
- 2 >= pattern
&& p
[-2] == '[')
2160 && !(p
- 3 >= pattern
&& p
[-3] == '[' && p
[-2] == '^')
2164 = compile_range (&p
, pend
, translate
, syntax
, b
);
2165 if (ret
!= REG_NOERROR
) FREE_STACK_RETURN (ret
);
2168 else if (p
[0] == '-' && p
[1] != ']')
2169 { /* This handles ranges made up of characters only. */
2172 /* Move past the `-'. */
2175 ret
= compile_range (&p
, pend
, translate
, syntax
, b
);
2176 if (ret
!= REG_NOERROR
) FREE_STACK_RETURN (ret
);
2179 /* See if we're at the beginning of a possible character
2182 else if (syntax
& RE_CHAR_CLASSES
&& c
== '[' && *p
== ':')
2183 { /* Leave room for the null. */
2184 char str
[CHAR_CLASS_MAX_LENGTH
+ 1];
2189 /* If pattern is `[[:'. */
2190 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2195 if ((c
== ':' && *p
== ']') || p
== pend
2196 || c1
== CHAR_CLASS_MAX_LENGTH
)
2202 /* If isn't a word bracketed by `[:' and `:]':
2203 undo the ending character, the letters, and leave
2204 the leading `:' and `[' (but set bits for them). */
2205 if (c
== ':' && *p
== ']')
2207 /* CYGNUS LOCAL: Skip this code if we don't have btowc(). btowc() is */
2208 /* defined in the 1994 Amendment 1 to ISO C and may not be present on */
2209 /* systems where we have wchar.h and wctype.h. */
2210 #if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H && defined HAVE_BTOWC)
2211 boolean is_lower
= STREQ (str
, "lower");
2212 boolean is_upper
= STREQ (str
, "upper");
2216 wt
= IS_CHAR_CLASS (str
);
2218 FREE_STACK_RETURN (REG_ECTYPE
);
2220 /* Throw away the ] at the end of the character
2224 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2226 for (ch
= 0; ch
< 1 << BYTEWIDTH
; ++ch
)
2229 if (__iswctype (__btowc (ch
), wt
))
2232 if (iswctype (btowc (ch
), wt
))
2236 if (translate
&& (is_upper
|| is_lower
)
2237 && (ISUPPER (ch
) || ISLOWER (ch
)))
2241 had_char_class
= true;
2244 boolean is_alnum
= STREQ (str
, "alnum");
2245 boolean is_alpha
= STREQ (str
, "alpha");
2246 boolean is_blank
= STREQ (str
, "blank");
2247 boolean is_cntrl
= STREQ (str
, "cntrl");
2248 boolean is_digit
= STREQ (str
, "digit");
2249 boolean is_graph
= STREQ (str
, "graph");
2250 boolean is_lower
= STREQ (str
, "lower");
2251 boolean is_print
= STREQ (str
, "print");
2252 boolean is_punct
= STREQ (str
, "punct");
2253 boolean is_space
= STREQ (str
, "space");
2254 boolean is_upper
= STREQ (str
, "upper");
2255 boolean is_xdigit
= STREQ (str
, "xdigit");
2257 if (!IS_CHAR_CLASS (str
))
2258 FREE_STACK_RETURN (REG_ECTYPE
);
2260 /* Throw away the ] at the end of the character
2264 if (p
== pend
) FREE_STACK_RETURN (REG_EBRACK
);
2266 for (ch
= 0; ch
< 1 << BYTEWIDTH
; ch
++)
2268 /* This was split into 3 if's to
2269 avoid an arbitrary limit in some compiler. */
2270 if ( (is_alnum
&& ISALNUM (ch
))
2271 || (is_alpha
&& ISALPHA (ch
))
2272 || (is_blank
&& ISBLANK (ch
))
2273 || (is_cntrl
&& ISCNTRL (ch
)))
2275 if ( (is_digit
&& ISDIGIT (ch
))
2276 || (is_graph
&& ISGRAPH (ch
))
2277 || (is_lower
&& ISLOWER (ch
))
2278 || (is_print
&& ISPRINT (ch
)))
2280 if ( (is_punct
&& ISPUNCT (ch
))
2281 || (is_space
&& ISSPACE (ch
))
2282 || (is_upper
&& ISUPPER (ch
))
2283 || (is_xdigit
&& ISXDIGIT (ch
)))
2285 if ( translate
&& (is_upper
|| is_lower
)
2286 && (ISUPPER (ch
) || ISLOWER (ch
)))
2289 had_char_class
= true;
2290 #endif /* libc || wctype.h */
2299 had_char_class
= false;
2304 had_char_class
= false;
2309 /* Discard any (non)matching list bytes that are all 0 at the
2310 end of the map. Decrease the map-length byte too. */
2311 while ((int) b
[-1] > 0 && b
[b
[-1] - 1] == 0)
2319 if (syntax
& RE_NO_BK_PARENS
)
2326 if (syntax
& RE_NO_BK_PARENS
)
2333 if (syntax
& RE_NEWLINE_ALT
)
2340 if (syntax
& RE_NO_BK_VBAR
)
2347 if (syntax
& RE_INTERVALS
&& syntax
& RE_NO_BK_BRACES
)
2348 goto handle_interval
;
2354 if (p
== pend
) FREE_STACK_RETURN (REG_EESCAPE
);
2356 /* Do not translate the character after the \, so that we can
2357 distinguish, e.g., \B from \b, even if we normally would
2358 translate, e.g., B to b. */
2364 if (syntax
& RE_NO_BK_PARENS
)
2365 goto normal_backslash
;
2371 if (COMPILE_STACK_FULL
)
2373 RETALLOC (compile_stack
.stack
, compile_stack
.size
<< 1,
2374 compile_stack_elt_t
);
2375 if (compile_stack
.stack
== NULL
) return REG_ESPACE
;
2377 compile_stack
.size
<<= 1;
2380 /* These are the values to restore when we hit end of this
2381 group. They are all relative offsets, so that if the
2382 whole pattern moves because of realloc, they will still
2384 COMPILE_STACK_TOP
.begalt_offset
= begalt
- bufp
->buffer
;
2385 COMPILE_STACK_TOP
.fixup_alt_jump
2386 = fixup_alt_jump
? fixup_alt_jump
- bufp
->buffer
+ 1 : 0;
2387 COMPILE_STACK_TOP
.laststart_offset
= b
- bufp
->buffer
;
2388 COMPILE_STACK_TOP
.regnum
= regnum
;
2390 /* We will eventually replace the 0 with the number of
2391 groups inner to this one. But do not push a
2392 start_memory for groups beyond the last one we can
2393 represent in the compiled pattern. */
2394 if (regnum
<= MAX_REGNUM
)
2396 COMPILE_STACK_TOP
.inner_group_offset
= b
- bufp
->buffer
+ 2;
2397 BUF_PUSH_3 (start_memory
, regnum
, 0);
2400 compile_stack
.avail
++;
2405 /* If we've reached MAX_REGNUM groups, then this open
2406 won't actually generate any code, so we'll have to
2407 clear pending_exact explicitly. */
2413 if (syntax
& RE_NO_BK_PARENS
) goto normal_backslash
;
2415 if (COMPILE_STACK_EMPTY
)
2417 if (syntax
& RE_UNMATCHED_RIGHT_PAREN_ORD
)
2418 goto normal_backslash
;
2420 FREE_STACK_RETURN (REG_ERPAREN
);
2425 { /* Push a dummy failure point at the end of the
2426 alternative for a possible future
2427 `pop_failure_jump' to pop. See comments at
2428 `push_dummy_failure' in `re_match_2'. */
2429 BUF_PUSH (push_dummy_failure
);
2431 /* We allocated space for this jump when we assigned
2432 to `fixup_alt_jump', in the `handle_alt' case below. */
2433 STORE_JUMP (jump_past_alt
, fixup_alt_jump
, b
- 1);
2436 /* See similar code for backslashed left paren above. */
2437 if (COMPILE_STACK_EMPTY
)
2439 if (syntax
& RE_UNMATCHED_RIGHT_PAREN_ORD
)
2442 FREE_STACK_RETURN (REG_ERPAREN
);
2445 /* Since we just checked for an empty stack above, this
2446 ``can't happen''. */
2447 assert (compile_stack
.avail
!= 0);
2449 /* We don't just want to restore into `regnum', because
2450 later groups should continue to be numbered higher,
2451 as in `(ab)c(de)' -- the second group is #2. */
2452 regnum_t this_group_regnum
;
2454 compile_stack
.avail
--;
2455 begalt
= bufp
->buffer
+ COMPILE_STACK_TOP
.begalt_offset
;
2457 = COMPILE_STACK_TOP
.fixup_alt_jump
2458 ? bufp
->buffer
+ COMPILE_STACK_TOP
.fixup_alt_jump
- 1
2460 laststart
= bufp
->buffer
+ COMPILE_STACK_TOP
.laststart_offset
;
2461 this_group_regnum
= COMPILE_STACK_TOP
.regnum
;
2462 /* If we've reached MAX_REGNUM groups, then this open
2463 won't actually generate any code, so we'll have to
2464 clear pending_exact explicitly. */
2467 /* We're at the end of the group, so now we know how many
2468 groups were inside this one. */
2469 if (this_group_regnum
<= MAX_REGNUM
)
2471 unsigned char *inner_group_loc
2472 = bufp
->buffer
+ COMPILE_STACK_TOP
.inner_group_offset
;
2474 *inner_group_loc
= regnum
- this_group_regnum
;
2475 BUF_PUSH_3 (stop_memory
, this_group_regnum
,
2476 regnum
- this_group_regnum
);
2482 case '|': /* `\|'. */
2483 if (syntax
& RE_LIMITED_OPS
|| syntax
& RE_NO_BK_VBAR
)
2484 goto normal_backslash
;
2486 if (syntax
& RE_LIMITED_OPS
)
2489 /* Insert before the previous alternative a jump which
2490 jumps to this alternative if the former fails. */
2491 GET_BUFFER_SPACE (3);
2492 INSERT_JUMP (on_failure_jump
, begalt
, b
+ 6);
2496 /* The alternative before this one has a jump after it
2497 which gets executed if it gets matched. Adjust that
2498 jump so it will jump to this alternative's analogous
2499 jump (put in below, which in turn will jump to the next
2500 (if any) alternative's such jump, etc.). The last such
2501 jump jumps to the correct final destination. A picture:
2507 If we are at `b', then fixup_alt_jump right now points to a
2508 three-byte space after `a'. We'll put in the jump, set
2509 fixup_alt_jump to right after `b', and leave behind three
2510 bytes which we'll fill in when we get to after `c'. */
2513 STORE_JUMP (jump_past_alt
, fixup_alt_jump
, b
);
2515 /* Mark and leave space for a jump after this alternative,
2516 to be filled in later either by next alternative or
2517 when know we're at the end of a series of alternatives. */
2519 GET_BUFFER_SPACE (3);
2528 /* If \{ is a literal. */
2529 if (!(syntax
& RE_INTERVALS
)
2530 /* If we're at `\{' and it's not the open-interval
2532 || ((syntax
& RE_INTERVALS
) && (syntax
& RE_NO_BK_BRACES
))
2533 || (p
- 2 == pattern
&& p
== pend
))
2534 goto normal_backslash
;
2538 /* If got here, then the syntax allows intervals. */
2540 /* At least (most) this many matches must be made. */
2541 int lower_bound
= -1, upper_bound
= -1;
2543 beg_interval
= p
- 1;
2547 if (syntax
& RE_NO_BK_BRACES
)
2548 goto unfetch_interval
;
2550 FREE_STACK_RETURN (REG_EBRACE
);
2553 GET_UNSIGNED_NUMBER (lower_bound
);
2557 GET_UNSIGNED_NUMBER (upper_bound
);
2558 if (upper_bound
< 0) upper_bound
= RE_DUP_MAX
;
2561 /* Interval such as `{1}' => match exactly once. */
2562 upper_bound
= lower_bound
;
2564 if (lower_bound
< 0 || upper_bound
> RE_DUP_MAX
2565 || lower_bound
> upper_bound
)
2567 if (syntax
& RE_NO_BK_BRACES
)
2568 goto unfetch_interval
;
2570 FREE_STACK_RETURN (REG_BADBR
);
2573 if (!(syntax
& RE_NO_BK_BRACES
))
2575 if (c
!= '\\') FREE_STACK_RETURN (REG_EBRACE
);
2582 if (syntax
& RE_NO_BK_BRACES
)
2583 goto unfetch_interval
;
2585 FREE_STACK_RETURN (REG_BADBR
);
2588 /* We just parsed a valid interval. */
2590 /* If it's invalid to have no preceding re. */
2593 if (syntax
& RE_CONTEXT_INVALID_OPS
)
2594 FREE_STACK_RETURN (REG_BADRPT
);
2595 else if (syntax
& RE_CONTEXT_INDEP_OPS
)
2598 goto unfetch_interval
;
2601 /* If the upper bound is zero, don't want to succeed at
2602 all; jump from `laststart' to `b + 3', which will be
2603 the end of the buffer after we insert the jump. */
2604 if (upper_bound
== 0)
2606 GET_BUFFER_SPACE (3);
2607 INSERT_JUMP (jump
, laststart
, b
+ 3);
2611 /* Otherwise, we have a nontrivial interval. When
2612 we're all done, the pattern will look like:
2613 set_number_at <jump count> <upper bound>
2614 set_number_at <succeed_n count> <lower bound>
2615 succeed_n <after jump addr> <succeed_n count>
2617 jump_n <succeed_n addr> <jump count>
2618 (The upper bound and `jump_n' are omitted if
2619 `upper_bound' is 1, though.) */
2621 { /* If the upper bound is > 1, we need to insert
2622 more at the end of the loop. */
2623 unsigned nbytes
= 10 + (upper_bound
> 1) * 10;
2625 GET_BUFFER_SPACE (nbytes
);
2627 /* Initialize lower bound of the `succeed_n', even
2628 though it will be set during matching by its
2629 attendant `set_number_at' (inserted next),
2630 because `re_compile_fastmap' needs to know.
2631 Jump to the `jump_n' we might insert below. */
2632 INSERT_JUMP2 (succeed_n
, laststart
,
2633 b
+ 5 + (upper_bound
> 1) * 5,
2637 /* Code to initialize the lower bound. Insert
2638 before the `succeed_n'. The `5' is the last two
2639 bytes of this `set_number_at', plus 3 bytes of
2640 the following `succeed_n'. */
2641 insert_op2 (set_number_at
, laststart
, 5, lower_bound
, b
);
2644 if (upper_bound
> 1)
2645 { /* More than one repetition is allowed, so
2646 append a backward jump to the `succeed_n'
2647 that starts this interval.
2649 When we've reached this during matching,
2650 we'll have matched the interval once, so
2651 jump back only `upper_bound - 1' times. */
2652 STORE_JUMP2 (jump_n
, b
, laststart
+ 5,
2656 /* The location we want to set is the second
2657 parameter of the `jump_n'; that is `b-2' as
2658 an absolute address. `laststart' will be
2659 the `set_number_at' we're about to insert;
2660 `laststart+3' the number to set, the source
2661 for the relative address. But we are
2662 inserting into the middle of the pattern --
2663 so everything is getting moved up by 5.
2664 Conclusion: (b - 2) - (laststart + 3) + 5,
2665 i.e., b - laststart.
2667 We insert this at the beginning of the loop
2668 so that if we fail during matching, we'll
2669 reinitialize the bounds. */
2670 insert_op2 (set_number_at
, laststart
, b
- laststart
,
2671 upper_bound
- 1, b
);
2676 beg_interval
= NULL
;
2681 /* If an invalid interval, match the characters as literals. */
2682 assert (beg_interval
);
2684 beg_interval
= NULL
;
2686 /* normal_char and normal_backslash need `c'. */
2689 if (!(syntax
& RE_NO_BK_BRACES
))
2691 if (p
> pattern
&& p
[-1] == '\\')
2692 goto normal_backslash
;
2697 /* There is no way to specify the before_dot and after_dot
2698 operators. rms says this is ok. --karl */
2706 BUF_PUSH_2 (syntaxspec
, syntax_spec_code
[c
]);
2712 BUF_PUSH_2 (notsyntaxspec
, syntax_spec_code
[c
]);
2718 if (syntax
& RE_NO_GNU_OPS
)
2721 BUF_PUSH (wordchar
);
2726 if (syntax
& RE_NO_GNU_OPS
)
2729 BUF_PUSH (notwordchar
);
2734 if (syntax
& RE_NO_GNU_OPS
)
2740 if (syntax
& RE_NO_GNU_OPS
)
2746 if (syntax
& RE_NO_GNU_OPS
)
2748 BUF_PUSH (wordbound
);
2752 if (syntax
& RE_NO_GNU_OPS
)
2754 BUF_PUSH (notwordbound
);
2758 if (syntax
& RE_NO_GNU_OPS
)
2764 if (syntax
& RE_NO_GNU_OPS
)
2769 case '1': case '2': case '3': case '4': case '5':
2770 case '6': case '7': case '8': case '9':
2771 if (syntax
& RE_NO_BK_REFS
)
2777 FREE_STACK_RETURN (REG_ESUBREG
);
2779 /* Can't back reference to a subexpression if inside of it. */
2780 if (group_in_compile_stack (compile_stack
, (regnum_t
) c1
))
2784 BUF_PUSH_2 (duplicate
, c1
);
2790 if (syntax
& RE_BK_PLUS_QM
)
2793 goto normal_backslash
;
2797 /* You might think it would be useful for \ to mean
2798 not to translate; but if we don't translate it
2799 it will never match anything. */
2807 /* Expects the character in `c'. */
2809 /* If no exactn currently being built. */
2812 /* If last exactn not at current position. */
2813 || pending_exact
+ *pending_exact
+ 1 != b
2815 /* We have only one byte following the exactn for the count. */
2816 || *pending_exact
== (1 << BYTEWIDTH
) - 1
2818 /* If followed by a repetition operator. */
2819 || *p
== '*' || *p
== '^'
2820 || ((syntax
& RE_BK_PLUS_QM
)
2821 ? *p
== '\\' && (p
[1] == '+' || p
[1] == '?')
2822 : (*p
== '+' || *p
== '?'))
2823 || ((syntax
& RE_INTERVALS
)
2824 && ((syntax
& RE_NO_BK_BRACES
)
2826 : (p
[0] == '\\' && p
[1] == '{'))))
2828 /* Start building a new exactn. */
2832 BUF_PUSH_2 (exactn
, 0);
2833 pending_exact
= b
- 1;
2840 } /* while p != pend */
2843 /* Through the pattern now. */
2846 STORE_JUMP (jump_past_alt
, fixup_alt_jump
, b
);
2848 if (!COMPILE_STACK_EMPTY
)
2849 FREE_STACK_RETURN (REG_EPAREN
);
2851 /* If we don't want backtracking, force success
2852 the first time we reach the end of the compiled pattern. */
2853 if (syntax
& RE_NO_POSIX_BACKTRACKING
)
2856 free (compile_stack
.stack
);
2858 /* We have succeeded; set the length of the buffer. */
2859 bufp
->used
= b
- bufp
->buffer
;
2864 DEBUG_PRINT1 ("\nCompiled pattern: \n");
2865 print_compiled_pattern (bufp
);
2869 #ifndef MATCH_MAY_ALLOCATE
2870 /* Initialize the failure stack to the largest possible stack. This
2871 isn't necessary unless we're trying to avoid calling alloca in
2872 the search and match routines. */
2874 int num_regs
= bufp
->re_nsub
+ 1;
2876 /* Since DOUBLE_FAIL_STACK refuses to double only if the current size
2877 is strictly greater than re_max_failures, the largest possible stack
2878 is 2 * re_max_failures failure points. */
2879 if (fail_stack
.size
< (2 * re_max_failures
* MAX_FAILURE_ITEMS
))
2881 fail_stack
.size
= (2 * re_max_failures
* MAX_FAILURE_ITEMS
);
2884 if (! fail_stack
.stack
)
2886 = (fail_stack_elt_t
*) xmalloc (fail_stack
.size
2887 * sizeof (fail_stack_elt_t
));
2890 = (fail_stack_elt_t
*) xrealloc (fail_stack
.stack
,
2892 * sizeof (fail_stack_elt_t
)));
2893 # else /* not emacs */
2894 if (! fail_stack
.stack
)
2896 = (fail_stack_elt_t
*) malloc (fail_stack
.size
2897 * sizeof (fail_stack_elt_t
));
2900 = (fail_stack_elt_t
*) realloc (fail_stack
.stack
,
2902 * sizeof (fail_stack_elt_t
)));
2903 # endif /* not emacs */
2906 regex_grow_registers (num_regs
);
2908 #endif /* not MATCH_MAY_ALLOCATE */
2911 } /* regex_compile */
2913 /* Subroutines for `regex_compile'. */
2915 /* Store OP at LOC followed by two-byte integer parameter ARG. */
2918 store_op1 (op
, loc
, arg
)
2923 *loc
= (unsigned char) op
;
2924 STORE_NUMBER (loc
+ 1, arg
);
2928 /* Like `store_op1', but for two two-byte parameters ARG1 and ARG2. */
2931 store_op2 (op
, loc
, arg1
, arg2
)
2936 *loc
= (unsigned char) op
;
2937 STORE_NUMBER (loc
+ 1, arg1
);
2938 STORE_NUMBER (loc
+ 3, arg2
);
2942 /* Copy the bytes from LOC to END to open up three bytes of space at LOC
2943 for OP followed by two-byte integer parameter ARG. */
2946 insert_op1 (op
, loc
, arg
, end
)
2952 register unsigned char *pfrom
= end
;
2953 register unsigned char *pto
= end
+ 3;
2955 while (pfrom
!= loc
)
2958 store_op1 (op
, loc
, arg
);
2962 /* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2. */
2965 insert_op2 (op
, loc
, arg1
, arg2
, end
)
2971 register unsigned char *pfrom
= end
;
2972 register unsigned char *pto
= end
+ 5;
2974 while (pfrom
!= loc
)
2977 store_op2 (op
, loc
, arg1
, arg2
);
2981 /* P points to just after a ^ in PATTERN. Return true if that ^ comes
2982 after an alternative or a begin-subexpression. We assume there is at
2983 least one character before the ^. */
2986 at_begline_loc_p (pattern
, p
, syntax
)
2987 const char *pattern
, *p
;
2988 reg_syntax_t syntax
;
2990 const char *prev
= p
- 2;
2991 boolean prev_prev_backslash
= prev
> pattern
&& prev
[-1] == '\\';
2994 /* After a subexpression? */
2995 (*prev
== '(' && (syntax
& RE_NO_BK_PARENS
|| prev_prev_backslash
))
2996 /* After an alternative? */
2997 || (*prev
== '|' && (syntax
& RE_NO_BK_VBAR
|| prev_prev_backslash
));
3001 /* The dual of at_begline_loc_p. This one is for $. We assume there is
3002 at least one character after the $, i.e., `P < PEND'. */
3005 at_endline_loc_p (p
, pend
, syntax
)
3006 const char *p
, *pend
;
3007 reg_syntax_t syntax
;
3009 const char *next
= p
;
3010 boolean next_backslash
= *next
== '\\';
3011 const char *next_next
= p
+ 1 < pend
? p
+ 1 : 0;
3014 /* Before a subexpression? */
3015 (syntax
& RE_NO_BK_PARENS
? *next
== ')'
3016 : next_backslash
&& next_next
&& *next_next
== ')')
3017 /* Before an alternative? */
3018 || (syntax
& RE_NO_BK_VBAR
? *next
== '|'
3019 : next_backslash
&& next_next
&& *next_next
== '|');
3023 /* Returns true if REGNUM is in one of COMPILE_STACK's elements and
3024 false if it's not. */
3027 group_in_compile_stack (compile_stack
, regnum
)
3028 compile_stack_type compile_stack
;
3033 for (this_element
= compile_stack
.avail
- 1;
3036 if (compile_stack
.stack
[this_element
].regnum
== regnum
)
3043 /* Read the ending character of a range (in a bracket expression) from the
3044 uncompiled pattern *P_PTR (which ends at PEND). We assume the
3045 starting character is in `P[-2]'. (`P[-1]' is the character `-'.)
3046 Then we set the translation of all bits between the starting and
3047 ending characters (inclusive) in the compiled pattern B.
3049 Return an error code.
3051 We use these short variable names so we can use the same macros as
3052 `regex_compile' itself. */
3054 static reg_errcode_t
3055 compile_range (p_ptr
, pend
, translate
, syntax
, b
)
3056 const char **p_ptr
, *pend
;
3057 RE_TRANSLATE_TYPE translate
;
3058 reg_syntax_t syntax
;
3063 const char *p
= *p_ptr
;
3064 unsigned int range_start
, range_end
;
3069 /* Even though the pattern is a signed `char *', we need to fetch
3070 with unsigned char *'s; if the high bit of the pattern character
3071 is set, the range endpoints will be negative if we fetch using a
3074 We also want to fetch the endpoints without translating them; the
3075 appropriate translation is done in the bit-setting loop below. */
3076 /* The SVR4 compiler on the 3B2 had trouble with unsigned const char *. */
3077 range_start
= ((const unsigned char *) p
)[-2];
3078 range_end
= ((const unsigned char *) p
)[0];
3080 /* Have to increment the pointer into the pattern string, so the
3081 caller isn't still at the ending character. */
3084 /* If the start is after the end, the range is empty. */
3085 if (range_start
> range_end
)
3086 return syntax
& RE_NO_EMPTY_RANGES
? REG_ERANGE
: REG_NOERROR
;
3088 /* Here we see why `this_char' has to be larger than an `unsigned
3089 char' -- the range is inclusive, so if `range_end' == 0xff
3090 (assuming 8-bit characters), we would otherwise go into an infinite
3091 loop, since all characters <= 0xff. */
3092 for (this_char
= range_start
; this_char
<= range_end
; this_char
++)
3094 SET_LIST_BIT (TRANSLATE (this_char
));
3100 /* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
3101 BUFP. A fastmap records which of the (1 << BYTEWIDTH) possible
3102 characters can start a string that matches the pattern. This fastmap
3103 is used by re_search to skip quickly over impossible starting points.
3105 The caller must supply the address of a (1 << BYTEWIDTH)-byte data
3106 area as BUFP->fastmap.
3108 We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in
3111 Returns 0 if we succeed, -2 if an internal error. */
3114 re_compile_fastmap (bufp
)
3115 struct re_pattern_buffer
*bufp
;
3118 #ifdef MATCH_MAY_ALLOCATE
3119 fail_stack_type fail_stack
;
3121 #ifndef REGEX_MALLOC
3125 register char *fastmap
= bufp
->fastmap
;
3126 unsigned char *pattern
= bufp
->buffer
;
3127 unsigned char *p
= pattern
;
3128 register unsigned char *pend
= pattern
+ bufp
->used
;
3131 /* This holds the pointer to the failure stack, when
3132 it is allocated relocatably. */
3133 fail_stack_elt_t
*failure_stack_ptr
;
3136 /* Assume that each path through the pattern can be null until
3137 proven otherwise. We set this false at the bottom of switch
3138 statement, to which we get only if a particular path doesn't
3139 match the empty string. */
3140 boolean path_can_be_null
= true;
3142 /* We aren't doing a `succeed_n' to begin with. */
3143 boolean succeed_n_p
= false;
3145 assert (fastmap
!= NULL
&& p
!= NULL
);
3148 bzero (fastmap
, 1 << BYTEWIDTH
); /* Assume nothing's valid. */
3149 bufp
->fastmap_accurate
= 1; /* It will be when we're done. */
3150 bufp
->can_be_null
= 0;
3154 if (p
== pend
|| *p
== succeed
)
3156 /* We have reached the (effective) end of pattern. */
3157 if (!FAIL_STACK_EMPTY ())
3159 bufp
->can_be_null
|= path_can_be_null
;
3161 /* Reset for next path. */
3162 path_can_be_null
= true;
3164 p
= fail_stack
.stack
[--fail_stack
.avail
].pointer
;
3172 /* We should never be about to go beyond the end of the pattern. */
3175 switch (SWITCH_ENUM_CAST ((re_opcode_t
) *p
++))
3178 /* I guess the idea here is to simply not bother with a fastmap
3179 if a backreference is used, since it's too hard to figure out
3180 the fastmap for the corresponding group. Setting
3181 `can_be_null' stops `re_search_2' from using the fastmap, so
3182 that is all we do. */
3184 bufp
->can_be_null
= 1;
3188 /* Following are the cases which match a character. These end
3197 for (j
= *p
++ * BYTEWIDTH
- 1; j
>= 0; j
--)
3198 if (p
[j
/ BYTEWIDTH
] & (1 << (j
% BYTEWIDTH
)))
3204 /* Chars beyond end of map must be allowed. */
3205 for (j
= *p
* BYTEWIDTH
; j
< (1 << BYTEWIDTH
); j
++)
3208 for (j
= *p
++ * BYTEWIDTH
- 1; j
>= 0; j
--)
3209 if (!(p
[j
/ BYTEWIDTH
] & (1 << (j
% BYTEWIDTH
))))
3215 for (j
= 0; j
< (1 << BYTEWIDTH
); j
++)
3216 if (SYNTAX (j
) == Sword
)
3222 for (j
= 0; j
< (1 << BYTEWIDTH
); j
++)
3223 if (SYNTAX (j
) != Sword
)
3230 int fastmap_newline
= fastmap
['\n'];
3232 /* `.' matches anything ... */
3233 for (j
= 0; j
< (1 << BYTEWIDTH
); j
++)
3236 /* ... except perhaps newline. */
3237 if (!(bufp
->syntax
& RE_DOT_NEWLINE
))
3238 fastmap
['\n'] = fastmap_newline
;
3240 /* Return if we have already set `can_be_null'; if we have,
3241 then the fastmap is irrelevant. Something's wrong here. */
3242 else if (bufp
->can_be_null
)
3245 /* Otherwise, have to check alternative paths. */
3252 for (j
= 0; j
< (1 << BYTEWIDTH
); j
++)
3253 if (SYNTAX (j
) == (enum syntaxcode
) k
)
3260 for (j
= 0; j
< (1 << BYTEWIDTH
); j
++)
3261 if (SYNTAX (j
) != (enum syntaxcode
) k
)
3266 /* All cases after this match the empty string. These end with
3286 case push_dummy_failure
:
3291 case pop_failure_jump
:
3292 case maybe_pop_jump
:
3295 case dummy_failure_jump
:
3296 EXTRACT_NUMBER_AND_INCR (j
, p
);
3301 /* Jump backward implies we just went through the body of a
3302 loop and matched nothing. Opcode jumped to should be
3303 `on_failure_jump' or `succeed_n'. Just treat it like an
3304 ordinary jump. For a * loop, it has pushed its failure
3305 point already; if so, discard that as redundant. */
3306 if ((re_opcode_t
) *p
!= on_failure_jump
3307 && (re_opcode_t
) *p
!= succeed_n
)
3311 EXTRACT_NUMBER_AND_INCR (j
, p
);
3314 /* If what's on the stack is where we are now, pop it. */
3315 if (!FAIL_STACK_EMPTY ()
3316 && fail_stack
.stack
[fail_stack
.avail
- 1].pointer
== p
)
3322 case on_failure_jump
:
3323 case on_failure_keep_string_jump
:
3324 handle_on_failure_jump
:
3325 EXTRACT_NUMBER_AND_INCR (j
, p
);
3327 /* For some patterns, e.g., `(a?)?', `p+j' here points to the
3328 end of the pattern. We don't want to push such a point,
3329 since when we restore it above, entering the switch will
3330 increment `p' past the end of the pattern. We don't need
3331 to push such a point since we obviously won't find any more
3332 fastmap entries beyond `pend'. Such a pattern can match
3333 the null string, though. */
3336 if (!PUSH_PATTERN_OP (p
+ j
, fail_stack
))
3338 RESET_FAIL_STACK ();
3343 bufp
->can_be_null
= 1;
3347 EXTRACT_NUMBER_AND_INCR (k
, p
); /* Skip the n. */
3348 succeed_n_p
= false;
3355 /* Get to the number of times to succeed. */
3358 /* Increment p past the n for when k != 0. */
3359 EXTRACT_NUMBER_AND_INCR (k
, p
);
3363 succeed_n_p
= true; /* Spaghetti code alert. */
3364 goto handle_on_failure_jump
;
3381 abort (); /* We have listed all the cases. */
3384 /* Getting here means we have found the possible starting
3385 characters for one path of the pattern -- and that the empty
3386 string does not match. We need not follow this path further.
3387 Instead, look at the next alternative (remembered on the
3388 stack), or quit if no more. The test at the top of the loop
3389 does these things. */
3390 path_can_be_null
= false;
3394 /* Set `can_be_null' for the last path (also the first path, if the
3395 pattern is empty). */
3396 bufp
->can_be_null
|= path_can_be_null
;
3399 RESET_FAIL_STACK ();
3401 } /* re_compile_fastmap */
3403 weak_alias (__re_compile_fastmap
, re_compile_fastmap
)
3406 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
3407 ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
3408 this memory for recording register information. STARTS and ENDS
3409 must be allocated using the malloc library routine, and must each
3410 be at least NUM_REGS * sizeof (regoff_t) bytes long.
3412 If NUM_REGS == 0, then subsequent matches should allocate their own
3415 Unless this function is called, the first search or match using
3416 PATTERN_BUFFER will allocate its own register data, without
3417 freeing the old data. */
3420 re_set_registers (bufp
, regs
, num_regs
, starts
, ends
)
3421 struct re_pattern_buffer
*bufp
;
3422 struct re_registers
*regs
;
3424 regoff_t
*starts
, *ends
;
3428 bufp
->regs_allocated
= REGS_REALLOCATE
;
3429 regs
->num_regs
= num_regs
;
3430 regs
->start
= starts
;
3435 bufp
->regs_allocated
= REGS_UNALLOCATED
;
3437 regs
->start
= regs
->end
= (regoff_t
*) 0;
3441 weak_alias (__re_set_registers
, re_set_registers
)
3444 /* Searching routines. */
3446 /* Like re_search_2, below, but only one string is specified, and
3447 doesn't let you say where to stop matching. */
3450 re_search (bufp
, string
, size
, startpos
, range
, regs
)
3451 struct re_pattern_buffer
*bufp
;
3453 int size
, startpos
, range
;
3454 struct re_registers
*regs
;
3456 return re_search_2 (bufp
, NULL
, 0, string
, size
, startpos
, range
,
3460 weak_alias (__re_search
, re_search
)
3464 /* Using the compiled pattern in BUFP->buffer, first tries to match the
3465 virtual concatenation of STRING1 and STRING2, starting first at index
3466 STARTPOS, then at STARTPOS + 1, and so on.
3468 STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
3470 RANGE is how far to scan while trying to match. RANGE = 0 means try
3471 only at STARTPOS; in general, the last start tried is STARTPOS +
3474 In REGS, return the indices of the virtual concatenation of STRING1
3475 and STRING2 that matched the entire BUFP->buffer and its contained
3478 Do not consider matching one past the index STOP in the virtual
3479 concatenation of STRING1 and STRING2.
3481 We return either the position in the strings at which the match was
3482 found, -1 if no match, or -2 if error (such as failure
3486 re_search_2 (bufp
, string1
, size1
, string2
, size2
, startpos
, range
, regs
, stop
)
3487 struct re_pattern_buffer
*bufp
;
3488 const char *string1
, *string2
;
3492 struct re_registers
*regs
;
3496 register char *fastmap
= bufp
->fastmap
;
3497 register RE_TRANSLATE_TYPE translate
= bufp
->translate
;
3498 int total_size
= size1
+ size2
;
3499 int endpos
= startpos
+ range
;
3501 /* Check for out-of-range STARTPOS. */
3502 if (startpos
< 0 || startpos
> total_size
)
3505 /* Fix up RANGE if it might eventually take us outside
3506 the virtual concatenation of STRING1 and STRING2.
3507 Make sure we won't move STARTPOS below 0 or above TOTAL_SIZE. */
3509 range
= 0 - startpos
;
3510 else if (endpos
> total_size
)
3511 range
= total_size
- startpos
;
3513 /* If the search isn't to be a backwards one, don't waste time in a
3514 search for a pattern that must be anchored. */
3515 if (bufp
->used
> 0 && (re_opcode_t
) bufp
->buffer
[0] == begbuf
&& range
> 0)
3524 /* In a forward search for something that starts with \=.
3525 don't keep searching past point. */
3526 if (bufp
->used
> 0 && (re_opcode_t
) bufp
->buffer
[0] == at_dot
&& range
> 0)
3528 range
= PT
- startpos
;
3534 /* Update the fastmap now if not correct already. */
3535 if (fastmap
&& !bufp
->fastmap_accurate
)
3536 if (re_compile_fastmap (bufp
) == -2)
3539 /* Loop through the string, looking for a place to start matching. */
3542 /* If a fastmap is supplied, skip quickly over characters that
3543 cannot be the start of a match. If the pattern can match the
3544 null string, however, we don't need to skip characters; we want
3545 the first null string. */
3546 if (fastmap
&& startpos
< total_size
&& !bufp
->can_be_null
)
3548 if (range
> 0) /* Searching forwards. */
3550 register const char *d
;
3551 register int lim
= 0;
3554 if (startpos
< size1
&& startpos
+ range
>= size1
)
3555 lim
= range
- (size1
- startpos
);
3557 d
= (startpos
>= size1
? string2
- size1
: string1
) + startpos
;
3559 /* Written out as an if-else to avoid testing `translate'
3563 && !fastmap
[(unsigned char)
3564 translate
[(unsigned char) *d
++]])
3567 while (range
> lim
&& !fastmap
[(unsigned char) *d
++])
3570 startpos
+= irange
- range
;
3572 else /* Searching backwards. */
3574 register char c
= (size1
== 0 || startpos
>= size1
3575 ? string2
[startpos
- size1
]
3576 : string1
[startpos
]);
3578 if (!fastmap
[(unsigned char) TRANSLATE (c
)])
3583 /* If can't match the null string, and that's all we have left, fail. */
3584 if (range
>= 0 && startpos
== total_size
&& fastmap
3585 && !bufp
->can_be_null
)
3588 val
= re_match_2_internal (bufp
, string1
, size1
, string2
, size2
,
3589 startpos
, regs
, stop
);
3590 #ifndef REGEX_MALLOC
3619 weak_alias (__re_search_2
, re_search_2
)
3622 /* This converts PTR, a pointer into one of the search strings `string1'
3623 and `string2' into an offset from the beginning of that string. */
3624 #define POINTER_TO_OFFSET(ptr) \
3625 (FIRST_STRING_P (ptr) \
3626 ? ((regoff_t) ((ptr) - string1)) \
3627 : ((regoff_t) ((ptr) - string2 + size1)))
3629 /* Macros for dealing with the split strings in re_match_2. */
3631 #define MATCHING_IN_FIRST_STRING (dend == end_match_1)
3633 /* Call before fetching a character with *d. This switches over to
3634 string2 if necessary. */
3635 #define PREFETCH() \
3638 /* End of string2 => fail. */ \
3639 if (dend == end_match_2) \
3641 /* End of string1 => advance to string2. */ \
3643 dend = end_match_2; \
3647 /* Test if at very beginning or at very end of the virtual concatenation
3648 of `string1' and `string2'. If only one string, it's `string2'. */
3649 #define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2)
3650 #define AT_STRINGS_END(d) ((d) == end2)
3653 /* Test if D points to a character which is word-constituent. We have
3654 two special cases to check for: if past the end of string1, look at
3655 the first character in string2; and if before the beginning of
3656 string2, look at the last character in string1. */
3657 #define WORDCHAR_P(d) \
3658 (SYNTAX ((d) == end1 ? *string2 \
3659 : (d) == string2 - 1 ? *(end1 - 1) : *(d)) \
3662 /* Disabled due to a compiler bug -- see comment at case wordbound */
3664 /* Test if the character before D and the one at D differ with respect
3665 to being word-constituent. */
3666 #define AT_WORD_BOUNDARY(d) \
3667 (AT_STRINGS_BEG (d) || AT_STRINGS_END (d) \
3668 || WORDCHAR_P (d - 1) != WORDCHAR_P (d))
3671 /* Free everything we malloc. */
3672 #ifdef MATCH_MAY_ALLOCATE
3673 # define FREE_VAR(var) if (var) REGEX_FREE (var); var = NULL
3674 # define FREE_VARIABLES() \
3676 REGEX_FREE_STACK (fail_stack.stack); \
3677 FREE_VAR (regstart); \
3678 FREE_VAR (regend); \
3679 FREE_VAR (old_regstart); \
3680 FREE_VAR (old_regend); \
3681 FREE_VAR (best_regstart); \
3682 FREE_VAR (best_regend); \
3683 FREE_VAR (reg_info); \
3684 FREE_VAR (reg_dummy); \
3685 FREE_VAR (reg_info_dummy); \
3688 # define FREE_VARIABLES() ((void)0) /* Do nothing! But inhibit gcc warning. */
3689 #endif /* not MATCH_MAY_ALLOCATE */
3691 /* These values must meet several constraints. They must not be valid
3692 register values; since we have a limit of 255 registers (because
3693 we use only one byte in the pattern for the register number), we can
3694 use numbers larger than 255. They must differ by 1, because of
3695 NUM_FAILURE_ITEMS above. And the value for the lowest register must
3696 be larger than the value for the highest register, so we do not try
3697 to actually save any registers when none are active. */
3698 #define NO_HIGHEST_ACTIVE_REG (1 << BYTEWIDTH)
3699 #define NO_LOWEST_ACTIVE_REG (NO_HIGHEST_ACTIVE_REG + 1)
3701 /* Matching routines. */
3703 #ifndef emacs /* Emacs never uses this. */
3704 /* re_match is like re_match_2 except it takes only a single string. */
3707 re_match (bufp
, string
, size
, pos
, regs
)
3708 struct re_pattern_buffer
*bufp
;
3711 struct re_registers
*regs
;
3713 int result
= re_match_2_internal (bufp
, NULL
, 0, string
, size
,
3715 # ifndef REGEX_MALLOC
3723 weak_alias (__re_match
, re_match
)
3725 #endif /* not emacs */
3727 static boolean group_match_null_string_p
_RE_ARGS ((unsigned char **p
,
3729 register_info_type
*reg_info
));
3730 static boolean alt_match_null_string_p
_RE_ARGS ((unsigned char *p
,
3732 register_info_type
*reg_info
));
3733 static boolean common_op_match_null_string_p
_RE_ARGS ((unsigned char **p
,
3735 register_info_type
*reg_info
));
3736 static int bcmp_translate
_RE_ARGS ((const char *s1
, const char *s2
,
3737 int len
, char *translate
));
3739 /* re_match_2 matches the compiled pattern in BUFP against the
3740 the (virtual) concatenation of STRING1 and STRING2 (of length SIZE1
3741 and SIZE2, respectively). We start matching at POS, and stop
3744 If REGS is non-null and the `no_sub' field of BUFP is nonzero, we
3745 store offsets for the substring each group matched in REGS. See the
3746 documentation for exactly how many groups we fill.
3748 We return -1 if no match, -2 if an internal error (such as the
3749 failure stack overflowing). Otherwise, we return the length of the
3750 matched substring. */
3753 re_match_2 (bufp
, string1
, size1
, string2
, size2
, pos
, regs
, stop
)
3754 struct re_pattern_buffer
*bufp
;
3755 const char *string1
, *string2
;
3758 struct re_registers
*regs
;
3761 int result
= re_match_2_internal (bufp
, string1
, size1
, string2
, size2
,
3763 #ifndef REGEX_MALLOC
3771 weak_alias (__re_match_2
, re_match_2
)
3774 /* This is a separate function so that we can force an alloca cleanup
3777 re_match_2_internal (bufp
, string1
, size1
, string2
, size2
, pos
, regs
, stop
)
3778 struct re_pattern_buffer
*bufp
;
3779 const char *string1
, *string2
;
3782 struct re_registers
*regs
;
3785 /* General temporaries. */
3789 /* Just past the end of the corresponding string. */
3790 const char *end1
, *end2
;
3792 /* Pointers into string1 and string2, just past the last characters in
3793 each to consider matching. */
3794 const char *end_match_1
, *end_match_2
;
3796 /* Where we are in the data, and the end of the current string. */
3797 const char *d
, *dend
;
3799 /* Where we are in the pattern, and the end of the pattern. */
3800 unsigned char *p
= bufp
->buffer
;
3801 register unsigned char *pend
= p
+ bufp
->used
;
3803 /* Mark the opcode just after a start_memory, so we can test for an
3804 empty subpattern when we get to the stop_memory. */
3805 unsigned char *just_past_start_mem
= 0;
3807 /* We use this to map every character in the string. */
3808 RE_TRANSLATE_TYPE translate
= bufp
->translate
;
3810 /* Failure point stack. Each place that can handle a failure further
3811 down the line pushes a failure point on this stack. It consists of
3812 restart, regend, and reg_info for all registers corresponding to
3813 the subexpressions we're currently inside, plus the number of such
3814 registers, and, finally, two char *'s. The first char * is where
3815 to resume scanning the pattern; the second one is where to resume
3816 scanning the strings. If the latter is zero, the failure point is
3817 a ``dummy''; if a failure happens and the failure point is a dummy,
3818 it gets discarded and the next next one is tried. */
3819 #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
3820 fail_stack_type fail_stack
;
3823 static unsigned failure_id
= 0;
3824 unsigned nfailure_points_pushed
= 0, nfailure_points_popped
= 0;
3828 /* This holds the pointer to the failure stack, when
3829 it is allocated relocatably. */
3830 fail_stack_elt_t
*failure_stack_ptr
;
3833 /* We fill all the registers internally, independent of what we
3834 return, for use in backreferences. The number here includes
3835 an element for register zero. */
3836 size_t num_regs
= bufp
->re_nsub
+ 1;
3838 /* The currently active registers. */
3839 active_reg_t lowest_active_reg
= NO_LOWEST_ACTIVE_REG
;
3840 active_reg_t highest_active_reg
= NO_HIGHEST_ACTIVE_REG
;
3842 /* Information on the contents of registers. These are pointers into
3843 the input strings; they record just what was matched (on this
3844 attempt) by a subexpression part of the pattern, that is, the
3845 regnum-th regstart pointer points to where in the pattern we began
3846 matching and the regnum-th regend points to right after where we
3847 stopped matching the regnum-th subexpression. (The zeroth register
3848 keeps track of what the whole pattern matches.) */
3849 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
3850 const char **regstart
, **regend
;
3853 /* If a group that's operated upon by a repetition operator fails to
3854 match anything, then the register for its start will need to be
3855 restored because it will have been set to wherever in the string we
3856 are when we last see its open-group operator. Similarly for a
3858 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
3859 const char **old_regstart
, **old_regend
;
3862 /* The is_active field of reg_info helps us keep track of which (possibly
3863 nested) subexpressions we are currently in. The matched_something
3864 field of reg_info[reg_num] helps us tell whether or not we have
3865 matched any of the pattern so far this time through the reg_num-th
3866 subexpression. These two fields get reset each time through any
3867 loop their register is in. */
3868 #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
3869 register_info_type
*reg_info
;
3872 /* The following record the register info as found in the above
3873 variables when we find a match better than any we've seen before.
3874 This happens as we backtrack through the failure points, which in
3875 turn happens only if we have not yet matched the entire string. */
3876 unsigned best_regs_set
= false;
3877 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
3878 const char **best_regstart
, **best_regend
;
3881 /* Logically, this is `best_regend[0]'. But we don't want to have to
3882 allocate space for that if we're not allocating space for anything
3883 else (see below). Also, we never need info about register 0 for
3884 any of the other register vectors, and it seems rather a kludge to
3885 treat `best_regend' differently than the rest. So we keep track of
3886 the end of the best match so far in a separate variable. We
3887 initialize this to NULL so that when we backtrack the first time
3888 and need to test it, it's not garbage. */
3889 const char *match_end
= NULL
;
3891 /* This helps SET_REGS_MATCHED avoid doing redundant work. */
3892 int set_regs_matched_done
= 0;
3894 /* Used when we pop values we don't care about. */
3895 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
3896 const char **reg_dummy
;
3897 register_info_type
*reg_info_dummy
;
3901 /* Counts the total number of registers pushed. */
3902 unsigned num_regs_pushed
= 0;
3905 DEBUG_PRINT1 ("\n\nEntering re_match_2.\n");
3909 #ifdef MATCH_MAY_ALLOCATE
3910 /* Do not bother to initialize all the register variables if there are
3911 no groups in the pattern, as it takes a fair amount of time. If
3912 there are groups, we include space for register 0 (the whole
3913 pattern), even though we never use it, since it simplifies the
3914 array indexing. We should fix this. */
3917 regstart
= REGEX_TALLOC (num_regs
, const char *);
3918 regend
= REGEX_TALLOC (num_regs
, const char *);
3919 old_regstart
= REGEX_TALLOC (num_regs
, const char *);
3920 old_regend
= REGEX_TALLOC (num_regs
, const char *);
3921 best_regstart
= REGEX_TALLOC (num_regs
, const char *);
3922 best_regend
= REGEX_TALLOC (num_regs
, const char *);
3923 reg_info
= REGEX_TALLOC (num_regs
, register_info_type
);
3924 reg_dummy
= REGEX_TALLOC (num_regs
, const char *);
3925 reg_info_dummy
= REGEX_TALLOC (num_regs
, register_info_type
);
3927 if (!(regstart
&& regend
&& old_regstart
&& old_regend
&& reg_info
3928 && best_regstart
&& best_regend
&& reg_dummy
&& reg_info_dummy
))
3936 /* We must initialize all our variables to NULL, so that
3937 `FREE_VARIABLES' doesn't try to free them. */
3938 regstart
= regend
= old_regstart
= old_regend
= best_regstart
3939 = best_regend
= reg_dummy
= NULL
;
3940 reg_info
= reg_info_dummy
= (register_info_type
*) NULL
;
3942 #endif /* MATCH_MAY_ALLOCATE */
3944 /* The starting position is bogus. */
3945 if (pos
< 0 || pos
> size1
+ size2
)
3951 /* Initialize subexpression text positions to -1 to mark ones that no
3952 start_memory/stop_memory has been seen for. Also initialize the
3953 register information struct. */
3954 for (mcnt
= 1; (unsigned) mcnt
< num_regs
; mcnt
++)
3956 regstart
[mcnt
] = regend
[mcnt
]
3957 = old_regstart
[mcnt
] = old_regend
[mcnt
] = REG_UNSET_VALUE
;
3959 REG_MATCH_NULL_STRING_P (reg_info
[mcnt
]) = MATCH_NULL_UNSET_VALUE
;
3960 IS_ACTIVE (reg_info
[mcnt
]) = 0;
3961 MATCHED_SOMETHING (reg_info
[mcnt
]) = 0;
3962 EVER_MATCHED_SOMETHING (reg_info
[mcnt
]) = 0;
3965 /* We move `string1' into `string2' if the latter's empty -- but not if
3966 `string1' is null. */
3967 if (size2
== 0 && string1
!= NULL
)
3974 end1
= string1
+ size1
;
3975 end2
= string2
+ size2
;
3977 /* Compute where to stop matching, within the two strings. */
3980 end_match_1
= string1
+ stop
;
3981 end_match_2
= string2
;
3986 end_match_2
= string2
+ stop
- size1
;
3989 /* `p' scans through the pattern as `d' scans through the data.
3990 `dend' is the end of the input string that `d' points within. `d'
3991 is advanced into the following input string whenever necessary, but
3992 this happens before fetching; therefore, at the beginning of the
3993 loop, `d' can be pointing at the end of a string, but it cannot
3995 if (size1
> 0 && pos
<= size1
)
4002 d
= string2
+ pos
- size1
;
4006 DEBUG_PRINT1 ("The compiled pattern is:\n");
4007 DEBUG_PRINT_COMPILED_PATTERN (bufp
, p
, pend
);
4008 DEBUG_PRINT1 ("The string to match is: `");
4009 DEBUG_PRINT_DOUBLE_STRING (d
, string1
, size1
, string2
, size2
);
4010 DEBUG_PRINT1 ("'\n");
4012 /* This loops over pattern commands. It exits by returning from the
4013 function if the match is complete, or it drops through if the match
4014 fails at this starting point in the input data. */
4018 DEBUG_PRINT2 ("\n%p: ", p
);
4020 DEBUG_PRINT2 ("\n0x%x: ", p
);
4024 { /* End of pattern means we might have succeeded. */
4025 DEBUG_PRINT1 ("end of pattern ... ");
4027 /* If we haven't matched the entire string, and we want the
4028 longest match, try backtracking. */
4029 if (d
!= end_match_2
)
4031 /* 1 if this match ends in the same string (string1 or string2)
4032 as the best previous match. */
4033 boolean same_str_p
= (FIRST_STRING_P (match_end
)
4034 == MATCHING_IN_FIRST_STRING
);
4035 /* 1 if this match is the best seen so far. */
4036 boolean best_match_p
;
4038 /* AIX compiler got confused when this was combined
4039 with the previous declaration. */
4041 best_match_p
= d
> match_end
;
4043 best_match_p
= !MATCHING_IN_FIRST_STRING
;
4045 DEBUG_PRINT1 ("backtracking.\n");
4047 if (!FAIL_STACK_EMPTY ())
4048 { /* More failure points to try. */
4050 /* If exceeds best match so far, save it. */
4051 if (!best_regs_set
|| best_match_p
)
4053 best_regs_set
= true;
4056 DEBUG_PRINT1 ("\nSAVING match as best so far.\n");
4058 for (mcnt
= 1; (unsigned) mcnt
< num_regs
; mcnt
++)
4060 best_regstart
[mcnt
] = regstart
[mcnt
];
4061 best_regend
[mcnt
] = regend
[mcnt
];
4067 /* If no failure points, don't restore garbage. And if
4068 last match is real best match, don't restore second
4070 else if (best_regs_set
&& !best_match_p
)
4073 /* Restore best match. It may happen that `dend ==
4074 end_match_1' while the restored d is in string2.
4075 For example, the pattern `x.*y.*z' against the
4076 strings `x-' and `y-z-', if the two strings are
4077 not consecutive in memory. */
4078 DEBUG_PRINT1 ("Restoring best registers.\n");
4081 dend
= ((d
>= string1
&& d
<= end1
)
4082 ? end_match_1
: end_match_2
);
4084 for (mcnt
= 1; (unsigned) mcnt
< num_regs
; mcnt
++)
4086 regstart
[mcnt
] = best_regstart
[mcnt
];
4087 regend
[mcnt
] = best_regend
[mcnt
];
4090 } /* d != end_match_2 */
4093 DEBUG_PRINT1 ("Accepting match.\n");
4095 /* If caller wants register contents data back, do it. */
4096 if (regs
&& !bufp
->no_sub
)
4098 /* Have the register data arrays been allocated? */
4099 if (bufp
->regs_allocated
== REGS_UNALLOCATED
)
4100 { /* No. So allocate them with malloc. We need one
4101 extra element beyond `num_regs' for the `-1' marker
4103 regs
->num_regs
= MAX (RE_NREGS
, num_regs
+ 1);
4104 regs
->start
= TALLOC (regs
->num_regs
, regoff_t
);
4105 regs
->end
= TALLOC (regs
->num_regs
, regoff_t
);
4106 if (regs
->start
== NULL
|| regs
->end
== NULL
)
4111 bufp
->regs_allocated
= REGS_REALLOCATE
;
4113 else if (bufp
->regs_allocated
== REGS_REALLOCATE
)
4114 { /* Yes. If we need more elements than were already
4115 allocated, reallocate them. If we need fewer, just
4117 if (regs
->num_regs
< num_regs
+ 1)
4119 regs
->num_regs
= num_regs
+ 1;
4120 RETALLOC (regs
->start
, regs
->num_regs
, regoff_t
);
4121 RETALLOC (regs
->end
, regs
->num_regs
, regoff_t
);
4122 if (regs
->start
== NULL
|| regs
->end
== NULL
)
4131 /* These braces fend off a "empty body in an else-statement"
4132 warning under GCC when assert expands to nothing. */
4133 assert (bufp
->regs_allocated
== REGS_FIXED
);
4136 /* Convert the pointer data in `regstart' and `regend' to
4137 indices. Register zero has to be set differently,
4138 since we haven't kept track of any info for it. */
4139 if (regs
->num_regs
> 0)
4141 regs
->start
[0] = pos
;
4142 regs
->end
[0] = (MATCHING_IN_FIRST_STRING
4143 ? ((regoff_t
) (d
- string1
))
4144 : ((regoff_t
) (d
- string2
+ size1
)));
4147 /* Go through the first `min (num_regs, regs->num_regs)'
4148 registers, since that is all we initialized. */
4149 for (mcnt
= 1; (unsigned) mcnt
< MIN (num_regs
, regs
->num_regs
);
4152 if (REG_UNSET (regstart
[mcnt
]) || REG_UNSET (regend
[mcnt
]))
4153 regs
->start
[mcnt
] = regs
->end
[mcnt
] = -1;
4157 = (regoff_t
) POINTER_TO_OFFSET (regstart
[mcnt
]);
4159 = (regoff_t
) POINTER_TO_OFFSET (regend
[mcnt
]);
4163 /* If the regs structure we return has more elements than
4164 were in the pattern, set the extra elements to -1. If
4165 we (re)allocated the registers, this is the case,
4166 because we always allocate enough to have at least one
4168 for (mcnt
= num_regs
; (unsigned) mcnt
< regs
->num_regs
; mcnt
++)
4169 regs
->start
[mcnt
] = regs
->end
[mcnt
] = -1;
4170 } /* regs && !bufp->no_sub */
4172 DEBUG_PRINT4 ("%u failure points pushed, %u popped (%u remain).\n",
4173 nfailure_points_pushed
, nfailure_points_popped
,
4174 nfailure_points_pushed
- nfailure_points_popped
);
4175 DEBUG_PRINT2 ("%u registers pushed.\n", num_regs_pushed
);
4177 mcnt
= d
- pos
- (MATCHING_IN_FIRST_STRING
4181 DEBUG_PRINT2 ("Returning %d from re_match_2.\n", mcnt
);
4187 /* Otherwise match next pattern command. */
4188 switch (SWITCH_ENUM_CAST ((re_opcode_t
) *p
++))
4190 /* Ignore these. Used to ignore the n of succeed_n's which
4191 currently have n == 0. */
4193 DEBUG_PRINT1 ("EXECUTING no_op.\n");
4197 DEBUG_PRINT1 ("EXECUTING succeed.\n");
4200 /* Match the next n pattern characters exactly. The following
4201 byte in the pattern defines n, and the n bytes after that
4202 are the characters to match. */
4205 DEBUG_PRINT2 ("EXECUTING exactn %d.\n", mcnt
);
4207 /* This is written out as an if-else so we don't waste time
4208 testing `translate' inside the loop. */
4214 if ((unsigned char) translate
[(unsigned char) *d
++]
4215 != (unsigned char) *p
++)
4225 if (*d
++ != (char) *p
++) goto fail
;
4229 SET_REGS_MATCHED ();
4233 /* Match any character except possibly a newline or a null. */
4235 DEBUG_PRINT1 ("EXECUTING anychar.\n");
4239 if ((!(bufp
->syntax
& RE_DOT_NEWLINE
) && TRANSLATE (*d
) == '\n')
4240 || (bufp
->syntax
& RE_DOT_NOT_NULL
&& TRANSLATE (*d
) == '\000'))
4243 SET_REGS_MATCHED ();
4244 DEBUG_PRINT2 (" Matched `%d'.\n", *d
);
4252 register unsigned char c
;
4253 boolean
not = (re_opcode_t
) *(p
- 1) == charset_not
;
4255 DEBUG_PRINT2 ("EXECUTING charset%s.\n", not ? "_not" : "");
4258 c
= TRANSLATE (*d
); /* The character to match. */
4260 /* Cast to `unsigned' instead of `unsigned char' in case the
4261 bit list is a full 32 bytes long. */
4262 if (c
< (unsigned) (*p
* BYTEWIDTH
)
4263 && p
[1 + c
/ BYTEWIDTH
] & (1 << (c
% BYTEWIDTH
)))
4268 if (!not) goto fail
;
4270 SET_REGS_MATCHED ();
4276 /* The beginning of a group is represented by start_memory.
4277 The arguments are the register number in the next byte, and the
4278 number of groups inner to this one in the next. The text
4279 matched within the group is recorded (in the internal
4280 registers data structure) under the register number. */
4282 DEBUG_PRINT3 ("EXECUTING start_memory %d (%d):\n", *p
, p
[1]);
4284 /* Find out if this group can match the empty string. */
4285 p1
= p
; /* To send to group_match_null_string_p. */
4287 if (REG_MATCH_NULL_STRING_P (reg_info
[*p
]) == MATCH_NULL_UNSET_VALUE
)
4288 REG_MATCH_NULL_STRING_P (reg_info
[*p
])
4289 = group_match_null_string_p (&p1
, pend
, reg_info
);
4291 /* Save the position in the string where we were the last time
4292 we were at this open-group operator in case the group is
4293 operated upon by a repetition operator, e.g., with `(a*)*b'
4294 against `ab'; then we want to ignore where we are now in
4295 the string in case this attempt to match fails. */
4296 old_regstart
[*p
] = REG_MATCH_NULL_STRING_P (reg_info
[*p
])
4297 ? REG_UNSET (regstart
[*p
]) ? d
: regstart
[*p
]
4299 DEBUG_PRINT2 (" old_regstart: %d\n",
4300 POINTER_TO_OFFSET (old_regstart
[*p
]));
4303 DEBUG_PRINT2 (" regstart: %d\n", POINTER_TO_OFFSET (regstart
[*p
]));
4305 IS_ACTIVE (reg_info
[*p
]) = 1;
4306 MATCHED_SOMETHING (reg_info
[*p
]) = 0;
4308 /* Clear this whenever we change the register activity status. */
4309 set_regs_matched_done
= 0;
4311 /* This is the new highest active register. */
4312 highest_active_reg
= *p
;
4314 /* If nothing was active before, this is the new lowest active
4316 if (lowest_active_reg
== NO_LOWEST_ACTIVE_REG
)
4317 lowest_active_reg
= *p
;
4319 /* Move past the register number and inner group count. */
4321 just_past_start_mem
= p
;
4326 /* The stop_memory opcode represents the end of a group. Its
4327 arguments are the same as start_memory's: the register
4328 number, and the number of inner groups. */
4330 DEBUG_PRINT3 ("EXECUTING stop_memory %d (%d):\n", *p
, p
[1]);
4332 /* We need to save the string position the last time we were at
4333 this close-group operator in case the group is operated
4334 upon by a repetition operator, e.g., with `((a*)*(b*)*)*'
4335 against `aba'; then we want to ignore where we are now in
4336 the string in case this attempt to match fails. */
4337 old_regend
[*p
] = REG_MATCH_NULL_STRING_P (reg_info
[*p
])
4338 ? REG_UNSET (regend
[*p
]) ? d
: regend
[*p
]
4340 DEBUG_PRINT2 (" old_regend: %d\n",
4341 POINTER_TO_OFFSET (old_regend
[*p
]));
4344 DEBUG_PRINT2 (" regend: %d\n", POINTER_TO_OFFSET (regend
[*p
]));
4346 /* This register isn't active anymore. */
4347 IS_ACTIVE (reg_info
[*p
]) = 0;
4349 /* Clear this whenever we change the register activity status. */
4350 set_regs_matched_done
= 0;
4352 /* If this was the only register active, nothing is active
4354 if (lowest_active_reg
== highest_active_reg
)
4356 lowest_active_reg
= NO_LOWEST_ACTIVE_REG
;
4357 highest_active_reg
= NO_HIGHEST_ACTIVE_REG
;
4360 { /* We must scan for the new highest active register, since
4361 it isn't necessarily one less than now: consider
4362 (a(b)c(d(e)f)g). When group 3 ends, after the f), the
4363 new highest active register is 1. */
4364 unsigned char r
= *p
- 1;
4365 while (r
> 0 && !IS_ACTIVE (reg_info
[r
]))
4368 /* If we end up at register zero, that means that we saved
4369 the registers as the result of an `on_failure_jump', not
4370 a `start_memory', and we jumped to past the innermost
4371 `stop_memory'. For example, in ((.)*) we save
4372 registers 1 and 2 as a result of the *, but when we pop
4373 back to the second ), we are at the stop_memory 1.
4374 Thus, nothing is active. */
4377 lowest_active_reg
= NO_LOWEST_ACTIVE_REG
;
4378 highest_active_reg
= NO_HIGHEST_ACTIVE_REG
;
4381 highest_active_reg
= r
;
4384 /* If just failed to match something this time around with a
4385 group that's operated on by a repetition operator, try to
4386 force exit from the ``loop'', and restore the register
4387 information for this group that we had before trying this
4389 if ((!MATCHED_SOMETHING (reg_info
[*p
])
4390 || just_past_start_mem
== p
- 1)
4393 boolean is_a_jump_n
= false;
4397 switch ((re_opcode_t
) *p1
++)
4401 case pop_failure_jump
:
4402 case maybe_pop_jump
:
4404 case dummy_failure_jump
:
4405 EXTRACT_NUMBER_AND_INCR (mcnt
, p1
);
4415 /* If the next operation is a jump backwards in the pattern
4416 to an on_failure_jump right before the start_memory
4417 corresponding to this stop_memory, exit from the loop
4418 by forcing a failure after pushing on the stack the
4419 on_failure_jump's jump in the pattern, and d. */
4420 if (mcnt
< 0 && (re_opcode_t
) *p1
== on_failure_jump
4421 && (re_opcode_t
) p1
[3] == start_memory
&& p1
[4] == *p
)
4423 /* If this group ever matched anything, then restore
4424 what its registers were before trying this last
4425 failed match, e.g., with `(a*)*b' against `ab' for
4426 regstart[1], and, e.g., with `((a*)*(b*)*)*'
4427 against `aba' for regend[3].
4429 Also restore the registers for inner groups for,
4430 e.g., `((a*)(b*))*' against `aba' (register 3 would
4431 otherwise get trashed). */
4433 if (EVER_MATCHED_SOMETHING (reg_info
[*p
]))
4437 EVER_MATCHED_SOMETHING (reg_info
[*p
]) = 0;
4439 /* Restore this and inner groups' (if any) registers. */
4440 for (r
= *p
; r
< (unsigned) *p
+ (unsigned) *(p
+ 1);
4443 regstart
[r
] = old_regstart
[r
];
4445 /* xx why this test? */
4446 if (old_regend
[r
] >= regstart
[r
])
4447 regend
[r
] = old_regend
[r
];
4451 EXTRACT_NUMBER_AND_INCR (mcnt
, p1
);
4452 PUSH_FAILURE_POINT (p1
+ mcnt
, d
, -2);
4458 /* Move past the register number and the inner group count. */
4463 /* \<digit> has been turned into a `duplicate' command which is
4464 followed by the numeric value of <digit> as the register number. */
4467 register const char *d2
, *dend2
;
4468 int regno
= *p
++; /* Get which register to match against. */
4469 DEBUG_PRINT2 ("EXECUTING duplicate %d.\n", regno
);
4471 /* Can't back reference a group which we've never matched. */
4472 if (REG_UNSET (regstart
[regno
]) || REG_UNSET (regend
[regno
]))
4475 /* Where in input to try to start matching. */
4476 d2
= regstart
[regno
];
4478 /* Where to stop matching; if both the place to start and
4479 the place to stop matching are in the same string, then
4480 set to the place to stop, otherwise, for now have to use
4481 the end of the first string. */
4483 dend2
= ((FIRST_STRING_P (regstart
[regno
])
4484 == FIRST_STRING_P (regend
[regno
]))
4485 ? regend
[regno
] : end_match_1
);
4488 /* If necessary, advance to next segment in register
4492 if (dend2
== end_match_2
) break;
4493 if (dend2
== regend
[regno
]) break;
4495 /* End of string1 => advance to string2. */
4497 dend2
= regend
[regno
];
4499 /* At end of register contents => success */
4500 if (d2
== dend2
) break;
4502 /* If necessary, advance to next segment in data. */
4505 /* How many characters left in this segment to match. */
4508 /* Want how many consecutive characters we can match in
4509 one shot, so, if necessary, adjust the count. */
4510 if (mcnt
> dend2
- d2
)
4513 /* Compare that many; failure if mismatch, else move
4516 ? bcmp_translate (d
, d2
, mcnt
, translate
)
4517 : memcmp (d
, d2
, mcnt
))
4519 d
+= mcnt
, d2
+= mcnt
;
4521 /* Do this because we've match some characters. */
4522 SET_REGS_MATCHED ();
4528 /* begline matches the empty string at the beginning of the string
4529 (unless `not_bol' is set in `bufp'), and, if
4530 `newline_anchor' is set, after newlines. */
4532 DEBUG_PRINT1 ("EXECUTING begline.\n");
4534 if (AT_STRINGS_BEG (d
))
4536 if (!bufp
->not_bol
) break;
4538 else if (d
[-1] == '\n' && bufp
->newline_anchor
)
4542 /* In all other cases, we fail. */
4546 /* endline is the dual of begline. */
4548 DEBUG_PRINT1 ("EXECUTING endline.\n");
4550 if (AT_STRINGS_END (d
))
4552 if (!bufp
->not_eol
) break;
4555 /* We have to ``prefetch'' the next character. */
4556 else if ((d
== end1
? *string2
: *d
) == '\n'
4557 && bufp
->newline_anchor
)
4564 /* Match at the very beginning of the data. */
4566 DEBUG_PRINT1 ("EXECUTING begbuf.\n");
4567 if (AT_STRINGS_BEG (d
))
4572 /* Match at the very end of the data. */
4574 DEBUG_PRINT1 ("EXECUTING endbuf.\n");
4575 if (AT_STRINGS_END (d
))
4580 /* on_failure_keep_string_jump is used to optimize `.*\n'. It
4581 pushes NULL as the value for the string on the stack. Then
4582 `pop_failure_point' will keep the current value for the
4583 string, instead of restoring it. To see why, consider
4584 matching `foo\nbar' against `.*\n'. The .* matches the foo;
4585 then the . fails against the \n. But the next thing we want
4586 to do is match the \n against the \n; if we restored the
4587 string value, we would be back at the foo.
4589 Because this is used only in specific cases, we don't need to
4590 check all the things that `on_failure_jump' does, to make
4591 sure the right things get saved on the stack. Hence we don't
4592 share its code. The only reason to push anything on the
4593 stack at all is that otherwise we would have to change
4594 `anychar's code to do something besides goto fail in this
4595 case; that seems worse than this. */
4596 case on_failure_keep_string_jump
:
4597 DEBUG_PRINT1 ("EXECUTING on_failure_keep_string_jump");
4599 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
4601 DEBUG_PRINT3 (" %d (to %p):\n", mcnt
, p
+ mcnt
);
4603 DEBUG_PRINT3 (" %d (to 0x%x):\n", mcnt
, p
+ mcnt
);
4606 PUSH_FAILURE_POINT (p
+ mcnt
, NULL
, -2);
4610 /* Uses of on_failure_jump:
4612 Each alternative starts with an on_failure_jump that points
4613 to the beginning of the next alternative. Each alternative
4614 except the last ends with a jump that in effect jumps past
4615 the rest of the alternatives. (They really jump to the
4616 ending jump of the following alternative, because tensioning
4617 these jumps is a hassle.)
4619 Repeats start with an on_failure_jump that points past both
4620 the repetition text and either the following jump or
4621 pop_failure_jump back to this on_failure_jump. */
4622 case on_failure_jump
:
4624 DEBUG_PRINT1 ("EXECUTING on_failure_jump");
4626 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
4628 DEBUG_PRINT3 (" %d (to %p)", mcnt
, p
+ mcnt
);
4630 DEBUG_PRINT3 (" %d (to 0x%x)", mcnt
, p
+ mcnt
);
4633 /* If this on_failure_jump comes right before a group (i.e.,
4634 the original * applied to a group), save the information
4635 for that group and all inner ones, so that if we fail back
4636 to this point, the group's information will be correct.
4637 For example, in \(a*\)*\1, we need the preceding group,
4638 and in \(zz\(a*\)b*\)\2, we need the inner group. */
4640 /* We can't use `p' to check ahead because we push
4641 a failure point to `p + mcnt' after we do this. */
4644 /* We need to skip no_op's before we look for the
4645 start_memory in case this on_failure_jump is happening as
4646 the result of a completed succeed_n, as in \(a\)\{1,3\}b\1
4648 while (p1
< pend
&& (re_opcode_t
) *p1
== no_op
)
4651 if (p1
< pend
&& (re_opcode_t
) *p1
== start_memory
)
4653 /* We have a new highest active register now. This will
4654 get reset at the start_memory we are about to get to,
4655 but we will have saved all the registers relevant to
4656 this repetition op, as described above. */
4657 highest_active_reg
= *(p1
+ 1) + *(p1
+ 2);
4658 if (lowest_active_reg
== NO_LOWEST_ACTIVE_REG
)
4659 lowest_active_reg
= *(p1
+ 1);
4662 DEBUG_PRINT1 (":\n");
4663 PUSH_FAILURE_POINT (p
+ mcnt
, d
, -2);
4667 /* A smart repeat ends with `maybe_pop_jump'.
4668 We change it to either `pop_failure_jump' or `jump'. */
4669 case maybe_pop_jump
:
4670 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
4671 DEBUG_PRINT2 ("EXECUTING maybe_pop_jump %d.\n", mcnt
);
4673 register unsigned char *p2
= p
;
4675 /* Compare the beginning of the repeat with what in the
4676 pattern follows its end. If we can establish that there
4677 is nothing that they would both match, i.e., that we
4678 would have to backtrack because of (as in, e.g., `a*a')
4679 then we can change to pop_failure_jump, because we'll
4680 never have to backtrack.
4682 This is not true in the case of alternatives: in
4683 `(a|ab)*' we do need to backtrack to the `ab' alternative
4684 (e.g., if the string was `ab'). But instead of trying to
4685 detect that here, the alternative has put on a dummy
4686 failure point which is what we will end up popping. */
4688 /* Skip over open/close-group commands.
4689 If what follows this loop is a ...+ construct,
4690 look at what begins its body, since we will have to
4691 match at least one of that. */
4695 && ((re_opcode_t
) *p2
== stop_memory
4696 || (re_opcode_t
) *p2
== start_memory
))
4698 else if (p2
+ 6 < pend
4699 && (re_opcode_t
) *p2
== dummy_failure_jump
)
4706 /* p1[0] ... p1[2] are the `on_failure_jump' corresponding
4707 to the `maybe_finalize_jump' of this case. Examine what
4710 /* If we're at the end of the pattern, we can change. */
4713 /* Consider what happens when matching ":\(.*\)"
4714 against ":/". I don't really understand this code
4716 p
[-3] = (unsigned char) pop_failure_jump
;
4718 (" End of pattern: change to `pop_failure_jump'.\n");
4721 else if ((re_opcode_t
) *p2
== exactn
4722 || (bufp
->newline_anchor
&& (re_opcode_t
) *p2
== endline
))
4724 register unsigned char c
4725 = *p2
== (unsigned char) endline
? '\n' : p2
[2];
4727 if ((re_opcode_t
) p1
[3] == exactn
&& p1
[5] != c
)
4729 p
[-3] = (unsigned char) pop_failure_jump
;
4730 DEBUG_PRINT3 (" %c != %c => pop_failure_jump.\n",
4734 else if ((re_opcode_t
) p1
[3] == charset
4735 || (re_opcode_t
) p1
[3] == charset_not
)
4737 int not = (re_opcode_t
) p1
[3] == charset_not
;
4739 if (c
< (unsigned char) (p1
[4] * BYTEWIDTH
)
4740 && p1
[5 + c
/ BYTEWIDTH
] & (1 << (c
% BYTEWIDTH
)))
4743 /* `not' is equal to 1 if c would match, which means
4744 that we can't change to pop_failure_jump. */
4747 p
[-3] = (unsigned char) pop_failure_jump
;
4748 DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
4752 else if ((re_opcode_t
) *p2
== charset
)
4755 register unsigned char c
4756 = *p2
== (unsigned char) endline
? '\n' : p2
[2];
4760 if ((re_opcode_t
) p1
[3] == exactn
4761 && ! ((int) p2
[1] * BYTEWIDTH
> (int) p1
[5]
4762 && (p2
[2 + p1
[5] / BYTEWIDTH
]
4763 & (1 << (p1
[5] % BYTEWIDTH
)))))
4765 if ((re_opcode_t
) p1
[3] == exactn
4766 && ! ((int) p2
[1] * BYTEWIDTH
> (int) p1
[4]
4767 && (p2
[2 + p1
[4] / BYTEWIDTH
]
4768 & (1 << (p1
[4] % BYTEWIDTH
)))))
4771 p
[-3] = (unsigned char) pop_failure_jump
;
4772 DEBUG_PRINT3 (" %c != %c => pop_failure_jump.\n",
4776 else if ((re_opcode_t
) p1
[3] == charset_not
)
4779 /* We win if the charset_not inside the loop
4780 lists every character listed in the charset after. */
4781 for (idx
= 0; idx
< (int) p2
[1]; idx
++)
4782 if (! (p2
[2 + idx
] == 0
4783 || (idx
< (int) p1
[4]
4784 && ((p2
[2 + idx
] & ~ p1
[5 + idx
]) == 0))))
4789 p
[-3] = (unsigned char) pop_failure_jump
;
4790 DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
4793 else if ((re_opcode_t
) p1
[3] == charset
)
4796 /* We win if the charset inside the loop
4797 has no overlap with the one after the loop. */
4799 idx
< (int) p2
[1] && idx
< (int) p1
[4];
4801 if ((p2
[2 + idx
] & p1
[5 + idx
]) != 0)
4804 if (idx
== p2
[1] || idx
== p1
[4])
4806 p
[-3] = (unsigned char) pop_failure_jump
;
4807 DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
4812 p
-= 2; /* Point at relative address again. */
4813 if ((re_opcode_t
) p
[-1] != pop_failure_jump
)
4815 p
[-1] = (unsigned char) jump
;
4816 DEBUG_PRINT1 (" Match => jump.\n");
4817 goto unconditional_jump
;
4819 /* Note fall through. */
4822 /* The end of a simple repeat has a pop_failure_jump back to
4823 its matching on_failure_jump, where the latter will push a
4824 failure point. The pop_failure_jump takes off failure
4825 points put on by this pop_failure_jump's matching
4826 on_failure_jump; we got through the pattern to here from the
4827 matching on_failure_jump, so didn't fail. */
4828 case pop_failure_jump
:
4830 /* We need to pass separate storage for the lowest and
4831 highest registers, even though we don't care about the
4832 actual values. Otherwise, we will restore only one
4833 register from the stack, since lowest will == highest in
4834 `pop_failure_point'. */
4835 active_reg_t dummy_low_reg
, dummy_high_reg
;
4836 unsigned char *pdummy
;
4839 DEBUG_PRINT1 ("EXECUTING pop_failure_jump.\n");
4840 POP_FAILURE_POINT (sdummy
, pdummy
,
4841 dummy_low_reg
, dummy_high_reg
,
4842 reg_dummy
, reg_dummy
, reg_info_dummy
);
4844 /* Note fall through. */
4848 DEBUG_PRINT2 ("\n%p: ", p
);
4850 DEBUG_PRINT2 ("\n0x%x: ", p
);
4852 /* Note fall through. */
4854 /* Unconditionally jump (without popping any failure points). */
4856 EXTRACT_NUMBER_AND_INCR (mcnt
, p
); /* Get the amount to jump. */
4857 DEBUG_PRINT2 ("EXECUTING jump %d ", mcnt
);
4858 p
+= mcnt
; /* Do the jump. */
4860 DEBUG_PRINT2 ("(to %p).\n", p
);
4862 DEBUG_PRINT2 ("(to 0x%x).\n", p
);
4867 /* We need this opcode so we can detect where alternatives end
4868 in `group_match_null_string_p' et al. */
4870 DEBUG_PRINT1 ("EXECUTING jump_past_alt.\n");
4871 goto unconditional_jump
;
4874 /* Normally, the on_failure_jump pushes a failure point, which
4875 then gets popped at pop_failure_jump. We will end up at
4876 pop_failure_jump, also, and with a pattern of, say, `a+', we
4877 are skipping over the on_failure_jump, so we have to push
4878 something meaningless for pop_failure_jump to pop. */
4879 case dummy_failure_jump
:
4880 DEBUG_PRINT1 ("EXECUTING dummy_failure_jump.\n");
4881 /* It doesn't matter what we push for the string here. What
4882 the code at `fail' tests is the value for the pattern. */
4883 PUSH_FAILURE_POINT (NULL
, NULL
, -2);
4884 goto unconditional_jump
;
4887 /* At the end of an alternative, we need to push a dummy failure
4888 point in case we are followed by a `pop_failure_jump', because
4889 we don't want the failure point for the alternative to be
4890 popped. For example, matching `(a|ab)*' against `aab'
4891 requires that we match the `ab' alternative. */
4892 case push_dummy_failure
:
4893 DEBUG_PRINT1 ("EXECUTING push_dummy_failure.\n");
4894 /* See comments just above at `dummy_failure_jump' about the
4896 PUSH_FAILURE_POINT (NULL
, NULL
, -2);
4899 /* Have to succeed matching what follows at least n times.
4900 After that, handle like `on_failure_jump'. */
4902 EXTRACT_NUMBER (mcnt
, p
+ 2);
4903 DEBUG_PRINT2 ("EXECUTING succeed_n %d.\n", mcnt
);
4906 /* Originally, this is how many times we HAVE to succeed. */
4911 STORE_NUMBER_AND_INCR (p
, mcnt
);
4913 DEBUG_PRINT3 (" Setting %p to %d.\n", p
- 2, mcnt
);
4915 DEBUG_PRINT3 (" Setting 0x%x to %d.\n", p
- 2, mcnt
);
4921 DEBUG_PRINT2 (" Setting two bytes from %p to no_op.\n", p
+2);
4923 DEBUG_PRINT2 (" Setting two bytes from 0x%x to no_op.\n", p
+2);
4925 p
[2] = (unsigned char) no_op
;
4926 p
[3] = (unsigned char) no_op
;
4932 EXTRACT_NUMBER (mcnt
, p
+ 2);
4933 DEBUG_PRINT2 ("EXECUTING jump_n %d.\n", mcnt
);
4935 /* Originally, this is how many times we CAN jump. */
4939 STORE_NUMBER (p
+ 2, mcnt
);
4941 DEBUG_PRINT3 (" Setting %p to %d.\n", p
+ 2, mcnt
);
4943 DEBUG_PRINT3 (" Setting 0x%x to %d.\n", p
+ 2, mcnt
);
4945 goto unconditional_jump
;
4947 /* If don't have to jump any more, skip over the rest of command. */
4954 DEBUG_PRINT1 ("EXECUTING set_number_at.\n");
4956 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
4958 EXTRACT_NUMBER_AND_INCR (mcnt
, p
);
4960 DEBUG_PRINT3 (" Setting %p to %d.\n", p1
, mcnt
);
4962 DEBUG_PRINT3 (" Setting 0x%x to %d.\n", p1
, mcnt
);
4964 STORE_NUMBER (p1
, mcnt
);
4969 /* The DEC Alpha C compiler 3.x generates incorrect code for the
4970 test WORDCHAR_P (d - 1) != WORDCHAR_P (d) in the expansion of
4971 AT_WORD_BOUNDARY, so this code is disabled. Expanding the
4972 macro and introducing temporary variables works around the bug. */
4975 DEBUG_PRINT1 ("EXECUTING wordbound.\n");
4976 if (AT_WORD_BOUNDARY (d
))
4981 DEBUG_PRINT1 ("EXECUTING notwordbound.\n");
4982 if (AT_WORD_BOUNDARY (d
))
4988 boolean prevchar
, thischar
;
4990 DEBUG_PRINT1 ("EXECUTING wordbound.\n");
4991 if (AT_STRINGS_BEG (d
) || AT_STRINGS_END (d
))
4994 prevchar
= WORDCHAR_P (d
- 1);
4995 thischar
= WORDCHAR_P (d
);
4996 if (prevchar
!= thischar
)
5003 boolean prevchar
, thischar
;
5005 DEBUG_PRINT1 ("EXECUTING notwordbound.\n");
5006 if (AT_STRINGS_BEG (d
) || AT_STRINGS_END (d
))
5009 prevchar
= WORDCHAR_P (d
- 1);
5010 thischar
= WORDCHAR_P (d
);
5011 if (prevchar
!= thischar
)
5018 DEBUG_PRINT1 ("EXECUTING wordbeg.\n");
5019 if (WORDCHAR_P (d
) && (AT_STRINGS_BEG (d
) || !WORDCHAR_P (d
- 1)))
5024 DEBUG_PRINT1 ("EXECUTING wordend.\n");
5025 if (!AT_STRINGS_BEG (d
) && WORDCHAR_P (d
- 1)
5026 && (!WORDCHAR_P (d
) || AT_STRINGS_END (d
)))
5032 DEBUG_PRINT1 ("EXECUTING before_dot.\n");
5033 if (PTR_CHAR_POS ((unsigned char *) d
) >= point
)
5038 DEBUG_PRINT1 ("EXECUTING at_dot.\n");
5039 if (PTR_CHAR_POS ((unsigned char *) d
) != point
)
5044 DEBUG_PRINT1 ("EXECUTING after_dot.\n");
5045 if (PTR_CHAR_POS ((unsigned char *) d
) <= point
)
5050 DEBUG_PRINT2 ("EXECUTING syntaxspec %d.\n", mcnt
);
5055 DEBUG_PRINT1 ("EXECUTING Emacs wordchar.\n");
5059 /* Can't use *d++ here; SYNTAX may be an unsafe macro. */
5061 if (SYNTAX (d
[-1]) != (enum syntaxcode
) mcnt
)
5063 SET_REGS_MATCHED ();
5067 DEBUG_PRINT2 ("EXECUTING notsyntaxspec %d.\n", mcnt
);
5069 goto matchnotsyntax
;
5072 DEBUG_PRINT1 ("EXECUTING Emacs notwordchar.\n");
5076 /* Can't use *d++ here; SYNTAX may be an unsafe macro. */
5078 if (SYNTAX (d
[-1]) == (enum syntaxcode
) mcnt
)
5080 SET_REGS_MATCHED ();
5083 #else /* not emacs */
5085 DEBUG_PRINT1 ("EXECUTING non-Emacs wordchar.\n");
5087 if (!WORDCHAR_P (d
))
5089 SET_REGS_MATCHED ();
5094 DEBUG_PRINT1 ("EXECUTING non-Emacs notwordchar.\n");
5098 SET_REGS_MATCHED ();
5101 #endif /* not emacs */
5106 continue; /* Successfully executed one pattern command; keep going. */
5109 /* We goto here if a matching operation fails. */
5111 if (!FAIL_STACK_EMPTY ())
5112 { /* A restart point is known. Restore to that state. */
5113 DEBUG_PRINT1 ("\nFAIL:\n");
5114 POP_FAILURE_POINT (d
, p
,
5115 lowest_active_reg
, highest_active_reg
,
5116 regstart
, regend
, reg_info
);
5118 /* If this failure point is a dummy, try the next one. */
5122 /* If we failed to the end of the pattern, don't examine *p. */
5126 boolean is_a_jump_n
= false;
5128 /* If failed to a backwards jump that's part of a repetition
5129 loop, need to pop this failure point and use the next one. */
5130 switch ((re_opcode_t
) *p
)
5134 case maybe_pop_jump
:
5135 case pop_failure_jump
:
5138 EXTRACT_NUMBER_AND_INCR (mcnt
, p1
);
5141 if ((is_a_jump_n
&& (re_opcode_t
) *p1
== succeed_n
)
5143 && (re_opcode_t
) *p1
== on_failure_jump
))
5151 if (d
>= string1
&& d
<= end1
)
5155 break; /* Matching at this starting point really fails. */
5159 goto restore_best_regs
;
5163 return -1; /* Failure to match. */
5166 /* Subroutine definitions for re_match_2. */
5169 /* We are passed P pointing to a register number after a start_memory.
5171 Return true if the pattern up to the corresponding stop_memory can
5172 match the empty string, and false otherwise.
5174 If we find the matching stop_memory, sets P to point to one past its number.
5175 Otherwise, sets P to an undefined byte less than or equal to END.
5177 We don't handle duplicates properly (yet). */
5180 group_match_null_string_p (p
, end
, reg_info
)
5181 unsigned char **p
, *end
;
5182 register_info_type
*reg_info
;
5185 /* Point to after the args to the start_memory. */
5186 unsigned char *p1
= *p
+ 2;
5190 /* Skip over opcodes that can match nothing, and return true or
5191 false, as appropriate, when we get to one that can't, or to the
5192 matching stop_memory. */
5194 switch ((re_opcode_t
) *p1
)
5196 /* Could be either a loop or a series of alternatives. */
5197 case on_failure_jump
:
5199 EXTRACT_NUMBER_AND_INCR (mcnt
, p1
);
5201 /* If the next operation is not a jump backwards in the
5206 /* Go through the on_failure_jumps of the alternatives,
5207 seeing if any of the alternatives cannot match nothing.
5208 The last alternative starts with only a jump,
5209 whereas the rest start with on_failure_jump and end
5210 with a jump, e.g., here is the pattern for `a|b|c':
5212 /on_failure_jump/0/6/exactn/1/a/jump_past_alt/0/6
5213 /on_failure_jump/0/6/exactn/1/b/jump_past_alt/0/3
5216 So, we have to first go through the first (n-1)
5217 alternatives and then deal with the last one separately. */
5220 /* Deal with the first (n-1) alternatives, which start
5221 with an on_failure_jump (see above) that jumps to right
5222 past a jump_past_alt. */
5224 while ((re_opcode_t
) p1
[mcnt
-3] == jump_past_alt
)
5226 /* `mcnt' holds how many bytes long the alternative
5227 is, including the ending `jump_past_alt' and
5230 if (!alt_match_null_string_p (p1
, p1
+ mcnt
- 3,
5234 /* Move to right after this alternative, including the
5238 /* Break if it's the beginning of an n-th alternative
5239 that doesn't begin with an on_failure_jump. */
5240 if ((re_opcode_t
) *p1
!= on_failure_jump
)
5243 /* Still have to check that it's not an n-th
5244 alternative that starts with an on_failure_jump. */
5246 EXTRACT_NUMBER_AND_INCR (mcnt
, p1
);
5247 if ((re_opcode_t
) p1
[mcnt
-3] != jump_past_alt
)
5249 /* Get to the beginning of the n-th alternative. */
5255 /* Deal with the last alternative: go back and get number
5256 of the `jump_past_alt' just before it. `mcnt' contains
5257 the length of the alternative. */
5258 EXTRACT_NUMBER (mcnt
, p1
- 2);
5260 if (!alt_match_null_string_p (p1
, p1
+ mcnt
, reg_info
))
5263 p1
+= mcnt
; /* Get past the n-th alternative. */
5269 assert (p1
[1] == **p
);
5275 if (!common_op_match_null_string_p (&p1
, end
, reg_info
))
5278 } /* while p1 < end */
5281 } /* group_match_null_string_p */
5284 /* Similar to group_match_null_string_p, but doesn't deal with alternatives:
5285 It expects P to be the first byte of a single alternative and END one
5286 byte past the last. The alternative can contain groups. */
5289 alt_match_null_string_p (p
, end
, reg_info
)
5290 unsigned char *p
, *end
;
5291 register_info_type
*reg_info
;
5294 unsigned char *p1
= p
;
5298 /* Skip over opcodes that can match nothing, and break when we get
5299 to one that can't. */
5301 switch ((re_opcode_t
) *p1
)
5304 case on_failure_jump
:
5306 EXTRACT_NUMBER_AND_INCR (mcnt
, p1
);
5311 if (!common_op_match_null_string_p (&p1
, end
, reg_info
))
5314 } /* while p1 < end */
5317 } /* alt_match_null_string_p */
5320 /* Deals with the ops common to group_match_null_string_p and
5321 alt_match_null_string_p.
5323 Sets P to one after the op and its arguments, if any. */
5326 common_op_match_null_string_p (p
, end
, reg_info
)
5327 unsigned char **p
, *end
;
5328 register_info_type
*reg_info
;
5333 unsigned char *p1
= *p
;
5335 switch ((re_opcode_t
) *p1
++)
5355 assert (reg_no
> 0 && reg_no
<= MAX_REGNUM
);
5356 ret
= group_match_null_string_p (&p1
, end
, reg_info
);
5358 /* Have to set this here in case we're checking a group which
5359 contains a group and a back reference to it. */
5361 if (REG_MATCH_NULL_STRING_P (reg_info
[reg_no
]) == MATCH_NULL_UNSET_VALUE
)
5362 REG_MATCH_NULL_STRING_P (reg_info
[reg_no
]) = ret
;
5368 /* If this is an optimized succeed_n for zero times, make the jump. */
5370 EXTRACT_NUMBER_AND_INCR (mcnt
, p1
);
5378 /* Get to the number of times to succeed. */
5380 EXTRACT_NUMBER_AND_INCR (mcnt
, p1
);
5385 EXTRACT_NUMBER_AND_INCR (mcnt
, p1
);
5393 if (!REG_MATCH_NULL_STRING_P (reg_info
[*p1
]))
5401 /* All other opcodes mean we cannot match the empty string. */
5407 } /* common_op_match_null_string_p */
5410 /* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
5411 bytes; nonzero otherwise. */
5414 bcmp_translate (s1
, s2
, len
, translate
)
5415 const char *s1
, *s2
;
5417 RE_TRANSLATE_TYPE translate
;
5419 register const unsigned char *p1
= (const unsigned char *) s1
;
5420 register const unsigned char *p2
= (const unsigned char *) s2
;
5423 if (translate
[*p1
++] != translate
[*p2
++]) return 1;
5429 /* Entry points for GNU code. */
5431 /* re_compile_pattern is the GNU regular expression compiler: it
5432 compiles PATTERN (of length SIZE) and puts the result in BUFP.
5433 Returns 0 if the pattern was valid, otherwise an error string.
5435 Assumes the `allocated' (and perhaps `buffer') and `translate' fields
5436 are set in BUFP on entry.
5438 We call regex_compile to do the actual compilation. */
5441 re_compile_pattern (pattern
, length
, bufp
)
5442 const char *pattern
;
5444 struct re_pattern_buffer
*bufp
;
5448 /* GNU code is written to assume at least RE_NREGS registers will be set
5449 (and at least one extra will be -1). */
5450 bufp
->regs_allocated
= REGS_UNALLOCATED
;
5452 /* And GNU code determines whether or not to get register information
5453 by passing null for the REGS argument to re_match, etc., not by
5457 /* Match anchors at newline. */
5458 bufp
->newline_anchor
= 1;
5460 ret
= regex_compile (pattern
, length
, re_syntax_options
, bufp
);
5464 return gettext (re_error_msgid
[(int) ret
]);
5467 weak_alias (__re_compile_pattern
, re_compile_pattern
)
5470 /* Entry points compatible with 4.2 BSD regex library. We don't define
5471 them unless specifically requested. */
5473 #if defined _REGEX_RE_COMP || defined _LIBC
5475 /* BSD has one and only one pattern buffer. */
5476 static struct re_pattern_buffer re_comp_buf
;
5480 /* Make these definitions weak in libc, so POSIX programs can redefine
5481 these names if they don't use our functions, and still use
5482 regcomp/regexec below without link errors. */
5492 if (!re_comp_buf
.buffer
)
5493 return gettext ("No previous regular expression");
5497 if (!re_comp_buf
.buffer
)
5499 re_comp_buf
.buffer
= (unsigned char *) malloc (200);
5500 if (re_comp_buf
.buffer
== NULL
)
5501 return (char *) gettext (re_error_msgid
[(int) REG_ESPACE
]);
5502 re_comp_buf
.allocated
= 200;
5504 re_comp_buf
.fastmap
= (char *) malloc (1 << BYTEWIDTH
);
5505 if (re_comp_buf
.fastmap
== NULL
)
5506 return (char *) gettext (re_error_msgid
[(int) REG_ESPACE
]);
5509 /* Since `re_exec' always passes NULL for the `regs' argument, we
5510 don't need to initialize the pattern buffer fields which affect it. */
5512 /* Match anchors at newlines. */
5513 re_comp_buf
.newline_anchor
= 1;
5515 ret
= regex_compile (s
, strlen (s
), re_syntax_options
, &re_comp_buf
);
5520 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
5521 return (char *) gettext (re_error_msgid
[(int) ret
]);
5532 const int len
= strlen (s
);
5534 0 <= re_search (&re_comp_buf
, s
, len
, 0, len
, (struct re_registers
*) 0);
5537 #endif /* _REGEX_RE_COMP */
5539 /* POSIX.2 functions. Don't define these for Emacs. */
5543 /* regcomp takes a regular expression as a string and compiles it.
5545 PREG is a regex_t *. We do not expect any fields to be initialized,
5546 since POSIX says we shouldn't. Thus, we set
5548 `buffer' to the compiled pattern;
5549 `used' to the length of the compiled pattern;
5550 `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
5551 REG_EXTENDED bit in CFLAGS is set; otherwise, to
5552 RE_SYNTAX_POSIX_BASIC;
5553 `newline_anchor' to REG_NEWLINE being set in CFLAGS;
5554 `fastmap' and `fastmap_accurate' to zero;
5555 `re_nsub' to the number of subexpressions in PATTERN.
5557 PATTERN is the address of the pattern string.
5559 CFLAGS is a series of bits which affect compilation.
5561 If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
5562 use POSIX basic syntax.
5564 If REG_NEWLINE is set, then . and [^...] don't match newline.
5565 Also, regexec will try a match beginning after every newline.
5567 If REG_ICASE is set, then we considers upper- and lowercase
5568 versions of letters to be equivalent when matching.
5570 If REG_NOSUB is set, then when PREG is passed to regexec, that
5571 routine will report only success or failure, and nothing about the
5574 It returns 0 if it succeeds, nonzero if it doesn't. (See gnu-regex.h for
5575 the return codes and their meanings.) */
5578 regcomp (preg
, pattern
, cflags
)
5580 const char *pattern
;
5585 = (cflags
& REG_EXTENDED
) ?
5586 RE_SYNTAX_POSIX_EXTENDED
: RE_SYNTAX_POSIX_BASIC
;
5588 /* regex_compile will allocate the space for the compiled pattern. */
5590 preg
->allocated
= 0;
5593 /* Don't bother to use a fastmap when searching. This simplifies the
5594 REG_NEWLINE case: if we used a fastmap, we'd have to put all the
5595 characters after newlines into the fastmap. This way, we just try
5599 if (cflags
& REG_ICASE
)
5604 = (RE_TRANSLATE_TYPE
) malloc (CHAR_SET_SIZE
5605 * sizeof (*(RE_TRANSLATE_TYPE
)0));
5606 if (preg
->translate
== NULL
)
5607 return (int) REG_ESPACE
;
5609 /* Map uppercase characters to corresponding lowercase ones. */
5610 for (i
= 0; i
< CHAR_SET_SIZE
; i
++)
5611 preg
->translate
[i
] = ISUPPER (i
) ? tolower (i
) : i
;
5614 preg
->translate
= NULL
;
5616 /* If REG_NEWLINE is set, newlines are treated differently. */
5617 if (cflags
& REG_NEWLINE
)
5618 { /* REG_NEWLINE implies neither . nor [^...] match newline. */
5619 syntax
&= ~RE_DOT_NEWLINE
;
5620 syntax
|= RE_HAT_LISTS_NOT_NEWLINE
;
5621 /* It also changes the matching behavior. */
5622 preg
->newline_anchor
= 1;
5625 preg
->newline_anchor
= 0;
5627 preg
->no_sub
= !!(cflags
& REG_NOSUB
);
5629 /* POSIX says a null character in the pattern terminates it, so we
5630 can use strlen here in compiling the pattern. */
5631 ret
= regex_compile (pattern
, strlen (pattern
), syntax
, preg
);
5633 /* POSIX doesn't distinguish between an unmatched open-group and an
5634 unmatched close-group: both are REG_EPAREN. */
5635 if (ret
== REG_ERPAREN
) ret
= REG_EPAREN
;
5640 weak_alias (__regcomp
, regcomp
)
5644 /* regexec searches for a given pattern, specified by PREG, in the
5647 If NMATCH is zero or REG_NOSUB was set in the cflags argument to
5648 `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
5649 least NMATCH elements, and we set them to the offsets of the
5650 corresponding matched substrings.
5652 EFLAGS specifies `execution flags' which affect matching: if
5653 REG_NOTBOL is set, then ^ does not match at the beginning of the
5654 string; if REG_NOTEOL is set, then $ does not match at the end.
5656 We return 0 if we find a match and REG_NOMATCH if not. */
5659 regexec (preg
, string
, nmatch
, pmatch
, eflags
)
5660 const regex_t
*preg
;
5663 regmatch_t pmatch
[];
5667 struct re_registers regs
;
5668 regex_t private_preg
;
5669 int len
= strlen (string
);
5670 boolean want_reg_info
= !preg
->no_sub
&& nmatch
> 0;
5672 private_preg
= *preg
;
5674 private_preg
.not_bol
= !!(eflags
& REG_NOTBOL
);
5675 private_preg
.not_eol
= !!(eflags
& REG_NOTEOL
);
5677 /* The user has told us exactly how many registers to return
5678 information about, via `nmatch'. We have to pass that on to the
5679 matching routines. */
5680 private_preg
.regs_allocated
= REGS_FIXED
;
5684 regs
.num_regs
= nmatch
;
5685 regs
.start
= TALLOC (nmatch
, regoff_t
);
5686 regs
.end
= TALLOC (nmatch
, regoff_t
);
5687 if (regs
.start
== NULL
|| regs
.end
== NULL
)
5688 return (int) REG_NOMATCH
;
5691 /* Perform the searching operation. */
5692 ret
= re_search (&private_preg
, string
, len
,
5693 /* start: */ 0, /* range: */ len
,
5694 want_reg_info
? ®s
: (struct re_registers
*) 0);
5696 /* Copy the register information to the POSIX structure. */
5703 for (r
= 0; r
< nmatch
; r
++)
5705 pmatch
[r
].rm_so
= regs
.start
[r
];
5706 pmatch
[r
].rm_eo
= regs
.end
[r
];
5710 /* If we needed the temporary register info, free the space now. */
5715 /* We want zero return to mean success, unlike `re_search'. */
5716 return ret
>= 0 ? (int) REG_NOERROR
: (int) REG_NOMATCH
;
5719 weak_alias (__regexec
, regexec
)
5723 /* Returns a message corresponding to an error code, ERRCODE, returned
5724 from either regcomp or regexec. We don't use PREG here. */
5727 __regerror (errcode
, preg
, errbuf
, errbuf_size
)
5729 const regex_t
*preg
;
5737 || errcode
>= (int) (sizeof (re_error_msgid
)
5738 / sizeof (re_error_msgid
[0])))
5739 /* Only error codes returned by the rest of the code should be passed
5740 to this routine. If we are given anything else, or if other regex
5741 code generates an invalid error code, then the program has a bug.
5742 Dump core so we can fix it. */
5745 msg
= gettext (re_error_msgid
[errcode
]);
5747 msg_size
= strlen (msg
) + 1; /* Includes the null. */
5749 if (errbuf_size
!= 0)
5751 if (msg_size
> errbuf_size
)
5753 #if defined HAVE_MEMPCPY || defined _LIBC
5754 *((char *) __mempcpy (errbuf
, msg
, errbuf_size
- 1)) = '\0';
5756 memcpy (errbuf
, msg
, errbuf_size
- 1);
5757 errbuf
[errbuf_size
- 1] = 0;
5761 memcpy (errbuf
, msg
, msg_size
);
5767 weak_alias (__regerror
, regerror
)
5771 /* Free dynamically allocated space used by PREG. */
5777 if (preg
->buffer
!= NULL
)
5778 free (preg
->buffer
);
5779 preg
->buffer
= NULL
;
5781 preg
->allocated
= 0;
5784 if (preg
->fastmap
!= NULL
)
5785 free (preg
->fastmap
);
5786 preg
->fastmap
= NULL
;
5787 preg
->fastmap_accurate
= 0;
5789 if (preg
->translate
!= NULL
)
5790 free (preg
->translate
);
5791 preg
->translate
= NULL
;
5794 weak_alias (__regfree
, regfree
)
5797 #endif /* not emacs */