regex.c 35 KB

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