summaryrefslogtreecommitdiffstats
path: root/lib/search/search.c
blob: 8ccb65fb9f88ab7aeec924149f85e5d866b7565a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*
   Search text engine.
   Interface functions

   Copyright (C) 2009-2023
   Free Software Foundation, Inc.

   Written by:
   Slava Zanko <slavazanko@gmail.com>, 2009
   Andrew Borodin <aborodin@vmail.ru>, 2013

   This file is part of the Midnight Commander.

   The Midnight Commander is free software: you can redistribute it
   and/or modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation, either version 3 of the License,
   or (at your option) any later version.

   The Midnight Commander is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include <stdarg.h>
#include <stdlib.h>
#include <sys/types.h>

#include "lib/global.h"
#include "lib/strutil.h"
#include "lib/search.h"
#include "lib/util.h"
#ifdef HAVE_CHARSET
#include "lib/charsets.h"
#endif

#include "internal.h"

/*** global variables ****************************************************************************/

/*** file scope macro definitions ****************************************************************/

/*** file scope type declarations ****************************************************************/

/*** forward declarations (file scope functions) *************************************************/

/*** file scope variables ************************************************************************/

static const mc_search_type_str_t mc_search__list_types[] = {
    {N_("No&rmal"), MC_SEARCH_T_NORMAL},
    {N_("Re&gular expression"), MC_SEARCH_T_REGEX},
    {N_("He&xadecimal"), MC_SEARCH_T_HEX},
    {N_("Wil&dcard search"), MC_SEARCH_T_GLOB},
    {NULL, MC_SEARCH_T_INVALID}
};

/* --------------------------------------------------------------------------------------------- */
/*** file scope functions ************************************************************************/
/* --------------------------------------------------------------------------------------------- */

static mc_search_cond_t *
mc_search__cond_struct_new (mc_search_t * lc_mc_search, const GString * str, const char *charset)
{
    mc_search_cond_t *mc_search_cond;

    mc_search_cond = g_malloc0 (sizeof (mc_search_cond_t));
    mc_search_cond->str = mc_g_string_dup (str);
    mc_search_cond->charset = g_strdup (charset);
#ifdef HAVE_PCRE2
    lc_mc_search->regex_match_info = pcre2_match_data_create (MC_SEARCH__NUM_REPLACE_ARGS, NULL);
    lc_mc_search->iovector = pcre2_get_ovector_pointer (lc_mc_search->regex_match_info);
#endif
    switch (lc_mc_search->search_type)
    {
    case MC_SEARCH_T_GLOB:
        mc_search__cond_struct_new_init_glob (charset, lc_mc_search, mc_search_cond);
        break;
    case MC_SEARCH_T_NORMAL:
        mc_search__cond_struct_new_init_normal (charset, lc_mc_search, mc_search_cond);
        break;
    case MC_SEARCH_T_REGEX:
        mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond);
        break;
    case MC_SEARCH_T_HEX:
        mc_search__cond_struct_new_init_hex (charset, lc_mc_search, mc_search_cond);
        break;
    default:
        break;
    }
    return mc_search_cond;
}

/* --------------------------------------------------------------------------------------------- */

static void
mc_search__cond_struct_free (gpointer data)
{
    mc_search_cond_t *mc_search_cond = (mc_search_cond_t *) data;

    if (mc_search_cond->upper != NULL)
        g_string_free (mc_search_cond->upper, TRUE);

    if (mc_search_cond->lower != NULL)
        g_string_free (mc_search_cond->lower, TRUE);

    g_string_free (mc_search_cond->str, TRUE);
    g_free (mc_search_cond->charset);

#ifdef SEARCH_TYPE_GLIB
    if (mc_search_cond->regex_handle != NULL)
        g_regex_unref (mc_search_cond->regex_handle);
#else /* SEARCH_TYPE_GLIB */
    g_free (mc_search_cond->regex_handle);
#endif /* SEARCH_TYPE_GLIB */

    g_free (mc_search_cond);
}

