regex.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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
  215. (regex, search_str->str, -1, 0, G_REGEX_MATCH_NEWLINE_ANY, &lc_mc_search->regex_match_info,
  216. &error))
  217. {
  218. g_match_info_free (lc_mc_search->regex_match_info);
  219. lc_mc_search->regex_match_info = NULL;
  220. if (error)
  221. {
  222. lc_mc_search->error = MC_SEARCH_E_REGEX;
  223. lc_mc_search->error_str =
  224. str_conv_gerror_message (error, _("Regular expression error"));
  225. g_error_free (error);
  226. return COND__FOUND_ERROR;
  227. }
  228. return COND__NOT_FOUND;
  229. }
  230. lc_mc_search->num_results = g_match_info_get_match_count (lc_mc_search->regex_match_info);
  231. #else /* SEARCH_TYPE_GLIB */
  232. lc_mc_search->num_results = pcre_exec (regex, lc_mc_search->regex_match_info,
  233. search_str->str, search_str->len - 1, 0, 0,
  234. lc_mc_search->iovector, MC_SEARCH__NUM_REPLACE_ARGS);
  235. if (lc_mc_search->num_results < 0)
  236. {
  237. return COND__NOT_FOUND;
  238. }
  239. #endif /* SEARCH_TYPE_GLIB */
  240. return COND__FOUND_OK;
  241. }
  242. /* --------------------------------------------------------------------------------------------- */
  243. static mc_search__found_cond_t
  244. mc_search__regex_found_cond (mc_search_t * lc_mc_search, GString * search_str)
  245. {
  246. gsize loop1;
  247. mc_search_cond_t *mc_search_cond;
  248. mc_search__found_cond_t ret;
  249. for (loop1 = 0; loop1 < lc_mc_search->conditions->len; loop1++)
  250. {
  251. mc_search_cond = (mc_search_cond_t *) g_ptr_array_index (lc_mc_search->conditions, loop1);
  252. if (!mc_search_cond->regex_handle)
  253. continue;
  254. ret =
  255. mc_search__regex_found_cond_one (lc_mc_search, mc_search_cond->regex_handle,
  256. search_str);
  257. if (ret != COND__NOT_FOUND)
  258. return ret;
  259. }
  260. return COND__NOT_ALL_FOUND;
  261. }
  262. /* --------------------------------------------------------------------------------------------- */
  263. static int
  264. mc_search_regex__get_max_num_of_replace_tokens (const gchar * str, gsize len)
  265. {
  266. int max_token = 0;
  267. gsize loop;
  268. for (loop = 0; loop < len - 1; loop++)
  269. {
  270. if (str[loop] == '\\' && g_ascii_isdigit (str[loop + 1]))
  271. {
  272. if (strutils_is_char_escaped (str, &str[loop]))
  273. continue;
  274. if (max_token < str[loop + 1] - '0')
  275. max_token = str[loop + 1] - '0';
  276. continue;
  277. }
  278. if (str[loop] == '$' && str[loop + 1] == '{')
  279. {
  280. gsize tmp_len;
  281. char *tmp_str;
  282. int tmp_token;
  283. if (strutils_is_char_escaped (str, &str[loop]))
  284. continue;
  285. for (tmp_len = 0;
  286. loop + tmp_len + 2 < len && (str[loop + 2 + tmp_len] & (char) 0xf0) == 0x30;
  287. tmp_len++);
  288. if (str[loop + 2 + tmp_len] == '}')
  289. {
  290. tmp_str = g_strndup (&str[loop + 2], tmp_len);
  291. tmp_token = atoi (tmp_str);
  292. if (max_token < tmp_token)
  293. max_token = tmp_token;
  294. g_free (tmp_str);
  295. }
  296. }
  297. }
  298. return max_token;
  299. }
  300. /* --------------------------------------------------------------------------------------------- */
  301. static char *
  302. mc_search_regex__get_token_by_num (const mc_search_t * lc_mc_search, gsize lc_index)
  303. {
  304. int fnd_start = 0, fnd_end = 0;
  305. #ifdef SEARCH_TYPE_GLIB
  306. g_match_info_fetch_pos (lc_mc_search->regex_match_info, lc_index, &fnd_start, &fnd_end);
  307. #else /* SEARCH_TYPE_GLIB */
  308. fnd_start = lc_mc_search->iovector[lc_index * 2 + 0];
  309. fnd_end = lc_mc_search->iovector[lc_index * 2 + 1];
  310. #endif /* SEARCH_TYPE_GLIB */
  311. if (fnd_end - fnd_start == 0)
  312. return NULL;
  313. return g_strndup (lc_mc_search->regex_buffer->str + fnd_start, fnd_end - fnd_start);
  314. }
  315. /* --------------------------------------------------------------------------------------------- */
  316. static gboolean
  317. mc_search_regex__replace_handle_esc_seq (const GString * replace_str, const gsize current_pos,
  318. gsize * skip_len, int *ret)
  319. {
  320. char *curr_str = &(replace_str->str[current_pos]);
  321. char c = *(curr_str + 1);
  322. if (replace_str->len > current_pos + 2)
  323. {
  324. if (c == '{')
  325. {
  326. for (*skip_len = 2; /* \{ */
  327. current_pos + *skip_len < replace_str->len
  328. && *(curr_str + *skip_len) >= '0'
  329. && *(curr_str + *skip_len) <= '7'; (*skip_len)++);
  330. if (current_pos + *skip_len < replace_str->len && *(curr_str + *skip_len) == '}')
  331. {
  332. (*skip_len)++;
  333. *ret = REPLACE_PREPARE_T_ESCAPE_SEQ;
  334. return FALSE;
  335. }
  336. else
  337. {
  338. *ret = REPLACE_PREPARE_T_NOTHING_SPECIAL;
  339. return TRUE;
  340. }
  341. }
  342. if (c == 'x')
  343. {
  344. *skip_len = 2; /* \x */
  345. c = *(curr_str + 2);
  346. if (c == '{')
  347. {
  348. for (*skip_len = 3; /* \x{ */
  349. current_pos + *skip_len < replace_str->len
  350. && g_ascii_isxdigit ((guchar) * (curr_str + *skip_len)); (*skip_len)++);
  351. if (current_pos + *skip_len < replace_str->len && *(curr_str + *skip_len) == '}')
  352. {
  353. (*skip_len)++;
  354. *ret = REPLACE_PREPARE_T_ESCAPE_SEQ;
  355. return FALSE;
  356. }
  357. else
  358. {
  359. *ret = REPLACE_PREPARE_T_NOTHING_SPECIAL;
  360. return TRUE;
  361. }
  362. }
  363. else if (!g_ascii_isxdigit ((guchar) c))
  364. {
  365. *skip_len = 2; /* \x without number behind */
  366. *ret = REPLACE_PREPARE_T_NOTHING_SPECIAL;
  367. return FALSE;
  368. }
  369. else
  370. {
  371. c = *(curr_str + 3);
  372. if (!g_ascii_isxdigit ((guchar) c))
  373. *skip_len = 3; /* \xH */
  374. else
  375. *skip_len = 4; /* \xHH */
  376. *ret = REPLACE_PREPARE_T_ESCAPE_SEQ;
  377. return FALSE;
  378. }
  379. }
  380. }
  381. if (strchr ("ntvbrfa", c) != NULL)
  382. {
  383. *skip_len = 2;
  384. *ret = REPLACE_PREPARE_T_ESCAPE_SEQ;
  385. return FALSE;
  386. }
  387. return TRUE;
  388. }
  389. /* --------------------------------------------------------------------------------------------- */
  390. static int
  391. mc_search_regex__process_replace_str (const GString * replace_str, const gsize current_pos,
  392. gsize * skip_len, replace_transform_type_t * replace_flags)
  393. {
  394. int ret = -1;
  395. char *tmp_str;
  396. const char *curr_str = &(replace_str->str[current_pos]);
  397. if (current_pos > replace_str->len)
  398. return REPLACE_PREPARE_T_NOTHING_SPECIAL;
  399. *skip_len = 0;
  400. if ((*curr_str == '$') && (*(curr_str + 1) == '{') && ((*(curr_str + 2) & (char) 0xf0) == 0x30)
  401. && (replace_str->len > current_pos + 2))
  402. {
  403. if (strutils_is_char_escaped (replace_str->str, curr_str))
  404. {
  405. *skip_len = 1;
  406. return REPLACE_PREPARE_T_NOTHING_SPECIAL;
  407. }
  408. for (*skip_len = 0;
  409. current_pos + *skip_len + 2 < replace_str->len
  410. && (*(curr_str + 2 + *skip_len) & (char) 0xf0) == 0x30; (*skip_len)++);
  411. if (*(curr_str + 2 + *skip_len) != '}')
  412. return REPLACE_PREPARE_T_NOTHING_SPECIAL;
  413. tmp_str = g_strndup (curr_str + 2, *skip_len);
  414. if (tmp_str == NULL)
  415. return REPLACE_PREPARE_T_NOTHING_SPECIAL;
  416. ret = atoi (tmp_str);
  417. g_free (tmp_str);
  418. *skip_len += 3; /* ${} */
  419. return ret; /* capture buffer index >= 0 */
  420. }
  421. if ((*curr_str == '\\') && (replace_str->len > current_pos + 1))
  422. {
  423. if (strutils_is_char_escaped (replace_str->str, curr_str))
  424. {
  425. *skip_len = 1;
  426. return REPLACE_PREPARE_T_NOTHING_SPECIAL;
  427. }
  428. if (g_ascii_isdigit (*(curr_str + 1)))
  429. {
  430. ret = g_ascii_digit_value (*(curr_str + 1)); /* capture buffer index >= 0 */
  431. *skip_len = 2; /* \\ and one digit */
  432. return ret;
  433. }
  434. if (!mc_search_regex__replace_handle_esc_seq (replace_str, current_pos, skip_len, &ret))
  435. return ret;
  436. ret = REPLACE_PREPARE_T_REPLACE_FLAG;
  437. *skip_len += 2;
  438. switch (*(curr_str + 1))
  439. {
  440. case 'U':
  441. *replace_flags |= REPLACE_T_UPP_TRANSFORM;
  442. *replace_flags &= ~REPLACE_T_LOW_TRANSFORM;
  443. break;
  444. case 'u':
  445. *replace_flags |= REPLACE_T_UPP_TRANSFORM_CHAR;
  446. break;
  447. case 'L':
  448. *replace_flags |= REPLACE_T_LOW_TRANSFORM;
  449. *replace_flags &= ~REPLACE_T_UPP_TRANSFORM;
  450. break;
  451. case 'l':
  452. *replace_flags |= REPLACE_T_LOW_TRANSFORM_CHAR;
  453. break;
  454. case 'E':
  455. *replace_flags = REPLACE_T_NO_TRANSFORM;
  456. break;
  457. default:
  458. ret = REPLACE_PREPARE_T_NOTHING_SPECIAL;
  459. break;
  460. }
  461. }
  462. return ret;
  463. }
  464. /* --------------------------------------------------------------------------------------------- */
  465. static void
  466. mc_search_regex__process_append_str (GString * dest_str, const char *from, gsize len,
  467. replace_transform_type_t * replace_flags)
  468. {
  469. gsize loop = 0;
  470. gsize char_len;
  471. char *tmp_str;
  472. GString *tmp_string;
  473. if (len == (gsize) - 1)
  474. len = strlen (from);
  475. if (*replace_flags == REPLACE_T_NO_TRANSFORM)
  476. {
  477. g_string_append_len (dest_str, from, len);
  478. return;
  479. }
  480. while (loop < len)
  481. {
  482. tmp_str = mc_search__get_one_symbol (NULL, from + loop, len - loop, NULL);
  483. char_len = strlen (tmp_str);
  484. if (*replace_flags & REPLACE_T_UPP_TRANSFORM_CHAR)
  485. {
  486. *replace_flags &= ~REPLACE_T_UPP_TRANSFORM_CHAR;
  487. tmp_string = mc_search__toupper_case_str (NULL, tmp_str, char_len);
  488. g_string_append (dest_str, tmp_string->str);
  489. g_string_free (tmp_string, TRUE);
  490. }
  491. else if (*replace_flags & REPLACE_T_LOW_TRANSFORM_CHAR)
  492. {
  493. *replace_flags &= ~REPLACE_T_LOW_TRANSFORM_CHAR;
  494. tmp_string = mc_search__toupper_case_str (NULL, tmp_str, char_len);
  495. g_string_append (dest_str, tmp_string->str);
  496. g_string_free (tmp_string, TRUE);
  497. }
  498. else if (*replace_flags & REPLACE_T_UPP_TRANSFORM)
  499. {
  500. tmp_string = mc_search__toupper_case_str (NULL, tmp_str, char_len);
  501. g_string_append (dest_str, tmp_string->str);
  502. g_string_free (tmp_string, TRUE);
  503. }
  504. else if (*replace_flags & REPLACE_T_LOW_TRANSFORM)
  505. {
  506. tmp_string = mc_search__tolower_case_str (NULL, tmp_str, char_len);
  507. g_string_append (dest_str, tmp_string->str);
  508. g_string_free (tmp_string, TRUE);
  509. }
  510. else
  511. {
  512. g_string_append (dest_str, tmp_str);
  513. }
  514. g_free (tmp_str);
  515. loop += char_len;
  516. }
  517. }
  518. /* --------------------------------------------------------------------------------------------- */
  519. static void
  520. mc_search_regex__process_escape_sequence (GString * dest_str, const char *from, gsize len,
  521. replace_transform_type_t * replace_flags,
  522. gboolean is_utf8)
  523. {
  524. gsize i = 0;
  525. unsigned int c = 0;
  526. char b;
  527. if (len == (gsize) (-1))
  528. len = strlen (from);
  529. if (len == 0)
  530. return;
  531. if (from[i] == '{')
  532. i++;
  533. if (i >= len)
  534. return;
  535. if (from[i] == 'x')
  536. {
  537. i++;
  538. if (i < len && from[i] == '{')
  539. i++;
  540. for (; i < len; i++)
  541. {
  542. if (from[i] >= '0' && from[i] <= '9')
  543. c = c * 16 + from[i] - '0';
  544. else if (from[i] >= 'a' && from[i] <= 'f')
  545. c = c * 16 + 10 + from[i] - 'a';
  546. else if (from[i] >= 'A' && from[i] <= 'F')
  547. c = c * 16 + 10 + from[i] - 'A';
  548. else
  549. break;
  550. }
  551. }
  552. else if (from[i] >= '0' && from[i] <= '7')
  553. for (; i < len && from[i] >= '0' && from[i] <= '7'; i++)
  554. c = c * 8 + from[i] - '0';
  555. else
  556. {
  557. switch (from[i])
  558. {
  559. case 'n':
  560. c = '\n';
  561. break;
  562. case 't':
  563. c = '\t';
  564. break;
  565. case 'v':
  566. c = '\v';
  567. break;
  568. case 'b':
  569. c = '\b';
  570. break;
  571. case 'r':
  572. c = '\r';
  573. break;
  574. case 'f':
  575. c = '\f';
  576. break;
  577. case 'a':
  578. c = '\a';
  579. break;
  580. default:
  581. mc_search_regex__process_append_str (dest_str, from, len, replace_flags);
  582. return;
  583. }
  584. }
  585. if (c < 0x80 || !is_utf8)
  586. g_string_append_c (dest_str, (char) c);
  587. else if (c < 0x800)
  588. {
  589. b = 0xC0 | (c >> 6);
  590. g_string_append_c (dest_str, b);
  591. b = 0x80 | (c & 0x3F);
  592. g_string_append_c (dest_str, b);
  593. }
  594. else if (c < 0x10000)
  595. {
  596. b = 0xE0 | (c >> 12);
  597. g_string_append_c (dest_str, b);
  598. b = 0x80 | ((c >> 6) & 0x3F);
  599. g_string_append_c (dest_str, b);
  600. b = 0x80 | (c & 0x3F);
  601. g_string_append_c (dest_str, b);
  602. }
  603. else if (c < 0x10FFFF)
  604. {
  605. b = 0xF0 | (c >> 16);
  606. g_string_append_c (dest_str, b);
  607. b = 0x80 | ((c >> 12) & 0x3F);
  608. g_string_append_c (dest_str, b);
  609. b = 0x80 | ((c >> 6) & 0x3F);
  610. g_string_append_c (dest_str, b);
  611. b = 0x80 | (c & 0x3F);
  612. g_string_append_c (dest_str, b);
  613. }
  614. }
  615. /* --------------------------------------------------------------------------------------------- */
  616. /*** public functions ****************************************************************************/
  617. /* --------------------------------------------------------------------------------------------- */
  618. void
  619. mc_search__cond_struct_new_init_regex (const char *charset, mc_search_t * lc_mc_search,
  620. mc_search_cond_t * mc_search_cond)
  621. {
  622. #ifdef SEARCH_TYPE_GLIB
  623. GError *error = NULL;
  624. if (!lc_mc_search->is_case_sensitive)
  625. {
  626. GString *tmp;
  627. tmp = mc_search_cond->str;
  628. mc_search_cond->str = mc_search__cond_struct_new_regex_ci_str (charset, tmp);
  629. g_string_free (tmp, TRUE);
  630. }
  631. mc_search_cond->regex_handle =
  632. g_regex_new (mc_search_cond->str->str, G_REGEX_OPTIMIZE | G_REGEX_RAW | G_REGEX_DOTALL,
  633. 0, &error);
  634. if (error != NULL)
  635. {
  636. lc_mc_search->error = MC_SEARCH_E_REGEX_COMPILE;
  637. lc_mc_search->error_str = str_conv_gerror_message (error, _("Regular expression error"));
  638. g_error_free (error);
  639. return;
  640. }
  641. #else /* SEARCH_TYPE_GLIB */
  642. const char *error;
  643. int erroffset;
  644. int pcre_options = PCRE_EXTRA | PCRE_MULTILINE;
  645. if (str_isutf8 (charset) && mc_global.utf8_display)
  646. {
  647. pcre_options |= PCRE_UTF8;
  648. if (lc_mc_search->is_case_sensitive)
  649. pcre_options |= PCRE_CASELESS;
  650. }
  651. else
  652. {
  653. if (!lc_mc_search->is_case_sensitive)
  654. {
  655. GString *tmp;
  656. tmp = mc_search_cond->str;
  657. mc_search_cond->str = mc_search__cond_struct_new_regex_ci_str (charset, tmp);
  658. g_string_free (tmp, TRUE);
  659. }
  660. }
  661. mc_search_cond->regex_handle =
  662. pcre_compile (mc_search_cond->str->str, pcre_options, &error, &erroffset, NULL);
  663. if (mc_search_cond->regex_handle == NULL)
  664. {
  665. lc_mc_search->error = MC_SEARCH_E_REGEX_COMPILE;
  666. lc_mc_search->error_str = g_strdup (error);
  667. return;
  668. }
  669. lc_mc_search->regex_match_info = pcre_study (mc_search_cond->regex_handle, 0, &error);
  670. if (lc_mc_search->regex_match_info == NULL)
  671. {
  672. if (error)
  673. {
  674. lc_mc_search->error = MC_SEARCH_E_REGEX_COMPILE;
  675. lc_mc_search->error_str = g_strdup (error);
  676. g_free (mc_search_cond->regex_handle);
  677. mc_search_cond->regex_handle = NULL;
  678. return;
  679. }
  680. }
  681. #endif /* SEARCH_TYPE_GLIB */
  682. lc_mc_search->is_utf8 = str_isutf8 (charset);
  683. }
  684. /* --------------------------------------------------------------------------------------------- */
  685. gboolean
  686. mc_search__run_regex (mc_search_t * lc_mc_search, const void *user_data,
  687. gsize start_search, gsize end_search, gsize * found_len)
  688. {
  689. gsize current_pos, virtual_pos;
  690. int current_chr = 0;
  691. gint start_pos;
  692. gint end_pos;
  693. if (lc_mc_search->regex_buffer != NULL)
  694. g_string_free (lc_mc_search->regex_buffer, TRUE);
  695. lc_mc_search->regex_buffer = g_string_sized_new (64);
  696. virtual_pos = current_pos = start_search;
  697. while (virtual_pos <= end_search)
  698. {
  699. g_string_set_size (lc_mc_search->regex_buffer, 0);
  700. lc_mc_search->start_buffer = current_pos;
  701. while (1)
  702. {
  703. current_chr = mc_search__get_char (lc_mc_search, user_data, current_pos);
  704. if (current_chr == MC_SEARCH_CB_ABORT)
  705. break;
  706. if (current_chr == MC_SEARCH_CB_INVALID)
  707. continue;
  708. current_pos++;
  709. if (current_chr == MC_SEARCH_CB_SKIP)
  710. continue;
  711. virtual_pos++;
  712. g_string_append_c (lc_mc_search->regex_buffer, (char) current_chr);
  713. if (current_chr == 0 || (char) current_chr == '\n')
  714. break;
  715. if (virtual_pos > end_search)
  716. break;
  717. }
  718. switch (mc_search__regex_found_cond (lc_mc_search, lc_mc_search->regex_buffer))
  719. {
  720. case COND__FOUND_OK:
  721. #ifdef SEARCH_TYPE_GLIB
  722. if (lc_mc_search->whole_words)
  723. {
  724. g_match_info_fetch_pos (lc_mc_search->regex_match_info, 2, &start_pos, &end_pos);
  725. }
  726. else
  727. {
  728. g_match_info_fetch_pos (lc_mc_search->regex_match_info, 0, &start_pos, &end_pos);
  729. }
  730. #else /* SEARCH_TYPE_GLIB */
  731. if (lc_mc_search->whole_words)
  732. {
  733. start_pos = lc_mc_search->iovector[4];
  734. end_pos = lc_mc_search->iovector[5];
  735. }
  736. else
  737. {
  738. start_pos = lc_mc_search->iovector[0];
  739. end_pos = lc_mc_search->iovector[1];
  740. }
  741. #endif /* SEARCH_TYPE_GLIB */
  742. if (found_len)
  743. *found_len = end_pos - start_pos;
  744. lc_mc_search->normal_offset = lc_mc_search->start_buffer + start_pos;
  745. return TRUE;
  746. case COND__NOT_ALL_FOUND:
  747. break;
  748. default:
  749. g_string_free (lc_mc_search->regex_buffer, TRUE);
  750. lc_mc_search->regex_buffer = NULL;
  751. return FALSE;
  752. }
  753. if ((lc_mc_search->update_fn != NULL) &&
  754. ((lc_mc_search->update_fn) (user_data, current_pos) == MC_SEARCH_CB_ABORT))
  755. current_chr = MC_SEARCH_CB_ABORT;
  756. if (current_chr == MC_SEARCH_CB_ABORT)
  757. break;
  758. }
  759. g_string_free (lc_mc_search->regex_buffer, TRUE);
  760. lc_mc_search->regex_buffer = NULL;
  761. lc_mc_search->error = MC_SEARCH_E_NOTFOUND;
  762. if (current_chr != MC_SEARCH_CB_ABORT)
  763. lc_mc_search->error_str = g_strdup (_(STR_E_NOTFOUND));
  764. else
  765. lc_mc_search->error_str = NULL;
  766. return FALSE;
  767. }
  768. /* --------------------------------------------------------------------------------------------- */
  769. GString *
  770. mc_search_regex_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str)
  771. {
  772. GString *ret;
  773. gchar *tmp_str;
  774. int num_replace_tokens, lc_index;
  775. gsize loop;
  776. gsize len = 0;
  777. gchar *prev_str;
  778. replace_transform_type_t replace_flags = REPLACE_T_NO_TRANSFORM;
  779. num_replace_tokens =
  780. mc_search_regex__get_max_num_of_replace_tokens (replace_str->str, replace_str->len);
  781. if (lc_mc_search->num_results < 0)
  782. return g_string_new_len (replace_str->str, replace_str->len);
  783. if (num_replace_tokens > lc_mc_search->num_results - 1
  784. || num_replace_tokens > MC_SEARCH__NUM_REPLACE_ARGS)
  785. {
  786. lc_mc_search->error = MC_SEARCH_E_REGEX_REPLACE;
  787. lc_mc_search->error_str = g_strdup (_(STR_E_RPL_NOT_EQ_TO_FOUND));
  788. return NULL;
  789. }
  790. ret = g_string_sized_new (64);
  791. prev_str = replace_str->str;
  792. for (loop = 0; loop < replace_str->len - 1; loop++)
  793. {
  794. lc_index = mc_search_regex__process_replace_str (replace_str, loop, &len, &replace_flags);
  795. if (lc_index == REPLACE_PREPARE_T_NOTHING_SPECIAL)
  796. {
  797. if (len != 0)
  798. {
  799. mc_search_regex__process_append_str (ret, prev_str,
  800. replace_str->str - prev_str + loop,
  801. &replace_flags);
  802. mc_search_regex__process_append_str (ret, replace_str->str + loop + 1, len - 1,
  803. &replace_flags);
  804. prev_str = replace_str->str + loop + len;
  805. loop += len - 1;
  806. }
  807. continue;
  808. }
  809. if (lc_index == REPLACE_PREPARE_T_REPLACE_FLAG)
  810. {
  811. if (loop)
  812. mc_search_regex__process_append_str (ret, prev_str,
  813. replace_str->str - prev_str + loop,
  814. &replace_flags);
  815. prev_str = replace_str->str + loop + len;
  816. loop += len - 1;
  817. continue;
  818. }
  819. /* escape sequence */
  820. if (lc_index == REPLACE_PREPARE_T_ESCAPE_SEQ)
  821. {
  822. mc_search_regex__process_append_str (ret, prev_str,
  823. replace_str->str + loop - prev_str,
  824. &replace_flags);
  825. /* call process_escape_sequence without starting '\\' */
  826. mc_search_regex__process_escape_sequence (ret, replace_str->str + loop + 1, len - 1,
  827. &replace_flags, lc_mc_search->is_utf8);
  828. prev_str = replace_str->str + loop + len;
  829. loop += len - 1;
  830. continue;
  831. }
  832. /* invalid capture buffer number */
  833. if (lc_index > lc_mc_search->num_results)
  834. {
  835. g_string_free (ret, TRUE);
  836. lc_mc_search->error = MC_SEARCH_E_REGEX_REPLACE;
  837. lc_mc_search->error_str = g_strdup_printf (_(STR_E_RPL_INVALID_TOKEN), lc_index);
  838. return NULL;
  839. }
  840. tmp_str = mc_search_regex__get_token_by_num (lc_mc_search, lc_index);
  841. if (tmp_str == NULL)
  842. continue;
  843. if (loop)
  844. mc_search_regex__process_append_str (ret, prev_str, replace_str->str - prev_str + loop,
  845. &replace_flags);
  846. prev_str = replace_str->str + loop + len;
  847. mc_search_regex__process_append_str (ret, tmp_str, -1, &replace_flags);
  848. g_free (tmp_str);
  849. loop += len - 1;
  850. }
  851. mc_search_regex__process_append_str (ret, prev_str,
  852. replace_str->str - prev_str + replace_str->len,
  853. &replace_flags);
  854. return ret;
  855. }