PageRenderTime 67ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/CS/tags/V1.2rc2/libs/csutil/generic/regcomp.c

#
C | 2315 lines | 1878 code | 161 blank | 276 comment | 600 complexity | 54758e812c38d3850d49b3fd9ef6acce MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, LGPL-2.0
  1. /* Extended regular expression matching and search library.
  2. Copyright (C) 2002, 2003 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. #ifdef _MSC_VER
  18. // Silence some warnings on MSVC
  19. #pragma warning (disable:4018) // signed/unsigned mismatch
  20. #pragma warning (disable:4047) /* 'type' differs in levels of indirection
  21. * from 'type' */
  22. #pragma warning (disable:4267) /* conversion from 'type' to 'type', possible
  23. * loss of data */
  24. #endif
  25. static reg_errcode_t re_compile_internal (regex_t *preg, const char * pattern,
  26. int length, reg_syntax_t syntax);
  27. static void re_compile_fastmap_iter (regex_t *bufp,
  28. const re_dfastate_t *init_state,
  29. char *fastmap);
  30. static reg_errcode_t init_dfa (re_dfa_t *dfa, int pat_len);
  31. static reg_errcode_t init_word_char (re_dfa_t *dfa);
  32. #ifdef RE_ENABLE_I18N
  33. static void free_charset (re_charset_t *cset);
  34. #endif /* RE_ENABLE_I18N */
  35. static void free_workarea_compile (regex_t *preg);
  36. static reg_errcode_t create_initial_state (re_dfa_t *dfa);
  37. static reg_errcode_t analyze (re_dfa_t *dfa);
  38. static reg_errcode_t analyze_tree (re_dfa_t *dfa, bin_tree_t *node);
  39. static void calc_first (re_dfa_t *dfa, bin_tree_t *node);
  40. static void calc_next (re_dfa_t *dfa, bin_tree_t *node);
  41. static void calc_epsdest (re_dfa_t *dfa, bin_tree_t *node);
  42. static reg_errcode_t duplicate_node_closure (re_dfa_t *dfa, int top_org_node,
  43. int top_clone_node, int root_node,
  44. unsigned int constraint);
  45. static reg_errcode_t duplicate_node (int *new_idx, re_dfa_t *dfa, int org_idx,
  46. unsigned int constraint);
  47. static int search_duplicated_node (re_dfa_t *dfa, int org_node,
  48. unsigned int constraint);
  49. static reg_errcode_t calc_eclosure (re_dfa_t *dfa);
  50. static reg_errcode_t calc_eclosure_iter (re_node_set *new_set, re_dfa_t *dfa,
  51. int node, int root);
  52. static void calc_inveclosure (re_dfa_t *dfa);
  53. static int fetch_number (re_string_t *input, re_token_t *token,
  54. reg_syntax_t syntax);
  55. static re_token_t fetch_token (re_string_t *input, reg_syntax_t syntax);
  56. static int peek_token (re_token_t *token, re_string_t *input,
  57. reg_syntax_t syntax);
  58. static int peek_token_bracket (re_token_t *token, re_string_t *input,
  59. reg_syntax_t syntax);
  60. static bin_tree_t *parse (re_string_t *regexp, regex_t *preg,
  61. reg_syntax_t syntax, reg_errcode_t *err);
  62. static bin_tree_t *parse_reg_exp (re_string_t *regexp, regex_t *preg,
  63. re_token_t *token, reg_syntax_t syntax,
  64. int nest, reg_errcode_t *err);
  65. static bin_tree_t *parse_branch (re_string_t *regexp, regex_t *preg,
  66. re_token_t *token, reg_syntax_t syntax,
  67. int nest, reg_errcode_t *err);
  68. static bin_tree_t *parse_expression (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_sub_exp (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_dup_op (bin_tree_t *dup_elem, re_string_t *regexp,
  75. re_dfa_t *dfa, re_token_t *token,
  76. reg_syntax_t syntax, reg_errcode_t *err);
  77. static bin_tree_t *parse_bracket_exp (re_string_t *regexp, re_dfa_t *dfa,
  78. re_token_t *token, reg_syntax_t syntax,
  79. reg_errcode_t *err);
  80. static reg_errcode_t parse_bracket_element (bracket_elem_t *elem,
  81. re_string_t *regexp,
  82. re_token_t *token, int token_len,
  83. re_dfa_t *dfa,
  84. reg_syntax_t syntax);
  85. static reg_errcode_t parse_bracket_symbol (bracket_elem_t *elem,
  86. re_string_t *regexp,
  87. re_token_t *token);
  88. #ifndef _LIBC
  89. # ifdef RE_ENABLE_I18N
  90. static reg_errcode_t build_range_exp (re_bitset_ptr_t sbcset,
  91. re_charset_t *mbcset, int *range_alloc,
  92. bracket_elem_t *start_elem,
  93. bracket_elem_t *end_elem);
  94. static reg_errcode_t build_collating_symbol (re_bitset_ptr_t sbcset,
  95. re_charset_t *mbcset,
  96. int *coll_sym_alloc,
  97. const unsigned char *name);
  98. # else /* not RE_ENABLE_I18N */
  99. static reg_errcode_t build_range_exp (re_bitset_ptr_t sbcset,
  100. bracket_elem_t *start_elem,
  101. bracket_elem_t *end_elem);
  102. static reg_errcode_t build_collating_symbol (re_bitset_ptr_t sbcset,
  103. const unsigned char *name);
  104. # endif /* not RE_ENABLE_I18N */
  105. #endif /* not _LIBC */
  106. #ifdef RE_ENABLE_I18N
  107. static reg_errcode_t build_equiv_class (re_bitset_ptr_t sbcset,
  108. re_charset_t *mbcset,
  109. int *equiv_class_alloc,
  110. const unsigned char *name);
  111. static reg_errcode_t build_charclass (re_bitset_ptr_t sbcset,
  112. re_charset_t *mbcset,
  113. int *char_class_alloc,
  114. const unsigned char *class_name,
  115. reg_syntax_t syntax);
  116. #else /* not RE_ENABLE_I18N */
  117. static reg_errcode_t build_equiv_class (re_bitset_ptr_t sbcset,
  118. const unsigned char *name);
  119. static reg_errcode_t build_charclass (re_bitset_ptr_t sbcset,
  120. const unsigned char *class_name,
  121. reg_syntax_t syntax);
  122. #endif /* not RE_ENABLE_I18N */
  123. static bin_tree_t *build_word_op (re_dfa_t *dfa, int not, reg_errcode_t *err);
  124. static void free_bin_tree (bin_tree_t *tree);
  125. static bin_tree_t *create_tree (bin_tree_t *left, bin_tree_t *right,
  126. re_token_type_t type, int index);
  127. static bin_tree_t *duplicate_tree (const bin_tree_t *src, re_dfa_t *dfa);
  128. /* This table gives an error message for each of the error codes listed
  129. in regex.h. Obviously the order here has to be same as there.
  130. POSIX doesn't require that we do anything for REG_NOERROR,
  131. but why not be nice? */
  132. const char __re_error_msgid[] attribute_hidden =
  133. {
  134. #define REG_NOERROR_IDX 0
  135. gettext_noop ("Success") /* REG_NOERROR */
  136. "\0"
  137. #define REG_NOMATCH_IDX (REG_NOERROR_IDX + sizeof "Success")
  138. gettext_noop ("No match") /* REG_NOMATCH */
  139. "\0"
  140. #define REG_BADPAT_IDX (REG_NOMATCH_IDX + sizeof "No match")
  141. gettext_noop ("Invalid regular expression") /* REG_BADPAT */
  142. "\0"
  143. #define REG_ECOLLATE_IDX (REG_BADPAT_IDX + sizeof "Invalid regular expression")
  144. gettext_noop ("Invalid collation character") /* REG_ECOLLATE */
  145. "\0"
  146. #define REG_ECTYPE_IDX (REG_ECOLLATE_IDX + sizeof "Invalid collation character")
  147. gettext_noop ("Invalid character class name") /* REG_ECTYPE */
  148. "\0"
  149. #define REG_EESCAPE_IDX (REG_ECTYPE_IDX + sizeof "Invalid character class name")
  150. gettext_noop ("Trailing backslash") /* REG_EESCAPE */
  151. "\0"
  152. #define REG_ESUBREG_IDX (REG_EESCAPE_IDX + sizeof "Trailing backslash")
  153. gettext_noop ("Invalid back reference") /* REG_ESUBREG */
  154. "\0"
  155. #define REG_EBRACK_IDX (REG_ESUBREG_IDX + sizeof "Invalid back reference")
  156. gettext_noop ("Unmatched [ or [^") /* REG_EBRACK */
  157. "\0"
  158. #define REG_EPAREN_IDX (REG_EBRACK_IDX + sizeof "Unmatched [ or [^")
  159. gettext_noop ("Unmatched ( or \\(") /* REG_EPAREN */
  160. "\0"
  161. #define REG_EBRACE_IDX (REG_EPAREN_IDX + sizeof "Unmatched ( or \\(")
  162. gettext_noop ("Unmatched \\{") /* REG_EBRACE */
  163. "\0"
  164. #define REG_BADBR_IDX (REG_EBRACE_IDX + sizeof "Unmatched \\{")
  165. gettext_noop ("Invalid content of \\{\\}") /* REG_BADBR */
  166. "\0"
  167. #define REG_ERANGE_IDX (REG_BADBR_IDX + sizeof "Invalid content of \\{\\}")
  168. gettext_noop ("Invalid range end") /* REG_ERANGE */
  169. "\0"
  170. #define REG_ESPACE_IDX (REG_ERANGE_IDX + sizeof "Invalid range end")
  171. gettext_noop ("Memory exhausted") /* REG_ESPACE */
  172. "\0"
  173. #define REG_BADRPT_IDX (REG_ESPACE_IDX + sizeof "Memory exhausted")
  174. gettext_noop ("Invalid preceding regular expression") /* REG_BADRPT */
  175. "\0"
  176. #define REG_EEND_IDX (REG_BADRPT_IDX + sizeof "Invalid preceding regular expression")
  177. gettext_noop ("Premature end of regular expression") /* REG_EEND */
  178. "\0"
  179. #define REG_ESIZE_IDX (REG_EEND_IDX + sizeof "Premature end of regular expression")
  180. gettext_noop ("Regular expression too big") /* REG_ESIZE */
  181. "\0"
  182. #define REG_ERPAREN_IDX (REG_ESIZE_IDX + sizeof "Regular expression too big")
  183. gettext_noop ("Unmatched ) or \\)") /* REG_ERPAREN */
  184. };
  185. const size_t __re_error_msgid_idx[] attribute_hidden =
  186. {
  187. REG_NOERROR_IDX,
  188. REG_NOMATCH_IDX,
  189. REG_BADPAT_IDX,
  190. REG_ECOLLATE_IDX,
  191. REG_ECTYPE_IDX,
  192. REG_EESCAPE_IDX,
  193. REG_ESUBREG_IDX,
  194. REG_EBRACK_IDX,
  195. REG_EPAREN_IDX,
  196. REG_EBRACE_IDX,
  197. REG_BADBR_IDX,
  198. REG_ERANGE_IDX,
  199. REG_ESPACE_IDX,
  200. REG_BADRPT_IDX,
  201. REG_EEND_IDX,
  202. REG_ESIZE_IDX,
  203. REG_ERPAREN_IDX
  204. };
  205. /* Entry points for GNU code. */
  206. /* re_compile_pattern is the GNU regular expression compiler: it
  207. compiles PATTERN (of length LENGTH) and puts the result in BUFP.
  208. Returns 0 if the pattern was valid, otherwise an error string.
  209. Assumes the `allocated' (and perhaps `buffer') and `translate' fields
  210. are set in BUFP on entry. */
  211. const char *
  212. re_compile_pattern (pattern, length, bufp)
  213. const char *pattern;
  214. size_t length;
  215. struct re_pattern_buffer *bufp;
  216. {
  217. reg_errcode_t ret;
  218. /* And GNU code determines whether or not to get register information
  219. by passing null for the REGS argument to re_match, etc., not by
  220. setting no_sub. */
  221. bufp->no_sub = 0;
  222. /* Match anchors at newline. */
  223. bufp->newline_anchor = 1;
  224. ret = re_compile_internal (bufp, pattern, length, re_syntax_options);
  225. if (!ret)
  226. return NULL;
  227. return gettext (__re_error_msgid + __re_error_msgid_idx[(int) ret]);
  228. }
  229. #ifdef _LIBC
  230. weak_alias (__re_compile_pattern, re_compile_pattern)
  231. #endif
  232. /* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
  233. also be assigned to arbitrarily: each pattern buffer stores its own
  234. syntax, so it can be changed between regex compilations. */
  235. /* This has no initializer because initialized variables in Emacs
  236. become read-only after dumping. */
  237. reg_syntax_t re_syntax_options;
  238. /* Specify the precise syntax of regexps for compilation. This provides
  239. for compatibility for various utilities which historically have
  240. different, incompatible syntaxes.
  241. The argument SYNTAX is a bit mask comprised of the various bits
  242. defined in regex.h. We return the old syntax. */
  243. reg_syntax_t
  244. re_set_syntax (syntax)
  245. reg_syntax_t syntax;
  246. {
  247. reg_syntax_t ret = re_syntax_options;
  248. re_syntax_options = syntax;
  249. return ret;
  250. }
  251. #ifdef _LIBC
  252. weak_alias (__re_set_syntax, re_set_syntax)
  253. #endif
  254. int
  255. re_compile_fastmap (bufp)
  256. struct re_pattern_buffer *bufp;
  257. {
  258. re_dfa_t *dfa = (re_dfa_t *) bufp->buffer;
  259. char *fastmap = bufp->fastmap;
  260. memset (fastmap, '\0', sizeof (char) * SBC_MAX);
  261. re_compile_fastmap_iter (bufp, dfa->init_state, fastmap);
  262. if (dfa->init_state != dfa->init_state_word)
  263. re_compile_fastmap_iter (bufp, dfa->init_state_word, fastmap);
  264. if (dfa->init_state != dfa->init_state_nl)
  265. re_compile_fastmap_iter (bufp, dfa->init_state_nl, fastmap);
  266. if (dfa->init_state != dfa->init_state_begbuf)
  267. re_compile_fastmap_iter (bufp, dfa->init_state_begbuf, fastmap);
  268. bufp->fastmap_accurate = 1;
  269. return 0;
  270. }
  271. #ifdef _LIBC
  272. weak_alias (__re_compile_fastmap, re_compile_fastmap)
  273. #endif
  274. static inline void
  275. re_set_fastmap (char *fastmap, int icase, int ch)
  276. {
  277. fastmap[ch] = 1;
  278. if (icase)
  279. fastmap[tolower (ch)] = 1;
  280. }
  281. /* Helper function for re_compile_fastmap.
  282. Compile fastmap for the initial_state INIT_STATE. */
  283. static void
  284. re_compile_fastmap_iter (bufp, init_state, fastmap)
  285. regex_t *bufp;
  286. const re_dfastate_t *init_state;
  287. char *fastmap;
  288. {
  289. re_dfa_t *dfa = (re_dfa_t *) bufp->buffer;
  290. int node_cnt;
  291. int icase = (MB_CUR_MAX == 1 && (bufp->syntax & RE_ICASE));
  292. for (node_cnt = 0; node_cnt < init_state->nodes.nelem; ++node_cnt)
  293. {
  294. int node = init_state->nodes.elems[node_cnt];
  295. re_token_type_t type = dfa->nodes[node].type;
  296. if (type == CHARACTER)
  297. re_set_fastmap (fastmap, icase, dfa->nodes[node].opr.c);
  298. else if (type == SIMPLE_BRACKET)
  299. {
  300. int i, j, ch;
  301. for (i = 0, ch = 0; i < BITSET_UINTS; ++i)
  302. for (j = 0; j < UINT_BITS; ++j, ++ch)
  303. if (dfa->nodes[node].opr.sbcset[i] & (1 << j))
  304. re_set_fastmap (fastmap, icase, ch);
  305. }
  306. #ifdef RE_ENABLE_I18N
  307. else if (type == COMPLEX_BRACKET)
  308. {
  309. int i;
  310. re_charset_t *cset = dfa->nodes[node].opr.mbcset;
  311. if (cset->non_match || cset->ncoll_syms || cset->nequiv_classes
  312. || cset->nranges || cset->nchar_classes)
  313. {
  314. # ifdef _LIBC
  315. if (_NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES) != 0)
  316. {
  317. /* In this case we want to catch the bytes which are
  318. the first byte of any collation elements.
  319. e.g. In da_DK, we want to catch 'a' since "aa"
  320. is a valid collation element, and don't catch
  321. 'b' since 'b' is the only collation element
  322. which starts from 'b'. */
  323. int j, ch;
  324. const int32_t *table = (const int32_t *)
  325. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
  326. for (i = 0, ch = 0; i < BITSET_UINTS; ++i)
  327. for (j = 0; j < UINT_BITS; ++j, ++ch)
  328. if (table[ch] < 0)
  329. re_set_fastmap (fastmap, icase, ch);
  330. }
  331. # else
  332. if (MB_CUR_MAX > 1)
  333. for (i = 0; i < SBC_MAX; ++i)
  334. if (__btowc (i) == WEOF)
  335. re_set_fastmap (fastmap, icase, i);
  336. # endif /* not _LIBC */
  337. }
  338. for (i = 0; i < cset->nmbchars; ++i)
  339. {
  340. char buf[256];
  341. mbstate_t state;
  342. memset (&state, '\0', sizeof (state));
  343. __wcrtomb (buf, cset->mbchars[i], &state);
  344. re_set_fastmap (fastmap, icase, *(unsigned char *) buf);
  345. }
  346. }
  347. #endif /* RE_ENABLE_I18N */
  348. else if (type == END_OF_RE || type == OP_PERIOD)
  349. {
  350. memset (fastmap, '\1', sizeof (char) * SBC_MAX);
  351. if (type == END_OF_RE)
  352. bufp->can_be_null = 1;
  353. return;
  354. }
  355. }
  356. }
  357. /* Entry point for POSIX code. */
  358. /* regcomp takes a regular expression as a string and compiles it.
  359. PREG is a regex_t *. We do not expect any fields to be initialized,
  360. since POSIX says we shouldn't. Thus, we set
  361. `buffer' to the compiled pattern;
  362. `used' to the length of the compiled pattern;
  363. `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
  364. REG_EXTENDED bit in CFLAGS is set; otherwise, to
  365. RE_SYNTAX_POSIX_BASIC;
  366. `newline_anchor' to REG_NEWLINE being set in CFLAGS;
  367. `fastmap' to an allocated space for the fastmap;
  368. `fastmap_accurate' to zero;
  369. `re_nsub' to the number of subexpressions in PATTERN.
  370. PATTERN is the address of the pattern string.
  371. CFLAGS is a series of bits which affect compilation.
  372. If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
  373. use POSIX basic syntax.
  374. If REG_NEWLINE is set, then . and [^...] don't match newline.
  375. Also, regexec will try a match beginning after every newline.
  376. If REG_ICASE is set, then we considers upper- and lowercase
  377. versions of letters to be equivalent when matching.
  378. If REG_NOSUB is set, then when PREG is passed to regexec, that
  379. routine will report only success or failure, and nothing about the
  380. registers.
  381. It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
  382. the return codes and their meanings.) */
  383. int
  384. regcomp (preg, pattern, cflags)
  385. regex_t *__restrict preg;
  386. const char *__restrict pattern;
  387. int cflags;
  388. {
  389. reg_errcode_t ret;
  390. reg_syntax_t syntax = ((cflags & REG_EXTENDED) ? RE_SYNTAX_POSIX_EXTENDED
  391. : RE_SYNTAX_POSIX_BASIC);
  392. preg->buffer = NULL;
  393. preg->allocated = 0;
  394. preg->used = 0;
  395. /* Try to allocate space for the fastmap. */
  396. preg->fastmap = re_malloc (char, SBC_MAX);
  397. if (BE (preg->fastmap == NULL, 0))
  398. return REG_ESPACE;
  399. syntax |= (cflags & REG_ICASE) ? RE_ICASE : 0;
  400. /* If REG_NEWLINE is set, newlines are treated differently. */
  401. if (cflags & REG_NEWLINE)
  402. { /* REG_NEWLINE implies neither . nor [^...] match newline. */
  403. syntax &= ~RE_DOT_NEWLINE;
  404. syntax |= RE_HAT_LISTS_NOT_NEWLINE;
  405. /* It also changes the matching behavior. */
  406. preg->newline_anchor = 1;
  407. }
  408. else
  409. preg->newline_anchor = 0;
  410. preg->no_sub = !!(cflags & REG_NOSUB);
  411. preg->translate = NULL;
  412. ret = re_compile_internal (preg, pattern, strlen (pattern), syntax);
  413. /* POSIX doesn't distinguish between an unmatched open-group and an
  414. unmatched close-group: both are REG_EPAREN. */
  415. if (ret == REG_ERPAREN)
  416. ret = REG_EPAREN;
  417. /* We have already checked preg->fastmap != NULL. */
  418. if (BE (ret == REG_NOERROR, 1))
  419. /* Compute the fastmap now, since regexec cannot modify the pattern
  420. buffer. This function nevers fails in this implementation. */
  421. (void) re_compile_fastmap (preg);
  422. else
  423. {
  424. /* Some error occurred while compiling the expression. */
  425. re_free (preg->fastmap);
  426. preg->fastmap = NULL;
  427. }
  428. return (int) ret;
  429. }
  430. #ifdef _LIBC
  431. weak_alias (__regcomp, regcomp)
  432. #endif
  433. /* Returns a message corresponding to an error code, ERRCODE, returned
  434. from either regcomp or regexec. We don't use PREG here. */
  435. size_t regerror (int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
  436. {
  437. const char *msg;
  438. size_t msg_size;
  439. if (BE (errcode < 0
  440. || errcode >= (int) (sizeof (__re_error_msgid_idx)
  441. / sizeof (__re_error_msgid_idx[0])), 0))
  442. /* Only error codes returned by the rest of the code should be passed
  443. to this routine. If we are given anything else, or if other regex
  444. code generates an invalid error code, then the program has a bug.
  445. Dump core so we can fix it. */
  446. abort ();
  447. msg = gettext (__re_error_msgid + __re_error_msgid_idx[errcode]);
  448. msg_size = strlen (msg) + 1; /* Includes the null. */
  449. if (BE (errbuf_size != 0, 1))
  450. {
  451. if (BE (msg_size > errbuf_size, 0))
  452. {
  453. #if defined HAVE_MEMPCPY || defined _LIBC
  454. *((char *) __mempcpy (errbuf, msg, errbuf_size - 1)) = '\0';
  455. #else
  456. memcpy (errbuf, msg, errbuf_size - 1);
  457. errbuf[errbuf_size - 1] = 0;
  458. #endif
  459. }
  460. else
  461. memcpy (errbuf, msg, msg_size);
  462. }
  463. return msg_size;
  464. }
  465. #ifdef _LIBC
  466. weak_alias (__regerror, regerror)
  467. #endif
  468. static void
  469. free_dfa_content (re_dfa_t *dfa)
  470. {
  471. int i, j;
  472. re_free (dfa->subexps);
  473. for (i = 0; i < dfa->nodes_len; ++i)
  474. {
  475. re_token_t *node = dfa->nodes + i;
  476. #ifdef RE_ENABLE_I18N
  477. if (node->type == COMPLEX_BRACKET && node->duplicated == 0)
  478. free_charset (node->opr.mbcset);
  479. else
  480. #endif /* RE_ENABLE_I18N */
  481. if (node->type == SIMPLE_BRACKET && node->duplicated == 0)
  482. re_free (node->opr.sbcset);
  483. }
  484. re_free (dfa->nexts);
  485. for (i = 0; i < dfa->nodes_len; ++i)
  486. {
  487. if (dfa->eclosures != NULL)
  488. re_node_set_free (dfa->eclosures + i);
  489. if (dfa->inveclosures != NULL)
  490. re_node_set_free (dfa->inveclosures + i);
  491. if (dfa->edests != NULL)
  492. re_node_set_free (dfa->edests + i);
  493. }
  494. re_free (dfa->edests);
  495. re_free (dfa->eclosures);
  496. re_free (dfa->inveclosures);
  497. re_free (dfa->nodes);
  498. for (i = 0; i <= dfa->state_hash_mask; ++i)
  499. {
  500. struct re_state_table_entry *entry = dfa->state_table + i;
  501. for (j = 0; j < entry->num; ++j)
  502. {
  503. re_dfastate_t *state = entry->array[j];
  504. free_state (state);
  505. }
  506. re_free (entry->array);
  507. }
  508. re_free (dfa->state_table);
  509. if (dfa->word_char != NULL)
  510. re_free (dfa->word_char);
  511. #ifdef DEBUG
  512. re_free (dfa->re_str);
  513. #endif
  514. re_free (dfa);
  515. }
  516. /* Free dynamically allocated space used by PREG. */
  517. void
  518. regfree (preg)
  519. regex_t *preg;
  520. {
  521. re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
  522. if (BE (dfa != NULL, 1))
  523. free_dfa_content (dfa);
  524. re_free (preg->fastmap);
  525. }
  526. #ifdef _LIBC
  527. weak_alias (__regfree, regfree)
  528. #endif
  529. /* Entry points compatible with 4.2 BSD regex library. We don't define
  530. them unless specifically requested. */
  531. #if defined _REGEX_RE_COMP || defined _LIBC
  532. /* BSD has one and only one pattern buffer. */
  533. static struct re_pattern_buffer re_comp_buf;
  534. char *
  535. # ifdef _LIBC
  536. /* Make these definitions weak in libc, so POSIX programs can redefine
  537. these names if they don't use our functions, and still use
  538. regcomp/regexec above without link errors. */
  539. weak_function
  540. # endif
  541. re_comp (s)
  542. const char *s;
  543. {
  544. reg_errcode_t ret;
  545. char *fastmap;
  546. if (!s)
  547. {
  548. if (!re_comp_buf.buffer)
  549. return gettext ("No previous regular expression");
  550. return 0;
  551. }
  552. if (re_comp_buf.buffer)
  553. {
  554. fastmap = re_comp_buf.fastmap;
  555. re_comp_buf.fastmap = NULL;
  556. __regfree (&re_comp_buf);
  557. memset (&re_comp_buf, '\0', sizeof (re_comp_buf));
  558. re_comp_buf.fastmap = fastmap;
  559. }
  560. if (re_comp_buf.fastmap == NULL)
  561. {
  562. re_comp_buf.fastmap = (char *) malloc (SBC_MAX);
  563. if (re_comp_buf.fastmap == NULL)
  564. return (char *) gettext (__re_error_msgid
  565. + __re_error_msgid_idx[(int) REG_ESPACE]);
  566. }
  567. /* Since `re_exec' always passes NULL for the `regs' argument, we
  568. don't need to initialize the pattern buffer fields which affect it. */
  569. /* Match anchors at newlines. */
  570. re_comp_buf.newline_anchor = 1;
  571. ret = re_compile_internal (&re_comp_buf, s, strlen (s), re_syntax_options);
  572. if (!ret)
  573. return NULL;
  574. /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
  575. return (char *) gettext (__re_error_msgid + __re_error_msgid_idx[(int) ret]);
  576. }
  577. #ifdef _LIBC
  578. libc_freeres_fn (free_mem)
  579. {
  580. __regfree (&re_comp_buf);
  581. }
  582. #endif
  583. #endif /* _REGEX_RE_COMP */
  584. /* Internal entry point.
  585. Compile the regular expression PATTERN, whose length is LENGTH.
  586. SYNTAX indicate regular expression's syntax. */
  587. static reg_errcode_t
  588. re_compile_internal (preg, pattern, length, syntax)
  589. regex_t *preg;
  590. const char * pattern;
  591. int length;
  592. reg_syntax_t syntax;
  593. {
  594. reg_errcode_t err = REG_NOERROR;
  595. re_dfa_t *dfa;
  596. re_string_t regexp;
  597. /* Initialize the pattern buffer. */
  598. preg->fastmap_accurate = 0;
  599. preg->syntax = syntax;
  600. preg->not_bol = preg->not_eol = 0;
  601. preg->used = 0;
  602. preg->re_nsub = 0;
  603. preg->can_be_null = 0;
  604. preg->regs_allocated = REGS_UNALLOCATED;
  605. /* Initialize the dfa. */
  606. dfa = (re_dfa_t *) preg->buffer;
  607. if (preg->allocated < sizeof (re_dfa_t))
  608. {
  609. /* If zero allocated, but buffer is non-null, try to realloc
  610. enough space. This loses if buffer's address is bogus, but
  611. that is the user's responsibility. If ->buffer is NULL this
  612. is a simple allocation. */
  613. dfa = re_realloc (preg->buffer, re_dfa_t, 1);
  614. if (dfa == NULL)
  615. return REG_ESPACE;
  616. preg->allocated = sizeof (re_dfa_t);
  617. }
  618. preg->buffer = (unsigned char *) dfa;
  619. preg->used = sizeof (re_dfa_t);
  620. err = init_dfa (dfa, length);
  621. if (BE (err != REG_NOERROR, 0))
  622. {
  623. re_free (dfa);
  624. preg->buffer = NULL;
  625. preg->allocated = 0;
  626. return err;
  627. }
  628. #ifdef DEBUG
  629. dfa->re_str = re_malloc (char, length + 1);
  630. strncpy (dfa->re_str, pattern, length + 1);
  631. #endif
  632. err = re_string_construct (&regexp, pattern, length, preg->translate,
  633. syntax & RE_ICASE);
  634. if (BE (err != REG_NOERROR, 0))
  635. {
  636. re_free (dfa);
  637. preg->buffer = NULL;
  638. preg->allocated = 0;
  639. return err;
  640. }
  641. /* Parse the regular expression, and build a structure tree. */
  642. preg->re_nsub = 0;
  643. dfa->str_tree = parse (&regexp, preg, syntax, &err);
  644. if (BE (dfa->str_tree == NULL, 0))
  645. goto re_compile_internal_free_return;
  646. /* Analyze the tree and collect information which is necessary to
  647. create the dfa. */
  648. err = analyze (dfa);
  649. if (BE (err != REG_NOERROR, 0))
  650. goto re_compile_internal_free_return;
  651. /* Then create the initial state of the dfa. */
  652. err = create_initial_state (dfa);
  653. /* Release work areas. */
  654. free_workarea_compile (preg);
  655. re_string_destruct (&regexp);
  656. if (BE (err != REG_NOERROR, 0))
  657. {
  658. re_compile_internal_free_return:
  659. free_dfa_content (dfa);
  660. preg->buffer = NULL;
  661. preg->allocated = 0;
  662. }
  663. return err;
  664. }
  665. /* Initialize DFA. We use the length of the regular expression PAT_LEN
  666. as the initial length of some arrays. */
  667. static reg_errcode_t
  668. init_dfa (dfa, pat_len)
  669. re_dfa_t *dfa;
  670. int pat_len;
  671. {
  672. int table_size;
  673. memset (dfa, '\0', sizeof (re_dfa_t));
  674. dfa->nodes_alloc = pat_len + 1;
  675. dfa->nodes = re_malloc (re_token_t, dfa->nodes_alloc);
  676. dfa->states_alloc = pat_len + 1;
  677. /* table_size = 2 ^ ceil(log pat_len) */
  678. for (table_size = 1; table_size > 0; table_size <<= 1)
  679. if (table_size > pat_len)
  680. break;
  681. dfa->state_table = calloc (sizeof (struct re_state_table_entry), table_size);
  682. dfa->state_hash_mask = table_size - 1;
  683. dfa->subexps_alloc = 1;
  684. dfa->subexps = re_malloc (re_subexp_t, dfa->subexps_alloc);
  685. dfa->word_char = NULL;
  686. if (BE (dfa->nodes == NULL || dfa->state_table == NULL
  687. || dfa->subexps == NULL, 0))
  688. {
  689. /* We don't bother to free anything which was allocated. Very
  690. soon the process will go down anyway. */
  691. dfa->subexps = NULL;
  692. dfa->state_table = NULL;
  693. dfa->nodes = NULL;
  694. return REG_ESPACE;
  695. }
  696. return REG_NOERROR;
  697. }
  698. /* Initialize WORD_CHAR table, which indicate which character is
  699. "word". In this case "word" means that it is the word construction
  700. character used by some operators like "\<", "\>", etc. */
  701. static reg_errcode_t
  702. init_word_char (dfa)
  703. re_dfa_t *dfa;
  704. {
  705. int i, j, ch;
  706. dfa->word_char = (re_bitset_ptr_t) calloc (sizeof (bitset), 1);
  707. if (BE (dfa->word_char == NULL, 0))
  708. return REG_ESPACE;
  709. for (i = 0, ch = 0; i < BITSET_UINTS; ++i)
  710. for (j = 0; j < UINT_BITS; ++j, ++ch)
  711. if (isalnum (ch) || ch == '_')
  712. dfa->word_char[i] |= 1 << j;
  713. return REG_NOERROR;
  714. }
  715. /* Free the work area which are only used while compiling. */
  716. static void
  717. free_workarea_compile (preg)
  718. regex_t *preg;
  719. {
  720. re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
  721. free_bin_tree (dfa->str_tree);
  722. dfa->str_tree = NULL;
  723. re_free (dfa->org_indices);
  724. dfa->org_indices = NULL;
  725. }
  726. /* Create initial states for all contexts. */
  727. static reg_errcode_t
  728. create_initial_state (dfa)
  729. re_dfa_t *dfa;
  730. {
  731. int first, i;
  732. reg_errcode_t err;
  733. re_node_set init_nodes;
  734. /* Initial states have the epsilon closure of the node which is
  735. the first node of the regular expression. */
  736. first = dfa->str_tree->first;
  737. dfa->init_node = first;
  738. err = re_node_set_init_copy (&init_nodes, dfa->eclosures + first);
  739. if (BE (err != REG_NOERROR, 0))
  740. return err;
  741. /* The back-references which are in initial states can epsilon transit,
  742. since in this case all of the subexpressions can be null.
  743. Then we add epsilon closures of the nodes which are the next nodes of
  744. the back-references. */
  745. if (dfa->nbackref > 0)
  746. for (i = 0; i < init_nodes.nelem; ++i)
  747. {
  748. int node_idx = init_nodes.elems[i];
  749. re_token_type_t type = dfa->nodes[node_idx].type;
  750. int clexp_idx;
  751. if (type != OP_BACK_REF)
  752. continue;
  753. for (clexp_idx = 0; clexp_idx < init_nodes.nelem; ++clexp_idx)
  754. {
  755. re_token_t *clexp_node;
  756. clexp_node = dfa->nodes + init_nodes.elems[clexp_idx];
  757. if (clexp_node->type == OP_CLOSE_SUBEXP
  758. && clexp_node->opr.idx + 1 == dfa->nodes[node_idx].opr.idx)
  759. break;
  760. }
  761. if (clexp_idx == init_nodes.nelem)
  762. continue;
  763. if (type == OP_BACK_REF)
  764. {
  765. int dest_idx = dfa->edests[node_idx].elems[0];
  766. if (!re_node_set_contains (&init_nodes, dest_idx))
  767. {
  768. re_node_set_merge (&init_nodes, dfa->eclosures + dest_idx);
  769. i = 0;
  770. }
  771. }
  772. }
  773. /* It must be the first time to invoke acquire_state. */
  774. dfa->init_state = re_acquire_state_context (&err, dfa, &init_nodes, 0);
  775. /* We don't check ERR here, since the initial state must not be NULL. */
  776. if (BE (dfa->init_state == NULL, 0))
  777. return err;
  778. if (dfa->init_state->has_constraint)
  779. {
  780. dfa->init_state_word = re_acquire_state_context (&err, dfa, &init_nodes,
  781. CONTEXT_WORD);
  782. dfa->init_state_nl = re_acquire_state_context (&err, dfa, &init_nodes,
  783. CONTEXT_NEWLINE);
  784. dfa->init_state_begbuf = re_acquire_state_context (&err, dfa,
  785. &init_nodes,
  786. CONTEXT_NEWLINE
  787. | CONTEXT_BEGBUF);
  788. if (BE (dfa->init_state_word == NULL || dfa->init_state_nl == NULL
  789. || dfa->init_state_begbuf == NULL, 0))
  790. return err;
  791. }
  792. else
  793. dfa->init_state_word = dfa->init_state_nl
  794. = dfa->init_state_begbuf = dfa->init_state;
  795. re_node_set_free (&init_nodes);
  796. return REG_NOERROR;
  797. }
  798. /* Analyze the structure tree, and calculate "first", "next", "edest",
  799. "eclosure", and "inveclosure". */
  800. static reg_errcode_t
  801. analyze (dfa)
  802. re_dfa_t *dfa;
  803. {
  804. int i;
  805. reg_errcode_t ret;
  806. /* Allocate arrays. */
  807. dfa->nexts = re_malloc (int, dfa->nodes_alloc);
  808. dfa->org_indices = re_malloc (int, dfa->nodes_alloc);
  809. dfa->edests = re_malloc (re_node_set, dfa->nodes_alloc);
  810. dfa->eclosures = re_malloc (re_node_set, dfa->nodes_alloc);
  811. dfa->inveclosures = re_malloc (re_node_set, dfa->nodes_alloc);
  812. if (BE (dfa->nexts == NULL || dfa->org_indices == NULL || dfa->edests == NULL
  813. || dfa->eclosures == NULL || dfa->inveclosures == NULL, 0))
  814. return REG_ESPACE;
  815. /* Initialize them. */
  816. for (i = 0; i < dfa->nodes_len; ++i)
  817. {
  818. dfa->nexts[i] = -1;
  819. re_node_set_init_empty (dfa->edests + i);
  820. re_node_set_init_empty (dfa->eclosures + i);
  821. re_node_set_init_empty (dfa->inveclosures + i);
  822. }
  823. ret = analyze_tree (dfa, dfa->str_tree);
  824. if (BE (ret == REG_NOERROR, 1))
  825. {
  826. ret = calc_eclosure (dfa);
  827. if (ret == REG_NOERROR)
  828. calc_inveclosure (dfa);
  829. }
  830. return ret;
  831. }
  832. /* Helper functions for analyze.
  833. This function calculate "first", "next", and "edest" for the subtree
  834. whose root is NODE. */
  835. static reg_errcode_t
  836. analyze_tree (dfa, node)
  837. re_dfa_t *dfa;
  838. bin_tree_t *node;
  839. {
  840. reg_errcode_t ret;
  841. if (node->first == -1)
  842. calc_first (dfa, node);
  843. if (node->next == -1)
  844. calc_next (dfa, node);
  845. if (node->eclosure.nelem == 0)
  846. calc_epsdest (dfa, node);
  847. /* Calculate "first" etc. for the left child. */
  848. if (node->left != NULL)
  849. {
  850. ret = analyze_tree (dfa, node->left);
  851. if (BE (ret != REG_NOERROR, 0))
  852. return ret;
  853. }
  854. /* Calculate "first" etc. for the right child. */
  855. if (node->right != NULL)
  856. {
  857. ret = analyze_tree (dfa, node->right);
  858. if (BE (ret != REG_NOERROR, 0))
  859. return ret;
  860. }
  861. return REG_NOERROR;
  862. }
  863. /* Calculate "first" for the node NODE. */
  864. static void
  865. calc_first (dfa, node)
  866. re_dfa_t *dfa;
  867. bin_tree_t *node;
  868. {
  869. int idx, type;
  870. idx = node->node_idx;
  871. type = (node->type == 0) ? dfa->nodes[idx].type : node->type;
  872. switch (type)
  873. {
  874. #ifdef DEBUG
  875. case OP_OPEN_BRACKET:
  876. case OP_CLOSE_BRACKET:
  877. case OP_OPEN_DUP_NUM:
  878. case OP_CLOSE_DUP_NUM:
  879. case OP_NON_MATCH_LIST:
  880. case OP_OPEN_COLL_ELEM:
  881. case OP_CLOSE_COLL_ELEM:
  882. case OP_OPEN_EQUIV_CLASS:
  883. case OP_CLOSE_EQUIV_CLASS:
  884. case OP_OPEN_CHAR_CLASS:
  885. case OP_CLOSE_CHAR_CLASS:
  886. /* These must not be appeared here. */
  887. assert (0);
  888. #endif
  889. case END_OF_RE:
  890. case CHARACTER:
  891. case OP_PERIOD:
  892. case OP_DUP_ASTERISK:
  893. case OP_DUP_QUESTION:
  894. #ifdef RE_ENABLE_I18N
  895. case COMPLEX_BRACKET:
  896. #endif /* RE_ENABLE_I18N */
  897. case SIMPLE_BRACKET:
  898. case OP_BACK_REF:
  899. case ANCHOR:
  900. case OP_OPEN_SUBEXP:
  901. case OP_CLOSE_SUBEXP:
  902. node->first = idx;
  903. break;
  904. case OP_DUP_PLUS:
  905. #ifdef DEBUG
  906. assert (node->left != NULL);
  907. #endif
  908. if (node->left->first == -1)
  909. calc_first (dfa, node->left);
  910. node->first = node->left->first;
  911. break;
  912. case OP_ALT:
  913. node->first = idx;
  914. break;
  915. /* else fall through */
  916. default:
  917. #ifdef DEBUG
  918. assert (node->left != NULL);
  919. #endif
  920. if (node->left->first == -1)
  921. calc_first (dfa, node->left);
  922. node->first = node->left->first;
  923. break;
  924. }
  925. }
  926. /* Calculate "next" for the node NODE. */
  927. static void
  928. calc_next (dfa, node)
  929. re_dfa_t *dfa;
  930. bin_tree_t *node;
  931. {
  932. int idx, type;
  933. bin_tree_t *parent = node->parent;
  934. if (parent == NULL)
  935. {
  936. node->next = -1;
  937. idx = node->node_idx;
  938. if (node->type == 0)
  939. dfa->nexts[idx] = node->next;
  940. return;
  941. }
  942. idx = parent->node_idx;
  943. type = (parent->type == 0) ? dfa->nodes[idx].type : parent->type;
  944. switch (type)
  945. {
  946. case OP_DUP_ASTERISK:
  947. case OP_DUP_PLUS:
  948. node->next = idx;
  949. break;
  950. case CONCAT:
  951. if (parent->left == node)
  952. {
  953. if (parent->right->first == -1)
  954. calc_first (dfa, parent->right);
  955. node->next = parent->right->first;
  956. break;
  957. }
  958. /* else fall through */
  959. default:
  960. if (parent->next == -1)
  961. calc_next (dfa, parent);
  962. node->next = parent->next;
  963. break;
  964. }
  965. idx = node->node_idx;
  966. if (node->type == 0)
  967. dfa->nexts[idx] = node->next;
  968. }
  969. /* Calculate "edest" for the node NODE. */
  970. static void
  971. calc_epsdest (dfa, node)
  972. re_dfa_t *dfa;
  973. bin_tree_t *node;
  974. {
  975. int idx;
  976. idx = node->node_idx;
  977. if (node->type == 0)
  978. {
  979. if (dfa->nodes[idx].type == OP_DUP_ASTERISK
  980. || dfa->nodes[idx].type == OP_DUP_PLUS
  981. || dfa->nodes[idx].type == OP_DUP_QUESTION)
  982. {
  983. if (node->left->first == -1)
  984. calc_first (dfa, node->left);
  985. if (node->next == -1)
  986. calc_next (dfa, node);
  987. re_node_set_init_2 (dfa->edests + idx, node->left->first,
  988. node->next);
  989. }
  990. else if (dfa->nodes[idx].type == OP_ALT)
  991. {
  992. int left, right;
  993. if (node->left != NULL)
  994. {
  995. if (node->left->first == -1)
  996. calc_first (dfa, node->left);
  997. left = node->left->first;
  998. }
  999. else
  1000. {
  1001. if (node->next == -1)
  1002. calc_next (dfa, node);
  1003. left = node->next;
  1004. }
  1005. if (node->right != NULL)
  1006. {
  1007. if (node->right->first == -1)
  1008. calc_first (dfa, node->right);
  1009. right = node->right->first;
  1010. }
  1011. else
  1012. {
  1013. if (node->next == -1)
  1014. calc_next (dfa, node);
  1015. right = node->next;
  1016. }
  1017. re_node_set_init_2 (dfa->edests + idx, left, right);
  1018. }
  1019. else if (dfa->nodes[idx].type == ANCHOR
  1020. || dfa->nodes[idx].type == OP_OPEN_SUBEXP
  1021. || dfa->nodes[idx].type == OP_CLOSE_SUBEXP
  1022. || dfa->nodes[idx].type == OP_BACK_REF)
  1023. re_node_set_init_1 (dfa->edests + idx, node->next);
  1024. }
  1025. }
  1026. /* Duplicate the epsilon closure of the node ROOT_NODE.
  1027. Note that duplicated nodes have constraint INIT_CONSTRAINT in addition
  1028. to their own constraint. */
  1029. static reg_errcode_t
  1030. duplicate_node_closure (dfa, top_org_node, top_clone_node, root_node,
  1031. init_constraint)
  1032. re_dfa_t *dfa;
  1033. int top_org_node, top_clone_node, root_node;
  1034. unsigned int init_constraint;
  1035. {
  1036. reg_errcode_t err;
  1037. int org_node, clone_node, ret;
  1038. unsigned int constraint = init_constraint;
  1039. for (org_node = top_org_node, clone_node = top_clone_node;;)
  1040. {
  1041. int org_dest, clone_dest;
  1042. if (dfa->nodes[org_node].type == OP_BACK_REF)
  1043. {
  1044. /* If the back reference epsilon-transit, its destination must
  1045. also have the constraint. Then duplicate the epsilon closure
  1046. of the destination of the back reference, and store it in
  1047. edests of the back reference. */
  1048. org_dest = dfa->nexts[org_node];
  1049. re_node_set_empty (dfa->edests + clone_node);
  1050. err = duplicate_node (&clone_dest, dfa, org_dest, constraint);
  1051. if (BE (err != REG_NOERROR, 0))
  1052. return err;
  1053. dfa->nexts[clone_node] = dfa->nexts[org_node];
  1054. ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
  1055. if (BE (ret < 0, 0))
  1056. return REG_ESPACE;
  1057. }
  1058. else if (dfa->edests[org_node].nelem == 0)
  1059. {
  1060. /* In case of the node can't epsilon-transit, don't duplicate the
  1061. destination and store the original destination as the
  1062. destination of the node. */
  1063. dfa->nexts[clone_node] = dfa->nexts[org_node];
  1064. break;
  1065. }
  1066. else if (dfa->edests[org_node].nelem == 1)
  1067. {
  1068. /* In case of the node can epsilon-transit, and it has only one
  1069. destination. */
  1070. org_dest = dfa->edests[org_node].elems[0];
  1071. re_node_set_empty (dfa->edests + clone_node);
  1072. if (dfa->nodes[org_node].type == ANCHOR)
  1073. {
  1074. /* In case of the node has another constraint, append it. */
  1075. if (org_node == root_node && clone_node != org_node)
  1076. {
  1077. /* ...but if the node is root_node itself, it means the
  1078. epsilon closure have a loop, then tie it to the
  1079. destination of the root_node. */
  1080. ret = re_node_set_insert (dfa->edests + clone_node,
  1081. org_dest);
  1082. if (BE (ret < 0, 0))
  1083. return REG_ESPACE;
  1084. break;
  1085. }
  1086. constraint |= dfa->nodes[org_node].opr.ctx_type;
  1087. }
  1088. err = duplicate_node (&clone_dest, dfa, org_dest, constraint);
  1089. if (BE (err != REG_NOERROR, 0))
  1090. return err;
  1091. ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
  1092. if (BE (ret < 0, 0))
  1093. return REG_ESPACE;
  1094. }
  1095. else /* dfa->edests[org_node].nelem == 2 */
  1096. {
  1097. /* In case of the node can epsilon-transit, and it has two
  1098. destinations. E.g. '|', '*', '+', '?'. */
  1099. org_dest = dfa->edests[org_node].elems[0];
  1100. re_node_set_empty (dfa->edests + clone_node);
  1101. /* Search for a duplicated node which satisfies the constraint. */
  1102. clone_dest = search_duplicated_node (dfa, org_dest, constraint);
  1103. if (clone_dest == -1)
  1104. {
  1105. /* There are no such a duplicated node, create a new one. */
  1106. err = duplicate_node (&clone_dest, dfa, org_dest, constraint);
  1107. if (BE (err != REG_NOERROR, 0))
  1108. return err;
  1109. ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
  1110. if (BE (ret < 0, 0))
  1111. return REG_ESPACE;
  1112. err = duplicate_node_closure (dfa, org_dest, clone_dest,
  1113. root_node, constraint);
  1114. if (BE (err != REG_NOERROR, 0))
  1115. return err;
  1116. }
  1117. else
  1118. {
  1119. /* There are a duplicated node which satisfy the constraint,
  1120. use it to avoid infinite loop. */
  1121. ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
  1122. if (BE (ret < 0, 0))
  1123. return REG_ESPACE;
  1124. }
  1125. org_dest = dfa->edests[org_node].elems[1];
  1126. err = duplicate_node (&clone_dest, dfa, org_dest, constraint);
  1127. if (BE (err != REG_NOERROR, 0))
  1128. return err;
  1129. ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
  1130. if (BE (ret < 0, 0))
  1131. return REG_ESPACE;
  1132. }
  1133. org_node = org_dest;
  1134. clone_node = clone_dest;
  1135. }
  1136. return REG_NOERROR;
  1137. }
  1138. /* Search for a node which is duplicated from the node ORG_NODE, and
  1139. satisfies the constraint CONSTRAINT. */
  1140. static int
  1141. search_duplicated_node (dfa, org_node, constraint)
  1142. re_dfa_t *dfa;
  1143. int org_node;
  1144. unsigned int constraint;
  1145. {
  1146. int idx;
  1147. for (idx = dfa->nodes_len - 1; dfa->nodes[idx].duplicated && idx > 0; --idx)
  1148. {
  1149. if (org_node == dfa->org_indices[idx]
  1150. && constraint == dfa->nodes[idx].constraint)
  1151. return idx; /* Found. */
  1152. }
  1153. return -1; /* Not found. */
  1154. }
  1155. /* Duplicate the node whose index is ORG_IDX and set the constraint CONSTRAINT.
  1156. The new index will be stored in NEW_IDX and return REG_NOERROR if succeeded,
  1157. otherwise return the error code. */
  1158. static reg_errcode_t
  1159. duplicate_node (new_idx, dfa, org_idx, constraint)
  1160. re_dfa_t *dfa;
  1161. int *new_idx, org_idx;
  1162. unsigned int constraint;
  1163. {
  1164. re_token_t dup;
  1165. int dup_idx;
  1166. dup = dfa->nodes[org_idx];
  1167. dup_idx = re_dfa_add_node (dfa, dup, 1);
  1168. if (BE (dup_idx == -1, 0))
  1169. return REG_ESPACE;
  1170. dfa->nodes[dup_idx].constraint = constraint;
  1171. if (dfa->nodes[org_idx].type == ANCHOR)
  1172. dfa->nodes[dup_idx].constraint |= dfa->nodes[org_idx].opr.ctx_type;
  1173. dfa->nodes[dup_idx].duplicated = 1;
  1174. re_node_set_init_empty (dfa->edests + dup_idx);
  1175. re_node_set_init_empty (dfa->eclosures + dup_idx);
  1176. re_node_set_init_empty (dfa->inveclosures + dup_idx);
  1177. /* Store the index of the original node. */
  1178. dfa->org_indices[dup_idx] = org_idx;
  1179. *new_idx = dup_idx;
  1180. return REG_NOERROR;
  1181. }
  1182. static void
  1183. calc_inveclosure (dfa)
  1184. re_dfa_t *dfa;
  1185. {
  1186. int src, idx, dest;
  1187. for (src = 0; src < dfa->nodes_len; ++src)
  1188. {
  1189. for (idx = 0; idx < dfa->eclosures[src].nelem; ++idx)
  1190. {
  1191. dest = dfa->eclosures[src].elems[idx];
  1192. re_node_set_insert (dfa->inveclosures + dest, src);
  1193. }
  1194. }
  1195. }
  1196. /* Calculate "eclosure" for all the node in DFA. */
  1197. static reg_errcode_t
  1198. calc_eclosure (dfa)
  1199. re_dfa_t *dfa;
  1200. {
  1201. int node_idx, incomplete;
  1202. #ifdef DEBUG
  1203. assert (dfa->nodes_len > 0);
  1204. #endif
  1205. incomplete = 0;
  1206. /* For each nodes, calculate epsilon closure. */
  1207. for (node_idx = 0; ; ++node_idx)
  1208. {
  1209. reg_errcode_t err;
  1210. re_node_set eclosure_elem;
  1211. if (node_idx == dfa->nodes_len)
  1212. {
  1213. if (!incomplete)
  1214. break;
  1215. incomplete = 0;
  1216. node_idx = 0;
  1217. }
  1218. #ifdef DEBUG
  1219. assert (dfa->eclosures[node_idx].nelem != -1);
  1220. #endif
  1221. /* If we have already calculated, skip it. */
  1222. if (dfa->eclosures[node_idx].nelem != 0)
  1223. continue;
  1224. /* Calculate epsilon closure of `node_idx'. */
  1225. err = calc_eclosure_iter (&eclosure_elem, dfa, node_idx, 1);
  1226. if (BE (err != REG_NOERROR, 0))
  1227. return err;
  1228. if (dfa->eclosures[node_idx].nelem == 0)
  1229. {
  1230. incomplete = 1;
  1231. re_node_set_free (&eclosure_elem);
  1232. }
  1233. }
  1234. return REG_NOERROR;
  1235. }
  1236. /* Calculate epsilon closure of NODE. */
  1237. static reg_errcode_t
  1238. calc_eclosure_iter (new_set, dfa, node, root)
  1239. re_node_set *new_set;
  1240. re_dfa_t *dfa;
  1241. int node, root;
  1242. {
  1243. reg_errcode_t err;
  1244. unsigned int constraint;
  1245. int i, incomplete;
  1246. re_node_set eclosure;
  1247. incomplete = 0;
  1248. err = re_node_set_alloc (&eclosure, dfa->edests[node].nelem + 1);
  1249. if (BE (err != REG_NOERROR, 0))
  1250. return err;
  1251. /* This indicates that we are calculating this node now.
  1252. We reference this value to avoid infinite loop. */
  1253. dfa->eclosures[node].nelem = -1;
  1254. constraint = ((dfa->nodes[node].type == ANCHOR)
  1255. ? dfa->nodes[node].opr.ctx_type : 0);
  1256. /* If the current node has constraints, duplicate all nodes.
  1257. Since they must inherit the constraints. */
  1258. if (constraint && !dfa->nodes[dfa->edests[node].elems[0]].duplicated)
  1259. {
  1260. int org_node, cur_node;
  1261. org_node = cur_node = node;
  1262. err = duplicate_node_closure (dfa, node, node, node, constraint);
  1263. if (BE (err != REG_NOERROR, 0))
  1264. return err;
  1265. }
  1266. /* Expand each epsilon destination nodes. */
  1267. if (IS_EPSILON_NODE(dfa->nodes[node].type))
  1268. for (i = 0; i < dfa->edests[node].nelem; ++i)
  1269. {
  1270. re_node_set eclosure_elem;
  1271. int edest = dfa->edests[node].elems[i];
  1272. /* If calculating the epsilon closure of `edest' is in progress,
  1273. return intermediate result. */
  1274. if (dfa->eclosures[edest].nelem == -1)
  1275. {
  1276. incomplete = 1;
  1277. continue;
  1278. }
  1279. /* If we haven't calculated the epsilon closure of `edest' yet,
  1280. calculate now. Otherwise use calculated epsilon closure. */
  1281. if (dfa->eclosures[edest].nelem == 0)
  1282. {
  1283. err = calc_eclosure_iter (&eclosure_elem, dfa, edest, 0);
  1284. if (BE (err != REG_NOERROR, 0))
  1285. return err;
  1286. }
  1287. else
  1288. eclosure_elem = dfa->eclosures[edest];
  1289. /* Merge the epsilon closure of `edest'. */
  1290. re_node_set_merge (&eclosure, &eclosure_elem);
  1291. /* If the epsilon closure of `edest' is incomplete,
  1292. the epsilon closure of this node is also incomplete. */
  1293. if (dfa->eclosures[edest].nelem == 0)
  1294. {
  1295. incomplete = 1;
  1296. re_node_set_free (&eclosure_elem);
  1297. }
  1298. }
  1299. /* Epsilon closures include itself. */
  1300. re_node_set_insert (&eclosure, node);
  1301. if (incomplete && !root)
  1302. dfa->eclosures[node].nelem = 0;
  1303. else
  1304. dfa->eclosures[node] = eclosure;
  1305. *new_set = eclosure;
  1306. return REG_NOERROR;
  1307. }
  1308. /* Functions for token which are used in the parser. */
  1309. /* Fetch a token from INPUT.
  1310. We must not use this function inside bracket expressions. */
  1311. static re_token_t
  1312. fetch_token (input, syntax)
  1313. re_string_t *input;
  1314. reg_syntax_t syntax;
  1315. {
  1316. re_token_t token;
  1317. int consumed_byte;
  1318. consumed_byte = peek_token (&token, input, syntax);
  1319. re_string_skip_bytes (input, consumed_byte);
  1320. return token;
  1321. }
  1322. /* Peek a token from INPUT, and return the length of the token.
  1323. We must not use this function inside bracket expressions. */
  1324. static int
  1325. peek_token (token, input, syntax)
  1326. re_token_t *token;
  1327. re_string_t *input;
  1328. reg_syntax_t syntax;
  1329. {
  1330. unsigned char c;
  1331. if (re_string_eoi (input))
  1332. {
  1333. token->type = END_OF_RE;
  1334. return 0;
  1335. }
  1336. c = re_string_peek_byte (input, 0);
  1337. token->opr.c = c;
  1338. #ifdef RE_ENABLE_I18N
  1339. token->mb_partial = 0;
  1340. if (MB_CUR_MAX > 1 &&
  1341. !re_string_first_byte (input, re_string_cur_idx (input)))
  1342. {
  1343. token->type = CHARACTER;
  1344. token->mb_partial = 1;
  1345. return 1;
  1346. }
  1347. #endif
  1348. if (c == '\\')
  1349. {
  1350. unsigned char c2;
  1351. if (re_string_cur_idx (input) + 1 >= re_string_length (input))
  1352. {
  1353. token->type = BACK_SLASH;
  1354. return 1;
  1355. }
  1356. c2 = re_string_peek_byte_case (input, 1);
  1357. token->opr.c = c2;
  1358. token->type = CHARACTER;
  1359. switch (c2)
  1360. {
  1361. case '|':
  1362. if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_NO_BK_VBAR))
  1363. token->type = OP_ALT;
  1364. break;
  1365. case '1': case '2': case '3': case '4': case '5':
  1366. case '6': case '7': case '8': case '9':
  1367. if (!(syntax & RE_NO_BK_REFS))
  1368. {
  1369. token->type = OP_BACK_REF;
  1370. token->opr.idx = c2 - '0';
  1371. }
  1372. break;
  1373. case '<':
  1374. if (!(syntax & RE_NO_GNU_OPS))
  1375. {
  1376. token->type = ANCHOR;
  1377. token->opr.idx = WORD_FIRST;
  1378. }
  1379. break;
  1380. case '>':
  1381. if (!(syntax & RE_NO_GNU_OPS))
  1382. {
  1383. token->type = ANCHOR;
  1384. token->opr.idx = WORD_LAST;
  1385. }
  1386. break;
  1387. case 'b':
  1388. if (!(syntax & RE_NO_GNU_OPS))
  1389. {
  1390. token->type = ANCHOR;
  1391. token->opr.idx = WORD_DELIM;
  1392. }
  1393. break;
  1394. case 'B':
  1395. if (!(syntax & RE_NO_GNU_OPS))
  1396. {
  1397. token->type = ANCHOR;
  1398. token->opr.idx = INSIDE_WORD;
  1399. }
  1400. break;
  1401. case 'w':
  1402. if (!(syntax & RE_NO_GNU_OPS))
  1403. token->type = OP_WORD;
  1404. break;
  1405. case 'W':
  1406. if (!(syntax & RE_NO_GNU_OPS))
  1407. token->type = OP_NOTWORD;
  1408. break;
  1409. case '`':
  1410. if (!(syntax & RE_NO_GNU_OPS))
  1411. {
  1412. token->type = ANCHOR;
  1413. token->opr.idx = BUF_FIRST;
  1414. }
  1415. break;
  1416. case '\'':
  1417. if (!(syntax & RE_NO_GNU_OPS))
  1418. {
  1419. token->type = ANCHOR;
  1420. token->opr.idx = BUF_LAST;
  1421. }
  1422. break;
  1423. case '(':
  1424. if (!(syntax & RE_NO_BK_PARENS))
  1425. token->type = OP_OPEN_SUBEXP;
  1426. break;
  1427. case ')':
  1428. if (!(syntax & RE_NO_BK_PARENS))
  1429. token->type = OP_CLOSE_SUBEXP;
  1430. break;
  1431. case '+':
  1432. if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_BK_PLUS_QM))
  1433. token->type = OP_DUP_PLUS;
  1434. break;
  1435. case '?':
  1436. if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_BK_PLUS_QM))
  1437. token->type = OP_DUP_QUESTION;
  1438. break;
  1439. case '{':
  1440. if ((syntax & RE_INTERVALS) && (!(syntax & RE_NO_BK_BRACES)))
  1441. token->type = OP_OPEN_DUP_NUM;
  1442. break;
  1443. case '}':
  1444. if ((syntax & RE_INTERVALS) && (!(syntax & RE_NO_BK_BRACES)))
  1445. token->type = OP_CLOSE_DUP_NUM;
  1446. break;
  1447. default:
  1448. break;
  1449. }
  1450. return 2;
  1451. }
  1452. token->type = CHARACTER;
  1453. switch (c)
  1454. {
  1455. case '\n':
  1456. if (syntax & RE_NEWLINE_ALT)
  1457. token->type = OP_ALT;
  1458. break;
  1459. case '|':
  1460. if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_NO_BK_VBAR))
  1461. token->type = OP_ALT;
  1462. break;
  1463. case '*':
  1464. token->type = OP_DUP_ASTERISK;
  1465. break;
  1466. case '+':
  1467. if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_BK_PLUS_QM))
  1468. token->type = OP_DUP_PLUS;
  1469. break;
  1470. case '?':
  1471. if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_BK_PLUS_QM))
  1472. token->type = OP_DUP_QUESTION;
  1473. break;
  1474. case '{':
  1475. if ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
  1476. token->type = OP_OPEN_DUP_NUM;
  1477. break;
  1478. case '}':
  1479. if ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
  1480. token->type = OP_CLOSE_DUP_NUM;
  1481. break;
  1482. case '(':
  1483. if (syntax & RE_NO_BK_PARENS)
  1484. token->type = OP_OPEN_SUBEXP;
  1485. break;
  1486. case ')':
  1487. if (syntax & RE_NO_BK_PARENS)
  1488. token->type = OP_CLOSE_SUBEXP;
  1489. break;
  1490. case '[':
  1491. token->type = OP_OPEN_BRACKET;
  1492. break;
  1493. case '.':
  1494. token->type = OP_PERIOD;
  1495. break;
  1496. case '^':
  1497. if (!(syntax & RE_CONTEXT_INDEP_ANCHORS) &&
  1498. re_string_cur_idx (input) != 0)
  1499. {
  1500. char prev = re_string_peek_byte (input, -1);
  1501. if (prev != '|' && prev != '(' &&
  1502. (!(syntax & RE_NEWLINE_ALT) || prev != '\n'))
  1503. break;
  1504. }
  1505. token->type = ANCHOR;
  1506. token->opr.idx = LINE_FIRST;
  1507. break;
  1508. case '$':
  1509. if (!(syntax & RE_CONTEXT_INDEP_ANCHORS) &&
  1510. re_string_cur_idx (input) + 1 != re_string_length (input))
  1511. {
  1512. re_token_t next;
  1513. re_string_skip_bytes (input, 1);
  1514. peek_token (&next, input, syntax);
  1515. re_string_skip_bytes (input, -1);
  1516. if (next.type != OP_ALT && next.type != OP_CLOSE_SUBEXP)
  1517. break;
  1518. }
  1519. token->type = ANCHOR;
  1520. token->opr.idx = LINE_LAST;
  1521. break;
  1522. default:
  1523. break;
  1524. }
  1525. return 1;
  1526. }
  1527. /* Peek a token from INPUT, and return the length of the token.
  1528. We must not use this function out of bracket expressions. */
  1529. static int
  1530. peek_token_bracket (token, input, syntax)
  1531. re_token_t *token;
  1532. re_string_t *input;
  1533. reg_syntax_t syntax;
  1534. {
  1535. unsigned char c;
  1536. if (re_string_eoi (input))
  1537. {
  1538. token->type = END_OF_RE;
  1539. return 0;
  1540. }
  1541. c = re_string_peek_byte (input, 0);
  1542. token->opr.c = c;
  1543. #ifdef RE_ENABLE_I18N
  1544. if (MB_CUR_MAX > 1 &&
  1545. !re_string_first_byte (input, re_string_cur_idx (input)))
  1546. {
  1547. token->type = CHARACTER;
  1548. return 1;
  1549. }
  1550. #endif /* RE_ENABLE_I18N */
  1551. if (c == '\\' && (syntax & RE_BACKSLASH_ESCAPE_IN_LISTS))
  1552. {
  1553. /* In this case, '\' escape a character. */
  1554. unsigned char c2;
  1555. re_string_skip_bytes (input, 1);
  1556. c2 = re_string_peek_byte (input, 0);
  1557. token->opr.c = c2;
  1558. token->type = CHARACTER;
  1559. return 1;
  1560. }
  1561. if (c == '[') /* '[' is a special char in a bracket exps. */
  1562. {
  1563. unsigned char c2;
  1564. int token_len;
  1565. c2 = re_string_peek_byte (input, 1);
  1566. token->opr.c = c2;
  1567. token_len = 2;
  1568. switch (c2)
  1569. {
  1570. case '.':
  1571. token->type = OP_OPEN_COLL_ELEM;
  1572. break;
  1573. case '=':
  1574. token->type = OP_OPEN_EQUIV_CLASS;
  1575. break;
  1576. case ':':
  1577. if (syntax & RE_CHAR_CLASSES)
  1578. {
  1579. token->type = OP_OPEN_CHAR_CLASS;
  1580. break;
  1581. }
  1582. /* else fall through. */
  1583. default:
  1584. token->type = CHARACTER;
  1585. token->opr.c = c;
  1586. token_len = 1;
  1587. break;
  1588. }
  1589. return token_len;
  1590. }
  1591. switch (c)
  1592. {
  1593. case '-':
  1594. token->type = OP_CHARSET_RANGE;
  1595. break;
  1596. case ']':
  1597. token->type = OP_CLOSE_BRACKET;
  1598. break;
  1599. case '^':
  1600. token->type = OP_NON_MATCH_LIST;
  1601. break;
  1602. default:
  1603. token->type = CHARACTER;
  1604. }
  1605. return 1;
  1606. }
  1607. /* Functions for parser. */
  1608. /* Entry point of the parser.
  1609. Parse the regular expression REGEXP and return the structure tree.
  1610. If an error is occured, ERR is set by error code, and return NULL.
  1611. This function build the following tree, from regular expression <reg_exp>:
  1612. CAT
  1613. / \
  1614. / \
  1615. <reg_exp> EOR
  1616. CAT means concatenation.
  1617. EOR means end of regular expression. */
  1618. static bin_tree_t *
  1619. parse (regexp, preg, syntax, err)
  1620. re_string_t *regexp;
  1621. regex_t *preg;
  1622. reg_syntax_t syntax;
  1623. reg_errcode_t *err;
  1624. {
  1625. re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
  1626. bin_tree_t *tree, *eor, *root;
  1627. re_token_t current_token;
  1628. int new_idx;
  1629. current_token = fetch_token (regexp, syntax);
  1630. tree = parse_reg_exp (regexp, preg, &current_token, syntax, 0, err);
  1631. if (BE (*err != REG_NOERROR && tree == NULL, 0))
  1632. return NULL;
  1633. new_idx = re_dfa_add_node (dfa, current_token, 0);
  1634. eor = create_tree (NULL, NULL, 0, new_idx);
  1635. if (tree != NULL)
  1636. root = create_tree (tree, eor, CONCAT, 0);
  1637. else
  1638. root = eor;
  1639. if (BE (new_idx == -1 || eor == NULL || root == NULL, 0))
  1640. {
  1641. *err = REG_ESPACE;
  1642. return NULL;
  1643. }
  1644. return root;
  1645. }
  1646. /* This function build the following tree, from regular expression
  1647. <branch1>|<branch2>:
  1648. ALT
  1649. / \
  1650. / \
  1651. <branch1> <branch2>
  1652. ALT means alternative, which represents the operator `|'. */
  1653. static bin_tree_t *
  1654. parse_reg_exp (regexp, preg, token, syntax, nest, err)
  1655. re_string_t *regexp;
  1656. regex_t *preg;
  1657. re_token_t *token;
  1658. reg_syntax_t syntax;
  1659. int nest;
  1660. reg_errcode_t *err;
  1661. {
  1662. re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
  1663. bin_tree_t *tree, *branch = NULL;
  1664. int new_idx;
  1665. tree = parse_branch (regexp, preg, token, syntax, nest, err);
  1666. if (BE (*err != REG_NOERROR && tree == NULL, 0))
  1667. return NULL;
  1668. while (token->type == OP_ALT)
  1669. {
  1670. re_token_t alt_token = *token;
  1671. new_idx = re_dfa_add_node (dfa, alt_token, 0);
  1672. *token = fetch_token (regexp, syntax);
  1673. if (token->type != OP_ALT && token->type != END_OF_RE
  1674. && (nest == 0 || token->type != OP_CLOSE_SUBEXP))
  1675. {
  1676. branch = parse_branch (regexp, preg, token, syntax, nest, err);
  1677. if (BE (*err != REG_NOERROR && branch == NULL, 0))
  1678. {
  1679. free_bin_tree (tree);
  1680. return NULL;
  1681. }
  1682. }
  1683. else
  1684. branch = NULL;
  1685. tree = create_tree (tree, branch, 0, new_idx);
  1686. if (BE (new_idx == -1 || tree == NULL, 0))
  1687. {
  1688. *err = REG_ESPACE;
  1689. return NULL;
  1690. }
  1691. dfa->has_plural_match = 1;
  1692. }
  1693. return tree;
  1694. }
  1695. /* This function build the following tree, from regular expression
  1696. <exp1><exp2>:
  1697. CAT
  1698. / \
  1699. / \
  1700. <exp1> <exp2>
  1701. CAT means concatenation. */
  1702. static bin_tree_t *
  1703. parse_branch (regexp, preg, token, syntax, nest, err)
  1704. re_string_t *regexp;
  1705. regex_t *preg;
  1706. re_token_t *token;
  1707. reg_syntax_t syntax;
  1708. int nest;
  1709. reg_errcode_t *err;
  1710. {
  1711. bin_tree_t *tree, *exp;
  1712. tree = parse_expression (regexp, preg, token, syntax, nest, err);
  1713. if (BE (*err != REG_NOERROR && tree == NULL, 0))
  1714. return NULL;
  1715. while (token->type != OP_ALT && token->type != END_OF_RE
  1716. && (nest == 0 || token->type != OP_CLOSE_SUBEXP))
  1717. {
  1718. exp = parse_expression (regexp, preg, token, syntax, nest, err);
  1719. if (BE (*err != REG_NOERROR && exp == NULL, 0))
  1720. {
  1721. free_bin_tree (tree);
  1722. return NULL;
  1723. }
  1724. if (tree != NULL && exp != NULL)
  1725. {
  1726. tree = create_tree (tree, exp, CONCAT, 0);
  1727. if (tree == NULL)
  1728. {
  1729. *err = REG_ESPACE;
  1730. return NULL;
  1731. }
  1732. }
  1733. else if (tree == NULL)
  1734. tree = exp;
  1735. /* Otherwise exp == NULL, we don't need to create new tree. */
  1736. }
  1737. return tree;
  1738. }
  1739. /* This function build the following tree, from regular expression a*:
  1740. *
  1741. |
  1742. a
  1743. */
  1744. static bin_tree_t *
  1745. parse_expression (regexp, preg, token, syntax, nest, err)
  1746. re_string_t *regexp;
  1747. regex_t *preg;
  1748. re_token_t *token;
  1749. reg_syntax_t syntax;
  1750. int nest;
  1751. reg_errcode_t *err;
  1752. {
  1753. re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
  1754. bin_tree_t *tree;
  1755. int new_idx;
  1756. switch (token->type)
  1757. {
  1758. case CHARACTER:
  1759. new_idx = re_dfa_add_node (dfa, *token, 0);
  1760. tree = create_tree (NULL, NULL, 0, new_idx);
  1761. if (BE (new_idx == -1 || tree == NULL, 0))
  1762. {
  1763. *err = REG_ESPACE;
  1764. return NULL;
  1765. }
  1766. #ifdef RE_ENABLE_I18N
  1767. if (MB_CUR_MAX > 1)
  1768. {
  1769. while (!re_string_eoi (regexp)
  1770. && !re_string_first_byte (regexp, re_string_cur_idx (regexp)))
  1771. {
  1772. bin_tree_t *mbc_remain;
  1773. *token = fetch_token (regexp, syntax);
  1774. new_idx = re_dfa_add_node (dfa, *token, 0);
  1775. mbc_remain = create_tree (NULL, NULL, 0, new_idx);
  1776. tree = create_tree (tree, mbc_remain, CONCAT, 0);
  1777. if (BE (new_idx == -1 || mbc_remain == NULL || tree == NULL, 0))
  1778. {
  1779. *err = REG_ESPACE;
  1780. return NULL;
  1781. }
  1782. }
  1783. }
  1784. #endif
  1785. break;
  1786. case OP_OPEN_SUBEXP:
  1787. tree = parse_sub_exp (regexp, preg, token, syntax, nest + 1, err);
  1788. if (BE (*err != REG_NOERROR && tree == NULL, 0))
  1789. return NULL;
  1790. break;
  1791. case OP_OPEN_BRACKET:
  1792. tree = parse_bracket_exp (regexp, dfa, token, syntax, err);
  1793. if (BE (*err != REG_NOERROR && tree == NULL, 0))
  1794. return NULL;
  1795. break;
  1796. case OP_BACK_REF:
  1797. if (BE (preg->re_nsub < token->opr.idx
  1798. || dfa->subexps[token->opr.idx - 1].end == -1, 0))
  1799. {
  1800. *err = REG_ESUBREG;
  1801. return NULL;
  1802. }
  1803. dfa->used_bkref_map |= 1 << (token->opr.idx - 1);
  1804. new_idx = re_dfa_add_node (dfa, *token, 0);
  1805. tree = create_tree (NULL, NULL, 0, new_idx);
  1806. if (BE (new_idx == -1 || tree == NULL, 0))
  1807. {
  1808. *err = REG_ESPACE;
  1809. return NULL;
  1810. }
  1811. ++dfa->nbackref;
  1812. dfa->has_mb_node = 1;
  1813. break;
  1814. case OP_DUP_ASTERISK:
  1815. case OP_DUP_PLUS:
  1816. case OP_DUP_QUESTION:
  1817. case OP_OPEN_DUP_NUM:
  1818. if (syntax & RE_CONTEXT_INVALID_OPS)
  1819. {
  1820. *err = REG_BADRPT;
  1821. return NULL;
  1822. }
  1823. else if (syntax & RE_CONTEXT_INDEP_OPS)
  1824. {
  1825. *token = fetch_token (regexp, syntax);
  1826. return parse_expression (regexp, preg, token, syntax, nest, err);
  1827. }
  1828. /* else fall through */
  1829. case OP_CLOSE_SUBEXP:
  1830. if ((token->type == OP_CLOSE_SUBEXP) &&
  1831. !(syntax & RE_UNMATCHED_RIGHT_PAREN_ORD))
  1832. {
  1833. *err = REG_ERPAREN;
  1834. return NULL;
  1835. }
  1836. /* else fall through */
  1837. case OP_CLOSE_DUP_NUM:
  1838. /* We treat it as a normal character. */
  1839. /* Then we can these characters as normal characters. */
  1840. token->type = CHARACTER;
  1841. new_idx = re_dfa_add_node (dfa, *token, 0);
  1842. tree = create_tree (NULL, NULL, 0, new_idx);
  1843. if (BE (new_idx == -1 || tree == NULL, 0))
  1844. {
  1845. *err = REG_ESPACE;
  1846. return NULL;
  1847. }
  1848. break;
  1849. case ANCHOR:
  1850. if (dfa->word_char == NULL)
  1851. {
  1852. *err = init_word_char (dfa);
  1853. if (BE (*err != REG_NOERROR, 0))
  1854. return NULL;
  1855. }
  1856. if (token->opr.ctx_type == WORD_DELIM)
  1857. {
  1858. bin_tree_t *tree_first, *tree_last;
  1859. int idx_first, idx_last;
  1860. token->opr.ctx_type = WORD_FIRST;
  1861. idx_first = re_dfa_add_node (dfa, *token, 0);
  1862. tree_first = create_tree (NULL, NULL, 0, idx_first);
  1863. token->opr.ctx_type = WORD_LAST;
  1864. idx_last = re_dfa_add_node (dfa, *token, 0);
  1865. tree_last = create_tree (NULL, NULL, 0, idx_last);
  1866. token->type = OP_ALT;
  1867. new_idx = re_dfa_add_node (dfa, *token, 0);
  1868. tree = create_tree (tree_first, tree_last, 0, new_idx);
  1869. if (BE (idx_first == -1 || idx_last == -1 || new_idx == -1
  1870. || tree_first == NULL || tree_last == NULL
  1871. || tree == NULL, 0))
  1872. {
  1873. *err = REG_ESPACE;
  1874. return NULL;
  1875. }
  1876. }
  1877. else
  1878. {
  1879. new_idx = re_dfa_add_node (dfa, *token, 0);
  1880. tree = create_tree (NULL, NULL, 0, new_idx);
  1881. if (BE (new_idx == -1 || tree == NULL, 0))
  1882. {
  1883. *err = REG_ESPACE;
  1884. return NULL;
  1885. }
  1886. }
  1887. /* We must return here, since ANCHORs can't be followed
  1888. by repetition operators.
  1889. eg. RE"^*" is invalid or "<ANCHOR(^)><CHAR(*)>",
  1890. it must not be "<ANCHOR(^)><REPEAT(*)>". */
  1891. *token = fetch_token (regexp, syntax);
  1892. return tree;
  1893. case OP_PERIOD:
  1894. new_idx = re_dfa_add_node (dfa, *token, 0);
  1895. tree = create_tree (NULL, NULL, 0, new_idx);
  1896. if (BE (new_idx == -1 || tree == NULL, 0))
  1897. {
  1898. *err = REG_ESPACE;
  1899. return NULL;
  1900. }
  1901. if (MB_CUR_MAX > 1)
  1902. dfa->has_mb_node = 1;
  1903. break;
  1904. case OP_WORD:
  1905. tree = build_word_op (dfa, 0, err);
  1906. if (BE (*err != REG_NOERROR && tree == NULL, 0))
  1907. return NULL;
  1908. break;
  1909. case OP_NOTWORD:
  1910. tree = build_word_op (dfa, 1, err);
  1911. if (BE (*err != REG_NOERROR && tree == NULL, 0))
  1912. return NULL;
  1913. break;
  1914. case OP_ALT:
  1915. case END_OF_RE:
  1916. return NULL;
  1917. case BACK_SLASH:
  1918. *err = REG_EESCAPE;
  1919. return NULL;
  1920. default:
  1921. /* Must not happen? */
  1922. #ifdef DEBUG
  1923. assert (0);
  1924. #endif
  1925. return NULL;
  1926. }
  1927. *token = fetch_token (regexp, syntax);
  1928. while (token->type == OP_DUP_ASTERISK || token->type == OP_DUP_PLUS
  1929. || token->type == OP_DUP_QUESTION || token->type == OP_OPEN_DUP_NUM)
  1930. {
  1931. tree = parse_dup_op (tree, regexp, dfa, token, syntax, err);
  1932. if (BE (*err != REG_NOERROR && tree == NULL, 0))
  1933. return NULL;
  1934. dfa->has_plural_match = 1;
  1935. }
  1936. return tree;
  1937. }
  1938. /* This function build the following tree, from regular expression
  1939. (<reg_exp>):
  1940. SUBEXP
  1941. |
  1942. <reg_exp>
  1943. */
  1944. static bin_tree_t *
  1945. parse_sub_exp (regexp, preg, token, syntax, nest, err)
  1946. re_string_t *regexp;
  1947. regex_t *preg;
  1948. re_token_t *token;
  1949. reg_syntax_t syntax;
  1950. int nest;
  1951. reg_errcode_t *err;
  1952. {
  1953. re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
  1954. bin_tree_t *tree, *left_par, *right_par;
  1955. size_t cur_nsub;
  1956. int new_idx;
  1957. cur_nsub = preg->re_nsub++;
  1958. if (dfa->subexps_alloc < preg->re_nsub)
  1959. {
  1960. re_subexp_t *new_array;
  1961. dfa->subexps_alloc *= 2;
  1962. new_array = re_realloc (dfa->subexps, re_subexp_t, dfa->subexps_alloc);
  1963. if (BE (new_array == NULL, 0))
  1964. {
  1965. dfa->subexps_alloc /= 2;
  1966. *err = REG_ESPACE;
  1967. return NULL;
  1968. }
  1969. dfa->subexps = new_array;
  1970. }
  1971. dfa->subexps[cur_nsub].start = dfa->nodes_len;
  1972. dfa->subexps[cur_nsub].end = -1;
  1973. new_idx = re_dfa_add_node (dfa, *token, 0);
  1974. left_par = create_tree (NULL, NULL, 0, new_idx);
  1975. if (BE (new_idx == -1 || left_par == NULL, 0))
  1976. {
  1977. *err = REG_ESPACE;
  1978. return NULL;
  1979. }
  1980. dfa->nodes[new_idx].opr.idx = cur_nsub;
  1981. *token = fetch_token (regexp, syntax);
  1982. /* The subexpression may be a null string. */
  1983. if (token->type == OP_CLOSE_SUBEXP)
  1984. tree = NULL;
  1985. else
  1986. {
  1987. tree = parse_reg_exp (regexp, preg, token, syntax, nest, err);
  1988. if (BE (*err != REG_NOERROR && tree == NULL, 0))
  1989. return NULL;
  1990. }
  1991. if (BE (token->type != OP_CLOSE_SUBEXP, 0))
  1992. {
  1993. free_bin_tree (tree);
  1994. *err = REG_BADPAT;
  1995. return NULL;
  1996. }
  1997. new_idx = re_dfa_add_node (dfa, *token, 0);
  1998. dfa->subexps[cur_nsub].end = dfa->nodes_len;
  1999. right_par = create_tree (NULL, NULL, 0, new_idx);
  2000. tree = ((tree == NULL) ? right_par
  2001. : create_tree (tree, right_par, CONCAT, 0));
  2002. tree = create_tree (left_par, tree, CONCAT, 0);
  2003. if (BE (new_idx == -1 || right_par == NULL || tree == NULL, 0))
  2004. {
  2005. *err = REG_ESPACE;
  2006. return NULL;
  2007. }
  2008. dfa->nodes[new_idx].opr.idx = cur_nsub;
  2009. return tree;
  2010. }
  2011. /* This function parse repetition operators like "*", "+", "{1,3}" etc. */
  2012. static bin_tree_t *
  2013. parse_dup_op (dup_elem, regexp, dfa, token, syntax, err)
  2014. bin_tree_t *dup_elem;
  2015. re_string_t *regexp;
  2016. re_dfa_t *dfa;
  2017. re_token_t *token;
  2018. reg_syntax_t syntax;
  2019. reg_errcode_t *err;
  2020. {
  2021. re_token_t dup_token;
  2022. bin_tree_t *tree = dup_elem, *work_tree;
  2023. int new_idx, start_idx = re_string_cur_idx (regexp);
  2024. re_token_t start_token = *token;
  2025. if (token->type == OP_OPEN_DUP_NUM)
  2026. {
  2027. int i;
  2028. int end = 0;
  2029. int start = fetch_number (regexp, token, syntax);
  2030. bin_tree_t *elem;
  2031. if (start == -1)
  2032. {
  2033. if (token->type == CHARACTER && token->opr.c == ',')
  2034. start = 0; /* We treat "{,m}" as "{0,m}". */
  2035. else
  2036. {
  2037. *err = REG_BADBR; /* <re>{} is invalid. */
  2038. return NULL;
  2039. }
  2040. }
  2041. if (BE (start != -2, 1))
  2042. {
  2043. /* We treat "{n}" as "{n,n}". */
  2044. end = ((token->type == OP_CLOSE_DUP_NUM) ? start
  2045. : ((token->type == CHARACTER && token->opr.c == ',')
  2046. ? fetch_number (regexp, token, syntax) : -2));
  2047. }
  2048. if (BE (start == -2 || end == -2, 0))
  2049. {
  2050. /* Invalid sequence. */
  2051. if (token->type == OP_CLOSE_DUP_NUM)
  2052. goto parse_dup_op_invalid_interval;
  2053. else
  2054. goto parse_dup_op_ebrace;
  2055. }
  2056. if (BE (start == 0 && end == 0, 0))
  2057. {
  2058. /* We treat "<re>{0}" and "<re>{0,0}" as null string. */
  2059. *token = fetch_token (regexp, syntax);
  2060. free_bin_tree (dup_elem);
  2061. return NULL;
  2062. }
  2063. /* Extract "<re>{n,m}" to "<re><re>...<re><re>{0,<m-n>}". */
  2064. elem = tree;
  2065. for (i = 0; i < start; ++i)
  2066. if (i != 0)
  2067. {
  2068. work_tree = duplicate_tree (elem, dfa);
  2069. tree = create_tree (tree, work_tree, CONCAT, 0);
  2070. if (BE (work_tree == NULL || tree == NULL, 0))
  2071. goto parse_dup_op_espace;
  2072. }
  2073. if (end == -1)
  2074. {
  2075. /* We treat "<re>{0,}" as "<re>*". */
  2076. dup_token.type = OP_DUP_ASTERISK;
  2077. if (start > 0)
  2078. {
  2079. elem = duplicate_tree (elem, dfa);
  2080. new_idx = re_dfa_add_node (dfa, dup_token, 0);
  2081. work_tree = create_tree (elem, NULL, 0, new_idx);
  2082. tree = create_tree (tree, work_tree, CONCAT, 0);
  2083. if (BE (elem == NULL || new_idx == -1 || work_tree == NULL
  2084. || tree == NULL, 0))
  2085. goto parse_dup_op_espace;
  2086. }
  2087. else
  2088. {
  2089. new_idx = re_dfa_add_node (dfa, dup_token, 0);
  2090. tree = create_tree (elem, NULL, 0, new_idx);
  2091. if (BE (new_idx == -1 || tree == NULL, 0))
  2092. goto parse_dup_op_espace;
  2093. }
  2094. }
  2095. else if (end - start > 0)
  2096. {
  2097. /* Then extract "<re>{0,m}" to "<re>?<re>?...<re>?". */
  2098. dup_token.type = OP_DUP_QUESTION;
  2099. if (start > 0)
  2100. {
  2101. elem = duplicate_tree (elem, dfa);
  2102. new_idx = re_dfa_add_node (dfa, dup_token, 0);
  2103. elem = create_tree (elem, NULL, 0, new_idx);
  2104. tree = create_tree (tree, elem, CONCAT, 0);
  2105. if (BE (elem == NULL || new_idx == -1 || tree == NULL, 0))
  2106. goto parse_dup_op_espace;
  2107. }
  2108. else
  2109. {
  2110. new_idx = re_dfa_add_node (dfa, dup_token, 0);
  2111. tree = elem = create_tree (elem, NULL, 0, new_idx);
  2112. if (BE (new_idx == -1 || tree == NULL, 0))
  2113. goto parse_dup_op_espace;
  2114. }
  2115. for (i = 1; i < end - start; ++i)
  2116. {
  2117. work_tree = duplicate_tree (elem, dfa);
  2118. tree = create_tree (tree, work_tree, CONCAT, 0);
  2119. if (BE (work_tree == NULL || tree == NULL, 0))
  2120. {
  2121. *err = REG_ESPACE;
  2122. return NULL;
  2123. }
  2124. }
  2125. }
  2126. }
  2127. else
  2128. {
  2129. new_idx