5 Copyright (C) 2009 The Free Software Foundation, Inc.
8 Slava Zanko <slavazanko@gmail.com>, 2009.
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software; you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of the
15 License, or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be
18 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
19 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
32 #include "lib/global.h"
33 #include "lib/strutil.h"
35 #include "src/strescape.h"
36 #include "src/charsets.h"
41 /*** global variables ****************************************************************************/
43 /*** file scope macro definitions ****************************************************************/
45 /*** file scope type declarations ****************************************************************/
48 REPLACE_T_NO_TRANSFORM
= 0,
49 REPLACE_T_UPP_TRANSFORM_CHAR
= 1,
50 REPLACE_T_LOW_TRANSFORM_CHAR
= 2,
51 REPLACE_T_UPP_TRANSFORM
= 4,
52 REPLACE_T_LOW_TRANSFORM
= 8
53 } replace_transform_type_t
;
55 /*** file scope variables ************************************************************************/
57 /*** file scope functions ************************************************************************/
60 mc_search__regex_str_append_if_special (GString
* copy_to
, GString
* regex_str
, gsize
* offset
)
64 const char **spec_chr
;
65 const char *special_chars
[] = {
80 spec_chr
= special_chars
;
82 tmp_regex_str
= &(regex_str
->str
[*offset
]);
85 spec_chr_len
= strlen (*spec_chr
);
86 if (!strncmp (tmp_regex_str
, *spec_chr
, spec_chr_len
)) {
87 if (!strutils_is_char_escaped (regex_str
->str
, tmp_regex_str
)) {
88 if (!strncmp ("\\x", *spec_chr
, spec_chr_len
)) {
89 if (*(tmp_regex_str
+ spec_chr_len
) == '{') {
90 while ((spec_chr_len
< regex_str
->len
- *offset
)
91 && *(tmp_regex_str
+ spec_chr_len
) != '}')
93 if (*(tmp_regex_str
+ spec_chr_len
) == '}')
98 g_string_append_len (copy_to
, tmp_regex_str
, spec_chr_len
);
99 *offset
+= spec_chr_len
;
109 /* --------------------------------------------------------------------------------------------- */
111 mc_search__cond_struct_new_regex_hex_add (const char *charset
, GString
* str_to
,
112 const char *one_char
, gsize str_len
)
118 upp
= mc_search__toupper_case_str (charset
, one_char
, str_len
);
119 low
= mc_search__tolower_case_str (charset
, one_char
, str_len
);
121 for (loop
= 0; loop
< upp
->len
; loop
++) {
123 if (loop
< low
->len
) {
124 if (upp
->str
[loop
] == low
->str
[loop
])
125 tmp_str
= g_strdup_printf ("\\x%02X", (unsigned char) upp
->str
[loop
]);
128 g_strdup_printf ("[\\x%02X\\x%02X]", (unsigned char) upp
->str
[loop
],
129 (unsigned char) low
->str
[loop
]);
131 tmp_str
= g_strdup_printf ("\\x%02X", (unsigned char) upp
->str
[loop
]);
133 g_string_append (str_to
, tmp_str
);
136 g_string_free (upp
, TRUE
);
137 g_string_free (low
, TRUE
);
140 /* --------------------------------------------------------------------------------------------- */
143 mc_search__cond_struct_new_regex_accum_append (const char *charset
, GString
* str_to
,
146 GString
*recoded_part
;
149 gboolean just_letters
;
152 recoded_part
= g_string_new ("");
154 while (loop
< str_from
->len
) {
156 mc_search__get_one_symbol (charset
, &(str_from
->str
[loop
]),
157 (str_from
->len
- loop
> 6) ? 6 : str_from
->len
- loop
,
159 if (!strlen (one_char
)) {
164 mc_search__cond_struct_new_regex_hex_add (charset
, recoded_part
, one_char
,
167 g_string_append (recoded_part
, one_char
);
169 loop
+= strlen (one_char
);
170 if (!strlen (one_char
))
175 g_string_append (str_to
, recoded_part
->str
);
176 g_string_free (recoded_part
, TRUE
);
177 g_string_set_size (str_from
, 0);
180 /* --------------------------------------------------------------------------------------------- */
183 mc_search__cond_struct_new_regex_ci_str (const char *charset
, const char *str
, gsize str_len
)
185 GString
*accumulator
, *spec_char
, *ret_str
;
188 tmp
= g_string_new_len (str
, str_len
);
191 ret_str
= g_string_new ("");
192 accumulator
= g_string_new ("");
193 spec_char
= g_string_new ("");
196 while (loop
<= str_len
) {
197 if (mc_search__regex_str_append_if_special (spec_char
, tmp
, &loop
)) {
198 mc_search__cond_struct_new_regex_accum_append (charset
, ret_str
, accumulator
);
199 g_string_append_len (ret_str
, spec_char
->str
, spec_char
->len
);
200 g_string_set_size (spec_char
, 0);
204 if (tmp
->str
[loop
] == '[' && !strutils_is_char_escaped (tmp
->str
, &(tmp
->str
[loop
]))) {
205 mc_search__cond_struct_new_regex_accum_append (charset
, ret_str
, accumulator
);
207 while (loop
< str_len
&& !(tmp
->str
[loop
] == ']'
208 && !strutils_is_char_escaped (tmp
->str
,
209 &(tmp
->str
[loop
])))) {
210 g_string_append_c (ret_str
, tmp
->str
[loop
]);
214 g_string_append_c (ret_str
, tmp
->str
[loop
]);
221 g_string_append_c (accumulator
, tmp
->str
[loop
]);
224 mc_search__cond_struct_new_regex_accum_append (charset
, ret_str
, accumulator
);
226 g_string_free (accumulator
, TRUE
);
227 g_string_free (spec_char
, TRUE
);
228 g_string_free (tmp
, TRUE
);
232 /* --------------------------------------------------------------------------------------------- */
234 static mc_search__found_cond_t
235 mc_search__regex_found_cond_one (mc_search_t
* lc_mc_search
, mc_search_regex_t
* regex
,
236 GString
* search_str
)
238 #ifdef SEARCH_TYPE_GLIB
239 GError
*error
= NULL
;
241 if (!g_regex_match_full
242 (regex
, search_str
->str
, -1, 0, G_REGEX_MATCH_NEWLINE_ANY
, &lc_mc_search
->regex_match_info
,
244 g_match_info_free (lc_mc_search
->regex_match_info
);
245 lc_mc_search
->regex_match_info
= NULL
;
247 lc_mc_search
->error
= MC_SEARCH_E_REGEX
;
248 lc_mc_search
->error_str
= str_conv_gerror_message (error
, _(" Regular expression error "));
249 g_error_free (error
);
250 return COND__FOUND_ERROR
;
252 return COND__NOT_FOUND
;
254 lc_mc_search
->num_rezults
= g_match_info_get_match_count (lc_mc_search
->regex_match_info
);
255 #else /* SEARCH_TYPE_GLIB */
256 lc_mc_search
->num_rezults
= pcre_exec (regex
, lc_mc_search
->regex_match_info
,
257 search_str
->str
, search_str
->len
- 1, 0, 0, lc_mc_search
->iovector
,
258 MC_SEARCH__NUM_REPLACE_ARGS
);
259 if (lc_mc_search
->num_rezults
< 0) {
260 return COND__NOT_FOUND
;
262 #endif /* SEARCH_TYPE_GLIB */
263 return COND__FOUND_OK
;
267 /* --------------------------------------------------------------------------------------------- */
269 static mc_search__found_cond_t
270 mc_search__regex_found_cond (mc_search_t
* lc_mc_search
, GString
* search_str
)
273 mc_search_cond_t
*mc_search_cond
;
274 mc_search__found_cond_t ret
;
276 for (loop1
= 0; loop1
< lc_mc_search
->conditions
->len
; loop1
++) {
277 mc_search_cond
= (mc_search_cond_t
*) g_ptr_array_index (lc_mc_search
->conditions
, loop1
);
279 if (!mc_search_cond
->regex_handle
)
282 ret
= mc_search__regex_found_cond_one (lc_mc_search
, mc_search_cond
->regex_handle
, search_str
);
284 if (ret
!= COND__NOT_FOUND
)
287 return COND__NOT_ALL_FOUND
;
290 /* --------------------------------------------------------------------------------------------- */
293 mc_search_regex__get_max_num_of_replace_tokens (const gchar
* str
, gsize len
)
297 for (loop
= 0; loop
< len
- 1; loop
++) {
298 if (str
[loop
] == '\\' && (str
[loop
+ 1] & (char) 0xf0) == 0x30 /* 0-9 */ ) {
299 if (strutils_is_char_escaped (str
, &str
[loop
]))
301 if (max_token
< str
[loop
+ 1] - '0')
302 max_token
= str
[loop
+ 1] - '0';
305 if (str
[loop
] == '$' && str
[loop
+ 1] == '{') {
309 if (strutils_is_char_escaped (str
, &str
[loop
]))
313 loop
+ tmp_len
+ 2 < len
&& (str
[loop
+ 2 + tmp_len
] & (char) 0xf0) == 0x30;
315 if (str
[loop
+ 2 + tmp_len
] == '}') {
316 tmp_str
= g_strndup (&str
[loop
+ 2], tmp_len
);
317 tmp_token
= atoi (tmp_str
);
318 if (max_token
< tmp_token
)
319 max_token
= tmp_token
;
327 /* --------------------------------------------------------------------------------------------- */
330 mc_search_regex__get_token_by_num (const mc_search_t
* lc_mc_search
, gsize lc_index
)
332 int fnd_start
= 0, fnd_end
= 0;
334 #ifdef SEARCH_TYPE_GLIB
335 g_match_info_fetch_pos (lc_mc_search
->regex_match_info
, lc_index
, &fnd_start
, &fnd_end
);
336 #else /* SEARCH_TYPE_GLIB */
337 fnd_start
= lc_mc_search
->iovector
[lc_index
* 2 + 0];
338 fnd_end
= lc_mc_search
->iovector
[lc_index
* 2 + 1];
339 #endif /* SEARCH_TYPE_GLIB */
341 if (fnd_end
- fnd_start
== 0)
344 return g_strndup (lc_mc_search
->regex_buffer
->str
+ fnd_start
, fnd_end
- fnd_start
);
348 /* --------------------------------------------------------------------------------------------- */
350 mc_search_regex__process_replace_str (const GString
* replace_str
, const gsize current_pos
,
351 gsize
* skip_len
, replace_transform_type_t
* replace_flags
)
355 const char *curr_str
= &(replace_str
->str
[current_pos
]);
357 if (current_pos
> replace_str
->len
)
362 if (*curr_str
== '$' && *(curr_str
+ 1) == '{' && (*(curr_str
+ 2) & (char) 0xf0) == 0x30) {
363 if (strutils_is_char_escaped (replace_str
->str
, curr_str
)) {
369 current_pos
+ *skip_len
+ 2 < replace_str
->len
370 && (*(curr_str
+ 2 + *skip_len
) & (char) 0xf0) == 0x30; (*skip_len
)++);
372 if (*(curr_str
+ 2 + *skip_len
) != '}')
375 tmp_str
= g_strndup (curr_str
+ 2, *skip_len
);
379 ret
= atoi (tmp_str
);
382 *skip_len
+= 3; /* ${} */
386 if (*curr_str
== '\\') {
387 if (strutils_is_char_escaped (replace_str
->str
, curr_str
)) {
392 if ((*(curr_str
+ 1) & (char) 0xf0) == 0x30) {
393 ret
= *(curr_str
+ 1) - '0';
394 *skip_len
= 2; /* \\ and one digit */
399 switch (*(curr_str
+ 1)) {
401 *replace_flags
|= REPLACE_T_UPP_TRANSFORM
;
402 *replace_flags
&= ~REPLACE_T_LOW_TRANSFORM
;
405 *replace_flags
|= REPLACE_T_UPP_TRANSFORM_CHAR
;
408 *replace_flags
|= REPLACE_T_LOW_TRANSFORM
;
409 *replace_flags
&= ~REPLACE_T_UPP_TRANSFORM
;
412 *replace_flags
|= REPLACE_T_LOW_TRANSFORM_CHAR
;
415 *replace_flags
= REPLACE_T_NO_TRANSFORM
;
425 mc_search_regex__process_append_str (GString
* dest_str
, const char *from
, gsize len
,
426 replace_transform_type_t
* replace_flags
)
433 if (len
== (gsize
) -1)
436 if (*replace_flags
== REPLACE_T_NO_TRANSFORM
) {
437 g_string_append_len (dest_str
, from
, len
);
441 tmp_str
= mc_search__get_one_symbol (NULL
, from
+ loop
, len
- loop
, NULL
);
442 char_len
= strlen (tmp_str
);
443 if (*replace_flags
& REPLACE_T_UPP_TRANSFORM_CHAR
) {
444 *replace_flags
&= !REPLACE_T_UPP_TRANSFORM_CHAR
;
445 tmp_string
= mc_search__toupper_case_str (NULL
, tmp_str
, char_len
);
446 g_string_append (dest_str
, tmp_string
->str
);
447 g_string_free (tmp_string
, TRUE
);
449 } else if (*replace_flags
& REPLACE_T_LOW_TRANSFORM_CHAR
) {
450 *replace_flags
&= !REPLACE_T_LOW_TRANSFORM_CHAR
;
451 tmp_string
= mc_search__toupper_case_str (NULL
, tmp_str
, char_len
);
452 g_string_append (dest_str
, tmp_string
->str
);
453 g_string_free (tmp_string
, TRUE
);
455 } else if (*replace_flags
& REPLACE_T_UPP_TRANSFORM
) {
456 tmp_string
= mc_search__toupper_case_str (NULL
, tmp_str
, char_len
);
457 g_string_append (dest_str
, tmp_string
->str
);
458 g_string_free (tmp_string
, TRUE
);
460 } else if (*replace_flags
& REPLACE_T_LOW_TRANSFORM
) {
461 tmp_string
= mc_search__tolower_case_str (NULL
, tmp_str
, char_len
);
462 g_string_append (dest_str
, tmp_string
->str
);
463 g_string_free (tmp_string
, TRUE
);
466 g_string_append (dest_str
, tmp_str
);
474 /*** public functions ****************************************************************************/
477 mc_search__cond_struct_new_init_regex (const char *charset
, mc_search_t
* lc_mc_search
,
478 mc_search_cond_t
* mc_search_cond
)
481 #ifdef SEARCH_TYPE_GLIB
482 GError
*error
= NULL
;
483 #else /* SEARCH_TYPE_GLIB */
486 #endif /* SEARCH_TYPE_GLIB */
488 if (!lc_mc_search
->is_case_sentitive
) {
489 tmp
= g_string_new_len (mc_search_cond
->str
->str
, mc_search_cond
->str
->len
);
490 g_string_free (mc_search_cond
->str
, TRUE
);
491 mc_search_cond
->str
= mc_search__cond_struct_new_regex_ci_str (charset
, tmp
->str
, tmp
->len
);
492 g_string_free (tmp
, TRUE
);
494 #ifdef SEARCH_TYPE_GLIB
495 mc_search_cond
->regex_handle
=
496 g_regex_new (mc_search_cond
->str
->str
, G_REGEX_OPTIMIZE
| G_REGEX_RAW
| G_REGEX_DOTALL
, 0,
500 lc_mc_search
->error
= MC_SEARCH_E_REGEX_COMPILE
;
501 lc_mc_search
->error_str
= str_conv_gerror_message (error
, _(" Regular expression error "));
502 g_error_free (error
);
505 #else /* SEARCH_TYPE_GLIB */
506 mc_search_cond
->regex_handle
=
507 pcre_compile (mc_search_cond
->str
->str
, PCRE_EXTRA
, &error
, &erroffset
, NULL
);
508 if (mc_search_cond
->regex_handle
== NULL
) {
509 lc_mc_search
->error
= MC_SEARCH_E_REGEX_COMPILE
;
510 lc_mc_search
->error_str
= g_strdup (error
);
513 lc_mc_search
->regex_match_info
= pcre_study (mc_search_cond
->regex_handle
, 0, &error
);
514 if (lc_mc_search
->regex_match_info
== NULL
) {
516 lc_mc_search
->error
= MC_SEARCH_E_REGEX_COMPILE
;
517 lc_mc_search
->error_str
= g_strdup (error
);
518 g_free (mc_search_cond
->regex_handle
);
519 mc_search_cond
->regex_handle
= NULL
;
523 #endif /* SEARCH_TYPE_GLIB */
526 /* --------------------------------------------------------------------------------------------- */
529 mc_search__run_regex (mc_search_t
* lc_mc_search
, const void *user_data
,
530 gsize start_search
, gsize end_search
, gsize
* found_len
)
532 gsize current_pos
, virtual_pos
;
537 if (lc_mc_search
->regex_buffer
!= NULL
)
538 g_string_free (lc_mc_search
->regex_buffer
, TRUE
);
540 lc_mc_search
->regex_buffer
= g_string_new ("");
542 virtual_pos
= current_pos
= start_search
;
543 while (virtual_pos
<= end_search
) {
544 g_string_set_size (lc_mc_search
->regex_buffer
, 0);
545 lc_mc_search
->start_buffer
= current_pos
;
548 current_chr
= mc_search__get_char (lc_mc_search
, user_data
, current_pos
);
549 if (current_chr
== MC_SEARCH_CB_ABORT
)
554 if (current_chr
== MC_SEARCH_CB_SKIP
)
559 g_string_append_c (lc_mc_search
->regex_buffer
, (char) current_chr
);
562 if (current_chr
== 0 || (char) current_chr
== '\n')
565 if (virtual_pos
> end_search
)
569 switch (mc_search__regex_found_cond (lc_mc_search
, lc_mc_search
->regex_buffer
)) {
571 #ifdef SEARCH_TYPE_GLIB
572 g_match_info_fetch_pos (lc_mc_search
->regex_match_info
, 0, &start_pos
, &end_pos
);
573 #else /* SEARCH_TYPE_GLIB */
574 start_pos
= lc_mc_search
->iovector
[0];
575 end_pos
= lc_mc_search
->iovector
[1];
576 #endif /* SEARCH_TYPE_GLIB */
578 *found_len
= end_pos
- start_pos
;
579 lc_mc_search
->normal_offset
= lc_mc_search
->start_buffer
+ start_pos
;
582 case COND__NOT_ALL_FOUND
:
585 g_string_free (lc_mc_search
->regex_buffer
, TRUE
);
586 lc_mc_search
->regex_buffer
= NULL
;
590 if (( lc_mc_search
->update_fn
!= NULL
) &&
591 ( (lc_mc_search
->update_fn
) (user_data
, current_pos
) == MC_SEARCH_CB_ABORT
))
592 current_chr
= MC_SEARCH_CB_ABORT
;
594 if (current_chr
== MC_SEARCH_CB_ABORT
)
597 g_string_free (lc_mc_search
->regex_buffer
, TRUE
);
598 lc_mc_search
->regex_buffer
= NULL
;
599 lc_mc_search
->error
= MC_SEARCH_E_NOTFOUND
;
601 if (current_chr
!= MC_SEARCH_CB_ABORT
)
602 lc_mc_search
->error_str
= g_strdup (_(STR_E_NOTFOUND
));
604 lc_mc_search
->error_str
= NULL
;
609 /* --------------------------------------------------------------------------------------------- */
611 mc_search_regex_prepare_replace_str (mc_search_t
* lc_mc_search
, GString
* replace_str
)
616 int num_replace_tokens
, lc_index
;
620 replace_transform_type_t replace_flags
= REPLACE_T_NO_TRANSFORM
;
623 mc_search_regex__get_max_num_of_replace_tokens (replace_str
->str
, replace_str
->len
);
625 if (lc_mc_search
->num_rezults
< 0)
626 return g_string_new_len (replace_str
->str
, replace_str
->len
);
628 if (num_replace_tokens
> lc_mc_search
->num_rezults
- 1
629 || num_replace_tokens
> MC_SEARCH__NUM_REPLACE_ARGS
) {
630 lc_mc_search
->error
= MC_SEARCH_E_REGEX_REPLACE
;
631 lc_mc_search
->error_str
= g_strdup (STR_E_RPL_NOT_EQ_TO_FOUND
);
635 ret
= g_string_new ("");
636 prev_str
= replace_str
->str
;
637 for (loop
= 0; loop
< replace_str
->len
- 1; loop
++) {
638 lc_index
= mc_search_regex__process_replace_str (replace_str
, loop
, &len
, &replace_flags
);
640 if (lc_index
== -1) {
642 mc_search_regex__process_append_str (ret
, prev_str
,
643 replace_str
->str
- prev_str
+ loop
,
645 mc_search_regex__process_append_str (ret
, replace_str
->str
+ loop
+ 1, len
- 1,
647 prev_str
= replace_str
->str
+ loop
+ len
;
653 if (lc_index
== -2) {
655 mc_search_regex__process_append_str (ret
, prev_str
,
656 replace_str
->str
- prev_str
+ loop
,
658 prev_str
= replace_str
->str
+ loop
+ len
;
663 if (lc_index
> lc_mc_search
->num_rezults
) {
664 g_string_free (ret
, TRUE
);
665 lc_mc_search
->error
= MC_SEARCH_E_REGEX_REPLACE
;
666 lc_mc_search
->error_str
= g_strdup_printf (STR_E_RPL_INVALID_TOKEN
, lc_index
);
670 tmp_str
= mc_search_regex__get_token_by_num (lc_mc_search
, lc_index
);
675 mc_search_regex__process_append_str (ret
, prev_str
, replace_str
->str
- prev_str
+ loop
,
677 prev_str
= replace_str
->str
+ loop
+ len
;
679 mc_search_regex__process_append_str (ret
, tmp_str
, -1, &replace_flags
);
683 mc_search_regex__process_append_str (ret
, prev_str
,
684 replace_str
->str
- prev_str
+ replace_str
->len
,