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-2019
   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);
 292     memcpy (string_safe, string, string_len);
 293     end = p + string_len;
 294 
 295     while (p < end)
 296     {
 297         gunichar c = g_utf8_get_char_validated (p, -1);
 298         if (c != (gunichar) (-1) && c != (gunichar) (-2))
 299         {
 300             p = g_utf8_next_char (p);
 301         }
 302         else
 303         {
 304             /* U+FFFD would be the proper choice, but then we'd have to
 305                maintain mapping between old and new offsets.
 306                So rather do a byte by byte replacement. */
 307             *p++ = '\0';
 308         }
 309     }
 310 
 311     ret =
 312         g_regex_match_full (regex, string_safe, string_len, start_position, match_options,
 313                             match_info, error);
 314     g_free (string_safe);
 315     return ret;
 316 }
 317 #endif /* SEARCH_TYPE_GLIB */
 318 
 319 /* --------------------------------------------------------------------------------------------- */
 320 
 321 static mc_search__found_cond_t
 322 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]  */
 323                                  GString * search_str)
 324 {
 325 #ifdef SEARCH_TYPE_GLIB
 326     GError *mcerror = NULL;
 327 
 328     if (!mc_search__g_regex_match_full_safe
 329         (regex, search_str->str, search_str->len, 0, G_REGEX_MATCH_NEWLINE_ANY,
 330          &lc_mc_search->regex_match_info, &mcerror))
 331     {
 332         g_match_info_free (lc_mc_search->regex_match_info);
 333         lc_mc_search->regex_match_info = NULL;
 334         if (mcerror != NULL)
 335         {
 336             lc_mc_search->error = MC_SEARCH_E_REGEX;
 337             g_free (lc_mc_search->error_str);
 338             lc_mc_search->error_str =
 339                 str_conv_gerror_message (mcerror, _("Regular expression error"));
 340             g_error_free (mcerror);
 341             return COND__FOUND_ERROR;
 342         }
 343         return COND__NOT_FOUND;
 344     }
 345     lc_mc_search->num_results = g_match_info_get_match_count (lc_mc_search->regex_match_info);
 346 #else /* SEARCH_TYPE_GLIB */
 347     lc_mc_search->num_results = pcre_exec (regex, lc_mc_search->regex_match_info,
 348                                            search_str->str, search_str->len, 0, 0,
 349                                            lc_mc_search->iovector, MC_SEARCH__NUM_REPLACE_ARGS);
 350     if (lc_mc_search->num_results < 0)
 351     {
 352         return COND__NOT_FOUND;
 353     }
 354 #endif /* SEARCH_TYPE_GLIB */
 355     return COND__FOUND_OK;
 356 
 357 }
 358 
 359 /* --------------------------------------------------------------------------------------------- */
 360 
 361 static mc_search__found_cond_t
 362 mc_search__regex_found_cond (mc_search_t * lc_mc_search, GString * search_str)
     /* [previous][next][first][last][top][bottom][index][help]  */
 363 {
 364     gsize loop1;
 365 
 366     for (loop1 = 0; loop1 < lc_mc_search->conditions->len; loop1++)
 367     {
 368         mc_search_cond_t *mc_search_cond;
 369         mc_search__found_cond_t ret;
 370 
 371         mc_search_cond = (mc_search_cond_t *) g_ptr_array_index (lc_mc_search->conditions, loop1);
 372 
 373         if (!mc_search_cond->regex_handle)
 374             continue;
 375 
 376         ret =
 377             mc_search__regex_found_cond_one (lc_mc_search, mc_search_cond->regex_handle,
 378                                              search_str);
 379         if (ret != COND__NOT_FOUND)
 380             return ret;
 381     }
 382     return COND__NOT_ALL_FOUND;
 383 }
 384 
 385 /* --------------------------------------------------------------------------------------------- */
 386 
 387 static int
 388 mc_search_regex__get_max_num_of_replace_tokens (const gchar * str, gsize len)
     /* [previous][next][first][last][top][bottom][index][help]  */
 389 {
 390     int max_token = 0;
 391     gsize loop;
 392     for (loop = 0; loop < len - 1; loop++)
 393     {
 394         if (str[loop] == '\\' && g_ascii_isdigit (str[loop + 1]))
 395         {
 396             if (strutils_is_char_escaped (str, &str[loop]))
 397                 continue;
 398             if (max_token < str[loop + 1] - '0')
 399                 max_token = str[loop + 1] - '0';
 400             continue;
 401         }
 402         if (str[loop] == '$' && str[loop + 1] == '{')
 403         {
 404             gsize tmp_len;
 405 
 406             if (strutils_is_char_escaped (str, &str[loop]))
 407                 continue;
 408 
 409             for (tmp_len = 0;
 410                  loop + tmp_len + 2 < len && (str[loop + 2 + tmp_len] & (char) 0xf0) == 0x30;
 411                  tmp_len++);
 412 
 413             if (str[loop + 2 + tmp_len] == '}')
 414             {
 415                 int tmp_token;
 416                 char *tmp_str;
 417 
 418                 tmp_str = g_strndup (&str[loop + 2], tmp_len);
 419                 tmp_token = atoi (tmp_str);
 420                 if (max_token < tmp_token)
 421                     max_token = tmp_token;
 422                 g_free (tmp_str);
 423             }
 424         }
 425     }
 426     return max_token;
 427 }
 428 
 429 /* --------------------------------------------------------------------------------------------- */
 430 
 431 static char *
 432 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]  */
 433 {
 434     int fnd_start = 0, fnd_end = 0;
 435 
 436 #ifdef SEARCH_TYPE_GLIB
 437     g_match_info_fetch_pos (lc_mc_search->regex_match_info, lc_index, &fnd_start, &fnd_end);
 438 #else /* SEARCH_TYPE_GLIB */
 439     fnd_start = lc_mc_search->iovector[lc_index * 2 + 0];
 440     fnd_end = lc_mc_search->iovector[lc_index * 2 + 1];
 441 #endif /* SEARCH_TYPE_GLIB */
 442 
 443     if (fnd_end == fnd_start)
 444         return g_strdup ("");
 445 
 446     return g_strndup (lc_mc_search->regex_buffer->str + fnd_start, fnd_end - fnd_start);
 447 
 448 }
 449 
 450 /* --------------------------------------------------------------------------------------------- */
 451 
 452 static gboolean
 453 mc_search_regex__replace_handle_esc_seq (const GString * replace_str, const gsize current_pos,
     /* [previous][next][first][last][top][bottom][index][help]  */
 454                                          gsize * skip_len, int *ret)
 455 {
 456     char *curr_str = &(replace_str->str[current_pos]);
 457     char c = curr_str[1];
 458 
 459     if (replace_str->len > current_pos + 2)
 460     {
 461         if (c == '{')
 462         {
 463             for (*skip_len = 2; /* \{ */
 464                  current_pos + *skip_len < replace_str->len && curr_str[*skip_len] >= '0'
 465                  && curr_str[*skip_len] <= '7'; (*skip_len)++)
 466                 ;
 467 
 468             if (current_pos + *skip_len < replace_str->len && curr_str[*skip_len] == '}')
 469             {
 470                 (*skip_len)++;
 471                 *ret = REPLACE_PREPARE_T_ESCAPE_SEQ;
 472                 return FALSE;
 473             }
 474             else
 475             {
 476                 *ret = REPLACE_PREPARE_T_NOTHING_SPECIAL;
 477                 return TRUE;
 478             }
 479         }
 480 
 481         if (c == 'x')
 482         {
 483             *skip_len = 2;      /* \x */
 484             c = curr_str[2];
 485             if (c == '{')
 486             {
 487                 for (*skip_len = 3;     /* \x{ */
 488                      current_pos + *skip_len < replace_str->len
 489                      && g_ascii_isxdigit ((guchar) curr_str[*skip_len]); (*skip_len)++)
 490                     ;
 491 
 492                 if (current_pos + *skip_len < replace_str->len && curr_str[*skip_len] == '}')
 493                 {
 494                     (*skip_len)++;
 495                     *ret = REPLACE_PREPARE_T_ESCAPE_SEQ;
 496                     return FALSE;
 497                 }
 498                 else
 499                 {
 500                     *ret = REPLACE_PREPARE_T_NOTHING_SPECIAL;
 501                     return TRUE;
 502                 }
 503             }
 504             else if (!g_ascii_isxdigit ((guchar) c))
 505             {
 506                 *skip_len = 2;  /* \x without number behind */
 507                 *ret = REPLACE_PREPARE_T_NOTHING_SPECIAL;
 508                 return FALSE;
 509             }
 510             else
 511             {
 512                 c = curr_str[3];
 513                 if (!g_ascii_isxdigit ((guchar) c))
 514                     *skip_len = 3;      /* \xH */
 515                 else
 516                     *skip_len = 4;      /* \xHH */
 517                 *ret = REPLACE_PREPARE_T_ESCAPE_SEQ;
 518                 return FALSE;
 519             }
 520         }
 521     }
 522 
 523     if (strchr ("ntvbrfa", c) != NULL)
 524     {
 525         *skip_len = 2;
 526         *ret = REPLACE_PREPARE_T_ESCAPE_SEQ;
 527         return FALSE;
 528     }
 529     return TRUE;
 530 }
 531 
 532 /* --------------------------------------------------------------------------------------------- */
 533 
 534 static int
 535 mc_search_regex__process_replace_str (const GString * replace_str, const gsize current_pos,
     /* [previous][next][first][last][top][bottom][index][help]  */
 536                                       gsize * skip_len, replace_transform_type_t * replace_flags)
 537 {
 538     int ret = -1;
 539     const char *curr_str = &(replace_str->str[current_pos]);
 540 
 541     if (current_pos > replace_str->len)
 542         return REPLACE_PREPARE_T_NOTHING_SPECIAL;
 543 
 544     *skip_len = 0;
 545 
 546     if (replace_str->len > current_pos + 2 && curr_str[0] == '$' && curr_str[1] == '{'
 547         && (curr_str[2] & (char) 0xf0) == 0x30)
 548     {
 549         char *tmp_str;
 550 
 551         if (strutils_is_char_escaped (replace_str->str, curr_str))
 552         {
 553             *skip_len = 1;
 554             return REPLACE_PREPARE_T_NOTHING_SPECIAL;
 555         }
 556 
 557         for (*skip_len = 0;
 558              current_pos + *skip_len + 2 < replace_str->len
 559              && (curr_str[2 + *skip_len] & (char) 0xf0) == 0x30; (*skip_len)++)
 560             ;
 561 
 562         if (curr_str[2 + *skip_len] != '}')
 563             return REPLACE_PREPARE_T_NOTHING_SPECIAL;
 564 
 565         tmp_str = g_strndup (curr_str + 2, *skip_len);
 566         if (tmp_str == NULL)
 567             return REPLACE_PREPARE_T_NOTHING_SPECIAL;
 568 
 569         ret = atoi (tmp_str);
 570         g_free (tmp_str);
 571 
 572         *skip_len += 3;         /* ${} */
 573         return ret;             /* capture buffer index >= 0 */
 574     }
 575 
 576     if (curr_str[0] == '\\' && replace_str->len > current_pos + 1)
 577     {
 578         if (strutils_is_char_escaped (replace_str->str, curr_str))
 579         {
 580             *skip_len = 1;
 581             return REPLACE_PREPARE_T_NOTHING_SPECIAL;
 582         }
 583 
 584         if (g_ascii_isdigit (curr_str[1]))
 585         {
 586             ret = g_ascii_digit_value (curr_str[1]);    /* capture buffer index >= 0 */
 587             *skip_len = 2;      /* \\ and one digit */
 588             return ret;
 589         }
 590 
 591         if (!mc_search_regex__replace_handle_esc_seq (replace_str, current_pos, skip_len, &ret))
 592             return ret;
 593 
 594         ret = REPLACE_PREPARE_T_REPLACE_FLAG;
 595         *skip_len += 2;
 596 
 597         switch (curr_str[1])
 598         {
 599         case 'U':
 600             *replace_flags |= REPLACE_T_UPP_TRANSFORM;
 601             *replace_flags &= ~REPLACE_T_LOW_TRANSFORM;
 602             break;
 603         case 'u':
 604             *replace_flags |= REPLACE_T_UPP_TRANSFORM_CHAR;
 605             break;
 606         case 'L':
 607             *replace_flags |= REPLACE_T_LOW_TRANSFORM;
 608             *replace_flags &= ~REPLACE_T_UPP_TRANSFORM;
 609             break;
 610         case 'l':
 611             *replace_flags |= REPLACE_T_LOW_TRANSFORM_CHAR;
 612             break;
 613         case 'E':
 614             *replace_flags = REPLACE_T_NO_TRANSFORM;
 615             break;
 616         default:
 617             ret = REPLACE_PREPARE_T_NOTHING_SPECIAL;
 618             break;
 619         }
 620     }
 621     return ret;
 622 }
 623 
 624 /* --------------------------------------------------------------------------------------------- */
 625 
 626 static void
 627 mc_search_regex__process_append_str (GString * dest_str, const char *from, gsize len,
     /* [previous][next][first][last][top][bottom][index][help]  */
 628                                      replace_transform_type_t * replace_flags)
 629 {
 630     gsize loop;
 631     gsize char_len;
 632 
 633     if (len == (gsize) (-1))
 634         len = strlen (from);
 635 
 636     if (*replace_flags == REPLACE_T_NO_TRANSFORM)
 637     {
 638         g_string_append_len (dest_str, from, len);
 639         return;
 640     }
 641 
 642     for (loop = 0; loop < len; loop += char_len)
 643     {
 644         GString *tmp_string = NULL;
 645         char *tmp_str;
 646 
 647         tmp_str = mc_search__get_one_symbol (NULL, from + loop, len - loop, NULL);
 648         char_len = strlen (tmp_str);
 649 
 650         if ((*replace_flags & REPLACE_T_UPP_TRANSFORM_CHAR) != 0)
 651         {
 652             *replace_flags &= ~REPLACE_T_UPP_TRANSFORM_CHAR;
 653             tmp_string = mc_search__toupper_case_str (NULL, tmp_str, char_len);
 654             g_string_append_len (dest_str, tmp_string->str, tmp_string->len);
 655             g_string_free (tmp_string, TRUE);
 656         }
 657         else if ((*replace_flags & REPLACE_T_LOW_TRANSFORM_CHAR) != 0)
 658         {
 659             *replace_flags &= ~REPLACE_T_LOW_TRANSFORM_CHAR;
 660             tmp_string = mc_search__tolower_case_str (NULL, tmp_str, char_len);
 661             g_string_append_len (dest_str, tmp_string->str, tmp_string->len);
 662             g_string_free (tmp_string, TRUE);
 663         }
 664         else if ((*replace_flags & REPLACE_T_UPP_TRANSFORM) != 0)
 665         {
 666             tmp_string = mc_search__toupper_case_str (NULL, tmp_str, char_len);
 667             g_string_append_len (dest_str, tmp_string->str, tmp_string->len);
 668             g_string_free (tmp_string, TRUE);
 669         }
 670         else if ((*replace_flags & REPLACE_T_LOW_TRANSFORM) != 0)
 671         {
 672             tmp_string = mc_search__tolower_case_str (NULL, tmp_str, char_len);
 673             g_string_append_len (dest_str, tmp_string->str, tmp_string->len);
 674             g_string_free (tmp_string, TRUE);
 675         }
 676 
 677         g_free (tmp_str);
 678     }
 679 }
 680 
 681 /* --------------------------------------------------------------------------------------------- */
 682 
 683 static void
 684 mc_search_regex__process_escape_sequence (GString * dest_str, const char *from, gsize len,
     /* [previous][next][first][last][top][bottom][index][help]  */
 685                                           replace_transform_type_t * replace_flags,
 686                                           gboolean is_utf8)
 687 {
 688     gsize i = 0;
 689     unsigned int c = 0;
 690     char b;
 691 
 692     if (len == (gsize) (-1))
 693         len = strlen (from);
 694     if (len == 0)
 695         return;
 696 
 697     if (from[i] == '{')
 698         i++;
 699     if (i >= len)
 700         return;
 701 
 702     if (from[i] == 'x')
 703     {
 704         i++;
 705         if (i < len && from[i] == '{')
 706             i++;
 707         for (; i < len; i++)
 708         {
 709             if (from[i] >= '0' && from[i] <= '9')
 710                 c = c * 16 + from[i] - '0';
 711             else if (from[i] >= 'a' && from[i] <= 'f')
 712                 c = c * 16 + 10 + from[i] - 'a';
 713             else if (from[i] >= 'A' && from[i] <= 'F')
 714                 c = c * 16 + 10 + from[i] - 'A';
 715             else
 716                 break;
 717         }
 718     }
 719     else if (from[i] >= '0' && from[i] <= '7')
 720         for (; i < len && from[i] >= '0' && from[i] <= '7'; i++)
 721             c = c * 8 + from[i] - '0';
 722     else
 723     {
 724         switch (from[i])
 725         {
 726         case 'n':
 727             c = '\n';
 728             break;
 729         case 't':
 730             c = '\t';
 731             break;
 732         case 'v':
 733             c = '\v';
 734             break;
 735         case 'b':
 736             c = '\b';
 737             break;
 738         case 'r':
 739             c = '\r';
 740             break;
 741         case 'f':
 742             c = '\f';
 743             break;
 744         case 'a':
 745             c = '\a';
 746             break;
 747         default:
 748             mc_search_regex__process_append_str (dest_str, from, len, replace_flags);
 749             return;
 750         }
 751     }
 752 
 753     if (c < 0x80 || !is_utf8)
 754         g_string_append_c (dest_str, (char) c);
 755     else if (c < 0x800)
 756     {
 757         b = 0xC0 | (c >> 6);
 758         g_string_append_c (dest_str, b);
 759         b = 0x80 | (c & 0x3F);
 760         g_string_append_c (dest_str, b);
 761     }
 762     else if (c < 0x10000)
 763     {
 764         b = 0xE0 | (c >> 12);
 765         g_string_append_c (dest_str, b);
 766         b = 0x80 | ((c >> 6) & 0x3F);
 767         g_string_append_c (dest_str, b);
 768         b = 0x80 | (c & 0x3F);
 769         g_string_append_c (dest_str, b);
 770     }
 771     else if (c < 0x10FFFF)
 772     {
 773         b = 0xF0 | (c >> 16);
 774         g_string_append_c (dest_str, b);
 775         b = 0x80 | ((c >> 12) & 0x3F);
 776         g_string_append_c (dest_str, b);
 777         b = 0x80 | ((c >> 6) & 0x3F);
 778         g_string_append_c (dest_str, b);
 779         b = 0x80 | (c & 0x3F);
 780         g_string_append_c (dest_str, b);
 781     }
 782 }
 783 
 784 /* --------------------------------------------------------------------------------------------- */
 785 /*** public functions ****************************************************************************/
 786 /* --------------------------------------------------------------------------------------------- */
 787 
 788 void
 789 mc_search__cond_struct_new_init_regex (const char *charset, mc_search_t * lc_mc_search,
     /* [previous][next][first][last][top][bottom][index][help]  */
 790                                        mc_search_cond_t * mc_search_cond)
 791 {
 792     if (lc_mc_search->whole_words && !lc_mc_search->is_entire_line)
 793     {
 794         /* NOTE: \b as word boundary doesn't allow search
 795          * whole words with non-ASCII symbols.
 796          * Update: Is it still true nowadays? Probably not. #2396, #3524 */
 797         g_string_prepend (mc_search_cond->str, "(?<![\\p{L}\\p{N}_])");
 798         g_string_append (mc_search_cond->str, "(?![\\p{L}\\p{N}_])");
 799     }
 800 
 801     {
 802 #ifdef SEARCH_TYPE_GLIB
 803         GError *mcerror = NULL;
 804         GRegexCompileFlags g_regex_options = G_REGEX_OPTIMIZE | G_REGEX_DOTALL;
 805 
 806         if (str_isutf8 (charset) && mc_global.utf8_display)
 807         {
 808             if (!lc_mc_search->is_case_sensitive)
 809                 g_regex_options |= G_REGEX_CASELESS;
 810         }
 811         else
 812         {
 813             g_regex_options |= G_REGEX_RAW;
 814 
 815             if (!lc_mc_search->is_case_sensitive)
 816             {
 817                 GString *tmp;
 818 
 819                 tmp = mc_search_cond->str;
 820                 mc_search_cond->str = mc_search__cond_struct_new_regex_ci_str (charset, tmp);
 821                 g_string_free (tmp, TRUE);
 822 
 823             }
 824         }
 825 
 826         mc_search_cond->regex_handle =
 827             g_regex_new (mc_search_cond->str->str, g_regex_options, 0, &mcerror);
 828 
 829         if (mcerror != NULL)
 830         {
 831             lc_mc_search->error = MC_SEARCH_E_REGEX_COMPILE;
 832             g_free (lc_mc_search->error_str);
 833             lc_mc_search->error_str =
 834                 str_conv_gerror_message (mcerror, _("Regular expression error"));
 835             g_error_free (mcerror);
 836             return;
 837         }
 838 #else /* SEARCH_TYPE_GLIB */
 839         const char *error;
 840         int erroffset;
 841         int pcre_options = PCRE_EXTRA | PCRE_MULTILINE;
 842 
 843         if (str_isutf8 (charset) && mc_global.utf8_display)
 844         {
 845             pcre_options |= PCRE_UTF8;
 846             if (!lc_mc_search->is_case_sensitive)
 847                 pcre_options |= PCRE_CASELESS;
 848         }
 849         else
 850         {
 851             if (!lc_mc_search->is_case_sensitive)
 852             {
 853                 GString *tmp;
 854 
 855                 tmp = mc_search_cond->str;
 856                 mc_search_cond->str = mc_search__cond_struct_new_regex_ci_str (charset, tmp);
 857                 g_string_free (tmp, TRUE);
 858             }
 859         }
 860 
 861         mc_search_cond->regex_handle =
 862             pcre_compile (mc_search_cond->str->str, pcre_options, &error, &erroffset, NULL);
 863         if (mc_search_cond->regex_handle == NULL)
 864         {
 865             mc_search_set_error (lc_mc_search, MC_SEARCH_E_REGEX_COMPILE, "%s", error);
 866             return;
 867         }
 868         lc_mc_search->regex_match_info = pcre_study (mc_search_cond->regex_handle, 0, &error);
 869         if (lc_mc_search->regex_match_info == NULL && error != NULL)
 870         {
 871             mc_search_set_error (lc_mc_search, MC_SEARCH_E_REGEX_COMPILE, "%s", error);
 872             MC_PTR_FREE (mc_search_cond->regex_handle);
 873             return;
 874         }
 875 #endif /* SEARCH_TYPE_GLIB */
 876     }
 877 
 878     lc_mc_search->is_utf8 = str_isutf8 (charset);
 879 }
 880 
 881 /* --------------------------------------------------------------------------------------------- */
 882 
 883 gboolean
 884 mc_search__run_regex (mc_search_t * lc_mc_search, const void *user_data,
     /* [previous][next][first][last][top][bottom][index][help]  */
 885                       gsize start_search, gsize end_search, gsize * found_len)
 886 {
 887     mc_search_cbret_t ret = MC_SEARCH_CB_NOTFOUND;
 888     gsize current_pos, virtual_pos;
 889     gint start_pos;
 890     gint end_pos;
 891 
 892     if (lc_mc_search->regex_buffer != NULL)
 893         g_string_set_size (lc_mc_search->regex_buffer, 0);
 894     else
 895         lc_mc_search->regex_buffer = g_string_sized_new (64);
 896 
 897     virtual_pos = current_pos = start_search;
 898     while (virtual_pos <= end_search)
 899     {
 900         g_string_set_size (lc_mc_search->regex_buffer, 0);
 901         lc_mc_search->start_buffer = current_pos;
 902 
 903         if (lc_mc_search->search_fn != NULL)
 904         {
 905             while (TRUE)
 906             {
 907                 int current_chr = '\n'; /* stop search symbol */
 908 
 909                 ret = lc_mc_search->search_fn (user_data, current_pos, &current_chr);
 910 
 911                 if (ret == MC_SEARCH_CB_ABORT)
 912                     break;
 913 
 914                 if (ret == MC_SEARCH_CB_INVALID)
 915                     continue;
 916 
 917                 current_pos++;
 918 
 919                 if (ret == MC_SEARCH_CB_SKIP)
 920                     continue;
 921 
 922                 virtual_pos++;
 923 
 924                 g_string_append_c (lc_mc_search->regex_buffer, (char) current_chr);
 925 
 926                 if ((char) current_chr == '\n' || virtual_pos > end_search)
 927                     break;
 928             }
 929         }
 930         else
 931         {
 932             /* optimization for standard case (for search from file manager)
 933              *  where there is no MC_SEARCH_CB_INVALID or MC_SEARCH_CB_SKIP
 934              *  return codes, so we can copy line at regex buffer all at once
 935              */
 936             while (TRUE)
 937             {
 938                 const char current_chr = ((const char *) user_data)[current_pos];
 939 
 940                 if (current_chr == '\0')
 941                     break;
 942 
 943                 current_pos++;
 944 
 945                 if (current_chr == '\n' || current_pos > end_search)
 946                     break;
 947             }
 948 
 949             /* use virtual_pos as index of start of current chunk */
 950             g_string_append_len (lc_mc_search->regex_buffer, (const char *) user_data + virtual_pos,
 951                                  current_pos - virtual_pos);
 952             virtual_pos = current_pos;
 953         }
 954 
 955         switch (mc_search__regex_found_cond (lc_mc_search, lc_mc_search->regex_buffer))
 956         {
 957         case COND__FOUND_OK:
 958 #ifdef SEARCH_TYPE_GLIB
 959             g_match_info_fetch_pos (lc_mc_search->regex_match_info, 0, &start_pos, &end_pos);
 960 #else /* SEARCH_TYPE_GLIB */
 961             start_pos = lc_mc_search->iovector[0];
 962             end_pos = lc_mc_search->iovector[1];
 963 #endif /* SEARCH_TYPE_GLIB */
 964             if (found_len != NULL)
 965                 *found_len = end_pos - start_pos;
 966             lc_mc_search->normal_offset = lc_mc_search->start_buffer + start_pos;
 967             return TRUE;
 968         case COND__NOT_ALL_FOUND:
 969             break;
 970         default:
 971             g_string_free (lc_mc_search->regex_buffer, TRUE);
 972             lc_mc_search->regex_buffer = NULL;
 973             return FALSE;
 974         }
 975 
 976         if ((lc_mc_search->update_fn != NULL) &&
 977             ((lc_mc_search->update_fn) (user_data, current_pos) == MC_SEARCH_CB_ABORT))
 978             ret = MC_SEARCH_CB_ABORT;
 979 
 980         if (ret == MC_SEARCH_CB_ABORT || ret == MC_SEARCH_CB_NOTFOUND)
 981             break;
 982     }
 983 
 984     g_string_free (lc_mc_search->regex_buffer, TRUE);
 985     lc_mc_search->regex_buffer = NULL;
 986 
 987     MC_PTR_FREE (lc_mc_search->error_str);
 988     lc_mc_search->error = ret == MC_SEARCH_CB_ABORT ? MC_SEARCH_E_ABORT : MC_SEARCH_E_NOTFOUND;
 989 
 990     return FALSE;
 991 }
 992 
 993 /* --------------------------------------------------------------------------------------------- */
 994 
 995 GString *
 996 mc_search_regex_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str)
     /* [previous][next][first][last][top][bottom][index][help]  */
 997 {
 998     GString *ret;
 999 
1000     int num_replace_tokens;
1001     gsize loop;
1002     gsize prev = 0;
1003     replace_transform_type_t replace_flags = REPLACE_T_NO_TRANSFORM;
1004 
1005     num_replace_tokens =
1006         mc_search_regex__get_max_num_of_replace_tokens (replace_str->str, replace_str->len);
1007 
1008     if (lc_mc_search->num_results < 0)
1009         return g_string_new_len (replace_str->str, replace_str->len);
1010 
1011     if (num_replace_tokens > lc_mc_search->num_results - 1
1012         || num_replace_tokens > MC_SEARCH__NUM_REPLACE_ARGS)
1013     {
1014         mc_search_set_error (lc_mc_search, MC_SEARCH_E_REGEX_REPLACE, "%s",
1015                              _(STR_E_RPL_NOT_EQ_TO_FOUND));
1016         return NULL;
1017     }
1018 
1019     ret = g_string_sized_new (64);
1020 
1021     for (loop = 0; loop < replace_str->len - 1; loop++)
1022     {
1023         int lc_index;
1024         gchar *tmp_str;
1025         gsize len = 0;
1026 
1027         lc_index = mc_search_regex__process_replace_str (replace_str, loop, &len, &replace_flags);
1028 
1029         if (lc_index == REPLACE_PREPARE_T_NOTHING_SPECIAL)
1030         {
1031             if (len != 0)
1032             {
1033                 mc_search_regex__process_append_str (ret, replace_str->str + prev, loop - prev,
1034                                                      &replace_flags);
1035                 mc_search_regex__process_append_str (ret, replace_str->str + loop + 1, len - 1,
1036                                                      &replace_flags);
1037                 prev = loop + len;
1038                 loop = prev - 1;        /* prepare to loop++ */
1039             }
1040 
1041             continue;
1042         }
1043 
1044         if (lc_index == REPLACE_PREPARE_T_REPLACE_FLAG)
1045         {
1046             if (loop != 0)
1047                 mc_search_regex__process_append_str (ret, replace_str->str + prev, loop - prev,
1048                                                      &replace_flags);
1049             prev = loop + len;
1050             loop = prev - 1;    /* prepare to loop++ */
1051             continue;
1052         }
1053 
1054         /* escape sequence */
1055         if (lc_index == REPLACE_PREPARE_T_ESCAPE_SEQ)
1056         {
1057             mc_search_regex__process_append_str (ret, replace_str->str + prev, loop - prev,
1058                                                  &replace_flags);
1059             /* call process_escape_sequence without starting '\\' */
1060             mc_search_regex__process_escape_sequence (ret, replace_str->str + loop + 1, len - 1,
1061                                                       &replace_flags, lc_mc_search->is_utf8);
1062             prev = loop + len;
1063             loop = prev - 1;    /* prepare to loop++ */
1064             continue;
1065         }
1066 
1067         /* invalid capture buffer number */
1068         if (lc_index > lc_mc_search->num_results)
1069         {
1070             g_string_free (ret, TRUE);
1071             mc_search_set_error (lc_mc_search, MC_SEARCH_E_REGEX_REPLACE,
1072                                  _(STR_E_RPL_INVALID_TOKEN), lc_index);
1073             return NULL;
1074         }
1075 
1076         tmp_str = mc_search_regex__get_token_by_num (lc_mc_search, lc_index);
1077 
1078         if (loop != 0)
1079             mc_search_regex__process_append_str (ret, replace_str->str + prev, loop - prev,
1080                                                  &replace_flags);
1081 
1082         mc_search_regex__process_append_str (ret, tmp_str, -1, &replace_flags);
1083         g_free (tmp_str);
1084 
1085         prev = loop + len;
1086         loop = prev - 1;        /* prepare to loop++ */
1087     }
1088 
1089     mc_search_regex__process_append_str (ret, replace_str->str + prev, replace_str->len - prev,
1090                                          &replace_flags);
1091 
1092     return ret;
1093 }

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