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 |
|
---|
56 | static int
|
---|
57 | is_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 |
|
---|
149 | extern const ERRMSG_TYPE __re_error_msgid[] attribute_hidden;
|
---|
150 | extern 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)
|
---|
156 | typedef unsigned int bitset[BITSET_UINTS];
|
---|
157 | typedef unsigned int *re_bitset_ptr_t;
|
---|
158 | typedef 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)
|
---|
168 | static inline void bitset_not (bitset set);
|
---|
169 | static inline void bitset_merge (bitset dest, const bitset src);
|
---|
170 | static inline void bitset_not_merge (bitset dest, const bitset src);
|
---|
171 | static 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 |
|
---|
184 | typedef 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 |
|
---|
198 | typedef struct
|
---|
199 | {
|
---|
200 | int alloc;
|
---|
201 | int nelem;
|
---|
202 | int *elems;
|
---|
203 | } re_node_set;
|
---|
204 |
|
---|
205 | typedef 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
|
---|
257 | typedef 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 |
|
---|
304 | typedef 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 |
|
---|
335 | struct 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 | };
|
---|
390 | typedef struct re_string_t re_string_t;
|
---|
391 |
|
---|
392 |
|
---|
393 | struct re_dfa_t;
|
---|
394 | typedef struct re_dfa_t re_dfa_t;
|
---|
395 |
|
---|
396 | #ifndef _LIBC
|
---|
397 | # ifdef __i386__
|
---|
398 | # define internal_function __attribute ((regparm (3), stdcall))
|
---|
399 | # else
|
---|
400 | # define internal_function
|
---|
401 | # endif
|
---|
402 | #endif
|
---|
403 |
|
---|
404 | #ifndef RE_NO_INTERNAL_PROTOTYPES
|
---|
405 | static reg_errcode_t re_string_allocate (re_string_t *pstr, const char *str,
|
---|
406 | int len, int init_len,
|
---|
407 | RE_TRANSLATE_TYPE trans, int icase,
|
---|
408 | const re_dfa_t *dfa)
|
---|
409 | internal_function;
|
---|
410 | static reg_errcode_t re_string_construct (re_string_t *pstr, const char *str,
|
---|
411 | int len, RE_TRANSLATE_TYPE trans,
|
---|
412 | int icase, const re_dfa_t *dfa)
|
---|
413 | internal_function;
|
---|
414 | static reg_errcode_t re_string_reconstruct (re_string_t *pstr, int idx,
|
---|
415 | int eflags) internal_function;
|
---|
416 | static reg_errcode_t re_string_realloc_buffers (re_string_t *pstr,
|
---|
417 | int new_buf_len)
|
---|
418 | internal_function;
|
---|
419 | # ifdef RE_ENABLE_I18N
|
---|
420 | static void build_wcs_buffer (re_string_t *pstr) internal_function;
|
---|
421 | static int build_wcs_upper_buffer (re_string_t *pstr) internal_function;
|
---|
422 | # endif /* RE_ENABLE_I18N */
|
---|
423 | static void build_upper_buffer (re_string_t *pstr) internal_function;
|
---|
424 | static void re_string_translate_buffer (re_string_t *pstr) internal_function;
|
---|
425 | static void re_string_destruct (re_string_t *pstr) internal_function;
|
---|
426 | # ifdef RE_ENABLE_I18N
|
---|
427 | static int re_string_elem_size_at (const re_string_t *pstr, int idx)
|
---|
428 | internal_function __attribute ((pure));
|
---|
429 | static inline int re_string_char_size_at (const re_string_t *pstr, int idx)
|
---|
430 | internal_function __attribute ((pure));
|
---|
431 | static inline wint_t re_string_wchar_at (const re_string_t *pstr, int idx)
|
---|
432 | internal_function __attribute ((pure));
|
---|
433 | # endif /* RE_ENABLE_I18N */
|
---|
434 | static unsigned int re_string_context_at (const re_string_t *input, int idx,
|
---|
435 | int eflags)
|
---|
436 | internal_function __attribute ((pure));
|
---|
437 | static unsigned char re_string_peek_byte_case (const re_string_t *pstr,
|
---|
438 | int idx)
|
---|
439 | internal_function __attribute ((pure));
|
---|
440 | static unsigned char re_string_fetch_byte_case (re_string_t *pstr)
|
---|
441 | internal_function __attribute ((pure));
|
---|
442 | #endif
|
---|
443 | #define re_string_peek_byte(pstr, offset) \
|
---|
444 | ((pstr)->mbs[(pstr)->cur_idx + offset])
|
---|
445 | #define re_string_fetch_byte(pstr) \
|
---|
446 | ((pstr)->mbs[(pstr)->cur_idx++])
|
---|
447 | #define re_string_first_byte(pstr, idx) \
|
---|
448 | ((idx) == (pstr)->valid_len || (pstr)->wcs[idx] != WEOF)
|
---|
449 | #define re_string_is_single_byte_char(pstr, idx) \
|
---|
450 | ((pstr)->wcs[idx] != WEOF && ((pstr)->valid_len == (idx) + 1 \
|
---|
451 | || (pstr)->wcs[(idx) + 1] != WEOF))
|
---|
452 | #define re_string_eoi(pstr) ((pstr)->stop <= (pstr)->cur_idx)
|
---|
453 | #define re_string_cur_idx(pstr) ((pstr)->cur_idx)
|
---|
454 | #define re_string_get_buffer(pstr) ((pstr)->mbs)
|
---|
455 | #define re_string_length(pstr) ((pstr)->len)
|
---|
456 | #define re_string_byte_at(pstr,idx) ((pstr)->mbs[idx])
|
---|
457 | #define re_string_skip_bytes(pstr,idx) ((pstr)->cur_idx += (idx))
|
---|
458 | #define re_string_set_index(pstr,idx) ((pstr)->cur_idx = (idx))
|
---|
459 |
|
---|
460 | #define re_malloc(t,n) ((t *) malloc ((n) * sizeof (t)))
|
---|
461 | /* SunOS 4.1.x realloc doesn't accept null pointers: pre-Standard C. Sigh. */
|
---|
462 | #define re_realloc(p,t,n) ((p != NULL) ? (t *) realloc (p,(n)*sizeof(t)) : (t *) calloc(n,sizeof(t)))
|
---|
463 | #define re_free(p) free (p)
|
---|
464 |
|
---|
465 | struct bin_tree_t
|
---|
466 | {
|
---|
467 | struct bin_tree_t *parent;
|
---|
468 | struct bin_tree_t *left;
|
---|
469 | struct bin_tree_t *right;
|
---|
470 | struct bin_tree_t *first;
|
---|
471 | struct bin_tree_t *next;
|
---|
472 |
|
---|
473 | re_token_t token;
|
---|
474 |
|
---|
475 | /* `node_idx' is the index in dfa->nodes, if `type' == 0.
|
---|
476 | Otherwise `type' indicate the type of this node. */
|
---|
477 | int node_idx;
|
---|
478 | };
|
---|
479 | typedef struct bin_tree_t bin_tree_t;
|
---|
480 |
|
---|
481 | #define BIN_TREE_STORAGE_SIZE \
|
---|
482 | ((1024 - sizeof (void *)) / sizeof (bin_tree_t))
|
---|
483 |
|
---|
484 | struct bin_tree_storage_t
|
---|
485 | {
|
---|
486 | struct bin_tree_storage_t *next;
|
---|
487 | bin_tree_t data[BIN_TREE_STORAGE_SIZE];
|
---|
488 | };
|
---|
489 | typedef struct bin_tree_storage_t bin_tree_storage_t;
|
---|
490 |
|
---|
491 | #define CONTEXT_WORD 1
|
---|
492 | #define CONTEXT_NEWLINE (CONTEXT_WORD << 1)
|
---|
493 | #define CONTEXT_BEGBUF (CONTEXT_NEWLINE << 1)
|
---|
494 | #define CONTEXT_ENDBUF (CONTEXT_BEGBUF << 1)
|
---|
495 |
|
---|
496 | #define IS_WORD_CONTEXT(c) ((c) & CONTEXT_WORD)
|
---|
497 | #define IS_NEWLINE_CONTEXT(c) ((c) & CONTEXT_NEWLINE)
|
---|
498 | #define IS_BEGBUF_CONTEXT(c) ((c) & CONTEXT_BEGBUF)
|
---|
499 | #define IS_ENDBUF_CONTEXT(c) ((c) & CONTEXT_ENDBUF)
|
---|
500 | #define IS_ORDINARY_CONTEXT(c) ((c) == 0)
|
---|
501 |
|
---|
502 | #define IS_WORD_CHAR(ch) (isalnum (ch) || (ch) == '_')
|
---|
503 | #define IS_NEWLINE(ch) ((ch) == NEWLINE_CHAR)
|
---|
504 | #define IS_WIDE_WORD_CHAR(ch) (iswalnum (ch) || (ch) == L'_')
|
---|
505 | #define IS_WIDE_NEWLINE(ch) ((ch) == WIDE_NEWLINE_CHAR)
|
---|
506 |
|
---|
507 | #define NOT_SATISFY_PREV_CONSTRAINT(constraint,context) \
|
---|
508 | ((((constraint) & PREV_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
|
---|
509 | || ((constraint & PREV_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
|
---|
510 | || ((constraint & PREV_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context))\
|
---|
511 | || ((constraint & PREV_BEGBUF_CONSTRAINT) && !IS_BEGBUF_CONTEXT (context)))
|
---|
512 |
|
---|
513 | #define NOT_SATISFY_NEXT_CONSTRAINT(constraint,context) \
|
---|
514 | ((((constraint) & NEXT_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
|
---|
515 | || (((constraint) & NEXT_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
|
---|
516 | || (((constraint) & NEXT_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context)) \
|
---|
517 | || (((constraint) & NEXT_ENDBUF_CONSTRAINT) && !IS_ENDBUF_CONTEXT (context)))
|
---|
518 |
|
---|
519 | struct re_dfastate_t
|
---|
520 | {
|
---|
521 | unsigned int hash;
|
---|
522 | re_node_set nodes;
|
---|
523 | re_node_set non_eps_nodes;
|
---|
524 | re_node_set inveclosure;
|
---|
525 | re_node_set *entrance_nodes;
|
---|
526 | struct re_dfastate_t **trtable, **word_trtable;
|
---|
527 | unsigned int context : 4;
|
---|
528 | unsigned int halt : 1;
|
---|
529 | /* If this state can accept `multi byte'.
|
---|
530 | Note that we refer to multibyte characters, and multi character
|
---|
531 | collating elements as `multi byte'. */
|
---|
532 | unsigned int accept_mb : 1;
|
---|
533 | /* If this state has backreference node(s). */
|
---|
534 | unsigned int has_backref : 1;
|
---|
535 | unsigned int has_constraint : 1;
|
---|
536 | };
|
---|
537 | typedef struct re_dfastate_t re_dfastate_t;
|
---|
538 |
|
---|
539 | struct re_state_table_entry
|
---|
540 | {
|
---|
541 | int num;
|
---|
542 | int alloc;
|
---|
543 | re_dfastate_t **array;
|
---|
544 | };
|
---|
545 |
|
---|
546 | /* Array type used in re_sub_match_last_t and re_sub_match_top_t. */
|
---|
547 |
|
---|
548 | typedef struct
|
---|
549 | {
|
---|
550 | int next_idx;
|
---|
551 | int alloc;
|
---|
552 | re_dfastate_t **array;
|
---|
553 | } state_array_t;
|
---|
554 |
|
---|
555 | /* Store information about the node NODE whose type is OP_CLOSE_SUBEXP. */
|
---|
556 |
|
---|
557 | typedef struct
|
---|
558 | {
|
---|
559 | int node;
|
---|
560 | int str_idx; /* The position NODE match at. */
|
---|
561 | state_array_t path;
|
---|
562 | } re_sub_match_last_t;
|
---|
563 |
|
---|
564 | /* Store information about the node NODE whose type is OP_OPEN_SUBEXP.
|
---|
565 | And information about the node, whose type is OP_CLOSE_SUBEXP,
|
---|
566 | corresponding to NODE is stored in LASTS. */
|
---|
567 |
|
---|
568 | typedef struct
|
---|
569 | {
|
---|
570 | int str_idx;
|
---|
571 | int node;
|
---|
572 | int next_last_offset;
|
---|
573 | state_array_t *path;
|
---|
574 | int alasts; /* Allocation size of LASTS. */
|
---|
575 | int nlasts; /* The number of LASTS. */
|
---|
576 | re_sub_match_last_t **lasts;
|
---|
577 | } re_sub_match_top_t;
|
---|
578 |
|
---|
579 | struct re_backref_cache_entry
|
---|
580 | {
|
---|
581 | int node;
|
---|
582 | int str_idx;
|
---|
583 | int subexp_from;
|
---|
584 | int subexp_to;
|
---|
585 | char more;
|
---|
586 | char unused;
|
---|
587 | unsigned short int eps_reachable_subexps_map;
|
---|
588 | };
|
---|
589 |
|
---|
590 | typedef struct
|
---|
591 | {
|
---|
592 | /* The string object corresponding to the input string. */
|
---|
593 | re_string_t input;
|
---|
594 | #if defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L)
|
---|
595 | re_dfa_t *const dfa;
|
---|
596 | #else
|
---|
597 | re_dfa_t *dfa;
|
---|
598 | #endif
|
---|
599 | /* EFLAGS of the argument of regexec. */
|
---|
600 | int eflags;
|
---|
601 | /* Where the matching ends. */
|
---|
602 | int match_last;
|
---|
603 | int last_node;
|
---|
604 | /* The state log used by the matcher. */
|
---|
605 | re_dfastate_t **state_log;
|
---|
606 | int state_log_top;
|
---|
607 | /* Back reference cache. */
|
---|
608 | int nbkref_ents;
|
---|
609 | int abkref_ents;
|
---|
610 | struct re_backref_cache_entry *bkref_ents;
|
---|
611 | int max_mb_elem_len;
|
---|
612 | int nsub_tops;
|
---|
613 | int asub_tops;
|
---|
614 | re_sub_match_top_t **sub_tops;
|
---|
615 | } re_match_context_t;
|
---|
616 |
|
---|
617 | typedef struct
|
---|
618 | {
|
---|
619 | re_dfastate_t **sifted_states;
|
---|
620 | re_dfastate_t **limited_states;
|
---|
621 | int last_node;
|
---|
622 | int last_str_idx;
|
---|
623 | re_node_set limits;
|
---|
624 | } re_sift_context_t;
|
---|
625 |
|
---|
626 | struct re_fail_stack_ent_t
|
---|
627 | {
|
---|
628 | int idx;
|
---|
629 | int node;
|
---|
630 | regmatch_t *regs;
|
---|
631 | re_node_set eps_via_nodes;
|
---|
632 | };
|
---|
633 |
|
---|
634 | struct re_fail_stack_t
|
---|
635 | {
|
---|
636 | int num;
|
---|
637 | int alloc;
|
---|
638 | struct re_fail_stack_ent_t *stack;
|
---|
639 | };
|
---|
640 |
|
---|
641 | struct re_dfa_t
|
---|
642 | {
|
---|
643 | re_token_t *nodes;
|
---|
644 | int nodes_alloc;
|
---|
645 | int nodes_len;
|
---|
646 | int *nexts;
|
---|
647 | int *org_indices;
|
---|
648 | re_node_set *edests;
|
---|
649 | re_node_set *eclosures;
|
---|
650 | re_node_set *inveclosures;
|
---|
651 | struct re_state_table_entry *state_table;
|
---|
652 | re_dfastate_t *init_state;
|
---|
653 | re_dfastate_t *init_state_word;
|
---|
654 | re_dfastate_t *init_state_nl;
|
---|
655 | re_dfastate_t *init_state_begbuf;
|
---|
656 | bin_tree_t *str_tree;
|
---|
657 | bin_tree_storage_t *str_tree_storage;
|
---|
658 | re_bitset_ptr_t sb_char;
|
---|
659 | int str_tree_storage_idx;
|
---|
660 |
|
---|
661 | /* number of subexpressions `re_nsub' is in regex_t. */
|
---|
662 | unsigned int state_hash_mask;
|
---|
663 | int states_alloc;
|
---|
664 | int init_node;
|
---|
665 | int nbackref; /* The number of backreference in this dfa. */
|
---|
666 |
|
---|
667 | /* Bitmap expressing which backreference is used. */
|
---|
668 | unsigned int used_bkref_map;
|
---|
669 | unsigned int completed_bkref_map;
|
---|
670 |
|
---|
671 | unsigned int has_plural_match : 1;
|
---|
672 | /* If this dfa has "multibyte node", which is a backreference or
|
---|
673 | a node which can accept multibyte character or multi character
|
---|
674 | collating element. */
|
---|
675 | unsigned int has_mb_node : 1;
|
---|
676 | unsigned int is_utf8 : 1;
|
---|
677 | unsigned int map_notascii : 1;
|
---|
678 | unsigned int word_ops_used : 1;
|
---|
679 | int mb_cur_max;
|
---|
680 | bitset word_char;
|
---|
681 | reg_syntax_t syntax;
|
---|
682 | int *subexp_map;
|
---|
683 | #ifdef DEBUG
|
---|
684 | char* re_str;
|
---|
685 | #endif
|
---|
686 | };
|
---|
687 |
|
---|
688 | #ifndef RE_NO_INTERNAL_PROTOTYPES
|
---|
689 | static reg_errcode_t re_node_set_alloc (re_node_set *set, int size) internal_function;
|
---|
690 | static reg_errcode_t re_node_set_init_1 (re_node_set *set, int elem) internal_function;
|
---|
691 | static reg_errcode_t re_node_set_init_2 (re_node_set *set, int elem1,
|
---|
692 | int elem2) internal_function;
|
---|
693 | #define re_node_set_init_empty(set) memset (set, '\0', sizeof (re_node_set))
|
---|
694 | static reg_errcode_t re_node_set_init_copy (re_node_set *dest,
|
---|
695 | const re_node_set *src) internal_function;
|
---|
696 | static reg_errcode_t re_node_set_add_intersect (re_node_set *dest,
|
---|
697 | const re_node_set *src1,
|
---|
698 | const re_node_set *src2) internal_function;
|
---|
699 | static reg_errcode_t re_node_set_init_union (re_node_set *dest,
|
---|
700 | const re_node_set *src1,
|
---|
701 | const re_node_set *src2) internal_function;
|
---|
702 | static reg_errcode_t re_node_set_merge (re_node_set *dest,
|
---|
703 | const re_node_set *src) internal_function;
|
---|
704 | static int re_node_set_insert (re_node_set *set, int elem) internal_function;
|
---|
705 | static int re_node_set_insert_last (re_node_set *set,
|
---|
706 | int elem) internal_function;
|
---|
707 | static int re_node_set_compare (const re_node_set *set1,
|
---|
708 | const re_node_set *set2)
|
---|
709 | internal_function __attribute ((pure));
|
---|
710 | static int re_node_set_contains (const re_node_set *set, int elem)
|
---|
711 | internal_function __attribute ((pure));
|
---|
712 | static void re_node_set_remove_at (re_node_set *set, int idx) internal_function;
|
---|
713 | #define re_node_set_remove(set,id) \
|
---|
714 | (re_node_set_remove_at (set, re_node_set_contains (set, id) - 1))
|
---|
715 | #define re_node_set_empty(p) ((p)->nelem = 0)
|
---|
716 | #define re_node_set_free(set) re_free ((set)->elems)
|
---|
717 | static int re_dfa_add_node (re_dfa_t *dfa, re_token_t token) internal_function;
|
---|
718 | static re_dfastate_t *re_acquire_state (reg_errcode_t *err, re_dfa_t *dfa,
|
---|
719 | const re_node_set *nodes) internal_function;
|
---|
720 | static re_dfastate_t *re_acquire_state_context (reg_errcode_t *err,
|
---|
721 | re_dfa_t *dfa,
|
---|
722 | const re_node_set *nodes,
|
---|
723 | unsigned int context) internal_function;
|
---|
724 | static void free_state (re_dfastate_t *state) internal_function;
|
---|
725 | #endif
|
---|
726 | |
---|
727 |
|
---|
728 |
|
---|
729 | typedef enum
|
---|
730 | {
|
---|
731 | SB_CHAR,
|
---|
732 | MB_CHAR,
|
---|
733 | EQUIV_CLASS,
|
---|
734 | COLL_SYM,
|
---|
735 | CHAR_CLASS
|
---|
736 | } bracket_elem_type;
|
---|
737 |
|
---|
738 | typedef struct
|
---|
739 | {
|
---|
740 | bracket_elem_type type;
|
---|
741 | union
|
---|
742 | {
|
---|
743 | unsigned char ch;
|
---|
744 | unsigned char *name;
|
---|
745 | wchar_t wch;
|
---|
746 | } opr;
|
---|
747 | } bracket_elem_t;
|
---|
748 |
|
---|
749 |
|
---|
750 | /* Inline functions for bitset operation. */
|
---|
751 | static inline void
|
---|
752 | bitset_not (bitset set)
|
---|
753 | {
|
---|
754 | int bitset_i;
|
---|
755 | for (bitset_i = 0; bitset_i < BITSET_UINTS; ++bitset_i)
|
---|
756 | set[bitset_i] = ~set[bitset_i];
|
---|
757 | }
|
---|
758 |
|
---|
759 | static inline void
|
---|
760 | bitset_merge (bitset dest, const bitset src)
|
---|
761 | {
|
---|
762 | int bitset_i;
|
---|
763 | for (bitset_i = 0; bitset_i < BITSET_UINTS; ++bitset_i)
|
---|
764 | dest[bitset_i] |= src[bitset_i];
|
---|
765 | }
|
---|
766 |
|
---|
767 | static inline void
|
---|
768 | bitset_not_merge (bitset dest, const bitset src)
|
---|
769 | {
|
---|
770 | int i;
|
---|
771 | for (i = 0; i < BITSET_UINTS; ++i)
|
---|
772 | dest[i] |= ~src[i];
|
---|
773 | }
|
---|
774 |
|
---|
775 | static inline void
|
---|
776 | bitset_mask (bitset dest, const bitset src)
|
---|
777 | {
|
---|
778 | int bitset_i;
|
---|
779 | for (bitset_i = 0; bitset_i < BITSET_UINTS; ++bitset_i)
|
---|
780 | dest[bitset_i] &= src[bitset_i];
|
---|
781 | }
|
---|
782 |
|
---|
783 | #if defined RE_ENABLE_I18N && !defined RE_NO_INTERNAL_PROTOTYPES
|
---|
784 | /* Inline functions for re_string. */
|
---|
785 | static inline int
|
---|
786 | internal_function
|
---|
787 | re_string_char_size_at (const re_string_t *pstr, int idx)
|
---|
788 | {
|
---|
789 | int byte_idx;
|
---|
790 | if (pstr->mb_cur_max == 1)
|
---|
791 | return 1;
|
---|
792 | for (byte_idx = 1; idx + byte_idx < pstr->valid_len; ++byte_idx)
|
---|
793 | if (pstr->wcs[idx + byte_idx] != WEOF)
|
---|
794 | break;
|
---|
795 | return byte_idx;
|
---|
796 | }
|
---|
797 |
|
---|
798 | static inline wint_t
|
---|
799 | internal_function
|
---|
800 | re_string_wchar_at (const re_string_t *pstr, int idx)
|
---|
801 | {
|
---|
802 | if (pstr->mb_cur_max == 1)
|
---|
803 | return (wint_t) pstr->mbs[idx];
|
---|
804 | return (wint_t) pstr->wcs[idx];
|
---|
805 | }
|
---|
806 |
|
---|
807 | static int
|
---|
808 | internal_function
|
---|
809 | re_string_elem_size_at (const re_string_t *pstr, int idx)
|
---|
810 | {
|
---|
811 | #ifdef _LIBC
|
---|
812 | const unsigned char *p, *extra;
|
---|
813 | const int32_t *table, *indirect;
|
---|
814 | int32_t tmp;
|
---|
815 | # include <locale/weight.h>
|
---|
816 | uint_fast32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
|
---|
817 |
|
---|
818 | if (nrules != 0)
|
---|
819 | {
|
---|
820 | table = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
|
---|
821 | extra = (const unsigned char *)
|
---|
822 | _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB);
|
---|
823 | indirect = (const int32_t *) _NL_CURRENT (LC_COLLATE,
|
---|
824 | _NL_COLLATE_INDIRECTMB);
|
---|
825 | p = pstr->mbs + idx;
|
---|
826 | tmp = findidx (&p);
|
---|
827 | return p - pstr->mbs - idx;
|
---|
828 | }
|
---|
829 | else
|
---|
830 | #endif /* _LIBC */
|
---|
831 | return 1;
|
---|
832 | }
|
---|
833 | #endif /* RE_ENABLE_I18N */
|
---|
834 |
|
---|
835 | #endif /* _REGEX_INTERNAL_H */
|
---|