Chris Allegretta | 11b0011 | 2000-08-06 21:13:45 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2 | /************************************************************************** |
| 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 Allegretta | f6b1342 | 2000-07-07 04:25:00 +0000 | [diff] [blame] | 22 | #include <unistd.h> |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 23 | #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 Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 31 | #ifndef NANO_SMALL |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 32 | #include <libintl.h> |
| 33 | #define _(string) gettext(string) |
| 34 | #else |
| 35 | #define _(string) (string) |
| 36 | #endif |
| 37 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 38 | /* Lower case a string - must be null terminated */ |
| 39 | void 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 */ |
| 51 | char *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 | |
| 83 | char *strstrwrapper(char *haystack, char *needle) |
| 84 | { |
Chris Allegretta | 805c26d | 2000-09-06 13:39:17 +0000 | [diff] [blame] | 85 | #ifdef HAVE_REGEX_H |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 86 | if (ISSET(USE_REGEXP)) { |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 87 | int result = regexec(&search_regexp, haystack, 10, regmatches, 0); |
| 88 | if (!result) |
| 89 | return haystack + regmatches[0].rm_so; |
| 90 | return 0; |
| 91 | } |
Chris Allegretta | f6b1342 | 2000-07-07 04:25:00 +0000 | [diff] [blame] | 92 | #endif |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 93 | if (ISSET(CASE_SENSITIVE)) |
| 94 | return strstr(haystack, needle); |
| 95 | else |
| 96 | return strcasestr(haystack, needle); |
| 97 | } |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 98 | |
| 99 | /* Thanks BG, many ppl have been asking for this... */ |
| 100 | void *nmalloc(size_t howmuch) |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 101 | { |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 102 | void *r; |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 103 | |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 104 | /* Panic save? */ |
| 105 | |
| 106 | if (!(r = malloc(howmuch))) |
| 107 | die(_("nano: malloc: out of memory!")); |
| 108 | |
| 109 | return r; |
| 110 | } |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 111 | |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 112 | void *nrealloc(void *ptr, size_t howmuch) |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 113 | { |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 114 | void *r; |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 115 | |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 116 | if (!(r = realloc(ptr, howmuch))) |
Jordi Mallach | e7a647c | 2000-11-01 18:43:21 +0000 | [diff] [blame] | 117 | die(_("nano: realloc: out of memory!")); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 118 | |
| 119 | return r; |
| 120 | } |
Robert Siemborski | 63b3d7e | 2000-07-04 22:15:39 +0000 | [diff] [blame] | 121 | |
Chris Allegretta | 31925e4 | 2000-11-02 04:40:39 +0000 | [diff] [blame] | 122 | /* Copy one malloced string to another pointer. |
| 123 | |
| 124 | Should be used as dest = mallocstrcpy(dest, src); |
| 125 | */ |
| 126 | void *mallocstrcpy(void *dest, void *src) |
| 127 | { |
| 128 | |
| 129 | if (dest != NULL) |
| 130 | free(dest); |
| 131 | |
Chris Allegretta | 5bf51d3 | 2000-11-16 06:01:10 +0000 | [diff] [blame] | 132 | if (src == NULL) { |
| 133 | dest = NULL; |
| 134 | return(dest); |
| 135 | } |
| 136 | |
Chris Allegretta | 31925e4 | 2000-11-02 04:40:39 +0000 | [diff] [blame] | 137 | dest = nmalloc(strlen(src) + 1); |
| 138 | strcpy(dest, src); |
| 139 | |
| 140 | return dest; |
| 141 | } |
| 142 | |
| 143 | |
Robert Siemborski | 63b3d7e | 2000-07-04 22:15:39 +0000 | [diff] [blame] | 144 | /* Append a new magic-line to filebot */ |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 145 | void new_magicline(void) |
| 146 | { |
Robert Siemborski | 63b3d7e | 2000-07-04 22:15:39 +0000 | [diff] [blame] | 147 | 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 Allegretta | e51c95f | 2000-12-10 05:54:27 +0000 | [diff] [blame^] | 155 | totsize++; |
Robert Siemborski | 63b3d7e | 2000-07-04 22:15:39 +0000 | [diff] [blame] | 156 | } |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 157 | |
Chris Allegretta | 09a8084 | 2000-11-30 02:31:13 +0000 | [diff] [blame] | 158 | #ifndef DISABLE_TABCOMP |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 159 | /* |
| 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 | */ |
| 173 | int 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 Allegretta | 09a8084 | 2000-11-30 02:31:13 +0000 | [diff] [blame] | 258 | #endif |