regex.c 32 KB

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