/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
/* Init search descriptor.
 *
 * @param original pattern to search
 * @param original_charset charset of #original. If NULL then cp_display will be used
 *
 * @return new mc_search_t object. Use #mc_search_free() to free it.
 */

mc_search_t *
mc_search_new (const gchar * original, const gchar * original_charset)
{
    if (original == NULL)
        return NULL;

    return mc_search_new_len (original, strlen (original), original_charset);
}

/* --------------------------------------------------------------------------------------------- */
/* Init search descriptor.
 *
 * @param original pattern to search
 * @param original_len length of #original or -1 if #original is NULL-terminated
 * @param original_charset charset of #original. If NULL then cp_display will be used
 *
 * @return new mc_search_t object. Use #mc_search_free() to free it.
 */

mc_search_t *
mc_search_new_len (const gchar * original, gsize original_len, const gchar * original_charset)
{
    mc_search_t *lc_mc_search;

    if (original == NULL || original_len == 0)
        return NULL;

    lc_mc_search = g_new0 (mc_search_t, 1);
    lc_mc_search->original.str = g_string_new_len (original, original_len);
#ifdef HAVE_CHARSET
    lc_mc_search->original.charset =
        g_strdup (original_charset != NULL
                  && *original_charset != '\0' ? original_charset : cp_display);
#else
    (void) original_charset;
#endif

    return lc_mc_search;
}

/* --------------------------------------------------------------------------------------------- */

void
mc_search_free (mc_search_t * lc_mc_search)
{
    if (lc_mc_search == NULL)
        return;

    g_string_free (lc_mc_search->original.str, TRUE);
#ifdef HAVE_CHARSET
    g_free (lc_mc_search->original.charset);
#endif
    g_free (lc_mc_search->error_str);

    if (lc_mc_search->prepared.conditions != NULL)
        g_ptr_array_free (lc_mc_search->prepared.conditions, TRUE);

#ifdef SEARCH_TYPE_GLIB
    if (lc_mc_search->regex_match_info != NULL)
        g_match_info_free (lc_mc_search->regex_match_info);
#else /* SEARCH_TYPE_GLIB */
    g_free (lc_mc_search->regex_match_info);
#endif /* SEARCH_TYPE_GLIB */

    if (lc_mc_search->regex_buffer != NULL)
        g_string_free (lc_mc_search->regex_buffer, TRUE);

    g_free (lc_mc_search);
}

/* --------------------------------------------------------------------------------------------- */

gboolean
mc_search_prepare (mc_search_t * lc_mc_search)
{
    GPtrArray *ret;

    if (lc_mc_search->prepared.conditions != NULL)
        return lc_mc_search->prepared.result;

    ret = g_ptr_array_new_with_free_func (mc_search__cond_struct_free);
#ifdef HAVE_CHARSET
    if (!lc_mc_search->is_all_charsets)
        g_ptr_array_add (ret,
                         mc_search__cond_struct_new (lc_mc_search, lc_mc_search->original.str,
                                                     lc_mc_search->original.charset));
    else
    {
        gsize loop1;

        for (loop1 = 0; loop1 < codepages->len; loop1++)
        {
            const char *id;

            id = ((codepage_desc *) g_ptr_array_index (codepages, loop1))->id;
            if (g_ascii_strcasecmp (id, lc_mc_search->original.charset) == 0)
                g_ptr_array_add (ret,
                                 mc_search__cond_struct_new (lc_mc_search,
                                                             lc_mc_search->original.str,
                                                             lc_mc_search->original.charset));
            else
            {
                GString *buffer;

                buffer =
                    mc_search__recode_str (lc_mc_search->original.str->str,
                                           lc_mc_search->original.str->len,
                                           lc_mc_search->original.charset, id);
                g_ptr_array_add (ret, mc_search__cond_struct_new (lc_mc_search, buffer, id));
                g_string_free (buffer, TRUE);
            }
        }
    }
#else
    g_ptr_array_add (ret,
                     mc_search__cond_struct_new (lc_mc_search, lc_mc_search->original.str,
                                                 str_detect_termencoding ()));
#endif
    lc_mc_search->prepared.conditions = ret;
    lc_mc_search->prepared.result = (lc_mc_search->error == MC_SEARCH_E_OK);

    return lc_mc_search->prepared.result;
}

