regex_internal.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. /* Extended regular expression matching and search library.
  2. Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #ifndef _REGEX_INTERNAL_H
  18. #define _REGEX_INTERNAL_H 1
  19. #include <assert.h>
  20. #include <ctype.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #if defined HAVE_LANGINFO_H || defined HAVE_LANGINFO_CODESET
  24. # include <langinfo.h>
  25. #endif
  26. #if defined HAVE_LOCALE_H
  27. # include <locale.h>
  28. #endif
  29. #if defined HAVE_WCHAR_H
  30. # include <wchar.h>
  31. #endif
  32. #if defined HAVE_WCTYPE_H
  33. # include <wctype.h>
  34. #endif
  35. #if defined HAVE_STDBOOL_H
  36. # include <stdbool.h>
  37. #endif
  38. #if defined HAVE_STDINT_H
  39. # include <stdint.h>
  40. #endif
  41. #ifdef __UCLIBC_HAS_THREADS__
  42. #include <bits/libc-lock.h>
  43. #else
  44. #define __libc_lock_define(CLASS, NAME)
  45. #define __libc_lock_init(NAME) do { } while (0)
  46. #define __libc_lock_lock(NAME) do { } while (0)
  47. #define __libc_lock_unlock(NAME) do { } while (0)
  48. #endif
  49. #undef gettext
  50. #undef gettext_noop
  51. #define gettext(msgid) (msgid)
  52. #define gettext_noop(String) String
  53. #if (defined MB_CUR_MAX && HAVE_LOCALE_H && HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_WCRTOMB && HAVE_MBRTOWC && HAVE_WCSCOLL)
  54. # define RE_ENABLE_I18N
  55. #endif
  56. #if __GNUC__ >= 3
  57. /* uclibc: lean towards smaller size a bit:
  58. * OFF: # define BE(expr, val) __builtin_expect (expr, val) */
  59. # define BE(expr, val) (expr)
  60. #else
  61. # define BE(expr, val) (expr)
  62. # define inline
  63. #endif
  64. /* Number of single byte character. */
  65. #define SBC_MAX 256
  66. #define COLL_ELEM_LEN_MAX 8
  67. /* The character which represents newline. */
  68. #define NEWLINE_CHAR '\n'
  69. #define WIDE_NEWLINE_CHAR L'\n'
  70. #ifdef __GNUC__
  71. # define __attribute(arg) __attribute__ (arg)
  72. #else
  73. # define __attribute(arg)
  74. #endif
  75. /* An integer used to represent a set of bits. It must be unsigned,
  76. and must be at least as wide as unsigned int. */
  77. typedef unsigned long int bitset_word_t;
  78. /* All bits set in a bitset_word_t. */
  79. #define BITSET_WORD_MAX ULONG_MAX
  80. /* Number of bits in a bitset_word_t. */
  81. #define BITSET_WORD_BITS (sizeof (bitset_word_t) * CHAR_BIT)
  82. /* Number of bitset_word_t in a bit_set. */
  83. #define BITSET_WORDS (SBC_MAX / BITSET_WORD_BITS)
  84. typedef bitset_word_t bitset_t[BITSET_WORDS];
  85. typedef bitset_word_t *re_bitset_ptr_t;
  86. typedef const bitset_word_t *re_const_bitset_ptr_t;
  87. #define bitset_set(set,i) \
  88. (set[i / BITSET_WORD_BITS] |= (bitset_word_t) 1 << i % BITSET_WORD_BITS)
  89. #define bitset_clear(set,i) \
  90. (set[i / BITSET_WORD_BITS] &= ~((bitset_word_t) 1 << i % BITSET_WORD_BITS))
  91. #define bitset_contain(set,i) \
  92. (set[i / BITSET_WORD_BITS] & ((bitset_word_t) 1 << i % BITSET_WORD_BITS))
  93. #define bitset_empty(set) memset (set, '\0', sizeof (bitset_t))
  94. #define bitset_set_all(set) memset (set, '\xff', sizeof (bitset_t))
  95. #define bitset_copy(dest,src) memcpy (dest, src, sizeof (bitset_t))
  96. #define PREV_WORD_CONSTRAINT 0x0001
  97. #define PREV_NOTWORD_CONSTRAINT 0x0002
  98. #define NEXT_WORD_CONSTRAINT 0x0004
  99. #define NEXT_NOTWORD_CONSTRAINT 0x0008
  100. #define PREV_NEWLINE_CONSTRAINT 0x0010
  101. #define NEXT_NEWLINE_CONSTRAINT 0x0020
  102. #define PREV_BEGBUF_CONSTRAINT 0x0040
  103. #define NEXT_ENDBUF_CONSTRAINT 0x0080
  104. #define WORD_DELIM_CONSTRAINT 0x0100
  105. #define NOT_WORD_DELIM_CONSTRAINT 0x0200
  106. typedef enum
  107. {
  108. INSIDE_WORD = PREV_WORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
  109. WORD_FIRST = PREV_NOTWORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
  110. WORD_LAST = PREV_WORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
  111. INSIDE_NOTWORD = PREV_NOTWORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
  112. LINE_FIRST = PREV_NEWLINE_CONSTRAINT,
  113. LINE_LAST = NEXT_NEWLINE_CONSTRAINT,
  114. BUF_FIRST = PREV_BEGBUF_CONSTRAINT,
  115. BUF_LAST = NEXT_ENDBUF_CONSTRAINT,
  116. WORD_DELIM = WORD_DELIM_CONSTRAINT,
  117. NOT_WORD_DELIM = NOT_WORD_DELIM_CONSTRAINT
  118. } re_context_type;
  119. typedef struct
  120. {
  121. int alloc;
  122. int nelem;
  123. int *elems;
  124. } re_node_set;
  125. typedef enum
  126. {
  127. NON_TYPE = 0,
  128. /* Node type, These are used by token, node, tree. */
  129. CHARACTER = 1,
  130. END_OF_RE = 2,
  131. SIMPLE_BRACKET = 3,
  132. OP_BACK_REF = 4,
  133. OP_PERIOD = 5,
  134. #ifdef RE_ENABLE_I18N
  135. COMPLEX_BRACKET = 6,
  136. OP_UTF8_PERIOD = 7,
  137. #endif /* RE_ENABLE_I18N */
  138. /* We define EPSILON_BIT as a macro so that OP_OPEN_SUBEXP is used
  139. when the debugger shows values of this enum type. */
  140. #define EPSILON_BIT 8
  141. OP_OPEN_SUBEXP = EPSILON_BIT | 0,
  142. OP_CLOSE_SUBEXP = EPSILON_BIT | 1,
  143. OP_ALT = EPSILON_BIT | 2,
  144. OP_DUP_ASTERISK = EPSILON_BIT | 3,
  145. ANCHOR = EPSILON_BIT | 4,
  146. /* Tree type, these are used only by tree. */
  147. CONCAT = 16,
  148. SUBEXP = 17,
  149. /* Token type, these are used only by token. */
  150. OP_DUP_PLUS = 18,
  151. OP_DUP_QUESTION,
  152. OP_OPEN_BRACKET,
  153. OP_CLOSE_BRACKET,
  154. OP_CHARSET_RANGE,
  155. OP_OPEN_DUP_NUM,
  156. OP_CLOSE_DUP_NUM,
  157. OP_NON_MATCH_LIST,
  158. OP_OPEN_COLL_ELEM,
  159. OP_CLOSE_COLL_ELEM,
  160. OP_OPEN_EQUIV_CLASS,
  161. OP_CLOSE_EQUIV_CLASS,
  162. OP_OPEN_CHAR_CLASS,
  163. OP_CLOSE_CHAR_CLASS,
  164. OP_WORD,
  165. OP_NOTWORD,
  166. OP_SPACE,
  167. OP_NOTSPACE,
  168. BACK_SLASH
  169. } re_token_type_t;
  170. #ifdef RE_ENABLE_I18N
  171. typedef struct
  172. {
  173. /* Multibyte characters. */
  174. wchar_t *mbchars;
  175. /* Collating symbols. */
  176. # if 0
  177. int32_t *coll_syms;
  178. # endif
  179. /* Equivalence classes. */
  180. # if 0
  181. int32_t *equiv_classes;
  182. # endif
  183. /* Range expressions. */
  184. # if 0
  185. uint32_t *range_starts;
  186. uint32_t *range_ends;
  187. # else
  188. wchar_t *range_starts;
  189. wchar_t *range_ends;
  190. # endif
  191. /* Character classes. */
  192. wctype_t *char_classes;
  193. /* If this character set is the non-matching list. */
  194. unsigned int non_match : 1;
  195. /* # of multibyte characters. */
  196. int nmbchars;
  197. /* # of collating symbols. */
  198. int ncoll_syms;
  199. /* # of equivalence classes. */
  200. int nequiv_classes;
  201. /* # of range expressions. */
  202. int nranges;
  203. /* # of character classes. */
  204. int nchar_classes;
  205. } re_charset_t;
  206. #endif /* RE_ENABLE_I18N */
  207. typedef struct
  208. {
  209. union
  210. {
  211. unsigned char c; /* for CHARACTER */
  212. re_bitset_ptr_t sbcset; /* for SIMPLE_BRACKET */
  213. #ifdef RE_ENABLE_I18N
  214. re_charset_t *mbcset; /* for COMPLEX_BRACKET */
  215. #endif /* RE_ENABLE_I18N */
  216. int idx; /* for BACK_REF */
  217. re_context_type ctx_type; /* for ANCHOR */
  218. } opr;
  219. #if __GNUC__ >= 2
  220. re_token_type_t type : 8;
  221. #else
  222. re_token_type_t type;
  223. #endif
  224. unsigned int constraint : 10; /* context constraint */
  225. unsigned int duplicated : 1;
  226. unsigned int opt_subexp : 1;
  227. #ifdef RE_ENABLE_I18N
  228. unsigned int accept_mb : 1;
  229. /* These 2 bits can be moved into the union if needed (e.g. if running out
  230. of bits; move opr.c to opr.c.c and move the flags to opr.c.flags). */
  231. unsigned int mb_partial : 1;
  232. #endif
  233. unsigned int word_char : 1;
  234. } re_token_t;
  235. #define IS_EPSILON_NODE(type) ((type) & EPSILON_BIT)
  236. struct re_string_t
  237. {
  238. /* Indicate the raw buffer which is the original string passed as an
  239. argument of regexec(), re_search(), etc.. */
  240. const unsigned char *raw_mbs;
  241. /* Store the multibyte string. In case of "case insensitive mode" like
  242. REG_ICASE, upper cases of the string are stored, otherwise MBS points
  243. the same address that RAW_MBS points. */
  244. unsigned char *mbs;
  245. #ifdef RE_ENABLE_I18N
  246. /* Store the wide character string which is corresponding to MBS. */
  247. wint_t *wcs;
  248. int *offsets;
  249. mbstate_t cur_state;
  250. #endif
  251. /* Index in RAW_MBS. Each character mbs[i] corresponds to
  252. raw_mbs[raw_mbs_idx + i]. */
  253. int raw_mbs_idx;
  254. /* The length of the valid characters in the buffers. */
  255. int valid_len;
  256. /* The corresponding number of bytes in raw_mbs array. */
  257. int valid_raw_len;
  258. /* The length of the buffers MBS and WCS. */
  259. int bufs_len;
  260. /* The index in MBS, which is updated by re_string_fetch_byte. */
  261. int cur_idx;
  262. /* length of RAW_MBS array. */
  263. int raw_len;
  264. /* This is RAW_LEN - RAW_MBS_IDX + VALID_LEN - VALID_RAW_LEN. */
  265. int len;
  266. /* End of the buffer may be shorter than its length in the cases such
  267. as re_match_2, re_search_2. Then, we use STOP for end of the buffer
  268. instead of LEN. */
  269. int raw_stop;
  270. /* This is RAW_STOP - RAW_MBS_IDX adjusted through OFFSETS. */
  271. int stop;
  272. /* The context of mbs[0]. We store the context independently, since
  273. the context of mbs[0] may be different from raw_mbs[0], which is
  274. the beginning of the input string. */
  275. unsigned int tip_context;
  276. /* The translation passed as a part of an argument of re_compile_pattern. */
  277. __RE_TRANSLATE_TYPE trans;
  278. /* Copy of re_dfa_t's word_char. */
  279. re_const_bitset_ptr_t word_char;
  280. /* 1 if REG_ICASE. */
  281. unsigned char icase;
  282. unsigned char is_utf8;
  283. unsigned char map_notascii;
  284. unsigned char mbs_allocated;
  285. unsigned char offsets_needed;
  286. unsigned char newline_anchor;
  287. unsigned char word_ops_used;
  288. int mb_cur_max;
  289. };
  290. typedef struct re_string_t re_string_t;
  291. struct re_dfa_t;
  292. typedef struct re_dfa_t re_dfa_t;
  293. static reg_errcode_t re_string_realloc_buffers (re_string_t *pstr,
  294. int new_buf_len)
  295. internal_function;
  296. #ifdef RE_ENABLE_I18N
  297. static void build_wcs_buffer (re_string_t *pstr) internal_function;
  298. static reg_errcode_t build_wcs_upper_buffer (re_string_t *pstr) internal_function;
  299. #endif /* RE_ENABLE_I18N */
  300. static void build_upper_buffer (re_string_t *pstr) internal_function;
  301. static void re_string_translate_buffer (re_string_t *pstr) internal_function;
  302. static unsigned int re_string_context_at (const re_string_t *input, int idx,
  303. int eflags)
  304. internal_function __attribute ((pure));
  305. #define re_string_peek_byte(pstr, offset) \
  306. ((pstr)->mbs[(pstr)->cur_idx + offset])
  307. #define re_string_fetch_byte(pstr) \
  308. ((pstr)->mbs[(pstr)->cur_idx++])
  309. #define re_string_first_byte(pstr, idx) \
  310. ((idx) == (pstr)->valid_len || (pstr)->wcs[idx] != WEOF)
  311. #define re_string_is_single_byte_char(pstr, idx) \
  312. ((pstr)->wcs[idx] != WEOF && ((pstr)->valid_len == (idx) + 1 \
  313. || (pstr)->wcs[(idx) + 1] != WEOF))
  314. #define re_string_eoi(pstr) ((pstr)->stop <= (pstr)->cur_idx)
  315. #define re_string_cur_idx(pstr) ((pstr)->cur_idx)
  316. #define re_string_get_buffer(pstr) ((pstr)->mbs)
  317. #define re_string_length(pstr) ((pstr)->len)
  318. #define re_string_byte_at(pstr,idx) ((pstr)->mbs[idx])
  319. #define re_string_skip_bytes(pstr,idx) ((pstr)->cur_idx += (idx))
  320. #define re_string_set_index(pstr,idx) ((pstr)->cur_idx = (idx))
  321. #include <alloca.h>
  322. #if 1
  323. # ifdef HAVE_ALLOCA
  324. /* The OS usually guarantees only one guard page at the bottom of the stack,
  325. and a page size can be as small as 4096 bytes. So we cannot safely
  326. allocate anything larger than 4096 bytes. Also care for the possibility
  327. of a few compiler-allocated temporary stack slots. */
  328. # define __libc_use_alloca(n) ((n) < 4032)
  329. # else
  330. /* alloca is implemented with malloc, so just use malloc. */
  331. # define __libc_use_alloca(n) 0
  332. # endif
  333. #endif
  334. #define re_malloc(t,n) ((t *) malloc ((n) * sizeof (t)))
  335. #define re_realloc(p,t,n) ((t *) realloc (p, (n) * sizeof (t)))
  336. #define re_free(p) free (p)
  337. struct bin_tree_t
  338. {
  339. struct bin_tree_t *parent;
  340. struct bin_tree_t *left;
  341. struct bin_tree_t *right;
  342. struct bin_tree_t *first;
  343. struct bin_tree_t *next;
  344. re_token_t token;
  345. /* `node_idx' is the index in dfa->nodes, if `type' == 0.
  346. Otherwise `type' indicate the type of this node. */
  347. int node_idx;
  348. };
  349. typedef struct bin_tree_t bin_tree_t;
  350. #define BIN_TREE_STORAGE_SIZE \
  351. ((1024 - sizeof (void *)) / sizeof (bin_tree_t))
  352. struct bin_tree_storage_t
  353. {
  354. struct bin_tree_storage_t *next;
  355. bin_tree_t data[BIN_TREE_STORAGE_SIZE];
  356. };
  357. typedef struct bin_tree_storage_t bin_tree_storage_t;
  358. #define CONTEXT_WORD 1
  359. #define CONTEXT_NEWLINE (CONTEXT_WORD << 1)
  360. #define CONTEXT_BEGBUF (CONTEXT_NEWLINE << 1)
  361. #define CONTEXT_ENDBUF (CONTEXT_BEGBUF << 1)
  362. #define IS_WORD_CONTEXT(c) ((c) & CONTEXT_WORD)
  363. #define IS_NEWLINE_CONTEXT(c) ((c) & CONTEXT_NEWLINE)
  364. #define IS_BEGBUF_CONTEXT(c) ((c) & CONTEXT_BEGBUF)
  365. #define IS_ENDBUF_CONTEXT(c) ((c) & CONTEXT_ENDBUF)
  366. #define IS_ORDINARY_CONTEXT(c) ((c) == 0)
  367. #define IS_WORD_CHAR(ch) (isalnum (ch) || (ch) == '_')
  368. #define IS_NEWLINE(ch) ((ch) == NEWLINE_CHAR)
  369. #define IS_WIDE_WORD_CHAR(ch) (iswalnum (ch) || (ch) == L'_')
  370. #define IS_WIDE_NEWLINE(ch) ((ch) == WIDE_NEWLINE_CHAR)
  371. #define NOT_SATISFY_PREV_CONSTRAINT(constraint,context) \
  372. ((((constraint) & PREV_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
  373. || ((constraint & PREV_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
  374. || ((constraint & PREV_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context))\
  375. || ((constraint & PREV_BEGBUF_CONSTRAINT) && !IS_BEGBUF_CONTEXT (context)))
  376. #define NOT_SATISFY_NEXT_CONSTRAINT(constraint,context) \
  377. ((((constraint) & NEXT_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
  378. || (((constraint) & NEXT_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
  379. || (((constraint) & NEXT_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context)) \
  380. || (((constraint) & NEXT_ENDBUF_CONSTRAINT) && !IS_ENDBUF_CONTEXT (context)))
  381. struct re_dfastate_t
  382. {
  383. unsigned int hash;
  384. re_node_set nodes;
  385. re_node_set non_eps_nodes;
  386. re_node_set inveclosure;
  387. re_node_set *entrance_nodes;
  388. struct re_dfastate_t **trtable, **word_trtable;
  389. unsigned int context : 4;
  390. unsigned int halt : 1;
  391. /* If this state can accept `multi byte'.
  392. Note that we refer to multibyte characters, and multi character
  393. collating elements as `multi byte'. */
  394. unsigned int accept_mb : 1;
  395. /* If this state has backreference node(s). */
  396. unsigned int has_backref : 1;
  397. unsigned int has_constraint : 1;
  398. };
  399. typedef struct re_dfastate_t re_dfastate_t;
  400. struct re_state_table_entry
  401. {
  402. int num;
  403. int alloc;
  404. re_dfastate_t **array;
  405. };
  406. /* Array type used in re_sub_match_last_t and re_sub_match_top_t. */
  407. typedef struct
  408. {
  409. int next_idx;
  410. int alloc;
  411. re_dfastate_t **array;
  412. } state_array_t;
  413. /* Store information about the node NODE whose type is OP_CLOSE_SUBEXP. */
  414. typedef struct
  415. {
  416. int node;
  417. int str_idx; /* The position NODE match at. */
  418. state_array_t path;
  419. } re_sub_match_last_t;
  420. /* Store information about the node NODE whose type is OP_OPEN_SUBEXP.
  421. And information about the node, whose type is OP_CLOSE_SUBEXP,
  422. corresponding to NODE is stored in LASTS. */
  423. typedef struct
  424. {
  425. int str_idx;
  426. int node;
  427. state_array_t *path;
  428. int alasts; /* Allocation size of LASTS. */
  429. int nlasts; /* The number of LASTS. */
  430. re_sub_match_last_t **lasts;
  431. } re_sub_match_top_t;
  432. struct re_backref_cache_entry
  433. {
  434. int node;
  435. int str_idx;
  436. int subexp_from;
  437. int subexp_to;
  438. char more;
  439. char unused;
  440. unsigned short int eps_reachable_subexps_map;
  441. };
  442. typedef struct
  443. {
  444. /* The string object corresponding to the input string. */
  445. re_string_t input;
  446. const re_dfa_t *dfa;
  447. /* EFLAGS of the argument of regexec. */
  448. int eflags;
  449. /* Where the matching ends. */
  450. int match_last;
  451. int last_node;
  452. /* The state log used by the matcher. */
  453. re_dfastate_t **state_log;
  454. int state_log_top;
  455. /* Back reference cache. */
  456. int nbkref_ents;
  457. int abkref_ents;
  458. struct re_backref_cache_entry *bkref_ents;
  459. int max_mb_elem_len;
  460. int nsub_tops;
  461. int asub_tops;
  462. re_sub_match_top_t **sub_tops;
  463. } re_match_context_t;
  464. typedef struct
  465. {
  466. re_dfastate_t **sifted_states;
  467. re_dfastate_t **limited_states;
  468. int last_node;
  469. int last_str_idx;
  470. re_node_set limits;
  471. } re_sift_context_t;
  472. struct re_fail_stack_ent_t
  473. {
  474. int idx;
  475. int node;
  476. regmatch_t *regs;
  477. re_node_set eps_via_nodes;
  478. };
  479. struct re_fail_stack_t
  480. {
  481. int num;
  482. int alloc;
  483. struct re_fail_stack_ent_t *stack;
  484. };
  485. struct re_dfa_t
  486. {
  487. re_token_t *nodes;
  488. size_t nodes_alloc;
  489. size_t nodes_len;
  490. int *nexts;
  491. int *org_indices;
  492. re_node_set *edests;
  493. re_node_set *eclosures;
  494. re_node_set *inveclosures;
  495. struct re_state_table_entry *state_table;
  496. re_dfastate_t *init_state;
  497. re_dfastate_t *init_state_word;
  498. re_dfastate_t *init_state_nl;
  499. re_dfastate_t *init_state_begbuf;
  500. bin_tree_t *str_tree;
  501. bin_tree_storage_t *str_tree_storage;
  502. re_bitset_ptr_t sb_char;
  503. int str_tree_storage_idx;
  504. /* number of subexpressions `re_nsub' is in regex_t. */
  505. unsigned int state_hash_mask;
  506. int init_node;
  507. int nbackref; /* The number of backreference in this dfa. */
  508. /* Bitmap expressing which backreference is used. */
  509. bitset_word_t used_bkref_map;
  510. bitset_word_t completed_bkref_map;
  511. unsigned int has_plural_match : 1;
  512. /* If this dfa has "multibyte node", which is a backreference or
  513. a node which can accept multibyte character or multi character
  514. collating element. */
  515. unsigned int has_mb_node : 1;
  516. unsigned int is_utf8 : 1;
  517. unsigned int map_notascii : 1;
  518. unsigned int word_ops_used : 1;
  519. int mb_cur_max;
  520. bitset_t word_char;
  521. reg_syntax_t syntax;
  522. int *subexp_map;
  523. #ifdef DEBUG
  524. char* re_str;
  525. #endif
  526. __libc_lock_define (, lock)
  527. };
  528. #define re_node_set_init_empty(set) memset (set, '\0', sizeof (re_node_set))
  529. #define re_node_set_remove(set,id) \
  530. (re_node_set_remove_at (set, re_node_set_contains (set, id) - 1))
  531. #define re_node_set_empty(p) ((p)->nelem = 0)
  532. #define re_node_set_free(set) re_free ((set)->elems)
  533. typedef enum
  534. {
  535. SB_CHAR,
  536. MB_CHAR,
  537. EQUIV_CLASS,
  538. COLL_SYM,
  539. CHAR_CLASS
  540. } bracket_elem_type;
  541. typedef struct
  542. {
  543. bracket_elem_type type;
  544. union
  545. {
  546. unsigned char ch;
  547. unsigned char *name;
  548. #ifdef __UCLIBC_HAS_WCHAR__
  549. wchar_t wch;
  550. #endif
  551. } opr;
  552. } bracket_elem_t;
  553. /* Inline functions for bitset operation. */
  554. static __inline__ void
  555. bitset_not (bitset_t set)
  556. {
  557. int bitset_i;
  558. for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
  559. set[bitset_i] = ~set[bitset_i];
  560. }
  561. static __inline__ void
  562. bitset_merge (bitset_t dest, const bitset_t src)
  563. {
  564. int bitset_i;
  565. for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
  566. dest[bitset_i] |= src[bitset_i];
  567. }
  568. static __inline__ void
  569. bitset_mask (bitset_t dest, const bitset_t src)
  570. {
  571. int bitset_i;
  572. for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
  573. dest[bitset_i] &= src[bitset_i];
  574. }
  575. #ifdef RE_ENABLE_I18N
  576. /* Inline functions for re_string. */
  577. static __inline__ int
  578. internal_function __attribute ((pure))
  579. re_string_char_size_at (const re_string_t *pstr, int idx)
  580. {
  581. int byte_idx;
  582. if (pstr->mb_cur_max == 1)
  583. return 1;
  584. for (byte_idx = 1; idx + byte_idx < pstr->valid_len; ++byte_idx)
  585. if (pstr->wcs[idx + byte_idx] != WEOF)
  586. break;
  587. return byte_idx;
  588. }
  589. static __inline__ wint_t
  590. internal_function __attribute ((pure))
  591. re_string_wchar_at (const re_string_t *pstr, int idx)
  592. {
  593. if (pstr->mb_cur_max == 1)
  594. return (wint_t) pstr->mbs[idx];
  595. return (wint_t) pstr->wcs[idx];
  596. }
  597. static int
  598. internal_function __attribute ((pure))
  599. re_string_elem_size_at (const re_string_t *pstr, int idx)
  600. {
  601. # if 0
  602. const unsigned char *p, *extra;
  603. const int32_t *table, *indirect;
  604. int32_t tmp;
  605. # include <locale/weight.h>
  606. uint_fast32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
  607. if (nrules != 0)
  608. {
  609. table = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
  610. extra = (const unsigned char *)
  611. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB);
  612. indirect = (const int32_t *) _NL_CURRENT (LC_COLLATE,
  613. _NL_COLLATE_INDIRECTMB);
  614. p = pstr->mbs + idx;
  615. tmp = findidx (&p);
  616. return p - pstr->mbs - idx;
  617. }
  618. # endif
  619. return 1;
  620. }
  621. #endif /* RE_ENABLE_I18N */
  622. #endif /* _REGEX_INTERNAL_H */