~ubuntu-branches/debian/squeeze/nano/squeeze

« back to all changes in this revision

Viewing changes to src/search.c

  • Committer: Bazaar Package Importer
  • Author(s): Jordi Mallach
  • Date: 2008-08-25 17:27:55 UTC
  • mfrom: (13.1.6 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080825172755-f7nqabuhghz16a5u
Tags: 2.0.7-4
* The "Valencia, tenemos la fórmula" release.
* Add patch successful_write_no_free_space.patch to solve an issue
  with nano reporting successful writes on full devices, potentially
  causing files to be zeroed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: search.c,v 1.193 2006/12/13 00:27:45 dolorous Exp $ */
 
1
/* $Id: search.c,v 1.197.2.6 2007/10/11 05:01:31 dolorous Exp $ */
2
2
/**************************************************************************
3
3
 *   search.c                                                             *
4
4
 *                                                                        *
5
 
 *   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004 Chris Allegretta    *
6
 
 *   Copyright (C) 2005, 2006 David Lawrence Ramsey                       *
 
5
 *   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007   *
 
6
 *   Free Software Foundation, Inc.                                       *
7
7
 *   This program is free software; you can redistribute it and/or modify *
8
8
 *   it under the terms of the GNU General Public License as published by *
9
 
 *   the Free Software Foundation; either version 2, or (at your option)  *
 
9
 *   the Free Software Foundation; either version 3, or (at your option)  *
10
10
 *   any later version.                                                   *
11
11
 *                                                                        *
12
12
 *   This program is distributed in the hope that it will be useful, but  *
39
39
static bool regexp_compiled = FALSE;
40
40
        /* Have we compiled any regular expressions? */
41
41
 
42
 
/* Regular expression helper functions. */
43
 
 
44
 
/* Compile the given regular expression.  Return value 0 means the
45
 
 * expression was invalid, and we wrote an error message on the status
46
 
 * bar.  Return value 1 means success. */
47
 
int regexp_init(const char *regexp)
 
42
/* Compile the regular expression regexp to see if it's valid.  Return
 
43
 * TRUE if it is, or FALSE otherwise. */
 
44
bool regexp_init(const char *regexp)
48
45
{
49
 
    int rc = regcomp(&search_regexp, regexp, REG_EXTENDED
 
46
    int rc;
 
47
 
 
48
    assert(!regexp_compiled);
 
49
 
 
50
    rc = regcomp(&search_regexp, regexp, REG_EXTENDED
50
51
#ifndef NANO_TINY
51
52
        | (ISSET(CASE_SENSITIVE) ? 0 : REG_ICASE)
52
53
#endif
53
54
        );
54
55
 
55
 
    assert(!regexp_compiled);
56
 
 
57
56
    if (rc != 0) {
58
57
        size_t len = regerror(rc, &search_regexp, NULL, 0);
59
58
        char *str = charalloc(len);
61
60
        regerror(rc, &search_regexp, str, len);
62
61
        statusbar(_("Bad regex \"%s\": %s"), regexp, str);
63
62
        free(str);
64
 
        return 0;
 
63
 
 
64
        return FALSE;
65
65
    }
66
66
 
67
67
    regexp_compiled = TRUE;
68
 
    return 1;
 
68
 
 
69
    return TRUE;
69
70
}
70
71
 
71
72
/* Decompile the compiled regular expression we used in the last
85
86
{
86
87
    char *disp;
87
88
    int numchars;
88
 
 
 
89
 
89
90
    assert(str != NULL);
90
91
 
91
92
    disp = display_string(str, 0, (COLS / 2) + 1, FALSE);
207
208
    backupstring = NULL;
208
209
 
209
210
    /* Cancel any search, or just return with no previous search. */
210
 
    if (i == -1 || (i < 0 && last_search[0] == '\0') ||
211
 
            (!replacing && i == 0 && answer[0] == '\0')) {
 
211
    if (i == -1 || (i < 0 && *last_search == '\0') || (!replacing &&
 
212
        i == 0 && *answer == '\0')) {
212
213
        statusbar(_("Cancelled"));
213
214
        return -1;
214
215
    } else {
218
219
#ifdef HAVE_REGEX_H
219
220
                /* Use last_search if answer is an empty string, or
220
221
                 * answer if it isn't. */
221
 
                if (ISSET(USE_REGEXP) &&
222
 
                        regexp_init((i == -2) ? last_search :
223
 
                        answer) == 0)
 
222
                if (ISSET(USE_REGEXP) && !regexp_init((i == -2) ?
 
223
                        last_search : answer))
224
224
                    return -1;
225
225
#endif
226
226
                break;
444
444
        return;
445
445
 
446
446
    /* If answer is now "", copy last_search into answer. */
447
 
    if (answer[0] == '\0')
 
447
    if (*answer == '\0')
448
448
        answer = mallocstrcpy(answer, last_search);
449
449
    else
450
450
        last_search = mallocstrcpy(last_search, answer);
511
511
    if (last_search[0] != '\0') {
512
512
#ifdef HAVE_REGEX_H
513
513
        /* Since answer is "", use last_search! */
514
 
        if (ISSET(USE_REGEXP) && regexp_init(last_search) == 0)
 
514
        if (ISSET(USE_REGEXP) && !regexp_init(last_search))
515
515
            return;
516
516
#endif
517
517
 
827
827
                openfile->current_x += match_len + length_change - 1;
828
828
 
829
829
            /* Cleanup. */
830
 
            openfile->totsize += length_change;
 
830
            openfile->totsize += mbstrlen(copy) -
 
831
                mbstrlen(openfile->current->data);
831
832
            free(openfile->current->data);
832
833
            openfile->current->data = copy;
833
834
 
977
978
    if (interactive) {
978
979
        char *ans = mallocstrcpy(NULL, answer);
979
980
 
980
 
        /* Ask for it. */
 
981
        /* Ask for the line and column. */
981
982
        int i = do_prompt(FALSE,
982
983
#ifndef DISABLE_TABCOMP
983
984
                TRUE,