1146040Stjr/* Extended regular expression matching and search library.
2250724Sjkim   Copyright (C) 2002-2012 Free Software Foundation, Inc.
3146040Stjr   This file is part of the GNU C Library.
4146040Stjr   Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
5146040Stjr
6146040Stjr   The GNU C Library is free software; you can redistribute it and/or
7146040Stjr   modify it under the terms of the GNU Lesser General Public
8146040Stjr   License as published by the Free Software Foundation; either
9146040Stjr   version 2.1 of the License, or (at your option) any later version.
10146040Stjr
11146040Stjr   The GNU C Library is distributed in the hope that it will be useful,
12146040Stjr   but WITHOUT ANY WARRANTY; without even the implied warranty of
13146040Stjr   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14146040Stjr   Lesser General Public License for more details.
15146040Stjr
16146040Stjr   You should have received a copy of the GNU Lesser General Public
17250724Sjkim   License along with the GNU C Library; if not, see
18250724Sjkim   <http://www.gnu.org/licenses/>.  */
19146040Stjr
20146040Stjr#ifndef _REGEX_INTERNAL_H
21146040Stjr#define _REGEX_INTERNAL_H 1
22146040Stjr
23146040Stjr#include <assert.h>
24146040Stjr#include <ctype.h>
25146040Stjr#include <stdio.h>
26146040Stjr#include <stdlib.h>
27146040Stjr#include <string.h>
28146040Stjr
29146040Stjr#if defined HAVE_LANGINFO_H || defined HAVE_LANGINFO_CODESET || defined _LIBC
30146040Stjr# include <langinfo.h>
31146040Stjr#endif
32146040Stjr#if defined HAVE_LOCALE_H || defined _LIBC
33146040Stjr# include <locale.h>
34146040Stjr#endif
35146040Stjr#if defined HAVE_WCHAR_H || defined _LIBC
36146040Stjr# include <wchar.h>
37146040Stjr#endif /* HAVE_WCHAR_H || _LIBC */
38146040Stjr#if defined HAVE_WCTYPE_H || defined _LIBC
39146040Stjr# include <wctype.h>
40146040Stjr#endif /* HAVE_WCTYPE_H || _LIBC */
41250724Sjkim#if defined HAVE_STDBOOL_H || defined _LIBC
42250724Sjkim# include <stdbool.h>
43250724Sjkim#endif /* HAVE_STDBOOL_H || _LIBC */
44250724Sjkim#if defined HAVE_STDINT_H || defined _LIBC
45250724Sjkim# include <stdint.h>
46250724Sjkim#endif /* HAVE_STDINT_H || _LIBC */
47250724Sjkim#if defined _LIBC
48250724Sjkim# include <bits/libc-lock.h>
49250724Sjkim#else
50250724Sjkim# define __libc_lock_define(CLASS,NAME)
51250724Sjkim# define __libc_lock_init(NAME) do { } while (0)
52250724Sjkim# define __libc_lock_lock(NAME) do { } while (0)
53250724Sjkim# define __libc_lock_unlock(NAME) do { } while (0)
54250724Sjkim#endif
55146040Stjr
56146040Stjr/* In case that the system doesn't have isblank().  */
57146040Stjr#if !defined _LIBC && !defined HAVE_ISBLANK && !defined isblank
58146040Stjr# define isblank(ch) ((ch) == ' ' || (ch) == '\t')
59146040Stjr#endif
60146040Stjr
61146040Stjr#ifdef _LIBC
62146040Stjr# ifndef _RE_DEFINE_LOCALE_FUNCTIONS
63146040Stjr#  define _RE_DEFINE_LOCALE_FUNCTIONS 1
64146040Stjr#   include <locale/localeinfo.h>
65146040Stjr#   include <locale/elem-hash.h>
66146040Stjr#   include <locale/coll-lookup.h>
67146040Stjr# endif
68146040Stjr#endif
69146040Stjr
70146040Stjr/* This is for other GNU distributions with internationalized messages.  */
71146040Stjr#if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
72146040Stjr# include <libintl.h>
73146040Stjr# ifdef _LIBC
74146040Stjr#  undef gettext
75146040Stjr#  define gettext(msgid) \
76250724Sjkim  __dcgettext (_libc_intl_domainname, msgid, LC_MESSAGES)
77146040Stjr# endif
78146040Stjr#else
79146040Stjr# define gettext(msgid) (msgid)
80146040Stjr#endif
81146040Stjr
82146040Stjr#ifndef gettext_noop
83146040Stjr/* This define is so xgettext can find the internationalizable
84146040Stjr   strings.  */
85146040Stjr# define gettext_noop(String) String
86146040Stjr#endif
87146040Stjr
88250724Sjkim/* For loser systems without the definition.  */
89250724Sjkim#ifndef SIZE_MAX
90250724Sjkim# define SIZE_MAX ((size_t) -1)
91250724Sjkim#endif
92250724Sjkim
93146040Stjr#if (defined MB_CUR_MAX && HAVE_LOCALE_H && HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_WCRTOMB && HAVE_MBRTOWC && HAVE_WCSCOLL) || _LIBC
94146040Stjr# define RE_ENABLE_I18N
95146040Stjr#endif
96146040Stjr
97146040Stjr#if __GNUC__ >= 3
98146040Stjr# define BE(expr, val) __builtin_expect (expr, val)
99146040Stjr#else
100146040Stjr# define BE(expr, val) (expr)
101146040Stjr# define inline
102146040Stjr#endif
103146040Stjr
104146040Stjr/* Number of single byte character.  */
105146040Stjr#define SBC_MAX 256
106146040Stjr
107146040Stjr#define COLL_ELEM_LEN_MAX 8
108146040Stjr
109146040Stjr/* The character which represents newline.  */
110146040Stjr#define NEWLINE_CHAR '\n'
111146040Stjr#define WIDE_NEWLINE_CHAR L'\n'
112146040Stjr
113146040Stjr/* Rename to standard API for using out of glibc.  */
114146040Stjr#ifndef _LIBC
115146040Stjr# define __wctype wctype
116146040Stjr# define __iswctype iswctype
117146040Stjr# define __btowc btowc
118250724Sjkim# define __mbrtowc mbrtowc
119146040Stjr# define __mempcpy mempcpy
120146040Stjr# define __wcrtomb wcrtomb
121146040Stjr# define __regfree regfree
122146040Stjr# define attribute_hidden
123146040Stjr#endif /* not _LIBC */
124146040Stjr
125146040Stjr#ifdef __GNUC__
126146040Stjr# define __attribute(arg) __attribute__ (arg)
127146040Stjr#else
128146040Stjr# define __attribute(arg)
129146040Stjr#endif
130146040Stjr
131146040Stjrextern const char __re_error_msgid[] attribute_hidden;
132146040Stjrextern const size_t __re_error_msgid_idx[] attribute_hidden;
133146040Stjr
134250724Sjkim/* An integer used to represent a set of bits.  It must be unsigned,
135250724Sjkim   and must be at least as wide as unsigned int.  */
136250724Sjkimtypedef unsigned long int bitset_word_t;
137250724Sjkim/* All bits set in a bitset_word_t.  */
138250724Sjkim#define BITSET_WORD_MAX ULONG_MAX
139250724Sjkim/* Number of bits in a bitset_word_t.  */
140250724Sjkim#define BITSET_WORD_BITS (sizeof (bitset_word_t) * CHAR_BIT)
141250724Sjkim/* Number of bitset_word_t in a bit_set.  */
142250724Sjkim#define BITSET_WORDS (SBC_MAX / BITSET_WORD_BITS)
143250724Sjkimtypedef bitset_word_t bitset_t[BITSET_WORDS];
144250724Sjkimtypedef bitset_word_t *re_bitset_ptr_t;
145250724Sjkimtypedef const bitset_word_t *re_const_bitset_ptr_t;
146146040Stjr
147250724Sjkim#define bitset_set(set,i) \
148250724Sjkim  (set[i / BITSET_WORD_BITS] |= (bitset_word_t) 1 << i % BITSET_WORD_BITS)
149250724Sjkim#define bitset_clear(set,i) \
150250724Sjkim  (set[i / BITSET_WORD_BITS] &= ~((bitset_word_t) 1 << i % BITSET_WORD_BITS))
151250724Sjkim#define bitset_contain(set,i) \
152250724Sjkim  (set[i / BITSET_WORD_BITS] & ((bitset_word_t) 1 << i % BITSET_WORD_BITS))
153250724Sjkim#define bitset_empty(set) memset (set, '\0', sizeof (bitset_t))
154250724Sjkim#define bitset_set_all(set) memset (set, '\xff', sizeof (bitset_t))
155250724Sjkim#define bitset_copy(dest,src) memcpy (dest, src, sizeof (bitset_t))
156146040Stjr
157146040Stjr#define PREV_WORD_CONSTRAINT 0x0001
158146040Stjr#define PREV_NOTWORD_CONSTRAINT 0x0002
159146040Stjr#define NEXT_WORD_CONSTRAINT 0x0004
160146040Stjr#define NEXT_NOTWORD_CONSTRAINT 0x0008
161146040Stjr#define PREV_NEWLINE_CONSTRAINT 0x0010
162146040Stjr#define NEXT_NEWLINE_CONSTRAINT 0x0020
163146040Stjr#define PREV_BEGBUF_CONSTRAINT 0x0040
164146040Stjr#define NEXT_ENDBUF_CONSTRAINT 0x0080
165146040Stjr#define WORD_DELIM_CONSTRAINT 0x0100
166146040Stjr#define NOT_WORD_DELIM_CONSTRAINT 0x0200
167146040Stjr
168146040Stjrtypedef enum
169146040Stjr{
170146040Stjr  INSIDE_WORD = PREV_WORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
171146040Stjr  WORD_FIRST = PREV_NOTWORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
172146040Stjr  WORD_LAST = PREV_WORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
173146040Stjr  INSIDE_NOTWORD = PREV_NOTWORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
174146040Stjr  LINE_FIRST = PREV_NEWLINE_CONSTRAINT,
175146040Stjr  LINE_LAST = NEXT_NEWLINE_CONSTRAINT,
176146040Stjr  BUF_FIRST = PREV_BEGBUF_CONSTRAINT,
177146040Stjr  BUF_LAST = NEXT_ENDBUF_CONSTRAINT,
178146040Stjr  WORD_DELIM = WORD_DELIM_CONSTRAINT,
179146040Stjr  NOT_WORD_DELIM = NOT_WORD_DELIM_CONSTRAINT
180146040Stjr} re_context_type;
181146040Stjr
182146040Stjrtypedef struct
183146040Stjr{
184146040Stjr  int alloc;
185146040Stjr  int nelem;
186146040Stjr  int *elems;
187146040Stjr} re_node_set;
188146040Stjr
189146040Stjrtypedef enum
190146040Stjr{
191146040Stjr  NON_TYPE = 0,
192146040Stjr
193146040Stjr  /* Node type, These are used by token, node, tree.  */
194146040Stjr  CHARACTER = 1,
195146040Stjr  END_OF_RE = 2,
196146040Stjr  SIMPLE_BRACKET = 3,
197146040Stjr  OP_BACK_REF = 4,
198146040Stjr  OP_PERIOD = 5,
199146040Stjr#ifdef RE_ENABLE_I18N
200146040Stjr  COMPLEX_BRACKET = 6,
201146040Stjr  OP_UTF8_PERIOD = 7,
202146040Stjr#endif /* RE_ENABLE_I18N */
203146040Stjr
204146040Stjr  /* We define EPSILON_BIT as a macro so that OP_OPEN_SUBEXP is used
205146040Stjr     when the debugger shows values of this enum type.  */
206146040Stjr#define EPSILON_BIT 8
207146040Stjr  OP_OPEN_SUBEXP = EPSILON_BIT | 0,
208146040Stjr  OP_CLOSE_SUBEXP = EPSILON_BIT | 1,
209146040Stjr  OP_ALT = EPSILON_BIT | 2,
210146040Stjr  OP_DUP_ASTERISK = EPSILON_BIT | 3,
211146040Stjr  ANCHOR = EPSILON_BIT | 4,
212146040Stjr
213146040Stjr  /* Tree type, these are used only by tree. */
214146040Stjr  CONCAT = 16,
215146040Stjr  SUBEXP = 17,
216146040Stjr
217146040Stjr  /* Token type, these are used only by token.  */
218146040Stjr  OP_DUP_PLUS = 18,
219146040Stjr  OP_DUP_QUESTION,
220146040Stjr  OP_OPEN_BRACKET,
221146040Stjr  OP_CLOSE_BRACKET,
222146040Stjr  OP_CHARSET_RANGE,
223146040Stjr  OP_OPEN_DUP_NUM,
224146040Stjr  OP_CLOSE_DUP_NUM,
225146040Stjr  OP_NON_MATCH_LIST,
226146040Stjr  OP_OPEN_COLL_ELEM,
227146040Stjr  OP_CLOSE_COLL_ELEM,
228146040Stjr  OP_OPEN_EQUIV_CLASS,
229146040Stjr  OP_CLOSE_EQUIV_CLASS,
230146040Stjr  OP_OPEN_CHAR_CLASS,
231146040Stjr  OP_CLOSE_CHAR_CLASS,
232146040Stjr  OP_WORD,
233146040Stjr  OP_NOTWORD,
234146040Stjr  OP_SPACE,
235146040Stjr  OP_NOTSPACE,
236146040Stjr  BACK_SLASH
237146040Stjr
238146040Stjr} re_token_type_t;
239146040Stjr
240146040Stjr#ifdef RE_ENABLE_I18N
241146040Stjrtypedef struct
242146040Stjr{
243146040Stjr  /* Multibyte characters.  */
244146040Stjr  wchar_t *mbchars;
245146040Stjr
246146040Stjr  /* Collating symbols.  */
247146040Stjr# ifdef _LIBC
248146040Stjr  int32_t *coll_syms;
249146040Stjr# endif
250146040Stjr
251146040Stjr  /* Equivalence classes. */
252146040Stjr# ifdef _LIBC
253146040Stjr  int32_t *equiv_classes;
254146040Stjr# endif
255146040Stjr
256146040Stjr  /* Range expressions. */
257146040Stjr# ifdef _LIBC
258146040Stjr  uint32_t *range_starts;
259146040Stjr  uint32_t *range_ends;
260146040Stjr# else /* not _LIBC */
261146040Stjr  wchar_t *range_starts;
262146040Stjr  wchar_t *range_ends;
263146040Stjr# endif /* not _LIBC */
264146040Stjr
265146040Stjr  /* Character classes. */
266146040Stjr  wctype_t *char_classes;
267146040Stjr
268146040Stjr  /* If this character set is the non-matching list.  */
269146040Stjr  unsigned int non_match : 1;
270146040Stjr
271146040Stjr  /* # of multibyte characters.  */
272146040Stjr  int nmbchars;
273146040Stjr
274146040Stjr  /* # of collating symbols.  */
275146040Stjr  int ncoll_syms;
276146040Stjr
277146040Stjr  /* # of equivalence classes. */
278146040Stjr  int nequiv_classes;
279146040Stjr
280146040Stjr  /* # of range expressions. */
281146040Stjr  int nranges;
282146040Stjr
283146040Stjr  /* # of character classes. */
284146040Stjr  int nchar_classes;
285146040Stjr} re_charset_t;
286146040Stjr#endif /* RE_ENABLE_I18N */
287146040Stjr
288146040Stjrtypedef struct
289146040Stjr{
290146040Stjr  union
291146040Stjr  {
292146040Stjr    unsigned char c;		/* for CHARACTER */
293146040Stjr    re_bitset_ptr_t sbcset;	/* for SIMPLE_BRACKET */
294146040Stjr#ifdef RE_ENABLE_I18N
295146040Stjr    re_charset_t *mbcset;	/* for COMPLEX_BRACKET */
296146040Stjr#endif /* RE_ENABLE_I18N */
297146040Stjr    int idx;			/* for BACK_REF */
298146040Stjr    re_context_type ctx_type;	/* for ANCHOR */
299146040Stjr  } opr;
300146040Stjr#if __GNUC__ >= 2
301146040Stjr  re_token_type_t type : 8;
302146040Stjr#else
303146040Stjr  re_token_type_t type;
304146040Stjr#endif
305146040Stjr  unsigned int constraint : 10;	/* context constraint */
306146040Stjr  unsigned int duplicated : 1;
307146040Stjr  unsigned int opt_subexp : 1;
308146040Stjr#ifdef RE_ENABLE_I18N
309146040Stjr  unsigned int accept_mb : 1;
310146040Stjr  /* These 2 bits can be moved into the union if needed (e.g. if running out
311146040Stjr     of bits; move opr.c to opr.c.c and move the flags to opr.c.flags).  */
312146040Stjr  unsigned int mb_partial : 1;
313146040Stjr#endif
314146040Stjr  unsigned int word_char : 1;
315146040Stjr} re_token_t;
316146040Stjr
317146040Stjr#define IS_EPSILON_NODE(type) ((type) & EPSILON_BIT)
318146040Stjr
319146040Stjrstruct re_string_t
320146040Stjr{
321146040Stjr  /* Indicate the raw buffer which is the original string passed as an
322146040Stjr     argument of regexec(), re_search(), etc..  */
323146040Stjr  const unsigned char *raw_mbs;
324146040Stjr  /* Store the multibyte string.  In case of "case insensitive mode" like
325146040Stjr     REG_ICASE, upper cases of the string are stored, otherwise MBS points
326146040Stjr     the same address that RAW_MBS points.  */
327146040Stjr  unsigned char *mbs;
328146040Stjr#ifdef RE_ENABLE_I18N
329146040Stjr  /* Store the wide character string which is corresponding to MBS.  */
330146040Stjr  wint_t *wcs;
331146040Stjr  int *offsets;
332146040Stjr  mbstate_t cur_state;
333146040Stjr#endif
334146040Stjr  /* Index in RAW_MBS.  Each character mbs[i] corresponds to
335146040Stjr     raw_mbs[raw_mbs_idx + i].  */
336146040Stjr  int raw_mbs_idx;
337146040Stjr  /* The length of the valid characters in the buffers.  */
338146040Stjr  int valid_len;
339146040Stjr  /* The corresponding number of bytes in raw_mbs array.  */
340146040Stjr  int valid_raw_len;
341146040Stjr  /* The length of the buffers MBS and WCS.  */
342146040Stjr  int bufs_len;
343146040Stjr  /* The index in MBS, which is updated by re_string_fetch_byte.  */
344146040Stjr  int cur_idx;
345146040Stjr  /* length of RAW_MBS array.  */
346146040Stjr  int raw_len;
347146040Stjr  /* This is RAW_LEN - RAW_MBS_IDX + VALID_LEN - VALID_RAW_LEN.  */
348146040Stjr  int len;
349146040Stjr  /* End of the buffer may be shorter than its length in the cases such
350146040Stjr     as re_match_2, re_search_2.  Then, we use STOP for end of the buffer
351146040Stjr     instead of LEN.  */
352146040Stjr  int raw_stop;
353146040Stjr  /* This is RAW_STOP - RAW_MBS_IDX adjusted through OFFSETS.  */
354146040Stjr  int stop;
355146040Stjr
356146040Stjr  /* The context of mbs[0].  We store the context independently, since
357146040Stjr     the context of mbs[0] may be different from raw_mbs[0], which is
358146040Stjr     the beginning of the input string.  */
359146040Stjr  unsigned int tip_context;
360146040Stjr  /* The translation passed as a part of an argument of re_compile_pattern.  */
361250724Sjkim  RE_TRANSLATE_TYPE trans;
362146040Stjr  /* Copy of re_dfa_t's word_char.  */
363146040Stjr  re_const_bitset_ptr_t word_char;
364146040Stjr  /* 1 if REG_ICASE.  */
365146040Stjr  unsigned char icase;
366146040Stjr  unsigned char is_utf8;
367146040Stjr  unsigned char map_notascii;
368146040Stjr  unsigned char mbs_allocated;
369146040Stjr  unsigned char offsets_needed;
370146040Stjr  unsigned char newline_anchor;
371146040Stjr  unsigned char word_ops_used;
372146040Stjr  int mb_cur_max;
373146040Stjr};
374146040Stjrtypedef struct re_string_t re_string_t;
375146040Stjr
376146040Stjr
377146040Stjrstruct re_dfa_t;
378146040Stjrtypedef struct re_dfa_t re_dfa_t;
379146040Stjr
380146040Stjr#ifndef _LIBC
381146040Stjr# ifdef __i386__
382146040Stjr#  define internal_function   __attribute ((regparm (3), stdcall))
383146040Stjr# else
384146040Stjr#  define internal_function
385146040Stjr# endif
386146040Stjr#endif
387146040Stjr
388250724Sjkim#ifndef NOT_IN_libc
389146040Stjrstatic reg_errcode_t re_string_realloc_buffers (re_string_t *pstr,
390146040Stjr						int new_buf_len)
391146040Stjr     internal_function;
392146040Stjr# ifdef RE_ENABLE_I18N
393146040Stjrstatic void build_wcs_buffer (re_string_t *pstr) internal_function;
394250724Sjkimstatic reg_errcode_t build_wcs_upper_buffer (re_string_t *pstr)
395250724Sjkim  internal_function;
396146040Stjr# endif /* RE_ENABLE_I18N */
397146040Stjrstatic void build_upper_buffer (re_string_t *pstr) internal_function;
398146040Stjrstatic void re_string_translate_buffer (re_string_t *pstr) internal_function;
399146040Stjrstatic unsigned int re_string_context_at (const re_string_t *input, int idx,
400146040Stjr					  int eflags)
401146040Stjr     internal_function __attribute ((pure));
402146040Stjr#endif
403146040Stjr#define re_string_peek_byte(pstr, offset) \
404146040Stjr  ((pstr)->mbs[(pstr)->cur_idx + offset])
405146040Stjr#define re_string_fetch_byte(pstr) \
406146040Stjr  ((pstr)->mbs[(pstr)->cur_idx++])
407146040Stjr#define re_string_first_byte(pstr, idx) \
408146040Stjr  ((idx) == (pstr)->valid_len || (pstr)->wcs[idx] != WEOF)
409146040Stjr#define re_string_is_single_byte_char(pstr, idx) \
410146040Stjr  ((pstr)->wcs[idx] != WEOF && ((pstr)->valid_len == (idx) + 1 \
411146040Stjr				|| (pstr)->wcs[(idx) + 1] != WEOF))
412146040Stjr#define re_string_eoi(pstr) ((pstr)->stop <= (pstr)->cur_idx)
413146040Stjr#define re_string_cur_idx(pstr) ((pstr)->cur_idx)
414146040Stjr#define re_string_get_buffer(pstr) ((pstr)->mbs)
415146040Stjr#define re_string_length(pstr) ((pstr)->len)
416146040Stjr#define re_string_byte_at(pstr,idx) ((pstr)->mbs[idx])
417146040Stjr#define re_string_skip_bytes(pstr,idx) ((pstr)->cur_idx += (idx))
418146040Stjr#define re_string_set_index(pstr,idx) ((pstr)->cur_idx = (idx))
419146040Stjr
420251437Sjkim#ifndef __FreeBSD__
421250724Sjkim#include <alloca.h>
422251437Sjkim#endif
423250724Sjkim
424250724Sjkim#ifndef _LIBC
425250724Sjkim# if HAVE_ALLOCA
426250724Sjkim/* The OS usually guarantees only one guard page at the bottom of the stack,
427250724Sjkim   and a page size can be as small as 4096 bytes.  So we cannot safely
428250724Sjkim   allocate anything larger than 4096 bytes.  Also care for the possibility
429250724Sjkim   of a few compiler-allocated temporary stack slots.  */
430250724Sjkim#  define __libc_use_alloca(n) ((n) < 4032)
431250724Sjkim# else
432250724Sjkim/* alloca is implemented with malloc, so just use malloc.  */
433250724Sjkim#  define __libc_use_alloca(n) 0
434250724Sjkim# endif
435250724Sjkim#endif
436250724Sjkim
437146040Stjr#define re_malloc(t,n) ((t *) malloc ((n) * sizeof (t)))
438146040Stjr#define re_realloc(p,t,n) ((t *) realloc (p, (n) * sizeof (t)))
439146040Stjr#define re_free(p) free (p)
440146040Stjr
441146040Stjrstruct bin_tree_t
442146040Stjr{
443146040Stjr  struct bin_tree_t *parent;
444146040Stjr  struct bin_tree_t *left;
445146040Stjr  struct bin_tree_t *right;
446146040Stjr  struct bin_tree_t *first;
447146040Stjr  struct bin_tree_t *next;
448146040Stjr
449146040Stjr  re_token_t token;
450146040Stjr
451146040Stjr  /* `node_idx' is the index in dfa->nodes, if `type' == 0.
452146040Stjr     Otherwise `type' indicate the type of this node.  */
453146040Stjr  int node_idx;
454146040Stjr};
455146040Stjrtypedef struct bin_tree_t bin_tree_t;
456146040Stjr
457146040Stjr#define BIN_TREE_STORAGE_SIZE \
458146040Stjr  ((1024 - sizeof (void *)) / sizeof (bin_tree_t))
459146040Stjr
460146040Stjrstruct bin_tree_storage_t
461146040Stjr{
462146040Stjr  struct bin_tree_storage_t *next;
463146040Stjr  bin_tree_t data[BIN_TREE_STORAGE_SIZE];
464146040Stjr};
465146040Stjrtypedef struct bin_tree_storage_t bin_tree_storage_t;
466146040Stjr
467146040Stjr#define CONTEXT_WORD 1
468146040Stjr#define CONTEXT_NEWLINE (CONTEXT_WORD << 1)
469146040Stjr#define CONTEXT_BEGBUF (CONTEXT_NEWLINE << 1)
470146040Stjr#define CONTEXT_ENDBUF (CONTEXT_BEGBUF << 1)
471146040Stjr
472146040Stjr#define IS_WORD_CONTEXT(c) ((c) & CONTEXT_WORD)
473146040Stjr#define IS_NEWLINE_CONTEXT(c) ((c) & CONTEXT_NEWLINE)
474146040Stjr#define IS_BEGBUF_CONTEXT(c) ((c) & CONTEXT_BEGBUF)
475146040Stjr#define IS_ENDBUF_CONTEXT(c) ((c) & CONTEXT_ENDBUF)
476146040Stjr#define IS_ORDINARY_CONTEXT(c) ((c) == 0)
477146040Stjr
478146040Stjr#define IS_WORD_CHAR(ch) (isalnum (ch) || (ch) == '_')
479146040Stjr#define IS_NEWLINE(ch) ((ch) == NEWLINE_CHAR)
480146040Stjr#define IS_WIDE_WORD_CHAR(ch) (iswalnum (ch) || (ch) == L'_')
481146040Stjr#define IS_WIDE_NEWLINE(ch) ((ch) == WIDE_NEWLINE_CHAR)
482146040Stjr
483146040Stjr#define NOT_SATISFY_PREV_CONSTRAINT(constraint,context) \
484146040Stjr ((((constraint) & PREV_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
485146040Stjr  || ((constraint & PREV_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
486146040Stjr  || ((constraint & PREV_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context))\
487146040Stjr  || ((constraint & PREV_BEGBUF_CONSTRAINT) && !IS_BEGBUF_CONTEXT (context)))
488146040Stjr
489146040Stjr#define NOT_SATISFY_NEXT_CONSTRAINT(constraint,context) \
490146040Stjr ((((constraint) & NEXT_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
491146040Stjr  || (((constraint) & NEXT_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
492146040Stjr  || (((constraint) & NEXT_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context)) \
493146040Stjr  || (((constraint) & NEXT_ENDBUF_CONSTRAINT) && !IS_ENDBUF_CONTEXT (context)))
494146040Stjr
495146040Stjrstruct re_dfastate_t
496146040Stjr{
497146040Stjr  unsigned int hash;
498146040Stjr  re_node_set nodes;
499146040Stjr  re_node_set non_eps_nodes;
500146040Stjr  re_node_set inveclosure;
501146040Stjr  re_node_set *entrance_nodes;
502146040Stjr  struct re_dfastate_t **trtable, **word_trtable;
503146040Stjr  unsigned int context : 4;
504146040Stjr  unsigned int halt : 1;
505146040Stjr  /* If this state can accept `multi byte'.
506146040Stjr     Note that we refer to multibyte characters, and multi character
507146040Stjr     collating elements as `multi byte'.  */
508146040Stjr  unsigned int accept_mb : 1;
509146040Stjr  /* If this state has backreference node(s).  */
510146040Stjr  unsigned int has_backref : 1;
511146040Stjr  unsigned int has_constraint : 1;
512146040Stjr};
513146040Stjrtypedef struct re_dfastate_t re_dfastate_t;
514146040Stjr
515146040Stjrstruct re_state_table_entry
516146040Stjr{
517146040Stjr  int num;
518146040Stjr  int alloc;
519146040Stjr  re_dfastate_t **array;
520146040Stjr};
521146040Stjr
522146040Stjr/* Array type used in re_sub_match_last_t and re_sub_match_top_t.  */
523146040Stjr
524146040Stjrtypedef struct
525146040Stjr{
526146040Stjr  int next_idx;
527146040Stjr  int alloc;
528146040Stjr  re_dfastate_t **array;
529146040Stjr} state_array_t;
530146040Stjr
531146040Stjr/* Store information about the node NODE whose type is OP_CLOSE_SUBEXP.  */
532146040Stjr
533146040Stjrtypedef struct
534146040Stjr{
535146040Stjr  int node;
536146040Stjr  int str_idx; /* The position NODE match at.  */
537146040Stjr  state_array_t path;
538146040Stjr} re_sub_match_last_t;
539146040Stjr
540146040Stjr/* Store information about the node NODE whose type is OP_OPEN_SUBEXP.
541146040Stjr   And information about the node, whose type is OP_CLOSE_SUBEXP,
542146040Stjr   corresponding to NODE is stored in LASTS.  */
543146040Stjr
544146040Stjrtypedef struct
545146040Stjr{
546146040Stjr  int str_idx;
547146040Stjr  int node;
548146040Stjr  state_array_t *path;
549146040Stjr  int alasts; /* Allocation size of LASTS.  */
550146040Stjr  int nlasts; /* The number of LASTS.  */
551146040Stjr  re_sub_match_last_t **lasts;
552146040Stjr} re_sub_match_top_t;
553146040Stjr
554146040Stjrstruct re_backref_cache_entry
555146040Stjr{
556146040Stjr  int node;
557146040Stjr  int str_idx;
558146040Stjr  int subexp_from;
559146040Stjr  int subexp_to;
560146040Stjr  char more;
561146040Stjr  char unused;
562146040Stjr  unsigned short int eps_reachable_subexps_map;
563146040Stjr};
564146040Stjr
565146040Stjrtypedef struct
566146040Stjr{
567146040Stjr  /* The string object corresponding to the input string.  */
568146040Stjr  re_string_t input;
569146040Stjr#if defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L)
570250724Sjkim  const re_dfa_t *const dfa;
571146040Stjr#else
572250724Sjkim  const re_dfa_t *dfa;
573146040Stjr#endif
574146040Stjr  /* EFLAGS of the argument of regexec.  */
575146040Stjr  int eflags;
576146040Stjr  /* Where the matching ends.  */
577146040Stjr  int match_last;
578146040Stjr  int last_node;
579146040Stjr  /* The state log used by the matcher.  */
580146040Stjr  re_dfastate_t **state_log;
581146040Stjr  int state_log_top;
582146040Stjr  /* Back reference cache.  */
583146040Stjr  int nbkref_ents;
584146040Stjr  int abkref_ents;
585146040Stjr  struct re_backref_cache_entry *bkref_ents;
586146040Stjr  int max_mb_elem_len;
587146040Stjr  int nsub_tops;
588146040Stjr  int asub_tops;
589146040Stjr  re_sub_match_top_t **sub_tops;
590146040Stjr} re_match_context_t;
591146040Stjr
592146040Stjrtypedef struct
593146040Stjr{
594146040Stjr  re_dfastate_t **sifted_states;
595146040Stjr  re_dfastate_t **limited_states;
596146040Stjr  int last_node;
597146040Stjr  int last_str_idx;
598146040Stjr  re_node_set limits;
599146040Stjr} re_sift_context_t;
600146040Stjr
601146040Stjrstruct re_fail_stack_ent_t
602146040Stjr{
603146040Stjr  int idx;
604146040Stjr  int node;
605146040Stjr  regmatch_t *regs;
606146040Stjr  re_node_set eps_via_nodes;
607146040Stjr};
608146040Stjr
609146040Stjrstruct re_fail_stack_t
610146040Stjr{
611146040Stjr  int num;
612146040Stjr  int alloc;
613146040Stjr  struct re_fail_stack_ent_t *stack;
614146040Stjr};
615146040Stjr
616146040Stjrstruct re_dfa_t
617146040Stjr{
618146040Stjr  re_token_t *nodes;
619250724Sjkim  size_t nodes_alloc;
620250724Sjkim  size_t nodes_len;
621146040Stjr  int *nexts;
622146040Stjr  int *org_indices;
623146040Stjr  re_node_set *edests;
624146040Stjr  re_node_set *eclosures;
625146040Stjr  re_node_set *inveclosures;
626146040Stjr  struct re_state_table_entry *state_table;
627146040Stjr  re_dfastate_t *init_state;
628146040Stjr  re_dfastate_t *init_state_word;
629146040Stjr  re_dfastate_t *init_state_nl;
630146040Stjr  re_dfastate_t *init_state_begbuf;
631146040Stjr  bin_tree_t *str_tree;
632146040Stjr  bin_tree_storage_t *str_tree_storage;
633146040Stjr  re_bitset_ptr_t sb_char;
634146040Stjr  int str_tree_storage_idx;
635146040Stjr
636146040Stjr  /* number of subexpressions `re_nsub' is in regex_t.  */
637146040Stjr  unsigned int state_hash_mask;
638146040Stjr  int init_node;
639146040Stjr  int nbackref; /* The number of backreference in this dfa.  */
640146040Stjr
641146040Stjr  /* Bitmap expressing which backreference is used.  */
642250724Sjkim  bitset_word_t used_bkref_map;
643250724Sjkim  bitset_word_t completed_bkref_map;
644146040Stjr
645146040Stjr  unsigned int has_plural_match : 1;
646146040Stjr  /* If this dfa has "multibyte node", which is a backreference or
647146040Stjr     a node which can accept multibyte character or multi character
648146040Stjr     collating element.  */
649146040Stjr  unsigned int has_mb_node : 1;
650146040Stjr  unsigned int is_utf8 : 1;
651146040Stjr  unsigned int map_notascii : 1;
652146040Stjr  unsigned int word_ops_used : 1;
653146040Stjr  int mb_cur_max;
654250724Sjkim  bitset_t word_char;
655146040Stjr  reg_syntax_t syntax;
656146040Stjr  int *subexp_map;
657146040Stjr#ifdef DEBUG
658146040Stjr  char* re_str;
659146040Stjr#endif
660250724Sjkim  __libc_lock_define (, lock)
661146040Stjr};
662146040Stjr
663146040Stjr#define re_node_set_init_empty(set) memset (set, '\0', sizeof (re_node_set))
664146040Stjr#define re_node_set_remove(set,id) \
665146040Stjr  (re_node_set_remove_at (set, re_node_set_contains (set, id) - 1))
666146040Stjr#define re_node_set_empty(p) ((p)->nelem = 0)
667146040Stjr#define re_node_set_free(set) re_free ((set)->elems)
668146040Stjr
669146040Stjr
670146040Stjrtypedef enum
671146040Stjr{
672146040Stjr  SB_CHAR,
673146040Stjr  MB_CHAR,
674146040Stjr  EQUIV_CLASS,
675146040Stjr  COLL_SYM,
676146040Stjr  CHAR_CLASS
677146040Stjr} bracket_elem_type;
678146040Stjr
679146040Stjrtypedef struct
680146040Stjr{
681146040Stjr  bracket_elem_type type;
682146040Stjr  union
683146040Stjr  {
684146040Stjr    unsigned char ch;
685146040Stjr    unsigned char *name;
686146040Stjr    wchar_t wch;
687146040Stjr  } opr;
688146040Stjr} bracket_elem_t;
689146040Stjr
690146040Stjr
691146040Stjr/* Inline functions for bitset operation.  */
692146040Stjrstatic inline void
693250724Sjkimbitset_not (bitset_t set)
694146040Stjr{
695146040Stjr  int bitset_i;
696250724Sjkim  for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
697146040Stjr    set[bitset_i] = ~set[bitset_i];
698146040Stjr}
699146040Stjr
700146040Stjrstatic inline void
701250724Sjkimbitset_merge (bitset_t dest, const bitset_t src)
702146040Stjr{
703146040Stjr  int bitset_i;
704250724Sjkim  for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
705146040Stjr    dest[bitset_i] |= src[bitset_i];
706146040Stjr}
707146040Stjr
708146040Stjrstatic inline void
709250724Sjkimbitset_mask (bitset_t dest, const bitset_t src)
710146040Stjr{
711146040Stjr  int bitset_i;
712250724Sjkim  for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
713146040Stjr    dest[bitset_i] &= src[bitset_i];
714146040Stjr}
715146040Stjr
716250724Sjkim#ifdef RE_ENABLE_I18N
717146040Stjr/* Inline functions for re_string.  */
718146040Stjrstatic inline int
719250724Sjkiminternal_function __attribute ((pure))
720146040Stjrre_string_char_size_at (const re_string_t *pstr, int idx)
721146040Stjr{
722146040Stjr  int byte_idx;
723146040Stjr  if (pstr->mb_cur_max == 1)
724146040Stjr    return 1;
725146040Stjr  for (byte_idx = 1; idx + byte_idx < pstr->valid_len; ++byte_idx)
726146040Stjr    if (pstr->wcs[idx + byte_idx] != WEOF)
727146040Stjr      break;
728146040Stjr  return byte_idx;
729146040Stjr}
730146040Stjr
731146040Stjrstatic inline wint_t
732250724Sjkiminternal_function __attribute ((pure))
733146040Stjrre_string_wchar_at (const re_string_t *pstr, int idx)
734146040Stjr{
735146040Stjr  if (pstr->mb_cur_max == 1)
736146040Stjr    return (wint_t) pstr->mbs[idx];
737146040Stjr  return (wint_t) pstr->wcs[idx];
738146040Stjr}
739146040Stjr
740250724Sjkim# ifndef NOT_IN_libc
741146040Stjrstatic int
742250724Sjkiminternal_function __attribute ((pure))
743146040Stjrre_string_elem_size_at (const re_string_t *pstr, int idx)
744146040Stjr{
745250724Sjkim#  ifdef _LIBC
746146040Stjr  const unsigned char *p, *extra;
747146040Stjr  const int32_t *table, *indirect;
748250724Sjkim#   include <locale/weight.h>
749146040Stjr  uint_fast32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
750146040Stjr
751146040Stjr  if (nrules != 0)
752146040Stjr    {
753146040Stjr      table = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
754146040Stjr      extra = (const unsigned char *)
755146040Stjr	_NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB);
756146040Stjr      indirect = (const int32_t *) _NL_CURRENT (LC_COLLATE,
757146040Stjr						_NL_COLLATE_INDIRECTMB);
758146040Stjr      p = pstr->mbs + idx;
759250724Sjkim      findidx (&p, pstr->len - idx);
760146040Stjr      return p - pstr->mbs - idx;
761146040Stjr    }
762146040Stjr  else
763250724Sjkim#  endif /* _LIBC */
764146040Stjr    return 1;
765146040Stjr}
766250724Sjkim# endif
767146040Stjr#endif /* RE_ENABLE_I18N */
768146040Stjr
769146040Stjr#endif /*  _REGEX_INTERNAL_H */
770