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