utils.c 6.25 KB
Newer Older
Chris Allegretta's avatar
Chris Allegretta committed
1
/* $Id$ */
Chris Allegretta's avatar
Chris Allegretta committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**************************************************************************
 *   utils.c                                                              *
 *                                                                        *
 *   Copyright (C) 1999 Chris Allegretta                                  *
 *   This program 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 1, or (at your option)  *
 *   any later version.                                                   *
 *                                                                        *
 *   This program 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, write to the Free Software          *
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.            *
 *                                                                        *
 **************************************************************************/

22
#include <unistd.h>
Chris Allegretta's avatar
Chris Allegretta committed
23
24
25
26
27
28
29
30
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "config.h"
#include "nano.h"
#include "proto.h"

Chris Allegretta's avatar
Chris Allegretta committed
31
#ifndef NANO_SMALL
Chris Allegretta's avatar
Chris Allegretta committed
32
33
34
35
36
37
#include <libintl.h>
#define _(string) gettext(string)
#else
#define _(string) (string)
#endif

Chris Allegretta's avatar
Chris Allegretta committed
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
/* Lower case a string - must be null terminated */
void lowercase(char *src)
{
    long i = 0;

    while (src[i] != 0) {
	src[i] = (char) tolower(src[i]);
	i++;
    }
}


/* I can't believe I have to write this function */
char *strcasestr(char *haystack, char *needle)
{
    char *localneedle, *localhaystack, *found, *tmp, *tmp2;

    /* Make a copy of the search string and searcgh space */
    localneedle = nmalloc(strlen(needle) + 2);
    localhaystack = nmalloc(strlen(haystack) + 2);

    strcpy(localneedle, needle);
    strcpy(localhaystack, haystack);

    /* Make them lowercase */
    lowercase(localneedle);
    lowercase(localhaystack);

    /* Look for the lowercased substring in the lowercased search space -
       return NULL if we didn't find anything */
    if ((found = strstr(localhaystack, localneedle)) == NULL) {
	free(localneedle);
	free(localhaystack);
	return NULL;
    }
    /* Else return the pointer to the same place in the real search space */
    tmp2 = haystack;
    for (tmp = localhaystack; tmp != found; tmp++)
	tmp2++;

    free(localneedle);
    free(localhaystack);
    return tmp2;
}

char *strstrwrapper(char *haystack, char *needle)
{
85
#ifdef HAVE_REGEX_H
86
    if (ISSET(USE_REGEXP)) {
87
88
89
90
91
	int result = regexec(&search_regexp, haystack, 10, regmatches, 0);
	if (!result)
	    return haystack + regmatches[0].rm_so;
	return 0;
    }
92
#endif
Chris Allegretta's avatar
Chris Allegretta committed
93
94
95
96
97
    if (ISSET(CASE_SENSITIVE))
	return strstr(haystack, needle);
    else
	return strcasestr(haystack, needle);
}
Chris Allegretta's avatar
Chris Allegretta committed
98
99
100

/* Thanks BG, many ppl have been asking for this... */
void *nmalloc(size_t howmuch)
Chris Allegretta's avatar
Chris Allegretta committed
101
{
Chris Allegretta's avatar
Chris Allegretta committed
102
    void *r;
Chris Allegretta's avatar
Chris Allegretta committed
103

Chris Allegretta's avatar
Chris Allegretta committed
104
105
106
107
108
109
110
    /* Panic save? */

    if (!(r = malloc(howmuch)))
	die(_("nano: malloc: out of memory!"));

    return r;
}
Chris Allegretta's avatar
Chris Allegretta committed
111

112
113
114
115
116
117
118
119
120
121
122
123
124
/* We're going to need this too */
void *ncalloc(size_t howmuch, size_t size)
{
    void *r;

    /* Panic save? */

    if (!(r = calloc(howmuch, size)))
	die(_("nano: calloc: out of memory!"));

    return r;
}

