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"
34 #include "lib/search.h"
35 #include "lib/strescape.h"
37 #include "src/charsets.h"
41 /*** global variables ****************************************************************************/
43 /*** file scope macro definitions ****************************************************************/
45 /*** file scope type declarations ****************************************************************/
49 REPLACE_T_NO_TRANSFORM
= 0,
50 REPLACE_T_UPP_TRANSFORM_CHAR
= 1,
51 REPLACE_T_LOW_TRANSFORM_CHAR
= 2,
52 REPLACE_T_UPP_TRANSFORM
= 4,
53 REPLACE_T_LOW_TRANSFORM
= 8
54 } replace_transform_type_t
;
56 /*** file scope variables ************************************************************************/
58 /*** file scope functions ************************************************************************/
61 mc_search__regex_str_append_if_special (GString
* copy_to
, GString
* regex_str
, gsize
* offset
)
65 const char **spec_chr
;
66 const char *special_chars
[] = {
81 spec_chr
= special_chars
;
83 tmp_regex_str
= &(regex_str
->str
[*offset
]);
87 spec_chr_len
= strlen (*spec_chr
);
88 if (!strncmp (tmp_regex_str
, *spec_chr
, spec_chr_len
))
90 if (!strutils_is_char_escaped (regex_str
->str
, tmp_regex_str
))
92 if (!strncmp ("\\x", *spec_chr
, spec_chr_len
))
94 if (*(tmp_regex_str
+ spec_chr_len
) == '{')
96 while ((spec_chr_len
< regex_str
->len
- *offset
)
97 && *(tmp_regex_str
+ spec_chr_len
) != '}')
99 if (*(tmp_regex_str
+ spec_chr_len
) == '}')
105 g_string_append_len (copy_to
, tmp_regex_str
, spec_chr_len
);
106 *offset
+= spec_chr_len
;
116 /* --------------------------------------------------------------------------------------------- */
118 mc_search__cond_struct_new_regex_hex_add (const char *charset
, GString
* str_to
,
119 const char *one_char
, gsize str_len
)
125 upp
= mc_search__toupper_case_str (charset
, one_char
, str_len
);
126 low
= mc_search__tolower_case_str (charset
, one_char
, str_len
);
128 for (loop
= 0; loop
< upp
->len
; loop
++)
133 if (upp
->str
[loop
] == low
->str
[loop
])
134 tmp_str
= g_strdup_printf ("\\x%02X", (unsigned char) upp
->str
[loop
]);
137 g_strdup_printf ("[\\x%02X\\x%02X]", (unsigned char) upp
->str
[loop
],
138 (unsigned char) low
->str
[loop
]);
142 tmp_str
= g_strdup_printf ("\\x%02X", (unsigned char) upp
->str
[loop
]);
144 g_string_append (str_to
, tmp_str
);
147 g_string_free (upp
, TRUE
);
148 g_string_free (low
, TRUE
);
151 /* --------------------------------------------------------------------------------------------- */
154 mc_search__cond_struct_new_regex_accum_append (const char *charset
, GString
* str_to
,
157 GString
*recoded_part
;
160 recoded_part
= g_string_sized_new (32);
162 while (loop
< str_from
->len
)
166 gboolean just_letters
;
169 mc_search__get_one_symbol (charset
, &(str_from
->str
[loop
]),
170 min (str_from
->len
- loop
, 6), &just_letters
);
171 one_char_len
= strlen (one_char
);
173 if (one_char_len
== 0)
177 loop
+= one_char_len
;
180 mc_search__cond_struct_new_regex_hex_add (charset
, recoded_part
, one_char
,
183 g_string_append_len (recoded_part
, one_char
, one_char_len
);
189 g_string_append (str_to
, recoded_part
->str
);
190 g_string_free (recoded_part
, TRUE
);
191 g_string_set_size (str_from
, 0);
194 /* --------------------------------------------------------------------------------------------- */
197 mc_search__cond_struct_new_regex_ci_str (const char *charset
, const char *str
, gsize str_len
)
199 GString
*accumulator
, *spec_char
, *ret_str
;
202 tmp
= g_string_new_len (str
, str_len
);
205 ret_str
= g_string_new ("");
206 accumulator
= g_string_new ("");
207 spec_char
= g_string_new ("");
210 while (loop
<= str_len
)
212 if (mc_search__regex_str_append_if_special (spec_char
, tmp
, &loop
))
214 mc_search__cond_struct_new_regex_accum_append (charset
, ret_str
, accumulator
);
215 g_string_append_len (ret_str
, spec_char
->str
, spec_char
->len
);
216 g_string_set_size (spec_char
, 0);
220 if (tmp
->str
[loop
] == '[' && !strutils_is_char_escaped (tmp
->str
, &(tmp
->str
[loop
])))
222 mc_search__cond_struct_new_regex_accum_append (charset
, ret_str
, accumulator
);
224 while (loop
< str_len
&& !(tmp
->str
[loop
] == ']'
225 && !strutils_is_char_escaped (tmp
->str
, &(tmp
->str
[loop
]))))
227 g_string_append_c (ret_str
, tmp
->str
[loop
]);
231 g_string_append_c (ret_str
, tmp
->str
[loop
]);
238 g_string_append_c (accumulator
, tmp
->str
[loop
]);
241 mc_search__cond_struct_new_regex_accum_append (charset
, ret_str
, accumulator
);
243 g_string_free (accumulator
, TRUE
);
244 g_string_free (spec_char
, TRUE
);
245 g_string_free (tmp
, TRUE
);
249 /* --------------------------------------------------------------------------------------------- */
251 static mc_search__found_cond_t
252 mc_search__regex_found_cond_one (mc_search_t
* lc_mc_search
, mc_search_regex_t
* regex
,
253 GString
* search_str
)
255 #ifdef SEARCH_TYPE_GLIB
256 GError
*error
= NULL
;
258 if (!g_regex_match_full
259 (regex
, search_str
->str
, -1, 0, G_REGEX_MATCH_NEWLINE_ANY
, &lc_mc_search
->regex_match_info
,
262 g_match_info_free (lc_mc_search
->regex_match_info
);
263 lc_mc_search
->regex_match_info
= NULL
;
266 lc_mc_search
->error
= MC_SEARCH_E_REGEX
;
267 lc_mc_search
->error_str
=
268 str_conv_gerror_message (error
, _(" Regular expression error "));
269 g_error_free (error
);
270 return COND__FOUND_ERROR
;
272 return COND__NOT_FOUND
;
274 lc_mc_search
->num_rezults
= g_match_info_get_match_count (lc_mc_search
->regex_match_info
);
275 #else /* SEARCH_TYPE_GLIB */
276 lc_mc_search
->num_rezults
= pcre_exec (regex
, lc_mc_search
->regex_match_info
,
277 search_str
->str
, search_str
->len
- 1, 0, 0,
278 lc_mc_search
->iovector
, MC_SEARCH__NUM_REPLACE_ARGS
);
279 if (lc_mc_search
->num_rezults
< 0)
281 return COND__NOT_FOUND
;
283 #endif /* SEARCH_TYPE_GLIB */
284 return COND__FOUND_OK
;
288 /* --------------------------------------------------------------------------------------------- */
290 static mc_search__found_cond_t
291 mc_search__regex_found_cond (mc_search_t
* lc_mc_search
, GString
* search_str
)
294 mc_search_cond_t
*mc_search_cond
;
295 mc_search__found_cond_t ret
;
297 for (loop1
= 0; loop1
< lc_mc_search
->conditions
->len
; loop1
++)
299 mc_search_cond
= (mc_search_cond_t
*) g_ptr_array_index (lc_mc_search
->conditions
, loop1
);
301 if (!mc_search_cond
->regex_handle
)
305 mc_search__regex_found_cond_one (lc_mc_search
, mc_search_cond
->regex_handle
,
308 if (ret
!= COND__NOT_FOUND
)
311 return COND__NOT_ALL_FOUND
;
314 /* --------------------------------------------------------------------------------------------- */
317 mc_search_regex__get_max_num_of_replace_tokens (const gchar
* str
, gsize len
)
321 for (loop
= 0; loop
< len
- 1; loop
++)
323 if (str
[loop
] == '\\' && (str
[loop
+ 1] & (char) 0xf0) == 0x30 /* 0-9 */ )
325 if (strutils_is_char_escaped (str
, &str
[loop
]))
327 if (max_token
< str
[loop
+ 1] - '0')
328 max_token
= str
[loop
+ 1] - '0';
331 if (str
[loop
] == '$' && str
[loop
+ 1] == '{')
336 if (strutils_is_char_escaped (str
, &str
[loop
]))
340 loop
+ tmp_len
+ 2 < len
&& (str
[loop
+ 2 + tmp_len
] & (char) 0xf0) == 0x30;
342 if (str
[loop
+ 2 + tmp_len
] == '}')
344 tmp_str
= g_strndup (&str
[loop
+ 2], tmp_len
);
345 tmp_token
= atoi (tmp_str
);
346 if (max_token
< tmp_token
)
347 max_token
= tmp_token
;
355 /* --------------------------------------------------------------------------------------------- */
358 mc_search_regex__get_token_by_num (const mc_search_t
* lc_mc_search
, gsize lc_index
)
360 int fnd_start
= 0, fnd_end
= 0;
362 #ifdef SEARCH_TYPE_GLIB
363 g_match_info_fetch_pos (lc_mc_search
->regex_match_info
, lc_index
, &fnd_start
, &fnd_end
);
364 #else /* SEARCH_TYPE_GLIB */
365 fnd_start
= lc_mc_search
->iovector
[lc_index
* 2 + 0];
366 fnd_end
= lc_mc_search
->iovector
[lc_index
* 2 + 1];
367 #endif /* SEARCH_TYPE_GLIB */
369 if (fnd_end
- fnd_start
== 0)
372 return g_strndup (lc_mc_search
->regex_buffer
->str
+ fnd_start
, fnd_end
- fnd_start
);
376 /* --------------------------------------------------------------------------------------------- */
378 mc_search_regex__process_replace_str (const GString
* replace_str
, const gsize current_pos
,
379 gsize
* skip_len
, replace_transform_type_t
* replace_flags
)
383 const char *curr_str
= &(replace_str
->str
[current_pos
]);
385 if (current_pos
> replace_str
->len
)
390 if (*curr_str
== '$' && *(curr_str
+ 1) == '{' && (*(curr_str
+ 2) & (char) 0xf0) == 0x30)
392 if (strutils_is_char_escaped (replace_str
->str
, curr_str
))
399 current_pos
+ *skip_len
+ 2 < replace_str
->len
400 && (*(curr_str
+ 2 + *skip_len
) & (char) 0xf0) == 0x30; (*skip_len
)++);
402 if (*(curr_str
+ 2 + *skip_len
) != '}')
405 tmp_str
= g_strndup (curr_str
+ 2, *skip_len
);
409 ret
= atoi (tmp_str
);
412 *skip_len
+= 3; /* ${} */
416 if (*curr_str
== '\\')
418 if (strutils_is_char_escaped (replace_str
->str
, curr_str
))
424 if ((*(curr_str
+ 1) & (char) 0xf0) == 0x30)
426 ret
= *(curr_str
+ 1) - '0';
427 *skip_len
= 2; /* \\ and one digit */
432 switch (*(curr_str
+ 1))
435 *replace_flags
|= REPLACE_T_UPP_TRANSFORM
;
436 *replace_flags
&= ~REPLACE_T_LOW_TRANSFORM
;
439 *replace_flags
|= REPLACE_T_UPP_TRANSFORM_CHAR
;
442 *replace_flags
|= REPLACE_T_LOW_TRANSFORM
;
443 *replace_flags
&= ~REPLACE_T_UPP_TRANSFORM
;
446 *replace_flags
|= REPLACE_T_LOW_TRANSFORM_CHAR
;
449 *replace_flags
= REPLACE_T_NO_TRANSFORM
;
459 mc_search_regex__process_append_str (GString
* dest_str
, const char *from
, gsize len
,
460 replace_transform_type_t
* replace_flags
)
467 if (len
== (gsize
) - 1)
470 if (*replace_flags
== REPLACE_T_NO_TRANSFORM
)
472 g_string_append_len (dest_str
, from
, len
);
477 tmp_str
= mc_search__get_one_symbol (NULL
, from
+ loop
, len
- loop
, NULL
);
478 char_len
= strlen (tmp_str
);
479 if (*replace_flags
& REPLACE_T_UPP_TRANSFORM_CHAR
)
481 *replace_flags
&= !REPLACE_T_UPP_TRANSFORM_CHAR
;
482 tmp_string
= mc_search__toupper_case_str (NULL
, tmp_str
, char_len
);
483 g_string_append (dest_str
, tmp_string
->str
);
484 g_string_free (tmp_string
, TRUE
);
487 else if (*replace_flags
& REPLACE_T_LOW_TRANSFORM_CHAR
)
489 *replace_flags
&= !REPLACE_T_LOW_TRANSFORM_CHAR
;
490 tmp_string
= mc_search__toupper_case_str (NULL
, tmp_str
, char_len
);
491 g_string_append (dest_str
, tmp_string
->str
);
492 g_string_free (tmp_string
, TRUE
);
495 else if (*replace_flags
& REPLACE_T_UPP_TRANSFORM
)
497 tmp_string
= mc_search__toupper_case_str (NULL
, tmp_str
, char_len
);
498 g_string_append (dest_str
, tmp_string
->str
);
499 g_string_free (tmp_string
, TRUE
);
502 else if (*replace_flags
& REPLACE_T_LOW_TRANSFORM
)
504 tmp_string
= mc_search__tolower_case_str (NULL
, tmp_str
, char_len
);
505 g_string_append (dest_str
, tmp_string
->str
);
506 g_string_free (tmp_string
, TRUE
);
511 g_string_append (dest_str
, tmp_str
);
519 /*** public functions ****************************************************************************/
522 mc_search__cond_struct_new_init_regex (const char *charset
, mc_search_t
* lc_mc_search
,
523 mc_search_cond_t
* mc_search_cond
)
526 #ifdef SEARCH_TYPE_GLIB
527 GError
*error
= NULL
;
528 #else /* SEARCH_TYPE_GLIB */
531 #endif /* SEARCH_TYPE_GLIB */
533 if (!lc_mc_search
->is_case_sentitive
)
535 tmp
= g_string_new_len (mc_search_cond
->str
->str
, mc_search_cond
->str
->len
);
536 g_string_free (mc_search_cond
->str
, TRUE
);
537 mc_search_cond
->str
= mc_search__cond_struct_new_regex_ci_str (charset
, tmp
->str
, tmp
->len
);
538 g_string_free (tmp
, TRUE
);
540 #ifdef SEARCH_TYPE_GLIB
541 mc_search_cond
->regex_handle
=
542 g_regex_new (mc_search_cond
->str
->str
, G_REGEX_OPTIMIZE
| G_REGEX_RAW
| G_REGEX_DOTALL
, 0,
547 lc_mc_search
->error
= MC_SEARCH_E_REGEX_COMPILE
;
548 lc_mc_search
->error_str
= str_conv_gerror_message (error
, _(" Regular expression error "));
549 g_error_free (error
);
552 #else /* SEARCH_TYPE_GLIB */
553 mc_search_cond
->regex_handle
=
554 pcre_compile (mc_search_cond
->str
->str
, PCRE_EXTRA
, &error
, &erroffset
, NULL
);
555 if (mc_search_cond
->regex_handle
== NULL
)
557 lc_mc_search
->error
= MC_SEARCH_E_REGEX_COMPILE
;
558 lc_mc_search
->error_str
= g_strdup (error
);
561 lc_mc_search
->regex_match_info
= pcre_study (mc_search_cond
->regex_handle
, 0, &error
);
562 if (lc_mc_search
->regex_match_info
== NULL
)
566 lc_mc_search
->error
= MC_SEARCH_E_REGEX_COMPILE
;
567 lc_mc_search
->error_str
= g_strdup (error
);
568 g_free (mc_search_cond
->regex_handle
);
569 mc_search_cond
->regex_handle
= NULL
;
573 #endif /* SEARCH_TYPE_GLIB */
576 /* --------------------------------------------------------------------------------------------- */
579 mc_search__run_regex (mc_search_t
* lc_mc_search
, const void *user_data
,
580 gsize start_search
, gsize end_search
, gsize
* found_len
)
582 gsize current_pos
, virtual_pos
;
587 if (lc_mc_search
->regex_buffer
!= NULL
)
588 g_string_free (lc_mc_search
->regex_buffer
, TRUE
);
590 lc_mc_search
->regex_buffer
= g_string_new ("");
592 virtual_pos
= current_pos
= start_search
;
593 while (virtual_pos
<= end_search
)
595 g_string_set_size (lc_mc_search
->regex_buffer
, 0);
596 lc_mc_search
->start_buffer
= current_pos
;
600 current_chr
= mc_search__get_char (lc_mc_search
, user_data
, current_pos
);
601 if (current_chr
== MC_SEARCH_CB_ABORT
)
606 if (current_chr
== MC_SEARCH_CB_SKIP
)
611 g_string_append_c (lc_mc_search
->regex_buffer
, (char) current_chr
);
614 if (current_chr
== 0 || (char) current_chr
== '\n')
617 if (virtual_pos
> end_search
)
621 switch (mc_search__regex_found_cond (lc_mc_search
, lc_mc_search
->regex_buffer
))
624 #ifdef SEARCH_TYPE_GLIB
625 g_match_info_fetch_pos (lc_mc_search
->regex_match_info
, 0, &start_pos
, &end_pos
);
626 #else /* SEARCH_TYPE_GLIB */
627 start_pos
= lc_mc_search
->iovector
[0];
628 end_pos
= lc_mc_search
->iovector
[1];
629 #endif /* SEARCH_TYPE_GLIB */
631 *found_len
= end_pos
- start_pos
;
632 lc_mc_search
->normal_offset
= lc_mc_search
->start_buffer
+ start_pos
;
635 case COND__NOT_ALL_FOUND
:
638 g_string_free (lc_mc_search
->regex_buffer
, TRUE
);
639 lc_mc_search
->regex_buffer
= NULL
;
643 if ((lc_mc_search
->update_fn
!= NULL
) &&
644 ((lc_mc_search
->update_fn
) (user_data
, current_pos
) == MC_SEARCH_CB_ABORT
))
645 current_chr
= MC_SEARCH_CB_ABORT
;
647 if (current_chr
== MC_SEARCH_CB_ABORT
)
650 g_string_free (lc_mc_search
->regex_buffer
, TRUE
);
651 lc_mc_search
->regex_buffer
= NULL
;
652 lc_mc_search
->error
= MC_SEARCH_E_NOTFOUND
;
654 if (current_chr
!= MC_SEARCH_CB_ABORT
)
655 lc_mc_search
->error_str
= g_strdup (_(STR_E_NOTFOUND
));
657 lc_mc_search
->error_str
= NULL
;
662 /* --------------------------------------------------------------------------------------------- */
664 mc_search_regex_prepare_replace_str (mc_search_t
* lc_mc_search
, GString
* replace_str
)
669 int num_replace_tokens
, lc_index
;
673 replace_transform_type_t replace_flags
= REPLACE_T_NO_TRANSFORM
;
676 mc_search_regex__get_max_num_of_replace_tokens (replace_str
->str
, replace_str
->len
);
678 if (lc_mc_search
->num_rezults
< 0)
679 return g_string_new_len (replace_str
->str
, replace_str
->len
);
681 if (num_replace_tokens
> lc_mc_search
->num_rezults
- 1
682 || num_replace_tokens
> MC_SEARCH__NUM_REPLACE_ARGS
)
684 lc_mc_search
->error
= MC_SEARCH_E_REGEX_REPLACE
;
685 lc_mc_search
->error_str
= g_strdup (STR_E_RPL_NOT_EQ_TO_FOUND
);
689 ret
= g_string_new ("");
690 prev_str
= replace_str
->str
;
691 for (loop
= 0; loop
< replace_str
->len
- 1; loop
++)
693 lc_index
= mc_search_regex__process_replace_str (replace_str
, loop
, &len
, &replace_flags
);
699 mc_search_regex__process_append_str (ret
, prev_str
,
700 replace_str
->str
- prev_str
+ loop
,
702 mc_search_regex__process_append_str (ret
, replace_str
->str
+ loop
+ 1, len
- 1,
704 prev_str
= replace_str
->str
+ loop
+ len
;
713 mc_search_regex__process_append_str (ret
, prev_str
,
714 replace_str
->str
- prev_str
+ loop
,
716 prev_str
= replace_str
->str
+ loop
+ len
;
721 if (lc_index
> lc_mc_search
->num_rezults
)
723 g_string_free (ret
, TRUE
);
724 lc_mc_search
->error
= MC_SEARCH_E_REGEX_REPLACE
;
725 lc_mc_search
->error_str
= g_strdup_printf (STR_E_RPL_INVALID_TOKEN
, lc_index
);
729 tmp_str
= mc_search_regex__get_token_by_num (lc_mc_search
, lc_index
);
734 mc_search_regex__process_append_str (ret
, prev_str
, replace_str
->str
- prev_str
+ loop
,
736 prev_str
= replace_str
->str
+ loop
+ len
;
738 mc_search_regex__process_append_str (ret
, tmp_str
, -1, &replace_flags
);
742 mc_search_regex__process_append_str (ret
, prev_str
,
743 replace_str
->str
- prev_str
+ replace_str
->len
,