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