Merge branch 'vendor/BINUTILS224'
[dragonfly.git] / contrib / grep / src / dfa.c
1 /* dfa.c - deterministic extended regexp routines for GNU
2    Copyright (C) 1988, 1998, 2000, 2002, 2004-2005, 2007-2012 Free Software
3    Foundation, Inc.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3, or (at your option)
8    any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc.,
18    51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA */
19
20 /* Written June, 1988 by Mike Haertel
21    Modified July, 1988 by Arthur David Olson to assist BMG speedups  */
22
23 #include <config.h>
24 #include <assert.h>
25 #include <ctype.h>
26 #include <stdio.h>
27 #include <sys/types.h>
28 #include <stdlib.h>
29 #include <limits.h>
30 #include <string.h>
31 #include <locale.h>
32 #include <stdbool.h>
33
34 #define STREQ(a, b) (strcmp (a, b) == 0)
35
36 /* ISASCIIDIGIT differs from isdigit, as follows:
37    - Its arg may be any int or unsigned int; it need not be an unsigned char.
38    - It's guaranteed to evaluate its argument exactly once.
39    - It's typically faster.
40    Posix 1003.2-1992 section 2.5.2.1 page 50 lines 1556-1558 says that
41    only '0' through '9' are digits.  Prefer ISASCIIDIGIT to isdigit unless
42    it's important to use the locale's definition of "digit" even when the
43    host does not conform to Posix.  */
44 #define ISASCIIDIGIT(c) ((unsigned) (c) - '0' <= 9)
45
46 /* gettext.h ensures that we don't use gettext if ENABLE_NLS is not defined */
47 #include "gettext.h"
48 #define _(str) gettext (str)
49
50 #include "mbsupport.h"          /* defines MBS_SUPPORT if appropriate */
51 #include <wchar.h>
52 #include <wctype.h>
53
54 #if HAVE_LANGINFO_CODESET
55 # include <langinfo.h>
56 #endif
57
58 #include "regex.h"
59 #include "dfa.h"
60 #include "xalloc.h"
61
62 /* HPUX, define those as macros in sys/param.h */
63 #ifdef setbit
64 # undef setbit
65 #endif
66 #ifdef clrbit
67 # undef clrbit
68 #endif
69
70 /* Number of bits in an unsigned char. */
71 #ifndef CHARBITS
72 # define CHARBITS 8
73 #endif
74
75 /* First integer value that is greater than any character code. */
76 #define NOTCHAR (1 << CHARBITS)
77
78 /* INTBITS need not be exact, just a lower bound. */
79 #ifndef INTBITS
80 # define INTBITS (CHARBITS * sizeof (int))
81 #endif
82
83 /* Number of ints required to hold a bit for every character. */
84 #define CHARCLASS_INTS ((NOTCHAR + INTBITS - 1) / INTBITS)
85
86 /* Sets of unsigned characters are stored as bit vectors in arrays of ints. */
87 typedef int charclass[CHARCLASS_INTS];
88
89 /* Convert a possibly-signed character to an unsigned character.  This is
90    a bit safer than casting to unsigned char, since it catches some type
91    errors that the cast doesn't.  */
92 static inline unsigned char
93 to_uchar (char ch)
94 {
95   return ch;
96 }
97
98 /* Contexts tell us whether a character is a newline or a word constituent.
99    Word-constituent characters are those that satisfy iswalnum(), plus '_'.
100    Each character has a single CTX_* value; bitmasks of CTX_* values denote
101    a particular character class.
102
103    A state also stores a context value, which is a bitmask of CTX_* values.
104    A state's context represents a set of characters that the state's
105    predecessors must match.  For example, a state whose context does not
106    include CTX_LETTER will never have transitions where the previous
107    character is a word constituent.  A state whose context is CTX_ANY
108    might have transitions from any character.  */
109
110 #define CTX_NONE        1
111 #define CTX_LETTER      2
112 #define CTX_NEWLINE     4
113 #define CTX_ANY         7
114
115 /* Sometimes characters can only be matched depending on the surrounding
116    context.  Such context decisions depend on what the previous character
117    was, and the value of the current (lookahead) character.  Context
118    dependent constraints are encoded as 8 bit integers.  Each bit that
119    is set indicates that the constraint succeeds in the corresponding
120    context.
121
122    bit 8-11 - valid contexts when next character is CTX_NEWLINE
123    bit 4-7  - valid contexts when next character is CTX_LETTER
124    bit 0-3  - valid contexts when next character is CTX_NONE
125
126    The macro SUCCEEDS_IN_CONTEXT determines whether a given constraint
127    succeeds in a particular context.  Prev is a bitmask of possible
128    context values for the previous character, curr is the (single-bit)
129    context value for the lookahead character. */
130 #define NEWLINE_CONSTRAINT(constraint) (((constraint) >> 8) & 0xf)
131 #define LETTER_CONSTRAINT(constraint)  (((constraint) >> 4) & 0xf)
132 #define OTHER_CONSTRAINT(constraint)    ((constraint)       & 0xf)
133
134 #define SUCCEEDS_IN_CONTEXT(constraint, prev, curr) \
135   ((((curr) & CTX_NONE      ? OTHER_CONSTRAINT(constraint) : 0) \
136     | ((curr) & CTX_LETTER  ? LETTER_CONSTRAINT(constraint) : 0) \
137     | ((curr) & CTX_NEWLINE ? NEWLINE_CONSTRAINT(constraint) : 0)) & (prev))
138
139 /* The following macros give information about what a constraint depends on. */
140 #define PREV_NEWLINE_CONSTRAINT(constraint) (((constraint) >> 2) & 0x111)
141 #define PREV_LETTER_CONSTRAINT(constraint)  (((constraint) >> 1) & 0x111)
142 #define PREV_OTHER_CONSTRAINT(constraint)    ((constraint)       & 0x111)
143
144 #define PREV_NEWLINE_DEPENDENT(constraint) \
145   (PREV_NEWLINE_CONSTRAINT (constraint) != PREV_OTHER_CONSTRAINT (constraint))
146 #define PREV_LETTER_DEPENDENT(constraint) \
147   (PREV_LETTER_CONSTRAINT (constraint) != PREV_OTHER_CONSTRAINT (constraint))
148
149 /* Tokens that match the empty string subject to some constraint actually
150    work by applying that constraint to determine what may follow them,
151    taking into account what has gone before.  The following values are
152    the constraints corresponding to the special tokens previously defined. */
153 #define NO_CONSTRAINT         0x777
154 #define BEGLINE_CONSTRAINT    0x444
155 #define ENDLINE_CONSTRAINT    0x700
156 #define BEGWORD_CONSTRAINT    0x050
157 #define ENDWORD_CONSTRAINT    0x202
158 #define LIMWORD_CONSTRAINT    0x252
159 #define NOTLIMWORD_CONSTRAINT 0x525
160
161 /* The regexp is parsed into an array of tokens in postfix form.  Some tokens
162    are operators and others are terminal symbols.  Most (but not all) of these
163    codes are returned by the lexical analyzer. */
164
165 typedef ptrdiff_t token;
166
167 /* Predefined token values.  */
168 enum
169 {
170   END = -1,                     /* END is a terminal symbol that matches the
171                                    end of input; any value of END or less in
172                                    the parse tree is such a symbol.  Accepting
173                                    states of the DFA are those that would have
174                                    a transition on END. */
175
176   /* Ordinary character values are terminal symbols that match themselves. */
177
178   EMPTY = NOTCHAR,              /* EMPTY is a terminal symbol that matches
179                                    the empty string. */
180
181   BACKREF,                      /* BACKREF is generated by \<digit>; it
182                                    is not completely handled.  If the scanner
183                                    detects a transition on backref, it returns
184                                    a kind of "semi-success" indicating that
185                                    the match will have to be verified with
186                                    a backtracking matcher. */
187
188   BEGLINE,                      /* BEGLINE is a terminal symbol that matches
189                                    the empty string if it is at the beginning
190                                    of a line. */
191
192   ENDLINE,                      /* ENDLINE is a terminal symbol that matches
193                                    the empty string if it is at the end of
194                                    a line. */
195
196   BEGWORD,                      /* BEGWORD is a terminal symbol that matches
197                                    the empty string if it is at the beginning
198                                    of a word. */
199
200   ENDWORD,                      /* ENDWORD is a terminal symbol that matches
201                                    the empty string if it is at the end of
202                                    a word. */
203
204   LIMWORD,                      /* LIMWORD is a terminal symbol that matches
205                                    the empty string if it is at the beginning
206                                    or the end of a word. */
207
208   NOTLIMWORD,                   /* NOTLIMWORD is a terminal symbol that
209                                    matches the empty string if it is not at
210                                    the beginning or end of a word. */
211
212   QMARK,                        /* QMARK is an operator of one argument that
213                                    matches zero or one occurrences of its
214                                    argument. */
215
216   STAR,                         /* STAR is an operator of one argument that
217                                    matches the Kleene closure (zero or more
218                                    occurrences) of its argument. */
219
220   PLUS,                         /* PLUS is an operator of one argument that
221                                    matches the positive closure (one or more
222                                    occurrences) of its argument. */
223
224   REPMN,                        /* REPMN is a lexical token corresponding
225                                    to the {m,n} construct.  REPMN never
226                                    appears in the compiled token vector. */
227
228   CAT,                          /* CAT is an operator of two arguments that
229                                    matches the concatenation of its
230                                    arguments.  CAT is never returned by the
231                                    lexical analyzer. */
232
233   OR,                           /* OR is an operator of two arguments that
234                                    matches either of its arguments. */
235
236   LPAREN,                       /* LPAREN never appears in the parse tree,
237                                    it is only a lexeme. */
238
239   RPAREN,                       /* RPAREN never appears in the parse tree. */
240
241   ANYCHAR,                      /* ANYCHAR is a terminal symbol that matches
242                                    any multibyte (or single byte) characters.
243                                    It is used only if MB_CUR_MAX > 1.  */
244
245   MBCSET,                       /* MBCSET is similar to CSET, but for
246                                    multibyte characters.  */
247
248   WCHAR,                        /* Only returned by lex.  wctok contains
249                                    the wide character representation.  */
250
251   CSET                          /* CSET and (and any value greater) is a
252                                    terminal symbol that matches any of a
253                                    class of characters. */
254 };
255
256
257 /* States of the recognizer correspond to sets of positions in the parse
258    tree, together with the constraints under which they may be matched.
259    So a position is encoded as an index into the parse tree together with
260    a constraint. */
261 typedef struct
262 {
263   size_t index;                 /* Index into the parse array. */
264   unsigned int constraint;      /* Constraint for matching this position. */
265 } position;
266
267 /* Sets of positions are stored as arrays. */
268 typedef struct
269 {
270   position *elems;              /* Elements of this position set. */
271   size_t nelem;                 /* Number of elements in this set. */
272   size_t alloc;                 /* Number of elements allocated in ELEMS.  */
273 } position_set;
274
275 /* Sets of leaves are also stored as arrays. */
276 typedef struct
277 {
278   size_t *elems;                /* Elements of this position set. */
279   size_t nelem;                 /* Number of elements in this set. */
280 } leaf_set;
281
282 /* A state of the dfa consists of a set of positions, some flags,
283    and the token value of the lowest-numbered position of the state that
284    contains an END token. */
285 typedef struct
286 {
287   size_t hash;                  /* Hash of the positions of this state. */
288   position_set elems;           /* Positions this state could match. */
289   unsigned char context;        /* Context from previous state. */
290   char backref;                 /* True if this state matches a \<digit>.  */
291   unsigned short constraint;    /* Constraint for this state to accept. */
292   token first_end;              /* Token value of the first END in elems. */
293   position_set mbps;            /* Positions which can match multibyte
294                                    characters.  e.g. period.
295                                    These staff are used only if
296                                    MB_CUR_MAX > 1.  */
297 } dfa_state;
298
299 /* States are indexed by state_num values.  These are normally
300    nonnegative but -1 is used as a special value.  */
301 typedef ptrdiff_t state_num;
302
303 /* A bracket operator.
304    e.g. [a-c], [[:alpha:]], etc.  */
305 struct mb_char_classes
306 {
307   ptrdiff_t cset;
308   int invert;
309   wchar_t *chars;               /* Normal characters.  */
310   size_t nchars;
311   wctype_t *ch_classes;         /* Character classes.  */
312   size_t nch_classes;
313   wchar_t *range_sts;           /* Range characters (start of the range).  */
314   wchar_t *range_ends;          /* Range characters (end of the range).  */
315   size_t nranges;
316   char **equivs;                /* Equivalence classes.  */
317   size_t nequivs;
318   char **coll_elems;
319   size_t ncoll_elems;           /* Collating elements.  */
320 };
321
322 /* A compiled regular expression. */
323 struct dfa
324 {
325   /* Fields filled by the scanner. */
326   charclass *charclasses;       /* Array of character sets for CSET tokens. */
327   size_t cindex;                /* Index for adding new charclasses. */
328   size_t calloc;                /* Number of charclasses currently allocated. */
329
330   /* Fields filled by the parser. */
331   token *tokens;                /* Postfix parse array. */
332   size_t tindex;                /* Index for adding new tokens. */
333   size_t talloc;                /* Number of tokens currently allocated. */
334   size_t depth;                 /* Depth required of an evaluation stack
335                                    used for depth-first traversal of the
336                                    parse tree. */
337   size_t nleaves;               /* Number of leaves on the parse tree. */
338   size_t nregexps;              /* Count of parallel regexps being built
339                                    with dfaparse(). */
340   unsigned int mb_cur_max;      /* Cached value of MB_CUR_MAX.  */
341   token utf8_anychar_classes[5];        /* To lower ANYCHAR in UTF-8 locales.  */
342
343   /* The following are used only if MB_CUR_MAX > 1.  */
344
345   /* The value of multibyte_prop[i] is defined by following rule.
346      if tokens[i] < NOTCHAR
347      bit 0 : tokens[i] is the first byte of a character, including
348      single-byte characters.
349      bit 1 : tokens[i] is the last byte of a character, including
350      single-byte characters.
351
352      if tokens[i] = MBCSET
353      ("the index of mbcsets corresponding to this operator" << 2) + 3
354
355      e.g.
356      tokens
357      = 'single_byte_a', 'multi_byte_A', single_byte_b'
358      = 'sb_a', 'mb_A(1st byte)', 'mb_A(2nd byte)', 'mb_A(3rd byte)', 'sb_b'
359      multibyte_prop
360      = 3     , 1               ,  0              ,  2              , 3
361    */
362   size_t nmultibyte_prop;
363   int *multibyte_prop;
364
365   /* Array of the bracket expression in the DFA.  */
366   struct mb_char_classes *mbcsets;
367   size_t nmbcsets;
368   size_t mbcsets_alloc;
369
370   /* Fields filled by the state builder. */
371   dfa_state *states;            /* States of the dfa. */
372   state_num sindex;             /* Index for adding new states. */
373   state_num salloc;             /* Number of states currently allocated. */
374
375   /* Fields filled by the parse tree->NFA conversion. */
376   position_set *follows;        /* Array of follow sets, indexed by position
377                                    index.  The follow of a position is the set
378                                    of positions containing characters that
379                                    could conceivably follow a character
380                                    matching the given position in a string
381                                    matching the regexp.  Allocated to the
382                                    maximum possible position index. */
383   int searchflag;               /* True if we are supposed to build a searching
384                                    as opposed to an exact matcher.  A searching
385                                    matcher finds the first and shortest string
386                                    matching a regexp anywhere in the buffer,
387                                    whereas an exact matcher finds the longest
388                                    string matching, but anchored to the
389                                    beginning of the buffer. */
390
391   /* Fields filled by dfaexec. */
392   state_num tralloc;            /* Number of transition tables that have
393                                    slots so far. */
394   int trcount;                  /* Number of transition tables that have
395                                    actually been built. */
396   state_num **trans;            /* Transition tables for states that can
397                                    never accept.  If the transitions for a
398                                    state have not yet been computed, or the
399                                    state could possibly accept, its entry in
400                                    this table is NULL. */
401   state_num **realtrans;        /* Trans always points to realtrans + 1; this
402                                    is so trans[-1] can contain NULL. */
403   state_num **fails;            /* Transition tables after failing to accept
404                                    on a state that potentially could do so. */
405   int *success;                 /* Table of acceptance conditions used in
406                                    dfaexec and computed in build_state. */
407   state_num *newlines;          /* Transitions on newlines.  The entry for a
408                                    newline in any transition table is always
409                                    -1 so we can count lines without wasting
410                                    too many cycles.  The transition for a
411                                    newline is stored separately and handled
412                                    as a special case.  Newline is also used
413                                    as a sentinel at the end of the buffer. */
414   struct dfamust *musts;        /* List of strings, at least one of which
415                                    is known to appear in any r.e. matching
416                                    the dfa. */
417 };
418
419 /* Some macros for user access to dfa internals. */
420
421 /* ACCEPTING returns true if s could possibly be an accepting state of r. */
422 #define ACCEPTING(s, r) ((r).states[s].constraint)
423
424 /* ACCEPTS_IN_CONTEXT returns true if the given state accepts in the
425    specified context. */
426 #define ACCEPTS_IN_CONTEXT(prev, curr, state, dfa) \
427   SUCCEEDS_IN_CONTEXT ((dfa).states[state].constraint, prev, curr)
428
429 static void dfamust (struct dfa *dfa);
430 static void regexp (void);
431
432 /* These two macros are identical to the ones in gnulib's xalloc.h,
433    except that they not to case the result to "(t *)", and thus may
434    be used via type-free CALLOC and MALLOC macros.  */
435 #undef XNMALLOC
436 #undef XCALLOC
437
438 /* Allocate memory for N elements of type T, with error checking.  */
439 /* extern t *XNMALLOC (size_t n, typename t); */
440 # define XNMALLOC(n, t) \
441     (sizeof (t) == 1 ? xmalloc (n) : xnmalloc (n, sizeof (t)))
442
443 /* Allocate memory for N elements of type T, with error checking,
444    and zero it.  */
445 /* extern t *XCALLOC (size_t n, typename t); */
446 # define XCALLOC(n, t) \
447     (sizeof (t) == 1 ? xzalloc (n) : xcalloc (n, sizeof (t)))
448
449 #define CALLOC(p, n) do { (p) = XCALLOC (n, *(p)); } while (0)
450 #define MALLOC(p, n) do { (p) = XNMALLOC (n, *(p)); } while (0)
451 #define REALLOC(p, n) do {(p) = xnrealloc (p, n, sizeof (*(p))); } while (0)
452
453 /* Reallocate an array of type *P if N_ALLOC is <= N_REQUIRED. */
454 #define REALLOC_IF_NECESSARY(p, n_alloc, n_required)            \
455   do                                                            \
456     {                                                           \
457       if ((n_alloc) <= (n_required))                            \
458         {                                                       \
459           size_t new_n_alloc = (n_required) + !(p);             \
460           (p) = x2nrealloc (p, &new_n_alloc, sizeof (*(p)));    \
461           (n_alloc) = new_n_alloc;                              \
462         }                                                       \
463     }                                                           \
464   while (false)
465
466
467 #ifdef DEBUG
468
469 static void
470 prtok (token t)
471 {
472   char const *s;
473
474   if (t < 0)
475     fprintf (stderr, "END");
476   else if (t < NOTCHAR)
477     {
478       int ch = t;
479       fprintf (stderr, "%c", ch);
480     }
481   else
482     {
483       switch (t)
484         {
485         case EMPTY:
486           s = "EMPTY";
487           break;
488         case BACKREF:
489           s = "BACKREF";
490           break;
491         case BEGLINE:
492           s = "BEGLINE";
493           break;
494         case ENDLINE:
495           s = "ENDLINE";
496           break;
497         case BEGWORD:
498           s = "BEGWORD";
499           break;
500         case ENDWORD:
501           s = "ENDWORD";
502           break;
503         case LIMWORD:
504           s = "LIMWORD";
505           break;
506         case NOTLIMWORD:
507           s = "NOTLIMWORD";
508           break;
509         case QMARK:
510           s = "QMARK";
511           break;
512         case STAR:
513           s = "STAR";
514           break;
515         case PLUS:
516           s = "PLUS";
517           break;
518         case CAT:
519           s = "CAT";
520           break;
521         case OR:
522           s = "OR";
523           break;
524         case LPAREN:
525           s = "LPAREN";
526           break;
527         case RPAREN:
528           s = "RPAREN";
529           break;
530         case ANYCHAR:
531           s = "ANYCHAR";
532           break;
533         case MBCSET:
534           s = "MBCSET";
535           break;
536         default:
537           s = "CSET";
538           break;
539         }
540       fprintf (stderr, "%s", s);
541     }
542 }
543 #endif /* DEBUG */
544
545 /* Stuff pertaining to charclasses. */
546
547 static int
548 tstbit (unsigned int b, charclass const c)
549 {
550   return c[b / INTBITS] & 1 << b % INTBITS;
551 }
552
553 static void
554 setbit (unsigned int b, charclass c)
555 {
556   c[b / INTBITS] |= 1 << b % INTBITS;
557 }
558
559 static void
560 clrbit (unsigned int b, charclass c)
561 {
562   c[b / INTBITS] &= ~(1 << b % INTBITS);
563 }
564
565 static void
566 copyset (charclass const src, charclass dst)
567 {
568   memcpy (dst, src, sizeof (charclass));
569 }
570
571 static void
572 zeroset (charclass s)
573 {
574   memset (s, 0, sizeof (charclass));
575 }
576
577 static void
578 notset (charclass s)
579 {
580   int i;
581
582   for (i = 0; i < CHARCLASS_INTS; ++i)
583     s[i] = ~s[i];
584 }
585
586 static int
587 equal (charclass const s1, charclass const s2)
588 {
589   return memcmp (s1, s2, sizeof (charclass)) == 0;
590 }
591
592 /* A pointer to the current dfa is kept here during parsing. */
593 static struct dfa *dfa;
594
595 /* Find the index of charclass s in dfa->charclasses, or allocate a new charclass. */
596 static size_t
597 charclass_index (charclass const s)
598 {
599   size_t i;
600
601   for (i = 0; i < dfa->cindex; ++i)
602     if (equal (s, dfa->charclasses[i]))
603       return i;
604   REALLOC_IF_NECESSARY (dfa->charclasses, dfa->calloc, dfa->cindex + 1);
605   ++dfa->cindex;
606   copyset (s, dfa->charclasses[i]);
607   return i;
608 }
609
610 /* Syntax bits controlling the behavior of the lexical analyzer. */
611 static reg_syntax_t syntax_bits, syntax_bits_set;
612
613 /* Flag for case-folding letters into sets. */
614 static int case_fold;
615
616 /* End-of-line byte in data.  */
617 static unsigned char eolbyte;
618
619 /* Cache of char-context values.  */
620 static int sbit[NOTCHAR];
621
622 /* Set of characters considered letters. */
623 static charclass letters;
624
625 /* Set of characters that are newline. */
626 static charclass newline;
627
628 /* Add this to the test for whether a byte is word-constituent, since on
629    BSD-based systems, many values in the 128..255 range are classified as
630    alphabetic, while on glibc-based systems, they are not.  */
631 #ifdef __GLIBC__
632 # define is_valid_unibyte_character(c) 1
633 #else
634 # define is_valid_unibyte_character(c) (! (MBS_SUPPORT && btowc (c) == WEOF))
635 #endif
636
637 /* Return non-zero if C is a "word-constituent" byte; zero otherwise.  */
638 #define IS_WORD_CONSTITUENT(C) \
639   (is_valid_unibyte_character (C) && (isalnum (C) || (C) == '_'))
640
641 static int
642 char_context (unsigned char c)
643 {
644   if (c == eolbyte || c == 0)
645     return CTX_NEWLINE;
646   if (IS_WORD_CONSTITUENT (c))
647     return CTX_LETTER;
648   return CTX_NONE;
649 }
650
651 static int
652 wchar_context (wint_t wc)
653 {
654   if (wc == (wchar_t) eolbyte || wc == 0)
655     return CTX_NEWLINE;
656   if (wc == L'_' || iswalnum (wc))
657     return CTX_LETTER;
658   return CTX_NONE;
659 }
660
661 /* Entry point to set syntax options. */
662 void
663 dfasyntax (reg_syntax_t bits, int fold, unsigned char eol)
664 {
665   unsigned int i;
666
667   syntax_bits_set = 1;
668   syntax_bits = bits;
669   case_fold = fold;
670   eolbyte = eol;
671
672   for (i = 0; i < NOTCHAR; ++i)
673     {
674       sbit[i] = char_context (i);
675       switch (sbit[i])
676         {
677         case CTX_LETTER:
678           setbit (i, letters);
679           break;
680         case CTX_NEWLINE:
681           setbit (i, newline);
682           break;
683         }
684     }
685 }
686
687 /* Set a bit in the charclass for the given wchar_t.  Do nothing if WC
688    is represented by a multi-byte sequence.  Even for MB_CUR_MAX == 1,
689    this may happen when folding case in weird Turkish locales where
690    dotless i/dotted I are not included in the chosen character set.
691    Return whether a bit was set in the charclass.  */
692 #if MBS_SUPPORT
693 static bool
694 setbit_wc (wint_t wc, charclass c)
695 {
696   int b = wctob (wc);
697   if (b == EOF)
698     return false;
699
700   setbit (b, c);
701   return true;
702 }
703
704 /* Set a bit in the charclass for the given single byte character,
705    if it is valid in the current character set.  */
706 static void
707 setbit_c (int b, charclass c)
708 {
709   /* Do nothing if b is invalid in this character set.  */
710   if (MB_CUR_MAX > 1 && btowc (b) == WEOF)
711     return;
712   setbit (b, c);
713 }
714 #else
715 # define setbit_c setbit
716 static inline bool
717 setbit_wc (wint_t wc, charclass c)
718 {
719   abort ();
720    /*NOTREACHED*/ return false;
721 }
722 #endif
723
724 /* Like setbit_c, but if case is folded, set both cases of a letter.  For
725    MB_CUR_MAX > 1, the resulting charset is only used as an optimization,
726    and the caller takes care of setting the appropriate field of struct
727    mb_char_classes.  */
728 static void
729 setbit_case_fold_c (int b, charclass c)
730 {
731   if (MB_CUR_MAX > 1)
732     {
733       wint_t wc = btowc (b);
734       if (wc == WEOF)
735         return;
736       setbit (b, c);
737       if (case_fold && iswalpha (wc))
738         setbit_wc (iswupper (wc) ? towlower (wc) : towupper (wc), c);
739     }
740   else
741     {
742       setbit (b, c);
743       if (case_fold && isalpha (b))
744         setbit_c (isupper (b) ? tolower (b) : toupper (b), c);
745     }
746 }
747
748
749
750 /* UTF-8 encoding allows some optimizations that we can't otherwise
751    assume in a multibyte encoding. */
752 static inline int
753 using_utf8 (void)
754 {
755   static int utf8 = -1;
756   if (utf8 == -1)
757     {
758 #if defined HAVE_LANGINFO_CODESET && MBS_SUPPORT
759       utf8 = (STREQ (nl_langinfo (CODESET), "UTF-8"));
760 #else
761       utf8 = 0;
762 #endif
763     }
764
765   return utf8;
766 }
767
768 /* Lexical analyzer.  All the dross that deals with the obnoxious
769    GNU Regex syntax bits is located here.  The poor, suffering
770    reader is referred to the GNU Regex documentation for the
771    meaning of the @#%!@#%^!@ syntax bits. */
772
773 static char const *lexptr;      /* Pointer to next input character. */
774 static size_t lexleft;          /* Number of characters remaining. */
775 static token lasttok;           /* Previous token returned; initially END. */
776 static int laststart;           /* True if we're separated from beginning or (, |
777                                    only by zero-width characters. */
778 static size_t parens;           /* Count of outstanding left parens. */
779 static int minrep, maxrep;      /* Repeat counts for {m,n}. */
780
781 static int cur_mb_len = 1;      /* Length of the multibyte representation of
782                                    wctok.  */
783 /* These variables are used only if (MB_CUR_MAX > 1).  */
784 static mbstate_t mbs;           /* Mbstate for mbrlen().  */
785 static wchar_t wctok;           /* Wide character representation of the current
786                                    multibyte character.  */
787 static unsigned char *mblen_buf;        /* Correspond to the input buffer in dfaexec().
788                                            Each element store the amount of remain
789                                            byte of corresponding multibyte character
790                                            in the input string.  A element's value
791                                            is 0 if corresponding character is a
792                                            single byte character.
793                                            e.g. input : 'a', <mb(0)>, <mb(1)>, <mb(2)>
794                                            mblen_buf :   0,       3,       2,       1
795                                          */
796 static wchar_t *inputwcs;       /* Wide character representation of input
797                                    string in dfaexec().
798                                    The length of this array is same as
799                                    the length of input string(char array).
800                                    inputstring[i] is a single-byte char,
801                                    or 1st byte of a multibyte char.
802                                    And inputwcs[i] is the codepoint.  */
803 static unsigned char const *buf_begin;  /* reference to begin in dfaexec().  */
804 static unsigned char const *buf_end;    /* reference to end in dfaexec().  */
805
806
807 #if MBS_SUPPORT
808 /* Note that characters become unsigned here. */
809 # define FETCH_WC(c, wc, eoferr)                \
810   do {                                          \
811     if (! lexleft)                              \
812       {                                         \
813         if ((eoferr) != 0)                      \
814           dfaerror (eoferr);                    \
815         else                                    \
816           return lasttok = END;                 \
817       }                                         \
818     else                                        \
819       {                                         \
820         wchar_t _wc;                            \
821         cur_mb_len = mbrtowc (&_wc, lexptr, lexleft, &mbs); \
822         if (cur_mb_len <= 0)                    \
823           {                                     \
824             cur_mb_len = 1;                     \
825             --lexleft;                          \
826             (wc) = (c) = to_uchar (*lexptr++);  \
827           }                                     \
828         else                                    \
829           {                                     \
830             lexptr += cur_mb_len;               \
831             lexleft -= cur_mb_len;              \
832             (wc) = _wc;                         \
833             (c) = wctob (wc);                   \
834           }                                     \
835       }                                         \
836   } while(0)
837
838 # define FETCH(c, eoferr)                       \
839   do {                                          \
840     wint_t wc;                                  \
841     FETCH_WC (c, wc, eoferr);                   \
842   } while (0)
843
844 #else
845 /* Note that characters become unsigned here. */
846 # define FETCH(c, eoferr)             \
847   do {                                \
848     if (! lexleft)                    \
849       {                               \
850         if ((eoferr) != 0)            \
851           dfaerror (eoferr);          \
852         else                          \
853           return lasttok = END;       \
854       }                               \
855     (c) = to_uchar (*lexptr++);       \
856     --lexleft;                        \
857   } while(0)
858
859 # define FETCH_WC(c, unused, eoferr) FETCH (c, eoferr)
860
861 #endif /* MBS_SUPPORT */
862
863 #ifndef MIN
864 # define MIN(a,b) ((a) < (b) ? (a) : (b))
865 #endif
866
867 typedef int predicate (int);
868
869 /* The following list maps the names of the Posix named character classes
870    to predicate functions that determine whether a given character is in
871    the class.  The leading [ has already been eaten by the lexical analyzer. */
872 struct dfa_ctype
873 {
874   const char *name;
875   predicate *func;
876   bool single_byte_only;
877 };
878
879 static const struct dfa_ctype prednames[] = {
880   {"alpha", isalpha, false},
881   {"upper", isupper, false},
882   {"lower", islower, false},
883   {"digit", isdigit, true},
884   {"xdigit", isxdigit, true},
885   {"space", isspace, false},
886   {"punct", ispunct, false},
887   {"alnum", isalnum, false},
888   {"print", isprint, false},
889   {"graph", isgraph, false},
890   {"cntrl", iscntrl, false},
891   {"blank", isblank, false},
892   {NULL, NULL, false}
893 };
894
895 static const struct dfa_ctype *_GL_ATTRIBUTE_PURE
896 find_pred (const char *str)
897 {
898   unsigned int i;
899   for (i = 0; prednames[i].name; ++i)
900     if (STREQ (str, prednames[i].name))
901       break;
902
903   return &prednames[i];
904 }
905
906 /* Multibyte character handling sub-routine for lex.
907    This function  parse a bracket expression and build a struct
908    mb_char_classes.  */
909 static token
910 parse_bracket_exp (void)
911 {
912   int invert;
913   int c, c1, c2;
914   charclass ccl;
915
916   /* Used to warn about [:space:].
917      Bit 0 = first character is a colon.
918      Bit 1 = last character is a colon.
919      Bit 2 = includes any other character but a colon.
920      Bit 3 = includes ranges, char/equiv classes or collation elements.  */
921   int colon_warning_state;
922
923   wint_t wc;
924   wint_t wc2;
925   wint_t wc1 = 0;
926
927   /* Work area to build a mb_char_classes.  */
928   struct mb_char_classes *work_mbc;
929   size_t chars_al, range_sts_al, range_ends_al, ch_classes_al,
930     equivs_al, coll_elems_al;
931
932   chars_al = 0;
933   range_sts_al = range_ends_al = 0;
934   ch_classes_al = equivs_al = coll_elems_al = 0;
935   if (MB_CUR_MAX > 1)
936     {
937       REALLOC_IF_NECESSARY (dfa->mbcsets, dfa->mbcsets_alloc,
938                             dfa->nmbcsets + 1);
939
940       /* dfa->multibyte_prop[] hold the index of dfa->mbcsets.
941          We will update dfa->multibyte_prop[] in addtok(), because we can't
942          decide the index in dfa->tokens[].  */
943
944       /* Initialize work area.  */
945       work_mbc = &(dfa->mbcsets[dfa->nmbcsets++]);
946       memset (work_mbc, 0, sizeof *work_mbc);
947     }
948   else
949     work_mbc = NULL;
950
951   memset (ccl, 0, sizeof ccl);
952   FETCH_WC (c, wc, _("unbalanced ["));
953   if (c == '^')
954     {
955       FETCH_WC (c, wc, _("unbalanced ["));
956       invert = 1;
957     }
958   else
959     invert = 0;
960
961   colon_warning_state = (c == ':');
962   do
963     {
964       c1 = EOF;                 /* mark c1 is not initialized".  */
965       colon_warning_state &= ~2;
966
967       /* Note that if we're looking at some other [:...:] construct,
968          we just treat it as a bunch of ordinary characters.  We can do
969          this because we assume regex has checked for syntax errors before
970          dfa is ever called. */
971       if (c == '[' && (syntax_bits & RE_CHAR_CLASSES))
972         {
973 #define BRACKET_BUFFER_SIZE 128
974           char str[BRACKET_BUFFER_SIZE];
975           FETCH_WC (c1, wc1, _("unbalanced ["));
976
977           /* If pattern contains '[[:', '[[.', or '[[='.  */
978           if (c1 == ':'
979               /* TODO: handle '[[.' and '[[=' also for MB_CUR_MAX == 1.  */
980               || (MB_CUR_MAX > 1 && (c1 == '.' || c1 == '=')))
981             {
982               size_t len = 0;
983               for (;;)
984                 {
985                   FETCH_WC (c, wc, _("unbalanced ["));
986                   if ((c == c1 && *lexptr == ']') || lexleft == 0)
987                     break;
988                   if (len < BRACKET_BUFFER_SIZE)
989                     str[len++] = c;
990                   else
991                     /* This is in any case an invalid class name.  */
992                     str[0] = '\0';
993                 }
994               str[len] = '\0';
995
996               /* Fetch bracket.  */
997               FETCH_WC (c, wc, _("unbalanced ["));
998               if (c1 == ':')
999                 /* build character class.  */
1000                 {
1001                   char const *class
1002                     = (case_fold && (STREQ (str, "upper")
1003                                      || STREQ (str, "lower")) ? "alpha" : str);
1004                   const struct dfa_ctype *pred = find_pred (class);
1005                   if (!pred)
1006                     dfaerror (_("invalid character class"));
1007
1008                   if (MB_CUR_MAX > 1 && !pred->single_byte_only)
1009                     {
1010                       /* Store the character class as wctype_t.  */
1011                       wctype_t wt = wctype (class);
1012
1013                       REALLOC_IF_NECESSARY (work_mbc->ch_classes,
1014                                             ch_classes_al,
1015                                             work_mbc->nch_classes + 1);
1016                       work_mbc->ch_classes[work_mbc->nch_classes++] = wt;
1017                     }
1018
1019                   for (c2 = 0; c2 < NOTCHAR; ++c2)
1020                     if (pred->func (c2))
1021                       setbit_case_fold_c (c2, ccl);
1022                 }
1023
1024               else if (MBS_SUPPORT && (c1 == '=' || c1 == '.'))
1025                 {
1026                   char *elem = xmemdup (str, len + 1);
1027
1028                   if (c1 == '=')
1029                     /* build equivalence class.  */
1030                     {
1031                       REALLOC_IF_NECESSARY (work_mbc->equivs,
1032                                             equivs_al, work_mbc->nequivs + 1);
1033                       work_mbc->equivs[work_mbc->nequivs++] = elem;
1034                     }
1035
1036                   if (c1 == '.')
1037                     /* build collating element.  */
1038                     {
1039                       REALLOC_IF_NECESSARY (work_mbc->coll_elems,
1040                                             coll_elems_al,
1041                                             work_mbc->ncoll_elems + 1);
1042                       work_mbc->coll_elems[work_mbc->ncoll_elems++] = elem;
1043                     }
1044                 }
1045               colon_warning_state |= 8;
1046
1047               /* Fetch new lookahead character.  */
1048               FETCH_WC (c1, wc1, _("unbalanced ["));
1049               continue;
1050             }
1051
1052           /* We treat '[' as a normal character here.  c/c1/wc/wc1
1053              are already set up.  */
1054         }
1055
1056       if (c == '\\' && (syntax_bits & RE_BACKSLASH_ESCAPE_IN_LISTS))
1057         FETCH_WC (c, wc, _("unbalanced ["));
1058
1059       if (c1 == EOF)
1060         FETCH_WC (c1, wc1, _("unbalanced ["));
1061
1062       if (c1 == '-')
1063         /* build range characters.  */
1064         {
1065           FETCH_WC (c2, wc2, _("unbalanced ["));
1066           if (c2 == ']')
1067             {
1068               /* In the case [x-], the - is an ordinary hyphen,
1069                  which is left in c1, the lookahead character. */
1070               lexptr -= cur_mb_len;
1071               lexleft += cur_mb_len;
1072             }
1073         }
1074
1075       if (c1 == '-' && c2 != ']')
1076         {
1077           if (c2 == '\\' && (syntax_bits & RE_BACKSLASH_ESCAPE_IN_LISTS))
1078             FETCH_WC (c2, wc2, _("unbalanced ["));
1079
1080           if (MB_CUR_MAX > 1)
1081             {
1082               /* When case folding map a range, say [m-z] (or even [M-z])
1083                  to the pair of ranges, [m-z] [M-Z].  */
1084               REALLOC_IF_NECESSARY (work_mbc->range_sts,
1085                                     range_sts_al, work_mbc->nranges + 1);
1086               REALLOC_IF_NECESSARY (work_mbc->range_ends,
1087                                     range_ends_al, work_mbc->nranges + 1);
1088               work_mbc->range_sts[work_mbc->nranges] =
1089                 case_fold ? towlower (wc) : (wchar_t) wc;
1090               work_mbc->range_ends[work_mbc->nranges++] =
1091                 case_fold ? towlower (wc2) : (wchar_t) wc2;
1092
1093 #ifndef GREP
1094               if (case_fold && (iswalpha (wc) || iswalpha (wc2)))
1095                 {
1096                   REALLOC_IF_NECESSARY (work_mbc->range_sts,
1097                                         range_sts_al, work_mbc->nranges + 1);
1098                   work_mbc->range_sts[work_mbc->nranges] = towupper (wc);
1099                   REALLOC_IF_NECESSARY (work_mbc->range_ends,
1100                                         range_ends_al, work_mbc->nranges + 1);
1101                   work_mbc->range_ends[work_mbc->nranges++] = towupper (wc2);
1102                 }
1103 #endif
1104             }
1105           else
1106             {
1107               /* Defer to the system regex library about the meaning
1108                  of range expressions.  */
1109               regex_t re;
1110               char pattern[6] = { '[', 0, '-', 0, ']', 0 };
1111               char subject[2] = { 0, 0 };
1112               c1 = c;
1113               if (case_fold)
1114                 {
1115                   c1 = tolower (c1);
1116                   c2 = tolower (c2);
1117                 }
1118
1119               pattern[1] = c1;
1120               pattern[3] = c2;
1121               regcomp (&re, pattern, REG_NOSUB);
1122               for (c = 0; c < NOTCHAR; ++c)
1123                 {
1124                   if ((case_fold && isupper (c))
1125                       || (MB_CUR_MAX > 1 && btowc (c) == WEOF))
1126                     continue;
1127                   subject[0] = c;
1128                   if (regexec (&re, subject, 0, NULL, 0) != REG_NOMATCH)
1129                     setbit_case_fold_c (c, ccl);
1130                 }
1131               regfree (&re);
1132             }
1133
1134           colon_warning_state |= 8;
1135           FETCH_WC (c1, wc1, _("unbalanced ["));
1136           continue;
1137         }
1138
1139       colon_warning_state |= (c == ':') ? 2 : 4;
1140
1141       if (MB_CUR_MAX == 1)
1142         {
1143           setbit_case_fold_c (c, ccl);
1144           continue;
1145         }
1146
1147       if (case_fold && iswalpha (wc))
1148         {
1149           wc = towlower (wc);
1150           if (!setbit_wc (wc, ccl))
1151             {
1152               REALLOC_IF_NECESSARY (work_mbc->chars, chars_al,
1153                                     work_mbc->nchars + 1);
1154               work_mbc->chars[work_mbc->nchars++] = wc;
1155             }
1156 #ifdef GREP
1157           continue;
1158 #else
1159           wc = towupper (wc);
1160 #endif
1161         }
1162       if (!setbit_wc (wc, ccl))
1163         {
1164           REALLOC_IF_NECESSARY (work_mbc->chars, chars_al,
1165                                 work_mbc->nchars + 1);
1166           work_mbc->chars[work_mbc->nchars++] = wc;
1167         }
1168     }
1169   while ((wc = wc1, (c = c1) != ']'));
1170
1171   if (colon_warning_state == 7)
1172     dfawarn (_("character class syntax is [[:space:]], not [:space:]"));
1173
1174   if (MB_CUR_MAX > 1)
1175     {
1176       static charclass zeroclass;
1177       work_mbc->invert = invert;
1178       work_mbc->cset = equal (ccl, zeroclass) ? -1 : charclass_index (ccl);
1179       return MBCSET;
1180     }
1181
1182   if (invert)
1183     {
1184       assert (MB_CUR_MAX == 1);
1185       notset (ccl);
1186       if (syntax_bits & RE_HAT_LISTS_NOT_NEWLINE)
1187         clrbit (eolbyte, ccl);
1188     }
1189
1190   return CSET + charclass_index (ccl);
1191 }
1192
1193 static token
1194 lex (void)
1195 {
1196   unsigned int c, c2;
1197   int backslash = 0;
1198   charclass ccl;
1199   int i;
1200
1201   /* Basic plan: We fetch a character.  If it's a backslash,
1202      we set the backslash flag and go through the loop again.
1203      On the plus side, this avoids having a duplicate of the
1204      main switch inside the backslash case.  On the minus side,
1205      it means that just about every case begins with
1206      "if (backslash) ...".  */
1207   for (i = 0; i < 2; ++i)
1208     {
1209       if (MB_CUR_MAX > 1)
1210         {
1211           FETCH_WC (c, wctok, NULL);
1212           if ((int) c == EOF)
1213             goto normal_char;
1214         }
1215       else
1216         FETCH (c, NULL);
1217
1218       switch (c)
1219         {
1220         case '\\':
1221           if (backslash)
1222             goto normal_char;
1223           if (lexleft == 0)
1224             dfaerror (_("unfinished \\ escape"));
1225           backslash = 1;
1226           break;
1227
1228         case '^':
1229           if (backslash)
1230             goto normal_char;
1231           if (syntax_bits & RE_CONTEXT_INDEP_ANCHORS
1232               || lasttok == END || lasttok == LPAREN || lasttok == OR)
1233             return lasttok = BEGLINE;
1234           goto normal_char;
1235
1236         case '$':
1237           if (backslash)
1238             goto normal_char;
1239           if (syntax_bits & RE_CONTEXT_INDEP_ANCHORS
1240               || lexleft == 0
1241               || (syntax_bits & RE_NO_BK_PARENS
1242                   ? lexleft > 0 && *lexptr == ')'
1243                   : lexleft > 1 && lexptr[0] == '\\' && lexptr[1] == ')')
1244               || (syntax_bits & RE_NO_BK_VBAR
1245                   ? lexleft > 0 && *lexptr == '|'
1246                   : lexleft > 1 && lexptr[0] == '\\' && lexptr[1] == '|')
1247               || ((syntax_bits & RE_NEWLINE_ALT)
1248                   && lexleft > 0 && *lexptr == '\n'))
1249             return lasttok = ENDLINE;
1250           goto normal_char;
1251
1252         case '1':
1253         case '2':
1254         case '3':
1255         case '4':
1256         case '5':
1257         case '6':
1258         case '7':
1259         case '8':
1260         case '9':
1261           if (backslash && !(syntax_bits & RE_NO_BK_REFS))
1262             {
1263               laststart = 0;
1264               return lasttok = BACKREF;
1265             }
1266           goto normal_char;
1267
1268         case '`':
1269           if (backslash && !(syntax_bits & RE_NO_GNU_OPS))
1270             return lasttok = BEGLINE;   /* FIXME: should be beginning of string */
1271           goto normal_char;
1272
1273         case '\'':
1274           if (backslash && !(syntax_bits & RE_NO_GNU_OPS))
1275             return lasttok = ENDLINE;   /* FIXME: should be end of string */
1276           goto normal_char;
1277
1278         case '<':
1279           if (backslash && !(syntax_bits & RE_NO_GNU_OPS))
1280             return lasttok = BEGWORD;
1281           goto normal_char;
1282
1283         case '>':
1284           if (backslash && !(syntax_bits & RE_NO_GNU_OPS))
1285             return lasttok = ENDWORD;
1286           goto normal_char;
1287
1288         case 'b':
1289           if (backslash && !(syntax_bits & RE_NO_GNU_OPS))
1290             return lasttok = LIMWORD;
1291           goto normal_char;
1292
1293         case 'B':
1294           if (backslash && !(syntax_bits & RE_NO_GNU_OPS))
1295             return lasttok = NOTLIMWORD;
1296           goto normal_char;
1297
1298         case '?':
1299           if (syntax_bits & RE_LIMITED_OPS)
1300             goto normal_char;
1301           if (backslash != ((syntax_bits & RE_BK_PLUS_QM) != 0))
1302             goto normal_char;
1303           if (!(syntax_bits & RE_CONTEXT_INDEP_OPS) && laststart)
1304             goto normal_char;
1305           return lasttok = QMARK;
1306
1307         case '*':
1308           if (backslash)
1309             goto normal_char;
1310           if (!(syntax_bits & RE_CONTEXT_INDEP_OPS) && laststart)
1311             goto normal_char;
1312           return lasttok = STAR;
1313
1314         case '+':
1315           if (syntax_bits & RE_LIMITED_OPS)
1316             goto normal_char;
1317           if (backslash != ((syntax_bits & RE_BK_PLUS_QM) != 0))
1318             goto normal_char;
1319           if (!(syntax_bits & RE_CONTEXT_INDEP_OPS) && laststart)
1320             goto normal_char;
1321           return lasttok = PLUS;
1322
1323         case '{':
1324           if (!(syntax_bits & RE_INTERVALS))
1325             goto normal_char;
1326           if (backslash != ((syntax_bits & RE_NO_BK_BRACES) == 0))
1327             goto normal_char;
1328           if (!(syntax_bits & RE_CONTEXT_INDEP_OPS) && laststart)
1329             goto normal_char;
1330
1331           /* Cases:
1332              {M} - exact count
1333              {M,} - minimum count, maximum is infinity
1334              {,N} - 0 through N
1335              {,} - 0 to infinity (same as '*')
1336              {M,N} - M through N */
1337           {
1338             char const *p = lexptr;
1339             char const *lim = p + lexleft;
1340             minrep = maxrep = -1;
1341             for (; p != lim && ISASCIIDIGIT (*p); p++)
1342               {
1343                 if (minrep < 0)
1344                   minrep = *p - '0';
1345                 else
1346                   minrep = MIN (RE_DUP_MAX + 1, minrep * 10 + *p - '0');
1347               }
1348             if (p != lim)
1349               {
1350                 if (*p != ',')
1351                   maxrep = minrep;
1352                 else
1353                   {
1354                     if (minrep < 0)
1355                       minrep = 0;
1356                     while (++p != lim && ISASCIIDIGIT (*p))
1357                       {
1358                         if (maxrep < 0)
1359                           maxrep = *p - '0';
1360                         else
1361                           maxrep = MIN (RE_DUP_MAX + 1, maxrep * 10 + *p - '0');
1362                       }
1363                   }
1364               }
1365             if (! ((! backslash || (p != lim && *p++ == '\\'))
1366                    && p != lim && *p++ == '}'
1367                    && 0 <= minrep && (maxrep < 0 || minrep <= maxrep)))
1368               {
1369                 if (syntax_bits & RE_INVALID_INTERVAL_ORD)
1370                   goto normal_char;
1371                 dfaerror (_("Invalid content of \\{\\}"));
1372               }
1373             if (RE_DUP_MAX < maxrep)
1374               dfaerror (_("Regular expression too big"));
1375             lexptr = p;
1376             lexleft = lim - p;
1377           }
1378           laststart = 0;
1379           return lasttok = REPMN;
1380
1381         case '|':
1382           if (syntax_bits & RE_LIMITED_OPS)
1383             goto normal_char;
1384           if (backslash != ((syntax_bits & RE_NO_BK_VBAR) == 0))
1385             goto normal_char;
1386           laststart = 1;
1387           return lasttok = OR;
1388
1389         case '\n':
1390           if (syntax_bits & RE_LIMITED_OPS
1391               || backslash || !(syntax_bits & RE_NEWLINE_ALT))
1392             goto normal_char;
1393           laststart = 1;
1394           return lasttok = OR;
1395
1396         case '(':
1397           if (backslash != ((syntax_bits & RE_NO_BK_PARENS) == 0))
1398             goto normal_char;
1399           ++parens;
1400           laststart = 1;
1401           return lasttok = LPAREN;
1402
1403         case ')':
1404           if (backslash != ((syntax_bits & RE_NO_BK_PARENS) == 0))
1405             goto normal_char;
1406           if (parens == 0 && syntax_bits & RE_UNMATCHED_RIGHT_PAREN_ORD)
1407             goto normal_char;
1408           --parens;
1409           laststart = 0;
1410           return lasttok = RPAREN;
1411
1412         case '.':
1413           if (backslash)
1414             goto normal_char;
1415           if (MB_CUR_MAX > 1)
1416             {
1417               /* In multibyte environment period must match with a single
1418                  character not a byte.  So we use ANYCHAR.  */
1419               laststart = 0;
1420               return lasttok = ANYCHAR;
1421             }
1422           zeroset (ccl);
1423           notset (ccl);
1424           if (!(syntax_bits & RE_DOT_NEWLINE))
1425             clrbit (eolbyte, ccl);
1426           if (syntax_bits & RE_DOT_NOT_NULL)
1427             clrbit ('\0', ccl);
1428           laststart = 0;
1429           return lasttok = CSET + charclass_index (ccl);
1430
1431         case 's':
1432         case 'S':
1433           if (!backslash || (syntax_bits & RE_NO_GNU_OPS))
1434             goto normal_char;
1435           zeroset (ccl);
1436           for (c2 = 0; c2 < NOTCHAR; ++c2)
1437             if (isspace (c2))
1438               setbit (c2, ccl);
1439           if (c == 'S')
1440             notset (ccl);
1441           laststart = 0;
1442           return lasttok = CSET + charclass_index (ccl);
1443
1444         case 'w':
1445         case 'W':
1446           if (!backslash || (syntax_bits & RE_NO_GNU_OPS))
1447             goto normal_char;
1448           zeroset (ccl);
1449           for (c2 = 0; c2 < NOTCHAR; ++c2)
1450             if (IS_WORD_CONSTITUENT (c2))
1451               setbit (c2, ccl);
1452           if (c == 'W')
1453             notset (ccl);
1454           laststart = 0;
1455           return lasttok = CSET + charclass_index (ccl);
1456
1457         case '[':
1458           if (backslash)
1459             goto normal_char;
1460           laststart = 0;
1461           return lasttok = parse_bracket_exp ();
1462
1463         default:
1464         normal_char:
1465           laststart = 0;
1466           /* For multibyte character sets, folding is done in atom.  Always
1467              return WCHAR.  */
1468           if (MB_CUR_MAX > 1)
1469             return lasttok = WCHAR;
1470
1471           if (case_fold && isalpha (c))
1472             {
1473               zeroset (ccl);
1474               setbit_case_fold_c (c, ccl);
1475               return lasttok = CSET + charclass_index (ccl);
1476             }
1477
1478           return lasttok = c;
1479         }
1480     }
1481
1482   /* The above loop should consume at most a backslash
1483      and some other character. */
1484   abort ();
1485   return END;                   /* keeps pedantic compilers happy. */
1486 }
1487
1488 /* Recursive descent parser for regular expressions. */
1489
1490 static token tok;               /* Lookahead token. */
1491 static size_t depth;            /* Current depth of a hypothetical stack
1492                                    holding deferred productions.  This is
1493                                    used to determine the depth that will be
1494                                    required of the real stack later on in
1495                                    dfaanalyze(). */
1496
1497 static void
1498 addtok_mb (token t, int mbprop)
1499 {
1500   if (MB_CUR_MAX > 1)
1501     {
1502       REALLOC_IF_NECESSARY (dfa->multibyte_prop, dfa->nmultibyte_prop,
1503                             dfa->tindex + 1);
1504       dfa->multibyte_prop[dfa->tindex] = mbprop;
1505     }
1506
1507   REALLOC_IF_NECESSARY (dfa->tokens, dfa->talloc, dfa->tindex + 1);
1508   dfa->tokens[dfa->tindex++] = t;
1509
1510   switch (t)
1511     {
1512     case QMARK:
1513     case STAR:
1514     case PLUS:
1515       break;
1516
1517     case CAT:
1518     case OR:
1519       --depth;
1520       break;
1521
1522     default:
1523       ++dfa->nleaves;
1524     case EMPTY:
1525       ++depth;
1526       break;
1527     }
1528   if (depth > dfa->depth)
1529     dfa->depth = depth;
1530 }
1531
1532 static void addtok_wc (wint_t wc);
1533
1534 /* Add the given token to the parse tree, maintaining the depth count and
1535    updating the maximum depth if necessary. */
1536 static void
1537 addtok (token t)
1538 {
1539   if (MB_CUR_MAX > 1 && t == MBCSET)
1540     {
1541       bool need_or = false;
1542       struct mb_char_classes *work_mbc = &dfa->mbcsets[dfa->nmbcsets - 1];
1543
1544       /* Extract wide characters into alternations for better performance.
1545          This does not require UTF-8.  */
1546       if (!work_mbc->invert)
1547         {
1548           size_t i;
1549           for (i = 0; i < work_mbc->nchars; i++)
1550             {
1551               addtok_wc (work_mbc->chars[i]);
1552               if (need_or)
1553                 addtok (OR);
1554               need_or = true;
1555             }
1556           work_mbc->nchars = 0;
1557         }
1558
1559       /* UTF-8 allows treating a simple, non-inverted MBCSET like a CSET.  */
1560       if (work_mbc->invert
1561           || (!using_utf8 () && work_mbc->cset != -1)
1562           || work_mbc->nchars != 0
1563           || work_mbc->nch_classes != 0
1564           || work_mbc->nranges != 0
1565           || work_mbc->nequivs != 0 || work_mbc->ncoll_elems != 0)
1566         {
1567           addtok_mb (MBCSET, ((dfa->nmbcsets - 1) << 2) + 3);
1568           if (need_or)
1569             addtok (OR);
1570         }
1571       else
1572         {
1573           /* Characters have been handled above, so it is possible
1574              that the mbcset is empty now.  Do nothing in that case.  */
1575           if (work_mbc->cset != -1)
1576             {
1577               assert (using_utf8 ());
1578               addtok (CSET + work_mbc->cset);
1579               if (need_or)
1580                 addtok (OR);
1581             }
1582         }
1583     }
1584   else
1585     {
1586       addtok_mb (t, 3);
1587     }
1588 }
1589
1590 #if MBS_SUPPORT
1591 /* We treat a multibyte character as a single atom, so that DFA
1592    can treat a multibyte character as a single expression.
1593
1594    e.g. We construct following tree from "<mb1><mb2>".
1595    <mb1(1st-byte)><mb1(2nd-byte)><CAT><mb1(3rd-byte)><CAT>
1596    <mb2(1st-byte)><mb2(2nd-byte)><CAT><mb2(3rd-byte)><CAT><CAT> */
1597 static void
1598 addtok_wc (wint_t wc)
1599 {
1600   unsigned char buf[MB_LEN_MAX];
1601   mbstate_t s;
1602   int i;
1603   memset (&s, 0, sizeof s);
1604   cur_mb_len = wcrtomb ((char *) buf, wc, &s);
1605
1606   /* This is merely stop-gap.  When cur_mb_len is 0 or negative,
1607      buf[0] is undefined, yet skipping the addtok_mb call altogether
1608      can result in heap corruption.  */
1609   if (cur_mb_len <= 0)
1610     buf[0] = 0;
1611
1612   addtok_mb (buf[0], cur_mb_len == 1 ? 3 : 1);
1613   for (i = 1; i < cur_mb_len; i++)
1614     {
1615       addtok_mb (buf[i], i == cur_mb_len - 1 ? 2 : 0);
1616       addtok (CAT);
1617     }
1618 }
1619 #else
1620 static void
1621 addtok_wc (wint_t wc)
1622 {
1623 }
1624 #endif
1625
1626 static void
1627 add_utf8_anychar (void)
1628 {
1629 #if MBS_SUPPORT
1630   static const charclass utf8_classes[5] = {
1631     {0, 0, 0, 0, ~0, ~0, 0, 0}, /* 80-bf: non-lead bytes */
1632     {~0, ~0, ~0, ~0, 0, 0, 0, 0},       /* 00-7f: 1-byte sequence */
1633     {0, 0, 0, 0, 0, 0, ~3, 0},          /* c2-df: 2-byte sequence */
1634     {0, 0, 0, 0, 0, 0, 0, 0xffff},      /* e0-ef: 3-byte sequence */
1635     {0, 0, 0, 0, 0, 0, 0, 0xff0000}     /* f0-f7: 4-byte sequence */
1636   };
1637   const unsigned int n = sizeof (utf8_classes) / sizeof (utf8_classes[0]);
1638   unsigned int i;
1639
1640   /* Define the five character classes that are needed below.  */
1641   if (dfa->utf8_anychar_classes[0] == 0)
1642     for (i = 0; i < n; i++)
1643       {
1644         charclass c;
1645         copyset (utf8_classes[i], c);
1646         if (i == 1)
1647           {
1648             if (!(syntax_bits & RE_DOT_NEWLINE))
1649               clrbit (eolbyte, c);
1650             if (syntax_bits & RE_DOT_NOT_NULL)
1651               clrbit ('\0', c);
1652           }
1653         dfa->utf8_anychar_classes[i] = CSET + charclass_index (c);
1654       }
1655
1656   /* A valid UTF-8 character is
1657
1658      ([0x00-0x7f]
1659      |[0xc2-0xdf][0x80-0xbf]
1660      |[0xe0-0xef[0x80-0xbf][0x80-0xbf]
1661      |[0xf0-f7][0x80-0xbf][0x80-0xbf][0x80-0xbf])
1662
1663      which I'll write more concisely "B|CA|DAA|EAAA".  Factor the [0x00-0x7f]
1664      and you get "B|(C|(D|EA)A)A".  And since the token buffer is in reverse
1665      Polish notation, you get "B C D E A CAT OR A CAT OR A CAT OR".  */
1666   for (i = 1; i < n; i++)
1667     addtok (dfa->utf8_anychar_classes[i]);
1668   while (--i > 1)
1669     {
1670       addtok (dfa->utf8_anychar_classes[0]);
1671       addtok (CAT);
1672       addtok (OR);
1673     }
1674 #endif
1675 }
1676
1677 /* The grammar understood by the parser is as follows.
1678
1679    regexp:
1680      regexp OR branch
1681      branch
1682
1683    branch:
1684      branch closure
1685      closure
1686
1687    closure:
1688      closure QMARK
1689      closure STAR
1690      closure PLUS
1691      closure REPMN
1692      atom
1693
1694    atom:
1695      <normal character>
1696      <multibyte character>
1697      ANYCHAR
1698      MBCSET
1699      CSET
1700      BACKREF
1701      BEGLINE
1702      ENDLINE
1703      BEGWORD
1704      ENDWORD
1705      LIMWORD
1706      NOTLIMWORD
1707      LPAREN regexp RPAREN
1708      <empty>
1709
1710    The parser builds a parse tree in postfix form in an array of tokens. */
1711
1712 static void
1713 atom (void)
1714 {
1715   if (0)
1716     {
1717       /* empty */
1718     }
1719   else if (MBS_SUPPORT && tok == WCHAR)
1720     {
1721       addtok_wc (case_fold ? towlower (wctok) : wctok);
1722 #ifndef GREP
1723       if (case_fold && iswalpha (wctok))
1724         {
1725           addtok_wc (towupper (wctok));
1726           addtok (OR);
1727         }
1728 #endif
1729
1730       tok = lex ();
1731     }
1732   else if (MBS_SUPPORT && tok == ANYCHAR && using_utf8 ())
1733     {
1734       /* For UTF-8 expand the period to a series of CSETs that define a valid
1735          UTF-8 character.  This avoids using the slow multibyte path.  I'm
1736          pretty sure it would be both profitable and correct to do it for
1737          any encoding; however, the optimization must be done manually as
1738          it is done above in add_utf8_anychar.  So, let's start with
1739          UTF-8: it is the most used, and the structure of the encoding
1740          makes the correctness more obvious.  */
1741       add_utf8_anychar ();
1742       tok = lex ();
1743     }
1744   else if ((tok >= 0 && tok < NOTCHAR) || tok >= CSET || tok == BACKREF
1745            || tok == BEGLINE || tok == ENDLINE || tok == BEGWORD
1746 #if MBS_SUPPORT
1747            || tok == ANYCHAR || tok == MBCSET
1748 #endif /* MBS_SUPPORT */
1749            || tok == ENDWORD || tok == LIMWORD || tok == NOTLIMWORD)
1750     {
1751       addtok (tok);
1752       tok = lex ();
1753     }
1754   else if (tok == LPAREN)
1755     {
1756       tok = lex ();
1757       regexp ();
1758       if (tok != RPAREN)
1759         dfaerror (_("unbalanced ("));
1760       tok = lex ();
1761     }
1762   else
1763     addtok (EMPTY);
1764 }
1765
1766 /* Return the number of tokens in the given subexpression. */
1767 static size_t _GL_ATTRIBUTE_PURE
1768 nsubtoks (size_t tindex)
1769 {
1770   size_t ntoks1;
1771
1772   switch (dfa->tokens[tindex - 1])
1773     {
1774     default:
1775       return 1;
1776     case QMARK:
1777     case STAR:
1778     case PLUS:
1779       return 1 + nsubtoks (tindex - 1);
1780     case CAT:
1781     case OR:
1782       ntoks1 = nsubtoks (tindex - 1);
1783       return 1 + ntoks1 + nsubtoks (tindex - 1 - ntoks1);
1784     }
1785 }
1786
1787 /* Copy the given subexpression to the top of the tree. */
1788 static void
1789 copytoks (size_t tindex, size_t ntokens)
1790 {
1791   size_t i;
1792
1793   for (i = 0; i < ntokens; ++i)
1794     {
1795       addtok (dfa->tokens[tindex + i]);
1796       /* Update index into multibyte csets.  */
1797       if (MB_CUR_MAX > 1 && dfa->tokens[tindex + i] == MBCSET)
1798         dfa->multibyte_prop[dfa->tindex - 1] = dfa->multibyte_prop[tindex + i];
1799     }
1800 }
1801
1802 static void
1803 closure (void)
1804 {
1805   int i;
1806   size_t tindex, ntokens;
1807
1808   atom ();
1809   while (tok == QMARK || tok == STAR || tok == PLUS || tok == REPMN)
1810     if (tok == REPMN && (minrep || maxrep))
1811       {
1812         ntokens = nsubtoks (dfa->tindex);
1813         tindex = dfa->tindex - ntokens;
1814         if (maxrep < 0)
1815           addtok (PLUS);
1816         if (minrep == 0)
1817           addtok (QMARK);
1818         for (i = 1; i < minrep; ++i)
1819           {
1820             copytoks (tindex, ntokens);
1821             addtok (CAT);
1822           }
1823         for (; i < maxrep; ++i)
1824           {
1825             copytoks (tindex, ntokens);
1826             addtok (QMARK);
1827             addtok (CAT);
1828           }
1829         tok = lex ();
1830       }
1831     else if (tok == REPMN)
1832       {
1833         dfa->tindex -= nsubtoks (dfa->tindex);
1834         tok = lex ();
1835         closure ();
1836       }
1837     else
1838       {
1839         addtok (tok);
1840         tok = lex ();
1841       }
1842 }
1843
1844 static void
1845 branch (void)
1846 {
1847   closure ();
1848   while (tok != RPAREN && tok != OR && tok >= 0)
1849     {
1850       closure ();
1851       addtok (CAT);
1852     }
1853 }
1854
1855 static void
1856 regexp (void)
1857 {
1858   branch ();
1859   while (tok == OR)
1860     {
1861       tok = lex ();
1862       branch ();
1863       addtok (OR);
1864     }
1865 }
1866
1867 /* Main entry point for the parser.  S is a string to be parsed, len is the
1868    length of the string, so s can include NUL characters.  D is a pointer to
1869    the struct dfa to parse into. */
1870 void
1871 dfaparse (char const *s, size_t len, struct dfa *d)
1872 {
1873   dfa = d;
1874   lexptr = s;
1875   lexleft = len;
1876   lasttok = END;
1877   laststart = 1;
1878   parens = 0;
1879   if (MB_CUR_MAX > 1)
1880     {
1881       cur_mb_len = 0;
1882       memset (&mbs, 0, sizeof mbs);
1883     }
1884
1885   if (!syntax_bits_set)
1886     dfaerror (_("no syntax specified"));
1887
1888   tok = lex ();
1889   depth = d->depth;
1890
1891   regexp ();
1892
1893   if (tok != END)
1894     dfaerror (_("unbalanced )"));
1895
1896   addtok (END - d->nregexps);
1897   addtok (CAT);
1898
1899   if (d->nregexps)
1900     addtok (OR);
1901
1902   ++d->nregexps;
1903 }
1904
1905 /* Some primitives for operating on sets of positions. */
1906
1907 /* Copy one set to another; the destination must be large enough. */
1908 static void
1909 copy (position_set const *src, position_set * dst)
1910 {
1911   REALLOC_IF_NECESSARY (dst->elems, dst->alloc, src->nelem);
1912   memcpy (dst->elems, src->elems, sizeof (dst->elems[0]) * src->nelem);
1913   dst->nelem = src->nelem;
1914 }
1915
1916 static void
1917 alloc_position_set (position_set * s, size_t size)
1918 {
1919   MALLOC (s->elems, size);
1920   s->alloc = size;
1921   s->nelem = 0;
1922 }
1923
1924 /* Insert position P in set S.  S is maintained in sorted order on
1925    decreasing index.  If there is already an entry in S with P.index
1926    then merge (logically-OR) P's constraints into the one in S.
1927    S->elems must point to an array large enough to hold the resulting set. */
1928 static void
1929 insert (position p, position_set * s)
1930 {
1931   size_t count = s->nelem;
1932   size_t lo = 0, hi = count;
1933   size_t i;
1934   while (lo < hi)
1935     {
1936       size_t mid = (lo + hi) >> 1;
1937       if (s->elems[mid].index > p.index)
1938         lo = mid + 1;
1939       else
1940         hi = mid;
1941     }
1942
1943   if (lo < count && p.index == s->elems[lo].index)
1944     {
1945       s->elems[lo].constraint |= p.constraint;
1946       return;
1947     }
1948
1949   REALLOC_IF_NECESSARY (s->elems, s->alloc, count + 1);
1950   for (i = count; i > lo; i--)
1951     s->elems[i] = s->elems[i - 1];
1952   s->elems[lo] = p;
1953   ++s->nelem;
1954 }
1955
1956 /* Merge two sets of positions into a third.  The result is exactly as if
1957    the positions of both sets were inserted into an initially empty set. */
1958 static void
1959 merge (position_set const *s1, position_set const *s2, position_set * m)
1960 {
1961   size_t i = 0, j = 0;
1962
1963   REALLOC_IF_NECESSARY (m->elems, m->alloc, s1->nelem + s2->nelem);
1964   m->nelem = 0;
1965   while (i < s1->nelem && j < s2->nelem)
1966     if (s1->elems[i].index > s2->elems[j].index)
1967       m->elems[m->nelem++] = s1->elems[i++];
1968     else if (s1->elems[i].index < s2->elems[j].index)
1969       m->elems[m->nelem++] = s2->elems[j++];
1970     else
1971       {
1972         m->elems[m->nelem] = s1->elems[i++];
1973         m->elems[m->nelem++].constraint |= s2->elems[j++].constraint;
1974       }
1975   while (i < s1->nelem)
1976     m->elems[m->nelem++] = s1->elems[i++];
1977   while (j < s2->nelem)
1978     m->elems[m->nelem++] = s2->elems[j++];
1979 }
1980
1981 /* Delete a position from a set. */
1982 static void
1983 delete (position p, position_set * s)
1984 {
1985   size_t i;
1986
1987   for (i = 0; i < s->nelem; ++i)
1988     if (p.index == s->elems[i].index)
1989       break;
1990   if (i < s->nelem)
1991     for (--s->nelem; i < s->nelem; ++i)
1992       s->elems[i] = s->elems[i + 1];
1993 }
1994
1995 /* Find the index of the state corresponding to the given position set with
1996    the given preceding context, or create a new state if there is no such
1997    state.  Context tells whether we got here on a newline or letter. */
1998 static state_num
1999 state_index (struct dfa *d, position_set const *s, int context)
2000 {
2001   size_t hash = 0;
2002   int constraint;
2003   state_num i, j;
2004
2005   for (i = 0; i < s->nelem; ++i)
2006     hash ^= s->elems[i].index + s->elems[i].constraint;
2007
2008   /* Try to find a state that exactly matches the proposed one. */
2009   for (i = 0; i < d->sindex; ++i)
2010     {
2011       if (hash != d->states[i].hash || s->nelem != d->states[i].elems.nelem
2012           || context != d->states[i].context)
2013         continue;
2014       for (j = 0; j < s->nelem; ++j)
2015         if (s->elems[j].constraint
2016             != d->states[i].elems.elems[j].constraint
2017             || s->elems[j].index != d->states[i].elems.elems[j].index)
2018           break;
2019       if (j == s->nelem)
2020         return i;
2021     }
2022
2023   /* We'll have to create a new state. */
2024   REALLOC_IF_NECESSARY (d->states, d->salloc, d->sindex + 1);
2025   d->states[i].hash = hash;
2026   alloc_position_set (&d->states[i].elems, s->nelem);
2027   copy (s, &d->states[i].elems);
2028   d->states[i].context = context;
2029   d->states[i].backref = 0;
2030   d->states[i].constraint = 0;
2031   d->states[i].first_end = 0;
2032   if (MBS_SUPPORT)
2033     {
2034       d->states[i].mbps.nelem = 0;
2035       d->states[i].mbps.elems = NULL;
2036     }
2037   for (j = 0; j < s->nelem; ++j)
2038     if (d->tokens[s->elems[j].index] < 0)
2039       {
2040         constraint = s->elems[j].constraint;
2041         if (SUCCEEDS_IN_CONTEXT (constraint, context, CTX_ANY))
2042           d->states[i].constraint |= constraint;
2043         if (!d->states[i].first_end)
2044           d->states[i].first_end = d->tokens[s->elems[j].index];
2045       }
2046     else if (d->tokens[s->elems[j].index] == BACKREF)
2047       {
2048         d->states[i].constraint = NO_CONSTRAINT;
2049         d->states[i].backref = 1;
2050       }
2051
2052   ++d->sindex;
2053
2054   return i;
2055 }
2056
2057 /* Find the epsilon closure of a set of positions.  If any position of the set
2058    contains a symbol that matches the empty string in some context, replace
2059    that position with the elements of its follow labeled with an appropriate
2060    constraint.  Repeat exhaustively until no funny positions are left.
2061    S->elems must be large enough to hold the result. */
2062 static void
2063 epsclosure (position_set * s, struct dfa const *d)
2064 {
2065   size_t i, j;
2066   char *visited;                /* array of booleans, enough to use char, not int */
2067   position p, old;
2068
2069   CALLOC (visited, d->tindex);
2070
2071   for (i = 0; i < s->nelem; ++i)
2072     if (d->tokens[s->elems[i].index] >= NOTCHAR
2073         && d->tokens[s->elems[i].index] != BACKREF
2074 #if MBS_SUPPORT
2075         && d->tokens[s->elems[i].index] != ANYCHAR
2076         && d->tokens[s->elems[i].index] != MBCSET
2077 #endif
2078         && d->tokens[s->elems[i].index] < CSET)
2079       {
2080         old = s->elems[i];
2081         p.constraint = old.constraint;
2082         delete (s->elems[i], s);
2083         if (visited[old.index])
2084           {
2085             --i;
2086             continue;
2087           }
2088         visited[old.index] = 1;
2089         switch (d->tokens[old.index])
2090           {
2091           case BEGLINE:
2092             p.constraint &= BEGLINE_CONSTRAINT;
2093             break;
2094           case ENDLINE:
2095             p.constraint &= ENDLINE_CONSTRAINT;
2096             break;
2097           case BEGWORD:
2098             p.constraint &= BEGWORD_CONSTRAINT;
2099             break;
2100           case ENDWORD:
2101             p.constraint &= ENDWORD_CONSTRAINT;
2102             break;
2103           case LIMWORD:
2104             p.constraint &= LIMWORD_CONSTRAINT;
2105             break;
2106           case NOTLIMWORD:
2107             p.constraint &= NOTLIMWORD_CONSTRAINT;
2108             break;
2109           default:
2110             break;
2111           }
2112         for (j = 0; j < d->follows[old.index].nelem; ++j)
2113           {
2114             p.index = d->follows[old.index].elems[j].index;
2115             insert (p, s);
2116           }
2117         /* Force rescan to start at the beginning. */
2118         i = -1;
2119       }
2120
2121   free (visited);
2122 }
2123
2124 /* Returns the set of contexts for which there is at least one
2125    character included in C.  */
2126
2127 static int
2128 charclass_context (charclass c)
2129 {
2130   int context = 0;
2131   unsigned int j;
2132
2133   if (tstbit (eolbyte, c))
2134     context |= CTX_NEWLINE;
2135
2136   for (j = 0; j < CHARCLASS_INTS; ++j)
2137     {
2138       if (c[j] & letters[j])
2139         context |= CTX_LETTER;
2140       if (c[j] & ~(letters[j] | newline[j]))
2141         context |= CTX_NONE;
2142     }
2143
2144   return context;
2145 }
2146
2147 /* Returns the contexts on which the position set S depends.  Each context
2148    in the set of returned contexts (let's call it SC) may have a different
2149    follow set than other contexts in SC, and also different from the
2150    follow set of the complement set (sc ^ CTX_ANY).  However, all contexts
2151    in the complement set will have the same follow set.  */
2152
2153 static int _GL_ATTRIBUTE_PURE
2154 state_separate_contexts (position_set const *s)
2155 {
2156   int separate_contexts = 0;
2157   size_t j;
2158
2159   for (j = 0; j < s->nelem; ++j)
2160     {
2161       if (PREV_NEWLINE_DEPENDENT (s->elems[j].constraint))
2162         separate_contexts |= CTX_NEWLINE;
2163       if (PREV_LETTER_DEPENDENT (s->elems[j].constraint))
2164         separate_contexts |= CTX_LETTER;
2165     }
2166
2167   return separate_contexts;
2168 }
2169
2170
2171 /* Perform bottom-up analysis on the parse tree, computing various functions.
2172    Note that at this point, we're pretending constructs like \< are real
2173    characters rather than constraints on what can follow them.
2174
2175    Nullable:  A node is nullable if it is at the root of a regexp that can
2176    match the empty string.
2177    *  EMPTY leaves are nullable.
2178    * No other leaf is nullable.
2179    * A QMARK or STAR node is nullable.
2180    * A PLUS node is nullable if its argument is nullable.
2181    * A CAT node is nullable if both its arguments are nullable.
2182    * An OR node is nullable if either argument is nullable.
2183
2184    Firstpos:  The firstpos of a node is the set of positions (nonempty leaves)
2185    that could correspond to the first character of a string matching the
2186    regexp rooted at the given node.
2187    * EMPTY leaves have empty firstpos.
2188    * The firstpos of a nonempty leaf is that leaf itself.
2189    * The firstpos of a QMARK, STAR, or PLUS node is the firstpos of its
2190      argument.
2191    * The firstpos of a CAT node is the firstpos of the left argument, union
2192      the firstpos of the right if the left argument is nullable.
2193    * The firstpos of an OR node is the union of firstpos of each argument.
2194
2195    Lastpos:  The lastpos of a node is the set of positions that could
2196    correspond to the last character of a string matching the regexp at
2197    the given node.
2198    * EMPTY leaves have empty lastpos.
2199    * The lastpos of a nonempty leaf is that leaf itself.
2200    * The lastpos of a QMARK, STAR, or PLUS node is the lastpos of its
2201      argument.
2202    * The lastpos of a CAT node is the lastpos of its right argument, union
2203      the lastpos of the left if the right argument is nullable.
2204    * The lastpos of an OR node is the union of the lastpos of each argument.
2205
2206    Follow:  The follow of a position is the set of positions that could
2207    correspond to the character following a character matching the node in
2208    a string matching the regexp.  At this point we consider special symbols
2209    that match the empty string in some context to be just normal characters.
2210    Later, if we find that a special symbol is in a follow set, we will
2211    replace it with the elements of its follow, labeled with an appropriate
2212    constraint.
2213    * Every node in the firstpos of the argument of a STAR or PLUS node is in
2214      the follow of every node in the lastpos.
2215    * Every node in the firstpos of the second argument of a CAT node is in
2216      the follow of every node in the lastpos of the first argument.
2217
2218    Because of the postfix representation of the parse tree, the depth-first
2219    analysis is conveniently done by a linear scan with the aid of a stack.
2220    Sets are stored as arrays of the elements, obeying a stack-like allocation
2221    scheme; the number of elements in each set deeper in the stack can be
2222    used to determine the address of a particular set's array. */
2223 void
2224 dfaanalyze (struct dfa *d, int searchflag)
2225 {
2226   int *nullable;                /* Nullable stack. */
2227   size_t *nfirstpos;            /* Element count stack for firstpos sets. */
2228   position *firstpos;           /* Array where firstpos elements are stored. */
2229   size_t *nlastpos;             /* Element count stack for lastpos sets. */
2230   position *lastpos;            /* Array where lastpos elements are stored. */
2231   position_set tmp;             /* Temporary set for merging sets. */
2232   position_set merged;          /* Result of merging sets. */
2233   int separate_contexts;        /* Context wanted by some position. */
2234   int *o_nullable;
2235   size_t *o_nfirst, *o_nlast;
2236   position *o_firstpos, *o_lastpos;
2237   size_t i, j;
2238   position *pos;
2239
2240 #ifdef DEBUG
2241   fprintf (stderr, "dfaanalyze:\n");
2242   for (i = 0; i < d->tindex; ++i)
2243     {
2244       fprintf (stderr, " %zd:", i);
2245       prtok (d->tokens[i]);
2246     }
2247   putc ('\n', stderr);
2248 #endif
2249
2250   d->searchflag = searchflag;
2251
2252   MALLOC (nullable, d->depth);
2253   o_nullable = nullable;
2254   MALLOC (nfirstpos, d->depth);
2255   o_nfirst = nfirstpos;
2256   MALLOC (firstpos, d->nleaves);
2257   o_firstpos = firstpos, firstpos += d->nleaves;
2258   MALLOC (nlastpos, d->depth);
2259   o_nlast = nlastpos;
2260   MALLOC (lastpos, d->nleaves);
2261   o_lastpos = lastpos, lastpos += d->nleaves;
2262   alloc_position_set (&merged, d->nleaves);
2263
2264   CALLOC (d->follows, d->tindex);
2265
2266   for (i = 0; i < d->tindex; ++i)
2267     {
2268       switch (d->tokens[i])
2269         {
2270         case EMPTY:
2271           /* The empty set is nullable. */
2272           *nullable++ = 1;
2273
2274           /* The firstpos and lastpos of the empty leaf are both empty. */
2275           *nfirstpos++ = *nlastpos++ = 0;
2276           break;
2277
2278         case STAR:
2279         case PLUS:
2280           /* Every element in the firstpos of the argument is in the follow
2281              of every element in the lastpos. */
2282           tmp.nelem = nfirstpos[-1];
2283           tmp.elems = firstpos;
2284           pos = lastpos;
2285           for (j = 0; j < nlastpos[-1]; ++j)
2286             {
2287               merge (&tmp, &d->follows[pos[j].index], &merged);
2288               copy (&merged, &d->follows[pos[j].index]);
2289             }
2290
2291         case QMARK:
2292           /* A QMARK or STAR node is automatically nullable. */
2293           if (d->tokens[i] != PLUS)
2294             nullable[-1] = 1;
2295           break;
2296
2297         case CAT:
2298           /* Every element in the firstpos of the second argument is in the
2299              follow of every element in the lastpos of the first argument. */
2300           tmp.nelem = nfirstpos[-1];
2301           tmp.elems = firstpos;
2302           pos = lastpos + nlastpos[-1];
2303           for (j = 0; j < nlastpos[-2]; ++j)
2304             {
2305               merge (&tmp, &d->follows[pos[j].index], &merged);
2306               copy (&merged, &d->follows[pos[j].index]);
2307             }
2308
2309           /* The firstpos of a CAT node is the firstpos of the first argument,
2310              union that of the second argument if the first is nullable. */
2311           if (nullable[-2])
2312             nfirstpos[-2] += nfirstpos[-1];
2313           else
2314             firstpos += nfirstpos[-1];
2315           --nfirstpos;
2316
2317           /* The lastpos of a CAT node is the lastpos of the second argument,
2318              union that of the first argument if the second is nullable. */
2319           if (nullable[-1])
2320             nlastpos[-2] += nlastpos[-1];
2321           else
2322             {
2323               pos = lastpos + nlastpos[-2];
2324               for (j = nlastpos[-1]; j-- > 0;)
2325                 pos[j] = lastpos[j];
2326               lastpos += nlastpos[-2];
2327               nlastpos[-2] = nlastpos[-1];
2328             }
2329           --nlastpos;
2330
2331           /* A CAT node is nullable if both arguments are nullable. */
2332           nullable[-2] = nullable[-1] && nullable[-2];
2333           --nullable;
2334           break;
2335
2336         case OR:
2337           /* The firstpos is the union of the firstpos of each argument. */
2338           nfirstpos[-2] += nfirstpos[-1];
2339           --nfirstpos;
2340
2341           /* The lastpos is the union of the lastpos of each argument. */
2342           nlastpos[-2] += nlastpos[-1];
2343           --nlastpos;
2344
2345           /* An OR node is nullable if either argument is nullable. */
2346           nullable[-2] = nullable[-1] || nullable[-2];
2347           --nullable;
2348           break;
2349
2350         default:
2351           /* Anything else is a nonempty position.  (Note that special
2352              constructs like \< are treated as nonempty strings here;
2353              an "epsilon closure" effectively makes them nullable later.
2354              Backreferences have to get a real position so we can detect
2355              transitions on them later.  But they are nullable. */
2356           *nullable++ = d->tokens[i] == BACKREF;
2357
2358           /* This position is in its own firstpos and lastpos. */
2359           *nfirstpos++ = *nlastpos++ = 1;
2360           --firstpos, --lastpos;
2361           firstpos->index = lastpos->index = i;
2362           firstpos->constraint = lastpos->constraint = NO_CONSTRAINT;
2363
2364           /* Allocate the follow set for this position. */
2365           alloc_position_set (&d->follows[i], 1);
2366           break;
2367         }
2368 #ifdef DEBUG
2369       /* ... balance the above nonsyntactic #ifdef goo... */
2370       fprintf (stderr, "node %zd:", i);
2371       prtok (d->tokens[i]);
2372       putc ('\n', stderr);
2373       fprintf (stderr, nullable[-1] ? " nullable: yes\n" : " nullable: no\n");
2374       fprintf (stderr, " firstpos:");
2375       for (j = nfirstpos[-1]; j-- > 0;)
2376         {
2377           fprintf (stderr, " %zd:", firstpos[j].index);
2378           prtok (d->tokens[firstpos[j].index]);
2379         }
2380       fprintf (stderr, "\n lastpos:");
2381       for (j = nlastpos[-1]; j-- > 0;)
2382         {
2383           fprintf (stderr, " %zd:", lastpos[j].index);
2384           prtok (d->tokens[lastpos[j].index]);
2385         }
2386       putc ('\n', stderr);
2387 #endif
2388     }
2389
2390   /* For each follow set that is the follow set of a real position, replace
2391      it with its epsilon closure. */
2392   for (i = 0; i < d->tindex; ++i)
2393     if (d->tokens[i] < NOTCHAR || d->tokens[i] == BACKREF
2394 #if MBS_SUPPORT
2395         || d->tokens[i] == ANYCHAR || d->tokens[i] == MBCSET
2396 #endif
2397         || d->tokens[i] >= CSET)
2398       {
2399 #ifdef DEBUG
2400         fprintf (stderr, "follows(%zd:", i);
2401         prtok (d->tokens[i]);
2402         fprintf (stderr, "):");
2403         for (j = d->follows[i].nelem; j-- > 0;)
2404           {
2405             fprintf (stderr, " %zd:", d->follows[i].elems[j].index);
2406             prtok (d->tokens[d->follows[i].elems[j].index]);
2407           }
2408         putc ('\n', stderr);
2409 #endif
2410         copy (&d->follows[i], &merged);
2411         epsclosure (&merged, d);
2412         copy (&merged, &d->follows[i]);
2413       }
2414
2415   /* Get the epsilon closure of the firstpos of the regexp.  The result will
2416      be the set of positions of state 0. */
2417   merged.nelem = 0;
2418   for (i = 0; i < nfirstpos[-1]; ++i)
2419     insert (firstpos[i], &merged);
2420   epsclosure (&merged, d);
2421
2422   /* Build the initial state. */
2423   d->salloc = 1;
2424   d->sindex = 0;
2425   MALLOC (d->states, d->salloc);
2426
2427   separate_contexts = state_separate_contexts (&merged);
2428   state_index (d, &merged,
2429                (separate_contexts & CTX_NEWLINE
2430                 ? CTX_NEWLINE : separate_contexts ^ CTX_ANY));
2431
2432   free (o_nullable);
2433   free (o_nfirst);
2434   free (o_firstpos);
2435   free (o_nlast);
2436   free (o_lastpos);
2437   free (merged.elems);
2438 }
2439
2440
2441 /* Find, for each character, the transition out of state s of d, and store
2442    it in the appropriate slot of trans.
2443
2444    We divide the positions of s into groups (positions can appear in more
2445    than one group).  Each group is labeled with a set of characters that
2446    every position in the group matches (taking into account, if necessary,
2447    preceding context information of s).  For each group, find the union
2448    of the its elements' follows.  This set is the set of positions of the
2449    new state.  For each character in the group's label, set the transition
2450    on this character to be to a state corresponding to the set's positions,
2451    and its associated backward context information, if necessary.
2452
2453    If we are building a searching matcher, we include the positions of state
2454    0 in every state.
2455
2456    The collection of groups is constructed by building an equivalence-class
2457    partition of the positions of s.
2458
2459    For each position, find the set of characters C that it matches.  Eliminate
2460    any characters from C that fail on grounds of backward context.
2461
2462    Search through the groups, looking for a group whose label L has nonempty
2463    intersection with C.  If L - C is nonempty, create a new group labeled
2464    L - C and having the same positions as the current group, and set L to
2465    the intersection of L and C.  Insert the position in this group, set
2466    C = C - L, and resume scanning.
2467
2468    If after comparing with every group there are characters remaining in C,
2469    create a new group labeled with the characters of C and insert this
2470    position in that group. */
2471 void
2472 dfastate (state_num s, struct dfa *d, state_num trans[])
2473 {
2474   leaf_set *grps;               /* As many as will ever be needed. */
2475   charclass *labels;            /* Labels corresponding to the groups. */
2476   size_t ngrps = 0;             /* Number of groups actually used. */
2477   position pos;                 /* Current position being considered. */
2478   charclass matches;            /* Set of matching characters. */
2479   int matchesf;                 /* True if matches is nonempty. */
2480   charclass intersect;          /* Intersection with some label set. */
2481   int intersectf;               /* True if intersect is nonempty. */
2482   charclass leftovers;          /* Stuff in the label that didn't match. */
2483   int leftoversf;               /* True if leftovers is nonempty. */
2484   position_set follows;         /* Union of the follows of some group. */
2485   position_set tmp;             /* Temporary space for merging sets. */
2486   int possible_contexts;        /* Contexts that this group can match. */
2487   int separate_contexts;        /* Context that new state wants to know. */
2488   state_num state;              /* New state. */
2489   state_num state_newline;      /* New state on a newline transition. */
2490   state_num state_letter;       /* New state on a letter transition. */
2491   int next_isnt_1st_byte = 0;   /* Flag if we can't add state0.  */
2492   size_t i, j, k;
2493
2494   MALLOC (grps, NOTCHAR);
2495   MALLOC (labels, NOTCHAR);
2496
2497   zeroset (matches);
2498
2499   for (i = 0; i < d->states[s].elems.nelem; ++i)
2500     {
2501       pos = d->states[s].elems.elems[i];
2502       if (d->tokens[pos.index] >= 0 && d->tokens[pos.index] < NOTCHAR)
2503         setbit (d->tokens[pos.index], matches);
2504       else if (d->tokens[pos.index] >= CSET)
2505         copyset (d->charclasses[d->tokens[pos.index] - CSET], matches);
2506       else if (MBS_SUPPORT
2507                && (d->tokens[pos.index] == ANYCHAR
2508                    || d->tokens[pos.index] == MBCSET))
2509         /* MB_CUR_MAX > 1  */
2510         {
2511           /* ANYCHAR and MBCSET must match with a single character, so we
2512              must put it to d->states[s].mbps, which contains the positions
2513              which can match with a single character not a byte.  */
2514           if (d->states[s].mbps.nelem == 0)
2515             alloc_position_set (&d->states[s].mbps, 1);
2516           insert (pos, &(d->states[s].mbps));
2517           continue;
2518         }
2519       else
2520         continue;
2521
2522       /* Some characters may need to be eliminated from matches because
2523          they fail in the current context. */
2524       if (pos.constraint != NO_CONSTRAINT)
2525         {
2526           if (!SUCCEEDS_IN_CONTEXT (pos.constraint,
2527                                     d->states[s].context, CTX_NEWLINE))
2528             for (j = 0; j < CHARCLASS_INTS; ++j)
2529               matches[j] &= ~newline[j];
2530           if (!SUCCEEDS_IN_CONTEXT (pos.constraint,
2531                                     d->states[s].context, CTX_LETTER))
2532             for (j = 0; j < CHARCLASS_INTS; ++j)
2533               matches[j] &= ~letters[j];
2534           if (!SUCCEEDS_IN_CONTEXT (pos.constraint,
2535                                     d->states[s].context, CTX_NONE))
2536             for (j = 0; j < CHARCLASS_INTS; ++j)
2537               matches[j] &= letters[j] | newline[j];
2538
2539           /* If there are no characters left, there's no point in going on. */
2540           for (j = 0; j < CHARCLASS_INTS && !matches[j]; ++j)
2541             continue;
2542           if (j == CHARCLASS_INTS)
2543             continue;
2544         }
2545
2546       for (j = 0; j < ngrps; ++j)
2547         {
2548           /* If matches contains a single character only, and the current
2549              group's label doesn't contain that character, go on to the
2550              next group. */
2551           if (d->tokens[pos.index] >= 0 && d->tokens[pos.index] < NOTCHAR
2552               && !tstbit (d->tokens[pos.index], labels[j]))
2553             continue;
2554
2555           /* Check if this group's label has a nonempty intersection with
2556              matches. */
2557           intersectf = 0;
2558           for (k = 0; k < CHARCLASS_INTS; ++k)
2559             (intersect[k] = matches[k] & labels[j][k]) ? (intersectf = 1) : 0;
2560           if (!intersectf)
2561             continue;
2562
2563           /* It does; now find the set differences both ways. */
2564           leftoversf = matchesf = 0;
2565           for (k = 0; k < CHARCLASS_INTS; ++k)
2566             {
2567               /* Even an optimizing compiler can't know this for sure. */
2568               int match = matches[k], label = labels[j][k];
2569
2570               (leftovers[k] = ~match & label) ? (leftoversf = 1) : 0;
2571               (matches[k] = match & ~label) ? (matchesf = 1) : 0;
2572             }
2573
2574           /* If there were leftovers, create a new group labeled with them. */
2575           if (leftoversf)
2576             {
2577               copyset (leftovers, labels[ngrps]);
2578               copyset (intersect, labels[j]);
2579               MALLOC (grps[ngrps].elems, d->nleaves);
2580               memcpy (grps[ngrps].elems, grps[j].elems,
2581                       sizeof (grps[j].elems[0]) * grps[j].nelem);
2582               grps[ngrps].nelem = grps[j].nelem;
2583               ++ngrps;
2584             }
2585
2586           /* Put the position in the current group.  The constraint is
2587              irrelevant here.  */
2588           grps[j].elems[grps[j].nelem++] = pos.index;
2589
2590           /* If every character matching the current position has been
2591              accounted for, we're done. */
2592           if (!matchesf)
2593             break;
2594         }
2595
2596       /* If we've passed the last group, and there are still characters
2597          unaccounted for, then we'll have to create a new group. */
2598       if (j == ngrps)
2599         {
2600           copyset (matches, labels[ngrps]);
2601           zeroset (matches);
2602           MALLOC (grps[ngrps].elems, d->nleaves);
2603           grps[ngrps].nelem = 1;
2604           grps[ngrps].elems[0] = pos.index;
2605           ++ngrps;
2606         }
2607     }
2608
2609   alloc_position_set (&follows, d->nleaves);
2610   alloc_position_set (&tmp, d->nleaves);
2611
2612   /* If we are a searching matcher, the default transition is to a state
2613      containing the positions of state 0, otherwise the default transition
2614      is to fail miserably. */
2615   if (d->searchflag)
2616     {
2617       /* Find the state(s) corresponding to the positions of state 0. */
2618       copy (&d->states[0].elems, &follows);
2619       separate_contexts = state_separate_contexts (&follows);
2620       state = state_index (d, &follows, separate_contexts ^ CTX_ANY);
2621       if (separate_contexts & CTX_NEWLINE)
2622         state_newline = state_index (d, &follows, CTX_NEWLINE);
2623       else
2624         state_newline = state;
2625       if (separate_contexts & CTX_LETTER)
2626         state_letter = state_index (d, &follows, CTX_LETTER);
2627       else
2628         state_letter = state;
2629
2630       for (i = 0; i < NOTCHAR; ++i)
2631         trans[i] = (IS_WORD_CONSTITUENT (i)) ? state_letter : state;
2632       trans[eolbyte] = state_newline;
2633     }
2634   else
2635     for (i = 0; i < NOTCHAR; ++i)
2636       trans[i] = -1;
2637
2638   for (i = 0; i < ngrps; ++i)
2639     {
2640       follows.nelem = 0;
2641
2642       /* Find the union of the follows of the positions of the group.
2643          This is a hideously inefficient loop.  Fix it someday. */
2644       for (j = 0; j < grps[i].nelem; ++j)
2645         for (k = 0; k < d->follows[grps[i].elems[j]].nelem; ++k)
2646           insert (d->follows[grps[i].elems[j]].elems[k], &follows);
2647
2648       if (d->mb_cur_max > 1)
2649         {
2650           /* If a token in follows.elems is not 1st byte of a multibyte
2651              character, or the states of follows must accept the bytes
2652              which are not 1st byte of the multibyte character.
2653              Then, if a state of follows encounter a byte, it must not be
2654              a 1st byte of a multibyte character nor single byte character.
2655              We cansel to add state[0].follows to next state, because
2656              state[0] must accept 1st-byte
2657
2658              For example, we assume <sb a> is a certain single byte
2659              character, <mb A> is a certain multibyte character, and the
2660              codepoint of <sb a> equals the 2nd byte of the codepoint of
2661              <mb A>.
2662              When state[0] accepts <sb a>, state[i] transit to state[i+1]
2663              by accepting accepts 1st byte of <mb A>, and state[i+1]
2664              accepts 2nd byte of <mb A>, if state[i+1] encounter the
2665              codepoint of <sb a>, it must not be <sb a> but 2nd byte of
2666              <mb A>, so we cannot add state[0].  */
2667
2668           next_isnt_1st_byte = 0;
2669           for (j = 0; j < follows.nelem; ++j)
2670             {
2671               if (!(d->multibyte_prop[follows.elems[j].index] & 1))
2672                 {
2673                   next_isnt_1st_byte = 1;
2674                   break;
2675                 }
2676             }
2677         }
2678
2679       /* If we are building a searching matcher, throw in the positions
2680          of state 0 as well. */
2681       if (d->searchflag
2682           && (!MBS_SUPPORT || (d->mb_cur_max == 1 || !next_isnt_1st_byte)))
2683         for (j = 0; j < d->states[0].elems.nelem; ++j)
2684           insert (d->states[0].elems.elems[j], &follows);
2685
2686       /* Find out if the new state will want any context information. */
2687       possible_contexts = charclass_context (labels[i]);
2688       separate_contexts = state_separate_contexts (&follows);
2689
2690       /* Find the state(s) corresponding to the union of the follows. */
2691       if ((separate_contexts & possible_contexts) != possible_contexts)
2692         state = state_index (d, &follows, separate_contexts ^ CTX_ANY);
2693       else
2694         state = -1;
2695       if (separate_contexts & possible_contexts & CTX_NEWLINE)
2696         state_newline = state_index (d, &follows, CTX_NEWLINE);
2697       else
2698         state_newline = state;
2699       if (separate_contexts & possible_contexts & CTX_LETTER)
2700         state_letter = state_index (d, &follows, CTX_LETTER);
2701       else
2702         state_letter = state;
2703
2704       /* Set the transitions for each character in the current label. */
2705       for (j = 0; j < CHARCLASS_INTS; ++j)
2706         for (k = 0; k < INTBITS; ++k)
2707           if (labels[i][j] & 1 << k)
2708             {
2709               int c = j * INTBITS + k;
2710
2711               if (c == eolbyte)
2712                 trans[c] = state_newline;
2713               else if (IS_WORD_CONSTITUENT (c))
2714                 trans[c] = state_letter;
2715               else if (c < NOTCHAR)
2716                 trans[c] = state;
2717             }
2718     }
2719
2720   for (i = 0; i < ngrps; ++i)
2721     free (grps[i].elems);
2722   free (follows.elems);
2723   free (tmp.elems);
2724   free (grps);
2725   free (labels);
2726 }
2727
2728 /* Some routines for manipulating a compiled dfa's transition tables.
2729    Each state may or may not have a transition table; if it does, and it
2730    is a non-accepting state, then d->trans[state] points to its table.
2731    If it is an accepting state then d->fails[state] points to its table.
2732    If it has no table at all, then d->trans[state] is NULL.
2733    TODO: Improve this comment, get rid of the unnecessary redundancy. */
2734
2735 static void
2736 build_state (state_num s, struct dfa *d)
2737 {
2738   state_num *trans;             /* The new transition table. */
2739   state_num i;
2740
2741   /* Set an upper limit on the number of transition tables that will ever
2742      exist at once.  1024 is arbitrary.  The idea is that the frequently
2743      used transition tables will be quickly rebuilt, whereas the ones that
2744      were only needed once or twice will be cleared away. */
2745   if (d->trcount >= 1024)
2746     {
2747       for (i = 0; i < d->tralloc; ++i)
2748         {
2749           free (d->trans[i]);
2750           free (d->fails[i]);
2751           d->trans[i] = d->fails[i] = NULL;
2752         }
2753       d->trcount = 0;
2754     }
2755
2756   ++d->trcount;
2757
2758   /* Set up the success bits for this state. */
2759   d->success[s] = 0;
2760   if (ACCEPTS_IN_CONTEXT (d->states[s].context, CTX_NEWLINE, s, *d))
2761     d->success[s] |= CTX_NEWLINE;
2762   if (ACCEPTS_IN_CONTEXT (d->states[s].context, CTX_LETTER, s, *d))
2763     d->success[s] |= CTX_LETTER;
2764   if (ACCEPTS_IN_CONTEXT (d->states[s].context, CTX_NONE, s, *d))
2765     d->success[s] |= CTX_NONE;
2766
2767   MALLOC (trans, NOTCHAR);
2768   dfastate (s, d, trans);
2769
2770   /* Now go through the new transition table, and make sure that the trans
2771      and fail arrays are allocated large enough to hold a pointer for the
2772      largest state mentioned in the table. */
2773   for (i = 0; i < NOTCHAR; ++i)
2774     if (trans[i] >= d->tralloc)
2775       {
2776         state_num oldalloc = d->tralloc;
2777
2778         while (trans[i] >= d->tralloc)
2779           d->tralloc *= 2;
2780         REALLOC (d->realtrans, d->tralloc + 1);
2781         d->trans = d->realtrans + 1;
2782         REALLOC (d->fails, d->tralloc);
2783         REALLOC (d->success, d->tralloc);
2784         REALLOC (d->newlines, d->tralloc);
2785         while (oldalloc < d->tralloc)
2786           {
2787             d->trans[oldalloc] = NULL;
2788             d->fails[oldalloc++] = NULL;
2789           }
2790       }
2791
2792   /* Keep the newline transition in a special place so we can use it as
2793      a sentinel. */
2794   d->newlines[s] = trans[eolbyte];
2795   trans[eolbyte] = -1;
2796
2797   if (ACCEPTING (s, *d))
2798     d->fails[s] = trans;
2799   else
2800     d->trans[s] = trans;
2801 }
2802
2803 static void
2804 build_state_zero (struct dfa *d)
2805 {
2806   d->tralloc = 1;
2807   d->trcount = 0;
2808   CALLOC (d->realtrans, d->tralloc + 1);
2809   d->trans = d->realtrans + 1;
2810   CALLOC (d->fails, d->tralloc);
2811   MALLOC (d->success, d->tralloc);
2812   MALLOC (d->newlines, d->tralloc);
2813   build_state (0, d);
2814 }
2815
2816 /* Multibyte character handling sub-routines for dfaexec.  */
2817
2818 /* Initial state may encounter the byte which is not a single byte character
2819    nor 1st byte of a multibyte character.  But it is incorrect for initial
2820    state to accept such a byte.
2821    For example, in sjis encoding the regular expression like "\\" accepts
2822    the codepoint 0x5c, but should not accept the 2nd byte of the codepoint
2823    0x815c. Then Initial state must skip the bytes which are not a single byte
2824    character nor 1st byte of a multibyte character.  */
2825 #define SKIP_REMAINS_MB_IF_INITIAL_STATE(s, p)          \
2826   if (s == 0)                                           \
2827     {                                                   \
2828       while (inputwcs[p - buf_begin] == 0               \
2829             && mblen_buf[p - buf_begin] > 0             \
2830             && (unsigned char const *) p < buf_end)     \
2831         ++p;                                            \
2832       if ((char *) p >= end)                            \
2833         {                                               \
2834           free (mblen_buf);                             \
2835           free (inputwcs);                              \
2836           *end = saved_end;                             \
2837           return NULL;                                  \
2838         }                                               \
2839     }
2840
2841 static void
2842 realloc_trans_if_necessary (struct dfa *d, state_num new_state)
2843 {
2844   /* Make sure that the trans and fail arrays are allocated large enough
2845      to hold a pointer for the new state. */
2846   if (new_state >= d->tralloc)
2847     {
2848       state_num oldalloc = d->tralloc;
2849
2850       while (new_state >= d->tralloc)
2851         d->tralloc *= 2;
2852       REALLOC (d->realtrans, d->tralloc + 1);
2853       d->trans = d->realtrans + 1;
2854       REALLOC (d->fails, d->tralloc);
2855       REALLOC (d->success, d->tralloc);
2856       REALLOC (d->newlines, d->tralloc);
2857       while (oldalloc < d->tralloc)
2858         {
2859           d->trans[oldalloc] = NULL;
2860           d->fails[oldalloc++] = NULL;
2861         }
2862     }
2863 }
2864
2865 /* Return values of transit_state_singlebyte(), and
2866    transit_state_consume_1char.  */
2867 typedef enum
2868 {
2869   TRANSIT_STATE_IN_PROGRESS,    /* State transition has not finished.  */
2870   TRANSIT_STATE_DONE,           /* State transition has finished.  */
2871   TRANSIT_STATE_END_BUFFER      /* Reach the end of the buffer.  */
2872 } status_transit_state;
2873
2874 /* Consume a single byte and transit state from 's' to '*next_state'.
2875    This function is almost same as the state transition routin in dfaexec().
2876    But state transition is done just once, otherwise matching succeed or
2877    reach the end of the buffer.  */
2878 static status_transit_state
2879 transit_state_singlebyte (struct dfa *d, state_num s, unsigned char const *p,
2880                           state_num * next_state)
2881 {
2882   state_num *t;
2883   state_num works = s;
2884
2885   status_transit_state rval = TRANSIT_STATE_IN_PROGRESS;
2886
2887   while (rval == TRANSIT_STATE_IN_PROGRESS)
2888     {
2889       if ((t = d->trans[works]) != NULL)
2890         {
2891           works = t[*p];
2892           rval = TRANSIT_STATE_DONE;
2893           if (works < 0)
2894             works = 0;
2895         }
2896       else if (works < 0)
2897         {
2898           if (p == buf_end)
2899             {
2900               /* At the moment, it must not happen.  */
2901               abort ();
2902             }
2903           works = 0;
2904         }
2905       else if (d->fails[works])
2906         {
2907           works = d->fails[works][*p];
2908           rval = TRANSIT_STATE_DONE;
2909         }
2910       else
2911         {
2912           build_state (works, d);
2913         }
2914     }
2915   *next_state = works;
2916   return rval;
2917 }
2918
2919 /* Match a "." against the current context.  buf_begin[IDX] is the
2920    current position.  Return the length of the match, in bytes.
2921    POS is the position of the ".".  */
2922 static int
2923 match_anychar (struct dfa *d, state_num s, position pos, size_t idx)
2924 {
2925   int context;
2926   wchar_t wc;
2927   int mbclen;
2928
2929   wc = inputwcs[idx];
2930   mbclen = (mblen_buf[idx] == 0) ? 1 : mblen_buf[idx];
2931
2932   /* Check syntax bits.  */
2933   if (wc == (wchar_t) eolbyte)
2934     {
2935       if (!(syntax_bits & RE_DOT_NEWLINE))
2936         return 0;
2937     }
2938   else if (wc == (wchar_t) '\0')
2939     {
2940       if (syntax_bits & RE_DOT_NOT_NULL)
2941         return 0;
2942     }
2943
2944   context = wchar_context (wc);
2945   if (!SUCCEEDS_IN_CONTEXT (pos.constraint, d->states[s].context, context))
2946     return 0;
2947
2948   return mbclen;
2949 }
2950
2951 /* Match a bracket expression against the current context.
2952    buf_begin[IDX] is the current position.
2953    Return the length of the match, in bytes.
2954    POS is the position of the bracket expression.  */
2955 static int
2956 match_mb_charset (struct dfa *d, state_num s, position pos, size_t idx)
2957 {
2958   size_t i;
2959   int match;                    /* Flag which represent that matching succeed.  */
2960   int match_len;                /* Length of the character (or collating element)
2961                                    with which this operator match.  */
2962   int op_len;                   /* Length of the operator.  */
2963   char buffer[128];
2964
2965   /* Pointer to the structure to which we are currently referring.  */
2966   struct mb_char_classes *work_mbc;
2967
2968   int context;
2969   wchar_t wc;                   /* Current referring character.  */
2970
2971   wc = inputwcs[idx];
2972
2973   /* Check syntax bits.  */
2974   if (wc == (wchar_t) eolbyte)
2975     {
2976       if (!(syntax_bits & RE_DOT_NEWLINE))
2977         return 0;
2978     }
2979   else if (wc == (wchar_t) '\0')
2980     {
2981       if (syntax_bits & RE_DOT_NOT_NULL)
2982         return 0;
2983     }
2984
2985   context = wchar_context (wc);
2986   if (!SUCCEEDS_IN_CONTEXT (pos.constraint, d->states[s].context, context))
2987     return 0;
2988
2989   /* Assign the current referring operator to work_mbc.  */
2990   work_mbc = &(d->mbcsets[(d->multibyte_prop[pos.index]) >> 2]);
2991   match = !work_mbc->invert;
2992   match_len = (mblen_buf[idx] == 0) ? 1 : mblen_buf[idx];
2993
2994   /* Match in range 0-255?  */
2995   if (wc < NOTCHAR && work_mbc->cset != -1
2996       && tstbit ((unsigned char) wc, d->charclasses[work_mbc->cset]))
2997     goto charset_matched;
2998
2999   /* match with a character class?  */
3000   for (i = 0; i < work_mbc->nch_classes; i++)
3001     {
3002       if (iswctype ((wint_t) wc, work_mbc->ch_classes[i]))
3003         goto charset_matched;
3004     }
3005
3006   strncpy (buffer, (char const *) buf_begin + idx, match_len);
3007   buffer[match_len] = '\0';
3008
3009   /* match with an equivalence class?  */
3010   for (i = 0; i < work_mbc->nequivs; i++)
3011     {
3012       op_len = strlen (work_mbc->equivs[i]);
3013       strncpy (buffer, (char const *) buf_begin + idx, op_len);
3014       buffer[op_len] = '\0';
3015       if (strcoll (work_mbc->equivs[i], buffer) == 0)
3016         {
3017           match_len = op_len;
3018           goto charset_matched;
3019         }
3020     }
3021
3022   /* match with a collating element?  */
3023   for (i = 0; i < work_mbc->ncoll_elems; i++)
3024     {
3025       op_len = strlen (work_mbc->coll_elems[i]);
3026       strncpy (buffer, (char const *) buf_begin + idx, op_len);
3027       buffer[op_len] = '\0';
3028
3029       if (strcoll (work_mbc->coll_elems[i], buffer) == 0)
3030         {
3031           match_len = op_len;
3032           goto charset_matched;
3033         }
3034     }
3035
3036   /* match with a range?  */
3037   for (i = 0; i < work_mbc->nranges; i++)
3038     {
3039       if (work_mbc->range_sts[i] <= wc && wc <= work_mbc->range_ends[i])
3040         goto charset_matched;
3041     }
3042
3043   /* match with a character?  */
3044   for (i = 0; i < work_mbc->nchars; i++)
3045     {
3046       if (wc == work_mbc->chars[i])
3047         goto charset_matched;
3048     }
3049
3050   match = !match;
3051
3052 charset_matched:
3053   return match ? match_len : 0;
3054 }
3055
3056 /* Check each of 'd->states[s].mbps.elem' can match or not. Then return the
3057    array which corresponds to 'd->states[s].mbps.elem' and each element of
3058    the array contains the amount of the bytes with which the element can
3059    match.
3060    'idx' is the index from the buf_begin, and it is the current position
3061    in the buffer.
3062    Caller MUST free the array which this function return.  */
3063 static int *
3064 check_matching_with_multibyte_ops (struct dfa *d, state_num s, size_t idx)
3065 {
3066   size_t i;
3067   int *rarray;
3068
3069   MALLOC (rarray, d->states[s].mbps.nelem);
3070   for (i = 0; i < d->states[s].mbps.nelem; ++i)
3071     {
3072       position pos = d->states[s].mbps.elems[i];
3073       switch (d->tokens[pos.index])
3074         {
3075         case ANYCHAR:
3076           rarray[i] = match_anychar (d, s, pos, idx);
3077           break;
3078         case MBCSET:
3079           rarray[i] = match_mb_charset (d, s, pos, idx);
3080           break;
3081         default:
3082           break;                /* cannot happen.  */
3083         }
3084     }
3085   return rarray;
3086 }
3087
3088 /* Consume a single character and enumerate all of the positions which can
3089    be next position from the state 's'.
3090    'match_lens' is the input. It can be NULL, but it can also be the output
3091    of check_matching_with_multibyte_ops() for optimization.
3092    'mbclen' and 'pps' are the output.  'mbclen' is the length of the
3093    character consumed, and 'pps' is the set this function enumerate.  */
3094 static status_transit_state
3095 transit_state_consume_1char (struct dfa *d, state_num s,
3096                              unsigned char const **pp,
3097                              int *match_lens, int *mbclen, position_set * pps)
3098 {
3099   size_t i, j;
3100   int k;
3101   state_num s1, s2;
3102   int *work_mbls;
3103   status_transit_state rs = TRANSIT_STATE_DONE;
3104
3105   /* Calculate the length of the (single/multi byte) character
3106      to which p points.  */
3107   *mbclen = (mblen_buf[*pp - buf_begin] == 0) ? 1 : mblen_buf[*pp - buf_begin];
3108
3109   /* Calculate the state which can be reached from the state 's' by
3110      consuming '*mbclen' single bytes from the buffer.  */
3111   s1 = s;
3112   for (k = 0; k < *mbclen; k++)
3113     {
3114       s2 = s1;
3115       rs = transit_state_singlebyte (d, s2, (*pp)++, &s1);
3116     }
3117   /* Copy the positions contained by 's1' to the set 'pps'.  */
3118   copy (&(d->states[s1].elems), pps);
3119
3120   /* Check (input) match_lens, and initialize if it is NULL.  */
3121   if (match_lens == NULL && d->states[s].mbps.nelem != 0)
3122     work_mbls = check_matching_with_multibyte_ops (d, s, *pp - buf_begin);
3123   else
3124     work_mbls = match_lens;
3125
3126   /* Add all of the positions which can be reached from 's' by consuming
3127      a single character.  */
3128   for (i = 0; i < d->states[s].mbps.nelem; i++)
3129     {
3130       if (work_mbls[i] == *mbclen)
3131         for (j = 0; j < d->follows[d->states[s].mbps.elems[i].index].nelem;
3132              j++)
3133           insert (d->follows[d->states[s].mbps.elems[i].index].elems[j], pps);
3134     }
3135
3136   if (match_lens == NULL && work_mbls != NULL)
3137     free (work_mbls);
3138
3139   /* FIXME: this return value is always ignored.  */
3140   return rs;
3141 }
3142
3143 /* Transit state from s, then return new state and update the pointer of the
3144    buffer.  This function is for some operator which can match with a multi-
3145    byte character or a collating element (which may be multi characters).  */
3146 static state_num
3147 transit_state (struct dfa *d, state_num s, unsigned char const **pp)
3148 {
3149   state_num s1;
3150   int mbclen;                   /* The length of current input multibyte character. */
3151   int maxlen = 0;
3152   size_t i, j;
3153   int *match_lens = NULL;
3154   size_t nelem = d->states[s].mbps.nelem;       /* Just a alias.  */
3155   position_set follows;
3156   unsigned char const *p1 = *pp;
3157   wchar_t wc;
3158
3159   if (nelem > 0)
3160     /* This state has (a) multibyte operator(s).
3161        We check whether each of them can match or not.  */
3162     {
3163       /* Note: caller must free the return value of this function.  */
3164       match_lens = check_matching_with_multibyte_ops (d, s, *pp - buf_begin);
3165
3166       for (i = 0; i < nelem; i++)
3167         /* Search the operator which match the longest string,
3168            in this state.  */
3169         {
3170           if (match_lens[i] > maxlen)
3171             maxlen = match_lens[i];
3172         }
3173     }
3174
3175   if (nelem == 0 || maxlen == 0)
3176     /* This state has no multibyte operator which can match.
3177        We need to check only one single byte character.  */
3178     {
3179       status_transit_state rs;
3180       rs = transit_state_singlebyte (d, s, *pp, &s1);
3181
3182       /* We must update the pointer if state transition succeeded.  */
3183       if (rs == TRANSIT_STATE_DONE)
3184         ++*pp;
3185
3186       free (match_lens);
3187       return s1;
3188     }
3189
3190   /* This state has some operators which can match a multibyte character.  */
3191   alloc_position_set (&follows, d->nleaves);
3192
3193   /* 'maxlen' may be longer than the length of a character, because it may
3194      not be a character but a (multi character) collating element.
3195      We enumerate all of the positions which 's' can reach by consuming
3196      'maxlen' bytes.  */
3197   transit_state_consume_1char (d, s, pp, match_lens, &mbclen, &follows);
3198
3199   wc = inputwcs[*pp - mbclen - buf_begin];
3200   s1 = state_index (d, &follows, wchar_context (wc));
3201   realloc_trans_if_necessary (d, s1);
3202
3203   while (*pp - p1 < maxlen)
3204     {
3205       transit_state_consume_1char (d, s1, pp, NULL, &mbclen, &follows);
3206
3207       for (i = 0; i < nelem; i++)
3208         {
3209           if (match_lens[i] == *pp - p1)
3210             for (j = 0;
3211                  j < d->follows[d->states[s1].mbps.elems[i].index].nelem; j++)
3212               insert (d->follows[d->states[s1].mbps.elems[i].index].elems[j],
3213                       &follows);
3214         }
3215
3216       wc = inputwcs[*pp - mbclen - buf_begin];
3217       s1 = state_index (d, &follows, wchar_context (wc));
3218       realloc_trans_if_necessary (d, s1);
3219     }
3220   free (match_lens);
3221   free (follows.elems);
3222   return s1;
3223 }
3224
3225
3226 /* Initialize mblen_buf and inputwcs with data from the next line.  */
3227
3228 static void
3229 prepare_wc_buf (const char *begin, const char *end)
3230 {
3231 #if MBS_SUPPORT
3232   unsigned char eol = eolbyte;
3233   size_t remain_bytes, i;
3234
3235   buf_begin = (unsigned char *) begin;
3236
3237   remain_bytes = 0;
3238   for (i = 0; i < end - begin + 1; i++)
3239     {
3240       if (remain_bytes == 0)
3241         {
3242           remain_bytes
3243             = mbrtowc (inputwcs + i, begin + i, end - begin - i + 1, &mbs);
3244           if (remain_bytes < 1
3245               || remain_bytes == (size_t) -1
3246               || remain_bytes == (size_t) -2
3247               || (remain_bytes == 1 && inputwcs[i] == (wchar_t) begin[i]))
3248             {
3249               remain_bytes = 0;
3250               inputwcs[i] = (wchar_t) begin[i];
3251               mblen_buf[i] = 0;
3252               if (begin[i] == eol)
3253                 break;
3254             }
3255           else
3256             {
3257               mblen_buf[i] = remain_bytes;
3258               remain_bytes--;
3259             }
3260         }
3261       else
3262         {
3263           mblen_buf[i] = remain_bytes;
3264           inputwcs[i] = 0;
3265           remain_bytes--;
3266         }
3267     }
3268
3269   buf_end = (unsigned char *) (begin + i);
3270   mblen_buf[i] = 0;
3271   inputwcs[i] = 0;              /* sentinel */
3272 #endif /* MBS_SUPPORT */
3273 }
3274
3275 /* Search through a buffer looking for a match to the given struct dfa.
3276    Find the first occurrence of a string matching the regexp in the
3277    buffer, and the shortest possible version thereof.  Return a pointer to
3278    the first character after the match, or NULL if none is found.  BEGIN
3279    points to the beginning of the buffer, and END points to the first byte
3280    after its end.  Note however that we store a sentinel byte (usually
3281    newline) in *END, so the actual buffer must be one byte longer.
3282    When ALLOW_NL is nonzero, newlines may appear in the matching string.
3283    If COUNT is non-NULL, increment *COUNT once for each newline processed.
3284    Finally, if BACKREF is non-NULL set *BACKREF to indicate whether we
3285    encountered a back-reference (1) or not (0).  The caller may use this
3286    to decide whether to fall back on a backtracking matcher. */
3287 char *
3288 dfaexec (struct dfa *d, char const *begin, char *end,
3289          int allow_nl, size_t *count, int *backref)
3290 {
3291   state_num s, s1;              /* Current state. */
3292   unsigned char const *p;       /* Current input character. */
3293   state_num **trans, *t;        /* Copy of d->trans so it can be optimized
3294                                    into a register. */
3295   unsigned char eol = eolbyte;  /* Likewise for eolbyte.  */
3296   unsigned char saved_end;
3297
3298   if (!d->tralloc)
3299     build_state_zero (d);
3300
3301   s = s1 = 0;
3302   p = (unsigned char const *) begin;
3303   trans = d->trans;
3304   saved_end = *(unsigned char *) end;
3305   *end = eol;
3306
3307   if (d->mb_cur_max > 1)
3308     {
3309       MALLOC (mblen_buf, end - begin + 2);
3310       MALLOC (inputwcs, end - begin + 2);
3311       memset (&mbs, 0, sizeof (mbstate_t));
3312       prepare_wc_buf ((const char *) p, end);
3313     }
3314
3315   for (;;)
3316     {
3317       if (d->mb_cur_max > 1)
3318         while ((t = trans[s]) != NULL)
3319           {
3320             if (p > buf_end)
3321               break;
3322             s1 = s;
3323             SKIP_REMAINS_MB_IF_INITIAL_STATE (s, p);
3324
3325             if (d->states[s].mbps.nelem == 0)
3326               {
3327                 s = t[*p++];
3328                 continue;
3329               }
3330
3331             /* Falling back to the glibc matcher in this case gives
3332                better performance (up to 25% better on [a-z], for
3333                example) and enables support for collating symbols and
3334                equivalence classes.  */
3335             if (backref)
3336               {
3337                 *backref = 1;
3338                 free (mblen_buf);
3339                 free (inputwcs);
3340                 *end = saved_end;
3341                 return (char *) p;
3342               }
3343
3344             /* Can match with a multibyte character (and multi character
3345                collating element).  Transition table might be updated.  */
3346             s = transit_state (d, s, &p);
3347             trans = d->trans;
3348           }
3349       else
3350         {
3351           while ((t = trans[s]) != NULL)
3352             {
3353               s1 = t[*p++];
3354               if ((t = trans[s1]) == NULL)
3355                 {
3356                   state_num tmp = s;
3357                   s = s1;
3358                   s1 = tmp;     /* swap */
3359                   break;
3360                 }
3361               s = t[*p++];
3362             }
3363         }
3364
3365       if (s >= 0 && (char *) p <= end && d->fails[s])
3366         {
3367           if (d->success[s] & sbit[*p])
3368             {
3369               if (backref)
3370                 *backref = (d->states[s].backref != 0);
3371               if (d->mb_cur_max > 1)
3372                 {
3373                   free (mblen_buf);
3374                   free (inputwcs);
3375                 }
3376               *end = saved_end;
3377               return (char *) p;
3378             }
3379
3380           s1 = s;
3381           if (d->mb_cur_max > 1)
3382             {
3383               /* Can match with a multibyte character (and multicharacter
3384                  collating element).  Transition table might be updated.  */
3385               s = transit_state (d, s, &p);
3386               trans = d->trans;
3387             }
3388           else
3389             s = d->fails[s][*p++];
3390           continue;
3391         }
3392
3393       /* If the previous character was a newline, count it. */
3394       if ((char *) p <= end && p[-1] == eol)
3395         {
3396           if (count)
3397             ++*count;
3398
3399           if (d->mb_cur_max > 1)
3400             prepare_wc_buf ((const char *) p, end);
3401         }
3402
3403       /* Check if we've run off the end of the buffer. */
3404       if ((char *) p > end)
3405         {
3406           if (d->mb_cur_max > 1)
3407             {
3408               free (mblen_buf);
3409               free (inputwcs);
3410             }
3411           *end = saved_end;
3412           return NULL;
3413         }
3414
3415       if (s >= 0)
3416         {
3417           build_state (s, d);
3418           trans = d->trans;
3419           continue;
3420         }
3421
3422       if (p[-1] == eol && allow_nl)
3423         {
3424           s = d->newlines[s1];
3425           continue;
3426         }
3427
3428       s = 0;
3429     }
3430 }
3431
3432 static void
3433 free_mbdata (struct dfa *d)
3434 {
3435   size_t i;
3436
3437   free (d->multibyte_prop);
3438   d->multibyte_prop = NULL;
3439
3440   for (i = 0; i < d->nmbcsets; ++i)
3441     {
3442       size_t j;
3443       struct mb_char_classes *p = &(d->mbcsets[i]);
3444       free (p->chars);
3445       free (p->ch_classes);
3446       free (p->range_sts);
3447       free (p->range_ends);
3448
3449       for (j = 0; j < p->nequivs; ++j)
3450         free (p->equivs[j]);
3451       free (p->equivs);
3452
3453       for (j = 0; j < p->ncoll_elems; ++j)
3454         free (p->coll_elems[j]);
3455       free (p->coll_elems);
3456     }
3457
3458   free (d->mbcsets);
3459   d->mbcsets = NULL;
3460   d->nmbcsets = 0;
3461 }
3462
3463 /* Initialize the components of a dfa that the other routines don't
3464    initialize for themselves. */
3465 void
3466 dfainit (struct dfa *d)
3467 {
3468   memset (d, 0, sizeof *d);
3469
3470   d->calloc = 1;
3471   MALLOC (d->charclasses, d->calloc);
3472
3473   d->talloc = 1;
3474   MALLOC (d->tokens, d->talloc);
3475
3476   d->mb_cur_max = MB_CUR_MAX;
3477
3478   if (d->mb_cur_max > 1)
3479     {
3480       d->nmultibyte_prop = 1;
3481       MALLOC (d->multibyte_prop, d->nmultibyte_prop);
3482       d->mbcsets_alloc = 1;
3483       MALLOC (d->mbcsets, d->mbcsets_alloc);
3484     }
3485 }
3486
3487 static void
3488 dfaoptimize (struct dfa *d)
3489 {
3490   size_t i;
3491
3492   if (!MBS_SUPPORT || !using_utf8 ())
3493     return;
3494
3495   for (i = 0; i < d->tindex; ++i)
3496     {
3497       switch (d->tokens[i])
3498         {
3499         case ANYCHAR:
3500           /* Lowered.  */
3501           abort ();
3502         case MBCSET:
3503           /* Requires multi-byte algorithm.  */
3504           return;
3505         default:
3506           break;
3507         }
3508     }
3509
3510   free_mbdata (d);
3511   d->mb_cur_max = 1;
3512 }
3513
3514 /* Parse and analyze a single string of the given length. */
3515 void
3516 dfacomp (char const *s, size_t len, struct dfa *d, int searchflag)
3517 {
3518   dfainit (d);
3519   dfaparse (s, len, d);
3520   dfamust (d);
3521   dfaoptimize (d);
3522   dfaanalyze (d, searchflag);
3523 }
3524
3525 /* Free the storage held by the components of a dfa. */
3526 void
3527 dfafree (struct dfa *d)
3528 {
3529   size_t i;
3530   struct dfamust *dm, *ndm;
3531
3532   free (d->charclasses);
3533   free (d->tokens);
3534
3535   if (d->mb_cur_max > 1)
3536     free_mbdata (d);
3537
3538   for (i = 0; i < d->sindex; ++i)
3539     {
3540       free (d->states[i].elems.elems);
3541       if (MBS_SUPPORT)
3542         free (d->states[i].mbps.elems);
3543     }
3544   free (d->states);
3545   for (i = 0; i < d->tindex; ++i)
3546     free (d->follows[i].elems);
3547   free (d->follows);
3548   for (i = 0; i < d->tralloc; ++i)
3549     {
3550       free (d->trans[i]);
3551       free (d->fails[i]);
3552     }
3553   free (d->realtrans);
3554   free (d->fails);
3555   free (d->newlines);
3556   free (d->success);
3557   for (dm = d->musts; dm; dm = ndm)
3558     {
3559       ndm = dm->next;
3560       free (dm->must);
3561       free (dm);
3562     }
3563 }
3564
3565 /* Having found the postfix representation of the regular expression,
3566    try to find a long sequence of characters that must appear in any line
3567    containing the r.e.
3568    Finding a "longest" sequence is beyond the scope here;
3569    we take an easy way out and hope for the best.
3570    (Take "(ab|a)b"--please.)
3571
3572    We do a bottom-up calculation of sequences of characters that must appear
3573    in matches of r.e.'s represented by trees rooted at the nodes of the postfix
3574    representation:
3575         sequences that must appear at the left of the match ("left")
3576         sequences that must appear at the right of the match ("right")
3577         lists of sequences that must appear somewhere in the match ("in")
3578         sequences that must constitute the match ("is")
3579
3580    When we get to the root of the tree, we use one of the longest of its
3581    calculated "in" sequences as our answer.  The sequence we find is returned in
3582    d->must (where "d" is the single argument passed to "dfamust");
3583    the length of the sequence is returned in d->mustn.
3584
3585    The sequences calculated for the various types of node (in pseudo ANSI c)
3586    are shown below.  "p" is the operand of unary operators (and the left-hand
3587    operand of binary operators); "q" is the right-hand operand of binary
3588    operators.
3589
3590    "ZERO" means "a zero-length sequence" below.
3591
3592         Type    left            right           is              in
3593         ----    ----            -----           --              --
3594         char c  # c             # c             # c             # c
3595
3596         ANYCHAR ZERO            ZERO            ZERO            ZERO
3597
3598         MBCSET  ZERO            ZERO            ZERO            ZERO
3599
3600         CSET    ZERO            ZERO            ZERO            ZERO
3601
3602         STAR    ZERO            ZERO            ZERO            ZERO
3603
3604         QMARK   ZERO            ZERO            ZERO            ZERO
3605
3606         PLUS    p->left         p->right        ZERO            p->in
3607
3608         CAT     (p->is==ZERO)?  (q->is==ZERO)?  (p->is!=ZERO && p->in plus
3609                 p->left :       q->right :      q->is!=ZERO) ?  q->in plus
3610                 p->is##q->left  p->right##q->is p->is##q->is :  p->right##q->left
3611                                                 ZERO
3612
3613         OR      longest common  longest common  (do p->is and   substrings common to
3614                 leading         trailing        q->is have same p->in and q->in
3615                 (sub)sequence   (sub)sequence   length and
3616                 of p->left      of p->right     content) ?
3617                 and q->left     and q->right    p->is : NULL
3618
3619    If there's anything else we recognize in the tree, all four sequences get set
3620    to zero-length sequences.  If there's something we don't recognize in the tree,
3621    we just return a zero-length sequence.
3622
3623    Break ties in favor of infrequent letters (choosing 'zzz' in preference to
3624    'aaa')?
3625
3626    And. . .is it here or someplace that we might ponder "optimizations" such as
3627         egrep 'psi|epsilon'     ->      egrep 'psi'
3628         egrep 'pepsi|epsilon'   ->      egrep 'epsi'
3629                                         (Yes, we now find "epsi" as a "string
3630                                         that must occur", but we might also
3631                                         simplify the *entire* r.e. being sought)
3632         grep '[c]'              ->      grep 'c'
3633         grep '(ab|a)b'          ->      grep 'ab'
3634         grep 'ab*'              ->      grep 'a'
3635         grep 'a*b'              ->      grep 'b'
3636
3637    There are several issues:
3638
3639    Is optimization easy (enough)?
3640
3641    Does optimization actually accomplish anything,
3642    or is the automaton you get from "psi|epsilon" (for example)
3643    the same as the one you get from "psi" (for example)?
3644
3645    Are optimizable r.e.'s likely to be used in real-life situations
3646    (something like 'ab*' is probably unlikely; something like is
3647    'psi|epsilon' is likelier)? */
3648
3649 static char *
3650 icatalloc (char *old, char const *new)
3651 {
3652   char *result;
3653   size_t oldsize = old == NULL ? 0 : strlen (old);
3654   size_t newsize = new == NULL ? 0 : strlen (new);
3655   if (newsize == 0)
3656     return old;
3657   result = xrealloc (old, oldsize + newsize + 1);
3658   memcpy (result + oldsize, new, newsize + 1);
3659   return result;
3660 }
3661
3662 static char *
3663 icpyalloc (char const *string)
3664 {
3665   return icatalloc (NULL, string);
3666 }
3667
3668 static char *_GL_ATTRIBUTE_PURE
3669 istrstr (char const *lookin, char const *lookfor)
3670 {
3671   char const *cp;
3672   size_t len;
3673
3674   len = strlen (lookfor);
3675   for (cp = lookin; *cp != '\0'; ++cp)
3676     if (strncmp (cp, lookfor, len) == 0)
3677       return (char *) cp;
3678   return NULL;
3679 }
3680
3681 static void
3682 freelist (char **cpp)
3683 {
3684   size_t i;
3685
3686   if (cpp == NULL)
3687     return;
3688   for (i = 0; cpp[i] != NULL; ++i)
3689     {
3690       free (cpp[i]);
3691       cpp[i] = NULL;
3692     }
3693 }
3694
3695 static char **
3696 enlist (char **cpp, char *new, size_t len)
3697 {
3698   size_t i, j;
3699
3700   if (cpp == NULL)
3701     return NULL;
3702   if ((new = icpyalloc (new)) == NULL)
3703     {
3704       freelist (cpp);
3705       return NULL;
3706     }
3707   new[len] = '\0';
3708   /* Is there already something in the list that's new (or longer)? */
3709   for (i = 0; cpp[i] != NULL; ++i)
3710     if (istrstr (cpp[i], new) != NULL)
3711       {
3712         free (new);
3713         return cpp;
3714       }
3715   /* Eliminate any obsoleted strings. */
3716   j = 0;
3717   while (cpp[j] != NULL)
3718     if (istrstr (new, cpp[j]) == NULL)
3719       ++j;
3720     else
3721       {
3722         free (cpp[j]);
3723         if (--i == j)
3724           break;
3725         cpp[j] = cpp[i];
3726         cpp[i] = NULL;
3727       }
3728   /* Add the new string. */
3729   REALLOC (cpp, i + 2);
3730   cpp[i] = new;
3731   cpp[i + 1] = NULL;
3732   return cpp;
3733 }
3734
3735 /* Given pointers to two strings, return a pointer to an allocated
3736    list of their distinct common substrings. Return NULL if something
3737    seems wild. */
3738 static char **
3739 comsubs (char *left, char const *right)
3740 {
3741   char **cpp;
3742   char *lcp;
3743   char *rcp;
3744   size_t i, len;
3745
3746   if (left == NULL || right == NULL)
3747     return NULL;
3748   cpp = malloc (sizeof *cpp);
3749   if (cpp == NULL)
3750     return NULL;
3751   cpp[0] = NULL;
3752   for (lcp = left; *lcp != '\0'; ++lcp)
3753     {
3754       len = 0;
3755       rcp = strchr (right, *lcp);
3756       while (rcp != NULL)
3757         {
3758           for (i = 1; lcp[i] != '\0' && lcp[i] == rcp[i]; ++i)
3759             continue;
3760           if (i > len)
3761             len = i;
3762           rcp = strchr (rcp + 1, *lcp);
3763         }
3764       if (len == 0)
3765         continue;
3766       {
3767         char **p = enlist (cpp, lcp, len);
3768         if (p == NULL)
3769           {
3770             freelist (cpp);
3771             cpp = NULL;
3772             break;
3773           }
3774         cpp = p;
3775       }
3776     }
3777   return cpp;
3778 }
3779
3780 static char **
3781 addlists (char **old, char **new)
3782 {
3783   size_t i;
3784
3785   if (old == NULL || new == NULL)
3786     return NULL;
3787   for (i = 0; new[i] != NULL; ++i)
3788     {
3789       old = enlist (old, new[i], strlen (new[i]));
3790       if (old == NULL)
3791         break;
3792     }
3793   return old;
3794 }
3795
3796 /* Given two lists of substrings, return a new list giving substrings
3797    common to both. */
3798 static char **
3799 inboth (char **left, char **right)
3800 {
3801   char **both;
3802   char **temp;
3803   size_t lnum, rnum;
3804
3805   if (left == NULL || right == NULL)
3806     return NULL;
3807   both = malloc (sizeof *both);
3808   if (both == NULL)
3809     return NULL;
3810   both[0] = NULL;
3811   for (lnum = 0; left[lnum] != NULL; ++lnum)
3812     {
3813       for (rnum = 0; right[rnum] != NULL; ++rnum)
3814         {
3815           temp = comsubs (left[lnum], right[rnum]);
3816           if (temp == NULL)
3817             {
3818               freelist (both);
3819               return NULL;
3820             }
3821           both = addlists (both, temp);
3822           freelist (temp);
3823           free (temp);
3824           if (both == NULL)
3825             return NULL;
3826         }
3827     }
3828   return both;
3829 }
3830
3831 typedef struct
3832 {
3833   char **in;
3834   char *left;
3835   char *right;
3836   char *is;
3837 } must;
3838
3839 static void
3840 resetmust (must * mp)
3841 {
3842   mp->left[0] = mp->right[0] = mp->is[0] = '\0';
3843   freelist (mp->in);
3844 }
3845
3846 static void
3847 dfamust (struct dfa *d)
3848 {
3849   must *musts;
3850   must *mp;
3851   char *result;
3852   size_t ri;
3853   size_t i;
3854   int exact;
3855   token t;
3856   static must must0;
3857   struct dfamust *dm;
3858   static char empty_string[] = "";
3859
3860   result = empty_string;
3861   exact = 0;
3862   MALLOC (musts, d->tindex + 1);
3863   mp = musts;
3864   for (i = 0; i <= d->tindex; ++i)
3865     mp[i] = must0;
3866   for (i = 0; i <= d->tindex; ++i)
3867     {
3868       mp[i].in = xmalloc (sizeof *mp[i].in);
3869       mp[i].left = xmalloc (2);
3870       mp[i].right = xmalloc (2);
3871       mp[i].is = xmalloc (2);
3872       mp[i].left[0] = mp[i].right[0] = mp[i].is[0] = '\0';
3873       mp[i].in[0] = NULL;
3874     }
3875 #ifdef DEBUG
3876   fprintf (stderr, "dfamust:\n");
3877   for (i = 0; i < d->tindex; ++i)
3878     {
3879       fprintf (stderr, " %zd:", i);
3880       prtok (d->tokens[i]);
3881     }
3882   putc ('\n', stderr);
3883 #endif
3884   for (ri = 0; ri < d->tindex; ++ri)
3885     {
3886       switch (t = d->tokens[ri])
3887         {
3888         case LPAREN:
3889         case RPAREN:
3890           assert (!"neither LPAREN nor RPAREN may appear here");
3891         case EMPTY:
3892         case BEGLINE:
3893         case ENDLINE:
3894         case BEGWORD:
3895         case ENDWORD:
3896         case LIMWORD:
3897         case NOTLIMWORD:
3898         case BACKREF:
3899           resetmust (mp);
3900           break;
3901         case STAR:
3902         case QMARK:
3903           assert (musts < mp);
3904           --mp;
3905           resetmust (mp);
3906           break;
3907         case OR:
3908           assert (&musts[2] <= mp);
3909           {
3910             char **new;
3911             must *lmp;
3912             must *rmp;
3913             size_t j, ln, rn, n;
3914
3915             rmp = --mp;
3916             lmp = --mp;
3917             /* Guaranteed to be.  Unlikely, but. . . */
3918             if (!STREQ (lmp->is, rmp->is))
3919               lmp->is[0] = '\0';
3920             /* Left side--easy */
3921             i = 0;
3922             while (lmp->left[i] != '\0' && lmp->left[i] == rmp->left[i])
3923               ++i;
3924             lmp->left[i] = '\0';
3925             /* Right side */
3926             ln = strlen (lmp->right);
3927             rn = strlen (rmp->right);
3928             n = ln;
3929             if (n > rn)
3930               n = rn;
3931             for (i = 0; i < n; ++i)
3932               if (lmp->right[ln - i - 1] != rmp->right[rn - i - 1])
3933                 break;
3934             for (j = 0; j < i; ++j)
3935               lmp->right[j] = lmp->right[(ln - i) + j];
3936             lmp->right[j] = '\0';
3937             new = inboth (lmp->in, rmp->in);
3938             if (new == NULL)
3939               goto done;
3940             freelist (lmp->in);
3941             free (lmp->in);
3942             lmp->in = new;
3943           }
3944           break;
3945         case PLUS:
3946           assert (musts < mp);
3947           --mp;
3948           mp->is[0] = '\0';
3949           break;
3950         case END:
3951           assert (mp == &musts[1]);
3952           for (i = 0; musts[0].in[i] != NULL; ++i)
3953             if (strlen (musts[0].in[i]) > strlen (result))
3954               result = musts[0].in[i];
3955           if (STREQ (result, musts[0].is))
3956             exact = 1;
3957           goto done;
3958         case CAT:
3959           assert (&musts[2] <= mp);
3960           {
3961             must *lmp;
3962             must *rmp;
3963
3964             rmp = --mp;
3965             lmp = --mp;
3966             /* In.  Everything in left, plus everything in
3967                right, plus concatenation of
3968                left's right and right's left. */
3969             lmp->in = addlists (lmp->in, rmp->in);
3970             if (lmp->in == NULL)
3971               goto done;
3972             if (lmp->right[0] != '\0' && rmp->left[0] != '\0')
3973               {
3974                 char *tp;
3975
3976                 tp = icpyalloc (lmp->right);
3977                 tp = icatalloc (tp, rmp->left);
3978                 lmp->in = enlist (lmp->in, tp, strlen (tp));
3979                 free (tp);
3980                 if (lmp->in == NULL)
3981                   goto done;
3982               }
3983             /* Left-hand */
3984             if (lmp->is[0] != '\0')
3985               {
3986                 lmp->left = icatalloc (lmp->left, rmp->left);
3987                 if (lmp->left == NULL)
3988                   goto done;
3989               }
3990             /* Right-hand */
3991             if (rmp->is[0] == '\0')
3992               lmp->right[0] = '\0';
3993             lmp->right = icatalloc (lmp->right, rmp->right);
3994             if (lmp->right == NULL)
3995               goto done;
3996             /* Guaranteed to be */
3997             if (lmp->is[0] != '\0' && rmp->is[0] != '\0')
3998               {
3999                 lmp->is = icatalloc (lmp->is, rmp->is);
4000                 if (lmp->is == NULL)
4001                   goto done;
4002               }
4003             else
4004               lmp->is[0] = '\0';
4005           }
4006           break;
4007         default:
4008           if (t < END)
4009             {
4010               assert (!"oops! t >= END");
4011             }
4012           else if (t == '\0')
4013             {
4014               /* not on *my* shift */
4015               goto done;
4016             }
4017           else if (t >= CSET || !MBS_SUPPORT || t == ANYCHAR || t == MBCSET)
4018             {
4019               /* easy enough */
4020               resetmust (mp);
4021             }
4022           else
4023             {
4024               /* plain character */
4025               resetmust (mp);
4026               mp->is[0] = mp->left[0] = mp->right[0] = t;
4027               mp->is[1] = mp->left[1] = mp->right[1] = '\0';
4028               mp->in = enlist (mp->in, mp->is, (size_t) 1);
4029               if (mp->in == NULL)
4030                 goto done;
4031             }
4032           break;
4033         }
4034 #ifdef DEBUG
4035       fprintf (stderr, " node: %zd:", ri);
4036       prtok (d->tokens[ri]);
4037       fprintf (stderr, "\n  in:");
4038       for (i = 0; mp->in[i]; ++i)
4039         fprintf (stderr, " \"%s\"", mp->in[i]);
4040       fprintf (stderr, "\n  is: \"%s\"\n", mp->is);
4041       fprintf (stderr, "  left: \"%s\"\n", mp->left);
4042       fprintf (stderr, "  right: \"%s\"\n", mp->right);
4043 #endif
4044       ++mp;
4045     }
4046 done:
4047   if (strlen (result))
4048     {
4049       MALLOC (dm, 1);
4050       dm->exact = exact;
4051       dm->must = xmemdup (result, strlen (result) + 1);
4052       dm->next = d->musts;
4053       d->musts = dm;
4054     }
4055   mp = musts;
4056   for (i = 0; i <= d->tindex; ++i)
4057     {
4058       freelist (mp[i].in);
4059       free (mp[i].in);
4060       free (mp[i].left);
4061       free (mp[i].right);
4062       free (mp[i].is);
4063     }
4064   free (mp);
4065 }
4066
4067 struct dfa *
4068 dfaalloc (void)
4069 {
4070   return xmalloc (sizeof (struct dfa));
4071 }
4072
4073 struct dfamust *_GL_ATTRIBUTE_PURE
4074 dfamusts (struct dfa const *d)
4075 {
4076   return d->musts;
4077 }
4078
4079 /* vim:set shiftwidth=2: */