PageRenderTime 63ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/CS/migrated/tags/PRE_CSARRAY_SIZE_T_INDICES/libs/csutil/generic/regcomp.c

#
C | 2319 lines | 1883 code | 161 blank | 275 comment | 600 complexity | 2d3208db64f839ba3659345bb4d680c0 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, LGPL-2.0

Large files files are truncated, but you can click here to view the full file

  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. #endif
  23. static reg_errcode_t re_compile_internal (regex_t *preg, const char * pattern,
  24. int length, reg_syntax_t syntax);
  25. static void re_compile_fastmap_iter (regex_t *bufp,
  26. const re_dfastate_t *init_state,
  27. char *fastmap);
  28. static reg_errcode_t init_dfa (re_dfa_t *dfa, int pat_len);
  29. static reg_errcode_t init_word_char (re_dfa_t *dfa);
  30. #ifdef RE_ENABLE_I18N
  31. static void free_charset (re_charset_t *cset);
  32. #endif /* RE_ENABLE_I18N */
  33. static void free_workarea_compile (regex_t *preg);
  34. static reg_errcode_t create_initial_state (re_dfa_t *dfa);
  35. static reg_errcode_t analyze (re_dfa_t *dfa);
  36. static reg_errcode_t analyze_tree (re_dfa_t *dfa, bin_tree_t *node);
  37. static void calc_first (re_dfa_t *dfa, bin_tree_t *node);
  38. static void calc_next (re_dfa_t *dfa, bin_tree_t *node);
  39. static void calc_epsdest (re_dfa_t *dfa, bin_tree_t *node);
  40. static reg_errcode_t duplicate_node_closure (re_dfa_t *dfa, int top_org_node,
  41. int top_clone_node, int root_node,
  42. unsigned int constraint);
  43. static reg_errcode_t duplicate_node (int *new_idx, re_dfa_t *dfa, int org_idx,
  44. unsigned int constraint);
  45. static int search_duplicated_node (re_dfa_t *dfa, int org_node,
  46. unsigned int constraint);
  47. static reg_errcode_t calc_eclosure (re_dfa_t *dfa);
  48. static reg_errcode_t calc_eclosure_iter (re_node_set *new_set, re_dfa_t *dfa,
  49. int node, int root);
  50. static void calc_inveclosure (re_dfa_t *dfa);
  51. static int fetch_number (re_string_t *input, re_token_t *token,
  52. reg_syntax_t syntax);
  53. static re_token_t fetch_token (re_string_t *input, reg_syntax_t syntax);
  54. static int peek_token (re_token_t *token, re_string_t *input,
  55. reg_syntax_t syntax);
  56. static int peek_token_bracket (re_token_t *token, re_string_t *input,
  57. reg_syntax_t syntax);
  58. static bin_tree_t *parse (re_string_t *regexp, regex_t *preg,
  59. reg_syntax_t syntax, reg_errcode_t *err);
  60. static bin_tree_t *parse_reg_exp (re_string_t *regexp, regex_t *preg,
  61. re_token_t *token, reg_syntax_t syntax,
  62. int nest, reg_errcode_t *err);
  63. static bin_tree_t *parse_branch (re_string_t *regexp, regex_t *preg,
  64. re_token_t *token, reg_syntax_t syntax,
  65. int nest, reg_errcode_t *err);
  66. static bin_tree_t *parse_expression (re_string_t *regexp, regex_t *preg,
  67. re_token_t *token, reg_syntax_t syntax,
  68. int nest, reg_errcode_t *err);
  69. static bin_tree_t *parse_sub_exp (re_string_t *regexp, regex_t *preg,
  70. re_token_t *token, reg_syntax_t syntax,
  71. int nest, reg_errcode_t *err);
  72. static bin_tree_t *parse_dup_op (bin_tree_t *dup_elem, re_string_t *regexp,
  73. re_dfa_t *dfa, re_token_t *token,
  74. reg_syntax_t syntax, reg_errcode_t *err);
  75. static bin_tree_t *parse_bracket_exp (re_string_t *regexp, re_dfa_t *dfa,
  76. re_token_t *token, reg_syntax_t syntax,
  77. reg_errcode_t *err);
  78. static reg_errcode_t parse_bracket_element (bracket_elem_t *elem,
  79. re_string_t *regexp,
  80. re_token_t *token, int token_len,
  81. re_dfa_t *dfa,
  82. reg_syntax_t syntax);
  83. static reg_errcode_t parse_bracket_symbol (bracket_elem_t *elem,
  84. re_string_t *regexp,
  85. re_token_t *token);
  86. #ifndef _LIBC
  87. # ifdef RE_ENABLE_I18N
  88. static reg_errcode_t build_range_exp (re_bitset_ptr_t sbcset,
  89. re_charset_t *mbcset, int *range_alloc,
  90. bracket_elem_t *start_elem,
  91. bracket_elem_t *end_elem);
  92. static reg_errcode_t build_collating_symbol (re_bitset_ptr_t sbcset,
  93. re_charset_t *mbcset,
  94. int *coll_sym_alloc,
  95. const unsigned char *name);
  96. # else /* not RE_ENABLE_I18N */
  97. static reg_errcode_t build_range_exp (re_bitset_ptr_t sbcset,
  98. bracket_elem_t *start_elem,
  99. bracket_elem_t *end_elem);
  100. static reg_errcode_t build_collating_symbol (re_bitset_ptr_t sbcset,
  101. const unsigned char *name);
  102. # endif /* not RE_ENABLE_I18N */
  103. #endif /* not _LIBC */
  104. #ifdef RE_ENABLE_I18N
  105. static reg_errcode_t build_equiv_class (re_bitset_ptr_t sbcset,
  106. re_charset_t *mbcset,
  107. int *equiv_class_alloc,
  108. const unsigned char *name);
  109. static reg_errcode_t build_charclass (re_bitset_ptr_t sbcset,
  110. re_charset_t *mbcset,
  111. int *char_class_alloc,
  112. const unsigned char *class_name,
  113. reg_syntax_t syntax);
  114. #else /* not RE_ENABLE_I18N */
  115. static reg_errcode_t build_equiv_class (re_bitset_ptr_t sbcset,
  116. const unsigned char *name);
  117. static reg_errcode_t build_charclass (re_bitset_ptr_t sbcset,
  118. const unsigned char *class_name,
  119. reg_syntax_t syntax);
  120. #endif /* not RE_ENABLE_I18N */
  121. static bin_tree_t *build_word_op (re_dfa_t *dfa, int not, reg_errcode_t *err);
  122. static void free_bin_tree (bin_tree_t *tree);
  123. static bin_tree_t *create_tree (bin_tree_t *left, bin_tree_t *right,
  124. re_token_type_t type, int index);
  125. static bin_tree_t *duplicate_tree (const bin_tree_t *src, re_dfa_t *dfa);
  126. /* This table gives an error message for each of the error codes listed
  127. in regex.h. Obviously the order here has to be same as there.
  128. POSIX doesn't require that we do anything for REG_NOERROR,
  129. but why not be nice? */
  130. const char __re_error_msgid[] attribute_hidden =
  131. {
  132. #define REG_NOERROR_IDX 0
  133. gettext_noop ("Success") /* REG_NOERROR */
  134. "\0"
  135. #define REG_NOMATCH_IDX (REG_NOERROR_IDX + sizeof "Success")
  136. gettext_noop ("No match") /* REG_NOMATCH */
  137. "\0"
  138. #define REG_BADPAT_IDX (REG_NOMATCH_IDX + sizeof "No match")
  139. gettext_noop ("Invalid regular expression") /* REG_BADPAT */
  140. "\0"
  141. #define REG_ECOLLATE_IDX (REG_BADPAT_IDX + sizeof "Invalid regular expression")
  142. gettext_noop ("Invalid collation character") /* REG_ECOLLATE */
  143. "\0"
  144. #define REG_ECTYPE_IDX (REG_ECOLLATE_IDX + sizeof "Invalid collation character")
  145. gettext_noop ("Invalid character class name") /* REG_ECTYPE */
  146. "\0"
  147. #define REG_EESCAPE_IDX (REG_ECTYPE_IDX + sizeof "Invalid character class name")
  148. gettext_noop ("Trailing backslash") /* REG_EESCAPE */
  149. "\0"
  150. #define REG_ESUBREG_IDX (REG_EESCAPE_IDX + sizeof "Trailing backslash")
  151. gettext_noop ("Invalid back reference") /* REG_ESUBREG */
  152. "\0"
  153. #define REG_EBRACK_IDX (REG_ESUBREG_IDX + sizeof "Invalid back reference")
  154. gettext_noop ("Unmatched [ or [^") /* REG_EBRACK */
  155. "\0"
  156. #define REG_EPAREN_IDX (REG_EBRACK_IDX + sizeof "Unmatched [ or [^")
  157. gettext_noop ("Unmatched ( or \\(") /* REG_EPAREN */
  158. "\0"
  159. #define REG_EBRACE_IDX (REG_EPAREN_IDX + sizeof "Unmatched ( or \\(")
  160. gettext_noop ("Unmatched \\{") /* REG_EBRACE */
  161. "\0"
  162. #define REG_BADBR_IDX (REG_EBRACE_IDX + sizeof "Unmatched \\{")
  163. gettext_noop ("Invalid content of \\{\\}") /* REG_BADBR */
  164. "\0"
  165. #define REG_ERANGE_IDX (REG_BADBR_IDX + sizeof "Invalid content of \\{\\}")
  166. gettext_noop ("Invalid range end") /* REG_ERANGE */
  167. "\0"
  168. #define REG_ESPACE_IDX (REG_ERANGE_IDX + sizeof "Invalid range end")
  169. gettext_noop ("Memory exhausted") /* REG_ESPACE */
  170. "\0"
  171. #define REG_BADRPT_IDX (REG_ESPACE_IDX + sizeof "Memory exhausted")
  172. gettext_noop ("Invalid preceding regular expression") /* REG_BADRPT */
  173. "\0"
  174. #define REG_EEND_IDX (REG_BADRPT_IDX + sizeof "Invalid preceding regular expression")
  175. gettext_noop ("Premature end of regular expression") /* REG_EEND */
  176. "\0"
  177. #define REG_ESIZE_IDX (REG_EEND_IDX + sizeof "Premature end of regular expression")
  178. gettext_noop ("Regular expression too big") /* REG_ESIZE */
  179. "\0"
  180. #define REG_ERPAREN_IDX (REG_ESIZE_IDX + sizeof "Regular expression too big")
  181. gettext_noop ("Unmatched ) or \\)") /* REG_ERPAREN */
  182. };
  183. const size_t __re_error_msgid_idx[] attribute_hidden =
  184. {
  185. REG_NOERROR_IDX,
  186. REG_NOMATCH_IDX,
  187. REG_BADPAT_IDX,
  188. REG_ECOLLATE_IDX,
  189. REG_ECTYPE_IDX,
  190. REG_EESCAPE_IDX,
  191. REG_ESUBREG_IDX,
  192. REG_EBRACK_IDX,
  193. REG_EPAREN_IDX,
  194. REG_EBRACE_IDX,
  195. REG_BADBR_IDX,
  196. REG_ERANGE_IDX,
  197. REG_ESPACE_IDX,
  198. REG_BADRPT_IDX,
  199. REG_EEND_IDX,
  200. REG_ESIZE_IDX,
  201. REG_ERPAREN_IDX
  202. };
  203. /* Entry points for GNU code. */
  204. /* re_compile_pattern is the GNU regular expression compiler: it
  205. compiles PATTERN (of length LENGTH) and puts the result in BUFP.
  206. Returns 0 if the pattern was valid, otherwise an error string.
  207. Assumes the `allocated' (and perhaps `buffer') and `translate' fields
  208. are set in BUFP on entry. */
  209. const char *
  210. re_compile_pattern (pattern, length, bufp)
  211. const char *pattern;
  212. size_t length;
  213. struct re_pattern_buffer *bufp;
  214. {
  215. reg_errcode_t ret;
  216. /* And GNU code determines whether or not to get register information
  217. by passing null for the REGS argument to re_match, etc., not by
  218. setting no_sub. */
  219. bufp->no_sub = 0;
  220. /* Match anchors at newline. */
  221. bufp->newline_anchor = 1;
  222. ret = re_compile_internal (bufp, pattern, length, re_syntax_options);
  223. if (!ret)
  224. return NULL;
  225. return gettext (__re_error_msgid + __re_error_msgid_idx[(int) ret]);
  226. }
  227. #ifdef _LIBC
  228. weak_alias (__re_compile_pattern, re_compile_pattern)
  229. #endif
  230. /* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
  231. also be assigned to arbitrarily: each pattern buffer stores its own
  232. syntax, so it can be changed between regex compilations. */
  233. /* This has no initializer because initialized variables in Emacs
  234. become read-only after dumping. */
  235. reg_syntax_t re_syntax_options;
  236. /* Specify the precise syntax of regexps for compilation. This provides
  237. for compatibility for various utilities which historically have
  238. different, incompatible syntaxes.
  239. The argument SYNTAX is a bit mask comprised of the various bits
  240. defined in regex.h. We return the old syntax. */
  241. reg_syntax_t
  242. re_set_syntax (syntax)
  243. reg_syntax_t syntax;
  244. {
  245. reg_syntax_t ret = re_syntax_options;
  246. re_syntax_options = syntax;
  247. return ret;
  248. }
  249. #ifdef _LIBC
  250. weak_alias (__re_set_syntax, re_set_syntax)
  251. #endif
  252. int
  253. re_compile_fastmap (bufp)
  254. struct re_pattern_buffer *bufp;
  255. {
  256. re_dfa_t *dfa = (re_dfa_t *) bufp->buffer;
  257. char *fastmap = bufp->fastmap;
  258. memset (fastmap, '\0', sizeof (char) * SBC_MAX);
  259. re_compile_fastmap_iter (bufp, dfa->init_state, fastmap);
  260. if (dfa->init_state != dfa->init_state_word)
  261. re_compile_fastmap_iter (bufp, dfa->init_state_word, fastmap);
  262. if (dfa->init_state != dfa->init_state_nl)
  263. re_compile_fastmap_iter (bufp, dfa->init_state_nl, fastmap);
  264. if (dfa->init_state != dfa->init_state_begbuf)
  265. re_compile_fastmap_iter (bufp, dfa->init_state_begbuf, fastmap);
  266. bufp->fastmap_accurate = 1;
  267. return 0;
  268. }
  269. #ifdef _LIBC
  270. weak_alias (__re_compile_fastmap, re_compile_fastmap)
  271. #endif
  272. static inline void
  273. re_set_fastmap (char *fastmap, int icase, int ch)
  274. {
  275. fastmap[ch] = 1;
  276. if (icase)
  277. fastmap[tolower (ch)] = 1;
  278. }
  279. /* Helper function for re_compile_fastmap.
  280. Compile fastmap for the initial_state INIT_STATE. */
  281. static void
  282. re_compile_fastmap_iter (bufp, init_state, fastmap)
  283. regex_t *bufp;
  284. const re_dfastate_t *init_state;
  285. char *fastmap;
  286. {
  287. re_dfa_t *dfa = (re_dfa_t *) bufp->buffer;
  288. int node_cnt;
  289. int icase = (MB_CUR_MAX == 1 && (bufp->syntax & RE_ICASE));
  290. for (node_cnt = 0; node_cnt < init_state->nodes.nelem; ++node_cnt)
  291. {
  292. int node = init_state->nodes.elems[node_cnt];
  293. re_token_type_t type = dfa->nodes[node].type;
  294. if (type == CHARACTER)
  295. re_set_fastmap (fastmap, icase, dfa->nodes[node].opr.c);
  296. else if (type == SIMPLE_BRACKET)
  297. {
  298. int i, j, ch;
  299. for (i = 0, ch = 0; i < BITSET_UINTS; ++i)
  300. for (j = 0; j < UINT_BITS; ++j, ++ch)
  301. if (dfa->nodes[node].opr.sbcset[i] & (1 << j))
  302. re_set_fastmap (fastmap, icase, ch);
  303. }
  304. #ifdef RE_ENABLE_I18N
  305. else if (type == COMPLEX_BRACKET)
  306. {
  307. int i;
  308. re_charset_t *cset = dfa->nodes[node].opr.mbcset;
  309. if (cset->non_match || cset->ncoll_syms || cset->nequiv_classes
  310. || cset->nranges || cset->nchar_classes)
  311. {
  312. # ifdef _LIBC
  313. if (_NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES) != 0)
  314. {
  315. /* In this case we want to catch the bytes which are
  316. the first byte of any collation elements.
  317. e.g. In da_DK, we want to catch 'a' since "aa"
  318. is a valid collation element, and don't catch
  319. 'b' since 'b' is the only collation element
  320. which starts from 'b'. */
  321. int j, ch;
  322. const int32_t *table = (const int32_t *)
  323. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
  324. for (i = 0, ch = 0; i < BITSET_UINTS; ++i)
  325. for (j = 0; j < UINT_BITS; ++j, ++ch)
  326. if (table[ch] < 0)
  327. re_set_fastmap (fastmap, icase, ch);
  328. }
  329. # else
  330. if (MB_CUR_MAX > 1)
  331. for (i = 0; i < SBC_MAX; ++i)
  332. if (__btowc (i) == WEOF)
  333. re_set_fastmap (fastmap, icase, i);
  334. # endif /* not _LIBC */
  335. }
  336. for (i = 0; i < cset->nmbchars; ++i)
  337. {
  338. char buf[256];
  339. mbstate_t state;
  340. memset (&state, '\0', sizeof (state));
  341. __wcrtomb (buf, cset->mbchars[i], &state);
  342. re_set_fastmap (fastmap, icase, *(unsigned char *) buf);
  343. }
  344. }
  345. #endif /* RE_ENABLE_I18N */
  346. else if (type == END_OF_RE || type == OP_PERIOD)
  347. {
  348. memset (fastmap, '\1', sizeof (char) * SBC_MAX);
  349. if (type == END_OF_RE)
  350. bufp->can_be_null = 1;
  351. return;
  352. }
  353. }
  354. }
  355. /* Entry point for POSIX code. */
  356. /* regcomp takes a regular expression as a string and compiles it.
  357. PREG is a regex_t *. We do not expect any fields to be initialized,
  358. since POSIX says we shouldn't. Thus, we set
  359. `buffer' to the compiled pattern;
  360. `used' to the length of the compiled pattern;
  361. `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
  362. REG_EXTENDED bit in CFLAGS is set; otherwise, to
  363. RE_SYNTAX_POSIX_BASIC;
  364. `newline_anchor' to REG_NEWLINE being set in CFLAGS;
  365. `fastmap' to an allocated space for the fastmap;
  366. `fastmap_accurate' to zero;
  367. `re_nsub' to the number of subexpressions in PATTERN.
  368. PATTERN is the address of the pattern string.
  369. CFLAGS is a series of bits which affect compilation.
  370. If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
  371. use POSIX basic syntax.
  372. If REG_NEWLINE is set, then . and [^...] don't match newline.
  373. Also, regexec will try a match beginning after every newline.
  374. If REG_ICASE is set, then we considers upper- and lowercase
  375. versions of letters to be equivalent when matching.
  376. If REG_NOSUB is set, then when PREG is passed to regexec, that
  377. routine will report only success or failure, and nothing about the
  378. registers.
  379. It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
  380. the return codes and their meanings.) */
  381. int
  382. regcomp (preg, pattern, cflags)
  383. regex_t *__restrict preg;
  384. const char *__restrict pattern;
  385. int cflags;
  386. {
  387. reg_errcode_t ret;
  388. reg_syntax_t syntax = ((cflags & REG_EXTENDED) ? RE_SYNTAX_POSIX_EXTENDED
  389. : RE_SYNTAX_POSIX_BASIC);
  390. preg->buffer = NULL;
  391. preg->allocated = 0;
  392. preg->used = 0;
  393. /* Try to allocate space for the fastmap. */
  394. preg->fastmap = re_malloc (char, SBC_MAX);
  395. if (BE (preg->fastmap == NULL, 0))
  396. return REG_ESPACE;
  397. syntax |= (cflags & REG_ICASE) ? RE_ICASE : 0;
  398. /* If REG_NEWLINE is set, newlines are treated differently. */
  399. if (cflags & REG_NEWLINE)
  400. { /* REG_NEWLINE implies neither . nor [^...] match newline. */
  401. syntax &= ~RE_DOT_NEWLINE;
  402. syntax |= RE_HAT_LISTS_NOT_NEWLINE;
  403. /* It also changes the matching behavior. */
  404. preg->newline_anchor = 1;
  405. }
  406. else
  407. preg->newline_anchor = 0;
  408. preg->no_sub = !!(cflags & REG_NOSUB);
  409. preg->translate = NULL;
  410. ret = re_compile_internal (preg, pattern, strlen (pattern), syntax);
  411. /* POSIX doesn't distinguish between an unmatched open-group and an
  412. unmatched close-group: both are REG_EPAREN. */
  413. if (ret == REG_ERPAREN)
  414. ret = REG_EPAREN;
  415. /* We have already checked preg->fastmap != NULL. */
  416. if (BE (ret == REG_NOERROR, 1))
  417. /* Compute the fastmap now, since regexec cannot modify the pattern
  418. buffer. This function nevers fails in this implementation. */
  419. (void) re_compile_fastmap (preg);
  420. else
  421. {
  422. /* Some error occurred while compiling the expression. */
  423. re_free (preg->fastmap);
  424. preg->fastmap = NULL;
  425. }
  426. return (int) ret;
  427. }
  428. #ifdef _LIBC
  429. weak_alias (__regcomp, regcomp)
  430. #endif
  431. /* Returns a message corresponding to an error code, ERRCODE, returned
  432. from either regcomp or regexec. We don't use PREG here. */
  433. size_t
  434. regerror (errcode, preg, errbuf, errbuf_size)
  435. int errcode;
  436. const regex_t *preg;
  437. char *errbuf;
  438. size_t errbuf_size;
  439. {
  440. const char *msg;
  441. size_t msg_size;
  442. if (BE (errcode < 0
  443. || errcode >= (int) (sizeof (__re_error_msgid_idx)
  444. / sizeof (__re_error_msgid_idx[0])), 0))
  445. /* Only error codes returned by the rest of the code should be passed
  446. to this routine. If we are given anything else, or if other regex
  447. code generates an invalid error code, then the program has a bug.
  448. Dump core so we can fix it. */
  449. abort ();
  450. msg = gettext (__re_error_msgid + __re_error_msgid_idx[errcode]);
  451. msg_size = strlen (msg) + 1; /* Includes the null. */
  452. if (BE (errbuf_size != 0, 1))
  453. {
  454. if (BE (msg_size > errbuf_size, 0))
  455. {
  456. #if defined HAVE_MEMPCPY || defined _LIBC
  457. *((char *) __mempcpy (errbuf, msg, errbuf_size - 1)) = '\0';
  458. #else
  459. memcpy (errbuf, msg, errbuf_size - 1);
  460. errbuf[errbuf_size - 1] = 0;
  461. #endif
  462. }
  463. else
  464. memcpy (errbuf, msg, msg_size);
  465. }
  466. return msg_size;
  467. }
  468. #ifdef _LIBC
  469. weak_alias (__regerror, regerror)
  470. #endif
  471. static void
  472. free_dfa_content (re_dfa_t *dfa)
  473. {
  474. int i, j;
  475. re_free (dfa->subexps);
  476. for (i = 0; i < dfa->nodes_len; ++i)
  477. {
  478. re_token_t *node = dfa->nodes + i;
  479. #ifdef RE_ENABLE_I18N
  480. if (node->type == COMPLEX_BRACKET && node->duplicated == 0)
  481. free_charset (node->opr.mbcset);
  482. else
  483. #endif /* RE_ENABLE_I18N */
  484. if (node->type == SIMPLE_BRACKET && node->duplicated == 0)
  485. re_free (node->opr.sbcset);
  486. }
  487. re_free (dfa->nexts);
  488. for (i = 0; i < dfa->nodes_len; ++i)
  489. {
  490. if (dfa->eclosures != NULL)
  491. re_node_set_free (dfa->eclosures + i);
  492. if (dfa->inveclosures != NULL)
  493. re_node_set_free (dfa->inveclosures + i);
  494. if (dfa->edests != NULL)
  495. re_node_set_free (dfa->edests + i);
  496. }
  497. re_free (dfa->edests);
  498. re_free (dfa->eclosures);
  499. re_free (dfa->inveclosures);
  500. re_free (dfa->nodes);
  501. for (i = 0; i <= dfa->state_hash_mask; ++i)
  502. {
  503. struct re_state_table_entry *entry = dfa->state_table + i;
  504. for (j = 0; j < entry->num; ++j)
  505. {
  506. re_dfastate_t *state = entry->array[j];
  507. free_state (state);
  508. }
  509. re_free (entry->array);
  510. }
  511. re_free (dfa->state_table);
  512. if (dfa->word_char != NULL)
  513. re_free (dfa->word_char);
  514. #ifdef DEBUG
  515. re_free (dfa->re_str);
  516. #endif
  517. re_free (dfa);
  518. }
  519. /* Free dynamically allocated space used by PREG. */
  520. void
  521. regfree (preg)
  522. regex_t *preg;
  523. {
  524. re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
  525. if (BE (dfa != NULL, 1))
  526. free_dfa_content (dfa);
  527. re_free (preg->fastmap);
  528. }
  529. #ifdef _LIBC
  530. weak_alias (__regfree, regfree)
  531. #endif
  532. /* Entry points compatible with 4.2 BSD regex library. We don't define
  533. them unless specifically requested. */
  534. #if defined _REGEX_RE_COMP || defined _LIBC
  535. /* BSD has one and only one pattern buffer. */
  536. static struct re_pattern_buffer re_comp_buf;
  537. char *
  538. # ifdef _LIBC
  539. /* Make these definitions weak in libc, so POSIX programs can redefine
  540. these names if they don't use our functions, and still use
  541. regcomp/regexec above without link errors. */
  542. weak_function
  543. # endif
  544. re_comp (s)
  545. const char *s;
  546. {
  547. reg_errcode_t ret;
  548. char *fastmap;
  549. if (!s)
  550. {
  551. if (!re_comp_buf.buffer)
  552. return gettext ("No previous regular expression");
  553. return 0;
  554. }
  555. if (re_comp_buf.buffer)
  556. {
  557. fastmap = re_comp_buf.fastmap;
  558. re_comp_buf.fastmap = NULL;
  559. __regfree (&re_comp_buf);
  560. memset (&re_comp_buf, '\0', sizeof (re_comp_buf));
  561. re_comp_buf.fastmap = fastmap;
  562. }
  563. if (re_comp_buf.fastmap == NULL)
  564. {
  565. re_comp_buf.fastmap = (char *) malloc (SBC_MAX);
  566. if (re_comp_buf.fastmap == NULL)
  567. return (char *) gettext (__re_error_msgid
  568. + __re_error_msgid_idx[(int) REG_ESPACE]);
  569. }
  570. /* Since `re_exec' always passes NULL for the `regs' argument, we
  571. don't need to initialize the pattern buffer fields which affect it. */
  572. /* Match anchors at newlines. */
  573. re_comp_buf.newline_anchor = 1;
  574. ret = re_compile_internal (&re_comp_buf, s, strlen (s), re_syntax_options);
  575. if (!ret)
  576. return NULL;
  577. /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
  578. return (char *) gettext (__re_error_msgid + __re_error_msgid_idx[(int) ret]);
  579. }
  580. #ifdef _LIBC
  581. libc_freeres_fn (free_mem)
  582. {
  583. __regfree (&re_comp_buf);
  584. }
  585. #endif
  586. #endif /* _REGEX_RE_COMP */
  587. /* Internal entry point.
  588. Compile the regular expression PATTERN, whose length is LENGTH.
  589. SYNTAX indicate regular expression's syntax. */
  590. static reg_errcode_t
  591. re_compile_internal (preg, pattern, length, syntax)
  592. regex_t *preg;
  593. const char * pattern;
  594. int length;
  595. reg_syntax_t syntax;
  596. {
  597. reg_errcode_t err = REG_NOERROR;
  598. re_dfa_t *dfa;
  599. re_string_t regexp;
  600. /* Initialize the pattern buffer. */
  601. preg->fastmap_accurate = 0;
  602. preg->syntax = syntax;
  603. preg->not_bol = preg->not_eol = 0;
  604. preg->used = 0;
  605. preg->re_nsub = 0;
  606. preg->can_be_null = 0;
  607. preg->regs_allocated = REGS_UNALLOCATED;
  608. /* Initialize the dfa. */
  609. dfa = (re_dfa_t *) preg->buffer;
  610. if (preg->allocated < sizeof (re_dfa_t))
  611. {
  612. /* If zero allocated, but buffer is non-null, try to realloc
  613. enough space. This loses if buffer's address is bogus, but
  614. that is the user's responsibility. If ->buffer is NULL this
  615. is a simple allocation. */
  616. dfa = re_realloc (preg->buffer, re_dfa_t, 1);
  617. if (dfa == NULL)
  618. return REG_ESPACE;
  619. preg->allocated = sizeof (re_dfa_t);
  620. }
  621. preg->buffer = (unsigned char *) dfa;
  622. preg->used = sizeof (re_dfa_t);
  623. err = init_dfa (dfa, length);
  624. if (BE (err != REG_NOERROR, 0))
  625. {
  626. re_free (dfa);
  627. preg->buffer = NULL;
  628. preg->allocated = 0;
  629. return err;
  630. }
  631. #ifdef DEBUG
  632. dfa->re_str = re_malloc (char, length + 1);
  633. strncpy (dfa->re_str, pattern, length + 1);
  634. #endif
  635. err = re_string_construct (&regexp, pattern, length, preg->translate,
  636. syntax & RE_ICASE);
  637. if (BE (err != REG_NOERROR, 0))
  638. {
  639. re_free (dfa);
  640. preg->buffer = NULL;
  641. preg->allocated = 0;
  642. return err;
  643. }
  644. /* Parse the regular expression, and build a structure tree. */
  645. preg->re_nsub = 0;
  646. dfa->str_tree = parse (&regexp, preg, syntax, &err);
  647. if (BE (dfa->str_tree == NULL, 0))
  648. goto re_compile_internal_free_return;
  649. /* Analyze the tree and collect information which is necessary to
  650. create the dfa. */
  651. err = analyze (dfa);
  652. if (BE (err != REG_NOERROR, 0))
  653. goto re_compile_internal_free_return;
  654. /* Then create the initial state of the dfa. */
  655. err = create_initial_state (dfa);
  656. /* Release work areas. */
  657. free_workarea_compile (preg);
  658. re_string_destruct (&regexp);
  659. if (BE (err != REG_NOERROR, 0))
  660. {
  661. re_compile_internal_free_return:
  662. free_dfa_content (dfa);
  663. preg->buffer = NULL;
  664. preg->allocated = 0;
  665. }
  666. return err;
  667. }
  668. /* Initialize DFA. We use the length of the regular expression PAT_LEN
  669. as the initial length of some arrays. */
  670. static reg_errcode_t
  671. init_dfa (dfa, pat_len)
  672. re_dfa_t *dfa;
  673. int pat_len;
  674. {
  675. int table_size;
  676. memset (dfa, '\0', sizeof (re_dfa_t));
  677. dfa->nodes_alloc = pat_len + 1;
  678. dfa->nodes = re_malloc (re_token_t, dfa->nodes_alloc);
  679. dfa->states_alloc = pat_len + 1;
  680. /* table_size = 2 ^ ceil(log pat_len) */
  681. for (table_size = 1; table_size > 0; table_size <<= 1)
  682. if (table_size > pat_len)
  683. break;
  684. dfa->state_table = calloc (sizeof (struct re_state_table_entry), table_size);
  685. dfa->state_hash_mask = table_size - 1;
  686. dfa->subexps_alloc = 1;
  687. dfa->subexps = re_malloc (re_subexp_t, dfa->subexps_alloc);
  688. dfa->word_char = NULL;
  689. if (BE (dfa->nodes == NULL || dfa->state_table == NULL
  690. || dfa->subexps == NULL, 0))
  691. {
  692. /* We don't bother to free anything which was allocated. Very
  693. soon the process will go down anyway. */
  694. dfa->subexps = NULL;
  695. dfa->state_table = NULL;
  696. dfa->nodes = NULL;
  697. return REG_ESPACE;
  698. }
  699. return REG_NOERROR;
  700. }
  701. /* Initialize WORD_CHAR table, which indicate which character is
  702. "word". In this case "word" means that it is the word construction
  703. character used by some operators like "\<", "\>", etc. */
  704. static reg_errcode_t
  705. init_word_char (dfa)
  706. re_dfa_t *dfa;
  707. {
  708. int i, j, ch;
  709. dfa->word_char = (re_bitset_ptr_t) calloc (sizeof (bitset), 1);
  710. if (BE (dfa->word_char == NULL, 0))
  711. return REG_ESPACE;
  712. for (i = 0, ch = 0; i < BITSET_UINTS; ++i)
  713. for (j = 0; j < UINT_BITS; ++j, ++ch)
  714. if (isalnum (ch) || ch == '_')
  715. dfa->word_char[i] |= 1 << j;
  716. return REG_NOERROR;
  717. }
  718. /* Free the work area which are only used while compiling. */
  719. static void
  720. free_workarea_compile (preg)
  721. regex_t *preg;
  722. {
  723. re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
  724. free_bin_tree (dfa->str_tree);
  725. dfa->str_tree = NULL;
  726. re_free (dfa->org_indices);
  727. dfa->org_indices = NULL;
  728. }
  729. /* Create initial states for all contexts. */
  730. static reg_errcode_t
  731. create_initial_state (dfa)
  732. re_dfa_t *dfa;
  733. {
  734. int first, i;
  735. reg_errcode_t err;
  736. re_node_set init_nodes;
  737. /* Initial states have the epsilon closure of the node which is
  738. the first node of the regular expression. */
  739. first = dfa->str_tree->first;
  740. dfa->init_node = first;
  741. err = re_node_set_init_copy (&init_nodes, dfa->eclosures + first);
  742. if (BE (err != REG_NOERROR, 0))
  743. return err;
  744. /* The back-references which are in initial states can epsilon transit,
  745. since in this case all of the subexpressions can be null.
  746. Then we add epsilon closures of the nodes which are the next nodes of
  747. the back-references. */
  748. if (dfa->nbackref > 0)
  749. for (i = 0; i < init_nodes.nelem; ++i)
  750. {
  751. int node_idx = init_nodes.elems[i];
  752. re_token_type_t type = dfa->nodes[node_idx].type;
  753. int clexp_idx;
  754. if (type != OP_BACK_REF)
  755. continue;
  756. for (clexp_idx = 0; clexp_idx < init_nodes.nelem; ++clexp_idx)
  757. {
  758. re_token_t *clexp_node;
  759. clexp_node = dfa->nodes + init_nodes.elems[clexp_idx];
  760. if (clexp_node->type == OP_CLOSE_SUBEXP
  761. && clexp_node->opr.idx + 1 == dfa->nodes[node_idx].opr.idx)
  762. break;
  763. }
  764. if (clexp_idx == init_nodes.nelem)
  765. continue;
  766. if (type == OP_BACK_REF)
  767. {
  768. int dest_idx = dfa->edests[node_idx].elems[0];
  769. if (!re_node_set_contains (&init_nodes, dest_idx))
  770. {
  771. re_node_set_merge (&init_nodes, dfa->eclosures + dest_idx);
  772. i = 0;
  773. }
  774. }
  775. }
  776. /* It must be the first time to invoke acquire_state. */
  777. dfa->init_state = re_acquire_state_context (&err, dfa, &init_nodes, 0);
  778. /* We don't check ERR here, since the initial state must not be NULL. */
  779. if (BE (dfa->init_state == NULL, 0))
  780. return err;
  781. if (dfa->init_state->has_constraint)
  782. {
  783. dfa->init_state_word = re_acquire_state_context (&err, dfa, &init_nodes,
  784. CONTEXT_WORD);
  785. dfa->init_state_nl = re_acquire_state_context (&err, dfa, &init_nodes,
  786. CONTEXT_NEWLINE);
  787. dfa->init_state_begbuf = re_acquire_state_context (&err, dfa,
  788. &init_nodes,
  789. CONTEXT_NEWLINE
  790. | CONTEXT_BEGBUF);
  791. if (BE (dfa->init_state_word == NULL || dfa->init_state_nl == NULL
  792. || dfa->init_state_begbuf == NULL, 0))
  793. return err;
  794. }
  795. else
  796. dfa->init_state_word = dfa->init_state_nl
  797. = dfa->init_state_begbuf = dfa->init_state;
  798. re_node_set_free (&init_nodes);
  799. return REG_NOERROR;
  800. }
  801. /* Analyze the structure tree, and calculate "first", "next", "edest",
  802. "eclosure", and "inveclosure". */
  803. static reg_errcode_t
  804. analyze (dfa)
  805. re_dfa_t *dfa;
  806. {
  807. int i;
  808. reg_errcode_t ret;
  809. /* Allocate arrays. */
  810. dfa->nexts = re_malloc (int, dfa->nodes_alloc);
  811. dfa->org_indices = re_malloc (int, dfa->nodes_alloc);
  812. dfa->edests = re_malloc (re_node_set, dfa->nodes_alloc);
  813. dfa->eclosures = re_malloc (re_node_set, dfa->nodes_alloc);
  814. dfa->inveclosures = re_malloc (re_node_set, dfa->nodes_alloc);
  815. if (BE (dfa->nexts == NULL || dfa->org_indices == NULL || dfa->edests == NULL
  816. || dfa->eclosures == NULL || dfa->inveclosures == NULL, 0))
  817. return REG_ESPACE;
  818. /* Initialize them. */
  819. for (i = 0; i < dfa->nodes_len; ++i)
  820. {
  821. dfa->nexts[i] = -1;
  822. re_node_set_init_empty (dfa->edests + i);
  823. re_node_set_init_empty (dfa->eclosures + i);
  824. re_node_set_init_empty (dfa->inveclosures + i);
  825. }
  826. ret = analyze_tree (dfa, dfa->str_tree);
  827. if (BE (ret == REG_NOERROR, 1))
  828. {
  829. ret = calc_eclosure (dfa);
  830. if (ret == REG_NOERROR)
  831. calc_inveclosure (dfa);
  832. }
  833. return ret;
  834. }
  835. /* Helper functions for analyze.
  836. This function calculate "first", "next", and "edest" for the subtree
  837. whose root is NODE. */
  838. static reg_errcode_t
  839. analyze_tree (dfa, node)
  840. re_dfa_t *dfa;
  841. bin_tree_t *node;
  842. {
  843. reg_errcode_t ret;
  844. if (node->first == -1)
  845. calc_first (dfa, node);
  846. if (node->next == -1)
  847. calc_next (dfa, node);
  848. if (node->eclosure.nelem == 0)
  849. calc_epsdest (dfa, node);
  850. /* Calculate "first" etc. for the left child. */
  851. if (node->left != NULL)
  852. {
  853. ret = analyze_tree (dfa, node->left);
  854. if (BE (ret != REG_NOERROR, 0))
  855. return ret;
  856. }
  857. /* Calculate "first" etc. for the right child. */
  858. if (node->right != NULL)
  859. {
  860. ret = analyze_tree (dfa, node->right);
  861. if (BE (ret != REG_NOERROR, 0))
  862. return ret;
  863. }
  864. return REG_NOERROR;
  865. }
  866. /* Calculate "first" for the node NODE. */
  867. static void
  868. calc_first (dfa, node)
  869. re_dfa_t *dfa;
  870. bin_tree_t *node;
  871. {
  872. int idx, type;
  873. idx = node->node_idx;
  874. type = (node->type == 0) ? dfa->nodes[idx].type : node->type;
  875. switch (type)
  876. {
  877. #ifdef DEBUG
  878. case OP_OPEN_BRACKET:
  879. case OP_CLOSE_BRACKET:
  880. case OP_OPEN_DUP_NUM:
  881. case OP_CLOSE_DUP_NUM:
  882. case OP_NON_MATCH_LIST:
  883. case OP_OPEN_COLL_ELEM:
  884. case OP_CLOSE_COLL_ELEM:
  885. case OP_OPEN_EQUIV_CLASS:
  886. case OP_CLOSE_EQUIV_CLASS:
  887. case OP_OPEN_CHAR_CLASS:
  888. case OP_CLOSE_CHAR_CLASS:
  889. /* These must not be appeared here. */
  890. assert (0);
  891. #endif
  892. case END_OF_RE:
  893. case CHARACTER:
  894. case OP_PERIOD:
  895. case OP_DUP_ASTERISK:
  896. case OP_DUP_QUESTION:
  897. #ifdef RE_ENABLE_I18N
  898. case COMPLEX_BRACKET:
  899. #endif /* RE_ENABLE_I18N */
  900. case SIMPLE_BRACKET:
  901. case OP_BACK_REF:
  902. case ANCHOR:
  903. case OP_OPEN_SUBEXP:
  904. case OP_CLOSE_SUBEXP:
  905. node->first = idx;
  906. break;
  907. case OP_DUP_PLUS:
  908. #ifdef DEBUG
  909. assert (node->left != NULL);
  910. #endif
  911. if (node->left->first == -1)
  912. calc_first (dfa, node->left);
  913. node->first = node->left->first;
  914. break;
  915. case OP_ALT:
  916. node->first = idx;
  917. break;
  918. /* else fall through */
  919. default:
  920. #ifdef DEBUG
  921. assert (node->left != NULL);
  922. #endif
  923. if (node->left->first == -1)
  924. calc_first (dfa, node->left);
  925. node->first = node->left->first;
  926. break;
  927. }
  928. }
  929. /* Calculate "next" for the node NODE. */
  930. static void
  931. calc_next (dfa, node)
  932. re_dfa_t *dfa;
  933. bin_tree_t *node;
  934. {
  935. int idx, type;
  936. bin_tree_t *parent = node->parent;
  937. if (parent == NULL)
  938. {
  939. node->next = -1;
  940. idx = node->node_idx;
  941. if (node->type == 0)
  942. dfa->nexts[idx] = node->next;
  943. return;
  944. }
  945. idx = parent->node_idx;
  946. type = (parent->type == 0) ? dfa->nodes[idx].type : parent->type;
  947. switch (type)
  948. {
  949. case OP_DUP_ASTERISK:
  950. case OP_DUP_PLUS:
  951. node->next = idx;
  952. break;
  953. case CONCAT:
  954. if (parent->left == node)
  955. {
  956. if (parent->right->first == -1)
  957. calc_first (dfa, parent->right);
  958. node->next = parent->right->first;
  959. break;
  960. }
  961. /* else fall through */
  962. default:
  963. if (parent->next == -1)
  964. calc_next (dfa, parent);
  965. node->next = parent->next;
  966. break;
  967. }
  968. idx = node->node_idx;
  969. if (node->type == 0)
  970. dfa->nexts[idx] = node->next;
  971. }
  972. /* Calculate "edest" for the node NODE. */
  973. static void
  974. calc_epsdest (dfa, node)
  975. re_dfa_t *dfa;
  976. bin_tree_t *node;
  977. {
  978. int idx;
  979. idx = node->node_idx;
  980. if (node->type == 0)
  981. {
  982. if (dfa->nodes[idx].type == OP_DUP_ASTERISK
  983. || dfa->nodes[idx].type == OP_DUP_PLUS
  984. || dfa->nodes[idx].type == OP_DUP_QUESTION)
  985. {
  986. if (node->left->first == -1)
  987. calc_first (dfa, node->left);
  988. if (node->next == -1)
  989. calc_next (dfa, node);
  990. re_node_set_init_2 (dfa->edests + idx, node->left->first,
  991. node->next);
  992. }
  993. else if (dfa->nodes[idx].type == OP_ALT)
  994. {
  995. int left, right;
  996. if (node->left != NULL)
  997. {
  998. if (node->left->first == -1)
  999. calc_first (dfa, node->left);
  1000. left = node->left->first;
  1001. }
  1002. else
  1003. {
  1004. if (node->next == -1)
  1005. calc_next (dfa, node);
  1006. left = node->next;
  1007. }
  1008. if (node->right != NULL)
  1009. {
  1010. if (node->right->first == -1)
  1011. calc_first (dfa, node->right);
  1012. right = node->right->first;
  1013. }
  1014. else
  1015. {
  1016. if (node->next == -1)
  1017. calc_next (dfa, node);
  1018. right = node->next;
  1019. }
  1020. re_node_set_init_2 (dfa->edests + idx, left, right);
  1021. }
  1022. else if (dfa->nodes[idx].type == ANCHOR
  1023. || dfa->nodes[idx].type == OP_OPEN_SUBEXP
  1024. || dfa->nodes[idx].type == OP_CLOSE_SUBEXP
  1025. || dfa->nodes[idx].type == OP_BACK_REF)
  1026. re_node_set_init_1 (dfa->edests + idx, node->next);
  1027. }
  1028. }
  1029. /* Duplicate the epsilon closure of the node ROOT_NODE.
  1030. Note that duplicated nodes have constraint INIT_CONSTRAINT in addition
  1031. to their own constraint. */
  1032. static reg_errcode_t
  1033. duplicate_node_closure (dfa, top_org_node, top_clone_node, root_node,
  1034. init_constraint)
  1035. re_dfa_t *dfa;
  1036. int top_org_node, top_clone_node, root_node;
  1037. unsigned int init_constraint;
  1038. {
  1039. reg_errcode_t err;
  1040. int org_node, clone_node, ret;
  1041. unsigned int constraint = init_constraint;
  1042. for (org_node = top_org_node, clone_node = top_clone_node;;)
  1043. {
  1044. int org_dest, clone_dest;
  1045. if (dfa->nodes[org_node].type == OP_BACK_REF)
  1046. {
  1047. /* If the back reference epsilon-transit, its destination must
  1048. also have the constraint. Then duplicate the epsilon closure
  1049. of the destination of the back reference, and store it in
  1050. edests of the back reference. */
  1051. org_dest = dfa->nexts[org_node];
  1052. re_node_set_empty (dfa->edests + clone_node);
  1053. err = duplicate_node (&clone_dest, dfa, org_dest, constraint);
  1054. if (BE (err != REG_NOERROR, 0))
  1055. return err;
  1056. dfa->nexts[clone_node] = dfa->nexts[org_node];
  1057. ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
  1058. if (BE (ret < 0, 0))
  1059. return REG_ESPACE;
  1060. }
  1061. else if (dfa->edests[org_node].nelem == 0)
  1062. {
  1063. /* In case of the node can't epsilon-transit, don't duplicate the
  1064. destination and store the original destination as the
  1065. destination of the node. */
  1066. dfa->nexts[clone_node] = dfa->nexts[org_node];
  1067. break;
  1068. }
  1069. else if (dfa->edests[org_node].nelem == 1)
  1070. {
  1071. /* In case of the node can epsilon-transit, and it has only one
  1072. destination. */
  1073. org_dest = dfa->edests[org_node].elems[0];
  1074. re_node_set_empty (dfa->edests + clone_node);
  1075. if (dfa->nodes[org_node].type == ANCHOR)
  1076. {
  1077. /* In case of the node has another constraint, append it. */
  1078. if (org_node == root_node && clone_node != org_node)
  1079. {
  1080. /* ...but if the node is root_node itself, it means the
  1081. epsilon closure have a loop, then tie it to the
  1082. destination of the root_node. */
  1083. ret = re_node_set_insert (dfa->edests + clone_node,
  1084. org_dest);
  1085. if (BE (ret < 0, 0))
  1086. return REG_ESPACE;
  1087. break;
  1088. }
  1089. constraint |= dfa->nodes[org_node].opr.ctx_type;
  1090. }
  1091. err = duplicate_node (&clone_dest, dfa, org_dest, constraint);
  1092. if (BE (err != REG_NOERROR, 0))
  1093. return err;
  1094. ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
  1095. if (BE (ret < 0, 0))
  1096. return REG_ESPACE;
  1097. }
  1098. else /* dfa->edests[org_node].nelem == 2 */
  1099. {
  1100. /* In case of the node can epsilon-transit, and it has two
  1101. destinations. E.g. '|', '*', '+', '?'. */
  1102. org_dest = dfa->edests[org_node].elems[0];
  1103. re_node_set_empty (dfa->edests + clone_node);
  1104. /* Search for a duplicated node which satisfies the constraint. */
  1105. clone_dest = search_duplicated_node (dfa, org_dest, constraint);
  1106. if (clone_dest == -1)
  1107. {
  1108. /* There are no such a duplicated node, create a new one. */
  1109. err = duplicate_node (&clone_dest, dfa, org_dest, constraint);
  1110. if (BE (err != REG_NOERROR, 0))
  1111. return err;
  1112. ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
  1113. if (BE (ret < 0, 0))
  1114. return REG_ESPACE;
  1115. err = duplicate_node_closure (dfa, org_dest, clone_dest,
  1116. root_node, constraint);
  1117. if (BE (err != REG_NOERROR, 0))
  1118. return err;
  1119. }
  1120. else
  1121. {
  1122. /* There are a duplicated node which satisfy the constraint,
  1123. use it to avoid infinite loop. */
  1124. ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
  1125. if (BE (ret < 0, 0))
  1126. return REG_ESPACE;
  1127. }
  1128. org_dest = dfa->edests[org_node].elems[1];
  1129. err = duplicate_node (&clone_dest, dfa, org_dest, constraint);
  1130. if (BE (err != REG_NOERROR, 0))
  1131. return err;
  1132. ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
  1133. if (BE (ret < 0, 0))
  1134. return REG_ESPACE;
  1135. }
  1136. org_node = org_dest;
  1137. clone_node = clone_dest;
  1138. }
  1139. return REG_NOERROR;
  1140. }
  1141. /* Search for a node which is duplicated from the node ORG_NODE, and
  1142. satisfies the constraint CONSTRAINT. */
  1143. static int
  1144. search_duplicated_node (dfa, org_node, constraint)
  1145. re_dfa_t *dfa;
  1146. int org_node;
  1147. unsigned int constraint;
  1148. {
  1149. int idx;
  1150. for (idx = dfa->nodes_len - 1; dfa->nodes[idx].duplicated && idx > 0; --idx)
  1151. {
  1152. if (org_node == dfa->org_indices[idx]
  1153. && constraint == dfa->nodes[idx].constraint)
  1154. return idx; /* Found. */
  1155. }
  1156. return -1; /* Not found. */
  1157. }
  1158. /* Duplicate the node whose index is ORG_IDX and set the constraint CONSTRAINT.
  1159. The new index will be stored in NEW_IDX and return REG_NOERROR if succeeded,
  1160. otherwise return the error code. */
  1161. static reg_errcode_t
  1162. duplicate_node (new_idx, dfa, org_idx, constraint)
  1163. re_dfa_t *dfa;
  1164. int *new_idx, org_idx;
  1165. unsigned int constraint;
  1166. {
  1167. re_token_t dup;
  1168. int dup_idx;
  1169. dup = dfa->nodes[org_idx];
  1170. dup_idx = re_dfa_add_node (dfa, dup, 1);
  1171. if (BE (dup_idx == -1, 0))
  1172. return REG_ESPACE;
  1173. dfa->nodes[dup_idx].constraint = constraint;
  1174. if (dfa->nodes[org_idx].type == ANCHOR)
  1175. dfa->nodes[dup_idx].constraint |= dfa->nodes[org_idx].opr.ctx_type;
  1176. dfa->nodes[dup_idx].duplicated = 1;
  1177. re_node_set_init_empty (dfa->edests + dup_idx);
  1178. re_node_set_init_empty (dfa->eclosures + dup_idx);
  1179. re_node_set_init_empty (dfa->inveclosures + dup_idx);
  1180. /* Store the index of the original node. */
  1181. dfa->org_indices[dup_idx] = org_idx;
  1182. *new_idx = dup_idx;
  1183. return REG_NOERROR;
  1184. }
  1185. static void
  1186. calc_inveclosure (dfa)
  1187. re_dfa_t *dfa;
  1188. {
  1189. int src, idx, dest;
  1190. for (src = 0; src < dfa->nodes_len; ++src)
  1191. {
  1192. for (idx = 0; idx < dfa->eclosures[src].nelem; ++idx)
  1193. {
  1194. dest = dfa->eclosures[src].elems[idx];
  1195. re_node_set_insert (dfa->inveclosures + dest, src);
  1196. }
  1197. }
  1198. }
  1199. /* Calculate "eclosure" for all the node in DFA. */
  1200. static reg_errcode_t
  1201. calc_eclosure (dfa)
  1202. re_dfa_t *dfa;
  1203. {
  1204. int node_idx, incomplete;
  1205. #ifdef DEBUG
  1206. assert (dfa->nodes_len > 0);
  1207. #endif
  1208. incomplete = 0;
  1209. /* For each nodes, calculate epsilon closure. */
  1210. for (node_idx = 0; ; ++node_idx)
  1211. {
  1212. reg_errcode_t err;
  1213. re_node_set eclosure_elem;
  1214. if (node_idx == dfa->nodes_len)
  1215. {
  1216. if (!incomplete)
  1217. break;
  1218. incomplete = 0;
  1219. node_idx = 0;
  1220. }
  1221. #ifdef DEBUG
  1222. assert (dfa->eclosures[node_idx].nelem != -1);
  1223. #endif
  1224. /* If we have already calculated, skip it. */
  1225. if (dfa->eclosures[node_idx].nelem != 0)
  1226. continue;
  1227. /* Calculate epsilon closure of `node_idx'. */
  1228. err = calc_eclosure_iter (&eclosure_elem, dfa, node_idx, 1);
  1229. if (BE (err != REG_NOERROR, 0))
  1230. return err;
  1231. if (dfa->eclosures[node_idx].nelem == 0)
  1232. {
  1233. incomplete = 1;
  1234. re_node_set_free (&eclosure_elem);
  1235. }
  1236. }
  1237. return REG_NOERROR;
  1238. }
  1239. /* Calculate epsilon closure of NODE. */
  1240. static reg_errcode_t
  1241. calc_eclosure_iter (new_set, dfa, node, root)
  1242. re_node_set *new_set;
  1243. re_dfa_t *dfa;
  1244. int node, root;
  1245. {
  1246. reg_errcode_t err;
  1247. unsigned int constraint;
  1248. int i, incomplete;
  1249. re_node_set eclosure;
  1250. incomplete = 0;
  1251. err = re_node_set_alloc (&eclosure, dfa->edests[node].nelem + 1);
  1252. if (BE (err != REG_NOERROR, 0))
  1253. return err;
  1254. /* This indicates that we are calculating this node now.
  1255. We reference this value to avoid infinite loop. */
  1256. dfa->eclosures[node].nelem = -1;
  1257. constraint = ((dfa->nodes[node].type == ANCHOR)
  1258. ? dfa->nodes[node].opr.ctx_type : 0);
  1259. /* If the current node has constraints, duplicate all nodes.
  1260. Since they must inherit the constraints. */
  1261. if (constraint && !dfa->nodes[dfa->edests[node].elems[0]].duplicated)
  1262. {
  1263. int org_node, cur_node;
  1264. org_node = cur_node = node;
  1265. err = duplicate_node_closure (dfa, node, node, node, constraint);
  1266. if (BE (err != REG_NOERROR, 0))
  1267. return err;
  1268. }
  1269. /* Expand each epsilon destination nodes. */
  1270. if (IS_EPSILON_NODE(dfa->nodes[node].type))
  1271. for (i = 0; i < dfa->edests[node].nelem; ++i)
  1272. {
  1273. re_node_set eclosure_elem;
  1274. int edest = dfa->edests[node].elems[i];
  1275. /* If calculating the epsilon closure of `edest' is in progress,
  1276. return intermediate result. */
  1277. if (dfa->eclosures[edest].nelem == -1)
  1278. {
  1279. incomplete = 1;
  1280. continue;
  1281. }
  1282. /* If we haven't calculated the epsilon closure of `edest' yet,
  1283. calculate now. Otherwise use calculated epsilon closure. */
  1284. if (dfa->eclosures[edest].nelem == 0)
  1285. {
  1286. err = calc_eclosure_iter (&eclosure_elem, dfa, edest, 0);
  1287. if (BE (err != REG_NOERROR, 0))
  1288. return err;
  1289. }
  1290. else
  1291. eclosure_elem = dfa->eclosures[edest];
  1292. /* Merge the epsilon closure of `edest'. */
  1293. re_node_set_merge (&eclosure, &eclosure_elem);
  1294. /* If the epsilon closure of `edest' is incomplete,
  1295. the epsilon closure of this node is also incomplete. */
  1296. if (dfa->eclosures[edest].nelem == 0)
  1297. {
  1298. incomplete = 1;
  1299. re_node_set_free (&eclosure_elem);
  1300. }
  1301. }
  1302. /* Epsilon closures include itself. */
  1303. re_node_set_insert (&eclosure, node);
  1304. if (incomplete && !root)
  1305. dfa->eclosures[node].nelem = 0;
  1306. else
  1307. dfa->eclosures[node] = eclosure;
  1308. *new_set = eclosure;
  1309. return REG_NOERROR;
  1310. }
  1311. /* Functions for token which are used in the parser. */
  1312. /* Fetch a token from INPUT.
  1313. We must not use this function inside bracket expressions. */
  1314. static re_token_t
  1315. fetch_token (input, syntax)
  1316. re_string_t *input;
  1317. reg_syntax_t syntax;
  1318. {
  1319. re_token_t token;
  1320. int consumed_byte;
  1321. consumed_byte = peek_token (&token, input, syntax);
  1322. re_string_skip_bytes (input, consumed_byte);
  1323. return token;
  1324. }
  1325. /* Peek a token from INPUT, and return the length of the token.
  1326. We must not use this function inside bracket expressions. */
  1327. static int
  1328. peek_token (token, input, syntax)
  1329. re_token_t *token;
  1330. re_string_t *input;
  1331. reg_syntax_t syntax;
  1332. {
  1333. unsigned char c;
  1334. if (re_string_eoi (input))
  1335. {
  1336. token->type = END_OF_RE;
  1337. return 0;
  1338. }
  1339. c = re_string_peek_byte (input, 0);
  1340. token->opr.c = c;
  1341. #ifdef RE_ENABLE_I18N
  1342. token->mb_partial = 0;
  1343. if (MB_CUR_MAX > 1 &&
  1344. !re_string_first_byte (input, re_string_cur_idx (input)))
  1345. {
  1346. token->type = CHARACTER;
  1347. token->mb_partial = 1;
  1348. return 1;
  1349. }
  1350. #endif
  1351. if (c == '\\')
  1352. {
  1353. unsigned char c2;
  1354. if (re_string_cur_idx (input) + 1 >= re_string_length (input))
  1355. {
  1356. token->type = BACK_SLASH;
  1357. return 1;
  1358. }
  1359. c2 = re_string_peek_byte_case (input, 1);
  1360. token->opr.c = c2;
  1361. token->type = CHARACTER;
  1362. switch (c2)
  1363. {
  1364. case '|':
  1365. if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_NO_BK_VBAR))
  1366. token->type = OP_ALT;
  1367. break;
  1368. case '1': case '2': case '3': case '4': case '5':
  1369. case '6': case '7': case '8': case '9':
  1370. if (!(syntax & RE_NO_BK_REFS))
  1371. {
  1372. token->type = OP_BACK_REF;
  1373. token->opr.idx = c2 - '0';
  1374. }
  1375. break;
  1376. case '<':
  1377. if (!(syntax & RE_NO_GNU_OPS))
  1378. {
  1379. token->type = ANCHOR;
  1380. token->opr.idx = WORD_FIRST;
  1381. }
  1382. break;
  1383. case '>':
  1384. if (!(syntax & RE_NO_GNU_OPS))
  1385. {
  1386. token->type = ANCHOR;
  1387. token->opr.idx = WORD_LAST;
  1388. }
  1389. break;
  1390. case 'b':
  1391. if (!(syntax & RE_NO_GNU_OPS))
  1392. {
  1393. token->type = ANCHOR;
  1394. token->opr.idx = WORD_DELIM;
  1395. }
  1396. break;
  1397. case 'B':
  1398. if (!(syntax & RE_NO_GNU_OPS))
  1399. {
  1400. token->type = ANCHOR;
  1401. token->opr.idx = INSIDE_WORD;
  1402. }
  1403. break;
  1404. case 'w':
  1405. if (!(syntax & RE_NO_GNU_OPS))
  1406. token->type = OP_WORD;
  1407. break;
  1408. case 'W':
  1409. if (!(syntax & RE_NO_GNU_OPS))
  1410. token->type = OP_NOTWORD;
  1411. break;
  1412. case '`':
  1413. if (!(syntax & RE_NO_GNU_OPS))
  1414. {
  1415. token->type = ANCHOR;
  1416. token->opr.idx = BUF_FIRST;
  1417. }
  1418. break;
  1419. case '\'':
  1420. if (!(syntax & RE_NO_GNU_OPS))
  1421. {
  1422. token->type = ANCHOR;
  1423. token->opr.idx = BUF_LAST;
  1424. }
  1425. break;
  1426. case '(':
  1427. if (!(syntax & RE_NO_BK_PARENS))
  1428. token->type = OP_OPEN_SUBEXP;
  1429. break;
  1430. case ')':
  1431. if (!(syntax & RE_NO_BK_PARENS))
  1432. token->type = OP_CLOSE_SUBEXP;
  1433. break;
  1434. case '+':
  1435. if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_BK_PLUS_QM))
  1436. token->type = OP_DUP_PLUS;
  1437. break;
  1438. case '?':
  1439. if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_BK_PLUS_QM))
  1440. token->type = OP_DUP_QUESTION;
  1441. break;
  1442. case '{':
  1443. if ((syntax & RE_INTERVALS) && (!(syntax & RE_NO_BK_BRACES)))
  1444. token->type = OP_OPEN_DUP_NUM;
  1445. break;
  1446. case '}':
  1447. if ((syntax & RE_INTERVALS) && (!(syntax & RE_NO_BK_BRACES)))
  1448. token->type = OP_CLOSE_DUP_NUM;
  1449. break;
  1450. default:
  1451. break;
  1452. }
  1453. return 2;
  1454. }
  1455. token->type = CHARACTER;
  1456. switch (c)
  1457. {
  1458. case '\n':
  1459. if (syntax & RE_NEWLINE_ALT)
  1460. token->type = OP_ALT;
  1461. break;
  1462. case '|':
  1463. if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_NO_BK_VBAR))
  1464. token->type = OP_ALT;
  1465. break;
  1466. case '*':
  1467. token->type = OP_DUP_ASTERISK;
  1468. break;
  1469. case '+':
  1470. if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_BK_PLUS_QM))
  1471. token->type = OP_DUP_PLUS;
  1472. break;
  1473. case '?':
  1474. if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_BK_PLUS_QM))
  1475. token->type = OP_DUP_QUESTION;
  1476. break;
  1477. case '{':
  1478. if ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
  1479. token->type = OP_OPEN_DUP_NUM;
  1480. break;
  1481. case '}':
  1482. if ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
  1483. token->type = OP_CLOSE_DUP_NUM;
  1484. break;
  1485. case '(':
  1486. if (syntax & RE_NO_BK_PARENS)
  1487. token->type = OP_OPEN_SUBEXP;
  1488. break;
  1489. case ')':
  1490. if (syntax & RE_NO_BK_PARENS)
  1491. token->type = OP_CLOSE_SUBEXP;
  1492. break;
  1493. case '[':
  1494. token->type = OP_OPEN_BRACKET;
  1495. break;
  1496. case '.':
  1497. token->type = OP_PERIOD;
  1498. break;
  1499. case '^':
  1500. if (!(syntax & RE_CONTEXT_INDEP_ANCHORS) &&
  1501. re_string_cur_idx (input) != 0)
  1502. {
  1503. char prev = re_string_peek_byte (input, -1);
  1504. if (prev != '|' && prev != '(' &&
  1505. (!(syntax & RE_NEWLINE_ALT) || prev != '\n'))
  1506. break;
  1507. }
  1508. token->type = ANCHOR;
  1509. token->opr.idx = LINE_FIRST;
  1510. break;
  1511. case '$':
  1512. if (!(syntax & RE_CONTEXT_INDEP_ANCHORS) &&
  1513. re_string_cur_idx (input) + 1 != re_string_length (input))
  1514. {
  1515. re_token_t next;
  1516. re_string_skip_bytes (input, 1);
  1517. peek_token (&next, input, syntax);
  1518. re_string_skip_bytes (input, -1);
  1519. if (next.type != OP_ALT && next.type != OP_CLOSE_SUBEXP)
  1520. break;
  1521. }
  1522. token->type = ANCHOR;
  1523. token->opr.idx = LINE_LAST;
  1524. break;
  1525. default:
  1526. break;
  1527. }
  1528. return 1;
  1529. }
  1530. /* Peek a token from INPUT, and return the length of the token.
  1531. We must not use this function out of bracket expressions. */
  1532. static int
  1533. peek_token_bracket (token, input, syntax)
  1534. re_token_t *token;
  1535. re_string_t *input;
  1536. reg_syntax_t syntax;
  1537. {
  1538. unsigned char c;
  1539. if (re_string_eoi (input))
  1540. {
  1541. token->type = END_OF_RE;
  1542. return 0;
  1543. }
  1544. c = re_string_peek_byte (input, 0);
  1545. token->opr.c = c;
  1546. #ifdef RE_ENABLE_I18N
  1547. if (MB_CUR_MAX > 1 &&
  1548. !re_string_first_byte (input, re_string_cur_idx (input)))
  1549. {
  1550. token->type = CHARACTER;
  1551. return 1;
  1552. }
  1553. #endif /* RE_ENABLE_I18N */
  1554. if (c == '\\' && (syntax & RE_BACKSLASH_ESCAPE_IN_LISTS))
  1555. {
  1556. /* In this case, '\' escape a character. */
  1557. unsigned char c2;
  1558. re_string_skip_bytes (input, 1);
  1559. c2 = re_string_peek_byte (input, 0);
  1560. token->opr.c = c2;
  1561. token->type = CHARACTER;
  1562. return 1;
  1563. }
  1564. if (c == '[') /* '[…

Large files files are truncated, but you can click here to view the full file