blob: 1628418d8667876f4c3fbe301d0d1b54a94ded4e [file] [log] [blame]
Chris Allegretta11b00112000-08-06 21:13:45 +00001/* $Id$ */
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00002/**************************************************************************
3 * utils.c *
4 * *
Chris Allegrettad757e252003-01-15 19:33:27 +00005 * Copyright (C) 1999-2003 Chris Allegretta *
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00006 * 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 *
Chris Allegretta3a24f3f2001-10-24 11:33:54 +00008 * the Free Software Foundation; either version 2, or (at your option) *
Chris Allegrettaa2ea1932000-06-06 05:53:49 +00009 * 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
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +000022#include "config.h"
23
Chris Allegretta77777d42002-03-29 16:31:29 +000024#include <stdio.h>
Chris Allegrettaf6b13422000-07-07 04:25:00 +000025#include <unistd.h>
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000026#include <stdlib.h>
27#include <string.h>
28#include <ctype.h>
Chris Allegretta6df90f52002-07-19 01:08:59 +000029#include <assert.h>
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000030#include "proto.h"
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +000031#include "nano.h"
Chris Allegrettaa2ea1932000-06-06 05:53:49 +000032
Chris Allegrettad8451932003-03-11 03:50:40 +000033#ifdef BROKEN_REGEXEC
34#undef regexec
35int regexec_safe(const regex_t *preg, const char *string, size_t nmatch,
36 regmatch_t pmatch[], int eflags)
37{
38 if (string != NULL && *string != '\0')
39 return regexec(preg, string, nmatch, pmatch, eflags);
40 return REG_NOMATCH;
41}
42#define regexec(preg, string, nmatch, pmatch, eflags) regexec_safe(preg, string, nmatch, pmatch, eflags)
43#endif
44
Chris Allegrettacf287c82002-07-20 13:57:41 +000045int is_cntrl_char(int c)
46{
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +000047 return (-128 <= c && c < -96) || (0 <= c && c < 32) ||
48 (127 <= c && c < 160);
Chris Allegrettacf287c82002-07-20 13:57:41 +000049}
50
Chris Allegretta48b06702002-02-22 04:30:50 +000051int num_of_digits(int n)
52{
53 int i = 1;
54
55 if (n < 0)
David Lawrence Ramsey0341b582002-08-21 16:10:37 +000056 n = -n;
Chris Allegretta48b06702002-02-22 04:30:50 +000057
58 while (n > 10) {
59 n /= 10;
60 i++;
61 }
62
63 return i;
64}
65
Chris Allegretta6df90f52002-07-19 01:08:59 +000066/* Fix the memory allocation for a string. */
67void align(char **strp)
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +000068{
David Lawrence Ramsey70047ee2003-06-14 20:41:34 +000069 assert(strp != NULL);
70 if (*strp != NULL)
71 *strp = charealloc(*strp, strlen(*strp) + 1);
Chris Allegretta6df90f52002-07-19 01:08:59 +000072}
73
74/* Null a string at a certain index and align it. */
75void null_at(char **data, size_t index)
76{
77 assert(data != NULL);
Chris Allegrettae1e0fd62003-04-15 01:15:09 +000078 *data = charealloc(*data, index + 1);
Chris Allegretta6df90f52002-07-19 01:08:59 +000079 (*data)[index] = '\0';
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +000080}
81
82/* For non-null-terminated lines. A line, by definition, shouldn't
Chris Allegretta6df90f52002-07-19 01:08:59 +000083 * normally have newlines in it, so encode its nulls as newlines. */
84void unsunder(char *str, size_t true_len)
85{
86 assert(str != NULL);
87 for(; true_len > 0; true_len--, str++)
88 if (*str == '\0')
89 *str = '\n';
90}
91
92/* For non-null-terminated lines. A line, by definition, shouldn't
93 * normally have newlines in it, so decode its newlines into nulls. */
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +000094void sunder(char *str)
95{
Chris Allegretta6df90f52002-07-19 01:08:59 +000096 assert(str != NULL);
97 for(; *str != '\0'; str++)
98 if (*str == '\n')
99 *str = '\0';
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000100}
101
Chris Allegretta4f5335d2003-08-04 02:51:12 +0000102#ifndef HAVE_STRCASECMP
103/* This function is equivalent to strcasecmp(). */
104int nstricmp(const char *s1, const char *s2)
105{
106 assert(s1 != NULL && s2 != NULL);
107 for (; *s1 != '\0' && *s2 != '\0'; s1++, s2++) {
108 if (tolower(*s1) != tolower(*s2))
109 break;
110 }
111 return (tolower(*s1) - tolower(*s2));
112}
113#endif
114
115#ifndef HAVE_STRNCASECMP
116/* This function is equivalent to strncasecmp(). */
117int nstrnicmp(const char *s1, const char *s2, size_t n)
118{
119 assert(s1 != NULL && s2 != NULL);
120 for (; n > 0 && *s1 != '\0' && *s2 != '\0'; n--, s1++, s2++) {
121 if (tolower(*s1) != tolower(*s2))
122 break;
123 }
124 if (n > 0)
125 return (tolower(*s1) - tolower(*s2));
126 else if (n == 0)
127 return 0;
128 else if (n < 0)
129 return -1;
130}
131#endif
132
Chris Allegretta5f36c372001-07-16 00:48:53 +0000133/* None of this is needed if we're using NANO_SMALL! */
134#ifndef NANO_SMALL
David Lawrence Ramsey0341b582002-08-21 16:10:37 +0000135const char *revstrstr(const char *haystack, const char *needle,
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000136 const char *rev_start)
Chris Allegrettae4933a32001-06-13 02:35:44 +0000137{
Chris Allegretta6df90f52002-07-19 01:08:59 +0000138 for(; rev_start >= haystack ; rev_start--) {
139 const char *r, *q;
Chris Allegrettae4933a32001-06-13 02:35:44 +0000140
Chris Allegretta6df90f52002-07-19 01:08:59 +0000141 for (r = rev_start, q = needle ; *q == *r && *q != '\0'; r++, q++)
Chris Allegrettae4933a32001-06-13 02:35:44 +0000142 ;
143 if (*q == '\0')
Chris Allegretta6df90f52002-07-19 01:08:59 +0000144 return rev_start;
Chris Allegrettae4933a32001-06-13 02:35:44 +0000145 }
Chris Allegretta6df90f52002-07-19 01:08:59 +0000146 return NULL;
Chris Allegrettae4933a32001-06-13 02:35:44 +0000147}
148
David Lawrence Ramsey0341b582002-08-21 16:10:37 +0000149const char *revstristr(const char *haystack, const char *needle,
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000150 const char *rev_start)
Chris Allegrettae4933a32001-06-13 02:35:44 +0000151{
Chris Allegretta6df90f52002-07-19 01:08:59 +0000152 for (; rev_start >= haystack; rev_start--) {
153 const char *r = rev_start, *q = needle;
Chris Allegrettae4933a32001-06-13 02:35:44 +0000154
Chris Allegretta6df90f52002-07-19 01:08:59 +0000155 for (; (tolower(*q) == tolower(*r)) && (*q != '\0') ; r++, q++)
Chris Allegrettae4933a32001-06-13 02:35:44 +0000156 ;
157 if (*q == '\0')
Chris Allegretta6df90f52002-07-19 01:08:59 +0000158 return rev_start;
Chris Allegrettae4933a32001-06-13 02:35:44 +0000159 }
Chris Allegretta6df90f52002-07-19 01:08:59 +0000160 return NULL;
Chris Allegrettae4933a32001-06-13 02:35:44 +0000161}
Chris Allegretta6df90f52002-07-19 01:08:59 +0000162#endif /* !NANO_SMALL */
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000163
Chris Allegretta99afb852001-05-16 04:20:57 +0000164/* This is now mutt's version (called mutt_stristr) because it doesn't
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +0000165 * use memory allocation to do a simple search (yuck). */
Chris Allegretta6df90f52002-07-19 01:08:59 +0000166const char *stristr(const char *haystack, const char *needle)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000167{
Chris Allegretta99afb852001-05-16 04:20:57 +0000168 const char *p, *q;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000169
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +0000170 if (haystack == NULL)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000171 return NULL;
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +0000172 if (needle == NULL)
173 return haystack;
Chris Allegretta99afb852001-05-16 04:20:57 +0000174
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +0000175 while (*(p = haystack) != '\0') {
176 for (q = needle; *p != 0 && *q != 0 && tolower(*p) == tolower(*q); p++, q++)
Chris Allegretta99afb852001-05-16 04:20:57 +0000177 ;
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +0000178 if (*q == 0)
Chris Allegretta6df90f52002-07-19 01:08:59 +0000179 return haystack;
Chris Allegrettae4933a32001-06-13 02:35:44 +0000180 haystack++;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000181 }
Chris Allegretta99afb852001-05-16 04:20:57 +0000182 return NULL;
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000183}
184
Chris Allegretta9090f2e2003-01-26 04:45:05 +0000185/* If we are searching backwards, we will find the last match
186 * that starts no later than rev_start. If we are doing a regexp search,
187 * then line_pos should be 0 if haystack starts at the beginning of a
188 * line, and positive otherwise. In the regexp case, we fill in the
189 * global variable regmatches with at most 9 subexpression matches. Also,
190 * all .rm_so elements are relative to the start of the whole match, so
191 * regmatches[0].rm_so == 0. */
Chris Allegretta6df90f52002-07-19 01:08:59 +0000192const char *strstrwrapper(const char *haystack, const char *needle,
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000193 const char *rev_start, int line_pos)
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000194{
Chris Allegretta805c26d2000-09-06 13:39:17 +0000195#ifdef HAVE_REGEX_H
Chris Allegretta9fc8d432000-07-07 01:49:52 +0000196 if (ISSET(USE_REGEXP)) {
Chris Allegretta8dbfb5c2002-01-28 15:56:15 +0000197#ifndef NANO_SMALL
Chris Allegretta9090f2e2003-01-26 04:45:05 +0000198 if (ISSET(REVERSE_SEARCH)) {
199 /* When doing a backwards search, haystack is a whole line. */
200 if (!regexec(&search_regexp, haystack, 1, regmatches, 0) &&
201 haystack + regmatches[0].rm_so <= rev_start) {
202 const char *retval = haystack + regmatches[0].rm_so;
Chris Allegretta8dbfb5c2002-01-28 15:56:15 +0000203
Chris Allegretta9090f2e2003-01-26 04:45:05 +0000204 /* Search forward until there is no more match. */
205 while (!regexec(&search_regexp, retval + 1, 1, regmatches,
206 REG_NOTBOL) &&
207 retval + 1 + regmatches[0].rm_so <= rev_start)
208 retval += 1 + regmatches[0].rm_so;
209 /* Finally, put the subexpression matches in global
210 * variable regmatches. The REG_NOTBOL flag doesn't
211 * matter now. */
212 regexec(&search_regexp, retval, 10, regmatches, 0);
213 return retval;
Chris Allegrettae4933a32001-06-13 02:35:44 +0000214 }
Chris Allegretta9090f2e2003-01-26 04:45:05 +0000215 } else
216#endif /* !NANO_SMALL */
217 if (!regexec(&search_regexp, haystack, 10, regmatches,
218 line_pos > 0 ? REG_NOTBOL : 0)) {
219 const char *retval = haystack + regmatches[0].rm_so;
220
221 regexec(&search_regexp, retval, 10, regmatches, 0);
222 return retval;
Chris Allegretta8dbfb5c2002-01-28 15:56:15 +0000223 }
Chris Allegretta9090f2e2003-01-26 04:45:05 +0000224 return NULL;
Chris Allegrettabd9e7c32000-10-26 01:44:42 +0000225 }
Chris Allegretta9090f2e2003-01-26 04:45:05 +0000226#endif /* HAVE_REGEX_H */
Chris Allegretta5f36c372001-07-16 00:48:53 +0000227#ifndef NANO_SMALL
Chris Allegrettae10f3892001-10-02 03:54:40 +0000228 if (ISSET(CASE_SENSITIVE)) {
Chris Allegretta5f36c372001-07-16 00:48:53 +0000229 if (ISSET(REVERSE_SEARCH))
Chris Allegrettae4933a32001-06-13 02:35:44 +0000230 return revstrstr(haystack, needle, rev_start);
Chris Allegretta5f36c372001-07-16 00:48:53 +0000231 else
Chris Allegretta9090f2e2003-01-26 04:45:05 +0000232 return strstr(haystack, needle);
233 } else if (ISSET(REVERSE_SEARCH))
234 return revstristr(haystack, needle, rev_start);
Chris Allegretta5f36c372001-07-16 00:48:53 +0000235#endif
Chris Allegretta9090f2e2003-01-26 04:45:05 +0000236 return stristr(haystack, needle);
Chris Allegrettaa2ea1932000-06-06 05:53:49 +0000237}
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000238
Chris Allegretta77777d42002-03-29 16:31:29 +0000239/* This is a wrapper for the perror function. The wrapper takes care of
240 * ncurses, calls perror (which writes to STDERR), then refreshes the
Chris Allegretta6df90f52002-07-19 01:08:59 +0000241 * screen. Note that nperror causes the window to flicker once. */
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +0000242void nperror(const char *s)
243{
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +0000244 /* leave ncurses mode, go to the terminal */
Chris Allegretta77777d42002-03-29 16:31:29 +0000245 if (endwin() != ERR) {
246 perror(s); /* print the error */
247 total_refresh(); /* return to ncurses and repaint */
248 }
249}
250
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000251/* Thanks BG, many ppl have been asking for this... */
252void *nmalloc(size_t howmuch)
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000253{
Chris Allegretta7662c862003-01-13 01:35:15 +0000254 void *r = malloc(howmuch);
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000255
Chris Allegretta7662c862003-01-13 01:35:15 +0000256 if (r == NULL && howmuch != 0)
257 die(_("nano is out of memory!"));
Chris Allegretta8d8e0122001-04-18 04:28:54 +0000258
Chris Allegretta6df90f52002-07-19 01:08:59 +0000259 return r;
Chris Allegretta8d8e0122001-04-18 04:28:54 +0000260}
261
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000262void *nrealloc(void *ptr, size_t howmuch)
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000263{
Chris Allegretta7662c862003-01-13 01:35:15 +0000264 void *r = realloc(ptr, howmuch);
Chris Allegretta4da1fc62000-06-21 03:00:43 +0000265
Chris Allegretta7662c862003-01-13 01:35:15 +0000266 if (r == NULL && howmuch != 0)
267 die(_("nano is out of memory!"));
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000268
269 return r;
270}
Robert Siemborski63b3d7e2000-07-04 22:15:39 +0000271
Chris Allegretta6df90f52002-07-19 01:08:59 +0000272/* Copy one malloc()ed string to another pointer. Should be used as:
273 * dest = mallocstrcpy(dest, src); */
274char *mallocstrcpy(char *dest, const char *src)
Chris Allegretta31925e42000-11-02 04:40:39 +0000275{
Chris Allegrettae1ebaf32001-01-07 05:50:36 +0000276 if (src == dest)
Chris Allegretta6df90f52002-07-19 01:08:59 +0000277 return dest;
Chris Allegrettae1ebaf32001-01-07 05:50:36 +0000278
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +0000279 if (dest != NULL)
Chris Allegretta31925e42000-11-02 04:40:39 +0000280 free(dest);
281
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +0000282 if (src == NULL)
Chris Allegretta6df90f52002-07-19 01:08:59 +0000283 return NULL;
Chris Allegretta5bf51d32000-11-16 06:01:10 +0000284
Chris Allegretta88b09152001-05-17 11:35:43 +0000285 dest = charalloc(strlen(src) + 1);
Chris Allegretta31925e42000-11-02 04:40:39 +0000286 strcpy(dest, src);
287
288 return dest;
289}
290
Chris Allegretta6df90f52002-07-19 01:08:59 +0000291/* Append a new magic-line to filebot. */
Chris Allegrettabd9e7c32000-10-26 01:44:42 +0000292void new_magicline(void)
293{
David Lawrence Ramsey70047ee2003-06-14 20:41:34 +0000294 filebot->next = (filestruct *)nmalloc(sizeof(filestruct));
Chris Allegretta88b09152001-05-17 11:35:43 +0000295 filebot->next->data = charalloc(1);
Robert Siemborski63b3d7e2000-07-04 22:15:39 +0000296 filebot->next->data[0] = '\0';
297 filebot->next->prev = filebot;
298 filebot->next->next = NULL;
299 filebot->next->lineno = filebot->lineno + 1;
300 filebot = filebot->next;
301 totlines++;
Chris Allegrettae51c95f2000-12-10 05:54:27 +0000302 totsize++;
Robert Siemborski63b3d7e2000-07-04 22:15:39 +0000303}
Chris Allegretta04d848e2000-11-05 17:54:41 +0000304
David Lawrence Ramsey5ffbec52003-09-16 01:16:49 +0000305#ifndef NANO_SMALL
306/* Set top_x and bot_x to the top and bottom x-coordinates of the mark,
307 * respectively, based on the locations of top and bot. */
308void mark_order(const filestruct **top, size_t *top_x,
309 const filestruct **bot, size_t *bot_x)
310{
311 assert(top != NULL && top_x != NULL && bot != NULL && bot_x != NULL);
312 if ((current->lineno == mark_beginbuf->lineno && current_x > mark_beginx)
313 || current->lineno > mark_beginbuf->lineno) {
314 *top = mark_beginbuf;
315 *top_x = mark_beginx;
316 *bot = current;
317 *bot_x = current_x;
318 } else {
319 *bot = mark_beginbuf;
320 *bot_x = mark_beginx;
321 *top = current;
322 *top_x = current_x;
323 }
324}
325#endif
326
Rocco Corsi06aca1c2001-01-11 05:30:31 +0000327#ifndef DISABLE_TABCOMP
Chris Allegretta04d848e2000-11-05 17:54:41 +0000328/*
329 * Routine to see if a text string is matched by a wildcard pattern.
330 * Returns TRUE if the text is matched, or FALSE if it is not matched
331 * or if the pattern is invalid.
332 * * matches zero or more characters
333 * ? matches a single character
334 * [abc] matches 'a', 'b' or 'c'
335 * \c quotes character c
336 * Adapted from code written by Ingo Wilken, and
337 * then taken from sash, Copyright (c) 1999 by David I. Bell
338 * Permission is granted to use, distribute, or modify this source,
339 * provided that this copyright notice remains intact.
340 * Permission to distribute this code under the GPL has been granted.
341 */
342int check_wildcard_match(const char *text, const char *pattern)
343{
Chris Allegretta6df90f52002-07-19 01:08:59 +0000344 const char *retrypat;
345 const char *retrytext;
Chris Allegretta04d848e2000-11-05 17:54:41 +0000346 int ch;
347 int found;
348 int len;
349
Chris Allegretta6df90f52002-07-19 01:08:59 +0000350 retrypat = NULL;
351 retrytext = NULL;
Chris Allegretta04d848e2000-11-05 17:54:41 +0000352
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +0000353 while (*text != '\0' || *pattern != '\0') {
Chris Allegretta04d848e2000-11-05 17:54:41 +0000354 ch = *pattern++;
355
356 switch (ch) {
357 case '*':
Chris Allegretta6df90f52002-07-19 01:08:59 +0000358 retrypat = pattern;
359 retrytext = text;
Chris Allegretta04d848e2000-11-05 17:54:41 +0000360 break;
361
362 case '[':
363 found = FALSE;
364
365 while ((ch = *pattern++) != ']') {
366 if (ch == '\\')
367 ch = *pattern++;
368
369 if (ch == '\0')
370 return FALSE;
371
372 if (*text == ch)
373 found = TRUE;
374 }
375 len = strlen(text);
376 if (found == FALSE && len != 0) {
377 return FALSE;
378 }
379 if (found == TRUE) {
380 if (strlen(pattern) == 0 && len == 1) {
381 return TRUE;
382 }
383 if (len != 0) {
384 text++;
385 continue;
386 }
387 }
388
389 /* fall into next case */
390
391 case '?':
392 if (*text++ == '\0')
393 return FALSE;
394
395 break;
396
397 case '\\':
398 ch = *pattern++;
399
400 if (ch == '\0')
401 return FALSE;
402
403 /* fall into next case */
404
405 default:
406 if (*text == ch) {
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +0000407 if (*text != '\0')
Chris Allegretta04d848e2000-11-05 17:54:41 +0000408 text++;
409 break;
410 }
411
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +0000412 if (*text != '\0') {
Chris Allegretta6df90f52002-07-19 01:08:59 +0000413 pattern = retrypat;
414 text = ++retrytext;
Chris Allegretta04d848e2000-11-05 17:54:41 +0000415 break;
416 }
417
418 return FALSE;
419 }
420
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +0000421 if (pattern == NULL)
Chris Allegretta04d848e2000-11-05 17:54:41 +0000422 return FALSE;
423 }
424
425 return TRUE;
426}
Chris Allegretta09a80842000-11-30 02:31:13 +0000427#endif