PageRenderTime 77ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/gnu/lib/libregex/regcomp.c

https://bitbucket.org/freebsd/freebsd-head/
C | 3924 lines | 3135 code | 310 blank | 479 comment | 993 complexity | 744e7746d7c578df2d001fa8adab820b MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, LGPL-2.0, LGPL-2.1, BSD-2-Clause, 0BSD, JSON, AGPL-1.0, GPL-2.0
  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. static reg_errcode_t re_compile_internal (regex_t *preg, const char * pattern,
  18. int length, reg_syntax_t syntax);
  19. static void re_compile_fastmap_iter (regex_t *bufp,
  20. const re_dfastate_t *init_state,
  21. char *fastmap);
  22. static reg_errcode_t init_dfa (re_dfa_t *dfa, int pat_len);
  23. static void init_word_char (re_dfa_t *dfa);
  24. #ifdef RE_ENABLE_I18N
  25. static void free_charset (re_charset_t *cset);
  26. #endif /* RE_ENABLE_I18N */
  27. static void free_workarea_compile (regex_t *preg);
  28. static reg_errcode_t create_initial_state (re_dfa_t *dfa);
  29. #ifdef RE_ENABLE_I18N
  30. static void optimize_utf8 (re_dfa_t *dfa);
  31. #endif
  32. static reg_errcode_t analyze (regex_t *preg);
  33. static reg_errcode_t create_initial_state (re_dfa_t *dfa);
  34. static reg_errcode_t preorder (bin_tree_t *root,
  35. reg_errcode_t (fn (void *, bin_tree_t *)),
  36. void *extra);
  37. static reg_errcode_t postorder (bin_tree_t *root,
  38. reg_errcode_t (fn (void *, bin_tree_t *)),
  39. void *extra);
  40. static reg_errcode_t optimize_subexps (void *extra, bin_tree_t *node);
  41. static reg_errcode_t lower_subexps (void *extra, bin_tree_t *node);
  42. static bin_tree_t *lower_subexp (reg_errcode_t *err, regex_t *preg,
  43. bin_tree_t *node);
  44. static reg_errcode_t calc_first (void *extra, bin_tree_t *node);
  45. static reg_errcode_t calc_next (void *extra, bin_tree_t *node);
  46. static reg_errcode_t link_nfa_nodes (void *extra, bin_tree_t *node);
  47. static reg_errcode_t duplicate_node_closure (re_dfa_t *dfa, int top_org_node,
  48. int top_clone_node, int root_node,
  49. unsigned int constraint);
  50. static reg_errcode_t duplicate_node (int *new_idx, re_dfa_t *dfa, int org_idx,
  51. unsigned int constraint);
  52. static int search_duplicated_node (re_dfa_t *dfa, int org_node,
  53. unsigned int constraint);
  54. static reg_errcode_t calc_eclosure (re_dfa_t *dfa);
  55. static reg_errcode_t calc_eclosure_iter (re_node_set *new_set, re_dfa_t *dfa,
  56. int node, int root);
  57. static reg_errcode_t calc_inveclosure (re_dfa_t *dfa);
  58. static int fetch_number (re_string_t *input, re_token_t *token,
  59. reg_syntax_t syntax);
  60. static void fetch_token (re_token_t *result, re_string_t *input,
  61. reg_syntax_t syntax);
  62. static int peek_token (re_token_t *token, re_string_t *input,
  63. reg_syntax_t syntax);
  64. static int peek_token_bracket (re_token_t *token, re_string_t *input,
  65. reg_syntax_t syntax);
  66. static bin_tree_t *parse (re_string_t *regexp, regex_t *preg,
  67. reg_syntax_t syntax, reg_errcode_t *err);
  68. static bin_tree_t *parse_reg_exp (re_string_t *regexp, regex_t *preg,
  69. re_token_t *token, reg_syntax_t syntax,
  70. int nest, reg_errcode_t *err);
  71. static bin_tree_t *parse_branch (re_string_t *regexp, regex_t *preg,
  72. re_token_t *token, reg_syntax_t syntax,
  73. int nest, reg_errcode_t *err);
  74. static bin_tree_t *parse_expression (re_string_t *regexp, regex_t *preg,
  75. re_token_t *token, reg_syntax_t syntax,
  76. int nest, reg_errcode_t *err);
  77. static bin_tree_t *parse_sub_exp (re_string_t *regexp, regex_t *preg,
  78. re_token_t *token, reg_syntax_t syntax,
  79. int nest, reg_errcode_t *err);
  80. static bin_tree_t *parse_dup_op (bin_tree_t *dup_elem, re_string_t *regexp,
  81. re_dfa_t *dfa, re_token_t *token,
  82. reg_syntax_t syntax, reg_errcode_t *err);
  83. static bin_tree_t *parse_bracket_exp (re_string_t *regexp, re_dfa_t *dfa,
  84. re_token_t *token, reg_syntax_t syntax,
  85. reg_errcode_t *err);
  86. static reg_errcode_t parse_bracket_element (bracket_elem_t *elem,
  87. re_string_t *regexp,
  88. re_token_t *token, int token_len,
  89. re_dfa_t *dfa,
  90. reg_syntax_t syntax,
  91. int accept_hyphen);
  92. static reg_errcode_t parse_bracket_symbol (bracket_elem_t *elem,
  93. re_string_t *regexp,
  94. re_token_t *token);
  95. #ifndef _LIBC
  96. # ifdef RE_ENABLE_I18N
  97. static reg_errcode_t build_range_exp (re_bitset_ptr_t sbcset,
  98. re_charset_t *mbcset, int *range_alloc,
  99. bracket_elem_t *start_elem,
  100. bracket_elem_t *end_elem);
  101. static reg_errcode_t build_collating_symbol (re_bitset_ptr_t sbcset,
  102. re_charset_t *mbcset,
  103. int *coll_sym_alloc,
  104. const unsigned char *name);
  105. # else /* not RE_ENABLE_I18N */
  106. static reg_errcode_t build_range_exp (re_bitset_ptr_t sbcset,
  107. bracket_elem_t *start_elem,
  108. bracket_elem_t *end_elem);
  109. static reg_errcode_t build_collating_symbol (re_bitset_ptr_t sbcset,
  110. const unsigned char *name);
  111. # endif /* not RE_ENABLE_I18N */
  112. #endif /* not _LIBC */
  113. #ifdef RE_ENABLE_I18N
  114. static reg_errcode_t build_equiv_class (re_bitset_ptr_t sbcset,
  115. re_charset_t *mbcset,
  116. int *equiv_class_alloc,
  117. const unsigned char *name);
  118. static reg_errcode_t build_charclass (unsigned RE_TRANSLATE_TYPE trans,
  119. re_bitset_ptr_t sbcset,
  120. re_charset_t *mbcset,
  121. int *char_class_alloc,
  122. const unsigned char *class_name,
  123. reg_syntax_t syntax);
  124. #else /* not RE_ENABLE_I18N */
  125. static reg_errcode_t build_equiv_class (re_bitset_ptr_t sbcset,
  126. const unsigned char *name);
  127. static reg_errcode_t build_charclass (unsigned RE_TRANSLATE_TYPE trans,
  128. re_bitset_ptr_t sbcset,
  129. const unsigned char *class_name,
  130. reg_syntax_t syntax);
  131. #endif /* not RE_ENABLE_I18N */
  132. static bin_tree_t *build_charclass_op (re_dfa_t *dfa,
  133. unsigned RE_TRANSLATE_TYPE trans,
  134. const unsigned char *class_name,
  135. const unsigned char *extra,
  136. int non_match, reg_errcode_t *err);
  137. static bin_tree_t *create_tree (re_dfa_t *dfa,
  138. bin_tree_t *left, bin_tree_t *right,
  139. re_token_type_t type);
  140. static bin_tree_t *create_token_tree (re_dfa_t *dfa,
  141. bin_tree_t *left, bin_tree_t *right,
  142. const re_token_t *token);
  143. static bin_tree_t *duplicate_tree (const bin_tree_t *src, re_dfa_t *dfa);
  144. static void free_token (re_token_t *node);
  145. static reg_errcode_t free_tree (void *extra, bin_tree_t *node);
  146. static reg_errcode_t mark_opt_subexp (void *extra, bin_tree_t *node);
  147. /* This table gives an error message for each of the error codes listed
  148. in regex.h. Obviously the order here has to be same as there.
  149. POSIX doesn't require that we do anything for REG_NOERROR,
  150. but why not be nice? */
  151. const char __re_error_msgid[] attribute_hidden =
  152. {
  153. #define REG_NOERROR_IDX 0
  154. gettext_noop ("Success") /* REG_NOERROR */
  155. "\0"
  156. #define REG_NOMATCH_IDX (REG_NOERROR_IDX + sizeof "Success")
  157. gettext_noop ("No match") /* REG_NOMATCH */
  158. "\0"
  159. #define REG_BADPAT_IDX (REG_NOMATCH_IDX + sizeof "No match")
  160. gettext_noop ("Invalid regular expression") /* REG_BADPAT */
  161. "\0"
  162. #define REG_ECOLLATE_IDX (REG_BADPAT_IDX + sizeof "Invalid regular expression")
  163. gettext_noop ("Invalid collation character") /* REG_ECOLLATE */
  164. "\0"
  165. #define REG_ECTYPE_IDX (REG_ECOLLATE_IDX + sizeof "Invalid collation character")
  166. gettext_noop ("Invalid character class name") /* REG_ECTYPE */
  167. "\0"
  168. #define REG_EESCAPE_IDX (REG_ECTYPE_IDX + sizeof "Invalid character class name")
  169. gettext_noop ("Trailing backslash") /* REG_EESCAPE */
  170. "\0"
  171. #define REG_ESUBREG_IDX (REG_EESCAPE_IDX + sizeof "Trailing backslash")
  172. gettext_noop ("Invalid back reference") /* REG_ESUBREG */
  173. "\0"
  174. #define REG_EBRACK_IDX (REG_ESUBREG_IDX + sizeof "Invalid back reference")
  175. gettext_noop ("Unmatched [ or [^") /* REG_EBRACK */
  176. "\0"
  177. #define REG_EPAREN_IDX (REG_EBRACK_IDX + sizeof "Unmatched [ or [^")
  178. gettext_noop ("Unmatched ( or \\(") /* REG_EPAREN */
  179. "\0"
  180. #define REG_EBRACE_IDX (REG_EPAREN_IDX + sizeof "Unmatched ( or \\(")
  181. gettext_noop ("Unmatched \\{") /* REG_EBRACE */
  182. "\0"
  183. #define REG_BADBR_IDX (REG_EBRACE_IDX + sizeof "Unmatched \\{")
  184. gettext_noop ("Invalid content of \\{\\}") /* REG_BADBR */
  185. "\0"
  186. #define REG_ERANGE_IDX (REG_BADBR_IDX + sizeof "Invalid content of \\{\\}")
  187. gettext_noop ("Invalid range end") /* REG_ERANGE */
  188. "\0"
  189. #define REG_ESPACE_IDX (REG_ERANGE_IDX + sizeof "Invalid range end")
  190. gettext_noop ("Memory exhausted") /* REG_ESPACE */
  191. "\0"
  192. #define REG_BADRPT_IDX (REG_ESPACE_IDX + sizeof "Memory exhausted")
  193. gettext_noop ("Invalid preceding regular expression") /* REG_BADRPT */
  194. "\0"
  195. #define REG_EEND_IDX (REG_BADRPT_IDX + sizeof "Invalid preceding regular expression")
  196. gettext_noop ("Premature end of regular expression") /* REG_EEND */
  197. "\0"
  198. #define REG_ESIZE_IDX (REG_EEND_IDX + sizeof "Premature end of regular expression")
  199. gettext_noop ("Regular expression too big") /* REG_ESIZE */
  200. "\0"
  201. #define REG_ERPAREN_IDX (REG_ESIZE_IDX + sizeof "Regular expression too big")
  202. gettext_noop ("Unmatched ) or \\)") /* REG_ERPAREN */
  203. };
  204. const size_t __re_error_msgid_idx[] attribute_hidden =
  205. {
  206. REG_NOERROR_IDX,
  207. REG_NOMATCH_IDX,
  208. REG_BADPAT_IDX,
  209. REG_ECOLLATE_IDX,
  210. REG_ECTYPE_IDX,
  211. REG_EESCAPE_IDX,
  212. REG_ESUBREG_IDX,
  213. REG_EBRACK_IDX,
  214. REG_EPAREN_IDX,
  215. REG_EBRACE_IDX,
  216. REG_BADBR_IDX,
  217. REG_ERANGE_IDX,
  218. REG_ESPACE_IDX,
  219. REG_BADRPT_IDX,
  220. REG_EEND_IDX,
  221. REG_ESIZE_IDX,
  222. REG_ERPAREN_IDX
  223. };
  224. /* Entry points for GNU code. */
  225. /* re_compile_pattern is the GNU regular expression compiler: it
  226. compiles PATTERN (of length LENGTH) and puts the result in BUFP.
  227. Returns 0 if the pattern was valid, otherwise an error string.
  228. Assumes the `allocated' (and perhaps `buffer') and `translate' fields
  229. are set in BUFP on entry. */
  230. const char *
  231. re_compile_pattern (pattern, length, bufp)
  232. const char *pattern;
  233. size_t length;
  234. struct re_pattern_buffer *bufp;
  235. {
  236. reg_errcode_t ret;
  237. /* And GNU code determines whether or not to get register information
  238. by passing null for the REGS argument to re_match, etc., not by
  239. setting no_sub, unless RE_NO_SUB is set. */
  240. bufp->no_sub = !!(re_syntax_options & RE_NO_SUB);
  241. /* Match anchors at newline. */
  242. bufp->newline_anchor = 1;
  243. ret = re_compile_internal (bufp, pattern, length, re_syntax_options);
  244. if (!ret)
  245. return NULL;
  246. return gettext (__re_error_msgid + __re_error_msgid_idx[(int) ret]);
  247. }
  248. #ifdef _LIBC
  249. weak_alias (__re_compile_pattern, re_compile_pattern)
  250. #endif
  251. /* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
  252. also be assigned to arbitrarily: each pattern buffer stores its own
  253. syntax, so it can be changed between regex compilations. */
  254. /* This has no initializer because initialized variables in Emacs
  255. become read-only after dumping. */
  256. reg_syntax_t re_syntax_options;
  257. /* Specify the precise syntax of regexps for compilation. This provides
  258. for compatibility for various utilities which historically have
  259. different, incompatible syntaxes.
  260. The argument SYNTAX is a bit mask comprised of the various bits
  261. defined in regex.h. We return the old syntax. */
  262. reg_syntax_t
  263. re_set_syntax (syntax)
  264. reg_syntax_t syntax;
  265. {
  266. reg_syntax_t ret = re_syntax_options;
  267. re_syntax_options = syntax;
  268. return ret;
  269. }
  270. #ifdef _LIBC
  271. weak_alias (__re_set_syntax, re_set_syntax)
  272. #endif
  273. int
  274. re_compile_fastmap (bufp)
  275. struct re_pattern_buffer *bufp;
  276. {
  277. re_dfa_t *dfa = (re_dfa_t *) bufp->buffer;
  278. char *fastmap = bufp->fastmap;
  279. memset (fastmap, '\0', sizeof (char) * SBC_MAX);
  280. re_compile_fastmap_iter (bufp, dfa->init_state, fastmap);
  281. if (dfa->init_state != dfa->init_state_word)
  282. re_compile_fastmap_iter (bufp, dfa->init_state_word, fastmap);
  283. if (dfa->init_state != dfa->init_state_nl)
  284. re_compile_fastmap_iter (bufp, dfa->init_state_nl, fastmap);
  285. if (dfa->init_state != dfa->init_state_begbuf)
  286. re_compile_fastmap_iter (bufp, dfa->init_state_begbuf, fastmap);
  287. bufp->fastmap_accurate = 1;
  288. return 0;
  289. }
  290. #ifdef _LIBC
  291. weak_alias (__re_compile_fastmap, re_compile_fastmap)
  292. #endif
  293. static inline void
  294. __attribute ((always_inline))
  295. re_set_fastmap (char *fastmap, int icase, int ch)
  296. {
  297. fastmap[ch] = 1;
  298. if (icase)
  299. fastmap[tolower (ch)] = 1;
  300. }
  301. /* Helper function for re_compile_fastmap.
  302. Compile fastmap for the initial_state INIT_STATE. */
  303. static void
  304. re_compile_fastmap_iter (bufp, init_state, fastmap)
  305. regex_t *bufp;
  306. const re_dfastate_t *init_state;
  307. char *fastmap;
  308. {
  309. re_dfa_t *dfa = (re_dfa_t *) bufp->buffer;
  310. int node_cnt;
  311. int icase = (dfa->mb_cur_max == 1 && (bufp->syntax & RE_ICASE));
  312. for (node_cnt = 0; node_cnt < init_state->nodes.nelem; ++node_cnt)
  313. {
  314. int node = init_state->nodes.elems[node_cnt];
  315. re_token_type_t type = dfa->nodes[node].type;
  316. if (type == CHARACTER)
  317. {
  318. re_set_fastmap (fastmap, icase, dfa->nodes[node].opr.c);
  319. #ifdef RE_ENABLE_I18N
  320. if ((bufp->syntax & RE_ICASE) && dfa->mb_cur_max > 1)
  321. {
  322. unsigned char *buf = alloca (dfa->mb_cur_max), *p;
  323. wchar_t wc;
  324. mbstate_t state;
  325. p = buf;
  326. *p++ = dfa->nodes[node].opr.c;
  327. while (++node < dfa->nodes_len
  328. && dfa->nodes[node].type == CHARACTER
  329. && dfa->nodes[node].mb_partial)
  330. *p++ = dfa->nodes[node].opr.c;
  331. memset (&state, 0, sizeof (state));
  332. if (mbrtowc (&wc, (const char *) buf, p - buf,
  333. &state) == p - buf
  334. && (__wcrtomb ((char *) buf, towlower (wc), &state)
  335. != (size_t) -1))
  336. re_set_fastmap (fastmap, 0, buf[0]);
  337. }
  338. #endif
  339. }
  340. else if (type == SIMPLE_BRACKET)
  341. {
  342. int i, j, ch;
  343. for (i = 0, ch = 0; i < BITSET_UINTS; ++i)
  344. for (j = 0; j < UINT_BITS; ++j, ++ch)
  345. if (dfa->nodes[node].opr.sbcset[i] & (1 << j))
  346. re_set_fastmap (fastmap, icase, ch);
  347. }
  348. #ifdef RE_ENABLE_I18N
  349. else if (type == COMPLEX_BRACKET)
  350. {
  351. int i;
  352. re_charset_t *cset = dfa->nodes[node].opr.mbcset;
  353. if (cset->non_match || cset->ncoll_syms || cset->nequiv_classes
  354. || cset->nranges || cset->nchar_classes)
  355. {
  356. # ifdef _LIBC
  357. if (_NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES) != 0)
  358. {
  359. /* In this case we want to catch the bytes which are
  360. the first byte of any collation elements.
  361. e.g. In da_DK, we want to catch 'a' since "aa"
  362. is a valid collation element, and don't catch
  363. 'b' since 'b' is the only collation element
  364. which starts from 'b'. */
  365. int j, ch;
  366. const int32_t *table = (const int32_t *)
  367. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
  368. for (i = 0, ch = 0; i < BITSET_UINTS; ++i)
  369. for (j = 0; j < UINT_BITS; ++j, ++ch)
  370. if (table[ch] < 0)
  371. re_set_fastmap (fastmap, icase, ch);
  372. }
  373. # else
  374. if (dfa->mb_cur_max > 1)
  375. for (i = 0; i < SBC_MAX; ++i)
  376. if (__btowc (i) == WEOF)
  377. re_set_fastmap (fastmap, icase, i);
  378. # endif /* not _LIBC */
  379. }
  380. for (i = 0; i < cset->nmbchars; ++i)
  381. {
  382. char buf[256];
  383. mbstate_t state;
  384. memset (&state, '\0', sizeof (state));
  385. if (__wcrtomb (buf, cset->mbchars[i], &state) != (size_t) -1)
  386. re_set_fastmap (fastmap, icase, *(unsigned char *) buf);
  387. if ((bufp->syntax & RE_ICASE) && dfa->mb_cur_max > 1)
  388. {
  389. if (__wcrtomb (buf, towlower (cset->mbchars[i]), &state)
  390. != (size_t) -1)
  391. re_set_fastmap (fastmap, 0, *(unsigned char *) buf);
  392. }
  393. }
  394. }
  395. #endif /* RE_ENABLE_I18N */
  396. else if (type == OP_PERIOD
  397. #ifdef RE_ENABLE_I18N
  398. || type == OP_UTF8_PERIOD
  399. #endif /* RE_ENABLE_I18N */
  400. || type == END_OF_RE)
  401. {
  402. memset (fastmap, '\1', sizeof (char) * SBC_MAX);
  403. if (type == END_OF_RE)
  404. bufp->can_be_null = 1;
  405. return;
  406. }
  407. }
  408. }
  409. /* Entry point for POSIX code. */
  410. /* regcomp takes a regular expression as a string and compiles it.
  411. PREG is a regex_t *. We do not expect any fields to be initialized,
  412. since POSIX says we shouldn't. Thus, we set
  413. `buffer' to the compiled pattern;
  414. `used' to the length of the compiled pattern;
  415. `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
  416. REG_EXTENDED bit in CFLAGS is set; otherwise, to
  417. RE_SYNTAX_POSIX_BASIC;
  418. `newline_anchor' to REG_NEWLINE being set in CFLAGS;
  419. `fastmap' to an allocated space for the fastmap;
  420. `fastmap_accurate' to zero;
  421. `re_nsub' to the number of subexpressions in PATTERN.
  422. PATTERN is the address of the pattern string.
  423. CFLAGS is a series of bits which affect compilation.
  424. If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
  425. use POSIX basic syntax.
  426. If REG_NEWLINE is set, then . and [^...] don't match newline.
  427. Also, regexec will try a match beginning after every newline.
  428. If REG_ICASE is set, then we considers upper- and lowercase
  429. versions of letters to be equivalent when matching.
  430. If REG_NOSUB is set, then when PREG is passed to regexec, that
  431. routine will report only success or failure, and nothing about the
  432. registers.
  433. It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
  434. the return codes and their meanings.) */
  435. int
  436. regcomp (preg, pattern, cflags)
  437. regex_t *__restrict preg;
  438. const char *__restrict pattern;
  439. int cflags;
  440. {
  441. reg_errcode_t ret;
  442. reg_syntax_t syntax = ((cflags & REG_EXTENDED) ? RE_SYNTAX_POSIX_EXTENDED
  443. : RE_SYNTAX_POSIX_BASIC);
  444. preg->buffer = NULL;
  445. preg->allocated = 0;
  446. preg->used = 0;
  447. /* Try to allocate space for the fastmap. */
  448. preg->fastmap = re_malloc (char, SBC_MAX);
  449. if (BE (preg->fastmap == NULL, 0))
  450. return REG_ESPACE;
  451. syntax |= (cflags & REG_ICASE) ? RE_ICASE : 0;
  452. /* If REG_NEWLINE is set, newlines are treated differently. */
  453. if (cflags & REG_NEWLINE)
  454. { /* REG_NEWLINE implies neither . nor [^...] match newline. */
  455. syntax &= ~RE_DOT_NEWLINE;
  456. syntax |= RE_HAT_LISTS_NOT_NEWLINE;
  457. /* It also changes the matching behavior. */
  458. preg->newline_anchor = 1;
  459. }
  460. else
  461. preg->newline_anchor = 0;
  462. preg->no_sub = !!(cflags & REG_NOSUB);
  463. preg->translate = NULL;
  464. ret = re_compile_internal (preg, pattern, strlen (pattern), syntax);
  465. /* POSIX doesn't distinguish between an unmatched open-group and an
  466. unmatched close-group: both are REG_EPAREN. */
  467. if (ret == REG_ERPAREN)
  468. ret = REG_EPAREN;
  469. /* We have already checked preg->fastmap != NULL. */
  470. if (BE (ret == REG_NOERROR, 1))
  471. /* Compute the fastmap now, since regexec cannot modify the pattern
  472. buffer. This function never fails in this implementation. */
  473. (void) re_compile_fastmap (preg);
  474. else
  475. {
  476. /* Some error occurred while compiling the expression. */
  477. re_free (preg->fastmap);
  478. preg->fastmap = NULL;
  479. }
  480. return (int) ret;
  481. }
  482. #ifdef _LIBC
  483. weak_alias (__regcomp, regcomp)
  484. #endif
  485. /* Returns a message corresponding to an error code, ERRCODE, returned
  486. from either regcomp or regexec. We don't use PREG here. */
  487. size_t
  488. regerror (errcode, preg, errbuf, errbuf_size)
  489. int errcode;
  490. const regex_t *preg;
  491. char *errbuf;
  492. size_t errbuf_size;
  493. {
  494. const char *msg;
  495. size_t msg_size;
  496. if (BE (errcode < 0
  497. || errcode >= (int) (sizeof (__re_error_msgid_idx)
  498. / sizeof (__re_error_msgid_idx[0])), 0))
  499. /* Only error codes returned by the rest of the code should be passed
  500. to this routine. If we are given anything else, or if other regex
  501. code generates an invalid error code, then the program has a bug.
  502. Dump core so we can fix it. */
  503. abort ();
  504. msg = gettext (__re_error_msgid + __re_error_msgid_idx[errcode]);
  505. msg_size = strlen (msg) + 1; /* Includes the null. */
  506. if (BE (errbuf_size != 0, 1))
  507. {
  508. if (BE (msg_size > errbuf_size, 0))
  509. {
  510. #if defined HAVE_MEMPCPY || defined _LIBC
  511. *((char *) __mempcpy (errbuf, msg, errbuf_size - 1)) = '\0';
  512. #else
  513. memcpy (errbuf, msg, errbuf_size - 1);
  514. errbuf[errbuf_size - 1] = 0;
  515. #endif
  516. }
  517. else
  518. memcpy (errbuf, msg, msg_size);
  519. }
  520. return msg_size;
  521. }
  522. #ifdef _LIBC
  523. weak_alias (__regerror, regerror)
  524. #endif
  525. #ifdef RE_ENABLE_I18N
  526. /* This static array is used for the map to single-byte characters when
  527. UTF-8 is used. Otherwise we would allocate memory just to initialize
  528. it the same all the time. UTF-8 is the preferred encoding so this is
  529. a worthwhile optimization. */
  530. static const bitset utf8_sb_map =
  531. {
  532. /* Set the first 128 bits. */
  533. # if UINT_MAX == 0xffffffff
  534. 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff
  535. # else
  536. # error "Add case for new unsigned int size"
  537. # endif
  538. };
  539. #endif
  540. static void
  541. free_dfa_content (re_dfa_t *dfa)
  542. {
  543. int i, j;
  544. if (dfa->nodes)
  545. for (i = 0; i < dfa->nodes_len; ++i)
  546. free_token (dfa->nodes + i);
  547. re_free (dfa->nexts);
  548. for (i = 0; i < dfa->nodes_len; ++i)
  549. {
  550. if (dfa->eclosures != NULL)
  551. re_node_set_free (dfa->eclosures + i);
  552. if (dfa->inveclosures != NULL)
  553. re_node_set_free (dfa->inveclosures + i);
  554. if (dfa->edests != NULL)
  555. re_node_set_free (dfa->edests + i);
  556. }
  557. re_free (dfa->edests);
  558. re_free (dfa->eclosures);
  559. re_free (dfa->inveclosures);
  560. re_free (dfa->nodes);
  561. if (dfa->state_table)
  562. for (i = 0; i <= dfa->state_hash_mask; ++i)
  563. {
  564. struct re_state_table_entry *entry = dfa->state_table + i;
  565. for (j = 0; j < entry->num; ++j)
  566. {
  567. re_dfastate_t *state = entry->array[j];
  568. free_state (state);
  569. }
  570. re_free (entry->array);
  571. }
  572. re_free (dfa->state_table);
  573. #ifdef RE_ENABLE_I18N
  574. if (dfa->sb_char != utf8_sb_map)
  575. re_free (dfa->sb_char);
  576. #endif
  577. re_free (dfa->subexp_map);
  578. #ifdef DEBUG
  579. re_free (dfa->re_str);
  580. #endif
  581. re_free (dfa);
  582. }
  583. /* Free dynamically allocated space used by PREG. */
  584. void
  585. regfree (preg)
  586. regex_t *preg;
  587. {
  588. re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
  589. if (BE (dfa != NULL, 1))
  590. free_dfa_content (dfa);
  591. preg->buffer = NULL;
  592. preg->allocated = 0;
  593. re_free (preg->fastmap);
  594. preg->fastmap = NULL;
  595. re_free (preg->translate);
  596. preg->translate = NULL;
  597. }
  598. #ifdef _LIBC
  599. weak_alias (__regfree, regfree)
  600. #endif
  601. /* Entry points compatible with 4.2 BSD regex library. We don't define
  602. them unless specifically requested. */
  603. #if defined _REGEX_RE_COMP || defined _LIBC
  604. /* BSD has one and only one pattern buffer. */
  605. static struct re_pattern_buffer re_comp_buf;
  606. char *
  607. # ifdef _LIBC
  608. /* Make these definitions weak in libc, so POSIX programs can redefine
  609. these names if they don't use our functions, and still use
  610. regcomp/regexec above without link errors. */
  611. weak_function
  612. # endif
  613. re_comp (s)
  614. const char *s;
  615. {
  616. reg_errcode_t ret;
  617. char *fastmap;
  618. if (!s)
  619. {
  620. if (!re_comp_buf.buffer)
  621. return gettext ("No previous regular expression");
  622. return 0;
  623. }
  624. if (re_comp_buf.buffer)
  625. {
  626. fastmap = re_comp_buf.fastmap;
  627. re_comp_buf.fastmap = NULL;
  628. __regfree (&re_comp_buf);
  629. memset (&re_comp_buf, '\0', sizeof (re_comp_buf));
  630. re_comp_buf.fastmap = fastmap;
  631. }
  632. if (re_comp_buf.fastmap == NULL)
  633. {
  634. re_comp_buf.fastmap = (char *) malloc (SBC_MAX);
  635. if (re_comp_buf.fastmap == NULL)
  636. return (char *) gettext (__re_error_msgid
  637. + __re_error_msgid_idx[(int) REG_ESPACE]);
  638. }
  639. /* Since `re_exec' always passes NULL for the `regs' argument, we
  640. don't need to initialize the pattern buffer fields which affect it. */
  641. /* Match anchors at newlines. */
  642. re_comp_buf.newline_anchor = 1;
  643. ret = re_compile_internal (&re_comp_buf, s, strlen (s), re_syntax_options);
  644. if (!ret)
  645. return NULL;
  646. /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
  647. return (char *) gettext (__re_error_msgid + __re_error_msgid_idx[(int) ret]);
  648. }
  649. #ifdef _LIBC
  650. libc_freeres_fn (free_mem)
  651. {
  652. __regfree (&re_comp_buf);
  653. }
  654. #endif
  655. #endif /* _REGEX_RE_COMP */
  656. /* Internal entry point.
  657. Compile the regular expression PATTERN, whose length is LENGTH.
  658. SYNTAX indicate regular expression's syntax. */
  659. static reg_errcode_t
  660. re_compile_internal (preg, pattern, length, syntax)
  661. regex_t *preg;
  662. const char * pattern;
  663. int length;
  664. reg_syntax_t syntax;
  665. {
  666. reg_errcode_t err = REG_NOERROR;
  667. re_dfa_t *dfa;
  668. re_string_t regexp;
  669. /* Initialize the pattern buffer. */
  670. preg->fastmap_accurate = 0;
  671. preg->syntax = syntax;
  672. preg->not_bol = preg->not_eol = 0;
  673. preg->used = 0;
  674. preg->re_nsub = 0;
  675. preg->can_be_null = 0;
  676. preg->regs_allocated = REGS_UNALLOCATED;
  677. /* Initialize the dfa. */
  678. dfa = (re_dfa_t *) preg->buffer;
  679. if (BE (preg->allocated < sizeof (re_dfa_t), 0))
  680. {
  681. /* If zero allocated, but buffer is non-null, try to realloc
  682. enough space. This loses if buffer's address is bogus, but
  683. that is the user's responsibility. If ->buffer is NULL this
  684. is a simple allocation. */
  685. dfa = re_realloc (preg->buffer, re_dfa_t, 1);
  686. if (dfa == NULL)
  687. return REG_ESPACE;
  688. preg->allocated = sizeof (re_dfa_t);
  689. preg->buffer = (unsigned char *) dfa;
  690. }
  691. preg->used = sizeof (re_dfa_t);
  692. err = init_dfa (dfa, length);
  693. if (BE (err != REG_NOERROR, 0))
  694. {
  695. free_dfa_content (dfa);
  696. preg->buffer = NULL;
  697. preg->allocated = 0;
  698. return err;
  699. }
  700. #ifdef DEBUG
  701. dfa->re_str = re_malloc (char, length + 1);
  702. strncpy (dfa->re_str, pattern, length + 1);
  703. #endif
  704. err = re_string_construct (&regexp, pattern, length, preg->translate,
  705. syntax & RE_ICASE, dfa);
  706. if (BE (err != REG_NOERROR, 0))
  707. {
  708. re_compile_internal_free_return:
  709. free_workarea_compile (preg);
  710. re_string_destruct (&regexp);
  711. free_dfa_content (dfa);
  712. preg->buffer = NULL;
  713. preg->allocated = 0;
  714. return err;
  715. }
  716. /* Parse the regular expression, and build a structure tree. */
  717. preg->re_nsub = 0;
  718. dfa->str_tree = parse (&regexp, preg, syntax, &err);
  719. if (BE (dfa->str_tree == NULL, 0))
  720. goto re_compile_internal_free_return;
  721. /* Analyze the tree and create the nfa. */
  722. err = analyze (preg);
  723. if (BE (err != REG_NOERROR, 0))
  724. goto re_compile_internal_free_return;
  725. #ifdef RE_ENABLE_I18N
  726. /* If possible, do searching in single byte encoding to speed things up. */
  727. if (dfa->is_utf8 && !(syntax & RE_ICASE) && preg->translate == NULL)
  728. optimize_utf8 (dfa);
  729. #endif
  730. /* Then create the initial state of the dfa. */
  731. err = create_initial_state (dfa);
  732. /* Release work areas. */
  733. free_workarea_compile (preg);
  734. re_string_destruct (&regexp);
  735. if (BE (err != REG_NOERROR, 0))
  736. {
  737. free_dfa_content (dfa);
  738. preg->buffer = NULL;
  739. preg->allocated = 0;
  740. }
  741. return err;
  742. }
  743. /* Initialize DFA. We use the length of the regular expression PAT_LEN
  744. as the initial length of some arrays. */
  745. static reg_errcode_t
  746. init_dfa (dfa, pat_len)
  747. re_dfa_t *dfa;
  748. int pat_len;
  749. {
  750. int table_size;
  751. #ifndef _LIBC
  752. char *codeset_name;
  753. #endif
  754. memset (dfa, '\0', sizeof (re_dfa_t));
  755. /* Force allocation of str_tree_storage the first time. */
  756. dfa->str_tree_storage_idx = BIN_TREE_STORAGE_SIZE;
  757. dfa->nodes_alloc = pat_len + 1;
  758. dfa->nodes = re_malloc (re_token_t, dfa->nodes_alloc);
  759. dfa->states_alloc = pat_len + 1;
  760. /* table_size = 2 ^ ceil(log pat_len) */
  761. for (table_size = 1; table_size > 0; table_size <<= 1)
  762. if (table_size > pat_len)
  763. break;
  764. dfa->state_table = calloc (sizeof (struct re_state_table_entry), table_size);
  765. dfa->state_hash_mask = table_size - 1;
  766. dfa->mb_cur_max = MB_CUR_MAX;
  767. #ifdef _LIBC
  768. if (dfa->mb_cur_max == 6
  769. && strcmp (_NL_CURRENT (LC_CTYPE, _NL_CTYPE_CODESET_NAME), "UTF-8") == 0)
  770. dfa->is_utf8 = 1;
  771. dfa->map_notascii = (_NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_MAP_TO_NONASCII)
  772. != 0);
  773. #else
  774. # ifdef HAVE_LANGINFO_CODESET
  775. codeset_name = nl_langinfo (CODESET);
  776. # else
  777. codeset_name = getenv ("LC_ALL");
  778. if (codeset_name == NULL || codeset_name[0] == '\0')
  779. codeset_name = getenv ("LC_CTYPE");
  780. if (codeset_name == NULL || codeset_name[0] == '\0')
  781. codeset_name = getenv ("LANG");
  782. if (codeset_name == NULL)
  783. codeset_name = "";
  784. else if (strchr (codeset_name, '.') != NULL)
  785. codeset_name = strchr (codeset_name, '.') + 1;
  786. # endif
  787. if (strcasecmp (codeset_name, "UTF-8") == 0
  788. || strcasecmp (codeset_name, "UTF8") == 0)
  789. dfa->is_utf8 = 1;
  790. /* We check exhaustively in the loop below if this charset is a
  791. superset of ASCII. */
  792. dfa->map_notascii = 0;
  793. #endif
  794. #ifdef RE_ENABLE_I18N
  795. if (dfa->mb_cur_max > 1)
  796. {
  797. if (dfa->is_utf8)
  798. dfa->sb_char = (re_bitset_ptr_t) utf8_sb_map;
  799. else
  800. {
  801. int i, j, ch;
  802. dfa->sb_char = (re_bitset_ptr_t) calloc (sizeof (bitset), 1);
  803. if (BE (dfa->sb_char == NULL, 0))
  804. return REG_ESPACE;
  805. /* Clear all bits by, then set those corresponding to single
  806. byte chars. */
  807. bitset_empty (dfa->sb_char);
  808. for (i = 0, ch = 0; i < BITSET_UINTS; ++i)
  809. for (j = 0; j < UINT_BITS; ++j, ++ch)
  810. {
  811. wchar_t wch = __btowc (ch);
  812. if (wch != WEOF)
  813. dfa->sb_char[i] |= 1 << j;
  814. # ifndef _LIBC
  815. if (isascii (ch) && wch != (wchar_t) ch)
  816. dfa->map_notascii = 1;
  817. # endif
  818. }
  819. }
  820. }
  821. #endif
  822. if (BE (dfa->nodes == NULL || dfa->state_table == NULL, 0))
  823. return REG_ESPACE;
  824. return REG_NOERROR;
  825. }
  826. /* Initialize WORD_CHAR table, which indicate which character is
  827. "word". In this case "word" means that it is the word construction
  828. character used by some operators like "\<", "\>", etc. */
  829. static void
  830. init_word_char (dfa)
  831. re_dfa_t *dfa;
  832. {
  833. int i, j, ch;
  834. dfa->word_ops_used = 1;
  835. for (i = 0, ch = 0; i < BITSET_UINTS; ++i)
  836. for (j = 0; j < UINT_BITS; ++j, ++ch)
  837. if (isalnum (ch) || ch == '_')
  838. dfa->word_char[i] |= 1 << j;
  839. }
  840. /* Free the work area which are only used while compiling. */
  841. static void
  842. free_workarea_compile (preg)
  843. regex_t *preg;
  844. {
  845. re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
  846. bin_tree_storage_t *storage, *next;
  847. for (storage = dfa->str_tree_storage; storage; storage = next)
  848. {
  849. next = storage->next;
  850. re_free (storage);
  851. }
  852. dfa->str_tree_storage = NULL;
  853. dfa->str_tree_storage_idx = BIN_TREE_STORAGE_SIZE;
  854. dfa->str_tree = NULL;
  855. re_free (dfa->org_indices);
  856. dfa->org_indices = NULL;
  857. }
  858. /* Create initial states for all contexts. */
  859. static reg_errcode_t
  860. create_initial_state (dfa)
  861. re_dfa_t *dfa;
  862. {
  863. int first, i;
  864. reg_errcode_t err;
  865. re_node_set init_nodes;
  866. /* Initial states have the epsilon closure of the node which is
  867. the first node of the regular expression. */
  868. first = dfa->str_tree->first->node_idx;
  869. dfa->init_node = first;
  870. err = re_node_set_init_copy (&init_nodes, dfa->eclosures + first);
  871. if (BE (err != REG_NOERROR, 0))
  872. return err;
  873. /* The back-references which are in initial states can epsilon transit,
  874. since in this case all of the subexpressions can be null.
  875. Then we add epsilon closures of the nodes which are the next nodes of
  876. the back-references. */
  877. if (dfa->nbackref > 0)
  878. for (i = 0; i < init_nodes.nelem; ++i)
  879. {
  880. int node_idx = init_nodes.elems[i];
  881. re_token_type_t type = dfa->nodes[node_idx].type;
  882. int clexp_idx;
  883. if (type != OP_BACK_REF)
  884. continue;
  885. for (clexp_idx = 0; clexp_idx < init_nodes.nelem; ++clexp_idx)
  886. {
  887. re_token_t *clexp_node;
  888. clexp_node = dfa->nodes + init_nodes.elems[clexp_idx];
  889. if (clexp_node->type == OP_CLOSE_SUBEXP
  890. && clexp_node->opr.idx == dfa->nodes[node_idx].opr.idx)
  891. break;
  892. }
  893. if (clexp_idx == init_nodes.nelem)
  894. continue;
  895. if (type == OP_BACK_REF)
  896. {
  897. int dest_idx = dfa->edests[node_idx].elems[0];
  898. if (!re_node_set_contains (&init_nodes, dest_idx))
  899. {
  900. re_node_set_merge (&init_nodes, dfa->eclosures + dest_idx);
  901. i = 0;
  902. }
  903. }
  904. }
  905. /* It must be the first time to invoke acquire_state. */
  906. dfa->init_state = re_acquire_state_context (&err, dfa, &init_nodes, 0);
  907. /* We don't check ERR here, since the initial state must not be NULL. */
  908. if (BE (dfa->init_state == NULL, 0))
  909. return err;
  910. if (dfa->init_state->has_constraint)
  911. {
  912. dfa->init_state_word = re_acquire_state_context (&err, dfa, &init_nodes,
  913. CONTEXT_WORD);
  914. dfa->init_state_nl = re_acquire_state_context (&err, dfa, &init_nodes,
  915. CONTEXT_NEWLINE);
  916. dfa->init_state_begbuf = re_acquire_state_context (&err, dfa,
  917. &init_nodes,
  918. CONTEXT_NEWLINE
  919. | CONTEXT_BEGBUF);
  920. if (BE (dfa->init_state_word == NULL || dfa->init_state_nl == NULL
  921. || dfa->init_state_begbuf == NULL, 0))
  922. return err;
  923. }
  924. else
  925. dfa->init_state_word = dfa->init_state_nl
  926. = dfa->init_state_begbuf = dfa->init_state;
  927. re_node_set_free (&init_nodes);
  928. return REG_NOERROR;
  929. }
  930. #ifdef RE_ENABLE_I18N
  931. /* If it is possible to do searching in single byte encoding instead of UTF-8
  932. to speed things up, set dfa->mb_cur_max to 1, clear is_utf8 and change
  933. DFA nodes where needed. */
  934. static void
  935. optimize_utf8 (dfa)
  936. re_dfa_t *dfa;
  937. {
  938. int node, i, mb_chars = 0, has_period = 0;
  939. for (node = 0; node < dfa->nodes_len; ++node)
  940. switch (dfa->nodes[node].type)
  941. {
  942. case CHARACTER:
  943. if (dfa->nodes[node].opr.c >= 0x80)
  944. mb_chars = 1;
  945. break;
  946. case ANCHOR:
  947. switch (dfa->nodes[node].opr.idx)
  948. {
  949. case LINE_FIRST:
  950. case LINE_LAST:
  951. case BUF_FIRST:
  952. case BUF_LAST:
  953. break;
  954. default:
  955. /* Word anchors etc. cannot be handled. */
  956. return;
  957. }
  958. break;
  959. case OP_PERIOD:
  960. has_period = 1;
  961. break;
  962. case OP_BACK_REF:
  963. case OP_ALT:
  964. case END_OF_RE:
  965. case OP_DUP_ASTERISK:
  966. case OP_OPEN_SUBEXP:
  967. case OP_CLOSE_SUBEXP:
  968. break;
  969. case COMPLEX_BRACKET:
  970. return;
  971. case SIMPLE_BRACKET:
  972. /* Just double check. */
  973. for (i = 0x80 / UINT_BITS; i < BITSET_UINTS; ++i)
  974. if (dfa->nodes[node].opr.sbcset[i])
  975. return;
  976. break;
  977. default:
  978. abort ();
  979. }
  980. if (mb_chars || has_period)
  981. for (node = 0; node < dfa->nodes_len; ++node)
  982. {
  983. if (dfa->nodes[node].type == CHARACTER
  984. && dfa->nodes[node].opr.c >= 0x80)
  985. dfa->nodes[node].mb_partial = 0;
  986. else if (dfa->nodes[node].type == OP_PERIOD)
  987. dfa->nodes[node].type = OP_UTF8_PERIOD;
  988. }
  989. /* The search can be in single byte locale. */
  990. dfa->mb_cur_max = 1;
  991. dfa->is_utf8 = 0;
  992. dfa->has_mb_node = dfa->nbackref > 0 || has_period;
  993. }
  994. #endif
  995. /* Analyze the structure tree, and calculate "first", "next", "edest",
  996. "eclosure", and "inveclosure". */
  997. static reg_errcode_t
  998. analyze (preg)
  999. regex_t *preg;
  1000. {
  1001. re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
  1002. reg_errcode_t ret;
  1003. /* Allocate arrays. */
  1004. dfa->nexts = re_malloc (int, dfa->nodes_alloc);
  1005. dfa->org_indices = re_malloc (int, dfa->nodes_alloc);
  1006. dfa->edests = re_malloc (re_node_set, dfa->nodes_alloc);
  1007. dfa->eclosures = re_malloc (re_node_set, dfa->nodes_alloc);
  1008. if (BE (dfa->nexts == NULL || dfa->org_indices == NULL || dfa->edests == NULL
  1009. || dfa->eclosures == NULL, 0))
  1010. return REG_ESPACE;
  1011. dfa->subexp_map = re_malloc (int, preg->re_nsub);
  1012. if (dfa->subexp_map != NULL)
  1013. {
  1014. int i;
  1015. for (i = 0; i < preg->re_nsub; i++)
  1016. dfa->subexp_map[i] = i;
  1017. preorder (dfa->str_tree, optimize_subexps, dfa);
  1018. for (i = 0; i < preg->re_nsub; i++)
  1019. if (dfa->subexp_map[i] != i)
  1020. break;
  1021. if (i == preg->re_nsub)
  1022. {
  1023. free (dfa->subexp_map);
  1024. dfa->subexp_map = NULL;
  1025. }
  1026. }
  1027. ret = postorder (dfa->str_tree, lower_subexps, preg);
  1028. if (BE (ret != REG_NOERROR, 0))
  1029. return ret;
  1030. ret = postorder (dfa->str_tree, calc_first, dfa);
  1031. if (BE (ret != REG_NOERROR, 0))
  1032. return ret;
  1033. preorder (dfa->str_tree, calc_next, dfa);
  1034. ret = preorder (dfa->str_tree, link_nfa_nodes, dfa);
  1035. if (BE (ret != REG_NOERROR, 0))
  1036. return ret;
  1037. ret = calc_eclosure (dfa);
  1038. if (BE (ret != REG_NOERROR, 0))
  1039. return ret;
  1040. /* We only need this during the prune_impossible_nodes pass in regexec.c;
  1041. skip it if p_i_n will not run, as calc_inveclosure can be quadratic. */
  1042. if ((!preg->no_sub && preg->re_nsub > 0 && dfa->has_plural_match)
  1043. || dfa->nbackref)
  1044. {
  1045. dfa->inveclosures = re_malloc (re_node_set, dfa->nodes_len);
  1046. if (BE (dfa->inveclosures == NULL, 0))
  1047. return REG_ESPACE;
  1048. ret = calc_inveclosure (dfa);
  1049. }
  1050. return ret;
  1051. }
  1052. /* Our parse trees are very unbalanced, so we cannot use a stack to
  1053. implement parse tree visits. Instead, we use parent pointers and
  1054. some hairy code in these two functions. */
  1055. static reg_errcode_t
  1056. postorder (root, fn, extra)
  1057. bin_tree_t *root;
  1058. reg_errcode_t (fn (void *, bin_tree_t *));
  1059. void *extra;
  1060. {
  1061. bin_tree_t *node, *prev;
  1062. for (node = root; ; )
  1063. {
  1064. /* Descend down the tree, preferably to the left (or to the right
  1065. if that's the only child). */
  1066. while (node->left || node->right)
  1067. if (node->left)
  1068. node = node->left;
  1069. else
  1070. node = node->right;
  1071. do
  1072. {
  1073. reg_errcode_t err = fn (extra, node);
  1074. if (BE (err != REG_NOERROR, 0))
  1075. return err;
  1076. if (node->parent == NULL)
  1077. return REG_NOERROR;
  1078. prev = node;
  1079. node = node->parent;
  1080. }
  1081. /* Go up while we have a node that is reached from the right. */
  1082. while (node->right == prev || node->right == NULL);
  1083. node = node->right;
  1084. }
  1085. }
  1086. static reg_errcode_t
  1087. preorder (root, fn, extra)
  1088. bin_tree_t *root;
  1089. reg_errcode_t (fn (void *, bin_tree_t *));
  1090. void *extra;
  1091. {
  1092. bin_tree_t *node;
  1093. for (node = root; ; )
  1094. {
  1095. reg_errcode_t err = fn (extra, node);
  1096. if (BE (err != REG_NOERROR, 0))
  1097. return err;
  1098. /* Go to the left node, or up and to the right. */
  1099. if (node->left)
  1100. node = node->left;
  1101. else
  1102. {
  1103. bin_tree_t *prev = NULL;
  1104. while (node->right == prev || node->right == NULL)
  1105. {
  1106. prev = node;
  1107. node = node->parent;
  1108. if (!node)
  1109. return REG_NOERROR;
  1110. }
  1111. node = node->right;
  1112. }
  1113. }
  1114. }
  1115. /* Optimization pass: if a SUBEXP is entirely contained, strip it and tell
  1116. re_search_internal to map the inner one's opr.idx to this one's. Adjust
  1117. backreferences as well. Requires a preorder visit. */
  1118. static reg_errcode_t
  1119. optimize_subexps (extra, node)
  1120. void *extra;
  1121. bin_tree_t *node;
  1122. {
  1123. re_dfa_t *dfa = (re_dfa_t *) extra;
  1124. if (node->token.type == OP_BACK_REF && dfa->subexp_map)
  1125. {
  1126. int idx = node->token.opr.idx;
  1127. node->token.opr.idx = dfa->subexp_map[idx];
  1128. dfa->used_bkref_map |= 1 << node->token.opr.idx;
  1129. }
  1130. else if (node->token.type == SUBEXP
  1131. && node->left && node->left->token.type == SUBEXP)
  1132. {
  1133. int other_idx = node->left->token.opr.idx;
  1134. node->left = node->left->left;
  1135. if (node->left)
  1136. node->left->parent = node;
  1137. dfa->subexp_map[other_idx] = dfa->subexp_map[node->token.opr.idx];
  1138. if (other_idx < 8 * sizeof (dfa->used_bkref_map))
  1139. dfa->used_bkref_map &= ~(1 << other_idx);
  1140. }
  1141. return REG_NOERROR;
  1142. }
  1143. /* Lowering pass: Turn each SUBEXP node into the appropriate concatenation
  1144. of OP_OPEN_SUBEXP, the body of the SUBEXP (if any) and OP_CLOSE_SUBEXP. */
  1145. static reg_errcode_t
  1146. lower_subexps (extra, node)
  1147. void *extra;
  1148. bin_tree_t *node;
  1149. {
  1150. regex_t *preg = (regex_t *) extra;
  1151. reg_errcode_t err = REG_NOERROR;
  1152. if (node->left && node->left->token.type == SUBEXP)
  1153. {
  1154. node->left = lower_subexp (&err, preg, node->left);
  1155. if (node->left)
  1156. node->left->parent = node;
  1157. }
  1158. if (node->right && node->right->token.type == SUBEXP)
  1159. {
  1160. node->right = lower_subexp (&err, preg, node->right);
  1161. if (node->right)
  1162. node->right->parent = node;
  1163. }
  1164. return err;
  1165. }
  1166. static bin_tree_t *
  1167. lower_subexp (err, preg, node)
  1168. reg_errcode_t *err;
  1169. regex_t *preg;
  1170. bin_tree_t *node;
  1171. {
  1172. re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
  1173. bin_tree_t *body = node->left;
  1174. bin_tree_t *op, *cls, *tree1, *tree;
  1175. if (preg->no_sub
  1176. /* We do not optimize empty subexpressions, because otherwise we may
  1177. have bad CONCAT nodes with NULL children. This is obviously not
  1178. very common, so we do not lose much. An example that triggers
  1179. this case is the sed "script" /\(\)/x. */
  1180. && node->left != NULL
  1181. && (node->token.opr.idx >= 8 * sizeof (dfa->used_bkref_map)
  1182. || !(dfa->used_bkref_map & (1 << node->token.opr.idx))))
  1183. return node->left;
  1184. /* Convert the SUBEXP node to the concatenation of an
  1185. OP_OPEN_SUBEXP, the contents, and an OP_CLOSE_SUBEXP. */
  1186. op = create_tree (dfa, NULL, NULL, OP_OPEN_SUBEXP);
  1187. cls = create_tree (dfa, NULL, NULL, OP_CLOSE_SUBEXP);
  1188. tree1 = body ? create_tree (dfa, body, cls, CONCAT) : cls;
  1189. tree = create_tree (dfa, op, tree1, CONCAT);
  1190. if (BE (tree == NULL || tree1 == NULL || op == NULL || cls == NULL, 0))
  1191. {
  1192. *err = REG_ESPACE;
  1193. return NULL;
  1194. }
  1195. op->token.opr.idx = cls->token.opr.idx = node->token.opr.idx;
  1196. op->token.opt_subexp = cls->token.opt_subexp = node->token.opt_subexp;
  1197. return tree;
  1198. }
  1199. /* Pass 1 in building the NFA: compute FIRST and create unlinked automaton
  1200. nodes. Requires a postorder visit. */
  1201. static reg_errcode_t
  1202. calc_first (extra, node)
  1203. void *extra;
  1204. bin_tree_t *node;
  1205. {
  1206. re_dfa_t *dfa = (re_dfa_t *) extra;
  1207. if (node->token.type == CONCAT)
  1208. {
  1209. node->first = node->left->first;
  1210. node->node_idx = node->left->node_idx;
  1211. }
  1212. else
  1213. {
  1214. node->first = node;
  1215. node->node_idx = re_dfa_add_node (dfa, node->token);
  1216. if (BE (node->node_idx == -1, 0))
  1217. return REG_ESPACE;
  1218. }
  1219. return REG_NOERROR;
  1220. }
  1221. /* Pass 2: compute NEXT on the tree. Preorder visit. */
  1222. static reg_errcode_t
  1223. calc_next (extra, node)
  1224. void *extra;
  1225. bin_tree_t *node;
  1226. {
  1227. switch (node->token.type)
  1228. {
  1229. case OP_DUP_ASTERISK:
  1230. node->left->next = node;
  1231. break;
  1232. case CONCAT:
  1233. node->left->next = node->right->first;
  1234. node->right->next = node->next;
  1235. break;
  1236. default:
  1237. if (node->left)
  1238. node->left->next = node->next;
  1239. if (node->right)
  1240. node->right->next = node->next;
  1241. break;
  1242. }
  1243. return REG_NOERROR;
  1244. }
  1245. /* Pass 3: link all DFA nodes to their NEXT node (any order will do). */
  1246. static reg_errcode_t
  1247. link_nfa_nodes (extra, node)
  1248. void *extra;
  1249. bin_tree_t *node;
  1250. {
  1251. re_dfa_t *dfa = (re_dfa_t *) extra;
  1252. int idx = node->node_idx;
  1253. reg_errcode_t err = REG_NOERROR;
  1254. switch (node->token.type)
  1255. {
  1256. case CONCAT:
  1257. break;
  1258. case END_OF_RE:
  1259. assert (node->next == NULL);
  1260. break;
  1261. case OP_DUP_ASTERISK:
  1262. case OP_ALT:
  1263. {
  1264. int left, right;
  1265. dfa->has_plural_match = 1;
  1266. if (node->left != NULL)
  1267. left = node->left->first->node_idx;
  1268. else
  1269. left = node->next->node_idx;
  1270. if (node->right != NULL)
  1271. right = node->right->first->node_idx;
  1272. else
  1273. right = node->next->node_idx;
  1274. assert (left > -1);
  1275. assert (right > -1);
  1276. err = re_node_set_init_2 (dfa->edests + idx, left, right);
  1277. }
  1278. break;
  1279. case ANCHOR:
  1280. case OP_OPEN_SUBEXP:
  1281. case OP_CLOSE_SUBEXP:
  1282. err = re_node_set_init_1 (dfa->edests + idx, node->next->node_idx);
  1283. break;
  1284. case OP_BACK_REF:
  1285. dfa->nexts[idx] = node->next->node_idx;
  1286. if (node->token.type == OP_BACK_REF)
  1287. re_node_set_init_1 (dfa->edests + idx, dfa->nexts[idx]);
  1288. break;
  1289. default:
  1290. assert (!IS_EPSILON_NODE (node->token.type));
  1291. dfa->nexts[idx] = node->next->node_idx;
  1292. break;
  1293. }
  1294. return err;
  1295. }
  1296. /* Duplicate the epsilon closure of the node ROOT_NODE.
  1297. Note that duplicated nodes have constraint INIT_CONSTRAINT in addition
  1298. to their own constraint. */
  1299. static reg_errcode_t
  1300. duplicate_node_closure (dfa, top_org_node, top_clone_node, root_node,
  1301. init_constraint)
  1302. re_dfa_t *dfa;
  1303. int top_org_node, top_clone_node, root_node;
  1304. unsigned int init_constraint;
  1305. {
  1306. reg_errcode_t err;
  1307. int org_node, clone_node, ret;
  1308. unsigned int constraint = init_constraint;
  1309. for (org_node = top_org_node, clone_node = top_clone_node;;)
  1310. {
  1311. int org_dest, clone_dest;
  1312. if (dfa->nodes[org_node].type == OP_BACK_REF)
  1313. {
  1314. /* If the back reference epsilon-transit, its destination must
  1315. also have the constraint. Then duplicate the epsilon closure
  1316. of the destination of the back reference, and store it in
  1317. edests of the back reference. */
  1318. org_dest = dfa->nexts[org_node];
  1319. re_node_set_empty (dfa->edests + clone_node);
  1320. err = duplicate_node (&clone_dest, dfa, org_dest, constraint);
  1321. if (BE (err != REG_NOERROR, 0))
  1322. return err;
  1323. dfa->nexts[clone_node] = dfa->nexts[org_node];
  1324. ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
  1325. if (BE (ret < 0, 0))
  1326. return REG_ESPACE;
  1327. }
  1328. else if (dfa->edests[org_node].nelem == 0)
  1329. {
  1330. /* In case of the node can't epsilon-transit, don't duplicate the
  1331. destination and store the original destination as the
  1332. destination of the node. */
  1333. dfa->nexts[clone_node] = dfa->nexts[org_node];
  1334. break;
  1335. }
  1336. else if (dfa->edests[org_node].nelem == 1)
  1337. {
  1338. /* In case of the node can epsilon-transit, and it has only one
  1339. destination. */
  1340. org_dest = dfa->edests[org_node].elems[0];
  1341. re_node_set_empty (dfa->edests + clone_node);
  1342. if (dfa->nodes[org_node].type == ANCHOR)
  1343. {
  1344. /* In case of the node has another constraint, append it. */
  1345. if (org_node == root_node && clone_node != org_node)
  1346. {
  1347. /* ...but if the node is root_node itself, it means the
  1348. epsilon closure have a loop, then tie it to the
  1349. destination of the root_node. */
  1350. ret = re_node_set_insert (dfa->edests + clone_node,
  1351. org_dest);
  1352. if (BE (ret < 0, 0))
  1353. return REG_ESPACE;
  1354. break;
  1355. }
  1356. constraint |= dfa->nodes[org_node].opr.ctx_type;
  1357. }
  1358. err = duplicate_node (&clone_dest, dfa, org_dest, constraint);
  1359. if (BE (err != REG_NOERROR, 0))
  1360. return err;
  1361. ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
  1362. if (BE (ret < 0, 0))
  1363. return REG_ESPACE;
  1364. }
  1365. else /* dfa->edests[org_node].nelem == 2 */
  1366. {
  1367. /* In case of the node can epsilon-transit, and it has two
  1368. destinations. In the bin_tree_t and DFA, that's '|' and '*'. */
  1369. org_dest = dfa->edests[org_node].elems[0];
  1370. re_node_set_empty (dfa->edests + clone_node);
  1371. /* Search for a duplicated node which satisfies the constraint. */
  1372. clone_dest = search_duplicated_node (dfa, org_dest, constraint);
  1373. if (clone_dest == -1)
  1374. {
  1375. /* There are no such a duplicated node, create a new one. */
  1376. err = duplicate_node (&clone_dest, dfa, org_dest, constraint);
  1377. if (BE (err != REG_NOERROR, 0))
  1378. return err;
  1379. ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
  1380. if (BE (ret < 0, 0))
  1381. return REG_ESPACE;
  1382. err = duplicate_node_closure (dfa, org_dest, clone_dest,
  1383. root_node, constraint);
  1384. if (BE (err != REG_NOERROR, 0))
  1385. return err;
  1386. }
  1387. else
  1388. {
  1389. /* There are a duplicated node which satisfy the constraint,
  1390. use it to avoid infinite loop. */
  1391. ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
  1392. if (BE (ret < 0, 0))
  1393. return REG_ESPACE;
  1394. }
  1395. org_dest = dfa->edests[org_node].elems[1];
  1396. err = duplicate_node (&clone_dest, dfa, org_dest, constraint);
  1397. if (BE (err != REG_NOERROR, 0))
  1398. return err;
  1399. ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
  1400. if (BE (ret < 0, 0))
  1401. return REG_ESPACE;
  1402. }
  1403. org_node = org_dest;
  1404. clone_node = clone_dest;
  1405. }
  1406. return REG_NOERROR;
  1407. }
  1408. /* Search for a node which is duplicated from the node ORG_NODE, and
  1409. satisfies the constraint CONSTRAINT. */
  1410. static int
  1411. search_duplicated_node (dfa, org_node, constraint)
  1412. re_dfa_t *dfa;
  1413. int org_node;
  1414. unsigned int constraint;
  1415. {
  1416. int idx;
  1417. for (idx = dfa->nodes_len - 1; dfa->nodes[idx].duplicated && idx > 0; --idx)
  1418. {
  1419. if (org_node == dfa->org_indices[idx]
  1420. && constraint == dfa->nodes[idx].constraint)
  1421. return idx; /* Found. */
  1422. }
  1423. return -1; /* Not found. */
  1424. }
  1425. /* Duplicate the node whose index is ORG_IDX and set the constraint CONSTRAINT.
  1426. The new index will be stored in NEW_IDX and return REG_NOERROR if succeeded,
  1427. otherwise return the error code. */
  1428. static reg_errcode_t
  1429. duplicate_node (new_idx, dfa, org_idx, constraint)
  1430. re_dfa_t *dfa;
  1431. int *new_idx, org_idx;
  1432. unsigned int constraint;
  1433. {
  1434. int dup_idx = re_dfa_add_node (dfa, dfa->nodes[org_idx]);
  1435. if (BE (dup_idx == -1, 0))
  1436. return REG_ESPACE;
  1437. dfa->nodes[dup_idx].constraint = constraint;
  1438. if (dfa->nodes[org_idx].type == ANCHOR)
  1439. dfa->nodes[dup_idx].constraint |= dfa->nodes[org_idx].opr.ctx_type;
  1440. dfa->nodes[dup_idx].duplicated = 1;
  1441. /* Store the index of the original node. */
  1442. dfa->org_indices[dup_idx] = org_idx;
  1443. *new_idx = dup_idx;
  1444. return REG_NOERROR;
  1445. }
  1446. static reg_errcode_t
  1447. calc_inveclosure (dfa)
  1448. re_dfa_t *dfa;
  1449. {
  1450. int src, idx, ret;
  1451. for (idx = 0; idx < dfa->nodes_len; ++idx)
  1452. re_node_set_init_empty (dfa->inveclosures + idx);
  1453. for (src = 0; src < dfa->nodes_len; ++src)
  1454. {
  1455. int *elems = dfa->eclosures[src].elems;
  1456. for (idx = 0; idx < dfa->eclosures[src].nelem; ++idx)
  1457. {
  1458. ret = re_node_set_insert_last (dfa->inveclosures + elems[idx], src);
  1459. if (BE (ret == -1, 0))
  1460. return REG_ESPACE;
  1461. }
  1462. }
  1463. return REG_NOERROR;
  1464. }
  1465. /* Calculate "eclosure" for all the node in DFA. */
  1466. static reg_errcode_t
  1467. calc_eclosure (dfa)
  1468. re_dfa_t *dfa;
  1469. {
  1470. int node_idx, incomplete;
  1471. #ifdef DEBUG
  1472. assert (dfa->nodes_len > 0);
  1473. #endif
  1474. incomplete = 0;
  1475. /* For each nodes, calculate epsilon closure. */
  1476. for (node_idx = 0; ; ++node_idx)
  1477. {
  1478. reg_errcode_t err;
  1479. re_node_set eclosure_elem;
  1480. if (node_idx == dfa->nodes_len)
  1481. {
  1482. if (!incomplete)
  1483. break;
  1484. incomplete = 0;
  1485. node_idx = 0;
  1486. }
  1487. #ifdef DEBUG
  1488. assert (dfa->eclosures[node_idx].nelem != -1);
  1489. #endif
  1490. /* If we have already calculated, skip it. */
  1491. if (dfa->eclosures[node_idx].nelem != 0)
  1492. continue;
  1493. /* Calculate epsilon closure of `node_idx'. */
  1494. err = calc_eclosure_iter (&eclosure_elem, dfa, node_idx, 1);
  1495. if (BE (err != REG_NOERROR, 0))
  1496. return err;
  1497. if (dfa->eclosures[node_idx].nelem == 0)
  1498. {
  1499. incomplete = 1;
  1500. re_node_set_free (&eclosure_elem);
  1501. }
  1502. }
  1503. return REG_NOERROR;
  1504. }
  1505. /* Calculate epsilon closure of NODE. */
  1506. static reg_errcode_t
  1507. calc_eclosure_iter (new_set, dfa, node, root)
  1508. re_node_set *new_set;
  1509. re_dfa_t *dfa;
  1510. int node, root;
  1511. {
  1512. reg_errcode_t err;
  1513. unsigned int constraint;
  1514. int i, incomplete;
  1515. re_node_set eclosure;
  1516. incomplete = 0;
  1517. err = re_node_set_alloc (&eclosure, dfa->edests[node].nelem + 1);
  1518. if (BE (err != REG_NOERROR, 0))
  1519. return err;
  1520. /* This indicates that we are calculating this node now.
  1521. We reference this value to avoid infinite loop. */
  1522. dfa->eclosures[node].nelem = -1;
  1523. constraint = ((dfa->nodes[node].type == ANCHOR)
  1524. ? dfa->nodes[node].opr.ctx_type : 0);
  1525. /* If the current node has constraints, duplicate all nodes.
  1526. Since they must inherit the constraints. */
  1527. if (constraint
  1528. && dfa->edests[node].nelem
  1529. && !dfa->nodes[dfa->edests[node].elems[0]].duplicated)
  1530. {
  1531. int org_node, cur_node;
  1532. org_node = cur_node = node;
  1533. err = duplicate_node_closure (dfa, node, node, node, constraint);
  1534. if (BE (err != REG_NOERROR, 0))
  1535. return err;
  1536. }
  1537. /* Expand each epsilon destination nodes. */
  1538. if (IS_EPSILON_NODE(dfa->nodes[node].type))
  1539. for (i = 0; i < dfa->edests[node].nelem; ++i)
  1540. {
  1541. re_node_set eclosure_elem;
  1542. int edest = dfa->edests[node].elems[i];
  1543. /* If calculating the epsilon closure of `edest' is in progress,
  1544. return intermediate result. */
  1545. if (dfa->eclosures[edest].nelem == -1)
  1546. {
  1547. incomplete = 1;
  1548. continue;
  1549. }
  1550. /* If we haven't calculated the epsilon closure of `edest' yet,
  1551. calculate now. Otherwise use calculated epsilon closure. */
  1552. if (dfa->eclosures[edest].nelem == 0)
  1553. {
  1554. err = calc_eclosure_iter (&eclosure_elem, dfa, edest, 0);
  1555. if (BE (err != REG_NOERROR, 0))
  1556. return err;
  1557. }
  1558. else
  1559. eclosure_elem = dfa->eclosures[edest];
  1560. /* Merge the epsilon closure of `edest'. */
  1561. re_node_set_merge (&eclosure, &eclosure_elem);
  1562. /* If the epsilon closure of `edest' is incomplete,
  1563. the epsilon closure of this node is also incomplete. */
  1564. if (dfa->eclosures[edest].nelem == 0)
  1565. {
  1566. incomplete = 1;
  1567. re_node_set_free (&eclosure_elem);
  1568. }
  1569. }
  1570. /* Epsilon closures include itself. */
  1571. re_node_set_insert (&eclosure, node);
  1572. if (incomplete && !root)
  1573. dfa->eclosures[node].nelem = 0;
  1574. else
  1575. dfa->eclosures[node] = eclosure;
  1576. *new_set = eclosure;
  1577. return REG_NOERROR;
  1578. }
  1579. /* Functions for token which are used in the parser. */
  1580. /* Fetch a token from INPUT.
  1581. We must not use this function inside bracket expressions. */
  1582. static void
  1583. fetch_token (result, input, syntax)
  1584. re_token_t *result;
  1585. re_string_t *input;
  1586. reg_syntax_t syntax;
  1587. {
  1588. re_string_skip_bytes (input, peek_token (result, input, syntax));
  1589. }
  1590. /* Peek a token from INPUT, and return the length of the token.
  1591. We must not use this function inside bracket expressions. */
  1592. static int
  1593. peek_token (token, input, syntax)
  1594. re_token_t *token;
  1595. re_string_t *input;
  1596. reg_syntax_t syntax;
  1597. {
  1598. unsigned char c;
  1599. if (re_string_eoi (input))
  1600. {
  1601. token->type = END_OF_RE;
  1602. return 0;
  1603. }
  1604. c = re_string_peek_byte (input, 0);
  1605. token->opr.c = c;
  1606. token->word_char = 0;
  1607. #ifdef RE_ENABLE_I18N
  1608. token->mb_partial = 0;
  1609. if (input->mb_cur_max > 1 &&
  1610. !re_string_first_byte (input, re_string_cur_idx (input)))
  1611. {
  1612. token->type = CHARACTER;
  1613. token->mb_partial = 1;
  1614. return 1;
  1615. }
  1616. #endif
  1617. if (c == '\\')
  1618. {
  1619. unsigned char c2;
  1620. if (re_string_cur_idx (input) + 1 >= re_string_length (input))
  1621. {
  1622. token->type = BACK_SLASH;
  1623. return 1;
  1624. }
  1625. c2 = re_string_peek_byte_case (input, 1);
  1626. token->opr.c = c2;
  1627. token->type = CHARACTER;
  1628. #ifdef RE_ENABLE_I18N
  1629. if (input->mb_cur_max > 1)
  1630. {
  1631. wint_t wc = re_string_wchar_at (input,
  1632. re_string_cur_idx (input) + 1);
  1633. token->word_char = IS_WIDE_WORD_CHAR (wc) != 0;
  1634. }
  1635. else
  1636. #endif
  1637. token->word_char = IS_WORD_CHAR (c2) != 0;
  1638. switch (c2)
  1639. {
  1640. case '|':
  1641. if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_NO_BK_VBAR))
  1642. token->type = OP_ALT;
  1643. break;
  1644. case '1': case '2': case '3': case '4': case '5':
  1645. case '6': case '7': case '8': case '9':
  1646. if (!(syntax & RE_NO_BK_REFS))
  1647. {
  1648. token->type = OP_BACK_REF;
  1649. token->opr.idx = c2 - '1';
  1650. }
  1651. break;
  1652. case '<':
  1653. if (!(syntax & RE_NO_GNU_OPS))
  1654. {
  1655. token->type = ANCHOR;
  1656. token->opr.ctx_type = WORD_FIRST;
  1657. }
  1658. break;
  1659. case '>':
  1660. if (!(syntax & RE_NO_GNU_OPS))
  1661. {
  1662. token->type = ANCHOR;
  1663. token->opr.ctx_type = WORD_LAST;
  1664. }
  1665. break;
  1666. case 'b':
  1667. if (!(syntax & RE_NO_GNU_OPS))
  1668. {
  1669. token->type = ANCHOR;
  1670. token->opr.ctx_type = WORD_DELIM;
  1671. }
  1672. break;
  1673. case 'B':
  1674. if (!(syntax & RE_NO_GNU_OPS))
  1675. {
  1676. token->type = ANCHOR;
  1677. token->opr.ctx_type = NOT_WORD_DELIM;
  1678. }
  1679. break;
  1680. case 'w':
  1681. if (!(syntax & RE_NO_GNU_OPS))
  1682. token->type = OP_WORD;
  1683. break;
  1684. case 'W':
  1685. if (!(syntax & RE_NO_GNU_OPS))
  1686. token->type = OP_NOTWORD;
  1687. break;
  1688. case 's':
  1689. if (!(syntax & RE_NO_GNU_OPS))
  1690. token->type = OP_SPACE;
  1691. break;
  1692. case 'S':
  1693. if (!(syntax & RE_NO_GNU_OPS))
  1694. token->type = OP_NOTSPACE;
  1695. break;
  1696. case '`':
  1697. if (!(syntax & RE_NO_GNU_OPS))
  1698. {
  1699. token->type = ANCHOR;
  1700. token->opr.ctx_type = BUF_FIRST;
  1701. }
  1702. break;
  1703. case '\'':
  1704. if (!(syntax & RE_NO_GNU_OPS))
  1705. {
  1706. token->type = ANCHOR;
  1707. token->opr.ctx_type = BUF_LAST;
  1708. }
  1709. break;
  1710. case '(':
  1711. if (!(syntax & RE_NO_BK_PARENS))
  1712. token->type = OP_OPEN_SUBEXP;
  1713. break;
  1714. case ')':
  1715. if (!(syntax & RE_NO_BK_PARENS))
  1716. token->type = OP_CLOSE_SUBEXP;
  1717. break;
  1718. case '+':
  1719. if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_BK_PLUS_QM))
  1720. token->type = OP_DUP_PLUS;
  1721. break;
  1722. case '?':
  1723. if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_BK_PLUS_QM))
  1724. token->type = OP_DUP_QUESTION;
  1725. break;
  1726. case '{':
  1727. if ((syntax & RE_INTERVALS) && (!(syntax & RE_NO_BK_BRACES)))
  1728. token->type = OP_OPEN_DUP_NUM;
  1729. break;
  1730. case '}':
  1731. if ((syntax & RE_INTERVALS) && (!(syntax & RE_NO_BK_BRACES)))
  1732. token->type = OP_CLOSE_DUP_NUM;
  1733. break;
  1734. default:
  1735. break;
  1736. }
  1737. return 2;
  1738. }
  1739. token->type = CHARACTER;
  1740. #ifdef RE_ENABLE_I18N
  1741. if (input->mb_cur_max > 1)
  1742. {
  1743. wint_t wc = re_string_wchar_at (input, re_string_cur_idx (input));
  1744. token->word_char = IS_WIDE_WORD_CHAR (wc) != 0;
  1745. }
  1746. else
  1747. #endif
  1748. token->word_char = IS_WORD_CHAR (token->opr.c);
  1749. switch (c)
  1750. {
  1751. case '\n':
  1752. if (syntax & RE_NEWLINE_ALT)
  1753. token->type = OP_ALT;
  1754. break;
  1755. case '|':
  1756. if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_NO_BK_VBAR))
  1757. token->type = OP_ALT;
  1758. break;
  1759. case '*':
  1760. token->type = OP_DUP_ASTERISK;
  1761. break;
  1762. case '+':
  1763. if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_BK_PLUS_QM))
  1764. token->type = OP_DUP_PLUS;
  1765. break;
  1766. case '?':
  1767. if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_BK_PLUS_QM))
  1768. token->type = OP_DUP_QUESTION;
  1769. break;
  1770. case '{':
  1771. if ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
  1772. token->type = OP_OPEN_DUP_NUM;
  1773. break;
  1774. case '}':
  1775. if ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
  1776. token->type = OP_CLOSE_DUP_NUM;
  1777. break;
  1778. case '(':
  1779. if (syntax & RE_NO_BK_PARENS)
  1780. token->type = OP_OPEN_SUBEXP;
  1781. break;
  1782. case ')':
  1783. if (syntax & RE_NO_BK_PARENS)
  1784. token->type = OP_CLOSE_SUBEXP;
  1785. break;
  1786. case '[':
  1787. token->type = OP_OPEN_BRACKET;
  1788. break;
  1789. case '.':
  1790. token->type = OP_PERIOD;
  1791. break;
  1792. case '^':
  1793. if (!(syntax & (RE_CONTEXT_INDEP_ANCHORS | RE_CARET_ANCHORS_HERE)) &&
  1794. re_string_cur_idx (input) != 0)
  1795. {
  1796. char prev = re_string_peek_byte (input, -1);
  1797. if (!(syntax & RE_NEWLINE_ALT) || prev != '\n')
  1798. break;
  1799. }
  1800. token->type = ANCHOR;
  1801. token->opr.ctx_type = LINE_FIRST;
  1802. break;
  1803. case '$':
  1804. if (!(syntax & RE_CONTEXT_INDEP_ANCHORS) &&
  1805. re_string_cur_idx (input) + 1 != re_string_length (input))
  1806. {
  1807. re_token_t next;
  1808. re_string_skip_bytes (input, 1);
  1809. peek_token (&next, input, syntax);
  1810. re_string_skip_bytes (input, -1);
  1811. if (next.type != OP_ALT && next.type != OP_CLOSE_SUBEXP)
  1812. break;
  1813. }
  1814. token->type = ANCHOR;
  1815. token->opr.ctx_type = LINE_LAST;
  1816. break;
  1817. default:
  1818. break;
  1819. }
  1820. return 1;
  1821. }
  1822. /* Peek a token from INPUT, and return the length of the token.
  1823. We must not use this function out of bracket expressions. */
  1824. static int
  1825. peek_token_bracket (token, input, syntax)
  1826. re_token_t *token;
  1827. re_string_t *input;
  1828. reg_syntax_t syntax;
  1829. {
  1830. unsigned char c;
  1831. if (re_string_eoi (input))
  1832. {
  1833. token->type = END_OF_RE;
  1834. return 0;
  1835. }
  1836. c = re_string_peek_byte (input, 0);
  1837. token->opr.c = c;
  1838. #ifdef RE_ENABLE_I18N
  1839. if (input->mb_cur_max > 1 &&
  1840. !re_string_first_byte (input, re_string_cur_idx (input)))
  1841. {
  1842. token->type = CHARACTER;
  1843. return 1;
  1844. }
  1845. #endif /* RE_ENABLE_I18N */
  1846. if (c == '\\' && (syntax & RE_BACKSLASH_ESCAPE_IN_LISTS)
  1847. && re_string_cur_idx (input) + 1 < re_string_length (input))
  1848. {
  1849. /* In this case, '\' escape a character. */
  1850. unsigned char c2;
  1851. re_string_skip_bytes (input, 1);
  1852. c2 = re_string_peek_byte (input, 0);
  1853. token->opr.c = c2;
  1854. token->type = CHARACTER;
  1855. return 1;
  1856. }
  1857. if (c == '[') /* '[' is a special char in a bracket exps. */
  1858. {
  1859. unsigned char c2;
  1860. int token_len;
  1861. if (re_string_cur_idx (input) + 1 < re_string_length (input))
  1862. c2 = re_string_peek_byte (input, 1);
  1863. else
  1864. c2 = 0;
  1865. token->opr.c = c2;
  1866. token_len = 2;
  1867. switch (c2)
  1868. {
  1869. case '.':
  1870. token->type = OP_OPEN_COLL_ELEM;
  1871. break;
  1872. case '=':
  1873. token->type = OP_OPEN_EQUIV_CLASS;
  1874. break;
  1875. case ':':
  1876. if (syntax & RE_CHAR_CLASSES)
  1877. {
  1878. token->type = OP_OPEN_CHAR_CLASS;
  1879. break;
  1880. }
  1881. /* else fall through. */
  1882. default:
  1883. token->type = CHARACTER;
  1884. token->opr.c = c;
  1885. token_len = 1;
  1886. break;
  1887. }
  1888. return token_len;
  1889. }
  1890. switch (c)
  1891. {
  1892. case '-':
  1893. token->type = OP_CHARSET_RANGE;
  1894. break;
  1895. case ']':
  1896. token->type = OP_CLOSE_BRACKET;
  1897. break;
  1898. case '^':
  1899. token->type = OP_NON_MATCH_LIST;
  1900. break;
  1901. default:
  1902. token->type = CHARACTER;
  1903. }
  1904. return 1;
  1905. }
  1906. /* Functions for parser. */
  1907. /* Entry point of the parser.
  1908. Parse the regular expression REGEXP and return the structure tree.
  1909. If an error is occured, ERR is set by error code, and return NULL.
  1910. This function build the following tree, from regular expression <reg_exp>:
  1911. CAT
  1912. / \
  1913. / \
  1914. <reg_exp> EOR
  1915. CAT means concatenation.
  1916. EOR means end of regular expression. */
  1917. static bin_tree_t *
  1918. parse (regexp, preg, syntax, err)
  1919. re_string_t *regexp;
  1920. regex_t *preg;
  1921. reg_syntax_t syntax;
  1922. reg_errcode_t *err;
  1923. {
  1924. re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
  1925. bin_tree_t *tree, *eor, *root;
  1926. re_token_t current_token;
  1927. dfa->syntax = syntax;
  1928. fetch_token (&current_token, regexp, syntax | RE_CARET_ANCHORS_HERE);
  1929. tree = parse_reg_exp (regexp, preg, &current_token, syntax, 0, err);
  1930. if (BE (*err != REG_NOERROR && tree == NULL, 0))
  1931. return NULL;
  1932. eor = create_tree (dfa, NULL, NULL, END_OF_RE);
  1933. if (tree != NULL)
  1934. root = create_tree (dfa, tree, eor, CONCAT);
  1935. else
  1936. root = eor;
  1937. if (BE (eor == NULL || root == NULL, 0))
  1938. {
  1939. *err = REG_ESPACE;
  1940. return NULL;
  1941. }
  1942. return root;
  1943. }
  1944. /* This function build the following tree, from regular expression
  1945. <branch1>|<branch2>:
  1946. ALT
  1947. / \
  1948. / \
  1949. <branch1> <branch2>
  1950. ALT means alternative, which represents the operator `|'. */
  1951. static bin_tree_t *
  1952. parse_reg_exp (regexp, preg, token, syntax, nest, err)
  1953. re_string_t *regexp;
  1954. regex_t *preg;
  1955. re_token_t *token;
  1956. reg_syntax_t syntax;
  1957. int nest;
  1958. reg_errcode_t *err;
  1959. {
  1960. re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
  1961. bin_tree_t *tree, *branch = NULL;
  1962. tree = parse_branch (regexp, preg, token, syntax, nest, err);
  1963. if (BE (*err != REG_NOERROR && tree == NULL, 0))
  1964. return NULL;
  1965. while (token->type == OP_ALT)
  1966. {
  1967. fetch_token (token, regexp, syntax | RE_CARET_ANCHORS_HERE);
  1968. if (token->type != OP_ALT && token->type != END_OF_RE
  1969. && (nest == 0 || token->type != OP_CLOSE_SUBEXP))
  1970. {
  1971. branch = parse_branch (regexp, preg, token, syntax, nest, err);
  1972. if (BE (*err != REG_NOERROR && branch == NULL, 0))
  1973. return NULL;
  1974. }
  1975. else
  1976. branch = NULL;
  1977. tree = create_tree (dfa, tree, branch, OP_ALT);
  1978. if (BE (tree == NULL, 0))
  1979. {
  1980. *err = REG_ESPACE;
  1981. return NULL;
  1982. }
  1983. }
  1984. return tree;
  1985. }
  1986. /* This function build the following tree, from regular expression
  1987. <exp1><exp2>:
  1988. CAT
  1989. / \
  1990. / \
  1991. <exp1> <exp2>
  1992. CAT means concatenation. */
  1993. static bin_tree_t *
  1994. parse_branch (regexp, preg, token, syntax, nest, err)
  1995. re_string_t *regexp;
  1996. regex_t *preg;
  1997. re_token_t *token;
  1998. reg_syntax_t syntax;
  1999. int nest;
  2000. reg_errcode_t *err;
  2001. {
  2002. bin_tree_t *tree, *exp;
  2003. re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
  2004. tree = parse_expression (regexp, preg, token, syntax, nest, err);
  2005. if (BE (*err != REG_NOERROR && tree == NULL, 0))
  2006. return NULL;
  2007. while (token->type != OP_ALT && token->type != END_OF_RE
  2008. && (nest == 0 || token->type != OP_CLOSE_SUBEXP))
  2009. {
  2010. exp = parse_expression (regexp, preg, token, syntax, nest, err);
  2011. if (BE (*err != REG_NOERROR && exp == NULL, 0))
  2012. {
  2013. return NULL;
  2014. }
  2015. if (tree != NULL && exp != NULL)
  2016. {
  2017. tree = create_tree (dfa, tree, exp, CONCAT);
  2018. if (tree == NULL)
  2019. {
  2020. *err = REG_ESPACE;
  2021. return NULL;
  2022. }
  2023. }
  2024. else if (tree == NULL)
  2025. tree = exp;
  2026. /* Otherwise exp == NULL, we don't need to create new tree. */
  2027. }
  2028. return tree;
  2029. }
  2030. /* This function build the following tree, from regular expression a*:
  2031. *
  2032. |
  2033. a
  2034. */
  2035. static bin_tree_t *
  2036. parse_expression (regexp, preg, token, syntax, nest, err)
  2037. re_string_t *regexp;
  2038. regex_t *preg;
  2039. re_token_t *token;
  2040. reg_syntax_t syntax;
  2041. int nest;
  2042. reg_errcode_t *err;
  2043. {
  2044. re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
  2045. bin_tree_t *tree;
  2046. switch (token->type)
  2047. {
  2048. case CHARACTER:
  2049. tree = create_token_tree (dfa, NULL, NULL, token);
  2050. if (BE (tree == NULL, 0))
  2051. {
  2052. *err = REG_ESPACE;
  2053. return NULL;
  2054. }
  2055. #ifdef RE_ENABLE_I18N
  2056. if (dfa->mb_cur_max > 1)
  2057. {
  2058. while (!re_string_eoi (regexp)
  2059. && !re_string_first_byte (regexp, re_string_cur_idx (regexp)))
  2060. {
  2061. bin_tree_t *mbc_remain;
  2062. fetch_token (token, regexp, syntax);
  2063. mbc_remain = create_token_tree (dfa, NULL, NULL, token);
  2064. tree = create_tree (dfa, tree, mbc_remain, CONCAT);
  2065. if (BE (mbc_remain == NULL || tree == NULL, 0))
  2066. {
  2067. *err = REG_ESPACE;
  2068. return NULL;
  2069. }
  2070. }
  2071. }
  2072. #endif
  2073. break;
  2074. case OP_OPEN_SUBEXP:
  2075. tree = parse_sub_exp (regexp, preg, token, syntax, nest + 1, err);
  2076. if (BE (*err != REG_NOERROR && tree == NULL, 0))
  2077. return NULL;
  2078. break;
  2079. case OP_OPEN_BRACKET:
  2080. tree = parse_bracket_exp (regexp, dfa, token, syntax, err);
  2081. if (BE (*err != REG_NOERROR && tree == NULL, 0))
  2082. return NULL;
  2083. break;
  2084. case OP_BACK_REF:
  2085. if (!BE (dfa->completed_bkref_map & (1 << token->opr.idx), 1))
  2086. {
  2087. *err = REG_ESUBREG;
  2088. return NULL;
  2089. }
  2090. dfa->used_bkref_map |= 1 << token->opr.idx;
  2091. tree = create_token_tree (dfa, NULL, NULL, token);
  2092. if (BE (tree == NULL, 0))
  2093. {
  2094. *err = REG_ESPACE;
  2095. return NULL;
  2096. }
  2097. ++dfa->nbackref;
  2098. dfa->has_mb_node = 1;
  2099. break;
  2100. case OP_OPEN_DUP_NUM:
  2101. if (syntax & RE_CONTEXT_INVALID_DUP)
  2102. {
  2103. *err = REG_BADRPT;
  2104. return NULL;
  2105. }
  2106. /* FALLTHROUGH */
  2107. case OP_DUP_ASTERISK:
  2108. case OP_DUP_PLUS:
  2109. case OP_DUP_QUESTION:
  2110. if (syntax & RE_CONTEXT_INVALID_OPS)
  2111. {
  2112. *err = REG_BADRPT;
  2113. return NULL;
  2114. }
  2115. else if (syntax & RE_CONTEXT_INDEP_OPS)
  2116. {
  2117. fetch_token (token, regexp, syntax);
  2118. return parse_expression (regexp, preg, token, syntax, nest, err);
  2119. }
  2120. /* else fall through */
  2121. case OP_CLOSE_SUBEXP:
  2122. if ((token->type == OP_CLOSE_SUBEXP) &&
  2123. !(syntax & RE_UNMATCHED_RIGHT_PAREN_ORD))
  2124. {
  2125. *err = REG_ERPAREN;
  2126. return NULL;
  2127. }
  2128. /* else fall through */
  2129. case OP_CLOSE_DUP_NUM:
  2130. /* We treat it as a normal character. */
  2131. /* Then we can these characters as normal characters. */
  2132. token->type = CHARACTER;
  2133. /* mb_partial and word_char bits should be initialized already
  2134. by peek_token. */
  2135. tree = create_token_tree (dfa, NULL, NULL, token);
  2136. if (BE (tree == NULL, 0))
  2137. {
  2138. *err = REG_ESPACE;
  2139. return NULL;
  2140. }
  2141. break;
  2142. case ANCHOR:
  2143. if ((token->opr.ctx_type
  2144. & (WORD_DELIM | NOT_WORD_DELIM | WORD_FIRST | WORD_LAST))
  2145. && dfa->word_ops_used == 0)
  2146. init_word_char (dfa);
  2147. if (token->opr.ctx_type == WORD_DELIM
  2148. || token->opr.ctx_type == NOT_WORD_DELIM)
  2149. {
  2150. bin_tree_t *tree_first, *tree_last;
  2151. if (token->opr.ctx_type == WORD_DELIM)
  2152. {
  2153. token->opr.ctx_type = WORD_FIRST;
  2154. tree_first = create_token_tree (dfa, NULL, NULL, token);
  2155. token->opr.ctx_type = WORD_LAST;
  2156. }
  2157. else
  2158. {
  2159. token->opr.ctx_type = INSIDE_WORD;
  2160. tree_first = create_token_tree (dfa, NULL, NULL, token);
  2161. token->opr.ctx_type = INSIDE_NOTWORD;
  2162. }
  2163. tree_last = create_token_tree (dfa, NULL, NULL, token);
  2164. tree = create_tree (dfa, tree_first, tree_last, OP_ALT);
  2165. if (BE (tree_first == NULL || tree_last == NULL || tree == NULL, 0))
  2166. {
  2167. *err = REG_ESPACE;
  2168. return NULL;
  2169. }
  2170. }
  2171. else
  2172. {
  2173. tree = create_token_tree (dfa, NULL, NULL, token);
  2174. if (BE (tree == NULL, 0))
  2175. {
  2176. *err = REG_ESPACE;
  2177. return NULL;
  2178. }
  2179. }
  2180. /* We must return here, since ANCHORs can't be followed
  2181. by repetition operators.
  2182. eg. RE"^*" is invalid or "<ANCHOR(^)><CHAR(*)>",
  2183. it must not be "<ANCHOR(^)><REPEAT(*)>". */
  2184. fetch_token (token, regexp, syntax);
  2185. return tree;
  2186. case OP_PERIOD:
  2187. tree = create_token_tree (dfa, NULL, NULL, token);
  2188. if (BE (tree == NULL, 0))
  2189. {
  2190. *err = REG_ESPACE;
  2191. return NULL;
  2192. }
  2193. if (dfa->mb_cur_max > 1)
  2194. dfa->has_mb_node = 1;
  2195. break;
  2196. case OP_WORD:
  2197. case OP_NOTWORD:
  2198. tree = build_charclass_op (dfa, regexp->trans,
  2199. (const unsigned char *) "alnum",
  2200. (const unsigned char *) "_",
  2201. token->type == OP_NOTWORD, err);
  2202. if (BE (*err != REG_NOERROR && tree == NULL, 0))
  2203. return NULL;
  2204. break;
  2205. case OP_SPACE:
  2206. case OP_NOTSPACE:
  2207. tree = build_charclass_op (dfa, regexp->trans,
  2208. (const unsigned char *) "space",
  2209. (const unsigned char *) "",
  2210. token->type == OP_NOTSPACE, err);
  2211. if (BE (*err != REG_NOERROR && tree == NULL, 0))
  2212. return NULL;
  2213. break;
  2214. case OP_ALT:
  2215. case END_OF_RE:
  2216. return NULL;
  2217. case BACK_SLASH:
  2218. *err = REG_EESCAPE;
  2219. return NULL;
  2220. default:
  2221. /* Must not happen? */
  2222. #ifdef DEBUG
  2223. assert (0);
  2224. #endif
  2225. return NULL;
  2226. }
  2227. fetch_token (token, regexp, syntax);
  2228. while (token->type == OP_DUP_ASTERISK || token->type == OP_DUP_PLUS
  2229. || token->type == OP_DUP_QUESTION || token->type == OP_OPEN_DUP_NUM)
  2230. {
  2231. tree = parse_dup_op (tree, regexp, dfa, token, syntax, err);
  2232. if (BE (*err != REG_NOERROR && tree == NULL, 0))
  2233. return NULL;
  2234. /* In BRE consecutive duplications are not allowed. */
  2235. if ((syntax & RE_CONTEXT_INVALID_DUP)
  2236. && (token->type == OP_DUP_ASTERISK
  2237. || token->type == OP_OPEN_DUP_NUM))
  2238. {
  2239. *err = REG_BADRPT;
  2240. return NULL;
  2241. }
  2242. }
  2243. return tree;
  2244. }
  2245. /* This function build the following tree, from regular expression
  2246. (<reg_exp>):
  2247. SUBEXP
  2248. |
  2249. <reg_exp>
  2250. */
  2251. static bin_tree_t *
  2252. parse_sub_exp (regexp, preg, token, syntax, nest, err)
  2253. re_string_t *regexp;
  2254. regex_t *preg;
  2255. re_token_t *token;
  2256. reg_syntax_t syntax;
  2257. int nest;
  2258. reg_errcode_t *err;
  2259. {
  2260. re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
  2261. bin_tree_t *tree;
  2262. size_t cur_nsub;
  2263. cur_nsub = preg->re_nsub++;
  2264. fetch_token (token, regexp, syntax | RE_CARET_ANCHORS_HERE);
  2265. /* The subexpression may be a null string. */
  2266. if (token->type == OP_CLOSE_SUBEXP)
  2267. tree = NULL;
  2268. else
  2269. {
  2270. tree = parse_reg_exp (regexp, preg, token, syntax, nest, err);
  2271. if (BE (*err == REG_NOERROR && token->type != OP_CLOSE_SUBEXP, 0))
  2272. *err = REG_EPAREN;
  2273. if (BE (*err != REG_NOERROR, 0))
  2274. return NULL;
  2275. }
  2276. dfa->completed_bkref_map |= 1 << cur_nsub;
  2277. tree = create_tree (dfa, tree, NULL, SUBEXP);
  2278. if (BE (tree == NULL, 0))
  2279. {
  2280. *err = REG_ESPACE;
  2281. return NULL;
  2282. }
  2283. tree->token.opr.idx = cur_nsub;
  2284. return tree;
  2285. }
  2286. /* This function parse repetition operators like "*", "+", "{1,3}" etc. */
  2287. static bin_tree_t *
  2288. parse_dup_op (elem, regexp, dfa, token, syntax, err)
  2289. bin_tree_t *elem;
  2290. re_string_t *regexp;
  2291. re_dfa_t *dfa;
  2292. re_token_t *token;
  2293. reg_syntax_t syntax;
  2294. reg_errcode_t *err;
  2295. {
  2296. bin_tree_t *tree = NULL, *old_tree = NULL;
  2297. int i, start, end, start_idx = re_string_cur_idx (regexp);
  2298. re_token_t start_token = *token;
  2299. if (token->type == OP_OPEN_DUP_NUM)
  2300. {
  2301. end = 0;
  2302. start = fetch_number (regexp, token, syntax);
  2303. if (start == -1)
  2304. {
  2305. if (token->type == CHARACTER && token->opr.c == ',')
  2306. start = 0; /* We treat "{,m}" as "{0,m}". */
  2307. else
  2308. {
  2309. *err = REG_BADBR; /* <re>{} is invalid. */
  2310. return NULL;
  2311. }
  2312. }
  2313. if (BE (start != -2, 1))
  2314. {
  2315. /* We treat "{n}" as "{n,n}". */
  2316. end = ((token->type == OP_CLOSE_DUP_NUM) ? start
  2317. : ((token->type == CHARACTER && token->opr.c == ',')
  2318. ? fetch_number (regexp, token, syntax) : -2));
  2319. }
  2320. if (BE (start == -2 || end == -2, 0))
  2321. {
  2322. /* Invalid sequence. */
  2323. if (BE (!(syntax & RE_INVALID_INTERVAL_ORD), 0))
  2324. {
  2325. if (token->type == END_OF_RE)
  2326. *err = REG_EBRACE;
  2327. else
  2328. *err = REG_BADBR;
  2329. return NULL;
  2330. }
  2331. /* If the syntax bit is set, rollback. */
  2332. re_string_set_index (regexp, start_idx);
  2333. *token = start_token;
  2334. token->type = CHARACTER;
  2335. /* mb_partial and word_char bits should be already initialized by
  2336. peek_token. */
  2337. return elem;
  2338. }
  2339. if (BE (end != -1 && start > end, 0))
  2340. {
  2341. /* First number greater than second. */
  2342. *err = REG_BADBR;
  2343. return NULL;
  2344. }
  2345. }
  2346. else
  2347. {
  2348. start = (token->type == OP_DUP_PLUS) ? 1 : 0;
  2349. end = (token->type == OP_DUP_QUESTION) ? 1 : -1;
  2350. }
  2351. fetch_token (token, regexp, syntax);
  2352. if (BE (elem == NULL, 0))
  2353. return NULL;
  2354. if (BE (start == 0 && end == 0, 0))
  2355. {
  2356. postorder (elem, free_tree, NULL);
  2357. return NULL;
  2358. }
  2359. /* Extract "<re>{n,m}" to "<re><re>...<re><re>{0,<m-n>}". */
  2360. if (BE (start > 0, 0))
  2361. {
  2362. tree = elem;
  2363. for (i = 2; i <= start; ++i)
  2364. {
  2365. elem = duplicate_tree (elem, dfa);
  2366. tree = create_tree (dfa, tree, elem, CONCAT);
  2367. if (BE (elem == NULL || tree == NULL, 0))
  2368. goto parse_dup_op_espace;
  2369. }
  2370. if (start == end)
  2371. return tree;
  2372. /* Duplicate ELEM before it is marked optional. */
  2373. elem = duplicate_tree (elem, dfa);
  2374. old_tree = tree;
  2375. }
  2376. else
  2377. old_tree = NULL;
  2378. if (elem->token.type == SUBEXP)
  2379. postorder (elem, mark_opt_subexp, (void *) (long) elem->token.opr.idx);
  2380. tree = create_tree (dfa, elem, NULL, (end == -1 ? OP_DUP_ASTERISK : OP_ALT));
  2381. if (BE (tree == NULL, 0))
  2382. goto parse_dup_op_espace;
  2383. /* This loop is actually executed only when end != -1,
  2384. to rewrite <re>{0,n} as (<re>(<re>...<re>?)?)?... We have
  2385. already created the start+1-th copy. */
  2386. for (i = start + 2; i <= end; ++i)
  2387. {
  2388. elem = duplicate_tree (elem, dfa);
  2389. tree = create_tree (dfa, tree, elem, CONCAT);
  2390. if (BE (elem == NULL || tree == NULL, 0))
  2391. goto parse_dup_op_espace;
  2392. tree = create_tree (dfa, tree, NULL, OP_ALT);
  2393. if (BE (tree == NULL, 0))
  2394. goto parse_dup_op_espace;
  2395. }
  2396. if (old_tree)
  2397. tree = create_tree (dfa, old_tree, tree, CONCAT);
  2398. return tree;
  2399. parse_dup_op_espace:
  2400. *err = REG_ESPACE;
  2401. return NULL;
  2402. }
  2403. /* Size of the names for collating symbol/equivalence_class/character_class.
  2404. I'm not sure, but maybe enough. */
  2405. #define BRACKET_NAME_BUF_SIZE 32
  2406. #ifndef _LIBC
  2407. /* Local function for parse_bracket_exp only used in case of NOT _LIBC.
  2408. Build the range expression which starts from START_ELEM, and ends
  2409. at END_ELEM. The result are written to MBCSET and SBCSET.
  2410. RANGE_ALLOC is the allocated size of mbcset->range_starts, and
  2411. mbcset->range_ends, is a pointer argument sinse we may
  2412. update it. */
  2413. static reg_errcode_t
  2414. # ifdef RE_ENABLE_I18N
  2415. build_range_exp (sbcset, mbcset, range_alloc, start_elem, end_elem)
  2416. re_charset_t *mbcset;
  2417. int *range_alloc;
  2418. # else /* not RE_ENABLE_I18N */
  2419. build_range_exp (sbcset, start_elem, end_elem)
  2420. # endif /* not RE_ENABLE_I18N */
  2421. re_bitset_ptr_t sbcset;
  2422. bracket_elem_t *start_elem, *end_elem;
  2423. {
  2424. unsigned int start_ch, end_ch;
  2425. /* Equivalence Classes and Character Classes can't be a range start/end. */
  2426. if (BE (start_elem->type == EQUIV_CLASS || start_elem->type == CHAR_CLASS
  2427. || end_elem->type == EQUIV_CLASS || end_elem->type == CHAR_CLASS,
  2428. 0))
  2429. return REG_ERANGE;
  2430. /* We can handle no multi character collating elements without libc
  2431. support. */
  2432. if (BE ((start_elem->type == COLL_SYM
  2433. && strlen ((char *) start_elem->opr.name) > 1)
  2434. || (end_elem->type == COLL_SYM
  2435. && strlen ((char *) end_elem->opr.name) > 1), 0))
  2436. return REG_ECOLLATE;
  2437. # ifdef RE_ENABLE_I18N
  2438. {
  2439. wchar_t wc, start_wc, end_wc;
  2440. wchar_t cmp_buf[6] = {L'\0', L'\0', L'\0', L'\0', L'\0', L'\0'};
  2441. start_ch = ((start_elem->type == SB_CHAR) ? start_elem->opr.ch
  2442. : ((start_elem->type == COLL_SYM) ? start_elem->opr.name[0]
  2443. : 0));
  2444. end_ch = ((end_elem->type == SB_CHAR) ? end_elem->opr.ch
  2445. : ((end_elem->type == COLL_SYM) ? end_elem->opr.name[0]
  2446. : 0));
  2447. start_wc = ((start_elem->type == SB_CHAR || start_elem->type == COLL_SYM)
  2448. ? __btowc (start_ch) : start_elem->opr.wch);
  2449. end_wc = ((end_elem->type == SB_CHAR || end_elem->type == COLL_SYM)
  2450. ? __btowc (end_ch) : end_elem->opr.wch);
  2451. if (start_wc == WEOF || end_wc == WEOF)
  2452. return REG_ECOLLATE;
  2453. cmp_buf[0] = start_wc;
  2454. cmp_buf[4] = end_wc;
  2455. if (wcscoll (cmp_buf, cmp_buf + 4) > 0)
  2456. return REG_ERANGE;
  2457. /* Got valid collation sequence values, add them as a new entry.
  2458. However, for !_LIBC we have no collation elements: if the
  2459. character set is single byte, the single byte character set
  2460. that we build below suffices. parse_bracket_exp passes
  2461. no MBCSET if dfa->mb_cur_max == 1. */
  2462. if (mbcset)
  2463. {
  2464. /* Check the space of the arrays. */
  2465. if (BE (*range_alloc == mbcset->nranges, 0))
  2466. {
  2467. /* There is not enough space, need realloc. */
  2468. wchar_t *new_array_start, *new_array_end;
  2469. int new_nranges;
  2470. /* +1 in case of mbcset->nranges is 0. */
  2471. new_nranges = 2 * mbcset->nranges + 1;
  2472. /* Use realloc since mbcset->range_starts and mbcset->range_ends
  2473. are NULL if *range_alloc == 0. */
  2474. new_array_start = re_realloc (mbcset->range_starts, wchar_t,
  2475. new_nranges);
  2476. new_array_end = re_realloc (mbcset->range_ends, wchar_t,
  2477. new_nranges);
  2478. if (BE (new_array_start == NULL || new_array_end == NULL, 0))
  2479. return REG_ESPACE;
  2480. mbcset->range_starts = new_array_start;
  2481. mbcset->range_ends = new_array_end;
  2482. *range_alloc = new_nranges;
  2483. }
  2484. mbcset->range_starts[mbcset->nranges] = start_wc;
  2485. mbcset->range_ends[mbcset->nranges++] = end_wc;
  2486. }
  2487. /* Build the table for single byte characters. */
  2488. for (wc = 0; wc < SBC_MAX; ++wc)
  2489. {
  2490. cmp_buf[2] = wc;
  2491. if (wcscoll (cmp_buf, cmp_buf + 2) <= 0
  2492. && wcscoll (cmp_buf + 2, cmp_buf + 4) <= 0)
  2493. bitset_set (sbcset, wc);
  2494. }
  2495. }
  2496. # else /* not RE_ENABLE_I18N */
  2497. {
  2498. unsigned int ch;
  2499. start_ch = ((start_elem->type == SB_CHAR ) ? start_elem->opr.ch
  2500. : ((start_elem->type == COLL_SYM) ? start_elem->opr.name[0]
  2501. : 0));
  2502. end_ch = ((end_elem->type == SB_CHAR ) ? end_elem->opr.ch
  2503. : ((end_elem->type == COLL_SYM) ? end_elem->opr.name[0]
  2504. : 0));
  2505. if (start_ch > end_ch)
  2506. return REG_ERANGE;
  2507. /* Build the table for single byte characters. */
  2508. for (ch = 0; ch < SBC_MAX; ++ch)
  2509. if (start_ch <= ch && ch <= end_ch)
  2510. bitset_set (sbcset, ch);
  2511. }
  2512. # endif /* not RE_ENABLE_I18N */
  2513. return REG_NOERROR;
  2514. }
  2515. #endif /* not _LIBC */
  2516. #ifndef _LIBC
  2517. /* Helper function for parse_bracket_exp only used in case of NOT _LIBC..
  2518. Build the collating element which is represented by NAME.
  2519. The result are written to MBCSET and SBCSET.
  2520. COLL_SYM_ALLOC is the allocated size of mbcset->coll_sym, is a
  2521. pointer argument since we may update it. */
  2522. static reg_errcode_t
  2523. # ifdef RE_ENABLE_I18N
  2524. build_collating_symbol (sbcset, mbcset, coll_sym_alloc, name)
  2525. re_charset_t *mbcset;
  2526. int *coll_sym_alloc;
  2527. # else /* not RE_ENABLE_I18N */
  2528. build_collating_symbol (sbcset, name)
  2529. # endif /* not RE_ENABLE_I18N */
  2530. re_bitset_ptr_t sbcset;
  2531. const unsigned char *name;
  2532. {
  2533. size_t name_len = strlen ((const char *) name);
  2534. if (BE (name_len != 1, 0))
  2535. return REG_ECOLLATE;
  2536. else
  2537. {
  2538. bitset_set (sbcset, name[0]);
  2539. return REG_NOERROR;
  2540. }
  2541. }
  2542. #endif /* not _LIBC */
  2543. /* This function parse bracket expression like "[abc]", "[a-c]",
  2544. "[[.a-a.]]" etc. */
  2545. static bin_tree_t *
  2546. parse_bracket_exp (regexp, dfa, token, syntax, err)
  2547. re_string_t *regexp;
  2548. re_dfa_t *dfa;
  2549. re_token_t *token;
  2550. reg_syntax_t syntax;
  2551. reg_errcode_t *err;
  2552. {
  2553. #ifdef _LIBC
  2554. const unsigned char *collseqmb;
  2555. const char *collseqwc;
  2556. uint32_t nrules;
  2557. int32_t table_size;
  2558. const int32_t *symb_table;
  2559. const unsigned char *extra;
  2560. /* Local function for parse_bracket_exp used in _LIBC environement.
  2561. Seek the collating symbol entry correspondings to NAME.
  2562. Return the index of the symbol in the SYMB_TABLE. */
  2563. auto inline int32_t
  2564. __attribute ((always_inline))
  2565. seek_collating_symbol_entry (name, name_len)
  2566. const unsigned char *name;
  2567. size_t name_len;
  2568. {
  2569. int32_t hash = elem_hash ((const char *) name, name_len);
  2570. int32_t elem = hash % table_size;
  2571. int32_t second = hash % (table_size - 2);
  2572. while (symb_table[2 * elem] != 0)
  2573. {
  2574. /* First compare the hashing value. */
  2575. if (symb_table[2 * elem] == hash
  2576. /* Compare the length of the name. */
  2577. && name_len == extra[symb_table[2 * elem + 1]]
  2578. /* Compare the name. */
  2579. && memcmp (name, &extra[symb_table[2 * elem + 1] + 1],
  2580. name_len) == 0)
  2581. {
  2582. /* Yep, this is the entry. */
  2583. break;
  2584. }
  2585. /* Next entry. */
  2586. elem += second;
  2587. }
  2588. return elem;
  2589. }
  2590. /* Local function for parse_bracket_exp used in _LIBC environement.
  2591. Look up the collation sequence value of BR_ELEM.
  2592. Return the value if succeeded, UINT_MAX otherwise. */
  2593. auto inline unsigned int
  2594. __attribute ((always_inline))
  2595. lookup_collation_sequence_value (br_elem)
  2596. bracket_elem_t *br_elem;
  2597. {
  2598. if (br_elem->type == SB_CHAR)
  2599. {
  2600. /*
  2601. if (MB_CUR_MAX == 1)
  2602. */
  2603. if (nrules == 0)
  2604. return collseqmb[br_elem->opr.ch];
  2605. else
  2606. {
  2607. wint_t wc = __btowc (br_elem->opr.ch);
  2608. return __collseq_table_lookup (collseqwc, wc);
  2609. }
  2610. }
  2611. else if (br_elem->type == MB_CHAR)
  2612. {
  2613. return __collseq_table_lookup (collseqwc, br_elem->opr.wch);
  2614. }
  2615. else if (br_elem->type == COLL_SYM)
  2616. {
  2617. size_t sym_name_len = strlen ((char *) br_elem->opr.name);
  2618. if (nrules != 0)
  2619. {
  2620. int32_t elem, idx;
  2621. elem = seek_collating_symbol_entry (br_elem->opr.name,
  2622. sym_name_len);
  2623. if (symb_table[2 * elem] != 0)
  2624. {
  2625. /* We found the entry. */
  2626. idx = symb_table[2 * elem + 1];
  2627. /* Skip the name of collating element name. */
  2628. idx += 1 + extra[idx];
  2629. /* Skip the byte sequence of the collating element. */
  2630. idx += 1 + extra[idx];
  2631. /* Adjust for the alignment. */
  2632. idx = (idx + 3) & ~3;
  2633. /* Skip the multibyte collation sequence value. */
  2634. idx += sizeof (unsigned int);
  2635. /* Skip the wide char sequence of the collating element. */
  2636. idx += sizeof (unsigned int) *
  2637. (1 + *(unsigned int *) (extra + idx));
  2638. /* Return the collation sequence value. */
  2639. return *(unsigned int *) (extra + idx);
  2640. }
  2641. else if (symb_table[2 * elem] == 0 && sym_name_len == 1)
  2642. {
  2643. /* No valid character. Match it as a single byte
  2644. character. */
  2645. return collseqmb[br_elem->opr.name[0]];
  2646. }
  2647. }
  2648. else if (sym_name_len == 1)
  2649. return collseqmb[br_elem->opr.name[0]];
  2650. }
  2651. return UINT_MAX;
  2652. }
  2653. /* Local function for parse_bracket_exp used in _LIBC environement.
  2654. Build the range expression which starts from START_ELEM, and ends
  2655. at END_ELEM. The result are written to MBCSET and SBCSET.
  2656. RANGE_ALLOC is the allocated size of mbcset->range_starts, and
  2657. mbcset->range_ends, is a pointer argument sinse we may
  2658. update it. */
  2659. auto inline reg_errcode_t
  2660. __attribute ((always_inline))
  2661. build_range_exp (sbcset, mbcset, range_alloc, start_elem, end_elem)
  2662. re_charset_t *mbcset;
  2663. int *range_alloc;
  2664. re_bitset_ptr_t sbcset;
  2665. bracket_elem_t *start_elem, *end_elem;
  2666. {
  2667. unsigned int ch;
  2668. uint32_t start_collseq;
  2669. uint32_t end_collseq;
  2670. /* Equivalence Classes and Character Classes can't be a range
  2671. start/end. */
  2672. if (BE (start_elem->type == EQUIV_CLASS || start_elem->type == CHAR_CLASS
  2673. || end_elem->type == EQUIV_CLASS || end_elem->type == CHAR_CLASS,
  2674. 0))
  2675. return REG_ERANGE;
  2676. start_collseq = lookup_collation_sequence_value (start_elem);
  2677. end_collseq = lookup_collation_sequence_value (end_elem);
  2678. /* Check start/end collation sequence values. */
  2679. if (BE (start_collseq == UINT_MAX || end_collseq == UINT_MAX, 0))
  2680. return REG_ECOLLATE;
  2681. if (BE ((syntax & RE_NO_EMPTY_RANGES) && start_collseq > end_collseq, 0))
  2682. return REG_ERANGE;
  2683. /* Got valid collation sequence values, add them as a new entry.
  2684. However, if we have no collation elements, and the character set
  2685. is single byte, the single byte character set that we
  2686. build below suffices. */
  2687. if (nrules > 0 || dfa->mb_cur_max > 1)
  2688. {
  2689. /* Check the space of the arrays. */
  2690. if (BE (*range_alloc == mbcset->nranges, 0))
  2691. {
  2692. /* There is not enough space, need realloc. */
  2693. uint32_t *new_array_start;
  2694. uint32_t *new_array_end;
  2695. int new_nranges;
  2696. /* +1 in case of mbcset->nranges is 0. */
  2697. new_nranges = 2 * mbcset->nranges + 1;
  2698. new_array_start = re_realloc (mbcset->range_starts, uint32_t,
  2699. new_nranges);
  2700. new_array_end = re_realloc (mbcset->range_ends, uint32_t,
  2701. new_nranges);
  2702. if (BE (new_array_start == NULL || new_array_end == NULL, 0))
  2703. return REG_ESPACE;
  2704. mbcset->range_starts = new_array_start;
  2705. mbcset->range_ends = new_array_end;
  2706. *range_alloc = new_nranges;
  2707. }
  2708. mbcset->range_starts[mbcset->nranges] = start_collseq;
  2709. mbcset->range_ends[mbcset->nranges++] = end_collseq;
  2710. }
  2711. /* Build the table for single byte characters. */
  2712. for (ch = 0; ch < SBC_MAX; ch++)
  2713. {
  2714. uint32_t ch_collseq;
  2715. /*
  2716. if (MB_CUR_MAX == 1)
  2717. */
  2718. if (nrules == 0)
  2719. ch_collseq = collseqmb[ch];
  2720. else
  2721. ch_collseq = __collseq_table_lookup (collseqwc, __btowc (ch));
  2722. if (start_collseq <= ch_collseq && ch_collseq <= end_collseq)
  2723. bitset_set (sbcset, ch);
  2724. }
  2725. return REG_NOERROR;
  2726. }
  2727. /* Local function for parse_bracket_exp used in _LIBC environement.
  2728. Build the collating element which is represented by NAME.
  2729. The result are written to MBCSET and SBCSET.
  2730. COLL_SYM_ALLOC is the allocated size of mbcset->coll_sym, is a
  2731. pointer argument sinse we may update it. */
  2732. auto inline reg_errcode_t
  2733. __attribute ((always_inline))
  2734. build_collating_symbol (sbcset, mbcset, coll_sym_alloc, name)
  2735. re_charset_t *mbcset;
  2736. int *coll_sym_alloc;
  2737. re_bitset_ptr_t sbcset;
  2738. const unsigned char *name;
  2739. {
  2740. int32_t elem, idx;
  2741. size_t name_len = strlen ((const char *) name);
  2742. if (nrules != 0)
  2743. {
  2744. elem = seek_collating_symbol_entry (name, name_len);
  2745. if (symb_table[2 * elem] != 0)
  2746. {
  2747. /* We found the entry. */
  2748. idx = symb_table[2 * elem + 1];
  2749. /* Skip the name of collating element name. */
  2750. idx += 1 + extra[idx];
  2751. }
  2752. else if (symb_table[2 * elem] == 0 && name_len == 1)
  2753. {
  2754. /* No valid character, treat it as a normal
  2755. character. */
  2756. bitset_set (sbcset, name[0]);
  2757. return REG_NOERROR;
  2758. }
  2759. else
  2760. return REG_ECOLLATE;
  2761. /* Got valid collation sequence, add it as a new entry. */
  2762. /* Check the space of the arrays. */
  2763. if (BE (*coll_sym_alloc == mbcset->ncoll_syms, 0))
  2764. {
  2765. /* Not enough, realloc it. */
  2766. /* +1 in case of mbcset->ncoll_syms is 0. */
  2767. int new_coll_sym_alloc = 2 * mbcset->ncoll_syms + 1;
  2768. /* Use realloc since mbcset->coll_syms is NULL
  2769. if *alloc == 0. */
  2770. int32_t *new_coll_syms = re_realloc (mbcset->coll_syms, int32_t,
  2771. new_coll_sym_alloc);
  2772. if (BE (new_coll_syms == NULL, 0))
  2773. return REG_ESPACE;
  2774. mbcset->coll_syms = new_coll_syms;
  2775. *coll_sym_alloc = new_coll_sym_alloc;
  2776. }
  2777. mbcset->coll_syms[mbcset->ncoll_syms++] = idx;
  2778. return REG_NOERROR;
  2779. }
  2780. else
  2781. {
  2782. if (BE (name_len != 1, 0))
  2783. return REG_ECOLLATE;
  2784. else
  2785. {
  2786. bitset_set (sbcset, name[0]);
  2787. return REG_NOERROR;
  2788. }
  2789. }
  2790. }
  2791. #endif
  2792. re_token_t br_token;
  2793. re_bitset_ptr_t sbcset;
  2794. #ifdef RE_ENABLE_I18N
  2795. re_charset_t *mbcset;
  2796. int coll_sym_alloc = 0, range_alloc = 0, mbchar_alloc = 0;
  2797. int equiv_class_alloc = 0, char_class_alloc = 0;
  2798. #endif /* not RE_ENABLE_I18N */
  2799. int non_match = 0;
  2800. bin_tree_t *work_tree;
  2801. int token_len;
  2802. int first_round = 1;
  2803. #ifdef _LIBC
  2804. collseqmb = (const unsigned char *)
  2805. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQMB);
  2806. nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
  2807. if (nrules)
  2808. {
  2809. /*
  2810. if (MB_CUR_MAX > 1)
  2811. */
  2812. collseqwc = _NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQWC);
  2813. table_size = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_SYMB_HASH_SIZEMB);
  2814. symb_table = (const int32_t *) _NL_CURRENT (LC_COLLATE,
  2815. _NL_COLLATE_SYMB_TABLEMB);
  2816. extra = (const unsigned char *) _NL_CURRENT (LC_COLLATE,
  2817. _NL_COLLATE_SYMB_EXTRAMB);
  2818. }
  2819. #endif
  2820. sbcset = (re_bitset_ptr_t) calloc (sizeof (unsigned int), BITSET_UINTS);
  2821. #ifdef RE_ENABLE_I18N
  2822. mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1);
  2823. #endif /* RE_ENABLE_I18N */
  2824. #ifdef RE_ENABLE_I18N
  2825. if (BE (sbcset == NULL || mbcset == NULL, 0))
  2826. #else
  2827. if (BE (sbcset == NULL, 0))
  2828. #endif /* RE_ENABLE_I18N */
  2829. {
  2830. *err = REG_ESPACE;
  2831. return NULL;
  2832. }
  2833. token_len = peek_token_bracket (token, regexp, syntax);
  2834. if (BE (token->type == END_OF_RE, 0))
  2835. {
  2836. *err = REG_BADPAT;
  2837. goto parse_bracket_exp_free_return;
  2838. }
  2839. if (token->type == OP_NON_MATCH_LIST)
  2840. {
  2841. #ifdef RE_ENABLE_I18N
  2842. mbcset->non_match = 1;
  2843. #endif /* not RE_ENABLE_I18N */
  2844. non_match = 1;
  2845. if (syntax & RE_HAT_LISTS_NOT_NEWLINE)
  2846. bitset_set (sbcset, '\0');
  2847. re_string_skip_bytes (regexp, token_len); /* Skip a token. */
  2848. token_len = peek_token_bracket (token, regexp, syntax);
  2849. if (BE (token->type == END_OF_RE, 0))
  2850. {
  2851. *err = REG_BADPAT;
  2852. goto parse_bracket_exp_free_return;
  2853. }
  2854. }
  2855. /* We treat the first ']' as a normal character. */
  2856. if (token->type == OP_CLOSE_BRACKET)
  2857. token->type = CHARACTER;
  2858. while (1)
  2859. {
  2860. bracket_elem_t start_elem, end_elem;
  2861. unsigned char start_name_buf[BRACKET_NAME_BUF_SIZE];
  2862. unsigned char end_name_buf[BRACKET_NAME_BUF_SIZE];
  2863. reg_errcode_t ret;
  2864. int token_len2 = 0, is_range_exp = 0;
  2865. re_token_t token2;
  2866. start_elem.opr.name = start_name_buf;
  2867. ret = parse_bracket_element (&start_elem, regexp, token, token_len, dfa,
  2868. syntax, first_round);
  2869. if (BE (ret != REG_NOERROR, 0))
  2870. {
  2871. *err = ret;
  2872. goto parse_bracket_exp_free_return;
  2873. }
  2874. first_round = 0;
  2875. /* Get information about the next token. We need it in any case. */
  2876. token_len = peek_token_bracket (token, regexp, syntax);
  2877. /* Do not check for ranges if we know they are not allowed. */
  2878. if (start_elem.type != CHAR_CLASS && start_elem.type != EQUIV_CLASS)
  2879. {
  2880. if (BE (token->type == END_OF_RE, 0))
  2881. {
  2882. *err = REG_EBRACK;
  2883. goto parse_bracket_exp_free_return;
  2884. }
  2885. if (token->type == OP_CHARSET_RANGE)
  2886. {
  2887. re_string_skip_bytes (regexp, token_len); /* Skip '-'. */
  2888. token_len2 = peek_token_bracket (&token2, regexp, syntax);
  2889. if (BE (token2.type == END_OF_RE, 0))
  2890. {
  2891. *err = REG_EBRACK;
  2892. goto parse_bracket_exp_free_return;
  2893. }
  2894. if (token2.type == OP_CLOSE_BRACKET)
  2895. {
  2896. /* We treat the last '-' as a normal character. */
  2897. re_string_skip_bytes (regexp, -token_len);
  2898. token->type = CHARACTER;
  2899. }
  2900. else
  2901. is_range_exp = 1;
  2902. }
  2903. }
  2904. if (is_range_exp == 1)
  2905. {
  2906. end_elem.opr.name = end_name_buf;
  2907. ret = parse_bracket_element (&end_elem, regexp, &token2, token_len2,
  2908. dfa, syntax, 1);
  2909. if (BE (ret != REG_NOERROR, 0))
  2910. {
  2911. *err = ret;
  2912. goto parse_bracket_exp_free_return;
  2913. }
  2914. token_len = peek_token_bracket (token, regexp, syntax);
  2915. #ifdef _LIBC
  2916. *err = build_range_exp (sbcset, mbcset, &range_alloc,
  2917. &start_elem, &end_elem);
  2918. #else
  2919. # ifdef RE_ENABLE_I18N
  2920. *err = build_range_exp (sbcset,
  2921. dfa->mb_cur_max > 1 ? mbcset : NULL,
  2922. &range_alloc, &start_elem, &end_elem);
  2923. # else
  2924. *err = build_range_exp (sbcset, &start_elem, &end_elem);
  2925. # endif
  2926. #endif /* RE_ENABLE_I18N */
  2927. if (BE (*err != REG_NOERROR, 0))
  2928. goto parse_bracket_exp_free_return;
  2929. }
  2930. else
  2931. {
  2932. switch (start_elem.type)
  2933. {
  2934. case SB_CHAR:
  2935. bitset_set (sbcset, start_elem.opr.ch);
  2936. break;
  2937. #ifdef RE_ENABLE_I18N
  2938. case MB_CHAR:
  2939. /* Check whether the array has enough space. */
  2940. if (BE (mbchar_alloc == mbcset->nmbchars, 0))
  2941. {
  2942. wchar_t *new_mbchars;
  2943. /* Not enough, realloc it. */
  2944. /* +1 in case of mbcset->nmbchars is 0. */
  2945. mbchar_alloc = 2 * mbcset->nmbchars + 1;
  2946. /* Use realloc since array is NULL if *alloc == 0. */
  2947. new_mbchars = re_realloc (mbcset->mbchars, wchar_t,
  2948. mbchar_alloc);
  2949. if (BE (new_mbchars == NULL, 0))
  2950. goto parse_bracket_exp_espace;
  2951. mbcset->mbchars = new_mbchars;
  2952. }
  2953. mbcset->mbchars[mbcset->nmbchars++] = start_elem.opr.wch;
  2954. break;
  2955. #endif /* RE_ENABLE_I18N */
  2956. case EQUIV_CLASS:
  2957. *err = build_equiv_class (sbcset,
  2958. #ifdef RE_ENABLE_I18N
  2959. mbcset, &equiv_class_alloc,
  2960. #endif /* RE_ENABLE_I18N */
  2961. start_elem.opr.name);
  2962. if (BE (*err != REG_NOERROR, 0))
  2963. goto parse_bracket_exp_free_return;
  2964. break;
  2965. case COLL_SYM:
  2966. *err = build_collating_symbol (sbcset,
  2967. #ifdef RE_ENABLE_I18N
  2968. mbcset, &coll_sym_alloc,
  2969. #endif /* RE_ENABLE_I18N */
  2970. start_elem.opr.name);
  2971. if (BE (*err != REG_NOERROR, 0))
  2972. goto parse_bracket_exp_free_return;
  2973. break;
  2974. case CHAR_CLASS:
  2975. *err = build_charclass (regexp->trans, sbcset,
  2976. #ifdef RE_ENABLE_I18N
  2977. mbcset, &char_class_alloc,
  2978. #endif /* RE_ENABLE_I18N */
  2979. start_elem.opr.name, syntax);
  2980. if (BE (*err != REG_NOERROR, 0))
  2981. goto parse_bracket_exp_free_return;
  2982. break;
  2983. default:
  2984. assert (0);
  2985. break;
  2986. }
  2987. }
  2988. if (BE (token->type == END_OF_RE, 0))
  2989. {
  2990. *err = REG_EBRACK;
  2991. goto parse_bracket_exp_free_return;
  2992. }
  2993. if (token->type == OP_CLOSE_BRACKET)
  2994. break;
  2995. }
  2996. re_string_skip_bytes (regexp, token_len); /* Skip a token. */
  2997. /* If it is non-matching list. */
  2998. if (non_match)
  2999. bitset_not (sbcset);
  3000. #ifdef RE_ENABLE_I18N
  3001. /* Ensure only single byte characters are set. */
  3002. if (dfa->mb_cur_max > 1)
  3003. bitset_mask (sbcset, dfa->sb_char);
  3004. if (mbcset->nmbchars || mbcset->ncoll_syms || mbcset->nequiv_classes
  3005. || mbcset->nranges || (dfa->mb_cur_max > 1 && (mbcset->nchar_classes
  3006. || mbcset->non_match)))
  3007. {
  3008. bin_tree_t *mbc_tree;
  3009. int sbc_idx;
  3010. /* Build a tree for complex bracket. */
  3011. dfa->has_mb_node = 1;
  3012. br_token.type = COMPLEX_BRACKET;
  3013. br_token.opr.mbcset = mbcset;
  3014. mbc_tree = create_token_tree (dfa, NULL, NULL, &br_token);
  3015. if (BE (mbc_tree == NULL, 0))
  3016. goto parse_bracket_exp_espace;
  3017. for (sbc_idx = 0; sbc_idx < BITSET_UINTS; ++sbc_idx)
  3018. if (sbcset[sbc_idx])
  3019. break;
  3020. /* If there are no bits set in sbcset, there is no point
  3021. of having both SIMPLE_BRACKET and COMPLEX_BRACKET. */
  3022. if (sbc_idx < BITSET_UINTS)
  3023. {
  3024. /* Build a tree for simple bracket. */
  3025. br_token.type = SIMPLE_BRACKET;
  3026. br_token.opr.sbcset = sbcset;
  3027. work_tree = create_token_tree (dfa, NULL, NULL, &br_token);
  3028. if (BE (work_tree == NULL, 0))
  3029. goto parse_bracket_exp_espace;
  3030. /* Then join them by ALT node. */
  3031. work_tree = create_tree (dfa, work_tree, mbc_tree, OP_ALT);
  3032. if (BE (work_tree == NULL, 0))
  3033. goto parse_bracket_exp_espace;
  3034. }
  3035. else
  3036. {
  3037. re_free (sbcset);
  3038. work_tree = mbc_tree;
  3039. }
  3040. }
  3041. else
  3042. #endif /* not RE_ENABLE_I18N */
  3043. {
  3044. #ifdef RE_ENABLE_I18N
  3045. free_charset (mbcset);
  3046. #endif
  3047. /* Build a tree for simple bracket. */
  3048. br_token.type = SIMPLE_BRACKET;
  3049. br_token.opr.sbcset = sbcset;
  3050. work_tree = create_token_tree (dfa, NULL, NULL, &br_token);
  3051. if (BE (work_tree == NULL, 0))
  3052. goto parse_bracket_exp_espace;
  3053. }
  3054. return work_tree;
  3055. parse_bracket_exp_espace:
  3056. *err = REG_ESPACE;
  3057. parse_bracket_exp_free_return:
  3058. re_free (sbcset);
  3059. #ifdef RE_ENABLE_I18N
  3060. free_charset (mbcset);
  3061. #endif /* RE_ENABLE_I18N */
  3062. return NULL;
  3063. }
  3064. /* Parse an element in the bracket expression. */
  3065. static reg_errcode_t
  3066. parse_bracket_element (elem, regexp, token, token_len, dfa, syntax,
  3067. accept_hyphen)
  3068. bracket_elem_t *elem;
  3069. re_string_t *regexp;
  3070. re_token_t *token;
  3071. int token_len;
  3072. re_dfa_t *dfa;
  3073. reg_syntax_t syntax;
  3074. int accept_hyphen;
  3075. {
  3076. #ifdef RE_ENABLE_I18N
  3077. int cur_char_size;
  3078. cur_char_size = re_string_char_size_at (regexp, re_string_cur_idx (regexp));
  3079. if (cur_char_size > 1)
  3080. {
  3081. elem->type = MB_CHAR;
  3082. elem->opr.wch = re_string_wchar_at (regexp, re_string_cur_idx (regexp));
  3083. re_string_skip_bytes (regexp, cur_char_size);
  3084. return REG_NOERROR;
  3085. }
  3086. #endif /* RE_ENABLE_I18N */
  3087. re_string_skip_bytes (regexp, token_len); /* Skip a token. */
  3088. if (token->type == OP_OPEN_COLL_ELEM || token->type == OP_OPEN_CHAR_CLASS
  3089. || token->type == OP_OPEN_EQUIV_CLASS)
  3090. return parse_bracket_symbol (elem, regexp, token);
  3091. if (BE (token->type == OP_CHARSET_RANGE, 0) && !accept_hyphen)
  3092. {
  3093. /* A '-' must only appear as anything but a range indicator before
  3094. the closing bracket. Everything else is an error. */
  3095. re_token_t token2;
  3096. (void) peek_token_bracket (&token2, regexp, syntax);
  3097. if (token2.type != OP_CLOSE_BRACKET)
  3098. /* The actual error value is not standardized since this whole
  3099. case is undefined. But ERANGE makes good sense. */
  3100. return REG_ERANGE;
  3101. }
  3102. elem->type = SB_CHAR;
  3103. elem->opr.ch = token->opr.c;
  3104. return REG_NOERROR;
  3105. }
  3106. /* Parse a bracket symbol in the bracket expression. Bracket symbols are
  3107. such as [:<character_class>:], [.<collating_element>.], and
  3108. [=<equivalent_class>=]. */
  3109. static reg_errcode_t
  3110. parse_bracket_symbol (elem, regexp, token)
  3111. bracket_elem_t *elem;
  3112. re_string_t *regexp;
  3113. re_token_t *token;
  3114. {
  3115. unsigned char ch, delim = token->opr.c;
  3116. int i = 0;
  3117. if (re_string_eoi(regexp))
  3118. return REG_EBRACK;
  3119. for (;; ++i)
  3120. {
  3121. if (i >= BRACKET_NAME_BUF_SIZE)
  3122. return REG_EBRACK;
  3123. if (token->type == OP_OPEN_CHAR_CLASS)
  3124. ch = re_string_fetch_byte_case (regexp);
  3125. else
  3126. ch = re_string_fetch_byte (regexp);
  3127. if (re_string_eoi(regexp))
  3128. return REG_EBRACK;
  3129. if (ch == delim && re_string_peek_byte (regexp, 0) == ']')
  3130. break;
  3131. elem->opr.name[i] = ch;
  3132. }
  3133. re_string_skip_bytes (regexp, 1);
  3134. elem->opr.name[i] = '\0';
  3135. switch (token->type)
  3136. {
  3137. case OP_OPEN_COLL_ELEM:
  3138. elem->type = COLL_SYM;
  3139. break;
  3140. case OP_OPEN_EQUIV_CLASS:
  3141. elem->type = EQUIV_CLASS;
  3142. break;
  3143. case OP_OPEN_CHAR_CLASS:
  3144. elem->type = CHAR_CLASS;
  3145. break;
  3146. default:
  3147. break;
  3148. }
  3149. return REG_NOERROR;
  3150. }
  3151. /* Helper function for parse_bracket_exp.
  3152. Build the equivalence class which is represented by NAME.
  3153. The result are written to MBCSET and SBCSET.
  3154. EQUIV_CLASS_ALLOC is the allocated size of mbcset->equiv_classes,
  3155. is a pointer argument sinse we may update it. */
  3156. static reg_errcode_t
  3157. #ifdef RE_ENABLE_I18N
  3158. build_equiv_class (sbcset, mbcset, equiv_class_alloc, name)
  3159. re_charset_t *mbcset;
  3160. int *equiv_class_alloc;
  3161. #else /* not RE_ENABLE_I18N */
  3162. build_equiv_class (sbcset, name)
  3163. #endif /* not RE_ENABLE_I18N */
  3164. re_bitset_ptr_t sbcset;
  3165. const unsigned char *name;
  3166. {
  3167. #if defined _LIBC
  3168. uint32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
  3169. if (nrules != 0)
  3170. {
  3171. const int32_t *table, *indirect;
  3172. const unsigned char *weights, *extra, *cp;
  3173. unsigned char char_buf[2];
  3174. int32_t idx1, idx2;
  3175. unsigned int ch;
  3176. size_t len;
  3177. /* This #include defines a local function! */
  3178. # include <locale/weight.h>
  3179. /* Calculate the index for equivalence class. */
  3180. cp = name;
  3181. table = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
  3182. weights = (const unsigned char *) _NL_CURRENT (LC_COLLATE,
  3183. _NL_COLLATE_WEIGHTMB);
  3184. extra = (const unsigned char *) _NL_CURRENT (LC_COLLATE,
  3185. _NL_COLLATE_EXTRAMB);
  3186. indirect = (const int32_t *) _NL_CURRENT (LC_COLLATE,
  3187. _NL_COLLATE_INDIRECTMB);
  3188. idx1 = findidx (&cp);
  3189. if (BE (idx1 == 0 || cp < name + strlen ((const char *) name), 0))
  3190. /* This isn't a valid character. */
  3191. return REG_ECOLLATE;
  3192. /* Build single byte matcing table for this equivalence class. */
  3193. char_buf[1] = (unsigned char) '\0';
  3194. len = weights[idx1];
  3195. for (ch = 0; ch < SBC_MAX; ++ch)
  3196. {
  3197. char_buf[0] = ch;
  3198. cp = char_buf;
  3199. idx2 = findidx (&cp);
  3200. /*
  3201. idx2 = table[ch];
  3202. */
  3203. if (idx2 == 0)
  3204. /* This isn't a valid character. */
  3205. continue;
  3206. if (len == weights[idx2])
  3207. {
  3208. int cnt = 0;
  3209. while (cnt <= len &&
  3210. weights[idx1 + 1 + cnt] == weights[idx2 + 1 + cnt])
  3211. ++cnt;
  3212. if (cnt > len)
  3213. bitset_set (sbcset, ch);
  3214. }
  3215. }
  3216. /* Check whether the array has enough space. */
  3217. if (BE (*equiv_class_alloc == mbcset->nequiv_classes, 0))
  3218. {
  3219. /* Not enough, realloc it. */
  3220. /* +1 in case of mbcset->nequiv_classes is 0. */
  3221. int new_equiv_class_alloc = 2 * mbcset->nequiv_classes + 1;
  3222. /* Use realloc since the array is NULL if *alloc == 0. */
  3223. int32_t *new_equiv_classes = re_realloc (mbcset->equiv_classes,
  3224. int32_t,
  3225. new_equiv_class_alloc);
  3226. if (BE (new_equiv_classes == NULL, 0))
  3227. return REG_ESPACE;
  3228. mbcset->equiv_classes = new_equiv_classes;
  3229. *equiv_class_alloc = new_equiv_class_alloc;
  3230. }
  3231. mbcset->equiv_classes[mbcset->nequiv_classes++] = idx1;
  3232. }
  3233. else
  3234. #endif /* _LIBC */
  3235. {
  3236. if (BE (strlen ((const char *) name) != 1, 0))
  3237. return REG_ECOLLATE;
  3238. bitset_set (sbcset, *name);
  3239. }
  3240. return REG_NOERROR;
  3241. }
  3242. /* Helper function for parse_bracket_exp.
  3243. Build the character class which is represented by NAME.
  3244. The result are written to MBCSET and SBCSET.
  3245. CHAR_CLASS_ALLOC is the allocated size of mbcset->char_classes,
  3246. is a pointer argument sinse we may update it. */
  3247. static reg_errcode_t
  3248. #ifdef RE_ENABLE_I18N
  3249. build_charclass (trans, sbcset, mbcset, char_class_alloc, class_name, syntax)
  3250. re_charset_t *mbcset;
  3251. int *char_class_alloc;
  3252. #else /* not RE_ENABLE_I18N */
  3253. build_charclass (trans, sbcset, class_name, syntax)
  3254. #endif /* not RE_ENABLE_I18N */
  3255. unsigned RE_TRANSLATE_TYPE trans;
  3256. re_bitset_ptr_t sbcset;
  3257. const unsigned char *class_name;
  3258. reg_syntax_t syntax;
  3259. {
  3260. int i;
  3261. const char *name = (const char *) class_name;
  3262. /* In case of REG_ICASE "upper" and "lower" match the both of
  3263. upper and lower cases. */
  3264. if ((syntax & RE_ICASE)
  3265. && (strcmp (name, "upper") == 0 || strcmp (name, "lower") == 0))
  3266. name = "alpha";
  3267. #ifdef RE_ENABLE_I18N
  3268. /* Check the space of the arrays. */
  3269. if (BE (*char_class_alloc == mbcset->nchar_classes, 0))
  3270. {
  3271. /* Not enough, realloc it. */
  3272. /* +1 in case of mbcset->nchar_classes is 0. */
  3273. int new_char_class_alloc = 2 * mbcset->nchar_classes + 1;
  3274. /* Use realloc since array is NULL if *alloc == 0. */
  3275. wctype_t *new_char_classes = re_realloc (mbcset->char_classes, wctype_t,
  3276. new_char_class_alloc);
  3277. if (BE (new_char_classes == NULL, 0))
  3278. return REG_ESPACE;
  3279. mbcset->char_classes = new_char_classes;
  3280. *char_class_alloc = new_char_class_alloc;
  3281. }
  3282. mbcset->char_classes[mbcset->nchar_classes++] = __wctype (name);
  3283. #endif /* RE_ENABLE_I18N */
  3284. #define BUILD_CHARCLASS_LOOP(ctype_func) \
  3285. for (i = 0; i < SBC_MAX; ++i) \
  3286. { \
  3287. if (ctype_func (i)) \
  3288. { \
  3289. int ch = trans ? trans[i] : i; \
  3290. bitset_set (sbcset, ch); \
  3291. } \
  3292. }
  3293. if (strcmp (name, "alnum") == 0)
  3294. BUILD_CHARCLASS_LOOP (isalnum)
  3295. else if (strcmp (name, "cntrl") == 0)
  3296. BUILD_CHARCLASS_LOOP (iscntrl)
  3297. else if (strcmp (name, "lower") == 0)
  3298. BUILD_CHARCLASS_LOOP (islower)
  3299. else if (strcmp (name, "space") == 0)
  3300. BUILD_CHARCLASS_LOOP (isspace)
  3301. else if (strcmp (name, "alpha") == 0)
  3302. BUILD_CHARCLASS_LOOP (isalpha)
  3303. else if (strcmp (name, "digit") == 0)
  3304. BUILD_CHARCLASS_LOOP (isdigit)
  3305. else if (strcmp (name, "print") == 0)
  3306. BUILD_CHARCLASS_LOOP (isprint)
  3307. else if (strcmp (name, "upper") == 0)
  3308. BUILD_CHARCLASS_LOOP (isupper)
  3309. else if (strcmp (name, "blank") == 0)
  3310. BUILD_CHARCLASS_LOOP (isblank)
  3311. else if (strcmp (name, "graph") == 0)
  3312. BUILD_CHARCLASS_LOOP (isgraph)
  3313. else if (strcmp (name, "punct") == 0)
  3314. BUILD_CHARCLASS_LOOP (ispunct)
  3315. else if (strcmp (name, "xdigit") == 0)
  3316. BUILD_CHARCLASS_LOOP (isxdigit)
  3317. else
  3318. return REG_ECTYPE;
  3319. return REG_NOERROR;
  3320. }
  3321. static bin_tree_t *
  3322. build_charclass_op (dfa, trans, class_name, extra, non_match, err)
  3323. re_dfa_t *dfa;
  3324. unsigned RE_TRANSLATE_TYPE trans;
  3325. const unsigned char *class_name;
  3326. const unsigned char *extra;
  3327. int non_match;
  3328. reg_errcode_t *err;
  3329. {
  3330. re_bitset_ptr_t sbcset;
  3331. #ifdef RE_ENABLE_I18N
  3332. re_charset_t *mbcset;
  3333. int alloc = 0;
  3334. #endif /* not RE_ENABLE_I18N */
  3335. reg_errcode_t ret;
  3336. re_token_t br_token;
  3337. bin_tree_t *tree;
  3338. sbcset = (re_bitset_ptr_t) calloc (sizeof (unsigned int), BITSET_UINTS);
  3339. #ifdef RE_ENABLE_I18N
  3340. mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1);
  3341. #endif /* RE_ENABLE_I18N */
  3342. #ifdef RE_ENABLE_I18N
  3343. if (BE (sbcset == NULL || mbcset == NULL, 0))
  3344. #else /* not RE_ENABLE_I18N */
  3345. if (BE (sbcset == NULL, 0))
  3346. #endif /* not RE_ENABLE_I18N */
  3347. {
  3348. *err = REG_ESPACE;
  3349. return NULL;
  3350. }
  3351. if (non_match)
  3352. {
  3353. #ifdef RE_ENABLE_I18N
  3354. /*
  3355. if (syntax & RE_HAT_LISTS_NOT_NEWLINE)
  3356. bitset_set(cset->sbcset, '\0');
  3357. */
  3358. mbcset->non_match = 1;
  3359. #endif /* not RE_ENABLE_I18N */
  3360. }
  3361. /* We don't care the syntax in this case. */
  3362. ret = build_charclass (trans, sbcset,
  3363. #ifdef RE_ENABLE_I18N
  3364. mbcset, &alloc,
  3365. #endif /* RE_ENABLE_I18N */
  3366. class_name, 0);
  3367. if (BE (ret != REG_NOERROR, 0))
  3368. {
  3369. re_free (sbcset);
  3370. #ifdef RE_ENABLE_I18N
  3371. free_charset (mbcset);
  3372. #endif /* RE_ENABLE_I18N */
  3373. *err = ret;
  3374. return NULL;
  3375. }
  3376. /* \w match '_' also. */
  3377. for (; *extra; extra++)
  3378. bitset_set (sbcset, *extra);
  3379. /* If it is non-matching list. */
  3380. if (non_match)
  3381. bitset_not (sbcset);
  3382. #ifdef RE_ENABLE_I18N
  3383. /* Ensure only single byte characters are set. */
  3384. if (dfa->mb_cur_max > 1)
  3385. bitset_mask (sbcset, dfa->sb_char);
  3386. #endif
  3387. /* Build a tree for simple bracket. */
  3388. br_token.type = SIMPLE_BRACKET;
  3389. br_token.opr.sbcset = sbcset;
  3390. tree = create_token_tree (dfa, NULL, NULL, &br_token);
  3391. if (BE (tree == NULL, 0))
  3392. goto build_word_op_espace;
  3393. #ifdef RE_ENABLE_I18N
  3394. if (dfa->mb_cur_max > 1)
  3395. {
  3396. bin_tree_t *mbc_tree;
  3397. /* Build a tree for complex bracket. */
  3398. br_token.type = COMPLEX_BRACKET;
  3399. br_token.opr.mbcset = mbcset;
  3400. dfa->has_mb_node = 1;
  3401. mbc_tree = create_token_tree (dfa, NULL, NULL, &br_token);
  3402. if (BE (mbc_tree == NULL, 0))
  3403. goto build_word_op_espace;
  3404. /* Then join them by ALT node. */
  3405. tree = create_tree (dfa, tree, mbc_tree, OP_ALT);
  3406. if (BE (mbc_tree != NULL, 1))
  3407. return tree;
  3408. }
  3409. else
  3410. {
  3411. free_charset (mbcset);
  3412. return tree;
  3413. }
  3414. #else /* not RE_ENABLE_I18N */
  3415. return tree;
  3416. #endif /* not RE_ENABLE_I18N */
  3417. build_word_op_espace:
  3418. re_free (sbcset);
  3419. #ifdef RE_ENABLE_I18N
  3420. free_charset (mbcset);
  3421. #endif /* RE_ENABLE_I18N */
  3422. *err = REG_ESPACE;
  3423. return NULL;
  3424. }
  3425. /* This is intended for the expressions like "a{1,3}".
  3426. Fetch a number from `input', and return the number.
  3427. Return -1, if the number field is empty like "{,1}".
  3428. Return -2, If an error is occured. */
  3429. static int
  3430. fetch_number (input, token, syntax)
  3431. re_string_t *input;
  3432. re_token_t *token;
  3433. reg_syntax_t syntax;
  3434. {
  3435. int num = -1;
  3436. unsigned char c;
  3437. while (1)
  3438. {
  3439. fetch_token (token, input, syntax);
  3440. c = token->opr.c;
  3441. if (BE (token->type == END_OF_RE, 0))
  3442. return -2;
  3443. if (token->type == OP_CLOSE_DUP_NUM || c == ',')
  3444. break;
  3445. num = ((token->type != CHARACTER || c < '0' || '9' < c || num == -2)
  3446. ? -2 : ((num == -1) ? c - '0' : num * 10 + c - '0'));
  3447. num = (num > RE_DUP_MAX) ? -2 : num;
  3448. }
  3449. return num;
  3450. }
  3451. #ifdef RE_ENABLE_I18N
  3452. static void
  3453. free_charset (re_charset_t *cset)
  3454. {
  3455. re_free (cset->mbchars);
  3456. # ifdef _LIBC
  3457. re_free (cset->coll_syms);
  3458. re_free (cset->equiv_classes);
  3459. re_free (cset->range_starts);
  3460. re_free (cset->range_ends);
  3461. # endif
  3462. re_free (cset->char_classes);
  3463. re_free (cset);
  3464. }
  3465. #endif /* RE_ENABLE_I18N */
  3466. /* Functions for binary tree operation. */
  3467. /* Create a tree node. */
  3468. static bin_tree_t *
  3469. create_tree (dfa, left, right, type)
  3470. re_dfa_t *dfa;
  3471. bin_tree_t *left;
  3472. bin_tree_t *right;
  3473. re_token_type_t type;
  3474. {
  3475. re_token_t t;
  3476. t.type = type;
  3477. return create_token_tree (dfa, left, right, &t);
  3478. }
  3479. static bin_tree_t *
  3480. create_token_tree (dfa, left, right, token)
  3481. re_dfa_t *dfa;
  3482. bin_tree_t *left;
  3483. bin_tree_t *right;
  3484. const re_token_t *token;
  3485. {
  3486. bin_tree_t *tree;
  3487. if (BE (dfa->str_tree_storage_idx == BIN_TREE_STORAGE_SIZE, 0))
  3488. {
  3489. bin_tree_storage_t *storage = re_malloc (bin_tree_storage_t, 1);
  3490. if (storage == NULL)
  3491. return NULL;
  3492. storage->next = dfa->str_tree_storage;
  3493. dfa->str_tree_storage = storage;
  3494. dfa->str_tree_storage_idx = 0;
  3495. }
  3496. tree = &dfa->str_tree_storage->data[dfa->str_tree_storage_idx++];
  3497. tree->parent = NULL;
  3498. tree->left = left;
  3499. tree->right = right;
  3500. tree->token = *token;
  3501. tree->token.duplicated = 0;
  3502. tree->token.opt_subexp = 0;
  3503. tree->first = NULL;
  3504. tree->next = NULL;
  3505. tree->node_idx = -1;
  3506. if (left != NULL)
  3507. left->parent = tree;
  3508. if (right != NULL)
  3509. right->parent = tree;
  3510. return tree;
  3511. }
  3512. /* Mark the tree SRC as an optional subexpression.
  3513. To be called from preorder or postorder. */
  3514. static reg_errcode_t
  3515. mark_opt_subexp (extra, node)
  3516. void *extra;
  3517. bin_tree_t *node;
  3518. {
  3519. int idx = (int) (long) extra;
  3520. if (node->token.type == SUBEXP && node->token.opr.idx == idx)
  3521. node->token.opt_subexp = 1;
  3522. return REG_NOERROR;
  3523. }
  3524. /* Free the allocated memory inside NODE. */
  3525. static void
  3526. free_token (re_token_t *node)
  3527. {
  3528. #ifdef RE_ENABLE_I18N
  3529. if (node->type == COMPLEX_BRACKET && node->duplicated == 0)
  3530. free_charset (node->opr.mbcset);
  3531. else
  3532. #endif /* RE_ENABLE_I18N */
  3533. if (node->type == SIMPLE_BRACKET && node->duplicated == 0)
  3534. re_free (node->opr.sbcset);
  3535. }
  3536. /* Worker function for tree walking. Free the allocated memory inside NODE
  3537. and its children. */
  3538. static reg_errcode_t
  3539. free_tree (void *extra, bin_tree_t *node)
  3540. {
  3541. free_token (&node->token);
  3542. return REG_NOERROR;
  3543. }
  3544. /* Duplicate the node SRC, and return new node. This is a preorder
  3545. visit similar to the one implemented by the generic visitor, but
  3546. we need more infrastructure to maintain two parallel trees --- so,
  3547. it's easier to duplicate. */
  3548. static bin_tree_t *
  3549. duplicate_tree (root, dfa)
  3550. const bin_tree_t *root;
  3551. re_dfa_t *dfa;
  3552. {
  3553. const bin_tree_t *node;
  3554. bin_tree_t *dup_root;
  3555. bin_tree_t **p_new = &dup_root, *dup_node = root->parent;
  3556. for (node = root; ; )
  3557. {
  3558. /* Create a new tree and link it back to the current parent. */
  3559. *p_new = create_token_tree (dfa, NULL, NULL, &node->token);
  3560. if (*p_new == NULL)
  3561. return NULL;
  3562. (*p_new)->parent = dup_node;
  3563. (*p_new)->token.duplicated = 1;
  3564. dup_node = *p_new;
  3565. /* Go to the left node, or up and to the right. */
  3566. if (node->left)
  3567. {
  3568. node = node->left;
  3569. p_new = &dup_node->left;
  3570. }
  3571. else
  3572. {
  3573. const bin_tree_t *prev = NULL;
  3574. while (node->right == prev || node->right == NULL)
  3575. {
  3576. prev = node;
  3577. node = node->parent;
  3578. dup_node = dup_node->parent;
  3579. if (!node)
  3580. return dup_root;
  3581. }
  3582. node = node->right;
  3583. p_new = &dup_node->right;
  3584. }
  3585. }
  3586. }