/* --------------------------------------------------------------------------------------------- */

/**
 * Carries out the search.
 *
 * Returns TRUE if found.
 *
 * Returns FALSE if not found. In this case, lc_mc_search->error reveals
 * the reason:
 *
 *   - MC_SEARCH_E_NOTFOUND: the pattern isn't in the subject string.
 *   - MC_SEARCH_E_ABORT: the user aborted the search.
 *   - For any other reason (but not for the above two!): the description
 *     is in lc_mc_search->error_str.
 */
gboolean
mc_search_run (mc_search_t * lc_mc_search, const void *user_data,
               gsize start_search, gsize end_search, gsize * found_len)
{
    gboolean ret = FALSE;

    if (lc_mc_search == NULL || user_data == NULL)
        return FALSE;
    if (!mc_search_is_type_avail (lc_mc_search->search_type))
    {
        mc_search_set_error (lc_mc_search, MC_SEARCH_E_INPUT, "%s", _(STR_E_UNKNOWN_TYPE));
        return FALSE;
    }
#ifdef SEARCH_TYPE_GLIB
    if (lc_mc_search->regex_match_info != NULL)
    {
        g_match_info_free (lc_mc_search->regex_match_info);
        lc_mc_search->regex_match_info = NULL;
    }
#endif /* SEARCH_TYPE_GLIB */

    mc_search_set_error (lc_mc_search, MC_SEARCH_E_OK, NULL);

    if (!mc_search_prepare (lc_mc_search))
        return FALSE;

    switch (lc_mc_search->search_type)
    {
    case MC_SEARCH_T_NORMAL:
        ret = mc_search__run_normal (lc_mc_search, user_data, start_search, end_search, found_len);
        break;
    case MC_SEARCH_T_REGEX:
        ret = mc_search__run_regex (lc_mc_search, user_data, start_search, end_search, found_len);
        break;
    case MC_SEARCH_T_GLOB:
        ret = mc_search__run_glob (lc_mc_search, user_data, start_search, end_search, found_len);
        break;
    case MC_SEARCH_T_HEX:
        ret = mc_search__run_hex (lc_mc_search, user_data, start_search, end_search, found_len);
        break;
    default:
        break;
    }
    return ret;
}

/* --------------------------------------------------------------------------------------------- */

gboolean
mc_search_is_type_avail (mc_search_type_t search_type)
{
    switch (search_type)
    {
    case MC_SEARCH_T_GLOB:
    case MC_SEARCH_T_NORMAL:
    case MC_SEARCH_T_REGEX:
    case MC_SEARCH_T_HEX:
        return TRUE;
    default:
        break;
    }
    return FALSE;
}

/* --------------------------------------------------------------------------------------------- */

const mc_search_type_str_t *
mc_search_types_list_get (size_t * num)
{
    /* don't count last NULL item */
    if (num != NULL)
        *num = G_N_ELEMENTS (mc_search__list_types) - 1;

    return mc_search__list_types;
}

/* --------------------------------------------------------------------------------------------- */

GString *
mc_search_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str)
{
    GString *ret;

    if (replace_str == NULL || replace_str->len == 0)
        return g_string_new ("");

    if (lc_mc_search == NULL)
        return mc_g_string_dup (replace_str);

    switch (lc_mc_search->search_type)
    {
    case MC_SEARCH_T_REGEX:
        ret = mc_search_regex_prepare_replace_str (lc_mc_search, replace_str);
        break;
    case MC_SEARCH_T_GLOB:
        ret = mc_search_glob_prepare_replace_str (lc_mc_search, replace_str);
        break;
    case MC_SEARCH_T_NORMAL:
        ret = mc_search_normal_prepare_replace_str (lc_mc_search, replace_str);
        break;
    case MC_SEARCH_T_HEX:
        ret = mc_search_hex_prepare_replace_str (lc_mc_search, replace_str);
        break;
    default:
        ret = mc_g_string_dup (replace_str);
        break;
    }
    return ret;
}

