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