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