blob: 53e9bddb06f423c34a83ed1d0e1a1b063c4c4f11 [file] [log] [blame]
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00001/**************************************************************************
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 Allegrettaf6b13422000-07-07 04:25:00 +000021#include <unistd.h>
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000022#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 Allegretta4da1fc62000-06-21 03:00:43 +000030#ifndef NANO_SMALL
Chris Allegrettabceb1b22000-06-19 04:22:15 +000031#include <libintl.h>
32#define _(string) gettext(string)
33#else
34#define _(string) (string)
35#endif
36
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000037/* Lower case a string - must be null terminated */
38void 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 */
50char *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
82char *strstrwrapper(char *haystack, char *needle)
83{
Chris Allegrettaf6b13422000-07-07 04:25:00 +000084#ifdef _POSIX_VERSION
Chris Allegretta9fc8d432000-07-07 01:49:52 +000085 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 Allegrettaf6b13422000-07-07 04:25:00 +000091#endif
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000092 if (ISSET(CASE_SENSITIVE))
93 return strstr(haystack, needle);
94 else
95 return strcasestr(haystack, needle);
96}
Chris Allegrettabceb1b22000-06-19 04:22:15 +000097
98/* Thanks BG, many ppl have been asking for this... */
99void *nmalloc(size_t howmuch)
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000100{
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000101 void *r;
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000102
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000103 /* Panic save? */
104
105 if (!(r = malloc(howmuch)))
106 die(_("nano: malloc: out of memory!"));
107
108 return r;
109}
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000110
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000111void *nrealloc(void *ptr, size_t howmuch)
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000112{
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000113 void *r;
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000114
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000115 if (!(r = realloc(ptr, howmuch)))
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000116 die("nano: realloc: out of memory!");
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000117
118 return r;
119}
Robert Siemborski63b3d7e2000-07-04 22:15:39 +0000120
121/* Append a new magic-line to filebot */
122void 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}