Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1 | /************************************************************************** |
Benno Schulenberg | 514cd9a | 2016-08-29 17:10:49 +0200 | [diff] [blame] | 2 | * utils.c -- This file is part of GNU nano. * |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3 | * * |
Chris Allegretta | 8a07a96 | 2009-12-02 03:36:22 +0000 | [diff] [blame] | 4 | * Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, * |
Benno Schulenberg | 7a9f4a4 | 2014-04-30 20:18:26 +0000 | [diff] [blame] | 5 | * 2008, 2009, 2010, 2011, 2013, 2014 Free Software Foundation, Inc. * |
Benno Schulenberg | 406e524 | 2016-08-29 15:14:18 +0200 | [diff] [blame] | 6 | * Copyright (C) 2016 Benno Schulenberg * |
| 7 | * * |
Benno Schulenberg | 514cd9a | 2016-08-29 17:10:49 +0200 | [diff] [blame] | 8 | * GNU nano is free software: you can redistribute it and/or modify * |
| 9 | * it under the terms of the GNU General Public License as published * |
| 10 | * by the Free Software Foundation, either version 3 of the License, * |
| 11 | * or (at your option) any later version. * |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 12 | * * |
Benno Schulenberg | 514cd9a | 2016-08-29 17:10:49 +0200 | [diff] [blame] | 13 | * GNU nano is distributed in the hope that it will be useful, * |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty * |
| 15 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * |
| 16 | * See the GNU General Public License for more details. * |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 17 | * * |
| 18 | * You should have received a copy of the GNU General Public License * |
Benno Schulenberg | 514cd9a | 2016-08-29 17:10:49 +0200 | [diff] [blame] | 19 | * along with this program. If not, see http://www.gnu.org/licenses/. * |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 20 | * * |
| 21 | **************************************************************************/ |
| 22 | |
David Lawrence Ramsey | 034b994 | 2005-12-08 02:47:10 +0000 | [diff] [blame] | 23 | #include "proto.h" |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 24 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 25 | #include <string.h> |
David Lawrence Ramsey | 49c3f24 | 2004-07-12 16:07:14 +0000 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <unistd.h> |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 28 | #include <pwd.h> |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 29 | #include <ctype.h> |
David Lawrence Ramsey | 49c3f24 | 2004-07-12 16:07:14 +0000 | [diff] [blame] | 30 | #include <errno.h> |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 31 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 32 | /* Return the user's home directory. We use $HOME, and if that fails, |
David Lawrence Ramsey | 6335fb5 | 2007-01-01 05:15:32 +0000 | [diff] [blame] | 33 | * we fall back on the home directory of the effective user ID. */ |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 34 | void get_homedir(void) |
| 35 | { |
| 36 | if (homedir == NULL) { |
| 37 | const char *homenv = getenv("HOME"); |
| 38 | |
Benno Schulenberg | 3a3b176 | 2016-01-22 16:10:36 +0000 | [diff] [blame] | 39 | /* When HOME isn't set, or when we're root, get the home directory |
| 40 | * from the password file instead. */ |
| 41 | if (homenv == NULL || geteuid() == 0) { |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 42 | const struct passwd *userage = getpwuid(geteuid()); |
| 43 | |
| 44 | if (userage != NULL) |
| 45 | homenv = userage->pw_dir; |
| 46 | } |
Benno Schulenberg | e06472a | 2015-06-20 08:21:35 +0000 | [diff] [blame] | 47 | |
| 48 | /* Only set homedir if some home directory could be determined, |
| 49 | * otherwise keep homedir NULL. */ |
| 50 | if (homenv != NULL && strcmp(homenv, "") != 0) |
| 51 | homedir = mallocstrcpy(NULL, homenv); |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
Faissal Bensefia | de95ca6 | 2016-10-20 09:44:29 +0100 | [diff] [blame] | 55 | #ifdef ENABLE_LINENUMBERS |
| 56 | /* Return the number of digits that the given integer n takes up. */ |
David Lawrence Ramsey | bd920b1 | 2016-12-09 11:36:01 -0600 | [diff] [blame] | 57 | int digits(ssize_t n) |
Faissal Bensefia | de95ca6 | 2016-10-20 09:44:29 +0100 | [diff] [blame] | 58 | { |
| 59 | if (n < 100000) { |
| 60 | if (n < 1000) { |
| 61 | if (n < 100) |
| 62 | return 2; |
| 63 | else |
| 64 | return 3; |
| 65 | } else { |
| 66 | if (n < 10000) |
| 67 | return 4; |
| 68 | else |
| 69 | return 5; |
| 70 | } |
| 71 | } else { |
| 72 | if (n < 10000000) { |
| 73 | if (n < 1000000) |
| 74 | return 6; |
| 75 | else |
| 76 | return 7; |
| 77 | } |
| 78 | else { |
| 79 | if (n < 100000000) |
| 80 | return 8; |
| 81 | else |
| 82 | return 9; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | #endif |
| 87 | |
Benno Schulenberg | ea40765 | 2017-01-11 19:15:45 +0100 | [diff] [blame] | 88 | /* Read an integer from str. If it parses okay, store it in *result |
| 89 | * and return TRUE; otherwise, return FALSE. */ |
| 90 | bool parse_num(const char *str, ssize_t *result) |
David Lawrence Ramsey | 49c3f24 | 2004-07-12 16:07:14 +0000 | [diff] [blame] | 91 | { |
| 92 | char *first_error; |
Benno Schulenberg | ea40765 | 2017-01-11 19:15:45 +0100 | [diff] [blame] | 93 | ssize_t value; |
David Lawrence Ramsey | 49c3f24 | 2004-07-12 16:07:14 +0000 | [diff] [blame] | 94 | |
Benno Schulenberg | ea40765 | 2017-01-11 19:15:45 +0100 | [diff] [blame] | 95 | /* The manual page for strtol() says this is required. */ |
Chris Allegretta | b7a2dfb | 2013-01-13 08:37:54 +0000 | [diff] [blame] | 96 | errno = 0; |
| 97 | |
Benno Schulenberg | ea40765 | 2017-01-11 19:15:45 +0100 | [diff] [blame] | 98 | value = (ssize_t)strtol(str, &first_error, 10); |
David Lawrence Ramsey | 32085a8 | 2004-12-08 04:00:26 +0000 | [diff] [blame] | 99 | |
David Lawrence Ramsey | 49c3f24 | 2004-07-12 16:07:14 +0000 | [diff] [blame] | 100 | if (errno == ERANGE || *str == '\0' || *first_error != '\0') |
David Lawrence Ramsey | c53ab2a | 2004-08-04 18:24:53 +0000 | [diff] [blame] | 101 | return FALSE; |
David Lawrence Ramsey | 32085a8 | 2004-12-08 04:00:26 +0000 | [diff] [blame] | 102 | |
Benno Schulenberg | ea40765 | 2017-01-11 19:15:45 +0100 | [diff] [blame] | 103 | *result = value; |
David Lawrence Ramsey | 32085a8 | 2004-12-08 04:00:26 +0000 | [diff] [blame] | 104 | |
David Lawrence Ramsey | c53ab2a | 2004-08-04 18:24:53 +0000 | [diff] [blame] | 105 | return TRUE; |
David Lawrence Ramsey | 49c3f24 | 2004-07-12 16:07:14 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Benno Schulenberg | 7e3a9c3 | 2016-07-04 17:55:25 +0200 | [diff] [blame] | 108 | /* Read two numbers, separated by a comma, from str, and store them in |
| 109 | * *line and *column. Return FALSE on error, and TRUE otherwise. */ |
David Lawrence Ramsey | 2cf6d71 | 2005-06-28 06:25:34 +0000 | [diff] [blame] | 110 | bool parse_line_column(const char *str, ssize_t *line, ssize_t *column) |
David Lawrence Ramsey | b68c01b | 2005-05-16 23:23:15 +0000 | [diff] [blame] | 111 | { |
Benno Schulenberg | ccfb1eb | 2016-07-04 20:51:11 +0200 | [diff] [blame] | 112 | bool retval; |
| 113 | char *firstpart; |
Rishabh Dave | 9c7940b | 2016-07-05 00:57:53 +0530 | [diff] [blame] | 114 | const char *comma; |
| 115 | |
| 116 | while (*str == ' ') |
| 117 | str++; |
| 118 | |
| 119 | comma = strpbrk(str, "m,. /;"); |
David Lawrence Ramsey | b68c01b | 2005-05-16 23:23:15 +0000 | [diff] [blame] | 120 | |
Benno Schulenberg | ccfb1eb | 2016-07-04 20:51:11 +0200 | [diff] [blame] | 121 | if (comma == NULL) |
| 122 | return parse_num(str, line); |
David Lawrence Ramsey | b68c01b | 2005-05-16 23:23:15 +0000 | [diff] [blame] | 123 | |
Benno Schulenberg | 87b2df6 | 2016-12-26 11:45:38 +0100 | [diff] [blame] | 124 | retval = parse_num(comma + 1, column); |
David Lawrence Ramsey | b68c01b | 2005-05-16 23:23:15 +0000 | [diff] [blame] | 125 | |
Benno Schulenberg | ccfb1eb | 2016-07-04 20:51:11 +0200 | [diff] [blame] | 126 | if (comma == str) |
Benno Schulenberg | 87b2df6 | 2016-12-26 11:45:38 +0100 | [diff] [blame] | 127 | return retval; |
David Lawrence Ramsey | b68c01b | 2005-05-16 23:23:15 +0000 | [diff] [blame] | 128 | |
Benno Schulenberg | 62eeda3 | 2016-07-11 20:39:53 +0200 | [diff] [blame] | 129 | firstpart = mallocstrcpy(NULL, str); |
Benno Schulenberg | ccfb1eb | 2016-07-04 20:51:11 +0200 | [diff] [blame] | 130 | firstpart[comma - str] = '\0'; |
David Lawrence Ramsey | c5659d3 | 2005-05-17 00:25:50 +0000 | [diff] [blame] | 131 | |
Benno Schulenberg | e8c7cf2 | 2017-01-17 14:17:42 +0100 | [diff] [blame] | 132 | retval = parse_num(firstpart, line) && retval; |
Benno Schulenberg | 87b2df6 | 2016-12-26 11:45:38 +0100 | [diff] [blame] | 133 | |
Benno Schulenberg | ccfb1eb | 2016-07-04 20:51:11 +0200 | [diff] [blame] | 134 | free(firstpart); |
David Lawrence Ramsey | c5659d3 | 2005-05-17 00:25:50 +0000 | [diff] [blame] | 135 | |
| 136 | return retval; |
David Lawrence Ramsey | b68c01b | 2005-05-16 23:23:15 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Benno Schulenberg | 8c7e4f5 | 2016-12-16 20:34:38 +0100 | [diff] [blame] | 139 | /* Reduce the memory allocation of a string to what is needed. */ |
| 140 | void snuggly_fit(char **str) |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 141 | { |
David Lawrence Ramsey | 6a0d5b8 | 2005-06-13 14:00:22 +0000 | [diff] [blame] | 142 | if (*str != NULL) |
| 143 | *str = charealloc(*str, strlen(*str) + 1); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /* Null a string at a certain index and align it. */ |
| 147 | void null_at(char **data, size_t index) |
| 148 | { |
Chris Allegretta | e1e0fd6 | 2003-04-15 01:15:09 +0000 | [diff] [blame] | 149 | *data = charealloc(*data, index + 1); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 150 | (*data)[index] = '\0'; |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | /* For non-null-terminated lines. A line, by definition, shouldn't |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 154 | * normally have newlines in it, so encode its nulls as newlines. */ |
| 155 | void unsunder(char *str, size_t true_len) |
| 156 | { |
David Lawrence Ramsey | a7c9364 | 2004-02-25 03:19:29 +0000 | [diff] [blame] | 157 | for (; true_len > 0; true_len--, str++) { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 158 | if (*str == '\0') |
| 159 | *str = '\n'; |
David Lawrence Ramsey | a7c9364 | 2004-02-25 03:19:29 +0000 | [diff] [blame] | 160 | } |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | /* For non-null-terminated lines. A line, by definition, shouldn't |
David Lawrence Ramsey | 9d4b8fb | 2006-11-27 05:49:32 +0000 | [diff] [blame] | 164 | * normally have newlines in it, so decode its newlines as nulls. */ |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 165 | void sunder(char *str) |
| 166 | { |
David Lawrence Ramsey | a7c9364 | 2004-02-25 03:19:29 +0000 | [diff] [blame] | 167 | for (; *str != '\0'; str++) { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 168 | if (*str == '\n') |
| 169 | *str = '\0'; |
David Lawrence Ramsey | a7c9364 | 2004-02-25 03:19:29 +0000 | [diff] [blame] | 170 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 171 | } |
| 172 | |
David Lawrence Ramsey | d15c92d | 2005-11-17 18:58:56 +0000 | [diff] [blame] | 173 | /* These functions, ngetline() (originally getline()) and ngetdelim() |
| 174 | * (originally getdelim()), were adapted from GNU mailutils 0.5 |
| 175 | * (mailbox/getline.c). Here is the notice from that file, after |
David Lawrence Ramsey | 13b6115 | 2005-11-30 18:57:05 +0000 | [diff] [blame] | 176 | * converting to the GPL via LGPL clause 3, and with the Free Software |
David Lawrence Ramsey | 84c6845 | 2007-08-10 22:07:14 +0000 | [diff] [blame] | 177 | * Foundation's address and the copyright years updated: |
David Lawrence Ramsey | 3f7c8c5 | 2005-11-14 22:20:35 +0000 | [diff] [blame] | 178 | * |
| 179 | * GNU Mailutils -- a suite of utilities for electronic mail |
David Lawrence Ramsey | d8a1d37 | 2007-10-11 05:01:32 +0000 | [diff] [blame] | 180 | * Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007 |
| 181 | * Free Software Foundation, Inc. |
David Lawrence Ramsey | 3f7c8c5 | 2005-11-14 22:20:35 +0000 | [diff] [blame] | 182 | * |
| 183 | * This library is free software; you can redistribute it and/or |
David Lawrence Ramsey | a1064d4 | 2005-11-14 23:02:03 +0000 | [diff] [blame] | 184 | * modify it under the terms of the GNU General Public License as |
David Lawrence Ramsey | d0035b4 | 2007-08-11 05:17:36 +0000 | [diff] [blame] | 185 | * published by the Free Software Foundation; either version 3 of the |
David Lawrence Ramsey | a1064d4 | 2005-11-14 23:02:03 +0000 | [diff] [blame] | 186 | * License, or (at your option) any later version. |
David Lawrence Ramsey | 3f7c8c5 | 2005-11-14 22:20:35 +0000 | [diff] [blame] | 187 | * |
| 188 | * This library is distributed in the hope that it will be useful, |
| 189 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 190 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
David Lawrence Ramsey | a1064d4 | 2005-11-14 23:02:03 +0000 | [diff] [blame] | 191 | * General Public License for more details. |
David Lawrence Ramsey | 3f7c8c5 | 2005-11-14 22:20:35 +0000 | [diff] [blame] | 192 | * |
David Lawrence Ramsey | a1064d4 | 2005-11-14 23:02:03 +0000 | [diff] [blame] | 193 | * You should have received a copy of the GNU General Public License |
| 194 | * along with this library; if not, write to the Free Software |
David Lawrence Ramsey | 13b6115 | 2005-11-30 18:57:05 +0000 | [diff] [blame] | 195 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 196 | * 02110-1301, USA. */ |
David Lawrence Ramsey | 3f7c8c5 | 2005-11-14 22:20:35 +0000 | [diff] [blame] | 197 | |
Benno Schulenberg | eea0908 | 2014-04-13 20:50:20 +0000 | [diff] [blame] | 198 | #ifndef DISABLE_NANORC |
David Lawrence Ramsey | a27bd65 | 2004-08-17 05:23:38 +0000 | [diff] [blame] | 199 | |
| 200 | #ifndef HAVE_GETDELIM |
David Lawrence Ramsey | 3f7c8c5 | 2005-11-14 22:20:35 +0000 | [diff] [blame] | 201 | /* This function is equivalent to getdelim(). */ |
David Lawrence Ramsey | a27bd65 | 2004-08-17 05:23:38 +0000 | [diff] [blame] | 202 | ssize_t ngetdelim(char **lineptr, size_t *n, int delim, FILE *stream) |
| 203 | { |
David Lawrence Ramsey | a27bd65 | 2004-08-17 05:23:38 +0000 | [diff] [blame] | 204 | size_t indx = 0; |
| 205 | int c; |
| 206 | |
| 207 | /* Sanity checks. */ |
David Lawrence Ramsey | 497a9a2 | 2006-06-04 00:15:56 +0000 | [diff] [blame] | 208 | if (lineptr == NULL || n == NULL || stream == NULL || |
| 209 | fileno(stream) == -1) { |
David Lawrence Ramsey | 76f9485 | 2006-01-09 03:23:29 +0000 | [diff] [blame] | 210 | errno = EINVAL; |
| 211 | return -1; |
| 212 | } |
David Lawrence Ramsey | a27bd65 | 2004-08-17 05:23:38 +0000 | [diff] [blame] | 213 | |
| 214 | /* Allocate the line the first time. */ |
| 215 | if (*lineptr == NULL) { |
David Lawrence Ramsey | 5b2f17e | 2005-04-19 21:47:01 +0000 | [diff] [blame] | 216 | *n = MAX_BUF_SIZE; |
David Lawrence Ramsey | 273c113 | 2007-08-10 17:03:29 +0000 | [diff] [blame] | 217 | *lineptr = charalloc(*n); |
David Lawrence Ramsey | a27bd65 | 2004-08-17 05:23:38 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | while ((c = getc(stream)) != EOF) { |
| 221 | /* Check if more memory is needed. */ |
| 222 | if (indx >= *n) { |
David Lawrence Ramsey | 5b2f17e | 2005-04-19 21:47:01 +0000 | [diff] [blame] | 223 | *n += MAX_BUF_SIZE; |
David Lawrence Ramsey | 273c113 | 2007-08-10 17:03:29 +0000 | [diff] [blame] | 224 | *lineptr = charealloc(*lineptr, *n); |
David Lawrence Ramsey | a27bd65 | 2004-08-17 05:23:38 +0000 | [diff] [blame] | 225 | } |
| 226 | |
David Lawrence Ramsey | 8dec783 | 2005-11-17 04:13:01 +0000 | [diff] [blame] | 227 | /* Put the result in the line. */ |
David Lawrence Ramsey | a27bd65 | 2004-08-17 05:23:38 +0000 | [diff] [blame] | 228 | (*lineptr)[indx++] = (char)c; |
| 229 | |
| 230 | /* Bail out. */ |
| 231 | if (c == delim) |
| 232 | break; |
| 233 | } |
| 234 | |
| 235 | /* Make room for the null character. */ |
| 236 | if (indx >= *n) { |
David Lawrence Ramsey | 5b2f17e | 2005-04-19 21:47:01 +0000 | [diff] [blame] | 237 | *n += MAX_BUF_SIZE; |
David Lawrence Ramsey | 273c113 | 2007-08-10 17:03:29 +0000 | [diff] [blame] | 238 | *lineptr = charealloc(*lineptr, *n); |
David Lawrence Ramsey | a27bd65 | 2004-08-17 05:23:38 +0000 | [diff] [blame] | 239 | } |
| 240 | |
David Lawrence Ramsey | e54f1c4 | 2006-10-29 21:14:53 +0000 | [diff] [blame] | 241 | /* Null-terminate the buffer. */ |
David Lawrence Ramsey | c08368b | 2004-08-17 19:20:05 +0000 | [diff] [blame] | 242 | null_at(lineptr, indx++); |
David Lawrence Ramsey | 35fb55a | 2004-08-17 19:28:54 +0000 | [diff] [blame] | 243 | *n = indx; |
David Lawrence Ramsey | a27bd65 | 2004-08-17 05:23:38 +0000 | [diff] [blame] | 244 | |
David Lawrence Ramsey | 8dec783 | 2005-11-17 04:13:01 +0000 | [diff] [blame] | 245 | /* The last line may not have the delimiter. We have to return what |
| 246 | * we got, and the error will be seen on the next iteration. */ |
David Lawrence Ramsey | a27bd65 | 2004-08-17 05:23:38 +0000 | [diff] [blame] | 247 | return (c == EOF && (indx - 1) == 0) ? -1 : indx - 1; |
| 248 | } |
| 249 | #endif |
Chris Allegretta | ecc245c | 2009-02-01 00:04:42 +0000 | [diff] [blame] | 250 | |
| 251 | #ifndef HAVE_GETLINE |
| 252 | /* This function is equivalent to getline(). */ |
| 253 | ssize_t ngetline(char **lineptr, size_t *n, FILE *stream) |
| 254 | { |
| 255 | return getdelim(lineptr, n, '\n', stream); |
| 256 | } |
| 257 | #endif |
Benno Schulenberg | eea0908 | 2014-04-13 20:50:20 +0000 | [diff] [blame] | 258 | #endif /* !DISABLE_NANORC */ |
David Lawrence Ramsey | a27bd65 | 2004-08-17 05:23:38 +0000 | [diff] [blame] | 259 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 260 | #ifdef HAVE_REGEX_H |
Benno Schulenberg | 8f61511 | 2014-04-14 09:57:06 +0000 | [diff] [blame] | 261 | /* Fix the regex if we're on platforms which require an adjustment |
| 262 | * from GNU-style to BSD-style word boundaries. */ |
Benno Schulenberg | 1de337d | 2014-06-04 16:02:51 +0000 | [diff] [blame] | 263 | const char *fixbounds(const char *r) |
| 264 | { |
Chris Allegretta | 6b83e52 | 2008-08-30 05:16:20 +0000 | [diff] [blame] | 265 | #ifndef GNU_WORDBOUNDS |
| 266 | int i, j = 0; |
| 267 | char *r2 = charalloc(strlen(r) * 5); |
| 268 | char *r3; |
| 269 | |
| 270 | #ifdef DEBUG |
| 271 | fprintf(stderr, "fixbounds(): Start string = \"%s\"\n", r); |
| 272 | #endif |
| 273 | |
| 274 | for (i = 0; i < strlen(r); i++) { |
Benno Schulenberg | 1de337d | 2014-06-04 16:02:51 +0000 | [diff] [blame] | 275 | if (r[i] != '\0' && r[i] == '\\' && (r[i + 1] == '>' || r[i + 1] == '<')) { |
| 276 | strcpy(&r2[j], "[[:"); |
| 277 | r2[j + 3] = r[i + 1]; |
| 278 | strcpy(&r2[j + 4], ":]]"); |
| 279 | i++; |
| 280 | j += 6; |
| 281 | } else |
| 282 | r2[j] = r[i]; |
| 283 | j++; |
Chris Allegretta | 6b83e52 | 2008-08-30 05:16:20 +0000 | [diff] [blame] | 284 | } |
| 285 | r2[j] = '\0'; |
| 286 | r3 = mallocstrcpy(NULL, r2); |
| 287 | free(r2); |
| 288 | #ifdef DEBUG |
| 289 | fprintf(stderr, "fixbounds(): Ending string = \"%s\"\n", r3); |
| 290 | #endif |
| 291 | return (const char *) r3; |
Benno Schulenberg | af70884 | 2015-04-25 14:52:58 +0000 | [diff] [blame] | 292 | #endif /* !GNU_WORDBOUNDS */ |
Chris Allegretta | 6b83e52 | 2008-08-30 05:16:20 +0000 | [diff] [blame] | 293 | |
| 294 | return r; |
| 295 | } |
Benno Schulenberg | af70884 | 2015-04-25 14:52:58 +0000 | [diff] [blame] | 296 | #endif /* HAVE_REGEX_H */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 297 | |
David Lawrence Ramsey | cf9c34a | 2005-11-16 05:59:06 +0000 | [diff] [blame] | 298 | #ifndef DISABLE_SPELLER |
Benno Schulenberg | cc0a3d8 | 2016-05-02 22:20:19 +0200 | [diff] [blame] | 299 | /* Is the word starting at the given position in buf and of the given length |
| 300 | * a separate word? That is: is it not part of a longer word?*/ |
| 301 | bool is_separate_word(size_t position, size_t length, const char *buf) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 302 | { |
Benno Schulenberg | 8db21b6 | 2016-12-26 10:27:42 +0100 | [diff] [blame] | 303 | char before[mb_cur_max()], after[mb_cur_max()]; |
Benno Schulenberg | cc0a3d8 | 2016-05-02 22:20:19 +0200 | [diff] [blame] | 304 | size_t word_end = position + length; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 305 | |
Benno Schulenberg | cc0a3d8 | 2016-05-02 22:20:19 +0200 | [diff] [blame] | 306 | /* Get the characters before and after the word, if any. */ |
| 307 | parse_mbchar(buf + move_mbleft(buf, position), before, NULL); |
| 308 | parse_mbchar(buf + word_end, after, NULL); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 309 | |
Benno Schulenberg | 20058a1 | 2016-08-02 22:09:22 +0200 | [diff] [blame] | 310 | /* If the word starts at the beginning of the line OR the character before |
| 311 | * the word isn't a letter, and if the word ends at the end of the line OR |
| 312 | * the character after the word isn't a letter, we have a whole word. */ |
Benno Schulenberg | 8db21b6 | 2016-12-26 10:27:42 +0100 | [diff] [blame] | 313 | return ((position == 0 || !is_alpha_mbchar(before)) && |
| 314 | (buf[word_end] == '\0' || !is_alpha_mbchar(after))); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 315 | } |
David Lawrence Ramsey | cf9c34a | 2005-11-16 05:59:06 +0000 | [diff] [blame] | 316 | #endif /* !DISABLE_SPELLER */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 317 | |
Benno Schulenberg | 64aa875 | 2017-01-26 16:24:18 +0100 | [diff] [blame] | 318 | /* Return the position of the needle in the haystack, or NULL if not found. |
| 319 | * When searching backwards, we will find the last match that starts no later |
| 320 | * than the given start; otherwise, we find the first match starting no earlier |
| 321 | * than start. If we are doing a regexp search, and we find a match, we fill |
| 322 | * in the global variable regmatches with at most 9 subexpression matches. */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 323 | const char *strstrwrapper(const char *haystack, const char *needle, |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 324 | const char *start) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 325 | { |
Chris Allegretta | 805c26d | 2000-09-06 13:39:17 +0000 | [diff] [blame] | 326 | #ifdef HAVE_REGEX_H |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 327 | if (ISSET(USE_REGEXP)) { |
David Lawrence Ramsey | f3ecffd | 2005-06-16 18:48:30 +0000 | [diff] [blame] | 328 | if (ISSET(BACKWARDS_SEARCH)) { |
Benno Schulenberg | 64aa875 | 2017-01-26 16:24:18 +0100 | [diff] [blame] | 329 | size_t last_find, ceiling, far_end; |
| 330 | size_t floor = 0, next_rung = 0; |
| 331 | /* The start of the search range, and the next start. */ |
Chris Allegretta | 8dbfb5c | 2002-01-28 15:56:15 +0000 | [diff] [blame] | 332 | |
Benno Schulenberg | 64aa875 | 2017-01-26 16:24:18 +0100 | [diff] [blame] | 333 | if (regexec(&search_regexp, haystack, 1, regmatches, 0) != 0) |
| 334 | return NULL; |
| 335 | |
| 336 | far_end = strlen(haystack); |
| 337 | ceiling = start - haystack; |
| 338 | last_find = regmatches[0].rm_so; |
| 339 | |
| 340 | /* A result beyond the search range also means: no match. */ |
| 341 | if (last_find > ceiling) |
| 342 | return NULL; |
| 343 | |
| 344 | /* Move the start-of-range forward until there is no more match; |
| 345 | * then the last match found is the first match backwards. */ |
| 346 | while (regmatches[0].rm_so <= ceiling) { |
| 347 | floor = next_rung; |
| 348 | last_find = regmatches[0].rm_so; |
| 349 | /* If this is the last possible match, don't try to advance. */ |
| 350 | if (last_find == ceiling) |
| 351 | break; |
| 352 | next_rung = move_mbright(haystack, last_find); |
| 353 | regmatches[0].rm_so = next_rung; |
| 354 | regmatches[0].rm_eo = far_end; |
| 355 | if (regexec(&search_regexp, haystack, 1, regmatches, |
| 356 | REG_STARTEND) != 0) |
| 357 | break; |
Chris Allegretta | e4933a3 | 2001-06-13 02:35:44 +0000 | [diff] [blame] | 358 | } |
Chris Allegretta | 9090f2e | 2003-01-26 04:45:05 +0000 | [diff] [blame] | 359 | |
Benno Schulenberg | 64aa875 | 2017-01-26 16:24:18 +0100 | [diff] [blame] | 360 | /* Find the last match again, to get possible submatches. */ |
| 361 | regmatches[0].rm_so = floor; |
| 362 | regmatches[0].rm_eo = far_end; |
| 363 | if (regexec(&search_regexp, haystack, 10, regmatches, |
| 364 | REG_STARTEND) != 0) |
| 365 | statusline(ALERT, "BAD: failed to refind the match!"); |
| 366 | |
| 367 | return haystack + regmatches[0].rm_so; |
Chris Allegretta | 8dbfb5c | 2002-01-28 15:56:15 +0000 | [diff] [blame] | 368 | } |
Benno Schulenberg | 64aa875 | 2017-01-26 16:24:18 +0100 | [diff] [blame] | 369 | |
| 370 | /* Do a forward regex search from the starting point. */ |
| 371 | regmatches[0].rm_so = start - haystack; |
| 372 | regmatches[0].rm_eo = strlen(haystack); |
| 373 | if (regexec(&search_regexp, haystack, 10, regmatches, |
| 374 | REG_STARTEND) != 0) |
| 375 | return NULL; |
| 376 | else |
| 377 | return haystack + regmatches[0].rm_so; |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 378 | } |
Chris Allegretta | 9090f2e | 2003-01-26 04:45:05 +0000 | [diff] [blame] | 379 | #endif /* HAVE_REGEX_H */ |
Chris Allegretta | e10f389 | 2001-10-02 03:54:40 +0000 | [diff] [blame] | 380 | if (ISSET(CASE_SENSITIVE)) { |
David Lawrence Ramsey | f3ecffd | 2005-06-16 18:48:30 +0000 | [diff] [blame] | 381 | if (ISSET(BACKWARDS_SEARCH)) |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 382 | return revstrstr(haystack, needle, start); |
Chris Allegretta | 5f36c37 | 2001-07-16 00:48:53 +0000 | [diff] [blame] | 383 | else |
David Lawrence Ramsey | 2a464cb | 2004-03-07 21:16:18 +0000 | [diff] [blame] | 384 | return strstr(start, needle); |
Benno Schulenberg | 08cd197 | 2016-09-08 21:00:51 +0200 | [diff] [blame] | 385 | } else if (ISSET(BACKWARDS_SEARCH)) |
David Lawrence Ramsey | 345260c | 2005-01-24 01:14:17 +0000 | [diff] [blame] | 386 | return mbrevstrcasestr(haystack, needle, start); |
Benno Schulenberg | 08cd197 | 2016-09-08 21:00:51 +0200 | [diff] [blame] | 387 | |
David Lawrence Ramsey | 42abfe0 | 2005-01-22 18:24:16 +0000 | [diff] [blame] | 388 | return mbstrcasestr(start, needle); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 389 | } |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 390 | |
David Lawrence Ramsey | f70f67b | 2006-06-03 17:31:52 +0000 | [diff] [blame] | 391 | /* This is a wrapper for the perror() function. The wrapper temporarily |
| 392 | * leaves curses mode, calls perror() (which writes to stderr), and then |
| 393 | * reenters curses mode, updating the screen in the process. Note that |
| 394 | * nperror() causes the window to flicker once. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 395 | void nperror(const char *s) |
| 396 | { |
David Lawrence Ramsey | f70f67b | 2006-06-03 17:31:52 +0000 | [diff] [blame] | 397 | endwin(); |
| 398 | perror(s); |
| 399 | doupdate(); |
Chris Allegretta | 77777d4 | 2002-03-29 16:31:29 +0000 | [diff] [blame] | 400 | } |
| 401 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 402 | /* This is a wrapper for the malloc() function that properly handles |
| 403 | * things when we run out of memory. Thanks, BG, many people have been |
| 404 | * asking for this... */ |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 405 | void *nmalloc(size_t howmuch) |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 406 | { |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 407 | void *r = malloc(howmuch); |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 408 | |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 409 | if (r == NULL && howmuch != 0) |
| 410 | die(_("nano is out of memory!")); |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 411 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 412 | return r; |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 413 | } |
| 414 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 415 | /* This is a wrapper for the realloc() function that properly handles |
| 416 | * things when we run out of memory. */ |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 417 | void *nrealloc(void *ptr, size_t howmuch) |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 418 | { |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 419 | void *r = realloc(ptr, howmuch); |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 420 | |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 421 | if (r == NULL && howmuch != 0) |
| 422 | die(_("nano is out of memory!")); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 423 | |
| 424 | return r; |
| 425 | } |
Robert Siemborski | 63b3d7e | 2000-07-04 22:15:39 +0000 | [diff] [blame] | 426 | |
Benno Schulenberg | 06c8062 | 2016-07-11 20:10:10 +0200 | [diff] [blame] | 427 | /* Allocate and copy the first n characters of the given src string, after |
| 428 | * freeing the destination. Usage: "dest = mallocstrncpy(dest, src, n);". */ |
David Lawrence Ramsey | 4a1fc55 | 2004-11-02 15:18:30 +0000 | [diff] [blame] | 429 | char *mallocstrncpy(char *dest, const char *src, size_t n) |
Chris Allegretta | 31925e4 | 2000-11-02 04:40:39 +0000 | [diff] [blame] | 430 | { |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 431 | if (src == NULL) |
David Lawrence Ramsey | 6aec4b8 | 2004-03-15 20:26:30 +0000 | [diff] [blame] | 432 | src = ""; |
| 433 | |
| 434 | if (src != dest) |
| 435 | free(dest); |
Chris Allegretta | 5bf51d3 | 2000-11-16 06:01:10 +0000 | [diff] [blame] | 436 | |
David Lawrence Ramsey | 4a1fc55 | 2004-11-02 15:18:30 +0000 | [diff] [blame] | 437 | dest = charalloc(n); |
David Lawrence Ramsey | 16799ba | 2005-06-21 22:32:50 +0000 | [diff] [blame] | 438 | strncpy(dest, src, n); |
Chris Allegretta | 31925e4 | 2000-11-02 04:40:39 +0000 | [diff] [blame] | 439 | |
| 440 | return dest; |
| 441 | } |
| 442 | |
Benno Schulenberg | 06c8062 | 2016-07-11 20:10:10 +0200 | [diff] [blame] | 443 | /* Free the dest string and return a malloc'ed copy of src. Should be used as: |
David Lawrence Ramsey | 4a1fc55 | 2004-11-02 15:18:30 +0000 | [diff] [blame] | 444 | * "dest = mallocstrcpy(dest, src);". */ |
| 445 | char *mallocstrcpy(char *dest, const char *src) |
| 446 | { |
Benno Schulenberg | f2da466 | 2015-12-04 21:11:10 +0000 | [diff] [blame] | 447 | return mallocstrncpy(dest, src, (src == NULL) ? 1 : strlen(src) + 1); |
David Lawrence Ramsey | 4a1fc55 | 2004-11-02 15:18:30 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Benno Schulenberg | 8b28de1 | 2016-07-13 15:04:40 +0200 | [diff] [blame] | 450 | /* Free the string at dest and return the string at src. */ |
| 451 | char *free_and_assign(char *dest, char *src) |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 452 | { |
| 453 | free(dest); |
| 454 | return src; |
| 455 | } |
| 456 | |
David Lawrence Ramsey | 2b9d6a0 | 2005-11-01 17:45:31 +0000 | [diff] [blame] | 457 | /* nano scrolls horizontally within a line in chunks. Return the column |
| 458 | * number of the first character displayed in the edit window when the |
| 459 | * cursor is at the given column. Note that (0 <= column - |
| 460 | * get_page_start(column) < COLS). */ |
| 461 | size_t get_page_start(size_t column) |
| 462 | { |
Faissal Bensefia | de95ca6 | 2016-10-20 09:44:29 +0100 | [diff] [blame] | 463 | if (column == 0 || column < editwincols - 1) |
David Lawrence Ramsey | 2b9d6a0 | 2005-11-01 17:45:31 +0000 | [diff] [blame] | 464 | return 0; |
Faissal Bensefia | de95ca6 | 2016-10-20 09:44:29 +0100 | [diff] [blame] | 465 | else if (editwincols > 8) |
| 466 | return column - 7 - (column - 7) % (editwincols - 8); |
David Lawrence Ramsey | 2b9d6a0 | 2005-11-01 17:45:31 +0000 | [diff] [blame] | 467 | else |
Faissal Bensefia | de95ca6 | 2016-10-20 09:44:29 +0100 | [diff] [blame] | 468 | return column - (editwincols - 2); |
David Lawrence Ramsey | 2b9d6a0 | 2005-11-01 17:45:31 +0000 | [diff] [blame] | 469 | } |
| 470 | |
David Lawrence Ramsey | b159f94 | 2006-07-28 17:06:27 +0000 | [diff] [blame] | 471 | /* Return the placewewant associated with current_x, i.e. the zero-based |
Benno Schulenberg | dbe3990 | 2016-04-10 17:42:01 +0200 | [diff] [blame] | 472 | * column position of the cursor. */ |
David Lawrence Ramsey | 81c4e18 | 2005-10-31 23:07:58 +0000 | [diff] [blame] | 473 | size_t xplustabs(void) |
| 474 | { |
Benno Schulenberg | dbe3990 | 2016-04-10 17:42:01 +0200 | [diff] [blame] | 475 | return strnlenpt(openfile->current->data, openfile->current_x); |
David Lawrence Ramsey | 81c4e18 | 2005-10-31 23:07:58 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Benno Schulenberg | fbd817f | 2016-05-28 13:23:41 +0200 | [diff] [blame] | 478 | /* Return the index in text of the character that (when displayed) will |
| 479 | * not overshoot the given column. */ |
| 480 | size_t actual_x(const char *text, size_t column) |
David Lawrence Ramsey | 81c4e18 | 2005-10-31 23:07:58 +0000 | [diff] [blame] | 481 | { |
Benno Schulenberg | fbd817f | 2016-05-28 13:23:41 +0200 | [diff] [blame] | 482 | size_t index = 0; |
| 483 | /* The index in text, returned. */ |
| 484 | size_t width = 0; |
| 485 | /* The screen display width to text[index], in columns. */ |
David Lawrence Ramsey | 81c4e18 | 2005-10-31 23:07:58 +0000 | [diff] [blame] | 486 | |
Benno Schulenberg | fbd817f | 2016-05-28 13:23:41 +0200 | [diff] [blame] | 487 | while (*text != '\0') { |
| 488 | int charlen = parse_mbchar(text, NULL, &width); |
David Lawrence Ramsey | 81c4e18 | 2005-10-31 23:07:58 +0000 | [diff] [blame] | 489 | |
Benno Schulenberg | fbd817f | 2016-05-28 13:23:41 +0200 | [diff] [blame] | 490 | if (width > column) |
David Lawrence Ramsey | 81c4e18 | 2005-10-31 23:07:58 +0000 | [diff] [blame] | 491 | break; |
| 492 | |
Benno Schulenberg | fbd817f | 2016-05-28 13:23:41 +0200 | [diff] [blame] | 493 | index += charlen; |
| 494 | text += charlen; |
David Lawrence Ramsey | 81c4e18 | 2005-10-31 23:07:58 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Benno Schulenberg | fbd817f | 2016-05-28 13:23:41 +0200 | [diff] [blame] | 497 | return index; |
David Lawrence Ramsey | 81c4e18 | 2005-10-31 23:07:58 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Benno Schulenberg | 981b414 | 2016-04-24 22:02:48 +0200 | [diff] [blame] | 500 | /* A strnlen() with tabs and multicolumn characters factored in: |
| 501 | * how many columns wide are the first maxlen bytes of text? */ |
| 502 | size_t strnlenpt(const char *text, size_t maxlen) |
David Lawrence Ramsey | 81c4e18 | 2005-10-31 23:07:58 +0000 | [diff] [blame] | 503 | { |
Benno Schulenberg | 981b414 | 2016-04-24 22:02:48 +0200 | [diff] [blame] | 504 | size_t width = 0; |
| 505 | /* The screen display width to text[maxlen]. */ |
David Lawrence Ramsey | 81c4e18 | 2005-10-31 23:07:58 +0000 | [diff] [blame] | 506 | |
| 507 | if (maxlen == 0) |
| 508 | return 0; |
| 509 | |
Benno Schulenberg | 981b414 | 2016-04-24 22:02:48 +0200 | [diff] [blame] | 510 | while (*text != '\0') { |
| 511 | int charlen = parse_mbchar(text, NULL, &width); |
David Lawrence Ramsey | 81c4e18 | 2005-10-31 23:07:58 +0000 | [diff] [blame] | 512 | |
Benno Schulenberg | 981b414 | 2016-04-24 22:02:48 +0200 | [diff] [blame] | 513 | if (maxlen <= charlen) |
David Lawrence Ramsey | 81c4e18 | 2005-10-31 23:07:58 +0000 | [diff] [blame] | 514 | break; |
| 515 | |
Benno Schulenberg | 981b414 | 2016-04-24 22:02:48 +0200 | [diff] [blame] | 516 | maxlen -= charlen; |
Benno Schulenberg | fbd817f | 2016-05-28 13:23:41 +0200 | [diff] [blame] | 517 | text += charlen; |
David Lawrence Ramsey | 81c4e18 | 2005-10-31 23:07:58 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Benno Schulenberg | 981b414 | 2016-04-24 22:02:48 +0200 | [diff] [blame] | 520 | return width; |
David Lawrence Ramsey | 81c4e18 | 2005-10-31 23:07:58 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Benno Schulenberg | 981b414 | 2016-04-24 22:02:48 +0200 | [diff] [blame] | 523 | /* A strlen() with tabs and multicolumn characters factored in: |
| 524 | * how many columns wide is text? */ |
| 525 | size_t strlenpt(const char *text) |
David Lawrence Ramsey | 81c4e18 | 2005-10-31 23:07:58 +0000 | [diff] [blame] | 526 | { |
Benno Schulenberg | 981b414 | 2016-04-24 22:02:48 +0200 | [diff] [blame] | 527 | return strnlenpt(text, (size_t)-1); |
David Lawrence Ramsey | 81c4e18 | 2005-10-31 23:07:58 +0000 | [diff] [blame] | 528 | } |
| 529 | |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 530 | /* Append a new magicline to filebot. */ |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 531 | void new_magicline(void) |
| 532 | { |
David Lawrence Ramsey | 17f5c05 | 2017-02-09 16:00:07 -0600 | [diff] [blame] | 533 | openfile->filebot->next = make_new_node(openfile->filebot); |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 534 | openfile->filebot->next->data = mallocstrcpy(NULL, ""); |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 535 | openfile->filebot = openfile->filebot->next; |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 536 | openfile->totsize++; |
Robert Siemborski | 63b3d7e | 2000-07-04 22:15:39 +0000 | [diff] [blame] | 537 | } |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 538 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 539 | #ifndef NANO_TINY |
David Lawrence Ramsey | 8ae06a8 | 2004-11-07 01:26:39 +0000 | [diff] [blame] | 540 | /* Remove the magicline from filebot, if there is one and it isn't the |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 541 | * only line in the file. Assume that edittop and current are not at |
| 542 | * filebot. */ |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 543 | void remove_magicline(void) |
| 544 | { |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 545 | if (openfile->filebot->data[0] == '\0' && |
Benno Schulenberg | 95f417f | 2016-06-14 11:06:04 +0200 | [diff] [blame] | 546 | openfile->filebot != openfile->fileage) { |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 547 | openfile->filebot = openfile->filebot->prev; |
| 548 | free_filestruct(openfile->filebot->next); |
| 549 | openfile->filebot->next = NULL; |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 550 | openfile->totsize--; |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | |
David Lawrence Ramsey | 93c8405 | 2004-11-23 04:08:28 +0000 | [diff] [blame] | 554 | /* Set top_x and bot_x to the top and bottom x-coordinates of the mark, |
| 555 | * respectively, based on the locations of top and bot. If |
David Lawrence Ramsey | 3305c11 | 2007-08-07 18:37:39 +0000 | [diff] [blame] | 556 | * right_side_up isn't NULL, set it to TRUE if the mark begins with |
David Lawrence Ramsey | 5128de8 | 2005-07-12 17:40:16 +0000 | [diff] [blame] | 557 | * (mark_begin, mark_begin_x) and ends with (current, current_x), or |
David Lawrence Ramsey | 93c8405 | 2004-11-23 04:08:28 +0000 | [diff] [blame] | 558 | * FALSE otherwise. */ |
| 559 | void mark_order(const filestruct **top, size_t *top_x, const filestruct |
| 560 | **bot, size_t *bot_x, bool *right_side_up) |
| 561 | { |
David Lawrence Ramsey | 5128de8 | 2005-07-12 17:40:16 +0000 | [diff] [blame] | 562 | if ((openfile->current->lineno == openfile->mark_begin->lineno && |
Benno Schulenberg | 95f417f | 2016-06-14 11:06:04 +0200 | [diff] [blame] | 563 | openfile->current_x > openfile->mark_begin_x) || |
| 564 | openfile->current->lineno > openfile->mark_begin->lineno) { |
David Lawrence Ramsey | 5128de8 | 2005-07-12 17:40:16 +0000 | [diff] [blame] | 565 | *top = openfile->mark_begin; |
| 566 | *top_x = openfile->mark_begin_x; |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 567 | *bot = openfile->current; |
| 568 | *bot_x = openfile->current_x; |
David Lawrence Ramsey | 93c8405 | 2004-11-23 04:08:28 +0000 | [diff] [blame] | 569 | if (right_side_up != NULL) |
| 570 | *right_side_up = TRUE; |
| 571 | } else { |
David Lawrence Ramsey | 5128de8 | 2005-07-12 17:40:16 +0000 | [diff] [blame] | 572 | *bot = openfile->mark_begin; |
| 573 | *bot_x = openfile->mark_begin_x; |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 574 | *top = openfile->current; |
| 575 | *top_x = openfile->current_x; |
David Lawrence Ramsey | 93c8405 | 2004-11-23 04:08:28 +0000 | [diff] [blame] | 576 | if (right_side_up != NULL) |
| 577 | *right_side_up = FALSE; |
| 578 | } |
| 579 | } |
David Lawrence Ramsey | 93c8405 | 2004-11-23 04:08:28 +0000 | [diff] [blame] | 580 | |
Benno Schulenberg | fe9cf6f | 2016-05-08 10:41:53 +0200 | [diff] [blame] | 581 | /* Given a line number, return a pointer to the corresponding struct. */ |
Chris Allegretta | 14c8620 | 2008-08-03 04:48:05 +0000 | [diff] [blame] | 582 | filestruct *fsfromline(ssize_t lineno) |
| 583 | { |
| 584 | filestruct *f = openfile->current; |
| 585 | |
| 586 | if (lineno <= openfile->current->lineno) |
Benno Schulenberg | fe9cf6f | 2016-05-08 10:41:53 +0200 | [diff] [blame] | 587 | while (f->lineno != lineno && f->prev != NULL) |
| 588 | f = f->prev; |
Chris Allegretta | 14c8620 | 2008-08-03 04:48:05 +0000 | [diff] [blame] | 589 | else |
Benno Schulenberg | fe9cf6f | 2016-05-08 10:41:53 +0200 | [diff] [blame] | 590 | while (f->lineno != lineno && f->next != NULL) |
| 591 | f = f->next; |
Chris Allegretta | 14c8620 | 2008-08-03 04:48:05 +0000 | [diff] [blame] | 592 | |
Benno Schulenberg | 06b449b | 2016-05-08 10:51:40 +0200 | [diff] [blame] | 593 | if (f->lineno != lineno) { |
David Lawrence Ramsey | 84d6f1a | 2016-12-08 13:30:59 -0600 | [diff] [blame] | 594 | statusline(ALERT, _("Internal error: can't match line %ld. " |
| 595 | "Please save your work."), (long)lineno); |
Benno Schulenberg | fe9cf6f | 2016-05-08 10:41:53 +0200 | [diff] [blame] | 596 | return NULL; |
Benno Schulenberg | 06b449b | 2016-05-08 10:51:40 +0200 | [diff] [blame] | 597 | } |
Benno Schulenberg | fe9cf6f | 2016-05-08 10:41:53 +0200 | [diff] [blame] | 598 | |
Chris Allegretta | 14c8620 | 2008-08-03 04:48:05 +0000 | [diff] [blame] | 599 | return f; |
| 600 | } |
Benno Schulenberg | 9fa95a3 | 2016-12-15 13:04:52 +0100 | [diff] [blame] | 601 | #endif /* !NANO_TINY */ |
| 602 | |
| 603 | /* Count the number of characters from begin to end, and return it. */ |
| 604 | size_t get_totsize(const filestruct *begin, const filestruct *end) |
| 605 | { |
| 606 | const filestruct *line; |
| 607 | size_t totsize = 0; |
| 608 | |
| 609 | /* Sum the number of characters (plus a newline) in each line. */ |
| 610 | for (line = begin; line != end->next; line = line->next) |
| 611 | totsize += mbstrlen(line->data) + 1; |
| 612 | |
| 613 | /* The last line of a file doesn't have a newline -- otherwise it |
| 614 | * wouldn't be the last line -- so subtract 1 when at EOF. */ |
| 615 | if (line == NULL) |
| 616 | totsize--; |
| 617 | |
| 618 | return totsize; |
| 619 | } |
Chris Allegretta | 14c8620 | 2008-08-03 04:48:05 +0000 | [diff] [blame] | 620 | |
Chris Allegretta | 599c559 | 2008-08-05 01:35:42 +0000 | [diff] [blame] | 621 | #ifdef DEBUG |
| 622 | /* Dump the filestruct inptr to stderr. */ |
| 623 | void dump_filestruct(const filestruct *inptr) |
| 624 | { |
| 625 | if (inptr == openfile->fileage) |
| 626 | fprintf(stderr, "Dumping file buffer to stderr...\n"); |
| 627 | else if (inptr == cutbuffer) |
| 628 | fprintf(stderr, "Dumping cutbuffer to stderr...\n"); |
| 629 | else |
| 630 | fprintf(stderr, "Dumping a buffer to stderr...\n"); |
| 631 | |
| 632 | while (inptr != NULL) { |
| 633 | fprintf(stderr, "(%ld) %s\n", (long)inptr->lineno, inptr->data); |
| 634 | inptr = inptr->next; |
| 635 | } |
| 636 | } |
Chris Allegretta | 14c8620 | 2008-08-03 04:48:05 +0000 | [diff] [blame] | 637 | |
David Lawrence Ramsey | 00cc565 | 2005-11-02 15:44:01 +0000 | [diff] [blame] | 638 | /* Dump the current buffer's filestruct to stderr in reverse. */ |
| 639 | void dump_filestruct_reverse(void) |
| 640 | { |
| 641 | const filestruct *fileptr = openfile->filebot; |
| 642 | |
| 643 | while (fileptr != NULL) { |
| 644 | fprintf(stderr, "(%ld) %s\n", (long)fileptr->lineno, |
| 645 | fileptr->data); |
| 646 | fileptr = fileptr->prev; |
| 647 | } |
| 648 | } |
| 649 | #endif /* DEBUG */ |