Back to index

glibc  2.9
regex_internal.h
Go to the documentation of this file.
00001 /* Extended regular expression matching and search library.
00002    Copyright (C) 2002, 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #ifndef _REGEX_INTERNAL_H
00022 #define _REGEX_INTERNAL_H 1
00023 
00024 #include <assert.h>
00025 #include <ctype.h>
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 
00030 #if defined HAVE_LANGINFO_H || defined HAVE_LANGINFO_CODESET || defined _LIBC
00031 # include <langinfo.h>
00032 #endif
00033 #if defined HAVE_LOCALE_H || defined _LIBC
00034 # include <locale.h>
00035 #endif
00036 #if defined HAVE_WCHAR_H || defined _LIBC
00037 # include <wchar.h>
00038 #endif /* HAVE_WCHAR_H || _LIBC */
00039 #if defined HAVE_WCTYPE_H || defined _LIBC
00040 # include <wctype.h>
00041 #endif /* HAVE_WCTYPE_H || _LIBC */
00042 #if defined HAVE_STDBOOL_H || defined _LIBC
00043 # include <stdbool.h>
00044 #endif /* HAVE_STDBOOL_H || _LIBC */
00045 #if defined HAVE_STDINT_H || defined _LIBC
00046 # include <stdint.h>
00047 #endif /* HAVE_STDINT_H || _LIBC */
00048 #if defined _LIBC
00049 # include <bits/libc-lock.h>
00050 #else
00051 # define __libc_lock_define(CLASS,NAME)
00052 # define __libc_lock_init(NAME) do { } while (0)
00053 # define __libc_lock_lock(NAME) do { } while (0)
00054 # define __libc_lock_unlock(NAME) do { } while (0)
00055 #endif
00056 
00057 /* In case that the system doesn't have isblank().  */
00058 #if !defined _LIBC && !defined HAVE_ISBLANK && !defined isblank
00059 # define isblank(ch) ((ch) == ' ' || (ch) == '\t')
00060 #endif
00061 
00062 #ifdef _LIBC
00063 # ifndef _RE_DEFINE_LOCALE_FUNCTIONS
00064 #  define _RE_DEFINE_LOCALE_FUNCTIONS 1
00065 #   include <locale/localeinfo.h>
00066 #   include <locale/elem-hash.h>
00067 #   include <locale/coll-lookup.h>
00068 # endif
00069 #endif
00070 
00071 /* This is for other GNU distributions with internationalized messages.  */
00072 #if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
00073 # include <libintl.h>
00074 # ifdef _LIBC
00075 #  undef gettext
00076 #  define gettext(msgid) \
00077   INTUSE(__dcgettext) (_libc_intl_domainname, msgid, LC_MESSAGES)
00078 # endif
00079 #else
00080 # define gettext(msgid) (msgid)
00081 #endif
00082 
00083 #ifndef gettext_noop
00084 /* This define is so xgettext can find the internationalizable
00085    strings.  */
00086 # define gettext_noop(String) String
00087 #endif
00088 
00089 /* For loser systems without the definition.  */
00090 #ifndef SIZE_MAX
00091 # define SIZE_MAX ((size_t) -1)
00092 #endif
00093 
00094 #if (defined MB_CUR_MAX && HAVE_LOCALE_H && HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_WCRTOMB && HAVE_MBRTOWC && HAVE_WCSCOLL) || _LIBC
00095 # define RE_ENABLE_I18N
00096 #endif
00097 
00098 #if __GNUC__ >= 3
00099 # define BE(expr, val) __builtin_expect (expr, val)
00100 #else
00101 # define BE(expr, val) (expr)
00102 # define inline
00103 #endif
00104 
00105 /* Number of single byte character.  */
00106 #define SBC_MAX 256
00107 
00108 #define COLL_ELEM_LEN_MAX 8
00109 
00110 /* The character which represents newline.  */
00111 #define NEWLINE_CHAR '\n'
00112 #define WIDE_NEWLINE_CHAR L'\n'
00113 
00114 /* Rename to standard API for using out of glibc.  */
00115 #ifndef _LIBC
00116 # define __wctype wctype
00117 # define __iswctype iswctype
00118 # define __btowc btowc
00119 # define __mempcpy mempcpy
00120 # define __wcrtomb wcrtomb
00121 # define __regfree regfree
00122 # define attribute_hidden
00123 #endif /* not _LIBC */
00124 
00125 #ifdef __GNUC__
00126 # define __attribute(arg) __attribute__ (arg)
00127 #else
00128 # define __attribute(arg)
00129 #endif
00130 
00131 extern const char __re_error_msgid[] attribute_hidden;
00132 extern const size_t __re_error_msgid_idx[] attribute_hidden;
00133 
00134 /* An integer used to represent a set of bits.  It must be unsigned,
00135    and must be at least as wide as unsigned int.  */
00136 typedef unsigned long int bitset_word_t;
00137 /* All bits set in a bitset_word_t.  */
00138 #define BITSET_WORD_MAX ULONG_MAX
00139 /* Number of bits in a bitset_word_t.  */
00140 #define BITSET_WORD_BITS (sizeof (bitset_word_t) * CHAR_BIT)
00141 /* Number of bitset_word_t in a bit_set.  */
00142 #define BITSET_WORDS (SBC_MAX / BITSET_WORD_BITS)
00143 typedef bitset_word_t bitset_t[BITSET_WORDS];
00144 typedef bitset_word_t *re_bitset_ptr_t;
00145 typedef const bitset_word_t *re_const_bitset_ptr_t;
00146 
00147 #define bitset_set(set,i) \
00148   (set[i / BITSET_WORD_BITS] |= (bitset_word_t) 1 << i % BITSET_WORD_BITS)
00149 #define bitset_clear(set,i) \
00150   (set[i / BITSET_WORD_BITS] &= ~((bitset_word_t) 1 << i % BITSET_WORD_BITS))
00151 #define bitset_contain(set,i) \
00152   (set[i / BITSET_WORD_BITS] & ((bitset_word_t) 1 << i % BITSET_WORD_BITS))
00153 #define bitset_empty(set) memset (set, '\0', sizeof (bitset_t))
00154 #define bitset_set_all(set) memset (set, '\xff', sizeof (bitset_t))
00155 #define bitset_copy(dest,src) memcpy (dest, src, sizeof (bitset_t))
00156 
00157 #define PREV_WORD_CONSTRAINT 0x0001
00158 #define PREV_NOTWORD_CONSTRAINT 0x0002
00159 #define NEXT_WORD_CONSTRAINT 0x0004
00160 #define NEXT_NOTWORD_CONSTRAINT 0x0008
00161 #define PREV_NEWLINE_CONSTRAINT 0x0010
00162 #define NEXT_NEWLINE_CONSTRAINT 0x0020
00163 #define PREV_BEGBUF_CONSTRAINT 0x0040
00164 #define NEXT_ENDBUF_CONSTRAINT 0x0080
00165 #define WORD_DELIM_CONSTRAINT 0x0100
00166 #define NOT_WORD_DELIM_CONSTRAINT 0x0200
00167 
00168 typedef enum
00169 {
00170   INSIDE_WORD = PREV_WORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
00171   WORD_FIRST = PREV_NOTWORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
00172   WORD_LAST = PREV_WORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
00173   INSIDE_NOTWORD = PREV_NOTWORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
00174   LINE_FIRST = PREV_NEWLINE_CONSTRAINT,
00175   LINE_LAST = NEXT_NEWLINE_CONSTRAINT,
00176   BUF_FIRST = PREV_BEGBUF_CONSTRAINT,
00177   BUF_LAST = NEXT_ENDBUF_CONSTRAINT,
00178   WORD_DELIM = WORD_DELIM_CONSTRAINT,
00179   NOT_WORD_DELIM = NOT_WORD_DELIM_CONSTRAINT
00180 } re_context_type;
00181 
00182 typedef struct
00183 {
00184   int alloc;
00185   int nelem;
00186   int *elems;
00187 } re_node_set;
00188 
00189 typedef enum
00190 {
00191   NON_TYPE = 0,
00192 
00193   /* Node type, These are used by token, node, tree.  */
00194   CHARACTER = 1,
00195   END_OF_RE = 2,
00196   SIMPLE_BRACKET = 3,
00197   OP_BACK_REF = 4,
00198   OP_PERIOD = 5,
00199 #ifdef RE_ENABLE_I18N
00200   COMPLEX_BRACKET = 6,
00201   OP_UTF8_PERIOD = 7,
00202 #endif /* RE_ENABLE_I18N */
00203 
00204   /* We define EPSILON_BIT as a macro so that OP_OPEN_SUBEXP is used
00205      when the debugger shows values of this enum type.  */
00206 #define EPSILON_BIT 8
00207   OP_OPEN_SUBEXP = EPSILON_BIT | 0,
00208   OP_CLOSE_SUBEXP = EPSILON_BIT | 1,
00209   OP_ALT = EPSILON_BIT | 2,
00210   OP_DUP_ASTERISK = EPSILON_BIT | 3,
00211   ANCHOR = EPSILON_BIT | 4,
00212 
00213   /* Tree type, these are used only by tree. */
00214   CONCAT = 16,
00215   SUBEXP = 17,
00216 
00217   /* Token type, these are used only by token.  */
00218   OP_DUP_PLUS = 18,
00219   OP_DUP_QUESTION,
00220   OP_OPEN_BRACKET,
00221   OP_CLOSE_BRACKET,
00222   OP_CHARSET_RANGE,
00223   OP_OPEN_DUP_NUM,
00224   OP_CLOSE_DUP_NUM,
00225   OP_NON_MATCH_LIST,
00226   OP_OPEN_COLL_ELEM,
00227   OP_CLOSE_COLL_ELEM,
00228   OP_OPEN_EQUIV_CLASS,
00229   OP_CLOSE_EQUIV_CLASS,
00230   OP_OPEN_CHAR_CLASS,
00231   OP_CLOSE_CHAR_CLASS,
00232   OP_WORD,
00233   OP_NOTWORD,
00234   OP_SPACE,
00235   OP_NOTSPACE,
00236   BACK_SLASH
00237 
00238 } re_token_type_t;
00239 
00240 #ifdef RE_ENABLE_I18N
00241 typedef struct
00242 {
00243   /* Multibyte characters.  */
00244   wchar_t *mbchars;
00245 
00246   /* Collating symbols.  */
00247 # ifdef _LIBC
00248   int32_t *coll_syms;
00249 # endif
00250 
00251   /* Equivalence classes. */
00252 # ifdef _LIBC
00253   int32_t *equiv_classes;
00254 # endif
00255 
00256   /* Range expressions. */
00257 # ifdef _LIBC
00258   uint32_t *range_starts;
00259   uint32_t *range_ends;
00260 # else /* not _LIBC */
00261   wchar_t *range_starts;
00262   wchar_t *range_ends;
00263 # endif /* not _LIBC */
00264 
00265   /* Character classes. */
00266   wctype_t *char_classes;
00267 
00268   /* If this character set is the non-matching list.  */
00269   unsigned int non_match : 1;
00270 
00271   /* # of multibyte characters.  */
00272   int nmbchars;
00273 
00274   /* # of collating symbols.  */
00275   int ncoll_syms;
00276 
00277   /* # of equivalence classes. */
00278   int nequiv_classes;
00279 
00280   /* # of range expressions. */
00281   int nranges;
00282 
00283   /* # of character classes. */
00284   int nchar_classes;
00285 } re_charset_t;
00286 #endif /* RE_ENABLE_I18N */
00287 
00288 typedef struct
00289 {
00290   union
00291   {
00292     unsigned char c;        /* for CHARACTER */
00293     re_bitset_ptr_t sbcset; /* for SIMPLE_BRACKET */
00294 #ifdef RE_ENABLE_I18N
00295     re_charset_t *mbcset;   /* for COMPLEX_BRACKET */
00296 #endif /* RE_ENABLE_I18N */
00297     int idx;                /* for BACK_REF */
00298     re_context_type ctx_type;      /* for ANCHOR */
00299   } opr;
00300 #if __GNUC__ >= 2
00301   re_token_type_t type : 8;
00302 #else
00303   re_token_type_t type;
00304 #endif
00305   unsigned int constraint : 10;    /* context constraint */
00306   unsigned int duplicated : 1;
00307   unsigned int opt_subexp : 1;
00308 #ifdef RE_ENABLE_I18N
00309   unsigned int accept_mb : 1;
00310   /* These 2 bits can be moved into the union if needed (e.g. if running out
00311      of bits; move opr.c to opr.c.c and move the flags to opr.c.flags).  */
00312   unsigned int mb_partial : 1;
00313 #endif
00314   unsigned int word_char : 1;
00315 } re_token_t;
00316 
00317 #define IS_EPSILON_NODE(type) ((type) & EPSILON_BIT)
00318 
00319 struct re_string_t
00320 {
00321   /* Indicate the raw buffer which is the original string passed as an
00322      argument of regexec(), re_search(), etc..  */
00323   const unsigned char *raw_mbs;
00324   /* Store the multibyte string.  In case of "case insensitive mode" like
00325      REG_ICASE, upper cases of the string are stored, otherwise MBS points
00326      the same address that RAW_MBS points.  */
00327   unsigned char *mbs;
00328 #ifdef RE_ENABLE_I18N
00329   /* Store the wide character string which is corresponding to MBS.  */
00330   wint_t *wcs;
00331   int *offsets;
00332   mbstate_t cur_state;
00333 #endif
00334   /* Index in RAW_MBS.  Each character mbs[i] corresponds to
00335      raw_mbs[raw_mbs_idx + i].  */
00336   int raw_mbs_idx;
00337   /* The length of the valid characters in the buffers.  */
00338   int valid_len;
00339   /* The corresponding number of bytes in raw_mbs array.  */
00340   int valid_raw_len;
00341   /* The length of the buffers MBS and WCS.  */
00342   int bufs_len;
00343   /* The index in MBS, which is updated by re_string_fetch_byte.  */
00344   int cur_idx;
00345   /* length of RAW_MBS array.  */
00346   int raw_len;
00347   /* This is RAW_LEN - RAW_MBS_IDX + VALID_LEN - VALID_RAW_LEN.  */
00348   int len;
00349   /* End of the buffer may be shorter than its length in the cases such
00350      as re_match_2, re_search_2.  Then, we use STOP for end of the buffer
00351      instead of LEN.  */
00352   int raw_stop;
00353   /* This is RAW_STOP - RAW_MBS_IDX adjusted through OFFSETS.  */
00354   int stop;
00355 
00356   /* The context of mbs[0].  We store the context independently, since
00357      the context of mbs[0] may be different from raw_mbs[0], which is
00358      the beginning of the input string.  */
00359   unsigned int tip_context;
00360   /* The translation passed as a part of an argument of re_compile_pattern.  */
00361   RE_TRANSLATE_TYPE trans;
00362   /* Copy of re_dfa_t's word_char.  */
00363   re_const_bitset_ptr_t word_char;
00364   /* 1 if REG_ICASE.  */
00365   unsigned char icase;
00366   unsigned char is_utf8;
00367   unsigned char map_notascii;
00368   unsigned char mbs_allocated;
00369   unsigned char offsets_needed;
00370   unsigned char newline_anchor;
00371   unsigned char word_ops_used;
00372   int mb_cur_max;
00373 };
00374 typedef struct re_string_t re_string_t;
00375 
00376 
00377 struct re_dfa_t;
00378 typedef struct re_dfa_t re_dfa_t;
00379 
00380 #ifndef _LIBC
00381 # ifdef __i386__
00382 #  define internal_function   __attribute ((regparm (3), stdcall))
00383 # else
00384 #  define internal_function
00385 # endif
00386 #endif
00387 
00388 #ifndef NOT_IN_libc
00389 static reg_errcode_t re_string_realloc_buffers (re_string_t *pstr,
00390                                           int new_buf_len)
00391      internal_function;
00392 # ifdef RE_ENABLE_I18N
00393 static void build_wcs_buffer (re_string_t *pstr) internal_function;
00394 static int build_wcs_upper_buffer (re_string_t *pstr) internal_function;
00395 # endif /* RE_ENABLE_I18N */
00396 static void build_upper_buffer (re_string_t *pstr) internal_function;
00397 static void re_string_translate_buffer (re_string_t *pstr) internal_function;
00398 static unsigned int re_string_context_at (const re_string_t *input, int idx,
00399                                      int eflags)
00400      internal_function __attribute ((pure));
00401 #endif
00402 #define re_string_peek_byte(pstr, offset) \
00403   ((pstr)->mbs[(pstr)->cur_idx + offset])
00404 #define re_string_fetch_byte(pstr) \
00405   ((pstr)->mbs[(pstr)->cur_idx++])
00406 #define re_string_first_byte(pstr, idx) \
00407   ((idx) == (pstr)->valid_len || (pstr)->wcs[idx] != WEOF)
00408 #define re_string_is_single_byte_char(pstr, idx) \
00409   ((pstr)->wcs[idx] != WEOF && ((pstr)->valid_len == (idx) + 1 \
00410                             || (pstr)->wcs[(idx) + 1] != WEOF))
00411 #define re_string_eoi(pstr) ((pstr)->stop <= (pstr)->cur_idx)
00412 #define re_string_cur_idx(pstr) ((pstr)->cur_idx)
00413 #define re_string_get_buffer(pstr) ((pstr)->mbs)
00414 #define re_string_length(pstr) ((pstr)->len)
00415 #define re_string_byte_at(pstr,idx) ((pstr)->mbs[idx])
00416 #define re_string_skip_bytes(pstr,idx) ((pstr)->cur_idx += (idx))
00417 #define re_string_set_index(pstr,idx) ((pstr)->cur_idx = (idx))
00418 
00419 #include <alloca.h>
00420 
00421 #ifndef _LIBC
00422 # if HAVE_ALLOCA
00423 /* The OS usually guarantees only one guard page at the bottom of the stack,
00424    and a page size can be as small as 4096 bytes.  So we cannot safely
00425    allocate anything larger than 4096 bytes.  Also care for the possibility
00426    of a few compiler-allocated temporary stack slots.  */
00427 #  define __libc_use_alloca(n) ((n) < 4032)
00428 # else
00429 /* alloca is implemented with malloc, so just use malloc.  */
00430 #  define __libc_use_alloca(n) 0
00431 # endif
00432 #endif
00433 
00434 #define re_malloc(t,n) ((t *) malloc ((n) * sizeof (t)))
00435 #define re_realloc(p,t,n) ((t *) realloc (p, (n) * sizeof (t)))
00436 #define re_free(p) free (p)
00437 
00438 struct bin_tree_t
00439 {
00440   struct bin_tree_t *parent;
00441   struct bin_tree_t *left;
00442   struct bin_tree_t *right;
00443   struct bin_tree_t *first;
00444   struct bin_tree_t *next;
00445 
00446   re_token_t token;
00447 
00448   /* `node_idx' is the index in dfa->nodes, if `type' == 0.
00449      Otherwise `type' indicate the type of this node.  */
00450   int node_idx;
00451 };
00452 typedef struct bin_tree_t bin_tree_t;
00453 
00454 #define BIN_TREE_STORAGE_SIZE \
00455   ((1024 - sizeof (void *)) / sizeof (bin_tree_t))
00456 
00457 struct bin_tree_storage_t
00458 {
00459   struct bin_tree_storage_t *next;
00460   bin_tree_t data[BIN_TREE_STORAGE_SIZE];
00461 };
00462 typedef struct bin_tree_storage_t bin_tree_storage_t;
00463 
00464 #define CONTEXT_WORD 1
00465 #define CONTEXT_NEWLINE (CONTEXT_WORD << 1)
00466 #define CONTEXT_BEGBUF (CONTEXT_NEWLINE << 1)
00467 #define CONTEXT_ENDBUF (CONTEXT_BEGBUF << 1)
00468 
00469 #define IS_WORD_CONTEXT(c) ((c) & CONTEXT_WORD)
00470 #define IS_NEWLINE_CONTEXT(c) ((c) & CONTEXT_NEWLINE)
00471 #define IS_BEGBUF_CONTEXT(c) ((c) & CONTEXT_BEGBUF)
00472 #define IS_ENDBUF_CONTEXT(c) ((c) & CONTEXT_ENDBUF)
00473 #define IS_ORDINARY_CONTEXT(c) ((c) == 0)
00474 
00475 #define IS_WORD_CHAR(ch) (isalnum (ch) || (ch) == '_')
00476 #define IS_NEWLINE(ch) ((ch) == NEWLINE_CHAR)
00477 #define IS_WIDE_WORD_CHAR(ch) (iswalnum (ch) || (ch) == L'_')
00478 #define IS_WIDE_NEWLINE(ch) ((ch) == WIDE_NEWLINE_CHAR)
00479 
00480 #define NOT_SATISFY_PREV_CONSTRAINT(constraint,context) \
00481  ((((constraint) & PREV_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
00482   || ((constraint & PREV_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
00483   || ((constraint & PREV_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context))\
00484   || ((constraint & PREV_BEGBUF_CONSTRAINT) && !IS_BEGBUF_CONTEXT (context)))
00485 
00486 #define NOT_SATISFY_NEXT_CONSTRAINT(constraint,context) \
00487  ((((constraint) & NEXT_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
00488   || (((constraint) & NEXT_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
00489   || (((constraint) & NEXT_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context)) \
00490   || (((constraint) & NEXT_ENDBUF_CONSTRAINT) && !IS_ENDBUF_CONTEXT (context)))
00491 
00492 struct re_dfastate_t
00493 {
00494   unsigned int hash;
00495   re_node_set nodes;
00496   re_node_set non_eps_nodes;
00497   re_node_set inveclosure;
00498   re_node_set *entrance_nodes;
00499   struct re_dfastate_t **trtable, **word_trtable;
00500   unsigned int context : 4;
00501   unsigned int halt : 1;
00502   /* If this state can accept `multi byte'.
00503      Note that we refer to multibyte characters, and multi character
00504      collating elements as `multi byte'.  */
00505   unsigned int accept_mb : 1;
00506   /* If this state has backreference node(s).  */
00507   unsigned int has_backref : 1;
00508   unsigned int has_constraint : 1;
00509 };
00510 typedef struct re_dfastate_t re_dfastate_t;
00511 
00512 struct re_state_table_entry
00513 {
00514   int num;
00515   int alloc;
00516   re_dfastate_t **array;
00517 };
00518 
00519 /* Array type used in re_sub_match_last_t and re_sub_match_top_t.  */
00520 
00521 typedef struct
00522 {
00523   int next_idx;
00524   int alloc;
00525   re_dfastate_t **array;
00526 } state_array_t;
00527 
00528 /* Store information about the node NODE whose type is OP_CLOSE_SUBEXP.  */
00529 
00530 typedef struct
00531 {
00532   int node;
00533   int str_idx; /* The position NODE match at.  */
00534   state_array_t path;
00535 } re_sub_match_last_t;
00536 
00537 /* Store information about the node NODE whose type is OP_OPEN_SUBEXP.
00538    And information about the node, whose type is OP_CLOSE_SUBEXP,
00539    corresponding to NODE is stored in LASTS.  */
00540 
00541 typedef struct
00542 {
00543   int str_idx;
00544   int node;
00545   state_array_t *path;
00546   int alasts; /* Allocation size of LASTS.  */
00547   int nlasts; /* The number of LASTS.  */
00548   re_sub_match_last_t **lasts;
00549 } re_sub_match_top_t;
00550 
00551 struct re_backref_cache_entry
00552 {
00553   int node;
00554   int str_idx;
00555   int subexp_from;
00556   int subexp_to;
00557   char more;
00558   char unused;
00559   unsigned short int eps_reachable_subexps_map;
00560 };
00561 
00562 typedef struct
00563 {
00564   /* The string object corresponding to the input string.  */
00565   re_string_t input;
00566 #if defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L)
00567   const re_dfa_t *const dfa;
00568 #else
00569   const re_dfa_t *dfa;
00570 #endif
00571   /* EFLAGS of the argument of regexec.  */
00572   int eflags;
00573   /* Where the matching ends.  */
00574   int match_last;
00575   int last_node;
00576   /* The state log used by the matcher.  */
00577   re_dfastate_t **state_log;
00578   int state_log_top;
00579   /* Back reference cache.  */
00580   int nbkref_ents;
00581   int abkref_ents;
00582   struct re_backref_cache_entry *bkref_ents;
00583   int max_mb_elem_len;
00584   int nsub_tops;
00585   int asub_tops;
00586   re_sub_match_top_t **sub_tops;
00587 } re_match_context_t;
00588 
00589 typedef struct
00590 {
00591   re_dfastate_t **sifted_states;
00592   re_dfastate_t **limited_states;
00593   int last_node;
00594   int last_str_idx;
00595   re_node_set limits;
00596 } re_sift_context_t;
00597 
00598 struct re_fail_stack_ent_t
00599 {
00600   int idx;
00601   int node;
00602   regmatch_t *regs;
00603   re_node_set eps_via_nodes;
00604 };
00605 
00606 struct re_fail_stack_t
00607 {
00608   int num;
00609   int alloc;
00610   struct re_fail_stack_ent_t *stack;
00611 };
00612 
00613 struct re_dfa_t
00614 {
00615   re_token_t *nodes;
00616   size_t nodes_alloc;
00617   size_t nodes_len;
00618   int *nexts;
00619   int *org_indices;
00620   re_node_set *edests;
00621   re_node_set *eclosures;
00622   re_node_set *inveclosures;
00623   struct re_state_table_entry *state_table;
00624   re_dfastate_t *init_state;
00625   re_dfastate_t *init_state_word;
00626   re_dfastate_t *init_state_nl;
00627   re_dfastate_t *init_state_begbuf;
00628   bin_tree_t *str_tree;
00629   bin_tree_storage_t *str_tree_storage;
00630   re_bitset_ptr_t sb_char;
00631   int str_tree_storage_idx;
00632 
00633   /* number of subexpressions `re_nsub' is in regex_t.  */
00634   unsigned int state_hash_mask;
00635   int init_node;
00636   int nbackref; /* The number of backreference in this dfa.  */
00637 
00638   /* Bitmap expressing which backreference is used.  */
00639   bitset_word_t used_bkref_map;
00640   bitset_word_t completed_bkref_map;
00641 
00642   unsigned int has_plural_match : 1;
00643   /* If this dfa has "multibyte node", which is a backreference or
00644      a node which can accept multibyte character or multi character
00645      collating element.  */
00646   unsigned int has_mb_node : 1;
00647   unsigned int is_utf8 : 1;
00648   unsigned int map_notascii : 1;
00649   unsigned int word_ops_used : 1;
00650   int mb_cur_max;
00651   bitset_t word_char;
00652   reg_syntax_t syntax;
00653   int *subexp_map;
00654 #ifdef DEBUG
00655   char* re_str;
00656 #endif
00657   __libc_lock_define (, lock)
00658 };
00659 
00660 #define re_node_set_init_empty(set) memset (set, '\0', sizeof (re_node_set))
00661 #define re_node_set_remove(set,id) \
00662   (re_node_set_remove_at (set, re_node_set_contains (set, id) - 1))
00663 #define re_node_set_empty(p) ((p)->nelem = 0)
00664 #define re_node_set_free(set) re_free ((set)->elems)
00665 
00666 
00667 typedef enum
00668 {
00669   SB_CHAR,
00670   MB_CHAR,
00671   EQUIV_CLASS,
00672   COLL_SYM,
00673   CHAR_CLASS
00674 } bracket_elem_type;
00675 
00676 typedef struct
00677 {
00678   bracket_elem_type type;
00679   union
00680   {
00681     unsigned char ch;
00682     unsigned char *name;
00683     wchar_t wch;
00684   } opr;
00685 } bracket_elem_t;
00686 
00687 
00688 /* Inline functions for bitset operation.  */
00689 static inline void
00690 bitset_not (bitset_t set)
00691 {
00692   int bitset_i;
00693   for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
00694     set[bitset_i] = ~set[bitset_i];
00695 }
00696 
00697 static inline void
00698 bitset_merge (bitset_t dest, const bitset_t src)
00699 {
00700   int bitset_i;
00701   for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
00702     dest[bitset_i] |= src[bitset_i];
00703 }
00704 
00705 static inline void
00706 bitset_mask (bitset_t dest, const bitset_t src)
00707 {
00708   int bitset_i;
00709   for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
00710     dest[bitset_i] &= src[bitset_i];
00711 }
00712 
00713 #ifdef RE_ENABLE_I18N
00714 /* Inline functions for re_string.  */
00715 static inline int
00716 internal_function __attribute ((pure))
00717 re_string_char_size_at (const re_string_t *pstr, int idx)
00718 {
00719   int byte_idx;
00720   if (pstr->mb_cur_max == 1)
00721     return 1;
00722   for (byte_idx = 1; idx + byte_idx < pstr->valid_len; ++byte_idx)
00723     if (pstr->wcs[idx + byte_idx] != WEOF)
00724       break;
00725   return byte_idx;
00726 }
00727 
00728 static inline wint_t
00729 internal_function __attribute ((pure))
00730 re_string_wchar_at (const re_string_t *pstr, int idx)
00731 {
00732   if (pstr->mb_cur_max == 1)
00733     return (wint_t) pstr->mbs[idx];
00734   return (wint_t) pstr->wcs[idx];
00735 }
00736 
00737 # ifndef NOT_IN_libc
00738 static int
00739 internal_function __attribute ((pure))
00740 re_string_elem_size_at (const re_string_t *pstr, int idx)
00741 {
00742 #  ifdef _LIBC
00743   const unsigned char *p, *extra;
00744   const int32_t *table, *indirect;
00745   int32_t tmp;
00746 #   include <locale/weight.h>
00747   uint_fast32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
00748 
00749   if (nrules != 0)
00750     {
00751       table = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
00752       extra = (const unsigned char *)
00753        _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB);
00754       indirect = (const int32_t *) _NL_CURRENT (LC_COLLATE,
00755                                           _NL_COLLATE_INDIRECTMB);
00756       p = pstr->mbs + idx;
00757       tmp = findidx (&p);
00758       return p - pstr->mbs - idx;
00759     }
00760   else
00761 #  endif /* _LIBC */
00762     return 1;
00763 }
00764 # endif
00765 #endif /* RE_ENABLE_I18N */
00766 
00767 #endif /*  _REGEX_INTERNAL_H */