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