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