root/lib/search/regex.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. mc_search__regex_str_append_if_special
  2. mc_search__cond_struct_new_regex_hex_add
  3. mc_search__cond_struct_new_regex_accum_append
  4. mc_search__cond_struct_new_regex_ci_str
  5. mc_search__g_regex_match_full_safe
  6. mc_search__regex_found_cond_one
  7. mc_search__regex_found_cond
  8. mc_search_regex__get_max_num_of_replace_tokens
  9. mc_search_regex__get_token_by_num
  10. mc_search_regex__replace_handle_esc_seq
  11. mc_search_regex__process_replace_str
  12. mc_search_regex__process_append_str
  13. mc_search_regex__process_escape_sequence
  14. mc_search__cond_struct_new_init_regex
  15. mc_search__run_regex
  16. mc_search_regex_prepare_replace_str

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

/* [previous][next][first][last][top][bottom][index][help]  */