regex.c 36 KB

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