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-2024
   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/util.h"           /* MC_PTR_FREE */
  37 
  38 #include "internal.h"
  39 
  40 /*** global variables ****************************************************************************/
  41 
  42 /*** file scope macro definitions ****************************************************************/
  43 
  44 #define REPLACE_PREPARE_T_NOTHING_SPECIAL -1
  45 #define REPLACE_PREPARE_T_REPLACE_FLAG    -2
  46 #define REPLACE_PREPARE_T_ESCAPE_SEQ      -3
  47 
  48 /*** file scope type declarations ****************************************************************/
  49 
  50 typedef enum
  51 {
  52     REPLACE_T_NO_TRANSFORM = 0,
  53     REPLACE_T_UPP_TRANSFORM_CHAR = 1,
  54     REPLACE_T_LOW_TRANSFORM_CHAR = 2,
  55     REPLACE_T_UPP_TRANSFORM = 4,
  56     REPLACE_T_LOW_TRANSFORM = 8
  57 } replace_transform_type_t;
  58 
  59 /*** forward declarations (file scope functions) *************************************************/
  60 
  61 /*** file scope variables ************************************************************************/
  62 
  63 /* --------------------------------------------------------------------------------------------- */
  64 /*** file scope functions ************************************************************************/
  65 /* --------------------------------------------------------------------------------------------- */
  66 
  67 static gboolean
  68 mc_search__regex_str_append_if_special (GString *copy_to, const GString *regex_str, gsize *offset)
     /* [previous][next][first][last][top][bottom][index][help]  */
  69 {
  70     const char *special_chars[] = {
  71         "\\s", "\\S",
  72         "\\d", "\\D",
  73         "\\b", "\\B",
  74         "\\w", "\\W",
  75         "\\t", "\\n",
  76         "\\r", "\\f",
  77         "\\a", "\\e",
  78         "\\x", "\\X",
  79         "\\c", "\\C",
  80         "\\l", "\\L",
  81         "\\u", "\\U",
  82         "\\E", "\\Q",
  83         NULL
  84     };
  85 
  86     char *tmp_regex_str;
  87     const char **spec_chr;
  88 
  89     tmp_regex_str = &(regex_str->str[*offset]);
  90 
  91     for (spec_chr = special_chars; *spec_chr != NULL; spec_chr++)
  92     {
  93         gsize spec_chr_len;
  94 
  95         spec_chr_len = strlen (*spec_chr);
  96 
  97         if (strncmp (tmp_regex_str, *spec_chr, spec_chr_len) == 0
  98             && !str_is_char_escaped (regex_str->str, tmp_regex_str))
  99         {
 100             if (strncmp ("\\x", *spec_chr, spec_chr_len) == 0)
 101             {
 102                 if (tmp_regex_str[spec_chr_len] != '{')
 103                     spec_chr_len += 2;
 104                 else
 105                 {
 106                     while ((spec_chr_len < regex_str->len - *offset)
 107                            && tmp_regex_str[spec_chr_len] != '}')
 108                         spec_chr_len++;
 109                     if (tmp_regex_str[spec_chr_len] == '}')
 110                         spec_chr_len++;
 111                 }
 112             }
 113             g_string_append_len (copy_to, tmp_regex_str, spec_chr_len);
 114             *offset += spec_chr_len;
 115             return TRUE;
 116         }
 117     }
 118 
 119     return FALSE;
 120 }
 121 
 122 /* --------------------------------------------------------------------------------------------- */
 123 
 124 static void
 125 mc_search__cond_struct_new_regex_hex_add (const char *charset, GString *str_to,
     /* [previous][next][first][last][top][bottom][index][help]  */
 126                                           const GString *one_char)
 127 {
 128     GString *upp, *low;
 129     gsize loop;
 130 
 131     upp = mc_search__toupper_case_str (charset, one_char);
 132     low = mc_search__tolower_case_str (charset, one_char);
 133 
 134     for (loop = 0; loop < upp->len; loop++)
 135     {
 136         gchar tmp_str[10 + 1];  /* longest content is "[\\x%02X\\x%02X]" */
 137         gint tmp_len;
 138 
 139         if (loop >= low->len || upp->str[loop] == low->str[loop])
 140             tmp_len =
 141                 g_snprintf (tmp_str, sizeof (tmp_str), "\\x%02X", (unsigned char) upp->str[loop]);
 142         else
 143             tmp_len =
 144                 g_snprintf (tmp_str, sizeof (tmp_str), "[\\x%02X\\x%02X]",
 145                             (unsigned char) upp->str[loop], (unsigned char) low->str[loop]);
 146 
 147         g_string_append_len (str_to, tmp_str, tmp_len);
 148     }
 149 
 150     g_string_free (upp, TRUE);
 151     g_string_free (low, TRUE);
 152 }
 153 
 154 /* --------------------------------------------------------------------------------------------- */
 155 
 156 static void
 157 mc_search__cond_struct_new_regex_accum_append (const char *charset, GString *str_to,
     /* [previous][next][first][last][top][bottom][index][help]  */
 158                                                GString *str_from)
 159 {
 160     GString *recoded_part;
 161     gsize loop = 0;
 162 
 163     recoded_part = g_string_sized_new (32);
 164 
 165     while (loop < str_from->len)
 166     {
 167         GString *one_char;
 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 
 174         if (one_char->len == 0)
 175             loop++;
 176         else
 177         {
 178             loop += one_char->len;
 179 
 180             if (just_letters)
 181                 mc_search__cond_struct_new_regex_hex_add (charset, recoded_part, one_char);
 182             else
 183                 g_string_append_len (recoded_part, one_char->str, one_char->len);
 184         }
 185 
 186         g_string_free (one_char, TRUE);
 187     }
 188 
 189     g_string_append_len (str_to, recoded_part->str, recoded_part->len);
 190     g_string_free (recoded_part, TRUE);
 191     g_string_set_size (str_from, 0);
 192 }
 193 
 194 /* --------------------------------------------------------------------------------------------- */
 195 
 196 /**
 197  * Creates a case-insensitive version of a regex pattern.
 198  *
 199  * For example (assuming ASCII charset): given "\\bHello!\\xAB", returns
 200  * "\\b[Hh][Ee][Ll][Ll][Oo]!\\xAB" (this example is for easier reading; in
 201  * reality hex codes are used instead of letters).
 202  *
 203  * This function knows not to ruin special regex symbols.
 204  *
 205  * This function is used when working with non-UTF-8 charsets: GLib's
 206  * regex engine doesn't understand such charsets and therefore can't do
 207  * this job itself.
 208  */
 209 static GString *
 210 mc_search__cond_struct_new_regex_ci_str (const char *charset, const GString *astr)
     /* [previous][next][first][last][top][bottom][index][help]  */
 211 {
 212     GString *accumulator, *spec_char, *ret_str;
 213     gsize loop;
 214 
 215     ret_str = g_string_sized_new (64);
 216     accumulator = g_string_sized_new (64);
 217     spec_char = g_string_sized_new (64);
 218     loop = 0;
 219 
 220     while (loop < astr->len)
 221     {
 222         if (mc_search__regex_str_append_if_special (spec_char, astr, &loop))
 223         {
 224             mc_search__cond_struct_new_regex_accum_append (charset, ret_str, accumulator);
 225             g_string_append_len (ret_str, spec_char->str, spec_char->len);
 226             g_string_set_size (spec_char, 0);
 227             continue;
 228         }
 229 
 230         if (astr->str[loop] == '[' && !str_is_char_escaped (astr->str, &(astr->str[loop])))
 231         {
 232             mc_search__cond_struct_new_regex_accum_append (charset, ret_str, accumulator);
 233 
 234             while (loop < astr->len && !(astr->str[loop] == ']'
 235                                          && !str_is_char_escaped (astr->str, &(astr->str[loop]))))
 236             {
 237                 g_string_append_c (ret_str, astr->str[loop]);
 238                 loop++;
 239             }
 240 
 241             g_string_append_c (ret_str, astr->str[loop]);
 242             loop++;
 243             continue;
 244         }
 245         /*
 246            TODO: handle [ and ]
 247          */
 248         g_string_append_c (accumulator, astr->str[loop]);
 249         loop++;
 250     }
 251     mc_search__cond_struct_new_regex_accum_append (charset, ret_str, accumulator);
 252 
 253     g_string_free (accumulator, TRUE);
 254     g_string_free (spec_char, TRUE);
 255 
 256     return ret_str;
 257 }
 258 
 259 /* --------------------------------------------------------------------------------------------- */
 260 
 261 #ifdef SEARCH_TYPE_GLIB
 262 /* A thin wrapper above g_regex_match_full that makes sure the string passed
 263  * to it is valid UTF-8 (unless G_REGEX_RAW compile flag was set), as it is a
 264  * requirement by glib and it might crash otherwise. See: mc ticket 3449.
 265  * Be careful: there might be embedded NULs in the strings. */
 266 static gboolean
 267 mc_search__g_regex_match_full_safe (const GRegex *regex,
     /* [previous][next][first][last][top][bottom][index][help]  */
 268                                     const gchar *string,
 269                                     gssize string_len,
 270                                     gint start_position,
 271                                     GRegexMatchFlags match_options,
 272                                     GMatchInfo **match_info, GError **error)
 273 {
 274     char *string_safe, *p, *end;
 275     gboolean ret;
 276 
 277     if (string_len < 0)
 278         string_len = strlen (string);
 279 
 280     if ((g_regex_get_compile_flags (regex) & G_REGEX_RAW)
 281         || g_utf8_validate (string, string_len, NULL))
 282     {
 283         return g_regex_match_full (regex, string, string_len, start_position, match_options,
 284                                    match_info, error);
 285     }
 286 
 287     /* Correctly handle embedded NULs while copying */
 288     p = string_safe = g_malloc (string_len + 1);
 289     memcpy (string_safe, string, string_len);
 290     string_safe[string_len] = '\0';
 291     end = p + string_len;
 292 
 293     while (p < end)
 294     {
 295         gunichar c = g_utf8_get_char_validated (p, -1);
 296         if (c != (gunichar) (-1) && c != (gunichar) (-2))
 297         {
 298             p = g_utf8_next_char (p);
 299         }
 300         else
 301         {
 302             /* U+FFFD would be the proper choice, but then we'd have to
 303                maintain mapping between old and new offsets.
 304                So rather do a byte by byte replacement. */
 305             *p++ = '\0';
 306         }
 307     }
 308 
 309     ret =
 310         g_regex_match_full (regex, string_safe, string_len, start_position, match_options,
 311                             match_info, error);
 312     g_free (string_safe);
 313     return ret;
 314 }
 315 #endif /* SEARCH_TYPE_GLIB */
 316 
 317 /* --------------------------------------------------------------------------------------------- */
 318 
 319 static mc_search__found_cond_t
 320 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]  */
 321                                  GString *search_str)
 322 {
 323 #ifdef SEARCH_TYPE_GLIB
 324     GError *mcerror = NULL;
 325 
 326     if (!mc_search__g_regex_match_full_safe
 327         (regex, search_str->str, search_str->len, 0, G_REGEX_MATCH_NEWLINE_ANY,
 328          &lc_mc_search->regex_match_info, &mcerror))
 329     {
 330         g_match_info_free (lc_mc_search->regex_match_info);
 331         lc_mc_search->regex_match_info = NULL;
 332         if (mcerror != NULL)
 333         {
 334             lc_mc_search->error = MC_SEARCH_E_REGEX;
 335             g_free (lc_mc_search->error_str);
 336             lc_mc_search->error_str =
 337                 str_conv_gerror_message (mcerror, _("Regular expression error"));
 338             g_error_free (mcerror);
 339             return COND__FOUND_ERROR;
 340         }
 341         return COND__NOT_FOUND;
 342     }
 343     lc_mc_search->num_results = g_match_info_get_match_count (lc_mc_search->regex_match_info);
 344 #else /* SEARCH_TYPE_GLIB */
 345 
 346     lc_mc_search->num_results =
 347 #ifdef HAVE_PCRE2
 348         pcre2_match (regex, (unsigned char *) search_str->str, search_str->len, 0, 0,
 349                      lc_mc_search->regex_match_info, NULL);
 350 #else
 351         pcre_exec (regex, lc_mc_search->regex_match_info, search_str->str, search_str->len, 0, 0,
 352                    lc_mc_search->iovector, MC_SEARCH__NUM_REPLACE_ARGS);
 353 #endif
 354     if (lc_mc_search->num_results < 0)
 355     {
 356         return COND__NOT_FOUND;
 357     }
 358 #endif /* SEARCH_TYPE_GLIB */
 359     return COND__FOUND_OK;
 360 
 361 }
 362 
 363 /* --------------------------------------------------------------------------------------------- */
 364 
 365 static mc_search__found_cond_t
 366 mc_search__regex_found_cond (mc_search_t *lc_mc_search, GString *search_str)
     /* [previous][next][first][last][top][bottom][index][help]  */
 367 {
 368     gsize loop1;
 369 
 370     for (loop1 = 0; loop1 < lc_mc_search->prepared.conditions->len; loop1++)
 371     {
 372         mc_search_cond_t *mc_search_cond;
 373         mc_search__found_cond_t ret;
 374 
 375         mc_search_cond =
 376             (mc_search_cond_t *) g_ptr_array_index (lc_mc_search->prepared.conditions, loop1);
 377 
 378         if (!mc_search_cond->regex_handle)
 379             continue;
 380 
 381         ret =
 382             mc_search__regex_found_cond_one (lc_mc_search, mc_search_cond->regex_handle,
 383                                              search_str);
 384         if (ret != COND__NOT_FOUND)
 385             return ret;
 386     }
 387     return COND__NOT_ALL_FOUND;
 388 }
 389 
 390 /* --------------------------------------------------------------------------------------------- */
 391 
 392 static int
 393 mc_search_regex__get_max_num_of_replace_tokens (const gchar *str, gsize len)
     /* [previous][next][first][last][top][bottom][index][help]  */
 394 {
 395     int max_token = 0;
 396     gsize loop;
 397 
 398     for (loop = 0; loop < len - 1; loop++)
 399         if (str[loop] == '\\' && g_ascii_isdigit (str[loop + 1]))
 400         {
 401             if (str_is_char_escaped (str, &str[loop]))
 402                 continue;
 403             if (max_token < str[loop + 1] - '0')
 404                 max_token = str[loop + 1] - '0';
 405         }
 406         else if (str[loop] == '$' && str[loop + 1] == '{')
 407         {
 408             gsize tmp_len;
 409 
 410             if (str_is_char_escaped (str, &str[loop]))
 411                 continue;
 412 
 413             for (tmp_len = 0;
 414                  loop + tmp_len + 2 < len && (str[loop + 2 + tmp_len] & (char) 0xf0) == 0x30;
 415                  tmp_len++);
 416 
 417             if (str[loop + 2 + tmp_len] == '}')
 418             {
 419                 int tmp_token;
 420                 char *tmp_str;
 421 
 422                 tmp_str = g_strndup (&str[loop + 2], tmp_len);
 423                 tmp_token = atoi (tmp_str);
 424                 if (max_token < tmp_token)
 425                     max_token = tmp_token;
 426                 g_free (tmp_str);
 427             }
 428         }
 429 
 430     return max_token;
 431 }
 432 
 433 /* --------------------------------------------------------------------------------------------- */
 434 
 435 static char *
 436 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]  */
 437 {
 438     int fnd_start = 0, fnd_end = 0;
 439 
 440 #ifdef SEARCH_TYPE_GLIB
 441     g_match_info_fetch_pos (lc_mc_search->regex_match_info, lc_index, &fnd_start, &fnd_end);
 442 #else /* SEARCH_TYPE_GLIB */
 443     fnd_start = lc_mc_search->iovector[lc_index * 2 + 0];
 444     fnd_end = lc_mc_search->iovector[lc_index * 2 + 1];
 445 #endif /* SEARCH_TYPE_GLIB */
 446 
 447     if (fnd_end == fnd_start)
 448         return g_strdup ("");
 449 
 450     return g_strndup (lc_mc_search->regex_buffer->str + fnd_start, fnd_end - fnd_start);
 451 
 452 }
 453 
 454 /* --------------------------------------------------------------------------------------------- */
 455 
 456 static gboolean
 457 mc_search_regex__replace_handle_esc_seq (const GString *replace_str, const gsize current_pos,
     /* [previous][next][first][last][top][bottom][index][help]  */
 458                                          gsize *skip_len, int *ret)
 459 {
 460     char *curr_str = &(replace_str->str[current_pos]);
 461     char c = curr_str[1];
 462 
 463     if (replace_str->len > current_pos + 2)
 464     {
 465         if (c == '{')
 466         {
 467             for (*skip_len = 2; /* \{ */
 468                  current_pos + *skip_len < replace_str->len && curr_str[*skip_len] >= '0'
 469                  && curr_str[*skip_len] <= '7'; (*skip_len)++)
 470                 ;
 471 
 472             if (current_pos + *skip_len < replace_str->len && curr_str[*skip_len] == '}')
 473             {
 474                 (*skip_len)++;
 475                 *ret = REPLACE_PREPARE_T_ESCAPE_SEQ;
 476                 return FALSE;
 477             }
 478             else
 479             {
 480                 *ret = REPLACE_PREPARE_T_NOTHING_SPECIAL;
 481                 return TRUE;
 482             }
 483         }
 484 
 485         if (c == 'x')
 486         {
 487             *skip_len = 2;      /* \x */
 488             c = curr_str[2];
 489             if (c == '{')
 490             {
 491                 for (*skip_len = 3;     /* \x{ */
 492                      current_pos + *skip_len < replace_str->len
 493                      && g_ascii_isxdigit ((guchar) curr_str[*skip_len]); (*skip_len)++)
 494                     ;
 495 
 496                 if (current_pos + *skip_len < replace_str->len && curr_str[*skip_len] == '}')
 497                 {
 498                     (*skip_len)++;
 499                     *ret = REPLACE_PREPARE_T_ESCAPE_SEQ;
 500                     return FALSE;
 501                 }
 502                 else
 503                 {
 504                     *ret = REPLACE_PREPARE_T_NOTHING_SPECIAL;
 505                     return TRUE;
 506                 }
 507             }
 508             else if (!g_ascii_isxdigit ((guchar) c))
 509             {
 510                 *skip_len = 2;  /* \x without number behind */
 511                 *ret = REPLACE_PREPARE_T_NOTHING_SPECIAL;
 512                 return FALSE;
 513             }
 514             else
 515             {
 516                 c = curr_str[3];
 517                 if (!g_ascii_isxdigit ((guchar) c))
 518                     *skip_len = 3;      /* \xH */
 519                 else
 520                     *skip_len = 4;      /* \xHH */
 521                 *ret = REPLACE_PREPARE_T_ESCAPE_SEQ;
 522                 return FALSE;
 523             }
 524         }
 525     }
 526 
 527     if (strchr ("ntvbrfa", c) != NULL)
 528     {
 529         *skip_len = 2;
 530         *ret = REPLACE_PREPARE_T_ESCAPE_SEQ;
 531         return FALSE;
 532     }
 533     return TRUE;
 534 }
 535 
 536 /* --------------------------------------------------------------------------------------------- */
 537 
 538 static int
 539 mc_search_regex__process_replace_str (const GString *replace_str, const gsize current_pos,
     /* [previous][next][first][last][top][bottom][index][help]  */
 540                                       gsize *skip_len, replace_transform_type_t *replace_flags)
 541 {
 542     int ret = -1;
 543     const char *curr_str = &(replace_str->str[current_pos]);
 544 
 545     if (current_pos > replace_str->len)
 546         return REPLACE_PREPARE_T_NOTHING_SPECIAL;
 547 
 548     *skip_len = 0;
 549 
 550     if (replace_str->len > current_pos + 2 && curr_str[0] == '$' && curr_str[1] == '{'
 551         && (curr_str[2] & (char) 0xf0) == 0x30)
 552     {
 553         char *tmp_str;
 554 
 555         if (str_is_char_escaped (replace_str->str, curr_str))
 556         {
 557             *skip_len = 1;
 558             return REPLACE_PREPARE_T_NOTHING_SPECIAL;
 559         }
 560 
 561         for (*skip_len = 0;
 562              current_pos + *skip_len + 2 < replace_str->len
 563              && (curr_str[2 + *skip_len] & (char) 0xf0) == 0x30; (*skip_len)++)
 564             ;
 565 
 566         if (curr_str[2 + *skip_len] != '}')
 567             return REPLACE_PREPARE_T_NOTHING_SPECIAL;
 568 
 569         tmp_str = g_strndup (curr_str + 2, *skip_len);
 570         if (tmp_str == NULL)
 571             return REPLACE_PREPARE_T_NOTHING_SPECIAL;
 572 
 573         ret = atoi (tmp_str);
 574         g_free (tmp_str);
 575 
 576         *skip_len += 3;         /* ${} */
 577         return ret;             /* capture buffer index >= 0 */
 578     }
 579 
 580     if (curr_str[0] == '\\' && replace_str->len > current_pos + 1)
 581     {
 582         if (str_is_char_escaped (replace_str->str, curr_str))
 583         {
 584             *skip_len = 1;
 585             return REPLACE_PREPARE_T_NOTHING_SPECIAL;
 586         }
 587 
 588         if (g_ascii_isdigit (curr_str[1]))
 589         {
 590             ret = g_ascii_digit_value (curr_str[1]);    /* capture buffer index >= 0 */
 591             *skip_len = 2;      /* \\ and one digit */
 592             return ret;
 593         }
 594 
 595         if (!mc_search_regex__replace_handle_esc_seq (replace_str, current_pos, skip_len, &ret))
 596             return ret;
 597 
 598         ret = REPLACE_PREPARE_T_REPLACE_FLAG;
 599         *skip_len += 2;
 600 
 601         switch (curr_str[1])
 602         {
 603         case 'U':
 604             *replace_flags |= REPLACE_T_UPP_TRANSFORM;
 605             *replace_flags &= ~REPLACE_T_LOW_TRANSFORM;
 606             break;
 607         case 'u':
 608             *replace_flags |= REPLACE_T_UPP_TRANSFORM_CHAR;
 609             break;
 610         case 'L':
 611             *replace_flags |= REPLACE_T_LOW_TRANSFORM;
 612             *replace_flags &= ~REPLACE_T_UPP_TRANSFORM;
 613             break;
 614         case 'l':
 615             *replace_flags |= REPLACE_T_LOW_TRANSFORM_CHAR;
 616             break;
 617         case 'E':
 618             *replace_flags = REPLACE_T_NO_TRANSFORM;
 619             break;
 620         default:
 621             ret = REPLACE_PREPARE_T_NOTHING_SPECIAL;
 622             break;
 623         }
 624     }
 625     return ret;
 626 }
 627 
 628 /* --------------------------------------------------------------------------------------------- */
 629 
 630 static void
 631 mc_search_regex__process_append_str (GString *dest_str, const char *from, gsize len,
     /* [previous][next][first][last][top][bottom][index][help]  */
 632                                      replace_transform_type_t *replace_flags)
 633 {
 634     gsize loop;
 635     gsize char_len;
 636 
 637     if (len == (gsize) (-1))
 638         len = strlen (from);
 639 
 640     if (*replace_flags == REPLACE_T_NO_TRANSFORM)
 641     {
 642         g_string_append_len (dest_str, from, len);
 643         return;
 644     }
 645 
 646     for (loop = 0; loop < len; loop += char_len)
 647     {
 648         GString *tmp_string = NULL;
 649         GString *s;
 650 
 651         s = mc_search__get_one_symbol (NULL, from + loop, len - loop, NULL);
 652         char_len = s->len;
 653 
 654         if ((*replace_flags & REPLACE_T_UPP_TRANSFORM_CHAR) != 0)
 655         {
 656             *replace_flags &= ~REPLACE_T_UPP_TRANSFORM_CHAR;
 657             tmp_string = mc_search__toupper_case_str (NULL, s);
 658             g_string_append_len (dest_str, tmp_string->str, tmp_string->len);
 659         }
 660         else if ((*replace_flags & REPLACE_T_LOW_TRANSFORM_CHAR) != 0)
 661         {
 662             *replace_flags &= ~REPLACE_T_LOW_TRANSFORM_CHAR;
 663             tmp_string = mc_search__tolower_case_str (NULL, s);
 664             g_string_append_len (dest_str, tmp_string->str, tmp_string->len);
 665         }
 666         else if ((*replace_flags & REPLACE_T_UPP_TRANSFORM) != 0)
 667         {
 668             tmp_string = mc_search__toupper_case_str (NULL, s);
 669             g_string_append_len (dest_str, tmp_string->str, tmp_string->len);
 670         }
 671         else if ((*replace_flags & REPLACE_T_LOW_TRANSFORM) != 0)
 672         {
 673             tmp_string = mc_search__tolower_case_str (NULL, s);
 674             g_string_append_len (dest_str, tmp_string->str, tmp_string->len);
 675         }
 676 
 677         g_string_free (s, TRUE);
 678         if (tmp_string != NULL)
 679             g_string_free (tmp_string, TRUE);
 680     }
 681 }
 682 
 683 /* --------------------------------------------------------------------------------------------- */
 684 
 685 static void
 686 mc_search_regex__process_escape_sequence (GString *dest_str, const char *from, gsize len,
     /* [previous][next][first][last][top][bottom][index][help]  */
 687                                           replace_transform_type_t *replace_flags, 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         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 
 840 #ifdef HAVE_PCRE2
 841         int errcode;
 842         char error[BUF_SMALL];
 843         size_t erroffset;
 844         int pcre_options = PCRE2_MULTILINE;
 845 #else
 846         const char *error;
 847         int erroffset;
 848         int pcre_options = PCRE_EXTRA | PCRE_MULTILINE;
 849 #endif
 850 
 851         if (str_isutf8 (charset) && mc_global.utf8_display)
 852         {
 853 #ifdef HAVE_PCRE2
 854             pcre_options |= PCRE2_UTF;
 855             if (!lc_mc_search->is_case_sensitive)
 856                 pcre_options |= PCRE2_CASELESS;
 857 #else
 858             pcre_options |= PCRE_UTF8;
 859             if (!lc_mc_search->is_case_sensitive)
 860                 pcre_options |= PCRE_CASELESS;
 861 #endif
 862         }
 863         else if (!lc_mc_search->is_case_sensitive)
 864         {
 865             GString *tmp;
 866 
 867             tmp = mc_search_cond->str;
 868             mc_search_cond->str = mc_search__cond_struct_new_regex_ci_str (charset, tmp);
 869             g_string_free (tmp, TRUE);
 870         }
 871 
 872         mc_search_cond->regex_handle =
 873 #ifdef HAVE_PCRE2
 874             pcre2_compile ((unsigned char *) mc_search_cond->str->str, PCRE2_ZERO_TERMINATED,
 875                            pcre_options, &errcode, &erroffset, NULL);
 876 #else
 877             pcre_compile (mc_search_cond->str->str, pcre_options, &error, &erroffset, NULL);
 878 #endif
 879         if (mc_search_cond->regex_handle == NULL)
 880         {
 881 #ifdef HAVE_PCRE2
 882             pcre2_get_error_message (errcode, (unsigned char *) error, sizeof (error));
 883 #endif
 884             mc_search_set_error (lc_mc_search, MC_SEARCH_E_REGEX_COMPILE, "%s", error);
 885             return;
 886         }
 887 #ifdef HAVE_PCRE2
 888         if (pcre2_jit_compile (mc_search_cond->regex_handle, PCRE2_JIT_COMPLETE) && *error != '\0')
 889 #else
 890         lc_mc_search->regex_match_info = pcre_study (mc_search_cond->regex_handle, 0, &error);
 891         if (lc_mc_search->regex_match_info == NULL && error != NULL)
 892 #endif
 893         {
 894             mc_search_set_error (lc_mc_search, MC_SEARCH_E_REGEX_COMPILE, "%s", error);
 895             MC_PTR_FREE (mc_search_cond->regex_handle);
 896             return;
 897         }
 898 #endif /* SEARCH_TYPE_GLIB */
 899     }
 900 
 901     lc_mc_search->is_utf8 = str_isutf8 (charset);
 902 }
 903 
 904 /* --------------------------------------------------------------------------------------------- */
 905 
 906 gboolean
 907 mc_search__run_regex (mc_search_t *lc_mc_search, const void *user_data,
     /* [previous][next][first][last][top][bottom][index][help]  */
 908                       gsize start_search, gsize end_search, gsize *found_len)
 909 {
 910     mc_search_cbret_t ret = MC_SEARCH_CB_NOTFOUND;
 911     gsize current_pos, virtual_pos;
 912     gint start_pos;
 913     gint end_pos;
 914 
 915     if (lc_mc_search->regex_buffer != NULL)
 916         g_string_set_size (lc_mc_search->regex_buffer, 0);
 917     else
 918         lc_mc_search->regex_buffer = g_string_sized_new (64);
 919 
 920     virtual_pos = current_pos = start_search;
 921     while (virtual_pos <= end_search)
 922     {
 923         g_string_set_size (lc_mc_search->regex_buffer, 0);
 924         lc_mc_search->start_buffer = current_pos;
 925 
 926         if (lc_mc_search->search_fn != NULL)
 927         {
 928             while (TRUE)
 929             {
 930                 int current_chr = '\n'; /* stop search symbol */
 931 
 932                 ret = lc_mc_search->search_fn (user_data, current_pos, &current_chr);
 933 
 934                 if (ret == MC_SEARCH_CB_ABORT)
 935                     break;
 936 
 937                 if (ret == MC_SEARCH_CB_INVALID)
 938                     continue;
 939 
 940                 current_pos++;
 941 
 942                 if (ret == MC_SEARCH_CB_SKIP)
 943                     continue;
 944 
 945                 virtual_pos++;
 946 
 947                 g_string_append_c (lc_mc_search->regex_buffer, (char) current_chr);
 948 
 949                 if ((char) current_chr == '\n' || virtual_pos > end_search)
 950                     break;
 951             }
 952         }
 953         else
 954         {
 955             /* optimization for standard case (for search from file manager)
 956              *  where there is no MC_SEARCH_CB_INVALID or MC_SEARCH_CB_SKIP
 957              *  return codes, so we can copy line at regex buffer all at once
 958              */
 959             while (TRUE)
 960             {
 961                 const char current_chr = ((const char *) user_data)[current_pos];
 962 
 963                 if (current_chr == '\0')
 964                     break;
 965 
 966                 current_pos++;
 967 
 968                 if (current_chr == '\n' || current_pos > end_search)
 969                     break;
 970             }
 971 
 972             /* use virtual_pos as index of start of current chunk */
 973             g_string_append_len (lc_mc_search->regex_buffer, (const char *) user_data + virtual_pos,
 974                                  current_pos - virtual_pos);
 975             virtual_pos = current_pos;
 976         }
 977 
 978         switch (mc_search__regex_found_cond (lc_mc_search, lc_mc_search->regex_buffer))
 979         {
 980         case COND__FOUND_OK:
 981 #ifdef SEARCH_TYPE_GLIB
 982             g_match_info_fetch_pos (lc_mc_search->regex_match_info, 0, &start_pos, &end_pos);
 983 #else /* SEARCH_TYPE_GLIB */
 984             start_pos = lc_mc_search->iovector[0];
 985             end_pos = lc_mc_search->iovector[1];
 986 #endif /* SEARCH_TYPE_GLIB */
 987             if (found_len != NULL)
 988                 *found_len = end_pos - start_pos;
 989             lc_mc_search->normal_offset = lc_mc_search->start_buffer + start_pos;
 990             return TRUE;
 991         case COND__NOT_ALL_FOUND:
 992             break;
 993         default:
 994             g_string_free (lc_mc_search->regex_buffer, TRUE);
 995             lc_mc_search->regex_buffer = NULL;
 996             return FALSE;
 997         }
 998 
 999         if ((lc_mc_search->update_fn != NULL) &&
1000             ((lc_mc_search->update_fn) (user_data, current_pos) == MC_SEARCH_CB_ABORT))
1001             ret = MC_SEARCH_CB_ABORT;
1002 
1003         if (ret == MC_SEARCH_CB_ABORT || ret == MC_SEARCH_CB_NOTFOUND)
1004             break;
1005     }
1006 
1007     g_string_free (lc_mc_search->regex_buffer, TRUE);
1008     lc_mc_search->regex_buffer = NULL;
1009 
1010     MC_PTR_FREE (lc_mc_search->error_str);
1011     lc_mc_search->error = ret == MC_SEARCH_CB_ABORT ? MC_SEARCH_E_ABORT : MC_SEARCH_E_NOTFOUND;
1012 
1013     return FALSE;
1014 }
1015 
1016 /* --------------------------------------------------------------------------------------------- */
1017 
1018 GString *
1019 mc_search_regex_prepare_replace_str (mc_search_t *lc_mc_search, GString *replace_str)
     /* [previous][next][first][last][top][bottom][index][help]  */
