regex.c 31 KB

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