Chris Allegretta's avatar
Chris Allegretta committed
125
void *nrealloc(void *ptr, size_t howmuch)
Chris Allegretta's avatar
Chris Allegretta committed
126
{
Chris Allegretta's avatar
Chris Allegretta committed
127
    void *r;
Chris Allegretta's avatar
Chris Allegretta committed
128

Chris Allegretta's avatar
Chris Allegretta committed
129
    if (!(r = realloc(ptr, howmuch)))
Jordi Mallach's avatar
   
Jordi Mallach committed
130
	die(_("nano: realloc: out of memory!"));
Chris Allegretta's avatar
Chris Allegretta committed
131
132
133

    return r;
}
Robert Siemborski's avatar
Robert Siemborski committed
134

135
136
137
138
/* Copy one malloced string to another pointer.

   Should be used as dest = mallocstrcpy(dest, src);
*/
139
void *mallocstrcpy(char *dest, char *src)
140
141
{

142
143
144
145

    if (src == dest)
	return src;

146
147
148
    if (dest != NULL)
	free(dest);

149
150
151
152
153
    if (src == NULL) {
	dest = NULL;
	return(dest);
    }

154
155
156
157
158
159
160
    dest = nmalloc(strlen(src) + 1);
    strcpy(dest, src);

    return dest;
}


Robert Siemborski's avatar
Robert Siemborski committed
161
/* Append a new magic-line to filebot */
162
163
void new_magicline(void)
{
Robert Siemborski's avatar
Robert Siemborski committed
164
165
166
167
168
169
170
171
    filebot->next = nmalloc(sizeof(filestruct));
    filebot->next->data = nmalloc(1);
    filebot->next->data[0] = '\0';
    filebot->next->prev = filebot;
    filebot->next->next = NULL;
    filebot->next->lineno = filebot->lineno + 1;
    filebot = filebot->next;
    totlines++;
172
    totsize++;
Robert Siemborski's avatar
Robert Siemborski committed
173
}
Chris Allegretta's avatar
Chris Allegretta committed
174

175
#ifndef DISABLE_TABCOMP
Chris Allegretta's avatar
Chris Allegretta committed
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
/*
 * Routine to see if a text string is matched by a wildcard pattern.
 * Returns TRUE if the text is matched, or FALSE if it is not matched
 * or if the pattern is invalid.
 *  *		matches zero or more characters
 *  ?		matches a single character
 *  [abc]	matches 'a', 'b' or 'c'
 *  \c		quotes character c
 * Adapted from code written by Ingo Wilken, and
 * then taken from sash, Copyright (c) 1999 by David I. Bell
 * Permission is granted to use, distribute, or modify this source,
 * provided that this copyright notice remains intact.
 * Permission to distribute this code under the GPL has been granted.
 */
int check_wildcard_match(const char *text, const char *pattern)
{
    const char *retryPat;
    const char *retryText;
    int ch;
    int found;
    int len;

    retryPat = NULL;
    retryText = NULL;

    while (*text || *pattern) {
	ch = *pattern++;

	switch (ch) {
	case '*':
	    retryPat = pattern;
	    retryText = text;
	    break;

	case '[':
	    found = FALSE;

	    while ((ch = *pattern++) != ']') {
		if (ch == '\\')
		    ch = *pattern++;

		if (ch == '\0')
		    return FALSE;

		if (*text == ch)
		    found = TRUE;
	    }
	    len = strlen(text);
	    if (found == FALSE && len != 0) {
		return FALSE;
	    }
	    if (found == TRUE) {
		if (strlen(pattern) == 0 && len == 1) {
		    return TRUE;
		}
		if (len != 0) {
		    text++;
		    continue;
		}
	    }

	    /* fall into next case */

	case '?':
	    if (*text++ == '\0')
		return FALSE;

	    break;

	case '\\':
	    ch = *pattern++;

	    if (ch == '\0')
		return FALSE;

	    /* fall into next case */

	default:
	    if (*text == ch) {
		if (*text)
		    text++;
		break;
	    }

	    if (*text) {
		pattern = retryPat;
		text = ++retryText;
		break;
	    }

	    return FALSE;
	}

	if (pattern == NULL)
	    return FALSE;
    }

    return TRUE;
}
275
#endif