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