blob: 3d0ea6d76b0a2a9542cdba602c0195a8a4294976 [file] [log] [blame]
Chris Allegretta11b00112000-08-06 21:13:45 +00001/* $Id$ */
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002/**************************************************************************
3 * utils.c *
4 * *
5 * Copyright (C) 1999 Chris Allegretta *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 1, or (at your option) *
9 * any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
19 * *
20 **************************************************************************/
21
Chris Allegrettaf6b13422000-07-07 04:25:00 +000022#include <unistd.h>
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000023#include <stdlib.h>
24#include <string.h>
25#include <ctype.h>
26
27#include "config.h"
28#include "nano.h"
29#include "proto.h"
30
Chris Allegretta4da1fc62000-06-21 03:00:43 +000031#ifndef NANO_SMALL
Chris Allegrettabceb1b22000-06-19 04:22:15 +000032#include <libintl.h>
33#define _(string) gettext(string)
34#else
35#define _(string) (string)
36#endif
37
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000038/* Lower case a string - must be null terminated */
39void lowercase(char *src)
40{
41 long i = 0;
42
43 while (src[i] != 0) {
44 src[i] = (char) tolower(src[i]);
45 i++;
46 }
47}
48
49
50/* I can't believe I have to write this function */
51char *strcasestr(char *haystack, char *needle)
52{
53 char *localneedle, *localhaystack, *found, *tmp, *tmp2;
54
55 /* Make a copy of the search string and searcgh space */
56 localneedle = nmalloc(strlen(needle) + 2);
57 localhaystack = nmalloc(strlen(haystack) + 2);
58
59 strcpy(localneedle, needle);
60 strcpy(localhaystack, haystack);
61
62 /* Make them lowercase */
63 lowercase(localneedle);
64 lowercase(localhaystack);
65
66 /* Look for the lowercased substring in the lowercased search space -
67 return NULL if we didn't find anything */
68 if ((found = strstr(localhaystack, localneedle)) == NULL) {
69 free(localneedle);
70 free(localhaystack);
71 return NULL;
72 }
73 /* Else return the pointer to the same place in the real search space */
74 tmp2 = haystack;
75 for (tmp = localhaystack; tmp != found; tmp++)
76 tmp2++;
77
78 free(localneedle);
79 free(localhaystack);
80 return tmp2;
81}
82
83char *strstrwrapper(char *haystack, char *needle)
84{
Chris Allegretta805c26d2000-09-06 13:39:17 +000085#ifdef HAVE_REGEX_H
Chris Allegretta9fc8d432000-07-07 01:49:52 +000086 if (ISSET(USE_REGEXP)) {
Chris Allegrettabd9e7c32000-10-26 01:44:42 +000087 int result = regexec(&search_regexp, haystack, 10, regmatches, 0);
88 if (!result)
89 return haystack + regmatches[0].rm_so;
90 return 0;
91 }
Chris Allegrettaf6b13422000-07-07 04:25:00 +000092#endif
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000093 if (ISSET(CASE_SENSITIVE))
94 return strstr(haystack, needle);
95 else
96 return strcasestr(haystack, needle);
97}
Chris Allegrettabceb1b22000-06-19 04:22:15 +000098
99/* Thanks BG, many ppl have been asking for this... */
100void *nmalloc(size_t howmuch)
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000101{
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000102 void *r;
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000103
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000104 /* Panic save? */
105
106 if (!(r = malloc(howmuch)))
107 die(_("nano: malloc: out of memory!"));
108
109 return r;
110}
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000111
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000112void *nrealloc(void *ptr, size_t howmuch)
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000113{
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000114 void *r;
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000115
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000116 if (!(r = realloc(ptr, howmuch)))
Jordi Mallache7a647c2000-11-01 18:43:21 +0000117 die(_("nano: realloc: out of memory!"));
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000118
119 return r;
120}
Robert Siemborski63b3d7e2000-07-04 22:15:39 +0000121
Chris Allegretta31925e42000-11-02 04:40:39 +0000122/* Copy one malloced string to another pointer.
123
124 Should be used as dest = mallocstrcpy(dest, src);
125*/
126void *mallocstrcpy(void *dest, void *src)
127{
128
129 if (dest != NULL)
130 free(dest);
131
Chris Allegretta5bf51d32000-11-16 06:01:10 +0000132 if (src == NULL) {
133 dest = NULL;
134 return(dest);
135 }
136
Chris Allegretta31925e42000-11-02 04:40:39 +0000137 dest = nmalloc(strlen(src) + 1);
138 strcpy(dest, src);
139
140 return dest;
141}
142
143
Robert Siemborski63b3d7e2000-07-04 22:15:39 +0000144/* Append a new magic-line to filebot */
Chris Allegrettabd9e7c32000-10-26 01:44:42 +0000145void new_magicline(void)
146{
Robert Siemborski63b3d7e2000-07-04 22:15:39 +0000147 filebot->next = nmalloc(sizeof(filestruct));
148 filebot->next->data = nmalloc(1);
149 filebot->next->data[0] = '\0';
150 filebot->next->prev = filebot;
151 filebot->next->next = NULL;
152 filebot->next->lineno = filebot->lineno + 1;
153 filebot = filebot->next;
154 totlines++;
Chris Allegrettae51c95f2000-12-10 05:54:27 +0000155 totsize++;
Robert Siemborski63b3d7e2000-07-04 22:15:39 +0000156}
Chris Allegretta04d848e2000-11-05 17:54:41 +0000157
Chris Allegretta09a80842000-11-30 02:31:13 +0000158#ifndef DISABLE_TABCOMP
Chris Allegretta04d848e2000-11-05 17:54:41 +0000159/*
160 * Routine to see if a text string is matched by a wildcard pattern.
161 * Returns TRUE if the text is matched, or FALSE if it is not matched
162 * or if the pattern is invalid.
163 * * matches zero or more characters
164 * ? matches a single character
165 * [abc] matches 'a', 'b' or 'c'
166 * \c quotes character c
167 * Adapted from code written by Ingo Wilken, and
168 * then taken from sash, Copyright (c) 1999 by David I. Bell
169 * Permission is granted to use, distribute, or modify this source,
170 * provided that this copyright notice remains intact.
171 * Permission to distribute this code under the GPL has been granted.
172 */
173int check_wildcard_match(const char *text, const char *pattern)
174{
175 const char *retryPat;
176 const char *retryText;
177 int ch;
178 int found;
179 int len;
180
181 retryPat = NULL;
182 retryText = NULL;
183
184 while (*text || *pattern) {
185 ch = *pattern++;
186
187 switch (ch) {
188 case '*':
189 retryPat = pattern;
190 retryText = text;
191 break;
192
193 case '[':
194 found = FALSE;
195
196 while ((ch = *pattern++) != ']') {
197 if (ch == '\\')
198 ch = *pattern++;
199
200 if (ch == '\0')
201 return FALSE;
202
203 if (*text == ch)
204 found = TRUE;
205 }
206 len = strlen(text);
207 if (found == FALSE && len != 0) {
208 return FALSE;
209 }
210 if (found == TRUE) {
211 if (strlen(pattern) == 0 && len == 1) {
212 return TRUE;
213 }
214 if (len != 0) {
215 text++;
216 continue;
217 }
218 }
219
220 /* fall into next case */
221
222 case '?':
223 if (*text++ == '\0')
224 return FALSE;
225
226 break;
227
228 case '\\':
229 ch = *pattern++;
230
231 if (ch == '\0')
232 return FALSE;
233
234 /* fall into next case */
235
236 default:
237 if (*text == ch) {
238 if (*text)
239 text++;
240 break;
241 }
242
243 if (*text) {
244 pattern = retryPat;
245 text = ++retryText;
246 break;
247 }
248
249 return FALSE;
250 }
251
252 if (pattern == NULL)
253 return FALSE;
254 }
255
256 return TRUE;
257}
Chris Allegretta09a80842000-11-30 02:31:13 +0000258#endif