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