Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * utils.c * |
| 3 | * * |
| 4 | * Copyright (C) 1999 Chris Allegretta * |
| 5 | * This program is free software; you can redistribute it and/or modify * |
| 6 | * it under the terms of the GNU General Public License as published by * |
| 7 | * the Free Software Foundation; either version 1, or (at your option) * |
| 8 | * any later version. * |
| 9 | * * |
| 10 | * This program is distributed in the hope that it will be useful, * |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | * GNU General Public License for more details. * |
| 14 | * * |
| 15 | * You should have received a copy of the GNU General Public License * |
| 16 | * along with this program; if not, write to the Free Software * |
| 17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * |
| 18 | * * |
| 19 | **************************************************************************/ |
| 20 | |
Chris Allegretta | f6b1342 | 2000-07-07 04:25:00 +0000 | [diff] [blame] | 21 | #include <unistd.h> |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <ctype.h> |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "nano.h" |
| 28 | #include "proto.h" |
| 29 | |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 30 | #ifndef NANO_SMALL |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 31 | #include <libintl.h> |
| 32 | #define _(string) gettext(string) |
| 33 | #else |
| 34 | #define _(string) (string) |
| 35 | #endif |
| 36 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 37 | /* Lower case a string - must be null terminated */ |
| 38 | void lowercase(char *src) |
| 39 | { |
| 40 | long i = 0; |
| 41 | |
| 42 | while (src[i] != 0) { |
| 43 | src[i] = (char) tolower(src[i]); |
| 44 | i++; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | |
| 49 | /* I can't believe I have to write this function */ |
| 50 | char *strcasestr(char *haystack, char *needle) |
| 51 | { |
| 52 | char *localneedle, *localhaystack, *found, *tmp, *tmp2; |
| 53 | |
| 54 | /* Make a copy of the search string and searcgh space */ |
| 55 | localneedle = nmalloc(strlen(needle) + 2); |
| 56 | localhaystack = nmalloc(strlen(haystack) + 2); |
| 57 | |
| 58 | strcpy(localneedle, needle); |
| 59 | strcpy(localhaystack, haystack); |
| 60 | |
| 61 | /* Make them lowercase */ |
| 62 | lowercase(localneedle); |
| 63 | lowercase(localhaystack); |
| 64 | |
| 65 | /* Look for the lowercased substring in the lowercased search space - |
| 66 | return NULL if we didn't find anything */ |
| 67 | if ((found = strstr(localhaystack, localneedle)) == NULL) { |
| 68 | free(localneedle); |
| 69 | free(localhaystack); |
| 70 | return NULL; |
| 71 | } |
| 72 | /* Else return the pointer to the same place in the real search space */ |
| 73 | tmp2 = haystack; |
| 74 | for (tmp = localhaystack; tmp != found; tmp++) |
| 75 | tmp2++; |
| 76 | |
| 77 | free(localneedle); |
| 78 | free(localhaystack); |
| 79 | return tmp2; |
| 80 | } |
| 81 | |
| 82 | char *strstrwrapper(char *haystack, char *needle) |
| 83 | { |
Chris Allegretta | f6b1342 | 2000-07-07 04:25:00 +0000 | [diff] [blame] | 84 | #ifdef _POSIX_VERSION |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 85 | if (ISSET(USE_REGEXP)) { |
| 86 | int result=regexec(&search_regexp, haystack, 10, regmatches, 0); |
| 87 | if (!result) |
| 88 | return haystack+regmatches[0].rm_so; |
| 89 | return 0; |
| 90 | } |
Chris Allegretta | f6b1342 | 2000-07-07 04:25:00 +0000 | [diff] [blame] | 91 | #endif |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 92 | if (ISSET(CASE_SENSITIVE)) |
| 93 | return strstr(haystack, needle); |
| 94 | else |
| 95 | return strcasestr(haystack, needle); |
| 96 | } |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 97 | |
| 98 | /* Thanks BG, many ppl have been asking for this... */ |
| 99 | void *nmalloc(size_t howmuch) |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 100 | { |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 101 | void *r; |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 102 | |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 103 | /* Panic save? */ |
| 104 | |
| 105 | if (!(r = malloc(howmuch))) |
| 106 | die(_("nano: malloc: out of memory!")); |
| 107 | |
| 108 | return r; |
| 109 | } |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 110 | |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 111 | void *nrealloc(void *ptr, size_t howmuch) |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 112 | { |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 113 | void *r; |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 114 | |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 115 | if (!(r = realloc(ptr, howmuch))) |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 116 | die("nano: realloc: out of memory!"); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 117 | |
| 118 | return r; |
| 119 | } |
Robert Siemborski | 63b3d7e | 2000-07-04 22:15:39 +0000 | [diff] [blame] | 120 | |
| 121 | /* Append a new magic-line to filebot */ |
| 122 | void new_magicline(void) { |
| 123 | filebot->next = nmalloc(sizeof(filestruct)); |
| 124 | filebot->next->data = nmalloc(1); |
| 125 | filebot->next->data[0] = '\0'; |
| 126 | filebot->next->prev = filebot; |
| 127 | filebot->next->next = NULL; |
| 128 | filebot->next->lineno = filebot->lineno + 1; |
| 129 | filebot = filebot->next; |
| 130 | totlines++; |
| 131 | } |