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