Changes for build after moving strutil into lib
[midnight-commander/osp/lisovros.git] / lib / search / regex.c
blob585ae2ba8b40a8fe3af8c6663da5e5633cc2a578
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"
35 #include "src/strescape.h"
36 #include "src/charsets.h"
38 #include "search.h"
39 #include "internal.h"
41 /*** global variables ****************************************************************************/
43 /*** file scope macro definitions ****************************************************************/
45 /*** file scope type declarations ****************************************************************/
47 typedef enum {
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 ************************************************************************/
59 static gboolean
60 mc_search__regex_str_append_if_special (GString * copy_to, GString * regex_str, gsize * offset)
62 char *tmp_regex_str;
63 gsize spec_chr_len;
64 const char **spec_chr;
65 const char *special_chars[] = {
66 "\\s", "\\S",
67 "\\d", "\\D",
68 "\\b", "\\B",
69 "\\w", "\\W",
70 "\\t", "\\n",
71 "\\r", "\\f",
72 "\\a", "\\e",
73 "\\x", "\\X",
74 "\\c", "\\C",
75 "\\l", "\\L",
76 "\\u", "\\U",
77 "\\E", "\\Q",
78 NULL
80 spec_chr = special_chars;
82 tmp_regex_str = &(regex_str->str[*offset]);
84 while (*spec_chr) {
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) != '}')
92 spec_chr_len++;
93 if (*(tmp_regex_str + spec_chr_len) == '}')
94 spec_chr_len++;
95 } else
96 spec_chr_len += 2;
98 g_string_append_len (copy_to, tmp_regex_str, spec_chr_len);
99 *offset += spec_chr_len;
100 return TRUE;
103 spec_chr++;
105 return FALSE;
109 /* --------------------------------------------------------------------------------------------- */
110 static void
111 mc_search__cond_struct_new_regex_hex_add (const char *charset, GString * str_to,
112 const char *one_char, gsize str_len)
114 GString *upp, *low;
115 gchar *tmp_str;
116 gsize loop;
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]);
126 else
127 tmp_str =
128 g_strdup_printf ("[\\x%02X\\x%02X]", (unsigned char) upp->str[loop],
129 (unsigned char) low->str[loop]);
130 } else {
131 tmp_str = g_strdup_printf ("\\x%02X", (unsigned char) upp->str[loop]);
133 g_string_append (str_to, tmp_str);
134 g_free (tmp_str);
136 g_string_free (upp, TRUE);
137 g_string_free (low, TRUE);
140 /* --------------------------------------------------------------------------------------------- */
142 static void
143 mc_search__cond_struct_new_regex_accum_append (const char *charset, GString * str_to,
144 GString * str_from)
146 GString *recoded_part;
147 gchar *one_char;
148 gsize loop;
149 gboolean just_letters;
151 loop = 0;
152 recoded_part = g_string_new ("");
154 while (loop < str_from->len) {
155 one_char =
156 mc_search__get_one_symbol (charset, &(str_from->str[loop]),
157 (str_from->len - loop > 6) ? 6 : str_from->len - loop,
158 &just_letters);
159 if (!strlen (one_char)) {
160 loop++;
161 continue;
163 if (just_letters) {
164 mc_search__cond_struct_new_regex_hex_add (charset, recoded_part, one_char,
165 strlen (one_char));
166 } else {
167 g_string_append (recoded_part, one_char);
169 loop += strlen (one_char);
170 if (!strlen (one_char))
171 loop++;
172 g_free (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 /* --------------------------------------------------------------------------------------------- */
182 static GString *
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;
186 gsize loop;
187 GString *tmp;
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 ("");
194 loop = 0;
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);
201 continue;
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]);
211 loop++;
214 g_string_append_c (ret_str, tmp->str[loop]);
215 loop++;
216 continue;
219 TODO: handle [ and ]
221 g_string_append_c (accumulator, tmp->str[loop]);
222 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);
229 return ret_str;
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,
243 &error)) {
244 g_match_info_free (lc_mc_search->regex_match_info);
245 lc_mc_search->regex_match_info = NULL;
246 if (error) {
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)
272 gsize loop1;
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)
280 continue;
282 ret = mc_search__regex_found_cond_one (lc_mc_search, mc_search_cond->regex_handle, search_str);
284 if (ret != COND__NOT_FOUND)
285 return ret;
287 return COND__NOT_ALL_FOUND;
290 /* --------------------------------------------------------------------------------------------- */
292 static int
293 mc_search_regex__get_max_num_of_replace_tokens (const gchar * str, gsize len)
295 int max_token = 0;
296 gsize loop;
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]))
300 continue;
301 if (max_token < str[loop + 1] - '0')
302 max_token = str[loop + 1] - '0';
303 continue;
305 if (str[loop] == '$' && str[loop + 1] == '{') {
306 gsize tmp_len;
307 char *tmp_str;
308 int tmp_token;
309 if (strutils_is_char_escaped (str, &str[loop]))
310 continue;
312 for (tmp_len = 0;
313 loop + tmp_len + 2 < len && (str[loop + 2 + tmp_len] & (char) 0xf0) == 0x30;
314 tmp_len++);
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;
320 g_free (tmp_str);
324 return max_token;
327 /* --------------------------------------------------------------------------------------------- */
329 static char *
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)
342 return NULL;
344 return g_strndup (lc_mc_search->regex_buffer->str + fnd_start, fnd_end - fnd_start);
348 /* --------------------------------------------------------------------------------------------- */
349 static int
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)
353 int ret = -1;
354 char *tmp_str;
355 const char *curr_str = &(replace_str->str[current_pos]);
357 if (current_pos > replace_str->len)
358 return -1;
360 *skip_len = 0;
362 if (*curr_str == '$' && *(curr_str + 1) == '{' && (*(curr_str + 2) & (char) 0xf0) == 0x30) {
363 if (strutils_is_char_escaped (replace_str->str, curr_str)) {
364 *skip_len = 1;
365 return -1;
368 for (*skip_len = 0;
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) != '}')
373 return -1;
375 tmp_str = g_strndup (curr_str + 2, *skip_len);
376 if (tmp_str == NULL)
377 return -1;
379 ret = atoi (tmp_str);
380 g_free (tmp_str);
382 *skip_len += 3; /* ${} */
383 return ret;
386 if (*curr_str == '\\') {
387 if (strutils_is_char_escaped (replace_str->str, curr_str)) {
388 *skip_len = 1;
389 return -1;
392 if ((*(curr_str + 1) & (char) 0xf0) == 0x30) {
393 ret = *(curr_str + 1) - '0';
394 *skip_len = 2; /* \\ and one digit */
395 return ret;
397 ret = -2;
398 *skip_len += 2;
399 switch (*(curr_str + 1)) {
400 case 'U':
401 *replace_flags |= REPLACE_T_UPP_TRANSFORM;
402 *replace_flags &= ~REPLACE_T_LOW_TRANSFORM;
403 break;
404 case 'u':
405 *replace_flags |= REPLACE_T_UPP_TRANSFORM_CHAR;
406 break;
407 case 'L':
408 *replace_flags |= REPLACE_T_LOW_TRANSFORM;
409 *replace_flags &= ~REPLACE_T_UPP_TRANSFORM;
410 break;
411 case 'l':
412 *replace_flags |= REPLACE_T_LOW_TRANSFORM_CHAR;
413 break;
414 case 'E':
415 *replace_flags = REPLACE_T_NO_TRANSFORM;
416 break;
417 default:
418 ret = -1;
419 break;
422 return ret;
424 static void
425 mc_search_regex__process_append_str (GString * dest_str, const char *from, gsize len,
426 replace_transform_type_t * replace_flags)
428 gsize loop = 0;
429 gsize char_len;
430 char *tmp_str;
431 GString *tmp_string;
433 if (len == (gsize) -1)
434 len = strlen (from);
436 if (*replace_flags == REPLACE_T_NO_TRANSFORM) {
437 g_string_append_len (dest_str, from, len);
438 return;
440 while (loop < 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);
465 } else {
466 g_string_append (dest_str, tmp_str);
468 g_free (tmp_str);
469 loop += char_len;
474 /*** public functions ****************************************************************************/
476 void
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)
480 GString *tmp = NULL;
481 #ifdef SEARCH_TYPE_GLIB
482 GError *error = NULL;
483 #else /* SEARCH_TYPE_GLIB */
484 const char *error;
485 int erroffset;
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,
497 &error);
499 if (error != NULL) {
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);
503 return;
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);
511 return;
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) {
515 if (error) {
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;
520 return;
523 #endif /* SEARCH_TYPE_GLIB */
526 /* --------------------------------------------------------------------------------------------- */
528 gboolean
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;
533 int current_chr = 0;
534 gint start_pos;
535 gint end_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;
547 while (1) {
548 current_chr = mc_search__get_char (lc_mc_search, user_data, current_pos);
549 if (current_chr == MC_SEARCH_CB_ABORT)
550 break;
552 current_pos++;
554 if (current_chr == MC_SEARCH_CB_SKIP)
555 continue;
557 virtual_pos++;
559 g_string_append_c (lc_mc_search->regex_buffer, (char) current_chr);
562 if (current_chr == 0 || (char) current_chr == '\n')
563 break;
565 if (virtual_pos > end_search)
566 break;
569 switch (mc_search__regex_found_cond (lc_mc_search, lc_mc_search->regex_buffer)) {
570 case COND__FOUND_OK:
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 */
577 if (found_len)
578 *found_len = end_pos - start_pos;
579 lc_mc_search->normal_offset = lc_mc_search->start_buffer + start_pos;
580 return TRUE;
581 break;
582 case COND__NOT_ALL_FOUND:
583 break;
584 default:
585 g_string_free (lc_mc_search->regex_buffer, TRUE);
586 lc_mc_search->regex_buffer = NULL;
587 return FALSE;
588 break;
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)
595 break;
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));
603 else
604 lc_mc_search->error_str = NULL;
606 return FALSE;
609 /* --------------------------------------------------------------------------------------------- */
610 GString *
611 mc_search_regex_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str)
613 GString *ret;
614 gchar *tmp_str;
616 int num_replace_tokens, lc_index;
617 gsize loop;
618 gsize len = 0;
619 gchar *prev_str;
620 replace_transform_type_t replace_flags = REPLACE_T_NO_TRANSFORM;
622 num_replace_tokens =
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);
632 return NULL;
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) {
641 if (len != 0) {
642 mc_search_regex__process_append_str (ret, prev_str,
643 replace_str->str - prev_str + loop,
644 &replace_flags);
645 mc_search_regex__process_append_str (ret, replace_str->str + loop + 1, len - 1,
646 &replace_flags);
647 prev_str = replace_str->str + loop + len;
648 loop += len - 1;
650 continue;
653 if (lc_index == -2) {
654 if (loop)
655 mc_search_regex__process_append_str (ret, prev_str,
656 replace_str->str - prev_str + loop,
657 &replace_flags);
658 prev_str = replace_str->str + loop + len;
659 loop += len - 1;
660 continue;
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);
667 return NULL;
670 tmp_str = mc_search_regex__get_token_by_num (lc_mc_search, lc_index);
671 if (tmp_str == NULL)
672 continue;
674 if (loop)
675 mc_search_regex__process_append_str (ret, prev_str, replace_str->str - prev_str + loop,
676 &replace_flags);
677 prev_str = replace_str->str + loop + len;
679 mc_search_regex__process_append_str (ret, tmp_str, -1, &replace_flags);
680 g_free (tmp_str);
681 loop += len - 1;
683 mc_search_regex__process_append_str (ret, prev_str,
684 replace_str->str - prev_str + replace_str->len,
685 &replace_flags);
687 return ret;