regex.c 31 KB

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