1020 {
1021     GString *ret;
1022 
1023     int num_replace_tokens;
1024     gsize loop;
1025     gsize prev = 0;
1026     replace_transform_type_t replace_flags = REPLACE_T_NO_TRANSFORM;
1027 
1028     num_replace_tokens =
1029         mc_search_regex__get_max_num_of_replace_tokens (replace_str->str, replace_str->len);
1030 
1031     if (lc_mc_search->num_results < 0)
1032         return mc_g_string_dup (replace_str);
1033 
1034     if (num_replace_tokens > lc_mc_search->num_results - 1
1035         || num_replace_tokens > MC_SEARCH__NUM_REPLACE_ARGS)
1036     {
1037         mc_search_set_error (lc_mc_search, MC_SEARCH_E_REGEX_REPLACE, "%s",
1038                              _(STR_E_RPL_NOT_EQ_TO_FOUND));
1039         return NULL;
1040     }
1041 
1042     ret = g_string_sized_new (64);
1043 
1044     for (loop = 0; loop < replace_str->len - 1; loop++)
1045     {
1046         int lc_index;
1047         gchar *tmp_str;
1048         gsize len = 0;
1049 
1050         lc_index = mc_search_regex__process_replace_str (replace_str, loop, &len, &replace_flags);
1051 
1052         if (lc_index == REPLACE_PREPARE_T_NOTHING_SPECIAL)
1053         {
1054             if (len != 0)
1055             {
1056                 mc_search_regex__process_append_str (ret, replace_str->str + prev, loop - prev,
1057                                                      &replace_flags);
1058                 mc_search_regex__process_append_str (ret, replace_str->str + loop + 1, len - 1,
1059                                                      &replace_flags);
1060                 prev = loop + len;
1061                 loop = prev - 1;        /* prepare to loop++ */
1062             }
1063 
1064             continue;
1065         }
1066 
1067         if (lc_index == REPLACE_PREPARE_T_REPLACE_FLAG)
1068         {
1069             if (loop != 0)
1070                 mc_search_regex__process_append_str (ret, replace_str->str + prev, loop - prev,
1071                                                      &replace_flags);
1072             prev = loop + len;
1073             loop = prev - 1;    /* prepare to loop++ */
1074             continue;
1075         }
1076 
1077         /* escape sequence */
1078         if (lc_index == REPLACE_PREPARE_T_ESCAPE_SEQ)
1079         {
1080             mc_search_regex__process_append_str (ret, replace_str->str + prev, loop - prev,
1081                                                  &replace_flags);
1082             /* call process_escape_sequence without starting '\\' */
1083             mc_search_regex__process_escape_sequence (ret, replace_str->str + loop + 1, len - 1,
1084                                                       &replace_flags, lc_mc_search->is_utf8);
1085             prev = loop + len;
1086             loop = prev - 1;    /* prepare to loop++ */
1087             continue;
1088         }
1089 
1090         /* invalid capture buffer number */
1091         if (lc_index > lc_mc_search->num_results)
1092         {
1093             g_string_free (ret, TRUE);
1094             mc_search_set_error (lc_mc_search, MC_SEARCH_E_REGEX_REPLACE,
1095                                  _(STR_E_RPL_INVALID_TOKEN), lc_index);
1096             return NULL;
1097         }
1098 
1099         tmp_str = mc_search_regex__get_token_by_num (lc_mc_search, lc_index);
1100 
1101         if (loop != 0)
1102             mc_search_regex__process_append_str (ret, replace_str->str + prev, loop - prev,
1103                                                  &replace_flags);
1104 
1105         mc_search_regex__process_append_str (ret, tmp_str, -1, &replace_flags);
1106         g_free (tmp_str);
1107 
1108         prev = loop + len;
1109         loop = prev - 1;        /* prepare to loop++ */
1110     }
1111 
1112     mc_search_regex__process_append_str (ret, replace_str->str + prev, replace_str->len - prev,
1113                                          &replace_flags);
1114 
1115     return ret;
1116 }

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