/* --------------------------------------------------------------------------------------------- */

char *
mc_search_prepare_replace_str2 (mc_search_t * lc_mc_search, const char *replace_str)
{
    GString *ret;
    GString *replace_str2;

    replace_str2 = g_string_new (replace_str);
    ret = mc_search_prepare_replace_str (lc_mc_search, replace_str2);
    g_string_free (replace_str2, TRUE);
    return (ret != NULL) ? g_string_free (ret, FALSE) : NULL;
}

/* --------------------------------------------------------------------------------------------- */

gboolean
mc_search_is_fixed_search_str (const mc_search_t * lc_mc_search)
{
    if (lc_mc_search == NULL)
        return FALSE;
    switch (lc_mc_search->search_type)
    {
    case MC_SEARCH_T_REGEX:
    case MC_SEARCH_T_GLOB:
        return FALSE;
    default:
        return TRUE;
    }
}

/* --------------------------------------------------------------------------------------------- */
/* Search specified pattern in specified string.
 *
 * @param pattern string to search
 * @param pattern_charset charset of #pattern. If NULL then cp_display will be used
 * @param str string where search #pattern
 * @param search type (normal, regex, hex or glob)
 *
 * @return TRUE if found is successful, FALSE otherwise.
 */

gboolean
mc_search (const gchar * pattern, const gchar * pattern_charset, const gchar * str,
           mc_search_type_t type)
{
    gboolean ret;
    mc_search_t *search;

    if (str == NULL)
        return FALSE;

    search = mc_search_new (pattern, pattern_charset);
    if (search == NULL)
        return FALSE;

    search->search_type = type;
    search->is_case_sensitive = TRUE;

    if (type == MC_SEARCH_T_GLOB)
        search->is_entire_line = TRUE;

    ret = mc_search_run (search, str, 0, strlen (str), NULL);
    mc_search_free (search);
    return ret;
}

/* --------------------------------------------------------------------------------------------- */

int
mc_search_getstart_result_by_num (mc_search_t * lc_mc_search, int lc_index)
{
    if (lc_mc_search == NULL)
        return 0;
    if (lc_mc_search->search_type == MC_SEARCH_T_NORMAL)
        return 0;
#ifdef SEARCH_TYPE_GLIB
    {
        gint start_pos;
        gint end_pos;

        g_match_info_fetch_pos (lc_mc_search->regex_match_info, lc_index, &start_pos, &end_pos);
        return (int) start_pos;
    }
#else /* SEARCH_TYPE_GLIB */
    return lc_mc_search->iovector[lc_index * 2];
#endif /* SEARCH_TYPE_GLIB */
}

/* --------------------------------------------------------------------------------------------- */

int
mc_search_getend_result_by_num (mc_search_t * lc_mc_search, int lc_index)
{
    if (lc_mc_search == NULL)
        return 0;
    if (lc_mc_search->search_type == MC_SEARCH_T_NORMAL)
        return 0;
#ifdef SEARCH_TYPE_GLIB
    {
        gint start_pos;
        gint end_pos;

        g_match_info_fetch_pos (lc_mc_search->regex_match_info, lc_index, &start_pos, &end_pos);
        return (int) end_pos;
    }
#else /* SEARCH_TYPE_GLIB */
    return lc_mc_search->iovector[lc_index * 2 + 1];
#endif /* SEARCH_TYPE_GLIB */
}

/* --------------------------------------------------------------------------------------------- */
/**
 * Replace an old error code and message of an mc_search_t object.
 *
 * @param mc_search mc_search_t object
 * @param code error code, one of mc_search_error_t values
 * @param format format of error message. If NULL, the old error string is free'd and become NULL
 */

void
mc_search_set_error (mc_search_t * lc_mc_search, mc_search_error_t code, const gchar * format, ...)
{
    lc_mc_search->error = code;

    MC_PTR_FREE (lc_mc_search->error_str);

    if (format != NULL)
    {
        va_list args;

        va_start (args, format);
        lc_mc_search->error_str = g_strdup_vprintf (format, args);
        va_end (args);
    }
}

/* --------------------------------------------------------------------------------------------- */