regex.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. /*
  2. Search text engine.
  3. Regex search
  4. Copyright (C) 2009 The Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2009,2010,2011
  7. Vitaliy Filippov <vitalif@yourcmc.ru>, 2011
  8. This file is part of the Midnight Commander.
  9. The Midnight Commander is free software; you can redistribute it
  10. and/or modify it under the terms of the GNU General Public License as
  11. published by the Free Software Foundation; either version 2 of the
  12. License, or (at your option) any later version.
  13. The Midnight Commander is distributed in the hope that it will be
  14. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  15. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  20. MA 02110-1301, USA.
  21. */
  22. #include <config.h>
  23. #include <stdlib.h>
  24. #include "lib/global.h"
  25. #include "lib/strutil.h"
  26. #include "lib/search.h"
  27. #include "lib/strescape.h"
  28. #include "lib/charsets.h"
  29. #include "internal.h"
  30. /*** global variables ****************************************************************************/
  31. /*** file scope macro definitions ****************************************************************/
  32. #define REPLACE_PREPARE_T_NOTHING_SPECIAL -1
  33. #define REPLACE_PREPARE_T_REPLACE_FLAG -2
  34. #define REPLACE_PREPARE_T_ESCAPE_SEQ -3
  35. /*** file scope type declarations ****************************************************************/
  36. typedef enum
  37. {
  38. REPLACE_T_NO_TRANSFORM = 0,
  39. REPLACE_T_UPP_TRANSFORM_CHAR = 1,
  40. REPLACE_T_LOW_TRANSFORM_CHAR = 2,
  41. REPLACE_T_UPP_TRANSFORM = 4,
  42. REPLACE_T_LOW_TRANSFORM = 8
  43. } replace_transform_type_t;
  44. /*** file scope variables ************************************************************************/
  45. /*** file scope functions ************************************************************************/
  46. static gboolean
  47. mc_search__regex_str_append_if_special (GString * copy_to, const GString * regex_str,
  48. gsize * offset)
  49. {
  50. char *tmp_regex_str;
  51. gsize spec_chr_len;
  52. const char **spec_chr;
  53. const char *special_chars[] = {
  54. "\\s", "\\S",
  55. "\\d", "\\D",
  56. "\\b", "\\B",
  57. "\\w", "\\W",
  58. "\\t", "\\n",
  59. "\\r", "\\f",
  60. "\\a", "\\e",
  61. "\\x", "\\X",
  62. "\\c", "\\C",
  63. "\\l", "\\L",
  64. "\\u", "\\U",
  65. "\\E", "\\Q",
  66. NULL
  67. };
  68. spec_chr = special_chars;
  69. tmp_regex_str = &(regex_str->str[*offset]);
  70. while (*spec_chr)
  71. {
  72. spec_chr_len = strlen (*spec_chr);
  73. if (!strncmp (tmp_regex_str, *spec_chr, spec_chr_len))
  74. {
  75. if (!strutils_is_char_escaped (regex_str->str, tmp_regex_str))
  76. {
  77. if (!strncmp ("\\x", *spec_chr, spec_chr_len))
  78. {
  79. if (*(tmp_regex_str + spec_chr_len) == '{')
  80. {
  81. while ((spec_chr_len < regex_str->len - *offset)
  82. && *(tmp_regex_str + spec_chr_len) != '}')
  83. spec_chr_len++;
  84. if (*(tmp_regex_str + spec_chr_len) == '}')
  85. spec_chr_len++;
  86. }
  87. else
  88. spec_chr_len += 2;
  89. }
  90. g_string_append_len (copy_to, tmp_regex_str, spec_chr_len);
  91. *offset += spec_chr_len;
  92. return TRUE;
  93. }
  94. }
  95. spec_chr++;
  96. }
  97. return FALSE;
  98. }
  99. /* --------------------------------------------------------------------------------------------- */
  100. static void
  101. mc_search__cond_struct_new_regex_hex_add (const char *charset, GString * str_to,
  102. const char *one_char, gsize str_len)
  103. {
  104. GString *upp, *low;
  105. gchar *tmp_str;
  106. gsize loop;
  107. upp = mc_search__toupper_case_str (charset, one_char, str_len);
  108. low = mc_search__tolower_case_str (charset, one_char, str_len);
  109. for (loop = 0; loop < upp->len; loop++)
  110. {
  111. if (loop < low->len)
  112. {
  113. if (upp->str[loop] == low->str[loop])
  114. tmp_str = g_strdup_printf ("\\x%02X", (unsigned char) upp->str[loop]);
  115. else
  116. tmp_str =
  117. g_strdup_printf ("[\\x%02X\\x%02X]", (unsigned char) upp->str[loop],
  118. (unsigned char) low->str[loop]);
  119. }
  120. else
  121. {
  122. tmp_str = g_strdup_printf ("\\x%02X", (unsigned char) upp->str[loop]);
  123. }
  124. g_string_append (str_to, tmp_str);
  125. g_free (tmp_str);
  126. }
  127. g_string_free (upp, TRUE);
  128. g_string_free (low, TRUE);
  129. }
  130. /* --------------------------------------------------------------------------------------------- */
  131. static void
  132. mc_search__cond_struct_new_regex_accum_append (const char *charset, GString * str_to,
  133. GString * str_from)
  134. {
  135. GString *recoded_part;
  136. gsize loop = 0;
  137. recoded_part = g_string_sized_new (32);
  138. while (loop < str_from->len)
  139. {
  140. gchar *one_char;
  141. gsize one_char_len;
  142. gboolean just_letters;
  143. one_char =
  144. mc_search__get_one_symbol (charset, &(str_from->str[loop]),
  145. min (str_from->len - loop, 6), &just_letters);
  146. one_char_len = strlen (one_char);
  147. if (one_char_len == 0)
  148. loop++;
  149. else
  150. {
  151. loop += one_char_len;
  152. if (just_letters)
  153. mc_search__cond_struct_new_regex_hex_add (charset, recoded_part, one_char,
  154. one_char_len);
  155. else
  156. g_string_append_len (recoded_part, one_char, one_char_len);
  157. }
  158. g_free (one_char);
  159. }
  160. g_string_append (str_to, recoded_part->str);
  161. g_string_free (recoded_part, TRUE);
  162. g_string_set_size (str_from, 0);
  163. }
  164. /* --------------------------------------------------------------------------------------------- */
  165. static GString *
  166. mc_search__cond_struct_new_regex_ci_str (const char *charset, const GString * astr)
  167. {
  168. GString *accumulator, *spec_char, *ret_str;
  169. gsize loop;
  170. ret_str = g_string_sized_new (64);
  171. accumulator = g_string_sized_new (64);
  172. spec_char = g_string_sized_new (64);
  173. loop = 0;
  174. while (loop <= astr->len)
  175. {
  176. if (mc_search__regex_str_append_if_special (spec_char, astr, &loop))
  177. {
  178. mc_search__cond_struct_new_regex_accum_append (charset, ret_str, accumulator);
  179. g_string_append_len (ret_str, spec_char->str, spec_char->len);
  180. g_string_set_size (spec_char, 0);
  181. continue;
  182. }
  183. if (astr->str[loop] == '[' && !strutils_is_char_escaped (astr->str, &(astr->str[loop])))
  184. {
  185. mc_search__cond_struct_new_regex_accum_append (charset, ret_str, accumulator);
  186. while (loop < astr->len && !(astr->str[loop] == ']'
  187. && !strutils_is_char_escaped (astr->str,
  188. &(astr->str[loop]))))
  189. {
  190. g_string_append_c (ret_str, astr->str[loop]);
  191. loop++;
  192. }
  193. g_string_append_c (ret_str, astr->str[loop]);
  194. loop++;
  195. continue;
  196. }
  197. /*
  198. TODO: handle [ and ]
  199. */
  200. g_string_append_c (accumulator, astr->str[loop]);
  201. loop++;
  202. }
  203. mc_search__cond_struct_new_regex_accum_append (charset, ret_str, accumulator);
  204. g_string_free (accumulator, TRUE);
  205. g_string_free (spec_char, TRUE);
  206. return ret_str;
  207. }
  208. /* --------------------------------------------------------------------------------------------- */
  209. static mc_search__found_cond_t
  210. mc_search__regex_found_cond_one (mc_search_t * lc_mc_search, mc_search_regex_t * regex,
  211. GString * search_str)
  212. {
  213. #ifdef SEARCH_TYPE_GLIB
  214. GError *error = NULL;
  215. if (!g_regex_match_full
  216. (regex, search_str->str, -1, 0, G_REGEX_MATCH_NEWLINE_ANY, &lc_mc_search->regex_match_info,
  217. &error))
  218. {
  219. g_match_info_free (lc_mc_search->regex_match_info);
  220. lc_mc_search->regex_match_info = NULL;
  221. if (error)
  222. {
  223. lc_mc_search->error = MC_SEARCH_E_REGEX;
  224. lc_mc_search->error_str =
  225. str_conv_gerror_message (error, _("Regular expression error"));
  226. g_error_free (error);
  227. return COND__FOUND_ERROR;
  228. }
  229. return COND__NOT_FOUND;
  230. }
  231. lc_mc_search->num_results = g_match_info_get_match_count (lc_mc_search->regex_match_info);
  232. #else /* SEARCH_TYPE_GLIB */
  233. lc_mc_search->num_results = pcre_exec (regex, lc_mc_search->regex_match_info,
  234. search_str->str, search_str->len - 1, 0, 0,
  235. lc_mc_search->iovector, MC_SEARCH__NUM_REPLACE_ARGS);
  236. if (lc_mc_search->num_results < 0)
  237. {
  238. return COND__NOT_FOUND;
  239. }
  240. #endif /* SEARCH_TYPE_GLIB */
  241. return COND__FOUND_OK;
  242. }
  243. /* --------------------------------------------------------------------------------------------- */
  244. static mc_search__found_cond_t
  245. mc_search__regex_found_cond (mc_search_t * lc_mc_search, GString * search_str)
  246. {
  247. gsize loop1;
  248. mc_search_cond_t *mc_search_cond;
  249. mc_search__found_cond_t ret;
  250. for (loop1 = 0; loop1 < lc_mc_search->conditions->len; loop1++)
  251. {
  252. mc_search_cond = (mc_search_cond_t *) g_ptr_array_index (lc_mc_search->conditions, loop1);
  253. if (!mc_search_cond->regex_handle)
  254. continue;
  255. ret =
  256. mc_search__regex_found_cond_one (lc_mc_search, mc_search_cond->regex_handle,
  257. search_str);
  258. if (ret != COND__NOT_FOUND)
  259. return ret;
  260. }
  261. return COND__NOT_ALL_FOUND;
  262. }
  263. /* --------------------------------------------------------------------------------------------- */
  264. static int
  265. mc_search_regex__get_max_num_of_replace_tokens (const gchar * str, gsize len)
  266. {
  267. int max_token = 0;
  268. gsize loop;
  269. for (loop = 0; loop < len - 1; loop++)
  270. {
  271. if (str[loop] == '\\' && g_ascii_isdigit (str[loop + 1]))
  272. {
  273. if (strutils_is_char_escaped (str, &str[loop]))
  274. continue;
  275. if (max_token < str[loop + 1] - '0')
  276. max_token = str[loop + 1] - '0';
  277. continue;
  278. }
  279. if (str[loop] == '$' && str[loop + 1] == '{')
  280. {
  281. gsize tmp_len;
  282. char *tmp_str;
  283. int tmp_token;
  284. if (strutils_is_char_escaped (str, &str[loop]))
  285. continue;
  286. for (tmp_len = 0;
  287. loop + tmp_len + 2 < len && (str[loop + 2 + tmp_len] & (char) 0xf0) == 0x30;
  288. tmp_len++);
  289. if (str[loop + 2 + tmp_len] == '}')
  290. {
  291. tmp_str = g_strndup (&str[loop + 2], tmp_len);
  292. tmp_token = atoi (tmp_str);
  293. if (max_token < tmp_token)
  294. max_token = tmp_token;
  295. g_free (tmp_str);
  296. }
  297. }
  298. }
  299. return max_token;
  300. }
  301. /* --------------------------------------------------------------------------------------------- */
  302. static char *
  303. mc_search_regex__get_token_by_num (const mc_search_t * lc_mc_search, gsize lc_index)
  304. {
  305. int fnd_start = 0, fnd_end = 0;
  306. #ifdef SEARCH_TYPE_GLIB
  307. g_match_info_fetch_pos (lc_mc_search->regex_match_info, lc_index, &fnd_start, &fnd_end);
  308. #else /* SEARCH_TYPE_GLIB */
  309. fnd_start = lc_mc_search->iovector[lc_index * 2 + 0];
  310. fnd_end = lc_mc_search->iovector[lc_index * 2 + 1];
  311. #endif /* SEARCH_TYPE_GLIB */
  312. if (fnd_end - fnd_start == 0)
  313. return NULL;
  314. return g_strndup (lc_mc_search->regex_buffer->str + fnd_start, fnd_end - fnd_start);
  315. }
  316. /* --------------------------------------------------------------------------------------------- */
  317. static gboolean
  318. mc_search_regex__replace_handle_esc_seq (const GString * replace_str, const gsize current_pos,
  319. gsize * skip_len, int *ret)
  320. {
  321. char *curr_str = &(replace_str->str[current_pos]);
  322. char c = *(curr_str + 1);
  323. if (replace_str->len > current_pos + 2)
  324. {
  325. if (c == '{')
  326. {
  327. for (*skip_len = 2; /* \{ */
  328. current_pos + *skip_len < replace_str->len
  329. && *(curr_str + *skip_len) >= '0'
  330. && *(curr_str + *skip_len) <= '7'; (*skip_len)++);
  331. if (current_pos + *skip_len < replace_str->len && *(curr_str + *skip_len) == '}')
  332. {
  333. (*skip_len)++;
  334. *ret = REPLACE_PREPARE_T_ESCAPE_SEQ;
  335. return FALSE;
  336. }
  337. else
  338. {
  339. *ret = REPLACE_PREPARE_T_NOTHING_SPECIAL;
  340. return TRUE;
  341. }
  342. }
  343. if (c == 'x')
  344. {
  345. *skip_len = 2; /* \x */
  346. c = *(curr_str + 2);
  347. if (c == '{')
  348. {
  349. for (*skip_len = 3; /* \x{ */
  350. current_pos + *skip_len < replace_str->len
  351. && g_ascii_isxdigit ((guchar) * (curr_str + *skip_len)); (*skip_len)++);
  352. if (current_pos + *skip_len < replace_str->len && *(curr_str + *skip_len) == '}')
  353. {
  354. (*skip_len)++;
  355. *ret = REPLACE_PREPARE_T_ESCAPE_SEQ;
  356. return FALSE;
  357. }
  358. else
  359. {
  360. *ret = REPLACE_PREPARE_T_NOTHING_SPECIAL;
  361. return TRUE;
  362. }
  363. }
  364. else if (!g_ascii_isxdigit ((guchar) c))
  365. {
  366. *skip_len = 2; /* \x without number behind */
  367. *ret = REPLACE_PREPARE_T_NOTHING_SPECIAL;
  368. return FALSE;
  369. }
  370. else
  371. {
  372. c = *(curr_str + 3);
  373. if (!g_ascii_isxdigit ((guchar) c))
  374. *skip_len = 3; /* \xH */
  375. else
  376. *skip_len = 4; /* \xHH */
  377. *ret = REPLACE_PREPARE_T_ESCAPE_SEQ;
  378. return FALSE;
  379. }
  380. }
  381. }
  382. if (strchr ("ntvbrfa", c) != NULL)
  383. {
  384. *skip_len = 2;
  385. *ret = REPLACE_PREPARE_T_ESCAPE_SEQ;
  386. return FALSE;
  387. }
  388. return TRUE;
  389. }
  390. /* --------------------------------------------------------------------------------------------- */
  391. static int
  392. mc_search_regex__process_replace_str (const GString * replace_str, const gsize current_pos,
  393. gsize * skip_len, replace_transform_type_t * replace_flags)
  394. {
  395. int ret = -1;
  396. char *tmp_str;
  397. const char *curr_str = &(replace_str->str[current_pos]);
  398. if (current_pos > replace_str->len)
  399. return REPLACE_PREPARE_T_NOTHING_SPECIAL;
  400. *skip_len = 0;
  401. if ((*curr_str == '$') && (*(curr_str + 1) == '{') && ((*(curr_str + 2) & (char) 0xf0) == 0x30)
  402. && (replace_str->len > current_pos + 2))
  403. {
  404. if (strutils_is_char_escaped (replace_str->str, curr_str))
  405. {
  406. *skip_len = 1;
  407. return REPLACE_PREPARE_T_NOTHING_SPECIAL;
  408. }
  409. for (*skip_len = 0;
  410. current_pos + *skip_len + 2 < replace_str->len
  411. && (*(curr_str + 2 + *skip_len) & (char) 0xf0) == 0x30; (*skip_len)++);
  412. if (*(curr_str + 2 + *skip_len) != '}')
  413. return REPLACE_PREPARE_T_NOTHING_SPECIAL;
  414. tmp_str = g_strndup (curr_str + 2, *skip_len);
  415. if (tmp_str == NULL)
  416. return REPLACE_PREPARE_T_NOTHING_SPECIAL;
  417. ret = atoi (tmp_str);
  418. g_free (tmp_str);
  419. *skip_len += 3; /* ${} */
  420. return ret; /* capture buffer index >= 0 */
  421. }
  422. if ((*curr_str == '\\') && (replace_str->len > current_pos + 1))
  423. {
  424. if (strutils_is_char_escaped (replace_str->str, curr_str))
  425. {
  426. *skip_len = 1;
  427. return REPLACE_PREPARE_T_NOTHING_SPECIAL;
  428. }
  429. if (g_ascii_isdigit (*(curr_str + 1)))
  430. {
  431. ret = g_ascii_digit_value (*(curr_str + 1)); /* capture buffer index >= 0 */
  432. *skip_len = 2; /* \\ and one digit */
  433. return ret;
  434. }
  435. if (!mc_search_regex__replace_handle_esc_seq (replace_str, current_pos, skip_len, &ret))
  436. return ret;
  437. ret = REPLACE_PREPARE_T_REPLACE_FLAG;
  438. *skip_len += 2;
  439. switch (*(curr_str + 1))
  440. {
  441. case 'U':
  442. *replace_flags |= REPLACE_T_UPP_TRANSFORM;
  443. *replace_flags &= ~REPLACE_T_LOW_TRANSFORM;
  444. break;
  445. case 'u':
  446. *replace_flags |= REPLACE_T_UPP_TRANSFORM_CHAR;
  447. break;
  448. case 'L':
  449. *replace_flags |= REPLACE_T_LOW_TRANSFORM;
  450. *replace_flags &= ~REPLACE_T_UPP_TRANSFORM;
  451. break;
  452. case 'l':
  453. *replace_flags |= REPLACE_T_LOW_TRANSFORM_CHAR;
  454. break;
  455. case 'E':
  456. *replace_flags = REPLACE_T_NO_TRANSFORM;
  457. break;
  458. default:
  459. ret = REPLACE_PREPARE_T_NOTHING_SPECIAL;
  460. break;
  461. }
  462. }
  463. return ret;
  464. }
  465. /* --------------------------------------------------------------------------------------------- */
  466. static void
  467. mc_search_regex__process_append_str (GString * dest_str, const char *from, gsize len,
  468. replace_transform_type_t * replace_flags)
  469. {
  470. gsize loop = 0;
  471. gsize char_len;
  472. char *tmp_str;
  473. GString *tmp_string;
  474. if (len == (gsize) - 1)
  475. len = strlen (from);
  476. if (*replace_flags == REPLACE_T_NO_TRANSFORM)
  477. {
  478. g_string_append_len (dest_str, from, len);
  479. return;
  480. }
  481. while (loop < len)
  482. {
  483. tmp_str = mc_search__get_one_symbol (NULL, from + loop, len - loop, NULL);
  484. char_len = strlen (tmp_str);
  485. if (*replace_flags & REPLACE_T_UPP_TRANSFORM_CHAR)
  486. {
  487. *replace_flags &= ~REPLACE_T_UPP_TRANSFORM_CHAR;
  488. tmp_string = mc_search__toupper_case_str (NULL, tmp_str, char_len);
  489. g_string_append (dest_str, tmp_string->str);
  490. g_string_free (tmp_string, TRUE);
  491. }
  492. else if (*replace_flags & REPLACE_T_LOW_TRANSFORM_CHAR)
  493. {
  494. *replace_flags &= ~REPLACE_T_LOW_TRANSFORM_CHAR;
  495. tmp_string = mc_search__toupper_case_str (NULL, tmp_str, char_len);
  496. g_string_append (dest_str, tmp_string->str);
  497. g_string_free (tmp_string, TRUE);
  498. }
  499. else if (*replace_flags & REPLACE_T_UPP_TRANSFORM)
  500. {
  501. tmp_string = mc_search__toupper_case_str (NULL, tmp_str, char_len);
  502. g_string_append (dest_str, tmp_string->str);
  503. g_string_free (tmp_string, TRUE);
  504. }
  505. else if (*replace_flags & REPLACE_T_LOW_TRANSFORM)
  506. {
  507. tmp_string = mc_search__tolower_case_str (NULL, tmp_str, char_len);
  508. g_string_append (dest_str, tmp_string->str);
  509. g_string_free (tmp_string, TRUE);
  510. }
  511. else
  512. {
  513. g_string_append (dest_str, tmp_str);
  514. }
  515. g_free (tmp_str);
  516. loop += char_len;
  517. }
  518. }
  519. /* --------------------------------------------------------------------------------------------- */
  520. static void
  521. mc_search_regex__process_escape_sequence (GString * dest_str, const char *from, gsize len,
  522. replace_transform_type_t * replace_flags,
  523. gboolean is_utf8)
  524. {
  525. gsize i = 0;
  526. unsigned int c = 0;
  527. char b;
  528. if (len == (gsize) (-1))
  529. len = strlen (from);
  530. if (len == 0)
  531. return;
  532. if (from[i] == '{')
  533. i++;
  534. if (i >= len)
  535. return;
  536. if (from[i] == 'x')
  537. {
  538. i++;
  539. if (i < len && from[i] == '{')
  540. i++;
  541. for (; i < len; i++)
  542. {
  543. if (from[i] >= '0' && from[i] <= '9')
  544. c = c * 16 + from[i] - '0';
  545. else if (from[i] >= 'a' && from[i] <= 'f')
  546. c = c * 16 + 10 + from[i] - 'a';
  547. else if (from[i] >= 'A' && from[i] <= 'F')
  548. c = c * 16 + 10 + from[i] - 'A';
  549. else
  550. break;
  551. }
  552. }
  553. else if (from[i] >= '0' && from[i] <= '7')
  554. for (; i < len && from[i] >= '0' && from[i] <= '7'; i++)
  555. c = c * 8 + from[i] - '0';
  556. else
  557. {
  558. switch (from[i])
  559. {
  560. case 'n':
  561. c = '\n';
  562. break;
  563. case 't':
  564. c = '\t';
  565. break;
  566. case 'v':
  567. c = '\v';
  568. break;
  569. case 'b':
  570. c = '\b';
  571. break;
  572. case 'r':
  573. c = '\r';
  574. break;
  575. case 'f':
  576. c = '\f';
  577. break;
  578. case 'a':
  579. c = '\a';
  580. break;
  581. default:
  582. mc_search_regex__process_append_str (dest_str, from, len, replace_flags);
  583. return;
  584. }
  585. }
  586. if (c < 0x80 || !is_utf8)
  587. g_string_append_c (dest_str, (char) c);
  588. else if (c < 0x800)
  589. {
  590. b = 0xC0 | (c >> 6);
  591. g_string_append_c (dest_str, b);
  592. b = 0x80 | (c & 0x3F);
  593. g_string_append_c (dest_str, b);
  594. }
  595. else if (c < 0x10000)
  596. {
  597. b = 0xE0 | (c >> 12);
  598. g_string_append_c (dest_str, b);
  599. b = 0x80 | ((c >> 6) & 0x3F);
  600. g_string_append_c (dest_str, b);
  601. b = 0x80 | (c & 0x3F);
  602. g_string_append_c (dest_str, b);
  603. }
  604. else if (c < 0x10FFFF)
  605. {
  606. b = 0xF0 | (c >> 16);
  607. g_string_append_c (dest_str, b);
  608. b = 0x80 | ((c >> 12) & 0x3F);
  609. g_string_append_c (dest_str, b);
  610. b = 0x80 | ((c >> 6) & 0x3F);
  611. g_string_append_c (dest_str, b);
  612. b = 0x80 | (c & 0x3F);
  613. g_string_append_c (dest_str, b);
  614. }
  615. }
  616. /* --------------------------------------------------------------------------------------------- */
  617. /*** public functions ****************************************************************************/
  618. /* --------------------------------------------------------------------------------------------- */
  619. void
  620. mc_search__cond_struct_new_init_regex (const char *charset, mc_search_t * lc_mc_search,
  621. mc_search_cond_t * mc_search_cond)
  622. {
  623. #ifdef SEARCH_TYPE_GLIB
  624. GError *error = NULL;
  625. if (!lc_mc_search->is_case_sensitive)
  626. {
  627. GString *tmp;
  628. tmp = mc_search_cond->str;
  629. mc_search_cond->str = mc_search__cond_struct_new_regex_ci_str (charset, tmp);
  630. g_string_free (tmp, TRUE);
  631. }
  632. mc_search_cond->regex_handle =
  633. g_regex_new (mc_search_cond->str->str, G_REGEX_OPTIMIZE | G_REGEX_RAW | G_REGEX_DOTALL,
  634. 0, &error);
  635. if (error != NULL)
  636. {
  637. lc_mc_search->error = MC_SEARCH_E_REGEX_COMPILE;
  638. lc_mc_search->error_str = str_conv_gerror_message (error, _("Regular expression error"));
  639. g_error_free (error);
  640. return;
  641. }
  642. #else /* SEARCH_TYPE_GLIB */
  643. const char *error;
  644. int erroffset;
  645. int pcre_options = PCRE_EXTRA | PCRE_MULTILINE;
  646. if (str_isutf8 (charset))
  647. {
  648. pcre_options |= PCRE_UTF8;
  649. if (lc_mc_search->is_case_sensitive)
  650. pcre_options |= PCRE_CASELESS;
  651. }
  652. else
  653. {
  654. if (!lc_mc_search->is_case_sensitive)
  655. {
  656. GString *tmp;
  657. tmp = mc_search_cond->str;
  658. mc_search_cond->str = mc_search__cond_struct_new_regex_ci_str (charset, tmp);
  659. g_string_free (tmp, TRUE);
  660. }
  661. }
  662. mc_search_cond->regex_handle =
  663. pcre_compile (mc_search_cond->str->str, pcre_options, &error, &erroffset, NULL);
  664. if (mc_search_cond->regex_handle == NULL)
  665. {
  666. lc_mc_search->error = MC_SEARCH_E_REGEX_COMPILE;
  667. lc_mc_search->error_str = g_strdup (error);
  668. return;
  669. }
  670. lc_mc_search->regex_match_info = pcre_study (mc_search_cond->regex_handle, 0, &error);
  671. if (lc_mc_search->regex_match_info == NULL)
  672. {
  673. if (error)
  674. {
  675. lc_mc_search->error = MC_SEARCH_E_REGEX_COMPILE;
  676. lc_mc_search->error_str = g_strdup (error);
  677. g_free (mc_search_cond->regex_handle);
  678. mc_search_cond->regex_handle = NULL;
  679. return;
  680. }
  681. }
  682. #endif /* SEARCH_TYPE_GLIB */
  683. lc_mc_search->is_utf8 = str_isutf8 (charset);
  684. }
  685. /* --------------------------------------------------------------------------------------------- */
  686. gboolean
  687. mc_search__run_regex (mc_search_t * lc_mc_search, const void *user_data,
  688. gsize start_search, gsize end_search, gsize * found_len)
  689. {
  690. gsize current_pos, virtual_pos;
  691. int current_chr = 0;
  692. gint start_pos;
  693. gint end_pos;
  694. if (lc_mc_search->regex_buffer != NULL)
  695. g_string_free (lc_mc_search->regex_buffer, TRUE);
  696. lc_mc_search->regex_buffer = g_string_sized_new (64);
  697. virtual_pos = current_pos = start_search;
  698. while (virtual_pos <= end_search)
  699. {
  700. g_string_set_size (lc_mc_search->regex_buffer, 0);
  701. lc_mc_search->start_buffer = current_pos;
  702. while (1)
  703. {
  704. current_chr = mc_search__get_char (lc_mc_search, user_data, current_pos);
  705. if (current_chr == MC_SEARCH_CB_ABORT)
  706. break;
  707. if (current_chr == MC_SEARCH_CB_INVALID)
  708. continue;
  709. current_pos++;
  710. if (current_chr == MC_SEARCH_CB_SKIP)
  711. continue;
  712. virtual_pos++;
  713. g_string_append_c (lc_mc_search->regex_buffer, (char) current_chr);
  714. if (current_chr == 0 || (char) current_chr == '\n')
  715. break;
  716. if (virtual_pos > end_search)
  717. break;
  718. }
  719. switch (mc_search__regex_found_cond (lc_mc_search, lc_mc_search->regex_buffer))
  720. {
  721. case COND__FOUND_OK:
  722. #ifdef SEARCH_TYPE_GLIB
  723. if (lc_mc_search->whole_words)
  724. {
  725. g_match_info_fetch_pos (lc_mc_search->regex_match_info, 2, &start_pos, &end_pos);
  726. }
  727. else
  728. {
  729. g_match_info_fetch_pos (lc_mc_search->regex_match_info, 0, &start_pos, &end_pos);
  730. }
  731. #else /* SEARCH_TYPE_GLIB */
  732. if (lc_mc_search->whole_words)
  733. {
  734. start_pos = lc_mc_search->iovector[4];
  735. end_pos = lc_mc_search->iovector[5];
  736. }
  737. else
  738. {
  739. start_pos = lc_mc_search->iovector[0];
  740. end_pos = lc_mc_search->iovector[1];
  741. }
  742. #endif /* SEARCH_TYPE_GLIB */
  743. if (found_len)
  744. *found_len = end_pos - start_pos;
  745. lc_mc_search->normal_offset = lc_mc_search->start_buffer + start_pos;
  746. return TRUE;
  747. case COND__NOT_ALL_FOUND:
  748. break;
  749. default:
  750. g_string_free (lc_mc_search->regex_buffer, TRUE);
  751. lc_mc_search->regex_buffer = NULL;
  752. return FALSE;
  753. }
  754. if ((lc_mc_search->update_fn != NULL) &&
  755. ((lc_mc_search->update_fn) (user_data, current_pos) == MC_SEARCH_CB_ABORT))
  756. current_chr = MC_SEARCH_CB_ABORT;
  757. if (current_chr == MC_SEARCH_CB_ABORT)
  758. break;
  759. }
  760. g_string_free (lc_mc_search->regex_buffer, TRUE);
  761. lc_mc_search->regex_buffer = NULL;
  762. lc_mc_search->error = MC_SEARCH_E_NOTFOUND;
  763. if (current_chr != MC_SEARCH_CB_ABORT)
  764. lc_mc_search->error_str = g_strdup (_(STR_E_NOTFOUND));
  765. else
  766. lc_mc_search->error_str = NULL;
  767. return FALSE;
  768. }
  769. /* --------------------------------------------------------------------------------------------- */
  770. GString *
  771. mc_search_regex_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str)
  772. {
  773. GString *ret;
  774. gchar *tmp_str;
  775. int num_replace_tokens, lc_index;
  776. gsize loop;
  777. gsize len = 0;
  778. gchar *prev_str;
  779. replace_transform_type_t replace_flags = REPLACE_T_NO_TRANSFORM;
  780. num_replace_tokens =
  781. mc_search_regex__get_max_num_of_replace_tokens (replace_str->str, replace_str->len);
  782. if (lc_mc_search->num_results < 0)
  783. return g_string_new_len (replace_str->str, replace_str->len);
  784. if (num_replace_tokens > lc_mc_search->num_results - 1
  785. || num_replace_tokens > MC_SEARCH__NUM_REPLACE_ARGS)
  786. {
  787. lc_mc_search->error = MC_SEARCH_E_REGEX_REPLACE;
  788. lc_mc_search->error_str = g_strdup (_(STR_E_RPL_NOT_EQ_TO_FOUND));
  789. return NULL;
  790. }
  791. ret = g_string_sized_new (64);
  792. prev_str = replace_str->str;
  793. for (loop = 0; loop < replace_str->len - 1; loop++)
  794. {
  795. lc_index = mc_search_regex__process_replace_str (replace_str, loop, &len, &replace_flags);
  796. if (lc_index == REPLACE_PREPARE_T_NOTHING_SPECIAL)
  797. {
  798. if (len != 0)
  799. {
  800. mc_search_regex__process_append_str (ret, prev_str,
  801. replace_str->str - prev_str + loop,
  802. &replace_flags);
  803. mc_search_regex__process_append_str (ret, replace_str->str + loop + 1, len - 1,
  804. &replace_flags);
  805. prev_str = replace_str->str + loop + len;
  806. loop += len - 1;
  807. }
  808. continue;
  809. }
  810. if (lc_index == REPLACE_PREPARE_T_REPLACE_FLAG)
  811. {
  812. if (loop)
  813. mc_search_regex__process_append_str (ret, prev_str,
  814. replace_str->str - prev_str + loop,
  815. &replace_flags);
  816. prev_str = replace_str->str + loop + len;
  817. loop += len - 1;
  818. continue;
  819. }
  820. /* escape sequence */
  821. if (lc_index == REPLACE_PREPARE_T_ESCAPE_SEQ)
  822. {
  823. mc_search_regex__process_append_str (ret, prev_str,
  824. replace_str->str + loop - prev_str,
  825. &replace_flags);
  826. /* call process_escape_sequence without starting '\\' */
  827. mc_search_regex__process_escape_sequence (ret, replace_str->str + loop + 1, len - 1,
  828. &replace_flags, lc_mc_search->is_utf8);
  829. prev_str = replace_str->str + loop + len;
  830. loop += len - 1;
  831. continue;
  832. }
  833. /* invalid capture buffer number */
  834. if (lc_index > lc_mc_search->num_results)
  835. {
  836. g_string_free (ret, TRUE);
  837. lc_mc_search->error = MC_SEARCH_E_REGEX_REPLACE;
  838. lc_mc_search->error_str = g_strdup_printf (_(STR_E_RPL_INVALID_TOKEN), lc_index);
  839. return NULL;
  840. }
  841. tmp_str = mc_search_regex__get_token_by_num (lc_mc_search, lc_index);
  842. if (tmp_str == NULL)
  843. continue;
  844. if (loop)
  845. mc_search_regex__process_append_str (ret, prev_str, replace_str->str - prev_str + loop,
  846. &replace_flags);
  847. prev_str = replace_str->str + loop + len;
  848. mc_search_regex__process_append_str (ret, tmp_str, -1, &replace_flags);
  849. g_free (tmp_str);
  850. loop += len - 1;
  851. }
  852. mc_search_regex__process_append_str (ret, prev_str,
  853. replace_str->str - prev_str + replace_str->len,
  854. &replace_flags);
  855. return ret;
  856. }