Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /************************************************************************** |
Chris Allegretta | a943480 | 2001-05-05 15:02:27 +0000 | [diff] [blame] | 3 | * rcfile.c * |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 4 | * * |
Jordi Mallach | 8ae5789 | 2002-01-04 17:57:40 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2002 Chris Allegretta * |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
Chris Allegretta | 3a24f3f | 2001-10-24 11:33:54 +0000 | [diff] [blame] | 8 | * the Free Software Foundation; either version 2, or (at your option) * |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 9 | * any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, write to the Free Software * |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * |
| 19 | * * |
| 20 | **************************************************************************/ |
| 21 | |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 22 | #include "config.h" |
| 23 | |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 24 | #include <stdlib.h> |
Chris Allegretta | 34f8098 | 2002-01-22 20:09:20 +0000 | [diff] [blame] | 25 | #include <stdarg.h> |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 26 | #include <string.h> |
| 27 | #include <stdio.h> |
| 28 | #include <errno.h> |
Chris Allegretta | 4dc03d5 | 2002-05-11 03:04:44 +0000 | [diff] [blame] | 29 | #include <unistd.h> |
| 30 | #include <pwd.h> |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 31 | #include <sys/types.h> |
| 32 | #include <sys/stat.h> |
| 33 | #include <fcntl.h> |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 34 | #include <assert.h> |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 35 | #include "proto.h" |
| 36 | #include "nano.h" |
| 37 | |
| 38 | #ifdef ENABLE_NANORC |
| 39 | |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 40 | #ifdef ENABLE_NLS |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 41 | #include <libintl.h> |
| 42 | #define _(string) gettext(string) |
| 43 | #else |
| 44 | #define _(string) (string) |
| 45 | #endif |
| 46 | |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 47 | const static rcoption rcopts[] = { |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 48 | #ifndef NANO_SMALL |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 49 | {"autoindent", AUTOINDENT}, |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 50 | {"backup", BACKUP_FILE}, |
| 51 | #endif |
| 52 | {"const", CONSTUPDATE}, |
| 53 | #ifndef NANO_SMALL |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 54 | {"cut", CUT_TO_END}, |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 55 | #endif |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 56 | #ifndef DISABLE_WRAPJUSTIFY |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 57 | {"fill", 0}, |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 58 | #endif |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 59 | {"keypad", ALT_KEYPAD}, |
| 60 | #ifndef DISABLE_MOUSE |
| 61 | {"mouse", USE_MOUSE}, |
| 62 | #endif |
| 63 | #ifdef ENABLE_MULTIBUFFER |
| 64 | {"multibuffer", MULTIBUFFER}, |
| 65 | #endif |
| 66 | #ifndef NANO_SMALL |
| 67 | {"noconvert", NO_CONVERT}, |
| 68 | #endif |
| 69 | {"nofollow", FOLLOW_SYMLINKS}, |
| 70 | {"nohelp", NO_HELP}, |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 71 | #ifndef DISABLE_WRAPPING |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 72 | {"nowrap", NO_WRAP}, |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 73 | #endif |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 74 | #ifndef DISABLE_OPERATINGDIR |
| 75 | {"operatingdir", 0}, |
| 76 | #endif |
| 77 | {"pico", PICO_MODE}, |
| 78 | #ifndef NANO_SMALL |
| 79 | {"quotestr", 0}, |
| 80 | #endif |
| 81 | #ifdef HAVE_REGEX_H |
| 82 | {"regexp", USE_REGEXP}, |
| 83 | #endif |
| 84 | #ifndef NANO_SMALL |
| 85 | {"smooth", SMOOTHSCROLL}, |
| 86 | #endif |
| 87 | #ifndef DISABLE_SPELLER |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 88 | {"speller", 0}, |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 89 | #endif |
| 90 | {"suspend", SUSPEND}, |
| 91 | {"tabsize", 0}, |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 92 | {"tempfile", TEMP_OPT}, |
| 93 | {"view", VIEW_MODE}, |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 94 | {NULL, 0} |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 95 | }; |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 96 | |
Chris Allegretta | f478f83 | 2002-01-18 21:54:35 +0000 | [diff] [blame] | 97 | static int errors = 0; |
| 98 | static int lineno = 0; |
| 99 | static char *nanorc; |
| 100 | |
Chris Allegretta | 88520c9 | 2001-05-05 17:45:54 +0000 | [diff] [blame] | 101 | /* We have an error in some part of the rcfile; put it on stderr and |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 102 | make the user hit return to continue starting up nano. */ |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 103 | void rcfile_error(const char *msg, ...) |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 104 | { |
| 105 | va_list ap; |
| 106 | |
| 107 | fprintf(stderr, "\n"); |
Chris Allegretta | 78f0fc6 | 2002-03-29 19:41:57 +0000 | [diff] [blame] | 108 | if (lineno > 0) |
| 109 | fprintf(stderr, _("Error in %s on line %d: "), nanorc, lineno); |
| 110 | |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 111 | va_start(ap, msg); |
| 112 | vfprintf(stderr, msg, ap); |
| 113 | va_end(ap); |
| 114 | fprintf(stderr, _("\nPress return to continue starting nano\n")); |
| 115 | |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 116 | while (getchar() != '\n'); |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 119 | /* Just print the error (one of many, perhaps) but don't abort, yet. */ |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 120 | void rcfile_msg(const char *msg, ...) |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 121 | { |
| 122 | va_list ap; |
| 123 | |
Chris Allegretta | f478f83 | 2002-01-18 21:54:35 +0000 | [diff] [blame] | 124 | if (!errors) { |
| 125 | errors = 1; |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 126 | fprintf(stderr, "\n"); |
| 127 | } |
| 128 | va_start(ap, msg); |
| 129 | vfprintf(stderr, msg, ap); |
| 130 | va_end(ap); |
| 131 | fprintf(stderr, "\n"); |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 134 | /* Parse the next word from the string. Returns NULL if we hit EOL. */ |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 135 | char *parse_next_word(char *ptr) |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 136 | { |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 137 | while (*ptr != ' ' && *ptr != '\t' && *ptr != '\n' && *ptr != '\0') |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 138 | ptr++; |
| 139 | |
Chris Allegretta | 13fd44b | 2002-01-02 13:59:11 +0000 | [diff] [blame] | 140 | if (*ptr == '\0') |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 141 | return NULL; |
Chris Allegretta | f478f83 | 2002-01-18 21:54:35 +0000 | [diff] [blame] | 142 | |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 143 | /* Null terminate and advance ptr */ |
| 144 | *ptr++ = 0; |
| 145 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 146 | while (*ptr == ' ' || *ptr == '\t') |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 147 | ptr++; |
| 148 | |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 149 | return ptr; |
| 150 | } |
| 151 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 152 | /* The keywords operatingdir, fill, tabsize, speller, and quotestr take |
| 153 | * an argument when set. Among these, operatingdir, speller, and |
| 154 | * quotestr have to allow tabs and spaces in the argument. Thus, if the |
| 155 | * next word starts with a ", we say it ends with the last " of the line. |
| 156 | * Otherwise, the word is interpreted as usual. That is so the arguments |
| 157 | * can contain "s too. */ |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 158 | char *parse_argument(char *ptr) |
| 159 | { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 160 | const char *ptr_bak = ptr; |
| 161 | char *last_quote = NULL; |
| 162 | |
| 163 | assert(ptr != NULL); |
| 164 | |
| 165 | if (*ptr != '"') |
| 166 | return parse_next_word(ptr); |
| 167 | |
| 168 | do { |
| 169 | ptr++; |
| 170 | if (*ptr == '"') |
| 171 | last_quote = ptr; |
| 172 | } while (*ptr != '\n' && *ptr != '\0'); |
| 173 | |
| 174 | if (last_quote == NULL) { |
| 175 | if (*ptr == '\0') |
| 176 | ptr = NULL; |
| 177 | else |
| 178 | *ptr++ = '\0'; |
| 179 | rcfile_error(_("argument %s has unterminated \""), ptr_bak); |
| 180 | } else { |
| 181 | *last_quote = '\0'; |
| 182 | ptr = last_quote + 1; |
| 183 | } |
| 184 | if (ptr != NULL) |
| 185 | while (*ptr == ' ' || *ptr == '\t') |
| 186 | ptr++; |
| 187 | return ptr; |
| 188 | } |
| 189 | |
| 190 | #ifdef ENABLE_COLOR |
| 191 | |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 192 | int colortoint(const char *colorname, int *bright) |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 193 | { |
| 194 | int mcolor = 0; |
| 195 | |
| 196 | if (colorname == NULL) |
| 197 | return -1; |
| 198 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 199 | if (!strncasecmp(colorname, "bright", 6)) { |
Chris Allegretta | 2fa11b8 | 2001-12-02 04:55:44 +0000 | [diff] [blame] | 200 | *bright = 1; |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 201 | colorname += 6; |
| 202 | } |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 203 | |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 204 | if (!strcasecmp(colorname, "green")) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 205 | mcolor = COLOR_GREEN; |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 206 | else if (!strcasecmp(colorname, "red")) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 207 | mcolor = COLOR_RED; |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 208 | else if (!strcasecmp(colorname, "blue")) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 209 | mcolor = COLOR_BLUE; |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 210 | else if (!strcasecmp(colorname, "white")) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 211 | mcolor = COLOR_WHITE; |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 212 | else if (!strcasecmp(colorname, "yellow")) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 213 | mcolor = COLOR_YELLOW; |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 214 | else if (!strcasecmp(colorname, "cyan")) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 215 | mcolor = COLOR_CYAN; |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 216 | else if (!strcasecmp(colorname, "magenta")) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 217 | mcolor = COLOR_MAGENTA; |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 218 | else if (!strcasecmp(colorname, "black")) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 219 | mcolor = COLOR_BLACK; |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 220 | else { |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 221 | rcfile_error(_("color %s not understood.\n" |
| 222 | "Valid colors are \"green\", \"red\", \"blue\", \n" |
| 223 | "\"white\", \"yellow\", \"cyan\", \"magenta\" and \n" |
| 224 | "\"black\", with the optional prefix \"bright\".\n")); |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 225 | exit(1); |
| 226 | } |
| 227 | |
| 228 | return mcolor; |
| 229 | } |
| 230 | |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 231 | char *parse_next_regex(char *ptr) |
| 232 | { |
| 233 | while ((*ptr != '"' || (*(ptr + 1) != ' ' && *(ptr + 1) != '\n')) |
| 234 | && *ptr != '\n' && *ptr != '\0') |
| 235 | ptr++; |
| 236 | |
| 237 | if (*ptr == '\0') |
| 238 | return NULL; |
| 239 | |
| 240 | /* Null terminate and advance ptr */ |
| 241 | *ptr++ = '\0'; |
| 242 | |
| 243 | while (*ptr == ' ' || *ptr == '\t') |
| 244 | ptr++; |
| 245 | |
| 246 | return ptr; |
| 247 | } |
| 248 | |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 249 | void parse_syntax(char *ptr) |
Chris Allegretta | b6c5dc2 | 2002-05-04 03:47:33 +0000 | [diff] [blame] | 250 | { |
| 251 | syntaxtype *tmpsyntax = NULL; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 252 | const char *fileregptr = NULL, *nameptr = NULL; |
Chris Allegretta | b6c5dc2 | 2002-05-04 03:47:33 +0000 | [diff] [blame] | 253 | exttype *exttmp = NULL; |
| 254 | |
| 255 | while (*ptr == ' ') |
| 256 | ptr++; |
| 257 | |
| 258 | if (*ptr == '\n' || *ptr == '\0') |
| 259 | return; |
| 260 | |
| 261 | if (*ptr != '"') { |
| 262 | rcfile_error(_("regex strings must begin and end with a \" character\n")); |
Chris Allegretta | 09900ff | 2002-05-04 04:23:30 +0000 | [diff] [blame] | 263 | return; |
Chris Allegretta | b6c5dc2 | 2002-05-04 03:47:33 +0000 | [diff] [blame] | 264 | } |
| 265 | ptr++; |
| 266 | |
| 267 | nameptr = ptr; |
| 268 | ptr = parse_next_regex(ptr); |
| 269 | |
| 270 | if (ptr == NULL) { |
| 271 | rcfile_error(_("Missing syntax name")); |
Chris Allegretta | 09900ff | 2002-05-04 04:23:30 +0000 | [diff] [blame] | 272 | return; |
Chris Allegretta | b6c5dc2 | 2002-05-04 03:47:33 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 275 | if (syntaxes == NULL) { |
| 276 | syntaxes = (syntaxtype *)nmalloc(sizeof(syntaxtype)); |
| 277 | tmpsyntax = syntaxes; |
| 278 | } else { |
| 279 | for (tmpsyntax = syntaxes; tmpsyntax->next != NULL; |
| 280 | tmpsyntax = tmpsyntax->next) |
| 281 | ; |
| 282 | tmpsyntax->next = (syntaxtype *)nmalloc(sizeof(syntaxtype)); |
| 283 | tmpsyntax = tmpsyntax->next; |
Chris Allegretta | b6c5dc2 | 2002-05-04 03:47:33 +0000 | [diff] [blame] | 284 | #ifdef DEBUG |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 285 | fprintf(stderr, _("Adding new syntax after 1st\n")); |
Chris Allegretta | b6c5dc2 | 2002-05-04 03:47:33 +0000 | [diff] [blame] | 286 | #endif |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 287 | } |
| 288 | tmpsyntax->desc = mallocstrcpy(NULL, nameptr); |
| 289 | tmpsyntax->color = NULL; |
| 290 | tmpsyntax->extensions = NULL; |
| 291 | tmpsyntax->next = NULL; |
Chris Allegretta | b6c5dc2 | 2002-05-04 03:47:33 +0000 | [diff] [blame] | 292 | #ifdef DEBUG |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 293 | fprintf(stderr, _("Starting a new syntax type\n")); |
| 294 | fprintf(stderr, "string val=%s\n", nameptr); |
Chris Allegretta | b6c5dc2 | 2002-05-04 03:47:33 +0000 | [diff] [blame] | 295 | #endif |
| 296 | |
Chris Allegretta | b6c5dc2 | 2002-05-04 03:47:33 +0000 | [diff] [blame] | 297 | /* Now load in the extensions to their part of the struct */ |
| 298 | while (*ptr != '\n' && *ptr != '\0') { |
Chris Allegretta | b6c5dc2 | 2002-05-04 03:47:33 +0000 | [diff] [blame] | 299 | while (*ptr != '"' && *ptr != '\n' && *ptr != '\0') |
| 300 | ptr++; |
| 301 | |
| 302 | if (*ptr == '\n' || *ptr == '\0') |
| 303 | return; |
| 304 | ptr++; |
| 305 | |
| 306 | fileregptr = ptr; |
| 307 | ptr = parse_next_regex(ptr); |
| 308 | |
| 309 | if (tmpsyntax->extensions == NULL) { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 310 | tmpsyntax->extensions = (exttype *)nmalloc(sizeof(exttype)); |
| 311 | exttmp = tmpsyntax->extensions; |
| 312 | } else { |
Chris Allegretta | b6c5dc2 | 2002-05-04 03:47:33 +0000 | [diff] [blame] | 313 | for (exttmp = tmpsyntax->extensions; exttmp->next != NULL; |
| 314 | exttmp = exttmp->next); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 315 | exttmp->next = (exttype *)nmalloc(sizeof(exttype)); |
| 316 | exttmp = exttmp->next; |
Chris Allegretta | b6c5dc2 | 2002-05-04 03:47:33 +0000 | [diff] [blame] | 317 | } |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 318 | exttmp->val = mallocstrcpy(NULL, fileregptr); |
| 319 | exttmp->next = NULL; |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 320 | } |
Chris Allegretta | b6c5dc2 | 2002-05-04 03:47:33 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 323 | /* Parse the color stuff into the colorstrings array */ |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 324 | void parse_colors(char *ptr) |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 325 | { |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 326 | int fg, bg, bright = 0; |
| 327 | int expectend = 0; /* Do we expect an end= line? */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 328 | char *fgstr; |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 329 | colortype *tmpcolor = NULL; |
Chris Allegretta | b6c5dc2 | 2002-05-04 03:47:33 +0000 | [diff] [blame] | 330 | syntaxtype *tmpsyntax = NULL; |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 331 | |
| 332 | fgstr = ptr; |
| 333 | ptr = parse_next_word(ptr); |
| 334 | |
| 335 | if (ptr == NULL) { |
Chris Allegretta | f478f83 | 2002-01-18 21:54:35 +0000 | [diff] [blame] | 336 | rcfile_error(_("Missing color name")); |
Chris Allegretta | 09900ff | 2002-05-04 04:23:30 +0000 | [diff] [blame] | 337 | return; |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | if (strstr(fgstr, ",")) { |
| 341 | strtok(fgstr, ","); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 342 | bg = colortoint(strtok(NULL, ","), &bright); |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 343 | } else |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 344 | bg = -1; |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 345 | |
Chris Allegretta | f478f83 | 2002-01-18 21:54:35 +0000 | [diff] [blame] | 346 | fg = colortoint(fgstr, &bright); |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 347 | |
Chris Allegretta | b6c5dc2 | 2002-05-04 03:47:33 +0000 | [diff] [blame] | 348 | if (syntaxes == NULL) { |
| 349 | rcfile_error(_("Cannot add a color directive without a syntax line")); |
Chris Allegretta | 09900ff | 2002-05-04 04:23:30 +0000 | [diff] [blame] | 350 | return; |
Chris Allegretta | b6c5dc2 | 2002-05-04 03:47:33 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | for (tmpsyntax = syntaxes; tmpsyntax->next != NULL; |
| 354 | tmpsyntax = tmpsyntax->next) |
| 355 | ; |
| 356 | |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 357 | /* Now the fun part, start adding regexps to individual strings |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 358 | in the colorstrings array, woo! */ |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 359 | |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 360 | while (*ptr != '\0') { |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 361 | while (*ptr == ' ') |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 362 | ptr++; |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 363 | |
| 364 | if (*ptr == '\n' || *ptr == '\0') |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 365 | break; |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 366 | |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 367 | if (!strncasecmp(ptr, "start=", 6)) { |
| 368 | ptr += 6; |
| 369 | expectend = 1; |
Chris Allegretta | f478f83 | 2002-01-18 21:54:35 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 372 | if (*ptr != '"') { |
| 373 | rcfile_error(_("regex strings must begin and end with a \" character\n")); |
| 374 | continue; |
| 375 | } |
| 376 | ptr++; |
| 377 | |
Chris Allegretta | b6c5dc2 | 2002-05-04 03:47:33 +0000 | [diff] [blame] | 378 | if (tmpsyntax->color == NULL) { |
| 379 | tmpsyntax->color = nmalloc(sizeof(colortype)); |
Chris Allegretta | b6c5dc2 | 2002-05-04 03:47:33 +0000 | [diff] [blame] | 380 | tmpcolor = tmpsyntax->color; |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 381 | #ifdef DEBUG |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 382 | fprintf(stderr, _("Starting a new colorstring for fg %d bg %d\n"), |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 383 | fg, bg); |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 384 | #endif |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 385 | } else { |
Chris Allegretta | b6c5dc2 | 2002-05-04 03:47:33 +0000 | [diff] [blame] | 386 | for (tmpcolor = tmpsyntax->color; |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 387 | tmpcolor->next != NULL; tmpcolor = tmpcolor->next); |
| 388 | #ifdef DEBUG |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 389 | fprintf(stderr, _("Adding new entry for fg %d bg %d\n"), fg, bg); |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 390 | #endif |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 391 | tmpcolor->next = nmalloc(sizeof(colortype)); |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 392 | tmpcolor = tmpcolor->next; |
| 393 | } |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 394 | tmpcolor->fg = fg; |
| 395 | tmpcolor->bg = bg; |
| 396 | tmpcolor->bright = bright; |
| 397 | tmpcolor->next = NULL; |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 398 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 399 | tmpcolor->start = ptr; |
| 400 | ptr = parse_next_regex(ptr); |
| 401 | tmpcolor->start = mallocstrcpy(NULL, tmpcolor->start); |
| 402 | #ifdef DEBUG |
| 403 | fprintf(stderr, _("string val=%s\n"), tmpcolor->start); |
| 404 | #endif |
| 405 | |
| 406 | if (!expectend) |
| 407 | tmpcolor->end = NULL; |
| 408 | else { |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 409 | if (ptr == NULL || strncasecmp(ptr, "end=", 4)) { |
| 410 | rcfile_error(_ |
| 411 | ("\n\t\"start=\" requires a corresponding \"end=\"")); |
| 412 | return; |
| 413 | } |
| 414 | |
| 415 | ptr += 4; |
| 416 | |
| 417 | if (*ptr != '"') { |
| 418 | rcfile_error(_ |
| 419 | ("regex strings must begin and end with a \" character\n")); |
| 420 | continue; |
| 421 | } |
| 422 | ptr++; |
| 423 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 424 | tmpcolor->end = ptr; |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 425 | ptr = parse_next_regex(ptr); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 426 | tmpcolor->end = mallocstrcpy(NULL, tmpcolor->end); |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 427 | #ifdef DEBUG |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 428 | fprintf(stderr, _("For end part, beginning = \"%s\"\n"), |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 429 | tmpcolor->end); |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 430 | #endif |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 431 | } |
Chris Allegretta | f478f83 | 2002-01-18 21:54:35 +0000 | [diff] [blame] | 432 | } |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 433 | } |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 434 | |
| 435 | #endif /* ENABLE_COLOR */ |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 436 | |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 437 | /* Parse the RC file, once it has been opened successfully */ |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 438 | void parse_rcfile(FILE *rcstream) |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 439 | { |
| 440 | char *buf, *ptr, *keyword, *option; |
Chris Allegretta | 1596d38 | 2002-03-07 00:46:17 +0000 | [diff] [blame] | 441 | int set = 0, i, j; |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 442 | |
Chris Allegretta | 8d848af | 2001-05-18 04:44:16 +0000 | [diff] [blame] | 443 | buf = charalloc(1024); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 444 | while (fgets(buf, 1023, rcstream) != 0) { |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 445 | lineno++; |
| 446 | ptr = buf; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 447 | while (*ptr == ' ' || *ptr == '\t') |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 448 | ptr++; |
| 449 | |
| 450 | if (*ptr == '\n' || *ptr == '\0') |
| 451 | continue; |
| 452 | |
| 453 | if (*ptr == '#') { |
| 454 | #ifdef DEBUG |
| 455 | fprintf(stderr, _("parse_rcfile: Read a comment\n")); |
| 456 | #endif |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 457 | continue; /* Skip past commented lines */ |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | /* Else skip to the next space */ |
| 461 | keyword = ptr; |
| 462 | ptr = parse_next_word(ptr); |
| 463 | if (!ptr) |
| 464 | continue; |
| 465 | |
| 466 | /* Else try to parse the keyword */ |
| 467 | if (!strcasecmp(keyword, "set")) |
| 468 | set = 1; |
| 469 | else if (!strcasecmp(keyword, "unset")) |
| 470 | set = -1; |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 471 | #ifdef ENABLE_COLOR |
Chris Allegretta | b6c5dc2 | 2002-05-04 03:47:33 +0000 | [diff] [blame] | 472 | else if (!strcasecmp(keyword, "syntax")) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 473 | parse_syntax(ptr); |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 474 | else if (!strcasecmp(keyword, "color")) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 475 | parse_colors(ptr); |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 476 | #endif /* ENABLE_COLOR */ |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 477 | else { |
Chris Allegretta | f478f83 | 2002-01-18 21:54:35 +0000 | [diff] [blame] | 478 | rcfile_msg(_("command %s not understood"), keyword); |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 479 | continue; |
| 480 | } |
| 481 | |
| 482 | option = ptr; |
| 483 | ptr = parse_next_word(ptr); |
| 484 | /* We don't care if ptr == NULL, as it should if using proper syntax */ |
| 485 | |
| 486 | if (set != 0) { |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 487 | for (i = 0; rcopts[i].name != NULL; i++) { |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 488 | if (!strcasecmp(option, rcopts[i].name)) { |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 489 | #ifdef DEBUG |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 490 | fprintf(stderr, _("parse_rcfile: Parsing option %s\n"), |
| 491 | rcopts[i].name); |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 492 | #endif |
| 493 | if (set == 1 || rcopts[i].flag == FOLLOW_SYMLINKS) { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 494 | if (!strcasecmp(rcopts[i].name, "tabsize") |
| 495 | #ifndef DISABLE_OPERATINGDIR |
| 496 | || !strcasecmp(rcopts[i].name, "operatingdir") |
| 497 | #endif |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 498 | #ifndef DISABLE_WRAPJUSTIFY |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 499 | || !strcasecmp(rcopts[i].name, "fill") |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 500 | #endif |
Chris Allegretta | d76ca2b | 2002-03-03 22:52:52 +0000 | [diff] [blame] | 501 | #ifndef DISABLE_JUSTIFY |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 502 | || !strcasecmp(rcopts[i].name, "quotestr") |
Chris Allegretta | d76ca2b | 2002-03-03 22:52:52 +0000 | [diff] [blame] | 503 | #endif |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 504 | #ifndef DISABLE_SPELLER |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 505 | || !strcasecmp(rcopts[i].name, "speller") |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 506 | #endif |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 507 | ) { |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 508 | if (*ptr == '\n' || *ptr == '\0') { |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 509 | rcfile_error(_ |
| 510 | ("option %s requires an argument"), |
| 511 | rcopts[i].name); |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 512 | continue; |
| 513 | } |
| 514 | option = ptr; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 515 | if (*option == '"') |
| 516 | option++; |
| 517 | ptr = parse_argument(ptr); |
| 518 | #ifdef DEBUG |
| 519 | fprintf(stderr, "option = %s\n", option); |
| 520 | #endif |
| 521 | #ifndef DISABLE_OPERATINGDIR |
| 522 | if (!strcasecmp(rcopts[i].name, "operatingdir")) |
| 523 | operating_dir = mallocstrcpy(NULL, option); |
| 524 | else |
| 525 | #endif |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 526 | #ifndef DISABLE_WRAPJUSTIFY |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 527 | if (!strcasecmp(rcopts[i].name, "fill")) { |
| 528 | char *first_error; |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 529 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 530 | /* Using strtol instead of atoi lets us |
| 531 | * accept 0 while checking other |
| 532 | * errors. */ |
| 533 | j = (int)strtol(option, &first_error, 10); |
| 534 | if (errno == ERANGE || *option == '\0' || *first_error != '\0') |
| 535 | rcfile_error(_("requested fill size %d invalid"), |
Chris Allegretta | 1596d38 | 2002-03-07 00:46:17 +0000 | [diff] [blame] | 536 | j); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 537 | else |
| 538 | wrap_at = j; |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 539 | } else |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 540 | #endif |
Chris Allegretta | d76ca2b | 2002-03-03 22:52:52 +0000 | [diff] [blame] | 541 | #ifndef DISABLE_JUSTIFY |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 542 | if (!strcasecmp(rcopts[i].name, "quotestr")) |
| 543 | quotestr = mallocstrcpy(NULL, option); |
| 544 | else |
Chris Allegretta | d76ca2b | 2002-03-03 22:52:52 +0000 | [diff] [blame] | 545 | #endif |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 546 | #ifndef DISABLE_SPELLER |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 547 | if (!strcasecmp(rcopts[i].name, "speller")) |
| 548 | alt_speller = mallocstrcpy(NULL, option); |
| 549 | else |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 550 | #endif |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 551 | { |
| 552 | char *first_error; |
| 553 | |
| 554 | /* Using strtol instead of atoi lets us |
| 555 | * accept 0 while checking other |
| 556 | * errors. */ |
| 557 | j = (int)strtol(option, &first_error, 10); |
| 558 | if (errno == ERANGE || *option == '\0' || *first_error != '\0') |
| 559 | rcfile_error(_("requested tab size %d invalid"), |
| 560 | j); |
| 561 | else |
| 562 | tabsize = j; |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 563 | } |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 564 | } else |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 565 | SET(rcopts[i].flag); |
| 566 | #ifdef DEBUG |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 567 | fprintf(stderr, _("set flag %d!\n"), |
| 568 | rcopts[i].flag); |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 569 | #endif |
| 570 | } else { |
| 571 | UNSET(rcopts[i].flag); |
| 572 | #ifdef DEBUG |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 573 | fprintf(stderr, _("unset flag %d!\n"), |
| 574 | rcopts[i].flag); |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 575 | #endif |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 576 | } |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | } |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 580 | } |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 581 | free(buf); |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 582 | if (errors) |
| 583 | rcfile_error(_("Errors found in .nanorc file")); |
| 584 | |
| 585 | return; |
| 586 | } |
| 587 | |
| 588 | /* The main rc file function, tries to open the rc file */ |
| 589 | void do_rcfile(void) |
| 590 | { |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 591 | FILE *rcstream; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 592 | const struct passwd *userage; |
| 593 | uid_t euid = geteuid(); |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 594 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 595 | #ifdef SYSCONFDIR |
| 596 | assert(sizeof(SYSCONFDIR) == strlen(SYSCONFDIR) + 1); |
| 597 | nanorc = charalloc(sizeof(SYSCONFDIR) + 7); |
Chris Allegretta | ff8a68c | 2002-02-16 20:34:57 +0000 | [diff] [blame] | 598 | sprintf(nanorc, "%s/nanorc", SYSCONFDIR); |
Chris Allegretta | ff8a68c | 2002-02-16 20:34:57 +0000 | [diff] [blame] | 599 | /* Try to open system nanorc */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 600 | if ((rcstream = fopen(nanorc, "r")) != NULL) { |
| 601 | /* Parse it! */ |
| 602 | parse_rcfile(rcstream); |
| 603 | fclose(rcstream); |
| 604 | } |
| 605 | #endif |
Chris Allegretta | ff8a68c | 2002-02-16 20:34:57 +0000 | [diff] [blame] | 606 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 607 | /* Determine home directory using getpwent(), don't rely on $HOME */ |
| 608 | do { |
| 609 | userage = getpwent(); |
| 610 | } while (userage != NULL && userage->pw_uid != euid); |
| 611 | endpwent(); |
Chris Allegretta | ff8a68c | 2002-02-16 20:34:57 +0000 | [diff] [blame] | 612 | |
Chris Allegretta | 78f0fc6 | 2002-03-29 19:41:57 +0000 | [diff] [blame] | 613 | lineno = 0; |
Chris Allegretta | 4dc03d5 | 2002-05-11 03:04:44 +0000 | [diff] [blame] | 614 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 615 | if (userage == NULL) |
Chris Allegretta | 4dc03d5 | 2002-05-11 03:04:44 +0000 | [diff] [blame] | 616 | rcfile_error(_("I can't find my home directory! Wah!")); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 617 | else { |
| 618 | nanorc = nrealloc(nanorc, strlen(userage->pw_dir) + 9); |
| 619 | sprintf(nanorc, "%s/.nanorc", userage->pw_dir); |
| 620 | |
| 621 | if ((rcstream = fopen(nanorc, "r")) == NULL) { |
| 622 | /* Don't complain about the file not existing */ |
| 623 | if (errno != ENOENT) |
| 624 | rcfile_error(_("Unable to open ~/.nanorc file, %s"), |
| 625 | strerror(errno)); |
| 626 | } else { |
| 627 | parse_rcfile(rcstream); |
| 628 | fclose(rcstream); |
| 629 | } |
Chris Allegretta | 4dc03d5 | 2002-05-11 03:04:44 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 632 | free(nanorc); |
Chris Allegretta | 8d8e012 | 2001-04-18 04:28:54 +0000 | [diff] [blame] | 633 | } |
| 634 | |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 635 | #endif /* ENABLE_NANORC */ |