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