Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 1 | /************************************************************************** |
Benno Schulenberg | 514cd9a | 2016-08-29 17:10:49 +0200 | [diff] [blame] | 2 | * rcfile.c -- This file is part of GNU nano. * |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 3 | * * |
Benno Schulenberg | 7a9f4a4 | 2014-04-30 20:18:26 +0000 | [diff] [blame] | 4 | * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, * |
| 5 | * 2010, 2011, 2013, 2014 Free Software Foundation, Inc. * |
Benno Schulenberg | 406e524 | 2016-08-29 15:14:18 +0200 | [diff] [blame] | 6 | * Copyright (C) 2014 Mike Frysinger * |
| 7 | * Copyright (C) 2014, 2015, 2016 Benno Schulenberg * |
| 8 | * * |
Benno Schulenberg | 514cd9a | 2016-08-29 17:10:49 +0200 | [diff] [blame] | 9 | * GNU nano is free software: you can redistribute it and/or modify * |
| 10 | * it under the terms of the GNU General Public License as published * |
| 11 | * by the Free Software Foundation, either version 3 of the License, * |
| 12 | * or (at your option) any later version. * |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 13 | * * |
Benno Schulenberg | 514cd9a | 2016-08-29 17:10:49 +0200 | [diff] [blame] | 14 | * GNU nano is distributed in the hope that it will be useful, * |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty * |
| 16 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * |
| 17 | * See the GNU General Public License for more details. * |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 18 | * * |
| 19 | * You should have received a copy of the GNU General Public License * |
Benno Schulenberg | 514cd9a | 2016-08-29 17:10:49 +0200 | [diff] [blame] | 20 | * along with this program. If not, see http://www.gnu.org/licenses/. * |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 21 | * * |
| 22 | **************************************************************************/ |
| 23 | |
David Lawrence Ramsey | 034b994 | 2005-12-08 02:47:10 +0000 | [diff] [blame] | 24 | #include "proto.h" |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 25 | |
Benno Schulenberg | 3814283 | 2014-03-30 20:37:40 +0000 | [diff] [blame] | 26 | #include <glob.h> |
Chris Allegretta | 34f8098 | 2002-01-22 20:09:20 +0000 | [diff] [blame] | 27 | #include <stdarg.h> |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 28 | #include <string.h> |
| 29 | #include <stdio.h> |
| 30 | #include <errno.h> |
Chris Allegretta | 4dc03d5 | 2002-05-11 03:04:44 +0000 | [diff] [blame] | 31 | #include <unistd.h> |
David Lawrence Ramsey | 9830d75 | 2004-05-13 17:19:54 +0000 | [diff] [blame] | 32 | #include <ctype.h> |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 33 | |
Benno Schulenberg | eea0908 | 2014-04-13 20:50:20 +0000 | [diff] [blame] | 34 | #ifndef DISABLE_NANORC |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 35 | |
Benno Schulenberg | 77023a7 | 2016-11-27 16:34:34 +0100 | [diff] [blame] | 36 | #ifndef RCFILE_NAME |
| 37 | #define RCFILE_NAME ".nanorc" |
| 38 | #endif |
| 39 | |
David Lawrence Ramsey | 6835102 | 2006-08-21 21:37:39 +0000 | [diff] [blame] | 40 | static const rcoption rcopts[] = { |
David Lawrence Ramsey | 4d72de7 | 2006-04-12 15:27:40 +0000 | [diff] [blame] | 41 | {"boldtext", BOLD_TEXT}, |
Faissal Bensefia | de95ca6 | 2016-10-20 09:44:29 +0100 | [diff] [blame] | 42 | #ifdef ENABLE_LINENUMBERS |
| 43 | {"linenumbers", LINE_NUMBERS}, |
| 44 | #endif |
David Lawrence Ramsey | 2c62b07 | 2004-05-29 16:38:57 +0000 | [diff] [blame] | 45 | #ifndef DISABLE_JUSTIFY |
| 46 | {"brackets", 0}, |
| 47 | #endif |
Benno Schulenberg | 7952615 | 2015-07-15 19:40:37 +0000 | [diff] [blame] | 48 | {"const", CONST_UPDATE}, /* deprecated form, remove in 2018 */ |
| 49 | {"constantshow", CONST_UPDATE}, |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 50 | #ifndef DISABLE_WRAPJUSTIFY |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 51 | {"fill", 0}, |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 52 | #endif |
Benno Schulenberg | 7c2f53b | 2015-06-18 18:51:27 +0000 | [diff] [blame] | 53 | #ifndef DISABLE_HISTORIES |
| 54 | {"historylog", HISTORYLOG}, |
Chris Allegretta | bf88d27 | 2013-01-01 03:24:39 +0000 | [diff] [blame] | 55 | #endif |
Benno Schulenberg | 7c2f53b | 2015-06-18 18:51:27 +0000 | [diff] [blame] | 56 | {"morespace", MORE_SPACE}, |
David Lawrence Ramsey | f5b256b | 2003-10-03 20:26:25 +0000 | [diff] [blame] | 57 | #ifndef DISABLE_MOUSE |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 58 | {"mouse", USE_MOUSE}, |
| 59 | #endif |
Benno Schulenberg | 0636d7b | 2014-04-03 20:23:07 +0000 | [diff] [blame] | 60 | #ifndef DISABLE_MULTIBUFFER |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 61 | {"multibuffer", MULTIBUFFER}, |
| 62 | #endif |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 63 | {"nohelp", NO_HELP}, |
David Lawrence Ramsey | a0168ca | 2005-11-05 17:35:44 +0000 | [diff] [blame] | 64 | {"nonewlines", NO_NEWLINES}, |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 65 | #ifndef DISABLE_WRAPPING |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 66 | {"nowrap", NO_WRAP}, |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 67 | #endif |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 68 | #ifndef DISABLE_OPERATINGDIR |
| 69 | {"operatingdir", 0}, |
| 70 | #endif |
Benno Schulenberg | 7c2f53b | 2015-06-18 18:51:27 +0000 | [diff] [blame] | 71 | #ifndef DISABLE_HISTORIES |
Benno Schulenberg | cadb4f3 | 2015-07-15 19:50:55 +0000 | [diff] [blame] | 72 | {"poslog", POS_HISTORY}, /* deprecated form, remove in 2018 */ |
| 73 | {"positionlog", POS_HISTORY}, |
Benno Schulenberg | 7c2f53b | 2015-06-18 18:51:27 +0000 | [diff] [blame] | 74 | #endif |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 75 | {"preserve", PRESERVE}, |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 76 | #ifndef DISABLE_JUSTIFY |
David Lawrence Ramsey | 2c62b07 | 2004-05-29 16:38:57 +0000 | [diff] [blame] | 77 | {"punct", 0}, |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 78 | {"quotestr", 0}, |
| 79 | #endif |
David Lawrence Ramsey | f38230a | 2004-04-21 22:25:16 +0000 | [diff] [blame] | 80 | {"rebinddelete", REBIND_DELETE}, |
David Lawrence Ramsey | 057edf7 | 2005-08-10 21:22:15 +0000 | [diff] [blame] | 81 | {"rebindkeypad", REBIND_KEYPAD}, |
David Lawrence Ramsey | f3ecffd | 2005-06-16 18:48:30 +0000 | [diff] [blame] | 82 | #ifdef HAVE_REGEX_H |
| 83 | {"regexp", USE_REGEXP}, |
| 84 | #endif |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 85 | #ifndef DISABLE_SPELLER |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 86 | {"speller", 0}, |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 87 | #endif |
| 88 | {"suspend", SUSPEND}, |
| 89 | {"tabsize", 0}, |
David Lawrence Ramsey | edab0cc | 2004-07-03 03:09:12 +0000 | [diff] [blame] | 90 | {"tempfile", TEMP_FILE}, |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 91 | {"view", VIEW_MODE}, |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 92 | #ifndef NANO_TINY |
Benno Schulenberg | 7c2f53b | 2015-06-18 18:51:27 +0000 | [diff] [blame] | 93 | {"allow_insecure_backup", INSECURE_BACKUP}, |
David Lawrence Ramsey | 6399000 | 2005-06-17 19:01:00 +0000 | [diff] [blame] | 94 | {"autoindent", AUTOINDENT}, |
| 95 | {"backup", BACKUP_FILE}, |
| 96 | {"backupdir", 0}, |
| 97 | {"backwards", BACKWARDS_SEARCH}, |
| 98 | {"casesensitive", CASE_SENSITIVE}, |
| 99 | {"cut", CUT_TO_END}, |
Chris Allegretta | 9f98333 | 2016-02-25 21:04:45 +0000 | [diff] [blame] | 100 | {"justifytrim", JUSTIFY_TRIM}, |
Benno Schulenberg | 7c2f53b | 2015-06-18 18:51:27 +0000 | [diff] [blame] | 101 | {"locking", LOCKING}, |
David Lawrence Ramsey | d89617f | 2006-01-06 21:51:10 +0000 | [diff] [blame] | 102 | {"matchbrackets", 0}, |
David Lawrence Ramsey | 6399000 | 2005-06-17 19:01:00 +0000 | [diff] [blame] | 103 | {"noconvert", NO_CONVERT}, |
David Lawrence Ramsey | e29111f | 2005-06-17 19:06:25 +0000 | [diff] [blame] | 104 | {"quickblank", QUICK_BLANK}, |
Benno Schulenberg | 7c2f53b | 2015-06-18 18:51:27 +0000 | [diff] [blame] | 105 | {"quiet", QUIET}, |
Benno Schulenberg | b92d35d | 2016-09-11 09:41:09 +0200 | [diff] [blame] | 106 | {"showcursor", SHOW_CURSOR}, |
David Lawrence Ramsey | 6399000 | 2005-06-17 19:01:00 +0000 | [diff] [blame] | 107 | {"smarthome", SMART_HOME}, |
| 108 | {"smooth", SMOOTH_SCROLL}, |
Benno Schulenberg | 4499520 | 2015-06-20 18:48:43 +0000 | [diff] [blame] | 109 | {"softwrap", SOFTWRAP}, |
David Lawrence Ramsey | 6399000 | 2005-06-17 19:01:00 +0000 | [diff] [blame] | 110 | {"tabstospaces", TABS_TO_SPACES}, |
Benno Schulenberg | eac0446 | 2015-08-09 16:31:01 +0000 | [diff] [blame] | 111 | {"unix", MAKE_IT_UNIX}, |
David Lawrence Ramsey | 483ea32 | 2004-05-29 16:25:30 +0000 | [diff] [blame] | 112 | {"whitespace", 0}, |
David Lawrence Ramsey | 4f03daf | 2005-08-10 22:12:28 +0000 | [diff] [blame] | 113 | {"wordbounds", WORD_BOUNDS}, |
Benno Schulenberg | 6f12992 | 2016-06-30 18:02:45 +0200 | [diff] [blame] | 114 | {"wordchars", 0}, |
David Lawrence Ramsey | 483ea32 | 2004-05-29 16:25:30 +0000 | [diff] [blame] | 115 | #endif |
Benno Schulenberg | 1663994 | 2014-05-03 18:24:45 +0000 | [diff] [blame] | 116 | #ifndef DISABLE_COLOR |
| 117 | {"titlecolor", 0}, |
Benno Schulenberg | de2aa4f | 2016-10-20 10:07:48 +0200 | [diff] [blame] | 118 | {"numbercolor", 0}, |
Benno Schulenberg | 1663994 | 2014-05-03 18:24:45 +0000 | [diff] [blame] | 119 | {"statuscolor", 0}, |
| 120 | {"keycolor", 0}, |
| 121 | {"functioncolor", 0}, |
| 122 | #endif |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 123 | {NULL, 0} |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 124 | }; |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 125 | |
David Lawrence Ramsey | a6d26d0 | 2004-07-30 22:52:44 +0000 | [diff] [blame] | 126 | static bool errors = FALSE; |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 127 | /* Whether we got any errors while parsing an rcfile. */ |
David Lawrence Ramsey | 2cf6d71 | 2005-06-28 06:25:34 +0000 | [diff] [blame] | 128 | static size_t lineno = 0; |
David Lawrence Ramsey | 6335fb5 | 2007-01-01 05:15:32 +0000 | [diff] [blame] | 129 | /* If we did, the line number where the last error occurred. */ |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 130 | static char *nanorc = NULL; |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 131 | /* The path to the rcfile we're parsing. */ |
Benno Schulenberg | 0038992 | 2014-04-04 11:59:03 +0000 | [diff] [blame] | 132 | #ifndef DISABLE_COLOR |
Benno Schulenberg | 275e9f0 | 2016-02-28 20:38:14 +0000 | [diff] [blame] | 133 | static bool opensyntax = FALSE; |
| 134 | /* Whether we're allowed to add to the last syntax. When a file ends, |
| 135 | * or when a new syntax command is seen, this bool becomes FALSE. */ |
Benno Schulenberg | 8a5ae21 | 2016-03-10 20:36:12 +0000 | [diff] [blame] | 136 | static syntaxtype *live_syntax; |
Benno Schulenberg | 04262f0 | 2016-03-10 20:06:01 +0000 | [diff] [blame] | 137 | /* The syntax that is currently being parsed. */ |
Benno Schulenberg | 8fbb922 | 2016-03-13 20:05:36 +0000 | [diff] [blame] | 138 | static colortype *lastcolor = NULL; |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 139 | /* The end of the color list for the current syntax. */ |
| 140 | #endif |
Chris Allegretta | f478f83 | 2002-01-18 21:54:35 +0000 | [diff] [blame] | 141 | |
David Lawrence Ramsey | 6835102 | 2006-08-21 21:37:39 +0000 | [diff] [blame] | 142 | /* We have an error in some part of the rcfile. Print the error message |
| 143 | * on stderr, and then make the user hit Enter to continue starting |
| 144 | * nano. */ |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 145 | void rcfile_error(const char *msg, ...) |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 146 | { |
| 147 | va_list ap; |
| 148 | |
Chris Allegretta | a30eb78 | 2009-02-09 04:03:20 +0000 | [diff] [blame] | 149 | if (ISSET(QUIET)) |
| 150 | return; |
| 151 | |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 152 | fprintf(stderr, "\n"); |
David Lawrence Ramsey | a6d26d0 | 2004-07-30 22:52:44 +0000 | [diff] [blame] | 153 | if (lineno > 0) { |
| 154 | errors = TRUE; |
David Lawrence Ramsey | e796a7b | 2005-08-01 05:54:11 +0000 | [diff] [blame] | 155 | fprintf(stderr, _("Error in %s on line %lu: "), nanorc, (unsigned long)lineno); |
David Lawrence Ramsey | a6d26d0 | 2004-07-30 22:52:44 +0000 | [diff] [blame] | 156 | } |
Chris Allegretta | 78f0fc6 | 2002-03-29 19:41:57 +0000 | [diff] [blame] | 157 | |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 158 | va_start(ap, msg); |
David Lawrence Ramsey | 576bf33 | 2004-07-12 03:10:30 +0000 | [diff] [blame] | 159 | vfprintf(stderr, _(msg), ap); |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 160 | va_end(ap); |
David Lawrence Ramsey | 6420d44 | 2004-08-11 05:13:08 +0000 | [diff] [blame] | 161 | |
| 162 | fprintf(stderr, "\n"); |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 163 | } |
Benno Schulenberg | b341f29 | 2014-06-19 20:05:24 +0000 | [diff] [blame] | 164 | #endif /* !DISABLE_NANORC */ |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 165 | |
Benno Schulenberg | b341f29 | 2014-06-19 20:05:24 +0000 | [diff] [blame] | 166 | #if !defined(DISABLE_NANORC) || !defined(DISABLE_HISTORIES) |
David Lawrence Ramsey | 6835102 | 2006-08-21 21:37:39 +0000 | [diff] [blame] | 167 | /* Parse the next word from the string, null-terminate it, and return |
| 168 | * a pointer to the first character after the null terminator. The |
| 169 | * returned pointer will point to '\0' if we hit the end of the line. */ |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 170 | char *parse_next_word(char *ptr) |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 171 | { |
David Lawrence Ramsey | d864048 | 2005-06-12 17:48:46 +0000 | [diff] [blame] | 172 | while (!isblank(*ptr) && *ptr != '\0') |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 173 | ptr++; |
| 174 | |
Chris Allegretta | 13fd44b | 2002-01-02 13:59:11 +0000 | [diff] [blame] | 175 | if (*ptr == '\0') |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 176 | return ptr; |
Chris Allegretta | f478f83 | 2002-01-18 21:54:35 +0000 | [diff] [blame] | 177 | |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 178 | /* Null-terminate and advance ptr. */ |
| 179 | *ptr++ = '\0'; |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 180 | |
David Lawrence Ramsey | d864048 | 2005-06-12 17:48:46 +0000 | [diff] [blame] | 181 | while (isblank(*ptr)) |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 182 | ptr++; |
| 183 | |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 184 | return ptr; |
| 185 | } |
Benno Schulenberg | b341f29 | 2014-06-19 20:05:24 +0000 | [diff] [blame] | 186 | #endif /* !DISABLE_NANORC || !DISABLE_HISTORIES */ |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 187 | |
Benno Schulenberg | b341f29 | 2014-06-19 20:05:24 +0000 | [diff] [blame] | 188 | #ifndef DISABLE_NANORC |
David Lawrence Ramsey | d89617f | 2006-01-06 21:51:10 +0000 | [diff] [blame] | 189 | /* Parse an argument, with optional quotes, after a keyword that takes |
| 190 | * one. If the next word starts with a ", we say that it ends with the |
| 191 | * last " of the line. Otherwise, we interpret it as usual, so that the |
| 192 | * arguments can contain "'s too. */ |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 193 | char *parse_argument(char *ptr) |
| 194 | { |
David Lawrence Ramsey | 5cbd990 | 2006-04-05 21:28:52 +0000 | [diff] [blame] | 195 | const char *ptr_save = ptr; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 196 | char *last_quote = NULL; |
| 197 | |
| 198 | assert(ptr != NULL); |
| 199 | |
| 200 | if (*ptr != '"') |
| 201 | return parse_next_word(ptr); |
| 202 | |
| 203 | do { |
| 204 | ptr++; |
| 205 | if (*ptr == '"') |
| 206 | last_quote = ptr; |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 207 | } while (*ptr != '\0'); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 208 | |
| 209 | if (last_quote == NULL) { |
| 210 | if (*ptr == '\0') |
| 211 | ptr = NULL; |
| 212 | else |
| 213 | *ptr++ = '\0'; |
David Lawrence Ramsey | 4bb60db | 2006-06-08 02:50:56 +0000 | [diff] [blame] | 214 | rcfile_error(N_("Argument '%s' has an unterminated \""), ptr_save); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 215 | } else { |
| 216 | *last_quote = '\0'; |
| 217 | ptr = last_quote + 1; |
| 218 | } |
| 219 | if (ptr != NULL) |
David Lawrence Ramsey | d864048 | 2005-06-12 17:48:46 +0000 | [diff] [blame] | 220 | while (isblank(*ptr)) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 221 | ptr++; |
| 222 | return ptr; |
| 223 | } |
| 224 | |
Benno Schulenberg | 0038992 | 2014-04-04 11:59:03 +0000 | [diff] [blame] | 225 | #ifndef DISABLE_COLOR |
Benno Schulenberg | b8aae4d | 2016-03-11 16:39:27 +0000 | [diff] [blame] | 226 | /* Pass over the current regex string in the line starting at ptr, |
| 227 | * null-terminate it, and return a pointer to the /next/ word. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 228 | char *parse_next_regex(char *ptr) |
| 229 | { |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 230 | assert(ptr != NULL); |
| 231 | |
Benno Schulenberg | b8aae4d | 2016-03-11 16:39:27 +0000 | [diff] [blame] | 232 | /* Continue until the end of line, or until a " followed by a |
| 233 | * blank character or the end of line. */ |
| 234 | while (*ptr != '\0' && (*ptr != '"' || |
| 235 | (*(ptr + 1) != '\0' && !isblank(*(ptr + 1))))) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 236 | ptr++; |
| 237 | |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 238 | assert(*ptr == '"' || *ptr == '\0'); |
| 239 | |
| 240 | if (*ptr == '\0') { |
David Lawrence Ramsey | 08c70ae | 2005-03-14 17:47:17 +0000 | [diff] [blame] | 241 | rcfile_error( |
| 242 | N_("Regex strings must begin and end with a \" character")); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 243 | return NULL; |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 244 | } |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 245 | |
David Lawrence Ramsey | e54f1c4 | 2006-10-29 21:14:53 +0000 | [diff] [blame] | 246 | /* Null-terminate and advance ptr. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 247 | *ptr++ = '\0'; |
| 248 | |
David Lawrence Ramsey | d864048 | 2005-06-12 17:48:46 +0000 | [diff] [blame] | 249 | while (isblank(*ptr)) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 250 | ptr++; |
| 251 | |
| 252 | return ptr; |
| 253 | } |
| 254 | |
David Lawrence Ramsey | 2385c1a | 2005-07-29 21:42:08 +0000 | [diff] [blame] | 255 | /* Compile the regular expression regex to see if it's valid. Return |
Benno Schulenberg | b8aae4d | 2016-03-11 16:39:27 +0000 | [diff] [blame] | 256 | * TRUE if it is, and FALSE otherwise. */ |
Benno Schulenberg | 6ed6462 | 2016-03-13 19:37:21 +0000 | [diff] [blame] | 257 | bool nregcomp(const char *regex, int compile_flags) |
Chris Allegretta | ce452fb | 2003-02-03 02:56:44 +0000 | [diff] [blame] | 258 | { |
David Lawrence Ramsey | 2385c1a | 2005-07-29 21:42:08 +0000 | [diff] [blame] | 259 | regex_t preg; |
Chris Allegretta | 6b83e52 | 2008-08-30 05:16:20 +0000 | [diff] [blame] | 260 | const char *r = fixbounds(regex); |
Benno Schulenberg | 6ed6462 | 2016-03-13 19:37:21 +0000 | [diff] [blame] | 261 | int rc = regcomp(&preg, r, compile_flags); |
Chris Allegretta | ce452fb | 2003-02-03 02:56:44 +0000 | [diff] [blame] | 262 | |
| 263 | if (rc != 0) { |
David Lawrence Ramsey | 2385c1a | 2005-07-29 21:42:08 +0000 | [diff] [blame] | 264 | size_t len = regerror(rc, &preg, NULL, 0); |
Chris Allegretta | ce452fb | 2003-02-03 02:56:44 +0000 | [diff] [blame] | 265 | char *str = charalloc(len); |
| 266 | |
David Lawrence Ramsey | 2385c1a | 2005-07-29 21:42:08 +0000 | [diff] [blame] | 267 | regerror(rc, &preg, str, len); |
Chris Allegretta | 6b83e52 | 2008-08-30 05:16:20 +0000 | [diff] [blame] | 268 | rcfile_error(N_("Bad regex \"%s\": %s"), r, str); |
Chris Allegretta | ce452fb | 2003-02-03 02:56:44 +0000 | [diff] [blame] | 269 | free(str); |
| 270 | } |
David Lawrence Ramsey | 9adace8 | 2005-03-04 15:09:55 +0000 | [diff] [blame] | 271 | |
David Lawrence Ramsey | 2385c1a | 2005-07-29 21:42:08 +0000 | [diff] [blame] | 272 | regfree(&preg); |
David Lawrence Ramsey | db95802 | 2005-07-13 20:18:46 +0000 | [diff] [blame] | 273 | return (rc == 0); |
Chris Allegretta | ce452fb | 2003-02-03 02:56:44 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Benno Schulenberg | b8aae4d | 2016-03-11 16:39:27 +0000 | [diff] [blame] | 276 | /* Parse the next syntax name and its possible extension regexes from the |
| 277 | * line at ptr, and add it to the global linked list of color syntaxes. */ |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 278 | void parse_syntax(char *ptr) |
Chris Allegretta | b6c5dc2 | 2002-05-04 03:47:33 +0000 | [diff] [blame] | 279 | { |
Benno Schulenberg | 2f63e8d | 2016-03-09 21:00:42 +0000 | [diff] [blame] | 280 | char *nameptr; |
| 281 | /* A pointer to what should be the name of the syntax. */ |
Chris Allegretta | b6c5dc2 | 2002-05-04 03:47:33 +0000 | [diff] [blame] | 282 | |
Benno Schulenberg | 275e9f0 | 2016-02-28 20:38:14 +0000 | [diff] [blame] | 283 | opensyntax = FALSE; |
| 284 | |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 285 | assert(ptr != NULL); |
Chris Allegretta | b6c5dc2 | 2002-05-04 03:47:33 +0000 | [diff] [blame] | 286 | |
Benno Schulenberg | b0bdfbb | 2016-03-09 20:28:50 +0000 | [diff] [blame] | 287 | /* Check that the syntax name is not empty. */ |
| 288 | if (*ptr == '\0' || (*ptr == '"' && |
| 289 | (*(ptr + 1) == '\0' || *(ptr + 1) == '"'))) { |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 290 | rcfile_error(N_("Missing syntax name")); |
Chris Allegretta | b6c5dc2 | 2002-05-04 03:47:33 +0000 | [diff] [blame] | 291 | return; |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 292 | } |
Chris Allegretta | b6c5dc2 | 2002-05-04 03:47:33 +0000 | [diff] [blame] | 293 | |
Benno Schulenberg | b0bdfbb | 2016-03-09 20:28:50 +0000 | [diff] [blame] | 294 | nameptr = ++ptr; |
| 295 | ptr = parse_next_word(ptr); |
| 296 | |
| 297 | /* Check that the name starts and ends with a double quote. */ |
| 298 | if (*(nameptr - 1) != '\x22' || nameptr[strlen(nameptr) - 1] != '\x22') { |
| 299 | rcfile_error(N_("A syntax name must be quoted")); |
Chris Allegretta | 09900ff | 2002-05-04 04:23:30 +0000 | [diff] [blame] | 300 | return; |
Chris Allegretta | b6c5dc2 | 2002-05-04 03:47:33 +0000 | [diff] [blame] | 301 | } |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 302 | |
Benno Schulenberg | b0bdfbb | 2016-03-09 20:28:50 +0000 | [diff] [blame] | 303 | /* Strip the end quote. */ |
| 304 | nameptr[strlen(nameptr) - 1] = '\0'; |
Chris Allegretta | b6c5dc2 | 2002-05-04 03:47:33 +0000 | [diff] [blame] | 305 | |
Benno Schulenberg | 717e697 | 2016-02-29 10:29:52 +0000 | [diff] [blame] | 306 | /* Redefining the "none" syntax is not allowed. */ |
| 307 | if (strcmp(nameptr, "none") == 0) { |
| 308 | rcfile_error(N_("The \"none\" syntax is reserved")); |
| 309 | return; |
| 310 | } |
| 311 | |
Benno Schulenberg | 04262f0 | 2016-03-10 20:06:01 +0000 | [diff] [blame] | 312 | /* Initialize a new syntax struct. */ |
Benno Schulenberg | 8a5ae21 | 2016-03-10 20:36:12 +0000 | [diff] [blame] | 313 | live_syntax = (syntaxtype *)nmalloc(sizeof(syntaxtype)); |
| 314 | live_syntax->name = mallocstrcpy(NULL, nameptr); |
| 315 | live_syntax->extensions = NULL; |
| 316 | live_syntax->headers = NULL; |
| 317 | live_syntax->magics = NULL; |
| 318 | live_syntax->linter = NULL; |
| 319 | live_syntax->formatter = NULL; |
Mike Scalora | 6a2032f | 2016-05-25 22:13:50 +0200 | [diff] [blame] | 320 | live_syntax->comment = NULL; |
Benno Schulenberg | 8a5ae21 | 2016-03-10 20:36:12 +0000 | [diff] [blame] | 321 | live_syntax->color = NULL; |
Benno Schulenberg | 8fbb922 | 2016-03-13 20:05:36 +0000 | [diff] [blame] | 322 | lastcolor = NULL; |
Benno Schulenberg | 8a5ae21 | 2016-03-10 20:36:12 +0000 | [diff] [blame] | 323 | live_syntax->nmultis = 0; |
Benno Schulenberg | 04262f0 | 2016-03-10 20:06:01 +0000 | [diff] [blame] | 324 | |
| 325 | /* Hook the new syntax in at the top of the list. */ |
Benno Schulenberg | 8a5ae21 | 2016-03-10 20:36:12 +0000 | [diff] [blame] | 326 | live_syntax->next = syntaxes; |
| 327 | syntaxes = live_syntax; |
David Lawrence Ramsey | 2385c1a | 2005-07-29 21:42:08 +0000 | [diff] [blame] | 328 | |
Benno Schulenberg | 275e9f0 | 2016-02-28 20:38:14 +0000 | [diff] [blame] | 329 | opensyntax = TRUE; |
| 330 | |
Chris Allegretta | b6c5dc2 | 2002-05-04 03:47:33 +0000 | [diff] [blame] | 331 | #ifdef DEBUG |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 332 | fprintf(stderr, "Starting a new syntax type: \"%s\"\n", nameptr); |
Chris Allegretta | b6c5dc2 | 2002-05-04 03:47:33 +0000 | [diff] [blame] | 333 | #endif |
| 334 | |
David Lawrence Ramsey | 179b1ba | 2005-08-01 04:23:29 +0000 | [diff] [blame] | 335 | /* The default syntax should have no associated extensions. */ |
Benno Schulenberg | 8a5ae21 | 2016-03-10 20:36:12 +0000 | [diff] [blame] | 336 | if (strcmp(live_syntax->name, "default") == 0 && *ptr != '\0') { |
David Lawrence Ramsey | d152ad3 | 2005-08-01 04:59:34 +0000 | [diff] [blame] | 337 | rcfile_error( |
Benno Schulenberg | 2994ea9 | 2016-03-10 09:46:21 +0000 | [diff] [blame] | 338 | N_("The \"default\" syntax does not accept extensions")); |
David Lawrence Ramsey | 179b1ba | 2005-08-01 04:23:29 +0000 | [diff] [blame] | 339 | return; |
| 340 | } |
| 341 | |
Benno Schulenberg | 2f63e8d | 2016-03-09 21:00:42 +0000 | [diff] [blame] | 342 | /* If there seem to be extension regexes, pick them up. */ |
| 343 | if (*ptr != '\0') |
Benno Schulenberg | 8a5ae21 | 2016-03-10 20:36:12 +0000 | [diff] [blame] | 344 | grab_and_store("extension", ptr, &live_syntax->extensions); |
Chris Allegretta | b00d0b9 | 2011-02-13 04:23:10 +0000 | [diff] [blame] | 345 | } |
Benno Schulenberg | 0038992 | 2014-04-04 11:59:03 +0000 | [diff] [blame] | 346 | #endif /* !DISABLE_COLOR */ |
Chris Allegretta | b6c5dc2 | 2002-05-04 03:47:33 +0000 | [diff] [blame] | 347 | |
Benno Schulenberg | 4651f49 | 2014-06-27 16:14:52 +0000 | [diff] [blame] | 348 | /* Check whether the given executable function is "universal" (meaning |
| 349 | * any horizontal movement or deletion) and thus is present in almost |
| 350 | * all menus. */ |
| 351 | bool is_universal(void (*func)) |
| 352 | { |
| 353 | if (func == do_left || func == do_right || |
| 354 | func == do_home || func == do_end || |
| 355 | func == do_prev_word_void || func == do_next_word_void || |
| 356 | func == do_verbatim_input || func == do_cut_text_void || |
| 357 | func == do_delete || func == do_backspace || |
| 358 | func == do_tab || func == do_enter) |
| 359 | return TRUE; |
| 360 | else |
| 361 | return FALSE; |
| 362 | } |
| 363 | |
Benno Schulenberg | f7c5eee | 2014-04-08 11:43:50 +0000 | [diff] [blame] | 364 | /* Bind or unbind a key combo, to or from a function. */ |
Benno Schulenberg | 2cdaaac | 2014-04-08 11:22:41 +0000 | [diff] [blame] | 365 | void parse_binding(char *ptr, bool dobind) |
Chris Allegretta | 79a33bb | 2008-03-05 07:34:01 +0000 | [diff] [blame] | 366 | { |
| 367 | char *keyptr = NULL, *keycopy = NULL, *funcptr = NULL, *menuptr = NULL; |
Benno Schulenberg | 2cdaaac | 2014-04-08 11:22:41 +0000 | [diff] [blame] | 368 | sc *s, *newsc = NULL; |
Benno Schulenberg | f7c5eee | 2014-04-08 11:43:50 +0000 | [diff] [blame] | 369 | int menu; |
Chris Allegretta | 79a33bb | 2008-03-05 07:34:01 +0000 | [diff] [blame] | 370 | |
| 371 | assert(ptr != NULL); |
| 372 | |
Benno Schulenberg | 2cdaaac | 2014-04-08 11:22:41 +0000 | [diff] [blame] | 373 | #ifdef DEBUG |
| 374 | fprintf(stderr, "Starting the rebinding code...\n"); |
| 375 | #endif |
| 376 | |
Chris Allegretta | 79a33bb | 2008-03-05 07:34:01 +0000 | [diff] [blame] | 377 | if (*ptr == '\0') { |
| 378 | rcfile_error(N_("Missing key name")); |
| 379 | return; |
| 380 | } |
| 381 | |
| 382 | keyptr = ptr; |
| 383 | ptr = parse_next_word(ptr); |
| 384 | keycopy = mallocstrcpy(NULL, keyptr); |
Benno Schulenberg | f7c5eee | 2014-04-08 11:43:50 +0000 | [diff] [blame] | 385 | |
| 386 | if (strlen(keycopy) < 2) { |
| 387 | rcfile_error(N_("Key name is too short")); |
Benno Schulenberg | 95e1f55 | 2015-08-03 19:52:48 +0000 | [diff] [blame] | 388 | goto free_copy; |
Benno Schulenberg | f7c5eee | 2014-04-08 11:43:50 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /* Uppercase only the first two or three characters of the key name. */ |
| 392 | keycopy[0] = toupper(keycopy[0]); |
| 393 | keycopy[1] = toupper(keycopy[1]); |
| 394 | if (keycopy[0] == 'M' && keycopy[1] == '-') { |
| 395 | if (strlen(keycopy) > 2) |
| 396 | keycopy[2] = toupper(keycopy[2]); |
| 397 | else { |
| 398 | rcfile_error(N_("Key name is too short")); |
Benno Schulenberg | 95e1f55 | 2015-08-03 19:52:48 +0000 | [diff] [blame] | 399 | goto free_copy; |
Benno Schulenberg | f7c5eee | 2014-04-08 11:43:50 +0000 | [diff] [blame] | 400 | } |
| 401 | } |
Chris Allegretta | 79a33bb | 2008-03-05 07:34:01 +0000 | [diff] [blame] | 402 | |
Benno Schulenberg | 94b1d01 | 2014-04-21 18:05:11 +0000 | [diff] [blame] | 403 | /* Allow the codes for Insert and Delete to be rebound, but apart |
| 404 | * from those two only Control, Meta and Function sequences. */ |
| 405 | if (!strcasecmp(keycopy, "Ins") || !strcasecmp(keycopy, "Del")) |
| 406 | keycopy[1] = tolower(keycopy[1]); |
| 407 | else if (keycopy[0] != '^' && keycopy[0] != 'M' && keycopy[0] != 'F') { |
Benno Schulenberg | f7c5eee | 2014-04-08 11:43:50 +0000 | [diff] [blame] | 408 | rcfile_error(N_("Key name must begin with \"^\", \"M\", or \"F\"")); |
Benno Schulenberg | 95e1f55 | 2015-08-03 19:52:48 +0000 | [diff] [blame] | 409 | goto free_copy; |
Rishabh Dave | e2027ae | 2016-09-14 17:19:53 +0530 | [diff] [blame] | 410 | } else if ((keycopy[0] == 'M' && keycopy[1] != '-') || |
Rishabh Dave | 01bf034 | 2016-10-11 19:02:02 +0530 | [diff] [blame] | 411 | (keycopy[0] == '^' && ((keycopy[1] < 'A' || keycopy[1] > 'z') || |
| 412 | keycopy[1] == '[' || keycopy[1] == '`' || |
Rishabh Dave | e2027ae | 2016-09-14 17:19:53 +0530 | [diff] [blame] | 413 | (strlen(keycopy) > 2 && strcmp(keycopy, "^Space") != 0))) || |
| 414 | (strlen(keycopy) > 3 && strcmp(keycopy, "^Space") != 0 && |
| 415 | strcmp(keycopy, "M-Space") != 0)) { |
Benno Schulenberg | 14d1b3b | 2016-02-10 15:06:45 +0000 | [diff] [blame] | 416 | rcfile_error(N_("Key name %s is invalid"), keycopy); |
| 417 | goto free_copy; |
Chris Allegretta | 79a33bb | 2008-03-05 07:34:01 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Benno Schulenberg | 2cdaaac | 2014-04-08 11:22:41 +0000 | [diff] [blame] | 420 | if (dobind) { |
| 421 | funcptr = ptr; |
| 422 | ptr = parse_next_word(ptr); |
Chris Allegretta | 79a33bb | 2008-03-05 07:34:01 +0000 | [diff] [blame] | 423 | |
Benno Schulenberg | ed0086b | 2014-06-09 14:33:00 +0000 | [diff] [blame] | 424 | if (funcptr[0] == '\0') { |
Benno Schulenberg | e15abc9 | 2014-04-21 18:12:29 +0000 | [diff] [blame] | 425 | rcfile_error(N_("Must specify a function to bind the key to")); |
Benno Schulenberg | 95e1f55 | 2015-08-03 19:52:48 +0000 | [diff] [blame] | 426 | goto free_copy; |
Chris Allegretta | 79a33bb | 2008-03-05 07:34:01 +0000 | [diff] [blame] | 427 | } |
| 428 | } |
Chris Allegretta | e9dee88 | 2009-11-21 16:26:59 +0000 | [diff] [blame] | 429 | |
| 430 | menuptr = ptr; |
| 431 | ptr = parse_next_word(ptr); |
| 432 | |
Benno Schulenberg | ed0086b | 2014-06-09 14:33:00 +0000 | [diff] [blame] | 433 | if (menuptr[0] == '\0') { |
Benno Schulenberg | d19be5a | 2014-04-08 18:38:45 +0000 | [diff] [blame] | 434 | /* TRANSLATORS: Do not translate the word "all". */ |
Benno Schulenberg | e15abc9 | 2014-04-21 18:12:29 +0000 | [diff] [blame] | 435 | rcfile_error(N_("Must specify a menu (or \"all\") in which to bind/unbind the key")); |
Benno Schulenberg | 95e1f55 | 2015-08-03 19:52:48 +0000 | [diff] [blame] | 436 | goto free_copy; |
Benno Schulenberg | f7c5eee | 2014-04-08 11:43:50 +0000 | [diff] [blame] | 437 | } |
Benno Schulenberg | 2cdaaac | 2014-04-08 11:22:41 +0000 | [diff] [blame] | 438 | |
| 439 | if (dobind) { |
Benno Schulenberg | 1f934e3 | 2014-04-13 11:56:08 +0000 | [diff] [blame] | 440 | newsc = strtosc(funcptr); |
Benno Schulenberg | 2cdaaac | 2014-04-08 11:22:41 +0000 | [diff] [blame] | 441 | if (newsc == NULL) { |
Benno Schulenberg | f7c5eee | 2014-04-08 11:43:50 +0000 | [diff] [blame] | 442 | rcfile_error(N_("Cannot map name \"%s\" to a function"), funcptr); |
Benno Schulenberg | 95e1f55 | 2015-08-03 19:52:48 +0000 | [diff] [blame] | 443 | goto free_copy; |
Benno Schulenberg | 2cdaaac | 2014-04-08 11:22:41 +0000 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
Benno Schulenberg | e15abc9 | 2014-04-21 18:12:29 +0000 | [diff] [blame] | 447 | menu = strtomenu(menuptr); |
| 448 | if (menu < 1) { |
| 449 | rcfile_error(N_("Cannot map name \"%s\" to a menu"), menuptr); |
Benno Schulenberg | 95e1f55 | 2015-08-03 19:52:48 +0000 | [diff] [blame] | 450 | goto free_copy; |
Benno Schulenberg | e15abc9 | 2014-04-21 18:12:29 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Chris Allegretta | e9dee88 | 2009-11-21 16:26:59 +0000 | [diff] [blame] | 453 | #ifdef DEBUG |
Benno Schulenberg | 2cdaaac | 2014-04-08 11:22:41 +0000 | [diff] [blame] | 454 | if (dobind) |
Benno Schulenberg | 27a52a8 | 2014-04-21 13:07:18 +0000 | [diff] [blame] | 455 | fprintf(stderr, "newsc address is now %ld, assigned func = %ld, menu = %x\n", |
| 456 | (long)&newsc, (long)newsc->scfunc, menu); |
Benno Schulenberg | 111071a | 2014-05-13 08:34:29 +0000 | [diff] [blame] | 457 | else |
Benno Schulenberg | 2cdaaac | 2014-04-08 11:22:41 +0000 | [diff] [blame] | 458 | fprintf(stderr, "unbinding \"%s\" from menu %x\n", keycopy, menu); |
Chris Allegretta | e9dee88 | 2009-11-21 16:26:59 +0000 | [diff] [blame] | 459 | #endif |
| 460 | |
Benno Schulenberg | 79ff393 | 2015-03-08 15:42:52 +0000 | [diff] [blame] | 461 | if (dobind) { |
Benno Schulenberg | 4651f49 | 2014-06-27 16:14:52 +0000 | [diff] [blame] | 462 | subnfunc *f; |
| 463 | int mask = 0; |
| 464 | |
| 465 | /* Tally up the menus where the function exists. */ |
| 466 | for (f = allfuncs; f != NULL; f = f->next) |
| 467 | if (f->scfunc == newsc->scfunc) |
| 468 | mask = mask | f->menus; |
| 469 | |
Benno Schulenberg | 7b7d2bf | 2016-09-01 09:36:47 +0200 | [diff] [blame] | 470 | #ifndef NANO_TINY |
Benno Schulenberg | 79ff393 | 2015-03-08 15:42:52 +0000 | [diff] [blame] | 471 | /* Handle the special case of the toggles. */ |
| 472 | if (newsc->scfunc == do_toggle_void) |
| 473 | mask = MMAIN; |
Benno Schulenberg | 7b7d2bf | 2016-09-01 09:36:47 +0200 | [diff] [blame] | 474 | #endif |
Benno Schulenberg | 79ff393 | 2015-03-08 15:42:52 +0000 | [diff] [blame] | 475 | |
Benno Schulenberg | 4651f49 | 2014-06-27 16:14:52 +0000 | [diff] [blame] | 476 | /* Now limit the given menu to those where the function exists. */ |
| 477 | if (is_universal(newsc->scfunc)) |
| 478 | menu = menu & MMOST; |
| 479 | else |
| 480 | menu = menu & mask; |
| 481 | |
| 482 | if (!menu) { |
| 483 | rcfile_error(N_("Function '%s' does not exist in menu '%s'"), funcptr, menuptr); |
| 484 | free(newsc); |
Benno Schulenberg | 95e1f55 | 2015-08-03 19:52:48 +0000 | [diff] [blame] | 485 | goto free_copy; |
Benno Schulenberg | 4651f49 | 2014-06-27 16:14:52 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Benno Schulenberg | 1f866c2 | 2015-07-15 20:13:05 +0000 | [diff] [blame] | 488 | newsc->menus = menu; |
Benno Schulenberg | 55878ef | 2016-10-15 17:55:19 +0200 | [diff] [blame] | 489 | assign_keyinfo(newsc, keycopy, 0); |
Benno Schulenberg | 2cdaaac | 2014-04-08 11:22:41 +0000 | [diff] [blame] | 490 | |
Benno Schulenberg | e295070 | 2016-07-23 14:42:40 +0200 | [diff] [blame] | 491 | /* Do not allow rebinding a frequent escape-sequence starter: Esc [. */ |
Benno Schulenberg | 1c9ab8b | 2016-07-24 21:49:07 +0200 | [diff] [blame] | 492 | if (newsc->meta && newsc->keycode == 91) { |
Benno Schulenberg | 111071a | 2014-05-13 08:34:29 +0000 | [diff] [blame] | 493 | rcfile_error(N_("Sorry, keystroke \"%s\" may not be rebound"), newsc->keystr); |
| 494 | free(newsc); |
Benno Schulenberg | 95e1f55 | 2015-08-03 19:52:48 +0000 | [diff] [blame] | 495 | goto free_copy; |
Benno Schulenberg | 2cdaaac | 2014-04-08 11:22:41 +0000 | [diff] [blame] | 496 | } |
Benno Schulenberg | 6337095 | 2016-01-14 14:44:11 +0000 | [diff] [blame] | 497 | #ifdef DEBUG |
| 498 | fprintf(stderr, "s->keystr = \"%s\"\n", newsc->keystr); |
Benno Schulenberg | 1c9ab8b | 2016-07-24 21:49:07 +0200 | [diff] [blame] | 499 | fprintf(stderr, "s->keycode = \"%d\"\n", newsc->keycode); |
Benno Schulenberg | 6337095 | 2016-01-14 14:44:11 +0000 | [diff] [blame] | 500 | #endif |
Benno Schulenberg | 2cdaaac | 2014-04-08 11:22:41 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | /* Now find and delete any existing same shortcut in the menu(s). */ |
Chris Allegretta | e9dee88 | 2009-11-21 16:26:59 +0000 | [diff] [blame] | 504 | for (s = sclist; s != NULL; s = s->next) { |
Benno Schulenberg | 1f866c2 | 2015-07-15 20:13:05 +0000 | [diff] [blame] | 505 | if ((s->menus & menu) && !strcmp(s->keystr, keycopy)) { |
Chris Allegretta | e9dee88 | 2009-11-21 16:26:59 +0000 | [diff] [blame] | 506 | #ifdef DEBUG |
Benno Schulenberg | 1f866c2 | 2015-07-15 20:13:05 +0000 | [diff] [blame] | 507 | fprintf(stderr, "deleting entry from among menus %x\n", s->menus); |
Chris Allegretta | e9dee88 | 2009-11-21 16:26:59 +0000 | [diff] [blame] | 508 | #endif |
Benno Schulenberg | 1f866c2 | 2015-07-15 20:13:05 +0000 | [diff] [blame] | 509 | s->menus &= ~menu; |
Chris Allegretta | e9dee88 | 2009-11-21 16:26:59 +0000 | [diff] [blame] | 510 | } |
| 511 | } |
Benno Schulenberg | 2cdaaac | 2014-04-08 11:22:41 +0000 | [diff] [blame] | 512 | |
| 513 | if (dobind) { |
Benno Schulenberg | 7b7d2bf | 2016-09-01 09:36:47 +0200 | [diff] [blame] | 514 | #ifndef NANO_TINY |
Benno Schulenberg | 5ac6a87 | 2015-07-06 17:51:17 +0000 | [diff] [blame] | 515 | /* If this is a toggle, copy its sequence number. */ |
| 516 | if (newsc->scfunc == do_toggle_void) { |
| 517 | for (s = sclist; s != NULL; s = s->next) |
Benno Schulenberg | 3d2784e | 2015-08-03 08:03:22 +0000 | [diff] [blame] | 518 | if (s->scfunc == do_toggle_void && s->toggle == newsc->toggle) |
Benno Schulenberg | 5ac6a87 | 2015-07-06 17:51:17 +0000 | [diff] [blame] | 519 | newsc->ordinal = s->ordinal; |
| 520 | } else |
| 521 | newsc->ordinal = 0; |
Benno Schulenberg | 7b7d2bf | 2016-09-01 09:36:47 +0200 | [diff] [blame] | 522 | #endif |
Benno Schulenberg | 2cdaaac | 2014-04-08 11:22:41 +0000 | [diff] [blame] | 523 | /* Add the new shortcut at the start of the list. */ |
| 524 | newsc->next = sclist; |
| 525 | sclist = newsc; |
Benno Schulenberg | 95e1f55 | 2015-08-03 19:52:48 +0000 | [diff] [blame] | 526 | return; |
| 527 | } |
| 528 | |
| 529 | free_copy: |
| 530 | free(keycopy); |
Chris Allegretta | e9dee88 | 2009-11-21 16:26:59 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Benno Schulenberg | 981a1d3 | 2016-11-27 17:27:04 +0100 | [diff] [blame] | 533 | /* Verify that the given file is not a folder nor a device. */ |
| 534 | bool is_good_file(char *file) |
| 535 | { |
| 536 | struct stat rcinfo; |
| 537 | |
| 538 | /* If the thing exists, it may not be a directory nor a device. */ |
| 539 | if (stat(file, &rcinfo) != -1 && (S_ISDIR(rcinfo.st_mode) || |
| 540 | S_ISCHR(rcinfo.st_mode) || S_ISBLK(rcinfo.st_mode))) { |
| 541 | rcfile_error(S_ISDIR(rcinfo.st_mode) ? _("\"%s\" is a directory") : |
| 542 | _("\"%s\" is a device file"), file); |
| 543 | return FALSE; |
| 544 | } else |
| 545 | return TRUE; |
| 546 | } |
| 547 | |
Benno Schulenberg | 0038992 | 2014-04-04 11:59:03 +0000 | [diff] [blame] | 548 | #ifndef DISABLE_COLOR |
Benno Schulenberg | 3cb80ff | 2016-03-12 09:43:10 +0000 | [diff] [blame] | 549 | /* Read and parse one included syntax file. */ |
| 550 | static void parse_one_include(char *file) |
David Lawrence Ramsey | cee20e5 | 2006-04-13 02:43:54 +0000 | [diff] [blame] | 551 | { |
David Lawrence Ramsey | cee20e5 | 2006-04-13 02:43:54 +0000 | [diff] [blame] | 552 | FILE *rcstream; |
David Lawrence Ramsey | cee20e5 | 2006-04-13 02:43:54 +0000 | [diff] [blame] | 553 | |
David Lawrence Ramsey | cee20e5 | 2006-04-13 02:43:54 +0000 | [diff] [blame] | 554 | /* Don't open directories, character files, or block files. */ |
Benno Schulenberg | 981a1d3 | 2016-11-27 17:27:04 +0100 | [diff] [blame] | 555 | if (!is_good_file(file)) |
Benno Schulenberg | 1762920 | 2016-11-26 17:43:36 +0100 | [diff] [blame] | 556 | return; |
David Lawrence Ramsey | cee20e5 | 2006-04-13 02:43:54 +0000 | [diff] [blame] | 557 | |
Benno Schulenberg | 281a56f | 2016-11-26 17:41:31 +0100 | [diff] [blame] | 558 | /* Open the included syntax file. */ |
| 559 | rcstream = fopen(file, "rb"); |
| 560 | |
| 561 | if (rcstream == NULL) { |
| 562 | rcfile_error(_("Error reading %s: %s"), file, strerror(errno)); |
Chris Allegretta | 4522ca2 | 2009-02-07 00:01:40 +0000 | [diff] [blame] | 563 | return; |
David Lawrence Ramsey | cee20e5 | 2006-04-13 02:43:54 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Benno Schulenberg | 281a56f | 2016-11-26 17:41:31 +0100 | [diff] [blame] | 566 | /* Use the name and line number position of the included syntax file |
David Lawrence Ramsey | cee20e5 | 2006-04-13 02:43:54 +0000 | [diff] [blame] | 567 | * while parsing it, so we can know where any errors in it are. */ |
Benno Schulenberg | 3814283 | 2014-03-30 20:37:40 +0000 | [diff] [blame] | 568 | nanorc = file; |
David Lawrence Ramsey | cee20e5 | 2006-04-13 02:43:54 +0000 | [diff] [blame] | 569 | lineno = 0; |
| 570 | |
Chris Allegretta | cc60c3a | 2008-03-20 05:41:00 +0000 | [diff] [blame] | 571 | #ifdef DEBUG |
Benno Schulenberg | 3814283 | 2014-03-30 20:37:40 +0000 | [diff] [blame] | 572 | fprintf(stderr, "Parsing file \"%s\"\n", file); |
Chris Allegretta | cc60c3a | 2008-03-20 05:41:00 +0000 | [diff] [blame] | 573 | #endif |
| 574 | |
Benno Schulenberg | de53c53 | 2015-04-03 15:57:22 +0000 | [diff] [blame] | 575 | parse_rcfile(rcstream, TRUE); |
Benno Schulenberg | 3814283 | 2014-03-30 20:37:40 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Benno Schulenberg | 3cb80ff | 2016-03-12 09:43:10 +0000 | [diff] [blame] | 578 | /* Expand globs in the passed name, and parse the resultant files. */ |
| 579 | void parse_includes(char *ptr) |
Benno Schulenberg | 3814283 | 2014-03-30 20:37:40 +0000 | [diff] [blame] | 580 | { |
| 581 | char *option, *nanorc_save = nanorc, *expanded; |
| 582 | size_t lineno_save = lineno, i; |
| 583 | glob_t files; |
| 584 | |
| 585 | option = ptr; |
| 586 | if (*option == '"') |
| 587 | option++; |
| 588 | ptr = parse_argument(ptr); |
| 589 | |
| 590 | /* Expand tildes first, then the globs. */ |
| 591 | expanded = real_dir_from_tilde(option); |
| 592 | |
| 593 | if (glob(expanded, GLOB_ERR|GLOB_NOSORT, NULL, &files) == 0) { |
| 594 | for (i = 0; i < files.gl_pathc; ++i) |
Benno Schulenberg | 3cb80ff | 2016-03-12 09:43:10 +0000 | [diff] [blame] | 595 | parse_one_include(files.gl_pathv[i]); |
| 596 | } else |
Benno Schulenberg | 281a56f | 2016-11-26 17:41:31 +0100 | [diff] [blame] | 597 | rcfile_error(_("Error expanding %s: %s"), option, strerror(errno)); |
David Lawrence Ramsey | cee20e5 | 2006-04-13 02:43:54 +0000 | [diff] [blame] | 598 | |
Benno Schulenberg | 20011f4 | 2014-04-27 11:29:50 +0000 | [diff] [blame] | 599 | globfree(&files); |
| 600 | free(expanded); |
| 601 | |
Benno Schulenberg | 3cb80ff | 2016-03-12 09:43:10 +0000 | [diff] [blame] | 602 | /* We're done with the included file(s). Restore the original |
David Lawrence Ramsey | cee20e5 | 2006-04-13 02:43:54 +0000 | [diff] [blame] | 603 | * filename and line number position. */ |
| 604 | nanorc = nanorc_save; |
| 605 | lineno = lineno_save; |
David Lawrence Ramsey | cee20e5 | 2006-04-13 02:43:54 +0000 | [diff] [blame] | 606 | } |
| 607 | |
David Lawrence Ramsey | 8d2d0d9 | 2006-05-28 19:00:16 +0000 | [diff] [blame] | 608 | /* Return the short value corresponding to the color named in colorname, |
| 609 | * and set bright to TRUE if that color is bright. */ |
Chris Allegretta | 22c83ec | 2013-03-17 22:09:38 +0000 | [diff] [blame] | 610 | short color_to_short(const char *colorname, bool *bright) |
David Lawrence Ramsey | 8d2d0d9 | 2006-05-28 19:00:16 +0000 | [diff] [blame] | 611 | { |
David Lawrence Ramsey | 8d2d0d9 | 2006-05-28 19:00:16 +0000 | [diff] [blame] | 612 | assert(colorname != NULL && bright != NULL); |
| 613 | |
| 614 | if (strncasecmp(colorname, "bright", 6) == 0) { |
| 615 | *bright = TRUE; |
| 616 | colorname += 6; |
| 617 | } |
| 618 | |
| 619 | if (strcasecmp(colorname, "green") == 0) |
Benno Schulenberg | 6e37723 | 2016-03-11 16:45:00 +0000 | [diff] [blame] | 620 | return COLOR_GREEN; |
David Lawrence Ramsey | 8d2d0d9 | 2006-05-28 19:00:16 +0000 | [diff] [blame] | 621 | else if (strcasecmp(colorname, "red") == 0) |
Benno Schulenberg | 6e37723 | 2016-03-11 16:45:00 +0000 | [diff] [blame] | 622 | return COLOR_RED; |
David Lawrence Ramsey | 8d2d0d9 | 2006-05-28 19:00:16 +0000 | [diff] [blame] | 623 | else if (strcasecmp(colorname, "blue") == 0) |
Benno Schulenberg | 6e37723 | 2016-03-11 16:45:00 +0000 | [diff] [blame] | 624 | return COLOR_BLUE; |
David Lawrence Ramsey | 8d2d0d9 | 2006-05-28 19:00:16 +0000 | [diff] [blame] | 625 | else if (strcasecmp(colorname, "white") == 0) |
Benno Schulenberg | 6e37723 | 2016-03-11 16:45:00 +0000 | [diff] [blame] | 626 | return COLOR_WHITE; |
David Lawrence Ramsey | 8d2d0d9 | 2006-05-28 19:00:16 +0000 | [diff] [blame] | 627 | else if (strcasecmp(colorname, "yellow") == 0) |
Benno Schulenberg | 6e37723 | 2016-03-11 16:45:00 +0000 | [diff] [blame] | 628 | return COLOR_YELLOW; |
David Lawrence Ramsey | 8d2d0d9 | 2006-05-28 19:00:16 +0000 | [diff] [blame] | 629 | else if (strcasecmp(colorname, "cyan") == 0) |
Benno Schulenberg | 6e37723 | 2016-03-11 16:45:00 +0000 | [diff] [blame] | 630 | return COLOR_CYAN; |
David Lawrence Ramsey | 8d2d0d9 | 2006-05-28 19:00:16 +0000 | [diff] [blame] | 631 | else if (strcasecmp(colorname, "magenta") == 0) |
Benno Schulenberg | 6e37723 | 2016-03-11 16:45:00 +0000 | [diff] [blame] | 632 | return COLOR_MAGENTA; |
David Lawrence Ramsey | 8d2d0d9 | 2006-05-28 19:00:16 +0000 | [diff] [blame] | 633 | else if (strcasecmp(colorname, "black") == 0) |
Benno Schulenberg | 6e37723 | 2016-03-11 16:45:00 +0000 | [diff] [blame] | 634 | return COLOR_BLACK; |
| 635 | |
| 636 | rcfile_error(N_("Color \"%s\" not understood.\n" |
David Lawrence Ramsey | 8d2d0d9 | 2006-05-28 19:00:16 +0000 | [diff] [blame] | 637 | "Valid colors are \"green\", \"red\", \"blue\",\n" |
| 638 | "\"white\", \"yellow\", \"cyan\", \"magenta\" and\n" |
| 639 | "\"black\", with the optional prefix \"bright\"\n" |
| 640 | "for foreground colors."), colorname); |
Benno Schulenberg | 6e37723 | 2016-03-11 16:45:00 +0000 | [diff] [blame] | 641 | return -1; |
David Lawrence Ramsey | 8d2d0d9 | 2006-05-28 19:00:16 +0000 | [diff] [blame] | 642 | } |
| 643 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 644 | /* Parse the color string in the line at ptr, and add it to the current |
Benno Schulenberg | 6ed6462 | 2016-03-13 19:37:21 +0000 | [diff] [blame] | 645 | * file's associated colors. rex_flags are the regex compilation flags |
| 646 | * to use, excluding or including REG_ICASE for case (in)sensitivity. */ |
| 647 | void parse_colors(char *ptr, int rex_flags) |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 648 | { |
Chris Allegretta | 22c83ec | 2013-03-17 22:09:38 +0000 | [diff] [blame] | 649 | short fg, bg; |
Benno Schulenberg | 1663994 | 2014-05-03 18:24:45 +0000 | [diff] [blame] | 650 | bool bright = FALSE; |
Benno Schulenberg | de36e1c | 2017-01-17 12:33:46 +0100 | [diff] [blame] | 651 | char *item; |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 652 | |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 653 | assert(ptr != NULL); |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 654 | |
Benno Schulenberg | 275e9f0 | 2016-02-28 20:38:14 +0000 | [diff] [blame] | 655 | if (!opensyntax) { |
David Lawrence Ramsey | cee20e5 | 2006-04-13 02:43:54 +0000 | [diff] [blame] | 656 | rcfile_error( |
Benno Schulenberg | 7070812 | 2016-03-14 16:09:52 +0000 | [diff] [blame] | 657 | N_("A '%s' command requires a preceding 'syntax' command"), |
| 658 | "color"); |
David Lawrence Ramsey | cee20e5 | 2006-04-13 02:43:54 +0000 | [diff] [blame] | 659 | return; |
| 660 | } |
| 661 | |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 662 | if (*ptr == '\0') { |
David Lawrence Ramsey | 6420d44 | 2004-08-11 05:13:08 +0000 | [diff] [blame] | 663 | rcfile_error(N_("Missing color name")); |
Chris Allegretta | 09900ff | 2002-05-04 04:23:30 +0000 | [diff] [blame] | 664 | return; |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Benno Schulenberg | de36e1c | 2017-01-17 12:33:46 +0100 | [diff] [blame] | 667 | item = ptr; |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 668 | ptr = parse_next_word(ptr); |
Benno Schulenberg | de36e1c | 2017-01-17 12:33:46 +0100 | [diff] [blame] | 669 | if (!parse_color_names(item, &fg, &bg, &bright)) |
Benno Schulenberg | 1663994 | 2014-05-03 18:24:45 +0000 | [diff] [blame] | 670 | return; |
Chris Allegretta | 17ec14b | 2003-02-07 00:19:05 +0000 | [diff] [blame] | 671 | |
David Lawrence Ramsey | a9d45bb | 2005-06-09 01:09:00 +0000 | [diff] [blame] | 672 | if (*ptr == '\0') { |
Benno Schulenberg | f1b9ba2 | 2016-05-03 09:31:59 +0200 | [diff] [blame] | 673 | rcfile_error(N_("Missing regex string after '%s' command"), "color"); |
David Lawrence Ramsey | a9d45bb | 2005-06-09 01:09:00 +0000 | [diff] [blame] | 674 | return; |
| 675 | } |
| 676 | |
David Lawrence Ramsey | 5854a87 | 2005-06-03 19:10:47 +0000 | [diff] [blame] | 677 | /* Now for the fun part. Start adding regexes to individual strings |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 678 | * in the colorstrings array, woo! */ |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 679 | while (ptr != NULL && *ptr != '\0') { |
Benno Schulenberg | 1e5614b | 2016-07-22 15:48:06 +0200 | [diff] [blame] | 680 | colortype *newcolor = NULL; |
Benno Schulenberg | 07f14de | 2014-05-12 12:11:24 +0000 | [diff] [blame] | 681 | /* The container for a color plus its regexes. */ |
Benno Schulenberg | f72e165 | 2016-03-12 10:21:02 +0000 | [diff] [blame] | 682 | bool goodstart; |
| 683 | /* Whether the start expression was valid. */ |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 684 | bool expectend = FALSE; |
Benno Schulenberg | f72e165 | 2016-03-12 10:21:02 +0000 | [diff] [blame] | 685 | /* Whether to expect an end= line. */ |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 686 | |
David Lawrence Ramsey | b8c479a | 2004-07-31 14:10:23 +0000 | [diff] [blame] | 687 | if (strncasecmp(ptr, "start=", 6) == 0) { |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 688 | ptr += 6; |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 689 | expectend = TRUE; |
Chris Allegretta | f478f83 | 2002-01-18 21:54:35 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 692 | if (*ptr != '"') { |
David Lawrence Ramsey | 08c70ae | 2005-03-14 17:47:17 +0000 | [diff] [blame] | 693 | rcfile_error( |
| 694 | N_("Regex strings must begin and end with a \" character")); |
Chris Allegretta | de85262 | 2002-09-18 00:28:57 +0000 | [diff] [blame] | 695 | ptr = parse_next_regex(ptr); |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 696 | continue; |
| 697 | } |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 698 | |
Benno Schulenberg | de36e1c | 2017-01-17 12:33:46 +0100 | [diff] [blame] | 699 | item = ++ptr; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 700 | ptr = parse_next_regex(ptr); |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 701 | if (ptr == NULL) |
| 702 | break; |
| 703 | |
Benno Schulenberg | de36e1c | 2017-01-17 12:33:46 +0100 | [diff] [blame] | 704 | if (*item == '\0') { |
Benno Schulenberg | a8c1dc1 | 2017-01-16 16:28:48 +0100 | [diff] [blame] | 705 | rcfile_error(N_("Empty regex string")); |
| 706 | goodstart = FALSE; |
| 707 | } else |
Benno Schulenberg | de36e1c | 2017-01-17 12:33:46 +0100 | [diff] [blame] | 708 | goodstart = nregcomp(item, rex_flags); |
Benno Schulenberg | f72e165 | 2016-03-12 10:21:02 +0000 | [diff] [blame] | 709 | |
| 710 | /* If the starting regex is valid, initialize a new color struct, |
| 711 | * and hook it in at the tail of the linked list. */ |
| 712 | if (goodstart) { |
Benno Schulenberg | 6a4d3aa | 2016-03-04 20:50:38 +0000 | [diff] [blame] | 713 | newcolor = (colortype *)nmalloc(sizeof(colortype)); |
| 714 | |
Chris Allegretta | ce452fb | 2003-02-03 02:56:44 +0000 | [diff] [blame] | 715 | newcolor->fg = fg; |
| 716 | newcolor->bg = bg; |
| 717 | newcolor->bright = bright; |
Benno Schulenberg | 6ed6462 | 2016-03-13 19:37:21 +0000 | [diff] [blame] | 718 | newcolor->rex_flags = rex_flags; |
David Lawrence Ramsey | 2385c1a | 2005-07-29 21:42:08 +0000 | [diff] [blame] | 719 | |
Benno Schulenberg | de36e1c | 2017-01-17 12:33:46 +0100 | [diff] [blame] | 720 | newcolor->start_regex = mallocstrcpy(NULL, item); |
David Lawrence Ramsey | 2385c1a | 2005-07-29 21:42:08 +0000 | [diff] [blame] | 721 | newcolor->start = NULL; |
| 722 | |
David Lawrence Ramsey | d2361f0 | 2005-07-14 18:33:51 +0000 | [diff] [blame] | 723 | newcolor->end_regex = NULL; |
Chris Allegretta | ce452fb | 2003-02-03 02:56:44 +0000 | [diff] [blame] | 724 | newcolor->end = NULL; |
David Lawrence Ramsey | 2385c1a | 2005-07-29 21:42:08 +0000 | [diff] [blame] | 725 | |
David Lawrence Ramsey | db95802 | 2005-07-13 20:18:46 +0000 | [diff] [blame] | 726 | newcolor->next = NULL; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 727 | |
Benno Schulenberg | f72e165 | 2016-03-12 10:21:02 +0000 | [diff] [blame] | 728 | #ifdef DEBUG |
| 729 | fprintf(stderr, "Adding an entry for fg %hd, bg %hd\n", fg, bg); |
| 730 | #endif |
Benno Schulenberg | 8fbb922 | 2016-03-13 20:05:36 +0000 | [diff] [blame] | 731 | if (lastcolor == NULL) |
Benno Schulenberg | 8a5ae21 | 2016-03-10 20:36:12 +0000 | [diff] [blame] | 732 | live_syntax->color = newcolor; |
Benno Schulenberg | 63d5965 | 2016-03-12 11:15:12 +0000 | [diff] [blame] | 733 | else |
Benno Schulenberg | 8fbb922 | 2016-03-13 20:05:36 +0000 | [diff] [blame] | 734 | lastcolor->next = newcolor; |
David Lawrence Ramsey | 2385c1a | 2005-07-29 21:42:08 +0000 | [diff] [blame] | 735 | |
Benno Schulenberg | 8fbb922 | 2016-03-13 20:05:36 +0000 | [diff] [blame] | 736 | lastcolor = newcolor; |
Benno Schulenberg | f72e165 | 2016-03-12 10:21:02 +0000 | [diff] [blame] | 737 | } |
Chris Allegretta | ce452fb | 2003-02-03 02:56:44 +0000 | [diff] [blame] | 738 | |
Benno Schulenberg | fa82824 | 2016-03-12 09:52:16 +0000 | [diff] [blame] | 739 | if (!expectend) |
| 740 | continue; |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 741 | |
Benno Schulenberg | fa82824 | 2016-03-12 09:52:16 +0000 | [diff] [blame] | 742 | if (ptr == NULL || strncasecmp(ptr, "end=", 4) != 0) { |
| 743 | rcfile_error(N_("\"start=\" requires a corresponding \"end=\"")); |
| 744 | return; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 745 | } |
Benno Schulenberg | fa82824 | 2016-03-12 09:52:16 +0000 | [diff] [blame] | 746 | |
| 747 | ptr += 4; |
| 748 | if (*ptr != '"') { |
| 749 | rcfile_error(N_("Regex strings must begin and end with a \" character")); |
| 750 | continue; |
| 751 | } |
| 752 | |
Benno Schulenberg | de36e1c | 2017-01-17 12:33:46 +0100 | [diff] [blame] | 753 | item = ++ptr; |
Benno Schulenberg | fa82824 | 2016-03-12 09:52:16 +0000 | [diff] [blame] | 754 | ptr = parse_next_regex(ptr); |
| 755 | if (ptr == NULL) |
Benno Schulenberg | cdcd365 | 2016-05-17 11:33:21 +0200 | [diff] [blame] | 756 | break; |
Benno Schulenberg | fa82824 | 2016-03-12 09:52:16 +0000 | [diff] [blame] | 757 | |
Benno Schulenberg | de36e1c | 2017-01-17 12:33:46 +0100 | [diff] [blame] | 758 | if (*item == '\0') { |
Benno Schulenberg | a8c1dc1 | 2017-01-16 16:28:48 +0100 | [diff] [blame] | 759 | rcfile_error(N_("Empty regex string")); |
| 760 | continue; |
| 761 | } |
| 762 | |
Benno Schulenberg | fa82824 | 2016-03-12 09:52:16 +0000 | [diff] [blame] | 763 | /* If the start regex was invalid, skip past the end regex |
| 764 | * to stay in sync. */ |
Benno Schulenberg | f72e165 | 2016-03-12 10:21:02 +0000 | [diff] [blame] | 765 | if (!goodstart) |
Benno Schulenberg | fa82824 | 2016-03-12 09:52:16 +0000 | [diff] [blame] | 766 | continue; |
| 767 | |
| 768 | /* If it's valid, save the ending regex string. */ |
Benno Schulenberg | de36e1c | 2017-01-17 12:33:46 +0100 | [diff] [blame] | 769 | if (nregcomp(item, rex_flags)) |
| 770 | newcolor->end_regex = mallocstrcpy(NULL, item); |
Benno Schulenberg | fa82824 | 2016-03-12 09:52:16 +0000 | [diff] [blame] | 771 | |
| 772 | /* Lame way to skip another static counter. */ |
| 773 | newcolor->id = live_syntax->nmultis; |
| 774 | live_syntax->nmultis++; |
Chris Allegretta | f478f83 | 2002-01-18 21:54:35 +0000 | [diff] [blame] | 775 | } |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 776 | } |
Chris Allegretta | f30c139 | 2008-09-21 23:02:30 +0000 | [diff] [blame] | 777 | |
Benno Schulenberg | 1663994 | 2014-05-03 18:24:45 +0000 | [diff] [blame] | 778 | /* Parse the color name, or pair of color names, in combostr. */ |
| 779 | bool parse_color_names(char *combostr, short *fg, short *bg, bool *bright) |
| 780 | { |
| 781 | bool no_fgcolor = FALSE; |
| 782 | |
| 783 | if (combostr == NULL) |
Chris Allegretta | 61523be | 2014-05-11 03:09:00 +0000 | [diff] [blame] | 784 | return FALSE; |
Benno Schulenberg | 1663994 | 2014-05-03 18:24:45 +0000 | [diff] [blame] | 785 | |
| 786 | if (strchr(combostr, ',') != NULL) { |
| 787 | char *bgcolorname; |
| 788 | strtok(combostr, ","); |
| 789 | bgcolorname = strtok(NULL, ","); |
| 790 | if (bgcolorname == NULL) { |
| 791 | /* If we have a background color without a foreground color, |
| 792 | * parse it properly. */ |
| 793 | bgcolorname = combostr + 1; |
| 794 | no_fgcolor = TRUE; |
| 795 | } |
| 796 | if (strncasecmp(bgcolorname, "bright", 6) == 0) { |
| 797 | rcfile_error(N_("Background color \"%s\" cannot be bright"), bgcolorname); |
Chris Allegretta | 61523be | 2014-05-11 03:09:00 +0000 | [diff] [blame] | 798 | return FALSE; |
Benno Schulenberg | 1663994 | 2014-05-03 18:24:45 +0000 | [diff] [blame] | 799 | } |
| 800 | *bg = color_to_short(bgcolorname, bright); |
| 801 | } else |
| 802 | *bg = -1; |
| 803 | |
| 804 | if (!no_fgcolor) { |
| 805 | *fg = color_to_short(combostr, bright); |
| 806 | |
| 807 | /* Don't try to parse screwed-up foreground colors. */ |
| 808 | if (*fg == -1) |
Chris Allegretta | 61523be | 2014-05-11 03:09:00 +0000 | [diff] [blame] | 809 | return FALSE; |
Benno Schulenberg | 1663994 | 2014-05-03 18:24:45 +0000 | [diff] [blame] | 810 | } else |
| 811 | *fg = -1; |
| 812 | |
Chris Allegretta | 61523be | 2014-05-11 03:09:00 +0000 | [diff] [blame] | 813 | return TRUE; |
Benno Schulenberg | 1663994 | 2014-05-03 18:24:45 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Benno Schulenberg | 1fba31e | 2016-02-28 15:16:27 +0000 | [diff] [blame] | 816 | /* Read regex strings enclosed in double quotes from the line pointed at |
| 817 | * by ptr, and store them quoteless in the passed storage place. */ |
Benno Schulenberg | ed29652 | 2016-03-10 11:00:59 +0000 | [diff] [blame] | 818 | void grab_and_store(const char *kind, char *ptr, regexlisttype **storage) |
Benno Schulenberg | 1fba31e | 2016-02-28 15:16:27 +0000 | [diff] [blame] | 819 | { |
Benno Schulenberg | 07f71e6 | 2016-02-28 16:36:23 +0000 | [diff] [blame] | 820 | regexlisttype *lastthing; |
Chris Allegretta | f30c139 | 2008-09-21 23:02:30 +0000 | [diff] [blame] | 821 | |
Benno Schulenberg | d08d72a | 2016-03-11 16:51:15 +0000 | [diff] [blame] | 822 | if (!opensyntax) { |
| 823 | rcfile_error( |
| 824 | N_("A '%s' command requires a preceding 'syntax' command"), kind); |
| 825 | return; |
| 826 | } |
| 827 | |
Benno Schulenberg | 2994ea9 | 2016-03-10 09:46:21 +0000 | [diff] [blame] | 828 | /* The default syntax doesn't take any file matching stuff. */ |
Benno Schulenberg | 8a5ae21 | 2016-03-10 20:36:12 +0000 | [diff] [blame] | 829 | if (strcmp(live_syntax->name, "default") == 0 && *ptr != '\0') { |
Benno Schulenberg | 2994ea9 | 2016-03-10 09:46:21 +0000 | [diff] [blame] | 830 | rcfile_error( |
| 831 | N_("The \"default\" syntax does not accept '%s' regexes"), kind); |
| 832 | return; |
| 833 | } |
| 834 | |
Chris Allegretta | f30c139 | 2008-09-21 23:02:30 +0000 | [diff] [blame] | 835 | if (*ptr == '\0') { |
Benno Schulenberg | 1fba31e | 2016-02-28 15:16:27 +0000 | [diff] [blame] | 836 | rcfile_error(N_("Missing regex string after '%s' command"), kind); |
Chris Allegretta | f30c139 | 2008-09-21 23:02:30 +0000 | [diff] [blame] | 837 | return; |
| 838 | } |
| 839 | |
Benno Schulenberg | 07f71e6 | 2016-02-28 16:36:23 +0000 | [diff] [blame] | 840 | lastthing = *storage; |
| 841 | |
| 842 | /* If there was an earlier command, go to the last of those regexes. */ |
| 843 | while (lastthing != NULL && lastthing->next != NULL) |
| 844 | lastthing = lastthing->next; |
| 845 | |
Benno Schulenberg | 68d94a0 | 2016-02-28 17:16:39 +0000 | [diff] [blame] | 846 | /* Now gather any valid regexes and add them to the linked list. */ |
Benno Schulenberg | cf4f80d | 2014-05-12 13:52:50 +0000 | [diff] [blame] | 847 | while (*ptr != '\0') { |
| 848 | const char *regexstring; |
Benno Schulenberg | c2148cc | 2016-02-28 15:47:37 +0000 | [diff] [blame] | 849 | regexlisttype *newthing; |
Chris Allegretta | f30c139 | 2008-09-21 23:02:30 +0000 | [diff] [blame] | 850 | |
| 851 | if (*ptr != '"') { |
| 852 | rcfile_error( |
| 853 | N_("Regex strings must begin and end with a \" character")); |
Benno Schulenberg | 3e7591f | 2016-02-28 11:04:36 +0000 | [diff] [blame] | 854 | return; |
Chris Allegretta | f30c139 | 2008-09-21 23:02:30 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Benno Schulenberg | c2148cc | 2016-02-28 15:47:37 +0000 | [diff] [blame] | 857 | regexstring = ++ptr; |
Chris Allegretta | f30c139 | 2008-09-21 23:02:30 +0000 | [diff] [blame] | 858 | ptr = parse_next_regex(ptr); |
| 859 | if (ptr == NULL) |
Benno Schulenberg | c2148cc | 2016-02-28 15:47:37 +0000 | [diff] [blame] | 860 | return; |
Chris Allegretta | f30c139 | 2008-09-21 23:02:30 +0000 | [diff] [blame] | 861 | |
Benno Schulenberg | 68d94a0 | 2016-02-28 17:16:39 +0000 | [diff] [blame] | 862 | /* If the regex string is malformed, skip it. */ |
Benno Schulenberg | 2f817a6 | 2016-03-22 10:42:28 +0000 | [diff] [blame] | 863 | if (!nregcomp(regexstring, NANO_REG_EXTENDED | REG_NOSUB)) |
Benno Schulenberg | 68d94a0 | 2016-02-28 17:16:39 +0000 | [diff] [blame] | 864 | continue; |
| 865 | |
| 866 | /* Copy the regex into a struct, and hook this in at the end. */ |
Benno Schulenberg | c2148cc | 2016-02-28 15:47:37 +0000 | [diff] [blame] | 867 | newthing = (regexlisttype *)nmalloc(sizeof(regexlisttype)); |
Benno Schulenberg | 68d94a0 | 2016-02-28 17:16:39 +0000 | [diff] [blame] | 868 | newthing->full_regex = mallocstrcpy(NULL, regexstring); |
Benno Schulenberg | 68d94a0 | 2016-02-28 17:16:39 +0000 | [diff] [blame] | 869 | newthing->next = NULL; |
Chris Allegretta | f30c139 | 2008-09-21 23:02:30 +0000 | [diff] [blame] | 870 | |
Benno Schulenberg | 68d94a0 | 2016-02-28 17:16:39 +0000 | [diff] [blame] | 871 | if (lastthing == NULL) |
| 872 | *storage = newthing; |
| 873 | else |
| 874 | lastthing->next = newthing; |
Chris Allegretta | f30c139 | 2008-09-21 23:02:30 +0000 | [diff] [blame] | 875 | |
Benno Schulenberg | 68d94a0 | 2016-02-28 17:16:39 +0000 | [diff] [blame] | 876 | lastthing = newthing; |
Chris Allegretta | f30c139 | 2008-09-21 23:02:30 +0000 | [diff] [blame] | 877 | } |
| 878 | } |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 879 | |
Benno Schulenberg | 837b4e6 | 2016-03-10 10:36:49 +0000 | [diff] [blame] | 880 | /* Parse and store the name given after a linter/formatter command. */ |
| 881 | void pick_up_name(const char *kind, char *ptr, char **storage) |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 882 | { |
| 883 | assert(ptr != NULL); |
| 884 | |
Benno Schulenberg | 275e9f0 | 2016-02-28 20:38:14 +0000 | [diff] [blame] | 885 | if (!opensyntax) { |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 886 | rcfile_error( |
Benno Schulenberg | 837b4e6 | 2016-03-10 10:36:49 +0000 | [diff] [blame] | 887 | N_("A '%s' command requires a preceding 'syntax' command"), kind); |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 888 | return; |
| 889 | } |
| 890 | |
| 891 | if (*ptr == '\0') { |
Benno Schulenberg | 837b4e6 | 2016-03-10 10:36:49 +0000 | [diff] [blame] | 892 | rcfile_error(N_("Missing command after '%s'"), kind); |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 893 | return; |
| 894 | } |
| 895 | |
Benno Schulenberg | 837b4e6 | 2016-03-10 10:36:49 +0000 | [diff] [blame] | 896 | free(*storage); |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 897 | |
Benno Schulenberg | 837b4e6 | 2016-03-10 10:36:49 +0000 | [diff] [blame] | 898 | /* Allow unsetting the command by using an empty string. */ |
Chris Allegretta | e52eac5 | 2014-03-02 05:27:56 +0000 | [diff] [blame] | 899 | if (!strcmp(ptr, "\"\"")) |
Benno Schulenberg | 837b4e6 | 2016-03-10 10:36:49 +0000 | [diff] [blame] | 900 | *storage = NULL; |
Mike Scalora | 6a2032f | 2016-05-25 22:13:50 +0200 | [diff] [blame] | 901 | else if (*ptr == '"') { |
| 902 | *storage = mallocstrcpy(NULL, ++ptr); |
| 903 | char* q = *storage; |
| 904 | char* p = *storage; |
| 905 | /* Snip out the backslashes of escaped characters. */ |
| 906 | while (*p != '"') { |
| 907 | if (*p == '\0') { |
| 908 | rcfile_error(N_("Argument of '%s' lacks closing \""), kind); |
| 909 | free(*storage); |
| 910 | *storage = NULL; |
| 911 | return; |
| 912 | } else if (*p == '\\' && *(p + 1) != '\0') { |
| 913 | p++; |
| 914 | } |
| 915 | *q++ = *p++; |
| 916 | } |
| 917 | *q = '\0'; |
| 918 | } |
Chris Allegretta | e52eac5 | 2014-03-02 05:27:56 +0000 | [diff] [blame] | 919 | else |
Benno Schulenberg | 837b4e6 | 2016-03-10 10:36:49 +0000 | [diff] [blame] | 920 | *storage = mallocstrcpy(NULL, ptr); |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 921 | } |
Benno Schulenberg | 0038992 | 2014-04-04 11:59:03 +0000 | [diff] [blame] | 922 | #endif /* !DISABLE_COLOR */ |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 923 | |
Benno Schulenberg | 77023a7 | 2016-11-27 16:34:34 +0100 | [diff] [blame] | 924 | /* Verify that the user has not unmapped every shortcut for a |
| 925 | * function that we consider 'vital' (such as "Exit"). */ |
Chris Allegretta | 90ee8ee | 2008-03-20 04:45:55 +0000 | [diff] [blame] | 926 | static void check_vitals_mapped(void) |
| 927 | { |
| 928 | subnfunc *f; |
| 929 | int v; |
| 930 | #define VITALS 5 |
Chris Allegretta | 637daa8 | 2011-02-07 14:45:56 +0000 | [diff] [blame] | 931 | void (*vitals[VITALS])(void) = { do_exit, do_exit, do_cancel, do_cancel, do_cancel }; |
Chris Allegretta | 90ee8ee | 2008-03-20 04:45:55 +0000 | [diff] [blame] | 932 | int inmenus[VITALS] = { MMAIN, MHELP, MWHEREIS, MREPLACE, MGOTOLINE }; |
| 933 | |
| 934 | for (v = 0; v < VITALS; v++) { |
Benno Schulenberg | 492e9f6 | 2014-06-20 10:48:26 +0000 | [diff] [blame] | 935 | for (f = allfuncs; f != NULL; f = f->next) { |
| 936 | if (f->scfunc == vitals[v] && f->menus & inmenus[v]) { |
| 937 | const sc *s = first_sc_for(inmenus[v], f->scfunc); |
| 938 | if (!s) { |
| 939 | fprintf(stderr, _("Fatal error: no keys mapped for function " |
Benno Schulenberg | cdcd365 | 2016-05-17 11:33:21 +0200 | [diff] [blame] | 940 | "\"%s\". Exiting.\n"), f->desc); |
Benno Schulenberg | 492e9f6 | 2014-06-20 10:48:26 +0000 | [diff] [blame] | 941 | fprintf(stderr, _("If needed, use nano with the -I option " |
Benno Schulenberg | cdcd365 | 2016-05-17 11:33:21 +0200 | [diff] [blame] | 942 | "to adjust your nanorc settings.\n")); |
| 943 | exit(1); |
Benno Schulenberg | 492e9f6 | 2014-06-20 10:48:26 +0000 | [diff] [blame] | 944 | } |
| 945 | break; |
| 946 | } |
| 947 | } |
Chris Allegretta | 90ee8ee | 2008-03-20 04:45:55 +0000 | [diff] [blame] | 948 | } |
| 949 | } |
| 950 | |
David Lawrence Ramsey | 8f07329 | 2006-06-01 20:23:24 +0000 | [diff] [blame] | 951 | /* Parse the rcfile, once it has been opened successfully at rcstream, |
Benno Schulenberg | 77023a7 | 2016-11-27 16:34:34 +0100 | [diff] [blame] | 952 | * and close it afterwards. If syntax_only is TRUE, allow the file to |
| 953 | * to contain only color syntax commands. */ |
| 954 | void parse_rcfile(FILE *rcstream, bool syntax_only) |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 955 | { |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 956 | char *buf = NULL; |
| 957 | ssize_t len; |
Chris Allegretta | dcf5cbb | 2009-11-27 05:09:56 +0000 | [diff] [blame] | 958 | size_t n = 0; |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 959 | |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 960 | while ((len = getline(&buf, &n, rcstream)) > 0) { |
| 961 | char *ptr, *keyword, *option; |
David Lawrence Ramsey | db39a5e | 2006-05-26 13:38:49 +0000 | [diff] [blame] | 962 | int set = 0; |
| 963 | size_t i; |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 964 | |
David Lawrence Ramsey | 03ee332 | 2006-04-12 22:03:47 +0000 | [diff] [blame] | 965 | /* Ignore the newline. */ |
David Lawrence Ramsey | 5566e44 | 2006-05-26 13:49:00 +0000 | [diff] [blame] | 966 | if (buf[len - 1] == '\n') |
| 967 | buf[len - 1] = '\0'; |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 968 | |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 969 | lineno++; |
| 970 | ptr = buf; |
David Lawrence Ramsey | d864048 | 2005-06-12 17:48:46 +0000 | [diff] [blame] | 971 | while (isblank(*ptr)) |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 972 | ptr++; |
| 973 | |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 974 | /* If we have a blank line or a comment, skip to the next |
| 975 | * line. */ |
| 976 | if (*ptr == '\0' || *ptr == '#') |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 977 | continue; |
| 978 | |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 979 | /* Otherwise, skip to the next space. */ |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 980 | keyword = ptr; |
| 981 | ptr = parse_next_word(ptr); |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 982 | |
Benno Schulenberg | 0038992 | 2014-04-04 11:59:03 +0000 | [diff] [blame] | 983 | #ifndef DISABLE_COLOR |
Chris Allegretta | e52eac5 | 2014-03-02 05:27:56 +0000 | [diff] [blame] | 984 | /* Handle extending first... */ |
| 985 | if (strcasecmp(keyword, "extendsyntax") == 0) { |
Benno Schulenberg | 04669b5 | 2016-02-29 12:17:53 +0000 | [diff] [blame] | 986 | syntaxtype *sint; |
Chris Allegretta | e52eac5 | 2014-03-02 05:27:56 +0000 | [diff] [blame] | 987 | char *syntaxname = ptr; |
Chris Allegretta | e52eac5 | 2014-03-02 05:27:56 +0000 | [diff] [blame] | 988 | |
| 989 | ptr = parse_next_word(ptr); |
Benno Schulenberg | 04669b5 | 2016-02-29 12:17:53 +0000 | [diff] [blame] | 990 | |
| 991 | for (sint = syntaxes; sint != NULL; sint = sint->next) |
| 992 | if (!strcmp(sint->name, syntaxname)) |
Chris Allegretta | e52eac5 | 2014-03-02 05:27:56 +0000 | [diff] [blame] | 993 | break; |
| 994 | |
Benno Schulenberg | 04669b5 | 2016-02-29 12:17:53 +0000 | [diff] [blame] | 995 | if (sint == NULL) { |
| 996 | rcfile_error(N_("Could not find syntax \"%s\" to extend"), |
| 997 | syntaxname); |
Benno Schulenberg | 85e166b | 2016-02-29 12:04:22 +0000 | [diff] [blame] | 998 | opensyntax = FALSE; |
Chris Allegretta | e52eac5 | 2014-03-02 05:27:56 +0000 | [diff] [blame] | 999 | continue; |
Chris Allegretta | e52eac5 | 2014-03-02 05:27:56 +0000 | [diff] [blame] | 1000 | } |
Benno Schulenberg | b8aae4d | 2016-03-11 16:39:27 +0000 | [diff] [blame] | 1001 | |
| 1002 | live_syntax = sint; |
| 1003 | opensyntax = TRUE; |
| 1004 | |
Benno Schulenberg | 63d5965 | 2016-03-12 11:15:12 +0000 | [diff] [blame] | 1005 | /* Refind the tail of the color list for this syntax. */ |
Benno Schulenberg | 8fbb922 | 2016-03-13 20:05:36 +0000 | [diff] [blame] | 1006 | lastcolor = sint->color; |
| 1007 | if (lastcolor != NULL) |
| 1008 | while (lastcolor->next != NULL) |
| 1009 | lastcolor = lastcolor->next; |
Benno Schulenberg | 63d5965 | 2016-03-12 11:15:12 +0000 | [diff] [blame] | 1010 | |
Benno Schulenberg | b8aae4d | 2016-03-11 16:39:27 +0000 | [diff] [blame] | 1011 | keyword = ptr; |
| 1012 | ptr = parse_next_word(ptr); |
Chris Allegretta | e52eac5 | 2014-03-02 05:27:56 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 1015 | /* Try to parse the keyword. */ |
Benno Schulenberg | 9330aa6 | 2016-04-27 10:07:45 +0200 | [diff] [blame] | 1016 | if (strcasecmp(keyword, "syntax") == 0) { |
Benno Schulenberg | 8fbb922 | 2016-03-13 20:05:36 +0000 | [diff] [blame] | 1017 | if (opensyntax && lastcolor == NULL) |
David Lawrence Ramsey | 8d2d0d9 | 2006-05-28 19:00:16 +0000 | [diff] [blame] | 1018 | rcfile_error(N_("Syntax \"%s\" has no color commands"), |
Benno Schulenberg | 8a5ae21 | 2016-03-10 20:36:12 +0000 | [diff] [blame] | 1019 | live_syntax->name); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1020 | parse_syntax(ptr); |
Chris Allegretta | b00d0b9 | 2011-02-13 04:23:10 +0000 | [diff] [blame] | 1021 | } |
Benno Schulenberg | b8aae4d | 2016-03-11 16:39:27 +0000 | [diff] [blame] | 1022 | else if (strcasecmp(keyword, "header") == 0) |
| 1023 | grab_and_store("header", ptr, &live_syntax->headers); |
Benno Schulenberg | a997aa6 | 2014-03-26 19:25:38 +0000 | [diff] [blame] | 1024 | else if (strcasecmp(keyword, "magic") == 0) |
Benno Schulenberg | d7ade1f | 2015-04-03 17:28:30 +0000 | [diff] [blame] | 1025 | #ifdef HAVE_LIBMAGIC |
Benno Schulenberg | 8a5ae21 | 2016-03-10 20:36:12 +0000 | [diff] [blame] | 1026 | grab_and_store("magic", ptr, &live_syntax->magics); |
Benno Schulenberg | d7ade1f | 2015-04-03 17:28:30 +0000 | [diff] [blame] | 1027 | #else |
| 1028 | ; |
| 1029 | #endif |
Mike Scalora | 6a2032f | 2016-05-25 22:13:50 +0200 | [diff] [blame] | 1030 | else if (strcasecmp(keyword, "comment") == 0) |
| 1031 | #ifdef ENABLE_COMMENT |
| 1032 | pick_up_name("comment", ptr, &live_syntax->comment); |
| 1033 | #else |
| 1034 | ; |
| 1035 | #endif |
Chris Allegretta | f30c139 | 2008-09-21 23:02:30 +0000 | [diff] [blame] | 1036 | else if (strcasecmp(keyword, "color") == 0) |
Benno Schulenberg | 2f817a6 | 2016-03-22 10:42:28 +0000 | [diff] [blame] | 1037 | parse_colors(ptr, NANO_REG_EXTENDED); |
David Lawrence Ramsey | 23555f2 | 2005-06-27 03:07:10 +0000 | [diff] [blame] | 1038 | else if (strcasecmp(keyword, "icolor") == 0) |
Benno Schulenberg | 2f817a6 | 2016-03-22 10:42:28 +0000 | [diff] [blame] | 1039 | parse_colors(ptr, NANO_REG_EXTENDED | REG_ICASE); |
Benno Schulenberg | a997aa6 | 2014-03-26 19:25:38 +0000 | [diff] [blame] | 1040 | else if (strcasecmp(keyword, "linter") == 0) |
Benno Schulenberg | 8a5ae21 | 2016-03-10 20:36:12 +0000 | [diff] [blame] | 1041 | pick_up_name("linter", ptr, &live_syntax->linter); |
Chris Allegretta | 4b3f277 | 2015-01-03 07:24:17 +0000 | [diff] [blame] | 1042 | else if (strcasecmp(keyword, "formatter") == 0) |
Benno Schulenberg | d7ade1f | 2015-04-03 17:28:30 +0000 | [diff] [blame] | 1043 | #ifndef DISABLE_SPELLER |
Benno Schulenberg | 8a5ae21 | 2016-03-10 20:36:12 +0000 | [diff] [blame] | 1044 | pick_up_name("formatter", ptr, &live_syntax->formatter); |
Benno Schulenberg | d7ade1f | 2015-04-03 17:28:30 +0000 | [diff] [blame] | 1045 | #else |
| 1046 | ; |
| 1047 | #endif |
Benno Schulenberg | 9330aa6 | 2016-04-27 10:07:45 +0200 | [diff] [blame] | 1048 | else if (syntax_only) |
| 1049 | rcfile_error(N_("Command \"%s\" not allowed in included file"), |
| 1050 | keyword); |
| 1051 | else if (strcasecmp(keyword, "include") == 0) |
| 1052 | parse_includes(ptr); |
| 1053 | else |
Benno Schulenberg | 0038992 | 2014-04-04 11:59:03 +0000 | [diff] [blame] | 1054 | #endif /* !DISABLE_COLOR */ |
Benno Schulenberg | 9330aa6 | 2016-04-27 10:07:45 +0200 | [diff] [blame] | 1055 | if (strcasecmp(keyword, "set") == 0) |
| 1056 | set = 1; |
| 1057 | else if (strcasecmp(keyword, "unset") == 0) |
| 1058 | set = -1; |
Chris Allegretta | 79a33bb | 2008-03-05 07:34:01 +0000 | [diff] [blame] | 1059 | else if (strcasecmp(keyword, "bind") == 0) |
Benno Schulenberg | 2cdaaac | 2014-04-08 11:22:41 +0000 | [diff] [blame] | 1060 | parse_binding(ptr, TRUE); |
Chris Allegretta | e9dee88 | 2009-11-21 16:26:59 +0000 | [diff] [blame] | 1061 | else if (strcasecmp(keyword, "unbind") == 0) |
Benno Schulenberg | 2cdaaac | 2014-04-08 11:22:41 +0000 | [diff] [blame] | 1062 | parse_binding(ptr, FALSE); |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 1063 | else |
David Lawrence Ramsey | 8d2d0d9 | 2006-05-28 19:00:16 +0000 | [diff] [blame] | 1064 | rcfile_error(N_("Command \"%s\" not understood"), keyword); |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 1065 | |
Benno Schulenberg | 0038992 | 2014-04-04 11:59:03 +0000 | [diff] [blame] | 1066 | #ifndef DISABLE_COLOR |
Benno Schulenberg | 8a5ae21 | 2016-03-10 20:36:12 +0000 | [diff] [blame] | 1067 | /* If a syntax was extended, it stops at the end of the command. */ |
| 1068 | if (live_syntax != syntaxes) |
Benno Schulenberg | 85e166b | 2016-02-29 12:04:22 +0000 | [diff] [blame] | 1069 | opensyntax = FALSE; |
Chris Allegretta | e52eac5 | 2014-03-02 05:27:56 +0000 | [diff] [blame] | 1070 | #endif |
| 1071 | |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 1072 | if (set == 0) |
| 1073 | continue; |
| 1074 | |
| 1075 | if (*ptr == '\0') { |
Benno Schulenberg | 2f00c9d | 2014-04-07 20:38:29 +0000 | [diff] [blame] | 1076 | rcfile_error(N_("Missing option")); |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 1077 | continue; |
| 1078 | } |
| 1079 | |
| 1080 | option = ptr; |
| 1081 | ptr = parse_next_word(ptr); |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 1082 | |
Benno Schulenberg | 8866f72 | 2016-03-14 17:14:35 +0000 | [diff] [blame] | 1083 | /* Find the just read name among the existing options. */ |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 1084 | for (i = 0; rcopts[i].name != NULL; i++) { |
Benno Schulenberg | 8866f72 | 2016-03-14 17:14:35 +0000 | [diff] [blame] | 1085 | if (strcasecmp(option, rcopts[i].name) == 0) |
| 1086 | break; |
| 1087 | } |
| 1088 | |
| 1089 | if (rcopts[i].name == NULL) { |
| 1090 | rcfile_error(N_("Unknown option \"%s\""), option); |
| 1091 | continue; |
| 1092 | } |
| 1093 | |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 1094 | #ifdef DEBUG |
Benno Schulenberg | d88423e | 2016-07-13 20:21:50 +0200 | [diff] [blame] | 1095 | fprintf(stderr, " Option name = \"%s\"\n", rcopts[i].name); |
| 1096 | fprintf(stderr, " Flag = %ld\n", rcopts[i].flag); |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 1097 | #endif |
Benno Schulenberg | 8866f72 | 2016-03-14 17:14:35 +0000 | [diff] [blame] | 1098 | /* First handle unsetting. */ |
| 1099 | if (set == -1) { |
Benno Schulenberg | cdcd365 | 2016-05-17 11:33:21 +0200 | [diff] [blame] | 1100 | if (rcopts[i].flag != 0) |
Benno Schulenberg | 8866f72 | 2016-03-14 17:14:35 +0000 | [diff] [blame] | 1101 | UNSET(rcopts[i].flag); |
Benno Schulenberg | cdcd365 | 2016-05-17 11:33:21 +0200 | [diff] [blame] | 1102 | else |
Benno Schulenberg | 8866f72 | 2016-03-14 17:14:35 +0000 | [diff] [blame] | 1103 | rcfile_error(N_("Cannot unset option \"%s\""), rcopts[i].name); |
| 1104 | continue; |
| 1105 | } |
| 1106 | |
| 1107 | /* If the option has a flag, it doesn't take an argument. */ |
| 1108 | if (rcopts[i].flag != 0) { |
| 1109 | SET(rcopts[i].flag); |
| 1110 | continue; |
| 1111 | } |
| 1112 | |
| 1113 | /* The option doesn't have a flag, so it takes an argument. */ |
| 1114 | if (*ptr == '\0') { |
| 1115 | rcfile_error(N_("Option \"%s\" requires an argument"), |
David Lawrence Ramsey | 08c70ae | 2005-03-14 17:47:17 +0000 | [diff] [blame] | 1116 | rcopts[i].name); |
Benno Schulenberg | 8866f72 | 2016-03-14 17:14:35 +0000 | [diff] [blame] | 1117 | continue; |
| 1118 | } |
David Lawrence Ramsey | 7bf86e1 | 2005-06-08 19:50:02 +0000 | [diff] [blame] | 1119 | |
Benno Schulenberg | 8866f72 | 2016-03-14 17:14:35 +0000 | [diff] [blame] | 1120 | option = ptr; |
| 1121 | if (*option == '"') |
| 1122 | option++; |
| 1123 | ptr = parse_argument(ptr); |
| 1124 | |
| 1125 | option = mallocstrcpy(NULL, option); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1126 | #ifdef DEBUG |
Benno Schulenberg | d88423e | 2016-07-13 20:21:50 +0200 | [diff] [blame] | 1127 | fprintf(stderr, " Option argument = \"%s\"\n", option); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1128 | #endif |
Benno Schulenberg | 8866f72 | 2016-03-14 17:14:35 +0000 | [diff] [blame] | 1129 | /* Make sure the option argument is a valid multibyte string. */ |
| 1130 | if (!is_valid_mbstring(option)) { |
Benno Schulenberg | ecccb8d | 2016-12-07 21:01:32 +0100 | [diff] [blame] | 1131 | rcfile_error(N_("Argument is not a valid multibyte string")); |
Benno Schulenberg | 8866f72 | 2016-03-14 17:14:35 +0000 | [diff] [blame] | 1132 | continue; |
| 1133 | } |
David Lawrence Ramsey | 30d0a81 | 2005-06-13 14:50:32 +0000 | [diff] [blame] | 1134 | |
Benno Schulenberg | 1663994 | 2014-05-03 18:24:45 +0000 | [diff] [blame] | 1135 | #ifndef DISABLE_COLOR |
Benno Schulenberg | 8866f72 | 2016-03-14 17:14:35 +0000 | [diff] [blame] | 1136 | if (strcasecmp(rcopts[i].name, "titlecolor") == 0) |
| 1137 | specified_color_combo[TITLE_BAR] = option; |
Benno Schulenberg | de2aa4f | 2016-10-20 10:07:48 +0200 | [diff] [blame] | 1138 | else if (strcasecmp(rcopts[i].name, "numbercolor") == 0) |
| 1139 | specified_color_combo[LINE_NUMBER] = option; |
Benno Schulenberg | 8866f72 | 2016-03-14 17:14:35 +0000 | [diff] [blame] | 1140 | else if (strcasecmp(rcopts[i].name, "statuscolor") == 0) |
| 1141 | specified_color_combo[STATUS_BAR] = option; |
| 1142 | else if (strcasecmp(rcopts[i].name, "keycolor") == 0) |
| 1143 | specified_color_combo[KEY_COMBO] = option; |
| 1144 | else if (strcasecmp(rcopts[i].name, "functioncolor") == 0) |
| 1145 | specified_color_combo[FUNCTION_TAG] = option; |
| 1146 | else |
Benno Schulenberg | 1663994 | 2014-05-03 18:24:45 +0000 | [diff] [blame] | 1147 | #endif |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1148 | #ifndef DISABLE_OPERATINGDIR |
Benno Schulenberg | 8866f72 | 2016-03-14 17:14:35 +0000 | [diff] [blame] | 1149 | if (strcasecmp(rcopts[i].name, "operatingdir") == 0) |
| 1150 | operating_dir = option; |
| 1151 | else |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1152 | #endif |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 1153 | #ifndef DISABLE_WRAPJUSTIFY |
Benno Schulenberg | 8866f72 | 2016-03-14 17:14:35 +0000 | [diff] [blame] | 1154 | if (strcasecmp(rcopts[i].name, "fill") == 0) { |
| 1155 | if (!parse_num(option, &wrap_at)) { |
| 1156 | rcfile_error(N_("Requested fill size \"%s\" is invalid"), |
| 1157 | option); |
| 1158 | wrap_at = -CHARS_FROM_EOL; |
Benno Schulenberg | eca6fae | 2017-01-11 09:49:24 +0100 | [diff] [blame] | 1159 | } else |
Benno Schulenberg | 65bf36b | 2016-11-13 19:47:15 +0100 | [diff] [blame] | 1160 | UNSET(NO_WRAP); |
Benno Schulenberg | eca6fae | 2017-01-11 09:49:24 +0100 | [diff] [blame] | 1161 | free(option); |
Benno Schulenberg | 8866f72 | 2016-03-14 17:14:35 +0000 | [diff] [blame] | 1162 | } else |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 1163 | #endif |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1164 | #ifndef NANO_TINY |
Benno Schulenberg | 8866f72 | 2016-03-14 17:14:35 +0000 | [diff] [blame] | 1165 | if (strcasecmp(rcopts[i].name, "matchbrackets") == 0) { |
| 1166 | matchbrackets = option; |
| 1167 | if (has_blank_mbchars(matchbrackets)) { |
| 1168 | rcfile_error(N_("Non-blank characters required")); |
| 1169 | free(matchbrackets); |
| 1170 | matchbrackets = NULL; |
| 1171 | } |
| 1172 | } else if (strcasecmp(rcopts[i].name, "whitespace") == 0) { |
| 1173 | whitespace = option; |
| 1174 | if (mbstrlen(whitespace) != 2 || strlenpt(whitespace) != 2) { |
| 1175 | rcfile_error(N_("Two single-column characters required")); |
| 1176 | free(whitespace); |
| 1177 | whitespace = NULL; |
| 1178 | } else { |
| 1179 | whitespace_len[0] = parse_mbchar(whitespace, NULL, NULL); |
| 1180 | whitespace_len[1] = parse_mbchar(whitespace + |
David Lawrence Ramsey | 96452cb | 2005-07-26 06:13:45 +0000 | [diff] [blame] | 1181 | whitespace_len[0], NULL, NULL); |
Benno Schulenberg | 8866f72 | 2016-03-14 17:14:35 +0000 | [diff] [blame] | 1182 | } |
| 1183 | } else |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 1184 | #endif |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 1185 | #ifndef DISABLE_JUSTIFY |
Benno Schulenberg | 8866f72 | 2016-03-14 17:14:35 +0000 | [diff] [blame] | 1186 | if (strcasecmp(rcopts[i].name, "punct") == 0) { |
| 1187 | punct = option; |
| 1188 | if (has_blank_mbchars(punct)) { |
| 1189 | rcfile_error(N_("Non-blank characters required")); |
| 1190 | free(punct); |
| 1191 | punct = NULL; |
| 1192 | } |
| 1193 | } else if (strcasecmp(rcopts[i].name, "brackets") == 0) { |
| 1194 | brackets = option; |
| 1195 | if (has_blank_mbchars(brackets)) { |
| 1196 | rcfile_error(N_("Non-blank characters required")); |
| 1197 | free(brackets); |
| 1198 | brackets = NULL; |
| 1199 | } |
| 1200 | } else if (strcasecmp(rcopts[i].name, "quotestr") == 0) |
| 1201 | quotestr = option; |
| 1202 | else |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 1203 | #endif |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1204 | #ifndef NANO_TINY |
Benno Schulenberg | 8866f72 | 2016-03-14 17:14:35 +0000 | [diff] [blame] | 1205 | if (strcasecmp(rcopts[i].name, "backupdir") == 0) |
| 1206 | backup_dir = option; |
| 1207 | else |
Benno Schulenberg | 6f12992 | 2016-06-30 18:02:45 +0200 | [diff] [blame] | 1208 | if (strcasecmp(rcopts[i].name, "wordchars") == 0) |
| 1209 | word_chars = option; |
| 1210 | else |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 1211 | #endif |
| 1212 | #ifndef DISABLE_SPELLER |
Benno Schulenberg | 8866f72 | 2016-03-14 17:14:35 +0000 | [diff] [blame] | 1213 | if (strcasecmp(rcopts[i].name, "speller") == 0) |
| 1214 | alt_speller = option; |
| 1215 | else |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 1216 | #endif |
Benno Schulenberg | 8866f72 | 2016-03-14 17:14:35 +0000 | [diff] [blame] | 1217 | if (strcasecmp(rcopts[i].name, "tabsize") == 0) { |
| 1218 | if (!parse_num(option, &tabsize) || tabsize <= 0) { |
| 1219 | rcfile_error(N_("Requested tab size \"%s\" is invalid"), |
| 1220 | option); |
| 1221 | tabsize = -1; |
Benno Schulenberg | eca6fae | 2017-01-11 09:49:24 +0100 | [diff] [blame] | 1222 | } |
| 1223 | free(option); |
Benno Schulenberg | 8866f72 | 2016-03-14 17:14:35 +0000 | [diff] [blame] | 1224 | } else |
| 1225 | assert(FALSE); |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 1226 | } |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 1227 | |
Benno Schulenberg | 0038992 | 2014-04-04 11:59:03 +0000 | [diff] [blame] | 1228 | #ifndef DISABLE_COLOR |
Benno Schulenberg | 8fbb922 | 2016-03-13 20:05:36 +0000 | [diff] [blame] | 1229 | if (opensyntax && lastcolor == NULL) |
David Lawrence Ramsey | 8d2d0d9 | 2006-05-28 19:00:16 +0000 | [diff] [blame] | 1230 | rcfile_error(N_("Syntax \"%s\" has no color commands"), |
Benno Schulenberg | 8a5ae21 | 2016-03-10 20:36:12 +0000 | [diff] [blame] | 1231 | live_syntax->name); |
David Lawrence Ramsey | 0b4920a | 2006-05-26 12:56:30 +0000 | [diff] [blame] | 1232 | |
Benno Schulenberg | 275e9f0 | 2016-02-28 20:38:14 +0000 | [diff] [blame] | 1233 | opensyntax = FALSE; |
Benno Schulenberg | 1786484 | 2016-03-11 17:14:30 +0000 | [diff] [blame] | 1234 | #endif |
Benno Schulenberg | 275e9f0 | 2016-02-28 20:38:14 +0000 | [diff] [blame] | 1235 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1236 | free(buf); |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 1237 | fclose(rcstream); |
| 1238 | lineno = 0; |
David Lawrence Ramsey | a6d26d0 | 2004-07-30 22:52:44 +0000 | [diff] [blame] | 1239 | |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 1240 | return; |
| 1241 | } |
| 1242 | |
Benno Schulenberg | c1a4842 | 2016-11-27 18:21:04 +0100 | [diff] [blame] | 1243 | /* Read and interpret one of the two nanorc files. */ |
| 1244 | void parse_one_nanorc(void) |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 1245 | { |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 1246 | FILE *rcstream; |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 1247 | |
Benno Schulenberg | c1a4842 | 2016-11-27 18:21:04 +0100 | [diff] [blame] | 1248 | /* Don't try to open directories nor devices. */ |
Benno Schulenberg | 981a1d3 | 2016-11-27 17:27:04 +0100 | [diff] [blame] | 1249 | if (!is_good_file(nanorc)) |
Benno Schulenberg | c1a4842 | 2016-11-27 18:21:04 +0100 | [diff] [blame] | 1250 | return; |
David Lawrence Ramsey | 5fac171 | 2006-04-12 21:44:07 +0000 | [diff] [blame] | 1251 | |
Chris Allegretta | cc60c3a | 2008-03-20 05:41:00 +0000 | [diff] [blame] | 1252 | #ifdef DEBUG |
Benno Schulenberg | c1a4842 | 2016-11-27 18:21:04 +0100 | [diff] [blame] | 1253 | fprintf(stderr, "Going to parse file \"%s\"\n", nanorc); |
Chris Allegretta | cc60c3a | 2008-03-20 05:41:00 +0000 | [diff] [blame] | 1254 | #endif |
| 1255 | |
David Lawrence Ramsey | 979de23 | 2006-04-05 21:25:47 +0000 | [diff] [blame] | 1256 | rcstream = fopen(nanorc, "rb"); |
Benno Schulenberg | c1a4842 | 2016-11-27 18:21:04 +0100 | [diff] [blame] | 1257 | |
| 1258 | /* If opening the file succeeded, parse it. Otherwise, only |
| 1259 | * complain if the file actually exists. */ |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 1260 | if (rcstream != NULL) |
Benno Schulenberg | 77023a7 | 2016-11-27 16:34:34 +0100 | [diff] [blame] | 1261 | parse_rcfile(rcstream, FALSE); |
Benno Schulenberg | c1a4842 | 2016-11-27 18:21:04 +0100 | [diff] [blame] | 1262 | else if (errno != ENOENT) |
| 1263 | rcfile_error(N_("Error reading %s: %s"), nanorc, strerror(errno)); |
| 1264 | } |
| 1265 | |
| 1266 | /* First read the system-wide rcfile, then the user's rcfile. */ |
| 1267 | void do_rcfiles(void) |
| 1268 | { |
| 1269 | nanorc = mallocstrcpy(nanorc, SYSCONFDIR "/nanorc"); |
| 1270 | |
| 1271 | /* Process the system-wide nanorc. */ |
| 1272 | parse_one_nanorc(); |
Chris Allegretta | ff8a68c | 2002-02-16 20:34:57 +0000 | [diff] [blame] | 1273 | |
Benno Schulenberg | 77023a7 | 2016-11-27 16:34:34 +0100 | [diff] [blame] | 1274 | /* When configured with --disable-wrapping-as-root, turn wrapping off |
| 1275 | * for root, so that only root's .nanorc or --fill can turn it on. */ |
David Lawrence Ramsey | e53e125 | 2006-07-19 15:50:19 +0000 | [diff] [blame] | 1276 | #ifdef DISABLE_ROOTWRAPPING |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 1277 | if (geteuid() == NANO_ROOT_UID) |
| 1278 | SET(NO_WRAP); |
| 1279 | #endif |
Chris Allegretta | 4dc03d5 | 2002-05-11 03:04:44 +0000 | [diff] [blame] | 1280 | |
David Lawrence Ramsey | 65e6ecb | 2005-02-08 20:37:53 +0000 | [diff] [blame] | 1281 | get_homedir(); |
Chris Allegretta | 2e39c1c | 2003-02-13 03:36:15 +0000 | [diff] [blame] | 1282 | |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 1283 | if (homedir == NULL) |
David Lawrence Ramsey | a27bd65 | 2004-08-17 05:23:38 +0000 | [diff] [blame] | 1284 | rcfile_error(N_("I can't find my home directory! Wah!")); |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 1285 | else { |
Chris Allegretta | faeeb5b | 2008-08-28 06:13:05 +0000 | [diff] [blame] | 1286 | nanorc = charealloc(nanorc, strlen(homedir) + strlen(RCFILE_NAME) + 2); |
| 1287 | sprintf(nanorc, "%s/%s", homedir, RCFILE_NAME); |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 1288 | |
Benno Schulenberg | c1a4842 | 2016-11-27 18:21:04 +0100 | [diff] [blame] | 1289 | /* Process the current user's nanorc. */ |
| 1290 | parse_one_nanorc(); |
Chris Allegretta | 4dc03d5 | 2002-05-11 03:04:44 +0000 | [diff] [blame] | 1291 | } |
David Lawrence Ramsey | 9adace8 | 2005-03-04 15:09:55 +0000 | [diff] [blame] | 1292 | |
Benno Schulenberg | 925a001 | 2016-11-27 16:40:54 +0100 | [diff] [blame] | 1293 | check_vitals_mapped(); |
| 1294 | |
David Lawrence Ramsey | 202d3c2 | 2005-03-10 20:55:11 +0000 | [diff] [blame] | 1295 | free(nanorc); |
Chris Allegretta | 4dc03d5 | 2002-05-11 03:04:44 +0000 | [diff] [blame] | 1296 | |
Chris Allegretta | a30eb78 | 2009-02-09 04:03:20 +0000 | [diff] [blame] | 1297 | if (errors && !ISSET(QUIET)) { |
| 1298 | errors = FALSE; |
Benno Schulenberg | b8aae4d | 2016-03-11 16:39:27 +0000 | [diff] [blame] | 1299 | fprintf(stderr, _("\nPress Enter to continue starting nano.\n")); |
Chris Allegretta | a30eb78 | 2009-02-09 04:03:20 +0000 | [diff] [blame] | 1300 | while (getchar() != '\n') |
| 1301 | ; |
| 1302 | } |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
Benno Schulenberg | eea0908 | 2014-04-13 20:50:20 +0000 | [diff] [blame] | 1305 | #endif /* !DISABLE_NANORC */ |