f2d8231dc81f6e4d31d8998673e2c9b5c77e8775
[debian/tar] / gnu / regex_internal.h
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Extended regular expression matching and search library.
4    Copyright (C) 2002-2013 Free Software Foundation, Inc.
5    This file is part of the GNU C Library.
6    Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
7
8    The GNU C Library is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public
10    License as published by the Free Software Foundation; either
11    version 3 of the License, or (at your option) any later version.
12
13    The GNU C Library is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public
19    License along with the GNU C Library; if not, see
20    <http://www.gnu.org/licenses/>.  */
21
22 #ifndef _REGEX_INTERNAL_H
23 #define _REGEX_INTERNAL_H 1
24
25 #include <assert.h>
26 #include <ctype.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <langinfo.h>
32 #include <locale.h>
33 #include <wchar.h>
34 #include <wctype.h>
35 #include <stdbool.h>
36 #include <stdint.h>
37
38 #ifdef _LIBC
39 # include <bits/libc-lock.h>
40 # define lock_define(name) __libc_lock_define (, name)
41 # define lock_init(lock) (__libc_lock_init (lock), 0)
42 # define lock_fini(lock) 0
43 # define lock_lock(lock) __libc_lock_lock (lock)
44 # define lock_unlock(lock) __libc_lock_unlock (lock)
45 #elif defined GNULIB_LOCK
46 # include "glthread/lock.h"
47   /* Use gl_lock_define if empty macro arguments are known to work.
48      Otherwise, fall back on less-portable substitutes.  */
49 # if ((defined __GNUC__ && !defined __STRICT_ANSI__) \
50       || (defined __STDC_VERSION__ && 199901L <= __STDC_VERSION__))
51 #  define lock_define(name) gl_lock_define (, name)
52 # elif USE_POSIX_THREADS
53 #  define lock_define(name) pthread_mutex_t name;
54 # elif USE_PTH_THREADS
55 #  define lock_define(name) pth_mutex_t name;
56 # elif USE_SOLARIS_THREADS
57 #  define lock_define(name) mutex_t name;
58 # elif USE_WINDOWS_THREADS
59 #  define lock_define(name) gl_lock_t name;
60 # else
61 #  define lock_define(name)
62 # endif
63 # define lock_init(lock) glthread_lock_init (&(lock))
64 # define lock_fini(lock) glthread_lock_destroy (&(lock))
65 # define lock_lock(lock) glthread_lock_lock (&(lock))
66 # define lock_unlock(lock) glthread_lock_unlock (&(lock))
67 #elif defined GNULIB_PTHREAD
68 # include <pthread.h>
69 # define lock_define(name) pthread_mutex_t name;
70 # define lock_init(lock) pthread_mutex_init (&(lock), 0)
71 # define lock_fini(lock) pthread_mutex_destroy (&(lock))
72 # define lock_lock(lock) pthread_mutex_lock (&(lock))
73 # define lock_unlock(lock) pthread_mutex_unlock (&(lock))
74 #else
75 # define lock_define(name)
76 # define lock_init(lock) 0
77 # define lock_fini(lock) 0
78 # define lock_lock(lock) ((void) 0)
79 # define lock_unlock(lock) ((void) 0)
80 #endif
81
82 /* In case that the system doesn't have isblank().  */
83 #if !defined _LIBC && ! (defined isblank || (HAVE_ISBLANK && HAVE_DECL_ISBLANK))
84 # define isblank(ch) ((ch) == ' ' || (ch) == '\t')
85 #endif
86
87 #ifdef _LIBC
88 # ifndef _RE_DEFINE_LOCALE_FUNCTIONS
89 #  define _RE_DEFINE_LOCALE_FUNCTIONS 1
90 #   include <locale/localeinfo.h>
91 #   include <locale/elem-hash.h>
92 #   include <locale/coll-lookup.h>
93 # endif
94 #endif
95
96 /* This is for other GNU distributions with internationalized messages.  */
97 #if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
98 # include <libintl.h>
99 # ifdef _LIBC
100 #  undef gettext
101 #  define gettext(msgid) \
102   __dcgettext (_libc_intl_domainname, msgid, LC_MESSAGES)
103 # endif
104 #else
105 # define gettext(msgid) (msgid)
106 #endif
107
108 #ifndef gettext_noop
109 /* This define is so xgettext can find the internationalizable
110    strings.  */
111 # define gettext_noop(String) String
112 #endif
113
114 #if (defined MB_CUR_MAX && HAVE_WCTYPE_H && HAVE_ISWCTYPE && HAVE_WCSCOLL) || _LIBC
115 # define RE_ENABLE_I18N
116 #endif
117
118 #if __GNUC__ >= 3
119 # define BE(expr, val) __builtin_expect (expr, val)
120 #else
121 # define BE(expr, val) (expr)
122 #endif
123
124 /* Number of ASCII characters.  */
125 #define ASCII_CHARS 0x80
126
127 /* Number of single byte characters.  */
128 #define SBC_MAX (UCHAR_MAX + 1)
129
130 #define COLL_ELEM_LEN_MAX 8
131
132 /* The character which represents newline.  */
133 #define NEWLINE_CHAR '\n'
134 #define WIDE_NEWLINE_CHAR L'\n'
135
136 /* Rename to standard API for using out of glibc.  */
137 #ifndef _LIBC
138 # undef __wctype
139 # undef __iswctype
140 # define __wctype wctype
141 # define __iswctype iswctype
142 # define __btowc btowc
143 # define __mbrtowc mbrtowc
144 # define __wcrtomb wcrtomb
145 # define __regfree regfree
146 # define attribute_hidden
147 #endif /* not _LIBC */
148
149 #if __GNUC__ < 3 + (__GNUC_MINOR__ < 1)
150 # define __attribute__(arg)
151 #endif
152
153 typedef __re_idx_t Idx;
154 #ifdef _REGEX_LARGE_OFFSETS
155 # define IDX_MAX (SIZE_MAX - 2)
156 #else
157 # define IDX_MAX INT_MAX
158 #endif
159
160 /* Special return value for failure to match.  */
161 #define REG_MISSING ((Idx) -1)
162
163 /* Special return value for internal error.  */
164 #define REG_ERROR ((Idx) -2)
165
166 /* Test whether N is a valid index, and is not one of the above.  */
167 #ifdef _REGEX_LARGE_OFFSETS
168 # define REG_VALID_INDEX(n) ((Idx) (n) < REG_ERROR)
169 #else
170 # define REG_VALID_INDEX(n) (0 <= (n))
171 #endif
172
173 /* Test whether N is a valid nonzero index.  */
174 #ifdef _REGEX_LARGE_OFFSETS
175 # define REG_VALID_NONZERO_INDEX(n) ((Idx) ((n) - 1) < (Idx) (REG_ERROR - 1))
176 #else
177 # define REG_VALID_NONZERO_INDEX(n) (0 < (n))
178 #endif
179
180 /* A hash value, suitable for computing hash tables.  */
181 typedef __re_size_t re_hashval_t;
182
183 /* An integer used to represent a set of bits.  It must be unsigned,
184    and must be at least as wide as unsigned int.  */
185 typedef unsigned long int bitset_word_t;
186 /* All bits set in a bitset_word_t.  */
187 #define BITSET_WORD_MAX ULONG_MAX
188
189 /* Number of bits in a bitset_word_t.  For portability to hosts with
190    padding bits, do not use '(sizeof (bitset_word_t) * CHAR_BIT)';
191    instead, deduce it directly from BITSET_WORD_MAX.  Avoid
192    greater-than-32-bit integers and unconditional shifts by more than
193    31 bits, as they're not portable.  */
194 #if BITSET_WORD_MAX == 0xffffffffUL
195 # define BITSET_WORD_BITS 32
196 #elif BITSET_WORD_MAX >> 31 >> 4 == 1
197 # define BITSET_WORD_BITS 36
198 #elif BITSET_WORD_MAX >> 31 >> 16 == 1
199 # define BITSET_WORD_BITS 48
200 #elif BITSET_WORD_MAX >> 31 >> 28 == 1
201 # define BITSET_WORD_BITS 60
202 #elif BITSET_WORD_MAX >> 31 >> 31 >> 1 == 1
203 # define BITSET_WORD_BITS 64
204 #elif BITSET_WORD_MAX >> 31 >> 31 >> 9 == 1
205 # define BITSET_WORD_BITS 72
206 #elif BITSET_WORD_MAX >> 31 >> 31 >> 31 >> 31 >> 3 == 1
207 # define BITSET_WORD_BITS 128
208 #elif BITSET_WORD_MAX >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 7 == 1
209 # define BITSET_WORD_BITS 256
210 #elif BITSET_WORD_MAX >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 7 > 1
211 # define BITSET_WORD_BITS 257 /* any value > SBC_MAX will do here */
212 # if BITSET_WORD_BITS <= SBC_MAX
213 #  error "Invalid SBC_MAX"
214 # endif
215 #else
216 # error "Add case for new bitset_word_t size"
217 #endif
218
219 /* Number of bitset_word_t values in a bitset_t.  */
220 #define BITSET_WORDS ((SBC_MAX + BITSET_WORD_BITS - 1) / BITSET_WORD_BITS)
221
222 typedef bitset_word_t bitset_t[BITSET_WORDS];
223 typedef bitset_word_t *re_bitset_ptr_t;
224 typedef const bitset_word_t *re_const_bitset_ptr_t;
225
226 #define PREV_WORD_CONSTRAINT 0x0001
227 #define PREV_NOTWORD_CONSTRAINT 0x0002
228 #define NEXT_WORD_CONSTRAINT 0x0004
229 #define NEXT_NOTWORD_CONSTRAINT 0x0008
230 #define PREV_NEWLINE_CONSTRAINT 0x0010
231 #define NEXT_NEWLINE_CONSTRAINT 0x0020
232 #define PREV_BEGBUF_CONSTRAINT 0x0040
233 #define NEXT_ENDBUF_CONSTRAINT 0x0080
234 #define WORD_DELIM_CONSTRAINT 0x0100
235 #define NOT_WORD_DELIM_CONSTRAINT 0x0200
236
237 typedef enum
238 {
239   INSIDE_WORD = PREV_WORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
240   WORD_FIRST = PREV_NOTWORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
241   WORD_LAST = PREV_WORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
242   INSIDE_NOTWORD = PREV_NOTWORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
243   LINE_FIRST = PREV_NEWLINE_CONSTRAINT,
244   LINE_LAST = NEXT_NEWLINE_CONSTRAINT,
245   BUF_FIRST = PREV_BEGBUF_CONSTRAINT,
246   BUF_LAST = NEXT_ENDBUF_CONSTRAINT,
247   WORD_DELIM = WORD_DELIM_CONSTRAINT,
248   NOT_WORD_DELIM = NOT_WORD_DELIM_CONSTRAINT
249 } re_context_type;
250
251 typedef struct
252 {
253   Idx alloc;
254   Idx nelem;
255   Idx *elems;
256 } re_node_set;
257
258 typedef enum
259 {
260   NON_TYPE = 0,
261
262   /* Node type, These are used by token, node, tree.  */
263   CHARACTER = 1,
264   END_OF_RE = 2,
265   SIMPLE_BRACKET = 3,
266   OP_BACK_REF = 4,
267   OP_PERIOD = 5,
268 #ifdef RE_ENABLE_I18N
269   COMPLEX_BRACKET = 6,
270   OP_UTF8_PERIOD = 7,
271 #endif /* RE_ENABLE_I18N */
272
273   /* We define EPSILON_BIT as a macro so that OP_OPEN_SUBEXP is used
274      when the debugger shows values of this enum type.  */
275 #define EPSILON_BIT 8
276   OP_OPEN_SUBEXP = EPSILON_BIT | 0,
277   OP_CLOSE_SUBEXP = EPSILON_BIT | 1,
278   OP_ALT = EPSILON_BIT | 2,
279   OP_DUP_ASTERISK = EPSILON_BIT | 3,
280   ANCHOR = EPSILON_BIT | 4,
281
282   /* Tree type, these are used only by tree. */
283   CONCAT = 16,
284   SUBEXP = 17,
285
286   /* Token type, these are used only by token.  */
287   OP_DUP_PLUS = 18,
288   OP_DUP_QUESTION,
289   OP_OPEN_BRACKET,
290   OP_CLOSE_BRACKET,
291   OP_CHARSET_RANGE,
292   OP_OPEN_DUP_NUM,
293   OP_CLOSE_DUP_NUM,
294   OP_NON_MATCH_LIST,
295   OP_OPEN_COLL_ELEM,
296   OP_CLOSE_COLL_ELEM,
297   OP_OPEN_EQUIV_CLASS,
298   OP_CLOSE_EQUIV_CLASS,
299   OP_OPEN_CHAR_CLASS,
300   OP_CLOSE_CHAR_CLASS,
301   OP_WORD,
302   OP_NOTWORD,
303   OP_SPACE,
304   OP_NOTSPACE,
305   BACK_SLASH
306
307 } re_token_type_t;
308
309 #ifdef RE_ENABLE_I18N
310 typedef struct
311 {
312   /* Multibyte characters.  */
313   wchar_t *mbchars;
314
315   /* Collating symbols.  */
316 # ifdef _LIBC
317   int32_t *coll_syms;
318 # endif
319
320   /* Equivalence classes. */
321 # ifdef _LIBC
322   int32_t *equiv_classes;
323 # endif
324
325   /* Range expressions. */
326 # ifdef _LIBC
327   uint32_t *range_starts;
328   uint32_t *range_ends;
329 # else /* not _LIBC */
330   wchar_t *range_starts;
331   wchar_t *range_ends;
332 # endif /* not _LIBC */
333
334   /* Character classes. */
335   wctype_t *char_classes;
336
337   /* If this character set is the non-matching list.  */
338   unsigned int non_match : 1;
339
340   /* # of multibyte characters.  */
341   Idx nmbchars;
342
343   /* # of collating symbols.  */
344   Idx ncoll_syms;
345
346   /* # of equivalence classes. */
347   Idx nequiv_classes;
348
349   /* # of range expressions. */
350   Idx nranges;
351
352   /* # of character classes. */
353   Idx nchar_classes;
354 } re_charset_t;
355 #endif /* RE_ENABLE_I18N */
356
357 typedef struct
358 {
359   union
360   {
361     unsigned char c;            /* for CHARACTER */
362     re_bitset_ptr_t sbcset;     /* for SIMPLE_BRACKET */
363 #ifdef RE_ENABLE_I18N
364     re_charset_t *mbcset;       /* for COMPLEX_BRACKET */
365 #endif /* RE_ENABLE_I18N */
366     Idx idx;                    /* for BACK_REF */
367     re_context_type ctx_type;   /* for ANCHOR */
368   } opr;
369 #if __GNUC__ >= 2 && !defined __STRICT_ANSI__
370   re_token_type_t type : 8;
371 #else
372   re_token_type_t type;
373 #endif
374   unsigned int constraint : 10; /* context constraint */
375   unsigned int duplicated : 1;
376   unsigned int opt_subexp : 1;
377 #ifdef RE_ENABLE_I18N
378   unsigned int accept_mb : 1;
379   /* These 2 bits can be moved into the union if needed (e.g. if running out
380      of bits; move opr.c to opr.c.c and move the flags to opr.c.flags).  */
381   unsigned int mb_partial : 1;
382 #endif
383   unsigned int word_char : 1;
384 } re_token_t;
385
386 #define IS_EPSILON_NODE(type) ((type) & EPSILON_BIT)
387
388 struct re_string_t
389 {
390   /* Indicate the raw buffer which is the original string passed as an
391      argument of regexec(), re_search(), etc..  */
392   const unsigned char *raw_mbs;
393   /* Store the multibyte string.  In case of "case insensitive mode" like
394      REG_ICASE, upper cases of the string are stored, otherwise MBS points
395      the same address that RAW_MBS points.  */
396   unsigned char *mbs;
397 #ifdef RE_ENABLE_I18N
398   /* Store the wide character string which is corresponding to MBS.  */
399   wint_t *wcs;
400   Idx *offsets;
401   mbstate_t cur_state;
402 #endif
403   /* Index in RAW_MBS.  Each character mbs[i] corresponds to
404      raw_mbs[raw_mbs_idx + i].  */
405   Idx raw_mbs_idx;
406   /* The length of the valid characters in the buffers.  */
407   Idx valid_len;
408   /* The corresponding number of bytes in raw_mbs array.  */
409   Idx valid_raw_len;
410   /* The length of the buffers MBS and WCS.  */
411   Idx bufs_len;
412   /* The index in MBS, which is updated by re_string_fetch_byte.  */
413   Idx cur_idx;
414   /* length of RAW_MBS array.  */
415   Idx raw_len;
416   /* This is RAW_LEN - RAW_MBS_IDX + VALID_LEN - VALID_RAW_LEN.  */
417   Idx len;
418   /* End of the buffer may be shorter than its length in the cases such
419      as re_match_2, re_search_2.  Then, we use STOP for end of the buffer
420      instead of LEN.  */
421   Idx raw_stop;
422   /* This is RAW_STOP - RAW_MBS_IDX adjusted through OFFSETS.  */
423   Idx stop;
424
425   /* The context of mbs[0].  We store the context independently, since
426      the context of mbs[0] may be different from raw_mbs[0], which is
427      the beginning of the input string.  */
428   unsigned int tip_context;
429   /* The translation passed as a part of an argument of re_compile_pattern.  */
430   RE_TRANSLATE_TYPE trans;
431   /* Copy of re_dfa_t's word_char.  */
432   re_const_bitset_ptr_t word_char;
433   /* true if REG_ICASE.  */
434   unsigned char icase;
435   unsigned char is_utf8;
436   unsigned char map_notascii;
437   unsigned char mbs_allocated;
438   unsigned char offsets_needed;
439   unsigned char newline_anchor;
440   unsigned char word_ops_used;
441   int mb_cur_max;
442 };
443 typedef struct re_string_t re_string_t;
444
445
446 struct re_dfa_t;
447 typedef struct re_dfa_t re_dfa_t;
448
449 #ifndef _LIBC
450 # define internal_function
451 #endif
452
453 #ifndef NOT_IN_libc
454 static reg_errcode_t re_string_realloc_buffers (re_string_t *pstr,
455                                                 Idx new_buf_len)
456      internal_function;
457 # ifdef RE_ENABLE_I18N
458 static void build_wcs_buffer (re_string_t *pstr) internal_function;
459 static reg_errcode_t build_wcs_upper_buffer (re_string_t *pstr)
460   internal_function;
461 # endif /* RE_ENABLE_I18N */
462 static void build_upper_buffer (re_string_t *pstr) internal_function;
463 static void re_string_translate_buffer (re_string_t *pstr) internal_function;
464 static unsigned int re_string_context_at (const re_string_t *input, Idx idx,
465                                           int eflags)
466      internal_function __attribute__ ((pure));
467 #endif
468 #define re_string_peek_byte(pstr, offset) \
469   ((pstr)->mbs[(pstr)->cur_idx + offset])
470 #define re_string_fetch_byte(pstr) \
471   ((pstr)->mbs[(pstr)->cur_idx++])
472 #define re_string_first_byte(pstr, idx) \
473   ((idx) == (pstr)->valid_len || (pstr)->wcs[idx] != WEOF)
474 #define re_string_is_single_byte_char(pstr, idx) \
475   ((pstr)->wcs[idx] != WEOF && ((pstr)->valid_len == (idx) + 1 \
476                                 || (pstr)->wcs[(idx) + 1] != WEOF))
477 #define re_string_eoi(pstr) ((pstr)->stop <= (pstr)->cur_idx)
478 #define re_string_cur_idx(pstr) ((pstr)->cur_idx)
479 #define re_string_get_buffer(pstr) ((pstr)->mbs)
480 #define re_string_length(pstr) ((pstr)->len)
481 #define re_string_byte_at(pstr,idx) ((pstr)->mbs[idx])
482 #define re_string_skip_bytes(pstr,idx) ((pstr)->cur_idx += (idx))
483 #define re_string_set_index(pstr,idx) ((pstr)->cur_idx = (idx))
484
485 #if defined _LIBC || HAVE_ALLOCA
486 # include <alloca.h>
487 #endif
488
489 #ifndef _LIBC
490 # if HAVE_ALLOCA
491 /* The OS usually guarantees only one guard page at the bottom of the stack,
492    and a page size can be as small as 4096 bytes.  So we cannot safely
493    allocate anything larger than 4096 bytes.  Also care for the possibility
494    of a few compiler-allocated temporary stack slots.  */
495 #  define __libc_use_alloca(n) ((n) < 4032)
496 # else
497 /* alloca is implemented with malloc, so just use malloc.  */
498 #  define __libc_use_alloca(n) 0
499 #  undef alloca
500 #  define alloca(n) malloc (n)
501 # endif
502 #endif
503
504 #ifdef _LIBC
505 # define MALLOC_0_IS_NONNULL 1
506 #elif !defined MALLOC_0_IS_NONNULL
507 # define MALLOC_0_IS_NONNULL 0
508 #endif
509
510 #ifndef MAX
511 # define MAX(a,b) ((a) < (b) ? (b) : (a))
512 #endif
513 #ifndef MIN
514 # define MIN(a,b) ((a) < (b) ? (a) : (b))
515 #endif
516
517 #define re_malloc(t,n) ((t *) malloc ((n) * sizeof (t)))
518 #define re_realloc(p,t,n) ((t *) realloc (p, (n) * sizeof (t)))
519 #define re_free(p) free (p)
520
521 struct bin_tree_t
522 {
523   struct bin_tree_t *parent;
524   struct bin_tree_t *left;
525   struct bin_tree_t *right;
526   struct bin_tree_t *first;
527   struct bin_tree_t *next;
528
529   re_token_t token;
530
531   /* 'node_idx' is the index in dfa->nodes, if 'type' == 0.
532      Otherwise 'type' indicate the type of this node.  */
533   Idx node_idx;
534 };
535 typedef struct bin_tree_t bin_tree_t;
536
537 #define BIN_TREE_STORAGE_SIZE \
538   ((1024 - sizeof (void *)) / sizeof (bin_tree_t))
539
540 struct bin_tree_storage_t
541 {
542   struct bin_tree_storage_t *next;
543   bin_tree_t data[BIN_TREE_STORAGE_SIZE];
544 };
545 typedef struct bin_tree_storage_t bin_tree_storage_t;
546
547 #define CONTEXT_WORD 1
548 #define CONTEXT_NEWLINE (CONTEXT_WORD << 1)
549 #define CONTEXT_BEGBUF (CONTEXT_NEWLINE << 1)
550 #define CONTEXT_ENDBUF (CONTEXT_BEGBUF << 1)
551
552 #define IS_WORD_CONTEXT(c) ((c) & CONTEXT_WORD)
553 #define IS_NEWLINE_CONTEXT(c) ((c) & CONTEXT_NEWLINE)
554 #define IS_BEGBUF_CONTEXT(c) ((c) & CONTEXT_BEGBUF)
555 #define IS_ENDBUF_CONTEXT(c) ((c) & CONTEXT_ENDBUF)
556 #define IS_ORDINARY_CONTEXT(c) ((c) == 0)
557
558 #define IS_WORD_CHAR(ch) (isalnum (ch) || (ch) == '_')
559 #define IS_NEWLINE(ch) ((ch) == NEWLINE_CHAR)
560 #define IS_WIDE_WORD_CHAR(ch) (iswalnum (ch) || (ch) == L'_')
561 #define IS_WIDE_NEWLINE(ch) ((ch) == WIDE_NEWLINE_CHAR)
562
563 #define NOT_SATISFY_PREV_CONSTRAINT(constraint,context) \
564  ((((constraint) & PREV_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
565   || ((constraint & PREV_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
566   || ((constraint & PREV_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context))\
567   || ((constraint & PREV_BEGBUF_CONSTRAINT) && !IS_BEGBUF_CONTEXT (context)))
568
569 #define NOT_SATISFY_NEXT_CONSTRAINT(constraint,context) \
570  ((((constraint) & NEXT_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
571   || (((constraint) & NEXT_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
572   || (((constraint) & NEXT_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context)) \
573   || (((constraint) & NEXT_ENDBUF_CONSTRAINT) && !IS_ENDBUF_CONTEXT (context)))
574
575 struct re_dfastate_t
576 {
577   re_hashval_t hash;
578   re_node_set nodes;
579   re_node_set non_eps_nodes;
580   re_node_set inveclosure;
581   re_node_set *entrance_nodes;
582   struct re_dfastate_t **trtable, **word_trtable;
583   unsigned int context : 4;
584   unsigned int halt : 1;
585   /* If this state can accept "multi byte".
586      Note that we refer to multibyte characters, and multi character
587      collating elements as "multi byte".  */
588   unsigned int accept_mb : 1;
589   /* If this state has backreference node(s).  */
590   unsigned int has_backref : 1;
591   unsigned int has_constraint : 1;
592 };
593 typedef struct re_dfastate_t re_dfastate_t;
594
595 struct re_state_table_entry
596 {
597   Idx num;
598   Idx alloc;
599   re_dfastate_t **array;
600 };
601
602 /* Array type used in re_sub_match_last_t and re_sub_match_top_t.  */
603
604 typedef struct
605 {
606   Idx next_idx;
607   Idx alloc;
608   re_dfastate_t **array;
609 } state_array_t;
610
611 /* Store information about the node NODE whose type is OP_CLOSE_SUBEXP.  */
612
613 typedef struct
614 {
615   Idx node;
616   Idx str_idx; /* The position NODE match at.  */
617   state_array_t path;
618 } re_sub_match_last_t;
619
620 /* Store information about the node NODE whose type is OP_OPEN_SUBEXP.
621    And information about the node, whose type is OP_CLOSE_SUBEXP,
622    corresponding to NODE is stored in LASTS.  */
623
624 typedef struct
625 {
626   Idx str_idx;
627   Idx node;
628   state_array_t *path;
629   Idx alasts; /* Allocation size of LASTS.  */
630   Idx nlasts; /* The number of LASTS.  */
631   re_sub_match_last_t **lasts;
632 } re_sub_match_top_t;
633
634 struct re_backref_cache_entry
635 {
636   Idx node;
637   Idx str_idx;
638   Idx subexp_from;
639   Idx subexp_to;
640   char more;
641   char unused;
642   unsigned short int eps_reachable_subexps_map;
643 };
644
645 typedef struct
646 {
647   /* The string object corresponding to the input string.  */
648   re_string_t input;
649 #if defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L)
650   const re_dfa_t *const dfa;
651 #else
652   const re_dfa_t *dfa;
653 #endif
654   /* EFLAGS of the argument of regexec.  */
655   int eflags;
656   /* Where the matching ends.  */
657   Idx match_last;
658   Idx last_node;
659   /* The state log used by the matcher.  */
660   re_dfastate_t **state_log;
661   Idx state_log_top;
662   /* Back reference cache.  */
663   Idx nbkref_ents;
664   Idx abkref_ents;
665   struct re_backref_cache_entry *bkref_ents;
666   int max_mb_elem_len;
667   Idx nsub_tops;
668   Idx asub_tops;
669   re_sub_match_top_t **sub_tops;
670 } re_match_context_t;
671
672 typedef struct
673 {
674   re_dfastate_t **sifted_states;
675   re_dfastate_t **limited_states;
676   Idx last_node;
677   Idx last_str_idx;
678   re_node_set limits;
679 } re_sift_context_t;
680
681 struct re_fail_stack_ent_t
682 {
683   Idx idx;
684   Idx node;
685   regmatch_t *regs;
686   re_node_set eps_via_nodes;
687 };
688
689 struct re_fail_stack_t
690 {
691   Idx num;
692   Idx alloc;
693   struct re_fail_stack_ent_t *stack;
694 };
695
696 struct re_dfa_t
697 {
698   re_token_t *nodes;
699   size_t nodes_alloc;
700   size_t nodes_len;
701   Idx *nexts;
702   Idx *org_indices;
703   re_node_set *edests;
704   re_node_set *eclosures;
705   re_node_set *inveclosures;
706   struct re_state_table_entry *state_table;
707   re_dfastate_t *init_state;
708   re_dfastate_t *init_state_word;
709   re_dfastate_t *init_state_nl;
710   re_dfastate_t *init_state_begbuf;
711   bin_tree_t *str_tree;
712   bin_tree_storage_t *str_tree_storage;
713   re_bitset_ptr_t sb_char;
714   int str_tree_storage_idx;
715
716   /* number of subexpressions 're_nsub' is in regex_t.  */
717   re_hashval_t state_hash_mask;
718   Idx init_node;
719   Idx nbackref; /* The number of backreference in this dfa.  */
720
721   /* Bitmap expressing which backreference is used.  */
722   bitset_word_t used_bkref_map;
723   bitset_word_t completed_bkref_map;
724
725   unsigned int has_plural_match : 1;
726   /* If this dfa has "multibyte node", which is a backreference or
727      a node which can accept multibyte character or multi character
728      collating element.  */
729   unsigned int has_mb_node : 1;
730   unsigned int is_utf8 : 1;
731   unsigned int map_notascii : 1;
732   unsigned int word_ops_used : 1;
733   int mb_cur_max;
734   bitset_t word_char;
735   reg_syntax_t syntax;
736   Idx *subexp_map;
737 #ifdef DEBUG
738   char* re_str;
739 #endif
740   lock_define (lock)
741 };
742
743 #define re_node_set_init_empty(set) memset (set, '\0', sizeof (re_node_set))
744 #define re_node_set_remove(set,id) \
745   (re_node_set_remove_at (set, re_node_set_contains (set, id) - 1))
746 #define re_node_set_empty(p) ((p)->nelem = 0)
747 #define re_node_set_free(set) re_free ((set)->elems)
748 \f
749
750 typedef enum
751 {
752   SB_CHAR,
753   MB_CHAR,
754   EQUIV_CLASS,
755   COLL_SYM,
756   CHAR_CLASS
757 } bracket_elem_type;
758
759 typedef struct
760 {
761   bracket_elem_type type;
762   union
763   {
764     unsigned char ch;
765     unsigned char *name;
766     wchar_t wch;
767   } opr;
768 } bracket_elem_t;
769
770
771 /* Functions for bitset_t operation.  */
772
773 static void
774 bitset_set (bitset_t set, Idx i)
775 {
776   set[i / BITSET_WORD_BITS] |= (bitset_word_t) 1 << i % BITSET_WORD_BITS;
777 }
778
779 static void
780 bitset_clear (bitset_t set, Idx i)
781 {
782   set[i / BITSET_WORD_BITS] &= ~ ((bitset_word_t) 1 << i % BITSET_WORD_BITS);
783 }
784
785 static bool
786 bitset_contain (const bitset_t set, Idx i)
787 {
788   return (set[i / BITSET_WORD_BITS] >> i % BITSET_WORD_BITS) & 1;
789 }
790
791 static void
792 bitset_empty (bitset_t set)
793 {
794   memset (set, '\0', sizeof (bitset_t));
795 }
796
797 static void
798 bitset_set_all (bitset_t set)
799 {
800   memset (set, -1, sizeof (bitset_word_t) * (SBC_MAX / BITSET_WORD_BITS));
801   if (SBC_MAX % BITSET_WORD_BITS != 0)
802     set[BITSET_WORDS - 1] =
803       ((bitset_word_t) 1 << SBC_MAX % BITSET_WORD_BITS) - 1;
804 }
805
806 static void
807 bitset_copy (bitset_t dest, const bitset_t src)
808 {
809   memcpy (dest, src, sizeof (bitset_t));
810 }
811
812 static void __attribute__ ((unused))
813 bitset_not (bitset_t set)
814 {
815   int bitset_i;
816   for (bitset_i = 0; bitset_i < SBC_MAX / BITSET_WORD_BITS; ++bitset_i)
817     set[bitset_i] = ~set[bitset_i];
818   if (SBC_MAX % BITSET_WORD_BITS != 0)
819     set[BITSET_WORDS - 1] =
820       ((((bitset_word_t) 1 << SBC_MAX % BITSET_WORD_BITS) - 1)
821        & ~set[BITSET_WORDS - 1]);
822 }
823
824 static void __attribute__ ((unused))
825 bitset_merge (bitset_t dest, const bitset_t src)
826 {
827   int bitset_i;
828   for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
829     dest[bitset_i] |= src[bitset_i];
830 }
831
832 static void __attribute__ ((unused))
833 bitset_mask (bitset_t dest, const bitset_t src)
834 {
835   int bitset_i;
836   for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
837     dest[bitset_i] &= src[bitset_i];
838 }
839
840 #ifdef RE_ENABLE_I18N
841 /* Functions for re_string.  */
842 static int
843 internal_function __attribute__ ((pure, unused))
844 re_string_char_size_at (const re_string_t *pstr, Idx idx)
845 {
846   int byte_idx;
847   if (pstr->mb_cur_max == 1)
848     return 1;
849   for (byte_idx = 1; idx + byte_idx < pstr->valid_len; ++byte_idx)
850     if (pstr->wcs[idx + byte_idx] != WEOF)
851       break;
852   return byte_idx;
853 }
854
855 static wint_t
856 internal_function __attribute__ ((pure, unused))
857 re_string_wchar_at (const re_string_t *pstr, Idx idx)
858 {
859   if (pstr->mb_cur_max == 1)
860     return (wint_t) pstr->mbs[idx];
861   return (wint_t) pstr->wcs[idx];
862 }
863
864 # ifndef NOT_IN_libc
865 static int
866 internal_function __attribute__ ((pure, unused))
867 re_string_elem_size_at (const re_string_t *pstr, Idx idx)
868 {
869 #  ifdef _LIBC
870   const unsigned char *p, *extra;
871   const int32_t *table, *indirect;
872 #   include <locale/weight.h>
873   uint_fast32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
874
875   if (nrules != 0)
876     {
877       table = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
878       extra = (const unsigned char *)
879         _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB);
880       indirect = (const int32_t *) _NL_CURRENT (LC_COLLATE,
881                                                 _NL_COLLATE_INDIRECTMB);
882       p = pstr->mbs + idx;
883       findidx (&p, pstr->len - idx);
884       return p - pstr->mbs - idx;
885     }
886   else
887 #  endif /* _LIBC */
888     return 1;
889 }
890 # endif
891 #endif /* RE_ENABLE_I18N */
892
893 #ifndef __GNUC_PREREQ
894 # if defined __GNUC__ && defined __GNUC_MINOR__
895 #  define __GNUC_PREREQ(maj, min) \
896          ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
897 # else
898 #  define __GNUC_PREREQ(maj, min) 0
899 # endif
900 #endif
901
902 #if __GNUC_PREREQ (3,4)
903 # undef __attribute_warn_unused_result__
904 # define __attribute_warn_unused_result__ \
905    __attribute__ ((__warn_unused_result__))
906 #else
907 # define __attribute_warn_unused_result__ /* empty */
908 #endif
909
910 #endif /*  _REGEX_INTERNAL_H */