Updated Russian translation.
[midnight-commander.git] / lib / search / regex.c
blob2d90fa68ae4ee69f1509d80907b5fdc29d699016
1 /*
2 Search text engine.
3 Regex search
5 Copyright (C) 2009 The Free Software Foundation, Inc.
7 Written by:
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,
25 MA 02110-1301, USA.
28 #include <config.h>
30 #include <stdlib.h>
32 #include "lib/global.h"
33 #include "lib/strutil.h"
34 #include "lib/search.h"
35 #include "lib/strescape.h"
37 #include "lib/charsets.h"
39 #include "internal.h"
41 /*** global variables ****************************************************************************/
43 /*** file scope macro definitions ****************************************************************/
45 /*** file scope type declarations ****************************************************************/
47 typedef enum
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 ************************************************************************/
60 static gboolean
61 mc_search__regex_str_append_if_special (GString * copy_to, const GString * regex_str,
62 gsize * offset)
64 char *tmp_regex_str;
65 gsize spec_chr_len;
66 const char **spec_chr;
67 const char *special_chars[] = {
68 "\\s", "\\S",
69 "\\d", "\\D",
70 "\\b", "\\B",
71 "\\w", "\\W",
72 "\\t", "\\n",
73 "\\r", "\\f",
74 "\\a", "\\e",
75 "\\x", "\\X",
76 "\\c", "\\C",
77 "\\l", "\\L",
78 "\\u", "\\U",
79 "\\E", "\\Q",
80 NULL
82 spec_chr = special_chars;
84 tmp_regex_str = &(regex_str->str[*offset]);
86 while (*spec_chr)
88 spec_chr_len = strlen (*spec_chr);
89 if (!strncmp (tmp_regex_str, *spec_chr, spec_chr_len))
91 if (!strutils_is_char_escaped (regex_str->str, tmp_regex_str))
93 if (!strncmp ("\\x", *spec_chr, spec_chr_len))
95 if (*(tmp_regex_str + spec_chr_len) == '{')
97 while ((spec_chr_len < regex_str->len - *offset)
98 && *(tmp_regex_str + spec_chr_len) != '}')
99 spec_chr_len++;
100 if (*(tmp_regex_str + spec_chr_len) == '}')
101 spec_chr_len++;
103 else
104 spec_chr_len += 2;
106 g_string_append_len (copy_to, tmp_regex_str, spec_chr_len);
107 *offset += spec_chr_len;
108 return TRUE;
111 spec_chr++;
113 return FALSE;
117 /* --------------------------------------------------------------------------------------------- */
118 static void
119 mc_search__cond_struct_new_regex_hex_add (const char *charset, GString * str_to,
120 const char *one_char, gsize str_len)
122 GString *upp, *low;
123 gchar *tmp_str;
124 gsize loop;
126 upp = mc_search__toupper_case_str (charset, one_char, str_len);
127 low = mc_search__tolower_case_str (charset, one_char, str_len);
129 for (loop = 0; loop < upp->len; loop++)
132 if (loop < low->len)
134 if (upp->str[loop] == low->str[loop])
135 tmp_str = g_strdup_printf ("\\x%02X", (unsigned char) upp->str[loop]);
136 else
137 tmp_str =
138 g_strdup_printf ("[\\x%02X\\x%02X]", (unsigned char) upp->str[loop],
139 (unsigned char) low->str[loop]);
141 else
143 tmp_str = g_strdup_printf ("\\x%02X", (unsigned char) upp->str[loop]);
145 g_string_append (str_to, tmp_str);
146 g_free (tmp_str);
148 g_string_free (upp, TRUE);
149 g_string_free (low, TRUE);
152 /* --------------------------------------------------------------------------------------------- */
154 static void
155 mc_search__cond_struct_new_regex_accum_append (const char *charset, GString * str_to,
156 GString * str_from)
158 GString *recoded_part;
159 gsize loop = 0;
161 recoded_part = g_string_sized_new (32);
163 while (loop < str_from->len)
165 gchar *one_char;
166 gsize one_char_len;
167 gboolean just_letters;
169 one_char =
170 mc_search__get_one_symbol (charset, &(str_from->str[loop]),
171 min (str_from->len - loop, 6), &just_letters);
172 one_char_len = strlen (one_char);
174 if (one_char_len == 0)
175 loop++;
176 else
178 loop += one_char_len;
180 if (just_letters)
181 mc_search__cond_struct_new_regex_hex_add (charset, recoded_part, one_char,
182 one_char_len);
183 else
184 g_string_append_len (recoded_part, one_char, one_char_len);
187 g_free (one_char);
190 g_string_append (str_to, recoded_part->str);
191 g_string_free (recoded_part, TRUE);
192 g_string_set_size (str_from, 0);
195 /* --------------------------------------------------------------------------------------------- */
197 static GString *
198 mc_search__cond_struct_new_regex_ci_str (const char *charset, const GString * astr)
200 GString *accumulator, *spec_char, *ret_str;
201 gsize loop;
203 ret_str = g_string_sized_new (64);
204 accumulator = g_string_sized_new (64);
205 spec_char = g_string_sized_new (64);
206 loop = 0;
208 while (loop <= astr->len)
210 if (mc_search__regex_str_append_if_special (spec_char, astr, &loop))
212 mc_search__cond_struct_new_regex_accum_append (charset, ret_str, accumulator);
213 g_string_append_len (ret_str, spec_char->str, spec_char->len);
214 g_string_set_size (spec_char, 0);
215 continue;
218 if (astr->str[loop] == '[' && !strutils_is_char_escaped (astr->str, &(astr->str[loop])))
220 mc_search__cond_struct_new_regex_accum_append (charset, ret_str, accumulator);
222 while (loop < astr->len && !(astr->str[loop] == ']'
223 && !strutils_is_char_escaped (astr->str,
224 &(astr->str[loop]))))
226 g_string_append_c (ret_str, astr->str[loop]);
227 loop++;
230 g_string_append_c (ret_str, astr->str[loop]);
231 loop++;
232 continue;
235 TODO: handle [ and ]
237 g_string_append_c (accumulator, astr->str[loop]);
238 loop++;
240 mc_search__cond_struct_new_regex_accum_append (charset, ret_str, accumulator);
242 g_string_free (accumulator, TRUE);
243 g_string_free (spec_char, TRUE);
245 return ret_str;
248 /* --------------------------------------------------------------------------------------------- */
250 static mc_search__found_cond_t
251 mc_search__regex_found_cond_one (mc_search_t * lc_mc_search, mc_search_regex_t * regex,
252 GString * search_str)
254 #ifdef SEARCH_TYPE_GLIB
255 GError *error = NULL;
257 if (!g_regex_match_full
258 (regex, search_str->str, -1, 0, G_REGEX_MATCH_NEWLINE_ANY, &lc_mc_search->regex_match_info,
259 &error))
261 g_match_info_free (lc_mc_search->regex_match_info);
262 lc_mc_search->regex_match_info = NULL;
263 if (error)
265 lc_mc_search->error = MC_SEARCH_E_REGEX;
266 lc_mc_search->error_str =
267 str_conv_gerror_message (error, _("Regular expression error"));
268 g_error_free (error);
269 return COND__FOUND_ERROR;
271 return COND__NOT_FOUND;
273 lc_mc_search->num_results = g_match_info_get_match_count (lc_mc_search->regex_match_info);
274 #else /* SEARCH_TYPE_GLIB */
275 lc_mc_search->num_results = pcre_exec (regex, lc_mc_search->regex_match_info,
276 search_str->str, search_str->len - 1, 0, 0,
277 lc_mc_search->iovector, MC_SEARCH__NUM_REPLACE_ARGS);
278 if (lc_mc_search->num_results < 0)
280 return COND__NOT_FOUND;
282 #endif /* SEARCH_TYPE_GLIB */
283 return COND__FOUND_OK;
287 /* --------------------------------------------------------------------------------------------- */
289 static mc_search__found_cond_t
290 mc_search__regex_found_cond (mc_search_t * lc_mc_search, GString * search_str)
292 gsize loop1;
293 mc_search_cond_t *mc_search_cond;
294 mc_search__found_cond_t ret;
296 for (loop1 = 0; loop1 < lc_mc_search->conditions->len; loop1++)
298 mc_search_cond = (mc_search_cond_t *) g_ptr_array_index (lc_mc_search->conditions, loop1);
300 if (!mc_search_cond->regex_handle)
301 continue;
303 ret =
304 mc_search__regex_found_cond_one (lc_mc_search, mc_search_cond->regex_handle,
305 search_str);
307 if (ret != COND__NOT_FOUND)
308 return ret;
310 return COND__NOT_ALL_FOUND;
313 /* --------------------------------------------------------------------------------------------- */
315 static int
316 mc_search_regex__get_max_num_of_replace_tokens (const gchar * str, gsize len)
318 int max_token = 0;
319 gsize loop;
320 for (loop = 0; loop < len - 1; loop++)
322 if (str[loop] == '\\' && g_ascii_isdigit (str[loop + 1]))
324 if (strutils_is_char_escaped (str, &str[loop]))
325 continue;
326 if (max_token < str[loop + 1] - '0')
327 max_token = str[loop + 1] - '0';
328 continue;
330 if (str[loop] == '$' && str[loop + 1] == '{')
332 gsize tmp_len;
333 char *tmp_str;
334 int tmp_token;
335 if (strutils_is_char_escaped (str, &str[loop]))
336 continue;
338 for (tmp_len = 0;
339 loop + tmp_len + 2 < len && (str[loop + 2 + tmp_len] & (char) 0xf0) == 0x30;
340 tmp_len++);
341 if (str[loop + 2 + tmp_len] == '}')
343 tmp_str = g_strndup (&str[loop + 2], tmp_len);
344 tmp_token = atoi (tmp_str);
345 if (max_token < tmp_token)
346 max_token = tmp_token;
347 g_free (tmp_str);
351 return max_token;
354 /* --------------------------------------------------------------------------------------------- */
356 static char *
357 mc_search_regex__get_token_by_num (const mc_search_t * lc_mc_search, gsize lc_index)
359 int fnd_start = 0, fnd_end = 0;
361 #ifdef SEARCH_TYPE_GLIB
362 g_match_info_fetch_pos (lc_mc_search->regex_match_info, lc_index, &fnd_start, &fnd_end);
363 #else /* SEARCH_TYPE_GLIB */
364 fnd_start = lc_mc_search->iovector[lc_index * 2 + 0];
365 fnd_end = lc_mc_search->iovector[lc_index * 2 + 1];
366 #endif /* SEARCH_TYPE_GLIB */
368 if (fnd_end - fnd_start == 0)
369 return NULL;
371 return g_strndup (lc_mc_search->regex_buffer->str + fnd_start, fnd_end - fnd_start);
375 /* --------------------------------------------------------------------------------------------- */
377 static int
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)
381 int ret = -1;
382 char *tmp_str;
383 const char *curr_str = &(replace_str->str[current_pos]);
385 if (current_pos > replace_str->len)
386 return -1;
388 *skip_len = 0;
390 if (*curr_str == '$' && *(curr_str + 1) == '{' && (*(curr_str + 2) & (char) 0xf0) == 0x30)
392 if (strutils_is_char_escaped (replace_str->str, curr_str))
394 *skip_len = 1;
395 return -1;
398 for (*skip_len = 0;
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) != '}')
403 return -1;
405 tmp_str = g_strndup (curr_str + 2, *skip_len);
406 if (tmp_str == NULL)
407 return -1;
409 ret = atoi (tmp_str);
410 g_free (tmp_str);
412 *skip_len += 3; /* ${} */
413 return ret;
416 if (*curr_str == '\\')
418 if (strutils_is_char_escaped (replace_str->str, curr_str))
420 *skip_len = 1;
421 return -1;
424 if (g_ascii_isdigit (*(curr_str + 1)))
426 ret = g_ascii_digit_value (*(curr_str + 1));
427 *skip_len = 2; /* \\ and one digit */
428 return ret;
430 ret = -2;
431 *skip_len += 2;
432 switch (*(curr_str + 1))
434 case 'U':
435 *replace_flags |= REPLACE_T_UPP_TRANSFORM;
436 *replace_flags &= ~REPLACE_T_LOW_TRANSFORM;
437 break;
438 case 'u':
439 *replace_flags |= REPLACE_T_UPP_TRANSFORM_CHAR;
440 break;
441 case 'L':
442 *replace_flags |= REPLACE_T_LOW_TRANSFORM;
443 *replace_flags &= ~REPLACE_T_UPP_TRANSFORM;
444 break;
445 case 'l':
446 *replace_flags |= REPLACE_T_LOW_TRANSFORM_CHAR;
447 break;
448 case 'E':
449 *replace_flags = REPLACE_T_NO_TRANSFORM;
450 break;
451 default:
452 ret = -1;
453 break;
456 return ret;
459 static void
460 mc_search_regex__process_append_str (GString * dest_str, const char *from, gsize len,
461 replace_transform_type_t * replace_flags)
463 gsize loop = 0;
464 gsize char_len;
465 char *tmp_str;
466 GString *tmp_string;
468 if (len == (gsize) - 1)
469 len = strlen (from);
471 if (*replace_flags == REPLACE_T_NO_TRANSFORM)
473 g_string_append_len (dest_str, from, len);
474 return;
476 while (loop < len)
478 tmp_str = mc_search__get_one_symbol (NULL, from + loop, len - loop, NULL);
479 char_len = strlen (tmp_str);
480 if (*replace_flags & REPLACE_T_UPP_TRANSFORM_CHAR)
482 *replace_flags &= ~REPLACE_T_UPP_TRANSFORM_CHAR;
483 tmp_string = mc_search__toupper_case_str (NULL, tmp_str, char_len);
484 g_string_append (dest_str, tmp_string->str);
485 g_string_free (tmp_string, TRUE);
488 else if (*replace_flags & REPLACE_T_LOW_TRANSFORM_CHAR)
490 *replace_flags &= ~REPLACE_T_LOW_TRANSFORM_CHAR;
491 tmp_string = mc_search__toupper_case_str (NULL, tmp_str, char_len);
492 g_string_append (dest_str, tmp_string->str);
493 g_string_free (tmp_string, TRUE);
496 else if (*replace_flags & REPLACE_T_UPP_TRANSFORM)
498 tmp_string = mc_search__toupper_case_str (NULL, tmp_str, char_len);
499 g_string_append (dest_str, tmp_string->str);
500 g_string_free (tmp_string, TRUE);
503 else if (*replace_flags & REPLACE_T_LOW_TRANSFORM)
505 tmp_string = mc_search__tolower_case_str (NULL, tmp_str, char_len);
506 g_string_append (dest_str, tmp_string->str);
507 g_string_free (tmp_string, TRUE);
510 else
512 g_string_append (dest_str, tmp_str);
514 g_free (tmp_str);
515 loop += char_len;
520 /*** public functions ****************************************************************************/
522 void
523 mc_search__cond_struct_new_init_regex (const char *charset, mc_search_t * lc_mc_search,
524 mc_search_cond_t * mc_search_cond)
526 #ifdef SEARCH_TYPE_GLIB
527 GError *error = NULL;
528 #else /* SEARCH_TYPE_GLIB */
529 const char *error;
530 int erroffset;
531 #endif /* SEARCH_TYPE_GLIB */
533 if (!lc_mc_search->is_case_sensitive)
535 GString *tmp;
537 tmp = mc_search_cond->str;
538 mc_search_cond->str = mc_search__cond_struct_new_regex_ci_str (charset, tmp);
539 g_string_free (tmp, TRUE);
541 #ifdef SEARCH_TYPE_GLIB
542 mc_search_cond->regex_handle =
543 g_regex_new (mc_search_cond->str->str, G_REGEX_OPTIMIZE | G_REGEX_RAW | G_REGEX_DOTALL, 0,
544 &error);
546 if (error != NULL)
548 lc_mc_search->error = MC_SEARCH_E_REGEX_COMPILE;
549 lc_mc_search->error_str = str_conv_gerror_message (error, _("Regular expression error"));
550 g_error_free (error);
551 return;
553 #else /* SEARCH_TYPE_GLIB */
554 mc_search_cond->regex_handle =
555 pcre_compile (mc_search_cond->str->str, PCRE_EXTRA, &error, &erroffset, NULL);
556 if (mc_search_cond->regex_handle == NULL)
558 lc_mc_search->error = MC_SEARCH_E_REGEX_COMPILE;
559 lc_mc_search->error_str = g_strdup (error);
560 return;
562 lc_mc_search->regex_match_info = pcre_study (mc_search_cond->regex_handle, 0, &error);
563 if (lc_mc_search->regex_match_info == NULL)
565 if (error)
567 lc_mc_search->error = MC_SEARCH_E_REGEX_COMPILE;
568 lc_mc_search->error_str = g_strdup (error);
569 g_free (mc_search_cond->regex_handle);
570 mc_search_cond->regex_handle = NULL;
571 return;
574 #endif /* SEARCH_TYPE_GLIB */
577 /* --------------------------------------------------------------------------------------------- */
579 gboolean
580 mc_search__run_regex (mc_search_t * lc_mc_search, const void *user_data,
581 gsize start_search, gsize end_search, gsize * found_len)
583 gsize current_pos, virtual_pos;
584 int current_chr = 0;
585 gint start_pos;
586 gint end_pos;
588 if (lc_mc_search->regex_buffer != NULL)
589 g_string_free (lc_mc_search->regex_buffer, TRUE);
591 lc_mc_search->regex_buffer = g_string_sized_new (64);
593 virtual_pos = current_pos = start_search;
594 while (virtual_pos <= end_search)
596 g_string_set_size (lc_mc_search->regex_buffer, 0);
597 lc_mc_search->start_buffer = current_pos;
599 while (1)
601 current_chr = mc_search__get_char (lc_mc_search, user_data, current_pos);
602 if (current_chr == MC_SEARCH_CB_ABORT)
603 break;
605 if (current_chr == MC_SEARCH_CB_INVALID)
606 continue;
608 current_pos++;
610 if (current_chr == MC_SEARCH_CB_SKIP)
611 continue;
613 virtual_pos++;
615 g_string_append_c (lc_mc_search->regex_buffer, (char) current_chr);
618 if (current_chr == 0 || (char) current_chr == '\n')
619 break;
621 if (virtual_pos > end_search)
622 break;
625 switch (mc_search__regex_found_cond (lc_mc_search, lc_mc_search->regex_buffer))
627 case COND__FOUND_OK:
628 #ifdef SEARCH_TYPE_GLIB
629 if (lc_mc_search->whole_words)
631 g_match_info_fetch_pos (lc_mc_search->regex_match_info, 2, &start_pos, &end_pos);
633 else
635 g_match_info_fetch_pos (lc_mc_search->regex_match_info, 0, &start_pos, &end_pos);
637 #else /* SEARCH_TYPE_GLIB */
638 if (lc_mc_search->whole_words)
640 start_pos = lc_mc_search->iovector[4];
641 end_pos = lc_mc_search->iovector[5];
643 else
645 start_pos = lc_mc_search->iovector[0];
646 end_pos = lc_mc_search->iovector[1];
648 #endif /* SEARCH_TYPE_GLIB */
649 if (found_len)
650 *found_len = end_pos - start_pos;
651 lc_mc_search->normal_offset = lc_mc_search->start_buffer + start_pos;
652 return TRUE;
653 case COND__NOT_ALL_FOUND:
654 break;
655 default:
656 g_string_free (lc_mc_search->regex_buffer, TRUE);
657 lc_mc_search->regex_buffer = NULL;
658 return FALSE;
660 if ((lc_mc_search->update_fn != NULL) &&
661 ((lc_mc_search->update_fn) (user_data, current_pos) == MC_SEARCH_CB_ABORT))
662 current_chr = MC_SEARCH_CB_ABORT;
664 if (current_chr == MC_SEARCH_CB_ABORT)
665 break;
667 g_string_free (lc_mc_search->regex_buffer, TRUE);
668 lc_mc_search->regex_buffer = NULL;
669 lc_mc_search->error = MC_SEARCH_E_NOTFOUND;
671 if (current_chr != MC_SEARCH_CB_ABORT)
672 lc_mc_search->error_str = g_strdup (_(STR_E_NOTFOUND));
673 else
674 lc_mc_search->error_str = NULL;
676 return FALSE;
679 /* --------------------------------------------------------------------------------------------- */
681 GString *
682 mc_search_regex_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str)
684 GString *ret;
685 gchar *tmp_str;
687 int num_replace_tokens, lc_index;
688 gsize loop;
689 gsize len = 0;
690 gchar *prev_str;
691 replace_transform_type_t replace_flags = REPLACE_T_NO_TRANSFORM;
693 num_replace_tokens =
694 mc_search_regex__get_max_num_of_replace_tokens (replace_str->str, replace_str->len);
696 if (lc_mc_search->num_results < 0)
697 return g_string_new_len (replace_str->str, replace_str->len);
699 if (num_replace_tokens > lc_mc_search->num_results - 1
700 || num_replace_tokens > MC_SEARCH__NUM_REPLACE_ARGS)
702 lc_mc_search->error = MC_SEARCH_E_REGEX_REPLACE;
703 lc_mc_search->error_str = g_strdup (_(STR_E_RPL_NOT_EQ_TO_FOUND));
704 return NULL;
707 ret = g_string_sized_new (64);
708 prev_str = replace_str->str;
710 for (loop = 0; loop < replace_str->len - 1; loop++)
712 lc_index = mc_search_regex__process_replace_str (replace_str, loop, &len, &replace_flags);
714 if (lc_index == -1)
716 if (len != 0)
718 mc_search_regex__process_append_str (ret, prev_str,
719 replace_str->str - prev_str + loop,
720 &replace_flags);
721 mc_search_regex__process_append_str (ret, replace_str->str + loop + 1, len - 1,
722 &replace_flags);
723 prev_str = replace_str->str + loop + len;
724 loop += len - 1;
726 continue;
729 if (lc_index == -2)
731 if (loop)
732 mc_search_regex__process_append_str (ret, prev_str,
733 replace_str->str - prev_str + loop,
734 &replace_flags);
735 prev_str = replace_str->str + loop + len;
736 loop += len - 1;
737 continue;
740 if (lc_index > lc_mc_search->num_results)
742 g_string_free (ret, TRUE);
743 lc_mc_search->error = MC_SEARCH_E_REGEX_REPLACE;
744 lc_mc_search->error_str = g_strdup_printf (_(STR_E_RPL_INVALID_TOKEN), lc_index);
745 return NULL;
748 tmp_str = mc_search_regex__get_token_by_num (lc_mc_search, lc_index);
749 if (tmp_str == NULL)
750 continue;
752 if (loop)
753 mc_search_regex__process_append_str (ret, prev_str, replace_str->str - prev_str + loop,
754 &replace_flags);
755 prev_str = replace_str->str + loop + len;
757 mc_search_regex__process_append_str (ret, tmp_str, -1, &replace_flags);
758 g_free (tmp_str);
759 loop += len - 1;
761 mc_search_regex__process_append_str (ret, prev_str,
762 replace_str->str - prev_str + replace_str->len,
763 &replace_flags);
765 return ret;