source: trunk/essentials/sys-apps/gawk/regex_internal.h

Last change on this file was 3081, checked in by bird, 18 years ago

our gcc doesn't like stdcall + regparm, so use Optlink instead.

File size: 24.3 KB
Line 
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
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, write to the Free
18 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA. */
20
21#ifndef _REGEX_INTERNAL_H
22#define _REGEX_INTERNAL_H 1
23
24#include <assert.h>
25#include <ctype.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29
30#if defined HAVE_LANGINFO_H || defined HAVE_LANGINFO_CODESET || defined _LIBC
31# include <langinfo.h>
32#endif
33#if defined HAVE_LOCALE_H || defined _LIBC
34# include <locale.h>
35#endif
36#if defined HAVE_WCHAR_H || defined _LIBC
37# include <wchar.h>
38#endif /* HAVE_WCHAR_H || _LIBC */
39#if defined HAVE_WCTYPE_H || defined _LIBC
40# include <wctype.h>
41#endif /* HAVE_WCTYPE_H || _LIBC */
42
43#ifndef GAWK
44/* In case that the system doesn't have isblank(). */
45#if !defined _LIBC && !defined HAVE_ISBLANK && !defined isblank
46# define isblank(ch) ((ch) == ' ' || (ch) == '\t')
47#endif
48#else /* GAWK */
49/*
50 * This is a freaking mess. On glibc systems you have to define
51 * a magic constant to get isblank() out of <ctype.h>, since it's
52 * a C99 function. To heck wiht all that and borrow a page from
53 * dfa.c's book.
54 */
55
56static int
57is_blank (int c)
58{
59 return (c == ' ' || c == '\t');
60}
61#endif /* GAWK */
62
63#ifdef _LIBC
64# ifndef _RE_DEFINE_LOCALE_FUNCTIONS
65# define _RE_DEFINE_LOCALE_FUNCTIONS 1
66# include <locale/localeinfo.h>
67# include <locale/elem-hash.h>
68# include <locale/coll-lookup.h>
69# endif
70#endif
71
72/* This is for other GNU distributions with internationalized messages. */
73#if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
74# include <libintl.h>
75# ifdef _LIBC
76# undef gettext
77# define gettext(msgid) \
78 INTUSE(__dcgettext) (INTUSE(_libc_intl_domainname), msgid, LC_MESSAGES)
79# endif
80#else
81# define gettext(msgid) (msgid)
82#endif
83
84#ifndef gettext_noop
85/* This define is so xgettext can find the internationalizable
86 strings. */
87# define gettext_noop(String) String
88#endif
89
90#ifndef NO_MBSUPPORT
91#include "mbsupport.h" /* gawk */
92#endif
93#ifndef MB_CUR_MAX
94#define MB_CUR_MAX 1
95#endif
96
97#if (defined MBS_SUPPORT) || _LIBC
98# define RE_ENABLE_I18N
99#endif
100
101#if __GNUC__ >= 3
102# define BE(expr, val) __builtin_expect (expr, val)
103#else
104# define BE(expr, val) (expr)
105# define inline
106#endif
107
108/* Number of bits in a byte. */
109#define BYTE_BITS 8
110/* Number of single byte character. */
111#define SBC_MAX 256
112
113#define COLL_ELEM_LEN_MAX 8
114
115/* The character which represents newline. */
116#define NEWLINE_CHAR '\n'
117#define WIDE_NEWLINE_CHAR L'\n'
118
119/* Rename to standard API for using out of glibc. */
120#ifndef _LIBC
121# define __wctype wctype
122# define __iswctype iswctype
123# define __btowc btowc
124# define __mempcpy mempcpy
125# define __wcrtomb wcrtomb
126# define __regfree regfree
127# define attribute_hidden
128#endif /* not _LIBC */
129
130#ifdef __GNUC__
131# define __attribute(arg) __attribute__ (arg)
132#else
133# define __attribute(arg)
134#endif
135
136/* Error messages (regcomp.c), with hackery to support very old compilers. */
137#ifdef NO_TOKEN_PASTING
138#define RE_ERRMSG(idx) __re_error_msgid[(int) (idx)]
139#define ERRMSG_TYPE char * const /* pointers */
140#define ERRMSG_OFFSET(base,offset) ((base)+1) /* array index */
141#define ERRMSG_SEPARATOR ,
142#else
143#define RE_ERRMSG(idx) (__re_error_msgid + __re_error_msgid_idx[(int) (idx)])
144#define ERRMSG_TYPE char /* one string */
145#define ERRMSG_OFFSET(base,offset) ((base)+(offset)) /* substring index */
146#define ERRMSG_SEPARATOR "\0"
147#endif
148
149extern const ERRMSG_TYPE __re_error_msgid[] attribute_hidden;
150extern const size_t __re_error_msgid_idx[] attribute_hidden;
151
152/* Number of bits in an unsinged int. */
153#define UINT_BITS (sizeof (unsigned int) * BYTE_BITS)
154/* Number of unsigned int in an bit_set. */
155#define BITSET_UINTS ((SBC_MAX + UINT_BITS - 1) / UINT_BITS)
156typedef unsigned int bitset[BITSET_UINTS];
157typedef unsigned int *re_bitset_ptr_t;
158typedef const unsigned int *re_const_bitset_ptr_t;
159
160#define bitset_set(set,i) (set[i / UINT_BITS] |= 1 << i % UINT_BITS)
161#define bitset_clear(set,i) (set[i / UINT_BITS] &= ~(1 << i % UINT_BITS))
162#define bitset_contain(set,i) (set[i / UINT_BITS] & (1 << i % UINT_BITS))
163#define bitset_empty(set) memset (set, 0, sizeof (unsigned int) * BITSET_UINTS)
164#define bitset_set_all(set) \
165 memset (set, 255, sizeof (unsigned int) * BITSET_UINTS)
166#define bitset_copy(dest,src) \
167 memcpy (dest, src, sizeof (unsigned int) * BITSET_UINTS)
168static inline void bitset_not (bitset set);
169static inline void bitset_merge (bitset dest, const bitset src);
170static inline void bitset_not_merge (bitset dest, const bitset src);
171static inline void bitset_mask (bitset dest, const bitset src);
172
173#define PREV_WORD_CONSTRAINT 0x0001
174#define PREV_NOTWORD_CONSTRAINT 0x0002
175#define NEXT_WORD_CONSTRAINT 0x0004
176#define NEXT_NOTWORD_CONSTRAINT 0x0008
177#define PREV_NEWLINE_CONSTRAINT 0x0010
178#define NEXT_NEWLINE_CONSTRAINT 0x0020
179#define PREV_BEGBUF_CONSTRAINT 0x0040
180#define NEXT_ENDBUF_CONSTRAINT 0x0080
181#define WORD_DELIM_CONSTRAINT 0x0100
182#define NOT_WORD_DELIM_CONSTRAINT 0x0200
183
184typedef enum
185{
186 INSIDE_WORD = PREV_WORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
187 WORD_FIRST = PREV_NOTWORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
188 WORD_LAST = PREV_WORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
189 INSIDE_NOTWORD = PREV_NOTWORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
190 LINE_FIRST = PREV_NEWLINE_CONSTRAINT,
191 LINE_LAST = NEXT_NEWLINE_CONSTRAINT,
192 BUF_FIRST = PREV_BEGBUF_CONSTRAINT,
193 BUF_LAST = NEXT_ENDBUF_CONSTRAINT,
194 WORD_DELIM = WORD_DELIM_CONSTRAINT,
195 NOT_WORD_DELIM = NOT_WORD_DELIM_CONSTRAINT
196} re_context_type;
197
198typedef struct
199{
200 int alloc;
201 int nelem;
202 int *elems;
203} re_node_set;
204
205typedef enum
206{
207 NON_TYPE = 0,
208
209 /* Node type, These are used by token, node, tree. */
210 CHARACTER = 1,
211 END_OF_RE = 2,
212 SIMPLE_BRACKET = 3,
213 OP_BACK_REF = 4,
214 OP_PERIOD = 5,
215#ifdef RE_ENABLE_I18N
216 COMPLEX_BRACKET = 6,
217 OP_UTF8_PERIOD = 7,
218#endif /* RE_ENABLE_I18N */
219
220 /* We define EPSILON_BIT as a macro so that OP_OPEN_SUBEXP is used
221 when the debugger shows values of this enum type. */
222#define EPSILON_BIT 8
223 OP_OPEN_SUBEXP = EPSILON_BIT | 0,
224 OP_CLOSE_SUBEXP = EPSILON_BIT | 1,
225 OP_ALT = EPSILON_BIT | 2,
226 OP_DUP_ASTERISK = EPSILON_BIT | 3,
227 ANCHOR = EPSILON_BIT | 4,
228
229 /* Tree type, these are used only by tree. */
230 CONCAT = 16,
231 SUBEXP = 17,
232
233 /* Token type, these are used only by token. */
234 OP_DUP_PLUS = 18,
235 OP_DUP_QUESTION,
236 OP_OPEN_BRACKET,
237 OP_CLOSE_BRACKET,
238 OP_CHARSET_RANGE,
239 OP_OPEN_DUP_NUM,
240 OP_CLOSE_DUP_NUM,
241 OP_NON_MATCH_LIST,
242 OP_OPEN_COLL_ELEM,
243 OP_CLOSE_COLL_ELEM,
244 OP_OPEN_EQUIV_CLASS,
245 OP_CLOSE_EQUIV_CLASS,
246 OP_OPEN_CHAR_CLASS,
247 OP_CLOSE_CHAR_CLASS,
248 OP_WORD,
249 OP_NOTWORD,
250 OP_SPACE,
251 OP_NOTSPACE,
252 BACK_SLASH
253
254} re_token_type_t;
255
256#ifdef RE_ENABLE_I18N
257typedef struct
258{
259 /* Multibyte characters. */
260 wchar_t *mbchars;
261
262 /* Collating symbols. */
263# ifdef _LIBC
264 int32_t *coll_syms;
265# endif
266
267 /* Equivalence classes. */
268# ifdef _LIBC
269 int32_t *equiv_classes;
270# endif
271
272 /* Range expressions. */
273# ifdef _LIBC
274 uint32_t *range_starts;
275 uint32_t *range_ends;
276# else /* not _LIBC */
277 wchar_t *range_starts;
278 wchar_t *range_ends;
279# endif /* not _LIBC */
280
281 /* Character classes. */
282 wctype_t *char_classes;
283
284 /* If this character set is the non-matching list. */
285 unsigned int non_match : 1;
286
287 /* # of multibyte characters. */
288 int nmbchars;
289
290 /* # of collating symbols. */
291 int ncoll_syms;
292
293 /* # of equivalence classes. */
294 int nequiv_classes;
295
296 /* # of range expressions. */
297 int nranges;
298
299 /* # of character classes. */
300 int nchar_classes;
301} re_charset_t;
302#endif /* RE_ENABLE_I18N */
303
304typedef struct
305{
306 union
307 {
308 unsigned char c; /* for CHARACTER */
309 re_bitset_ptr_t sbcset; /* for SIMPLE_BRACKET */
310#ifdef RE_ENABLE_I18N
311 re_charset_t *mbcset; /* for COMPLEX_BRACKET */
312#endif /* RE_ENABLE_I18N */
313 int idx; /* for BACK_REF */
314 re_context_type ctx_type; /* for ANCHOR */
315 } opr;
316#if __GNUC__ >= 2
317 re_token_type_t type : 8;
318#else
319 re_token_type_t type;
320#endif
321 unsigned int constraint : 10; /* context constraint */
322 unsigned int duplicated : 1;
323 unsigned int opt_subexp : 1;
324#ifdef RE_ENABLE_I18N
325 unsigned int accept_mb : 1;
326 /* These 2 bits can be moved into the union if needed (e.g. if running out
327 of bits; move opr.c to opr.c.c and move the flags to opr.c.flags). */
328 unsigned int mb_partial : 1;
329#endif
330 unsigned int word_char : 1;
331} re_token_t;
332
333#define IS_EPSILON_NODE(type) ((type) & EPSILON_BIT)
334
335struct re_string_t
336{
337 /* Indicate the raw buffer which is the original string passed as an
338 argument of regexec(), re_search(), etc.. */
339 const unsigned char *raw_mbs;
340 /* Store the multibyte string. In case of "case insensitive mode" like
341 REG_ICASE, upper cases of the string are stored, otherwise MBS points
342 the same address that RAW_MBS points. */
343 unsigned char *mbs;
344#ifdef RE_ENABLE_I18N
345 /* Store the wide character string which is corresponding to MBS. */
346 wint_t *wcs;
347 int *offsets;
348 mbstate_t cur_state;
349#endif
350 /* Index in RAW_MBS. Each character mbs[i] corresponds to
351 raw_mbs[raw_mbs_idx + i]. */
352 int raw_mbs_idx;
353 /* The length of the valid characters in the buffers. */
354 int valid_len;
355 /* The corresponding number of bytes in raw_mbs array. */
356 int valid_raw_len;
357 /* The length of the buffers MBS and WCS. */
358 int bufs_len;
359 /* The index in MBS, which is updated by re_string_fetch_byte. */
360 int cur_idx;
361 /* length of RAW_MBS array. */
362 int raw_len;
363 /* This is RAW_LEN - RAW_MBS_IDX + VALID_LEN - VALID_RAW_LEN. */
364 int len;
365 /* End of the buffer may be shorter than its length in the cases such
366 as re_match_2, re_search_2. Then, we use STOP for end of the buffer
367 instead of LEN. */
368 int raw_stop;
369 /* This is RAW_STOP - RAW_MBS_IDX adjusted through OFFSETS. */
370 int stop;
371
372 /* The context of mbs[0]. We store the context independently, since
373 the context of mbs[0] may be different from raw_mbs[0], which is
374 the beginning of the input string. */
375 unsigned int tip_context;
376 /* The translation passed as a part of an argument of re_compile_pattern. */
377 unsigned RE_TRANSLATE_TYPE trans;
378 /* Copy of re_dfa_t's word_char. */
379 re_const_bitset_ptr_t word_char;
380 /* 1 if REG_ICASE. */
381 unsigned char icase;
382 unsigned char is_utf8;
383 unsigned char map_notascii;
384 unsigned char mbs_allocated;
385 unsigned char offsets_needed;
386 unsigned char newline_anchor;
387 unsigned char word_ops_used;
388 int mb_cur_max;
389};
390typedef struct re_string_t re_string_t;
391
392
393struct re_dfa_t;
394typedef struct re_dfa_t re_dfa_t;
395
396#ifndef _LIBC
397# ifdef __i386__
398# ifdef __EMX__
399# define internal_function _Optlink
400# else
401# define internal_function __attribute ((regparm (3), stdcall))
402# endif
403# else
404# define internal_function
405# endif
406#endif
407
408#ifndef RE_NO_INTERNAL_PROTOTYPES
409static reg_errcode_t re_string_allocate (re_string_t *pstr, const char *str,
410 int len, int init_len,
411 RE_TRANSLATE_TYPE trans, int icase,
412 const re_dfa_t *dfa)
413 internal_function;
414static reg_errcode_t re_string_construct (re_string_t *pstr, const char *str,
415 int len, RE_TRANSLATE_TYPE trans,
416 int icase, const re_dfa_t *dfa)
417 internal_function;
418static reg_errcode_t re_string_reconstruct (re_string_t *pstr, int idx,
419 int eflags) internal_function;
420static reg_errcode_t re_string_realloc_buffers (re_string_t *pstr,
421 int new_buf_len)
422 internal_function;
423# ifdef RE_ENABLE_I18N
424static void build_wcs_buffer (re_string_t *pstr) internal_function;
425static int build_wcs_upper_buffer (re_string_t *pstr) internal_function;
426# endif /* RE_ENABLE_I18N */
427static void build_upper_buffer (re_string_t *pstr) internal_function;
428static void re_string_translate_buffer (re_string_t *pstr) internal_function;
429static void re_string_destruct (re_string_t *pstr) internal_function;
430# ifdef RE_ENABLE_I18N
431static int re_string_elem_size_at (const re_string_t *pstr, int idx)
432 internal_function __attribute ((pure));
433static inline int re_string_char_size_at (const re_string_t *pstr, int idx)
434 internal_function __attribute ((pure));
435static inline wint_t re_string_wchar_at (const re_string_t *pstr, int idx)
436 internal_function __attribute ((pure));
437# endif /* RE_ENABLE_I18N */
438static unsigned int re_string_context_at (const re_string_t *input, int idx,
439 int eflags)
440 internal_function __attribute ((pure));
441static unsigned char re_string_peek_byte_case (const re_string_t *pstr,
442 int idx)
443 internal_function __attribute ((pure));
444static unsigned char re_string_fetch_byte_case (re_string_t *pstr)
445 internal_function __attribute ((pure));
446#endif
447#define re_string_peek_byte(pstr, offset) \
448 ((pstr)->mbs[(pstr)->cur_idx + offset])
449#define re_string_fetch_byte(pstr) \
450 ((pstr)->mbs[(pstr)->cur_idx++])
451#define re_string_first_byte(pstr, idx) \
452 ((idx) == (pstr)->valid_len || (pstr)->wcs[idx] != WEOF)
453#define re_string_is_single_byte_char(pstr, idx) \
454 ((pstr)->wcs[idx] != WEOF && ((pstr)->valid_len == (idx) + 1 \
455 || (pstr)->wcs[(idx) + 1] != WEOF))
456#define re_string_eoi(pstr) ((pstr)->stop <= (pstr)->cur_idx)
457#define re_string_cur_idx(pstr) ((pstr)->cur_idx)
458#define re_string_get_buffer(pstr) ((pstr)->mbs)
459#define re_string_length(pstr) ((pstr)->len)
460#define re_string_byte_at(pstr,idx) ((pstr)->mbs[idx])
461#define re_string_skip_bytes(pstr,idx) ((pstr)->cur_idx += (idx))
462#define re_string_set_index(pstr,idx) ((pstr)->cur_idx = (idx))
463
464#define re_malloc(t,n) ((t *) malloc ((n) * sizeof (t)))
465/* SunOS 4.1.x realloc doesn't accept null pointers: pre-Standard C. Sigh. */
466#define re_realloc(p,t,n) ((p != NULL) ? (t *) realloc (p,(n)*sizeof(t)) : (t *) calloc(n,sizeof(t)))
467#define re_free(p) free (p)
468
469struct bin_tree_t
470{
471 struct bin_tree_t *parent;
472 struct bin_tree_t *left;
473 struct bin_tree_t *right;
474 struct bin_tree_t *first;
475 struct bin_tree_t *next;
476
477 re_token_t token;
478
479 /* `node_idx' is the index in dfa->nodes, if `type' == 0.
480 Otherwise `type' indicate the type of this node. */
481 int node_idx;
482};
483typedef struct bin_tree_t bin_tree_t;
484
485#define BIN_TREE_STORAGE_SIZE \
486 ((1024 - sizeof (void *)) / sizeof (bin_tree_t))
487
488struct bin_tree_storage_t
489{
490 struct bin_tree_storage_t *next;
491 bin_tree_t data[BIN_TREE_STORAGE_SIZE];
492};
493typedef struct bin_tree_storage_t bin_tree_storage_t;
494
495#define CONTEXT_WORD 1
496#define CONTEXT_NEWLINE (CONTEXT_WORD << 1)
497#define CONTEXT_BEGBUF (CONTEXT_NEWLINE << 1)
498#define CONTEXT_ENDBUF (CONTEXT_BEGBUF << 1)
499
500#define IS_WORD_CONTEXT(c) ((c) & CONTEXT_WORD)
501#define IS_NEWLINE_CONTEXT(c) ((c) & CONTEXT_NEWLINE)
502#define IS_BEGBUF_CONTEXT(c) ((c) & CONTEXT_BEGBUF)
503#define IS_ENDBUF_CONTEXT(c) ((c) & CONTEXT_ENDBUF)
504#define IS_ORDINARY_CONTEXT(c) ((c) == 0)
505
506#define IS_WORD_CHAR(ch) (isalnum (ch) || (ch) == '_')
507#define IS_NEWLINE(ch) ((ch) == NEWLINE_CHAR)
508#define IS_WIDE_WORD_CHAR(ch) (iswalnum (ch) || (ch) == L'_')
509#define IS_WIDE_NEWLINE(ch) ((ch) == WIDE_NEWLINE_CHAR)
510
511#define NOT_SATISFY_PREV_CONSTRAINT(constraint,context) \
512 ((((constraint) & PREV_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
513 || ((constraint & PREV_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
514 || ((constraint & PREV_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context))\
515 || ((constraint & PREV_BEGBUF_CONSTRAINT) && !IS_BEGBUF_CONTEXT (context)))
516
517#define NOT_SATISFY_NEXT_CONSTRAINT(constraint,context) \
518 ((((constraint) & NEXT_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
519 || (((constraint) & NEXT_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
520 || (((constraint) & NEXT_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context)) \
521 || (((constraint) & NEXT_ENDBUF_CONSTRAINT) && !IS_ENDBUF_CONTEXT (context)))
522
523struct re_dfastate_t
524{
525 unsigned int hash;
526 re_node_set nodes;
527 re_node_set non_eps_nodes;
528 re_node_set inveclosure;
529 re_node_set *entrance_nodes;
530 struct re_dfastate_t **trtable, **word_trtable;
531 unsigned int context : 4;
532 unsigned int halt : 1;
533 /* If this state can accept `multi byte'.
534 Note that we refer to multibyte characters, and multi character
535 collating elements as `multi byte'. */
536 unsigned int accept_mb : 1;
537 /* If this state has backreference node(s). */
538 unsigned int has_backref : 1;
539 unsigned int has_constraint : 1;
540};
541typedef struct re_dfastate_t re_dfastate_t;
542
543struct re_state_table_entry
544{
545 int num;
546 int alloc;
547 re_dfastate_t **array;
548};
549
550/* Array type used in re_sub_match_last_t and re_sub_match_top_t. */
551
552typedef struct
553{
554 int next_idx;
555 int alloc;
556 re_dfastate_t **array;
557} state_array_t;
558
559/* Store information about the node NODE whose type is OP_CLOSE_SUBEXP. */
560
561typedef struct
562{
563 int node;
564 int str_idx; /* The position NODE match at. */
565 state_array_t path;
566} re_sub_match_last_t;
567
568/* Store information about the node NODE whose type is OP_OPEN_SUBEXP.
569 And information about the node, whose type is OP_CLOSE_SUBEXP,
570 corresponding to NODE is stored in LASTS. */
571
572typedef struct
573{
574 int str_idx;
575 int node;
576 int next_last_offset;
577 state_array_t *path;
578 int alasts; /* Allocation size of LASTS. */
579 int nlasts; /* The number of LASTS. */
580 re_sub_match_last_t **lasts;
581} re_sub_match_top_t;
582
583struct re_backref_cache_entry
584{
585 int node;
586 int str_idx;
587 int subexp_from;
588 int subexp_to;
589 char more;
590 char unused;
591 unsigned short int eps_reachable_subexps_map;
592};
593
594typedef struct
595{
596 /* The string object corresponding to the input string. */
597 re_string_t input;
598#if defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L)
599 re_dfa_t *const dfa;
600#else
601 re_dfa_t *dfa;
602#endif
603 /* EFLAGS of the argument of regexec. */
604 int eflags;
605 /* Where the matching ends. */
606 int match_last;
607 int last_node;
608 /* The state log used by the matcher. */
609 re_dfastate_t **state_log;
610 int state_log_top;
611 /* Back reference cache. */
612 int nbkref_ents;
613 int abkref_ents;
614 struct re_backref_cache_entry *bkref_ents;
615 int max_mb_elem_len;
616 int nsub_tops;
617 int asub_tops;
618 re_sub_match_top_t **sub_tops;
619} re_match_context_t;
620
621typedef struct
622{
623 re_dfastate_t **sifted_states;
624 re_dfastate_t **limited_states;
625 int last_node;
626 int last_str_idx;
627 re_node_set limits;
628} re_sift_context_t;
629
630struct re_fail_stack_ent_t
631{
632 int idx;
633 int node;
634 regmatch_t *regs;
635 re_node_set eps_via_nodes;
636};
637
638struct re_fail_stack_t
639{
640 int num;
641 int alloc;
642 struct re_fail_stack_ent_t *stack;
643};
644
645struct re_dfa_t
646{
647 re_token_t *nodes;
648 int nodes_alloc;
649 int nodes_len;
650 int *nexts;
651 int *org_indices;
652 re_node_set *edests;
653 re_node_set *eclosures;
654 re_node_set *inveclosures;
655 struct re_state_table_entry *state_table;
656 re_dfastate_t *init_state;
657 re_dfastate_t *init_state_word;
658 re_dfastate_t *init_state_nl;
659 re_dfastate_t *init_state_begbuf;
660 bin_tree_t *str_tree;
661 bin_tree_storage_t *str_tree_storage;
662 re_bitset_ptr_t sb_char;
663 int str_tree_storage_idx;
664
665 /* number of subexpressions `re_nsub' is in regex_t. */
666 unsigned int state_hash_mask;
667 int states_alloc;
668 int init_node;
669 int nbackref; /* The number of backreference in this dfa. */
670
671 /* Bitmap expressing which backreference is used. */
672 unsigned int used_bkref_map;
673 unsigned int completed_bkref_map;
674
675 unsigned int has_plural_match : 1;
676 /* If this dfa has "multibyte node", which is a backreference or
677 a node which can accept multibyte character or multi character
678 collating element. */
679 unsigned int has_mb_node : 1;
680 unsigned int is_utf8 : 1;
681 unsigned int map_notascii : 1;
682 unsigned int word_ops_used : 1;
683 int mb_cur_max;
684 bitset word_char;
685 reg_syntax_t syntax;
686 int *subexp_map;
687#ifdef DEBUG
688 char* re_str;
689#endif
690};
691
692#ifndef RE_NO_INTERNAL_PROTOTYPES
693static reg_errcode_t re_node_set_alloc (re_node_set *set, int size) internal_function;
694static reg_errcode_t re_node_set_init_1 (re_node_set *set, int elem) internal_function;
695static reg_errcode_t re_node_set_init_2 (re_node_set *set, int elem1,
696 int elem2) internal_function;
697#define re_node_set_init_empty(set) memset (set, '\0', sizeof (re_node_set))
698static reg_errcode_t re_node_set_init_copy (re_node_set *dest,
699 const re_node_set *src) internal_function;
700static reg_errcode_t re_node_set_add_intersect (re_node_set *dest,
701 const re_node_set *src1,
702 const re_node_set *src2) internal_function;
703static reg_errcode_t re_node_set_init_union (re_node_set *dest,
704 const re_node_set *src1,
705 const re_node_set *src2) internal_function;
706static reg_errcode_t re_node_set_merge (re_node_set *dest,
707 const re_node_set *src) internal_function;
708static int re_node_set_insert (re_node_set *set, int elem) internal_function;
709static int re_node_set_insert_last (re_node_set *set,
710 int elem) internal_function;
711static int re_node_set_compare (const re_node_set *set1,
712 const re_node_set *set2)
713 internal_function __attribute ((pure));
714static int re_node_set_contains (const re_node_set *set, int elem)
715 internal_function __attribute ((pure));
716static void re_node_set_remove_at (re_node_set *set, int idx) internal_function;
717#define re_node_set_remove(set,id) \
718 (re_node_set_remove_at (set, re_node_set_contains (set, id) - 1))
719#define re_node_set_empty(p) ((p)->nelem = 0)
720#define re_node_set_free(set) re_free ((set)->elems)
721static int re_dfa_add_node (re_dfa_t *dfa, re_token_t token) internal_function;
722static re_dfastate_t *re_acquire_state (reg_errcode_t *err, re_dfa_t *dfa,
723 const re_node_set *nodes) internal_function;
724static re_dfastate_t *re_acquire_state_context (reg_errcode_t *err,
725 re_dfa_t *dfa,
726 const re_node_set *nodes,
727 unsigned int context) internal_function;
728static void free_state (re_dfastate_t *state) internal_function;
729#endif
730
731
732
733typedef enum
734{
735 SB_CHAR,
736 MB_CHAR,
737 EQUIV_CLASS,
738 COLL_SYM,
739 CHAR_CLASS
740} bracket_elem_type;
741
742typedef struct
743{
744 bracket_elem_type type;
745 union
746 {
747 unsigned char ch;
748 unsigned char *name;
749 wchar_t wch;
750 } opr;
751} bracket_elem_t;
752
753
754/* Inline functions for bitset operation. */
755static inline void
756bitset_not (bitset set)
757{
758 int bitset_i;
759 for (bitset_i = 0; bitset_i < BITSET_UINTS; ++bitset_i)
760 set[bitset_i] = ~set[bitset_i];
761}
762
763static inline void
764bitset_merge (bitset dest, const bitset src)
765{
766 int bitset_i;
767 for (bitset_i = 0; bitset_i < BITSET_UINTS; ++bitset_i)
768 dest[bitset_i] |= src[bitset_i];
769}
770
771static inline void
772bitset_not_merge (bitset dest, const bitset src)
773{
774 int i;
775 for (i = 0; i < BITSET_UINTS; ++i)
776 dest[i] |= ~src[i];
777}
778
779static inline void
780bitset_mask (bitset dest, const bitset src)
781{
782 int bitset_i;
783 for (bitset_i = 0; bitset_i < BITSET_UINTS; ++bitset_i)
784 dest[bitset_i] &= src[bitset_i];
785}
786
787#if defined RE_ENABLE_I18N && !defined RE_NO_INTERNAL_PROTOTYPES
788/* Inline functions for re_string. */
789static inline int
790internal_function
791re_string_char_size_at (const re_string_t *pstr, int idx)
792{
793 int byte_idx;
794 if (pstr->mb_cur_max == 1)
795 return 1;
796 for (byte_idx = 1; idx + byte_idx < pstr->valid_len; ++byte_idx)
797 if (pstr->wcs[idx + byte_idx] != WEOF)
798 break;
799 return byte_idx;
800}
801
802static inline wint_t
803internal_function
804re_string_wchar_at (const re_string_t *pstr, int idx)
805{
806 if (pstr->mb_cur_max == 1)
807 return (wint_t) pstr->mbs[idx];
808 return (wint_t) pstr->wcs[idx];
809}
810
811static int
812internal_function
813re_string_elem_size_at (const re_string_t *pstr, int idx)
814{
815#ifdef _LIBC
816 const unsigned char *p, *extra;
817 const int32_t *table, *indirect;
818 int32_t tmp;
819# include <locale/weight.h>
820 uint_fast32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
821
822 if (nrules != 0)
823 {
824 table = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
825 extra = (const unsigned char *)
826 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB);
827 indirect = (const int32_t *) _NL_CURRENT (LC_COLLATE,
828 _NL_COLLATE_INDIRECTMB);
829 p = pstr->mbs + idx;
830 tmp = findidx (&p);
831 return p - pstr->mbs - idx;
832 }
833 else
834#endif /* _LIBC */
835 return 1;
836}
837#endif /* RE_ENABLE_I18N */
838
839#endif /* _REGEX_INTERNAL_H */
Note: See TracBrowser for help on using the repository browser.